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
|
/* Copyright (c) 2015-2018, 2021, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/of.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/log2.h>
#include <linux/module.h>
#include <linux/msm_gsi.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include "gsi.h"
#include "gsi_reg.h"
#define GSI_CMD_TIMEOUT (5*HZ)
#define GSI_STOP_CMD_TIMEOUT_MS 20
#define GSI_MAX_CH_LOW_WEIGHT 15
#define GSI_MHI_ER_START 10
#define GSI_MHI_ER_END 16
#define GSI_RESET_WA_MIN_SLEEP 1000
#define GSI_RESET_WA_MAX_SLEEP 2000
static const struct of_device_id msm_gsi_match[] = {
{ .compatible = "qcom,msm_gsi", },
{ },
};
struct gsi_ctx *gsi_ctx;
static void __gsi_config_type_irq(int ee, uint32_t mask, uint32_t val)
{
uint32_t curr;
curr = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_TYPE_IRQ_MSK_OFFS(ee));
gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
GSI_EE_n_CNTXT_TYPE_IRQ_MSK_OFFS(ee));
}
static void __gsi_config_ch_irq(int ee, uint32_t mask, uint32_t val)
{
uint32_t curr;
curr = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK_OFFS(ee));
gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK_OFFS(ee));
}
static void __gsi_config_evt_irq(int ee, uint32_t mask, uint32_t val)
{
uint32_t curr;
curr = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK_OFFS(ee));
gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK_OFFS(ee));
}
static void __gsi_config_ieob_irq(int ee, uint32_t mask, uint32_t val)
{
uint32_t curr;
curr = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_OFFS(ee));
gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_OFFS(ee));
}
static void __gsi_config_glob_irq(int ee, uint32_t mask, uint32_t val)
{
uint32_t curr;
curr = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS(ee));
gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS(ee));
}
static void __gsi_config_gen_irq(int ee, uint32_t mask, uint32_t val)
{
uint32_t curr;
curr = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_GSI_IRQ_EN_OFFS(ee));
gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
GSI_EE_n_CNTXT_GSI_IRQ_EN_OFFS(ee));
}
static void gsi_handle_ch_ctrl(int ee)
{
uint32_t ch;
int i;
uint32_t val;
struct gsi_chan_ctx *ctx;
ch = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_OFFS(ee));
gsi_writel(ch, gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_CLR_OFFS(ee));
GSIDBG("ch %x\n", ch);
for (i = 0; i < 32; i++) {
if ((1 << i) & ch) {
if (i >= gsi_ctx->max_ch || i >= GSI_CHAN_MAX) {
GSIERR("invalid channel %d\n", i);
break;
}
ctx = &gsi_ctx->chan[i];
val = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(i, ee));
ctx->state = (val &
GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_BMSK) >>
GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_SHFT;
GSIDBG("ch %u state updated to %u\n", i, ctx->state);
complete(&ctx->compl);
gsi_ctx->ch_dbg[i].cmd_completed++;
}
}
}
static void gsi_handle_ev_ctrl(int ee)
{
uint32_t ch;
int i;
uint32_t val;
struct gsi_evt_ctx *ctx;
ch = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_OFFS(ee));
gsi_writel(ch, gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_CLR_OFFS(ee));
GSIDBG("ev %x\n", ch);
for (i = 0; i < 32; i++) {
if ((1 << i) & ch) {
if (i >= gsi_ctx->max_ev || i >= GSI_EVT_RING_MAX) {
GSIERR("invalid event %d\n", i);
break;
}
ctx = &gsi_ctx->evtr[i];
val = gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_0_OFFS(i, ee));
ctx->state = (val &
GSI_EE_n_EV_CH_k_CNTXT_0_CHSTATE_BMSK) >>
GSI_EE_n_EV_CH_k_CNTXT_0_CHSTATE_SHFT;
GSIDBG("evt %u state updated to %u\n", i, ctx->state);
complete(&ctx->compl);
}
}
}
static void gsi_handle_glob_err(uint32_t err)
{
struct gsi_log_err *log;
struct gsi_chan_ctx *ch;
struct gsi_evt_ctx *ev;
struct gsi_chan_err_notify chan_notify;
struct gsi_evt_err_notify evt_notify;
struct gsi_per_notify per_notify;
uint32_t val;
log = (struct gsi_log_err *)&err;
GSIERR("log err_type=%u ee=%u idx=%u\n", log->err_type, log->ee,
log->virt_idx);
GSIERR("code=%u arg1=%u arg2=%u arg3=%u\n", log->code, log->arg1,
log->arg2, log->arg3);
switch (log->err_type) {
case GSI_ERR_TYPE_GLOB:
per_notify.evt_id = GSI_PER_EVT_GLOB_ERROR;
per_notify.user_data = gsi_ctx->per.user_data;
per_notify.data.err_desc = err & 0xFFFF;
gsi_ctx->per.notify_cb(&per_notify);
break;
case GSI_ERR_TYPE_CHAN:
if (log->virt_idx >= gsi_ctx->max_ch) {
GSIERR("Unexpected ch %d\n", log->virt_idx);
WARN_ON(1);
return;
}
ch = &gsi_ctx->chan[log->virt_idx];
chan_notify.chan_user_data = ch->props.chan_user_data;
chan_notify.err_desc = err & 0xFFFF;
if (log->code == GSI_INVALID_TRE_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
val = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(log->virt_idx,
gsi_ctx->per.ee));
ch->state = (val &
GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_BMSK) >>
GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_SHFT;
GSIDBG("ch %u state updated to %u\n", log->virt_idx,
ch->state);
ch->stats.invalid_tre_error++;
BUG_ON(ch->state != GSI_CHAN_STATE_ERROR);
chan_notify.evt_id = GSI_CHAN_INVALID_TRE_ERR;
} else if (log->code == GSI_OUT_OF_BUFFERS_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
chan_notify.evt_id = GSI_CHAN_OUT_OF_BUFFERS_ERR;
} else if (log->code == GSI_OUT_OF_RESOURCES_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
chan_notify.evt_id = GSI_CHAN_OUT_OF_RESOURCES_ERR;
complete(&ch->compl);
} else if (log->code == GSI_UNSUPPORTED_INTER_EE_OP_ERR) {
chan_notify.evt_id =
GSI_CHAN_UNSUPPORTED_INTER_EE_OP_ERR;
} else if (log->code == GSI_NON_ALLOCATED_EVT_ACCESS_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
chan_notify.evt_id =
GSI_CHAN_NON_ALLOCATED_EVT_ACCESS_ERR;
} else if (log->code == GSI_HWO_1_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
chan_notify.evt_id = GSI_CHAN_HWO_1_ERR;
} else {
BUG();
}
if (ch->props.err_cb)
ch->props.err_cb(&chan_notify);
else
WARN_ON(1);
break;
case GSI_ERR_TYPE_EVT:
if (log->virt_idx >= gsi_ctx->max_ev) {
GSIERR("Unexpected ev %d\n", log->virt_idx);
WARN_ON(1);
return;
}
ev = &gsi_ctx->evtr[log->virt_idx];
evt_notify.user_data = ev->props.user_data;
evt_notify.err_desc = err & 0xFFFF;
if (log->code == GSI_OUT_OF_BUFFERS_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
evt_notify.evt_id = GSI_EVT_OUT_OF_BUFFERS_ERR;
} else if (log->code == GSI_OUT_OF_RESOURCES_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
evt_notify.evt_id = GSI_EVT_OUT_OF_RESOURCES_ERR;
complete(&ev->compl);
} else if (log->code == GSI_UNSUPPORTED_INTER_EE_OP_ERR) {
evt_notify.evt_id = GSI_EVT_UNSUPPORTED_INTER_EE_OP_ERR;
} else if (log->code == GSI_EVT_RING_EMPTY_ERR) {
BUG_ON(log->ee != gsi_ctx->per.ee);
evt_notify.evt_id = GSI_EVT_EVT_RING_EMPTY_ERR;
} else {
BUG();
}
if (ev->props.err_cb)
ev->props.err_cb(&evt_notify);
else
WARN_ON(1);
break;
default:
WARN_ON(1);
}
}
static void gsi_handle_gp_int1(void)
{
complete(&gsi_ctx->gen_ee_cmd_compl);
}
static void gsi_handle_glob_ee(int ee)
{
uint32_t val;
uint32_t err;
struct gsi_per_notify notify;
uint32_t clr = ~0;
val = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_GLOB_IRQ_STTS_OFFS(ee));
notify.user_data = gsi_ctx->per.user_data;
if (val & GSI_EE_n_CNTXT_GLOB_IRQ_STTS_ERROR_INT_BMSK) {
err = gsi_readl(gsi_ctx->base +
GSI_EE_n_ERROR_LOG_OFFS(ee));
if (gsi_ctx->per.ver >= GSI_VER_1_2)
gsi_writel(0, gsi_ctx->base +
GSI_EE_n_ERROR_LOG_OFFS(ee));
gsi_writel(clr, gsi_ctx->base +
GSI_EE_n_ERROR_LOG_CLR_OFFS(ee));
gsi_handle_glob_err(err);
}
if (val & GSI_EE_n_CNTXT_GLOB_IRQ_EN_GP_INT1_BMSK) {
gsi_handle_gp_int1();
}
if (val & GSI_EE_n_CNTXT_GLOB_IRQ_EN_GP_INT2_BMSK) {
notify.evt_id = GSI_PER_EVT_GLOB_GP2;
gsi_ctx->per.notify_cb(¬ify);
}
if (val & GSI_EE_n_CNTXT_GLOB_IRQ_EN_GP_INT3_BMSK) {
notify.evt_id = GSI_PER_EVT_GLOB_GP3;
gsi_ctx->per.notify_cb(¬ify);
}
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_CNTXT_GLOB_IRQ_CLR_OFFS(ee));
}
static void gsi_incr_ring_wp(struct gsi_ring_ctx *ctx)
{
ctx->wp_local += ctx->elem_sz;
if (ctx->wp_local == ctx->end)
ctx->wp_local = ctx->base;
}
static void gsi_incr_ring_rp(struct gsi_ring_ctx *ctx)
{
ctx->rp_local += ctx->elem_sz;
if (ctx->rp_local == ctx->end)
ctx->rp_local = ctx->base;
}
uint16_t gsi_find_idx_from_addr(struct gsi_ring_ctx *ctx, uint64_t addr)
{
BUG_ON(addr < ctx->base || addr >= ctx->end);
return (uint32_t)(addr - ctx->base)/ctx->elem_sz;
}
static void gsi_process_chan(struct gsi_xfer_compl_evt *evt,
struct gsi_chan_xfer_notify *notify, bool callback)
{
uint32_t ch_id;
struct gsi_chan_ctx *ch_ctx;
uint16_t rp_idx;
uint64_t rp;
ch_id = evt->chid;
if (ch_id >= gsi_ctx->max_ch) {
GSIERR("Unexpected ch %d\n", ch_id);
WARN_ON(1);
return;
}
ch_ctx = &gsi_ctx->chan[ch_id];
BUG_ON(ch_ctx->props.prot != GSI_CHAN_PROT_GPI);
rp = evt->xfer_ptr;
while (ch_ctx->ring.rp_local != rp) {
gsi_incr_ring_rp(&ch_ctx->ring);
ch_ctx->stats.completed++;
}
/* the element at RP is also processed */
gsi_incr_ring_rp(&ch_ctx->ring);
ch_ctx->stats.completed++;
ch_ctx->ring.rp = ch_ctx->ring.rp_local;
rp_idx = gsi_find_idx_from_addr(&ch_ctx->ring, rp);
notify->xfer_user_data = ch_ctx->user_data[rp_idx];
notify->chan_user_data = ch_ctx->props.chan_user_data;
notify->evt_id = evt->code;
notify->bytes_xfered = evt->len;
if (callback) {
if (atomic_read(&ch_ctx->poll_mode)) {
GSIERR("Calling client callback in polling mode\n");
WARN_ON(1);
}
ch_ctx->props.xfer_cb(notify);
}
}
static void gsi_process_evt_re(struct gsi_evt_ctx *ctx,
struct gsi_chan_xfer_notify *notify, bool callback)
{
struct gsi_xfer_compl_evt *evt;
uint16_t idx;
idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.rp_local);
evt = (struct gsi_xfer_compl_evt *)(ctx->ring.base_va +
idx * ctx->ring.elem_sz);
gsi_process_chan(evt, notify, callback);
gsi_incr_ring_rp(&ctx->ring);
/* recycle this element */
gsi_incr_ring_wp(&ctx->ring);
ctx->stats.completed++;
}
static void gsi_ring_evt_doorbell(struct gsi_evt_ctx *ctx)
{
uint32_t val;
/* write order MUST be MSB followed by LSB */
val = ((ctx->ring.wp_local >> 32) &
GSI_EE_n_EV_CH_k_DOORBELL_1_WRITE_PTR_MSB_BMSK) <<
GSI_EE_n_EV_CH_k_DOORBELL_1_WRITE_PTR_MSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_DOORBELL_1_OFFS(ctx->id,
gsi_ctx->per.ee));
val = (ctx->ring.wp_local &
GSI_EE_n_EV_CH_k_DOORBELL_0_WRITE_PTR_LSB_BMSK) <<
GSI_EE_n_EV_CH_k_DOORBELL_0_WRITE_PTR_LSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_DOORBELL_0_OFFS(ctx->id,
gsi_ctx->per.ee));
}
static void gsi_ring_chan_doorbell(struct gsi_chan_ctx *ctx)
{
uint32_t val;
/* write order MUST be MSB followed by LSB */
val = ((ctx->ring.wp_local >> 32) &
GSI_EE_n_GSI_CH_k_DOORBELL_1_WRITE_PTR_MSB_BMSK) <<
GSI_EE_n_GSI_CH_k_DOORBELL_1_WRITE_PTR_MSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_DOORBELL_1_OFFS(ctx->props.ch_id,
gsi_ctx->per.ee));
val = (ctx->ring.wp_local &
GSI_EE_n_GSI_CH_k_DOORBELL_0_WRITE_PTR_LSB_BMSK) <<
GSI_EE_n_GSI_CH_k_DOORBELL_0_WRITE_PTR_LSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_DOORBELL_0_OFFS(ctx->props.ch_id,
gsi_ctx->per.ee));
}
static void gsi_handle_ieob(int ee)
{
uint32_t ch;
int i;
uint64_t rp;
struct gsi_evt_ctx *ctx;
struct gsi_chan_xfer_notify notify;
unsigned long flags;
unsigned long cntr;
uint32_t msk;
ch = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_IEOB_IRQ_OFFS(ee));
msk = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_OFFS(ee));
gsi_writel(ch & msk, gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_OFFS(ee));
for (i = 0; i < 32; i++) {
if ((1 << i) & ch & msk) {
if (i >= gsi_ctx->max_ev || i >= GSI_EVT_RING_MAX) {
GSIERR("invalid event %d\n", i);
break;
}
ctx = &gsi_ctx->evtr[i];
BUG_ON(ctx->props.intf != GSI_EVT_CHTYPE_GPI_EV);
spin_lock_irqsave(&ctx->ring.slock, flags);
check_again:
cntr = 0;
rp = gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(i, ee));
rp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_5_OFFS(i, ee))) << 32;
ctx->ring.rp = rp;
while (ctx->ring.rp_local != rp) {
++cntr;
if (ctx->props.exclusive &&
atomic_read(&ctx->chan->poll_mode)) {
cntr = 0;
break;
}
gsi_process_evt_re(ctx, ¬ify, true);
}
gsi_ring_evt_doorbell(ctx);
if (cntr != 0)
goto check_again;
spin_unlock_irqrestore(&ctx->ring.slock, flags);
}
}
}
static void gsi_handle_inter_ee_ch_ctrl(int ee)
{
uint32_t ch;
int i;
ch = gsi_readl(gsi_ctx->base +
GSI_INTER_EE_n_SRC_GSI_CH_IRQ_OFFS(ee));
gsi_writel(ch, gsi_ctx->base +
GSI_INTER_EE_n_SRC_GSI_CH_IRQ_CLR_OFFS(ee));
for (i = 0; i < 32; i++) {
if ((1 << i) & ch) {
/* not currently expected */
GSIERR("ch %u was inter-EE changed\n", i);
}
}
}
static void gsi_handle_inter_ee_ev_ctrl(int ee)
{
uint32_t ch;
int i;
ch = gsi_readl(gsi_ctx->base +
GSI_INTER_EE_n_SRC_EV_CH_IRQ_OFFS(ee));
gsi_writel(ch, gsi_ctx->base +
GSI_INTER_EE_n_SRC_EV_CH_IRQ_CLR_OFFS(ee));
for (i = 0; i < 32; i++) {
if ((1 << i) & ch) {
/* not currently expected */
GSIERR("evt %u was inter-EE changed\n", i);
}
}
}
static void gsi_handle_general(int ee)
{
uint32_t val;
struct gsi_per_notify notify;
val = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_GSI_IRQ_STTS_OFFS(ee));
notify.user_data = gsi_ctx->per.user_data;
if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_MCS_STACK_OVRFLOW_BMSK)
notify.evt_id = GSI_PER_EVT_GENERAL_MCS_STACK_OVERFLOW;
if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_CMD_FIFO_OVRFLOW_BMSK)
notify.evt_id = GSI_PER_EVT_GENERAL_CMD_FIFO_OVERFLOW;
if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_BUS_ERROR_BMSK)
notify.evt_id = GSI_PER_EVT_GENERAL_BUS_ERROR;
if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_BREAK_POINT_BMSK)
notify.evt_id = GSI_PER_EVT_GENERAL_BREAK_POINT;
if (gsi_ctx->per.notify_cb)
gsi_ctx->per.notify_cb(¬ify);
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_CNTXT_GSI_IRQ_CLR_OFFS(ee));
}
#define GSI_ISR_MAX_ITER 50
static void gsi_handle_irq(void)
{
uint32_t type;
int ee = gsi_ctx->per.ee;
unsigned long cnt = 0;
while (1) {
type = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_TYPE_IRQ_OFFS(ee));
if (!type)
break;
GSIDBG_LOW("type %x\n", type);
if (type & GSI_EE_n_CNTXT_TYPE_IRQ_CH_CTRL_BMSK)
gsi_handle_ch_ctrl(ee);
if (type & GSI_EE_n_CNTXT_TYPE_IRQ_EV_CTRL_BMSK)
gsi_handle_ev_ctrl(ee);
if (type & GSI_EE_n_CNTXT_TYPE_IRQ_GLOB_EE_BMSK)
gsi_handle_glob_ee(ee);
if (type & GSI_EE_n_CNTXT_TYPE_IRQ_IEOB_BMSK)
gsi_handle_ieob(ee);
if (type & GSI_EE_n_CNTXT_TYPE_IRQ_INTER_EE_CH_CTRL_BMSK)
gsi_handle_inter_ee_ch_ctrl(ee);
if (type & GSI_EE_n_CNTXT_TYPE_IRQ_INTER_EE_EV_CTRL_BMSK)
gsi_handle_inter_ee_ev_ctrl(ee);
if (type & GSI_EE_n_CNTXT_TYPE_IRQ_GENERAL_BMSK)
gsi_handle_general(ee);
if (++cnt > GSI_ISR_MAX_ITER)
BUG();
}
}
static irqreturn_t gsi_isr(int irq, void *ctxt)
{
BUG_ON(ctxt != gsi_ctx);
if (gsi_ctx->per.req_clk_cb) {
bool granted = false;
gsi_ctx->per.req_clk_cb(gsi_ctx->per.user_data, &granted);
if (granted) {
gsi_handle_irq();
gsi_ctx->per.rel_clk_cb(gsi_ctx->per.user_data);
}
} else {
gsi_handle_irq();
}
return IRQ_HANDLED;
}
static uint32_t gsi_get_max_channels(enum gsi_ver ver)
{
uint32_t reg;
switch (ver) {
case GSI_VER_1_0:
reg = gsi_readl(gsi_ctx->base +
GSI_V1_0_EE_n_GSI_HW_PARAM_OFFS(gsi_ctx->per.ee));
reg = (reg & GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_CH_NUM_BMSK) >>
GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_CH_NUM_SHFT;
break;
case GSI_VER_1_2:
reg = gsi_readl(gsi_ctx->base +
GSI_V1_2_EE_n_GSI_HW_PARAM_0_OFFS(gsi_ctx->per.ee));
reg = (reg & GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_CH_NUM_BMSK) >>
GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_CH_NUM_SHFT;
break;
case GSI_VER_1_3:
reg = gsi_readl(gsi_ctx->base +
GSI_V1_3_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
reg = (reg &
GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_BMSK) >>
GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_SHFT;
break;
default:
GSIERR("bad gsi version %d\n", ver);
WARN_ON(1);
reg = 0;
}
GSIDBG("max channels %d\n", reg);
return reg;
}
static uint32_t gsi_get_max_event_rings(enum gsi_ver ver)
{
uint32_t reg;
switch (ver) {
case GSI_VER_1_0:
reg = gsi_readl(gsi_ctx->base +
GSI_V1_0_EE_n_GSI_HW_PARAM_OFFS(gsi_ctx->per.ee));
reg = (reg & GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_EV_CH_NUM_BMSK) >>
GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_EV_CH_NUM_SHFT;
break;
case GSI_VER_1_2:
reg = gsi_readl(gsi_ctx->base +
GSI_V1_2_EE_n_GSI_HW_PARAM_0_OFFS(gsi_ctx->per.ee));
reg = (reg & GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_EV_CH_NUM_BMSK) >>
GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_EV_CH_NUM_SHFT;
break;
case GSI_VER_1_3:
reg = gsi_readl(gsi_ctx->base +
GSI_V1_3_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
reg = (reg &
GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_BMSK) >>
GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_SHFT;
break;
default:
GSIERR("bad gsi version %d\n", ver);
WARN_ON(1);
reg = 0;
}
GSIDBG("max event rings %d\n", reg);
return reg;
}
int gsi_complete_clk_grant(unsigned long dev_hdl)
{
unsigned long flags;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!gsi_ctx->per_registered) {
GSIERR("no client registered\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (dev_hdl != (uintptr_t)gsi_ctx) {
GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%p\n", dev_hdl,
gsi_ctx);
return -GSI_STATUS_INVALID_PARAMS;
}
spin_lock_irqsave(&gsi_ctx->slock, flags);
gsi_handle_irq();
gsi_ctx->per.rel_clk_cb(gsi_ctx->per.user_data);
spin_unlock_irqrestore(&gsi_ctx->slock, flags);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_complete_clk_grant);
int gsi_register_device(struct gsi_per_props *props, unsigned long *dev_hdl)
{
int res;
uint32_t val;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!props || !dev_hdl) {
GSIERR("bad params props=%p dev_hdl=%p\n", props, dev_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->ver <= GSI_VER_ERR || props->ver >= GSI_VER_MAX) {
GSIERR("bad params gsi_ver=%d\n", props->ver);
return -GSI_STATUS_INVALID_PARAMS;
}
if (!props->notify_cb) {
GSIERR("notify callback must be provided\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->req_clk_cb && !props->rel_clk_cb) {
GSIERR("rel callback must be provided\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (gsi_ctx->per_registered) {
GSIERR("per already registered\n");
return -GSI_STATUS_UNSUPPORTED_OP;
}
spin_lock_init(&gsi_ctx->slock);
if (props->intr == GSI_INTR_IRQ) {
if (!props->irq) {
GSIERR("bad irq specified %u\n", props->irq);
return -GSI_STATUS_INVALID_PARAMS;
}
res = devm_request_irq(gsi_ctx->dev, props->irq,
(irq_handler_t) gsi_isr,
props->req_clk_cb ? IRQF_TRIGGER_RISING :
IRQF_TRIGGER_HIGH,
"gsi",
gsi_ctx);
if (res) {
GSIERR("failed to register isr for %u\n", props->irq);
return -GSI_STATUS_ERROR;
}
res = enable_irq_wake(props->irq);
if (res)
GSIERR("failed to enable wake irq %u\n", props->irq);
else
GSIERR("GSI irq is wake enabled %u\n", props->irq);
} else {
GSIERR("do not support interrupt type %u\n", props->intr);
return -GSI_STATUS_UNSUPPORTED_OP;
}
gsi_ctx->base = devm_ioremap_nocache(gsi_ctx->dev, props->phys_addr,
props->size);
if (!gsi_ctx->base) {
GSIERR("failed to remap GSI HW\n");
devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
return -GSI_STATUS_RES_ALLOC_FAILURE;
}
gsi_ctx->per = *props;
gsi_ctx->per_registered = true;
mutex_init(&gsi_ctx->mlock);
atomic_set(&gsi_ctx->num_chan, 0);
atomic_set(&gsi_ctx->num_evt_ring, 0);
gsi_ctx->max_ch = gsi_get_max_channels(gsi_ctx->per.ver);
if (gsi_ctx->max_ch == 0) {
devm_iounmap(gsi_ctx->dev, gsi_ctx->base);
devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
GSIERR("failed to get max channels\n");
return -GSI_STATUS_ERROR;
}
gsi_ctx->max_ev = gsi_get_max_event_rings(gsi_ctx->per.ver);
if (gsi_ctx->max_ev == 0) {
devm_iounmap(gsi_ctx->dev, gsi_ctx->base);
devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
GSIERR("failed to get max event rings\n");
return -GSI_STATUS_ERROR;
}
/* bitmap is max events excludes reserved events */
gsi_ctx->evt_bmap = ~((1 << gsi_ctx->max_ev) - 1);
gsi_ctx->evt_bmap |= ((1 << (GSI_MHI_ER_END + 1)) - 1) ^
((1 << GSI_MHI_ER_START) - 1);
/*
* enable all interrupts but GSI_BREAK_POINT.
* Inter EE commands / interrupt are no supported.
*/
__gsi_config_type_irq(props->ee, ~0, ~0);
__gsi_config_ch_irq(props->ee, ~0, ~0);
__gsi_config_evt_irq(props->ee, ~0, ~0);
__gsi_config_ieob_irq(props->ee, ~0, ~0);
__gsi_config_glob_irq(props->ee, ~0, ~0);
__gsi_config_gen_irq(props->ee, ~0,
~GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_BREAK_POINT_BMSK);
gsi_writel(props->intr, gsi_ctx->base +
GSI_EE_n_CNTXT_INTSET_OFFS(gsi_ctx->per.ee));
val = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_STATUS_OFFS(gsi_ctx->per.ee));
if (val & GSI_EE_n_GSI_STATUS_ENABLED_BMSK)
gsi_ctx->enabled = true;
else
GSIERR("Manager EE has not enabled GSI, GSI un-usable\n");
if (gsi_ctx->per.ver >= GSI_VER_1_2)
gsi_writel(0, gsi_ctx->base +
GSI_EE_n_ERROR_LOG_OFFS(gsi_ctx->per.ee));
*dev_hdl = (uintptr_t)gsi_ctx;
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_register_device);
int gsi_write_device_scratch(unsigned long dev_hdl,
struct gsi_device_scratch *val)
{
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!gsi_ctx->per_registered) {
GSIERR("no client registered\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (dev_hdl != (uintptr_t)gsi_ctx) {
GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%p\n", dev_hdl,
gsi_ctx);
return -GSI_STATUS_INVALID_PARAMS;
}
if (val->max_usb_pkt_size_valid &&
val->max_usb_pkt_size != 1024 &&
val->max_usb_pkt_size != 512) {
GSIERR("bad USB max pkt size dev_hdl=0x%lx sz=%u\n", dev_hdl,
val->max_usb_pkt_size);
return -GSI_STATUS_INVALID_PARAMS;
}
mutex_lock(&gsi_ctx->mlock);
if (val->mhi_base_chan_idx_valid)
gsi_ctx->scratch.word0.s.mhi_base_chan_idx =
val->mhi_base_chan_idx;
if (val->max_usb_pkt_size_valid)
gsi_ctx->scratch.word0.s.max_usb_pkt_size =
(val->max_usb_pkt_size == 1024) ? 1 : 0;
gsi_writel(gsi_ctx->scratch.word0.val,
gsi_ctx->base +
GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
mutex_unlock(&gsi_ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_write_device_scratch);
int gsi_deregister_device(unsigned long dev_hdl, bool force)
{
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!gsi_ctx->per_registered) {
GSIERR("no client registered\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (dev_hdl != (uintptr_t)gsi_ctx) {
GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%p\n", dev_hdl,
gsi_ctx);
return -GSI_STATUS_INVALID_PARAMS;
}
if (!force && atomic_read(&gsi_ctx->num_chan)) {
GSIERR("%u channels are allocated\n",
atomic_read(&gsi_ctx->num_chan));
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (!force && atomic_read(&gsi_ctx->num_evt_ring)) {
GSIERR("%u evt rings are allocated\n",
atomic_read(&gsi_ctx->num_evt_ring));
return -GSI_STATUS_UNSUPPORTED_OP;
}
/* disable all interrupts */
__gsi_config_type_irq(gsi_ctx->per.ee, ~0, 0);
__gsi_config_ch_irq(gsi_ctx->per.ee, ~0, 0);
__gsi_config_evt_irq(gsi_ctx->per.ee, ~0, 0);
__gsi_config_ieob_irq(gsi_ctx->per.ee, ~0, 0);
__gsi_config_glob_irq(gsi_ctx->per.ee, ~0, 0);
__gsi_config_gen_irq(gsi_ctx->per.ee, ~0, 0);
devm_free_irq(gsi_ctx->dev, gsi_ctx->per.irq, gsi_ctx);
devm_iounmap(gsi_ctx->dev, gsi_ctx->base);
memset(gsi_ctx, 0, sizeof(*gsi_ctx));
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_deregister_device);
static void gsi_program_evt_ring_ctx(struct gsi_evt_ring_props *props,
uint8_t evt_id, unsigned int ee)
{
uint32_t val;
GSIDBG("intf=%u intr=%u re=%u\n", props->intf, props->intr,
props->re_size);
val = (((props->intf << GSI_EE_n_EV_CH_k_CNTXT_0_CHTYPE_SHFT) &
GSI_EE_n_EV_CH_k_CNTXT_0_CHTYPE_BMSK) |
((props->intr << GSI_EE_n_EV_CH_k_CNTXT_0_INTYPE_SHFT) &
GSI_EE_n_EV_CH_k_CNTXT_0_INTYPE_BMSK) |
((props->re_size << GSI_EE_n_EV_CH_k_CNTXT_0_ELEMENT_SIZE_SHFT)
& GSI_EE_n_EV_CH_k_CNTXT_0_ELEMENT_SIZE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_0_OFFS(evt_id, ee));
val = (props->ring_len & GSI_EE_n_EV_CH_k_CNTXT_1_R_LENGTH_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_1_R_LENGTH_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_1_OFFS(evt_id, ee));
val = (props->ring_base_addr &
GSI_EE_n_EV_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_2_OFFS(evt_id, ee));
val = ((props->ring_base_addr >> 32) &
GSI_EE_n_EV_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_3_OFFS(evt_id, ee));
val = (((props->int_modt << GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODT_SHFT) &
GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODT_BMSK) |
((props->int_modc << GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODC_SHFT) &
GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODC_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_8_OFFS(evt_id, ee));
val = (props->intvec & GSI_EE_n_EV_CH_k_CNTXT_9_INTVEC_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_9_INTVEC_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_9_OFFS(evt_id, ee));
val = (props->msi_addr & GSI_EE_n_EV_CH_k_CNTXT_10_MSI_ADDR_LSB_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_10_MSI_ADDR_LSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_10_OFFS(evt_id, ee));
val = ((props->msi_addr >> 32) &
GSI_EE_n_EV_CH_k_CNTXT_11_MSI_ADDR_MSB_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_11_MSI_ADDR_MSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_11_OFFS(evt_id, ee));
val = (props->rp_update_addr &
GSI_EE_n_EV_CH_k_CNTXT_12_RP_UPDATE_ADDR_LSB_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_12_RP_UPDATE_ADDR_LSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_12_OFFS(evt_id, ee));
val = ((props->rp_update_addr >> 32) &
GSI_EE_n_EV_CH_k_CNTXT_13_RP_UPDATE_ADDR_MSB_BMSK) <<
GSI_EE_n_EV_CH_k_CNTXT_13_RP_UPDATE_ADDR_MSB_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_13_OFFS(evt_id, ee));
}
static void gsi_init_evt_ring(struct gsi_evt_ring_props *props,
struct gsi_ring_ctx *ctx)
{
ctx->base_va = (uintptr_t)props->ring_base_vaddr;
ctx->base = props->ring_base_addr;
ctx->wp = ctx->base;
ctx->rp = ctx->base;
ctx->wp_local = ctx->base;
ctx->rp_local = ctx->base;
ctx->len = props->ring_len;
ctx->elem_sz = props->re_size;
ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
ctx->end = ctx->base + (ctx->max_num_elem + 1) * ctx->elem_sz;
}
static void gsi_prime_evt_ring(struct gsi_evt_ctx *ctx)
{
unsigned long flags;
spin_lock_irqsave(&ctx->ring.slock, flags);
memset((void *)ctx->ring.base_va, 0, ctx->ring.len);
ctx->ring.wp_local = ctx->ring.base +
ctx->ring.max_num_elem * ctx->ring.elem_sz;
gsi_ring_evt_doorbell(ctx);
spin_unlock_irqrestore(&ctx->ring.slock, flags);
}
static int gsi_validate_evt_ring_props(struct gsi_evt_ring_props *props)
{
uint64_t ra;
if ((props->re_size == GSI_EVT_RING_RE_SIZE_4B &&
props->ring_len % 4) ||
(props->re_size == GSI_EVT_RING_RE_SIZE_16B &&
props->ring_len % 16)) {
GSIERR("bad params ring_len %u not a multiple of RE size %u\n",
props->ring_len, props->re_size);
return -GSI_STATUS_INVALID_PARAMS;
}
ra = props->ring_base_addr;
do_div(ra, roundup_pow_of_two(props->ring_len));
if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
props->ring_base_addr,
roundup_pow_of_two(props->ring_len));
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->intf == GSI_EVT_CHTYPE_GPI_EV &&
!props->ring_base_vaddr) {
GSIERR("protocol %u requires ring base VA\n", props->intf);
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->intf == GSI_EVT_CHTYPE_MHI_EV &&
(!props->evchid_valid ||
props->evchid > GSI_MHI_ER_END ||
props->evchid < GSI_MHI_ER_START)) {
GSIERR("MHI requires evchid valid=%d val=%u\n",
props->evchid_valid, props->evchid);
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->intf != GSI_EVT_CHTYPE_MHI_EV &&
props->evchid_valid) {
GSIERR("protocol %u cannot specify evchid\n", props->intf);
return -GSI_STATUS_INVALID_PARAMS;
}
if (!props->err_cb) {
GSIERR("err callback must be provided\n");
return -GSI_STATUS_INVALID_PARAMS;
}
return GSI_STATUS_SUCCESS;
}
int gsi_alloc_evt_ring(struct gsi_evt_ring_props *props, unsigned long dev_hdl,
unsigned long *evt_ring_hdl)
{
unsigned long evt_id;
enum gsi_evt_ch_cmd_opcode op = GSI_EVT_ALLOCATE;
uint32_t val;
struct gsi_evt_ctx *ctx;
int res;
int ee;
unsigned long flags;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
ee = gsi_ctx->per.ee;
if (!props || !evt_ring_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
GSIERR("bad params props=%p dev_hdl=0x%lx evt_ring_hdl=%p\n",
props, dev_hdl, evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
if (gsi_validate_evt_ring_props(props)) {
GSIERR("invalid params\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (!props->evchid_valid) {
mutex_lock(&gsi_ctx->mlock);
evt_id = find_first_zero_bit(&gsi_ctx->evt_bmap,
sizeof(unsigned long) * BITS_PER_BYTE);
if (evt_id == sizeof(unsigned long) * BITS_PER_BYTE) {
GSIERR("failed to alloc event ID\n");
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_RES_ALLOC_FAILURE;
}
set_bit(evt_id, &gsi_ctx->evt_bmap);
mutex_unlock(&gsi_ctx->mlock);
} else {
evt_id = props->evchid;
}
GSIDBG("Using %lu as virt evt id\n", evt_id);
ctx = &gsi_ctx->evtr[evt_id];
memset(ctx, 0, sizeof(*ctx));
mutex_init(&ctx->mlock);
init_completion(&ctx->compl);
atomic_set(&ctx->chan_ref_cnt, 0);
ctx->props = *props;
mutex_lock(&gsi_ctx->mlock);
val = (((evt_id << GSI_EE_n_EV_CH_CMD_CHID_SHFT) &
GSI_EE_n_EV_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_EV_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_EV_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_CMD_OFFS(ee));
res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
if (res == 0) {
GSIERR("evt_id=%lu timed out\n", evt_id);
if (!props->evchid_valid)
clear_bit(evt_id, &gsi_ctx->evt_bmap);
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_TIMED_OUT;
}
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("evt_id=%lu allocation failed state=%u\n",
evt_id, ctx->state);
if (!props->evchid_valid)
clear_bit(evt_id, &gsi_ctx->evt_bmap);
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_RES_ALLOC_FAILURE;
}
gsi_program_evt_ring_ctx(props, evt_id, gsi_ctx->per.ee);
spin_lock_init(&ctx->ring.slock);
gsi_init_evt_ring(props, &ctx->ring);
ctx->id = evt_id;
*evt_ring_hdl = evt_id;
atomic_inc(&gsi_ctx->num_evt_ring);
if (props->intf == GSI_EVT_CHTYPE_GPI_EV)
gsi_prime_evt_ring(ctx);
mutex_unlock(&gsi_ctx->mlock);
spin_lock_irqsave(&gsi_ctx->slock, flags);
gsi_writel(1 << evt_id, gsi_ctx->base +
GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_OFFS(ee));
if (props->intf != GSI_EVT_CHTYPE_GPI_EV)
__gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << evt_id, 0);
else
__gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->id, ~0);
spin_unlock_irqrestore(&gsi_ctx->slock, flags);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_alloc_evt_ring);
static void __gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
union __packed gsi_evt_scratch val)
{
gsi_writel(val.data.word1, gsi_ctx->base +
GSI_EE_n_EV_CH_k_SCRATCH_0_OFFS(evt_ring_hdl,
gsi_ctx->per.ee));
gsi_writel(val.data.word2, gsi_ctx->base +
GSI_EE_n_EV_CH_k_SCRATCH_1_OFFS(evt_ring_hdl,
gsi_ctx->per.ee));
}
int gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
union __packed gsi_evt_scratch val)
{
struct gsi_evt_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (evt_ring_hdl >= gsi_ctx->max_ev) {
GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->evtr[evt_ring_hdl];
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("bad state %d\n",
gsi_ctx->evtr[evt_ring_hdl].state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&ctx->mlock);
ctx->scratch = val;
__gsi_write_evt_ring_scratch(evt_ring_hdl, val);
mutex_unlock(&ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_write_evt_ring_scratch);
int gsi_dealloc_evt_ring(unsigned long evt_ring_hdl)
{
uint32_t val;
enum gsi_evt_ch_cmd_opcode op = GSI_EVT_DE_ALLOC;
struct gsi_evt_ctx *ctx;
int res;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (evt_ring_hdl >= gsi_ctx->max_ev) {
GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->evtr[evt_ring_hdl];
if (atomic_read(&ctx->chan_ref_cnt)) {
GSIERR("%d channels still using this event ring\n",
atomic_read(&ctx->chan_ref_cnt));
return -GSI_STATUS_UNSUPPORTED_OP;
}
/* TODO: add check for ERROR state */
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&gsi_ctx->mlock);
reinit_completion(&ctx->compl);
val = (((evt_ring_hdl << GSI_EE_n_EV_CH_CMD_CHID_SHFT) &
GSI_EE_n_EV_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_EV_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_EV_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
if (res == 0) {
GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_TIMED_OUT;
}
if (ctx->state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
ctx->state);
BUG();
}
mutex_unlock(&gsi_ctx->mlock);
if (!ctx->props.evchid_valid) {
mutex_lock(&gsi_ctx->mlock);
clear_bit(evt_ring_hdl, &gsi_ctx->evt_bmap);
mutex_unlock(&gsi_ctx->mlock);
}
atomic_dec(&gsi_ctx->num_evt_ring);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_dealloc_evt_ring);
int gsi_query_evt_ring_db_addr(unsigned long evt_ring_hdl,
uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
{
struct gsi_evt_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!db_addr_wp_msb || !db_addr_wp_lsb) {
GSIERR("bad params msb=%p lsb=%p\n", db_addr_wp_msb,
db_addr_wp_lsb);
return -GSI_STATUS_INVALID_PARAMS;
}
if (evt_ring_hdl >= gsi_ctx->max_ev) {
GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->evtr[evt_ring_hdl];
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("bad state %d\n",
gsi_ctx->evtr[evt_ring_hdl].state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
*db_addr_wp_lsb = gsi_ctx->per.phys_addr +
GSI_EE_n_EV_CH_k_DOORBELL_0_OFFS(evt_ring_hdl, gsi_ctx->per.ee);
*db_addr_wp_msb = gsi_ctx->per.phys_addr +
GSI_EE_n_EV_CH_k_DOORBELL_1_OFFS(evt_ring_hdl, gsi_ctx->per.ee);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_query_evt_ring_db_addr);
int gsi_ring_evt_ring_db(unsigned long evt_ring_hdl, uint64_t value)
{
struct gsi_evt_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (evt_ring_hdl >= gsi_ctx->max_ev) {
GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->evtr[evt_ring_hdl];
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("bad state %d\n",
gsi_ctx->evtr[evt_ring_hdl].state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
ctx->ring.wp_local = value;
gsi_ring_evt_doorbell(ctx);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_ring_evt_ring_db);
int gsi_reset_evt_ring(unsigned long evt_ring_hdl)
{
uint32_t val;
enum gsi_evt_ch_cmd_opcode op = GSI_EVT_RESET;
struct gsi_evt_ctx *ctx;
int res;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (evt_ring_hdl >= gsi_ctx->max_ev) {
GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->evtr[evt_ring_hdl];
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&gsi_ctx->mlock);
reinit_completion(&ctx->compl);
val = (((evt_ring_hdl << GSI_EE_n_EV_CH_CMD_CHID_SHFT) &
GSI_EE_n_EV_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_EV_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_EV_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_EV_CH_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
if (res == 0) {
GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_TIMED_OUT;
}
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
ctx->state);
BUG();
}
gsi_program_evt_ring_ctx(&ctx->props, evt_ring_hdl, gsi_ctx->per.ee);
gsi_init_evt_ring(&ctx->props, &ctx->ring);
/* restore scratch */
__gsi_write_evt_ring_scratch(evt_ring_hdl, ctx->scratch);
if (ctx->props.intf == GSI_EVT_CHTYPE_GPI_EV)
gsi_prime_evt_ring(ctx);
mutex_unlock(&gsi_ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_reset_evt_ring);
int gsi_get_evt_ring_cfg(unsigned long evt_ring_hdl,
struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
{
struct gsi_evt_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!props || !scr) {
GSIERR("bad params props=%p scr=%p\n", props, scr);
return -GSI_STATUS_INVALID_PARAMS;
}
if (evt_ring_hdl >= gsi_ctx->max_ev) {
GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->evtr[evt_ring_hdl];
if (ctx->state == GSI_EVT_RING_STATE_NOT_ALLOCATED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&ctx->mlock);
*props = ctx->props;
*scr = ctx->scratch;
mutex_unlock(&ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_get_evt_ring_cfg);
int gsi_set_evt_ring_cfg(unsigned long evt_ring_hdl,
struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
{
struct gsi_evt_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!props || gsi_validate_evt_ring_props(props)) {
GSIERR("bad params props=%p\n", props);
return -GSI_STATUS_INVALID_PARAMS;
}
if (evt_ring_hdl >= gsi_ctx->max_ev) {
GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->evtr[evt_ring_hdl];
if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (ctx->props.exclusive != props->exclusive) {
GSIERR("changing immutable fields not supported\n");
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&ctx->mlock);
ctx->props = *props;
if (scr)
ctx->scratch = *scr;
mutex_unlock(&ctx->mlock);
return gsi_reset_evt_ring(evt_ring_hdl);
}
EXPORT_SYMBOL(gsi_set_evt_ring_cfg);
static void gsi_program_chan_ctx(struct gsi_chan_props *props, unsigned int ee,
uint8_t erindex)
{
uint32_t val;
val = (((props->prot << GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_PROTOCOL_SHFT)
& GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_PROTOCOL_BMSK) |
((props->dir << GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_DIR_SHFT) &
GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_DIR_BMSK) |
((erindex << GSI_EE_n_GSI_CH_k_CNTXT_0_ERINDEX_SHFT) &
GSI_EE_n_GSI_CH_k_CNTXT_0_ERINDEX_BMSK) |
((props->re_size << GSI_EE_n_GSI_CH_k_CNTXT_0_ELEMENT_SIZE_SHFT)
& GSI_EE_n_GSI_CH_k_CNTXT_0_ELEMENT_SIZE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(props->ch_id, ee));
val = (props->ring_len & GSI_EE_n_GSI_CH_k_CNTXT_1_R_LENGTH_BMSK) <<
GSI_EE_n_GSI_CH_k_CNTXT_1_R_LENGTH_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_1_OFFS(props->ch_id, ee));
val = (props->ring_base_addr &
GSI_EE_n_GSI_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_BMSK) <<
GSI_EE_n_GSI_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_2_OFFS(props->ch_id, ee));
val = ((props->ring_base_addr >> 32) &
GSI_EE_n_GSI_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_BMSK) <<
GSI_EE_n_GSI_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_SHFT;
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_3_OFFS(props->ch_id, ee));
val = (((props->low_weight << GSI_EE_n_GSI_CH_k_QOS_WRR_WEIGHT_SHFT) &
GSI_EE_n_GSI_CH_k_QOS_WRR_WEIGHT_BMSK) |
((props->max_prefetch <<
GSI_EE_n_GSI_CH_k_QOS_MAX_PREFETCH_SHFT) &
GSI_EE_n_GSI_CH_k_QOS_MAX_PREFETCH_BMSK) |
((props->use_db_eng << GSI_EE_n_GSI_CH_k_QOS_USE_DB_ENG_SHFT) &
GSI_EE_n_GSI_CH_k_QOS_USE_DB_ENG_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_QOS_OFFS(props->ch_id, ee));
}
static void gsi_init_chan_ring(struct gsi_chan_props *props,
struct gsi_ring_ctx *ctx)
{
ctx->base_va = (uintptr_t)props->ring_base_vaddr;
ctx->base = props->ring_base_addr;
ctx->wp = ctx->base;
ctx->rp = ctx->base;
ctx->wp_local = ctx->base;
ctx->rp_local = ctx->base;
ctx->len = props->ring_len;
ctx->elem_sz = props->re_size;
ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
ctx->end = ctx->base + (ctx->max_num_elem + 1) *
ctx->elem_sz;
}
static int gsi_validate_channel_props(struct gsi_chan_props *props)
{
uint64_t ra;
if (props->ch_id >= gsi_ctx->max_ch) {
GSIERR("ch_id %u invalid\n", props->ch_id);
return -GSI_STATUS_INVALID_PARAMS;
}
if ((props->re_size == GSI_CHAN_RE_SIZE_4B &&
props->ring_len % 4) ||
(props->re_size == GSI_CHAN_RE_SIZE_16B &&
props->ring_len % 16) ||
(props->re_size == GSI_CHAN_RE_SIZE_32B &&
props->ring_len % 32)) {
GSIERR("bad params ring_len %u not a multiple of re size %u\n",
props->ring_len, props->re_size);
return -GSI_STATUS_INVALID_PARAMS;
}
ra = props->ring_base_addr;
do_div(ra, roundup_pow_of_two(props->ring_len));
if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
props->ring_base_addr,
roundup_pow_of_two(props->ring_len));
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->prot == GSI_CHAN_PROT_GPI &&
!props->ring_base_vaddr) {
GSIERR("protocol %u requires ring base VA\n", props->prot);
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->low_weight > GSI_MAX_CH_LOW_WEIGHT) {
GSIERR("invalid channel low weight %u\n", props->low_weight);
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->prot == GSI_CHAN_PROT_GPI && !props->xfer_cb) {
GSIERR("xfer callback must be provided\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (!props->err_cb) {
GSIERR("err callback must be provided\n");
return -GSI_STATUS_INVALID_PARAMS;
}
return GSI_STATUS_SUCCESS;
}
int gsi_alloc_channel(struct gsi_chan_props *props, unsigned long dev_hdl,
unsigned long *chan_hdl)
{
struct gsi_chan_ctx *ctx;
uint32_t val;
int res, size;
int ee;
enum gsi_ch_cmd_opcode op = GSI_CH_ALLOCATE;
uint8_t erindex;
void **user_data;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
ee = gsi_ctx->per.ee;
if (!props || !chan_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
GSIERR("bad params props=%p dev_hdl=0x%lx chan_hdl=%p\n",
props, dev_hdl, chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
if (gsi_validate_channel_props(props)) {
GSIERR("bad params\n");
return -GSI_STATUS_INVALID_PARAMS;
}
if (props->evt_ring_hdl != ~0) {
if (props->evt_ring_hdl >= GSI_EVT_RING_MAX) {
GSIERR("invalid evt ring=%lu\n", props->evt_ring_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
if (atomic_read(
&gsi_ctx->evtr[props->evt_ring_hdl].chan_ref_cnt) &&
gsi_ctx->evtr[props->evt_ring_hdl].props.exclusive) {
GSIERR("evt ring=%lu exclusively used by chan_hdl=%p\n",
props->evt_ring_hdl, chan_hdl);
return -GSI_STATUS_UNSUPPORTED_OP;
}
}
ctx = &gsi_ctx->chan[props->ch_id];
if (ctx->allocated) {
GSIERR("chan %d already allocated\n", props->ch_id);
return -GSI_STATUS_NODEV;
}
memset(ctx, 0, sizeof(*ctx));
size = (props->ring_len / props->re_size) * sizeof(void *);
user_data = kzalloc(size, GFP_KERNEL);
if (user_data == NULL) {
GSIERR("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_RES_ALLOC_FAILURE;
}
mutex_init(&ctx->mlock);
init_completion(&ctx->compl);
atomic_set(&ctx->poll_mode, GSI_CHAN_MODE_CALLBACK);
ctx->props = *props;
mutex_lock(&gsi_ctx->mlock);
gsi_ctx->ch_dbg[props->ch_id].ch_allocate++;
val = (((props->ch_id << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_CMD_OFFS(ee));
res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
if (res == 0) {
GSIERR("chan_hdl=%u timed out\n", props->ch_id);
mutex_unlock(&gsi_ctx->mlock);
kfree(user_data);
return -GSI_STATUS_TIMED_OUT;
}
if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
GSIERR("chan_hdl=%u allocation failed state=%d\n",
props->ch_id, ctx->state);
mutex_unlock(&gsi_ctx->mlock);
kfree(user_data);
return -GSI_STATUS_RES_ALLOC_FAILURE;
}
mutex_unlock(&gsi_ctx->mlock);
erindex = props->evt_ring_hdl != ~0 ? props->evt_ring_hdl :
GSI_NO_EVT_ERINDEX;
if (erindex != GSI_NO_EVT_ERINDEX) {
ctx->evtr = &gsi_ctx->evtr[erindex];
atomic_inc(&ctx->evtr->chan_ref_cnt);
if (ctx->evtr->props.exclusive)
ctx->evtr->chan = ctx;
}
gsi_program_chan_ctx(props, gsi_ctx->per.ee, erindex);
spin_lock_init(&ctx->ring.slock);
gsi_init_chan_ring(props, &ctx->ring);
if (!props->max_re_expected)
ctx->props.max_re_expected = ctx->ring.max_num_elem;
ctx->user_data = user_data;
*chan_hdl = props->ch_id;
ctx->allocated = true;
ctx->stats.dp.last_timestamp = jiffies_to_msecs(jiffies);
atomic_inc(&gsi_ctx->num_chan);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_alloc_channel);
static void __gsi_write_channel_scratch(unsigned long chan_hdl,
union __packed gsi_channel_scratch val)
{
uint32_t reg;
gsi_writel(val.data.word1, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_SCRATCH_0_OFFS(chan_hdl,
gsi_ctx->per.ee));
gsi_writel(val.data.word2, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_SCRATCH_1_OFFS(chan_hdl,
gsi_ctx->per.ee));
gsi_writel(val.data.word3, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS(chan_hdl,
gsi_ctx->per.ee));
/* below sequence is not atomic. assumption is sequencer specific fields
* will remain unchanged across this sequence
*/
reg = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
gsi_ctx->per.ee));
reg &= 0xFFFF;
reg |= (val.data.word4 & 0xFFFF0000);
gsi_writel(reg, gsi_ctx->base +
GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
gsi_ctx->per.ee));
}
int gsi_write_channel_scratch(unsigned long chan_hdl,
union __packed gsi_channel_scratch val)
{
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
GSIERR("bad state %d\n",
gsi_ctx->chan[chan_hdl].state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
ctx = &gsi_ctx->chan[chan_hdl];
mutex_lock(&ctx->mlock);
ctx->scratch = val;
__gsi_write_channel_scratch(chan_hdl, val);
mutex_unlock(&ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_write_channel_scratch);
int gsi_query_channel_db_addr(unsigned long chan_hdl,
uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
{
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!db_addr_wp_msb || !db_addr_wp_lsb) {
GSIERR("bad params msb=%p lsb=%p\n", db_addr_wp_msb,
db_addr_wp_lsb);
return -GSI_STATUS_INVALID_PARAMS;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
if (gsi_ctx->chan[chan_hdl].state == GSI_CHAN_STATE_NOT_ALLOCATED) {
GSIERR("bad state %d\n",
gsi_ctx->chan[chan_hdl].state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
*db_addr_wp_lsb = gsi_ctx->per.phys_addr +
GSI_EE_n_GSI_CH_k_DOORBELL_0_OFFS(chan_hdl, gsi_ctx->per.ee);
*db_addr_wp_msb = gsi_ctx->per.phys_addr +
GSI_EE_n_GSI_CH_k_DOORBELL_1_OFFS(chan_hdl, gsi_ctx->per.ee);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_query_channel_db_addr);
int gsi_start_channel(unsigned long chan_hdl)
{
enum gsi_ch_cmd_opcode op = GSI_CH_START;
int res;
uint32_t val;
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->state != GSI_CHAN_STATE_ALLOCATED &&
ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
ctx->state != GSI_CHAN_STATE_STOPPED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&gsi_ctx->mlock);
reinit_completion(&ctx->compl);
gsi_ctx->ch_dbg[chan_hdl].ch_start++;
val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
if (res == 0) {
GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_TIMED_OUT;
}
if (ctx->state != GSI_CHAN_STATE_STARTED) {
GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
BUG();
}
mutex_unlock(&gsi_ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_start_channel);
int gsi_stop_channel(unsigned long chan_hdl)
{
enum gsi_ch_cmd_opcode op = GSI_CH_STOP;
int res;
uint32_t val;
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->state == GSI_CHAN_STATE_STOPPED) {
GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
return GSI_STATUS_SUCCESS;
}
if (ctx->state != GSI_CHAN_STATE_STARTED &&
ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
ctx->state != GSI_CHAN_STATE_ERROR) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&gsi_ctx->mlock);
reinit_completion(&ctx->compl);
gsi_ctx->ch_dbg[chan_hdl].ch_stop++;
val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&ctx->compl,
msecs_to_jiffies(GSI_STOP_CMD_TIMEOUT_MS));
if (res == 0) {
/*
* check channel state here in case the channel is stopped but
* the interrupt was not handled yet.
*/
val = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(chan_hdl,
gsi_ctx->per.ee));
ctx->state = (val &
GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_BMSK) >>
GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_SHFT;
if (ctx->state == GSI_CHAN_STATE_STOPPED) {
res = GSI_STATUS_SUCCESS;
goto free_lock;
}
GSIDBG("chan_hdl=%lu timed out\n", chan_hdl);
res = -GSI_STATUS_TIMED_OUT;
goto free_lock;
}
if (ctx->state != GSI_CHAN_STATE_STOPPED &&
ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
res = -GSI_STATUS_BAD_STATE;
goto free_lock;
}
if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
GSIERR("chan=%lu busy try again\n", chan_hdl);
res = -GSI_STATUS_AGAIN;
goto free_lock;
}
res = GSI_STATUS_SUCCESS;
free_lock:
mutex_unlock(&gsi_ctx->mlock);
return res;
}
EXPORT_SYMBOL(gsi_stop_channel);
int gsi_stop_db_channel(unsigned long chan_hdl)
{
enum gsi_ch_cmd_opcode op = GSI_CH_DB_STOP;
int res;
uint32_t val;
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->state == GSI_CHAN_STATE_STOPPED) {
GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
return GSI_STATUS_SUCCESS;
}
if (ctx->state != GSI_CHAN_STATE_STARTED &&
ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&gsi_ctx->mlock);
reinit_completion(&ctx->compl);
gsi_ctx->ch_dbg[chan_hdl].ch_db_stop++;
val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&ctx->compl,
msecs_to_jiffies(GSI_STOP_CMD_TIMEOUT_MS));
if (res == 0) {
GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
res = -GSI_STATUS_TIMED_OUT;
goto free_lock;
}
if (ctx->state != GSI_CHAN_STATE_STOPPED &&
ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
res = -GSI_STATUS_BAD_STATE;
goto free_lock;
}
if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
GSIERR("chan=%lu busy try again\n", chan_hdl);
res = -GSI_STATUS_AGAIN;
goto free_lock;
}
res = GSI_STATUS_SUCCESS;
free_lock:
mutex_unlock(&gsi_ctx->mlock);
return res;
}
EXPORT_SYMBOL(gsi_stop_db_channel);
int gsi_reset_channel(unsigned long chan_hdl)
{
enum gsi_ch_cmd_opcode op = GSI_CH_RESET;
int res;
uint32_t val;
struct gsi_chan_ctx *ctx;
bool reset_done = false;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->state != GSI_CHAN_STATE_STOPPED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&gsi_ctx->mlock);
reset:
reinit_completion(&ctx->compl);
gsi_ctx->ch_dbg[chan_hdl].ch_reset++;
val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
if (res == 0) {
GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_TIMED_OUT;
}
if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
ctx->state);
BUG();
}
/* workaround: reset GSI producers again */
if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && !reset_done) {
usleep_range(GSI_RESET_WA_MIN_SLEEP, GSI_RESET_WA_MAX_SLEEP);
reset_done = true;
goto reset;
}
gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
gsi_init_chan_ring(&ctx->props, &ctx->ring);
/* restore scratch */
__gsi_write_channel_scratch(chan_hdl, ctx->scratch);
mutex_unlock(&gsi_ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_reset_channel);
int gsi_dealloc_channel(unsigned long chan_hdl)
{
enum gsi_ch_cmd_opcode op = GSI_CH_DE_ALLOC;
int res;
uint32_t val;
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&gsi_ctx->mlock);
reinit_completion(&ctx->compl);
gsi_ctx->ch_dbg[chan_hdl].ch_de_alloc++;
val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
if (res == 0) {
GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
mutex_unlock(&gsi_ctx->mlock);
return -GSI_STATUS_TIMED_OUT;
}
if (ctx->state != GSI_CHAN_STATE_NOT_ALLOCATED) {
GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
ctx->state);
BUG();
}
mutex_unlock(&gsi_ctx->mlock);
kfree(ctx->user_data);
ctx->allocated = false;
if (ctx->evtr)
atomic_dec(&ctx->evtr->chan_ref_cnt);
atomic_dec(&gsi_ctx->num_chan);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_dealloc_channel);
void gsi_update_ch_dp_stats(struct gsi_chan_ctx *ctx, uint16_t used)
{
unsigned long now = jiffies_to_msecs(jiffies);
unsigned long elapsed;
if (used == 0) {
elapsed = now - ctx->stats.dp.last_timestamp;
if (ctx->stats.dp.empty_time < elapsed)
ctx->stats.dp.empty_time = elapsed;
}
if (used <= ctx->props.max_re_expected / 3)
++ctx->stats.dp.ch_below_lo;
else if (used <= 2 * ctx->props.max_re_expected / 3)
++ctx->stats.dp.ch_below_hi;
else
++ctx->stats.dp.ch_above_hi;
ctx->stats.dp.last_timestamp = now;
}
static void __gsi_query_channel_free_re(struct gsi_chan_ctx *ctx,
uint16_t *num_free_re)
{
uint16_t start;
uint16_t start_hw;
uint16_t end;
uint64_t rp;
uint64_t rp_hw;
int ee = gsi_ctx->per.ee;
uint16_t used;
uint16_t used_hw;
rp_hw = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS(ctx->props.ch_id, ee));
rp_hw |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_5_OFFS(ctx->props.ch_id, ee)))
<< 32;
if (!ctx->evtr) {
rp = rp_hw;
ctx->ring.rp = rp;
} else {
rp = ctx->ring.rp_local;
}
start = gsi_find_idx_from_addr(&ctx->ring, rp);
start_hw = gsi_find_idx_from_addr(&ctx->ring, rp_hw);
end = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
if (end >= start)
used = end - start;
else
used = ctx->ring.max_num_elem + 1 - (start - end);
if (end >= start_hw)
used_hw = end - start_hw;
else
used_hw = ctx->ring.max_num_elem + 1 - (start_hw - end);
*num_free_re = ctx->ring.max_num_elem - used;
gsi_update_ch_dp_stats(ctx, used_hw);
}
int gsi_query_channel_info(unsigned long chan_hdl,
struct gsi_chan_info *info)
{
struct gsi_chan_ctx *ctx;
spinlock_t *slock;
unsigned long flags;
uint64_t rp;
uint64_t wp;
int ee;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
ee = gsi_ctx->per.ee;
if (chan_hdl >= gsi_ctx->max_ch || !info) {
GSIERR("bad params chan_hdl=%lu info=%p\n", chan_hdl, info);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->evtr) {
slock = &ctx->evtr->ring.slock;
info->evt_valid = true;
} else {
slock = &ctx->ring.slock;
info->evt_valid = false;
}
spin_lock_irqsave(slock, flags);
rp = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS(ctx->props.ch_id, ee));
rp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_5_OFFS(ctx->props.ch_id, ee))) << 32;
ctx->ring.rp = rp;
info->rp = rp;
wp = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_6_OFFS(ctx->props.ch_id, ee));
wp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_7_OFFS(ctx->props.ch_id, ee))) << 32;
ctx->ring.wp = wp;
info->wp = wp;
if (info->evt_valid) {
rp = gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(ctx->evtr->id, ee));
rp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_5_OFFS(ctx->evtr->id, ee)))
<< 32;
info->evt_rp = rp;
wp = gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_6_OFFS(ctx->evtr->id, ee));
wp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_7_OFFS(ctx->evtr->id, ee)))
<< 32;
info->evt_wp = wp;
}
spin_unlock_irqrestore(slock, flags);
GSIDBG("ch=%lu RP=0x%llx WP=0x%llx ev_valid=%d ERP=0x%llx EWP=0x%llx\n",
chan_hdl, info->rp, info->wp,
info->evt_valid, info->evt_rp, info->evt_wp);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_query_channel_info);
int gsi_is_channel_empty(unsigned long chan_hdl, bool *is_empty)
{
struct gsi_chan_ctx *ctx;
spinlock_t *slock;
unsigned long flags;
uint64_t rp;
uint64_t wp;
int ee;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
ee = gsi_ctx->per.ee;
if (chan_hdl >= gsi_ctx->max_ch || !is_empty) {
GSIERR("bad params chan_hdl=%lu is_empty=%p\n",
chan_hdl, is_empty);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->props.prot != GSI_CHAN_PROT_GPI) {
GSIERR("op not supported for protocol %u\n", ctx->props.prot);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (ctx->evtr)
slock = &ctx->evtr->ring.slock;
else
slock = &ctx->ring.slock;
spin_lock_irqsave(slock, flags);
rp = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS(ctx->props.ch_id, ee));
rp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_5_OFFS(ctx->props.ch_id, ee))) << 32;
ctx->ring.rp = rp;
wp = gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_6_OFFS(ctx->props.ch_id, ee));
wp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_GSI_CH_k_CNTXT_7_OFFS(ctx->props.ch_id, ee))) << 32;
ctx->ring.wp = wp;
if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI)
*is_empty = (ctx->ring.rp_local == rp) ? true : false;
else
*is_empty = (wp == rp) ? true : false;
spin_unlock_irqrestore(slock, flags);
GSIDBG("ch=%lu RP=0x%llx WP=0x%llx RP_LOCAL=0x%llx\n",
chan_hdl, rp, wp, ctx->ring.rp_local);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_is_channel_empty);
int gsi_queue_xfer(unsigned long chan_hdl, uint16_t num_xfers,
struct gsi_xfer_elem *xfer, bool ring_db)
{
struct gsi_chan_ctx *ctx;
uint16_t free;
struct gsi_tre tre;
struct gsi_tre *tre_ptr;
uint16_t idx;
uint64_t wp_rollback;
int i;
spinlock_t *slock;
unsigned long flags;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch || !num_xfers || !xfer) {
GSIERR("bad params chan_hdl=%lu num_xfers=%u xfer=%p\n",
chan_hdl, num_xfers, xfer);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->props.prot != GSI_CHAN_PROT_GPI) {
GSIERR("op not supported for protocol %u\n", ctx->props.prot);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (ctx->evtr)
slock = &ctx->evtr->ring.slock;
else
slock = &ctx->ring.slock;
spin_lock_irqsave(slock, flags);
__gsi_query_channel_free_re(ctx, &free);
if (num_xfers > free) {
GSIERR("chan_hdl=%lu num_xfers=%u free=%u\n",
chan_hdl, num_xfers, free);
spin_unlock_irqrestore(slock, flags);
return -GSI_STATUS_RING_INSUFFICIENT_SPACE;
}
wp_rollback = ctx->ring.wp_local;
for (i = 0; i < num_xfers; i++) {
memset(&tre, 0, sizeof(tre));
tre.buffer_ptr = xfer[i].addr;
tre.buf_len = xfer[i].len;
if (xfer[i].type == GSI_XFER_ELEM_DATA) {
tre.re_type = GSI_RE_XFER;
} else if (xfer[i].type == GSI_XFER_ELEM_IMME_CMD) {
tre.re_type = GSI_RE_IMMD_CMD;
} else {
GSIERR("chan_hdl=%lu bad RE type=%u\n", chan_hdl,
xfer[i].type);
break;
}
tre.bei = (xfer[i].flags & GSI_XFER_FLAG_BEI) ? 1 : 0;
tre.ieot = (xfer[i].flags & GSI_XFER_FLAG_EOT) ? 1 : 0;
tre.ieob = (xfer[i].flags & GSI_XFER_FLAG_EOB) ? 1 : 0;
tre.chain = (xfer[i].flags & GSI_XFER_FLAG_CHAIN) ? 1 : 0;
idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
tre_ptr = (struct gsi_tre *)(ctx->ring.base_va +
idx * ctx->ring.elem_sz);
/* write the TRE to ring */
*tre_ptr = tre;
ctx->user_data[idx] = xfer[i].xfer_user_data;
gsi_incr_ring_wp(&ctx->ring);
}
if (i != num_xfers) {
/* reject all the xfers */
ctx->ring.wp_local = wp_rollback;
spin_unlock_irqrestore(slock, flags);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx->stats.queued += num_xfers;
/* ensure TRE is set before ringing doorbell */
wmb();
if (ring_db)
gsi_ring_chan_doorbell(ctx);
spin_unlock_irqrestore(slock, flags);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_queue_xfer);
int gsi_start_xfer(unsigned long chan_hdl)
{
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->props.prot != GSI_CHAN_PROT_GPI) {
GSIERR("op not supported for protocol %u\n", ctx->props.prot);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (ctx->state != GSI_CHAN_STATE_STARTED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
gsi_ring_chan_doorbell(ctx);
return GSI_STATUS_SUCCESS;
};
EXPORT_SYMBOL(gsi_start_xfer);
int gsi_poll_channel(unsigned long chan_hdl,
struct gsi_chan_xfer_notify *notify)
{
struct gsi_chan_ctx *ctx;
uint64_t rp;
int ee;
unsigned long flags;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
ee = gsi_ctx->per.ee;
if (chan_hdl >= gsi_ctx->max_ch || !notify) {
GSIERR("bad params chan_hdl=%lu notify=%p\n", chan_hdl, notify);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->props.prot != GSI_CHAN_PROT_GPI) {
GSIERR("op not supported for protocol %u\n", ctx->props.prot);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (!ctx->evtr) {
GSIERR("no event ring associated chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_UNSUPPORTED_OP;
}
spin_lock_irqsave(&ctx->evtr->ring.slock, flags);
rp = gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(ctx->evtr->id, ee));
rp |= ((uint64_t)gsi_readl(gsi_ctx->base +
GSI_EE_n_EV_CH_k_CNTXT_5_OFFS(ctx->evtr->id, ee))) << 32;
ctx->evtr->ring.rp = rp;
if (rp == ctx->evtr->ring.rp_local) {
spin_unlock_irqrestore(&ctx->evtr->ring.slock, flags);
ctx->stats.poll_empty++;
return GSI_STATUS_POLL_EMPTY;
}
gsi_process_evt_re(ctx->evtr, notify, false);
gsi_ring_evt_doorbell(ctx->evtr);
spin_unlock_irqrestore(&ctx->evtr->ring.slock, flags);
ctx->stats.poll_ok++;
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_poll_channel);
int gsi_config_channel_mode(unsigned long chan_hdl, enum gsi_chan_mode mode)
{
struct gsi_chan_ctx *ctx;
enum gsi_chan_mode curr;
unsigned long flags;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu mode=%u\n", chan_hdl, mode);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->props.prot != GSI_CHAN_PROT_GPI) {
GSIERR("op not supported for protocol %u\n", ctx->props.prot);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (!ctx->evtr || !ctx->evtr->props.exclusive) {
GSIERR("cannot configure mode on chan_hdl=%lu\n",
chan_hdl);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (atomic_read(&ctx->poll_mode))
curr = GSI_CHAN_MODE_POLL;
else
curr = GSI_CHAN_MODE_CALLBACK;
if (mode == curr) {
GSIERR("already in requested mode %u chan_hdl=%lu\n",
curr, chan_hdl);
return -GSI_STATUS_UNSUPPORTED_OP;
}
spin_lock_irqsave(&gsi_ctx->slock, flags);
if (curr == GSI_CHAN_MODE_CALLBACK &&
mode == GSI_CHAN_MODE_POLL) {
__gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, 0);
ctx->stats.callback_to_poll++;
}
if (curr == GSI_CHAN_MODE_POLL &&
mode == GSI_CHAN_MODE_CALLBACK) {
__gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, ~0);
ctx->stats.poll_to_callback++;
}
atomic_set(&ctx->poll_mode, mode);
spin_unlock_irqrestore(&gsi_ctx->slock, flags);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_config_channel_mode);
int gsi_get_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
union gsi_channel_scratch *scr)
{
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!props || !scr) {
GSIERR("bad params props=%p scr=%p\n", props, scr);
return -GSI_STATUS_INVALID_PARAMS;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&ctx->mlock);
*props = ctx->props;
*scr = ctx->scratch;
mutex_unlock(&ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_get_channel_cfg);
int gsi_set_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
union gsi_channel_scratch *scr)
{
struct gsi_chan_ctx *ctx;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (!props || gsi_validate_channel_props(props)) {
GSIERR("bad params props=%p\n", props);
return -GSI_STATUS_INVALID_PARAMS;
}
if (chan_hdl >= gsi_ctx->max_ch) {
GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
return -GSI_STATUS_INVALID_PARAMS;
}
ctx = &gsi_ctx->chan[chan_hdl];
if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
GSIERR("bad state %d\n", ctx->state);
return -GSI_STATUS_UNSUPPORTED_OP;
}
if (ctx->props.ch_id != props->ch_id ||
ctx->props.evt_ring_hdl != props->evt_ring_hdl) {
GSIERR("changing immutable fields not supported\n");
return -GSI_STATUS_UNSUPPORTED_OP;
}
mutex_lock(&ctx->mlock);
ctx->props = *props;
if (scr)
ctx->scratch = *scr;
gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
gsi_init_chan_ring(&ctx->props, &ctx->ring);
/* restore scratch */
__gsi_write_channel_scratch(chan_hdl, ctx->scratch);
mutex_unlock(&ctx->mlock);
return GSI_STATUS_SUCCESS;
}
EXPORT_SYMBOL(gsi_set_channel_cfg);
static void gsi_configure_ieps(void *base)
{
void __iomem *gsi_base = (void __iomem *)base;
gsi_writel(1, gsi_base + GSI_GSI_IRAM_PTR_CH_CMD_OFFS);
gsi_writel(2, gsi_base + GSI_GSI_IRAM_PTR_CH_DB_OFFS);
gsi_writel(3, gsi_base + GSI_GSI_IRAM_PTR_CH_DIS_COMP_OFFS);
gsi_writel(4, gsi_base + GSI_GSI_IRAM_PTR_CH_EMPTY_OFFS);
gsi_writel(5, gsi_base + GSI_GSI_IRAM_PTR_EE_GENERIC_CMD_OFFS);
gsi_writel(6, gsi_base + GSI_GSI_IRAM_PTR_EVENT_GEN_COMP_OFFS);
gsi_writel(7, gsi_base + GSI_GSI_IRAM_PTR_INT_MOD_STOPED_OFFS);
gsi_writel(8, gsi_base + GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_0_OFFS);
gsi_writel(9, gsi_base + GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_2_OFFS);
gsi_writel(10, gsi_base + GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_1_OFFS);
gsi_writel(11, gsi_base + GSI_GSI_IRAM_PTR_NEW_RE_OFFS);
gsi_writel(12, gsi_base + GSI_GSI_IRAM_PTR_READ_ENG_COMP_OFFS);
gsi_writel(13, gsi_base + GSI_GSI_IRAM_PTR_TIMER_EXPIRED_OFFS);
}
static void gsi_configure_bck_prs_matrix(void *base)
{
void __iomem *gsi_base = (void __iomem *)base;
/*
* For now, these are default values. In the future, GSI FW image will
* produce optimized back-pressure values based on the FW image.
*/
gsi_writel(0xfffffffe,
gsi_base + GSI_IC_DISABLE_CHNL_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffffff,
gsi_base + GSI_IC_DISABLE_CHNL_BCK_PRS_MSB_OFFS);
gsi_writel(0xffffffbf, gsi_base + GSI_IC_GEN_EVNT_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffffff, gsi_base + GSI_IC_GEN_EVNT_BCK_PRS_MSB_OFFS);
gsi_writel(0xffffefff, gsi_base + GSI_IC_GEN_INT_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffffff, gsi_base + GSI_IC_GEN_INT_BCK_PRS_MSB_OFFS);
gsi_writel(0xffffefff,
gsi_base + GSI_IC_STOP_INT_MOD_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffffff,
gsi_base + GSI_IC_STOP_INT_MOD_BCK_PRS_MSB_OFFS);
gsi_writel(0x00000000,
gsi_base + GSI_IC_PROCESS_DESC_BCK_PRS_LSB_OFFS);
gsi_writel(0x00000000,
gsi_base + GSI_IC_PROCESS_DESC_BCK_PRS_MSB_OFFS);
gsi_writel(0xf9ffffff, gsi_base + GSI_IC_TLV_STOP_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffffff, gsi_base + GSI_IC_TLV_STOP_BCK_PRS_MSB_OFFS);
gsi_writel(0xf9ffffff, gsi_base + GSI_IC_TLV_RESET_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffffff, gsi_base + GSI_IC_TLV_RESET_BCK_PRS_MSB_OFFS);
gsi_writel(0xffffffff, gsi_base + GSI_IC_RGSTR_TIMER_BCK_PRS_LSB_OFFS);
gsi_writel(0xfffffffe, gsi_base + GSI_IC_RGSTR_TIMER_BCK_PRS_MSB_OFFS);
gsi_writel(0xffffffff, gsi_base + GSI_IC_READ_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffefff, gsi_base + GSI_IC_READ_BCK_PRS_MSB_OFFS);
gsi_writel(0xffffffff, gsi_base + GSI_IC_WRITE_BCK_PRS_LSB_OFFS);
gsi_writel(0xffffdfff, gsi_base + GSI_IC_WRITE_BCK_PRS_MSB_OFFS);
gsi_writel(0xffffffff,
gsi_base + GSI_IC_UCONTROLLER_GPR_BCK_PRS_LSB_OFFS);
gsi_writel(0xff03ffff,
gsi_base + GSI_IC_UCONTROLLER_GPR_BCK_PRS_MSB_OFFS);
}
int gsi_configure_regs(phys_addr_t gsi_base_addr, u32 gsi_size,
phys_addr_t per_base_addr)
{
void __iomem *gsi_base;
gsi_base = ioremap_nocache(gsi_base_addr, gsi_size);
if (!gsi_base) {
GSIERR("ioremap failed for 0x%pa\n", &gsi_base_addr);
return -GSI_STATUS_RES_ALLOC_FAILURE;
}
gsi_writel(0, gsi_base + GSI_GSI_PERIPH_BASE_ADDR_MSB_OFFS);
gsi_writel(per_base_addr,
gsi_base + GSI_GSI_PERIPH_BASE_ADDR_LSB_OFFS);
gsi_configure_bck_prs_matrix((void *)gsi_base);
gsi_configure_ieps((void *)gsi_base);
iounmap(gsi_base);
return 0;
}
EXPORT_SYMBOL(gsi_configure_regs);
int gsi_enable_fw(phys_addr_t gsi_base_addr, u32 gsi_size)
{
void __iomem *gsi_base;
uint32_t value;
gsi_base = ioremap_nocache(gsi_base_addr, gsi_size);
if (!gsi_base) {
GSIERR("ioremap failed for 0x%pa\n", &gsi_base_addr);
return -GSI_STATUS_RES_ALLOC_FAILURE;
}
/* Enable the MCS and set to x2 clocks */
if (gsi_ctx->per.ver >= GSI_VER_1_2) {
value = ((1 << GSI_GSI_MCS_CFG_MCS_ENABLE_SHFT) &
GSI_GSI_MCS_CFG_MCS_ENABLE_BMSK);
gsi_writel(value, gsi_base + GSI_GSI_MCS_CFG_OFFS);
value = (((1 << GSI_GSI_CFG_GSI_ENABLE_SHFT) &
GSI_GSI_CFG_GSI_ENABLE_BMSK) |
((0 << GSI_GSI_CFG_MCS_ENABLE_SHFT) &
GSI_GSI_CFG_MCS_ENABLE_BMSK) |
((1 << GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_SHFT) &
GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_BMSK) |
((0 << GSI_GSI_CFG_UC_IS_MCS_SHFT) &
GSI_GSI_CFG_UC_IS_MCS_BMSK) |
((0 << GSI_GSI_CFG_GSI_PWR_CLPS_SHFT) &
GSI_GSI_CFG_GSI_PWR_CLPS_BMSK) |
((0 << GSI_GSI_CFG_BP_MTRIX_DISABLE_SHFT) &
GSI_GSI_CFG_BP_MTRIX_DISABLE_BMSK));
gsi_writel(value, gsi_base + GSI_GSI_CFG_OFFS);
} else {
value = (((1 << GSI_GSI_CFG_GSI_ENABLE_SHFT) &
GSI_GSI_CFG_GSI_ENABLE_BMSK) |
((1 << GSI_GSI_CFG_MCS_ENABLE_SHFT) &
GSI_GSI_CFG_MCS_ENABLE_BMSK) |
((1 << GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_SHFT) &
GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_BMSK) |
((0 << GSI_GSI_CFG_UC_IS_MCS_SHFT) &
GSI_GSI_CFG_UC_IS_MCS_BMSK));
gsi_writel(value, gsi_base + GSI_GSI_CFG_OFFS);
}
iounmap(gsi_base);
return 0;
}
EXPORT_SYMBOL(gsi_enable_fw);
void gsi_get_inst_ram_offset_and_size(unsigned long *base_offset,
unsigned long *size)
{
if (base_offset)
*base_offset = GSI_GSI_INST_RAM_n_OFFS(0);
if (size)
*size = GSI_GSI_INST_RAM_n_WORD_SZ *
(GSI_GSI_INST_RAM_n_MAXn + 1);
}
EXPORT_SYMBOL(gsi_get_inst_ram_offset_and_size);
int gsi_halt_channel_ee(unsigned int chan_idx, unsigned int ee, int *code)
{
enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_HALT_CHANNEL;
uint32_t val;
int res;
if (!gsi_ctx) {
pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
return -GSI_STATUS_NODEV;
}
if (chan_idx >= gsi_ctx->max_ch || !code) {
GSIERR("bad params chan_idx=%d\n", chan_idx);
return -GSI_STATUS_INVALID_PARAMS;
}
mutex_lock(&gsi_ctx->mlock);
reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
/* invalidate the response */
gsi_ctx->scratch.word0.val = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
gsi_writel(gsi_ctx->scratch.word0.val, gsi_ctx->base +
GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
gsi_ctx->gen_ee_cmd_dbg.halt_channel++;
val = (((op << GSI_EE_n_GSI_EE_GENERIC_CMD_OPCODE_SHFT) &
GSI_EE_n_GSI_EE_GENERIC_CMD_OPCODE_BMSK) |
((chan_idx << GSI_EE_n_GSI_EE_GENERIC_CMD_VIRT_CHAN_IDX_SHFT) &
GSI_EE_n_GSI_EE_GENERIC_CMD_VIRT_CHAN_IDX_BMSK) |
((ee << GSI_EE_n_GSI_EE_GENERIC_CMD_EE_SHFT) &
GSI_EE_n_GSI_EE_GENERIC_CMD_EE_BMSK));
gsi_writel(val, gsi_ctx->base +
GSI_EE_n_GSI_EE_GENERIC_CMD_OFFS(gsi_ctx->per.ee));
res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
msecs_to_jiffies(GSI_CMD_TIMEOUT));
if (res == 0) {
GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
res = -GSI_STATUS_TIMED_OUT;
goto free_lock;
}
gsi_ctx->scratch.word0.val = gsi_readl(gsi_ctx->base +
GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
GSIERR("No response received\n");
res = -GSI_STATUS_ERROR;
goto free_lock;
}
res = GSI_STATUS_SUCCESS;
*code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
free_lock:
mutex_unlock(&gsi_ctx->mlock);
return res;
}
EXPORT_SYMBOL(gsi_halt_channel_ee);
static int msm_gsi_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
pr_debug("gsi_probe\n");
gsi_ctx = devm_kzalloc(dev, sizeof(*gsi_ctx), GFP_KERNEL);
if (!gsi_ctx) {
dev_err(dev, "failed to allocated gsi context\n");
return -ENOMEM;
}
gsi_ctx->ipc_logbuf = ipc_log_context_create(GSI_IPC_LOG_PAGES,
"gsi", 0);
if (gsi_ctx->ipc_logbuf == NULL)
GSIERR("failed to create IPC log, continue...\n");
gsi_ctx->dev = dev;
init_completion(&gsi_ctx->gen_ee_cmd_compl);
gsi_debugfs_init();
return 0;
}
static struct platform_driver msm_gsi_driver = {
.probe = msm_gsi_probe,
.driver = {
.owner = THIS_MODULE,
.name = "gsi",
.of_match_table = msm_gsi_match,
},
};
/**
* Module Init.
*/
static int __init gsi_init(void)
{
pr_debug("gsi_init\n");
return platform_driver_register(&msm_gsi_driver);
}
arch_initcall(gsi_init);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Generic Software Interface (GSI)");
|