summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_helpers_test.c
blob: 058e1ad15dfefd3ef6cf1ca5b0be1f1497f6935c (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
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
// SPDX-License-Identifier: GPL-2.0 OR MIT
/*
 * KUnit tests for amdgpu_dm_helpers.c
 *
 * Copyright 2026 Advanced Micro Devices, Inc.
 */

#include <kunit/test.h>
#include <drm/drm_edid.h>
#include <drm/drm_kunit_helpers.h>
#include <drm/display/drm_dp_mst_helper.h>

#include "dc.h"
#include "core_types.h"
#include "amdgpu.h"
#include "amdgpu_mode.h"
#include "amdgpu_dm.h"
#include "amdgpu_dm_mst_types.h"
#include "dc_bios_types.h"
#include "dm_helpers.h"
#include "ddc_service_types.h"
#include "dmub_cmd.h"
#include "amdgpu_dm_helpers.h"
#include "amdgpu_dm_kunit_test_helpers.h"

/* Tests for edid_extract_panel_id() */

/**
 * dm_test_edid_extract_panel_id_basic - Test Edid extract panel id basic
 * @test: The KUnit test context
 */
static void dm_test_edid_extract_panel_id_basic(struct kunit *test)
{
	struct edid *edid;
	u32 panel_id;

	edid = kunit_kzalloc(test, sizeof(*edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid);

	edid->mfg_id[0] = 0x12;
	edid->mfg_id[1] = 0x34;
	edid->prod_code[0] = 0xAB;
	edid->prod_code[1] = 0xCD;

	panel_id = edid_extract_panel_id(edid);

	/*
	 * Expected: (0x12 << 24) | (0x34 << 16) | EDID_PRODUCT_ID(edid)
	 * EDID_PRODUCT_ID = prod_code[0] | (prod_code[1] << 8) = 0xAB | 0xCD00 = 0xCDAB
	 * Result: 0x12340000 | 0x0000CDAB = 0x1234CDAB
	 */
	KUNIT_EXPECT_EQ(test, panel_id, (u32)0x1234CDAB);
}

/**
 * dm_test_edid_extract_panel_id_zeros - Test Edid extract panel id zeros
 * @test: The KUnit test context
 */
static void dm_test_edid_extract_panel_id_zeros(struct kunit *test)
{
	struct edid *edid;

	edid = kunit_kzalloc(test, sizeof(*edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid);

	KUNIT_EXPECT_EQ(test, edid_extract_panel_id(edid), 0U);
}

/* Tests for apply_edid_quirks() */

/*
 * Build an EDID whose extracted panel id equals @panel_id. Inverse of
 * edid_extract_panel_id(): mfg_id holds the top 16 bits, prod_code the
 * low 16 bits (prod_code[0] | prod_code[1] << 8).
 */
static struct edid *dm_test_edid_with_panel_id(struct kunit *test, u32 panel_id)
{
	struct edid *edid;

	edid = kunit_kzalloc(test, sizeof(*edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid);

	edid->mfg_id[0] = (panel_id >> 24) & 0xff;
	edid->mfg_id[1] = (panel_id >> 16) & 0xff;
	edid->prod_code[0] = panel_id & 0xff;
	edid->prod_code[1] = (panel_id >> 8) & 0xff;

	return edid;
}

/*
 * Wire a connector-backed link so apply_edid_quirks() can resolve
 * link->priv->base.dev for its drm_dbg_driver() calls.
 */
static struct dc_link *dm_test_quirk_link(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;
	struct dc_link *link;

	adev = dm_kunit_alloc_adev(test);
	link = dm_kunit_alloc_link(test);
	aconnector = dm_kunit_alloc_connector(test, adev, NULL);
	link->priv = aconnector;

	return link;
}

/**
 * dm_test_apply_edid_quirks_dpcd_poweroff_delay - Test GBT 0x3215 delay quirk
 * @test: The KUnit test context
 */
static void dm_test_apply_edid_quirks_dpcd_poweroff_delay(struct kunit *test)
{
	struct dc_edid_caps edid_caps = {0};
	struct dc_link *link = dm_test_quirk_link(test);
	struct edid *edid = dm_test_edid_with_panel_id(test,
			drm_edid_encode_panel_id('G', 'B', 'T', 0x3215));

	apply_edid_quirks(link, edid, &edid_caps);

	KUNIT_EXPECT_EQ(test, edid_caps.panel_patch.wait_after_dpcd_poweroff_ms, 10000U);
}

/**
 * dm_test_apply_edid_quirks_disable_fams - Test SAM panel FAMS-disable quirk
 * @test: The KUnit test context
 */
static void dm_test_apply_edid_quirks_disable_fams(struct kunit *test)
{
	struct dc_edid_caps edid_caps = {0};
	struct dc_link *link = dm_test_quirk_link(test);
	struct edid *edid = dm_test_edid_with_panel_id(test,
			drm_edid_encode_panel_id('S', 'A', 'M', 0x0E5E));

	apply_edid_quirks(link, edid, &edid_caps);

	KUNIT_EXPECT_TRUE(test, edid_caps.panel_patch.disable_fams);
}

/**
 * dm_test_apply_edid_quirks_remove_sink_ext_caps - Test AUO 0x317 clear quirk
 * @test: The KUnit test context
 */
static void dm_test_apply_edid_quirks_remove_sink_ext_caps(struct kunit *test)
{
	struct dc_edid_caps edid_caps = {0};
	struct dc_link *link = dm_test_quirk_link(test);
	struct edid *edid = dm_test_edid_with_panel_id(test,
			drm_edid_encode_panel_id('A', 'U', 'O', 0xA7AB));

	apply_edid_quirks(link, edid, &edid_caps);

	KUNIT_EXPECT_TRUE(test, edid_caps.panel_patch.remove_sink_ext_caps);
}

/**
 * dm_test_apply_edid_quirks_disable_colorimetry - Test SDC VSC-disable quirk
 * @test: The KUnit test context
 */
static void dm_test_apply_edid_quirks_disable_colorimetry(struct kunit *test)
{
	struct dc_edid_caps edid_caps = {0};
	struct dc_link *link = dm_test_quirk_link(test);
	struct edid *edid = dm_test_edid_with_panel_id(test,
			drm_edid_encode_panel_id('S', 'D', 'C', 0x4154));

	apply_edid_quirks(link, edid, &edid_caps);

	KUNIT_EXPECT_TRUE(test, edid_caps.panel_patch.disable_colorimetry);
}

/**
 * dm_test_apply_edid_quirks_skip_phy_ssc - Test DEL 0x4147 PHY SSC quirk
 * @test: The KUnit test context
 */
static void dm_test_apply_edid_quirks_skip_phy_ssc(struct kunit *test)
{
	struct dc_edid_caps edid_caps = {0};
	struct dc_link *link = dm_test_quirk_link(test);
	struct edid *edid = dm_test_edid_with_panel_id(test,
			drm_edid_encode_panel_id('D', 'E', 'L', 0x4147));

	apply_edid_quirks(link, edid, &edid_caps);

	KUNIT_EXPECT_TRUE(test, link->wa_flags.skip_phy_ssc_reduction);
}

/**
 * dm_test_apply_edid_quirks_unknown_noop - Test unknown panel id is a no-op
 * @test: The KUnit test context
 */
static void dm_test_apply_edid_quirks_unknown_noop(struct kunit *test)
{
	struct dc_edid_caps edid_caps = {0};
	struct dc_link *link = dm_test_quirk_link(test);
	struct edid *edid = dm_test_edid_with_panel_id(test,
			drm_edid_encode_panel_id('X', 'Y', 'Z', 0x0000));

	apply_edid_quirks(link, edid, &edid_caps);

	/* default: branch leaves every quirk field untouched */
	KUNIT_EXPECT_EQ(test, edid_caps.panel_patch.wait_after_dpcd_poweroff_ms, 0U);
	KUNIT_EXPECT_FALSE(test, edid_caps.panel_patch.disable_fams);
	KUNIT_EXPECT_FALSE(test, edid_caps.panel_patch.remove_sink_ext_caps);
	KUNIT_EXPECT_FALSE(test, edid_caps.panel_patch.disable_colorimetry);
	KUNIT_EXPECT_FALSE(test, link->wa_flags.skip_phy_ssc_reduction);
}

/* Tests for dm_helpers_parse_edid_caps() */

/*
 * Build a minimal valid base EDID block into @dc_edid. When @good_checksum is
 * false the final checksum byte is corrupted so drm_edid_is_valid() fails.
 *
 *   manufacturer_id = 0xAC10, product_id = 0x1234, serial = 0x12345678,
 *   week = 10, year (raw) = 30, digital input, no CEA extension.
 */
static void dm_test_fill_base_edid(struct dc_edid *dc_edid, bool good_checksum)
{
	static const u8 header[8] = {
		0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00
	};
	u8 *raw = dc_edid->raw_edid;
	int sum = 0;
	int i;

	memset(raw, 0, EDID_LENGTH);
	memcpy(raw, header, sizeof(header));

	raw[8] = 0x10;	raw[9] = 0xAC;		/* mfg_id    -> 0xAC10 */
	raw[10] = 0x34;	raw[11] = 0x12;		/* prod_code -> 0x1234 */
	raw[12] = 0x78;	raw[13] = 0x56;		/* serial    -> 0x12345678 */
	raw[14] = 0x34;	raw[15] = 0x12;
	raw[16] = 10;				/* mfg_week */
	raw[17] = 30;				/* mfg_year (raw) */
	raw[18] = 1;				/* version */
	raw[19] = 4;				/* revision */
	raw[20] = DRM_EDID_INPUT_DIGITAL;	/* digital input */
	raw[126] = 0;				/* no extensions */

	for (i = 0; i < EDID_LENGTH - 1; i++)
		sum += raw[i];
	raw[127] = (256 - (sum % 256)) % 256;
	if (!good_checksum)
		raw[127] ^= 0xFF;		/* corrupt checksum */

	dc_edid->length = EDID_LENGTH;
}

/**
 * dm_test_parse_edid_caps_null_edid - Test NULL edid returns EDID_BAD_INPUT
 * @test: The KUnit test context
 */
static void dm_test_parse_edid_caps_null_edid(struct kunit *test)
{
	struct dc_edid_caps edid_caps = {0};
	struct dc_link *link = dm_test_quirk_link(test);

	KUNIT_EXPECT_EQ(test, dm_helpers_parse_edid_caps(link, NULL, &edid_caps), EDID_BAD_INPUT);
}

/**
 * dm_test_parse_edid_caps_null_caps - Test NULL edid_caps returns EDID_BAD_INPUT
 * @test: The KUnit test context
 */
static void dm_test_parse_edid_caps_null_caps(struct kunit *test)
{
	struct dc_link *link = dm_test_quirk_link(test);
	struct dc_edid *dc_edid;

	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	dm_test_fill_base_edid(dc_edid, true);

	KUNIT_EXPECT_EQ(test, dm_helpers_parse_edid_caps(link, dc_edid, NULL), EDID_BAD_INPUT);
}

/**
 * dm_test_parse_edid_caps_valid - Test field extraction from a valid EDID
 * @test: The KUnit test context
 */
static void dm_test_parse_edid_caps_valid(struct kunit *test)
{
	struct dc_link *link = dm_test_quirk_link(test);
	struct dc_edid_caps *edid_caps;
	struct dc_edid *dc_edid;

	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	edid_caps = kunit_kzalloc(test, sizeof(*edid_caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid_caps);

	dm_test_fill_base_edid(dc_edid, true);

	KUNIT_EXPECT_EQ(test, dm_helpers_parse_edid_caps(link, dc_edid, edid_caps), EDID_OK);
	KUNIT_EXPECT_EQ(test, edid_caps->manufacturer_id, 0xAC10);
	KUNIT_EXPECT_EQ(test, edid_caps->product_id, 0x1234);
	KUNIT_EXPECT_EQ(test, edid_caps->serial_number, 0x12345678U);
	KUNIT_EXPECT_EQ(test, edid_caps->manufacture_week, 10);
	KUNIT_EXPECT_EQ(test, edid_caps->manufacture_year, 30);
	KUNIT_EXPECT_FALSE(test, edid_caps->analog);
}

/**
 * dm_test_parse_edid_caps_bad_checksum - Test bad checksum still parses fields
 * @test: The KUnit test context
 */
static void dm_test_parse_edid_caps_bad_checksum(struct kunit *test)
{
	struct dc_link *link = dm_test_quirk_link(test);
	struct dc_edid_caps *edid_caps;
	struct dc_edid *dc_edid;

	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	edid_caps = kunit_kzalloc(test, sizeof(*edid_caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid_caps);

	dm_test_fill_base_edid(dc_edid, false);

	/* Invalid checksum -> EDID_BAD_CHECKSUM but fields are still parsed */
	KUNIT_EXPECT_EQ(test,
			dm_helpers_parse_edid_caps(link, dc_edid, edid_caps),
			EDID_BAD_CHECKSUM);
	KUNIT_EXPECT_EQ(test, edid_caps->manufacturer_id, 0xAC10);
	KUNIT_EXPECT_EQ(test, edid_caps->product_id, 0x1234);
}

/**
 * dm_test_parse_edid_caps_hdmi_frl - Test HDMI/FRL branch via connector info
 * @test: The KUnit test context
 *
 * Drives the edid_caps->edid_hdmi path by marking the connector as HDMI and
 * providing a fake dc with FRL enabled, so populate_hdmi_info_from_connector()
 * is exercised inside dm_helpers_parse_edid_caps().
 */
static void dm_test_parse_edid_caps_hdmi_frl(struct kunit *test)
{
	struct dc_link *link = dm_test_quirk_link(test);
	struct amdgpu_dm_connector *aconnector = link->priv;
	struct drm_connector *connector = &aconnector->base;
	struct dc_edid_caps *edid_caps;
	struct dc_edid *dc_edid;
	struct dc *dc;

	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	edid_caps = kunit_kzalloc(test, sizeof(*edid_caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid_caps);
	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc);

	link->dc = dc;
	dc->config.enable_frl = true;

	dm_test_fill_base_edid(dc_edid, true);

	/* Drive the HDMI/FRL branch */
	connector->display_info.is_hdmi = true;
	connector->display_info.hdmi.scdc.supported = true;
	connector->display_info.hdmi.max_lanes = 4;
	connector->display_info.hdmi.max_frl_rate_per_lane = 12;

	KUNIT_EXPECT_EQ(test, dm_helpers_parse_edid_caps(link, dc_edid, edid_caps), EDID_OK);
	KUNIT_EXPECT_TRUE(test, edid_caps->edid_hdmi);
	KUNIT_EXPECT_TRUE(test, edid_caps->scdc_present);
	/* max_lanes 4 + max_frl_rate_per_lane 12 -> rate index 6 */
	KUNIT_EXPECT_EQ(test, edid_caps->max_frl_rate, 6);
}

/**
 * dm_test_parse_edid_caps_hdmi_frl_dsc - Test HDMI FRL DSC sub-branch
 * @test: The KUnit test context
 *
 * Sets the connector's HDMI DSC capability so populate_hdmi_info_from_connector()
 * reports frl_dsc_support, exercising the frl_dsc_support log path.
 */
static void dm_test_parse_edid_caps_hdmi_frl_dsc(struct kunit *test)
{
	struct dc_link *link = dm_test_quirk_link(test);
	struct amdgpu_dm_connector *aconnector = link->priv;
	struct drm_connector *connector = &aconnector->base;
	struct dc_edid_caps *edid_caps;
	struct dc_edid *dc_edid;
	struct dc *dc;

	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	edid_caps = kunit_kzalloc(test, sizeof(*edid_caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid_caps);
	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc);

	link->dc = dc;
	dc->config.enable_frl = true;

	dm_test_fill_base_edid(dc_edid, true);

	connector->display_info.is_hdmi = true;
	/* Drive the frl_dsc_support sub-branch */
	connector->display_info.hdmi.dsc_cap.v_1p2 = true;
	connector->display_info.hdmi.dsc_cap.bpc_supported = 10;

	KUNIT_EXPECT_EQ(test, dm_helpers_parse_edid_caps(link, dc_edid, edid_caps), EDID_OK);
	KUNIT_EXPECT_TRUE(test, edid_caps->frl_dsc_support);
	KUNIT_EXPECT_TRUE(test, edid_caps->frl_dsc_10bpc);
}

/*
 * Build a 2-block EDID: a valid base block plus a CTA-861 extension block
 * carrying one LPCM Short Audio Descriptor and, when @with_speaker is set, a
 * Speaker Allocation Data Block, so the audio/speaker parsing code is
 * exercised.
 */
static void dm_test_fill_cea_edid(struct dc_edid *dc_edid, bool with_speaker)
{
	u8 *raw = dc_edid->raw_edid;
	u8 *ext = raw + EDID_LENGTH;
	u8 dtd_offset;
	int sum = 0;
	int i;

	/* Base block, flagged as having one extension */
	dm_test_fill_base_edid(dc_edid, true);
	raw[126] = 1;
	for (i = 0; i < EDID_LENGTH - 1; i++)
		sum += raw[i];
	raw[127] = (256 - (sum % 256)) % 256;

	/* Audio DB spans [4,7]; optional Speaker Alloc DB spans [8,11] */
	dtd_offset = with_speaker ? 12 : 8;

	/* CTA-861 extension block */
	memset(ext, 0, EDID_LENGTH);
	ext[0] = 0x02;			/* CTA extension tag */
	ext[1] = 0x03;			/* revision 3 */
	ext[2] = dtd_offset;		/* DTD offset; end of data blocks */
	ext[3] = 0x00;			/* no native DTDs / feature flags */
	/* Audio Data Block: tag 1, length 3, one LPCM SAD */
	ext[4] = (1 << 5) | 3;
	ext[5] = (1 << 3) | 1;		/* format 1 (LPCM), 2 channels */
	ext[6] = 0x07;			/* 32 / 44.1 / 48 kHz */
	ext[7] = 0x07;			/* 16 / 20 / 24-bit */
	if (with_speaker) {
		/* Speaker Allocation Data Block: tag 4, length 3 */
		ext[8] = (4 << 5) | 3;
		ext[9] = 0x01;		/* front left / front right */
	}

	sum = 0;
	for (i = 0; i < EDID_LENGTH - 1; i++)
		sum += ext[i];
	ext[127] = (256 - (sum % 256)) % 256;

	dc_edid->length = 2 * EDID_LENGTH;
}

/**
 * dm_test_parse_edid_caps_cea_audio - Test CEA audio/speaker block parsing
 * @test: The KUnit test context
 */
static void dm_test_parse_edid_caps_cea_audio(struct kunit *test)
{
	struct dc_link *link = dm_test_quirk_link(test);
	struct dc_edid_caps *edid_caps;
	struct dc_edid *dc_edid;

	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	edid_caps = kunit_kzalloc(test, sizeof(*edid_caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid_caps);

	dm_test_fill_cea_edid(dc_edid, true);

	KUNIT_EXPECT_EQ(test, dm_helpers_parse_edid_caps(link, dc_edid, edid_caps), EDID_OK);
	KUNIT_EXPECT_EQ(test, edid_caps->audio_mode_count, 1);
	KUNIT_EXPECT_EQ(test, edid_caps->audio_modes[0].format_code, 1);
	KUNIT_EXPECT_EQ(test, edid_caps->audio_modes[0].channel_count, 2);
	KUNIT_EXPECT_EQ(test, edid_caps->audio_modes[0].sample_rate, 0x07);
	KUNIT_EXPECT_EQ(test, edid_caps->audio_modes[0].sample_size, 0x07);
	KUNIT_EXPECT_EQ(test, edid_caps->speaker_flags, 0x01);
}

/**
 * dm_test_parse_edid_caps_cea_no_speaker - Test default speaker flags path
 * @test: The KUnit test context
 *
 * Audio data block present but no Speaker Allocation Data Block, so
 * speaker_flags falls back to DEFAULT_SPEAKER_LOCATION.
 */
static void dm_test_parse_edid_caps_cea_no_speaker(struct kunit *test)
{
	struct dc_link *link = dm_test_quirk_link(test);
	struct dc_edid_caps *edid_caps;
	struct dc_edid *dc_edid;

	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	edid_caps = kunit_kzalloc(test, sizeof(*edid_caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, edid_caps);

	dm_test_fill_cea_edid(dc_edid, false);

	KUNIT_EXPECT_EQ(test, dm_helpers_parse_edid_caps(link, dc_edid, edid_caps), EDID_OK);
	KUNIT_EXPECT_EQ(test, edid_caps->audio_mode_count, 1);
	KUNIT_EXPECT_EQ(test, edid_caps->speaker_flags, DEFAULT_SPEAKER_LOCATION);
}

/* Tests for ACPI and VBIOS EDID readers */

/**
 * dm_test_probe_acpi_edid_no_companion - Test ACPI probe without companion
 * @test: The KUnit test context
 */
static void dm_test_probe_acpi_edid_no_companion(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;
	u8 buf[EDID_LENGTH] = {0};

	adev = dm_kunit_alloc_adev(test);
	aconnector = dm_kunit_alloc_connector(test, adev, NULL);

	KUNIT_EXPECT_EQ(test,
			dm_helpers_probe_acpi_edid(&aconnector->base, buf, 0, sizeof(buf)),
			-ENODEV);
}

/**
 * dm_test_read_acpi_edid_debug_mask_disabled - Test debug mask early return
 * @test: The KUnit test context
 */
static void dm_test_read_acpi_edid_debug_mask_disabled(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;
	uint old_debug_mask;

	adev = dm_kunit_alloc_adev(test);
	aconnector = dm_kunit_alloc_connector(test, adev, NULL);
	old_debug_mask = dm_helpers_get_dc_debug_mask();

	dm_helpers_set_dc_debug_mask(old_debug_mask | DC_DISABLE_ACPI_EDID);
	KUNIT_EXPECT_NULL(test, dm_helpers_read_acpi_edid(aconnector));
	dm_helpers_set_dc_debug_mask(old_debug_mask);
}

/**
 * dm_test_read_acpi_edid_non_panel_connector - Test non-panel connector skip
 * @test: The KUnit test context
 */
static void dm_test_read_acpi_edid_non_panel_connector(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;

	adev = dm_kunit_alloc_adev(test);
	aconnector = dm_kunit_alloc_connector(test, adev, NULL);
	aconnector->base.connector_type = DRM_MODE_CONNECTOR_HDMIA;

	KUNIT_EXPECT_NULL(test, dm_helpers_read_acpi_edid(aconnector));
}

/**
 * dm_test_read_acpi_edid_force_off - Test forced-off connector skip
 * @test: The KUnit test context
 */
static void dm_test_read_acpi_edid_force_off(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;

	adev = dm_kunit_alloc_adev(test);
	aconnector = dm_kunit_alloc_connector(test, adev, NULL);
	aconnector->base.connector_type = DRM_MODE_CONNECTOR_eDP;
	aconnector->base.force = DRM_FORCE_OFF;

	KUNIT_EXPECT_NULL(test, dm_helpers_read_acpi_edid(aconnector));
}

struct dm_test_vbios_edid {
	struct dc_bios bios;
	enum bp_result result;
	const u8 *fake_edid;
	u16 fake_edid_size;
	u16 width_mm;
	u16 height_mm;
};

static enum bp_result dm_test_get_embedded_panel_info(struct dc_bios *bios,
						      struct embedded_panel_info *info)
{
	struct dm_test_vbios_edid *fixture;

	fixture = container_of(bios, struct dm_test_vbios_edid, bios);
	if (fixture->result != BP_RESULT_OK)
		return fixture->result;

	info->fake_edid = fixture->fake_edid;
	info->fake_edid_size = fixture->fake_edid_size;
	info->panel_width_mm = fixture->width_mm;
	info->panel_height_mm = fixture->height_mm;

	return BP_RESULT_OK;
}

static const struct dc_vbios_funcs dm_test_vbios_edid_funcs = {
	.get_embedded_panel_info = dm_test_get_embedded_panel_info,
};

static void dm_test_setup_vbios_link(struct kunit *test,
				     struct dc_link **link_out,
				     struct amdgpu_dm_connector **aconnector_out,
				     struct dm_test_vbios_edid **fixture_out)
{
	struct dm_test_vbios_edid *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_link *link;

	adev = dm_kunit_alloc_adev(test);
	link = dm_kunit_alloc_link(test);
	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	fixture = kunit_kzalloc(test, sizeof(*fixture), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fixture);
	aconnector = dm_kunit_alloc_connector(test, adev, link);

	fixture->bios.funcs = &dm_test_vbios_edid_funcs;
	fixture->result = BP_RESULT_OK;
	ctx->dc_bios = &fixture->bios;
	link->ctx = ctx;
	link->priv = aconnector;
	link->connector_signal = SIGNAL_TYPE_EDP;

	*link_out = link;
	*aconnector_out = aconnector;
	*fixture_out = fixture;
}

/**
 * dm_test_read_vbios_edid_non_embedded - Test non-embedded signal skip
 * @test: The KUnit test context
 */
static void dm_test_read_vbios_edid_non_embedded(struct kunit *test)
{
	struct dm_test_vbios_edid *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_link *link;

	dm_test_setup_vbios_link(test, &link, &aconnector, &fixture);
	link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;

	KUNIT_EXPECT_NULL(test,
			  dm_helpers_read_vbios_hardcoded_edid(link, aconnector));
}

/**
 * dm_test_read_vbios_edid_missing_callback - Test missing callback skip
 * @test: The KUnit test context
 */
static void dm_test_read_vbios_edid_missing_callback(struct kunit *test)
{
	static const struct dc_vbios_funcs empty_funcs;
	struct dm_test_vbios_edid *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_link *link;

	dm_test_setup_vbios_link(test, &link, &aconnector, &fixture);
	fixture->bios.funcs = &empty_funcs;

	KUNIT_EXPECT_NULL(test,
			  dm_helpers_read_vbios_hardcoded_edid(link, aconnector));
}

/**
 * dm_test_read_vbios_edid_callback_error - Test callback failure skip
 * @test: The KUnit test context
 */
static void dm_test_read_vbios_edid_callback_error(struct kunit *test)
{
	struct dm_test_vbios_edid *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_link *link;

	dm_test_setup_vbios_link(test, &link, &aconnector, &fixture);
	fixture->result = BP_RESULT_BADINPUT;

	KUNIT_EXPECT_NULL(test,
			  dm_helpers_read_vbios_hardcoded_edid(link, aconnector));
}

/**
 * dm_test_read_vbios_edid_missing_fake_edid - Test missing fake EDID skip
 * @test: The KUnit test context
 */
static void dm_test_read_vbios_edid_missing_fake_edid(struct kunit *test)
{
	struct dm_test_vbios_edid *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_link *link;

	dm_test_setup_vbios_link(test, &link, &aconnector, &fixture);
	fixture->fake_edid = NULL;
	fixture->fake_edid_size = 0;

	KUNIT_EXPECT_NULL(test,
			  dm_helpers_read_vbios_hardcoded_edid(link, aconnector));
}

/**
 * dm_test_read_vbios_edid_invalid_fake_edid - Test invalid fake EDID skip
 * @test: The KUnit test context
 */
static void dm_test_read_vbios_edid_invalid_fake_edid(struct kunit *test)
{
	struct dm_test_vbios_edid *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_edid *dc_edid;
	struct dc_link *link;

	dm_test_setup_vbios_link(test, &link, &aconnector, &fixture);
	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	dm_test_fill_base_edid(dc_edid, false);
	fixture->fake_edid = dc_edid->raw_edid;
	fixture->fake_edid_size = EDID_LENGTH;

	KUNIT_EXPECT_NULL(test,
			  dm_helpers_read_vbios_hardcoded_edid(link, aconnector));
}

/**
 * dm_test_read_vbios_edid_valid - Test valid fake EDID updates display size
 * @test: The KUnit test context
 */
static void dm_test_read_vbios_edid_valid(struct kunit *test)
{
	struct dm_test_vbios_edid *fixture;
	struct amdgpu_dm_connector *aconnector;
	const struct drm_edid *edid;
	struct dc_edid *dc_edid;
	struct dc_link *link;

	dm_test_setup_vbios_link(test, &link, &aconnector, &fixture);
	dc_edid = kunit_kzalloc(test, sizeof(*dc_edid), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc_edid);
	dm_test_fill_base_edid(dc_edid, true);
	fixture->fake_edid = dc_edid->raw_edid;
	fixture->fake_edid_size = EDID_LENGTH;
	fixture->width_mm = 301;
	fixture->height_mm = 201;

	edid = dm_helpers_read_vbios_hardcoded_edid(link, aconnector);

	KUNIT_ASSERT_NOT_NULL(test, edid);
	KUNIT_EXPECT_EQ(test, aconnector->base.display_info.width_mm, 301U);
	KUNIT_EXPECT_EQ(test, aconnector->base.display_info.height_mm, 201U);
	drm_edid_free(edid);
}

/* Tests for dm_is_freesync_pcon_whitelist() */

/**
 * dm_test_freesync_pcon_whitelist_all_known - Test all known Freesync Pcon whitelist entries
 * @test: The KUnit test context
 *
 * Iterates over the driver's whitelist table directly so that any ID added
 * to dm_freesync_pcon_whitelist[] is automatically covered by this test.
 */
static void dm_test_freesync_pcon_whitelist_all_known(struct kunit *test)
{
	u32 i;

	for (i = 0; i < dm_freesync_pcon_whitelist_count(); i++)
		KUNIT_EXPECT_TRUE(test,
				  dm_is_freesync_pcon_whitelist(dm_freesync_pcon_whitelist[i]));
}

/**
 * dm_test_freesync_pcon_whitelist_not_in_list - Test Freesync pcon whitelist not in list
 * @test: The KUnit test context
 */
static void dm_test_freesync_pcon_whitelist_not_in_list(struct kunit *test)
{
	/* 0xFFFFFF is not a known whitelist device */
	KUNIT_EXPECT_FALSE(test, dm_is_freesync_pcon_whitelist(0xFFFFFF));
}

/**
 * dm_test_freesync_pcon_whitelist_zero - Test Freesync pcon whitelist zero
 * @test: The KUnit test context
 */
static void dm_test_freesync_pcon_whitelist_zero(struct kunit *test)
{
	KUNIT_EXPECT_FALSE(test, dm_is_freesync_pcon_whitelist(0));
}

/* Tests for populate_hdmi_info_from_connector() */

/**
 * dm_test_populate_hdmi_scdc_present_true - Test Populate hdmi scdc present true
 * @test: The KUnit test context
 */
static void dm_test_populate_hdmi_scdc_present_true(struct kunit *test)
{
	struct drm_hdmi_info *hdmi;
	struct dc_edid_caps *caps;

	hdmi = kunit_kzalloc(test, sizeof(*hdmi), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, hdmi);
	caps = kunit_kzalloc(test, sizeof(*caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, caps);

	hdmi->scdc.supported = true;

	populate_hdmi_info_from_connector(true, hdmi, caps);

	KUNIT_EXPECT_TRUE(test, caps->scdc_present);
}

/**
 * dm_test_populate_hdmi_scdc_present_false - Test Populate hdmi scdc present false
 * @test: The KUnit test context
 */
static void dm_test_populate_hdmi_scdc_present_false(struct kunit *test)
{
	struct drm_hdmi_info *hdmi;
	struct dc_edid_caps *caps;

	hdmi = kunit_kzalloc(test, sizeof(*hdmi), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, hdmi);
	caps = kunit_kzalloc(test, sizeof(*caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, caps);

	hdmi->scdc.supported = false;
	caps->scdc_present = true; /* pre-set to confirm it gets cleared */

	populate_hdmi_info_from_connector(true, hdmi, caps);

	KUNIT_EXPECT_FALSE(test, caps->scdc_present);
}

/**
 * dm_test_populate_hdmi_frl_dsc_10bpc - Test HDMI FRL DSC 10 bpc caps
 * @test: The KUnit test context
 */
static void dm_test_populate_hdmi_frl_dsc_10bpc(struct kunit *test)
{
	struct drm_hdmi_info *hdmi;
	struct dc_edid_caps *caps;

	hdmi = kunit_kzalloc(test, sizeof(*hdmi), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, hdmi);
	caps = kunit_kzalloc(test, sizeof(*caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, caps);

	hdmi->max_lanes = 4;
	hdmi->max_frl_rate_per_lane = 12;
	hdmi->dsc_cap.v_1p2 = true;
	hdmi->dsc_cap.bpc_supported = 10;
	hdmi->dsc_cap.all_bpp = true;
	hdmi->dsc_cap.native_420 = true;
	hdmi->dsc_cap.max_slices = 8;
	hdmi->dsc_cap.clk_per_slice = 400;
	hdmi->dsc_cap.max_lanes = 4;
	hdmi->dsc_cap.max_frl_rate_per_lane = 10;
	hdmi->dsc_cap.total_chunk_kbytes = 7;

	populate_hdmi_info_from_connector(true, hdmi, caps);

	KUNIT_EXPECT_EQ(test, caps->max_frl_rate, 6);
	KUNIT_EXPECT_TRUE(test, caps->frl_dsc_support);
	KUNIT_EXPECT_TRUE(test, caps->frl_dsc_10bpc);
	KUNIT_EXPECT_FALSE(test, caps->frl_dsc_12bpc);
	KUNIT_EXPECT_TRUE(test, caps->frl_dsc_all_bpp);
	KUNIT_EXPECT_TRUE(test, caps->frl_dsc_native_420);
	KUNIT_EXPECT_EQ(test, caps->frl_dsc_max_slices, 5);
	KUNIT_EXPECT_EQ(test, caps->frl_dsc_max_frl_rate, 5);
	KUNIT_EXPECT_EQ(test, caps->frl_dsc_total_chunk_kbytes, 7);
}

/**
 * dm_test_populate_hdmi_frl_dsc_12bpc - Test HDMI FRL DSC 12 bpc caps
 * @test: The KUnit test context
 */
static void dm_test_populate_hdmi_frl_dsc_12bpc(struct kunit *test)
{
	struct drm_hdmi_info *hdmi;
	struct dc_edid_caps *caps;

	hdmi = kunit_kzalloc(test, sizeof(*hdmi), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, hdmi);
	caps = kunit_kzalloc(test, sizeof(*caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, caps);

	hdmi->max_lanes = 3;
	hdmi->max_frl_rate_per_lane = 6;
	hdmi->dsc_cap.v_1p2 = true;
	hdmi->dsc_cap.bpc_supported = 12;
	hdmi->dsc_cap.max_slices = 16;
	hdmi->dsc_cap.clk_per_slice = 400;
	hdmi->dsc_cap.max_lanes = 3;
	hdmi->dsc_cap.max_frl_rate_per_lane = 3;

	populate_hdmi_info_from_connector(true, hdmi, caps);

	KUNIT_EXPECT_EQ(test, caps->max_frl_rate, 2);
	KUNIT_EXPECT_TRUE(test, caps->frl_dsc_support);
	KUNIT_EXPECT_FALSE(test, caps->frl_dsc_10bpc);
	KUNIT_EXPECT_TRUE(test, caps->frl_dsc_12bpc);
	KUNIT_EXPECT_EQ(test, caps->frl_dsc_max_slices, 7);
	KUNIT_EXPECT_EQ(test, caps->frl_dsc_max_frl_rate, 1);
}

/**
 * dm_test_populate_hdmi_frl_dsc_unknown_values - Test HDMI FRL DSC unknown values
 * @test: The KUnit test context
 */
static void dm_test_populate_hdmi_frl_dsc_unknown_values(struct kunit *test)
{
	struct drm_hdmi_info *hdmi;
	struct dc_edid_caps *caps;

	hdmi = kunit_kzalloc(test, sizeof(*hdmi), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, hdmi);
	caps = kunit_kzalloc(test, sizeof(*caps), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, caps);

	hdmi->max_lanes = 2;
	hdmi->max_frl_rate_per_lane = 3;
	hdmi->dsc_cap.v_1p2 = true;
	hdmi->dsc_cap.bpc_supported = 8;
	hdmi->dsc_cap.max_slices = 3;
	hdmi->dsc_cap.clk_per_slice = 340;
	hdmi->dsc_cap.max_lanes = 2;
	hdmi->dsc_cap.max_frl_rate_per_lane = 12;

	populate_hdmi_info_from_connector(true, hdmi, caps);

	KUNIT_EXPECT_EQ(test, caps->max_frl_rate, 0);
	KUNIT_EXPECT_TRUE(test, caps->frl_dsc_support);
	KUNIT_EXPECT_FALSE(test, caps->frl_dsc_10bpc);
	KUNIT_EXPECT_FALSE(test, caps->frl_dsc_12bpc);
	KUNIT_EXPECT_EQ(test, caps->frl_dsc_max_slices, 0);
	KUNIT_EXPECT_EQ(test, caps->frl_dsc_max_frl_rate, 0);
}

/* Tests for dm_get_adaptive_sync_support_type() */

/**
 * dm_test_adaptive_sync_type_none_default - Test Adaptive sync type none default
 * @test: The KUnit test context
 */
static void dm_test_adaptive_sync_type_none_default(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* dongle_type = 0 (DISPLAY_DONGLE_NONE) → default case → TYPE_NONE */
	KUNIT_EXPECT_EQ(test,
			(int)dm_get_adaptive_sync_support_type(link),
			(int)ADAPTIVE_SYNC_TYPE_NONE);
}

/**
 * dm_test_adaptive_sync_type_converter_no_conditions - Converter without caps
 * @test: The KUnit test context
 */
static void dm_test_adaptive_sync_type_converter_no_conditions(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* HDMI converter but no adaptive sync cap → still NONE */
	link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_HDMI_CONVERTER;

	KUNIT_EXPECT_EQ(test,
			(int)dm_get_adaptive_sync_support_type(link),
			(int)ADAPTIVE_SYNC_TYPE_NONE);
}

/**
 * dm_test_adaptive_sync_type_converter_partial_conditions - Partial caps
 * @test: The KUnit test context
 */
static void dm_test_adaptive_sync_type_converter_partial_conditions(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* Cap set and whitelist ID, but allow_invalid_MSA_timing_param = false */
	link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_HDMI_CONVERTER;
	link->dpcd_caps.adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT = 1;
	link->dpcd_caps.allow_invalid_MSA_timing_param = false;
	link->dpcd_caps.branch_dev_id = DP_BRANCH_DEVICE_ID_0060AD;

	KUNIT_EXPECT_EQ(test,
			(int)dm_get_adaptive_sync_support_type(link),
			(int)ADAPTIVE_SYNC_TYPE_NONE);
}

/**
 * dm_test_adaptive_sync_type_pcon_whitelist - Test Adaptive sync type pcon whitelist
 * @test: The KUnit test context
 */
static void dm_test_adaptive_sync_type_pcon_whitelist(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* All conditions met → FREESYNC_TYPE_PCON_IN_WHITELIST */
	link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_HDMI_CONVERTER;
	link->dpcd_caps.adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT = 1;
	link->dpcd_caps.allow_invalid_MSA_timing_param = true;
	link->dpcd_caps.branch_dev_id = DP_BRANCH_DEVICE_ID_0060AD;

	KUNIT_EXPECT_EQ(test,
			(int)dm_get_adaptive_sync_support_type(link),
			(int)FREESYNC_TYPE_PCON_IN_WHITELIST);
}

/**
 * dm_test_adaptive_sync_type_converter_nonwhitelist - Converter not whitelisted
 * @test: The KUnit test context
 */
static void dm_test_adaptive_sync_type_converter_nonwhitelist(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* All conditions met but branch_dev_id not in whitelist → NONE */
	link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_HDMI_CONVERTER;
	link->dpcd_caps.adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT = 1;
	link->dpcd_caps.allow_invalid_MSA_timing_param = true;
	link->dpcd_caps.branch_dev_id = 0xFFFFFF;

	KUNIT_EXPECT_EQ(test,
			(int)dm_get_adaptive_sync_support_type(link),
			(int)ADAPTIVE_SYNC_TYPE_NONE);
}

/* Tests for dm_helpers_is_fullscreen() and dm_helpers_is_hdr_on() */

/**
 * dm_test_helpers_is_fullscreen_returns_false - Test Helpers is fullscreen returns false
 * @test: The KUnit test context
 */
static void dm_test_helpers_is_fullscreen_returns_false(struct kunit *test)
{
	/* Stub — always returns false */
	KUNIT_EXPECT_FALSE(test, dm_helpers_is_fullscreen(NULL, NULL));
}

/**
 * dm_test_helpers_is_hdr_on_returns_false - Test Helpers is hdr on returns false
 * @test: The KUnit test context
 */
static void dm_test_helpers_is_hdr_on_returns_false(struct kunit *test)
{
	/* Stub — always returns false */
	KUNIT_EXPECT_FALSE(test, dm_helpers_is_hdr_on(NULL, NULL));
}

/* Tests for get_max_frl_rate() */

/**
 * dm_test_get_max_frl_rate_3lanes_3gbps - Test Get max frl rate 3lanes 3gbps
 * @test: The KUnit test context
 */
static void dm_test_get_max_frl_rate_3lanes_3gbps(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_max_frl_rate(3, 3), 1);
}

/**
 * dm_test_get_max_frl_rate_3lanes_6gbps - Test Get max frl rate 3lanes 6gbps
 * @test: The KUnit test context
 */
static void dm_test_get_max_frl_rate_3lanes_6gbps(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_max_frl_rate(3, 6), 2);
}

/**
 * dm_test_get_max_frl_rate_4lanes_6gbps - Test Get max frl rate 4lanes 6gbps
 * @test: The KUnit test context
 */
static void dm_test_get_max_frl_rate_4lanes_6gbps(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_max_frl_rate(4, 6), 3);
}

/**
 * dm_test_get_max_frl_rate_4lanes_8gbps - Test Get max frl rate 4lanes 8gbps
 * @test: The KUnit test context
 */
static void dm_test_get_max_frl_rate_4lanes_8gbps(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_max_frl_rate(4, 8), 4);
}

/**
 * dm_test_get_max_frl_rate_4lanes_10gbps - Test Get max frl rate 4lanes 10gbps
 * @test: The KUnit test context
 */
static void dm_test_get_max_frl_rate_4lanes_10gbps(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_max_frl_rate(4, 10), 5);
}

/**
 * dm_test_get_max_frl_rate_4lanes_12gbps - Test Get max frl rate 4lanes 12gbps
 * @test: The KUnit test context
 */
static void dm_test_get_max_frl_rate_4lanes_12gbps(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_max_frl_rate(4, 12), 6);
}

/**
 * dm_test_get_max_frl_rate_unknown - Test Get max frl rate unknown
 * @test: The KUnit test context
 */
static void dm_test_get_max_frl_rate_unknown(struct kunit *test)
{
	/* Unknown lane/rate combination → 0 */
	KUNIT_EXPECT_EQ(test, get_max_frl_rate(2, 3), 0);
}

/* Tests for dm_dtn_log_begin() / dm_dtn_log_append_v() / dm_dtn_log_end() */

/**
 * dm_test_dtn_log_buffer_accumulates - Test DTN log buffer accumulation
 * @test: The KUnit test context
 */
static void dm_test_dtn_log_buffer_accumulates(struct kunit *test)
{
	struct dc_log_buffer_ctx log_ctx = {0};

	dm_dtn_log_begin(NULL, &log_ctx);
	dm_dtn_log_append_v(NULL, &log_ctx, "x=%d\n", 7);
	dm_dtn_log_end(NULL, &log_ctx);

	KUNIT_ASSERT_NOT_NULL(test, log_ctx.buf);
	KUNIT_EXPECT_STREQ(test, log_ctx.buf, "[dtn begin]\nx=7\n[dtn end]\n");
	KUNIT_EXPECT_EQ(test, log_ctx.pos, strlen("[dtn begin]\nx=7\n[dtn end]\n"));

	kvfree(log_ctx.buf);
}

/**
 * dm_test_dtn_log_null_ctx_no_crash - Test DTN log helpers with NULL log buffer
 * @test: The KUnit test context
 */
static void dm_test_dtn_log_null_ctx_no_crash(struct kunit *test)
{
	/* NULL log_ctx redirects to dmesg and must not dereference a buffer */
	dm_dtn_log_begin(NULL, NULL);
	dm_dtn_log_append_v(NULL, NULL, "value %d\n", 1);
	dm_dtn_log_end(NULL, NULL);

	KUNIT_EXPECT_TRUE(test, true);
}

/* Tests for dm_helpers_dp_read_dpcd() / dm_helpers_dp_write_dpcd() */

/**
 * dm_test_dp_read_dpcd_null_priv - Test DPCD read returns false without connector
 * @test: The KUnit test context
 */
static void dm_test_dp_read_dpcd_null_priv(struct kunit *test)
{
	struct dc_link *link;
	uint8_t data = 0;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* link->priv (aconnector) is NULL → early return false */
	KUNIT_EXPECT_FALSE(test,
			   dm_helpers_dp_read_dpcd(NULL, link, 0, &data, sizeof(data)));
}

/**
 * dm_test_dp_write_dpcd_null_priv - Test DPCD write returns false without connector
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dpcd_null_priv(struct kunit *test)
{
	struct dc_link *link;
	uint8_t data = 0;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* link->priv (aconnector) is NULL → early return false */
	KUNIT_EXPECT_FALSE(test,
			   dm_helpers_dp_write_dpcd(NULL, link, 0, &data, sizeof(data)));
}

/*
 * Stub AUX transfer that ACKs every transaction (zero-filling reads), so
 * drm_dp_dpcd_read()/drm_dp_dpcd_write() report the full transfer size.
 */
static ssize_t dm_test_dpcd_ack_transfer(struct drm_dp_aux *aux,
					 struct drm_dp_aux_msg *msg)
{
	if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_NATIVE_READ)
		memset(msg->buffer, 0, msg->size);
	msg->reply = DP_AUX_NATIVE_REPLY_ACK;
	return msg->size;
}

/*
 * Wire a connector-backed link with a working AUX channel so the DPCD
 * read/write helpers can complete a real transaction.
 */
static struct dc_link *dm_test_dpcd_link(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;
	struct dc_link *link;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	link = dm_kunit_alloc_link(test);
	aconnector = dm_kunit_alloc_connector(test, adev, NULL);

	aconnector->dm_dp_aux.aux.drm_dev = &adev->ddev;
	aconnector->dm_dp_aux.aux.transfer = dm_test_dpcd_ack_transfer;
	drm_dp_aux_init(&aconnector->dm_dp_aux.aux);

	link->priv = aconnector;

	return link;
}

/**
 * dm_test_dp_read_dpcd_success - Test DPCD read returns true on ACKed transfer
 * @test: The KUnit test context
 */
static void dm_test_dp_read_dpcd_success(struct kunit *test)
{
	struct dc_link *link = dm_test_dpcd_link(test);
	uint8_t data = 0;

	KUNIT_EXPECT_TRUE(test,
			  dm_helpers_dp_read_dpcd(NULL, link, 0, &data, sizeof(data)));
}

/**
 * dm_test_dp_write_dpcd_success - Test DPCD write returns true on ACKed transfer
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dpcd_success(struct kunit *test)
{
	struct dc_link *link = dm_test_dpcd_link(test);
	uint8_t data = 0;

	KUNIT_EXPECT_TRUE(test,
			  dm_helpers_dp_write_dpcd(NULL, link, 0, &data, sizeof(data)));
}

/* Tests for dm_helpers_execute_fused_io() */

/**
 * dm_test_execute_fused_io_null_dmub_srv - Test fused IO fails without DMUB service
 * @test: The KUnit test context
 */
static void dm_test_execute_fused_io_null_dmub_srv(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_link *link;
	union dmub_rb_cmd *commands;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);
	mutex_init(&adev->dm.dpia_aux_lock);
	spin_lock_init(&adev->dm.dmub_lock);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);
	commands = kunit_kzalloc(test, sizeof(*commands), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, commands);

	ctx->driver_context = adev;
	link->ctx = ctx;
	commands[0].fused_io.request.u.aux.ddc_line = 0;

	KUNIT_EXPECT_FALSE(test, dm_helpers_execute_fused_io(ctx, link, commands, 1, 1));
}

struct dm_test_synaptics_aux {
	struct drm_dp_aux aux;
	u32 fail_address;
	u32 last_dpcd_write_address;
	u8 rc_result;
	u8 dpcd_read_value;
	u8 dpcd_write_value;
	u8 last_rc_data[16];
	u8 read_rc_data[16];
	u8 last_rc_command;
	u8 rc_commands[32];
	u8 dsc_enable_values[8];
	u32 last_rc_offset;
	u32 last_rc_length;
	unsigned int rc_data_writes;
	unsigned int rc_data_reads;
	unsigned int rc_command_reads;
	unsigned int rc_result_reads;
	unsigned int rc_command_count;
	unsigned int downspread_reads;
	unsigned int downspread_writes;
	unsigned int dsc_enable_writes;
};

static ssize_t dm_test_synaptics_aux_transfer(struct drm_dp_aux *aux,
					      struct drm_dp_aux_msg *msg)
{
	struct dm_test_synaptics_aux *fixture;
	u8 request;
	u8 *buffer;
	size_t copy_size;
	unsigned int index;

	fixture = container_of(aux, struct dm_test_synaptics_aux, aux);
	request = msg->request & ~DP_AUX_I2C_MOT;
	buffer = msg->buffer;

	if (fixture->fail_address == msg->address)
		return -EIO;

	if (request == DP_AUX_NATIVE_WRITE) {
		switch (msg->address) {
		case DP_DOWNSPREAD_CTRL:
			fixture->last_dpcd_write_address = msg->address;
			if (msg->size)
				fixture->dpcd_write_value = buffer[0];
			fixture->downspread_writes++;
			break;
		case SYNAPTICS_RC_DATA:
			copy_size = min_t(size_t, msg->size, sizeof(fixture->last_rc_data));
			memset(fixture->last_rc_data, 0, sizeof(fixture->last_rc_data));
			memcpy(fixture->last_rc_data, buffer, copy_size);
			fixture->rc_data_writes++;
			break;
		case SYNAPTICS_RC_OFFSET:
			if (msg->size >= 4)
				fixture->last_rc_offset = buffer[0] | buffer[1] << 8 |
							  buffer[2] << 16 | buffer[3] << 24;
			break;
		case SYNAPTICS_RC_LENGTH:
			if (msg->size >= 2)
				fixture->last_rc_length = buffer[0] | buffer[1] << 8;
			break;
		case SYNAPTICS_RC_COMMAND:
			fixture->last_rc_command = buffer[0];
			if (fixture->rc_command_count < ARRAY_SIZE(fixture->rc_commands)) {
				fixture->rc_commands[fixture->rc_command_count] = buffer[0] & 0x7f;
				fixture->rc_command_count++;
			}
			break;
		case DP_DSC_ENABLE:
			if (fixture->dsc_enable_writes < ARRAY_SIZE(fixture->dsc_enable_values)) {
				fixture->dsc_enable_values[fixture->dsc_enable_writes] = buffer[0];
				fixture->dsc_enable_writes++;
			}
			break;
		}
		msg->reply = DP_AUX_NATIVE_REPLY_ACK;
		return msg->size;
	}

	if (request == DP_AUX_NATIVE_READ) {
		memset(buffer, 0, msg->size);
		switch (msg->address) {
		case DP_DOWNSPREAD_CTRL:
			if (msg->size)
				buffer[0] = fixture->dpcd_read_value;
			fixture->downspread_reads++;
			break;
		case SYNAPTICS_RC_COMMAND:
			if (msg->size)
				buffer[0] = fixture->last_rc_command & 0x7f;
			fixture->rc_command_reads++;
			break;
		case SYNAPTICS_RC_RESULT:
			if (msg->size)
				buffer[0] = fixture->rc_result;
			fixture->rc_result_reads++;
			break;
		case SYNAPTICS_RC_DATA:
			copy_size = min_t(size_t, msg->size, sizeof(fixture->read_rc_data));
			for (index = 0; index < copy_size; index++)
				buffer[index] = fixture->read_rc_data[index];
			fixture->rc_data_reads++;
			break;
		}
		msg->reply = DP_AUX_NATIVE_REPLY_ACK;
		return msg->size;
	}

	msg->reply = DP_AUX_NATIVE_REPLY_ACK;
	return msg->size;
}

static struct dm_test_synaptics_aux *dm_test_current_aux_recorder;

static ssize_t dm_test_current_aux_transfer(struct drm_dp_aux *aux,
					    struct drm_dp_aux_msg *msg)
{
	return dm_test_synaptics_aux_transfer(&dm_test_current_aux_recorder->aux, msg);
}

static struct dm_test_synaptics_aux *dm_test_alloc_synaptics_aux_with_dev(struct kunit *test,
								 struct drm_device *drm_dev)
{
	struct dm_test_synaptics_aux *fixture;
	unsigned int index;

	fixture = kunit_kzalloc(test, sizeof(*fixture), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fixture);

	for (index = 0; index < ARRAY_SIZE(fixture->read_rc_data); index++)
		fixture->read_rc_data[index] = 0x03;

	fixture->rc_result = 0;
	fixture->aux.drm_dev = drm_dev;
	fixture->aux.transfer = dm_test_synaptics_aux_transfer;
	drm_dp_aux_init(&fixture->aux);

	return fixture;
}

static struct dm_test_synaptics_aux *dm_test_alloc_synaptics_aux(struct kunit *test)
{
	struct amdgpu_device *adev;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	return dm_test_alloc_synaptics_aux_with_dev(test, &adev->ddev);
}

static void dm_test_expect_synaptics_commands(struct kunit *test,
					      struct dm_test_synaptics_aux *fixture,
					      const u8 *expected_commands,
					      unsigned int expected_count)
{
	unsigned int index;

	KUNIT_ASSERT_EQ(test, fixture->rc_command_count, expected_count);

	for (index = 0; index < expected_count; index++)
		KUNIT_EXPECT_EQ(test, fixture->rc_commands[index], expected_commands[index]);
}

static void dm_test_setup_synaptics_stream(struct dc_stream_state *stream,
						  struct dc_link *link)
{
	stream->link = link;
	stream->signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
	link->dpcd_caps.branch_dev_id = DP_BRANCH_DEVICE_ID_90CC24;
	link->dpcd_caps.dpcd_rev.raw = DP_DPCD_REV_14;
	link->dpcd_caps.sink_count.bits.SINK_COUNT = 2;
	memcpy(link->dpcd_caps.branch_dev_name, "SYNA", 4);
}

/**
 * dm_test_execute_synaptics_rc_command_write_success - Test RC write success
 * @test: The KUnit test context
 */
static void dm_test_execute_synaptics_rc_command_write_success(struct kunit *test)
{
	struct dm_test_synaptics_aux *fixture;
	u8 data[5] = { 'P', 'R', 'I', 'U', 'S' };

	fixture = dm_test_alloc_synaptics_aux(test);

	KUNIT_EXPECT_TRUE(test, execute_synaptics_rc_command(&fixture->aux, true,
							     0x01, sizeof(data), 0x123456,
							     data));
	KUNIT_EXPECT_EQ(test, memcmp(fixture->last_rc_data, data, sizeof(data)), 0);
	KUNIT_EXPECT_EQ(test, fixture->last_rc_offset, 0x123456U);
	KUNIT_EXPECT_EQ(test, fixture->last_rc_length, (u32)sizeof(data));
	KUNIT_EXPECT_EQ(test, fixture->last_rc_command, (u8)0x81);
	KUNIT_EXPECT_EQ(test, fixture->rc_command_reads, 1U);
	KUNIT_EXPECT_EQ(test, fixture->rc_result_reads, 1U);
}

/**
 * dm_test_execute_synaptics_rc_command_read_success - Test RC read success
 * @test: The KUnit test context
 */
static void dm_test_execute_synaptics_rc_command_read_success(struct kunit *test)
{
	struct dm_test_synaptics_aux *fixture;
	u8 data[4] = { 0 };
	u8 expected[4] = { 0xa5, 0x5a, 0xc3, 0x3c };

	fixture = dm_test_alloc_synaptics_aux(test);
	memcpy(fixture->read_rc_data, expected, sizeof(expected));

	KUNIT_EXPECT_TRUE(test, execute_synaptics_rc_command(&fixture->aux, false,
							     0x31, sizeof(data), 0x220998,
							     data));
	KUNIT_EXPECT_EQ(test, memcmp(data, expected, sizeof(expected)), 0);
	KUNIT_EXPECT_EQ(test, fixture->rc_data_writes, 0U);
	KUNIT_EXPECT_EQ(test, fixture->rc_data_reads, 1U);
	KUNIT_EXPECT_EQ(test, fixture->last_rc_offset, 0x220998U);
	KUNIT_EXPECT_EQ(test, fixture->last_rc_length, (u32)sizeof(data));
}

/**
 * dm_test_execute_synaptics_rc_command_write_fail - Test RC write failure
 * @test: The KUnit test context
 */
static void dm_test_execute_synaptics_rc_command_write_fail(struct kunit *test)
{
	struct dm_test_synaptics_aux *fixture;
	u8 data = 0;

	fixture = dm_test_alloc_synaptics_aux(test);
	fixture->fail_address = SYNAPTICS_RC_LENGTH;

	KUNIT_EXPECT_FALSE(test, execute_synaptics_rc_command(&fixture->aux, true,
							      0x01, sizeof(data), 0, &data));
	KUNIT_EXPECT_EQ(test, fixture->rc_command_count, 0U);
}

/**
 * dm_test_apply_synaptics_fifo_reset_wa_full - Test full FIFO reset sequence
 * @test: The KUnit test context
 */
static void dm_test_apply_synaptics_fifo_reset_wa_full(struct kunit *test)
{
	static const u8 expected_commands[] = {
		0x01, 0x31, 0x21, 0x31, 0x21, 0x31, 0x21,
		0x31, 0x21, 0x31, 0x31, 0x21, 0x02,
	};
	struct dm_test_synaptics_aux *fixture;

	fixture = dm_test_alloc_synaptics_aux(test);

	apply_synaptics_fifo_reset_wa(&fixture->aux);

	dm_test_expect_synaptics_commands(test, fixture, expected_commands,
					  ARRAY_SIZE(expected_commands));
	KUNIT_EXPECT_EQ(test, fixture->rc_result_reads, (unsigned int)ARRAY_SIZE(expected_commands));
}

/**
 * dm_test_apply_synaptics_fifo_reset_wa_first_fail - Test FIFO reset early exit
 * @test: The KUnit test context
 */
static void dm_test_apply_synaptics_fifo_reset_wa_first_fail(struct kunit *test)
{
	struct dm_test_synaptics_aux *fixture;

	fixture = dm_test_alloc_synaptics_aux(test);
	fixture->rc_result = 0xff;

	apply_synaptics_fifo_reset_wa(&fixture->aux);

	KUNIT_EXPECT_EQ(test, fixture->rc_command_count, 1U);
	KUNIT_EXPECT_EQ(test, fixture->rc_commands[0], (u8)0x01);
}

static void dm_test_write_dsc_enable_synaptics(struct kunit *test,
					       bool link_active,
					       bool enable,
					       bool synaptics_branch,
					       unsigned int expected_dsc_writes,
					       unsigned int expected_rc_commands)
{
	struct dm_test_synaptics_aux *fixture;
	struct dc_stream_state *stream;
	struct dc_link *link;
	u8 ret;

	fixture = dm_test_alloc_synaptics_aux(test);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);
	link = dm_kunit_alloc_link(test);

	dm_test_setup_synaptics_stream(stream, link);
	link->link_status.link_active = link_active;
	if (!synaptics_branch)
		memcpy(link->dpcd_caps.branch_dev_name, "ABCD", 4);

	ret = write_dsc_enable_synaptics_non_virtual_dpcd_mst(&fixture->aux, stream, enable);

	KUNIT_EXPECT_EQ(test, ret, expected_dsc_writes ? 1 : 0);
	KUNIT_EXPECT_EQ(test, fixture->dsc_enable_writes, expected_dsc_writes);
	KUNIT_EXPECT_EQ(test, fixture->rc_command_count, expected_rc_commands);
	if (expected_dsc_writes)
		KUNIT_EXPECT_EQ(test, fixture->dsc_enable_values[0], enable ? 1 : 0);
}

/**
 * dm_test_write_dsc_enable_synaptics_enable_inactive - Test enable plus FIFO reset
 * @test: The KUnit test context
 */
static void dm_test_write_dsc_enable_synaptics_enable_inactive(struct kunit *test)
{
	dm_test_write_dsc_enable_synaptics(test, false, true, true, 1, 13);
}

/**
 * dm_test_write_dsc_enable_synaptics_enable_active - Test enable skips FIFO reset
 * @test: The KUnit test context
 */
static void dm_test_write_dsc_enable_synaptics_enable_active(struct kunit *test)
{
	dm_test_write_dsc_enable_synaptics(test, true, true, true, 1, 0);
}

/**
 * dm_test_write_dsc_enable_synaptics_disable_inactive - Test inactive disable writes DPCD
 * @test: The KUnit test context
 */
static void dm_test_write_dsc_enable_synaptics_disable_inactive(struct kunit *test)
{
	dm_test_write_dsc_enable_synaptics(test, false, false, true, 1, 0);
}

/**
 * dm_test_write_dsc_enable_synaptics_disable_active - Test active disable skips DPCD
 * @test: The KUnit test context
 */
static void dm_test_write_dsc_enable_synaptics_disable_active(struct kunit *test)
{
	dm_test_write_dsc_enable_synaptics(test, true, false, true, 0, 0);
}

/**
 * dm_test_write_dsc_enable_synaptics_enable_non_synaptics - Test non-Synaptics enable
 * @test: The KUnit test context
 */
static void dm_test_write_dsc_enable_synaptics_enable_non_synaptics(struct kunit *test)
{
	dm_test_write_dsc_enable_synaptics(test, false, true, false, 1, 0);
}

/**
 * dm_test_dp_write_dsc_enable_routes_synaptics - Test public DSC helper workaround route
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_routes_synaptics(struct kunit *test)
{
	struct dm_test_synaptics_aux *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_stream_state *stream;
	struct dc_link *link;

	fixture = dm_test_alloc_synaptics_aux(test);
	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);
	link = dm_kunit_alloc_link(test);

	dm_test_setup_synaptics_stream(stream, link);
	stream->dm_stream_context = aconnector;
	aconnector->dc_link = link;
	aconnector->dsc_aux = &fixture->aux;

	KUNIT_EXPECT_TRUE(test, dm_helpers_dp_write_dsc_enable(NULL, stream, true));
	KUNIT_EXPECT_EQ(test, fixture->dsc_enable_writes, 1U);
	KUNIT_EXPECT_EQ(test, fixture->dsc_enable_values[0], (u8)1);
	KUNIT_EXPECT_EQ(test, fixture->rc_command_count, 13U);
}

/* Tests for dm_helpers_dp_mst_start_top_mgr() / dm_helpers_dp_mst_stop_top_mgr() */

/**
 * dm_test_mst_start_top_mgr_null_priv - Test MST start returns false without connector
 * @test: The KUnit test context
 */
static void dm_test_mst_start_top_mgr_null_priv(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	KUNIT_EXPECT_FALSE(test, dm_helpers_dp_mst_start_top_mgr(NULL, link, false));
}

/**
 * dm_test_mst_stop_top_mgr_null_priv - Test MST stop returns false without connector
 * @test: The KUnit test context
 */
static void dm_test_mst_stop_top_mgr_null_priv(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	KUNIT_EXPECT_FALSE(test, dm_helpers_dp_mst_stop_top_mgr(NULL, link));
}

/**
 * dm_test_mst_start_top_mgr_boot - Test MST start boot path on a connector-backed link
 * @test: The KUnit test context
 *
 * Uses the DRM KUnit mock device to back the connector so the link is a
 * realistic connector-backed link. The boot path short-circuits and returns
 * true without touching the MST topology manager.
 */
static void dm_test_mst_start_top_mgr_boot(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;
	struct dc_link *link;

	adev = dm_kunit_alloc_adev(test);

	link = dm_kunit_alloc_link(test);

	aconnector = dm_kunit_alloc_connector(test, adev, NULL);

	link->priv = aconnector;

	KUNIT_EXPECT_TRUE(test, dm_helpers_dp_mst_start_top_mgr(NULL, link, true));
}

/* Tests for dm_helpers_dp_write_hblank_reduction() */

/**
 * dm_test_dp_write_hblank_reduction_false - Test hblank reduction stub returns false
 * @test: The KUnit test context
 */
static void dm_test_dp_write_hblank_reduction_false(struct kunit *test)
{
	KUNIT_EXPECT_FALSE(test, dm_helpers_dp_write_hblank_reduction(NULL, NULL));
}

/* Tests for get_dsc_max_slices() */

/**
 * dm_test_get_dsc_max_slices_1_340 - Test 1 slice at 340 MHz
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_1_340(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(1, 340), 1);
}

/**
 * dm_test_get_dsc_max_slices_2_340 - Test 2 slices at 340 MHz
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_2_340(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(2, 340), 2);
}

/**
 * dm_test_get_dsc_max_slices_4_340 - Test 4 slices at 340 MHz
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_4_340(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(4, 340), 3);
}

/**
 * dm_test_get_dsc_max_slices_8_340 - Test 8 slices at 340 MHz
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_8_340(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(8, 340), 4);
}

/**
 * dm_test_get_dsc_max_slices_8_400 - Test 8 slices at 400 MHz
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_8_400(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(8, 400), 5);
}

/**
 * dm_test_get_dsc_max_slices_12_400 - Test 12 slices at 400 MHz
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_12_400(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(12, 400), 6);
}

/**
 * dm_test_get_dsc_max_slices_16_400 - Test 16 slices at 400 MHz
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_16_400(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(16, 400), 7);
}

/**
 * dm_test_get_dsc_max_slices_unknown - Test unknown combination returns 0
 * @test: The KUnit test context
 */
static void dm_test_get_dsc_max_slices_unknown(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(3, 340), 0);
	KUNIT_EXPECT_EQ(test, get_dsc_max_slices(1, 400), 0);
}

/* Tests for dm_helpers_init_panel_settings() */

/**
 * dm_test_init_panel_settings_pps - Test panel power sequence settings init
 * @test: The KUnit test context
 */
static void dm_test_init_panel_settings_pps(struct kunit *test)
{
	struct dc_panel_config panel_config = {0};
	struct dc_sink *sink;

	sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, sink);

	sink->edid_caps.panel_patch.extra_t3_ms = 100;
	sink->edid_caps.panel_patch.extra_t7_ms = 200;
	sink->edid_caps.panel_patch.extra_delay_backlight_off = 50;
	sink->edid_caps.panel_patch.extra_t12_ms = 300;

	dm_helpers_init_panel_settings(NULL, &panel_config, sink);

	KUNIT_EXPECT_EQ(test, panel_config.pps.extra_t3_ms, 100U);
	KUNIT_EXPECT_EQ(test, panel_config.pps.extra_t7_ms, 200U);
	KUNIT_EXPECT_EQ(test, panel_config.pps.extra_delay_backlight_off, 50U);
	KUNIT_EXPECT_EQ(test, panel_config.pps.extra_post_t7_ms, 0U);
	KUNIT_EXPECT_EQ(test, panel_config.pps.extra_pre_t11_ms, 0U);
	KUNIT_EXPECT_EQ(test, panel_config.pps.extra_t12_ms, 300U);
	KUNIT_EXPECT_EQ(test, panel_config.pps.extra_post_OUI_ms, 0U);
}

/**
 * dm_test_init_panel_settings_dsc - Test DSC defaults in panel settings init
 * @test: The KUnit test context
 */
static void dm_test_init_panel_settings_dsc(struct kunit *test)
{
	struct dc_panel_config panel_config;
	struct dc_sink *sink;

	memset(&panel_config, 0xFF, sizeof(panel_config));

	sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, sink);

	dm_helpers_init_panel_settings(NULL, &panel_config, sink);

	KUNIT_EXPECT_FALSE(test, panel_config.dsc.disable_dsc_edp);
	KUNIT_EXPECT_EQ(test, panel_config.dsc.force_dsc_edp_policy, 0U);
}

/* Tests for dm_helpers_override_panel_settings() */

/**
 * dm_test_override_panel_settings_debug_mask_disables_dsc - Test DSC mask
 * @test: The KUnit test context
 */
static void dm_test_override_panel_settings_debug_mask_disables_dsc(struct kunit *test)
{
	struct dc_context *ctx;
	struct dc_link *link;
	struct dc *dc;
	uint old_debug_mask;

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc);
	link = dm_kunit_alloc_link(test);
	ctx->dc = dc;
	link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;

	old_debug_mask = dm_helpers_get_dc_debug_mask();
	dm_helpers_set_dc_debug_mask(old_debug_mask | DC_DISABLE_DSC);
	dm_helpers_override_panel_settings(ctx, link);
	dm_helpers_set_dc_debug_mask(old_debug_mask);

	KUNIT_EXPECT_TRUE(test, link->panel_config.dsc.disable_dsc_edp);
}

/**
 * dm_test_override_panel_settings_second_edp_disables_psr - Test eDP index 1
 * @test: The KUnit test context
 */
static void dm_test_override_panel_settings_second_edp_disables_psr(struct kunit *test)
{
	struct dc_context *ctx;
	struct dc_link *first_link;
	struct dc_link *second_link;
	struct dc *dc;

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc);
	first_link = dm_kunit_alloc_link(test);
	second_link = dm_kunit_alloc_link(test);

	ctx->dc = dc;
	dc->link_count = 2;
	dc->links[0] = first_link;
	dc->links[1] = second_link;
	first_link->connector_signal = SIGNAL_TYPE_EDP;
	second_link->connector_signal = SIGNAL_TYPE_EDP;

	dm_helpers_override_panel_settings(ctx, second_link);

	KUNIT_EXPECT_TRUE(test, second_link->panel_config.psr.disable_psr);
	KUNIT_EXPECT_TRUE(test, second_link->panel_config.psr.disallow_psrsu);
	KUNIT_EXPECT_TRUE(test, second_link->panel_config.psr.disallow_replay);
}

/* Tests for fill_dc_mst_payload_table_from_drm() */

/**
 * dm_test_fill_mst_payload_table_enable - Test payload table fill on enable
 * @test: The KUnit test context
 */
static void dm_test_fill_mst_payload_table_enable(struct kunit *test)
{
	struct dc_link *link;
	struct drm_dp_mst_atomic_payload payload = {0};
	struct dc_dp_mst_stream_allocation_table table = {0};

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* Pre-existing allocation in the link table */
	link->mst_stream_alloc_table.stream_count = 1;
	link->mst_stream_alloc_table.stream_allocations[0].vcp_id = 1;
	link->mst_stream_alloc_table.stream_allocations[0].slot_count = 4;

	/* New payload to add */
	payload.vcpi = 2;
	payload.time_slots = 8;

	fill_dc_mst_payload_table_from_drm(link, true, &payload, &table);

	/* Should contain both the pre-existing and new allocation */
	KUNIT_EXPECT_EQ(test, table.stream_count, 2);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[0].vcp_id, 1);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[0].slot_count, 4);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[1].vcp_id, 2);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[1].slot_count, 8);
}

/**
 * dm_test_fill_mst_payload_table_disable - Test payload table fill on disable
 * @test: The KUnit test context
 */
static void dm_test_fill_mst_payload_table_disable(struct kunit *test)
{
	struct dc_link *link;
	struct drm_dp_mst_atomic_payload payload = {0};
	struct dc_dp_mst_stream_allocation_table table = {0};

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* Two existing allocations in the link table */
	link->mst_stream_alloc_table.stream_count = 2;
	link->mst_stream_alloc_table.stream_allocations[0].vcp_id = 1;
	link->mst_stream_alloc_table.stream_allocations[0].slot_count = 4;
	link->mst_stream_alloc_table.stream_allocations[1].vcp_id = 2;
	link->mst_stream_alloc_table.stream_allocations[1].slot_count = 8;

	/* Remove vcp_id 1 */
	payload.vcpi = 1;
	payload.time_slots = 4;

	fill_dc_mst_payload_table_from_drm(link, false, &payload, &table);

	/* Only vcp_id 2 should remain */
	KUNIT_EXPECT_EQ(test, table.stream_count, 1);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[0].vcp_id, 2);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[0].slot_count, 8);
}

/**
 * dm_test_fill_mst_payload_table_empty - Test payload table fill when empty
 * @test: The KUnit test context
 */
static void dm_test_fill_mst_payload_table_empty(struct kunit *test)
{
	struct dc_link *link;
	struct drm_dp_mst_atomic_payload payload = {0};
	struct dc_dp_mst_stream_allocation_table table = {0};

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* Enable on an empty table */
	payload.vcpi = 5;
	payload.time_slots = 12;

	fill_dc_mst_payload_table_from_drm(link, true, &payload, &table);

	KUNIT_EXPECT_EQ(test, table.stream_count, 1);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[0].vcp_id, 5);
	KUNIT_EXPECT_EQ(test, table.stream_allocations[0].slot_count, 12);
}

/* Tests for dm_helpers_submit_i2c() */

/**
 * dm_test_submit_i2c_null_priv - Test i2c submit returns false without connector
 * @test: The KUnit test context
 */
static void dm_test_submit_i2c_null_priv(struct kunit *test)
{
	struct dc_link *link;
	struct i2c_command cmd = {0};

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	KUNIT_EXPECT_FALSE(test, dm_helpers_submit_i2c(NULL, link, &cmd));
}

struct dm_test_i2c_adapter {
	struct i2c_adapter base;
	struct kunit *test;
	struct i2c_payload *expected_payloads;
	int expected_num;
	int ret;
	int calls;
};

static void dm_test_i2c_lock_bus(struct i2c_adapter *adapter,
					 unsigned int flags)
{
	rt_mutex_lock(&adapter->bus_lock);
}

static int dm_test_i2c_trylock_bus(struct i2c_adapter *adapter,
					   unsigned int flags)
{
	return rt_mutex_trylock(&adapter->bus_lock);
}

static void dm_test_i2c_unlock_bus(struct i2c_adapter *adapter,
					   unsigned int flags)
{
	rt_mutex_unlock(&adapter->bus_lock);
}

static const struct i2c_lock_operations dm_test_i2c_lock_ops = {
	.lock_bus = dm_test_i2c_lock_bus,
	.trylock_bus = dm_test_i2c_trylock_bus,
	.unlock_bus = dm_test_i2c_unlock_bus,
};

static int dm_test_i2c_master_xfer(struct i2c_adapter *adapter,
					   struct i2c_msg *msgs,
					   int num)
{
	struct dm_test_i2c_adapter *fake;
	int i;

	fake = container_of(adapter, struct dm_test_i2c_adapter, base);
	fake->calls++;

	KUNIT_EXPECT_EQ(fake->test, num, fake->expected_num);

	for (i = 0; i < num; i++) {
		KUNIT_EXPECT_EQ(fake->test, msgs[i].flags,
				 fake->expected_payloads[i].write ? 0 : I2C_M_RD);
		KUNIT_EXPECT_EQ(fake->test, msgs[i].addr,
				 (u16)fake->expected_payloads[i].address);
		KUNIT_EXPECT_EQ(fake->test, msgs[i].len,
				 (u16)fake->expected_payloads[i].length);
		KUNIT_EXPECT_PTR_EQ(fake->test, msgs[i].buf,
				    fake->expected_payloads[i].data);
	}

	return fake->ret;
}

static const struct i2c_algorithm dm_test_i2c_algorithm = {
	.master_xfer = dm_test_i2c_master_xfer,
};

struct dm_test_mccs_i2c_adapter {
	struct i2c_adapter base;
	u8 write_data[16];
	u8 read_reply[11];
	int write_ret;
	int read_ret;
	unsigned int write_len;
	unsigned int writes;
	unsigned int reads;
};

static int dm_test_mccs_i2c_master_xfer(struct i2c_adapter *adapter,
						struct i2c_msg *msgs,
						int num)
{
	struct dm_test_mccs_i2c_adapter *fake;
	struct i2c_msg *msg = msgs;
	size_t copy_len;

	fake = container_of(adapter, struct dm_test_mccs_i2c_adapter, base);

	if (num != 1)
		return 0;

	if (msg->flags & I2C_M_RD) {
		fake->reads++;
		if (fake->read_ret != 1)
			return fake->read_ret;

		copy_len = min_t(size_t, msg->len, sizeof(fake->read_reply));
		memcpy(msg->buf, fake->read_reply, copy_len);
		return 1;
	}

	fake->writes++;
	if (fake->write_ret != 1)
		return fake->write_ret;

	copy_len = min_t(size_t, msg->len, sizeof(fake->write_data));
	memcpy(fake->write_data, msg->buf, copy_len);
	fake->write_len = copy_len;

	return 1;
}

static const struct i2c_algorithm dm_test_mccs_i2c_algorithm = {
	.master_xfer = dm_test_mccs_i2c_master_xfer,
};

static struct dm_test_mccs_i2c_adapter *dm_test_alloc_mccs_i2c(struct kunit *test)
{
	struct dm_test_mccs_i2c_adapter *fake;

	fake = kunit_kzalloc(test, sizeof(*fake), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fake);

	fake->base.algo = &dm_test_mccs_i2c_algorithm;
	fake->base.lock_ops = &dm_test_i2c_lock_ops;
	fake->write_ret = 1;
	fake->read_ret = 1;
	rt_mutex_init(&fake->base.bus_lock);
	rt_mutex_init(&fake->base.mux_lock);

	return fake;
}

static u8 dm_test_mccs_checksum(const u8 *data, unsigned int len)
{
	u8 checksum = 0x6e;
	unsigned int i;

	for (i = 0; i < len; i++)
		checksum ^= data[i];

	return checksum;
}

static void dm_test_submit_i2c_transfer(struct kunit *test,
					bool full_transfer)
{
	struct amdgpu_dm_connector *aconnector;
	struct dm_test_i2c_adapter *fake;
	struct dc_link *link;
	u8 write_data[2] = { 0x12, 0x34 };
	u8 read_data[3] = { 0 };
	struct i2c_payload payloads[] = {
		{ true, 0x50, sizeof(write_data), write_data },
		{ false, 0x51, sizeof(read_data), read_data },
	};
	struct i2c_command cmd = {
		.payloads = payloads,
		.number_of_payloads = ARRAY_SIZE(payloads),
	};

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);
	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	fake = kunit_kzalloc(test, sizeof(*fake), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fake);

	fake->test = test;
	fake->expected_payloads = payloads;
	fake->expected_num = ARRAY_SIZE(payloads);
	fake->ret = full_transfer ? ARRAY_SIZE(payloads) : 1;
	fake->base.algo = &dm_test_i2c_algorithm;
	fake->base.lock_ops = &dm_test_i2c_lock_ops;
	rt_mutex_init(&fake->base.bus_lock);
	rt_mutex_init(&fake->base.mux_lock);

	aconnector->i2c = (struct amdgpu_i2c_adapter *)fake;
	link->priv = aconnector;

	KUNIT_EXPECT_EQ(test, dm_helpers_submit_i2c(NULL, link, &cmd), full_transfer);
	KUNIT_EXPECT_EQ(test, fake->calls, 1);
}

/**
 * dm_test_submit_i2c_success - Test payloads are mapped to i2c_msg array
 * @test: The KUnit test context
 */
static void dm_test_submit_i2c_success(struct kunit *test)
{
	dm_test_submit_i2c_transfer(test, true);
}

/**
 * dm_test_submit_i2c_partial_transfer - Test short i2c transfer returns false
 * @test: The KUnit test context
 */
static void dm_test_submit_i2c_partial_transfer(struct kunit *test)
{
	dm_test_submit_i2c_transfer(test, false);
}

/* Tests for dm_helper_dmub_aux_transfer_sync() */

/**
 * dm_test_dmub_aux_transfer_sync_hpd_discon - Test aux transfer with HPD disconnected
 * @test: The KUnit test context
 */
static void dm_test_dmub_aux_transfer_sync_hpd_discon(struct kunit *test)
{
	struct dc_link *link;
	struct dc_context *ctx;
	struct aux_payload payload = {0};
	enum aux_return_code_type result = AUX_RET_SUCCESS;
	int ret;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);
	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);

	link->hpd_status = false;

	ret = dm_helper_dmub_aux_transfer_sync(ctx, link, &payload, &result);

	KUNIT_EXPECT_EQ(test, ret, -1);
	KUNIT_EXPECT_EQ(test, (int)result, (int)AUX_RET_ERROR_HPD_DISCON);
}

/* Tests for empty stub functions (must not crash) */

/**
 * dm_test_dp_update_branch_info_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_dp_update_branch_info_no_crash(struct kunit *test)
{
	dm_helpers_dp_update_branch_info(NULL, NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mst_poll_pending_down_reply_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_mst_poll_pending_down_reply_no_crash(struct kunit *test)
{
	dm_helpers_dp_mst_poll_pending_down_reply(NULL, NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mst_clear_payload_alloc_table_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_mst_clear_payload_alloc_table_no_crash(struct kunit *test)
{
	dm_helpers_dp_mst_clear_payload_allocation_table(NULL, NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_set_dcn_clocks_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_set_dcn_clocks_no_crash(struct kunit *test)
{
	dm_set_dcn_clocks(NULL, NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_dmu_timeout_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_dmu_timeout_no_crash(struct kunit *test)
{
	dm_helpers_dmu_timeout(NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_smu_timeout_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_smu_timeout_no_crash(struct kunit *test)
{
	dm_helpers_smu_timeout(NULL, 0, 0, 0);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_set_phyd32clk_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_set_phyd32clk_no_crash(struct kunit *test)
{
	dm_set_phyd32clk(NULL, 0);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mst_update_branch_bandwidth_no_crash - Test empty stub does not crash
 * @test: The KUnit test context
 */
static void dm_test_mst_update_branch_bandwidth_no_crash(struct kunit *test)
{
	dm_helpers_dp_mst_update_branch_bandwidth(NULL, NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/* Tests for MST functions null-connector early returns */

/**
 * dm_test_mst_write_payload_alloc_table_null_ctx - Test null connector returns false
 * @test: The KUnit test context
 */
static void dm_test_mst_write_payload_alloc_table_null_ctx(struct kunit *test)
{
	struct dc_stream_state *stream;

	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	/* dm_stream_context is NULL → aconnector is NULL → return false */
	KUNIT_EXPECT_FALSE(test,
			   dm_helpers_dp_mst_write_payload_allocation_table(NULL, stream, NULL, true));
}

/**
 * dm_test_mst_poll_for_act_null_ctx - Test null connector returns ACT_FAILED
 * @test: The KUnit test context
 */
static void dm_test_mst_poll_for_act_null_ctx(struct kunit *test)
{
	struct dc_stream_state *stream;

	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	KUNIT_EXPECT_EQ(test,
			(int)dm_helpers_dp_mst_poll_for_allocation_change_trigger(NULL, stream),
			(int)ACT_FAILED);
}

/**
 * dm_test_mst_send_payload_alloc_null_ctx - Test null connector does not crash
 * @test: The KUnit test context
 */
static void dm_test_mst_send_payload_alloc_null_ctx(struct kunit *test)
{
	struct dc_stream_state *stream;

	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	/* Should early-return without crash */
	dm_helpers_dp_mst_send_payload_allocation(NULL, stream);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mst_update_mgr_dealloc_null_ctx - Test null connector does not crash
 * @test: The KUnit test context
 */
static void dm_test_mst_update_mgr_dealloc_null_ctx(struct kunit *test)
{
	struct dc_stream_state *stream;

	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	dm_helpers_dp_mst_update_mst_mgr_for_deallocation(NULL, stream);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_is_dp_sink_present_null_priv - Test null connector returns true
 * @test: The KUnit test context
 */
static void dm_test_is_dp_sink_present_null_priv(struct kunit *test)
{
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* NULL priv → DRM_ERROR + return true */
	KUNIT_EXPECT_TRUE(test, dm_helpers_is_dp_sink_present(link));
}

/* Tests for dm_helpers_dmub_outbox_interrupt_control() */

/**
 * dm_test_dmub_outbox_interrupt_control_null_dc - Test outbox irq control with NULL dc
 * @test: The KUnit test context
 *
 * dc_interrupt_set() is NULL-safe and returns false when dc is NULL, so the
 * helper returns false without touching real interrupt hardware.
 */
static void dm_test_dmub_outbox_interrupt_control_null_dc(struct kunit *test)
{
	struct dc_context *ctx;

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);

	/* ctx->dc is NULL → dc_interrupt_set returns false */
	KUNIT_EXPECT_FALSE(test, dm_helpers_dmub_outbox_interrupt_control(ctx, true));
	KUNIT_EXPECT_FALSE(test, dm_helpers_dmub_outbox_interrupt_control(ctx, false));
}

/* Tests for dm_helpers_mst_enable_stream_features() */

/**
 * dm_test_mst_enable_stream_features_aux_disabled - Test early return when aux disabled
 * @test: The KUnit test context
 */
static void dm_test_mst_enable_stream_features_aux_disabled(struct kunit *test)
{
	struct dc_stream_state *stream;
	struct dc_link *link;

	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	stream->link = link;
	link->aux_access_disabled = true;

	/* aux_access_disabled → early return without DPCD access */
	dm_helpers_mst_enable_stream_features(stream);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mst_enable_stream_features_writes_downspread - Test MSA ignore write
 * @test: The KUnit test context
 */
static void dm_test_mst_enable_stream_features_writes_downspread(struct kunit *test)
{
	struct dm_test_synaptics_aux *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_stream_state *stream;
	struct amdgpu_device *adev;
	struct dc_link *link;

	adev = dm_kunit_alloc_adev(test);
	fixture = dm_test_alloc_synaptics_aux_with_dev(test, &adev->ddev);
	aconnector = dm_kunit_alloc_connector(test, adev, NULL);
	link = dm_kunit_alloc_link(test);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	dm_test_current_aux_recorder = fixture;
	aconnector->dm_dp_aux.aux.drm_dev = &adev->ddev;
	aconnector->dm_dp_aux.aux.transfer = dm_test_current_aux_transfer;
	drm_dp_aux_init(&aconnector->dm_dp_aux.aux);
	link->priv = aconnector;
	stream->link = link;
	stream->ignore_msa_timing_param = true;

	dm_helpers_mst_enable_stream_features(stream);

	KUNIT_EXPECT_EQ(test, fixture->downspread_reads, 1U);
	KUNIT_EXPECT_EQ(test, fixture->downspread_writes, 1U);
	KUNIT_EXPECT_EQ(test, fixture->last_dpcd_write_address,
			 DP_DOWNSPREAD_CTRL);
	KUNIT_EXPECT_EQ(test, fixture->dpcd_write_value, (u8)BIT(7));
}

/* Tests for dm_helpers_enable_periodic_detection() */

struct dm_test_idle_work {
	struct idle_workqueue base;
	bool ran;
};

static void dm_test_idle_work_func(struct work_struct *work)
{
	struct dm_test_idle_work *idle_work;

	idle_work = container_of(work, struct dm_test_idle_work, base.work);
	idle_work->ran = true;
}

/**
 * dm_test_enable_periodic_detection_no_workqueue - Test no-op without idle workqueue
 * @test: The KUnit test context
 */
static void dm_test_enable_periodic_detection_no_workqueue(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;

	adev = dm_kunit_alloc_adev(test);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	ctx->driver_context = adev;

	/* adev->dm.idle_workqueue is NULL → no-op, no crash */
	dm_helpers_enable_periodic_detection(ctx, true);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_enable_periodic_detection_updates_enable - Test idle work enable flag
 * @test: The KUnit test context
 *
 * Keeps idle_workqueue->running set so the helper only updates the enable flag
 * and does not queue the idle worker.
 */
static void dm_test_enable_periodic_detection_updates_enable(struct kunit *test)
{
	struct idle_workqueue *idle_work;
	struct amdgpu_device *adev;
	struct dc_context *ctx;

	adev = dm_kunit_alloc_adev(test);
	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	idle_work = kunit_kzalloc(test, sizeof(*idle_work), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, idle_work);

	ctx->driver_context = adev;
	adev->dm.idle_workqueue = idle_work;
	idle_work->running = true;

	dm_helpers_enable_periodic_detection(ctx, true);
	KUNIT_EXPECT_TRUE(test, idle_work->enable);

	dm_helpers_enable_periodic_detection(ctx, false);
	KUNIT_EXPECT_FALSE(test, idle_work->enable);
}

/**
 * dm_test_enable_periodic_detection_schedules_work - Test headless schedule path
 * @test: The KUnit test context
 */
static void dm_test_enable_periodic_detection_schedules_work(struct kunit *test)
{
	struct dm_test_idle_work *idle_work;
	struct amdgpu_device *adev;
	struct dc_context *ctx;

	adev = dm_kunit_alloc_adev(test);
	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	idle_work = kunit_kzalloc(test, sizeof(*idle_work), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, idle_work);

	ctx->driver_context = adev;
	adev->dm.ddev = &adev->ddev;
	adev->dm.idle_workqueue = &idle_work->base;
	INIT_WORK(&idle_work->base.work, dm_test_idle_work_func);

	dm_helpers_enable_periodic_detection(ctx, true);
	flush_work(&idle_work->base.work);

	KUNIT_EXPECT_TRUE(test, idle_work->base.enable);
	KUNIT_EXPECT_TRUE(test, idle_work->ran);
}

/* Tests for dm_helpers_read_mccs_caps() */

/**
 * dm_test_read_mccs_caps_null_ctx - Test early return with NULL context
 * @test: The KUnit test context
 */
static void dm_test_read_mccs_caps_null_ctx(struct kunit *test)
{
	/* NULL ctx → early return, no crash */
	dm_helpers_read_mccs_caps(NULL, NULL, NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_read_mccs_caps_null_link - Test early return with NULL link
 * @test: The KUnit test context
 */
static void dm_test_read_mccs_caps_null_link(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_sink *sink;

	adev = dm_kunit_alloc_adev(test);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	ctx->driver_context = adev;
	sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, sink);

	/* link is NULL → drm_dbg_driver + return */
	dm_helpers_read_mccs_caps(ctx, NULL, sink);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_read_mccs_caps_no_vcp_code - Test no-vcp-code path clears freesync support
 * @test: The KUnit test context
 *
 * With freesync_vcp_code == 0 the i2c/MCCS path is skipped entirely and the
 * function only clears sink->mccs_caps.freesync_supported.
 */
static void dm_test_read_mccs_caps_no_vcp_code(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_link *link;
	struct dc_sink *sink;

	adev = dm_kunit_alloc_adev(test);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	ctx->driver_context = adev;
	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);
	sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, sink);

	sink->edid_caps.freesync_vcp_code = 0;
	sink->mccs_caps.freesync_supported = true; /* should be cleared */

	dm_helpers_read_mccs_caps(ctx, link, sink);

	KUNIT_EXPECT_FALSE(test, sink->mccs_caps.freesync_supported);
}

/*
 * Allocate and wire the adev/ctx/link/sink/connector/i2c objects shared by the
 * MCCS read/set tests so each test only configures the fields it exercises.
 */
struct dm_test_mccs_fixture {
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_link *link;
	struct dc_sink *sink;
	struct amdgpu_dm_connector *aconnector;
	struct dm_test_mccs_i2c_adapter *fake;
};

static struct dm_test_mccs_fixture dm_test_alloc_mccs_fixture(struct kunit *test)
{
	struct dm_test_mccs_fixture fixture;

	fixture.adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, fixture.adev);
	fixture.ctx = kunit_kzalloc(test, sizeof(*fixture.ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fixture.ctx);
	fixture.link = kunit_kzalloc(test, sizeof(*fixture.link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fixture.link);
	fixture.sink = kunit_kzalloc(test, sizeof(*fixture.sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fixture.sink);
	fixture.aconnector = kunit_kzalloc(test, sizeof(*fixture.aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fixture.aconnector);
	fixture.fake = dm_test_alloc_mccs_i2c(test);

	fixture.ctx->driver_context = fixture.adev;
	fixture.aconnector->i2c = (struct amdgpu_i2c_adapter *)fixture.fake;
	fixture.link->priv = fixture.aconnector;

	return fixture;
}

/**
 * dm_test_read_mccs_caps_i2c_vcp_request - Test MCCS VCP request packet
 * @test: The KUnit test context
 */
static void dm_test_read_mccs_caps_i2c_vcp_request(struct kunit *test)
{
	static const u8 expected_prefix[] = { 0x51, 0x82, 0x01, 0xe3 };
	struct dm_test_mccs_fixture fixture = dm_test_alloc_mccs_fixture(test);
	struct dm_test_mccs_i2c_adapter *fake = fixture.fake;
	struct dc_context *ctx = fixture.ctx;
	struct dc_link *link = fixture.link;
	struct dc_sink *sink = fixture.sink;

	link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
	link->dpcd_caps.dpcd_rev.raw = DP_DPCD_REV_14;
	link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_HDMI_CONVERTER;
	link->dpcd_caps.branch_dev_id = DP_BRANCH_DEVICE_ID_0060AD;
	link->dpcd_caps.adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT = 1;
	sink->edid_caps.freesync_vcp_code = 0xe3;
	fake->read_reply[1] = 0x82;
	fake->read_reply[9] = 0x01;

	dm_helpers_read_mccs_caps(ctx, link, sink);

	KUNIT_EXPECT_TRUE(test, sink->mccs_caps.freesync_supported);
	KUNIT_EXPECT_EQ(test, fake->writes, 1U);
	KUNIT_EXPECT_EQ(test, fake->reads, 1U);
	KUNIT_EXPECT_EQ(test, fake->write_len, (unsigned int)sizeof(expected_prefix) + 1);
	KUNIT_EXPECT_EQ(test, memcmp(fake->write_data, expected_prefix,
					     sizeof(expected_prefix)), 0);
	KUNIT_EXPECT_EQ(test, fake->write_data[4],
			dm_test_mccs_checksum(expected_prefix, sizeof(expected_prefix)));
}

/**
 * dm_test_read_mccs_caps_hdmi_vcp_request - Test local HDMI MCCS path
 * @test: The KUnit test context
 */
static void dm_test_read_mccs_caps_hdmi_vcp_request(struct kunit *test)
{
	struct dm_test_mccs_fixture fixture = dm_test_alloc_mccs_fixture(test);
	struct dm_test_mccs_i2c_adapter *fake = fixture.fake;
	struct dc_context *ctx = fixture.ctx;
	struct dc_link *link = fixture.link;
	struct dc_sink *sink = fixture.sink;

	link->connector_signal = SIGNAL_TYPE_HDMI_TYPE_A;
	sink->edid_caps.freesync_vcp_code = 0xe3;
	fake->read_reply[1] = 0x82;
	fake->read_reply[9] = 0x01;

	dm_helpers_read_mccs_caps(ctx, link, sink);

	KUNIT_EXPECT_TRUE(test, sink->mccs_caps.freesync_supported);
	KUNIT_EXPECT_EQ(test, fake->writes, 1U);
	KUNIT_EXPECT_EQ(test, fake->reads, 1U);
}

/**
 * dm_test_read_mccs_caps_legacy_pcon_vcp_request - Test legacy PCON path
 * @test: The KUnit test context
 */
static void dm_test_read_mccs_caps_legacy_pcon_vcp_request(struct kunit *test)
{
	struct dm_test_mccs_fixture fixture = dm_test_alloc_mccs_fixture(test);
	struct dm_test_mccs_i2c_adapter *fake = fixture.fake;
	struct dc_context *ctx = fixture.ctx;
	struct dc_link *link = fixture.link;
	struct dc_sink *sink = fixture.sink;

	link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
	link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_DVI_CONVERTER;
	sink->edid_caps.freesync_vcp_code = 0xe3;
	fake->read_reply[1] = 0x82;
	fake->read_reply[9] = 0x01;

	dm_helpers_read_mccs_caps(ctx, link, sink);

	KUNIT_EXPECT_TRUE(test, sink->mccs_caps.freesync_supported);
	KUNIT_EXPECT_EQ(test, fake->writes, 1U);
	KUNIT_EXPECT_EQ(test, fake->reads, 1U);
}

/**
 * dm_test_read_mccs_caps_i2c_failure - Test VCP request retry failure
 * @test: The KUnit test context
 */
static void dm_test_read_mccs_caps_i2c_failure(struct kunit *test)
{
	struct dm_test_mccs_fixture fixture = dm_test_alloc_mccs_fixture(test);
	struct dm_test_mccs_i2c_adapter *fake = fixture.fake;
	struct dc_context *ctx = fixture.ctx;
	struct dc_link *link = fixture.link;
	struct dc_sink *sink = fixture.sink;

	fixture.aconnector->base.dev = &fixture.adev->ddev;
	link->connector_signal = SIGNAL_TYPE_DISPLAY_PORT;
	link->dpcd_caps.dpcd_rev.raw = DP_DPCD_REV_14;
	link->dpcd_caps.dongle_type = DISPLAY_DONGLE_DP_HDMI_CONVERTER;
	link->dpcd_caps.branch_dev_id = DP_BRANCH_DEVICE_ID_0060AD;
	link->dpcd_caps.adaptive_sync_caps.dp_adap_sync_caps.bits.ADAPTIVE_SYNC_SDP_SUPPORT = 1;
	sink->edid_caps.freesync_vcp_code = 0xe3;
	fake->write_ret = 0;

	dm_helpers_read_mccs_caps(ctx, link, sink);

	KUNIT_EXPECT_FALSE(test, sink->mccs_caps.freesync_supported);
	KUNIT_EXPECT_EQ(test, fake->writes, 5U);
	KUNIT_EXPECT_EQ(test, fake->reads, 0U);
}

/* Tests for dm_helpers_mccs_vcp_set() */

/**
 * dm_test_mccs_vcp_set_null_ctx - Test early return with NULL context
 * @test: The KUnit test context
 */
static void dm_test_mccs_vcp_set_null_ctx(struct kunit *test)
{
	/* NULL ctx → early return, no crash */
	dm_helpers_mccs_vcp_set(NULL, NULL, NULL);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mccs_vcp_set_not_supported - Test early return when freesync unsupported
 * @test: The KUnit test context
 */
static void dm_test_mccs_vcp_set_not_supported(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_link *link;
	struct dc_sink *sink;

	adev = dm_kunit_alloc_adev(test);

	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	ctx->driver_context = adev;
	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);
	sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, sink);

	sink->mccs_caps.freesync_supported = false;

	/* freesync not supported → early return without i2c */
	dm_helpers_mccs_vcp_set(ctx, link, sink);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mccs_vcp_set_null_link - Test early return with NULL link
 * @test: The KUnit test context
 */
static void dm_test_mccs_vcp_set_null_link(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_sink *sink;

	adev = dm_kunit_alloc_adev(test);
	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, sink);

	ctx->driver_context = adev;
	dm_helpers_mccs_vcp_set(ctx, NULL, sink);
	KUNIT_EXPECT_TRUE(test, true);
}

/**
 * dm_test_mccs_vcp_set_i2c_packet - Test MCCS VCP set packet
 * @test: The KUnit test context
 */
static void dm_test_mccs_vcp_set_i2c_packet(struct kunit *test)
{
	static const u8 expected_prefix[] = {
		0x51, 0x84, 0x03, 0xe3, 0x01, 0x01,
	};
	struct dm_test_mccs_fixture fixture = dm_test_alloc_mccs_fixture(test);
	struct dm_test_mccs_i2c_adapter *fake = fixture.fake;
	struct dc_context *ctx = fixture.ctx;
	struct dc_link *link = fixture.link;
	struct dc_sink *sink = fixture.sink;

	sink->mccs_caps.freesync_supported = true;
	sink->edid_caps.freesync_vcp_code = 0xe3;

	dm_helpers_mccs_vcp_set(ctx, link, sink);

	KUNIT_EXPECT_EQ(test, fake->writes, 1U);
	KUNIT_EXPECT_EQ(test, fake->reads, 0U);
	KUNIT_EXPECT_EQ(test, fake->write_len, (unsigned int)sizeof(expected_prefix) + 1);
	KUNIT_EXPECT_EQ(test, memcmp(fake->write_data, expected_prefix,
					     sizeof(expected_prefix)), 0);
	KUNIT_EXPECT_EQ(test, fake->write_data[6],
			dm_test_mccs_checksum(expected_prefix, sizeof(expected_prefix)));
}

/**
 * dm_test_mccs_vcp_set_i2c_failure - Test VCP set retry failure path
 * @test: The KUnit test context
 */
static void dm_test_mccs_vcp_set_i2c_failure(struct kunit *test)
{
	struct dm_test_mccs_fixture fixture = dm_test_alloc_mccs_fixture(test);
	struct dm_test_mccs_i2c_adapter *fake = fixture.fake;
	struct dc_context *ctx = fixture.ctx;
	struct dc_link *link = fixture.link;
	struct dc_sink *sink = fixture.sink;

	sink->mccs_caps.freesync_supported = true;
	sink->edid_caps.freesync_vcp_code = 0xe3;
	fake->write_ret = 0;

	dm_helpers_mccs_vcp_set(ctx, link, sink);

	KUNIT_EXPECT_EQ(test, fake->writes, 5U);
	KUNIT_EXPECT_EQ(test, fake->reads, 0U);
}

/* Tests for dm_helpers_construct_old_payload() */

/**
 * dm_test_construct_old_payload_empty_list - Test PBN/time-slot calc, empty list
 * @test: The KUnit test context
 *
 * With no other payloads, next_payload_vc_start stays at mgr->next_start_slot,
 * so allocated time_slots = next_start_slot - vc_start_slot.
 */
static void dm_test_construct_old_payload_empty_list(struct kunit *test)
{
	struct drm_dp_mst_topology_mgr *mgr;
	struct drm_dp_mst_topology_state *mst_state;
	struct drm_dp_mst_atomic_payload *new_payload;
	struct drm_dp_mst_atomic_payload *old_payload;

	mgr = kunit_kzalloc(test, sizeof(*mgr), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mgr);
	mst_state = kunit_kzalloc(test, sizeof(*mst_state), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mst_state);
	new_payload = kunit_kzalloc(test, sizeof(*new_payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, new_payload);
	old_payload = kunit_kzalloc(test, sizeof(*old_payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, old_payload);

	INIT_LIST_HEAD(&mst_state->payloads);
	mgr->next_start_slot = 10;
	mst_state->pbn_div.full = 5 << 12;	/* dfixed_trunc → 5 PBN/slot */
	new_payload->vc_start_slot = 3;

	dm_helpers_construct_old_payload(mgr, mst_state, new_payload,
					 old_payload);

	/* 10 - 3 = 7 slots, 7 * 5 = 35 PBN */
	KUNIT_EXPECT_EQ(test, old_payload->time_slots, 7);
	KUNIT_EXPECT_EQ(test, old_payload->pbn, 35);
}

/**
 * dm_test_construct_old_payload_intervening - Test calc with an intervening payload
 * @test: The KUnit test context
 *
 * A payload whose vc_start_slot falls between the new payload and the manager's
 * next_start_slot narrows the allocated time-slot window.
 */
static void dm_test_construct_old_payload_intervening(struct kunit *test)
{
	struct drm_dp_mst_topology_mgr *mgr;
	struct drm_dp_mst_topology_state *mst_state;
	struct drm_dp_mst_atomic_payload *new_payload;
	struct drm_dp_mst_atomic_payload *other_payload;
	struct drm_dp_mst_atomic_payload *old_payload;

	mgr = kunit_kzalloc(test, sizeof(*mgr), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mgr);
	mst_state = kunit_kzalloc(test, sizeof(*mst_state), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mst_state);
	new_payload = kunit_kzalloc(test, sizeof(*new_payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, new_payload);
	other_payload = kunit_kzalloc(test, sizeof(*other_payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, other_payload);
	old_payload = kunit_kzalloc(test, sizeof(*old_payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, old_payload);

	INIT_LIST_HEAD(&mst_state->payloads);
	mgr->next_start_slot = 10;
	mst_state->pbn_div.full = 5 << 12;
	new_payload->vc_start_slot = 3;

	/* other payload at slot 6 (between 3 and 10) narrows window to 6 */
	other_payload->vc_start_slot = 6;
	list_add_tail(&other_payload->next, &mst_state->payloads);

	dm_helpers_construct_old_payload(mgr, mst_state, new_payload,
					 old_payload);

	/* 6 - 3 = 3 slots, 3 * 5 = 15 PBN */
	KUNIT_EXPECT_EQ(test, old_payload->time_slots, 3);
	KUNIT_EXPECT_EQ(test, old_payload->pbn, 15);
}

/* Tests for dm_helpers_dp_mst_write_payload_allocation_table() success path */

/**
 * dm_test_write_payload_alloc_table_success - Exercise the MST success path
 * @test: The KUnit test context
 * @enable: true for the add-payload path, false for the remove-payload path
 *
 * Builds a minimal MST topology-state fixture so the helper traverses past the
 * early NULL checks and runs the real DRM MST payload helpers
 * (drm_dp_add_payload_part1 / drm_dp_remove_payload_part1). With
 * mgr->mst_primary == NULL the topology walk fails gracefully, so no remote
 * DPCD/AUX traffic is generated, and the helper still returns true.
 */
static void dm_test_write_payload_alloc_table_success(struct kunit *test,
						      bool enable)
{
	struct amdgpu_device *adev;
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_mst_topology_mgr *mgr;
	struct drm_dp_mst_topology_state *mst_state;
	struct drm_dp_mst_atomic_payload *payload;
	struct drm_dp_mst_port *port;
	struct dc_stream_state *stream;
	struct dc_link *link;
	struct dc_dp_mst_stream_allocation_table table = { 0 };
	bool ret;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	mst_state = kunit_kzalloc(test, sizeof(*mst_state), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mst_state);
	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, payload);
	port = kunit_kzalloc(test, sizeof(*port), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, port);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);
	link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, link);

	/* aconnector acts as its own MST root for this fixture */
	aconnector->mst_root = aconnector;
	aconnector->mst_output_port = port;

	mgr = &aconnector->mst_mgr;
	mutex_init(&mgr->lock);
	mgr->dev = &adev->ddev;
	mgr->mst_primary = NULL;		/* topology walk fails gracefully */
	mgr->base.state = &mst_state->base;

	INIT_LIST_HEAD(&mst_state->payloads);
	mst_state->pbn_div.full = 5 << 12;	/* dfixed_trunc → 5 PBN/slot */

	/* payload found by drm_atomic_get_mst_payload_state via matching port */
	payload->port = port;
	payload->vcpi = 1;
	payload->vc_start_slot = 1;
	payload->time_slots = 2;
	list_add_tail(&payload->next, &mst_state->payloads);

	/* pre-existing HW allocation so the disable path finds a VCPI to clear */
	link->mst_stream_alloc_table.stream_count = 1;
	link->mst_stream_alloc_table.stream_allocations[0].vcp_id = 1;
	link->mst_stream_alloc_table.stream_allocations[0].slot_count = 2;

	stream->dm_stream_context = aconnector;
	stream->link = link;

	ret = dm_helpers_dp_mst_write_payload_allocation_table(NULL, stream,
							       &table, enable);

	KUNIT_EXPECT_TRUE(test, ret);

	if (enable)
		/* add path keeps the old entry and appends the new payload */
		KUNIT_EXPECT_EQ(test, table.stream_count, 2);
	else
		/* remove path clears the only entry */
		KUNIT_EXPECT_EQ(test, table.stream_count, 0);
}

/**
 * dm_test_write_payload_alloc_table_enable - Test add-payload success path
 * @test: The KUnit test context
 */
static void dm_test_write_payload_alloc_table_enable(struct kunit *test)
{
	dm_test_write_payload_alloc_table_success(test, true);
}

/**
 * dm_test_write_payload_alloc_table_disable - Test remove-payload success path
 * @test: The KUnit test context
 */
static void dm_test_write_payload_alloc_table_disable(struct kunit *test)
{
	dm_test_write_payload_alloc_table_success(test, false);
}

/* Tests for dm_helpers_dp_mst_poll_for_allocation_change_trigger() */

/*
 * Stub AUX transfer that ACKs a DPCD read of the payload-table update status
 * with the ACT-handled bit set, so drm_dp_check_act_status() returns 0.
 */
static ssize_t dm_test_act_aux_transfer_handled(struct drm_dp_aux *aux,
						struct drm_dp_aux_msg *msg)
{
	if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_NATIVE_READ) {
		memset(msg->buffer, 0, msg->size);
		if (msg->size > 0)
			((u8 *)msg->buffer)[0] = DP_PAYLOAD_ACT_HANDLED;
	}
	msg->reply = DP_AUX_NATIVE_REPLY_ACK;
	return msg->size;
}

/*
 * Stub AUX transfer that fails every transaction, so the ACT status read
 * returns an error and drm_dp_check_act_status() returns non-zero.
 */
static ssize_t dm_test_act_aux_transfer_fail(struct drm_dp_aux *aux,
					     struct drm_dp_aux_msg *msg)
{
	return -EIO;
}

/**
 * dm_test_mst_start_top_mgr_set_mst_fail - Test MST start failure path
 * @test: The KUnit test context
 *
 * With a connector-backed link and a failing AUX channel, the non-boot
 * path calls drm_dp_mst_topology_mgr_set_mst(true), which fails to read the
 * DPCD caps and returns a negative error, so the helper returns false.
 */
static void dm_test_mst_start_top_mgr_set_mst_fail(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_aux *aux;
	struct dc_link *link;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	aux = kunit_kzalloc(test, sizeof(*aux), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aux);
	link = dm_kunit_alloc_link(test);

	aux->drm_dev = &adev->ddev;
	aux->transfer = dm_test_act_aux_transfer_fail;
	drm_dp_aux_init(aux);

	mutex_init(&aconnector->mst_mgr.lock);
	aconnector->mst_mgr.dev = &adev->ddev;
	aconnector->mst_mgr.aux = aux;
	link->priv = aconnector;

	KUNIT_EXPECT_FALSE(test, dm_helpers_dp_mst_start_top_mgr(NULL, link, false));
}

/**
 * dm_test_mst_stop_top_mgr_active - Test MST stop on an active topology manager
 * @test: The KUnit test context
 *
 * With mst_state set, the helper calls drm_dp_mst_topology_mgr_set_mst(false)
 * to disable MST and clears the link lane count. The helper always returns
 * false.
 */
static void dm_test_mst_stop_top_mgr_active(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_aux *aux;
	struct dc_link *link;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	aux = kunit_kzalloc(test, sizeof(*aux), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aux);
	link = dm_kunit_alloc_link(test);

	aux->drm_dev = &adev->ddev;
	aux->transfer = dm_test_act_aux_transfer_handled;
	drm_dp_aux_init(aux);

	mutex_init(&aconnector->mst_mgr.lock);
	aconnector->mst_mgr.dev = &adev->ddev;
	aconnector->mst_mgr.aux = aux;
	aconnector->mst_mgr.mst_state = true;
	link->cur_link_settings.lane_count = 4;
	link->priv = aconnector;

	KUNIT_EXPECT_FALSE(test, dm_helpers_dp_mst_stop_top_mgr(NULL, link));
	KUNIT_EXPECT_EQ(test, link->cur_link_settings.lane_count, 0);
}

/**
 * dm_test_poll_for_act_no_mst_state - Test ACT poll bails when MST not started
 * @test: The KUnit test context
 *
 * With mst_root set but mst_mgr->mst_state false, the helper returns
 * ACT_FAILED before touching the AUX channel.
 */
static void dm_test_poll_for_act_no_mst_state(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct dc_stream_state *stream;

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	aconnector->mst_root = aconnector;
	aconnector->mst_mgr.mst_state = false;
	stream->dm_stream_context = aconnector;

	KUNIT_EXPECT_EQ(test,
			(int)dm_helpers_dp_mst_poll_for_allocation_change_trigger(NULL, stream),
			(int)ACT_FAILED);
}

/**
 * dm_test_poll_for_act_success - Test ACT poll success path
 * @test: The KUnit test context
 *
 * With MST started and the AUX channel reporting ACT handled,
 * drm_dp_check_act_status() returns 0 and the helper returns ACT_SUCCESS.
 */
static void dm_test_poll_for_act_success(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_aux *aux;
	struct dc_stream_state *stream;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	aux = kunit_kzalloc(test, sizeof(*aux), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aux);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	aux->drm_dev = &adev->ddev;
	aux->transfer = dm_test_act_aux_transfer_handled;
	drm_dp_aux_init(aux);

	aconnector->mst_root = aconnector;
	aconnector->mst_mgr.mst_state = true;
	aconnector->mst_mgr.aux = aux;
	stream->dm_stream_context = aconnector;

	KUNIT_EXPECT_EQ(test,
			(int)dm_helpers_dp_mst_poll_for_allocation_change_trigger(NULL, stream),
			(int)ACT_SUCCESS);
}

/**
 * dm_test_poll_for_act_status_failed - Test ACT poll failure path
 * @test: The KUnit test context
 *
 * With MST started but the AUX channel failing, drm_dp_check_act_status()
 * returns non-zero and the helper returns ACT_FAILED.
 */
static void dm_test_poll_for_act_status_failed(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_aux *aux;
	struct dc_stream_state *stream;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	aux = kunit_kzalloc(test, sizeof(*aux), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aux);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	aux->drm_dev = &adev->ddev;
	aux->transfer = dm_test_act_aux_transfer_fail;
	drm_dp_aux_init(aux);

	aconnector->mst_root = aconnector;
	aconnector->mst_mgr.mst_state = true;
	aconnector->mst_mgr.aux = aux;
	stream->dm_stream_context = aconnector;

	KUNIT_EXPECT_EQ(test,
			(int)dm_helpers_dp_mst_poll_for_allocation_change_trigger(NULL, stream),
			(int)ACT_FAILED);
}

/* Tests for dm_helpers_dp_mst_send_payload_allocation() */

/**
 * dm_test_mst_send_payload_alloc_part2_fail - Exercise the failure branch
 * @test: The KUnit test context
 *
 * Builds a minimal MST topology-state fixture so the helper runs past the early
 * NULL checks and calls drm_dp_add_payload_part2(). The payload's allocation
 * status is left at its default (not DRM_DP_MST_PAYLOAD_ALLOCATION_DFP), so
 * drm_dp_add_payload_part2() returns -EIO without any remote DPCD/AUX traffic.
 * The non-zero return drives the failure branch, which clears the
 * MST_ALLOCATE_NEW_PAYLOAD status bit.
 */
static void dm_test_mst_send_payload_alloc_part2_fail(struct kunit *test)
{
	struct amdgpu_device *adev;
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_mst_topology_mgr *mgr;
	struct drm_dp_mst_topology_state *mst_state;
	struct drm_dp_mst_atomic_payload *payload;
	struct drm_dp_mst_port *port;
	struct dc_stream_state *stream;

	adev = dm_kunit_alloc_adev(test);
	KUNIT_ASSERT_NOT_NULL(test, adev);

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	mst_state = kunit_kzalloc(test, sizeof(*mst_state), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mst_state);
	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, payload);
	port = kunit_kzalloc(test, sizeof(*port), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, port);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	/* aconnector acts as its own MST root for this fixture */
	aconnector->mst_root = aconnector;
	aconnector->mst_output_port = port;
	/* drm_dp_add_payload_part2() logs port->connector->name on failure */
	port->connector = &aconnector->base;

	mgr = &aconnector->mst_mgr;
	mgr->dev = &adev->ddev;
	mgr->base.state = &mst_state->base;

	INIT_LIST_HEAD(&mst_state->payloads);

	/* payload found by drm_atomic_get_mst_payload_state via matching port */
	payload->port = port;
	list_add_tail(&payload->next, &mst_state->payloads);

	/* pre-set the bit so we can observe it being cleared on failure */
	aconnector->mst_status = MST_ALLOCATE_NEW_PAYLOAD;

	stream->dm_stream_context = aconnector;
	dm_helpers_dp_mst_send_payload_allocation(NULL, stream);

	KUNIT_EXPECT_EQ(test,
			aconnector->mst_status & MST_ALLOCATE_NEW_PAYLOAD, 0);
}

/* Tests for dm_helpers_dp_mst_update_mst_mgr_for_deallocation() */

/**
 * dm_test_mst_update_mgr_dealloc_success - Exercise the deallocation path
 * @test: The KUnit test context
 *
 * Builds a minimal MST topology-state fixture so the helper runs past the early
 * NULL checks through dm_helpers_construct_old_payload() and
 * drm_dp_remove_payload_part2(), both of which are pure list/slot math with no
 * remote DPCD/AUX traffic. Afterwards MST_CLEAR_ALLOCATED_PAYLOAD must be set,
 * MST_ALLOCATE_NEW_PAYLOAD cleared, and the payload's slot released.
 */
static void dm_test_mst_update_mgr_dealloc_success(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_mst_topology_mgr *mgr;
	struct drm_dp_mst_topology_state *mst_state;
	struct drm_dp_mst_atomic_payload *payload;
	struct drm_dp_mst_port *port;
	struct dc_stream_state *stream;

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	mst_state = kunit_kzalloc(test, sizeof(*mst_state), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mst_state);
	payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, payload);
	port = kunit_kzalloc(test, sizeof(*port), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, port);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	/* aconnector acts as its own MST root for this fixture */
	aconnector->mst_root = aconnector;
	aconnector->mst_output_port = port;

	mgr = &aconnector->mst_mgr;
	mgr->base.state = &mst_state->base;
	mgr->next_start_slot = 10;
	mgr->payload_count = 1;

	INIT_LIST_HEAD(&mst_state->payloads);
	mst_state->pbn_div.full = 5 << 12;	/* dfixed_trunc → 5 PBN/slot */

	/* payload found by drm_atomic_get_mst_payload_state via matching port */
	payload->port = port;
	payload->vc_start_slot = 3;
	payload->time_slots = 7;
	list_add_tail(&payload->next, &mst_state->payloads);

	/* pre-set the bit so we can observe it being cleared */
	aconnector->mst_status = MST_ALLOCATE_NEW_PAYLOAD;

	stream->dm_stream_context = aconnector;
	dm_helpers_dp_mst_update_mst_mgr_for_deallocation(NULL, stream);

	KUNIT_EXPECT_EQ(test,
			aconnector->mst_status & MST_CLEAR_ALLOCATED_PAYLOAD,
			(int)MST_CLEAR_ALLOCATED_PAYLOAD);
	KUNIT_EXPECT_EQ(test,
			aconnector->mst_status & MST_ALLOCATE_NEW_PAYLOAD, 0);
	/* drm_dp_remove_payload_part2() releases the payload's slot */
	KUNIT_EXPECT_EQ(test, payload->vc_start_slot, -1);
}

/* Tests for dm_helpers_dp_write_dsc_enable() */

/**
 * dm_test_dp_write_dsc_enable_mst_no_aux - Test MST early return without dsc_aux
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_mst_no_aux(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct dc_stream_state *stream;

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	stream->dm_stream_context = aconnector;
	stream->signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
	aconnector->dsc_aux = NULL;

	/* MST signal with NULL dsc_aux → return false */
	KUNIT_EXPECT_FALSE(test, dm_helpers_dp_write_dsc_enable(NULL, stream, true));
}

/**
 * dm_test_dp_write_dsc_enable_non_dp - Test non-DP signal returns false
 * @test: The KUnit test context
 *
 * For an HDMI signal neither the MST nor the DP/eDP block runs, so ret stays 0.
 */
static void dm_test_dp_write_dsc_enable_non_dp(struct kunit *test)
{
	struct amdgpu_dm_connector *aconnector;
	struct dc_stream_state *stream;

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);

	stream->dm_stream_context = aconnector;
	stream->signal = SIGNAL_TYPE_HDMI_TYPE_A;

	/* Non-DP/MST signal → no DPCD write, ret stays 0 (false) */
	KUNIT_EXPECT_FALSE(test, dm_helpers_dp_write_dsc_enable(NULL, stream, true));
}

struct dm_test_dsc_aux_pair {
	struct dm_test_synaptics_aux *main;
	struct dm_test_synaptics_aux *passthrough;
};

static struct amdgpu_dm_connector *dm_test_alloc_dsc_connector(struct kunit *test,
						       struct dc_link *link)
{
	struct amdgpu_dm_connector *aconnector;

	aconnector = kunit_kzalloc(test, sizeof(*aconnector), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, aconnector);
	aconnector->dc_link = link;

	return aconnector;
}

static struct dc_stream_state *dm_test_alloc_dsc_stream(struct kunit *test,
						       struct dc_link *link,
						       enum signal_type signal)
{
	struct dc_stream_state *stream;
	struct dc_sink *sink;

	stream = kunit_kzalloc(test, sizeof(*stream), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, stream);
	sink = kunit_kzalloc(test, sizeof(*sink), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, sink);

	stream->link = link;
	stream->signal = signal;
	stream->sink = sink;
	sink->link = link;

	return stream;
}

static struct dm_test_dsc_aux_pair dm_test_dp_write_dsc_enable_mst(struct kunit *test,
							   bool enable,
							   bool passthrough)
{
	struct dm_test_dsc_aux_pair aux_pair;
	struct amdgpu_dm_connector *aconnector;
	struct drm_dp_mst_port *port;
	struct dc_stream_state *stream;
	struct dc_link *link;
	bool ret;

	link = dm_kunit_alloc_link(test);
	aconnector = dm_test_alloc_dsc_connector(test, link);
	stream = dm_test_alloc_dsc_stream(test, link, SIGNAL_TYPE_DISPLAY_PORT_MST);
	port = kunit_kzalloc(test, sizeof(*port), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, port);
	aux_pair.main = dm_test_alloc_synaptics_aux_with_dev(test, NULL);
	aux_pair.passthrough = dm_test_alloc_synaptics_aux_with_dev(test, NULL);

	stream->dm_stream_context = aconnector;
	aconnector->dsc_aux = &aux_pair.main->aux;
	aconnector->mst_output_port = port;
	if (passthrough)
		port->passthrough_aux = &aux_pair.passthrough->aux;

	ret = dm_helpers_dp_write_dsc_enable(NULL, stream, enable);

	KUNIT_EXPECT_TRUE(test, ret);
	KUNIT_EXPECT_EQ(test, aux_pair.main->dsc_enable_writes, 1U);
	KUNIT_EXPECT_EQ(test, aux_pair.main->dsc_enable_values[0], enable ? 1 : 0);
	KUNIT_EXPECT_EQ(test, aux_pair.passthrough->dsc_enable_writes,
			 passthrough ? 1U : 0U);
	if (passthrough)
		KUNIT_EXPECT_EQ(test, aux_pair.passthrough->dsc_enable_values[0], enable ? 2 : 0);

	return aux_pair;
}

/**
 * dm_test_dp_write_dsc_enable_mst_enable_decode_only - Test MST enable decoding write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_mst_enable_decode_only(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_mst(test, true, false);
}

/**
 * dm_test_dp_write_dsc_enable_mst_enable_passthrough - Test MST enable passthrough write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_mst_enable_passthrough(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_mst(test, true, true);
}

/**
 * dm_test_dp_write_dsc_enable_mst_disable_decode_only - Test MST disable decoding write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_mst_disable_decode_only(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_mst(test, false, false);
}

/**
 * dm_test_dp_write_dsc_enable_mst_disable_passthrough - Test MST disable passthrough write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_mst_disable_passthrough(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_mst(test, false, true);
}

static void dm_test_dp_write_dsc_enable_sst(struct kunit *test,
					   enum display_dongle_type dongle_type,
					   bool enable,
					   u8 expected_value)
{
	struct dm_test_synaptics_aux *fixture;
	struct amdgpu_dm_connector *aconnector;
	struct dc_stream_state *stream;
	struct dc_link *link;
	bool ret;

	link = dm_kunit_alloc_link(test);
	aconnector = dm_test_alloc_dsc_connector(test, link);
	stream = dm_test_alloc_dsc_stream(test, link, SIGNAL_TYPE_DISPLAY_PORT);
	fixture = dm_test_alloc_synaptics_aux_with_dev(test, NULL);

	stream->dm_stream_context = aconnector;
	link->priv = aconnector;
	link->dpcd_caps.dongle_type = dongle_type;
	dm_test_current_aux_recorder = fixture;
	aconnector->dm_dp_aux.aux.transfer = dm_test_current_aux_transfer;
	drm_dp_aux_init(&aconnector->dm_dp_aux.aux);

	ret = dm_helpers_dp_write_dsc_enable(NULL, stream, enable);

	KUNIT_EXPECT_TRUE(test, ret);
	KUNIT_EXPECT_EQ(test, fixture->dsc_enable_writes, 1U);
	KUNIT_EXPECT_EQ(test, fixture->dsc_enable_values[0], expected_value);
}

/**
 * dm_test_dp_write_dsc_enable_sst_rx_enable - Test SST RX enable DPCD write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_sst_rx_enable(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_sst(test, DISPLAY_DONGLE_NONE, true, 1);
}

/**
 * dm_test_dp_write_dsc_enable_sst_rx_disable - Test SST RX disable DPCD write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_sst_rx_disable(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_sst(test, DISPLAY_DONGLE_NONE, false, 0);
}

/**
 * dm_test_dp_write_dsc_enable_pcon_enable - Test DP-HDMI PCON enable DPCD write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_pcon_enable(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_sst(test, DISPLAY_DONGLE_DP_HDMI_CONVERTER, true, 1);
}

/**
 * dm_test_dp_write_dsc_enable_pcon_disable - Test DP-HDMI PCON disable DPCD write
 * @test: The KUnit test context
 */
static void dm_test_dp_write_dsc_enable_pcon_disable(struct kunit *test)
{
	dm_test_dp_write_dsc_enable_sst(test, DISPLAY_DONGLE_DP_HDMI_CONVERTER, false, 0);
}

/* Tests for dm_helpers_dp_handle_test_pattern_request() */

/**
 * dm_test_dp_handle_test_pattern_no_pipe - Test no matching pipe returns false
 * @test: The KUnit test context
 */
static void dm_test_dp_handle_test_pattern_no_pipe(struct kunit *test)
{
	union link_test_pattern test_pattern = {0};
	union test_misc test_params = {0};
	struct amdgpu_dm_connector *aconnector;
	struct amdgpu_device *adev;
	struct dc_context *ctx;
	struct dc_state *state;
	struct dc_link *link;
	struct dc *dc;

	adev = dm_kunit_alloc_adev(test);
	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, ctx);
	dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, dc);
	state = kunit_kzalloc(test, sizeof(*state), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, state);
	link = dm_kunit_alloc_link(test);
	aconnector = dm_kunit_alloc_connector(test, adev, link);

	ctx->dc = dc;
	dc->current_state = state;
	link->dc = dc;
	link->priv = aconnector;

	KUNIT_EXPECT_FALSE(test,
			dm_helpers_dp_handle_test_pattern_request(ctx, link,
							       test_pattern,
							       test_params));
}

static struct kunit_case amdgpu_dm_helpers_test_cases[] = {
	/* edid_extract_panel_id */
	KUNIT_CASE(dm_test_edid_extract_panel_id_basic),
	KUNIT_CASE(dm_test_edid_extract_panel_id_zeros),
	/* apply_edid_quirks */
	KUNIT_CASE(dm_test_apply_edid_quirks_dpcd_poweroff_delay),
	KUNIT_CASE(dm_test_apply_edid_quirks_disable_fams),
	KUNIT_CASE(dm_test_apply_edid_quirks_remove_sink_ext_caps),
	KUNIT_CASE(dm_test_apply_edid_quirks_disable_colorimetry),
	KUNIT_CASE(dm_test_apply_edid_quirks_skip_phy_ssc),
	KUNIT_CASE(dm_test_apply_edid_quirks_unknown_noop),
	/* dm_helpers_parse_edid_caps */
	KUNIT_CASE(dm_test_parse_edid_caps_null_edid),
	KUNIT_CASE(dm_test_parse_edid_caps_null_caps),
	KUNIT_CASE(dm_test_parse_edid_caps_valid),
	KUNIT_CASE(dm_test_parse_edid_caps_bad_checksum),
	KUNIT_CASE(dm_test_parse_edid_caps_hdmi_frl),
	KUNIT_CASE(dm_test_parse_edid_caps_hdmi_frl_dsc),
	KUNIT_CASE(dm_test_parse_edid_caps_cea_audio),
	KUNIT_CASE(dm_test_parse_edid_caps_cea_no_speaker),
	/* ACPI / VBIOS / local EDID readers */
	KUNIT_CASE(dm_test_probe_acpi_edid_no_companion),
	KUNIT_CASE(dm_test_read_acpi_edid_debug_mask_disabled),
	KUNIT_CASE(dm_test_read_acpi_edid_non_panel_connector),
	KUNIT_CASE(dm_test_read_acpi_edid_force_off),
	KUNIT_CASE(dm_test_read_vbios_edid_non_embedded),
	KUNIT_CASE(dm_test_read_vbios_edid_missing_callback),
	KUNIT_CASE(dm_test_read_vbios_edid_callback_error),
	KUNIT_CASE(dm_test_read_vbios_edid_missing_fake_edid),
	KUNIT_CASE(dm_test_read_vbios_edid_invalid_fake_edid),
	KUNIT_CASE(dm_test_read_vbios_edid_valid),
	/* dm_is_freesync_pcon_whitelist */
	KUNIT_CASE(dm_test_freesync_pcon_whitelist_all_known),
	KUNIT_CASE(dm_test_freesync_pcon_whitelist_not_in_list),
	KUNIT_CASE(dm_test_freesync_pcon_whitelist_zero),
	/* populate_hdmi_info_from_connector */
	KUNIT_CASE(dm_test_populate_hdmi_scdc_present_true),
	KUNIT_CASE(dm_test_populate_hdmi_scdc_present_false),
	KUNIT_CASE(dm_test_populate_hdmi_frl_dsc_10bpc),
	KUNIT_CASE(dm_test_populate_hdmi_frl_dsc_12bpc),
	KUNIT_CASE(dm_test_populate_hdmi_frl_dsc_unknown_values),
	/* dm_get_adaptive_sync_support_type */
	KUNIT_CASE(dm_test_adaptive_sync_type_none_default),
	KUNIT_CASE(dm_test_adaptive_sync_type_converter_no_conditions),
	KUNIT_CASE(dm_test_adaptive_sync_type_converter_partial_conditions),
	KUNIT_CASE(dm_test_adaptive_sync_type_pcon_whitelist),
	KUNIT_CASE(dm_test_adaptive_sync_type_converter_nonwhitelist),
	/* dm_helpers_is_fullscreen / dm_helpers_is_hdr_on */
	KUNIT_CASE(dm_test_helpers_is_fullscreen_returns_false),
	KUNIT_CASE(dm_test_helpers_is_hdr_on_returns_false),
	/* get_max_frl_rate */
	KUNIT_CASE(dm_test_get_max_frl_rate_3lanes_3gbps),
	KUNIT_CASE(dm_test_get_max_frl_rate_3lanes_6gbps),
	KUNIT_CASE(dm_test_get_max_frl_rate_4lanes_6gbps),
	KUNIT_CASE(dm_test_get_max_frl_rate_4lanes_8gbps),
	KUNIT_CASE(dm_test_get_max_frl_rate_4lanes_10gbps),
	KUNIT_CASE(dm_test_get_max_frl_rate_4lanes_12gbps),
	KUNIT_CASE(dm_test_get_max_frl_rate_unknown),
	/* dm_dtn_log_begin / dm_dtn_log_append_v / dm_dtn_log_end */
	KUNIT_CASE(dm_test_dtn_log_buffer_accumulates),
	KUNIT_CASE(dm_test_dtn_log_null_ctx_no_crash),
	/* dm_helpers_dp_read_dpcd / dm_helpers_dp_write_dpcd */
	KUNIT_CASE(dm_test_dp_read_dpcd_null_priv),
	KUNIT_CASE(dm_test_dp_write_dpcd_null_priv),
	KUNIT_CASE(dm_test_dp_read_dpcd_success),
	KUNIT_CASE(dm_test_dp_write_dpcd_success),
	/* dm_helpers_execute_fused_io */
	KUNIT_CASE(dm_test_execute_fused_io_null_dmub_srv),
	/* Synaptics RC/FIFO/DSC helpers */
	KUNIT_CASE(dm_test_execute_synaptics_rc_command_write_success),
	KUNIT_CASE(dm_test_execute_synaptics_rc_command_read_success),
	KUNIT_CASE(dm_test_execute_synaptics_rc_command_write_fail),
	KUNIT_CASE(dm_test_apply_synaptics_fifo_reset_wa_full),
	KUNIT_CASE(dm_test_apply_synaptics_fifo_reset_wa_first_fail),
	KUNIT_CASE(dm_test_write_dsc_enable_synaptics_enable_inactive),
	KUNIT_CASE(dm_test_write_dsc_enable_synaptics_enable_active),
	KUNIT_CASE(dm_test_write_dsc_enable_synaptics_disable_inactive),
	KUNIT_CASE(dm_test_write_dsc_enable_synaptics_disable_active),
	KUNIT_CASE(dm_test_write_dsc_enable_synaptics_enable_non_synaptics),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_routes_synaptics),
	/* dm_helpers_dp_mst_start_top_mgr / dm_helpers_dp_mst_stop_top_mgr */
	KUNIT_CASE(dm_test_mst_start_top_mgr_null_priv),
	KUNIT_CASE(dm_test_mst_stop_top_mgr_null_priv),
	KUNIT_CASE(dm_test_mst_start_top_mgr_boot),
	KUNIT_CASE(dm_test_mst_start_top_mgr_set_mst_fail),
	KUNIT_CASE(dm_test_mst_stop_top_mgr_active),
	/* dm_helpers_dp_write_hblank_reduction */
	KUNIT_CASE(dm_test_dp_write_hblank_reduction_false),
	/* get_dsc_max_slices */
	KUNIT_CASE(dm_test_get_dsc_max_slices_1_340),
	KUNIT_CASE(dm_test_get_dsc_max_slices_2_340),
	KUNIT_CASE(dm_test_get_dsc_max_slices_4_340),
	KUNIT_CASE(dm_test_get_dsc_max_slices_8_340),
	KUNIT_CASE(dm_test_get_dsc_max_slices_8_400),
	KUNIT_CASE(dm_test_get_dsc_max_slices_12_400),
	KUNIT_CASE(dm_test_get_dsc_max_slices_16_400),
	KUNIT_CASE(dm_test_get_dsc_max_slices_unknown),
	/* dm_helpers_init_panel_settings */
	KUNIT_CASE(dm_test_init_panel_settings_pps),
	KUNIT_CASE(dm_test_init_panel_settings_dsc),
	/* dm_helpers_override_panel_settings */
	KUNIT_CASE(dm_test_override_panel_settings_debug_mask_disables_dsc),
	KUNIT_CASE(dm_test_override_panel_settings_second_edp_disables_psr),
	/* fill_dc_mst_payload_table_from_drm */
	KUNIT_CASE(dm_test_fill_mst_payload_table_enable),
	KUNIT_CASE(dm_test_fill_mst_payload_table_disable),
	KUNIT_CASE(dm_test_fill_mst_payload_table_empty),
	/* dm_helpers_submit_i2c */
	KUNIT_CASE(dm_test_submit_i2c_null_priv),
	KUNIT_CASE(dm_test_submit_i2c_success),
	KUNIT_CASE(dm_test_submit_i2c_partial_transfer),
	/* dm_helper_dmub_aux_transfer_sync */
	KUNIT_CASE(dm_test_dmub_aux_transfer_sync_hpd_discon),
	/* Empty stub functions */
	KUNIT_CASE(dm_test_dp_update_branch_info_no_crash),
	KUNIT_CASE(dm_test_mst_poll_pending_down_reply_no_crash),
	KUNIT_CASE(dm_test_mst_clear_payload_alloc_table_no_crash),
	KUNIT_CASE(dm_test_set_dcn_clocks_no_crash),
	KUNIT_CASE(dm_test_dmu_timeout_no_crash),
	KUNIT_CASE(dm_test_smu_timeout_no_crash),
	KUNIT_CASE(dm_test_set_phyd32clk_no_crash),
	KUNIT_CASE(dm_test_mst_update_branch_bandwidth_no_crash),
	/* dm_helpers_dp_mst_write_payload_allocation_table success path */
	KUNIT_CASE(dm_test_write_payload_alloc_table_enable),
	KUNIT_CASE(dm_test_write_payload_alloc_table_disable),
	/* MST null-connector early returns */
	KUNIT_CASE(dm_test_mst_write_payload_alloc_table_null_ctx),
	KUNIT_CASE(dm_test_mst_poll_for_act_null_ctx),
	/* dm_helpers_dp_mst_poll_for_allocation_change_trigger success/fail */
	KUNIT_CASE(dm_test_poll_for_act_no_mst_state),
	KUNIT_CASE(dm_test_poll_for_act_success),
	KUNIT_CASE(dm_test_poll_for_act_status_failed),
	KUNIT_CASE(dm_test_mst_send_payload_alloc_null_ctx),
	KUNIT_CASE(dm_test_mst_update_mgr_dealloc_null_ctx),
	/* dm_helpers_dp_mst_send_payload_allocation failure path */
	KUNIT_CASE(dm_test_mst_send_payload_alloc_part2_fail),
	/* dm_helpers_dp_mst_update_mst_mgr_for_deallocation success path */
	KUNIT_CASE(dm_test_mst_update_mgr_dealloc_success),
	/* dm_helpers_is_dp_sink_present */
	KUNIT_CASE(dm_test_is_dp_sink_present_null_priv),
	/* dm_helpers_dmub_outbox_interrupt_control */
	KUNIT_CASE(dm_test_dmub_outbox_interrupt_control_null_dc),
	/* dm_helpers_mst_enable_stream_features */
	KUNIT_CASE(dm_test_mst_enable_stream_features_aux_disabled),
	KUNIT_CASE(dm_test_mst_enable_stream_features_writes_downspread),
	/* dm_helpers_enable_periodic_detection */
	KUNIT_CASE(dm_test_enable_periodic_detection_no_workqueue),
	KUNIT_CASE(dm_test_enable_periodic_detection_updates_enable),
	KUNIT_CASE(dm_test_enable_periodic_detection_schedules_work),
	/* dm_helpers_read_mccs_caps */
	KUNIT_CASE(dm_test_read_mccs_caps_null_ctx),
	KUNIT_CASE(dm_test_read_mccs_caps_null_link),
	KUNIT_CASE(dm_test_read_mccs_caps_no_vcp_code),
	KUNIT_CASE(dm_test_read_mccs_caps_i2c_vcp_request),
	KUNIT_CASE(dm_test_read_mccs_caps_hdmi_vcp_request),
	KUNIT_CASE(dm_test_read_mccs_caps_legacy_pcon_vcp_request),
	KUNIT_CASE(dm_test_read_mccs_caps_i2c_failure),
	/* dm_helpers_mccs_vcp_set */
	KUNIT_CASE(dm_test_mccs_vcp_set_null_ctx),
	KUNIT_CASE(dm_test_mccs_vcp_set_not_supported),
	KUNIT_CASE(dm_test_mccs_vcp_set_null_link),
	KUNIT_CASE(dm_test_mccs_vcp_set_i2c_packet),
	KUNIT_CASE(dm_test_mccs_vcp_set_i2c_failure),
	/* dm_helpers_construct_old_payload */
	KUNIT_CASE(dm_test_construct_old_payload_empty_list),
	KUNIT_CASE(dm_test_construct_old_payload_intervening),
	/* dm_helpers_dp_write_dsc_enable */
	KUNIT_CASE(dm_test_dp_write_dsc_enable_mst_no_aux),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_non_dp),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_mst_enable_decode_only),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_mst_enable_passthrough),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_mst_disable_decode_only),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_mst_disable_passthrough),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_sst_rx_enable),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_sst_rx_disable),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_pcon_enable),
	KUNIT_CASE(dm_test_dp_write_dsc_enable_pcon_disable),
	/* dm_helpers_dp_handle_test_pattern_request */
	KUNIT_CASE(dm_test_dp_handle_test_pattern_no_pipe),
	{}
};

static struct kunit_suite amdgpu_dm_helpers_test_suite = {
	.name = "amdgpu_dm_helpers",
	.test_cases = amdgpu_dm_helpers_test_cases,
};

kunit_test_suite(amdgpu_dm_helpers_test_suite);

MODULE_DESCRIPTION("KUnit tests for amdgpu_dm_helpers");
MODULE_LICENSE("Dual MIT/GPL");