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
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
|
/* $Id: DevXHCI.cpp $ */
/** @file
* DevXHCI - eXtensible Host Controller Interface for USB.
*/
/*
* Copyright (C) 2012-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/** @page pg_dev_xhci xHCI - eXtensible Host Controller Interface Emulation.
*
* This component implements an xHCI USB controller.
*
* The xHCI device is significantly different from the EHCI and OHCI
* controllers in that it is not timer driven. A worker thread is responsible
* for transferring data between xHCI and VUSB.
*
* Since there can be dozens or even hundreds of USB devices, and because USB
* transfers must share the same bus, only one worker thread is created (per
* host controller).
*
*
* The xHCI operational model is heavily based around a producer/consumer
* model utilizing rings -- Command, Event, and Transfer rings. The Event ring
* is only written by the xHC and is read-only for the HCD (Host Controller
* Driver). The Command/Transfer rings are only written by the HCD and are
* read-only for the xHC.
*
* The rings contain TRBs (Transfer Request Blocks). The TRBs represent not
* only data transfers but also commands and status information. Each type of
* ring only produces/consumes specific TRB types.
*
* When processing a ring, the xHC simply keeps advancing an internal pointer.
* For the Command/Transfer rings, the HCD uses Link TRBs to manage the ring
* storage in a fairly arbitrary manner. Since the HCD cannot write to the
* Event ring, the Event Ring Segment Table (ERST) is used to manage the ring
* storage instead.
*
* The Cycle bit is used to manage the ring buffer full/empty condition. The
* Producer and Consumer both have their own Cycle State (PCS/CCS). The Cycle
* bit of each TRB determines who owns it. The consumer only processes TRBs
* whose Cycle bit matches the CCS. HCD software typically toggles the Cycle
* bit on each pass through the ring. The Link TRB can be used to toggle the
* CCS accordingly.
*
* Multiple Transfer TRBs can be chained together (via the Chain bit) into a
* single Transfer Descriptor (TD). This provides a convenient capability for
* the HCD to turn a URB into a single TD regardless of how the URB is laid
* out in physical memory. If a transfer encounters an error or is terminated
* by a short packet, the entire TD (i.e. chain of TRBs) is retired.
*
* Note that the xHC detects and handles short packets on its own. Backends
* are always asked not to consider a short packet to be an error condition.
*
* Command and Event TRBs cannot be chained, thus an ED (Event Descriptor)
* or a Command Descriptor (CD) always consists of a single TRB.
*
* There is one Command ring per xHC, one Event ring per interrupter (one or
* more), and a potentially very large number of Transfer rings. There is a
* 1:1 mapping between Transfer Rings and USB pipes, hence each USB device
* uses 1-31 Transfer rings (at least one for the default control endpoint,
* up to 31 if all IN/OUT endpoints are used). USB 3.0 devices may also use
* up to 64K streams per endpoint, each with its Transfer ring, massively
* increasing the potential number of Transfer rings in use.
*
* When building a Transfer ring, it's possible to queue up a large number
* of TDs and as soon as the oldest ones are retired, queue up new TDs. The
* Transfer ring might thus never be empty.
*
* For tracking ring buffer position, the TRDP and TREP fields in an endpoint
* context are used. The TRDP is the 'TR Dequeue Pointer', i.e. the position
* of the next TRB to be completed. This field is visible by the HCD when the
* endpoint isn't running. It reflects TRBs completely processed by the xHC
* and hence no longer owned by the xHC.
*
* The TREP field is the 'TR Enqueue Pointer' and tracks the position of the
* next TRB to start processing (submit). This is purely internal to the
* xHC. The TREP can potentially get far ahead of the TRDP, but only in the
* part of the ring owned by the xHC (i.e. with matching DCS bit).
*
* Unlike most other xHCI data structures, transfer TRBs may describe memory
* buffers with no alignment restrictions (both starting position and size).
* In addition, there is no relationship between TRB boundaries and USB
* packet boundaries.
*
*
* Typically an event would be generated via the IOC bit (Interrupt On
* Completion) when the last TRB of a TD is completed. However, multiple IOC
* bits may be set per TD. This may be required when a TD equal or larger
* than 16MB is used, since transfer events utilize a 24-bit length field.
*
* There is also the option of using Transfer Event TRBs to report TRB
* completion. Transfer Event TRBs may be freely intermixed with transfer
* TRBs. Note that an event TRB will produce an event reporting the size of
* data transferred since the last event TRB or since the beginning of a TD.
* The xHC submits URBs such that they either comprise the entire TD or end
* at a Transfer Event TRB, thus there is no need to track the EDTLA
* separately.
*
* Transfer errors always generate events, irrespective of IOC settings. The
* xHC has always the option to generate events at implementation-specific
* points so that the HCD does not fall too far behind.
*
* Control transfers use special TDs. A Setup Stage TD consists of only a
* single Setup Stage TRB (there's no Chain bit). The optional Data Stage
* TD consists of a Data Stage TRB chained to zero or more Normal TRBs
* and/or Event Data TRBs. The Status Stage TD then consists of a Status
* Stage TRB optionally chained to an Event Data TRB. The HCD is responsible
* for building the TDs correctly.
*
* For isochronous transfers, only the first TRB of a TD is actually an
* isochronous TRB. If the TD is chained, it will contain Normal TRBs (and
* possibly Event Data TRBs).
*
*
* Isochronous transfers require multiple TDs/URBs to be in flight at a
* time. This complicates dealing with non-data TRBs (such as link or event
* data TRBs). These TRBs cannot be completed while a previous TRB is still
* in flight. They are completed either: a) when submitting URBs and there
* are no in-flight URBs, or b) just prior to completing an URB.
*
* This approach works because URBs must be completed strictly in-order. The
* TRDP and TREP determine whether there are in-flight TRBs (TREP equals
* TRDP if and only if there are no in-flight TRBs).
*
* When submitting TRBs and there is in-flight traffic, non-data TRBs must
* be examined and skipped over. Link TRBs need to be taken into account.
*
* Unfortunately, certain HCDs (looking at you, Microsoft!) violate the xHCI
* specification and make assumptions about how far ahead of the TRDP the
* xHC can get. We have to artificially limit the number of in-flight TDs
* for this reason.
*
* Non-isochronous TRBs do not require this treatment for correct function
* but are likely to benefit performance-wise from the pipelining.
*
* With high-speed and faster transfers, there is an added complication for
* endpoints with more than one transfer per frame, i.e. short intervals. At
* least some host USB stacks require URBs to cover an entire frame, which
* means we may have to glue together several TDs into a single URB.
*
*
* A buggy or malicious guest can create a transfer or command ring that
* loops in on itself (in the simplest case using a sequence of one or more
* link TRBs where the last TRB points to the beginning of the sequence).
* Such a loop would effectively hang the processing thread. Since we cannot
* easily detect a generic loop, and because even non-looped TRB/command
* rings might contain extremely large number of items, we limit the number
* of entries that we are willing to process at once. If the limit is
* crossed, the xHC reports a host controller error and shuts itself down
* until it's reset.
*
* Note that for TRB lists, both URB submission and completion must protect
* against loops because the lists in guest memory are not guaranteed to stay
* unchanged between submitting and completing URBs.
*
* The event ring is not susceptible to loops because the xHC is the producer,
* not consumer. The event ring can run out of space but that is not a fatal
* problem.
*
*
* The interrupt logic uses an internal IPE (Interrupt Pending Enable) bit
* which controls whether the register-visible IP (Interrupt Pending) bit
* can be set. The IPE bit is set when a non-blocking event (BEI bit clear)
* is enqueued. The IPE bit is cleared when the event ring is initialized or
* transitions to empty (i.e. ERDP == EREP). When IPE transtitions to set,
* it will set IP unless the EHB (Event Handler Busy) bit is set or IMODC
* (Interrupt Moderation Counter) is non-zero. When IMODC counts down to
* zero, it sets the IP bit if IPE is set and EHB is not. Setting the IP bit
* triggers interrupt delivery. Note that clearing the IPE bit does not
* change the IP bit state.
*
* Interrupt delivery depends on whether MSI/MSI-X is in use or not. With MSI,
* an interrupter's IP (Interrupt Pending) bit is cleared as soon as the MSI
* message is written; with classic PCI interrupt delivery, the HCD must clear
* the IP bit. However, the EHB (Event Handler Busy) bit is always set, which
* causes further interrupts to be blocked on the interrupter until the HCD
* processes pending events and clears the EHB bit.
*
* Note that clearing the EHB bit may immediately trigger an interrupt if
* additional event TRBs were queued up while the HCD was processing previous
* ones.
*
*
* Each enabled USB device has a corresponding slot ID, a doorbell, as well as
* a device context which can be accessed through the DCBAA (Device Context
* Base Address Array). Valid slot IDs are in the 1-255 range; the first entry
* (i.e. index 0) in the DCBAA may optionally point to the Scratchpad Buffer
* Array, while doorbell 0 is associated with the Command Ring.
*
* While 255 valid slot IDs is an xHCI architectural limit, existing xHC
* implementations usually set a considerably lower limit, such as 32. See
* the XHCI_NDS constant.
*
* It would be tempting to use the DCBAA to determine which slots are free.
* Unfortunately the xHC is not allowed to access DCBAA entries which map to
* disabled slots (see section 6.1). A parallel aSlotState array is hence used
* to internally track the slot state and find available slots. Once a slot
* is enabled, the slot context entry in the DCBAA is used to track the
* slot state.
*
*
* Unlike OHCI/UHCI/EHCI, the xHC much more closely tracks USB device state.
* HCDs are not allowed to issue SET_ADDRESS requests at all and must use
* the Address Device xHCI command instead.
*
* HCDs can use SET_CONFIGURATION and SET_INTERFACE requests normally, but
* must inform the xHC of the changes via Configure Endpoint and Evaluate
* Context commands. Similarly there are Reset Endpoint and Stop Endpoint
* commands to manage endpoint state.
*
* A corollary of the above is that unlike OHCI/UHCI/EHCI, with xHCI there
* are very clear rules and a straightforward protocol for managing
* ownership of structures in physical memory. During normal operation, the
* xHC owns all device context memory and the HCD must explicitly ask the xHC
* to relinquish the ownership.
*
* The xHCI architecture offers an interesting feature in that it reserves
* opaque fields for xHCI use in certain data structures (slot and endpoint
* contexts) and gives the xHC an option to request scratchpad buffers that
* a HCD must provide. The xHC may use the opaque storage and/or scratchpad
* buffers for saving internal state.
*
* For implementation reasons, the xHCI device creates two root hubs on the
* VUSB level; one for USB2 devices (USB 1.x and 2.0), one for USB3. The
* behavior of USB2 vs. USB3 ports is different, and a device can only be
* attached to either one or the other hub. However, there is a single array
* of ports to avoid overly complicating the code, given that port numbering
* is linear and encompasses both USB2 and USB3 ports.
*
*
* The default emulated device is an Intel 7-Series xHC aka Panther Point.
* This was Intel's first xHC and is widely supported. It is also possible
* to select an Intel 8-Series xHC aka Lynx Point; this is only useful for
* debugging and requires the 'other' set of Windows 7 drivers.
*
* For Windows XP guest support, it is possible to emulate a Renesas
* (formerly NEC) uPD720201 xHC. It would be possible to emulate the earlier
* NEC chips but those a) only support xHCI 0.96, and b) their drivers
* require a reboot during installation. Renesas' drivers also support
* Windows Vista and 7.
*
*
* NB: Endpoints are addressed differently in xHCI and USB. In USB,
* endpoint addresses are 8-bit values with the low four bits identifying
* the endpoint number and the high bit indicating the direction (0=OUT,
* 1=IN); see e.g. 9.3.4 in USB 2.0 spec. In xHCI, endpoint addresses are
* used as DCIs (Device Context Index) and for that reason, they're
* compressed into 5 bits where the lowest bit(!) indicates direction (again
* 1=IN) and bits 1-4 designate the endpoint number. Endpoint 0 is somewhat
* special and uses DCI 1. See 4.8.1 in xHCI spec.
*
*
* NB: A variable named iPort is a zero-based index into the port array.
* On the other hand, a variable named uPort is a one-based port number!
* The implementation (obviously) uses zero-based indexing, but USB ports
* are numbered starting with 1. The same is true of xHCI slot numbering.
* The macros IDX_TO_ID() and ID_TO_IDX(a) should be used to convert between
* the two numbering conventions to make the intent clear.
*
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_XHCI
#include <VBox/pci.h>
#include <VBox/msi.h>
#include <VBox/vmm/pdm.h>
#include <VBox/err.h>
#include <VBox/log.h>
#include <iprt/assert.h>
#ifdef IN_RING3
# include <iprt/uuid.h>
# include <iprt/critsect.h>
#endif
#include <VBox/vusb.h>
#ifdef VBOX_IN_EXTPACK_R3
# include <VBox/version.h>
#endif
#ifndef VBOX_IN_EXTPACK
# include "VBoxDD.h"
#endif
/*********************************************************************************************************************************
* (Most of the) Defined Constants, Macros and Structures *
*********************************************************************************************************************************/
/* Optional error injection support via DBGF. */
//#define XHCI_ERROR_INJECTION
/** The saved state version. */
#define XHCI_SAVED_STATE_VERSION 1
/** Convert a zero-based index to a 1-based ID. */
#define IDX_TO_ID(a) (a + 1)
/** Convert a 1-based ID to a zero-based index. */
#define ID_TO_IDX(a) (a - 1)
/** PCI device related constants. */
#define XHCI_PCI_MSI_CAP_OFS 0x80
/** Number of LUNs/root hubs. One each for USB2/USB3. */
#define XHCI_NUM_LUNS 2
/** @name The following two constants were determined experimentally.
* They determine the maximum number of TDs allowed to be in flight.
* NB: For isochronous TDs, the number *must* be limited because
* Windows 8+ violates the xHCI specification and does not keep
* the transfer rings consistent.
* @{
*/
//#define XHCI_MAX_ISOC_IN_FLIGHT 3 /* Scarlett needs 3; was 12 */
#define XHCI_MAX_ISOC_IN_FLIGHT 12
#define XHCI_MAX_BULK_IN_FLIGHT 8
/** @} */
/** @name Implementation limit on the number of TRBs and commands
* the xHC is willing to process at once. A larger number is taken
* to indicate a broken or malicious guest, and causes a HC error.
* @{
*/
#define XHCI_MAX_NUM_CMDS 128
#define XHCI_MAX_NUM_TRBS 1024
/** @} */
/** Implementation TD size limit. Prevents EDTLA wrap-around. */
#define XHCI_MAX_TD_SIZE (16 * _1M - 1)
/** Special value to prevent further queuing. */
#define XHCI_NO_QUEUING_IN_FLIGHT (XHCI_MAX_BULK_IN_FLIGHT * 2)
/* Structural Parameters #1 (HCSPARAMS1) values. */
/** Maximum allowed Number of Downstream Ports on the root hub. Careful
* when changing -- other structures may need adjusting!
*/
#define XHCI_NDP_MAX 32
/** Default number of USB 2.0 ports.
*
* @note AppleUSBXHCI does not handle more than 15 ports. At least OS X
* 10.8.2 crashes if we report more than 15 ports! Hence the default
* is 8 USB2 + 6 USB3 ports for a total of 14 so that OS X is happy.
*/
#define XHCI_NDP_20_DEFAULT 8
/** Default number of USB 3.0 ports. */
#define XHCI_NDP_30_DEFAULT 6
/** Number of interrupters. */
#define XHCI_NINTR 8
/** Mask for interrupter indexing. */
#define XHCI_INTR_MASK (XHCI_NINTR - 1)
/* The following is only true if XHCI_NINTR is a (non-zero) power of two. */
AssertCompile((XHCI_NINTR & XHCI_INTR_MASK) == 0);
/** Number of Device Slots. Determines the number of doorbell
* registers and device slots, among other things. */
#define XHCI_NDS 32
/* Enforce xHCI architectural limits on HCSPARAMS1. */
AssertCompile(XHCI_NDP_MAX < 255 && XHCI_NINTR < 1024 && XHCI_NDS < 255);
AssertCompile(XHCI_NDP_20_DEFAULT + XHCI_NDP_30_DEFAULT <= XHCI_NDP_MAX);
AssertCompile(XHCI_NDP_MAX <= XHCI_NDS);
/* Structural Parameters #2 (HCSPARAMS2) values. */
/** Isochronous Scheduling Threshold. */
#define XHCI_IST (RT_BIT(3) | 1) /* One frame. */
/** Max number of Event Ring Segment Table entries as a power of two. */
#define XHCI_ERSTMAX_LOG2 5
/** Max number of Event Ring Segment Table entries. */
#define XHCI_ERSTMAX RT_BIT(XHCI_ERSTMAX_LOG2)
/* Enforce xHCI architectural limits on HCSPARAMS2. */
AssertCompile(XHCI_ERSTMAX_LOG2 < 16);
/** Size of the xHCI memory-mapped I/O region. */
#define XHCI_MMIO_SIZE _64K
/** Size of the capability part of the MMIO region. */
#define XHCI_CAPS_REG_SIZE 0x80
/** Offset of the port registers in operational register space. */
#define XHCI_PORT_REG_OFFSET 0x400
/** Offset of xHCI extended capabilities in MMIO region. */
#define XHCI_XECP_OFFSET 0x1000
/** Offset of the run-time registers in MMIO region. */
#define XHCI_RTREG_OFFSET 0x2000
/** Offset of the doorbell registers in MMIO region. */
#define XHCI_DOORBELL_OFFSET 0x3000
/** Size of the extended capability area. */
#define XHCI_EXT_CAP_SIZE 1024
/* Make sure we can identify MMIO register accesses properly. */
AssertCompile(XHCI_DOORBELL_OFFSET > XHCI_RTREG_OFFSET);
AssertCompile(XHCI_XECP_OFFSET > XHCI_PORT_REG_OFFSET + XHCI_CAPS_REG_SIZE);
AssertCompile(XHCI_RTREG_OFFSET > XHCI_XECP_OFFSET + XHCI_EXT_CAP_SIZE);
/** Maximum size of a single extended capability. */
#define MAX_XCAP_SIZE 256
/** @name xHCI Extended capability types.
* @{ */
#define XHCI_XCP_USB_LEGACY 1 /**< USB legacy support. */
#define XHCI_XCP_PROTOCOL 2 /**< Protocols supported by ports. */
#define XHCI_XCP_EXT_PM 3 /**< Extended power management (non-PCI). */
#define XHCI_XCP_IOVIRT 4 /**< Hardware xHCI virtualization support. */
#define XHCI_XCP_MSI 5 /**< Message interrupts (non-PCI). */
#define XHCI_XCP_LOCAL_MEM 6 /**< Local memory (for debug support). */
#define XHCI_XCP_USB_DEBUG 10 /**< USB debug capability. */
#define XHCI_XCP_EXT_MSI 17 /**< MSI-X (non-PCI). */
/** @} */
/* xHCI Register Bits. */
/** @name Capability Parameters (HCCPARAMS) bits
* @{ */
#define XHCI_HCC_AC64 RT_BIT(0) /**< RO */
#define XHCI_HCC_BNC RT_BIT(1) /**< RO */
#define XHCI_HCC_CSZ RT_BIT(2) /**< RO */
#define XHCI_HCC_PPC RT_BIT(3) /**< RO */
#define XHCI_HCC_PIND RT_BIT(4) /**< RO */
#define XHCI_HCC_LHRC RT_BIT(5) /**< RO */
#define XHCI_HCC_LTC RT_BIT(6) /**< RO */
#define XHCI_HCC_NSS RT_BIT(7) /**< RO */
#define XHCI_HCC_MAXPSA_MASK (RT_BIT(12)|RT_BIT(13)|RT_BIT(14)| RT_BIT(15)) /**< RO */
#define XHCI_HCC_MAXPSA_SHIFT 12
#define XHCI_HCC_XECP_MASK 0xFFFF0000 /**< RO */
#define XHCI_HCC_XECP_SHIFT 16
/** @} */
/** @name Command Register (USBCMD) bits
* @{ */
#define XHCI_CMD_RS RT_BIT(0) /**< RW - Run/Stop */
#define XHCI_CMD_HCRST RT_BIT(1) /**< RW - Host Controller Reset */
#define XHCI_CMD_INTE RT_BIT(2) /**< RW - Interrupter Enable */
#define XHCI_CMD_HSEE RT_BIT(3) /**< RW - Host System Error Enable */
#define XHCI_CMD_LCRST RT_BIT(7) /**< RW - Light HC Reset */
#define XHCI_CMD_CSS RT_BIT(8) /**< RW - Controller Save State */
#define XHCI_CMD_CRS RT_BIT(9) /**< RW - Controller Restore State */
#define XHCI_CMD_EWE RT_BIT(10) /**< RW - Enable Wrap Event */
#define XHCI_CMD_EU3S RT_BIT(11) /**< RW - Enable U3 MFINDEX Stop */
#define XHCI_CMD_MASK ( XHCI_CMD_RS | XHCI_CMD_HCRST | XHCI_CMD_INTE | XHCI_CMD_HSEE | XHCI_CMD_LCRST \
| XHCI_CMD_CSS | XHCI_CMD_CRS | XHCI_CMD_EWE | XHCI_CMD_EU3S)
/** @} */
/** @name Status Register (USBSTS) bits
* @{ */
#define XHCI_STATUS_HCH RT_BIT(0) /**< RO - HC Halted */
#define XHCI_STATUS_HSE RT_BIT(2) /**< RW1C - Host System Error */
#define XHCI_STATUS_EINT RT_BIT(3) /**< RW1C - Event Interrupt */
#define XHCI_STATUS_PCD RT_BIT(4) /**< RW1C - Port Change Detect */
#define XHCI_STATUS_SSS RT_BIT(8) /**< RO - Save State Status */
#define XHCI_STATUS_RSS RT_BIT(9) /**< RO - Resture State Status */
#define XHCI_STATUS_SRE RT_BIT(10) /**< RW1C - Save/Restore Error */
#define XHCI_STATUS_CNR RT_BIT(11) /**< RO - Controller Not Ready */
#define XHCI_STATUS_HCE RT_BIT(12) /**< RO - Host Controller Error */
#define XHCI_STATUS_WRMASK (XHCI_STATUS_HSE | XHCI_STATUS_EINT | XHCI_STATUS_PCD | XHCI_STATUS_SRE)
/** @} */
/** @name Default xHCI speed definitions (7.2.2.1.1)
* @{ */
#define XHCI_SPD_FULL 1
#define XHCI_SPD_LOW 2
#define XHCI_SPD_HIGH 3
#define XHCI_SPD_SUPER 4
/** @} */
/** @name Port Status and Control Register bits (PORTSCUSB2/PORTSCUSB3)
* @{ */
#define XHCI_PORT_CCS RT_BIT(0) /**< ROS - Current Connection Status */
#define XHCI_PORT_PED RT_BIT(1) /**< RW1S - Port Enabled/Disabled */
#define XHCI_PORT_OCA RT_BIT(3) /**< RO - Over-current Active */
#define XHCI_PORT_PR RT_BIT(4) /**< RW1S - Port Reset */
#define XHCI_PORT_PLS_MASK (RT_BIT(5) | RT_BIT(6) | RT_BIT(7) | RT_BIT(8)) /**< RWS */
#define XHCI_PORT_PLS_SHIFT 5
#define XHCI_PORT_PP RT_BIT(9) /**< RWS - Port Power */
#define XHCI_PORT_SPD_MASK (RT_BIT(10) | RT_BIT(11) | RT_BIT(12) | RT_BIT(13)) /**< ROS */
#define XHCI_PORT_SPD_SHIFT 10
#define XHCI_PORT_LWS RT_BIT(16) /**< RW - Link State Write Strobe */
#define XHCI_PORT_CSC RT_BIT(17) /**< RW1CS - Connect Status Change */
#define XHCI_PORT_PEC RT_BIT(18) /**< RW1CS - Port Enabled/Disabled Change */
#define XHCI_PORT_WRC RT_BIT(19) /**< RW1CS - Warm Port Reset Change */
#define XHCI_PORT_OCC RT_BIT(20) /**< RW1CS - Over-current Change */
#define XHCI_PORT_PRC RT_BIT(21) /**< RW1CS - Port Reset Change */
#define XHCI_PORT_PLC RT_BIT(22) /**< RW1CS - Port Link State Change */
#define XHCI_PORT_CEC RT_BIT(23) /**< RW1CS - Port Config Error Change */
#define XHCI_PORT_CAS RT_BIT(24) /**< RO - Cold Attach Status */
#define XHCI_PORT_WCE RT_BIT(25) /**< RWS - Wake on Connect Enable */
#define XHCI_PORT_WDE RT_BIT(26) /**< RWS - Wake on Disconnect Enable */
#define XHCI_PORT_WOE RT_BIT(27) /**< RWS - Wake on Over-current Enable */
#define XHCI_PORT_DR RT_BIT(30) /**< RO - Device (Not) Removable */
#define XHCI_PORT_WPR RT_BIT(31) /**< RW1S - Warm Port Reset */
#define XHCI_PORT_RESERVED (RT_BIT(2) | RT_BIT(14) | RT_BIT(15) | RT_BIT(28) | RT_BIT(29))
#define XHCI_PORT_WAKE_MASK (XHCI_PORT_WCE|XHCI_PORT_WDE|XHCI_PORT_WOE)
#define XHCI_PORT_CHANGE_MASK (XHCI_PORT_CSC|XHCI_PORT_PEC|XHCI_PORT_WRC|XHCI_PORT_OCC|XHCI_PORT_PRC|XHCI_PORT_PLC|XHCI_PORT_CEC)
#define XHCI_PORT_CTL_RW_MASK (XHCI_PORT_PP|XHCI_PORT_LWS)
#define XHCI_PORT_CTL_W1_MASK (XHCI_PORT_PED|XHCI_PORT_PR|XHCI_PORT_WPR)
#define XHCI_PORT_RO_MASK (XHCI_PORT_CCS|XHCI_PORT_OCA|XHCI_PORT_SPD_MASK|XHCI_PORT_CAS|XHCI_PORT_DR)
/** @} */
/** @name Port Link State values
* @{ */
#define XHCI_PLS_U0 0 /**< U0 State. */
#define XHCI_PLS_U1 1 /**< U1 State. */
#define XHCI_PLS_U2 2 /**< U2 State. */
#define XHCI_PLS_U3 3 /**< U3 State (Suspended). */
#define XHCI_PLS_DISABLED 4 /**< Disabled. */
#define XHCI_PLS_RXDETECT 5 /**< RxDetect. */
#define XHCI_PLS_INACTIVE 6 /**< Inactive. */
#define XHCI_PLS_POLLING 7 /**< Polling. */
#define XHCI_PLS_RECOVERY 8 /**< Recovery. */
#define XHCI_PLS_HOTRST 9 /**< Hot Reset. */
#define XHCI_PLS_CMPLMODE 10 /**< Compliance Mode. */
#define XHCI_PLS_TSTMODE 11 /**< Test Mode. */
/* Values 12-14 are reserved. */
#define XHCI_PLS_RESUME 15 /**< Resume. */
/** @} */
/** @name Command Ring Control Register (CRCR) bits
* @{ */
#define XHCI_CRCR_RCS RT_BIT(0) /**< RW - Ring Cycle State */
#define XHCI_CRCR_CS RT_BIT(1) /**< RW1S - Command Stop */
#define XHCI_CRCR_CA RT_BIT(2) /**< RW1S - Command Abort */
#define XHCI_CRCR_CRR RT_BIT(3) /**< RO - Command Ring Running */
#define XHCI_CRCR_RD_MASK UINT64_C(0xFFFFFFFFFFFFFFF8) /* Mask off bits always read as zero. */
#define XHCI_CRCR_ADDR_MASK UINT64_C(0xFFFFFFFFFFFFFFC0)
#define XHCI_CRCR_UPD_MASK (XHCI_CRCR_ADDR_MASK | XHCI_CRCR_RCS)
/** @} */
/** @name Interrupter Management Register (IMAN) bits
* @{ */
#define XHCI_IMAN_IP RT_BIT(0) /**< RW1C - Interrupt Pending */
#define XHCI_IMAN_IE RT_BIT(1) /**< RW - Interrupt Enable */
#define XHCI_IMAN_VALID_MASK (XHCI_IMAN_IP | XHCI_IMAN_IE)
/** @} */
/** @name Interrupter Moderation Register (IMOD) bits
* @{ */
#define XHCI_IMOD_IMODC_MASK 0xFFFF0000 /**< RW */
#define XHCI_IMOD_IMODC_SHIFT 16
#define XHCI_IMOD_IMODI_MASK 0x0000FFFF /**< RW */
/** @} */
/** @name Event Ring Segment Table Size Register (ERSTSZ) bits
* @{ */
#define XHCI_ERSTSZ_MASK 0x0000FFFF /**< RW */
/** @} */
/** @name Event Ring Segment Table Base Address Register (ERSTBA) bits
* @{ */
#define XHCI_ERST_ADDR_MASK UINT64_C(0xFFFFFFFFFFFFFFC0)
/** @} */
/** For reasons that are not obvious, NEC/Renesas xHCs only require 16-bit
* alignment for the ERST base. This is not in line with the xHCI spec
* (which requires 64-bit alignment) but is clearly documented by NEC.
*/
#define NEC_ERST_ADDR_MASK UINT64_C(0xFFFFFFFFFFFFFFF0)
/** Firmware revision reported in NEC/Renesas mode. Value chosen based on
* OS X driver check (OS X supports these chips since they're commonly
* found in ExpressCards).
*/
#define NEC_FW_REV 0x3028
/** @name Event Ring Deqeue Pointer Register (ERDP) bits
* @{ */
#define XHCI_ERDP_DESI_MASK 0x00000007 /**< RW - Dequeue ERST Segment Index */
#define XHCI_ERDP_EHB RT_BIT(3) /**< RW1C - Event Handler Busy */
#define XHCI_ERDP_ADDR_MASK UINT64_C(0xFFFFFFFFFFFFFFF0) /**< RW - ERDP address mask */
/** @} */
/** @name Device Context Base Address Array (DCBAA) definitions
* @{ */
#define XHCI_DCBAA_ADDR_MASK UINT64_C(0xFFFFFFFFFFFFFFC0) /**< Applies to DCBAAP and its entries. */
/** @} */
/** @name Doorbell Register bits
* @{ */
#define XHCI_DB_TGT_MASK 0x000000FF /**< DB Target mask. */
#define XHCI_DB_STRMID_SHIFT 16 /**< DB Stream ID shift. */
#define XHCI_DB_STRMID_MASK 0xFFFF0000 /**< DB Stream ID mask. */
/** @} */
/** Address mask for device/endpoint/input contexts. */
#define XHCI_CTX_ADDR_MASK UINT64_C(0xFFFFFFFFFFFFFFF0)
/** @name TRB Completion Codes
* @{ */
#define XHCI_TCC_INVALID 0 /**< CC field not updated. */
#define XHCI_TCC_SUCCESS 1 /**< Successful TRB completion. */
#define XHCI_TCC_DATA_BUF_ERR 2 /**< Overrun/underrun. */
#define XHCI_TCC_BABBLE 3 /**< Babble detected. */
#define XHCI_TCC_USB_XACT_ERR 4 /**< USB transaction error. */
#define XHCI_TCC_TRB_ERR 5 /**< TRB error detected. */
#define XHCI_TCC_STALL 6 /**< USB Stall detected. */
#define XHCI_TCC_RSRC_ERR 7 /**< Inadequate xHC resources. */
#define XHCI_TCC_BWIDTH_ERR 8 /**< Unable to allocate bandwidth. */
#define XHCI_TCC_NO_SLOTS 9 /**< MaxSlots (NDS) exceeded. */
#define XHCI_TCC_INV_STRM_TYP 10 /**< Invalid stream context type. */
#define XHCI_TCC_SLOT_NOT_ENB 11 /**< Slot not enabled. */
#define XHCI_TCC_EP_NOT_ENB 12 /**< Endpoint not enabled. */
#define XHCI_TCC_SHORT_PKT 13 /**< Short packet detected. */
#define XHCI_TCC_RING_UNDERRUN 14 /**< Transfer ring underrun. */
#define XHCI_TCC_RING_OVERRUN 15 /**< Transfer ring overrun. */
#define XHCI_TCC_VF_RING_FULL 16 /**< VF event ring full. */
#define XHCI_TCC_PARM_ERR 17 /**< Invalid context parameter. */
#define XHCI_TCC_BWIDTH_OVER 18 /**< Isoc bandwidth overrun. */
#define XHCI_TCC_CTX_STATE_ERR 19 /**< Transition from illegal context state. */
#define XHCI_TCC_NO_PING 20 /**< No ping response in time. */
#define XHCI_TCC_EVT_RING_FULL 21 /**< Event Ring full. */
#define XHCI_TCC_DEVICE_COMPAT 22 /**< Incompatible device detected. */
#define XHCI_TCC_MISS_SVC 23 /**< Missed isoc service. */
#define XHCI_TCC_CMDR_STOPPED 24 /**< Command ring stopped. */
#define XHCI_TCC_CMD_ABORTED 25 /**< Command aborted. */
#define XHCI_TCC_STOPPED 26 /**< Endpoint stopped. */
#define XHCI_TCC_STP_INV_LEN 27 /**< EP stopped, invalid transfer length. */
/* 28 Reserved. */
#define XHCI_TCC_MAX_EXIT_LAT 29 /**< Max exit latency too large. */
/* 30 Reserved. */
#define XHCI_TCC_ISOC_OVERRUN 31 /**< Isochronous buffer overrun. */
#define XHCI_TCC_EVT_LOST 32 /**< Event lost due to overrun. */
#define XHCI_TCC_ERR_OTHER 33 /**< Implementation specific error. */
#define XHCI_TCC_INV_STRM_ID 34 /**< Invalid stream ID. */
#define XHCI_TCC_SEC_BWIDTH_ERR 35 /**< Secondary bandwidth error. */
#define XHCI_TCC_SPLIT_ERR 36 /**< Split transaction error. */
/** @} */
#if defined(IN_RING3) && defined(LOG_ENABLED)
/** Human-readable completion code descriptions for debugging. */
static const char * const g_apszCmplCodes[] = {
"CC field not updated", "Successful TRB completion", "Overrun/underrun", "Babble detected", /* 0-3 */
"USB transaction error", "TRB error detected", "USB Stall detected", "Inadequate xHC resources", /* 4-7 */
"Unable to allocate bandwidth", "MaxSlots (NDS) exceeded", "Invalid stream context type", "Slot not enabled", /* 8-11 */
"Endpoint not enabled", "Short packet detected", "Transfer ring underrun", "Transfer ring overrun", /* 12-15 */
"VF event ring full", "Invalid context param", "Isoc bandwidth overrun", "Transition from illegal ctx state", /* 16-19 */
"No ping response in time", "Event Ring full", "Incompatible device detected", "Missed isoc service", /* 20-23 */
"Command ring stopped", "Command aborted", "Endpoint stopped", "EP stopped, invalid transfer length", /* 24-27 */
"Reserved", "Max exit latency too large", "Reserved", "Isochronous buffer overrun", /* 28-31 */
"Event lost due to overrun", "Implementation specific error", "Invalid stream ID", "Secondary bandwidth error", /* 32-35 */
"Split transaction error" /* 36 */
};
#endif
/* TRBs marked as 'TRB' are only valid in the transfer ring. TRBs marked
* as 'Command' are only valid in the command ring. TRBs marked as 'Event'
* are the only ones generated in the event ring. The Link TRB is valid
* in both the transfer and command rings.
*/
/** @name TRB Types
* @{ */
#define XHCI_TRB_INVALID 0 /**< Reserved/unused TRB type. */
#define XHCI_TRB_NORMAL 1 /**< Normal TRB. */
#define XHCI_TRB_SETUP_STG 2 /**< Setup Stage TRB. */
#define XHCI_TRB_DATA_STG 3 /**< Data Stage TRB. */
#define XHCI_TRB_STATUS_STG 4 /**< Status Stage TRB. */
#define XHCI_TRB_ISOCH 5 /**< Isochronous TRB. */
#define XHCI_TRB_LINK 6 /**< Link. */
#define XHCI_TRB_EVT_DATA 7 /**< Event Data TRB. */
#define XHCI_TRB_NOOP_XFER 8 /**< No-op transfer TRB. */
#define XHCI_TRB_ENB_SLOT 9 /**< Enable Slot Command. */
#define XHCI_TRB_DIS_SLOT 10 /**< Disable Slot Command. */
#define XHCI_TRB_ADDR_DEV 11 /**< Address Device Command. */
#define XHCI_TRB_CFG_EP 12 /**< Configure Endpoint Command. */
#define XHCI_TRB_EVAL_CTX 13 /**< Evaluate Context Command. */
#define XHCI_TRB_RESET_EP 14 /**< Reset Endpoint Command. */
#define XHCI_TRB_STOP_EP 15 /**< Stop Endpoint Command. */
#define XHCI_TRB_SET_DEQ_PTR 16 /**< Set TR Dequeue Pointer Command. */
#define XHCI_TRB_RESET_DEV 17 /**< Reset Device Command. */
#define XHCI_TRB_FORCE_EVT 18 /**< Force Event Command. */
#define XHCI_TRB_NEG_BWIDTH 19 /**< Negotiate Bandwidth Command. */
#define XHCI_TRB_SET_LTV 20 /**< Set Latency Tolerate Value Command. */
#define XHCI_TRB_GET_PORT_BW 21 /**< Get Port Bandwidth Command. */
#define XHCI_TRB_FORCE_HDR 22 /**< Force Header Command. */
#define XHCI_TRB_NOOP_CMD 23 /**< No-op Command. */
/* 24-31 Reserved. */
#define XHCI_TRB_XFER 32 /**< Transfer Event. */
#define XHCI_TRB_CMD_CMPL 33 /**< Command Completion Event. */
#define XHCI_TRB_PORT_SC 34 /**< Port Status Change Event. */
#define XHCI_TRB_BW_REQ 35 /**< Bandwidth Request Event. */
#define XHCI_TRB_DBELL 36 /**< Doorbell Event. */
#define XHCI_TRB_HC_EVT 37 /**< Host Controller Event. */
#define XHCI_TRB_DEV_NOTIFY 38 /**< Device Notification Event. */
#define XHCI_TRB_MFIDX_WRAP 39 /**< MFINDEX Wrap Event. */
/* 40-47 Reserved. */
#define NEC_TRB_CMD_CMPL 48 /**< Command Completion Event, NEC specific. */
#define NEC_TRB_GET_FW_VER 49 /**< Get Firmware Version Command, NEC specific. */
#define NEC_TRB_AUTHENTICATE 50 /**< Authenticate Command, NEC specific. */
/** @} */
#if defined(IN_RING3) && defined(LOG_ENABLED)
/** Human-readable TRB names for debugging. */
static const char * const g_apszTrbNames[] = {
"Reserved/unused TRB!!", "Normal TRB", "Setup Stage TRB", "Data Stage TRB", /* 0-3 */
"Status Stage TRB", "Isochronous TRB", "Link", "Event Data TRB", /* 4-7 */
"No-op transfer TRB", "Enable Slot", "Disable Slot", "Address Device", /* 8-11 */
"Configure Endpoint", "Evaluate Context", "Reset Endpoint", "Stop Endpoint", /* 12-15 */
"Set TR Dequeue Pointer", "Reset Device", "Force Event", "Negotiate Bandwidth", /* 16-19 */
"Set Latency Tolerate Value", "Get Port Bandwidth", "Force Header", "No-op", /* 20-23 */
"UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", /* 24-31 */
"Transfer", "Command Completion", "Port Status Change", "BW Request", /* 32-35 */
"Doorbell", "Host Controller", "Device Notification", "MFINDEX Wrap", /* 36-39 */
"UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", "UNDEF", /* 40-47 */
"NEC FW Version Completion", "NEC Get FW Version", "NEC Authenticate" /* 48-50 */
};
#endif
/** Generic TRB template. */
typedef struct sXHCI_TRB_G {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_G;
AssertCompile(sizeof(XHCI_TRB_G) == 0x10);
/** Generic transfer TRB template. */
typedef struct sXHCI_TRB_GX {
uint32_t resvd0;
uint32_t resvd1;
uint32_t xfr_len : 17; /**< Transfer length. */
uint32_t resvd2 : 5;
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t ent : 1; /**< Evaluate Next TRB. */
uint32_t isp : 1; /**< Interrupt on Short Packet. */
uint32_t ns : 1; /**< No Snoop. */
uint32_t ch : 1; /**< Chain bit. */
uint32_t ioc : 1; /**< Interrupt On Completion. */
uint32_t idt : 1; /**< Immediate Data. */
uint32_t resvd3 : 3;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_GX;
AssertCompile(sizeof(XHCI_TRB_GX) == 0x10);
/* -= Transfer TRB types =- */
/** Normal Transfer TRB. */
typedef struct sXHCI_TRB_NORM {
uint64_t data_ptr; /**< Pointer or data. */
uint32_t xfr_len : 17; /**< Transfer length. */
uint32_t td_size : 5; /**< Remaining packets. */
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t ent : 1; /**< Evaluate Next TRB. */
uint32_t isp : 1; /**< Interrupt on Short Packet. */
uint32_t ns : 1; /**< No Snoop. */
uint32_t ch : 1; /**< Chain bit. */
uint32_t ioc : 1; /**< Interrupt On Completion. */
uint32_t idt : 1; /**< Immediate Data. */
uint32_t resvd0 : 2;
uint32_t bei : 1; /**< Block Event Interrupt. */
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd1 : 16;
} XHCI_TRB_NORM;
AssertCompile(sizeof(XHCI_TRB_NORM) == 0x10);
/** Control Transfer - Setup Stage TRB. */
typedef struct sXHCI_TRB_CTSP {
uint8_t bmRequestType; /**< See the USB spec. */
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
uint32_t xfr_len : 17; /**< Transfer length (8). */
uint32_t resvd0 : 5;
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 4;
uint32_t ioc : 1; /**< Interrupt On Completion. */
uint32_t idt : 1; /**< Immediate Data. */
uint32_t resvd2 : 2;
uint32_t bei : 1; /**< Block Event Interrupt. */
uint32_t type : 6; /**< TRB Type. */
uint32_t trt : 2; /**< Transfer Type. */
uint32_t resvd3 : 14;
} XHCI_TRB_CTSP;
AssertCompile(sizeof(XHCI_TRB_CTSP) == 0x10);
/** Control Transfer - Data Stage TRB. */
typedef struct sXHCI_TRB_CTDT {
uint64_t data_ptr; /**< Pointer or data. */
uint32_t xfr_len : 17; /**< Transfer length. */
uint32_t td_size : 5; /**< Remaining packets. */
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t ent : 1; /**< Evaluate Next TRB. */
uint32_t isp : 1; /**< Interrupt on Short Packet. */
uint32_t ns : 1; /**< No Snoop. */
uint32_t ch : 1; /**< Chain bit. */
uint32_t ioc : 1; /**< Interrupt On Completion. */
uint32_t idt : 1; /**< Immediate Data. */
uint32_t resvd0 : 3;
uint32_t type : 6; /**< TRB Type. */
uint32_t dir : 1; /**< Direction (1=IN). */
uint32_t resvd1 : 15;
} XHCI_TRB_CTDT;
AssertCompile(sizeof(XHCI_TRB_CTDT) == 0x10);
/** Control Transfer - Status Stage TRB. */
typedef struct sXHCI_TRB_CTSS {
uint64_t resvd0;
uint32_t resvd1 : 22;
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t ent : 1; /**< Evaluate Next TRB. */
uint32_t resvd2 : 2;
uint32_t ch : 1; /**< Chain bit. */
uint32_t ioc : 1; /**< Interrupt On Completion. */
uint32_t resvd3 : 4;
uint32_t type : 6; /**< TRB Type. */
uint32_t dir : 1; /**< Direction (1=IN). */
uint32_t resvd4 : 15;
} XHCI_TRB_CTSS;
AssertCompile(sizeof(XHCI_TRB_CTSS) == 0x10);
/** Isochronous Transfer TRB. */
typedef struct sXHCI_TRB_ISOC {
uint64_t data_ptr; /**< Pointer or data. */
uint32_t xfr_len : 17; /**< Transfer length. */
uint32_t td_size : 5; /**< Remaining packets. */
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t ent : 1; /**< Evaluate Next TRB. */
uint32_t isp : 1; /**< Interrupt on Short Packet. */
uint32_t ns : 1; /**< No Snoop. */
uint32_t ch : 1; /**< Chain bit. */
uint32_t ioc : 1; /**< Interrupt On Completion. */
uint32_t idt : 1; /**< Immediate Data. */
uint32_t tbc : 2; /**< Transfer Burst Count. */
uint32_t bei : 1; /**< Block Event Interrupt. */
uint32_t type : 6; /**< TRB Type. */
uint32_t tlbpc : 4; /**< Transfer Last Burst Packet Count. */
uint32_t frm_id : 11; /**< Frame ID. */
uint32_t sia : 1; /**< Start Isoch ASAP. */
} XHCI_TRB_ISOC;
AssertCompile(sizeof(XHCI_TRB_ISOC) == 0x10);
/* Number of bits in the frame ID. */
#define XHCI_FRAME_ID_BITS 11
/** No Op Transfer TRB. */
typedef struct sXHCI_TRB_NOPT {
uint64_t resvd0;
uint32_t resvd1 : 22;
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t ent : 1; /**< Evaluate Next TRB. */
uint32_t resvd2 : 2;
uint32_t ch : 1; /**< Chain bit. */
uint32_t ioc : 1; /**< Interrupt On Completion. */
uint32_t resvd3 : 4;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_NOPT;
AssertCompile(sizeof(XHCI_TRB_NOPT) == 0x10);
/* -= Event TRB types =- */
/** Transfer Event TRB. */
typedef struct sXHCI_TRB_TE {
uint64_t trb_ptr; /**< TRB pointer. */
uint32_t xfr_len : 24; /**< Transfer length. */
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd0 : 1;
uint32_t ed : 1; /**< Event Data flag. */
uint32_t resvd1 : 7;
uint32_t type : 6; /**< TRB Type. */
uint32_t ep_id : 5; /**< Endpoint ID. */
uint32_t resvd2 : 3;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_TE;
AssertCompile(sizeof(XHCI_TRB_TE) == 0x10);
/** Command Completion Event TRB. */
typedef struct sXHCI_TRB_CCE {
uint64_t trb_ptr; /**< Command TRB pointer. */
uint32_t resvd0 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t vf_id : 8; /**< Virtual Function ID. */
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_CCE;
AssertCompile(sizeof(XHCI_TRB_CCE) == 0x10);
/** Port Staus Change Event TRB. */
typedef struct sXHCI_TRB_PSCE {
uint32_t resvd0 : 24;
uint32_t port_id : 8; /**< Port ID. */
uint32_t resvd1;
uint32_t resvd2 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_PSCE;
AssertCompile(sizeof(XHCI_TRB_PSCE) == 0x10);
/** Bandwidth Request Event TRB. */
typedef struct sXHCI_TRB_BRE {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_BRE;
AssertCompile(sizeof(XHCI_TRB_BRE) == 0x10);
/** Doorbell Event TRB. */
typedef struct sXHCI_TRB_DBE {
uint32_t reason : 5; /**< DB Reason/target. */
uint32_t resvd0 : 27;
uint32_t resvd1;
uint32_t resvd2 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t vf_id : 8; /**< Virtual Function ID. */
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_DBE;
AssertCompile(sizeof(XHCI_TRB_DBE) == 0x10);
/** Host Controller Event TRB. */
typedef struct sXHCI_TRB_HCE {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_HCE;
AssertCompile(sizeof(XHCI_TRB_HCE) == 0x10);
/** Device Notification Event TRB. */
typedef struct sXHCI_TRB_DNE {
uint32_t resvd0 : 4;
uint32_t dn_type : 4; /**< Device Notification Type. */
uint32_t dnd_lo : 5; /**< Device Notification Data Lo. */
uint32_t dnd_hi; /**< Device Notification Data Hi. */
uint32_t resvd1 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd2 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd3 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_DNE;
AssertCompile(sizeof(XHCI_TRB_DNE) == 0x10);
/** MFINDEX Wrap Event TRB. */
typedef struct sXHCI_TRB_MWE {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2 : 24;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_MWE;
AssertCompile(sizeof(XHCI_TRB_MWE) == 0x10);
/** NEC Specific Command Completion Event TRB. */
typedef struct sXHCI_TRB_NCE {
uint64_t trb_ptr; /**< Command TRB pointer. */
uint32_t word1 : 16; /**< First result word. */
uint32_t resvd0 : 8;
uint32_t cc : 8; /**< Completion Code. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t word2 : 16; /**< Second result word. */
} XHCI_TRB_NCE;
AssertCompile(sizeof(XHCI_TRB_NCE) == 0x10);
/* -= Command TRB types =- */
/** No Op Command TRB. */
typedef struct sXHCI_TRB_NOPC {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_NOPC;
AssertCompile(sizeof(XHCI_TRB_NOPC) == 0x10);
/** Enable Slot Command TRB. */
typedef struct sXHCI_TRB_ESL {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 16;
} XHCI_TRB_ESL;
AssertCompile(sizeof(XHCI_TRB_ESL) == 0x10);
/** Disable Slot Command TRB. */
typedef struct sXHCI_TRB_DSL {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_DSL;
AssertCompile(sizeof(XHCI_TRB_DSL) == 0x10);
/** Address Device Command TRB. */
typedef struct sXHCI_TRB_ADR {
uint64_t ctx_ptr; /**< Input Context pointer. */
uint32_t resvd0;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 8;
uint32_t bsr : 1; /**< Block Set Address Request. */
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd2 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_ADR;
AssertCompile(sizeof(XHCI_TRB_ADR) == 0x10);
/** Configure Endpoint Command TRB. */
typedef struct sXHCI_TRB_CFG {
uint64_t ctx_ptr; /**< Input Context pointer. */
uint32_t resvd0;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 8;
uint32_t dc : 1; /**< Deconfigure. */
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd2 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_CFG;
AssertCompile(sizeof(XHCI_TRB_CFG) == 0x10);
/** Evaluate Context Command TRB. */
typedef struct sXHCI_TRB_EVC {
uint64_t ctx_ptr; /**< Input Context pointer. */
uint32_t resvd0;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd2 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_EVC;
AssertCompile(sizeof(XHCI_TRB_EVC) == 0x10);
/** Reset Endpoint Command TRB. */
typedef struct sXHCI_TRB_RSE {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 8;
uint32_t tsp : 1; /**< Transfer State Preserve. */
uint32_t type : 6; /**< TRB Type. */
uint32_t ep_id : 5; /**< Endpoint ID. */
uint32_t resvd4 : 3;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_RSE;
AssertCompile(sizeof(XHCI_TRB_RSE) == 0x10);
/** Stop Endpoint Command TRB. */
typedef struct sXHCI_TRB_STP {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t ep_id : 5; /**< Endpoint ID. */
uint32_t resvd4 : 2;
uint32_t sp : 1; /**< Suspend. */
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_STP;
AssertCompile(sizeof(XHCI_TRB_STP) == 0x10);
/** Set TR Dequeue Pointer Command TRB. */
typedef struct sXHCI_TRB_STDP {
#if 0
uint64_t dcs : 1; /**< Dequeue Cycle State. */
uint64_t sct : 3; /**< Stream Context Type. */
uint64_t tr_dqp : 60; /**< New TR Dequeue Pointer (63:4). */
#else
uint64_t tr_dqp;
#endif
uint16_t resvd0;
uint16_t strm_id; /**< Stream ID. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t ep_id : 5; /**< Endpoint ID. */
uint32_t resvd2 : 3;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_STDP;
AssertCompile(sizeof(XHCI_TRB_STDP) == 0x10);
/** Reset Device Command TRB. */
typedef struct sXHCI_TRB_RSD {
uint32_t resvd0;
uint32_t resvd1;
uint32_t resvd2;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd3 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd4 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_RSD;
AssertCompile(sizeof(XHCI_TRB_RSD) == 0x10);
/** Get Port Bandwidth Command TRB. */
typedef struct sXHCI_TRB_GPBW {
uint64_t pbctx_ptr; /**< Port Bandwidth Context pointer. */
uint32_t resvd0;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t spd : 4; /**< Dev Speed. */
uint32_t resvd2 : 4;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_GPBW;
AssertCompile(sizeof(XHCI_TRB_GPBW) == 0x10);
/** Force Header Command TRB. */
typedef struct sXHCI_TRB_FHD {
uint32_t pkt_typ : 5; /**< Packet Type. */
uint32_t hdr_lo : 27; /**< Header Info Lo. */
uint32_t hdr_mid; /**< Header Info Mid. */
uint32_t hdr_hi; /**< Header Info Hi. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd0 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd1 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_FHD;
AssertCompile(sizeof(XHCI_TRB_FHD) == 0x10);
/** NEC Specific Authenticate Command TRB. */
typedef struct sXHCI_TRB_NAC {
uint64_t cookie; /**< Cookie to munge. */
uint32_t resvd0;
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t resvd1 : 9;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd2 : 8;
uint32_t slot_id : 8; /**< Slot ID. */
} XHCI_TRB_NAC;
AssertCompile(sizeof(XHCI_TRB_NAC) == 0x10);
/* -= Other TRB types =- */
/** Link TRB. */
typedef struct sXHCI_TRB_LNK {
uint64_t rseg_ptr; /**< Ring Segment Pointer. */
uint32_t resvd0 : 22;
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t toggle : 1; /**< Toggle Cycle flag. */
uint32_t resvd1 : 2;
uint32_t chain : 1; /**< Chain flag. */
uint32_t ioc : 1; /**< Interrupt On Completion flag. */
uint32_t resvd2 : 4;
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd3 : 16;
} XHCI_TRB_LNK;
AssertCompile(sizeof(XHCI_TRB_LNK) == 0x10);
/** Event Data TRB. */
typedef struct sXHCI_TRB_EVTD {
uint64_t evt_data; /**< Event Data. */
uint32_t resvd0 : 22;
uint32_t int_tgt : 10; /**< Interrupter target. */
uint32_t cycle : 1; /**< Cycle bit. */
uint32_t ent : 1; /**< Evaluate Next Target flag. */
uint32_t resvd1 : 2;
uint32_t chain : 1; /**< Chain flag. */
uint32_t ioc : 1; /**< Interrupt On Completion flag. */
uint32_t resvd2 : 3;
uint32_t bei : 1; /**< Block Event Interrupt flag. */
uint32_t type : 6; /**< TRB Type. */
uint32_t resvd3 : 16;
} XHCI_TRB_EVTD;
AssertCompile(sizeof(XHCI_TRB_EVTD) == 0x10);
/* -= Union TRB types for the three rings =- */
typedef union sXHCI_XFER_TRB {
XHCI_TRB_NORM norm;
XHCI_TRB_CTSP setup;
XHCI_TRB_CTDT data;
XHCI_TRB_CTSS status;
XHCI_TRB_ISOC isoc;
XHCI_TRB_EVTD evtd;
XHCI_TRB_NOPT nop;
XHCI_TRB_LNK link;
XHCI_TRB_GX gen;
} XHCI_XFER_TRB;
AssertCompile(sizeof(XHCI_XFER_TRB) == 0x10);
typedef union sXHCI_COMMAND_TRB {
XHCI_TRB_ESL esl;
XHCI_TRB_DSL dsl;
XHCI_TRB_ADR adr;
XHCI_TRB_CFG cfg;
XHCI_TRB_EVC evc;
XHCI_TRB_RSE rse;
XHCI_TRB_STP stp;
XHCI_TRB_STDP stdp;
XHCI_TRB_RSD rsd;
XHCI_TRB_GPBW gpbw;
XHCI_TRB_FHD fhd;
XHCI_TRB_NAC nac;
XHCI_TRB_NOPC nopc;
XHCI_TRB_LNK link;
XHCI_TRB_G gen;
} XHCI_COMMAND_TRB;
AssertCompile(sizeof(XHCI_COMMAND_TRB) == 0x10);
typedef union sXHCI_EVENT_TRB {
XHCI_TRB_TE te;
XHCI_TRB_CCE cce;
XHCI_TRB_PSCE psce;
XHCI_TRB_BRE bre;
XHCI_TRB_DBE dbe;
XHCI_TRB_HCE hce;
XHCI_TRB_DNE dne;
XHCI_TRB_MWE mwe;
XHCI_TRB_NCE nce;
XHCI_TRB_G gen;
} XHCI_EVENT_TRB;
AssertCompile(sizeof(XHCI_EVENT_TRB) == 0x10);
/* -=-=-= Contexts =-=-=- */
/** Slot Context. */
typedef struct sXHCI_SLOT_CTX {
uint32_t route_str : 20; /**< Route String. */
uint32_t speed : 4; /**< Device speed. */
uint32_t resvd0 : 1;
uint32_t mtt : 1; /**< Multi-TT flag. */
uint32_t hub : 1; /**< Hub flag. */
uint32_t ctx_ent : 5; /**< Context entries. */
uint32_t max_lat : 16; /**< Max exit latency in usec. */
uint32_t rh_port : 8; /**< Root hub port number (1-based). */
uint32_t n_ports : 8; /**< No. of ports for hubs. */
uint32_t tt_slot : 8; /**< TT hub slot ID. */
uint32_t tt_port : 8; /**< TT port number. */
uint32_t ttt : 2; /**< TT Think Time. */
uint32_t resvd1 : 4;
uint32_t intr_tgt : 10; /**< Interrupter Target. */
uint32_t dev_addr : 8; /**< Device Address. */
uint32_t resvd2 : 19;
uint32_t slot_state : 5; /**< Slot State. */
uint32_t opaque[4]; /**< For xHC (i.e. our own) use. */
} XHCI_SLOT_CTX;
AssertCompile(sizeof(XHCI_SLOT_CTX) == 0x20);
/** @name Slot Context states
* @{ */
#define XHCI_SLTST_ENDIS 0 /**< Enabled/Disabled. */
#define XHCI_SLTST_DEFAULT 1 /**< Default. */
#define XHCI_SLTST_ADDRESSED 2 /**< Addressed. */
#define XHCI_SLTST_CONFIGURED 3 /**< Configured. */
/** @} */
#ifdef IN_RING3
/** Human-readable slot state descriptions for debugging. */
static const char * const g_apszSltStates[] = {
"Enabled/Disabled", "Default", "Addressed", "Configured" /* 0-3 */
};
#endif
/** Endpoint Context. */
typedef struct sXHCI_EP_CTX {
uint32_t ep_state : 3; /**< Endpoint state. */
uint32_t resvd0 : 5;
uint32_t mult : 2; /**< SS isoc burst count. */
uint32_t maxps : 5; /**< Max Primary Streams. */
uint32_t lsa : 1; /**< Linear Stream Array. */
uint32_t interval : 8; /**< USB request interval. */
uint32_t resvd1 : 8;
uint32_t resvd2 : 1;
uint32_t c_err : 2; /**< Error count. */
uint32_t ep_type : 3; /**< Endpoint type. */
uint32_t resvd3 : 1;
uint32_t hid : 1; /**< Host Initiate Disable. */
uint32_t max_brs_sz : 8; /**< Max Burst Size. */
uint32_t max_pkt_sz : 16; /**< Max Packet Size. */
uint64_t trdp; /**< TR Dequeue Pointer. */
uint32_t avg_trb_len : 16; /**< Average TRB Length. */
uint32_t max_esit : 16; /**< Max EP Service Interval Time Payload. */
/**< The rest for xHC (i.e. our own) use. */
uint32_t last_frm : 16; /**< Last isochronous frame used (opaque). */
uint32_t ifc : 8; /**< isoch in-flight TD count (opaque). */
uint32_t last_cc : 8; /**< Last TRB completion code (opaque). */
uint64_t trep; /**< TR Enqueue Pointer (opaque). */
} XHCI_EP_CTX;
AssertCompile(sizeof(XHCI_EP_CTX) == 0x20);
/** @name Endpoint Context states
* @{ */
#define XHCI_EPST_DISABLED 0 /**< Disabled. */
#define XHCI_EPST_RUNNING 1 /**< Running. */
#define XHCI_EPST_HALTED 2 /**< Halted. */
#define XHCI_EPST_STOPPED 3 /**< Not running/stopped. */
#define XHCI_EPST_ERROR 4 /**< Not running/error. */
/** @} */
/** @name Endpoint Type values
* @{ */
#define XHCI_EPTYPE_INVALID 0 /**< Not valid. */
#define XHCI_EPTYPE_ISOCH_OUT 1 /**< Isochronous Out. */
#define XHCI_EPTYPE_BULK_OUT 2 /**< Bulk Out. */
#define XHCI_EPTYPE_INTR_OUT 3 /**< Interrupt Out. */
#define XHCI_EPTYPE_CONTROL 4 /**< Control Bidi. */
#define XHCI_EPTYPE_ISOCH_IN 5 /**< Isochronous In. */
#define XHCI_EPTYPE_BULK_IN 6 /**< Bulk In. */
#define XHCI_EPTYPE_INTR_IN 7 /**< Interrupt In. */
/** @} */
/* Pick out transfer type from endpoint. */
#define XHCI_EP_XTYPE(a) (a & 3)
/* Endpoint transfer types. */
#define XHCI_XFTYPE_CONTROL 0
#define XHCI_XFTYPE_ISOCH XHCI_EPTYPE_ISOCH_OUT
#define XHCI_XFTYPE_BULK XHCI_EPTYPE_BULK_OUT
#define XHCI_XFTYPE_INTR XHCI_EPTYPE_INTR_OUT
/* Transfer Ring Dequeue Pointer address mask. */
#define XHCI_TRDP_ADDR_MASK UINT64_C(0xFFFFFFFFFFFFFFF0)
#define XHCI_TRDP_DCS_MASK RT_BIT(0) /* Dequeue Cycle State bit. */
#ifdef IN_RING3
/* Human-readable endpoint state descriptions for debugging. */
static const char * const g_apszEpStates[] = {
"Disabled", "Running", "Halted", "Stopped", "Error" /* 0-4 */
};
/* Human-readable endpoint type descriptions for debugging. */
static const char * const g_apszEpTypes[] = {
"Not Valid", "Isoch Out", "Bulk Out", "Interrupt Out", /* 0-3 */
"Control", "Isoch In", "Bulk In", "Interrupt In" /* 4-7 */
};
#endif /* IN_RING3 */
/* Input Control Context. */
typedef struct sXHCI_INPC_CTX {
uint32_t drop_flags; /* Drop Context flags (2-31). */
uint32_t add_flags; /* Add Context flags (0-31). */
uint32_t resvd[6];
} XHCI_INPC_CTX;
AssertCompile(sizeof(XHCI_INPC_CTX) == 0x20);
/* Make sure all contexts are the same size. */
AssertCompile(sizeof(XHCI_EP_CTX) == sizeof(XHCI_SLOT_CTX));
AssertCompile(sizeof(XHCI_EP_CTX) == sizeof(XHCI_INPC_CTX));
/* -= Event Ring Segment Table =- */
/** Event Ring Segment Table Entry. */
typedef struct sXHCI_ERSTE {
uint64_t addr;
uint16_t size;
uint16_t resvd0;
uint32_t resvd1;
} XHCI_ERSTE;
AssertCompile(sizeof(XHCI_ERSTE) == 0x10);
/* -=-= Internal data structures not defined by xHCI =-=- */
/** Device slot entry -- either slot context or endpoint context. */
typedef union sXHCI_DS_ENTRY {
XHCI_SLOT_CTX sc; /**< Slot context. */
XHCI_EP_CTX ep; /**< Endpoint context. */
} XHCI_DS_ENTRY;
/** Full device context (slot context + 31 endpoint contexts). */
typedef struct sXHCI_DEV_CTX {
XHCI_DS_ENTRY entry[32];
} XHCI_DEV_CTX;
AssertCompile(sizeof(XHCI_DEV_CTX) == 32 * sizeof(XHCI_EP_CTX));
AssertCompile(sizeof(XHCI_DEV_CTX) == 32 * sizeof(XHCI_SLOT_CTX));
/** Pointer to the xHCI device state. */
typedef struct XHCI *PXHCI;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/**
* The xHCI controller data associated with each URB.
*/
typedef struct VUSBURBHCIINT
{
/** The slot index. */
uint8_t uSlotID;
/** Number of Tds in the array. */
uint32_t cTRB;
} VUSBURBHCIINT;
#endif
/**
* An xHCI root hub port, shared.
*/
typedef struct XHCIHUBPORT
{
/** PORTSC: Port status/control register (R/W). */
uint32_t portsc;
/** PORTPM: Power management status/control register (R/W). */
uint32_t portpm;
/** PORTLI: USB3 port link information (R/O). */
uint32_t portli;
} XHCIHUBPORT;
/** Pointer to a shared xHCI root hub port. */
typedef XHCIHUBPORT *PXHCIHUBPORT;
/**
* An xHCI root hub port, ring-3.
*/
typedef struct XHCIHUBPORTR3
{
/** Flag whether there is a device attached to the port. */
bool fAttached;
} XHCIHUBPORTR3;
/** Pointer to a ring-3 xHCI root hub port. */
typedef XHCIHUBPORTR3 *PXHCIHUBPORTR3;
/**
* The xHCI root hub, ring-3 only.
*
* @implements PDMIBASE
* @implements VUSBIROOTHUBPORT
*/
typedef struct XHCIROOTHUBR3
{
/** Pointer to the parent xHC. */
R3PTRTYPE(struct XHCIR3 *) pXhciR3;
/** Pointer to the base interface of the VUSB RootHub. */
R3PTRTYPE(PPDMIBASE) pIBase;
/** Pointer to the connector interface of the VUSB RootHub. */
R3PTRTYPE(PVUSBIROOTHUBCONNECTOR) pIRhConn;
/** The base interface exposed to the roothub driver. */
PDMIBASE IBase;
/** The roothub port interface exposed to the roothub driver. */
VUSBIROOTHUBPORT IRhPort;
/** The LED for this hub. */
PDMLED Led;
/** Number of actually implemented ports. */
uint8_t cPortsImpl;
/** Index of first port for this hub. */
uint8_t uPortBase;
uint16_t Alignment0; /**< Force alignment. */
#if HC_ARCH_BITS == 64
uint32_t Alignment1;
#endif
} XHCIROOTHUBR3;
/** Pointer to a xHCI root hub (ring-3 only). */
typedef XHCIROOTHUBR3 *PXHCIROOTHUBR3;
/**
* An xHCI interrupter.
*/
typedef struct sXHCIINTRPTR
{
/* Registers defined by xHCI. */
/** IMAN: Interrupt Management Register (R/W). */
uint32_t iman;
/** IMOD: Interrupt Moderation Register (R/W). */
uint32_t imod;
/** ERSTSZ: Event Ring Segment Table Size (R/W). */
uint32_t erstsz;
/* Reserved/padding. */
uint32_t reserved;
/** ERSTBA: Event Ring Segment Table Base Address (R/W). */
uint64_t erstba;
/** ERDP: Event Ring Dequeue Pointer (R/W). */
uint64_t erdp;
/* Interrupter lock. */
PDMCRITSECT lock;
/* Internal xHCI non-register state. */
/** Internal Event Ring enqueue pointer. */
uint64_t erep;
/** Internal ERDP re-write counter. */
uint32_t erdp_rewrites;
/** This interrupter's index (for logging). */
uint32_t index;
/** Internal index into Event Ring Segment Table. */
uint16_t erst_idx;
/** Internal index into Event Ring Segment. */
uint16_t trb_count;
/** Internal Event Ring Producer Cycle State. */
bool evtr_pcs;
/** Internal Interrupt Pending Enable flag. */
bool ipe;
} XHCIINTRPTR, *PXHCIINTRPTR;
/**
* xHCI device state.
* @implements PDMILEDPORTS
*/
typedef struct XHCI
{
/** MFINDEX wraparound timer. */
TMTIMERHANDLE hWrapTimer;
#ifdef XHCI_ERROR_INJECTION
bool fDropIntrHw;
bool fDropIntrIpe;
bool fDropUrb;
uint8_t Alignment00[1];
#else
uint32_t Alignment00; /**< Force alignment. */
#endif
/** Flag indicating a sleeping worker thread. */
volatile bool fWrkThreadSleeping;
volatile bool afPadding[3];
/** The event semaphore the worker thread waits on. */
SUPSEMEVENT hEvtProcess;
/** Bitmap for finished tasks (R3 -> Guest). */
volatile uint32_t u32TasksFinished;
/** Bitmap for finished queued tasks (R3 -> Guest). */
volatile uint32_t u32QueuedTasksFinished;
/** Bitmap for new queued tasks (Guest -> R3). */
volatile uint32_t u32TasksNew;
/** Copy of XHCIR3::RootHub2::cPortsImpl. */
uint8_t cUsb2Ports;
/** Copy of XHCIR3::RootHub3::cPortsImpl. */
uint8_t cUsb3Ports;
/** Sum of cUsb2Ports and cUsb3Ports. */
uint8_t cTotalPorts;
/** Explicit padding. */
uint8_t bPadding;
/** Start of current frame. */
uint64_t SofTime;
/** State of the individual ports. */
XHCIHUBPORT aPorts[XHCI_NDP_MAX];
/** Interrupters array. */
XHCIINTRPTR aInterrupters[XHCI_NINTR];
/** @name Host Controller Capability Registers
* @{ */
/** CAPLENGTH: base + CAPLENGTH = operational register start (R/O). */
uint32_t cap_length;
/** HCIVERSION: host controller interface version (R/O). */
uint32_t hci_version;
/** HCSPARAMS: Structural parameters 1 (R/O). */
uint32_t hcs_params1;
/** HCSPARAMS: Structural parameters 2 (R/O). */
uint32_t hcs_params2;
/** HCSPARAMS: Structural parameters 3 (R/O). */
uint32_t hcs_params3;
/** HCCPARAMS: Capability parameters (R/O). */
uint32_t hcc_params;
/** DBOFF: Doorbell offset (R/O). */
uint32_t dbell_off;
/** RTSOFF: Run-time register space offset (R/O). */
uint32_t rts_off;
/** @} */
/** @name Host Controller Operational Registers
* @{ */
/** USB command register - USBCMD (R/W). */
uint32_t cmd;
/** USB status register - USBSTS (R/W).*/
uint32_t status;
/** Device Control Notification register - DNCTRL (R/W). */
uint32_t dnctrl;
/** Configure Register (R/W). */
uint32_t config;
/** Command Ring Control Register - CRCR (R/W). */
uint64_t crcr;
/** Device Context Base Address Array Pointer (R/W). */
uint64_t dcbaap;
/** @} */
/** Extended Capabilities storage. */
uint8_t abExtCap[XHCI_EXT_CAP_SIZE];
/** Size of valid extended capabilities. */
uint32_t cbExtCap;
uint32_t Alignment1; /**< Align cmdr_dqp. */
/** @name Internal xHCI non-register state
* @{ */
/** Internal Command Ring dequeue pointer. */
uint64_t cmdr_dqp;
/** Internal Command Ring Consumer Cycle State. */
bool cmdr_ccs;
uint8_t aAlignment2[7]; /**< Force alignment. */
/** Internal Device Slot states. */
uint8_t aSlotState[XHCI_NDS];
/** Internal doorbell states. Each bit corresponds to an endpoint. */
uint32_t aBellsRung[XHCI_NDS];
/** @} */
/** @name Model specific configuration
* @{ */
/** ERST address mask. */
uint64_t erst_addr_mask;
/** @} */
/** The MMIO region. */
IOMMMIOHANDLE hMmio;
/** Detected isochronous URBs completed with error. */
STAMCOUNTER StatErrorIsocUrbs;
/** Detected isochronous packets (not URBs!) with error. */
STAMCOUNTER StatErrorIsocPkts;
/** Event TRBs written to event ring(s). */
STAMCOUNTER StatEventsWritten;
/** Event TRBs not written to event ring(s) due to HC being stopped. */
STAMCOUNTER StatEventsDropped;
/** Requests to set the IP bit. */
STAMCOUNTER StatIntrsPending;
/** Actual interrupt deliveries. */
STAMCOUNTER StatIntrsSet;
/** Interrupts not raised because they were disabled. */
STAMCOUNTER StatIntrsNotSet;
/** A pending interrupt was cleared. */
STAMCOUNTER StatIntrsCleared;
/** Number of TRBs that formed a single control URB. */
STAMCOUNTER StatTRBsPerCtlUrb;
/** Number of TRBs that formed a single data (bulk/interrupt) URB. */
STAMCOUNTER StatTRBsPerDtaUrb;
/** Number of TRBs that formed a single isochronous URB. */
STAMCOUNTER StatTRBsPerIsoUrb;
/** Size of a control URB in bytes. */
STAMCOUNTER StatUrbSizeCtrl;
/** Size of a data URB in bytes. */
STAMCOUNTER StatUrbSizeData;
/** Size of an isochronous URB in bytes. */
STAMCOUNTER StatUrbSizeIsoc;
#ifdef VBOX_WITH_STATISTICS
/** @name Register access counters.
* @{ */
STAMCOUNTER StatRdCaps;
STAMCOUNTER StatRdCmdRingCtlHi;
STAMCOUNTER StatRdCmdRingCtlLo;
STAMCOUNTER StatRdConfig;
STAMCOUNTER StatRdDevCtxBaapHi;
STAMCOUNTER StatRdDevCtxBaapLo;
STAMCOUNTER StatRdDevNotifyCtrl;
STAMCOUNTER StatRdDoorBell;
STAMCOUNTER StatRdEvtRingDeqPtrHi;
STAMCOUNTER StatRdEvtRingDeqPtrLo;
STAMCOUNTER StatRdEvtRsTblBaseHi;
STAMCOUNTER StatRdEvtRsTblBaseLo;
STAMCOUNTER StatRdEvtRstblSize;
STAMCOUNTER StatRdEvtRsvd;
STAMCOUNTER StatRdIntrMgmt;
STAMCOUNTER StatRdIntrMod;
STAMCOUNTER StatRdMfIndex;
STAMCOUNTER StatRdPageSize;
STAMCOUNTER StatRdPortLinkInfo;
STAMCOUNTER StatRdPortPowerMgmt;
STAMCOUNTER StatRdPortRsvd;
STAMCOUNTER StatRdPortStatusCtrl;
STAMCOUNTER StatRdUsbCmd;
STAMCOUNTER StatRdUsbSts;
STAMCOUNTER StatRdUnknown;
STAMCOUNTER StatWrCmdRingCtlHi;
STAMCOUNTER StatWrCmdRingCtlLo;
STAMCOUNTER StatWrConfig;
STAMCOUNTER StatWrDevCtxBaapHi;
STAMCOUNTER StatWrDevCtxBaapLo;
STAMCOUNTER StatWrDevNotifyCtrl;
STAMCOUNTER StatWrDoorBell0;
STAMCOUNTER StatWrDoorBellN;
STAMCOUNTER StatWrEvtRingDeqPtrHi;
STAMCOUNTER StatWrEvtRingDeqPtrLo;
STAMCOUNTER StatWrEvtRsTblBaseHi;
STAMCOUNTER StatWrEvtRsTblBaseLo;
STAMCOUNTER StatWrEvtRstblSize;
STAMCOUNTER StatWrIntrMgmt;
STAMCOUNTER StatWrIntrMod;
STAMCOUNTER StatWrPortPowerMgmt;
STAMCOUNTER StatWrPortStatusCtrl;
STAMCOUNTER StatWrUsbCmd;
STAMCOUNTER StatWrUsbSts;
STAMCOUNTER StatWrUnknown;
/** @} */
#endif
} XHCI;
/**
* xHCI device state, ring-3 edition.
* @implements PDMILEDPORTS
*/
typedef struct XHCIR3
{
/** The async worker thread. */
R3PTRTYPE(PPDMTHREAD) pWorkerThread;
/** The device instance.
* @note This is only so interface functions can get their bearings. */
PPDMDEVINSR3 pDevIns;
/** Status LUN: The base interface. */
PDMIBASE IBase;
/** Status LUN: Leds interface. */
PDMILEDPORTS ILeds;
/** Status LUN: Partner of ILeds. */
R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
/** USB 2.0 Root hub device. */
XHCIROOTHUBR3 RootHub2;
/** USB 3.0 Root hub device. */
XHCIROOTHUBR3 RootHub3;
/** State of the individual ports. */
XHCIHUBPORTR3 aPorts[XHCI_NDP_MAX];
/** Critsect to synchronize worker and I/O completion threads. */
RTCRITSECT CritSectThrd;
} XHCIR3;
/** Pointer to ring-3 xHCI device state. */
typedef XHCIR3 *PXHCIR3;
/**
* xHCI device data, ring-0 edition.
*/
typedef struct XHCIR0
{
uint32_t uUnused;
} XHCIR0;
/** Pointer to ring-0 xHCI device data. */
typedef struct XHCIR0 *PXHCIR0;
/**
* xHCI device data, raw-mode edition.
*/
typedef struct XHCIRC
{
uint32_t uUnused;
} XHCIRC;
/** Pointer to raw-mode xHCI device data. */
typedef struct XHCIRC *PXHCIRC;
/** @typedef XHCICC
* The xHCI device data for the current context. */
typedef CTX_SUFF(XHCI) XHCICC;
/** @typedef PXHCICC
* Pointer to the xHCI device for the current context. */
typedef CTX_SUFF(PXHCI) PXHCICC;
/* -=-= Local implementation details =-=- */
typedef enum sXHCI_JOB {
XHCI_JOB_PROCESS_CMDRING, /**< Process the command ring. */
XHCI_JOB_DOORBELL, /**< A doorbell (other than DB0) was rung. */
XHCI_JOB_XFER_DONE, /**< Transfer completed, look for more work. */
XHCI_JOB_MAX
} XHCI_JOB;
/* -=-=- Local xHCI definitions -=-=- */
/** @name USB states.
* @{ */
#define XHCI_USB_RESET 0x00
#define XHCI_USB_RESUME 0x40
#define XHCI_USB_OPERATIONAL 0x80
#define XHCI_USB_SUSPEND 0xc0
/** @} */
/* Primary interrupter (for readability). */
#define XHCI_PRIMARY_INTERRUPTER 0
/** @name Device Slot states.
* @{ */
#define XHCI_DEVSLOT_EMPTY 0
#define XHCI_DEVSLOT_ENABLED 1
#define XHCI_DEVSLOT_DEFAULT 2
#define XHCI_DEVSLOT_ADDRESSED 3
#define XHCI_DEVSLOT_CONFIGURED 4
/** @} */
/** Get the pointer to a root hub corresponding to given port index. */
#define GET_PORT_PRH(a_pThisCC, a_uPort) \
((a_uPort) >= (a_pThisCC)->RootHub2.cPortsImpl ? &(a_pThisCC)->RootHub3 : &(a_pThisCC)->RootHub2)
#define GET_VUSB_PORT_FROM_XHCI_PORT(a_pRh, a_iPort) \
(((a_iPort) - (a_pRh)->uPortBase) + 1)
#define GET_XHCI_PORT_FROM_VUSB_PORT(a_pRh, a_uPort) \
((a_pRh)->uPortBase + (a_uPort) - 1)
/** Check if port corresponding to index is USB3, using shared data. */
#define IS_USB3_PORT_IDX_SHR(a_pThis, a_uPort) ((a_uPort) >= (a_pThis)->cUsb2Ports)
/** Check if port corresponding to index is USB3, using ring-3 data. */
#define IS_USB3_PORT_IDX_R3(a_pThisCC, a_uPort) ((a_uPort) >= (a_pThisCC)->RootHub2.cPortsImpl)
/** Query the number of configured USB2 ports. */
#define XHCI_NDP_USB2(a_pThisCC) ((unsigned)(a_pThisCC)->RootHub2.cPortsImpl)
/** Query the number of configured USB3 ports. */
#define XHCI_NDP_USB3(a_pThisCC) ((unsigned)(a_pThisCC)->RootHub3.cPortsImpl)
/** Query the total number of configured ports. */
#define XHCI_NDP_CFG(a_pThis) ((unsigned)RT_MIN((a_pThis)->cTotalPorts, XHCI_NDP_MAX))
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
#ifdef IN_RING3
/** Build a Protocol extended capability. */
static uint32_t xhciR3BuildProtocolCaps(uint8_t *pbCap, uint32_t cbMax, int cPorts, int nPortOfs, int ver)
{
uint32_t *pu32Cap = (uint32_t *)pbCap;
unsigned cPsi;
Assert(nPortOfs + cPorts < 255);
Assert(ver == 2 || ver == 3);
cPsi = 0; /* Currently only implied port speed IDs. */
/* Make sure there's enough room. */
if (cPsi * 4 + 16 > cbMax)
return 0;
/* Header - includes (USB) specification version. */
*pu32Cap++ = (ver << 24) | (0 << 16) | XHCI_XCP_PROTOCOL;
/* Specification - 'USB ' */
*pu32Cap++ = 0x20425355;
/* Port offsets and counts. 1-based! */
*pu32Cap++ = (cPsi << 28) | (cPorts << 8) | (nPortOfs + 1);
/* Reserved dword. */
*pu32Cap++ = 0;
return (uint8_t *)pu32Cap - pbCap;
}
/** Add an extended capability and link it into the chain. */
static int xhciR3AddExtCap(PXHCI pThis, const uint8_t *pCap, uint32_t cbCap, uint32_t *puPrevOfs)
{
Assert(*puPrevOfs <= pThis->cbExtCap);
Assert(!(cbCap & 3));
/* Check that the extended capability is sane. */
if (cbCap == 0)
return VERR_BUFFER_UNDERFLOW;
if (pThis->cbExtCap + cbCap > XHCI_EXT_CAP_SIZE)
return VERR_BUFFER_OVERFLOW;
if (cbCap > 255 * 4) /* Size must fit into 8-bit dword count. */
return VERR_BUFFER_OVERFLOW;
/* Copy over the capability data and update offsets. */
memcpy(pThis->abExtCap + pThis->cbExtCap, pCap, cbCap);
pThis->abExtCap[*puPrevOfs + 1] = cbCap >> 2;
pThis->abExtCap[pThis->cbExtCap + 1] = 0;
*puPrevOfs = pThis->cbExtCap;
pThis->cbExtCap += cbCap;
return VINF_SUCCESS;
}
/** Build the xHCI Extended Capabilities region. */
static int xhciR3BuildExtCaps(PXHCI pThis, PXHCICC pThisCC)
{
int rc;
uint8_t abXcp[MAX_XCAP_SIZE];
uint32_t cbXcp;
uint32_t uPrevOfs = 0;
Assert(XHCI_NDP_USB2(pThisCC));
Assert(XHCI_NDP_USB3(pThisCC));
/* Most of the extended capabilities are optional or not relevant for PCI
* implementations. However, the Supported Protocol caps are required.
*/
cbXcp = xhciR3BuildProtocolCaps(abXcp, sizeof(abXcp), XHCI_NDP_USB2(pThisCC), 0, 2);
rc = xhciR3AddExtCap(pThis, abXcp, cbXcp, &uPrevOfs);
AssertReturn(RT_SUCCESS(rc), rc);
cbXcp = xhciR3BuildProtocolCaps(abXcp, sizeof(abXcp), XHCI_NDP_USB3(pThisCC), XHCI_NDP_USB2(pThisCC), 3);
rc = xhciR3AddExtCap(pThis, abXcp, cbXcp, &uPrevOfs);
AssertReturn(RT_SUCCESS(rc), rc);
return VINF_SUCCESS;
}
/**
* Select an unused device address. Note that this may fail in the unlikely
* case where all possible addresses are exhausted.
*/
static uint8_t xhciR3SelectNewAddress(PXHCI pThis, uint8_t uSlotID)
{
RT_NOREF(pThis, uSlotID);
/*
* Since there is a 1:1 mapping between USB devices and device slots, we
* should be able to assign a USB address which equals slot ID to any USB
* device. However, the address selection algorithm could be completely
* different (it is not defined by the xHCI spec).
*/
return uSlotID;
}
/**
* Read the address of a device context for a slot from the DCBAA.
*
* @returns Given slot's device context base address.
* @param pDevIns The device instance.
* @param pThis Pointer to the xHCI state.
* @param uSlotID Slot ID to get the context address of.
*/
static uint64_t xhciR3FetchDevCtxAddr(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID)
{
uint64_t uCtxAddr;
RTGCPHYS GCPhysDCBAAE;
Assert(uSlotID > 0);
Assert(uSlotID < XHCI_NDS);
/* Fetch the address of the output slot context from the DCBAA. */
GCPhysDCBAAE = pThis->dcbaap + uSlotID * sizeof(uint64_t);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysDCBAAE, &uCtxAddr, sizeof(uCtxAddr));
LogFlowFunc(("Slot ID %u, device context @ %RGp\n", uSlotID, uCtxAddr));
Assert(uCtxAddr);
return uCtxAddr & XHCI_CTX_ADDR_MASK;
}
/**
* Fetch a device's slot or endpoint context from memory.
*
* @param pDevIns The device instance.
* @param pThis The xHCI device state.
* @param uSlotID Slot ID to access.
* @param uDCI Device Context Index.
* @param pCtx Pointer to storage for the context.
*/
static int xhciR3FetchDevCtx(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID, uint8_t uDCI, void *pCtx)
{
RTGCPHYS GCPhysCtx;
GCPhysCtx = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
LogFlowFunc(("Reading device context @ %RGp, DCI %u\n", GCPhysCtx, uDCI));
GCPhysCtx += uDCI * sizeof(XHCI_SLOT_CTX);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysCtx, pCtx, sizeof(XHCI_SLOT_CTX));
return VINF_SUCCESS;
}
/**
* Fetch a device's slot and endpoint contexts from guest memory.
*
* @param pDevIns The device instance.
* @param pThis The xHCI device state.
* @param uSlotID Slot ID to access.
* @param uDCI Endpoint Device Context Index.
* @param pSlot Pointer to storage for the slot context.
* @param pEp Pointer to storage for the endpoint context.
*/
static int xhciR3FetchCtxAndEp(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID, uint8_t uDCI, XHCI_SLOT_CTX *pSlot, XHCI_EP_CTX *pEp)
{
AssertPtr(pSlot);
AssertPtr(pEp);
Assert(uDCI); /* Can't be 0 -- that's the device context. */
/* Load the slot context. */
xhciR3FetchDevCtx(pDevIns, pThis, uSlotID, 0, pSlot);
/// @todo sanity check the slot context here?
Assert(pSlot->ctx_ent >= uDCI);
/* Load the endpoint context. */
xhciR3FetchDevCtx(pDevIns, pThis, uSlotID, uDCI, pEp);
/// @todo sanity check the endpoint context here?
return VINF_SUCCESS;
}
/**
* Update an endpoint context in guest memory.
*
* @param pDevIns The device instance.
* @param pThis The xHCI device state.
* @param uSlotID Slot ID to access.
* @param uDCI Endpoint Device Context Index.
* @param pEp Pointer to storage of the endpoint context.
*/
static int xhciR3WriteBackEp(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID, uint8_t uDCI, XHCI_EP_CTX *pEp)
{
RTGCPHYS GCPhysCtx;
AssertPtr(pEp);
Assert(uDCI); /* Can't be 0 -- that's the device context. */
/// @todo sanity check the endpoint context here?
/* Find the physical address. */
GCPhysCtx = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
LogFlowFunc(("Writing device context @ %RGp, DCI %u\n", GCPhysCtx, uDCI));
GCPhysCtx += uDCI * sizeof(XHCI_SLOT_CTX);
/* Write the updated context. */
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysCtx, pEp, sizeof(XHCI_EP_CTX));
return VINF_SUCCESS;
}
/**
* Modify an endpoint context such that it enters the running state.
*
* @param pEpCtx Pointer to the endpoint context.
*/
static void xhciR3EnableEP(XHCI_EP_CTX *pEpCtx)
{
LogFlow(("Enabling EP, TRDP @ %RGp, DCS=%u\n", pEpCtx->trdp & XHCI_TRDP_ADDR_MASK, pEpCtx->trdp & XHCI_TRDP_DCS_MASK));
pEpCtx->ep_state = XHCI_EPST_RUNNING;
pEpCtx->trep = pEpCtx->trdp;
}
#endif /* IN_RING3 */
#define MFIND_PERIOD_NS (UINT64_C(2048) * 1000000)
/**
* Set up the MFINDEX wrap timer.
*/
static void xhciSetWrapTimer(PPDMDEVINS pDevIns, PXHCI pThis)
{
uint64_t u64Now;
uint64_t u64LastWrap;
uint64_t u64Expire;
int rc;
/* Try to avoid drift. */
u64Now = PDMDevHlpTimerGet(pDevIns, pThis->hWrapTimer);
// u64LastWrap = u64Now - (u64Now % (0x3FFF * 125000));
u64LastWrap = u64Now / MFIND_PERIOD_NS * MFIND_PERIOD_NS;
/* The MFINDEX counter wraps around every 2048 milliseconds. */
u64Expire = u64LastWrap + (uint64_t)2048 * 1000000;
rc = PDMDevHlpTimerSet(pDevIns, pThis->hWrapTimer, u64Expire);
AssertRC(rc);
}
/**
* Determine whether MSI/MSI-X is enabled for this PCI device.
*
* This influences interrupt handling in xHCI. NB: There should be a PCIDevXxx
* function for this.
*/
static bool xhciIsMSIEnabled(PPDMPCIDEV pDevIns)
{
uint16_t uMsgCtl;
uMsgCtl = PDMPciDevGetWord(pDevIns, XHCI_PCI_MSI_CAP_OFS + VBOX_MSI_CAP_MESSAGE_CONTROL);
return !!(uMsgCtl & VBOX_PCI_MSI_FLAGS_ENABLE);
}
/**
* Get the worker thread going -- there's something to do.
*/
static void xhciKickWorker(PPDMDEVINS pDevIns, PXHCI pThis, XHCI_JOB enmJob, uint32_t uWorkDesc)
{
RT_NOREF(enmJob, uWorkDesc);
/* Tell the worker thread there's something to do. */
if (ASMAtomicReadBool(&pThis->fWrkThreadSleeping))
{
LogFlowFunc(("Signal event semaphore\n"));
int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtProcess);
AssertRC(rc);
}
}
/**
* Fetch the current ERST entry from guest memory.
*/
static void xhciFetchErstEntry(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip)
{
RTGCPHYS GCPhysErste;
XHCI_ERSTE entry;
Assert(ip->erst_idx < ip->erstsz);
GCPhysErste = ip->erstba + ip->erst_idx * sizeof(XHCI_ERSTE);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysErste, &entry, sizeof(entry));
/*
* 6.5 claims values in 16-4096 range are valid, but does not say what
* happens for values outside of that range...
*/
Assert((pThis->status & XHCI_STATUS_HCH) || (entry.size >= 16 && entry.size <= 4096));
/* Cache the entry data internally. */
ip->erep = entry.addr & pThis->erst_addr_mask;
ip->trb_count = entry.size;
Log(("Fetched ERST Entry at %RGp: %u entries at %RGp\n", GCPhysErste, ip->trb_count, ip->erep));
}
/**
* Set the interrupter's IP and EHB bits and trigger an interrupt if required.
*
* @param pDevIns The PDM device instance.
* @param pThis Pointer to the xHCI state.
* @param ip Pointer to the interrupter structure.
*
*/
static void xhciSetIntr(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip)
{
Assert(pThis && ip);
LogFlowFunc(("old IP: %u\n", !!(ip->iman & XHCI_IMAN_IP)));
if (!(ip->iman & XHCI_IMAN_IP))
{
/// @todo assert that we own the interrupter lock
ASMAtomicOrU32(&pThis->status, XHCI_STATUS_EINT);
ASMAtomicOrU64(&ip->erdp, XHCI_ERDP_EHB);
ASMAtomicOrU32(&ip->iman, XHCI_IMAN_IP);
if ((ip->iman & XHCI_IMAN_IE) && (pThis->cmd & XHCI_CMD_INTE))
{
#ifdef XHCI_ERROR_INJECTION
if (pThis->fDropIntrHw)
{
pThis->fDropIntrHw = false;
ASMAtomicAndU32(&ip->iman, ~XHCI_IMAN_IP);
}
else
#endif
{
Log2(("Triggering interrupt on interrupter %u\n", ip->index));
PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
STAM_COUNTER_INC(&pThis->StatIntrsSet);
}
}
else
{
Log2(("Not triggering interrupt on interrupter %u (interrupts disabled)\n", ip->index));
STAM_COUNTER_INC(&pThis->StatIntrsNotSet);
}
/* If MSI/MSI-X is in use, the IP bit is immediately cleared again. */
if (xhciIsMSIEnabled(pDevIns->apPciDevs[0]))
ASMAtomicAndU32(&ip->iman, ~XHCI_IMAN_IP);
}
}
#ifdef IN_RING3
/**
* Set the interrupter's IPE bit. If this causes a 0->1 transition, an
* interrupt may be triggered.
*
* @param pDevIns The PDM device instance.
* @param pThis Pointer to the xHCI state.
* @param ip Pointer to the interrupter structure.
*/
static void xhciR3SetIntrPending(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip)
{
uint16_t imodc = (ip->imod >> XHCI_IMOD_IMODC_SHIFT) & XHCI_IMOD_IMODC_MASK;
Assert(pThis && ip);
LogFlowFunc(("old IPE: %u, IMODC: %u, EREP: %RGp, EHB: %u\n", ip->ipe, imodc, (RTGCPHYS)ip->erep, !!(ip->erdp & XHCI_ERDP_EHB)));
STAM_COUNTER_INC(&pThis->StatIntrsPending);
if (!ip->ipe)
{
#ifdef XHCI_ERROR_INJECTION
if (pThis->fDropIntrIpe)
{
pThis->fDropIntrIpe = false;
}
else
#endif
{
ip->ipe = true;
if (!(ip->erdp & XHCI_ERDP_EHB) && (imodc == 0))
xhciSetIntr(pDevIns, pThis, ip);
}
}
}
/**
* Check if there is space available for writing at least two events on the
* event ring. See 4.9.4 for the state machine (right hand side of diagram).
* If there's only room for one event, the Event Ring Full TRB will need to
* be written out, hence the ring is considered full.
*
* @returns True if space is available, false otherwise.
* @param pDevIns The PDM device instance.
* @param pThis Pointer to the xHCI state.
* @param pIntr Pointer to the interrupter structure.
*/
static bool xhciR3IsEvtRingFull(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR pIntr)
{
uint64_t next_ptr;
uint64_t erdp = pIntr->erdp & XHCI_ERDP_ADDR_MASK;
if (pIntr->trb_count > 1)
{
/* Check the current segment. */
next_ptr = pIntr->erep + sizeof(XHCI_EVENT_TRB);
}
else
{
uint16_t erst_idx;
XHCI_ERSTE entry;
RTGCPHYS GCPhysErste;
/* Need to check the next segment. */
erst_idx = pIntr->erst_idx + 1;
if (erst_idx == pIntr->erstsz)
erst_idx = 0;
GCPhysErste = pIntr->erstba + erst_idx * sizeof(XHCI_ERSTE);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysErste, &entry, sizeof(entry));
next_ptr = entry.addr & pThis->erst_addr_mask;
}
/// @todo We'll have to remember somewhere that the ring is full
return erdp == next_ptr;
}
/**
* Write an event to the given Event Ring. This implements a good chunk of
* the event ring state machine in section 4.9.4 of the xHCI spec.
*
* @returns VBox status code. Error if event could not be enqueued.
* @param pDevIns The PDM device instance.
* @param pThis Pointer to the xHCI state.
* @param pEvent Pointer to the Event TRB to be enqueued.
* @param iIntr Index of the interrupter to write to.
* @param fBlockInt Set if interrupt should be blocked (BEI bit).
*/
static int xhciR3WriteEvent(PPDMDEVINS pDevIns, PXHCI pThis, XHCI_EVENT_TRB *pEvent, unsigned iIntr, bool fBlockInt)
{
PXHCIINTRPTR pIntr;
int rc = VINF_SUCCESS;
LogFlowFunc(("Interrupter: %u\n", iIntr));
/* If the HC isn't running, events can not be generated. However,
* especially port change events can be triggered at any time. We just
* drop them here -- it's often not an error condition.
*/
if (pThis->cmd & XHCI_CMD_RS)
{
STAM_COUNTER_INC(&pThis->StatEventsWritten);
Assert(iIntr < XHCI_NINTR); /* Supplied by guest, potentially invalid. */
pIntr = &pThis->aInterrupters[iIntr & XHCI_INTR_MASK];
/*
* If the interrupter/event ring isn't in a sane state, just
* give up and report Host Controller Error (HCE).
*/
// pIntr->erst_idx
int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pIntr->lock, VERR_IGNORED); /* R3 only, no rcBusy. */
PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pIntr->lock, rcLock); /* eventually, most call chains ignore the status. */
if (xhciR3IsEvtRingFull(pDevIns, pThis, pIntr))
{
LogRel(("xHCI: Event ring full!\n"));
}
/* Set the TRB's Cycle bit as appropriate. */
pEvent->gen.cycle = pIntr->evtr_pcs;
/* Write out the TRB and advance the EREP. */
/// @todo This either has to be atomic from the guest's POV or the cycle bit needs to be toggled last!!
PDMDevHlpPCIPhysWriteMeta(pDevIns, pIntr->erep, pEvent, sizeof(*pEvent));
pIntr->erep += sizeof(*pEvent);
--pIntr->trb_count;
/* Advance to the next ERST entry if necessary. */
if (pIntr->trb_count == 0)
{
++pIntr->erst_idx;
/* If necessary, roll over back to the beginning. */
if (pIntr->erst_idx == pIntr->erstsz)
{
pIntr->erst_idx = 0;
pIntr->evtr_pcs = !pIntr->evtr_pcs;
}
xhciFetchErstEntry(pDevIns, pThis, pIntr);
}
/* Set the IPE bit unless interrupts are blocked. */
if (!fBlockInt)
xhciR3SetIntrPending(pDevIns, pThis, pIntr);
PDMDevHlpCritSectLeave(pDevIns, &pIntr->lock);
}
else
{
STAM_COUNTER_INC(&pThis->StatEventsDropped);
Log(("Event dropped because HC is not running.\n"));
}
return rc;
}
/**
* Post a port change TRB to an Event Ring.
*/
static int xhciR3GenPortChgEvent(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uPort)
{
XHCI_EVENT_TRB ed; /* Event Descriptor */
LogFlowFunc(("Port ID: %u\n", uPort));
/*
* Devices can be "physically" attached/detached regardless of whether
* the HC is running or not, but the port status change events can only
* be generated when R/S is set; xhciR3WriteEvent() takes care of that.
*/
RT_ZERO(ed);
ed.psce.cc = XHCI_TCC_SUCCESS;
ed.psce.port_id = uPort;
ed.psce.type = XHCI_TRB_PORT_SC;
return xhciR3WriteEvent(pDevIns, pThis, &ed, XHCI_PRIMARY_INTERRUPTER, false);
}
/**
* Post a command completion TRB to an Event Ring.
*/
static int xhciR3PostCmdCompletion(PPDMDEVINS pDevIns, PXHCI pThis, unsigned cc, unsigned uSlotID)
{
XHCI_EVENT_TRB ed; /* Event Descriptor */
LogFlowFunc(("Cmd @ %RGp, Completion Code: %u (%s), Slot ID: %u\n", (RTGCPHYS)pThis->cmdr_dqp, cc,
cc < RT_ELEMENTS(g_apszCmplCodes) ? g_apszCmplCodes[cc] : "WHAT?!!", uSlotID));
/* The Command Ring dequeue pointer still holds the address of the current
* command TRB. It is written to the completion event TRB as the command
* TRB pointer.
*/
RT_ZERO(ed);
ed.cce.trb_ptr = pThis->cmdr_dqp;
ed.cce.cc = cc;
ed.cce.type = XHCI_TRB_CMD_CMPL;
ed.cce.slot_id = uSlotID;
return xhciR3WriteEvent(pDevIns, pThis, &ed, XHCI_PRIMARY_INTERRUPTER, false);
}
/**
* Post a transfer event TRB to an Event Ring.
*/
static int xhciR3PostXferEvent(PPDMDEVINS pDevIns, PXHCI pThis, unsigned uIntTgt, unsigned uXferLen, unsigned cc,
unsigned uSlotID, unsigned uEpDCI, uint64_t uEvtData, bool fBlockInt, bool fEvent)
{
XHCI_EVENT_TRB ed; /* Event Descriptor */
LogFlowFunc(("Xfer @ %RGp, Completion Code: %u (%s), Slot ID=%u DCI=%u Target=%u EvtData=%RX64 XfrLen=%u BEI=%u ED=%u\n",
(RTGCPHYS)pThis->cmdr_dqp, cc, cc < RT_ELEMENTS(g_apszCmplCodes) ? g_apszCmplCodes[cc] : "WHAT?!!",
uSlotID, uEpDCI, uIntTgt, uEvtData, uXferLen, fBlockInt, fEvent));
/* A transfer event may be either generated by TRB completion (in case
* fEvent=false) or by a special transfer event TRB (fEvent=true). In
* either case, interrupts may be suppressed.
*/
RT_ZERO(ed);
ed.te.trb_ptr = uEvtData;
ed.te.xfr_len = uXferLen;
ed.te.cc = cc;
ed.te.ed = fEvent;
ed.te.type = XHCI_TRB_XFER;
ed.te.ep_id = uEpDCI;
ed.te.slot_id = uSlotID;
return xhciR3WriteEvent(pDevIns, pThis, &ed, uIntTgt, fBlockInt); /* Sets the cycle bit, too. */
}
static int xhciR3FindRhDevBySlot(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, uint8_t uSlotID, PXHCIROOTHUBR3 *ppRh, uint32_t *puPort)
{
XHCI_SLOT_CTX slot_ctx;
PXHCIROOTHUBR3 pRh;
unsigned iPort;
int rc;
/// @todo Do any of these need to be release assertions?
Assert(uSlotID <= RT_ELEMENTS(pThis->aSlotState));
Assert(pThis->aSlotState[ID_TO_IDX(uSlotID)] > XHCI_DEVSLOT_EMPTY);
/* Load the slot context. */
xhciR3FetchDevCtx(pDevIns, pThis, uSlotID, 0, &slot_ctx);
/* The port ID is stored in the slot context. */
iPort = ID_TO_IDX(slot_ctx.rh_port);
if (iPort < XHCI_NDP_CFG(pThis))
{
/* Find the corresponding root hub. */
pRh = GET_PORT_PRH(pThisCC, iPort);
Assert(pRh);
/* And the device; if the device was ripped out fAttached will be false. */
if (pThisCC->aPorts[iPort].fAttached)
{
/* Provide the information the caller asked for. */
if (ppRh)
*ppRh = pRh;
if (puPort)
*puPort = GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort);
rc = VINF_SUCCESS;
}
else
{
LogFunc(("No device attached (port index %u)!\n", iPort));
rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
}
}
else
{
LogFunc(("Port out of range (index %u)!\n", iPort));
rc = VERR_INVALID_PARAMETER;
}
return rc;
}
static void xhciR3EndlessTrbError(PPDMDEVINS pDevIns, PXHCI pThis)
{
/* Clear the R/S bit and indicate controller error. */
ASMAtomicAndU32(&pThis->cmd, ~XHCI_CMD_RS);
ASMAtomicOrU32(&pThis->status, XHCI_STATUS_HCE);
/* Ensure that XHCI_STATUS_HCH gets set by the worker thread. */
xhciKickWorker(pDevIns, pThis, XHCI_JOB_XFER_DONE, 0);
LogRelMax(10, ("xHCI: Attempted to process too many TRBs, stopping xHC!\n"));
}
/**
* TRB walker callback prototype.
*
* @returns true if walking should continue.
* @returns false if walking should be terminated.
* @param pDevIns The device instance.
* @param pThis The xHCI device state.
* @param pXferTRB Pointer to the transfer TRB to handle.
* @param GCPhysXfrTRB Physical address of the TRB.
* @param pvContext User-defined walk context.
* @remarks We don't need to use DECLCALLBACKPTR here, since all users are in
* the same source file, but having the functions marked with
* DECLCALLBACK helps readability.
*/
typedef DECLCALLBACKPTR(bool, PFNTRBWALKCB,(PPDMDEVINS pDevIns, PXHCI pThis, const XHCI_XFER_TRB *pXferTRB,
RTGCPHYS GCPhysXfrTRB, void *pvContext));
/**
* Walk a chain of TRBs which comprise a single TD.
*
* This is something we need to do potentially more than once when submitting a
* URB and then often again when completing the URB. Note that the walker does
* not update the endpoint state (TRDP/TREP/DCS) so that it can be re-run
* multiple times.
*
* @param pDevIns The device instance.
* @param pThis The xHCI device state.
* @param uTRP Initial TR pointer and DCS.
* @param pfnCbk Callback routine.
* @param pvContext User-defined walk context.
* @param pTREP Pointer to storage for final TR Enqueue Pointer/DCS.
*/
static int xhciR3WalkXferTrbChain(PPDMDEVINS pDevIns, PXHCI pThis, uint64_t uTRP,
PFNTRBWALKCB pfnCbk, void *pvContext, uint64_t *pTREP)
{
RTGCPHYS GCPhysXfrTRB;
uint64_t uTREP;
XHCI_XFER_TRB XferTRB;
bool fContinue = true;
bool dcs;
int rc = VINF_SUCCESS;
unsigned cTrbs = 0;
AssertPtr(pvContext);
AssertPtr(pTREP);
Assert(uTRP);
/* Find the transfer TRB address and the DCS. */
GCPhysXfrTRB = uTRP & XHCI_TRDP_ADDR_MASK;
dcs = !!(uTRP & XHCI_TRDP_DCS_MASK); /* MSC upgrades bool to signed something when comparing with a uint8_t:1. */
LogFlowFunc(("Walking Transfer Ring, TREP:%RGp DCS=%u\n", GCPhysXfrTRB, dcs));
do {
/* Fetch the transfer TRB. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysXfrTRB, &XferTRB, sizeof(XferTRB));
if ((bool)XferTRB.gen.cycle == dcs)
{
Log2(("Walking TRB@%RGp, type %u (%s) %u bytes ENT=%u ISP=%u NS=%u CH=%u IOC=%u IDT=%u\n", GCPhysXfrTRB, XferTRB.gen.type,
XferTRB.gen.type < RT_ELEMENTS(g_apszTrbNames) ? g_apszTrbNames[XferTRB.gen.type] : "WHAT?!!",
XferTRB.gen.xfr_len, XferTRB.gen.ent, XferTRB.gen.isp, XferTRB.gen.ns, XferTRB.gen.ch, XferTRB.gen.ioc, XferTRB.gen.idt));
/* DCS matches, the TRB is ours to process. */
switch (XferTRB.gen.type) {
case XHCI_TRB_LINK:
Log2(("Link intra-TD: Ptr=%RGp IOC=%u TC=%u CH=%u\n", XferTRB.link.rseg_ptr, XferTRB.link.ioc, XferTRB.link.toggle, XferTRB.link.chain));
Assert(XferTRB.link.chain);
/* Do not update the actual TRDP/TREP and DCS yet, just the temporary images. */
GCPhysXfrTRB = XferTRB.link.rseg_ptr & XHCI_TRDP_ADDR_MASK;
if (XferTRB.link.toggle)
dcs = !dcs;
Assert(!XferTRB.link.ioc); /// @todo Needs to be reported.
break;
case XHCI_TRB_NORMAL:
case XHCI_TRB_ISOCH:
case XHCI_TRB_SETUP_STG:
case XHCI_TRB_DATA_STG:
case XHCI_TRB_STATUS_STG:
case XHCI_TRB_EVT_DATA:
fContinue = pfnCbk(pDevIns, pThis, &XferTRB, GCPhysXfrTRB, pvContext);
GCPhysXfrTRB += sizeof(XferTRB);
break;
default:
/* NB: No-op TRBs are not allowed within TDs (4.11.7). */
Log(("Bad TRB type %u found within TD!!\n", XferTRB.gen.type));
fContinue = false;
/// @todo Stop EP etc.?
}
}
else
{
/* We don't have a complete TD. Interesting times. */
Log2(("DCS mismatch, no more TRBs available.\n"));
fContinue = false;
rc = VERR_TRY_AGAIN;
}
/* Kill the xHC if the TRB list has no end in sight. */
if (++cTrbs > XHCI_MAX_NUM_TRBS)
{
/* Stop the xHC with an error. */
xhciR3EndlessTrbError(pDevIns, pThis);
/* Get out of the loop. */
fContinue = false;
rc = VERR_NOT_SUPPORTED; /* No good error code really... */
}
} while (fContinue);
/* Inform caller of the new TR Enqueue Pointer/DCS (not necessarily changed). */
Assert(!(GCPhysXfrTRB & ~XHCI_TRDP_ADDR_MASK));
uTREP = GCPhysXfrTRB | (unsigned)dcs;
Log2(("Final TRP after walk: %RGp\n", uTREP));
*pTREP = uTREP;
return rc;
}
/** Context for probing TD size. */
typedef struct {
uint32_t uXferLen;
uint32_t cTRB;
uint32_t uXfrLenLastED;
uint32_t cTRBLastED;
} XHCI_CTX_XFER_PROBE;
/** Context for submitting 'out' TDs. */
typedef struct {
PVUSBURB pUrb;
uint32_t uXferPos;
unsigned cTRB;
} XHCI_CTX_XFER_SUBMIT;
/** Context for completing TDs. */
typedef struct {
PVUSBURB pUrb;
uint32_t uXferPos;
uint32_t uXferLeft;
unsigned cTRB;
uint32_t uEDTLA : 24;
uint32_t uLastCC : 8;
uint8_t uSlotID;
uint8_t uEpDCI;
bool fMaxCount;
} XHCI_CTX_XFER_COMPLETE;
/** Context for building isochronous URBs. */
typedef struct {
PVUSBURB pUrb;
unsigned iPkt;
uint32_t offCur;
uint64_t uInitTREP;
bool fSubmitFailed;
} XHCI_CTX_ISOCH;
/**
* @callback_method_impl{PFNTRBWALKCB,
* Probe a TD and figure out how big it is so that a URB can be allocated to back it.}
*/
static DECLCALLBACK(bool)
xhciR3WalkDataTRBsProbe(PPDMDEVINS pDevIns, PXHCI pThis, const XHCI_XFER_TRB *pXferTRB, RTGCPHYS GCPhysXfrTRB, void *pvContext)
{
RT_NOREF(pDevIns, pThis, GCPhysXfrTRB);
XHCI_CTX_XFER_PROBE *pCtx = (XHCI_CTX_XFER_PROBE *)pvContext;
pCtx->cTRB++;
/* Only consider TRBs which transfer data. */
switch (pXferTRB->gen.type)
{
case XHCI_TRB_NORMAL:
case XHCI_TRB_ISOCH:
case XHCI_TRB_SETUP_STG:
case XHCI_TRB_DATA_STG:
case XHCI_TRB_STATUS_STG:
pCtx->uXferLen += pXferTRB->norm.xfr_len;
if (RT_UNLIKELY(pCtx->uXferLen > XHCI_MAX_TD_SIZE))
{
/* NB: We let the TD size get a bit past the max so that we don't lose anything,
* but the EDTLA will wrap around.
*/
LogRelMax(10, ("xHCI: TD size (%u) too big, not continuing!\n", pCtx->uXferLen));
return false;
}
break;
case XHCI_TRB_EVT_DATA:
/* Remember where the last seen Event Data TRB was. */
pCtx->cTRBLastED = pCtx->cTRB;
pCtx->uXfrLenLastED = pCtx->uXferLen;
break;
default: /* Could be a link TRB, too. */
break;
}
return pXferTRB->gen.ch;
}
/**
* @callback_method_impl{PFNTRBWALKCB,
* Copy data from a TD (TRB chain) into the corresponding TD. OUT direction only.}
*/
static DECLCALLBACK(bool)
xhciR3WalkDataTRBsSubmit(PPDMDEVINS pDevIns, PXHCI pThis, const XHCI_XFER_TRB *pXferTRB, RTGCPHYS GCPhysXfrTRB, void *pvContext)
{
RT_NOREF(pThis, GCPhysXfrTRB);
XHCI_CTX_XFER_SUBMIT *pCtx = (XHCI_CTX_XFER_SUBMIT *)pvContext;
uint32_t uXferLen = pXferTRB->norm.xfr_len;
/* Only consider TRBs which transfer data. */
switch (pXferTRB->gen.type)
{
case XHCI_TRB_NORMAL:
case XHCI_TRB_ISOCH:
case XHCI_TRB_SETUP_STG:
case XHCI_TRB_DATA_STG:
case XHCI_TRB_STATUS_STG:
/* NB: Transfer length may be zero! */
/// @todo explain/verify abuse of various TRB types here (data stage mapped to normal etc.).
if (uXferLen)
{
/* Sanity check for broken guests (TRBs may have changed since probing). */
if (pCtx->uXferPos + uXferLen <= pCtx->pUrb->cbData)
{
/* Data might be immediate or elsewhere in memory. */
if (pXferTRB->norm.idt)
{
/* If an immediate data TRB claims there's more than 8 bytes, we have a problem. */
if (uXferLen > 8)
{
LogRelMax(10, ("xHCI: Immediate data TRB length %u bytes, ignoring!\n", uXferLen));
return false; /* Stop walking the chain immediately. */
}
Assert(uXferLen >= 1 && uXferLen <= 8);
Log2(("Copying %u bytes to URB offset %u (immediate data)\n", uXferLen, pCtx->uXferPos));
memcpy(pCtx->pUrb->abData + pCtx->uXferPos, pXferTRB, uXferLen);
}
else
{
PDMDevHlpPCIPhysReadUser(pDevIns, pXferTRB->norm.data_ptr, pCtx->pUrb->abData + pCtx->uXferPos, uXferLen);
Log2(("Copying %u bytes to URB offset %u (from %RGp)\n", uXferLen, pCtx->uXferPos, pXferTRB->norm.data_ptr));
}
pCtx->uXferPos += uXferLen;
}
else
{
LogRelMax(10, ("xHCI: Attempted to submit too much data, ignoring!\n"));
return false; /* Stop walking the chain immediately. */
}
}
break;
default: /* Could be an event or status stage TRB, too. */
break;
}
pCtx->cTRB++;
/// @todo Maybe have to make certain that the number of probed TRBs matches? Potentially
/// by the time TRBs get submitted, there might be more of them available if the TD was
/// initially not fully written by HCD.
return pXferTRB->gen.ch;
}
/**
* Perform URB completion processing.
*
* Figure out how much data was really transferred, post events if required, and
* for IN transfers, copy data from the URB.
*
* @callback_method_impl{PFNTRBWALKCB}
*/
static DECLCALLBACK(bool)
xhciR3WalkDataTRBsComplete(PPDMDEVINS pDevIns, PXHCI pThis, const XHCI_XFER_TRB *pXferTRB, RTGCPHYS GCPhysXfrTRB, void *pvContext)
{
XHCI_CTX_XFER_COMPLETE *pCtx = (XHCI_CTX_XFER_COMPLETE *)pvContext;
int rc;
unsigned uXferLen;
unsigned uResidue;
uint8_t cc;
bool fKeepGoing = true;
switch (pXferTRB->gen.type)
{
case XHCI_TRB_NORMAL:
case XHCI_TRB_ISOCH:
case XHCI_TRB_SETUP_STG:
case XHCI_TRB_DATA_STG: /// @todo document abuse; esp. check BEI bit
case XHCI_TRB_STATUS_STG:
/* Assume successful transfer. */
uXferLen = pXferTRB->norm.xfr_len;
cc = XHCI_TCC_SUCCESS;
/* If there was a short packet, handle it accordingly. */
if (pCtx->uXferLeft < uXferLen)
{
/* The completion code is set regardless of IOC/ISP. It may be
* reported later via an Event Data TRB (4.10.1.1)
*/
uXferLen = pCtx->uXferLeft;
cc = XHCI_TCC_SHORT_PKT;
}
if (pCtx->pUrb->enmDir == VUSBDIRECTION_IN)
{
Assert(!pXferTRB->norm.idt);
/* NB: Transfer length may be zero! */
if (uXferLen)
{
if (uXferLen <= pCtx->uXferLeft)
{
Log2(("Writing %u bytes to %RGp from URB offset %u (TRB@%RGp)\n", uXferLen, pXferTRB->norm.data_ptr, pCtx->uXferPos, GCPhysXfrTRB));
PDMDevHlpPCIPhysWriteUser(pDevIns, pXferTRB->norm.data_ptr, pCtx->pUrb->abData + pCtx->uXferPos, uXferLen);
}
else
{
LogRelMax(10, ("xHCI: Attempted to read too much data, ignoring!\n"));
}
}
}
/* Update position within TD. */
pCtx->uXferLeft -= uXferLen;
pCtx->uXferPos += uXferLen;
Log2(("Current uXferLeft=%u, uXferPos=%u (length was %u)\n", pCtx->uXferLeft, pCtx->uXferPos, uXferLen));
/* Keep track of the EDTLA and last completion status. */
pCtx->uEDTLA += uXferLen; /* May wrap around! */
pCtx->uLastCC = cc;
/* Report events as required. */
uResidue = pXferTRB->norm.xfr_len - uXferLen;
if (pXferTRB->norm.ioc || (pXferTRB->norm.isp && uResidue))
{
rc = xhciR3PostXferEvent(pDevIns, pThis, pXferTRB->norm.int_tgt, uResidue, cc,
pCtx->uSlotID, pCtx->uEpDCI, GCPhysXfrTRB, pXferTRB->norm.bei, false);
}
break;
case XHCI_TRB_EVT_DATA:
if (pXferTRB->evtd.ioc)
{
rc = xhciR3PostXferEvent(pDevIns, pThis, pXferTRB->evtd.int_tgt, pCtx->uEDTLA, pCtx->uLastCC,
pCtx->uSlotID, pCtx->uEpDCI, pXferTRB->evtd.evt_data, pXferTRB->evtd.bei, true);
}
/* Clear the EDTLA. */
pCtx->uEDTLA = 0;
break;
default:
AssertMsgFailed(("%#x\n", pXferTRB->gen.type));
break;
}
pCtx->cTRB--;
/* For TD fragments, enforce the maximum count, but only as long as the transfer
* is successful. In case of error we have to complete the entire TD! */
if (!pCtx->cTRB && pCtx->fMaxCount && pCtx->uLastCC == XHCI_TCC_SUCCESS)
{
Log2(("Stopping at the end of TD Fragment.\n"));
fKeepGoing = false;
}
/* NB: We currently do not enforce that the number of TRBs can't change between
* submission and completion. If we do, we'll have to store it somewhere for
* isochronous URBs.
*/
return pXferTRB->gen.ch && fKeepGoing;
}
/**
* Process (consume) non-data TRBs on a transfer ring. This function
* completes TRBs which do not have any URB associated with them. Only
* used with running endpoints. Usable regardless of whether there are
* in-flight TRBs or not. Returns the next TRB and its address to the
* caller. May modify the endpoint context!
*
* @param pDevIns The device instance.
* @param pThis The xHCI device state.
* @param uSlotID The slot corresponding to this USB device.
* @param uEpDCI The DCI of this endpoint.
* @param pEpCtx Endpoint context. May be modified.
* @param pXfer Storage for returning the next TRB to caller.
* @param pGCPhys Storage for returning the physical address of TRB.
*/
static int xhciR3ConsumeNonXferTRBs(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID, uint8_t uEpDCI,
XHCI_EP_CTX *pEpCtx, XHCI_XFER_TRB *pXfer, RTGCPHYS *pGCPhys)
{
XHCI_XFER_TRB xfer;
RTGCPHYS GCPhysXfrTRB = 0;
bool dcs;
bool fInFlight;
bool fContinue = true;
int rc;
unsigned cTrbs = 0;
LogFlowFunc(("Slot ID: %u, EP DCI %u\n", uSlotID, uEpDCI));
Assert(uSlotID > 0);
Assert(uSlotID <= XHCI_NDS);
Assert(pEpCtx->ep_state == XHCI_EPST_RUNNING);
do
{
/* Find the transfer TRB address. */
GCPhysXfrTRB = pEpCtx->trdp & XHCI_TRDP_ADDR_MASK;
dcs = !!(pEpCtx->trdp & XHCI_TRDP_DCS_MASK);
/* Determine whether there are any in-flight TRBs or not. This affects TREP
* processing -- when nothing is in flight, we have to move both TREP and TRDP;
* otherwise only the TRDP must be updated.
*/
fInFlight = pEpCtx->trep != pEpCtx->trdp;
LogFlowFunc(("Skipping non-data TRBs, TREP:%RGp, TRDP:%RGp, in-flight: %RTbool\n", pEpCtx->trep, pEpCtx->trdp, fInFlight));
/* Fetch the transfer TRB. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysXfrTRB, &xfer, sizeof(xfer));
/* Make sure the Cycle State matches. */
if ((bool)xfer.gen.cycle == dcs)
{
Log2(("TRB @ %RGp, type %u (%s) %u bytes ENT=%u ISP=%u NS=%u CH=%u IOC=%u IDT=%u\n", GCPhysXfrTRB, xfer.gen.type,
xfer.gen.type < RT_ELEMENTS(g_apszTrbNames) ? g_apszTrbNames[xfer.gen.type] : "WHAT?!!",
xfer.gen.xfr_len, xfer.gen.ent, xfer.gen.isp, xfer.gen.ns, xfer.gen.ch, xfer.gen.ioc, xfer.gen.idt));
switch (xfer.gen.type) {
case XHCI_TRB_LINK:
Log2(("Link extra-TD: Ptr=%RGp IOC=%u TC=%u CH=%u\n", xfer.link.rseg_ptr, xfer.link.ioc, xfer.link.toggle, xfer.link.chain));
Assert(!xfer.link.chain);
/* Set new TRDP but leave DCS bit alone... */
pEpCtx->trdp = (xfer.link.rseg_ptr & XHCI_TRDP_ADDR_MASK) | (pEpCtx->trdp & XHCI_TRDP_DCS_MASK);
/* ...and flip the DCS bit if required. Then update the TREP. */
if (xfer.link.toggle)
pEpCtx->trdp = (pEpCtx->trdp & ~XHCI_TRDP_DCS_MASK) | (pEpCtx->trdp ^ XHCI_TRDP_DCS_MASK);
if (!fInFlight)
pEpCtx->trep = pEpCtx->trdp;
if (xfer.link.ioc)
rc = xhciR3PostXferEvent(pDevIns, pThis, xfer.link.int_tgt, 0, XHCI_TCC_SUCCESS, uSlotID, uEpDCI,
GCPhysXfrTRB, false, false);
break;
case XHCI_TRB_NOOP_XFER:
Log2(("No op xfer: IOC=%u CH=%u ENT=%u\n", xfer.nop.ioc, xfer.nop.ch, xfer.nop.ent));
/* A no-op transfer TRB must not be part of a chain. See 4.11.7. */
Assert(!xfer.link.chain);
/* Update enqueue/dequeue pointers. */
pEpCtx->trdp += sizeof(XHCI_XFER_TRB);
if (!fInFlight)
pEpCtx->trep += sizeof(XHCI_XFER_TRB);
if (xfer.nop.ioc)
rc = xhciR3PostXferEvent(pDevIns, pThis, xfer.nop.int_tgt, 0, XHCI_TCC_SUCCESS, uSlotID, uEpDCI,
GCPhysXfrTRB, false, false);
break;
default:
fContinue = false;
break;
}
}
else
{
LogFunc(("Transfer Ring empty\n"));
fContinue = false;
}
/* Kill the xHC if the TRB list has no end in sight. */
/* NB: The limit here could perhaps be much lower because a sequence of Link
* and No-op TRBs with no real work to be done would be highly suspect.
*/
if (++cTrbs > XHCI_MAX_NUM_TRBS)
{
/* Stop the xHC with an error. */
xhciR3EndlessTrbError(pDevIns, pThis);
/* Get out of the loop. */
fContinue = false;
rc = VERR_NOT_SUPPORTED; /* No good error code really... */
}
} while (fContinue);
/* The caller will need the next TRB. Hand it over. */
Assert(GCPhysXfrTRB);
*pGCPhys = GCPhysXfrTRB;
*pXfer = xfer;
LogFlowFunc(("Final TREP:%RGp, TRDP:%RGp GCPhysXfrTRB:%RGp\n", pEpCtx->trep, pEpCtx->trdp, GCPhysXfrTRB));
return VINF_SUCCESS;
}
/**
* Transfer completion callback routine.
*
* VUSB will call this when a transfer have been completed
* in a one or another way.
*
* @param pInterface Pointer to XHCI::ROOTHUB::IRhPort.
* @param pUrb Pointer to the URB in question.
*/
static DECLCALLBACK(void) xhciR3RhXferCompletion(PVUSBIROOTHUBPORT pInterface, PVUSBURB pUrb)
{
PXHCIROOTHUBR3 pRh = RT_FROM_MEMBER(pInterface, XHCIROOTHUBR3, IRhPort);
PXHCICC pThisCC = pRh->pXhciR3;
PPDMDEVINS pDevIns = pThisCC->pDevIns;
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
XHCI_SLOT_CTX slot_ctx;
XHCI_EP_CTX ep_ctx;
XHCI_XFER_TRB xfer;
RTGCPHYS GCPhysXfrTRB;
int rc;
unsigned uResidue = 0;
uint8_t uSlotID = pUrb->pHci->uSlotID;
uint8_t cc = XHCI_TCC_SUCCESS;
uint8_t uEpDCI;
/* Check for URBs completed synchronously as part of xHCI command execution.
* The URB will have zero cTRB as it's not associated with a TD.
*/
if (!pUrb->pHci->cTRB)
{
LogFlow(("%s: xhciR3RhXferCompletion: uSlotID=%u EP=%u cTRB=%d cbData=%u status=%u\n",
pUrb->pszDesc, uSlotID, pUrb->EndPt, pUrb->pHci->cTRB, pUrb->cbData, pUrb->enmStatus));
LogFlow(("%s: xhciR3RhXferCompletion: Completing xHCI-generated request\n", pUrb->pszDesc));
return;
}
/* If the xHC isn't running, just drop the URB right here. */
if (pThis->status & XHCI_STATUS_HCH)
{
LogFlow(("%s: xhciR3RhXferCompletion: uSlotID=%u EP=%u cTRB=%d cbData=%u status=%u\n",
pUrb->pszDesc, uSlotID, pUrb->EndPt, pUrb->pHci->cTRB, pUrb->cbData, pUrb->enmStatus));
LogFlow(("%s: xhciR3RhXferCompletion: xHC halted, skipping URB completion\n", pUrb->pszDesc));
return;
}
#ifdef XHCI_ERROR_INJECTION
if (pThis->fDropUrb)
{
LogFlow(("%s: xhciR3RhXferCompletion: Error injection, dropping URB!\n", pUrb->pszDesc));
pThis->fDropUrb = false;
return;
}
#endif
RTCritSectEnter(&pThisCC->CritSectThrd);
/* Convert USB endpoint address to xHCI format. */
if (pUrb->EndPt)
uEpDCI = pUrb->EndPt * 2 + (pUrb->enmDir == VUSBDIRECTION_IN ? 1 : 0);
else
uEpDCI = 1; /* EP 0 */
LogFlow(("%s: xhciR3RhXferCompletion: uSlotID=%u EP=%u cTRB=%d\n",
pUrb->pszDesc, uSlotID, pUrb->EndPt, pUrb->pHci->cTRB));
LogFlow(("%s: xhciR3RhXferCompletion: EP DCI=%u, cbData=%u status=%u\n", pUrb->pszDesc, uEpDCI, pUrb->cbData, pUrb->enmStatus));
/* Load the slot/endpoint contexts from guest memory. */
xhciR3FetchCtxAndEp(pDevIns, pThis, uSlotID, uEpDCI, &slot_ctx, &ep_ctx);
/* If the EP is disabled, we don't own it so we can't complete the URB.
* Leave this EP alone and drop the URB.
*/
if (ep_ctx.ep_state != XHCI_EPST_RUNNING)
{
Log(("EP DCI %u not running (state %u), skipping URB completion\n", uEpDCI, ep_ctx.ep_state));
RTCritSectLeave(&pThisCC->CritSectThrd);
return;
}
/* Now complete any non-transfer TRBs that might be on the transfer ring before
* the TRB(s) corresponding to this URB. Preloads the TRB as a side effect.
* Endpoint state now must be written back in case it was modified!
*/
xhciR3ConsumeNonXferTRBs(pDevIns, pThis, uSlotID, uEpDCI, &ep_ctx, &xfer, &GCPhysXfrTRB);
/* Deal with failures which halt the EP first. */
if (RT_UNLIKELY(pUrb->enmStatus != VUSBSTATUS_OK))
{
switch(pUrb->enmStatus)
{
case VUSBSTATUS_STALL:
/* Halt the endpoint and inform the HCD.
* NB: The TRDP is NOT advanced in case of error.
*/
ep_ctx.ep_state = XHCI_EPST_HALTED;
cc = XHCI_TCC_STALL;
rc = xhciR3PostXferEvent(pDevIns, pThis, xfer.gen.int_tgt, uResidue, cc,
uSlotID, uEpDCI, GCPhysXfrTRB, false, false);
break;
case VUSBSTATUS_DNR:
/* Halt the endpoint and inform the HCD.
* NB: The TRDP is NOT advanced in case of error.
*/
ep_ctx.ep_state = XHCI_EPST_HALTED;
cc = XHCI_TCC_USB_XACT_ERR;
rc = xhciR3PostXferEvent(pDevIns, pThis, xfer.gen.int_tgt, uResidue, cc,
uSlotID, uEpDCI, GCPhysXfrTRB, false, false);
break;
case VUSBSTATUS_CRC: /// @todo Separate status for canceling?!
ep_ctx.ep_state = XHCI_EPST_HALTED;
cc = XHCI_TCC_USB_XACT_ERR;
rc = xhciR3PostXferEvent(pDevIns, pThis, xfer.gen.int_tgt, uResidue, cc,
uSlotID, uEpDCI, GCPhysXfrTRB, false, false);
/* NB: The TRDP is *not* advanced and TREP is reset. */
ep_ctx.trep = ep_ctx.trdp;
break;
case VUSBSTATUS_DATA_OVERRUN:
case VUSBSTATUS_DATA_UNDERRUN:
/* Halt the endpoint and inform the HCD.
* NB: The TRDP is NOT advanced in case of error.
*/
ep_ctx.ep_state = XHCI_EPST_HALTED;
cc = XHCI_TCC_DATA_BUF_ERR;
rc = xhciR3PostXferEvent(pDevIns, pThis, xfer.gen.int_tgt, uResidue, cc,
uSlotID, uEpDCI, GCPhysXfrTRB, false, false);
break;
default:
AssertMsgFailed(("Unexpected URB status %u\n", pUrb->enmStatus));
}
if (pUrb->enmType == VUSBXFERTYPE_ISOC)
STAM_COUNTER_INC(&pThis->StatErrorIsocUrbs);
}
else if (xfer.gen.type == XHCI_TRB_NORMAL)
{
XHCI_CTX_XFER_COMPLETE ctxComplete;
uint64_t uTRDP;
ctxComplete.pUrb = pUrb;
ctxComplete.uXferPos = 0;
ctxComplete.uXferLeft = pUrb->cbData;
ctxComplete.cTRB = pUrb->pHci->cTRB;
ctxComplete.uSlotID = uSlotID;
ctxComplete.uEpDCI = uEpDCI;
ctxComplete.uEDTLA = 0; // Always zero at the beginning of a new TD.
ctxComplete.uLastCC = cc;
ctxComplete.fMaxCount = ep_ctx.ifc >= XHCI_NO_QUEUING_IN_FLIGHT;
xhciR3WalkXferTrbChain(pDevIns, pThis, ep_ctx.trdp, xhciR3WalkDataTRBsComplete, &ctxComplete, &uTRDP);
ep_ctx.last_cc = ctxComplete.uLastCC;
ep_ctx.trdp = uTRDP;
if (ep_ctx.ifc >= XHCI_NO_QUEUING_IN_FLIGHT)
ep_ctx.ifc -= XHCI_NO_QUEUING_IN_FLIGHT; /* TD fragment done, allow further queuing. */
else
ep_ctx.ifc--; /* TD done, decrement in-flight counter. */
}
else if (xfer.gen.type == XHCI_TRB_ISOCH)
{
XHCI_CTX_XFER_COMPLETE ctxComplete;
uint64_t uTRDP;
unsigned iPkt;
ctxComplete.pUrb = pUrb;
ctxComplete.uSlotID = uSlotID;
ctxComplete.uEpDCI = uEpDCI;
for (iPkt = 0; iPkt < pUrb->cIsocPkts; ++iPkt) {
ctxComplete.uXferPos = pUrb->aIsocPkts[iPkt].off;
ctxComplete.uXferLeft = pUrb->aIsocPkts[iPkt].cb;
ctxComplete.cTRB = pUrb->pHci->cTRB;
ctxComplete.uEDTLA = 0; // Zero at TD start.
ctxComplete.uLastCC = cc;
ctxComplete.fMaxCount = false;
if (pUrb->aIsocPkts[iPkt].enmStatus != VUSBSTATUS_OK)
STAM_COUNTER_INC(&pThis->StatErrorIsocPkts);
xhciR3WalkXferTrbChain(pDevIns, pThis, ep_ctx.trdp, xhciR3WalkDataTRBsComplete, &ctxComplete, &uTRDP);
ep_ctx.last_cc = ctxComplete.uLastCC;
ep_ctx.trdp = uTRDP;
xhciR3ConsumeNonXferTRBs(pDevIns, pThis, uSlotID, uEpDCI, &ep_ctx, &xfer, &GCPhysXfrTRB);
}
ep_ctx.ifc--; /* TD done, decrement in-flight counter. */
}
else if (xfer.gen.type == XHCI_TRB_SETUP_STG || xfer.gen.type == XHCI_TRB_DATA_STG || xfer.gen.type == XHCI_TRB_STATUS_STG)
{
XHCI_CTX_XFER_COMPLETE ctxComplete;
uint64_t uTRDP;
ctxComplete.pUrb = pUrb;
ctxComplete.uXferPos = 0;
ctxComplete.uXferLeft = pUrb->cbData;
ctxComplete.cTRB = pUrb->pHci->cTRB;
ctxComplete.uSlotID = uSlotID;
ctxComplete.uEpDCI = uEpDCI;
ctxComplete.uEDTLA = 0; // Always zero at the beginning of a new TD.
ctxComplete.uLastCC = cc;
ctxComplete.fMaxCount = ep_ctx.ifc >= XHCI_NO_QUEUING_IN_FLIGHT;
xhciR3WalkXferTrbChain(pDevIns, pThis, ep_ctx.trdp, xhciR3WalkDataTRBsComplete, &ctxComplete, &uTRDP);
ep_ctx.last_cc = ctxComplete.uLastCC;
ep_ctx.trdp = uTRDP;
}
else
{
AssertMsgFailed(("Unexpected TRB type %u\n", xfer.gen.type));
Log2(("TRB @ %RGp, type %u unexpected!\n", GCPhysXfrTRB, xfer.gen.type));
/* Advance the TRDP anyway so that the endpoint isn't completely stuck. */
ep_ctx.trdp += sizeof(XHCI_XFER_TRB);
}
/* Update the endpoint state. */
xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uEpDCI, &ep_ctx);
RTCritSectLeave(&pThisCC->CritSectThrd);
if (pUrb->enmStatus == VUSBSTATUS_OK)
{
/* Completion callback usually runs on a separate thread. Let the worker do more. */
Log2(("Ring bell for slot %u, DCI %u\n", uSlotID, uEpDCI));
ASMAtomicOrU32(&pThis->aBellsRung[ID_TO_IDX(uSlotID)], 1 << uEpDCI);
xhciKickWorker(pDevIns, pThis, XHCI_JOB_XFER_DONE, 0);
}
}
/**
* Handle transfer errors.
*
* VUSB calls this when a transfer attempt failed. This function will respond
* indicating whether to retry or complete the URB with failure.
*
* @returns true if the URB should be retired.
* @returns false if the URB should be re-tried.
* @param pInterface Pointer to XHCI::ROOTHUB::IRhPort.
* @param pUrb Pointer to the URB in question.
*/
static DECLCALLBACK(bool) xhciR3RhXferError(PVUSBIROOTHUBPORT pInterface, PVUSBURB pUrb)
{
PXHCIROOTHUBR3 pRh = RT_FROM_MEMBER(pInterface, XHCIROOTHUBR3, IRhPort);
PXHCICC pThisCC = pRh->pXhciR3;
PXHCI pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PXHCI);
bool fRetire = true;
/* If the xHC isn't running, get out of here immediately. */
if (pThis->status & XHCI_STATUS_HCH)
{
Log(("xHC halted, skipping URB error handling\n"));
return fRetire;
}
RTCritSectEnter(&pThisCC->CritSectThrd);
Assert(pUrb->pHci->cTRB); /* xHCI-generated URBs should not fail! */
if (!pUrb->pHci->cTRB)
{
Log(("%s: Failing xHCI-generated request!\n", pUrb->pszDesc));
}
else if (pUrb->enmStatus == VUSBSTATUS_STALL)
{
/* Don't retry on stall. */
Log2(("%s: xhciR3RhXferError: STALL, giving up.\n", pUrb->pszDesc));
} else if (pUrb->enmStatus == VUSBSTATUS_CRC) {
/* Don't retry on CRC errors either. These indicate canceled URBs, among others. */
Log2(("%s: xhciR3RhXferError: CRC, giving up.\n", pUrb->pszDesc));
} else if (pUrb->enmStatus == VUSBSTATUS_DNR) {
/* Don't retry on DNR errors. These indicate the device vanished. */
Log2(("%s: xhciR3RhXferError: DNR, giving up.\n", pUrb->pszDesc));
} else if (pUrb->enmStatus == VUSBSTATUS_DATA_OVERRUN) {
/* Don't retry on OVERRUN errors. These indicate a fatal error. */
Log2(("%s: xhciR3RhXferError: OVERRUN, giving up.\n", pUrb->pszDesc));
} else if (pUrb->enmStatus == VUSBSTATUS_DATA_UNDERRUN) {
/* Don't retry on UNDERRUN errors. These indicate a fatal error. */
Log2(("%s: xhciR3RhXferError: UNDERRUN, giving up.\n", pUrb->pszDesc));
} else {
/// @todo
AssertMsgFailed(("%#x\n", pUrb->enmStatus));
}
RTCritSectLeave(&pThisCC->CritSectThrd);
return fRetire;
}
/**
* Queue a TD composed of normal TRBs, event data TRBs, and suchlike.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pThis The xHCI device state, shared edition.
* @param pThisCC The xHCI device state, ring-3 edition.
* @param pRh Root hub for the device.
* @param GCPhysTRB Physical gues address of the TRB.
* @param pTrb Pointer to the contents of the first TRB.
* @param pEpCtx Pointer to the cached EP context.
* @param uSlotID ID of the associated slot context.
* @param uAddr The device address.
* @param uEpDCI The DCI(!) of the endpoint.
*/
static int xhciR3QueueDataTD(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, PXHCIROOTHUBR3 pRh, RTGCPHYS GCPhysTRB,
XHCI_XFER_TRB *pTrb, XHCI_EP_CTX *pEpCtx, uint8_t uSlotID, uint8_t uAddr, uint8_t uEpDCI)
{
RT_NOREF(GCPhysTRB);
XHCI_CTX_XFER_PROBE ctxProbe;
XHCI_CTX_XFER_SUBMIT ctxSubmit;
uint64_t uTREP;
bool fFragOnly = false;
int rc;
VUSBXFERTYPE enmType;
VUSBDIRECTION enmDir;
/* Discover how big this TD is. */
RT_ZERO(ctxProbe);
rc = xhciR3WalkXferTrbChain(pDevIns, pThis, pEpCtx->trep, xhciR3WalkDataTRBsProbe, &ctxProbe, &uTREP);
if (RT_SUCCESS(rc))
LogFlowFunc(("Probed %u TRBs, %u bytes total, TREP@%RX64\n", ctxProbe.cTRB, ctxProbe.uXferLen, uTREP));
else
{
LogFlowFunc(("Probing failed after %u TRBs, %u bytes total (last ED after %u TRBs and %u bytes), TREP@%RX64\n", ctxProbe.cTRB, ctxProbe.uXferLen, ctxProbe.cTRBLastED, ctxProbe.uXfrLenLastED, uTREP));
if (rc == VERR_TRY_AGAIN && pTrb->gen.type == XHCI_TRB_NORMAL && ctxProbe.cTRBLastED)
{
/* The TD is incomplete, but we have at least one TD fragment. We can create a URB for
* what we have but we can't safely queue any more because if any error occurs, the
* TD needs to fail as a whole.
* OS X Mavericks and Yosemite tend to trigger this case when reading from USB 3.0
* MSDs (transfers up to 1MB).
*/
fFragOnly = true;
/* Because we currently do not maintain the EDTLA across URBs, we have to only submit
* TD fragments up to where we last saw an Event Data TRB. If there was no Event Data
* TRB, we'll just try waiting a bit longer for the TD to be complete or an Event Data
* TRB to show up. The guest is extremely likely to do one or the other, since otherwise
* it has no way to tell when the transfer completed.
*/
ctxProbe.cTRB = ctxProbe.cTRBLastED;
ctxProbe.uXferLen = ctxProbe.uXfrLenLastED;
}
else
return rc;
}
/* Determine the transfer kind based on endpoint type. */
switch (pEpCtx->ep_type)
{
case XHCI_EPTYPE_BULK_IN:
case XHCI_EPTYPE_BULK_OUT:
enmType = VUSBXFERTYPE_BULK;
break;
case XHCI_EPTYPE_INTR_IN:
case XHCI_EPTYPE_INTR_OUT:
enmType = VUSBXFERTYPE_INTR;
break;
case XHCI_EPTYPE_CONTROL:
enmType = VUSBXFERTYPE_CTRL;
break;
case XHCI_EPTYPE_ISOCH_IN:
case XHCI_EPTYPE_ISOCH_OUT:
default:
enmType = VUSBXFERTYPE_INVALID;
AssertMsgFailed(("%#x\n", pEpCtx->ep_type));
}
/* Determine the direction based on endpoint type. */
switch (pEpCtx->ep_type)
{
case XHCI_EPTYPE_BULK_IN:
case XHCI_EPTYPE_INTR_IN:
enmDir = VUSBDIRECTION_IN;
break;
case XHCI_EPTYPE_BULK_OUT:
case XHCI_EPTYPE_INTR_OUT:
enmDir = VUSBDIRECTION_OUT;
break;
default:
enmDir = VUSBDIRECTION_INVALID;
AssertMsgFailed(("%#x\n", pEpCtx->ep_type));
}
/* Allocate and initialize a URB. */
PVUSBURB pUrb = VUSBIRhNewUrb(pRh->pIRhConn, uAddr, VUSB_DEVICE_PORT_INVALID, enmType, enmDir, ctxProbe.uXferLen, ctxProbe.cTRB, NULL);
if (!pUrb)
return VERR_OUT_OF_RESOURCES; /// @todo handle error!
STAM_COUNTER_ADD(&pThis->StatTRBsPerDtaUrb, ctxProbe.cTRB);
/* See 4.5.1 about xHCI vs. USB endpoint addressing. */
Assert(uEpDCI);
pUrb->EndPt = uEpDCI / 2; /* DCI = EP * 2 + direction */
pUrb->fShortNotOk = false; /* We detect short packets ourselves. */
pUrb->enmStatus = VUSBSTATUS_OK;
/// @todo Cross check that the EP type corresponds to direction. Probably
//should check when configuring device?
pUrb->pHci->uSlotID = uSlotID;
/* For OUT transfers, copy the TD data into the URB. */
if (pUrb->enmDir == VUSBDIRECTION_OUT)
{
ctxSubmit.pUrb = pUrb;
ctxSubmit.uXferPos = 0;
ctxSubmit.cTRB = 0;
xhciR3WalkXferTrbChain(pDevIns, pThis, pEpCtx->trep, xhciR3WalkDataTRBsSubmit, &ctxSubmit, &uTREP);
Assert(ctxProbe.cTRB == ctxSubmit.cTRB);
ctxProbe.cTRB = ctxSubmit.cTRB;
}
/* If only completing a fragment, remember the TRB count and increase
* the in-flight count past the limit so we won't queue any more.
*/
pUrb->pHci->cTRB = ctxProbe.cTRB;
if (fFragOnly)
/* Bit of a hack -- prevent further queuing. */
pEpCtx->ifc += XHCI_NO_QUEUING_IN_FLIGHT;
else
/* Increment the in-flight counter before queuing more. */
pEpCtx->ifc++;
/* Commit the updated TREP; submitting the URB may already invoke completion callbacks. */
pEpCtx->trep = uTREP;
xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uEpDCI, pEpCtx);
/*
* Submit the URB.
*/
STAM_COUNTER_ADD(&pThis->StatUrbSizeData, pUrb->cbData);
Log(("%s: xhciR3QueueDataTD: Addr=%u, EndPt=%u, enmDir=%u cbData=%u\n",
pUrb->pszDesc, pUrb->DstAddress, pUrb->EndPt, pUrb->enmDir, pUrb->cbData));
RTCritSectLeave(&pThisCC->CritSectThrd);
rc = VUSBIRhSubmitUrb(pRh->pIRhConn, pUrb, &pRh->Led);
RTCritSectEnter(&pThisCC->CritSectThrd);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
/* Failure cleanup. Can happen if we're still resetting the device or out of resources,
* or the user just ripped out the device.
*/
/// @todo Mark the EP as halted and inactive and write back the changes.
return VERR_OUT_OF_RESOURCES;
}
/**
* Queue an isochronous TD composed of isochronous and normal TRBs, event
* data TRBs, and suchlike. This TD may either correspond to a single URB or
* form one packet of an isochronous URB.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pThis The xHCI device state, shared edition.
* @param pThisCC The xHCI device state, ring-3 edition.
* @param pRh Root hub for the device.
* @param GCPhysTRB Physical guest address of the TRB.
* @param pTrb Pointer to the contents of the first TRB.
* @param pEpCtx Pointer to the cached EP context.
* @param uSlotID ID of the associated slot context.
* @param uAddr The device address.
* @param uEpDCI The DCI(!) of the endpoint.
* @param pCtxIso Additional isochronous URB context.
*/
static int xhciR3QueueIsochTD(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, PXHCIROOTHUBR3 pRh, RTGCPHYS GCPhysTRB,
XHCI_XFER_TRB *pTrb, XHCI_EP_CTX *pEpCtx, uint8_t uSlotID, uint8_t uAddr, uint8_t uEpDCI,
XHCI_CTX_ISOCH *pCtxIso)
{
RT_NOREF(GCPhysTRB, pTrb);
XHCI_CTX_XFER_PROBE ctxProbe;
XHCI_CTX_XFER_SUBMIT ctxSubmit;
uint64_t uTREP;
PVUSBURB pUrb;
unsigned cIsoPackets;
uint32_t cbPktMax;
/* Discover how big this TD is. */
RT_ZERO(ctxProbe);
xhciR3WalkXferTrbChain(pDevIns, pThis, pEpCtx->trep, xhciR3WalkDataTRBsProbe, &ctxProbe, &uTREP);
LogFlowFunc(("Probed %u TRBs, %u bytes total, TREP@%RX64\n", ctxProbe.cTRB, ctxProbe.uXferLen, uTREP));
/* See 4.5.1 about xHCI vs. USB endpoint addressing. */
Assert(uEpDCI);
/* For isochronous transfers, there's a bit of extra work to do. The interval
* is key and determines whether the TD will directly correspond to a URB or
* if it will only form part of a larger URB. In any case, one TD equals one
* 'packet' of an isochronous URB.
*/
switch (pEpCtx->interval)
{
case 0: /* Every 2^0 * 125us, i.e. 8 per frame. */
cIsoPackets = 8;
break;
case 1: /* Every 2^1 * 125us, i.e. 4 per frame. */
cIsoPackets = 4;
break;
case 2: /* Every 2^2 * 125us, i.e. 2 per frame. */
cIsoPackets = 2;
break;
case 3: /* Every 2^3 * 125us, i.e. 1 per frame. */
default:/* Or any larger interval (every n frames).*/
cIsoPackets = 1;
break;
}
/* We do not know exactly how much data might be transferred until we
* look at all TDs/packets that constitute the URB. However, we do know
* the maximum possible size even without probing any TDs at all.
* The actual size is expected to be the same or at most slightly smaller,
* hence it makes sense to allocate the URB right away and copy data into
* it as we go, rather than doing complicated probing first.
* The Max Endpoint Service Interval Time (ESIT) Payload defines the
* maximum number of bytes that can be transferred per interval (4.14.2).
* Unfortunately Apple was lazy and their driver leaves the Max ESIT
* Payload as zero, so we have to do the math ourselves.
*/
/* Calculate the maximum transfer size per (micro)frame. */
/// @todo This ought to be stored within the URB somewhere.
cbPktMax = pEpCtx->max_pkt_sz * (pEpCtx->max_brs_sz + 1) * (pEpCtx->mult + 1);
if (!pCtxIso->pUrb)
{
uint32_t cbUrbMax = cIsoPackets * cbPktMax;
/* Validate endpoint type. */
AssertMsg(pEpCtx->ep_type == XHCI_EPTYPE_ISOCH_IN || pEpCtx->ep_type == XHCI_EPTYPE_ISOCH_OUT,
("%#x\n", pEpCtx->ep_type));
/* Allocate and initialize a new URB. */
pUrb = VUSBIRhNewUrb(pRh->pIRhConn, uAddr, VUSB_DEVICE_PORT_INVALID, VUSBXFERTYPE_ISOC,
(pEpCtx->ep_type == XHCI_EPTYPE_ISOCH_IN) ? VUSBDIRECTION_IN : VUSBDIRECTION_OUT,
cbUrbMax, ctxProbe.cTRB, NULL);
if (!pUrb)
return VERR_OUT_OF_RESOURCES; /// @todo handle error!
STAM_COUNTER_ADD(&pThis->StatTRBsPerIsoUrb, ctxProbe.cTRB);
LogFlowFunc(("Allocated URB with %u packets, %u bytes total (ESIT payload %u)\n", cIsoPackets, cbUrbMax, cbPktMax));
pUrb->EndPt = uEpDCI / 2; /* DCI = EP * 2 + direction */
pUrb->fShortNotOk = false; /* We detect short packets ourselves. */
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->cIsocPkts = cIsoPackets;
pUrb->pHci->uSlotID = uSlotID;
pUrb->pHci->cTRB = ctxProbe.cTRB;
/* If TRB says so or if there are multiple packets per interval, don't even
* bother with frame counting and schedule everything ASAP.
*/
if (pTrb->isoc.sia || cIsoPackets != 1)
pUrb->uStartFrameDelta = 0;
else
{
uint16_t uFrameDelta;
uint32_t uPort;
/* Abort the endpoint, i.e. cancel any outstanding URBs. This needs to be done after
* writing back the EP state so that the completion callback can operate.
*/
if (RT_SUCCESS(xhciR3FindRhDevBySlot(pDevIns, pThis, pThisCC, uSlotID, NULL, &uPort)))
{
uFrameDelta = pRh->pIRhConn->pfnUpdateIsocFrameDelta(pRh->pIRhConn, uPort, uEpDCI / 2,
uEpDCI & 1 ? VUSBDIRECTION_IN : VUSBDIRECTION_OUT,
pTrb->isoc.frm_id, XHCI_FRAME_ID_BITS);
pUrb->uStartFrameDelta = uFrameDelta;
Log(("%s: Isoch frame delta set to %u\n", pUrb->pszDesc, uFrameDelta));
}
else
{
Log(("%s: Failed to find device for slot! Setting frame delta to zero.\n", pUrb->pszDesc));
pUrb->uStartFrameDelta = 0;
}
}
Log(("%s: Addr=%u, EndPt=%u, enmDir=%u cIsocPkts=%u cbData=%u FrmID=%u Isoch URB created\n",
pUrb->pszDesc, pUrb->DstAddress, pUrb->EndPt, pUrb->enmDir, pUrb->cIsocPkts, pUrb->cbData, pTrb->isoc.frm_id));
/* Set up the context for later use. */
pCtxIso->pUrb = pUrb;
/* Save the current TREP in case we need to rewind. */
pCtxIso->uInitTREP = pEpCtx->trep;
}
else
{
Assert(cIsoPackets > 1);
/* Grab the URB we initialized earlier. */
pUrb = pCtxIso->pUrb;
}
/* Set up the packet corresponding to this TD. */
pUrb->aIsocPkts[pCtxIso->iPkt].cb = RT_MIN(ctxProbe.uXferLen, cbPktMax);
pUrb->aIsocPkts[pCtxIso->iPkt].off = pCtxIso->offCur;
pUrb->aIsocPkts[pCtxIso->iPkt].enmStatus = VUSBSTATUS_NOT_ACCESSED;
/* For OUT transfers, copy the TD data into the URB. */
if (pUrb->enmDir == VUSBDIRECTION_OUT)
{
ctxSubmit.pUrb = pUrb;
ctxSubmit.uXferPos = pCtxIso->offCur;
ctxSubmit.cTRB = 0;
xhciR3WalkXferTrbChain(pDevIns, pThis, pEpCtx->trep, xhciR3WalkDataTRBsSubmit, &ctxSubmit, &uTREP);
Assert(ctxProbe.cTRB == ctxSubmit.cTRB);
}
/* Done preparing this packet. */
Assert(pCtxIso->iPkt < 8);
pCtxIso->iPkt++;
pCtxIso->offCur += ctxProbe.uXferLen;
Assert(pCtxIso->offCur <= pUrb->cbData);
/* Increment the in-flight counter before queuing more. */
if (pCtxIso->iPkt == pUrb->cIsocPkts)
pEpCtx->ifc++;
/* Commit the updated TREP; submitting the URB may already invoke completion callbacks. */
pEpCtx->trep = uTREP;
xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uEpDCI, pEpCtx);
/* If the URB is complete, submit it. */
if (pCtxIso->iPkt == pUrb->cIsocPkts)
{
/* Change cbData to reflect how much data should be transferred. This can differ
* from how much data was allocated for the URB.
*/
pUrb->cbData = pCtxIso->offCur;
STAM_COUNTER_ADD(&pThis->StatUrbSizeIsoc, pUrb->cbData);
Log(("%s: Addr=%u, EndPt=%u, enmDir=%u cIsocPkts=%u cbData=%u Isoch URB being submitted\n",
pUrb->pszDesc, pUrb->DstAddress, pUrb->EndPt, pUrb->enmDir, pUrb->cIsocPkts, pUrb->cbData));
RTCritSectLeave(&pThisCC->CritSectThrd);
int rc = VUSBIRhSubmitUrb(pRh->pIRhConn, pUrb, &pRh->Led);
RTCritSectEnter(&pThisCC->CritSectThrd);
if (RT_FAILURE(rc))
{
/* Failure cleanup. Can happen if we're still resetting the device or out of resources,
* or the user just ripped out the device.
*/
pCtxIso->fSubmitFailed = true;
/// @todo Mark the EP as halted and inactive and write back the changes.
return VERR_OUT_OF_RESOURCES;
}
/* Clear the isochronous URB context. */
RT_ZERO(*pCtxIso);
}
return VINF_SUCCESS;
}
/**
* Queue a control TD composed of setup/data/status stage TRBs, event data
* TRBs, and suchlike.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pThis The xHCI device state, shared edition.
* @param pThisCC The xHCI device state, ring-3 edition.
* @param pRh Root hub for the device.
* @param GCPhysTRB Physical guest address of th TRB.
* @param pTrb Pointer to the contents of the first TRB.
* @param pEpCtx Pointer to the cached EP context.
* @param uSlotID ID of the associated slot context.
* @param uAddr The device address.
* @param uEpDCI The DCI(!) of the endpoint.
*/
static int xhciR3QueueControlTD(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, PXHCIROOTHUBR3 pRh, RTGCPHYS GCPhysTRB,
XHCI_XFER_TRB *pTrb, XHCI_EP_CTX *pEpCtx, uint8_t uSlotID, uint8_t uAddr, uint8_t uEpDCI)
{
RT_NOREF(GCPhysTRB);
XHCI_CTX_XFER_PROBE ctxProbe;
XHCI_CTX_XFER_SUBMIT ctxSubmit;
uint64_t uTREP;
int rc;
VUSBDIRECTION enmDir;
/* Discover how big this TD is. */
RT_ZERO(ctxProbe);
rc = xhciR3WalkXferTrbChain(pDevIns, pThis, pEpCtx->trep, xhciR3WalkDataTRBsProbe, &ctxProbe, &uTREP);
if (RT_SUCCESS(rc))
LogFlowFunc(("Probed %u TRBs, %u bytes total, TREP@%RX64\n", ctxProbe.cTRB, ctxProbe.uXferLen, uTREP));
else
{
LogFlowFunc(("Probing failed after %u TRBs, %u bytes total (last ED after %u TRBs and %u bytes), TREP@%RX64\n", ctxProbe.cTRB, ctxProbe.uXferLen, ctxProbe.cTRBLastED, ctxProbe.uXfrLenLastED, uTREP));
return rc;
}
/* Determine the transfer direction. */
switch (pTrb->gen.type)
{
case XHCI_TRB_SETUP_STG:
enmDir = VUSBDIRECTION_SETUP;
/* For setup TRBs, there is always 8 bytes of immediate data. */
Assert(sizeof(VUSBSETUP) == 8);
Assert(ctxProbe.uXferLen == 8);
Log2(("bmRequestType:%02X bRequest:%02X wValue:%04X wIndex:%04X wLength:%04X\n", pTrb->setup.bmRequestType,
pTrb->setup.bRequest, pTrb->setup.wValue, pTrb->setup.wIndex, pTrb->setup.wLength));
break;
case XHCI_TRB_STATUS_STG:
enmDir = pTrb->status.dir ? VUSBDIRECTION_IN : VUSBDIRECTION_OUT;
break;
case XHCI_TRB_DATA_STG:
enmDir = pTrb->data.dir ? VUSBDIRECTION_IN : VUSBDIRECTION_OUT;
break;
default:
AssertMsgFailed(("%#x\n", pTrb->gen.type)); /* Can't happen unless caller messed up. */
return VERR_INTERNAL_ERROR;
}
/* Allocate and initialize a URB. */
PVUSBURB pUrb = VUSBIRhNewUrb(pRh->pIRhConn, uAddr, VUSB_DEVICE_PORT_INVALID, VUSBXFERTYPE_CTRL, enmDir, ctxProbe.uXferLen, ctxProbe.cTRB,
NULL);
if (!pUrb)
return VERR_OUT_OF_RESOURCES; /// @todo handle error!
STAM_COUNTER_ADD(&pThis->StatTRBsPerCtlUrb, ctxProbe.cTRB);
/* See 4.5.1 about xHCI vs. USB endpoint addressing. */
Assert(uEpDCI);
/* This had better be a control endpoint. */
AssertMsg(pEpCtx->ep_type == XHCI_EPTYPE_CONTROL, ("%#x\n", pEpCtx->ep_type));
pUrb->EndPt = uEpDCI / 2; /* DCI = EP * 2 + direction */
pUrb->fShortNotOk = false; /* We detect short packets ourselves. */
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->pHci->uSlotID = uSlotID;
/* For OUT/SETUP transfers, copy the TD data into the URB. */
if (pUrb->enmDir == VUSBDIRECTION_OUT || pUrb->enmDir == VUSBDIRECTION_SETUP)
{
ctxSubmit.pUrb = pUrb;
ctxSubmit.uXferPos = 0;
ctxSubmit.cTRB = 0;
xhciR3WalkXferTrbChain(pDevIns, pThis, pEpCtx->trep, xhciR3WalkDataTRBsSubmit, &ctxSubmit, &uTREP);
Assert(ctxProbe.cTRB == ctxSubmit.cTRB);
ctxProbe.cTRB = ctxSubmit.cTRB;
}
pUrb->pHci->cTRB = ctxProbe.cTRB;
/* Commit the updated TREP; submitting the URB may already invoke completion callbacks. */
pEpCtx->trep = uTREP;
xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uEpDCI, pEpCtx);
/*
* Submit the URB.
*/
STAM_COUNTER_ADD(&pThis->StatUrbSizeCtrl, pUrb->cbData);
Log(("%s: xhciR3QueueControlTD: Addr=%u, EndPt=%u, enmDir=%u cbData=%u\n",
pUrb->pszDesc, pUrb->DstAddress, pUrb->EndPt, pUrb->enmDir, pUrb->cbData));
RTCritSectLeave(&pThisCC->CritSectThrd);
rc = VUSBIRhSubmitUrb(pRh->pIRhConn, pUrb, &pRh->Led);
RTCritSectEnter(&pThisCC->CritSectThrd);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
/* Failure cleanup. Can happen if we're still resetting the device or out of resources,
* or the user just ripped out the device.
*/
/// @todo Mark the EP as halted and inactive and write back the changes.
return VERR_OUT_OF_RESOURCES;
}
/**
* Process a device context (transfer data).
*
* @param pDevIns The device instance.
* @param pThis The xHCI device state, shared edition.
* @param pThisCC The xHCI device state, ring-3 edition.
* @param uSlotID Slot/doorbell which had been rung.
* @param uDBVal Value written to the doorbell.
*/
static int xhciR3ProcessDevCtx(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, uint8_t uSlotID, uint32_t uDBVal)
{
uint8_t uDBTarget = uDBVal & XHCI_DB_TGT_MASK;
XHCI_CTX_ISOCH ctxIsoch = {0};
XHCI_SLOT_CTX slot_ctx;
XHCI_EP_CTX ep_ctx;
XHCI_XFER_TRB xfer;
RTGCPHYS GCPhysXfrTRB;
PXHCIROOTHUBR3 pRh;
bool dcs;
bool fContinue = true;
int rc;
unsigned cTrbs = 0;
LogFlowFunc(("Slot ID: %u, DB target %u, DB stream ID %u\n", uSlotID, uDBTarget, (uDBVal & XHCI_DB_STRMID_MASK) >> XHCI_DB_STRMID_SHIFT));
Assert(uSlotID > 0);
Assert(uSlotID <= XHCI_NDS);
/// @todo report errors for bogus DB targets
Assert(uDBTarget > 0);
Assert(uDBTarget < 32);
/// @todo Check for aborts and the like?
/* Load the slot and endpoint contexts. */
xhciR3FetchCtxAndEp(pDevIns, pThis, uSlotID, uDBTarget, &slot_ctx, &ep_ctx);
/// @todo sanity check the context in here?
/* Select the root hub corresponding to the port. */
pRh = GET_PORT_PRH(pThisCC, ID_TO_IDX(slot_ctx.rh_port));
/* Stopped endpoints automatically transition to running state. */
if (RT_UNLIKELY(ep_ctx.ep_state == XHCI_EPST_STOPPED))
{
Log(("EP DCI %u stopped -> running\n", uDBTarget));
ep_ctx.ep_state = XHCI_EPST_RUNNING;
/* Update EP right here. Theoretically could be postponed, but we
* must ensure that the EP does get written back even if there is
* no other work to do.
*/
xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uDBTarget, &ep_ctx);
}
/* If the EP isn't running, get outta here. */
if (RT_UNLIKELY(ep_ctx.ep_state != XHCI_EPST_RUNNING))
{
Log2(("EP DCI %u not running (state %u), bail!\n", uDBTarget, ep_ctx.ep_state));
return VINF_SUCCESS;
}
/* Get any non-transfer TRBs out of the way. */
xhciR3ConsumeNonXferTRBs(pDevIns, pThis, uSlotID, uDBTarget, &ep_ctx, &xfer, &GCPhysXfrTRB);
/// @todo This is inefficient.
xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uDBTarget, &ep_ctx);
do
{
/* Fetch the contexts again and find the TRB address at enqueue point. */
xhciR3FetchCtxAndEp(pDevIns, pThis, uSlotID, uDBTarget, &slot_ctx, &ep_ctx);
GCPhysXfrTRB = ep_ctx.trep & XHCI_TRDP_ADDR_MASK;
dcs = !!(ep_ctx.trep & XHCI_TRDP_DCS_MASK);
LogFlowFunc(("Processing Transfer Ring, TREP: %RGp\n", GCPhysXfrTRB));
/* Fetch the transfer TRB. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysXfrTRB, &xfer, sizeof(xfer));
/* Make sure the Cycle State matches. */
if ((bool)xfer.gen.cycle == dcs)
{
Log2(("TRB @ %RGp, type %u (%s) %u bytes ENT=%u ISP=%u NS=%u CH=%u IOC=%u IDT=%u\n", GCPhysXfrTRB, xfer.gen.type,
xfer.gen.type < RT_ELEMENTS(g_apszTrbNames) ? g_apszTrbNames[xfer.gen.type] : "WHAT?!!",
xfer.gen.xfr_len, xfer.gen.ent, xfer.gen.isp, xfer.gen.ns, xfer.gen.ch, xfer.gen.ioc, xfer.gen.idt));
/* If there is an "in-flight" TRDP, check if we need to wait until the transfer completes. */
if ((ep_ctx.trdp & XHCI_TRDP_ADDR_MASK) != GCPhysXfrTRB)
{
switch (xfer.gen.type) {
case XHCI_TRB_ISOCH:
if (ep_ctx.ifc >= XHCI_MAX_ISOC_IN_FLIGHT)
{
Log(("%u isoch URBs in flight, backing off\n", ep_ctx.ifc));
fContinue = false;
break;
}
RT_FALL_THRU();
case XHCI_TRB_LINK:
Log2(("TRB OK, continuing @ %RX64\n", GCPhysXfrTRB));
break;
case XHCI_TRB_NORMAL:
if (XHCI_EP_XTYPE(ep_ctx.ep_type) != XHCI_XFTYPE_BULK)
{
Log2(("Normal TRB not bulk, not continuing @ %RX64\n", GCPhysXfrTRB));
fContinue = false;
break;
}
if (ep_ctx.ifc >= XHCI_MAX_BULK_IN_FLIGHT)
{
Log(("%u normal URBs in flight, backing off\n", ep_ctx.ifc));
fContinue = false;
break;
}
Log2(("Bulk TRB OK, continuing @ %RX64\n", GCPhysXfrTRB));
break;
case XHCI_TRB_EVT_DATA:
case XHCI_TRB_NOOP_XFER:
Log2(("TRB not OK, not continuing @ %RX64\n", GCPhysXfrTRB));
fContinue = false;
break;
default:
Log2(("Some other TRB (type %u), not continuing @ %RX64\n", xfer.gen.type, GCPhysXfrTRB));
fContinue = false;
break;
}
}
if (!fContinue)
break;
switch (xfer.gen.type) {
case XHCI_TRB_NORMAL:
Log(("Normal TRB: Ptr=%RGp IOC=%u CH=%u\n", xfer.norm.data_ptr, xfer.norm.ioc, xfer.norm.ch));
rc = xhciR3QueueDataTD(pDevIns, pThis, pThisCC, pRh, GCPhysXfrTRB, &xfer, &ep_ctx, uSlotID,
slot_ctx.dev_addr, uDBTarget);
break;
case XHCI_TRB_SETUP_STG:
Log(("Setup stage TRB: IOC=%u IDT=%u\n", xfer.setup.ioc, xfer.setup.idt));
rc = xhciR3QueueControlTD(pDevIns, pThis, pThisCC, pRh, GCPhysXfrTRB, &xfer, &ep_ctx, uSlotID,
slot_ctx.dev_addr, uDBTarget);
break;
case XHCI_TRB_DATA_STG:
Log(("Data stage TRB: Ptr=%RGp IOC=%u CH=%u DIR=%u\n", xfer.data.data_ptr, xfer.data.ioc, xfer.data.ch, xfer.data.dir));
rc = xhciR3QueueControlTD(pDevIns, pThis, pThisCC, pRh, GCPhysXfrTRB, &xfer, &ep_ctx, uSlotID,
slot_ctx.dev_addr, uDBTarget);
break;
case XHCI_TRB_STATUS_STG:
Log(("Status stage TRB: IOC=%u CH=%u DIR=%u\n", xfer.status.ioc, xfer.status.ch, xfer.status.dir));
rc = xhciR3QueueControlTD(pDevIns, pThis, pThisCC, pRh, GCPhysXfrTRB, &xfer, &ep_ctx, uSlotID,
slot_ctx.dev_addr, uDBTarget);
break;
case XHCI_TRB_ISOCH:
Log(("Isoch TRB: Ptr=%RGp IOC=%u CH=%u TLBPC=%u TBC=%u SIA=%u FrmID=%u\n", xfer.isoc.data_ptr, xfer.isoc.ioc, xfer.isoc.ch, xfer.isoc.tlbpc, xfer.isoc.tbc, xfer.isoc.sia, xfer.isoc.frm_id));
rc = xhciR3QueueIsochTD(pDevIns, pThis, pThisCC, pRh, GCPhysXfrTRB, &xfer, &ep_ctx, uSlotID,
slot_ctx.dev_addr, uDBTarget, &ctxIsoch);
break;
case XHCI_TRB_LINK:
Log2(("Link extra-TD: Ptr=%RGp IOC=%u TC=%u CH=%u\n", xfer.link.rseg_ptr, xfer.link.ioc, xfer.link.toggle, xfer.link.chain));
Assert(!xfer.link.chain);
/* Set new TREP but leave DCS bit alone... */
ep_ctx.trep = (xfer.link.rseg_ptr & XHCI_TRDP_ADDR_MASK) | (ep_ctx.trep & XHCI_TRDP_DCS_MASK);
/* ...and flip the DCS bit if required. Then update the TREP. */
if (xfer.link.toggle)
ep_ctx.trep = (ep_ctx.trep & ~XHCI_TRDP_DCS_MASK) | (ep_ctx.trep ^ XHCI_TRDP_DCS_MASK);
rc = xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uDBTarget, &ep_ctx);
break;
case XHCI_TRB_NOOP_XFER:
Log2(("No op xfer: IOC=%u CH=%u ENT=%u\n", xfer.nop.ioc, xfer.nop.ch, xfer.nop.ent));
/* A no-op transfer TRB must not be part of a chain. See 4.11.7. */
Assert(!xfer.link.chain);
/* Update enqueue pointer (TRB was not yet completed). */
ep_ctx.trep += sizeof(XHCI_XFER_TRB);
rc = xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uDBTarget, &ep_ctx);
break;
default:
Log(("Unsupported TRB!!\n"));
rc = VERR_NOT_SUPPORTED;
break;
}
/* If queuing failed, stop right here. */
if (RT_FAILURE(rc))
fContinue = false;
}
else
{
LogFunc(("Transfer Ring empty\n"));
fContinue = false;
/* If an isochronous ring is empty, this is an overrun/underrun. At this point
* the ring will no longer be scheduled (until the doorbell is rung again)
* but it remains in the Running state. This error is only reported if someone
* rang the doorbell and there are no TDs available or in-flight.
*/
if ( (ep_ctx.trep == ep_ctx.trdp) /* Nothing in-flight? */
&& (ep_ctx.ep_type == XHCI_EPTYPE_ISOCH_IN || ep_ctx.ep_type == XHCI_EPTYPE_ISOCH_OUT))
{
/* There is no TRB associated with this error; the slot context
* determines the interrupter.
*/
Log(("Isochronous ring %s, TRDP:%RGp\n", ep_ctx.ep_type == XHCI_EPTYPE_ISOCH_IN ? "overrun" : "underrun", ep_ctx.trdp & XHCI_TRDP_ADDR_MASK));
rc = xhciR3PostXferEvent(pDevIns, pThis, slot_ctx.intr_tgt, 0,
ep_ctx.ep_type == XHCI_EPTYPE_ISOCH_IN ? XHCI_TCC_RING_OVERRUN : XHCI_TCC_RING_UNDERRUN,
uSlotID, uDBTarget, 0, false, false);
}
}
/* Kill the xHC if the TRB list has no end in sight. */
if (++cTrbs > XHCI_MAX_NUM_TRBS)
{
/* Stop the xHC with an error. */
xhciR3EndlessTrbError(pDevIns, pThis);
/* Get out of the loop. */
fContinue = false;
rc = VERR_NOT_SUPPORTED; /* No good error code really... */
}
} while (fContinue);
/* It can unfortunately happen that for endpoints with more than one
* transfer per USB frame, there won't be a complete multi-packet URB ready
* when we go looking for it. If that happens, we'll "rewind" the TREP and
* try again later. Since the URB construction is done under a lock, this
* is safe as we won't be accessing the endpoint concurrently.
*/
if (ctxIsoch.pUrb)
{
Log(("Unfinished ISOC URB (%u packets out of %u)!\n", ctxIsoch.iPkt, ctxIsoch.pUrb->cIsocPkts));
/* If submitting failed, the URB is already freed. */
if (!ctxIsoch.fSubmitFailed)
VUSBIRhFreeUrb(pRh->pIRhConn, ctxIsoch.pUrb);
ep_ctx.trep = ctxIsoch.uInitTREP;
xhciR3WriteBackEp(pDevIns, pThis, uSlotID, uDBTarget, &ep_ctx);
}
return VINF_SUCCESS;
}
/**
* A worker routine for Address Device command. Builds a URB containing
* a SET_ADDRESS requests and (synchronously) submits it to VUSB, then
* follows up with a status stage URB.
*
* @returns true on success.
* @returns false on failure to submit.
* @param pThisCC The xHCI device state, ring-3 edition.
* @param uSlotID Slot ID to assign address to.
* @param uDevAddr New device address.
* @param iPort The xHCI root hub port index.
*/
static bool xhciR3IssueSetAddress(PXHCICC pThisCC, uint8_t uSlotID, uint8_t uDevAddr, unsigned iPort)
{
PXHCIROOTHUBR3 pRh = GET_PORT_PRH(pThisCC, iPort);
Assert(uSlotID);
LogFlowFunc(("Slot %u port idx %u: new address is %u\n", uSlotID, iPort, uDevAddr));
/* For USB3 devices, force the port number. This simulates the fact that USB3 uses directed (unicast) traffic. */
if (!IS_USB3_PORT_IDX_R3(pThisCC, iPort))
iPort = VUSB_DEVICE_PORT_INVALID;
else
iPort = GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort);
/* Allocate and initialize a URB. NB: Zero cTds indicates a URB not submitted by guest. */
PVUSBURB pUrb = VUSBIRhNewUrb(pRh->pIRhConn, 0 /* address */, iPort, VUSBXFERTYPE_CTRL, VUSBDIRECTION_SETUP,
sizeof(VUSBSETUP), 0 /* cTds */, NULL);
if (!pUrb)
return false;
pUrb->EndPt = 0;
pUrb->fShortNotOk = true;
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->pHci->uSlotID = uSlotID;
pUrb->pHci->cTRB = 0;
/* Build the request. */
PVUSBSETUP pSetup = (PVUSBSETUP)pUrb->abData;
pSetup->bmRequestType = VUSB_DIR_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_TO_DEVICE;
pSetup->bRequest = VUSB_REQ_SET_ADDRESS;
pSetup->wValue = uDevAddr;
pSetup->wIndex = 0;
pSetup->wLength = 0;
/* NB: We assume the address assignment is a synchronous operation. */
/* Submit the setup URB. */
Log(("%s: xhciSetAddress setup: cbData=%u\n", pUrb->pszDesc, pUrb->cbData));
RTCritSectLeave(&pThisCC->CritSectThrd);
int rc = VUSBIRhSubmitUrb(pRh->pIRhConn, pUrb, &pRh->Led);
RTCritSectEnter(&pThisCC->CritSectThrd);
if (RT_FAILURE(rc))
{
Log(("xhciSetAddress: setup stage failed pUrb=%p!!\n", pUrb));
return false;
}
/* To complete the SET_ADDRESS request, the status stage must succeed. */
pUrb = VUSBIRhNewUrb(pRh->pIRhConn, 0 /* address */, iPort, VUSBXFERTYPE_CTRL, VUSBDIRECTION_IN, 0 /* cbData */, 0 /* cTds */,
NULL);
if (!pUrb)
return false;
pUrb->EndPt = 0;
pUrb->fShortNotOk = true;
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->pHci->uSlotID = uSlotID;
pUrb->pHci->cTRB = 0;
/* Submit the setup URB. */
Log(("%s: xhciSetAddress status: cbData=%u\n", pUrb->pszDesc, pUrb->cbData));
RTCritSectLeave(&pThisCC->CritSectThrd);
rc = VUSBIRhSubmitUrb(pRh->pIRhConn, pUrb, &pRh->Led);
RTCritSectEnter(&pThisCC->CritSectThrd);
if (RT_FAILURE(rc))
{
Log(("xhciSetAddress: status stage failed pUrb=%p!!\n", pUrb));
return false;
}
Log(("xhciSetAddress: set address succeeded\n"));
return true;
}
/**
* Address a device.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis The xHCI device state, shared edition.
* @param pThisCC The xHCI device state, ring-3 edition.
* @param uInpCtxAddr Address of the input context.
* @param uSlotID Slot ID to assign address to.
* @param fBSR Block Set address Request flag.
*/
static unsigned xhciR3AddressDevice(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, uint64_t uInpCtxAddr,
uint8_t uSlotID, bool fBSR)
{
RTGCPHYS GCPhysInpCtx = uInpCtxAddr & XHCI_CTX_ADDR_MASK;
RTGCPHYS GCPhysInpSlot;
RTGCPHYS GCPhysOutSlot;
XHCI_INPC_CTX icc; /* Input Control Context (ICI=0). */
XHCI_SLOT_CTX inp_slot_ctx; /* Input Slot Context (ICI=1). */
XHCI_EP_CTX ep_ctx; /* Endpoint Context (ICI=2+). */
XHCI_SLOT_CTX out_slot_ctx; /* Output Slot Context. */
uint8_t dev_addr;
unsigned cc = XHCI_TCC_SUCCESS;
Assert(GCPhysInpCtx);
Assert(uSlotID);
LogFlowFunc(("Slot ID %u, input control context @ %RGp\n", uSlotID, GCPhysInpCtx));
/* Determine the address of the output slot context. */
GCPhysOutSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
/* Fetch the output slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysOutSlot, &out_slot_ctx, sizeof(out_slot_ctx));
/// @todo Check for valid context (6.2.2.1, 6.2.3.1)
/* See 4.6.5 */
do {
/* Parameter validation depends on whether the BSR flag is set or not. */
if (fBSR)
{
/* Check that the output slot context state is in Enabled state. */
if (out_slot_ctx.slot_state >= XHCI_SLTST_DEFAULT)
{
Log(("Output slot context state (%u) wrong (BSR)!\n", out_slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
dev_addr = 0;
}
else
{
/* Check that the output slot context state is in Enabled or Default state. */
if (out_slot_ctx.slot_state > XHCI_SLTST_DEFAULT)
{
Log(("Output slot context state (%u) wrong (no-BSR)!\n", out_slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
dev_addr = xhciR3SelectNewAddress(pThis, uSlotID);
}
/* Fetch the input control context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysInpCtx, &icc, sizeof(icc));
Assert(icc.add_flags == (RT_BIT(0) | RT_BIT(1))); /* Should have been already checked. */
Assert(!icc.drop_flags);
/* Calculate the address of the input slot context (ICI=1/DCI=0). */
GCPhysInpSlot = GCPhysInpCtx + sizeof(XHCI_INPC_CTX);
/* Read the input slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysInpSlot, &inp_slot_ctx, sizeof(inp_slot_ctx));
/* If BSR isn't set, issue the actual SET_ADDRESS request. */
if (!fBSR) {
unsigned iPort;
/* We have to dig out the port number/index to determine which virtual root hub to use. */
iPort = ID_TO_IDX(inp_slot_ctx.rh_port);
if (iPort >= XHCI_NDP_CFG(pThis))
{
Log(("Port out of range (index %u)!\n", iPort));
cc = XHCI_TCC_USB_XACT_ERR;
break;
}
if (!xhciR3IssueSetAddress(pThisCC, uSlotID, dev_addr, iPort))
{
Log(("SET_ADDRESS failed!\n"));
cc = XHCI_TCC_USB_XACT_ERR;
break;
}
}
/* Copy the slot context with appropriate modifications. */
out_slot_ctx = inp_slot_ctx;
if (fBSR)
out_slot_ctx.slot_state = XHCI_SLTST_DEFAULT;
else
out_slot_ctx.slot_state = XHCI_SLTST_ADDRESSED;
out_slot_ctx.dev_addr = dev_addr;
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysOutSlot, &out_slot_ctx, sizeof(out_slot_ctx));
/* Point at the EP0 contexts. */
GCPhysInpSlot += sizeof(inp_slot_ctx);
GCPhysOutSlot += sizeof(out_slot_ctx);
/* Copy EP0 context with appropriate modifications. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysInpSlot, &ep_ctx, sizeof(ep_ctx));
xhciR3EnableEP(&ep_ctx);
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysOutSlot, &ep_ctx, sizeof(ep_ctx));
} while (0);
return cc;
}
/**
* Reset a halted endpoint.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis Pointer to the xHCI state.
* @param uSlotID Slot ID to work with.
* @param uDCI DCI of the endpoint to reset.
* @param fTSP The Transfer State Preserve flag.
*/
static unsigned xhciR3ResetEndpoint(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID, uint8_t uDCI, bool fTSP)
{
RT_NOREF(fTSP);
RTGCPHYS GCPhysSlot;
RTGCPHYS GCPhysEndp;
XHCI_SLOT_CTX slot_ctx;
XHCI_EP_CTX endp_ctx;
unsigned cc = XHCI_TCC_SUCCESS;
Assert(uSlotID);
/* Determine the addresses of the contexts. */
GCPhysSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
GCPhysEndp = GCPhysSlot + uDCI * sizeof(XHCI_EP_CTX);
/* Fetch the slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysSlot, &slot_ctx, sizeof(slot_ctx));
/* See 4.6.8 */
do {
/* Check that the slot context state is Default, Addressed, or Configured. */
if (slot_ctx.slot_state < XHCI_SLTST_DEFAULT)
{
Log(("Slot context state wrong (%u)!\n", slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Fetch the endpoint context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
/* Check that the endpoint context state is Halted. */
if (endp_ctx.ep_state != XHCI_EPST_HALTED)
{
Log(("Endpoint context state wrong (%u)!\n", endp_ctx.ep_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Transition EP state. */
endp_ctx.ep_state = XHCI_EPST_STOPPED;
/// @todo What can we do with the TSP flag?
/// @todo Anything to do WRT enabling the corresponding doorbell register?
/* Write back the updated endpoint context. */
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
} while (0);
return cc;
}
/**
* Stop a running endpoint.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis The xHCI device state, shared edition.
* @param pThisCC The xHCI device state, ring-3 edition.
* @param uSlotID Slot ID to work with.
* @param uDCI DCI of the endpoint to stop.
* @param fTSP The Suspend flag.
*/
static unsigned xhciR3StopEndpoint(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, uint8_t uSlotID, uint8_t uDCI, bool fTSP)
{
RT_NOREF(fTSP);
RTGCPHYS GCPhysSlot;
RTGCPHYS GCPhysEndp;
XHCI_SLOT_CTX slot_ctx;
XHCI_EP_CTX endp_ctx;
unsigned cc = XHCI_TCC_SUCCESS;
Assert(uSlotID);
/* Determine the addresses of the contexts. */
GCPhysSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
GCPhysEndp = GCPhysSlot + uDCI * sizeof(XHCI_EP_CTX);
/* Fetch the slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysSlot, &slot_ctx, sizeof(slot_ctx));
/* See 4.6.9 */
do {
/* Check that the slot context state is Default, Addressed, or Configured. */
if (slot_ctx.slot_state < XHCI_SLTST_DEFAULT)
{
Log(("Slot context state wrong (%u)!\n", slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* The doorbell could be ringing; stop it if so. */
if (pThis->aBellsRung[ID_TO_IDX(uSlotID)] & (1 << uDCI))
{
Log(("Unring bell for slot ID %u, DCI %u\n", uSlotID, uDCI));
ASMAtomicAndU32(&pThis->aBellsRung[ID_TO_IDX(uSlotID)], ~(1 << uDCI));
}
/* Fetch the endpoint context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
/* Check that the endpoint context state is Running. */
if (endp_ctx.ep_state != XHCI_EPST_RUNNING)
{
Log(("Endpoint context state wrong (%u)!\n", endp_ctx.ep_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Transition EP state. */
endp_ctx.ep_state = XHCI_EPST_STOPPED;
/* Write back the updated endpoint context *now*, before actually canceling anyhing. */
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
/// @todo What can we do with the SP flag?
PXHCIROOTHUBR3 pRh;
uint32_t uPort;
/* Abort the endpoint, i.e. cancel any outstanding URBs. This needs to be done after
* writing back the EP state so that the completion callback can operate.
*/
if (RT_SUCCESS(xhciR3FindRhDevBySlot(pDevIns, pThis, pThisCC, uSlotID, &pRh, &uPort)))
{
/* Temporarily give up the lock so that the completion callbacks can run. */
RTCritSectLeave(&pThisCC->CritSectThrd);
Log(("Aborting DCI %u -> ep=%u d=%u\n", uDCI, uDCI / 2, uDCI & 1 ? VUSBDIRECTION_IN : VUSBDIRECTION_OUT));
pRh->pIRhConn->pfnAbortEp(pRh->pIRhConn, uPort, uDCI / 2, uDCI & 1 ? VUSBDIRECTION_IN : VUSBDIRECTION_OUT);
RTCritSectEnter(&pThisCC->CritSectThrd);
}
/// @todo The completion callbacks should do more work for canceled URBs.
/* Once the completion callbacks had a chance to run, we have to adjust
* the endpoint state.
* NB: The guest may just ring the doorbell to continue and not execute
* 'Set TRDP' after stopping the endpoint.
*/
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
bool fXferWasInProgress = endp_ctx.trep != endp_ctx.trdp;
/* Reset the TREP, but the EDTLA should be left alone. */
endp_ctx.trep = endp_ctx.trdp;
if (fXferWasInProgress)
{
/* Fetch the transfer TRB to see the length. */
RTGCPHYS GCPhysXfrTRB = endp_ctx.trdp & XHCI_TRDP_ADDR_MASK;
XHCI_XFER_TRB XferTRB;
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysXfrTRB, &XferTRB, sizeof(XferTRB));
xhciR3PostXferEvent(pDevIns, pThis, slot_ctx.intr_tgt, XferTRB.gen.xfr_len, XHCI_TCC_STOPPED, uSlotID, uDCI,
GCPhysXfrTRB, false, false);
}
else
{
/* We need to generate a Force Stopped Event or FSE. Note that FSEs were optional
* in xHCI 0.96 but aren't in 1.0.
*/
xhciR3PostXferEvent(pDevIns, pThis, slot_ctx.intr_tgt, 0, XHCI_TCC_STP_INV_LEN, uSlotID, uDCI,
endp_ctx.trdp & XHCI_TRDP_ADDR_MASK, false, false);
}
/* Write back the updated endpoint context again. */
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
} while (0);
return cc;
}
/**
* Set a new TR Dequeue Pointer for an endpoint.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis Pointer to the xHCI state.
* @param uSlotID Slot ID to work with.
* @param uDCI DCI of the endpoint to reset.
* @param uTRDP The TRDP including DCS/ flag.
*/
static unsigned xhciR3SetTRDP(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID, uint8_t uDCI, uint64_t uTRDP)
{
RTGCPHYS GCPhysSlot;
RTGCPHYS GCPhysEndp;
XHCI_SLOT_CTX slot_ctx;
XHCI_EP_CTX endp_ctx;
unsigned cc = XHCI_TCC_SUCCESS;
Assert(uSlotID);
/* Determine the addresses of the contexts. */
GCPhysSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
GCPhysEndp = GCPhysSlot + uDCI * sizeof(XHCI_EP_CTX);
/* Fetch the slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysSlot, &slot_ctx, sizeof(slot_ctx));
/* See 4.6.10 */
do {
/* Check that the slot context state is Default, Addressed, or Configured. */
if (slot_ctx.slot_state < XHCI_SLTST_DEFAULT)
{
Log(("Slot context state wrong (%u)!\n", slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Fetch the endpoint context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
/* Check that the endpoint context state is Stopped or Error. */
if (endp_ctx.ep_state != XHCI_EPST_STOPPED && endp_ctx.ep_state != XHCI_EPST_ERROR)
{
Log(("Endpoint context state wrong (%u)!\n", endp_ctx.ep_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Update the TRDP/TREP and DCS. */
endp_ctx.trdp = uTRDP;
endp_ctx.trep = uTRDP;
/* Also clear the in-flight counter! */
endp_ctx.ifc = 0;
/// @todo Handle streams
/* Write back the updated endpoint context. */
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysEndp, &endp_ctx, sizeof(endp_ctx));
} while (0);
return cc;
}
/**
* Prepare for a device reset.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis Pointer to the xHCI state.
* @param uSlotID Slot ID to work with.
*/
static unsigned xhciR3ResetDevice(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uSlotID)
{
RTGCPHYS GCPhysSlot;
XHCI_SLOT_CTX slot_ctx;
XHCI_DEV_CTX dc;
unsigned num_ctx;
unsigned i;
unsigned cc = XHCI_TCC_SUCCESS;
Assert(uSlotID);
/* Determine the address of the slot/device context. */
GCPhysSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
/* Fetch the slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysSlot, &slot_ctx, sizeof(slot_ctx));
/* See 4.6.11. */
do {
/* Check that the slot context state is Addressed or Configured. */
if (slot_ctx.slot_state < XHCI_SLTST_ADDRESSED)
{
Log(("Slot context state wrong (%u)!\n", slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Read the entire Device Context. */
num_ctx = slot_ctx.ctx_ent + 1; /* Slot context plus EPs. */
Assert(num_ctx);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysSlot, &dc, num_ctx * sizeof(XHCI_SLOT_CTX));
/// @todo Abort any outstanding transfers!
/* Set slot state to Default and reset the USB device address. */
dc.entry[0].sc.slot_state = XHCI_SLTST_DEFAULT;
dc.entry[0].sc.dev_addr = 0;
/* Disable all endpoints except for EP 0 (aka DCI 1). */
for (i = 2; i < num_ctx; ++i)
dc.entry[i].ep.ep_state = XHCI_EPST_DISABLED;
/* Write back the updated device context. */
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysSlot, &dc, num_ctx * sizeof(XHCI_SLOT_CTX));
} while (0);
return cc;
}
/**
* Configure a device (even though the relevant command is called 'Configure
* Endpoint'. This includes adding/dropping endpoint contexts as directed by
* the input control context bits.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis Pointer to the xHCI state.
* @param uInpCtxAddr Address of the input context.
* @param uSlotID Slot ID associated with the context.
* @param fDC Deconfigure flag set (input context unused).
*/
static unsigned xhciR3ConfigureDevice(PPDMDEVINS pDevIns, PXHCI pThis, uint64_t uInpCtxAddr, uint8_t uSlotID, bool fDC)
{
RTGCPHYS GCPhysInpCtx = uInpCtxAddr & XHCI_CTX_ADDR_MASK;
RTGCPHYS GCPhysInpSlot;
RTGCPHYS GCPhysOutSlot;
RTGCPHYS GCPhysOutEndp;
XHCI_INPC_CTX icc; /* Input Control Context (ICI=0). */
XHCI_SLOT_CTX out_slot_ctx; /* Slot context (DCI=0). */
XHCI_EP_CTX out_endp_ctx; /* Endpoint Context (DCI=1). */
unsigned cc = XHCI_TCC_SUCCESS;
uint32_t uAddFlags;
uint32_t uDropFlags;
unsigned num_inp_ctx;
unsigned num_out_ctx;
XHCI_DEV_CTX dc_inp;
XHCI_DEV_CTX dc_out;
unsigned uDCI;
Assert(uSlotID);
LogFlowFunc(("Slot ID %u, input control context @ %RGp\n", uSlotID, GCPhysInpCtx));
/* Determine the address of the output slot context. */
GCPhysOutSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
Assert(GCPhysOutSlot);
/* Fetch the output slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysOutSlot, &out_slot_ctx, sizeof(out_slot_ctx));
/* See 4.6.6 */
do {
/* Check that the output slot context state is Addressed, or Configured. */
if (out_slot_ctx.slot_state < XHCI_SLTST_ADDRESSED)
{
Log(("Output slot context state wrong (%u)!\n", out_slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Check for deconfiguration request. */
if (fDC) {
if (out_slot_ctx.slot_state == XHCI_SLTST_CONFIGURED) {
/* Disable all enabled endpoints. */
uDropFlags = 0xFFFFFFFC; /** @todo r=bird: Why do you set uDropFlags and uAddFlags in a code path that doesn't use
* them? This is a _very_ difficult function to get the hang of the way it's written.
* Stuff like this looks like there's a control flow flaw (as to the do-break-while-false
* loop which doesn't do any clean up or logging at the end and seems only sever the very
* dubious purpose of making sure ther's only one return statement). The insistance on
* C-style variable declarations (top of function), makes checking state harder, which is
* why it's discouraged. */
uAddFlags = 0;
/* Start with EP1. */
GCPhysOutEndp = GCPhysOutSlot + sizeof(XHCI_SLOT_CTX) + sizeof(XHCI_EP_CTX);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysOutEndp, &out_endp_ctx, sizeof(out_endp_ctx));
out_endp_ctx.ep_state = XHCI_EPST_DISABLED;
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysOutEndp, &out_endp_ctx, sizeof(out_endp_ctx));
GCPhysOutEndp += sizeof(XHCI_EP_CTX); /* Point to the next EP context. */
/* Finally update the output slot context. */
out_slot_ctx.ctx_ent = 1; /* Only EP0 left. */
out_slot_ctx.slot_state = XHCI_SLTST_ADDRESSED;
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysOutSlot, &out_slot_ctx, sizeof(out_slot_ctx));
LogFlow(("Setting Output Slot State to Addressed, Context Entries = %u\n", out_slot_ctx.ctx_ent));
}
else
/* NB: Attempts to deconfigure a slot in Addressed state are ignored. */
Log(("Ignoring attempt to deconfigure slot in Addressed state!\n"));
break;
}
/* Fetch the input control context. */
Assert(GCPhysInpCtx);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysInpCtx, &icc, sizeof(icc));
Assert(icc.add_flags || icc.drop_flags); /* Make sure there's something to do. */
uAddFlags = icc.add_flags;
uDropFlags = icc.drop_flags;
LogFlowFunc(("Add Flags=%08X, Drop Flags=%08X\n", uAddFlags, uDropFlags));
/* If and only if any 'add context' flag is set, fetch the corresponding
* input device context.
*/
if (uAddFlags) {
/* Calculate the address of the input slot context (ICI=1/DCI=0). */
GCPhysInpSlot = GCPhysInpCtx + sizeof(XHCI_INPC_CTX);
/* Read the input Slot Context plus all Endpoint Contexts up to and
* including the one with the highest 'add' bit set.
*/
num_inp_ctx = ASMBitLastSetU32(uAddFlags);
Assert(num_inp_ctx);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysInpSlot, &dc_inp, num_inp_ctx * sizeof(XHCI_DS_ENTRY));
/// @todo Check that the highest set add flag isn't beyond input slot Context Entries
/// @todo Check input slot context according to 6.2.2.2
/// @todo Check input EP contexts according to 6.2.3.2
}
/** @todo r=bird: Looks like MSC is right that dc_inp can be used uninitalized.
*
* However, this function is so hard to read I'm leaving the exorcism of it to
* the author and just zeroing it in the mean time.
*
*/
else
RT_ZERO(dc_inp);
/* Read the output Slot Context plus all Endpoint Contexts up to and
* including the one with the highest 'add' or 'drop' bit set.
*/
num_out_ctx = ASMBitLastSetU32(uAddFlags | uDropFlags);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysOutSlot, &dc_out, num_out_ctx * sizeof(XHCI_DS_ENTRY));
/* Drop contexts as directed by flags. */
for (uDCI = 2; uDCI < 32; ++uDCI)
{
if (!((1 << uDCI) & uDropFlags))
continue;
Log2(("Dropping EP DCI %u\n", uDCI));
dc_out.entry[uDCI].ep.ep_state = XHCI_EPST_DISABLED;
/// @todo Do we need to bother tracking resources/bandwidth?
}
/* Now add contexts as directed by flags. */
for (uDCI = 2; uDCI < 32; ++uDCI)
{
if (!((1 << uDCI) & uAddFlags))
continue;
Assert(!fDC);
/* Copy over EP context, set to running. */
Log2(("Adding EP DCI %u\n", uDCI));
dc_out.entry[uDCI].ep = dc_inp.entry[uDCI].ep;
xhciR3EnableEP(&dc_out.entry[uDCI].ep);
/// @todo Do we need to bother tracking resources/bandwidth?
}
/* Finally update the device context. */
if (fDC || dc_inp.entry[0].sc.ctx_ent == 1)
{
dc_out.entry[0].sc.slot_state = XHCI_SLTST_ADDRESSED;
dc_out.entry[0].sc.ctx_ent = 1;
LogFlow(("Setting Output Slot State to Addressed\n"));
}
else
{
uint32_t uKillFlags = uDropFlags & ~uAddFlags; /* Endpoints going away. */
/* At least one EP enabled. Update Context Entries and state. */
Assert(dc_inp.entry[0].sc.ctx_ent);
dc_out.entry[0].sc.slot_state = XHCI_SLTST_CONFIGURED;
if (ID_TO_IDX(ASMBitLastSetU32(uAddFlags)) > dc_out.entry[0].sc.ctx_ent)
{
/* Adding new endpoints. */
dc_out.entry[0].sc.ctx_ent = ID_TO_IDX(ASMBitLastSetU32(uAddFlags));
}
else if (ID_TO_IDX(ASMBitLastSetU32(uKillFlags)) == dc_out.entry[0].sc.ctx_ent)
{
/* Removing the last endpoint, find the last non-disabled one. */
unsigned num_ctx_ent;
Assert(dc_out.entry[0].sc.ctx_ent + 1u == num_out_ctx);
for (num_ctx_ent = dc_out.entry[0].sc.ctx_ent; num_ctx_ent > 1; --num_ctx_ent)
if (dc_out.entry[num_ctx_ent].ep.ep_state != XHCI_EPST_DISABLED)
break;
dc_out.entry[0].sc.ctx_ent = num_ctx_ent; /* Last valid index to be precise. */
}
LogFlow(("Setting Output Slot State to Configured, Context Entries = %u\n", dc_out.entry[0].sc.ctx_ent));
}
/* If there were no errors, write back the updated output context. */
LogFlow(("Success, updating Output Context @ %RGp\n", GCPhysOutSlot));
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysOutSlot, &dc_out, num_out_ctx * sizeof(XHCI_DS_ENTRY));
} while (0);
return cc;
}
/**
* Evaluate an input context. This involves modifying device and endpoint
* contexts as directed by the input control context add bits.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis Pointer to the xHCI state.
* @param uInpCtxAddr Address of the input context.
* @param uSlotID Slot ID associated with the context.
*/
static unsigned xhciR3EvalContext(PPDMDEVINS pDevIns, PXHCI pThis, uint64_t uInpCtxAddr, uint8_t uSlotID)
{
RTGCPHYS GCPhysInpCtx = uInpCtxAddr & XHCI_CTX_ADDR_MASK;
RTGCPHYS GCPhysInpSlot;
RTGCPHYS GCPhysOutSlot;
XHCI_INPC_CTX icc; /* Input Control Context (ICI=0). */
XHCI_SLOT_CTX out_slot_ctx; /* Slot context (DCI=0). */
unsigned cc = XHCI_TCC_SUCCESS;
uint32_t uAddFlags;
uint32_t uDropFlags;
unsigned num_inp_ctx;
unsigned num_out_ctx;
XHCI_DEV_CTX dc_inp;
XHCI_DEV_CTX dc_out;
unsigned uDCI;
Assert(GCPhysInpCtx);
Assert(uSlotID);
LogFlowFunc(("Slot ID %u, input control context @ %RGp\n", uSlotID, GCPhysInpCtx));
/* Determine the address of the output slot context. */
GCPhysOutSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
Assert(GCPhysOutSlot);
/* Fetch the output slot context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysOutSlot, &out_slot_ctx, sizeof(out_slot_ctx));
/* See 4.6.7 */
do {
/* Check that the output slot context state is Default, Addressed, or Configured. */
if (out_slot_ctx.slot_state < XHCI_SLTST_DEFAULT)
{
Log(("Output slot context state wrong (%u)!\n", out_slot_ctx.slot_state));
cc = XHCI_TCC_CTX_STATE_ERR;
break;
}
/* Fetch the input control context. */
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysInpCtx, &icc, sizeof(icc));
uAddFlags = icc.add_flags;
uDropFlags = icc.drop_flags;
LogFlowFunc(("Add Flags=%08X, Drop Flags=%08X\n", uAddFlags, uDropFlags));
/* Drop flags "shall be cleared to 0" but also "do not apply" (4.6.7). Log & ignore. */
if (uDropFlags)
Log(("Drop flags set (%X) for evaluating context!\n", uDropFlags));
/* If no add flags are set, nothing will be done but an error is not reported
* according to the logic flow in 4.6.7.
*/
if (!uAddFlags)
{
Log(("Warning: no add flags set for evaluating context!\n"));
break;
}
/* Calculate the address of the input slot context (ICI=1/DCI=0). */
GCPhysInpSlot = GCPhysInpCtx + sizeof(XHCI_INPC_CTX);
/* Read the output Slot Context plus all Endpoint Contexts up to and
* including the one with the highest 'add' bit set.
*/
num_inp_ctx = ASMBitLastSetU32(uAddFlags);
Assert(num_inp_ctx);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysInpSlot, &dc_inp, num_inp_ctx * sizeof(XHCI_DS_ENTRY));
/* Read the output Slot Context plus all Endpoint Contexts up to and
* including the one with the highest 'add' bit set.
*/
num_out_ctx = ASMBitLastSetU32(uAddFlags);
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysOutSlot, &dc_out, num_out_ctx * sizeof(XHCI_DS_ENTRY));
/// @todo Check input slot context according to 6.2.2.3
/// @todo Check input EP contexts according to 6.2.3.3
/// @todo Check that the highest set add flag isn't beyond input slot Context Entries
/* Evaluate endpoint contexts as directed by add flags. */
/// @todo 6.2.3.3 suggests only the A1 bit matters? Anything besides A0/A1 is ignored??
for (uDCI = 1; uDCI < 32; ++uDCI)
{
if (!((1 << uDCI) & uAddFlags))
continue;
/* Evaluate Max Packet Size. */
LogFunc(("DCI %u: Max Packet Size: %u -> %u\n", uDCI, dc_out.entry[uDCI].ep.max_pkt_sz, dc_inp.entry[uDCI].ep.max_pkt_sz));
dc_out.entry[uDCI].ep.max_pkt_sz = dc_inp.entry[uDCI].ep.max_pkt_sz;
}
/* Finally update the device context if directed to do so (A0 flag set). */
if (uAddFlags & RT_BIT(0))
{
/* 6.2.2.3 - evaluate Interrupter Target and Max Exit Latency. */
Log(("Interrupter Target: %u -> %u\n", dc_out.entry[0].sc.intr_tgt, dc_inp.entry[0].sc.intr_tgt));
Log(("Max Exit Latency : %u -> %u\n", dc_out.entry[0].sc.max_lat, dc_inp.entry[0].sc.max_lat));
/// @todo Non-zero Max Exit Latency (see 4.6.7)
dc_out.entry[0].sc.intr_tgt = dc_inp.entry[0].sc.intr_tgt;
dc_out.entry[0].sc.max_lat = dc_inp.entry[0].sc.max_lat;
}
/* If there were no errors, write back the updated output context. */
LogFlow(("Success, updating Output Context @ %RGp\n", GCPhysOutSlot));
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysOutSlot, &dc_out, num_out_ctx * sizeof(XHCI_DS_ENTRY));
} while (0);
return cc;
}
/**
* Query available port bandwidth.
*
* @returns TRB completion code.
* @param pDevIns The device instance.
* @param pThis Pointer to the xHCI state.
* @param uDevSpd Speed of not yet attached devices.
* @param uHubSlotID Hub Slot ID to query (unsupported).
* @param uBwCtx Bandwidth context physical address.
*/
static unsigned xhciR3GetPortBandwidth(PPDMDEVINS pDevIns, PXHCI pThis, uint8_t uDevSpd, uint8_t uHubSlotID, uint64_t uBwCtx)
{
RT_NOREF(uHubSlotID);
RTGCPHYS GCPhysBwCtx;
unsigned cc = XHCI_TCC_SUCCESS;
unsigned ctx_size;
unsigned iPort;
uint8_t bw_ctx[RT_ALIGN_32(XHCI_NDP_MAX + 1, 4)] = {0};
uint8_t dev_spd;
uint8_t avail_bw;
Assert(!uHubSlotID);
Assert(uBwCtx);
/* See 4.6.15. */
/* Hubs are not supported because guests will never see them. The
* reported values are more or less dummy because we have no real
* information about the bandwidth available on the host. The reported
* values are optimistic, as if each port had its own separate Bus
* Instance aka BI.
*/
GCPhysBwCtx = uBwCtx & XHCI_CTX_ADDR_MASK;
/* Number of ports + 1, rounded up to DWORDs. */
ctx_size = RT_ALIGN_32(XHCI_NDP_CFG(pThis) + 1, 4);
LogFlowFunc(("BW Context at %RGp, size %u\n", GCPhysBwCtx, ctx_size));
Assert(ctx_size <= sizeof(bw_ctx));
/* Go over all the ports. */
for (iPort = 0; iPort < XHCI_NDP_CFG(pThis); ++iPort)
{
/* Get the device speed from the port... */
dev_spd = (pThis->aPorts[iPort].portsc & XHCI_PORT_PLS_MASK) >> XHCI_PORT_PLS_SHIFT;
/* ...and if nothing is attached, use the provided default. */
if (!dev_spd)
dev_spd = uDevSpd;
/* For USB3 ports, report 90% available for SS devices (see 6.2.6). */
if (IS_USB3_PORT_IDX_SHR(pThis, iPort))
avail_bw = dev_spd == XHCI_SPD_SUPER ? 90 : 0;
else
/* For USB2 ports, report 80% available for HS and 90% for FS/LS. */
switch (dev_spd)
{
case XHCI_SPD_HIGH:
avail_bw = 80;
break;
case XHCI_SPD_FULL:
case XHCI_SPD_LOW:
avail_bw = 90;
break;
default:
avail_bw = 0;
}
/* The first entry in the context is reserved. */
bw_ctx[iPort + 1] = avail_bw;
}
/* Write back the bandwidth context. */
PDMDevHlpPCIPhysWriteMeta(pDevIns, GCPhysBwCtx, &bw_ctx, ctx_size);
return cc;
}
#define NEC_MAGIC ('x' | ('H' << 8) | ('C' << 16) | ('I' << 24))
/**
* Take a 64-bit input, shake well, produce 32-bit token. This mechanism
* prevents NEC/Renesas drivers from running on 3rd party hardware. Mirrors
* code found in vendor's drivers.
*/
static uint32_t xhciR3NecAuthenticate(uint64_t cookie)
{
uint32_t cookie_lo = RT_LODWORD(cookie);
uint32_t cookie_hi = RT_HIDWORD(cookie);
uint32_t shift_cnt;
uint32_t token;
shift_cnt = (cookie_hi >> 8) & 31;
token = ASMRotateRightU32(cookie_lo - NEC_MAGIC, shift_cnt);
shift_cnt = cookie_hi & 31;
token += ASMRotateLeftU32(cookie_lo + NEC_MAGIC, shift_cnt);
shift_cnt = (cookie_lo >> 16) & 31;
token -= ASMRotateLeftU32(cookie_hi ^ NEC_MAGIC, shift_cnt);
return ~token;
}
/**
* Process a single command TRB and post completion information.
*/
static int xhciR3ExecuteCommand(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC, XHCI_COMMAND_TRB *pCmd)
{
XHCI_EVENT_TRB ed;
uint32_t token;
unsigned slot;
unsigned cc;
int rc = VINF_SUCCESS;
LogFlowFunc(("Executing command %u (%s) @ %RGp\n", pCmd->gen.type,
pCmd->gen.type < RT_ELEMENTS(g_apszTrbNames) ? g_apszTrbNames[pCmd->gen.type] : "WHAT?!!",
(RTGCPHYS)pThis->cmdr_dqp));
switch (pCmd->gen.type)
{
case XHCI_TRB_NOOP_CMD:
/* No-op, slot ID is always zero. */
rc = xhciR3PostCmdCompletion(pDevIns, pThis, XHCI_TCC_SUCCESS, 0);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_LINK:
/* Link; set the dequeue pointer. CH bit is ignored. */
Log(("Link: Ptr=%RGp IOC=%u TC=%u\n", pCmd->link.rseg_ptr, pCmd->link.ioc, pCmd->link.toggle));
if (pCmd->link.ioc) /* Command completion event is optional! */
rc = xhciR3PostCmdCompletion(pDevIns, pThis, XHCI_TCC_SUCCESS, 0);
/* Update the dequeue pointer and flip DCS if required. */
pThis->cmdr_dqp = pCmd->link.rseg_ptr & XHCI_TRDP_ADDR_MASK;
pThis->cmdr_ccs = pThis->cmdr_ccs ^ pCmd->link.toggle;
break;
case XHCI_TRB_ENB_SLOT:
/* Look for an empty device slot. */
for (slot = 0; slot < RT_ELEMENTS(pThis->aSlotState); ++slot)
{
if (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY)
{
/* Found a slot - transition to enabled state. */
pThis->aSlotState[slot] = XHCI_DEVSLOT_ENABLED;
break;
}
}
Log(("Enable Slot: found slot ID %u\n", IDX_TO_ID(slot)));
/* Post command completion event. */
if (slot == RT_ELEMENTS(pThis->aSlotState))
xhciR3PostCmdCompletion(pDevIns, pThis, XHCI_TCC_NO_SLOTS, 0);
else
xhciR3PostCmdCompletion(pDevIns, pThis, XHCI_TCC_SUCCESS, IDX_TO_ID(slot));
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_DIS_SLOT:
/* Disable the given device slot. */
Log(("Disable Slot: slot ID %u\n", pCmd->dsl.slot_id));
cc = XHCI_TCC_SUCCESS;
slot = ID_TO_IDX(pCmd->dsl.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
{
/// @todo set slot state of assoc. context to disabled
pThis->aSlotState[slot] = XHCI_DEVSLOT_EMPTY;
}
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->dsl.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_ADDR_DEV:
/* Address a device. */
Log(("Address Device: slot ID %u, BSR=%u\n", pCmd->adr.slot_id, pCmd->adr.bsr));
slot = ID_TO_IDX(pCmd->cfg.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
cc = xhciR3AddressDevice(pDevIns, pThis, pThisCC, pCmd->adr.ctx_ptr, pCmd->adr.slot_id, pCmd->adr.bsr);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->adr.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_CFG_EP:
/* Configure endpoint. */
Log(("Configure endpoint: slot ID %u, DC=%u, Ctx @ %RGp\n", pCmd->cfg.slot_id, pCmd->cfg.dc, pCmd->cfg.ctx_ptr));
slot = ID_TO_IDX(pCmd->cfg.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
cc = xhciR3ConfigureDevice(pDevIns, pThis, pCmd->cfg.ctx_ptr, pCmd->cfg.slot_id, pCmd->cfg.dc);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->cfg.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_EVAL_CTX:
/* Evaluate context. */
Log(("Evaluate context: slot ID %u, Ctx @ %RGp\n", pCmd->evc.slot_id, pCmd->evc.ctx_ptr));
slot = ID_TO_IDX(pCmd->evc.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
cc = xhciR3EvalContext(pDevIns, pThis, pCmd->evc.ctx_ptr, pCmd->evc.slot_id);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->evc.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_RESET_EP:
/* Reset the given endpoint. */
Log(("Reset Endpoint: slot ID %u, EP ID %u, TSP=%u\n", pCmd->rse.slot_id, pCmd->rse.ep_id, pCmd->rse.tsp));
cc = XHCI_TCC_SUCCESS;
slot = ID_TO_IDX(pCmd->rse.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
cc = xhciR3ResetEndpoint(pDevIns, pThis, pCmd->rse.slot_id, pCmd->rse.ep_id, pCmd->rse.tsp);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->stp.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_STOP_EP:
/* Stop the given endpoint. */
Log(("Stop Endpoint: slot ID %u, EP ID %u, SP=%u\n", pCmd->stp.slot_id, pCmd->stp.ep_id, pCmd->stp.sp));
cc = XHCI_TCC_SUCCESS;
slot = ID_TO_IDX(pCmd->stp.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
cc = xhciR3StopEndpoint(pDevIns, pThis, pThisCC, pCmd->stp.slot_id, pCmd->stp.ep_id, pCmd->stp.sp);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->stp.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_SET_DEQ_PTR:
/* Set TR Dequeue Pointer. */
Log(("Set TRDP: slot ID %u, EP ID %u, TRDP=%RX64\n", pCmd->stdp.slot_id, pCmd->stdp.ep_id, pCmd->stdp.tr_dqp));
cc = XHCI_TCC_SUCCESS;
slot = ID_TO_IDX(pCmd->stdp.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
cc = xhciR3SetTRDP(pDevIns, pThis, pCmd->stdp.slot_id, pCmd->stdp.ep_id, pCmd->stdp.tr_dqp);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->stdp.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_RESET_DEV:
/* Reset a device. */
Log(("Reset Device: slot ID %u\n", pCmd->rsd.slot_id));
cc = XHCI_TCC_SUCCESS;
slot = ID_TO_IDX(pCmd->rsd.slot_id);
if ((slot >= RT_ELEMENTS(pThis->aSlotState)) || (pThis->aSlotState[slot] == XHCI_DEVSLOT_EMPTY))
cc = XHCI_TCC_SLOT_NOT_ENB;
else
cc = xhciR3ResetDevice(pDevIns, pThis, pCmd->rsd.slot_id);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, pCmd->rsd.slot_id);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case XHCI_TRB_GET_PORT_BW:
/* Get port bandwidth. */
Log(("Get Port Bandwidth: Dev Speed %u, Hub Slot ID %u, Context=%RX64\n", pCmd->gpbw.spd, pCmd->gpbw.slot_id, pCmd->gpbw.pbctx_ptr));
cc = XHCI_TCC_SUCCESS;
if (pCmd->gpbw.slot_id)
cc = XHCI_TCC_PARM_ERR; /* Potential undefined behavior, see 4.6.15. */
else
cc = xhciR3GetPortBandwidth(pDevIns, pThis, pCmd->gpbw.spd, pCmd->gpbw.slot_id, pCmd->gpbw.pbctx_ptr);
xhciR3PostCmdCompletion(pDevIns, pThis, cc, 0);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case NEC_TRB_GET_FW_VER:
/* Get NEC firmware version. */
Log(("Get NEC firmware version\n"));
cc = XHCI_TCC_SUCCESS;
RT_ZERO(ed);
ed.nce.word1 = NEC_FW_REV;
ed.nce.trb_ptr = pThis->cmdr_dqp;
ed.nce.cc = cc;
ed.nce.type = NEC_TRB_CMD_CMPL;
xhciR3WriteEvent(pDevIns, pThis, &ed, XHCI_PRIMARY_INTERRUPTER, false);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
case NEC_TRB_AUTHENTICATE:
/* NEC authentication. */
Log(("NEC authentication, cookie %RX64\n", pCmd->nac.cookie));
cc = XHCI_TCC_SUCCESS;
token = xhciR3NecAuthenticate(pCmd->nac.cookie);
RT_ZERO(ed);
ed.nce.word1 = RT_LOWORD(token);
ed.nce.word2 = RT_HIWORD(token);
ed.nce.trb_ptr = pThis->cmdr_dqp;
ed.nce.cc = cc;
ed.nce.type = NEC_TRB_CMD_CMPL;
xhciR3WriteEvent(pDevIns, pThis, &ed, XHCI_PRIMARY_INTERRUPTER, false);
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
default:
Log(("Unsupported command!\n"));
pThis->cmdr_dqp += sizeof(XHCI_COMMAND_TRB);
break;
}
return rc;
}
/**
* Stop the Command Ring.
*/
static int xhciR3StopCommandRing(PPDMDEVINS pDevIns, PXHCI pThis)
{
LogFlowFunc(("Command Ring stopping\n"));
Assert(pThis->crcr & (XHCI_CRCR_CA | XHCI_CRCR_CS));
Assert(pThis->crcr & XHCI_CRCR_CRR);
ASMAtomicAndU64(&pThis->crcr, ~(XHCI_CRCR_CRR | XHCI_CRCR_CA | XHCI_CRCR_CS));
return xhciR3PostCmdCompletion(pDevIns, pThis, XHCI_TCC_CMDR_STOPPED, 0);
}
/**
* Process the xHCI command ring.
*/
static int xhciR3ProcessCommandRing(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC)
{
RTGCPHYS GCPhysCmdTRB;
XHCI_COMMAND_TRB cmd; /* Command Descriptor */
unsigned cCmds;
Assert(pThis->crcr & XHCI_CRCR_CRR);
LogFlowFunc(("Processing commands...\n"));
for (cCmds = 0;; cCmds++)
{
/* First check if the xHC is running at all. */
if (!(pThis->cmd & XHCI_CMD_RS))
{
/* Note that this will call xhciR3PostCmdCompletion() which will
* end up doing nothing because R/S is clear.
*/
xhciR3StopCommandRing(pDevIns, pThis);
break;
}
/* Check if Command Ring was stopped in the meantime. */
if (pThis->crcr & (XHCI_CRCR_CS | XHCI_CRCR_CA))
{
/* NB: We currently do not abort commands. If we did, we would
* abort the currently running command and complete it with
* the XHCI_TCC_CMD_ABORTED status.
*/
xhciR3StopCommandRing(pDevIns, pThis);
break;
}
/* Fetch the command TRB. */
GCPhysCmdTRB = pThis->cmdr_dqp;
PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhysCmdTRB, &cmd, sizeof(cmd));
/* Make sure the Cycle State matches. */
if ((bool)cmd.gen.cycle == pThis->cmdr_ccs)
xhciR3ExecuteCommand(pDevIns, pThis, pThisCC, &cmd);
else
{
Log(("Command Ring empty\n"));
break;
}
/* Check if we're being fed suspiciously many commands. */
if (cCmds > XHCI_MAX_NUM_CMDS)
{
/* Clear the R/S bit and any command ring running bits.
* Note that the caller (xhciR3WorkerLoop) will set XHCI_STATUS_HCH.
*/
ASMAtomicAndU32(&pThis->cmd, ~XHCI_CMD_RS);
ASMAtomicAndU64(&pThis->crcr, ~(XHCI_CRCR_CRR | XHCI_CRCR_CA | XHCI_CRCR_CS));
ASMAtomicOrU32(&pThis->status, XHCI_STATUS_HCE);
LogRelMax(10, ("xHCI: Attempted to execute too many commands, stopping xHC!\n"));
break;
}
}
return VINF_SUCCESS;
}
/**
* The xHCI asynchronous worker thread.
*
* @returns VBox status code.
* @param pDevIns The xHCI device instance.
* @param pThread The worker thread.
*/
static DECLCALLBACK(int) xhciR3WorkerLoop(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PXHCICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PXHCICC);
int rc;
LogFlow(("xHCI entering worker thread loop.\n"));
if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
return VINF_SUCCESS;
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
uint32_t u32Tasks = 0;
uint8_t uSlotID;
ASMAtomicWriteBool(&pThis->fWrkThreadSleeping, true);
u32Tasks = ASMAtomicXchgU32(&pThis->u32TasksNew, 0);
if (!u32Tasks)
{
Assert(ASMAtomicReadBool(&pThis->fWrkThreadSleeping));
rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEvtProcess, RT_INDEFINITE_WAIT);
AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
break;
LogFlowFunc(("Woken up with rc=%Rrc\n", rc));
u32Tasks = ASMAtomicXchgU32(&pThis->u32TasksNew, 0);
}
RTCritSectEnter(&pThisCC->CritSectThrd);
if (pThis->crcr & XHCI_CRCR_CRR)
xhciR3ProcessCommandRing(pDevIns, pThis, pThisCC);
/* Run down the list of doorbells that are ringing. */
for (uSlotID = 1; uSlotID < XHCI_NDS; ++uSlotID)
{
if (pThis->aSlotState[ID_TO_IDX(uSlotID)] >= XHCI_DEVSLOT_ENABLED)
{
while (pThis->aBellsRung[ID_TO_IDX(uSlotID)])
{
uint8_t bit;
uint32_t uDBVal = 0;
for (bit = 0; bit < 32; ++bit)
if (pThis->aBellsRung[ID_TO_IDX(uSlotID)] & (1 << bit))
{
uDBVal = bit;
break;
}
Log2(("Stop ringing bell for slot %u, DCI %u\n", uSlotID, uDBVal));
ASMAtomicAndU32(&pThis->aBellsRung[ID_TO_IDX(uSlotID)], ~(1 << uDBVal));
xhciR3ProcessDevCtx(pDevIns, pThis, pThisCC, uSlotID, uDBVal);
}
}
}
/* If the R/S bit is no longer set, halt the xHC. */
if (!(pThis->cmd & XHCI_CMD_RS))
{
Log(("R/S clear, halting the xHC.\n"));
ASMAtomicOrU32(&pThis->status, XHCI_STATUS_HCH);
}
RTCritSectLeave(&pThisCC->CritSectThrd);
ASMAtomicWriteBool(&pThis->fWrkThreadSleeping, false);
} /* While running */
LogFlow(("xHCI worker thread exiting.\n"));
return VINF_SUCCESS;
}
/**
* Unblock the worker thread so it can respond to a state change.
*
* @returns VBox status code.
* @param pDevIns The xHCI device instance.
* @param pThread The worker thread.
*/
static DECLCALLBACK(int) xhciR3WorkerWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
{
NOREF(pThread);
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtProcess);
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) xhciR3RhQueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
PXHCIROOTHUBR3 pRh = RT_FROM_MEMBER(pInterface, XHCIROOTHUBR3, IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pRh->IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, VUSBIROOTHUBPORT, &pRh->IRhPort);
return NULL;
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) xhciR3QueryStatusInterface(PPDMIBASE pInterface, const char *pszIID)
{
PXHCIR3 pThisCC = RT_FROM_MEMBER(pInterface, XHCIR3, IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThisCC->ILeds);
return NULL;
}
/**
* Gets the pointer to the status LED of a unit.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param iLUN The unit which status LED we desire.
* @param ppLed Where to store the LED pointer.
*/
static DECLCALLBACK(int) xhciR3QueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
{
PXHCICC pThisCC = RT_FROM_MEMBER(pInterface, XHCIR3, ILeds);
if (iLUN < XHCI_NUM_LUNS)
{
*ppLed = iLUN ? &pThisCC->RootHub3.Led : &pThisCC->RootHub2.Led;
Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
return VINF_SUCCESS;
}
return VERR_PDM_LUN_NOT_FOUND;
}
/**
* Get the number of ports available in the hub.
*
* @returns The number of ports available.
* @param pInterface Pointer to this structure.
* @param pAvailable Bitmap indicating the available ports. Set bit == available port.
*/
static DECLCALLBACK(unsigned) xhciR3RhGetAvailablePorts(PVUSBIROOTHUBPORT pInterface, PVUSBPORTBITMAP pAvailable)
{
PXHCIROOTHUBR3 pRh = RT_FROM_MEMBER(pInterface, XHCIROOTHUBR3, IRhPort);
PXHCICC pThisCC = pRh->pXhciR3;
PPDMDEVINS pDevIns = pThisCC->pDevIns;
unsigned iPort;
unsigned cPorts = 0;
LogFlow(("xhciR3RhGetAvailablePorts\n"));
memset(pAvailable, 0, sizeof(*pAvailable));
int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
for (iPort = pRh->uPortBase; iPort < (unsigned)pRh->uPortBase + pRh->cPortsImpl; iPort++)
{
Assert(iPort < XHCI_NDP_CFG(PDMDEVINS_2_DATA(pDevIns, PXHCI)));
if (!pThisCC->aPorts[iPort].fAttached)
{
cPorts++;
ASMBitSet(pAvailable, IDX_TO_ID(iPort - pRh->uPortBase));
}
}
PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
return cPorts;
}
/**
* Get the supported USB versions for USB2 hubs.
*
* @returns The mask of supported USB versions.
* @param pInterface Pointer to this structure.
*/
static DECLCALLBACK(uint32_t) xhciR3RhGetUSBVersions2(PVUSBIROOTHUBPORT pInterface)
{
RT_NOREF(pInterface);
return VUSB_STDVER_11 | VUSB_STDVER_20;
}
/**
* Get the supported USB versions for USB2 hubs.
*
* @returns The mask of supported USB versions.
* @param pInterface Pointer to this structure.
*/
static DECLCALLBACK(uint32_t) xhciR3RhGetUSBVersions3(PVUSBIROOTHUBPORT pInterface)
{
RT_NOREF(pInterface);
return VUSB_STDVER_30;
}
/**
* Start sending SOF tokens across the USB bus, lists are processed in the
* next frame.
*/
static void xhciR3BusStart(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC)
{
unsigned iPort;
pThisCC->RootHub2.pIRhConn->pfnPowerOn(pThisCC->RootHub2.pIRhConn);
pThisCC->RootHub3.pIRhConn->pfnPowerOn(pThisCC->RootHub3.pIRhConn);
// xhciR3BumpFrameNumber(pThis);
Log(("xHCI: Bus started\n"));
Assert(pThis->status & XHCI_STATUS_HCH);
ASMAtomicAndU32(&pThis->status, ~XHCI_STATUS_HCH);
/* HCH gates PSCEG (4.19.2). When clearing HCH, re-evaluate port changes. */
for (iPort = 0; iPort < XHCI_NDP_CFG(pThis); ++iPort)
{
if (pThis->aPorts[iPort].portsc & XHCI_PORT_CHANGE_MASK)
xhciR3GenPortChgEvent(pDevIns, pThis, IDX_TO_ID(iPort));
}
/// @todo record the starting time?
// pThis->SofTime = TMTimerGet(pThis->CTX_SUFF(pEndOfFrameTimer)) - pThis->cTicksPerFrame;
}
/**
* Stop sending SOF tokens on the bus and processing the data.
*/
static void xhciR3BusStop(PPDMDEVINS pDevIns, PXHCI pThis, PXHCICC pThisCC)
{
LogFlow(("xhciR3BusStop\n"));
/* Stop the controller and Command Ring. */
pThis->cmd &= ~XHCI_CMD_RS;
pThis->crcr |= XHCI_CRCR_CS;
/* Power off the root hubs. */
pThisCC->RootHub2.pIRhConn->pfnPowerOff(pThisCC->RootHub2.pIRhConn);
pThisCC->RootHub3.pIRhConn->pfnPowerOff(pThisCC->RootHub3.pIRhConn);
/* The worker thread will halt the HC (set HCH) when done. */
xhciKickWorker(pDevIns, pThis, XHCI_JOB_PROCESS_CMDRING, 0);
}
/**
* Power a port up or down
*/
static void xhciR3PortPower(PXHCI pThis, PXHCICC pThisCC, unsigned iPort, bool fPowerUp)
{
PXHCIHUBPORT pPort = &pThis->aPorts[iPort];
PXHCIHUBPORTR3 pPortR3 = &pThisCC->aPorts[iPort];
PXHCIROOTHUBR3 pRh = GET_PORT_PRH(pThisCC, iPort);
bool fOldPPS = !!(pPort->portsc & XHCI_PORT_PP);
LogFlow(("xhciR3PortPower (port %u) %s\n", IDX_TO_ID(iPort), fPowerUp ? "UP" : "DOWN"));
if (fPowerUp)
{
/* Power up a port. */
if (pPortR3->fAttached)
ASMAtomicOrU32(&pPort->portsc, XHCI_PORT_CCS);
if (pPort->portsc & XHCI_PORT_CCS)
ASMAtomicOrU32(&pPort->portsc, XHCI_PORT_PP);
if (pPortR3->fAttached && !fOldPPS)
VUSBIRhDevPowerOn(pRh->pIRhConn, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort));
}
else
{
/* Power down. */
ASMAtomicAndU32(&pPort->portsc, ~(XHCI_PORT_PP | XHCI_PORT_CCS));
if (pPortR3->fAttached && fOldPPS)
VUSBIRhDevPowerOff(pRh->pIRhConn, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort));
}
}
/**
* Port reset done callback.
*
* @returns nothing.
* @param pDevIns The device instance data.
* @param iPort The XHCI port index of the port being resetted.
*/
static void xhciR3PortResetDone(PPDMDEVINS pDevIns, unsigned iPort)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
Log2(("xhciR3PortResetDone\n"));
AssertReturnVoid(iPort < XHCI_NDP_CFG(pThis));
/*
* Successful reset.
*/
Log2(("xhciR3PortResetDone: Reset completed.\n"));
uint32_t fChangeMask = XHCI_PORT_PED | XHCI_PORT_PRC;
/* For USB2 ports, transition the link state. */
if (!IS_USB3_PORT_IDX_SHR(pThis, iPort))
{
pThis->aPorts[iPort].portsc &= ~XHCI_PORT_PLS_MASK;
pThis->aPorts[iPort].portsc |= XHCI_PLS_U0 << XHCI_PORT_PLS_SHIFT;
}
else
{
if (pThis->aPorts[iPort].portsc & XHCI_PORT_WPR)
fChangeMask |= XHCI_PORT_WRC;
}
ASMAtomicAndU32(&pThis->aPorts[iPort].portsc, ~(XHCI_PORT_PR | XHCI_PORT_WPR));
ASMAtomicOrU32(&pThis->aPorts[iPort].portsc, fChangeMask);
/// @todo Set USBSTS.PCD and manage PSCEG correctly!
/// @todo just guessing?!
// ASMAtomicOrU32(&pThis->aPorts[iPort].portsc, XHCI_PORT_CSC | XHCI_PORT_PLC);
/// @todo Is this the right place?
xhciR3GenPortChgEvent(pDevIns, pThis, IDX_TO_ID(iPort));
}
/**
* Sets a flag in a port status register, but only if a device is connected;
* if not, set ConnectStatusChange flag to force HCD to reevaluate connect status.
*
* @returns true if device was connected and the flag was cleared.
*/
static bool xhciR3RhPortSetIfConnected(PXHCI pThis, int iPort, uint32_t fValue)
{
/*
* Writing a 0 has no effect
*/
if (fValue == 0)
return false;
/*
* The port might be still/already disconnected.
*/
if (!(pThis->aPorts[iPort].portsc & XHCI_PORT_CCS))
return false;
bool fRc = !(pThis->aPorts[iPort].portsc & fValue);
/* Set the bit. */
ASMAtomicOrU32(&pThis->aPorts[iPort].portsc, fValue);
return fRc;
}
/** Translate VUSB speed enum to xHCI definition. */
static unsigned xhciR3UsbSpd2XhciSpd(VUSBSPEED enmSpeed)
{
unsigned uSpd;
switch (enmSpeed)
{
default: AssertMsgFailed(("%d\n", enmSpeed));
RT_FALL_THRU();
case VUSB_SPEED_LOW: uSpd = XHCI_SPD_LOW; break;
case VUSB_SPEED_FULL: uSpd = XHCI_SPD_FULL; break;
case VUSB_SPEED_HIGH: uSpd = XHCI_SPD_HIGH; break;
case VUSB_SPEED_SUPER: uSpd = XHCI_SPD_SUPER; break;
}
return uSpd;
}
/** @interface_method_impl{VUSBIROOTHUBPORT,pfnAttach} */
static DECLCALLBACK(int) xhciR3RhAttach(PVUSBIROOTHUBPORT pInterface, unsigned uPort, VUSBSPEED enmSpeed)
{
PXHCIROOTHUBR3 pRh = RT_FROM_MEMBER(pInterface, XHCIROOTHUBR3, IRhPort);
PXHCICC pThisCC = pRh->pXhciR3;
PPDMDEVINS pDevIns = pThisCC->pDevIns;
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PXHCIHUBPORT pPort;
unsigned iPort;
LogFlow(("xhciR3RhAttach: uPort=%u (iPort=%u)\n", uPort, ID_TO_IDX(uPort) + pRh->uPortBase));
int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
AssertRCReturn(rcLock, rcLock);
/*
* Validate and adjust input.
*/
Assert(uPort >= 1 && uPort <= pRh->cPortsImpl);
iPort = ID_TO_IDX(uPort) + pRh->uPortBase;
Assert(iPort < XHCI_NDP_CFG(pThis));
pPort = &pThis->aPorts[iPort];
Assert(!pThisCC->aPorts[iPort].fAttached);
Assert(enmSpeed != VUSB_SPEED_UNKNOWN);
/*
* Attach it.
*/
ASMAtomicOrU32(&pPort->portsc, XHCI_PORT_CCS | XHCI_PORT_CSC);
pThisCC->aPorts[iPort].fAttached = true;
xhciR3PortPower(pThis, pThisCC, iPort, 1 /* power on */);
/* USB3 ports automatically transition to Enabled state. */
if (IS_USB3_PORT_IDX_R3(pThisCC, iPort))
{
Assert(enmSpeed == VUSB_SPEED_SUPER);
pPort->portsc |= XHCI_PORT_PED;
pPort->portsc &= ~XHCI_PORT_PLS_MASK;
pPort->portsc |= XHCI_PLS_U0 << XHCI_PORT_PLS_SHIFT;
pPort->portsc &= ~XHCI_PORT_SPD_MASK;
pPort->portsc |= XHCI_SPD_SUPER << XHCI_PORT_SPD_SHIFT;
VUSBIRhDevReset(pRh->pIRhConn, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort),
false, NULL /* sync */, NULL, PDMDevHlpGetVM(pDevIns));
}
else
{
Assert(enmSpeed == VUSB_SPEED_LOW || enmSpeed == VUSB_SPEED_FULL || enmSpeed == VUSB_SPEED_HIGH);
pPort->portsc &= ~XHCI_PORT_SPD_MASK;
pPort->portsc |= xhciR3UsbSpd2XhciSpd(enmSpeed) << XHCI_PORT_SPD_SHIFT;
}
xhciR3GenPortChgEvent(pDevIns, pThis, IDX_TO_ID(iPort));
PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
return VINF_SUCCESS;
}
/**
* A device is being detached from a port in the root hub.
*
* @param pInterface Pointer to this structure.
* @param uPort The 1-based port number assigned to the device.
*/
static DECLCALLBACK(void) xhciR3RhDetach(PVUSBIROOTHUBPORT pInterface, unsigned uPort)
{
PXHCIROOTHUBR3 pRh = RT_FROM_MEMBER(pInterface, XHCIROOTHUBR3, IRhPort);
PXHCICC pThisCC = pRh->pXhciR3;
PPDMDEVINS pDevIns = pThisCC->pDevIns;
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PXHCIHUBPORT pPort;
unsigned iPort;
LogFlow(("xhciR3RhDetach: uPort=%u iPort=%u\n", uPort, ID_TO_IDX(uPort) + pRh->uPortBase));
int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
/*
* Validate and adjust input.
*/
Assert(uPort >= 1 && uPort <= pRh->cPortsImpl);
iPort = ID_TO_IDX(uPort) + pRh->uPortBase;
Assert(iPort < XHCI_NDP_CFG(pThis));
pPort = &pThis->aPorts[iPort];
Assert(pThisCC->aPorts[iPort].fAttached);
/*
* Detach it.
*/
pThisCC->aPorts[iPort].fAttached = false;
ASMAtomicAndU32(&pPort->portsc, ~(XHCI_PORT_CCS | XHCI_PORT_SPD_MASK | XHCI_PORT_PLS_MASK));
ASMAtomicOrU32(&pPort->portsc, XHCI_PORT_CSC);
/* Link state goes to RxDetect. */
ASMAtomicOrU32(&pPort->portsc, XHCI_PLS_RXDETECT << XHCI_PORT_PLS_SHIFT);
/* Disconnect clears the port enable bit. */
if (pPort->portsc & XHCI_PORT_PED)
ASMAtomicAndU32(&pPort->portsc, ~XHCI_PORT_PED);
xhciR3GenPortChgEvent(pDevIns, pThis, IDX_TO_ID(iPort));
PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
}
/**
* One of the root hub devices has completed its reset
* operation.
*
* Currently, we don't think anything is required to be done here
* so it's just a stub for forcing async resetting of the devices
* during a root hub reset.
*
* @param pDev The root hub device.
* @param rc The result of the operation.
* @param uPort The port number of the device on the roothub being resetted.
* @param pvUser Pointer to the controller.
*/
static DECLCALLBACK(void) xhciR3RhResetDoneOneDev(PVUSBIDEVICE pDev, uint32_t uPort, int rc, void *pvUser)
{
LogRel(("xHCI: Root hub-attached device reset completed with %Rrc\n", rc));
RT_NOREF(pDev, uPort, rc, pvUser);
}
/**
* Does a software or hardware reset of the controller.
*
* This is called in response to setting HcCommandStatus.HCR, hardware reset,
* and device construction.
*
* @param pThis The shared XHCI instance data
* @param pThisCC The ring-3 XHCI instance data
* @param fNewMode The new mode of operation. This is UsbSuspend if
* it's a software reset, and UsbReset if it's a
* hardware reset / cold boot.
* @param fTrueReset Set if we can do a real reset of the devices
* attached to the root hub. This is really a just a
* hack for the non-working linux device reset. Linux
* has this feature called 'logical disconnect' if
* device reset fails which prevents us from doing
* resets when the guest asks for it - the guest will
* get confused when the device seems to be
* reconnected everytime it tries to reset it. But if
* we're at hardware reset time, we can allow a device
* to be 'reconnected' without upsetting the guest.
*
* @remark This has nothing to do with software setting the
* mode to UsbReset.
*/
static void xhciR3DoReset(PXHCI pThis, PXHCICC pThisCC, uint32_t fNewMode, bool fTrueReset)
{
LogFunc(("%s reset%s\n", fNewMode == XHCI_USB_RESET ? "Hardware" : "Software",
fTrueReset ? " (really reset devices)" : ""));
/*
* Cancel all outstanding URBs.
*
* We can't, and won't, deal with URBs until we're moved out of the
* suspend/reset state. Also, a real HC isn't going to send anything
* any more when a reset has been signaled.
*/
pThisCC->RootHub2.pIRhConn->pfnCancelAllUrbs(pThisCC->RootHub2.pIRhConn);
pThisCC->RootHub3.pIRhConn->pfnCancelAllUrbs(pThisCC->RootHub3.pIRhConn);
/*
* Reset the hardware registers.
*/
/** @todo other differences between hardware reset and VM reset? */
pThis->cmd = 0;
pThis->status = XHCI_STATUS_HCH;
pThis->dnctrl = 0;
pThis->crcr = 0;
pThis->dcbaap = 0;
pThis->config = 0;
/*
* Reset the internal state.
*/
pThis->cmdr_dqp = 0;
pThis->cmdr_ccs = 0;
RT_ZERO(pThis->aSlotState);
RT_ZERO(pThis->aBellsRung);
/* Zap everything but the lock. */
for (unsigned i = 0; i < RT_ELEMENTS(pThis->aInterrupters); ++i)
{
pThis->aInterrupters[i].iman = 0;
pThis->aInterrupters[i].imod = 0;
pThis->aInterrupters[i].erstsz = 0;
pThis->aInterrupters[i].erstba = 0;
pThis->aInterrupters[i].erdp = 0;
pThis->aInterrupters[i].erep = 0;
pThis->aInterrupters[i].erst_idx = 0;
pThis->aInterrupters[i].trb_count = 0;
pThis->aInterrupters[i].evtr_pcs = false;
pThis->aInterrupters[i].ipe = false;
}
if (fNewMode == XHCI_USB_RESET)
{
/* Only a hardware reset reinits the port registers. */
for (unsigned i = 0; i < XHCI_NDP_CFG(pThis); i++)
{
/* Need to preserve the speed of attached devices. */
pThis->aPorts[i].portsc &= XHCI_PORT_SPD_MASK;
pThis->aPorts[i].portsc |= XHCI_PLS_RXDETECT << XHCI_PORT_PLS_SHIFT;
/* If Port Power Control is not supported, ports are always powered on. */
if (!(pThis->hcc_params & XHCI_HCC_PPC))
pThis->aPorts[i].portsc |= XHCI_PORT_PP;
}
}
/*
* If this is a hardware reset, we will initialize the root hub too.
* Software resets doesn't do this according to the specs.
* (It's not possible to have a device connected at the time of the
* device construction, so nothing to worry about there.)
*/
if (fNewMode == XHCI_USB_RESET)
{
pThisCC->RootHub2.pIRhConn->pfnReset(pThisCC->RootHub2.pIRhConn, fTrueReset);
pThisCC->RootHub3.pIRhConn->pfnReset(pThisCC->RootHub3.pIRhConn, fTrueReset);
/*
* Reattach the devices.
*/
for (unsigned i = 0; i < XHCI_NDP_CFG(pThis); i++)
{
bool fAttached = pThisCC->aPorts[i].fAttached;
PXHCIROOTHUBR3 pRh = GET_PORT_PRH(pThisCC, i);
pThisCC->aPorts[i].fAttached = false;
if (fAttached)
{
VUSBSPEED enmSpeed = VUSBIRhDevGetSpeed(pRh->pIRhConn, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, i));
xhciR3RhAttach(&pRh->IRhPort, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, i), enmSpeed);
}
}
}
}
/**
* Reset the root hub.
*
* @returns VBox status code.
* @param pInterface Pointer to this structure.
* @param fTrueReset This is used to indicate whether we're at VM reset
* time and can do real resets or if we're at any other
* time where that isn't such a good idea.
* @remark Do NOT call VUSBIDevReset on the root hub in an async fashion!
* @thread EMT
*/
static DECLCALLBACK(int) xhciR3RhReset(PVUSBIROOTHUBPORT pInterface, bool fTrueReset)
{
PXHCIROOTHUBR3 pRh = RT_FROM_MEMBER(pInterface, XHCIROOTHUBR3, IRhPort);
PXHCICC pThisCC = pRh->pXhciR3;
PPDMDEVINS pDevIns = pThisCC->pDevIns;
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
Log(("xhciR3RhReset fTrueReset=%d\n", fTrueReset));
int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
AssertRCReturn(rcLock, rcLock);
/* Soft reset first */
xhciR3DoReset(pThis, pThisCC, XHCI_USB_SUSPEND, false /* N/A */);
/*
* We're pretending to _reattach_ the devices without resetting them.
* Except, during VM reset where we use the opportunity to do a proper
* reset before the guest comes along and expects things.
*
* However, it's very very likely that we're not doing the right thing
* here when end up here on request from the guest (USB Reset state).
* The docs talk about root hub resetting, however what exact behaviour
* in terms of root hub status and changed bits, and HC interrupts aren't
* stated clearly. IF we get trouble and see the guest doing "USB Resets"
* we will have to look into this. For the time being we stick with simple.
*/
for (unsigned iPort = pRh->uPortBase; iPort < XHCI_NDP_CFG(pThis); iPort++)
{
if (pThisCC->aPorts[iPort].fAttached)
{
ASMAtomicOrU32(&pThis->aPorts[iPort].portsc, XHCI_PORT_CCS | XHCI_PORT_CSC);
if (fTrueReset)
VUSBIRhDevReset(pRh->pIRhConn, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort), fTrueReset,
xhciR3RhResetDoneOneDev, pDevIns, PDMDevHlpGetVM(pDevIns));
}
}
PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
return VINF_SUCCESS;
}
#endif /* IN_RING3 */
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* xHCI Operational Register access routines */
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/**
* Read the USBCMD register of the host controller.
*/
static VBOXSTRICTRC HcUsbcmd_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdUsbCmd);
*pu32Value = pThis->cmd;
return VINF_SUCCESS;
}
/**
* Write to the USBCMD register of the host controller.
*/
static VBOXSTRICTRC HcUsbcmd_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
#ifdef IN_RING3
PXHCICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PXHCICC);
#endif
RT_NOREF(iReg);
STAM_COUNTER_INC(&pThis->StatWrUsbCmd);
#ifdef LOG_ENABLED
Log(("HcUsbcmd_w old=%x new=%x\n", pThis->cmd, val));
if (val & XHCI_CMD_RS)
Log((" XHCI_CMD_RS\n"));
if (val & XHCI_CMD_HCRST)
Log((" XHCI_CMD_HCRST\n"));
if (val & XHCI_CMD_INTE )
Log((" XHCI_CMD_INTE\n"));
if (val & XHCI_CMD_HSEE)
Log((" XHCI_CMD_HSEE\n"));
if (val & XHCI_CMD_LCRST)
Log((" XHCI_CMD_LCRST\n"));
if (val & XHCI_CMD_CSS)
Log((" XHCI_CMD_CSS\n"));
if (val & XHCI_CMD_CRS)
Log((" XHCI_CMD_CRS\n"));
if (val & XHCI_CMD_EWE)
Log((" XHCI_CMD_EWE\n"));
if (val & XHCI_CMD_EU3S)
Log((" XHCI_CMD_EU3S\n"));
#endif
if (val & ~XHCI_CMD_MASK)
Log(("Unknown USBCMD bits %#x are set!\n", val & ~XHCI_CMD_MASK));
uint32_t old_cmd = pThis->cmd;
#ifdef IN_RING3
pThis->cmd = val;
#endif
if (val & XHCI_CMD_HCRST)
{
#ifdef IN_RING3
LogRel(("xHCI: Hardware reset\n"));
xhciR3DoReset(pThis, pThisCC, XHCI_USB_RESET, true /* reset devices */);
#else
return VINF_IOM_R3_MMIO_WRITE;
#endif
}
else if (val & XHCI_CMD_LCRST)
{
#ifdef IN_RING3
LogRel(("xHCI: Software reset\n"));
xhciR3DoReset(pThis, pThisCC, XHCI_USB_SUSPEND, false /* N/A */);
#else
return VINF_IOM_R3_MMIO_WRITE;
#endif
}
else if (pThis->status & XHCI_STATUS_HCE)
{
/* If HCE is set, don't restart the controller. Only a reset
* will clear the HCE bit.
*/
Log(("xHCI: HCE bit set, ignoring USBCMD register changes!\n"));
pThis->cmd = old_cmd;
return VINF_SUCCESS;
}
else
{
/* See what changed and take action on that. First the R/S bit. */
uint32_t old_state = old_cmd & XHCI_CMD_RS;
uint32_t new_state = val & XHCI_CMD_RS;
if (old_state != new_state)
{
#ifdef IN_RING3
switch (new_state)
{
case XHCI_CMD_RS:
LogRel(("xHCI: USB Operational\n"));
xhciR3BusStart(pDevIns, pThis, pThisCC);
break;
case 0:
xhciR3BusStop(pDevIns, pThis, pThisCC);
LogRel(("xHCI: USB Suspended\n"));
break;
}
#else
return VINF_IOM_R3_MMIO_WRITE;
#endif
}
/* Check EWE (Enable MFINDEX Wraparound Event) changes. */
old_state = old_cmd & XHCI_CMD_EWE;
new_state = val & XHCI_CMD_EWE;
if (old_state != new_state)
{
switch (new_state)
{
case XHCI_CMD_EWE:
Log(("xHCI: MFINDEX Wrap timer started\n"));
xhciSetWrapTimer(pDevIns, pThis);
break;
case 0:
PDMDevHlpTimerStop(pDevIns, pThis->hWrapTimer);
Log(("xHCI: MFINDEX Wrap timer stopped\n"));
break;
}
}
/* INTE transitions need to twiddle interrupts. */
old_state = old_cmd & XHCI_CMD_INTE;
new_state = val & XHCI_CMD_INTE;
if (old_state != new_state)
{
switch (new_state)
{
case XHCI_CMD_INTE:
/* Check whether the event interrupt bit is set and trigger an interrupt. */
if (pThis->status & XHCI_STATUS_EINT)
PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
break;
case 0:
PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
break;
}
}
/* We currently do nothing for state save/restore. If we did, the CSS/CRS command bits
* would set the SSS/RSS status bits until the operation is done. The CSS/CRS bits are
* never read as one.
*/
/// @todo 4.9.4 describes internal state that needs to be saved/restored:
/// ERSTE, ERST Count, EREP, and TRB Count
/// Command Ring Dequeue Pointer?
if (val & XHCI_CMD_CSS)
{
Log(("xHCI: Save State requested\n"));
val &= ~XHCI_CMD_CSS;
}
if (val & XHCI_CMD_CRS)
{
Log(("xHCI: Restore State requested\n"));
val &= ~XHCI_CMD_CRS;
}
}
#ifndef IN_RING3
pThis->cmd = val;
#endif
return VINF_SUCCESS;
}
#ifdef LOG_ENABLED
static void HcUsbstsLogBits(uint32_t val)
{
if (val & XHCI_STATUS_HCH)
Log((" XHCI_STATUS_HCH (HC Halted)\n"));
if (val & XHCI_STATUS_HSE)
Log((" XHCI_STATUS_HSE (Host System Error)\n"));
if (val & XHCI_STATUS_EINT)
Log((" XHCI_STATUS_EINT (Event Interrupt)\n"));
if (val & XHCI_STATUS_PCD)
Log((" XHCI_STATUS_PCD (Port Change Detect)\n"));
if (val & XHCI_STATUS_SSS)
Log((" XHCI_STATUS_SSS (Save State Status)\n"));
if (val & XHCI_STATUS_RSS)
Log((" XHCI_STATUS_RSS (Restore State Status)\n"));
if (val & XHCI_STATUS_SRE)
Log((" XHCI_STATUS_SRE (Save/Restore Error)\n"));
if (val & XHCI_STATUS_CNR)
Log((" XHCI_STATUS_CNR (Controller Not Ready)\n"));
if (val & XHCI_STATUS_HCE)
Log((" XHCI_STATUS_HCE (Host Controller Error)\n"));
}
#endif
/**
* Read the USBSTS register of the host controller.
*/
static VBOXSTRICTRC HcUsbsts_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
#ifdef LOG_ENABLED
Log(("HcUsbsts_r current value %x\n", pThis->status));
HcUsbstsLogBits(pThis->status);
#endif
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdUsbSts);
*pu32Value = pThis->status;
return VINF_SUCCESS;
}
/**
* Write to the USBSTS register of the host controller.
*/
static VBOXSTRICTRC HcUsbsts_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
#ifdef LOG_ENABLED
Log(("HcUsbsts_w current value %x; new %x\n", pThis->status, val));
HcUsbstsLogBits(val);
#endif
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatWrUsbSts);
if ( (val & ~XHCI_STATUS_WRMASK)
&& val != 0xffffffff /* Ignore clear-all-like requests. */)
Log(("Unknown USBSTS bits %#x are set!\n", val & ~XHCI_STATUS_WRMASK));
/* Most bits are read-only. */
val &= XHCI_STATUS_WRMASK;
/* "The Host Controller Driver may clear specific bits in this
* register by writing '1' to bit positions to be cleared"
*/
ASMAtomicAndU32(&pThis->status, ~val);
return VINF_SUCCESS;
}
/**
* Read the PAGESIZE register of the host controller.
*/
static VBOXSTRICTRC HcPagesize_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis, iReg);
STAM_COUNTER_INC(&pThis->StatRdPageSize);
*pu32Value = 1; /* 2^(bit n + 12) -> 4K page size only. */
return VINF_SUCCESS;
}
/**
* Read the DNCTRL (Device Notification Control) register.
*/
static VBOXSTRICTRC HcDevNotifyCtrl_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdDevNotifyCtrl);
*pu32Value = pThis->dnctrl;
return VINF_SUCCESS;
}
/**
* Write the DNCTRL (Device Notification Control) register.
*/
static VBOXSTRICTRC HcDevNotifyCtrl_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatWrDevNotifyCtrl);
pThis->dnctrl = val;
return VINF_SUCCESS;
}
/**
* Read the low dword of CRCR (Command Ring Control) register.
*/
static VBOXSTRICTRC HcCmdRingCtlLo_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdCmdRingCtlLo);
*pu32Value = (uint32_t)(pThis->crcr & XHCI_CRCR_RD_MASK);
return VINF_SUCCESS;
}
/**
* Write the low dword of CRCR (Command Ring Control) register.
*/
static VBOXSTRICTRC HcCmdRingCtlLo_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF(iReg);
STAM_COUNTER_INC(&pThis->StatWrCmdRingCtlLo);
/* NB: A dword write to the low half clears the high half. */
/* Sticky Abort/Stop bits - update register and kick the worker thread. */
if (val & (XHCI_CRCR_CA | XHCI_CRCR_CS))
{
pThis->crcr |= val & (XHCI_CRCR_CA | XHCI_CRCR_CS);
xhciKickWorker(pDevIns, pThis, XHCI_JOB_PROCESS_CMDRING, 0);
}
/*
* If the command ring is not running, the internal dequeue pointer
* and the cycle state is updated. Otherwise the update is ignored.
*/
if (!(pThis->crcr & XHCI_CRCR_CRR))
{
pThis->crcr = (pThis->crcr & ~XHCI_CRCR_UPD_MASK) | (val & XHCI_CRCR_UPD_MASK);
/// @todo cmdr_dqp: atomic? volatile?
pThis->cmdr_dqp = pThis->crcr & XHCI_CRCR_ADDR_MASK;
pThis->cmdr_ccs = pThis->crcr & XHCI_CRCR_RCS;
}
return VINF_SUCCESS;
}
/**
* Read the high dword of CRCR (Command Ring Control) register.
*/
static VBOXSTRICTRC HcCmdRingCtlHi_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdCmdRingCtlHi);
*pu32Value = pThis->crcr >> 32;
return VINF_SUCCESS;
}
/**
* Write the high dword of CRCR (Command Ring Control) register.
*/
static VBOXSTRICTRC HcCmdRingCtlHi_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatWrCmdRingCtlHi);
if (!(pThis->crcr & XHCI_CRCR_CRR))
{
pThis->crcr = ((uint64_t)val << 32) | (uint32_t)pThis->crcr;
pThis->cmdr_dqp = pThis->crcr & XHCI_CRCR_ADDR_MASK;
}
return VINF_SUCCESS;
}
/**
* Read the low dword of the DCBAAP register.
*/
static VBOXSTRICTRC HcDevCtxBAAPLo_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdDevCtxBaapLo);
*pu32Value = (uint32_t)pThis->dcbaap;
return VINF_SUCCESS;
}
/**
* Write the low dword of the DCBAAP register.
*/
static VBOXSTRICTRC HcDevCtxBAAPLo_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatWrDevCtxBaapLo);
/* NB: A dword write to the low half clears the high half. */
/// @todo Should this mask off the reserved bits?
pThis->dcbaap = val;
return VINF_SUCCESS;
}
/**
* Read the high dword of the DCBAAP register.
*/
static VBOXSTRICTRC HcDevCtxBAAPHi_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdDevCtxBaapHi);
*pu32Value = pThis->dcbaap >> 32;
return VINF_SUCCESS;
}
/**
* Write the high dword of the DCBAAP register.
*/
static VBOXSTRICTRC HcDevCtxBAAPHi_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatWrDevCtxBaapHi);
pThis->dcbaap = ((uint64_t)val << 32) | (uint32_t)pThis->dcbaap;
return VINF_SUCCESS;
}
/**
* Read the CONFIG register.
*/
static VBOXSTRICTRC HcConfig_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatRdConfig);
*pu32Value = pThis->config;
return VINF_SUCCESS;
}
/**
* Write the CONFIG register.
*/
static VBOXSTRICTRC HcConfig_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t val)
{
RT_NOREF(pDevIns, iReg);
STAM_COUNTER_INC(&pThis->StatWrConfig);
/// @todo side effects?
pThis->config = val;
return VINF_SUCCESS;
}
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* xHCI Port Register access routines */
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/**
* Read the PORTSC register.
*/
static VBOXSTRICTRC HcPortStatusCtrl_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iPort, uint32_t *pu32Value)
{
PXHCIHUBPORT p = &pThis->aPorts[iPort];
RT_NOREF(pDevIns);
STAM_COUNTER_INC(&pThis->StatRdPortStatusCtrl);
Assert(!(pThis->hcc_params & XHCI_HCC_PPC));
if (p->portsc & XHCI_PORT_PR)
{
/// @todo Probably not needed?
#ifdef IN_RING3
Log2(("HcPortStatusCtrl_r(): port %u: Impatient guest!\n", IDX_TO_ID(iPort)));
RTThreadYield();
#else
Log2(("HcPortStatusCtrl_r: yield -> VINF_IOM_R3_MMIO_READ\n"));
return VINF_IOM_R3_MMIO_READ;
#endif
}
/* The WPR bit is always read as zero. */
*pu32Value = p->portsc & ~XHCI_PORT_WPR;
return VINF_SUCCESS;
}
/**
* Write the PORTSC register.
*/
static VBOXSTRICTRC HcPortStatusCtrl_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iPort, uint32_t val)
{
PXHCIHUBPORT p = &pThis->aPorts[iPort];
#ifdef IN_RING3
PXHCICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PXHCICC);
#endif
STAM_COUNTER_INC(&pThis->StatWrPortStatusCtrl);
/* If no register change results, we're done. */
if ( p->portsc == val
&& !(val & XHCI_PORT_CHANGE_MASK))
return VINF_SUCCESS;
/* If port state is not changing (status bits are being cleared etc.), we can do it in any context.
* This case occurs when the R/W control bits are not changing and the W1C bits are not being set.
*/
if ( (p->portsc & XHCI_PORT_CTL_RW_MASK) == (val & XHCI_PORT_CTL_RW_MASK)
&& !(val & XHCI_PORT_CTL_W1_MASK))
{
Log(("HcPortStatusCtrl_w port %u (status only): old=%x new=%x\n", IDX_TO_ID(iPort), p->portsc, val));
if (val & XHCI_PORT_RESERVED)
Log(("Reserved bits set %x!\n", val & XHCI_PORT_RESERVED));
/* A write to clear any of the change notification bits. */
if (val & XHCI_PORT_CHANGE_MASK)
p->portsc &= ~(val & XHCI_PORT_CHANGE_MASK);
/* Update the wake mask. */
p->portsc &= ~XHCI_PORT_WAKE_MASK;
p->portsc |= val & XHCI_PORT_WAKE_MASK;
/* There may still be differences between 'portsc' and 'val' in
* the R/O bits; that does not count as a register change and is fine.
* The RW1x control bits are not considered either since those only matter
* if set in 'val'. Since the LWS bit was not set, the PLS bits should not
* be compared. The port change bits may differ as well since the guest
* could be clearing only some or none of them.
*/
AssertMsg(!(val & XHCI_PORT_CTL_W1_MASK), ("val=%X\n", val));
AssertMsg(!(val & XHCI_PORT_LWS), ("val=%X\n", val));
AssertMsg((val & ~(XHCI_PORT_RO_MASK|XHCI_PORT_CTL_W1_MASK|XHCI_PORT_PLS_MASK|XHCI_PORT_CHANGE_MASK)) == (p->portsc & ~(XHCI_PORT_RO_MASK|XHCI_PORT_CTL_W1_MASK|XHCI_PORT_PLS_MASK|XHCI_PORT_CHANGE_MASK)), ("val=%X vs. portsc=%X\n", val, p->portsc));
return VINF_SUCCESS;
}
/* Actual USB port state changes need to be done in R3. */
#ifdef IN_RING3
Log(("HcPortStatusCtrl_w port %u: old=%x new=%x\n", IDX_TO_ID(iPort), p->portsc, val));
Assert(!(pThis->hcc_params & XHCI_HCC_PPC));
Assert(p->portsc & XHCI_PORT_PP);
if (val & XHCI_PORT_RESERVED)
Log(("Reserved bits set %x!\n", val & XHCI_PORT_RESERVED));
/* A write to clear any of the change notification bits. */
if (val & XHCI_PORT_CHANGE_MASK)
p->portsc &= ~(val & XHCI_PORT_CHANGE_MASK);
/* Writing the Port Enable/Disable bit as 1 disables a port; it cannot be
* enabled that way. Writing the bit as zero does does nothing.
*/
if ((val & XHCI_PORT_PED) && (p->portsc & XHCI_PORT_PED))
{
p->portsc &= ~XHCI_PORT_PED;
Log(("HcPortStatusCtrl_w(): port %u: DISABLE\n", IDX_TO_ID(iPort)));
}
if (!(val & XHCI_PORT_PP) && (p->portsc & XHCI_PORT_PP))
{
p->portsc &= ~XHCI_PORT_PP;
Log(("HcPortStatusCtrl_w(): port %u: POWER OFF\n", IDX_TO_ID(iPort)));
}
/* Warm Port Reset - USB3 only; see 4.19.5.1. */
if ((val & XHCI_PORT_WPR) && IS_USB3_PORT_IDX_SHR(pThis, iPort))
{
Log(("HcPortStatusCtrl_w(): port %u: WARM RESET\n", IDX_TO_ID(iPort)));
if (xhciR3RhPortSetIfConnected(pThis, iPort, XHCI_PORT_PR | XHCI_PORT_WPR))
{
PXHCIROOTHUBR3 pRh = GET_PORT_PRH(pThisCC, iPort);
VUSBIRhDevReset(pRh->pIRhConn, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort), false /* don't reset on linux */, NULL /* sync */, NULL, PDMDevHlpGetVM(pDevIns));
xhciR3PortResetDone(pDevIns, iPort);
}
}
if (val & XHCI_PORT_PR)
{
Log(("HcPortStatusCtrl_w(): port %u: RESET\n", IDX_TO_ID(iPort)));
if (xhciR3RhPortSetIfConnected(pThis, iPort, XHCI_PORT_PR))
{
PXHCIROOTHUBR3 pRh = GET_PORT_PRH(pThisCC, iPort);
VUSBIRhDevReset(pRh->pIRhConn, GET_VUSB_PORT_FROM_XHCI_PORT(pRh, iPort), false /* don't reset on linux */, NULL /* sync */, NULL, PDMDevHlpGetVM(pDevIns));
xhciR3PortResetDone(pDevIns, iPort);
}
else if (p->portsc & XHCI_PORT_PR)
{
/* the guest is getting impatient. */
Log2(("HcPortStatusCtrl_w(): port %u: Impatient guest!\n", IDX_TO_ID(iPort)));
RTThreadYield();
}
}
/// @todo Do some sanity checking on the new link state?
/* Update the link state if requested. */
if (val & XHCI_PORT_LWS)
{
unsigned old_pls;
unsigned new_pls;
old_pls = (p->portsc & XHCI_PORT_PLS_MASK) >> XHCI_PORT_PLS_SHIFT;
new_pls = (val & XHCI_PORT_PLS_MASK) >> XHCI_PORT_PLS_SHIFT;
p->portsc &= ~XHCI_PORT_PLS_MASK;
p->portsc |= new_pls << XHCI_PORT_PLS_SHIFT;
Log2(("HcPortStatusCtrl_w(): port %u: Updating link state from %u to %u\n", IDX_TO_ID(iPort), old_pls, new_pls));
/* U3->U0 (USB3) and Resume->U0 transitions set the PLC flag. See 4.15.2.2 */
if (new_pls == XHCI_PLS_U0)
if (old_pls == XHCI_PLS_U3 || old_pls == XHCI_PLS_RESUME)
{
p->portsc |= XHCI_PORT_PLC;
xhciR3GenPortChgEvent(pDevIns, pThis, IDX_TO_ID(iPort));
}
}
/// @todo which other bits can we safely ignore?
/* Update the wake mask. */
p->portsc &= ~XHCI_PORT_WAKE_MASK;
p->portsc |= val & XHCI_PORT_WAKE_MASK;
return VINF_SUCCESS;
#else /* !IN_RING3 */
RT_NOREF(pDevIns);
return VINF_IOM_R3_MMIO_WRITE;
#endif /* !IN_RING3 */
}
/**
* Read the PORTPMSC register.
*/
static VBOXSTRICTRC HcPortPowerMgmt_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iPort, uint32_t *pu32Value)
{
PXHCIHUBPORT p = &pThis->aPorts[iPort];
RT_NOREF(pDevIns);
STAM_COUNTER_INC(&pThis->StatRdPortPowerMgmt);
*pu32Value = p->portpm;
return VINF_SUCCESS;
}
/**
* Write the PORTPMSC register.
*/
static VBOXSTRICTRC HcPortPowerMgmt_w(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iPort, uint32_t val)
{
PXHCIHUBPORT p = &pThis->aPorts[iPort];
RT_NOREF(pDevIns);
STAM_COUNTER_INC(&pThis->StatWrPortPowerMgmt);
/// @todo anything to do here?
p->portpm = val;
return VINF_SUCCESS;
}
/**
* Read the PORTLI register.
*/
static VBOXSTRICTRC HcPortLinkInfo_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iPort, uint32_t *pu32Value)
{
PXHCIHUBPORT p = &pThis->aPorts[iPort];
RT_NOREF(pDevIns);
STAM_COUNTER_INC(&pThis->StatRdPortLinkInfo);
/* The link information is R/O; we probably can't get it at all. If we
* do maintain it for USB3 ports, we also have to reset it (5.4.10).
*/
*pu32Value = p->portli;
return VINF_SUCCESS;
}
/**
* Read the reserved register. Linux likes to do this.
*/
static VBOXSTRICTRC HcPortRsvd_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iPort, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis, iPort);
STAM_COUNTER_INC(&pThis->StatRdPortRsvd);
*pu32Value = 0;
return VINF_SUCCESS;
}
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* xHCI Interrupter Register access routines */
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/**
* Read the IMAN register.
*/
static VBOXSTRICTRC HcIntrMgmt_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatRdIntrMgmt);
*pu32Value = ip->iman;
return VINF_SUCCESS;
}
/**
* Write the IMAN register.
*/
static VBOXSTRICTRC HcIntrMgmt_w(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t val)
{
uint32_t uNew = val & XHCI_IMAN_VALID_MASK;
STAM_COUNTER_INC(&pThis->StatWrIntrMgmt);
if (val & ~XHCI_IMAN_VALID_MASK)
Log(("Reserved bits set %x!\n", val & ~XHCI_IMAN_VALID_MASK));
/* If the Interrupt Pending (IP) bit is set, writing one clears it.
* Note that when MSIs are enabled, the bit auto-clears almost immediately.
*/
if (val & ip->iman & XHCI_IMAN_IP)
{
Log2(("clearing interrupt on interrupter %u\n", ip->index));
PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
STAM_COUNTER_INC(&pThis->StatIntrsCleared);
uNew &= ~XHCI_IMAN_IP;
}
else
{
/* Preserve the current IP bit. */
uNew = (uNew & ~XHCI_IMAN_IP) | (ip->iman & XHCI_IMAN_IP);
}
/* Trigger an interrupt if the IP bit is set and IE transitions from 0 to 1. */
if ( (uNew & XHCI_IMAN_IE)
&& !(ip->iman & XHCI_IMAN_IE)
&& (ip->iman & XHCI_IMAN_IP)
&& (pThis->cmd & XHCI_CMD_INTE))
PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
ip->iman = uNew;
return VINF_SUCCESS;
}
/**
* Read the IMOD register.
*/
static VBOXSTRICTRC HcIntrMod_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatRdIntrMod);
*pu32Value = ip->imod;
return VINF_SUCCESS;
}
/**
* Write the IMOD register.
*/
static VBOXSTRICTRC HcIntrMod_w(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t val)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatWrIntrMod);
/// @todo Does writing a zero to IMODC/IMODI potentially trigger
/// an interrupt?
ip->imod = val;
return VINF_SUCCESS;
}
/**
* Read the ERSTSZ register.
*/
static VBOXSTRICTRC HcEvtRSTblSize_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatRdEvtRstblSize);
*pu32Value = ip->erstsz;
return VINF_SUCCESS;
}
/**
* Write the ERSTSZ register.
*/
static VBOXSTRICTRC HcEvtRSTblSize_w(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t val)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatWrEvtRstblSize);
if (val & ~XHCI_ERSTSZ_MASK)
Log(("Reserved bits set %x!\n", val & ~XHCI_ERSTSZ_MASK));
if (val > XHCI_ERSTMAX)
Log(("ERSTSZ (%u) > ERSTMAX (%u)!\n", val, XHCI_ERSTMAX));
/* Enforce the maximum size. */
ip->erstsz = RT_MIN(val, XHCI_ERSTMAX);
if (!ip->index && !ip->erstsz) /* Windows 8 does this temporarily. Thanks guys... */
Log(("ERSTSZ is zero for primary interrupter: undefined behavior!\n"));
return VINF_SUCCESS;
}
/**
* Read the reserved register. Linux likes to do this.
*/
static VBOXSTRICTRC HcEvtRsvd_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis, ip);
STAM_COUNTER_INC(&pThis->StatRdEvtRsvd);
*pu32Value = 0;
return VINF_SUCCESS;
}
/**
* Read the low dword of the ERSTBA register.
*/
static VBOXSTRICTRC HcEvtRSTblBaseLo_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatRdEvtRsTblBaseLo);
*pu32Value = (uint32_t)ip->erstba;
return VINF_SUCCESS;
}
/**
* Write the low dword of the ERSTBA register.
*/
static VBOXSTRICTRC HcEvtRSTblBaseLo_w(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t val)
{
STAM_COUNTER_INC(&pThis->StatWrEvtRsTblBaseLo);
if (val & ~pThis->erst_addr_mask)
Log(("Reserved bits set %x!\n", val & ~pThis->erst_addr_mask));
/* NB: A dword write to the low half clears the high half. */
ip->erstba = val & pThis->erst_addr_mask;
/* Initialize the internal event ring state. */
ip->evtr_pcs = 1;
ip->erst_idx = 0;
ip->ipe = false;
/* Fetch the first ERST entry now. Not later! That "sets the Event Ring
* State Machine:EREP Advancement to the Start state"
*/
xhciFetchErstEntry(pDevIns, pThis, ip);
return VINF_SUCCESS;
}
/**
* Read the high dword of the ERSTBA register.
*/
static VBOXSTRICTRC HcEvtRSTblBaseHi_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatRdEvtRsTblBaseHi);
*pu32Value = (uint32_t)(ip->erstba >> 32);
return VINF_SUCCESS;
}
/**
* Write the high dword of the ERSTBA register.
*/
static VBOXSTRICTRC HcEvtRSTblBaseHi_w(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t val)
{
STAM_COUNTER_INC(&pThis->StatWrEvtRsTblBaseHi);
/* Update the high dword while preserving the low one. */
ip->erstba = ((uint64_t)val << 32) | (uint32_t)ip->erstba;
/* We shouldn't be doing this when AC64 is set. But High Sierra
* ignores that because it "knows" the xHC handles 64-bit addressing,
* so we're going to assume that OSes are not going to write junk into
* ERSTBAH when they don't see AC64 set.
*/
xhciFetchErstEntry(pDevIns, pThis, ip);
return VINF_SUCCESS;
}
/**
* Read the low dword of the ERDP register.
*/
static VBOXSTRICTRC HcEvtRingDeqPtrLo_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pThis);
STAM_COUNTER_INC(&pThis->StatRdEvtRingDeqPtrLo);
/* Lock to avoid incomplete update being seen. */
int rc = PDMDevHlpCritSectEnter(pDevIns, &ip->lock, VINF_IOM_R3_MMIO_READ);
if (rc != VINF_SUCCESS)
return rc;
*pu32Value = (uint32_t)ip->erdp;
PDMDevHlpCritSectLeave(pDevIns, &ip->lock);
return VINF_SUCCESS;
}
/**
* Write the low dword of the ERDP register.
*/
static VBOXSTRICTRC HcEvtRingDeqPtrLo_w(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t val)
{
uint64_t old_erdp;
uint64_t new_erdp;
STAM_COUNTER_INC(&pThis->StatWrEvtRingDeqPtrLo);
/* NB: A dword write to the low half clears the high half.
* The high dword should be ignored when AC64=0, but High Sierra
* does not care what we report. Therefore a write to the low dword
* handles all the control bits and a write to the high dword still
* updates the ERDP address. On a 64-bit host, there must be a
* back-to-back low dword + high dword access. We are going to boldly
* assume that the guest will not place the event ring across the 4G
* boundary (i.e. storing the bottom part in the firmware ROM).
*/
int rc = PDMDevHlpCritSectEnter(pDevIns, &ip->lock, VINF_IOM_R3_MMIO_WRITE);
if (rc != VINF_SUCCESS)
return rc;
old_erdp = ip->erdp & XHCI_ERDP_ADDR_MASK; /* Remember old ERDP address. */
new_erdp = ip->erdp & XHCI_ERDP_EHB; /* Preserve EHB */
/* If the Event Handler Busy (EHB) bit is set, writing a one clears it. */
if (val & ip->erdp & XHCI_ERDP_EHB)
{
Log2(("clearing EHB on interrupter %p\n", ip));
new_erdp &= ~XHCI_ERDP_EHB;
}
/// @todo Check if this might inadvertently set EHB!
new_erdp |= val & ~XHCI_ERDP_EHB;
ip->erdp = new_erdp;
/* Check if the ERDP changed. See workaround below. */
if (old_erdp != (new_erdp & XHCI_ERDP_ADDR_MASK))
ip->erdp_rewrites = 0;
else
++ip->erdp_rewrites;
LogFlowFunc(("ERDP: %RGp, EREP: %RGp\n", (RTGCPHYS)(ip->erdp & XHCI_ERDP_ADDR_MASK), (RTGCPHYS)ip->erep));
if ((ip->erdp & XHCI_ERDP_ADDR_MASK) == ip->erep)
{
Log2(("Event Ring empty, clearing IPE\n"));
ip->ipe = false;
}
else if (ip->ipe && (val & XHCI_ERDP_EHB))
{
/* EHB is being cleared but the ring isn't empty and IPE is still set. */
if (RT_UNLIKELY(old_erdp == (new_erdp & XHCI_ERDP_ADDR_MASK) && ip->erdp_rewrites > 2))
{
/* If guest does not advance the ERDP, do not trigger an interrupt
* again. Workaround for buggy xHCI initialization in Linux 4.6 which
* enables interrupts before setting up internal driver state. That
* leads to the guest IRQ handler not actually handling events and
* infinitely re-triggering interrupts. However, only do this if the
* guest has already written the same ERDP value a few times. The Intel
* xHCI driver always writes the same ERDP twice and we must still
* re-trigger interrupts in that case.
* See @bugref{8546}.
*/
Log2(("Event Ring not empty, ERDP not advanced, not re-triggering interrupt!\n"));
ip->ipe = false;
}
else
{
Log2(("Event Ring not empty, re-triggering interrupt\n"));
xhciSetIntr(pDevIns, pThis, ip);
}
}
PDMDevHlpCritSectLeave(pDevIns, &ip->lock);
return VINF_SUCCESS;
}
/**
* Read the high dword of the ERDP register.
*/
static VBOXSTRICTRC HcEvtRingDeqPtrHi_r(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t *pu32Value)
{
RT_NOREF(pDevIns, pThis);
STAM_COUNTER_INC(&pThis->StatRdEvtRingDeqPtrHi);
*pu32Value = (uint32_t)(ip->erdp >> 32);
return VINF_SUCCESS;
}
/**
* Write the high dword of the ERDP register.
*/
static VBOXSTRICTRC HcEvtRingDeqPtrHi_w(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR ip, uint32_t val)
{
RT_NOREF(pThis);
STAM_COUNTER_INC(&pThis->StatWrEvtRingDeqPtrHi);
/* See HcEvtRingDeqPtrLo_w for semantics. */
int rc = PDMDevHlpCritSectEnter(pDevIns, &ip->lock, VINF_IOM_R3_MMIO_WRITE);
if (rc != VINF_SUCCESS)
return rc;
/* Update the high dword while preserving the low one. */
ip->erdp = ((uint64_t)val << 32) | (uint32_t)ip->erdp;
PDMDevHlpCritSectLeave(pDevIns, &ip->lock);
return VINF_SUCCESS;
}
/**
* xHCI register access routines.
*/
typedef struct
{
const char *pszName;
VBOXSTRICTRC (*pfnRead )(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t *pu32Value);
VBOXSTRICTRC (*pfnWrite)(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t iReg, uint32_t u32Value);
} XHCIREGACC;
/**
* xHCI interrupter register access routines.
*/
typedef struct
{
const char *pszName;
VBOXSTRICTRC (*pfnIntrRead )(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR pIntr, uint32_t *pu32Value);
VBOXSTRICTRC (*pfnIntrWrite)(PPDMDEVINS pDevIns, PXHCI pThis, PXHCIINTRPTR pIntr, uint32_t u32Value);
} XHCIINTRREGACC;
/**
* Operational registers descriptor table.
*/
static const XHCIREGACC g_aOpRegs[] =
{
{"USBCMD" , HcUsbcmd_r, HcUsbcmd_w },
{"USBSTS", HcUsbsts_r, HcUsbsts_w },
{"PAGESIZE", HcPagesize_r, NULL },
{"Unused", NULL, NULL },
{"Unused", NULL, NULL },
{"DNCTRL", HcDevNotifyCtrl_r, HcDevNotifyCtrl_w },
{"CRCRL", HcCmdRingCtlLo_r, HcCmdRingCtlLo_w },
{"CRCRH", HcCmdRingCtlHi_r, HcCmdRingCtlHi_w },
{"Unused", NULL, NULL },
{"Unused", NULL, NULL },
{"Unused", NULL, NULL },
{"Unused", NULL, NULL },
{"DCBAAPL", HcDevCtxBAAPLo_r, HcDevCtxBAAPLo_w },
{"DCBAAPH", HcDevCtxBAAPHi_r, HcDevCtxBAAPHi_w },
{"CONFIG", HcConfig_r, HcConfig_w }
};
/**
* Port registers descriptor table (for a single port). The number of ports
* and their associated registers depends on the NDP value.
*/
static const XHCIREGACC g_aPortRegs[] =
{
/*
*/
{"PORTSC", HcPortStatusCtrl_r, HcPortStatusCtrl_w },
{"PORTPMSC", HcPortPowerMgmt_r, HcPortPowerMgmt_w },
{"PORTLI", HcPortLinkInfo_r, NULL },
{"Reserved", HcPortRsvd_r, NULL }
};
AssertCompile(RT_ELEMENTS(g_aPortRegs) * sizeof(uint32_t) == 0x10);
/**
* Interrupter runtime registers descriptor table (for a single interrupter).
* The number of interrupters depends on the XHCI_NINTR value.
*/
static const XHCIINTRREGACC g_aIntrRegs[] =
{
{"IMAN", HcIntrMgmt_r, HcIntrMgmt_w },
{"IMOD", HcIntrMod_r, HcIntrMod_w },
{"ERSTSZ", HcEvtRSTblSize_r, HcEvtRSTblSize_w },
{"Reserved", HcEvtRsvd_r, NULL },
{"ERSTBAL", HcEvtRSTblBaseLo_r, HcEvtRSTblBaseLo_w },
{"ERSTBAH", HcEvtRSTblBaseHi_r, HcEvtRSTblBaseHi_w },
{"ERDPL", HcEvtRingDeqPtrLo_r, HcEvtRingDeqPtrLo_w },
{"ERDPH", HcEvtRingDeqPtrHi_r, HcEvtRingDeqPtrHi_w }
};
AssertCompile(RT_ELEMENTS(g_aIntrRegs) * sizeof(uint32_t) == 0x20);
/**
* Read the MFINDEX register.
*/
static int HcMfIndex_r(PPDMDEVINS pDevIns, PXHCI pThis, uint32_t *pu32Value)
{
uint64_t uNanoTime;
uint64_t uMfTime;
STAM_COUNTER_INC(&pThis->StatRdMfIndex);
/* MFINDEX increments once per micro-frame, i.e. 8 times per millisecond
* or every 125us. The resolution is only 14 bits, meaning that MFINDEX
* wraps around after it reaches 0x3FFF (16383) or every 2048 milliseconds.
*/
/// @todo MFINDEX should only be running when R/S is set. May not matter.
uNanoTime = PDMDevHlpTimerGet(pDevIns, pThis->hWrapTimer);
uMfTime = uNanoTime / 125000;
*pu32Value = uMfTime & 0x3FFF;
Log2(("MFINDEX read: %u\n", *pu32Value));
return VINF_SUCCESS;
}
/**
* @callback_method_impl{FNIOMMMIONEWREAD, Read a MMIO register.}
*
* @note We only accept 32-bit writes that are 32-bit aligned.
*/
static DECLCALLBACK(VBOXSTRICTRC) xhciMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
const uint32_t offReg = (uint32_t)off;
uint32_t * const pu32 = (uint32_t *)pv;
uint32_t iReg;
RT_NOREF(pvUser);
Log2(("xhciRead %RGp (offset %04X) size=%d\n", off, offReg, cb));
if (offReg < XHCI_CAPS_REG_SIZE)
{
switch (offReg)
{
case 0x0: /* CAPLENGTH + HCIVERSION */
*pu32 = (pThis->hci_version << 16) | pThis->cap_length;
break;
case 0x4: /* HCSPARAMS1 (structural) */
Log2(("HCSPARAMS1 read\n"));
*pu32 = pThis->hcs_params1;
break;
case 0x8: /* HCSPARAMS2 (structural) */
Log2(("HCSPARAMS2 read\n"));
*pu32 = pThis->hcs_params2;
break;
case 0xC: /* HCSPARAMS3 (structural) */
Log2(("HCSPARAMS3 read\n"));
*pu32 = pThis->hcs_params3;
break;
case 0x10: /* HCCPARAMS1 (caps) */
Log2(("HCCPARAMS1 read\n"));
*pu32 = pThis->hcc_params;
break;
case 0x14: /* DBOFF (doorbell offset) */
Log2(("DBOFF read\n"));
*pu32 = pThis->dbell_off;
break;
case 0x18: /* RTSOFF (run-time register offset) */
Log2(("RTSOFF read\n"));
*pu32 = pThis->rts_off;
break;
case 0x1C: /* HCCPARAMS2 (caps) */
Log2(("HCCPARAMS2 read\n"));
*pu32 = 0; /* xHCI 1.1 only */
break;
default:
Log(("xHCI: Trying to read unknown capability register %u!\n", offReg));
STAM_COUNTER_INC(&pThis->StatRdUnknown);
return VINF_IOM_MMIO_UNUSED_FF;
}
STAM_COUNTER_INC(&pThis->StatRdCaps);
Log2(("xhciRead %RGp size=%d -> val=%x\n", off, cb, *pu32));
return VINF_SUCCESS;
}
/*
* Validate the access (in case of IOM bugs or incorrect MMIO registration).
*/
AssertMsgReturn(cb == sizeof(uint32_t), ("IOM bug? %RGp LB %d\n", off, cb),
VINF_IOM_MMIO_UNUSED_FF /* No idea what really would happen... */);
/** r=bird: If you don't have an idea what would happen for non-dword reads,
* then the flags passed to IOM when creating the MMIO region are doubtful, right? */
AssertMsgReturn(!(off & 0x3), ("IOM bug? %RGp LB %d\n", off, cb), VINF_IOM_MMIO_UNUSED_FF);
/*
* Validate the register and call the read operator.
*/
VBOXSTRICTRC rcStrict = VINF_IOM_MMIO_UNUSED_FF;
if (offReg >= XHCI_DOORBELL_OFFSET)
{
/* The doorbell registers are effectively write-only and return 0 when read. */
iReg = (offReg - XHCI_DOORBELL_OFFSET) >> 2;
if (iReg < XHCI_NDS)
{
STAM_COUNTER_INC(&pThis->StatRdDoorBell);
*pu32 = 0;
rcStrict = VINF_SUCCESS;
Log2(("xhciRead: DBellReg (DB %u) %RGp size=%d -> val=%x (rc=%d)\n", iReg, off, cb, *pu32, VBOXSTRICTRC_VAL(rcStrict)));
}
}
else if (offReg >= XHCI_RTREG_OFFSET)
{
/* Run-time registers. */
Assert(offReg < XHCI_DOORBELL_OFFSET);
/* The MFINDEX register would be interrupter -1... */
if (offReg < XHCI_RTREG_OFFSET + RT_ELEMENTS(g_aIntrRegs) * sizeof(uint32_t))
{
if (offReg == XHCI_RTREG_OFFSET)
rcStrict = HcMfIndex_r(pDevIns, pThis, pu32);
else
{
/* The silly Linux xHCI driver reads the reserved registers. */
STAM_COUNTER_INC(&pThis->StatRdUnknown);
*pu32 = 0;
rcStrict = VINF_SUCCESS;
}
}
else
{
Assert((offReg - XHCI_RTREG_OFFSET) / (RT_ELEMENTS(g_aIntrRegs) * sizeof(uint32_t)) > 0);
const uint32_t iIntr = (offReg - XHCI_RTREG_OFFSET) / (RT_ELEMENTS(g_aIntrRegs) * sizeof(uint32_t)) - 1;
if (iIntr < XHCI_NINTR)
{
iReg = (offReg >> 2) & (RT_ELEMENTS(g_aIntrRegs) - 1);
const XHCIINTRREGACC *pReg = &g_aIntrRegs[iReg];
if (pReg->pfnIntrRead)
{
PXHCIINTRPTR pIntr = &pThis->aInterrupters[iIntr];
rcStrict = pReg->pfnIntrRead(pDevIns, pThis, pIntr, pu32);
Log2(("xhciRead: IntrReg (intr %u): %RGp (%s) size=%d -> val=%x (rc=%d)\n", iIntr, off, pReg->pszName, cb, *pu32, VBOXSTRICTRC_VAL(rcStrict)));
}
}
}
}
else if (offReg >= XHCI_XECP_OFFSET)
{
/* Extended Capability registers. */
Assert(offReg < XHCI_RTREG_OFFSET);
uint32_t offXcp = offReg - XHCI_XECP_OFFSET;
if (offXcp + cb <= RT_MIN(pThis->cbExtCap, sizeof(pThis->abExtCap))) /* can't trust cbExtCap in ring-0. */
{
*pu32 = *(uint32_t *)&pThis->abExtCap[offXcp];
rcStrict = VINF_SUCCESS;
}
Log2(("xhciRead: ExtCapReg %RGp size=%d -> val=%x (rc=%d)\n", off, cb, *pu32, VBOXSTRICTRC_VAL(rcStrict)));
}
else
{
/* Operational registers (incl. port registers). */
Assert(offReg < XHCI_XECP_OFFSET);
iReg = (offReg - XHCI_CAPS_REG_SIZE) >> 2;
if (iReg < RT_ELEMENTS(g_aOpRegs))
{
const XHCIREGACC *pReg = &g_aOpRegs[iReg];
if (pReg->pfnRead)
{
rcStrict = pReg->pfnRead(pDevIns, pThis, iReg, pu32);
Log2(("xhciRead: OpReg %RGp (%s) size=%d -> val=%x (rc=%d)\n", off, pReg->pszName, cb, *pu32, VBOXSTRICTRC_VAL(rcStrict)));
}
}
else if (iReg >= (XHCI_PORT_REG_OFFSET >> 2))
{
iReg -= (XHCI_PORT_REG_OFFSET >> 2);
const uint32_t iPort = iReg / RT_ELEMENTS(g_aPortRegs);
if (iPort < XHCI_NDP_CFG(pThis))
{
iReg = (offReg >> 2) & (RT_ELEMENTS(g_aPortRegs) - 1);
Assert(iReg < RT_ELEMENTS(g_aPortRegs));
const XHCIREGACC *pReg = &g_aPortRegs[iReg];
if (pReg->pfnRead)
{
rcStrict = pReg->pfnRead(pDevIns, pThis, iPort, pu32);
Log2(("xhciRead: PortReg (port %u): %RGp (%s) size=%d -> val=%x (rc=%d)\n", IDX_TO_ID(iPort), off, pReg->pszName, cb, *pu32, VBOXSTRICTRC_VAL(rcStrict)));
}
}
}
}
if (rcStrict != VINF_IOM_MMIO_UNUSED_FF)
{ /* likely */ }
else
{
STAM_COUNTER_INC(&pThis->StatRdUnknown);
Log(("xHCI: Trying to read unimplemented register at offset %04X!\n", offReg));
}
return rcStrict;
}
/**
* @callback_method_impl{FNIOMMMIONEWWRITE, Write to a MMIO register.}
*
* @note We only accept 32-bit writes that are 32-bit aligned.
*/
static DECLCALLBACK(VBOXSTRICTRC) xhciMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
const uint32_t offReg = (uint32_t)off;
uint32_t * const pu32 = (uint32_t *)pv;
uint32_t iReg;
RT_NOREF(pvUser);
Log2(("xhciWrite %RGp (offset %04X) %x size=%d\n", off, offReg, *(uint32_t *)pv, cb));
if (offReg < XHCI_CAPS_REG_SIZE)
{
/* These are read-only */
Log(("xHCI: Trying to write to register %u!\n", offReg));
STAM_COUNTER_INC(&pThis->StatWrUnknown);
return VINF_SUCCESS;
}
/*
* Validate the access (in case of IOM bug or incorrect MMIO registration).
*/
AssertMsgReturn(cb == sizeof(uint32_t), ("IOM bug? %RGp LB %d\n", off, cb), VINF_SUCCESS);
AssertMsgReturn(!(off & 0x3), ("IOM bug? %RGp LB %d\n", off, cb), VINF_SUCCESS);
/*
* Validate the register and call the write operator.
*/
VBOXSTRICTRC rcStrict = VINF_IOM_MMIO_UNUSED_FF;
if (offReg >= XHCI_DOORBELL_OFFSET)
{
/* Let's spring into action... as long as the xHC is running. */
iReg = (offReg - XHCI_DOORBELL_OFFSET) >> 2;
if ((pThis->cmd & XHCI_CMD_RS) && iReg < XHCI_NDS)
{
if (iReg == 0)
{
/* DB0 aka Command Ring. */
STAM_COUNTER_INC(&pThis->StatWrDoorBell0);
if (*pu32 == 0)
{
/* Set the Command Ring state to Running if not already set. */
if (!(pThis->crcr & XHCI_CRCR_CRR))
{
Log(("Command ring entered Running state\n"));
ASMAtomicOrU64(&pThis->crcr, XHCI_CRCR_CRR);
}
xhciKickWorker(pDevIns, pThis, XHCI_JOB_PROCESS_CMDRING, 0);
}
else
Log2(("Ignoring DB0 write with value %X!\n", *pu32));
}
else
{
/* Device context doorbell. Do basic parameter checking to avoid
* waking up the worker thread needlessly.
*/
STAM_COUNTER_INC(&pThis->StatWrDoorBellN);
uint8_t uDBTarget = *pu32 & XHCI_DB_TGT_MASK;
Assert(uDBTarget < 32); /// @todo Report an error? Or just ignore?
if (uDBTarget < 32)
{
Log2(("Ring bell for slot %u, DCI %u\n", iReg, uDBTarget));
ASMAtomicOrU32(&pThis->aBellsRung[ID_TO_IDX(iReg)], 1 << uDBTarget);
xhciKickWorker(pDevIns, pThis, XHCI_JOB_DOORBELL, *pu32);
}
else
Log2(("Ignoring DB%u write with bad target %u!\n", iReg, uDBTarget));
}
rcStrict = VINF_SUCCESS;
Log2(("xhciWrite: DBellReg (DB %u) %RGp size=%d <- val=%x (rc=%d)\n", iReg, off, cb, *(uint32_t *)pv, VBOXSTRICTRC_VAL(rcStrict)));
}
}
else if (offReg >= XHCI_RTREG_OFFSET)
{
/* Run-time registers. */
Assert(offReg < XHCI_DOORBELL_OFFSET);
/* NB: The MFINDEX register is R/O. */
if (offReg >= XHCI_RTREG_OFFSET + (RT_ELEMENTS(g_aIntrRegs) * sizeof(uint32_t)))
{
Assert((offReg - XHCI_RTREG_OFFSET) / (RT_ELEMENTS(g_aIntrRegs) * sizeof(uint32_t)) > 0);
const uint32_t iIntr = (offReg - XHCI_RTREG_OFFSET) / (RT_ELEMENTS(g_aIntrRegs) * sizeof(uint32_t)) - 1;
if (iIntr < XHCI_NINTR)
{
iReg = (offReg >> 2) & (RT_ELEMENTS(g_aIntrRegs) - 1);
const XHCIINTRREGACC *pReg = &g_aIntrRegs[iReg];
if (pReg->pfnIntrWrite)
{
PXHCIINTRPTR pIntr = &pThis->aInterrupters[iIntr];
rcStrict = pReg->pfnIntrWrite(pDevIns, pThis, pIntr, *pu32);
Log2(("xhciWrite: IntrReg (intr %u): %RGp (%s) size=%d <- val=%x (rc=%d)\n", iIntr, off, pReg->pszName, cb, *pu32, VBOXSTRICTRC_VAL(rcStrict)));
}
}
}
}
else
{
/* Operational registers (incl. port registers). */
Assert(offReg < XHCI_RTREG_OFFSET);
iReg = (offReg - pThis->cap_length) >> 2;
if (iReg < RT_ELEMENTS(g_aOpRegs))
{
const XHCIREGACC *pReg = &g_aOpRegs[iReg];
if (pReg->pfnWrite)
{
rcStrict = pReg->pfnWrite(pDevIns, pThis, iReg, *(uint32_t *)pv);
Log2(("xhciWrite: OpReg %RGp (%s) size=%d <- val=%x (rc=%d)\n", off, pReg->pszName, cb, *(uint32_t *)pv, VBOXSTRICTRC_VAL(rcStrict)));
}
}
else if (iReg >= (XHCI_PORT_REG_OFFSET >> 2))
{
iReg -= (XHCI_PORT_REG_OFFSET >> 2);
const uint32_t iPort = iReg / RT_ELEMENTS(g_aPortRegs);
if (iPort < XHCI_NDP_CFG(pThis))
{
iReg = (offReg >> 2) & (RT_ELEMENTS(g_aPortRegs) - 1);
Assert(iReg < RT_ELEMENTS(g_aPortRegs));
const XHCIREGACC *pReg = &g_aPortRegs[iReg];
if (pReg->pfnWrite)
{
rcStrict = pReg->pfnWrite(pDevIns, pThis, iPort, *pu32);
Log2(("xhciWrite: PortReg (port %u): %RGp (%s) size=%d <- val=%x (rc=%d)\n", IDX_TO_ID(iPort), off, pReg->pszName, cb, *pu32, VBOXSTRICTRC_VAL(rcStrict)));
}
}
}
}
if (rcStrict != VINF_IOM_MMIO_UNUSED_FF)
{ /* likely */ }
else
{
/* Ignore writes to unimplemented or read-only registers. */
STAM_COUNTER_INC(&pThis->StatWrUnknown);
Log(("xHCI: Trying to write unimplemented or R/O register at offset %04X!\n", offReg));
rcStrict = VINF_SUCCESS;
}
return rcStrict;
}
#ifdef IN_RING3
/**
* @callback_method_impl{FNTMTIMERDEV,
* Provides periodic MFINDEX wrap events. See 4.14.2.}
*/
static DECLCALLBACK(void) xhciR3WrapTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
{
PXHCI pThis = (PXHCI)pvUser;
XHCI_EVENT_TRB ed;
LogFlow(("xhciR3WrapTimer:\n"));
RT_NOREF(hTimer);
/*
* Post the MFINDEX Wrap event and rearm the timer. Only called
* when the EWE bit is set in command register.
*/
RT_ZERO(ed);
ed.mwe.cc = XHCI_TCC_SUCCESS;
ed.mwe.type = XHCI_TRB_MFIDX_WRAP;
xhciR3WriteEvent(pDevIns, pThis, &ed, XHCI_PRIMARY_INTERRUPTER, false);
xhciSetWrapTimer(pDevIns, pThis);
}
/**
* @callback_method_impl{FNSSMDEVSAVEEXEC}
*/
static DECLCALLBACK(int) xhciR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
uint32_t iPort;
uint32_t iSlot;
uint32_t iIntr;
LogFlow(("xhciR3SaveExec: \n"));
/* Save HC operational registers. */
pHlp->pfnSSMPutU32(pSSM, pThis->cmd);
pHlp->pfnSSMPutU32(pSSM, pThis->status);
pHlp->pfnSSMPutU32(pSSM, pThis->dnctrl);
pHlp->pfnSSMPutU64(pSSM, pThis->crcr);
pHlp->pfnSSMPutU64(pSSM, pThis->dcbaap);
pHlp->pfnSSMPutU32(pSSM, pThis->config);
/* Save HC non-register state. */
pHlp->pfnSSMPutU64(pSSM, pThis->cmdr_dqp);
pHlp->pfnSSMPutBool(pSSM, pThis->cmdr_ccs);
/* Save per-slot state. */
pHlp->pfnSSMPutU32(pSSM, XHCI_NDS);
for (iSlot = 0; iSlot < XHCI_NDS; ++iSlot)
{
pHlp->pfnSSMPutU8 (pSSM, pThis->aSlotState[iSlot]);
pHlp->pfnSSMPutU32(pSSM, pThis->aBellsRung[iSlot]);
}
/* Save root hub (port) state. */
pHlp->pfnSSMPutU32(pSSM, XHCI_NDP_CFG(pThis));
for (iPort = 0; iPort < XHCI_NDP_CFG(pThis); ++iPort)
{
pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[iPort].portsc);
pHlp->pfnSSMPutU32(pSSM, pThis->aPorts[iPort].portpm);
}
/* Save interrupter state. */
pHlp->pfnSSMPutU32(pSSM, XHCI_NINTR);
for (iIntr = 0; iIntr < XHCI_NINTR; ++iIntr)
{
pHlp->pfnSSMPutU32(pSSM, pThis->aInterrupters[iIntr].iman);
pHlp->pfnSSMPutU32(pSSM, pThis->aInterrupters[iIntr].imod);
pHlp->pfnSSMPutU32(pSSM, pThis->aInterrupters[iIntr].erstsz);
pHlp->pfnSSMPutU64(pSSM, pThis->aInterrupters[iIntr].erstba);
pHlp->pfnSSMPutU64(pSSM, pThis->aInterrupters[iIntr].erdp);
pHlp->pfnSSMPutU64(pSSM, pThis->aInterrupters[iIntr].erep);
pHlp->pfnSSMPutU16(pSSM, pThis->aInterrupters[iIntr].erst_idx);
pHlp->pfnSSMPutU16(pSSM, pThis->aInterrupters[iIntr].trb_count);
pHlp->pfnSSMPutBool(pSSM, pThis->aInterrupters[iIntr].evtr_pcs);
pHlp->pfnSSMPutBool(pSSM, pThis->aInterrupters[iIntr].ipe);
}
/* Terminator marker. */
pHlp->pfnSSMPutU32(pSSM, UINT32_MAX);
/* If not continuing after save, force HC into non-running state to avoid trouble later. */
if (pHlp->pfnSSMHandleGetAfter(pSSM) != SSMAFTER_CONTINUE)
pThis->cmd &= ~XHCI_CMD_RS;
return VINF_SUCCESS;
}
/**
* @callback_method_impl{FNSSMDEVLOADEXEC}
*/
static DECLCALLBACK(int) xhciR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
int rc;
uint32_t cPorts;
uint32_t iPort;
uint32_t cSlots;
uint32_t iSlot;
uint32_t cIntrs;
uint32_t iIntr;
uint64_t u64Dummy;
uint32_t u32Dummy;
uint16_t u16Dummy;
uint8_t u8Dummy;
bool fDummy;
LogFlow(("xhciR3LoadExec:\n"));
Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
if (uVersion != XHCI_SAVED_STATE_VERSION)
return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
/* Load HC operational registers. */
pHlp->pfnSSMGetU32(pSSM, &pThis->cmd);
pHlp->pfnSSMGetU32(pSSM, &pThis->status);
pHlp->pfnSSMGetU32(pSSM, &pThis->dnctrl);
pHlp->pfnSSMGetU64(pSSM, &pThis->crcr);
pHlp->pfnSSMGetU64(pSSM, &pThis->dcbaap);
pHlp->pfnSSMGetU32(pSSM, &pThis->config);
/* Load HC non-register state. */
pHlp->pfnSSMGetU64(pSSM, &pThis->cmdr_dqp);
pHlp->pfnSSMGetBool(pSSM, &pThis->cmdr_ccs);
/* Load per-slot state. */
rc = pHlp->pfnSSMGetU32(pSSM, &cSlots);
AssertRCReturn(rc, rc);
if (cSlots > 256) /* Sanity check. */
return VERR_SSM_INVALID_STATE;
for (iSlot = 0; iSlot < cSlots; ++iSlot)
{
/* Load only as many slots as we have; discard any extras. */
if (iSlot < XHCI_NDS)
{
pHlp->pfnSSMGetU8 (pSSM, &pThis->aSlotState[iSlot]);
pHlp->pfnSSMGetU32(pSSM, &pThis->aBellsRung[iSlot]);
}
else
{
pHlp->pfnSSMGetU8 (pSSM, &u8Dummy);
pHlp->pfnSSMGetU32(pSSM, &u32Dummy);
}
}
/* Load root hub (port) state. */
rc = pHlp->pfnSSMGetU32(pSSM, &cPorts);
AssertRCReturn(rc, rc);
if (cPorts > 256) /* Sanity check. */
return VERR_SSM_INVALID_STATE;
for (iPort = 0; iPort < cPorts; ++iPort)
{
/* Load only as many ports as we have; discard any extras. */
if (iPort < XHCI_NDP_CFG(pThis))
{
pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[iPort].portsc);
pHlp->pfnSSMGetU32(pSSM, &pThis->aPorts[iPort].portpm);
}
else
{
pHlp->pfnSSMGetU32(pSSM, &u32Dummy);
pHlp->pfnSSMGetU32(pSSM, &u32Dummy);
}
}
/* Load interrupter state. */
rc = pHlp->pfnSSMGetU32(pSSM, &cIntrs);
AssertRCReturn(rc, rc);
if (cIntrs > 256) /* Sanity check. */
return VERR_SSM_INVALID_STATE;
for (iIntr = 0; iIntr < cIntrs; ++iIntr)
{
/* Load only as many interrupters as we have; discard any extras. */
if (iIntr < XHCI_NINTR)
{
pHlp->pfnSSMGetU32(pSSM, &pThis->aInterrupters[iIntr].iman);
pHlp->pfnSSMGetU32(pSSM, &pThis->aInterrupters[iIntr].imod);
pHlp->pfnSSMGetU32(pSSM, &pThis->aInterrupters[iIntr].erstsz);
pHlp->pfnSSMGetU64(pSSM, &pThis->aInterrupters[iIntr].erstba);
pHlp->pfnSSMGetU64(pSSM, &pThis->aInterrupters[iIntr].erdp);
pHlp->pfnSSMGetU64(pSSM, &pThis->aInterrupters[iIntr].erep);
pHlp->pfnSSMGetU16(pSSM, &pThis->aInterrupters[iIntr].erst_idx);
pHlp->pfnSSMGetU16(pSSM, &pThis->aInterrupters[iIntr].trb_count);
pHlp->pfnSSMGetBool(pSSM, &pThis->aInterrupters[iIntr].evtr_pcs);
pHlp->pfnSSMGetBool(pSSM, &pThis->aInterrupters[iIntr].ipe);
}
else
{
pHlp->pfnSSMGetU32(pSSM, &u32Dummy);
pHlp->pfnSSMGetU32(pSSM, &u32Dummy);
pHlp->pfnSSMGetU32(pSSM, &u32Dummy);
pHlp->pfnSSMGetU64(pSSM, &u64Dummy);
pHlp->pfnSSMGetU64(pSSM, &u64Dummy);
pHlp->pfnSSMGetU64(pSSM, &u64Dummy);
pHlp->pfnSSMGetU16(pSSM, &u16Dummy);
pHlp->pfnSSMGetU16(pSSM, &u16Dummy);
pHlp->pfnSSMGetBool(pSSM, &fDummy);
pHlp->pfnSSMGetBool(pSSM, &fDummy);
}
}
/* Terminator marker. */
rc = pHlp->pfnSSMGetU32(pSSM, &u32Dummy);
AssertRCReturn(rc, rc);
AssertReturn(u32Dummy == UINT32_MAX, VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
return rc;
}
/* -=-=-=-=- DBGF -=-=-=-=- */
/**
* @callback_method_impl{FNDBGFHANDLERDEV, Dumps xHCI state.}
*/
static DECLCALLBACK(void) xhciR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
RTGCPHYS GPAddr;
bool fVerbose = false;
unsigned i, j;
uint64_t u64Val;
/* Parse arguments. */
if (pszArgs)
fVerbose = strstr(pszArgs, "verbose") != NULL;
#ifdef XHCI_ERROR_INJECTION
if (pszArgs && strstr(pszArgs, "dropintrhw"))
{
pHlp->pfnPrintf(pHlp, "Dropping the next interrupt (external)!\n");
pThis->fDropIntrHw = true;
return;
}
if (pszArgs && strstr(pszArgs, "dropintrint"))
{
pHlp->pfnPrintf(pHlp, "Dropping the next interrupt (internal)!\n");
pThis->fDropIntrIpe = true;
return;
}
if (pszArgs && strstr(pszArgs, "dropurb"))
{
pHlp->pfnPrintf(pHlp, "Dropping the next URB!\n");
pThis->fDropUrb = true;
return;
}
#endif
/* Show basic information. */
pHlp->pfnPrintf(pHlp,
"%s#%d: PCI MMIO=%RGp IRQ=%u MSI=%s R0=%RTbool RC=%RTbool\n",
pDevIns->pReg->szName,
pDevIns->iInstance,
PDMDevHlpMmioGetMappingAddress(pDevIns, pThis->hMmio),
PCIDevGetInterruptLine(pDevIns->apPciDevs[0]),
#ifdef VBOX_WITH_MSI_DEVICES
xhciIsMSIEnabled(pDevIns->apPciDevs[0]) ? "on" : "off",
#else
"none",
#endif
pDevIns->fR0Enabled, pDevIns->fRCEnabled);
/* Command register. */
pHlp->pfnPrintf(pHlp, "USBCMD: %X:", pThis->cmd);
if (pThis->cmd & XHCI_CMD_EU3S) pHlp->pfnPrintf(pHlp, " EU3S" );
if (pThis->cmd & XHCI_CMD_EWE) pHlp->pfnPrintf(pHlp, " EWE" );
if (pThis->cmd & XHCI_CMD_CRS) pHlp->pfnPrintf(pHlp, " CRS" );
if (pThis->cmd & XHCI_CMD_CSS) pHlp->pfnPrintf(pHlp, " CSS" );
if (pThis->cmd & XHCI_CMD_LCRST) pHlp->pfnPrintf(pHlp, " LCRST" );
if (pThis->cmd & XHCI_CMD_HSEE) pHlp->pfnPrintf(pHlp, " HSEE" );
if (pThis->cmd & XHCI_CMD_INTE) pHlp->pfnPrintf(pHlp, " INTE" );
if (pThis->cmd & XHCI_CMD_HCRST) pHlp->pfnPrintf(pHlp, " HCRST" );
if (pThis->cmd & XHCI_CMD_RS) pHlp->pfnPrintf(pHlp, " RS" );
pHlp->pfnPrintf(pHlp, "\n");
/* Status register. */
pHlp->pfnPrintf(pHlp, "USBSTS: %X:", pThis->status);
if (pThis->status & XHCI_STATUS_HCH) pHlp->pfnPrintf(pHlp, " HCH" );
if (pThis->status & XHCI_STATUS_HSE) pHlp->pfnPrintf(pHlp, " HSE" );
if (pThis->status & XHCI_STATUS_EINT) pHlp->pfnPrintf(pHlp, " EINT" );
if (pThis->status & XHCI_STATUS_PCD) pHlp->pfnPrintf(pHlp, " PCD" );
if (pThis->status & XHCI_STATUS_SSS) pHlp->pfnPrintf(pHlp, " SSS" );
if (pThis->status & XHCI_STATUS_RSS) pHlp->pfnPrintf(pHlp, " RSS" );
if (pThis->status & XHCI_STATUS_SRE) pHlp->pfnPrintf(pHlp, " SRE" );
if (pThis->status & XHCI_STATUS_CNR) pHlp->pfnPrintf(pHlp, " CNR" );
if (pThis->status & XHCI_STATUS_HCE) pHlp->pfnPrintf(pHlp, " HCE" );
pHlp->pfnPrintf(pHlp, "\n");
/* Device Notification Control and Configure registers. */
pHlp->pfnPrintf(pHlp, "DNCTRL: %X CONFIG: %X (%u slots)\n", pThis->dnctrl, pThis->config, pThis->config);
/* Device Context Base Address Array. */
GPAddr = pThis->dcbaap & XHCI_DCBAA_ADDR_MASK;
pHlp->pfnPrintf(pHlp, "DCBAA ptr: %RGp\n", GPAddr);
/* The DCBAA must be valid in 'run' state. */
if (fVerbose && (pThis->cmd & XHCI_CMD_RS))
{
PDMDevHlpPCIPhysRead(pDevIns, GPAddr, &u64Val, sizeof(u64Val));
pHlp->pfnPrintf(pHlp, " Scratchpad buffer: %RX64\n", u64Val);
}
/* Command Ring Control Register. */
pHlp->pfnPrintf(pHlp, "CRCR: %X:", pThis->crcr & ~XHCI_CRCR_ADDR_MASK);
if (pThis->crcr & XHCI_CRCR_RCS) pHlp->pfnPrintf(pHlp, " RCS");
if (pThis->crcr & XHCI_CRCR_CS) pHlp->pfnPrintf(pHlp, " CS" );
if (pThis->crcr & XHCI_CRCR_CA) pHlp->pfnPrintf(pHlp, " CA" );
if (pThis->crcr & XHCI_CRCR_CRR) pHlp->pfnPrintf(pHlp, " CRR");
pHlp->pfnPrintf(pHlp, "\n");
GPAddr = pThis->crcr & XHCI_CRCR_ADDR_MASK;
pHlp->pfnPrintf(pHlp, "CRCR ptr : %RGp\n", GPAddr);
/* Interrupters. */
if (fVerbose)
{
for (i = 0; i < RT_ELEMENTS(pThis->aInterrupters); ++i)
{
if (pThis->aInterrupters[i].erstsz)
{
XHCIINTRPTR *ir = &pThis->aInterrupters[i];
pHlp->pfnPrintf(pHlp, "Interrupter %d (IPE=%u)\n", i, ir->ipe);
/* The Interrupt Management Register. */
pHlp->pfnPrintf(pHlp, " IMAN : %X:", ir->iman);
if (ir->iman & XHCI_IMAN_IP) pHlp->pfnPrintf(pHlp, " IP");
if (ir->iman & XHCI_IMAN_IE) pHlp->pfnPrintf(pHlp, " IE");
pHlp->pfnPrintf(pHlp, "\n");
/* The Interrupt Moderation Register. */
pHlp->pfnPrintf(pHlp, " IMOD : %X:", ir->imod);
pHlp->pfnPrintf(pHlp, " IMODI=%u", ir->imod & XHCI_IMOD_IMODI_MASK);
pHlp->pfnPrintf(pHlp, " IMODC=%u", (ir->imod & XHCI_IMOD_IMODC_MASK) >> XHCI_IMOD_IMODC_SHIFT);
pHlp->pfnPrintf(pHlp, "\n");
pHlp->pfnPrintf(pHlp, " ERSTSZ: %X\n", ir->erstsz);
pHlp->pfnPrintf(pHlp, " ERSTBA: %RGp\n", (RTGCPHYS)ir->erstba);
pHlp->pfnPrintf(pHlp, " ERDP : %RGp:", (RTGCPHYS)ir->erdp);
pHlp->pfnPrintf(pHlp, " EHB=%u", !!(ir->erdp & XHCI_ERDP_EHB));
pHlp->pfnPrintf(pHlp, " DESI=%u", ir->erdp & XHCI_ERDP_DESI_MASK);
pHlp->pfnPrintf(pHlp, " ptr=%RGp", ir->erdp & XHCI_ERDP_ADDR_MASK);
pHlp->pfnPrintf(pHlp, "\n");
pHlp->pfnPrintf(pHlp, " EREP : %RGp", ir->erep);
pHlp->pfnPrintf(pHlp, " Free TRBs in seg=%u", ir->trb_count);
pHlp->pfnPrintf(pHlp, "\n");
}
}
}
/* Port control/status. */
for (i = 0; i < XHCI_NDP_CFG(pThis); ++i)
{
PXHCIHUBPORT p = &pThis->aPorts[i];
pHlp->pfnPrintf(pHlp, "Port %02u (USB%c): ", IDX_TO_ID(i), IS_USB3_PORT_IDX_SHR(pThis, i) ? '3' : '2');
/* Port Status register. */
pHlp->pfnPrintf(pHlp, "PORTSC: %8X:", p->portsc);
if (p->portsc & XHCI_PORT_CCS) pHlp->pfnPrintf(pHlp, " CCS" );
if (p->portsc & XHCI_PORT_PED) pHlp->pfnPrintf(pHlp, " PED" );
if (p->portsc & XHCI_PORT_OCA) pHlp->pfnPrintf(pHlp, " OCA" );
if (p->portsc & XHCI_PORT_PR ) pHlp->pfnPrintf(pHlp, " PR" );
pHlp->pfnPrintf(pHlp, " PLS=%u", (p->portsc & XHCI_PORT_PLS_MASK) >> XHCI_PORT_PLS_SHIFT);
if (p->portsc & XHCI_PORT_PP ) pHlp->pfnPrintf(pHlp, " PP" );
pHlp->pfnPrintf(pHlp, " SPD=%u", (p->portsc & XHCI_PORT_SPD_MASK) >> XHCI_PORT_SPD_SHIFT);
if (p->portsc & XHCI_PORT_LWS) pHlp->pfnPrintf(pHlp, " LWS" );
if (p->portsc & XHCI_PORT_CSC) pHlp->pfnPrintf(pHlp, " CSC" );
if (p->portsc & XHCI_PORT_PEC) pHlp->pfnPrintf(pHlp, " PEC" );
if (p->portsc & XHCI_PORT_WRC) pHlp->pfnPrintf(pHlp, " WRC" );
if (p->portsc & XHCI_PORT_OCC) pHlp->pfnPrintf(pHlp, " OCC" );
if (p->portsc & XHCI_PORT_PRC) pHlp->pfnPrintf(pHlp, " PRC" );
if (p->portsc & XHCI_PORT_PLC) pHlp->pfnPrintf(pHlp, " PLC" );
if (p->portsc & XHCI_PORT_CEC) pHlp->pfnPrintf(pHlp, " CEC" );
if (p->portsc & XHCI_PORT_CAS) pHlp->pfnPrintf(pHlp, " CAS" );
if (p->portsc & XHCI_PORT_WCE) pHlp->pfnPrintf(pHlp, " WCE" );
if (p->portsc & XHCI_PORT_WDE) pHlp->pfnPrintf(pHlp, " WDE" );
if (p->portsc & XHCI_PORT_WOE) pHlp->pfnPrintf(pHlp, " WOE" );
if (p->portsc & XHCI_PORT_DR ) pHlp->pfnPrintf(pHlp, " DR" );
if (p->portsc & XHCI_PORT_WPR) pHlp->pfnPrintf(pHlp, " WPR" );
pHlp->pfnPrintf(pHlp, "\n");
}
/* Device contexts. */
if (fVerbose && (pThis->cmd & XHCI_CMD_RS))
{
for (i = 0; i < XHCI_NDS; ++i)
{
if (pThis->aSlotState[i] > XHCI_DEVSLOT_EMPTY)
{
RTGCPHYS GCPhysSlot;
XHCI_DEV_CTX ctxDevice;
XHCI_SLOT_CTX ctxSlot;
const char *pcszDesc;
uint8_t uSlotID = IDX_TO_ID(i);
/* Find the slot address/ */
GCPhysSlot = xhciR3FetchDevCtxAddr(pDevIns, pThis, uSlotID);
pHlp->pfnPrintf(pHlp, "Slot %d (device context @ %RGp)\n", uSlotID, GCPhysSlot);
if (!GCPhysSlot)
{
pHlp->pfnPrintf(pHlp, "Bad context address, skipping!\n");
continue;
}
/* Just read in the whole lot and sort in which contexts are valid later. */
PDMDevHlpPCIPhysRead(pDevIns, GCPhysSlot, &ctxDevice, sizeof(ctxDevice));
ctxSlot = ctxDevice.entry[0].sc;
pcszDesc = ctxSlot.slot_state < RT_ELEMENTS(g_apszSltStates) ? g_apszSltStates[ctxSlot.slot_state] : "BAD!!!";
pHlp->pfnPrintf(pHlp, " Speed:%u Entries:%u RhPort:%u", ctxSlot.speed, ctxSlot.ctx_ent, ctxSlot.rh_port);
pHlp->pfnPrintf(pHlp, " Address:%u State:%s \n", ctxSlot.dev_addr, pcszDesc);
/* Endpoint contexts. */
for (j = 1; j <= ctxSlot.ctx_ent; ++j)
{
XHCI_EP_CTX ctxEP = ctxDevice.entry[j].ep;
/* Skip disabled endpoints -- they may be unused and do not
* contain valid data in any case.
*/
if (ctxEP.ep_state == XHCI_EPST_DISABLED)
continue;
pcszDesc = ctxEP.ep_state < RT_ELEMENTS(g_apszEpStates) ? g_apszEpStates[ctxEP.ep_state] : "BAD!!!";
pHlp->pfnPrintf(pHlp, " Endpoint DCI %u State:%s", j, pcszDesc);
pcszDesc = ctxEP.ep_type < RT_ELEMENTS(g_apszEpTypes) ? g_apszEpTypes[ctxEP.ep_type] : "BAD!!!";
pHlp->pfnPrintf(pHlp, " Type:%s\n",pcszDesc);
pHlp->pfnPrintf(pHlp, " Mult:%u MaxPStreams:%u LSA:%u Interval:%u\n",
ctxEP.mult, ctxEP.maxps, ctxEP.lsa, ctxEP.interval);
pHlp->pfnPrintf(pHlp, " CErr:%u HID:%u MaxPS:%u MaxBS:%u",
ctxEP.c_err, ctxEP.hid, ctxEP.max_pkt_sz, ctxEP.max_brs_sz);
pHlp->pfnPrintf(pHlp, " AvgTRBLen:%u MaxESIT:%u",
ctxEP.avg_trb_len, ctxEP.max_esit);
pHlp->pfnPrintf(pHlp, " LastFrm:%u IFC:%u LastCC:%u\n",
ctxEP.last_frm, ctxEP.ifc, ctxEP.last_cc);
pHlp->pfnPrintf(pHlp, " TRDP:%RGp DCS:%u\n", (RTGCPHYS)(ctxEP.trdp & XHCI_TRDP_ADDR_MASK),
ctxEP.trdp & XHCI_TRDP_DCS_MASK);
pHlp->pfnPrintf(pHlp, " TREP:%RGp DCS:%u\n", (RTGCPHYS)(ctxEP.trep & XHCI_TRDP_ADDR_MASK),
ctxEP.trep & XHCI_TRDP_DCS_MASK);
}
}
}
}
}
/**
* @interface_method_impl{PDMDEVREG,pfnReset}
*/
static DECLCALLBACK(void) xhciR3Reset(PPDMDEVINS pDevIns)
{
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PXHCICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PXHCICC);
LogFlow(("xhciR3Reset:\n"));
/*
* There is no distinction between cold boot, warm reboot and software reboots,
* all of these are treated as cold boots. We are also doing the initialization
* job of a BIOS or SMM driver.
*
* Important: Don't confuse UsbReset with hardware reset. Hardware reset is
* just one way of getting into the UsbReset state.
*/
/* Set the HC Halted bit now to prevent completion callbacks from running
*(there is really no point when resetting).
*/
ASMAtomicOrU32(&pThis->status, XHCI_STATUS_HCH);
xhciR3BusStop(pDevIns, pThis, pThisCC);
xhciR3DoReset(pThis, pThisCC, XHCI_USB_RESET, true /* reset devices */);
}
/**
* @interface_method_impl{PDMDEVREG,pfnDestruct}
*/
static DECLCALLBACK(int) xhciR3Destruct(PPDMDEVINS pDevIns)
{
PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PXHCICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PXHCICC);
LogFlow(("xhciR3Destruct:\n"));
/*
* Destroy interrupter locks.
*/
for (unsigned i = 0; i < RT_ELEMENTS(pThis->aInterrupters); ++i)
{
if (PDMDevHlpCritSectIsInitialized(pDevIns, &pThis->aInterrupters[i].lock))
PDMDevHlpCritSectDelete(pDevIns, &pThis->aInterrupters[i].lock);
}
/*
* Clean up the worker thread and associated machinery.
*/
if (pThis->hEvtProcess != NIL_SUPSEMEVENT)
{
PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEvtProcess);
pThis->hEvtProcess = NIL_SUPSEMEVENT;
}
if (RTCritSectIsInitialized(&pThisCC->CritSectThrd))
RTCritSectDelete(&pThisCC->CritSectThrd);
return VINF_SUCCESS;
}
/**
* Worker for xhciR3Construct that registers a LUN (USB root hub).
*/
static int xhciR3RegisterHub(PPDMDEVINS pDevIns, PXHCIROOTHUBR3 pRh, int iLun, const char *pszDesc)
{
int rc = PDMDevHlpDriverAttach(pDevIns, iLun, &pRh->IBase, &pRh->pIBase, pszDesc);
AssertMsgRCReturn(rc, ("Configuration error: Failed to attach root hub driver to LUN #%d! (%Rrc)\n", iLun, rc), rc);
pRh->pIRhConn = PDMIBASE_QUERY_INTERFACE(pRh->pIBase, VUSBIROOTHUBCONNECTOR);
AssertMsgReturn(pRh->pIRhConn,
("Configuration error: The driver doesn't provide the VUSBIROOTHUBCONNECTOR interface!\n"),
VERR_PDM_MISSING_INTERFACE);
/* Set URB parameters. */
rc = VUSBIRhSetUrbParams(pRh->pIRhConn, sizeof(VUSBURBHCIINT), 0);
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, N_("OHCI: Failed to set URB parameters"));
return rc;
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct,XHCI
* constructor}
*/
static DECLCALLBACK(int) xhciR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
PXHCICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PXHCICC);
PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
uint32_t cUsb2Ports;
uint32_t cUsb3Ports;
int rc;
LogFlow(("xhciR3Construct:\n"));
RT_NOREF(iInstance);
/*
* Initialize data so the destructor runs smoothly.
*/
pThis->hEvtProcess = NIL_SUPSEMEVENT;
/*
* Validate and read configuration.
*/
PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "USB2Ports|USB3Ports|ChipType", "");
/* Number of USB2 ports option. */
rc = pHlp->pfnCFGMQueryU32Def(pCfg, "USB2Ports", &cUsb2Ports, XHCI_NDP_20_DEFAULT);
if (RT_FAILURE(rc))
return PDMDEV_SET_ERROR(pDevIns, rc,
N_("xHCI configuration error: failed to read USB2Ports as integer"));
if (cUsb2Ports == 0 || cUsb2Ports > XHCI_NDP_MAX)
return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
N_("xHCI configuration error: USB2Ports must be in range [%u,%u]"),
1, XHCI_NDP_MAX);
/* Number of USB3 ports option. */
rc = pHlp->pfnCFGMQueryU32Def(pCfg, "USB3Ports", &cUsb3Ports, XHCI_NDP_30_DEFAULT);
if (RT_FAILURE(rc))
return PDMDEV_SET_ERROR(pDevIns, rc,
N_("xHCI configuration error: failed to read USB3Ports as integer"));
if (cUsb3Ports == 0 || cUsb3Ports > XHCI_NDP_MAX)
return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
N_("xHCI configuration error: USB3Ports must be in range [%u,%u]"),
1, XHCI_NDP_MAX);
/* Check that the total number of ports is within limits.*/
if (cUsb2Ports + cUsb3Ports > XHCI_NDP_MAX)
return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
N_("xHCI configuration error: USB2Ports + USB3Ports must be in range [%u,%u]"),
1, XHCI_NDP_MAX);
/* Determine the model. */
char szChipType[16];
rc = pHlp->pfnCFGMQueryStringDef(pCfg, "ChipType", &szChipType[0], sizeof(szChipType), "PantherPoint");
if (RT_FAILURE(rc))
return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
N_("xHCI configuration error: Querying \"ChipType\" as string failed"));
/*
* The default model is Panther Point (8086:1E31), Intel's first and most widely
* supported xHCI implementation. For debugging, the Lynx Point (8086:8C31) model
* can be selected. These two models work with the 7 Series and 8 Series Intel xHCI
* drivers for Windows 7, respectively. There is no functional difference.
* For Windows XP support, it's also possible to present a Renesas uPD720201 xHC;
* this is an evolution of the original NEC xHCI chip.
*/
bool fChipLynxPoint = false;
bool fChipRenesas = false;
if (!strcmp(szChipType, "PantherPoint"))
fChipLynxPoint = false;
else if (!strcmp(szChipType, "LynxPoint"))
fChipLynxPoint = true;
else if (!strcmp(szChipType, "uPD720201"))
fChipRenesas = true;
else
return PDMDevHlpVMSetError(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES, RT_SRC_POS,
N_("xHCI configuration error: The \"ChipType\" value \"%s\" is unsupported"), szChipType);
LogFunc(("cUsb2Ports=%u cUsb3Ports=%u szChipType=%s (%d,%d) fR0Enabled=%d fRCEnabled=%d\n", cUsb2Ports, cUsb3Ports,
szChipType, fChipLynxPoint, fChipRenesas, pDevIns->fR0Enabled, pDevIns->fRCEnabled));
/* Set up interrupter locks. */
for (unsigned i = 0; i < RT_ELEMENTS(pThis->aInterrupters); ++i)
{
rc = PDMDevHlpCritSectInit(pDevIns, &pThis->aInterrupters[i].lock, RT_SRC_POS, "xHCIIntr#%u", i);
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
N_("xHCI: Failed to create critical section for interrupter %u"), i);
pThis->aInterrupters[i].index = i; /* Stash away index, mostly for logging/debugging. */
}
/*
* Init instance data.
*/
pThisCC->pDevIns = pDevIns;
PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
if (fChipRenesas)
{
pThis->erst_addr_mask = NEC_ERST_ADDR_MASK;
PCIDevSetVendorId(pPciDev, 0x1912);
PCIDevSetDeviceId(pPciDev, 0x0014);
PCIDevSetByte(pPciDev, VBOX_PCI_REVISION_ID, 0x02);
}
else
{
pThis->erst_addr_mask = XHCI_ERST_ADDR_MASK;
PCIDevSetVendorId(pPciDev, 0x8086);
if (fChipLynxPoint)
PCIDevSetDeviceId(pPciDev, 0x8C31); /* Lynx Point / 8 Series */
else
PCIDevSetDeviceId(pPciDev, 0x1E31); /* Panther Point / 7 Series */
}
PCIDevSetClassProg(pPciDev, 0x30); /* xHCI */
PCIDevSetClassSub(pPciDev, 0x03); /* USB 3.0 */
PCIDevSetClassBase(pPciDev, 0x0C);
PCIDevSetInterruptPin(pPciDev, 0x01);
#ifdef VBOX_WITH_MSI_DEVICES
PCIDevSetStatus(pPciDev, VBOX_PCI_STATUS_CAP_LIST);
PCIDevSetCapabilityList(pPciDev, 0x80);
#endif
PDMPciDevSetByte(pPciDev, 0x60, 0x20); /* serial bus release number register; 0x20 = USB 2.0 */
/** @todo USBLEGSUP & USBLEGCTLSTS? Legacy interface for the BIOS (0xEECP+0 & 0xEECP+4) */
pThis->cTotalPorts = (uint8_t)(cUsb2Ports + cUsb3Ports);
/* Set up the USB2 root hub interface. */
pThis->cUsb2Ports = (uint8_t)cUsb2Ports;
pThisCC->RootHub2.pXhciR3 = pThisCC;
pThisCC->RootHub2.cPortsImpl = cUsb2Ports;
pThisCC->RootHub2.uPortBase = 0;
pThisCC->RootHub2.IBase.pfnQueryInterface = xhciR3RhQueryInterface;
pThisCC->RootHub2.IRhPort.pfnGetAvailablePorts = xhciR3RhGetAvailablePorts;
pThisCC->RootHub2.IRhPort.pfnGetUSBVersions = xhciR3RhGetUSBVersions2;
pThisCC->RootHub2.IRhPort.pfnAttach = xhciR3RhAttach;
pThisCC->RootHub2.IRhPort.pfnDetach = xhciR3RhDetach;
pThisCC->RootHub2.IRhPort.pfnReset = xhciR3RhReset;
pThisCC->RootHub2.IRhPort.pfnXferCompletion = xhciR3RhXferCompletion;
pThisCC->RootHub2.IRhPort.pfnXferError = xhciR3RhXferError;
/* Now the USB3 root hub interface. */
pThis->cUsb3Ports = (uint8_t)cUsb3Ports;
pThisCC->RootHub3.pXhciR3 = pThisCC;
pThisCC->RootHub3.cPortsImpl = cUsb3Ports;
pThisCC->RootHub3.uPortBase = XHCI_NDP_USB2(pThisCC);
pThisCC->RootHub3.IBase.pfnQueryInterface = xhciR3RhQueryInterface;
pThisCC->RootHub3.IRhPort.pfnGetAvailablePorts = xhciR3RhGetAvailablePorts;
pThisCC->RootHub3.IRhPort.pfnGetUSBVersions = xhciR3RhGetUSBVersions3;
pThisCC->RootHub3.IRhPort.pfnAttach = xhciR3RhAttach;
pThisCC->RootHub3.IRhPort.pfnDetach = xhciR3RhDetach;
pThisCC->RootHub3.IRhPort.pfnReset = xhciR3RhReset;
pThisCC->RootHub3.IRhPort.pfnXferCompletion = xhciR3RhXferCompletion;
pThisCC->RootHub3.IRhPort.pfnXferError = xhciR3RhXferError;
/* USB LED */
pThisCC->RootHub2.Led.u32Magic = PDMLED_MAGIC;
pThisCC->RootHub3.Led.u32Magic = PDMLED_MAGIC;
pThisCC->IBase.pfnQueryInterface = xhciR3QueryStatusInterface;
pThisCC->ILeds.pfnQueryStatusLed = xhciR3QueryStatusLed;
/* Initialize the capability registers */
pThis->cap_length = XHCI_CAPS_REG_SIZE;
pThis->hci_version = 0x100; /* Version 1.0 */
pThis->hcs_params1 = (XHCI_NDP_CFG(pThis) << 24) | (XHCI_NINTR << 8) | XHCI_NDS;
pThis->hcs_params2 = (XHCI_ERSTMAX_LOG2 << 4) | XHCI_IST;
pThis->hcs_params3 = (4 << 16) | 1; /* Matches Intel 7 Series xHCI. */
/* Note: The Intel 7 Series xHCI does not have port power control (XHCI_HCC_PPC). */
pThis->hcc_params = ((XHCI_XECP_OFFSET >> 2) << XHCI_HCC_XECP_SHIFT); /// @todo other fields
pThis->dbell_off = XHCI_DOORBELL_OFFSET;
pThis->rts_off = XHCI_RTREG_OFFSET;
/*
* Set up extended capabilities.
*/
rc = xhciR3BuildExtCaps(pThis, pThisCC);
AssertRCReturn(rc, rc);
/*
* Register PCI device and I/O region.
*/
rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
AssertRCReturn(rc, rc);
#ifdef VBOX_WITH_MSI_DEVICES
PDMMSIREG MsiReg;
RT_ZERO(MsiReg);
MsiReg.cMsiVectors = 1;
MsiReg.iMsiCapOffset = XHCI_PCI_MSI_CAP_OFS;
MsiReg.iMsiNextOffset = 0x00;
rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
if (RT_FAILURE (rc))
{
PCIDevSetCapabilityList(pPciDev, 0x0);
/* That's OK, we can work without MSI */
}
#endif
rc = PDMDevHlpPCIIORegionCreateMmio(pDevIns, 0, XHCI_MMIO_SIZE, PCI_ADDRESS_SPACE_MEM,
xhciMmioWrite, xhciMmioRead, NULL,
IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_DWORD_ZEROED
/*| IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_WRITE*/,
"USB xHCI", &pThis->hMmio);
AssertRCReturn(rc, rc);
/*
* Register the saved state data unit.
*/
rc = PDMDevHlpSSMRegisterEx(pDevIns, XHCI_SAVED_STATE_VERSION, sizeof(*pThis), NULL,
NULL, NULL, NULL,
NULL, xhciR3SaveExec, NULL,
NULL, xhciR3LoadExec, NULL);
AssertRCReturn(rc, rc);
/*
* Attach to the VBox USB RootHub Driver on LUN #0 (USB3 root hub).
* NB: USB3 must come first so that emulated devices which support both USB2
* and USB3 are attached to the USB3 hub.
*/
rc = xhciR3RegisterHub(pDevIns, &pThisCC->RootHub3, 0, "RootHubUSB3");
AssertRCReturn(rc, rc);
/*
* Attach to the VBox USB RootHub Driver on LUN #1 (USB2 root hub).
*/
rc = xhciR3RegisterHub(pDevIns, &pThisCC->RootHub2, 1, "RootHubUSB2");
AssertRCReturn(rc, rc);
/*
* Attach the status LED (optional).
*/
PPDMIBASE pBase;
rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThisCC->IBase, &pBase, "Status Port");
if (RT_SUCCESS(rc))
pThisCC->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
else if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
{
AssertMsgFailed(("xHCI: Failed to attach to status driver. rc=%Rrc\n", rc));
return PDMDEV_SET_ERROR(pDevIns, rc, N_("xHCI cannot attach to status driver"));
}
/*
* Create the MFINDEX wrap event timer.
*/
rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, xhciR3WrapTimer, pThis,
TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_RING0, "xHCI MFINDEX Wrap", &pThis->hWrapTimer);
AssertRCReturn(rc, rc);
/*
* Set up the worker thread.
*/
rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEvtProcess);
AssertLogRelRCReturn(rc, rc);
rc = RTCritSectInit(&pThisCC->CritSectThrd);
AssertLogRelRCReturn(rc, rc);
rc = PDMDevHlpThreadCreate(pDevIns, &pThisCC->pWorkerThread, pThis, xhciR3WorkerLoop, xhciR3WorkerWakeUp,
0, RTTHREADTYPE_IO, "xHCI");
AssertLogRelRCReturn(rc, rc);
/*
* Do a hardware reset.
*/
xhciR3DoReset(pThis, pThisCC, XHCI_USB_RESET, false /* don't reset devices */);
# ifdef VBOX_WITH_STATISTICS
/*
* Register statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatErrorIsocUrbs, STAMTYPE_COUNTER, "IsocUrbsErr", STAMUNIT_OCCURENCES, "Isoch URBs completed w/error.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatErrorIsocPkts, STAMTYPE_COUNTER, "IsocPktsErr", STAMUNIT_OCCURENCES, "Isoch packets completed w/error.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatEventsWritten, STAMTYPE_COUNTER, "EventsWritten", STAMUNIT_OCCURENCES, "Event TRBs delivered.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatEventsDropped, STAMTYPE_COUNTER, "EventsDropped", STAMUNIT_OCCURENCES, "Event TRBs dropped (HC stopped).");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrsPending, STAMTYPE_COUNTER, "IntrsPending", STAMUNIT_OCCURENCES, "Requests to set the IP bit.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrsSet, STAMTYPE_COUNTER, "IntrsSet", STAMUNIT_OCCURENCES, "Actual interrupts delivered.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrsNotSet, STAMTYPE_COUNTER, "IntrsNotSet", STAMUNIT_OCCURENCES, "Interrupts not delivered/disabled.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrsCleared, STAMTYPE_COUNTER, "IntrsCleared", STAMUNIT_OCCURENCES, "Interrupts cleared by guest.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTRBsPerCtlUrb, STAMTYPE_COUNTER, "UrbTrbsCtl", STAMUNIT_COUNT, "TRBs per one control URB.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTRBsPerDtaUrb, STAMTYPE_COUNTER, "UrbTrbsDta", STAMUNIT_COUNT, "TRBs per one data (bulk/intr) URB.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTRBsPerIsoUrb, STAMTYPE_COUNTER, "UrbTrbsIso", STAMUNIT_COUNT, "TRBs per one isochronous URB.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatUrbSizeCtrl, STAMTYPE_COUNTER, "UrbSizeCtl", STAMUNIT_COUNT, "Size of a control URB in bytes.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatUrbSizeData, STAMTYPE_COUNTER, "UrbSizeDta", STAMUNIT_COUNT, "Size of a data (bulk/intr) URB in bytes.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatUrbSizeIsoc, STAMTYPE_COUNTER, "UrbSizeIso", STAMUNIT_COUNT, "Size of an isochronous URB in bytes.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdCaps, STAMTYPE_COUNTER, "Regs/RdCaps", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdCmdRingCtlHi, STAMTYPE_COUNTER, "Regs/RdCmdRingCtlHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdCmdRingCtlLo, STAMTYPE_COUNTER, "Regs/RdCmdRingCtlLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdConfig, STAMTYPE_COUNTER, "Regs/RdConfig", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdDevCtxBaapHi, STAMTYPE_COUNTER, "Regs/RdDevCtxBaapHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdDevCtxBaapLo, STAMTYPE_COUNTER, "Regs/RdDevCtxBaapLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdDevNotifyCtrl, STAMTYPE_COUNTER, "Regs/RdDevNotifyCtrl", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdDoorBell, STAMTYPE_COUNTER, "Regs/RdDoorBell", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdEvtRingDeqPtrHi, STAMTYPE_COUNTER, "Regs/RdEvtRingDeqPtrHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdEvtRingDeqPtrLo, STAMTYPE_COUNTER, "Regs/RdEvtRingDeqPtrLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdEvtRsTblBaseHi, STAMTYPE_COUNTER, "Regs/RdEvtRsTblBaseHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdEvtRsTblBaseLo, STAMTYPE_COUNTER, "Regs/RdEvtRsTblBaseLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdEvtRstblSize, STAMTYPE_COUNTER, "Regs/RdEvtRstblSize", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdEvtRsvd, STAMTYPE_COUNTER, "Regs/RdEvtRsvd", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdIntrMgmt, STAMTYPE_COUNTER, "Regs/RdIntrMgmt", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdIntrMod, STAMTYPE_COUNTER, "Regs/RdIntrMod", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdMfIndex, STAMTYPE_COUNTER, "Regs/RdMfIndex", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdPageSize, STAMTYPE_COUNTER, "Regs/RdPageSize", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdPortLinkInfo, STAMTYPE_COUNTER, "Regs/RdPortLinkInfo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdPortPowerMgmt, STAMTYPE_COUNTER, "Regs/RdPortPowerMgmt", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdPortRsvd, STAMTYPE_COUNTER, "Regs/RdPortRsvd", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdPortStatusCtrl, STAMTYPE_COUNTER, "Regs/RdPortStatusCtrl", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdUsbCmd, STAMTYPE_COUNTER, "Regs/RdUsbCmd", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdUsbSts, STAMTYPE_COUNTER, "Regs/RdUsbSts", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRdUnknown, STAMTYPE_COUNTER, "Regs/RdUnknown", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrCmdRingCtlHi, STAMTYPE_COUNTER, "Regs/WrCmdRingCtlHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrCmdRingCtlLo, STAMTYPE_COUNTER, "Regs/WrCmdRingCtlLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrConfig, STAMTYPE_COUNTER, "Regs/WrConfig", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrDevCtxBaapHi, STAMTYPE_COUNTER, "Regs/WrDevCtxBaapHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrDevCtxBaapLo, STAMTYPE_COUNTER, "Regs/WrDevCtxBaapLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrDevNotifyCtrl, STAMTYPE_COUNTER, "Regs/WrDevNotifyCtrl", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrDoorBell0, STAMTYPE_COUNTER, "Regs/WrDoorBell0", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrDoorBellN, STAMTYPE_COUNTER, "Regs/WrDoorBellN", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrEvtRingDeqPtrHi, STAMTYPE_COUNTER, "Regs/WrEvtRingDeqPtrHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrEvtRingDeqPtrLo, STAMTYPE_COUNTER, "Regs/WrEvtRingDeqPtrLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrEvtRsTblBaseHi, STAMTYPE_COUNTER, "Regs/WrEvtRsTblBaseHi", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrEvtRsTblBaseLo, STAMTYPE_COUNTER, "Regs/WrEvtRsTblBaseLo", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrEvtRstblSize, STAMTYPE_COUNTER, "Regs/WrEvtRstblSize", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrIntrMgmt, STAMTYPE_COUNTER, "Regs/WrIntrMgmt", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrIntrMod, STAMTYPE_COUNTER, "Regs/WrIntrMod", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrPortPowerMgmt, STAMTYPE_COUNTER, "Regs/WrPortPowerMgmt", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrPortStatusCtrl, STAMTYPE_COUNTER, "Regs/WrPortStatusCtrl", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrUsbCmd, STAMTYPE_COUNTER, "Regs/WrUsbCmd", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrUsbSts, STAMTYPE_COUNTER, "Regs/WrUsbSts", STAMUNIT_COUNT, "");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatWrUnknown, STAMTYPE_COUNTER, "Regs/WrUnknown", STAMUNIT_COUNT, "");
# endif /* VBOX_WITH_STATISTICS */
/*
* Register debugger info callbacks.
*/
PDMDevHlpDBGFInfoRegister(pDevIns, "xhci", "xHCI registers.", xhciR3Info);
return VINF_SUCCESS;
}
#else /* !IN_RING3 */
/**
* @callback_method_impl{PDMDEVREGR0,pfnConstruct}
*/
static DECLCALLBACK(int) xhciRZConstruct(PPDMDEVINS pDevIns)
{
PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
PXHCI pThis = PDMDEVINS_2_DATA(pDevIns, PXHCI);
int rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, xhciMmioWrite, xhciMmioRead, NULL /*pvUser*/);
AssertRCReturn(rc, rc);
return VINF_SUCCESS;
}
#endif /* !IN_RING3 */
/* Without this, g_DeviceXHCI won't be visible outside this module! */
extern "C" const PDMDEVREG g_DeviceXHCI;
const PDMDEVREG g_DeviceXHCI =
{
/* .u32version = */ PDM_DEVREG_VERSION,
/* .uReserved0 = */ 0,
/* .szName = */ "usb-xhci",
/* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
/* .fClass = */ PDM_DEVREG_CLASS_BUS_USB,
/* .cMaxInstances = */ ~0U,
/* .uSharedVersion = */ 42,
/* .cbInstanceShared = */ sizeof(XHCI),
/* .cbInstanceCC = */ sizeof(XHCICC),
/* .cbInstanceRC = */ sizeof(XHCIRC),
/* .cMaxPciDevices = */ 1,
/* .cMaxMsixVectors = */ 0,
/* .pszDescription = */ "xHCI USB controller.\n",
#if defined(IN_RING3)
# ifdef VBOX_IN_EXTPACK
/* .pszRCMod = */ "VBoxEhciRC.rc",
/* .pszR0Mod = */ "VBoxEhciR0.r0",
# else
/* .pszRCMod = */ "VBoxDDRC.rc",
/* .pszR0Mod = */ "VBoxDDR0.r0",
# endif
/* .pfnConstruct = */ xhciR3Construct,
/* .pfnDestruct = */ xhciR3Destruct,
/* .pfnRelocate = */ NULL,
/* .pfnMemSetup = */ NULL,
/* .pfnPowerOn = */ NULL,
/* .pfnReset = */ xhciR3Reset,
/* .pfnSuspend = */ NULL,
/* .pfnResume = */ NULL,
/* .pfnAttach = */ NULL,
/* .pfnDetach = */ NULL,
/* .pfnQueryInterface = */ NULL,
/* .pfnInitComplete = */ NULL,
/* .pfnPowerOff = */ NULL,
/* .pfnSoftReset = */ NULL,
/* .pfnReserved0 = */ NULL,
/* .pfnReserved1 = */ NULL,
/* .pfnReserved2 = */ NULL,
/* .pfnReserved3 = */ NULL,
/* .pfnReserved4 = */ NULL,
/* .pfnReserved5 = */ NULL,
/* .pfnReserved6 = */ NULL,
/* .pfnReserved7 = */ NULL,
#elif defined(IN_RING0)
/* .pfnEarlyConstruct = */ NULL,
/* .pfnConstruct = */ xhciRZConstruct,
/* .pfnDestruct = */ NULL,
/* .pfnFinalDestruct = */ NULL,
/* .pfnRequest = */ NULL,
/* .pfnReserved0 = */ NULL,
/* .pfnReserved1 = */ NULL,
/* .pfnReserved2 = */ NULL,
/* .pfnReserved3 = */ NULL,
/* .pfnReserved4 = */ NULL,
/* .pfnReserved5 = */ NULL,
/* .pfnReserved6 = */ NULL,
/* .pfnReserved7 = */ NULL,
#elif defined(IN_RC)
/* .pfnConstruct = */ xhciRZConstruct,
/* .pfnReserved0 = */ NULL,
/* .pfnReserved1 = */ NULL,
/* .pfnReserved2 = */ NULL,
/* .pfnReserved3 = */ NULL,
/* .pfnReserved4 = */ NULL,
/* .pfnReserved5 = */ NULL,
/* .pfnReserved6 = */ NULL,
/* .pfnReserved7 = */ NULL,
#else
# error "Not in IN_RING3, IN_RING0 or IN_RC!"
#endif
/* .u32VersionEnd = */ PDM_DEVREG_VERSION
};
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
|