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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
/*************************************************************************/ /*!
@File
@Title          Debug Driver
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@Description    32 Bit kernel mode debug 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.
*/ /**************************************************************************/
 
#if defined(_WIN32)
#pragma  warning(disable:4201)
#pragma  warning(disable:4214)
#pragma  warning(disable:4115)
#pragma  warning(disable:4514)
 
 
#include <ntddk.h>
#include <windef.h>
#include <winerror.h>
#endif /* _WIN32 */
 
#ifdef LINUX
#include <linux/string.h>
#endif
 
#if defined (__QNXNTO__) || defined (INTEGRITY_OS)
#include <string.h>
#endif
 
#include "img_types.h"
#include "img_defs.h"
#include "pvr_debug.h"
#include "dbgdrvif_srv5.h"
#include "dbgdriv.h"
#include "hostfunc.h"
 
#ifdef _WIN32
#pragma  warning(default:4214)
#pragma  warning(default:4115)
#endif /* _WIN32 */
 
 
/******************************************************************************
 Types
******************************************************************************/
 
/*
   Per-buffer control structure.
*/
typedef struct _DBG_STREAM_
{
   struct _DBG_STREAM_* psNext;
   struct _DBG_STREAM_* psInitStream;
   struct _DBG_STREAM_* psDeinitStream;
   IMG_UINT32 ui32Flags;            /*!< flags (see DEBUG_FLAGS) */
   void *pvBase;
   IMG_UINT32 ui32Size;
   IMG_UINT32 ui32RPtr;
   IMG_UINT32 ui32WPtr;
 
   IMG_UINT32 ui32Marker;            /*!< Size marker for file splitting */
 
   IMG_UINT32 ui32InitPhaseWOff;    /*!< snapshot offset for init phase end for follow-on pdump */
 
   IMG_CHAR   szName[DEBUG_STREAM_NAME_MAX];            /* Give this a size, some compilers don't like [] */
} DBG_STREAM;
 
/* Check 4xDBG_STREAM will fit in one page */
static_assert((sizeof(DBG_STREAM) * 4) < HOST_PAGESIZE, "DBG_STREAM is too large");
 
/******************************************************************************
 Global variables
******************************************************************************/
 
static PDBG_STREAM          g_psStreamList = 0;
 
/* Mutex used to prevent UM threads (via the dbgdrv ioctl interface) and KM
 * threads (from pvrsrvkm via the ExtDBG API) entering the debug driver core
 * and changing the state of share data at the same time.
 */
void *                      g_pvAPIMutex=NULL;
 
static IMG_UINT32            g_PDumpCurrentFrameNo = 0;
 
DBGKM_SERVICE_TABLE g_sDBGKMServices =
{
   sizeof (DBGKM_SERVICE_TABLE),
   ExtDBGDrivCreateStream,
   ExtDBGDrivDestroyStream,
   ExtDBGDrivWrite2,
   ExtDBGDrivSetMarker,
   ExtDBGDrivWaitForEvent,
   ExtDBGDrivGetCtrlState,
   ExtDBGDrivSetFrame
};
 
 
/***************************************************************************
 Forward declarations
***************************************************************************/
 
IMG_BOOL   IMG_CALLCONV DBGDrivCreateStream(IMG_CHAR *pszName, IMG_UINT32 ui32Flags, IMG_UINT32 ui32Pages, IMG_HANDLE* phInit, IMG_HANDLE* phMain, IMG_HANDLE* phDeinit);
void   IMG_CALLCONV DBGDrivDestroyStream(IMG_HANDLE hInit,IMG_HANDLE hMain, IMG_HANDLE hDeinit);
void * IMG_CALLCONV DBGDrivFindStream(IMG_CHAR * pszName, IMG_BOOL bResetStream);
IMG_UINT32 IMG_CALLCONV DBGDrivRead(PDBG_STREAM psStream, IMG_UINT32 ui32BufID, IMG_UINT32 ui32OutBufferSize,IMG_UINT8 *pui8OutBuf);
void   IMG_CALLCONV DBGDrivSetCaptureMode(PDBG_STREAM psStream,IMG_UINT32 ui32Mode,IMG_UINT32 ui32Start,IMG_UINT32 ui32Stop,IMG_UINT32 ui32SampleRate);
IMG_UINT32 IMG_CALLCONV DBGDrivWrite2(PDBG_STREAM psStream,IMG_UINT8 *pui8InBuf,IMG_UINT32 ui32InBuffSize);
void   IMG_CALLCONV DBGDrivSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker);
IMG_UINT32 IMG_CALLCONV DBGDrivGetMarker(PDBG_STREAM psStream);
void   IMG_CALLCONV DBGDrivWaitForEvent(DBG_EVENT eEvent);
IMG_UINT32 IMG_CALLCONV DBGDrivGetCtrlState(PDBG_STREAM psStream, IMG_UINT32 ui32StateID);
IMG_UINT32 IMG_CALLCONV DBGDrivGetFrame(void);
void   IMG_CALLCONV DBGDrivSetFrame(IMG_UINT32 ui32Frame);
void   DestroyAllStreams(void);
 
/* Static function declarations */
static IMG_UINT32 SpaceInStream(PDBG_STREAM psStream);
static IMG_BOOL ExpandStreamBuffer(PDBG_STREAM psStream, IMG_UINT32 ui32NewSize);
static void InvalidateAllStreams(void);
 
 
/*****************************************************************************
 Code
*****************************************************************************/
 
/*!
 @name    ExtDBGDrivCreateStream
 */
IMG_BOOL IMG_CALLCONV ExtDBGDrivCreateStream(IMG_CHAR *pszName, IMG_UINT32 ui32Flags, IMG_UINT32 ui32Size, IMG_HANDLE* phInit, IMG_HANDLE* phMain, IMG_HANDLE* phDeinit)
{
   IMG_BOOL pvRet;
 
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   pvRet=DBGDrivCreateStream(pszName, ui32Flags, ui32Size, phInit, phMain, phDeinit);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return pvRet;
}
 
/*!
 @name    ExtDBGDrivDestroyStream
 */
void IMG_CALLCONV ExtDBGDrivDestroyStream(IMG_HANDLE hInit,IMG_HANDLE hMain, IMG_HANDLE hDeinit)
{
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   DBGDrivDestroyStream(hInit, hMain, hDeinit);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return;
}
 
/*!
 @name    ExtDBGDrivFindStream
 */
void * IMG_CALLCONV ExtDBGDrivFindStream(IMG_CHAR * pszName, IMG_BOOL bResetStream)
{
   void *    pvRet;
 
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   pvRet=DBGDrivFindStream(pszName, bResetStream);
   if (pvRet == NULL)
   {
       PVR_DPF((PVR_DBG_ERROR, "ExtDBGDrivFindStream: Stream not found"));
   }
 
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return pvRet;
}
 
/*!
 @name    ExtDBGDrivRead
 */
IMG_UINT32 IMG_CALLCONV ExtDBGDrivRead(PDBG_STREAM psStream, IMG_UINT32 ui32BufID, IMG_UINT32 ui32OutBuffSize,IMG_UINT8 * pui8OutBuf)
{
   IMG_UINT32 ui32Ret;
 
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   ui32Ret=DBGDrivRead(psStream, ui32BufID, ui32OutBuffSize, pui8OutBuf);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return ui32Ret;
}
 
/*!
 @name    ExtDBGDrivWrite2
 */
IMG_UINT32 IMG_CALLCONV ExtDBGDrivWrite2(PDBG_STREAM psStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize)
{
   IMG_UINT32    ui32Ret;
 
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   ui32Ret=DBGDrivWrite2(psStream, pui8InBuf, ui32InBuffSize);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return ui32Ret;
}
 
/*!
 @name    ExtDBGDrivSetMarker
 */
void IMG_CALLCONV ExtDBGDrivSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker)
{
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   DBGDrivSetMarker(psStream, ui32Marker);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return;
}
 
/*!
 @name    ExtDBGDrivGetMarker
 */
IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetMarker(PDBG_STREAM psStream)
{
   IMG_UINT32    ui32Marker;
 
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   ui32Marker = DBGDrivGetMarker(psStream);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return ui32Marker;
}
 
/*!
 @name    ExtDBGDrivWaitForEvent
 */
void IMG_CALLCONV ExtDBGDrivWaitForEvent(DBG_EVENT eEvent)
{
#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
   DBGDrivWaitForEvent(eEvent);
#else    /* defined(SUPPORT_DBGDRV_EVENT_OBJECTS) */
   PVR_UNREFERENCED_PARAMETER(eEvent);                /* PRQA S 3358 */
#endif    /* defined(SUPPORT_DBGDRV_EVENT_OBJECTS) */
}
 
 
/*!
 @name    ExtDBGDrivGetCtrlState
 */
IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetCtrlState(PDBG_STREAM psStream, IMG_UINT32 ui32StateID)
{
   IMG_UINT32 ui32State = 0;
 
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   ui32State = DBGDrivGetCtrlState(psStream, ui32StateID);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return ui32State;
}
 
/*!
 @name    ExtDBGDrivGetFrame
 */
IMG_UINT32 IMG_CALLCONV ExtDBGDrivGetFrame(void)
{
   IMG_UINT32 ui32Frame = 0;
 
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   ui32Frame = DBGDrivGetFrame();
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return ui32Frame;
}
 
/*!
 @name    ExtDBGDrivGetCtrlState
 */
void IMG_CALLCONV ExtDBGDrivSetFrame(IMG_UINT32 ui32Frame)
{
   /* Acquire API Mutex */
   HostAquireMutex(g_pvAPIMutex);
 
   DBGDrivSetFrame(ui32Frame);
 
   /* Release API Mutex */
   HostReleaseMutex(g_pvAPIMutex);
 
   return;
}
 
 
 
/*!****************************************************************************
 @name        AtoI
 @brief        Returns the integer value of a decimal string
 @param        szIn - String with hexadecimal value
 @return    IMG_UINT32 integer value, 0 if string is null or not valid
               Based on Max`s one, now copes with (only) hex ui32ords, upper or lower case a-f.
*****************************************************************************/
IMG_UINT32 AtoI(IMG_CHAR *szIn)
{
   IMG_INT        iLen = 0;
   IMG_UINT32    ui32Value = 0;
   IMG_UINT32    ui32Digit=1;
   IMG_UINT32    ui32Base=10;
   IMG_INT        iPos;
   IMG_CHAR    bc;
 
   //get len of string
   while (szIn[iLen] > 0)
   {
       iLen ++;
   }
 
   //nothing to do
   if (iLen == 0)
   {
       return (0);
   }
 
   /* See if we have an 'x' or 'X' before the number to make it a hex number */
   iPos=0;
   while (szIn[iPos] == '0')
   {
       iPos++;
   }
   if (szIn[iPos] == '\0')
   {
       return 0;
   }
   if (szIn[iPos] == 'x' || szIn[iPos] == 'X')
   {
       ui32Base=16;
       szIn[iPos]='0';
   }
 
   //go through string from right (least significant) to left
   for (iPos = iLen - 1; iPos >= 0; iPos --)
   {
       bc = szIn[iPos];
 
       if ( (bc >= 'a') && (bc <= 'f') && ui32Base == 16)            //handle lower case a-f
       {
           bc -= 'a' - 0xa;
       }
       else
       if ( (bc >= 'A') && (bc <= 'F') && ui32Base == 16)            //handle upper case A-F
       {
           bc -= 'A' - 0xa;
       }
       else
       if ((bc >= '0') && (bc <= '9'))                //if char out of range, return 0
       {
           bc -= '0';
       }
       else
           return (0);
 
       ui32Value += (IMG_UINT32)bc  * ui32Digit;
 
       ui32Digit = ui32Digit * ui32Base;
   }
   return (ui32Value);
}
 
 
/*!****************************************************************************
 @name        StreamValid
 @brief        Validates supplied debug buffer.
 @param        psStream - debug stream
 @return    true if valid
*****************************************************************************/
static IMG_BOOL StreamValid(PDBG_STREAM psStream)
{
   PDBG_STREAM    psThis;
 
   psThis = g_psStreamList;
 
   while (psThis)
   {
       if (psStream && ((psThis == psStream) ||
                       (psThis->psInitStream == psStream) ||
                       (psThis->psDeinitStream == psStream)) )
       {
           return(IMG_TRUE);
       }
       else
       {
           psThis = psThis->psNext;
       }
   }
 
   return(IMG_FALSE);
}
 
 
/*!****************************************************************************
 @name        StreamValidForRead
 @brief        Validates supplied debug buffer for read op.
 @param        psStream - debug stream
 @return    true if readable
*****************************************************************************/
static IMG_BOOL StreamValidForRead(PDBG_STREAM psStream)
{
   if( StreamValid(psStream) &&
       ((psStream->ui32Flags & DEBUG_FLAGS_WRITEONLY) == 0) )
   {
       return(IMG_TRUE);
   }
 
   return(IMG_FALSE);
}
 
/*!****************************************************************************
 @name        StreamValidForWrite
 @brief        Validates supplied debug buffer for write op.
 @param        psStream - debug stream
 @return    true if writable
*****************************************************************************/
static IMG_BOOL StreamValidForWrite(PDBG_STREAM psStream)
{
   if( StreamValid(psStream) &&
       ((psStream->ui32Flags & DEBUG_FLAGS_READONLY) == 0) )
   {
       return(IMG_TRUE);
   }
 
   return(IMG_FALSE);
}
 
/*!****************************************************************************
 @name        Write
 @brief        Copies data from a buffer into selected stream. Stream size is fixed.
 @param        psStream - stream for output
 @param        pui8Data - input buffer
 @param        ui32InBuffSize - size of input
 @return    none
*****************************************************************************/
static void Write(PDBG_STREAM psStream,IMG_PUINT8 pui8Data,IMG_UINT32 ui32InBuffSize)
{
   /*
       Split copy into two bits as necessary (if we're allowed to wrap).
   */
   if ((psStream->ui32Flags & DEBUG_FLAGS_CIRCULAR) == 0)
   {
       PVR_ASSERT( (psStream->ui32WPtr + ui32InBuffSize) < psStream->ui32Size );
   }
 
   if ((psStream->ui32WPtr + ui32InBuffSize) > psStream->ui32Size)
   {
       /* Yes we need two bits, calculate their sizes */
       IMG_UINT32 ui32B1 = psStream->ui32Size - psStream->ui32WPtr;
       IMG_UINT32 ui32B2 = ui32InBuffSize - ui32B1;
 
       /* Copy first block to current location */
       HostMemCopy((void *)((uintptr_t)psStream->pvBase + psStream->ui32WPtr),
               (void *) pui8Data,
               ui32B1);
 
       /* Copy second block to start of buffer */
       HostMemCopy(psStream->pvBase,
               (void *)(pui8Data + ui32B1),
               ui32B2);
 
       /* Set pointer to be the new end point */
       psStream->ui32WPtr = ui32B2;
   }
   else
   {    /* Can fit block in single chunk */
       HostMemCopy((void *)((uintptr_t)psStream->pvBase + psStream->ui32WPtr),
               (void *) pui8Data,
               ui32InBuffSize);
 
       psStream->ui32WPtr += ui32InBuffSize;
 
       if (psStream->ui32WPtr == psStream->ui32Size)
       {
           psStream->ui32WPtr = 0;
       }
   }
}
 
 
/*!****************************************************************************
 @name        WriteExpandingBuffer
 @brief        Copies data from a buffer into selected stream. Stream size may be expandable.
 @param        psStream - stream for output
 @param        pui8InBuf - input buffer
 @param        ui32InBuffSize - size of input
 @return    bytes copied
*****************************************************************************/
static IMG_UINT32 WriteExpandingBuffer(PDBG_STREAM psStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize)
{
   IMG_UINT ui32Space;
 
   /*
       How much space have we got in the buffer ?
   */
   ui32Space = SpaceInStream(psStream);
 
   /*
       Check if we can expand the buffer 
   */
   if (psStream->ui32Flags & DEBUG_FLAGS_NO_BUF_EXPANDSION)
   {
       /*
           Don't do anything if we've got less that 32 ui8tes of space and
           we're not allowing expansion of buffer space...
       */
       if (ui32Space < 32)
       {
           PVR_DPF((PVR_DBG_ERROR, "WriteExpandingBuffer: buffer %p is full and isn't expandable", psStream));
           return(0);
       }
   }
   else
   {
       if ((ui32Space < 32) || (ui32Space <= (ui32InBuffSize + 4)))
       {
           IMG_UINT32    ui32NewBufSize;
 
           /*
               Find new buffer size, double the current size or increase by 1MB
           */
           ui32NewBufSize = MIN(psStream->ui32Size<<1,psStream->ui32Size+(1<<20));
           ui32NewBufSize = MIN(ui32NewBufSize, PDUMP_STREAMBUF_MAX_SIZE_MB<<20);
           PVR_DPF((PVR_DBGDRIV_MESSAGE, "Expanding buffer size = %x, new size = %x",
                   psStream->ui32Size, ui32NewBufSize));
 
           if (ui32InBuffSize > psStream->ui32Size)
           {
               ui32NewBufSize += ui32InBuffSize;
               PVR_DPF((PVR_DBG_ERROR, "WriteExpandingBuffer: buffer %p is expanding by size of input buffer %u", psStream, ui32NewBufSize));
           }
 
           /* 
               Attempt to expand the buffer 
           */
           if ((ui32NewBufSize < psStream->ui32Size) ||
                   !ExpandStreamBuffer(psStream,ui32NewBufSize))
           {
               if (ui32Space < 32)
               {
                   if((psStream->ui32Flags & DEBUG_FLAGS_CIRCULAR) != 0)
                   {
                       return(0);
                   }
                   else
                   {
                       /* out of memory */
                       PVR_DPF((PVR_DBG_ERROR, "WriteExpandingBuffer: Unable to expand %p. Out of memory.", psStream));
                       InvalidateAllStreams();
                       return (0xFFFFFFFFUL);
                   }
               }
           }
 
           /* 
               Recalc the space in the buffer 
           */
           ui32Space = SpaceInStream(psStream);
           PVR_DPF((PVR_DBGDRIV_MESSAGE, "Expanded buffer, free space = %x",
                   ui32Space));
       }
   }
 
   /*
       Only copy what we can..
   */
   if (ui32Space <= (ui32InBuffSize + 4))
   {
       ui32InBuffSize = ui32Space - 4;
   }
 
   /*
       Write the stuff...
   */
   Write(psStream,pui8InBuf,ui32InBuffSize);
 
#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
   if (ui32InBuffSize)
   {
       HostSignalEvent(DBG_EVENT_STREAM_DATA);
   }
#endif
   return(ui32InBuffSize);
}
 
/*****************************************************************************
******************************************************************************
******************************************************************************
 THE ACTUAL FUNCTIONS
******************************************************************************
******************************************************************************
*****************************************************************************/
 
static void DBGDrivSetStreamName(PDBG_STREAM psStream,
                                    IMG_CHAR* pszBase,
                                    IMG_CHAR* pszExt)
{
   IMG_CHAR* pCh = psStream->szName;
   IMG_CHAR* pChEnd = psStream->szName+DEBUG_STREAM_NAME_MAX-8;
   IMG_CHAR* pSrcCh;
   IMG_CHAR* pSrcChEnd;
 
   for (pSrcCh = pszBase, pSrcChEnd = pszBase+strlen(pszBase);
           (pSrcCh < pSrcChEnd) && (pCh < pChEnd) ;
           pSrcCh++, pCh++)
   {
       *pCh = *pSrcCh;
   }
 
   for (pSrcCh = pszExt, pSrcChEnd = pszExt+strlen(pszExt);
           (pSrcCh < pSrcChEnd) && (pCh < pChEnd) ;
           pSrcCh++, pCh++)
   {
       *pCh = *pSrcCh;
   }
 
   *pCh = '\0';
}
 
/*!****************************************************************************
 @name        DBGDrivCreateStream
 @brief        Creates a pdump/debug stream
 @param        pszName - stream name
 @param        ui32Flags - output flags, text stream bit is set for pdumping
 @param        ui32Size - size of stream buffer in pages
 @return    none
*****************************************************************************/
IMG_BOOL IMG_CALLCONV DBGDrivCreateStream(IMG_CHAR *pszName,
                                          IMG_UINT32 ui32Flags,
                                          IMG_UINT32 ui32Size,
                                          IMG_HANDLE* phInit,
                                          IMG_HANDLE* phMain,
                                          IMG_HANDLE* phDeinit)
{
   IMG_BOOL            bUseNonPagedMem4Buffers = ((ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0);
   PDBG_STREAM         psStream = NULL;
   PDBG_STREAM            psInitStream = NULL;
   PDBG_STREAM         psStreamDeinit = NULL;
   void*           pvBase = NULL;
 
   /*
       If we already have a buffer using this name just return
       its handle.
   */
   psStream = (PDBG_STREAM) DBGDrivFindStream(pszName, IMG_FALSE);
   if (psStream)
   {
       *phInit = psStream->psInitStream;
       *phMain = psStream;
       *phDeinit = psStream->psDeinitStream;
       return IMG_TRUE;
   }
 
   /*
       Allocate memory for control structures
   */
   psStream = HostNonPageablePageAlloc(1);
   if    (!psStream)
   {
       PVR_DPF((PVR_DBG_ERROR,"DBGDriv: Couldn't alloc control structs\n\r"));
       goto errCleanup;
   }
   psInitStream = psStream+1;
   psStreamDeinit = psStream+2;
 
 
   /* Allocate memory for Main buffer */
   psStream->pvBase = NULL;
   if (bUseNonPagedMem4Buffers)
   {
       pvBase = HostNonPageablePageAlloc(ui32Size);
   }
   else
   {
       pvBase = HostPageablePageAlloc(ui32Size);
   }
 
   if (!pvBase)
   {
       PVR_DPF((PVR_DBG_ERROR,"DBGDriv: Couldn't alloc Stream buffer\n\r"));
       goto errCleanup;
   }
 
   /*
       Setup debug buffer state.
   */
   psStream->psNext = 0;
   psStream->pvBase = pvBase;
   psStream->ui32Flags = ui32Flags | DEBUG_FLAGS_CIRCULAR;
   psStream->ui32Size = ui32Size * HOST_PAGESIZE;
   psStream->ui32RPtr = 0;
   psStream->ui32WPtr = 0;
   psStream->ui32Marker = 0;
   psStream->ui32InitPhaseWOff = 0;
   DBGDrivSetStreamName(psStream, pszName, "");
   PVR_DPF((PVR_DBG_MESSAGE,"DBGDriv: Created stream with deinit name (%s)\n\r", psStream->szName));
 
   /* Allocate memory for Init buffer */
   psInitStream->pvBase = NULL;
   if (bUseNonPagedMem4Buffers)
   {
       pvBase = HostNonPageablePageAlloc(ui32Size);
   }
   else
   {
       pvBase = HostPageablePageAlloc(ui32Size);
   }
 
   if (!pvBase)
   {
       PVR_DPF((PVR_DBG_ERROR,"DBGDriv: Couldn't alloc InitStream buffer\n\r"));
       goto errCleanup;
   }
 
   /* Initialise the stream for the Init phase */
   psInitStream->psNext = psInitStream->psInitStream = psInitStream->psDeinitStream = NULL;
   psInitStream->ui32Flags = ui32Flags;
   psInitStream->pvBase = pvBase;
   psInitStream->ui32Size = ui32Size * HOST_PAGESIZE;
   psInitStream->ui32RPtr = 0;
   psInitStream->ui32WPtr = 0;
   psInitStream->ui32Marker = 0;
   psInitStream->ui32InitPhaseWOff = 0;
   DBGDrivSetStreamName(psInitStream, pszName, "_Init");
   PVR_DPF((PVR_DBG_MESSAGE,"DBGDriv: Created stream with init name (%s)\n\r", psInitStream->szName));
   psStream->psInitStream = psInitStream;
 
   /* Allocate memory for Deinit buffer */
   psStreamDeinit->pvBase = NULL;
   if (bUseNonPagedMem4Buffers)
   {
       pvBase = HostNonPageablePageAlloc(1);
   }
   else
   {
       pvBase = HostPageablePageAlloc(1);
   }
 
   if (!pvBase)
   {
       PVR_DPF((PVR_DBG_ERROR,"DBGDriv: Couldn't alloc DeinitStream buffer\n\r"));
       goto errCleanup;
   }
 
   /* Initialise the stream for the Deinit phase */
   psStreamDeinit->psNext = psStreamDeinit->psInitStream = psStreamDeinit->psDeinitStream = NULL;
   psStreamDeinit->pvBase = pvBase;
   psStreamDeinit->ui32Flags = ui32Flags;
   psStreamDeinit->ui32Size = HOST_PAGESIZE;
   psStreamDeinit->ui32RPtr = 0;
   psStreamDeinit->ui32WPtr = 0;
   psStreamDeinit->ui32Marker = 0;
   psStreamDeinit->ui32InitPhaseWOff = 0;
   DBGDrivSetStreamName(psStreamDeinit, pszName, "_Deinit");
   PVR_DPF((PVR_DBG_MESSAGE,"DBGDriv: Created stream with deinit name (%s)\n\r", psStreamDeinit->szName));
 
   psStream->psDeinitStream = psStreamDeinit;
 
   /*
       Insert into list.
   */
   psStream->psNext = g_psStreamList;
   g_psStreamList = psStream;
 
   AddSIDEntry(psStream);
   
   *phInit = psStream->psInitStream;
   *phMain = psStream;
   *phDeinit = psStream->psDeinitStream;
 
   return IMG_TRUE;
 
errCleanup:
   if (bUseNonPagedMem4Buffers)
   {
       if (psStream) HostNonPageablePageFree(psStream->pvBase);
       if (psInitStream) HostNonPageablePageFree(psInitStream->pvBase);
       if (psStreamDeinit) HostNonPageablePageFree(psStreamDeinit->pvBase);
   }
   else
   {
       if (psStream) HostPageablePageFree(psStream->pvBase);
       if (psInitStream) HostPageablePageFree(psInitStream->pvBase);
       if (psStreamDeinit) HostPageablePageFree(psStreamDeinit->pvBase);
   }
   HostNonPageablePageFree(psStream);
   psStream = psInitStream = psStreamDeinit = NULL;
   return IMG_FALSE;
}
 
/*!****************************************************************************
 @name        DBGDrivDestroyStream
 @brief        Delete a stream and free its memory
 @param        psStream - stream to be removed
 @return    none
*****************************************************************************/
void IMG_CALLCONV DBGDrivDestroyStream(IMG_HANDLE hInit,IMG_HANDLE hMain, IMG_HANDLE hDeinit)
{
   PDBG_STREAM psStreamInit = (PDBG_STREAM) hInit;
   PDBG_STREAM psStream = (PDBG_STREAM) hMain;
   PDBG_STREAM    psStreamDeinit = (PDBG_STREAM) hDeinit;
   PDBG_STREAM    psStreamThis;
   PDBG_STREAM    psStreamPrev;
 
   PVR_DPF((PVR_DBG_MESSAGE, "DBGDriv: Destroying stream %s\r\n", psStream->szName ));
 
   /*
       Validate buffer.
   */
   if (!StreamValid(psStream))
   {
       return;
   }
 
   RemoveSIDEntry(psStream);
   
   /*
       Remove from linked list.
   */
   psStreamThis = g_psStreamList;
   psStreamPrev = 0;
 
   while (psStreamThis)
   {
       if (psStreamThis == psStream)
       {
           if (psStreamPrev)
           {
               psStreamPrev->psNext = psStreamThis->psNext;
           }
           else
           {
               g_psStreamList = psStreamThis->psNext;
           }
 
           psStreamThis = 0;
       }
       else
       {
           psStreamPrev = psStreamThis;
           psStreamThis = psStreamThis->psNext;
       }
   }
 
   /*
       And free its memory.
   */
   if ((psStream->ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
   {
       HostNonPageablePageFree(psStream->pvBase);
       HostNonPageablePageFree(psStreamInit->pvBase);
       HostNonPageablePageFree(psStreamDeinit->pvBase);
   }
   else
   {
       HostPageablePageFree(psStream->pvBase);
       HostPageablePageFree(psStreamInit->pvBase);
       HostPageablePageFree(psStreamDeinit->pvBase);
   }
 
   /* Free the shared page used for the three stream tuple */
   HostNonPageablePageFree(psStream);
   psStream = psStreamInit = psStreamDeinit = NULL;
 
   if (g_psStreamList == 0)
   {
       PVR_DPF((PVR_DBG_MESSAGE,"DBGDriv: Stream list now empty" ));
   }
 
   return;
}
 
/*!****************************************************************************
 @name        DBGDrivFindStream
 @brief        Finds/resets a named stream
 @param        pszName - stream name
 @param        bResetStream - whether to reset the stream, e.g. to end pdump init phase
 @return    none
*****************************************************************************/
void * IMG_CALLCONV DBGDrivFindStream(IMG_CHAR * pszName, IMG_BOOL bResetStream)
{
   PDBG_STREAM    psStream;
   PDBG_STREAM    psThis;
   IMG_UINT32    ui32Off;
   IMG_BOOL    bAreSame;
 
   psStream = 0;
 
   PVR_DPF((PVR_DBGDRIV_MESSAGE, "PDump client connecting to %s %s",
           pszName,
           (bResetStream == IMG_TRUE) ? "with reset" : "no reset"));
 
   /*
       Scan buffer names for supplied one.
   */
   for (psThis = g_psStreamList; psThis != NULL; psThis = psThis->psNext)
   {
       bAreSame = IMG_TRUE;
       ui32Off = 0;
 
       if (strlen(psThis->szName) == strlen(pszName))
       {
           while ((ui32Off < DEBUG_STREAM_NAME_MAX) && (psThis->szName[ui32Off] != 0) && (pszName[ui32Off] != 0) && bAreSame)
           {
               if (psThis->szName[ui32Off] != pszName[ui32Off])
               {
                   bAreSame = IMG_FALSE;
               }
 
               ui32Off++;
           }
       }
       else
       {
           bAreSame = IMG_FALSE;
       }
 
       if (bAreSame)
       {
           psStream = psThis;
           break;
       }
   }
 
   if(psStream)
   {
       psStream->psInitStream->ui32RPtr = 0;
       psStream->psDeinitStream->ui32RPtr = 0;
       psStream->ui32RPtr = 0;
       if (bResetStream)
       {
           /* This will erase any data written to the main stream 
            * before the client starts. */
           psStream->ui32WPtr = 0;
       }
       psStream->ui32Marker = psStream->psInitStream->ui32Marker = 0;
 
 
       /* mark init stream to prevent further reading by pdump client */
       /* Check for possible race condition */
       psStream->psInitStream->ui32InitPhaseWOff = psStream->psInitStream->ui32WPtr;
 
       PVR_DPF((PVR_DBGDRIV_MESSAGE, "Set %s client marker bo %x",
               psStream->szName,
               psStream->psInitStream->ui32InitPhaseWOff));
   }
 
   return((void *) psStream);
}
 
static void IMG_CALLCONV DBGDrivInvalidateStream(PDBG_STREAM psStream)
{
   IMG_CHAR pszErrorMsg[] = "**OUTOFMEM\n";
   IMG_UINT32 ui32Space;
   IMG_UINT32 ui32Off = 0;
   IMG_UINT32 ui32WPtr = psStream->ui32WPtr;
   IMG_PUINT8 pui8Buffer = (IMG_UINT8 *) psStream->pvBase;
   
   PVR_DPF((PVR_DBG_ERROR, "DBGDrivInvalidateStream: An error occurred for stream %s", psStream->szName ));
 
   /*
       Validate buffer.
   */
   /*
   if (!StreamValid(psStream))
   {
       return;
   }
*/
   /* Write what we can of the error message */
   ui32Space = SpaceInStream(psStream);
 
   /* Make sure there's space for termination character */
   if(ui32Space > 0)
   {
       ui32Space--;
   }
   else
   {
       PVR_DPF((PVR_DBG_ERROR, "DBGDrivInvalidateStream: Buffer full."));
   }
 
   while((pszErrorMsg[ui32Off] != 0) && (ui32Off < ui32Space))
   {
       pui8Buffer[ui32WPtr] = (IMG_UINT8)pszErrorMsg[ui32Off];
       ui32Off++;
       ui32WPtr++;
   }
   pui8Buffer[ui32WPtr++] = '\0';
   psStream->ui32WPtr = ui32WPtr;
 
   /* Buffer will accept no more params from Services/client driver */
   psStream->ui32Flags |= DEBUG_FLAGS_READONLY;
}
 
/*!****************************************************************************
 @name        InvalidateAllStreams
 @brief        invalidate all streams in list
 @return    none
*****************************************************************************/
static void InvalidateAllStreams(void)
{
   PDBG_STREAM psStream = g_psStreamList;
   while (psStream != NULL)
   {
       DBGDrivInvalidateStream(psStream);
       DBGDrivInvalidateStream(psStream->psInitStream);
       DBGDrivInvalidateStream(psStream->psDeinitStream);
       psStream = psStream->psNext;
   }
   return;
}
 
/*!****************************************************************************
 @name        DBGDrivWrite2
 @brief        Copies data from a buffer into selected (expandable) stream.
 @param        psStream - stream for output
 @param        pui8InBuf - input buffer
 @param        ui32InBuffSize - size of input
 @return    bytes copied, 0 if recoverable error, -1 if unrecoverable error
*****************************************************************************/
IMG_UINT32 IMG_CALLCONV DBGDrivWrite2(PDBG_STREAM psStream,IMG_UINT8 * pui8InBuf,IMG_UINT32 ui32InBuffSize)
{
 
   /*
       Validate buffer.
   */
   if (!StreamValidForWrite(psStream))
   {
       PVR_DPF((PVR_DBG_ERROR, "DBGDrivWrite2: stream not valid"));
       return(0xFFFFFFFFUL);
   }
 
   PVR_DPF((PVR_DBGDRIV_MESSAGE, "Recv(exp) %d b for %s: Roff = %x, WOff = %x",
           ui32InBuffSize,
           psStream->szName,
           psStream->ui32RPtr,
           psStream->ui32WPtr));
 
   return( WriteExpandingBuffer(psStream, pui8InBuf, ui32InBuffSize) );
}
 
/*!****************************************************************************
 @name        DBGDrivRead
 @brief        Read from debug driver buffers
 @param        psMainStream - stream
 @param        ui32BufID - on of the DEBUG_READ_BUFID flags to indicate which buffer
 @param        ui32OutBuffSize - available space in client buffer
 @param        pui8OutBuf - output buffer
 @return    bytes read, 0 if failure occurred
*****************************************************************************/
IMG_UINT32 IMG_CALLCONV DBGDrivRead(PDBG_STREAM psMainStream, IMG_UINT32 ui32BufID, IMG_UINT32 ui32OutBuffSize,IMG_UINT8 * pui8OutBuf)
{
   IMG_UINT32 ui32Data;
   DBG_STREAM *psStream;
 
   /*
       Validate buffer.
   */
   if (!StreamValidForRead(psMainStream))
   {
       PVR_DPF((PVR_DBG_ERROR, "DBGDrivRead: buffer %p is invalid", psMainStream));
       return(0);
   }
 
   if(ui32BufID == DEBUG_READ_BUFID_INIT)
   {
       psStream = psMainStream->psInitStream;
   }
   else if (ui32BufID == DEBUG_READ_BUFID_DEINIT)
   {
       psStream = psMainStream->psDeinitStream;
   }
   else
   {
       psStream = psMainStream;
   }
 
   /* Don't read beyond the init phase marker point */
   if (psStream->ui32RPtr == psStream->ui32WPtr ||
       ((psStream->ui32InitPhaseWOff > 0) &&
        (psStream->ui32RPtr >= psStream->ui32InitPhaseWOff)) )
   {
       return(0);
   }
 
   /*
       Get amount of data in buffer.
   */
   if (psStream->ui32RPtr <= psStream->ui32WPtr)
   {
       ui32Data = psStream->ui32WPtr - psStream->ui32RPtr;
   }
   else
   {
       ui32Data = psStream->ui32WPtr + (psStream->ui32Size - psStream->ui32RPtr);
   }
 
   /*
       Don't read beyond the init phase marker point
   */
   if ((psStream->ui32InitPhaseWOff > 0) &&
       (psStream->ui32InitPhaseWOff < psStream->ui32WPtr))
   {
       ui32Data = psStream->ui32InitPhaseWOff - psStream->ui32RPtr;
   }
 
   /*
       Only transfer what target buffer can handle.
   */
   if (ui32Data > ui32OutBuffSize)
   {
       ui32Data = ui32OutBuffSize;
   }
 
   PVR_DPF((PVR_DBGDRIV_MESSAGE, "Send %x b from %s: Roff = %x, WOff = %x",
           ui32Data,
           psStream->szName,
           psStream->ui32RPtr,
           psStream->ui32WPtr));
 
   /*
       Split copy into two bits or one depending on W/R position.
   */
   if ((psStream->ui32RPtr + ui32Data) > psStream->ui32Size)
   {    /* Calc block 1 and block 2 sizes */
       IMG_UINT32 ui32B1 = psStream->ui32Size - psStream->ui32RPtr;
       IMG_UINT32 ui32B2 = ui32Data - ui32B1;
 
       /* Copy up to end of circular buffer */
       HostMemCopy((void *) pui8OutBuf,
               (void *)((uintptr_t)psStream->pvBase + psStream->ui32RPtr),
               ui32B1);
 
       /* Copy from start of circular buffer */
       HostMemCopy((void *)(pui8OutBuf + ui32B1),
               psStream->pvBase,
               ui32B2);
 
       /* Update read pointer now that we've copied the data out */
       psStream->ui32RPtr = ui32B2;
   }
   else
   {    /* Copy data from wherever */
       HostMemCopy((void *) pui8OutBuf,
               (void *)((uintptr_t)psStream->pvBase + psStream->ui32RPtr),
               ui32Data);
 
       /* Update read pointer now that we've copied the data out */
       psStream->ui32RPtr += ui32Data;
 
       /* Check for wrapping */
       if ((psStream->ui32RPtr != psStream->ui32WPtr) &&
           (psStream->ui32RPtr >= psStream->ui32Size))
       {
           psStream->ui32RPtr = 0;
       }
   }
 
   return(ui32Data);
}
 
/*!****************************************************************************
 @name        DBGDrivSetMarker
 @brief        Sets the marker in the stream to split output files
 @param        psStream, ui32Marker
 @return    nothing
*****************************************************************************/
void IMG_CALLCONV DBGDrivSetMarker(PDBG_STREAM psStream, IMG_UINT32 ui32Marker)
{
   /*
       Validate buffer
   */
   if (!StreamValid(psStream))
   {
       return;
   }
 
   /* Called by PDump client to reset the marker to zero after a file split */
   if ((ui32Marker == 0) && (psStream->ui32Marker == 0))
   {
       PVR_DPF((PVR_DBG_ERROR, "DBGDrivSetMarker: Client resetting marker that is already zero!"));
   }
   /* Called by pvrsrvkm to set the marker to signal a file split is required */
   if ((ui32Marker != 0) && (psStream->ui32Marker != 0))
   {
       /* In this case a previous split request is still outstanding. The
        * client has not yet actioned and acknowledged the previous
        * marker. This may be an error if the client does not catch-up and
        * the stream's written data is allowed to pass the max file
        * size again. If this happens the PDump is invalid as the offsets
        * from the script file will be incorrect.
        */
       PVR_DPF((PVR_DBG_ERROR, "DBGDrivSetMarker: Server setting marker that is already set!"));
   }
   else
   {
       PVR_DPF((PVR_DBG_MESSAGE, "DBGDrivSetMarker: Setting stream split marker to %d (was %d)", ui32Marker, psStream->ui32Marker));
   }
 
   psStream->ui32Marker = ui32Marker;
}
 
/*!****************************************************************************
 @name        DBGDrivGetMarker
 @brief     Gets the marker in the stream to split output files
 @param         psStream - stream
 @return    marker offset
*****************************************************************************/
IMG_UINT32 IMG_CALLCONV DBGDrivGetMarker(PDBG_STREAM psStream)
{
   /*
       Validate buffer
   */
   if (!StreamValid(psStream))
   {
       return 0;
   }
 
   return psStream->ui32Marker;
}
 
/*!****************************************************************************
 @name        DBGDrivGetServiceTable
 @brief        get jump table for Services driver
 @return    pointer to jump table
*****************************************************************************/
void * IMG_CALLCONV DBGDrivGetServiceTable(void)
{
   return &g_sDBGKMServices;
}
 
 
#if defined(SUPPORT_DBGDRV_EVENT_OBJECTS)
/*!****************************************************************************
 @name        DBGDrivWaitForEvent
 @brief        waits for an event
 @param        eEvent - debug driver event
 @return    void
*****************************************************************************/
void IMG_CALLCONV DBGDrivWaitForEvent(DBG_EVENT eEvent)
{
   HostWaitForEvent(eEvent);
}
#endif
 
/*!****************************************************************************
 @name        DBGDrivGetCtrlState
 @brief        Gets a state value from the debug driver or stream
 @param        psStream - stream
 @param        ui32StateID - state ID
 @return    Nothing
*****************************************************************************/
IMG_UINT32 IMG_CALLCONV DBGDrivGetCtrlState(PDBG_STREAM psStream, IMG_UINT32 ui32StateID)
{
   /* Validate buffer */
   if (!StreamValid(psStream))
   {
       return (0xFFFFFFFF);
   }
 
   /* Retrieve the state asked for */
   switch (ui32StateID)
   {
   case DBG_GET_STATE_FLAG_IS_READONLY:
       return ((psStream->ui32Flags & DEBUG_FLAGS_READONLY) != 0);
 
   case 0xFE: /* Dump the current stream state */
       PVR_DPF((PVR_DBG_CALLTRACE,
                "------ PDUMP DBGDriv: psStream( %p ) ( -- %s -- ) ui32Flags( %x )",
                psStream, psStream->szName, psStream->ui32Flags));
       PVR_DPF((PVR_DBG_CALLTRACE,
                "------ PDUMP DBGDriv: psStream->pvBase( %p ) psStream->ui32Size( %u )",
                psStream->pvBase, psStream->ui32Size));
       PVR_DPF((PVR_DBG_CALLTRACE,
                "------ PDUMP DBGDriv: psStream->ui32RPtr( %u ) psStream->ui32WPtr( %u )",
                psStream->ui32RPtr, psStream->ui32WPtr));
       PVR_DPF((PVR_DBG_CALLTRACE,
                "------ PDUMP DBGDriv: psStream->ui32Marker( %u ) psStream->ui32InitPhaseWOff( %u )",
                psStream->ui32Marker, psStream->ui32InitPhaseWOff));
       if (psStream->psInitStream)
       {
           PVR_DPF((PVR_DBG_CALLTRACE,
                    "-------- PDUMP DBGDriv: psInitStream( %p ) ( -- %s -- ) ui32Flags( %x )",
                    psStream->psInitStream, psStream->psInitStream->szName, psStream->ui32Flags));
           PVR_DPF((PVR_DBG_CALLTRACE,
                    "-------- PDUMP DBGDriv: psInitStream->pvBase( %p ) psInitStream->ui32Size( %u )",
                    psStream->psInitStream->pvBase, psStream->psInitStream->ui32Size));
           PVR_DPF((PVR_DBG_CALLTRACE,
                    "-------- PDUMP DBGDriv: psInitStream->ui32RPtr( %u ) psInitStream->ui32WPtr( %u )",
                    psStream->psInitStream->ui32RPtr, psStream->psInitStream->ui32WPtr));
           PVR_DPF((PVR_DBG_CALLTRACE,
                    "-------- PDUMP DBGDriv: psInitStream->ui32Marker( %u ) psInitStream->ui32InitPhaseWOff( %u ) ",
                    psStream->psInitStream->ui32Marker, psStream->psInitStream->ui32InitPhaseWOff));
       }
 
       break;
 
   case 0xFF: /* Dump driver state not in a stream */
       {
           PVR_DPF((PVR_DBG_CALLTRACE,
                    "------ PDUMP DBGDriv: g_psStreamList( head %p ) g_pvAPIMutex( %p ) g_PDumpCurrentFrameNo( %u )",
                    g_psStreamList, g_pvAPIMutex, g_PDumpCurrentFrameNo));
       }
       break;
 
   default:
       PVR_ASSERT(0);
   }
 
   return (0xFFFFFFFF);
}
 
IMG_UINT32 IMG_CALLCONV DBGDrivGetFrame(void)
{
   return g_PDumpCurrentFrameNo;
}
 
void IMG_CALLCONV DBGDrivSetFrame(IMG_UINT32 ui32Frame)
{
   g_PDumpCurrentFrameNo = ui32Frame;
}
 
 
/*!****************************************************************************
 @name        ExpandStreamBuffer
 @brief        allocates a new buffer when the current one is full
 @param        psStream - stream
 @param        ui32NewSize - new size
 @return    IMG_TRUE - if allocation succeeded, IMG_FALSE - if not
*****************************************************************************/
static IMG_BOOL ExpandStreamBuffer(PDBG_STREAM psStream, IMG_UINT32 ui32NewSize)
{
   void *    pvNewBuf;
   IMG_UINT32    ui32NewSizeInPages;
   IMG_UINT32    ui32NewWOffset;
   IMG_UINT32    ui32NewROffset;
   IMG_UINT32    ui32SpaceInOldBuf;
 
   /* 
       First check new size is bigger than existing size 
   */
   if (psStream->ui32Size >= ui32NewSize)
   {
       return IMG_FALSE;
   }
 
   /*
       Calc space in old buffer 
   */
   ui32SpaceInOldBuf = SpaceInStream(psStream);
 
   /*
       Allocate new buffer 
   */
   ui32NewSizeInPages = ((ui32NewSize + 0xfffUL) & ~0xfffUL) / 4096UL;
 
   if ((psStream->ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
   {
       pvNewBuf = HostNonPageablePageAlloc(ui32NewSizeInPages);
   }
   else
   {
       pvNewBuf = HostPageablePageAlloc(ui32NewSizeInPages);
   }
 
   if (pvNewBuf == NULL)
   {
       return IMG_FALSE;
   }
 
   if ((psStream->ui32Flags & DEBUG_FLAGS_CIRCULAR) != 0)
   {
       /*
           Copy over old buffer to new one, we place data at start of buffer
           even if Read offset is not at start of buffer
       */
       if (psStream->ui32RPtr <= psStream->ui32WPtr)
       {
           /*
               No wrapping of data so copy data to start of new buffer 
           */
       HostMemCopy(pvNewBuf,
                   (void *)((uintptr_t)psStream->pvBase + psStream->ui32RPtr),
                   psStream->ui32WPtr - psStream->ui32RPtr);
       }
       else
       {
           IMG_UINT32    ui32FirstCopySize;
   
           /*
               The data has wrapped around the buffer, copy beginning of buffer first 
           */
           ui32FirstCopySize = psStream->ui32Size - psStream->ui32RPtr;
   
           HostMemCopy(pvNewBuf,
                   (void *)((uintptr_t)psStream->pvBase + psStream->ui32RPtr),
                   ui32FirstCopySize);
   
           /*
               Now second half 
           */
           HostMemCopy((void *)((uintptr_t)pvNewBuf + ui32FirstCopySize),
                   (void *)(IMG_PBYTE)psStream->pvBase,
                   psStream->ui32WPtr);
       }
       ui32NewROffset = 0; 
   }
   else
   {
       /* Copy everything in the old buffer to the new one */
       HostMemCopy(pvNewBuf, psStream->pvBase,    psStream->ui32WPtr);
       ui32NewROffset = psStream->ui32RPtr;
   }
 
   /*
       New Write offset is at end of data 
   */                                                        
   ui32NewWOffset = psStream->ui32Size - ui32SpaceInOldBuf;
 
   /*
       Free old buffer 
   */
   if ((psStream->ui32Flags & DEBUG_FLAGS_USE_NONPAGED_MEM) != 0)
   {
       HostNonPageablePageFree(psStream->pvBase);
   }
   else
   {
       HostPageablePageFree(psStream->pvBase);
   }
 
   /*
       Now set new params up 
   */
   psStream->pvBase = pvNewBuf;
   psStream->ui32RPtr = ui32NewROffset;
   psStream->ui32WPtr = ui32NewWOffset;
   psStream->ui32Size = ui32NewSizeInPages * 4096;
 
   return IMG_TRUE;
}
 
/*!****************************************************************************
 @name        SpaceInStream
 @brief        remaining space in stream
 @param        psStream - stream
 @return    bytes remaining
*****************************************************************************/
static IMG_UINT32 SpaceInStream(PDBG_STREAM psStream)
{
   IMG_UINT32    ui32Space;
 
   if ((psStream->ui32Flags & DEBUG_FLAGS_CIRCULAR) != 0)
   {
       /* Allow overwriting the buffer which was already read */
       if (psStream->ui32RPtr > psStream->ui32WPtr)
       {
           ui32Space = psStream->ui32RPtr - psStream->ui32WPtr;
       }
       else
       {
           ui32Space = psStream->ui32RPtr + (psStream->ui32Size - psStream->ui32WPtr);
       }
   }
   else
   {
       /* Don't overwrite anything */
       ui32Space = psStream->ui32Size - psStream->ui32WPtr;
   }
 
   return ui32Space;
}
 
 
/*!****************************************************************************
 @name        DestroyAllStreams
 @brief        delete all streams in list
 @return    none
*****************************************************************************/
void DestroyAllStreams(void)
{
   PDBG_STREAM psStream = g_psStreamList;
   PDBG_STREAM psStreamToFree;
 
   while (psStream != NULL)
   {
       psStreamToFree = psStream;
       psStream = psStream->psNext;
       DBGDrivDestroyStream(psStreamToFree->psInitStream, psStreamToFree, psStreamToFree->psDeinitStream);
   }
   g_psStreamList = NULL;
   return;
}
 
/******************************************************************************
 End of file (DBGDRIV.C)
******************************************************************************/