1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
*/
/* Uncomment this block to log an error on every VERIFY failure */
/*
* #ifndef VERIFY_PRINT_ERROR
* #define VERIFY_PRINT_ERROR
* #endif
*/
#include <linux/dma-buf.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <linux/completion.h>
#include <linux/pagemap.h>
#include <linux/mm.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/list.h>
#include <linux/hash.h>
#include <linux/msm_ion.h>
#include <soc/qcom/secure_buffer.h>
#include <linux/rpmsg.h>
#include <linux/ipc_logging.h>
#include <soc/qcom/subsystem_notif.h>
#include <soc/qcom/subsystem_restart.h>
#include <soc/qcom/service-notifier.h>
#include <soc/qcom/service-locator.h>
#include <linux/scatterlist.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/dma-contiguous.h>
#include <linux/cma.h>
#include <linux/iommu.h>
#include <linux/sort.h>
#include <linux/cred.h>
#include <linux/dma-iommu.h>
#include <linux/msm_dma_iommu_mapping.h>
#include "adsprpc_compat.h"
#include "adsprpc_shared.h"
#include <soc/qcom/ramdump.h>
#include <linux/delay.h>
#include <linux/debugfs.h>
#include <linux/pm_qos.h>
#include <linux/stat.h>
#include <linux/preempt.h>
#include <linux/of_reserved_mem.h>
#define CREATE_TRACE_POINTS
#include <trace/events/fastrpc.h>
#define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C
#define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D
#define TZ_PIL_AUTH_QDSP6_PROC 1
#define FASTRPC_DMAHANDLE_NOMAP (16)
#define FASTRPC_ENOSUCH 39
#define DEBUGFS_SIZE 3072
#define UL_SIZE 25
#define PID_SIZE 10
#define AUDIO_PDR_ADSP_DTSI_PROPERTY_NAME "qcom,fastrpc-adsp-audio-pdr"
#define AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME "audio_pdr_adsprpc"
#define AUDIO_PDR_ADSP_SERVICE_NAME "avs/audio"
#define ADSP_AUDIOPD_NAME "msm/adsp/audio_pd"
#define SENSORS_PDR_ADSP_DTSI_PROPERTY_NAME "qcom,fastrpc-adsp-sensors-pdr"
#define SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME "sensors_pdr_adsprpc"
#define SENSORS_PDR_ADSP_SERVICE_NAME "tms/servreg"
#define ADSP_SENSORPD_NAME "msm/adsp/sensor_pd"
#define SENSORS_PDR_SLPI_DTSI_PROPERTY_NAME "qcom,fastrpc-slpi-sensors-pdr"
#define SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME "sensors_pdr_sdsprpc"
#define SENSORS_PDR_SLPI_SERVICE_NAME SENSORS_PDR_ADSP_SERVICE_NAME
#define SLPI_SENSORPD_NAME "msm/slpi/sensor_pd"
#define FASTRPC_SECURE_WAKE_SOURCE_CLIENT_NAME "adsprpc-secure"
#define FASTRPC_NON_SECURE_WAKE_SOURCE_CLIENT_NAME "adsprpc-non_secure"
#define RPC_TIMEOUT (5 * HZ)
#define BALIGN 128
#define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/
#define NUM_SESSIONS 13 /* max 12 compute, 1 cpz */
#define M_FDLIST (16)
#define M_CRCLIST (64)
#define SESSION_ID_INDEX (30)
#define FASTRPC_CTX_MAGIC (0xbeeddeed)
/*
* Fastrpc context ID bit-map:
*
* bits 0-3 : type of remote PD
* bit 4 : type of job (sync/async)
* bit 5 : reserved
* bits 6-15 : index in context table
* bits 16-63 : incrementing context ID
*/
#define FASTRPC_CTX_MAX (1024)
#define FASTRPC_CTX_JOB_TYPE_POS (4)
#define FASTRPC_CTX_TABLE_IDX_POS (6)
#define FASTRPC_CTX_JOBID_POS (16)
#define FASTRPC_CTX_TABLE_IDX_MASK \
((FASTRPC_CTX_MAX - 1) << FASTRPC_CTX_TABLE_IDX_POS)
#define FASTRPC_ASYNC_JOB_MASK (1)
#define GET_TABLE_IDX_FROM_CTXID(ctxid) \
((ctxid & FASTRPC_CTX_TABLE_IDX_MASK) >> FASTRPC_CTX_TABLE_IDX_POS)
/* Reserve few entries in context table for critical kernel and static RPC
* calls to avoid user invocations from exhausting all entries.
*/
#define NUM_KERNEL_AND_STATIC_ONLY_CONTEXTS (70)
/* Maximum number of pending contexts per remote session */
#define MAX_PENDING_CTX_PER_SESSION (64)
#define NUM_DEVICES 2 /* adsprpc-smd, adsprpc-smd-secure */
#define MINOR_NUM_DEV 0
#define MINOR_NUM_SECURE_DEV 1
#define NON_SECURE_CHANNEL 0
#define SECURE_CHANNEL 1
#define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0)
#ifndef ION_FLAG_CACHED
#define ION_FLAG_CACHED (1)
#endif
#define ADSP_DOMAIN_ID (0)
#define MDSP_DOMAIN_ID (1)
#define SDSP_DOMAIN_ID (2)
#define CDSP_DOMAIN_ID (3)
/*
* ctxid of every message is OR-ed with fl->pd (0/1/2) before
* it is sent to DSP. So mask 2 LSBs to retrieve actual context
*/
#define CONTEXT_PD_CHECK (3)
#define RH_CID ADSP_DOMAIN_ID
#define PERF_KEYS \
"count:flush:map:copy:rpmsg:getargs:putargs:invalidate:invoke"
#define FASTRPC_STATIC_HANDLE_PROCESS_GROUP (1)
#define FASTRPC_STATIC_HANDLE_DSP_UTILITIES (2)
#define FASTRPC_STATIC_HANDLE_LISTENER (3)
#define FASTRPC_STATIC_HANDLE_MAX (20)
#define FASTRPC_LATENCY_CTRL_ENB (1)
/* Maximum PM timeout that can be voted through fastrpc */
#define MAX_PM_TIMEOUT_MS 50
/* timeout in us for busy polling after early response from remote processor */
#define FASTRPC_POLL_TIME (4000)
/* timeout in us for polling without preempt */
#define FASTRPC_POLL_TIME_WITHOUT_PREEMPT (500)
/* timeout in us for polling completion signal after user early hint */
#define FASTRPC_USER_EARLY_HINT_TIMEOUT (500)
/* Early wake up poll completion number received from remote processor */
#define FASTRPC_EARLY_WAKEUP_POLL (0xabbccdde)
/* latency in us, early wake up signal used below this value */
#define FASTRPC_EARLY_WAKEUP_LATENCY (200)
/* response version number */
#define FASTRPC_RSP_VERSION2 (2)
/* CPU feature information to DSP */
#define FASTRPC_CPUINFO_DEFAULT (0)
#define FASTRPC_CPUINFO_EARLY_WAKEUP (1)
#define INIT_FILELEN_MAX (2*1024*1024)
#define INIT_MEMLEN_MAX (8*1024*1024)
#define MAX_CACHE_BUF_SIZE (8*1024*1024)
/* Maximum buffers cached in cached buffer list */
#define MAX_CACHED_BUFS (32)
/* Max no. of persistent headers pre-allocated per process */
#define MAX_PERSISTENT_HEADERS (25)
/* Length of glink transaction history to store */
#define GLINK_MSG_HISTORY_LEN (128)
/* Fastrpc remote process attributes */
enum fastrpc_proc_attr {
FASTRPC_MODE_UNSIGNED_MODULE = (1 << 3),
FASTRPC_MODE_PRIVILEGED = (1 << 6),
};
/* Type of fastrpc DMA bufs sent to DSP */
enum fastrpc_buf_type {
METADATA_BUF,
COPYDATA_BUF,
INITMEM_BUF,
USERHEAP_BUF,
};
/* Types of RPC calls to DSP */
enum fastrpc_msg_type {
USER_MSG = 0,
KERNEL_MSG_WITH_ZERO_PID,
KERNEL_MSG_WITH_NONZERO_PID,
};
#define PERF_END (void)0
#define PERF(enb, cnt, ff) \
{\
struct timespec64 startT = {0};\
int64_t *counter = cnt;\
if (enb && counter) {\
ktime_get_real_ts64(&startT);\
} \
ff ;\
if (enb && counter) {\
*counter += getnstimediff(&startT);\
} \
}
#define GET_COUNTER(perf_ptr, offset) \
(perf_ptr != NULL ?\
(((offset >= 0) && (offset < PERF_KEY_MAX)) ?\
(int64_t *)(perf_ptr + offset)\
: (int64_t *)NULL) : (int64_t *)NULL)
/* Macro for comparing local client and PD names with those from callback */
#define COMPARE_SERVICE_LOCATOR_NAMES(cb_client, local_client, \
cb_pdname, local_pdname) \
((!strcmp(cb_client, local_client)) \
&& (!strcmp(cb_pdname, local_pdname)))
#define IS_ASYNC_FASTRPC_AVAILABLE (1)
static int fastrpc_pdr_notifier_cb(struct notifier_block *nb,
unsigned long code,
void *data);
static struct dentry *debugfs_root;
static struct dentry *debugfs_global_file;
static inline uint64_t buf_page_start(uint64_t buf)
{
uint64_t start = (uint64_t) buf & PAGE_MASK;
return start;
}
static inline uint64_t buf_page_offset(uint64_t buf)
{
uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1);
return offset;
}
static inline uint64_t buf_num_pages(uint64_t buf, size_t len)
{
uint64_t start = buf_page_start(buf) >> PAGE_SHIFT;
uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
uint64_t nPages = end - start + 1;
return nPages;
}
static inline uint64_t buf_page_size(uint32_t size)
{
uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
return sz > PAGE_SIZE ? sz : PAGE_SIZE;
}
static inline void *uint64_to_ptr(uint64_t addr)
{
void *ptr = (void *)((uintptr_t)addr);
return ptr;
}
static inline uint64_t ptr_to_uint64(void *ptr)
{
uint64_t addr = (uint64_t)((uintptr_t)ptr);
return addr;
}
struct secure_vm {
int *vmid;
int *vmperm;
int vmcount;
};
struct gid_list {
unsigned int *gids;
unsigned int gidcount;
};
struct qos_cores {
int *coreno;
int corecount;
};
struct fastrpc_file;
struct fastrpc_buf {
struct hlist_node hn;
struct hlist_node hn_rem;
struct fastrpc_file *fl;
void *virt;
uint64_t phys;
size_t size;
unsigned long dma_attr;
uintptr_t raddr;
uint32_t flags;
int type; /* One of "fastrpc_buf_type" */
bool in_use; /* Used only for persistent header buffers */
};
struct fastrpc_ctx_lst;
struct fastrpc_tx_msg {
struct smq_msg msg; /* Msg sent to remote subsystem */
int rpmsg_send_err; /* rpmsg error */
int64_t ns; /* Timestamp (in ns) of msg */
};
struct fastrpc_rx_msg {
struct smq_invoke_rspv2 rsp; /* Response from remote subsystem */
int64_t ns; /* Timestamp (in ns) of response */
};
struct fastrpc_rpmsg_log {
unsigned int tx_index; /* Current index of 'tx_msgs' array */
unsigned int rx_index; /* Current index of 'rx_msgs' array */
/* Rolling history of messages sent to remote subsystem */
struct fastrpc_tx_msg tx_msgs[GLINK_MSG_HISTORY_LEN];
/* Rolling history of responses from remote subsystem */
struct fastrpc_rx_msg rx_msgs[GLINK_MSG_HISTORY_LEN];
spinlock_t lock;
};
struct overlap {
uintptr_t start;
uintptr_t end;
int raix;
uintptr_t mstart;
uintptr_t mend;
uintptr_t offset;
int do_cmo; /*used for cache maintenance of inrout buffers*/
};
struct smq_invoke_ctx {
struct hlist_node hn;
/* Async node to add to async job ctx list */
struct list_head asyncn;
struct completion work;
int retval;
int pid;
int tgid;
remote_arg_t *lpra;
remote_arg64_t *rpra;
remote_arg64_t *lrpra; /* Local copy of rpra for put_args */
int *fds;
unsigned int *attrs;
struct fastrpc_mmap **maps;
struct fastrpc_buf *buf;
struct fastrpc_buf *copybuf; /*used to copy non-ion buffers */
size_t used;
struct fastrpc_file *fl;
uint32_t handle;
uint32_t sc;
struct overlap *overs;
struct overlap **overps;
struct smq_msg msg;
uint32_t *crc;
unsigned int magic;
uint64_t ctxid;
/* response flags from remote processor */
enum fastrpc_response_flags rsp_flags;
/* user hint of completion time in us */
uint32_t early_wake_time;
/* work done status flag */
bool is_work_done;
/* Store Async job in the context*/
struct fastrpc_async_job asyncjob;
/* Async early flag to check the state of context */
bool is_early_wakeup;
};
struct fastrpc_ctx_lst {
struct hlist_head pending;
struct hlist_head interrupted;
/* Number of active contexts queued to DSP */
uint32_t num_active_ctxs;
/* Queue which holds all async job contexts of process */
struct list_head async_queue;
};
struct fastrpc_smmu {
struct device *dev;
const char *dev_name;
int cb;
int enabled;
int faults;
int secure;
int coherent;
int best_fit;
};
struct fastrpc_session_ctx {
struct device *dev;
struct fastrpc_smmu smmu;
int used;
};
struct fastrpc_static_pd {
char *servloc_name;
char *spdname;
struct notifier_block pdrnb;
struct notifier_block get_service_nb;
void *pdrhandle;
uint64_t pdrcount;
uint64_t prevpdrcount;
int ispdup;
int cid;
};
struct fastrpc_dsp_capabilities {
uint32_t is_cached; //! Flag if dsp attributes are cached
uint32_t dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
};
struct fastrpc_channel_ctx {
char *name;
char *subsys;
struct rpmsg_device *rpdev;
struct device *dev;
struct fastrpc_session_ctx session[NUM_SESSIONS];
struct fastrpc_static_pd spd[NUM_SESSIONS];
struct completion work;
struct completion workport;
struct notifier_block nb;
struct mutex smd_mutex;
struct mutex rpmsg_mutex;
uint64_t sesscount;
uint64_t ssrcount;
void *handle;
uint64_t prevssrcount;
int issubsystemup;
int vmid;
struct secure_vm rhvm;
int ramdumpenabled;
void *rh_dump_dev;
/* Indicates, if channel is restricted to secure node only */
int secure;
/* Indicates whether the channel supports unsigned PD */
bool unsigned_support;
struct fastrpc_dsp_capabilities dsp_cap_kernel;
/* cpu capabilities shared to DSP */
uint64_t cpuinfo_todsp;
bool cpuinfo_status;
struct smq_invoke_ctx *ctxtable[FASTRPC_CTX_MAX];
spinlock_t ctxlock;
struct fastrpc_rpmsg_log gmsg_log;
};
struct fastrpc_apps {
struct fastrpc_channel_ctx *channel;
struct cdev cdev;
struct class *class;
struct smq_phy_page range;
struct hlist_head maps;
uint32_t staticpd_flags;
dev_t dev_no;
int compat;
struct hlist_head drivers;
spinlock_t hlock;
struct device *dev;
unsigned int latency;
int rpmsg_register;
bool legacy_remote_heap;
/* Unique job id for each message */
uint64_t jobid[NUM_CHANNELS];
struct gid_list gidlist;
struct device *secure_dev;
struct device *non_secure_dev;
/* Secure subsystems like ADSP/SLPI will use secure client */
struct wakeup_source *wake_source_secure;
/* Non-secure subsystem like CDSP will use regular client */
struct wakeup_source *wake_source;
uint32_t duplicate_rsp_err_cnt;
struct qos_cores silvercores;
uint32_t max_size_limit;
};
struct fastrpc_mmap {
struct hlist_node hn;
struct fastrpc_file *fl;
struct fastrpc_apps *apps;
int fd;
uint32_t flags;
struct dma_buf *buf;
struct sg_table *table;
struct dma_buf_attachment *attach;
struct ion_handle *handle;
uint64_t phys;
size_t size;
uintptr_t va;
size_t len;
int refs;
uintptr_t raddr;
int uncached;
int secure;
uintptr_t attr;
};
enum fastrpc_perfkeys {
PERF_COUNT = 0,
PERF_FLUSH = 1,
PERF_MAP = 2,
PERF_COPY = 3,
PERF_LINK = 4,
PERF_GETARGS = 5,
PERF_PUTARGS = 6,
PERF_INVARGS = 7,
PERF_INVOKE = 8,
PERF_KEY_MAX = 9,
};
struct fastrpc_perf {
int64_t count;
int64_t flush;
int64_t map;
int64_t copy;
int64_t link;
int64_t getargs;
int64_t putargs;
int64_t invargs;
int64_t invoke;
int64_t tid;
struct hlist_node hn;
};
struct fastrpc_file {
struct hlist_node hn;
spinlock_t hlock;
struct hlist_head maps;
struct hlist_head cached_bufs;
uint32_t num_cached_buf;
struct hlist_head remote_bufs;
struct fastrpc_ctx_lst clst;
struct fastrpc_session_ctx *sctx;
struct fastrpc_buf *init_mem;
/* No. of persistent headers */
unsigned int num_pers_hdrs;
/* Pre-allocated header buffer */
struct fastrpc_buf *pers_hdr_buf;
/* Pre-allocated buffer divided into N chunks */
struct fastrpc_buf *hdr_bufs;
struct fastrpc_session_ctx *secsctx;
uint32_t mode;
uint32_t profile;
int sessionid;
int tgid_open; /* Process ID during device open */
int tgid; /* Process ID that uses device for RPC calls */
int cid;
uint64_t ssrcount;
int pd;
char *servloc_name;
int file_close;
int dsp_proc_init;
struct fastrpc_apps *apps;
struct hlist_head perf;
struct dentry *debugfs_file;
struct mutex perf_mutex;
struct dev_pm_qos_request *dev_pm_qos_req;
int qos_request;
struct mutex map_mutex;
struct mutex internal_map_mutex;
/* Identifies the device (MINOR_NUM_DEV / MINOR_NUM_SECURE_DEV) */
int dev_minor;
char *debug_buf;
/* Flag to indicate attempt has been made to allocate memory for debug_buf*/
int debug_buf_alloced_attempted;
/* Flag to enable PM wake/relax voting for every remote invoke */
int wake_enable;
struct gid_list gidlist;
/* Number of jobs pending in Async Queue */
atomic_t async_queue_job_count;
/* Async wait queue to synchronize glink response and async thread */
wait_queue_head_t async_wait_queue;
/* IRQ safe spin lock for protecting async queue */
spinlock_t aqlock;
uint32_t ws_timeout;
bool untrusted_process;
};
static struct fastrpc_apps gfa;
static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = {
{
.name = "adsprpc-smd",
.subsys = "adsp",
.spd = {
{
.servloc_name =
AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
.spdname = ADSP_AUDIOPD_NAME,
.pdrnb.notifier_call =
fastrpc_pdr_notifier_cb,
.cid = ADSP_DOMAIN_ID,
},
{
.servloc_name =
SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME,
.spdname = ADSP_SENSORPD_NAME,
.pdrnb.notifier_call =
fastrpc_pdr_notifier_cb,
.cid = ADSP_DOMAIN_ID,
}
},
.cpuinfo_todsp = FASTRPC_CPUINFO_DEFAULT,
.cpuinfo_status = false,
},
{
.name = "mdsprpc-smd",
.subsys = "modem",
.spd = {
{
.cid = MDSP_DOMAIN_ID,
}
},
.cpuinfo_todsp = FASTRPC_CPUINFO_DEFAULT,
.cpuinfo_status = false,
},
{
.name = "sdsprpc-smd",
.subsys = "slpi",
.spd = {
{
.servloc_name =
SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME,
.spdname = SLPI_SENSORPD_NAME,
.pdrnb.notifier_call =
fastrpc_pdr_notifier_cb,
.cid = SDSP_DOMAIN_ID,
}
},
.cpuinfo_todsp = FASTRPC_CPUINFO_DEFAULT,
.cpuinfo_status = false,
},
{
.name = "cdsprpc-smd",
.subsys = "cdsp",
.spd = {
{
.cid = CDSP_DOMAIN_ID,
}
},
.cpuinfo_todsp = FASTRPC_CPUINFO_EARLY_WAKEUP,
.cpuinfo_status = false,
},
};
static int hlosvm[1] = {VMID_HLOS};
static int hlosvmperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
static uint32_t kernel_capabilities[FASTRPC_MAX_ATTRIBUTES -
FASTRPC_MAX_DSP_ATTRIBUTES] = {
1,
/* PERF_LOGGING_V2_SUPPORT feature is supported, unsupported = 0 */
1
/* Fastrpc Driver error code changes present */
};
static inline void fastrpc_pm_awake(struct fastrpc_file *fl, int channel_type);
static int fastrpc_mem_map_to_dsp(struct fastrpc_file *fl, int fd, int offset,
uint32_t flags, uintptr_t va, uint64_t phys,
size_t size, uintptr_t *raddr);
static inline int64_t getnstimediff(struct timespec64 *start)
{
int64_t ns;
struct timespec64 ts, b;
ktime_get_real_ts64(&ts);
b = timespec64_sub(ts, *start);
ns = timespec64_to_ns(&b);
return ns;
}
/**
* get_timestamp_in_ns - Gets time of day in nanoseconds
*
* Returns: Timestamp in nanoseconds
*/
static inline int64_t get_timestamp_in_ns(void)
{
int64_t ns = 0;
struct timespec64 ts;
ktime_get_real_ts64(&ts);
ns = timespec64_to_ns(&ts);
return ns;
}
static inline int64_t *getperfcounter(struct fastrpc_file *fl, int key)
{
int err = 0;
int64_t *val = NULL;
struct fastrpc_perf *perf = NULL, *fperf = NULL;
struct hlist_node *n = NULL;
VERIFY(err, !IS_ERR_OR_NULL(fl));
if (err)
goto bail;
mutex_lock(&fl->perf_mutex);
hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
if (perf->tid == current->pid) {
fperf = perf;
break;
}
}
if (IS_ERR_OR_NULL(fperf)) {
fperf = kzalloc(sizeof(*fperf), GFP_KERNEL);
VERIFY(err, !IS_ERR_OR_NULL(fperf));
if (err) {
mutex_unlock(&fl->perf_mutex);
kfree(fperf);
goto bail;
}
fperf->tid = current->pid;
hlist_add_head(&fperf->hn, &fl->perf);
}
val = ((int64_t *)fperf) + key;
mutex_unlock(&fl->perf_mutex);
bail:
return val;
}
static inline int poll_on_early_response(struct smq_invoke_ctx *ctx)
{
int ii, jj, err = -EIO;
uint32_t sc = ctx->sc;
struct smq_invoke_buf *list;
struct smq_phy_page *pages;
uint64_t *fdlist;
uint32_t *crclist, *poll;
unsigned int inbufs, outbufs, handles;
/* calculate poll memory location */
inbufs = REMOTE_SCALARS_INBUFS(sc);
outbufs = REMOTE_SCALARS_OUTBUFS(sc);
handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
list = smq_invoke_buf_start(ctx->rpra, sc);
pages = smq_phy_page_start(sc, list);
fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
crclist = (uint32_t *)(fdlist + M_FDLIST);
poll = (uint32_t *)(crclist + M_CRCLIST);
/*
* poll on memory for actual completion after receiving
* early response from DSP. Return failure on timeout.
*/
preempt_disable();
for (ii = 0, jj = 0; ii < FASTRPC_POLL_TIME; ii++, jj++) {
if (*poll == FASTRPC_EARLY_WAKEUP_POLL) {
err = 0;
break;
}
if (jj == FASTRPC_POLL_TIME_WITHOUT_PREEMPT) {
/* limit preempt disable time with no rescheduling */
preempt_enable();
/* Wait for DSP to finish updating poll memory */
rmb();
preempt_disable();
jj = 0;
}
udelay(1);
}
preempt_enable();
return err;
}
/**
* fastrpc_update_txmsg_buf - Update history of sent glink messages
* @chan : Channel context
* @msg : Pointer to RPC message to remote subsystem
* @rpmsg_send_err : Error from rpmsg
* @ns : Timestamp (in ns) of sent message
*
* Returns none
*/
static inline void fastrpc_update_txmsg_buf(struct fastrpc_channel_ctx *chan,
struct smq_msg *msg, int rpmsg_send_err, int64_t ns)
{
unsigned long flags = 0;
unsigned int tx_index = 0;
struct fastrpc_tx_msg *tx_msg = NULL;
spin_lock_irqsave(&chan->gmsg_log.lock, flags);
tx_index = chan->gmsg_log.tx_index;
tx_msg = &chan->gmsg_log.tx_msgs[tx_index];
memcpy(&tx_msg->msg, msg, sizeof(struct smq_msg));
tx_msg->rpmsg_send_err = rpmsg_send_err;
tx_msg->ns = ns;
tx_index++;
chan->gmsg_log.tx_index =
(tx_index > (GLINK_MSG_HISTORY_LEN - 1)) ? 0 : tx_index;
spin_unlock_irqrestore(&chan->gmsg_log.lock, flags);
}
/**
* fastrpc_update_rxmsg_buf - Update history of received glink responses
* @chan : Channel context
* @ctx : Context of received response from DSP
* @retval : Return value for RPC call
* @rsp_flags : Response type
* @early_wake_time : Poll time for early wakeup
* @ver : Version of response
* @ns : Timestamp (in ns) of response
*
* Returns none
*/
static inline void fastrpc_update_rxmsg_buf(struct fastrpc_channel_ctx *chan,
uint64_t ctx, int retval, uint32_t rsp_flags,
uint32_t early_wake_time, uint32_t ver, int64_t ns)
{
unsigned long flags = 0;
unsigned int rx_index = 0;
struct fastrpc_rx_msg *rx_msg = NULL;
struct smq_invoke_rspv2 *rsp = NULL;
spin_lock_irqsave(&chan->gmsg_log.lock, flags);
rx_index = chan->gmsg_log.rx_index;
rx_msg = &chan->gmsg_log.rx_msgs[rx_index];
rsp = &rx_msg->rsp;
rsp->ctx = ctx;
rsp->retval = retval;
rsp->flags = rsp_flags;
rsp->early_wake_time = early_wake_time;
rsp->version = ver;
rx_msg->ns = ns;
rx_index++;
chan->gmsg_log.rx_index =
(rx_index > (GLINK_MSG_HISTORY_LEN - 1)) ? 0 : rx_index;
spin_unlock_irqrestore(&chan->gmsg_log.lock, flags);
}
static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache)
{
struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl;
int vmid;
if (!fl)
return;
if (buf->in_use) {
/* Don't free persistent header buf. Just mark as available */
spin_lock(&fl->hlock);
buf->in_use = false;
spin_unlock(&fl->hlock);
return;
}
if (cache && buf->size < MAX_CACHE_BUF_SIZE) {
spin_lock(&fl->hlock);
if (fl->num_cached_buf > MAX_CACHED_BUFS) {
spin_unlock(&fl->hlock);
goto skip_buf_cache;
}
hlist_add_head(&buf->hn, &fl->cached_bufs);
fl->num_cached_buf++;
spin_unlock(&fl->hlock);
buf->type = -1;
return;
}
skip_buf_cache:
if (buf->type == USERHEAP_BUF) {
spin_lock(&fl->hlock);
hlist_del_init(&buf->hn_rem);
spin_unlock(&fl->hlock);
buf->raddr = 0;
}
if (!IS_ERR_OR_NULL(buf->virt)) {
int destVM[1] = {VMID_HLOS};
int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
if (fl->sctx->smmu.cb)
buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32);
vmid = fl->apps->channel[fl->cid].vmid;
if (vmid) {
int srcVM[2] = {VMID_HLOS, vmid};
int hyp_err = 0;
hyp_err = hyp_assign_phys(buf->phys,
buf_page_size(buf->size),
srcVM, 2, destVM, destVMperm, 1);
if (hyp_err) {
ADSPRPC_ERR(
"rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
hyp_err, buf->phys, buf->size);
}
}
trace_fastrpc_dma_free(fl->cid, buf->phys, buf->size);
dma_free_attrs(fl->sctx->smmu.dev, buf->size, buf->virt,
buf->phys, buf->dma_attr);
}
kfree(buf);
}
static void fastrpc_cached_buf_list_free(struct fastrpc_file *fl)
{
struct fastrpc_buf *buf, *free;
do {
struct hlist_node *n;
free = NULL;
spin_lock(&fl->hlock);
hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
hlist_del_init(&buf->hn);
fl->num_cached_buf--;
free = buf;
break;
}
spin_unlock(&fl->hlock);
if (free)
fastrpc_buf_free(free, 0);
} while (free);
}
static void fastrpc_remote_buf_list_free(struct fastrpc_file *fl)
{
struct fastrpc_buf *buf, *free;
do {
struct hlist_node *n;
free = NULL;
spin_lock(&fl->hlock);
hlist_for_each_entry_safe(buf, n, &fl->remote_bufs, hn_rem) {
free = buf;
break;
}
spin_unlock(&fl->hlock);
if (free)
fastrpc_buf_free(free, 0);
} while (free);
}
static void fastrpc_mmap_add(struct fastrpc_mmap *map)
{
if (map->flags == ADSP_MMAP_HEAP_ADDR ||
map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
struct fastrpc_apps *me = &gfa;
spin_lock(&me->hlock);
hlist_add_head(&map->hn, &me->maps);
spin_unlock(&me->hlock);
} else {
struct fastrpc_file *fl = map->fl;
hlist_add_head(&map->hn, &fl->maps);
}
}
static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd,
uintptr_t va, size_t len, int mflags, int refs,
struct fastrpc_mmap **ppmap)
{
struct fastrpc_apps *me = &gfa;
struct fastrpc_mmap *match = NULL, *map = NULL;
struct hlist_node *n;
if ((va + len) < va)
return -EFAULT;
if (mflags == ADSP_MMAP_HEAP_ADDR ||
mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
spin_lock(&me->hlock);
hlist_for_each_entry_safe(map, n, &me->maps, hn) {
if (va >= map->va &&
va + len <= map->va + map->len &&
map->fd == fd) {
if (refs) {
if (map->refs + 1 == INT_MAX) {
spin_unlock(&me->hlock);
return -ETOOMANYREFS;
}
map->refs++;
}
match = map;
break;
}
}
spin_unlock(&me->hlock);
} else {
hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
if (va >= map->va &&
va + len <= map->va + map->len &&
map->fd == fd) {
if (refs) {
if (map->refs + 1 == INT_MAX)
return -ETOOMANYREFS;
map->refs++;
}
match = map;
break;
}
}
}
if (match) {
*ppmap = match;
return 0;
}
return -ENXIO;
}
static int fastrpc_alloc_cma_memory(dma_addr_t *region_phys, void **vaddr,
size_t size, unsigned long dma_attr)
{
int err = 0;
struct fastrpc_apps *me = &gfa;
if (me->dev == NULL) {
ADSPRPC_ERR(
"failed to allocate CMA memory, device adsprpc-mem is not initialized\n");
return -ENODEV;
}
VERIFY(err, size > 0 && size < me->max_size_limit);
if (err) {
err = -EFAULT;
pr_err("adsprpc: %s: invalid allocation size 0x%zx\n",
__func__, size);
return err;
}
*vaddr = dma_alloc_attrs(me->dev, size, region_phys,
GFP_KERNEL, dma_attr);
if (IS_ERR_OR_NULL(*vaddr)) {
ADSPRPC_ERR(
"dma_alloc_attrs failed for device %s size 0x%zx dma_attr %lu, returned %ld\n",
dev_name(me->dev), size, dma_attr, PTR_ERR(*vaddr));
return -ENOBUFS;
}
return 0;
}
static int fastrpc_mmap_remove(struct fastrpc_file *fl, int fd, uintptr_t va,
size_t len, struct fastrpc_mmap **ppmap)
{
struct fastrpc_mmap *match = NULL, *map;
struct hlist_node *n;
struct fastrpc_apps *me = &gfa;
/*
* Search for a mapping by matching fd, remote address and length.
* For backward compatibility, search for a mapping by matching is
* limited to remote address and length when passed fd < 0.
*/
spin_lock(&me->hlock);
hlist_for_each_entry_safe(map, n, &me->maps, hn) {
if ((fd < 0 || map->fd == fd) && map->raddr == va &&
map->raddr + map->len == va + len &&
map->refs == 1) {
match = map;
hlist_del_init(&map->hn);
break;
}
}
spin_unlock(&me->hlock);
if (match) {
*ppmap = match;
return 0;
}
hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
if ((fd < 0 || map->fd == fd) && map->raddr == va &&
map->raddr + map->len == va + len &&
map->refs == 1) {
match = map;
hlist_del_init(&map->hn);
break;
}
}
if (match) {
*ppmap = match;
return 0;
}
return -ETOOMANYREFS;
}
static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags)
{
struct fastrpc_apps *me = &gfa;
struct fastrpc_file *fl;
int vmid, cid = -1, err = 0;
struct fastrpc_session_ctx *sess;
if (!map)
return;
fl = map->fl;
if (fl && !(map->flags == ADSP_MMAP_HEAP_ADDR ||
map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)) {
cid = fl->cid;
VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
pr_err("adsprpc: ERROR:%s, Invalid channel id: %d, err:%d\n",
__func__, cid, err);
return;
}
}
if (map->flags == ADSP_MMAP_HEAP_ADDR ||
map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
spin_lock(&me->hlock);
map->refs--;
if (!map->refs)
hlist_del_init(&map->hn);
spin_unlock(&me->hlock);
if (map->refs > 0) {
ADSPRPC_WARN(
"multiple references for remote heap size %zu va 0x%lx ref count is %d\n",
map->size, map->va, map->refs);
return;
}
} else {
map->refs--;
if (!map->refs)
hlist_del_init(&map->hn);
if (map->refs > 0 && !flags)
return;
}
if (map->flags == ADSP_MMAP_HEAP_ADDR ||
map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
if (me->dev == NULL) {
ADSPRPC_ERR(
"failed to free remote heap allocation, device is not initialized\n");
return;
}
trace_fastrpc_dma_free(-1, map->phys, map->size);
if (map->phys) {
dma_free_attrs(me->dev, map->size, (void *)map->va,
(dma_addr_t)map->phys, (unsigned long)map->attr);
}
} else if (map->flags == FASTRPC_DMAHANDLE_NOMAP) {
trace_fastrpc_dma_unmap(fl->cid, map->phys, map->size);
if (!IS_ERR_OR_NULL(map->table))
dma_buf_unmap_attachment(map->attach, map->table,
DMA_BIDIRECTIONAL);
if (!IS_ERR_OR_NULL(map->attach))
dma_buf_detach(map->buf, map->attach);
if (!IS_ERR_OR_NULL(map->buf))
dma_buf_put(map->buf);
} else {
int destVM[1] = {VMID_HLOS};
int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
if (map->secure)
sess = fl->secsctx;
else
sess = fl->sctx;
vmid = fl->apps->channel[fl->cid].vmid;
if (vmid && map->phys) {
int hyp_err = 0;
int srcVM[2] = {VMID_HLOS, vmid};
hyp_err = hyp_assign_phys(map->phys,
buf_page_size(map->size),
srcVM, 2, destVM, destVMperm, 1);
if (hyp_err) {
ADSPRPC_ERR(
"rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
hyp_err, map->phys, map->size);
}
}
trace_fastrpc_dma_unmap(fl->cid, map->phys, map->size);
if (!IS_ERR_OR_NULL(map->table))
dma_buf_unmap_attachment(map->attach, map->table,
DMA_BIDIRECTIONAL);
if (!IS_ERR_OR_NULL(map->attach))
dma_buf_detach(map->buf, map->attach);
if (!IS_ERR_OR_NULL(map->buf))
dma_buf_put(map->buf);
}
kfree(map);
}
static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
struct fastrpc_session_ctx **session);
static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd,
unsigned int attr, uintptr_t va, size_t len, int mflags,
struct fastrpc_mmap **ppmap)
{
struct fastrpc_apps *me = &gfa;
struct fastrpc_session_ctx *sess;
struct fastrpc_apps *apps = fl->apps;
int cid = fl->cid;
struct fastrpc_channel_ctx *chan = NULL;
struct fastrpc_mmap *map = NULL;
dma_addr_t region_phys = 0;
void *region_vaddr = NULL;
unsigned long flags;
int err = 0, vmid, sgl_index = 0;
struct scatterlist *sgl = NULL;
VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
chan = &apps->channel[cid];
if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap))
return 0;
map = kzalloc(sizeof(*map), GFP_KERNEL);
VERIFY(err, !IS_ERR_OR_NULL(map));
if (err) {
err = -ENOMEM;
goto bail;
}
INIT_HLIST_NODE(&map->hn);
map->flags = mflags;
map->refs = 1;
map->fl = fl;
map->fd = fd;
map->attr = attr;
if (mflags == ADSP_MMAP_HEAP_ADDR ||
mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
map->apps = me;
map->fl = NULL;
map->attr |= DMA_ATTR_SKIP_ZEROING | DMA_ATTR_NO_KERNEL_MAPPING;
err = fastrpc_alloc_cma_memory(®ion_phys, ®ion_vaddr,
len, (unsigned long) map->attr);
if (err)
goto bail;
trace_fastrpc_dma_alloc(fl->cid, (uint64_t)region_phys, len,
(unsigned long)map->attr, mflags);
map->phys = (uintptr_t)region_phys;
map->size = len;
map->va = (uintptr_t)region_vaddr;
} else if (mflags == FASTRPC_DMAHANDLE_NOMAP) {
VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
if (err) {
ADSPRPC_ERR("dma_buf_get failed for fd %d ret %ld\n",
fd, PTR_ERR(map->buf));
err = -EBADFD;
goto bail;
}
err = dma_buf_get_flags(map->buf, &flags);
if (err) {
ADSPRPC_ERR(
"dma_buf_get_flags failed for fd %d ret %d\n",
fd, err);
err = -EFAULT;
goto bail;
}
map->secure = flags & ION_FLAG_SECURE;
map->uncached = 1;
map->va = 0;
map->phys = 0;
VERIFY(err, !IS_ERR_OR_NULL(map->attach =
dma_buf_attach(map->buf, me->dev)));
if (err) {
ADSPRPC_ERR(
"dma_buf_attach for fd %d failed to map buffer on SMMU device %s ret %ld\n",
fd, dev_name(me->dev), PTR_ERR(map->attach));
err = -EFAULT;
goto bail;
}
map->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
VERIFY(err, !IS_ERR_OR_NULL(map->table =
dma_buf_map_attachment(map->attach,
DMA_BIDIRECTIONAL)));
if (err) {
ADSPRPC_ERR(
"dma_buf_map_attachment for fd %d failed on device %s ret %ld\n",
fd, dev_name(me->dev), PTR_ERR(map->table));
err = -EFAULT;
goto bail;
}
VERIFY(err, map->table->nents == 1);
if (err) {
ADSPRPC_ERR(
"multiple scatter-gather entries (%u) present for NOMAP fd %d\n",
map->table->nents, fd);
err = -EFAULT;
goto bail;
}
map->phys = sg_dma_address(map->table->sgl);
map->size = len;
trace_fastrpc_dma_map(fl->cid, fd, map->phys, map->size,
len, mflags, map->attach->dma_map_attrs);
} else {
if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
ADSPRPC_INFO("buffer mapped with persist attr 0x%x\n",
(unsigned int)map->attr);
map->refs = 2;
}
VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
if (err) {
ADSPRPC_ERR("dma_buf_get failed for fd %d ret %ld\n",
fd, PTR_ERR(map->buf));
err = -EBADFD;
goto bail;
}
err = dma_buf_get_flags(map->buf, &flags);
if (err) {
ADSPRPC_ERR(
"dma_buf_get_flags failed for fd %d ret %d\n",
fd, err);
err = -EFAULT;
goto bail;
}
map->secure = flags & ION_FLAG_SECURE;
if (map->secure) {
if (!fl->secsctx)
err = fastrpc_session_alloc(chan, 1,
&fl->secsctx);
if (err) {
ADSPRPC_ERR(
"fastrpc_session_alloc failed for fd %d ret %d\n",
fd, err);
err = -ENOSR;
goto bail;
}
}
if (map->secure)
sess = fl->secsctx;
else
sess = fl->sctx;
VERIFY(err, !IS_ERR_OR_NULL(sess));
if (err) {
ADSPRPC_ERR(
"session is invalid for fd %d, secure flag %d\n",
fd, map->secure);
goto bail;
}
map->uncached = !(flags & ION_FLAG_CACHED);
if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent)
map->uncached = 1;
VERIFY(err, !IS_ERR_OR_NULL(map->attach =
dma_buf_attach(map->buf, sess->smmu.dev)));
if (err) {
ADSPRPC_ERR(
"dma_buf_attach for fd %d failed to map buffer on SMMU device %s ret %ld\n",
fd, dev_name(sess->smmu.dev),
PTR_ERR(map->attach));
err = -EFAULT;
goto bail;
}
map->attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
map->attach->dma_map_attrs |= DMA_ATTR_EXEC_MAPPING;
/*
* Skip CPU sync if IO Cohernecy is not supported
* or if it is supported but buffer is uncached
*/
if ((sess->smmu.coherent && map->uncached) ||
(!sess->smmu.coherent))
map->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
VERIFY(err, !IS_ERR_OR_NULL(map->table =
dma_buf_map_attachment(map->attach,
DMA_BIDIRECTIONAL)));
if (err) {
ADSPRPC_ERR(
"dma_buf_map_attachment for fd %d failed on device %s ret %ld\n",
fd, dev_name(sess->smmu.dev),
PTR_ERR(map->table));
err = -EFAULT;
goto bail;
}
if (!sess->smmu.enabled) {
VERIFY(err, map->table->nents == 1);
if (err) {
ADSPRPC_ERR(
"multiple scatter-gather entries (%u) present for fd %d mapped on SMMU disabled device\n",
map->table->nents, fd);
err = -EFAULT;
goto bail;
}
}
map->phys = sg_dma_address(map->table->sgl);
if (sess->smmu.cb) {
map->phys += ((uint64_t)sess->smmu.cb << 32);
for_each_sg(map->table->sgl, sgl, map->table->nents,
sgl_index)
map->size += sg_dma_len(sgl);
} else {
map->size = buf_page_size(len);
}
trace_fastrpc_dma_map(fl->cid, fd, map->phys, map->size,
len, mflags, map->attach->dma_map_attrs);
VERIFY(err, map->size >= len && map->size < me->max_size_limit);
if (err) {
err = -EFAULT;
pr_err("adsprpc: %s: invalid map size 0x%zx len 0x%zx\n",
__func__, map->size, len);
goto bail;
}
vmid = fl->apps->channel[fl->cid].vmid;
if (vmid) {
int srcVM[1] = {VMID_HLOS};
int destVM[2] = {VMID_HLOS, vmid};
int destVMperm[2] = {PERM_READ | PERM_WRITE,
PERM_READ | PERM_WRITE | PERM_EXEC};
err = hyp_assign_phys(map->phys,
buf_page_size(map->size),
srcVM, 1, destVM, destVMperm, 2);
if (err) {
ADSPRPC_ERR(
"rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
err, map->phys, map->size);
err = -EADDRNOTAVAIL;
goto bail;
}
}
map->va = va;
}
map->len = len;
fastrpc_mmap_add(map);
*ppmap = map;
bail:
if (err && map)
fastrpc_mmap_free(map, 0);
return err;
}
static inline bool fastrpc_get_cached_buf(struct fastrpc_file *fl,
size_t size, int buf_type, struct fastrpc_buf **obuf)
{
bool found = false;
struct fastrpc_buf *buf = NULL, *fr = NULL;
struct hlist_node *n = NULL;
if (buf_type == USERHEAP_BUF)
goto bail;
/* find the smallest buffer that fits in the cache */
spin_lock(&fl->hlock);
hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
if (buf->size >= size && (!fr || fr->size > buf->size))
fr = buf;
}
if (fr) {
hlist_del_init(&fr->hn);
fl->num_cached_buf--;
}
spin_unlock(&fl->hlock);
if (fr) {
fr->type = buf_type;
*obuf = fr;
found = true;
}
bail:
return found;
}
static inline bool fastrpc_get_persistent_buf(struct fastrpc_file *fl,
size_t size, int buf_type, struct fastrpc_buf **obuf)
{
unsigned int i = 0;
bool found = false;
struct fastrpc_buf *buf = NULL;
spin_lock(&fl->hlock);
if (!fl->num_pers_hdrs)
goto bail;
/*
* Persistent header buffer can be used only if
* metadata length is less than 1 page size.
*/
if (buf_type != METADATA_BUF || size > PAGE_SIZE)
goto bail;
for (i = 0; i < fl->num_pers_hdrs; i++) {
buf = &fl->hdr_bufs[i];
/* If buffer not in use, then assign it for requested alloc */
if (!buf->in_use) {
buf->in_use = true;
*obuf = buf;
found = true;
break;
}
}
bail:
spin_unlock(&fl->hlock);
return found;
}
static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size,
unsigned long dma_attr, uint32_t rflags,
int buf_type, struct fastrpc_buf **obuf)
{
int err = 0, vmid;
struct fastrpc_apps *me = &gfa;
struct fastrpc_buf *buf = NULL;
VERIFY(err, size > 0 && size < me->max_size_limit);
if (err) {
err = -EFAULT;
pr_err("adsprpc: %s: invalid allocation size 0x%zx\n",
__func__, size);
goto bail;
}
VERIFY(err, fl->sctx != NULL);
if (err) {
err = -EBADR;
goto bail;
}
VERIFY(err, size > 0 && fl->sctx->smmu.dev);
if (err) {
err = (fl->sctx->smmu.dev == NULL) ? -ENODEV : err;
goto bail;
}
if (fastrpc_get_persistent_buf(fl, size, buf_type, obuf))
return err;
if (fastrpc_get_cached_buf(fl, size, buf_type, obuf))
return err;
/* If unable to get persistent or cached buf, allocate new buffer */
VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
if (err) {
err = -ENOMEM;
goto bail;
}
INIT_HLIST_NODE(&buf->hn);
buf->fl = fl;
buf->virt = NULL;
buf->phys = 0;
buf->size = size;
buf->dma_attr = dma_attr;
buf->flags = rflags;
buf->raddr = 0;
buf->type = buf_type;
buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
(dma_addr_t *)&buf->phys,
GFP_KERNEL, buf->dma_attr);
if (IS_ERR_OR_NULL(buf->virt)) {
/* free cache and retry */
fastrpc_cached_buf_list_free(fl);
buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
(dma_addr_t *)&buf->phys, GFP_KERNEL,
buf->dma_attr);
VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
}
if (err) {
ADSPRPC_ERR(
"dma_alloc_attrs failed for size 0x%zx, returned %pK\n",
size, buf->virt);
err = -ENOBUFS;
goto bail;
}
if (fl->sctx->smmu.cb)
buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
trace_fastrpc_dma_alloc(fl->cid, buf->phys, size,
dma_attr, (int)rflags);
vmid = fl->apps->channel[fl->cid].vmid;
if (vmid) {
int srcVM[1] = {VMID_HLOS};
int destVM[2] = {VMID_HLOS, vmid};
int destVMperm[2] = {PERM_READ | PERM_WRITE,
PERM_READ | PERM_WRITE | PERM_EXEC};
err = hyp_assign_phys(buf->phys, buf_page_size(size),
srcVM, 1, destVM, destVMperm, 2);
if (err) {
ADSPRPC_DEBUG(
"rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
err, buf->phys, size);
err = -EADDRNOTAVAIL;
goto bail;
}
}
if (buf_type == USERHEAP_BUF) {
INIT_HLIST_NODE(&buf->hn_rem);
spin_lock(&fl->hlock);
hlist_add_head(&buf->hn_rem, &fl->remote_bufs);
spin_unlock(&fl->hlock);
}
*obuf = buf;
bail:
if (err && buf)
fastrpc_buf_free(buf, 0);
return err;
}
static int context_restore_interrupted(struct fastrpc_file *fl,
struct fastrpc_ioctl_invoke_async *inv,
struct smq_invoke_ctx **po)
{
int err = 0;
struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
struct hlist_node *n;
struct fastrpc_ioctl_invoke *invoke = &inv->inv;
spin_lock(&fl->hlock);
hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
if (ictx->pid == current->pid) {
if (invoke->sc != ictx->sc || ictx->fl != fl) {
err = -EINVAL;
ADSPRPC_ERR(
"interrupted sc (0x%x) or fl (%pK) does not match with invoke sc (0x%x) or fl (%pK)\n",
ictx->sc, ictx->fl, invoke->sc, fl);
}
else {
ctx = ictx;
hlist_del_init(&ctx->hn);
hlist_add_head(&ctx->hn, &fl->clst.pending);
}
break;
}
}
spin_unlock(&fl->hlock);
if (ctx)
*po = ctx;
return err;
}
static unsigned int sorted_lists_intersection(unsigned int *listA,
unsigned int lenA, unsigned int *listB, unsigned int lenB)
{
unsigned int i = 0, j = 0;
while (i < lenA && j < lenB) {
if (listA[i] < listB[j])
i++;
else if (listA[i] > listB[j])
j++;
else
return listA[i];
}
return 0;
}
#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
static int uint_cmp_func(const void *p1, const void *p2)
{
unsigned int a1 = *((unsigned int *)p1);
unsigned int a2 = *((unsigned int *)p2);
return CMP(a1, a2);
}
static int overlap_ptr_cmp(const void *a, const void *b)
{
struct overlap *pa = *((struct overlap **)a);
struct overlap *pb = *((struct overlap **)b);
/* sort with lowest starting buffer first */
int st = CMP(pa->start, pb->start);
/* sort with highest ending buffer first */
int ed = CMP(pb->end, pa->end);
return st == 0 ? ed : st;
}
static int context_build_overlap(struct smq_invoke_ctx *ctx)
{
int i, err = 0;
remote_arg_t *lpra = ctx->lpra;
int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
int nbufs = inbufs + outbufs;
struct overlap max;
for (i = 0; i < nbufs; ++i) {
ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
if (lpra[i].buf.len) {
VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
if (err) {
err = -EFAULT;
ADSPRPC_ERR(
"Invalid address 0x%llx and size %zu\n",
(uintptr_t)lpra[i].buf.pv,
lpra[i].buf.len);
goto bail;
}
}
ctx->overs[i].raix = i;
ctx->overps[i] = &ctx->overs[i];
}
sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL);
max.start = 0;
max.end = 0;
for (i = 0; i < nbufs; ++i) {
if (ctx->overps[i]->start < max.end) {
ctx->overps[i]->mstart = max.end;
ctx->overps[i]->mend = ctx->overps[i]->end;
ctx->overps[i]->offset = max.end -
ctx->overps[i]->start;
if (ctx->overps[i]->end > max.end) {
max.end = ctx->overps[i]->end;
} else {
if ((max.raix < inbufs &&
ctx->overps[i]->raix + 1 > inbufs) ||
(ctx->overps[i]->raix < inbufs &&
max.raix + 1 > inbufs))
ctx->overps[i]->do_cmo = 1;
ctx->overps[i]->mend = 0;
ctx->overps[i]->mstart = 0;
}
} else {
ctx->overps[i]->mend = ctx->overps[i]->end;
ctx->overps[i]->mstart = ctx->overps[i]->start;
ctx->overps[i]->offset = 0;
max = *ctx->overps[i];
}
}
bail:
return err;
}
#define K_COPY_FROM_USER(err, kernel, dst, src, size) \
do {\
if (!(kernel))\
err = copy_from_user((dst),\
(void const __user *)(src),\
(size));\
else\
memmove((dst), (src), (size));\
} while (0)
#define K_COPY_TO_USER(err, kernel, dst, src, size) \
do {\
if (!(kernel))\
err = copy_to_user((void __user *)(dst),\
(src), (size));\
else\
memmove((dst), (src), (size));\
} while (0)
static void context_free(struct smq_invoke_ctx *ctx);
static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
struct fastrpc_ioctl_invoke_async *invokefd,
struct smq_invoke_ctx **po)
{
struct fastrpc_apps *me = &gfa;
int err = 0, bufs, ii, size = 0, cid = fl->cid;
struct smq_invoke_ctx *ctx = NULL;
struct fastrpc_ctx_lst *clst = &fl->clst;
struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
struct fastrpc_channel_ctx *chan = NULL;
unsigned long irq_flags = 0;
spin_lock(&fl->hlock);
if (fl->clst.num_active_ctxs > MAX_PENDING_CTX_PER_SESSION &&
!(kernel || invoke->handle < FASTRPC_STATIC_HANDLE_MAX)) {
err = -EDQUOT;
spin_unlock(&fl->hlock);
goto bail;
}
spin_unlock(&fl->hlock);
bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
sizeof(*ctx->fds) * (bufs) +
sizeof(*ctx->attrs) * (bufs) +
sizeof(*ctx->overs) * (bufs) +
sizeof(*ctx->overps) * (bufs);
VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
if (err) {
err = -ENOMEM;
goto bail;
}
INIT_HLIST_NODE(&ctx->hn);
INIT_LIST_HEAD(&ctx->asyncn);
hlist_add_fake(&ctx->hn);
ctx->fl = fl;
ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
ctx->fds = (int *)(&ctx->lpra[bufs]);
ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra,
bufs * sizeof(*ctx->lpra));
if (err) {
ADSPRPC_ERR(
"copy from user failed with %d for remote arguments list\n",
err);
err = -EFAULT;
goto bail;
}
if (invokefd->fds) {
K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
bufs * sizeof(*ctx->fds));
if (err) {
ADSPRPC_ERR(
"copy from user failed with %d for fd list\n",
err);
err = -EFAULT;
goto bail;
}
} else {
ctx->fds = NULL;
}
if (invokefd->attrs) {
K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
bufs * sizeof(*ctx->attrs));
if (err) {
ADSPRPC_ERR(
"copy from user failed with %d for attribute list\n",
err);
err = -EFAULT;
goto bail;
}
}
ctx->crc = (uint32_t *)invokefd->crc;
ctx->handle = invoke->handle;
ctx->sc = invoke->sc;
if (bufs) {
VERIFY(err, 0 == (err = context_build_overlap(ctx)));
if (err)
goto bail;
}
ctx->retval = -1;
ctx->pid = current->pid;
ctx->tgid = fl->tgid;
init_completion(&ctx->work);
ctx->magic = FASTRPC_CTX_MAGIC;
ctx->rsp_flags = NORMAL_RESPONSE;
ctx->is_work_done = false;
ctx->copybuf = NULL;
ctx->is_early_wakeup = false;
if (invokefd->job) {
K_COPY_FROM_USER(err, kernel, &ctx->asyncjob, invokefd->job,
sizeof(ctx->asyncjob));
if (err)
goto bail;
}
chan = &me->channel[cid];
spin_lock_irqsave(&chan->ctxlock, irq_flags);
me->jobid[cid]++;
for (ii = ((kernel || ctx->handle < FASTRPC_STATIC_HANDLE_MAX)
? 0 : NUM_KERNEL_AND_STATIC_ONLY_CONTEXTS);
ii < FASTRPC_CTX_MAX; ii++) {
if (!chan->ctxtable[ii]) {
chan->ctxtable[ii] = ctx;
ctx->ctxid = (me->jobid[cid] << FASTRPC_CTX_JOBID_POS)
| (ii << FASTRPC_CTX_TABLE_IDX_POS)
| ((ctx->asyncjob.isasyncjob &&
FASTRPC_ASYNC_JOB_MASK) << FASTRPC_CTX_JOB_TYPE_POS);
break;
}
}
spin_unlock_irqrestore(&chan->ctxlock, irq_flags);
VERIFY(err, ii < FASTRPC_CTX_MAX);
if (err) {
ADSPRPC_ERR(
"adsprpc: out of context table entries for handle 0x%x, sc 0x%x\n",
ctx->handle, ctx->sc);
err = -ENOKEY;
goto bail;
}
spin_lock(&fl->hlock);
hlist_add_head(&ctx->hn, &clst->pending);
clst->num_active_ctxs++;
spin_unlock(&fl->hlock);
trace_fastrpc_context_alloc((uint64_t)ctx,
ctx->ctxid | fl->pd, ctx->handle, ctx->sc);
*po = ctx;
bail:
if (ctx && err)
context_free(ctx);
return err;
}
static void context_save_interrupted(struct smq_invoke_ctx *ctx)
{
struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
spin_lock(&ctx->fl->hlock);
hlist_del_init(&ctx->hn);
hlist_add_head(&ctx->hn, &clst->interrupted);
spin_unlock(&ctx->fl->hlock);
}
static void context_free(struct smq_invoke_ctx *ctx)
{
uint32_t i = 0;
struct fastrpc_apps *me = &gfa;
int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
REMOTE_SCALARS_OUTBUFS(ctx->sc);
int cid = ctx->fl->cid;
struct fastrpc_channel_ctx *chan = &me->channel[cid];
unsigned long irq_flags = 0;
i = (uint32_t)GET_TABLE_IDX_FROM_CTXID(ctx->ctxid);
spin_lock_irqsave(&chan->ctxlock, irq_flags);
if (i < FASTRPC_CTX_MAX && chan->ctxtable[i] == ctx) {
chan->ctxtable[i] = NULL;
} else {
for (i = 0; i < FASTRPC_CTX_MAX; i++) {
if (chan->ctxtable[i] == ctx) {
chan->ctxtable[i] = NULL;
break;
}
}
}
spin_unlock_irqrestore(&chan->ctxlock, irq_flags);
spin_lock(&ctx->fl->hlock);
if (!hlist_unhashed(&ctx->hn)) {
hlist_del_init(&ctx->hn);
ctx->fl->clst.num_active_ctxs--;
}
spin_unlock(&ctx->fl->hlock);
mutex_lock(&ctx->fl->map_mutex);
for (i = 0; i < nbufs; ++i)
fastrpc_mmap_free(ctx->maps[i], 0);
mutex_unlock(&ctx->fl->map_mutex);
fastrpc_buf_free(ctx->buf, 1);
if (ctx->copybuf != ctx->buf)
fastrpc_buf_free(ctx->copybuf, 1);
kfree(ctx->lrpra);
ctx->lrpra = NULL;
ctx->magic = 0;
ctx->ctxid = 0;
trace_fastrpc_context_free((uint64_t)ctx,
ctx->msg.invoke.header.ctx, ctx->handle, ctx->sc);
kfree(ctx);
}
static void fastrpc_queue_completed_async_job(struct smq_invoke_ctx *ctx)
{
struct fastrpc_file *fl = ctx->fl;
unsigned long flags;
spin_lock_irqsave(&fl->aqlock, flags);
if (ctx->is_early_wakeup)
goto bail;
list_add_tail(&ctx->asyncn, &fl->clst.async_queue);
atomic_add(1, &fl->async_queue_job_count);
ctx->is_early_wakeup = true;
wake_up_interruptible(&fl->async_wait_queue);
bail:
spin_unlock_irqrestore(&fl->aqlock, flags);
}
static void context_notify_user(struct smq_invoke_ctx *ctx,
int retval, uint32_t rsp_flags, uint32_t early_wake_time)
{
fastrpc_pm_awake(ctx->fl, gcinfo[ctx->fl->cid].secure);
ctx->retval = retval;
ctx->rsp_flags = (enum fastrpc_response_flags)rsp_flags;
trace_fastrpc_context_complete(ctx->fl->cid, (uint64_t)ctx, retval,
ctx->msg.invoke.header.ctx, ctx->handle, ctx->sc);
switch (rsp_flags) {
case NORMAL_RESPONSE:
case COMPLETE_SIGNAL:
/* normal and complete response with return value */
ctx->is_work_done = true;
if (ctx->asyncjob.isasyncjob)
fastrpc_queue_completed_async_job(ctx);
complete(&ctx->work);
break;
case USER_EARLY_SIGNAL:
/* user hint of approximate time of completion */
ctx->early_wake_time = early_wake_time;
if (ctx->asyncjob.isasyncjob)
break;
case EARLY_RESPONSE:
/* rpc framework early response with return value */
if (ctx->asyncjob.isasyncjob)
fastrpc_queue_completed_async_job(ctx);
else
complete(&ctx->work);
break;
default:
break;
}
}
static void fastrpc_notify_users(struct fastrpc_file *me)
{
struct smq_invoke_ctx *ictx;
struct hlist_node *n;
spin_lock(&me->hlock);
hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
ictx->is_work_done = true;
ictx->retval = -ECONNRESET;
trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
ictx->retval, ictx->msg.invoke.header.ctx,
ictx->handle, ictx->sc);
if (ictx->asyncjob.isasyncjob)
fastrpc_queue_completed_async_job(ictx);
else
complete(&ictx->work);
}
hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
ictx->is_work_done = true;
ictx->retval = -ECONNRESET;
trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
ictx->retval, ictx->msg.invoke.header.ctx,
ictx->handle, ictx->sc);
complete(&ictx->work);
}
spin_unlock(&me->hlock);
}
static void fastrpc_notify_users_staticpd_pdr(struct fastrpc_file *me)
{
struct smq_invoke_ctx *ictx;
struct hlist_node *n;
spin_lock(&me->hlock);
hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
if (ictx->msg.pid) {
ictx->is_work_done = true;
ictx->retval = -ECONNRESET;
trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
ictx->retval, ictx->msg.invoke.header.ctx,
ictx->handle, ictx->sc);
if (ictx->asyncjob.isasyncjob)
fastrpc_queue_completed_async_job(ictx);
else
complete(&ictx->work);
}
}
hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
if (ictx->msg.pid) {
ictx->is_work_done = true;
ictx->retval = -ECONNRESET;
trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
ictx->retval, ictx->msg.invoke.header.ctx,
ictx->handle, ictx->sc);
complete(&ictx->work);
}
}
spin_unlock(&me->hlock);
}
static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
{
struct fastrpc_file *fl;
struct hlist_node *n;
spin_lock(&me->hlock);
hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
if (fl->cid == cid)
fastrpc_notify_users(fl);
}
spin_unlock(&me->hlock);
}
static void fastrpc_notify_pdr_drivers(struct fastrpc_apps *me,
char *servloc_name)
{
struct fastrpc_file *fl;
struct hlist_node *n;
spin_lock(&me->hlock);
hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
if (fl->servloc_name && !strcmp(servloc_name, fl->servloc_name))
fastrpc_notify_users_staticpd_pdr(fl);
}
spin_unlock(&me->hlock);
}
static void context_list_ctor(struct fastrpc_ctx_lst *me)
{
INIT_HLIST_HEAD(&me->interrupted);
INIT_HLIST_HEAD(&me->pending);
me->num_active_ctxs = 0;
INIT_LIST_HEAD(&me->async_queue);
}
static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
{
struct fastrpc_ctx_lst *clst = &fl->clst;
struct smq_invoke_ctx *ictx = NULL, *ctxfree;
struct hlist_node *n;
do {
ctxfree = NULL;
spin_lock(&fl->hlock);
hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
hlist_del_init(&ictx->hn);
clst->num_active_ctxs--;
ctxfree = ictx;
break;
}
spin_unlock(&fl->hlock);
if (ctxfree)
context_free(ctxfree);
} while (ctxfree);
do {
ctxfree = NULL;
spin_lock(&fl->hlock);
hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
hlist_del_init(&ictx->hn);
clst->num_active_ctxs--;
ctxfree = ictx;
break;
}
spin_unlock(&fl->hlock);
if (ctxfree)
context_free(ctxfree);
} while (ctxfree);
}
static int fastrpc_file_free(struct fastrpc_file *fl);
static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
{
struct fastrpc_file *fl, *free;
struct hlist_node *n;
do {
free = NULL;
spin_lock(&me->hlock);
hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
hlist_del_init(&fl->hn);
free = fl;
break;
}
spin_unlock(&me->hlock);
if (free)
fastrpc_file_free(free);
} while (free);
}
static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
{
remote_arg64_t *rpra, *lrpra;
remote_arg_t *lpra = ctx->lpra;
struct smq_invoke_buf *list;
struct smq_phy_page *pages, *ipage;
uint32_t sc = ctx->sc;
int inbufs = REMOTE_SCALARS_INBUFS(sc);
int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
int handles, bufs = inbufs + outbufs;
uintptr_t args = 0;
size_t rlen = 0, copylen = 0, metalen = 0, lrpralen = 0;
size_t totallen = 0; //header and non ion copy buf len
int i, oix;
int err = 0;
int mflags = 0;
uint64_t *fdlist;
uint32_t *crclist;
uint32_t early_hint;
int64_t *perf_counter = NULL;
if (ctx->fl->profile)
perf_counter = getperfcounter(ctx->fl, PERF_COUNT);
/* calculate size of the metadata */
rpra = NULL;
lrpra = NULL;
list = smq_invoke_buf_start(rpra, sc);
pages = smq_phy_page_start(sc, list);
ipage = pages;
PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
for (i = 0; i < bufs; ++i) {
uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
size_t len = lpra[i].buf.len;
mutex_lock(&ctx->fl->map_mutex);
if (ctx->fds && (ctx->fds[i] != -1))
err = fastrpc_mmap_create(ctx->fl, ctx->fds[i],
ctx->attrs[i], buf, len,
mflags, &ctx->maps[i]);
mutex_unlock(&ctx->fl->map_mutex);
if (err)
goto bail;
ipage += 1;
}
PERF_END);
handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
mutex_lock(&ctx->fl->map_mutex);
for (i = bufs; i < bufs + handles; i++) {
int dmaflags = 0;
if (ctx->attrs && (ctx->attrs[i] & FASTRPC_ATTR_NOMAP))
dmaflags = FASTRPC_DMAHANDLE_NOMAP;
if (ctx->fds && (ctx->fds[i] != -1))
err = fastrpc_mmap_create(ctx->fl, ctx->fds[i],
FASTRPC_ATTR_NOVA, 0, 0, dmaflags,
&ctx->maps[i]);
if (err) {
mutex_unlock(&ctx->fl->map_mutex);
goto bail;
}
ipage += 1;
}
mutex_unlock(&ctx->fl->map_mutex);
/* metalen includes meta data, fds, crc and early wakeup hint */
metalen = totallen = (size_t)&ipage[0] + (sizeof(uint64_t) * M_FDLIST) +
(sizeof(uint32_t) * M_CRCLIST) + sizeof(early_hint);
if (metalen) {
err = fastrpc_buf_alloc(ctx->fl, metalen, 0, 0,
METADATA_BUF, &ctx->buf);
if (err)
goto bail;
VERIFY(err, !IS_ERR_OR_NULL(ctx->buf->virt));
if (err)
goto bail;
memset(ctx->buf->virt, 0, metalen);
}
ctx->used = metalen;
/* allocate new local rpra buffer */
lrpralen = (size_t)&list[0];
if (lrpralen) {
lrpra = kzalloc(lrpralen, GFP_KERNEL);
VERIFY(err, !IS_ERR_OR_NULL(lrpra));
if (err)
goto bail;
}
ctx->lrpra = lrpra;
/* calculate len required for copying */
for (oix = 0; oix < inbufs + outbufs; ++oix) {
int i = ctx->overps[oix]->raix;
uintptr_t mstart, mend;
size_t len = lpra[i].buf.len;
if (!len)
continue;
if (ctx->maps[i])
continue;
if (ctx->overps[oix]->offset == 0)
copylen = ALIGN(copylen, BALIGN);
mstart = ctx->overps[oix]->mstart;
mend = ctx->overps[oix]->mend;
VERIFY(err, (mend - mstart) <= LONG_MAX);
if (err) {
err = -EFAULT;
goto bail;
}
copylen += mend - mstart;
}
totallen = ALIGN(totallen, BALIGN) + copylen;
/* allocate non -ion copy buffer */
/* Checking if copylen can be accomodated in metalen*/
/*if not allocating new buffer */
if (totallen <= (size_t)buf_page_size(metalen)) {
args = (uintptr_t)ctx->buf->virt + metalen;
ctx->copybuf = ctx->buf;
rlen = totallen - metalen;
} else if (copylen) {
err = fastrpc_buf_alloc(ctx->fl, copylen, 0, 0, COPYDATA_BUF,
&ctx->copybuf);
if (err)
goto bail;
memset(ctx->copybuf->virt, 0, copylen);
args = (uintptr_t)ctx->copybuf->virt;
rlen = copylen;
totallen = copylen;
}
/* copy metadata */
rpra = ctx->buf->virt;
ctx->rpra = rpra;
list = smq_invoke_buf_start(rpra, sc);
pages = smq_phy_page_start(sc, list);
ipage = pages;
for (i = 0; i < bufs + handles; ++i) {
if (lpra[i].buf.len)
list[i].num = 1;
else
list[i].num = 0;
list[i].pgidx = ipage - pages;
ipage++;
}
/* map ion buffers */
PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
for (i = 0; rpra && i < inbufs + outbufs; ++i) {
struct fastrpc_mmap *map = ctx->maps[i];
uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
size_t len = lpra[i].buf.len;
rpra[i].buf.pv = 0;
rpra[i].buf.len = len;
if (!len)
continue;
if (map) {
struct vm_area_struct *vma;
uintptr_t offset;
uint64_t num = buf_num_pages(buf, len);
int idx = list[i].pgidx;
if (map->attr & FASTRPC_ATTR_NOVA) {
offset = 0;
} else {
down_read(¤t->mm->mmap_sem);
VERIFY(err, NULL != (vma = find_vma(current->mm,
map->va)));
if (err) {
up_read(¤t->mm->mmap_sem);
goto bail;
}
offset = buf_page_start(buf) - vma->vm_start;
up_read(¤t->mm->mmap_sem);
VERIFY(err, offset < (uintptr_t)map->size);
if (err) {
ADSPRPC_ERR(
"buffer address is invalid for the fd passed for %d address 0x%llx and size %zu\n",
i, (uintptr_t)lpra[i].buf.pv,
lpra[i].buf.len);
err = -EFAULT;
goto bail;
}
}
pages[idx].addr = map->phys + offset;
pages[idx].size = num << PAGE_SHIFT;
}
rpra[i].buf.pv = buf;
}
PERF_END);
for (i = bufs; i < bufs + handles; ++i) {
struct fastrpc_mmap *map = ctx->maps[i];
if (map) {
pages[i].addr = map->phys;
pages[i].size = map->size;
}
}
fdlist = (uint64_t *)&pages[bufs + handles];
crclist = (uint32_t *)&fdlist[M_FDLIST];
/* reset fds, crc and early wakeup hint memory */
/* remote process updates these values before responding */
memset(fdlist, 0, sizeof(uint64_t)*M_FDLIST +
sizeof(uint32_t)*M_CRCLIST + sizeof(early_hint));
/* copy non ion buffers */
PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY),
for (oix = 0; rpra && oix < inbufs + outbufs; ++oix) {
int i = ctx->overps[oix]->raix;
struct fastrpc_mmap *map = ctx->maps[i];
size_t mlen;
uint64_t buf;
size_t len = lpra[i].buf.len;
if (!len)
continue;
if (map)
continue;
if (ctx->overps[oix]->offset == 0) {
rlen -= ALIGN(args, BALIGN) - args;
args = ALIGN(args, BALIGN);
}
mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
VERIFY(err, rlen >= mlen);
if (err)
goto bail;
rpra[i].buf.pv =
(args - ctx->overps[oix]->offset);
pages[list[i].pgidx].addr = ctx->copybuf->phys -
ctx->overps[oix]->offset +
(totallen - rlen);
pages[list[i].pgidx].addr =
buf_page_start(pages[list[i].pgidx].addr);
buf = rpra[i].buf.pv;
pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
if (i < inbufs) {
K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
lpra[i].buf.pv, len);
if (err) {
ADSPRPC_ERR(
"copy from user failed with %d for dst 0x%llx, src %pK, size 0x%zx, arg %d\n",
err, buf, lpra[i].buf.pv, len, i+1);
err = -EFAULT;
goto bail;
}
}
if (len > DEBUG_PRINT_SIZE_LIMIT)
ADSPRPC_DEBUG(
"copied non ion buffer sc 0x%x pv 0x%llx, mend 0x%llx mstart 0x%llx, len %zu\n",
sc, rpra[i].buf.pv,
ctx->overps[oix]->mend,
ctx->overps[oix]->mstart, len);
args = args + mlen;
rlen -= mlen;
}
PERF_END);
PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
for (oix = 0; oix < inbufs + outbufs; ++oix) {
int i = ctx->overps[oix]->raix;
struct fastrpc_mmap *map = ctx->maps[i];
if (i+1 > inbufs) // Avoiding flush for outbufs
continue;
if (map && map->uncached)
continue;
if (ctx->fl->sctx->smmu.coherent)
continue;
if (map && (map->attr & FASTRPC_ATTR_FORCE_NOFLUSH))
continue;
if (rpra && rpra[i].buf.len && (ctx->overps[oix]->mstart ||
ctx->overps[oix]->do_cmo == 1)) {
if (map && map->buf) {
if (((buf_page_size(ctx->overps[oix]->mend -
ctx->overps[oix]->mstart)) == map->size) ||
ctx->overps[oix]->do_cmo) {
dma_buf_begin_cpu_access(map->buf,
DMA_TO_DEVICE);
dma_buf_end_cpu_access(map->buf,
DMA_TO_DEVICE);
ADSPRPC_DEBUG(
"sc 0x%x pv 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
sc, rpra[i].buf.pv,
ctx->overps[oix]->mend,
ctx->overps[oix]->mstart,
rpra[i].buf.len, map->size);
} else {
uintptr_t offset;
uint64_t flush_len;
struct vm_area_struct *vma;
down_read(¤t->mm->mmap_sem);
VERIFY(err, NULL != (vma = find_vma(
current->mm, rpra[i].buf.pv)));
if (err) {
up_read(¤t->mm->mmap_sem);
goto bail;
}
if (ctx->overps[oix]->do_cmo) {
offset = rpra[i].buf.pv -
vma->vm_start;
flush_len = rpra[i].buf.len;
} else {
offset =
ctx->overps[oix]->mstart
- vma->vm_start;
flush_len =
ctx->overps[oix]->mend -
ctx->overps[oix]->mstart;
}
up_read(¤t->mm->mmap_sem);
dma_buf_begin_cpu_access_partial(
map->buf, DMA_TO_DEVICE, offset,
flush_len);
dma_buf_end_cpu_access_partial(
map->buf, DMA_TO_DEVICE, offset,
flush_len);
ADSPRPC_DEBUG(
"sc 0x%x vm_start 0x%llx pv 0x%llx, offset 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
sc, vma->vm_start,
rpra[i].buf.pv, offset,
ctx->overps[oix]->mend,
ctx->overps[oix]->mstart,
rpra[i].buf.len, map->size);
}
}
}
}
PERF_END);
for (i = bufs; ctx->fds && rpra && i < bufs + handles; i++) {
rpra[i].dma.fd = ctx->fds[i];
rpra[i].dma.len = (uint32_t)lpra[i].buf.len;
rpra[i].dma.offset =
(uint32_t)(uintptr_t)lpra[i].buf.pv;
}
/* Copy rpra to local buffer */
if (ctx->lrpra && rpra && lrpralen > 0)
memcpy(ctx->lrpra, rpra, lrpralen);
bail:
return err;
}
static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
remote_arg_t *upra)
{
uint32_t sc = ctx->sc;
struct smq_invoke_buf *list;
struct smq_phy_page *pages;
struct fastrpc_mmap *mmap;
uint64_t *fdlist;
uint32_t *crclist = NULL;
remote_arg64_t *rpra = ctx->lrpra;
int i, inbufs, outbufs, handles;
int err = 0;
inbufs = REMOTE_SCALARS_INBUFS(sc);
outbufs = REMOTE_SCALARS_OUTBUFS(sc);
handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
list = smq_invoke_buf_start(ctx->rpra, sc);
pages = smq_phy_page_start(sc, list);
fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
crclist = (uint32_t *)(fdlist + M_FDLIST);
for (i = inbufs; i < inbufs + outbufs; ++i) {
if (!ctx->maps[i]) {
K_COPY_TO_USER(err, kernel,
ctx->lpra[i].buf.pv,
uint64_to_ptr(rpra[i].buf.pv),
rpra[i].buf.len);
if (err) {
ADSPRPC_ERR(
"Invalid size 0x%llx for output argument %d ret %ld\n",
rpra[i].buf.len, i+1, err);
err = -EFAULT;
goto bail;
}
} else {
mutex_lock(&ctx->fl->map_mutex);
fastrpc_mmap_free(ctx->maps[i], 0);
mutex_unlock(&ctx->fl->map_mutex);
ctx->maps[i] = NULL;
}
}
mutex_lock(&ctx->fl->map_mutex);
for (i = 0; i < M_FDLIST; i++) {
if (!fdlist[i])
break;
if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0,
0, 0, &mmap))
fastrpc_mmap_free(mmap, 0);
}
mutex_unlock(&ctx->fl->map_mutex);
if (ctx->crc && crclist && rpra)
K_COPY_TO_USER(err, kernel, ctx->crc,
crclist, M_CRCLIST*sizeof(uint32_t));
bail:
return err;
}
static void inv_args(struct smq_invoke_ctx *ctx)
{
int i, inbufs, outbufs;
uint32_t sc = ctx->sc;
remote_arg64_t *rpra = ctx->lrpra;
int err = 0;
inbufs = REMOTE_SCALARS_INBUFS(sc);
outbufs = REMOTE_SCALARS_OUTBUFS(sc);
for (i = 0; i < inbufs + outbufs; ++i) {
int over = ctx->overps[i]->raix;
struct fastrpc_mmap *map = ctx->maps[over];
if ((over + 1 <= inbufs))
continue;
if (map && map->uncached)
continue;
if (!rpra[over].buf.len)
continue;
if (ctx->fl->sctx->smmu.coherent)
continue;
if (map && (map->attr & FASTRPC_ATTR_FORCE_NOINVALIDATE))
continue;
if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
buf_page_start(rpra[over].buf.pv)) {
continue;
}
if (ctx->overps[i]->mstart || ctx->overps[i]->do_cmo == 1) {
if (map && map->buf) {
if (((buf_page_size(ctx->overps[i]->mend -
ctx->overps[i]->mstart)) == map->size) ||
ctx->overps[i]->do_cmo) {
dma_buf_begin_cpu_access(map->buf,
DMA_TO_DEVICE);
dma_buf_end_cpu_access(map->buf,
DMA_FROM_DEVICE);
ADSPRPC_DEBUG(
"sc 0x%x pv 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
sc, rpra[over].buf.pv,
ctx->overps[i]->mend,
ctx->overps[i]->mstart,
rpra[over].buf.len, map->size);
} else {
uintptr_t offset;
uint64_t inv_len;
struct vm_area_struct *vma;
down_read(¤t->mm->mmap_sem);
VERIFY(err, NULL != (vma = find_vma(
current->mm,
rpra[over].buf.pv)));
if (err) {
up_read(¤t->mm->mmap_sem);
goto bail;
}
if (ctx->overps[i]->do_cmo) {
offset = rpra[over].buf.pv -
vma->vm_start;
inv_len = rpra[over].buf.len;
} else {
offset =
ctx->overps[i]->mstart -
vma->vm_start;
inv_len =
ctx->overps[i]->mend -
ctx->overps[i]->mstart;
}
up_read(¤t->mm->mmap_sem);
dma_buf_begin_cpu_access_partial(
map->buf, DMA_TO_DEVICE, offset,
inv_len);
dma_buf_end_cpu_access_partial(map->buf,
DMA_FROM_DEVICE, offset,
inv_len);
ADSPRPC_DEBUG(
"sc 0x%x vm_start 0x%llx pv 0x%llx, offset 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
sc, vma->vm_start,
rpra[over].buf.pv,
offset, ctx->overps[i]->mend,
ctx->overps[i]->mstart,
rpra[over].buf.len, map->size);
}
}
}
}
bail:
return;
}
static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
uint32_t kernel, uint32_t handle)
{
struct smq_msg *msg = &ctx->msg;
struct fastrpc_file *fl = ctx->fl;
struct fastrpc_channel_ctx *channel_ctx = NULL;
int err = 0, cid = -1;
uint32_t sc = ctx->sc;
int64_t ns = 0;
cid = fl->cid;
VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
channel_ctx = &fl->apps->channel[fl->cid];
mutex_lock(&channel_ctx->smd_mutex);
msg->pid = fl->tgid;
msg->tid = current->pid;
if (fl->sessionid)
msg->tid |= (1 << SESSION_ID_INDEX);
if (kernel == KERNEL_MSG_WITH_ZERO_PID)
msg->pid = 0;
msg->invoke.header.ctx = ctx->ctxid | fl->pd;
msg->invoke.header.handle = handle;
msg->invoke.header.sc = sc;
msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
msg->invoke.page.size = buf_page_size(ctx->used);
if (fl->ssrcount != channel_ctx->ssrcount) {
err = -ECONNRESET;
mutex_unlock(&channel_ctx->smd_mutex);
goto bail;
}
mutex_unlock(&channel_ctx->smd_mutex);
mutex_lock(&channel_ctx->rpmsg_mutex);
VERIFY(err, !IS_ERR_OR_NULL(channel_ctx->rpdev));
if (err) {
err = -ENODEV;
mutex_unlock(&channel_ctx->rpmsg_mutex);
goto bail;
}
err = rpmsg_send(channel_ctx->rpdev->ept, (void *)msg, sizeof(*msg));
mutex_unlock(&channel_ctx->rpmsg_mutex);
trace_fastrpc_rpmsg_send(fl->cid, (uint64_t)ctx, msg->invoke.header.ctx,
handle, sc, msg->invoke.page.addr, msg->invoke.page.size);
ns = get_timestamp_in_ns();
fastrpc_update_txmsg_buf(channel_ctx, msg, err, ns);
bail:
return err;
}
static void fastrpc_init(struct fastrpc_apps *me)
{
int i;
INIT_HLIST_HEAD(&me->drivers);
INIT_HLIST_HEAD(&me->maps);
spin_lock_init(&me->hlock);
me->channel = &gcinfo[0];
for (i = 0; i < NUM_CHANNELS; i++) {
init_completion(&me->channel[i].work);
init_completion(&me->channel[i].workport);
me->channel[i].sesscount = 0;
/* All channels are secure by default except CDSP */
me->channel[i].secure = SECURE_CHANNEL;
me->channel[i].unsigned_support = false;
mutex_init(&me->channel[i].smd_mutex);
mutex_init(&me->channel[i].rpmsg_mutex);
spin_lock_init(&me->channel[i].ctxlock);
spin_lock_init(&me->channel[i].gmsg_log.lock);
}
/* Set CDSP channel to non secure */
me->channel[CDSP_DOMAIN_ID].secure = NON_SECURE_CHANNEL;
me->channel[CDSP_DOMAIN_ID].unsigned_support = true;
}
static inline void fastrpc_pm_awake(struct fastrpc_file *fl, int channel_type)
{
struct fastrpc_apps *me = &gfa;
struct wakeup_source *wake_source = NULL;
if (!fl->wake_enable)
return;
/*
* Vote with PM to abort any suspend in progress and
* keep system awake for specified timeout
*/
if (channel_type == SECURE_CHANNEL)
wake_source = me->wake_source_secure;
else if (channel_type == NON_SECURE_CHANNEL)
wake_source = me->wake_source;
if (wake_source)
pm_wakeup_ws_event(wake_source, fl->ws_timeout, true);
}
static inline int fastrpc_wait_for_response(struct smq_invoke_ctx *ctx,
uint32_t kernel)
{
int interrupted = 0;
if (kernel)
wait_for_completion(&ctx->work);
else
interrupted = wait_for_completion_interruptible(&ctx->work);
return interrupted;
}
static void fastrpc_wait_for_completion(struct smq_invoke_ctx *ctx,
int *ptr_interrupted, uint32_t kernel, uint32_t async,
bool *ptr_isworkdone)
{
int interrupted = 0, err = 0;
int jj;
bool wait_resp;
uint32_t wTimeout = FASTRPC_USER_EARLY_HINT_TIMEOUT;
uint32_t wakeTime = 0;
unsigned long flags;
if (!ctx) {
/* This failure is not expected */
err = *ptr_interrupted = EFAULT;
*ptr_isworkdone = false;
ADSPRPC_ERR("ctx is NULL, cannot wait for response err %d\n",
err);
return;
}
wakeTime = ctx->early_wake_time;
do {
switch (ctx->rsp_flags) {
/* try polling on completion with timeout */
case USER_EARLY_SIGNAL:
/* try wait if completion time is less than timeout */
/* disable preempt to avoid context switch latency */
preempt_disable();
jj = 0;
wait_resp = false;
for (; wakeTime < wTimeout && jj < wTimeout; jj++) {
wait_resp = try_wait_for_completion(&ctx->work);
if (wait_resp)
break;
udelay(1);
}
preempt_enable();
if (async) {
spin_lock_irqsave(&ctx->fl->aqlock, flags);
if (!ctx->is_work_done) {
ctx->is_early_wakeup = false;
*ptr_isworkdone = false;
} else
*ptr_isworkdone = true;
spin_unlock_irqrestore(&ctx->fl->aqlock, flags);
goto bail;
} else if (!wait_resp) {
interrupted = fastrpc_wait_for_response(ctx,
kernel);
*ptr_interrupted = interrupted;
if (interrupted || ctx->is_work_done)
goto bail;
}
break;
/* busy poll on memory for actual job done */
case EARLY_RESPONSE:
err = poll_on_early_response(ctx);
/* Mark job done if poll on memory successful */
/* Wait for completion if poll on memory timoeut */
if (!err) {
ctx->is_work_done = true;
*ptr_isworkdone = true;
goto bail;
}
ADSPRPC_INFO("poll timeout for handle 0x%x, sc 0x%x\n",
ctx->handle, ctx->sc);
if (async) {
spin_lock_irqsave(&ctx->fl->aqlock, flags);
if (!ctx->is_work_done) {
ctx->is_early_wakeup = false;
*ptr_isworkdone = false;
} else
*ptr_isworkdone = true;
spin_unlock_irqrestore(&ctx->fl->aqlock, flags);
goto bail;
} else if (!ctx->is_work_done) {
interrupted = fastrpc_wait_for_response(ctx,
kernel);
*ptr_interrupted = interrupted;
if (interrupted || ctx->is_work_done)
goto bail;
}
break;
case COMPLETE_SIGNAL:
case NORMAL_RESPONSE:
if (!async) {
interrupted = fastrpc_wait_for_response(ctx,
kernel);
*ptr_interrupted = interrupted;
if (interrupted || ctx->is_work_done)
goto bail;
} else {
spin_lock_irqsave(&ctx->fl->aqlock, flags);
if (!ctx->is_work_done) {
ctx->is_early_wakeup = false;
*ptr_isworkdone = false;
} else
*ptr_isworkdone = true;
spin_unlock_irqrestore(&ctx->fl->aqlock, flags);
goto bail;
}
break;
default:
*ptr_interrupted = EBADR;
*ptr_isworkdone = false;
ADSPRPC_ERR(
"unsupported response flags 0x%x for handle 0x%x, sc 0x%x\n",
ctx->rsp_flags, ctx->handle, ctx->sc);
goto bail;
} /* end of switch */
} while (!ctx->is_work_done);
bail:
return;
}
static void fastrpc_update_invoke_count(uint32_t handle, int64_t *perf_counter,
struct timespec64 *invoket)
{
/* update invoke count for dynamic handles */
if (handle != FASTRPC_STATIC_HANDLE_LISTENER) {
int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE);
if (count)
*count += getnstimediff(invoket);
}
if (handle > FASTRPC_STATIC_HANDLE_MAX) {
int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT);
if (count)
*count += 1;
}
}
static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
uint32_t kernel,
struct fastrpc_ioctl_invoke_async *inv)
{
struct smq_invoke_ctx *ctx = NULL;
struct fastrpc_ioctl_invoke *invoke = &inv->inv;
int err = 0, interrupted = 0, cid = -1;
struct timespec64 invoket = {0};
int64_t *perf_counter = NULL;
bool isasyncinvoke = false, isworkdone = false;
cid = fl->cid;
VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS &&
fl->sctx != NULL);
if (err) {
ADSPRPC_ERR("kernel session not initialized yet for %s\n",
current->comm);
err = -EBADR;
goto bail;
}
if (fl->profile) {
perf_counter = getperfcounter(fl, PERF_COUNT);
ktime_get_real_ts64(&invoket);
}
if (!kernel) {
VERIFY(err, invoke->handle !=
FASTRPC_STATIC_HANDLE_PROCESS_GROUP);
VERIFY(err, invoke->handle !=
FASTRPC_STATIC_HANDLE_DSP_UTILITIES);
if (err) {
err = -EINVAL;
ADSPRPC_ERR(
"user application %s trying to send a kernel RPC message to channel %d, handle 0x%x\n",
cid, invoke->handle);
goto bail;
}
}
if (!kernel) {
VERIFY(err, 0 == (err = context_restore_interrupted(fl,
inv, &ctx)));
if (err)
goto bail;
if (fl->sctx->smmu.faults)
err = -FASTRPC_ENOSUCH;
if (err)
goto bail;
if (ctx) {
trace_fastrpc_context_restore(cid, (uint64_t)ctx,
ctx->msg.invoke.header.ctx,
ctx->handle, ctx->sc);
goto wait;
}
}
VERIFY(err, 0 == (err = context_alloc(fl, kernel, inv, &ctx)));
if (err)
goto bail;
isasyncinvoke = (ctx->asyncjob.isasyncjob ? true : false);
PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS),
VERIFY(err, 0 == (err = get_args(kernel, ctx)));
PERF_END);
if (err)
goto bail;
PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
inv_args(ctx);
PERF_END);
PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK),
VERIFY(err, 0 == (err = fastrpc_invoke_send(ctx,
kernel, invoke->handle)));
PERF_END);
if (err)
goto bail;
if (isasyncinvoke)
goto invoke_end;
wait:
fastrpc_wait_for_completion(ctx, &interrupted, kernel, 0, &isworkdone);
VERIFY(err, 0 == (err = interrupted));
if (err)
goto bail;
if (!ctx->is_work_done) {
err = -ETIMEDOUT;
ADSPRPC_ERR(
"WorkDone state is invalid for handle 0x%x, sc 0x%x\n",
invoke->handle, ctx->sc);
goto bail;
}
PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
inv_args(ctx);
PERF_END);
PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
VERIFY(err, 0 == (err = put_args(kernel, ctx, invoke->pra)));
PERF_END);
if (err)
goto bail;
VERIFY(err, 0 == (err = ctx->retval));
if (err)
goto bail;
bail:
if (ctx && interrupted == -ERESTARTSYS) {
trace_fastrpc_context_interrupt(cid, (uint64_t)ctx,
ctx->msg.invoke.header.ctx, ctx->handle, ctx->sc);
context_save_interrupted(ctx);
}
else if (ctx)
context_free(ctx);
if (fl->ssrcount != fl->apps->channel[cid].ssrcount)
err = -ECONNRESET;
invoke_end:
if (fl->profile && !interrupted)
fastrpc_update_invoke_count(invoke->handle, perf_counter,
&invoket);
return err;
}
static int fastrpc_wait_on_async_queue(
struct fastrpc_ioctl_async_response *async_res,
struct fastrpc_file *fl)
{
int err = 0, ierr = 0, interrupted = 0;
struct smq_invoke_ctx *ctx = NULL, *ictx = NULL, *n = NULL;
unsigned long flags;
int64_t *perf_counter = NULL;
bool isworkdone = false;
read_async_job:
interrupted = wait_event_interruptible(fl->async_wait_queue,
atomic_read(&fl->async_queue_job_count));
if (!fl || fl->file_close == 1) {
err = -EBADF;
goto bail;
}
VERIFY(err, 0 == (err = interrupted));
if (err)
goto bail;
spin_lock_irqsave(&fl->aqlock, flags);
list_for_each_entry_safe(ictx, n, &fl->clst.async_queue, asyncn) {
list_del_init(&ictx->asyncn);
atomic_sub(1, &fl->async_queue_job_count);
ctx = ictx;
break;
}
spin_unlock_irqrestore(&fl->aqlock, flags);
if (ctx) {
fastrpc_wait_for_completion(ctx, &interrupted, 0, 1,
&isworkdone);
if (!isworkdone) {//In valid workdone state
ADSPRPC_DEBUG(
"Async early wake response did not reach on time for thread %d handle 0x%x, sc 0x%x\n",
ctx->pid, ctx->handle, ctx->sc);
goto read_async_job;
}
async_res->jobid = ctx->asyncjob.jobid;
async_res->result = ctx->retval;
PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
inv_args(ctx);
PERF_END);
if (ctx->retval != 0)
goto bail;
PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
VERIFY(ierr, 0 == (ierr = put_args(0, ctx, NULL)));
PERF_END);
if (ierr)
goto bail;
} else {//Retry again if ctx is NULL
ADSPRPC_ERR("Invalid async job wake up\n");
goto read_async_job;
}
bail:
if (ierr)
async_res->result = ierr;
if (ctx)
context_free(ctx);
return err;
}
static int fastrpc_get_async_response(
struct fastrpc_ioctl_async_response *async_res,
void *param, struct fastrpc_file *fl)
{
int err = 0;
err = fastrpc_wait_on_async_queue(async_res, fl);
if (err)
goto bail;
K_COPY_TO_USER(err, 0, param, async_res,
sizeof(struct fastrpc_ioctl_async_response));
bail:
return err;
}
static int fastrpc_create_persistent_headers(struct fastrpc_file *fl,
uint32_t user_concurrency)
{
int err = 0, i = 0;
uint64_t virtb = 0;
struct fastrpc_buf *pers_hdr_buf = NULL, *hdr_bufs = NULL, *buf = NULL;
unsigned int num_pers_hdrs = 0;
size_t hdr_buf_alloc_len = 0;
if (fl->pers_hdr_buf || !user_concurrency)
goto bail;
/*
* Pre-allocate memory for persistent header buffers based
* on concurrency info passed by user. Upper limit enforced.
*/
num_pers_hdrs = (user_concurrency > MAX_PERSISTENT_HEADERS) ?
MAX_PERSISTENT_HEADERS : user_concurrency;
hdr_buf_alloc_len = num_pers_hdrs*PAGE_SIZE;
err = fastrpc_buf_alloc(fl, hdr_buf_alloc_len, 0, 0,
METADATA_BUF, &pers_hdr_buf);
if (err)
goto bail;
virtb = ptr_to_uint64(pers_hdr_buf->virt);
/* Map entire buffer on remote subsystem in single RPC call */
err = fastrpc_mem_map_to_dsp(fl, -1, 0, ADSP_MMAP_PERSIST_HDR, 0,
pers_hdr_buf->phys, pers_hdr_buf->size,
&pers_hdr_buf->raddr);
if (err)
goto bail;
/* Divide and store as N chunks, each of 1 page size */
hdr_bufs = kcalloc(num_pers_hdrs, sizeof(struct fastrpc_buf),
GFP_KERNEL);
if (!hdr_bufs) {
err = -ENOMEM;
goto bail;
}
spin_lock(&fl->hlock);
fl->pers_hdr_buf = pers_hdr_buf;
fl->num_pers_hdrs = num_pers_hdrs;
fl->hdr_bufs = hdr_bufs;
for (i = 0; i < num_pers_hdrs; i++) {
buf = &fl->hdr_bufs[i];
buf->fl = fl;
buf->virt = uint64_to_ptr(virtb + (i*PAGE_SIZE));
buf->phys = pers_hdr_buf->phys + (i*PAGE_SIZE);
buf->size = PAGE_SIZE;
buf->dma_attr = pers_hdr_buf->dma_attr;
buf->flags = pers_hdr_buf->flags;
buf->type = pers_hdr_buf->type;
buf->in_use = false;
}
spin_unlock(&fl->hlock);
bail:
if (err) {
fl->pers_hdr_buf = NULL;
fl->hdr_bufs = NULL;
fl->num_pers_hdrs = 0;
if (!IS_ERR_OR_NULL(pers_hdr_buf))
fastrpc_buf_free(pers_hdr_buf, 0);
if (!IS_ERR_OR_NULL(hdr_bufs))
kfree(hdr_bufs);
}
return err;
}
static int fastrpc_internal_invoke2(struct fastrpc_file *fl,
struct fastrpc_ioctl_invoke2 *inv2)
{
union {
struct fastrpc_ioctl_invoke_async inv;
struct fastrpc_ioctl_async_response async_res;
uint32_t user_concurrency;
} p;
struct fastrpc_dsp_capabilities *dsp_cap_ptr = NULL;
uint32_t size = 0;
int err = 0, domain = fl->cid;
if (inv2->req == FASTRPC_INVOKE2_ASYNC ||
inv2->req == FASTRPC_INVOKE2_ASYNC_RESPONSE) {
VERIFY(err, domain == CDSP_DOMAIN_ID && fl->sctx != NULL);
if (err)
goto bail;
dsp_cap_ptr = &gcinfo[domain].dsp_cap_kernel;
VERIFY(err,
dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP] == 1);
if (err) {
err = -EPROTONOSUPPORT;
goto bail;
}
}
switch (inv2->req) {
case FASTRPC_INVOKE2_ASYNC:
size = sizeof(struct fastrpc_ioctl_invoke_async);
VERIFY(err, size == inv2->size);
if (err) {
err = -EBADE;
goto bail;
}
K_COPY_FROM_USER(err, 0, &p.inv, (void *)inv2->invparam, size);
if (err)
goto bail;
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
USER_MSG, &p.inv)));
if (err)
goto bail;
break;
case FASTRPC_INVOKE2_ASYNC_RESPONSE:
VERIFY(err,
sizeof(struct fastrpc_ioctl_async_response) == inv2->size);
if (err) {
err = -EBADE;
goto bail;
}
err = fastrpc_get_async_response(&p.async_res,
(void *)inv2->invparam, fl);
break;
case FASTRPC_INVOKE2_KERNEL_OPTIMIZATIONS:
if (inv2->size != sizeof(uint32_t)) {
err = -EBADE;
goto bail;
}
K_COPY_FROM_USER(err, 0, &p.user_concurrency,
(void *)inv2->invparam, size);
if (err)
goto bail;
err = fastrpc_create_persistent_headers(fl,
p.user_concurrency);
break;
default:
err = -ENOTTY;
break;
}
bail:
return err;
}
static int fastrpc_get_spd_session(char *name, int *session, int *cid)
{
struct fastrpc_apps *me = &gfa;
int err = 0, i, j, match = 0;
for (i = 0; i < NUM_CHANNELS; i++) {
for (j = 0; j < NUM_SESSIONS; j++) {
if (!me->channel[i].spd[j].servloc_name)
continue;
if (!strcmp(name, me->channel[i].spd[j].servloc_name)) {
match = 1;
break;
}
}
if (match)
break;
}
VERIFY(err, i < NUM_CHANNELS && j < NUM_SESSIONS);
if (err) {
err = -EUSERS;
goto bail;
}
*cid = i;
*session = j;
bail:
return err;
}
static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl);
static int fastrpc_channel_open(struct fastrpc_file *fl);
static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl);
/*
* This function makes a call to create a thread group in the root
* process or static process on the remote subsystem.
* Examples:
* - guestOS daemons on all DSPs
* - sensors daemon on sensorsPD on SLPI/ADSP
*/
static int fastrpc_init_attach_process(struct fastrpc_file *fl,
struct fastrpc_ioctl_init *init)
{
int err = 0, tgid = fl->tgid;
remote_arg_t ra[1];
struct fastrpc_ioctl_invoke_async ioctl;
/*
* Prepare remote arguments for creating thread group
* in guestOS/staticPD on the remote subsystem.
*/
ra[0].buf.pv = (void *)&tgid;
ra[0].buf.len = sizeof(tgid);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
if (init->flags == FASTRPC_INIT_ATTACH)
fl->pd = 0;
else if (init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
if (fl->cid == ADSP_DOMAIN_ID)
fl->servloc_name =
SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME;
else if (fl->cid == SDSP_DOMAIN_ID)
fl->servloc_name =
SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME;
/* Setting to 2 will route the message to sensorsPD */
fl->pd = 2;
}
err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
if (err)
goto bail;
bail:
return err;
}
/*
* This function makes a call to spawn a dynamic process
* on the remote subsystem.
* Example: all compute offloads to CDSP
*/
static int fastrpc_init_create_dynamic_process(struct fastrpc_file *fl,
struct fastrpc_ioctl_init_attrs *uproc)
{
int err = 0, memlen = 0, mflags = 0, locked = 0;
struct fastrpc_ioctl_invoke_async ioctl;
struct fastrpc_ioctl_init *init = &uproc->init;
struct smq_phy_page pages[1];
struct fastrpc_mmap *file = NULL;
struct fastrpc_buf *imem = NULL;
unsigned long imem_dma_attr = 0;
remote_arg_t ra[6];
int fds[6];
unsigned int gid = 0, one_mb = 1024*1024;
unsigned int dsp_userpd_memlen = 3 * one_mb;
struct fastrpc_buf *init_mem;
struct {
int pgid;
unsigned int namelen;
unsigned int filelen;
unsigned int pageslen;
int attrs;
int siglen;
} inbuf;
inbuf.pgid = fl->tgid;
inbuf.namelen = strlen(current->comm) + 1;
inbuf.filelen = init->filelen;
fl->pd = 1;
/* Check if file memory passed by userspace is valid */
VERIFY(err, access_ok((void __user *)init->file, init->filelen));
if (err)
goto bail;
if (init->filelen) {
/* Map the shell file buffer to remote subsystem */
mutex_lock(&fl->map_mutex);
err = fastrpc_mmap_create(fl, init->filefd, 0,
init->file, init->filelen, mflags, &file);
mutex_unlock(&fl->map_mutex);
if (err)
goto bail;
}
inbuf.pageslen = 1;
/* Untrusted apps are not allowed to offload to signedPD on DSP. */
if (fl->untrusted_process) {
VERIFY(err, uproc->attrs & FASTRPC_MODE_UNSIGNED_MODULE);
if (err) {
err = -ECONNREFUSED;
ADSPRPC_ERR(
"untrusted app trying to offload to signed remote process\n");
goto bail;
}
}
/* Disregard any privilege bits from userspace */
uproc->attrs &= (~FASTRPC_MODE_PRIVILEGED);
/*
* Check if the primary or supplementary group(s) of the process is
* one of the 'privileged' fastrpc GIDs stored in the device-tree.
*/
gid = sorted_lists_intersection(fl->gidlist.gids,
fl->gidlist.gidcount, gfa.gidlist.gids, gfa.gidlist.gidcount);
if (gid) {
ADSPRPC_INFO("PID %d, GID %u is a privileged process\n",
fl->tgid, gid);
uproc->attrs |= FASTRPC_MODE_PRIVILEGED;
}
/*
* Userspace client should try to allocate the initial memory donated
* to remote subsystem as only the kernel and DSP should have access
* to that memory.
*/
VERIFY(err, !init->mem);
if (err) {
err = -EINVAL;
ADSPRPC_ERR("donated memory allocated in userspace\n");
goto bail;
}
/* Free any previous donated memory */
spin_lock(&fl->hlock);
locked = 1;
if (fl->init_mem) {
init_mem = fl->init_mem;
fl->init_mem = NULL;
spin_unlock(&fl->hlock);
locked = 0;
fastrpc_buf_free(init_mem, 0);
}
if (locked) {
spin_unlock(&fl->hlock);
locked = 0;
}
/* Allocate DMA buffer in kernel for donating to remote process
* Unsigned PD requires additional memory because of the
* additional static heap initialized within the process.
*/
if (uproc->attrs & FASTRPC_MODE_UNSIGNED_MODULE)
dsp_userpd_memlen += 2*one_mb;
memlen = ALIGN(max(dsp_userpd_memlen, init->filelen * 4), one_mb);
imem_dma_attr = DMA_ATTR_EXEC_MAPPING |
DMA_ATTR_DELAYED_UNMAP |
DMA_ATTR_NO_KERNEL_MAPPING;
err = fastrpc_buf_alloc(fl, memlen, imem_dma_attr, 0,
INITMEM_BUF, &imem);
if (err)
goto bail;
fl->init_mem = imem;
/*
* Prepare remote arguments for dynamic process create
* call to remote subsystem.
*/
inbuf.pageslen = 1;
ra[0].buf.pv = (void *)&inbuf;
ra[0].buf.len = sizeof(inbuf);
fds[0] = -1;
ra[1].buf.pv = (void *)current->comm;
ra[1].buf.len = inbuf.namelen;
fds[1] = -1;
ra[2].buf.pv = (void *)init->file;
ra[2].buf.len = inbuf.filelen;
fds[2] = init->filefd;
pages[0].addr = imem->phys;
pages[0].size = imem->size;
ra[3].buf.pv = (void *)pages;
ra[3].buf.len = 1 * sizeof(*pages);
fds[3] = -1;
inbuf.attrs = uproc->attrs;
ra[4].buf.pv = (void *)&(inbuf.attrs);
ra[4].buf.len = sizeof(inbuf.attrs);
fds[4] = -1;
inbuf.siglen = uproc->siglen;
ra[5].buf.pv = (void *)&(inbuf.siglen);
ra[5].buf.len = sizeof(inbuf.siglen);
fds[5] = -1;
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
/*
* Choose appropriate remote method ID depending on whether the
* HLOS process has any attributes enabled (like unsignedPD,
* critical process, adaptive QoS, CRC checks etc).
*/
ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
if (uproc->attrs)
ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0);
ioctl.inv.pra = ra;
ioctl.fds = fds;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
if (err)
goto bail;
bail:
/*
* Shell is loaded into the donated memory on remote subsystem. So, the
* original file buffer can be DMA unmapped. In case of a failure also,
* the mapping needs to be removed.
*/
if (file) {
mutex_lock(&fl->map_mutex);
fastrpc_mmap_free(file, 0);
mutex_unlock(&fl->map_mutex);
}
if (err) {
spin_lock(&fl->hlock);
locked = 1;
if (!IS_ERR_OR_NULL(fl->init_mem)) {
init_mem = fl->init_mem;
fl->init_mem = NULL;
spin_unlock(&fl->hlock);
locked = 0;
fastrpc_buf_free(init_mem, 0);
}
if (locked) {
spin_unlock(&fl->hlock);
locked = 0;
}
}
return err;
}
/*
* This function makes a call to create a thread group in the static
* process on the remote subsystem.
* Example: audio daemon 'adsprpcd' on audioPD on ADSP
*/
static int fastrpc_init_create_static_process(struct fastrpc_file *fl,
struct fastrpc_ioctl_init *init)
{
int err = 0, rh_hyp_done = 0;
struct fastrpc_apps *me = &gfa;
struct fastrpc_ioctl_invoke_async ioctl;
struct smq_phy_page pages[1];
struct fastrpc_mmap *mem = NULL;
char *proc_name = NULL;
remote_arg_t ra[3];
uint64_t phys = 0;
size_t size = 0;
int fds[3];
struct secure_vm *rhvm = &me->channel[fl->cid].rhvm;
struct {
int pgid;
unsigned int namelen;
unsigned int pageslen;
} inbuf;
if (!init->filelen)
goto bail;
proc_name = kzalloc(init->filelen, GFP_KERNEL);
VERIFY(err, !IS_ERR_OR_NULL(proc_name));
if (err) {
err = -ENOMEM;
goto bail;
}
err = copy_from_user((void *)proc_name,
(void __user *)init->file, init->filelen);
if (err) {
err = -EFAULT;
goto bail;
}
fl->pd = 1;
inbuf.pgid = fl->tgid;
inbuf.namelen = init->filelen;
inbuf.pageslen = 0;
if (!strcmp(proc_name, "audiopd")) {
fl->servloc_name = AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME;
/*
* Remove any previous mappings in case process is trying
* to reconnect after a PD restart on remote subsystem.
*/
err = fastrpc_mmap_remove_pdr(fl);
if (err)
goto bail;
}
if (!me->staticpd_flags && !me->legacy_remote_heap) {
inbuf.pageslen = 1;
mutex_lock(&fl->map_mutex);
err = fastrpc_mmap_create(fl, -1, 0, init->mem,
init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR, &mem);
mutex_unlock(&fl->map_mutex);
if (err)
goto bail;
phys = mem->phys;
size = mem->size;
/*
* If remote-heap VMIDs are defined in DTSI, then do
* hyp_assign from HLOS to those VMs (LPASS, ADSP).
*/
if (rhvm->vmid) {
err = hyp_assign_phys(phys, (uint64_t)size,
hlosvm, 1,
rhvm->vmid, rhvm->vmperm, rhvm->vmcount);
if (err) {
ADSPRPC_ERR(
"rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
err, phys, size);
err = -EADDRNOTAVAIL;
goto bail;
}
rh_hyp_done = 1;
}
me->staticpd_flags = 1;
}
/*
* Prepare remote arguments for static process create
* call to remote subsystem.
*/
ra[0].buf.pv = (void *)&inbuf;
ra[0].buf.len = sizeof(inbuf);
fds[0] = -1;
ra[1].buf.pv = (void *)proc_name;
ra[1].buf.len = inbuf.namelen;
fds[1] = -1;
pages[0].addr = phys;
pages[0].size = size;
ra[2].buf.pv = (void *)pages;
ra[2].buf.len = sizeof(*pages);
fds[2] = -1;
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
if (err)
goto bail;
bail:
kfree(proc_name);
if (err) {
me->staticpd_flags = 0;
if (rh_hyp_done) {
int hyp_err = 0;
/* Assign memory back to HLOS in case of errors */
hyp_err = hyp_assign_phys(phys, (uint64_t)size,
rhvm->vmid, rhvm->vmcount,
hlosvm, hlosvmperm, 1);
if (hyp_err)
ADSPRPC_WARN(
"rh hyp unassign failed with %d for phys 0x%llx of size %zu\n",
hyp_err, phys, size);
}
mutex_lock(&fl->map_mutex);
fastrpc_mmap_free(mem, 0);
mutex_unlock(&fl->map_mutex);
}
return err;
}
static int fastrpc_init_process(struct fastrpc_file *fl,
struct fastrpc_ioctl_init_attrs *uproc)
{
int err = 0;
struct fastrpc_ioctl_init *init = &uproc->init;
VERIFY(err, init->filelen < INIT_FILELEN_MAX
&& init->memlen < INIT_MEMLEN_MAX);
if (err)
goto bail;
if (err) {
ADSPRPC_ERR(
"file size 0x%x or init memory 0x%x is more than max allowed file size 0x%x or init len 0x%x\n",
init->filelen, init->memlen,
INIT_FILELEN_MAX, INIT_MEMLEN_MAX);
err = -EFBIG;
goto bail;
}
err = fastrpc_channel_open(fl);
if (err)
goto bail;
switch (init->flags) {
case FASTRPC_INIT_ATTACH:
case FASTRPC_INIT_ATTACH_SENSORS:
err = fastrpc_init_attach_process(fl, init);
break;
case FASTRPC_INIT_CREATE:
err = fastrpc_init_create_dynamic_process(fl, uproc);
break;
case FASTRPC_INIT_CREATE_STATIC:
err = fastrpc_init_create_static_process(fl, init);
break;
default:
err = -ENOTTY;
break;
}
if (err)
goto bail;
fl->dsp_proc_init = 1;
bail:
return err;
}
static int fastrpc_send_cpuinfo_to_dsp(struct fastrpc_file *fl)
{
int err = 0;
uint64_t cpuinfo = 0;
struct fastrpc_apps *me = &gfa;
struct fastrpc_ioctl_invoke_async ioctl;
remote_arg_t ra[2];
VERIFY(err, fl && fl->cid >= ADSP_DOMAIN_ID && fl->cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
cpuinfo = me->channel[fl->cid].cpuinfo_todsp;
/* return success if already updated to remote processor */
if (me->channel[fl->cid].cpuinfo_status)
return 0;
ra[0].buf.pv = (void *)&cpuinfo;
ra[0].buf.len = sizeof(cpuinfo);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_DSP_UTILITIES;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
fl->pd = 1;
err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
if (!err)
me->channel[fl->cid].cpuinfo_status = true;
bail:
return err;
}
static int fastrpc_get_info_from_dsp(struct fastrpc_file *fl,
uint32_t *dsp_attr_buf,
uint32_t dsp_attr_buf_len,
uint32_t domain)
{
int err = 0;
struct fastrpc_ioctl_invoke_async ioctl;
remote_arg_t ra[2];
dsp_attr_buf[0] = 0; // Capability filled in userspace
// Fastrpc to modem not supported
if (domain == MDSP_DOMAIN_ID)
goto bail;
err = fastrpc_channel_open(fl);
if (err)
goto bail;
ra[0].buf.pv = (void *)&dsp_attr_buf_len;
ra[0].buf.len = sizeof(dsp_attr_buf_len);
ra[1].buf.pv = (void *)(&dsp_attr_buf[1]);
ra[1].buf.len = dsp_attr_buf_len * sizeof(uint32_t);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_DSP_UTILITIES;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 1);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
fl->pd = 1;
err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
bail:
if (err)
ADSPRPC_ERR("could not obtain dsp information, err val %d\n",
err);
return err;
}
static int fastrpc_get_info_from_kernel(
struct fastrpc_ioctl_capability *cap,
struct fastrpc_file *fl)
{
int err = 0;
uint32_t domain = cap->domain, attribute_ID = cap->attribute_ID;
uint32_t async_capability = 0;
struct fastrpc_dsp_capabilities *dsp_cap_ptr = NULL;
VERIFY(err, domain < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
/*
* Check if number of attribute IDs obtained from userspace
* is less than the number of attribute IDs supported by
* kernel
*/
if (attribute_ID >= FASTRPC_MAX_ATTRIBUTES) {
err = -EOVERFLOW;
goto bail;
}
dsp_cap_ptr = &gcinfo[domain].dsp_cap_kernel;
if (attribute_ID >= FASTRPC_MAX_DSP_ATTRIBUTES) {
// Driver capability, pass it to user
memcpy(&cap->capability,
&kernel_capabilities[attribute_ID -
FASTRPC_MAX_DSP_ATTRIBUTES],
sizeof(cap->capability));
} else if (!dsp_cap_ptr->is_cached) {
/*
* Information not on kernel, query device for information
* and cache on kernel
*/
err = fastrpc_get_info_from_dsp(fl,
dsp_cap_ptr->dsp_attributes,
FASTRPC_MAX_DSP_ATTRIBUTES - 1,
domain);
if (err)
goto bail;
/* Async capability support depends on both kernel and DSP */
async_capability = IS_ASYNC_FASTRPC_AVAILABLE &&
dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP];
dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP]
= async_capability;
memcpy(&cap->capability,
&dsp_cap_ptr->dsp_attributes[attribute_ID],
sizeof(cap->capability));
dsp_cap_ptr->is_cached = 1;
} else {
// Information on Kernel, pass it to user
memcpy(&cap->capability,
&dsp_cap_ptr->dsp_attributes[attribute_ID],
sizeof(cap->capability));
}
bail:
return err;
}
static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
{
int err = 0;
struct fastrpc_ioctl_invoke_async ioctl;
remote_arg_t ra[1];
int tgid = 0;
VERIFY(err, fl->cid >= ADSP_DOMAIN_ID && fl->cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
VERIFY(err, fl->sctx != NULL);
if (err) {
err = -EBADR;
goto bail;
}
VERIFY(err, fl->apps->channel[fl->cid].rpdev != NULL);
if (err) {
err = -ENODEV;
goto bail;
}
VERIFY(err, fl->apps->channel[fl->cid].issubsystemup == 1);
if (err) {
err = -ECONNRESET;
goto bail;
}
tgid = fl->tgid;
ra[0].buf.pv = (void *)&tgid;
ra[0].buf.len = sizeof(tgid);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
/*
* Pass 2 for "kernel" arg to send kernel msg to DSP
* with non-zero msg PID for the DSP to directly use
* that info to kill the remote process.
*/
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_NONZERO_PID, &ioctl)));
if (err && fl->dsp_proc_init)
ADSPRPC_ERR(
"releasing DSP process failed with %d (0x%x) for %s\n",
err, err, current->comm);
bail:
return err;
}
static int fastrpc_mem_map_to_dsp(struct fastrpc_file *fl, int fd, int offset,
uint32_t flags, uintptr_t va, uint64_t phys,
size_t size, uintptr_t *raddr)
{
struct fastrpc_ioctl_invoke_async ioctl;
struct smq_phy_page page;
remote_arg_t ra[4];
int err = 0;
struct {
int pid;
int fd;
int offset;
uint32_t flags;
uint64_t vaddrin;
int num;
int data_len;
} inargs;
struct {
uint64_t vaddrout;
} routargs;
inargs.pid = fl->tgid;
inargs.fd = fd;
inargs.offset = offset;
inargs.vaddrin = (uintptr_t)va;
inargs.flags = flags;
inargs.num = sizeof(page);
inargs.data_len = 0;
ra[0].buf.pv = (void *)&inargs;
ra[0].buf.len = sizeof(inargs);
page.addr = phys;
page.size = size;
ra[1].buf.pv = (void *)&page;
ra[1].buf.len = sizeof(page);
ra[2].buf.pv = (void *)&page;
ra[2].buf.len = 0;
ra[3].buf.pv = (void *)&routargs;
ra[3].buf.len = sizeof(routargs);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(10, 3, 1);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
if (err)
goto bail;
if (raddr)
*raddr = (uintptr_t)routargs.vaddrout;
bail:
if (err) {
pr_err("adsprpc: %s failed. err 0x%x fd %d len 0x%x\n",
__func__, err, fd, size);
}
return err;
}
static int fastrpc_mem_unmap_to_dsp(struct fastrpc_file *fl, int fd,
uint32_t flags, uintptr_t va,
uint64_t phys, size_t size)
{
struct fastrpc_ioctl_invoke_async ioctl;
remote_arg_t ra[1];
int err = 0;
struct {
int pid;
int fd;
uint64_t vaddrin;
uint64_t len;
} inargs;
inargs.pid = fl->tgid;
inargs.fd = fd;
inargs.vaddrin = (uint64_t)va;
inargs.len = (uint64_t)size;
ra[0].buf.pv = (void *)&inargs;
ra[0].buf.len = sizeof(inargs);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(11, 1, 0);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
if (err)
goto bail;
bail:
if (err) {
pr_err("adsprpc: %s failed. err 0x%x fd %d len 0x%x\n",
__func__, err, fd, size);
}
return err;
}
static int fastrpc_unmap_on_dsp(struct fastrpc_file *fl,
uintptr_t raddr, uint64_t phys, size_t size, uint32_t flags)
{
struct fastrpc_ioctl_invoke_async ioctl;
remote_arg_t ra[1] = {};
int err = 0;
struct {
int pid;
uintptr_t vaddrout;
size_t size;
} inargs;
inargs.pid = fl->tgid;
inargs.size = size;
inargs.vaddrout = raddr;
ra[0].buf.pv = (void *)&inargs;
ra[0].buf.len = sizeof(inargs);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
if (fl->apps->compat)
ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
else
ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
if (err)
goto bail;
bail:
return err;
}
static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
uintptr_t va, uint64_t phys,
size_t size, uintptr_t *raddr)
{
struct fastrpc_ioctl_invoke_async ioctl;
struct fastrpc_apps *me = &gfa;
struct smq_phy_page page;
int num = 1;
remote_arg_t ra[3];
int err = 0;
struct {
int pid;
uint32_t flags;
uintptr_t vaddrin;
int num;
} inargs;
struct {
uintptr_t vaddrout;
} routargs;
inargs.pid = fl->tgid;
inargs.vaddrin = (uintptr_t)va;
inargs.flags = flags;
inargs.num = fl->apps->compat ? num * sizeof(page) : num;
ra[0].buf.pv = (void *)&inargs;
ra[0].buf.len = sizeof(inargs);
page.addr = phys;
page.size = size;
ra[1].buf.pv = (void *)&page;
ra[1].buf.len = num * sizeof(page);
ra[2].buf.pv = (void *)&routargs;
ra[2].buf.len = sizeof(routargs);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
if (fl->apps->compat)
ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
else
ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
*raddr = (uintptr_t)routargs.vaddrout;
if (err)
goto bail;
if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR
&& me->channel[fl->cid].rhvm.vmid) {
err = hyp_assign_phys(phys, (uint64_t)size,
hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
me->channel[fl->cid].rhvm.vmperm,
me->channel[fl->cid].rhvm.vmcount);
if (err) {
ADSPRPC_ERR(
"rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
err, phys, size);
err = -EADDRNOTAVAIL;
err = fastrpc_unmap_on_dsp(fl,
*raddr, phys, size, flags);
if (err) {
ADSPRPC_ERR(
"failed to unmap %d for phys 0x%llx, size %zd\n",
err, phys, size);
}
goto bail;
}
}
bail:
return err;
}
static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl, uint64_t phys,
size_t size, uint32_t flags)
{
int err = 0;
struct fastrpc_apps *me = &gfa;
int tgid = 0;
int destVM[1] = {VMID_HLOS};
int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
if (flags == ADSP_MMAP_HEAP_ADDR) {
struct fastrpc_ioctl_invoke_async ioctl;
remote_arg_t ra[2];
int err = 0;
struct {
uint8_t skey;
} routargs;
if (fl == NULL)
goto bail;
tgid = fl->tgid;
ra[0].buf.pv = (void *)&tgid;
ra[0].buf.len = sizeof(tgid);
ra[1].buf.pv = (void *)&routargs;
ra[1].buf.len = sizeof(routargs);
ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
ioctl.inv.sc = REMOTE_SCALARS_MAKE(9, 1, 1);
ioctl.inv.pra = ra;
ioctl.fds = NULL;
ioctl.attrs = NULL;
ioctl.crc = NULL;
ioctl.job = NULL;
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
if (err)
goto bail;
} else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
if (me->channel[fl->cid].rhvm.vmid) {
err = hyp_assign_phys(phys,
(uint64_t)size,
me->channel[fl->cid].rhvm.vmid,
me->channel[fl->cid].rhvm.vmcount,
destVM, destVMperm, 1);
if (err) {
ADSPRPC_ERR(
"rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
err, phys, size);
err = -EADDRNOTAVAIL;
goto bail;
}
}
}
bail:
return err;
}
static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, uintptr_t raddr,
uint64_t phys, size_t size, uint32_t flags)
{
int err = 0;
VERIFY(err, 0 == (err = fastrpc_unmap_on_dsp(fl, raddr, phys,
size, flags)));
if (err)
goto bail;
if (flags == ADSP_MMAP_HEAP_ADDR ||
flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
VERIFY(err, !(err = fastrpc_munmap_on_dsp_rh(fl, phys,
size, flags)));
if (err)
goto bail;
}
bail:
return err;
}
static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl)
{
struct fastrpc_mmap *match = NULL, *map = NULL;
struct hlist_node *n = NULL;
int err = 0, ret = 0;
struct fastrpc_apps *me = &gfa;
struct ramdump_segment *ramdump_segments_rh = NULL;
VERIFY(err, fl->cid == RH_CID);
if (err) {
err = -EBADR;
goto bail;
}
do {
match = NULL;
spin_lock(&me->hlock);
hlist_for_each_entry_safe(map, n, &me->maps, hn) {
match = map;
hlist_del_init(&map->hn);
break;
}
spin_unlock(&me->hlock);
if (match) {
err = fastrpc_munmap_on_dsp_rh(fl, match->phys,
match->size, match->flags);
if (err)
goto bail;
if (me->channel[RH_CID].ramdumpenabled) {
ramdump_segments_rh = kcalloc(1,
sizeof(struct ramdump_segment), GFP_KERNEL);
if (ramdump_segments_rh) {
ramdump_segments_rh->address =
match->phys;
ramdump_segments_rh->size = match->size;
ret = do_elf_ramdump(
me->channel[RH_CID].rh_dump_dev,
ramdump_segments_rh, 1);
if (ret < 0)
pr_err("adsprpc: %s: unable to dump heap (err %d)\n",
__func__, ret);
kfree(ramdump_segments_rh);
}
}
fastrpc_mmap_free(match, 0);
}
} while (match);
bail:
if (err && match)
fastrpc_mmap_add(match);
return err;
}
static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl)
{
struct fastrpc_apps *me = &gfa;
int session = 0, err = 0, cid = -1;
err = fastrpc_get_spd_session(fl->servloc_name,
&session, &cid);
if (err)
goto bail;
VERIFY(err, cid == fl->cid);
if (err) {
err = -EBADR;
goto bail;
}
if (!me->channel[fl->cid].spd[session].ispdup) {
err = -ENOTCONN;
goto bail;
}
if (me->channel[fl->cid].spd[session].pdrcount !=
me->channel[fl->cid].spd[session].prevpdrcount) {
err = fastrpc_mmap_remove_ssr(fl);
if (err)
ADSPRPC_WARN("failed to unmap remote heap (err %d)\n",
err);
me->channel[fl->cid].spd[session].prevpdrcount =
me->channel[fl->cid].spd[session].pdrcount;
}
bail:
return err;
}
static inline void get_fastrpc_ioctl_mmap_64(
struct fastrpc_ioctl_mmap_64 *mmap64,
struct fastrpc_ioctl_mmap *immap)
{
immap->fd = mmap64->fd;
immap->flags = mmap64->flags;
immap->vaddrin = (uintptr_t)mmap64->vaddrin;
immap->size = mmap64->size;
}
static inline void put_fastrpc_ioctl_mmap_64(
struct fastrpc_ioctl_mmap_64 *mmap64,
struct fastrpc_ioctl_mmap *immap)
{
mmap64->vaddrout = (uint64_t)immap->vaddrout;
}
static inline void get_fastrpc_ioctl_munmap_64(
struct fastrpc_ioctl_munmap_64 *munmap64,
struct fastrpc_ioctl_munmap *imunmap)
{
imunmap->vaddrout = (uintptr_t)munmap64->vaddrout;
imunmap->size = munmap64->size;
}
static int fastrpc_internal_munmap(struct fastrpc_file *fl,
struct fastrpc_ioctl_munmap *ud)
{
int err = 0;
struct fastrpc_mmap *map = NULL;
struct fastrpc_buf *rbuf = NULL, *free = NULL;
struct hlist_node *n;
VERIFY(err, fl->dsp_proc_init == 1);
if (err) {
ADSPRPC_ERR(
"user application %s trying to unmap without initialization\n",
current->comm);
err = -EHOSTDOWN;
goto bail;
}
mutex_lock(&fl->internal_map_mutex);
spin_lock(&fl->hlock);
hlist_for_each_entry_safe(rbuf, n, &fl->remote_bufs, hn_rem) {
if (rbuf->raddr && ((rbuf->flags == ADSP_MMAP_ADD_PAGES) ||
(rbuf->flags == ADSP_MMAP_ADD_PAGES_LLC))) {
if ((rbuf->raddr == ud->vaddrout) &&
(rbuf->size == ud->size)) {
free = rbuf;
break;
}
}
}
spin_unlock(&fl->hlock);
if (free) {
VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, free->raddr,
free->phys, free->size, free->flags)));
if (err)
goto bail;
fastrpc_buf_free(rbuf, 0);
mutex_unlock(&fl->internal_map_mutex);
return err;
}
mutex_lock(&fl->map_mutex);
VERIFY(err, !(err = fastrpc_mmap_remove(fl, -1, ud->vaddrout,
ud->size, &map)));
mutex_unlock(&fl->map_mutex);
if (err)
goto bail;
VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, map->raddr,
map->phys, map->size, map->flags)));
if (err)
goto bail;
mutex_lock(&fl->map_mutex);
fastrpc_mmap_free(map, 0);
mutex_unlock(&fl->map_mutex);
bail:
if (err && map) {
mutex_lock(&fl->map_mutex);
fastrpc_mmap_add(map);
mutex_unlock(&fl->map_mutex);
}
mutex_unlock(&fl->internal_map_mutex);
return err;
}
/*
* fastrpc_internal_munmap_fd can only be used for buffers
* mapped with persist attributes. This can only be called
* once for any persist buffer
*/
static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
struct fastrpc_ioctl_munmap_fd *ud)
{
int err = 0;
struct fastrpc_mmap *map = NULL;
VERIFY(err, (fl && ud));
if (err) {
err = -EINVAL;
return err;
}
VERIFY(err, fl->dsp_proc_init == 1);
if (err) {
ADSPRPC_ERR(
"user application %s trying to unmap without initialization\n",
current->comm);
err = -EHOSTDOWN;
return err;
}
mutex_lock(&fl->internal_map_mutex);
mutex_lock(&fl->map_mutex);
err = fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map);
if (err) {
ADSPRPC_ERR(
"mapping not found to unmap fd 0x%x, va 0x%llx, len 0x%x, err %d\n",
ud->fd, (unsigned long long)ud->va,
(unsigned int)ud->len, err);
mutex_unlock(&fl->map_mutex);
goto bail;
}
if (map && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
map->attr = map->attr & (~FASTRPC_ATTR_KEEP_MAP);
fastrpc_mmap_free(map, 0);
}
mutex_unlock(&fl->map_mutex);
bail:
mutex_unlock(&fl->internal_map_mutex);
return err;
}
static int fastrpc_internal_mem_map(struct fastrpc_file *fl,
struct fastrpc_ioctl_mem_map *ud)
{
int err = 0;
struct fastrpc_mmap *map = NULL;
VERIFY(err, fl->dsp_proc_init == 1);
if (err) {
pr_err("adsprpc: ERROR: %s: user application %s trying to map without initialization\n",
__func__, current->comm);
err = EBADR;
goto bail;
}
/* create SMMU mapping */
mutex_lock(&fl->map_mutex);
VERIFY(err, !fastrpc_mmap_create(fl, ud->m.fd, ud->m.attrs,
ud->m.vaddrin, ud->m.length,
ud->m.flags, &map));
mutex_unlock(&fl->map_mutex);
if (err)
goto bail;
if (map->raddr) {
err = -EEXIST;
goto bail;
}
/* create DSP mapping */
VERIFY(err, !(err = fastrpc_mem_map_to_dsp(fl, ud->m.fd, ud->m.offset,
ud->m.flags, map->va, map->phys, ud->m.length, &map->raddr)));
if (err)
goto bail;
ud->m.vaddrout = map->raddr;
bail:
if (err) {
pr_err("adsprpc: %s failed to map fd %d flags %d err %d\n",
__func__, ud->m.fd, ud->m.flags, err);
if (map) {
mutex_lock(&fl->map_mutex);
fastrpc_mmap_free(map, 0);
mutex_unlock(&fl->map_mutex);
}
}
return err;
}
static int fastrpc_internal_mem_unmap(struct fastrpc_file *fl,
struct fastrpc_ioctl_mem_unmap *ud)
{
int err = 0;
struct fastrpc_mmap *map = NULL;
VERIFY(err, fl->dsp_proc_init == 1);
if (err) {
pr_err("adsprpc: ERROR: %s: user application %s trying to map without initialization\n",
__func__, current->comm);
err = EBADR;
goto bail;
}
mutex_lock(&fl->map_mutex);
VERIFY(err, !fastrpc_mmap_remove(fl, ud->um.fd,
(uintptr_t)ud->um.vaddr, ud->um.length, &map));
mutex_unlock(&fl->map_mutex);
if (err)
goto bail;
VERIFY(err, map->flags == FASTRPC_MAP_FD ||
map->flags == FASTRPC_MAP_FD_DELAYED ||
map->flags == FASTRPC_MAP_STATIC);
if (err) {
err = -EBADMSG;
goto bail;
}
/* remove mapping on DSP */
VERIFY(err, !(err = fastrpc_mem_unmap_to_dsp(fl, map->fd, map->flags,
map->raddr, map->phys, map->size)));
if (err)
goto bail;
/* remove SMMU mapping */
mutex_lock(&fl->map_mutex);
fastrpc_mmap_free(map, 0);
mutex_unlock(&fl->map_mutex);
map = NULL;
bail:
if (err) {
pr_err("adsprpc: %s failed to unmap fd %d addr 0x%llx length 0x%x err 0x%x\n",
__func__, ud->um.fd, ud->um.vaddr, ud->um.length, err);
/* Add back to map list in case of error to unmap on DSP */
if (map) {
mutex_lock(&fl->map_mutex);
fastrpc_mmap_add(map);
mutex_unlock(&fl->map_mutex);
}
}
return err;
}
static int fastrpc_internal_mmap(struct fastrpc_file *fl,
struct fastrpc_ioctl_mmap *ud)
{
struct fastrpc_mmap *map = NULL;
struct fastrpc_buf *rbuf = NULL;
unsigned long dma_attr = 0;
uintptr_t raddr = 0;
int err = 0;
VERIFY(err, fl->dsp_proc_init == 1);
if (err) {
ADSPRPC_ERR(
"user application %s trying to map without initialization\n",
current->comm);
err = -EHOSTDOWN;
return err;
}
mutex_lock(&fl->internal_map_mutex);
if ((ud->flags == ADSP_MMAP_ADD_PAGES) ||
(ud->flags == ADSP_MMAP_ADD_PAGES_LLC)) {
if (ud->vaddrin) {
err = -EINVAL;
ADSPRPC_ERR(
"adding user allocated pages is not supported\n");
goto bail;
}
dma_attr = DMA_ATTR_EXEC_MAPPING |
DMA_ATTR_DELAYED_UNMAP |
DMA_ATTR_NO_KERNEL_MAPPING;
if (ud->flags == ADSP_MMAP_ADD_PAGES_LLC)
dma_attr |= DMA_ATTR_IOMMU_USE_UPSTREAM_HINT;
err = fastrpc_buf_alloc(fl, ud->size, dma_attr, ud->flags,
USERHEAP_BUF, &rbuf);
if (err)
goto bail;
err = fastrpc_mmap_on_dsp(fl, ud->flags, 0,
rbuf->phys, rbuf->size, &raddr);
if (err)
goto bail;
rbuf->raddr = raddr;
} else {
uintptr_t va_to_dsp;
mutex_lock(&fl->map_mutex);
VERIFY(err, !(err = fastrpc_mmap_create(fl, ud->fd, 0,
(uintptr_t)ud->vaddrin, ud->size,
ud->flags, &map)));
mutex_unlock(&fl->map_mutex);
if (err)
goto bail;
if (ud->flags == ADSP_MMAP_HEAP_ADDR ||
ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
va_to_dsp = 0;
else
va_to_dsp = (uintptr_t)map->va;
VERIFY(err, 0 == (err = fastrpc_mmap_on_dsp(fl, ud->flags,
va_to_dsp, map->phys, map->size, &raddr)));
if (err)
goto bail;
map->raddr = raddr;
}
ud->vaddrout = raddr;
bail:
if (err) {
if (map) {
mutex_lock(&fl->map_mutex);
fastrpc_mmap_free(map, 0);
mutex_unlock(&fl->map_mutex);
}
if (!IS_ERR_OR_NULL(rbuf))
fastrpc_buf_free(rbuf, 0);
}
mutex_unlock(&fl->internal_map_mutex);
return err;
}
static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
int secure, struct fastrpc_session_ctx **session)
{
struct fastrpc_apps *me = &gfa;
uint64_t idx = 0;
int err = 0;
if (chan->sesscount) {
for (idx = 0; idx < chan->sesscount; ++idx) {
if (!chan->session[idx].used &&
chan->session[idx].smmu.secure == secure) {
chan->session[idx].used = 1;
break;
}
}
if (idx >= chan->sesscount) {
err = -EUSERS;
ADSPRPC_ERR(
"max concurrent sessions limit (%d) already reached on %s err %d\n",
chan->sesscount, chan->subsys, err);
goto bail;
}
chan->session[idx].smmu.faults = 0;
} else {
VERIFY(err, me->dev != NULL);
if (err) {
err = -ENODEV;
goto bail;
}
chan->session[0].dev = me->dev;
chan->session[0].smmu.dev = me->dev;
}
*session = &chan->session[idx];
bail:
return err;
}
static inline int get_cid_from_rpdev(struct rpmsg_device *rpdev)
{
int err = 0, cid = -1;
VERIFY(err, !IS_ERR_OR_NULL(rpdev));
if (err)
return -ENODEV;
if (!strcmp(rpdev->dev.parent->of_node->name, "cdsp"))
cid = CDSP_DOMAIN_ID;
else if (!strcmp(rpdev->dev.parent->of_node->name, "adsp"))
cid = ADSP_DOMAIN_ID;
else if (!strcmp(rpdev->dev.parent->of_node->name, "dsps"))
cid = SDSP_DOMAIN_ID;
else if (!strcmp(rpdev->dev.parent->of_node->name, "mdsp"))
cid = MDSP_DOMAIN_ID;
return cid;
}
static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
{
int err = 0;
int cid = -1;
VERIFY(err, !IS_ERR_OR_NULL(rpdev));
if (err)
return -ENODEV;
cid = get_cid_from_rpdev(rpdev);
VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
mutex_lock(&gcinfo[cid].rpmsg_mutex);
gcinfo[cid].rpdev = rpdev;
mutex_unlock(&gcinfo[cid].rpmsg_mutex);
ADSPRPC_INFO("opened rpmsg channel for %s\n",
gcinfo[cid].subsys);
bail:
if (err)
ADSPRPC_ERR("rpmsg probe of %s cid %d failed\n",
rpdev->dev.parent->of_node->name, cid);
return err;
}
static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
{
int err = 0;
int cid = -1;
struct fastrpc_apps *me = &gfa;
VERIFY(err, !IS_ERR_OR_NULL(rpdev));
if (err) {
err = -ENODEV;
return;
}
cid = get_cid_from_rpdev(rpdev);
VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
mutex_lock(&gcinfo[cid].rpmsg_mutex);
gcinfo[cid].rpdev = NULL;
mutex_unlock(&gcinfo[cid].rpmsg_mutex);
fastrpc_notify_drivers(me, cid);
ADSPRPC_INFO("closed rpmsg channel of %s\n",
gcinfo[cid].subsys);
bail:
if (err)
ADSPRPC_ERR("rpmsg remove of %s cid %d failed\n",
rpdev->dev.parent->of_node->name, cid);
}
static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
int len, void *priv, u32 addr)
{
struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)data;
struct smq_invoke_rspv2 *rspv2 = NULL;
struct smq_invoke_ctx *ctx = NULL;
struct fastrpc_apps *me = &gfa;
uint32_t index, rsp_flags = 0, early_wake_time = 0, ver = 0;
int err = 0, cid = -1, ignore_rpmsg_err = 0;
struct fastrpc_channel_ctx *chan = NULL;
unsigned long irq_flags = 0;
int64_t ns = 0;
cid = get_cid_from_rpdev(rpdev);
VERIFY(err, (cid >= ADSP_DOMAIN_ID && cid <= NUM_CHANNELS));
if (err) {
err = -ECHRNG;
goto bail;
}
chan = &me->channel[cid];
VERIFY(err, (rsp && len >= sizeof(*rsp)));
if (err)
goto bail;
if (len >= sizeof(struct smq_invoke_rspv2))
rspv2 = (struct smq_invoke_rspv2 *)data;
if (rspv2) {
early_wake_time = rspv2->early_wake_time;
rsp_flags = rspv2->flags;
ver = rspv2->version;
}
trace_fastrpc_rpmsg_response(cid, rsp->ctx,
rsp->retval, rsp_flags, early_wake_time);
ns = get_timestamp_in_ns();
fastrpc_update_rxmsg_buf(chan, rsp->ctx, rsp->retval,
rsp_flags, early_wake_time, ver, ns);
index = (uint32_t)GET_TABLE_IDX_FROM_CTXID(rsp->ctx);
VERIFY(err, index < FASTRPC_CTX_MAX);
if (err)
goto bail;
spin_lock_irqsave(&chan->ctxlock, irq_flags);
ctx = chan->ctxtable[index];
VERIFY(err, !IS_ERR_OR_NULL(ctx) &&
(ctx->ctxid == (rsp->ctx & ~CONTEXT_PD_CHECK)) &&
ctx->magic == FASTRPC_CTX_MAGIC);
if (err) {
/*
* Received an anticipatory COMPLETE_SIGNAL from DSP for a
* context after CPU successfully polling on memory and
* completed processing of context. Ignore the message.
*/
ignore_rpmsg_err = (rsp_flags == COMPLETE_SIGNAL) ? 1 : 0;
goto bail_unlock;
}
if (rspv2) {
VERIFY(err, rspv2->version == FASTRPC_RSP_VERSION2);
if (err)
goto bail_unlock;
}
context_notify_user(ctx, rsp->retval, rsp_flags, early_wake_time);
bail_unlock:
spin_unlock_irqrestore(&chan->ctxlock, irq_flags);
bail:
if (err) {
err = -ENOKEY;
if (!ignore_rpmsg_err)
ADSPRPC_ERR(
"invalid response data %pK, len %d from remote subsystem err %d\n",
data, len, err);
else
me->duplicate_rsp_err_cnt++;
}
return err;
}
static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
struct fastrpc_session_ctx **session)
{
int err = 0;
mutex_lock(&chan->smd_mutex);
if (!*session)
err = fastrpc_session_alloc_locked(chan, secure, session);
mutex_unlock(&chan->smd_mutex);
return err;
}
static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
struct fastrpc_session_ctx *session)
{
mutex_lock(&chan->smd_mutex);
session->used = 0;
mutex_unlock(&chan->smd_mutex);
}
static int fastrpc_file_free(struct fastrpc_file *fl)
{
struct hlist_node *n = NULL;
struct fastrpc_mmap *map = NULL, *lmap = NULL;
struct fastrpc_perf *perf = NULL, *fperf = NULL;
unsigned long flags;
int cid;
if (!fl)
return 0;
cid = fl->cid;
(void)fastrpc_release_current_dsp_process(fl);
spin_lock(&fl->apps->hlock);
hlist_del_init(&fl->hn);
spin_unlock(&fl->apps->hlock);
kfree(fl->debug_buf);
kfree(fl->gidlist.gids);
if (!fl->sctx) {
kfree(fl);
return 0;
}
spin_lock(&fl->hlock);
fl->file_close = 1;
spin_unlock(&fl->hlock);
//Dummy wake up to exit Async worker thread
spin_lock_irqsave(&fl->aqlock, flags);
atomic_add(1, &fl->async_queue_job_count);
wake_up_interruptible(&fl->async_wait_queue);
spin_unlock_irqrestore(&fl->aqlock, flags);
if (!IS_ERR_OR_NULL(fl->init_mem))
fastrpc_buf_free(fl->init_mem, 0);
fastrpc_context_list_dtor(fl);
fastrpc_cached_buf_list_free(fl);
if (!IS_ERR_OR_NULL(fl->hdr_bufs))
kfree(fl->hdr_bufs);
if (!IS_ERR_OR_NULL(fl->pers_hdr_buf))
fastrpc_buf_free(fl->pers_hdr_buf, 0);
mutex_lock(&fl->map_mutex);
do {
lmap = NULL;
hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
hlist_del_init(&map->hn);
lmap = map;
break;
}
fastrpc_mmap_free(lmap, 1);
} while (lmap);
mutex_unlock(&fl->map_mutex);
if (fl->sctx)
fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
if (fl->secsctx)
fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
mutex_lock(&fl->perf_mutex);
do {
struct hlist_node *pn = NULL;
fperf = NULL;
hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) {
hlist_del_init(&perf->hn);
fperf = perf;
break;
}
kfree(fperf);
} while (fperf);
fastrpc_remote_buf_list_free(fl);
mutex_unlock(&fl->perf_mutex);
mutex_destroy(&fl->perf_mutex);
mutex_destroy(&fl->map_mutex);
mutex_destroy(&fl->internal_map_mutex);
kfree(fl->dev_pm_qos_req);
kfree(fl);
return 0;
}
static int fastrpc_device_release(struct inode *inode, struct file *file)
{
struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
struct fastrpc_apps *me = &gfa;
u32 ii;
if (!fl)
return 0;
if (fl->qos_request && fl->dev_pm_qos_req) {
for (ii = 0; ii < me->silvercores.corecount; ii++) {
if (!dev_pm_qos_request_active(&fl->dev_pm_qos_req[ii]))
continue;
dev_pm_qos_remove_request(&fl->dev_pm_qos_req[ii]);
}
}
debugfs_remove(fl->debugfs_file);
fastrpc_file_free(fl);
file->private_data = NULL;
return 0;
}
static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
size_t count, loff_t *position)
{
struct fastrpc_apps *me = &gfa;
struct fastrpc_file *fl = filp->private_data;
struct hlist_node *n;
struct fastrpc_buf *buf = NULL;
struct fastrpc_mmap *map = NULL;
struct fastrpc_mmap *gmaps = NULL;
struct smq_invoke_ctx *ictx = NULL;
struct fastrpc_channel_ctx *chan = NULL;
unsigned int len = 0;
int i, j, sess_used = 0, ret = 0;
char *fileinfo = NULL;
char single_line[UL_SIZE] = "----------------";
char title[UL_SIZE] = "=========================";
fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
if (!fileinfo) {
ret = -ENOMEM;
goto bail;
}
if (fl == NULL) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n%s %s %s\n", title, " CHANNEL INFO ", title);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-7s|%-10s|%-14s|%-9s|%-13s\n",
"subsys", "sesscount", "issubsystemup",
"ssrcount", "session_used");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"-%s%s%s%s-\n", single_line, single_line,
single_line, single_line);
for (i = 0; i < NUM_CHANNELS; i++) {
sess_used = 0;
chan = &gcinfo[i];
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len, "%-7s", chan->subsys);
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len, "|%-10u",
chan->sesscount);
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len, "|%-14d",
chan->issubsystemup);
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len, "|%-9u",
chan->ssrcount);
for (j = 0; j < chan->sesscount; j++)
sess_used += chan->session[j].used;
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len, "|%-13d\n", sess_used);
}
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n%s%s%s\n", "=============",
" CMA HEAP ", "==============");
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len, "%-20s|%-20s\n", "addr", "size");
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len, "--%s%s---\n",
single_line, single_line);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n==========%s %s %s===========\n",
title, " GMAPS ", title);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20s|%-20s|%-20s|%-20s\n",
"fd", "phys", "size", "va");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n", single_line, single_line,
single_line, single_line, single_line);
hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20d|0x%-18llX|0x%-18X|0x%-20lX\n\n",
gmaps->fd, gmaps->phys,
(uint32_t)gmaps->size,
gmaps->va);
}
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20s|%-20s|%-20s|%-20s\n",
"len", "refs", "raddr", "flags");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n", single_line, single_line,
single_line, single_line, single_line);
hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"0x%-18X|%-20d|%-20lu|%-20u\n",
(uint32_t)gmaps->len, gmaps->refs,
gmaps->raddr, gmaps->flags);
}
} else {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n%s %13s %d\n", "cid", ":", fl->cid);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %12s %d\n", "tgid", ":", fl->tgid);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %7s %d\n", "sessionid", ":", fl->sessionid);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %8s %u\n", "ssrcount", ":", fl->ssrcount);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %14s %d\n", "pd", ":", fl->pd);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %9s %s\n", "servloc_name", ":", fl->servloc_name);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %6s %d\n", "file_close", ":", fl->file_close);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %9s %d\n", "profile", ":", fl->profile);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %3s %d\n", "smmu.coherent", ":",
fl->sctx->smmu.coherent);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %4s %d\n", "smmu.enabled", ":",
fl->sctx->smmu.enabled);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %9s %d\n", "smmu.cb", ":", fl->sctx->smmu.cb);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %5s %d\n", "smmu.secure", ":",
fl->sctx->smmu.secure);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s %5s %d\n", "smmu.faults", ":",
fl->sctx->smmu.faults);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n=======%s %s %s======\n", title,
" LIST OF MAPS ", title);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20s|%-20s|%-20s\n", "va", "phys", "size");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n",
single_line, single_line, single_line,
single_line, single_line);
hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"0x%-20lX|0x%-20llX|0x%-20zu\n\n",
map->va, map->phys,
map->size);
}
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20s|%-20s|%-20s|%-20s\n",
"len", "refs",
"raddr", "uncached");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n",
single_line, single_line, single_line,
single_line, single_line);
hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20zu|%-20d|0x%-20lX|%-20d\n\n",
map->len, map->refs, map->raddr,
map->uncached);
}
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20s|%-20s\n", "secure", "attr");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n",
single_line, single_line, single_line,
single_line, single_line);
hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20d|0x%-20lX\n\n",
map->secure, map->attr);
}
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n======%s %s %s======\n", title,
" LIST OF BUFS ", title);
spin_lock(&fl->hlock);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-19s|%-19s|%-19s\n",
"virt", "phys", "size");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n", single_line, single_line,
single_line, single_line, single_line);
hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
len += scnprintf(fileinfo + len,
DEBUGFS_SIZE - len,
"0x%-17p|0x%-17llX|%-19zu\n",
buf->virt, (uint64_t)buf->phys, buf->size);
}
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n%s %s %s\n", title,
" LIST OF PENDING SMQCONTEXTS ", title);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20s|%-10s|%-10s|%-10s|%-20s\n",
"sc", "pid", "tgid", "used", "ctxid");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n", single_line, single_line,
single_line, single_line, single_line);
hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"0x%-18X|%-10d|%-10d|%-10zu|0x%-20llX\n\n",
ictx->sc, ictx->pid, ictx->tgid,
ictx->used, ictx->ctxid);
}
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"\n%s %s %s\n", title,
" LIST OF INTERRUPTED SMQCONTEXTS ", title);
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20s|%-10s|%-10s|%-10s|%-20s\n",
"sc", "pid", "tgid", "used", "ctxid");
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%s%s%s%s%s\n", single_line, single_line,
single_line, single_line, single_line);
hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
"%-20u|%-20d|%-20d|%-20zu|0x%-20llX\n\n",
ictx->sc, ictx->pid, ictx->tgid,
ictx->used, ictx->ctxid);
}
spin_unlock(&fl->hlock);
}
if (len > DEBUGFS_SIZE)
len = DEBUGFS_SIZE;
ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
kfree(fileinfo);
bail:
return ret;
}
static const struct file_operations debugfs_fops = {
.open = simple_open,
.read = fastrpc_debugfs_read,
};
static int fastrpc_channel_open(struct fastrpc_file *fl)
{
struct fastrpc_apps *me = &gfa;
int cid = -1, err = 0;
VERIFY(err, fl && fl->sctx && fl->cid >= 0 && fl->cid < NUM_CHANNELS);
if (err) {
ADSPRPC_ERR("kernel session not initialized yet for %s\n",
current->comm);
err = -EBADR;
return err;
}
cid = fl->cid;
mutex_lock(&me->channel[cid].rpmsg_mutex);
VERIFY(err, NULL != me->channel[cid].rpdev);
if (err) {
err = -ENODEV;
mutex_unlock(&me->channel[cid].rpmsg_mutex);
goto bail;
}
mutex_unlock(&me->channel[cid].rpmsg_mutex);
mutex_lock(&me->channel[cid].smd_mutex);
if (me->channel[cid].ssrcount !=
me->channel[cid].prevssrcount) {
if (!me->channel[cid].issubsystemup) {
err = -ECONNREFUSED;
mutex_unlock(&me->channel[cid].smd_mutex);
goto bail;
}
}
fl->ssrcount = me->channel[cid].ssrcount;
if (cid == ADSP_DOMAIN_ID && me->channel[cid].ssrcount !=
me->channel[cid].prevssrcount) {
mutex_lock(&fl->map_mutex);
err = fastrpc_mmap_remove_ssr(fl);
if (err)
ADSPRPC_WARN(
"failed to unmap remote heap for %s (err %d)\n",
me->channel[cid].subsys, err);
mutex_unlock(&fl->map_mutex);
me->channel[cid].prevssrcount =
me->channel[cid].ssrcount;
}
mutex_unlock(&me->channel[cid].smd_mutex);
bail:
return err;
}
static inline void fastrpc_register_wakeup_source(struct device *dev,
const char *client_name, struct wakeup_source **device_wake_source)
{
struct wakeup_source *wake_source = NULL;
wake_source = wakeup_source_register(dev, client_name);
if (IS_ERR_OR_NULL(wake_source)) {
ADSPRPC_ERR(
"wakeup_source_register failed for dev %s, client %s with err %ld\n",
dev_name(dev), client_name, PTR_ERR(wake_source));
return;
}
*device_wake_source = wake_source;
}
static int fastrpc_device_open(struct inode *inode, struct file *filp)
{
int err = 0;
struct fastrpc_file *fl = NULL;
struct fastrpc_apps *me = &gfa;
/*
* Indicates the device node opened
* MINOR_NUM_DEV or MINOR_NUM_SECURE_DEV
*/
int dev_minor = MINOR(inode->i_rdev);
VERIFY(err, ((dev_minor == MINOR_NUM_DEV) ||
(dev_minor == MINOR_NUM_SECURE_DEV)));
if (err) {
ADSPRPC_ERR("Invalid dev minor num %d\n",
dev_minor);
return err;
}
VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
if (err) {
err = -ENOMEM;
return err;
}
context_list_ctor(&fl->clst);
spin_lock_init(&fl->hlock);
spin_lock_init(&fl->aqlock);
INIT_HLIST_HEAD(&fl->maps);
INIT_HLIST_HEAD(&fl->perf);
INIT_HLIST_HEAD(&fl->cached_bufs);
fl->num_cached_buf = 0;
INIT_HLIST_HEAD(&fl->remote_bufs);
init_waitqueue_head(&fl->async_wait_queue);
INIT_HLIST_NODE(&fl->hn);
fl->sessionid = 0;
fl->tgid_open = current->tgid;
fl->apps = me;
fl->mode = FASTRPC_MODE_SERIAL;
fl->cid = -1;
fl->dev_minor = dev_minor;
fl->init_mem = NULL;
memset(&fl->perf, 0, sizeof(fl->perf));
fl->qos_request = 0;
fl->dsp_proc_init = 0;
filp->private_data = fl;
mutex_init(&fl->internal_map_mutex);
mutex_init(&fl->map_mutex);
spin_lock(&me->hlock);
hlist_add_head(&fl->hn, &me->drivers);
spin_unlock(&me->hlock);
mutex_init(&fl->perf_mutex);
fl->dev_pm_qos_req = kcalloc(me->silvercores.corecount,
sizeof(struct dev_pm_qos_request),
GFP_KERNEL);
return 0;
}
static int fastrpc_get_process_gids(struct gid_list *gidlist)
{
struct group_info *group_info = get_current_groups();
int i = 0, err = 0, num_gids = group_info->ngroups + 1;
unsigned int *gids = NULL;
gids = kcalloc(num_gids, sizeof(unsigned int), GFP_KERNEL);
if (!gids) {
err = -ENOMEM;
goto bail;
}
/* Get the real GID */
gids[0] = __kgid_val(current_gid());
/* Get the supplemental GIDs */
for (i = 1; i < num_gids; i++)
gids[i] = __kgid_val(group_info->gid[i - 1]);
sort(gids, num_gids, sizeof(*gids), uint_cmp_func, NULL);
gidlist->gids = gids;
gidlist->gidcount = num_gids;
bail:
if (err)
kfree(gids);
return err;
}
static int fastrpc_set_process_info(struct fastrpc_file *fl)
{
int err = 0, buf_size = 0;
char strpid[PID_SIZE];
fl->tgid = current->tgid;
/*
* Third-party apps don't have permission to open the fastrpc device, so
* it is opened on their behalf by DSP HAL. This is detected by
* comparing current PID with the one stored during device open.
*/
if (current->tgid != fl->tgid_open)
fl->untrusted_process = true;
snprintf(strpid, PID_SIZE, "%d", current->pid);
if (debugfs_root) {
buf_size = strlen(current->comm) + strlen("_")
+ strlen(strpid) + 1;
spin_lock(&fl->hlock);
if (fl->debug_buf_alloced_attempted) {
spin_unlock(&fl->hlock);
return err;
}
fl->debug_buf_alloced_attempted = 1;
spin_unlock(&fl->hlock);
fl->debug_buf = kzalloc(buf_size, GFP_KERNEL);
if (!fl->debug_buf) {
err = -ENOMEM;
return err;
}
snprintf(fl->debug_buf, UL_SIZE, "%.10s%s%d",
current->comm, "_", current->pid);
fl->debugfs_file = debugfs_create_file(fl->debug_buf, 0644,
debugfs_root, fl, &debugfs_fops);
if (IS_ERR_OR_NULL(fl->debugfs_file)) {
pr_warn("Error: %s: %s: failed to create debugfs file %s\n",
current->comm, __func__, fl->debug_buf);
fl->debugfs_file = NULL;
kfree(fl->debug_buf);
fl->debug_buf = NULL;
}
}
return err;
}
static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
{
int err = 0;
uint32_t cid;
VERIFY(err, fl != NULL);
if (err)
goto bail;
fastrpc_get_process_gids(&fl->gidlist);
err = fastrpc_set_process_info(fl);
if (err)
goto bail;
if (fl->cid == -1) {
cid = *info;
VERIFY(err, cid < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
/* Check to see if the device node is non-secure */
if (fl->dev_minor == MINOR_NUM_DEV) {
/*
* If an app is trying to offload to a secure remote
* channel by opening the non-secure device node, allow
* the access if the subsystem supports unsigned
* offload. Untrusted apps will be restricted from
* offloading to signed PD using DSP HAL.
*/
if (fl->apps->channel[cid].secure == SECURE_CHANNEL
&& !fl->apps->channel[cid].unsigned_support) {
ADSPRPC_ERR(
"cannot use domain %d with non-secure device\n",
cid);
err = -EACCES;
goto bail;
}
}
fl->cid = cid;
fl->ssrcount = fl->apps->channel[cid].ssrcount;
mutex_lock(&fl->apps->channel[cid].smd_mutex);
err = fastrpc_session_alloc_locked(&fl->apps->channel[cid],
0, &fl->sctx);
mutex_unlock(&fl->apps->channel[cid].smd_mutex);
if (err)
goto bail;
}
VERIFY(err, fl->sctx != NULL);
if (err)
goto bail;
*info = (fl->sctx->smmu.enabled ? 1 : 0);
bail:
return err;
}
static int fastrpc_internal_control(struct fastrpc_file *fl,
struct fastrpc_ioctl_control *cp)
{
int err = 0;
unsigned int latency;
struct fastrpc_apps *me = &gfa;
u32 silver_core_count = me->silvercores.corecount, ii = 0, cpu;
VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
if (err) {
err = -EBADF;
goto bail;
}
VERIFY(err, !IS_ERR_OR_NULL(cp));
if (err) {
err = -EINVAL;
goto bail;
}
switch (cp->req) {
case FASTRPC_CONTROL_LATENCY:
latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
fl->apps->latency : PM_QOS_DEFAULT_VALUE;
VERIFY(err, latency != 0);
if (err) {
err = -EINVAL;
goto bail;
}
VERIFY(err, me->silvercores.coreno && fl->dev_pm_qos_req);
if (err)
goto bail;
for (ii = 0; ii < silver_core_count; ii++) {
cpu = me->silvercores.coreno[ii];
if (!fl->qos_request) {
err = dev_pm_qos_add_request(
get_cpu_device(cpu),
&fl->dev_pm_qos_req[ii],
DEV_PM_QOS_RESUME_LATENCY,
latency);
} else {
err = dev_pm_qos_update_request(
&fl->dev_pm_qos_req[ii],
latency);
}
if (err < 0) {
pr_warn("adsprpc: %s: %s: PM voting for cpu:%d failed, err %d, QoS update %d\n",
current->comm, __func__, cpu,
err, fl->qos_request);
break;
}
}
if (err >= 0)
fl->qos_request = 1;
/* Ensure CPU feature map updated to DSP for early WakeUp */
fastrpc_send_cpuinfo_to_dsp(fl);
break;
case FASTRPC_CONTROL_KALLOC:
cp->kalloc.kalloc_support = 1;
break;
case FASTRPC_CONTROL_WAKELOCK:
if (fl->dev_minor != MINOR_NUM_SECURE_DEV) {
ADSPRPC_ERR(
"PM voting not allowed for non-secure device node %d\n",
fl->dev_minor);
err = -EPERM;
goto bail;
}
fl->wake_enable = cp->wp.enable;
break;
case FASTRPC_CONTROL_PM:
if (!fl->wake_enable) {
/* Kernel PM voting not requested by this application */
err = -EACCES;
goto bail;
}
if (cp->pm.timeout > MAX_PM_TIMEOUT_MS)
fl->ws_timeout = MAX_PM_TIMEOUT_MS;
else
fl->ws_timeout = cp->pm.timeout;
fastrpc_pm_awake(fl, gcinfo[fl->cid].secure);
break;
case FASTRPC_CONTROL_DSPPROCESS_CLEAN:
(void)fastrpc_release_current_dsp_process(fl);
break;
default:
err = -EBADRQC;
break;
}
bail:
return err;
}
static int fastrpc_check_pd_status(struct fastrpc_file *fl, char *sloc_name)
{
int err = 0, session = -1, cid = -1;
struct fastrpc_apps *me = &gfa;
if (fl->servloc_name && sloc_name
&& !strcmp(fl->servloc_name, sloc_name)) {
err = fastrpc_get_spd_session(sloc_name, &session, &cid);
if (err || cid != fl->cid)
goto bail;
if (!me->channel[cid].spd[session].ispdup) {
err = -ENOTCONN;
goto bail;
}
}
bail:
return err;
}
static int fastrpc_setmode(unsigned long ioctl_param,
struct fastrpc_file *fl)
{
int err = 0;
switch ((uint32_t)ioctl_param) {
case FASTRPC_MODE_PARALLEL:
case FASTRPC_MODE_SERIAL:
fl->mode = (uint32_t)ioctl_param;
break;
case FASTRPC_MODE_PROFILE:
fl->profile = (uint32_t)ioctl_param;
break;
case FASTRPC_MODE_SESSION:
if (fl->untrusted_process) {
err = -EPERM;
ADSPRPC_ERR(
"multiple sessions not allowed for untrusted apps\n");
goto bail;
}
fl->sessionid = 1;
fl->tgid |= (1 << SESSION_ID_INDEX);
break;
default:
err = -ENOTTY;
break;
}
bail:
return err;
}
static int fastrpc_getperf(struct fastrpc_ioctl_perf *ioctl_perf,
void *param, struct fastrpc_file *fl)
{
int err = 0;
K_COPY_FROM_USER(err, 0, ioctl_perf,
param, sizeof(*ioctl_perf));
if (err) {
err = -EFAULT;
goto bail;
}
ioctl_perf->numkeys = PERF_KEY_MAX;
if (ioctl_perf->keys) {
char *keys = PERF_KEYS;
K_COPY_TO_USER(err, 0, (void *)ioctl_perf->keys,
keys, strlen(keys)+1);
if (err) {
err = -EFAULT;
goto bail;
}
}
if (ioctl_perf->data) {
struct fastrpc_perf *perf = NULL, *fperf = NULL;
struct hlist_node *n = NULL;
mutex_lock(&fl->perf_mutex);
hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
if (perf->tid == current->pid) {
fperf = perf;
break;
}
}
mutex_unlock(&fl->perf_mutex);
if (fperf) {
K_COPY_TO_USER(err, 0,
(void *)ioctl_perf->data, fperf,
sizeof(*fperf) - sizeof(struct hlist_node));
}
}
K_COPY_TO_USER(err, 0, param, ioctl_perf, sizeof(*ioctl_perf));
if (err) {
err = -EFAULT;
goto bail;
}
bail:
return err;
}
static int fastrpc_control(struct fastrpc_ioctl_control *cp,
void *param, struct fastrpc_file *fl)
{
int err = 0;
K_COPY_FROM_USER(err, 0, cp, param,
sizeof(*cp));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_control(fl, cp)));
if (err)
goto bail;
if (cp->req == FASTRPC_CONTROL_KALLOC) {
K_COPY_TO_USER(err, 0, param, cp, sizeof(*cp));
if (err) {
err = -EFAULT;
goto bail;
}
}
bail:
return err;
}
static int fastrpc_get_dsp_info(
struct fastrpc_ioctl_capability *cap,
void *param, struct fastrpc_file *fl)
{
int err = 0;
K_COPY_FROM_USER(err, 0, cap, param,
sizeof(struct fastrpc_ioctl_capability));
VERIFY(err, cap->domain < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
cap->capability = 0;
err = fastrpc_get_info_from_kernel(cap, fl);
if (err)
goto bail;
K_COPY_TO_USER(err, 0, &((struct fastrpc_ioctl_capability *)
param)->capability, &cap->capability, sizeof(cap->capability));
bail:
return err;
}
union fastrpc_ioctl_param {
struct fastrpc_ioctl_invoke_async inv;
struct fastrpc_ioctl_mem_map mem_map;
struct fastrpc_ioctl_mem_unmap mem_unmap;
struct fastrpc_ioctl_mmap mmap;
struct fastrpc_ioctl_mmap_64 mmap64;
struct fastrpc_ioctl_munmap munmap;
struct fastrpc_ioctl_munmap_64 munmap64;
struct fastrpc_ioctl_munmap_fd munmap_fd;
struct fastrpc_ioctl_init_attrs init;
struct fastrpc_ioctl_perf perf;
struct fastrpc_ioctl_control cp;
struct fastrpc_ioctl_capability cap;
struct fastrpc_ioctl_invoke2 inv2;
};
static inline int fastrpc_mmap_device_ioctl(struct fastrpc_file *fl,
unsigned int ioctl_num, union fastrpc_ioctl_param *p,
void *param)
{
union {
struct fastrpc_ioctl_mmap mmap;
struct fastrpc_ioctl_munmap munmap;
} i;
int err = 0;
switch (ioctl_num) {
case FASTRPC_IOCTL_MEM_MAP:
K_COPY_FROM_USER(err, 0, &p->mem_map, param,
sizeof(p->mem_map));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_mem_map(fl,
&p->mem_map)));
if (err)
goto bail;
K_COPY_TO_USER(err, 0, param, &p->mem_map, sizeof(p->mem_map));
if (err) {
err = -EFAULT;
goto bail;
}
break;
case FASTRPC_IOCTL_MEM_UNMAP:
K_COPY_FROM_USER(err, 0, &p->mem_unmap, param,
sizeof(p->mem_unmap));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_mem_unmap(fl,
&p->mem_unmap)));
if (err)
goto bail;
K_COPY_TO_USER(err, 0, param, &p->mem_unmap,
sizeof(p->mem_unmap));
if (err) {
err = -EFAULT;
goto bail;
}
break;
case FASTRPC_IOCTL_MMAP:
K_COPY_FROM_USER(err, 0, &p->mmap, param,
sizeof(p->mmap));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p->mmap)));
if (err)
goto bail;
K_COPY_TO_USER(err, 0, param, &p->mmap, sizeof(p->mmap));
if (err) {
err = -EFAULT;
goto bail;
}
break;
case FASTRPC_IOCTL_MUNMAP:
K_COPY_FROM_USER(err, 0, &p->munmap, param,
sizeof(p->munmap));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
&p->munmap)));
if (err)
goto bail;
break;
case FASTRPC_IOCTL_MMAP_64:
K_COPY_FROM_USER(err, 0, &p->mmap64, param,
sizeof(p->mmap64));
if (err) {
err = -EFAULT;
goto bail;
}
get_fastrpc_ioctl_mmap_64(&p->mmap64, &i.mmap);
VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &i.mmap)));
if (err)
goto bail;
put_fastrpc_ioctl_mmap_64(&p->mmap64, &i.mmap);
K_COPY_TO_USER(err, 0, param, &p->mmap64, sizeof(p->mmap64));
if (err) {
err = -EFAULT;
goto bail;
}
break;
case FASTRPC_IOCTL_MUNMAP_64:
K_COPY_FROM_USER(err, 0, &p->munmap64, param,
sizeof(p->munmap64));
if (err) {
err = -EFAULT;
goto bail;
}
get_fastrpc_ioctl_munmap_64(&p->munmap64, &i.munmap);
VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
&i.munmap)));
if (err)
goto bail;
break;
case FASTRPC_IOCTL_MUNMAP_FD:
K_COPY_FROM_USER(err, 0, &p->munmap_fd, param,
sizeof(p->munmap_fd));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
&p->munmap_fd)));
if (err)
goto bail;
break;
default:
err = -ENOTTY;
pr_info("bad ioctl: %d\n", ioctl_num);
break;
}
bail:
return err;
}
static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
unsigned long ioctl_param)
{
union fastrpc_ioctl_param p;
void *param = (char *)ioctl_param;
struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
int size = 0, err = 0;
uint32_t info;
p.inv.fds = NULL;
p.inv.attrs = NULL;
p.inv.crc = NULL;
p.inv.job = NULL;
err = fastrpc_check_pd_status(fl,
AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME);
if (err)
goto bail;
spin_lock(&fl->hlock);
if (fl->file_close == 1) {
err = -ESHUTDOWN;
pr_warn("adsprpc: fastrpc_device_release is happening, So not sending any new requests to DSP\n");
spin_unlock(&fl->hlock);
goto bail;
}
spin_unlock(&fl->hlock);
switch (ioctl_num) {
case FASTRPC_IOCTL_INVOKE:
size = sizeof(struct fastrpc_ioctl_invoke);
/* fall through */
case FASTRPC_IOCTL_INVOKE_FD:
if (!size)
size = sizeof(struct fastrpc_ioctl_invoke_fd);
/* fall through */
case FASTRPC_IOCTL_INVOKE_ATTRS:
if (!size)
size = sizeof(struct fastrpc_ioctl_invoke_attrs);
/* fall through */
case FASTRPC_IOCTL_INVOKE_CRC:
if (!size)
size = sizeof(struct fastrpc_ioctl_invoke_crc);
K_COPY_FROM_USER(err, 0, &p.inv, param, size);
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
USER_MSG, &p.inv)));
if (err)
goto bail;
break;
case FASTRPC_IOCTL_INVOKE2:
K_COPY_FROM_USER(err, 0, &p.inv2, param,
sizeof(struct fastrpc_ioctl_invoke2));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_internal_invoke2(fl, &p.inv2)));
if (err)
goto bail;
break;
case FASTRPC_IOCTL_SETMODE:
err = fastrpc_setmode(ioctl_param, fl);
break;
case FASTRPC_IOCTL_GETPERF:
err = fastrpc_getperf(&p.perf, param, fl);
break;
case FASTRPC_IOCTL_CONTROL:
err = fastrpc_control(&p.cp, param, fl);
break;
case FASTRPC_IOCTL_GETINFO:
K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
if (err)
goto bail;
K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
if (err) {
err = -EFAULT;
goto bail;
}
break;
case FASTRPC_IOCTL_INIT:
p.init.attrs = 0;
p.init.siglen = 0;
size = sizeof(struct fastrpc_ioctl_init);
/* fall through */
case FASTRPC_IOCTL_INIT_ATTRS:
if (!size)
size = sizeof(struct fastrpc_ioctl_init_attrs);
K_COPY_FROM_USER(err, 0, &p.init, param, size);
if (err) {
err = -EFAULT;
goto bail;
}
VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
if (err)
goto bail;
break;
case FASTRPC_IOCTL_GET_DSP_INFO:
err = fastrpc_get_dsp_info(&p.cap, param, fl);
break;
case FASTRPC_IOCTL_MEM_MAP:
/* fall through */
case FASTRPC_IOCTL_MEM_UNMAP:
/* fall through */
case FASTRPC_IOCTL_MMAP:
/* fall through */
case FASTRPC_IOCTL_MUNMAP:
/* fall through */
case FASTRPC_IOCTL_MMAP_64:
/* fall through */
case FASTRPC_IOCTL_MUNMAP_64:
/* fall through */
case FASTRPC_IOCTL_MUNMAP_FD:
err = fastrpc_mmap_device_ioctl(fl, ioctl_num, &p, param);
break;
default:
err = -ENOTTY;
pr_info("bad ioctl: %d\n", ioctl_num);
break;
}
bail:
return err;
}
static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
unsigned long code,
void *data)
{
struct fastrpc_apps *me = &gfa;
struct fastrpc_channel_ctx *ctx;
struct notif_data *notifdata = (struct notif_data *)data;
int cid = -1;
ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
cid = ctx - &me->channel[0];
if (code == SUBSYS_BEFORE_SHUTDOWN) {
pr_info("adsprpc: %s: %s subsystem is restarting\n",
__func__, gcinfo[cid].subsys);
mutex_lock(&me->channel[cid].smd_mutex);
ctx->ssrcount++;
ctx->issubsystemup = 0;
mutex_unlock(&me->channel[cid].smd_mutex);
} else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
if (cid == RH_CID) {
if (me->channel[RH_CID].rh_dump_dev &&
notifdata->enable_ramdump) {
me->channel[RH_CID].ramdumpenabled = 1;
}
}
pr_info("adsprpc: %s: received RAMDUMP notification for %s\n",
__func__, gcinfo[cid].subsys);
} else if (code == SUBSYS_AFTER_POWERUP) {
pr_info("adsprpc: %s: %s subsystem is up\n",
__func__, gcinfo[cid].subsys);
ctx->issubsystemup = 1;
}
return NOTIFY_DONE;
}
static int fastrpc_pdr_notifier_cb(struct notifier_block *pdrnb,
unsigned long code,
void *data)
{
struct fastrpc_apps *me = &gfa;
struct fastrpc_static_pd *spd;
struct notif_data *notifdata = (struct notif_data *)data;
spd = container_of(pdrnb, struct fastrpc_static_pd, pdrnb);
if (code == SERVREG_NOTIF_SERVICE_STATE_DOWN_V01) {
pr_info("adsprpc: %s: %s (%s) is down for PDR on %s\n",
__func__, spd->spdname, spd->servloc_name,
gcinfo[spd->cid].subsys);
mutex_lock(&me->channel[spd->cid].smd_mutex);
spd->pdrcount++;
spd->ispdup = 0;
mutex_unlock(&me->channel[spd->cid].smd_mutex);
if (!strcmp(spd->servloc_name,
AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME))
me->staticpd_flags = 0;
fastrpc_notify_pdr_drivers(me, spd->servloc_name);
} else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
if (spd->cid == RH_CID) {
if (me->channel[RH_CID].rh_dump_dev &&
notifdata->enable_ramdump) {
me->channel[RH_CID].ramdumpenabled = 1;
}
}
pr_info("adsprpc: %s: received %s RAMDUMP notification for %s (%s)\n",
__func__, gcinfo[spd->cid].subsys,
spd->spdname, spd->servloc_name);
} else if (code == SERVREG_NOTIF_SERVICE_STATE_UP_V01) {
pr_info("adsprpc: %s: %s (%s) is up on %s\n",
__func__, spd->spdname, spd->servloc_name,
gcinfo[spd->cid].subsys);
spd->ispdup = 1;
}
return NOTIFY_DONE;
}
/*
* The service locator callback function where the PDR notification
* callback functions are registered.
* (like audioPD on ADSP, sensorPD on SLPI/ADSP)
*/
static int fastrpc_get_service_location_notify(struct notifier_block *nb,
unsigned long opcode, void *data)
{
struct fastrpc_static_pd *spd;
struct pd_qmi_client_data *pdr = data;
int curr_state = 0, i = 0;
char *cb_pdname = NULL, *subsys = NULL;
uint32_t instance_id = 0;
spd = container_of(nb, struct fastrpc_static_pd, get_service_nb);
subsys = gcinfo[spd->cid].subsys;
if (opcode == LOCATOR_DOWN) {
pr_warn("adsprpc: %s: PDR notifier locator for %s is down for %s\n",
__func__, subsys, spd->servloc_name);
return NOTIFY_DONE;
}
for (i = 0; i < pdr->total_domains; i++) {
cb_pdname = pdr->domain_list[i].name;
instance_id = pdr->domain_list[i].instance_id;
/* Check the client and staticPD in the callback */
if (COMPARE_SERVICE_LOCATOR_NAMES(spd->servloc_name,
AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
cb_pdname, ADSP_AUDIOPD_NAME) ||
COMPARE_SERVICE_LOCATOR_NAMES(spd->servloc_name,
SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME,
cb_pdname, ADSP_SENSORPD_NAME) ||
COMPARE_SERVICE_LOCATOR_NAMES(spd->servloc_name,
SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME,
cb_pdname, SLPI_SENSORPD_NAME)) {
goto pdr_register;
}
}
return NOTIFY_DONE;
pdr_register:
if (!spd->pdrhandle) {
/* Register the PDR notifier callback function */
spd->pdrhandle = service_notif_register_notifier(cb_pdname,
instance_id, &spd->pdrnb, &curr_state);
if (IS_ERR_OR_NULL(spd->pdrhandle))
pr_warn("adsprpc: %s: PDR notifier for %s register failed for %s (%s) with err %ld\n",
__func__, subsys, cb_pdname, spd->servloc_name,
PTR_ERR(spd->pdrhandle));
else
pr_info("adsprpc: %s: PDR notifier for %s registered for %s (%s)\n",
__func__, subsys, cb_pdname, spd->servloc_name);
} else {
pr_warn("adsprpc: %s: %s (%s) notifier is already registered for %s\n",
__func__, cb_pdname, spd->servloc_name, subsys);
}
if (curr_state == SERVREG_NOTIF_SERVICE_STATE_UP_V01) {
pr_info("adsprpc: %s: %s (%s) PDR service for %s is up\n",
__func__, spd->servloc_name, cb_pdname, subsys);
spd->ispdup = 1;
} else if (curr_state == SERVREG_NOTIF_SERVICE_STATE_UNINIT_V01) {
pr_info("adsprpc: %s: %s (%s) PDR service for %s is uninitialized\n",
__func__, spd->servloc_name, cb_pdname, subsys);
}
return NOTIFY_DONE;
}
static const struct file_operations fops = {
.open = fastrpc_device_open,
.release = fastrpc_device_release,
.unlocked_ioctl = fastrpc_device_ioctl,
.compat_ioctl = compat_fastrpc_device_ioctl,
};
static const struct of_device_id fastrpc_match_table[] = {
{ .compatible = "qcom,msm-fastrpc-adsp", },
{ .compatible = "qcom,msm-fastrpc-compute", },
{ .compatible = "qcom,msm-fastrpc-compute-cb", },
{ .compatible = "qcom,msm-adsprpc-mem-region", },
{}
};
static int fastrpc_cb_probe(struct device *dev)
{
struct fastrpc_channel_ctx *chan;
struct fastrpc_session_ctx *sess;
struct of_phandle_args iommuspec;
struct fastrpc_apps *me = &gfa;
const char *name;
int err = 0, cid = -1, i = 0;
u32 sharedcb_count = 0, j = 0;
uint32_t dma_addr_pool[2] = {0, 0};
VERIFY(err, NULL != (name = of_get_property(dev->of_node,
"label", NULL)));
if (err)
goto bail;
for (i = 0; i < NUM_CHANNELS; i++) {
if (!gcinfo[i].name)
continue;
if (!strcmp(name, gcinfo[i].name))
break;
}
VERIFY(err, i < NUM_CHANNELS);
if (err) {
err = -ECHRNG;
goto bail;
}
cid = i;
chan = &gcinfo[i];
VERIFY(err, chan->sesscount < NUM_SESSIONS);
if (err)
goto bail;
err = of_parse_phandle_with_args(dev->of_node, "iommus",
"#iommu-cells", 0, &iommuspec);
if (err) {
pr_err("Error: adsprpc: %s: parsing iommu arguments failed for %s with err %d\n",
__func__, dev_name(dev), err);
goto bail;
}
sess = &chan->session[chan->sesscount];
sess->used = 0;
#if IS_ENABLED(CONFIG_ADSPRPC_QGKI)
/*
* On a GKI kernel, the DMA driver does not support I/O coherency and
* the fastrpc driver needs to do the cache maintenance. So this
* device-tree property needs to be read only on a QGKI kernel.
*/
sess->smmu.coherent = of_property_read_bool(dev->of_node,
"dma-coherent-hint-cached");
#endif
sess->smmu.secure = of_property_read_bool(dev->of_node,
"qcom,secure-context-bank");
sess->smmu.cb = iommuspec.args[0] & 0xf;
sess->smmu.dev = dev;
sess->smmu.dev_name = dev_name(dev);
sess->smmu.enabled = 1;
if (!sess->smmu.dev->dma_parms)
sess->smmu.dev->dma_parms = devm_kzalloc(sess->smmu.dev,
sizeof(*sess->smmu.dev->dma_parms), GFP_KERNEL);
dma_set_max_seg_size(sess->smmu.dev, DMA_BIT_MASK(32));
dma_set_seg_boundary(sess->smmu.dev, (unsigned long)DMA_BIT_MASK(64));
err = iommu_dma_enable_best_fit_algo(sess->smmu.dev);
if (err) {
ADSPRPC_ERR("enabling SMMU best fit algo failed for %s with err %d\n",
sess->smmu.dev_name, err);
goto bail;
}
sess->smmu.best_fit = 1;
of_property_read_u32_array(dev->of_node, "qcom,iommu-dma-addr-pool",
dma_addr_pool, 2);
me->max_size_limit = (dma_addr_pool[1] == 0 ? 0x78000000 :
dma_addr_pool[1]);
if (of_get_property(dev->of_node, "shared-cb", NULL) != NULL) {
err = of_property_read_u32(dev->of_node, "shared-cb",
&sharedcb_count);
if (err)
goto bail;
if (sharedcb_count > 0) {
struct fastrpc_session_ctx *dup_sess;
for (j = 1; j < sharedcb_count &&
chan->sesscount < NUM_SESSIONS; j++) {
chan->sesscount++;
dup_sess = &chan->session[chan->sesscount];
memcpy(dup_sess, sess,
sizeof(struct fastrpc_session_ctx));
}
}
}
chan->sesscount++;
if (debugfs_root && !debugfs_global_file) {
debugfs_global_file = debugfs_create_file("global", 0644,
debugfs_root, NULL, &debugfs_fops);
if (IS_ERR_OR_NULL(debugfs_global_file)) {
pr_warn("Error: %s: %s: failed to create debugfs global file\n",
current->comm, __func__);
debugfs_global_file = NULL;
}
}
bail:
return err;
}
static void init_secure_vmid_list(struct device *dev, char *prop_name,
struct secure_vm *destvm)
{
int err = 0;
u32 len = 0, i = 0;
u32 *rhvmlist = NULL;
u32 *rhvmpermlist = NULL;
if (!of_find_property(dev->of_node, prop_name, &len))
goto bail;
if (len == 0)
goto bail;
len /= sizeof(u32);
VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
if (err)
goto bail;
VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
GFP_KERNEL)));
if (err)
goto bail;
for (i = 0; i < len; i++) {
err = of_property_read_u32_index(dev->of_node, prop_name, i,
&rhvmlist[i]);
if (err) {
pr_err("Error: adsprpc: %s: failed to read VMID\n",
__func__);
goto bail;
}
ADSPRPC_INFO("secure VMID = %d\n",
rhvmlist[i]);
rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC;
}
destvm->vmid = rhvmlist;
destvm->vmperm = rhvmpermlist;
destvm->vmcount = len;
bail:
if (err) {
kfree(rhvmlist);
kfree(rhvmpermlist);
}
}
static void init_qos_cores_list(struct device *dev, char *prop_name,
struct qos_cores *silvercores)
{
int err = 0;
u32 len = 0, i = 0;
u32 *coreslist = NULL;
if (!of_find_property(dev->of_node, prop_name, &len))
goto bail;
if (len == 0)
goto bail;
len /= sizeof(u32);
VERIFY(err, NULL != (coreslist = kcalloc(len, sizeof(u32),
GFP_KERNEL)));
if (err)
goto bail;
for (i = 0; i < len; i++) {
err = of_property_read_u32_index(dev->of_node, prop_name, i,
&coreslist[i]);
if (err) {
pr_err("adsprpc: %s: failed to read QOS cores list\n",
__func__);
goto bail;
}
}
silvercores->coreno = coreslist;
silvercores->corecount = len;
bail:
if (err) {
kfree(coreslist);
}
}
static void fastrpc_init_privileged_gids(struct device *dev, char *prop_name,
struct gid_list *gidlist)
{
int err = 0;
u32 len = 0, i = 0;
u32 *gids = NULL;
if (!of_find_property(dev->of_node, prop_name, &len))
goto bail;
if (len == 0)
goto bail;
len /= sizeof(u32);
gids = kcalloc(len, sizeof(u32), GFP_KERNEL);
if (!gids) {
err = ENOMEM;
goto bail;
}
for (i = 0; i < len; i++) {
err = of_property_read_u32_index(dev->of_node, prop_name,
i, &gids[i]);
if (err) {
pr_err("Error: adsprpc: %s: failed to read GID %u\n",
__func__, i);
goto bail;
}
pr_info("adsprpc: %s: privileged GID: %u\n", __func__, gids[i]);
}
sort(gids, len, sizeof(*gids), uint_cmp_func, NULL);
gidlist->gids = gids;
gidlist->gidcount = len;
bail:
if (err)
kfree(gids);
}
static void configure_secure_channels(uint32_t secure_domains)
{
struct fastrpc_apps *me = &gfa;
int ii = 0;
/*
* secure_domains contains the bitmask of the secure channels
* Bit 0 - ADSP
* Bit 1 - MDSP
* Bit 2 - SLPI
* Bit 3 - CDSP
*/
for (ii = ADSP_DOMAIN_ID; ii <= CDSP_DOMAIN_ID; ++ii) {
int secure = (secure_domains >> ii) & 0x01;
me->channel[ii].secure = secure;
ADSPRPC_INFO("domain %d configured as secure %d\n", ii, secure);
}
}
/*
* This function is used to create the service locator required for
* registering for remote process restart (PDR) notifications if that
* PDR property has been enabled in the fastrpc node on the DTSI.
*/
static int fastrpc_setup_service_locator(struct device *dev,
const char *propname, char *client_name, char *service_name)
{
int err = 0, session = -1, cid = -1;
struct fastrpc_apps *me = &gfa;
if (of_property_read_bool(dev->of_node, propname)) {
err = fastrpc_get_spd_session(client_name, &session, &cid);
if (err)
goto bail;
/* Register the service locator's callback function */
me->channel[cid].spd[session].get_service_nb.notifier_call =
fastrpc_get_service_location_notify;
err = get_service_location(client_name, service_name,
&me->channel[cid].spd[session].get_service_nb);
if (err)
pr_warn("adsprpc: %s: get service location failed with %d for %s (%s)\n",
__func__, err, service_name, client_name);
else
pr_info("adsprpc: %s: service location enabled for %s (%s)\n",
__func__, service_name, client_name);
}
bail:
return err;
}
static int fastrpc_probe(struct platform_device *pdev)
{
int err = 0;
struct fastrpc_apps *me = &gfa;
struct device *dev = &pdev->dev;
int ret = 0;
uint32_t secure_domains = 0;
if (of_device_is_compatible(dev->of_node,
"qcom,msm-fastrpc-compute")) {
init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
&gcinfo[0].rhvm);
fastrpc_init_privileged_gids(dev, "qcom,fastrpc-gids",
&me->gidlist);
init_qos_cores_list(dev, "qcom,qos-cores",
&me->silvercores);
of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
&me->latency);
if (of_get_property(dev->of_node,
"qcom,secure-domains", NULL) != NULL) {
VERIFY(err, !of_property_read_u32(dev->of_node,
"qcom,secure-domains",
&secure_domains));
if (!err)
configure_secure_channels(secure_domains);
else
pr_info("adsprpc: unable to read the domain configuration from dts\n");
}
}
if (of_device_is_compatible(dev->of_node,
"qcom,msm-fastrpc-compute-cb"))
return fastrpc_cb_probe(dev);
if (of_device_is_compatible(dev->of_node,
"qcom,msm-adsprpc-mem-region")) {
me->dev = dev;
ret = of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
if (ret) {
pr_warn("adsprpc: Error: %s: initialization of memory region adsp_mem failed with %d\n",
__func__, ret);
}
goto bail;
}
me->legacy_remote_heap = of_property_read_bool(dev->of_node,
"qcom,fastrpc-legacy-remote-heap");
fastrpc_setup_service_locator(dev, AUDIO_PDR_ADSP_DTSI_PROPERTY_NAME,
AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
AUDIO_PDR_ADSP_SERVICE_NAME);
fastrpc_setup_service_locator(dev, SENSORS_PDR_ADSP_DTSI_PROPERTY_NAME,
SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME,
SENSORS_PDR_ADSP_SERVICE_NAME);
fastrpc_setup_service_locator(dev, SENSORS_PDR_SLPI_DTSI_PROPERTY_NAME,
SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME,
SENSORS_PDR_SLPI_SERVICE_NAME);
err = of_platform_populate(pdev->dev.of_node,
fastrpc_match_table,
NULL, &pdev->dev);
if (err)
goto bail;
bail:
return err;
}
static void fastrpc_deinit(void)
{
struct fastrpc_channel_ctx *chan = gcinfo;
int i, j;
for (i = 0; i < NUM_CHANNELS; i++, chan++) {
for (j = 0; j < NUM_SESSIONS; j++) {
struct fastrpc_session_ctx *sess = &chan->session[j];
if (sess->smmu.dev)
sess->smmu.dev = NULL;
}
kfree(chan->rhvm.vmid);
kfree(chan->rhvm.vmperm);
}
}
static struct platform_driver fastrpc_driver = {
.probe = fastrpc_probe,
.driver = {
.name = "fastrpc",
.of_match_table = fastrpc_match_table,
.suppress_bind_attrs = true,
},
};
static const struct rpmsg_device_id fastrpc_rpmsg_match[] = {
{ FASTRPC_GLINK_GUID },
{ },
};
static const struct of_device_id fastrpc_rpmsg_of_match[] = {
{ .compatible = "qcom,msm-fastrpc-rpmsg" },
{ },
};
MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
static struct rpmsg_driver fastrpc_rpmsg_client = {
.id_table = fastrpc_rpmsg_match,
.probe = fastrpc_rpmsg_probe,
.remove = fastrpc_rpmsg_remove,
.callback = fastrpc_rpmsg_callback,
.drv = {
.name = "qcom,msm_fastrpc_rpmsg",
.of_match_table = fastrpc_rpmsg_of_match,
},
};
static int __init fastrpc_device_init(void)
{
struct fastrpc_apps *me = &gfa;
int err = 0, i;
debugfs_root = debugfs_create_dir("adsprpc", NULL);
if (IS_ERR_OR_NULL(debugfs_root)) {
pr_warn("Error: %s: %s: failed to create debugfs root dir\n",
current->comm, __func__);
debugfs_remove_recursive(debugfs_root);
debugfs_root = NULL;
}
memset(me, 0, sizeof(*me));
fastrpc_init(me);
me->dev = NULL;
me->legacy_remote_heap = false;
VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
if (err)
goto register_bail;
VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
DEVICE_NAME));
if (err)
goto alloc_chrdev_bail;
cdev_init(&me->cdev, &fops);
me->cdev.owner = THIS_MODULE;
VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
NUM_DEVICES));
if (err)
goto cdev_init_bail;
me->class = class_create(THIS_MODULE, "fastrpc");
VERIFY(err, !IS_ERR(me->class));
if (err)
goto class_create_bail;
me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
/*
* Create devices and register with sysfs
* Create first device with minor number 0
*/
me->non_secure_dev = device_create(me->class, NULL,
MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV),
NULL, DEVICE_NAME);
VERIFY(err, !IS_ERR_OR_NULL(me->non_secure_dev));
if (err) {
err = -ENODEV;
goto device_create_bail;
}
/* Create secure device with minor number for secure device */
me->secure_dev = device_create(me->class, NULL,
MKDEV(MAJOR(me->dev_no), MINOR_NUM_SECURE_DEV),
NULL, DEVICE_NAME_SECURE);
VERIFY(err, !IS_ERR_OR_NULL(me->secure_dev));
if (err)
goto device_create_bail;
for (i = 0; i < NUM_CHANNELS; i++) {
me->jobid[i] = 1;
me->channel[i].dev = me->secure_dev;
if (i == CDSP_DOMAIN_ID)
me->channel[i].dev = me->non_secure_dev;
me->channel[i].ssrcount = 0;
me->channel[i].prevssrcount = 0;
me->channel[i].issubsystemup = 1;
me->channel[i].ramdumpenabled = 0;
me->channel[i].rh_dump_dev = NULL;
me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
me->channel[i].handle = subsys_notif_register_notifier(
gcinfo[i].subsys,
&me->channel[i].nb);
if (IS_ERR_OR_NULL(me->channel[i].handle))
pr_warn("adsprpc: %s: SSR notifier register failed for %s with err %d\n",
__func__, gcinfo[i].subsys,
PTR_ERR(me->channel[i].handle));
else
pr_info("adsprpc: %s: SSR notifier registered for %s\n",
__func__, gcinfo[i].subsys);
}
err = register_rpmsg_driver(&fastrpc_rpmsg_client);
if (err) {
pr_err("Error: adsprpc: %s: register_rpmsg_driver failed with err %d\n",
__func__, err);
goto device_create_bail;
}
me->rpmsg_register = 1;
fastrpc_register_wakeup_source(me->non_secure_dev,
FASTRPC_NON_SECURE_WAKE_SOURCE_CLIENT_NAME,
&me->wake_source);
fastrpc_register_wakeup_source(me->secure_dev,
FASTRPC_SECURE_WAKE_SOURCE_CLIENT_NAME,
&me->wake_source_secure);
return 0;
device_create_bail:
for (i = 0; i < NUM_CHANNELS; i++) {
if (me->channel[i].handle)
subsys_notif_unregister_notifier(me->channel[i].handle,
&me->channel[i].nb);
}
if (!IS_ERR_OR_NULL(me->non_secure_dev))
device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
MINOR_NUM_DEV));
if (!IS_ERR_OR_NULL(me->secure_dev))
device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
MINOR_NUM_SECURE_DEV));
class_destroy(me->class);
class_create_bail:
cdev_del(&me->cdev);
cdev_init_bail:
unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
alloc_chrdev_bail:
register_bail:
fastrpc_deinit();
return err;
}
static void __exit fastrpc_device_exit(void)
{
struct fastrpc_apps *me = &gfa;
int i;
fastrpc_file_list_dtor(me);
fastrpc_deinit();
wakeup_source_unregister(me->wake_source);
wakeup_source_unregister(me->wake_source_secure);
for (i = 0; i < NUM_CHANNELS; i++) {
if (!gcinfo[i].name)
continue;
subsys_notif_unregister_notifier(me->channel[i].handle,
&me->channel[i].nb);
}
/* Destroy the secure and non secure devices */
device_destroy(me->class, MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV));
device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
MINOR_NUM_SECURE_DEV));
of_reserved_mem_device_release(me->dev);
class_destroy(me->class);
cdev_del(&me->cdev);
unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
if (me->rpmsg_register == 1)
unregister_rpmsg_driver(&fastrpc_rpmsg_client);
kfree(me->gidlist.gids);
debugfs_remove_recursive(debugfs_root);
}
module_init(fastrpc_device_init);
module_exit(fastrpc_device_exit);
MODULE_LICENSE("GPL v2");
|