1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
|
/*++
* WonderMedia Memory Block driver
*
* Copyright c 2010 WonderMedia Technologies, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* WonderMedia Technologies, Inc.
* 10F, 529, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C
--*/
/*----------------------------------------------------------------------------
* MODULE : driver/video/wmt/memblock.c
* DATE : 2008/12/08
* DESCRIPTION : Physical memory management driver
* HISTORY :
* Version 1.0.0.2 , 2013/11/12
*----------------------------------------------------------------------------*/
#ifndef MEMBLOCK_C
#define MEMBLOCK_C
#endif
#include <linux/version.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/mman.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <asm/page.h>
#include <asm/uaccess.h>
#include <asm/cacheflush.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/swap.h>
#include <mach/wmt_env.h>
#include "com-mb.h"
#include <linux/wmt-mb.h>
#define THE_MB_USER "WMT-MB"
#define MB_VERSION_MAJOR 1
#define MB_VERSION_MINOR 0
#define MB_VERSION_MICRO 0
#define MB_VERSION_BUILD 6
#define MB_DEBUG
#define MB_INFO(fmt, args...) \
printk(KERN_INFO "["THE_MB_USER"] " fmt , ## args)
#define MB_WARN(fmt, args...) \
printk(KERN_WARNING "["THE_MB_USER" *W*] " fmt, ## args)
#define MB_ERROR(fmt, args...) \
printk(KERN_ERR "["THE_MB_USER" *E*] " fmt , ## args)
#ifdef MB_DEBUG
#define MB_OPDBG(fmt, args...) { /* FILE OPERATION DBG */ \
if (MBMSG_LEVEL) { \
printk("\t" KERN_DEBUG "["THE_MB_USER"] %s: " fmt, \
__func__ , ## args); \
} \
}
#define MB_DBG(fmt, args...) { \
if (MBMSG_LEVEL > 1) { \
printk("\t" KERN_DEBUG "["THE_MB_USER"] %s: " fmt, \
__func__ , ## args); \
} \
}
#define MB_WDBG(fmt, args...) { \
if (MBMSG_LEVEL > 2) { \
printk("\t" KERN_DEBUG "["THE_MB_USER"] %s: " fmt, \
__func__ , ## args); \
} \
}
#else
#define MB_OPDBG(fmt, args...) do {} while (0)
#define MB_DBG(fmt, args...) do {} while (0)
#define MB_WDBG(fmt, args...) do {} while (0)
#endif
#ifdef CONFIG_WMT_MB_RESERVE_FROM_IO
#include <linux/fb.h>
#define MBIO_FSCREENINFO FBIOGET_FSCREENINFO
#define MBA_MAX_ORDER 20 /* 2^20 pages = 4 GB */
#else
#define MBA_MAX_ORDER (MAX_ORDER - 1) /* 2^10 pages = 4 MB */
#endif
#define MBA_MIN_ORDER (MAX_ORDER - 5) /* 2^6 pages = 256 KB */
#define MBAFLAG_STATIC 0x80000000 /* static, without release if empty */
#define MBFLAG_CACHED 0x00000001 /* MB mapping as cachable */
#define MBFIND_VIRT 0x00000000 /* search virt address */
#define MBFIND_PHYS 0x00000001 /* search phys address */
#define MBUFIND_USER 0x00000001 /* search user address */
#define MBUFIND_VIRT 0x00000002 /* search kernel virt address */
#define MBUFIND_PHYS 0x00000004 /* search kernel phys address */
#define MBUFIND_ALL 0x00000010 /* search all mbu */
#define MBUFIND_CREATOR 0x00000020 /* search creator */
#define MBUFIND_MMAP 0x00000040 /* search mbu which mapping from user */
#define MBUFIND_GETPUT 0x00000080 /* search mbu which came from get/put */
#define MBUFIND_ADDRMASK (MBUFIND_USER|MBUFIND_VIRT|MBUFIND_PHYS)
#define MBPROC_BUFSIZE (16*1024)
#define MBA_SHOW_BUFSIZE (8*1024)
#define MB_SHOW_BUFSIZE (4*1024)
#define MB_COUNT(mb) atomic_read(&(mb)->count)
#define MB_IN_USE(mb) (MB_COUNT(mb)) /* count != 0 */
#define PAGE_KB(a) ((a)*(PAGE_SIZE/1024))
#define MB_COMBIND_MBA(h, t) { \
(h)->pgi.pfn_end = (t)->pgi.pfn_end; \
(h)->pages += (t)->pages; \
(h)->tot_free_pages += (t)->tot_free_pages; \
(h)->max_available_pages += (t)->max_available_pages; \
list_del(&((t)->mba_list)); \
wmt_mbah->nr_mba--; \
kmem_cache_free(wmt_mbah->mba_cachep, t); \
}
#define list_loop list_for_each_entry
struct mb_user;
struct page_info {
unsigned long pfn_start; /* start pfn of this block */
unsigned long pfn_end; /* end pfn of this block */
};
struct mba_host_struct {
/* MB area link list link all MBAs */
struct list_head mba_list;
/* page information of this MBA */
unsigned int nr_mba;
/* total pages of all manager MBAs */
unsigned long tot_pages;
/* total static pages of all manager MBAs */
unsigned long tot_static_pages;
/* total free pages of all manager MBAs */
unsigned long tot_free_pages;
/* max free pages of all manager MBAs */
unsigned long max_available_pages;
/* allocator of MBA,
use slab to prevent memory from fragment. */
struct kmem_cache *mba_cachep;
/* allocator of MB,
use slab to prevent memory from fragment. */
struct kmem_cache *mb_cachep;
/* allocator of mb_user,
use slab to prevent memory from fragment. */
struct kmem_cache *mbu_cachep;
/* allocator of mb_task_info,
use slab to prevent memory from fragment. */
struct kmem_cache *mbti_cachep;
};
struct mb_area_struct {
/* MB area link list link all MBAs */
struct list_head mba_list;
/* link list link to all MBs
belong to this MBA */
struct list_head mb_list;
/* pointer point to dedicated MBAH */
struct mba_host_struct *mbah;
/* flags of MBA */
unsigned int flags;
/* prefech record task id */
pid_t tgid;
/* start physical address of this MBA */
unsigned long phys;
/* start virtual address of this MBA */
void *virt;
/* size of this MB area. Normally,
MAX kernel permitted size */
unsigned long pages;
/* page information of this MBA */
struct page_info pgi;
/* page information of this MBA */
unsigned int nr_mb;
/* cur total free pages of this MBA */
unsigned long tot_free_pages;
/* cur max free pages of this MBA */
unsigned long max_available_pages;
};
struct mb_struct {
/* MB link list link all MBs
in dedicated MBA */
struct list_head mb_list;
/* pointer point to dedicated MBA */
struct mb_area_struct *mba;
/* MB kernel page information */
struct page_info pgi;
/* start physical address of this MB */
unsigned long phys;
/* start virtual address of this MB */
void *virt;
/* allocate size */
unsigned long size;
/* current MB use count,
release until zero */
atomic_t count;
/* flags of MB */
unsigned int flags;
/* point to owner created the mb.
this enlisted in mbu_list */
struct mb_user *creator;
/* use for trace the user of mb */
struct list_head mbu_list;
};
struct mb_user {
/* user link list link all users
(include creator) of belonged MB */
struct list_head mbu_list;
/* the mb to which this user belong */
struct mb_struct *mb;
/* task id to which user belonged,
user space user only */
pid_t tgid;
/* mb_mmap and MBIO_GET : user address
mb_get and mb->creator : physical address */
unsigned long addr;
/* kernel space user: mb size
* user space user: mmap size
* zero: owner - user space allocate but not mapped yet
* not owner -come from get/put */
unsigned long size;
/* user name for recoder */
char the_user[TASK_COMM_LEN+1];
};
struct mb_task_info {
/* task link list link all tasks which use MBDev */
struct list_head mbti_list;
pid_t tgid; /* task pid */
atomic_t count; /* multi open record */
char task_name[TASK_COMM_LEN+1];
struct task_struct *task;
};
#ifdef CONFIG_WMT_MB_SIZE
static int MB_TOTAL_SIZE = CONFIG_WMT_MB_SIZE * 1024;
#else
static int MB_TOTAL_SIZE = 32 * 1024;
#endif
static struct mba_host_struct *wmt_mbah;
static struct mb_task_info wmt_mbti;
const struct file_operations mb_fops;
static unsigned char MBMSG_LEVEL;
static unsigned char MBMAX_ORDER;
static unsigned char MBMIN_ORDER;
static unsigned char USR2PRDT_METHOD;
/* read/write spinlock for multientry protection. */
static spinlock_t mb_do_lock;
static spinlock_t mb_search_lock;
static spinlock_t mb_ioctl_lock;
static spinlock_t mb_task_mm_lock;
static spinlock_t mb_task_lock;
static char show_mb_buffer[MB_SHOW_BUFSIZE];
static char show_mba_buffer[MBA_SHOW_BUFSIZE];
static inline uint64_t checkSum(unsigned long phys, int size, int type)
{
char *d = (type) ? __va(phys) : mb_phys_to_virt(phys);
int i, pageSize, len, offset;
uint64_t sum, cs = 0;
len = size;
offset = (unsigned long)d % PAGE_SIZE;
while (len > 0) {
sum = 0;
pageSize = min((int)(PAGE_SIZE - offset), len);
for (i = 0; i < pageSize; i++)
sum += d[i];
MB_WDBG("[CS] addr %#lx ~ %#lx size %#x cs %#llx\n",
phys, phys + size, size, sum);
MB_WDBG("[CS:DATA] %02x %02x %02x %02x %02x %02x %02x %02x\n",
d[0] & 0xff, d[1] & 0xff, d[2] & 0xff, d[3] & 0xff,
d[4] & 0xff, d[5] & 0xff, d[6] & 0xff, d[7] & 0xff);
d += pageSize;
len -= pageSize;
cs += sum;
}
return cs;
}
unsigned long user_to_phys(unsigned long user)
{
unsigned long par, phys, offset;
offset = user % PAGE_SIZE;
asm volatile(
"mcr p15, 0, %[user], c7, c8, 0\n"
"isb\n"
"mrc p15, 0, %[par], c7, c4, 0\n"
: [par] "=r" (par)
: [user] "r" (user));
phys = (par & PAGE_MASK) + offset;
MB_DBG("%s: user %lx par %lx phys %lx\n",
__func__, user, par, phys);
return (par & 0x1) ? 0 : phys;
}
EXPORT_SYMBOL(user_to_phys);
/* return address is guaranteeed only under page alignment */
void *user_to_virt(unsigned long user)
{
unsigned long phys;
phys = user_to_phys(user);
if (!phys) {
MB_WARN("user 0x%lx to virt fail.\n", user);
return 0;
}
return __va(phys);
}
EXPORT_SYMBOL(user_to_virt);
struct prdt_info {
int index;
int items;
int offset;
int size;
struct prdt_struct *prdt;
struct prdt_struct *last;
};
static int prdt_proc(struct prdt_info *info, unsigned long phys)
{
int len;
if (!info) {
MB_WARN("PRDT process fail. (NULL info)\n");
return -1;
}
/* PRDT has done or no PRDT space */
if (info->size <= 0 ||
(info->last && info->last->EDT))
return 0;
len = PAGE_SIZE - info->offset;
if (len > info->size) /* last page */
len = info->size;
/* Check it could combind with previous one */
if (info->last &&
/* prd size boundary check, MAX 60K */
(info->last->size <= ((1 << 16) - (2 * PAGE_SIZE))) &&
/* page continuity check */
((info->last->addr + info->last->size) == phys))
info->last->size += len;
else { /* create new one */
if (!info->items) {
MB_WARN("PRD table full (ptr %p # %d left %d).\n",
info->last+1, info->index+1, info->size);
if (info->last)
info->last->EDT = 1;
return -1;
}
if (info->last == NULL)
info->last = info->prdt;
else
info->last++;
info->last->addr = phys + info->offset;
info->last->size = len;
info->index++;
info->items--;
info->offset = 0;
}
info->size -= len;
info->last->reserve = 0;
info->last->EDT = (info->size) ? 0 : 1;
MB_WDBG("\t[PRD %3d] %p (0x%x ~ 0x%x, 0x%x, %x) phys 0x%lx offset 0x%x len 0x%x left %x\n",
info->index, info->last, info->last->addr, info->last->addr + info->last->size,
info->last->size, info->last->EDT, phys, info->offset, len, info->size);
return 0;
}
static inline void show_prdt(struct prdt_struct *next)
{
int idx = 1;
while (!next->EDT) {
MB_INFO("PRDT %d-th item: addr %x size %d EDT %d\n",
idx, next->addr, next->size, next->EDT);
idx++;
next++;
}
MB_INFO("PRDT last(%d-th) item: addr %x size %d EDT %d\n",
idx, next->addr, next->size, next->EDT);
}
int user_to_prdt(
unsigned long user,
unsigned int size,
struct prdt_struct *prdt,
unsigned int items)
{
struct prdt_info info = {0};
unsigned long addr;
int idx;
if (!prdt || ((size / PAGE_SIZE) + 2) > items) {
MB_WARN("PRD table space not enough (ptr %p at least %lu but %d).\n",
prdt, (size/PAGE_SIZE)+2, items);
return -EINVAL;
}
MB_DBG("Memory(%#lx,%#x) PRDT(%p,%d)\n", user, size, prdt, items);
info.items = items;
info.offset = user % PAGE_SIZE;
info.size = size;
info.prdt = prdt;
addr = user & PAGE_MASK;
while (info.size > 0) {
unsigned long phys = user_to_phys(addr);
if (!phys) {
MB_WARN("user to prdt fail: unknown addr %lx (%lx, %x)\n",
addr, user, size);
return -EINVAL;
}
prdt_proc(&info, phys);
addr += PAGE_SIZE;
}
/* flush cache */
MB_WDBG("flush PRDT %p, from %lx size %d\n", prdt, user, size);
dmac_flush_range((const void *)user, (const void *)(user + size));
for (idx = 0;; idx++) {
MB_WDBG("PRDT[%d] %p addr %x( ~ %x) size %d EDT %d (%p ~ %p)\n",
idx, &prdt[idx], prdt[idx].addr, prdt[idx].addr + prdt[idx].size, prdt[idx].size,
prdt[idx].EDT, __va(prdt[idx].addr), __va(prdt[idx].addr + prdt[idx].size));
outer_flush_range(prdt[idx].addr,
prdt[idx].addr + prdt[idx].size);
if (prdt[idx].EDT)
break;
}
/* show_prdt(prdt); */
return 0;
}
EXPORT_SYMBOL(user_to_prdt);
int mb_to_prdt(
unsigned long phys,
unsigned int size,
struct prdt_struct *prdt,
unsigned int items)
{
struct prdt_info info = {0};
unsigned long addr;
void *virt = NULL;
int idx;
if (!prdt || ((size / PAGE_SIZE) + 2) > items) {
MB_WARN("PRD table space not enough (ptr %p at least %lu but %d).\n",
prdt, (size/PAGE_SIZE)+2, items);
return -EINVAL;
}
MB_DBG("Memory(%#lx,%#x) PRDT(%p,%d)\n", phys, size, prdt, items);
info.items = items;
info.offset = phys % PAGE_SIZE;
info.size = size;
info.prdt = prdt;
addr = phys & PAGE_MASK;
while (info.size > 0) {
prdt_proc(&info, addr);
addr += PAGE_SIZE;
}
/* flush cache */
MB_WDBG("PRDT %p, from %lx size %d\n", prdt, phys, size);
virt = mb_phys_to_virt(phys);
dmac_flush_range(virt, virt + size);
for (idx = 0;; idx++) {
MB_WDBG("PRDT[%d] addr %x size %d EDT %d\n",
idx, prdt[idx].addr, prdt[idx].size, prdt[idx].EDT);
outer_flush_range(prdt[idx].addr,
prdt[idx].addr + prdt[idx].size);
if (prdt[idx].EDT)
break;
}
/* show_prdt(prdt); */
return 0;
}
EXPORT_SYMBOL(mb_to_prdt);
unsigned int wmt_mmu_table_size(unsigned int size)
{
unsigned int nPte, nPde, need;
if (!size)
return 0;
nPte = (size / PAGE_SIZE) + 2; /* maybe cross page up and down boundary */
nPde = PAGE_ALIGN(nPte * 4) / PAGE_SIZE;
need = PAGE_ALIGN(nPde * 4);
need += nPte * 4;
MB_DBG("PDE %d PTE %d RequestSize %d\n", nPde, nPte, need);
return need;
}
EXPORT_SYMBOL(wmt_mmu_table_size);
int wmt_mmu_table_check(
unsigned int mmuPhys,
unsigned int mmuSize,
unsigned int size)
{
int request;
if (!size) {
MB_WARN("mmu table failure. NULL size\n");
return 0;
}
if (!mmuSize) {
MB_WARN("mmu table failure. NULL mmu size\n");
return 0;
}
if (mmuPhys % PAGE_SIZE) {
MB_WARN("mmu table failure. PDE Table not align\n");
return 0;
}
request = wmt_mmu_table_size(size);
if (mmuSize < request) {
MB_WARN("mmu table failure. size %d < %d (request)\n",
mmuSize, request);
return 0;
}
return 1;
}
EXPORT_SYMBOL(wmt_mmu_table_check);
void wmt_mmu_table_dump(struct mmu_table_info *info)
{
unsigned int size, iPde, iPte, addrOffset;
unsigned int *mmuVirt, *pte = NULL, *pde = NULL;
if (info == 0) {
MB_ERROR("[WMT_MMU_TABLE] Null input pointer\n");
return;
}
mmuVirt = (unsigned int *)mb_phys_to_virt(info->addr);
size = info->size;
iPte = (info->offset >> 12) % 1024;
iPde = (info->offset >> 22) % 1024;
addrOffset = info->offset % PAGE_SIZE;
MB_INFO("MMU (%x): offset pde %x pte %x addr %x\n",
info->offset, iPde, iPte, addrOffset);
pde = mmuVirt;
pte = mb_phys_to_virt(pde[iPde]);
while (size > 0) {
int pageSize = min((unsigned int)(PAGE_SIZE - addrOffset), size);
MB_INFO("PDE(%p/%#x @ %d) -> PTE(%p/%#x @ %d) -> %#x, s %#x # %#x\n",
pde + iPde, info->addr + iPde * sizeof(pde), iPde,
pte + iPte, pde[iPde] + iPte * sizeof(pte), iPte,
pte[iPte] + addrOffset, pageSize, size);
iPte++;
addrOffset = 0;
size -= pageSize;
if (!(iPte % 1024)) {
iPde++;
iPte = 0;
pte = mb_phys_to_virt(pde[iPde]);
}
}
}
EXPORT_SYMBOL(wmt_mmu_table_dump);
/* parameter:
mmuPhys [IN] mmu table phys address
mmuSize [IN] mmu table size
addr [IN] physical address of convert data
size [IN] convert size */
unsigned int wmt_mmu_table_from_phys(
unsigned int mmuPhys,
unsigned int mmuSize,
unsigned int addr,
unsigned int size)
{
unsigned int iPde, iPte, nPte, nPde, virBufAddr;
unsigned int *pte, *pde, *mmuVirt;
int64_t len = size;
void *virt = NULL;
iPde = iPte = nPte = nPde = 0;
if (!wmt_mmu_table_check(mmuPhys, mmuSize, size)) {
MB_WARN("phys %x (size %d) to mmu fail\n", addr, size);
return 0xFFFFFFFF;
}
virt = mb_phys_to_virt(addr);
virBufAddr = addr % PAGE_SIZE;
nPte = size / PAGE_SIZE + 2;
nPde = PAGE_ALIGN(nPte * 4) / PAGE_SIZE;
pde = mmuVirt = mb_phys_to_virt(mmuPhys);
pte = pde + nPde * 1024;
*pde = mmuPhys + nPde * PAGE_SIZE;
MB_DBG("[%s] pde %p/%x # %d pte %p/%x # %d\n",
__func__, pde, mmuPhys, nPde, pte, *pde, nPte);
while (len > 0) {
pte[iPte] = addr & PAGE_MASK;
iPte++;
if (!(iPte % 1024)) {
pde[iPte + 1] = pde[iPte] + PAGE_SIZE;
iPte++;
}
addr += PAGE_SIZE;
len -= PAGE_SIZE;
}
dmac_flush_range(virt, virt + size);
outer_flush_range(addr, addr + size);
return virBufAddr;
}
EXPORT_SYMBOL(wmt_mmu_table_from_phys);
struct mmu_info {
unsigned int size;
unsigned int iPte;
unsigned int *pte;
unsigned int *pde;
};
static inline int wmt_mmu_table_proc(struct mmu_info *info, unsigned long phys)
{
int pageSize, offset;
if (!info) {
MB_WARN("WMT MMU process fail. (NULL info)\n");
return -1;
}
if (info->size <= 0) {
MB_WARN("WMT MMU process fail. (size %d)\n", info->size);
return -1;
}
offset = phys % PAGE_SIZE;
pageSize = min((unsigned int)(PAGE_SIZE - offset), info->size);
*info->pte = phys & PAGE_MASK;
info->size -= pageSize;
MB_WDBG("\t[PTE] PDE[%d](%p)=%x PTE[%d](%p)=%x size %#x left %#x\n",
info->iPte/1024, info->pde, *info->pde, info->iPte,
info->pte, *info->pte, pageSize, info->size);
info->pte++;
info->iPte++;
if (!(info->iPte % 1024)) {
*info->pde = mb_virt_to_phys(info->pte);
info->pde++;
}
return 0;
}
/* parameter:
mmuPhys [IN] mmu table phys address
mmuSize [IN] mmu table size
addr [IN] physical address of convert data
size [IN] convert size */
unsigned int wmt_mmu_table_from_user(
unsigned int mmuPhys,
unsigned int mmuSize,
unsigned int user,
unsigned int size)
{
#ifndef __KERNEL__
return wmt_mmu_table_from_phys(mmuPhys, mmuSize, user, size);
#else
struct mmu_info info = {0};
unsigned int nPte, nPde;
unsigned long addr = user;
int offset = user % PAGE_SIZE;
if (!wmt_mmu_table_check(mmuPhys, mmuSize, size)) {
printk(KERN_WARNING "user %x (size %d) to mmu fail\n",
user, size);
return 0xFFFFFFFF;
}
MB_DBG("[%s] Memory(%#x,%#x) MMU(%#x,%d)\n",
__func__, user, size, mmuPhys, mmuSize);
nPte = size / PAGE_SIZE + 2;
nPde = PAGE_ALIGN(nPte * 4) / PAGE_SIZE;
info.size = size;
info.pde = mb_phys_to_virt(mmuPhys);
info.pte = info.pde + nPde * 1024;
*info.pde = mmuPhys + nPde * PAGE_SIZE;
dmac_flush_range((const void *)user, (const void *)(user + size));
while (info.size > 0) {
unsigned long phys = user_to_phys(addr);
int pageSize = min((unsigned int)(PAGE_SIZE - offset), info.size);
if (!phys) {
MB_WARN("user to mmu fail: unknown addr %lx (%x # %x)\n",
addr, user, size);
return -EINVAL;
}
outer_flush_range(phys, phys + pageSize);
wmt_mmu_table_proc(&info, phys);
addr += pageSize;
offset = 0;
}
return user % PAGE_SIZE;
#endif
}
EXPORT_SYMBOL(wmt_mmu_table_from_user);
/* parameter:
addr [IN] start address (user or phys depends on addr_type)
size [IN] size of memory
addrType [IN] address type (0: phys 1: user)
info [out] pointer of mmu table info */
unsigned int wmt_mmu_table_create(
unsigned int addr,
unsigned int size,
unsigned int addrType,
struct mmu_table_info *info)
{
int i, nPde, nPte, *pde, mmuSize;
unsigned int *mmuAddr = NULL;
if (!addr || !size || !info || addrType > 1) {
MB_ERROR("[WMT_MMU_TABLE] invalid args:\n");
MB_ERROR("\t addr %x # %x type %x info %p\n",
addr, size, addrType, info);
return -EINVAL;
}
mmuSize = wmt_mmu_table_size(size);
info->addr = mb_alloc(mmuSize);
info->size = size;
MB_DBG("[%s] mmu table (%#x # %#x) addr %#x size %#x type %d\n",
__func__, info->addr, mmuSize, addr, size, addrType);
if (!info->addr) {
MB_ERROR("[%s] fail. Out of MB (%d)\n", __func__, mmuSize);
return -ENOMEM;
}
pde = mmuAddr = (unsigned int *)mb_phys_to_virt((unsigned long)info->addr);
memset(mmuAddr, 0x0, mmuSize);
nPte = (size / PAGE_SIZE) + 2; /* maybe cross page up and down boundary */
nPde = PAGE_ALIGN(nPte * 4) / PAGE_SIZE;
for (i = 1; i <= nPde; i++, pde++)
*pde = info->addr + i * PAGE_SIZE;
info->offset = (addrType) ?
wmt_mmu_table_from_user(info->addr, mmuSize, addr, size) :
wmt_mmu_table_from_phys(info->addr, mmuSize, addr, size);
if (info->offset == 0xFFFFFFFF) {
MB_ERROR("[WMT_MMU_TABLE] create fail:");
MB_ERROR("\ttype %x addr %x # %x mmuAddr %p/%x # %d\n",
addrType, addr, size, mmuAddr, info->addr, info->size);
mb_free(info->addr);
return -EFAULT;
}
MB_DBG("[%s] success! (addr %#x size %#x offset %#x)\n",
__func__, info->addr, info->size, info->offset);
return 0;
}
EXPORT_SYMBOL(wmt_mmu_table_create);
unsigned int wmt_mmu_table_destroy(struct mmu_table_info *info)
{
if (!info) {
MB_ERROR("[WMT_MMU_TABLE] destroy fail. NULL mmu table info");
return -EINVAL;
}
MB_DBG("[%s] success! addr %#x\n", __func__, info->addr);
mb_free(info->addr);
return 0;
}
EXPORT_SYMBOL(wmt_mmu_table_destroy);
static int mb_show_mb(
struct mb_struct *mb,
char *msg,
char *str,
int size
)
{
struct mb_user *mbu;
char *p;
if (!str && MBMSG_LEVEL <= 1)
return 0;
if (!mb)
return 0;
memset(show_mb_buffer, 0x0, MB_SHOW_BUFSIZE);
p = show_mb_buffer;
p += sprintf(p, "%s%s[MB,%p] %p %08lx %4ldKB [%4lx,%4lx] %3x\n",
msg ? msg : "", msg ? " " : "", mb, mb->virt, mb->phys,
mb->size/1024, mb->pgi.pfn_start, mb->pgi.pfn_end,
MB_COUNT(mb));
if (!list_empty(&mb->mbu_list)) {
list_loop(mbu, &mb->mbu_list, mbu_list) {
char tmp[256], *p1 = tmp;
int expectLen = (int)(p - show_mb_buffer);
p1 += sprintf(tmp, " [%s%s,%p] %08lx ",
(mbu == mb->creator) ? "O" : " ",
(mbu->tgid == MB_DEF_TGID) ? "K" : "U",
mbu, mbu->addr);
p1 += sprintf(p1, "# %4ldKB By %s(%d)\n",
mbu->size/1024, mbu->the_user, mbu->tgid);
expectLen += strlen(tmp) + 1;
if (expectLen > MB_SHOW_BUFSIZE) {
p += sprintf(p, "\n\n .......\n");
break;
}
p += sprintf(p, "%s", tmp);
}
}
if (str) {
if (strlen(show_mb_buffer) < size)
size = strlen(show_mb_buffer);
strncpy(str, show_mb_buffer, size);
} else {
size = strlen(show_mb_buffer);
MB_DBG("%s", show_mb_buffer);
}
return size;
}
static int mb_show_mba(
struct mb_area_struct *mba,
char *msg,
char *str,
int size,
int follow
)
{
struct mb_struct *mb;
char *p;
int idx = 1;
if (!str && MBMSG_LEVEL <= 1)
return 0;
if (!mba)
return 0;
memset(show_mba_buffer, 0x0, MBA_SHOW_BUFSIZE);
p = show_mba_buffer;
if (msg)
p += sprintf(p, "%s ", msg);
p += sprintf(p, "[%p] %p %08lx %3ldMB [%5lx,%5lx] ",
mba, mba->virt, mba->phys, PAGE_KB(mba->pages)/1024,
mba->pgi.pfn_start, mba->pgi.pfn_end);
p += sprintf(p, "%3x %6ldKB/%6ldKB %s\n", mba->nr_mb,
PAGE_KB(mba->max_available_pages),
PAGE_KB(mba->tot_free_pages),
(mba->flags | MBAFLAG_STATIC) ? "S" : "D");
if (follow) {
p += sprintf(p, " index - [MemBlock] VirtAddr PhysAddr");
p += sprintf(p, " size [ zs, ze] cnt\n");
list_loop(mb, &mba->mb_list, mb_list) {
if ((MBA_SHOW_BUFSIZE - (p - show_mba_buffer)) < 22) {
p += sprintf(p, "\n\n more ...\n");
break;
}
p += sprintf(p, " %5d - ", idx++);
/* -2 is for \n and zero */
p += mb_show_mb(mb, NULL, p,
MBA_SHOW_BUFSIZE - (p - show_mba_buffer) - 2);
}
}
p += sprintf(p, "\n");
if (str) {
if (strlen(show_mba_buffer) < size)
size = strlen(show_mba_buffer);
strncpy(str, show_mba_buffer, size);
} else {
size = strlen(show_mba_buffer);
MB_DBG("%s", show_mba_buffer);
}
return size;
}
/* return physical address */
#ifdef CONFIG_WMT_MB_RESERVE_FROM_IO
/* mb_do_lock locked */
static void *mb_alloc_pages(unsigned long *phys, unsigned long *nr_pages)
{
unsigned int order = get_order(num_physpages << PAGE_SHIFT);
unsigned int size = *nr_pages << PAGE_SHIFT;
unsigned long addr = ((1 << order) - *nr_pages) << PAGE_SHIFT;
void *virt = NULL;
if (!nr_pages || !phys) {
MB_WARN("mb_alloc_pages fail. unknown argument\n");
return NULL;
}
*phys = 0;
if (*nr_pages == 0) {
MB_WARN("mb_alloc_pages zero size\n");
return NULL;
}
if (!request_mem_region(addr, size, "memblock")) {
MB_WARN("request memory region fail. addr %#lx size %d(KB).\n",
addr, size/1024);
return NULL;
}
virt = ioremap(addr, size);
if (!virt) {
MB_WARN("cannot ioremap memory. addr %#lx size %d(KB).\n",
addr, size/1024);
release_mem_region(addr, size);
return NULL;
}
*phys = addr;
MB_DBG("allocate mem region. addr V %p P %#lx size %d(KB)\n",
virt, addr, size/1024);
return virt;
}
/* mb_do_lock locked */
static void mb_free_pages(void *virt, unsigned long phys, unsigned int nr_pages)
{
iounmap(virt);
MB_DBG("release mem region. addr V %p P %#lx size %d(KB)\n",
virt, phys, 4*nr_pages);
return release_mem_region(phys, nr_pages << PAGE_SHIFT);
}
#else
/* mb_do_lock locked */
static void *mb_alloc_pages(unsigned long *phys, unsigned long *nr_pages)
{
unsigned int i, order;
unsigned long virt = 0;
struct zone *zone;
if (!nr_pages || !phys) {
MB_WARN("mb_alloc_pages fail. unknown argument\n");
return NULL;
}
*phys = 0;
if (*nr_pages == 0) {
MB_WARN("mb_alloc_pages zero size\n");
return NULL;
}
order = get_order(*nr_pages * PAGE_SIZE);
order = max(order, (unsigned int)(MBA_MIN_ORDER));
MB_DBG(" %ld/%d pages, order %d\n", *nr_pages, 1 << order, order);
if (order > MBMAX_ORDER) {
MB_WARN("mb_alloc_mba fail. page out of size, %ld/%d/%d\n",
*nr_pages, (1<<order), (1<<MBMAX_ORDER));
return NULL;
}
zone = (first_online_pgdat())->node_zones;
for (i = order; i < MAX_ORDER; i++) {
if (zone->free_area[i].nr_free) {
virt = __get_free_pages(GFP_ATOMIC | GFP_DMA, order);
break;
}
}
if (virt) {
*nr_pages = 1 << order;
*phys = __pa(virt);
for (i = 0; i < *nr_pages; i++)
SetPageReserved(virt_to_page(virt + i * PAGE_SIZE));
MB_DBG("allocate mem region. addr V %#lx P %#lx size %ld(KB)\n",
virt, *phys, PAGE_KB(*nr_pages));
} else {
struct free_area *fa;
char msg[256], *p = msg;
MB_WARN("__get_free_pages fail! (pages: %d free %lu)\n",
1 << order, (unsigned long)nr_free_pages());
zone = (first_online_pgdat())->node_zones;
fa = zone->free_area;
p += sprintf(msg, "DMA ZONE:");
for (i = 0; i < MAX_ORDER; i++)
p += sprintf(p, " %ld*%dKB", fa[i].nr_free, 4 << i);
MB_WARN("%s= %ldkB\n", msg, PAGE_KB(nr_free_pages()));
}
return (void *)virt;
}
/* mb_do_lock locked */
static void mb_free_pages(void *virt, unsigned long phys, unsigned int nr_pages)
{
unsigned int index;
if (!phys || !nr_pages) {
MB_WARN("mb_free_pages unknow addr V %p P %#lx size %d pages\n",
virt, phys, nr_pages);
return;
}
for (index = 0; index < nr_pages; index++) {
unsigned long addr = (unsigned long)virt + index * PAGE_SIZE;
ClearPageReserved(virt_to_page(addr));
}
free_pages((unsigned long)virt, get_order(nr_pages * PAGE_SIZE));
}
#endif
/* mb_do_lock locked */
static struct mb_area_struct *mb_alloc_mba(unsigned int pages)
{
struct mba_host_struct *mbah = wmt_mbah;
struct mb_area_struct *mba;
if (!mbah || !pages) {
MB_WARN("mb_alloc_mba fail. mbah %p, pages %d\n", mbah, pages);
return NULL;
}
mba = kmem_cache_alloc(mbah->mba_cachep, GFP_ATOMIC);
if (!mba) {
MB_WARN("mb_alloc_mba fail. out of memory\n");
return NULL;
}
memset(mba, 0x0, sizeof(struct mb_area_struct));
mba->pages = pages;
mba->virt = mb_alloc_pages(&mba->phys, &mba->pages);
if (!mba->virt) {
MB_WARN("mb_alloc_mba fail. no available space\n");
kmem_cache_free(mbah->mba_cachep, mba);
return NULL;
}
/* initialization */
INIT_LIST_HEAD(&mba->mb_list);
INIT_LIST_HEAD(&mba->mba_list);
mba->mbah = mbah;
mba->tot_free_pages = mba->max_available_pages = mba->pages;
mba->pgi.pfn_start = mba->phys >> PAGE_SHIFT;
mba->pgi.pfn_end = mba->pgi.pfn_start + mba->pages;
list_add_tail(&mba->mba_list, &mbah->mba_list);
/* update MBA host */
mbah->nr_mba++;
mbah->tot_pages += mba->pages;
mbah->tot_free_pages += mba->tot_free_pages;
if (mbah->max_available_pages < mba->max_available_pages)
mbah->max_available_pages = mba->max_available_pages;
mb_show_mba(mba, "alloc", NULL, 0, 0);
return mba;
}
/* mb_do_lock locked */
static int mb_free_mba(struct mb_area_struct *mba)
{
struct mba_host_struct *mbah;
struct mb_area_struct *entry;
if (!mba || !mba->mbah || mba->nr_mb) {
MB_WARN("mb_free_mba fail. unknow arg.(%p,%p,%x)\n",
mba, mba ? mba->mbah : NULL, mba ? mba->nr_mb : 0);
return -EFAULT;
}
mbah = mba->mbah;
list_loop(entry, &mbah->mba_list, mba_list) {
if (entry == mba)
break;
}
if (entry != mba) {
MB_WARN("mb_free_mba fail. unknow MBA %p\n", mba);
return -EFAULT;
}
if (mba->flags & MBAFLAG_STATIC)
return 0;
mb_show_mba(mba, "ReleaseMBA", NULL, 0, 0);
/* free mba */
list_del(&mba->mba_list);
mb_free_pages(mba->virt, mba->phys, mba->pages);
mbah->nr_mba--;
mbah->tot_free_pages -= mba->tot_free_pages;
mbah->tot_pages -= mba->pages;
kmem_cache_free(mbah->mba_cachep, mba);
mba = NULL;
/* update max mb size */
mbah->max_available_pages = 0;
list_loop(entry, &mbah->mba_list, mba_list) {
if (mbah->max_available_pages < entry->max_available_pages)
mbah->max_available_pages = entry->max_available_pages;
}
return 0;
}
/* mb_do_lock locked */
static struct mb_struct *mb_alloc_mb(
struct mb_area_struct *mba,
unsigned long size)
{
struct mba_host_struct *mbah;
struct mb_struct *mb, *entry;
struct list_head *next;
unsigned long pages, zs, ze;
if (!mba || !mba->mbah || !size) {
MB_WARN("mb_alloc_mb fail. unknow arg.(%p,%p,%lx)\n",
mba, mba ? mba->mbah : NULL, size);
return NULL;
}
mbah = mba->mbah;
size = PAGE_ALIGN(size);
pages = size >> PAGE_SHIFT;
/* mb free size is not enough */
if (mba->max_available_pages < pages) {
MB_WARN("mb_alloc_mb fail. no space in MBA (%lx<%lx)\n",
pages, mba->max_available_pages);
return NULL;
}
/* search available zone
zone start from mba start */
ze = zs = mba->pgi.pfn_start;
next = &mba->mb_list;
list_loop(entry, &mba->mb_list, mb_list) {
next = &entry->mb_list;
ze = entry->pgi.pfn_start;
if ((ze - zs) >= pages)
break;
zs = entry->pgi.pfn_end;
}
if (zs >= ze) {
next = &mba->mb_list;
ze = mba->pgi.pfn_end;
}
/* impossible, something wrong */
if ((ze - zs) < pages) {
MB_WARN("something wrong in MBA %p when allocate MB.\n", mba);
return NULL;
}
MB_DBG("Zone finding start %lx end %lx size %lx for size %lx\n",
zs, ze, ze - zs, pages);
mb = kmem_cache_alloc(mbah->mb_cachep, GFP_ATOMIC);
if (!mb) {
MB_WARN("mb_alloc_mb mb_cachep out of memory\n");
return NULL;
}
memset(mb, 0x0, sizeof(struct mb_struct));
INIT_LIST_HEAD(&mb->mb_list);
INIT_LIST_HEAD(&mb->mbu_list);
mb->mba = mba;
mb->pgi.pfn_start = zs;
mb->pgi.pfn_end = mb->pgi.pfn_start + pages;
mb->size = size;
mb->phys = mba->phys + ((zs - mba->pgi.pfn_start) << PAGE_SHIFT);
mb->virt = mba->virt + (mb->phys - mba->phys);
list_add_tail(&mb->mb_list, next);
mba->nr_mb++;
mba->tot_free_pages -= pages;
mbah->tot_free_pages -= pages;
/* update mba */
zs = mba->pgi.pfn_start;
mba->max_available_pages = 0;
list_loop(entry, &mba->mb_list, mb_list) {
mba->max_available_pages =
max(entry->pgi.pfn_start - zs, mba->max_available_pages);
zs = entry->pgi.pfn_end;
}
mba->max_available_pages =
max(mba->pgi.pfn_end - zs, mba->max_available_pages);
/* update mbah */
mbah->max_available_pages = 0;
list_loop(mba, &mbah->mba_list, mba_list) {
if (mbah->max_available_pages < mba->max_available_pages)
mbah->max_available_pages = mba->max_available_pages;
}
mb_show_mb(mb, "alloc", NULL, 0);
return mb;
}
/* mb_do_lock locked */
static int mb_free_mb(struct mb_struct *mb)
{
unsigned long zs, nr_pages;
struct mba_host_struct *mbah;
struct mb_area_struct *mba;
struct mb_struct *entry;
if (!mb) {
MB_WARN("mb_free_mb fail. unknow MB %p.\n", mb);
return -EFAULT;
}
if (MB_IN_USE(mb)) {
MB_WARN("mb_free_mb fail. invalid arg.(%p,%x,%lx,%ldKB)\n",
mb, MB_COUNT(mb), mb->phys, mb->size/1024);
return -EINVAL;
}
mba = mb->mba;
if (!mba || !mba->mbah) {
MB_WARN("mb_free_mb fail. unknow para.(%p,%p)\n",
mba, mba ? mba->mbah : NULL);
return -EFAULT;
}
list_loop(entry, &mba->mb_list, mb_list) {
if (entry == mb)
break;
}
if (entry != mb) {
MB_WARN("mb_free_mb fail. unknow MB %p\n", mb);
return -EFAULT;
}
mbah = mba->mbah;
mb_show_mb(mb, "Retrieve unused MB", NULL, 0);
/* free mb */
list_del(&mb->mb_list);
kmem_cache_free(mbah->mb_cachep, mb);
mba->nr_mb--;
mb = NULL;
/* unused mba, release it */
if (!mba->nr_mb && !(mba->flags & MBAFLAG_STATIC))
return mb_free_mba(mba);
/* update max mb size and free mb size */
/* reduce old one and then increase new one */
mbah->tot_free_pages -= mba->tot_free_pages;
zs = mba->pgi.pfn_start;
mba->tot_free_pages = mba->max_available_pages = nr_pages = 0;
list_loop(entry, &mba->mb_list, mb_list) {
nr_pages = max(entry->pgi.pfn_start - zs, nr_pages);
mba->tot_free_pages += entry->pgi.pfn_start - zs;
zs = entry->pgi.pfn_end;
}
mba->tot_free_pages += mba->pgi.pfn_end - zs;
mba->max_available_pages = max(mba->pgi.pfn_end - zs, nr_pages);
mbah->tot_free_pages += mba->tot_free_pages; /* add new one */
mbah->max_available_pages =
max(mbah->max_available_pages, mba->max_available_pages);
return 0;
}
/* type 0 - virtual address
* 1 - physical address */
static struct mb_struct *mb_find_mb(void *addr, int type)
{
struct mba_host_struct *mbah = wmt_mbah;
struct mb_area_struct *mba;
struct mb_struct *mb;
unsigned long flags;
if (!addr || (type != MBFIND_PHYS && type != MBFIND_VIRT)) {
MB_WARN("mb_find_mb fail. unknow type %d addr %p\n",
type, addr);
return NULL;
}
MB_DBG("IN, type %x addr %p\n", type, addr);
spin_lock_irqsave(&mb_search_lock, flags);
list_loop(mba, &mbah->mba_list, mba_list) {
void *eaddr, *saddr = (void *)(mba->phys);
if (type != MBFIND_PHYS)
saddr = mba->virt;
eaddr = saddr + mba->pages*PAGE_SIZE;
if (addr < saddr || addr > eaddr)
continue; /* address out of mba range */
list_loop(mb, &mba->mb_list, mb_list) {
void *mbaddr = (void *)(mb->phys);
if (type != MBFIND_PHYS)
mbaddr = mb->virt;
if (addr >= mbaddr && addr < (mbaddr + mb->size)) {
spin_unlock_irqrestore(&mb_search_lock, flags);
MB_DBG("OUT, va %p pa %lx s %ld(KB) cnt %d\n",
mb->virt, mb->phys, mb->size/1024,
MB_COUNT(mb));
return mb;
}
}
}
spin_unlock_irqrestore(&mb_search_lock, flags);
MB_DBG("OUT, NULL mb\n");
return NULL;
}
/*
* addr - search address
* tgid - task pid
* st - search type (MBUFIND_XXX)
* MBUFIND_USER: user address
* MBUFIND_VIRT: kernel virt address
* MBUFIND_PHYS: kernel phys address
* MBUFIND_ALL:
* 1. mbu->tgid == tgid
* 2. tgid == MB_DEF_TGID, address and
* one of MBUFIND_VIRT and MBUFIND_PHYS must be set
* tgid != MB_DEF_TGID, address and MBUFIND_USER must be set
* MBUFIND_CREATOR:
* 1. mbu->tgid == tgid
* 2. tgid == MB_DEF_TGID, address and
* one of MBUFIND_VIRT and MBUFIND_PHYS must be set
* tgid != MB_DEF_TGID, address and MBUFIND_USER must be set
* 3. mbu == mb->creator
* 4. mbu->size == mb->size
* MBUFIND_GETPUT:
* 1. mbu->tgid == tgid
* 2. tgid == MB_DEF_TGID, address and
* MBUFIND_PHYS must be set (mbu->addr == mb->phys)
* tgid != MB_DEF_TGID, address and
* MBUFIND_USER must be set (mbu->addr == user address)
* 3. mbu->size == 0
* MBUFIND_MMAP:
* 1. mbu->tgid == tgid
* 2. address and MBUFIND_USER must be set
* 3. mbu->size != 0
*/
static struct mb_user *mb_find_mbu(void *addr, pid_t tgid, int st)
{
struct mba_host_struct *mbah = wmt_mbah;
struct mb_area_struct *mba = NULL;
struct mb_struct *mb = NULL;
struct mb_user *mbu = NULL;
unsigned long flags;
if (!addr || !(st & MBUFIND_ADDRMASK)) {
MB_WARN("mb_find_mbu fail. unknow addr %p\n", addr);
return NULL;
}
MB_DBG("IN, addr %p search type %x TGID %x CurTask %s\n",
addr, st, tgid, current->comm);
spin_lock_irqsave(&mb_search_lock, flags);
list_loop(mba, &mbah->mba_list, mba_list) {
if (st & (MBUFIND_VIRT | MBUFIND_PHYS)) {
void *eaddr, *saddr = (void *)mba->phys;
if (st & MBUFIND_VIRT)
saddr = mba->virt;
eaddr = saddr + mba->pages*PAGE_SIZE;
if (addr < saddr || addr > eaddr)
continue; /* address out of mba range */
}
list_loop(mb, &mba->mb_list, mb_list) {
if ((st & MBUFIND_PHYS) && (addr != (void *)mb->phys))
continue; /* physical address not match */
if ((st & MBUFIND_VIRT) && (addr != mb->virt))
continue; /* virtual address not match */
list_loop(mbu, &mb->mbu_list, mbu_list) {
if (mbu->tgid != tgid && tgid != MB_DEF_TGID)
continue; /* tgid not match */
if ((st & MBUFIND_USER) &&
(addr != (void *)mbu->addr))
continue; /* user address not match */
if (st & MBUFIND_ALL)
goto leave; /* found */
if (st & MBUFIND_CREATOR) {
mbu = mb->creator;
goto leave; /* found */
}
/* found (if mmap, mbu->size has map size) */
if (st & MBUFIND_MMAP && mbu->size)
goto leave;
/* found (if get/put, mbu->size should be 0) */
if (st & MBUFIND_GETPUT &&
mbu->addr && !mbu->size)
goto leave;
}
}
}
mbu = NULL;
leave:
if (mbu == NULL)
MB_DBG("OUT, NULL mbu\n");
if (mbu)
MB_DBG("OUT, mbu %p (%p TGID %x a %#lx s %ld(KB) by %s)\n",
mbu, mbu->mb, mbu->tgid, mbu->addr,
mbu->size/1024, mbu->the_user);
spin_unlock_irqrestore(&mb_search_lock, flags);
return mbu;
}
static void mb_update_mbah(void)
{
unsigned long mbah_free = 0, mbah_max = 0;
struct mba_host_struct *mbah = wmt_mbah;
struct mb_area_struct *mba = NULL;
struct mb_struct *mb = NULL;
if (!mbah) {
MB_WARN("mb_update_mbah fail. unknow mbah\n");
return;
}
list_loop(mba, &mbah->mba_list, mba_list) {
unsigned long mba_free = 0, mba_max = 0, nr_pages = 0;
unsigned long zs = mba->pgi.pfn_start; /* zone start */
list_loop(mb, &mba->mb_list, mb_list) {
nr_pages = max(mb->pgi.pfn_start - zs, nr_pages);
mba_free += mb->pgi.pfn_start - zs;
zs = mb->pgi.pfn_end;
}
mba_free += mba->pgi.pfn_end - zs;
mbah_free += mba_free;
mba_max = max(mba->pgi.pfn_end - zs, nr_pages);
mbah_max = max(mbah_max, mba_max);
}
mbah->tot_free_pages = mbah_free;
mbah->max_available_pages = mbah_max;
return;
}
/* called from kernel only, mb_do_lock isn't needed */
void *mb_do_phys_to_virt(unsigned long phys, pid_t tgid, char *name)
{
struct mb_struct *mb = NULL;
void *virt = NULL;
MB_DBG("IN, Phys %lx TGID %x NAME %s\n", phys, tgid, name);
mb = mb_find_mb((void *)phys, MBFIND_PHYS);
if (mb)
virt = mb->virt + (phys - mb->phys);
else {
virt = __va(phys);
MB_WARN("%s do phys to virt fail. addr %lx not found\n",
name, phys);
}
MB_DBG("OUT, Virt %p\n", virt);
return virt;
}
EXPORT_SYMBOL(mb_do_phys_to_virt);
/* called from kernel only, mb_do_lock isn't needed */
unsigned long mb_do_virt_to_phys(void *virt, pid_t tgid, char *name)
{
struct mb_struct *mb = NULL;
unsigned long phys = 0x0;
MB_DBG("IN, Virt %p TGID %x NAME %s\n", virt, tgid, name);
mb = mb_find_mb(virt, MBFIND_VIRT);
if (mb)
phys = mb->phys + (virt - mb->virt);
else {
phys = __pa(virt);
MB_WARN("%s do virt to phys fail. addr %p not found\n",
name, virt);
}
MB_DBG("OUT, Phys %lx\n", phys);
return phys;
}
EXPORT_SYMBOL(mb_do_virt_to_phys);
unsigned long mb_do_user_to_phys(unsigned long user, pid_t tgid, char *name)
{
unsigned long flags, phys = 0;
struct mb_user *mbu = NULL;
MB_DBG("IN, usr %lx TGID %x name %s\n", user, tgid, name);
spin_lock_irqsave(&mb_do_lock, flags);
mbu = mb_find_mbu((void *)user, tgid, MBUFIND_USER|MBUFIND_ALL);
if (!mbu) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("%s do user to phys. unknow addr %lx\n", name, user);
return 0;
}
if (!mbu->mb)
MB_WARN("%s do user to phys fail. unknow block (addr %lx)\n",
name, user);
else
phys = mbu->mb->phys;
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_DBG("OUT, Phys %#lx\n", phys);
return phys;
}
EXPORT_SYMBOL(mb_do_user_to_phys);
unsigned long mb_do_user_to_virt(unsigned long user, pid_t tgid, char *name)
{
struct mb_user *mbu = NULL;
unsigned long flags;
void *virt = NULL;
MB_DBG("IN, usr %lx TGID %x name %s\n", user, tgid, name);
spin_lock_irqsave(&mb_do_lock, flags);
mbu = mb_find_mbu((void *)user, tgid, MBUFIND_USER|MBUFIND_ALL);
if (!mbu) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("%s user to virt fail. unknow addr %lx\n", name, user);
return 0;
}
if (!mbu->mb)
MB_WARN("%s user to virt fail. unknow block (addr %lx)\n",
name, user);
else
virt = mbu->mb->virt;
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_DBG("OUT, Virt %p\n", virt);
return (unsigned long)virt;
}
EXPORT_SYMBOL(mb_do_user_to_virt);
/* physical address */
int mb_do_counter(unsigned long addr, char *name)
{
struct mb_struct *mb;
unsigned long flags;
int counter = -1;
MB_DBG("IN, addr %#lx name %s\n", addr, name);
if (!addr || !wmt_mbah) {
MB_WARN("%s do counter fail. invalid args %p/%lx\n",
name, wmt_mbah, addr);
return -EINVAL;
}
spin_lock_irqsave(&mb_do_lock, flags);
mb = mb_find_mb((void *)addr, MBFIND_PHYS);
counter = (mb) ? MB_COUNT(mb) : -1;
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_DBG("OUT, addr %#lx count %d\n", addr, counter);
return counter;
}
EXPORT_SYMBOL(mb_do_counter);
unsigned long mb_do_alloc(unsigned long size, pid_t tgid, char *name)
{
struct mb_area_struct *entry, *mba = NULL;
struct mb_struct *mb = NULL;
struct mb_user *mbu = NULL;
unsigned long flags, addr;
unsigned int pages;
size_t ns; /* name size */
if (!name) {
MB_WARN("mb_alloc fail. null user tgid %x\n", tgid);
return 0;
}
size = PAGE_ALIGN(size);
pages = size >> PAGE_SHIFT;
if (!pages) {
MB_WARN("%s alloc fail. unavailable size %x(KB)\n",
name, 4*pages);
return 0;
}
mbu = kmem_cache_alloc(wmt_mbah->mbu_cachep, GFP_ATOMIC);
if (!mbu) {
MB_WARN("%s alloc fail. mbu_cachep out of memory.\n", name);
return 0;
}
memset(mbu, 0x0, sizeof(struct mb_user));
MB_DBG("IN, TGID %x size %ld(KB) name %s\n", tgid, size/1024, name);
spin_lock_irqsave(&mb_do_lock, flags);
if (pages > wmt_mbah->max_available_pages) {
#ifdef CONFIG_WMT_MB_DYNAMIC_ALLOCATE_SUPPORT
mba = mb_alloc_mba(pages);
if (mba == NULL)
MB_WARN("%s alloc %u page fail. [MBA %lu/%lu/%lu]\n",
name, pages, wmt_mbah->max_available_pages,
wmt_mbah->tot_free_pages, wmt_mbah->tot_pages);
#else
MB_DBG("DYNAMIC ALLOCATED(%u) unsuported. [MBA %lu/%lu/%lu]\n",
pages, wmt_mbah->max_available_pages,
wmt_mbah->tot_free_pages, wmt_mbah->tot_pages);
goto error;
#endif
} else {
list_loop(entry, &wmt_mbah->mba_list, mba_list) {
if (entry->max_available_pages >= pages) {
mba = entry;
break;
}
}
}
if (!mba) {
MB_WARN("%s alloc fail. dedicated MBA not found\n", name);
goto error;
}
mb = mb_alloc_mb(mba, size);
if (mb == NULL) {
MB_WARN("%s alloc fail. create MB\n", name);
goto error;
}
INIT_LIST_HEAD(&mbu->mbu_list);
mbu->mb = mb;
mbu->tgid = tgid;
mbu->size = mb->size;
/* mbu->addr = 0; // address of creator is 0 */
ns = strlen(name);
if (ns > TASK_COMM_LEN)
ns = TASK_COMM_LEN;
strncpy(mbu->the_user, name, ns);
mbu->the_user[ns] = 0;
list_add_tail(&mbu->mbu_list, &mb->mbu_list);
atomic_inc(&mb->count);
mb->creator = mbu;
addr = mb->phys;
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_DBG("OUT, Addr %lx TGID %x size %ld(KB) name %s\n",
addr, tgid, size/1024, name);
return addr;
error:
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
spin_unlock_irqrestore(&mb_do_lock, flags);
return 0;
}
EXPORT_SYMBOL(mb_do_alloc);
/* physical address */
int mb_do_free(unsigned long addr, pid_t tgid, char *name)
{
struct mb_struct *mb;
struct mb_user *mbu;
unsigned long flags;
size_t ns; /* name size */
int ret;
if (!addr || !name) {
MB_WARN("mb_free fail. invalid args %lx/%s\n",
addr, name);
return -EINVAL;
}
MB_DBG("IN, TGID %x addr %#lx name %s\n", tgid, addr, name);
spin_lock_irqsave(&mb_do_lock, flags);
mbu = mb_find_mbu((void *)addr, tgid, MBUFIND_PHYS|MBUFIND_CREATOR);
if (!mbu || !mbu->mb) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("%s free fail. unknow addr %lx\n", name, addr);
return -EINVAL;
}
ns = strlen(name);
if (ns > TASK_COMM_LEN)
ns = TASK_COMM_LEN;
if (strncmp(mbu->the_user, name, ns))
MB_DBG("Owner no match. (%s/%s)\n", mbu->the_user, name);
mb = mbu->mb;
list_del(&mbu->mbu_list);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
mb->creator = NULL;
atomic_dec(&mb->count);
if (MB_IN_USE(mb)) {
MB_DBG("<mb_free, MB %8lx still in use. Cnt %d>\n",
mb->phys, MB_COUNT(mb));
spin_unlock_irqrestore(&mb_do_lock, flags);
return 0;
}
ret = mb_free_mb(mb);
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_DBG("OUT\n");
return ret;
}
EXPORT_SYMBOL(mb_do_free);
int mb_do_get(unsigned long addr, pid_t tgid, char *name)
{
struct mb_user *mbu = NULL, *newmbu;
struct mb_struct *mb = NULL;
unsigned long flags;
size_t ns; /* name size */
void *virt = (void *)addr;
if (!addr || !name) {
MB_WARN("mb_do_get fail. invalid args %lx/%s\n",
addr, name);
return -EINVAL;
}
newmbu = kmem_cache_alloc(wmt_mbah->mbu_cachep, GFP_ATOMIC);
if (!newmbu) {
MB_DBG("<mbu_cachep out of memory.>\n");
return -ENOMEM;
}
MB_DBG("IN, TGID %x addr %#lx name %s\n", tgid, addr, name);
spin_lock_irqsave(&mb_do_lock, flags);
if (tgid != MB_DEF_TGID) { /* find user address, only exist on MMAP */
mbu = mb_find_mbu(virt, tgid, MBUFIND_USER|MBUFIND_MMAP);
mb = (mbu) ? mbu->mb : NULL;
} else
mb = mb_find_mb(virt, MBFIND_PHYS);
if (!mb) {
spin_unlock_irqrestore(&mb_do_lock, flags);
kmem_cache_free(wmt_mbah->mbu_cachep, newmbu);
MB_WARN("%s mb_get fail. unknown addr %8lx\n", name, addr);
return -EFAULT;
}
memset(newmbu, 0x0, sizeof(struct mb_user));
INIT_LIST_HEAD(&newmbu->mbu_list);
newmbu->addr = addr;
newmbu->mb = mb;
newmbu->tgid = tgid;
ns = strlen(name);
if (ns > TASK_COMM_LEN)
ns = TASK_COMM_LEN;
strncpy(newmbu->the_user, name, ns);
newmbu->the_user[ns] = 0;
atomic_inc(&mb->count);
list_add_tail(&newmbu->mbu_list, &mb->mbu_list);
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_DBG("out, [mbu] addr %lx %s [mb] addr %lx cnt %d\n",
addr, newmbu->the_user, mb->phys, MB_COUNT(mb));
return 0;
}
EXPORT_SYMBOL(mb_do_get);
int mb_do_put(unsigned long addr, pid_t tgid, char *name)
{
struct mb_struct *mb;
struct mb_user *mbu;
unsigned long flags;
void *virt = (void *)addr;
if (!addr || !name) {
MB_WARN("mb_do_put fail. invalid args %lx/%s\n", addr, name);
return -EINVAL;
}
MB_DBG("IN, TGID %x addr %#lx name %s\n", tgid, addr, name);
spin_lock_irqsave(&mb_do_lock, flags);
if (tgid == MB_DEF_TGID)
mbu = mb_find_mbu(virt, tgid, MBUFIND_PHYS|MBUFIND_GETPUT);
else
mbu = mb_find_mbu(virt, tgid, MBUFIND_USER|MBUFIND_GETPUT);
if (!mbu) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("%s mb_put fail. unknow addr %8lx\n", name, addr);
return -EINVAL;
}
mb = mbu->mb;
if (!MB_IN_USE(mb)) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("%s mb_put fail. block %p unbalance.\n", name, mb);
return -EPERM;
}
list_del_init(&mbu->mbu_list);
atomic_dec(&mb->count);
/* retrieve unused block */
if (!MB_IN_USE(mb))
mb_free_mb(mb);
spin_unlock_irqrestore(&mb_do_lock, flags);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
MB_DBG("out, [mbu] addr %lx %s [mb] addr %lx cnt %d\n",
addr, mbu->the_user, mb->phys, MB_COUNT(mb));
return 0;
}
EXPORT_SYMBOL(mb_do_put);
#define DEVICE_NAME "Memory Block"
static int mb_dev_major;
static int mb_dev_minor;
static int mb_dev_nr;
static struct cdev *mb_cdev;
static struct class *mb_dev_class;
static int mb_open(struct inode *inode, struct file *filp)
{
struct mb_task_info *mbti = NULL;
unsigned long flags;
int task_exist = 0;
size_t ns;
if (filp->private_data) {
MB_ERROR("none empty private data.\n");
return -EFAULT;
}
spin_lock_irqsave(&mb_task_lock, flags);
list_loop(mbti, &wmt_mbti.mbti_list, mbti_list) {
if (mbti->task && mbti->tgid == current->tgid) {
task_exist = 1;
break;
}
}
if (!task_exist) {
mbti = kmem_cache_alloc(wmt_mbah->mbti_cachep, GFP_ATOMIC);
if (!mbti) {
spin_unlock_irqrestore(&mb_task_lock, flags);
MB_ERROR("out of memory (mb_task_info).\n");
return -EFAULT;
}
memset(mbti, 0x0, sizeof(struct mb_task_info));
INIT_LIST_HEAD(&mbti->mbti_list);
mbti->task = current;
mbti->tgid = current->tgid;
ns = strlen(current->comm);
if (ns > TASK_COMM_LEN)
ns = TASK_COMM_LEN;
strncpy(mbti->task_name, current->comm, ns);
mbti->task_name[ns] = 0;
atomic_set(&mbti->count, 0);
list_add_tail(&mbti->mbti_list, &wmt_mbti.mbti_list);
}
atomic_inc(&mbti->count);
MB_DBG("mb driver is opened by task(%p) %s TGID %x count %d.\n",
current, current->comm, current->tgid,
atomic_read(&(mbti->count)));
filp->private_data = (void *)mbti;
spin_unlock_irqrestore(&mb_task_lock, flags);
return 0;
}
static int mb_release(struct inode *inode, struct file *filp)
{
struct mb_area_struct *mba;
struct mb_struct *mb;
struct mb_user *mbu;
struct mb_task_info *cmbti = NULL, *mbti = NULL;
unsigned long flags, tflags;
int task_exist = 0;
cmbti = (struct mb_task_info *)(filp->private_data);
if (!cmbti) {
MB_ERROR("none empty private data.\n");
return -EFAULT;
}
spin_lock_irqsave(&mb_task_lock, tflags);
list_loop(mbti, &wmt_mbti.mbti_list, mbti_list) {
if (mbti->task && mbti->tgid == cmbti->tgid) {
atomic_dec(&mbti->count);
task_exist = 1;
break;
}
}
if (!task_exist) {
spin_unlock_irqrestore(&mb_task_lock, tflags);
MB_INFO("mb driver is closed by unknown task %s TGID %x.\n",
current->comm, current->tgid);
return 0;
}
MB_DBG("mb driver is closed by task %s TGID %x cnt %d.\n",
mbti->task_name, mbti->tgid, atomic_read(&mbti->count));
if (atomic_read(&mbti->count)) {
spin_unlock_irqrestore(&mb_task_lock, tflags);
return 0;
}
spin_lock_irqsave(&mb_ioctl_lock, flags);
RESCAN_MBA:
/* munmap virtual memroy and retrieve unused MB */
list_loop(mba, &wmt_mbah->mba_list, mba_list) {
#ifdef CONFIG_WMT_MB_DYNAMIC_ALLOCATE_SUPPORT
if (mba->tgid == mbti->tgid) { /* free prefetched mba marker */
wmt_mbah->tot_static_pages -= mba->pages;
mba->flags &= ~(MBAFLAG_STATIC);
mba->tgid = MB_DEF_TGID;
if (!mba->nr_mb) {
mb_free_mba(mba);
goto RESCAN_MBA;
}
}
#endif
list_loop(mb, &mba->mb_list, mb_list) {
RESCAN_MBU:
list_loop(mbu, &mb->mbu_list, mbu_list) {
if (mbu->tgid == mbti->tgid) {
char msg[128], *p = msg;
const char *opStr[3] = {
"\"ALLOC\"",
"\"MAP\"",
"\"GET\""};
int op = (mbu->size) ? 1 : 2;
if (mb->creator == mbu) {
op = 0;
mb->creator = NULL;
}
p += sprintf(p, "Stop mb operation %s",
opStr[op]);
p += sprintf(p, "(addr %lx # %lu(KB) ",
mb->phys, mbu->size/1024);
p += sprintf(p, "by %s) (Cnt %d)\n",
mbu->the_user, MB_COUNT(mb)-1);
MB_INFO("%s", msg);
atomic_dec(&mb->count);
list_del_init(&mbu->mbu_list);
kmem_cache_free(
wmt_mbah->mbu_cachep, mbu);
if (MB_COUNT(mb))
goto RESCAN_MBU;
MB_INFO("Recycle mb addr %lx # %lu\n",
mb->phys, mb->size);
mb_free_mb(mb);
goto RESCAN_MBA;
}
}
}
}
mb_update_mbah();
list_del(&mbti->mbti_list);
kmem_cache_free(wmt_mbah->mbti_cachep, mbti);
filp->private_data = NULL;
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
spin_unlock_irqrestore(&mb_task_lock, tflags);
return 0;
}
static long mb_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct mb_task_info *mbti = NULL;
struct mb_area_struct *mba = NULL;
struct mb_struct *mb = NULL;
struct mb_user *mbu = NULL;
unsigned long value, flags, phys, virt, size;
pid_t tgid = MB_DEF_TGID;
int count, ret = 0, cmdSize;
size_t len;
/* check type and number, if fail return ENOTTY */
#ifdef CONFIG_WMT_MB_RESERVE_FROM_IO
if (_IOC_TYPE(cmd) != MB_IOC_MAGIC && cmd != FBIOGET_FSCREENINFO)
return -ENOTTY;
#else
if (_IOC_TYPE(cmd) != MB_IOC_MAGIC)
return -ENOTTY;
#endif
/* check argument area */
cmdSize = _IOC_SIZE(cmd);
if (_IOC_DIR(cmd) & _IOC_READ)
ret = !access_ok(VERIFY_WRITE, (void __user *) arg, cmdSize);
else if (_IOC_DIR(cmd) & _IOC_WRITE)
ret = !access_ok(VERIFY_READ, (void __user *) arg, cmdSize);
if (ret)
return -EFAULT;
/* check task validation */
if (!filp || !filp->private_data) {
MB_ERROR("mmap with null file info or task.\n");
return -EFAULT;
}
mbti = (struct mb_task_info *)(filp->private_data);
tgid = mbti->tgid;
if (tgid != current->tgid) {
MB_WARN("ioctl within diff task.\n");
MB_WARN("Open: task %s TGID %x VS Curr: task %s TGID %x\n",
mbti->task_name, tgid, current->comm, current->tgid);
}
switch (cmd) {
/* _IOR (MB_IOC_MAGIC, 16, unsigned long) O: mb driver version */
case MBIO_GET_VERSION:
value = MB_VERSION_MAJOR << 24 |
MB_VERSION_MINOR << 16 |
MB_VERSION_MICRO << 8 |
MB_VERSION_BUILD;
put_user(value, (unsigned long *)arg);
break;
/* _IOWR(MB_IOC_MAGIC, 0, unsigned long)
* I: size
* O: physical address */
case MBIO_MALLOC:
get_user(value, (unsigned long *)arg);
phys = mb_do_alloc(value, tgid, mbti->task_name);
if (!phys) {
MB_WARN("MALLOC: size %ld fail.\n\n", value);
return -EFAULT;
}
MB_OPDBG("MALLOC: addr %lx size %ld\n\n", phys, value);
put_user(phys, (unsigned long *)arg);
break;
/* _IOW (MB_IOC_MAGIC, 17, unsigned long) O: property of MB */
case MBIO_SET_CACHABLE:
get_user(value, (unsigned long *)arg);
spin_lock_irqsave(&mb_do_lock, flags);
mb = mb_find_mb((void *)value, MBFIND_PHYS);
if (!mb) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("SET_CACHABLE: phys %8lx (name %s) fail\n",
value, mbti->task_name);
return -EFAULT;
}
mb->flags |= MBFLAG_CACHED;
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_OPDBG("SET_CACHABLE: phys %8lx\n\n", value);
break;
/* _IOW (MB_IOC_MAGIC,18, unsigned long) O: property of MB */
case MBIO_CLR_CACHABLE:
get_user(value, (unsigned long *)arg);
spin_lock_irqsave(&mb_do_lock, flags);
mb = mb_find_mb((void *)value, MBFIND_PHYS);
if (!mb) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("CLR_CACHABLE: phys %8lx (name %s) fail\n",
value, mbti->task_name);
return -EFAULT;
}
mb->flags &= ~MBFLAG_CACHED;
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_OPDBG("CLR_CACHABLE: phys %8lx\n\n", value);
break;
/* _IOWR(MB_IOC_MAGIC, 1, unsigned long)
* I: user address if map, physical address if not map
* O: ummap size */
case MBIO_FREE: /* if not unmap, unmap first */
get_user(value, (unsigned long *)arg);
spin_lock_irqsave(&mb_ioctl_lock, flags);
size = value;
mbu = mb_find_mbu((void *)value, tgid,
MBUFIND_USER|MBUFIND_MMAP);
if (mbu != NULL) {
size = mbu->size;
value = mbu->mb->phys;
list_del(&mbu->mbu_list);
atomic_dec(&mbu->mb->count);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
} else
MB_DBG("FREE: unknown MMAP addr %lx\n", value);
ret = mb_do_free(value, tgid, mbti->task_name);
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
if (ret < 0)
MB_WARN("FREE: addr %lx fail\n", value);
/* return mmap size for ummap */
put_user(size, (unsigned long *)arg);
MB_OPDBG("FREE: addr %lx out %ld\n\n", value, size);
break;
/* _IOWR(MB_IOC_MAGIC, 2, unsigned long)
* I: user address
* O: ummap size */
case MBIO_UNMAP:
get_user(value, (unsigned long *)arg);
spin_lock_irqsave(&mb_ioctl_lock, flags);
mbu = mb_find_mbu((void *)value, tgid,
MBUFIND_USER|MBUFIND_MMAP);
if (mbu == NULL) {
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
MB_WARN("UNMAP: addr %lx fail\n\n", value);
return -EFAULT;
}
mb = mbu->mb;
size = mbu->size;
list_del(&mbu->mbu_list);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
atomic_dec(&mb->count);
count = MB_COUNT(mb);
if (count <= 0)
MB_WARN("UNMAP: MB %8lx count %d weird\n\n",
mb->phys, count);
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
/* return mmap size for ummap */
put_user(size, (unsigned long *)arg);
return 0;
/* _IOWR(MB_IOC_MAGIC, 3, unsigned long)
* I: phys address
* O: mb size */
case MBIO_MBSIZE:
get_user(value, (unsigned long *)arg);
spin_lock_irqsave(&mb_ioctl_lock, flags);
mb = mb_find_mb((void *)value, MBFIND_PHYS);
if (mb == NULL) {
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
MB_WARN("MBSIZE: user %lx fail.\n\n", value);
return -EFAULT;
}
size = mb->size;
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
MB_OPDBG("MBSIZE: addr %lx size %ld\n\n", value, size);
put_user(size, (unsigned long *)arg);
break;
/* _IOR (MB_IOC_MAGIC, 4, unsigned long)
* O: max free mba size */
case MBIO_MAX_AVAILABLE_SIZE:
put_user(wmt_mbah->max_available_pages*PAGE_SIZE,
(unsigned long *)arg);
MB_OPDBG("MAX_MB_SIZE: size %ld KB\n\n",
PAGE_KB(wmt_mbah->max_available_pages));
break;
/* _IOW (MB_IOC_MAGIC, 5, unsigned long)
* I: user address */
case MBIO_GET:
get_user(value, (unsigned long *)arg);
ret = mb_do_get(value, tgid, mbti->task_name);
if (MBMSG_LEVEL) {
spin_lock_irqsave(&mb_ioctl_lock, flags);
mbu = mb_find_mbu((void *)value, tgid,
MBUFIND_USER|MBUFIND_GETPUT);
count = mbu ? MB_COUNT(mbu->mb) : 0xffffffff;
MB_OPDBG("GET: addr %lx cnt %x\n\n", value, count);
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
}
break;
/* _IOW (MB_IOC_MAGIC, 6, unsigned long)
* I: user address */
case MBIO_PUT:
get_user(value, (unsigned long *)arg);
if (MBMSG_LEVEL) {
spin_lock_irqsave(&mb_ioctl_lock, flags);
mbu = mb_find_mbu((void *)value, tgid,
MBUFIND_USER|MBUFIND_GETPUT);
count = mbu ? MB_COUNT(mbu->mb) - 1 : 0xffffffff;
MB_OPDBG("PUT: addr %lx cnt %x\n\n", value, count);
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
}
ret = mb_do_put(value, tgid, mbti->task_name);
break;
/* _IOWR(MB_IOC_MAGIC, 7, unsigned long)
* I: user address
* O: virt address */
case MBIO_USER_TO_VIRT:
get_user(value, (unsigned long *)arg);
virt = mb_do_user_to_virt(value, tgid, mbti->task_name);
put_user(virt, (unsigned long *)arg);
MB_OPDBG("USER_TO_VERT: user %8lx virt %lx\n\n", value, virt);
break;
/* _IOWR(MB_IOC_MAGIC, 8, unsigned long)
* I: user address
* O: phys address */
case MBIO_USER_TO_PHYS:
get_user(value, (unsigned long *)arg);
phys = mb_do_user_to_phys(value, tgid, mbti->task_name);
put_user(phys, (unsigned long *)arg);
MB_OPDBG("USER_TO_PHYS: user %8lx phys %lx\n\n", value, phys);
break;
#ifdef CONFIG_WMT_MB_RESERVE_FROM_IO
/* This IOCTRL is provide AP to recognize MB region as framebuffer,
then can access mb memory directly. */
case MBIO_FSCREENINFO:
{
struct fb_fix_screeninfo ffs;
if (!wmt_mbah || list_empty(&wmt_mbah->mba_list)) {
MB_WARN("MBIO_FSCREENINFO: MB driver does not init.\n");
return -EFAULT;
}
mba = (struct mb_area_struct *)wmt_mbah->mba_list.next;
ffs.smem_start = mba->phys;
ffs.smem_len = wmt_mbah->tot_static_pages * PAGE_SIZE;
if (copy_to_user((void __user *)arg, &ffs, sizeof(ffs)))
ret = -EFAULT;
MB_OPDBG("FSCREENINFO: phys %lx len %x\n\n",
ffs.smem_start, ffs.smem_len);
break;
}
#endif
/* _IOW (MB_IOC_MAGIC, 9, unsigned long)
* I: size */
case MBIO_PREFETCH:
{
unsigned long fetch_size = 0;
get_user(value, (unsigned long *)arg);
#ifdef CONFIG_WMT_MB_DYNAMIC_ALLOCATE_SUPPORT
{
unsigned long try_size = 0;
MB_OPDBG("PREFETCH: size %ld KB\n\n", value/1024);
spin_lock_irqsave(&mb_ioctl_lock, flags);
while (try_size < value) {
int order;
order = get_order(value-fetch_size);
if (order > MBMAX_ORDER)
order = MBMAX_ORDER;
try_size += (1 << order) * PAGE_SIZE;
mba = mb_alloc_mba(1 << order);
if (mba) {
mba->flags |= MBAFLAG_STATIC;
mba->tgid = current->tgid;
wmt_mbah->tot_static_pages += mba->pages;
fetch_size += mba->pages * PAGE_SIZE;
}
}
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
MB_INFO("PREFETCH: SIZE %ld / %ld kB by %s\n",
fetch_size/1024, value/1024, current->comm);
}
#else
MB_WARN("PREFETCH: SIZE %ld / %ld kB by %s fail\n",
fetch_size/1024, value/1024, current->comm);
MB_WARN("PREFETCH: Dynamic Allocated Not Support\n");
ret = -ENOTTY;
#endif
break;
}
/* _IOW (MB_IOC_MAGIC, 10, unsigned long)
* O: static mba size */
case MBIO_STATIC_SIZE:
put_user(wmt_mbah->tot_static_pages * PAGE_SIZE,
(unsigned long *)arg);
MB_OPDBG("STATIC_SIZE: size %ld KB\n\n",
PAGE_KB(wmt_mbah->tot_static_pages));
break;
/* _IOWR(MB_IOC_MAGIC,11, unsigned long)
* I: physical address
* O: use counter */
case MBIO_MB_USER_COUNT:
get_user(value, (unsigned long *)arg);
spin_lock_irqsave(&mb_ioctl_lock, flags);
mb = mb_find_mb((void *)value, MBFIND_PHYS);
if (mb == NULL)
size = 0;
else
size = MB_COUNT(mb);
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
put_user(size, (unsigned long *)arg);
MB_OPDBG("USER_COUNT: addr %lx count %ld\n\n", value, size);
break;
/* _IO (MB_IOC_MAGIC,12) */
case MBIO_FORCE_RESET:
spin_lock_irqsave(&mb_ioctl_lock, flags);
RESCAN_MBA:
list_loop(mba, &wmt_mbah->mba_list, mba_list) {
list_loop(mb, &mba->mb_list, mb_list) {
list_loop(
mbu, &mb->mbu_list, mbu_list) {
list_del(&mbu->mbu_list);
kmem_cache_free(
wmt_mbah->mbu_cachep, mbu);
}
mb->creator = NULL;
atomic_set(&mb->count, 0);
mb_free_mb(mb);
/* because mba link maybe breaken */
goto RESCAN_MBA;
}
}
mb_update_mbah();
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
MB_OPDBG("RESET: OK\n\n");
break;
/* _IOW (MB_IOC_MAGIC,13, unsigned long) // I: phys address */
case MBIO_GET_BY_PHYS:
get_user(value, (unsigned long *)arg);
if (!value || !mbti->task_name || tgid == MB_DEF_TGID) {
MB_WARN("GET(phys): invalid args %lx/%d/%s\n",
value, tgid, mbti->task_name);
return -EINVAL;
}
mbu = kmem_cache_alloc(wmt_mbah->mbu_cachep, GFP_ATOMIC);
if (!mbu) {
MB_WARN("GET(phys): mbu_cachep out of memory\n");
return -ENOMEM;
}
spin_lock_irqsave(&mb_do_lock, flags);
mb = mb_find_mb((void *)value, MBFIND_PHYS);
if (!mb) {
spin_unlock_irqrestore(&mb_do_lock, flags);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
MB_WARN("GET(phys): unknown phys %8lx name %s\n",
value, mbti->task_name);
return -EFAULT;
}
memset(mbu, 0x0, sizeof(struct mb_user));
INIT_LIST_HEAD(&mbu->mbu_list);
mbu->addr = value;
mbu->mb = mb;
mbu->tgid = tgid;
len = strlen(mbti->task_name);
if (len > TASK_COMM_LEN)
len = TASK_COMM_LEN;
strncpy(mbu->the_user, mbti->task_name, len);
mbu->the_user[len] = 0;
atomic_inc(&mb->count);
list_add_tail(&mbu->mbu_list, &mb->mbu_list);
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_OPDBG("GET(phys): phys %8lx cnt %x\n\n",
value, MB_COUNT(mb));
break;
/* _IOW (MB_IOC_MAGIC,14, unsigned long) // I: phys address */
case MBIO_PUT_BY_PHYS:
get_user(value, (unsigned long *)arg);
if (!value || !mbti->task_name || tgid == MB_DEF_TGID) {
MB_WARN("PUT(phys): invalid args %lx/%d/%s\n",
value, tgid, mbti->task_name);
return -EINVAL;
}
spin_lock_irqsave(&mb_do_lock, flags);
mbu = mb_find_mbu((void *)value, tgid,
MBUFIND_PHYS|MBUFIND_GETPUT);
if (!mbu) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("PUT(phys): unknow phys %8lx name %s\n",
value, mbti->task_name);
return -EINVAL;
}
mb = mbu->mb;
if (!MB_IN_USE(mb)) {
spin_unlock_irqrestore(&mb_do_lock, flags);
MB_WARN("PUT(phys): phys %8lx unbalance.\n", value);
return -EPERM;
}
list_del_init(&mbu->mbu_list);
atomic_dec(&mb->count);
size = MB_COUNT(mb);
/* retrieve unused block */
if (!size)
mb_free_mb(mb);
spin_unlock_irqrestore(&mb_do_lock, flags);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
MB_OPDBG("PUT(phys): phys %8lx cnt %lx\n\n", value, size);
break;
/* _IOW (MB_IOC_MAGIC,15, unsigned long) // I: user address */
case MBIO_SYNC_CACHE:
get_user(value, (unsigned long *)arg);
spin_lock_irqsave(&mb_ioctl_lock, flags);
mbu = mb_find_mbu((void *)value, tgid,
MBUFIND_USER|MBUFIND_MMAP);
if (mbu == NULL) {
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
MB_WARN("SYNC_CACHE: invalid addr %lx\n\n", value);
return -EFAULT;
}
mb = mbu->mb;
size = mbu->size;
dmac_flush_range(mb->virt, mb->virt + size);
outer_flush_range(mb->phys, mb->phys + size);
MB_OPDBG("SYNC_CACHE: (%lx # %lx) V %p ~ %p P %lx ~ %lx\n\n",
value, size, mb->virt, mb->virt + size,
mb->phys, mb->phys + size);
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
return 0;
/* _IOW(MB_IOC_MAGIC, 19, unsigned long) // I: phys address */
case MBIO_STATIC_GET:
get_user(value, (unsigned long *)arg);
if (!value || !mbti->task_name || tgid == MB_DEF_TGID) {
MB_WARN("GET(STATIC): invalid args %lx/%d/%s\n",
value, tgid, mbti->task_name);
return -EINVAL;
}
ret = mb_do_get(value, MB_DEF_TGID, mbti->task_name);
if (ret != 0)
MB_WARN("GET(STATIC): fail (args %lx/%d/%s). ret %d\n",
value, tgid, mbti->task_name, ret);
MB_OPDBG("GET(STATIC): phys %8lx done\n\n", value);
break;
/* _IOW(MB_IOC_MAGIC, 20, unsigned long) // I: phys address */
case MBIO_STATIC_PUT:
get_user(value, (unsigned long *)arg);
if (!value || !mbti->task_name || tgid == MB_DEF_TGID) {
MB_WARN("PUT(STATIC): invalid args %lx/%d/%s\n",
value, tgid, mbti->task_name);
return -EINVAL;
}
ret = mb_do_put(value, MB_DEF_TGID, mbti->task_name);
if (ret != 0)
MB_WARN("PUT(STATIC): fail (args %lx/%d/%s). ret %d\n",
value, tgid, mbti->task_name, ret);
MB_OPDBG("PUT(STATIC): phys %8lx done\n\n", value);
break;
/* _IOWR(MB_IOC_MAGIC, 100, unsigned long)
I: user address O: physical address */
case MBIO_TEST_USER2PHYS:
get_user(value, (unsigned long *)arg);
if (!value) {
MB_WARN("USER2PHYS: invalid args %lx/%d/%s\n",
value, tgid, mbti->task_name);
return -EINVAL;
}
phys = user_to_phys(value);
put_user(phys, (unsigned long *)arg);
if (!phys)
MB_WARN("USER2PHYS fail: value %#lx phys %#lx\n", value, phys);
break;
/* _IOWR(MB_IOC_MAGIC, 101, unsigned long)
I: user address, size, prdt addr, and prdt size */
case MBIO_TEST_USER2PRDT:
/* _IOW(MB_IOC_MAGIC, 103, unsigned long)
* I: user address, size, prdt addr, and prdt size */
case MBIO_TEST_MB2PRDT: {
int items, type = (cmd == MBIO_TEST_MB2PRDT) ? 0 : 1;
unsigned long size, data[4] = {0};
struct prdt_struct *prdt;
copy_from_user(data, (const void *)arg, 4 * sizeof(unsigned long));
if (!data[0] || !data[1] || !data[2] || !data[3]) {
MB_WARN("%s: invalid args [%#lx, %#lx, %#lx, %#lx]\n",
type ? "MB2PRDT" : "USER2PRDT",
data[0], data[1], data[2], data[3]);
return -EINVAL;
}
items = data[1] / PAGE_SIZE + 2;
size = items * sizeof(struct prdt_struct);
if (data[3] < size) {
MB_WARN("%s: invalid args. prdt size %#lx < %#lx\n",
type ? "MB2PRDT" : "USER2PRDT", data[3], size);
return -EINVAL;
}
prdt = kmalloc(size, GFP_ATOMIC);
if (prdt == NULL) {
MB_WARN("%s: fail, no prdt space %d for size %ld\n",
type ? "MB2PRDT" : "USER2PRDT",
items * sizeof(struct prdt_struct), data[1]);
return -EINVAL;
}
MB_DBG("%s: IN %#lx, %#lx, %#lx, %#lx\n",
type ? "MB2PRDT" : "USER2PRDT",
data[0], data[1], data[2], data[3]);
memset(prdt, 0x0, items * sizeof(struct prdt_struct));
if ((!type && !mb_to_prdt(data[0], data[1], prdt, items)) ||
(!user_to_prdt(data[0], data[1], prdt, items))) {
if (MBMSG_LEVEL)
show_prdt(prdt);
copy_to_user((void __user *)data[2], prdt, size);
MB_INFO("%s: Done %#lx, %#lx, %#lx, %#lx\n",
type ? "MB2PRDT" : "USER2PRDT",
data[0], data[1], data[2], data[3]);
} else {
MB_WARN("%s: fail.\n", type ? "MB2PRDT" : "USER2PRDT");
ret = -EINVAL;
}
kfree(prdt);
break;
}
/* _IOWR(MB_IOC_MAGIC, 102, unsigned long)
I: user address, size, mmu addr, and mmu size O: mmu physical addr */
case MBIO_TEST_USER2MMUT:
/* _IOWR(MB_IOC_MAGIC, 104, unsigned long)
I: user address, size, mmu addr, and mmu size O: mmu physical addr */
case MBIO_TEST_MB2MMUT: {
int type = (cmd == MBIO_TEST_MB2MMUT) ? 0 : 1;
unsigned long mmuSize, data[4] = {0};
struct mmu_table_info info = {0};
copy_from_user(data, (const void *)arg, 4 * sizeof(unsigned long));
if (!data[0] || !data[1] || !data[2] || !data[3]) {
MB_WARN("%s: invalid args [%#lx, %#lx, %#lx, %#lx]\n",
type ? "USER2MMUT" : "PHYS2MMUT",
data[0], data[1], data[2], data[3]);
return -EINVAL;
}
mmuSize = wmt_mmu_table_size(data[1]);
if (data[3] < mmuSize) {
MB_WARN("%s: invalid args. mmu size %#lx < %#lx\n",
type ? "MB2PRDT" : "USER2PRDT", data[3], mmuSize);
return -EINVAL;
}
MB_DBG("%s: IN %#lx, %#lx, %#lx, %#lx(req %#lx)\n",
type ? "USER2MMUT" : "PHYS2MMUT",
data[0], data[1], data[2], data[3], mmuSize);
if (wmt_mmu_table_create(data[0], data[1], type, &info) == 0) {
void *mmuAddr = mb_phys_to_virt((unsigned long)info.addr);
if (MBMSG_LEVEL)
wmt_mmu_table_dump(&info);
copy_to_user((void __user *)data[2], mmuAddr, mmuSize);
put_user(info.addr, (unsigned long *)arg);
ret = mb_do_get(info.addr, MB_DEF_TGID, mbti->task_name);
wmt_mmu_table_destroy(&info);
MB_INFO("%s: Done %#lx, %#lx, %#lx, %#lx\n",
type ? "USER2MMUT" : "PHYS2MMUT",
data[0], data[1], data[2], data[3]);
} else {
MB_WARN("%s: fail. no mmu table space for size %ld\n",
type ? "USER2MMUT" : "PHYS2MMUT", data[1]);
return -EINVAL;
}
break;
}
default:
ret = -ENOTTY;
break;
}
return ret;
}
static int mb_mmap(struct file *filp, struct vm_area_struct *vma)
{
struct mb_user *mbu = NULL;
struct mb_task_info *mbti;
struct mb_struct *mb;
unsigned long off, flags;
unsigned int len;
pid_t tgid;
size_t ns;
if (!filp || !filp->private_data) {
MB_ERROR("mmap with null file info or task.\n");
return -EFAULT;
}
mbti = (struct mb_task_info *)(filp->private_data);
tgid = mbti->tgid;
if (tgid != current->tgid) {
MB_WARN("mmap within diff task.\n");
MB_WARN("Open: task %s TGID %x VS Curr: task %s TGID %x\n",
mbti->task_name, tgid, current->comm, current->tgid);
}
off = vma->vm_pgoff << PAGE_SHIFT;
MB_DBG("IN, offset %lx TGID %x name %s\n", off, tgid, mbti->task_name);
#ifdef CONFIG_WMT_MB_RESERVE_FROM_IO
/* if page offset under MB total size, it means page offset
* is not physical address from MB_alloc and then map function
* works like framebuffer.
* Suppose MB total size will not cross MBAH phys start address */
if (vma->vm_pgoff <= wmt_mbah->tot_static_pages) {
struct mb_area_struct *mba = NULL;
unsigned long mbtotal =
wmt_mbah->tot_static_pages << PAGE_SHIFT;
if ((vma->vm_end - vma->vm_start + off) > mbtotal) {
MB_ERROR("mmap io fail. range %lx @ [%lx, %lx] s %lx\n",
off, vma->vm_start, vma->vm_end, mbtotal);
return -EINVAL;
}
mba = (struct mb_area_struct *)wmt_mbah->mba_list.next;
vma->vm_pgoff += mba->phys >> PAGE_SHIFT;
vma->vm_flags |= VM_IO | VM_RESERVED;
if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
MB_ERROR("mmap io fail. (io_remap pfn %lx len %lx)\n",
vma->vm_pgoff, vma->vm_end - vma->vm_start);
return -EAGAIN;
}
MB_OPDBG("MAP: IO from %lx to %lx/%lx size %ld\n\n",
vma->vm_pgoff << PAGE_SHIFT,
vma->vm_start, vma->vm_end,
vma->vm_end - vma->vm_start);
return 0;
}
#endif
mbu = kmem_cache_alloc(wmt_mbah->mbu_cachep, GFP_ATOMIC);
if (!mbu) {
MB_ERROR("mbu_cachep out of memory.\n");
return -ENOMEM;
}
spin_lock_irqsave(&mb_ioctl_lock, flags);
mb = mb_find_mb((void *)off, MBFIND_PHYS);
if (!mb) {
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
MB_ERROR("map addr %lx not exist in MB.\n", off);
return -EINVAL;
}
len = PAGE_ALIGN(mb->phys + mb->size);
if ((vma->vm_end - vma->vm_start + off) > len) {
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
return -EINVAL;
}
memset(mbu, 0x0, sizeof(struct mb_user));
INIT_LIST_HEAD(&mbu->mbu_list);
mbu->tgid = tgid;
mbu->mb = mb;
mbu->addr = vma->vm_start;
mbu->size = vma->vm_end - vma->vm_start;
ns = strlen(mbti->task_name);
if (ns > TASK_COMM_LEN)
ns = TASK_COMM_LEN;
strncpy(mbu->the_user, mbti->task_name, ns);
mbu->the_user[ns] = 0;
list_add_tail(&mbu->mbu_list, &mb->mbu_list);
atomic_inc(&mb->count);
off = mb->phys;
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
vma->vm_flags |= VM_IO | VM_RESERVED;
if (!(mb->flags & MBFLAG_CACHED))
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
spin_lock_irqsave(&mb_ioctl_lock, flags);
list_del(&mbu->mbu_list);
atomic_dec(&mb->count);
kmem_cache_free(wmt_mbah->mbu_cachep, mbu);
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
MB_ERROR("virtual address memory map fail.\n");
return -EAGAIN;
}
MB_OPDBG("MAP: IO from %lx to %lx/%lx size %ld mbcnt %d\n\n",
off, vma->vm_start, vma->vm_end,
(vma->vm_end - vma->vm_start), MB_COUNT(mb));
return 0;
}
/*!*************************************************************************
driver file operations struct define
****************************************************************************/
const struct file_operations mb_fops = {
.owner = THIS_MODULE,
.open = mb_open,
.release = mb_release,
.unlocked_ioctl = mb_ioctl,
.mmap = mb_mmap,
};
static int mb_probe(struct platform_device *dev)
{
struct mb_area_struct *mba = NULL, *bind = NULL;
unsigned long flags;
int ret = 0;
dev_t dev_no;
dev_no = MKDEV(mb_dev_major, mb_dev_minor);
if (wmt_mbah) {
MB_ERROR("%s dirty.\n", DEVICE_NAME);
return -EINVAL;
}
/* register char device */
mb_cdev = cdev_alloc();
if (!mb_cdev) {
MB_ERROR("alloc dev error.\n");
return -ENOMEM;
}
cdev_init(mb_cdev, &mb_fops);
ret = cdev_add(mb_cdev, dev_no, 1);
if (ret) {
MB_ERROR("reg char dev error(%d).\n", ret);
cdev_del(mb_cdev);
return ret;
}
wmt_mbah = kmalloc(sizeof(struct mba_host_struct), GFP_ATOMIC);
if (!wmt_mbah) {
MB_ERROR("out of memory (mb_area_struct).\n");
cdev_del(mb_cdev);
return -ENOMEM;
}
memset(wmt_mbah, 0x0, sizeof(struct mba_host_struct));
wmt_mbah->mba_cachep = kmem_cache_create("mb_area_struct",
sizeof(struct mb_area_struct),
0,
SLAB_HWCACHE_ALIGN,
NULL);
if (!wmt_mbah->mba_cachep) {
MB_ERROR("out of memory (mba_cachep).\n");
kfree(wmt_mbah);
cdev_del(mb_cdev);
return -ENOMEM;
}
wmt_mbah->mb_cachep = kmem_cache_create("mb_struct",
sizeof(struct mb_struct),
0,
SLAB_HWCACHE_ALIGN,
NULL);
if (!wmt_mbah->mb_cachep) {
MB_ERROR("out of memory (mb_cachep).\n");
kmem_cache_destroy(wmt_mbah->mba_cachep);
kfree(wmt_mbah);
cdev_del(mb_cdev);
return -ENOMEM;
}
wmt_mbah->mbu_cachep = kmem_cache_create("mb_user",
sizeof(struct mb_user),
0,
SLAB_HWCACHE_ALIGN,
NULL);
if (!wmt_mbah->mbu_cachep) {
MB_ERROR("out of memory (mbu_cachep).\n");
kmem_cache_destroy(wmt_mbah->mb_cachep);
kmem_cache_destroy(wmt_mbah->mba_cachep);
kfree(wmt_mbah);
cdev_del(mb_cdev);
return -ENOMEM;
}
wmt_mbah->mbti_cachep = kmem_cache_create("mb_task_info",
sizeof(struct mb_task_info),
0,
SLAB_HWCACHE_ALIGN,
NULL);
if (!wmt_mbah->mbti_cachep) {
MB_ERROR("out of memory (mbti_cachep).\n");
kmem_cache_destroy(wmt_mbah->mbu_cachep);
kmem_cache_destroy(wmt_mbah->mb_cachep);
kmem_cache_destroy(wmt_mbah->mba_cachep);
kfree(wmt_mbah);
cdev_del(mb_cdev);
return -ENOMEM;
}
MBMAX_ORDER = MBA_MAX_ORDER;
MBMIN_ORDER = MBA_MIN_ORDER;
INIT_LIST_HEAD(&wmt_mbah->mba_list);
INIT_LIST_HEAD(&wmt_mbti.mbti_list);
spin_lock_init(&mb_do_lock);
spin_lock_init(&mb_search_lock);
spin_lock_init(&mb_ioctl_lock);
spin_lock_init(&mb_task_mm_lock);
spin_lock_init(&mb_task_lock);
MB_INFO("Preparing VIDEO BUFFER (SIZE %d kB) ...\n", MB_TOTAL_SIZE);
spin_lock_irqsave(&mb_do_lock, flags);
while (PAGE_KB(wmt_mbah->tot_static_pages) < MB_TOTAL_SIZE) {
int pages = MB_TOTAL_SIZE*1024 -
wmt_mbah->tot_static_pages * PAGE_SIZE;
pages = min((pages >> PAGE_SHIFT), (int)(1 << MBMAX_ORDER));
mba = mb_alloc_mba(pages);
if (mba) {
mba->flags |= MBAFLAG_STATIC;
mba->tgid = MB_DEF_TGID;
wmt_mbah->tot_static_pages += mba->pages;
/* conbine to continue mba if possible */
if (bind && bind->pgi.pfn_end == mba->pgi.pfn_start) {
MB_COMBIND_MBA(bind, mba);
continue;
}
if (bind && bind->pgi.pfn_start == mba->pgi.pfn_end)
MB_COMBIND_MBA(mba, bind);
bind = mba;
}
}
spin_unlock_irqrestore(&mb_do_lock, flags);
mb_update_mbah();
MB_INFO("MB version: %d.%d.%d.%d\n",
MB_VERSION_MAJOR, MB_VERSION_MINOR,
MB_VERSION_MICRO, MB_VERSION_BUILD);
MB_INFO("MAX MB Area size: Max %ld Kb Min %ld Kb\n",
PAGE_KB(1 << MBMAX_ORDER), PAGE_KB(1 << MBMIN_ORDER));
MB_INFO("prob /dev/%s major %d, minor %d\n",
DEVICE_NAME, mb_dev_major, mb_dev_minor);
return ret;
}
static int mb_remove(struct platform_device *dev)
{
if (mb_cdev)
cdev_del(mb_cdev);
if (wmt_mbah) {
if (wmt_mbah->mba_cachep)
kmem_cache_destroy(wmt_mbah->mba_cachep);
if (wmt_mbah->mb_cachep)
kmem_cache_destroy(wmt_mbah->mb_cachep);
if (wmt_mbah->mbu_cachep)
kmem_cache_destroy(wmt_mbah->mbu_cachep);
if (wmt_mbah->mbti_cachep)
kmem_cache_destroy(wmt_mbah->mbti_cachep);
kfree(wmt_mbah);
}
MB_INFO("MB dev remove\n");
return 0;
}
static int mb_suspend(struct platform_device *dev, pm_message_t state)
{
MB_DBG("MB get suspend, Event %d.\n", state.event);
return 0;
}
static int mb_resume(struct platform_device *dev)
{
MB_DBG("MB get resume.\n");
return 0;
}
int mb_area_read_proc(
char *buf, char **start, off_t off,
int count, int *eof, void *data
)
{
struct mb_area_struct *mba;
struct free_area *fa;
struct zone *zone;
unsigned long flags;
unsigned int idx = 1;
char *p = buf, *base = (char *)data;
int datalen = 0, len;
if (!data || !count) {
p += sprintf(p, "no resource for mb_area read proc. (%p,%d)\n",
data, count);
return p-buf;
}
if (!wmt_mbah) {
p += sprintf(p, "no MB area host existed.\n");
return p-buf;
}
spin_lock_irqsave(&mb_ioctl_lock, flags);
if (!off) { /* re read mb area information */
p = base;
memset(p, 0x0, MBPROC_BUFSIZE);
/* show block information */
p += sprintf(p, "VERSION: %3d.%3d.%3d.%3d\n",
MB_VERSION_MAJOR, MB_VERSION_MINOR,
MB_VERSION_MICRO, MB_VERSION_BUILD);
p += sprintf(p, "MESSAGE LEVEL: %8d /%8lx\n",
MBMSG_LEVEL, __pa(&MBMSG_LEVEL));
p += sprintf(p, "STATIC MB SIZE: %8ld MB /%5d MB\n",
PAGE_KB(wmt_mbah->tot_static_pages)/1024,
MB_TOTAL_SIZE/1024);
p += sprintf(p, "MAX MBA ORDER: %8d /%8lx\n",
MBMAX_ORDER, __pa(&MBMAX_ORDER));
p += sprintf(p, "MIN MBA ORDER: %8d /%8lx\n\n",
MBMIN_ORDER, __pa(&MBMIN_ORDER));
p += sprintf(p, "USER TO PRDT METHOD: %8d /%8lx\n\n",
USR2PRDT_METHOD, __pa(&USR2PRDT_METHOD));
p += sprintf(p, "total MB areas: %8d\n",
wmt_mbah->nr_mba);
p += sprintf(p, "total size: %8ld kB\n",
PAGE_KB(wmt_mbah->tot_pages));
p += sprintf(p, "total free size: %8ld kB\n",
PAGE_KB(wmt_mbah->tot_free_pages));
p += sprintf(p, "max MB size: %8ld kB\n\n",
PAGE_KB(wmt_mbah->max_available_pages));
list_loop(mba, &wmt_mbah->mba_list, mba_list) {
p += sprintf(p, "(ID) [MB Area] VirtAddr PhysAddr ");
p += sprintf(p, "size [ zs, ze] MBs Max/");
p += sprintf(p, " Free Ste\n");
len = (int)(p-base);
if ((MBPROC_BUFSIZE - len) < 12) {
p += sprintf(p, " more ...\n");
break;
}
p += sprintf(p, "(%2d)", idx++);
len = (int)(p-base);
/* show all MBs */
/* -2 is for \n and zero */
p += mb_show_mba(mba, NULL, p,
MBPROC_BUFSIZE - len - 2, 1);
p += sprintf(p, "\n");
}
/* show memory fragment */
zone = (first_online_pgdat())->node_zones;
fa = zone->free_area;
p += sprintf(p, "DMA ZONE:\n");
for (idx = 0; idx < MAX_ORDER; idx++)
p += sprintf(p, " %5ld * %5ldkB = %5ld kB\n",
fa[idx].nr_free, PAGE_KB((1<<idx)),
fa[idx].nr_free * PAGE_KB((1<<idx)));
p += sprintf(p, " ------------------------------\n");
p += sprintf(p, " + = %ld kB\n\n",
PAGE_KB((unsigned long)nr_free_pages()));
}
spin_unlock_irqrestore(&mb_ioctl_lock, flags);
datalen = strlen(base);
if (off >= datalen) {
*eof = 1;
return 0;
}
len = min((int)(datalen - off), count);
memcpy(buf, &base[off], len);
*start = (char *)len; /* for case1: *start < page, mass read data */
return len;
}
static struct platform_driver mb_driver = {
.driver.name = "wmt_mb",
.probe = mb_probe,
.remove = mb_remove,
.suspend = mb_suspend,
.resume = mb_resume
};
static void mb_platform_release(struct device *device)
{
return;
}
static struct platform_device mb_device = {
.name = "wmt_mb",
.id = 0,
.dev = { .release = mb_platform_release, },
.num_resources = 0,
.resource = NULL,
};
static int __init mb_init(void)
{
int varlen = 32, ret = -1, mbtotal = 0;
unsigned char buf[32];
dev_t dev_no;
mb_dev_major = MB_MAJOR;
mb_dev_minor = 0;
mb_dev_nr = 1;
dev_no = MKDEV(mb_dev_major, mb_dev_minor);
ret = register_chrdev_region(dev_no, mb_dev_nr, "wmt_mb");
if (ret < 0) {
MB_ERROR("can't get %s device major %d\n",
DEVICE_NAME, mb_dev_major);
return ret;
}
if (wmt_getsyspara("mbsize", buf, &varlen) == 0) {
sscanf(buf, "%dM", &mbtotal);
mbtotal *= 1024;
MB_INFO("Set MB total size %d KB\n", mbtotal);
if (mbtotal > 0)
MB_TOTAL_SIZE = mbtotal;
}
wmt_mbah = NULL;
MBMSG_LEVEL = MBMAX_ORDER = MBMIN_ORDER = USR2PRDT_METHOD = 0;
mb_dev_class = class_create(THIS_MODULE, "mbdev");
device_create(mb_dev_class, NULL, dev_no, NULL, "mbdev");
#ifdef CONFIG_PROC_FS
{
void *proc_buffer = kmalloc(MBPROC_BUFSIZE, GFP_KERNEL);
struct proc_dir_entry *res = NULL;
if (proc_buffer) {
memset(proc_buffer, 0x0, MBPROC_BUFSIZE);
res = create_proc_entry("mbinfo", 0, NULL);
if (res) {
res->read_proc = mb_area_read_proc;
res->data = proc_buffer;
MB_DBG("create MB proc\n");
} else
kfree(proc_buffer);
}
}
#endif
ret = platform_driver_register(&mb_driver);
if (!ret) {
ret = platform_device_register(&mb_device);
if (ret)
platform_driver_unregister(&mb_driver);
}
return ret;
}
/* because mb will not exit from system, not need to release proc resource */
static void __exit mb_exit(void)
{
dev_t dev_no;
platform_driver_unregister(&mb_driver);
platform_device_unregister(&mb_device);
dev_no = MKDEV(mb_dev_major, mb_dev_minor);
device_destroy(mb_dev_class, dev_no);
class_destroy(mb_dev_class);
unregister_chrdev_region(dev_no, mb_dev_nr);
return;
}
fs_initcall(mb_init);
module_exit(mb_exit);
MODULE_AUTHOR("WonderMedia Technologies, Inc.");
MODULE_DESCRIPTION("memory block driver");
MODULE_LICENSE("GPL");
|