hc
2023-05-26 a23f51ed7a39e452c1037343a84d7db1ca2c5bd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*************************************************************************/ /*!
@File            tlclient.c
@Title          Services Transport Layer shared API
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@Description    Transport layer common API used in both clients and server
@License        Dual MIT/GPLv2
 
The contents of this file are subject to the MIT license as set out below.
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 ("GPL") in which case the provisions
of GPL are applicable instead of those above.
 
If you wish to allow use of your version of this file only under the terms of
GPL, and not to allow others to use your version of this file under the terms
of the MIT license, indicate your decision by deleting the provisions above
and replace them with the notice and other provisions required by GPL as set
out in the file called "GPL-COPYING" included in this distribution. If you do
not delete the provisions above, a recipient may use your version of this file
under the terms of either the MIT license or GPL.
 
This License is also included in this distribution in the file called
"MIT-COPYING".
 
EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ /**************************************************************************/
 
/* DESIGN NOTE
 * This transport layer consumer-role API was created as a shared API when a
 * client wanted to read the data of a TL stream from within the KM server
 * driver. This was in addition to the existing clients supported externally
 * by the UM client library component via PVR API layer.
 * This shared API is thus used by the PVR TL API in the client library and
 * by clients internal to the server driver module. It depends on
 * client entry points of the TL and DEVMEM bridge modules. These entry points
 * encapsulate from the TL shared API whether a direct bridge or an indirect
 * (ioctl) bridge is used.
 * One reason for needing this layer centres around the fact that some of the
 * API functions make multiple bridge calls and the logic that glues these
 * together is common regardless of client location. Further this layer has
 * allowed the defensive coding that checks parameters to move into the PVR
 * API layer where untrusted clients enter giving a more efficient KM code path.
 */
 
#include "img_defs.h"
#include "pvrsrv_error.h"
#include "pvr_debug.h"
#include "osfunc.h"
 
#include "allocmem.h"
#include "devicemem.h"
 
#include "tlclient.h"
#include "pvr_tlcommon.h"
#include "client_pvrtl_bridge.h"
#include "pvrsrv_tlcommon.h"
 
/* Defines/Constants
 */
 
#define NO_ACQUIRE             0xffffffffU
 
/* User-side stream descriptor structure.
 */
typedef struct _TL_STREAM_DESC_
{
   /* Handle on kernel-side stream descriptor*/
   IMG_HANDLE        hServerSD;
 
   /* Stream data buffer variables */
   DEVMEM_MEMDESC*            psUMmemDesc;
   IMG_PBYTE                pBaseAddr;
 
   /* Offset in bytes into the circular buffer and valid only after
    * an Acquire call and undefined after a release. */
   IMG_UINT32     uiReadOffset;
 
   /* Always a positive integer when the Acquire call returns and a release
    * is outstanding. Undefined at all other times. */
   IMG_UINT32    uiReadLen;
 
} TL_STREAM_DESC, *PTL_STREAM_DESC;
 
 
IMG_INTERNAL
PVRSRV_ERROR TLClientOpenStream(IMG_HANDLE hSrvHandle,
       const IMG_CHAR* pszName,
       IMG_UINT32   ui32Mode,
       IMG_HANDLE*  phSD)
{
   PVRSRV_ERROR                 eError = PVRSRV_OK;
   TL_STREAM_DESC*             psSD = 0;
   IMG_HANDLE hTLPMR;
   IMG_HANDLE hTLImportHandle;
   IMG_DEVMEM_SIZE_T uiImportSize;
   IMG_UINT32 ui32MemFlags = PVRSRV_MEMALLOCFLAG_CPU_READABLE;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(pszName);
   PVR_ASSERT(phSD);
   *phSD = NULL;
 
   /* Allocate memory for the stream descriptor object, initialise with
    * "no data read" yet. */
   psSD = OSAllocZMem(sizeof(TL_STREAM_DESC));
   if (psSD == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       PVR_DPF((PVR_DBG_ERROR, "BridgeTLOpenStream: KM returned %d", eError));
       goto e0;
   }
   psSD->uiReadLen = psSD->uiReadOffset = NO_ACQUIRE;
 
   /* Send open stream request to kernel server to get stream handle and
    * buffer cookie so we can get access to the buffer in this process. */
   eError = BridgeTLOpenStream(hSrvHandle, pszName, ui32Mode,
                                       &psSD->hServerSD, &hTLPMR);
   if (eError != PVRSRV_OK)
   {
       if ((ui32Mode & PVRSRV_STREAM_FLAG_OPEN_WAIT) &&
           (eError == PVRSRV_ERROR_TIMEOUT))
       {
           goto e1;
       }
       PVR_LOGG_IF_ERROR(eError, "BridgeTLOpenStream", e1);
   }
 
   /* Convert server export cookie into a cookie for use by this client */
   eError = DevmemMakeLocalImportHandle(hSrvHandle,
                                       hTLPMR, &hTLImportHandle);
   PVR_LOGG_IF_ERROR(eError, "DevmemMakeLocalImportHandle", e2);
 
   ui32MemFlags |= ui32Mode & PVRSRV_STREAM_FLAG_OPEN_WO ?
           PVRSRV_MEMALLOCFLAG_CPU_WRITEABLE : 0;
   /* Now convert client cookie into a client handle on the buffer's
    * physical memory region */
   eError = DevmemLocalImport(hSrvHandle,
                              hTLImportHandle,
                              PVRSRV_MEMALLOCFLAG_CPU_READABLE |
                              PVRSRV_MEMALLOCFLAG_CPU_WRITEABLE,
                              &psSD->psUMmemDesc,
                              &uiImportSize,
                              "TLBuffer");
   PVR_LOGG_IF_ERROR(eError, "DevmemImport", e3);
 
   /* Now map the memory into the virtual address space of this process. */
   eError = DevmemAcquireCpuVirtAddr(psSD->psUMmemDesc, (void **)
                                                           &psSD->pBaseAddr);
   PVR_LOGG_IF_ERROR(eError, "DevmemAcquireCpuVirtAddr", e4);
 
   /* Ignore error, not much that can be done */
   (void) DevmemUnmakeLocalImportHandle(hSrvHandle,
           hTLImportHandle);
 
   /* Return client descriptor handle to caller */
   *phSD = psSD;
   return PVRSRV_OK;
 
/* Clean up post buffer setup */
e4:
   DevmemFree(psSD->psUMmemDesc);
e3:
   (void) DevmemUnmakeLocalImportHandle(hSrvHandle,
               &hTLImportHandle);
/* Clean up post stream open */
e2:
   BridgeTLCloseStream(hSrvHandle, psSD->hServerSD);
 
/* Cleanup post allocation of the descriptor object */
e1:
   OSFreeMem(psSD);
 
e0:
   return eError;
}
 
 
IMG_INTERNAL
PVRSRV_ERROR TLClientCloseStream(IMG_HANDLE hSrvHandle,
       IMG_HANDLE hSD)
{
   PVRSRV_ERROR          eError = PVRSRV_OK;
   TL_STREAM_DESC* psSD = (TL_STREAM_DESC*) hSD;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(hSD);
 
   /* Check the caller provided connection is valid */
   if (!psSD->hServerSD)
   {
       PVR_DPF((PVR_DBG_ERROR, "TLClientCloseStream: descriptor already closed/not open"));
       return PVRSRV_ERROR_HANDLE_NOT_FOUND;
   }
 
   /* Check if acquire is outstanding, perform release if it is, ignore result
    * as there is not much we can do if it is an error other than close */
   if (psSD->uiReadLen != NO_ACQUIRE)
   {
       (void) BridgeTLReleaseData(hSrvHandle, psSD->hServerSD,
                                   psSD->uiReadOffset, psSD->uiReadLen);
       psSD->uiReadLen = psSD->uiReadOffset = NO_ACQUIRE;
   }
 
   /* Clean up DevMem resources used for this stream in this client */
   DevmemReleaseCpuVirtAddr(psSD->psUMmemDesc);
 
   DevmemFree(psSD->psUMmemDesc);
 
   /* Send close to server to clean up kernel mode resources for this
    * handle and release the memory. */
   eError = BridgeTLCloseStream(hSrvHandle, psSD->hServerSD);
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR, "BridgeTLCloseStream: KM returned %d", eError));
       /* Not much we can do with error, fall through to clean up
        * return eError; */
   }
 
   OSCachedMemSet(psSD, 0x00, sizeof(TL_STREAM_DESC));
   OSFreeMem (psSD);
 
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR TLClientDiscoverStreams(IMG_HANDLE hSrvHandle,
       const IMG_CHAR *pszNamePattern,
       IMG_UINT32 *pui32Streams,
       IMG_UINT32 *pui32NumFound)
{
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(pszNamePattern);
   PVR_ASSERT(pui32NumFound);
 
   return BridgeTLDiscoverStreams(hSrvHandle,
                                  pszNamePattern,
                                  *pui32NumFound,
                                  pui32Streams,
                                  pui32NumFound);
}
 
IMG_INTERNAL
PVRSRV_ERROR TLClientReserveStream(IMG_HANDLE hSrvHandle,
       IMG_HANDLE hSD,
       IMG_UINT8 **ppui8Data,
       IMG_UINT32 ui32Size)
{
   PVRSRV_ERROR eError;
   TL_STREAM_DESC* psSD = (TL_STREAM_DESC*) hSD;
   IMG_UINT32 ui32BufferOffset, ui32Dummy;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(hSD);
   PVR_ASSERT(ppui8Data);
   PVR_ASSERT(ui32Size);
 
   eError = BridgeTLReserveStream(hSrvHandle, psSD->hServerSD,
                                  &ui32BufferOffset, ui32Size, ui32Size,
                                  &ui32Dummy);
   if (eError != PVRSRV_OK)
   {
       return eError;
   }
 
   *ppui8Data = psSD->pBaseAddr + ui32BufferOffset;
 
   return PVRSRV_OK;
}
 
IMG_INTERNAL
PVRSRV_ERROR TLClientReserveStream2(IMG_HANDLE hSrvHandle,
       IMG_HANDLE hSD,
       IMG_UINT8 **ppui8Data,
       IMG_UINT32 ui32Size,
       IMG_UINT32 ui32SizeMin,
       IMG_UINT32 *pui32Available)
{
       PVRSRV_ERROR eError;
   TL_STREAM_DESC* psSD = (TL_STREAM_DESC*) hSD;
   IMG_UINT32 ui32BufferOffset;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(hSD);
   PVR_ASSERT(ppui8Data);
   PVR_ASSERT(ui32Size);
 
   eError = BridgeTLReserveStream(hSrvHandle, psSD->hServerSD,
                                  &ui32BufferOffset, ui32Size, ui32SizeMin,
                                  pui32Available);
   if (eError != PVRSRV_OK)
   {
       return eError;
   }
 
   *ppui8Data = psSD->pBaseAddr + ui32BufferOffset;
 
   return PVRSRV_OK;
}
 
IMG_INTERNAL
PVRSRV_ERROR TLClientCommitStream(IMG_HANDLE hSrvHandle,
       IMG_HANDLE hSD,
       IMG_UINT32 ui32Size)
{
   PVRSRV_ERROR eError;
   TL_STREAM_DESC* psSD = (TL_STREAM_DESC*) hSD;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(hSD);
   PVR_ASSERT(ui32Size);
 
   eError = BridgeTLCommitStream(hSrvHandle, psSD->hServerSD, ui32Size);
   if (eError != PVRSRV_OK)
   {
       return eError;
   }
 
   return PVRSRV_OK;
}
 
IMG_INTERNAL
PVRSRV_ERROR TLClientAcquireData(IMG_HANDLE hSrvHandle,
       IMG_HANDLE  hSD,
       IMG_PBYTE*  ppPacketBuf,
       IMG_UINT32* pui32BufLen)
{
   PVRSRV_ERROR           eError = PVRSRV_OK;
   TL_STREAM_DESC* psSD = (TL_STREAM_DESC*) hSD;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(hSD);
   PVR_ASSERT(ppPacketBuf);
   PVR_ASSERT(pui32BufLen);
 
   /* Check Acquire has not been called twice in a row without a release */
   if (psSD->uiReadOffset != NO_ACQUIRE)
   {
       PVR_DPF((PVR_DBG_ERROR, "TLClientAcquireData: acquire already outstanding"));
       return PVRSRV_ERROR_RETRY;
   }
 
   *pui32BufLen = 0;
   /* Ask the kernel server for the next chunk of data to read */
   eError = BridgeTLAcquireData(hSrvHandle, psSD->hServerSD,
                                   &psSD->uiReadOffset, &psSD->uiReadLen);
   if (eError != PVRSRV_OK)
   {
       if ((eError != PVRSRV_ERROR_RESOURCE_UNAVAILABLE) &&
           (eError != PVRSRV_ERROR_TIMEOUT))
       {
           PVR_DPF((PVR_DBG_ERROR, "BridgeTLAcquireData: KM returned %d", eError));
       }
       psSD->uiReadOffset = psSD->uiReadLen = NO_ACQUIRE;
       return eError;
   }
 
   /* Return the data offset and length to the caller if bytes are available
    * to be read. Could be zero for non-blocking mode. */
   if (psSD->uiReadLen)
   {
       *ppPacketBuf = psSD->pBaseAddr + psSD->uiReadOffset;
       *pui32BufLen = psSD->uiReadLen;
   }
   else
   {
       /* On non-blocking, zero length data could be returned from server
        * Which is basically a no-acquire operation */
       *ppPacketBuf = 0;
       *pui32BufLen = 0;
   }
 
   return eError;
}
 
 
IMG_INTERNAL
PVRSRV_ERROR TLClientReleaseData(IMG_HANDLE hSrvHandle,
       IMG_HANDLE hSD)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   TL_STREAM_DESC* psSD = (TL_STREAM_DESC*) hSD;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(hSD);
 
   /* the previous acquire did not return any data, this is a no-operation */
   if (psSD->uiReadLen == 0)
   {
       return PVRSRV_OK;
   }
 
   /* Check release has not been called twice in a row without an acquire */
   if (psSD->uiReadOffset == NO_ACQUIRE)
   {
       PVR_DPF((PVR_DBG_ERROR, "TLClientReleaseData_: no acquire to release"));
       return PVRSRV_ERROR_RETRY;
   }
 
   /* Inform the kernel to release the data from the buffer */
   eError = BridgeTLReleaseData(hSrvHandle, psSD->hServerSD,
                                       psSD->uiReadOffset, psSD->uiReadLen);
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR, "BridgeTLReleaseData: KM returned %d", eError));
       /* Need to continue to keep client data consistent, fall through
        * return eError */
   }
 
   /* Reset state to indicate no outstanding acquire */
   psSD->uiReadLen = psSD->uiReadOffset = NO_ACQUIRE;
 
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR TLClientWriteData(IMG_HANDLE hSrvHandle,
       IMG_HANDLE hSD,
       IMG_UINT32 ui32Size,
       IMG_BYTE *pui8Data)
{
   PVRSRV_ERROR eError;
   TL_STREAM_DESC* psSD = (TL_STREAM_DESC*) hSD;
 
   PVR_ASSERT(hSrvHandle);
   PVR_ASSERT(hSD);
   PVR_ASSERT(ui32Size);
   PVR_ASSERT(pui8Data);
 
   eError = BridgeTLWriteData(hSrvHandle, psSD->hServerSD, ui32Size, pui8Data);
   if (eError != PVRSRV_OK)
   {
       if (eError == PVRSRV_ERROR_STREAM_RESERVE_TOO_BIG)
       {
           static IMG_BOOL bPrinted = IMG_FALSE;
 
           if (!bPrinted) {
               PVR_DPF((PVR_DBG_ERROR, "Not enough space. Failed to write"
                       " data to the stream (%d).", eError));
               bPrinted = IMG_TRUE;
           }
       }
       else
       {
           PVR_DPF((PVR_DBG_ERROR, "TLClientWriteData: KM returned %d",
                   eError));
       }
   }
 
   return eError;
}
 
/******************************************************************************
 End of file (tlclient.c)
******************************************************************************/