hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
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
/*
 * Copyright (C) 2013-2018 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <linux/file.h>
#include "mali_timeline.h"
#include "mali_kernel_common.h"
#include "mali_scheduler.h"
#include "mali_soft_job.h"
#include "mali_timeline_fence_wait.h"
#include "mali_timeline_sync_fence.h"
#include "mali_executor.h"
#include "mali_pp_job.h"
 
#define MALI_TIMELINE_SYSTEM_LOCKED(system) (mali_spinlock_reentrant_is_held((system)->spinlock, _mali_osk_get_tid()))
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
_mali_osk_wq_work_t *sync_fence_callback_work_t = NULL;
_mali_osk_spinlock_irq_t *sync_fence_callback_list_lock = NULL;
static _MALI_OSK_LIST_HEAD_STATIC_INIT(sync_fence_callback_queue);
#endif
 
/*
 * Following three elements are used to record how many
 * gp, physical pp or virtual pp jobs are delayed in the whole
 * timeline system, we can use these three value to decide
 * if need to deactivate idle group.
 */
_mali_osk_atomic_t gp_tracker_count;
_mali_osk_atomic_t phy_pp_tracker_count;
_mali_osk_atomic_t virt_pp_tracker_count;
 
static mali_scheduler_mask mali_timeline_system_release_waiter(struct mali_timeline_system *system,
       struct mali_timeline_waiter *waiter);
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
#include <linux/list.h>
#include <linux/workqueue.h>
#include <linux/spinlock.h>
 
struct mali_deferred_fence_put_entry {
   struct hlist_node list;
   struct sync_fence *fence;
};
 
static HLIST_HEAD(mali_timeline_sync_fence_to_free_list);
static DEFINE_SPINLOCK(mali_timeline_sync_fence_to_free_lock);
 
static void put_sync_fences(struct work_struct *ignore)
{
   struct hlist_head list;
   struct hlist_node *tmp, *pos;
   unsigned long flags;
   struct mali_deferred_fence_put_entry *o;
 
   spin_lock_irqsave(&mali_timeline_sync_fence_to_free_lock, flags);
   hlist_move_list(&mali_timeline_sync_fence_to_free_list, &list);
   spin_unlock_irqrestore(&mali_timeline_sync_fence_to_free_lock, flags);
 
   hlist_for_each_entry_safe(o, pos, tmp, &list, list) {
       sync_fence_put(o->fence);
       kfree(o);
   }
}
 
static DECLARE_DELAYED_WORK(delayed_sync_fence_put, put_sync_fences);
#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0) */
 
/* Callback that is called when a sync fence a tracker is waiting on is signaled. */
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
static void mali_timeline_sync_fence_callback(struct sync_fence *sync_fence, struct sync_fence_waiter *sync_fence_waiter)
#else
static void mali_timeline_sync_fence_callback(struct mali_internal_sync_fence *sync_fence, struct mali_internal_sync_fence_waiter *sync_fence_waiter)
#endif
{
   struct mali_timeline_tracker *tracker;
 
   MALI_IGNORE(sync_fence);
   MALI_DEBUG_ASSERT_POINTER(sync_fence_waiter);
 
   tracker = _MALI_OSK_CONTAINER_OF(sync_fence_waiter, struct mali_timeline_tracker, sync_fence_waiter);
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   _mali_osk_spinlock_irq_lock(sync_fence_callback_list_lock);
   _mali_osk_list_addtail(&tracker->sync_fence_signal_list, &sync_fence_callback_queue);
   _mali_osk_spinlock_irq_unlock(sync_fence_callback_list_lock);
 
   _mali_osk_wq_schedule_work(sync_fence_callback_work_t);
}
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
static mali_scheduler_mask mali_timeline_tracker_time_out(struct mali_timeline_tracker *tracker)
{
   MALI_DEBUG_ASSERT_POINTER(tracker);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_TRACKER_SOFT == tracker->type);
 
   return mali_soft_job_system_timeout_job((struct mali_soft_job *) tracker->job);
}
 
static void mali_timeline_timer_callback(void *data)
{
   struct mali_timeline_system *system;
   struct mali_timeline_tracker *tracker;
   struct mali_timeline *timeline;
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
   u32 tid = _mali_osk_get_tid();
 
   timeline = (struct mali_timeline *) data;
   MALI_DEBUG_ASSERT_POINTER(timeline);
 
   system = timeline->system;
   MALI_DEBUG_ASSERT_POINTER(system);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   if (!system->timer_enabled) {
       mali_spinlock_reentrant_signal(system->spinlock, tid);
       return;
   }
 
   tracker = timeline->tracker_tail;
   timeline->timer_active = MALI_FALSE;
 
   if (NULL != tracker && MALI_TRUE == tracker->timer_active) {
       /* This is likely the delayed work that has been schedule out before cancelled. */
       if (MALI_TIMELINE_TIMEOUT_HZ > (_mali_osk_time_tickcount() - tracker->os_tick_activate)) {
           mali_spinlock_reentrant_signal(system->spinlock, tid);
           return;
       }
 
       schedule_mask = mali_timeline_tracker_time_out(tracker);
       tracker->timer_active = MALI_FALSE;
   } else {
       MALI_PRINT_ERROR(("Mali Timeline: Soft job timer callback without a waiting tracker.\n"));
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   mali_executor_schedule_from_mask(schedule_mask, MALI_FALSE);
}
 
void mali_timeline_system_stop_timer(struct mali_timeline_system *system)
{
   u32 i;
   u32 tid = _mali_osk_get_tid();
 
   MALI_DEBUG_ASSERT_POINTER(system);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
   system->timer_enabled = MALI_FALSE;
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       struct mali_timeline *timeline = system->timelines[i];
 
       MALI_DEBUG_ASSERT_POINTER(timeline);
 
       if (NULL != timeline->delayed_work) {
           _mali_osk_wq_delayed_cancel_work_sync(timeline->delayed_work);
           timeline->timer_active = MALI_FALSE;
       }
   }
}
 
static void mali_timeline_destroy(struct mali_timeline *timeline)
{
   MALI_DEBUG_ASSERT_POINTER(timeline);
   if (NULL != timeline) {
       /* Assert that the timeline object has been properly cleaned up before destroying it. */
       MALI_DEBUG_ASSERT(timeline->point_oldest == timeline->point_next);
       MALI_DEBUG_ASSERT(NULL == timeline->tracker_head);
       MALI_DEBUG_ASSERT(NULL == timeline->tracker_tail);
       MALI_DEBUG_ASSERT(NULL == timeline->waiter_head);
       MALI_DEBUG_ASSERT(NULL == timeline->waiter_tail);
       MALI_DEBUG_ASSERT(NULL != timeline->system);
       MALI_DEBUG_ASSERT(MALI_TIMELINE_MAX > timeline->id);
 
       if (NULL != timeline->delayed_work) {
           _mali_osk_wq_delayed_cancel_work_sync(timeline->delayed_work);
           _mali_osk_wq_delayed_delete_work_nonflush(timeline->delayed_work);
       }
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
       if (NULL != timeline->sync_tl) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
           sync_timeline_destroy(timeline->sync_tl);
#else
           mali_internal_sync_timeline_destroy(timeline->sync_tl);
#endif
       }
#else
       _mali_osk_free(timeline);
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
   }
}
 
static struct mali_timeline *mali_timeline_create(struct mali_timeline_system *system, enum mali_timeline_id id)
{
   struct mali_timeline *timeline;
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT(id < MALI_TIMELINE_MAX);
 
   timeline = (struct mali_timeline *) _mali_osk_calloc(1, sizeof(struct mali_timeline));
   if (NULL == timeline) {
       return NULL;
   }
 
   /* Initially the timeline is empty. */
#if defined(MALI_TIMELINE_DEBUG_START_POINT)
   /* Start the timeline a bit before wrapping when debugging. */
   timeline->point_next = UINT_MAX - MALI_TIMELINE_MAX_POINT_SPAN - 128;
#else
   timeline->point_next = 1;
#endif
   timeline->point_oldest = timeline->point_next;
 
   /* The tracker and waiter lists will initially be empty. */
 
   timeline->system = system;
   timeline->id = id;
 
   timeline->delayed_work = _mali_osk_wq_delayed_create_work(mali_timeline_timer_callback, timeline);
   if (NULL == timeline->delayed_work) {
       mali_timeline_destroy(timeline);
       return NULL;
   }
 
   timeline->timer_active = MALI_FALSE;
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   {
       char timeline_name[32];
 
       switch (id) {
       case MALI_TIMELINE_GP:
           _mali_osk_snprintf(timeline_name, 32, "mali-%u-gp", _mali_osk_get_pid());
           break;
       case MALI_TIMELINE_PP:
           _mali_osk_snprintf(timeline_name, 32, "mali-%u-pp", _mali_osk_get_pid());
           break;
       case MALI_TIMELINE_SOFT:
           _mali_osk_snprintf(timeline_name, 32, "mali-%u-soft", _mali_osk_get_pid());
           break;
       default:
           MALI_PRINT_ERROR(("Mali Timeline: Invalid timeline id %d\n", id));
           mali_timeline_destroy(timeline);
           return NULL;
       }
 
       timeline->destroyed = MALI_FALSE;
 
       timeline->sync_tl = mali_sync_timeline_create(timeline, timeline_name);
       if (NULL == timeline->sync_tl) {
           mali_timeline_destroy(timeline);
           return NULL;
       }
 
       timeline->spinlock = mali_spinlock_reentrant_init(_MALI_OSK_LOCK_ORDER_TIMELINE_SYSTEM);
       if (NULL == timeline->spinlock) {
           mali_timeline_destroy(timeline);
           return NULL;
       }
   }
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
   return timeline;
}
 
static void mali_timeline_insert_tracker(struct mali_timeline *timeline, struct mali_timeline_tracker *tracker)
{
   MALI_DEBUG_ASSERT_POINTER(timeline);
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   if (mali_timeline_is_full(timeline)) {
       /* Don't add tracker if timeline is full. */
       tracker->point = MALI_TIMELINE_NO_POINT;
       return;
   }
 
   tracker->timeline = timeline;
   tracker->point    = timeline->point_next;
 
   /* Find next available point. */
   timeline->point_next++;
   if (MALI_TIMELINE_NO_POINT == timeline->point_next) {
       timeline->point_next++;
   }
 
   MALI_DEBUG_ASSERT(!mali_timeline_is_empty(timeline));
 
   if (MALI_TIMELINE_TRACKER_GP == tracker->type) {
       _mali_osk_atomic_inc(&gp_tracker_count);
   } else if (MALI_TIMELINE_TRACKER_PP == tracker->type) {
       if (mali_pp_job_is_virtual((struct mali_pp_job *)tracker->job)) {
           _mali_osk_atomic_inc(&virt_pp_tracker_count);
       } else {
           _mali_osk_atomic_inc(&phy_pp_tracker_count);
       }
   }
 
   /* Add tracker as new head on timeline's tracker list. */
   if (NULL == timeline->tracker_head) {
       /* Tracker list is empty. */
       MALI_DEBUG_ASSERT(NULL == timeline->tracker_tail);
 
       timeline->tracker_tail = tracker;
 
       MALI_DEBUG_ASSERT(NULL == tracker->timeline_next);
       MALI_DEBUG_ASSERT(NULL == tracker->timeline_prev);
   } else {
       MALI_DEBUG_ASSERT(NULL == timeline->tracker_head->timeline_next);
 
       tracker->timeline_prev = timeline->tracker_head;
       timeline->tracker_head->timeline_next = tracker;
 
       MALI_DEBUG_ASSERT(NULL == tracker->timeline_next);
   }
   timeline->tracker_head = tracker;
 
   MALI_DEBUG_ASSERT(NULL == timeline->tracker_head->timeline_next);
   MALI_DEBUG_ASSERT(NULL == timeline->tracker_tail->timeline_prev);
}
 
/* Inserting the waiter object into the given timeline */
static void mali_timeline_insert_waiter(struct mali_timeline *timeline, struct mali_timeline_waiter *waiter_new)
{
   struct mali_timeline_waiter *waiter_prev;
   struct mali_timeline_waiter *waiter_next;
 
   /* Waiter time must be between timeline head and tail, and there must
    * be less than MALI_TIMELINE_MAX_POINT_SPAN elements between */
   MALI_DEBUG_ASSERT((waiter_new->point - timeline->point_oldest) < MALI_TIMELINE_MAX_POINT_SPAN);
   MALI_DEBUG_ASSERT((-waiter_new->point + timeline->point_next) < MALI_TIMELINE_MAX_POINT_SPAN);
 
   /* Finding out where to put this waiter, in the linked waiter list of the given timeline **/
   waiter_prev = timeline->waiter_head; /* Insert new after  waiter_prev */
   waiter_next = NULL;                  /* Insert new before waiter_next */
 
   /* Iterating backwards from head (newest) to tail (oldest) until we
    * find the correct spot to insert the new waiter */
   while (waiter_prev && mali_timeline_point_after(waiter_prev->point, waiter_new->point)) {
       waiter_next = waiter_prev;
       waiter_prev = waiter_prev->timeline_prev;
   }
 
   if (NULL == waiter_prev && NULL == waiter_next) {
       /* list is empty */
       timeline->waiter_head = waiter_new;
       timeline->waiter_tail = waiter_new;
   } else if (NULL == waiter_next) {
       /* insert at head */
       waiter_new->timeline_prev = timeline->waiter_head;
       timeline->waiter_head->timeline_next = waiter_new;
       timeline->waiter_head = waiter_new;
   } else if (NULL == waiter_prev) {
       /* insert at tail */
       waiter_new->timeline_next = timeline->waiter_tail;
       timeline->waiter_tail->timeline_prev = waiter_new;
       timeline->waiter_tail = waiter_new;
   } else {
       /* insert between */
       waiter_new->timeline_next = waiter_next;
       waiter_new->timeline_prev = waiter_prev;
       waiter_next->timeline_prev = waiter_new;
       waiter_prev->timeline_next = waiter_new;
   }
}
 
static void mali_timeline_update_delayed_work(struct mali_timeline *timeline)
{
   struct mali_timeline_system *system;
   struct mali_timeline_tracker *oldest_tracker;
 
   MALI_DEBUG_ASSERT_POINTER(timeline);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SOFT == timeline->id);
 
   system = timeline->system;
   MALI_DEBUG_ASSERT_POINTER(system);
 
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   /* Timer is disabled, early out. */
   if (!system->timer_enabled) return;
 
   oldest_tracker = timeline->tracker_tail;
   if (NULL != oldest_tracker && 0 == oldest_tracker->trigger_ref_count) {
       if (MALI_FALSE == oldest_tracker->timer_active) {
           if (MALI_TRUE == timeline->timer_active) {
               _mali_osk_wq_delayed_cancel_work_async(timeline->delayed_work);
           }
           _mali_osk_wq_delayed_schedule_work(timeline->delayed_work, MALI_TIMELINE_TIMEOUT_HZ);
           oldest_tracker->timer_active = MALI_TRUE;
           timeline->timer_active = MALI_TRUE;
       }
   } else if (MALI_TRUE == timeline->timer_active) {
       _mali_osk_wq_delayed_cancel_work_async(timeline->delayed_work);
       timeline->timer_active = MALI_FALSE;
   }
}
 
static mali_scheduler_mask mali_timeline_update_oldest_point(struct mali_timeline *timeline)
{
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
 
   MALI_DEBUG_ASSERT_POINTER(timeline);
 
   MALI_DEBUG_CODE({
       struct mali_timeline_system *system = timeline->system;
       MALI_DEBUG_ASSERT_POINTER(system);
 
       MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
   });
 
   if (NULL != timeline->tracker_tail) {
       /* Set oldest point to oldest tracker's point */
       timeline->point_oldest = timeline->tracker_tail->point;
   } else {
       /* No trackers, mark point list as empty */
       timeline->point_oldest = timeline->point_next;
   }
 
   /* Release all waiters no longer on the timeline's point list.
    * Releasing a waiter can trigger this function to be called again, so
    * we do not store any pointers on stack. */
   while (NULL != timeline->waiter_tail) {
       u32 waiter_time_relative;
       u32 time_head_relative;
       struct mali_timeline_waiter *waiter = timeline->waiter_tail;
 
       time_head_relative = timeline->point_next - timeline->point_oldest;
       waiter_time_relative = waiter->point - timeline->point_oldest;
 
       if (waiter_time_relative < time_head_relative) {
           /* This and all following waiters are on the point list, so we are done. */
           break;
       }
 
       /* Remove waiter from timeline's waiter list. */
       if (NULL != waiter->timeline_next) {
           waiter->timeline_next->timeline_prev = NULL;
       } else {
           /* This was the last waiter */
           timeline->waiter_head = NULL;
       }
       timeline->waiter_tail = waiter->timeline_next;
 
       /* Release waiter.  This could activate a tracker, if this was
        * the last waiter for the tracker. */
       schedule_mask |= mali_timeline_system_release_waiter(timeline->system, waiter);
   }
 
   return schedule_mask;
}
 
static mali_scheduler_mask mali_timeline_release_with_depended_point(struct mali_timeline_tracker *tracker)
{
   struct mali_timeline *timeline;
   struct mali_timeline_waiter *waiter;
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
 
   timeline = tracker->timeline;
   MALI_DEBUG_ASSERT_POINTER(timeline);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SOFT == timeline->id);
 
   MALI_DEBUG_CODE({
       struct mali_timeline_system *system = timeline->system;
       MALI_DEBUG_ASSERT_POINTER(system);
 
       MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
   });
 
   /* Only release the waiter that wait for the tracker. */
   waiter = timeline->waiter_tail;
   while (NULL != waiter) {
       if (waiter->point == tracker->point) {
 
           struct mali_timeline_waiter *waiter_next;
           struct mali_timeline_waiter *waiter_prev;
 
           waiter_next = waiter->timeline_next;
           waiter_prev = waiter->timeline_prev;
           waiter->timeline_next = NULL;
           waiter->timeline_prev = NULL;
 
           if (NULL != waiter_prev) {
               waiter_prev->timeline_next = waiter_next;
           }
 
           if (NULL != waiter_next) {
               waiter_next->timeline_prev = waiter_prev;
           }
 
           if (waiter ==  timeline->waiter_tail)
                timeline->waiter_tail = waiter_next;
 
           if (waiter == timeline->waiter_head)
               timeline->waiter_head = NULL;
 
           schedule_mask |= mali_timeline_system_release_waiter(timeline->system, waiter);
           waiter = waiter_next;
       }else {
 
           waiter = waiter->timeline_next;
       }
   }
 
   return schedule_mask;
}
 
void mali_timeline_tracker_init(struct mali_timeline_tracker *tracker,
               mali_timeline_tracker_type type,
               struct mali_timeline_fence *fence,
               void *job)
{
   MALI_DEBUG_ASSERT_POINTER(tracker);
   MALI_DEBUG_ASSERT_POINTER(job);
 
   MALI_DEBUG_ASSERT(MALI_TIMELINE_TRACKER_MAX > type);
 
   /* Zero out all tracker members. */
   _mali_osk_memset(tracker, 0, sizeof(*tracker));
 
   tracker->type = type;
   tracker->job = job;
   tracker->trigger_ref_count = 1;  /* Prevents any callback from trigging while adding it */
   tracker->os_tick_create = _mali_osk_time_tickcount();
   MALI_DEBUG_CODE(tracker->magic = MALI_TIMELINE_TRACKER_MAGIC);
 
   tracker->activation_error = MALI_TIMELINE_ACTIVATION_ERROR_NONE;
 
   /* Copy fence. */
   if (NULL != fence) {
       _mali_osk_memcpy(&tracker->fence, fence, sizeof(struct mali_timeline_fence));
   }
}
 
mali_scheduler_mask mali_timeline_tracker_release(struct mali_timeline_tracker *tracker)
{
   struct mali_timeline *timeline;
   struct mali_timeline_system *system;
   struct mali_timeline_tracker *tracker_next, *tracker_prev;
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
   u32 tid = _mali_osk_get_tid();
 
   /* Upon entry a group lock will be held, but not a scheduler lock. */
   MALI_DEBUG_ASSERT_POINTER(tracker);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_TRACKER_MAGIC == tracker->magic);
 
   /* Tracker should have been triggered */
   MALI_DEBUG_ASSERT(0 == tracker->trigger_ref_count);
 
   /* All waiters should have been released at this point */
   MALI_DEBUG_ASSERT(NULL == tracker->waiter_head);
   MALI_DEBUG_ASSERT(NULL == tracker->waiter_tail);
 
   MALI_DEBUG_PRINT(3, ("Mali Timeline: releasing tracker for job 0x%08X\n", tracker->job));
 
   timeline = tracker->timeline;
   if (NULL == timeline) {
       /* Tracker was not on a timeline, there is nothing to release. */
       return MALI_SCHEDULER_MASK_EMPTY;
   }
 
   system = timeline->system;
   MALI_DEBUG_ASSERT_POINTER(system);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   /* Tracker should still be on timeline */
   MALI_DEBUG_ASSERT(!mali_timeline_is_empty(timeline));
   MALI_DEBUG_ASSERT(mali_timeline_is_point_on(timeline, tracker->point));
 
   /* Tracker is no longer valid. */
   MALI_DEBUG_CODE(tracker->magic = 0);
 
   tracker_next = tracker->timeline_next;
   tracker_prev = tracker->timeline_prev;
   tracker->timeline_next = NULL;
   tracker->timeline_prev = NULL;
 
   /* Removing tracker from timeline's tracker list */
   if (NULL == tracker_next) {
       /* This tracker was the head */
       timeline->tracker_head = tracker_prev;
   } else {
       tracker_next->timeline_prev = tracker_prev;
   }
 
   if (NULL == tracker_prev) {
       /* This tracker was the tail */
       timeline->tracker_tail = tracker_next;
       MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
       /* Update the timeline's oldest time and release any waiters */
       schedule_mask |= mali_timeline_update_oldest_point(timeline);
       MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
   } else {
       tracker_prev->timeline_next = tracker_next;
       if (MALI_TIMELINE_SOFT == tracker->timeline->id) {
           /* Use the signaled soft tracker to release the depended soft waiter */
           schedule_mask |= mali_timeline_release_with_depended_point(tracker);
           MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
       }
   }
 
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   /* Update delayed work only when it is the soft job timeline */
   if (MALI_TIMELINE_SOFT == tracker->timeline->id) {
       mali_timeline_update_delayed_work(tracker->timeline);
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   return schedule_mask;
}
 
void mali_timeline_system_release_waiter_list(struct mali_timeline_system *system,
       struct mali_timeline_waiter *tail,
       struct mali_timeline_waiter *head)
{
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(head);
   MALI_DEBUG_ASSERT_POINTER(tail);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   head->tracker_next = system->waiter_empty_list;
   system->waiter_empty_list = tail;
}
 
static mali_scheduler_mask mali_timeline_tracker_activate(struct mali_timeline_tracker *tracker)
{
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
   struct mali_timeline_system *system;
   struct mali_timeline *timeline;
   u32 tid = _mali_osk_get_tid();
 
   MALI_DEBUG_ASSERT_POINTER(tracker);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_TRACKER_MAGIC == tracker->magic);
 
   system = tracker->system;
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   tracker->os_tick_activate = _mali_osk_time_tickcount();
 
   if (NULL != tracker->waiter_head) {
       mali_timeline_system_release_waiter_list(system, tracker->waiter_tail, tracker->waiter_head);
       tracker->waiter_head = NULL;
       tracker->waiter_tail = NULL;
   }
 
   switch (tracker->type) {
   case MALI_TIMELINE_TRACKER_GP:
       schedule_mask = mali_scheduler_activate_gp_job((struct mali_gp_job *) tracker->job);
 
       _mali_osk_atomic_dec(&gp_tracker_count);
       break;
   case MALI_TIMELINE_TRACKER_PP:
       if (mali_pp_job_is_virtual((struct mali_pp_job *)tracker->job)) {
           _mali_osk_atomic_dec(&virt_pp_tracker_count);
       } else {
           _mali_osk_atomic_dec(&phy_pp_tracker_count);
       }
       schedule_mask = mali_scheduler_activate_pp_job((struct mali_pp_job *) tracker->job);
       break;
   case MALI_TIMELINE_TRACKER_SOFT:
       timeline = tracker->timeline;
       MALI_DEBUG_ASSERT_POINTER(timeline);
 
       schedule_mask |= mali_soft_job_system_activate_job((struct mali_soft_job *) tracker->job);
 
       /* Start a soft timer to make sure the soft job be released in a limited time */
       mali_spinlock_reentrant_wait(system->spinlock, tid);
       mali_timeline_update_delayed_work(timeline);
       mali_spinlock_reentrant_signal(system->spinlock, tid);
       break;
   case MALI_TIMELINE_TRACKER_WAIT:
       mali_timeline_fence_wait_activate((struct mali_timeline_fence_wait_tracker *) tracker->job);
       break;
   case MALI_TIMELINE_TRACKER_SYNC:
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
       mali_timeline_sync_fence_activate((struct mali_timeline_sync_fence_tracker *) tracker->job);
#else
       MALI_PRINT_ERROR(("Mali Timeline: sync tracker not supported\n", tracker->type));
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
       break;
   default:
       MALI_PRINT_ERROR(("Mali Timeline - Illegal tracker type: %d\n", tracker->type));
       break;
   }
 
   return schedule_mask;
}
 
void mali_timeline_system_tracker_get(struct mali_timeline_system *system, struct mali_timeline_tracker *tracker)
{
   u32 tid = _mali_osk_get_tid();
 
   MALI_DEBUG_ASSERT_POINTER(tracker);
   MALI_DEBUG_ASSERT_POINTER(system);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   MALI_DEBUG_ASSERT(0 < tracker->trigger_ref_count);
   tracker->trigger_ref_count++;
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
}
 
mali_scheduler_mask mali_timeline_system_tracker_put(struct mali_timeline_system *system, struct mali_timeline_tracker *tracker, mali_timeline_activation_error activation_error)
{
   u32 tid = _mali_osk_get_tid();
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
 
   MALI_DEBUG_ASSERT_POINTER(tracker);
   MALI_DEBUG_ASSERT_POINTER(system);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   MALI_DEBUG_ASSERT(0 < tracker->trigger_ref_count);
   tracker->trigger_ref_count--;
 
   tracker->activation_error |= activation_error;
 
   if (0 == tracker->trigger_ref_count) {
       schedule_mask |= mali_timeline_tracker_activate(tracker);
       tracker = NULL;
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   return schedule_mask;
}
 
void mali_timeline_fence_copy_uk_fence(struct mali_timeline_fence *fence, _mali_uk_fence_t *uk_fence)
{
   u32 i;
 
   MALI_DEBUG_ASSERT_POINTER(fence);
   MALI_DEBUG_ASSERT_POINTER(uk_fence);
 
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       fence->points[i] = uk_fence->points[i];
   }
 
   fence->sync_fd = uk_fence->sync_fd;
}
 
struct mali_timeline_system *mali_timeline_system_create(struct mali_session_data *session)
{
   u32 i;
   struct mali_timeline_system *system;
 
   MALI_DEBUG_ASSERT_POINTER(session);
   MALI_DEBUG_PRINT(4, ("Mali Timeline: creating timeline system\n"));
 
   system = (struct mali_timeline_system *) _mali_osk_calloc(1, sizeof(struct mali_timeline_system));
   if (NULL == system) {
       return NULL;
   }
 
   system->spinlock = mali_spinlock_reentrant_init(_MALI_OSK_LOCK_ORDER_TIMELINE_SYSTEM);
   if (NULL == system->spinlock) {
       mali_timeline_system_destroy(system);
       return NULL;
   }
 
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       system->timelines[i] = mali_timeline_create(system, (enum mali_timeline_id)i);
       if (NULL == system->timelines[i]) {
           mali_timeline_system_destroy(system);
           return NULL;
       }
   }
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   system->signaled_sync_tl = mali_sync_timeline_create(NULL, "mali-always-signaled");
   if (NULL == system->signaled_sync_tl) {
       mali_timeline_system_destroy(system);
       return NULL;
   }
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
   system->waiter_empty_list = NULL;
   system->session = session;
   system->timer_enabled = MALI_TRUE;
 
   system->wait_queue = _mali_osk_wait_queue_init();
   if (NULL == system->wait_queue) {
       mali_timeline_system_destroy(system);
       return NULL;
   }
 
   return system;
}
 
#if defined(CONFIG_MALI_DMA_BUF_FENCE) ||defined(CONFIG_SYNC) ||defined(CONFIG_SYNC_FILE)
/**
 * Check if there are any trackers left on timeline.
 *
 * Used as a wait queue conditional.
 *
 * @param data Timeline.
 * @return MALI_TRUE if there are no trackers on timeline, MALI_FALSE if not.
 */
static mali_bool mali_timeline_has_no_trackers(void *data)
{
   struct mali_timeline *timeline = (struct mali_timeline *) data;
 
   MALI_DEBUG_ASSERT_POINTER(timeline);
 
   return mali_timeline_is_empty(timeline);
}
#if defined(CONFIG_SYNC) ||defined(CONFIG_SYNC_FILE)
/**
 * Cancel sync fence waiters waited upon by trackers on all timelines.
 *
 * Will return after all timelines have no trackers left.
 *
 * @param system Timeline system.
 */
static void mali_timeline_cancel_sync_fence_waiters(struct mali_timeline_system *system)
{
   u32 i;
   u32 tid = _mali_osk_get_tid();
   struct mali_timeline_tracker *tracker, *tracker_next;
   _MALI_OSK_LIST_HEAD_STATIC_INIT(tracker_list);
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(system->session);
   MALI_DEBUG_ASSERT(system->session->is_aborting);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   /* Cancel sync fence waiters. */
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       struct mali_timeline *timeline = system->timelines[i];
 
       MALI_DEBUG_ASSERT_POINTER(timeline);
 
       tracker_next = timeline->tracker_tail;
       while (NULL != tracker_next) {
           tracker = tracker_next;
           tracker_next = tracker->timeline_next;
 
           if (NULL == tracker->sync_fence) continue;
 
           MALI_DEBUG_PRINT(3, ("Mali Timeline: Cancelling sync fence wait for tracker 0x%08X.\n", tracker));
 
           /* Cancel sync fence waiter. */
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
           if (0 == sync_fence_cancel_async(tracker->sync_fence, &tracker->sync_fence_waiter)) {
#else
           if (0 == mali_internal_sync_fence_cancel_async(tracker->sync_fence, &tracker->sync_fence_waiter)) {
#endif
               /* Callback was not called, move tracker to local list. */
               _mali_osk_list_add(&tracker->sync_fence_cancel_list, &tracker_list);
           }
       }
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   /* Manually call sync fence callback in order to release waiter and trigger activation of tracker. */
   _MALI_OSK_LIST_FOREACHENTRY(tracker, tracker_next, &tracker_list, struct mali_timeline_tracker, sync_fence_cancel_list) {
       mali_timeline_sync_fence_callback(tracker->sync_fence, &tracker->sync_fence_waiter);
   }
 
   /* Sleep until all sync fence callbacks are done and all timelines are empty. */
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       struct mali_timeline *timeline = system->timelines[i];
 
       MALI_DEBUG_ASSERT_POINTER(timeline);
 
       _mali_osk_wait_queue_wait_event(system->wait_queue, mali_timeline_has_no_trackers, (void *) timeline);
   }
}
 
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
#if defined(CONFIG_MALI_DMA_BUF_FENCE)
static void mali_timeline_cancel_dma_fence_waiters(struct mali_timeline_system *system)
{
   u32 i, j;
   u32 tid = _mali_osk_get_tid();
   struct mali_pp_job *pp_job = NULL;
   struct mali_pp_job *next_pp_job = NULL;
   struct mali_timeline *timeline = NULL;
   struct mali_timeline_tracker *tracker, *tracker_next;
   _MALI_OSK_LIST_HEAD_STATIC_INIT(pp_job_list);
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(system->session);
   MALI_DEBUG_ASSERT(system->session->is_aborting);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   /* Cancel dma fence waiters. */
   timeline = system->timelines[MALI_TIMELINE_PP];
   MALI_DEBUG_ASSERT_POINTER(timeline);
 
   tracker_next = timeline->tracker_tail;
   while (NULL != tracker_next) {
       mali_bool fence_is_signaled = MALI_TRUE;
       tracker = tracker_next;
       tracker_next = tracker->timeline_next;
 
       if (NULL == tracker->waiter_dma_fence) continue;
       pp_job = (struct mali_pp_job *)tracker->job;
       MALI_DEBUG_ASSERT_POINTER(pp_job);
       MALI_DEBUG_PRINT(3, ("Mali Timeline: Cancelling dma fence waiter for tracker 0x%08X.\n", tracker));
 
       for (j = 0; j < pp_job->dma_fence_context.num_dma_fence_waiter; j++) {
           if (pp_job->dma_fence_context.mali_dma_fence_waiters[j]) {
               /* Cancel a previously callback from the fence.
               * This function returns true if the callback is successfully removed,
               * or false if the fence has already been signaled.
               */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
               bool ret = dma_fence_remove_callback(pp_job->dma_fence_context.mali_dma_fence_waiters[j]->fence,
                                    &pp_job->dma_fence_context.mali_dma_fence_waiters[j]->base);
 
#else
               bool ret = fence_remove_callback(pp_job->dma_fence_context.mali_dma_fence_waiters[j]->fence,
                                &pp_job->dma_fence_context.mali_dma_fence_waiters[j]->base);
#endif
               if (ret) {
                   fence_is_signaled = MALI_FALSE;
               }
           }
       }
 
       /* Callbacks were not called, move pp job to local list. */
       if (MALI_FALSE == fence_is_signaled)
           _mali_osk_list_add(&pp_job->list, &pp_job_list);
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   /* Manually call dma fence callback in order to release waiter and trigger activation of tracker. */
   _MALI_OSK_LIST_FOREACHENTRY(pp_job, next_pp_job, &pp_job_list, struct mali_pp_job, list) {
       mali_timeline_dma_fence_callback((void *)pp_job);
   }
 
   /* Sleep until all dma fence callbacks are done and all timelines are empty. */
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       struct mali_timeline *timeline = system->timelines[i];
       MALI_DEBUG_ASSERT_POINTER(timeline);
       _mali_osk_wait_queue_wait_event(system->wait_queue, mali_timeline_has_no_trackers, (void *) timeline);
   }
}
#endif
#endif
void mali_timeline_system_abort(struct mali_timeline_system *system)
{
   MALI_DEBUG_CODE(u32 tid = _mali_osk_get_tid(););
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(system->session);
   MALI_DEBUG_ASSERT(system->session->is_aborting);
 
   MALI_DEBUG_PRINT(3, ("Mali Timeline: Aborting timeline system for session 0x%08X.\n", system->session));
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   mali_timeline_cancel_sync_fence_waiters(system);
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
#if defined(CONFIG_MALI_DMA_BUF_FENCE)
   mali_timeline_cancel_dma_fence_waiters(system);
#endif
 
   /* Should not be any waiters or trackers left at this point. */
   MALI_DEBUG_CODE({
       u32 i;
       mali_spinlock_reentrant_wait(system->spinlock, tid);
       for (i = 0; i < MALI_TIMELINE_MAX; ++i)
       {
           struct mali_timeline *timeline = system->timelines[i];
           MALI_DEBUG_ASSERT_POINTER(timeline);
           MALI_DEBUG_ASSERT(timeline->point_oldest == timeline->point_next);
           MALI_DEBUG_ASSERT(NULL == timeline->tracker_head);
           MALI_DEBUG_ASSERT(NULL == timeline->tracker_tail);
           MALI_DEBUG_ASSERT(NULL == timeline->waiter_head);
           MALI_DEBUG_ASSERT(NULL == timeline->waiter_tail);
       }
       mali_spinlock_reentrant_signal(system->spinlock, tid);
   });
}
 
void mali_timeline_system_destroy(struct mali_timeline_system *system)
{
   u32 i;
   struct mali_timeline_waiter *waiter, *next;
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   u32 tid = _mali_osk_get_tid();
#endif
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(system->session);
 
   MALI_DEBUG_PRINT(4, ("Mali Timeline: destroying timeline system\n"));
 
   if (NULL != system) {
 
       /* There should be no waiters left on this queue. */
       if (NULL != system->wait_queue) {
           _mali_osk_wait_queue_term(system->wait_queue);
           system->wait_queue = NULL;
       }
 
       /* Free all waiters in empty list */
       waiter = system->waiter_empty_list;
       while (NULL != waiter) {
           next = waiter->tracker_next;
           _mali_osk_free(waiter);
           waiter = next;
       }
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
       if (NULL != system->signaled_sync_tl) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
           sync_timeline_destroy(system->signaled_sync_tl);
#else
           mali_internal_sync_timeline_destroy(system->signaled_sync_tl);
#endif
       }
 
       for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
           if ((NULL != system->timelines[i]) && (NULL != system->timelines[i]->spinlock)) {
               mali_spinlock_reentrant_wait(system->timelines[i]->spinlock, tid);
               system->timelines[i]->destroyed = MALI_TRUE;
               mali_spinlock_reentrant_signal(system->timelines[i]->spinlock, tid);
           }
       }
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
       for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
           if (NULL != system->timelines[i]) {
               mali_timeline_destroy(system->timelines[i]);
           }
       }
 
       if (NULL != system->spinlock) {
           mali_spinlock_reentrant_term(system->spinlock);
       }
 
       _mali_osk_free(system);
   }
}
 
/**
 * Find how many waiters are needed for a given fence.
 *
 * @param fence The fence to check.
 * @return Number of waiters needed for fence.
 */
static u32 mali_timeline_fence_num_waiters(struct mali_timeline_fence *fence)
{
   u32 i, num_waiters = 0;
 
   MALI_DEBUG_ASSERT_POINTER(fence);
 
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       if (MALI_TIMELINE_NO_POINT != fence->points[i]) {
           ++num_waiters;
       }
   }
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   if (-1 != fence->sync_fd) ++num_waiters;
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
   return num_waiters;
}
 
static struct mali_timeline_waiter *mali_timeline_system_get_zeroed_waiter(struct mali_timeline_system *system)
{
   struct mali_timeline_waiter *waiter;
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   waiter = system->waiter_empty_list;
   if (NULL != waiter) {
       /* Remove waiter from empty list and zero it */
       system->waiter_empty_list = waiter->tracker_next;
       _mali_osk_memset(waiter, 0, sizeof(*waiter));
   }
 
   /* Return NULL if list was empty. */
   return waiter;
}
 
static void mali_timeline_system_allocate_waiters(struct mali_timeline_system *system,
       struct mali_timeline_waiter **tail,
       struct mali_timeline_waiter **head,
       int max_num_waiters)
{
   u32 i, tid = _mali_osk_get_tid();
   mali_bool do_alloc;
   struct mali_timeline_waiter *waiter;
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(tail);
   MALI_DEBUG_ASSERT_POINTER(head);
 
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   *head = *tail = NULL;
   do_alloc = MALI_FALSE;
   i = 0;
   while (i < max_num_waiters) {
       if (MALI_FALSE == do_alloc) {
           waiter = mali_timeline_system_get_zeroed_waiter(system);
           if (NULL == waiter) {
               do_alloc = MALI_TRUE;
               mali_spinlock_reentrant_signal(system->spinlock, tid);
               continue;
           }
       } else {
           waiter = _mali_osk_calloc(1, sizeof(struct mali_timeline_waiter));
           if (NULL == waiter) break;
       }
       ++i;
       if (NULL == *tail) {
           *tail = waiter;
           *head = waiter;
       } else {
           (*head)->tracker_next = waiter;
           *head = waiter;
       }
   }
   if (MALI_TRUE == do_alloc) {
       mali_spinlock_reentrant_wait(system->spinlock, tid);
   }
}
 
/**
 * Create waiters for the given tracker. The tracker is activated when all waiters are release.
 *
 * @note Tracker can potentially be activated before this function returns.
 *
 * @param system Timeline system.
 * @param tracker Tracker we will create waiters for.
 * @param waiter_tail List of pre-allocated waiters.
 * @param waiter_head List of pre-allocated waiters.
 */
static void mali_timeline_system_create_waiters_and_unlock(struct mali_timeline_system *system,
       struct mali_timeline_tracker *tracker,
       struct mali_timeline_waiter *waiter_tail,
       struct mali_timeline_waiter *waiter_head)
{
   int i;
   u32 tid = _mali_osk_get_tid();
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
   struct sync_fence *sync_fence = NULL;
#else
   struct mali_internal_sync_fence *sync_fence = NULL;
#endif
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   MALI_DEBUG_ASSERT(NULL == tracker->waiter_head);
   MALI_DEBUG_ASSERT(NULL == tracker->waiter_tail);
   MALI_DEBUG_ASSERT(NULL != tracker->job);
 
   /* Creating waiter object for all the timelines the fence is put on. Inserting this waiter
    * into the timelines sorted list of waiters */
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       mali_timeline_point point;
       struct mali_timeline *timeline;
       struct mali_timeline_waiter *waiter;
 
       /* Get point on current timeline from tracker's fence. */
       point = tracker->fence.points[i];
 
       if (likely(MALI_TIMELINE_NO_POINT == point)) {
           /* Fence contains no point on this timeline so we don't need a waiter. */
           continue;
       }
 
       timeline = system->timelines[i];
       MALI_DEBUG_ASSERT_POINTER(timeline);
 
       if (unlikely(!mali_timeline_is_point_valid(timeline, point))) {
           MALI_PRINT_ERROR(("Mali Timeline: point %d is not valid (oldest=%d, next=%d)\n",
                     point, timeline->point_oldest, timeline->point_next));
           continue;
       }
 
       if (likely(mali_timeline_is_point_released(timeline, point))) {
           /* Tracker representing the point has been released so we don't need a
            * waiter. */
           continue;
       }
 
       if ((MALI_TIMELINE_SOFT == timeline->id) && mali_timeline_is_tracker_released(timeline, point)) {
           /* The tracker that the point related to has already been released, so no need to a waiter. */
           continue;
       }
 
       /* The point is on timeline. */
       MALI_DEBUG_ASSERT(mali_timeline_is_point_on(timeline, point));
 
       /* Get a new zeroed waiter object. */
       if (likely(NULL != waiter_tail)) {
           waiter = waiter_tail;
           waiter_tail = waiter_tail->tracker_next;
       } else {
           MALI_PRINT_ERROR(("Mali Timeline: failed to allocate memory for waiter\n"));
           continue;
       }
 
       /* Yanking the trigger ref count of the tracker. */
       tracker->trigger_ref_count++;
 
       waiter->point   = point;
       waiter->tracker = tracker;
 
       /* Insert waiter on tracker's singly-linked waiter list. */
       if (NULL == tracker->waiter_head) {
           /* list is empty */
           MALI_DEBUG_ASSERT(NULL == tracker->waiter_tail);
           tracker->waiter_tail = waiter;
       } else {
           tracker->waiter_head->tracker_next = waiter;
       }
       tracker->waiter_head = waiter;
 
       /* Add waiter to timeline. */
       mali_timeline_insert_waiter(timeline, waiter);
   }
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   if (-1 != tracker->fence.sync_fd) {
       int ret;
       struct mali_timeline_waiter *waiter;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
       sync_fence = sync_fence_fdget(tracker->fence.sync_fd);
#else
       sync_fence = mali_internal_sync_fence_fdget(tracker->fence.sync_fd);
#endif
       if (unlikely(NULL == sync_fence)) {
           MALI_PRINT_ERROR(("Mali Timeline: failed to get sync fence from fd %d\n", tracker->fence.sync_fd));
           goto exit;
       }
 
       /* Check if we have a zeroed waiter object available. */
       if (unlikely(NULL == waiter_tail)) {
           MALI_PRINT_ERROR(("Mali Timeline: failed to allocate memory for waiter\n"));
           goto exit;
       }
 
       /* Start asynchronous wait that will release waiter when the fence is signaled. */
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
       sync_fence_waiter_init(&tracker->sync_fence_waiter, mali_timeline_sync_fence_callback);
       ret = sync_fence_wait_async(sync_fence, &tracker->sync_fence_waiter);
#else
       mali_internal_sync_fence_waiter_init(&tracker->sync_fence_waiter, mali_timeline_sync_fence_callback);
       ret = mali_internal_sync_fence_wait_async(sync_fence, &tracker->sync_fence_waiter);
#endif
       if (1 == ret) {
           /* Fence already signaled, no waiter needed. */
           tracker->fence.sync_fd = -1;
           goto exit;
       } else if (0 != ret) {
           MALI_PRINT_ERROR(("Mali Timeline: sync fence fd %d signaled with error %d\n", tracker->fence.sync_fd, ret));
           tracker->activation_error |= MALI_TIMELINE_ACTIVATION_ERROR_SYNC_BIT;
           goto exit;
       }
 
       /* Grab new zeroed waiter object. */
       waiter = waiter_tail;
       waiter_tail = waiter_tail->tracker_next;
 
       /* Increase the trigger ref count of the tracker. */
       tracker->trigger_ref_count++;
 
       waiter->point   = MALI_TIMELINE_NO_POINT;
       waiter->tracker = tracker;
 
       /* Insert waiter on tracker's singly-linked waiter list. */
       if (NULL == tracker->waiter_head) {
           /* list is empty */
           MALI_DEBUG_ASSERT(NULL == tracker->waiter_tail);
           tracker->waiter_tail = waiter;
       } else {
           tracker->waiter_head->tracker_next = waiter;
       }
       tracker->waiter_head = waiter;
 
       /* Also store waiter in separate field for easy access by sync callback. */
       tracker->waiter_sync = waiter;
 
       /* Store the sync fence in tracker so we can retrieve in abort session, if needed. */
       tracker->sync_fence = sync_fence;
 
       sync_fence = NULL;
   }
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)*/
#if defined(CONFIG_MALI_DMA_BUF_FENCE)
   if ((NULL != tracker->timeline) && (MALI_TIMELINE_PP == tracker->timeline->id)) {
 
       struct mali_pp_job *job = (struct mali_pp_job *)tracker->job;
 
       if (0 < job->dma_fence_context.num_dma_fence_waiter) {
           struct mali_timeline_waiter *waiter;
           /* Check if we have a zeroed waiter object available. */
           if (unlikely(NULL == waiter_tail)) {
               MALI_PRINT_ERROR(("Mali Timeline: failed to allocate memory for waiter\n"));
               goto exit;
           }
 
           /* Grab new zeroed waiter object. */
           waiter = waiter_tail;
           waiter_tail = waiter_tail->tracker_next;
 
           /* Increase the trigger ref count of the tracker. */
           tracker->trigger_ref_count++;
 
           waiter->point   = MALI_TIMELINE_NO_POINT;
           waiter->tracker = tracker;
 
           /* Insert waiter on tracker's singly-linked waiter list. */
           if (NULL == tracker->waiter_head) {
               /* list is empty */
               MALI_DEBUG_ASSERT(NULL == tracker->waiter_tail);
               tracker->waiter_tail = waiter;
           } else {
               tracker->waiter_head->tracker_next = waiter;
           }
           tracker->waiter_head = waiter;
 
           /* Also store waiter in separate field for easy access by sync callback. */
           tracker->waiter_dma_fence = waiter;
       }
   }
#endif /* defined(CONFIG_MALI_DMA_BUF_FENCE)*/
 
#if defined(CONFIG_MALI_DMA_BUF_FENCE) ||defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
exit:
#endif /* defined(CONFIG_MALI_DMA_BUF_FENCE) || defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
   if (NULL != waiter_tail) {
       mali_timeline_system_release_waiter_list(system, waiter_tail, waiter_head);
   }
 
   /* Release the initial trigger ref count. */
   tracker->trigger_ref_count--;
 
   /* If there were no waiters added to this tracker we activate immediately. */
   if (0 == tracker->trigger_ref_count) {
       schedule_mask |= mali_timeline_tracker_activate(tracker);
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   if (NULL != sync_fence) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
       sync_fence_put(sync_fence);
#else
       fput(sync_fence->file);
#endif
   }
#endif /* defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE) */
 
   mali_executor_schedule_from_mask(schedule_mask, MALI_FALSE);
}
 
mali_timeline_point mali_timeline_system_add_tracker(struct mali_timeline_system *system,
       struct mali_timeline_tracker *tracker,
       enum mali_timeline_id timeline_id)
{
   int num_waiters = 0;
   struct mali_timeline_waiter *waiter_tail, *waiter_head;
   u32 tid = _mali_osk_get_tid();
 
   mali_timeline_point point = MALI_TIMELINE_NO_POINT;
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(system->session);
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   MALI_DEBUG_ASSERT(MALI_FALSE == system->session->is_aborting);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_TRACKER_MAX > tracker->type);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_TRACKER_MAGIC == tracker->magic);
 
   MALI_DEBUG_PRINT(4, ("Mali Timeline: adding tracker for job %p, timeline: %d\n", tracker->job, timeline_id));
 
   MALI_DEBUG_ASSERT(0 < tracker->trigger_ref_count);
   tracker->system = system;
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   num_waiters = mali_timeline_fence_num_waiters(&tracker->fence);
 
#if defined(CONFIG_MALI_DMA_BUF_FENCE)
   if (MALI_TIMELINE_PP == timeline_id) {
       struct mali_pp_job *job = (struct mali_pp_job *)tracker->job;
       if (0 < job->dma_fence_context.num_dma_fence_waiter)
           num_waiters++;
   }
#endif
 
   /* Allocate waiters. */
   mali_timeline_system_allocate_waiters(system, &waiter_tail, &waiter_head, num_waiters);
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   /* Add tracker to timeline.  This will allocate a point for the tracker on the timeline. If
    * timeline ID is MALI_TIMELINE_NONE the tracker will NOT be added to a timeline and the
    * point will be MALI_TIMELINE_NO_POINT.
    *
    * NOTE: the tracker can fail to be added if the timeline is full.  If this happens, the
    * point will be MALI_TIMELINE_NO_POINT. */
   MALI_DEBUG_ASSERT(timeline_id < MALI_TIMELINE_MAX || timeline_id == MALI_TIMELINE_NONE);
   if (likely(timeline_id < MALI_TIMELINE_MAX)) {
       struct mali_timeline *timeline = system->timelines[timeline_id];
       mali_timeline_insert_tracker(timeline, tracker);
       MALI_DEBUG_ASSERT(!mali_timeline_is_empty(timeline));
   }
 
   point = tracker->point;
 
   /* Create waiters for tracker based on supplied fence.  Each waiter will increase the
    * trigger ref count. */
   mali_timeline_system_create_waiters_and_unlock(system, tracker, waiter_tail, waiter_head);
   tracker = NULL;
 
   /* At this point the tracker object might have been freed so we should no longer
    * access it. */
 
 
   /* The tracker will always be activated after calling add_tracker, even if NO_POINT is
    * returned. */
   return point;
}
 
static mali_scheduler_mask mali_timeline_system_release_waiter(struct mali_timeline_system *system,
       struct mali_timeline_waiter *waiter)
{
   struct mali_timeline_tracker *tracker;
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
 
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(waiter);
 
   MALI_DEBUG_ASSERT(MALI_TIMELINE_SYSTEM_LOCKED(system));
 
   tracker = waiter->tracker;
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   /* At this point the waiter has been removed from the timeline's waiter list, but it is
    * still on the tracker's waiter list.  All of the tracker's waiters will be released when
    * the tracker is activated. */
 
   waiter->point   = MALI_TIMELINE_NO_POINT;
   waiter->tracker = NULL;
 
   tracker->trigger_ref_count--;
   if (0 == tracker->trigger_ref_count) {
       /* This was the last waiter; activate tracker */
       schedule_mask |= mali_timeline_tracker_activate(tracker);
       tracker = NULL;
   }
 
   return schedule_mask;
}
 
mali_timeline_point mali_timeline_system_get_latest_point(struct mali_timeline_system *system,
       enum mali_timeline_id timeline_id)
{
   mali_timeline_point point;
   struct mali_timeline *timeline;
   u32 tid = _mali_osk_get_tid();
 
   MALI_DEBUG_ASSERT_POINTER(system);
 
   if (MALI_TIMELINE_MAX <= timeline_id) {
       return MALI_TIMELINE_NO_POINT;
   }
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   timeline = system->timelines[timeline_id];
   MALI_DEBUG_ASSERT_POINTER(timeline);
 
   point = MALI_TIMELINE_NO_POINT;
   if (timeline->point_oldest != timeline->point_next) {
       point = timeline->point_next - 1;
       if (MALI_TIMELINE_NO_POINT == point) point--;
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   return point;
}
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
static void mali_timeline_do_sync_fence_callback(void *arg)
{
   _MALI_OSK_LIST_HEAD_STATIC_INIT(list);
   struct mali_timeline_tracker *tracker;
   struct mali_timeline_tracker *tmp_tracker;
   u32 tid = _mali_osk_get_tid();
 
   MALI_IGNORE(arg);
 
   /*
    * Quickly "unhook" the jobs pending to be deleted, so we can release
    * the lock before we start deleting the job objects
    * (without any locks held)
    */
   _mali_osk_spinlock_irq_lock(sync_fence_callback_list_lock);
   _mali_osk_list_move_list(&sync_fence_callback_queue, &list);
   _mali_osk_spinlock_irq_unlock(sync_fence_callback_list_lock);
 
   _MALI_OSK_LIST_FOREACHENTRY(tracker, tmp_tracker, &list,
                   struct mali_timeline_tracker, sync_fence_signal_list) {
       mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
       mali_bool is_aborting = MALI_FALSE;
       int fence_status = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
       struct sync_fence *sync_fence = NULL;
#else
       struct mali_internal_sync_fence *sync_fence = NULL;
#endif
       struct mali_timeline_system  *system = NULL;
       struct mali_timeline_waiter  *waiter = NULL;
 
       _mali_osk_list_delinit(&tracker->sync_fence_signal_list);
 
       sync_fence = tracker->sync_fence;
       MALI_DEBUG_ASSERT_POINTER(sync_fence);
 
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
       fence_status = sync_fence->status;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
       fence_status = atomic_read(&sync_fence->status);
#else
       fence_status = sync_fence->fence->ops->signaled(sync_fence->fence);
#endif
 
       system = tracker->system;
       MALI_DEBUG_ASSERT_POINTER(system);
       MALI_DEBUG_ASSERT_POINTER(system->session);
 
       mali_spinlock_reentrant_wait(system->spinlock, tid);
 
       is_aborting = system->session->is_aborting;
       if (!is_aborting && (0 > fence_status)) {
           MALI_PRINT_ERROR(("Mali Timeline: sync fence fd %d signaled with error %d\n", tracker->fence.sync_fd, fence_status));
           tracker->activation_error |= MALI_TIMELINE_ACTIVATION_ERROR_SYNC_BIT;
       }
 
       waiter = tracker->waiter_sync;
       MALI_DEBUG_ASSERT_POINTER(waiter);
 
       tracker->sync_fence = NULL;
       tracker->fence.sync_fd = -1;
 
       schedule_mask |= mali_timeline_system_release_waiter(system, waiter);
 
       /* If aborting, wake up sleepers that are waiting for sync fence callbacks to complete. */
       if (is_aborting) {
           _mali_osk_wait_queue_wake_up(system->wait_queue);
       }
 
       mali_spinlock_reentrant_signal(system->spinlock, tid);
 
       /*
        * Older versions of Linux, before 3.5, doesn't support fput() in interrupt
        * context. For those older kernels, allocate a list object and put the
        * fence object on that and defer the call to sync_fence_put() to a workqueue.
        */
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
       {
           struct mali_deferred_fence_put_entry *obj;
 
           obj = kzalloc(sizeof(struct mali_deferred_fence_put_entry), GFP_ATOMIC);
           if (obj) {
               unsigned long flags;
               mali_bool schedule = MALI_FALSE;
 
               obj->fence = sync_fence;
 
               spin_lock_irqsave(&mali_timeline_sync_fence_to_free_lock, flags);
               if (hlist_empty(&mali_timeline_sync_fence_to_free_list))
                   schedule = MALI_TRUE;
               hlist_add_head(&obj->list, &mali_timeline_sync_fence_to_free_list);
               spin_unlock_irqrestore(&mali_timeline_sync_fence_to_free_lock, flags);
 
               if (schedule)
                   schedule_delayed_work(&delayed_sync_fence_put, 0);
           }
       }
#else
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
       sync_fence_put(sync_fence);
#else
       fput(sync_fence->file);
#endif
#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0) */
 
       if (!is_aborting) {
           mali_executor_schedule_from_mask(schedule_mask, MALI_TRUE);
       }
   }
}
#endif
_mali_osk_errcode_t mali_timeline_initialize(void)
{
   _mali_osk_atomic_init(&gp_tracker_count, 0);
   _mali_osk_atomic_init(&phy_pp_tracker_count, 0);
   _mali_osk_atomic_init(&virt_pp_tracker_count, 0);
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   sync_fence_callback_list_lock = _mali_osk_spinlock_irq_init(_MALI_OSK_LOCKFLAG_UNORDERED, _MALI_OSK_LOCK_ORDER_FIRST);
   if (NULL == sync_fence_callback_list_lock) {
       return _MALI_OSK_ERR_NOMEM;
   }
 
   sync_fence_callback_work_t = _mali_osk_wq_create_work(
                        mali_timeline_do_sync_fence_callback, NULL);
 
   if (NULL == sync_fence_callback_work_t) {
       return _MALI_OSK_ERR_FAULT;
   }
#endif
   return _MALI_OSK_ERR_OK;
}
 
 
void mali_timeline_terminate(void)
{
   _mali_osk_atomic_term(&gp_tracker_count);
   _mali_osk_atomic_term(&phy_pp_tracker_count);
   _mali_osk_atomic_term(&virt_pp_tracker_count);
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   if (NULL != sync_fence_callback_list_lock) {
       _mali_osk_spinlock_irq_term(sync_fence_callback_list_lock);
       sync_fence_callback_list_lock = NULL;
   }
 
   if (NULL != sync_fence_callback_work_t) {
       _mali_osk_wq_delete_work(sync_fence_callback_work_t);
       sync_fence_callback_work_t = NULL;
   }
#endif
}
 
#if defined(MALI_TIMELINE_DEBUG_FUNCTIONS)
 
static mali_bool is_waiting_on_timeline(struct mali_timeline_tracker *tracker, enum mali_timeline_id id)
{
   struct mali_timeline *timeline;
   struct mali_timeline_system *system;
 
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   MALI_DEBUG_ASSERT_POINTER(tracker->timeline);
   timeline = tracker->timeline;
 
   MALI_DEBUG_ASSERT_POINTER(timeline->system);
   system = timeline->system;
 
   if (MALI_TIMELINE_MAX > id) {
       if (MALI_TIMELINE_NO_POINT != tracker->fence.points[id]) {
           return mali_timeline_is_point_on(system->timelines[id], tracker->fence.points[id]);
       } else {
           return MALI_FALSE;
       }
   } else {
       MALI_DEBUG_ASSERT(MALI_TIMELINE_NONE == id);
       return MALI_FALSE;
   }
}
 
static const char *timeline_id_to_string(enum mali_timeline_id id)
{
   switch (id) {
   case MALI_TIMELINE_GP:
       return "GP";
   case MALI_TIMELINE_PP:
       return "PP";
   case MALI_TIMELINE_SOFT:
       return "SOFT";
   default:
       return "NONE";
   }
}
 
static const char *timeline_tracker_type_to_string(enum mali_timeline_tracker_type type)
{
   switch (type) {
   case MALI_TIMELINE_TRACKER_GP:
       return "GP";
   case MALI_TIMELINE_TRACKER_PP:
       return "PP";
   case MALI_TIMELINE_TRACKER_SOFT:
       return "SOFT";
   case MALI_TIMELINE_TRACKER_WAIT:
       return "WAIT";
   case MALI_TIMELINE_TRACKER_SYNC:
       return "SYNC";
   default:
       return "INVALID";
   }
}
 
mali_timeline_tracker_state mali_timeline_debug_get_tracker_state(struct mali_timeline_tracker *tracker)
{
   struct mali_timeline *timeline = NULL;
 
   MALI_DEBUG_ASSERT_POINTER(tracker);
   timeline = tracker->timeline;
 
   if (0 != tracker->trigger_ref_count) {
       return MALI_TIMELINE_TS_WAITING;
   }
 
   if (timeline && (timeline->tracker_tail == tracker || NULL != tracker->timeline_prev)) {
       return MALI_TIMELINE_TS_ACTIVE;
   }
 
   if (timeline && (MALI_TIMELINE_NO_POINT == tracker->point)) {
       return MALI_TIMELINE_TS_INIT;
   }
 
   return MALI_TIMELINE_TS_FINISH;
}
 
void mali_timeline_debug_print_tracker(struct mali_timeline_tracker *tracker, _mali_osk_print_ctx *print_ctx)
{
   const char *tracker_state = "IWAF";
   char state_char = 'I';
   char tracker_type[32] = {0};
 
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   state_char = *(tracker_state + mali_timeline_debug_get_tracker_state(tracker));
   _mali_osk_snprintf(tracker_type, sizeof(tracker_type), "%s", timeline_tracker_type_to_string(tracker->type));
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   if (0 != tracker->trigger_ref_count) {
       if (print_ctx)
           _mali_osk_ctxprintf(print_ctx, "TL:  %s %u %c - ref_wait:%u [%s(%u),%s(%u),%s(%u), fd:%d, fence:(0x%08X)]  job:(0x%08X)\n",
                       tracker_type, tracker->point, state_char, tracker->trigger_ref_count,
                       is_waiting_on_timeline(tracker, MALI_TIMELINE_GP) ? "WaitGP" : " ", tracker->fence.points[0],
                       is_waiting_on_timeline(tracker, MALI_TIMELINE_PP) ? "WaitPP" : " ", tracker->fence.points[1],
                       is_waiting_on_timeline(tracker, MALI_TIMELINE_SOFT) ? "WaitSOFT" : " ", tracker->fence.points[2],
                       tracker->fence.sync_fd, (unsigned int)(uintptr_t)(tracker->sync_fence), (unsigned int)(uintptr_t)(tracker->job));
       else
           MALI_DEBUG_PRINT(2, ("TL:  %s %u %c - ref_wait:%u [%s(%u),%s(%u),%s(%u), fd:%d, fence:(0x%08X)]  job:(0x%08X)\n",
                        tracker_type, tracker->point, state_char, tracker->trigger_ref_count,
                        is_waiting_on_timeline(tracker, MALI_TIMELINE_GP) ? "WaitGP" : " ", tracker->fence.points[0],
                        is_waiting_on_timeline(tracker, MALI_TIMELINE_PP) ? "WaitPP" : " ", tracker->fence.points[1],
                        is_waiting_on_timeline(tracker, MALI_TIMELINE_SOFT) ? "WaitSOFT" : " ", tracker->fence.points[2],
                        tracker->fence.sync_fd, (unsigned int)(uintptr_t)(tracker->sync_fence), (unsigned int)(uintptr_t)(tracker->job)));
   } else {
       if (print_ctx)
           _mali_osk_ctxprintf(print_ctx, "TL:  %s %u %c  fd:%d  fence:(0x%08X)  job:(0x%08X)\n",
                       tracker_type, tracker->point, state_char,
                       tracker->fence.sync_fd, (unsigned int)(uintptr_t)(tracker->sync_fence), (unsigned int)(uintptr_t)(tracker->job));
       else
           MALI_DEBUG_PRINT(2, ("TL:  %s %u %c  fd:%d  fence:(0x%08X)  job:(0x%08X)\n",
                        tracker_type, tracker->point, state_char,
                        tracker->fence.sync_fd, (unsigned int)(uintptr_t)(tracker->sync_fence), (unsigned int)(uintptr_t)(tracker->job)));
 
   }
#else
   if (0 != tracker->trigger_ref_count) {
       if (print_ctx)
           _mali_osk_ctxprintf(print_ctx, "TL:  %s %u %c - ref_wait:%u [%s(%u),%s(%u),%s(%u)]  job:(0x%08X)\n",
                       tracker_type, tracker->point, state_char, tracker->trigger_ref_count,
                       is_waiting_on_timeline(tracker, MALI_TIMELINE_GP) ? "WaitGP" : " ", tracker->fence.points[0],
                       is_waiting_on_timeline(tracker, MALI_TIMELINE_PP) ? "WaitPP" : " ", tracker->fence.points[1],
                       is_waiting_on_timeline(tracker, MALI_TIMELINE_SOFT) ? "WaitSOFT" : " ", tracker->fence.points[2],
                       (unsigned int)(uintptr_t)(tracker->job));
       else
           MALI_DEBUG_PRINT(2, ("TL:  %s %u %c - ref_wait:%u [%s(%u),%s(%u),%s(%u)]  job:(0x%08X)\n",
                        tracker_type, tracker->point, state_char, tracker->trigger_ref_count,
                        is_waiting_on_timeline(tracker, MALI_TIMELINE_GP) ? "WaitGP" : " ", tracker->fence.points[0],
                        is_waiting_on_timeline(tracker, MALI_TIMELINE_PP) ? "WaitPP" : " ", tracker->fence.points[1],
                        is_waiting_on_timeline(tracker, MALI_TIMELINE_SOFT) ? "WaitSOFT" : " ", tracker->fence.points[2],
                        (unsigned int)(uintptr_t)(tracker->job)));
   } else {
       if (print_ctx)
           _mali_osk_ctxprintf(print_ctx, "TL:  %s %u %c  job:(0x%08X)\n",
                       tracker_type, tracker->point, state_char,
                       (unsigned int)(uintptr_t)(tracker->job));
       else
           MALI_DEBUG_PRINT(2, ("TL:  %s %u %c  job:(0x%08X)\n",
                        tracker_type, tracker->point, state_char,
                        (unsigned int)(uintptr_t)(tracker->job)));
 
   }
#endif
}
 
void mali_timeline_debug_print_timeline(struct mali_timeline *timeline, _mali_osk_print_ctx *print_ctx)
{
   struct mali_timeline_tracker *tracker = NULL;
 
   MALI_DEBUG_ASSERT_POINTER(timeline);
 
   tracker = timeline->tracker_tail;
   while (NULL != tracker) {
       mali_timeline_debug_print_tracker(tracker, print_ctx);
       tracker = tracker->timeline_next;
   }
}
 
#if !(LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0))
void mali_timeline_debug_direct_print_tracker(struct mali_timeline_tracker *tracker)
{
   const char *tracker_state = "IWAF";
   char state_char = 'I';
   char tracker_type[32] = {0};
 
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   state_char = *(tracker_state + mali_timeline_debug_get_tracker_state(tracker));
   _mali_osk_snprintf(tracker_type, sizeof(tracker_type), "%s", timeline_tracker_type_to_string(tracker->type));
 
#if defined(CONFIG_SYNC) || defined(CONFIG_SYNC_FILE)
   if (0 != tracker->trigger_ref_count) {
       MALI_PRINT(("TL:  %s %u %c - ref_wait:%u [%s(%u),%s(%u),%s(%u), fd:%d, fence:(0x%08X)]  job:(0x%08X)\n",
               tracker_type, tracker->point, state_char, tracker->trigger_ref_count,
               is_waiting_on_timeline(tracker, MALI_TIMELINE_GP) ? "WaitGP" : " ", tracker->fence.points[0],
               is_waiting_on_timeline(tracker, MALI_TIMELINE_PP) ? "WaitPP" : " ", tracker->fence.points[1],
               is_waiting_on_timeline(tracker, MALI_TIMELINE_SOFT) ? "WaitSOFT" : " ", tracker->fence.points[2],
               tracker->fence.sync_fd, tracker->sync_fence, tracker->job));
   } else {
       MALI_PRINT(("TL:  %s %u %c  fd:%d  fence:(0x%08X)  job:(0x%08X)\n",
               tracker_type, tracker->point, state_char,
               tracker->fence.sync_fd, tracker->sync_fence, tracker->job));
   }
#else
   if (0 != tracker->trigger_ref_count) {
       MALI_PRINT(("TL:  %s %u %c - ref_wait:%u [%s(%u),%s(%u),%s(%u)]  job:(0x%08X)\n",
               tracker_type, tracker->point, state_char, tracker->trigger_ref_count,
               is_waiting_on_timeline(tracker, MALI_TIMELINE_GP) ? "WaitGP" : " ", tracker->fence.points[0],
               is_waiting_on_timeline(tracker, MALI_TIMELINE_PP) ? "WaitPP" : " ", tracker->fence.points[1],
               is_waiting_on_timeline(tracker, MALI_TIMELINE_SOFT) ? "WaitSOFT" : " ", tracker->fence.points[2],
               tracker->job));
   } else {
       MALI_PRINT(("TL:  %s %u %c  job:(0x%08X)\n",
               tracker_type, tracker->point, state_char,
               tracker->job));
   }
#endif
}
 
void mali_timeline_debug_direct_print_timeline(struct mali_timeline *timeline)
{
   struct mali_timeline_tracker *tracker = NULL;
 
   MALI_DEBUG_ASSERT_POINTER(timeline);
 
   tracker = timeline->tracker_tail;
   while (NULL != tracker) {
       mali_timeline_debug_direct_print_tracker(tracker);
       tracker = tracker->timeline_next;
   }
}
 
#endif
 
void mali_timeline_debug_print_system(struct mali_timeline_system *system, _mali_osk_print_ctx *print_ctx)
{
   int i;
   int num_printed = 0;
   u32 tid = _mali_osk_get_tid();
 
   MALI_DEBUG_ASSERT_POINTER(system);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   /* Print all timelines */
   for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
       struct mali_timeline *timeline = system->timelines[i];
 
       MALI_DEBUG_ASSERT_POINTER(timeline);
 
       if (NULL == timeline->tracker_head) continue;
       if (print_ctx)
           _mali_osk_ctxprintf(print_ctx, "TL: Timeline %s:\n",
                       timeline_id_to_string((enum mali_timeline_id)i));
       else
           MALI_DEBUG_PRINT(2, ("TL: Timeline %s: oldest (%u) next(%u)\n",
                        timeline_id_to_string((enum mali_timeline_id)i), timeline->point_oldest, timeline->point_next));
 
       mali_timeline_debug_print_timeline(timeline, print_ctx);
       num_printed++;
   }
 
   if (0 == num_printed) {
       if (print_ctx)
           _mali_osk_ctxprintf(print_ctx, "TL: All timelines empty\n");
       else
           MALI_DEBUG_PRINT(2, ("TL: All timelines empty\n"));
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
}
 
#endif /* defined(MALI_TIMELINE_DEBUG_FUNCTIONS) */
 
#if defined(CONFIG_MALI_DMA_BUF_FENCE)
void mali_timeline_dma_fence_callback(void *pp_job_ptr)
{
   struct mali_timeline_system  *system;
   struct mali_timeline_waiter  *waiter;
   struct mali_timeline_tracker *tracker;
   struct mali_pp_job *pp_job = (struct mali_pp_job *)pp_job_ptr;
   mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
   u32 tid = _mali_osk_get_tid();
   mali_bool is_aborting = MALI_FALSE;
 
   MALI_DEBUG_ASSERT_POINTER(pp_job);
 
   tracker = &pp_job->tracker;
   MALI_DEBUG_ASSERT_POINTER(tracker);
 
   system = tracker->system;
   MALI_DEBUG_ASSERT_POINTER(system);
   MALI_DEBUG_ASSERT_POINTER(system->session);
 
   mali_spinlock_reentrant_wait(system->spinlock, tid);
 
   waiter = tracker->waiter_dma_fence;
   MALI_DEBUG_ASSERT_POINTER(waiter);
 
   schedule_mask |= mali_timeline_system_release_waiter(system, waiter);
 
   is_aborting = system->session->is_aborting;
 
   /* If aborting, wake up sleepers that are waiting for dma fence callbacks to complete. */
   if (is_aborting) {
       _mali_osk_wait_queue_wake_up(system->wait_queue);
   }
 
   mali_spinlock_reentrant_signal(system->spinlock, tid);
 
   if (!is_aborting) {
       mali_executor_schedule_from_mask(schedule_mask, MALI_TRUE);
   }
}
#endif