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
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
/*************************************************************************/ /*!
@File
@Title          Services synchronisation interface
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@Description    Implements client side code for services synchronisation
                interface
@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.
*/ /**************************************************************************/
 
#include "img_types.h"
#include "client_sync_bridge.h"
#if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING)
#include "client_synctracking_bridge.h"
#endif
#include "pvr_bridge.h"
#include "allocmem.h"
#include "osfunc.h"
#include "devicemem.h"
#include "devicemem_pdump.h"
#include "pvr_debug.h"
#include "dllist.h"
#include "sync.h"
#include "sync_internal.h"
#include "lock.h"
#include "log2.h"
/* FIXME */
#if defined(__KERNEL__)
#include "pvrsrv.h"
#endif
 
 
#define SYNC_BLOCK_LIST_CHUNCK_SIZE    10
 
/*
   This defines the maximum amount of synchronisation memory
   that can be allocated per SyncPrim context.
   In reality this number is meaningless as we would run out
   of synchronisation memory before we reach this limit, but
   we need to provide a size to the span RA.
*/
#define MAX_SYNC_MEM                (4 * 1024 * 1024)
 
typedef struct _SYNC_BLOCK_LIST_
{
   IMG_UINT32            ui32BlockCount;            /*!< Number of contexts in the list */
   IMG_UINT32            ui32BlockListSize;        /*!< Size of the array contexts */
   SYNC_PRIM_BLOCK        **papsSyncPrimBlock;    /*!< Array of syncprim blocks */
} SYNC_BLOCK_LIST;
 
typedef struct _SYNC_OP_COOKIE_
{
   IMG_UINT32                ui32SyncCount;
   IMG_UINT32                ui32ClientSyncCount;
   IMG_UINT32                ui32ServerSyncCount;
   IMG_BOOL                bHaveServerSync;
   IMG_HANDLE                hBridge;
   IMG_HANDLE                hServerCookie;
 
   SYNC_BLOCK_LIST            *psSyncBlockList;
   PVRSRV_CLIENT_SYNC_PRIM    **papsSyncPrim;
   /*
       Client sync(s) info.
       If this changes update the calculation of ui32ClientAllocSize
   */
   IMG_UINT32                *paui32SyncBlockIndex;
   IMG_UINT32                *paui32Index;
   IMG_UINT32                *paui32Flags;
   IMG_UINT32                *paui32FenceValue;
   IMG_UINT32                *paui32UpdateValue;
 
   /*
       Server sync(s) info
       If this changes update the calculation of ui32ServerAllocSize
   */
   IMG_HANDLE                *pahServerSync;
   IMG_UINT32              *paui32ServerFlags;
} SYNC_OP_COOKIE;
 
/* forward declaration */
static PVRSRV_ERROR
_SyncPrimSetValue(SYNC_PRIM *psSyncInt, IMG_UINT32 ui32Value);
 
/*
   Internal interfaces for management of SYNC_PRIM_CONTEXT
*/
static void
_SyncPrimContextUnref(SYNC_PRIM_CONTEXT *psContext)
{
   if (!OSAtomicRead(&psContext->hRefCount))
   {
       PVR_DPF((PVR_DBG_ERROR, "_SyncPrimContextUnref context already freed"));
   }
   else if (0 == OSAtomicDecrement(&psContext->hRefCount))
   {
       /* SyncPrimContextDestroy only when no longer referenced */
       RA_Delete(psContext->psSpanRA);
       RA_Delete(psContext->psSubAllocRA);
       OSFreeMem(psContext);
   }
}
 
static void
_SyncPrimContextRef(SYNC_PRIM_CONTEXT *psContext)
{
   if (!OSAtomicRead(&psContext->hRefCount))
   {
       PVR_DPF((PVR_DBG_ERROR, "_SyncPrimContextRef context use after free"));
   }
   else
   {
       OSAtomicIncrement(&psContext->hRefCount);
   }
}
 
/*
   Internal interfaces for management of synchronisation block memory
*/
static PVRSRV_ERROR
AllocSyncPrimitiveBlock(SYNC_PRIM_CONTEXT *psContext,
                       SYNC_PRIM_BLOCK **ppsSyncBlock)
{
   SYNC_PRIM_BLOCK *psSyncBlk;
   IMG_HANDLE hSyncPMR;
   IMG_HANDLE hSyncImportHandle;
   IMG_DEVMEM_SIZE_T uiImportSize;
   PVRSRV_ERROR eError;
 
   psSyncBlk = OSAllocMem(sizeof(SYNC_PRIM_BLOCK));
   if (psSyncBlk == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_alloc;
   }
   psSyncBlk->psContext = psContext;
 
   /* Allocate sync prim block */
   eError = BridgeAllocSyncPrimitiveBlock(psContext->hDevConnection,
                                          &psSyncBlk->hServerSyncPrimBlock,
                                          &psSyncBlk->ui32FirmwareAddr,
                                          &psSyncBlk->ui32SyncBlockSize,
                                          &hSyncPMR);
   if (eError != PVRSRV_OK)
   {
       goto fail_blockalloc;
   }
 
   /* Make it mappable by the client */
   eError = DevmemMakeLocalImportHandle(psContext->hDevConnection,
                                       hSyncPMR,
                                       &hSyncImportHandle);
   if (eError != PVRSRV_OK)
   {
       goto fail_export;
   }
 
   /* Get CPU mapping of the memory block */
   eError = DevmemLocalImport(psContext->hDevConnection,
                              hSyncImportHandle,
                              PVRSRV_MEMALLOCFLAG_CPU_READABLE,
                              &psSyncBlk->hMemDesc,
                              &uiImportSize,
                              "SyncPrimitiveBlock");
 
   /*
       Regardless of success or failure we "undo" the export
   */
   DevmemUnmakeLocalImportHandle(psContext->hDevConnection,
                                hSyncImportHandle);
 
   if (eError != PVRSRV_OK)
   {
       goto fail_import;
   }
 
   eError = DevmemAcquireCpuVirtAddr(psSyncBlk->hMemDesc,
                                     (void **) &psSyncBlk->pui32LinAddr);
   if (eError != PVRSRV_OK)
   {
       goto fail_cpuvaddr;
   }
 
   *ppsSyncBlock = psSyncBlk;
   return PVRSRV_OK;
 
fail_cpuvaddr:
   DevmemFree(psSyncBlk->hMemDesc);
fail_import:
fail_export:
   BridgeFreeSyncPrimitiveBlock(psContext->hDevConnection,
                                psSyncBlk->hServerSyncPrimBlock);
fail_blockalloc:
   OSFreeMem(psSyncBlk);
fail_alloc:
   return eError;
}
 
static void
FreeSyncPrimitiveBlock(SYNC_PRIM_BLOCK *psSyncBlk)
{
   SYNC_PRIM_CONTEXT *psContext = psSyncBlk->psContext;
 
   DevmemReleaseCpuVirtAddr(psSyncBlk->hMemDesc);
   DevmemFree(psSyncBlk->hMemDesc);
   BridgeFreeSyncPrimitiveBlock(psContext->hDevConnection,
                                psSyncBlk->hServerSyncPrimBlock);
   OSFreeMem(psSyncBlk);
}
 
static PVRSRV_ERROR
SyncPrimBlockImport(RA_PERARENA_HANDLE hArena,
                   RA_LENGTH_T uSize,
                   RA_FLAGS_T uFlags,
                   const IMG_CHAR *pszAnnotation,
                   RA_BASE_T *puiBase,
                   RA_LENGTH_T *puiActualSize,
                   RA_PERISPAN_HANDLE *phImport)
{
   SYNC_PRIM_CONTEXT *psContext = hArena;
   SYNC_PRIM_BLOCK *psSyncBlock = NULL;
   RA_LENGTH_T uiSpanSize;
   PVRSRV_ERROR eError;
   PVR_UNREFERENCED_PARAMETER(uFlags);
 
   /* Check we've not be called with an unexpected size */
   if (!hArena || sizeof(IMG_UINT32) != uSize)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid input params", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto e0;
   }
 
   /*
       Ensure the synprim context doesn't go away while we have sync blocks
       attached to it
   */
   _SyncPrimContextRef(psContext);
 
   /* Allocate the block of memory */
   eError = AllocSyncPrimitiveBlock(psContext, &psSyncBlock);
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR, "Failed to allocate syncprim block (%d)", eError));
       goto fail_syncblockalloc;
   }
 
   /* Allocate a span for it */
   eError = RA_Alloc(psContext->psSpanRA,
                   psSyncBlock->ui32SyncBlockSize,
                   RA_NO_IMPORT_MULTIPLIER,
                   0,
                   psSyncBlock->ui32SyncBlockSize,
                   pszAnnotation,
                   &psSyncBlock->uiSpanBase,
                   &uiSpanSize,
                   NULL);
   if (eError != PVRSRV_OK)
   {
       goto fail_spanalloc;
   }
 
   /*
       There is no reason the span RA should return an allocation larger
       then we request
   */
   PVR_ASSERT(uiSpanSize == psSyncBlock->ui32SyncBlockSize);
 
   *puiBase = psSyncBlock->uiSpanBase;
   *puiActualSize = psSyncBlock->ui32SyncBlockSize;
   *phImport = psSyncBlock;
   return PVRSRV_OK;
 
fail_spanalloc:
   FreeSyncPrimitiveBlock(psSyncBlock);
fail_syncblockalloc:
   _SyncPrimContextUnref(psContext);
e0:
   return eError;
}
 
static void
SyncPrimBlockUnimport(RA_PERARENA_HANDLE hArena,
                     RA_BASE_T uiBase,
                     RA_PERISPAN_HANDLE hImport)
{
   SYNC_PRIM_CONTEXT *psContext = hArena;
   SYNC_PRIM_BLOCK *psSyncBlock = hImport;
 
   if (!psContext || !psSyncBlock || uiBase != psSyncBlock->uiSpanBase)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid input params", __FUNCTION__));
       return;
   }
 
   /* Free the span this import is using */
   RA_Free(psContext->psSpanRA, uiBase);
 
   /* Free the syncpim block */
   FreeSyncPrimitiveBlock(psSyncBlock);
 
   /*    Drop our reference to the syncprim context */
   _SyncPrimContextUnref(psContext);
}
 
static INLINE IMG_UINT32 SyncPrimGetOffset(SYNC_PRIM *psSyncInt)
{
   IMG_UINT64 ui64Temp;
   
   PVR_ASSERT(psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL);
 
   /* FIXME: Subtracting a 64-bit address from another and then implicit
    * cast to 32-bit number. Need to review all call sequences that use this
    * function, added explicit casting for now.
    */
   ui64Temp =  psSyncInt->u.sLocal.uiSpanAddr - psSyncInt->u.sLocal.psSyncBlock->uiSpanBase;
   PVR_ASSERT(ui64Temp<IMG_UINT32_MAX);
   return (IMG_UINT32)ui64Temp;
}
 
static void SyncPrimGetCPULinAddr(SYNC_PRIM *psSyncInt)
{
   SYNC_PRIM_BLOCK *psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
 
   psSyncInt->sCommon.pui32LinAddr = psSyncBlock->pui32LinAddr +
                                     (SyncPrimGetOffset(psSyncInt)/sizeof(IMG_UINT32));
}
 
static void SyncPrimLocalFree(SYNC_PRIM *psSyncInt)
{
   SYNC_PRIM_BLOCK *psSyncBlock;
   SYNC_PRIM_CONTEXT *psContext;
 
   psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
   psContext = psSyncBlock->psContext;
 
   {
       PVRSRV_ERROR eError;
       IMG_HANDLE hConn =
               psSyncInt->u.sLocal.psSyncBlock->psContext->hDevConnection;
 
#if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING)
       if(PVRSRVIsBridgeEnabled(hConn, PVRSRV_BRIDGE_SYNCTRACKING))
       {
           /* remove this sync record */
           eError = BridgeSyncRecordRemoveByHandle(hConn,
                                                   psSyncInt->u.sLocal.hRecord);
           if (PVRSRV_OK != eError)
           {
               PVR_DPF((PVR_DBG_ERROR, "%s: failed to remove SyncRecord", __FUNCTION__));
           }
       }
       else
#endif /* if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING) */
       {
           IMG_UINT32 ui32FWAddr = psSyncBlock->ui32FirmwareAddr +
                   SyncPrimGetOffset(psSyncInt);
 
           eError = BridgeSyncFreeEvent(hConn, ui32FWAddr);
           if (eError != PVRSRV_OK)
           {
               PVR_DPF((PVR_DBG_WARNING, "BridgeSyncAllocEvent failed with error:"
                       " %d", eError));
           }
       }
   }
   /* reset the sync prim value as it is freed.
    * this guarantees the client sync allocated to the client will
    * have a value of zero and the client does not need to
    * explicitly initialise the sync value to zero.
    * the allocation of the backing memory for the sync prim block
    * is done with ZERO_ON_ALLOC so the memory is initially all zero.
    */
   (void) _SyncPrimSetValue(psSyncInt, LOCAL_SYNC_PRIM_RESET_VALUE);
 
   RA_Free(psContext->psSubAllocRA, psSyncInt->u.sLocal.uiSpanAddr);
   OSFreeMem(psSyncInt);
   _SyncPrimContextUnref(psContext);
}
 
static void SyncPrimServerFree(SYNC_PRIM *psSyncInt)
{
   PVRSRV_ERROR eError;
 
   eError = BridgeServerSyncFree(psSyncInt->u.sServer.hBridge,
                                 psSyncInt->u.sServer.hServerSync);
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimServerFree failed"));
   }
   OSFreeMem(psSyncInt);
}
 
static void SyncPrimLocalUnref(SYNC_PRIM *psSyncInt)
{
   if (!OSAtomicRead(&psSyncInt->u.sLocal.hRefCount))
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimLocalUnref sync already freed"));
   }
   else if (0 == OSAtomicDecrement(&psSyncInt->u.sLocal.hRefCount))
   {
       SyncPrimLocalFree(psSyncInt);
   }
}
 
static void SyncPrimLocalRef(SYNC_PRIM *psSyncInt)
{
   if (!OSAtomicRead(&psSyncInt->u.sLocal.hRefCount))
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimLocalRef sync use after free"));
   }
   else
   {
       OSAtomicIncrement(&psSyncInt->u.sLocal.hRefCount);
   }
}
 
static IMG_UINT32 SyncPrimGetFirmwareAddrLocal(SYNC_PRIM *psSyncInt)
{
   SYNC_PRIM_BLOCK *psSyncBlock;
 
   psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
   return psSyncBlock->ui32FirmwareAddr + SyncPrimGetOffset(psSyncInt);    
}
 
static IMG_UINT32 SyncPrimGetFirmwareAddrServer(SYNC_PRIM *psSyncInt)
{
   return psSyncInt->u.sServer.ui32FirmwareAddr;
}
 
#if !defined(__KERNEL__)
static SYNC_BRIDGE_HANDLE _SyncPrimGetBridgeHandleLocal(SYNC_PRIM *psSyncInt)
{
   return psSyncInt->u.sLocal.psSyncBlock->psContext->hDevConnection;
}
 
static SYNC_BRIDGE_HANDLE _SyncPrimGetBridgeHandleServer(SYNC_PRIM *psSyncInt)
{
   return psSyncInt->u.sServer.hBridge;
}
 
static SYNC_BRIDGE_HANDLE _SyncPrimGetBridgeHandle(PVRSRV_CLIENT_SYNC_PRIM *psSync)
{
   SYNC_PRIM *psSyncInt;
 
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
   if (psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL)
   {
       return _SyncPrimGetBridgeHandleLocal(psSyncInt);
   }
   else if (psSyncInt->eType == SYNC_PRIM_TYPE_SERVER)
   {
       return _SyncPrimGetBridgeHandleServer(psSyncInt);
   }
   else
   {
       PVR_DPF((PVR_DBG_ERROR, "_SyncPrimGetBridgeHandle: Invalid sync type"));
       /*
           Either the client has given us a bad pointer or there is an
           error in this module
       */
       return 0;
   }
}
#endif
 
/*
   Internal interfaces for management of syncprim block lists
*/
static SYNC_BLOCK_LIST *_SyncPrimBlockListCreate(void)
{
   SYNC_BLOCK_LIST *psBlockList;
 
   psBlockList = OSAllocMem(sizeof(SYNC_BLOCK_LIST));
   if (!psBlockList)
   {
       return NULL;
   }
 
   psBlockList->ui32BlockCount = 0;
   psBlockList->ui32BlockListSize = SYNC_BLOCK_LIST_CHUNCK_SIZE;
 
   psBlockList->papsSyncPrimBlock = OSAllocMem(sizeof(SYNC_PRIM_BLOCK *)
                                                   * SYNC_BLOCK_LIST_CHUNCK_SIZE);
   if (!psBlockList->papsSyncPrimBlock)
   {
       OSFreeMem(psBlockList);
       return NULL;
   }
 
   OSCachedMemSet(psBlockList->papsSyncPrimBlock,
            0,
            sizeof(SYNC_PRIM_BLOCK *) * psBlockList->ui32BlockListSize);
 
   return psBlockList;
}
 
static PVRSRV_ERROR _SyncPrimBlockListAdd(SYNC_BLOCK_LIST *psBlockList,
                                           SYNC_PRIM_BLOCK *psSyncPrimBlock)
{
   IMG_UINT32 i;
 
   /* Check the context isn't already on the list */
   for (i=0;i<psBlockList->ui32BlockCount;i++)
   {
       if (psBlockList->papsSyncPrimBlock[i] == psSyncPrimBlock)
       {
           return PVRSRV_OK;
       }
   }
 
   /* Check we have space for a new item */
   if (psBlockList->ui32BlockCount == psBlockList->ui32BlockListSize)
   {
       SYNC_PRIM_BLOCK    **papsNewSyncPrimBlock;
 
       papsNewSyncPrimBlock = OSAllocMem(sizeof(SYNC_PRIM_BLOCK *) *
                                           (psBlockList->ui32BlockListSize +
                                           SYNC_BLOCK_LIST_CHUNCK_SIZE));
       if (!papsNewSyncPrimBlock)
       {
           return PVRSRV_ERROR_OUT_OF_MEMORY;
       }
 
       OSCachedMemCopy(papsNewSyncPrimBlock,
                 psBlockList->papsSyncPrimBlock,
                 sizeof(SYNC_PRIM_CONTEXT *) *
                 psBlockList->ui32BlockListSize);
 
       OSFreeMem(psBlockList->papsSyncPrimBlock);
 
       psBlockList->papsSyncPrimBlock = papsNewSyncPrimBlock;
       psBlockList->ui32BlockListSize += SYNC_BLOCK_LIST_CHUNCK_SIZE;
   }
 
   /* Add the context to the list */
   psBlockList->papsSyncPrimBlock[psBlockList->ui32BlockCount++] = psSyncPrimBlock;
   return PVRSRV_OK;
}
 
static PVRSRV_ERROR _SyncPrimBlockListBlockToIndex(SYNC_BLOCK_LIST *psBlockList,
                                                  SYNC_PRIM_BLOCK *psSyncPrimBlock,
                                                  IMG_UINT32 *pui32Index)
{
   IMG_UINT32 i;
 
   for (i=0;i<psBlockList->ui32BlockCount;i++)
   {
       if (psBlockList->papsSyncPrimBlock[i] == psSyncPrimBlock)
       {
           *pui32Index = i;
           return PVRSRV_OK;
       }
   }
 
   return PVRSRV_ERROR_INVALID_PARAMS;
}
 
static PVRSRV_ERROR _SyncPrimBlockListHandleArrayCreate(SYNC_BLOCK_LIST *psBlockList,
                                                       IMG_UINT32 *pui32BlockHandleCount,
                                                       IMG_HANDLE **ppahHandleList)
{
   IMG_HANDLE *pahHandleList;
   IMG_UINT32 i;
 
   pahHandleList = OSAllocMem(sizeof(IMG_HANDLE) *
                              psBlockList->ui32BlockCount);
   if (!pahHandleList)
   {
       return PVRSRV_ERROR_OUT_OF_MEMORY;
   }
 
   for (i=0;i<psBlockList->ui32BlockCount;i++)
   {
       pahHandleList[i] = psBlockList->papsSyncPrimBlock[i]->hServerSyncPrimBlock;
   }
 
   *ppahHandleList = pahHandleList;
   *pui32BlockHandleCount = psBlockList->ui32BlockCount;
 
   return PVRSRV_OK;
}
 
static void _SyncPrimBlockListHandleArrayDestroy(IMG_HANDLE *pahHandleList)
{
   OSFreeMem(pahHandleList);
}
 
static IMG_UINT32 _SyncPrimBlockListGetClientValue(SYNC_BLOCK_LIST *psBlockList,
                                                  IMG_UINT32 ui32BlockIndex,
                                                  IMG_UINT32 ui32Index)
{
   return psBlockList->papsSyncPrimBlock[ui32BlockIndex]->pui32LinAddr[ui32Index];
}
 
static void _SyncPrimBlockListDestroy(SYNC_BLOCK_LIST *psBlockList)
{
   OSFreeMem(psBlockList->papsSyncPrimBlock);
   OSFreeMem(psBlockList);
}
 
 
static INLINE IMG_UINT32 _Log2(IMG_UINT32 ui32Align)
{
   PVR_ASSERT(IsPower2(ui32Align));
   return ExactLog2(ui32Align);
}
 
/*
   External interfaces
*/
 
IMG_INTERNAL PVRSRV_ERROR
SyncPrimContextCreate(SHARED_DEV_CONNECTION hDevConnection,
                      PSYNC_PRIM_CONTEXT *phSyncPrimContext)
{
   SYNC_PRIM_CONTEXT *psContext;
   PVRSRV_ERROR eError;
 
   psContext = OSAllocMem(sizeof(SYNC_PRIM_CONTEXT));
   if (psContext == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_alloc;
   }
 
   psContext->hDevConnection = hDevConnection;
 
   OSSNPrintf(psContext->azName, SYNC_PRIM_NAME_SIZE, "Sync Prim RA-%p", psContext);
   OSSNPrintf(psContext->azSpanName, SYNC_PRIM_NAME_SIZE, "Sync Prim span RA-%p", psContext);
 
   /*
       Create the RA for sub-allocations of the SynPrim's
 
       Note:
       The import size doesn't matter here as the server will pass
       back the blocksize when does the import which overrides
       what we specify here.
   */
 
   psContext->psSubAllocRA = RA_Create(psContext->azName,
                                       /* Params for imports */
                                       _Log2(sizeof(IMG_UINT32)),
                                       RA_LOCKCLASS_2,
                                       SyncPrimBlockImport,
                                       SyncPrimBlockUnimport,
                                       psContext,
                                       IMG_FALSE);
   if (psContext->psSubAllocRA == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_suballoc;
   }
 
   /*
       Create the span-management RA
 
       The RA requires that we work with linear spans. For our use
       here we don't require this behaviour as we're always working
       within offsets of blocks (imports). However, we need to keep
       the RA happy so we create the "span" management RA which
       ensures that all are imports are added to the RA in a linear
       fashion
   */
   psContext->psSpanRA = RA_Create(psContext->azSpanName,
                                   /* Params for imports */
                                   0,
                                   RA_LOCKCLASS_1,
                                   NULL,
                                   NULL,
                                   NULL,
                                   IMG_FALSE);
   if (psContext->psSpanRA == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_span;
   }
 
   if (!RA_Add(psContext->psSpanRA, 0, MAX_SYNC_MEM, 0, NULL))
   {
       RA_Delete(psContext->psSpanRA);
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_span;
   }
 
   OSAtomicWrite(&psContext->hRefCount, 1);
 
   *phSyncPrimContext = psContext;
   return PVRSRV_OK;
fail_span:
   RA_Delete(psContext->psSubAllocRA);
fail_suballoc:
   OSFreeMem(psContext);
fail_alloc:
   return eError;
}
 
IMG_INTERNAL void SyncPrimContextDestroy(PSYNC_PRIM_CONTEXT hSyncPrimContext)
{
   SYNC_PRIM_CONTEXT *psContext = hSyncPrimContext;
   if (1 != OSAtomicRead(&psContext->hRefCount))
   {
       PVR_DPF((PVR_DBG_ERROR, "%s attempted with active references, may be the result of a race", __FUNCTION__));
   }
#if defined(PVRSRV_FORCE_UNLOAD_IF_BAD_STATE)
#if defined(__KERNEL__)
   if (PVRSRVGetPVRSRVData()->eServicesState != PVRSRV_SERVICES_STATE_OK)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: Forcing context destruction due to bad driver state.", __FUNCTION__));
       OSAtomicWrite(&psContext->hRefCount, 1);
   }
#endif
#endif
   _SyncPrimContextUnref(psContext);
}
 
static PVRSRV_ERROR _SyncPrimAlloc(PSYNC_PRIM_CONTEXT hSyncPrimContext,
                                   PVRSRV_CLIENT_SYNC_PRIM **ppsSync,
                                   const IMG_CHAR *pszClassName,
                                   IMG_BOOL bServerSync)
{
   SYNC_PRIM_CONTEXT *psContext = hSyncPrimContext;
   SYNC_PRIM_BLOCK *psSyncBlock;
   SYNC_PRIM *psNewSync;
   PVRSRV_ERROR eError;
   RA_BASE_T uiSpanAddr;
 
   if (!hSyncPrimContext)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid context", __func__));
       return PVRSRV_ERROR_INVALID_PARAMS;
   }
 
   psNewSync = OSAllocMem(sizeof(SYNC_PRIM));
   if (psNewSync == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto fail_alloc;
   }
 
   eError = RA_Alloc(psContext->psSubAllocRA,
                     sizeof(IMG_UINT32),
                     RA_NO_IMPORT_MULTIPLIER,
                     0,
                     sizeof(IMG_UINT32),
                     "Sync_Prim",
                     &uiSpanAddr,
                     NULL,
                     (RA_PERISPAN_HANDLE *) &psSyncBlock);
   if (PVRSRV_OK != eError)
   {
       goto fail_raalloc;
   }
   psNewSync->eType = SYNC_PRIM_TYPE_LOCAL;
   OSAtomicWrite(&psNewSync->u.sLocal.hRefCount, 1);
   psNewSync->u.sLocal.uiSpanAddr = uiSpanAddr;
   psNewSync->u.sLocal.psSyncBlock = psSyncBlock;
   SyncPrimGetCPULinAddr(psNewSync);
   *ppsSync = &psNewSync->sCommon;
   _SyncPrimContextRef(psContext);
 
#if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING)
   if(PVRSRVIsBridgeEnabled(psSyncBlock->psContext->hDevConnection, PVRSRV_BRIDGE_SYNCTRACKING))
   {
       IMG_CHAR szClassName[SYNC_MAX_CLASS_NAME_LEN];
       if(pszClassName)
       {
           /* Copy the class name annotation into a fixed-size array */
           OSStringNCopy(szClassName, pszClassName, SYNC_MAX_CLASS_NAME_LEN - 1);
           szClassName[SYNC_MAX_CLASS_NAME_LEN - 1] = 0;
       }
       else
       {
           /* No class name annotation */
           szClassName[0] = 0;
       }
       /* record this sync */
       eError = BridgeSyncRecordAdd(
                   psSyncBlock->psContext->hDevConnection,
                   &psNewSync->u.sLocal.hRecord,
                   psSyncBlock->hServerSyncPrimBlock,
                   psSyncBlock->ui32FirmwareAddr,
                   SyncPrimGetOffset(psNewSync),
                   bServerSync,
                   OSStringNLength(szClassName, SYNC_MAX_CLASS_NAME_LEN),
                   szClassName);
       if (PVRSRV_OK != eError)
       {
           PVR_DPF((PVR_DBG_ERROR, "%s: failed to add SyncRecord", __FUNCTION__));
       }
   }
   else
#endif /* if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING) */
   {
       eError = BridgeSyncAllocEvent(hSyncPrimContext->hDevConnection,
                                     bServerSync,
                                     psSyncBlock->ui32FirmwareAddr + SyncPrimGetOffset(psNewSync),
                                     OSStringNLength(pszClassName, SYNC_MAX_CLASS_NAME_LEN),
                                     pszClassName);
       if (eError != PVRSRV_OK)
       {
           PVR_DPF((PVR_DBG_WARNING, "BridgeSyncAllocEvent failed with error: %d",
                   eError));
       }
   }
 
   return PVRSRV_OK;
 
fail_raalloc:
   OSFreeMem(psNewSync);
fail_alloc:
   return eError;
}
 
#if defined(__KERNEL__)
IMG_INTERNAL PVRSRV_ERROR SyncPrimAllocForServerSync(PSYNC_PRIM_CONTEXT hSyncPrimContext,
                                       PVRSRV_CLIENT_SYNC_PRIM **ppsSync,
                                       const IMG_CHAR *pszClassName)
{
   return _SyncPrimAlloc(hSyncPrimContext,
                     ppsSync,
                     pszClassName,
                     IMG_TRUE);
}
#endif
 
IMG_INTERNAL PVRSRV_ERROR SyncPrimAlloc(PSYNC_PRIM_CONTEXT hSyncPrimContext,
                                       PVRSRV_CLIENT_SYNC_PRIM **ppsSync,
                                       const IMG_CHAR *pszClassName)
{
   return _SyncPrimAlloc(hSyncPrimContext,
                         ppsSync,
                         pszClassName,
                         IMG_FALSE);
}
 
static PVRSRV_ERROR
_SyncPrimSetValue(SYNC_PRIM *psSyncInt, IMG_UINT32 ui32Value)
{
   PVRSRV_ERROR eError;
 
   if (psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL)
   {
       SYNC_PRIM_BLOCK *psSyncBlock;
       SYNC_PRIM_CONTEXT *psContext;
 
       psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
       psContext = psSyncBlock->psContext;
 
       eError = BridgeSyncPrimSet(psContext->hDevConnection,
                                   psSyncBlock->hServerSyncPrimBlock,
                                   SyncPrimGetOffset(psSyncInt)/sizeof(IMG_UINT32),
                                   ui32Value);
   }
   else
   {
       eError = BridgeServerSyncPrimSet(psSyncInt->u.sServer.hBridge,
                                   psSyncInt->u.sServer.hServerSync,
                                   ui32Value);
   }
   /* These functions don't actually fail */
   return eError;
}
 
IMG_INTERNAL PVRSRV_ERROR SyncPrimFree(PVRSRV_CLIENT_SYNC_PRIM *psSync)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   SYNC_PRIM *psSyncInt;
 
   if (!psSync)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: null sync pointer", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto err_out;
   }
 
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
   if (psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL)
   {
       SyncPrimLocalUnref(psSyncInt);
   }
   else if (psSyncInt->eType == SYNC_PRIM_TYPE_SERVER)
   {
       SyncPrimServerFree(psSyncInt);
   }
   else
   {
       /*
           Either the client has given us a bad pointer or there is an
           error in this module
       */
       PVR_DPF((PVR_DBG_ERROR, "%s: Invalid sync type", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
       goto err_out;
   }
 
err_out:
   return eError;
}
 
#if defined(NO_HARDWARE)
IMG_INTERNAL PVRSRV_ERROR
SyncPrimNoHwUpdate(PVRSRV_CLIENT_SYNC_PRIM *psSync, IMG_UINT32 ui32Value)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   SYNC_PRIM *psSyncInt;
 
   if (!psSync)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: null sync pointer", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto err_out;
   }
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
 
   /* There is no check for the psSyncInt to be LOCAL as this call
      substitutes the Firmware updating a sync and that sync could
      be a server one */
 
   eError =  _SyncPrimSetValue(psSyncInt, ui32Value);
 
err_out:
   return eError;
}
#endif
 
IMG_INTERNAL PVRSRV_ERROR
SyncPrimSet(PVRSRV_CLIENT_SYNC_PRIM *psSync, IMG_UINT32 ui32Value)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   SYNC_PRIM *psSyncInt;
 
   if (!psSync)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: null sync pointer", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto err_out;
   }
 
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
   if (psSyncInt->eType != SYNC_PRIM_TYPE_LOCAL)
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimSet: Invalid sync type"));
       eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
       goto err_out;
   }
 
   eError = _SyncPrimSetValue(psSyncInt, ui32Value);
 
#if defined(PDUMP)
   SyncPrimPDump(psSync);
#endif
err_out:
   return eError;
}
 
IMG_INTERNAL PVRSRV_ERROR SyncPrimLocalGetHandleAndOffset(PVRSRV_CLIENT_SYNC_PRIM *psSync,
                           IMG_HANDLE *phBlock,
                           IMG_UINT32 *pui32Offset)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   SYNC_PRIM *psSyncInt;
 
   if(!psSync || !phBlock || !pui32Offset)
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimGetHandleAndOffset: invalid input pointer"));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto err_out;
   }
 
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
 
   if (psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL)
   {
       *phBlock = psSyncInt->u.sLocal.psSyncBlock->hServerSyncPrimBlock;
       *pui32Offset = psSyncInt->u.sLocal.uiSpanAddr - psSyncInt->u.sLocal.psSyncBlock->uiSpanBase;
   }
   else
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: psSync not a Local sync prim (%d)",
           __FUNCTION__, psSyncInt->eType));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto err_out;
   }
 
err_out:
   return eError;
}
 
IMG_INTERNAL PVRSRV_ERROR
SyncPrimGetFirmwareAddr(PVRSRV_CLIENT_SYNC_PRIM *psSync, IMG_UINT32 *pui32FwAddr)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   SYNC_PRIM *psSyncInt;
 
   *pui32FwAddr = 0;
   if (!psSync)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid input pointer", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto err_out;
   }
 
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
   if (psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL)
   {
       *pui32FwAddr = SyncPrimGetFirmwareAddrLocal(psSyncInt);
   }
   else if (psSyncInt->eType == SYNC_PRIM_TYPE_SERVER)
   {
       *pui32FwAddr = SyncPrimGetFirmwareAddrServer(psSyncInt);
   }
   else
   {
       /* Either the client has given us a bad pointer or there is an
        * error in this module
        */
       PVR_DPF((PVR_DBG_ERROR, "%s: Invalid sync type", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
       goto err_out;
   }
 
err_out:
   return eError;
}
 
#if !defined(__KERNEL__)
IMG_INTERNAL PVRSRV_ERROR SyncPrimDumpSyncs(IMG_UINT32 ui32SyncCount, PVRSRV_CLIENT_SYNC_PRIM **papsSync, const IMG_CHAR *pcszExtraInfo)
{
#if defined(PVRSRV_NEED_PVR_DPF)
   SYNC_PRIM *psSyncInt;
   PVRSRV_CLIENT_SYNC_PRIM **papsServerSync;
   IMG_UINT32 ui32ServerSyncs = 0;
   IMG_UINT32 *pui32UID = NULL;
   IMG_UINT32 *pui32FWAddr = NULL;
   IMG_UINT32 *pui32CurrentOp = NULL;
   IMG_UINT32 *pui32NextOp = NULL;
   IMG_UINT32 i;
   PVRSRV_ERROR eError = PVRSRV_OK;
 
   papsServerSync = OSAllocMem(ui32SyncCount * sizeof(PVRSRV_CLIENT_SYNC_PRIM *));
   if (!papsServerSync)
   {
       return PVRSRV_ERROR_OUT_OF_MEMORY;
   }
 
   for (i = 0; i < ui32SyncCount; i++)
   {
       psSyncInt = IMG_CONTAINER_OF(papsSync[i], SYNC_PRIM, sCommon);
       if (psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL)
       {
           PVR_DPF((PVR_DBG_ERROR, "%s: sync=local  fw=0x%x curr=0x%04x",
                    pcszExtraInfo,
                    SyncPrimGetFirmwareAddrLocal(psSyncInt),
                    *psSyncInt->sCommon.pui32LinAddr));
       }
       else if (psSyncInt->eType == SYNC_PRIM_TYPE_SERVER)
       {
           papsServerSync[ui32ServerSyncs++] = papsSync[i];
       }
       else
       {
           PVR_DPF((PVR_DBG_ERROR, "SyncPrimDumpSyncs: Invalid sync type"));
           /*
              Either the client has given us a bad pointer or there is an
              error in this module
              */
           eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
           goto err_free;
       }
   }
 
   if (ui32ServerSyncs > 0)
   {
       pui32UID = OSAllocMem(ui32ServerSyncs * sizeof(IMG_UINT32));
       if (!pui32UID)
       {
           eError = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free;
       }
       pui32FWAddr = OSAllocMem(ui32ServerSyncs * sizeof(IMG_UINT32));
       if (!pui32FWAddr)
       {
           eError = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free;
       }
       pui32CurrentOp = OSAllocMem(ui32ServerSyncs * sizeof(IMG_UINT32));
       if (!pui32CurrentOp)
       {
           eError = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free;
       }
       pui32NextOp = OSAllocMem(ui32ServerSyncs * sizeof(IMG_UINT32));
       if (!pui32NextOp)
       {
           eError = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free;
       }
       eError = SyncPrimServerGetStatus(ui32ServerSyncs, papsServerSync,
                                        pui32UID,
                                        pui32FWAddr,
                                        pui32CurrentOp,
                                        pui32NextOp);
       if (eError != PVRSRV_OK)
       {
           PVR_DPF((PVR_DBG_ERROR, "SyncPrimDumpSyncs: Error querying server sync status (%d)",
                    eError));
           goto err_free;
       }
       for (i = 0; i < ui32ServerSyncs; i++)
       {
           PVR_DPF((PVR_DBG_ERROR, "%s: sync=server fw=0x%x curr=0x%04x next=0x%04x id=%u%s",
                    pcszExtraInfo,
                    pui32FWAddr[i],
                    pui32CurrentOp[i],
                    pui32NextOp[i],
                    pui32UID[i],
                    (pui32NextOp[i] - pui32CurrentOp[i] == 1) ? " *" : 
                    (pui32NextOp[i] - pui32CurrentOp[i] >  1) ? " **" : 
                    ""));
       }
   }
 
err_free:
   OSFreeMem(papsServerSync);
   if (pui32UID)
   {
       OSFreeMem(pui32UID);
   }
   if (pui32FWAddr)
   {
       OSFreeMem(pui32FWAddr);
   }
   if (pui32CurrentOp)
   {
       OSFreeMem(pui32CurrentOp);
   }
   if (pui32NextOp)
   {
       OSFreeMem(pui32NextOp);
   }
   return eError;
#else
   PVR_UNREFERENCED_PARAMETER(ui32SyncCount);
   PVR_UNREFERENCED_PARAMETER(papsSync);
   PVR_UNREFERENCED_PARAMETER(pcszExtraInfo);
   return PVRSRV_OK;
#endif
}
#endif
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimOpCreate(IMG_UINT32 ui32SyncCount,
             PVRSRV_CLIENT_SYNC_PRIM **papsSyncPrim,
             PSYNC_OP_COOKIE *ppsCookie)
{
   SYNC_OP_COOKIE *psNewCookie;
   SYNC_BLOCK_LIST *psSyncBlockList;
   IMG_UINT32 ui32ServerSyncCount = 0;
   IMG_UINT32 ui32ClientSyncCount = 0;
   IMG_UINT32 ui32ServerAllocSize;
   IMG_UINT32 ui32ClientAllocSize;
   IMG_UINT32 ui32TotalAllocSize;
   IMG_UINT32 ui32ServerIndex = 0;
   IMG_UINT32 ui32ClientIndex = 0;
   IMG_UINT32 i;
   IMG_UINT32 ui32SyncBlockCount;
   IMG_HANDLE hBridge;
   IMG_HANDLE *pahHandleList;
   IMG_CHAR *pcPtr;
   PVRSRV_ERROR eError;
   IMG_BOOL bServerSync;
 
   psSyncBlockList = _SyncPrimBlockListCreate();
 
   if (!psSyncBlockList)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto e0;
   }
 
   for (i=0;i<ui32SyncCount;i++)
   {
       eError = SyncPrimIsServerSync(papsSyncPrim[i], &bServerSync);
       if (PVRSRV_OK != eError) goto e1;
       if (bServerSync)
       {
           ui32ServerSyncCount++;
       }
       else
       {
           SYNC_PRIM *psSync = (SYNC_PRIM *) papsSyncPrim[i];
 
           ui32ClientSyncCount++;
           eError = _SyncPrimBlockListAdd(psSyncBlockList, psSync->u.sLocal.psSyncBlock);
           if (eError != PVRSRV_OK)
           {
               goto e1;
           }
       }
   }
 
   ui32ServerAllocSize = ui32ServerSyncCount * (sizeof(IMG_HANDLE) + sizeof(IMG_UINT32));
   ui32ClientAllocSize = ui32ClientSyncCount * (5 * sizeof(IMG_UINT32));
   ui32TotalAllocSize = sizeof(SYNC_OP_COOKIE) +
                            (sizeof(PVRSRV_CLIENT_SYNC_PRIM *) * ui32SyncCount) +
                            ui32ServerAllocSize + 
                            ui32ClientAllocSize;
 
   psNewCookie = OSAllocMem(ui32TotalAllocSize);
   pcPtr = (IMG_CHAR *) psNewCookie;
 
   if (!psNewCookie)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto e1;
   }
 
   /* Setup the pointers */
   pcPtr += sizeof(SYNC_OP_COOKIE);
   psNewCookie->papsSyncPrim = (PVRSRV_CLIENT_SYNC_PRIM **) pcPtr;
 
   pcPtr += sizeof(PVRSRV_CLIENT_SYNC_PRIM *) * ui32SyncCount;
   psNewCookie->paui32SyncBlockIndex = (IMG_UINT32 *) pcPtr;
 
   pcPtr += sizeof(IMG_UINT32) * ui32ClientSyncCount;
   psNewCookie->paui32Index = (IMG_UINT32 *) pcPtr;
 
   pcPtr += sizeof(IMG_UINT32) * ui32ClientSyncCount;
   psNewCookie->paui32Flags = (IMG_UINT32 *) pcPtr;
 
   pcPtr += sizeof(IMG_UINT32) * ui32ClientSyncCount;
   psNewCookie->paui32FenceValue = (IMG_UINT32 *) pcPtr;
 
   pcPtr += sizeof(IMG_UINT32) * ui32ClientSyncCount;
   psNewCookie->paui32UpdateValue = (IMG_UINT32 *) pcPtr;
 
   pcPtr += sizeof(IMG_UINT32) * ui32ClientSyncCount;
   psNewCookie->pahServerSync =(IMG_HANDLE *) pcPtr;
   pcPtr += sizeof(IMG_HANDLE) * ui32ServerSyncCount;
 
   psNewCookie->paui32ServerFlags =(IMG_UINT32 *) pcPtr;
   pcPtr += sizeof(IMG_UINT32) * ui32ServerSyncCount;
 
   /* Check the pointer setup went ok */
   if (!(pcPtr == (((IMG_CHAR *) psNewCookie) + ui32TotalAllocSize)))
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: cookie setup failed", __FUNCTION__));
       eError = PVRSRV_ERROR_INTERNAL_ERROR;
       goto e2;
   }
 
   psNewCookie->ui32SyncCount = ui32SyncCount;
   psNewCookie->ui32ServerSyncCount = ui32ServerSyncCount;
   psNewCookie->ui32ClientSyncCount = ui32ClientSyncCount;
   psNewCookie->psSyncBlockList = psSyncBlockList;
 
   /*
       Get the bridge handle from the 1st sync.
 
       Note: We assume the all syncs have been created with the same
             services connection.
   */
   eError = SyncPrimIsServerSync(papsSyncPrim[0], &bServerSync);
   if (PVRSRV_OK != eError) goto e2;
   if (bServerSync)
   {
       SYNC_PRIM *psSync = (SYNC_PRIM *) papsSyncPrim[0];
 
       hBridge = psSync->u.sServer.hBridge;
   }
   else
   {
       SYNC_PRIM *psSync = (SYNC_PRIM *) papsSyncPrim[0];
 
       hBridge = psSync->u.sLocal.psSyncBlock->psContext->hDevConnection;
   }
 
   psNewCookie->hBridge = hBridge;
 
   if (ui32ServerSyncCount)
   {
       psNewCookie->bHaveServerSync = IMG_TRUE;
   }
   else
   {
       psNewCookie->bHaveServerSync = IMG_FALSE;
   }
 
   /* Fill in the server and client sync data */
   for (i=0;i<ui32SyncCount;i++)
   {
       SYNC_PRIM *psSync = (SYNC_PRIM *) papsSyncPrim[i];
 
       eError = SyncPrimIsServerSync(papsSyncPrim[i], &bServerSync);
       if (PVRSRV_OK != eError) goto e2;
       if (bServerSync)
       {
           psNewCookie->pahServerSync[ui32ServerIndex] = psSync->u.sServer.hServerSync;
 
           ui32ServerIndex++;
       }
       else
       {
           /* Location of sync */
           eError = _SyncPrimBlockListBlockToIndex(psSyncBlockList,
                                                   psSync->u.sLocal.psSyncBlock,
                                                   &psNewCookie->paui32SyncBlockIndex[ui32ClientIndex]);
           if (eError != PVRSRV_OK)
           {
               goto e2;
           }
 
           /* Workout the index to sync */
           psNewCookie->paui32Index[ui32ClientIndex] =
                   SyncPrimGetOffset(psSync)/sizeof(IMG_UINT32);
 
           ui32ClientIndex++;
       }
 
       psNewCookie->papsSyncPrim[i] = papsSyncPrim[i];
   }
 
   eError = _SyncPrimBlockListHandleArrayCreate(psSyncBlockList,
                                                &ui32SyncBlockCount,
                                                &pahHandleList);
   if (eError !=PVRSRV_OK)
   {
       goto e2;
   }
 
   /*
       Create the server side cookie. Here we pass in all the unchanging
       data so we only need to pass in the minimum at takeop time
   */
   eError = BridgeSyncPrimOpCreate(hBridge,
                                   ui32SyncBlockCount,
                                   pahHandleList,
                                   psNewCookie->ui32ClientSyncCount,
                                   psNewCookie->paui32SyncBlockIndex,
                                   psNewCookie->paui32Index,
                                   psNewCookie->ui32ServerSyncCount,
                                   psNewCookie->pahServerSync,
                                   &psNewCookie->hServerCookie);
 
   /* Free the handle list regardless of error */
   _SyncPrimBlockListHandleArrayDestroy(pahHandleList);
 
   if (eError != PVRSRV_OK)
   {
       goto e2;
   }
 
   /* Increase the reference count on all referenced local sync prims
    * so that they cannot be freed until this Op is finished with
    */
   for (i=0;i<ui32SyncCount;i++)
   {
       SYNC_PRIM *psSyncInt;
       psSyncInt = IMG_CONTAINER_OF(papsSyncPrim[i], SYNC_PRIM, sCommon);
       if (SYNC_PRIM_TYPE_LOCAL == psSyncInt->eType)
       {
           SyncPrimLocalRef(psSyncInt);
       }
   }
 
   *ppsCookie = psNewCookie;
   return PVRSRV_OK;
 
e2:
   OSFreeMem(psNewCookie);
e1:
   _SyncPrimBlockListDestroy(psSyncBlockList);
e0:
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimOpTake(PSYNC_OP_COOKIE psCookie,
                           IMG_UINT32 ui32SyncCount,
                           PVRSRV_CLIENT_SYNC_PRIM_OP *pasSyncOp)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   IMG_UINT32 ui32ServerIndex = 0;
   IMG_UINT32 ui32ClientIndex = 0;
   IMG_UINT32 i;
   IMG_BOOL bServerSync;
 
   /* Copy client sync operations */
   for (i=0;i<ui32SyncCount;i++)
   {
       /*
           Sanity check the client passes in the same syncs as the
           ones we got at create time
       */
       if (psCookie->papsSyncPrim[i] != pasSyncOp[i].psSync)
       {
           eError = PVRSRV_ERROR_INVALID_PARAMS;
           goto e0;
       }
 
       eError = SyncPrimIsServerSync(pasSyncOp[i].psSync, &bServerSync);
       if (PVRSRV_OK != eError) goto e0;
       if (bServerSync)
       {
           psCookie->paui32ServerFlags[ui32ServerIndex] =
                   pasSyncOp[i].ui32Flags;
 
           ui32ServerIndex++;
       }
       else
       {
           /* Client operation information */
           psCookie->paui32Flags[ui32ClientIndex] =
                   pasSyncOp[i].ui32Flags;
           psCookie->paui32FenceValue[ui32ClientIndex] =
                   pasSyncOp[i].ui32FenceValue;
           psCookie->paui32UpdateValue[ui32ClientIndex] =
                   pasSyncOp[i].ui32UpdateValue;
 
           ui32ClientIndex++;
       }
   }
 
   eError = BridgeSyncPrimOpTake(psCookie->hBridge,
                                 psCookie->hServerCookie,
                                 psCookie->ui32ClientSyncCount,
                                 psCookie->paui32Flags,
                                 psCookie->paui32FenceValue,
                                 psCookie->paui32UpdateValue,
                                 psCookie->ui32ServerSyncCount,
                                 psCookie->paui32ServerFlags);
 
e0:
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimOpReady(PSYNC_OP_COOKIE psCookie,
                            IMG_BOOL *pbReady)
{
   PVRSRV_ERROR eError;
   if (!psCookie)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid input pointer", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto e0;
   }
 
   /*
       If we have a server sync we have no choice
       but to do the check in the server
   */
   if (psCookie->bHaveServerSync)
   {
       eError = BridgeSyncPrimOpReady(psCookie->hBridge,
                                      psCookie->hServerCookie,
                                      pbReady);
       if (eError != PVRSRV_OK)
       {
           PVR_DPF((PVR_DBG_ERROR,
                    "%s: Failed to do sync check in server (Error = %d)",
                    __FUNCTION__, eError));
           goto e0;
       }
   }
   else
   {
       IMG_UINT32 i;
       IMG_UINT32 ui32SnapShot;
       IMG_BOOL bReady = IMG_TRUE;
 
       for (i=0;i<psCookie->ui32ClientSyncCount;i++)
       {
           if ((psCookie->paui32Flags[i] & PVRSRV_CLIENT_SYNC_PRIM_OP_CHECK) == 0)
           {
               continue;
           }
 
           ui32SnapShot = _SyncPrimBlockListGetClientValue(psCookie->psSyncBlockList,
                                                           psCookie->paui32SyncBlockIndex[i],
                                                           psCookie->paui32Index[i]);
           if (ui32SnapShot != psCookie->paui32FenceValue[i])
           {
               bReady = IMG_FALSE;
               break;
           }
       }
 
       *pbReady = bReady;
   }
 
   return PVRSRV_OK;
e0:
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimOpComplete(PSYNC_OP_COOKIE psCookie)
{
   PVRSRV_ERROR eError;
 
   eError = BridgeSyncPrimOpComplete(psCookie->hBridge,
                                     psCookie->hServerCookie);
 
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimOpDestroy(PSYNC_OP_COOKIE psCookie)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   IMG_UINT32 i;
 
   eError = BridgeSyncPrimOpDestroy(psCookie->hBridge, psCookie->hServerCookie);
   if (PVRSRV_OK != eError)
   {
       PVR_DPF((PVR_DBG_ERROR,
           "%s: Failed to destroy SyncPrimOp (Error = %d)",
            __FUNCTION__, eError));
       goto err_out;
   }
 
   /* Decrease the reference count on all referenced local sync prims
    * so that they can be freed now this Op is finished with
    */
   for (i=0;i<psCookie->ui32SyncCount;i++)
   {
       SYNC_PRIM *psSyncInt;
       psSyncInt = IMG_CONTAINER_OF(psCookie->papsSyncPrim[i], SYNC_PRIM, sCommon);
       if (SYNC_PRIM_TYPE_LOCAL == psSyncInt->eType)
       {
           SyncPrimLocalUnref(psSyncInt);
       }
   }
 
   _SyncPrimBlockListDestroy(psCookie->psSyncBlockList);
   OSFreeMem(psCookie);
 
err_out:
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimOpResolve(PSYNC_OP_COOKIE psCookie,
                              IMG_UINT32 *pui32SyncCount,
                              PVRSRV_CLIENT_SYNC_PRIM_OP **ppsSyncOp)
{
   IMG_UINT32 ui32ServerIndex = 0;
   IMG_UINT32 ui32ClientIndex = 0;
   PVRSRV_CLIENT_SYNC_PRIM_OP *psSyncOps;
   IMG_UINT32 i;
   IMG_BOOL bServerSync;
   PVRSRV_ERROR eError = PVRSRV_OK;
 
   psSyncOps = OSAllocMem(sizeof(PVRSRV_CLIENT_SYNC_PRIM_OP) * 
                          psCookie->ui32SyncCount);
   if (!psSyncOps)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto e0;
   }
 
   for (i=0; i<psCookie->ui32SyncCount; i++)
   {
       psSyncOps[i].psSync = psCookie->papsSyncPrim[i];
       eError = SyncPrimIsServerSync(psCookie->papsSyncPrim[i], &bServerSync);
       if (PVRSRV_OK != eError) goto e1;
       if (bServerSync)
       {
           psSyncOps[i].ui32FenceValue = 0;
           psSyncOps[i].ui32UpdateValue = 0;
           psSyncOps[i].ui32Flags = psCookie->paui32ServerFlags[ui32ServerIndex];
           ui32ServerIndex++;
       }
       else
       {
           psSyncOps[i].ui32FenceValue = psCookie->paui32FenceValue[ui32ClientIndex]; 
           psSyncOps[i].ui32UpdateValue = psCookie->paui32UpdateValue[ui32ClientIndex]; 
           psSyncOps[i].ui32Flags = psCookie->paui32Flags[ui32ClientIndex];
           ui32ClientIndex++;
       }
   }
 
   *ppsSyncOp = psSyncOps;
   *pui32SyncCount = psCookie->ui32SyncCount;
 
e1:
   OSFreeMem(psSyncOps);
e0:
   return eError;
}
 
#if !defined(__KERNEL__)
IMG_INTERNAL
PVRSRV_ERROR SyncPrimServerAlloc(SYNC_BRIDGE_HANDLE hBridge,
                                PVRSRV_CLIENT_SYNC_PRIM **ppsSync,
                                const IMG_CHAR *pszClassName
                                PVR_DBG_FILELINE_PARAM)
{
   IMG_CHAR szClassName[SYNC_MAX_CLASS_NAME_LEN];
   SYNC_PRIM *psNewSync;
   PVRSRV_ERROR eError;
 
#if !defined(PVR_SYNC_PRIM_ALLOC_TRACE)
   PVR_DBG_FILELINE_UNREF();
#endif
   psNewSync = OSAllocMem(sizeof(SYNC_PRIM));
   if (psNewSync == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto e0;
   }
   OSCachedMemSet(psNewSync, 0, sizeof(SYNC_PRIM));
 
   if(pszClassName)
   {
       /* Copy the class name annotation into a fixed-size array */
       OSStringNCopy(szClassName, pszClassName, SYNC_MAX_CLASS_NAME_LEN - 1);
       szClassName[SYNC_MAX_CLASS_NAME_LEN - 1] = 0;
   }
   else
   {
       /* No class name annotation */
       szClassName[0] = 0;
   }
 
   eError = BridgeServerSyncAlloc(hBridge,
                                  &psNewSync->u.sServer.hServerSync,
                                  &psNewSync->u.sServer.ui32FirmwareAddr,
                                  OSStringNLength(szClassName, SYNC_MAX_CLASS_NAME_LEN),
                                  szClassName);
 
   if (eError != PVRSRV_OK)
   {
       goto e1;
   }
 
#if defined(PVR_SYNC_PRIM_ALLOC_TRACE)
   PVR_DPF((PVR_DBG_WARNING, "Allocated sync=server fw=0x%x [%p]" PVR_DBG_FILELINE_FMT,
            psNewSync->u.sServer.ui32FirmwareAddr, &psNewSync->sCommon PVR_DBG_FILELINE_ARG));
#endif
 
   psNewSync->eType = SYNC_PRIM_TYPE_SERVER;
   psNewSync->u.sServer.hBridge = hBridge;
   *ppsSync = &psNewSync->sCommon;
 
   return PVRSRV_OK;
e1:
   OSFreeMem(psNewSync);
e0:
   return eError;
}
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimServerGetStatus(IMG_UINT32 ui32SyncCount,
                                    PVRSRV_CLIENT_SYNC_PRIM **papsSync,
                                    IMG_UINT32 *pui32UID,
                                    IMG_UINT32 *pui32FWAddr,
                                    IMG_UINT32 *pui32CurrentOp,
                                    IMG_UINT32 *pui32NextOp)
{
   PVRSRV_ERROR eError;
   IMG_UINT32 i;
   SYNC_BRIDGE_HANDLE hBridge = NULL;
   IMG_HANDLE *pahServerHandle;
   IMG_BOOL bServerSync;
 
   if (papsSync[0])
   {
       hBridge = _SyncPrimGetBridgeHandle(papsSync[0]);
   }
   if (!hBridge)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid Sync connection\n", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
       goto e0;
   }
 
   pahServerHandle = OSAllocMem(sizeof(IMG_HANDLE) * ui32SyncCount);
   if (pahServerHandle == NULL)
   {
       eError = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto e0;
   }
 
   /*
       Check that all the sync we've been passed are server syncs
       and that they all are on the same connection.
   */
   for (i=0;i<ui32SyncCount;i++)
   {
       SYNC_PRIM *psIntSync = IMG_CONTAINER_OF(papsSync[i], SYNC_PRIM, sCommon);
 
       eError = SyncPrimIsServerSync(papsSync[i], &bServerSync);
       if (PVRSRV_OK != eError) goto e1;
       if (!bServerSync)
       {
           eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
           goto e1;
       }
 
       if (!papsSync[i] || hBridge != _SyncPrimGetBridgeHandle(papsSync[i]))
       {
           PVR_DPF((PVR_DBG_ERROR, "SyncServerGetStatus: Sync connection is different\n"));
           eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
           goto e1;
       }
 
       pahServerHandle[i] = psIntSync->u.sServer.hServerSync;
   }
 
   eError = BridgeServerSyncGetStatus(hBridge,
                                      ui32SyncCount,
                                      pahServerHandle,
                                      pui32UID,
                                      pui32FWAddr,
                                      pui32CurrentOp,
                                      pui32NextOp);
   OSFreeMem(pahServerHandle);
 
   if (eError != PVRSRV_OK)
   {
       goto e0;
   }
   return PVRSRV_OK;
 
e1:
   OSFreeMem(pahServerHandle);
e0:
   return eError;
}
 
#endif
 
IMG_INTERNAL PVRSRV_ERROR
SyncPrimIsServerSync(PVRSRV_CLIENT_SYNC_PRIM *psSync, IMG_BOOL *pbServerSync)
{
   PVRSRV_ERROR eError = PVRSRV_OK;
   SYNC_PRIM *psSyncInt;
 
   if (!psSync)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid input pointer", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto e0;
   }
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
   if (psSyncInt->eType == SYNC_PRIM_TYPE_LOCAL)
   {
       *pbServerSync = IMG_FALSE;
   }
   else if (psSyncInt->eType == SYNC_PRIM_TYPE_SERVER)
   {
       *pbServerSync = IMG_TRUE;
   }
   else
   {
       /* Either the client has given us a bad pointer or there is an
        * error in this module
        */
       PVR_DPF((PVR_DBG_ERROR, "%s: Invalid sync type", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
       goto e0;
   }
 
e0:
   return eError;
}
 
IMG_INTERNAL
IMG_HANDLE SyncPrimGetServerHandle(PVRSRV_CLIENT_SYNC_PRIM *psSync)
{
   SYNC_PRIM *psSyncInt;
 
   if (!psSync)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid input pointer", __FUNCTION__));
       goto e0;
   }
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
   if (psSyncInt->eType == SYNC_PRIM_TYPE_SERVER)
   {
       return psSyncInt->u.sServer.hServerSync;
   }
   else
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid sync type (%d)",
           __FUNCTION__, psSyncInt->eType));
       goto e0;
   }
e0:
   return 0;
}
 
IMG_INTERNAL
PVRSRV_ERROR SyncPrimServerQueueOp(PVRSRV_CLIENT_SYNC_PRIM_OP *psSyncOp)
{
   SYNC_PRIM *psSyncInt;
   IMG_BOOL bUpdate;
   PVRSRV_ERROR eError;
 
   if (!psSyncOp)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid input pointer", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_PARAMS;
       goto e0;
   }
 
   psSyncInt = IMG_CONTAINER_OF(psSyncOp->psSync, SYNC_PRIM, sCommon);
   if (psSyncInt->eType != SYNC_PRIM_TYPE_SERVER)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: invalid sync type (%d)",
           __FUNCTION__, psSyncInt->eType));
       eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
       goto e0;
   }
   if (0 == psSyncOp->ui32Flags)
   {
       PVR_DPF((PVR_DBG_ERROR, "%s: no sync flags", __FUNCTION__));
       eError = PVRSRV_ERROR_INVALID_SYNC_PRIM;
       goto e0;
   }
 
   if (psSyncOp->ui32Flags & PVRSRV_CLIENT_SYNC_PRIM_OP_UPDATE)
   {
       bUpdate = IMG_TRUE;
   }else
   {
       bUpdate = IMG_FALSE;
   }
 
   eError = BridgeServerSyncQueueHWOp(psSyncInt->u.sServer.hBridge,
                                         psSyncInt->u.sServer.hServerSync,
                                         bUpdate,
                                         &psSyncOp->ui32FenceValue,
                                         &psSyncOp->ui32UpdateValue);
e0:
   return eError;
}
 
#if defined(PDUMP)
IMG_INTERNAL void SyncPrimPDump(PVRSRV_CLIENT_SYNC_PRIM *psSync)
{
   SYNC_PRIM *psSyncInt;
   SYNC_PRIM_BLOCK *psSyncBlock;
   SYNC_PRIM_CONTEXT *psContext;
   PVRSRV_ERROR eError;
 
   PVR_ASSERT(psSync != NULL);
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
 
   if (psSyncInt->eType != SYNC_PRIM_TYPE_LOCAL)
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimPDump: Invalid sync type"));
       PVR_ASSERT(IMG_FALSE);
       return;
   }
 
   psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
   psContext = psSyncBlock->psContext;
 
   eError = BridgeSyncPrimPDump(psContext->hDevConnection,
                                psSyncBlock->hServerSyncPrimBlock,
                                SyncPrimGetOffset(psSyncInt));
 
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR,
               "%s: failed with error %d",
               __FUNCTION__, eError));
   }
    PVR_ASSERT(eError == PVRSRV_OK);
}
 
IMG_INTERNAL void SyncPrimPDumpValue(PVRSRV_CLIENT_SYNC_PRIM *psSync, IMG_UINT32 ui32Value)
{
   SYNC_PRIM *psSyncInt;
   SYNC_PRIM_BLOCK *psSyncBlock;
   SYNC_PRIM_CONTEXT *psContext;
   PVRSRV_ERROR eError;
 
   PVR_ASSERT(psSync != NULL);
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
 
   if (psSyncInt->eType != SYNC_PRIM_TYPE_LOCAL)
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimPDump: Invalid sync type"));
       PVR_ASSERT(IMG_FALSE);
       return;
   }
 
   psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
   psContext = psSyncBlock->psContext;
 
   eError = BridgeSyncPrimPDumpValue(psContext->hDevConnection,
                                psSyncBlock->hServerSyncPrimBlock,
                                SyncPrimGetOffset(psSyncInt),
                                ui32Value);
 
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR,
               "%s: failed with error %d",
               __FUNCTION__, eError));
   }
    PVR_ASSERT(eError == PVRSRV_OK);
}
 
IMG_INTERNAL void SyncPrimPDumpPol(PVRSRV_CLIENT_SYNC_PRIM *psSync,
                                  IMG_UINT32 ui32Value,
                                  IMG_UINT32 ui32Mask,
                                  PDUMP_POLL_OPERATOR eOperator,
                                  IMG_UINT32 ui32PDumpFlags)
{
   SYNC_PRIM *psSyncInt;
   SYNC_PRIM_BLOCK *psSyncBlock;
   SYNC_PRIM_CONTEXT *psContext;
   PVRSRV_ERROR eError;
 
   PVR_ASSERT(psSync != NULL);
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
 
   if (psSyncInt->eType != SYNC_PRIM_TYPE_LOCAL)
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimPDumpPol: Invalid sync type (expected SYNC_PRIM_TYPE_LOCAL)"));
       PVR_ASSERT(IMG_FALSE);
       return;
   }
 
   psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
   psContext = psSyncBlock->psContext;
 
   eError = BridgeSyncPrimPDumpPol(psContext->hDevConnection,
                                   psSyncBlock->hServerSyncPrimBlock,
                                   SyncPrimGetOffset(psSyncInt),
                                   ui32Value,
                                   ui32Mask,
                                   eOperator,
                                   ui32PDumpFlags);
 
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR,
               "%s: failed with error %d",
               __FUNCTION__, eError));
   }
    PVR_ASSERT(eError == PVRSRV_OK);
}
 
IMG_INTERNAL void SyncPrimOpPDumpPol(PSYNC_OP_COOKIE psCookie,
                                    PDUMP_POLL_OPERATOR eOperator,
                                    IMG_UINT32 ui32PDumpFlags)
{
   PVRSRV_ERROR eError;
 
   PVR_ASSERT(psCookie != NULL);
 
   eError = BridgeSyncPrimOpPDumpPol(psCookie->hBridge,
                                   psCookie->hServerCookie,
                                   eOperator,
                                   ui32PDumpFlags);
 
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR,
               "%s: failed with error %d",
               __FUNCTION__, eError));
   }
   
    PVR_ASSERT(eError == PVRSRV_OK);
}
 
IMG_INTERNAL void SyncPrimPDumpCBP(PVRSRV_CLIENT_SYNC_PRIM *psSync,
                                  IMG_UINT64 uiWriteOffset,
                                  IMG_UINT64 uiPacketSize,
                                  IMG_UINT64 uiBufferSize)
{
   SYNC_PRIM *psSyncInt;
   SYNC_PRIM_BLOCK *psSyncBlock;
   SYNC_PRIM_CONTEXT *psContext;
   PVRSRV_ERROR eError;
 
   PVR_ASSERT(psSync != NULL);
   psSyncInt = IMG_CONTAINER_OF(psSync, SYNC_PRIM, sCommon);
 
   if (psSyncInt->eType != SYNC_PRIM_TYPE_LOCAL)
   {
       PVR_DPF((PVR_DBG_ERROR, "SyncPrimPDumpCBP: Invalid sync type"));
       PVR_ASSERT(IMG_FALSE);
       return;
   }
 
   psSyncBlock = psSyncInt->u.sLocal.psSyncBlock;
   psContext = psSyncBlock->psContext;
 
   /* FIXME: uiWriteOffset, uiPacketSize, uiBufferSize were changed to
    * 64-bit quantities to resolve Windows compiler warnings.
    * However the bridge is only 32-bit hence compiler warnings
    * of implicit cast and loss of data.
    * Added explicit cast and assert to remove warning.
    */
#if (defined(_WIN32) && !defined(_WIN64)) || (defined(LINUX) && defined(__i386__))
   PVR_ASSERT(uiWriteOffset<IMG_UINT32_MAX);
   PVR_ASSERT(uiPacketSize<IMG_UINT32_MAX);
   PVR_ASSERT(uiBufferSize<IMG_UINT32_MAX);
#endif
   eError = BridgeSyncPrimPDumpCBP(psContext->hDevConnection,
                                   psSyncBlock->hServerSyncPrimBlock,
                                   SyncPrimGetOffset(psSyncInt),
                                   (IMG_UINT32)uiWriteOffset,
                                   (IMG_UINT32)uiPacketSize,
                                   (IMG_UINT32)uiBufferSize);
 
   if (eError != PVRSRV_OK)
   {
       PVR_DPF((PVR_DBG_ERROR,
               "%s: failed with error %d",
               __FUNCTION__, eError));
   }
    PVR_ASSERT(eError == PVRSRV_OK);
}
 
#endif