RK3368 GPU version Rogue M 1.28
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / rogue_m / services / server / common / tlstream.c
1 /*************************************************************************/ /*!
2 @File
3 @Title          Transport Layer kernel side API implementation.
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description    Transport Layer API implementation.
6                 These functions are provided to driver components.
7 @License        Dual MIT/GPLv2
8
9 The contents of this file are subject to the MIT license as set out below.
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 Alternatively, the contents of this file may be used under the terms of
22 the GNU General Public License Version 2 ("GPL") in which case the provisions
23 of GPL are applicable instead of those above.
24
25 If you wish to allow use of your version of this file only under the terms of
26 GPL, and not to allow others to use your version of this file under the terms
27 of the MIT license, indicate your decision by deleting the provisions above
28 and replace them with the notice and other provisions required by GPL as set
29 out in the file called "GPL-COPYING" included in this distribution. If you do
30 not delete the provisions above, a recipient may use your version of this file
31 under the terms of either the MIT license or GPL.
32
33 This License is also included in this distribution in the file called
34 "MIT-COPYING".
35
36 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
37 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
39 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
40 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
41 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
42 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 */ /**************************************************************************/
44
45 //#define PVR_DPF_FUNCTION_TRACE_ON 1
46 #undef PVR_DPF_FUNCTION_TRACE_ON
47 #include "pvr_debug.h"
48
49 #include "allocmem.h"
50 #include "pvrsrv_error.h"
51 #include "osfunc.h"
52
53 #include "pvr_tlcommon.h"
54 #include "tlintern.h"
55 #include "tlstream.h"
56
57 /* To debug buffer utilisation enable this macro here and
58  * define PVRSRV_NEED_PVR_TRACE in the server pvr_debug.c and in tutils.c
59  * before the inclusion of pvr_debug.h. Issue pvrtutils 6 on target to see
60  * stream buffer utilisation. */
61 //#define TL_BUFFER_UTILIZATION 1
62
63 #define EVENT_OBJECT_TIMEOUT_MS 1000
64
65 /* Given the state of the buffer it returns a number of bytes that the client
66  * can use for a successful allocation. */
67 static INLINE IMG_UINT32 suggestAllocSize(IMG_UINT32 ui32LRead,
68                                                                                 IMG_UINT32 ui32LWrite, 
69                                                                                 IMG_UINT32 ui32CBSize,
70                                                                 IMG_UINT32 ui32ReqSizeMin)
71 {
72         IMG_UINT32 ui32AvSpace = 0;
73         
74         /* This could be written in fewer lines using the ? operator but it  
75                 would not be kind to potential readers of this source at all. */ 
76         if ( ui32LRead > ui32LWrite )                          /* Buffer WRAPPED */
77         {
78                 if ( (ui32LRead - ui32LWrite) > (sizeof(PVRSRVTL_PACKETHDR) + ui32ReqSizeMin + (IMG_INT) BUFFER_RESERVED_SPACE) )
79                 {
80                         ui32AvSpace =  ui32LRead - ui32LWrite - sizeof(PVRSRVTL_PACKETHDR) - (IMG_INT) BUFFER_RESERVED_SPACE;
81                 }
82         }
83         else                                                  /* Normal, no wrap */
84         {
85                 if ( (ui32CBSize - ui32LWrite) > (sizeof(PVRSRVTL_PACKETHDR) + ui32ReqSizeMin + (IMG_INT) BUFFER_RESERVED_SPACE) )
86                 {
87                         ui32AvSpace =  ui32CBSize - ui32LWrite - sizeof(PVRSRVTL_PACKETHDR) - (IMG_INT) BUFFER_RESERVED_SPACE;
88                 }
89                 else if ( (ui32LRead - 0) > (sizeof(PVRSRVTL_PACKETHDR) + ui32ReqSizeMin + (IMG_INT) BUFFER_RESERVED_SPACE) )
90                 {
91                         ui32AvSpace =  ui32LRead - sizeof(PVRSRVTL_PACKETHDR) - (IMG_INT) BUFFER_RESERVED_SPACE;
92                 }
93         }
94     /* The max size of a TL packet currently is UINT16. adjust accordingly */
95         return MIN(ui32AvSpace, IMG_UINT16_MAX);
96 }
97
98 /* Returns bytes left in the buffer. Negative if there is not any.
99  * two 4b aligned values are reserved, one for the write failed buffer flag
100  * and one to be able to distinguish the buffer full state to the buffer
101  * empty state.
102  * Always returns free space -8 even when the "write failed" packet may be
103  * already in the stream before this write. */
104 static INLINE IMG_INT
105 cbSpaceLeft(IMG_UINT32 ui32Read, IMG_UINT32 ui32Write, IMG_UINT32 ui32size)
106 {
107         /* We need to reserve 4b (one packet) in the buffer to be able to tell empty 
108          * buffers from full buffers and one more for packet write fail packet */
109         if ( ui32Read > ui32Write )
110         {
111                 return (IMG_INT) ui32Read - (IMG_INT)ui32Write - (IMG_INT) BUFFER_RESERVED_SPACE;
112         }
113         else
114         {
115                 return (IMG_INT)ui32size - ((IMG_INT)ui32Write - (IMG_INT)ui32Read) - (IMG_INT) BUFFER_RESERVED_SPACE;
116         }
117 }   
118
119 /******************************************************************************* 
120  * TL Server public API implementation.
121  ******************************************************************************/
122 PVRSRV_ERROR
123 TLStreamCreate(IMG_HANDLE *phStream,
124                            IMG_CHAR *szStreamName,
125                            IMG_UINT32 ui32Size,
126                            IMG_UINT32 ui32StreamFlags,
127                TL_STREAM_SOURCECB pfProducerCB,
128                IMG_PVOID pvProducerUD)
129 {
130         PTL_STREAM     psTmp;
131         PVRSRV_ERROR   eError;
132         IMG_HANDLE     hEventList;
133         PTL_SNODE      psn = 0;
134         IMG_CHAR       pszBufferLabel[PRVSRVTL_MAX_STREAM_NAME_SIZE+20];
135
136         DEVMEM_FLAGS_T uiMemFlags =  PVRSRV_MEMALLOCFLAG_CPU_READABLE |
137                                                                  PVRSRV_MEMALLOCFLAG_CPU_WRITEABLE | 
138                                                                  PVRSRV_MEMALLOCFLAG_GPU_READABLE |
139                                                                  PVRSRV_MEMALLOCFLAG_GPU_WRITEABLE |
140                                                                  PVRSRV_MEMALLOCFLAG_CPU_CACHE_INCOHERENT | /* CPU only */
141                                                                  PVRSRV_MEMALLOCFLAG_ZERO_ON_ALLOC;
142
143         PVR_DPF_ENTERED;
144         /* Sanity checks:  */
145         /* non NULL handler required */
146         if ( NULL == phStream ) 
147         { 
148                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
149         }
150         if (OSStringLength(szStreamName) >= PRVSRVTL_MAX_STREAM_NAME_SIZE) 
151         {
152                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
153         }
154
155         /* Acquire TL_GLOBAL_DATA lock here because, if the following TLFindStreamNodeByName()
156          * returns NULL, a new TL_SNODE will be added to TL_GLOBAL_DATA's TL_SNODE list */
157         OSLockAcquire (TLGGD()->hTLGDLock);
158         
159         /* Check if there already exists a stream with this name. */
160         psn = TLFindStreamNodeByName( szStreamName );
161         if ( IMG_NULL != psn )
162         {
163                 eError = PVRSRV_ERROR_ALREADY_EXISTS;
164                 goto e0;
165         }
166         
167         /* Allocate stream structure container (stream struct) for the new stream */
168         psTmp = OSAllocZMem(sizeof(TL_STREAM)) ;
169         if ( NULL == psTmp ) 
170         {
171                 eError = PVRSRV_ERROR_OUT_OF_MEMORY;
172                 goto e0;
173         }
174
175         OSStringCopy(psTmp->szName, szStreamName);
176
177         if ( ui32StreamFlags & TL_FLAG_FORCE_FLUSH )
178         {
179                 psTmp->bWaitForEmptyOnDestroy = IMG_TRUE;
180         }
181
182         psTmp->bNoSignalOnCommit = (ui32StreamFlags&TL_FLAG_NO_SIGNAL_ON_COMMIT) ?  IMG_TRUE : IMG_FALSE;
183
184         if ( ui32StreamFlags & TL_FLAG_DROP_DATA ) 
185         {
186                 if ( ui32StreamFlags & TL_FLAG_BLOCKING_RESERVE ) 
187                 {
188                         eError = PVRSRV_ERROR_INVALID_PARAMS;
189                         goto e1;
190                 }
191                 psTmp->bDrop = IMG_TRUE;
192         }
193         else if ( ui32StreamFlags & TL_FLAG_BLOCKING_RESERVE ) 
194     {   /* Additional synchronization object required for this kind of stream */
195         psTmp->bBlock = IMG_TRUE;
196
197                 eError = OSEventObjectCreate(NULL, &psTmp->hProducerEventObj);
198                 if (eError != PVRSRV_OK)
199                 {
200                         goto e1;
201                 }
202                 /* Create an event handle for this kind of stream */
203                 eError = OSEventObjectOpen(psTmp->hProducerEventObj, &psTmp->hProducerEvent);
204                 if (eError != PVRSRV_OK)
205                 {
206                         goto e2;
207                 }
208     }
209
210         /* Remember producer supplied CB and data for later */
211         psTmp->pfProducerCallback = (IMG_VOID(*)(IMG_VOID))pfProducerCB;
212         psTmp->pvProducerUserData = pvProducerUD;
213
214         /* Round the requested bytes to a multiple of array elements' size, eg round 3 to 4 */
215         psTmp->ui32Size = PVRSRVTL_ALIGN(ui32Size);
216         psTmp->ui32Read = 0;
217         psTmp->ui32Write = 0;
218         psTmp->ui32Pending = NOTHING_PENDING;
219
220         OSSNPrintf(pszBufferLabel, sizeof(pszBufferLabel), "TLStreamBuf-%s", szStreamName);
221
222         /* Allocate memory for the circular buffer and export it to user space. */
223         eError = DevmemAllocateExportable( IMG_NULL,
224                                                                            (IMG_HANDLE) TLGetGlobalRgxDevice(),
225                                                                            (IMG_DEVMEM_SIZE_T)psTmp->ui32Size,
226                                                                            (IMG_DEVMEM_ALIGN_T) OSGetPageSize(),
227                                                                            uiMemFlags | PVRSRV_MEMALLOCFLAG_KERNEL_CPU_MAPPABLE,
228                                                                            pszBufferLabel,
229                                                                            &psTmp->psStreamMemDesc);
230         PVR_LOGG_IF_ERROR(eError, "DevmemAllocateExportable", e3);
231
232         eError = DevmemAcquireCpuVirtAddr( psTmp->psStreamMemDesc, (IMG_VOID**) &psTmp->pbyBuffer );
233         PVR_LOGG_IF_ERROR(eError, "DevmemAcquireCpuVirtAddr", e4);
234
235         eError = DevmemExport(psTmp->psStreamMemDesc, &(psTmp->sExportCookie));
236         PVR_LOGG_IF_ERROR(eError, "DevmemExport", e5);
237
238         /* Synchronization object to synchronize with user side data transfers. */
239         eError = OSEventObjectCreate(psTmp->szName, &hEventList);
240         if (eError != PVRSRV_OK)
241         {
242                 goto e6;
243         }
244
245         eError = OSLockCreate (&psTmp->hStreamLock, LOCK_TYPE_PASSIVE);
246         if (eError != PVRSRV_OK)
247         {
248                 goto e7;
249         }
250
251         /* Now remember the stream in the global TL structures */
252         psn = TLMakeSNode(hEventList, (TL_STREAM *)psTmp, 0);
253         if (psn == NULL)
254         {
255                 eError=PVRSRV_ERROR_OUT_OF_MEMORY;
256                 goto e8;
257         }
258
259         /* Stream node created, now reset the write reference count to 1
260          * (i.e. this context's reference) */
261         psn->uiWRefCount = 1;
262
263         TLAddStreamNode(psn);
264
265         /* Release TL_GLOBAL_DATA lock as the new TL_SNODE is now added to the list */
266         OSLockRelease (TLGGD()->hTLGDLock);
267
268         /* Best effort signal, client wait timeout will ultimately let it find the
269          * new stream if this fails, acceptable to avoid cleanup as it is tricky
270          * at this point */
271         (void) OSEventObjectSignal(TLGGD()->hTLEventObj);
272
273         /* Pass the newly created stream handle back to caller */
274         *phStream = (IMG_HANDLE)psTmp;
275         PVR_DPF_RETURN_OK;
276
277 e8:
278         OSLockDestroy(psTmp->hStreamLock);
279 e7:
280         OSEventObjectDestroy(hEventList);
281 e6:
282         DevmemUnexport(psTmp->psStreamMemDesc, &(psTmp->sExportCookie));
283 e5:
284         DevmemReleaseCpuVirtAddr( psTmp->psStreamMemDesc );
285 e4:
286         DevmemFree(psTmp->psStreamMemDesc);
287 e3:
288         OSEventObjectClose(psTmp->hProducerEvent);
289 e2:
290         OSEventObjectDestroy(psTmp->hProducerEventObj);
291 e1:
292         OSFREEMEM(psTmp);
293 e0:
294         OSLockRelease (TLGGD()->hTLGDLock);
295
296         PVR_DPF_RETURN_RC(eError);
297 }
298
299 PVRSRV_ERROR
300 TLStreamOpen(IMG_HANDLE *phStream,
301              IMG_CHAR   *szStreamName)
302 {
303         PTL_SNODE  psTmpSNode;
304
305         PVR_DPF_ENTERED;
306
307         if ( IMG_NULL == phStream || IMG_NULL == szStreamName )
308         {
309                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
310         }
311
312         /* Acquire the TL_GLOBAL_DATA lock first to ensure,
313          * the TL_STREAM while returned and being modified,
314          * is not deleted by some other context */
315         OSLockAcquire (TLGGD()->hTLGDLock);
316         
317         /* Search for a stream node with a matching stream name */
318         psTmpSNode = TLFindStreamNodeByName(szStreamName);
319
320         if ( IMG_NULL == psTmpSNode )
321         {
322                 OSLockRelease (TLGGD()->hTLGDLock);
323                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_NOT_FOUND);
324         }
325
326         /* The TL_SNODE->uiWRefCount governs the presence of this node in the
327          * TL_GLOBAL_DATA list i.e. when uiWRefCount falls to zero we try removing
328          * this node from the TL_GLOBAL_DATA list. Hence, is protected using the
329          * TL_GLOBAL_DATA lock and not TL_STREAM lock */
330         psTmpSNode->uiWRefCount++;
331         
332         OSLockRelease (TLGGD()->hTLGDLock);
333
334         /* Return the stream handle to the caller */
335         *phStream = (IMG_HANDLE)psTmpSNode->psStream;
336
337         PVR_DPF_RETURN_VAL(PVRSRV_OK);
338 }
339
340 IMG_VOID 
341 TLStreamClose(IMG_HANDLE hStream)
342 {
343         PTL_STREAM      psTmp;
344         IMG_BOOL        bDestroyStream;
345
346         PVR_DPF_ENTERED;
347
348         if ( IMG_NULL == hStream )
349         {
350                 PVR_DPF((PVR_DBG_WARNING,
351                                  "TLStreamClose failed as NULL stream handler passed, nothing done.\n"));
352                 PVR_DPF_RETURN;
353         }
354
355         psTmp = (PTL_STREAM)hStream;
356
357         /* Acquire TL_GLOBAL_DATA lock for updating the reference count as this will be required 
358          * in-case this TL_STREAM node is to be deleted */
359         OSLockAcquire (TLGGD()->hTLGDLock);
360         
361         /* Decrement write reference counter of the stream */
362         psTmp->psNode->uiWRefCount--;
363
364         if ( 0 != psTmp->psNode->uiWRefCount )
365         {       /* The stream is still being used in other context(s) do not destroy anything */
366                 OSLockRelease (TLGGD()->hTLGDLock);
367                 PVR_DPF_RETURN;
368         }
369         else
370         {
371                 /* Now we try removing this TL_STREAM from TL_GLOBAL_DATA */
372
373                 if ( psTmp->bWaitForEmptyOnDestroy == IMG_TRUE )
374                 {
375                         /* We won't require the TL_STREAM lock to be acquired here for accessing its read
376                          * and write offsets. REASON: We are here because there is no producer context
377                          * referencing this TL_STREAM, hence its ui32Write offset won't be changed now.
378                          * Also, the updation of ui32Read offset is not protected by locks */
379                         while (psTmp->ui32Read != psTmp->ui32Write)
380                         {
381                                 /* Release lock before sleeping */
382                                 OSLockRelease (TLGGD()->hTLGDLock);
383                                 
384                                 OSEventObjectWaitTimeout(psTmp->hProducerEvent, EVENT_OBJECT_TIMEOUT_MS);
385                                 
386                                 OSLockAcquire (TLGGD()->hTLGDLock);
387
388                                 /* Ensure destruction of stream is still required */
389                                 if (0 != psTmp->psNode->uiWRefCount)
390                                 {
391                                         OSLockRelease (TLGGD()->hTLGDLock);
392                                         PVR_DPF_RETURN;
393                                 }
394                         }
395                 }
396
397                 /* Try removing the stream from TL_GLOBAL_DATA */
398                 bDestroyStream = TLTryRemoveStreamAndFreeStreamNode (psTmp->psNode);
399                 
400                 OSLockRelease (TLGGD()->hTLGDLock);
401                 
402                 if (bDestroyStream)
403                 {
404                         /* Destroy the stream if it was removed from TL_GLOBAL_DATA */
405                         TLStreamDestroy (psTmp);
406                         psTmp = IMG_NULL;
407                 }
408                 PVR_DPF_RETURN;
409         }
410 }
411
412 static PVRSRV_ERROR
413 DoTLStreamReserve(IMG_HANDLE hStream,
414                                 IMG_UINT8 **ppui8Data, 
415                                 IMG_UINT32 ui32ReqSize,
416                 IMG_UINT32 ui32ReqSizeMin,
417                                 PVRSRVTL_PACKETTYPE ePacketType,
418                                 IMG_UINT32* pui32AvSpace)
419 {
420         PTL_STREAM psTmp;
421         IMG_UINT32 *pui32Buf, ui32LRead, ui32LWrite, ui32LPending, lReqSizeAligned, lReqSizeActual;
422         IMG_INT pad, iFreeSpace;
423
424         PVR_DPF_ENTERED;
425         if (pui32AvSpace) *pui32AvSpace = 0;
426
427         if (( IMG_NULL == hStream ))
428         {
429                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
430         }
431         psTmp = (PTL_STREAM)hStream;
432
433         /* Assert used as the packet type parameter is currently only provided
434          * by the TL APIs, not the calling client */
435         PVR_ASSERT((PVRSRVTL_PACKETTYPE_UNDEF < ePacketType) && (PVRSRVTL_PACKETTYPE_LAST >= ePacketType));
436
437         /* The buffer is only used in "rounded" (aligned) chunks */
438         lReqSizeAligned = PVRSRVTL_ALIGN(ui32ReqSize);
439
440         /* Lock the stream before reading it's pending value, because if pending is set
441          * to NOTHING_PENDING, we update the pending value such that subsequent calls to
442          * this function from other context(s) fail with PVRSRV_ERROR_NOT_READY */
443         OSLockAcquire (psTmp->hStreamLock);
444
445         /* Get a local copy of the stream buffer parameters */
446         ui32LRead  = psTmp->ui32Read ;
447         ui32LWrite = psTmp->ui32Write ;
448         ui32LPending = psTmp->ui32Pending ;
449
450         /*  Multiple pending reserves are not supported. */
451         if ( NOTHING_PENDING != ui32LPending )
452         {
453                 OSLockRelease (psTmp->hStreamLock);
454                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_NOT_READY);
455         }
456
457         if ( IMG_UINT16_MAX < lReqSizeAligned )
458         {
459                 psTmp->ui32Pending = NOTHING_PENDING;
460                 if (pui32AvSpace)
461                 {
462                         *pui32AvSpace = suggestAllocSize(ui32LRead, ui32LWrite, psTmp->ui32Size, ui32ReqSizeMin);
463                 }
464                 OSLockRelease (psTmp->hStreamLock);
465                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_FULL);
466         }
467
468         /* Prevent other threads from entering this region before we are done updating
469          * the pending value and write offset (incase of padding). This is not exactly 
470          * a lock but a signal for other contexts that there is a TLStreamCommit operation
471          * pending on this stream */
472         psTmp->ui32Pending = 0;
473
474         OSLockRelease (psTmp->hStreamLock);
475
476         /* If there is enough contiguous space following the current Write
477          * position then no padding is required */
478         if (  psTmp->ui32Size
479                 < ui32LWrite + lReqSizeAligned + sizeof(PVRSRVTL_PACKETHDR) )
480         {
481                 pad = psTmp->ui32Size - ui32LWrite;
482         }
483         else
484         {
485                 pad = 0 ;
486         }
487
488         lReqSizeActual = lReqSizeAligned + sizeof(PVRSRVTL_PACKETHDR) + pad ;
489         /* If this is a blocking reserve and there is not enough space then wait. */
490         if( psTmp->bBlock )
491         {
492                 if( psTmp->ui32Size < lReqSizeActual )
493                 {
494                         /* Acquire stream lock for updating pending value */
495                         OSLockAcquire (psTmp->hStreamLock);
496                         psTmp->ui32Pending = NOTHING_PENDING;
497                         OSLockRelease (psTmp->hStreamLock);
498                         
499                         PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_MISUSE);
500                 }
501                 while ( ( cbSpaceLeft(ui32LRead, ui32LWrite, psTmp->ui32Size)
502                          <(IMG_INT) lReqSizeActual ) )
503                 {
504                         OSEventObjectWait(psTmp->hProducerEvent);
505                         // update local copies.
506                         ui32LRead  = psTmp->ui32Read ;
507                         ui32LWrite = psTmp->ui32Write ;
508                 }
509         }
510
511         iFreeSpace = cbSpaceLeft(ui32LRead, ui32LWrite, psTmp->ui32Size);
512         
513         /* The easy case: buffer has enough space to hold the requested packet (data + header) */
514         if (  iFreeSpace >=(IMG_INT) lReqSizeActual )
515         {
516                 if ( pad ) 
517                 { 
518                         /* Inserting padding packet. */
519                         pui32Buf = (IMG_UINT32*)&psTmp->pbyBuffer[ui32LWrite];
520                         *pui32Buf = PVRSRVTL_SET_PACKET_PADDING(pad-sizeof(PVRSRVTL_PACKETHDR)) ;
521
522                         /* CAUTION: the used pad value should always result in a properly 
523                          *          aligned ui32LWrite pointer, which in this case is 0 */
524                         ui32LWrite = (ui32LWrite + pad) % psTmp->ui32Size;
525                         /* Detect unaligned pad value */
526                         PVR_ASSERT( ui32LWrite == 0);
527                 }
528                 /* Insert size-stamped packet header */
529                 pui32Buf = (IMG_UINT32*) &psTmp->pbyBuffer[ui32LWrite];
530
531                 *pui32Buf = PVRSRVTL_SET_PACKET_HDR(ui32ReqSize, ePacketType);
532
533                 /* return the next position in the buffer to the user */
534                 *ppui8Data =  &psTmp->pbyBuffer[ ui32LWrite+sizeof(PVRSRVTL_PACKETHDR) ] ;
535
536                 /* update pending offset: size stamp + data  */
537                 ui32LPending = lReqSizeAligned + sizeof(PVRSRVTL_PACKETHDR) ;
538         }
539         /* The not so easy case: not enough space, decide how to handle data */
540         else
541         {
542
543 #if defined(DEBUG)
544                 /* Sanity check that the user is not trying to add more data than the
545                  * buffer size. Conditionally compile it out to ensure this check has
546                  * no impact to release performance */
547                 if ( lReqSizeAligned+sizeof(PVRSRVTL_PACKETHDR) > psTmp->ui32Size )
548                 {
549                         OSLockAcquire (psTmp->hStreamLock);
550                         psTmp->ui32Pending = NOTHING_PENDING;
551                         OSLockRelease (psTmp->hStreamLock);
552                         PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_MISUSE);
553                 }
554 #endif
555
556                 /* No data overwriting, insert write_failed flag and return */
557                 if (psTmp->bDrop) 
558                 {
559                         /* Caller should not try to use ppui8Data,
560                          * NULLify to give user a chance of avoiding memory corruption */
561                         ppui8Data = IMG_NULL;
562
563                         /* This flag should not be inserted two consecutive times, so 
564                          * check the last ui32 in case it was a packet drop packet. */
565                         pui32Buf =  ui32LWrite 
566                                           ? 
567                                             (IMG_UINT32*)&psTmp->pbyBuffer[ui32LWrite - sizeof(PVRSRVTL_PACKETHDR)]
568                                            : // Previous four bytes are not guaranteed to be a packet header...
569                                             (IMG_UINT32*)&psTmp->pbyBuffer[psTmp->ui32Size - PVRSRVTL_PACKET_ALIGNMENT];
570
571                         if ( PVRSRVTL_PACKETTYPE_MOST_RECENT_WRITE_FAILED
572                                  != 
573                                  GET_PACKET_TYPE( (PVRSRVTL_PACKETHDR*)pui32Buf ) )
574                         {
575                                 /* Insert size-stamped packet header */
576                                 pui32Buf = (IMG_UINT32*)&psTmp->pbyBuffer[ui32LWrite];
577                                 *pui32Buf = PVRSRVTL_SET_PACKET_WRITE_FAILED ;
578                                 ui32LWrite += sizeof(PVRSRVTL_PACKETHDR);
579                                 ui32LWrite %= psTmp->ui32Size;
580                                 iFreeSpace -= sizeof(PVRSRVTL_PACKETHDR);
581                         }
582
583                         OSLockAcquire (psTmp->hStreamLock);
584                         psTmp->ui32Write = ui32LWrite;
585                         psTmp->ui32Pending = NOTHING_PENDING;
586                         OSLockRelease (psTmp->hStreamLock);
587                         
588                         if (pui32AvSpace)
589                         {
590                                 *pui32AvSpace = suggestAllocSize(ui32LRead, ui32LWrite, psTmp->ui32Size, ui32ReqSizeMin);
591                         }
592                         PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_FULL);
593                 } 
594         }
595
596         /* Acquire stream lock for updating stream parameters */
597         OSLockAcquire (psTmp->hStreamLock);
598         psTmp->ui32Write = ui32LWrite ;
599         psTmp->ui32Pending = ui32LPending ;
600         OSLockRelease (psTmp->hStreamLock);
601
602         PVR_DPF_RETURN_OK;
603 }
604
605 PVRSRV_ERROR
606 TLStreamReserve(IMG_HANDLE hStream,
607                                 IMG_UINT8 **ppui8Data,
608                                 IMG_UINT32 ui32Size)
609 {
610         return DoTLStreamReserve(hStream, ppui8Data, ui32Size, ui32Size, PVRSRVTL_PACKETTYPE_DATA, NULL);
611 }
612
613 PVRSRV_ERROR
614 TLStreamReserve2(IMG_HANDLE hStream,
615                 IMG_UINT8  **ppui8Data,
616                 IMG_UINT32 ui32Size,
617                 IMG_UINT32 ui32SizeMin,
618                 IMG_UINT32* pui32Available)
619 {
620         return DoTLStreamReserve(hStream, ppui8Data, ui32Size, ui32SizeMin, PVRSRVTL_PACKETTYPE_DATA, pui32Available);
621 }
622
623 PVRSRV_ERROR
624 TLStreamCommit(IMG_HANDLE hStream, IMG_UINT32 ui32ReqSize)
625 {
626         PTL_STREAM psTmp;
627         IMG_UINT32 ui32LRead, ui32OldWrite, ui32LWrite, ui32LPending;
628         PVRSRV_ERROR eError;
629
630         PVR_DPF_ENTERED;
631
632         if ( IMG_NULL == hStream )
633         {
634                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
635         }
636         psTmp = (PTL_STREAM)hStream;
637
638         /* Get a local copy of the stream buffer parameters */
639         ui32LRead = psTmp->ui32Read ;
640         ui32LWrite = psTmp->ui32Write ;
641         ui32LPending = psTmp->ui32Pending ;
642
643         ui32OldWrite = ui32LWrite;
644
645         // Space in buffer is aligned
646         ui32ReqSize = PVRSRVTL_ALIGN(ui32ReqSize);
647
648         /* Sanity check. ReqSize + packet header size. */
649         if ( ui32LPending != ui32ReqSize + sizeof(PVRSRVTL_PACKETHDR) )
650         {
651                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_MISUSE);
652         }
653
654         /* Update pointer to written data. */
655         ui32LWrite = (ui32LWrite + ui32LPending) % psTmp->ui32Size;
656
657         /* and reset LPending to 0 since data are now submitted  */
658         ui32LPending = NOTHING_PENDING;
659
660         /* Calculate high water mark for debug purposes */
661 #if defined(TL_BUFFER_UTILIZATION)
662         {
663                 IMG_UINT32 tmp = 0;
664                 if (ui32LWrite > ui32LRead)
665                 {
666                         tmp = (ui32LWrite-ui32LRead);
667                 }
668                 else if (ui32LWrite < ui32LRead)
669                 {
670                         tmp = (psTmp->ui32Size-ui32LRead+ui32LWrite);
671                 } /* else equal, ignore */
672
673                 if (tmp > psTmp->ui32BufferUt)
674                 {
675                         psTmp->ui32BufferUt = tmp;
676                 }
677         }
678 #endif
679
680         /* Acquire stream lock to ensure other context(s) (if any)
681          * wait on the lock (in DoTLStreamReserve) for consistent values
682          * of write offset and pending value */
683         OSLockAcquire (psTmp->hStreamLock);
684
685         /* Update stream buffer parameters to match local copies */
686         psTmp->ui32Write = ui32LWrite ;
687         psTmp->ui32Pending = ui32LPending ;
688
689         OSLockRelease (psTmp->hStreamLock);
690
691         /* If  we have transitioned from an empty buffer to a non-empty buffer,
692          * signal any consumers that may be waiting */
693         if (ui32OldWrite == ui32LRead && !psTmp->bNoSignalOnCommit)
694         {
695                 /* Signal consumers that may be waiting */
696                 eError = OSEventObjectSignal(psTmp->psNode->hDataEventObj);
697                 if ( eError != PVRSRV_OK)
698                 {
699                         PVR_DPF_RETURN_RC(eError);
700                 }
701         }
702         
703         PVR_DPF_RETURN_OK;
704 }
705
706 PVRSRV_ERROR
707 TLStreamWrite(IMG_HANDLE hStream, IMG_UINT8 *pui8Src, IMG_UINT32 ui32Size)
708 {
709         IMG_BYTE *pbyDest = IMG_NULL;
710         PVRSRV_ERROR eError;
711
712         PVR_DPF_ENTERED;
713
714         if ( IMG_NULL == hStream )
715         {
716                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
717         }
718
719         eError = TLStreamReserve(hStream, &pbyDest, ui32Size);
720         if ( PVRSRV_OK != eError ) 
721         {       
722                 PVR_DPF_RETURN_RC(eError);
723         }
724         else
725         {
726                 PVR_ASSERT ( pbyDest != NULL );
727                 OSMemCopy((IMG_VOID*)pbyDest, (IMG_VOID*)pui8Src, ui32Size);
728                 eError = TLStreamCommit(hStream, ui32Size);
729                 if ( PVRSRV_OK != eError ) 
730                 {       
731                         PVR_DPF_RETURN_RC(eError);
732                 }
733         }
734         PVR_DPF_RETURN_OK;
735 }
736
737 IMG_VOID TLStreamInfo(PTL_STREAM_INFO psInfo)
738 {
739         IMG_DEVMEM_SIZE_T actual_req_size;
740         IMG_DEVMEM_ALIGN_T align = 4; /* Low dummy value so the real value can be obtained */
741
742         actual_req_size = 2; 
743         DevmemExportalignAdjustSizeAndAlign(IMG_NULL, &actual_req_size, &align);
744
745         psInfo->headerSize = sizeof(PVRSRVTL_PACKETHDR);
746         psInfo->minReservationSize = sizeof(IMG_UINT32);
747         psInfo->pageSize = (IMG_UINT32)(actual_req_size);
748         psInfo->pageAlign = (IMG_UINT32)(align);
749 }
750
751 PVRSRV_ERROR
752 TLStreamMarkEOS(IMG_HANDLE psStream)
753 {
754         PVRSRV_ERROR eError;
755         IMG_UINT8* pData;
756
757         PVR_DPF_ENTERED;
758
759         if ( IMG_NULL == psStream )
760         {
761                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
762         }
763
764         eError = DoTLStreamReserve(psStream, &pData, 0, 0, PVRSRVTL_PACKETTYPE_MARKER_EOS, NULL);
765         if ( PVRSRV_OK !=  eError )
766         {
767                 PVR_DPF_RETURN_RC(eError);
768         }
769
770         PVR_DPF_RETURN_RC(TLStreamCommit(psStream, 0));
771 }
772
773 PVRSRV_ERROR
774 TLStreamSync(IMG_HANDLE psStream)
775 {
776         PVRSRV_ERROR eError = PVRSRV_OK;
777         PTL_STREAM   psTmp;
778
779         PVR_DPF_ENTERED;
780
781         if ( IMG_NULL == psStream )
782         {
783                 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
784         }
785         psTmp = (PTL_STREAM)psStream;
786         
787         /* Signal clients only when data is available to read */
788         if (psTmp->ui32Read != psTmp->ui32Write)
789         {
790                 eError = OSEventObjectSignal(psTmp->psNode->hDataEventObj);
791         }
792
793         PVR_DPF_RETURN_RC(eError);
794 }
795
796 /*
797  * Internal stream APIs to server part of Transport Layer, declared in
798  * header tlintern.h. Direct pointers to stream objects are used here as
799  * these functions are internal.
800  */
801 IMG_UINT32
802 TLStreamAcquireReadPos(PTL_STREAM psStream, IMG_UINT32* puiReadOffset)
803 {
804         IMG_UINT32 uiReadLen = 0;
805         IMG_UINT32 ui32LRead, ui32LWrite;
806
807         PVR_DPF_ENTERED;
808
809         PVR_ASSERT(psStream);
810         PVR_ASSERT(puiReadOffset);
811
812         /* Grab a local copy */
813         ui32LRead = psStream->ui32Read;
814         ui32LWrite = psStream->ui32Write;
815
816         /* No data available and CB defined - try and get data */
817         if ((ui32LRead == ui32LWrite) && psStream->pfProducerCallback)
818         {
819                 PVRSRV_ERROR eRc;
820                 IMG_UINT32   ui32Resp = 0;
821
822                 eRc = ((TL_STREAM_SOURCECB)psStream->pfProducerCallback)(psStream, TL_SOURCECB_OP_CLIENT_EOS,
823                                 &ui32Resp, psStream->pvProducerUserData);
824                 PVR_LOG_IF_ERROR(eRc, "TLStream->pfProducerCallback");
825
826                 ui32LWrite = psStream->ui32Write;
827         }
828
829         /* No data available... */
830         if (ui32LRead == ui32LWrite)
831         {
832                 PVR_DPF_RETURN_VAL(0);
833         }
834
835         /* Data is available to read... */
836         *puiReadOffset = ui32LRead;
837
838         /*PVR_DPF((PVR_DBG_VERBOSE,
839          *              "TLStreamAcquireReadPos Start before: Write:%d, Read:%d, size:%d",
840          *              ui32LWrite, ui32LRead, psStream->ui32Size));
841          */
842
843         if ( ui32LRead > ui32LWrite )
844         {       /* CB has wrapped around. 
845                  * Return the first contiguous piece of memory, ie [ReadLen,EndOfBuffer]
846                  * and let a subsequent AcquireReadPos read the rest of the Buffer */
847                 /*PVR_DPF((PVR_DBG_VERBOSE, "TLStreamAcquireReadPos buffer has wrapped"));*/
848                 uiReadLen = psStream->ui32Size - ui32LRead;
849         }
850         else
851         {       // CB has not wrapped
852                 uiReadLen = ui32LWrite - ui32LRead;
853         }
854
855         PVR_DPF_RETURN_VAL(uiReadLen);
856 }
857
858 IMG_VOID
859 TLStreamAdvanceReadPos(PTL_STREAM psStream, IMG_UINT32 uiReadLen)
860 {
861         PVR_DPF_ENTERED;
862
863         PVR_ASSERT(psStream);
864
865         /* Update the read offset by the length provided in a circular manner.
866          * Assuming the updation to be atomic hence, avoiding use of locks */
867         psStream->ui32Read = (psStream->ui32Read + uiReadLen) % psStream->ui32Size;
868
869         /* If this is a blocking reserve stream, 
870          * notify reserves that may be pending */
871         if(psStream->bBlock)
872         {
873                 PVRSRV_ERROR eError;
874                 eError = OSEventObjectSignal(psStream->hProducerEventObj);
875                 if ( eError != PVRSRV_OK)
876                 {
877                         PVR_DPF((PVR_DBG_WARNING,
878                                          "Error in TLStreamAdvanceReadPos: OSEventObjectSignal returned:%u",
879                                          eError));
880                 }
881         }
882
883         PVR_DPF((PVR_DBG_VERBOSE,
884                          "TLStreamAdvanceReadPos Read now at: %d",
885                         psStream->ui32Read));
886         PVR_DPF_RETURN;
887 }
888
889 IMG_VOID
890 TLStreamDestroy (PTL_STREAM psStream)
891 {
892         PVR_ASSERT (psStream);
893         
894         OSLockDestroy (psStream->hStreamLock);
895
896         /* If block-while-reserve stream, the stream's hProducerEvent and hProducerEventObj
897          * need to be cleaned as well */
898         if ( IMG_TRUE == psStream->bBlock ) 
899         {
900                 OSEventObjectClose(psStream->hProducerEvent);
901                 OSEventObjectDestroy(psStream->hProducerEventObj);
902         }
903
904         DevmemUnexport(psStream->psStreamMemDesc, &psStream->sExportCookie);
905         DevmemReleaseCpuVirtAddr(psStream->psStreamMemDesc);
906         DevmemFree(psStream->psStreamMemDesc);
907         OSFREEMEM(psStream);
908 }
909
910 DEVMEM_EXPORTCOOKIE*
911 TLStreamGetBufferCookie(PTL_STREAM psStream)
912 {
913         PVR_DPF_ENTERED;
914
915         PVR_ASSERT(psStream);
916
917         PVR_DPF_RETURN_VAL(&psStream->sExportCookie);
918 }
919
920 IMG_BOOL
921 TLStreamEOS(PTL_STREAM psStream)
922 {
923         PVR_DPF_ENTERED;
924
925         PVR_ASSERT(psStream);
926
927         /* If both pointers are equal then the buffer is empty */
928         PVR_DPF_RETURN_VAL( psStream->ui32Read == psStream->ui32Write );
929 }
930