summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/panthor/panthor_sched.c
blob: 9a0ff48f7061d7115b037395fc4f4b8caf12de74 (plain)
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
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
// SPDX-License-Identifier: GPL-2.0 or MIT
/* Copyright 2023 Collabora ltd. */

#include <drm/drm_drv.h>
#include <drm/drm_exec.h>
#include <drm/drm_gem_shmem_helper.h>
#include <drm/drm_managed.h>
#include <drm/gpu_scheduler.h>
#include <drm/panthor_drm.h>

#include <linux/build_bug.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/dma-resv.h>
#include <linux/firmware.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/iosys-map.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>

#include "panthor_devfreq.h"
#include "panthor_device.h"
#include "panthor_fw.h"
#include "panthor_gem.h"
#include "panthor_gpu.h"
#include "panthor_heap.h"
#include "panthor_mmu.h"
#include "panthor_regs.h"
#include "panthor_sched.h"

/**
 * DOC: Scheduler
 *
 * Mali CSF hardware adopts a firmware-assisted scheduling model, where
 * the firmware takes care of scheduling aspects, to some extent.
 *
 * The scheduling happens at the scheduling group level, each group
 * contains 1 to N queues (N is FW/hardware dependent, and exposed
 * through the firmware interface). Each queue is assigned a command
 * stream ring buffer, which serves as a way to get jobs submitted to
 * the GPU, among other things.
 *
 * The firmware can schedule a maximum of M groups (M is FW/hardware
 * dependent, and exposed through the firmware interface). Passed
 * this maximum number of groups, the kernel must take care of
 * rotating the groups passed to the firmware so every group gets
 * a chance to have his queues scheduled for execution.
 *
 * The current implementation only supports with kernel-mode queues.
 * In other terms, userspace doesn't have access to the ring-buffer.
 * Instead, userspace passes indirect command stream buffers that are
 * called from the queue ring-buffer by the kernel using a pre-defined
 * sequence of command stream instructions to ensure the userspace driver
 * always gets consistent results (cache maintenance,
 * synchronization, ...).
 *
 * We rely on the drm_gpu_scheduler framework to deal with job
 * dependencies and submission. As any other driver dealing with a
 * FW-scheduler, we use the 1:1 entity:scheduler mode, such that each
 * entity has its own job scheduler. When a job is ready to be executed
 * (all its dependencies are met), it is pushed to the appropriate
 * queue ring-buffer, and the group is scheduled for execution if it
 * wasn't already active.
 *
 * Kernel-side group scheduling is timeslice-based. When we have less
 * groups than there are slots, the periodic tick is disabled and we
 * just let the FW schedule the active groups. When there are more
 * groups than slots, we let each group a chance to execute stuff for
 * a given amount of time, and then re-evaluate and pick new groups
 * to schedule. The group selection algorithm is based on
 * priority+round-robin.
 *
 * Even though user-mode queues is out of the scope right now, the
 * current design takes them into account by avoiding any guess on the
 * group/queue state that would be based on information we wouldn't have
 * if userspace was in charge of the ring-buffer. That's also one of the
 * reason we don't do 'cooperative' scheduling (encoding FW group slot
 * reservation as dma_fence that would be returned from the
 * drm_gpu_scheduler::prepare_job() hook, and treating group rotation as
 * a queue of waiters, ordered by job submission order). This approach
 * would work for kernel-mode queues, but would make user-mode queues a
 * lot more complicated to retrofit.
 */

#define JOB_TIMEOUT_MS				5000

#define MIN_CS_PER_CSG				8

#define MIN_CSGS				3
#define MAX_CSG_PRIO				0xf

struct panthor_group;

/**
 * struct panthor_csg_slot - Command stream group slot
 *
 * This represents a FW slot for a scheduling group.
 */
struct panthor_csg_slot {
	/** @group: Scheduling group bound to this slot. */
	struct panthor_group *group;

	/** @priority: Group priority. */
	u8 priority;

	/**
	 * @idle: True if the group bound to this slot is idle.
	 *
	 * A group is idle when it has nothing waiting for execution on
	 * all its queues, or when queues are blocked waiting for something
	 * to happen (synchronization object).
	 */
	bool idle;
};

/**
 * enum panthor_csg_priority - Group priority
 */
enum panthor_csg_priority {
	/** @PANTHOR_CSG_PRIORITY_LOW: Low priority group. */
	PANTHOR_CSG_PRIORITY_LOW = 0,

	/** @PANTHOR_CSG_PRIORITY_MEDIUM: Medium priority group. */
	PANTHOR_CSG_PRIORITY_MEDIUM,

	/** @PANTHOR_CSG_PRIORITY_HIGH: High priority group. */
	PANTHOR_CSG_PRIORITY_HIGH,

	/**
	 * @PANTHOR_CSG_PRIORITY_RT: Real-time priority group.
	 *
	 * Real-time priority allows one to preempt scheduling of other
	 * non-real-time groups. When such a group becomes executable,
	 * it will evict the group with the lowest non-rt priority if
	 * there's no free group slot available.
	 *
	 * Currently not exposed to userspace.
	 */
	PANTHOR_CSG_PRIORITY_RT,

	/** @PANTHOR_CSG_PRIORITY_COUNT: Number of priority levels. */
	PANTHOR_CSG_PRIORITY_COUNT,
};

/**
 * struct panthor_scheduler - Object used to manage the scheduler
 */
struct panthor_scheduler {
	/** @ptdev: Device. */
	struct panthor_device *ptdev;

	/**
	 * @wq: Workqueue used by our internal scheduler logic and
	 * drm_gpu_scheduler.
	 *
	 * Used for the scheduler tick, group update or other kind of FW
	 * event processing that can't be handled in the threaded interrupt
	 * path. Also passed to the drm_gpu_scheduler instances embedded
	 * in panthor_queue.
	 */
	struct workqueue_struct *wq;

	/**
	 * @heap_alloc_wq: Workqueue used to schedule tiler_oom works.
	 *
	 * We have a queue dedicated to heap chunk allocation works to avoid
	 * blocking the rest of the scheduler if the allocation tries to
	 * reclaim memory.
	 */
	struct workqueue_struct *heap_alloc_wq;

	/** @tick_work: Work executed on a scheduling tick. */
	struct delayed_work tick_work;

	/**
	 * @sync_upd_work: Work used to process synchronization object updates.
	 *
	 * We use this work to unblock queues/groups that were waiting on a
	 * synchronization object.
	 */
	struct work_struct sync_upd_work;

	/**
	 * @fw_events_work: Work used to process FW events outside the interrupt path.
	 *
	 * Even if the interrupt is threaded, we need any event processing
	 * that require taking the panthor_scheduler::lock to be processed
	 * outside the interrupt path so we don't block the tick logic when
	 * it calls panthor_fw_{csg,wait}_wait_acks(). Since most of the
	 * event processing requires taking this lock, we just delegate all
	 * FW event processing to the scheduler workqueue.
	 */
	struct work_struct fw_events_work;

	/**
	 * @fw_events: Bitmask encoding pending FW events.
	 */
	atomic_t fw_events;

	/**
	 * @resched_target: When the next tick should occur.
	 *
	 * Expressed in jiffies.
	 */
	u64 resched_target;

	/**
	 * @last_tick: When the last tick occurred.
	 *
	 * Expressed in jiffies.
	 */
	u64 last_tick;

	/** @tick_period: Tick period in jiffies. */
	u64 tick_period;

	/**
	 * @lock: Lock protecting access to all the scheduler fields.
	 *
	 * Should be taken in the tick work, the irq handler, and anywhere the @groups
	 * fields are touched.
	 */
	struct mutex lock;

	/** @groups: Various lists used to classify groups. */
	struct {
		/**
		 * @runnable: Runnable group lists.
		 *
		 * When a group has queues that want to execute something,
		 * its panthor_group::run_node should be inserted here.
		 *
		 * One list per-priority.
		 */
		struct list_head runnable[PANTHOR_CSG_PRIORITY_COUNT];

		/**
		 * @idle: Idle group lists.
		 *
		 * When all queues of a group are idle (either because they
		 * have nothing to execute, or because they are blocked), the
		 * panthor_group::run_node field should be inserted here.
		 *
		 * One list per-priority.
		 */
		struct list_head idle[PANTHOR_CSG_PRIORITY_COUNT];

		/**
		 * @waiting: List of groups whose queues are blocked on a
		 * synchronization object.
		 *
		 * Insert panthor_group::wait_node here when a group is waiting
		 * for synchronization objects to be signaled.
		 *
		 * This list is evaluated in the @sync_upd_work work.
		 */
		struct list_head waiting;
	} groups;

	/**
	 * @csg_slots: FW command stream group slots.
	 */
	struct panthor_csg_slot csg_slots[MAX_CSGS];

	/** @csg_slot_count: Number of command stream group slots exposed by the FW. */
	u32 csg_slot_count;

	/** @cs_slot_count: Number of command stream slot per group slot exposed by the FW. */
	u32 cs_slot_count;

	/** @as_slot_count: Number of address space slots supported by the MMU. */
	u32 as_slot_count;

	/** @used_csg_slot_count: Number of command stream group slot currently used. */
	u32 used_csg_slot_count;

	/** @sb_slot_count: Number of scoreboard slots. */
	u32 sb_slot_count;

	/**
	 * @might_have_idle_groups: True if an active group might have become idle.
	 *
	 * This will force a tick, so other runnable groups can be scheduled if one
	 * or more active groups became idle.
	 */
	bool might_have_idle_groups;

	/** @pm: Power management related fields. */
	struct {
		/** @has_ref: True if the scheduler owns a runtime PM reference. */
		bool has_ref;
	} pm;

	/** @reset: Reset related fields. */
	struct {
		/** @lock: Lock protecting the other reset fields. */
		struct mutex lock;

		/**
		 * @in_progress: True if a reset is in progress.
		 *
		 * Set to true in panthor_sched_pre_reset() and back to false in
		 * panthor_sched_post_reset().
		 */
		atomic_t in_progress;

		/**
		 * @stopped_groups: List containing all groups that were stopped
		 * before a reset.
		 *
		 * Insert panthor_group::run_node in the pre_reset path.
		 */
		struct list_head stopped_groups;
	} reset;
};

/**
 * struct panthor_syncobj_32b - 32-bit FW synchronization object
 */
struct panthor_syncobj_32b {
	/** @seqno: Sequence number. */
	u32 seqno;

	/**
	 * @status: Status.
	 *
	 * Not zero on failure.
	 */
	u32 status;
};

/**
 * struct panthor_syncobj_64b - 64-bit FW synchronization object
 */
struct panthor_syncobj_64b {
	/** @seqno: Sequence number. */
	u64 seqno;

	/**
	 * @status: Status.
	 *
	 * Not zero on failure.
	 */
	u32 status;

	/** @pad: MBZ. */
	u32 pad;
};

/**
 * struct panthor_queue - Execution queue
 */
struct panthor_queue {
	/** @scheduler: DRM scheduler used for this queue. */
	struct drm_gpu_scheduler scheduler;

	/** @entity: DRM scheduling entity used for this queue. */
	struct drm_sched_entity entity;

	/**
	 * @remaining_time: Time remaining before the job timeout expires.
	 *
	 * The job timeout is suspended when the queue is not scheduled by the
	 * FW. Every time we suspend the timer, we need to save the remaining
	 * time so we can restore it later on.
	 */
	unsigned long remaining_time;

	/** @timeout_suspended: True if the job timeout was suspended. */
	bool timeout_suspended;

	/**
	 * @doorbell_id: Doorbell assigned to this queue.
	 *
	 * Right now, all groups share the same doorbell, and the doorbell ID
	 * is assigned to group_slot + 1 when the group is assigned a slot. But
	 * we might decide to provide fine grained doorbell assignment at some
	 * point, so don't have to wake up all queues in a group every time one
	 * of them is updated.
	 */
	u8 doorbell_id;

	/**
	 * @priority: Priority of the queue inside the group.
	 *
	 * Must be less than 16 (Only 4 bits available).
	 */
	u8 priority;
#define CSF_MAX_QUEUE_PRIO	GENMASK(3, 0)

	/** @ringbuf: Command stream ring-buffer. */
	struct panthor_kernel_bo *ringbuf;

	/** @iface: Firmware interface. */
	struct {
		/** @mem: FW memory allocated for this interface. */
		struct panthor_kernel_bo *mem;

		/** @input: Input interface. */
		struct panthor_fw_ringbuf_input_iface *input;

		/** @output: Output interface. */
		const struct panthor_fw_ringbuf_output_iface *output;

		/** @input_fw_va: FW virtual address of the input interface buffer. */
		u32 input_fw_va;

		/** @output_fw_va: FW virtual address of the output interface buffer. */
		u32 output_fw_va;
	} iface;

	/**
	 * @syncwait: Stores information about the synchronization object this
	 * queue is waiting on.
	 */
	struct {
		/** @gpu_va: GPU address of the synchronization object. */
		u64 gpu_va;

		/** @ref: Reference value to compare against. */
		u64 ref;

		/** @gt: True if this is a greater-than test. */
		bool gt;

		/** @sync64: True if this is a 64-bit sync object. */
		bool sync64;

		/** @bo: Buffer object holding the synchronization object. */
		struct drm_gem_object *obj;

		/** @offset: Offset of the synchronization object inside @bo. */
		u64 offset;

		/**
		 * @kmap: Kernel mapping of the buffer object holding the
		 * synchronization object.
		 */
		void *kmap;
	} syncwait;

	/** @fence_ctx: Fence context fields. */
	struct {
		/** @lock: Used to protect access to all fences allocated by this context. */
		spinlock_t lock;

		/**
		 * @id: Fence context ID.
		 *
		 * Allocated with dma_fence_context_alloc().
		 */
		u64 id;

		/** @seqno: Sequence number of the last initialized fence. */
		atomic64_t seqno;

		/**
		 * @last_fence: Fence of the last submitted job.
		 *
		 * We return this fence when we get an empty command stream.
		 * This way, we are guaranteed that all earlier jobs have completed
		 * when drm_sched_job::s_fence::finished without having to feed
		 * the CS ring buffer with a dummy job that only signals the fence.
		 */
		struct dma_fence *last_fence;

		/**
		 * @in_flight_jobs: List containing all in-flight jobs.
		 *
		 * Used to keep track and signal panthor_job::done_fence when the
		 * synchronization object attached to the queue is signaled.
		 */
		struct list_head in_flight_jobs;
	} fence_ctx;
};

/**
 * enum panthor_group_state - Scheduling group state.
 */
enum panthor_group_state {
	/** @PANTHOR_CS_GROUP_CREATED: Group was created, but not scheduled yet. */
	PANTHOR_CS_GROUP_CREATED,

	/** @PANTHOR_CS_GROUP_ACTIVE: Group is currently scheduled. */
	PANTHOR_CS_GROUP_ACTIVE,

	/**
	 * @PANTHOR_CS_GROUP_SUSPENDED: Group was scheduled at least once, but is
	 * inactive/suspended right now.
	 */
	PANTHOR_CS_GROUP_SUSPENDED,

	/**
	 * @PANTHOR_CS_GROUP_TERMINATED: Group was terminated.
	 *
	 * Can no longer be scheduled. The only allowed action is a destruction.
	 */
	PANTHOR_CS_GROUP_TERMINATED,

	/**
	 * @PANTHOR_CS_GROUP_UNKNOWN_STATE: Group is an unknown state.
	 *
	 * The FW returned an inconsistent state. The group is flagged unusable
	 * and can no longer be scheduled. The only allowed action is a
	 * destruction.
	 *
	 * When that happens, we also schedule a FW reset, to start from a fresh
	 * state.
	 */
	PANTHOR_CS_GROUP_UNKNOWN_STATE,
};

/**
 * struct panthor_group - Scheduling group object
 */
struct panthor_group {
	/** @refcount: Reference count */
	struct kref refcount;

	/** @ptdev: Device. */
	struct panthor_device *ptdev;

	/** @vm: VM bound to the group. */
	struct panthor_vm *vm;

	/** @compute_core_mask: Mask of shader cores that can be used for compute jobs. */
	u64 compute_core_mask;

	/** @fragment_core_mask: Mask of shader cores that can be used for fragment jobs. */
	u64 fragment_core_mask;

	/** @tiler_core_mask: Mask of tiler cores that can be used for tiler jobs. */
	u64 tiler_core_mask;

	/** @max_compute_cores: Maximum number of shader cores used for compute jobs. */
	u8 max_compute_cores;

	/** @max_fragment_cores: Maximum number of shader cores used for fragment jobs. */
	u8 max_fragment_cores;

	/** @max_tiler_cores: Maximum number of tiler cores used for tiler jobs. */
	u8 max_tiler_cores;

	/** @priority: Group priority (check panthor_csg_priority). */
	u8 priority;

	/** @blocked_queues: Bitmask reflecting the blocked queues. */
	u32 blocked_queues;

	/** @idle_queues: Bitmask reflecting the idle queues. */
	u32 idle_queues;

	/** @fatal_lock: Lock used to protect access to fatal fields. */
	spinlock_t fatal_lock;

	/** @fatal_queues: Bitmask reflecting the queues that hit a fatal exception. */
	u32 fatal_queues;

	/** @tiler_oom: Mask of queues that have a tiler OOM event to process. */
	atomic_t tiler_oom;

	/** @queue_count: Number of queues in this group. */
	u32 queue_count;

	/** @queues: Queues owned by this group. */
	struct panthor_queue *queues[MAX_CS_PER_CSG];

	/**
	 * @csg_id: ID of the FW group slot.
	 *
	 * -1 when the group is not scheduled/active.
	 */
	int csg_id;

	/**
	 * @destroyed: True when the group has been destroyed.
	 *
	 * If a group is destroyed it becomes useless: no further jobs can be submitted
	 * to its queues. We simply wait for all references to be dropped so we can
	 * release the group object.
	 */
	bool destroyed;

	/**
	 * @timedout: True when a timeout occurred on any of the queues owned by
	 * this group.
	 *
	 * Timeouts can be reported by drm_sched or by the FW. In any case, any
	 * timeout situation is unrecoverable, and the group becomes useless.
	 * We simply wait for all references to be dropped so we can release the
	 * group object.
	 */
	bool timedout;

	/**
	 * @syncobjs: Pool of per-queue synchronization objects.
	 *
	 * One sync object per queue. The position of the sync object is
	 * determined by the queue index.
	 */
	struct panthor_kernel_bo *syncobjs;

	/** @state: Group state. */
	enum panthor_group_state state;

	/**
	 * @suspend_buf: Suspend buffer.
	 *
	 * Stores the state of the group and its queues when a group is suspended.
	 * Used at resume time to restore the group in its previous state.
	 *
	 * The size of the suspend buffer is exposed through the FW interface.
	 */
	struct panthor_kernel_bo *suspend_buf;

	/**
	 * @protm_suspend_buf: Protection mode suspend buffer.
	 *
	 * Stores the state of the group and its queues when a group that's in
	 * protection mode is suspended.
	 *
	 * Used at resume time to restore the group in its previous state.
	 *
	 * The size of the protection mode suspend buffer is exposed through the
	 * FW interface.
	 */
	struct panthor_kernel_bo *protm_suspend_buf;

	/** @sync_upd_work: Work used to check/signal job fences. */
	struct work_struct sync_upd_work;

	/** @tiler_oom_work: Work used to process tiler OOM events happening on this group. */
	struct work_struct tiler_oom_work;

	/** @term_work: Work used to finish the group termination procedure. */
	struct work_struct term_work;

	/**
	 * @release_work: Work used to release group resources.
	 *
	 * We need to postpone the group release to avoid a deadlock when
	 * the last ref is released in the tick work.
	 */
	struct work_struct release_work;

	/**
	 * @run_node: Node used to insert the group in the
	 * panthor_group::groups::{runnable,idle} and
	 * panthor_group::reset.stopped_groups lists.
	 */
	struct list_head run_node;

	/**
	 * @wait_node: Node used to insert the group in the
	 * panthor_group::groups::waiting list.
	 */
	struct list_head wait_node;
};

/**
 * group_queue_work() - Queue a group work
 * @group: Group to queue the work for.
 * @wname: Work name.
 *
 * Grabs a ref and queue a work item to the scheduler workqueue. If
 * the work was already queued, we release the reference we grabbed.
 *
 * Work callbacks must release the reference we grabbed here.
 */
#define group_queue_work(group, wname) \
	do { \
		group_get(group); \
		if (!queue_work((group)->ptdev->scheduler->wq, &(group)->wname ## _work)) \
			group_put(group); \
	} while (0)

/**
 * sched_queue_work() - Queue a scheduler work.
 * @sched: Scheduler object.
 * @wname: Work name.
 *
 * Conditionally queues a scheduler work if no reset is pending/in-progress.
 */
#define sched_queue_work(sched, wname) \
	do { \
		if (!atomic_read(&(sched)->reset.in_progress) && \
		    !panthor_device_reset_is_pending((sched)->ptdev)) \
			queue_work((sched)->wq, &(sched)->wname ## _work); \
	} while (0)

/**
 * sched_queue_delayed_work() - Queue a scheduler delayed work.
 * @sched: Scheduler object.
 * @wname: Work name.
 * @delay: Work delay in jiffies.
 *
 * Conditionally queues a scheduler delayed work if no reset is
 * pending/in-progress.
 */
#define sched_queue_delayed_work(sched, wname, delay) \
	do { \
		if (!atomic_read(&sched->reset.in_progress) && \
		    !panthor_device_reset_is_pending((sched)->ptdev)) \
			mod_delayed_work((sched)->wq, &(sched)->wname ## _work, delay); \
	} while (0)

/*
 * We currently set the maximum of groups per file to an arbitrary low value.
 * But this can be updated if we need more.
 */
#define MAX_GROUPS_PER_POOL 128

/**
 * struct panthor_group_pool - Group pool
 *
 * Each file get assigned a group pool.
 */
struct panthor_group_pool {
	/** @xa: Xarray used to manage group handles. */
	struct xarray xa;
};

/**
 * struct panthor_job - Used to manage GPU job
 */
struct panthor_job {
	/** @base: Inherit from drm_sched_job. */
	struct drm_sched_job base;

	/** @refcount: Reference count. */
	struct kref refcount;

	/** @group: Group of the queue this job will be pushed to. */
	struct panthor_group *group;

	/** @queue_idx: Index of the queue inside @group. */
	u32 queue_idx;

	/** @call_info: Information about the userspace command stream call. */
	struct {
		/** @start: GPU address of the userspace command stream. */
		u64 start;

		/** @size: Size of the userspace command stream. */
		u32 size;

		/**
		 * @latest_flush: Flush ID at the time the userspace command
		 * stream was built.
		 *
		 * Needed for the flush reduction mechanism.
		 */
		u32 latest_flush;
	} call_info;

	/** @ringbuf: Position of this job is in the ring buffer. */
	struct {
		/** @start: Start offset. */
		u64 start;

		/** @end: End offset. */
		u64 end;
	} ringbuf;

	/**
	 * @node: Used to insert the job in the panthor_queue::fence_ctx::in_flight_jobs
	 * list.
	 */
	struct list_head node;

	/** @done_fence: Fence signaled when the job is finished or cancelled. */
	struct dma_fence *done_fence;
};

static void
panthor_queue_put_syncwait_obj(struct panthor_queue *queue)
{
	if (queue->syncwait.kmap) {
		struct iosys_map map = IOSYS_MAP_INIT_VADDR(queue->syncwait.kmap);

		drm_gem_vunmap_unlocked(queue->syncwait.obj, &map);
		queue->syncwait.kmap = NULL;
	}

	drm_gem_object_put(queue->syncwait.obj);
	queue->syncwait.obj = NULL;
}

static void *
panthor_queue_get_syncwait_obj(struct panthor_group *group, struct panthor_queue *queue)
{
	struct panthor_device *ptdev = group->ptdev;
	struct panthor_gem_object *bo;
	struct iosys_map map;
	int ret;

	if (queue->syncwait.kmap)
		return queue->syncwait.kmap + queue->syncwait.offset;

	bo = panthor_vm_get_bo_for_va(group->vm,
				      queue->syncwait.gpu_va,
				      &queue->syncwait.offset);
	if (drm_WARN_ON(&ptdev->base, IS_ERR_OR_NULL(bo)))
		goto err_put_syncwait_obj;

	queue->syncwait.obj = &bo->base.base;
	ret = drm_gem_vmap_unlocked(queue->syncwait.obj, &map);
	if (drm_WARN_ON(&ptdev->base, ret))
		goto err_put_syncwait_obj;

	queue->syncwait.kmap = map.vaddr;
	if (drm_WARN_ON(&ptdev->base, !queue->syncwait.kmap))
		goto err_put_syncwait_obj;

	return queue->syncwait.kmap + queue->syncwait.offset;

err_put_syncwait_obj:
	panthor_queue_put_syncwait_obj(queue);
	return NULL;
}

static void group_free_queue(struct panthor_group *group, struct panthor_queue *queue)
{
	if (IS_ERR_OR_NULL(queue))
		return;

	if (queue->entity.fence_context)
		drm_sched_entity_destroy(&queue->entity);

	if (queue->scheduler.ops)
		drm_sched_fini(&queue->scheduler);

	panthor_queue_put_syncwait_obj(queue);

	panthor_kernel_bo_destroy(queue->ringbuf);
	panthor_kernel_bo_destroy(queue->iface.mem);

	/* Release the last_fence we were holding, if any. */
	dma_fence_put(queue->fence_ctx.last_fence);

	kfree(queue);
}

static void group_release_work(struct work_struct *work)
{
	struct panthor_group *group = container_of(work,
						   struct panthor_group,
						   release_work);
	u32 i;

	for (i = 0; i < group->queue_count; i++)
		group_free_queue(group, group->queues[i]);

	panthor_kernel_bo_destroy(group->suspend_buf);
	panthor_kernel_bo_destroy(group->protm_suspend_buf);
	panthor_kernel_bo_destroy(group->syncobjs);

	panthor_vm_put(group->vm);
	kfree(group);
}

static void group_release(struct kref *kref)
{
	struct panthor_group *group = container_of(kref,
						   struct panthor_group,
						   refcount);
	struct panthor_device *ptdev = group->ptdev;

	drm_WARN_ON(&ptdev->base, group->csg_id >= 0);
	drm_WARN_ON(&ptdev->base, !list_empty(&group->run_node));
	drm_WARN_ON(&ptdev->base, !list_empty(&group->wait_node));

	queue_work(panthor_cleanup_wq, &group->release_work);
}

static void group_put(struct panthor_group *group)
{
	if (group)
		kref_put(&group->refcount, group_release);
}

static struct panthor_group *
group_get(struct panthor_group *group)
{
	if (group)
		kref_get(&group->refcount);

	return group;
}

/**
 * group_bind_locked() - Bind a group to a group slot
 * @group: Group.
 * @csg_id: Slot.
 *
 * Return: 0 on success, a negative error code otherwise.
 */
static int
group_bind_locked(struct panthor_group *group, u32 csg_id)
{
	struct panthor_device *ptdev = group->ptdev;
	struct panthor_csg_slot *csg_slot;
	int ret;

	lockdep_assert_held(&ptdev->scheduler->lock);

	if (drm_WARN_ON(&ptdev->base, group->csg_id != -1 || csg_id >= MAX_CSGS ||
			ptdev->scheduler->csg_slots[csg_id].group))
		return -EINVAL;

	ret = panthor_vm_active(group->vm);
	if (ret)
		return ret;

	csg_slot = &ptdev->scheduler->csg_slots[csg_id];
	group_get(group);
	group->csg_id = csg_id;

	/* Dummy doorbell allocation: doorbell is assigned to the group and
	 * all queues use the same doorbell.
	 *
	 * TODO: Implement LRU-based doorbell assignment, so the most often
	 * updated queues get their own doorbell, thus avoiding useless checks
	 * on queues belonging to the same group that are rarely updated.
	 */
	for (u32 i = 0; i < group->queue_count; i++)
		group->queues[i]->doorbell_id = csg_id + 1;

	csg_slot->group = group;

	return 0;
}

/**
 * group_unbind_locked() - Unbind a group from a slot.
 * @group: Group to unbind.
 *
 * Return: 0 on success, a negative error code otherwise.
 */
static int
group_unbind_locked(struct panthor_group *group)
{
	struct panthor_device *ptdev = group->ptdev;
	struct panthor_csg_slot *slot;

	lockdep_assert_held(&ptdev->scheduler->lock);

	if (drm_WARN_ON(&ptdev->base, group->csg_id < 0 || group->csg_id >= MAX_CSGS))
		return -EINVAL;

	if (drm_WARN_ON(&ptdev->base, group->state == PANTHOR_CS_GROUP_ACTIVE))
		return -EINVAL;

	slot = &ptdev->scheduler->csg_slots[group->csg_id];
	panthor_vm_idle(group->vm);
	group->csg_id = -1;

	/* Tiler OOM events will be re-issued next time the group is scheduled. */
	atomic_set(&group->tiler_oom, 0);
	cancel_work(&group->tiler_oom_work);

	for (u32 i = 0; i < group->queue_count; i++)
		group->queues[i]->doorbell_id = -1;

	slot->group = NULL;

	group_put(group);
	return 0;
}

/**
 * cs_slot_prog_locked() - Program a queue slot
 * @ptdev: Device.
 * @csg_id: Group slot ID.
 * @cs_id: Queue slot ID.
 *
 * Program a queue slot with the queue information so things can start being
 * executed on this queue.
 *
 * The group slot must have a group bound to it already (group_bind_locked()).
 */
static void
cs_slot_prog_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id)
{
	struct panthor_queue *queue = ptdev->scheduler->csg_slots[csg_id].group->queues[cs_id];
	struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);

	lockdep_assert_held(&ptdev->scheduler->lock);

	queue->iface.input->extract = queue->iface.output->extract;
	drm_WARN_ON(&ptdev->base, queue->iface.input->insert < queue->iface.input->extract);

	cs_iface->input->ringbuf_base = panthor_kernel_bo_gpuva(queue->ringbuf);
	cs_iface->input->ringbuf_size = panthor_kernel_bo_size(queue->ringbuf);
	cs_iface->input->ringbuf_input = queue->iface.input_fw_va;
	cs_iface->input->ringbuf_output = queue->iface.output_fw_va;
	cs_iface->input->config = CS_CONFIG_PRIORITY(queue->priority) |
				  CS_CONFIG_DOORBELL(queue->doorbell_id);
	cs_iface->input->ack_irq_mask = ~0;
	panthor_fw_update_reqs(cs_iface, req,
			       CS_IDLE_SYNC_WAIT |
			       CS_IDLE_EMPTY |
			       CS_STATE_START |
			       CS_EXTRACT_EVENT,
			       CS_IDLE_SYNC_WAIT |
			       CS_IDLE_EMPTY |
			       CS_STATE_MASK |
			       CS_EXTRACT_EVENT);
	if (queue->iface.input->insert != queue->iface.input->extract && queue->timeout_suspended) {
		drm_sched_resume_timeout(&queue->scheduler, queue->remaining_time);
		queue->timeout_suspended = false;
	}
}

/**
 * cs_slot_reset_locked() - Reset a queue slot
 * @ptdev: Device.
 * @csg_id: Group slot.
 * @cs_id: Queue slot.
 *
 * Change the queue slot state to STOP and suspend the queue timeout if
 * the queue is not blocked.
 *
 * The group slot must have a group bound to it (group_bind_locked()).
 */
static int
cs_slot_reset_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id)
{
	struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
	struct panthor_group *group = ptdev->scheduler->csg_slots[csg_id].group;
	struct panthor_queue *queue = group->queues[cs_id];

	lockdep_assert_held(&ptdev->scheduler->lock);

	panthor_fw_update_reqs(cs_iface, req,
			       CS_STATE_STOP,
			       CS_STATE_MASK);

	/* If the queue is blocked, we want to keep the timeout running, so
	 * we can detect unbounded waits and kill the group when that happens.
	 */
	if (!(group->blocked_queues & BIT(cs_id)) && !queue->timeout_suspended) {
		queue->remaining_time = drm_sched_suspend_timeout(&queue->scheduler);
		queue->timeout_suspended = true;
		WARN_ON(queue->remaining_time > msecs_to_jiffies(JOB_TIMEOUT_MS));
	}

	return 0;
}

/**
 * csg_slot_sync_priority_locked() - Synchronize the group slot priority
 * @ptdev: Device.
 * @csg_id: Group slot ID.
 *
 * Group slot priority update happens asynchronously. When we receive a
 * %CSG_ENDPOINT_CONFIG, we know the update is effective, and can
 * reflect it to our panthor_csg_slot object.
 */
static void
csg_slot_sync_priority_locked(struct panthor_device *ptdev, u32 csg_id)
{
	struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
	struct panthor_fw_csg_iface *csg_iface;

	lockdep_assert_held(&ptdev->scheduler->lock);

	csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
	csg_slot->priority = (csg_iface->input->endpoint_req & CSG_EP_REQ_PRIORITY_MASK) >> 28;
}

/**
 * cs_slot_sync_queue_state_locked() - Synchronize the queue slot priority
 * @ptdev: Device.
 * @csg_id: Group slot.
 * @cs_id: Queue slot.
 *
 * Queue state is updated on group suspend or STATUS_UPDATE event.
 */
static void
cs_slot_sync_queue_state_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id)
{
	struct panthor_group *group = ptdev->scheduler->csg_slots[csg_id].group;
	struct panthor_queue *queue = group->queues[cs_id];
	struct panthor_fw_cs_iface *cs_iface =
		panthor_fw_get_cs_iface(group->ptdev, csg_id, cs_id);

	u32 status_wait_cond;

	switch (cs_iface->output->status_blocked_reason) {
	case CS_STATUS_BLOCKED_REASON_UNBLOCKED:
		if (queue->iface.input->insert == queue->iface.output->extract &&
		    cs_iface->output->status_scoreboards == 0)
			group->idle_queues |= BIT(cs_id);
		break;

	case CS_STATUS_BLOCKED_REASON_SYNC_WAIT:
		if (list_empty(&group->wait_node)) {
			list_move_tail(&group->wait_node,
				       &group->ptdev->scheduler->groups.waiting);
		}
		group->blocked_queues |= BIT(cs_id);
		queue->syncwait.gpu_va = cs_iface->output->status_wait_sync_ptr;
		queue->syncwait.ref = cs_iface->output->status_wait_sync_value;
		status_wait_cond = cs_iface->output->status_wait & CS_STATUS_WAIT_SYNC_COND_MASK;
		queue->syncwait.gt = status_wait_cond == CS_STATUS_WAIT_SYNC_COND_GT;
		if (cs_iface->output->status_wait & CS_STATUS_WAIT_SYNC_64B) {
			u64 sync_val_hi = cs_iface->output->status_wait_sync_value_hi;

			queue->syncwait.sync64 = true;
			queue->syncwait.ref |= sync_val_hi << 32;
		} else {
			queue->syncwait.sync64 = false;
		}
		break;

	default:
		/* Other reasons are not blocking. Consider the queue as runnable
		 * in those cases.
		 */
		break;
	}
}

static void
csg_slot_sync_queues_state_locked(struct panthor_device *ptdev, u32 csg_id)
{
	struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
	struct panthor_group *group = csg_slot->group;
	u32 i;

	lockdep_assert_held(&ptdev->scheduler->lock);

	group->idle_queues = 0;
	group->blocked_queues = 0;

	for (i = 0; i < group->queue_count; i++) {
		if (group->queues[i])
			cs_slot_sync_queue_state_locked(ptdev, csg_id, i);
	}
}

static void
csg_slot_sync_state_locked(struct panthor_device *ptdev, u32 csg_id)
{
	struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
	struct panthor_fw_csg_iface *csg_iface;
	struct panthor_group *group;
	enum panthor_group_state new_state, old_state;
	u32 csg_state;

	lockdep_assert_held(&ptdev->scheduler->lock);

	csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
	group = csg_slot->group;

	if (!group)
		return;

	old_state = group->state;
	csg_state = csg_iface->output->ack & CSG_STATE_MASK;
	switch (csg_state) {
	case CSG_STATE_START:
	case CSG_STATE_RESUME:
		new_state = PANTHOR_CS_GROUP_ACTIVE;
		break;
	case CSG_STATE_TERMINATE:
		new_state = PANTHOR_CS_GROUP_TERMINATED;
		break;
	case CSG_STATE_SUSPEND:
		new_state = PANTHOR_CS_GROUP_SUSPENDED;
		break;
	default:
		/* The unknown state might be caused by a FW state corruption,
		 * which means the group metadata can't be trusted anymore, and
		 * the SUSPEND operation might propagate the corruption to the
		 * suspend buffers. Flag the group state as unknown to make
		 * sure it's unusable after that point.
		 */
		drm_err(&ptdev->base, "Invalid state on CSG %d (state=%d)",
			csg_id, csg_state);
		new_state = PANTHOR_CS_GROUP_UNKNOWN_STATE;
		break;
	}

	if (old_state == new_state)
		return;

	/* The unknown state might be caused by a FW issue, reset the FW to
	 * take a fresh start.
	 */
	if (new_state == PANTHOR_CS_GROUP_UNKNOWN_STATE)
		panthor_device_schedule_reset(ptdev);

	if (new_state == PANTHOR_CS_GROUP_SUSPENDED)
		csg_slot_sync_queues_state_locked(ptdev, csg_id);

	if (old_state == PANTHOR_CS_GROUP_ACTIVE) {
		u32 i;

		/* Reset the queue slots so we start from a clean
		 * state when starting/resuming a new group on this
		 * CSG slot. No wait needed here, and no ringbell
		 * either, since the CS slot will only be re-used
		 * on the next CSG start operation.
		 */
		for (i = 0; i < group->queue_count; i++) {
			if (group->queues[i])
				cs_slot_reset_locked(ptdev, csg_id, i);
		}
	}

	group->state = new_state;
}

static int
csg_slot_prog_locked(struct panthor_device *ptdev, u32 csg_id, u32 priority)
{
	struct panthor_fw_csg_iface *csg_iface;
	struct panthor_csg_slot *csg_slot;
	struct panthor_group *group;
	u32 queue_mask = 0, i;

	lockdep_assert_held(&ptdev->scheduler->lock);

	if (priority > MAX_CSG_PRIO)
		return -EINVAL;

	if (drm_WARN_ON(&ptdev->base, csg_id >= MAX_CSGS))
		return -EINVAL;

	csg_slot = &ptdev->scheduler->csg_slots[csg_id];
	group = csg_slot->group;
	if (!group || group->state == PANTHOR_CS_GROUP_ACTIVE)
		return 0;

	csg_iface = panthor_fw_get_csg_iface(group->ptdev, csg_id);

	for (i = 0; i < group->queue_count; i++) {
		if (group->queues[i]) {
			cs_slot_prog_locked(ptdev, csg_id, i);
			queue_mask |= BIT(i);
		}
	}

	csg_iface->input->allow_compute = group->compute_core_mask;
	csg_iface->input->allow_fragment = group->fragment_core_mask;
	csg_iface->input->allow_other = group->tiler_core_mask;
	csg_iface->input->endpoint_req = CSG_EP_REQ_COMPUTE(group->max_compute_cores) |
					 CSG_EP_REQ_FRAGMENT(group->max_fragment_cores) |
					 CSG_EP_REQ_TILER(group->max_tiler_cores) |
					 CSG_EP_REQ_PRIORITY(priority);
	csg_iface->input->config = panthor_vm_as(group->vm);

	if (group->suspend_buf)
		csg_iface->input->suspend_buf = panthor_kernel_bo_gpuva(group->suspend_buf);
	else
		csg_iface->input->suspend_buf = 0;

	if (group->protm_suspend_buf) {
		csg_iface->input->protm_suspend_buf =
			panthor_kernel_bo_gpuva(group->protm_suspend_buf);
	} else {
		csg_iface->input->protm_suspend_buf = 0;
	}

	csg_iface->input->ack_irq_mask = ~0;
	panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, queue_mask);
	return 0;
}

static void
cs_slot_process_fatal_event_locked(struct panthor_device *ptdev,
				   u32 csg_id, u32 cs_id)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
	struct panthor_group *group = csg_slot->group;
	struct panthor_fw_cs_iface *cs_iface;
	u32 fatal;
	u64 info;

	lockdep_assert_held(&sched->lock);

	cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
	fatal = cs_iface->output->fatal;
	info = cs_iface->output->fatal_info;

	if (group)
		group->fatal_queues |= BIT(cs_id);

	if (CS_EXCEPTION_TYPE(fatal) == DRM_PANTHOR_EXCEPTION_CS_UNRECOVERABLE) {
		/* If this exception is unrecoverable, queue a reset, and make
		 * sure we stop scheduling groups until the reset has happened.
		 */
		panthor_device_schedule_reset(ptdev);
		cancel_delayed_work(&sched->tick_work);
	} else {
		sched_queue_delayed_work(sched, tick, 0);
	}

	drm_warn(&ptdev->base,
		 "CSG slot %d CS slot: %d\n"
		 "CS_FATAL.EXCEPTION_TYPE: 0x%x (%s)\n"
		 "CS_FATAL.EXCEPTION_DATA: 0x%x\n"
		 "CS_FATAL_INFO.EXCEPTION_DATA: 0x%llx\n",
		 csg_id, cs_id,
		 (unsigned int)CS_EXCEPTION_TYPE(fatal),
		 panthor_exception_name(ptdev, CS_EXCEPTION_TYPE(fatal)),
		 (unsigned int)CS_EXCEPTION_DATA(fatal),
		 info);
}

static void
cs_slot_process_fault_event_locked(struct panthor_device *ptdev,
				   u32 csg_id, u32 cs_id)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
	struct panthor_group *group = csg_slot->group;
	struct panthor_queue *queue = group && cs_id < group->queue_count ?
				      group->queues[cs_id] : NULL;
	struct panthor_fw_cs_iface *cs_iface;
	u32 fault;
	u64 info;

	lockdep_assert_held(&sched->lock);

	cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
	fault = cs_iface->output->fault;
	info = cs_iface->output->fault_info;

	if (queue && CS_EXCEPTION_TYPE(fault) == DRM_PANTHOR_EXCEPTION_CS_INHERIT_FAULT) {
		u64 cs_extract = queue->iface.output->extract;
		struct panthor_job *job;

		spin_lock(&queue->fence_ctx.lock);
		list_for_each_entry(job, &queue->fence_ctx.in_flight_jobs, node) {
			if (cs_extract >= job->ringbuf.end)
				continue;

			if (cs_extract < job->ringbuf.start)
				break;

			dma_fence_set_error(job->done_fence, -EINVAL);
		}
		spin_unlock(&queue->fence_ctx.lock);
	}

	drm_warn(&ptdev->base,
		 "CSG slot %d CS slot: %d\n"
		 "CS_FAULT.EXCEPTION_TYPE: 0x%x (%s)\n"
		 "CS_FAULT.EXCEPTION_DATA: 0x%x\n"
		 "CS_FAULT_INFO.EXCEPTION_DATA: 0x%llx\n",
		 csg_id, cs_id,
		 (unsigned int)CS_EXCEPTION_TYPE(fault),
		 panthor_exception_name(ptdev, CS_EXCEPTION_TYPE(fault)),
		 (unsigned int)CS_EXCEPTION_DATA(fault),
		 info);
}

static int group_process_tiler_oom(struct panthor_group *group, u32 cs_id)
{
	struct panthor_device *ptdev = group->ptdev;
	struct panthor_scheduler *sched = ptdev->scheduler;
	u32 renderpasses_in_flight, pending_frag_count;
	struct panthor_heap_pool *heaps = NULL;
	u64 heap_address, new_chunk_va = 0;
	u32 vt_start, vt_end, frag_end;
	int ret, csg_id;

	mutex_lock(&sched->lock);
	csg_id = group->csg_id;
	if (csg_id >= 0) {
		struct panthor_fw_cs_iface *cs_iface;

		cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
		heaps = panthor_vm_get_heap_pool(group->vm, false);
		heap_address = cs_iface->output->heap_address;
		vt_start = cs_iface->output->heap_vt_start;
		vt_end = cs_iface->output->heap_vt_end;
		frag_end = cs_iface->output->heap_frag_end;
		renderpasses_in_flight = vt_start - frag_end;
		pending_frag_count = vt_end - frag_end;
	}
	mutex_unlock(&sched->lock);

	/* The group got scheduled out, we stop here. We will get a new tiler OOM event
	 * when it's scheduled again.
	 */
	if (unlikely(csg_id < 0))
		return 0;

	if (IS_ERR(heaps) || frag_end > vt_end || vt_end >= vt_start) {
		ret = -EINVAL;
	} else {
		/* We do the allocation without holding the scheduler lock to avoid
		 * blocking the scheduling.
		 */
		ret = panthor_heap_grow(heaps, heap_address,
					renderpasses_in_flight,
					pending_frag_count, &new_chunk_va);
	}

	/* If the heap context doesn't have memory for us, we want to let the
	 * FW try to reclaim memory by waiting for fragment jobs to land or by
	 * executing the tiler OOM exception handler, which is supposed to
	 * implement incremental rendering.
	 */
	if (ret && ret != -ENOMEM) {
		drm_warn(&ptdev->base, "Failed to extend the tiler heap\n");
		group->fatal_queues |= BIT(cs_id);
		sched_queue_delayed_work(sched, tick, 0);
		goto out_put_heap_pool;
	}

	mutex_lock(&sched->lock);
	csg_id = group->csg_id;
	if (csg_id >= 0) {
		struct panthor_fw_csg_iface *csg_iface;
		struct panthor_fw_cs_iface *cs_iface;

		csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
		cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);

		cs_iface->input->heap_start = new_chunk_va;
		cs_iface->input->heap_end = new_chunk_va;
		panthor_fw_update_reqs(cs_iface, req, cs_iface->output->ack, CS_TILER_OOM);
		panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, BIT(cs_id));
		panthor_fw_ring_csg_doorbells(ptdev, BIT(csg_id));
	}
	mutex_unlock(&sched->lock);

	/* We allocated a chunck, but couldn't link it to the heap
	 * context because the group was scheduled out while we were
	 * allocating memory. We need to return this chunk to the heap.
	 */
	if (unlikely(csg_id < 0 && new_chunk_va))
		panthor_heap_return_chunk(heaps, heap_address, new_chunk_va);

	ret = 0;

out_put_heap_pool:
	panthor_heap_pool_put(heaps);
	return ret;
}

static void group_tiler_oom_work(struct work_struct *work)
{
	struct panthor_group *group =
		container_of(work, struct panthor_group, tiler_oom_work);
	u32 tiler_oom = atomic_xchg(&group->tiler_oom, 0);

	while (tiler_oom) {
		u32 cs_id = ffs(tiler_oom) - 1;

		group_process_tiler_oom(group, cs_id);
		tiler_oom &= ~BIT(cs_id);
	}

	group_put(group);
}

static void
cs_slot_process_tiler_oom_event_locked(struct panthor_device *ptdev,
				       u32 csg_id, u32 cs_id)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
	struct panthor_group *group = csg_slot->group;

	lockdep_assert_held(&sched->lock);

	if (drm_WARN_ON(&ptdev->base, !group))
		return;

	atomic_or(BIT(cs_id), &group->tiler_oom);

	/* We don't use group_queue_work() here because we want to queue the
	 * work item to the heap_alloc_wq.
	 */
	group_get(group);
	if (!queue_work(sched->heap_alloc_wq, &group->tiler_oom_work))
		group_put(group);
}

static bool cs_slot_process_irq_locked(struct panthor_device *ptdev,
				       u32 csg_id, u32 cs_id)
{
	struct panthor_fw_cs_iface *cs_iface;
	u32 req, ack, events;

	lockdep_assert_held(&ptdev->scheduler->lock);

	cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
	req = cs_iface->input->req;
	ack = cs_iface->output->ack;
	events = (req ^ ack) & CS_EVT_MASK;

	if (events & CS_FATAL)
		cs_slot_process_fatal_event_locked(ptdev, csg_id, cs_id);

	if (events & CS_FAULT)
		cs_slot_process_fault_event_locked(ptdev, csg_id, cs_id);

	if (events & CS_TILER_OOM)
		cs_slot_process_tiler_oom_event_locked(ptdev, csg_id, cs_id);

	/* We don't acknowledge the TILER_OOM event since its handling is
	 * deferred to a separate work.
	 */
	panthor_fw_update_reqs(cs_iface, req, ack, CS_FATAL | CS_FAULT);

	return (events & (CS_FAULT | CS_TILER_OOM)) != 0;
}

static void csg_slot_sync_idle_state_locked(struct panthor_device *ptdev, u32 csg_id)
{
	struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
	struct panthor_fw_csg_iface *csg_iface;

	lockdep_assert_held(&ptdev->scheduler->lock);

	csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
	csg_slot->idle = csg_iface->output->status_state & CSG_STATUS_STATE_IS_IDLE;
}

static void csg_slot_process_idle_event_locked(struct panthor_device *ptdev, u32 csg_id)
{
	struct panthor_scheduler *sched = ptdev->scheduler;

	lockdep_assert_held(&sched->lock);

	sched->might_have_idle_groups = true;

	/* Schedule a tick so we can evict idle groups and schedule non-idle
	 * ones. This will also update runtime PM and devfreq busy/idle states,
	 * so the device can lower its frequency or get suspended.
	 */
	sched_queue_delayed_work(sched, tick, 0);
}

static void csg_slot_sync_update_locked(struct panthor_device *ptdev,
					u32 csg_id)
{
	struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
	struct panthor_group *group = csg_slot->group;

	lockdep_assert_held(&ptdev->scheduler->lock);

	if (group)
		group_queue_work(group, sync_upd);

	sched_queue_work(ptdev->scheduler, sync_upd);
}

static void
csg_slot_process_progress_timer_event_locked(struct panthor_device *ptdev, u32 csg_id)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
	struct panthor_group *group = csg_slot->group;

	lockdep_assert_held(&sched->lock);

	drm_warn(&ptdev->base, "CSG slot %d progress timeout\n", csg_id);

	group = csg_slot->group;
	if (!drm_WARN_ON(&ptdev->base, !group))
		group->timedout = true;

	sched_queue_delayed_work(sched, tick, 0);
}

static void sched_process_csg_irq_locked(struct panthor_device *ptdev, u32 csg_id)
{
	u32 req, ack, cs_irq_req, cs_irq_ack, cs_irqs, csg_events;
	struct panthor_fw_csg_iface *csg_iface;
	u32 ring_cs_db_mask = 0;

	lockdep_assert_held(&ptdev->scheduler->lock);

	if (drm_WARN_ON(&ptdev->base, csg_id >= ptdev->scheduler->csg_slot_count))
		return;

	csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
	req = READ_ONCE(csg_iface->input->req);
	ack = READ_ONCE(csg_iface->output->ack);
	cs_irq_req = READ_ONCE(csg_iface->output->cs_irq_req);
	cs_irq_ack = READ_ONCE(csg_iface->input->cs_irq_ack);
	csg_events = (req ^ ack) & CSG_EVT_MASK;

	/* There may not be any pending CSG/CS interrupts to process */
	if (req == ack && cs_irq_req == cs_irq_ack)
		return;

	/* Immediately set IRQ_ACK bits to be same as the IRQ_REQ bits before
	 * examining the CS_ACK & CS_REQ bits. This would ensure that Host
	 * doesn't miss an interrupt for the CS in the race scenario where
	 * whilst Host is servicing an interrupt for the CS, firmware sends
	 * another interrupt for that CS.
	 */
	csg_iface->input->cs_irq_ack = cs_irq_req;

	panthor_fw_update_reqs(csg_iface, req, ack,
			       CSG_SYNC_UPDATE |
			       CSG_IDLE |
			       CSG_PROGRESS_TIMER_EVENT);

	if (csg_events & CSG_IDLE)
		csg_slot_process_idle_event_locked(ptdev, csg_id);

	if (csg_events & CSG_PROGRESS_TIMER_EVENT)
		csg_slot_process_progress_timer_event_locked(ptdev, csg_id);

	cs_irqs = cs_irq_req ^ cs_irq_ack;
	while (cs_irqs) {
		u32 cs_id = ffs(cs_irqs) - 1;

		if (cs_slot_process_irq_locked(ptdev, csg_id, cs_id))
			ring_cs_db_mask |= BIT(cs_id);

		cs_irqs &= ~BIT(cs_id);
	}

	if (csg_events & CSG_SYNC_UPDATE)
		csg_slot_sync_update_locked(ptdev, csg_id);

	if (ring_cs_db_mask)
		panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, ring_cs_db_mask);

	panthor_fw_ring_csg_doorbells(ptdev, BIT(csg_id));
}

static void sched_process_idle_event_locked(struct panthor_device *ptdev)
{
	struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);

	lockdep_assert_held(&ptdev->scheduler->lock);

	/* Acknowledge the idle event and schedule a tick. */
	panthor_fw_update_reqs(glb_iface, req, glb_iface->output->ack, GLB_IDLE);
	sched_queue_delayed_work(ptdev->scheduler, tick, 0);
}

/**
 * sched_process_global_irq_locked() - Process the scheduling part of a global IRQ
 * @ptdev: Device.
 */
static void sched_process_global_irq_locked(struct panthor_device *ptdev)
{
	struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
	u32 req, ack, evts;

	lockdep_assert_held(&ptdev->scheduler->lock);

	req = READ_ONCE(glb_iface->input->req);
	ack = READ_ONCE(glb_iface->output->ack);
	evts = (req ^ ack) & GLB_EVT_MASK;

	if (evts & GLB_IDLE)
		sched_process_idle_event_locked(ptdev);
}

static void process_fw_events_work(struct work_struct *work)
{
	struct panthor_scheduler *sched = container_of(work, struct panthor_scheduler,
						      fw_events_work);
	u32 events = atomic_xchg(&sched->fw_events, 0);
	struct panthor_device *ptdev = sched->ptdev;

	mutex_lock(&sched->lock);

	if (events & JOB_INT_GLOBAL_IF) {
		sched_process_global_irq_locked(ptdev);
		events &= ~JOB_INT_GLOBAL_IF;
	}

	while (events) {
		u32 csg_id = ffs(events) - 1;

		sched_process_csg_irq_locked(ptdev, csg_id);
		events &= ~BIT(csg_id);
	}

	mutex_unlock(&sched->lock);
}

/**
 * panthor_sched_report_fw_events() - Report FW events to the scheduler.
 */
void panthor_sched_report_fw_events(struct panthor_device *ptdev, u32 events)
{
	if (!ptdev->scheduler)
		return;

	atomic_or(events, &ptdev->scheduler->fw_events);
	sched_queue_work(ptdev->scheduler, fw_events);
}

static const char *fence_get_driver_name(struct dma_fence *fence)
{
	return "panthor";
}

static const char *queue_fence_get_timeline_name(struct dma_fence *fence)
{
	return "queue-fence";
}

static const struct dma_fence_ops panthor_queue_fence_ops = {
	.get_driver_name = fence_get_driver_name,
	.get_timeline_name = queue_fence_get_timeline_name,
};

struct panthor_csg_slots_upd_ctx {
	u32 update_mask;
	u32 timedout_mask;
	struct {
		u32 value;
		u32 mask;
	} requests[MAX_CSGS];
};

static void csgs_upd_ctx_init(struct panthor_csg_slots_upd_ctx *ctx)
{
	memset(ctx, 0, sizeof(*ctx));
}

static void csgs_upd_ctx_queue_reqs(struct panthor_device *ptdev,
				    struct panthor_csg_slots_upd_ctx *ctx,
				    u32 csg_id, u32 value, u32 mask)
{
	if (drm_WARN_ON(&ptdev->base, !mask) ||
	    drm_WARN_ON(&ptdev->base, csg_id >= ptdev->scheduler->csg_slot_count))
		return;

	ctx->requests[csg_id].value = (ctx->requests[csg_id].value & ~mask) | (value & mask);
	ctx->requests[csg_id].mask |= mask;
	ctx->update_mask |= BIT(csg_id);
}

static int csgs_upd_ctx_apply_locked(struct panthor_device *ptdev,
				     struct panthor_csg_slots_upd_ctx *ctx)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	u32 update_slots = ctx->update_mask;

	lockdep_assert_held(&sched->lock);

	if (!ctx->update_mask)
		return 0;

	while (update_slots) {
		struct panthor_fw_csg_iface *csg_iface;
		u32 csg_id = ffs(update_slots) - 1;

		update_slots &= ~BIT(csg_id);
		csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
		panthor_fw_update_reqs(csg_iface, req,
				       ctx->requests[csg_id].value,
				       ctx->requests[csg_id].mask);
	}

	panthor_fw_ring_csg_doorbells(ptdev, ctx->update_mask);

	update_slots = ctx->update_mask;
	while (update_slots) {
		struct panthor_fw_csg_iface *csg_iface;
		u32 csg_id = ffs(update_slots) - 1;
		u32 req_mask = ctx->requests[csg_id].mask, acked;
		int ret;

		update_slots &= ~BIT(csg_id);
		csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);

		ret = panthor_fw_csg_wait_acks(ptdev, csg_id, req_mask, &acked, 100);

		if (acked & CSG_ENDPOINT_CONFIG)
			csg_slot_sync_priority_locked(ptdev, csg_id);

		if (acked & CSG_STATE_MASK)
			csg_slot_sync_state_locked(ptdev, csg_id);

		if (acked & CSG_STATUS_UPDATE) {
			csg_slot_sync_queues_state_locked(ptdev, csg_id);
			csg_slot_sync_idle_state_locked(ptdev, csg_id);
		}

		if (ret && acked != req_mask &&
		    ((csg_iface->input->req ^ csg_iface->output->ack) & req_mask) != 0) {
			drm_err(&ptdev->base, "CSG %d update request timedout", csg_id);
			ctx->timedout_mask |= BIT(csg_id);
		}
	}

	if (ctx->timedout_mask)
		return -ETIMEDOUT;

	return 0;
}

struct panthor_sched_tick_ctx {
	struct list_head old_groups[PANTHOR_CSG_PRIORITY_COUNT];
	struct list_head groups[PANTHOR_CSG_PRIORITY_COUNT];
	u32 idle_group_count;
	u32 group_count;
	enum panthor_csg_priority min_priority;
	struct panthor_vm *vms[MAX_CS_PER_CSG];
	u32 as_count;
	bool immediate_tick;
	u32 csg_upd_failed_mask;
};

static bool
tick_ctx_is_full(const struct panthor_scheduler *sched,
		 const struct panthor_sched_tick_ctx *ctx)
{
	return ctx->group_count == sched->csg_slot_count;
}

static bool
group_is_idle(struct panthor_group *group)
{
	struct panthor_device *ptdev = group->ptdev;
	u32 inactive_queues;

	if (group->csg_id >= 0)
		return ptdev->scheduler->csg_slots[group->csg_id].idle;

	inactive_queues = group->idle_queues | group->blocked_queues;
	return hweight32(inactive_queues) == group->queue_count;
}

static bool
group_can_run(struct panthor_group *group)
{
	return group->state != PANTHOR_CS_GROUP_TERMINATED &&
	       group->state != PANTHOR_CS_GROUP_UNKNOWN_STATE &&
	       !group->destroyed && group->fatal_queues == 0 &&
	       !group->timedout;
}

static void
tick_ctx_pick_groups_from_list(const struct panthor_scheduler *sched,
			       struct panthor_sched_tick_ctx *ctx,
			       struct list_head *queue,
			       bool skip_idle_groups,
			       bool owned_by_tick_ctx)
{
	struct panthor_group *group, *tmp;

	if (tick_ctx_is_full(sched, ctx))
		return;

	list_for_each_entry_safe(group, tmp, queue, run_node) {
		u32 i;

		if (!group_can_run(group))
			continue;

		if (skip_idle_groups && group_is_idle(group))
			continue;

		for (i = 0; i < ctx->as_count; i++) {
			if (ctx->vms[i] == group->vm)
				break;
		}

		if (i == ctx->as_count && ctx->as_count == sched->as_slot_count)
			continue;

		if (!owned_by_tick_ctx)
			group_get(group);

		list_move_tail(&group->run_node, &ctx->groups[group->priority]);
		ctx->group_count++;
		if (group_is_idle(group))
			ctx->idle_group_count++;

		if (i == ctx->as_count)
			ctx->vms[ctx->as_count++] = group->vm;

		if (ctx->min_priority > group->priority)
			ctx->min_priority = group->priority;

		if (tick_ctx_is_full(sched, ctx))
			return;
	}
}

static void
tick_ctx_insert_old_group(struct panthor_scheduler *sched,
			  struct panthor_sched_tick_ctx *ctx,
			  struct panthor_group *group,
			  bool full_tick)
{
	struct panthor_csg_slot *csg_slot = &sched->csg_slots[group->csg_id];
	struct panthor_group *other_group;

	if (!full_tick) {
		list_add_tail(&group->run_node, &ctx->old_groups[group->priority]);
		return;
	}

	/* Rotate to make sure groups with lower CSG slot
	 * priorities have a chance to get a higher CSG slot
	 * priority next time they get picked. This priority
	 * has an impact on resource request ordering, so it's
	 * important to make sure we don't let one group starve
	 * all other groups with the same group priority.
	 */
	list_for_each_entry(other_group,
			    &ctx->old_groups[csg_slot->group->priority],
			    run_node) {
		struct panthor_csg_slot *other_csg_slot = &sched->csg_slots[other_group->csg_id];

		if (other_csg_slot->priority > csg_slot->priority) {
			list_add_tail(&csg_slot->group->run_node, &other_group->run_node);
			return;
		}
	}

	list_add_tail(&group->run_node, &ctx->old_groups[group->priority]);
}

static void
tick_ctx_init(struct panthor_scheduler *sched,
	      struct panthor_sched_tick_ctx *ctx,
	      bool full_tick)
{
	struct panthor_device *ptdev = sched->ptdev;
	struct panthor_csg_slots_upd_ctx upd_ctx;
	int ret;
	u32 i;

	memset(ctx, 0, sizeof(*ctx));
	csgs_upd_ctx_init(&upd_ctx);

	ctx->min_priority = PANTHOR_CSG_PRIORITY_COUNT;
	for (i = 0; i < ARRAY_SIZE(ctx->groups); i++) {
		INIT_LIST_HEAD(&ctx->groups[i]);
		INIT_LIST_HEAD(&ctx->old_groups[i]);
	}

	for (i = 0; i < sched->csg_slot_count; i++) {
		struct panthor_csg_slot *csg_slot = &sched->csg_slots[i];
		struct panthor_group *group = csg_slot->group;
		struct panthor_fw_csg_iface *csg_iface;

		if (!group)
			continue;

		csg_iface = panthor_fw_get_csg_iface(ptdev, i);
		group_get(group);

		/* If there was unhandled faults on the VM, force processing of
		 * CSG IRQs, so we can flag the faulty queue.
		 */
		if (panthor_vm_has_unhandled_faults(group->vm)) {
			sched_process_csg_irq_locked(ptdev, i);

			/* No fatal fault reported, flag all queues as faulty. */
			if (!group->fatal_queues)
				group->fatal_queues |= GENMASK(group->queue_count - 1, 0);
		}

		tick_ctx_insert_old_group(sched, ctx, group, full_tick);
		csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, i,
					csg_iface->output->ack ^ CSG_STATUS_UPDATE,
					CSG_STATUS_UPDATE);
	}

	ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
	if (ret) {
		panthor_device_schedule_reset(ptdev);
		ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask;
	}
}

#define NUM_INSTRS_PER_SLOT		16

static void
group_term_post_processing(struct panthor_group *group)
{
	struct panthor_job *job, *tmp;
	LIST_HEAD(faulty_jobs);
	bool cookie;
	u32 i = 0;

	if (drm_WARN_ON(&group->ptdev->base, group_can_run(group)))
		return;

	cookie = dma_fence_begin_signalling();
	for (i = 0; i < group->queue_count; i++) {
		struct panthor_queue *queue = group->queues[i];
		struct panthor_syncobj_64b *syncobj;
		int err;

		if (group->fatal_queues & BIT(i))
			err = -EINVAL;
		else if (group->timedout)
			err = -ETIMEDOUT;
		else
			err = -ECANCELED;

		if (!queue)
			continue;

		spin_lock(&queue->fence_ctx.lock);
		list_for_each_entry_safe(job, tmp, &queue->fence_ctx.in_flight_jobs, node) {
			list_move_tail(&job->node, &faulty_jobs);
			dma_fence_set_error(job->done_fence, err);
			dma_fence_signal_locked(job->done_fence);
		}
		spin_unlock(&queue->fence_ctx.lock);

		/* Manually update the syncobj seqno to unblock waiters. */
		syncobj = group->syncobjs->kmap + (i * sizeof(*syncobj));
		syncobj->status = ~0;
		syncobj->seqno = atomic64_read(&queue->fence_ctx.seqno);
		sched_queue_work(group->ptdev->scheduler, sync_upd);
	}
	dma_fence_end_signalling(cookie);

	list_for_each_entry_safe(job, tmp, &faulty_jobs, node) {
		list_del_init(&job->node);
		panthor_job_put(&job->base);
	}
}

static void group_term_work(struct work_struct *work)
{
	struct panthor_group *group =
		container_of(work, struct panthor_group, term_work);

	group_term_post_processing(group);
	group_put(group);
}

static void
tick_ctx_cleanup(struct panthor_scheduler *sched,
		 struct panthor_sched_tick_ctx *ctx)
{
	struct panthor_group *group, *tmp;
	u32 i;

	for (i = 0; i < ARRAY_SIZE(ctx->old_groups); i++) {
		list_for_each_entry_safe(group, tmp, &ctx->old_groups[i], run_node) {
			/* If everything went fine, we should only have groups
			 * to be terminated in the old_groups lists.
			 */
			drm_WARN_ON(&group->ptdev->base, !ctx->csg_upd_failed_mask &&
				    group_can_run(group));

			if (!group_can_run(group)) {
				list_del_init(&group->run_node);
				list_del_init(&group->wait_node);
				group_queue_work(group, term);
			} else if (group->csg_id >= 0) {
				list_del_init(&group->run_node);
			} else {
				list_move(&group->run_node,
					  group_is_idle(group) ?
					  &sched->groups.idle[group->priority] :
					  &sched->groups.runnable[group->priority]);
			}
			group_put(group);
		}
	}

	for (i = 0; i < ARRAY_SIZE(ctx->groups); i++) {
		/* If everything went fine, the groups to schedule lists should
		 * be empty.
		 */
		drm_WARN_ON(&group->ptdev->base,
			    !ctx->csg_upd_failed_mask && !list_empty(&ctx->groups[i]));

		list_for_each_entry_safe(group, tmp, &ctx->groups[i], run_node) {
			if (group->csg_id >= 0) {
				list_del_init(&group->run_node);
			} else {
				list_move(&group->run_node,
					  group_is_idle(group) ?
					  &sched->groups.idle[group->priority] :
					  &sched->groups.runnable[group->priority]);
			}
			group_put(group);
		}
	}
}

static void
tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *ctx)
{
	struct panthor_group *group, *tmp;
	struct panthor_device *ptdev = sched->ptdev;
	struct panthor_csg_slot *csg_slot;
	int prio, new_csg_prio = MAX_CSG_PRIO, i;
	u32 free_csg_slots = 0;
	struct panthor_csg_slots_upd_ctx upd_ctx;
	int ret;

	csgs_upd_ctx_init(&upd_ctx);

	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
		/* Suspend or terminate evicted groups. */
		list_for_each_entry(group, &ctx->old_groups[prio], run_node) {
			bool term = !group_can_run(group);
			int csg_id = group->csg_id;

			if (drm_WARN_ON(&ptdev->base, csg_id < 0))
				continue;

			csg_slot = &sched->csg_slots[csg_id];
			csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
						term ? CSG_STATE_TERMINATE : CSG_STATE_SUSPEND,
						CSG_STATE_MASK);
		}

		/* Update priorities on already running groups. */
		list_for_each_entry(group, &ctx->groups[prio], run_node) {
			struct panthor_fw_csg_iface *csg_iface;
			int csg_id = group->csg_id;

			if (csg_id < 0) {
				new_csg_prio--;
				continue;
			}

			csg_slot = &sched->csg_slots[csg_id];
			csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
			if (csg_slot->priority == new_csg_prio) {
				new_csg_prio--;
				continue;
			}

			panthor_fw_update_reqs(csg_iface, endpoint_req,
					       CSG_EP_REQ_PRIORITY(new_csg_prio),
					       CSG_EP_REQ_PRIORITY_MASK);
			csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
						csg_iface->output->ack ^ CSG_ENDPOINT_CONFIG,
						CSG_ENDPOINT_CONFIG);
			new_csg_prio--;
		}
	}

	ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
	if (ret) {
		panthor_device_schedule_reset(ptdev);
		ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask;
		return;
	}

	/* Unbind evicted groups. */
	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
		list_for_each_entry(group, &ctx->old_groups[prio], run_node) {
			/* This group is gone. Process interrupts to clear
			 * any pending interrupts before we start the new
			 * group.
			 */
			if (group->csg_id >= 0)
				sched_process_csg_irq_locked(ptdev, group->csg_id);

			group_unbind_locked(group);
		}
	}

	for (i = 0; i < sched->csg_slot_count; i++) {
		if (!sched->csg_slots[i].group)
			free_csg_slots |= BIT(i);
	}

	csgs_upd_ctx_init(&upd_ctx);
	new_csg_prio = MAX_CSG_PRIO;

	/* Start new groups. */
	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
		list_for_each_entry(group, &ctx->groups[prio], run_node) {
			int csg_id = group->csg_id;
			struct panthor_fw_csg_iface *csg_iface;

			if (csg_id >= 0) {
				new_csg_prio--;
				continue;
			}

			csg_id = ffs(free_csg_slots) - 1;
			if (drm_WARN_ON(&ptdev->base, csg_id < 0))
				break;

			csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
			csg_slot = &sched->csg_slots[csg_id];
			group_bind_locked(group, csg_id);
			csg_slot_prog_locked(ptdev, csg_id, new_csg_prio--);
			csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
						group->state == PANTHOR_CS_GROUP_SUSPENDED ?
						CSG_STATE_RESUME : CSG_STATE_START,
						CSG_STATE_MASK);
			csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
						csg_iface->output->ack ^ CSG_ENDPOINT_CONFIG,
						CSG_ENDPOINT_CONFIG);
			free_csg_slots &= ~BIT(csg_id);
		}
	}

	ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
	if (ret) {
		panthor_device_schedule_reset(ptdev);
		ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask;
		return;
	}

	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
		list_for_each_entry_safe(group, tmp, &ctx->groups[prio], run_node) {
			list_del_init(&group->run_node);

			/* If the group has been destroyed while we were
			 * scheduling, ask for an immediate tick to
			 * re-evaluate as soon as possible and get rid of
			 * this dangling group.
			 */
			if (group->destroyed)
				ctx->immediate_tick = true;
			group_put(group);
		}

		/* Return evicted groups to the idle or run queues. Groups
		 * that can no longer be run (because they've been destroyed
		 * or experienced an unrecoverable error) will be scheduled
		 * for destruction in tick_ctx_cleanup().
		 */
		list_for_each_entry_safe(group, tmp, &ctx->old_groups[prio], run_node) {
			if (!group_can_run(group))
				continue;

			if (group_is_idle(group))
				list_move_tail(&group->run_node, &sched->groups.idle[prio]);
			else
				list_move_tail(&group->run_node, &sched->groups.runnable[prio]);
			group_put(group);
		}
	}

	sched->used_csg_slot_count = ctx->group_count;
	sched->might_have_idle_groups = ctx->idle_group_count > 0;
}

static u64
tick_ctx_update_resched_target(struct panthor_scheduler *sched,
			       const struct panthor_sched_tick_ctx *ctx)
{
	/* We had space left, no need to reschedule until some external event happens. */
	if (!tick_ctx_is_full(sched, ctx))
		goto no_tick;

	/* If idle groups were scheduled, no need to wake up until some external
	 * event happens (group unblocked, new job submitted, ...).
	 */
	if (ctx->idle_group_count)
		goto no_tick;

	if (drm_WARN_ON(&sched->ptdev->base, ctx->min_priority >= PANTHOR_CSG_PRIORITY_COUNT))
		goto no_tick;

	/* If there are groups of the same priority waiting, we need to
	 * keep the scheduler ticking, otherwise, we'll just wait for
	 * new groups with higher priority to be queued.
	 */
	if (!list_empty(&sched->groups.runnable[ctx->min_priority])) {
		u64 resched_target = sched->last_tick + sched->tick_period;

		if (time_before64(sched->resched_target, sched->last_tick) ||
		    time_before64(resched_target, sched->resched_target))
			sched->resched_target = resched_target;

		return sched->resched_target - sched->last_tick;
	}

no_tick:
	sched->resched_target = U64_MAX;
	return U64_MAX;
}

static void tick_work(struct work_struct *work)
{
	struct panthor_scheduler *sched = container_of(work, struct panthor_scheduler,
						      tick_work.work);
	struct panthor_device *ptdev = sched->ptdev;
	struct panthor_sched_tick_ctx ctx;
	u64 remaining_jiffies = 0, resched_delay;
	u64 now = get_jiffies_64();
	int prio, ret, cookie;

	if (!drm_dev_enter(&ptdev->base, &cookie))
		return;

	ret = pm_runtime_resume_and_get(ptdev->base.dev);
	if (drm_WARN_ON(&ptdev->base, ret))
		goto out_dev_exit;

	if (time_before64(now, sched->resched_target))
		remaining_jiffies = sched->resched_target - now;

	mutex_lock(&sched->lock);
	if (panthor_device_reset_is_pending(sched->ptdev))
		goto out_unlock;

	tick_ctx_init(sched, &ctx, remaining_jiffies != 0);
	if (ctx.csg_upd_failed_mask)
		goto out_cleanup_ctx;

	if (remaining_jiffies) {
		/* Scheduling forced in the middle of a tick. Only RT groups
		 * can preempt non-RT ones. Currently running RT groups can't be
		 * preempted.
		 */
		for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1;
		     prio >= 0 && !tick_ctx_is_full(sched, &ctx);
		     prio--) {
			tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio],
						       true, true);
			if (prio == PANTHOR_CSG_PRIORITY_RT) {
				tick_ctx_pick_groups_from_list(sched, &ctx,
							       &sched->groups.runnable[prio],
							       true, false);
			}
		}
	}

	/* First pick non-idle groups */
	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1;
	     prio >= 0 && !tick_ctx_is_full(sched, &ctx);
	     prio--) {
		tick_ctx_pick_groups_from_list(sched, &ctx, &sched->groups.runnable[prio],
					       true, false);
		tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], true, true);
	}

	/* If we have free CSG slots left, pick idle groups */
	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1;
	     prio >= 0 && !tick_ctx_is_full(sched, &ctx);
	     prio--) {
		/* Check the old_group queue first to avoid reprogramming the slots */
		tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], false, true);
		tick_ctx_pick_groups_from_list(sched, &ctx, &sched->groups.idle[prio],
					       false, false);
	}

	tick_ctx_apply(sched, &ctx);
	if (ctx.csg_upd_failed_mask)
		goto out_cleanup_ctx;

	if (ctx.idle_group_count == ctx.group_count) {
		panthor_devfreq_record_idle(sched->ptdev);
		if (sched->pm.has_ref) {
			pm_runtime_put_autosuspend(ptdev->base.dev);
			sched->pm.has_ref = false;
		}
	} else {
		panthor_devfreq_record_busy(sched->ptdev);
		if (!sched->pm.has_ref) {
			pm_runtime_get(ptdev->base.dev);
			sched->pm.has_ref = true;
		}
	}

	sched->last_tick = now;
	resched_delay = tick_ctx_update_resched_target(sched, &ctx);
	if (ctx.immediate_tick)
		resched_delay = 0;

	if (resched_delay != U64_MAX)
		sched_queue_delayed_work(sched, tick, resched_delay);

out_cleanup_ctx:
	tick_ctx_cleanup(sched, &ctx);

out_unlock:
	mutex_unlock(&sched->lock);
	pm_runtime_mark_last_busy(ptdev->base.dev);
	pm_runtime_put_autosuspend(ptdev->base.dev);

out_dev_exit:
	drm_dev_exit(cookie);
}

static int panthor_queue_eval_syncwait(struct panthor_group *group, u8 queue_idx)
{
	struct panthor_queue *queue = group->queues[queue_idx];
	union {
		struct panthor_syncobj_64b sync64;
		struct panthor_syncobj_32b sync32;
	} *syncobj;
	bool result;
	u64 value;

	syncobj = panthor_queue_get_syncwait_obj(group, queue);
	if (!syncobj)
		return -EINVAL;

	value = queue->syncwait.sync64 ?
		syncobj->sync64.seqno :
		syncobj->sync32.seqno;

	if (queue->syncwait.gt)
		result = value > queue->syncwait.ref;
	else
		result = value <= queue->syncwait.ref;

	if (result)
		panthor_queue_put_syncwait_obj(queue);

	return result;
}

static void sync_upd_work(struct work_struct *work)
{
	struct panthor_scheduler *sched = container_of(work,
						      struct panthor_scheduler,
						      sync_upd_work);
	struct panthor_group *group, *tmp;
	bool immediate_tick = false;

	mutex_lock(&sched->lock);
	list_for_each_entry_safe(group, tmp, &sched->groups.waiting, wait_node) {
		u32 tested_queues = group->blocked_queues;
		u32 unblocked_queues = 0;

		while (tested_queues) {
			u32 cs_id = ffs(tested_queues) - 1;
			int ret;

			ret = panthor_queue_eval_syncwait(group, cs_id);
			drm_WARN_ON(&group->ptdev->base, ret < 0);
			if (ret)
				unblocked_queues |= BIT(cs_id);

			tested_queues &= ~BIT(cs_id);
		}

		if (unblocked_queues) {
			group->blocked_queues &= ~unblocked_queues;

			if (group->csg_id < 0) {
				list_move(&group->run_node,
					  &sched->groups.runnable[group->priority]);
				if (group->priority == PANTHOR_CSG_PRIORITY_RT)
					immediate_tick = true;
			}
		}

		if (!group->blocked_queues)
			list_del_init(&group->wait_node);
	}
	mutex_unlock(&sched->lock);

	if (immediate_tick)
		sched_queue_delayed_work(sched, tick, 0);
}

static void group_schedule_locked(struct panthor_group *group, u32 queue_mask)
{
	struct panthor_device *ptdev = group->ptdev;
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct list_head *queue = &sched->groups.runnable[group->priority];
	u64 delay_jiffies = 0;
	bool was_idle;
	u64 now;

	if (!group_can_run(group))
		return;

	/* All updated queues are blocked, no need to wake up the scheduler. */
	if ((queue_mask & group->blocked_queues) == queue_mask)
		return;

	was_idle = group_is_idle(group);
	group->idle_queues &= ~queue_mask;

	/* Don't mess up with the lists if we're in a middle of a reset. */
	if (atomic_read(&sched->reset.in_progress))
		return;

	if (was_idle && !group_is_idle(group))
		list_move_tail(&group->run_node, queue);

	/* RT groups are preemptive. */
	if (group->priority == PANTHOR_CSG_PRIORITY_RT) {
		sched_queue_delayed_work(sched, tick, 0);
		return;
	}

	/* Some groups might be idle, force an immediate tick to
	 * re-evaluate.
	 */
	if (sched->might_have_idle_groups) {
		sched_queue_delayed_work(sched, tick, 0);
		return;
	}

	/* Scheduler is ticking, nothing to do. */
	if (sched->resched_target != U64_MAX) {
		/* If there are free slots, force immediating ticking. */
		if (sched->used_csg_slot_count < sched->csg_slot_count)
			sched_queue_delayed_work(sched, tick, 0);

		return;
	}

	/* Scheduler tick was off, recalculate the resched_target based on the
	 * last tick event, and queue the scheduler work.
	 */
	now = get_jiffies_64();
	sched->resched_target = sched->last_tick + sched->tick_period;
	if (sched->used_csg_slot_count == sched->csg_slot_count &&
	    time_before64(now, sched->resched_target))
		delay_jiffies = min_t(unsigned long, sched->resched_target - now, ULONG_MAX);

	sched_queue_delayed_work(sched, tick, delay_jiffies);
}

static void queue_stop(struct panthor_queue *queue,
		       struct panthor_job *bad_job)
{
	drm_sched_stop(&queue->scheduler, bad_job ? &bad_job->base : NULL);
}

static void queue_start(struct panthor_queue *queue)
{
	struct panthor_job *job;

	/* Re-assign the parent fences. */
	list_for_each_entry(job, &queue->scheduler.pending_list, base.list)
		job->base.s_fence->parent = dma_fence_get(job->done_fence);

	drm_sched_start(&queue->scheduler, true);
}

static void panthor_group_stop(struct panthor_group *group)
{
	struct panthor_scheduler *sched = group->ptdev->scheduler;

	lockdep_assert_held(&sched->reset.lock);

	for (u32 i = 0; i < group->queue_count; i++)
		queue_stop(group->queues[i], NULL);

	group_get(group);
	list_move_tail(&group->run_node, &sched->reset.stopped_groups);
}

static void panthor_group_start(struct panthor_group *group)
{
	struct panthor_scheduler *sched = group->ptdev->scheduler;

	lockdep_assert_held(&group->ptdev->scheduler->reset.lock);

	for (u32 i = 0; i < group->queue_count; i++)
		queue_start(group->queues[i]);

	if (group_can_run(group)) {
		list_move_tail(&group->run_node,
			       group_is_idle(group) ?
			       &sched->groups.idle[group->priority] :
			       &sched->groups.runnable[group->priority]);
	} else {
		list_del_init(&group->run_node);
		list_del_init(&group->wait_node);
		group_queue_work(group, term);
	}

	group_put(group);
}

static void panthor_sched_immediate_tick(struct panthor_device *ptdev)
{
	struct panthor_scheduler *sched = ptdev->scheduler;

	sched_queue_delayed_work(sched, tick, 0);
}

/**
 * panthor_sched_report_mmu_fault() - Report MMU faults to the scheduler.
 */
void panthor_sched_report_mmu_fault(struct panthor_device *ptdev)
{
	/* Force a tick to immediately kill faulty groups. */
	if (ptdev->scheduler)
		panthor_sched_immediate_tick(ptdev);
}

void panthor_sched_resume(struct panthor_device *ptdev)
{
	/* Force a tick to re-evaluate after a resume. */
	panthor_sched_immediate_tick(ptdev);
}

void panthor_sched_suspend(struct panthor_device *ptdev)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_csg_slots_upd_ctx upd_ctx;
	struct panthor_group *group;
	u32 suspended_slots;
	u32 i;

	mutex_lock(&sched->lock);
	csgs_upd_ctx_init(&upd_ctx);
	for (i = 0; i < sched->csg_slot_count; i++) {
		struct panthor_csg_slot *csg_slot = &sched->csg_slots[i];

		if (csg_slot->group) {
			csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, i,
						group_can_run(csg_slot->group) ?
						CSG_STATE_SUSPEND : CSG_STATE_TERMINATE,
						CSG_STATE_MASK);
		}
	}

	suspended_slots = upd_ctx.update_mask;

	csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
	suspended_slots &= ~upd_ctx.timedout_mask;

	if (upd_ctx.timedout_mask) {
		u32 slot_mask = upd_ctx.timedout_mask;

		drm_err(&ptdev->base, "CSG suspend failed, escalating to termination");
		csgs_upd_ctx_init(&upd_ctx);
		while (slot_mask) {
			u32 csg_id = ffs(slot_mask) - 1;

			csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
						CSG_STATE_TERMINATE,
						CSG_STATE_MASK);
			slot_mask &= ~BIT(csg_id);
		}

		csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);

		slot_mask = upd_ctx.timedout_mask;
		while (slot_mask) {
			u32 csg_id = ffs(slot_mask) - 1;
			struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];

			/* Terminate command timedout, but the soft-reset will
			 * automatically terminate all active groups, so let's
			 * force the state to halted here.
			 */
			if (csg_slot->group->state != PANTHOR_CS_GROUP_TERMINATED)
				csg_slot->group->state = PANTHOR_CS_GROUP_TERMINATED;
			slot_mask &= ~BIT(csg_id);
		}
	}

	/* Flush L2 and LSC caches to make sure suspend state is up-to-date.
	 * If the flush fails, flag all queues for termination.
	 */
	if (suspended_slots) {
		bool flush_caches_failed = false;
		u32 slot_mask = suspended_slots;

		if (panthor_gpu_flush_caches(ptdev, CACHE_CLEAN, CACHE_CLEAN, 0))
			flush_caches_failed = true;

		while (slot_mask) {
			u32 csg_id = ffs(slot_mask) - 1;
			struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];

			if (flush_caches_failed)
				csg_slot->group->state = PANTHOR_CS_GROUP_TERMINATED;
			else
				csg_slot_sync_update_locked(ptdev, csg_id);

			slot_mask &= ~BIT(csg_id);
		}
	}

	for (i = 0; i < sched->csg_slot_count; i++) {
		struct panthor_csg_slot *csg_slot = &sched->csg_slots[i];

		group = csg_slot->group;
		if (!group)
			continue;

		group_get(group);

		if (group->csg_id >= 0)
			sched_process_csg_irq_locked(ptdev, group->csg_id);

		group_unbind_locked(group);

		drm_WARN_ON(&group->ptdev->base, !list_empty(&group->run_node));

		if (group_can_run(group)) {
			list_add(&group->run_node,
				 &sched->groups.idle[group->priority]);
		} else {
			/* We don't bother stopping the scheduler if the group is
			 * faulty, the group termination work will finish the job.
			 */
			list_del_init(&group->wait_node);
			group_queue_work(group, term);
		}
		group_put(group);
	}
	mutex_unlock(&sched->lock);
}

void panthor_sched_pre_reset(struct panthor_device *ptdev)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_group *group, *group_tmp;
	u32 i;

	mutex_lock(&sched->reset.lock);
	atomic_set(&sched->reset.in_progress, true);

	/* Cancel all scheduler works. Once this is done, these works can't be
	 * scheduled again until the reset operation is complete.
	 */
	cancel_work_sync(&sched->sync_upd_work);
	cancel_delayed_work_sync(&sched->tick_work);

	panthor_sched_suspend(ptdev);

	/* Stop all groups that might still accept jobs, so we don't get passed
	 * new jobs while we're resetting.
	 */
	for (i = 0; i < ARRAY_SIZE(sched->groups.runnable); i++) {
		/* All groups should be in the idle lists. */
		drm_WARN_ON(&ptdev->base, !list_empty(&sched->groups.runnable[i]));
		list_for_each_entry_safe(group, group_tmp, &sched->groups.runnable[i], run_node)
			panthor_group_stop(group);
	}

	for (i = 0; i < ARRAY_SIZE(sched->groups.idle); i++) {
		list_for_each_entry_safe(group, group_tmp, &sched->groups.idle[i], run_node)
			panthor_group_stop(group);
	}

	mutex_unlock(&sched->reset.lock);
}

void panthor_sched_post_reset(struct panthor_device *ptdev, bool reset_failed)
{
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_group *group, *group_tmp;

	mutex_lock(&sched->reset.lock);

	list_for_each_entry_safe(group, group_tmp, &sched->reset.stopped_groups, run_node) {
		/* Consider all previously running group as terminated if the
		 * reset failed.
		 */
		if (reset_failed)
			group->state = PANTHOR_CS_GROUP_TERMINATED;

		panthor_group_start(group);
	}

	/* We're done resetting the GPU, clear the reset.in_progress bit so we can
	 * kick the scheduler.
	 */
	atomic_set(&sched->reset.in_progress, false);
	mutex_unlock(&sched->reset.lock);

	/* No need to queue a tick and update syncs if the reset failed. */
	if (!reset_failed) {
		sched_queue_delayed_work(sched, tick, 0);
		sched_queue_work(sched, sync_upd);
	}
}

static void group_sync_upd_work(struct work_struct *work)
{
	struct panthor_group *group =
		container_of(work, struct panthor_group, sync_upd_work);
	struct panthor_job *job, *job_tmp;
	LIST_HEAD(done_jobs);
	u32 queue_idx;
	bool cookie;

	cookie = dma_fence_begin_signalling();
	for (queue_idx = 0; queue_idx < group->queue_count; queue_idx++) {
		struct panthor_queue *queue = group->queues[queue_idx];
		struct panthor_syncobj_64b *syncobj;

		if (!queue)
			continue;

		syncobj = group->syncobjs->kmap + (queue_idx * sizeof(*syncobj));

		spin_lock(&queue->fence_ctx.lock);
		list_for_each_entry_safe(job, job_tmp, &queue->fence_ctx.in_flight_jobs, node) {
			if (syncobj->seqno < job->done_fence->seqno)
				break;

			list_move_tail(&job->node, &done_jobs);
			dma_fence_signal_locked(job->done_fence);
		}
		spin_unlock(&queue->fence_ctx.lock);
	}
	dma_fence_end_signalling(cookie);

	list_for_each_entry_safe(job, job_tmp, &done_jobs, node) {
		list_del_init(&job->node);
		panthor_job_put(&job->base);
	}

	group_put(group);
}

static struct dma_fence *
queue_run_job(struct drm_sched_job *sched_job)
{
	struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
	struct panthor_group *group = job->group;
	struct panthor_queue *queue = group->queues[job->queue_idx];
	struct panthor_device *ptdev = group->ptdev;
	struct panthor_scheduler *sched = ptdev->scheduler;
	u32 ringbuf_size = panthor_kernel_bo_size(queue->ringbuf);
	u32 ringbuf_insert = queue->iface.input->insert & (ringbuf_size - 1);
	u64 addr_reg = ptdev->csif_info.cs_reg_count -
		       ptdev->csif_info.unpreserved_cs_reg_count;
	u64 val_reg = addr_reg + 2;
	u64 sync_addr = panthor_kernel_bo_gpuva(group->syncobjs) +
			job->queue_idx * sizeof(struct panthor_syncobj_64b);
	u32 waitall_mask = GENMASK(sched->sb_slot_count - 1, 0);
	struct dma_fence *done_fence;
	int ret;

	u64 call_instrs[NUM_INSTRS_PER_SLOT] = {
		/* MOV32 rX+2, cs.latest_flush */
		(2ull << 56) | (val_reg << 48) | job->call_info.latest_flush,

		/* FLUSH_CACHE2.clean_inv_all.no_wait.signal(0) rX+2 */
		(36ull << 56) | (0ull << 48) | (val_reg << 40) | (0 << 16) | 0x233,

		/* MOV48 rX:rX+1, cs.start */
		(1ull << 56) | (addr_reg << 48) | job->call_info.start,

		/* MOV32 rX+2, cs.size */
		(2ull << 56) | (val_reg << 48) | job->call_info.size,

		/* WAIT(0) => waits for FLUSH_CACHE2 instruction */
		(3ull << 56) | (1 << 16),

		/* CALL rX:rX+1, rX+2 */
		(32ull << 56) | (addr_reg << 40) | (val_reg << 32),

		/* MOV48 rX:rX+1, sync_addr */
		(1ull << 56) | (addr_reg << 48) | sync_addr,

		/* MOV48 rX+2, #1 */
		(1ull << 56) | (val_reg << 48) | 1,

		/* WAIT(all) */
		(3ull << 56) | (waitall_mask << 16),

		/* SYNC_ADD64.system_scope.propage_err.nowait rX:rX+1, rX+2*/
		(51ull << 56) | (0ull << 48) | (addr_reg << 40) | (val_reg << 32) | (0 << 16) | 1,

		/* ERROR_BARRIER, so we can recover from faults at job
		 * boundaries.
		 */
		(47ull << 56),
	};

	/* Need to be cacheline aligned to please the prefetcher. */
	static_assert(sizeof(call_instrs) % 64 == 0,
		      "call_instrs is not aligned on a cacheline");

	/* Stream size is zero, nothing to do except making sure all previously
	 * submitted jobs are done before we signal the
	 * drm_sched_job::s_fence::finished fence.
	 */
	if (!job->call_info.size) {
		job->done_fence = dma_fence_get(queue->fence_ctx.last_fence);
		return dma_fence_get(job->done_fence);
	}

	ret = pm_runtime_resume_and_get(ptdev->base.dev);
	if (drm_WARN_ON(&ptdev->base, ret))
		return ERR_PTR(ret);

	mutex_lock(&sched->lock);
	if (!group_can_run(group)) {
		done_fence = ERR_PTR(-ECANCELED);
		goto out_unlock;
	}

	dma_fence_init(job->done_fence,
		       &panthor_queue_fence_ops,
		       &queue->fence_ctx.lock,
		       queue->fence_ctx.id,
		       atomic64_inc_return(&queue->fence_ctx.seqno));

	memcpy(queue->ringbuf->kmap + ringbuf_insert,
	       call_instrs, sizeof(call_instrs));

	panthor_job_get(&job->base);
	spin_lock(&queue->fence_ctx.lock);
	list_add_tail(&job->node, &queue->fence_ctx.in_flight_jobs);
	spin_unlock(&queue->fence_ctx.lock);

	job->ringbuf.start = queue->iface.input->insert;
	job->ringbuf.end = job->ringbuf.start + sizeof(call_instrs);

	/* Make sure the ring buffer is updated before the INSERT
	 * register.
	 */
	wmb();

	queue->iface.input->extract = queue->iface.output->extract;
	queue->iface.input->insert = job->ringbuf.end;

	if (group->csg_id < 0) {
		/* If the queue is blocked, we want to keep the timeout running, so we
		 * can detect unbounded waits and kill the group when that happens.
		 * Otherwise, we suspend the timeout so the time we spend waiting for
		 * a CSG slot is not counted.
		 */
		if (!(group->blocked_queues & BIT(job->queue_idx)) &&
		    !queue->timeout_suspended) {
			queue->remaining_time = drm_sched_suspend_timeout(&queue->scheduler);
			queue->timeout_suspended = true;
		}

		group_schedule_locked(group, BIT(job->queue_idx));
	} else {
		gpu_write(ptdev, CSF_DOORBELL(queue->doorbell_id), 1);
		if (!sched->pm.has_ref &&
		    !(group->blocked_queues & BIT(job->queue_idx))) {
			pm_runtime_get(ptdev->base.dev);
			sched->pm.has_ref = true;
		}
	}

	/* Update the last fence. */
	dma_fence_put(queue->fence_ctx.last_fence);
	queue->fence_ctx.last_fence = dma_fence_get(job->done_fence);

	done_fence = dma_fence_get(job->done_fence);

out_unlock:
	mutex_unlock(&sched->lock);
	pm_runtime_mark_last_busy(ptdev->base.dev);
	pm_runtime_put_autosuspend(ptdev->base.dev);

	return done_fence;
}

static enum drm_gpu_sched_stat
queue_timedout_job(struct drm_sched_job *sched_job)
{
	struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
	struct panthor_group *group = job->group;
	struct panthor_device *ptdev = group->ptdev;
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_queue *queue = group->queues[job->queue_idx];

	drm_warn(&ptdev->base, "job timeout\n");

	drm_WARN_ON(&ptdev->base, atomic_read(&sched->reset.in_progress));

	queue_stop(queue, job);

	mutex_lock(&sched->lock);
	group->timedout = true;
	if (group->csg_id >= 0) {
		sched_queue_delayed_work(ptdev->scheduler, tick, 0);
	} else {
		/* Remove from the run queues, so the scheduler can't
		 * pick the group on the next tick.
		 */
		list_del_init(&group->run_node);
		list_del_init(&group->wait_node);

		group_queue_work(group, term);
	}
	mutex_unlock(&sched->lock);

	queue_start(queue);

	return DRM_GPU_SCHED_STAT_NOMINAL;
}

static void queue_free_job(struct drm_sched_job *sched_job)
{
	drm_sched_job_cleanup(sched_job);
	panthor_job_put(sched_job);
}

static const struct drm_sched_backend_ops panthor_queue_sched_ops = {
	.run_job = queue_run_job,
	.timedout_job = queue_timedout_job,
	.free_job = queue_free_job,
};

static struct panthor_queue *
group_create_queue(struct panthor_group *group,
		   const struct drm_panthor_queue_create *args)
{
	struct drm_gpu_scheduler *drm_sched;
	struct panthor_queue *queue;
	int ret;

	if (args->pad[0] || args->pad[1] || args->pad[2])
		return ERR_PTR(-EINVAL);

	if (args->ringbuf_size < SZ_4K || args->ringbuf_size > SZ_64K ||
	    !is_power_of_2(args->ringbuf_size))
		return ERR_PTR(-EINVAL);

	if (args->priority > CSF_MAX_QUEUE_PRIO)
		return ERR_PTR(-EINVAL);

	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
	if (!queue)
		return ERR_PTR(-ENOMEM);

	queue->fence_ctx.id = dma_fence_context_alloc(1);
	spin_lock_init(&queue->fence_ctx.lock);
	INIT_LIST_HEAD(&queue->fence_ctx.in_flight_jobs);

	queue->priority = args->priority;

	queue->ringbuf = panthor_kernel_bo_create(group->ptdev, group->vm,
						  args->ringbuf_size,
						  DRM_PANTHOR_BO_NO_MMAP,
						  DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC |
						  DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED,
						  PANTHOR_VM_KERNEL_AUTO_VA);
	if (IS_ERR(queue->ringbuf)) {
		ret = PTR_ERR(queue->ringbuf);
		goto err_free_queue;
	}

	ret = panthor_kernel_bo_vmap(queue->ringbuf);
	if (ret)
		goto err_free_queue;

	queue->iface.mem = panthor_fw_alloc_queue_iface_mem(group->ptdev,
							    &queue->iface.input,
							    &queue->iface.output,
							    &queue->iface.input_fw_va,
							    &queue->iface.output_fw_va);
	if (IS_ERR(queue->iface.mem)) {
		ret = PTR_ERR(queue->iface.mem);
		goto err_free_queue;
	}

	ret = drm_sched_init(&queue->scheduler, &panthor_queue_sched_ops,
			     group->ptdev->scheduler->wq, 1,
			     args->ringbuf_size / (NUM_INSTRS_PER_SLOT * sizeof(u64)),
			     0, msecs_to_jiffies(JOB_TIMEOUT_MS),
			     group->ptdev->reset.wq,
			     NULL, "panthor-queue", group->ptdev->base.dev);
	if (ret)
		goto err_free_queue;

	drm_sched = &queue->scheduler;
	ret = drm_sched_entity_init(&queue->entity, 0, &drm_sched, 1, NULL);

	return queue;

err_free_queue:
	group_free_queue(group, queue);
	return ERR_PTR(ret);
}

#define MAX_GROUPS_PER_POOL		128

int panthor_group_create(struct panthor_file *pfile,
			 const struct drm_panthor_group_create *group_args,
			 const struct drm_panthor_queue_create *queue_args)
{
	struct panthor_device *ptdev = pfile->ptdev;
	struct panthor_group_pool *gpool = pfile->groups;
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, 0);
	struct panthor_group *group = NULL;
	u32 gid, i, suspend_size;
	int ret;

	if (group_args->pad)
		return -EINVAL;

	if (group_args->priority > PANTHOR_CSG_PRIORITY_HIGH)
		return -EINVAL;

	if ((group_args->compute_core_mask & ~ptdev->gpu_info.shader_present) ||
	    (group_args->fragment_core_mask & ~ptdev->gpu_info.shader_present) ||
	    (group_args->tiler_core_mask & ~ptdev->gpu_info.tiler_present))
		return -EINVAL;

	if (hweight64(group_args->compute_core_mask) < group_args->max_compute_cores ||
	    hweight64(group_args->fragment_core_mask) < group_args->max_fragment_cores ||
	    hweight64(group_args->tiler_core_mask) < group_args->max_tiler_cores)
		return -EINVAL;

	group = kzalloc(sizeof(*group), GFP_KERNEL);
	if (!group)
		return -ENOMEM;

	spin_lock_init(&group->fatal_lock);
	kref_init(&group->refcount);
	group->state = PANTHOR_CS_GROUP_CREATED;
	group->csg_id = -1;

	group->ptdev = ptdev;
	group->max_compute_cores = group_args->max_compute_cores;
	group->compute_core_mask = group_args->compute_core_mask;
	group->max_fragment_cores = group_args->max_fragment_cores;
	group->fragment_core_mask = group_args->fragment_core_mask;
	group->max_tiler_cores = group_args->max_tiler_cores;
	group->tiler_core_mask = group_args->tiler_core_mask;
	group->priority = group_args->priority;

	INIT_LIST_HEAD(&group->wait_node);
	INIT_LIST_HEAD(&group->run_node);
	INIT_WORK(&group->term_work, group_term_work);
	INIT_WORK(&group->sync_upd_work, group_sync_upd_work);
	INIT_WORK(&group->tiler_oom_work, group_tiler_oom_work);
	INIT_WORK(&group->release_work, group_release_work);

	group->vm = panthor_vm_pool_get_vm(pfile->vms, group_args->vm_id);
	if (!group->vm) {
		ret = -EINVAL;
		goto err_put_group;
	}

	suspend_size = csg_iface->control->suspend_size;
	group->suspend_buf = panthor_fw_alloc_suspend_buf_mem(ptdev, suspend_size);
	if (IS_ERR(group->suspend_buf)) {
		ret = PTR_ERR(group->suspend_buf);
		group->suspend_buf = NULL;
		goto err_put_group;
	}

	suspend_size = csg_iface->control->protm_suspend_size;
	group->protm_suspend_buf = panthor_fw_alloc_suspend_buf_mem(ptdev, suspend_size);
	if (IS_ERR(group->protm_suspend_buf)) {
		ret = PTR_ERR(group->protm_suspend_buf);
		group->protm_suspend_buf = NULL;
		goto err_put_group;
	}

	group->syncobjs = panthor_kernel_bo_create(ptdev, group->vm,
						   group_args->queues.count *
						   sizeof(struct panthor_syncobj_64b),
						   DRM_PANTHOR_BO_NO_MMAP,
						   DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC |
						   DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED,
						   PANTHOR_VM_KERNEL_AUTO_VA);
	if (IS_ERR(group->syncobjs)) {
		ret = PTR_ERR(group->syncobjs);
		goto err_put_group;
	}

	ret = panthor_kernel_bo_vmap(group->syncobjs);
	if (ret)
		goto err_put_group;

	memset(group->syncobjs->kmap, 0,
	       group_args->queues.count * sizeof(struct panthor_syncobj_64b));

	for (i = 0; i < group_args->queues.count; i++) {
		group->queues[i] = group_create_queue(group, &queue_args[i]);
		if (IS_ERR(group->queues[i])) {
			ret = PTR_ERR(group->queues[i]);
			group->queues[i] = NULL;
			goto err_put_group;
		}

		group->queue_count++;
	}

	group->idle_queues = GENMASK(group->queue_count - 1, 0);

	ret = xa_alloc(&gpool->xa, &gid, group, XA_LIMIT(1, MAX_GROUPS_PER_POOL), GFP_KERNEL);
	if (ret)
		goto err_put_group;

	mutex_lock(&sched->reset.lock);
	if (atomic_read(&sched->reset.in_progress)) {
		panthor_group_stop(group);
	} else {
		mutex_lock(&sched->lock);
		list_add_tail(&group->run_node,
			      &sched->groups.idle[group->priority]);
		mutex_unlock(&sched->lock);
	}
	mutex_unlock(&sched->reset.lock);

	return gid;

err_put_group:
	group_put(group);
	return ret;
}

int panthor_group_destroy(struct panthor_file *pfile, u32 group_handle)
{
	struct panthor_group_pool *gpool = pfile->groups;
	struct panthor_device *ptdev = pfile->ptdev;
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_group *group;

	group = xa_erase(&gpool->xa, group_handle);
	if (!group)
		return -EINVAL;

	for (u32 i = 0; i < group->queue_count; i++) {
		if (group->queues[i])
			drm_sched_entity_destroy(&group->queues[i]->entity);
	}

	mutex_lock(&sched->reset.lock);
	mutex_lock(&sched->lock);
	group->destroyed = true;
	if (group->csg_id >= 0) {
		sched_queue_delayed_work(sched, tick, 0);
	} else if (!atomic_read(&sched->reset.in_progress)) {
		/* Remove from the run queues, so the scheduler can't
		 * pick the group on the next tick.
		 */
		list_del_init(&group->run_node);
		list_del_init(&group->wait_node);
		group_queue_work(group, term);
	}
	mutex_unlock(&sched->lock);
	mutex_unlock(&sched->reset.lock);

	group_put(group);
	return 0;
}

int panthor_group_get_state(struct panthor_file *pfile,
			    struct drm_panthor_group_get_state *get_state)
{
	struct panthor_group_pool *gpool = pfile->groups;
	struct panthor_device *ptdev = pfile->ptdev;
	struct panthor_scheduler *sched = ptdev->scheduler;
	struct panthor_group *group;

	if (get_state->pad)
		return -EINVAL;

	group = group_get(xa_load(&gpool->xa, get_state->group_handle));
	if (!group)
		return -EINVAL;

	memset(get_state, 0, sizeof(*get_state));

	mutex_lock(&sched->lock);
	if (group->timedout)
		get_state->state |= DRM_PANTHOR_GROUP_STATE_TIMEDOUT;
	if (group->fatal_queues) {
		get_state->state |= DRM_PANTHOR_GROUP_STATE_FATAL_FAULT;
		get_state->fatal_queues = group->fatal_queues;
	}
	mutex_unlock(&sched->lock);

	group_put(group);
	return 0;
}

int panthor_group_pool_create(struct panthor_file *pfile)
{
	struct panthor_group_pool *gpool;

	gpool = kzalloc(sizeof(*gpool), GFP_KERNEL);
	if (!gpool)
		return -ENOMEM;

	xa_init_flags(&gpool->xa, XA_FLAGS_ALLOC1);
	pfile->groups = gpool;
	return 0;
}

void panthor_group_pool_destroy(struct panthor_file *pfile)
{
	struct panthor_group_pool *gpool = pfile->groups;
	struct panthor_group *group;
	unsigned long i;

	if (IS_ERR_OR_NULL(gpool))
		return;

	xa_for_each(&gpool->xa, i, group)
		panthor_group_destroy(pfile, i);

	xa_destroy(&gpool->xa);
	kfree(gpool);
	pfile->groups = NULL;
}

static void job_release(struct kref *ref)
{
	struct panthor_job *job = container_of(ref, struct panthor_job, refcount);

	drm_WARN_ON(&job->group->ptdev->base, !list_empty(&job->node));

	if (job->base.s_fence)
		drm_sched_job_cleanup(&job->base);

	if (job->done_fence && job->done_fence->ops)
		dma_fence_put(job->done_fence);
	else
		dma_fence_free(job->done_fence);

	group_put(job->group);

	kfree(job);
}

struct drm_sched_job *panthor_job_get(struct drm_sched_job *sched_job)
{
	if (sched_job) {
		struct panthor_job *job = container_of(sched_job, struct panthor_job, base);

		kref_get(&job->refcount);
	}

	return sched_job;
}

void panthor_job_put(struct drm_sched_job *sched_job)
{
	struct panthor_job *job = container_of(sched_job, struct panthor_job, base);

	if (sched_job)
		kref_put(&job->refcount, job_release);
}

struct panthor_vm *panthor_job_vm(struct drm_sched_job *sched_job)
{
	struct panthor_job *job = container_of(sched_job, struct panthor_job, base);

	return job->group->vm;
}

struct drm_sched_job *
panthor_job_create(struct panthor_file *pfile,
		   u16 group_handle,
		   const struct drm_panthor_queue_submit *qsubmit)
{
	struct panthor_group_pool *gpool = pfile->groups;
	struct panthor_job *job;
	int ret;

	if (qsubmit->pad)
		return ERR_PTR(-EINVAL);

	/* If stream_addr is zero, so stream_size should be. */
	if ((qsubmit->stream_size == 0) != (qsubmit->stream_addr == 0))
		return ERR_PTR(-EINVAL);

	/* Make sure the address is aligned on 64-byte (cacheline) and the size is
	 * aligned on 8-byte (instruction size).
	 */
	if ((qsubmit->stream_addr & 63) || (qsubmit->stream_size & 7))
		return ERR_PTR(-EINVAL);

	/* bits 24:30 must be zero. */
	if (qsubmit->latest_flush & GENMASK(30, 24))
		return ERR_PTR(-EINVAL);

	job = kzalloc(sizeof(*job), GFP_KERNEL);
	if (!job)
		return ERR_PTR(-ENOMEM);

	kref_init(&job->refcount);
	job->queue_idx = qsubmit->queue_index;
	job->call_info.size = qsubmit->stream_size;
	job->call_info.start = qsubmit->stream_addr;
	job->call_info.latest_flush = qsubmit->latest_flush;
	INIT_LIST_HEAD(&job->node);

	job->group = group_get(xa_load(&gpool->xa, group_handle));
	if (!job->group) {
		ret = -EINVAL;
		goto err_put_job;
	}

	if (job->queue_idx >= job->group->queue_count ||
	    !job->group->queues[job->queue_idx]) {
		ret = -EINVAL;
		goto err_put_job;
	}

	/* Empty command streams don't need a fence, they'll pick the one from
	 * the previously submitted job.
	 */
	if (job->call_info.size) {
		job->done_fence = kzalloc(sizeof(*job->done_fence), GFP_KERNEL);
		if (!job->done_fence) {
			ret = -ENOMEM;
			goto err_put_job;
		}
	}

	ret = drm_sched_job_init(&job->base,
				 &job->group->queues[job->queue_idx]->entity,
				 1, job->group);
	if (ret)
		goto err_put_job;

	return &job->base;

err_put_job:
	panthor_job_put(&job->base);
	return ERR_PTR(ret);
}

void panthor_job_update_resvs(struct drm_exec *exec, struct drm_sched_job *sched_job)
{
	struct panthor_job *job = container_of(sched_job, struct panthor_job, base);

	/* Still not sure why we want USAGE_WRITE for external objects, since I
	 * was assuming this would be handled through explicit syncs being imported
	 * to external BOs with DMA_BUF_IOCTL_IMPORT_SYNC_FILE, but other drivers
	 * seem to pass DMA_RESV_USAGE_WRITE, so there must be a good reason.
	 */
	panthor_vm_update_resvs(job->group->vm, exec, &sched_job->s_fence->finished,
				DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_WRITE);
}

void panthor_sched_unplug(struct panthor_device *ptdev)
{
	struct panthor_scheduler *sched = ptdev->scheduler;

	cancel_delayed_work_sync(&sched->tick_work);

	mutex_lock(&sched->lock);
	if (sched->pm.has_ref) {
		pm_runtime_put(ptdev->base.dev);
		sched->pm.has_ref = false;
	}
	mutex_unlock(&sched->lock);
}

static void panthor_sched_fini(struct drm_device *ddev, void *res)
{
	struct panthor_scheduler *sched = res;
	int prio;

	if (!sched || !sched->csg_slot_count)
		return;

	cancel_delayed_work_sync(&sched->tick_work);

	if (sched->wq)
		destroy_workqueue(sched->wq);

	if (sched->heap_alloc_wq)
		destroy_workqueue(sched->heap_alloc_wq);

	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
		drm_WARN_ON(ddev, !list_empty(&sched->groups.runnable[prio]));
		drm_WARN_ON(ddev, !list_empty(&sched->groups.idle[prio]));
	}

	drm_WARN_ON(ddev, !list_empty(&sched->groups.waiting));
}

int panthor_sched_init(struct panthor_device *ptdev)
{
	struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
	struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, 0);
	struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, 0, 0);
	struct panthor_scheduler *sched;
	u32 gpu_as_count, num_groups;
	int prio, ret;

	sched = drmm_kzalloc(&ptdev->base, sizeof(*sched), GFP_KERNEL);
	if (!sched)
		return -ENOMEM;

	/* The highest bit in JOB_INT_* is reserved for globabl IRQs. That
	 * leaves 31 bits for CSG IRQs, hence the MAX_CSGS clamp here.
	 */
	num_groups = min_t(u32, MAX_CSGS, glb_iface->control->group_num);

	/* The FW-side scheduler might deadlock if two groups with the same
	 * priority try to access a set of resources that overlaps, with part
	 * of the resources being allocated to one group and the other part to
	 * the other group, both groups waiting for the remaining resources to
	 * be allocated. To avoid that, it is recommended to assign each CSG a
	 * different priority. In theory we could allow several groups to have
	 * the same CSG priority if they don't request the same resources, but
	 * that makes the scheduling logic more complicated, so let's clamp
	 * the number of CSG slots to MAX_CSG_PRIO + 1 for now.
	 */
	num_groups = min_t(u32, MAX_CSG_PRIO + 1, num_groups);

	/* We need at least one AS for the MCU and one for the GPU contexts. */
	gpu_as_count = hweight32(ptdev->gpu_info.as_present & GENMASK(31, 1));
	if (!gpu_as_count) {
		drm_err(&ptdev->base, "Not enough AS (%d, expected at least 2)",
			gpu_as_count + 1);
		return -EINVAL;
	}

	sched->ptdev = ptdev;
	sched->sb_slot_count = CS_FEATURES_SCOREBOARDS(cs_iface->control->features);
	sched->csg_slot_count = num_groups;
	sched->cs_slot_count = csg_iface->control->stream_num;
	sched->as_slot_count = gpu_as_count;
	ptdev->csif_info.csg_slot_count = sched->csg_slot_count;
	ptdev->csif_info.cs_slot_count = sched->cs_slot_count;
	ptdev->csif_info.scoreboard_slot_count = sched->sb_slot_count;

	sched->last_tick = 0;
	sched->resched_target = U64_MAX;
	sched->tick_period = msecs_to_jiffies(10);
	INIT_DELAYED_WORK(&sched->tick_work, tick_work);
	INIT_WORK(&sched->sync_upd_work, sync_upd_work);
	INIT_WORK(&sched->fw_events_work, process_fw_events_work);

	ret = drmm_mutex_init(&ptdev->base, &sched->lock);
	if (ret)
		return ret;

	for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
		INIT_LIST_HEAD(&sched->groups.runnable[prio]);
		INIT_LIST_HEAD(&sched->groups.idle[prio]);
	}
	INIT_LIST_HEAD(&sched->groups.waiting);

	ret = drmm_mutex_init(&ptdev->base, &sched->reset.lock);
	if (ret)
		return ret;

	INIT_LIST_HEAD(&sched->reset.stopped_groups);

	/* sched->heap_alloc_wq will be used for heap chunk allocation on
	 * tiler OOM events, which means we can't use the same workqueue for
	 * the scheduler because works queued by the scheduler are in
	 * the dma-signalling path. Allocate a dedicated heap_alloc_wq to
	 * work around this limitation.
	 *
	 * FIXME: Ultimately, what we need is a failable/non-blocking GEM
	 * allocation path that we can call when a heap OOM is reported. The
	 * FW is smart enough to fall back on other methods if the kernel can't
	 * allocate memory, and fail the tiling job if none of these
	 * countermeasures worked.
	 *
	 * Set WQ_MEM_RECLAIM on sched->wq to unblock the situation when the
	 * system is running out of memory.
	 */
	sched->heap_alloc_wq = alloc_workqueue("panthor-heap-alloc", WQ_UNBOUND, 0);
	sched->wq = alloc_workqueue("panthor-csf-sched", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
	if (!sched->wq || !sched->heap_alloc_wq) {
		panthor_sched_fini(&ptdev->base, sched);
		drm_err(&ptdev->base, "Failed to allocate the workqueues");
		return -ENOMEM;
	}

	ret = drmm_add_action_or_reset(&ptdev->base, panthor_sched_fini, sched);
	if (ret)
		return ret;

	ptdev->scheduler = sched;
	return 0;
}