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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*************************************************************************/ /*!
@File
@Title          Transport Layer kernel side API implementation.
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@Description    Transport Layer functions available to driver components in 
                the driver.
@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.
*/ /**************************************************************************/
//#define PVR_DPF_FUNCTION_TRACE_ON 1
#undef PVR_DPF_FUNCTION_TRACE_ON
#include "pvr_debug.h"
 
#include "allocmem.h"
#include "pvrsrv_error.h"
#include "osfunc.h"
#include "devicemem.h"
 
#include "pvrsrv_tlcommon.h"
#include "tlintern.h"
 
/*
 * Make functions
 */
PTL_STREAM_DESC
TLMakeStreamDesc(PTL_SNODE f1, IMG_UINT32 f2, IMG_HANDLE f3)
{
   PTL_STREAM_DESC ps = OSAllocZMem(sizeof(TL_STREAM_DESC));
   if (ps == NULL)
   {
       return NULL;
   }
   ps->psNode = f1;
   ps->ui32Flags = f2;
   ps->hReadEvent = f3;
   ps->uiRefCount = 1;
   return ps;
}
 
PTL_SNODE
TLMakeSNode(IMG_HANDLE f2, TL_STREAM *f3, TL_STREAM_DESC *f4)
{
   PTL_SNODE ps = OSAllocZMem(sizeof(TL_SNODE));
   if (ps == NULL)
   {
       return NULL;
   }
   ps->hReadEventObj = f2;
   ps->psStream = f3;
   ps->psRDesc = f4;
   f3->psNode = ps;
   return ps;
}
 
/*
 * Transport Layer Global top variables and functions
 */
static TL_GLOBAL_DATA  sTLGlobalData = { 0 };
 
TL_GLOBAL_DATA *TLGGD(void)    // TLGetGlobalData()
{
   return &sTLGlobalData;
}
 
/* TLInit must only be called once at driver initialisation for one device.
 * An assert is provided to check this condition on debug builds.
 */
PVRSRV_ERROR
TLInit(PVRSRV_DEVICE_NODE *psDevNode)
{
   PVRSRV_ERROR eError;
 
   PVR_DPF_ENTERED;
 
   PVR_ASSERT(psDevNode);
 
   /*
    * The Transport Layer is designed to work in a single device system but
    * this function will be called multiple times in a multi-device system.
    * Return an error in this case.
    */
   if (sTLGlobalData.psRgxDevNode)
   {
       return PVRSRV_ERROR_INIT_FAILURE;
   }
 
   /* Store the RGX device node for later use in devmem buffer allocations */
   sTLGlobalData.psRgxDevNode = (void*)psDevNode;
 
   /* Allocate a lock for TL global data, to be used while updating the TL data.
    * This is for making TL global data muti-thread safe */
   eError = OSLockCreate (&sTLGlobalData.hTLGDLock, LOCK_TYPE_PASSIVE);
   if (eError != PVRSRV_OK)
   {
       goto e0;
   }
   
   /* Allocate the event object used to signal global TL events such as
    * new stream created */
   eError = OSEventObjectCreate("TLGlobalEventObj", &sTLGlobalData.hTLEventObj);
   if (eError != PVRSRV_OK)
   {
       goto e1;
   }
   
   PVR_DPF_RETURN_OK;
 
/* Don't allow the driver to start up on error */
e1:
   OSLockDestroy (sTLGlobalData.hTLGDLock);
   sTLGlobalData.hTLGDLock = NULL;
e0:
   PVR_DPF_RETURN_RC (eError);
}
 
static void RemoveAndFreeStreamNode(PTL_SNODE psRemove)
{
   TL_GLOBAL_DATA*  psGD = TLGGD();
   PTL_SNODE*          last;
   PTL_SNODE          psn;
   PVRSRV_ERROR     eError;
 
   PVR_DPF_ENTERED;
 
   // Unlink the stream node from the master list
   PVR_ASSERT(psGD->psHead);
   last = &psGD->psHead;
   for (psn = psGD->psHead; psn; psn=psn->psNext)
   {
       if (psn == psRemove)
       {
           /* Other calling code may have freed and zero'd the pointers */
           if (psn->psRDesc)
           {
               OSFreeMem(psn->psRDesc);
               psn->psRDesc = NULL;
           }
           if (psn->psStream)
           {
               OSFreeMem(psn->psStream);
               psn->psStream = NULL;
           }
           *last = psn->psNext;
           break;
       }
       last = &psn->psNext;
   }
 
   // Release the event list object owned by the stream node
   if (psRemove->hReadEventObj)
   {
       eError = OSEventObjectDestroy(psRemove->hReadEventObj);
       PVR_LOG_IF_ERROR(eError, "OSEventObjectDestroy");
 
       psRemove->hReadEventObj = NULL;
   }
 
   // Release the memory of the stream node
   OSFreeMem(psRemove);
 
   PVR_DPF_RETURN;
}
 
void
TLDeInit(PVRSRV_DEVICE_NODE *psDevNode)
{
   PVR_DPF_ENTERED;
 
   if (sTLGlobalData.psRgxDevNode != psDevNode)
   {
       PVR_DPF_RETURN;
   }
 
   if (sTLGlobalData.uiClientCnt)
   {
       PVR_DPF((PVR_DBG_ERROR, "TLDeInit transport layer but %d client streams are still connected", sTLGlobalData.uiClientCnt));
       sTLGlobalData.uiClientCnt = 0;
   }
 
   /* Clean up the SNODE list */
   if (sTLGlobalData.psHead)
   {
       while (sTLGlobalData.psHead)
       {
           RemoveAndFreeStreamNode(sTLGlobalData.psHead);
       }
       /* Leave psHead NULL on loop exit */
   }
 
   /* Clean up the TL global event object */
   if (sTLGlobalData.hTLEventObj)
   {
       OSEventObjectDestroy(sTLGlobalData.hTLEventObj);
       sTLGlobalData.hTLEventObj = NULL;
   }
 
   /* Destroy the TL global data lock */
   if (sTLGlobalData.hTLGDLock)
   {
       OSLockDestroy (sTLGlobalData.hTLGDLock);
       sTLGlobalData.hTLGDLock = NULL;
   }
 
   sTLGlobalData.psRgxDevNode = NULL;
 
   PVR_DPF_RETURN;
}
 
PVRSRV_DEVICE_NODE*
TLGetGlobalRgxDevice(void)
{
   PVRSRV_DEVICE_NODE *p = (PVRSRV_DEVICE_NODE*)(TLGGD()->psRgxDevNode);
   if (!p)
   {
       PVR_DPF((PVR_DBG_ERROR, "TLGetGlobalRgxDevice() NULL node ptr, TL " \
               "can not be used when no RGX device has been found"));
       PVR_ASSERT(p);
   }
   return p;
}
 
void TLAddStreamNode(PTL_SNODE psAdd)
{
   PVR_DPF_ENTERED;
 
   PVR_ASSERT(psAdd);
   psAdd->psNext = TLGGD()->psHead;
   TLGGD()->psHead = psAdd;
   
   PVR_DPF_RETURN;
}
 
PTL_SNODE TLFindStreamNodeByName(const IMG_CHAR *pszName)
{
   TL_GLOBAL_DATA*  psGD = TLGGD();
   PTL_SNODE          psn;
 
   PVR_DPF_ENTERED;
 
   PVR_ASSERT(pszName);
 
   for (psn = psGD->psHead; psn; psn=psn->psNext)
   {
       if (psn->psStream && OSStringCompare(psn->psStream->szName, pszName)==0)
       {
           PVR_DPF_RETURN_VAL(psn);
       }
   }
 
   PVR_DPF_RETURN_VAL(NULL);
}
 
PTL_SNODE TLFindStreamNodeByDesc(PTL_STREAM_DESC psDesc)
{
   TL_GLOBAL_DATA*  psGD = TLGGD();
   PTL_SNODE          psn;
 
   PVR_DPF_ENTERED;
 
   PVR_ASSERT(psDesc);
 
   for (psn = psGD->psHead; psn; psn=psn->psNext)
   {
       if (psn->psRDesc == psDesc || psn->psWDesc == psDesc)
       {
           PVR_DPF_RETURN_VAL(psn);
       }
   }
   PVR_DPF_RETURN_VAL(NULL);
}
 
static inline IMG_BOOL IsDigit(IMG_CHAR c)
{
   return c >= '0' && c <= '9';
}
 
static inline IMG_BOOL ReadNumber(const IMG_CHAR *pszBuffer,
                                  IMG_UINT32 *pui32Number)
{
   IMG_CHAR acTmp[11] = {0}; // max 10 digits
   IMG_UINT32 ui32Result;
   IMG_UINT i;
 
   for (i = 0; i < sizeof(acTmp) - 1; i++)
   {
       if (!IsDigit(*pszBuffer))
           break;
       acTmp[i] = *pszBuffer++;
   }
 
   /* if there are no digits or there is something after the number */
   if (i == 0 || *pszBuffer != '\0')
       return IMG_FALSE;
 
   if (OSStringToUINT32(acTmp, 10, &ui32Result) != PVRSRV_OK)
       return IMG_FALSE;
 
   *pui32Number = ui32Result;
 
   return IMG_TRUE;
}
 
/**
 * Matches pszPattern against pszName and stores results in pui32Numbers.
 *
 * @Input pszPattern this is a beginning part of the name string that should
 *                   be followed by a number.
 * @Input pszName name of the stream
 * @Output pui32Number will contain numbers from stream's name end e.g.
 *                     1234 for name abc_1234
 * @Return IMG_TRUE when a stream was found or IMG_FALSE if not
 */
static IMG_BOOL MatchNamePattern(const IMG_CHAR *pszNamePattern,
                                 const IMG_CHAR *pszName,
                                 IMG_UINT32 *pui32Number)
{
   IMG_UINT uiPatternLen;
 
   uiPatternLen = OSStringLength(pszNamePattern);
 
   if (OSStringNCompare(pszNamePattern, pszName, uiPatternLen) != 0)
       return IMG_FALSE;
 
   return ReadNumber(pszName + uiPatternLen, pui32Number);
}
 
IMG_UINT32 TLDiscoverStreamNodes(const IMG_CHAR *pszNamePattern,
                                 IMG_UINT32 *pui32Streams,
                                 IMG_UINT32 ui32Max)
{
   TL_GLOBAL_DATA *psGD = TLGGD();
   PTL_SNODE psn;
   IMG_UINT32 ui32Count = 0;
 
   PVR_ASSERT(pszNamePattern);
 
   for (psn = psGD->psHead; psn; psn = psn->psNext)
   {
       IMG_UINT32 ui32Number = 0;
 
       if (!MatchNamePattern(pszNamePattern, psn->psStream->szName,
                             &ui32Number))
           continue;
 
       if (pui32Streams != NULL)
       {
           if (ui32Count > ui32Max)
               break;
 
           pui32Streams[ui32Count] = ui32Number;
       }
 
       ui32Count++;
   }
 
   return ui32Count;
}
 
PTL_SNODE TLFindAndGetStreamNodeByDesc(PTL_STREAM_DESC psDesc)
{
   PTL_SNODE psn;
 
   PVR_DPF_ENTERED;
 
   psn = TLFindStreamNodeByDesc(psDesc);
   if (psn == NULL)
       PVR_DPF_RETURN_VAL(NULL);
 
   PVR_ASSERT(psDesc == psn->psWDesc);
 
   psn->uiWRefCount++;
   psDesc->uiRefCount++;
 
   PVR_DPF_RETURN_VAL(psn);
}
 
void TLReturnStreamNode(PTL_SNODE psNode)
{
   psNode->uiWRefCount--;
   psNode->psWDesc->uiRefCount--;
 
   PVR_ASSERT(psNode->uiWRefCount > 0);
   PVR_ASSERT(psNode->psWDesc->uiRefCount > 0);
}
 
IMG_BOOL TLTryRemoveStreamAndFreeStreamNode(PTL_SNODE psRemove)
{
   PVR_DPF_ENTERED;
 
   PVR_ASSERT(psRemove);
 
   /* If there is a client connected to this stream, defer stream's deletion */
   if (psRemove->psRDesc != NULL || psRemove->psWDesc != NULL)
   {
       PVR_DPF_RETURN_VAL (IMG_FALSE);
   }
 
   /* Remove stream from TL_GLOBAL_DATA's list and free stream node */    
   psRemove->psStream = NULL;
   RemoveAndFreeStreamNode(psRemove);
 
   PVR_DPF_RETURN_VAL (IMG_TRUE);
}
 
IMG_BOOL TLUnrefDescAndTryFreeStreamNode(PTL_SNODE psNodeToRemove,
                                          PTL_STREAM_DESC psSD)
{
   PVR_DPF_ENTERED;
 
   PVR_ASSERT(psNodeToRemove);
   PVR_ASSERT(psSD);
 
   /* Decrement reference count. For descriptor obtained by reader it must
    * reach 0 (only single reader allowed) and for descriptors obtained by
    * writers it must reach value greater or equal to 0 (multiple writers
    * model). */
   psSD->uiRefCount--;
 
   if (psSD == psNodeToRemove->psRDesc)
   {
       PVR_ASSERT(0 == psSD->uiRefCount);
       /* Remove stream descriptor (i.e. stream reader context) */
       psNodeToRemove->psRDesc = NULL;
   }
   else if (psSD == psNodeToRemove->psWDesc)
   {
       PVR_ASSERT(0 <= psSD->uiRefCount);
 
       psNodeToRemove->uiWRefCount--;
 
       /* Remove stream descriptor if reference == 0 */
       if (0 == psSD->uiRefCount)
       {
           psNodeToRemove->psWDesc = NULL;
       }
   }
 
   /* Do not Free Stream Node if there is a write reference (a producer
    * context) to the stream */
   if (NULL != psNodeToRemove->psRDesc || NULL != psNodeToRemove->psWDesc ||
       0 != psNodeToRemove->uiWRefCount)
   {
       PVR_DPF_RETURN_VAL (IMG_FALSE);
   }
 
   /* Make stream pointer NULL to prevent it from being destroyed in
    * RemoveAndFreeStreamNode Cleanup of stream should be done by the calling
    * context */
   psNodeToRemove->psStream = NULL;
   RemoveAndFreeStreamNode(psNodeToRemove);
   
   PVR_DPF_RETURN_VAL (IMG_TRUE);
}