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
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
|
From 3e1e61f54538e8ce4bcbb5a9a213624eafcae514 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= <arve@android.com>
Date: Mon, 18 Nov 2013 20:46:48 -0800
Subject: [PATCH 18/32] ANDROID: trusty: Backport of trusty driver
This adds Trusty driver from android-trusty-5.10
Original commits:
b60d55f33484 ANDROID: trusty-ipc: Allow registering multiple handles
629a4d3318cc ANDROID: trusty: Support setting trusty_shared_mem_id_t
94a36a1374e7 ANDROID: trusty-log: Don't copy Trusty logs to linux kernel log
efc21cced8af ANDROID: trusty-log: rework buffer allocation
8cb1a07ca814 ANDROID: trusty-ipc: Fix lock protection of shared_handles
52cdd137fae0 ANDROID: trusty-log: support poll()
24c3649dceb9 ANDROID: trusty-irq: enqueue work in trusty_irq_cpu_up
05a05bdd921e ANDROID: trusty: Add config TRUSTY_CRASH_IS_PANIC
b5fbdba2ec72 ANDROID: trusty-ipc: Fix crash when running out of txbuffers
46da5b95605e ANDROID: trusty: Allow TRUSTY_LEND of buffers
2ebfb16645af ANDROID: trusty-virtio: remove unnecessary include of dma-mapping.h
bf9d994a65a2 ANDROID: trusty-log: Complement logging sink with unthrottled virtual file
d5cb51d0365d ANDROID: trusty-log: Refactor logging state to support concurrent sinks
b421a5ad3eb3 ANDROID: trusty-log: Sanitize u32 overflow of the log ring buffer write index
58e9681c57af ANDROID: trusty-log: On trusty panic, unthrottle sink to the kernel log
ba12be0f203a ANDROID: trusty-log: Update trusty log buffer size to hold a complete Trusty crash logs
a8a3f83e52b6 ANDROID: trusty_qemu_defconfig: Enable dma-buf and ion system heaps
988b52b392a1 ANDROID: trusty: Support setting FF-A Tag
f544e96489aa ANDROID: Add trusty_qemu_defconfig
8a9b09317f29 ANDROID: trusty-ipc: Switch from memfd to dma_buf
5460418ec9a4 ANDROID: trusty-irq: document new way of specifying IPIs
da3c30b943c2 ANDROID: trusty-irq: specify IPIs in new way
5b5bb7f74856 ANDROID: trusty: Add trusty-test driver
e80d87f422fd ANDROID: trusty: Add trusty-ipc driver
03c248cbf693 ANDROID: trusty: Add trusty-virtio driver
1047661edb97 ANDROID: trusty: Add trusty-log driver
18fd5c59b423 ANDROID: trusty: Add trusty-irq driver
479c39a683f8 ANDROID: trusty: Add trusty-core driver
Upstream-Status: Backport
Change-Id: I91f71b891a1091383a298e7fb2f9030382a19ca5
Signed-off-by: Arunachalam Ganapathy <arunachalam.ganapathy@arm.com>
---
.../devicetree/bindings/trusty/trusty-irq.txt | 67 +
.../devicetree/bindings/trusty/trusty-smc.txt | 6 +
arch/arm/configs/trusty_qemu_defconfig | 291 +++
.../configs/trusty_qemu_defconfig.fragment | 26 +
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/trusty/Kconfig | 116 +
drivers/trusty/Makefile | 14 +
drivers/trusty/trusty-ipc.c | 2256 +++++++++++++++++
drivers/trusty/trusty-irq.c | 645 +++++
drivers/trusty/trusty-log.c | 830 ++++++
drivers/trusty/trusty-log.h | 28 +
drivers/trusty/trusty-mem.c | 139 +
drivers/trusty/trusty-smc-arm.S | 41 +
drivers/trusty/trusty-smc-arm64.S | 35 +
drivers/trusty/trusty-smc.h | 26 +
drivers/trusty/trusty-test.c | 440 ++++
drivers/trusty/trusty-test.h | 13 +
drivers/trusty/trusty-virtio.c | 840 ++++++
drivers/trusty/trusty.c | 981 +++++++
include/linux/trusty/arm_ffa.h | 590 +++++
include/linux/trusty/sm_err.h | 28 +
include/linux/trusty/smcall.h | 124 +
include/linux/trusty/trusty.h | 131 +
include/linux/trusty/trusty_ipc.h | 89 +
include/uapi/linux/trusty/ipc.h | 65 +
include/uapi/linux/virtio_ids.h | 1 +
27 files changed, 7825 insertions(+)
create mode 100644 Documentation/devicetree/bindings/trusty/trusty-irq.txt
create mode 100644 Documentation/devicetree/bindings/trusty/trusty-smc.txt
create mode 100644 arch/arm/configs/trusty_qemu_defconfig
create mode 100644 arch/arm64/configs/trusty_qemu_defconfig.fragment
create mode 100644 drivers/trusty/Kconfig
create mode 100644 drivers/trusty/Makefile
create mode 100644 drivers/trusty/trusty-ipc.c
create mode 100644 drivers/trusty/trusty-irq.c
create mode 100644 drivers/trusty/trusty-log.c
create mode 100644 drivers/trusty/trusty-log.h
create mode 100644 drivers/trusty/trusty-mem.c
create mode 100644 drivers/trusty/trusty-smc-arm.S
create mode 100644 drivers/trusty/trusty-smc-arm64.S
create mode 100644 drivers/trusty/trusty-smc.h
create mode 100644 drivers/trusty/trusty-test.c
create mode 100644 drivers/trusty/trusty-test.h
create mode 100644 drivers/trusty/trusty-virtio.c
create mode 100644 drivers/trusty/trusty.c
create mode 100644 include/linux/trusty/arm_ffa.h
create mode 100644 include/linux/trusty/sm_err.h
create mode 100644 include/linux/trusty/smcall.h
create mode 100644 include/linux/trusty/trusty.h
create mode 100644 include/linux/trusty/trusty_ipc.h
create mode 100644 include/uapi/linux/trusty/ipc.h
diff --git a/Documentation/devicetree/bindings/trusty/trusty-irq.txt b/Documentation/devicetree/bindings/trusty/trusty-irq.txt
new file mode 100644
index 000000000000..cbb545ad452b
--- /dev/null
+++ b/Documentation/devicetree/bindings/trusty/trusty-irq.txt
@@ -0,0 +1,67 @@
+Trusty irq interface
+
+Trusty requires non-secure irqs to be forwarded to the secure OS.
+
+Required properties:
+- compatible: "android,trusty-irq-v1"
+
+Optional properties:
+
+- interrupt-templates: is an optional property that works together
+ with "interrupt-ranges" to specify secure side to kernel IRQs mapping.
+
+ It is a list of entries, each one of which defines a group of interrupts
+ having common properties, and has the following format:
+ < phandle irq_id_pos [templ_data]>
+ phandle - phandle of interrupt controller this template is for
+ irq_id_pos - the position of irq id in interrupt specifier array
+ for interrupt controller referenced by phandle.
+ templ_data - is an array of u32 values (could be empty) in the same
+ format as interrupt specifier for interrupt controller
+ referenced by phandle but with omitted irq id field.
+
+- interrupt-ranges: list of entries that specifies secure side to kernel
+ IRQs mapping.
+
+ Each entry in the "interrupt-ranges" list has the following format:
+ <beg end templ_idx>
+ beg - first entry in this range
+ end - last entry in this range
+ templ_idx - index of entry in "interrupt-templates" property
+ that must be used as a template for all interrupts
+ in this range
+
+- ipi-range: optional mapping of a linear range of trusty IRQs to a linear range
+ of IPIs (inter-processor interrupts). This has the following format:
+ <beg end ipi_base>
+ beg - first trusty IRQ number that is an IPI
+ end - last trusty IRQ number that is an IPI
+ ipi_base - IPI number of 'beg'
+
+Example:
+{
+ gic: interrupt-controller@50041000 {
+ compatible = "arm,gic-400";
+ #interrupt-cells = <3>;
+ interrupt-controller;
+ ...
+ };
+ ...
+ trusty {
+ compatible = "android,trusty-smc-v1";
+ ranges;
+ #address-cells = <2>;
+ #size-cells = <2>;
+
+ irq {
+ compatible = "android,trusty-irq-v1";
+ interrupt-templates = <&gic 1 GIC_PPI 0>,
+ <&gic 1 GIC_SPI 0>;
+ interrupt-ranges = <16 31 0>,
+ <32 223 1>;
+ ipi-range = <8 15 8>;
+ };
+ }
+}
+
+Must be a child of the node that provides the trusty std/fast call interface.
diff --git a/Documentation/devicetree/bindings/trusty/trusty-smc.txt b/Documentation/devicetree/bindings/trusty/trusty-smc.txt
new file mode 100644
index 000000000000..1b39ad317c67
--- /dev/null
+++ b/Documentation/devicetree/bindings/trusty/trusty-smc.txt
@@ -0,0 +1,6 @@
+Trusty smc interface
+
+Trusty is running in secure mode on the same (arm) cpu(s) as the current os.
+
+Required properties:
+- compatible: "android,trusty-smc-v1"
diff --git a/arch/arm/configs/trusty_qemu_defconfig b/arch/arm/configs/trusty_qemu_defconfig
new file mode 100644
index 000000000000..46ad9504c23d
--- /dev/null
+++ b/arch/arm/configs/trusty_qemu_defconfig
@@ -0,0 +1,291 @@
+# CONFIG_LOCALVERSION_AUTO is not set
+# CONFIG_SWAP is not set
+CONFIG_POSIX_MQUEUE=y
+CONFIG_AUDIT=y
+CONFIG_NO_HZ=y
+CONFIG_HIGH_RES_TIMERS=y
+CONFIG_PREEMPT=y
+CONFIG_BSD_PROCESS_ACCT=y
+CONFIG_BSD_PROCESS_ACCT_V3=y
+CONFIG_TASKSTATS=y
+CONFIG_TASK_DELAY_ACCT=y
+CONFIG_TASK_XACCT=y
+CONFIG_TASK_IO_ACCOUNTING=y
+CONFIG_IKCONFIG=y
+CONFIG_IKCONFIG_PROC=y
+CONFIG_LOG_BUF_SHIFT=14
+CONFIG_RT_GROUP_SCHED=y
+CONFIG_CGROUP_FREEZER=y
+CONFIG_CGROUP_CPUACCT=y
+CONFIG_CGROUP_DEBUG=y
+CONFIG_SCHED_AUTOGROUP=y
+CONFIG_BLK_DEV_INITRD=y
+CONFIG_KALLSYMS_ALL=y
+CONFIG_EMBEDDED=y
+# CONFIG_COMPAT_BRK is not set
+CONFIG_PROFILING=y
+CONFIG_ARCH_VIRT=y
+CONFIG_PCI=y
+CONFIG_PCI_HOST_GENERIC=y
+CONFIG_SMP=y
+CONFIG_HIGHMEM=y
+CONFIG_SECCOMP=y
+CONFIG_CMDLINE="console=ttyAMA0"
+CONFIG_PM_AUTOSLEEP=y
+CONFIG_PM_WAKELOCKS=y
+CONFIG_PM_WAKELOCKS_LIMIT=0
+# CONFIG_PM_WAKELOCKS_GC is not set
+CONFIG_PM_DEBUG=y
+# CONFIG_BLK_DEV_BSG is not set
+# CONFIG_IOSCHED_DEADLINE is not set
+# CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS is not set
+CONFIG_KSM=y
+CONFIG_NET=y
+CONFIG_PACKET=y
+CONFIG_UNIX=y
+CONFIG_XFRM_USER=y
+CONFIG_NET_KEY=y
+CONFIG_INET=y
+CONFIG_IP_MULTICAST=y
+CONFIG_IP_ADVANCED_ROUTER=y
+CONFIG_IP_MULTIPLE_TABLES=y
+CONFIG_IP_PNP=y
+CONFIG_IP_PNP_DHCP=y
+CONFIG_IP_PNP_BOOTP=y
+CONFIG_INET_ESP=y
+CONFIG_INET_DIAG_DESTROY=y
+CONFIG_IPV6_ROUTER_PREF=y
+CONFIG_IPV6_ROUTE_INFO=y
+CONFIG_IPV6_OPTIMISTIC_DAD=y
+CONFIG_INET6_AH=y
+CONFIG_INET6_ESP=y
+CONFIG_INET6_IPCOMP=y
+CONFIG_IPV6_MIP6=y
+CONFIG_IPV6_MULTIPLE_TABLES=y
+CONFIG_NETFILTER=y
+CONFIG_NF_CONNTRACK=y
+CONFIG_NF_CONNTRACK_SECMARK=y
+CONFIG_NF_CONNTRACK_EVENTS=y
+CONFIG_NF_CONNTRACK_AMANDA=y
+CONFIG_NF_CONNTRACK_FTP=y
+CONFIG_NF_CONNTRACK_H323=y
+CONFIG_NF_CONNTRACK_IRC=y
+CONFIG_NF_CONNTRACK_NETBIOS_NS=y
+CONFIG_NF_CONNTRACK_PPTP=y
+CONFIG_NF_CONNTRACK_SANE=y
+CONFIG_NF_CONNTRACK_TFTP=y
+CONFIG_NF_CT_NETLINK=y
+CONFIG_NETFILTER_XT_TARGET_CLASSIFY=y
+CONFIG_NETFILTER_XT_TARGET_CONNMARK=y
+CONFIG_NETFILTER_XT_TARGET_CONNSECMARK=y
+CONFIG_NETFILTER_XT_TARGET_IDLETIMER=y
+CONFIG_NETFILTER_XT_TARGET_MARK=y
+CONFIG_NETFILTER_XT_TARGET_NFLOG=y
+CONFIG_NETFILTER_XT_TARGET_NFQUEUE=y
+CONFIG_NETFILTER_XT_TARGET_TPROXY=y
+CONFIG_NETFILTER_XT_TARGET_TRACE=y
+CONFIG_NETFILTER_XT_TARGET_SECMARK=y
+CONFIG_NETFILTER_XT_TARGET_TCPMSS=y
+CONFIG_NETFILTER_XT_MATCH_COMMENT=y
+CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=y
+CONFIG_NETFILTER_XT_MATCH_CONNMARK=y
+CONFIG_NETFILTER_XT_MATCH_CONNTRACK=y
+CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=y
+CONFIG_NETFILTER_XT_MATCH_HELPER=y
+CONFIG_NETFILTER_XT_MATCH_IPRANGE=y
+CONFIG_NETFILTER_XT_MATCH_LENGTH=y
+CONFIG_NETFILTER_XT_MATCH_LIMIT=y
+CONFIG_NETFILTER_XT_MATCH_MAC=y
+CONFIG_NETFILTER_XT_MATCH_MARK=y
+CONFIG_NETFILTER_XT_MATCH_POLICY=y
+CONFIG_NETFILTER_XT_MATCH_PKTTYPE=y
+CONFIG_NETFILTER_XT_MATCH_QUOTA=y
+CONFIG_NETFILTER_XT_MATCH_QUOTA2=y
+CONFIG_NETFILTER_XT_MATCH_SOCKET=y
+CONFIG_NETFILTER_XT_MATCH_STATE=y
+CONFIG_NETFILTER_XT_MATCH_STATISTIC=y
+CONFIG_NETFILTER_XT_MATCH_STRING=y
+CONFIG_NETFILTER_XT_MATCH_TIME=y
+CONFIG_NETFILTER_XT_MATCH_U32=y
+CONFIG_IP_NF_IPTABLES=y
+CONFIG_IP_NF_MATCH_AH=y
+CONFIG_IP_NF_MATCH_ECN=y
+CONFIG_IP_NF_MATCH_RPFILTER=y
+CONFIG_IP_NF_MATCH_TTL=y
+CONFIG_IP_NF_FILTER=y
+CONFIG_IP_NF_TARGET_REJECT=y
+CONFIG_IP_NF_MANGLE=y
+CONFIG_IP_NF_TARGET_ECN=y
+CONFIG_IP_NF_TARGET_TTL=y
+CONFIG_IP_NF_RAW=y
+CONFIG_IP_NF_SECURITY=y
+CONFIG_IP_NF_ARPTABLES=y
+CONFIG_IP_NF_ARPFILTER=y
+CONFIG_IP_NF_ARP_MANGLE=y
+CONFIG_IP6_NF_IPTABLES=y
+CONFIG_IP6_NF_MATCH_AH=y
+CONFIG_IP6_NF_MATCH_EUI64=y
+CONFIG_IP6_NF_MATCH_FRAG=y
+CONFIG_IP6_NF_MATCH_OPTS=y
+CONFIG_IP6_NF_MATCH_HL=y
+CONFIG_IP6_NF_MATCH_IPV6HEADER=y
+CONFIG_IP6_NF_MATCH_MH=y
+CONFIG_IP6_NF_MATCH_RT=y
+CONFIG_IP6_NF_TARGET_HL=y
+CONFIG_IP6_NF_FILTER=y
+CONFIG_IP6_NF_TARGET_REJECT=y
+CONFIG_IP6_NF_MANGLE=y
+CONFIG_IP6_NF_RAW=y
+CONFIG_BRIDGE=y
+CONFIG_NET_SCHED=y
+CONFIG_NET_SCH_HTB=y
+CONFIG_NET_CLS_U32=y
+CONFIG_NET_EMATCH=y
+CONFIG_NET_EMATCH_U32=y
+CONFIG_NET_CLS_ACT=y
+# CONFIG_WIRELESS is not set
+CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug"
+CONFIG_BLK_DEV_LOOP=y
+CONFIG_BLK_DEV_RAM=y
+CONFIG_BLK_DEV_RAM_SIZE=8192
+CONFIG_VIRTIO_BLK=y
+CONFIG_SCSI=y
+# CONFIG_SCSI_PROC_FS is not set
+CONFIG_BLK_DEV_SD=y
+# CONFIG_SCSI_LOWLEVEL is not set
+CONFIG_MD=y
+CONFIG_BLK_DEV_DM=y
+CONFIG_DM_CRYPT=y
+CONFIG_DM_UEVENT=y
+CONFIG_DM_VERITY=y
+CONFIG_DM_VERITY_FEC=y
+CONFIG_NETDEVICES=y
+CONFIG_TUN=y
+CONFIG_VIRTIO_NET=y
+CONFIG_E1000=y
+CONFIG_E1000E=y
+CONFIG_PPP=y
+CONFIG_PPP_BSDCOMP=y
+CONFIG_PPP_DEFLATE=y
+CONFIG_PPP_MPPE=y
+# CONFIG_WLAN is not set
+CONFIG_INPUT_EVDEV=y
+CONFIG_KEYBOARD_GOLDFISH_EVENTS=y
+# CONFIG_INPUT_MOUSE is not set
+CONFIG_INPUT_JOYSTICK=y
+CONFIG_INPUT_TABLET=y
+CONFIG_INPUT_MISC=y
+CONFIG_INPUT_UINPUT=y
+# CONFIG_SERIO_SERPORT is not set
+# CONFIG_VT is not set
+# CONFIG_LEGACY_PTYS is not set
+# CONFIG_DEVMEM is not set
+CONFIG_SERIAL_AMBA_PL011=y
+CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
+CONFIG_VIRTIO_CONSOLE=y
+# CONFIG_HW_RANDOM is not set
+CONFIG_BATTERY_GOLDFISH=y
+# CONFIG_HWMON is not set
+CONFIG_TRUSTY=y
+CONFIG_MEDIA_SUPPORT=y
+CONFIG_FB=y
+CONFIG_FB_GOLDFISH=y
+CONFIG_FB_SIMPLE=y
+CONFIG_BACKLIGHT_LCD_SUPPORT=y
+CONFIG_LOGO=y
+# CONFIG_LOGO_LINUX_MONO is not set
+# CONFIG_LOGO_LINUX_VGA16 is not set
+CONFIG_SOUND=y
+CONFIG_SND=y
+CONFIG_HIDRAW=y
+CONFIG_UHID=y
+CONFIG_HID_A4TECH=y
+CONFIG_HID_ACRUX=y
+CONFIG_HID_ACRUX_FF=y
+CONFIG_HID_APPLE=y
+CONFIG_HID_BELKIN=y
+CONFIG_HID_CHERRY=y
+CONFIG_HID_CHICONY=y
+CONFIG_HID_PRODIKEYS=y
+CONFIG_HID_CYPRESS=y
+CONFIG_HID_DRAGONRISE=y
+CONFIG_DRAGONRISE_FF=y
+CONFIG_HID_EMS_FF=y
+CONFIG_HID_ELECOM=y
+CONFIG_HID_EZKEY=y
+CONFIG_HID_KEYTOUCH=y
+CONFIG_HID_KYE=y
+CONFIG_HID_WALTOP=y
+CONFIG_HID_GYRATION=y
+CONFIG_HID_TWINHAN=y
+CONFIG_HID_KENSINGTON=y
+CONFIG_HID_LCPOWER=y
+CONFIG_HID_LOGITECH=y
+CONFIG_HID_LOGITECH_DJ=y
+CONFIG_LOGITECH_FF=y
+CONFIG_LOGIRUMBLEPAD2_FF=y
+CONFIG_LOGIG940_FF=y
+CONFIG_HID_MAGICMOUSE=y
+CONFIG_HID_MICROSOFT=y
+CONFIG_HID_MONTEREY=y
+CONFIG_HID_MULTITOUCH=y
+CONFIG_HID_ORTEK=y
+CONFIG_HID_PANTHERLORD=y
+CONFIG_PANTHERLORD_FF=y
+CONFIG_HID_PETALYNX=y
+CONFIG_HID_PICOLCD=y
+CONFIG_HID_PRIMAX=y
+CONFIG_HID_SAITEK=y
+CONFIG_HID_SAMSUNG=y
+CONFIG_HID_SPEEDLINK=y
+CONFIG_HID_SUNPLUS=y
+CONFIG_HID_GREENASIA=y
+CONFIG_GREENASIA_FF=y
+CONFIG_HID_SMARTJOYPLUS=y
+CONFIG_SMARTJOYPLUS_FF=y
+CONFIG_HID_TIVO=y
+CONFIG_HID_TOPSEED=y
+CONFIG_HID_THRUSTMASTER=y
+CONFIG_HID_ZEROPLUS=y
+CONFIG_HID_ZYDACRON=y
+# CONFIG_USB_SUPPORT is not set
+CONFIG_RTC_CLASS=y
+CONFIG_VIRTIO_PCI=y
+CONFIG_VIRTIO_MMIO=y
+CONFIG_STAGING=y
+CONFIG_ASHMEM=y
+CONFIG_ION=y
+CONFIG_GOLDFISH_AUDIO=y
+CONFIG_GOLDFISH=y
+CONFIG_GOLDFISH_PIPE=y
+# CONFIG_IOMMU_SUPPORT is not set
+CONFIG_ANDROID=y
+CONFIG_ANDROID_BINDER_IPC=y
+CONFIG_EXT2_FS=y
+CONFIG_EXT4_FS=y
+CONFIG_EXT4_FS_SECURITY=y
+CONFIG_QUOTA=y
+CONFIG_FUSE_FS=y
+CONFIG_CUSE=y
+CONFIG_MSDOS_FS=y
+CONFIG_VFAT_FS=y
+CONFIG_TMPFS=y
+CONFIG_TMPFS_POSIX_ACL=y
+# CONFIG_MISC_FILESYSTEMS is not set
+CONFIG_NFS_FS=y
+CONFIG_ROOT_NFS=y
+CONFIG_NLS_CODEPAGE_437=y
+CONFIG_NLS_ISO8859_1=y
+CONFIG_SECURITY=y
+CONFIG_SECURITY_NETWORK=y
+CONFIG_SECURITY_SELINUX=y
+CONFIG_DYNAMIC_DEBUG=y
+CONFIG_DEBUG_INFO=y
+CONFIG_DEBUG_FS=y
+CONFIG_MAGIC_SYSRQ=y
+CONFIG_PANIC_TIMEOUT=5
+# CONFIG_SCHED_DEBUG is not set
+CONFIG_SCHEDSTATS=y
+# CONFIG_FTRACE is not set
+CONFIG_DMA_API_DEBUG=y
+CONFIG_ATOMIC64_SELFTEST=y
diff --git a/arch/arm64/configs/trusty_qemu_defconfig.fragment b/arch/arm64/configs/trusty_qemu_defconfig.fragment
new file mode 100644
index 000000000000..166eef1797fd
--- /dev/null
+++ b/arch/arm64/configs/trusty_qemu_defconfig.fragment
@@ -0,0 +1,26 @@
+# From goldfish
+CONFIG_VIRTIO_BLK=y
+CONFIG_VIRTIO_CONSOLE=y
+CONFIG_VIRTIO_INPUT=y
+CONFIG_VIRTIO_MMIO=y
+CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES=y
+CONFIG_VIRTIO_NET=y
+CONFIG_VIRTIO_PCI=y
+CONFIG_VIRTIO_PMEM=y
+# From Trusty
+CONFIG_TRUSTY=y
+CONFIG_DMA_API_DEBUG=y
+CONFIG_DYNAMIC_DEBUG=y
+CONFIG_PROVE_LOCKING=y
+CONFIG_DEBUG_ATOMIC_SLEEP=y
+CONFIG_SEMIHOSTING_EXIT=y
+CONFIG_E1000=y
+CONFIG_E1000E=y
+CONFIG_REBOOT_EMULATOR_EXIT=y
+CONFIG_DMABUF_HEAPS_SYSTEM=y
+# securefb test uses ION
+CONFIG_ION=y
+CONFIG_ION_SYSTEM_HEAP=y
+# LTO slows down build times considerably. Disable it.
+# CONFIG_LTO_CLANG is not set
+# CONFIG_LTO_CLANG_FULL is not set
diff --git a/drivers/Kconfig b/drivers/Kconfig
index dcecc9f6e33f..2e9abcc98126 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -86,6 +86,8 @@ source "drivers/hwmon/Kconfig"
source "drivers/thermal/Kconfig"
+source "drivers/trusty/Kconfig"
+
source "drivers/watchdog/Kconfig"
source "drivers/ssb/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index 576228037718..7d15799dbe77 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -118,6 +118,7 @@ obj-$(CONFIG_W1) += w1/
obj-y += power/
obj-$(CONFIG_HWMON) += hwmon/
obj-$(CONFIG_THERMAL) += thermal/
+obj-$(CONFIG_TRUSTY) += trusty/
obj-$(CONFIG_WATCHDOG) += watchdog/
obj-$(CONFIG_MD) += md/
obj-$(CONFIG_BT) += bluetooth/
diff --git a/drivers/trusty/Kconfig b/drivers/trusty/Kconfig
new file mode 100644
index 000000000000..fcde7f097acf
--- /dev/null
+++ b/drivers/trusty/Kconfig
@@ -0,0 +1,116 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Trusty driver
+#
+
+menu "Trusty driver"
+
+config TRUSTY
+ tristate "Trusty core driver"
+ depends on ARM || ARM64
+ help
+ Trusty is a secure OS that provides a Trusted Execution Environment
+ (TEE) for Android. Trusty runs on the same processor as Linux but is
+ isolated from the rest of the system by both hardware and software.
+
+ This option enables the core part of the Linux kernel driver for
+ Trusty. This doesn't do much by itself; you'll need to enable some of
+ the sub-modules too.
+
+ If you build this as a module, it will be called trusty-core.
+
+if TRUSTY
+
+config TRUSTY_IRQ
+ tristate "Trusty IRQ support"
+ default y
+ help
+ Enable forwarding of IRQs from Linux to Trusty. This module retrieves
+ from Trusty a list of IRQs that Trusty uses, and it registers handlers
+ for them which notify Trusty that the IRQ has been received.
+
+ If you build this as a module, it will be called trusty-irq.
+
+ Usually this is needed for Trusty to work, so say 'y' or 'm'.
+
+config TRUSTY_LOG
+ tristate "Trusty log support"
+ default y
+ help
+ Print log messages generated by the secure OS to the Linux kernel log.
+
+ While this module is loaded, messages are retrieved and printed after
+ each call into Trusty, and also during Linux kernel panics.
+
+ If you build this as a module, it will be called trusty-log.
+
+config TRUSTY_TEST
+ tristate "Trusty stdcall test"
+ default y
+ help
+ Allow running tests of the Trusty stdcall interface. Running these
+ tests is initiated by userspace writing to a sysfs file.
+
+ This depends on having a test sevice running on the Trusty side.
+
+ If you build this as a module, it will be called trusty-test.
+
+config TRUSTY_VIRTIO
+ tristate "Trusty virtio support"
+ select VIRTIO
+ default y
+ help
+ Enable the Trusty virtio driver, which is responsible for management
+ and interaction with virtio devices exposed by Trusty. This driver
+ requests the virtio device descriptors from Trusty, then parses them
+ and adds the corresponding virtio devices.
+
+ If you build this as a module, it will be called trusty-virtio.
+
+config TRUSTY_VIRTIO_IPC
+ tristate "Trusty Virtio IPC driver"
+ depends on TRUSTY_VIRTIO
+ default y
+ help
+ Enable support for communicating with Trusty services.
+
+ If you build this as a module, it will be called trusty-ipc.
+
+config TRUSTY_DMA_BUF_FFA_TAG
+ bool "Availability of trusty_dma_buf_get_ffa_tag"
+ default n
+ help
+ Whether trusty_dma_buf_get_ffa_tag is provided on this platform.
+ Providing this function will allow the platform to select what tag
+ should be passed to the SPM when attempting to transfer the buffer
+ to secure world. The value passed here is implementation defined and
+ may depend on your SPM.
+
+ If set to N, a default implementation which returns 0 will be used.
+
+config TRUSTY_DMA_BUF_SHARED_MEM_ID
+ bool "Availability of trusty_dma_buf_get_shared_mem_id"
+ default n
+ help
+ Whether trusty_dma_buf_get_shared_mem_id is provided on this platform.
+ Providing this function allows the platform to manage memory
+ transaction life cycle of DMA bufs independently of Trusty IPC driver.
+ The latter can query trusty_shared_mem_id_t value allocated for a
+ given DMA buf using trusty_dma_buf_get_shared_mem_id interface.
+
+ If set to N, a default implementation which does not allocate any IDs
+ will be used.
+
+config TRUSTY_CRASH_IS_PANIC
+ bool "When trusty panics, then panic the kernel"
+ help
+ This option will treat Trusty panics as fatal. This is useful if
+ your system cannot recover from Trusty panic/halt and you require
+ the system to reboot to recover.
+
+ If N, it will contine to run the kernel, but trusty operations will
+ return errors.
+
+endif # TRUSTY
+
+endmenu
diff --git a/drivers/trusty/Makefile b/drivers/trusty/Makefile
new file mode 100644
index 000000000000..2cf1cfccf97b
--- /dev/null
+++ b/drivers/trusty/Makefile
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Makefile for trusty components
+#
+
+obj-$(CONFIG_TRUSTY) += trusty-core.o
+trusty-core-objs += trusty.o trusty-mem.o
+trusty-core-$(CONFIG_ARM) += trusty-smc-arm.o
+trusty-core-$(CONFIG_ARM64) += trusty-smc-arm64.o
+obj-$(CONFIG_TRUSTY_IRQ) += trusty-irq.o
+obj-$(CONFIG_TRUSTY_LOG) += trusty-log.o
+obj-$(CONFIG_TRUSTY_TEST) += trusty-test.o
+obj-$(CONFIG_TRUSTY_VIRTIO) += trusty-virtio.o
+obj-$(CONFIG_TRUSTY_VIRTIO_IPC) += trusty-ipc.o
diff --git a/drivers/trusty/trusty-ipc.c b/drivers/trusty/trusty-ipc.c
new file mode 100644
index 000000000000..82d6ddeb41f4
--- /dev/null
+++ b/drivers/trusty/trusty-ipc.c
@@ -0,0 +1,2256 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Google, Inc.
+ */
+
+#include <linux/aio.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/cdev.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/poll.h>
+#include <linux/idr.h>
+#include <linux/completion.h>
+#include <linux/dma-buf.h>
+#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/compat.h>
+#include <linux/uio.h>
+#include <linux/file.h>
+
+#include <linux/virtio.h>
+#include <linux/virtio_ids.h>
+#include <linux/virtio_config.h>
+
+#include <linux/trusty/trusty.h>
+#include <linux/trusty/trusty_ipc.h>
+
+#include <uapi/linux/trusty/ipc.h>
+
+#define MAX_DEVICES 4
+
+#define REPLY_TIMEOUT 5000
+#define TXBUF_TIMEOUT 15000
+
+#define MAX_SRV_NAME_LEN 256
+#define MAX_DEV_NAME_LEN 32
+
+#define DEFAULT_MSG_BUF_SIZE PAGE_SIZE
+#define DEFAULT_MSG_BUF_ALIGN PAGE_SIZE
+
+#define TIPC_CTRL_ADDR 53
+#define TIPC_ANY_ADDR 0xFFFFFFFF
+
+#define TIPC_MIN_LOCAL_ADDR 1024
+
+#ifdef CONFIG_COMPAT
+#define TIPC_IOC32_CONNECT _IOW(TIPC_IOC_MAGIC, 0x80, compat_uptr_t)
+#endif
+
+struct tipc_virtio_dev;
+
+struct tipc_dev_config {
+ u32 msg_buf_max_size;
+ u32 msg_buf_alignment;
+ char dev_name[MAX_DEV_NAME_LEN];
+} __packed;
+
+struct tipc_shm {
+ trusty_shared_mem_id_t obj_id;
+ u64 size;
+ u64 tag;
+};
+
+struct tipc_msg_hdr {
+ u32 src;
+ u32 dst;
+ u16 reserved;
+ u16 shm_cnt;
+ u16 len;
+ u16 flags;
+ u8 data[];
+} __packed;
+
+enum tipc_ctrl_msg_types {
+ TIPC_CTRL_MSGTYPE_GO_ONLINE = 1,
+ TIPC_CTRL_MSGTYPE_GO_OFFLINE,
+ TIPC_CTRL_MSGTYPE_CONN_REQ,
+ TIPC_CTRL_MSGTYPE_CONN_RSP,
+ TIPC_CTRL_MSGTYPE_DISC_REQ,
+ TIPC_CTRL_MSGTYPE_RELEASE,
+};
+
+struct tipc_ctrl_msg {
+ u32 type;
+ u32 body_len;
+ u8 body[];
+} __packed;
+
+struct tipc_conn_req_body {
+ char name[MAX_SRV_NAME_LEN];
+} __packed;
+
+struct tipc_conn_rsp_body {
+ u32 target;
+ u32 status;
+ u32 remote;
+ u32 max_msg_size;
+ u32 max_msg_cnt;
+} __packed;
+
+struct tipc_disc_req_body {
+ u32 target;
+} __packed;
+
+struct tipc_release_body {
+ trusty_shared_mem_id_t id;
+} __packed;
+
+struct tipc_cdev_node {
+ struct cdev cdev;
+ struct device *dev;
+ unsigned int minor;
+};
+
+enum tipc_device_state {
+ VDS_OFFLINE = 0,
+ VDS_ONLINE,
+ VDS_DEAD,
+};
+
+struct tipc_virtio_dev {
+ struct kref refcount;
+ struct mutex lock; /* protects access to this device */
+ struct virtio_device *vdev;
+ struct virtqueue *rxvq;
+ struct virtqueue *txvq;
+ unsigned int msg_buf_cnt;
+ unsigned int msg_buf_max_cnt;
+ size_t msg_buf_max_sz;
+ unsigned int free_msg_buf_cnt;
+ struct list_head free_buf_list;
+ wait_queue_head_t sendq;
+ struct idr addr_idr;
+ enum tipc_device_state state;
+ struct tipc_cdev_node cdev_node;
+ /* protects shared_handles, dev lock never acquired while held */
+ struct mutex shared_handles_lock;
+ struct rb_root shared_handles;
+ char cdev_name[MAX_DEV_NAME_LEN];
+};
+
+enum tipc_chan_state {
+ TIPC_DISCONNECTED = 0,
+ TIPC_CONNECTING,
+ TIPC_CONNECTED,
+ TIPC_STALE,
+};
+
+struct tipc_chan {
+ struct mutex lock; /* protects channel state */
+ struct kref refcount;
+ enum tipc_chan_state state;
+ struct tipc_virtio_dev *vds;
+ const struct tipc_chan_ops *ops;
+ void *ops_arg;
+ u32 remote;
+ u32 local;
+ u32 max_msg_size;
+ u32 max_msg_cnt;
+ char srv_name[MAX_SRV_NAME_LEN];
+};
+
+struct tipc_shared_handle {
+ struct rb_node node;
+ struct tipc_shm tipc;
+ struct tipc_virtio_dev *vds;
+ struct dma_buf *dma_buf;
+ bool shared;
+ /*
+ * Following fields are only used if dma_buf does not own a
+ * trusty_shared_mem_id_t.
+ */
+ struct dma_buf_attachment *attach;
+ struct sg_table *sgt;
+};
+
+static struct class *tipc_class;
+static unsigned int tipc_major;
+
+static struct virtio_device *default_vdev;
+
+static DEFINE_IDR(tipc_devices);
+static DEFINE_MUTEX(tipc_devices_lock);
+
+static int _match_any(int id, void *p, void *data)
+{
+ return id;
+}
+
+static int _match_data(int id, void *p, void *data)
+{
+ return (p == data);
+}
+
+static void *_alloc_shareable_mem(size_t sz, gfp_t gfp)
+{
+ return alloc_pages_exact(sz, gfp);
+}
+
+static void _free_shareable_mem(size_t sz, void *va)
+{
+ free_pages_exact(va, sz);
+}
+
+static struct tipc_msg_buf *vds_alloc_msg_buf(struct tipc_virtio_dev *vds,
+ bool share_write)
+{
+ int ret;
+ struct tipc_msg_buf *mb;
+ size_t sz = vds->msg_buf_max_sz;
+ pgprot_t pgprot = share_write ? PAGE_KERNEL : PAGE_KERNEL_RO;
+
+ /* allocate tracking structure */
+ mb = kzalloc(sizeof(struct tipc_msg_buf), GFP_KERNEL);
+ if (!mb)
+ return NULL;
+
+ /* allocate buffer that can be shared with secure world */
+ mb->buf_va = _alloc_shareable_mem(sz, GFP_KERNEL);
+ if (!mb->buf_va)
+ goto err_alloc;
+
+ sg_init_one(&mb->sg, mb->buf_va, sz);
+ ret = trusty_share_memory_compat(vds->vdev->dev.parent->parent,
+ &mb->buf_id, &mb->sg, 1, pgprot);
+ if (ret) {
+ dev_err(&vds->vdev->dev, "trusty_share_memory failed: %d\n",
+ ret);
+ goto err_share;
+ }
+
+ mb->buf_sz = sz;
+ mb->shm_cnt = 0;
+
+ return mb;
+
+err_share:
+ _free_shareable_mem(sz, mb->buf_va);
+err_alloc:
+ kfree(mb);
+ return NULL;
+}
+
+static void vds_free_msg_buf(struct tipc_virtio_dev *vds,
+ struct tipc_msg_buf *mb)
+{
+ int ret;
+
+ ret = trusty_reclaim_memory(vds->vdev->dev.parent->parent, mb->buf_id,
+ &mb->sg, 1);
+ if (WARN_ON(ret)) {
+ dev_err(&vds->vdev->dev,
+ "trusty_revoke_memory failed: %d txbuf %lld\n",
+ ret, mb->buf_id);
+
+ /*
+ * It is not safe to free this memory if trusty_revoke_memory
+ * fails. Leak it in that case.
+ */
+ } else {
+ _free_shareable_mem(mb->buf_sz, mb->buf_va);
+ }
+ kfree(mb);
+}
+
+static void vds_free_msg_buf_list(struct tipc_virtio_dev *vds,
+ struct list_head *list)
+{
+ struct tipc_msg_buf *mb = NULL;
+
+ mb = list_first_entry_or_null(list, struct tipc_msg_buf, node);
+ while (mb) {
+ list_del(&mb->node);
+ vds_free_msg_buf(vds, mb);
+ mb = list_first_entry_or_null(list, struct tipc_msg_buf, node);
+ }
+}
+
+static inline void mb_reset(struct tipc_msg_buf *mb)
+{
+ mb->wpos = 0;
+ mb->rpos = 0;
+}
+
+static inline void mb_reset_read(struct tipc_msg_buf *mb)
+{
+ mb->rpos = 0;
+}
+
+static void _free_vds(struct kref *kref)
+{
+ struct tipc_virtio_dev *vds =
+ container_of(kref, struct tipc_virtio_dev, refcount);
+ /*
+ * If this WARN triggers, we're leaking remote memory references.
+ *
+ * No need to lock shared_handles_lock. All references to this lock
+ * should already be gone by this point, since we are freeing it in this
+ * function.
+ */
+ WARN_ON(!RB_EMPTY_ROOT(&vds->shared_handles));
+ kfree(vds);
+}
+
+static void _free_chan(struct kref *kref)
+{
+ struct tipc_chan *ch = container_of(kref, struct tipc_chan, refcount);
+
+ if (ch->ops && ch->ops->handle_release)
+ ch->ops->handle_release(ch->ops_arg);
+
+ kref_put(&ch->vds->refcount, _free_vds);
+ kfree(ch);
+}
+
+static bool _put_txbuf_locked(struct tipc_virtio_dev *vds,
+ struct tipc_msg_buf *mb)
+{
+ list_add_tail(&mb->node, &vds->free_buf_list);
+ return vds->free_msg_buf_cnt++ == 0;
+}
+
+static struct tipc_msg_buf *_get_txbuf_locked(struct tipc_virtio_dev *vds)
+{
+ struct tipc_msg_buf *mb;
+
+ if (vds->state != VDS_ONLINE)
+ return ERR_PTR(-ENODEV);
+
+ if (vds->free_msg_buf_cnt) {
+ /* take it out of free list */
+ mb = list_first_entry(&vds->free_buf_list,
+ struct tipc_msg_buf, node);
+ list_del(&mb->node);
+ mb->shm_cnt = 0;
+ vds->free_msg_buf_cnt--;
+ } else {
+ if (vds->msg_buf_cnt >= vds->msg_buf_max_cnt)
+ return ERR_PTR(-EAGAIN);
+
+ /* try to allocate it */
+ mb = vds_alloc_msg_buf(vds, false);
+ if (!mb)
+ return ERR_PTR(-ENOMEM);
+
+ vds->msg_buf_cnt++;
+ }
+ return mb;
+}
+
+static struct tipc_msg_buf *_vds_get_txbuf(struct tipc_virtio_dev *vds)
+{
+ struct tipc_msg_buf *mb;
+
+ mutex_lock(&vds->lock);
+ mb = _get_txbuf_locked(vds);
+ mutex_unlock(&vds->lock);
+
+ return mb;
+}
+
+static void vds_put_txbuf(struct tipc_virtio_dev *vds, struct tipc_msg_buf *mb)
+{
+ mutex_lock(&vds->lock);
+ _put_txbuf_locked(vds, mb);
+ wake_up_interruptible(&vds->sendq);
+ mutex_unlock(&vds->lock);
+}
+
+static struct tipc_msg_buf *vds_get_txbuf(struct tipc_virtio_dev *vds,
+ long timeout)
+{
+ struct tipc_msg_buf *mb;
+
+ mb = _vds_get_txbuf(vds);
+
+ if ((PTR_ERR(mb) == -EAGAIN) && timeout) {
+ DEFINE_WAIT_FUNC(wait, woken_wake_function);
+
+ timeout = msecs_to_jiffies(timeout);
+ add_wait_queue(&vds->sendq, &wait);
+ for (;;) {
+ timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
+ timeout);
+ if (!timeout) {
+ mb = ERR_PTR(-ETIMEDOUT);
+ break;
+ }
+
+ if (signal_pending(current)) {
+ mb = ERR_PTR(-ERESTARTSYS);
+ break;
+ }
+
+ mb = _vds_get_txbuf(vds);
+ if (PTR_ERR(mb) != -EAGAIN)
+ break;
+ }
+ remove_wait_queue(&vds->sendq, &wait);
+ }
+
+ if (IS_ERR(mb))
+ return mb;
+
+ if (WARN_ON(!mb))
+ return ERR_PTR(-EINVAL);
+
+ /* reset and reserve space for message header */
+ mb_reset(mb);
+ mb_put_data(mb, sizeof(struct tipc_msg_hdr));
+
+ return mb;
+}
+
+static int vds_queue_txbuf(struct tipc_virtio_dev *vds,
+ struct tipc_msg_buf *mb)
+{
+ int err;
+ struct scatterlist sg;
+ bool need_notify = false;
+
+ mutex_lock(&vds->lock);
+ if (vds->state == VDS_ONLINE) {
+ sg_init_one(&sg, mb, mb->wpos);
+ err = virtqueue_add_outbuf(vds->txvq, &sg, 1, mb, GFP_KERNEL);
+ need_notify = virtqueue_kick_prepare(vds->txvq);
+ } else {
+ err = -ENODEV;
+ }
+ mutex_unlock(&vds->lock);
+
+ if (need_notify)
+ virtqueue_notify(vds->txvq);
+
+ return err;
+}
+
+static int vds_add_channel(struct tipc_virtio_dev *vds,
+ struct tipc_chan *chan)
+{
+ int ret;
+
+ mutex_lock(&vds->lock);
+ if (vds->state == VDS_ONLINE) {
+ ret = idr_alloc(&vds->addr_idr, chan,
+ TIPC_MIN_LOCAL_ADDR, TIPC_ANY_ADDR - 1,
+ GFP_KERNEL);
+ if (ret > 0) {
+ chan->local = ret;
+ kref_get(&chan->refcount);
+ ret = 0;
+ }
+ } else {
+ ret = -EINVAL;
+ }
+ mutex_unlock(&vds->lock);
+
+ return ret;
+}
+
+static void vds_del_channel(struct tipc_virtio_dev *vds,
+ struct tipc_chan *chan)
+{
+ mutex_lock(&vds->lock);
+ if (chan->local) {
+ idr_remove(&vds->addr_idr, chan->local);
+ chan->local = 0;
+ chan->remote = 0;
+ kref_put(&chan->refcount, _free_chan);
+ }
+ mutex_unlock(&vds->lock);
+}
+
+static struct tipc_chan *vds_lookup_channel(struct tipc_virtio_dev *vds,
+ u32 addr)
+{
+ int id;
+ struct tipc_chan *chan = NULL;
+
+ mutex_lock(&vds->lock);
+ if (addr == TIPC_ANY_ADDR) {
+ id = idr_for_each(&vds->addr_idr, _match_any, NULL);
+ if (id > 0)
+ chan = idr_find(&vds->addr_idr, id);
+ } else {
+ chan = idr_find(&vds->addr_idr, addr);
+ }
+ if (chan)
+ kref_get(&chan->refcount);
+ mutex_unlock(&vds->lock);
+
+ return chan;
+}
+
+static struct tipc_chan *vds_create_channel(struct tipc_virtio_dev *vds,
+ const struct tipc_chan_ops *ops,
+ void *ops_arg)
+{
+ int ret;
+ struct tipc_chan *chan = NULL;
+
+ if (!vds)
+ return ERR_PTR(-ENOENT);
+
+ if (!ops)
+ return ERR_PTR(-EINVAL);
+
+ chan = kzalloc(sizeof(*chan), GFP_KERNEL);
+ if (!chan)
+ return ERR_PTR(-ENOMEM);
+
+ kref_get(&vds->refcount);
+ chan->vds = vds;
+ chan->ops = ops;
+ chan->ops_arg = ops_arg;
+ mutex_init(&chan->lock);
+ kref_init(&chan->refcount);
+ chan->state = TIPC_DISCONNECTED;
+
+ ret = vds_add_channel(vds, chan);
+ if (ret) {
+ kfree(chan);
+ kref_put(&vds->refcount, _free_vds);
+ return ERR_PTR(ret);
+ }
+
+ return chan;
+}
+
+static void fill_msg_hdr(struct tipc_msg_buf *mb, u32 src, u32 dst)
+{
+ struct tipc_msg_hdr *hdr = mb_get_data(mb, sizeof(*hdr));
+
+ hdr->src = src;
+ hdr->dst = dst;
+ hdr->len = mb_avail_data(mb);
+ hdr->flags = 0;
+ hdr->shm_cnt = mb->shm_cnt;
+ hdr->reserved = 0;
+}
+
+static int tipc_shared_handle_new(struct tipc_shared_handle **shared_handle,
+ struct tipc_virtio_dev *vds)
+{
+ struct tipc_shared_handle *out = kzalloc(sizeof(*out), GFP_KERNEL);
+
+ if (!out)
+ return -ENOMEM;
+
+ out->vds = vds;
+ *shared_handle = out;
+
+ return 0;
+}
+
+static struct device *tipc_shared_handle_dev(struct tipc_shared_handle
+ *shared_handle)
+{
+ return shared_handle->vds->vdev->dev.parent->parent;
+}
+
+static bool is_same_memory_region(struct tipc_shared_handle *h1,
+ struct tipc_shared_handle *h2)
+{
+ return h1->tipc.obj_id == h2->tipc.obj_id &&
+ h1->tipc.size == h2->tipc.size &&
+ h1->tipc.tag == h2->tipc.tag &&
+ h1->dma_buf == h2->dma_buf &&
+ h1->shared == h2->shared;
+}
+
+static bool dma_buf_owns_shared_mem_id(struct tipc_shared_handle *h)
+{
+ /* h->shared is true only if dma_buf did not own an shared memory ID */
+ return !h->shared;
+}
+
+static void tipc_shared_handle_register(struct tipc_shared_handle
+ *new_handle)
+{
+ struct tipc_virtio_dev *vds = new_handle->vds;
+ struct rb_node **new;
+ struct rb_node *parent = NULL;
+
+ mutex_lock(&vds->shared_handles_lock);
+
+ new = &vds->shared_handles.rb_node;
+ while (*new) {
+ struct tipc_shared_handle *handle =
+ rb_entry(*new, struct tipc_shared_handle, node);
+ parent = *new;
+ /*
+ * An obj_id can be registered multiple times if it's owned by a
+ * dma_buf, because in this case we use the same obj_id across
+ * multiple memory transfer operations.
+ */
+ if (handle->tipc.obj_id == new_handle->tipc.obj_id) {
+ if (dma_buf_owns_shared_mem_id(new_handle)) {
+ WARN_ON(!is_same_memory_region(handle,
+ new_handle));
+ } else {
+ WARN(1, "This handle is already registered");
+ goto already_registered;
+ }
+ }
+
+ if (handle->tipc.obj_id > new_handle->tipc.obj_id)
+ new = &((*new)->rb_left);
+ else
+ new = &((*new)->rb_right);
+ }
+
+ rb_link_node(&new_handle->node, parent, new);
+ rb_insert_color(&new_handle->node, &vds->shared_handles);
+
+already_registered:
+ mutex_unlock(&vds->shared_handles_lock);
+}
+
+static struct tipc_shared_handle *tipc_shared_handle_take(struct tipc_virtio_dev
+ *vds,
+ trusty_shared_mem_id_t
+ obj_id)
+{
+ struct rb_node *node;
+ struct tipc_shared_handle *out = NULL;
+
+ mutex_lock(&vds->shared_handles_lock);
+
+ node = vds->shared_handles.rb_node;
+ while (node) {
+ struct tipc_shared_handle *handle =
+ rb_entry(node, struct tipc_shared_handle, node);
+ if (obj_id == handle->tipc.obj_id) {
+ rb_erase(node, &vds->shared_handles);
+ out = handle;
+ break;
+ } else if (obj_id < handle->tipc.obj_id) {
+ node = node->rb_left;
+ } else {
+ node = node->rb_right;
+ }
+ }
+
+ mutex_unlock(&vds->shared_handles_lock);
+
+ return out;
+}
+
+static int tipc_shared_handle_drop(struct tipc_shared_handle *shared_handle)
+{
+ int ret;
+ struct tipc_virtio_dev *vds = shared_handle->vds;
+ struct device *dev = tipc_shared_handle_dev(shared_handle);
+
+ if (shared_handle->shared) {
+ /*
+ * If this warning fires, it means this shared handle was still
+ * in the set of active handles. This shouldn't happen (calling
+ * code should ensure it is out if the tree) but this serves as
+ * an extra check before it is released.
+ *
+ * However, the take itself should clean this incorrect state up
+ * by removing the handle from the tree.
+ *
+ * This warning is only applicable when registering a handle
+ * multiple times is not allowed, i.e. when dma_buf doesn't own
+ * the handle.
+ */
+ WARN_ON(tipc_shared_handle_take(vds,
+ shared_handle->tipc.obj_id));
+
+ ret = trusty_reclaim_memory(dev,
+ shared_handle->tipc.obj_id,
+ shared_handle->sgt->sgl,
+ shared_handle->sgt->orig_nents);
+ if (ret) {
+ /*
+ * We can't safely release this, it may still be in
+ * use outside Linux.
+ */
+ dev_warn(dev, "Failed to drop handle, leaking...\n");
+ return ret;
+ }
+ }
+
+ if (shared_handle->sgt)
+ dma_buf_unmap_attachment(shared_handle->attach,
+ shared_handle->sgt, DMA_BIDIRECTIONAL);
+ if (shared_handle->attach)
+ dma_buf_detach(shared_handle->dma_buf, shared_handle->attach);
+ if (shared_handle->dma_buf)
+ dma_buf_put(shared_handle->dma_buf);
+
+ kfree(shared_handle);
+
+ return 0;
+}
+
+/*****************************************************************************/
+
+struct tipc_chan *tipc_create_channel(struct device *dev,
+ const struct tipc_chan_ops *ops,
+ void *ops_arg)
+{
+ struct virtio_device *vd;
+ struct tipc_chan *chan;
+ struct tipc_virtio_dev *vds;
+
+ mutex_lock(&tipc_devices_lock);
+ if (dev) {
+ vd = container_of(dev, struct virtio_device, dev);
+ } else {
+ vd = default_vdev;
+ if (!vd) {
+ mutex_unlock(&tipc_devices_lock);
+ return ERR_PTR(-ENOENT);
+ }
+ }
+ vds = vd->priv;
+ kref_get(&vds->refcount);
+ mutex_unlock(&tipc_devices_lock);
+
+ chan = vds_create_channel(vds, ops, ops_arg);
+ kref_put(&vds->refcount, _free_vds);
+ return chan;
+}
+EXPORT_SYMBOL(tipc_create_channel);
+
+struct tipc_msg_buf *tipc_chan_get_rxbuf(struct tipc_chan *chan)
+{
+ return vds_alloc_msg_buf(chan->vds, true);
+}
+EXPORT_SYMBOL(tipc_chan_get_rxbuf);
+
+void tipc_chan_put_rxbuf(struct tipc_chan *chan, struct tipc_msg_buf *mb)
+{
+ vds_free_msg_buf(chan->vds, mb);
+}
+EXPORT_SYMBOL(tipc_chan_put_rxbuf);
+
+struct tipc_msg_buf *tipc_chan_get_txbuf_timeout(struct tipc_chan *chan,
+ long timeout)
+{
+ return vds_get_txbuf(chan->vds, timeout);
+}
+EXPORT_SYMBOL(tipc_chan_get_txbuf_timeout);
+
+void tipc_chan_put_txbuf(struct tipc_chan *chan, struct tipc_msg_buf *mb)
+{
+ vds_put_txbuf(chan->vds, mb);
+}
+EXPORT_SYMBOL(tipc_chan_put_txbuf);
+
+int tipc_chan_queue_msg(struct tipc_chan *chan, struct tipc_msg_buf *mb)
+{
+ int err;
+
+ mutex_lock(&chan->lock);
+ switch (chan->state) {
+ case TIPC_CONNECTED:
+ fill_msg_hdr(mb, chan->local, chan->remote);
+ err = vds_queue_txbuf(chan->vds, mb);
+ if (err) {
+ /* this should never happen */
+ dev_err(&chan->vds->vdev->dev,
+ "%s: failed to queue tx buffer (%d)\n",
+ __func__, err);
+ }
+ break;
+ case TIPC_DISCONNECTED:
+ case TIPC_CONNECTING:
+ err = -ENOTCONN;
+ break;
+ case TIPC_STALE:
+ err = -ESHUTDOWN;
+ break;
+ default:
+ err = -EBADFD;
+ dev_err(&chan->vds->vdev->dev,
+ "%s: unexpected channel state %d\n",
+ __func__, chan->state);
+ }
+ mutex_unlock(&chan->lock);
+ return err;
+}
+EXPORT_SYMBOL(tipc_chan_queue_msg);
+
+
+int tipc_chan_connect(struct tipc_chan *chan, const char *name)
+{
+ int err;
+ struct tipc_ctrl_msg *msg;
+ struct tipc_conn_req_body *body;
+ struct tipc_msg_buf *txbuf;
+
+ txbuf = vds_get_txbuf(chan->vds, TXBUF_TIMEOUT);
+ if (IS_ERR(txbuf))
+ return PTR_ERR(txbuf);
+
+ /* reserve space for connection request control message */
+ msg = mb_put_data(txbuf, sizeof(*msg) + sizeof(*body));
+ body = (struct tipc_conn_req_body *)msg->body;
+
+ /* fill message */
+ msg->type = TIPC_CTRL_MSGTYPE_CONN_REQ;
+ msg->body_len = sizeof(*body);
+
+ strncpy(body->name, name, sizeof(body->name));
+ body->name[sizeof(body->name)-1] = '\0';
+
+ mutex_lock(&chan->lock);
+ switch (chan->state) {
+ case TIPC_DISCONNECTED:
+ /* save service name we are connecting to */
+ strcpy(chan->srv_name, body->name);
+
+ fill_msg_hdr(txbuf, chan->local, TIPC_CTRL_ADDR);
+ err = vds_queue_txbuf(chan->vds, txbuf);
+ if (err) {
+ /* this should never happen */
+ dev_err(&chan->vds->vdev->dev,
+ "%s: failed to queue tx buffer (%d)\n",
+ __func__, err);
+ } else {
+ chan->state = TIPC_CONNECTING;
+ txbuf = NULL; /* prevents discarding buffer */
+ }
+ break;
+ case TIPC_CONNECTED:
+ case TIPC_CONNECTING:
+ /* check if we are trying to connect to the same service */
+ if (strcmp(chan->srv_name, body->name) == 0)
+ err = 0;
+ else
+ if (chan->state == TIPC_CONNECTING)
+ err = -EALREADY; /* in progress */
+ else
+ err = -EISCONN; /* already connected */
+ break;
+
+ case TIPC_STALE:
+ err = -ESHUTDOWN;
+ break;
+ default:
+ err = -EBADFD;
+ dev_err(&chan->vds->vdev->dev,
+ "%s: unexpected channel state %d\n",
+ __func__, chan->state);
+ break;
+ }
+ mutex_unlock(&chan->lock);
+
+ if (txbuf)
+ tipc_chan_put_txbuf(chan, txbuf); /* discard it */
+
+ return err;
+}
+EXPORT_SYMBOL(tipc_chan_connect);
+
+int tipc_chan_shutdown(struct tipc_chan *chan)
+{
+ int err;
+ struct tipc_ctrl_msg *msg;
+ struct tipc_disc_req_body *body;
+ struct tipc_msg_buf *txbuf = NULL;
+
+ /* get tx buffer */
+ txbuf = vds_get_txbuf(chan->vds, TXBUF_TIMEOUT);
+ if (IS_ERR(txbuf))
+ return PTR_ERR(txbuf);
+
+ mutex_lock(&chan->lock);
+ if (chan->state == TIPC_CONNECTED || chan->state == TIPC_CONNECTING) {
+ /* reserve space for disconnect request control message */
+ msg = mb_put_data(txbuf, sizeof(*msg) + sizeof(*body));
+ body = (struct tipc_disc_req_body *)msg->body;
+
+ msg->type = TIPC_CTRL_MSGTYPE_DISC_REQ;
+ msg->body_len = sizeof(*body);
+ body->target = chan->remote;
+
+ fill_msg_hdr(txbuf, chan->local, TIPC_CTRL_ADDR);
+ err = vds_queue_txbuf(chan->vds, txbuf);
+ if (err) {
+ /* this should never happen */
+ dev_err(&chan->vds->vdev->dev,
+ "%s: failed to queue tx buffer (%d)\n",
+ __func__, err);
+ }
+ } else {
+ err = -ENOTCONN;
+ }
+ chan->state = TIPC_STALE;
+ mutex_unlock(&chan->lock);
+
+ if (err) {
+ /* release buffer */
+ tipc_chan_put_txbuf(chan, txbuf);
+ }
+
+ return err;
+}
+EXPORT_SYMBOL(tipc_chan_shutdown);
+
+void tipc_chan_destroy(struct tipc_chan *chan)
+{
+ vds_del_channel(chan->vds, chan);
+ kref_put(&chan->refcount, _free_chan);
+}
+EXPORT_SYMBOL(tipc_chan_destroy);
+
+/***************************************************************************/
+
+struct tipc_dn_chan {
+ int state;
+ struct mutex lock; /* protects rx_msg_queue list and channel state */
+ struct tipc_chan *chan;
+ wait_queue_head_t readq;
+ struct completion reply_comp;
+ struct list_head rx_msg_queue;
+};
+
+static int dn_wait_for_reply(struct tipc_dn_chan *dn, int timeout)
+{
+ int ret;
+
+ ret = wait_for_completion_interruptible_timeout(&dn->reply_comp,
+ msecs_to_jiffies(timeout));
+ if (ret < 0)
+ return ret;
+
+ mutex_lock(&dn->lock);
+ if (!ret) {
+ /* no reply from remote */
+ dn->state = TIPC_STALE;
+ ret = -ETIMEDOUT;
+ } else {
+ /* got reply */
+ if (dn->state == TIPC_CONNECTED)
+ ret = 0;
+ else if (dn->state == TIPC_DISCONNECTED)
+ if (!list_empty(&dn->rx_msg_queue))
+ ret = 0;
+ else
+ ret = -ENOTCONN;
+ else
+ ret = -EIO;
+ }
+ mutex_unlock(&dn->lock);
+
+ return ret;
+}
+
+static struct tipc_msg_buf *dn_handle_msg(void *data,
+ struct tipc_msg_buf *rxbuf)
+{
+ struct tipc_dn_chan *dn = data;
+ struct tipc_msg_buf *newbuf = rxbuf;
+
+ mutex_lock(&dn->lock);
+ if (dn->state == TIPC_CONNECTED) {
+ /* get new buffer */
+ newbuf = tipc_chan_get_rxbuf(dn->chan);
+ if (newbuf) {
+ /* queue an old buffer and return a new one */
+ list_add_tail(&rxbuf->node, &dn->rx_msg_queue);
+ wake_up_interruptible(&dn->readq);
+ } else {
+ /*
+ * return an old buffer effectively discarding
+ * incoming message
+ */
+ dev_err(&dn->chan->vds->vdev->dev,
+ "%s: discard incoming message\n", __func__);
+ newbuf = rxbuf;
+ }
+ }
+ mutex_unlock(&dn->lock);
+
+ return newbuf;
+}
+
+static void dn_connected(struct tipc_dn_chan *dn)
+{
+ mutex_lock(&dn->lock);
+ dn->state = TIPC_CONNECTED;
+
+ /* complete all pending */
+ complete(&dn->reply_comp);
+
+ mutex_unlock(&dn->lock);
+}
+
+static void dn_disconnected(struct tipc_dn_chan *dn)
+{
+ mutex_lock(&dn->lock);
+ dn->state = TIPC_DISCONNECTED;
+
+ /* complete all pending */
+ complete(&dn->reply_comp);
+
+ /* wakeup all readers */
+ wake_up_interruptible_all(&dn->readq);
+
+ mutex_unlock(&dn->lock);
+}
+
+static void dn_shutdown(struct tipc_dn_chan *dn)
+{
+ mutex_lock(&dn->lock);
+
+ /* set state to STALE */
+ dn->state = TIPC_STALE;
+
+ /* complete all pending */
+ complete(&dn->reply_comp);
+
+ /* wakeup all readers */
+ wake_up_interruptible_all(&dn->readq);
+
+ mutex_unlock(&dn->lock);
+}
+
+static void dn_handle_event(void *data, int event)
+{
+ struct tipc_dn_chan *dn = data;
+
+ switch (event) {
+ case TIPC_CHANNEL_SHUTDOWN:
+ dn_shutdown(dn);
+ break;
+
+ case TIPC_CHANNEL_DISCONNECTED:
+ dn_disconnected(dn);
+ break;
+
+ case TIPC_CHANNEL_CONNECTED:
+ dn_connected(dn);
+ break;
+
+ default:
+ dev_err(&dn->chan->vds->vdev->dev,
+ "%s: unhandled event %d\n", __func__, event);
+ break;
+ }
+}
+
+static void dn_handle_release(void *data)
+{
+ kfree(data);
+}
+
+static const struct tipc_chan_ops _dn_ops = {
+ .handle_msg = dn_handle_msg,
+ .handle_event = dn_handle_event,
+ .handle_release = dn_handle_release,
+};
+
+#define cdev_to_cdn(c) container_of((c), struct tipc_cdev_node, cdev)
+#define cdn_to_vds(cdn) container_of((cdn), struct tipc_virtio_dev, cdev_node)
+
+static struct tipc_virtio_dev *_dn_lookup_vds(struct tipc_cdev_node *cdn)
+{
+ int ret;
+ struct tipc_virtio_dev *vds = NULL;
+
+ mutex_lock(&tipc_devices_lock);
+ ret = idr_for_each(&tipc_devices, _match_data, cdn);
+ if (ret) {
+ vds = cdn_to_vds(cdn);
+ kref_get(&vds->refcount);
+ }
+ mutex_unlock(&tipc_devices_lock);
+ return vds;
+}
+
+static int tipc_open(struct inode *inode, struct file *filp)
+{
+ int ret;
+ struct tipc_virtio_dev *vds;
+ struct tipc_dn_chan *dn;
+ struct tipc_cdev_node *cdn = cdev_to_cdn(inode->i_cdev);
+
+ vds = _dn_lookup_vds(cdn);
+ if (!vds) {
+ ret = -ENOENT;
+ goto err_vds_lookup;
+ }
+
+ dn = kzalloc(sizeof(*dn), GFP_KERNEL);
+ if (!dn) {
+ ret = -ENOMEM;
+ goto err_alloc_chan;
+ }
+
+ mutex_init(&dn->lock);
+ init_waitqueue_head(&dn->readq);
+ init_completion(&dn->reply_comp);
+ INIT_LIST_HEAD(&dn->rx_msg_queue);
+
+ dn->state = TIPC_DISCONNECTED;
+
+ dn->chan = vds_create_channel(vds, &_dn_ops, dn);
+ if (IS_ERR(dn->chan)) {
+ ret = PTR_ERR(dn->chan);
+ goto err_create_chan;
+ }
+
+ filp->private_data = dn;
+ kref_put(&vds->refcount, _free_vds);
+ return 0;
+
+err_create_chan:
+ kfree(dn);
+err_alloc_chan:
+ kref_put(&vds->refcount, _free_vds);
+err_vds_lookup:
+ return ret;
+}
+
+
+static int dn_connect_ioctl(struct tipc_dn_chan *dn, char __user *usr_name)
+{
+ int ret;
+ char name[MAX_SRV_NAME_LEN];
+
+ /* copy in service name from user space */
+ ret = strncpy_from_user(name, usr_name, sizeof(name));
+ if (ret < 0)
+ return ret;
+ if (ret == sizeof(name))
+ return -ENAMETOOLONG;
+
+ /* send connect request */
+ ret = tipc_chan_connect(dn->chan, name);
+ if (ret)
+ return ret;
+
+ /* and wait for reply */
+ return dn_wait_for_reply(dn, REPLY_TIMEOUT);
+}
+
+static int dn_share_fd(struct tipc_dn_chan *dn, int fd,
+ enum transfer_kind transfer_kind,
+ struct tipc_shared_handle **out)
+{
+ int ret = 0;
+ struct tipc_shared_handle *shared_handle = NULL;
+ struct file *file = NULL;
+ struct device *dev = &dn->chan->vds->vdev->dev;
+ bool writable = false;
+ pgprot_t prot;
+ u64 tag = 0;
+ trusty_shared_mem_id_t mem_id;
+ bool lend;
+
+ if (dn->state != TIPC_CONNECTED) {
+ dev_dbg(dev, "Tried to share fd while not connected\n");
+ return -ENOTCONN;
+ }
+
+ file = fget(fd);
+ if (!file) {
+ dev_dbg(dev, "Invalid fd (%d)\n", fd);
+ return -EBADF;
+ }
+
+ if (!(file->f_mode & FMODE_READ)) {
+ dev_dbg(dev, "Cannot create write-only mapping\n");
+ fput(file);
+ return -EACCES;
+ }
+
+ writable = file->f_mode & FMODE_WRITE;
+ prot = writable ? PAGE_KERNEL : PAGE_KERNEL_RO;
+ fput(file);
+ file = NULL;
+
+ ret = tipc_shared_handle_new(&shared_handle, dn->chan->vds);
+ if (ret)
+ return ret;
+
+ shared_handle->dma_buf = dma_buf_get(fd);
+ if (IS_ERR(shared_handle->dma_buf)) {
+ ret = PTR_ERR(shared_handle->dma_buf);
+ shared_handle->dma_buf = NULL;
+ dev_dbg(dev, "Unable to get dma buf from fd (%d)\n", ret);
+ goto cleanup_handle;
+ }
+
+ tag = trusty_dma_buf_get_ffa_tag(shared_handle->dma_buf);
+ ret = trusty_dma_buf_get_shared_mem_id(shared_handle->dma_buf, &mem_id);
+ /*
+ * Buffers with a preallocated mem_id should only be sent to Trusty
+ * using TRUSTY_SEND_SECURE. And conversely, TRUSTY_SEND_SECURE should
+ * only be used to send buffers with preallcoated mem_id.
+ */
+ if (!ret) {
+ /* Use shared memory ID owned by dma_buf */
+ /* TODO: Enforce transfer_kind == TRUSTY_SEND_SECURE */
+ WARN_ONCE(transfer_kind != TRUSTY_SEND_SECURE,
+ "Use TRUSTY_SEND_SECURE instead");
+ goto mem_id_allocated;
+ }
+
+ if (ret != -ENODATA) {
+ dev_err(dev, "dma_buf can't be transferred (%d)\n", ret);
+ goto cleanup_handle;
+ }
+
+ if (transfer_kind == TRUSTY_SEND_SECURE) {
+ dev_err(dev, "No mem ID for TRUSTY_SEND_SECURE\n");
+ goto cleanup_handle;
+ }
+ lend = (transfer_kind == TRUSTY_LEND);
+
+ shared_handle->attach = dma_buf_attach(shared_handle->dma_buf, dev);
+ if (IS_ERR(shared_handle->attach)) {
+ ret = PTR_ERR(shared_handle->attach);
+ shared_handle->attach = NULL;
+ dev_dbg(dev, "Unable to attach to dma_buf (%d)\n", ret);
+ goto cleanup_handle;
+ }
+
+ shared_handle->sgt = dma_buf_map_attachment(shared_handle->attach,
+ DMA_BIDIRECTIONAL);
+ if (IS_ERR(shared_handle->sgt)) {
+ ret = PTR_ERR(shared_handle->sgt);
+ shared_handle->sgt = NULL;
+ dev_dbg(dev, "Failed to match attachment (%d)\n", ret);
+ goto cleanup_handle;
+ }
+
+ ret = trusty_transfer_memory(tipc_shared_handle_dev(shared_handle),
+ &mem_id, shared_handle->sgt->sgl,
+ shared_handle->sgt->orig_nents, prot, tag,
+ lend);
+
+ if (ret < 0) {
+ dev_dbg(dev, "Transferring memory failed: %d\n", ret);
+ /*
+ * The handle now has a sgt containing the pages, so we no
+ * longer need to clean up the pages directly.
+ */
+ goto cleanup_handle;
+ }
+ shared_handle->shared = true;
+
+mem_id_allocated:
+ shared_handle->tipc.obj_id = mem_id;
+ shared_handle->tipc.size = shared_handle->dma_buf->size;
+ shared_handle->tipc.tag = tag;
+ *out = shared_handle;
+ return 0;
+
+cleanup_handle:
+ tipc_shared_handle_drop(shared_handle);
+ return ret;
+}
+
+static ssize_t txbuf_write_iter(struct tipc_msg_buf *txbuf,
+ struct iov_iter *iter)
+{
+ size_t len;
+ /* message length */
+ len = iov_iter_count(iter);
+
+ /* check available space */
+ if (len > mb_avail_space(txbuf))
+ return -EMSGSIZE;
+
+ /* copy in message data */
+ if (copy_from_iter(mb_put_data(txbuf, len), len, iter) != len)
+ return -EFAULT;
+
+ return len;
+}
+
+static ssize_t txbuf_write_handles(struct tipc_msg_buf *txbuf,
+ struct tipc_shared_handle **shm_handles,
+ size_t shm_cnt)
+{
+ size_t idx;
+
+ /* message length */
+ size_t len = shm_cnt * sizeof(struct tipc_shm);
+
+ /* check available space */
+ if (len > mb_avail_space(txbuf))
+ return -EMSGSIZE;
+
+ /* copy over handles */
+ for (idx = 0; idx < shm_cnt; idx++) {
+ memcpy(mb_put_data(txbuf, sizeof(struct tipc_shm)),
+ &shm_handles[idx]->tipc,
+ sizeof(struct tipc_shm));
+ }
+
+ txbuf->shm_cnt += shm_cnt;
+
+ return len;
+}
+
+static long filp_send_ioctl(struct file *filp,
+ const struct tipc_send_msg_req __user *arg)
+{
+ struct tipc_send_msg_req req;
+ struct iovec fast_iovs[UIO_FASTIOV];
+ struct iovec *iov = fast_iovs;
+ struct iov_iter iter;
+ struct trusty_shm *shm = NULL;
+ struct tipc_shared_handle **shm_handles = NULL;
+ int shm_idx = 0;
+ int release_idx;
+ struct tipc_dn_chan *dn = filp->private_data;
+ struct tipc_virtio_dev *vds = dn->chan->vds;
+ struct device *dev = &vds->vdev->dev;
+ long timeout = TXBUF_TIMEOUT;
+ struct tipc_msg_buf *txbuf = NULL;
+ long ret = 0;
+ ssize_t data_len = 0;
+ ssize_t shm_len = 0;
+
+ if (copy_from_user(&req, arg, sizeof(req)))
+ return -EFAULT;
+
+ if (req.shm_cnt > U16_MAX)
+ return -E2BIG;
+
+ shm = kmalloc_array(req.shm_cnt, sizeof(*shm), GFP_KERNEL);
+ if (!shm)
+ return -ENOMEM;
+
+ shm_handles = kmalloc_array(req.shm_cnt, sizeof(*shm_handles),
+ GFP_KERNEL);
+ if (!shm_handles) {
+ ret = -ENOMEM;
+ goto shm_handles_alloc_failed;
+ }
+
+ if (copy_from_user(shm, u64_to_user_ptr(req.shm),
+ req.shm_cnt * sizeof(struct trusty_shm))) {
+ ret = -EFAULT;
+ goto load_shm_args_failed;
+ }
+
+ ret = import_iovec(READ, u64_to_user_ptr(req.iov), req.iov_cnt,
+ ARRAY_SIZE(fast_iovs), &iov, &iter);
+ if (ret < 0) {
+ dev_dbg(dev, "Failed to import iovec\n");
+ goto iov_import_failed;
+ }
+
+ for (shm_idx = 0; shm_idx < req.shm_cnt; shm_idx++) {
+ switch (shm[shm_idx].transfer) {
+ case TRUSTY_SHARE:
+ case TRUSTY_LEND:
+ case TRUSTY_SEND_SECURE:
+ break;
+ default:
+ dev_err(dev, "Unknown transfer type: 0x%x\n",
+ shm[shm_idx].transfer);
+ goto shm_share_failed;
+ }
+ ret = dn_share_fd(dn, shm[shm_idx].fd, shm[shm_idx].transfer,
+ &shm_handles[shm_idx]);
+ if (ret) {
+ dev_dbg(dev, "Forwarding memory failed\n"
+ );
+ goto shm_share_failed;
+ }
+ }
+
+ if (filp->f_flags & O_NONBLOCK)
+ timeout = 0;
+
+ txbuf = tipc_chan_get_txbuf_timeout(dn->chan, timeout);
+ if (IS_ERR(txbuf)) {
+ dev_dbg(dev, "Failed to get txbuffer\n");
+ ret = PTR_ERR(txbuf);
+ goto get_txbuf_failed;
+ }
+
+ data_len = txbuf_write_iter(txbuf, &iter);
+ if (data_len < 0) {
+ ret = data_len;
+ goto txbuf_write_failed;
+ }
+
+ shm_len = txbuf_write_handles(txbuf, shm_handles, req.shm_cnt);
+ if (shm_len < 0) {
+ ret = shm_len;
+ goto txbuf_write_failed;
+ }
+
+ /*
+ * These need to be aded to the index before queueing the message.
+ * As soon as the message is sent, we may receive a message back from
+ * Trusty saying it's no longer in use, and the shared_handle needs
+ * to be there when that happens.
+ */
+ for (shm_idx = 0; shm_idx < req.shm_cnt; shm_idx++)
+ tipc_shared_handle_register(shm_handles[shm_idx]);
+
+ ret = tipc_chan_queue_msg(dn->chan, txbuf);
+
+ if (ret)
+ goto queue_failed;
+
+ ret = data_len;
+
+common_cleanup:
+ kfree(iov);
+iov_import_failed:
+load_shm_args_failed:
+ kfree(shm_handles);
+shm_handles_alloc_failed:
+ kfree(shm);
+ return ret;
+
+queue_failed:
+ for (release_idx = 0; release_idx < req.shm_cnt; release_idx++)
+ tipc_shared_handle_take(vds,
+ shm_handles[release_idx]->tipc.obj_id);
+txbuf_write_failed:
+ tipc_chan_put_txbuf(dn->chan, txbuf);
+get_txbuf_failed:
+shm_share_failed:
+ for (shm_idx--; shm_idx >= 0; shm_idx--)
+ tipc_shared_handle_drop(shm_handles[shm_idx]);
+ goto common_cleanup;
+}
+
+static long tipc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+ struct tipc_dn_chan *dn = filp->private_data;
+
+ switch (cmd) {
+ case TIPC_IOC_CONNECT:
+ return dn_connect_ioctl(dn, (char __user *)arg);
+ case TIPC_IOC_SEND_MSG:
+ return filp_send_ioctl(filp,
+ (const struct tipc_send_msg_req __user *)
+ arg);
+ default:
+ dev_dbg(&dn->chan->vds->vdev->dev,
+ "Unhandled ioctl cmd: 0x%x\n", cmd);
+ return -ENOTTY;
+ }
+}
+
+#ifdef CONFIG_COMPAT
+static long tipc_compat_ioctl(struct file *filp,
+ unsigned int cmd, unsigned long arg)
+{
+ struct tipc_dn_chan *dn = filp->private_data;
+
+ switch (cmd) {
+ case TIPC_IOC32_CONNECT:
+ cmd = TIPC_IOC_CONNECT;
+ break;
+ default:
+ dev_dbg(&dn->chan->vds->vdev->dev,
+ "Unhandled compat ioctl command: 0x%x\n", cmd);
+ return -ENOTTY;
+ }
+ return tipc_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
+}
+#endif
+
+static inline bool _got_rx(struct tipc_dn_chan *dn)
+{
+ if (dn->state != TIPC_CONNECTED)
+ return true;
+
+ if (!list_empty(&dn->rx_msg_queue))
+ return true;
+
+ return false;
+}
+
+static ssize_t tipc_read_iter(struct kiocb *iocb, struct iov_iter *iter)
+{
+ ssize_t ret;
+ size_t len;
+ struct tipc_msg_buf *mb;
+ struct file *filp = iocb->ki_filp;
+ struct tipc_dn_chan *dn = filp->private_data;
+
+ mutex_lock(&dn->lock);
+
+ while (list_empty(&dn->rx_msg_queue)) {
+ if (dn->state != TIPC_CONNECTED) {
+ if (dn->state == TIPC_CONNECTING)
+ ret = -ENOTCONN;
+ else if (dn->state == TIPC_DISCONNECTED)
+ ret = -ENOTCONN;
+ else if (dn->state == TIPC_STALE)
+ ret = -ESHUTDOWN;
+ else
+ ret = -EBADFD;
+ goto out;
+ }
+
+ mutex_unlock(&dn->lock);
+
+ if (filp->f_flags & O_NONBLOCK)
+ return -EAGAIN;
+
+ if (wait_event_interruptible(dn->readq, _got_rx(dn)))
+ return -ERESTARTSYS;
+
+ mutex_lock(&dn->lock);
+ }
+
+ mb = list_first_entry(&dn->rx_msg_queue, struct tipc_msg_buf, node);
+
+ len = mb_avail_data(mb);
+ if (len > iov_iter_count(iter)) {
+ ret = -EMSGSIZE;
+ goto out;
+ }
+
+ if (copy_to_iter(mb_get_data(mb, len), len, iter) != len) {
+ ret = -EFAULT;
+ goto out;
+ }
+
+ ret = len;
+ list_del(&mb->node);
+ tipc_chan_put_rxbuf(dn->chan, mb);
+
+out:
+ mutex_unlock(&dn->lock);
+ return ret;
+}
+
+static ssize_t tipc_write_iter(struct kiocb *iocb, struct iov_iter *iter)
+{
+ struct file *filp = iocb->ki_filp;
+ struct tipc_dn_chan *dn = filp->private_data;
+ long timeout = TXBUF_TIMEOUT;
+ struct tipc_msg_buf *txbuf = NULL;
+ ssize_t ret = 0;
+ ssize_t len = 0;
+
+ if (filp->f_flags & O_NONBLOCK)
+ timeout = 0;
+
+ txbuf = tipc_chan_get_txbuf_timeout(dn->chan, timeout);
+
+ if (IS_ERR(txbuf))
+ return PTR_ERR(txbuf);
+
+ len = txbuf_write_iter(txbuf, iter);
+ if (len < 0)
+ goto err_out;
+
+ /* queue message */
+ ret = tipc_chan_queue_msg(dn->chan, txbuf);
+ if (ret)
+ goto err_out;
+
+ return len;
+
+err_out:
+ tipc_chan_put_txbuf(dn->chan, txbuf);
+ return ret;
+}
+
+static __poll_t tipc_poll(struct file *filp, poll_table *wait)
+{
+ __poll_t mask = 0;
+ struct tipc_dn_chan *dn = filp->private_data;
+
+ mutex_lock(&dn->lock);
+
+ poll_wait(filp, &dn->readq, wait);
+
+ /* Writes always succeed for now */
+ mask |= EPOLLOUT | EPOLLWRNORM;
+
+ if (!list_empty(&dn->rx_msg_queue))
+ mask |= EPOLLIN | EPOLLRDNORM;
+
+ if (dn->state != TIPC_CONNECTED)
+ mask |= EPOLLERR;
+
+ mutex_unlock(&dn->lock);
+ return mask;
+}
+
+
+static int tipc_release(struct inode *inode, struct file *filp)
+{
+ struct tipc_dn_chan *dn = filp->private_data;
+
+ dn_shutdown(dn);
+
+ /* free all pending buffers */
+ vds_free_msg_buf_list(dn->chan->vds, &dn->rx_msg_queue);
+
+ /* shutdown channel */
+ tipc_chan_shutdown(dn->chan);
+
+ /* and destroy it */
+ tipc_chan_destroy(dn->chan);
+
+ return 0;
+}
+
+static const struct file_operations tipc_fops = {
+ .open = tipc_open,
+ .release = tipc_release,
+ .unlocked_ioctl = tipc_ioctl,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = tipc_compat_ioctl,
+#endif
+ .read_iter = tipc_read_iter,
+ .write_iter = tipc_write_iter,
+ .poll = tipc_poll,
+ .owner = THIS_MODULE,
+};
+
+/*****************************************************************************/
+
+static void chan_trigger_event(struct tipc_chan *chan, int event)
+{
+ if (!event)
+ return;
+
+ chan->ops->handle_event(chan->ops_arg, event);
+}
+
+static void _cleanup_vq(struct tipc_virtio_dev *vds, struct virtqueue *vq)
+{
+ struct tipc_msg_buf *mb;
+
+ while ((mb = virtqueue_detach_unused_buf(vq)) != NULL)
+ vds_free_msg_buf(vds, mb);
+}
+
+static int _create_cdev_node(struct device *parent,
+ struct tipc_cdev_node *cdn,
+ const char *name)
+{
+ int ret;
+ dev_t devt;
+
+ if (!name) {
+ dev_dbg(parent, "%s: cdev name has to be provided\n",
+ __func__);
+ return -EINVAL;
+ }
+
+ /* allocate minor */
+ ret = idr_alloc(&tipc_devices, cdn, 0, MAX_DEVICES, GFP_KERNEL);
+ if (ret < 0) {
+ dev_dbg(parent, "%s: failed (%d) to get id\n",
+ __func__, ret);
+ return ret;
+ }
+
+ cdn->minor = ret;
+ cdev_init(&cdn->cdev, &tipc_fops);
+ cdn->cdev.owner = THIS_MODULE;
+
+ /* Add character device */
+ devt = MKDEV(tipc_major, cdn->minor);
+ ret = cdev_add(&cdn->cdev, devt, 1);
+ if (ret) {
+ dev_dbg(parent, "%s: cdev_add failed (%d)\n",
+ __func__, ret);
+ goto err_add_cdev;
+ }
+
+ /* Create a device node */
+ cdn->dev = device_create(tipc_class, parent,
+ devt, NULL, "trusty-ipc-%s", name);
+ if (IS_ERR(cdn->dev)) {
+ ret = PTR_ERR(cdn->dev);
+ dev_dbg(parent, "%s: device_create failed: %d\n",
+ __func__, ret);
+ goto err_device_create;
+ }
+
+ return 0;
+
+err_device_create:
+ cdn->dev = NULL;
+ cdev_del(&cdn->cdev);
+err_add_cdev:
+ idr_remove(&tipc_devices, cdn->minor);
+ return ret;
+}
+
+static void create_cdev_node(struct tipc_virtio_dev *vds,
+ struct tipc_cdev_node *cdn)
+{
+ int err;
+
+ mutex_lock(&tipc_devices_lock);
+
+ if (!default_vdev) {
+ kref_get(&vds->refcount);
+ default_vdev = vds->vdev;
+ }
+
+ if (vds->cdev_name[0] && !cdn->dev) {
+ kref_get(&vds->refcount);
+ err = _create_cdev_node(&vds->vdev->dev, cdn, vds->cdev_name);
+ if (err) {
+ dev_err(&vds->vdev->dev,
+ "failed (%d) to create cdev node\n", err);
+ kref_put(&vds->refcount, _free_vds);
+ }
+ }
+ mutex_unlock(&tipc_devices_lock);
+}
+
+static void destroy_cdev_node(struct tipc_virtio_dev *vds,
+ struct tipc_cdev_node *cdn)
+{
+ mutex_lock(&tipc_devices_lock);
+ if (cdn->dev) {
+ device_destroy(tipc_class, MKDEV(tipc_major, cdn->minor));
+ cdev_del(&cdn->cdev);
+ idr_remove(&tipc_devices, cdn->minor);
+ cdn->dev = NULL;
+ kref_put(&vds->refcount, _free_vds);
+ }
+
+ if (default_vdev == vds->vdev) {
+ default_vdev = NULL;
+ kref_put(&vds->refcount, _free_vds);
+ }
+
+ mutex_unlock(&tipc_devices_lock);
+}
+
+static void _go_online(struct tipc_virtio_dev *vds)
+{
+ mutex_lock(&vds->lock);
+ if (vds->state == VDS_OFFLINE)
+ vds->state = VDS_ONLINE;
+ mutex_unlock(&vds->lock);
+
+ create_cdev_node(vds, &vds->cdev_node);
+
+ dev_info(&vds->vdev->dev, "is online\n");
+}
+
+static void _go_offline(struct tipc_virtio_dev *vds)
+{
+ struct tipc_chan *chan;
+
+ /* change state to OFFLINE */
+ mutex_lock(&vds->lock);
+ if (vds->state != VDS_ONLINE) {
+ mutex_unlock(&vds->lock);
+ return;
+ }
+ vds->state = VDS_OFFLINE;
+ mutex_unlock(&vds->lock);
+
+ /* wakeup all waiters */
+ wake_up_interruptible_all(&vds->sendq);
+
+ /* shutdown all channels */
+ while ((chan = vds_lookup_channel(vds, TIPC_ANY_ADDR))) {
+ mutex_lock(&chan->lock);
+ chan->state = TIPC_STALE;
+ chan->remote = 0;
+ chan_trigger_event(chan, TIPC_CHANNEL_SHUTDOWN);
+ mutex_unlock(&chan->lock);
+ kref_put(&chan->refcount, _free_chan);
+ }
+
+ /* shutdown device node */
+ destroy_cdev_node(vds, &vds->cdev_node);
+
+ dev_info(&vds->vdev->dev, "is offline\n");
+}
+
+static void _handle_conn_rsp(struct tipc_virtio_dev *vds,
+ struct tipc_conn_rsp_body *rsp, size_t len)
+{
+ struct tipc_chan *chan;
+
+ if (sizeof(*rsp) != len) {
+ dev_err(&vds->vdev->dev, "%s: Invalid response length %zd\n",
+ __func__, len);
+ return;
+ }
+
+ dev_dbg(&vds->vdev->dev,
+ "%s: connection response: for addr 0x%x: status %d remote addr 0x%x\n",
+ __func__, rsp->target, rsp->status, rsp->remote);
+
+ /* Lookup channel */
+ chan = vds_lookup_channel(vds, rsp->target);
+ if (chan) {
+ mutex_lock(&chan->lock);
+ if (chan->state == TIPC_CONNECTING) {
+ if (!rsp->status) {
+ chan->state = TIPC_CONNECTED;
+ chan->remote = rsp->remote;
+ chan->max_msg_cnt = rsp->max_msg_cnt;
+ chan->max_msg_size = rsp->max_msg_size;
+ chan_trigger_event(chan,
+ TIPC_CHANNEL_CONNECTED);
+ } else {
+ chan->state = TIPC_DISCONNECTED;
+ chan->remote = 0;
+ chan_trigger_event(chan,
+ TIPC_CHANNEL_DISCONNECTED);
+ }
+ }
+ mutex_unlock(&chan->lock);
+ kref_put(&chan->refcount, _free_chan);
+ }
+}
+
+static void _handle_disc_req(struct tipc_virtio_dev *vds,
+ struct tipc_disc_req_body *req, size_t len)
+{
+ struct tipc_chan *chan;
+
+ if (sizeof(*req) != len) {
+ dev_err(&vds->vdev->dev, "%s: Invalid request length %zd\n",
+ __func__, len);
+ return;
+ }
+
+ dev_dbg(&vds->vdev->dev, "%s: disconnect request: for addr 0x%x\n",
+ __func__, req->target);
+
+ chan = vds_lookup_channel(vds, req->target);
+ if (chan) {
+ mutex_lock(&chan->lock);
+ if (chan->state == TIPC_CONNECTED ||
+ chan->state == TIPC_CONNECTING) {
+ chan->state = TIPC_DISCONNECTED;
+ chan->remote = 0;
+ chan_trigger_event(chan, TIPC_CHANNEL_DISCONNECTED);
+ }
+ mutex_unlock(&chan->lock);
+ kref_put(&chan->refcount, _free_chan);
+ }
+}
+
+static void _handle_release(struct tipc_virtio_dev *vds,
+ struct tipc_release_body *req, size_t len)
+{
+ struct tipc_shared_handle *handle = NULL;
+ struct device *dev = &vds->vdev->dev;
+ int ret = 0;
+
+ if (len < sizeof(*req)) {
+ dev_err(dev, "Received undersized release control message\n");
+ return;
+ }
+
+ handle = tipc_shared_handle_take(vds, req->id);
+ if (!handle) {
+ dev_err(dev,
+ "Received release control message for untracked handle: 0x%llx\n",
+ req->id);
+ return;
+ }
+
+ ret = tipc_shared_handle_drop(handle);
+
+ if (ret) {
+ dev_err(dev,
+ "Failed to release handle 0x%llx upon request: (%d)\n",
+ req->id, ret);
+ /*
+ * Put the handle back in case we got a spurious release now and
+ * get a real one later. This path should not happen, we're
+ * just trying to be robust.
+ */
+ tipc_shared_handle_register(handle);
+ }
+}
+
+static void _handle_ctrl_msg(struct tipc_virtio_dev *vds,
+ void *data, int len, u32 src)
+{
+ struct tipc_ctrl_msg *msg = data;
+
+ if ((len < sizeof(*msg)) || (sizeof(*msg) + msg->body_len != len)) {
+ dev_err(&vds->vdev->dev,
+ "%s: Invalid message length ( %d vs. %d)\n",
+ __func__, (int)(sizeof(*msg) + msg->body_len), len);
+ return;
+ }
+
+ dev_dbg(&vds->vdev->dev,
+ "%s: Incoming ctrl message: src 0x%x type %d len %d\n",
+ __func__, src, msg->type, msg->body_len);
+
+ switch (msg->type) {
+ case TIPC_CTRL_MSGTYPE_GO_ONLINE:
+ _go_online(vds);
+ break;
+
+ case TIPC_CTRL_MSGTYPE_GO_OFFLINE:
+ _go_offline(vds);
+ break;
+
+ case TIPC_CTRL_MSGTYPE_CONN_RSP:
+ _handle_conn_rsp(vds, (struct tipc_conn_rsp_body *)msg->body,
+ msg->body_len);
+ break;
+
+ case TIPC_CTRL_MSGTYPE_DISC_REQ:
+ _handle_disc_req(vds, (struct tipc_disc_req_body *)msg->body,
+ msg->body_len);
+ break;
+
+ case TIPC_CTRL_MSGTYPE_RELEASE:
+ _handle_release(vds, (struct tipc_release_body *)msg->body,
+ msg->body_len);
+ break;
+
+ default:
+ dev_warn(&vds->vdev->dev,
+ "%s: Unexpected message type: %d\n",
+ __func__, msg->type);
+ }
+}
+
+static void handle_dropped_chan_msg(struct tipc_virtio_dev *vds,
+ struct tipc_msg_buf *mb,
+ struct tipc_msg_hdr *msg)
+{
+ int shm_idx;
+ struct tipc_shm *shm;
+ struct tipc_shared_handle *shared_handle;
+ struct device *dev = &vds->vdev->dev;
+ size_t len;
+
+ if (msg->len < msg->shm_cnt * sizeof(*shm)) {
+ dev_err(dev, "shm_cnt does not fit in dropped message");
+ /* The message is corrupt, so we can't recover resources */
+ return;
+ }
+
+ len = msg->len - msg->shm_cnt * sizeof(*shm);
+ /* skip normal data */
+ (void)mb_get_data(mb, len);
+
+ for (shm_idx = 0; shm_idx < msg->shm_cnt; shm_idx++) {
+ shm = mb_get_data(mb, sizeof(*shm));
+ shared_handle = tipc_shared_handle_take(vds, shm->obj_id);
+ if (shared_handle) {
+ if (tipc_shared_handle_drop(shared_handle))
+ dev_err(dev,
+ "Failed to drop handle found in dropped buffer");
+ } else {
+ dev_err(dev,
+ "Found handle in dropped buffer which was not registered to tipc device...");
+ }
+ }
+}
+
+static void handle_dropped_mb(struct tipc_virtio_dev *vds,
+ struct tipc_msg_buf *mb)
+{
+ struct tipc_msg_hdr *msg;
+
+ mb_reset_read(mb);
+ msg = mb_get_data(mb, sizeof(*msg));
+ if (msg->dst != TIPC_CTRL_ADDR) {
+ handle_dropped_chan_msg(vds, mb, msg);
+ }
+}
+
+static int _handle_rxbuf(struct tipc_virtio_dev *vds,
+ struct tipc_msg_buf *rxbuf, size_t rxlen)
+{
+ int err;
+ struct scatterlist sg;
+ struct tipc_msg_hdr *msg;
+ struct device *dev = &vds->vdev->dev;
+
+ /* message sanity check */
+ if (rxlen > rxbuf->buf_sz) {
+ dev_warn(dev, "inbound msg is too big: %zd\n", rxlen);
+ goto drop_it;
+ }
+
+ if (rxlen < sizeof(*msg)) {
+ dev_warn(dev, "inbound msg is too short: %zd\n", rxlen);
+ goto drop_it;
+ }
+
+ /* reset buffer and put data */
+ mb_reset(rxbuf);
+ mb_put_data(rxbuf, rxlen);
+
+ /* get message header */
+ msg = mb_get_data(rxbuf, sizeof(*msg));
+ if (mb_avail_data(rxbuf) != msg->len) {
+ dev_warn(dev, "inbound msg length mismatch: (%zu vs. %d)\n",
+ mb_avail_data(rxbuf), msg->len);
+ goto drop_it;
+ }
+
+ dev_dbg(dev, "From: %d, To: %d, Len: %d, Flags: 0x%x, Reserved: %d, shm_cnt: %d\n",
+ msg->src, msg->dst, msg->len, msg->flags, msg->reserved,
+ msg->shm_cnt);
+
+ /* message directed to control endpoint is a special case */
+ if (msg->dst == TIPC_CTRL_ADDR) {
+ _handle_ctrl_msg(vds, msg->data, msg->len, msg->src);
+ } else {
+ struct tipc_chan *chan = NULL;
+ /* Lookup channel */
+ chan = vds_lookup_channel(vds, msg->dst);
+ if (chan) {
+ /* handle it */
+ rxbuf = chan->ops->handle_msg(chan->ops_arg, rxbuf);
+ kref_put(&chan->refcount, _free_chan);
+ if (WARN_ON(!rxbuf))
+ return -EINVAL;
+ }
+ }
+
+drop_it:
+ /* add the buffer back to the virtqueue */
+ sg_init_one(&sg, rxbuf, rxbuf->buf_sz);
+ err = virtqueue_add_inbuf(vds->rxvq, &sg, 1, rxbuf, GFP_KERNEL);
+ if (err < 0) {
+ dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
+ return err;
+ }
+
+ return 0;
+}
+
+static void _rxvq_cb(struct virtqueue *rxvq)
+{
+ unsigned int len;
+ struct tipc_msg_buf *mb;
+ unsigned int msg_cnt = 0;
+ struct tipc_virtio_dev *vds = rxvq->vdev->priv;
+
+ while ((mb = virtqueue_get_buf(rxvq, &len)) != NULL) {
+ if (_handle_rxbuf(vds, mb, len))
+ break;
+ msg_cnt++;
+ }
+
+ /* tell the other size that we added rx buffers */
+ if (msg_cnt)
+ virtqueue_kick(rxvq);
+}
+
+static void _txvq_cb(struct virtqueue *txvq)
+{
+ unsigned int len;
+ struct tipc_msg_buf *mb;
+ bool need_wakeup = false;
+ struct tipc_virtio_dev *vds = txvq->vdev->priv;
+
+ /* detach all buffers */
+ mutex_lock(&vds->lock);
+ while ((mb = virtqueue_get_buf(txvq, &len)) != NULL) {
+ if ((int)len < 0)
+ handle_dropped_mb(vds, mb);
+ need_wakeup |= _put_txbuf_locked(vds, mb);
+ }
+ mutex_unlock(&vds->lock);
+
+ if (need_wakeup) {
+ /* wake up potential senders waiting for a tx buffer */
+ wake_up_interruptible_all(&vds->sendq);
+ }
+}
+
+static int tipc_virtio_probe(struct virtio_device *vdev)
+{
+ int err, i;
+ struct tipc_virtio_dev *vds;
+ struct tipc_dev_config config;
+ struct virtqueue *vqs[2];
+ vq_callback_t *vq_cbs[] = {_rxvq_cb, _txvq_cb};
+ static const char * const vq_names[] = { "rx", "tx" };
+
+ vds = kzalloc(sizeof(*vds), GFP_KERNEL);
+ if (!vds)
+ return -ENOMEM;
+
+ vds->vdev = vdev;
+
+ mutex_init(&vds->lock);
+ mutex_init(&vds->shared_handles_lock);
+ kref_init(&vds->refcount);
+ init_waitqueue_head(&vds->sendq);
+ INIT_LIST_HEAD(&vds->free_buf_list);
+ idr_init(&vds->addr_idr);
+ vds->shared_handles = RB_ROOT;
+ dma_coerce_mask_and_coherent(&vds->vdev->dev,
+ *vds->vdev->dev.parent->parent->dma_mask);
+
+ /* set default max message size and alignment */
+ memset(&config, 0, sizeof(config));
+ config.msg_buf_max_size = DEFAULT_MSG_BUF_SIZE;
+ config.msg_buf_alignment = DEFAULT_MSG_BUF_ALIGN;
+
+ /* get configuration if present */
+ vdev->config->get(vdev, 0, &config, sizeof(config));
+
+ /* copy dev name */
+ strncpy(vds->cdev_name, config.dev_name, sizeof(vds->cdev_name));
+ vds->cdev_name[sizeof(vds->cdev_name)-1] = '\0';
+
+ /* find tx virtqueues (rx and tx and in this order) */
+ err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, vq_names, NULL,
+ NULL);
+ if (err)
+ goto err_find_vqs;
+
+ vds->rxvq = vqs[0];
+ vds->txvq = vqs[1];
+
+ /* save max buffer size and count */
+ vds->msg_buf_max_sz = config.msg_buf_max_size;
+ vds->msg_buf_max_cnt = virtqueue_get_vring_size(vds->txvq);
+
+ /* set up the receive buffers */
+ for (i = 0; i < virtqueue_get_vring_size(vds->rxvq); i++) {
+ struct scatterlist sg;
+ struct tipc_msg_buf *rxbuf;
+
+ rxbuf = vds_alloc_msg_buf(vds, true);
+ if (!rxbuf) {
+ dev_err(&vdev->dev, "failed to allocate rx buffer\n");
+ err = -ENOMEM;
+ goto err_free_rx_buffers;
+ }
+
+ sg_init_one(&sg, rxbuf, rxbuf->buf_sz);
+ err = virtqueue_add_inbuf(vds->rxvq, &sg, 1, rxbuf, GFP_KERNEL);
+ WARN_ON(err); /* sanity check; this can't really happen */
+ }
+
+ vdev->priv = vds;
+ vds->state = VDS_OFFLINE;
+
+ dev_dbg(&vdev->dev, "%s: done\n", __func__);
+ return 0;
+
+err_free_rx_buffers:
+ _cleanup_vq(vds, vds->rxvq);
+err_find_vqs:
+ kref_put(&vds->refcount, _free_vds);
+ return err;
+}
+
+static void tipc_virtio_remove(struct virtio_device *vdev)
+{
+ struct tipc_virtio_dev *vds = vdev->priv;
+
+ _go_offline(vds);
+
+ mutex_lock(&vds->lock);
+ vds->state = VDS_DEAD;
+ vds->vdev = NULL;
+ mutex_unlock(&vds->lock);
+
+ vdev->config->reset(vdev);
+
+ idr_destroy(&vds->addr_idr);
+
+ _cleanup_vq(vds, vds->rxvq);
+ _cleanup_vq(vds, vds->txvq);
+ vds_free_msg_buf_list(vds, &vds->free_buf_list);
+
+ vdev->config->del_vqs(vds->vdev);
+
+ kref_put(&vds->refcount, _free_vds);
+}
+
+static const struct virtio_device_id tipc_virtio_id_table[] = {
+ { VIRTIO_ID_TRUSTY_IPC, VIRTIO_DEV_ANY_ID },
+ { 0 },
+};
+
+static const unsigned int features[] = {
+ 0,
+};
+
+static struct virtio_driver virtio_tipc_driver = {
+ .feature_table = features,
+ .feature_table_size = ARRAY_SIZE(features),
+ .driver.name = KBUILD_MODNAME,
+ .driver.owner = THIS_MODULE,
+ .id_table = tipc_virtio_id_table,
+ .probe = tipc_virtio_probe,
+ .remove = tipc_virtio_remove,
+};
+
+static int __init tipc_init(void)
+{
+ int ret;
+ dev_t dev;
+
+ ret = alloc_chrdev_region(&dev, 0, MAX_DEVICES, KBUILD_MODNAME);
+ if (ret) {
+ pr_err("%s: alloc_chrdev_region failed: %d\n", __func__, ret);
+ return ret;
+ }
+
+ tipc_major = MAJOR(dev);
+ tipc_class = class_create(THIS_MODULE, KBUILD_MODNAME);
+ if (IS_ERR(tipc_class)) {
+ ret = PTR_ERR(tipc_class);
+ pr_err("%s: class_create failed: %d\n", __func__, ret);
+ goto err_class_create;
+ }
+
+ ret = register_virtio_driver(&virtio_tipc_driver);
+ if (ret) {
+ pr_err("failed to register virtio driver: %d\n", ret);
+ goto err_register_virtio_drv;
+ }
+
+ return 0;
+
+err_register_virtio_drv:
+ class_destroy(tipc_class);
+
+err_class_create:
+ unregister_chrdev_region(dev, MAX_DEVICES);
+ return ret;
+}
+
+static void __exit tipc_exit(void)
+{
+ unregister_virtio_driver(&virtio_tipc_driver);
+ class_destroy(tipc_class);
+ unregister_chrdev_region(MKDEV(tipc_major, 0), MAX_DEVICES);
+}
+
+/* We need to init this early */
+subsys_initcall(tipc_init);
+module_exit(tipc_exit);
+
+MODULE_DEVICE_TABLE(tipc, tipc_virtio_id_table);
+MODULE_DESCRIPTION("Trusty IPC driver");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/trusty/trusty-irq.c b/drivers/trusty/trusty-irq.c
new file mode 100644
index 000000000000..5c6076108d0e
--- /dev/null
+++ b/drivers/trusty/trusty-irq.c
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2013 Google, Inc.
+ */
+
+#include <linux/cpu.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_irq.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/trusty/smcall.h>
+#include <linux/trusty/sm_err.h>
+#include <linux/trusty/trusty.h>
+
+struct trusty_irq {
+ struct trusty_irq_state *is;
+ struct hlist_node node;
+ unsigned int irq;
+ bool percpu;
+ bool enable;
+ bool doorbell;
+ struct trusty_irq __percpu *percpu_ptr;
+};
+
+struct trusty_irq_irqset {
+ struct hlist_head pending;
+ struct hlist_head inactive;
+};
+
+struct trusty_irq_state {
+ struct device *dev;
+ struct device *trusty_dev;
+ struct trusty_irq_irqset normal_irqs;
+ spinlock_t normal_irqs_lock;
+ struct trusty_irq_irqset __percpu *percpu_irqs;
+ struct notifier_block trusty_call_notifier;
+ struct hlist_node cpuhp_node;
+};
+
+static int trusty_irq_cpuhp_slot = -1;
+
+static void trusty_irq_enable_pending_irqs(struct trusty_irq_state *is,
+ struct trusty_irq_irqset *irqset,
+ bool percpu)
+{
+ struct hlist_node *n;
+ struct trusty_irq *trusty_irq;
+
+ hlist_for_each_entry_safe(trusty_irq, n, &irqset->pending, node) {
+ dev_dbg(is->dev,
+ "%s: enable pending irq %d, percpu %d, cpu %d\n",
+ __func__, trusty_irq->irq, percpu, smp_processor_id());
+ if (percpu)
+ enable_percpu_irq(trusty_irq->irq, 0);
+ else
+ enable_irq(trusty_irq->irq);
+ hlist_del(&trusty_irq->node);
+ hlist_add_head(&trusty_irq->node, &irqset->inactive);
+ }
+}
+
+static void trusty_irq_enable_irqset(struct trusty_irq_state *is,
+ struct trusty_irq_irqset *irqset)
+{
+ struct trusty_irq *trusty_irq;
+
+ hlist_for_each_entry(trusty_irq, &irqset->inactive, node) {
+ if (trusty_irq->enable) {
+ dev_warn(is->dev,
+ "%s: percpu irq %d already enabled, cpu %d\n",
+ __func__, trusty_irq->irq, smp_processor_id());
+ continue;
+ }
+ dev_dbg(is->dev, "%s: enable percpu irq %d, cpu %d\n",
+ __func__, trusty_irq->irq, smp_processor_id());
+ enable_percpu_irq(trusty_irq->irq, 0);
+ trusty_irq->enable = true;
+ }
+}
+
+static void trusty_irq_disable_irqset(struct trusty_irq_state *is,
+ struct trusty_irq_irqset *irqset)
+{
+ struct hlist_node *n;
+ struct trusty_irq *trusty_irq;
+
+ hlist_for_each_entry(trusty_irq, &irqset->inactive, node) {
+ if (!trusty_irq->enable) {
+ dev_warn(is->dev,
+ "irq %d already disabled, percpu %d, cpu %d\n",
+ trusty_irq->irq, trusty_irq->percpu,
+ smp_processor_id());
+ continue;
+ }
+ dev_dbg(is->dev, "%s: disable irq %d, percpu %d, cpu %d\n",
+ __func__, trusty_irq->irq, trusty_irq->percpu,
+ smp_processor_id());
+ trusty_irq->enable = false;
+ if (trusty_irq->percpu)
+ disable_percpu_irq(trusty_irq->irq);
+ else
+ disable_irq_nosync(trusty_irq->irq);
+ }
+ hlist_for_each_entry_safe(trusty_irq, n, &irqset->pending, node) {
+ if (!trusty_irq->enable) {
+ dev_warn(is->dev,
+ "pending irq %d already disabled, percpu %d, cpu %d\n",
+ trusty_irq->irq, trusty_irq->percpu,
+ smp_processor_id());
+ }
+ dev_dbg(is->dev,
+ "%s: disable pending irq %d, percpu %d, cpu %d\n",
+ __func__, trusty_irq->irq, trusty_irq->percpu,
+ smp_processor_id());
+ trusty_irq->enable = false;
+ hlist_del(&trusty_irq->node);
+ hlist_add_head(&trusty_irq->node, &irqset->inactive);
+ }
+}
+
+static int trusty_irq_call_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct trusty_irq_state *is;
+
+ if (WARN_ON(!irqs_disabled()))
+ return NOTIFY_DONE;
+
+ if (action != TRUSTY_CALL_PREPARE)
+ return NOTIFY_DONE;
+
+ is = container_of(nb, struct trusty_irq_state, trusty_call_notifier);
+
+ spin_lock(&is->normal_irqs_lock);
+ trusty_irq_enable_pending_irqs(is, &is->normal_irqs, false);
+ spin_unlock(&is->normal_irqs_lock);
+ trusty_irq_enable_pending_irqs(is, this_cpu_ptr(is->percpu_irqs), true);
+
+ return NOTIFY_OK;
+}
+
+static irqreturn_t trusty_irq_handler(int irq, void *data)
+{
+ struct trusty_irq *trusty_irq = data;
+ struct trusty_irq_state *is = trusty_irq->is;
+ struct trusty_irq_irqset *irqset;
+
+ dev_dbg(is->dev, "%s: irq %d, percpu %d, cpu %d, enable %d\n",
+ __func__, irq, trusty_irq->irq, smp_processor_id(),
+ trusty_irq->enable);
+
+ if (!trusty_irq->doorbell) {
+ if (trusty_irq->percpu) {
+ disable_percpu_irq(irq);
+ irqset = this_cpu_ptr(is->percpu_irqs);
+ } else {
+ disable_irq_nosync(irq);
+ irqset = &is->normal_irqs;
+ }
+
+ spin_lock(&is->normal_irqs_lock);
+ if (trusty_irq->enable) {
+ hlist_del(&trusty_irq->node);
+ hlist_add_head(&trusty_irq->node, &irqset->pending);
+ }
+ spin_unlock(&is->normal_irqs_lock);
+ }
+
+ trusty_enqueue_nop(is->trusty_dev, NULL);
+
+ dev_dbg(is->dev, "%s: irq %d done\n", __func__, irq);
+
+ return IRQ_HANDLED;
+}
+
+static int trusty_irq_cpu_up(unsigned int cpu, struct hlist_node *node)
+{
+ unsigned long irq_flags;
+ struct trusty_irq_state *is;
+
+ is = container_of(node, struct trusty_irq_state, cpuhp_node);
+
+ dev_dbg(is->dev, "%s: cpu %d\n", __func__, cpu);
+
+ local_irq_save(irq_flags);
+ trusty_irq_enable_irqset(is, this_cpu_ptr(is->percpu_irqs));
+ local_irq_restore(irq_flags);
+
+ /*
+ * Temporary workaround blindly enqueuing work to force trusty scheduler
+ * to run after a cpu suspend.
+ * Root causing the workqueue being inappropriately empty
+ * (e.g. loss of an IPI) may make this workaround unnecessary
+ * in the future.
+ */
+ trusty_enqueue_nop(is->trusty_dev, NULL);
+
+ return 0;
+}
+
+static int trusty_irq_cpu_down(unsigned int cpu, struct hlist_node *node)
+{
+ unsigned long irq_flags;
+ struct trusty_irq_state *is;
+
+ is = container_of(node, struct trusty_irq_state, cpuhp_node);
+
+ dev_dbg(is->dev, "%s: cpu %d\n", __func__, cpu);
+
+ local_irq_save(irq_flags);
+ trusty_irq_disable_irqset(is, this_cpu_ptr(is->percpu_irqs));
+ local_irq_restore(irq_flags);
+
+ return 0;
+}
+
+static int trusty_irq_map_ipi(struct trusty_irq_state *is, int irq)
+{
+ int ret;
+ u32 ipi_range[3];
+ struct device_node *gic;
+ struct of_phandle_args oirq = {};
+ u32 beg, end, ipi_base;
+
+ ret = of_property_read_u32_array(is->dev->of_node, "ipi-range",
+ ipi_range, ARRAY_SIZE(ipi_range));
+ if (ret != 0)
+ return -ENODATA;
+ beg = ipi_range[0];
+ end = ipi_range[1];
+ ipi_base = ipi_range[2];
+
+ if (irq < beg || irq > end)
+ return -ENODATA;
+
+ gic = of_irq_find_parent(is->dev->of_node);
+ if (!gic)
+ return -ENXIO;
+
+ oirq.np = gic;
+ oirq.args_count = 1;
+ oirq.args[0] = ipi_base + (irq - beg);
+
+ ret = irq_create_of_mapping(&oirq);
+
+ of_node_put(gic);
+ return (!ret) ? -EINVAL : ret;
+}
+
+static int trusty_irq_create_irq_mapping(struct trusty_irq_state *is, int irq)
+{
+ int ret;
+ int index;
+ u32 irq_pos;
+ u32 templ_idx;
+ u32 range_base;
+ u32 range_end;
+ struct of_phandle_args oirq;
+
+ /* check if this is an IPI (inter-processor interrupt) */
+ ret = trusty_irq_map_ipi(is, irq);
+ if (ret != -ENODATA)
+ return ret;
+
+ /* check if "interrupt-ranges" property is present */
+ if (!of_find_property(is->dev->of_node, "interrupt-ranges", NULL)) {
+ /* fallback to old behavior to be backward compatible with
+ * systems that do not need IRQ domains.
+ */
+ return irq;
+ }
+
+ /* find irq range */
+ for (index = 0;; index += 3) {
+ ret = of_property_read_u32_index(is->dev->of_node,
+ "interrupt-ranges",
+ index, &range_base);
+ if (ret)
+ return ret;
+
+ ret = of_property_read_u32_index(is->dev->of_node,
+ "interrupt-ranges",
+ index + 1, &range_end);
+ if (ret)
+ return ret;
+
+ if (irq >= range_base && irq <= range_end)
+ break;
+ }
+
+ /* read the rest of range entry: template index and irq_pos */
+ ret = of_property_read_u32_index(is->dev->of_node,
+ "interrupt-ranges",
+ index + 2, &templ_idx);
+ if (ret)
+ return ret;
+
+ /* read irq template */
+ ret = of_parse_phandle_with_args(is->dev->of_node,
+ "interrupt-templates",
+ "#interrupt-cells",
+ templ_idx, &oirq);
+ if (ret)
+ return ret;
+
+ WARN_ON(!oirq.np);
+ WARN_ON(!oirq.args_count);
+
+ /*
+ * An IRQ template is a non empty array of u32 values describing group
+ * of interrupts having common properties. The u32 entry with index
+ * zero contains the position of irq_id in interrupt specifier array
+ * followed by data representing interrupt specifier array with irq id
+ * field omitted, so to convert irq template to interrupt specifier
+ * array we have to move down one slot the first irq_pos entries and
+ * replace the resulting gap with real irq id.
+ */
+ irq_pos = oirq.args[0];
+
+ if (irq_pos >= oirq.args_count) {
+ dev_err(is->dev, "irq pos is out of range: %d\n", irq_pos);
+ return -EINVAL;
+ }
+
+ for (index = 1; index <= irq_pos; index++)
+ oirq.args[index - 1] = oirq.args[index];
+
+ oirq.args[irq_pos] = irq - range_base;
+
+ ret = irq_create_of_mapping(&oirq);
+
+ return (!ret) ? -EINVAL : ret;
+}
+
+static int trusty_irq_init_normal_irq(struct trusty_irq_state *is, int tirq)
+{
+ int ret;
+ int irq;
+ unsigned long irq_flags;
+ struct trusty_irq *trusty_irq;
+
+ dev_dbg(is->dev, "%s: irq %d\n", __func__, tirq);
+
+ irq = trusty_irq_create_irq_mapping(is, tirq);
+ if (irq < 0) {
+ dev_err(is->dev,
+ "trusty_irq_create_irq_mapping failed (%d)\n", irq);
+ return irq;
+ }
+
+ trusty_irq = kzalloc(sizeof(*trusty_irq), GFP_KERNEL);
+ if (!trusty_irq)
+ return -ENOMEM;
+
+ trusty_irq->is = is;
+ trusty_irq->irq = irq;
+ trusty_irq->enable = true;
+
+ spin_lock_irqsave(&is->normal_irqs_lock, irq_flags);
+ hlist_add_head(&trusty_irq->node, &is->normal_irqs.inactive);
+ spin_unlock_irqrestore(&is->normal_irqs_lock, irq_flags);
+
+ ret = request_irq(irq, trusty_irq_handler, IRQF_NO_THREAD,
+ "trusty", trusty_irq);
+ if (ret) {
+ dev_err(is->dev, "request_irq failed %d\n", ret);
+ goto err_request_irq;
+ }
+ return 0;
+
+err_request_irq:
+ spin_lock_irqsave(&is->normal_irqs_lock, irq_flags);
+ hlist_del(&trusty_irq->node);
+ spin_unlock_irqrestore(&is->normal_irqs_lock, irq_flags);
+ kfree(trusty_irq);
+ return ret;
+}
+
+static int trusty_irq_init_per_cpu_irq(struct trusty_irq_state *is, int tirq,
+ unsigned int type)
+{
+ int ret;
+ int irq;
+ unsigned int cpu;
+ struct trusty_irq __percpu *trusty_irq_handler_data;
+
+ dev_dbg(is->dev, "%s: irq %d\n", __func__, tirq);
+
+ irq = trusty_irq_create_irq_mapping(is, tirq);
+ if (irq <= 0) {
+ dev_err(is->dev,
+ "trusty_irq_create_irq_mapping failed (%d)\n", irq);
+ return irq;
+ }
+
+ trusty_irq_handler_data = alloc_percpu(struct trusty_irq);
+ if (!trusty_irq_handler_data)
+ return -ENOMEM;
+
+ for_each_possible_cpu(cpu) {
+ struct trusty_irq *trusty_irq;
+ struct trusty_irq_irqset *irqset;
+
+ trusty_irq = per_cpu_ptr(trusty_irq_handler_data, cpu);
+ irqset = per_cpu_ptr(is->percpu_irqs, cpu);
+
+ trusty_irq->is = is;
+ hlist_add_head(&trusty_irq->node, &irqset->inactive);
+ trusty_irq->irq = irq;
+ trusty_irq->percpu = true;
+ trusty_irq->doorbell = type == TRUSTY_IRQ_TYPE_DOORBELL;
+ trusty_irq->percpu_ptr = trusty_irq_handler_data;
+ }
+
+ ret = request_percpu_irq(irq, trusty_irq_handler, "trusty",
+ trusty_irq_handler_data);
+ if (ret) {
+ dev_err(is->dev, "request_percpu_irq failed %d\n", ret);
+ goto err_request_percpu_irq;
+ }
+
+ return 0;
+
+err_request_percpu_irq:
+ for_each_possible_cpu(cpu) {
+ struct trusty_irq *trusty_irq;
+
+ trusty_irq = per_cpu_ptr(trusty_irq_handler_data, cpu);
+ hlist_del(&trusty_irq->node);
+ }
+
+ free_percpu(trusty_irq_handler_data);
+ return ret;
+}
+
+static int trusty_smc_get_next_irq(struct trusty_irq_state *is,
+ unsigned long min_irq, unsigned int type)
+{
+ return trusty_fast_call32(is->trusty_dev, SMC_FC_GET_NEXT_IRQ,
+ min_irq, type, 0);
+}
+
+static int trusty_irq_init_one(struct trusty_irq_state *is,
+ int irq, unsigned int type)
+{
+ int ret;
+
+ irq = trusty_smc_get_next_irq(is, irq, type);
+ if (irq < 0)
+ return irq;
+
+ if (type != TRUSTY_IRQ_TYPE_NORMAL)
+ ret = trusty_irq_init_per_cpu_irq(is, irq, type);
+ else
+ ret = trusty_irq_init_normal_irq(is, irq);
+
+ if (ret) {
+ dev_warn(is->dev,
+ "failed to initialize irq %d, irq will be ignored\n",
+ irq);
+ }
+
+ return irq + 1;
+}
+
+static void trusty_irq_free_irqs(struct trusty_irq_state *is)
+{
+ struct trusty_irq *irq;
+ struct hlist_node *n;
+ unsigned int cpu;
+
+ hlist_for_each_entry_safe(irq, n, &is->normal_irqs.inactive, node) {
+ dev_dbg(is->dev, "%s: irq %d\n", __func__, irq->irq);
+ free_irq(irq->irq, irq);
+ hlist_del(&irq->node);
+ kfree(irq);
+ }
+ hlist_for_each_entry_safe(irq, n,
+ &this_cpu_ptr(is->percpu_irqs)->inactive,
+ node) {
+ struct trusty_irq __percpu *trusty_irq_handler_data;
+
+ dev_dbg(is->dev, "%s: percpu irq %d\n", __func__, irq->irq);
+ trusty_irq_handler_data = irq->percpu_ptr;
+ free_percpu_irq(irq->irq, trusty_irq_handler_data);
+ for_each_possible_cpu(cpu) {
+ struct trusty_irq *irq_tmp;
+
+ irq_tmp = per_cpu_ptr(trusty_irq_handler_data, cpu);
+ hlist_del(&irq_tmp->node);
+ }
+ free_percpu(trusty_irq_handler_data);
+ }
+}
+
+static int trusty_irq_probe(struct platform_device *pdev)
+{
+ int ret;
+ int irq;
+ unsigned long irq_flags;
+ struct trusty_irq_state *is;
+
+ is = kzalloc(sizeof(*is), GFP_KERNEL);
+ if (!is) {
+ ret = -ENOMEM;
+ goto err_alloc_is;
+ }
+
+ is->dev = &pdev->dev;
+ is->trusty_dev = is->dev->parent;
+ spin_lock_init(&is->normal_irqs_lock);
+ is->percpu_irqs = alloc_percpu(struct trusty_irq_irqset);
+ if (!is->percpu_irqs) {
+ ret = -ENOMEM;
+ goto err_alloc_pending_percpu_irqs;
+ }
+
+ platform_set_drvdata(pdev, is);
+
+ is->trusty_call_notifier.notifier_call = trusty_irq_call_notify;
+ ret = trusty_call_notifier_register(is->trusty_dev,
+ &is->trusty_call_notifier);
+ if (ret) {
+ dev_err(&pdev->dev,
+ "failed to register trusty call notifier\n");
+ goto err_trusty_call_notifier_register;
+ }
+
+ for (irq = 0; irq >= 0;)
+ irq = trusty_irq_init_one(is, irq, TRUSTY_IRQ_TYPE_PER_CPU);
+ for (irq = 0; irq >= 0;)
+ irq = trusty_irq_init_one(is, irq, TRUSTY_IRQ_TYPE_NORMAL);
+ for (irq = 0; irq >= 0;)
+ irq = trusty_irq_init_one(is, irq, TRUSTY_IRQ_TYPE_DOORBELL);
+
+ ret = cpuhp_state_add_instance(trusty_irq_cpuhp_slot, &is->cpuhp_node);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "cpuhp_state_add_instance failed %d\n",
+ ret);
+ goto err_add_cpuhp_instance;
+ }
+
+ return 0;
+
+err_add_cpuhp_instance:
+ spin_lock_irqsave(&is->normal_irqs_lock, irq_flags);
+ trusty_irq_disable_irqset(is, &is->normal_irqs);
+ spin_unlock_irqrestore(&is->normal_irqs_lock, irq_flags);
+ trusty_irq_free_irqs(is);
+ trusty_call_notifier_unregister(is->trusty_dev,
+ &is->trusty_call_notifier);
+err_trusty_call_notifier_register:
+ free_percpu(is->percpu_irqs);
+err_alloc_pending_percpu_irqs:
+ kfree(is);
+err_alloc_is:
+ return ret;
+}
+
+static int trusty_irq_remove(struct platform_device *pdev)
+{
+ int ret;
+ unsigned long irq_flags;
+ struct trusty_irq_state *is = platform_get_drvdata(pdev);
+
+ ret = cpuhp_state_remove_instance(trusty_irq_cpuhp_slot,
+ &is->cpuhp_node);
+ if (WARN_ON(ret))
+ return ret;
+
+ spin_lock_irqsave(&is->normal_irqs_lock, irq_flags);
+ trusty_irq_disable_irqset(is, &is->normal_irqs);
+ spin_unlock_irqrestore(&is->normal_irqs_lock, irq_flags);
+
+ trusty_irq_free_irqs(is);
+
+ trusty_call_notifier_unregister(is->trusty_dev,
+ &is->trusty_call_notifier);
+ free_percpu(is->percpu_irqs);
+ kfree(is);
+
+ return 0;
+}
+
+static const struct of_device_id trusty_test_of_match[] = {
+ { .compatible = "android,trusty-irq-v1", },
+ {},
+};
+
+MODULE_DEVICE_TABLE(trusty, trusty_test_of_match);
+
+static struct platform_driver trusty_irq_driver = {
+ .probe = trusty_irq_probe,
+ .remove = trusty_irq_remove,
+ .driver = {
+ .name = "trusty-irq",
+ .of_match_table = trusty_test_of_match,
+ },
+};
+
+static int __init trusty_irq_driver_init(void)
+{
+ int ret;
+
+ /* allocate dynamic cpuhp state slot */
+ ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
+ "trusty-irq:cpu:online",
+ trusty_irq_cpu_up,
+ trusty_irq_cpu_down);
+ if (ret < 0)
+ return ret;
+ trusty_irq_cpuhp_slot = ret;
+
+ /* Register platform driver */
+ ret = platform_driver_register(&trusty_irq_driver);
+ if (ret < 0)
+ goto err_driver_register;
+
+ return ret;
+
+err_driver_register:
+ /* undo cpuhp slot allocation */
+ cpuhp_remove_multi_state(trusty_irq_cpuhp_slot);
+ trusty_irq_cpuhp_slot = -1;
+
+ return ret;
+}
+
+static void __exit trusty_irq_driver_exit(void)
+{
+ platform_driver_unregister(&trusty_irq_driver);
+ cpuhp_remove_multi_state(trusty_irq_cpuhp_slot);
+ trusty_irq_cpuhp_slot = -1;
+}
+
+module_init(trusty_irq_driver_init);
+module_exit(trusty_irq_driver_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Trusty IRQ driver");
diff --git a/drivers/trusty/trusty-log.c b/drivers/trusty/trusty-log.c
new file mode 100644
index 000000000000..7b279fe63766
--- /dev/null
+++ b/drivers/trusty/trusty-log.c
@@ -0,0 +1,830 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2015 Google, Inc.
+ */
+#include <linux/platform_device.h>
+#include <linux/trusty/smcall.h>
+#include <linux/trusty/trusty.h>
+#include <linux/notifier.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/log2.h>
+#include <linux/miscdevice.h>
+#include <linux/poll.h>
+#include <linux/seq_file.h>
+#include <asm/page.h>
+#include "trusty-log.h"
+
+/*
+ * Rationale for the chosen default log buffer size:
+ * - the log buffer shall contain unthrottled Trusty crash dump.
+ * - the register list portion of a crash dump is about 1KB
+ * - the memory-around-registers portion of a crash dump can be up to 12 KB
+ * - an average size backtrace is about 1 KB
+ * - average length of non-crash trusty logs during boot is about 85 characters
+ * - a crash dump with 50 lines of context therefore requires up to 18 KB
+ * - buffer size needs to be power-of-two number of bytes
+ * - rounding up to power of two from 18 KB gives 32 KB
+ * The log size can be adjusted by setting the "trusty_log.log_size" parameter
+ * on the kernel command line. The specified value will be adjusted as needed.
+ */
+
+#define TRUSTY_LOG_DEFAULT_SIZE (32768)
+#define TRUSTY_LOG_MIN_SIZE (PAGE_SIZE / 2)
+#define TRUSTY_LOG_MAX_SIZE (1 * 1024 * 1024 * 1024)
+#define TRUSTY_LINE_BUFFER_SIZE (256)
+
+static size_t log_size_param = TRUSTY_LOG_DEFAULT_SIZE;
+
+static int trusty_log_size_set(const char *val, const struct kernel_param *kp)
+{
+ unsigned long long requested = memparse(val, NULL);
+
+ if (requested < TRUSTY_LOG_MIN_SIZE)
+ requested = TRUSTY_LOG_MIN_SIZE;
+ if (requested > TRUSTY_LOG_MAX_SIZE)
+ requested = TRUSTY_LOG_MAX_SIZE;
+ requested = rounddown_pow_of_two(requested);
+ log_size_param = requested;
+ return 0;
+}
+
+static int trusty_log_size_get(char *buffer, const struct kernel_param *kp)
+{
+ sprintf(buffer, "%zu", log_size_param);
+ return strlen(buffer);
+}
+
+module_param_call(log_size, trusty_log_size_set, trusty_log_size_get, NULL,
+ 0644);
+/*
+ * If we log too much and a UART or other slow source is connected, we can stall
+ * out another thread which is doing printk.
+ *
+ * Trusty crash logs are currently ~16 lines, so 100 should include context and
+ * the crash most of the time.
+ */
+static struct ratelimit_state trusty_log_rate_limit =
+ RATELIMIT_STATE_INIT("trusty_log", 1 * HZ, 100);
+
+/**
+ * struct trusty_log_sfile - trusty log misc device state
+ *
+ * @misc: misc device created for the trusty log virtual file
+ * @device_name: misc device name following the convention
+ * "trusty-<name><id>"
+ */
+struct trusty_log_sfile {
+ struct miscdevice misc;
+ char device_name[64];
+};
+
+/**
+ * struct trusty_log_sink_state - trusty log sink state
+ *
+ * @get: current read unwrapped index
+ * @trusty_panicked: trusty panic status at the start of the sink interation
+ * (only used for kernel log sink)
+ * @sfile: seq_file used for sinking to a virtual file (misc device);
+ * set to NULL for the kernel log sink.
+ * @ignore_overflow: ignore_overflow used to coalesce overflow messages and
+ * avoid reporting an overflow when sinking the oldest
+ * line to the virtual file (only used for virtual file sink)
+ *
+ * A sink state structure is used for both the kernel log sink
+ * and the virtual device sink.
+ * An instance of the sink state structure is dynamically created
+ * for each read iteration of the trusty log virtual file (misc device).
+ *
+ */
+struct trusty_log_sink_state {
+ u32 get;
+ bool trusty_panicked;
+
+ /* virtual file sink specific attributes */
+ struct seq_file *sfile;
+ bool ignore_overflow;
+};
+
+struct trusty_log_state {
+ struct device *dev;
+ struct device *trusty_dev;
+ struct trusty_log_sfile log_sfile;
+
+ struct log_rb *log;
+ struct trusty_log_sink_state klog_sink;
+
+ u32 log_num_pages;
+ struct scatterlist *sg;
+ trusty_shared_mem_id_t log_pages_shared_mem_id;
+
+ struct notifier_block call_notifier;
+ struct notifier_block panic_notifier;
+ char line_buffer[TRUSTY_LINE_BUFFER_SIZE];
+ wait_queue_head_t poll_waiters;
+ /* this lock protects access to wake_put */
+ spinlock_t wake_up_lock;
+ u32 last_wake_put;
+};
+
+static inline u32 u32_add_overflow(u32 a, u32 b)
+{
+ u32 d;
+
+ if (check_add_overflow(a, b, &d)) {
+ /*
+ * silence the overflow,
+ * what matters in the log buffer context
+ * is the casted addition
+ */
+ }
+ return d;
+}
+
+static inline u32 u32_sub_overflow(u32 a, u32 b)
+{
+ u32 d;
+
+ if (check_sub_overflow(a, b, &d)) {
+ /*
+ * silence the overflow,
+ * what matters in the log buffer context
+ * is the casted substraction
+ */
+ }
+ return d;
+}
+
+static int log_read_line(struct trusty_log_state *s, u32 put, u32 get)
+{
+ struct log_rb *log = s->log;
+ int i;
+ char c = '\0';
+ size_t max_to_read =
+ min_t(size_t,
+ u32_sub_overflow(put, get),
+ sizeof(s->line_buffer) - 1);
+ size_t mask = log->sz - 1;
+
+ for (i = 0; i < max_to_read && c != '\n';) {
+ c = log->data[get & mask];
+ s->line_buffer[i++] = c;
+ get = u32_add_overflow(get, 1);
+ }
+ s->line_buffer[i] = '\0';
+
+ return i;
+}
+
+/**
+ * trusty_log_has_data() - returns true when more data is available to sink
+ * @s: Current log state.
+ * @sink: trusty_log_sink_state holding the get index on a given sink
+ *
+ * Return: true if data is available.
+ */
+static bool trusty_log_has_data(struct trusty_log_state *s,
+ struct trusty_log_sink_state *sink)
+{
+ struct log_rb *log = s->log;
+
+ return (log->put != sink->get);
+}
+
+/**
+ * trusty_log_start() - initialize the sink iteration either to kernel log
+ * or to secondary log_sfile
+ * @s: Current log state.
+ * @sink: trusty_log_sink_state holding the get index on a given sink
+ * @index: Unwrapped ring buffer index from where iteration shall start
+ *
+ * Return: 0 if successful, negative error code otherwise
+ */
+static int trusty_log_start(struct trusty_log_state *s,
+ struct trusty_log_sink_state *sink,
+ u32 index)
+{
+ struct log_rb *log;
+
+ if (WARN_ON(!s))
+ return -EINVAL;
+
+ log = s->log;
+ if (WARN_ON(!is_power_of_2(log->sz)))
+ return -EINVAL;
+
+ sink->get = index;
+ return 0;
+}
+
+/**
+ * trusty_log_show() - sink log entry at current iteration
+ * @s: Current log state.
+ * @sink: trusty_log_sink_state holding the get index on a given sink
+ */
+static void trusty_log_show(struct trusty_log_state *s,
+ struct trusty_log_sink_state *sink)
+{
+ struct log_rb *log = s->log;
+ u32 alloc, put, get;
+ int read_chars;
+
+ /*
+ * For this ring buffer, at any given point, alloc >= put >= get.
+ * The producer side of the buffer is not locked, so the put and alloc
+ * pointers must be read in a defined order (put before alloc) so
+ * that the above condition is maintained. A read barrier is needed
+ * to make sure the hardware and compiler keep the reads ordered.
+ */
+ get = sink->get;
+ put = log->put;
+
+ /* Make sure that the read of put occurs before the read of log data */
+ rmb();
+
+ /* Read a line from the log */
+ read_chars = log_read_line(s, put, get);
+
+ /* Force the loads from log_read_line to complete. */
+ rmb();
+ alloc = log->alloc;
+
+ /*
+ * Discard the line that was just read if the data could
+ * have been corrupted by the producer.
+ */
+ if (u32_sub_overflow(alloc, get) > log->sz) {
+ /*
+ * this condition is acceptable in the case of the sfile sink
+ * when attempting to read the oldest entry (at alloc-log->sz)
+ * which may be overrun by a new one when ring buffer write
+ * index wraps around.
+ * So the overrun is not reported in case the oldest line
+ * was being read.
+ */
+ if (sink->sfile) {
+ if (!sink->ignore_overflow)
+ seq_puts(sink->sfile, "log overflow.\n");
+ /* coalesce subsequent contiguous overflows. */
+ sink->ignore_overflow = true;
+ } else {
+ dev_err(s->dev, "log overflow.\n");
+ }
+ sink->get = u32_sub_overflow(alloc, log->sz);
+ return;
+ }
+ /* compute next line index */
+ sink->get = u32_add_overflow(get, read_chars);
+ /* once a line is valid, ignore_overflow must be disabled */
+ sink->ignore_overflow = false;
+ if (sink->sfile) {
+ seq_printf(sink->sfile, "%s", s->line_buffer);
+ } else {
+ if (sink->trusty_panicked ||
+ __ratelimit(&trusty_log_rate_limit)) {
+ dev_info(s->dev, "%s", s->line_buffer);
+ }
+ }
+}
+
+static void *trusty_log_seq_start(struct seq_file *sfile, loff_t *pos)
+{
+ struct trusty_log_sfile *lb;
+ struct trusty_log_state *s;
+ struct log_rb *log;
+ struct trusty_log_sink_state *log_sfile_sink;
+ u32 index;
+ int rc;
+
+ if (WARN_ON(!pos))
+ return ERR_PTR(-EINVAL);
+
+ lb = sfile->private;
+ if (WARN_ON(!lb))
+ return ERR_PTR(-EINVAL);
+
+ log_sfile_sink = kzalloc(sizeof(*log_sfile_sink), GFP_KERNEL);
+ if (!log_sfile_sink)
+ return ERR_PTR(-ENOMEM);
+
+ s = container_of(lb, struct trusty_log_state, log_sfile);
+ log_sfile_sink->sfile = sfile;
+ log = s->log;
+ if (*pos == 0) {
+ /* start at the oldest line */
+ index = 0;
+ if (log->alloc > log->sz)
+ index = u32_sub_overflow(log->alloc, log->sz);
+ } else {
+ /*
+ * '*pos>0': pos hold the 32bits unwrapped index from where
+ * to start iterating
+ */
+ index = (u32)*pos;
+ }
+ pr_debug("%s start=%u\n", __func__, index);
+
+ log_sfile_sink->ignore_overflow = true;
+ rc = trusty_log_start(s, log_sfile_sink, index);
+ if (rc < 0)
+ goto free_sink;
+
+ if (!trusty_log_has_data(s, log_sfile_sink))
+ goto free_sink;
+
+ return log_sfile_sink;
+
+free_sink:
+ pr_debug("%s kfree\n", __func__);
+ kfree(log_sfile_sink);
+ return rc < 0 ? ERR_PTR(rc) : NULL;
+}
+
+static void *trusty_log_seq_next(struct seq_file *sfile, void *v, loff_t *pos)
+{
+ struct trusty_log_sfile *lb;
+ struct trusty_log_state *s;
+ struct trusty_log_sink_state *log_sfile_sink = v;
+ int rc = 0;
+
+ if (WARN_ON(!log_sfile_sink))
+ return ERR_PTR(-EINVAL);
+
+ lb = sfile->private;
+ if (WARN_ON(!lb)) {
+ rc = -EINVAL;
+ goto end_of_iter;
+ }
+ s = container_of(lb, struct trusty_log_state, log_sfile);
+
+ if (WARN_ON(!pos)) {
+ rc = -EINVAL;
+ goto end_of_iter;
+ }
+ /*
+ * When starting a virtual file sink, the start function is invoked
+ * with a pos argument which value is set to zero.
+ * Subsequent starts are invoked with pos being set to
+ * the unwrapped read index (get).
+ * Upon u32 wraparound, the get index could be reset to zero.
+ * Thus a msb is used to distinguish the `get` zero value
+ * from the `start of file` zero value.
+ */
+ *pos = (1UL << 32) + log_sfile_sink->get;
+ if (!trusty_log_has_data(s, log_sfile_sink))
+ goto end_of_iter;
+
+ return log_sfile_sink;
+
+end_of_iter:
+ pr_debug("%s kfree\n", __func__);
+ kfree(log_sfile_sink);
+ return rc < 0 ? ERR_PTR(rc) : NULL;
+}
+
+static void trusty_log_seq_stop(struct seq_file *sfile, void *v)
+{
+ /*
+ * When iteration completes or on error, the next callback frees
+ * the sink structure and returns NULL/error-code.
+ * In that case stop (being invoked with void* v set to the last next
+ * return value) would be invoked with v == NULL or error code.
+ * When user space stops the iteration earlier than the end
+ * (in case of user-space memory allocation limit for example)
+ * then the stop function receives a non NULL get pointer
+ * and is in charge or freeing the sink structure.
+ */
+ struct trusty_log_sink_state *log_sfile_sink = v;
+
+ /* nothing to do - sink structure already freed */
+ if (IS_ERR_OR_NULL(log_sfile_sink))
+ return;
+
+ kfree(log_sfile_sink);
+
+ pr_debug("%s kfree\n", __func__);
+}
+
+static int trusty_log_seq_show(struct seq_file *sfile, void *v)
+{
+ struct trusty_log_sfile *lb;
+ struct trusty_log_state *s;
+ struct trusty_log_sink_state *log_sfile_sink = v;
+
+ if (WARN_ON(!log_sfile_sink))
+ return -EINVAL;
+
+ lb = sfile->private;
+ if (WARN_ON(!lb))
+ return -EINVAL;
+
+ s = container_of(lb, struct trusty_log_state, log_sfile);
+
+ trusty_log_show(s, log_sfile_sink);
+ return 0;
+}
+
+static void trusty_dump_logs(struct trusty_log_state *s)
+{
+ int rc;
+ /*
+ * note: klog_sink.get initialized to zero by kzalloc
+ */
+ s->klog_sink.trusty_panicked = trusty_get_panic_status(s->trusty_dev);
+
+ rc = trusty_log_start(s, &s->klog_sink, s->klog_sink.get);
+ if (rc < 0)
+ return;
+
+ while (trusty_log_has_data(s, &s->klog_sink))
+ trusty_log_show(s, &s->klog_sink);
+}
+
+static int trusty_log_call_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct trusty_log_state *s;
+ unsigned long flags;
+ u32 cur_put;
+
+ if (action != TRUSTY_CALL_RETURNED)
+ return NOTIFY_DONE;
+
+ s = container_of(nb, struct trusty_log_state, call_notifier);
+ spin_lock_irqsave(&s->wake_up_lock, flags);
+ cur_put = s->log->put;
+ if (cur_put != s->last_wake_put) {
+ s->last_wake_put = cur_put;
+ wake_up_all(&s->poll_waiters);
+ }
+ spin_unlock_irqrestore(&s->wake_up_lock, flags);
+ return NOTIFY_OK;
+}
+
+static int trusty_log_panic_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct trusty_log_state *s;
+
+ /*
+ * Don't grab the spin lock to hold up the panic notifier, even
+ * though this is racy.
+ */
+ s = container_of(nb, struct trusty_log_state, panic_notifier);
+ dev_info(s->dev, "panic notifier - trusty version %s",
+ trusty_version_str_get(s->trusty_dev));
+ trusty_dump_logs(s);
+ return NOTIFY_OK;
+}
+
+const struct seq_operations trusty_log_seq_ops = {
+ .start = trusty_log_seq_start,
+ .stop = trusty_log_seq_stop,
+ .next = trusty_log_seq_next,
+ .show = trusty_log_seq_show,
+};
+
+static int trusty_log_sfile_dev_open(struct inode *inode, struct file *file)
+{
+ struct trusty_log_sfile *ls;
+ struct seq_file *sfile;
+ int rc;
+
+ /*
+ * file->private_data contains a pointer to the misc_device struct
+ * passed to misc_register()
+ */
+ if (WARN_ON(!file->private_data))
+ return -EINVAL;
+
+ ls = container_of(file->private_data, struct trusty_log_sfile, misc);
+
+ /*
+ * seq_open uses file->private_data to store the seq_file associated
+ * with the struct file, but it must be NULL when seq_open is called
+ */
+ file->private_data = NULL;
+ rc = seq_open(file, &trusty_log_seq_ops);
+ if (rc < 0)
+ return rc;
+
+ sfile = file->private_data;
+ if (WARN_ON(!sfile))
+ return -EINVAL;
+
+ sfile->private = ls;
+ return 0;
+}
+
+static unsigned int trusty_log_sfile_dev_poll(struct file *filp,
+ struct poll_table_struct *wait)
+{
+ struct seq_file *sfile;
+ struct trusty_log_sfile *lb;
+ struct trusty_log_state *s;
+ struct log_rb *log;
+
+ /*
+ * trusty_log_sfile_dev_open() pointed filp->private_data to a
+ * seq_file, and that seq_file->private to the trusty_log_sfile
+ * field of a trusty_log_state
+ */
+ sfile = filp->private_data;
+ lb = sfile->private;
+ s = container_of(lb, struct trusty_log_state, log_sfile);
+ poll_wait(filp, &s->poll_waiters, wait);
+ log = s->log;
+
+ /*
+ * Userspace has read up to filp->f_pos so far. Update klog_sink
+ * to indicate that, so that we don't end up dumping the entire
+ * Trusty log in case of panic.
+ */
+ s->klog_sink.get = (u32)filp->f_pos;
+
+ if (log->put != (u32)filp->f_pos) {
+ /* data ready to read */
+ return EPOLLIN | EPOLLRDNORM;
+ }
+ /* no data available, go to sleep */
+ return 0;
+}
+
+static const struct file_operations log_sfile_dev_operations = {
+ .owner = THIS_MODULE,
+ .open = trusty_log_sfile_dev_open,
+ .poll = trusty_log_sfile_dev_poll,
+ .read = seq_read,
+ .release = seq_release,
+};
+
+static int trusty_log_sfile_register(struct trusty_log_state *s)
+{
+ int ret;
+ struct trusty_log_sfile *ls = &s->log_sfile;
+
+ if (WARN_ON(!ls))
+ return -EINVAL;
+
+ snprintf(ls->device_name, sizeof(ls->device_name),
+ "trusty-log%d", s->dev->id);
+ ls->misc.minor = MISC_DYNAMIC_MINOR;
+ ls->misc.name = ls->device_name;
+ ls->misc.fops = &log_sfile_dev_operations;
+
+ ret = misc_register(&ls->misc);
+ if (ret) {
+ dev_err(s->dev,
+ "log_sfile error while doing misc_register ret=%d\n",
+ ret);
+ return ret;
+ }
+ dev_info(s->dev, "/dev/%s registered\n",
+ ls->device_name);
+ return 0;
+}
+
+static void trusty_log_sfile_unregister(struct trusty_log_state *s)
+{
+ struct trusty_log_sfile *ls = &s->log_sfile;
+
+ misc_deregister(&ls->misc);
+ if (s->dev) {
+ dev_info(s->dev, "/dev/%s unregistered\n",
+ ls->misc.name);
+ }
+}
+
+static bool trusty_supports_logging(struct device *device)
+{
+ int result;
+
+ result = trusty_std_call32(device, SMC_SC_SHARED_LOG_VERSION,
+ TRUSTY_LOG_API_VERSION, 0, 0);
+ if (result == SM_ERR_UNDEFINED_SMC) {
+ dev_info(device, "trusty-log not supported on secure side.\n");
+ return false;
+ } else if (result < 0) {
+ dev_err(device,
+ "trusty std call (SMC_SC_SHARED_LOG_VERSION) failed: %d\n",
+ result);
+ return false;
+ }
+
+ if (result != TRUSTY_LOG_API_VERSION) {
+ dev_info(device, "unsupported api version: %d, supported: %d\n",
+ result, TRUSTY_LOG_API_VERSION);
+ return false;
+ }
+ return true;
+}
+
+static int trusty_log_init(struct platform_device *pdev)
+{
+ struct trusty_log_state *s;
+ struct scatterlist *sg;
+ unsigned char *mem;
+ int i;
+ int result;
+ trusty_shared_mem_id_t mem_id;
+ int log_size;
+
+ s = kzalloc(sizeof(*s), GFP_KERNEL);
+ if (!s) {
+ result = -ENOMEM;
+ goto error_alloc_state;
+ }
+
+ s->dev = &pdev->dev;
+ s->trusty_dev = s->dev->parent;
+
+ s->log_num_pages = DIV_ROUND_UP(log_size_param + sizeof(struct log_rb),
+ PAGE_SIZE);
+ s->sg = kcalloc(s->log_num_pages, sizeof(*s->sg), GFP_KERNEL);
+ if (!s->sg) {
+ result = -ENOMEM;
+ goto error_alloc_sg;
+ }
+
+ log_size = s->log_num_pages * PAGE_SIZE;
+ mem = vzalloc(log_size);
+ if (!mem) {
+ result = -ENOMEM;
+ goto error_alloc_log;
+ }
+
+ s->log = (struct log_rb *)mem;
+
+ sg_init_table(s->sg, s->log_num_pages);
+ for_each_sg(s->sg, sg, s->log_num_pages, i) {
+ struct page *pg = vmalloc_to_page(mem + (i * PAGE_SIZE));
+
+ if (!pg) {
+ result = -ENOMEM;
+ goto err_share_memory;
+ }
+ sg_set_page(sg, pg, PAGE_SIZE, 0);
+ }
+ /*
+ * This will fail for Trusty api version < TRUSTY_API_VERSION_MEM_OBJ
+ * if s->log_num_pages > 1
+ * Use trusty_share_memory_compat instead of trusty_share_memory in case
+ * s->log_num_pages == 1 and api version < TRUSTY_API_VERSION_MEM_OBJ,
+ * In that case SMC_SC_SHARED_LOG_ADD expects a different value than
+ * what trusty_share_memory returns
+ */
+ result = trusty_share_memory_compat(s->trusty_dev, &mem_id, s->sg,
+ s->log_num_pages, PAGE_KERNEL);
+ if (result) {
+ dev_err(s->dev, "trusty_share_memory failed: %d\n", result);
+ goto err_share_memory;
+ }
+ s->log_pages_shared_mem_id = mem_id;
+
+ result = trusty_std_call32(s->trusty_dev,
+ SMC_SC_SHARED_LOG_ADD,
+ (u32)(mem_id), (u32)(mem_id >> 32),
+ log_size);
+ if (result < 0) {
+ dev_err(s->dev,
+ "trusty std call (SMC_SC_SHARED_LOG_ADD) failed: %d 0x%llx\n",
+ result, mem_id);
+ goto error_std_call;
+ }
+
+ init_waitqueue_head(&s->poll_waiters);
+ spin_lock_init(&s->wake_up_lock);
+
+ s->call_notifier.notifier_call = trusty_log_call_notify;
+ result = trusty_call_notifier_register(s->trusty_dev,
+ &s->call_notifier);
+ if (result < 0) {
+ dev_err(&pdev->dev,
+ "failed to register trusty call notifier\n");
+ goto error_call_notifier;
+ }
+
+ s->panic_notifier.notifier_call = trusty_log_panic_notify;
+ result = atomic_notifier_chain_register(&panic_notifier_list,
+ &s->panic_notifier);
+ if (result < 0) {
+ dev_err(&pdev->dev,
+ "failed to register panic notifier\n");
+ goto error_panic_notifier;
+ }
+
+ result = trusty_log_sfile_register(s);
+ if (result < 0) {
+ dev_err(&pdev->dev, "failed to register log_sfile\n");
+ goto error_log_sfile;
+ }
+
+ platform_set_drvdata(pdev, s);
+
+ return 0;
+
+error_log_sfile:
+ atomic_notifier_chain_unregister(&panic_notifier_list,
+ &s->panic_notifier);
+error_panic_notifier:
+ trusty_call_notifier_unregister(s->trusty_dev, &s->call_notifier);
+error_call_notifier:
+ trusty_std_call32(s->trusty_dev, SMC_SC_SHARED_LOG_RM,
+ (u32)mem_id, (u32)(mem_id >> 32), 0);
+error_std_call:
+ if (WARN_ON(trusty_reclaim_memory(s->trusty_dev, mem_id, s->sg,
+ s->log_num_pages))) {
+ dev_err(&pdev->dev, "trusty_revoke_memory failed: %d 0x%llx\n",
+ result, mem_id);
+ /*
+ * It is not safe to free this memory if trusty_revoke_memory
+ * fails. Leak it in that case.
+ */
+ } else {
+err_share_memory:
+ vfree(s->log);
+ }
+error_alloc_log:
+ kfree(s->sg);
+error_alloc_sg:
+ kfree(s);
+error_alloc_state:
+ return result;
+}
+
+static int trusty_log_probe(struct platform_device *pdev)
+{
+ int rc;
+
+ if (!trusty_supports_logging(pdev->dev.parent))
+ return -ENXIO;
+
+ rc = trusty_log_init(pdev);
+ if (rc && log_size_param > TRUSTY_LOG_MIN_SIZE) {
+ dev_warn(&pdev->dev, "init failed, retrying with 1-page log\n");
+ log_size_param = TRUSTY_LOG_MIN_SIZE;
+ rc = trusty_log_init(pdev);
+ }
+ return rc;
+}
+
+static int trusty_log_remove(struct platform_device *pdev)
+{
+ int result;
+ struct trusty_log_state *s = platform_get_drvdata(pdev);
+ trusty_shared_mem_id_t mem_id = s->log_pages_shared_mem_id;
+
+ trusty_log_sfile_unregister(s);
+ atomic_notifier_chain_unregister(&panic_notifier_list,
+ &s->panic_notifier);
+ trusty_call_notifier_unregister(s->trusty_dev, &s->call_notifier);
+
+ result = trusty_std_call32(s->trusty_dev, SMC_SC_SHARED_LOG_RM,
+ (u32)mem_id, (u32)(mem_id >> 32), 0);
+ if (result) {
+ dev_err(&pdev->dev,
+ "trusty std call (SMC_SC_SHARED_LOG_RM) failed: %d\n",
+ result);
+ }
+ result = trusty_reclaim_memory(s->trusty_dev, mem_id, s->sg,
+ s->log_num_pages);
+ if (WARN_ON(result)) {
+ dev_err(&pdev->dev,
+ "trusty failed to remove shared memory: %d\n", result);
+ } else {
+ /*
+ * It is not safe to free this memory if trusty_revoke_memory
+ * fails. Leak it in that case.
+ */
+ vfree(s->log);
+ }
+ kfree(s->sg);
+ kfree(s);
+
+ return 0;
+}
+
+static const struct of_device_id trusty_test_of_match[] = {
+ { .compatible = "android,trusty-log-v1", },
+ {},
+};
+
+MODULE_DEVICE_TABLE(trusty, trusty_test_of_match);
+
+static struct platform_driver trusty_log_driver = {
+ .probe = trusty_log_probe,
+ .remove = trusty_log_remove,
+ .driver = {
+ .name = "trusty-log",
+ .of_match_table = trusty_test_of_match,
+ },
+};
+
+module_platform_driver(trusty_log_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Trusty logging driver");
diff --git a/drivers/trusty/trusty-log.h b/drivers/trusty/trusty-log.h
new file mode 100644
index 000000000000..7b5e6096b51e
--- /dev/null
+++ b/drivers/trusty/trusty-log.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright (c) 2015 Google, Inc.
+ *
+ * Trusty also has a copy of this header. Please keep the copies in sync.
+ */
+#ifndef _TRUSTY_LOG_H_
+#define _TRUSTY_LOG_H_
+
+/*
+ * Ring buffer that supports one secure producer thread and one
+ * linux side consumer thread.
+ */
+struct log_rb {
+ volatile uint32_t alloc;
+ volatile uint32_t put;
+ uint32_t sz;
+ volatile char data[];
+} __packed;
+
+#define SMC_SC_SHARED_LOG_VERSION SMC_STDCALL_NR(SMC_ENTITY_LOGGING, 0)
+#define SMC_SC_SHARED_LOG_ADD SMC_STDCALL_NR(SMC_ENTITY_LOGGING, 1)
+#define SMC_SC_SHARED_LOG_RM SMC_STDCALL_NR(SMC_ENTITY_LOGGING, 2)
+
+#define TRUSTY_LOG_API_VERSION 1
+
+#endif
+
diff --git a/drivers/trusty/trusty-mem.c b/drivers/trusty/trusty-mem.c
new file mode 100644
index 000000000000..8a360298e501
--- /dev/null
+++ b/drivers/trusty/trusty-mem.c
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2015 Google, Inc.
+ */
+
+#include <linux/types.h>
+#include <linux/printk.h>
+#include <linux/trusty/arm_ffa.h>
+#include <linux/trusty/trusty.h>
+#include <linux/trusty/smcall.h>
+
+#define MEM_ATTR_STRONGLY_ORDERED (0x00U)
+#define MEM_ATTR_DEVICE (0x04U)
+#define MEM_ATTR_NORMAL_NON_CACHEABLE (0x44U)
+#define MEM_ATTR_NORMAL_WRITE_THROUGH (0xAAU)
+#define MEM_ATTR_NORMAL_WRITE_BACK_READ_ALLOCATE (0xEEU)
+#define MEM_ATTR_NORMAL_WRITE_BACK_WRITE_ALLOCATE (0xFFU)
+
+#define ATTR_RDONLY (1U << 7)
+#define ATTR_INNER_SHAREABLE (3U << 8)
+
+static int get_mem_attr(struct page *page, pgprot_t pgprot)
+{
+#if defined(CONFIG_ARM64)
+ u64 mair;
+ unsigned int attr_index = (pgprot_val(pgprot) & PTE_ATTRINDX_MASK) >> 2;
+
+ asm ("mrs %0, mair_el1\n" : "=&r" (mair));
+ return (mair >> (attr_index * 8)) & 0xff;
+
+#elif defined(CONFIG_ARM_LPAE)
+ u32 mair;
+ unsigned int attr_index = ((pgprot_val(pgprot) & L_PTE_MT_MASK) >> 2);
+
+ if (attr_index >= 4) {
+ attr_index -= 4;
+ asm volatile("mrc p15, 0, %0, c10, c2, 1\n" : "=&r" (mair));
+ } else {
+ asm volatile("mrc p15, 0, %0, c10, c2, 0\n" : "=&r" (mair));
+ }
+ return (mair >> (attr_index * 8)) & 0xff;
+
+#elif defined(CONFIG_ARM)
+ /* check memory type */
+ switch (pgprot_val(pgprot) & L_PTE_MT_MASK) {
+ case L_PTE_MT_WRITEALLOC:
+ return MEM_ATTR_NORMAL_WRITE_BACK_WRITE_ALLOCATE;
+
+ case L_PTE_MT_BUFFERABLE:
+ return MEM_ATTR_NORMAL_NON_CACHEABLE;
+
+ case L_PTE_MT_WRITEBACK:
+ return MEM_ATTR_NORMAL_WRITE_BACK_READ_ALLOCATE;
+
+ case L_PTE_MT_WRITETHROUGH:
+ return MEM_ATTR_NORMAL_WRITE_THROUGH;
+
+ case L_PTE_MT_UNCACHED:
+ return MEM_ATTR_STRONGLY_ORDERED;
+
+ case L_PTE_MT_DEV_SHARED:
+ case L_PTE_MT_DEV_NONSHARED:
+ return MEM_ATTR_DEVICE;
+
+ default:
+ return -EINVAL;
+ }
+#else
+ return 0;
+#endif
+}
+
+int trusty_encode_page_info(struct ns_mem_page_info *inf,
+ struct page *page, pgprot_t pgprot)
+{
+ int mem_attr;
+ u64 pte;
+ u8 ffa_mem_attr;
+ u8 ffa_mem_perm = 0;
+
+ if (!inf || !page)
+ return -EINVAL;
+
+ /* get physical address */
+ pte = (u64)page_to_phys(page);
+
+ /* get memory attributes */
+ mem_attr = get_mem_attr(page, pgprot);
+ if (mem_attr < 0)
+ return mem_attr;
+
+ switch (mem_attr) {
+ case MEM_ATTR_STRONGLY_ORDERED:
+ ffa_mem_attr = FFA_MEM_ATTR_DEVICE_NGNRNE;
+ break;
+
+ case MEM_ATTR_DEVICE:
+ ffa_mem_attr = FFA_MEM_ATTR_DEVICE_NGNRE;
+ break;
+
+ case MEM_ATTR_NORMAL_NON_CACHEABLE:
+ ffa_mem_attr = FFA_MEM_ATTR_NORMAL_MEMORY_UNCACHED;
+ break;
+
+ case MEM_ATTR_NORMAL_WRITE_BACK_READ_ALLOCATE:
+ case MEM_ATTR_NORMAL_WRITE_BACK_WRITE_ALLOCATE:
+ ffa_mem_attr = FFA_MEM_ATTR_NORMAL_MEMORY_CACHED_WB;
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ inf->paddr = pte;
+
+ /* add other attributes */
+#if defined(CONFIG_ARM64) || defined(CONFIG_ARM_LPAE)
+ pte |= pgprot_val(pgprot);
+#elif defined(CONFIG_ARM)
+ if (pgprot_val(pgprot) & L_PTE_RDONLY)
+ pte |= ATTR_RDONLY;
+ if (pgprot_val(pgprot) & L_PTE_SHARED)
+ pte |= ATTR_INNER_SHAREABLE; /* inner sharable */
+#endif
+
+ if (!(pte & ATTR_RDONLY))
+ ffa_mem_perm |= FFA_MEM_PERM_RW;
+ else
+ ffa_mem_perm |= FFA_MEM_PERM_RO;
+
+ if ((pte & ATTR_INNER_SHAREABLE) == ATTR_INNER_SHAREABLE)
+ ffa_mem_attr |= FFA_MEM_ATTR_INNER_SHAREABLE;
+
+ inf->ffa_mem_attr = ffa_mem_attr;
+ inf->ffa_mem_perm = ffa_mem_perm;
+ inf->compat_attr = (pte & 0x0000FFFFFFFFFFFFull) |
+ ((u64)mem_attr << 48);
+ return 0;
+}
diff --git a/drivers/trusty/trusty-smc-arm.S b/drivers/trusty/trusty-smc-arm.S
new file mode 100644
index 000000000000..8ff83547d33f
--- /dev/null
+++ b/drivers/trusty/trusty-smc-arm.S
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020 Google, Inc.
+ */
+
+#include <linux/linkage.h>
+
+.arch_extension sec
+
+ENTRY(trusty_smc8)
+ /* Save stack location where r3-r7 smc arguments are stored */
+ mov r12, sp
+
+ /* Save original r4-r7 values as caller expects these to be preserved */
+ push {r4-r7}
+
+ /* Save return value pointer and return address */
+ push {r0, lr}
+
+ /* arm abi shifts arguments when returning a struct, shift them back */
+ mov r0, r1
+ mov r1, r2
+ mov r2, r3
+
+ /* Load stack based arguments */
+ ldmia r12, {r3-r7}
+
+ smc #0
+
+ /* Restore return address and get return value pointer */
+ pop {r12, lr}
+
+ /* Copy 8-register smc return value to struct smc_ret8 return value */
+ stmia r12, {r0-r7}
+
+ /* Restore original r4-r7 values */
+ pop {r4-r7}
+
+ /* Return */
+ bx lr
+ENDPROC(trusty_smc8)
diff --git a/drivers/trusty/trusty-smc-arm64.S b/drivers/trusty/trusty-smc-arm64.S
new file mode 100644
index 000000000000..14c8fed28a5e
--- /dev/null
+++ b/drivers/trusty/trusty-smc-arm64.S
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020 Google, Inc.
+ */
+
+#include <linux/linkage.h>
+
+.macro push ra, rb
+stp \ra, \rb, [sp,#-16]!
+.endm
+
+.macro pop ra, rb
+ldp \ra, \rb, [sp], #16
+.endm
+
+lr .req x30
+
+SYM_FUNC_START(trusty_smc8)
+ /*
+ * Save x8 (return value ptr) and lr. The SMC calling convention says el3
+ * does not need to preserve x8. The normal ABI does not require either x8
+ * or lr to be preserved.
+ */
+ push x8, lr
+ smc #0
+ pop x8, lr
+
+ /* Copy 8-register smc return value to struct smc_ret8 return value */
+ stp x0, x1, [x8], #16
+ stp x2, x3, [x8], #16
+ stp x4, x5, [x8], #16
+ stp x6, x7, [x8], #16
+
+ ret
+SYM_FUNC_END(trusty_smc8)
diff --git a/drivers/trusty/trusty-smc.h b/drivers/trusty/trusty-smc.h
new file mode 100644
index 000000000000..b53e5abb4d05
--- /dev/null
+++ b/drivers/trusty/trusty-smc.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020 Google, Inc.
+ */
+#ifndef _TRUSTY_SMC_H
+#define _TRUSTY_SMC_H
+
+#include <linux/types.h>
+
+struct smc_ret8 {
+ unsigned long r0;
+ unsigned long r1;
+ unsigned long r2;
+ unsigned long r3;
+ unsigned long r4;
+ unsigned long r5;
+ unsigned long r6;
+ unsigned long r7;
+};
+
+struct smc_ret8 trusty_smc8(unsigned long r0, unsigned long r1,
+ unsigned long r2, unsigned long r3,
+ unsigned long r4, unsigned long r5,
+ unsigned long r6, unsigned long r7);
+
+#endif /* _TRUSTY_SMC_H */
diff --git a/drivers/trusty/trusty-test.c b/drivers/trusty/trusty-test.c
new file mode 100644
index 000000000000..844868981fa5
--- /dev/null
+++ b/drivers/trusty/trusty-test.c
@@ -0,0 +1,440 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2020 Google, Inc.
+ */
+
+#include <linux/ctype.h>
+#include <linux/list.h>
+#include <linux/platform_device.h>
+#include <linux/trusty/smcall.h>
+#include <linux/trusty/trusty.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+
+#include "trusty-test.h"
+
+struct trusty_test_state {
+ struct device *dev;
+ struct device *trusty_dev;
+};
+
+struct trusty_test_shmem_obj {
+ struct list_head node;
+ size_t page_count;
+ struct page **pages;
+ void *buf;
+ struct sg_table sgt;
+ trusty_shared_mem_id_t mem_id;
+};
+
+/*
+ * Allocate a test object with @page_count number of pages, map it and add it to
+ * @list.
+ * For multi-page allocations, order the pages so they are not contiguous.
+ */
+static int trusty_test_alloc_obj(struct trusty_test_state *s,
+ size_t page_count,
+ struct list_head *list)
+{
+ size_t i;
+ int ret = -ENOMEM;
+ struct trusty_test_shmem_obj *obj;
+
+ obj = kzalloc(sizeof(*obj), GFP_KERNEL);
+ if (!obj)
+ goto err_alloc_obj;
+ obj->page_count = page_count;
+
+ obj->pages = kmalloc_array(page_count, sizeof(*obj->pages), GFP_KERNEL);
+ if (!obj->pages) {
+ ret = -ENOMEM;
+ dev_err(s->dev, "failed to allocate page array, count %zd\n",
+ page_count);
+ goto err_alloc_pages;
+ }
+
+ for (i = 0; i < page_count; i++) {
+ obj->pages[i] = alloc_page(GFP_KERNEL);
+ if (!obj->pages[i]) {
+ ret = -ENOMEM;
+ dev_err(s->dev, "failed to allocate page %zd/%zd\n",
+ i, page_count);
+ goto err_alloc_page;
+ }
+ if (i > 0 && obj->pages[i - 1] + 1 == obj->pages[i]) {
+ /* swap adacent pages to increase fragmentation */
+ swap(obj->pages[i - 1], obj->pages[i]);
+ }
+ }
+
+ obj->buf = vmap(obj->pages, page_count, VM_MAP, PAGE_KERNEL);
+ if (!obj->buf) {
+ ret = -ENOMEM;
+ dev_err(s->dev, "failed to map test buffer page count %zd\n",
+ page_count);
+ goto err_map_pages;
+ }
+
+ ret = sg_alloc_table_from_pages(&obj->sgt, obj->pages, page_count,
+ 0, page_count * PAGE_SIZE, GFP_KERNEL);
+ if (ret) {
+ dev_err(s->dev, "sg_alloc_table_from_pages failed: %d\n", ret);
+ goto err_alloc_sgt;
+ }
+ list_add_tail(&obj->node, list);
+ dev_dbg(s->dev, "buffer has %d page runs\n", obj->sgt.nents);
+ return 0;
+
+err_alloc_sgt:
+ vunmap(obj->buf);
+err_map_pages:
+ for (i = page_count; i > 0; i--) {
+ __free_page(obj->pages[i - 1]);
+err_alloc_page:
+ ;
+ }
+ kfree(obj->pages);
+err_alloc_pages:
+ kfree(obj);
+err_alloc_obj:
+ return ret;
+}
+
+/* Unlink, unmap and free a test object and its pages */
+static void trusty_test_free_obj(struct trusty_test_state *s,
+ struct trusty_test_shmem_obj *obj)
+{
+ size_t i;
+
+ list_del(&obj->node);
+ sg_free_table(&obj->sgt);
+ vunmap(obj->buf);
+ for (i = obj->page_count; i > 0; i--)
+ __free_page(obj->pages[i - 1]);
+ kfree(obj->pages);
+ kfree(obj);
+}
+
+/*
+ * Share all the pages of all the test object in &obj_list.
+ * If sharing a test object fails, free it so that every test object that
+ * remains in @obj_list has been shared when this function returns.
+ * Return a error if any test object failed to be shared.
+ */
+static int trusty_test_share_objs(struct trusty_test_state *s,
+ struct list_head *obj_list, size_t size)
+{
+ int ret = 0;
+ int tmpret;
+ struct trusty_test_shmem_obj *obj;
+ struct trusty_test_shmem_obj *next_obj;
+ ktime_t t1;
+ ktime_t t2;
+
+ list_for_each_entry_safe(obj, next_obj, obj_list, node) {
+ t1 = ktime_get();
+ tmpret = trusty_share_memory(s->trusty_dev, &obj->mem_id,
+ obj->sgt.sgl, obj->sgt.nents,
+ PAGE_KERNEL);
+ t2 = ktime_get();
+ if (tmpret) {
+ ret = tmpret;
+ dev_err(s->dev,
+ "trusty_share_memory failed: %d, size=%zd\n",
+ ret, size);
+
+ /*
+ * Free obj and continue, so we can revoke the
+ * whole list in trusty_test_reclaim_objs.
+ */
+ trusty_test_free_obj(s, obj);
+ }
+ dev_dbg(s->dev, "share id=0x%llx, size=%zu took %lld ns\n",
+ obj->mem_id, size,
+ ktime_to_ns(ktime_sub(t2, t1)));
+ }
+
+ return ret;
+}
+
+/* Reclaim memory shared with trusty for all test objects in @obj_list. */
+static int trusty_test_reclaim_objs(struct trusty_test_state *s,
+ struct list_head *obj_list, size_t size)
+{
+ int ret = 0;
+ int tmpret;
+ struct trusty_test_shmem_obj *obj;
+ struct trusty_test_shmem_obj *next_obj;
+ ktime_t t1;
+ ktime_t t2;
+
+ list_for_each_entry_safe(obj, next_obj, obj_list, node) {
+ t1 = ktime_get();
+ tmpret = trusty_reclaim_memory(s->trusty_dev, obj->mem_id,
+ obj->sgt.sgl, obj->sgt.nents);
+ t2 = ktime_get();
+ if (tmpret) {
+ ret = tmpret;
+ dev_err(s->dev,
+ "trusty_reclaim_memory failed: %d, id=0x%llx\n",
+ ret, obj->mem_id);
+
+ /*
+ * It is not safe to free this memory if
+ * trusty_reclaim_memory fails. Leak it in that
+ * case.
+ */
+ list_del(&obj->node);
+ }
+ dev_dbg(s->dev, "revoke id=0x%llx, size=%zu took %lld ns\n",
+ obj->mem_id, size,
+ ktime_to_ns(ktime_sub(t2, t1)));
+ }
+
+ return ret;
+}
+
+/*
+ * Test a test object. First, initialize the memory, then make a std call into
+ * trusty which will read it and return an error if the initialized value does
+ * not match what it expects. If trusty reads the correct values, it will modify
+ * the memory and return 0. This function then checks that it can read the
+ * correct modified value.
+ */
+static int trusty_test_rw(struct trusty_test_state *s,
+ struct trusty_test_shmem_obj *obj)
+{
+ size_t size = obj->page_count * PAGE_SIZE;
+ int ret;
+ size_t i;
+ u64 *buf = obj->buf;
+ ktime_t t1;
+ ktime_t t2;
+
+ for (i = 0; i < size / sizeof(*buf); i++)
+ buf[i] = i;
+
+ t1 = ktime_get();
+ ret = trusty_std_call32(s->trusty_dev, SMC_SC_TEST_SHARED_MEM_RW,
+ (u32)(obj->mem_id), (u32)(obj->mem_id >> 32),
+ size);
+ t2 = ktime_get();
+ if (ret < 0) {
+ dev_err(s->dev,
+ "trusty std call (SMC_SC_TEST_SHARED_MEM_RW) failed: %d 0x%llx\n",
+ ret, obj->mem_id);
+ return ret;
+ }
+
+ for (i = 0; i < size / sizeof(*buf); i++) {
+ if (buf[i] != size - i) {
+ dev_err(s->dev,
+ "input mismatch at %zd, got 0x%llx instead of 0x%zx\n",
+ i, buf[i], size - i);
+ return -EIO;
+ }
+ }
+
+ dev_dbg(s->dev, "rw id=0x%llx, size=%zu took %lld ns\n", obj->mem_id,
+ size, ktime_to_ns(ktime_sub(t2, t1)));
+
+ return 0;
+}
+
+/*
+ * Run test on every test object in @obj_list. Repeat @repeat_access times.
+ */
+static int trusty_test_rw_objs(struct trusty_test_state *s,
+ struct list_head *obj_list,
+ size_t repeat_access)
+{
+ int ret;
+ size_t i;
+ struct trusty_test_shmem_obj *obj;
+
+ for (i = 0; i < repeat_access; i++) {
+ /*
+ * Repeat test in case the memory attributes don't match
+ * and either side see old data.
+ */
+ list_for_each_entry(obj, obj_list, node) {
+ ret = trusty_test_rw(s, obj);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Allocate @obj_count test object that each have @page_count pages. Share each
+ * object @repeat_share times, each time running tests on every object
+ * @repeat_access times.
+ */
+static int trusty_test_run(struct trusty_test_state *s, size_t page_count,
+ size_t obj_count, size_t repeat_share,
+ size_t repeat_access)
+{
+ int ret = 0;
+ int tmpret;
+ size_t i;
+ size_t size = page_count * PAGE_SIZE;
+ LIST_HEAD(obj_list);
+ struct trusty_test_shmem_obj *obj;
+ struct trusty_test_shmem_obj *next_obj;
+
+ for (i = 0; i < obj_count && !ret; i++)
+ ret = trusty_test_alloc_obj(s, page_count, &obj_list);
+
+ for (i = 0; i < repeat_share && !ret; i++) {
+ ret = trusty_test_share_objs(s, &obj_list, size);
+ if (ret) {
+ dev_err(s->dev,
+ "trusty_share_memory failed: %d, i=%zd/%zd, size=%zd\n",
+ ret, i, repeat_share, size);
+ } else {
+ ret = trusty_test_rw_objs(s, &obj_list, repeat_access);
+ if (ret)
+ dev_err(s->dev,
+ "test failed: %d, i=%zd/%zd, size=%zd\n",
+ ret, i, repeat_share, size);
+ }
+ tmpret = trusty_test_reclaim_objs(s, &obj_list, size);
+ if (tmpret) {
+ ret = tmpret;
+ dev_err(s->dev,
+ "trusty_reclaim_memory failed: %d, i=%zd/%zd\n",
+ ret, i, repeat_share);
+ }
+ }
+
+ list_for_each_entry_safe(obj, next_obj, &obj_list, node)
+ trusty_test_free_obj(s, obj);
+
+ dev_info(s->dev, "[ %s ] size %zd, obj_count %zd, repeat_share %zd, repeat_access %zd\n",
+ ret ? "FAILED" : "PASSED", size, obj_count, repeat_share,
+ repeat_access);
+
+ return ret;
+}
+
+/*
+ * Get an optional numeric argument from @buf, update @buf and return the value.
+ * If @buf does not start with ",", return @default_val instead.
+ */
+static size_t trusty_test_get_arg(const char **buf, size_t default_val)
+{
+ char *buf_next;
+ size_t ret;
+
+ if (**buf != ',')
+ return default_val;
+
+ (*buf)++;
+ ret = simple_strtoul(*buf, &buf_next, 0);
+ if (buf_next == *buf)
+ return default_val;
+
+ *buf = buf_next;
+
+ return ret;
+}
+
+/*
+ * Run tests described by a string in this format:
+ * <obj_size>,<obj_count=1>,<repeat_share=1>,<repeat_access=3>
+ */
+static ssize_t trusty_test_run_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct trusty_test_state *s = platform_get_drvdata(pdev);
+ size_t size;
+ size_t obj_count;
+ size_t repeat_share;
+ size_t repeat_access;
+ int ret;
+ char *buf_next;
+
+ while (true) {
+ while (isspace(*buf))
+ buf++;
+ size = simple_strtoul(buf, &buf_next, 0);
+ if (buf_next == buf)
+ return count;
+ buf = buf_next;
+ obj_count = trusty_test_get_arg(&buf, 1);
+ repeat_share = trusty_test_get_arg(&buf, 1);
+ repeat_access = trusty_test_get_arg(&buf, 3);
+
+ ret = trusty_test_run(s, DIV_ROUND_UP(size, PAGE_SIZE),
+ obj_count, repeat_share, repeat_access);
+ if (ret)
+ return ret;
+ }
+}
+
+static DEVICE_ATTR_WO(trusty_test_run);
+
+static struct attribute *trusty_test_attrs[] = {
+ &dev_attr_trusty_test_run.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(trusty_test);
+
+static int trusty_test_probe(struct platform_device *pdev)
+{
+ struct trusty_test_state *s;
+ int ret;
+
+ ret = trusty_std_call32(pdev->dev.parent, SMC_SC_TEST_VERSION,
+ TRUSTY_STDCALLTEST_API_VERSION, 0, 0);
+ if (ret != TRUSTY_STDCALLTEST_API_VERSION)
+ return -ENOENT;
+
+ s = kzalloc(sizeof(*s), GFP_KERNEL);
+ if (!s)
+ return -ENOMEM;
+
+ s->dev = &pdev->dev;
+ s->trusty_dev = s->dev->parent;
+
+ platform_set_drvdata(pdev, s);
+
+ return 0;
+}
+
+static int trusty_test_remove(struct platform_device *pdev)
+{
+ struct trusty_log_state *s = platform_get_drvdata(pdev);
+
+ kfree(s);
+ return 0;
+}
+
+static const struct of_device_id trusty_test_of_match[] = {
+ { .compatible = "android,trusty-test-v1", },
+ {},
+};
+
+MODULE_DEVICE_TABLE(trusty, trusty_test_of_match);
+
+static struct platform_driver trusty_test_driver = {
+ .probe = trusty_test_probe,
+ .remove = trusty_test_remove,
+ .driver = {
+ .name = "trusty-test",
+ .of_match_table = trusty_test_of_match,
+ .dev_groups = trusty_test_groups,
+ },
+};
+
+module_platform_driver(trusty_test_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Trusty test driver");
diff --git a/drivers/trusty/trusty-test.h b/drivers/trusty/trusty-test.h
new file mode 100644
index 000000000000..eea7beb96876
--- /dev/null
+++ b/drivers/trusty/trusty-test.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2020 Google, Inc.
+ */
+#ifndef _TRUSTY_TEST_H
+#define _TRUSTY_TEST_H
+
+#define SMC_SC_TEST_VERSION SMC_STDCALL_NR(SMC_ENTITY_TEST, 0)
+#define SMC_SC_TEST_SHARED_MEM_RW SMC_STDCALL_NR(SMC_ENTITY_TEST, 1)
+
+#define TRUSTY_STDCALLTEST_API_VERSION 1
+
+#endif /* _TRUSTY_TEST_H */
diff --git a/drivers/trusty/trusty-virtio.c b/drivers/trusty/trusty-virtio.c
new file mode 100644
index 000000000000..fea59cd2e218
--- /dev/null
+++ b/drivers/trusty/trusty-virtio.c
@@ -0,0 +1,840 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Trusty Virtio driver
+ *
+ * Copyright (C) 2015 Google, Inc.
+ */
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+
+#include <linux/dma-map-ops.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/notifier.h>
+#include <linux/workqueue.h>
+#include <linux/remoteproc.h>
+#include <linux/slab.h>
+
+#include <linux/platform_device.h>
+#include <linux/trusty/smcall.h>
+#include <linux/trusty/trusty.h>
+#include <linux/trusty/trusty_ipc.h>
+
+#include <linux/virtio.h>
+#include <linux/virtio_config.h>
+#include <linux/virtio_ids.h>
+#include <linux/virtio_ring.h>
+
+#include <linux/atomic.h>
+
+#define RSC_DESCR_VER 1
+
+struct trusty_vdev;
+
+struct trusty_ctx {
+ struct device *dev;
+ void *shared_va;
+ struct scatterlist shared_sg;
+ trusty_shared_mem_id_t shared_id;
+ size_t shared_sz;
+ struct work_struct check_vqs;
+ struct work_struct kick_vqs;
+ struct notifier_block call_notifier;
+ struct list_head vdev_list;
+ struct mutex mlock; /* protects vdev_list */
+ struct workqueue_struct *kick_wq;
+ struct workqueue_struct *check_wq;
+};
+
+struct trusty_vring {
+ void *vaddr;
+ struct scatterlist sg;
+ trusty_shared_mem_id_t shared_mem_id;
+ size_t size;
+ unsigned int align;
+ unsigned int elem_num;
+ u32 notifyid;
+ atomic_t needs_kick;
+ struct fw_rsc_vdev_vring *vr_descr;
+ struct virtqueue *vq;
+ struct trusty_vdev *tvdev;
+ struct trusty_nop kick_nop;
+};
+
+struct trusty_vdev {
+ struct list_head node;
+ struct virtio_device vdev;
+ struct trusty_ctx *tctx;
+ u32 notifyid;
+ unsigned int config_len;
+ void *config;
+ struct fw_rsc_vdev *vdev_descr;
+ unsigned int vring_num;
+ struct trusty_vring vrings[];
+};
+
+#define vdev_to_tvdev(vd) container_of((vd), struct trusty_vdev, vdev)
+
+static void check_all_vqs(struct work_struct *work)
+{
+ unsigned int i;
+ struct trusty_ctx *tctx = container_of(work, struct trusty_ctx,
+ check_vqs);
+ struct trusty_vdev *tvdev;
+
+ list_for_each_entry(tvdev, &tctx->vdev_list, node) {
+ for (i = 0; i < tvdev->vring_num; i++)
+ if (tvdev->vrings[i].vq)
+ vring_interrupt(0, tvdev->vrings[i].vq);
+ }
+}
+
+static int trusty_call_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct trusty_ctx *tctx;
+
+ if (action != TRUSTY_CALL_RETURNED)
+ return NOTIFY_DONE;
+
+ tctx = container_of(nb, struct trusty_ctx, call_notifier);
+ queue_work(tctx->check_wq, &tctx->check_vqs);
+
+ return NOTIFY_OK;
+}
+
+static void kick_vq(struct trusty_ctx *tctx,
+ struct trusty_vdev *tvdev,
+ struct trusty_vring *tvr)
+{
+ int ret;
+
+ dev_dbg(tctx->dev, "%s: vdev_id=%d: vq_id=%d\n",
+ __func__, tvdev->notifyid, tvr->notifyid);
+
+ ret = trusty_std_call32(tctx->dev->parent, SMC_SC_VDEV_KICK_VQ,
+ tvdev->notifyid, tvr->notifyid, 0);
+ if (ret) {
+ dev_err(tctx->dev, "vq notify (%d, %d) returned %d\n",
+ tvdev->notifyid, tvr->notifyid, ret);
+ }
+}
+
+static void kick_vqs(struct work_struct *work)
+{
+ unsigned int i;
+ struct trusty_vdev *tvdev;
+ struct trusty_ctx *tctx = container_of(work, struct trusty_ctx,
+ kick_vqs);
+ mutex_lock(&tctx->mlock);
+ list_for_each_entry(tvdev, &tctx->vdev_list, node) {
+ for (i = 0; i < tvdev->vring_num; i++) {
+ struct trusty_vring *tvr = &tvdev->vrings[i];
+
+ if (atomic_xchg(&tvr->needs_kick, 0))
+ kick_vq(tctx, tvdev, tvr);
+ }
+ }
+ mutex_unlock(&tctx->mlock);
+}
+
+static bool trusty_virtio_notify(struct virtqueue *vq)
+{
+ struct trusty_vring *tvr = vq->priv;
+ struct trusty_vdev *tvdev = tvr->tvdev;
+ struct trusty_ctx *tctx = tvdev->tctx;
+ u32 api_ver = trusty_get_api_version(tctx->dev->parent);
+
+ if (api_ver < TRUSTY_API_VERSION_SMP_NOP) {
+ atomic_set(&tvr->needs_kick, 1);
+ queue_work(tctx->kick_wq, &tctx->kick_vqs);
+ } else {
+ trusty_enqueue_nop(tctx->dev->parent, &tvr->kick_nop);
+ }
+
+ return true;
+}
+
+static int trusty_load_device_descr(struct trusty_ctx *tctx,
+ trusty_shared_mem_id_t id, size_t sz)
+{
+ int ret;
+
+ dev_dbg(tctx->dev, "%s: %zu bytes @ id %llu\n", __func__, sz, id);
+
+ ret = trusty_std_call32(tctx->dev->parent, SMC_SC_VIRTIO_GET_DESCR,
+ (u32)id, id >> 32, sz);
+ if (ret < 0) {
+ dev_err(tctx->dev, "%s: virtio get descr returned (%d)\n",
+ __func__, ret);
+ return -ENODEV;
+ }
+ return ret;
+}
+
+static void trusty_virtio_stop(struct trusty_ctx *tctx,
+ trusty_shared_mem_id_t id, size_t sz)
+{
+ int ret;
+
+ dev_dbg(tctx->dev, "%s: %zu bytes @ id %llu\n", __func__, sz, id);
+
+ ret = trusty_std_call32(tctx->dev->parent, SMC_SC_VIRTIO_STOP,
+ (u32)id, id >> 32, sz);
+ if (ret) {
+ dev_err(tctx->dev, "%s: virtio done returned (%d)\n",
+ __func__, ret);
+ return;
+ }
+}
+
+static int trusty_virtio_start(struct trusty_ctx *tctx,
+ trusty_shared_mem_id_t id, size_t sz)
+{
+ int ret;
+
+ dev_dbg(tctx->dev, "%s: %zu bytes @ id %llu\n", __func__, sz, id);
+
+ ret = trusty_std_call32(tctx->dev->parent, SMC_SC_VIRTIO_START,
+ (u32)id, id >> 32, sz);
+ if (ret) {
+ dev_err(tctx->dev, "%s: virtio start returned (%d)\n",
+ __func__, ret);
+ return -ENODEV;
+ }
+ return 0;
+}
+
+static void trusty_virtio_reset(struct virtio_device *vdev)
+{
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+ struct trusty_ctx *tctx = tvdev->tctx;
+
+ dev_dbg(&vdev->dev, "reset vdev_id=%d\n", tvdev->notifyid);
+ trusty_std_call32(tctx->dev->parent, SMC_SC_VDEV_RESET,
+ tvdev->notifyid, 0, 0);
+}
+
+static u64 trusty_virtio_get_features(struct virtio_device *vdev)
+{
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+
+ return tvdev->vdev_descr->dfeatures |
+ (1ULL << VIRTIO_F_ACCESS_PLATFORM);
+}
+
+static int trusty_virtio_finalize_features(struct virtio_device *vdev)
+{
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+ u64 features = vdev->features;
+
+ /*
+ * We set VIRTIO_F_ACCESS_PLATFORM to enable the dma mapping hooks.
+ * The other side does not need to know.
+ */
+ features &= ~(1ULL << VIRTIO_F_ACCESS_PLATFORM);
+
+ /* Make sure we don't have any features > 32 bits! */
+ if (WARN_ON((u32)vdev->features != features))
+ return -EINVAL;
+
+ tvdev->vdev_descr->gfeatures = vdev->features;
+ return 0;
+}
+
+static void trusty_virtio_get_config(struct virtio_device *vdev,
+ unsigned int offset, void *buf,
+ unsigned int len)
+{
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+
+ dev_dbg(&vdev->dev, "%s: %d bytes @ offset %d\n",
+ __func__, len, offset);
+
+ if (tvdev->config) {
+ if (offset + len <= tvdev->config_len)
+ memcpy(buf, tvdev->config + offset, len);
+ }
+}
+
+static void trusty_virtio_set_config(struct virtio_device *vdev,
+ unsigned int offset, const void *buf,
+ unsigned int len)
+{
+}
+
+static u8 trusty_virtio_get_status(struct virtio_device *vdev)
+{
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+
+ return tvdev->vdev_descr->status;
+}
+
+static void trusty_virtio_set_status(struct virtio_device *vdev, u8 status)
+{
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+
+ tvdev->vdev_descr->status = status;
+}
+
+static void _del_vqs(struct virtio_device *vdev)
+{
+ unsigned int i;
+ int ret;
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+ struct trusty_vring *tvr = &tvdev->vrings[0];
+
+ for (i = 0; i < tvdev->vring_num; i++, tvr++) {
+ /* dequeue kick_nop */
+ trusty_dequeue_nop(tvdev->tctx->dev->parent, &tvr->kick_nop);
+
+ /* delete vq */
+ if (tvr->vq) {
+ vring_del_virtqueue(tvr->vq);
+ tvr->vq = NULL;
+ }
+ /* delete vring */
+ if (tvr->vaddr) {
+ ret = trusty_reclaim_memory(tvdev->tctx->dev->parent,
+ tvr->shared_mem_id,
+ &tvr->sg, 1);
+ if (WARN_ON(ret)) {
+ dev_err(&vdev->dev,
+ "trusty_revoke_memory failed: %d 0x%llx\n",
+ ret, tvr->shared_mem_id);
+ /*
+ * It is not safe to free this memory if
+ * trusty_revoke_memory fails. Leak it in that
+ * case.
+ */
+ } else {
+ free_pages_exact(tvr->vaddr, tvr->size);
+ }
+ tvr->vaddr = NULL;
+ }
+ }
+}
+
+static void trusty_virtio_del_vqs(struct virtio_device *vdev)
+{
+ _del_vqs(vdev);
+}
+
+
+static struct virtqueue *_find_vq(struct virtio_device *vdev,
+ unsigned int id,
+ void (*callback)(struct virtqueue *vq),
+ const char *name,
+ bool ctx)
+{
+ struct trusty_vring *tvr;
+ struct trusty_vdev *tvdev = vdev_to_tvdev(vdev);
+ phys_addr_t pa;
+ int ret;
+
+ if (!name)
+ return ERR_PTR(-EINVAL);
+
+ if (id >= tvdev->vring_num)
+ return ERR_PTR(-EINVAL);
+
+ tvr = &tvdev->vrings[id];
+
+ /* actual size of vring (in bytes) */
+ tvr->size = PAGE_ALIGN(vring_size(tvr->elem_num, tvr->align));
+
+ /* allocate memory for the vring. */
+ tvr->vaddr = alloc_pages_exact(tvr->size, GFP_KERNEL | __GFP_ZERO);
+ if (!tvr->vaddr) {
+ dev_err(&vdev->dev, "vring alloc failed\n");
+ return ERR_PTR(-ENOMEM);
+ }
+
+ sg_init_one(&tvr->sg, tvr->vaddr, tvr->size);
+ ret = trusty_share_memory_compat(tvdev->tctx->dev->parent,
+ &tvr->shared_mem_id, &tvr->sg, 1,
+ PAGE_KERNEL);
+ if (ret) {
+ pa = virt_to_phys(tvr->vaddr);
+ dev_err(&vdev->dev, "trusty_share_memory failed: %d %pa\n",
+ ret, &pa);
+ goto err_share_memory;
+ }
+
+ /* save vring address to shared structure */
+ tvr->vr_descr->da = (u32)tvr->shared_mem_id;
+
+ /* da field is only 32 bit wide. Use previously unused 'reserved' field
+ * to store top 32 bits of 64-bit shared_mem_id
+ */
+ tvr->vr_descr->pa = (u32)(tvr->shared_mem_id >> 32);
+
+ dev_info(&vdev->dev, "vring%d: va(id) %p(%llx) qsz %d notifyid %d\n",
+ id, tvr->vaddr, (u64)tvr->shared_mem_id, tvr->elem_num,
+ tvr->notifyid);
+
+ tvr->vq = vring_new_virtqueue(id, tvr->elem_num, tvr->align,
+ vdev, true, ctx, tvr->vaddr,
+ trusty_virtio_notify, callback, name);
+ if (!tvr->vq) {
+ dev_err(&vdev->dev, "vring_new_virtqueue %s failed\n",
+ name);
+ goto err_new_virtqueue;
+ }
+
+ tvr->vq->priv = tvr;
+
+ return tvr->vq;
+
+err_new_virtqueue:
+ ret = trusty_reclaim_memory(tvdev->tctx->dev->parent,
+ tvr->shared_mem_id, &tvr->sg, 1);
+ if (WARN_ON(ret)) {
+ dev_err(&vdev->dev, "trusty_revoke_memory failed: %d 0x%llx\n",
+ ret, tvr->shared_mem_id);
+ /*
+ * It is not safe to free this memory if trusty_revoke_memory
+ * fails. Leak it in that case.
+ */
+ } else {
+err_share_memory:
+ free_pages_exact(tvr->vaddr, tvr->size);
+ }
+ tvr->vaddr = NULL;
+ return ERR_PTR(-ENOMEM);
+}
+
+static int trusty_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
+ struct virtqueue *vqs[],
+ vq_callback_t *callbacks[],
+ const char * const names[],
+ const bool *ctxs,
+ struct irq_affinity *desc)
+{
+ unsigned int i;
+ int ret;
+ bool ctx = false;
+
+ for (i = 0; i < nvqs; i++) {
+ ctx = false;
+ if (ctxs)
+ ctx = ctxs[i];
+ vqs[i] = _find_vq(vdev, i, callbacks[i], names[i], ctx);
+ if (IS_ERR(vqs[i])) {
+ ret = PTR_ERR(vqs[i]);
+ _del_vqs(vdev);
+ return ret;
+ }
+ }
+ return 0;
+}
+
+static const char *trusty_virtio_bus_name(struct virtio_device *vdev)
+{
+ return "trusty-virtio";
+}
+
+/* The ops structure which hooks everything together. */
+static const struct virtio_config_ops trusty_virtio_config_ops = {
+ .get_features = trusty_virtio_get_features,
+ .finalize_features = trusty_virtio_finalize_features,
+ .get = trusty_virtio_get_config,
+ .set = trusty_virtio_set_config,
+ .get_status = trusty_virtio_get_status,
+ .set_status = trusty_virtio_set_status,
+ .reset = trusty_virtio_reset,
+ .find_vqs = trusty_virtio_find_vqs,
+ .del_vqs = trusty_virtio_del_vqs,
+ .bus_name = trusty_virtio_bus_name,
+};
+
+static int trusty_virtio_add_device(struct trusty_ctx *tctx,
+ struct fw_rsc_vdev *vdev_descr,
+ struct fw_rsc_vdev_vring *vr_descr,
+ void *config)
+{
+ int i, ret;
+ struct trusty_vdev *tvdev;
+
+ tvdev = kzalloc(struct_size(tvdev, vrings, vdev_descr->num_of_vrings),
+ GFP_KERNEL);
+ if (!tvdev)
+ return -ENOMEM;
+
+ /* setup vdev */
+ tvdev->tctx = tctx;
+ tvdev->vdev.dev.parent = tctx->dev;
+ tvdev->vdev.id.device = vdev_descr->id;
+ tvdev->vdev.config = &trusty_virtio_config_ops;
+ tvdev->vdev_descr = vdev_descr;
+ tvdev->notifyid = vdev_descr->notifyid;
+
+ /* setup config */
+ tvdev->config = config;
+ tvdev->config_len = vdev_descr->config_len;
+
+ /* setup vrings and vdev resource */
+ tvdev->vring_num = vdev_descr->num_of_vrings;
+
+ for (i = 0; i < tvdev->vring_num; i++, vr_descr++) {
+ struct trusty_vring *tvr = &tvdev->vrings[i];
+
+ tvr->tvdev = tvdev;
+ tvr->vr_descr = vr_descr;
+ tvr->align = vr_descr->align;
+ tvr->elem_num = vr_descr->num;
+ tvr->notifyid = vr_descr->notifyid;
+ trusty_nop_init(&tvr->kick_nop, SMC_NC_VDEV_KICK_VQ,
+ tvdev->notifyid, tvr->notifyid);
+ }
+
+ /* register device */
+ ret = register_virtio_device(&tvdev->vdev);
+ if (ret) {
+ dev_err(tctx->dev,
+ "Failed (%d) to register device dev type %u\n",
+ ret, vdev_descr->id);
+ goto err_register;
+ }
+
+ /* add it to tracking list */
+ list_add_tail(&tvdev->node, &tctx->vdev_list);
+
+ return 0;
+
+err_register:
+ kfree(tvdev);
+ return ret;
+}
+
+static int trusty_parse_device_descr(struct trusty_ctx *tctx,
+ void *descr_va, size_t descr_sz)
+{
+ u32 i;
+ struct resource_table *descr = descr_va;
+
+ if (descr_sz < sizeof(*descr)) {
+ dev_err(tctx->dev, "descr table is too small (0x%x)\n",
+ (int)descr_sz);
+ return -ENODEV;
+ }
+
+ if (descr->ver != RSC_DESCR_VER) {
+ dev_err(tctx->dev, "unexpected descr ver (0x%x)\n",
+ (int)descr->ver);
+ return -ENODEV;
+ }
+
+ if (descr_sz < (sizeof(*descr) + descr->num * sizeof(u32))) {
+ dev_err(tctx->dev, "descr table is too small (0x%x)\n",
+ (int)descr->ver);
+ return -ENODEV;
+ }
+
+ for (i = 0; i < descr->num; i++) {
+ struct fw_rsc_hdr *hdr;
+ struct fw_rsc_vdev *vd;
+ struct fw_rsc_vdev_vring *vr;
+ void *cfg;
+ size_t vd_sz;
+
+ u32 offset = descr->offset[i];
+
+ if (offset >= descr_sz) {
+ dev_err(tctx->dev, "offset is out of bounds (%u)\n",
+ offset);
+ return -ENODEV;
+ }
+
+ /* check space for rsc header */
+ if ((descr_sz - offset) < sizeof(struct fw_rsc_hdr)) {
+ dev_err(tctx->dev, "no space for rsc header (%u)\n",
+ offset);
+ return -ENODEV;
+ }
+ hdr = (struct fw_rsc_hdr *)((u8 *)descr + offset);
+ offset += sizeof(struct fw_rsc_hdr);
+
+ /* check type */
+ if (hdr->type != RSC_VDEV) {
+ dev_err(tctx->dev, "unsupported rsc type (%u)\n",
+ hdr->type);
+ continue;
+ }
+
+ /* got vdev: check space for vdev */
+ if ((descr_sz - offset) < sizeof(struct fw_rsc_vdev)) {
+ dev_err(tctx->dev, "no space for vdev descr (%u)\n",
+ offset);
+ return -ENODEV;
+ }
+ vd = (struct fw_rsc_vdev *)((u8 *)descr + offset);
+
+ /* check space for vrings and config area */
+ vd_sz = sizeof(struct fw_rsc_vdev) +
+ vd->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) +
+ vd->config_len;
+
+ if ((descr_sz - offset) < vd_sz) {
+ dev_err(tctx->dev, "no space for vdev (%u)\n", offset);
+ return -ENODEV;
+ }
+ vr = (struct fw_rsc_vdev_vring *)vd->vring;
+ cfg = (void *)(vr + vd->num_of_vrings);
+
+ trusty_virtio_add_device(tctx, vd, vr, cfg);
+ }
+
+ return 0;
+}
+
+static void _remove_devices_locked(struct trusty_ctx *tctx)
+{
+ struct trusty_vdev *tvdev, *next;
+
+ list_for_each_entry_safe(tvdev, next, &tctx->vdev_list, node) {
+ list_del(&tvdev->node);
+ unregister_virtio_device(&tvdev->vdev);
+ kfree(tvdev);
+ }
+}
+
+static void trusty_virtio_remove_devices(struct trusty_ctx *tctx)
+{
+ mutex_lock(&tctx->mlock);
+ _remove_devices_locked(tctx);
+ mutex_unlock(&tctx->mlock);
+}
+
+static int trusty_virtio_add_devices(struct trusty_ctx *tctx)
+{
+ int ret;
+ int ret_tmp;
+ void *descr_va;
+ trusty_shared_mem_id_t descr_id;
+ size_t descr_sz;
+ size_t descr_buf_sz;
+
+ /* allocate buffer to load device descriptor into */
+ descr_buf_sz = PAGE_SIZE;
+ descr_va = alloc_pages_exact(descr_buf_sz, GFP_KERNEL | __GFP_ZERO);
+ if (!descr_va) {
+ dev_err(tctx->dev, "Failed to allocate shared area\n");
+ return -ENOMEM;
+ }
+
+ sg_init_one(&tctx->shared_sg, descr_va, descr_buf_sz);
+ ret = trusty_share_memory(tctx->dev->parent, &descr_id,
+ &tctx->shared_sg, 1, PAGE_KERNEL);
+ if (ret) {
+ dev_err(tctx->dev, "trusty_share_memory failed: %d\n", ret);
+ goto err_share_memory;
+ }
+
+ /* load device descriptors */
+ ret = trusty_load_device_descr(tctx, descr_id, descr_buf_sz);
+ if (ret < 0) {
+ dev_err(tctx->dev, "failed (%d) to load device descr\n", ret);
+ goto err_load_descr;
+ }
+
+ descr_sz = (size_t)ret;
+
+ mutex_lock(&tctx->mlock);
+
+ /* parse device descriptor and add virtio devices */
+ ret = trusty_parse_device_descr(tctx, descr_va, descr_sz);
+ if (ret) {
+ dev_err(tctx->dev, "failed (%d) to parse device descr\n", ret);
+ goto err_parse_descr;
+ }
+
+ /* register call notifier */
+ ret = trusty_call_notifier_register(tctx->dev->parent,
+ &tctx->call_notifier);
+ if (ret) {
+ dev_err(tctx->dev, "%s: failed (%d) to register notifier\n",
+ __func__, ret);
+ goto err_register_notifier;
+ }
+
+ /* start virtio */
+ ret = trusty_virtio_start(tctx, descr_id, descr_sz);
+ if (ret) {
+ dev_err(tctx->dev, "failed (%d) to start virtio\n", ret);
+ goto err_start_virtio;
+ }
+
+ /* attach shared area */
+ tctx->shared_va = descr_va;
+ tctx->shared_id = descr_id;
+ tctx->shared_sz = descr_buf_sz;
+
+ mutex_unlock(&tctx->mlock);
+
+ return 0;
+
+err_start_virtio:
+ trusty_call_notifier_unregister(tctx->dev->parent,
+ &tctx->call_notifier);
+ cancel_work_sync(&tctx->check_vqs);
+err_register_notifier:
+err_parse_descr:
+ _remove_devices_locked(tctx);
+ mutex_unlock(&tctx->mlock);
+ cancel_work_sync(&tctx->kick_vqs);
+ trusty_virtio_stop(tctx, descr_id, descr_sz);
+err_load_descr:
+ ret_tmp = trusty_reclaim_memory(tctx->dev->parent, descr_id,
+ &tctx->shared_sg, 1);
+ if (WARN_ON(ret_tmp)) {
+ dev_err(tctx->dev, "trusty_revoke_memory failed: %d 0x%llx\n",
+ ret_tmp, tctx->shared_id);
+ /*
+ * It is not safe to free this memory if trusty_revoke_memory
+ * fails. Leak it in that case.
+ */
+ } else {
+err_share_memory:
+ free_pages_exact(descr_va, descr_buf_sz);
+ }
+ return ret;
+}
+
+static dma_addr_t trusty_virtio_dma_map_page(struct device *dev,
+ struct page *page,
+ unsigned long offset, size_t size,
+ enum dma_data_direction dir,
+ unsigned long attrs)
+{
+ struct tipc_msg_buf *buf = page_to_virt(page) + offset;
+
+ return buf->buf_id;
+}
+
+static const struct dma_map_ops trusty_virtio_dma_map_ops = {
+ .map_page = trusty_virtio_dma_map_page,
+};
+
+static int trusty_virtio_probe(struct platform_device *pdev)
+{
+ int ret;
+ struct trusty_ctx *tctx;
+
+ tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
+ if (!tctx)
+ return -ENOMEM;
+
+ tctx->dev = &pdev->dev;
+ tctx->call_notifier.notifier_call = trusty_call_notify;
+ mutex_init(&tctx->mlock);
+ INIT_LIST_HEAD(&tctx->vdev_list);
+ INIT_WORK(&tctx->check_vqs, check_all_vqs);
+ INIT_WORK(&tctx->kick_vqs, kick_vqs);
+ platform_set_drvdata(pdev, tctx);
+
+ set_dma_ops(&pdev->dev, &trusty_virtio_dma_map_ops);
+
+ tctx->check_wq = alloc_workqueue("trusty-check-wq", WQ_UNBOUND, 0);
+ if (!tctx->check_wq) {
+ ret = -ENODEV;
+ dev_err(&pdev->dev, "Failed create trusty-check-wq\n");
+ goto err_create_check_wq;
+ }
+
+ tctx->kick_wq = alloc_workqueue("trusty-kick-wq",
+ WQ_UNBOUND | WQ_CPU_INTENSIVE, 0);
+ if (!tctx->kick_wq) {
+ ret = -ENODEV;
+ dev_err(&pdev->dev, "Failed create trusty-kick-wq\n");
+ goto err_create_kick_wq;
+ }
+
+ ret = trusty_virtio_add_devices(tctx);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to add virtio devices\n");
+ goto err_add_devices;
+ }
+
+ dev_info(&pdev->dev, "initializing done\n");
+ return 0;
+
+err_add_devices:
+ destroy_workqueue(tctx->kick_wq);
+err_create_kick_wq:
+ destroy_workqueue(tctx->check_wq);
+err_create_check_wq:
+ kfree(tctx);
+ return ret;
+}
+
+static int trusty_virtio_remove(struct platform_device *pdev)
+{
+ struct trusty_ctx *tctx = platform_get_drvdata(pdev);
+ int ret;
+
+ /* unregister call notifier and wait until workqueue is done */
+ trusty_call_notifier_unregister(tctx->dev->parent,
+ &tctx->call_notifier);
+ cancel_work_sync(&tctx->check_vqs);
+
+ /* remove virtio devices */
+ trusty_virtio_remove_devices(tctx);
+ cancel_work_sync(&tctx->kick_vqs);
+
+ /* destroy workqueues */
+ destroy_workqueue(tctx->kick_wq);
+ destroy_workqueue(tctx->check_wq);
+
+ /* notify remote that shared area goes away */
+ trusty_virtio_stop(tctx, tctx->shared_id, tctx->shared_sz);
+
+ /* free shared area */
+ ret = trusty_reclaim_memory(tctx->dev->parent, tctx->shared_id,
+ &tctx->shared_sg, 1);
+ if (WARN_ON(ret)) {
+ dev_err(tctx->dev, "trusty_revoke_memory failed: %d 0x%llx\n",
+ ret, tctx->shared_id);
+ /*
+ * It is not safe to free this memory if trusty_revoke_memory
+ * fails. Leak it in that case.
+ */
+ } else {
+ free_pages_exact(tctx->shared_va, tctx->shared_sz);
+ }
+
+ /* free context */
+ kfree(tctx);
+ return 0;
+}
+
+static const struct of_device_id trusty_of_match[] = {
+ {
+ .compatible = "android,trusty-virtio-v1",
+ },
+ {},
+};
+
+MODULE_DEVICE_TABLE(of, trusty_of_match);
+
+static struct platform_driver trusty_virtio_driver = {
+ .probe = trusty_virtio_probe,
+ .remove = trusty_virtio_remove,
+ .driver = {
+ .name = "trusty-virtio",
+ .of_match_table = trusty_of_match,
+ },
+};
+
+module_platform_driver(trusty_virtio_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Trusty virtio driver");
+/*
+ * TODO(b/168322325): trusty-virtio and trusty-ipc should be independent.
+ * However, trusty-virtio is not completely generic and is aware of trusty-ipc.
+ * See header includes. Particularly, trusty-virtio.ko can't be loaded before
+ * trusty-ipc.ko.
+ */
+MODULE_SOFTDEP("pre: trusty-ipc");
diff --git a/drivers/trusty/trusty.c b/drivers/trusty/trusty.c
new file mode 100644
index 000000000000..265eab52aea0
--- /dev/null
+++ b/drivers/trusty/trusty.c
@@ -0,0 +1,981 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2013 Google, Inc.
+ */
+
+#include <linux/delay.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/stat.h>
+#include <linux/string.h>
+#include <linux/trusty/arm_ffa.h>
+#include <linux/trusty/smcall.h>
+#include <linux/trusty/sm_err.h>
+#include <linux/trusty/trusty.h>
+
+#include <linux/scatterlist.h>
+#include <linux/dma-mapping.h>
+
+#include "trusty-smc.h"
+
+struct trusty_state;
+static struct platform_driver trusty_driver;
+
+struct trusty_work {
+ struct trusty_state *ts;
+ struct work_struct work;
+};
+
+struct trusty_state {
+ struct mutex smc_lock;
+ struct atomic_notifier_head notifier;
+ struct completion cpu_idle_completion;
+ char *version_str;
+ u32 api_version;
+ bool trusty_panicked;
+ struct device *dev;
+ struct workqueue_struct *nop_wq;
+ struct trusty_work __percpu *nop_works;
+ struct list_head nop_queue;
+ spinlock_t nop_lock; /* protects nop_queue */
+ struct device_dma_parameters dma_parms;
+ void *ffa_tx;
+ void *ffa_rx;
+ u16 ffa_local_id;
+ u16 ffa_remote_id;
+ struct mutex share_memory_msg_lock; /* protects share_memory_msg */
+};
+
+static inline unsigned long smc(unsigned long r0, unsigned long r1,
+ unsigned long r2, unsigned long r3)
+{
+ return trusty_smc8(r0, r1, r2, r3, 0, 0, 0, 0).r0;
+}
+
+s32 trusty_fast_call32(struct device *dev, u32 smcnr, u32 a0, u32 a1, u32 a2)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ if (WARN_ON(!s))
+ return SM_ERR_INVALID_PARAMETERS;
+ if (WARN_ON(!SMC_IS_FASTCALL(smcnr)))
+ return SM_ERR_INVALID_PARAMETERS;
+ if (WARN_ON(SMC_IS_SMC64(smcnr)))
+ return SM_ERR_INVALID_PARAMETERS;
+
+ return smc(smcnr, a0, a1, a2);
+}
+EXPORT_SYMBOL(trusty_fast_call32);
+
+#ifdef CONFIG_64BIT
+s64 trusty_fast_call64(struct device *dev, u64 smcnr, u64 a0, u64 a1, u64 a2)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ if (WARN_ON(!s))
+ return SM_ERR_INVALID_PARAMETERS;
+ if (WARN_ON(!SMC_IS_FASTCALL(smcnr)))
+ return SM_ERR_INVALID_PARAMETERS;
+ if (WARN_ON(!SMC_IS_SMC64(smcnr)))
+ return SM_ERR_INVALID_PARAMETERS;
+
+ return smc(smcnr, a0, a1, a2);
+}
+EXPORT_SYMBOL(trusty_fast_call64);
+#endif
+
+static unsigned long trusty_std_call_inner(struct device *dev,
+ unsigned long smcnr,
+ unsigned long a0, unsigned long a1,
+ unsigned long a2)
+{
+ unsigned long ret;
+ int retry = 5;
+
+ dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx)\n",
+ __func__, smcnr, a0, a1, a2);
+ while (true) {
+ ret = smc(smcnr, a0, a1, a2);
+ while ((s32)ret == SM_ERR_FIQ_INTERRUPTED)
+ ret = smc(SMC_SC_RESTART_FIQ, 0, 0, 0);
+ if ((int)ret != SM_ERR_BUSY || !retry)
+ break;
+
+ dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) returned busy, retry\n",
+ __func__, smcnr, a0, a1, a2);
+ retry--;
+ }
+
+ return ret;
+}
+
+static unsigned long trusty_std_call_helper(struct device *dev,
+ unsigned long smcnr,
+ unsigned long a0, unsigned long a1,
+ unsigned long a2)
+{
+ unsigned long ret;
+ int sleep_time = 1;
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ while (true) {
+ local_irq_disable();
+ atomic_notifier_call_chain(&s->notifier, TRUSTY_CALL_PREPARE,
+ NULL);
+ ret = trusty_std_call_inner(dev, smcnr, a0, a1, a2);
+ if (ret == SM_ERR_PANIC) {
+ s->trusty_panicked = true;
+ if (IS_ENABLED(CONFIG_TRUSTY_CRASH_IS_PANIC))
+ panic("trusty crashed");
+ else
+ WARN_ONCE(1, "trusty crashed");
+ }
+
+ atomic_notifier_call_chain(&s->notifier, TRUSTY_CALL_RETURNED,
+ NULL);
+ if (ret == SM_ERR_INTERRUPTED) {
+ /*
+ * Make sure this cpu will eventually re-enter trusty
+ * even if the std_call resumes on another cpu.
+ */
+ trusty_enqueue_nop(dev, NULL);
+ }
+ local_irq_enable();
+
+ if ((int)ret != SM_ERR_BUSY)
+ break;
+
+ if (sleep_time == 256)
+ dev_warn(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) returned busy\n",
+ __func__, smcnr, a0, a1, a2);
+ dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) returned busy, wait %d ms\n",
+ __func__, smcnr, a0, a1, a2, sleep_time);
+
+ msleep(sleep_time);
+ if (sleep_time < 1000)
+ sleep_time <<= 1;
+
+ dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) retry\n",
+ __func__, smcnr, a0, a1, a2);
+ }
+
+ if (sleep_time > 256)
+ dev_warn(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) busy cleared\n",
+ __func__, smcnr, a0, a1, a2);
+
+ return ret;
+}
+
+static void trusty_std_call_cpu_idle(struct trusty_state *s)
+{
+ int ret;
+
+ ret = wait_for_completion_timeout(&s->cpu_idle_completion, HZ * 10);
+ if (!ret) {
+ dev_warn(s->dev,
+ "%s: timed out waiting for cpu idle to clear, retry anyway\n",
+ __func__);
+ }
+}
+
+s32 trusty_std_call32(struct device *dev, u32 smcnr, u32 a0, u32 a1, u32 a2)
+{
+ int ret;
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ if (WARN_ON(SMC_IS_FASTCALL(smcnr)))
+ return SM_ERR_INVALID_PARAMETERS;
+
+ if (WARN_ON(SMC_IS_SMC64(smcnr)))
+ return SM_ERR_INVALID_PARAMETERS;
+
+ if (s->trusty_panicked) {
+ /*
+ * Avoid calling the notifiers if trusty has panicked as they
+ * can trigger more calls.
+ */
+ return SM_ERR_PANIC;
+ }
+
+ if (smcnr != SMC_SC_NOP) {
+ mutex_lock(&s->smc_lock);
+ reinit_completion(&s->cpu_idle_completion);
+ }
+
+ dev_dbg(dev, "%s(0x%x 0x%x 0x%x 0x%x) started\n",
+ __func__, smcnr, a0, a1, a2);
+
+ ret = trusty_std_call_helper(dev, smcnr, a0, a1, a2);
+ while (ret == SM_ERR_INTERRUPTED || ret == SM_ERR_CPU_IDLE) {
+ dev_dbg(dev, "%s(0x%x 0x%x 0x%x 0x%x) interrupted\n",
+ __func__, smcnr, a0, a1, a2);
+ if (ret == SM_ERR_CPU_IDLE)
+ trusty_std_call_cpu_idle(s);
+ ret = trusty_std_call_helper(dev, SMC_SC_RESTART_LAST, 0, 0, 0);
+ }
+ dev_dbg(dev, "%s(0x%x 0x%x 0x%x 0x%x) returned 0x%x\n",
+ __func__, smcnr, a0, a1, a2, ret);
+
+ if (smcnr == SMC_SC_NOP)
+ complete(&s->cpu_idle_completion);
+ else
+ mutex_unlock(&s->smc_lock);
+
+ return ret;
+}
+EXPORT_SYMBOL(trusty_std_call32);
+
+int trusty_share_memory(struct device *dev, u64 *id,
+ struct scatterlist *sglist, unsigned int nents,
+ pgprot_t pgprot)
+{
+ return trusty_transfer_memory(dev, id, sglist, nents, pgprot, 0,
+ false);
+}
+EXPORT_SYMBOL(trusty_share_memory);
+
+int trusty_transfer_memory(struct device *dev, u64 *id,
+ struct scatterlist *sglist, unsigned int nents,
+ pgprot_t pgprot, u64 tag, bool lend)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+ int ret;
+ struct ns_mem_page_info pg_inf;
+ struct scatterlist *sg;
+ size_t count;
+ size_t i;
+ size_t len;
+ u64 ffa_handle = 0;
+ size_t total_len;
+ size_t endpoint_count = 1;
+ struct ffa_mtd *mtd = s->ffa_tx;
+ size_t comp_mrd_offset = offsetof(struct ffa_mtd, emad[endpoint_count]);
+ struct ffa_comp_mrd *comp_mrd = s->ffa_tx + comp_mrd_offset;
+ struct ffa_cons_mrd *cons_mrd = comp_mrd->address_range_array;
+ size_t cons_mrd_offset = (void *)cons_mrd - s->ffa_tx;
+ struct smc_ret8 smc_ret;
+ u32 cookie_low;
+ u32 cookie_high;
+
+ if (WARN_ON(dev->driver != &trusty_driver.driver))
+ return -EINVAL;
+
+ if (WARN_ON(nents < 1))
+ return -EINVAL;
+
+ if (nents != 1 && s->api_version < TRUSTY_API_VERSION_MEM_OBJ) {
+ dev_err(s->dev, "%s: old trusty version does not support non-contiguous memory objects\n",
+ __func__);
+ return -EOPNOTSUPP;
+ }
+
+ count = dma_map_sg(dev, sglist, nents, DMA_BIDIRECTIONAL);
+ if (count != nents) {
+ dev_err(s->dev, "failed to dma map sg_table\n");
+ return -EINVAL;
+ }
+
+ sg = sglist;
+ ret = trusty_encode_page_info(&pg_inf, phys_to_page(sg_dma_address(sg)),
+ pgprot);
+ if (ret) {
+ dev_err(s->dev, "%s: trusty_encode_page_info failed\n",
+ __func__);
+ goto err_encode_page_info;
+ }
+
+ if (s->api_version < TRUSTY_API_VERSION_MEM_OBJ) {
+ *id = pg_inf.compat_attr;
+ return 0;
+ }
+
+ len = 0;
+ for_each_sg(sglist, sg, nents, i)
+ len += sg_dma_len(sg);
+
+ mutex_lock(&s->share_memory_msg_lock);
+
+ mtd->sender_id = s->ffa_local_id;
+ mtd->memory_region_attributes = pg_inf.ffa_mem_attr;
+ mtd->reserved_3 = 0;
+ mtd->flags = 0;
+ mtd->handle = 0;
+ mtd->tag = tag;
+ mtd->reserved_24_27 = 0;
+ mtd->emad_count = endpoint_count;
+ for (i = 0; i < endpoint_count; i++) {
+ struct ffa_emad *emad = &mtd->emad[i];
+ /* TODO: support stream ids */
+ emad->mapd.endpoint_id = s->ffa_remote_id;
+ emad->mapd.memory_access_permissions = pg_inf.ffa_mem_perm;
+ emad->mapd.flags = 0;
+ emad->comp_mrd_offset = comp_mrd_offset;
+ emad->reserved_8_15 = 0;
+ }
+ comp_mrd->total_page_count = len / PAGE_SIZE;
+ comp_mrd->address_range_count = nents;
+ comp_mrd->reserved_8_15 = 0;
+
+ total_len = cons_mrd_offset + nents * sizeof(*cons_mrd);
+ sg = sglist;
+ while (count) {
+ size_t lcount =
+ min_t(size_t, count, (PAGE_SIZE - cons_mrd_offset) /
+ sizeof(*cons_mrd));
+ size_t fragment_len = lcount * sizeof(*cons_mrd) +
+ cons_mrd_offset;
+
+ for (i = 0; i < lcount; i++) {
+ cons_mrd[i].address = sg_dma_address(sg);
+ cons_mrd[i].page_count = sg_dma_len(sg) / PAGE_SIZE;
+ cons_mrd[i].reserved_12_15 = 0;
+ sg = sg_next(sg);
+ }
+ count -= lcount;
+ if (cons_mrd_offset) {
+ u32 smc = lend ? SMC_FC_FFA_MEM_LEND :
+ SMC_FC_FFA_MEM_SHARE;
+ /* First fragment */
+ smc_ret = trusty_smc8(smc, total_len,
+ fragment_len, 0, 0, 0, 0, 0);
+ } else {
+ smc_ret = trusty_smc8(SMC_FC_FFA_MEM_FRAG_TX,
+ cookie_low, cookie_high,
+ fragment_len, 0, 0, 0, 0);
+ }
+ if (smc_ret.r0 == SMC_FC_FFA_MEM_FRAG_RX) {
+ cookie_low = smc_ret.r1;
+ cookie_high = smc_ret.r2;
+ dev_dbg(s->dev, "cookie %x %x", cookie_low,
+ cookie_high);
+ if (!count) {
+ /*
+ * We have sent all our descriptors. Expected
+ * SMC_FC_FFA_SUCCESS, not a request to send
+ * another fragment.
+ */
+ dev_err(s->dev, "%s: fragment_len %zd/%zd, unexpected SMC_FC_FFA_MEM_FRAG_RX\n",
+ __func__, fragment_len, total_len);
+ ret = -EIO;
+ break;
+ }
+ } else if (smc_ret.r0 == SMC_FC_FFA_SUCCESS) {
+ ffa_handle = smc_ret.r2 | (u64)smc_ret.r3 << 32;
+ dev_dbg(s->dev, "%s: fragment_len %zu/%zu, got handle 0x%llx\n",
+ __func__, fragment_len, total_len,
+ ffa_handle);
+ if (count) {
+ /*
+ * We have not sent all our descriptors.
+ * Expected SMC_FC_FFA_MEM_FRAG_RX not
+ * SMC_FC_FFA_SUCCESS.
+ */
+ dev_err(s->dev, "%s: fragment_len %zu/%zu, unexpected SMC_FC_FFA_SUCCESS, count %zu != 0\n",
+ __func__, fragment_len, total_len,
+ count);
+ ret = -EIO;
+ break;
+ }
+ } else {
+ dev_err(s->dev, "%s: fragment_len %zu/%zu, SMC_FC_FFA_MEM_SHARE failed 0x%lx 0x%lx 0x%lx",
+ __func__, fragment_len, total_len,
+ smc_ret.r0, smc_ret.r1, smc_ret.r2);
+ ret = -EIO;
+ break;
+ }
+
+ cons_mrd = s->ffa_tx;
+ cons_mrd_offset = 0;
+ }
+
+ mutex_unlock(&s->share_memory_msg_lock);
+
+ if (!ret) {
+ *id = ffa_handle;
+ dev_dbg(s->dev, "%s: done\n", __func__);
+ return 0;
+ }
+
+ dev_err(s->dev, "%s: failed %d", __func__, ret);
+
+err_encode_page_info:
+ dma_unmap_sg(dev, sglist, nents, DMA_BIDIRECTIONAL);
+ return ret;
+}
+EXPORT_SYMBOL(trusty_transfer_memory);
+
+/*
+ * trusty_share_memory_compat - trusty_share_memory wrapper for old apis
+ *
+ * Call trusty_share_memory and filter out memory attributes if trusty version
+ * is old. Used by clients that used to pass just a physical address to trusty
+ * instead of a physical address plus memory attributes value.
+ */
+int trusty_share_memory_compat(struct device *dev, u64 *id,
+ struct scatterlist *sglist, unsigned int nents,
+ pgprot_t pgprot)
+{
+ int ret;
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ ret = trusty_share_memory(dev, id, sglist, nents, pgprot);
+ if (!ret && s->api_version < TRUSTY_API_VERSION_PHYS_MEM_OBJ)
+ *id &= 0x0000FFFFFFFFF000ull;
+
+ return ret;
+}
+EXPORT_SYMBOL(trusty_share_memory_compat);
+
+int trusty_reclaim_memory(struct device *dev, u64 id,
+ struct scatterlist *sglist, unsigned int nents)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+ int ret = 0;
+ struct smc_ret8 smc_ret;
+
+ if (WARN_ON(dev->driver != &trusty_driver.driver))
+ return -EINVAL;
+
+ if (WARN_ON(nents < 1))
+ return -EINVAL;
+
+ if (s->api_version < TRUSTY_API_VERSION_MEM_OBJ) {
+ if (nents != 1) {
+ dev_err(s->dev, "%s: not supported\n", __func__);
+ return -EOPNOTSUPP;
+ }
+
+ dma_unmap_sg(dev, sglist, nents, DMA_BIDIRECTIONAL);
+
+ dev_dbg(s->dev, "%s: done\n", __func__);
+ return 0;
+ }
+
+ mutex_lock(&s->share_memory_msg_lock);
+
+ smc_ret = trusty_smc8(SMC_FC_FFA_MEM_RECLAIM, (u32)id, id >> 32, 0, 0,
+ 0, 0, 0);
+ if (smc_ret.r0 != SMC_FC_FFA_SUCCESS) {
+ dev_err(s->dev, "%s: SMC_FC_FFA_MEM_RECLAIM failed 0x%lx 0x%lx 0x%lx",
+ __func__, smc_ret.r0, smc_ret.r1, smc_ret.r2);
+ if (smc_ret.r0 == SMC_FC_FFA_ERROR &&
+ smc_ret.r2 == FFA_ERROR_DENIED)
+ ret = -EBUSY;
+ else
+ ret = -EIO;
+ }
+
+ mutex_unlock(&s->share_memory_msg_lock);
+
+ if (ret != 0)
+ return ret;
+
+ dma_unmap_sg(dev, sglist, nents, DMA_BIDIRECTIONAL);
+
+ dev_dbg(s->dev, "%s: done\n", __func__);
+ return 0;
+}
+EXPORT_SYMBOL(trusty_reclaim_memory);
+
+int trusty_call_notifier_register(struct device *dev, struct notifier_block *n)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ return atomic_notifier_chain_register(&s->notifier, n);
+}
+EXPORT_SYMBOL(trusty_call_notifier_register);
+
+int trusty_call_notifier_unregister(struct device *dev,
+ struct notifier_block *n)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ return atomic_notifier_chain_unregister(&s->notifier, n);
+}
+EXPORT_SYMBOL(trusty_call_notifier_unregister);
+
+static int trusty_remove_child(struct device *dev, void *data)
+{
+ platform_device_unregister(to_platform_device(dev));
+ return 0;
+}
+
+static ssize_t trusty_version_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ return scnprintf(buf, PAGE_SIZE, "%s\n", s->version_str ?: "unknown");
+}
+
+static DEVICE_ATTR(trusty_version, 0400, trusty_version_show, NULL);
+
+static struct attribute *trusty_attrs[] = {
+ &dev_attr_trusty_version.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(trusty);
+
+const char *trusty_version_str_get(struct device *dev)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ return s->version_str;
+}
+EXPORT_SYMBOL(trusty_version_str_get);
+
+static int trusty_init_msg_buf(struct trusty_state *s, struct device *dev)
+{
+ phys_addr_t tx_paddr;
+ phys_addr_t rx_paddr;
+ int ret;
+ struct smc_ret8 smc_ret;
+
+ if (s->api_version < TRUSTY_API_VERSION_MEM_OBJ)
+ return 0;
+
+ /* Get supported FF-A version and check if it is compatible */
+ smc_ret = trusty_smc8(SMC_FC_FFA_VERSION, FFA_CURRENT_VERSION, 0, 0,
+ 0, 0, 0, 0);
+ if (FFA_VERSION_TO_MAJOR(smc_ret.r0) != FFA_CURRENT_VERSION_MAJOR) {
+ dev_err(s->dev,
+ "%s: Unsupported FF-A version 0x%lx, expected 0x%x\n",
+ __func__, smc_ret.r0, FFA_CURRENT_VERSION);
+ ret = -EIO;
+ goto err_version;
+ }
+
+ /* Check that SMC_FC_FFA_MEM_SHARE is implemented */
+ smc_ret = trusty_smc8(SMC_FC_FFA_FEATURES, SMC_FC_FFA_MEM_SHARE, 0, 0,
+ 0, 0, 0, 0);
+ if (smc_ret.r0 != SMC_FC_FFA_SUCCESS) {
+ dev_err(s->dev,
+ "%s: SMC_FC_FFA_FEATURES(SMC_FC_FFA_MEM_SHARE) failed 0x%lx 0x%lx 0x%lx\n",
+ __func__, smc_ret.r0, smc_ret.r1, smc_ret.r2);
+ ret = -EIO;
+ goto err_features;
+ }
+
+ /*
+ * Set FF-A endpoint IDs.
+ *
+ * Hardcode 0x8000 for the secure os.
+ * TODO: Use FF-A call or device tree to configure this dynamically
+ */
+ smc_ret = trusty_smc8(SMC_FC_FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0);
+ if (smc_ret.r0 != SMC_FC_FFA_SUCCESS) {
+ dev_err(s->dev,
+ "%s: SMC_FC_FFA_ID_GET failed 0x%lx 0x%lx 0x%lx\n",
+ __func__, smc_ret.r0, smc_ret.r1, smc_ret.r2);
+ ret = -EIO;
+ goto err_id_get;
+ }
+
+ s->ffa_local_id = smc_ret.r2;
+ s->ffa_remote_id = 0x8000;
+
+ s->ffa_tx = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!s->ffa_tx) {
+ ret = -ENOMEM;
+ goto err_alloc_tx;
+ }
+ tx_paddr = virt_to_phys(s->ffa_tx);
+ if (WARN_ON(tx_paddr & (PAGE_SIZE - 1))) {
+ ret = -EINVAL;
+ goto err_unaligned_tx_buf;
+ }
+
+ s->ffa_rx = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!s->ffa_rx) {
+ ret = -ENOMEM;
+ goto err_alloc_rx;
+ }
+ rx_paddr = virt_to_phys(s->ffa_rx);
+ if (WARN_ON(rx_paddr & (PAGE_SIZE - 1))) {
+ ret = -EINVAL;
+ goto err_unaligned_rx_buf;
+ }
+
+ smc_ret = trusty_smc8(SMC_FCZ_FFA_RXTX_MAP, tx_paddr, rx_paddr, 1, 0,
+ 0, 0, 0);
+ if (smc_ret.r0 != SMC_FC_FFA_SUCCESS) {
+ dev_err(s->dev, "%s: SMC_FCZ_FFA_RXTX_MAP failed 0x%lx 0x%lx 0x%lx\n",
+ __func__, smc_ret.r0, smc_ret.r1, smc_ret.r2);
+ ret = -EIO;
+ goto err_rxtx_map;
+ }
+
+ return 0;
+
+err_rxtx_map:
+err_unaligned_rx_buf:
+ kfree(s->ffa_rx);
+ s->ffa_rx = NULL;
+err_alloc_rx:
+err_unaligned_tx_buf:
+ kfree(s->ffa_tx);
+ s->ffa_tx = NULL;
+err_alloc_tx:
+err_id_get:
+err_features:
+err_version:
+ return ret;
+}
+
+static void trusty_free_msg_buf(struct trusty_state *s, struct device *dev)
+{
+ struct smc_ret8 smc_ret;
+
+ smc_ret = trusty_smc8(SMC_FC_FFA_RXTX_UNMAP, 0, 0, 0, 0, 0, 0, 0);
+ if (smc_ret.r0 != SMC_FC_FFA_SUCCESS) {
+ dev_err(s->dev, "%s: SMC_FC_FFA_RXTX_UNMAP failed 0x%lx 0x%lx 0x%lx\n",
+ __func__, smc_ret.r0, smc_ret.r1, smc_ret.r2);
+ } else {
+ kfree(s->ffa_rx);
+ kfree(s->ffa_tx);
+ }
+}
+
+static void trusty_init_version(struct trusty_state *s, struct device *dev)
+{
+ int ret;
+ int i;
+ int version_str_len;
+
+ ret = trusty_fast_call32(dev, SMC_FC_GET_VERSION_STR, -1, 0, 0);
+ if (ret <= 0)
+ goto err_get_size;
+
+ version_str_len = ret;
+
+ s->version_str = kmalloc(version_str_len + 1, GFP_KERNEL);
+ for (i = 0; i < version_str_len; i++) {
+ ret = trusty_fast_call32(dev, SMC_FC_GET_VERSION_STR, i, 0, 0);
+ if (ret < 0)
+ goto err_get_char;
+ s->version_str[i] = ret;
+ }
+ s->version_str[i] = '\0';
+
+ dev_info(dev, "trusty version: %s\n", s->version_str);
+ return;
+
+err_get_char:
+ kfree(s->version_str);
+ s->version_str = NULL;
+err_get_size:
+ dev_err(dev, "failed to get version: %d\n", ret);
+}
+
+u32 trusty_get_api_version(struct device *dev)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ return s->api_version;
+}
+EXPORT_SYMBOL(trusty_get_api_version);
+
+bool trusty_get_panic_status(struct device *dev)
+{
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+ if (WARN_ON(dev->driver != &trusty_driver.driver))
+ return false;
+ return s->trusty_panicked;
+}
+EXPORT_SYMBOL(trusty_get_panic_status);
+
+static int trusty_init_api_version(struct trusty_state *s, struct device *dev)
+{
+ u32 api_version;
+
+ api_version = trusty_fast_call32(dev, SMC_FC_API_VERSION,
+ TRUSTY_API_VERSION_CURRENT, 0, 0);
+ if (api_version == SM_ERR_UNDEFINED_SMC)
+ api_version = 0;
+
+ if (api_version > TRUSTY_API_VERSION_CURRENT) {
+ dev_err(dev, "unsupported api version %u > %u\n",
+ api_version, TRUSTY_API_VERSION_CURRENT);
+ return -EINVAL;
+ }
+
+ dev_info(dev, "selected api version: %u (requested %u)\n",
+ api_version, TRUSTY_API_VERSION_CURRENT);
+ s->api_version = api_version;
+
+ return 0;
+}
+
+static bool dequeue_nop(struct trusty_state *s, u32 *args)
+{
+ unsigned long flags;
+ struct trusty_nop *nop = NULL;
+
+ spin_lock_irqsave(&s->nop_lock, flags);
+ if (!list_empty(&s->nop_queue)) {
+ nop = list_first_entry(&s->nop_queue,
+ struct trusty_nop, node);
+ list_del_init(&nop->node);
+ args[0] = nop->args[0];
+ args[1] = nop->args[1];
+ args[2] = nop->args[2];
+ } else {
+ args[0] = 0;
+ args[1] = 0;
+ args[2] = 0;
+ }
+ spin_unlock_irqrestore(&s->nop_lock, flags);
+ return nop;
+}
+
+static void locked_nop_work_func(struct work_struct *work)
+{
+ int ret;
+ struct trusty_work *tw = container_of(work, struct trusty_work, work);
+ struct trusty_state *s = tw->ts;
+
+ ret = trusty_std_call32(s->dev, SMC_SC_LOCKED_NOP, 0, 0, 0);
+ if (ret != 0)
+ dev_err(s->dev, "%s: SMC_SC_LOCKED_NOP failed %d",
+ __func__, ret);
+
+ dev_dbg(s->dev, "%s: done\n", __func__);
+}
+
+static void nop_work_func(struct work_struct *work)
+{
+ int ret;
+ bool next;
+ u32 args[3];
+ u32 last_arg0;
+ struct trusty_work *tw = container_of(work, struct trusty_work, work);
+ struct trusty_state *s = tw->ts;
+
+ dequeue_nop(s, args);
+ do {
+ dev_dbg(s->dev, "%s: %x %x %x\n",
+ __func__, args[0], args[1], args[2]);
+
+ last_arg0 = args[0];
+ ret = trusty_std_call32(s->dev, SMC_SC_NOP,
+ args[0], args[1], args[2]);
+
+ next = dequeue_nop(s, args);
+
+ if (ret == SM_ERR_NOP_INTERRUPTED) {
+ next = true;
+ } else if (ret != SM_ERR_NOP_DONE) {
+ dev_err(s->dev, "%s: SMC_SC_NOP %x failed %d",
+ __func__, last_arg0, ret);
+ if (last_arg0) {
+ /*
+ * Don't break out of the loop if a non-default
+ * nop-handler returns an error.
+ */
+ next = true;
+ }
+ }
+ } while (next);
+
+ dev_dbg(s->dev, "%s: done\n", __func__);
+}
+
+void trusty_enqueue_nop(struct device *dev, struct trusty_nop *nop)
+{
+ unsigned long flags;
+ struct trusty_work *tw;
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ preempt_disable();
+ tw = this_cpu_ptr(s->nop_works);
+ if (nop) {
+ WARN_ON(s->api_version < TRUSTY_API_VERSION_SMP_NOP);
+
+ spin_lock_irqsave(&s->nop_lock, flags);
+ if (list_empty(&nop->node))
+ list_add_tail(&nop->node, &s->nop_queue);
+ spin_unlock_irqrestore(&s->nop_lock, flags);
+ }
+ queue_work(s->nop_wq, &tw->work);
+ preempt_enable();
+}
+EXPORT_SYMBOL(trusty_enqueue_nop);
+
+void trusty_dequeue_nop(struct device *dev, struct trusty_nop *nop)
+{
+ unsigned long flags;
+ struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
+
+ if (WARN_ON(!nop))
+ return;
+
+ spin_lock_irqsave(&s->nop_lock, flags);
+ if (!list_empty(&nop->node))
+ list_del_init(&nop->node);
+ spin_unlock_irqrestore(&s->nop_lock, flags);
+}
+EXPORT_SYMBOL(trusty_dequeue_nop);
+
+static int trusty_probe(struct platform_device *pdev)
+{
+ int ret;
+ unsigned int cpu;
+ work_func_t work_func;
+ struct trusty_state *s;
+ struct device_node *node = pdev->dev.of_node;
+
+ if (!node) {
+ dev_err(&pdev->dev, "of_node required\n");
+ return -EINVAL;
+ }
+
+ s = kzalloc(sizeof(*s), GFP_KERNEL);
+ if (!s) {
+ ret = -ENOMEM;
+ goto err_allocate_state;
+ }
+
+ s->dev = &pdev->dev;
+ spin_lock_init(&s->nop_lock);
+ INIT_LIST_HEAD(&s->nop_queue);
+ mutex_init(&s->smc_lock);
+ mutex_init(&s->share_memory_msg_lock);
+ ATOMIC_INIT_NOTIFIER_HEAD(&s->notifier);
+ init_completion(&s->cpu_idle_completion);
+
+ s->dev->dma_parms = &s->dma_parms;
+ dma_set_max_seg_size(s->dev, 0xfffff000); /* dma_parms limit */
+ /*
+ * Set dma mask to 48 bits. This is the current limit of
+ * trusty_encode_page_info.
+ */
+ dma_coerce_mask_and_coherent(s->dev, DMA_BIT_MASK(48));
+
+ platform_set_drvdata(pdev, s);
+
+ trusty_init_version(s, &pdev->dev);
+
+ ret = trusty_init_api_version(s, &pdev->dev);
+ if (ret < 0)
+ goto err_api_version;
+
+ ret = trusty_init_msg_buf(s, &pdev->dev);
+ if (ret < 0)
+ goto err_init_msg_buf;
+
+ s->nop_wq = alloc_workqueue("trusty-nop-wq", WQ_CPU_INTENSIVE, 0);
+ if (!s->nop_wq) {
+ ret = -ENODEV;
+ dev_err(&pdev->dev, "Failed create trusty-nop-wq\n");
+ goto err_create_nop_wq;
+ }
+
+ s->nop_works = alloc_percpu(struct trusty_work);
+ if (!s->nop_works) {
+ ret = -ENOMEM;
+ dev_err(&pdev->dev, "Failed to allocate works\n");
+ goto err_alloc_works;
+ }
+
+ if (s->api_version < TRUSTY_API_VERSION_SMP)
+ work_func = locked_nop_work_func;
+ else
+ work_func = nop_work_func;
+
+ for_each_possible_cpu(cpu) {
+ struct trusty_work *tw = per_cpu_ptr(s->nop_works, cpu);
+
+ tw->ts = s;
+ INIT_WORK(&tw->work, work_func);
+ }
+
+ ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "Failed to add children: %d\n", ret);
+ goto err_add_children;
+ }
+
+ return 0;
+
+err_add_children:
+ for_each_possible_cpu(cpu) {
+ struct trusty_work *tw = per_cpu_ptr(s->nop_works, cpu);
+
+ flush_work(&tw->work);
+ }
+ free_percpu(s->nop_works);
+err_alloc_works:
+ destroy_workqueue(s->nop_wq);
+err_create_nop_wq:
+ trusty_free_msg_buf(s, &pdev->dev);
+err_init_msg_buf:
+err_api_version:
+ s->dev->dma_parms = NULL;
+ kfree(s->version_str);
+ device_for_each_child(&pdev->dev, NULL, trusty_remove_child);
+ mutex_destroy(&s->share_memory_msg_lock);
+ mutex_destroy(&s->smc_lock);
+ kfree(s);
+err_allocate_state:
+ return ret;
+}
+
+static int trusty_remove(struct platform_device *pdev)
+{
+ unsigned int cpu;
+ struct trusty_state *s = platform_get_drvdata(pdev);
+
+ device_for_each_child(&pdev->dev, NULL, trusty_remove_child);
+
+ for_each_possible_cpu(cpu) {
+ struct trusty_work *tw = per_cpu_ptr(s->nop_works, cpu);
+
+ flush_work(&tw->work);
+ }
+ free_percpu(s->nop_works);
+ destroy_workqueue(s->nop_wq);
+
+ mutex_destroy(&s->share_memory_msg_lock);
+ mutex_destroy(&s->smc_lock);
+ trusty_free_msg_buf(s, &pdev->dev);
+ s->dev->dma_parms = NULL;
+ kfree(s->version_str);
+ kfree(s);
+ return 0;
+}
+
+static const struct of_device_id trusty_of_match[] = {
+ { .compatible = "android,trusty-smc-v1", },
+ {},
+};
+
+MODULE_DEVICE_TABLE(trusty, trusty_of_match);
+
+static struct platform_driver trusty_driver = {
+ .probe = trusty_probe,
+ .remove = trusty_remove,
+ .driver = {
+ .name = "trusty",
+ .of_match_table = trusty_of_match,
+ .dev_groups = trusty_groups,
+ },
+};
+
+static int __init trusty_driver_init(void)
+{
+ return platform_driver_register(&trusty_driver);
+}
+
+static void __exit trusty_driver_exit(void)
+{
+ platform_driver_unregister(&trusty_driver);
+}
+
+subsys_initcall(trusty_driver_init);
+module_exit(trusty_driver_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Trusty core driver");
diff --git a/include/linux/trusty/arm_ffa.h b/include/linux/trusty/arm_ffa.h
new file mode 100644
index 000000000000..ab7b2afb794c
--- /dev/null
+++ b/include/linux/trusty/arm_ffa.h
@@ -0,0 +1,590 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright (C) 2020 Google, Inc.
+ *
+ * Trusty and TF-A also have a copy of this header.
+ * Please keep the copies in sync.
+ */
+#ifndef __LINUX_TRUSTY_ARM_FFA_H
+#define __LINUX_TRUSTY_ARM_FFA_H
+
+/*
+ * Subset of Arm PSA Firmware Framework for Arm v8-A 1.0 EAC 1_0
+ * (https://developer.arm.com/docs/den0077/a) needed for shared memory.
+ */
+
+#include "smcall.h"
+
+#ifndef STATIC_ASSERT
+#define STATIC_ASSERT(e) _Static_assert(e, #e)
+#endif
+
+#define FFA_CURRENT_VERSION_MAJOR (1U)
+#define FFA_CURRENT_VERSION_MINOR (0U)
+
+#define FFA_VERSION_TO_MAJOR(version) ((version) >> 16)
+#define FFA_VERSION_TO_MINOR(version) ((version) & (0xffff))
+#define FFA_VERSION(major, minor) (((major) << 16) | (minor))
+#define FFA_CURRENT_VERSION \
+ FFA_VERSION(FFA_CURRENT_VERSION_MAJOR, FFA_CURRENT_VERSION_MINOR)
+
+#define SMC_ENTITY_SHARED_MEMORY 4
+
+#define SMC_FASTCALL_NR_SHARED_MEMORY(nr) \
+ SMC_FASTCALL_NR(SMC_ENTITY_SHARED_MEMORY, nr)
+#define SMC_FASTCALL64_NR_SHARED_MEMORY(nr) \
+ SMC_FASTCALL64_NR(SMC_ENTITY_SHARED_MEMORY, nr)
+
+/**
+ * typedef ffa_endpoint_id16_t - Endpoint ID
+ *
+ * Current implementation only supports VMIDs. FFA spec also support stream
+ * endpoint ids.
+ */
+typedef uint16_t ffa_endpoint_id16_t;
+
+/**
+ * struct ffa_cons_mrd - Constituent memory region descriptor
+ * @address:
+ * Start address of contiguous memory region. Must be 4K page aligned.
+ * @page_count:
+ * Number of 4K pages in region.
+ * @reserved_12_15:
+ * Reserve bytes 12-15 to pad struct size to 16 bytes.
+ */
+struct ffa_cons_mrd {
+ uint64_t address;
+ uint32_t page_count;
+ uint32_t reserved_12_15;
+};
+STATIC_ASSERT(sizeof(struct ffa_cons_mrd) == 16);
+
+/**
+ * struct ffa_comp_mrd - Composite memory region descriptor
+ * @total_page_count:
+ * Number of 4k pages in memory region. Must match sum of
+ * @address_range_array[].page_count.
+ * @address_range_count:
+ * Number of entries in @address_range_array.
+ * @reserved_8_15:
+ * Reserve bytes 8-15 to pad struct size to 16 byte alignment and
+ * make @address_range_array 16 byte aligned.
+ * @address_range_array:
+ * Array of &struct ffa_cons_mrd entries.
+ */
+struct ffa_comp_mrd {
+ uint32_t total_page_count;
+ uint32_t address_range_count;
+ uint64_t reserved_8_15;
+ struct ffa_cons_mrd address_range_array[];
+};
+STATIC_ASSERT(sizeof(struct ffa_comp_mrd) == 16);
+
+/**
+ * typedef ffa_mem_attr8_t - Memory region attributes
+ *
+ * * @FFA_MEM_ATTR_DEVICE_NGNRNE:
+ * Device-nGnRnE.
+ * * @FFA_MEM_ATTR_DEVICE_NGNRE:
+ * Device-nGnRE.
+ * * @FFA_MEM_ATTR_DEVICE_NGRE:
+ * Device-nGRE.
+ * * @FFA_MEM_ATTR_DEVICE_GRE:
+ * Device-GRE.
+ * * @FFA_MEM_ATTR_NORMAL_MEMORY_UNCACHED
+ * Normal memory. Non-cacheable.
+ * * @FFA_MEM_ATTR_NORMAL_MEMORY_CACHED_WB
+ * Normal memory. Write-back cached.
+ * * @FFA_MEM_ATTR_NON_SHAREABLE
+ * Non-shareable. Combine with FFA_MEM_ATTR_NORMAL_MEMORY_*.
+ * * @FFA_MEM_ATTR_OUTER_SHAREABLE
+ * Outer Shareable. Combine with FFA_MEM_ATTR_NORMAL_MEMORY_*.
+ * * @FFA_MEM_ATTR_INNER_SHAREABLE
+ * Inner Shareable. Combine with FFA_MEM_ATTR_NORMAL_MEMORY_*.
+ */
+typedef uint8_t ffa_mem_attr8_t;
+#define FFA_MEM_ATTR_DEVICE_NGNRNE ((1U << 4) | (0x0U << 2))
+#define FFA_MEM_ATTR_DEVICE_NGNRE ((1U << 4) | (0x1U << 2))
+#define FFA_MEM_ATTR_DEVICE_NGRE ((1U << 4) | (0x2U << 2))
+#define FFA_MEM_ATTR_DEVICE_GRE ((1U << 4) | (0x3U << 2))
+#define FFA_MEM_ATTR_NORMAL_MEMORY_UNCACHED ((2U << 4) | (0x1U << 2))
+#define FFA_MEM_ATTR_NORMAL_MEMORY_CACHED_WB ((2U << 4) | (0x3U << 2))
+#define FFA_MEM_ATTR_NON_SHAREABLE (0x0U << 0)
+#define FFA_MEM_ATTR_OUTER_SHAREABLE (0x2U << 0)
+#define FFA_MEM_ATTR_INNER_SHAREABLE (0x3U << 0)
+
+/**
+ * typedef ffa_mem_perm8_t - Memory access permissions
+ *
+ * * @FFA_MEM_ATTR_RO
+ * Request or specify read-only mapping.
+ * * @FFA_MEM_ATTR_RW
+ * Request or allow read-write mapping.
+ * * @FFA_MEM_PERM_NX
+ * Deny executable mapping.
+ * * @FFA_MEM_PERM_X
+ * Request executable mapping.
+ */
+typedef uint8_t ffa_mem_perm8_t;
+#define FFA_MEM_PERM_RO (1U << 0)
+#define FFA_MEM_PERM_RW (1U << 1)
+#define FFA_MEM_PERM_NX (1U << 2)
+#define FFA_MEM_PERM_X (1U << 3)
+
+/**
+ * typedef ffa_mem_flag8_t - Endpoint memory flags
+ *
+ * * @FFA_MEM_FLAG_OTHER
+ * Other borrower. Memory region must not be or was not retrieved on behalf
+ * of this endpoint.
+ */
+typedef uint8_t ffa_mem_flag8_t;
+#define FFA_MEM_FLAG_OTHER (1U << 0)
+
+/**
+ * typedef ffa_mtd_flag32_t - Memory transaction descriptor flags
+ *
+ * * @FFA_MTD_FLAG_ZERO_MEMORY
+ * Zero memory after unmapping from sender (must be 0 for share).
+ * * @FFA_MTD_FLAG_TIME_SLICING
+ * Not supported by this implementation.
+ * * @FFA_MTD_FLAG_ZERO_MEMORY_AFTER_RELINQUISH
+ * Zero memory after unmapping from borrowers (must be 0 for share).
+ * * @FFA_MTD_FLAG_TYPE_MASK
+ * Bit-mask to extract memory management transaction type from flags.
+ * * @FFA_MTD_FLAG_TYPE_SHARE_MEMORY
+ * Share memory transaction flag.
+ * Used by @SMC_FC_FFA_MEM_RETRIEVE_RESP to indicate that memory came from
+ * @SMC_FC_FFA_MEM_SHARE and by @SMC_FC_FFA_MEM_RETRIEVE_REQ to specify that
+ * it must have.
+ * * @FFA_MTD_FLAG_ADDRESS_RANGE_ALIGNMENT_HINT_MASK
+ * Not supported by this implementation.
+ */
+typedef uint32_t ffa_mtd_flag32_t;
+#define FFA_MTD_FLAG_ZERO_MEMORY (1U << 0)
+#define FFA_MTD_FLAG_TIME_SLICING (1U << 1)
+#define FFA_MTD_FLAG_ZERO_MEMORY_AFTER_RELINQUISH (1U << 2)
+#define FFA_MTD_FLAG_TYPE_MASK (3U << 3)
+#define FFA_MTD_FLAG_TYPE_SHARE_MEMORY (1U << 3)
+#define FFA_MTD_FLAG_ADDRESS_RANGE_ALIGNMENT_HINT_MASK (0x1FU << 5)
+
+/**
+ * struct ffa_mapd - Memory access permissions descriptor
+ * @endpoint_id:
+ * Endpoint id that @memory_access_permissions and @flags apply to.
+ * (&typedef ffa_endpoint_id16_t).
+ * @memory_access_permissions:
+ * FFA_MEM_PERM_* values or'ed together (&typedef ffa_mem_perm8_t).
+ * @flags:
+ * FFA_MEM_FLAG_* values or'ed together (&typedef ffa_mem_flag8_t).
+ */
+struct ffa_mapd {
+ ffa_endpoint_id16_t endpoint_id;
+ ffa_mem_perm8_t memory_access_permissions;
+ ffa_mem_flag8_t flags;
+};
+STATIC_ASSERT(sizeof(struct ffa_mapd) == 4);
+
+/**
+ * struct ffa_emad - Endpoint memory access descriptor.
+ * @mapd: &struct ffa_mapd.
+ * @comp_mrd_offset:
+ * Offset of &struct ffa_comp_mrd form start of &struct ffa_mtd.
+ * @reserved_8_15:
+ * Reserved bytes 8-15. Must be 0.
+ */
+struct ffa_emad {
+ struct ffa_mapd mapd;
+ uint32_t comp_mrd_offset;
+ uint64_t reserved_8_15;
+};
+STATIC_ASSERT(sizeof(struct ffa_emad) == 16);
+
+/**
+ * struct ffa_mtd - Memory transaction descriptor.
+ * @sender_id:
+ * Sender endpoint id.
+ * @memory_region_attributes:
+ * FFA_MEM_ATTR_* values or'ed together (&typedef ffa_mem_attr8_t).
+ * @reserved_3:
+ * Reserved bytes 3. Must be 0.
+ * @flags:
+ * FFA_MTD_FLAG_* values or'ed together (&typedef ffa_mtd_flag32_t).
+ * @handle:
+ * Id of shared memory object. Most be 0 for MEM_SHARE.
+ * @tag: Client allocated tag. Must match original value.
+ * @reserved_24_27:
+ * Reserved bytes 24-27. Must be 0.
+ * @emad_count:
+ * Number of entries in @emad. Must be 1 in current implementation.
+ * FFA spec allows more entries.
+ * @emad:
+ * Endpoint memory access descriptor array (see @struct ffa_emad).
+ */
+struct ffa_mtd {
+ ffa_endpoint_id16_t sender_id;
+ ffa_mem_attr8_t memory_region_attributes;
+ uint8_t reserved_3;
+ ffa_mtd_flag32_t flags;
+ uint64_t handle;
+ uint64_t tag;
+ uint32_t reserved_24_27;
+ uint32_t emad_count;
+ struct ffa_emad emad[];
+};
+STATIC_ASSERT(sizeof(struct ffa_mtd) == 32);
+
+/**
+ * struct ffa_mem_relinquish_descriptor - Relinquish request descriptor.
+ * @handle:
+ * Id of shared memory object to relinquish.
+ * @flags:
+ * If bit 0 is set clear memory after unmapping from borrower. Must be 0
+ * for share. Bit[1]: Time slicing. Not supported, must be 0. All other
+ * bits are reserved 0.
+ * @endpoint_count:
+ * Number of entries in @endpoint_array.
+ * @endpoint_array:
+ * Array of endpoint ids.
+ */
+struct ffa_mem_relinquish_descriptor {
+ uint64_t handle;
+ uint32_t flags;
+ uint32_t endpoint_count;
+ ffa_endpoint_id16_t endpoint_array[];
+};
+STATIC_ASSERT(sizeof(struct ffa_mem_relinquish_descriptor) == 16);
+
+/**
+ * enum ffa_error - FF-A error code
+ * @FFA_ERROR_NOT_SUPPORTED:
+ * Operation contained possibly valid parameters not supported by the
+ * current implementation. Does not match FF-A 1.0 EAC 1_0 definition.
+ * @FFA_ERROR_INVALID_PARAMETERS:
+ * Invalid parameters. Conditions function specific.
+ * @FFA_ERROR_NO_MEMORY:
+ * Not enough memory.
+ * @FFA_ERROR_DENIED:
+ * Operation not allowed. Conditions function specific.
+ *
+ * FF-A 1.0 EAC 1_0 defines other error codes as well but the current
+ * implementation does not use them.
+ */
+enum ffa_error {
+ FFA_ERROR_NOT_SUPPORTED = -1,
+ FFA_ERROR_INVALID_PARAMETERS = -2,
+ FFA_ERROR_NO_MEMORY = -3,
+ FFA_ERROR_DENIED = -6,
+};
+
+/**
+ * SMC_FC32_FFA_MIN - First 32 bit SMC opcode reserved for FFA
+ */
+#define SMC_FC32_FFA_MIN SMC_FASTCALL_NR_SHARED_MEMORY(0x60)
+
+/**
+ * SMC_FC32_FFA_MAX - Last 32 bit SMC opcode reserved for FFA
+ */
+#define SMC_FC32_FFA_MAX SMC_FASTCALL_NR_SHARED_MEMORY(0x7F)
+
+/**
+ * SMC_FC64_FFA_MIN - First 64 bit SMC opcode reserved for FFA
+ */
+#define SMC_FC64_FFA_MIN SMC_FASTCALL64_NR_SHARED_MEMORY(0x60)
+
+/**
+ * SMC_FC64_FFA_MAX - Last 64 bit SMC opcode reserved for FFA
+ */
+#define SMC_FC64_FFA_MAX SMC_FASTCALL64_NR_SHARED_MEMORY(0x7F)
+
+/**
+ * SMC_FC_FFA_ERROR - SMC error return opcode
+ *
+ * Register arguments:
+ *
+ * * w1: VMID in [31:16], vCPU in [15:0]
+ * * w2: Error code (&enum ffa_error)
+ */
+#define SMC_FC_FFA_ERROR SMC_FASTCALL_NR_SHARED_MEMORY(0x60)
+
+/**
+ * SMC_FC_FFA_SUCCESS - 32 bit SMC success return opcode
+ *
+ * Register arguments:
+ *
+ * * w1: VMID in [31:16], vCPU in [15:0]
+ * * w2-w7: Function specific
+ */
+#define SMC_FC_FFA_SUCCESS SMC_FASTCALL_NR_SHARED_MEMORY(0x61)
+
+/**
+ * SMC_FC64_FFA_SUCCESS - 64 bit SMC success return opcode
+ *
+ * Register arguments:
+ *
+ * * w1: VMID in [31:16], vCPU in [15:0]
+ * * w2/x2-w7/x7: Function specific
+ */
+#define SMC_FC64_FFA_SUCCESS SMC_FASTCALL64_NR_SHARED_MEMORY(0x61)
+
+/**
+ * SMC_FC_FFA_VERSION - SMC opcode to return supported FF-A version
+ *
+ * Register arguments:
+ *
+ * * w1: Major version bit[30:16] and minor version in bit[15:0] supported
+ * by caller. Bit[31] must be 0.
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ * * w2: Major version bit[30:16], minor version in bit[15:0], bit[31] must
+ * be 0.
+ *
+ * or
+ *
+ * * w0: SMC_FC_FFA_ERROR
+ * * w2: FFA_ERROR_NOT_SUPPORTED if major version passed in is less than the
+ * minimum major version supported.
+ */
+#define SMC_FC_FFA_VERSION SMC_FASTCALL_NR_SHARED_MEMORY(0x63)
+
+/**
+ * SMC_FC_FFA_FEATURES - SMC opcode to check optional feature support
+ *
+ * Register arguments:
+ *
+ * * w1: FF-A function ID
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ * * w2: Bit[0]: Supports custom buffers for memory transactions.
+ * Bit[1:0]: For RXTX_MAP min buffer size and alignment boundary.
+ * Other bits must be 0.
+ * * w3: For FFA_MEM_RETRIEVE_REQ, bit[7-0]: Number of times receiver can
+ * retrieve each memory region before relinquishing it specified as
+ * ((1U << (value + 1)) - 1 (or value = bits in reference count - 1).
+ * For all other bits and commands: must be 0.
+ * or
+ *
+ * * w0: SMC_FC_FFA_ERROR
+ * * w2: FFA_ERROR_NOT_SUPPORTED if function is not implemented, or
+ * FFA_ERROR_INVALID_PARAMETERS if function id is not valid.
+ */
+#define SMC_FC_FFA_FEATURES SMC_FASTCALL_NR_SHARED_MEMORY(0x64)
+
+/**
+ * SMC_FC_FFA_RXTX_MAP - 32 bit SMC opcode to map message buffers
+ *
+ * Register arguments:
+ *
+ * * w1: TX address
+ * * w2: RX address
+ * * w3: RX/TX page count in bit[5:0]
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ */
+#define SMC_FC_FFA_RXTX_MAP SMC_FASTCALL_NR_SHARED_MEMORY(0x66)
+
+/**
+ * SMC_FC64_FFA_RXTX_MAP - 64 bit SMC opcode to map message buffers
+ *
+ * Register arguments:
+ *
+ * * x1: TX address
+ * * x2: RX address
+ * * x3: RX/TX page count in bit[5:0]
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ */
+#define SMC_FC64_FFA_RXTX_MAP SMC_FASTCALL64_NR_SHARED_MEMORY(0x66)
+#ifdef CONFIG_64BIT
+#define SMC_FCZ_FFA_RXTX_MAP SMC_FC64_FFA_RXTX_MAP
+#else
+#define SMC_FCZ_FFA_RXTX_MAP SMC_FC_FFA_RXTX_MAP
+#endif
+
+/**
+ * SMC_FC_FFA_RXTX_UNMAP - SMC opcode to unmap message buffers
+ *
+ * Register arguments:
+ *
+ * * w1: ID in [31:16]
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ */
+#define SMC_FC_FFA_RXTX_UNMAP SMC_FASTCALL_NR_SHARED_MEMORY(0x67)
+
+/**
+ * SMC_FC_FFA_ID_GET - SMC opcode to get endpoint id of caller
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ * * w2: ID in bit[15:0], bit[31:16] must be 0.
+ */
+#define SMC_FC_FFA_ID_GET SMC_FASTCALL_NR_SHARED_MEMORY(0x69)
+
+/**
+ * SMC_FC_FFA_MEM_DONATE - 32 bit SMC opcode to donate memory
+ *
+ * Not supported.
+ */
+#define SMC_FC_FFA_MEM_DONATE SMC_FASTCALL_NR_SHARED_MEMORY(0x71)
+
+/**
+ * SMC_FC_FFA_MEM_LEND - 32 bit SMC opcode to lend memory
+ *
+ * Not currently supported.
+ */
+#define SMC_FC_FFA_MEM_LEND SMC_FASTCALL_NR_SHARED_MEMORY(0x72)
+
+/**
+ * SMC_FC_FFA_MEM_SHARE - 32 bit SMC opcode to share memory
+ *
+ * Register arguments:
+ *
+ * * w1: Total length
+ * * w2: Fragment length
+ * * w3: Address
+ * * w4: Page count
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ * * w2/w3: Handle
+ *
+ * or
+ *
+ * * w0: &SMC_FC_FFA_MEM_FRAG_RX
+ * * w1-: See &SMC_FC_FFA_MEM_FRAG_RX
+ *
+ * or
+ *
+ * * w0: SMC_FC_FFA_ERROR
+ * * w2: Error code (&enum ffa_error)
+ */
+#define SMC_FC_FFA_MEM_SHARE SMC_FASTCALL_NR_SHARED_MEMORY(0x73)
+
+/**
+ * SMC_FC64_FFA_MEM_SHARE - 64 bit SMC opcode to share memory
+ *
+ * Register arguments:
+ *
+ * * w1: Total length
+ * * w2: Fragment length
+ * * x3: Address
+ * * w4: Page count
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ * * w2/w3: Handle
+ *
+ * or
+ *
+ * * w0: &SMC_FC_FFA_MEM_FRAG_RX
+ * * w1-: See &SMC_FC_FFA_MEM_FRAG_RX
+ *
+ * or
+ *
+ * * w0: SMC_FC_FFA_ERROR
+ * * w2: Error code (&enum ffa_error)
+ */
+#define SMC_FC64_FFA_MEM_SHARE SMC_FASTCALL64_NR_SHARED_MEMORY(0x73)
+
+/**
+ * SMC_FC_FFA_MEM_RETRIEVE_REQ - 32 bit SMC opcode to retrieve shared memory
+ *
+ * Register arguments:
+ *
+ * * w1: Total length
+ * * w2: Fragment length
+ * * w3: Address
+ * * w4: Page count
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_MEM_RETRIEVE_RESP
+ * * w1/x1-w5/x5: See &SMC_FC_FFA_MEM_RETRIEVE_RESP
+ */
+#define SMC_FC_FFA_MEM_RETRIEVE_REQ SMC_FASTCALL_NR_SHARED_MEMORY(0x74)
+
+/**
+ * SMC_FC64_FFA_MEM_RETRIEVE_REQ - 64 bit SMC opcode to retrieve shared memory
+ *
+ * Register arguments:
+ *
+ * * w1: Total length
+ * * w2: Fragment length
+ * * x3: Address
+ * * w4: Page count
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_MEM_RETRIEVE_RESP
+ * * w1/x1-w5/x5: See &SMC_FC_FFA_MEM_RETRIEVE_RESP
+ */
+#define SMC_FC64_FFA_MEM_RETRIEVE_REQ SMC_FASTCALL64_NR_SHARED_MEMORY(0x74)
+
+/**
+ * SMC_FC_FFA_MEM_RETRIEVE_RESP - Retrieve 32 bit SMC return opcode
+ *
+ * Register arguments:
+ *
+ * * w1: Total length
+ * * w2: Fragment length
+ */
+#define SMC_FC_FFA_MEM_RETRIEVE_RESP SMC_FASTCALL_NR_SHARED_MEMORY(0x75)
+
+/**
+ * SMC_FC_FFA_MEM_RELINQUISH - SMC opcode to relinquish shared memory
+ *
+ * Input in &struct ffa_mem_relinquish_descriptor format in message buffer.
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ */
+#define SMC_FC_FFA_MEM_RELINQUISH SMC_FASTCALL_NR_SHARED_MEMORY(0x76)
+
+/**
+ * SMC_FC_FFA_MEM_RECLAIM - SMC opcode to reclaim shared memory
+ *
+ * Register arguments:
+ *
+ * * w1/w2: Handle
+ * * w3: Flags
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_SUCCESS
+ */
+#define SMC_FC_FFA_MEM_RECLAIM SMC_FASTCALL_NR_SHARED_MEMORY(0x77)
+
+/**
+ * SMC_FC_FFA_MEM_FRAG_RX - SMC opcode to request next fragment.
+ *
+ * Register arguments:
+ *
+ * * w1/w2: Cookie
+ * * w3: Fragment offset.
+ * * w4: Endpoint id ID in [31:16], if client is hypervisor.
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_MEM_FRAG_TX
+ * * w1/x1-w5/x5: See &SMC_FC_FFA_MEM_FRAG_TX
+ */
+#define SMC_FC_FFA_MEM_FRAG_RX SMC_FASTCALL_NR_SHARED_MEMORY(0x7A)
+
+/**
+ * SMC_FC_FFA_MEM_FRAG_TX - SMC opcode to transmit next fragment
+ *
+ * Register arguments:
+ *
+ * * w1/w2: Cookie
+ * * w3: Fragment length.
+ * * w4: Sender endpoint id ID in [31:16], if client is hypervisor.
+ *
+ * Return:
+ * * w0: &SMC_FC_FFA_MEM_FRAG_RX or &SMC_FC_FFA_SUCCESS.
+ * * w1/x1-w5/x5: See opcode in w0.
+ */
+#define SMC_FC_FFA_MEM_FRAG_TX SMC_FASTCALL_NR_SHARED_MEMORY(0x7B)
+
+#endif /* __LINUX_TRUSTY_ARM_FFA_H */
diff --git a/include/linux/trusty/sm_err.h b/include/linux/trusty/sm_err.h
new file mode 100644
index 000000000000..f6504448c6c3
--- /dev/null
+++ b/include/linux/trusty/sm_err.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright (c) 2013 Google Inc. All rights reserved
+ *
+ * Trusty and TF-A also have a copy of this header.
+ * Please keep the copies in sync.
+ */
+#ifndef __LINUX_TRUSTY_SM_ERR_H
+#define __LINUX_TRUSTY_SM_ERR_H
+
+/* Errors from the secure monitor */
+#define SM_ERR_UNDEFINED_SMC 0xFFFFFFFF /* Unknown SMC (defined by ARM DEN 0028A(0.9.0) */
+#define SM_ERR_INVALID_PARAMETERS -2
+#define SM_ERR_INTERRUPTED -3 /* Got interrupted. Call back with restart SMC */
+#define SM_ERR_UNEXPECTED_RESTART -4 /* Got an restart SMC when we didn't expect it */
+#define SM_ERR_BUSY -5 /* Temporarily busy. Call back with original args */
+#define SM_ERR_INTERLEAVED_SMC -6 /* Got a trusted_service SMC when a restart SMC is required */
+#define SM_ERR_INTERNAL_FAILURE -7 /* Unknown error */
+#define SM_ERR_NOT_SUPPORTED -8
+#define SM_ERR_NOT_ALLOWED -9 /* SMC call not allowed */
+#define SM_ERR_END_OF_INPUT -10
+#define SM_ERR_PANIC -11 /* Secure OS crashed */
+#define SM_ERR_FIQ_INTERRUPTED -12 /* Got interrupted by FIQ. Call back with SMC_SC_RESTART_FIQ on same CPU */
+#define SM_ERR_CPU_IDLE -13 /* SMC call waiting for another CPU */
+#define SM_ERR_NOP_INTERRUPTED -14 /* Got interrupted. Call back with new SMC_SC_NOP */
+#define SM_ERR_NOP_DONE -15 /* Cpu idle after SMC_SC_NOP (not an error) */
+
+#endif
diff --git a/include/linux/trusty/smcall.h b/include/linux/trusty/smcall.h
new file mode 100644
index 000000000000..aea3f6068593
--- /dev/null
+++ b/include/linux/trusty/smcall.h
@@ -0,0 +1,124 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright (c) 2013-2014 Google Inc. All rights reserved
+ *
+ * Trusty and TF-A also have a copy of this header.
+ * Please keep the copies in sync.
+ */
+#ifndef __LINUX_TRUSTY_SMCALL_H
+#define __LINUX_TRUSTY_SMCALL_H
+
+#define SMC_NUM_ENTITIES 64
+#define SMC_NUM_ARGS 4
+#define SMC_NUM_PARAMS (SMC_NUM_ARGS - 1)
+
+#define SMC_IS_FASTCALL(smc_nr) ((smc_nr) & 0x80000000)
+#define SMC_IS_SMC64(smc_nr) ((smc_nr) & 0x40000000)
+#define SMC_ENTITY(smc_nr) (((smc_nr) & 0x3F000000) >> 24)
+#define SMC_FUNCTION(smc_nr) ((smc_nr) & 0x0000FFFF)
+
+#define SMC_NR(entity, fn, fastcall, smc64) ((((fastcall) & 0x1U) << 31) | \
+ (((smc64) & 0x1U) << 30) | \
+ (((entity) & 0x3FU) << 24) | \
+ ((fn) & 0xFFFFU) \
+ )
+
+#define SMC_FASTCALL_NR(entity, fn) SMC_NR((entity), (fn), 1, 0)
+#define SMC_STDCALL_NR(entity, fn) SMC_NR((entity), (fn), 0, 0)
+#define SMC_FASTCALL64_NR(entity, fn) SMC_NR((entity), (fn), 1, 1)
+#define SMC_STDCALL64_NR(entity, fn) SMC_NR((entity), (fn), 0, 1)
+
+#define SMC_ENTITY_ARCH 0 /* ARM Architecture calls */
+#define SMC_ENTITY_CPU 1 /* CPU Service calls */
+#define SMC_ENTITY_SIP 2 /* SIP Service calls */
+#define SMC_ENTITY_OEM 3 /* OEM Service calls */
+#define SMC_ENTITY_STD 4 /* Standard Service calls */
+#define SMC_ENTITY_RESERVED 5 /* Reserved for future use */
+#define SMC_ENTITY_TRUSTED_APP 48 /* Trusted Application calls */
+#define SMC_ENTITY_TRUSTED_OS 50 /* Trusted OS calls */
+#define SMC_ENTITY_LOGGING 51 /* Used for secure -> nonsecure logging */
+#define SMC_ENTITY_TEST 52 /* Used for secure -> nonsecure tests */
+#define SMC_ENTITY_SECURE_MONITOR 60 /* Trusted OS calls internal to secure monitor */
+
+/* FC = Fast call, SC = Standard call */
+#define SMC_SC_RESTART_LAST SMC_STDCALL_NR(SMC_ENTITY_SECURE_MONITOR, 0)
+#define SMC_SC_LOCKED_NOP SMC_STDCALL_NR(SMC_ENTITY_SECURE_MONITOR, 1)
+
+/**
+ * SMC_SC_RESTART_FIQ - Re-enter trusty after it was interrupted by an fiq
+ *
+ * No arguments, no return value.
+ *
+ * Re-enter trusty after returning to ns to process an fiq. Must be called iff
+ * trusty returns SM_ERR_FIQ_INTERRUPTED.
+ *
+ * Enable by selecting api version TRUSTY_API_VERSION_RESTART_FIQ (1) or later.
+ */
+#define SMC_SC_RESTART_FIQ SMC_STDCALL_NR(SMC_ENTITY_SECURE_MONITOR, 2)
+
+/**
+ * SMC_SC_NOP - Enter trusty to run pending work.
+ *
+ * No arguments.
+ *
+ * Returns SM_ERR_NOP_INTERRUPTED or SM_ERR_NOP_DONE.
+ * If SM_ERR_NOP_INTERRUPTED is returned, the call must be repeated.
+ *
+ * Enable by selecting api version TRUSTY_API_VERSION_SMP (2) or later.
+ */
+#define SMC_SC_NOP SMC_STDCALL_NR(SMC_ENTITY_SECURE_MONITOR, 3)
+
+/*
+ * Return from secure os to non-secure os with return value in r1
+ */
+#define SMC_SC_NS_RETURN SMC_STDCALL_NR(SMC_ENTITY_SECURE_MONITOR, 0)
+
+#define SMC_FC_RESERVED SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 0)
+#define SMC_FC_FIQ_EXIT SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 1)
+#define SMC_FC_REQUEST_FIQ SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 2)
+
+#define TRUSTY_IRQ_TYPE_NORMAL (0)
+#define TRUSTY_IRQ_TYPE_PER_CPU (1)
+#define TRUSTY_IRQ_TYPE_DOORBELL (2)
+#define SMC_FC_GET_NEXT_IRQ SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 3)
+
+#define SMC_FC_CPU_SUSPEND SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 7)
+#define SMC_FC_CPU_RESUME SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 8)
+
+#define SMC_FC_AARCH_SWITCH SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 9)
+#define SMC_FC_GET_VERSION_STR SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 10)
+
+/**
+ * SMC_FC_API_VERSION - Find and select supported API version.
+ *
+ * @r1: Version supported by client.
+ *
+ * Returns version supported by trusty.
+ *
+ * If multiple versions are supported, the client should start by calling
+ * SMC_FC_API_VERSION with the largest version it supports. Trusty will then
+ * return a version it supports. If the client does not support the version
+ * returned by trusty and the version returned is less than the version
+ * requested, repeat the call with the largest supported version less than the
+ * last returned version.
+ *
+ * This call must be made before any calls that are affected by the api version.
+ */
+#define TRUSTY_API_VERSION_RESTART_FIQ (1)
+#define TRUSTY_API_VERSION_SMP (2)
+#define TRUSTY_API_VERSION_SMP_NOP (3)
+#define TRUSTY_API_VERSION_PHYS_MEM_OBJ (4)
+#define TRUSTY_API_VERSION_MEM_OBJ (5)
+#define TRUSTY_API_VERSION_CURRENT (5)
+#define SMC_FC_API_VERSION SMC_FASTCALL_NR(SMC_ENTITY_SECURE_MONITOR, 11)
+
+/* TRUSTED_OS entity calls */
+#define SMC_SC_VIRTIO_GET_DESCR SMC_STDCALL_NR(SMC_ENTITY_TRUSTED_OS, 20)
+#define SMC_SC_VIRTIO_START SMC_STDCALL_NR(SMC_ENTITY_TRUSTED_OS, 21)
+#define SMC_SC_VIRTIO_STOP SMC_STDCALL_NR(SMC_ENTITY_TRUSTED_OS, 22)
+
+#define SMC_SC_VDEV_RESET SMC_STDCALL_NR(SMC_ENTITY_TRUSTED_OS, 23)
+#define SMC_SC_VDEV_KICK_VQ SMC_STDCALL_NR(SMC_ENTITY_TRUSTED_OS, 24)
+#define SMC_NC_VDEV_KICK_VQ SMC_STDCALL_NR(SMC_ENTITY_TRUSTED_OS, 25)
+
+#endif /* __LINUX_TRUSTY_SMCALL_H */
diff --git a/include/linux/trusty/trusty.h b/include/linux/trusty/trusty.h
new file mode 100644
index 000000000000..efbb36999a8b
--- /dev/null
+++ b/include/linux/trusty/trusty.h
@@ -0,0 +1,131 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2013 Google, Inc.
+ */
+#ifndef __LINUX_TRUSTY_TRUSTY_H
+#define __LINUX_TRUSTY_TRUSTY_H
+
+#include <linux/kernel.h>
+#include <linux/trusty/sm_err.h>
+#include <linux/types.h>
+#include <linux/device.h>
+#include <linux/pagemap.h>
+
+
+#if IS_ENABLED(CONFIG_TRUSTY)
+s32 trusty_std_call32(struct device *dev, u32 smcnr, u32 a0, u32 a1, u32 a2);
+s32 trusty_fast_call32(struct device *dev, u32 smcnr, u32 a0, u32 a1, u32 a2);
+#ifdef CONFIG_64BIT
+s64 trusty_fast_call64(struct device *dev, u64 smcnr, u64 a0, u64 a1, u64 a2);
+#endif
+#else
+static inline s32 trusty_std_call32(struct device *dev, u32 smcnr,
+ u32 a0, u32 a1, u32 a2)
+{
+ return SM_ERR_UNDEFINED_SMC;
+}
+static inline s32 trusty_fast_call32(struct device *dev, u32 smcnr,
+ u32 a0, u32 a1, u32 a2)
+{
+ return SM_ERR_UNDEFINED_SMC;
+}
+#ifdef CONFIG_64BIT
+static inline s64 trusty_fast_call64(struct device *dev,
+ u64 smcnr, u64 a0, u64 a1, u64 a2)
+{
+ return SM_ERR_UNDEFINED_SMC;
+}
+#endif
+#endif
+
+struct notifier_block;
+enum {
+ TRUSTY_CALL_PREPARE,
+ TRUSTY_CALL_RETURNED,
+};
+int trusty_call_notifier_register(struct device *dev,
+ struct notifier_block *n);
+int trusty_call_notifier_unregister(struct device *dev,
+ struct notifier_block *n);
+const char *trusty_version_str_get(struct device *dev);
+u32 trusty_get_api_version(struct device *dev);
+bool trusty_get_panic_status(struct device *dev);
+
+struct ns_mem_page_info {
+ u64 paddr;
+ u8 ffa_mem_attr;
+ u8 ffa_mem_perm;
+ u64 compat_attr;
+};
+
+int trusty_encode_page_info(struct ns_mem_page_info *inf,
+ struct page *page, pgprot_t pgprot);
+
+struct scatterlist;
+typedef u64 trusty_shared_mem_id_t;
+int trusty_share_memory(struct device *dev, trusty_shared_mem_id_t *id,
+ struct scatterlist *sglist, unsigned int nents,
+ pgprot_t pgprot);
+int trusty_share_memory_compat(struct device *dev, trusty_shared_mem_id_t *id,
+ struct scatterlist *sglist, unsigned int nents,
+ pgprot_t pgprot);
+int trusty_transfer_memory(struct device *dev, u64 *id,
+ struct scatterlist *sglist, unsigned int nents,
+ pgprot_t pgprot, u64 tag, bool lend);
+int trusty_reclaim_memory(struct device *dev, trusty_shared_mem_id_t id,
+ struct scatterlist *sglist, unsigned int nents);
+
+struct dma_buf;
+#ifdef CONFIG_TRUSTY_DMA_BUF_FFA_TAG
+u64 trusty_dma_buf_get_ffa_tag(struct dma_buf *dma_buf);
+#else
+static inline u64 trusty_dma_buf_get_ffa_tag(struct dma_buf *dma_buf)
+{
+ return 0;
+}
+#endif
+
+/* Invalid handle value is defined by FF-A spec */
+#ifdef CONFIG_TRUSTY_DMA_BUF_SHARED_MEM_ID
+/**
+ * trusty_dma_buf_get_shared_mem_id() - Get memory ID corresponding to a dma_buf
+ * @dma_buf: DMA buffer
+ * @id: Pointer to output trusty_shared_mem_id_t
+ *
+ * Sets @id to trusty_shared_mem_id_t corresponding to the given @dma_buf.
+ * @dma_buf "owns" the ID, i.e. is responsible for allocating/releasing it.
+ * @dma_buf with an allocated @id must be in secure memory and should only be
+ * sent to Trusty using TRUSTY_SEND_SECURE.
+ *
+ * Return:
+ * * 0 - success
+ * * -ENODATA - @dma_buf does not own a trusty_shared_mem_id_t
+ * * ... - @dma_buf should not be lent or shared
+ */
+int trusty_dma_buf_get_shared_mem_id(struct dma_buf *dma_buf,
+ trusty_shared_mem_id_t *id);
+#else
+static inline int trusty_dma_buf_get_shared_mem_id(struct dma_buf *dma_buf,
+ trusty_shared_mem_id_t *id)
+{
+ return -ENODATA;
+}
+#endif
+
+struct trusty_nop {
+ struct list_head node;
+ u32 args[3];
+};
+
+static inline void trusty_nop_init(struct trusty_nop *nop,
+ u32 arg0, u32 arg1, u32 arg2) {
+ INIT_LIST_HEAD(&nop->node);
+ nop->args[0] = arg0;
+ nop->args[1] = arg1;
+ nop->args[2] = arg2;
+}
+
+void trusty_enqueue_nop(struct device *dev, struct trusty_nop *nop);
+void trusty_dequeue_nop(struct device *dev, struct trusty_nop *nop);
+
+#endif
diff --git a/include/linux/trusty/trusty_ipc.h b/include/linux/trusty/trusty_ipc.h
new file mode 100644
index 000000000000..9386392f3a64
--- /dev/null
+++ b/include/linux/trusty/trusty_ipc.h
@@ -0,0 +1,89 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2015 Google, Inc.
+ */
+#ifndef __LINUX_TRUSTY_TRUSTY_IPC_H
+#define __LINUX_TRUSTY_TRUSTY_IPC_H
+
+#include <linux/list.h>
+#include <linux/scatterlist.h>
+#include <linux/trusty/trusty.h>
+#include <linux/types.h>
+
+struct tipc_chan;
+
+struct tipc_msg_buf {
+ void *buf_va;
+ struct scatterlist sg;
+ trusty_shared_mem_id_t buf_id;
+ size_t buf_sz;
+ size_t wpos;
+ size_t rpos;
+ size_t shm_cnt;
+ struct list_head node;
+};
+
+enum tipc_chan_event {
+ TIPC_CHANNEL_CONNECTED = 1,
+ TIPC_CHANNEL_DISCONNECTED,
+ TIPC_CHANNEL_SHUTDOWN,
+};
+
+struct tipc_chan_ops {
+ void (*handle_event)(void *cb_arg, int event);
+ struct tipc_msg_buf *(*handle_msg)(void *cb_arg,
+ struct tipc_msg_buf *mb);
+ void (*handle_release)(void *cb_arg);
+};
+
+struct tipc_chan *tipc_create_channel(struct device *dev,
+ const struct tipc_chan_ops *ops,
+ void *cb_arg);
+
+int tipc_chan_connect(struct tipc_chan *chan, const char *port);
+
+int tipc_chan_queue_msg(struct tipc_chan *chan, struct tipc_msg_buf *mb);
+
+int tipc_chan_shutdown(struct tipc_chan *chan);
+
+void tipc_chan_destroy(struct tipc_chan *chan);
+
+struct tipc_msg_buf *tipc_chan_get_rxbuf(struct tipc_chan *chan);
+
+void tipc_chan_put_rxbuf(struct tipc_chan *chan, struct tipc_msg_buf *mb);
+
+struct tipc_msg_buf *
+tipc_chan_get_txbuf_timeout(struct tipc_chan *chan, long timeout);
+
+void tipc_chan_put_txbuf(struct tipc_chan *chan, struct tipc_msg_buf *mb);
+
+static inline size_t mb_avail_space(struct tipc_msg_buf *mb)
+{
+ return mb->buf_sz - mb->wpos;
+}
+
+static inline size_t mb_avail_data(struct tipc_msg_buf *mb)
+{
+ return mb->wpos - mb->rpos;
+}
+
+static inline void *mb_put_data(struct tipc_msg_buf *mb, size_t len)
+{
+ void *pos = (u8 *)mb->buf_va + mb->wpos;
+
+ BUG_ON(mb->wpos + len > mb->buf_sz);
+ mb->wpos += len;
+ return pos;
+}
+
+static inline void *mb_get_data(struct tipc_msg_buf *mb, size_t len)
+{
+ void *pos = (u8 *)mb->buf_va + mb->rpos;
+
+ BUG_ON(mb->rpos + len > mb->wpos);
+ mb->rpos += len;
+ return pos;
+}
+
+#endif /* __LINUX_TRUSTY_TRUSTY_IPC_H */
+
diff --git a/include/uapi/linux/trusty/ipc.h b/include/uapi/linux/trusty/ipc.h
new file mode 100644
index 000000000000..af91035484f1
--- /dev/null
+++ b/include/uapi/linux/trusty/ipc.h
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+
+#ifndef _UAPI_LINUX_TRUSTY_IPC_H_
+#define _UAPI_LINUX_TRUSTY_IPC_H_
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+#include <linux/uio.h>
+
+/**
+ * enum transfer_kind - How to send an fd to Trusty
+ * @TRUSTY_SHARE: Memory will be accessible by Linux and Trusty. On ARM it
+ * will be mapped as nonsecure. Suitable for shared memory.
+ * The paired fd must be a "dma_buf".
+ * @TRUSTY_LEND: Memory will be accessible only to Trusty. On ARM it will
+ * be transitioned to "Secure" memory if Trusty is in
+ * TrustZone. This transfer kind is suitable for donating
+ * video buffers or other similar resources. The paired fd
+ * may need to come from a platform-specific allocator for
+ * memory that may be transitioned to "Secure".
+ * @TRUSTY_SEND_SECURE: Send memory that is already "Secure". Memory will be
+ * accessible only to Trusty. The paired fd may need to
+ * come from a platform-specific allocator that returns
+ * "Secure" buffers.
+ *
+ * Describes how the user would like the resource in question to be sent to
+ * Trusty. Options may be valid only for certain kinds of fds.
+ */
+enum transfer_kind {
+ TRUSTY_SHARE = 0,
+ TRUSTY_LEND = 1,
+ TRUSTY_SEND_SECURE = 2,
+};
+
+/**
+ * struct trusty_shm - Describes a transfer of memory to Trusty
+ * @fd: The fd to transfer
+ * @transfer: How to transfer it - see &enum transfer_kind
+ */
+struct trusty_shm {
+ __s32 fd;
+ __u32 transfer;
+};
+
+/**
+ * struct tipc_send_msg_req - Request struct for @TIPC_IOC_SEND_MSG
+ * @iov: Pointer to an array of &struct iovec describing data to be sent
+ * @shm: Pointer to an array of &struct trusty_shm describing any file
+ * descriptors to be transferred.
+ * @iov_cnt: Number of elements in the @iov array
+ * @shm_cnt: Number of elements in the @shm array
+ */
+struct tipc_send_msg_req {
+ __u64 iov;
+ __u64 shm;
+ __u64 iov_cnt;
+ __u64 shm_cnt;
+};
+
+#define TIPC_IOC_MAGIC 'r'
+#define TIPC_IOC_CONNECT _IOW(TIPC_IOC_MAGIC, 0x80, char *)
+#define TIPC_IOC_SEND_MSG _IOW(TIPC_IOC_MAGIC, 0x81, \
+ struct tipc_send_msg_req)
+
+#endif
diff --git a/include/uapi/linux/virtio_ids.h b/include/uapi/linux/virtio_ids.h
index b052355ac7a3..cf6b95d9a1ec 100644
--- a/include/uapi/linux/virtio_ids.h
+++ b/include/uapi/linux/virtio_ids.h
@@ -39,6 +39,7 @@
#define VIRTIO_ID_9P 9 /* 9p virtio console */
#define VIRTIO_ID_RPROC_SERIAL 11 /* virtio remoteproc serial link */
#define VIRTIO_ID_CAIF 12 /* Virtio caif */
+#define VIRTIO_ID_TRUSTY_IPC 13 /* virtio trusty ipc */
#define VIRTIO_ID_GPU 16 /* virtio GPU */
#define VIRTIO_ID_INPUT 18 /* virtio input */
#define VIRTIO_ID_VSOCK 19 /* virtio vsock transport */
--
2.30.2
|