1 /*************************************************************************/ /*!
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
9 The contents of this file are subject to the MIT license as set out below.
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:
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
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.
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.
33 This License is also included in this distribution in the file called
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 */ /**************************************************************************/
45 //#define PVR_DPF_FUNCTION_TRACE_ON 1
46 #undef PVR_DPF_FUNCTION_TRACE_ON
47 #include "pvr_debug.h"
50 #include "pvrsrv_error.h"
53 #include "pvr_tlcommon.h"
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
63 #define EVENT_OBJECT_TIMEOUT_MS 1000
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)
72 IMG_UINT32 ui32AvSpace = 0;
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 */
78 if ( (ui32LRead - ui32LWrite) > (sizeof(PVRSRVTL_PACKETHDR) + ui32ReqSizeMin + (IMG_INT) BUFFER_RESERVED_SPACE) )
80 ui32AvSpace = ui32LRead - ui32LWrite - sizeof(PVRSRVTL_PACKETHDR) - (IMG_INT) BUFFER_RESERVED_SPACE;
83 else /* Normal, no wrap */
85 if ( (ui32CBSize - ui32LWrite) > (sizeof(PVRSRVTL_PACKETHDR) + ui32ReqSizeMin + (IMG_INT) BUFFER_RESERVED_SPACE) )
87 ui32AvSpace = ui32CBSize - ui32LWrite - sizeof(PVRSRVTL_PACKETHDR) - (IMG_INT) BUFFER_RESERVED_SPACE;
89 else if ( (ui32LRead - 0) > (sizeof(PVRSRVTL_PACKETHDR) + ui32ReqSizeMin + (IMG_INT) BUFFER_RESERVED_SPACE) )
91 ui32AvSpace = ui32LRead - sizeof(PVRSRVTL_PACKETHDR) - (IMG_INT) BUFFER_RESERVED_SPACE;
94 /* The max size of a TL packet currently is UINT16. adjust accordingly */
95 return MIN(ui32AvSpace, IMG_UINT16_MAX);
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
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)
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 )
111 return (IMG_INT) ui32Read - (IMG_INT)ui32Write - (IMG_INT) BUFFER_RESERVED_SPACE;
115 return (IMG_INT)ui32size - ((IMG_INT)ui32Write - (IMG_INT)ui32Read) - (IMG_INT) BUFFER_RESERVED_SPACE;
119 /*******************************************************************************
120 * TL Server public API implementation.
121 ******************************************************************************/
123 TLStreamCreate(IMG_HANDLE *phStream,
124 IMG_CHAR *szStreamName,
126 IMG_UINT32 ui32StreamFlags,
127 TL_STREAM_SOURCECB pfProducerCB,
128 IMG_PVOID pvProducerUD)
132 IMG_HANDLE hEventList;
134 IMG_CHAR pszBufferLabel[PRVSRVTL_MAX_STREAM_NAME_SIZE+20];
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;
145 /* non NULL handler required */
146 if ( NULL == phStream )
148 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
150 if (OSStringLength(szStreamName) >= PRVSRVTL_MAX_STREAM_NAME_SIZE)
152 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
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);
159 /* Check if there already exists a stream with this name. */
160 psn = TLFindStreamNodeByName( szStreamName );
161 if ( IMG_NULL != psn )
163 eError = PVRSRV_ERROR_ALREADY_EXISTS;
167 /* Allocate stream structure container (stream struct) for the new stream */
168 psTmp = OSAllocZMem(sizeof(TL_STREAM)) ;
171 eError = PVRSRV_ERROR_OUT_OF_MEMORY;
175 OSStringCopy(psTmp->szName, szStreamName);
177 if ( ui32StreamFlags & TL_FLAG_FORCE_FLUSH )
179 psTmp->bWaitForEmptyOnDestroy = IMG_TRUE;
182 psTmp->bNoSignalOnCommit = (ui32StreamFlags&TL_FLAG_NO_SIGNAL_ON_COMMIT) ? IMG_TRUE : IMG_FALSE;
184 if ( ui32StreamFlags & TL_FLAG_DROP_DATA )
186 if ( ui32StreamFlags & TL_FLAG_BLOCKING_RESERVE )
188 eError = PVRSRV_ERROR_INVALID_PARAMS;
191 psTmp->bDrop = IMG_TRUE;
193 else if ( ui32StreamFlags & TL_FLAG_BLOCKING_RESERVE )
194 { /* Additional synchronization object required for this kind of stream */
195 psTmp->bBlock = IMG_TRUE;
197 eError = OSEventObjectCreate(NULL, &psTmp->hProducerEventObj);
198 if (eError != PVRSRV_OK)
202 /* Create an event handle for this kind of stream */
203 eError = OSEventObjectOpen(psTmp->hProducerEventObj, &psTmp->hProducerEvent);
204 if (eError != PVRSRV_OK)
210 /* Remember producer supplied CB and data for later */
211 psTmp->pfProducerCallback = (IMG_VOID(*)(IMG_VOID))pfProducerCB;
212 psTmp->pvProducerUserData = pvProducerUD;
214 /* Round the requested bytes to a multiple of array elements' size, eg round 3 to 4 */
215 psTmp->ui32Size = PVRSRVTL_ALIGN(ui32Size);
217 psTmp->ui32Write = 0;
218 psTmp->ui32Pending = NOTHING_PENDING;
220 OSSNPrintf(pszBufferLabel, sizeof(pszBufferLabel), "TLStreamBuf-%s", szStreamName);
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,
229 &psTmp->psStreamMemDesc);
230 PVR_LOGG_IF_ERROR(eError, "DevmemAllocateExportable", e3);
232 eError = DevmemAcquireCpuVirtAddr( psTmp->psStreamMemDesc, (IMG_VOID**) &psTmp->pbyBuffer );
233 PVR_LOGG_IF_ERROR(eError, "DevmemAcquireCpuVirtAddr", e4);
235 eError = DevmemExport(psTmp->psStreamMemDesc, &(psTmp->sExportCookie));
236 PVR_LOGG_IF_ERROR(eError, "DevmemExport", e5);
238 /* Synchronization object to synchronize with user side data transfers. */
239 eError = OSEventObjectCreate(psTmp->szName, &hEventList);
240 if (eError != PVRSRV_OK)
245 eError = OSLockCreate (&psTmp->hStreamLock, LOCK_TYPE_PASSIVE);
246 if (eError != PVRSRV_OK)
251 /* Now remember the stream in the global TL structures */
252 psn = TLMakeSNode(hEventList, (TL_STREAM *)psTmp, 0);
255 eError=PVRSRV_ERROR_OUT_OF_MEMORY;
259 /* Stream node created, now reset the write reference count to 1
260 * (i.e. this context's reference) */
261 psn->uiWRefCount = 1;
263 TLAddStreamNode(psn);
265 /* Release TL_GLOBAL_DATA lock as the new TL_SNODE is now added to the list */
266 OSLockRelease (TLGGD()->hTLGDLock);
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
271 (void) OSEventObjectSignal(TLGGD()->hTLEventObj);
273 /* Pass the newly created stream handle back to caller */
274 *phStream = (IMG_HANDLE)psTmp;
278 OSLockDestroy(psTmp->hStreamLock);
280 OSEventObjectDestroy(hEventList);
282 DevmemUnexport(psTmp->psStreamMemDesc, &(psTmp->sExportCookie));
284 DevmemReleaseCpuVirtAddr( psTmp->psStreamMemDesc );
286 DevmemFree(psTmp->psStreamMemDesc);
288 OSEventObjectClose(psTmp->hProducerEvent);
290 OSEventObjectDestroy(psTmp->hProducerEventObj);
294 OSLockRelease (TLGGD()->hTLGDLock);
296 PVR_DPF_RETURN_RC(eError);
300 TLStreamOpen(IMG_HANDLE *phStream,
301 IMG_CHAR *szStreamName)
303 PTL_SNODE psTmpSNode;
307 if ( IMG_NULL == phStream || IMG_NULL == szStreamName )
309 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
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);
317 /* Search for a stream node with a matching stream name */
318 psTmpSNode = TLFindStreamNodeByName(szStreamName);
320 if ( IMG_NULL == psTmpSNode )
322 OSLockRelease (TLGGD()->hTLGDLock);
323 PVR_DPF_RETURN_RC(PVRSRV_ERROR_NOT_FOUND);
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++;
332 OSLockRelease (TLGGD()->hTLGDLock);
334 /* Return the stream handle to the caller */
335 *phStream = (IMG_HANDLE)psTmpSNode->psStream;
337 PVR_DPF_RETURN_VAL(PVRSRV_OK);
341 TLStreamClose(IMG_HANDLE hStream)
344 IMG_BOOL bDestroyStream;
348 if ( IMG_NULL == hStream )
350 PVR_DPF((PVR_DBG_WARNING,
351 "TLStreamClose failed as NULL stream handler passed, nothing done.\n"));
355 psTmp = (PTL_STREAM)hStream;
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);
361 /* Decrement write reference counter of the stream */
362 psTmp->psNode->uiWRefCount--;
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);
371 /* Now we try removing this TL_STREAM from TL_GLOBAL_DATA */
373 if ( psTmp->bWaitForEmptyOnDestroy == IMG_TRUE )
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)
381 /* Release lock before sleeping */
382 OSLockRelease (TLGGD()->hTLGDLock);
384 OSEventObjectWaitTimeout(psTmp->hProducerEvent, EVENT_OBJECT_TIMEOUT_MS);
386 OSLockAcquire (TLGGD()->hTLGDLock);
388 /* Ensure destruction of stream is still required */
389 if (0 != psTmp->psNode->uiWRefCount)
391 OSLockRelease (TLGGD()->hTLGDLock);
397 /* Try removing the stream from TL_GLOBAL_DATA */
398 bDestroyStream = TLTryRemoveStreamAndFreeStreamNode (psTmp->psNode);
400 OSLockRelease (TLGGD()->hTLGDLock);
404 /* Destroy the stream if it was removed from TL_GLOBAL_DATA */
405 TLStreamDestroy (psTmp);
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)
421 IMG_UINT32 *pui32Buf, ui32LRead, ui32LWrite, ui32LPending, lReqSizeAligned, lReqSizeActual;
422 IMG_INT pad, iFreeSpace;
425 if (pui32AvSpace) *pui32AvSpace = 0;
427 if (( IMG_NULL == hStream ))
429 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
431 psTmp = (PTL_STREAM)hStream;
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));
437 /* The buffer is only used in "rounded" (aligned) chunks */
438 lReqSizeAligned = PVRSRVTL_ALIGN(ui32ReqSize);
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);
445 /* Get a local copy of the stream buffer parameters */
446 ui32LRead = psTmp->ui32Read ;
447 ui32LWrite = psTmp->ui32Write ;
448 ui32LPending = psTmp->ui32Pending ;
450 /* Multiple pending reserves are not supported. */
451 if ( NOTHING_PENDING != ui32LPending )
453 OSLockRelease (psTmp->hStreamLock);
454 PVR_DPF_RETURN_RC(PVRSRV_ERROR_NOT_READY);
457 if ( IMG_UINT16_MAX < lReqSizeAligned )
459 psTmp->ui32Pending = NOTHING_PENDING;
462 *pui32AvSpace = suggestAllocSize(ui32LRead, ui32LWrite, psTmp->ui32Size, ui32ReqSizeMin);
464 OSLockRelease (psTmp->hStreamLock);
465 PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_FULL);
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;
474 OSLockRelease (psTmp->hStreamLock);
476 /* If there is enough contiguous space following the current Write
477 * position then no padding is required */
479 < ui32LWrite + lReqSizeAligned + sizeof(PVRSRVTL_PACKETHDR) )
481 pad = psTmp->ui32Size - ui32LWrite;
488 lReqSizeActual = lReqSizeAligned + sizeof(PVRSRVTL_PACKETHDR) + pad ;
489 /* If this is a blocking reserve and there is not enough space then wait. */
492 if( psTmp->ui32Size < lReqSizeActual )
494 /* Acquire stream lock for updating pending value */
495 OSLockAcquire (psTmp->hStreamLock);
496 psTmp->ui32Pending = NOTHING_PENDING;
497 OSLockRelease (psTmp->hStreamLock);
499 PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_MISUSE);
501 while ( ( cbSpaceLeft(ui32LRead, ui32LWrite, psTmp->ui32Size)
502 <(IMG_INT) lReqSizeActual ) )
504 OSEventObjectWait(psTmp->hProducerEvent);
505 // update local copies.
506 ui32LRead = psTmp->ui32Read ;
507 ui32LWrite = psTmp->ui32Write ;
511 iFreeSpace = cbSpaceLeft(ui32LRead, ui32LWrite, psTmp->ui32Size);
513 /* The easy case: buffer has enough space to hold the requested packet (data + header) */
514 if ( iFreeSpace >=(IMG_INT) lReqSizeActual )
518 /* Inserting padding packet. */
519 pui32Buf = (IMG_UINT32*)&psTmp->pbyBuffer[ui32LWrite];
520 *pui32Buf = PVRSRVTL_SET_PACKET_PADDING(pad-sizeof(PVRSRVTL_PACKETHDR)) ;
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);
528 /* Insert size-stamped packet header */
529 pui32Buf = (IMG_UINT32*) &psTmp->pbyBuffer[ui32LWrite];
531 *pui32Buf = PVRSRVTL_SET_PACKET_HDR(ui32ReqSize, ePacketType);
533 /* return the next position in the buffer to the user */
534 *ppui8Data = &psTmp->pbyBuffer[ ui32LWrite+sizeof(PVRSRVTL_PACKETHDR) ] ;
536 /* update pending offset: size stamp + data */
537 ui32LPending = lReqSizeAligned + sizeof(PVRSRVTL_PACKETHDR) ;
539 /* The not so easy case: not enough space, decide how to handle data */
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 )
549 OSLockAcquire (psTmp->hStreamLock);
550 psTmp->ui32Pending = NOTHING_PENDING;
551 OSLockRelease (psTmp->hStreamLock);
552 PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_MISUSE);
556 /* No data overwriting, insert write_failed flag and return */
559 /* Caller should not try to use ppui8Data,
560 * NULLify to give user a chance of avoiding memory corruption */
561 ppui8Data = IMG_NULL;
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
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];
571 if ( PVRSRVTL_PACKETTYPE_MOST_RECENT_WRITE_FAILED
573 GET_PACKET_TYPE( (PVRSRVTL_PACKETHDR*)pui32Buf ) )
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);
583 OSLockAcquire (psTmp->hStreamLock);
584 psTmp->ui32Write = ui32LWrite;
585 psTmp->ui32Pending = NOTHING_PENDING;
586 OSLockRelease (psTmp->hStreamLock);
590 *pui32AvSpace = suggestAllocSize(ui32LRead, ui32LWrite, psTmp->ui32Size, ui32ReqSizeMin);
592 PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_FULL);
596 /* Acquire stream lock for updating stream parameters */
597 OSLockAcquire (psTmp->hStreamLock);
598 psTmp->ui32Write = ui32LWrite ;
599 psTmp->ui32Pending = ui32LPending ;
600 OSLockRelease (psTmp->hStreamLock);
606 TLStreamReserve(IMG_HANDLE hStream,
607 IMG_UINT8 **ppui8Data,
610 return DoTLStreamReserve(hStream, ppui8Data, ui32Size, ui32Size, PVRSRVTL_PACKETTYPE_DATA, NULL);
614 TLStreamReserve2(IMG_HANDLE hStream,
615 IMG_UINT8 **ppui8Data,
617 IMG_UINT32 ui32SizeMin,
618 IMG_UINT32* pui32Available)
620 return DoTLStreamReserve(hStream, ppui8Data, ui32Size, ui32SizeMin, PVRSRVTL_PACKETTYPE_DATA, pui32Available);
624 TLStreamCommit(IMG_HANDLE hStream, IMG_UINT32 ui32ReqSize)
627 IMG_UINT32 ui32LRead, ui32OldWrite, ui32LWrite, ui32LPending;
632 if ( IMG_NULL == hStream )
634 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
636 psTmp = (PTL_STREAM)hStream;
638 /* Get a local copy of the stream buffer parameters */
639 ui32LRead = psTmp->ui32Read ;
640 ui32LWrite = psTmp->ui32Write ;
641 ui32LPending = psTmp->ui32Pending ;
643 ui32OldWrite = ui32LWrite;
645 // Space in buffer is aligned
646 ui32ReqSize = PVRSRVTL_ALIGN(ui32ReqSize);
648 /* Sanity check. ReqSize + packet header size. */
649 if ( ui32LPending != ui32ReqSize + sizeof(PVRSRVTL_PACKETHDR) )
651 PVR_DPF_RETURN_RC(PVRSRV_ERROR_STREAM_MISUSE);
654 /* Update pointer to written data. */
655 ui32LWrite = (ui32LWrite + ui32LPending) % psTmp->ui32Size;
657 /* and reset LPending to 0 since data are now submitted */
658 ui32LPending = NOTHING_PENDING;
660 /* Calculate high water mark for debug purposes */
661 #if defined(TL_BUFFER_UTILIZATION)
664 if (ui32LWrite > ui32LRead)
666 tmp = (ui32LWrite-ui32LRead);
668 else if (ui32LWrite < ui32LRead)
670 tmp = (psTmp->ui32Size-ui32LRead+ui32LWrite);
671 } /* else equal, ignore */
673 if (tmp > psTmp->ui32BufferUt)
675 psTmp->ui32BufferUt = tmp;
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);
685 /* Update stream buffer parameters to match local copies */
686 psTmp->ui32Write = ui32LWrite ;
687 psTmp->ui32Pending = ui32LPending ;
689 OSLockRelease (psTmp->hStreamLock);
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)
695 /* Signal consumers that may be waiting */
696 eError = OSEventObjectSignal(psTmp->psNode->hDataEventObj);
697 if ( eError != PVRSRV_OK)
699 PVR_DPF_RETURN_RC(eError);
707 TLStreamWrite(IMG_HANDLE hStream, IMG_UINT8 *pui8Src, IMG_UINT32 ui32Size)
709 IMG_BYTE *pbyDest = IMG_NULL;
714 if ( IMG_NULL == hStream )
716 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
719 eError = TLStreamReserve(hStream, &pbyDest, ui32Size);
720 if ( PVRSRV_OK != eError )
722 PVR_DPF_RETURN_RC(eError);
726 PVR_ASSERT ( pbyDest != NULL );
727 OSMemCopy((IMG_VOID*)pbyDest, (IMG_VOID*)pui8Src, ui32Size);
728 eError = TLStreamCommit(hStream, ui32Size);
729 if ( PVRSRV_OK != eError )
731 PVR_DPF_RETURN_RC(eError);
737 IMG_VOID TLStreamInfo(PTL_STREAM_INFO psInfo)
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 */
743 DevmemExportalignAdjustSizeAndAlign(IMG_NULL, &actual_req_size, &align);
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);
752 TLStreamMarkEOS(IMG_HANDLE psStream)
759 if ( IMG_NULL == psStream )
761 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
764 eError = DoTLStreamReserve(psStream, &pData, 0, 0, PVRSRVTL_PACKETTYPE_MARKER_EOS, NULL);
765 if ( PVRSRV_OK != eError )
767 PVR_DPF_RETURN_RC(eError);
770 PVR_DPF_RETURN_RC(TLStreamCommit(psStream, 0));
774 TLStreamSync(IMG_HANDLE psStream)
776 PVRSRV_ERROR eError = PVRSRV_OK;
781 if ( IMG_NULL == psStream )
783 PVR_DPF_RETURN_RC(PVRSRV_ERROR_INVALID_PARAMS);
785 psTmp = (PTL_STREAM)psStream;
787 /* Signal clients only when data is available to read */
788 if (psTmp->ui32Read != psTmp->ui32Write)
790 eError = OSEventObjectSignal(psTmp->psNode->hDataEventObj);
793 PVR_DPF_RETURN_RC(eError);
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.
802 TLStreamAcquireReadPos(PTL_STREAM psStream, IMG_UINT32* puiReadOffset)
804 IMG_UINT32 uiReadLen = 0;
805 IMG_UINT32 ui32LRead, ui32LWrite;
809 PVR_ASSERT(psStream);
810 PVR_ASSERT(puiReadOffset);
812 /* Grab a local copy */
813 ui32LRead = psStream->ui32Read;
814 ui32LWrite = psStream->ui32Write;
816 /* No data available and CB defined - try and get data */
817 if ((ui32LRead == ui32LWrite) && psStream->pfProducerCallback)
820 IMG_UINT32 ui32Resp = 0;
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");
826 ui32LWrite = psStream->ui32Write;
829 /* No data available... */
830 if (ui32LRead == ui32LWrite)
832 PVR_DPF_RETURN_VAL(0);
835 /* Data is available to read... */
836 *puiReadOffset = ui32LRead;
838 /*PVR_DPF((PVR_DBG_VERBOSE,
839 * "TLStreamAcquireReadPos Start before: Write:%d, Read:%d, size:%d",
840 * ui32LWrite, ui32LRead, psStream->ui32Size));
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;
851 { // CB has not wrapped
852 uiReadLen = ui32LWrite - ui32LRead;
855 PVR_DPF_RETURN_VAL(uiReadLen);
859 TLStreamAdvanceReadPos(PTL_STREAM psStream, IMG_UINT32 uiReadLen)
863 PVR_ASSERT(psStream);
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;
869 /* If this is a blocking reserve stream,
870 * notify reserves that may be pending */
874 eError = OSEventObjectSignal(psStream->hProducerEventObj);
875 if ( eError != PVRSRV_OK)
877 PVR_DPF((PVR_DBG_WARNING,
878 "Error in TLStreamAdvanceReadPos: OSEventObjectSignal returned:%u",
883 PVR_DPF((PVR_DBG_VERBOSE,
884 "TLStreamAdvanceReadPos Read now at: %d",
885 psStream->ui32Read));
890 TLStreamDestroy (PTL_STREAM psStream)
892 PVR_ASSERT (psStream);
894 OSLockDestroy (psStream->hStreamLock);
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 )
900 OSEventObjectClose(psStream->hProducerEvent);
901 OSEventObjectDestroy(psStream->hProducerEventObj);
904 DevmemUnexport(psStream->psStreamMemDesc, &psStream->sExportCookie);
905 DevmemReleaseCpuVirtAddr(psStream->psStreamMemDesc);
906 DevmemFree(psStream->psStreamMemDesc);
911 TLStreamGetBufferCookie(PTL_STREAM psStream)
915 PVR_ASSERT(psStream);
917 PVR_DPF_RETURN_VAL(&psStream->sExportCookie);
921 TLStreamEOS(PTL_STREAM psStream)
925 PVR_ASSERT(psStream);
927 /* If both pointers are equal then the buffer is empty */
928 PVR_DPF_RETURN_VAL( psStream->ui32Read == psStream->ui32Write );