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
|
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <asm/io.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/fb.h>
/* #include <linux/earlysuspend.h> */
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <mach/sync_write.h>
#include <mach/mt_clkmgr.h>
#include <mach/irqs.h>
#include <asm/cacheflush.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/dma-direction.h>
#include <asm/page.h>
#include <linux/proc_fs.h>
#include "m4u_priv.h"
#include "m4u.h"
#include "m4u_hw.h"
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#ifdef M4U_TEE_SERVICE_ENABLE
#include "mobicore_driver_api.h"
#include "tz_m4u.h"
#ifdef __M4U_SECURE_SYSTRACE_ENABLE__
#include <linux/sectrace.h>
#endif
int m4u_tee_en = 0;
#endif
#if IS_ENABLED(CONFIG_COMPAT)
#include <linux/uaccess.h>
#include <linux/compat.h>
#endif
static m4u_buf_info_t gMvaNode_unknown = {
.va = 0,
.mva = 0,
.size = 0,
.port = M4U_PORT_UNKNOWN,
};
/* -------------------------------------Global variables------------------------------------------------// */
#ifdef M4U_PROFILE
MMP_Event M4U_MMP_Events[M4U_MMP_MAX];
#endif
#define M4U_DEV_NAME "m4u"
struct m4u_device *gM4uDev;
static int m4u_buf_show(void *priv, unsigned int mva_start, unsigned int mva_end, void *data)
{
m4u_buf_info_t *pMvaInfo = priv;
M4U_PRINT_LOG_OR_SEQ(data, "0x%-8x, 0x%-8x, 0x%lx, 0x%-8x, 0x%x, %s, 0x%x, 0x%x, 0x%x\n",
pMvaInfo->mva, pMvaInfo->mva+pMvaInfo->size-1, pMvaInfo->va,
pMvaInfo->size, pMvaInfo->prot, m4u_get_port_name(pMvaInfo->port),
pMvaInfo->flags, mva_start, mva_end);
return 0;
}
int m4u_dump_buf_info(struct seq_file *seq)
{
M4U_PRINT_LOG_OR_SEQ(seq, "\ndump mva allocated info ========>\n");
M4U_PRINT_LOG_OR_SEQ(seq,
"mva_start mva_end va size prot module flags debug1 debug2\n");
mva_foreach_priv((void *) m4u_buf_show, seq);
M4U_PRINT_LOG_OR_SEQ(seq, " dump mva allocated info done ========>\n");
return 0;
}
#ifdef M4U_PROFILE
static void m4u_profile_init(void)
{
MMP_Event M4U_Event;
MMProfileEnable(1);
M4U_Event = MMProfileRegisterEvent(MMP_RootEvent, "M4U");
/* register events */
M4U_MMP_Events[M4U_MMP_ALLOC_MVA] = MMProfileRegisterEvent(M4U_Event, "Alloc MVA");
M4U_MMP_Events[M4U_MMP_DEALLOC_MVA] = MMProfileRegisterEvent(M4U_Event, "DeAlloc MVA");
M4U_MMP_Events[M4U_MMP_CONFIG_PORT] = MMProfileRegisterEvent(M4U_Event, "Config Port");
M4U_MMP_Events[M4U_MMP_M4U_ERROR] = MMProfileRegisterEvent(M4U_Event, "M4U ERROR");
M4U_MMP_Events[M4U_MMP_CACHE_SYNC] = MMProfileRegisterEvent(M4U_Event, "M4U_CACHE_SYNC");
M4U_MMP_Events[M4U_MMP_TOGGLE_CG] = MMProfileRegisterEvent(M4U_Event, "M4U_Toggle_CG");
/* enable events by default */
MMProfileEnableEvent(M4U_MMP_Events[M4U_MMP_ALLOC_MVA], 1);
MMProfileEnableEvent(M4U_MMP_Events[M4U_MMP_DEALLOC_MVA], 1);
MMProfileEnableEvent(M4U_MMP_Events[M4U_MMP_CONFIG_PORT], 1);
MMProfileEnableEvent(M4U_MMP_Events[M4U_MMP_M4U_ERROR], 1);
MMProfileEnableEvent(M4U_MMP_Events[M4U_MMP_CACHE_SYNC], 1);
/* MMProfileEnableEvent(M4U_MMP_Events[M4U_MMP_TOGGLE_CG], 0); */
MMProfileStart(1);
}
#endif
/* get ref count on all pages in sgtable */
int m4u_get_sgtable_pages(struct sg_table *table)
{
return 0;
}
/* put ref count on all pages in sgtable */
int m4u_put_sgtable_pages(struct sg_table *table)
{
int i;
struct scatterlist *sg;
for_each_sg(table->sgl, sg, table->nents, i) {
struct page *page = sg_page(sg);
if (page) {
if (!PageReserved(page))
SetPageDirty(page);
page_cache_release(page);
}
}
return 0;
}
static m4u_buf_info_t *m4u_alloc_buf_info(void)
{
m4u_buf_info_t *pList = NULL;
pList = kzalloc(sizeof(m4u_buf_info_t), GFP_KERNEL);
if (pList == NULL) {
M4UMSG("m4u_client_add_buf(), pList=0x%p\n", pList);
return NULL;
}
INIT_LIST_HEAD(&(pList->link));
return pList;
}
static int m4u_free_buf_info(m4u_buf_info_t *pList)
{
kfree(pList);
return 0;
}
static int m4u_client_add_buf(m4u_client_t *client, m4u_buf_info_t *pList)
{
mutex_lock(&(client->dataMutex));
list_add(&(pList->link), &(client->mvaList));
mutex_unlock(&(client->dataMutex));
return 0;
}
/*
static int m4u_client_del_buf(m4u_client_t *client, m4u_buf_info_t *pList)
{
mutex_lock(&(client->dataMutex));
list_del(&(pList->link));
mutex_unlock(&(client->dataMutex));
return 0;
}
*/
/***********************************************************/
/** find or delete a buffer from client list
* @param client -- client to be searched
* @param mva -- mva to be searched
* @param del -- should we del this buffer from client?
*
* @return buffer_info if found, NULL on fail
* @remark
* @see
* @to-do we need to add multi domain support here.
* @author K Zhang @date 2013/11/14
************************************************************/
static m4u_buf_info_t *m4u_client_find_buf(m4u_client_t *client, unsigned int mva, int del)
{
struct list_head *pListHead;
m4u_buf_info_t *pList = NULL;
m4u_buf_info_t *ret = NULL;
if (client == NULL) {
M4UERR("m4u_delete_from_garbage_list(), client is NULL!\n");
m4u_dump_buf_info(NULL);
return NULL;
}
mutex_lock(&(client->dataMutex));
list_for_each(pListHead, &(client->mvaList)) {
pList = container_of(pListHead, m4u_buf_info_t, link);
if (pList->mva == mva)
break;
}
if (pListHead == &(client->mvaList)) {
ret = NULL;
} else {
if (del)
list_del(pListHead);
ret = pList;
}
mutex_unlock(&(client->dataMutex));
return ret;
}
/*
//dump buf info in client
static void m4u_client_dump_buf(m4u_client_t *client, const char *pMsg)
{
m4u_buf_info_t *pList;
struct list_head *pListHead;
M4UMSG("print mva list [%s] ================================>\n", pMsg);
mutex_lock(&(client->dataMutex));
list_for_each(pListHead, &(client->mvaList))
{
pList = container_of(pListHead, m4u_buf_info_t, link);
M4UMSG("port=%s, va=0x%x, size=0x%x, mva=0x%x, prot=%d\n",
m4u_get_port_name(pList->port), pList->va, pList->size, pList->mva, pList->prot);
}
mutex_unlock(&(client->dataMutex));
M4UMSG("print mva list done ==========================>\n");
}
*/
m4u_client_t *m4u_create_client(void)
{
m4u_client_t *client;
client = kmalloc(sizeof(m4u_client_t), GFP_ATOMIC);
if (!client)
return NULL;
mutex_init(&(client->dataMutex));
mutex_lock(&(client->dataMutex));
client->open_pid = current->pid;
client->open_tgid = current->tgid;
INIT_LIST_HEAD(&(client->mvaList));
mutex_unlock(&(client->dataMutex));
return client;
}
int m4u_destroy_client(m4u_client_t *client)
{
m4u_buf_info_t *pMvaInfo;
unsigned int mva, size;
M4U_PORT_ID port;
while (1) {
mutex_lock(&(client->dataMutex));
if (list_empty(&client->mvaList)) {
mutex_unlock(&(client->dataMutex));
break;
}
pMvaInfo = container_of(client->mvaList.next, m4u_buf_info_t, link);
M4UMSG("warnning: clean garbage at m4u close: module=%s,va=0x%lx,mva=0x%x,size=%d\n",
m4u_get_port_name(pMvaInfo->port), pMvaInfo->va, pMvaInfo->mva,
pMvaInfo->size);
port = pMvaInfo->port;
mva = pMvaInfo->mva;
size = pMvaInfo->size;
mutex_unlock(&(client->dataMutex));
m4u_reclaim_notify(port, mva, size);
/* m4u_dealloc_mva will lock client->dataMutex again */
m4u_dealloc_mva(client, port, mva);
}
kfree(client);
return 0;
}
static int m4u_dump_mmaps(unsigned long addr)
{
struct vm_area_struct *vma;
M4ULOG_MID("addr=0x%lx, name=%s, pid=0x%x,", addr, current->comm, current->pid);
vma = find_vma(current->mm, addr);
if (vma && (addr >= vma->vm_start)) {
M4ULOG_MID("find vma: 0x%16lx-0x%16lx, flags=0x%lx\n",
(vma->vm_start), (vma->vm_end), vma->vm_flags);
return 0;
}
M4UMSG("cannot find vma for addr 0x%lx\n", addr);
return -1;
}
/* to-do: need modification to support 4G DRAM */
static phys_addr_t m4u_user_v2p(unsigned long va)
{
unsigned long pageOffset = (va & (PAGE_SIZE - 1));
pgd_t *pgd;
pud_t *pud;
pmd_t *pmd;
pte_t *pte;
phys_addr_t pa;
if (NULL == current) {
M4UMSG("warning: m4u_user_v2p, current is NULL!\n");
return 0;
}
if (NULL == current->mm) {
M4UMSG("warning: m4u_user_v2p, current->mm is NULL! tgid=0x%x, name=%s\n",
current->tgid, current->comm);
return 0;
}
pgd = pgd_offset(current->mm, va); /* what is tsk->mm */
if (pgd_none(*pgd) || pgd_bad(*pgd)) {
M4UMSG("m4u_user_v2p(), va=0x%lx, pgd invalid!\n", va);
return 0;
}
pud = pud_offset(pgd, va);
if (pud_none(*pud) || pud_bad(*pud)) {
M4UMSG("m4u_user_v2p(), va=0x%lx, pud invalid!\n", va);
return 0;
}
pmd = pmd_offset(pud, va);
if (pmd_none(*pmd) || pmd_bad(*pmd)) {
M4UMSG("m4u_user_v2p(), va=0x%lx, pmd invalid!\n", va);
return 0;
}
pte = pte_offset_map(pmd, va);
if (pte_present(*pte)) {
/* pa=(pte_val(*pte) & (PAGE_MASK)) | pageOffset; */
pa = (pte_val(*pte) & (PHYS_MASK) & (~((phys_addr_t) 0xfff))) | pageOffset;
pte_unmap(pte);
return pa;
}
pte_unmap(pte);
M4UMSG("m4u_user_v2p(), va=0x%lx, pte invalid!\n", va);
return 0;
}
static int m4u_fill_sgtable_user(struct vm_area_struct *vma, unsigned long va, int page_num,
struct scatterlist **pSg, int has_page)
{
unsigned long va_align;
phys_addr_t pa = 0;
int i, ret;
struct scatterlist *sg = *pSg;
struct page *pages;
va_align = round_down(va, PAGE_SIZE);
for (i = 0; i < page_num; i++) {
int fault_cnt;
unsigned long va_tmp = va_align+i*PAGE_SIZE;
pa = 0;
for (fault_cnt = 0; fault_cnt < 3000; fault_cnt++) {
if (has_page) {
ret = get_user_pages(current, current->mm, va_tmp, 1,
(vma->vm_flags & VM_WRITE), 0, &pages, NULL);
if (ret == 1)
pa = (page_to_pfn(pages) << PAGE_SHIFT) | (va_tmp & ~PAGE_MASK);
} else {
pa = m4u_user_v2p(va_tmp);
if (!pa) {
handle_mm_fault(current->mm, vma, va_tmp,
(vma->vm_flags&VM_WRITE) ? FAULT_FLAG_WRITE : 0);
}
}
if (pa) {
/* Add one line comment for avoid kernel coding style, WARNING:BRACES: */
break;
}
cond_resched();
}
if (!pa || !sg) {
M4UMSG("%s: fail va=0x%lx,page_num=0x%x,fail_va=0x%lx,pa=0x%lx,sg=0x%p,i=%d\n",
__func__, va, page_num, va_tmp, (unsigned long)pa, sg, i);
show_pte(current->mm, va_tmp);
m4u_dump_mmaps(va);
m4u_dump_mmaps(va_tmp);
return -1;
}
if (fault_cnt > 2) {
M4UINFO("warning: handle_mm_fault for %d times\n", fault_cnt);
show_pte(current->mm, va_tmp);
m4u_dump_mmaps(va_tmp);
}
/* debug check... */
if ((pa & (PAGE_SIZE - 1)) != 0) {
M4ULOG_MID("pa error, pa: 0x%lx, va: 0x%lx, align: 0x%lx\n",
(unsigned long)pa, va_tmp, va_align);
}
if (has_page) {
struct page *page;
page = phys_to_page(pa);
/* M4UMSG("page=0x%x, pfn=%d\n", page, __phys_to_pfn(pa)); */
sg_set_page(sg, page, PAGE_SIZE, 0);
#ifdef CONFIG_NEED_SG_DMA_LENGTH
sg->dma_length = sg->length;
#endif
} else {
sg_dma_address(sg) = pa;
sg_dma_len(sg) = PAGE_SIZE;
}
sg = sg_next(sg);
}
*pSg = sg;
return 0;
}
static int m4u_create_sgtable_user(unsigned long va_align, struct sg_table *table)
{
int ret = 0;
struct vm_area_struct *vma;
struct scatterlist *sg = table->sgl;
unsigned int left_page_num = table->nents;
unsigned long va = va_align;
down_read(¤t->mm->mmap_sem);
while (left_page_num) {
unsigned int vma_page_num;
vma = find_vma(current->mm, va);
if (vma == NULL || vma->vm_start > va) {
M4UMSG("cannot find vma: va=0x%lx, vma=0x%p\n", va, vma);
m4u_dump_mmaps(va);
ret = -1;
goto out;
} else {
/* M4ULOG_MID("%s va: 0x%lx, vma->vm_start=0x%lx, vma->vm_end=0x%lx\n",
__func__, va, vma->vm_start, vma->vm_end); */
}
vma_page_num = (vma->vm_end - va) / PAGE_SIZE;
vma_page_num = min(vma_page_num, left_page_num);
if ((vma->vm_flags) & VM_PFNMAP) {
/* ion va or ioremap vma has this flag */
/* VM_PFNMAP: Page-ranges managed without "struct page", just pure PFN */
ret = m4u_fill_sgtable_user(vma, va, vma_page_num, &sg, 0);
M4ULOG_MID("alloc_mva VM_PFNMAP va=0x%lx, page_num=0x%x\n", va,
vma_page_num);
} else {
/* Add one line comment for avoid kernel coding style, WARNING:BRACES: */
ret = m4u_fill_sgtable_user(vma, va, vma_page_num, &sg, 1);
}
if (ret) {
/* Add one line comment for avoid kernel coding style, WARNING:BRACES: */
goto out;
}
left_page_num -= vma_page_num;
va += vma_page_num * PAGE_SIZE;
}
out:
up_read(¤t->mm->mmap_sem);
return ret;
}
/* make a sgtable for virtual buffer */
struct sg_table *m4u_create_sgtable(unsigned long va, unsigned int size)
{
struct sg_table *table;
int ret, i, page_num;
unsigned long va_align;
phys_addr_t pa;
struct scatterlist *sg;
struct page *page;
page_num = M4U_GET_PAGE_NUM(va, size);
va_align = round_down(va, PAGE_SIZE);
table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
if (!table) {
M4UMSG("%s table kmalloc fail: va=0x%lx, size=0x%x, page_num=%d\n",
__func__, va, size, page_num);
return ERR_PTR(-ENOMEM);
}
ret = sg_alloc_table(table, page_num, GFP_KERNEL);
if (ret) {
kfree(table);
M4UMSG("%s alloc_sgtable fail: va=0x%lx, size=0x%x, page_num=%d\n",
__func__, va, size, page_num);
return ERR_PTR(-ENOMEM);
}
M4ULOG_LOW("%s va=0x%lx, PAGE_OFFSET=0x%lx, VMALLOC_START=0x%lx, VMALLOC_END=0x%lx\n",
__func__, va, PAGE_OFFSET, VMALLOC_START, VMALLOC_END);
if (va < PAGE_OFFSET) { /* from user space */
if (va >= VMALLOC_START && va <= VMALLOC_END) { /* vmalloc */
M4ULOG_MID(" from user space vmalloc, va = 0x%lx", va);
for_each_sg(table->sgl, sg, table->nents, i) {
page = vmalloc_to_page((void *)(va_align + i * PAGE_SIZE));
if (!page) {
M4UMSG("vmalloc_to_page fail, va=0x%lx\n",
va_align + i * PAGE_SIZE);
goto err;
}
sg_set_page(sg, page, PAGE_SIZE, 0);
}
} else {
ret = m4u_create_sgtable_user(va_align, table);
if (ret) {
M4UMSG("%s error va=0x%lx, size=%d\n", __func__, va, size);
goto err;
}
}
} else { /* from kernel space */
if (va >= VMALLOC_START && va <= VMALLOC_END) { /* vmalloc */
M4ULOG_MID(" from kernel space vmalloc, va = 0x%lx", va);
for_each_sg(table->sgl, sg, table->nents, i) {
page = vmalloc_to_page((void *)(va_align + i * PAGE_SIZE));
if (!page) {
M4UMSG("vmalloc_to_page fail, va=0x%lx\n",
va_align + i * PAGE_SIZE);
goto err;
}
sg_set_page(sg, page, PAGE_SIZE, 0);
}
} else { /* kmalloc to-do: use one entry sgtable. */
for_each_sg(table->sgl, sg, table->nents, i) {
pa = virt_to_phys((void *)(va_align + i * PAGE_SIZE));
page = phys_to_page(pa);
sg_set_page(sg, page, PAGE_SIZE, 0);
}
}
}
return table;
err:
sg_free_table(table);
kfree(table);
return ERR_PTR(-EFAULT);
}
int m4u_destroy_sgtable(struct sg_table *table)
{
if (!IS_ERR_OR_NULL(table)) {
sg_free_table(table);
kfree(table);
}
return 0;
}
/* #define __M4U_MAP_MVA_TO_KERNEL_FOR_DEBUG__ */
int m4u_alloc_mva(m4u_client_t *client, M4U_PORT_ID port,
unsigned long va, struct sg_table *sg_table,
unsigned int size, unsigned int prot, unsigned int flags, unsigned int *pMva)
{
int ret;
m4u_buf_info_t *pMvaInfo;
unsigned int mva = 0, mva_align, size_align;
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_ALLOC_MVA], MMProfileFlagStart, va, size);
if (va && sg_table) {
M4UMSG("%s, va or sg_table are both valid: va=0x%lx, sg=0x%p\n", __func__,
va, sg_table);
}
if (va) {
sg_table = m4u_create_sgtable(va, size);
if (IS_ERR_OR_NULL(sg_table)) {
M4UMSG("%s, cannot create sg: larb=%d,module=%s,va=0x%lx,sg=0x%p,size=%d,prot=0x%x,flags=0x%x\n"
, __func__, m4u_port_2_larb_id(port), m4u_get_port_name(port),
va, sg_table, size, prot, flags);
ret = -EFAULT;
goto err;
}
}
/* here we get correct sg_table for this buffer */
pMvaInfo = m4u_alloc_buf_info();
if (!pMvaInfo) {
ret = -ENOMEM;
goto err;
}
pMvaInfo->va = va;
pMvaInfo->port = port;
pMvaInfo->size = size;
pMvaInfo->prot = prot;
pMvaInfo->flags = flags;
pMvaInfo->sg_table = sg_table;
if (flags & M4U_FLAGS_FIX_MVA)
mva = m4u_do_mva_alloc_fix(*pMva, size, pMvaInfo);
else
mva = m4u_do_mva_alloc(va, size, pMvaInfo);
if (mva == 0) {
m4u_aee_print("alloc mva fail: larb=%d,module=%s,size=%d\n",
m4u_port_2_larb_id(port), m4u_get_port_name(port), size);
m4u_dump_buf_info(NULL);
ret = -EINVAL;
goto err1;
} else
M4ULOG_LOW("%s,mva = 0x%x\n", __func__, mva);
m4u_get_sgtable_pages(sg_table);
mva_align = round_down(mva, PAGE_SIZE);
size_align = PAGE_ALIGN(mva + size - mva_align);
ret = m4u_map_sgtable(m4u_get_domain_by_port(port), mva_align, sg_table,
size_align, pMvaInfo->prot);
if (ret < 0) {
M4UMSG("error to map sgtable\n");
goto err2;
}
pMvaInfo->mva = mva;
pMvaInfo->mva_align = mva_align;
pMvaInfo->size_align = size_align;
*pMva = mva;
if (flags & M4U_FLAGS_SEQ_ACCESS)
pMvaInfo->seq_id = m4u_insert_seq_range(port, mva, mva + size - 1);
m4u_client_add_buf(client, pMvaInfo);
M4ULOG_MID("%s: pMvaInfo=0x%p, larb=%d,module=%s,va=0x%lx,sg=0x%p,size=%d,prot=0x%x,flags=0x%x,mva=0x%x\n",
__func__, pMvaInfo, m4u_port_2_larb_id(port), m4u_get_port_name(port), va, sg_table,
size, prot, flags, mva);
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_ALLOC_MVA], MMProfileFlagEnd, port, mva);
#ifdef __M4U_MAP_MVA_TO_KERNEL_FOR_DEBUG__
/* map this mva to kernel va just for debug */
{
unsigned long kernel_va;
unsigned int kernel_size;
int ret;
ret = m4u_mva_map_kernel(mva, size, &kernel_va, &kernel_size);
if (ret)
M4UMSG("error to map kernel va: mva=0x%x, size=%d\n", mva, size);
else {
pMvaInfo->mapped_kernel_va_for_debug = kernel_va;
M4ULOG_MID("[kernel_va_debug] map va: mva=0x%x, kernel_va=0x%lx, size=0x%x\n",
mva, kernel_va, size);
}
}
#endif
return 0;
err2:
m4u_do_mva_free(mva, size);
err1:
m4u_free_buf_info(pMvaInfo);
err:
if (va)
m4u_destroy_sgtable(sg_table);
*pMva = 0;
M4UMSG("error: larb=%d,module=%s,va=0x%lx,size=%d,prot=0x%x,flags=0x%x, mva=0x%x\n",
m4u_port_2_larb_id(port), m4u_get_port_name(port), va, size, prot, flags, mva);
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_ALLOC_MVA], MMProfileFlagEnd, port, 0);
return ret;
}
/* interface for ion */
static m4u_client_t *ion_m4u_client;
int m4u_alloc_mva_sg(int eModuleID,
struct sg_table *sg_table,
const unsigned int BufSize,
int security, int cache_coherent, unsigned int *pRetMVABuf)
{
int prot;
if (!ion_m4u_client) {
ion_m4u_client = m4u_create_client();
if (IS_ERR_OR_NULL(ion_m4u_client)) {
ion_m4u_client = NULL;
return -1;
}
}
prot = M4U_PROT_READ | M4U_PROT_WRITE
| (cache_coherent ? (M4U_PROT_SHARE | M4U_PROT_CACHE) : 0)
| (security ? M4U_PROT_SEC : 0);
return m4u_alloc_mva(ion_m4u_client, eModuleID, 0, sg_table, BufSize, prot, 0, pRetMVABuf);
}
#ifdef M4U_TEE_SERVICE_ENABLE
static int m4u_unmap_nonsec_buffer(unsigned int mva, unsigned int size);
int m4u_register_mva_share(int eModuleID, unsigned int mva)
{
m4u_buf_info_t *pMvaInfo;
pMvaInfo = mva_get_priv(mva);
if (!pMvaInfo) {
M4UMSG("%s cannot find mva: module=%s, mva=0x%x\n", __func__,
m4u_get_port_name(eModuleID), mva);
return -1;
}
pMvaInfo->flags |= M4U_FLAGS_SEC_SHAREABLE;
return 0;
}
#endif
int m4u_dealloc_mva_sg(int eModuleID, struct sg_table *sg_table,
const unsigned int BufSize, const unsigned int MVA)
{
if (!ion_m4u_client) {
m4u_aee_print("ion_m4u_client==NULL !! oops oops~~~~\n");
return -1;
}
return m4u_dealloc_mva(ion_m4u_client, eModuleID, MVA);
}
/* should not hold client->dataMutex here. */
int m4u_dealloc_mva(m4u_client_t *client, M4U_PORT_ID port, unsigned int mva)
{
m4u_buf_info_t *pMvaInfo;
int ret, is_err = 0;
unsigned int size;
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_DEALLOC_MVA], MMProfileFlagStart, port, mva);
pMvaInfo = m4u_client_find_buf(client, mva, 1);
if (unlikely(!pMvaInfo)) {
M4UMSG("error: m4u_dealloc_mva no mva found in client! module=%s, mva=0x%x\n",
m4u_get_port_name(port), mva);
m4u_dump_buf_info(NULL);
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_DEALLOC_MVA], MMProfileFlagStart, 0x5a5a5a5a, mva);
return -EINVAL;
}
pMvaInfo->flags |= M4U_FLAGS_MVA_IN_FREE;
M4ULOG_MID("m4u_dealloc_mva: larb=%d,module=%s,mva=0x%x, size=%d\n",
m4u_port_2_larb_id(port), m4u_get_port_name(port), mva, pMvaInfo->size);
#ifdef M4U_TEE_SERVICE_ENABLE
if (pMvaInfo->flags & M4U_FLAGS_SEC_SHAREABLE)
m4u_unmap_nonsec_buffer(mva, pMvaInfo->size);
#endif
ret = m4u_unmap(m4u_get_domain_by_port(port), pMvaInfo->mva_align, pMvaInfo->size_align);
if (ret) {
is_err = 1;
M4UMSG("m4u_unmap fail\n");
}
if (0 != pMvaInfo->va) {
/* non ion buffer*/
if (pMvaInfo->va < PAGE_OFFSET) { /* from user space */
if (!(pMvaInfo->va >= VMALLOC_START && pMvaInfo->va <= VMALLOC_END)) { /* non vmalloc */
m4u_put_sgtable_pages(pMvaInfo->sg_table);
}
}
}
ret = m4u_do_mva_free(mva, pMvaInfo->size);
if (ret) {
is_err = 1;
M4UMSG("do_mva_free fail\n");
}
if (pMvaInfo->va) { /* buffer is allocated by va */
m4u_destroy_sgtable(pMvaInfo->sg_table);
}
if (pMvaInfo->flags & M4U_FLAGS_SEQ_ACCESS) {
if (pMvaInfo->seq_id > 0)
m4u_invalid_seq_range_by_id(port, pMvaInfo->seq_id);
}
if (is_err) {
m4u_aee_print("%s fail: port=%s, mva=0x%x, size=0x%x, va=0x%lx\n", __func__,
m4u_get_port_name(port), mva, pMvaInfo->size, pMvaInfo->va);
ret = -EINVAL;
} else
ret = 0;
size = pMvaInfo->size;
#ifdef __M4U_MAP_MVA_TO_KERNEL_FOR_DEBUG__
/* unmap kernel va for debug */
{
if (pMvaInfo->mapped_kernel_va_for_debug) {
M4ULOG_MID("[kernel_va_debug] unmap va: mva=0x%x, kernel_va=0x%lx, size=0x%x\n",
pMvaInfo->mva, pMvaInfo->mapped_kernel_va_for_debug, pMvaInfo->size);
m4u_mva_unmap_kernel(pMvaInfo->mva, pMvaInfo->size,
pMvaInfo->mapped_kernel_va_for_debug);
}
}
#endif
m4u_free_buf_info(pMvaInfo);
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_DEALLOC_MVA], MMProfileFlagEnd, size, mva);
return ret;
}
int m4u_dma_cache_flush_all(void)
{
smp_inner_dcache_flush_all();
outer_flush_all();
return 0;
}
static struct vm_struct *cache_map_vm_struct;
static int m4u_cache_sync_init(void)
{
cache_map_vm_struct = get_vm_area(PAGE_SIZE, VM_ALLOC);
if (!cache_map_vm_struct)
return -ENOMEM;
return 0;
}
static void *m4u_cache_map_page_va(struct page *page)
{
int ret;
struct page **ppPage = &page;
ret = map_vm_area(cache_map_vm_struct, PAGE_KERNEL, ppPage);
if (ret) {
M4UMSG("error to map page\n");
return NULL;
}
return cache_map_vm_struct->addr;
}
static void m4u_cache_unmap_page_va(unsigned int va)
{
unmap_kernel_range((unsigned long)cache_map_vm_struct->addr, PAGE_SIZE);
}
static int __m4u_cache_sync_kernel(const void *start, size_t size, M4U_CACHE_SYNC_ENUM sync_type)
{
if (sync_type == M4U_CACHE_CLEAN_BY_RANGE)
dmac_map_area((void *)start, size, DMA_TO_DEVICE);
else if (sync_type == M4U_CACHE_INVALID_BY_RANGE)
dmac_unmap_area((void *)start, size, DMA_FROM_DEVICE);
else if (sync_type == M4U_CACHE_FLUSH_BY_RANGE)
dmac_flush_range((void *)start, (void *)(start + size));
return 0;
}
static struct page *m4u_cache_get_page(unsigned long va)
{
unsigned long start;
phys_addr_t pa;
struct page *page;
start = va & (~M4U_PAGE_MASK);
pa = m4u_user_v2p(start);
if ((pa == 0)) {
M4UMSG("error m4u_get_phys user_v2p return 0 on va=0x%lx\n", start);
/* dump_page(page); */
m4u_dump_mmaps(start);
show_pte(current->mm, va);
return NULL;
}
page = phys_to_page(pa);
return page;
}
/* lock to protect cache_map_vm_struct */
static DEFINE_MUTEX(gM4u_cache_sync_user_lock);
static int __m4u_cache_sync_user(unsigned long start, size_t size, M4U_CACHE_SYNC_ENUM sync_type)
{
unsigned long map_size, map_start, map_end;
unsigned long end = start + size;
struct page *page;
unsigned long map_va, map_va_align;
int ret = 0;
mutex_lock(&gM4u_cache_sync_user_lock);
if (!cache_map_vm_struct) {
M4UMSG(" error: cache_map_vm_struct is NULL, retry\n");
m4u_cache_sync_init();
}
if (!cache_map_vm_struct) {
M4UMSG("error: cache_map_vm_struct is NULL, no vmalloc area\n");
ret = -1;
goto out;
}
map_start = start;
while (map_start < end) {
map_end = min(((map_start & (~M4U_PAGE_MASK)) + PAGE_SIZE), end);
map_size = map_end - map_start;
page = m4u_cache_get_page(map_start);
if (!page) {
ret = -1;
goto out;
}
map_va = (unsigned long)m4u_cache_map_page_va(page);
if (!map_va) {
ret = -1;
goto out;
}
map_va_align = map_va | (map_start & (PAGE_SIZE - 1));
__m4u_cache_sync_kernel((void *)map_va_align, map_size, sync_type);
m4u_cache_unmap_page_va(map_va);
map_start = map_end;
}
out:
mutex_unlock(&gM4u_cache_sync_user_lock);
return ret;
}
int m4u_cache_sync_by_range(unsigned long va, unsigned int size,
M4U_CACHE_SYNC_ENUM sync_type, struct sg_table *table)
{
int ret = 0;
if (va < PAGE_OFFSET) { /* from user space */
ret = __m4u_cache_sync_user(va, size, sync_type);
} else {
ret = __m4u_cache_sync_kernel((void *)va, size, sync_type);
}
#ifdef CONFIG_OUTER_CACHE
{
struct scatterlist *sg;
int i;
for_each_sg(table->sgl, sg, table->nents, i) {
unsigned int len = sg_dma_len(sg);
phys_addr_t phys_addr = get_sg_phys(sg);
if (sync_type == M4U_CACHE_CLEAN_BY_RANGE)
outer_clean_range(phys_addr, phys_addr + len);
else if (sync_type == M4U_CACHE_INVALID_BY_RANGE)
outer_inv_range(phys_addr, phys_addr + len);
else if (sync_type == M4U_CACHE_FLUSH_BY_RANGE)
outer_flush_range(phys_addr, phys_addr + len);
}
}
#endif
return ret;
}
/**
notes: only mva allocated by m4u_alloc_mva can use this function.
if buffer is allocated by ion, please use ion_cache_sync
**/
int m4u_cache_sync(m4u_client_t *client, M4U_PORT_ID port,
unsigned long va, unsigned int size, unsigned int mva,
M4U_CACHE_SYNC_ENUM sync_type)
{
int ret = 0;
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_CACHE_SYNC], MMProfileFlagStart, va, mva);
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_CACHE_SYNC], MMProfileFlagPulse, size, ((sync_type)<<24) | port);
M4ULOG_MID("cache_sync port=%s, va=0x%lx, size=0x%x, mva=0x%x, type=%d\n",
m4u_get_port_name(port), va, size, mva, sync_type);
if (sync_type < M4U_CACHE_CLEAN_ALL) {
m4u_buf_info_t *pMvaInfo = NULL;
if (client)
pMvaInfo = m4u_client_find_buf(client, mva, 0);
/* some user may sync mva from other client (eg. ovl may not know
* who allocated this buffer, but he need to sync cache). */
/* we make a workaround here by query mva from mva manager */
if (!pMvaInfo)
pMvaInfo = mva_get_priv(mva);
if (!pMvaInfo) {
M4UMSG("cache sync fail, cannot find buf: mva=0x%x, client=0x%p\n", mva,
client);
m4u_dump_buf_info(NULL);
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_CACHE_SYNC], MMProfileFlagEnd, 0, 0);
return -1;
}
if ((pMvaInfo->size != size) || (pMvaInfo->va != va)) {
M4UMSG("cache_sync fail: expect mva=0x%x,size=0x%x,va=0x%lx, but mva=0x%x,size=0x%x,va=0x%lx\n",
pMvaInfo->mva, pMvaInfo->size, pMvaInfo->va, mva, size, va);
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_CACHE_SYNC], MMProfileFlagEnd,
pMvaInfo->va, pMvaInfo->mva);
return -1;
}
if ((va | size) & (L1_CACHE_BYTES - 1)) { /* va size should be cache line align */
M4UMSG("warning: cache_sync not align: va=0x%lx,size=0x%x,align=0x%x\n",
va, size, L1_CACHE_BYTES);
}
ret = m4u_cache_sync_by_range(va, size, sync_type, pMvaInfo->sg_table);
} else {
/* All cache operation */
if (sync_type == M4U_CACHE_CLEAN_ALL) {
smp_inner_dcache_flush_all();
outer_clean_all();
} else if (sync_type == M4U_CACHE_INVALID_ALL) {
M4UMSG("no one can use invalid all!\n");
return -1;
} else if (sync_type == M4U_CACHE_FLUSH_ALL) {
smp_inner_dcache_flush_all();
outer_flush_all();
}
}
MMProfileLogEx(M4U_MMP_Events[M4U_MMP_CACHE_SYNC], MMProfileFlagEnd, size, mva);
return ret;
}
void m4u_dma_map_area(void *start, size_t size, M4U_DMA_DIR dir)
{
if (dir == M4U_DMA_FROM_DEVICE)
dmac_map_area(start, size, DMA_FROM_DEVICE);
else if (dir == M4U_DMA_TO_DEVICE)
dmac_map_area(start, size, DMA_TO_DEVICE);
else if (dir == M4U_DMA_BIDIRECTIONAL)
dmac_map_area(start, size, DMA_BIDIRECTIONAL);
}
void m4u_dma_unmap_area(void *start, size_t size, M4U_DMA_DIR dir)
{
if (dir == M4U_DMA_FROM_DEVICE)
dmac_unmap_area(start, size, DMA_FROM_DEVICE);
else if (dir == M4U_DMA_TO_DEVICE)
dmac_unmap_area(start, size, DMA_TO_DEVICE);
else if (dir == M4U_DMA_BIDIRECTIONAL)
dmac_unmap_area(start, size, DMA_BIDIRECTIONAL);
}
long m4u_dma_op(m4u_client_t *client, M4U_PORT_ID port,
unsigned long va, unsigned int size, unsigned int mva,
M4U_DMA_TYPE dma_type, M4U_DMA_DIR dma_dir)
{
struct scatterlist *sg;
int i, j;
struct sg_table *table = NULL;
int npages = 0;
unsigned long start = -1;
m4u_buf_info_t *pMvaInfo = NULL;
if (client)
pMvaInfo = m4u_client_find_buf(client, mva, 0);
/* some user may sync mva from other client
(eg. ovl may not know who allocated this buffer,
but he need to sync cache).
we make a workaround here by query mva from mva manager */
if (!pMvaInfo)
pMvaInfo = mva_get_priv(mva);
if (!pMvaInfo) {
M4UMSG("m4u dma fail, cannot find buf: mva=0x%x, client=0x%p.\n", mva, client);
return -1;
}
if ((pMvaInfo->size != size) || (pMvaInfo->va != va)) {
M4UMSG("m4u dma fail: expect mva=0x%x,size=0x%x,va=0x%lx, but mva=0x%x,size=0x%x,va=0x%lx\n",
pMvaInfo->mva, pMvaInfo->size, pMvaInfo->va, mva, size, va);
return -1;
}
if ((va|size) & (L1_CACHE_BYTES-1)) /* va size should be cache line align */
M4UMSG("warning: cache_sync not align: va=0x%lx,size=0x%x,align=0x%x\n",
va, size, L1_CACHE_BYTES);
table = pMvaInfo->sg_table;
/* npages = PAGE_ALIGN(size) / PAGE_SIZE; */
npages = M4U_GET_PAGE_NUM(va, size);
mutex_lock(&gM4u_cache_sync_user_lock);
if (!cache_map_vm_struct) {
M4UMSG(" error: cache_map_vm_struct is NULL, retry\n");
m4u_cache_sync_init();
}
if (!cache_map_vm_struct) {
M4UMSG("error: cache_map_vm_struct is NULL, no vmalloc area\n");
mutex_unlock(&gM4u_cache_sync_user_lock);
return -ENOMEM;
}
for_each_sg(table->sgl, sg, table->nents, i) {
int npages_this_entry = PAGE_ALIGN(sg_dma_len(sg)) / PAGE_SIZE;
struct page *page = sg_page(sg);
if (!page) {
phys_addr_t pa = sg_dma_address(sg);
if (!pa) {
M4UMSG("m4u_dma_op fail, VM_PFNMAP, no page.\n");
return -EFAULT;
}
page = phys_to_page(pa);
if (!pfn_valid(page_to_pfn(page))) {
M4UMSG("m4u_dma_op fail, VM_PFNMAP, no page, va = 0x%lx, size = 0x%x, npages = 0x%x.\n",
va, size, npages);
return -EFAULT;
}
}
BUG_ON(i >= npages);
for (j = 0; j < npages_this_entry; j++) {
start = (unsigned long) m4u_cache_map_page_va(page++);
if (IS_ERR_OR_NULL((void *) start)) {
M4UMSG("cannot do cache sync: ret=%lu\n", start);
mutex_unlock(&gM4u_cache_sync_user_lock);
return -EFAULT;
}
if (dma_type == M4U_DMA_MAP_AREA)
m4u_dma_map_area((void *)start, PAGE_SIZE, dma_dir);
else if (dma_type == M4U_DMA_UNMAP_AREA)
m4u_dma_unmap_area((void *)start, PAGE_SIZE, dma_dir);
m4u_cache_unmap_page_va(start);
}
}
mutex_unlock(&gM4u_cache_sync_user_lock);
return 0;
}
int m4u_dump_info(int m4u_index)
{
return 0;
}
void m4u_get_pgd(m4u_client_t *client, M4U_PORT_ID port, void **pgd_va, void **pgd_pa,
unsigned int *size)
{
m4u_domain_t *pDomain;
pDomain = m4u_get_domain_by_port(port);
*pgd_va = pDomain->pgd;
*pgd_pa = (void *)(uintptr_t)pDomain->pgd_pa;
*size = M4U_PGD_SIZE;
}
unsigned long m4u_mva_to_pa(m4u_client_t *client, M4U_PORT_ID port, unsigned int mva)
{
unsigned long pa;
m4u_domain_t *pDomain;
pDomain = m4u_get_domain_by_port(port);
pa = m4u_get_pte(pDomain, mva);
return pa;
}
int m4u_query_mva_info(unsigned int mva, unsigned int size, unsigned int *real_mva,
unsigned int *real_size)
{
m4u_buf_info_t *pMvaInfo;
if ((!real_mva) || (!real_size))
return -1;
pMvaInfo = mva_get_priv(mva);
if (!pMvaInfo) {
M4UMSG("%s cannot find mva: mva=0x%x, size=0x%x\n", __func__, mva, size);
*real_mva = 0;
*real_size = 0;
return -2;
}
*real_mva = pMvaInfo->mva;
*real_size = pMvaInfo->size;
return 0;
}
EXPORT_SYMBOL(m4u_query_mva_info);
/***********************************************************/
/** map mva buffer to kernel va buffer
* this function should ONLY used for DEBUG
************************************************************/
int m4u_mva_map_kernel(unsigned int mva, unsigned int size, unsigned long *map_va,
unsigned int *map_size)
{
m4u_buf_info_t *pMvaInfo;
struct sg_table *table;
struct scatterlist *sg;
int i, j, k, ret = 0;
struct page **pages;
unsigned int page_num;
void *kernel_va;
unsigned int kernel_size;
pMvaInfo = mva_get_priv(mva);
if (!pMvaInfo || pMvaInfo->size < size) {
M4UMSG("%s cannot find mva: mva=0x%x, size=0x%x\n", __func__, mva, size);
if (pMvaInfo)
M4UMSG("pMvaInfo: mva=0x%x, size=0x%x\n", pMvaInfo->mva, pMvaInfo->size);
return -1;
}
table = pMvaInfo->sg_table;
page_num = M4U_GET_PAGE_NUM(mva, size);
pages = vmalloc(sizeof(struct page *) * page_num);
if (pages == NULL) {
M4UMSG("mva_map_kernel:error to vmalloc for %d\n",
(unsigned int)sizeof(struct page *) * page_num);
return -1;
}
k = 0;
for_each_sg(table->sgl, sg, table->nents, i) {
struct page *page_start;
int pages_in_this_sg = PAGE_ALIGN(sg_dma_len(sg)) / PAGE_SIZE;
#ifdef CONFIG_NEED_SG_DMA_LENGTH
if (0 == sg_dma_address(sg))
pages_in_this_sg = PAGE_ALIGN(sg->length) / PAGE_SIZE;
#endif
page_start = sg_page(sg);
for (j = 0; j < pages_in_this_sg; j++) {
pages[k++] = page_start++;
if (k >= page_num)
goto get_pages_done;
}
}
get_pages_done:
if (k < page_num) {
/* this should not happen, because we have checked the size before. */
M4UMSG("mva_map_kernel:only get %d pages: mva=0x%x, size=0x%x, pg_num=%d\n", k, mva,
size, page_num);
ret = -1;
goto error_out;
}
kernel_va = 0;
kernel_size = 0;
kernel_va = vmap(pages, page_num, VM_MAP, PAGE_KERNEL);
if (kernel_va == 0 || (unsigned long)kernel_va & M4U_PAGE_MASK) {
M4UMSG("mva_map_kernel:vmap fail: page_num=%d, kernel_va=0x%p\n", page_num,
kernel_va);
ret = -2;
goto error_out;
}
kernel_va += ((unsigned long)mva & (M4U_PAGE_MASK));
*map_va = (unsigned long)kernel_va;
*map_size = size;
error_out:
vfree(pages);
M4ULOG_LOW("mva_map_kernel:mva=0x%x,size=0x%x,map_va=0x%lx,map_size=0x%x\n",
mva, size, *map_va, *map_size);
return ret;
}
EXPORT_SYMBOL(m4u_mva_map_kernel);
int m4u_mva_unmap_kernel(unsigned int mva, unsigned int size, unsigned long map_va)
{
M4ULOG_LOW("mva_unmap_kernel:mva=0x%x,size=0x%x,va=0x%lx\n", mva, size, map_va);
vunmap((void *)(map_va & (~M4U_PAGE_MASK)));
return 0;
}
EXPORT_SYMBOL(m4u_mva_unmap_kernel);
static int MTK_M4U_open(struct inode *inode, struct file *file)
{
m4u_client_t *client;
client = m4u_create_client();
if (IS_ERR_OR_NULL(client)) {
M4UMSG("createclientfail\n");
return -ENOMEM;
}
file->private_data = client;
return 0;
}
static int MTK_M4U_release(struct inode *inode, struct file *file)
{
m4u_client_t *client = file->private_data;
m4u_destroy_client(client);
return 0;
}
static int MTK_M4U_flush(struct file *filp, fl_owner_t a_id)
{
return 0;
}
#ifdef M4U_TEE_SERVICE_ENABLE
#define TPLAY_DEV_NAME "tz_m4u"
#define M4U_DRV_UUID {{0x90, 0x73, 0xF0, 0x3A, 0x96, 0x18, 0x38, 0x3B, 0xB1, 0x85, 0x6E, 0xB3, 0xF9, 0x90, 0xBA, 0xBD} }
static const struct mc_uuid_t m4u_drv_uuid = M4U_DRV_UUID;
static struct mc_session_handle m4u_dci_session;
static m4u_msg_t *m4u_dci_msg;
static DEFINE_MUTEX(m4u_dci_mutex);
#define M4U_TL_UUID {{0x98, 0xfb, 0x95, 0xbc, 0xb4, 0xbf, 0x42, 0xd2, 0x64, 0x73, 0xea, 0xe4, 0x86, 0x90, 0xd7, 0xea} }
static const struct mc_uuid_t m4u_tl_uuid = M4U_TL_UUID;
static struct mc_session_handle m4u_tci_session;
static m4u_msg_t *m4u_tci_msg;
static DEFINE_MUTEX(m4u_tci_mutex);
static int m4u_open_trustlet(uint32_t deviceId)
{
enum mc_result mcRet;
/* Initialize session handle data */
memset(&m4u_tci_session, 0, sizeof(m4u_tci_session));
mcRet = mc_malloc_wsm(deviceId, 0, sizeof(m4u_msg_t), (uint8_t **) &m4u_tci_msg, 0);
if (MC_DRV_OK != mcRet) {
M4UMSG("tz_m4u: mc_malloc_wsm tci fail: %d\n", mcRet);
return -1;
}
/* Open session the trustlet */
m4u_tci_session.device_id = deviceId;
mcRet = mc_open_session(&m4u_tci_session,
&m4u_tl_uuid,
(uint8_t *) m4u_tci_msg,
(uint32_t) sizeof(m4u_msg_t));
if (MC_DRV_OK != mcRet) {
M4UMSG("tz_m4u: mc_open_session returned: %d\n", mcRet);
return -1;
}
M4UMSG("tz_m4u: open TCI session success\n");
return 0;
}
int m4u_close_trustlet(uint32_t deviceId)
{
enum mc_result mcRet;
mcRet = mc_free_wsm(deviceId, (uint8_t *) m4u_tci_msg);
if (mcRet) {
M4UMSG("tz_m4u: free tci struct fail: %d\n", mcRet);
return -1;
}
/* Close session */
mcRet = mc_close_session(&m4u_tci_session);
if (MC_DRV_OK != mcRet) {
M4UMSG("tz_m4u: mc_close_session returned: %d\n", mcRet);
return -1;
}
return 0;
}
static int m4u_exec_cmd(struct mc_session_handle *m4u_session, m4u_msg_t *m4u_msg)
{
enum mc_result ret;
if (NULL == m4u_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
return -1;
}
M4UMSG("Notify %x\n", m4u_msg->cmd);
ret = mc_notify(m4u_session);
if (MC_DRV_OK != ret) {
m4u_aee_print("tz_m4u Notify failed: %d\n", ret);
goto exit;
}
ret = mc_wait_notification(m4u_session, MC_INFINITE_TIMEOUT);
if (MC_DRV_OK != ret) {
m4u_aee_print("Wait for response notification failed: 0x%x\n", ret);
goto exit;
}
M4UMSG("get_resp %x\n", m4u_msg->cmd);
exit:
return ret;
}
static int __m4u_sec_init(void)
{
int ret;
void *pgd_va;
unsigned long pt_pa_nonsec;
unsigned int size;
mutex_lock(&m4u_tci_mutex);
if (NULL == m4u_tci_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
ret = MC_DRV_ERR_NO_FREE_MEMORY;
goto out;
}
m4u_get_pgd(NULL, 0, &pgd_va, (void *)&pt_pa_nonsec, &size);
m4u_tci_msg->cmd = CMD_M4UTL_INIT;
m4u_tci_msg->init_param.nonsec_pt_pa = pt_pa_nonsec;
m4u_tci_msg->init_param.l2_en = gM4U_L2_enable;
m4u_tci_msg->init_param.sec_pt_pa = 0; /* m4u_alloc_sec_pt_for_debug(); */
M4UMSG("%s call m4u_exec_cmd CMD_M4UTL_INIT, nonsec_pt_pa: 0x%lx\n", __func__,
pt_pa_nonsec);
ret = m4u_exec_cmd(&m4u_tci_session, m4u_tci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_tci_msg->rsp;
out:
mutex_unlock(&m4u_tci_mutex);
return ret;
}
/* ------------------------------------------------------------- */
#ifdef __M4U_SECURE_SYSTRACE_ENABLE__
static int dr_map(unsigned long pa, size_t size)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (!m4u_dci_msg) {
M4UMSG("error: m4u_dci_msg==null\n");
ret = -1;
goto out;
}
memset(m4u_dci_msg, 0, sizeof(m4u_msg_t));
m4u_dci_msg->cmd = CMD_M4U_SYSTRACE_MAP;
m4u_dci_msg->systrace_param.pa = pa;
m4u_dci_msg->systrace_param.size = size;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
static int dr_unmap(unsigned long pa, size_t size)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (!m4u_dci_msg) {
M4UMSG("error: m4u_dci_msg==null\n");
ret = -1;
goto out;
}
memset(m4u_dci_msg, 0, sizeof(m4u_msg_t));
m4u_dci_msg->cmd = CMD_M4U_SYSTRACE_UNMAP;
m4u_dci_msg->systrace_param.pa = pa;
m4u_dci_msg->systrace_param.size = size;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
static int dr_transact(void)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (!m4u_dci_msg) {
M4UMSG("error: m4u_dci_msg==null\n");
ret = -1;
goto out;
}
memset(m4u_dci_msg, 0, sizeof(m4u_msg_t));
m4u_dci_msg->cmd = CMD_M4U_SYSTRACE_TRANSACT;
m4u_dci_msg->systrace_param.pa = 0;
m4u_dci_msg->systrace_param.size = 0;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
#endif
/* ------------------------------------------------------------- */
int m4u_sec_init(void)
{
uint32_t deviceId = MC_DEVICE_ID_DEFAULT;
enum mc_result mcRet;
if (m4u_tee_en) {
M4UMSG("warning: m4u secure has been inited, %d\n", m4u_tee_en);
return 0;
}
M4UMSG("call m4u_sec_init in nornal m4u driver\n");
/* Initialize session handle data */
memset(&m4u_dci_session, 0, sizeof(m4u_dci_session));
/* Open MobiCore device */
mcRet = mc_open_device(deviceId);
if (MC_DRV_OK != mcRet) {
M4UMSG("tz_m4u: error mc_open_device returned: %d\n", mcRet);
if (mcRet != MC_DRV_ERR_INVALID_OPERATION)
return -1;
}
/* Allocating WSM for DCI */
mcRet = mc_malloc_wsm(deviceId, 0, sizeof(m4u_msg_t), (uint8_t **) &m4u_dci_msg, 0);
if (MC_DRV_OK != mcRet) {
M4UMSG("tz_m4u: mc_malloc_wsm returned: %d\n", mcRet);
return -1;
}
/* Open session the trustlet */
m4u_dci_session.device_id = deviceId;
mcRet = mc_open_session(&m4u_dci_session,
&m4u_drv_uuid,
(uint8_t *) m4u_dci_msg,
(uint32_t) sizeof(m4u_msg_t));
if (MC_DRV_OK != mcRet) {
M4UMSG("tz_m4u: mc_open_session returned: %d\n", mcRet);
return -1;
}
M4UMSG("tz_m4u: open DCI session returned: %d\n", mcRet);
{
volatile int i, j;
for (i = 0; i < 10000000; i++)
j++;
}
m4u_open_trustlet(deviceId);
__m4u_sec_init();
#ifdef __M4U_SECURE_SYSTRACE_ENABLE__
{
union callback_func callback;
callback.dr.map = dr_map;
callback.dr.unmap = dr_unmap;
callback.dr.transact = dr_transact;
init_sectrace("M4U", if_dci, usage_dr, 64, &callback);
}
#endif
m4u_close_trustlet(deviceId);
m4u_tee_en = 1;
return 0;
}
int m4u_config_port_tee(M4U_PORT_STRUCT *pM4uPort) /* native */
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (!m4u_dci_msg) {
M4UMSG("error: m4u_dci_msg==null\n");
ret = -1;
goto out;
}
m4u_dci_msg->cmd = CMD_M4U_CFG_PORT;
m4u_dci_msg->port_param.port = pM4uPort->ePortID;
m4u_dci_msg->port_param.virt = pM4uPort->Virtuality;
m4u_dci_msg->port_param.direction = pM4uPort->Direction;
m4u_dci_msg->port_param.distance = pM4uPort->Distance;
m4u_dci_msg->port_param.sec = 0;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
int m4u_config_port_array_tee(unsigned char *port_array) /* native */
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (!m4u_dci_msg) {
M4UMSG("error: m4u_dci_msg==null\n");
ret = -1;
goto out;
}
memset(m4u_dci_msg, 0, sizeof(m4u_msg_t));
memcpy(m4u_dci_msg->port_array_param.m4u_port_array, port_array,
sizeof(m4u_dci_msg->port_array_param.m4u_port_array));
m4u_dci_msg->cmd = CMD_M4U_CFG_PORT_ARRAY;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
static int m4u_unmap_nonsec_buffer(unsigned int mva, unsigned int size)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (NULL == m4u_dci_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
ret = MC_DRV_ERR_NO_FREE_MEMORY;
goto out;
}
m4u_dci_msg->cmd = CMD_M4U_UNMAP_NONSEC_BUFFER;
m4u_dci_msg->buf_param.mva = mva;
m4u_dci_msg->buf_param.size = size;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
int m4u_larb_backup_sec(unsigned int larb_idx)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (NULL == m4u_dci_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
ret = MC_DRV_ERR_NO_FREE_MEMORY;
goto out;
}
m4u_dci_msg->cmd = CMD_M4U_LARB_BACKUP;
m4u_dci_msg->larb_param.larb_idx = larb_idx;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
int m4u_larb_restore_sec(unsigned int larb_idx)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (NULL == m4u_dci_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
ret = MC_DRV_ERR_NO_FREE_MEMORY;
goto out;
}
m4u_dci_msg->cmd = CMD_M4U_LARB_RESTORE;
m4u_dci_msg->larb_param.larb_idx = larb_idx;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
static int m4u_reg_backup_sec(void)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (NULL == m4u_dci_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
ret = MC_DRV_ERR_NO_FREE_MEMORY;
goto out;
}
m4u_dci_msg->cmd = CMD_M4U_REG_BACKUP;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
static int m4u_reg_restore_sec(void)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (NULL == m4u_dci_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
ret = MC_DRV_ERR_NO_FREE_MEMORY;
goto out;
}
m4u_dci_msg->cmd = CMD_M4U_REG_RESTORE;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
/* static void m4u_early_suspend(struct early_suspend *h)
{
M4UMSG("m4u_early_suspend +, %d\n", m4u_tee_en);
if (m4u_tee_en)
m4u_reg_backup_sec();
M4UMSG("m4u_early_suspend -\n");
}
static void m4u_late_resume(struct early_suspend *h)
{
M4UMSG("m4u_late_resume +, %d\n", m4u_tee_en);
if (m4u_tee_en)
m4u_reg_restore_sec();
M4UMSG("m4u_late_resume -\n");
}
static struct early_suspend mtk_m4u_early_suspend_driver = {
.level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 251,
.suspend = m4u_early_suspend,
.resume = m4u_late_resume,
}; */
static void m4u_early_suspend(void)
{
M4UMSG("m4u_early_suspend +, %d\n", m4u_tee_en);
if (m4u_tee_en)
m4u_reg_backup_sec();
M4UMSG("m4u_early_suspend -\n");
}
static void m4u_late_resume(void)
{
M4UMSG("m4u_late_resume +, %d\n", m4u_tee_en);
if (m4u_tee_en)
m4u_reg_restore_sec();
M4UMSG("m4u_late_resume -\n");
}
static struct notifier_block m4u_fb_notifier;
static int m4u_fb_notifier_callback(struct notifier_block *self, unsigned long event, void *data)
{
struct fb_event *evdata = data;
int blank;
M4UMSG("m4u_fb_notifier_callback %ld, %d\n", event , FB_EVENT_BLANK);
if (event != FB_EVENT_BLANK)
return 0;
blank = *(int *)evdata->data;
switch (blank) {
case FB_BLANK_UNBLANK:
case FB_BLANK_NORMAL:
m4u_late_resume();
break;
case FB_BLANK_VSYNC_SUSPEND:
case FB_BLANK_HSYNC_SUSPEND:
break;
case FB_BLANK_POWERDOWN:
m4u_early_suspend();
break;
default:
return -EINVAL;
}
return 0;
}
#if 1
int m4u_map_nonsec_buf(int port, unsigned int mva, unsigned int size)
{
int ret;
mutex_lock(&m4u_dci_mutex);
if (NULL == m4u_dci_msg) {
M4UMSG("%s TCI/DCI error\n", __func__);
ret = MC_DRV_ERR_NO_FREE_MEMORY;
goto out;
}
m4u_dci_msg->cmd = CMD_M4U_MAP_NONSEC_BUFFER;
m4u_dci_msg->buf_param.mva = mva;
m4u_dci_msg->buf_param.size = size;
ret = m4u_exec_cmd(&m4u_dci_session, m4u_dci_msg);
if (ret) {
M4UMSG("m4u exec command fail\n");
ret = -1;
goto out;
}
ret = m4u_dci_msg->rsp;
out:
mutex_unlock(&m4u_dci_mutex);
return ret;
}
#endif
#endif
#ifdef M4U_TEE_SERVICE_ENABLE
static DEFINE_MUTEX(gM4u_sec_init);
#endif
static long MTK_M4U_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int ret = 0;
M4U_MOUDLE_STRUCT m4u_module;
M4U_PORT_STRUCT m4u_port;
/*M4U_PORT_ID PortID;*/
M4U_PORT_ID ModuleID;
M4U_CACHE_STRUCT m4u_cache_data;
M4U_DMA_STRUCT m4u_dma_data;
m4u_client_t *client = filp->private_data;
switch (cmd) {
case MTK_M4U_T_POWER_ON:
ret = copy_from_user(&ModuleID, (void *)arg, sizeof(unsigned int));
if (ModuleID < 0 || ModuleID >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", ModuleID);
return -EFAULT;
}
if (ret) {
M4UMSG("MTK_M4U_T_POWER_ON,copy_from_user failed,%d\n", ret);
return -EFAULT;
}
ret = m4u_power_on(ModuleID);
break;
case MTK_M4U_T_POWER_OFF:
ret = copy_from_user(&ModuleID, (void *)arg, sizeof(unsigned int));
if (ModuleID < 0 || ModuleID >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", ModuleID);
return -EFAULT;
}
if (ret) {
M4UMSG("MTK_M4U_T_POWER_OFF,copy_from_user failed,%d\n", ret);
return -EFAULT;
}
ret = m4u_power_off(ModuleID);
break;
case MTK_M4U_T_ALLOC_MVA:
ret = copy_from_user(&m4u_module, (void *)arg, sizeof(M4U_MOUDLE_STRUCT));
if (m4u_module.port < 0 || m4u_module.port >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", m4u_module.port);
return -EFAULT;
}
if (ret) {
M4UMSG("MTK_M4U_T_ALLOC_MVA,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
ret = m4u_alloc_mva(client, m4u_module.port, m4u_module.BufAddr, NULL,
m4u_module.BufSize, m4u_module.prot, m4u_module.flags,
&(m4u_module.MVAStart));
if (ret)
return ret;
ret = copy_to_user(&(((M4U_MOUDLE_STRUCT *) arg)->MVAStart),
&(m4u_module.MVAStart), sizeof(unsigned int));
if (ret) {
M4UMSG("MTK_M4U_T_ALLOC_MVA,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
break;
case MTK_M4U_T_DEALLOC_MVA:
{
ret = copy_from_user(&m4u_module, (void *)arg, sizeof(M4U_MOUDLE_STRUCT));
if (ret) {
M4UMSG("MTK_M4U_T_DEALLOC_MVA,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
if (m4u_module.port < 0 || m4u_module.port >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", m4u_module.port);
return -EFAULT;
}
ret = m4u_dealloc_mva(client, m4u_module.port, m4u_module.MVAStart);
if (ret)
return ret;
}
break;
case MTK_M4U_T_DUMP_INFO:
ret = copy_from_user(&ModuleID, (void *)arg, sizeof(unsigned int));
if (ModuleID < 0 || ModuleID >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", ModuleID);
return -EFAULT;
}
if (ret) {
M4UMSG("MTK_M4U_Invalid_TLB_Range,copy_from_user failed,%d\n", ret);
return -EFAULT;
}
break;
case MTK_M4U_T_CACHE_SYNC:
ret = copy_from_user(&m4u_cache_data, (void *)arg, sizeof(M4U_CACHE_STRUCT));
if (ret) {
M4UMSG("m4u_cache_sync,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
ret = m4u_cache_sync(client, m4u_cache_data.port, m4u_cache_data.va,
m4u_cache_data.size, m4u_cache_data.mva,
m4u_cache_data.eCacheSync);
break;
case MTK_M4U_T_DMA_OP:
ret = copy_from_user(&m4u_dma_data, (void *) arg,
sizeof(M4U_DMA_STRUCT));
if (ret) {
M4UMSG("m4u dma map/unmap area,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
ret = m4u_dma_op(client, m4u_dma_data.port, m4u_dma_data.va,
m4u_dma_data.size, m4u_dma_data.mva,
m4u_dma_data.eDMAType, m4u_dma_data.eDMADir);
break;
case MTK_M4U_T_CONFIG_PORT:
ret = copy_from_user(&m4u_port, (void *)arg, sizeof(M4U_PORT_STRUCT));
if (m4u_port.ePortID < 0 || m4u_port.ePortID >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", m4u_port.ePortID);
return -EFAULT;
}
if (ret) {
M4UMSG("MTK_M4U_T_CONFIG_PORT,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
#ifdef M4U_TEE_SERVICE_ENABLE
mutex_lock(&gM4u_sec_init);
#endif
ret = m4u_config_port(&m4u_port);
#ifdef M4U_TEE_SERVICE_ENABLE
mutex_unlock(&gM4u_sec_init);
#endif
break;
case MTK_M4U_T_CACHE_FLUSH_ALL:
m4u_dma_cache_flush_all();
break;
case MTK_M4U_T_CONFIG_PORT_ARRAY:
{
struct m4u_port_array port_array;
ret = copy_from_user(&port_array, (void *)arg, sizeof(struct m4u_port_array));
if (ret) {
M4UMSG("MTK_M4U_T_CONFIG_PORT,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
#ifdef M4U_TEE_SERVICE_ENABLE
mutex_lock(&gM4u_sec_init);
#endif
ret = m4u_config_port_array(&port_array);
#ifdef M4U_TEE_SERVICE_ENABLE
mutex_unlock(&gM4u_sec_init);
#endif
}
break;
case MTK_M4U_T_CONFIG_MAU:
{
M4U_MAU_STRUCT rMAU;
ret = copy_from_user(&rMAU, (void *)arg, sizeof(M4U_MAU_STRUCT));
if (rMAU.port < 0 || rMAU.port >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", rMAU.port);
return -EFAULT;
}
if (ret) {
M4UMSG("MTK_M4U_T_CONFIG_MAU,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
ret = config_mau(rMAU);
}
break;
case MTK_M4U_T_CONFIG_TF:
{
M4U_TF_STRUCT rM4UTF;
ret = copy_from_user(&rM4UTF, (void *)arg, sizeof(M4U_TF_STRUCT));
if (rM4UTF.port < 0 || rM4UTF.port >= M4U_PORT_UNKNOWN) {
M4UMSG("from user port id is invald,%d\n", rM4UTF.port);
return -EFAULT;
}
if (ret) {
M4UMSG("MTK_M4U_T_CONFIG_TF,copy_from_user failed:%d\n", ret);
return -EFAULT;
}
ret = m4u_enable_tf(rM4UTF.port, rM4UTF.fgEnable);
}
break;
#ifdef M4U_TEE_SERVICE_ENABLE
case MTK_M4U_T_SEC_INIT:
{
M4UMSG("MTK M4U ioctl : MTK_M4U_T_SEC_INIT command!! 0x%x\n", cmd);
mutex_lock(&gM4u_sec_init);
ret = m4u_sec_init();
mutex_unlock(&gM4u_sec_init);
}
break;
#endif
default:
/* M4UMSG("MTK M4U ioctl:No such command!!\n"); */
ret = -EINVAL;
break;
}
return ret;
}
#if IS_ENABLED(CONFIG_COMPAT)
typedef struct {
compat_uint_t port;
compat_ulong_t BufAddr;
compat_uint_t BufSize;
compat_uint_t prot;
compat_uint_t MVAStart;
compat_uint_t MVAEnd;
compat_uint_t flags;
} COMPAT_M4U_MOUDLE_STRUCT;
typedef struct {
compat_uint_t port;
compat_uint_t eCacheSync;
compat_ulong_t va;
compat_uint_t size;
compat_uint_t mva;
} COMPAT_M4U_CACHE_STRUCT;
typedef struct {
compat_uint_t port;
compat_uint_t eDMAType;
compat_uint_t eDMADir;
compat_ulong_t va;
compat_uint_t size;
compat_uint_t mva;
} COMPAT_M4U_DMA_STRUCT;
#define COMPAT_MTK_M4U_T_ALLOC_MVA _IOWR(MTK_M4U_MAGICNO, 4, int)
#define COMPAT_MTK_M4U_T_DEALLOC_MVA _IOW(MTK_M4U_MAGICNO, 5, int)
#define COMPAT_MTK_M4U_T_CACHE_SYNC _IOW(MTK_M4U_MAGICNO, 10, int)
#define COMPAT_MTK_M4U_T_DMA_OP _IOW(MTK_M4U_MAGICNO, 29, int)
static int compat_get_m4u_module_struct(COMPAT_M4U_MOUDLE_STRUCT __user *data32,
M4U_MOUDLE_STRUCT __user *data)
{
compat_uint_t u;
compat_ulong_t l;
int err;
err = get_user(u, &(data32->port));
err |= put_user(u, &(data->port));
err |= get_user(l, &(data32->BufAddr));
err |= put_user(l, &(data->BufAddr));
err |= get_user(u, &(data32->BufSize));
err |= put_user(u, &(data->BufSize));
err |= get_user(u, &(data32->prot));
err |= put_user(u, &(data->prot));
err |= get_user(u, &(data32->MVAStart));
err |= put_user(u, &(data->MVAStart));
err |= get_user(u, &(data32->MVAEnd));
err |= put_user(u, &(data->MVAEnd));
err |= get_user(u, &(data32->flags));
err |= put_user(u, &(data->flags));
return err;
}
static int compat_put_m4u_module_struct(COMPAT_M4U_MOUDLE_STRUCT __user *data32,
M4U_MOUDLE_STRUCT __user *data)
{
compat_uint_t u;
compat_ulong_t l;
int err;
err = get_user(u, &(data->port));
err |= put_user(u, &(data32->port));
err |= get_user(l, &(data->BufAddr));
err |= put_user(l, &(data32->BufAddr));
err |= get_user(u, &(data->BufSize));
err |= put_user(u, &(data32->BufSize));
err |= get_user(u, &(data->prot));
err |= put_user(u, &(data32->prot));
err |= get_user(u, &(data->MVAStart));
err |= put_user(u, &(data32->MVAStart));
err |= get_user(u, &(data->MVAEnd));
err |= put_user(u, &(data32->MVAEnd));
err |= get_user(u, &(data->flags));
err |= put_user(u, &(data32->flags));
return err;
}
static int compat_get_m4u_cache_struct(COMPAT_M4U_CACHE_STRUCT __user *data32,
M4U_CACHE_STRUCT __user *data)
{
compat_uint_t u;
compat_ulong_t l;
int err;
err = get_user(u, &(data32->port));
err |= put_user(u, &(data->port));
err |= get_user(u, &(data32->eCacheSync));
err |= put_user(u, &(data->eCacheSync));
err |= get_user(l, &(data32->va));
err |= put_user(l, &(data->va));
err |= get_user(u, &(data32->size));
err |= put_user(u, &(data->size));
err |= get_user(u, &(data32->mva));
err |= put_user(u, &(data->mva));
return err;
}
static int compat_get_m4u_dma_struct(
COMPAT_M4U_DMA_STRUCT __user *data32,
M4U_DMA_STRUCT __user *data)
{
compat_uint_t u;
compat_ulong_t l;
int err;
err = get_user(u, &(data32->port));
err |= put_user(u, &(data->port));
err |= get_user(u, &(data32->eDMAType));
err |= put_user(u, &(data->eDMAType));
err |= get_user(u, &(data32->eDMADir));
err |= put_user(u, &(data->eDMADir));
err |= get_user(l, &(data32->va));
err |= put_user(l, &(data->va));
err |= get_user(u, &(data32->size));
err |= put_user(u, &(data->size));
err |= get_user(u, &(data32->mva));
err |= put_user(u, &(data->mva));
return err;
}
long MTK_M4U_COMPAT_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
long ret;
if (!filp->f_op || !filp->f_op->unlocked_ioctl)
return -ENOTTY;
switch (cmd) {
case COMPAT_MTK_M4U_T_ALLOC_MVA:
{
COMPAT_M4U_MOUDLE_STRUCT __user *data32;
M4U_MOUDLE_STRUCT __user *data;
int err;
data32 = compat_ptr(arg);
data = compat_alloc_user_space(sizeof(M4U_MOUDLE_STRUCT));
if (data == NULL)
return -EFAULT;
err = compat_get_m4u_module_struct(data32, data);
if (err)
return err;
ret = filp->f_op->unlocked_ioctl(filp, MTK_M4U_T_ALLOC_MVA, (unsigned long)data);
err = compat_put_m4u_module_struct(data32, data);
if (err)
return err;
return ret;
}
case COMPAT_MTK_M4U_T_DEALLOC_MVA:
{
COMPAT_M4U_MOUDLE_STRUCT __user *data32;
M4U_MOUDLE_STRUCT __user *data;
int err;
data32 = compat_ptr(arg);
data = compat_alloc_user_space(sizeof(M4U_MOUDLE_STRUCT));
if (data == NULL)
return -EFAULT;
err = compat_get_m4u_module_struct(data32, data);
if (err)
return err;
return filp->f_op->unlocked_ioctl(filp, MTK_M4U_T_DEALLOC_MVA,
(unsigned long)data);
}
case COMPAT_MTK_M4U_T_CACHE_SYNC:
{
COMPAT_M4U_CACHE_STRUCT __user *data32;
M4U_CACHE_STRUCT __user *data;
int err;
data32 = compat_ptr(arg);
data = compat_alloc_user_space(sizeof(M4U_CACHE_STRUCT));
if (data == NULL)
return -EFAULT;
err = compat_get_m4u_cache_struct(data32, data);
if (err)
return err;
return filp->f_op->unlocked_ioctl(filp, MTK_M4U_T_CACHE_SYNC,
(unsigned long)data);
}
case COMPAT_MTK_M4U_T_DMA_OP:
{
COMPAT_M4U_DMA_STRUCT __user *data32;
M4U_DMA_STRUCT __user *data;
int err;
data32 = compat_ptr(arg);
data = compat_alloc_user_space(sizeof(M4U_DMA_STRUCT));
if (data == NULL)
return -EFAULT;
err = compat_get_m4u_dma_struct(data32, data);
if (err)
return err;
return filp->f_op->unlocked_ioctl(filp, MTK_M4U_T_DMA_OP,
(unsigned long)data);
}
case MTK_M4U_T_POWER_ON:
case MTK_M4U_T_POWER_OFF:
case MTK_M4U_T_DUMP_INFO:
case MTK_M4U_T_CONFIG_PORT:
case MTK_M4U_T_MONITOR_START:
case MTK_M4U_T_MONITOR_STOP:
case MTK_M4U_T_CACHE_FLUSH_ALL:
case MTK_M4U_T_CONFIG_PORT_ARRAY:
case MTK_M4U_T_SEC_INIT:
return filp->f_op->unlocked_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
default:
return -ENOIOCTLCMD;
}
}
#else
#define MTK_M4U_COMPAT_ioctl NULL
#endif
static const struct file_operations m4u_fops = {
.owner = THIS_MODULE,
.open = MTK_M4U_open,
.release = MTK_M4U_release,
.flush = MTK_M4U_flush,
.unlocked_ioctl = MTK_M4U_ioctl,
.compat_ioctl = MTK_M4U_COMPAT_ioctl,
/* .mmap = NULL; */
};
static int m4u_probe(struct platform_device *pdev)
{
struct device_node *node = pdev->dev.of_node;
M4UINFO("m4u_probe 0\n");
if (pdev->dev.of_node) {
int err;
err = of_property_read_u32(node, "cell-index", &pdev->id);
if (err)
M4UMSG("[DTS] get m4u platform_device id fail!!\n");
}
M4UINFO("m4u_probe 1, pdev id = %d name = %s\n", pdev->id, pdev->name);
gM4uDev->pDev[pdev->id] = &pdev->dev;
gM4uDev->m4u_base[pdev->id] = (unsigned long)of_iomap(node, 0);
gM4uDev->irq_num[pdev->id] = irq_of_parse_and_map(node, 0);
M4UMSG("m4u_probe 2, of_iomap: 0x%lx, irq_num: %d, pDev: %p\n",
gM4uDev->m4u_base[pdev->id], gM4uDev->irq_num[pdev->id], gM4uDev->pDev[pdev->id]);
if (0 == pdev->id) {
m4u_domain_init(gM4uDev, &gMvaNode_unknown);
#ifdef M4U_TEE_SERVICE_ENABLE
{
m4u_buf_info_t *pMvaInfo;
unsigned int mva;
pMvaInfo = m4u_alloc_buf_info();
if (!pMvaInfo) {
pMvaInfo->port = M4U_PORT_UNKNOWN;
pMvaInfo->size = M4U_NONSEC_MVA_START - 0x100000;
}
mva = m4u_do_mva_alloc(0, M4U_NONSEC_MVA_START - 0x100000, pMvaInfo);
M4UINFO("reserve sec mva: 0x%x\n", mva);
}
#endif
}
m4u_hw_init(gM4uDev, pdev->id);
M4UINFO("m4u_probe 3 finish...\n");
return 0;
}
static int m4u_remove(struct platform_device *pdev)
{
m4u_hw_deinit(gM4uDev, pdev->id);
#ifndef __M4U_USE_PROC_NODE
misc_deregister(&(gM4uDev->dev));
#else
if (gM4uDev->m4u_dev_proc_entry)
proc_remove(gM4uDev->m4u_dev_proc_entry);
#endif
return 0;
}
static int m4u_suspend(struct platform_device *pdev, pm_message_t mesg)
{
m4u_reg_backup();
M4UINFO("M4U backup in suspend\n");
return 0;
}
static int m4u_resume(struct platform_device *pdev)
{
m4u_reg_restore();
M4UINFO("M4U restore in resume\n");
return 0;
}
/*---------------------------------------------------------------------------*/
#ifdef CONFIG_PM
/*---------------------------------------------------------------------------*/
static int m4u_pm_suspend(struct device *device)
{
struct platform_device *pdev = to_platform_device(device);
BUG_ON(pdev == NULL);
return m4u_suspend(pdev, PMSG_SUSPEND);
}
static int m4u_pm_resume(struct device *device)
{
struct platform_device *pdev = to_platform_device(device);
BUG_ON(pdev == NULL);
return m4u_resume(pdev);
}
static int m4u_pm_restore_noirq(struct device *device)
{
int i;
for (i = 0; i < TOTAL_M4U_NUM; i++) {
irq_set_irq_type(gM4uDev->irq_num[i], IRQF_TRIGGER_LOW);
}
return 0;
}
/*---------------------------------------------------------------------------*/
#else /*CONFIG_PM */
/*---------------------------------------------------------------------------*/
#define m4u_pm_suspend NULL
#define m4u_pm_resume NULL
#define m4u_pm_restore_noirq NULL
/*---------------------------------------------------------------------------*/
#endif /*CONFIG_PM */
/*---------------------------------------------------------------------------*/
static const struct of_device_id iommu_of_ids[] = {
{.compatible = "mediatek,m4u",},
{.compatible = "mediatek,perisys_iommu",},
{}
};
const struct dev_pm_ops m4u_pm_ops = {
.suspend = m4u_pm_suspend,
.resume = m4u_pm_resume,
.freeze = m4u_pm_suspend,
.thaw = m4u_pm_resume,
.poweroff = m4u_pm_suspend,
.restore = m4u_pm_resume,
.restore_noirq = m4u_pm_restore_noirq,
};
static struct platform_driver m4uDrv = {
.probe = m4u_probe,
.remove = m4u_remove,
.suspend = m4u_suspend,
.resume = m4u_resume,
.driver = {
.name = "m4u",
.of_match_table = iommu_of_ids,
#ifdef CONFIG_PM
.pm = &m4u_pm_ops,
#endif
.owner = THIS_MODULE,
}
};
#if 0
static u64 m4u_dmamask = ~(u32) 0;
static struct platform_device mtk_m4u_dev = {
.name = M4U_DEV_NAME,
.id = 0,
.dev = {
.dma_mask = &m4u_dmamask,
.coherent_dma_mask = 0xffffffffUL}
};
#endif
#define __M4U_USE_PROC_NODE
static int __init MTK_M4U_Init(void)
{
int ret = 0;
gM4uDev = kzalloc(sizeof(struct m4u_device), GFP_KERNEL);
M4UINFO("MTK_M4U_Init kzalloc: %p\n", gM4uDev);
if (!gM4uDev) {
M4UMSG("kmalloc for m4u_device fail\n");
return -ENOMEM;
}
#ifndef __M4U_USE_PROC_NODE
gM4uDev->dev.minor = MISC_DYNAMIC_MINOR;
gM4uDev->dev.name = M4U_DEV_NAME;
gM4uDev->dev.fops = &m4u_fops;
gM4uDev->dev.parent = NULL;
ret = misc_register(&(gM4uDev->dev));
M4UINFO("misc_register, minor: %d\n", gM4uDev->dev.minor);
if (ret) {
M4UMSG("failed to register misc device.\n");
return ret;
}
#else
gM4uDev->m4u_dev_proc_entry = proc_create("m4u", 0, NULL, &m4u_fops);
if (!(gM4uDev->m4u_dev_proc_entry)) {
M4UMSG("m4u:failed to register m4u in proc/m4u_device.\n");
return ret;
}
#endif
m4u_debug_init(gM4uDev);
M4UINFO("M4U platform_driver_register start\n");
if (platform_driver_register(&m4uDrv)) {
M4UMSG("failed to register M4U driver");
return -ENODEV;
}
M4UINFO("M4U platform_driver_register finsish\n");
#if 0
retval = platform_device_register(&mtk_m4u_dev);
if (retval != 0)
return retval;
#endif
#ifdef M4U_PROFILE
m4u_profile_init();
#endif
#ifdef M4U_TEE_SERVICE_ENABLE
m4u_fb_notifier.notifier_call = m4u_fb_notifier_callback;
ret = fb_register_client(&m4u_fb_notifier);
if (ret)
M4UMSG("m4u register fb_notifier failed! ret(%d)\n", ret);
else
M4UMSG("m4u register fb_notifier OK!\n");
#endif
return 0;
}
static int __init mtk_m4u_late_init(void)
{
#if !defined(CONFIG_MTK_LEGACY)
smi_common_clock_off();
smi_larb0_clock_off();
#endif
return 0;
}
static void __exit MTK_M4U_Exit(void)
{
platform_driver_unregister(&m4uDrv);
}
subsys_initcall(MTK_M4U_Init);
late_initcall(mtk_m4u_late_init);
module_exit(MTK_M4U_Exit);
MODULE_DESCRIPTION("MTKM4Udriver");
MODULE_AUTHOR("MTK80347 <Xiang.Xu@mediatek.com>");
MODULE_LICENSE("GPL");
|