hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
 * Author:Mark Yao <mark.yao@rock-chips.com>
 *
 * based on exynos_drm_drv.c
 */
 
#include <linux/dma-buf-cache.h>
#include <linux/dma-mapping.h>
#include <linux/dma-iommu.h>
#include <linux/genalloc.h>
#include <linux/pm_runtime.h>
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/of_graph.h>
#include <linux/of_platform.h>
#include <linux/clk.h>
#include <linux/component.h>
#include <linux/console.h>
#include <linux/iommu.h>
#include <linux/of_reserved_mem.h>
 
#include <drm/drm_debugfs.h>
#include <drm/drm_drv.h>
#include <drm/drm_displayid.h>
#include <drm/drm_fb_helper.h>
#include <drm/drm_gem_cma_helper.h>
#include <drm/drm_of.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_vblank.h>
 
#include "rockchip_drm_drv.h"
#include "rockchip_drm_fb.h"
#include "rockchip_drm_fbdev.h"
#include "rockchip_drm_gem.h"
#include "rockchip_drm_logo.h"
 
#include "../drm_crtc_internal.h"
#include "../drivers/clk/rockchip/clk.h"
 
#define DRIVER_NAME    "rockchip"
#define DRIVER_DESC    "RockChip Soc DRM"
#define DRIVER_DATE    "20140818"
#define DRIVER_MAJOR    3
#define DRIVER_MINOR    0
 
#if IS_ENABLED(CONFIG_DRM_ROCKCHIP_VVOP)
static bool is_support_iommu = false;
#else
static bool is_support_iommu = true;
#endif
static bool iommu_reserve_map;
 
static struct drm_driver rockchip_drm_driver;
 
static unsigned int drm_debug;
module_param_named(debug, drm_debug, int, 0600);
 
static inline bool rockchip_drm_debug_enabled(enum rockchip_drm_debug_category category)
{
   return unlikely(drm_debug & category);
}
 
__printf(3, 4)
void rockchip_drm_dbg(const struct device *dev, enum rockchip_drm_debug_category category,
             const char *format, ...)
{
   struct va_format vaf;
   va_list args;
 
   if (!rockchip_drm_debug_enabled(category))
       return;
 
   va_start(args, format);
   vaf.fmt = format;
   vaf.va = &args;
 
   if (dev)
       dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
   else
       printk(KERN_DEBUG "%pV", &vaf);
 
   va_end(args);
}
 
/**
 * rockchip_drm_wait_vact_end
 * @crtc: CRTC to enable line flag
 * @mstimeout: millisecond for timeout
 *
 * Wait for vact_end line flag irq or timeout.
 *
 * Returns:
 * Zero on success, negative errno on failure.
 */
int rockchip_drm_wait_vact_end(struct drm_crtc *crtc, unsigned int mstimeout)
{
   struct rockchip_drm_private *priv;
   int pipe, ret = 0;
 
   if (!crtc)
       return -ENODEV;
 
   if (mstimeout <= 0)
       return -EINVAL;
 
   priv = crtc->dev->dev_private;
   pipe = drm_crtc_index(crtc);
 
   if (priv->crtc_funcs[pipe] && priv->crtc_funcs[pipe]->wait_vact_end)
       ret = priv->crtc_funcs[pipe]->wait_vact_end(crtc, mstimeout);
 
   return ret;
}
EXPORT_SYMBOL(rockchip_drm_wait_vact_end);
 
void drm_mode_convert_to_split_mode(struct drm_display_mode *mode)
{
   u16 hactive, hfp, hsync, hbp;
 
   hactive = mode->hdisplay;
   hfp = mode->hsync_start - mode->hdisplay;
   hsync = mode->hsync_end - mode->hsync_start;
   hbp = mode->htotal - mode->hsync_end;
 
   mode->clock *= 2;
   mode->crtc_clock *= 2;
   mode->hdisplay = hactive * 2;
   mode->hsync_start = mode->hdisplay + hfp * 2;
   mode->hsync_end = mode->hsync_start + hsync * 2;
   mode->htotal = mode->hsync_end + hbp * 2;
   drm_mode_set_name(mode);
}
EXPORT_SYMBOL(drm_mode_convert_to_split_mode);
 
void drm_mode_convert_to_origin_mode(struct drm_display_mode *mode)
{
   u16 hactive, hfp, hsync, hbp;
 
   hactive = mode->hdisplay;
   hfp = mode->hsync_start - mode->hdisplay;
   hsync = mode->hsync_end - mode->hsync_start;
   hbp = mode->htotal - mode->hsync_end;
 
   mode->clock /= 2;
   mode->crtc_clock /= 2;
   mode->hdisplay = hactive / 2;
   mode->hsync_start = mode->hdisplay + hfp / 2;
   mode->hsync_end = mode->hsync_start + hsync / 2;
   mode->htotal = mode->hsync_end + hbp / 2;
}
EXPORT_SYMBOL(drm_mode_convert_to_origin_mode);
 
/**
 * drm_connector_oob_hotplug_event - Report out-of-band hotplug event to connector
 * @connector: connector to report the event on
 *
 * On some hardware a hotplug event notification may come from outside the display
 * driver / device. An example of this is some USB Type-C setups where the hardware
 * muxes the DisplayPort data and aux-lines but does not pass the altmode HPD
 * status bit to the GPU's DP HPD pin.
 *
 * This function can be used to report these out-of-band events after obtaining
 * a drm_connector reference through calling drm_connector_find_by_fwnode().
 */
void drm_connector_oob_hotplug_event(struct fwnode_handle *connector_fwnode)
{
   struct rockchip_drm_sub_dev *sub_dev;
 
   if (!connector_fwnode || !connector_fwnode->dev)
       return;
 
   sub_dev = rockchip_drm_get_sub_dev(dev_of_node(connector_fwnode->dev));
 
   if (sub_dev && sub_dev->connector && sub_dev->oob_hotplug_event)
       sub_dev->oob_hotplug_event(sub_dev->connector);
}
EXPORT_SYMBOL(drm_connector_oob_hotplug_event);
 
uint32_t rockchip_drm_get_bpp(const struct drm_format_info *info)
{
   /* use whatever a driver has set */
   if (info->cpp[0])
       return info->cpp[0] * 8;
 
   switch (info->format) {
   case DRM_FORMAT_YUV420_8BIT:
       return 12;
   case DRM_FORMAT_YUV420_10BIT:
       return 15;
   case DRM_FORMAT_VUY101010:
       return 30;
   default:
       break;
   }
 
   /* all attempts failed */
   return 0;
}
EXPORT_SYMBOL(rockchip_drm_get_bpp);
 
uint32_t rockchip_drm_get_cycles_per_pixel(uint32_t bus_format)
{
   switch (bus_format) {
   case MEDIA_BUS_FMT_RGB565_1X16:
   case MEDIA_BUS_FMT_RGB666_1X18:
   case MEDIA_BUS_FMT_RGB888_1X24:
   case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
       return 1;
   case MEDIA_BUS_FMT_RGB565_2X8_LE:
   case MEDIA_BUS_FMT_BGR565_2X8_LE:
       return 2;
   case MEDIA_BUS_FMT_RGB666_3X6:
   case MEDIA_BUS_FMT_RGB888_3X8:
   case MEDIA_BUS_FMT_BGR888_3X8:
       return 3;
   case MEDIA_BUS_FMT_RGB888_DUMMY_4X8:
   case MEDIA_BUS_FMT_BGR888_DUMMY_4X8:
       return 4;
   default:
       return 1;
   }
}
EXPORT_SYMBOL(rockchip_drm_get_cycles_per_pixel);
 
/**
 * rockchip_drm_of_find_possible_crtcs - find the possible CRTCs for an active
 * encoder port
 * @dev: DRM device
 * @port: encoder port to scan for endpoints
 *
 * Scan all active endpoints attached to a port, locate their attached CRTCs,
 * and generate the DRM mask of CRTCs which may be attached to this
 * encoder.
 *
 * See Documentation/devicetree/bindings/graph.txt for the bindings.
 */
uint32_t rockchip_drm_of_find_possible_crtcs(struct drm_device *dev,
                        struct device_node *port)
{
   struct device_node *remote_port, *ep;
   uint32_t possible_crtcs = 0;
 
   for_each_endpoint_of_node(port, ep) {
       if (!of_device_is_available(ep))
           continue;
 
       remote_port = of_graph_get_remote_port(ep);
       if (!remote_port) {
           of_node_put(ep);
           continue;
       }
 
       possible_crtcs |= drm_of_crtc_port_mask(dev, remote_port);
 
       of_node_put(remote_port);
   }
 
   return possible_crtcs;
}
EXPORT_SYMBOL(rockchip_drm_of_find_possible_crtcs);
 
static DEFINE_MUTEX(rockchip_drm_sub_dev_lock);
static LIST_HEAD(rockchip_drm_sub_dev_list);
 
void rockchip_connector_update_vfp_for_vrr(struct drm_crtc *crtc, struct drm_display_mode *mode,
                      int vfp)
{
   struct rockchip_drm_sub_dev *sub_dev;
 
   mutex_lock(&rockchip_drm_sub_dev_lock);
   list_for_each_entry(sub_dev, &rockchip_drm_sub_dev_list, list) {
       if (sub_dev->connector->state->crtc == crtc) {
           if (sub_dev->update_vfp_for_vrr)
               sub_dev->update_vfp_for_vrr(sub_dev->connector, mode, vfp);
       }
   }
   mutex_unlock(&rockchip_drm_sub_dev_lock);
}
EXPORT_SYMBOL(rockchip_connector_update_vfp_for_vrr);
 
void rockchip_drm_register_sub_dev(struct rockchip_drm_sub_dev *sub_dev)
{
   mutex_lock(&rockchip_drm_sub_dev_lock);
   list_add_tail(&sub_dev->list, &rockchip_drm_sub_dev_list);
   mutex_unlock(&rockchip_drm_sub_dev_lock);
}
EXPORT_SYMBOL(rockchip_drm_register_sub_dev);
 
void rockchip_drm_unregister_sub_dev(struct rockchip_drm_sub_dev *sub_dev)
{
   mutex_lock(&rockchip_drm_sub_dev_lock);
   list_del(&sub_dev->list);
   mutex_unlock(&rockchip_drm_sub_dev_lock);
}
EXPORT_SYMBOL(rockchip_drm_unregister_sub_dev);
 
struct rockchip_drm_sub_dev *rockchip_drm_get_sub_dev(struct device_node *node)
{
   struct rockchip_drm_sub_dev *sub_dev = NULL;
   bool found = false;
 
   mutex_lock(&rockchip_drm_sub_dev_lock);
   list_for_each_entry(sub_dev, &rockchip_drm_sub_dev_list, list) {
       if (sub_dev->of_node == node) {
           found = true;
           break;
       }
   }
   mutex_unlock(&rockchip_drm_sub_dev_lock);
 
   return found ? sub_dev : NULL;
}
EXPORT_SYMBOL(rockchip_drm_get_sub_dev);
 
int rockchip_drm_get_sub_dev_type(void)
{
   int connector_type = DRM_MODE_CONNECTOR_Unknown;
   struct rockchip_drm_sub_dev *sub_dev = NULL;
 
   mutex_lock(&rockchip_drm_sub_dev_lock);
   list_for_each_entry(sub_dev, &rockchip_drm_sub_dev_list, list) {
       if (sub_dev->connector->encoder) {
           connector_type = sub_dev->connector->connector_type;
           break;
       }
   }
   mutex_unlock(&rockchip_drm_sub_dev_lock);
 
   return connector_type;
}
EXPORT_SYMBOL(rockchip_drm_get_sub_dev_type);
 
u32 rockchip_drm_get_scan_line_time_ns(void)
{
   struct rockchip_drm_sub_dev *sub_dev = NULL;
   struct drm_display_mode *mode;
   int linedur_ns = 0;
 
   mutex_lock(&rockchip_drm_sub_dev_lock);
   list_for_each_entry(sub_dev, &rockchip_drm_sub_dev_list, list) {
       if (sub_dev->connector->encoder && sub_dev->connector->state->crtc) {
           mode = &sub_dev->connector->state->crtc->state->adjusted_mode;
           linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, mode->crtc_clock);
           break;
       }
   }
   mutex_unlock(&rockchip_drm_sub_dev_lock);
 
   return linedur_ns;
}
EXPORT_SYMBOL(rockchip_drm_get_scan_line_time_ns);
 
void rockchip_drm_te_handle(struct drm_crtc *crtc)
{
   struct rockchip_drm_private *priv = crtc->dev->dev_private;
   int pipe = drm_crtc_index(crtc);
 
   if (priv->crtc_funcs[pipe] && priv->crtc_funcs[pipe]->te_handler)
       priv->crtc_funcs[pipe]->te_handler(crtc);
}
EXPORT_SYMBOL(rockchip_drm_te_handle);
 
static const struct drm_display_mode rockchip_drm_default_modes[] = {
   /* 4 - 1280x720@60Hz 16:9 */
   { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
          1430, 1650, 0, 720, 725, 730, 750, 0,
          DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
     .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
   /* 16 - 1920x1080@60Hz 16:9 */
   { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
          2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
          DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
     .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
   /* 31 - 1920x1080@50Hz 16:9 */
   { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
          2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
          DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
     .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
   /* 19 - 1280x720@50Hz 16:9 */
   { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
          1760, 1980, 0, 720, 725, 730, 750, 0,
          DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
     .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
   /* 0x10 - 1024x768@60Hz */
   { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
          1184, 1344, 0,  768, 771, 777, 806, 0,
          DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
   /* 17 - 720x576@50Hz 4:3 */
   { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
          796, 864, 0, 576, 581, 586, 625, 0,
          DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
     .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
   /* 2 - 720x480@60Hz 4:3 */
   { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
          798, 858, 0, 480, 489, 495, 525, 0,
          DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
     .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
};
 
int rockchip_drm_add_modes_noedid(struct drm_connector *connector)
{
   struct drm_device *dev = connector->dev;
   struct drm_display_mode *mode;
   int i, count, num_modes = 0;
 
   mutex_lock(&rockchip_drm_sub_dev_lock);
   count = ARRAY_SIZE(rockchip_drm_default_modes);
 
   for (i = 0; i < count; i++) {
       const struct drm_display_mode *ptr = &rockchip_drm_default_modes[i];
 
       mode = drm_mode_duplicate(dev, ptr);
       if (mode) {
           if (!i)
               mode->type = DRM_MODE_TYPE_PREFERRED;
           drm_mode_probed_add(connector, mode);
           num_modes++;
       }
   }
   mutex_unlock(&rockchip_drm_sub_dev_lock);
 
   return num_modes;
}
EXPORT_SYMBOL(rockchip_drm_add_modes_noedid);
 
static const struct rockchip_drm_width_dclk {
   int width;
   u32 dclk_khz;
} rockchip_drm_dclk[] = {
   {1920, 148500},
   {2048, 200000},
   {2560, 280000},
   {3840, 594000},
   {4096, 594000},
   {7680, 2376000},
};
 
u32 rockchip_drm_get_dclk_by_width(int width)
{
   int i = 0;
   u32 dclk_khz;
 
   for (i = 0; i < ARRAY_SIZE(rockchip_drm_dclk); i++) {
       if (width == rockchip_drm_dclk[i].width) {
           dclk_khz = rockchip_drm_dclk[i].dclk_khz;
           break;
       }
   }
 
   if (i == ARRAY_SIZE(rockchip_drm_dclk)) {
       DRM_ERROR("Can't not find %d width solution and use 148500 khz as max dclk\n", width);
 
       dclk_khz = 148500;
   }
 
   return dclk_khz;
}
EXPORT_SYMBOL(rockchip_drm_get_dclk_by_width);
 
static int
cea_db_tag(const u8 *db)
{
   return db[0] >> 5;
}
 
static int
cea_db_payload_len(const u8 *db)
{
   return db[0] & 0x1f;
}
 
#define for_each_cea_db(cea, i, start, end) \
   for ((i) = (start); \
        (i) < (end) && (i) + cea_db_payload_len(&(cea)[(i)]) < (end); \
        (i) += cea_db_payload_len(&(cea)[(i)]) + 1)
 
#define HDMI_NEXT_HDR_VSDB_OUI 0xd04601
 
static bool cea_db_is_hdmi_next_hdr_block(const u8 *db)
{
   unsigned int oui;
 
   if (cea_db_tag(db) != 0x07)
       return false;
 
   if (cea_db_payload_len(db) < 11)
       return false;
 
   oui = db[3] << 16 | db[2] << 8 | db[1];
 
   return oui == HDMI_NEXT_HDR_VSDB_OUI;
}
 
static bool cea_db_is_hdmi_forum_vsdb(const u8 *db)
{
   unsigned int oui;
 
   if (cea_db_tag(db) != 0x03)
       return false;
 
   if (cea_db_payload_len(db) < 7)
       return false;
 
   oui = db[3] << 16 | db[2] << 8 | db[1];
 
   return oui == HDMI_FORUM_IEEE_OUI;
}
 
static int
cea_db_offsets(const u8 *cea, int *start, int *end)
{
   /* DisplayID CTA extension blocks and top-level CEA EDID
    * block header definitions differ in the following bytes:
    *   1) Byte 2 of the header specifies length differently,
    *   2) Byte 3 is only present in the CEA top level block.
    *
    * The different definitions for byte 2 follow.
    *
    * DisplayID CTA extension block defines byte 2 as:
    *   Number of payload bytes
    *
    * CEA EDID block defines byte 2 as:
    *   Byte number (decimal) within this block where the 18-byte
    *   DTDs begin. If no non-DTD data is present in this extension
    *   block, the value should be set to 04h (the byte after next).
    *   If set to 00h, there are no DTDs present in this block and
    *   no non-DTD data.
    */
   if (cea[0] == 0x81) {
       /*
        * for_each_displayid_db() has already verified
        * that these stay within expected bounds.
        */
       *start = 3;
       *end = *start + cea[2];
   } else if (cea[0] == 0x02) {
       /* Data block offset in CEA extension block */
       *start = 4;
       *end = cea[2];
       if (*end == 0)
           *end = 127;
       if (*end < 4 || *end > 127)
           return -ERANGE;
   } else {
       return -EOPNOTSUPP;
   }
 
   return 0;
}
 
static u8 *find_edid_extension(const struct edid *edid,
                  int ext_id, int *ext_index)
{
   u8 *edid_ext = NULL;
   int i;
 
   /* No EDID or EDID extensions */
   if (edid == NULL || edid->extensions == 0)
       return NULL;
 
   /* Find CEA extension */
   for (i = *ext_index; i < edid->extensions; i++) {
       edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
       if (edid_ext[0] == ext_id)
           break;
   }
 
   if (i >= edid->extensions)
       return NULL;
 
   *ext_index = i + 1;
 
   return edid_ext;
}
 
static int validate_displayid(u8 *displayid, int length, int idx)
{
   int i, dispid_length;
   u8 csum = 0;
   struct displayid_hdr *base;
 
   base = (struct displayid_hdr *)&displayid[idx];
 
   DRM_DEBUG_KMS("base revision 0x%x, length %d, %d %d\n",
             base->rev, base->bytes, base->prod_id, base->ext_count);
 
   /* +1 for DispID checksum */
   dispid_length = sizeof(*base) + base->bytes + 1;
   if (dispid_length > length - idx)
       return -EINVAL;
 
   for (i = 0; i < dispid_length; i++)
       csum += displayid[idx + i];
   if (csum) {
       DRM_NOTE("DisplayID checksum invalid, remainder is %d\n", csum);
       return -EINVAL;
   }
 
   return 0;
}
 
static u8 *find_displayid_extension(const struct edid *edid,
                   int *length, int *idx,
                   int *ext_index)
{
   u8 *displayid = find_edid_extension(edid, 0x70, ext_index);
   struct displayid_hdr *base;
   int ret;
 
   if (!displayid)
       return NULL;
 
   /* EDID extensions block checksum isn't for us */
   *length = EDID_LENGTH - 1;
   *idx = 1;
 
   ret = validate_displayid(displayid, *length, *idx);
   if (ret)
       return NULL;
 
   base = (struct displayid_hdr *)&displayid[*idx];
   *length = *idx + sizeof(*base) + base->bytes;
 
   return displayid;
}
 
static u8 *find_cea_extension(const struct edid *edid)
{
   int length, idx;
   struct displayid_block *block;
   u8 *cea;
   u8 *displayid;
   int ext_index;
 
   /* Look for a top level CEA extension block */
   /* FIXME: make callers iterate through multiple CEA ext blocks? */
   ext_index = 0;
   cea = find_edid_extension(edid, 0x02, &ext_index);
   if (cea)
       return cea;
 
   /* CEA blocks can also be found embedded in a DisplayID block */
   ext_index = 0;
   for (;;) {
       displayid = find_displayid_extension(edid, &length, &idx,
                            &ext_index);
       if (!displayid)
           return NULL;
 
       idx += sizeof(struct displayid_hdr);
       for_each_displayid_db(displayid, block, idx, length) {
           if (block->tag == 0x81)
               return (u8 *)block;
       }
   }
 
   return NULL;
}
 
#define EDID_CEA_YCRCB422    (1 << 4)
 
int rockchip_drm_get_yuv422_format(struct drm_connector *connector,
                  struct edid *edid)
{
   struct drm_display_info *info;
   const u8 *edid_ext;
 
   if (!connector || !edid)
       return -EINVAL;
 
   info = &connector->display_info;
 
   edid_ext = find_cea_extension(edid);
   if (!edid_ext)
       return -EINVAL;
 
   if (edid_ext[3] & EDID_CEA_YCRCB422)
       info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
 
   return 0;
}
EXPORT_SYMBOL(rockchip_drm_get_yuv422_format);
 
static
void get_max_frl_rate(int max_frl_rate, u8 *max_lanes, u8 *max_rate_per_lane)
{
   switch (max_frl_rate) {
   case 1:
       *max_lanes = 3;
       *max_rate_per_lane = 3;
       break;
   case 2:
       *max_lanes = 3;
       *max_rate_per_lane = 6;
       break;
   case 3:
       *max_lanes = 4;
       *max_rate_per_lane = 6;
       break;
   case 4:
       *max_lanes = 4;
       *max_rate_per_lane = 8;
       break;
   case 5:
       *max_lanes = 4;
       *max_rate_per_lane = 10;
       break;
   case 6:
       *max_lanes = 4;
       *max_rate_per_lane = 12;
       break;
   case 0:
   default:
       *max_lanes = 0;
       *max_rate_per_lane = 0;
   }
}
 
#define EDID_DSC_10BPC            (1 << 0)
#define EDID_DSC_12BPC            (1 << 1)
#define EDID_DSC_16BPC            (1 << 2)
#define EDID_DSC_ALL_BPP        (1 << 3)
#define EDID_DSC_NATIVE_420        (1 << 6)
#define EDID_DSC_1P2            (1 << 7)
#define EDID_DSC_MAX_FRL_RATE_MASK    0xf0
#define EDID_DSC_MAX_SLICES        0xf
#define EDID_DSC_TOTAL_CHUNK_KBYTES    0x3f
#define EDID_MAX_FRL_RATE_MASK        0xf0
 
static
void parse_edid_forum_vsdb(struct rockchip_drm_dsc_cap *dsc_cap,
              u8 *max_frl_rate_per_lane, u8 *max_lanes, u8 *add_func,
              const u8 *hf_vsdb)
{
   u8 max_frl_rate;
   u8 dsc_max_frl_rate;
   u8 dsc_max_slices;
 
   if (!hf_vsdb[7])
       return;
 
   DRM_DEBUG_KMS("hdmi_21 sink detected. parsing edid\n");
   max_frl_rate = (hf_vsdb[7] & EDID_MAX_FRL_RATE_MASK) >> 4;
   get_max_frl_rate(max_frl_rate, max_lanes,
            max_frl_rate_per_lane);
 
   *add_func = hf_vsdb[8];
 
   if (cea_db_payload_len(hf_vsdb) < 13)
       return;
 
   dsc_cap->v_1p2 = hf_vsdb[11] & EDID_DSC_1P2;
 
   if (!dsc_cap->v_1p2)
       return;
 
   dsc_cap->native_420 = hf_vsdb[11] & EDID_DSC_NATIVE_420;
   dsc_cap->all_bpp = hf_vsdb[11] & EDID_DSC_ALL_BPP;
 
   if (hf_vsdb[11] & EDID_DSC_16BPC)
       dsc_cap->bpc_supported = 16;
   else if (hf_vsdb[11] & EDID_DSC_12BPC)
       dsc_cap->bpc_supported = 12;
   else if (hf_vsdb[11] & EDID_DSC_10BPC)
       dsc_cap->bpc_supported = 10;
   else
       dsc_cap->bpc_supported = 0;
 
   dsc_max_frl_rate = (hf_vsdb[12] & EDID_DSC_MAX_FRL_RATE_MASK) >> 4;
   get_max_frl_rate(dsc_max_frl_rate, &dsc_cap->max_lanes,
            &dsc_cap->max_frl_rate_per_lane);
   dsc_cap->total_chunk_kbytes = hf_vsdb[13] & EDID_DSC_TOTAL_CHUNK_KBYTES;
 
   dsc_max_slices = hf_vsdb[12] & EDID_DSC_MAX_SLICES;
   switch (dsc_max_slices) {
   case 1:
       dsc_cap->max_slices = 1;
       dsc_cap->clk_per_slice = 340;
       break;
   case 2:
       dsc_cap->max_slices = 2;
       dsc_cap->clk_per_slice = 340;
       break;
   case 3:
       dsc_cap->max_slices = 4;
       dsc_cap->clk_per_slice = 340;
       break;
   case 4:
       dsc_cap->max_slices = 8;
       dsc_cap->clk_per_slice = 340;
       break;
   case 5:
       dsc_cap->max_slices = 8;
       dsc_cap->clk_per_slice = 400;
       break;
   case 6:
       dsc_cap->max_slices = 12;
       dsc_cap->clk_per_slice = 400;
       break;
   case 7:
       dsc_cap->max_slices = 16;
       dsc_cap->clk_per_slice = 400;
       break;
   case 0:
   default:
       dsc_cap->max_slices = 0;
       dsc_cap->clk_per_slice = 0;
   }
}
 
enum {
   VER_26_BYTE_V0,
   VER_15_BYTE_V1,
   VER_12_BYTE_V1,
   VER_12_BYTE_V2,
};
 
static int check_next_hdr_version(const u8 *next_hdr_db)
{
   u16 ver;
 
   ver = (next_hdr_db[5] & 0xf0) << 8 | next_hdr_db[0];
 
   switch (ver) {
   case 0x00f9:
       return VER_26_BYTE_V0;
   case 0x20ee:
       return VER_15_BYTE_V1;
   case 0x20eb:
       return VER_12_BYTE_V1;
   case 0x40eb:
       return VER_12_BYTE_V2;
   default:
       return -ENOENT;
   }
}
 
static void parse_ver_26_v0_data(struct ver_26_v0 *hdr, const u8 *data)
{
   hdr->yuv422_12bit = data[5] & BIT(0);
   hdr->support_2160p_60 = (data[5] & BIT(1)) >> 1;
   hdr->global_dimming = (data[5] & BIT(2)) >> 2;
 
   hdr->dm_major_ver = (data[21] & 0xf0) >> 4;
   hdr->dm_minor_ver = data[21] & 0xf;
 
   hdr->t_min_pq = (data[19] << 4) | ((data[18] & 0xf0) >> 4);
   hdr->t_max_pq = (data[20] << 4) | (data[18] & 0xf);
 
   hdr->rx = (data[7] << 4) | ((data[6] & 0xf0) >> 4);
   hdr->ry = (data[8] << 4) | (data[6] & 0xf);
   hdr->gx = (data[10] << 4) | ((data[9] & 0xf0) >> 4);
   hdr->gy = (data[11] << 4) | (data[9] & 0xf);
   hdr->bx = (data[13] << 4) | ((data[12] & 0xf0) >> 4);
   hdr->by = (data[14] << 4) | (data[12] & 0xf);
   hdr->wx = (data[16] << 4) | ((data[15] & 0xf0) >> 4);
   hdr->wy = (data[17] << 4) | (data[15] & 0xf);
}
 
static void parse_ver_15_v1_data(struct ver_15_v1 *hdr, const u8 *data)
{
   hdr->yuv422_12bit = data[5] & BIT(0);
   hdr->support_2160p_60 = (data[5] & BIT(1)) >> 1;
   hdr->global_dimming = data[6] & BIT(0);
 
   hdr->dm_version = (data[5] & 0x1c) >> 2;
 
   hdr->colorimetry = data[7] & BIT(0);
 
   hdr->t_max_lum = (data[6] & 0xfe) >> 1;
   hdr->t_min_lum = (data[7] & 0xfe) >> 1;
 
   hdr->rx = data[9];
   hdr->ry = data[10];
   hdr->gx = data[11];
   hdr->gy = data[12];
   hdr->bx = data[13];
   hdr->by = data[14];
}
 
static void parse_ver_12_v1_data(struct ver_12_v1 *hdr, const u8 *data)
{
   hdr->yuv422_12bit = data[5] & BIT(0);
   hdr->support_2160p_60 = (data[5] & BIT(1)) >> 1;
   hdr->global_dimming = data[6] & BIT(0);
 
   hdr->dm_version = (data[5] & 0x1c) >> 2;
 
   hdr->colorimetry = data[7] & BIT(0);
 
   hdr->t_max_lum = (data[6] & 0xfe) >> 1;
   hdr->t_min_lum = (data[7] & 0xfe) >> 1;
 
   hdr->low_latency = data[8] & 0x3;
 
   hdr->unique_rx = (data[11] & 0xf8) >> 3;
   hdr->unique_ry = (data[11] & 0x7) << 2 | (data[10] & BIT(0)) << 1 |
       (data[9] & BIT(0));
   hdr->unique_gx = (data[9] & 0xfe) >> 1;
   hdr->unique_gy = (data[10] & 0xfe) >> 1;
   hdr->unique_bx = (data[8] & 0xe0) >> 5;
   hdr->unique_by = (data[8] & 0x1c) >> 2;
}
 
static void parse_ver_12_v2_data(struct ver_12_v2 *hdr, const u8 *data)
{
   hdr->yuv422_12bit = data[5] & BIT(0);
   hdr->backlt_ctrl = (data[5] & BIT(1)) >> 1;
   hdr->global_dimming = (data[6] & BIT(2)) >> 2;
 
   hdr->dm_version = (data[5] & 0x1c) >> 2;
   hdr->backlt_min_luma = data[6] & 0x3;
   hdr->interface = data[7] & 0x3;
   hdr->yuv444_10b_12b = (data[8] & BIT(0)) << 1 | (data[9] & BIT(0));
 
   hdr->t_min_pq_v2 = (data[6] & 0xf8) >> 3;
   hdr->t_max_pq_v2 = (data[7] & 0xf8) >> 3;
 
   hdr->unique_rx = (data[10] & 0xf8) >> 3;
   hdr->unique_ry = (data[11] & 0xf8) >> 3;
   hdr->unique_gx = (data[8] & 0xfe) >> 1;
   hdr->unique_gy = (data[9] & 0xfe) >> 1;
   hdr->unique_bx = data[10] & 0x7;
   hdr->unique_by = data[11] & 0x7;
}
 
static
void parse_next_hdr_block(struct next_hdr_sink_data *sink_data,
             const u8 *next_hdr_db)
{
   int version;
 
   version = check_next_hdr_version(next_hdr_db);
   if (version < 0)
       return;
 
   sink_data->version = version;
 
   switch (version) {
   case VER_26_BYTE_V0:
       parse_ver_26_v0_data(&sink_data->ver_26_v0, next_hdr_db);
       break;
   case VER_15_BYTE_V1:
       parse_ver_15_v1_data(&sink_data->ver_15_v1, next_hdr_db);
       break;
   case VER_12_BYTE_V1:
       parse_ver_12_v1_data(&sink_data->ver_12_v1, next_hdr_db);
       break;
   case VER_12_BYTE_V2:
       parse_ver_12_v2_data(&sink_data->ver_12_v2, next_hdr_db);
       break;
   default:
       break;
   }
}
 
int rockchip_drm_parse_cea_ext(struct rockchip_drm_dsc_cap *dsc_cap,
                  u8 *max_frl_rate_per_lane, u8 *max_lanes, u8 *add_func,
                  const struct edid *edid)
{
   const u8 *edid_ext;
   int i, start, end;
 
   if (!dsc_cap || !max_frl_rate_per_lane || !max_lanes || !edid || !add_func)
       return -EINVAL;
 
   edid_ext = find_cea_extension(edid);
   if (!edid_ext)
       return -EINVAL;
 
   if (cea_db_offsets(edid_ext, &start, &end))
       return -EINVAL;
 
   for_each_cea_db(edid_ext, i, start, end) {
       const u8 *db = &edid_ext[i];
 
       if (cea_db_is_hdmi_forum_vsdb(db))
           parse_edid_forum_vsdb(dsc_cap, max_frl_rate_per_lane,
                         max_lanes, add_func, db);
   }
 
   return 0;
}
EXPORT_SYMBOL(rockchip_drm_parse_cea_ext);
 
int rockchip_drm_parse_next_hdr(struct next_hdr_sink_data *sink_data,
               const struct edid *edid)
{
   const u8 *edid_ext;
   int i, start, end;
 
   if (!sink_data || !edid)
       return -EINVAL;
 
   memset(sink_data, 0, sizeof(struct next_hdr_sink_data));
 
   edid_ext = find_cea_extension(edid);
   if (!edid_ext)
       return -EINVAL;
 
   if (cea_db_offsets(edid_ext, &start, &end))
       return -EINVAL;
 
   for_each_cea_db(edid_ext, i, start, end) {
       const u8 *db = &edid_ext[i];
 
       if (cea_db_is_hdmi_next_hdr_block(db))
           parse_next_hdr_block(sink_data, db);
   }
 
   return 0;
}
EXPORT_SYMBOL(rockchip_drm_parse_next_hdr);
 
#define COLORIMETRY_DATA_BLOCK        0x5
#define USE_EXTENDED_TAG        0x07
 
static bool cea_db_is_hdmi_colorimetry_data_block(const u8 *db)
{
   if (cea_db_tag(db) != USE_EXTENDED_TAG)
       return false;
 
   if (db[1] != COLORIMETRY_DATA_BLOCK)
       return false;
 
   return true;
}
 
int
rockchip_drm_parse_colorimetry_data_block(u8 *colorimetry, const struct edid *edid)
{
   const u8 *edid_ext;
   int i, start, end;
 
   if (!colorimetry || !edid)
       return -EINVAL;
 
   *colorimetry = 0;
 
   edid_ext = find_cea_extension(edid);
   if (!edid_ext)
       return -EINVAL;
 
   if (cea_db_offsets(edid_ext, &start, &end))
       return -EINVAL;
 
   for_each_cea_db(edid_ext, i, start, end) {
       const u8 *db = &edid_ext[i];
 
       if (cea_db_is_hdmi_colorimetry_data_block(db))
           /* As per CEA 861-G spec */
           *colorimetry = ((db[3] & (0x1 << 7)) << 1) | db[2];
   }
 
   return 0;
}
EXPORT_SYMBOL(rockchip_drm_parse_colorimetry_data_block);
 
/*
 * Attach a (component) device to the shared drm dma mapping from master drm
 * device.  This is used by the VOPs to map GEM buffers to a common DMA
 * mapping.
 */
int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
                  struct device *dev)
{
   struct rockchip_drm_private *private = drm_dev->dev_private;
   int ret;
 
   if (!is_support_iommu)
       return 0;
 
   ret = iommu_attach_device(private->domain, dev);
   if (ret) {
       DRM_DEV_ERROR(dev, "Failed to attach iommu device\n");
       return ret;
   }
 
   return 0;
}
 
void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
                   struct device *dev)
{
   struct rockchip_drm_private *private = drm_dev->dev_private;
   struct iommu_domain *domain = private->domain;
 
   if (!is_support_iommu)
       return;
 
   iommu_detach_device(domain, dev);
}
 
void rockchip_drm_crtc_standby(struct drm_crtc *crtc, bool standby)
{
   struct rockchip_drm_private *priv = crtc->dev->dev_private;
   int pipe = drm_crtc_index(crtc);
 
   if (pipe < ROCKCHIP_MAX_CRTC &&
       priv->crtc_funcs[pipe] &&
       priv->crtc_funcs[pipe]->crtc_standby)
       priv->crtc_funcs[pipe]->crtc_standby(crtc, standby);
}
 
int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
                const struct rockchip_crtc_funcs *crtc_funcs)
{
   int pipe = drm_crtc_index(crtc);
   struct rockchip_drm_private *priv = crtc->dev->dev_private;
 
   if (pipe >= ROCKCHIP_MAX_CRTC)
       return -EINVAL;
 
   priv->crtc_funcs[pipe] = crtc_funcs;
 
   return 0;
}
 
void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
{
   int pipe = drm_crtc_index(crtc);
   struct rockchip_drm_private *priv = crtc->dev->dev_private;
 
   if (pipe >= ROCKCHIP_MAX_CRTC)
       return;
 
   priv->crtc_funcs[pipe] = NULL;
}
 
static int rockchip_drm_fault_handler(struct iommu_domain *iommu,
                     struct device *dev,
                     unsigned long iova, int flags, void *arg)
{
   struct drm_device *drm_dev = arg;
   struct rockchip_drm_private *priv = drm_dev->dev_private;
   struct drm_crtc *crtc;
 
   DRM_ERROR("iommu fault handler flags: 0x%x\n", flags);
   drm_for_each_crtc(crtc, drm_dev) {
       int pipe = drm_crtc_index(crtc);
 
       if (priv->crtc_funcs[pipe] &&
           priv->crtc_funcs[pipe]->regs_dump)
           priv->crtc_funcs[pipe]->regs_dump(crtc, NULL);
 
       if (priv->crtc_funcs[pipe] &&
           priv->crtc_funcs[pipe]->debugfs_dump)
           priv->crtc_funcs[pipe]->debugfs_dump(crtc, NULL);
   }
 
   return 0;
}
 
static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
{
   struct rockchip_drm_private *private = drm_dev->dev_private;
   struct iommu_domain_geometry *geometry;
   u64 start, end;
   int ret = 0;
 
   if (!is_support_iommu)
       return 0;
 
   private->domain = iommu_domain_alloc(&platform_bus_type);
   if (!private->domain)
       return -ENOMEM;
 
   geometry = &private->domain->geometry;
   start = geometry->aperture_start;
   end = geometry->aperture_end;
 
   DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
         start, end);
   drm_mm_init(&private->mm, start, end - start + 1);
   mutex_init(&private->mm_lock);
 
   iommu_set_fault_handler(private->domain, rockchip_drm_fault_handler,
               drm_dev);
 
   if (iommu_reserve_map) {
       /*
        * At 32 bit platform size_t maximum value is 0xffffffff, SZ_4G(0x100000000) will be
        * cliped to 0, so we split into two mapping
        */
       ret = iommu_map(private->domain, 0, 0, (size_t)SZ_2G,
               IOMMU_WRITE | IOMMU_READ | IOMMU_PRIV);
       if (ret)
           dev_err(drm_dev->dev, "failed to create 0-2G pre mapping\n");
 
       ret = iommu_map(private->domain, SZ_2G, SZ_2G, (size_t)SZ_2G,
               IOMMU_WRITE | IOMMU_READ | IOMMU_PRIV);
       if (ret)
           dev_err(drm_dev->dev, "failed to create 2G-4G pre mapping\n");
   }
 
   return ret;
}
 
static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
{
   struct rockchip_drm_private *private = drm_dev->dev_private;
 
   if (!is_support_iommu)
       return;
 
   if (iommu_reserve_map) {
       iommu_unmap(private->domain, 0, (size_t)SZ_2G);
       iommu_unmap(private->domain, SZ_2G, (size_t)SZ_2G);
   }
   drm_mm_takedown(&private->mm);
   iommu_domain_free(private->domain);
}
 
#ifdef CONFIG_DEBUG_FS
static int rockchip_drm_mm_dump(struct seq_file *s, void *data)
{
   struct drm_info_node *node = s->private;
   struct drm_minor *minor = node->minor;
   struct drm_device *drm_dev = minor->dev;
   struct rockchip_drm_private *priv = drm_dev->dev_private;
   struct drm_printer p = drm_seq_file_printer(s);
 
   if (!priv->domain)
       return 0;
   mutex_lock(&priv->mm_lock);
   drm_mm_print(&priv->mm, &p);
   mutex_unlock(&priv->mm_lock);
 
   return 0;
}
 
static int rockchip_drm_summary_show(struct seq_file *s, void *data)
{
   struct drm_info_node *node = s->private;
   struct drm_minor *minor = node->minor;
   struct drm_device *drm_dev = minor->dev;
   struct rockchip_drm_private *priv = drm_dev->dev_private;
   struct drm_crtc *crtc;
 
   drm_for_each_crtc(crtc, drm_dev) {
       int pipe = drm_crtc_index(crtc);
 
       if (priv->crtc_funcs[pipe] &&
           priv->crtc_funcs[pipe]->debugfs_dump)
           priv->crtc_funcs[pipe]->debugfs_dump(crtc, s);
   }
 
   return 0;
}
 
static int rockchip_drm_regs_dump(struct seq_file *s, void *data)
{
   struct drm_info_node *node = s->private;
   struct drm_minor *minor = node->minor;
   struct drm_device *drm_dev = minor->dev;
   struct rockchip_drm_private *priv = drm_dev->dev_private;
   struct drm_crtc *crtc;
 
   drm_for_each_crtc(crtc, drm_dev) {
       int pipe = drm_crtc_index(crtc);
 
       if (priv->crtc_funcs[pipe] &&
           priv->crtc_funcs[pipe]->regs_dump)
           priv->crtc_funcs[pipe]->regs_dump(crtc, s);
   }
 
   return 0;
}
 
static int rockchip_drm_active_regs_dump(struct seq_file *s, void *data)
{
   struct drm_info_node *node = s->private;
   struct drm_minor *minor = node->minor;
   struct drm_device *drm_dev = minor->dev;
   struct rockchip_drm_private *priv = drm_dev->dev_private;
   struct drm_crtc *crtc;
 
   drm_for_each_crtc(crtc, drm_dev) {
       int pipe = drm_crtc_index(crtc);
 
       if (priv->crtc_funcs[pipe] &&
           priv->crtc_funcs[pipe]->active_regs_dump)
           priv->crtc_funcs[pipe]->active_regs_dump(crtc, s);
   }
 
   return 0;
}
 
static struct drm_info_list rockchip_debugfs_files[] = {
   { "active_regs", rockchip_drm_active_regs_dump, 0, NULL },
   { "regs", rockchip_drm_regs_dump, 0, NULL },
   { "summary", rockchip_drm_summary_show, 0, NULL },
   { "mm_dump", rockchip_drm_mm_dump, 0, NULL },
};
 
static void rockchip_drm_debugfs_init(struct drm_minor *minor)
{
   struct drm_device *dev = minor->dev;
   struct rockchip_drm_private *priv = dev->dev_private;
   struct drm_crtc *crtc;
 
   drm_debugfs_create_files(rockchip_debugfs_files,
                ARRAY_SIZE(rockchip_debugfs_files),
                minor->debugfs_root, minor);
 
   drm_for_each_crtc(crtc, dev) {
       int pipe = drm_crtc_index(crtc);
 
       if (priv->crtc_funcs[pipe] &&
           priv->crtc_funcs[pipe]->debugfs_init)
           priv->crtc_funcs[pipe]->debugfs_init(minor, crtc);
   }
}
#endif
 
static const struct drm_prop_enum_list split_area[] = {
   { ROCKCHIP_DRM_SPLIT_UNSET, "UNSET" },
   { ROCKCHIP_DRM_SPLIT_LEFT_SIDE, "LEFT" },
   { ROCKCHIP_DRM_SPLIT_RIGHT_SIDE, "RIGHT" },
};
 
static int rockchip_drm_create_properties(struct drm_device *dev)
{
   struct drm_property *prop;
   struct rockchip_drm_private *private = dev->dev_private;
 
   prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
                    "EOTF", 0, 5);
   if (!prop)
       return -ENOMEM;
   private->eotf_prop = prop;
 
   prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
                    "COLOR_SPACE", 0, 12);
   if (!prop)
       return -ENOMEM;
   private->color_space_prop = prop;
 
   prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
                    "ASYNC_COMMIT", 0, 1);
   if (!prop)
       return -ENOMEM;
   private->async_commit_prop = prop;
 
   prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
                    "SHARE_ID", 0, UINT_MAX);
   if (!prop)
       return -ENOMEM;
   private->share_id_prop = prop;
 
   prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_IMMUTABLE,
                    "CONNECTOR_ID", 0, 0xf);
   if (!prop)
       return -ENOMEM;
   private->connector_id_prop = prop;
 
   prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "SPLIT_AREA",
                   split_area,
                   ARRAY_SIZE(split_area));
   private->split_area_prop = prop;
 
   prop = drm_property_create_object(dev,
                     DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_IMMUTABLE,
                     "SOC_ID", DRM_MODE_OBJECT_CRTC);
   private->soc_id_prop = prop;
 
   prop = drm_property_create_object(dev,
                     DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_IMMUTABLE,
                     "PORT_ID", DRM_MODE_OBJECT_CRTC);
   private->port_id_prop = prop;
 
   private->aclk_prop = drm_property_create_range(dev, 0, "ACLK", 0, UINT_MAX);
   private->bg_prop = drm_property_create_range(dev, 0, "BACKGROUND", 0, UINT_MAX);
   private->line_flag_prop = drm_property_create_range(dev, 0, "LINE_FLAG1", 0, UINT_MAX);
   private->cubic_lut_prop = drm_property_create(dev, DRM_MODE_PROP_BLOB, "CUBIC_LUT", 0);
   private->cubic_lut_size_prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE,
                                "CUBIC_LUT_SIZE", 0, UINT_MAX);
 
   return drm_mode_create_tv_properties(dev, 0, NULL);
}
 
static void rockchip_attach_connector_property(struct drm_device *drm)
{
   struct drm_connector *connector;
   struct drm_mode_config *conf = &drm->mode_config;
   struct drm_connector_list_iter conn_iter;
 
   mutex_lock(&drm->mode_config.mutex);
 
#define ROCKCHIP_PROP_ATTACH(prop, v) \
       drm_object_attach_property(&connector->base, prop, v)
 
   drm_connector_list_iter_begin(drm, &conn_iter);
   drm_for_each_connector_iter(connector, &conn_iter) {
       ROCKCHIP_PROP_ATTACH(conf->tv_brightness_property, 50);
       ROCKCHIP_PROP_ATTACH(conf->tv_contrast_property, 50);
       ROCKCHIP_PROP_ATTACH(conf->tv_saturation_property, 50);
       ROCKCHIP_PROP_ATTACH(conf->tv_hue_property, 50);
   }
   drm_connector_list_iter_end(&conn_iter);
#undef ROCKCHIP_PROP_ATTACH
 
   mutex_unlock(&drm->mode_config.mutex);
}
 
static void rockchip_drm_set_property_default(struct drm_device *drm)
{
   struct drm_connector *connector;
   struct drm_mode_config *conf = &drm->mode_config;
   struct drm_atomic_state *state;
   int ret;
   struct drm_connector_list_iter conn_iter;
 
   drm_modeset_lock_all(drm);
 
   state = drm_atomic_helper_duplicate_state(drm, conf->acquire_ctx);
   if (IS_ERR(state)) {
       DRM_ERROR("failed to alloc atomic state\n");
       goto err_unlock;
   }
   state->acquire_ctx = conf->acquire_ctx;
 
   drm_connector_list_iter_begin(drm, &conn_iter);
   drm_for_each_connector_iter(connector, &conn_iter) {
       struct drm_connector_state *connector_state;
 
       connector_state = drm_atomic_get_connector_state(state,
                                connector);
       if (IS_ERR(connector_state)) {
           DRM_ERROR("Connector[%d]: Failed to get state\n", connector->base.id);
           continue;
       }
 
       connector_state->tv.brightness = 50;
       connector_state->tv.contrast = 50;
       connector_state->tv.saturation = 50;
       connector_state->tv.hue = 50;
   }
   drm_connector_list_iter_end(&conn_iter);
 
   ret = drm_atomic_commit(state);
   WARN_ON(ret == -EDEADLK);
   if (ret)
       DRM_ERROR("Failed to update properties\n");
   drm_atomic_state_put(state);
 
err_unlock:
   drm_modeset_unlock_all(drm);
}
 
static int rockchip_gem_pool_init(struct drm_device *drm)
{
   struct rockchip_drm_private *private = drm->dev_private;
   struct device_node *np = drm->dev->of_node;
   struct device_node *node;
   phys_addr_t start, size;
   struct resource res;
   int ret;
 
   node = of_parse_phandle(np, "secure-memory-region", 0);
   if (!node)
       return -ENXIO;
 
   ret = of_address_to_resource(node, 0, &res);
   if (ret)
       return ret;
   start = res.start;
   size = resource_size(&res);
   if (!size)
       return -ENOMEM;
 
   private->secure_buffer_pool = gen_pool_create(PAGE_SHIFT, -1);
   if (!private->secure_buffer_pool)
       return -ENOMEM;
 
   gen_pool_add(private->secure_buffer_pool, start, size, -1);
 
   return 0;
}
 
static void rockchip_gem_pool_destroy(struct drm_device *drm)
{
   struct rockchip_drm_private *private = drm->dev_private;
 
   if (!private->secure_buffer_pool)
       return;
 
   gen_pool_destroy(private->secure_buffer_pool);
}
 
static int rockchip_drm_bind(struct device *dev)
{
   struct drm_device *drm_dev;
   struct rockchip_drm_private *private;
   int ret;
 
   drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
   if (IS_ERR(drm_dev))
       return PTR_ERR(drm_dev);
 
   dev_set_drvdata(dev, drm_dev);
 
   private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
   if (!private) {
       ret = -ENOMEM;
       goto err_free;
   }
 
   mutex_init(&private->ovl_lock);
 
   drm_dev->dev_private = private;
 
   INIT_LIST_HEAD(&private->psr_list);
   mutex_init(&private->psr_list_lock);
   mutex_init(&private->commit_lock);
 
   private->hdmi_pll.pll = devm_clk_get_optional(dev, "hdmi-tmds-pll");
   if (PTR_ERR(private->hdmi_pll.pll) == -EPROBE_DEFER) {
       ret = -EPROBE_DEFER;
       goto err_free;
   } else if (IS_ERR(private->hdmi_pll.pll)) {
       dev_err(dev, "failed to get hdmi-tmds-pll\n");
       ret = PTR_ERR(private->hdmi_pll.pll);
       goto err_free;
   }
   private->default_pll.pll = devm_clk_get_optional(dev, "default-vop-pll");
   if (PTR_ERR(private->default_pll.pll) == -EPROBE_DEFER) {
       ret = -EPROBE_DEFER;
       goto err_free;
   } else if (IS_ERR(private->default_pll.pll)) {
       dev_err(dev, "failed to get default vop pll\n");
       ret = PTR_ERR(private->default_pll.pll);
       goto err_free;
   }
 
   ret = drmm_mode_config_init(drm_dev);
   if (ret)
       goto err_free;
 
   rockchip_drm_mode_config_init(drm_dev);
   rockchip_drm_create_properties(drm_dev);
   /* Try to bind all sub drivers. */
   ret = component_bind_all(dev, drm_dev);
   if (ret)
       goto err_mode_config_cleanup;
 
   rockchip_attach_connector_property(drm_dev);
   ret = drm_vblank_init(drm_dev, drm_dev->mode_config.num_crtc);
   if (ret)
       goto err_unbind_all;
 
   drm_mode_config_reset(drm_dev);
   rockchip_drm_set_property_default(drm_dev);
 
   /*
    * enable drm irq mode.
    * - with irq_enabled = true, we can use the vblank feature.
    */
   drm_dev->irq_enabled = true;
 
   /* init kms poll for handling hpd */
   drm_kms_helper_poll_init(drm_dev);
 
   ret = rockchip_drm_init_iommu(drm_dev);
   if (ret)
       goto err_unbind_all;
 
   rockchip_gem_pool_init(drm_dev);
   ret = of_reserved_mem_device_init(drm_dev->dev);
   if (ret)
       DRM_DEBUG_KMS("No reserved memory region assign to drm\n");
 
   rockchip_drm_show_logo(drm_dev);
 
   ret = rockchip_drm_fbdev_init(drm_dev);
   if (ret)
       goto err_iommu_cleanup;
 
   drm_dev->mode_config.allow_fb_modifiers = true;
 
   ret = drm_dev_register(drm_dev, 0);
   if (ret)
       goto err_kms_helper_poll_fini;
 
   rockchip_clk_unprotect();
 
   return 0;
err_kms_helper_poll_fini:
   rockchip_gem_pool_destroy(drm_dev);
   drm_kms_helper_poll_fini(drm_dev);
   rockchip_drm_fbdev_fini(drm_dev);
err_iommu_cleanup:
   rockchip_iommu_cleanup(drm_dev);
err_unbind_all:
   component_unbind_all(dev, drm_dev);
err_mode_config_cleanup:
   drm_mode_config_cleanup(drm_dev);
err_free:
   drm_dev->dev_private = NULL;
   dev_set_drvdata(dev, NULL);
   drm_dev_put(drm_dev);
   return ret;
}
 
static void rockchip_drm_unbind(struct device *dev)
{
   struct drm_device *drm_dev = dev_get_drvdata(dev);
 
   drm_dev_unregister(drm_dev);
 
   rockchip_drm_fbdev_fini(drm_dev);
   rockchip_gem_pool_destroy(drm_dev);
   drm_kms_helper_poll_fini(drm_dev);
 
   drm_atomic_helper_shutdown(drm_dev);
   component_unbind_all(dev, drm_dev);
   drm_mode_config_cleanup(drm_dev);
   rockchip_iommu_cleanup(drm_dev);
 
   drm_dev->dev_private = NULL;
   dev_set_drvdata(dev, NULL);
   drm_dev_put(drm_dev);
}
 
static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
                           struct drm_file *file_priv)
{
   struct rockchip_drm_private *priv = crtc->dev->dev_private;
   int pipe = drm_crtc_index(crtc);
 
   if (pipe < ROCKCHIP_MAX_CRTC &&
       priv->crtc_funcs[pipe] &&
       priv->crtc_funcs[pipe]->cancel_pending_vblank)
       priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
}
 
static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
{
   struct drm_crtc *crtc;
 
   drm_for_each_crtc(crtc, dev)
       crtc->primary->fb = NULL;
 
   return 0;
}
 
static void rockchip_drm_postclose(struct drm_device *dev,
                  struct drm_file *file_priv)
{
   struct drm_crtc *crtc;
 
   list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
       rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
}
 
static void rockchip_drm_lastclose(struct drm_device *dev)
{
   struct rockchip_drm_private *priv = dev->dev_private;
 
   if (!priv->logo)
       drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev_helper);
}
 
static struct drm_pending_vblank_event *
rockchip_drm_add_vcnt_event(struct drm_crtc *crtc, union drm_wait_vblank *vblwait,
               struct drm_file *file_priv)
{
   struct drm_pending_vblank_event *e;
   struct drm_device *dev = crtc->dev;
   unsigned long flags;
 
   e = kzalloc(sizeof(*e), GFP_KERNEL);
   if (!e)
       return NULL;
 
   e->pipe = drm_crtc_index(crtc);
   e->event.base.type = DRM_EVENT_ROCKCHIP_CRTC_VCNT;
   e->event.base.length = sizeof(e->event.vbl);
   e->event.vbl.crtc_id = crtc->base.id;
   e->event.vbl.user_data = vblwait->request.signal;
 
   spin_lock_irqsave(&dev->event_lock, flags);
   drm_event_reserve_init_locked(dev, file_priv, &e->base, &e->event.base);
   spin_unlock_irqrestore(&dev->event_lock, flags);
 
   return e;
}
 
static int rockchip_drm_get_vcnt_event_ioctl(struct drm_device *dev, void *data,
                        struct drm_file *file_priv)
{
   struct rockchip_drm_private *priv = dev->dev_private;
   union drm_wait_vblank *vblwait = data;
   struct drm_pending_vblank_event *e;
   struct drm_crtc *crtc;
   unsigned int flags, pipe;
 
   flags = vblwait->request.type & (_DRM_VBLANK_FLAGS_MASK | _DRM_ROCKCHIP_VCNT_EVENT);
   pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
   if (pipe)
       pipe = pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
   else
       pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
 
   crtc = drm_crtc_from_index(dev, pipe);
 
   if (flags & _DRM_ROCKCHIP_VCNT_EVENT) {
       e = rockchip_drm_add_vcnt_event(crtc, vblwait, file_priv);
       priv->vcnt[pipe].event = e;
   }
 
   return 0;
}
 
static const struct drm_ioctl_desc rockchip_ioctls[] = {
   DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
             DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
   DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
             rockchip_gem_map_offset_ioctl,
             DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
   DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_GET_PHYS, rockchip_gem_get_phys_ioctl,
             DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
   DRM_IOCTL_DEF_DRV(ROCKCHIP_GET_VCNT_EVENT, rockchip_drm_get_vcnt_event_ioctl,
             DRM_UNLOCKED),
};
 
static const struct file_operations rockchip_drm_driver_fops = {
   .owner = THIS_MODULE,
   .open = drm_open,
   .mmap = rockchip_gem_mmap,
   .poll = drm_poll,
   .read = drm_read,
   .unlocked_ioctl = drm_ioctl,
   .compat_ioctl = drm_compat_ioctl,
   .release = drm_release,
};
 
static int rockchip_drm_gem_dmabuf_begin_cpu_access(struct dma_buf *dma_buf,
                           enum dma_data_direction dir)
{
   struct drm_gem_object *obj = dma_buf->priv;
 
   return rockchip_gem_prime_begin_cpu_access(obj, dir);
}
 
static int rockchip_drm_gem_dmabuf_end_cpu_access(struct dma_buf *dma_buf,
                         enum dma_data_direction dir)
{
   struct drm_gem_object *obj = dma_buf->priv;
 
   return rockchip_gem_prime_end_cpu_access(obj, dir);
}
 
static const struct dma_buf_ops rockchip_drm_gem_prime_dmabuf_ops = {
   .cache_sgt_mapping = true,
   .attach = drm_gem_map_attach,
   .detach = drm_gem_map_detach,
   .map_dma_buf = drm_gem_map_dma_buf,
   .unmap_dma_buf = drm_gem_unmap_dma_buf,
   .release = drm_gem_dmabuf_release,
   .mmap = drm_gem_dmabuf_mmap,
   .vmap = drm_gem_dmabuf_vmap,
   .vunmap = drm_gem_dmabuf_vunmap,
   .get_uuid = drm_gem_dmabuf_get_uuid,
   .begin_cpu_access = rockchip_drm_gem_dmabuf_begin_cpu_access,
   .end_cpu_access = rockchip_drm_gem_dmabuf_end_cpu_access,
};
 
static struct drm_gem_object *rockchip_drm_gem_prime_import_dev(struct drm_device *dev,
                               struct dma_buf *dma_buf,
                               struct device *attach_dev)
{
   struct dma_buf_attachment *attach;
   struct sg_table *sgt;
   struct drm_gem_object *obj;
   int ret;
 
   if (dma_buf->ops == &rockchip_drm_gem_prime_dmabuf_ops) {
       obj = dma_buf->priv;
       if (obj->dev == dev) {
           /*
            * Importing dmabuf exported from out own gem increases
            * refcount on gem itself instead of f_count of dmabuf.
            */
           drm_gem_object_get(obj);
           return obj;
       }
   }
 
   if (!dev->driver->gem_prime_import_sg_table)
       return ERR_PTR(-EINVAL);
 
   attach = dma_buf_attach(dma_buf, attach_dev);
   if (IS_ERR(attach))
       return ERR_CAST(attach);
 
   get_dma_buf(dma_buf);
 
   sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
   if (IS_ERR(sgt)) {
       ret = PTR_ERR(sgt);
       goto fail_detach;
   }
 
   obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
   if (IS_ERR(obj)) {
       ret = PTR_ERR(obj);
       goto fail_unmap;
   }
 
   obj->import_attach = attach;
   obj->resv = dma_buf->resv;
 
   return obj;
 
fail_unmap:
   dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
fail_detach:
   dma_buf_detach(dma_buf, attach);
   dma_buf_put(dma_buf);
 
   return ERR_PTR(ret);
}
 
static struct drm_gem_object *rockchip_drm_gem_prime_import(struct drm_device *dev,
                               struct dma_buf *dma_buf)
{
   return rockchip_drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
}
 
static struct dma_buf *rockchip_drm_gem_prime_export(struct drm_gem_object *obj,
                            int flags)
{
   struct drm_device *dev = obj->dev;
   struct dma_buf_export_info exp_info = {
       .exp_name = KBUILD_MODNAME, /* white lie for debug */
       .owner = dev->driver->fops->owner,
       .ops = &rockchip_drm_gem_prime_dmabuf_ops,
       .size = obj->size,
       .flags = flags,
       .priv = obj,
       .resv = obj->resv,
   };
 
   return drm_gem_dmabuf_export(dev, &exp_info);
}
 
static struct drm_driver rockchip_drm_driver = {
   .driver_features    = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC | DRIVER_RENDER,
   .postclose        = rockchip_drm_postclose,
   .lastclose        = rockchip_drm_lastclose,
   .open            = rockchip_drm_open,
   .gem_vm_ops        = &drm_gem_cma_vm_ops,
   .gem_free_object_unlocked = rockchip_gem_free_object,
   .dumb_create        = rockchip_gem_dumb_create,
   .prime_handle_to_fd    = drm_gem_prime_handle_to_fd,
   .prime_fd_to_handle    = drm_gem_prime_fd_to_handle,
   .gem_prime_import    = rockchip_drm_gem_prime_import,
   .gem_prime_export    = rockchip_drm_gem_prime_export,
   .gem_prime_get_sg_table    = rockchip_gem_prime_get_sg_table,
   .gem_prime_import_sg_table    = rockchip_gem_prime_import_sg_table,
   .gem_prime_vmap        = rockchip_gem_prime_vmap,
   .gem_prime_vunmap    = rockchip_gem_prime_vunmap,
   .gem_prime_mmap        = rockchip_gem_mmap_buf,
#ifdef CONFIG_DEBUG_FS
   .debugfs_init        = rockchip_drm_debugfs_init,
#endif
   .ioctls            = rockchip_ioctls,
   .num_ioctls        = ARRAY_SIZE(rockchip_ioctls),
   .fops            = &rockchip_drm_driver_fops,
   .name    = DRIVER_NAME,
   .desc    = DRIVER_DESC,
   .date    = DRIVER_DATE,
   .major    = DRIVER_MAJOR,
   .minor    = DRIVER_MINOR,
};
 
#ifdef CONFIG_PM_SLEEP
static int rockchip_drm_sys_suspend(struct device *dev)
{
   struct drm_device *drm = dev_get_drvdata(dev);
 
   return drm_mode_config_helper_suspend(drm);
}
 
static int rockchip_drm_sys_resume(struct device *dev)
{
   struct drm_device *drm = dev_get_drvdata(dev);
 
   return drm_mode_config_helper_resume(drm);
}
#endif
 
static const struct dev_pm_ops rockchip_drm_pm_ops = {
   SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
               rockchip_drm_sys_resume)
};
 
#define MAX_ROCKCHIP_SUB_DRIVERS 16
static struct platform_driver *rockchip_sub_drivers[MAX_ROCKCHIP_SUB_DRIVERS];
static int num_rockchip_sub_drivers;
 
/*
 * Check if a vop endpoint is leading to a rockchip subdriver or bridge.
 * Should be called from the component bind stage of the drivers
 * to ensure that all subdrivers are probed.
 *
 * @ep: endpoint of a rockchip vop
 *
 * returns true if subdriver, false if external bridge and -ENODEV
 * if remote port does not contain a device.
 */
int rockchip_drm_endpoint_is_subdriver(struct device_node *ep)
{
   struct device_node *node = of_graph_get_remote_port_parent(ep);
   struct platform_device *pdev;
   struct device_driver *drv;
   int i;
 
   if (!node)
       return -ENODEV;
 
   /* status disabled will prevent creation of platform-devices */
   pdev = of_find_device_by_node(node);
   of_node_put(node);
   if (!pdev)
       return -ENODEV;
 
   /*
    * All rockchip subdrivers have probed at this point, so
    * any device not having a driver now is an external bridge.
    */
   drv = pdev->dev.driver;
   if (!drv) {
       platform_device_put(pdev);
       return false;
   }
 
   for (i = 0; i < num_rockchip_sub_drivers; i++) {
       if (rockchip_sub_drivers[i] == to_platform_driver(drv)) {
           platform_device_put(pdev);
           return true;
       }
   }
 
   platform_device_put(pdev);
   return false;
}
 
static int compare_dev(struct device *dev, void *data)
{
   return dev == (struct device *)data;
}
 
static void rockchip_drm_match_remove(struct device *dev)
{
   struct device_link *link;
 
   list_for_each_entry(link, &dev->links.consumers, s_node)
       device_link_del(link);
}
 
static struct component_match *rockchip_drm_match_add(struct device *dev)
{
   struct component_match *match = NULL;
   int i;
 
   for (i = 0; i < num_rockchip_sub_drivers; i++) {
       struct platform_driver *drv = rockchip_sub_drivers[i];
       struct device *p = NULL, *d;
 
       do {
           d = platform_find_device_by_driver(p, &drv->driver);
           put_device(p);
           p = d;
 
           if (!d)
               break;
 
           device_link_add(dev, d, DL_FLAG_STATELESS);
           component_match_add(dev, &match, compare_dev, d);
       } while (true);
   }
 
   if (IS_ERR(match))
       rockchip_drm_match_remove(dev);
 
   return match ?: ERR_PTR(-ENODEV);
}
 
static const struct component_master_ops rockchip_drm_ops = {
   .bind = rockchip_drm_bind,
   .unbind = rockchip_drm_unbind,
};
 
static int rockchip_drm_platform_of_probe(struct device *dev)
{
   struct device_node *np = dev->of_node;
   struct device_node *port;
   bool found = false;
   int i;
 
   if (!np)
       return -ENODEV;
 
   for (i = 0;; i++) {
       struct device_node *iommu;
 
       port = of_parse_phandle(np, "ports", i);
       if (!port)
           break;
 
       if (!of_device_is_available(port->parent)) {
           of_node_put(port);
           continue;
       }
 
       iommu = of_parse_phandle(port->parent, "iommus", 0);
       if (!iommu || !of_device_is_available(iommu)) {
           DRM_DEV_DEBUG(dev,
                     "no iommu attached for %pOF, using non-iommu buffers\n",
                     port->parent);
           /*
            * if there is a crtc not support iommu, force set all
            * crtc use non-iommu buffer.
            */
           is_support_iommu = false;
       }
 
       found = true;
 
       iommu_reserve_map |= of_property_read_bool(iommu, "rockchip,reserve-map");
       of_node_put(iommu);
       of_node_put(port);
   }
 
   if (i == 0) {
       DRM_DEV_ERROR(dev, "missing 'ports' property\n");
       return -ENODEV;
   }
 
   if (!found) {
       DRM_DEV_ERROR(dev,
                 "No available vop found for display-subsystem.\n");
       return -ENODEV;
   }
 
   return 0;
}
 
static int rockchip_drm_platform_probe(struct platform_device *pdev)
{
   struct device *dev = &pdev->dev;
   struct component_match *match = NULL;
   int ret;
 
   ret = rockchip_drm_platform_of_probe(dev);
#if !IS_ENABLED(CONFIG_DRM_ROCKCHIP_VVOP)
   if (ret)
       return ret;
#endif
 
   match = rockchip_drm_match_add(dev);
   if (IS_ERR(match))
       return PTR_ERR(match);
 
   ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
   if (ret)
       goto err;
 
   ret = component_master_add_with_match(dev, &rockchip_drm_ops, match);
   if (ret < 0)
       goto err;
 
   return 0;
err:
   rockchip_drm_match_remove(dev);
 
   return ret;
}
 
static int rockchip_drm_platform_remove(struct platform_device *pdev)
{
   component_master_del(&pdev->dev, &rockchip_drm_ops);
 
   rockchip_drm_match_remove(&pdev->dev);
 
   return 0;
}
 
static void rockchip_drm_platform_shutdown(struct platform_device *pdev)
{
   struct drm_device *drm = platform_get_drvdata(pdev);
 
   if (drm)
       drm_atomic_helper_shutdown(drm);
}
 
static const struct of_device_id rockchip_drm_dt_ids[] = {
   { .compatible = "rockchip,display-subsystem", },
   { /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
 
static struct platform_driver rockchip_drm_platform_driver = {
   .probe = rockchip_drm_platform_probe,
   .remove = rockchip_drm_platform_remove,
   .shutdown = rockchip_drm_platform_shutdown,
   .driver = {
       .name = "rockchip-drm",
       .of_match_table = rockchip_drm_dt_ids,
       .pm = &rockchip_drm_pm_ops,
   },
};
 
#define ADD_ROCKCHIP_SUB_DRIVER(drv, cond) { \
   if (IS_ENABLED(cond) && \
       !WARN_ON(num_rockchip_sub_drivers >= MAX_ROCKCHIP_SUB_DRIVERS)) \
       rockchip_sub_drivers[num_rockchip_sub_drivers++] = &drv; \
}
 
static int __init rockchip_drm_init(void)
{
   int ret;
 
   num_rockchip_sub_drivers = 0;
#if IS_ENABLED(CONFIG_DRM_ROCKCHIP_VVOP)
   ADD_ROCKCHIP_SUB_DRIVER(vvop_platform_driver, CONFIG_DRM_ROCKCHIP_VVOP);
#else
   ADD_ROCKCHIP_SUB_DRIVER(vop_platform_driver, CONFIG_ROCKCHIP_VOP);
   ADD_ROCKCHIP_SUB_DRIVER(vop2_platform_driver, CONFIG_ROCKCHIP_VOP2);
   ADD_ROCKCHIP_SUB_DRIVER(vconn_platform_driver, CONFIG_ROCKCHIP_VCONN);
   ADD_ROCKCHIP_SUB_DRIVER(rockchip_lvds_driver,
               CONFIG_ROCKCHIP_LVDS);
   ADD_ROCKCHIP_SUB_DRIVER(rockchip_dp_driver,
               CONFIG_ROCKCHIP_ANALOGIX_DP);
   ADD_ROCKCHIP_SUB_DRIVER(cdn_dp_driver, CONFIG_ROCKCHIP_CDN_DP);
   ADD_ROCKCHIP_SUB_DRIVER(dw_hdmi_rockchip_pltfm_driver,
               CONFIG_ROCKCHIP_DW_HDMI);
   ADD_ROCKCHIP_SUB_DRIVER(dw_mipi_dsi_rockchip_driver,
               CONFIG_ROCKCHIP_DW_MIPI_DSI);
   ADD_ROCKCHIP_SUB_DRIVER(dw_mipi_dsi2_rockchip_driver,
               CONFIG_ROCKCHIP_DW_MIPI_DSI);
   ADD_ROCKCHIP_SUB_DRIVER(inno_hdmi_driver, CONFIG_ROCKCHIP_INNO_HDMI);
   ADD_ROCKCHIP_SUB_DRIVER(rk3066_hdmi_driver,
               CONFIG_ROCKCHIP_RK3066_HDMI);
   ADD_ROCKCHIP_SUB_DRIVER(rockchip_rgb_driver, CONFIG_ROCKCHIP_RGB);
   ADD_ROCKCHIP_SUB_DRIVER(rockchip_tve_driver, CONFIG_ROCKCHIP_DRM_TVE);
   ADD_ROCKCHIP_SUB_DRIVER(dw_dp_driver, CONFIG_ROCKCHIP_DW_DP);
 
#endif
   ret = platform_register_drivers(rockchip_sub_drivers,
                   num_rockchip_sub_drivers);
   if (ret)
       return ret;
 
   ret = platform_driver_register(&rockchip_drm_platform_driver);
   if (ret)
       goto err_unreg_drivers;
 
   rockchip_gem_get_ddr_info();
 
   return 0;
 
err_unreg_drivers:
   platform_unregister_drivers(rockchip_sub_drivers,
                   num_rockchip_sub_drivers);
   return ret;
}
 
static void __exit rockchip_drm_fini(void)
{
   platform_driver_unregister(&rockchip_drm_platform_driver);
 
   platform_unregister_drivers(rockchip_sub_drivers,
                   num_rockchip_sub_drivers);
}
 
#ifdef CONFIG_VIDEO_REVERSE_IMAGE
fs_initcall(rockchip_drm_init);
#else
module_init(rockchip_drm_init);
#endif
module_exit(rockchip_drm_fini);
 
MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
MODULE_LICENSE("GPL v2");