hc
2023-08-30 862c27fc9920c83318c784bfdadf43a65df1ec8f
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
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
/* -*- mode: c; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* vi: set ts=8 sw=8 sts=8: */
/*************************************************************************/ /*!
@File           pvr_sync_file.c
@Title          Kernel driver for Android's sync mechanism
@Codingstyle    LinuxKernel
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@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 "pvr_sync.h"
#include "pvr_fd_sync_kernel.h"
#include "services_kernel_client.h"
 
#include <linux/sync_file.h>
#include <linux/dma-fence.h>
#include <linux/dma-fence-array.h>
 
#include <linux/slab.h>
#include <linux/file.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/syscalls.h>
#include <linux/miscdevice.h>
#include <linux/anon_inodes.h>
 
#include "kernel_compatibility.h"
 
#include <linux/kref.h>
 
#define for_each_sync_pt(s, f, c) \
   for ((c) = 0, (s) = (f)->num_fences == 0 ? \
       NULL : (struct sync_pt *)(f)->cbs[0].sync_pt; \
        (c) < (f)->num_fences; \
        (c)++,   (s) = (c) < (f)->num_fences ? \
       (struct sync_pt *)(f)->cbs[c].sync_pt : NULL)
 
 
/* #define DEBUG_OUTPUT 1 */
 
#ifdef DEBUG_OUTPUT
#define DPF(fmt, ...) pr_err("pvr_sync: " fmt "\n", __VA_ARGS__)
#else
#define DPF(fmt, ...) do {} while (0)
#endif
 
#define PVR_DUMPDEBUG_LOG(pfnDumpDebugPrintf, pvDumpDebugFile, fmt, ...) \
   do { \
       if (pfnDumpDebugPrintf) { \
           pfnDumpDebugPrintf(pvDumpDebugFile, fmt, __VA_ARGS__); \
       } else { \
           pr_info("pvr_sync: " fmt, __VA_ARGS__); \
       } \
   } while (0)
 
#define SYNC_MAX_POOL_SIZE 10
 
enum {
   SYNC_TL_TYPE = 0,
   SYNC_PT_FENCE_TYPE = 1,
   SYNC_PT_CLEANUP_TYPE = 2,
   SYNC_PT_FOREIGN_FENCE_TYPE = 3,
   SYNC_PT_FOREIGN_CLEANUP_TYPE = 4,
};
 
struct pvr_sync_append_data {
   u32                    nr_updates;
   struct _RGXFWIF_DEV_VIRTADDR_        *update_ufo_addresses;
   u32                    *update_values;
   u32                    nr_checks;
   struct _RGXFWIF_DEV_VIRTADDR_        *check_ufo_addresses;
   u32                    *check_values;
 
   /* The cleanup list is needed for rollback (as that's the only op
    * taken).
    */
   u32                    nr_cleanup_syncs;
   struct pvr_sync_native_sync_prim    **cleanup_syncs;
 
   /* A FD is reserved in append_fences, but is not associated with
    * the update fence until pvr_sync_get_update_fd().
    */
   int                    update_fence_fd;
 
   /* Keep the sync points around for fput and if rollback is needed */
   struct sync_file            *update_fence;
   struct pvr_sync_native_sync_prim    *update_sync;
   struct pvr_sync_native_sync_prim    *update_timeline_sync;
   struct sync_file            *check_fence;
};
 
/* Services client sync prim wrapper. This is used to hold debug information
 * and make it possible to cache unused syncs.
 */
struct pvr_sync_native_sync_prim {
   /* List for the sync pool support. */
   struct list_head list;
 
   /* Base services sync prim structure */
   struct PVRSRV_CLIENT_SYNC_PRIM *client_sync;
 
   /* The next queued value which should be used */
   u32 next_value;
 
   /* Every sync data will get some unique id */
   u32 id;
 
   /* FWAddr used by the client sync */
   u32 vaddr;
 
   /* The type this sync is used for in our driver. Used in
    * pvr_sync_debug_request().
    */
   u8 type;
 
   /* A debug class name also printed in pvr_sync_debug_request(). */
   char class[32];
 
   /* List for the cleanup syncs attached to a sync_pt */
   struct list_head cleanup_list;
};
 
/*
 * for sync_file, to define new pvr_dma_fence_timeline
 * refer to sw_sync.c
 *
 * struct sync_timeline - sync object
 * @kref:       reference count on fence.
 * @name:       name of the sync_timeline. Useful for debugging
 * @lock:       lock protecting @pt_list and @value
 * @pt_tree:        rbtree of active (unsignaled/errored) sync_pts
 * @pt_list:        list of active (unsignaled/errored) sync_pts
 * @sync_timeline_list: membership in global sync_timeline_list
 */
struct dma_fence_timeline { // 对标 sync_timeline
   struct kref     kref;
   const struct dma_fence_ops *ops;
   char            name[32];
 
   /* protected by lock */
   u64         context;
   int         value;
 
   struct rb_root      pt_tree;
   struct list_head    pt_list;
   spinlock_t      lock;
 
   struct list_head    sync_timeline_list;
};
 
/* This is the actual timeline metadata. We might keep this around after the
 * base sync driver has destroyed the pvr_sync_timeline_wrapper object.
 */
struct pvr_sync_timeline {
   /* Back reference to the sync_timeline. Not always valid */
   struct dma_fence_timeline *obj;
 
   /* Global timeline list support */
   struct list_head list;
 
   /* Timeline sync */
   struct pvr_sync_kernel_pair *kernel;
 
   /* Reference count for this object */
   struct kref kref;
 
   /* Used only by pvr_sync_update_all_timelines(). False if the timeline
    * has been detected as racing with pvr_sync_destroy_timeline().
    */
   bool valid;
};
 
/* This is the IMG extension of a sync_timeline */
struct pvr_sync_timeline_wrapper {
   /* Original timeline struct. Needs to come first. */
   struct dma_fence_timeline obj;
 
   /* Pointer to extra timeline data. Separated life-cycle. */
   struct pvr_sync_timeline *timeline;
};
 
struct pvr_sync_kernel_pair {
   /* Binary sync point representing the android native sync in hw. */
   struct pvr_sync_native_sync_prim *fence_sync;
 
   /* Cleanup sync list. If the base sync prim is used for "checking"
    * only within a GL stream, there is no way of knowing when this has
    * happened. So each check appends another sync prim just used for
    * update at the end of the command, so we know if all syncs in this
    * cleanup list are complete there are no outstanding renders waiting
    * to check this, so it can safely be freed.
    */
   struct list_head cleanup_sync_list;
   /*  A temporary pointer used to track the 'new' cleanup_sync added to
    *  cleanup_sync_list within pvr_sync_append_fences()
    */
   struct pvr_sync_native_sync_prim *current_cleanup_sync;
 
   /* Sync points can go away when there are deferred hardware operations
    * still outstanding. We must not free the SERVER_SYNC_PRIMITIVE until
    * the hardware is finished, so we add it to a defer list which is
    * processed periodically ("defer-free").
    *
    * Note that the defer-free list is global, not per-timeline.
    */
   struct list_head list;
};
 
struct pvr_sync_data {
   /* Every sync point has a services sync object. This object is used
    * by the hardware to enforce ordering -- it is attached as a source
    * dependency to various commands.
    */
   struct pvr_sync_kernel_pair *kernel;
 
   /* The timeline update value for this sync point. */
   u32 timeline_update_value;
 
   /* This refcount is incremented at create and dup time, and decremented
    * at free time. It ensures the object doesn't start the defer-free
    * process until it is no longer referenced.
    */
   struct kref kref;
};
 
/*
 * define dma_fence_pt
 *
 * struct sync_pt - sync_pt object
 * @base: base fence object
 * @link: link on the sync timeline's list
 * @node: node in the sync timeline's tree
 */
struct dma_fence_pt { // 对标 sync_pt
   struct dma_fence base;
   struct list_head link;
   struct rb_node node;
};
 
/* This is the IMG extension of a sync_pt */
struct pvr_sync_pt {
   /* Original sync_pt structure. Needs to come first. */
   struct dma_fence_pt pt;
 
   /* Private shared data */
   struct pvr_sync_data *sync_data;
};
 
/* This is the IMG extension of a sync_fence */
struct pvr_sync_fence {
   /* Original sync_fence structure. Needs to come first. */
   struct sync_file *fence;
 
   /* To ensure callbacks are always received for fences / sync_pts, even
    * after the fence has been 'put' (freed), we must take a reference to
    * the fence. We still need to 'put' the fence ourselves, but this might
    * happen in irq context, where fput() is not allowed (in kernels <3.6).
    * We must add the fence to a list which is processed in WQ context.
    */
   struct list_head list;
};
 
/* Any sync point from a foreign (non-PVR) timeline needs to have a "shadow"
 * sync prim. This is modelled as a software operation. The foreign driver
 * completes the operation by calling a callback we registered with it.
 */
struct pvr_sync_fence_waiter {
   /* Base sync driver waiter structure */
   struct dma_fence_cb waiter;
 
   /* "Shadow" sync prim backing the foreign driver's sync_pt */
   struct pvr_sync_kernel_pair *kernel;
 
   /* Optimizes lookup of fence for defer-put operation */
   struct pvr_sync_fence *sync_fence;
};
 
/* Global data for the sync driver */
static struct {
   /* Complete notify handle */
   void *command_complete_handle;
 
   /* Defer-free workqueue. Syncs may still be in use by the HW when freed,
    * so we have to keep them around until the HW is done with them at
    * some later time. This workqueue iterates over the list of free'd
    * syncs, checks if they are in use, and frees the sync device memory
    * when done with.
    */
   struct workqueue_struct *defer_free_wq;
   struct work_struct defer_free_work;
 
   /* check_status workqueue: When a foreign point is completed, a SW
    * operation marks the sync as completed to allow the operations to
    * continue. This completion may require the hardware to be notified,
    * which may be expensive/take locks, so we push that to a workqueue
    */
   struct workqueue_struct *check_status_wq;
   struct work_struct check_status_work;
 
   /* Context used to create client sync prims. */
   struct SYNC_PRIM_CONTEXT *sync_prim_context;
 
   /* Debug notify handle */
   void *debug_notify_handle;
 
   /* Unique id counter for the sync prims */
   atomic_t sync_id;
 
   /* The global event object (used to wait between checks for
    * deferred-free sync status).
    */
   void *event_object_handle;
} pvr_sync_data;
 
static void sync_timeline_get(struct dma_fence_timeline *obj)
{
   kref_get(&obj->kref);
}
 
static void pvr_sync_release_timeline(struct pvr_sync_timeline *timeline);
 
static void sync_timeline_free(struct kref *kref)
{
   struct dma_fence_timeline *obj =
       container_of(kref, struct dma_fence_timeline, kref);
 
   struct pvr_sync_timeline_wrapper *timeline_wrapper =
       (struct pvr_sync_timeline_wrapper *)obj;
 
   pvr_sync_release_timeline(timeline_wrapper->timeline);
 
   kfree(obj);
}
 
static void sync_timeline_put(struct dma_fence_timeline *obj)
{
   kref_put(&obj->kref, sync_timeline_free);
}
 
/*---------------------------------------------------------------------------*/
 
static inline
struct dma_fence_timeline *dma_fence_parent(struct dma_fence *fence)
{
   return container_of(fence->lock, struct dma_fence_timeline, lock);
}
 
/*---------------------------------------------------------------------------*/
 
#define PVR_DRV_NAME "pvr"
#define PVR_TIMELINE_NAME PVR_DRV_NAME ".timeline"
 
static const char *
pvr_fence_get_driver_name(struct dma_fence *fence)
{
   return PVR_DRV_NAME;
}
 
static const char *
pvr_fence_get_timeline_name(struct dma_fence *fence)
{
   struct dma_fence_timeline *parent = dma_fence_parent(fence);
 
   return parent->name;
}
 
static bool
pvr_fence_enable_signaling(struct dma_fence *fence)
{
   return true;
}
 
static void pvr_sync_fence_install(struct sync_file *fence, int fd)
{
   fd_install(fd, fence->file);
}
 
static inline
struct dma_fence_timeline *pvr_sync_pt_parent(struct dma_fence_pt *pt)
{
   return container_of(pt->base.lock, struct dma_fence_timeline, lock);
}
 
/* List of timelines created by this driver */
static LIST_HEAD(timeline_list);
static DEFINE_MUTEX(timeline_list_mutex);
 
/* Sync pool support */
static LIST_HEAD(sync_pool_free_list);
static LIST_HEAD(sync_pool_active_list);
static DEFINE_MUTEX(sync_pool_mutex);
static s32 sync_pool_size;
static u32 sync_pool_created;
static u32 sync_pool_reused;
 
/* The "defer-free" object list. Driver global. */
static LIST_HEAD(sync_prim_free_list);
static DEFINE_SPINLOCK(sync_prim_free_list_spinlock);
 
/* The "defer-put" object list. Driver global. */
static LIST_HEAD(sync_fence_put_list);
static DEFINE_SPINLOCK(sync_fence_put_list_spinlock);
 
static void pvr_sync_update_all_timelines(void *command_complete_handle);
 
static inline void set_sync_value(struct pvr_sync_native_sync_prim *sync,
                 u32 value)
{
   *(sync->client_sync->pui32LinAddr) = value;
}
 
static inline u32 get_sync_value(struct pvr_sync_native_sync_prim *sync)
{
   return *(sync->client_sync->pui32LinAddr);
}
 
static inline void complete_sync(struct pvr_sync_native_sync_prim *sync)
{
   *(sync->client_sync->pui32LinAddr) = sync->next_value;
}
 
static inline bool is_sync_met(struct pvr_sync_native_sync_prim *sync)
{
   return *(sync->client_sync->pui32LinAddr) == sync->next_value;
}
 
static inline
struct pvr_sync_timeline *get_timeline(struct dma_fence_timeline *obj)
{
   return ((struct pvr_sync_timeline_wrapper *)obj)->timeline;
}
 
static inline struct pvr_sync_timeline *get_timeline_pt(struct dma_fence_pt *pt)
{
   return get_timeline(pvr_sync_pt_parent(pt));
}
 
static inline bool
pvr_sync_has_kernel_signaled(struct pvr_sync_kernel_pair *kernel)
{
   /* Idle syncs are always signaled */
   if (!kernel)
       return 1;
 
   return is_sync_met(kernel->fence_sync);
}
 
const struct dma_fence_ops pvr_fence_ops;
 
static inline
struct dma_fence_pt *dma_fence_to_pvr_dma_fence_pt(struct dma_fence *fence)
{
   if (fence->ops != &pvr_fence_ops) {
       BUG();
       return NULL;
   }
 
   return container_of(fence, struct dma_fence_pt, base);
}
 
static inline
struct pvr_sync_pt *dma_fence_to_pvr_sync_pt(struct dma_fence *dma_fence)
{
   struct dma_fence_pt *dma_fence_pt = dma_fence_to_pvr_dma_fence_pt(dma_fence);
 
   return container_of(dma_fence_pt, struct pvr_sync_pt, pt);
}
 
#ifdef DEBUG_OUTPUT
 
static char *debug_info_timeline(struct pvr_sync_timeline *timeline)
{
   static char info[256];
 
   snprintf(info, sizeof(info),
        "n='%s' id=%u fw=0x%x tl_curr=%u tl_next=%u",
        timeline->obj ? timeline->obj->name : "?",
        timeline->kernel->fence_sync->id,
        timeline->kernel->fence_sync->vaddr,
        get_sync_value(timeline->kernel->fence_sync),
        timeline->kernel->fence_sync->next_value);
 
   return info;
}
 
static char *debug_info_sync_pt(struct dma_fence_pt *pt)
{
   struct pvr_sync_timeline *timeline = get_timeline_pt(pt);
   struct pvr_sync_pt *pvr_pt = (struct pvr_sync_pt *)pt;
   struct pvr_sync_kernel_pair *kernel = pvr_pt->sync_data->kernel;
   static char info[256], info1[256];
 
   if (kernel) {
       unsigned int cleanup_count = 0;
       unsigned int info1_pos = 0;
       struct list_head *pos;
 
       info1[0] = 0;
 
       list_for_each(pos, &kernel->cleanup_sync_list) {
           struct pvr_sync_native_sync_prim *cleanup_sync =
               list_entry(pos,
                   struct pvr_sync_native_sync_prim,
                   cleanup_list);
           int string_size = 0;
 
           string_size = snprintf(info1 + info1_pos,
               sizeof(info1) - info1_pos,
               " # cleanup %u: id=%u fw=0x%x curr=%u next=%u",
               cleanup_count,
               cleanup_sync->id,
               cleanup_sync->vaddr,
               get_sync_value(cleanup_sync),
               cleanup_sync->next_value);
           cleanup_count++;
           info1_pos += string_size;
           /* Truncate the string and stop if we run out of space
            * This should stop any underflow of snprintf's 'size'
            * arg too
            */
           if (info1_pos >= sizeof(info1))
               break;
       }
 
       snprintf(info, sizeof(info),
            "status=%d tl_taken=%u ref=%d # sync: id=%u fw=0x%x curr=%u next=%u%s # tl: %s",
            pvr_sync_has_kernel_signaled(kernel),
            pvr_pt->sync_data->timeline_update_value,
            kref_read(&pvr_pt->sync_data->kref),
            kernel->fence_sync->id,
            kernel->fence_sync->vaddr,
            get_sync_value(kernel->fence_sync),
            kernel->fence_sync->next_value,
            info1, debug_info_timeline(timeline));
   } else {
       snprintf(info, sizeof(info),
            "status=%d tl_taken=%u ref=%d # sync: idle # tl: %s",
            pvr_sync_has_kernel_signaled(kernel),
            pvr_pt->sync_data->timeline_update_value,
            kref_read(&pvr_pt->sync_data->kref),
            debug_info_timeline(timeline));
   }
 
   return info;
}
 
#endif /* DEBUG_OUTPUT */
 
static enum PVRSRV_ERROR
sync_pool_get(struct pvr_sync_native_sync_prim **_sync,
         const char *class_name, u8 type)
{
   struct pvr_sync_native_sync_prim *sync;
   enum PVRSRV_ERROR error = PVRSRV_OK;
   u32 sync_addr;
 
   mutex_lock(&sync_pool_mutex);
 
   if (list_empty(&sync_pool_free_list)) {
       /* If there is nothing in the pool, create a new sync prim. */
       sync = kmalloc(sizeof(*sync),
                  GFP_KERNEL);
       if (!sync) {
           pr_err("pvr_sync: %s: Failed to allocate sync data\n",
                  __func__);
           error = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_unlock;
       }
 
       error = SyncPrimAlloc(pvr_sync_data.sync_prim_context,
                     &sync->client_sync, class_name);
       if (error != PVRSRV_OK) {
           pr_err("pvr_sync: %s: Failed to allocate sync prim (%s)\n",
                  __func__, PVRSRVGetErrorStringKM(error));
           goto err_free;
       }
 
       error = SyncPrimGetFirmwareAddr(sync->client_sync, &sync_addr);
       if (error != PVRSRV_OK) {
           pr_err("pvr_sync: %s: Failed to get FW address (%s)\n",
                  __func__, PVRSRVGetErrorStringKM(error));
           goto err_sync_prim_free;
       }
       sync->vaddr = sync_addr;
 
       list_add_tail(&sync->list, &sync_pool_active_list);
       ++sync_pool_created;
   } else {
       sync = list_first_entry(&sync_pool_free_list,
                   struct pvr_sync_native_sync_prim, list);
       list_move_tail(&sync->list, &sync_pool_active_list);
       --sync_pool_size;
       ++sync_pool_reused;
   }
 
   sync->id = atomic_inc_return(&pvr_sync_data.sync_id);
   sync->type = type;
 
   strncpy(sync->class, class_name, sizeof(sync->class));
   sync->class[sizeof(sync->class) - 1] = '\0';
   /* Its crucial to reset the sync to zero */
   set_sync_value(sync, 0);
   sync->next_value = 0;
 
   *_sync = sync;
err_unlock:
   mutex_unlock(&sync_pool_mutex);
   return error;
 
err_sync_prim_free:
   SyncPrimFree(sync->client_sync);
 
err_free:
   kfree(sync);
   goto err_unlock;
}
 
static void sync_pool_put(struct pvr_sync_native_sync_prim *sync)
{
   mutex_lock(&sync_pool_mutex);
 
   if (sync_pool_size < SYNC_MAX_POOL_SIZE) {
       /* Mark it as unused */
       set_sync_value(sync, 0xffffffff);
 
       list_move(&sync->list, &sync_pool_free_list);
       ++sync_pool_size;
   } else {
       /* Mark it as invalid */
       set_sync_value(sync, 0xdeadbeef);
 
       list_del(&sync->list);
       SyncPrimFree(sync->client_sync);
       kfree(sync);
   }
 
   mutex_unlock(&sync_pool_mutex);
}
 
static void sync_pool_clear(void)
{
   struct pvr_sync_native_sync_prim *sync, *n;
 
   mutex_lock(&sync_pool_mutex);
 
   list_for_each_entry_safe(sync, n, &sync_pool_free_list, list) {
       /* Mark it as invalid */
       set_sync_value(sync, 0xdeadbeef);
 
       list_del(&sync->list);
       SyncPrimFree(sync->client_sync);
       kfree(sync);
       --sync_pool_size;
   }
 
   mutex_unlock(&sync_pool_mutex);
}
 
static void pvr_sync_debug_request(void *hDebugRequestHandle,
                  u32 ui32VerbLevel,
                  DUMPDEBUG_PRINTF_FUNC *pfnDumpDebugPrintf,
                  void *pvDumpDebugFile)
{
   struct pvr_sync_native_sync_prim *sync;
 
   static const char *const type_names[] = {
       "Timeline", "Fence", "Cleanup",
       "Foreign Fence", "Foreign Cleanup"
   };
 
   if (ui32VerbLevel == DEBUG_REQUEST_VERBOSITY_HIGH) {
       mutex_lock(&sync_pool_mutex);
 
       PVR_DUMPDEBUG_LOG(pfnDumpDebugPrintf, pvDumpDebugFile,
                 "Dumping all pending android native syncs (Pool usage: %d%% - %d %d)",
                 sync_pool_reused ?
                 (10000 /
                  ((sync_pool_created + sync_pool_reused) *
                   100 / sync_pool_reused)) : 0,
                 sync_pool_created, sync_pool_reused);
 
       list_for_each_entry(sync, &sync_pool_active_list, list) {
           if (is_sync_met(sync))
               continue;
 
           BUG_ON(sync->type >= ARRAY_SIZE(type_names));
 
           PVR_DUMPDEBUG_LOG(pfnDumpDebugPrintf, pvDumpDebugFile,
                     "\tID = %d, FWAddr = 0x%08x: Current = 0x%08x, Next = 0x%08x, %s (%s)",
                     sync->id, sync->vaddr,
                     get_sync_value(sync),
                     sync->next_value,
                     sync->class,
                     type_names[sync->type]);
       }
       mutex_unlock(&sync_pool_mutex);
   }
}
 
/*
 * 调用者必须保证 传入的 'fence' 指向 pvr_dma_fence.
 */
static bool pvr_sync_has_signaled(struct dma_fence *fence)
{
   struct dma_fence_pt *sync_pt = dma_fence_to_pvr_dma_fence_pt(fence);
   struct pvr_sync_pt *pvr_pt = (struct pvr_sync_pt *)sync_pt;
 
   DPF("%s: # %s", __func__, debug_info_sync_pt(sync_pt));
 
   return pvr_sync_has_kernel_signaled(pvr_pt->sync_data->kernel);
}
 
static void wait_for_sync(struct pvr_sync_native_sync_prim *sync)
{
#ifndef NO_HARDWARE
   void *event_object = NULL;
   enum PVRSRV_ERROR error = PVRSRV_OK;
 
   while (sync && !is_sync_met(sync)) {
       if (!event_object) {
           error = OSEventObjectOpen(
               pvr_sync_data.event_object_handle,
               &event_object);
           if (error != PVRSRV_OK) {
               pr_err("pvr_sync: %s: Error opening event object (%s)\n",
                   __func__,
                   PVRSRVGetErrorStringKM(error));
               break;
           }
       }
       error = OSEventObjectWait(event_object);
       if (error != PVRSRV_OK && error != PVRSRV_ERROR_TIMEOUT) {
           pr_err("pvr_sync: %s: Error waiting on event object (%s)\n",
               __func__,
               PVRSRVGetErrorStringKM(error));
       }
   }
 
   if (event_object)
       OSEventObjectClose(event_object);
#endif /* NO_HARDWARE */
}
 
static void pvr_sync_defer_free(struct pvr_sync_kernel_pair *kernel)
{
   unsigned long flags;
 
   spin_lock_irqsave(&sync_prim_free_list_spinlock, flags);
   list_add_tail(&kernel->list, &sync_prim_free_list);
   spin_unlock_irqrestore(&sync_prim_free_list_spinlock, flags);
 
   queue_work(pvr_sync_data.defer_free_wq, &pvr_sync_data.defer_free_work);
}
 
/* This function assumes the timeline_list_mutex is held while it runs */
 
static void pvr_sync_destroy_timeline_locked(struct kref *kref)
{
   struct pvr_sync_timeline *timeline = (struct pvr_sync_timeline *)
       container_of(kref, struct pvr_sync_timeline, kref);
 
   pvr_sync_defer_free(timeline->kernel);
   list_del(&timeline->list);
   kfree(timeline);
}
 
static void pvr_sync_destroy_timeline(struct kref *kref)
{
   mutex_lock(&timeline_list_mutex);
   pvr_sync_destroy_timeline_locked(kref);
   mutex_unlock(&timeline_list_mutex);
}
 
static void pvr_sync_release_timeline(struct pvr_sync_timeline *timeline)
{
   /* If pvr_sync_open failed after calling sync_timeline_create, this
    * can be called with a timeline that has not got a timeline sync
    * or been added to our timeline list. Use a NULL timeline to
    * detect and handle this condition
    */
   if (!timeline)
       return;
 
   DPF("%s: # %s", __func__, debug_info_timeline(timeline));
 
   wait_for_sync(timeline->kernel->fence_sync);
 
   /* Whether or not we're the last reference, obj is going away
    * after this function returns, so remove our back reference
    * to it.
    */
   timeline->obj = NULL;
 
   /* This might be the last reference to the timeline object.
    * If so, we'll go ahead and delete it now.
    */
   kref_put(&timeline->kref, pvr_sync_destroy_timeline);
}
 
/* pvr_sync_create_sync_data() should be called with the bridge lock held */
static struct pvr_sync_data *
pvr_sync_create_sync_data(struct dma_fence_timeline *obj)
{
   struct pvr_sync_data *sync_data = NULL;
   enum PVRSRV_ERROR error;
 
   sync_data = kzalloc(sizeof(*sync_data), GFP_KERNEL);
   if (!sync_data)
       goto err_out;
 
   kref_init(&sync_data->kref);
 
   sync_data->kernel =
       kzalloc(sizeof(*sync_data->kernel),
       GFP_KERNEL);
 
   if (!sync_data->kernel)
       goto err_free_data;
 
   INIT_LIST_HEAD(&sync_data->kernel->cleanup_sync_list);
 
   error = sync_pool_get(&sync_data->kernel->fence_sync,
                 obj->name, SYNC_PT_FENCE_TYPE);
 
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to allocate sync prim (%s)\n",
              __func__, PVRSRVGetErrorStringKM(error));
       goto err_free_kernel;
   }
 
err_out:
   return sync_data;
 
err_free_kernel:
   kfree(sync_data->kernel);
err_free_data:
   kfree(sync_data);
   sync_data = NULL;
   goto err_out;
}
 
static void pvr_sync_free_sync_data(struct kref *kref)
{
   struct pvr_sync_data *sync_data = (struct pvr_sync_data *)
       container_of(kref, struct pvr_sync_data, kref);
 
   if (sync_data->kernel)
       pvr_sync_defer_free(sync_data->kernel);
   kfree(sync_data);
}
 
static void pvr_sync_free_sync(struct dma_fence_pt *sync_pt)
{
   struct pvr_sync_pt *pvr_pt = (struct pvr_sync_pt *)sync_pt;
 
   DPF("%s: # %s", __func__, debug_info_sync_pt(sync_pt));
 
   kref_put(&pvr_pt->sync_data->kref, pvr_sync_free_sync_data);
}
 
static void timeline_fence_value_str(struct dma_fence *fence,
   char *str, int size)
{
   snprintf(str, size, "%d", fence->seqno);
}
 
static void timeline_fence_timeline_value_str(struct dma_fence *fence,
   char *str, int size)
{
   struct dma_fence_timeline *parent = dma_fence_parent(fence);
 
   snprintf(str, size, "%d", parent->value);
}
 
static void pvr_fence_release(struct dma_fence *fence)
{
       // struct pvr_sync_pt *pvr_sync_pt = dma_fence_to_pvr_sync_pt(fence);
   struct dma_fence_pt *pt = dma_fence_to_pvr_dma_fence_pt(fence);
   struct dma_fence_timeline *parent = dma_fence_parent(fence);
   unsigned long flags;
 
   /*-------------------------------------------------------*/
   spin_lock_irqsave(fence->lock, flags);
 
   /* 释放当前 pvr_sync_pt 实例的 private_data. */
   pvr_sync_free_sync(pt);
 
    /*-------------------------------------------------------*/
   /* 参考自 timeline_fence_release(). */
 
   if (!list_empty(&pt->link)) {
       list_del(&pt->link);
       rb_erase(&pt->node, &parent->pt_tree);
   }
 
   spin_unlock_irqrestore(fence->lock, flags);
 
   sync_timeline_put(parent);
 
   dma_fence_free(fence);
}
 
const struct dma_fence_ops pvr_fence_ops = {
   //wait: refer to mali
   .wait = dma_fence_default_wait,
   .get_driver_name = pvr_fence_get_driver_name,
   .get_timeline_name = pvr_fence_get_timeline_name,
   .enable_signaling = pvr_fence_enable_signaling,
//Warning
   .fence_value_str = timeline_fence_value_str,
//Warning
   .timeline_value_str = timeline_fence_timeline_value_str,
   .signaled = pvr_sync_has_signaled,
   .release = pvr_fence_release,
};
 
/**
 * pvr_sync_pt_create() - creates a sync pt
 * @obj:    parent sync_timeline
 * @value:  value of the fence
 *
 * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
 * allocated allowing for implementation specific data to be kept after
 * the generic sync_timeline struct. Returns the sync_pt object or
 * NULL in case of error.
 */
struct dma_fence_pt *pvr_sync_pt_create(struct dma_fence_timeline *obj,
       unsigned int value, struct pvr_sync_data *sync_data)
{
   struct dma_fence_pt *pt;
   struct pvr_sync_pt *pvr_pt = NULL;
 
   pt = kzalloc(sizeof(struct pvr_sync_pt), GFP_KERNEL);
   if (!pt)
       return NULL;
 
   sync_timeline_get(obj);
   dma_fence_init(&pt->base, &pvr_fence_ops, &obj->lock,
           obj->context, value);
   INIT_LIST_HEAD(&pt->link);
 
   spin_lock_irq(&obj->lock);
 
   pvr_pt = (struct pvr_sync_pt *)pt;
 
   pvr_pt->sync_data = sync_data;
 
   if (!dma_fence_is_signaled_locked(&pt->base)) {
       struct rb_node **p = &obj->pt_tree.rb_node;
       struct rb_node *parent = NULL;
 
       while (*p) {
           struct dma_fence_pt *other;
           int cmp;
 
           parent = *p;
           other = rb_entry(parent, typeof(*pt), node);
           cmp = value - other->base.seqno;
           if (cmp > 0) {
               p = &parent->rb_right;
           } else if (cmp < 0) {
               p = &parent->rb_left;
           } else {
               if (dma_fence_get_rcu(&other->base)) {
                   sync_timeline_put(obj);
                   kfree(pt);
                   pt = other;
                   goto unlock;
               }
               p = &parent->rb_left;
           }
       }
       rb_link_node(&pt->node, parent, p);
       rb_insert_color(&pt->node, &obj->pt_tree);
 
       parent = rb_next(&pt->node);
       list_add_tail(&pt->link,
               parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
   }
unlock:
   spin_unlock_irq(&obj->lock);
 
   return pt;
}
 
/**
 * sync_timeline_create() - creates a sync object
 * @name:   sync_timeline name
 *
 * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
 * case of error.
 */
static struct dma_fence_timeline *pvr_sync_timeline_create(const char *name, int size, const struct dma_fence_ops *ops)
{
   struct dma_fence_timeline *obj;
 
   obj = kzalloc(size, GFP_KERNEL);
   if (!obj)
       return NULL;
 
   kref_init(&obj->kref);
   obj->ops = ops;
   obj->context = dma_fence_context_alloc(1);
   strlcpy(obj->name, name, sizeof(obj->name));
 
   obj->pt_tree = RB_ROOT;
   INIT_LIST_HEAD(&obj->pt_list);
   spin_lock_init(&obj->lock);
 
   //sync_timeline_debug_add(obj);
 
   return obj;
}
 
static bool timeline_fence_signaled(struct dma_fence *fence)
{
   return pvr_sync_has_signaled(fence);
}
 
/**
 * pvr_sync_timeline_signal() - signal a status change on a sync_timeline
 * @obj:    sync_timeline to signal
 * @inc:    num to increment on timeline->value
 *
 * A sync implementation should call this any time one of it's fences
 * has signaled or has an error condition.
 */
static void pvr_sync_timeline_signal(struct dma_fence_timeline *obj)
{
   struct dma_fence_pt *pt, *next;
 
   //trace_sync_timeline(obj);
 
   spin_lock_irq(&obj->lock);
 
   //obj->value += inc;
 
   list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
       if (!timeline_fence_signaled(&pt->base))
           break;
 
       list_del_init(&pt->link);
       rb_erase(&pt->node, &obj->pt_tree);
 
       /*
        * A signal callback may release the last reference to this
        * fence, causing it to be freed. That operation has to be
        * last to avoid a use after free inside this loop, and must
        * be after we remove the fence from the timeline in order to
        * prevent deadlocking on timeline->lock inside
        * timeline_fence_release().
        */
       dma_fence_signal_locked(&pt->base);
   }
 
   spin_unlock_irq(&obj->lock);
}
 
void pvr_sync_timeline_destroy(struct dma_fence_timeline *obj)
{
    //obj->destroyed = true;
    /*
     * Ensure timeline is marked as destroyed before
     * changing timeline's fences status.
     */
    //smp_wmb();
 
    /*
     * signal any children that their parent is going away.
     */
   pvr_sync_timeline_signal(obj);
   sync_timeline_put(obj);
}
 
static inline bool is_pvr_timeline(struct dma_fence_timeline *obj)
{
   return obj->ops == &pvr_fence_ops;
}
 
static inline bool is_pvr_dma_fence(struct dma_fence *dma_fence)
{
   return dma_fence->ops == &pvr_fence_ops;
}
 
/* foreign sync handling */
 
static void pvr_sync_foreign_sync_pt_signaled(struct dma_fence *fence,
                         struct dma_fence_cb *_waiter)
{
   struct pvr_sync_fence_waiter *waiter =
       (struct pvr_sync_fence_waiter *)_waiter;
   unsigned long flags;
 
   /* Complete the SW operation and free the sync if we can. If we can't,
    * it will be checked by a later workqueue kick.
    */
   complete_sync(waiter->kernel->fence_sync);
 
   /* We can 'put' the fence now, but this function might be called in
    * irq context so we must defer to WQ.
    * This WQ is triggered in pvr_sync_defer_free, so adding it to the
    * put list before that should guarantee it's cleaned up on the next
    * wq run.
    */
   spin_lock_irqsave(&sync_fence_put_list_spinlock, flags);
   list_add_tail(&waiter->sync_fence->list, &sync_fence_put_list);
   spin_unlock_irqrestore(&sync_fence_put_list_spinlock, flags);
 
   pvr_sync_defer_free(waiter->kernel);
 
   /* The completed sw-sync may allow other tasks to complete,
    * so we need to allow them to progress.
    */
   queue_work(pvr_sync_data.check_status_wq,
       &pvr_sync_data.check_status_work);
 
   kfree(waiter);
}
 
static struct pvr_sync_kernel_pair *
pvr_sync_create_waiter_for_foreign_sync(int fd)
{
   struct pvr_sync_native_sync_prim *cleanup_sync = NULL;
   struct pvr_sync_kernel_pair *kernel = NULL;
   struct pvr_sync_fence_waiter *waiter;
   struct pvr_sync_fence *sync_fence;
 
   struct sync_file *fence;
 
   enum PVRSRV_ERROR error;
   int err;
 
   //fence = sync_fence_fdget(fd);
   fence = sync_file_get(fd);
   if (!fence) {
       pr_err("pvr_sync: %s: Failed to take reference on fence\n",
              __func__);
       goto err_out;
   }
 
   kernel = kmalloc(sizeof(*kernel), GFP_KERNEL);
   if (!kernel) {
       pr_err("pvr_sync: %s: Failed to allocate sync kernel\n",
              __func__);
       goto err_put_fence;
   }
 
   INIT_LIST_HEAD(&kernel->cleanup_sync_list);
 
   sync_fence = kmalloc(sizeof(*sync_fence), GFP_KERNEL);
   if (!sync_fence) {
       pr_err("pvr_sync: %s: Failed to allocate pvr sync fence\n",
              __func__);
       goto err_free_kernel;
   }
 
   sync_fence->fence = fence;
 
   error = sync_pool_get(&kernel->fence_sync,
                 fence->user_name, SYNC_PT_FOREIGN_FENCE_TYPE);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to allocate sync prim (%s)\n",
              __func__, PVRSRVGetErrorStringKM(error));
       goto err_free_sync_fence;
   }
 
   kernel->fence_sync->next_value++;
 
   error = sync_pool_get(&cleanup_sync, fence->user_name,
       SYNC_PT_FOREIGN_CLEANUP_TYPE);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to allocate cleanup sync prim (%s)\n",
              __func__, PVRSRVGetErrorStringKM(error));
       goto err_free_sync;
   }
 
   cleanup_sync->next_value++;
 
   list_add(&cleanup_sync->cleanup_list, &kernel->cleanup_sync_list);
 
   /* The custom waiter structure is freed in the waiter callback */
   waiter = kmalloc(sizeof(*waiter), GFP_KERNEL);
   if (!waiter) {
       pr_err("pvr_sync: %s: Failed to allocate waiter\n", __func__);
       goto err_free_cleanup_sync;
   }
 
   waiter->kernel = kernel;
   waiter->sync_fence = sync_fence;
 
   err = dma_fence_add_callback(fence->fence, &waiter->waiter,
               pvr_sync_foreign_sync_pt_signaled);
   if (-ENOENT == err) {
       // V("'fence->fence' has been already signaled.");
       goto err_free_waiter;
   } else if (-EINVAL == err) {
       pr_err("pvr_sync_file: %s: failed to add callback to dma_fence, err: %d\n",
               __func__, err);
       goto err_free_waiter;
   }
 
   kernel->current_cleanup_sync = cleanup_sync;
 
err_out:
   return kernel;
err_free_waiter:
   kfree(waiter);
err_free_cleanup_sync:
   list_del(&cleanup_sync->cleanup_list);
   sync_pool_put(cleanup_sync);
err_free_sync:
   sync_pool_put(kernel->fence_sync);
err_free_sync_fence:
   kfree(sync_fence);
err_free_kernel:
   kfree(kernel);
   kernel = NULL;
err_put_fence:
   sync_file_put(fence);
   goto err_out;
}
 
static
struct pvr_sync_pt *pvr_sync_create_pt(struct pvr_sync_timeline *timeline)
{
   struct pvr_sync_data *sync_data;
   struct pvr_sync_pt *pvr_pt = NULL;
 
   sync_data = pvr_sync_create_sync_data(timeline->obj);
   if (!sync_data) {
       pr_err("pvr_sync: %s: Failed to create sync data\n", __func__);
       goto err_out;
   }
 
   sync_data->kernel->fence_sync->next_value++;
 
//Warning
   pvr_pt = (struct pvr_sync_pt *)
       pvr_sync_pt_create(timeline->obj, ++timeline->obj->value, sync_data);
 
   if (!pvr_pt) {
       pr_err("pvr_sync: %s: Failed to create sync pt\n", __func__);
       goto err_rollback_fence;
   }
 
   /* Increment the timeline next value */
   pvr_pt->sync_data->timeline_update_value =
       timeline->kernel->fence_sync->next_value++;
 
   return pvr_pt;
 
err_rollback_fence:
   sync_data->kernel->fence_sync->next_value--;
   kref_put(&sync_data->kref, pvr_sync_free_sync_data);
err_out:
   return NULL;
}
 
/* Predeclare the pvr_sync_fops as it's used for comparison to ensure the
 * update_timeline_fd passed in to pvr_sync_append_fences() is a pvr_sync
 * timeline.
 */
static const struct file_operations pvr_sync_fops;
 
enum PVRSRV_ERROR pvr_sync_append_fences(
   const char                *name,
   const s32                check_fence_fd,
   const s32                update_timeline_fd,
   const u32                nr_updates,
   const struct _RGXFWIF_DEV_VIRTADDR_    *update_ufo_addresses,
   const u32                *update_values,
   const u32                nr_checks,
   const struct _RGXFWIF_DEV_VIRTADDR_    *check_ufo_addresses,
   const u32                *check_values,
   struct pvr_sync_append_data        **append_sync_data)
{
   struct pvr_sync_native_sync_prim **cleanup_sync_pos;
   struct pvr_sync_pt *update_point = NULL;
   struct sync_file *update_fence = NULL;
   struct pvr_sync_append_data *sync_data;
   struct _RGXFWIF_DEV_VIRTADDR_ *update_address_pos;
   struct _RGXFWIF_DEV_VIRTADDR_ *check_address_pos;
   struct pvr_sync_timeline *timeline;
   unsigned int num_used_sync_updates;
   unsigned int num_used_sync_checks;
   enum PVRSRV_ERROR err = PVRSRV_OK;
   u32 *update_value_pos;
   u32 *check_value_pos;
 
   struct dma_fence **fences = NULL;
   unsigned int num_fences;
 
   if ((nr_updates && (!update_ufo_addresses || !update_values)) ||
       (nr_checks && (!check_ufo_addresses || !check_values))) {
       err =  PVRSRV_ERROR_INVALID_PARAMS;
       goto err_out;
   }
 
   sync_data =
       kzalloc(sizeof(*sync_data), GFP_KERNEL);
   if (!sync_data) {
       err = PVRSRV_ERROR_OUT_OF_MEMORY;
       goto err_out;
   }
 
   sync_data->update_fence_fd = -1;
 
   if (update_timeline_fd >= 0) {
       struct file *timeline_file;
 
       /* We reserve the update fence FD before taking any operations
        * as we do not want to fail (e.g. run out of FDs) after the
        * kick operation has been submitted to the hw.
        */
       sync_data->update_fence_fd = get_unused_fd();
       if (sync_data->update_fence_fd < 0) {
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free_append_data;
       }
 
       timeline_file = fget(update_timeline_fd);
       if (!timeline_file) {
           pr_err("pvr_sync: %s: Failed to open supplied timeline fd (%d)\n",
               __func__, update_timeline_fd);
           err = PVRSRV_ERROR_HANDLE_NOT_FOUND;
           goto err_free_append_data;
       }
 
       if (timeline_file->f_op != &pvr_sync_fops) {
           pr_err("pvr_sync: %s: Supplied timeline not pvr_sync timeline\n",
               __func__);
           fput(timeline_file);
           err = PVRSRV_ERROR_INVALID_PARAMS;
           goto err_free_append_data;
       }
 
       timeline = get_timeline(timeline_file->private_data);
 
       /* We know this will not free the timeline as the user still
        * has the fd referencing it.
        */
       fput(timeline_file);
 
       if (!timeline) {
           pr_err("pvr_sync: %s: Supplied timeline has no private data\n",
               __func__);
           err = PVRSRV_ERROR_HANDLE_NOT_FOUND;
           goto err_free_append_data;
       }
 
       update_point = pvr_sync_create_pt(timeline);
       if (!update_point) {
           printk("rk-debug pvr_sync: %s: Failed to create sync point\n",
               __func__);
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free_append_data;
       }
 
       //update_fence = sync_fence_create(name, &update_point->pt);
       update_fence = sync_file_create(&update_point->pt.base);
       dma_fence_put(&update_point->pt.base);
       if (!update_fence) {
           struct pvr_sync_native_sync_prim *fence_prim =
               update_point->sync_data->kernel->fence_sync;
           struct pvr_sync_native_sync_prim *timeline_prim =
               timeline->kernel->fence_sync;
 
           pr_err("pvr_sync: %s: Failed to create sync file\n",
               __func__);
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
 
           /* If the point was created but the fence failed to be
            * created, the point must be manually free'd as a
            * fence has not yet taken ownership.
            */
 
           /* First rollback the point's taken operations */
           timeline_prim->next_value--;
           fence_prim->next_value--;
           pvr_sync_free_sync(&update_point->pt);
           goto err_free_append_data;
       }
 
       sync_data->update_fence = update_fence;
       sync_data->update_sync =
           update_point->sync_data->kernel->fence_sync;
       sync_data->update_timeline_sync =
           timeline->kernel->fence_sync;
   }
 
   sync_data->nr_checks = nr_checks;
   sync_data->nr_updates = nr_updates;
 
   if (check_fence_fd >= 0) {
       struct sync_file *fence = sync_file_get(check_fence_fd); // check_fence
       struct pvr_sync_kernel_pair *sync_kernel;
       unsigned int points_on_fence = 0;
       bool has_foreign_point = false;
       // struct dma_fence_pt *sync_pt;
       struct dma_fence *dma_fence;
       int j;
 
       if (!fence) {
           pr_err("pvr_sync: %s: Failed to read sync private data for fd %d\n",
               __func__, check_fence_fd);
           err = PVRSRV_ERROR_HANDLE_NOT_FOUND;
           goto err_free_fence;
       }
 
       sync_data->check_fence = fence;
 
       /*-----------------------------------*/
 
       if (dma_fence_is_array(fence->fence)) {
           struct dma_fence_array *array = to_dma_fence_array(fence->fence);
           if (!array) {
               pr_err("%s: Failed to resolve dma fence array\n",
                      __func__);
           }
 
           fences = array->fences;
           num_fences = array->num_fences;
       } else {
           fences = &fence->fence;
           num_fences = 1;
       }
 
       (void)j;
       for (j = 0,
            dma_fence = ((num_fences == 0) ? NULL : fences[0]);
            j < num_fences;
            j++,
            dma_fence = ((j < num_fences) ? fences[j] : NULL)) {
           //for_each_sync_pt(sync_pt, fence, j) ...
           struct pvr_sync_native_sync_prim *cleanup_sync = NULL;
           struct dma_fence_pt *sync_pt;
           struct pvr_sync_pt *pvr_pt;
 
           // if (!is_dma_fence_pt(fences[j])) {
           if (!is_pvr_dma_fence(dma_fence)) {
               // if (!sync_pt_get_status(sync_pt))
               if (!dma_fence_is_signaled(dma_fence))
                   has_foreign_point = true;
               continue;
           }
 
           sync_pt = (struct dma_fence_pt *)dma_fence;
           pvr_pt = (struct pvr_sync_pt *)sync_pt;
           sync_kernel = pvr_pt->sync_data->kernel;
 
           if (!sync_kernel ||
               is_sync_met(sync_kernel->fence_sync)) {
               continue;
           }
 
           /* We will use the above sync for "check" only. In this
            * case also insert a "cleanup" update command into the
            * opengl stream. This can later be used for checking
            * if the sync prim could be freed.
            */
           err = sync_pool_get(&cleanup_sync,
               pvr_sync_pt_parent(&pvr_pt->pt)->name,
               SYNC_PT_CLEANUP_TYPE);
           if (err != PVRSRV_OK) {
               pr_err("pvr_sync: %s: Failed to allocate cleanup sync prim (%s)\n",
                      __func__,
                      PVRSRVGetErrorStringKM(err));
               goto err_free_append_data;
           }
           list_add(&cleanup_sync->cleanup_list,
               &sync_kernel->cleanup_sync_list);
           sync_kernel->current_cleanup_sync = cleanup_sync;
           points_on_fence++;
       }
 
       if (has_foreign_point)
           points_on_fence++;
 
       /* Each point has 1 check value, and 1 update value (for the
        * cleanup fence).
        */
       sync_data->nr_checks += points_on_fence;
       sync_data->nr_updates += points_on_fence;
       sync_data->nr_cleanup_syncs += points_on_fence;
   }
 
   if (update_point) {
       /* A fence update requires 2 update values (fence and timeline)
        */
        sync_data->nr_updates += 2;
   }
 
   if (sync_data->nr_updates > 0) {
       sync_data->update_ufo_addresses =
           kzalloc(sizeof(*sync_data->update_ufo_addresses) *
                   sync_data->nr_updates, GFP_KERNEL);
       if (!sync_data->update_ufo_addresses) {
           pr_err("pvr_sync: %s: Failed to allocate update UFO address list\n",
               __func__);
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free_fence;
       }
 
       sync_data->update_values =
           kzalloc(sizeof(*sync_data->update_values) *
               sync_data->nr_updates, GFP_KERNEL);
       if (!sync_data->update_values) {
           pr_err("pvr_sync: %s: Failed to allocate update value list\n",
               __func__);
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free_fence;
       }
   }
 
   if (sync_data->nr_checks > 0) {
 
       sync_data->check_ufo_addresses =
           kzalloc(sizeof(*sync_data->check_ufo_addresses) *
                   sync_data->nr_checks, GFP_KERNEL);
       if (!sync_data->check_ufo_addresses) {
           pr_err("pvr_sync: %s: Failed to allocate check UFO address list\n",
               __func__);
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free_fence;
       }
 
       sync_data->check_values =
           kzalloc(sizeof(*sync_data->check_values) *
               sync_data->nr_checks, GFP_KERNEL);
       if (!sync_data->check_values) {
           pr_err("pvr_sync: %s: Failed to allocate check value list\n",
               __func__);
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free_fence;
       }
   }
 
   if (sync_data->nr_cleanup_syncs > 0) {
       sync_data->cleanup_syncs =
           kzalloc(sizeof(*sync_data->cleanup_syncs) *
               sync_data->nr_cleanup_syncs, GFP_KERNEL);
       if (!sync_data->cleanup_syncs) {
           pr_err("pvr_sync: %s: Failed to allocate cleanup rollback list\n",
               __func__);
           err = PVRSRV_ERROR_OUT_OF_MEMORY;
           goto err_free_fence;
       }
   }
 
   update_address_pos = sync_data->update_ufo_addresses;
   update_value_pos = sync_data->update_values;
   check_address_pos = sync_data->check_ufo_addresses;
   check_value_pos = sync_data->check_values;
   cleanup_sync_pos = sync_data->cleanup_syncs;
 
   /* Everything should be allocated/sanity checked. No errors are
    * possible after this point.
    */
 
   /* Append any check syncs */
   if (sync_data->check_fence) {
       struct sync_file *fence = sync_data->check_fence;
       bool has_foreign_point = false;
       // struct dma_fence_pt *sync_pt;
       struct dma_fence *dma_fence;
       int j;
 
       if (dma_fence_is_array(fence->fence)) {
           struct dma_fence_array *array = to_dma_fence_array(fence->fence);
           if (!array) {
               pr_err("%s: Failed to resolve dma fence array\n",
                      __func__);
           }
 
           fences = array->fences;
           num_fences = array->num_fences;
       } else {
           fences = &fence->fence;
           num_fences = 1;
       }
 
       (void)j;
       for (j = 0,
            dma_fence = ((num_fences == 0) ? NULL : fences[0]);
            j < num_fences;
            j++,
            dma_fence = ((j < num_fences) ? fences[j] : NULL)) {
           //for_each_sync_pt(sync_pt, fence, j) ...
           struct dma_fence_pt *sync_pt;
           struct pvr_sync_pt *pvr_pt;
           struct pvr_sync_kernel_pair *sync_kernel;
 
           // if (!is_dma_fence_pt(fences[j])) {
           if (!is_pvr_dma_fence(dma_fence)) {
               // if (!sync_pt_get_status(sync_pt))
               if (!dma_fence_is_signaled(dma_fence))
                   has_foreign_point = true;
               continue;
           }
 
           sync_pt = (struct dma_fence_pt *)dma_fence;
           pvr_pt = (struct pvr_sync_pt *)sync_pt;
           sync_kernel = pvr_pt->sync_data->kernel;
 
           if (!sync_kernel ||
               is_sync_met(sync_kernel->fence_sync)) {
               continue;
           }
 
           (*check_address_pos++).ui32Addr =
               sync_kernel->fence_sync->vaddr;
           *check_value_pos++ =
               sync_kernel->fence_sync->next_value;
 
           (*update_address_pos++).ui32Addr =
               sync_kernel->current_cleanup_sync->vaddr;
           *update_value_pos++ =
               ++sync_kernel->current_cleanup_sync->next_value;
           *cleanup_sync_pos++ = sync_kernel->current_cleanup_sync;
 
           sync_kernel->current_cleanup_sync = NULL;
       }
 
       if (has_foreign_point) {
           struct pvr_sync_kernel_pair *foreign_sync_kernel =
               pvr_sync_create_waiter_for_foreign_sync(
                   check_fence_fd);
 
           if (foreign_sync_kernel) {
               struct pvr_sync_native_sync_prim *fence_sync =
                   foreign_sync_kernel->fence_sync;
               struct pvr_sync_native_sync_prim *cleanup_sync =
                   foreign_sync_kernel->
                       current_cleanup_sync;
 
               (*check_address_pos++).ui32Addr =
                   fence_sync->vaddr;
               *check_value_pos++ =
                   fence_sync->next_value;
 
               (*update_address_pos++).ui32Addr =
                   cleanup_sync->vaddr;
               *update_value_pos++ =
                   ++cleanup_sync->next_value;
               *cleanup_sync_pos++ = cleanup_sync;
               foreign_sync_kernel->current_cleanup_sync =
                   NULL;
           }
       }
   }
 
   /* Append the update sync (if requested) */
   if (update_point) {
       struct pvr_sync_data *sync_data =
           update_point->sync_data;
       struct pvr_sync_kernel_pair *sync_kernel =
           sync_data->kernel;
 
       (*update_address_pos++).ui32Addr =
           sync_kernel->fence_sync->vaddr;
       *update_value_pos++ =
           sync_kernel->fence_sync->next_value;
 
       (*update_address_pos++).ui32Addr =
           timeline->kernel->fence_sync->vaddr;
 
       /* Copy in the timeline next value (which was incremented
        * when this point was created).
        */
       sync_data->timeline_update_value =
           timeline->kernel->fence_sync->next_value;
 
       /* ...and set that to be updated when this kick is completed */
       *update_value_pos++ =
           sync_data->timeline_update_value;
   }
 
   /* We count the total number of sync points we attach, as it's possible
    * some have become complete since the first loop through, or a waiter
    * for a foreign point skipped (But they can never become un-complete,
    * so it will only ever be the same or less, so the allocated arrays
    * should still be sufficiently sized).
    */
   num_used_sync_updates =
       update_address_pos - sync_data->update_ufo_addresses;
   num_used_sync_checks =
       check_address_pos - sync_data->check_ufo_addresses;
 
   sync_data->nr_checks = nr_checks + num_used_sync_checks;
   sync_data->nr_updates = nr_updates + num_used_sync_updates;
 
   /* Append original check and update sync values/addresses */
   if (update_ufo_addresses)
       memcpy(update_address_pos, update_ufo_addresses,
           sizeof(*update_ufo_addresses) * nr_updates);
   if (update_values)
       memcpy(update_value_pos, update_values,
           sizeof(*update_values) * nr_updates);
 
   if (check_ufo_addresses)
       memcpy(check_address_pos, check_ufo_addresses,
           sizeof(*check_ufo_addresses) * nr_checks);
   if (check_values)
       memcpy(check_value_pos, check_values,
           sizeof(*check_values) * nr_checks);
 
   *append_sync_data = sync_data;
 
   return PVRSRV_OK;
 
err_free_fence:
   if (update_point) {
       /* First rollback the taken operations */
       timeline->kernel->fence_sync->next_value--;
       update_point->sync_data->kernel->fence_sync->next_value--;
   }
err_free_append_data:
   pvr_sync_free_append_fences_data(sync_data);
err_out:
   return err;
}
 
void pvr_sync_get_updates(const struct pvr_sync_append_data *sync_data,
   u32 *nr_fences, struct _RGXFWIF_DEV_VIRTADDR_ **ufo_addrs, u32 **values)
{
   *nr_fences = sync_data->nr_updates;
   *ufo_addrs = sync_data->update_ufo_addresses;
   *values = sync_data->update_values;
}
 
void pvr_sync_get_checks(const struct pvr_sync_append_data *sync_data,
   u32 *nr_fences, struct _RGXFWIF_DEV_VIRTADDR_ **ufo_addrs, u32 **values)
{
   *nr_fences = sync_data->nr_checks;
   *ufo_addrs = sync_data->check_ufo_addresses;
   *values = sync_data->check_values;
}
 
void pvr_sync_rollback_append_fences(struct pvr_sync_append_data *sync_data)
{
   u32 i;
 
   if (!sync_data)
       return;
 
   for (i = 0; i < sync_data->nr_cleanup_syncs; i++) {
       struct pvr_sync_native_sync_prim *cleanup_sync =
           sync_data->cleanup_syncs[i];
 
       /* If this cleanup was called on a partially-created data set
        * it's possible to have NULL cleanup sync pointers.
        */
       if (!cleanup_sync)
           continue;
       cleanup_sync->next_value--;
   }
 
   /* If there was an update, rollback the next values taken on the
    * fence and timeline. This must be done before the sync_fence_put()
    * as that may free the corresponding fence.
    */
 
   if (sync_data->update_sync) {
       BUG_ON(sync_data->update_sync->next_value != 1);
       sync_data->update_sync->next_value = 0;
       sync_data->update_sync = NULL;
   }
 
   if (sync_data->update_timeline_sync) {
       BUG_ON(sync_data->update_timeline_sync->next_value == 0);
       sync_data->update_timeline_sync->next_value--;
       sync_data->update_timeline_sync = NULL;
   }
}
 
int pvr_sync_get_update_fd(struct pvr_sync_append_data *sync_data)
{
   int fd = -EINVAL;
 
   if (!sync_data || !sync_data->update_fence ||
       sync_data->update_fence_fd < 0)
       goto err_out;
 
   fd = sync_data->update_fence_fd;
   sync_data->update_fence_fd = -1;
 
   pvr_sync_fence_install(sync_data->update_fence, fd);
 
   /* Note: It is invalid for an FD to have been installed on the update
    * fence then fput called - as this would leave a dangling reference
    * in the FD table. Set it to NULL so the free_append_fences_data()
    * call doesn't fput it.
    */
   sync_data->update_fence = NULL;
 
err_out:
   return fd;
}
 
void pvr_sync_free_append_fences_data(struct pvr_sync_append_data *sync_data)
{
   if (!sync_data)
       return;
 
   if (sync_data->check_fence)
       sync_file_put(sync_data->check_fence);
 
   if (sync_data->update_fence)
       sync_file_put(sync_data->update_fence);
 
   if (sync_data->update_fence_fd >= 0)
       put_unused_fd(sync_data->update_fence_fd);
 
   kfree(sync_data->update_ufo_addresses);
   kfree(sync_data->update_values);
   kfree(sync_data->check_ufo_addresses);
   kfree(sync_data->check_values);
   kfree(sync_data->cleanup_syncs);
   kfree(sync_data);
}
 
void pvr_sync_nohw_complete_fences(struct pvr_sync_append_data *sync_data)
{
   u32 i;
 
   if (!sync_data)
       return;
 
   for (i = 0; i < sync_data->nr_cleanup_syncs; i++) {
       struct pvr_sync_native_sync_prim *cleanup_sync =
           sync_data->cleanup_syncs[i];
 
       if (!cleanup_sync)
           continue;
 
       complete_sync(cleanup_sync);
   }
 
   if (sync_data->update_sync)
       complete_sync(sync_data->update_sync);
   if (sync_data->update_timeline_sync)
       complete_sync(sync_data->update_timeline_sync);
 
   pvr_sync_update_all_timelines(NULL);
}
 
/* ioctl and fops handling */
 
static int pvr_sync_open(struct inode *inode, struct file *file)
{
   struct pvr_sync_timeline_wrapper *timeline_wrapper;
   struct pvr_sync_timeline *timeline;
   char task_comm[TASK_COMM_LEN];
   enum PVRSRV_ERROR error;
   int err = -ENOMEM;
 
   get_task_comm(task_comm, current);
 
//Warning
   timeline_wrapper = (struct pvr_sync_timeline_wrapper *)
       pvr_sync_timeline_create(task_comm, sizeof(*timeline_wrapper), &pvr_fence_ops);
   if (!timeline_wrapper) {
       pr_err("pvr_sync: %s: pvr_sync_timeline_create failed\n", __func__);
       goto err_out;
   }
 
   timeline = kmalloc(sizeof(*timeline), GFP_KERNEL);
   if (!timeline) {
       pr_err("pvr_sync: %s: Out of memory\n", __func__);
       goto err_free_timeline_wrapper;
   }
 
   timeline->kernel = kzalloc(sizeof(*timeline->kernel),
                  GFP_KERNEL);
   if (!timeline->kernel) {
       pr_err("pvr_sync: %s: Out of memory\n", __func__);
       goto err_free_timeline;
   }
 
   INIT_LIST_HEAD(&timeline->kernel->cleanup_sync_list);
 
   OSAcquireBridgeLock();
   error = sync_pool_get(&timeline->kernel->fence_sync,
                 task_comm, SYNC_TL_TYPE);
   OSReleaseBridgeLock();
 
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to allocate sync prim (%s)\n",
           __func__, PVRSRVGetErrorStringKM(error));
       goto err_free_timeline_kernel;
   }
 
   timeline_wrapper->timeline = timeline;
 
   timeline->obj = &timeline_wrapper->obj;
   kref_init(&timeline->kref);
 
   mutex_lock(&timeline_list_mutex);
   list_add_tail(&timeline->list, &timeline_list);
   mutex_unlock(&timeline_list_mutex);
 
   DPF("%s: # %s", __func__, debug_info_timeline(timeline));
 
   file->private_data = timeline_wrapper;
   err = 0;
err_out:
   return err;
 
err_free_timeline_kernel:
   kfree(timeline->kernel);
err_free_timeline:
   kfree(timeline);
 
   /* Use a NULL timeline to detect this partially-setup timeline in the
    * timeline release function (called by sync_timeline_destroy) and
    * handle it appropriately.
    */
   timeline_wrapper->timeline = NULL;
err_free_timeline_wrapper:
   pvr_sync_timeline_destroy(&timeline_wrapper->obj);
   goto err_out;
}
 
static int pvr_sync_close(struct inode *inode, struct file *file)
{
   struct dma_fence_timeline *obj = file->private_data;
 
   if (is_pvr_timeline(obj)) {
       DPF("%s: # %s", __func__,
           debug_info_timeline(get_timeline(obj)));
   }
 
   pvr_sync_timeline_destroy(obj);
   return 0;
}
 
static long pvr_sync_ioctl_rename(struct pvr_sync_timeline *timeline,
   void __user *user_data)
{
   int err = 0;
   struct pvr_sync_rename_ioctl_data data;
 
   if (!access_ok(VERIFY_READ, user_data, sizeof(data))) {
       err = -EFAULT;
       goto err;
   }
 
   if (copy_from_user(&data, user_data, sizeof(data))) {
       err = -EFAULT;
       goto err;
   }
 
   data.szName[sizeof(data.szName) - 1] = '\0';
   strlcpy(timeline->obj->name, data.szName, sizeof(timeline->obj->name));
 
   mutex_lock(&sync_pool_mutex);
   strlcpy(timeline->kernel->fence_sync->class, data.szName,
       sizeof(timeline->kernel->fence_sync->class));
   mutex_unlock(&sync_pool_mutex);
err:
   return err;
}
 
static long
pvr_sync_ioctl(struct file *file, unsigned int cmd, unsigned long __user arg)
{
   struct dma_fence_timeline *obj = file->private_data;
   void __user *user_data = (void __user *)arg;
   long err = -ENOTTY;
 
   if (is_pvr_timeline(obj)) {
       struct pvr_sync_timeline *pvr = get_timeline(obj);
 
       switch (cmd) {
       case PVR_SYNC_IOC_RENAME:
           err = pvr_sync_ioctl_rename(pvr, user_data);
           break;
       default:
           break;
       }
   }
 
   return err;
}
 
static void
pvr_sync_check_status_work_queue_function(struct work_struct *data)
{
   /* A completed SW operation may un-block the GPU */
   PVRSRVCheckStatus(NULL);
}
 
/* Returns true if the freelist still has entries, else false if empty */
static bool
pvr_sync_clean_freelist(void)
{
   struct pvr_sync_kernel_pair *kernel, *k;
   struct pvr_sync_fence *sync_fence, *f;
   LIST_HEAD(unlocked_free_list);
   unsigned long flags;
   bool freelist_empty;
 
   /* We can't call PVRSRVServerSyncFreeKM directly in this loop because
    * that will take the mmap mutex. We can't take mutexes while we have
    * this list locked with a spinlock. So move all the items we want to
    * free to another, local list (no locking required) and process it
    * in a second loop.
    */
 
   spin_lock_irqsave(&sync_prim_free_list_spinlock, flags);
   list_for_each_entry_safe(kernel, k, &sync_prim_free_list, list) {
       bool in_use = false;
       struct list_head *pos;
 
       /* Check if this sync is not used anymore. */
       if (!is_sync_met(kernel->fence_sync))
           continue;
       list_for_each(pos, &kernel->cleanup_sync_list) {
           struct pvr_sync_native_sync_prim *cleanup_sync =
               list_entry(pos,
                   struct pvr_sync_native_sync_prim,
                   cleanup_list);
 
           if (!is_sync_met(cleanup_sync)) {
               in_use = true;
               break;
           }
       }
 
       if (in_use)
           continue;
 
       /* Remove the entry from the free list. */
       list_move_tail(&kernel->list, &unlocked_free_list);
   }
 
   /* Wait and loop if there are still syncs on the free list (IE
    * are still in use by the HW).
    */
   freelist_empty = list_empty(&sync_prim_free_list);
 
   spin_unlock_irqrestore(&sync_prim_free_list_spinlock, flags);
 
   OSAcquireBridgeLock();
 
   list_for_each_entry_safe(kernel, k, &unlocked_free_list, list) {
       struct list_head *pos, *n;
 
       list_del(&kernel->list);
 
       sync_pool_put(kernel->fence_sync);
 
       list_for_each_safe(pos, n, &kernel->cleanup_sync_list) {
           struct pvr_sync_native_sync_prim *cleanup_sync =
               list_entry(pos,
                   struct pvr_sync_native_sync_prim,
                    cleanup_list);
           list_del(&cleanup_sync->cleanup_list);
           sync_pool_put(cleanup_sync);
       }
       kfree(kernel);
   }
 
   OSReleaseBridgeLock();
 
   /* sync_fence_put() must be called from process/WQ context
    * because it uses fput(), which is not allowed to be called
    * from interrupt context in kernels <3.6.
    */
   INIT_LIST_HEAD(&unlocked_free_list);
 
   spin_lock_irqsave(&sync_fence_put_list_spinlock, flags);
   list_for_each_entry_safe(sync_fence, f, &sync_fence_put_list, list) {
       list_move_tail(&sync_fence->list, &unlocked_free_list);
   }
   spin_unlock_irqrestore(&sync_fence_put_list_spinlock, flags);
 
   list_for_each_entry_safe(sync_fence, f, &unlocked_free_list, list) {
       list_del(&sync_fence->list);
       sync_file_put(sync_fence->fence);
       kfree(sync_fence);
   }
 
   return !freelist_empty;
}
 
static void
pvr_sync_defer_free_work_queue_function(struct work_struct *data)
{
   enum PVRSRV_ERROR error = PVRSRV_OK;
   void *event_object;
 
   error = OSEventObjectOpen(pvr_sync_data.event_object_handle,
       &event_object);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Error opening event object (%s)\n",
           __func__, PVRSRVGetErrorStringKM(error));
       return;
 
   }
 
   while (pvr_sync_clean_freelist()) {
 
       error = OSEventObjectWait(event_object);
 
       switch (error) {
 
       case PVRSRV_OK:
       case PVRSRV_ERROR_TIMEOUT:
           /* Timeout is normal behaviour */
           continue;
       default:
           pr_err("pvr_sync: %s: Error waiting for event object (%s)\n",
               __func__, PVRSRVGetErrorStringKM(error));
           break;
       }
   }
   error = OSEventObjectClose(event_object);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Error closing event object (%s)\n",
           __func__, PVRSRVGetErrorStringKM(error));
   }
}
 
static const struct file_operations pvr_sync_fops = {
   .owner          = THIS_MODULE,
   .open           = pvr_sync_open,
   .release        = pvr_sync_close,
   .unlocked_ioctl = pvr_sync_ioctl,
   .compat_ioctl   = pvr_sync_ioctl,
};
 
static struct miscdevice pvr_sync_device = {
   .minor          = MISC_DYNAMIC_MINOR,
   .name           = PVRSYNC_MODNAME,
   .fops           = &pvr_sync_fops,
};
 
static
void pvr_sync_update_all_timelines(void *command_complete_handle)
{
   struct pvr_sync_timeline *timeline, *n;
 
   mutex_lock(&timeline_list_mutex);
 
   list_for_each_entry(timeline, &timeline_list, list) {
       /* If a timeline is destroyed via pvr_sync_release_timeline()
        * in parallel with a call to pvr_sync_update_all_timelines(),
        * the timeline_list_mutex will block destruction of the
        * 'timeline' pointer. Use kref_get_unless_zero() to detect
        * and handle this race. Skip the timeline if it's being
        * destroyed, blocked only on the timeline_list_mutex.
        */
       timeline->valid =
           kref_get_unless_zero(&timeline->kref) ? true : false;
   }
 
   list_for_each_entry_safe(timeline, n, &timeline_list, list) {
       /* We know timeline is valid at this point because we're
        * holding the list lock (so pvr_sync_destroy_timeline() has
        * to wait).
        */
       void *obj = timeline->obj;
 
       /* If we're racing with pvr_sync_release_timeline(), ignore */
       if (!timeline->valid)
           continue;
 
       /* If syncs have signaled on the GPU, echo this in pvr_sync.
        *
        * At this point we know the timeline is valid, but obj might
        * have raced and been set to NULL. It's only important that
        * we use NULL / non-NULL consistently with the if() and call
        * to sync_timeline_signal() -- the timeline->obj can't be
        * freed (pvr_sync_release_timeline() will be stuck waiting
        * for the timeline_list_mutex) but it might have been made
        * invalid by the base sync driver, in which case this call
        * will bounce harmlessly.
        */
       if (obj)
           pvr_sync_timeline_signal(obj);
 
       /* We're already holding the timeline_list_mutex */
       kref_put(&timeline->kref, pvr_sync_destroy_timeline_locked);
   }
 
   mutex_unlock(&timeline_list_mutex);
}
 
enum PVRSRV_ERROR pvr_sync_init(void *device_cookie)
{
   enum PVRSRV_ERROR error;
   int err;
 
   DPF("%s", __func__);
 
   atomic_set(&pvr_sync_data.sync_id, 0);
 
   error = PVRSRVAcquireGlobalEventObjectKM(
       &pvr_sync_data.event_object_handle);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to acquire global event object (%s)\n",
           __func__, PVRSRVGetErrorStringKM(error));
       goto err_out;
   }
 
   OSAcquireBridgeLock();
 
   error = SyncPrimContextCreate(device_cookie,
                     &pvr_sync_data.sync_prim_context);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to create sync prim context (%s)\n",
              __func__, PVRSRVGetErrorStringKM(error));
       OSReleaseBridgeLock();
       goto err_release_event_object;
   }
 
   OSReleaseBridgeLock();
 
   pvr_sync_data.defer_free_wq =
       create_freezable_workqueue("pvr_sync_defer_free_workqueue");
   if (!pvr_sync_data.defer_free_wq) {
       pr_err("pvr_sync: %s: Failed to create pvr_sync defer_free workqueue\n",
              __func__);
       goto err_free_sync_context;
   }
 
   INIT_WORK(&pvr_sync_data.defer_free_work,
       pvr_sync_defer_free_work_queue_function);
 
   pvr_sync_data.check_status_wq =
       create_freezable_workqueue("pvr_sync_check_status_workqueue");
   if (!pvr_sync_data.check_status_wq) {
       pr_err("pvr_sync: %s: Failed to create pvr_sync check_status workqueue\n",
              __func__);
       goto err_destroy_defer_free_wq;
   }
 
   INIT_WORK(&pvr_sync_data.check_status_work,
       pvr_sync_check_status_work_queue_function);
   error = PVRSRVRegisterCmdCompleteNotify(
           &pvr_sync_data.command_complete_handle,
           &pvr_sync_update_all_timelines,
           &device_cookie);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to register MISR notification (%s)\n",
              __func__, PVRSRVGetErrorStringKM(error));
       goto err_destroy_status_wq;
   }
 
   error = PVRSRVRegisterDbgRequestNotify(
           &pvr_sync_data.debug_notify_handle,
           device_cookie,
           pvr_sync_debug_request,
           DEBUG_REQUEST_ANDROIDSYNC,
           NULL);
   if (error != PVRSRV_OK) {
       pr_err("pvr_sync: %s: Failed to register debug notifier (%s)\n",
           __func__, PVRSRVGetErrorStringKM(error));
       goto err_unregister_cmd_complete;
   }
 
   err = misc_register(&pvr_sync_device);
   if (err) {
       pr_err("pvr_sync: %s: Failed to register pvr_sync device (%d)\n",
              __func__, err);
       error = PVRSRV_ERROR_RESOURCE_UNAVAILABLE;
       goto err_unregister_dbg;
   }
 
   error = PVRSRV_OK;
   return error;
 
err_unregister_dbg:
   PVRSRVUnregisterDbgRequestNotify(pvr_sync_data.debug_notify_handle);
err_unregister_cmd_complete:
   PVRSRVUnregisterCmdCompleteNotify(
       pvr_sync_data.command_complete_handle);
err_destroy_status_wq:
   destroy_workqueue(pvr_sync_data.check_status_wq);
err_destroy_defer_free_wq:
   destroy_workqueue(pvr_sync_data.defer_free_wq);
err_free_sync_context:
   OSAcquireBridgeLock();
   SyncPrimContextDestroy(pvr_sync_data.sync_prim_context);
   OSReleaseBridgeLock();
err_release_event_object:
   PVRSRVReleaseGlobalEventObjectKM(pvr_sync_data.event_object_handle);
err_out:
 
   return error;
}
 
void pvr_sync_deinit(void)
{
   DPF("%s", __func__);
 
   misc_deregister(&pvr_sync_device);
 
   PVRSRVUnregisterDbgRequestNotify(pvr_sync_data.debug_notify_handle);
 
   PVRSRVUnregisterCmdCompleteNotify(
       pvr_sync_data.command_complete_handle);
 
   /* This will drain the workqueue, so we guarantee that all deferred
    * syncs are free'd before returning.
    */
   destroy_workqueue(pvr_sync_data.defer_free_wq);
   destroy_workqueue(pvr_sync_data.check_status_wq);
 
   OSAcquireBridgeLock();
 
   sync_pool_clear();
 
   SyncPrimContextDestroy(pvr_sync_data.sync_prim_context);
 
   OSReleaseBridgeLock();
 
   PVRSRVReleaseGlobalEventObjectKM(pvr_sync_data.event_object_handle);
}