summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/Bus/DevIommuAmd.cpp
blob: ca839e5050767f08a848d19a7361f8eee2f9ab9a (plain)
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
/* $Id: DevIommuAmd.cpp $ */
/** @file
 * IOMMU - Input/Output Memory Management Unit - AMD implementation.
 */

/*
 * Copyright (C) 2020-2023 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
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_IOMMU
#include <VBox/msi.h>
#include <VBox/iommu-amd.h>
#include <VBox/vmm/pdmdev.h>

#include <iprt/x86.h>
#include <iprt/string.h>
#include <iprt/avl.h>
#ifdef IN_RING3
# include <iprt/mem.h>
#endif

#include "VBoxDD.h"
#include "DevIommuAmd.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** Release log prefix string. */
#define IOMMU_LOG_PFX                               "AMD-IOMMU"
/** The current saved state version. */
#define IOMMU_SAVED_STATE_VERSION                   1
/** The IOMMU device instance magic. */
#define IOMMU_MAGIC                                 0x10acce55

/** Enable the IOTLBE cache only in ring-3 for now, see @bugref{9654#c95}. */
#ifdef IN_RING3
# define IOMMU_WITH_IOTLBE_CACHE
#endif
/** Enable the interrupt cache. */
#define IOMMU_WITH_IRTE_CACHE

/* The DTE cache is mandatory for the IOTLB or interrupt cache to work. */
#if defined(IOMMU_WITH_IOTLBE_CACHE) || defined(IOMMU_WITH_IRTE_CACHE)
# define IOMMU_WITH_DTE_CACHE
/** The maximum number of device IDs in the cache. */
# define IOMMU_DEV_CACHE_COUNT                      16
/** An empty device ID. */
# define IOMMU_DTE_CACHE_KEY_NIL                    0
#endif

#ifdef IOMMU_WITH_IRTE_CACHE
/** The maximum number of IRTE cache entries. */
# define IOMMU_IRTE_CACHE_COUNT                     32
/** A NIL IRTE cache entry key. */
# define IOMMU_IRTE_CACHE_KEY_NIL                   (~(uint32_t)0U)
/** Gets the device ID from an IRTE cache entry key. */
#define IOMMU_IRTE_CACHE_KEY_GET_DEVICE_ID(a_Key)   RT_HIWORD(a_Key)
/** Gets the IOVA from the IOTLB entry key. */
# define IOMMU_IRTE_CACHE_KEY_GET_OFF(a_Key)        RT_LOWORD(a_Key)
/** Makes an IRTE cache entry key.
 *
 * Bits 31:16 is the device ID (Bus, Device, Function).
 * Bits  15:0 is the the offset into the IRTE table.
 */
# define IOMMU_IRTE_CACHE_KEY_MAKE(a_DevId, a_off)  RT_MAKE_U32(a_off, a_DevId)
#endif  /* IOMMU_WITH_IRTE_CACHE */

#ifdef IOMMU_WITH_IOTLBE_CACHE
/** The maximum number of IOTLB entries. */
# define IOMMU_IOTLBE_MAX                           64
/** The mask of bits covering the domain ID in the IOTLBE key. */
# define IOMMU_IOTLB_DOMAIN_ID_MASK                 UINT64_C(0xffffff0000000000)
/** The mask of bits covering the IOVA in the IOTLBE key. */
# define IOMMU_IOTLB_IOVA_MASK                     (~IOMMU_IOTLB_DOMAIN_ID_MASK)
/** The number of bits to shift for the domain ID of the IOTLBE key. */
# define IOMMU_IOTLB_DOMAIN_ID_SHIFT                40
/** A NIL IOTLB key. */
# define IOMMU_IOTLB_KEY_NIL                        UINT64_C(0)
/** Gets the domain ID from an IOTLB entry key. */
# define IOMMU_IOTLB_KEY_GET_DOMAIN_ID(a_Key)       ((a_Key) >> IOMMU_IOTLB_DOMAIN_ID_SHIFT)
/** Gets the IOVA from the IOTLB entry key. */
# define IOMMU_IOTLB_KEY_GET_IOVA(a_Key)            (((a_Key) & IOMMU_IOTLB_IOVA_MASK) << X86_PAGE_4K_SHIFT)
/** Makes an IOTLB entry key.
 *
 * Address bits 63:52 of the IOVA are zero extended, so top 12 bits are free.
 * Address bits 11:0 of the IOVA are offset into the minimum page size of 4K,
 * so bottom 12 bits are free.
 *
 * Thus we use the top 24 bits of key to hold bits 15:0 of the domain ID.
 * We use the bottom 40 bits of the key to hold bits 51:12 of the IOVA.
 */
# define IOMMU_IOTLB_KEY_MAKE(a_DomainId, a_uIova)  (  ((uint64_t)(a_DomainId) << IOMMU_IOTLB_DOMAIN_ID_SHIFT) \
                                                     | (((a_uIova) >> X86_PAGE_4K_SHIFT) & IOMMU_IOTLB_IOVA_MASK))
#endif  /* IOMMU_WITH_IOTLBE_CACHE */

#ifdef IOMMU_WITH_DTE_CACHE
/** @name IOMMU_DTE_CACHE_F_XXX: DTE cache flags.
 *
 *  Some of these flags are "basic" i.e. they correspond directly to their bits in
 *  the DTE. The rest of the flags are based on checks or operations on several DTE
 *  bits.
 *
 *  The basic flags are:
 *    - VALID                (DTE.V)
 *    - IO_PERM_READ         (DTE.IR)
 *    - IO_PERM_WRITE        (DTE.IW)
 *    - IO_PERM_RSVD         (bit following DTW.IW reserved for future & to keep
 *                            masking consistent)
 *    - SUPPRESS_ALL_IOPF    (DTE.SA)
 *    - SUPPRESS_IOPF        (DTE.SE)
 *    - INTR_MAP_VALID       (DTE.IV)
 *    - IGNORE_UNMAPPED_INTR (DTE.IG)
 *
 *  @see iommuAmdGetBasicDevFlags()
 *  @{ */
/** The DTE is present. */
# define IOMMU_DTE_CACHE_F_PRESENT                       RT_BIT(0)
/** The DTE is valid. */
# define IOMMU_DTE_CACHE_F_VALID                         RT_BIT(1)
/** The DTE permissions apply for address translations. */
# define IOMMU_DTE_CACHE_F_IO_PERM                       RT_BIT(2)
/** DTE permission - I/O read allowed. */
# define IOMMU_DTE_CACHE_F_IO_PERM_READ                  RT_BIT(3)
/** DTE permission - I/O write allowed. */
# define IOMMU_DTE_CACHE_F_IO_PERM_WRITE                 RT_BIT(4)
/** DTE permission - reserved. */
# define IOMMU_DTE_CACHE_F_IO_PERM_RSVD                  RT_BIT(5)
/** Address translation required. */
# define IOMMU_DTE_CACHE_F_ADDR_TRANSLATE                RT_BIT(6)
/** Suppress all I/O page faults. */
# define IOMMU_DTE_CACHE_F_SUPPRESS_ALL_IOPF             RT_BIT(7)
/** Suppress I/O page faults. */
# define IOMMU_DTE_CACHE_F_SUPPRESS_IOPF                 RT_BIT(8)
/** Interrupt map valid. */
# define IOMMU_DTE_CACHE_F_INTR_MAP_VALID                RT_BIT(9)
/** Ignore unmapped interrupts. */
# define IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR          RT_BIT(10)
/** An I/O page fault has been raised for this device. */
# define IOMMU_DTE_CACHE_F_IO_PAGE_FAULT_RAISED          RT_BIT(11)
/** Fixed and arbitrary interrupt control: Target Abort. */
# define IOMMU_DTE_CACHE_F_INTR_CTRL_TARGET_ABORT        RT_BIT(12)
/** Fixed and arbitrary interrupt control: Forward unmapped. */
# define IOMMU_DTE_CACHE_F_INTR_CTRL_FWD_UNMAPPED        RT_BIT(13)
/** Fixed and arbitrary interrupt control: Remapped. */
# define IOMMU_DTE_CACHE_F_INTR_CTRL_REMAPPED            RT_BIT(14)
/** Fixed and arbitrary interrupt control: Reserved. */
# define IOMMU_DTE_CACHE_F_INTR_CTRL_RSVD                RT_BIT(15)
/** @} */

/** The number of bits to shift I/O device flags for DTE permissions. */
# define IOMMU_DTE_CACHE_F_IO_PERM_SHIFT                 3
/** The mask of DTE permissions in I/O device flags. */
# define IOMMU_DTE_CACHE_F_IO_PERM_MASK                  0x3
/** The number of bits to shift I/O device flags for interrupt control bits. */
# define IOMMU_DTE_CACHE_F_INTR_CTRL_SHIFT               12
/** The mask of interrupt control bits in I/O device flags. */
# define IOMMU_DTE_CACHE_F_INTR_CTRL_MASK                0x3
/** The number of bits to shift for ignore-unmapped interrupts bit. */
# define IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR_SHIFT    10

/** Acquires the cache lock. */
# define IOMMU_CACHE_LOCK(a_pDevIns, a_pThis) \
    do { \
        int const rcLock = PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSectCache, VINF_SUCCESS); \
        PDM_CRITSECT_RELEASE_ASSERT_RC_DEV((a_pDevIns), &(a_pThis)->CritSectCache, rcLock); \
    } while (0)

/** Releases the cache lock.  */
# define IOMMU_CACHE_UNLOCK(a_pDevIns, a_pThis)     PDMDevHlpCritSectLeave((a_pDevIns), &(a_pThis)->CritSectCache)
#endif  /* IOMMU_WITH_DTE_CACHE */

/** Acquires the IOMMU lock (returns a_rcBusy on contention). */
#define IOMMU_LOCK_RET(a_pDevIns, a_pThisCC, a_rcBusy)  \
    do { \
        int const rcLock = (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), (a_rcBusy)); \
        if (RT_LIKELY(rcLock == VINF_SUCCESS)) \
        { /* likely */ } \
        else \
            return rcLock; \
    } while (0)

/** Acquires the IOMMU lock (can fail under extraordinary circumstance in ring-0). */
#define IOMMU_LOCK(a_pDevIns, a_pThisCC) \
    do { \
        int const rcLock = (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), VINF_SUCCESS); \
        PDM_CRITSECT_RELEASE_ASSERT_RC_DEV((a_pDevIns), NULL, rcLock); \
    } while (0)

/** Checks if the current thread owns the PDM lock. */
# define IOMMU_ASSERT_LOCK_IS_OWNER(a_pDevIns, a_pThisCC) \
    do \
    { \
        Assert((a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLockIsOwner((a_pDevIns))); \
        NOREF(a_pThisCC); \
    } while (0)

/** Releases the PDM lock.   */
# define IOMMU_UNLOCK(a_pDevIns, a_pThisCC)         (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnUnlock((a_pDevIns))

/** Gets the maximum valid IOVA for the given I/O page-table level. */
#define IOMMU_GET_MAX_VALID_IOVA(a_Level)           ((X86_PAGE_4K_SIZE << ((a_Level) * 9)) - 1)


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * IOMMU operation (transaction).
 */
typedef enum IOMMUOP
{
    /** Address translation request. */
    IOMMUOP_TRANSLATE_REQ = 0,
    /** Memory read request. */
    IOMMUOP_MEM_READ,
    /** Memory write request. */
    IOMMUOP_MEM_WRITE,
    /** Interrupt request. */
    IOMMUOP_INTR_REQ,
    /** Command. */
    IOMMUOP_CMD
} IOMMUOP;
/** Pointer to a IOMMU operation. */
typedef IOMMUOP *PIOMMUOP;

/**
 * I/O page lookup.
 */
typedef struct IOPAGELOOKUP
{
    /** The translated system physical address. */
    RTGCPHYS        GCPhysSpa;
    /** The number of offset bits in the system physical address. */
    uint8_t         cShift;
    /** The I/O permissions for this translation, see IOMMU_IO_PERM_XXX. */
    uint8_t         fPerm;
} IOPAGELOOKUP;
/** Pointer to an I/O page lookup. */
typedef IOPAGELOOKUP *PIOPAGELOOKUP;
/** Pointer to a const I/O page lookup. */
typedef IOPAGELOOKUP const *PCIOPAGELOOKUP;

/**
 * I/O address range.
 */
typedef struct IOADDRRANGE
{
    /** The address (virtual or physical). */
    uint64_t        uAddr;
    /** The size of the access in bytes. */
    size_t          cb;
    /** The I/O permissions for this translation, see IOMMU_IO_PERM_XXX. */
    uint8_t         fPerm;
} IOADDRRANGE;
/** Pointer to an I/O address range. */
typedef IOADDRRANGE *PIOADDRRANGE;
/** Pointer to a const I/O address range. */
typedef IOADDRRANGE const *PCIOADDRRANGE;

#ifdef IOMMU_WITH_DTE_CACHE
/**
 * Device Table Entry Cache.
 */
typedef struct DTECACHE
{
    /** This device's flags, see IOMMU_DTE_CACHE_F_XXX. */
    uint16_t        fFlags;
    /** The domain ID assigned for this device by software. */
    uint16_t        idDomain;
} DTECACHE;
/** Pointer to an I/O device struct. */
typedef DTECACHE *PDTECACHE;
/** Pointer to a const I/O device struct. */
typedef DTECACHE *PCDTECACHE;
AssertCompileSize(DTECACHE, 4);
#endif  /* IOMMU_WITH_DTE_CACHE */

#ifdef IOMMU_WITH_IOTLBE_CACHE
/**
 * I/O TLB Entry.
 * Keep this as small and aligned as possible.
 */
typedef struct IOTLBE
{
    /** The AVL tree node. */
    AVLU64NODECORE      Core;
    /** The least recently used (LRU) list node. */
    RTLISTNODE          NdLru;
    /** The I/O page lookup results of the translation. */
    IOPAGELOOKUP        PageLookup;
    /** Whether the entry needs to be evicted from the cache. */
    bool                fEvictPending;
} IOTLBE;
/** Pointer to an IOMMU I/O TLB entry struct. */
typedef IOTLBE *PIOTLBE;
/** Pointer to a const IOMMU I/O TLB entry struct. */
typedef IOTLBE const *PCIOTLBE;
AssertCompileSizeAlignment(IOTLBE, 8);
AssertCompileMemberOffset(IOTLBE, Core, 0);
#endif  /* IOMMU_WITH_IOTLBE_CACHE */

#ifdef IOMMU_WITH_IRTE_CACHE
/**
 * Interrupt Remap Table Entry Cache.
 */
typedef struct IRTECACHE
{
    /** The key, see IOMMU_IRTE_CACHE_KEY_MAKE. */
    uint32_t            uKey;
    /** The IRTE. */
    IRTE_T              Irte;
} IRTECACHE;
/** Pointer to an IRTE cache struct. */
typedef IRTECACHE *PIRTECACHE;
/** Pointer to a const IRTE cache struct. */
typedef IRTECACHE const *PCIRTECACHE;
AssertCompileSizeAlignment(IRTECACHE, 4);
#endif /* IOMMU_WITH_IRTE_CACHE */

/**
 * The shared IOMMU device state.
 */
typedef struct IOMMU
{
    /** IOMMU device index (0 is at the top of the PCI tree hierarchy). */
    uint32_t                    idxIommu;
    /** IOMMU magic. */
    uint32_t                    u32Magic;

    /** The MMIO handle. */
    IOMMMIOHANDLE               hMmio;
    /** The event semaphore the command thread waits on. */
    SUPSEMEVENT                 hEvtCmdThread;
    /** Whether the command thread has been signaled for wake up. */
    bool volatile               fCmdThreadSignaled;
    /** Padding. */
    bool                        afPadding0[3];
    /** The IOMMU PCI address. */
    PCIBDF                      uPciAddress;

#ifdef IOMMU_WITH_DTE_CACHE
    /** The critsect that protects the cache from concurrent access. */
    PDMCRITSECT                 CritSectCache;
    /** Array of device IDs. */
    uint16_t                    aDeviceIds[IOMMU_DEV_CACHE_COUNT];
    /** Array of DTE cache entries. */
    DTECACHE                    aDteCache[IOMMU_DEV_CACHE_COUNT];
#endif
#ifdef IOMMU_WITH_IRTE_CACHE
    /** Array of IRTE cache entries. */
    IRTECACHE                   aIrteCache[IOMMU_IRTE_CACHE_COUNT];
#endif

    /** @name PCI: Base capability block registers.
     * @{ */
    IOMMU_BAR_T                 IommuBar;               /**< IOMMU base address register. */
    /** @} */

    /** @name MMIO: Control and status registers.
     * @{ */
    DEV_TAB_BAR_T               aDevTabBaseAddrs[8];    /**< Device table base address registers. */
    CMD_BUF_BAR_T               CmdBufBaseAddr;         /**< Command buffer base address register. */
    EVT_LOG_BAR_T               EvtLogBaseAddr;         /**< Event log base address register. */
    IOMMU_CTRL_T                Ctrl;                   /**< IOMMU control register. */
    IOMMU_EXCL_RANGE_BAR_T      ExclRangeBaseAddr;      /**< IOMMU exclusion range base register. */
    IOMMU_EXCL_RANGE_LIMIT_T    ExclRangeLimit;         /**< IOMMU exclusion range limit. */
    IOMMU_EXT_FEAT_T            ExtFeat;                /**< IOMMU extended feature register. */
    /** @} */

    /** @name MMIO: Peripheral Page Request (PPR) Log registers.
     * @{ */
    PPR_LOG_BAR_T               PprLogBaseAddr;         /**< PPR Log base address register. */
    IOMMU_HW_EVT_HI_T           HwEvtHi;                /**< IOMMU hardware event register (Hi). */
    IOMMU_HW_EVT_LO_T           HwEvtLo;                /**< IOMMU hardware event register (Lo). */
    IOMMU_HW_EVT_STATUS_T       HwEvtStatus;            /**< IOMMU hardware event status. */
    /** @} */

    /** @todo IOMMU: SMI filter. */

    /** @name MMIO: Guest Virtual-APIC Log registers.
     * @{ */
    GALOG_BAR_T                 GALogBaseAddr;          /**< Guest Virtual-APIC Log base address register. */
    GALOG_TAIL_ADDR_T           GALogTailAddr;          /**< Guest Virtual-APIC Log Tail address register. */
    /** @} */

    /** @name MMIO: Alternate PPR and Event Log registers.
     *  @{ */
    PPR_LOG_B_BAR_T             PprLogBBaseAddr;        /**< PPR Log B base address register. */
    EVT_LOG_B_BAR_T             EvtLogBBaseAddr;        /**< Event Log B base address register. */
    /** @} */

    /** @name MMIO: Device-specific feature registers.
     * @{ */
    DEV_SPECIFIC_FEAT_T         DevSpecificFeat;        /**< Device-specific feature extension register (DSFX). */
    DEV_SPECIFIC_CTRL_T         DevSpecificCtrl;        /**< Device-specific control extension register (DSCX). */
    DEV_SPECIFIC_STATUS_T       DevSpecificStatus;      /**< Device-specific status extension register (DSSX). */
    /** @} */

    /** @name MMIO: MSI Capability Block registers.
     * @{ */
    MSI_MISC_INFO_T             MiscInfo;               /**< MSI Misc. info registers / MSI Vector registers. */
    /** @} */

    /** @name MMIO: Performance Optimization Control registers.
     *  @{ */
    IOMMU_PERF_OPT_CTRL_T       PerfOptCtrl;            /**< IOMMU Performance optimization control register. */
    /** @} */

    /** @name MMIO: x2APIC Control registers.
     * @{ */
    IOMMU_XT_GEN_INTR_CTRL_T    XtGenIntrCtrl;          /**< IOMMU X2APIC General interrupt control register. */
    IOMMU_XT_PPR_INTR_CTRL_T    XtPprIntrCtrl;          /**< IOMMU X2APIC PPR interrupt control register. */
    IOMMU_XT_GALOG_INTR_CTRL_T  XtGALogIntrCtrl;        /**< IOMMU X2APIC Guest Log interrupt control register. */
    /** @} */

    /** @name MMIO: Memory Address Routing & Control (MARC) registers.
     * @{ */
    MARC_APER_T                 aMarcApers[4];          /**< MARC Aperture Registers. */
    /** @} */

    /** @name MMIO: Reserved register.
     *  @{ */
    IOMMU_RSVD_REG_T            RsvdReg;                /**< IOMMU Reserved Register. */
    /** @} */

    /** @name MMIO: Command and Event Log pointer registers.
     * @{ */
    CMD_BUF_HEAD_PTR_T          CmdBufHeadPtr;          /**< Command buffer head pointer register. */
    CMD_BUF_TAIL_PTR_T          CmdBufTailPtr;          /**< Command buffer tail pointer register. */
    EVT_LOG_HEAD_PTR_T          EvtLogHeadPtr;          /**< Event log head pointer register. */
    EVT_LOG_TAIL_PTR_T          EvtLogTailPtr;          /**< Event log tail pointer register. */
    /** @} */

    /** @name MMIO: Command and Event Status register.
     * @{ */
    IOMMU_STATUS_T              Status;                 /**< IOMMU status register. */
    /** @} */

    /** @name MMIO: PPR Log Head and Tail pointer registers.
     * @{ */
    PPR_LOG_HEAD_PTR_T          PprLogHeadPtr;          /**< IOMMU PPR log head pointer register. */
    PPR_LOG_TAIL_PTR_T          PprLogTailPtr;          /**< IOMMU PPR log tail pointer register. */
    /** @} */

    /** @name MMIO: Guest Virtual-APIC Log Head and Tail pointer registers.
     * @{ */
    GALOG_HEAD_PTR_T            GALogHeadPtr;           /**< Guest Virtual-APIC log head pointer register. */
    GALOG_TAIL_PTR_T            GALogTailPtr;           /**< Guest Virtual-APIC log tail pointer register. */
    /** @} */

    /** @name MMIO: PPR Log B Head and Tail pointer registers.
     *  @{ */
    PPR_LOG_B_HEAD_PTR_T        PprLogBHeadPtr;         /**< PPR log B head pointer register. */
    PPR_LOG_B_TAIL_PTR_T        PprLogBTailPtr;         /**< PPR log B tail pointer register. */
    /** @} */

    /** @name MMIO: Event Log B Head and Tail pointer registers.
     * @{ */
    EVT_LOG_B_HEAD_PTR_T        EvtLogBHeadPtr;         /**< Event log B head pointer register. */
    EVT_LOG_B_TAIL_PTR_T        EvtLogBTailPtr;         /**< Event log B tail pointer register. */
    /** @} */

    /** @name MMIO: PPR Log Overflow protection registers.
     * @{ */
    PPR_LOG_AUTO_RESP_T         PprLogAutoResp;         /**< PPR Log Auto Response register. */
    PPR_LOG_OVERFLOW_EARLY_T    PprLogOverflowEarly;    /**< PPR Log Overflow Early Indicator register. */
    PPR_LOG_B_OVERFLOW_EARLY_T  PprLogBOverflowEarly;   /**< PPR Log B Overflow Early Indicator register. */
    /** @} */

    /** @todo IOMMU: IOMMU Event counter registers. */

#ifdef VBOX_WITH_STATISTICS
    /** @name IOMMU: Stat counters.
     * @{ */
    STAMCOUNTER                 StatMmioReadR3;            /**< Number of MMIO reads in R3. */
    STAMCOUNTER                 StatMmioReadRZ;            /**< Number of MMIO reads in RZ. */
    STAMCOUNTER                 StatMmioWriteR3;           /**< Number of MMIO writes in R3. */
    STAMCOUNTER                 StatMmioWriteRZ;           /**< Number of MMIO writes in RZ. */

    STAMCOUNTER                 StatMsiRemapR3;            /**< Number of MSI remap requests in R3. */
    STAMCOUNTER                 StatMsiRemapRZ;            /**< Number of MSI remap requests in RZ. */

    STAMCOUNTER                 StatMemReadR3;             /**< Number of memory read translation requests in R3. */
    STAMCOUNTER                 StatMemReadRZ;             /**< Number of memory read translation requests in RZ. */
    STAMCOUNTER                 StatMemWriteR3;            /**< Number of memory write translation requests in R3. */
    STAMCOUNTER                 StatMemWriteRZ;            /**< Number of memory write translation requests in RZ. */

    STAMCOUNTER                 StatMemBulkReadR3;         /**< Number of memory read bulk translation requests in R3. */
    STAMCOUNTER                 StatMemBulkReadRZ;         /**< Number of memory read bulk translation requests in RZ. */
    STAMCOUNTER                 StatMemBulkWriteR3;        /**< Number of memory write bulk translation requests in R3. */
    STAMCOUNTER                 StatMemBulkWriteRZ;        /**< Number of memory write bulk translation requests in RZ. */

    STAMCOUNTER                 StatCmd;                   /**< Number of commands processed in total. */
    STAMCOUNTER                 StatCmdCompWait;           /**< Number of Completion Wait commands processed. */
    STAMCOUNTER                 StatCmdInvDte;             /**< Number of Invalidate DTE commands processed. */
    STAMCOUNTER                 StatCmdInvIommuPages;      /**< Number of Invalidate IOMMU pages commands processed. */
    STAMCOUNTER                 StatCmdInvIotlbPages;      /**< Number of Invalidate IOTLB pages commands processed. */
    STAMCOUNTER                 StatCmdInvIntrTable;       /**< Number of Invalidate Interrupt Table commands processed. */
    STAMCOUNTER                 StatCmdPrefIommuPages;     /**< Number of Prefetch IOMMU Pages commands processed. */
    STAMCOUNTER                 StatCmdCompletePprReq;     /**< Number of Complete PPR Requests commands processed. */
    STAMCOUNTER                 StatCmdInvIommuAll;        /**< Number of Invalidate IOMMU All commands processed. */

    STAMCOUNTER                 StatIotlbeCached;          /**< Number of IOTLB entries in the cache. */
    STAMCOUNTER                 StatIotlbeLazyEvictReuse;  /**< Number of IOTLB entries re-used after lazy eviction. */

    STAMPROFILEADV              StatProfDteLookup;         /**< Profiling of I/O page walk (from memory). */
    STAMPROFILEADV              StatProfIotlbeLookup;      /**< Profiling of IOTLB entry lookup (from cache). */

    STAMPROFILEADV              StatProfIrteLookup;        /**< Profiling of IRTE entry lookup (from memory). */
    STAMPROFILEADV              StatProfIrteCacheLookup;   /**< Profiling of IRTE entry lookup (from cache). */

    STAMCOUNTER                 StatAccessCacheHit;        /**< Number of IOTLB cache hits. */
    STAMCOUNTER                 StatAccessCacheHitFull;    /**< Number of accesses that were fully looked up from the cache. */
    STAMCOUNTER                 StatAccessCacheMiss;       /**< Number of cache misses (resulting in DTE lookups). */
    STAMCOUNTER                 StatAccessCacheNonContig;  /**< Number of cache accesses resulting in non-contiguous access. */
    STAMCOUNTER                 StatAccessCachePermDenied; /**< Number of cache accesses resulting in insufficient permissions. */
    STAMCOUNTER                 StatAccessDteNonContig;    /**< Number of DTE accesses resulting in non-contiguous access. */
    STAMCOUNTER                 StatAccessDtePermDenied;   /**< Number of DTE accesses resulting in insufficient permissions. */

    STAMCOUNTER                 StatIntrCacheHit;          /**< Number of interrupt cache hits. */
    STAMCOUNTER                 StatIntrCacheMiss;         /**< Number of interrupt cache misses. */

    STAMCOUNTER                 StatNonStdPageSize;        /**< Number of non-standard page size translations. */
    STAMCOUNTER                 StatIopfs;                 /**< Number of I/O page faults. */
    /** @} */
#endif
} IOMMU;
/** Pointer to the IOMMU device state. */
typedef IOMMU *PIOMMU;
/** Pointer to the const IOMMU device state. */
typedef const IOMMU *PCIOMMU;
AssertCompileMemberAlignment(IOMMU, hMmio, 8);
#ifdef IOMMU_WITH_DTE_CACHE
AssertCompileMemberAlignment(IOMMU, CritSectCache, 8);
AssertCompileMemberAlignment(IOMMU, aDeviceIds, 8);
AssertCompileMemberAlignment(IOMMU, aDteCache, 8);
#endif
#ifdef IOMMU_WITH_IRTE_CACHE
AssertCompileMemberAlignment(IOMMU, aIrteCache, 8);
#endif
AssertCompileMemberAlignment(IOMMU, IommuBar, 8);
AssertCompileMemberAlignment(IOMMU, aDevTabBaseAddrs, 8);
AssertCompileMemberAlignment(IOMMU, CmdBufHeadPtr, 8);
AssertCompileMemberAlignment(IOMMU, Status, 8);

/**
 * The ring-3 IOMMU device state.
 */
typedef struct IOMMUR3
{
    /** Device instance. */
    PPDMDEVINSR3                pDevInsR3;
    /** The IOMMU helpers. */
    R3PTRTYPE(PCPDMIOMMUHLPR3)  pIommuHlpR3;
    /** The command thread handle. */
    R3PTRTYPE(PPDMTHREAD)       pCmdThread;
#ifdef IOMMU_WITH_IOTLBE_CACHE
    /** Pointer to array of pre-allocated IOTLBEs. */
    PIOTLBE                     paIotlbes;
    /** Maps [DomainId,Iova] to [IOTLBE]. */
    AVLU64TREE                  TreeIotlbe;
    /** LRU list anchor for IOTLB entries. */
    RTLISTANCHOR                LstLruIotlbe;
    /** Index of the next unused IOTLB. */
    uint32_t                    idxUnusedIotlbe;
    /** Number of cached IOTLB entries in the tree. */
    uint32_t                    cCachedIotlbes;
#endif
} IOMMUR3;
/** Pointer to the ring-3 IOMMU device state. */
typedef IOMMUR3 *PIOMMUR3;
/** Pointer to the const ring-3 IOMMU device state. */
typedef const IOMMUR3 *PCIOMMUR3;
#ifdef IOMMU_WITH_IOTLBE_CACHE
AssertCompileMemberAlignment(IOMMUR3, paIotlbes, 8);
AssertCompileMemberAlignment(IOMMUR3, TreeIotlbe, 8);
AssertCompileMemberAlignment(IOMMUR3, LstLruIotlbe, 8);
#endif

/**
 * The ring-0 IOMMU device state.
 */
typedef struct IOMMUR0
{
    /** Device instance. */
    PPDMDEVINSR0                pDevInsR0;
    /** The IOMMU helpers. */
    R0PTRTYPE(PCPDMIOMMUHLPR0)  pIommuHlpR0;
} IOMMUR0;
/** Pointer to the ring-0 IOMMU device state. */
typedef IOMMUR0 *PIOMMUR0;

/**
 * The raw-mode IOMMU device state.
 */
typedef struct IOMMURC
{
    /** Device instance. */
    PPDMDEVINSRC                pDevInsRC;
    /** The IOMMU helpers. */
    RCPTRTYPE(PCPDMIOMMUHLPRC)  pIommuHlpRC;
} IOMMURC;
/** Pointer to the raw-mode IOMMU device state. */
typedef IOMMURC *PIOMMURC;

/** The IOMMU device state for the current context. */
typedef CTX_SUFF(IOMMU)  IOMMUCC;
/** Pointer to the IOMMU device state for the current context. */
typedef CTX_SUFF(PIOMMU) PIOMMUCC;

/**
 * IOMMU register access.
 */
typedef struct IOMMUREGACC
{
    const char   *pszName;
    VBOXSTRICTRC (*pfnRead)(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value);
    VBOXSTRICTRC (*pfnWrite)(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value);
} IOMMUREGACC;
/** Pointer to an IOMMU register access. */
typedef IOMMUREGACC *PIOMMUREGACC;
/** Pointer to a const IOMMU register access. */
typedef IOMMUREGACC const *PCIOMMUREGACC;

#ifdef IOMMU_WITH_IOTLBE_CACHE
/**
 * IOTLBE flush argument.
 */
typedef struct IOTLBEFLUSHARG
{
    /** The ring-3 IOMMU device state. */
    PIOMMUR3            pIommuR3;
    /** The domain ID to flush. */
    uint16_t            idDomain;
} IOTLBEFLUSHARG;
/** Pointer to an IOTLBE flush argument. */
typedef IOTLBEFLUSHARG *PIOTLBEFLUSHARG;
/** Pointer to a const IOTLBE flush argument. */
typedef IOTLBEFLUSHARG const *PCIOTLBEFLUSHARG;

/**
 * IOTLBE Info. argument.
 */
typedef struct IOTLBEINFOARG
{
    /** The ring-3 IOMMU device state. */
    PIOMMUR3            pIommuR3;
    /** The info helper. */
    PCDBGFINFOHLP       pHlp;
    /** The domain ID to dump IOTLB entry. */
    uint16_t            idDomain;
} IOTLBEINFOARG;
/** Pointer to an IOTLBE flush argument. */
typedef IOTLBEINFOARG *PIOTLBEINFOARG;
/** Pointer to a const IOTLBE flush argument. */
typedef IOTLBEINFOARG const *PCIOTLBEINFOARG;
#endif

/**
 * IOMMU operation auxiliary info.
 */
typedef struct IOMMUOPAUX
{
    /** The IOMMU operation being performed. */
    IOMMUOP         enmOp;
    /** The device table entry (can be NULL). */
    PCDTE_T         pDte;
    /** The device ID (bus, device, function). */
    uint16_t        idDevice;
    /** The domain ID (when the DTE isn't provided). */
    uint16_t        idDomain;
} IOMMUOPAUX;
/** Pointer to an I/O address lookup struct. */
typedef IOMMUOPAUX *PIOMMUOPAUX;
/** Pointer to a const I/O address lookup struct. */
typedef IOMMUOPAUX const *PCIOMMUOPAUX;

typedef DECLCALLBACKTYPE(int, FNIOPAGELOOKUP,(PPDMDEVINS pDevIns, uint64_t uIovaPage, uint8_t fPerm, PCIOMMUOPAUX pAux,
                                              PIOPAGELOOKUP pPageLookup));
typedef FNIOPAGELOOKUP *PFNIOPAGELOOKUP;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
#ifdef IN_RING3
/**
 * An array of the number of device table segments supported.
 * Indexed by u2DevTabSegSup.
 */
static uint8_t const g_acDevTabSegs[] = { 0, 2, 4, 8 };
#endif

#if (defined(IN_RING3) && defined(IOMMU_WITH_IOTLBE_CACHE)) || defined(LOG_ENABLED)
/**
 * The IOMMU I/O permission names.
 */
static const char * const g_aszPerm[] = { "none", "read", "write", "read+write" };
#endif

/**
 * An array of the masks to select the device table segment index from a device ID.
 */
static uint16_t const g_auDevTabSegMasks[] = { 0x0, 0x8000, 0xc000, 0xe000 };

/**
 * An array of the shift values to select the device table segment index from a
 * device ID.
 */
static uint8_t const g_auDevTabSegShifts[] = { 0, 15, 14, 13 };

/**
 * The maximum size (inclusive) of each device table segment (0 to 7).
 * Indexed by the device table segment index.
 */
static uint16_t const g_auDevTabSegMaxSizes[] = { 0x1ff, 0xff, 0x7f, 0x7f, 0x3f, 0x3f, 0x3f, 0x3f };


#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/**
 * Gets the maximum number of buffer entries for the given buffer length.
 *
 * @returns Number of buffer entries.
 * @param   uEncodedLen     The length (power-of-2 encoded).
 */
DECLINLINE(uint32_t) iommuAmdGetBufMaxEntries(uint8_t uEncodedLen)
{
    Assert(uEncodedLen > 7);
    Assert(uEncodedLen < 16);
    return 2 << (uEncodedLen - 1);
}


/**
 * Gets the total length of the buffer given a base register's encoded length.
 *
 * @returns The length of the buffer in bytes.
 * @param   uEncodedLen     The length (power-of-2 encoded).
 */
DECLINLINE(uint32_t) iommuAmdGetTotalBufLength(uint8_t uEncodedLen)
{
    Assert(uEncodedLen > 7);
    Assert(uEncodedLen < 16);
    return (2 << (uEncodedLen - 1)) << 4;
}


/**
 * Gets the number of (unconsumed) entries in the event log.
 *
 * @returns The number of entries in the event log.
 * @param   pThis   The shared IOMMU device state.
 */
static uint32_t iommuAmdGetEvtLogEntryCount(PIOMMU pThis)
{
    uint32_t const idxTail = pThis->EvtLogTailPtr.n.off >> IOMMU_EVT_GENERIC_SHIFT;
    uint32_t const idxHead = pThis->EvtLogHeadPtr.n.off >> IOMMU_EVT_GENERIC_SHIFT;
    if (idxTail >= idxHead)
        return idxTail - idxHead;

    uint32_t const cMaxEvts = iommuAmdGetBufMaxEntries(pThis->EvtLogBaseAddr.n.u4Len);
    return cMaxEvts - idxHead + idxTail;
}


#if (defined(IN_RING3) && defined(IOMMU_WITH_IOTLBE_CACHE)) || defined(LOG_ENABLED)
/**
 * Gets the descriptive I/O permission name for a memory access.
 *
 * @returns The I/O permission name.
 * @param   fPerm   The I/O permissions for the access, see IOMMU_IO_PERM_XXX.
 */
static const char *iommuAmdMemAccessGetPermName(uint8_t fPerm)
{
    /* We shouldn't construct an access with "none" or "read+write" (must be read or write) permissions. */
    Assert(fPerm > 0 && fPerm < RT_ELEMENTS(g_aszPerm));
    return g_aszPerm[fPerm & IOMMU_IO_PERM_MASK];
}
#endif


#ifdef IOMMU_WITH_DTE_CACHE
/**
 * Gets the basic I/O device flags for the given device table entry.
 *
 * @returns The basic I/O device flags.
 * @param   pDte    The device table entry.
 */
static uint16_t iommuAmdGetBasicDevFlags(PCDTE_T pDte)
{
    /* Extract basic flags from bits 127:0 of the DTE. */
    uint16_t fFlags = 0;
    if (pDte->n.u1Valid)
    {
        fFlags |= IOMMU_DTE_CACHE_F_VALID;

        /** @todo Skip the if checks here (shift/mask the relevant bits over).  */
        if (pDte->n.u1SuppressAllPfEvents)
            fFlags |= IOMMU_DTE_CACHE_F_SUPPRESS_ALL_IOPF;
        if (pDte->n.u1SuppressPfEvents)
            fFlags |= IOMMU_DTE_CACHE_F_SUPPRESS_IOPF;

        uint16_t const fDtePerm = (pDte->au64[0] >> IOMMU_IO_PERM_SHIFT) & IOMMU_IO_PERM_MASK;
        AssertCompile(IOMMU_DTE_CACHE_F_IO_PERM_MASK == IOMMU_IO_PERM_MASK);
        fFlags |= fDtePerm << IOMMU_DTE_CACHE_F_IO_PERM_SHIFT;
    }

    /* Extract basic flags from bits 255:128 of the DTE. */
    if (pDte->n.u1IntrMapValid)
    {
        fFlags |= IOMMU_DTE_CACHE_F_INTR_MAP_VALID;

        /** @todo Skip the if check here (shift/mask the relevant bit over).  */
        if (pDte->n.u1IgnoreUnmappedIntrs)
            fFlags |= IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR;

        uint16_t const fIntrCtrl = IOMMU_DTE_GET_INTR_CTRL(pDte);
        AssertCompile(IOMMU_DTE_CACHE_F_INTR_CTRL_MASK == IOMMU_DTE_INTR_CTRL_MASK);
        fFlags |= fIntrCtrl << IOMMU_DTE_CACHE_F_INTR_CTRL_SHIFT;
    }
    return fFlags;
}
#endif


/**
 * Remaps the source MSI to the destination MSI given the IRTE.
 *
 * @param   pMsiIn      The source MSI.
 * @param   pMsiOut     Where to store the remapped MSI.
 * @param   pIrte       The IRTE used for the remapping.
 */
static void iommuAmdIrteRemapMsi(PCMSIMSG pMsiIn, PMSIMSG pMsiOut, PCIRTE_T pIrte)
{
    /* Preserve all bits from the source MSI address and data that don't map 1:1 from the IRTE. */
    *pMsiOut = *pMsiIn;

    pMsiOut->Addr.n.u1DestMode = pIrte->n.u1DestMode;
    pMsiOut->Addr.n.u8DestId   = pIrte->n.u8Dest;

    pMsiOut->Data.n.u8Vector       = pIrte->n.u8Vector;
    pMsiOut->Data.n.u3DeliveryMode = pIrte->n.u3IntrType;
}


#ifdef IOMMU_WITH_DTE_CACHE
/**
 * Looks up an entry in the DTE cache for the given device ID.
 *
 * @returns The index of the entry, or the cache capacity if no entry was found.
 * @param   pThis       The shared IOMMU device state.
 * @param   idDevice    The device ID (bus, device, function).
 */
DECLINLINE(uint16_t) iommuAmdDteCacheEntryLookup(PIOMMU pThis, uint16_t idDevice)
{
    uint16_t const cDeviceIds = RT_ELEMENTS(pThis->aDeviceIds);
    for (uint16_t i = 0; i < cDeviceIds; i++)
    {
        if (pThis->aDeviceIds[i] == idDevice)
            return i;
    }
    return cDeviceIds;
}


/**
 * Gets an free/unused DTE cache entry.
 *
 * @returns The index of an unused entry, or cache capacity if the cache is full.
 * @param   pThis   The shared IOMMU device state.
 */
DECLINLINE(uint16_t) iommuAmdDteCacheEntryGetUnused(PCIOMMU pThis)
{
    /*
     * ASSUMES device ID 0 is the PCI host bridge or the IOMMU itself
     * (the latter being an ugly hack) and cannot be a valid device ID.
     */
    uint16_t const cDeviceIds = RT_ELEMENTS(pThis->aDeviceIds);
    for (uint16_t i = 0; i < cDeviceIds; i++)
    {
        if (!pThis->aDeviceIds[i])
            return i;
    }
    return cDeviceIds;
}


/**
 * Adds a DTE cache entry at the given index.
 *
 * @param   pThis       The shared IOMMU device state.
 * @param   idxDte      The index of the DTE cache entry.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   fFlags      Device flags to set, see IOMMU_DTE_CACHE_F_XXX.
 * @param   idDomain    The domain ID.
 *
 * @remarks Requires the cache lock to be taken.
 */
DECL_FORCE_INLINE(void) iommuAmdDteCacheAddAtIndex(PIOMMU pThis, uint16_t idxDte, uint16_t idDevice, uint16_t fFlags,
                                                   uint16_t idDomain)
{
    pThis->aDeviceIds[idxDte]         = idDevice;
    pThis->aDteCache[idxDte].fFlags   = fFlags;
    pThis->aDteCache[idxDte].idDomain = idDomain;
}


/**
 * Adds a DTE cache entry.
 *
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   pDte        The device table entry.
 */
static void iommuAmdDteCacheAdd(PPDMDEVINS pDevIns, uint16_t idDevice, PCDTE_T pDte)
{
    uint16_t const fFlags   = iommuAmdGetBasicDevFlags(pDte) | IOMMU_DTE_CACHE_F_PRESENT;
    uint16_t const idDomain = pDte->n.u16DomainId;

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    uint16_t const cDteCache = RT_ELEMENTS(pThis->aDteCache);
    uint16_t idxDte = iommuAmdDteCacheEntryLookup(pThis, idDevice);
    if (   idxDte >= cDteCache                                              /* Not found. */
        && (idxDte = iommuAmdDteCacheEntryGetUnused(pThis)) < cDteCache)    /* Get new/unused slot index. */
        iommuAmdDteCacheAddAtIndex(pThis, idxDte, idDevice, fFlags, idDomain);

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}


/**
 * Updates flags for an existing DTE cache entry given its index.
 *
 * @param   pThis       The shared IOMMU device state.
 * @param   idxDte      The index of the DTE cache entry.
 * @param   fOrMask     Device flags to add to the existing flags, see
 *                      IOMMU_DTE_CACHE_F_XXX.
 * @param   fAndMask    Device flags to remove from the existing flags, see
 *                      IOMMU_DTE_CACHE_F_XXX.
 *
 * @remarks Requires the cache lock to be taken.
 */
DECL_FORCE_INLINE(void) iommuAmdDteCacheUpdateFlagsForIndex(PIOMMU pThis, uint16_t idxDte, uint16_t fOrMask, uint16_t fAndMask)
{
    uint16_t const fOldFlags = pThis->aDteCache[idxDte].fFlags;
    uint16_t const fNewFlags = (fOldFlags | fOrMask) & ~fAndMask;
    Assert(fOldFlags & IOMMU_DTE_CACHE_F_PRESENT);
    pThis->aDteCache[idxDte].fFlags = fNewFlags;
}


#ifdef IOMMU_WITH_IOTLBE_CACHE
/**
 * Adds a new DTE cache entry or updates flags for an existing DTE cache entry.
 * If the cache is full, nothing happens.
 *
 * @param   pDevIns     The IOMMU instance data.
 * @param   pDte                The device table entry.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   fOrMask     Device flags to add to the existing flags, see
 *                      IOMMU_DTE_CACHE_F_XXX.
 * @param   fAndMask    Device flags to remove from the existing flags, see
 *                      IOMMU_DTE_CACHE_F_XXX.
 */
static void iommuAmdDteCacheAddOrUpdateFlags(PPDMDEVINS pDevIns, PCDTE_T pDte, uint16_t idDevice, uint16_t fOrMask,
                                             uint16_t fAndMask)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    uint16_t const cDteCache = RT_ELEMENTS(pThis->aDteCache);
    uint16_t idxDte = iommuAmdDteCacheEntryLookup(pThis, idDevice);
    if (idxDte < cDteCache)
        iommuAmdDteCacheUpdateFlagsForIndex(pThis, idxDte, fOrMask, fAndMask);
    else if ((idxDte = iommuAmdDteCacheEntryGetUnused(pThis)) < cDteCache)
    {
        uint16_t const fFlags = (iommuAmdGetBasicDevFlags(pDte) | IOMMU_DTE_CACHE_F_PRESENT | fOrMask) & ~fAndMask;
        iommuAmdDteCacheAddAtIndex(pThis, idxDte, idDevice, fFlags, pDte->n.u16DomainId);
    }
    /* else: cache is full, shouldn't really happen. */

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}
#endif


/**
 * Updates flags for an existing DTE cache entry.
 *
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   fOrMask     Device flags to add to the existing flags, see
 *                      IOMMU_DTE_CACHE_F_XXX.
 * @param   fAndMask    Device flags to remove from the existing flags, see
 *                      IOMMU_DTE_CACHE_F_XXX.
 */
static void iommuAmdDteCacheUpdateFlags(PPDMDEVINS pDevIns, uint16_t idDevice, uint16_t fOrMask, uint16_t fAndMask)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    uint16_t const cDteCache = RT_ELEMENTS(pThis->aDteCache);
    uint16_t const idxDte = iommuAmdDteCacheEntryLookup(pThis, idDevice);
    if (idxDte < cDteCache)
        iommuAmdDteCacheUpdateFlagsForIndex(pThis, idxDte, fOrMask, fAndMask);

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}


# ifdef IN_RING3
/**
 * Removes a DTE cache entry.
 *
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID to remove cache entries for.
 */
static void iommuAmdDteCacheRemove(PPDMDEVINS pDevIns, uint16_t idDevice)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    uint16_t const cDteCache = RT_ELEMENTS(pThis->aDteCache);
    uint16_t const idxDte    = iommuAmdDteCacheEntryLookup(pThis, idDevice);
    if (idxDte < cDteCache)
    {
        pThis->aDteCache[idxDte].fFlags   = 0;
        pThis->aDteCache[idxDte].idDomain = 0;
    }

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}


/**
 * Removes all entries in the device table entry cache.
 *
 * @param   pDevIns     The IOMMU instance data.
 */
static void iommuAmdDteCacheRemoveAll(PPDMDEVINS pDevIns)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);
    RT_ZERO(pThis->aDeviceIds);
    RT_ZERO(pThis->aDteCache);
    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}
# endif  /* IN_RING3 */
#endif  /* IOMMU_WITH_DTE_CACHE */


#ifdef IOMMU_WITH_IOTLBE_CACHE
/**
 * Moves the IOTLB entry to the least recently used slot.
 *
 * @param   pThisR3     The ring-3 IOMMU device state.
 * @param   pIotlbe     The IOTLB entry to move.
 */
DECLINLINE(void) iommuAmdIotlbEntryMoveToLru(PIOMMUR3 pThisR3, PIOTLBE pIotlbe)
{
    if (!RTListNodeIsFirst(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru))
    {
        RTListNodeRemove(&pIotlbe->NdLru);
        RTListPrepend(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru);
    }
}


/**
 * Moves the IOTLB entry to the most recently used slot.
 *
 * @param   pThisR3     The ring-3 IOMMU device state.
 * @param   pIotlbe     The IOTLB entry to move.
 */
DECLINLINE(void) iommuAmdIotlbEntryMoveToMru(PIOMMUR3 pThisR3, PIOTLBE pIotlbe)
{
    if (!RTListNodeIsLast(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru))
    {
        RTListNodeRemove(&pIotlbe->NdLru);
        RTListAppend(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru);
    }
}


# ifdef IN_RING3
/**
 * Dumps the IOTLB entry via the debug info helper.
 *
 * @returns VINF_SUCCESS.
 * @param   pNode       Pointer to an IOTLB entry to dump info.
 * @param   pvUser      Pointer to an IOTLBEINFOARG.
 */
static DECLCALLBACK(int) iommuAmdR3IotlbEntryInfo(PAVLU64NODECORE pNode, void *pvUser)
{
    /* Validate. */
    PCIOTLBEINFOARG pArgs = (PCIOTLBEINFOARG)pvUser;
    AssertPtr(pArgs);
    AssertPtr(pArgs->pIommuR3);
    AssertPtr(pArgs->pHlp);
    //Assert(pArgs->pIommuR3->u32Magic == IOMMU_MAGIC);

    uint16_t const idDomain = IOMMU_IOTLB_KEY_GET_DOMAIN_ID(pNode->Key);
    if (idDomain == pArgs->idDomain)
    {
        PCIOTLBE pIotlbe = (PCIOTLBE)pNode;
        AVLU64KEY const  uKey          = pIotlbe->Core.Key;
        uint64_t const   uIova         = IOMMU_IOTLB_KEY_GET_IOVA(uKey);
        RTGCPHYS const   GCPhysSpa     = pIotlbe->PageLookup.GCPhysSpa;
        uint8_t const    cShift        = pIotlbe->PageLookup.cShift;
        size_t const     cbPage        = RT_BIT_64(cShift);
        uint8_t const    fPerm         = pIotlbe->PageLookup.fPerm;
        const char      *pszPerm       = iommuAmdMemAccessGetPermName(fPerm);
        bool const       fEvictPending = pIotlbe->fEvictPending;

        PCDBGFINFOHLP pHlp = pArgs->pHlp;
        pHlp->pfnPrintf(pHlp, " Key           = %#RX64 (%#RX64)\n", uKey, uIova);
        pHlp->pfnPrintf(pHlp, " GCPhys        = %#RGp\n",           GCPhysSpa);
        pHlp->pfnPrintf(pHlp, " cShift        = %u (%zu bytes)\n",  cShift, cbPage);
        pHlp->pfnPrintf(pHlp, " fPerm         = %#x (%s)\n",        fPerm, pszPerm);
        pHlp->pfnPrintf(pHlp, " fEvictPending = %RTbool\n",         fEvictPending);
    }

    return VINF_SUCCESS;
}
# endif /* IN_RING3 */


/**
 * Removes the IOTLB entry if it's associated with the specified domain ID.
 *
 * @returns VINF_SUCCESS.
 * @param   pNode       Pointer to an IOTLBE.
 * @param   pvUser      Pointer to an IOTLBEFLUSHARG containing the domain ID.
 */
static DECLCALLBACK(int) iommuAmdIotlbEntryRemoveDomainId(PAVLU64NODECORE pNode, void *pvUser)
{
    /* Validate. */
    PCIOTLBEFLUSHARG pArgs = (PCIOTLBEFLUSHARG)pvUser;
    AssertPtr(pArgs);
    AssertPtr(pArgs->pIommuR3);
    //Assert(pArgs->pIommuR3->u32Magic == IOMMU_MAGIC);

    uint16_t const idDomain = IOMMU_IOTLB_KEY_GET_DOMAIN_ID(pNode->Key);
    if (idDomain == pArgs->idDomain)
    {
        /* Mark this entry is as invalidated and needs to be evicted later. */
        PIOTLBE pIotlbe = (PIOTLBE)pNode;
        pIotlbe->fEvictPending = true;
        iommuAmdIotlbEntryMoveToLru(pArgs->pIommuR3, (PIOTLBE)pNode);
    }
    return VINF_SUCCESS;
}


/**
 * Destroys an IOTLB entry that's in the tree.
 *
 * @returns VINF_SUCCESS.
 * @param   pNode       Pointer to an IOTLBE.
 * @param   pvUser      Opaque data. Currently not used, will be NULL.
 */
static DECLCALLBACK(int) iommuAmdIotlbEntryDestroy(PAVLU64NODECORE pNode, void *pvUser)
{
    RT_NOREF(pvUser);
    PIOTLBE pIotlbe = (PIOTLBE)pNode;
    Assert(pIotlbe);
    pIotlbe->NdLru.pNext = NULL;
    pIotlbe->NdLru.pPrev = NULL;
    RT_ZERO(pIotlbe->PageLookup);
    pIotlbe->fEvictPending = false;
    return VINF_SUCCESS;
}


/**
 * Inserts an IOTLB entry into the cache.
 *
 * @param   pThis           The shared IOMMU device state.
 * @param   pThisR3         The ring-3 IOMMU device state.
 * @param   pIotlbe         The IOTLB entry to initialize and insert.
 * @param   idDomain        The domain ID.
 * @param   uIova           The I/O virtual address.
 * @param   pPageLookup     The I/O page lookup result of the access.
 */
static void iommuAmdIotlbEntryInsert(PIOMMU pThis, PIOMMUR3 pThisR3, PIOTLBE pIotlbe, uint16_t idDomain, uint64_t uIova,
                                     PCIOPAGELOOKUP pPageLookup)
{
    /* Initialize the IOTLB entry with results of the I/O page walk. */
    AVLU64KEY const uKey = IOMMU_IOTLB_KEY_MAKE(idDomain, uIova);
    Assert(uKey != IOMMU_IOTLB_KEY_NIL);

    /* Check if the entry already exists. */
    PIOTLBE pFound = (PIOTLBE)RTAvlU64Get(&pThisR3->TreeIotlbe, uKey);
    if (!pFound)
    {
        /* Insert the entry into the cache. */
        pIotlbe->Core.Key   = uKey;
        pIotlbe->PageLookup = *pPageLookup;
        Assert(!pIotlbe->fEvictPending);

        bool const fInserted = RTAvlU64Insert(&pThisR3->TreeIotlbe, &pIotlbe->Core);
        Assert(fInserted); NOREF(fInserted);
        Assert(pThisR3->cCachedIotlbes < IOMMU_IOTLBE_MAX);
        ++pThisR3->cCachedIotlbes;
        STAM_COUNTER_INC(&pThis->StatIotlbeCached); NOREF(pThis);
    }
    else
    {
        /* Update the existing entry. */
        Assert(pFound->Core.Key == uKey);
        if (pFound->fEvictPending)
        {
            pFound->fEvictPending = false;
            STAM_COUNTER_INC(&pThis->StatIotlbeLazyEvictReuse); NOREF(pThis);
        }
        pFound->PageLookup = *pPageLookup;
    }
}


/**
 * Removes an IOTLB entry from the cache for the given key.
 *
 * @returns Pointer to the removed IOTLB entry, NULL if the entry wasn't found in
 *          the tree.
 * @param   pThis       The shared IOMMU device state.
 * @param   pThisR3     The ring-3 IOMMU device state.
 * @param   uKey        The key of the IOTLB entry to remove.
 */
static PIOTLBE iommuAmdIotlbEntryRemove(PIOMMU pThis, PIOMMUR3 pThisR3, AVLU64KEY uKey)
{
    PIOTLBE pIotlbe = (PIOTLBE)RTAvlU64Remove(&pThisR3->TreeIotlbe, uKey);
    if (pIotlbe)
    {
        if (pIotlbe->fEvictPending)
            STAM_COUNTER_INC(&pThis->StatIotlbeLazyEvictReuse);

        RT_ZERO(pIotlbe->Core);
        RT_ZERO(pIotlbe->PageLookup);
        /* We must not erase the LRU node connections here! */
        pIotlbe->fEvictPending = false;
        Assert(pIotlbe->Core.Key == IOMMU_IOTLB_KEY_NIL);

        Assert(pThisR3->cCachedIotlbes > 0);
        --pThisR3->cCachedIotlbes;
        STAM_COUNTER_DEC(&pThis->StatIotlbeCached); NOREF(pThis);
    }
    return pIotlbe;
}


/**
 * Looks up an IOTLB from the cache.
 *
 * @returns Pointer to IOTLB entry if found, NULL otherwise.
 * @param   pThis       The shared IOMMU device state.
 * @param   pThisR3     The ring-3 IOMMU device state.
 * @param   idDomain    The domain ID.
 * @param   uIova       The I/O virtual address.
 */
static PIOTLBE iommuAmdIotlbLookup(PIOMMU pThis, PIOMMUR3 pThisR3, uint64_t idDomain, uint64_t uIova)
{
    RT_NOREF(pThis);

    uint64_t const uKey = IOMMU_IOTLB_KEY_MAKE(idDomain, uIova);
    PIOTLBE pIotlbe = (PIOTLBE)RTAvlU64Get(&pThisR3->TreeIotlbe, uKey);
    if (    pIotlbe
        && !pIotlbe->fEvictPending)
        return pIotlbe;

    /*
     * Domain Id wildcard invalidations only marks entries for eviction later but doesn't remove
     * them from the cache immediately. We found an entry pending eviction, just return that
     * nothing was found (rather than evicting now).
     */
    return NULL;
}


/**
 * Adds an IOTLB entry to the cache.
 *
 * @param   pThis           The shared IOMMU device state.
 * @param   pThisR3         The ring-3 IOMMU device state.
 * @param   idDomain        The domain ID.
 * @param   uIovaPage       The I/O virtual address (must be 4K aligned).
 * @param   pPageLookup     The I/O page lookup result of the access.
 */
static void iommuAmdIotlbAdd(PIOMMU pThis, PIOMMUR3 pThisR3, uint16_t idDomain, uint64_t uIovaPage, PCIOPAGELOOKUP pPageLookup)
{
    Assert(!(uIovaPage & X86_PAGE_4K_OFFSET_MASK));
    Assert(pPageLookup);
    Assert(pPageLookup->cShift <= 51);
    Assert(pPageLookup->fPerm != IOMMU_IO_PERM_NONE);

    /*
     * If there are no unused IOTLB entries, evict the LRU entry.
     * Otherwise, get a new IOTLB entry from the pre-allocated list.
     */
    if (pThisR3->idxUnusedIotlbe == IOMMU_IOTLBE_MAX)
    {
        /* Grab the least recently used entry. */
        PIOTLBE pIotlbe = RTListGetFirst(&pThisR3->LstLruIotlbe, IOTLBE, NdLru);
        Assert(pIotlbe);

        /* If the entry is in the cache, remove it. */
        if (pIotlbe->Core.Key != IOMMU_IOTLB_KEY_NIL)
            iommuAmdIotlbEntryRemove(pThis, pThisR3, pIotlbe->Core.Key);

        /* Initialize and insert the IOTLB entry into the cache. */
        iommuAmdIotlbEntryInsert(pThis, pThisR3, pIotlbe, idDomain, uIovaPage, pPageLookup);

        /* Move the entry to the most recently used slot. */
        iommuAmdIotlbEntryMoveToMru(pThisR3, pIotlbe);
    }
    else
    {
        /* Grab an unused IOTLB entry from the pre-allocated list. */
        PIOTLBE pIotlbe = &pThisR3->paIotlbes[pThisR3->idxUnusedIotlbe];
        ++pThisR3->idxUnusedIotlbe;

        /* Initialize and insert the IOTLB entry into the cache. */
        iommuAmdIotlbEntryInsert(pThis, pThisR3, pIotlbe, idDomain, uIovaPage, pPageLookup);

        /* Add the entry to the most recently used slot. */
        RTListAppend(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru);
    }
}


/**
 * Removes all IOTLB entries from the cache.
 *
 * @param   pDevIns     The IOMMU instance data.
 */
static void iommuAmdIotlbRemoveAll(PPDMDEVINS pDevIns)
{
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    if (pThisR3->cCachedIotlbes > 0)
    {
        RTAvlU64Destroy(&pThisR3->TreeIotlbe, iommuAmdIotlbEntryDestroy, NULL /* pvParam */);
        RTListInit(&pThisR3->LstLruIotlbe);
        pThisR3->idxUnusedIotlbe = 0;
        pThisR3->cCachedIotlbes  = 0;
        STAM_COUNTER_RESET(&pThis->StatIotlbeCached);
    }

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}


/**
 * Removes IOTLB entries for the range of I/O virtual addresses and the specified
 * domain ID from the cache.
 *
 * @param   pDevIns         The IOMMU instance data.
 * @param   idDomain        The domain ID.
 * @param   uIova           The I/O virtual address to invalidate.
 * @param   cbInvalidate    The size of the invalidation (must be 4K aligned).
 */
static void iommuAmdIotlbRemoveRange(PPDMDEVINS pDevIns, uint16_t idDomain, uint64_t uIova, size_t cbInvalidate)
{
    /* Validate. */
    Assert(!(uIova & X86_PAGE_4K_OFFSET_MASK));
    Assert(!(cbInvalidate & X86_PAGE_4K_OFFSET_MASK));
    Assert(cbInvalidate >= X86_PAGE_4K_SIZE);

    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    do
    {
        uint64_t const uKey = IOMMU_IOTLB_KEY_MAKE(idDomain, uIova);
        PIOTLBE pIotlbe = iommuAmdIotlbEntryRemove(pThis, pThisR3, uKey);
        if (pIotlbe)
            iommuAmdIotlbEntryMoveToLru(pThisR3, pIotlbe);
        uIova        += X86_PAGE_4K_SIZE;
        cbInvalidate -= X86_PAGE_4K_SIZE;
    } while (cbInvalidate > 0);

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}


/**
 * Removes all IOTLB entries for the specified domain ID.
 *
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDomain    The domain ID.
 */
static void iommuAmdIotlbRemoveDomainId(PPDMDEVINS pDevIns, uint16_t idDomain)
{
    /*
     * We need to iterate the tree and search based on the domain ID.
     * But it seems we cannot remove items while iterating the tree.
     * Thus, we simply mark entries for eviction later but move them to the LRU
     * so they will eventually get evicted and re-cycled as the cache gets re-populated.
     */
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    IOTLBEFLUSHARG Args;
    Args.pIommuR3 = pThisR3;
    Args.idDomain = idDomain;
    RTAvlU64DoWithAll(&pThisR3->TreeIotlbe, true /* fFromLeft */, iommuAmdIotlbEntryRemoveDomainId, &Args);

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}


/**
 * Adds or updates IOTLB entries for the given range of I/O virtual addresses.
 *
 * @param   pDevIns         The IOMMU instance data.
 * @param   idDomain        The domain ID.
 * @param   uIovaPage       The I/O virtual address (must be 4K aligned).
 * @param   cbContiguous    The size of the access.
 * @param   pAddrOut        The translated I/O address lookup.
 *
 * @remarks All pages in the range specified by @c cbContiguous must have identical
 *          permissions and page sizes.
 */
static void iommuAmdIotlbAddRange(PPDMDEVINS pDevIns, uint16_t idDomain, uint64_t uIovaPage, size_t cbContiguous,
                                  PCIOPAGELOOKUP pAddrOut)
{
    Assert(!(uIovaPage & X86_PAGE_4K_OFFSET_MASK));

    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);

    IOPAGELOOKUP PageLookup;
    PageLookup.GCPhysSpa = pAddrOut->GCPhysSpa & X86_PAGE_4K_BASE_MASK;
    PageLookup.cShift    = pAddrOut->cShift;
    PageLookup.fPerm     = pAddrOut->fPerm;

    size_t const cbIova = RT_ALIGN_Z(cbContiguous, X86_PAGE_4K_SIZE);
    Assert(!(cbIova & X86_PAGE_4K_OFFSET_MASK));
    Assert(cbIova >= X86_PAGE_4K_SIZE);

    size_t cPages = cbIova / X86_PAGE_4K_SIZE;
    cPages = RT_MIN(cPages, IOMMU_IOTLBE_MAX);

    IOMMU_CACHE_LOCK(pDevIns, pThis);
    /** @todo Re-check DTE cache? */
    /*
     * Add IOTLB entries for every page in the access.
     * The page size and permissions are assumed to be identical to every
     * page in this access.
     */
    while (cPages > 0)
    {
        iommuAmdIotlbAdd(pThis, pThisR3, idDomain, uIovaPage, &PageLookup);
        uIovaPage            += X86_PAGE_4K_SIZE;
        PageLookup.GCPhysSpa += X86_PAGE_4K_SIZE;
        --cPages;
    }
    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}
#endif  /* IOMMU_WITH_IOTLBE_CACHE */


#ifdef IOMMU_WITH_IRTE_CACHE
/**
 * Looks up an IRTE cache entry.
 *
 * @returns Index of the found entry, or cache capacity if not found.
 * @param   pThis       The shared IOMMU device state.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   offIrte     The offset into the interrupt remap table.
 */
static uint16_t iommuAmdIrteCacheEntryLookup(PCIOMMU pThis, uint16_t idDevice, uint16_t offIrte)
{
    /** @todo Consider sorting and binary search when the cache capacity grows.
     *  For the IRTE cache this should be okay since typically guests do not alter the
     *  interrupt remapping once programmed, so hopefully sorting shouldn't happen
     *  often. */
    uint32_t const uKey = IOMMU_IRTE_CACHE_KEY_MAKE(idDevice, offIrte);
    uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
    for (uint16_t i = 0; i < cIrteCache; i++)
        if (pThis->aIrteCache[i].uKey == uKey)
            return i;
    return cIrteCache;
}


/**
 * Gets a free/unused IRTE cache entry.
 *
 * @returns The index of an unused entry, or cache capacity if the cache is full.
 * @param   pThis   The shared IOMMU device state.
 */
static uint16_t iommuAmdIrteCacheEntryGetUnused(PCIOMMU pThis)
{
    uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
    for (uint16_t i = 0; i < cIrteCache; i++)
        if (pThis->aIrteCache[i].uKey == IOMMU_IRTE_CACHE_KEY_NIL)
        {
            Assert(!pThis->aIrteCache[i].Irte.u32);
            return i;
        }
    return cIrteCache;
}


/**
 * Looks up the IRTE cache for the given MSI.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   enmOp       The IOMMU operation being performed.
 * @param   pMsiIn      The source MSI.
 * @param   pMsiOut     Where to store the remapped MSI.
 */
static int iommuAmdIrteCacheLookup(PPDMDEVINS pDevIns, uint16_t idDevice, IOMMUOP enmOp, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
{
    RT_NOREF(enmOp); /* May need it if we have to report errors (currently we fallback to the slower path to do that). */

    int rc = VERR_NOT_FOUND;
    /* Deal with such cases in the slower/fallback path. */
    if ((pMsiIn->Addr.u64 & VBOX_MSI_ADDR_ADDR_MASK) == VBOX_MSI_ADDR_BASE)
    { /* likely */ }
    else
        return rc;

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    uint16_t const idxDteCache = iommuAmdDteCacheEntryLookup(pThis, idDevice);
    if (idxDteCache < RT_ELEMENTS(pThis->aDteCache))
    {
        PCDTECACHE pDteCache = &pThis->aDteCache[idxDteCache];
        if ((pDteCache->fFlags & (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_INTR_MAP_VALID))
                              == (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_INTR_MAP_VALID))
        {
            Assert((pMsiIn->Addr.u64 & VBOX_MSI_ADDR_ADDR_MASK) == VBOX_MSI_ADDR_BASE);        /* Paranoia. */

            /* Currently, we only cache remapping of fixed and arbitrated interrupts. */
            uint8_t const u8DeliveryMode = pMsiIn->Data.n.u3DeliveryMode;
            if (u8DeliveryMode <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO)
            {
                uint8_t const uIntrCtrl = (pDteCache->fFlags >> IOMMU_DTE_CACHE_F_INTR_CTRL_SHIFT)
                                        & IOMMU_DTE_CACHE_F_INTR_CTRL_MASK;
                if (uIntrCtrl == IOMMU_INTR_CTRL_REMAP)
                {
                    /* Interrupt table length has been verified prior to adding entries to the cache. */
                    uint16_t const offIrte      = IOMMU_GET_IRTE_OFF(pMsiIn->Data.u32);
                    uint16_t const idxIrteCache = iommuAmdIrteCacheEntryLookup(pThis, idDevice, offIrte);
                    if (idxIrteCache < RT_ELEMENTS(pThis->aIrteCache))
                    {
                        PCIRTE_T pIrte = &pThis->aIrteCache[idxIrteCache].Irte;
                        Assert(pIrte->n.u1RemapEnable);
                        Assert(pIrte->n.u3IntrType <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO);
                        iommuAmdIrteRemapMsi(pMsiIn, pMsiOut, pIrte);
                        rc = VINF_SUCCESS;
                    }
                }
                else if (uIntrCtrl == IOMMU_INTR_CTRL_FWD_UNMAPPED)
                {
                    *pMsiOut = *pMsiIn;
                    rc = VINF_SUCCESS;
                }
            }
        }
        else if (pDteCache->fFlags & IOMMU_DTE_CACHE_F_PRESENT)
        {
            *pMsiOut = *pMsiIn;
            rc = VINF_SUCCESS;
        }
    }

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
    return rc;
}


/**
 * Adds or updates the IRTE cache for the given IRTE.
 *
 * @returns VBox status code.
 * @retval  VERR_OUT_OF_RESOURCES if the cache is full.
 *
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   offIrte     The offset into the interrupt remap table.
 * @param   pIrte       The IRTE to cache.
 */
static int iommuAmdIrteCacheAdd(PPDMDEVINS pDevIns, uint16_t idDevice, uint16_t offIrte, PCIRTE_T pIrte)
{
    Assert(offIrte != 0xffff);  /* Shouldn't be a valid IRTE table offset since sizeof(IRTE) is a multiple of 4. */

    int rc = VINF_SUCCESS;
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    Assert(idDevice != pThis->uPciAddress);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    /* Find an existing entry or get an unused slot. */
    uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
    uint16_t idxIrteCache     = iommuAmdIrteCacheEntryLookup(pThis, idDevice, offIrte);
    if (   idxIrteCache < cIrteCache
        || (idxIrteCache = iommuAmdIrteCacheEntryGetUnused(pThis)) < cIrteCache)
    {
        pThis->aIrteCache[idxIrteCache].uKey = IOMMU_IRTE_CACHE_KEY_MAKE(idDevice, offIrte);
        pThis->aIrteCache[idxIrteCache].Irte = *pIrte;
    }
    else
        rc = VERR_OUT_OF_RESOURCES;

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
    return rc;
}


# ifdef IN_RING3
/**
 * Removes IRTE cache entries for the given device ID.
 *
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID (bus, device, function).
 */
static void iommuAmdIrteCacheRemove(PPDMDEVINS pDevIns, uint16_t idDevice)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);
    uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
    for (uint16_t i = 0; i < cIrteCache; i++)
    {
        PIRTECACHE pIrteCache = &pThis->aIrteCache[i];
        if (idDevice == IOMMU_IRTE_CACHE_KEY_GET_DEVICE_ID(pIrteCache->uKey))
        {
            pIrteCache->uKey     = IOMMU_IRTE_CACHE_KEY_NIL;
            pIrteCache->Irte.u32 = 0;
            /* There could multiple IRTE entries for a device ID, continue searching. */
        }
    }
    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}


/**
 * Removes all IRTE cache entries.
 *
 * @param   pDevIns     The IOMMU instance data.
 */
static void iommuAmdIrteCacheRemoveAll(PPDMDEVINS pDevIns)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);
    uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
    for (uint16_t i = 0; i < cIrteCache; i++)
    {
        pThis->aIrteCache[i].uKey     = IOMMU_IRTE_CACHE_KEY_NIL;
        pThis->aIrteCache[i].Irte.u32 = 0;
    }
    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}
# endif /* IN_RING3 */
#endif  /* IOMMU_WITH_IRTE_CACHE */


/**
 * Atomically reads the control register without locking the IOMMU device.
 *
 * @returns The control register.
 * @param   pThis   The shared IOMMU device state.
 */
DECL_FORCE_INLINE(IOMMU_CTRL_T) iommuAmdGetCtrlUnlocked(PCIOMMU pThis)
{
    IOMMU_CTRL_T Ctrl;
    Ctrl.u64 = ASMAtomicReadU64((volatile uint64_t *)&pThis->Ctrl.u64);
    return Ctrl;
}


/**
 * Returns whether MSI is enabled for the IOMMU.
 *
 * @returns Whether MSI is enabled.
 * @param   pDevIns     The IOMMU device instance.
 *
 * @note There should be a PCIDevXxx function for this.
 */
static bool iommuAmdIsMsiEnabled(PPDMDEVINS pDevIns)
{
    MSI_CAP_HDR_T MsiCapHdr;
    MsiCapHdr.u32 = PDMPciDevGetDWord(pDevIns->apPciDevs[0], IOMMU_PCI_OFF_MSI_CAP_HDR);
    return MsiCapHdr.n.u1MsiEnable;
}


/**
 * Signals a PCI target abort.
 *
 * @param   pDevIns     The IOMMU device instance.
 */
static void iommuAmdSetPciTargetAbort(PPDMDEVINS pDevIns)
{
    PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    uint16_t const u16Status = PDMPciDevGetStatus(pPciDev) | VBOX_PCI_STATUS_SIG_TARGET_ABORT;
    PDMPciDevSetStatus(pPciDev, u16Status);
}


/**
 * Wakes up the command thread if there are commands to be processed.
 *
 * @param   pDevIns     The IOMMU device instance.
 *
 * @remarks The IOMMU lock must be held while calling this!
 */
static void iommuAmdCmdThreadWakeUpIfNeeded(PPDMDEVINS pDevIns)
{
    Log4Func(("\n"));

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    if (    pThis->Status.n.u1CmdBufRunning
        &&  pThis->CmdBufTailPtr.n.off != pThis->CmdBufHeadPtr.n.off
        && !ASMAtomicXchgBool(&pThis->fCmdThreadSignaled, true))
    {
        Log4Func(("Signaling command thread\n"));
        PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtCmdThread);
    }
}


/**
 * Reads the Device Table Base Address Register.
 */
static VBOXSTRICTRC iommuAmdDevTabBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->aDevTabBaseAddrs[0].u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Command Buffer Base Address Register.
 */
static VBOXSTRICTRC iommuAmdCmdBufBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->CmdBufBaseAddr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Event Log Base Address Register.
 */
static VBOXSTRICTRC iommuAmdEvtLogBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->EvtLogBaseAddr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Control Register.
 */
static VBOXSTRICTRC iommuAmdCtrl_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->Ctrl.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Exclusion Range Base Address Register.
 */
static VBOXSTRICTRC iommuAmdExclRangeBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->ExclRangeBaseAddr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads to the Exclusion Range Limit Register.
 */
static VBOXSTRICTRC iommuAmdExclRangeLimit_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->ExclRangeLimit.u64;
    return VINF_SUCCESS;
}


/**
 * Reads to the Extended Feature Register.
 */
static VBOXSTRICTRC iommuAmdExtFeat_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->ExtFeat.u64;
    return VINF_SUCCESS;
}


/**
 * Reads to the PPR Log Base Address Register.
 */
static VBOXSTRICTRC iommuAmdPprLogBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->PprLogBaseAddr.u64;
    return VINF_SUCCESS;
}


/**
 * Writes the Hardware Event Register (Hi).
 */
static VBOXSTRICTRC iommuAmdHwEvtHi_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->HwEvtHi.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Hardware Event Register (Lo).
 */
static VBOXSTRICTRC iommuAmdHwEvtLo_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->HwEvtLo;
    return VINF_SUCCESS;
}


/**
 * Reads the Hardware Event Status Register.
 */
static VBOXSTRICTRC iommuAmdHwEvtStatus_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->HwEvtStatus.u64;
    return VINF_SUCCESS;
}


/**
 * Reads to the GA Log Base Address Register.
 */
static VBOXSTRICTRC iommuAmdGALogBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->GALogBaseAddr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads to the PPR Log B Base Address Register.
 */
static VBOXSTRICTRC iommuAmdPprLogBBaseAddr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->PprLogBBaseAddr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads to the Event Log B Base Address Register.
 */
static VBOXSTRICTRC iommuAmdEvtLogBBaseAddr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->EvtLogBBaseAddr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Device Table Segment Base Address Register.
 */
static VBOXSTRICTRC iommuAmdDevTabSegBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns);

    /* Figure out which segment is being written. */
    uint8_t const offSegment = (offReg - IOMMU_MMIO_OFF_DEV_TAB_SEG_FIRST) >> 3;
    uint8_t const idxSegment = offSegment + 1;
    Assert(idxSegment < RT_ELEMENTS(pThis->aDevTabBaseAddrs));

    *pu64Value = pThis->aDevTabBaseAddrs[idxSegment].u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Device Specific Feature Extension (DSFX) Register.
 */
static VBOXSTRICTRC iommuAmdDevSpecificFeat_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->DevSpecificFeat.u64;
    return VINF_SUCCESS;
}

/**
 * Reads the Device Specific Control Extension (DSCX) Register.
 */
static VBOXSTRICTRC iommuAmdDevSpecificCtrl_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->DevSpecificCtrl.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Device Specific Status Extension (DSSX) Register.
 */
static VBOXSTRICTRC iommuAmdDevSpecificStatus_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->DevSpecificStatus.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the MSI Vector Register 0 (32-bit) and the MSI Vector Register 1 (32-bit).
 */
static VBOXSTRICTRC iommuAmdDevMsiVector_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    uint32_t const uLo = pThis->MiscInfo.au32[0];
    uint32_t const uHi = pThis->MiscInfo.au32[1];
    *pu64Value = RT_MAKE_U64(uLo, uHi);
    return VINF_SUCCESS;
}


/**
 * Reads the MSI Capability Header Register (32-bit) and the MSI Address (Lo)
 * Register (32-bit).
 */
static VBOXSTRICTRC iommuAmdMsiCapHdrAndAddrLo_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pThis, offReg);
    PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
    uint32_t const uLo = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR);
    uint32_t const uHi = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO);
    *pu64Value = RT_MAKE_U64(uLo, uHi);
    return VINF_SUCCESS;
}


/**
 * Reads the MSI Address (Hi) Register (32-bit) and the MSI data register (32-bit).
 */
static VBOXSTRICTRC iommuAmdMsiAddrHiAndData_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pThis, offReg);
    PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
    uint32_t const uLo = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI);
    uint32_t const uHi = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA);
    *pu64Value = RT_MAKE_U64(uLo, uHi);
    return VINF_SUCCESS;
}


/**
 * Reads the Command Buffer Head Pointer Register.
 */
static VBOXSTRICTRC iommuAmdCmdBufHeadPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->CmdBufHeadPtr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Command Buffer Tail Pointer Register.
 */
static VBOXSTRICTRC iommuAmdCmdBufTailPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->CmdBufTailPtr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Event Log Head Pointer Register.
 */
static VBOXSTRICTRC iommuAmdEvtLogHeadPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->EvtLogHeadPtr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Event Log Tail Pointer Register.
 */
static VBOXSTRICTRC iommuAmdEvtLogTailPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->EvtLogTailPtr.u64;
    return VINF_SUCCESS;
}


/**
 * Reads the Status Register.
 */
static VBOXSTRICTRC iommuAmdStatus_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
{
    RT_NOREF(pDevIns, offReg);
    *pu64Value = pThis->Status.u64;
    return VINF_SUCCESS;
}


/**
 * Writes the Device Table Base Address Register.
 */
static VBOXSTRICTRC iommuAmdDevTabBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /* Mask out all unrecognized bits. */
    u64Value &= IOMMU_DEV_TAB_BAR_VALID_MASK;

    /* Update the register. */
    pThis->aDevTabBaseAddrs[0].u64 = u64Value;

    /* Paranoia. */
    Assert(pThis->aDevTabBaseAddrs[0].n.u9Size <= g_auDevTabSegMaxSizes[0]);
    return VINF_SUCCESS;
}


/**
 * Writes the Command Buffer Base Address Register.
 */
static VBOXSTRICTRC iommuAmdCmdBufBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /*
     * While this is not explicitly specified like the event log base address register,
     * the AMD IOMMU spec. does specify "CmdBufRun must be 0b to modify the command buffer registers properly".
     * Inconsistent specs :/
     */
    if (pThis->Status.n.u1CmdBufRunning)
    {
        LogFunc(("Setting CmdBufBar (%#RX64) when command buffer is running -> Ignored\n", u64Value));
        return VINF_SUCCESS;
    }

    /* Mask out all unrecognized bits. */
    CMD_BUF_BAR_T CmdBufBaseAddr;
    CmdBufBaseAddr.u64 = u64Value & IOMMU_CMD_BUF_BAR_VALID_MASK;

    /* Validate the length. */
    if (CmdBufBaseAddr.n.u4Len >= 8)
    {
        /* Update the register. */
        pThis->CmdBufBaseAddr.u64 = CmdBufBaseAddr.u64;

        /*
         * Writing the command buffer base address, clears the command buffer head and tail pointers.
         * See AMD IOMMU spec. 2.4 "Commands".
         */
        pThis->CmdBufHeadPtr.u64 = 0;
        pThis->CmdBufTailPtr.u64 = 0;
    }
    else
        LogFunc(("Command buffer length (%#x) invalid -> Ignored\n", CmdBufBaseAddr.n.u4Len));

    return VINF_SUCCESS;
}


/**
 * Writes the Event Log Base Address Register.
 */
static VBOXSTRICTRC iommuAmdEvtLogBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /*
     * IOMMU behavior is undefined when software writes this register when event logging is running.
     * In our emulation, we ignore the write entirely.
     * See AMD IOMMU spec. "Event Log Base Address Register".
     */
    if (pThis->Status.n.u1EvtLogRunning)
    {
        LogFunc(("Setting EvtLogBar (%#RX64) when event logging is running -> Ignored\n", u64Value));
        return VINF_SUCCESS;
    }

    /* Mask out all unrecognized bits. */
    u64Value &= IOMMU_EVT_LOG_BAR_VALID_MASK;
    EVT_LOG_BAR_T EvtLogBaseAddr;
    EvtLogBaseAddr.u64 = u64Value;

    /* Validate the length. */
    if (EvtLogBaseAddr.n.u4Len >= 8)
    {
        /* Update the register. */
        pThis->EvtLogBaseAddr.u64 = EvtLogBaseAddr.u64;

        /*
         * Writing the event log base address, clears the event log head and tail pointers.
         * See AMD IOMMU spec. 2.5 "Event Logging".
         */
        pThis->EvtLogHeadPtr.u64 = 0;
        pThis->EvtLogTailPtr.u64 = 0;
    }
    else
        LogFunc(("Event log length (%#x) invalid -> Ignored\n", EvtLogBaseAddr.n.u4Len));

    return VINF_SUCCESS;
}


/**
 * Writes the Control Register.
 */
static VBOXSTRICTRC iommuAmdCtrl_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /* Mask out all unrecognized bits. */
    u64Value &= IOMMU_CTRL_VALID_MASK;
    IOMMU_CTRL_T NewCtrl;
    NewCtrl.u64 = u64Value;

    /* Ensure the device table segments are within limits. */
    if (NewCtrl.n.u3DevTabSegEn <= pThis->ExtFeat.n.u2DevTabSegSup)
    {
        IOMMU_CTRL_T const OldCtrl = pThis->Ctrl;

        /* Update the register. */
        ASMAtomicWriteU64(&pThis->Ctrl.u64, NewCtrl.u64);

        bool const fNewIommuEn = NewCtrl.n.u1IommuEn;
        bool const fOldIommuEn = OldCtrl.n.u1IommuEn;

        /* Enable or disable event logging when the bit transitions. */
        bool const fOldEvtLogEn = OldCtrl.n.u1EvtLogEn;
        bool const fNewEvtLogEn = NewCtrl.n.u1EvtLogEn;
        if (   fOldEvtLogEn != fNewEvtLogEn
            || fOldIommuEn != fNewIommuEn)
        {
            if (   fNewIommuEn
                && fNewEvtLogEn)
            {
                ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_EVT_LOG_OVERFLOW);
                ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_EVT_LOG_RUNNING);
            }
            else
                ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_EVT_LOG_RUNNING);
        }

        /* Enable or disable command buffer processing when the bit transitions. */
        bool const fOldCmdBufEn = OldCtrl.n.u1CmdBufEn;
        bool const fNewCmdBufEn = NewCtrl.n.u1CmdBufEn;
        if (   fOldCmdBufEn != fNewCmdBufEn
            || fOldIommuEn != fNewIommuEn)
        {
            if (   fNewCmdBufEn
                && fNewIommuEn)
            {
                ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_CMD_BUF_RUNNING);
                LogFunc(("Command buffer enabled\n"));

                /* Wake up the command thread to start processing commands if any. */
                iommuAmdCmdThreadWakeUpIfNeeded(pDevIns);
            }
            else
            {
                ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_CMD_BUF_RUNNING);
                LogFunc(("Command buffer disabled\n"));
            }
        }
    }
    else
    {
        LogFunc(("Invalid number of device table segments enabled, exceeds %#x (%#RX64) -> Ignored!\n",
                 pThis->ExtFeat.n.u2DevTabSegSup, NewCtrl.u64));
    }

    return VINF_SUCCESS;
}


/**
 * Writes to the Exclusion Range Base Address Register.
 */
static VBOXSTRICTRC iommuAmdExclRangeBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);
    pThis->ExclRangeBaseAddr.u64 = u64Value & IOMMU_EXCL_RANGE_BAR_VALID_MASK;
    return VINF_SUCCESS;
}


/**
 * Writes to the Exclusion Range Limit Register.
 */
static VBOXSTRICTRC iommuAmdExclRangeLimit_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);
    u64Value &= IOMMU_EXCL_RANGE_LIMIT_VALID_MASK;
    u64Value |= UINT64_C(0xfff);
    pThis->ExclRangeLimit.u64 = u64Value;
    return VINF_SUCCESS;
}


/**
 * Writes the Hardware Event Register (Hi).
 */
static VBOXSTRICTRC iommuAmdHwEvtHi_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    /** @todo IOMMU: Why the heck is this marked read/write by the AMD IOMMU spec? */
    RT_NOREF(pDevIns, offReg);
    LogFlowFunc(("Writing %#RX64 to hardware event (Hi) register!\n", u64Value));
    pThis->HwEvtHi.u64 = u64Value;
    return VINF_SUCCESS;
}


/**
 * Writes the Hardware Event Register (Lo).
 */
static VBOXSTRICTRC iommuAmdHwEvtLo_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    /** @todo IOMMU: Why the heck is this marked read/write by the AMD IOMMU spec? */
    RT_NOREF(pDevIns, offReg);
    LogFlowFunc(("Writing %#RX64 to hardware event (Lo) register!\n", u64Value));
    pThis->HwEvtLo = u64Value;
    return VINF_SUCCESS;
}


/**
 * Writes the Hardware Event Status Register.
 */
static VBOXSTRICTRC iommuAmdHwEvtStatus_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /* Mask out all unrecognized bits. */
    u64Value &= IOMMU_HW_EVT_STATUS_VALID_MASK;

    /*
     * The two bits (HEO and HEV) are RW1C (Read/Write 1-to-Clear; writing 0 has no effect).
     * If the current status bits or the bits being written are both 0, we've nothing to do.
     * The Overflow bit (bit 1) is only valid when the Valid bit (bit 0) is 1.
     */
    uint64_t HwStatus = pThis->HwEvtStatus.u64;
    if (!(HwStatus & RT_BIT(0)))
        return VINF_SUCCESS;
    if (u64Value & HwStatus & RT_BIT_64(0))
        HwStatus &= ~RT_BIT_64(0);
    if (u64Value & HwStatus & RT_BIT_64(1))
        HwStatus &= ~RT_BIT_64(1);

    /* Update the register. */
    pThis->HwEvtStatus.u64 = HwStatus;
    return VINF_SUCCESS;
}


/**
 * Writes the Device Table Segment Base Address Register.
 */
static VBOXSTRICTRC iommuAmdDevTabSegBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns);

    /* Figure out which segment is being written. */
    uint8_t const offSegment = (offReg - IOMMU_MMIO_OFF_DEV_TAB_SEG_FIRST) >> 3;
    uint8_t const idxSegment = offSegment + 1;
    Assert(idxSegment < RT_ELEMENTS(pThis->aDevTabBaseAddrs));

    /* Mask out all unrecognized bits. */
    u64Value &= IOMMU_DEV_TAB_SEG_BAR_VALID_MASK;
    DEV_TAB_BAR_T DevTabSegBar;
    DevTabSegBar.u64 = u64Value;

    /* Validate the size. */
    uint16_t const uSegSize    = DevTabSegBar.n.u9Size;
    uint16_t const uMaxSegSize = g_auDevTabSegMaxSizes[idxSegment];
    if (uSegSize <= uMaxSegSize)
    {
        /* Update the register. */
        pThis->aDevTabBaseAddrs[idxSegment].u64 = u64Value;
    }
    else
        LogFunc(("Device table segment (%u) size invalid (%#RX32) -> Ignored\n", idxSegment, uSegSize));

    return VINF_SUCCESS;
}


/**
 * Writes the MSI Vector Register 0 (32-bit) and the MSI Vector Register 1 (32-bit).
 */
static VBOXSTRICTRC iommuAmdDevMsiVector_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /* MSI Vector Register 0 is read-only. */
    /* MSI Vector Register 1. */
    uint32_t const uReg = u64Value >> 32;
    pThis->MiscInfo.au32[1] = uReg & IOMMU_MSI_VECTOR_1_VALID_MASK;
    return VINF_SUCCESS;
}


/**
 * Writes the MSI Capability Header Register (32-bit) or the MSI Address (Lo)
 * Register (32-bit).
 */
static VBOXSTRICTRC iommuAmdMsiCapHdrAndAddrLo_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pThis, offReg);
    PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);

    /* MSI capability header. */
    {
        uint32_t const uReg = u64Value;
        MSI_CAP_HDR_T MsiCapHdr;
        MsiCapHdr.u32           = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR);
        MsiCapHdr.n.u1MsiEnable = RT_BOOL(uReg & IOMMU_MSI_CAP_HDR_MSI_EN_MASK);
        PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR, MsiCapHdr.u32);
    }

    /* MSI Address Lo. */
    {
        uint32_t const uReg = u64Value >> 32;
        uint32_t const uMsiAddrLo = uReg & VBOX_MSI_ADDR_VALID_MASK;
        PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO, uMsiAddrLo);
    }

    return VINF_SUCCESS;
}


/**
 * Writes the MSI Address (Hi) Register (32-bit) or the MSI data register (32-bit).
 */
static VBOXSTRICTRC iommuAmdMsiAddrHiAndData_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pThis, offReg);
    PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);

    /* MSI Address Hi. */
    {
        uint32_t const uReg = u64Value;
        PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI, uReg);
    }

    /* MSI Data. */
    {
        uint32_t const uReg = u64Value >> 32;
        uint32_t const uMsiData = uReg & VBOX_MSI_DATA_VALID_MASK;
        PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA, uMsiData);
    }

    return VINF_SUCCESS;
}


/**
 * Writes the Command Buffer Head Pointer Register.
 */
static VBOXSTRICTRC iommuAmdCmdBufHeadPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /*
     * IOMMU behavior is undefined when software writes this register when the command buffer is running.
     * In our emulation, we ignore the write entirely.
     * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
     */
    if (pThis->Status.n.u1CmdBufRunning)
    {
        LogFunc(("Setting CmdBufHeadPtr (%#RX64) when command buffer is running -> Ignored\n", u64Value));
        return VINF_SUCCESS;
    }

    /*
     * IOMMU behavior is undefined when software writes a value outside the buffer length.
     * In our emulation, we ignore the write entirely.
     */
    uint32_t const offBuf = u64Value & IOMMU_CMD_BUF_HEAD_PTR_VALID_MASK;
    uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
    Assert(cbBuf <= _512K);
    if (offBuf >= cbBuf)
    {
        LogFunc(("Setting CmdBufHeadPtr (%#RX32) to a value that exceeds buffer length (%#RX23) -> Ignored\n", offBuf, cbBuf));
        return VINF_SUCCESS;
    }

    /* Update the register. */
    pThis->CmdBufHeadPtr.au32[0] = offBuf;

    iommuAmdCmdThreadWakeUpIfNeeded(pDevIns);

    Log4Func(("Set CmdBufHeadPtr to %#RX32\n", offBuf));
    return VINF_SUCCESS;
}


/**
 * Writes the Command Buffer Tail Pointer Register.
 */
static VBOXSTRICTRC iommuAmdCmdBufTailPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /*
     * IOMMU behavior is undefined when software writes a value outside the buffer length.
     * In our emulation, we ignore the write entirely.
     * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
     */
    uint32_t const offBuf = u64Value & IOMMU_CMD_BUF_TAIL_PTR_VALID_MASK;
    uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
    Assert(cbBuf <= _512K);
    if (offBuf >= cbBuf)
    {
        LogFunc(("Setting CmdBufTailPtr (%#RX32) to a value that exceeds buffer length (%#RX32) -> Ignored\n", offBuf, cbBuf));
        return VINF_SUCCESS;
    }

    /*
     * IOMMU behavior is undefined if software advances the tail pointer equal to or beyond the
     * head pointer after adding one or more commands to the buffer.
     *
     * However, we cannot enforce this strictly because it's legal for software to shrink the
     * command queue (by reducing the offset) as well as wrap around the pointer (when head isn't
     * at 0). Software might even make the queue empty by making head and tail equal which is
     * allowed. I don't think we can or should try too hard to prevent software shooting itself
     * in the foot here. As long as we make sure the offset value is within the circular buffer
     * bounds (which we do by masking bits above) it should be sufficient.
     */
    pThis->CmdBufTailPtr.au32[0] = offBuf;

    iommuAmdCmdThreadWakeUpIfNeeded(pDevIns);

    Log4Func(("Set CmdBufTailPtr to %#RX32\n", offBuf));
    return VINF_SUCCESS;
}


/**
 * Writes the Event Log Head Pointer Register.
 */
static VBOXSTRICTRC iommuAmdEvtLogHeadPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /*
     * IOMMU behavior is undefined when software writes a value outside the buffer length.
     * In our emulation, we ignore the write entirely.
     * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
     */
    uint32_t const offBuf = u64Value & IOMMU_EVT_LOG_HEAD_PTR_VALID_MASK;
    uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
    Assert(cbBuf <= _512K);
    if (offBuf >= cbBuf)
    {
        LogFunc(("Setting EvtLogHeadPtr (%#RX32) to a value that exceeds buffer length (%#RX32) -> Ignored\n", offBuf, cbBuf));
        return VINF_SUCCESS;
    }

    /* Update the register. */
    pThis->EvtLogHeadPtr.au32[0] = offBuf;

    Log4Func(("Set EvtLogHeadPtr to %#RX32\n", offBuf));
    return VINF_SUCCESS;
}


/**
 * Writes the Event Log Tail Pointer Register.
 */
static VBOXSTRICTRC iommuAmdEvtLogTailPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);
    NOREF(pThis);

    /*
     * IOMMU behavior is undefined when software writes this register when the event log is running.
     * In our emulation, we ignore the write entirely.
     * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
     */
    if (pThis->Status.n.u1EvtLogRunning)
    {
        LogFunc(("Setting EvtLogTailPtr (%#RX64) when event log is running -> Ignored\n", u64Value));
        return VINF_SUCCESS;
    }

    /*
     * IOMMU behavior is undefined when software writes a value outside the buffer length.
     * In our emulation, we ignore the write entirely.
     */
    uint32_t const offBuf = u64Value & IOMMU_EVT_LOG_TAIL_PTR_VALID_MASK;
    uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
    Assert(cbBuf <= _512K);
    if (offBuf >= cbBuf)
    {
        LogFunc(("Setting EvtLogTailPtr (%#RX32) to a value that exceeds buffer length (%#RX32) -> Ignored\n", offBuf, cbBuf));
        return VINF_SUCCESS;
    }

    /* Update the register. */
    pThis->EvtLogTailPtr.au32[0] = offBuf;

    Log4Func(("Set EvtLogTailPtr to %#RX32\n", offBuf));
    return VINF_SUCCESS;
}


/**
 * Writes the Status Register.
 */
static VBOXSTRICTRC iommuAmdStatus_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
{
    RT_NOREF(pDevIns, offReg);

    /* Mask out all unrecognized bits. */
    u64Value &= IOMMU_STATUS_VALID_MASK;

    /*
     * Compute RW1C (read-only, write-1-to-clear) bits and preserve the rest (which are read-only).
     * Writing 0 to an RW1C bit has no effect. Writing 1 to an RW1C bit, clears the bit if it's already 1.
     */
    IOMMU_STATUS_T const OldStatus = pThis->Status;
    uint64_t const fOldRw1cBits = (OldStatus.u64 &  IOMMU_STATUS_RW1C_MASK);
    uint64_t const fOldRoBits   = (OldStatus.u64 & ~IOMMU_STATUS_RW1C_MASK);
    uint64_t const fNewRw1cBits = (u64Value      &  IOMMU_STATUS_RW1C_MASK);

    uint64_t const uNewStatus = (fOldRw1cBits & ~fNewRw1cBits) | fOldRoBits;

    /* Update the register. */
    ASMAtomicWriteU64(&pThis->Status.u64, uNewStatus);
    return VINF_SUCCESS;
}


/**
 * Register access table 0.
 * The MMIO offset of each entry must be a multiple of 8!
 */
static const IOMMUREGACC g_aRegAccess0[] =
{
    /* MMIO off.   Register name                           Read function                 Write function */
    { /* 0x00  */  "DEV_TAB_BAR",                          iommuAmdDevTabBar_r,          iommuAmdDevTabBar_w           },
    { /* 0x08  */  "CMD_BUF_BAR",                          iommuAmdCmdBufBar_r,          iommuAmdCmdBufBar_w           },
    { /* 0x10  */  "EVT_LOG_BAR",                          iommuAmdEvtLogBar_r,          iommuAmdEvtLogBar_w           },
    { /* 0x18  */  "CTRL",                                 iommuAmdCtrl_r,               iommuAmdCtrl_w                },
    { /* 0x20  */  "EXCL_BAR",                             iommuAmdExclRangeBar_r,       iommuAmdExclRangeBar_w        },
    { /* 0x28  */  "EXCL_RANGE_LIMIT",                     iommuAmdExclRangeLimit_r,     iommuAmdExclRangeLimit_w      },
    { /* 0x30  */  "EXT_FEAT",                             iommuAmdExtFeat_r,            NULL                          },
    { /* 0x38  */  "PPR_LOG_BAR",                          iommuAmdPprLogBar_r,          NULL                          },
    { /* 0x40  */  "HW_EVT_HI",                            iommuAmdHwEvtHi_r,            iommuAmdHwEvtHi_w             },
    { /* 0x48  */  "HW_EVT_LO",                            iommuAmdHwEvtLo_r,            iommuAmdHwEvtLo_w             },
    { /* 0x50  */  "HW_EVT_STATUS",                        iommuAmdHwEvtStatus_r,        iommuAmdHwEvtStatus_w         },
    { /* 0x58  */  NULL,                                   NULL,                         NULL                          },

    { /* 0x60  */  "SMI_FLT_0",                            NULL,                         NULL                          },
    { /* 0x68  */  "SMI_FLT_1",                            NULL,                         NULL                          },
    { /* 0x70  */  "SMI_FLT_2",                            NULL,                         NULL                          },
    { /* 0x78  */  "SMI_FLT_3",                            NULL,                         NULL                          },
    { /* 0x80  */  "SMI_FLT_4",                            NULL,                         NULL                          },
    { /* 0x88  */  "SMI_FLT_5",                            NULL,                         NULL                          },
    { /* 0x90  */  "SMI_FLT_6",                            NULL,                         NULL                          },
    { /* 0x98  */  "SMI_FLT_7",                            NULL,                         NULL                          },
    { /* 0xa0  */  "SMI_FLT_8",                            NULL,                         NULL                          },
    { /* 0xa8  */  "SMI_FLT_9",                            NULL,                         NULL                          },
    { /* 0xb0  */  "SMI_FLT_10",                           NULL,                         NULL                          },
    { /* 0xb8  */  "SMI_FLT_11",                           NULL,                         NULL                          },
    { /* 0xc0  */  "SMI_FLT_12",                           NULL,                         NULL                          },
    { /* 0xc8  */  "SMI_FLT_13",                           NULL,                         NULL                          },
    { /* 0xd0  */  "SMI_FLT_14",                           NULL,                         NULL                          },
    { /* 0xd8  */  "SMI_FLT_15",                           NULL,                         NULL                          },

    { /* 0xe0  */  "GALOG_BAR",                            iommuAmdGALogBar_r,           NULL                          },
    { /* 0xe8  */  "GALOG_TAIL_ADDR",                      NULL,                         NULL                          },
    { /* 0xf0  */  "PPR_LOG_B_BAR",                        iommuAmdPprLogBBaseAddr_r,    NULL                          },
    { /* 0xf8  */  "PPR_EVT_B_BAR",                        iommuAmdEvtLogBBaseAddr_r,    NULL                          },

    { /* 0x100 */  "DEV_TAB_SEG_1",                        iommuAmdDevTabSegBar_r,       iommuAmdDevTabSegBar_w        },
    { /* 0x108 */  "DEV_TAB_SEG_2",                        iommuAmdDevTabSegBar_r,       iommuAmdDevTabSegBar_w        },
    { /* 0x110 */  "DEV_TAB_SEG_3",                        iommuAmdDevTabSegBar_r,       iommuAmdDevTabSegBar_w        },
    { /* 0x118 */  "DEV_TAB_SEG_4",                        iommuAmdDevTabSegBar_r,       iommuAmdDevTabSegBar_w        },
    { /* 0x120 */  "DEV_TAB_SEG_5",                        iommuAmdDevTabSegBar_r,       iommuAmdDevTabSegBar_w        },
    { /* 0x128 */  "DEV_TAB_SEG_6",                        iommuAmdDevTabSegBar_r,       iommuAmdDevTabSegBar_w        },
    { /* 0x130 */  "DEV_TAB_SEG_7",                        iommuAmdDevTabSegBar_r,       iommuAmdDevTabSegBar_w        },

    { /* 0x138 */  "DEV_SPECIFIC_FEAT",                    iommuAmdDevSpecificFeat_r,    NULL                          },
    { /* 0x140 */  "DEV_SPECIFIC_CTRL",                    iommuAmdDevSpecificCtrl_r,    NULL                          },
    { /* 0x148 */  "DEV_SPECIFIC_STATUS",                  iommuAmdDevSpecificStatus_r,  NULL                          },

    { /* 0x150 */  "MSI_VECTOR_0 or MSI_VECTOR_1",         iommuAmdDevMsiVector_r,       iommuAmdDevMsiVector_w        },
    { /* 0x158 */  "MSI_CAP_HDR or MSI_ADDR_LO",           iommuAmdMsiCapHdrAndAddrLo_r, iommuAmdMsiCapHdrAndAddrLo_w  },
    { /* 0x160 */  "MSI_ADDR_HI or MSI_DATA",              iommuAmdMsiAddrHiAndData_r,   iommuAmdMsiAddrHiAndData_w    },
    { /* 0x168 */  "MSI_MAPPING_CAP_HDR or PERF_OPT_CTRL", NULL,                         NULL                          },

    { /* 0x170 */  "XT_GEN_INTR_CTRL",                     NULL,                         NULL                          },
    { /* 0x178 */  "XT_PPR_INTR_CTRL",                     NULL,                         NULL                          },
    { /* 0x180 */  "XT_GALOG_INT_CTRL",                    NULL,                         NULL                          },
};
AssertCompile(RT_ELEMENTS(g_aRegAccess0) == (IOMMU_MMIO_OFF_QWORD_TABLE_0_END - IOMMU_MMIO_OFF_QWORD_TABLE_0_START) / 8);

/**
 * Register access table 1.
 * The MMIO offset of each entry must be a multiple of 8!
 */
static const IOMMUREGACC g_aRegAccess1[] =
{
    /* MMIO offset   Register name         Read function   Write function */
    { /* 0x200 */    "MARC_APER_BAR_0",    NULL,           NULL },
    { /* 0x208 */    "MARC_APER_RELOC_0",  NULL,           NULL },
    { /* 0x210 */    "MARC_APER_LEN_0",    NULL,           NULL },
    { /* 0x218 */    "MARC_APER_BAR_1",    NULL,           NULL },
    { /* 0x220 */    "MARC_APER_RELOC_1",  NULL,           NULL },
    { /* 0x228 */    "MARC_APER_LEN_1",    NULL,           NULL },
    { /* 0x230 */    "MARC_APER_BAR_2",    NULL,           NULL },
    { /* 0x238 */    "MARC_APER_RELOC_2",  NULL,           NULL },
    { /* 0x240 */    "MARC_APER_LEN_2",    NULL,           NULL },
    { /* 0x248 */    "MARC_APER_BAR_3",    NULL,           NULL },
    { /* 0x250 */    "MARC_APER_RELOC_3",  NULL,           NULL },
    { /* 0x258 */    "MARC_APER_LEN_3",    NULL,           NULL }
};
AssertCompile(RT_ELEMENTS(g_aRegAccess1) == (IOMMU_MMIO_OFF_QWORD_TABLE_1_END - IOMMU_MMIO_OFF_QWORD_TABLE_1_START) / 8);

/**
 * Register access table 2.
 * The MMIO offset of each entry must be a multiple of 8!
 */
static const IOMMUREGACC g_aRegAccess2[] =
{
    /* MMIO offset    Register name               Read Function             Write function */
    { /* 0x1ff8 */    "RSVD_REG",                 NULL,                     NULL                    },

    { /* 0x2000 */    "CMD_BUF_HEAD_PTR",         iommuAmdCmdBufHeadPtr_r,  iommuAmdCmdBufHeadPtr_w },
    { /* 0x2008 */    "CMD_BUF_TAIL_PTR",         iommuAmdCmdBufTailPtr_r , iommuAmdCmdBufTailPtr_w },
    { /* 0x2010 */    "EVT_LOG_HEAD_PTR",         iommuAmdEvtLogHeadPtr_r,  iommuAmdEvtLogHeadPtr_w },
    { /* 0x2018 */    "EVT_LOG_TAIL_PTR",         iommuAmdEvtLogTailPtr_r,  iommuAmdEvtLogTailPtr_w },

    { /* 0x2020 */    "STATUS",                   iommuAmdStatus_r,         iommuAmdStatus_w        },
    { /* 0x2028 */    NULL,                       NULL,                     NULL                    },

    { /* 0x2030 */    "PPR_LOG_HEAD_PTR",         NULL,                     NULL                    },
    { /* 0x2038 */    "PPR_LOG_TAIL_PTR",         NULL,                     NULL                    },

    { /* 0x2040 */    "GALOG_HEAD_PTR",           NULL,                     NULL                    },
    { /* 0x2048 */    "GALOG_TAIL_PTR",           NULL,                     NULL                    },

    { /* 0x2050 */    "PPR_LOG_B_HEAD_PTR",       NULL,                     NULL                    },
    { /* 0x2058 */    "PPR_LOG_B_TAIL_PTR",       NULL,                     NULL                    },

    { /* 0x2060 */    NULL,                       NULL,                     NULL                    },
    { /* 0x2068 */    NULL,                       NULL,                     NULL                    },

    { /* 0x2070 */    "EVT_LOG_B_HEAD_PTR",       NULL,                     NULL                    },
    { /* 0x2078 */    "EVT_LOG_B_TAIL_PTR",       NULL,                     NULL                    },

    { /* 0x2080 */    "PPR_LOG_AUTO_RESP",        NULL,                     NULL                    },
    { /* 0x2088 */    "PPR_LOG_OVERFLOW_EARLY",   NULL,                     NULL                    },
    { /* 0x2090 */    "PPR_LOG_B_OVERFLOW_EARLY", NULL,                     NULL                    }
};
AssertCompile(RT_ELEMENTS(g_aRegAccess2) == (IOMMU_MMIO_OFF_QWORD_TABLE_2_END - IOMMU_MMIO_OFF_QWORD_TABLE_2_START) / 8);


/**
 * Gets the register access structure given its MMIO offset.
 *
 * @returns The register access structure, or NULL if the offset is invalid.
 * @param   off     The MMIO offset of the register being accessed.
 */
static PCIOMMUREGACC iommuAmdGetRegAccess(uint32_t off)
{
    /* Figure out which table the register belongs to and validate its index. */
    PCIOMMUREGACC pReg;
    if (off < IOMMU_MMIO_OFF_QWORD_TABLE_0_END)
    {
        uint32_t const idxReg = off >> 3;
        Assert(idxReg < RT_ELEMENTS(g_aRegAccess0));
        pReg = &g_aRegAccess0[idxReg];
    }
    else if (   off <  IOMMU_MMIO_OFF_QWORD_TABLE_1_END
             && off >= IOMMU_MMIO_OFF_QWORD_TABLE_1_START)
    {
        uint32_t const idxReg = (off - IOMMU_MMIO_OFF_QWORD_TABLE_1_START) >> 3;
        Assert(idxReg < RT_ELEMENTS(g_aRegAccess1));
        pReg = &g_aRegAccess1[idxReg];
    }
    else if (   off <  IOMMU_MMIO_OFF_QWORD_TABLE_2_END
             && off >= IOMMU_MMIO_OFF_QWORD_TABLE_2_START)
    {
        uint32_t const idxReg = (off - IOMMU_MMIO_OFF_QWORD_TABLE_2_START) >> 3;
        Assert(idxReg < RT_ELEMENTS(g_aRegAccess2));
        pReg = &g_aRegAccess2[idxReg];
    }
    else
        pReg = NULL;
    return pReg;
}


/**
 * Writes an IOMMU register (32-bit and 64-bit).
 *
 * @returns Strict VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   off         MMIO byte offset to the register.
 * @param   cb          The size of the write access.
 * @param   uValue      The value being written.
 *
 * @thread  EMT.
 */
static VBOXSTRICTRC iommuAmdRegisterWrite(PPDMDEVINS pDevIns, uint32_t off, uint8_t cb, uint64_t uValue)
{
    /*
     * Validate the access in case of IOM bug or incorrect assumption.
     */
    Assert(off < IOMMU_MMIO_REGION_SIZE);
    AssertMsgReturn(cb == 4 || cb == 8, ("Invalid access size %u\n", cb), VINF_SUCCESS);
    AssertMsgReturn(!(off & 3), ("Invalid offset %#x\n", off), VINF_SUCCESS);

    Log4Func(("off=%#x cb=%u uValue=%#RX64\n", off, cb, uValue));

    PIOMMU        pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUCC      pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
    PCIOMMUREGACC pReg    = iommuAmdGetRegAccess(off);
    if (pReg)
    { /* likely */ }
    else
    {
        LogFunc(("Writing unknown register %#x with %#RX64 -> Ignored\n", off, uValue));
        return VINF_SUCCESS;
    }

    /* If a write handler doesn't exist, it's either a reserved or read-only register. */
    if (pReg->pfnWrite)
    { /* likely */ }
    else
    {
        LogFunc(("Writing reserved or read-only register off=%#x (cb=%u) with %#RX64 -> Ignored\n", off, cb, uValue));
        return VINF_SUCCESS;
    }

    /*
     * If the write access is 64-bits and aligned on a 64-bit boundary, dispatch right away.
     * This handles writes to 64-bit registers as well as aligned, 64-bit writes to two
     * consecutive 32-bit registers.
     */
    if (cb == 8)
    {
        if (!(off & 7))
        {
            IOMMU_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_WRITE);
            VBOXSTRICTRC rcStrict = pReg->pfnWrite(pDevIns, pThis, off, uValue);
            IOMMU_UNLOCK(pDevIns, pThisCC);
            return rcStrict;
        }

        LogFunc(("Misaligned access while writing register at off=%#x (cb=%u) with %#RX64 -> Ignored\n", off, cb, uValue));
        return VINF_SUCCESS;
    }

    /* We shouldn't get sizes other than 32 bits here as we've specified so with IOM. */
    Assert(cb == 4);
    if (!(off & 7))
    {
        VBOXSTRICTRC rcStrict;
        IOMMU_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_WRITE);

        /*
         * Lower 32 bits of a 64-bit register or a 32-bit register is being written.
         * Merge with higher 32 bits (after reading the full 64-bits) and perform a 64-bit write.
         */
        uint64_t u64Read;
        if (pReg->pfnRead)
            rcStrict = pReg->pfnRead(pDevIns, pThis, off, &u64Read);
        else
        {
            rcStrict = VINF_SUCCESS;
            u64Read = 0;
        }

        if (RT_SUCCESS(rcStrict))
        {
            uValue = (u64Read & UINT64_C(0xffffffff00000000)) | uValue;
            rcStrict = pReg->pfnWrite(pDevIns, pThis, off, uValue);
        }
        else
            LogFunc(("Reading off %#x during split write failed! rc=%Rrc\n -> Ignored", off, VBOXSTRICTRC_VAL(rcStrict)));

        IOMMU_UNLOCK(pDevIns, pThisCC);
        return rcStrict;
    }

    /*
     * Higher 32 bits of a 64-bit register or a 32-bit register at a 32-bit boundary is being written.
     * Merge with lower 32 bits (after reading the full 64-bits) and perform a 64-bit write.
     */
    VBOXSTRICTRC rcStrict;
    Assert(!(off & 3));
    Assert(off & 7);
    Assert(off >= 4);
    uint64_t u64Read;
    IOMMU_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_WRITE);
    if (pReg->pfnRead)
        rcStrict = pReg->pfnRead(pDevIns, pThis, off - 4, &u64Read);
    else
    {
        rcStrict = VINF_SUCCESS;
        u64Read = 0;
    }

    if (RT_SUCCESS(rcStrict))
    {
        uValue = (uValue << 32) | (u64Read & UINT64_C(0xffffffff));
        rcStrict = pReg->pfnWrite(pDevIns, pThis, off - 4, uValue);
    }
    else
        LogFunc(("Reading off %#x during split write failed! rc=%Rrc\n -> Ignored", off, VBOXSTRICTRC_VAL(rcStrict)));

    IOMMU_UNLOCK(pDevIns, pThisCC);
    return rcStrict;
}


/**
 * Reads an IOMMU register (64-bit) given its MMIO offset.
 *
 * All reads are 64-bit but reads to 32-bit registers that are aligned on an 8-byte
 * boundary include the lower half of the subsequent register.
 *
 * This is because most registers are 64-bit and aligned on 8-byte boundaries but
 * some are really 32-bit registers aligned on an 8-byte boundary. We cannot assume
 * software will only perform 32-bit reads on those 32-bit registers that are
 * aligned on 8-byte boundaries.
 *
 * @returns Strict VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   off         The MMIO offset of the register in bytes.
 * @param   puResult    Where to store the value being read.
 *
 * @thread  EMT.
 */
static VBOXSTRICTRC iommuAmdRegisterRead(PPDMDEVINS pDevIns, uint32_t off, uint64_t *puResult)
{
    Assert(off < IOMMU_MMIO_REGION_SIZE);
    Assert(!(off & 7) || !(off & 3));

    Log4Func(("off=%#x\n", off));

    PIOMMU      pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUCC    pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
    PCPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev); NOREF(pPciDev);

    PCIOMMUREGACC pReg = iommuAmdGetRegAccess(off);
    if (pReg)
    { /* likely */ }
    else
    {
        LogFunc(("Reading unknown register %#x -> Ignored\n", off));
        return VINF_IOM_MMIO_UNUSED_FF;
    }

    /* If a read handler doesn't exist, it's a reserved or unknown register. */
    if (pReg->pfnRead)
    { /* likely */ }
    else
    {
        LogFunc(("Reading reserved or unknown register off=%#x -> returning 0s\n", off));
        return VINF_IOM_MMIO_UNUSED_00;
    }

    /*
     * If the read access is aligned on a 64-bit boundary, read the full 64-bits and return.
     * The caller takes care of truncating upper 32 bits for 32-bit reads.
     */
    if (!(off & 7))
    {
        IOMMU_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_READ);
        VBOXSTRICTRC rcStrict = pReg->pfnRead(pDevIns, pThis, off, puResult);
        IOMMU_UNLOCK(pDevIns, pThisCC);
        return rcStrict;
    }

    /*
     * High 32 bits of a 64-bit register or a 32-bit register at a non 64-bit boundary is being read.
     * Read full 64 bits at the previous 64-bit boundary but return only the high 32 bits.
     */
    Assert(!(off & 3));
    Assert(off & 7);
    Assert(off >= 4);
    IOMMU_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_READ);
    VBOXSTRICTRC rcStrict = pReg->pfnRead(pDevIns, pThis, off - 4, puResult);
    IOMMU_UNLOCK(pDevIns, pThisCC);
    if (RT_SUCCESS(rcStrict))
        *puResult >>= 32;
    else
    {
        *puResult = 0;
        LogFunc(("Reading off %#x during split read failed! rc=%Rrc\n -> Ignored", off, VBOXSTRICTRC_VAL(rcStrict)));
    }

    return rcStrict;
}


/**
 * Raises the MSI interrupt for the IOMMU device.
 *
 * @param   pDevIns     The IOMMU device instance.
 *
 * @thread  Any.
 * @remarks The IOMMU lock may or may not be held.
 */
static void iommuAmdMsiInterruptRaise(PPDMDEVINS pDevIns)
{
    LogFlowFunc(("\n"));
    if (iommuAmdIsMsiEnabled(pDevIns))
    {
        LogFunc(("Raising MSI\n"));
        PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
    }
}

#if 0
/**
 * Clears the MSI interrupt for the IOMMU device.
 *
 * @param   pDevIns     The IOMMU device instance.
 *
 * @thread  Any.
 * @remarks The IOMMU lock may or may not be held.
 */
static void iommuAmdMsiInterruptClear(PPDMDEVINS pDevIns)
{
    if (iommuAmdIsMsiEnabled(pDevIns))
        PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
}
#endif

/**
 * Writes an entry to the event log in memory.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   pEvent      The event to log.
 *
 * @thread  Any.
 * @remarks The IOMMU lock must be held while calling this function.
 */
static int iommuAmdEvtLogEntryWrite(PPDMDEVINS pDevIns, PCEVT_GENERIC_T pEvent)
{
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);

    IOMMU_LOCK(pDevIns, pThisCC);

    /* Check if event logging is active and the log has not overflowed. */
    IOMMU_STATUS_T const Status = pThis->Status;
    if (   Status.n.u1EvtLogRunning
        && !Status.n.u1EvtOverflow)
    {
        uint32_t const cbEvt = sizeof(*pEvent);

        /* Get the offset we need to write the event to in memory (circular buffer offset). */
        uint32_t const offEvt = pThis->EvtLogTailPtr.n.off;
        Assert(!(offEvt & ~IOMMU_EVT_LOG_TAIL_PTR_VALID_MASK));

        /* Ensure we have space in the event log. */
        uint32_t const cMaxEvts = iommuAmdGetBufMaxEntries(pThis->EvtLogBaseAddr.n.u4Len);
        uint32_t const cEvts    = iommuAmdGetEvtLogEntryCount(pThis);
        if (cEvts + 1 < cMaxEvts)
        {
            /* Write the event log entry to memory. */
            RTGCPHYS const GCPhysEvtLog      = pThis->EvtLogBaseAddr.n.u40Base << X86_PAGE_4K_SHIFT;
            RTGCPHYS const GCPhysEvtLogEntry = GCPhysEvtLog + offEvt;
            int rc = PDMDevHlpPCIPhysWrite(pDevIns, GCPhysEvtLogEntry, pEvent, cbEvt);
            if (RT_FAILURE(rc))
                LogFunc(("Failed to write event log entry at %#RGp. rc=%Rrc\n", GCPhysEvtLogEntry, rc));

            /* Increment the event log tail pointer. */
            uint32_t const cbEvtLog = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
            pThis->EvtLogTailPtr.n.off = (offEvt + cbEvt) % cbEvtLog;

            /* Indicate that an event log entry was written. */
            ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_EVT_LOG_INTR);

            /* Check and signal an interrupt if software wants to receive one when an event log entry is written. */
            if (pThis->Ctrl.n.u1EvtIntrEn)
                iommuAmdMsiInterruptRaise(pDevIns);
        }
        else
        {
            /* Indicate that the event log has overflowed. */
            ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_EVT_LOG_OVERFLOW);

            /* Check and signal an interrupt if software wants to receive one when the event log has overflowed. */
            if (pThis->Ctrl.n.u1EvtIntrEn)
                iommuAmdMsiInterruptRaise(pDevIns);
        }
    }

    IOMMU_UNLOCK(pDevIns, pThisCC);

    return VINF_SUCCESS;
}


/**
 * Sets an event in the hardware error registers.
 *
 * @param   pDevIns     The IOMMU device instance.
 * @param   pEvent      The event.
 *
 * @thread  Any.
 */
static void iommuAmdHwErrorSet(PPDMDEVINS pDevIns, PCEVT_GENERIC_T pEvent)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    if (pThis->ExtFeat.n.u1HwErrorSup)
    {
        if (pThis->HwEvtStatus.n.u1Valid)
            pThis->HwEvtStatus.n.u1Overflow = 1;
        pThis->HwEvtStatus.n.u1Valid = 1;
        pThis->HwEvtHi.u64 = RT_MAKE_U64(pEvent->au32[0], pEvent->au32[1]);
        pThis->HwEvtLo     = RT_MAKE_U64(pEvent->au32[2], pEvent->au32[3]);
        Assert(   pThis->HwEvtHi.n.u4EvtCode == IOMMU_EVT_DEV_TAB_HW_ERROR
               || pThis->HwEvtHi.n.u4EvtCode == IOMMU_EVT_PAGE_TAB_HW_ERROR
               || pThis->HwEvtHi.n.u4EvtCode == IOMMU_EVT_COMMAND_HW_ERROR);
    }
}


/**
 * Initializes a PAGE_TAB_HARDWARE_ERROR event.
 *
 * @param   idDevice            The device ID (bus, device, function).
 * @param   idDomain            The domain ID.
 * @param   GCPhysPtEntity      The system physical address of the page table
 *                              entity.
 * @param   enmOp               The IOMMU operation being performed.
 * @param   pEvtPageTabHwErr    Where to store the initialized event.
 */
static void iommuAmdPageTabHwErrorEventInit(uint16_t idDevice, uint16_t idDomain, RTGCPHYS GCPhysPtEntity, IOMMUOP enmOp,
                                            PEVT_PAGE_TAB_HW_ERR_T pEvtPageTabHwErr)
{
    memset(pEvtPageTabHwErr, 0, sizeof(*pEvtPageTabHwErr));
    pEvtPageTabHwErr->n.u16DevId           = idDevice;
    pEvtPageTabHwErr->n.u16DomainOrPasidLo = idDomain;
    pEvtPageTabHwErr->n.u1GuestOrNested    = 0;
    pEvtPageTabHwErr->n.u1Interrupt        = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
    pEvtPageTabHwErr->n.u1ReadWrite        = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
    pEvtPageTabHwErr->n.u1Translation      = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
    pEvtPageTabHwErr->n.u2Type             = enmOp == IOMMUOP_CMD ? HWEVTTYPE_DATA_ERROR : HWEVTTYPE_TARGET_ABORT;
    pEvtPageTabHwErr->n.u4EvtCode          = IOMMU_EVT_PAGE_TAB_HW_ERROR;
    pEvtPageTabHwErr->n.u64Addr            = GCPhysPtEntity;
}


/**
 * Raises a PAGE_TAB_HARDWARE_ERROR event.
 *
 * @param   pDevIns             The IOMMU device instance.
 * @param   enmOp               The IOMMU operation being performed.
 * @param   pEvtPageTabHwErr    The page table hardware error event.
 *
 * @thread  Any.
 */
static void iommuAmdPageTabHwErrorEventRaise(PPDMDEVINS pDevIns, IOMMUOP enmOp, PEVT_PAGE_TAB_HW_ERR_T pEvtPageTabHwErr)
{
    AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_PAGE_TAB_HW_ERR_T));
    PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtPageTabHwErr;

    PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
    IOMMU_LOCK(pDevIns, pThisCC);

    iommuAmdHwErrorSet(pDevIns, (PCEVT_GENERIC_T)pEvent);
    iommuAmdEvtLogEntryWrite(pDevIns, (PCEVT_GENERIC_T)pEvent);
    if (enmOp != IOMMUOP_CMD)
        iommuAmdSetPciTargetAbort(pDevIns);

    IOMMU_UNLOCK(pDevIns, pThisCC);

    LogFunc(("Raised PAGE_TAB_HARDWARE_ERROR. idDevice=%#x idDomain=%#x GCPhysPtEntity=%#RGp enmOp=%u u2Type=%u\n",
         pEvtPageTabHwErr->n.u16DevId, pEvtPageTabHwErr->n.u16DomainOrPasidLo, pEvtPageTabHwErr->n.u64Addr, enmOp,
         pEvtPageTabHwErr->n.u2Type));
}


#ifdef IN_RING3
/**
 * Initializes a COMMAND_HARDWARE_ERROR event.
 *
 * @param   GCPhysAddr      The system physical address the IOMMU attempted to access.
 * @param   pEvtCmdHwErr    Where to store the initialized event.
 */
static void iommuAmdCmdHwErrorEventInit(RTGCPHYS GCPhysAddr, PEVT_CMD_HW_ERR_T pEvtCmdHwErr)
{
    memset(pEvtCmdHwErr, 0, sizeof(*pEvtCmdHwErr));
    pEvtCmdHwErr->n.u2Type    = HWEVTTYPE_DATA_ERROR;
    pEvtCmdHwErr->n.u4EvtCode = IOMMU_EVT_COMMAND_HW_ERROR;
    pEvtCmdHwErr->n.u64Addr   = GCPhysAddr;
}


/**
 * Raises a COMMAND_HARDWARE_ERROR event.
 *
 * @param   pDevIns         The IOMMU device instance.
 * @param   pEvtCmdHwErr    The command hardware error event.
 *
 * @thread  Any.
 */
static void iommuAmdCmdHwErrorEventRaise(PPDMDEVINS pDevIns, PCEVT_CMD_HW_ERR_T pEvtCmdHwErr)
{
    AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_CMD_HW_ERR_T));
    PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtCmdHwErr;
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);

    PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
    IOMMU_LOCK(pDevIns, pThisCC);

    iommuAmdHwErrorSet(pDevIns, (PCEVT_GENERIC_T)pEvent);
    iommuAmdEvtLogEntryWrite(pDevIns, (PCEVT_GENERIC_T)pEvent);
    ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_CMD_BUF_RUNNING);

    IOMMU_UNLOCK(pDevIns, pThisCC);

    LogFunc(("Raised COMMAND_HARDWARE_ERROR. GCPhysCmd=%#RGp u2Type=%u\n", pEvtCmdHwErr->n.u64Addr, pEvtCmdHwErr->n.u2Type));
}
#endif /* IN_RING3 */


/**
 * Initializes a DEV_TAB_HARDWARE_ERROR event.
 *
 * @param   idDevice            The device ID (bus, device, function).
 * @param   GCPhysDte           The system physical address of the failed device table
 *                              access.
 * @param   enmOp               The IOMMU operation being performed.
 * @param   pEvtDevTabHwErr     Where to store the initialized event.
 */
static void iommuAmdDevTabHwErrorEventInit(uint16_t idDevice, RTGCPHYS GCPhysDte, IOMMUOP enmOp,
                                           PEVT_DEV_TAB_HW_ERROR_T pEvtDevTabHwErr)
{
    memset(pEvtDevTabHwErr, 0, sizeof(*pEvtDevTabHwErr));
    pEvtDevTabHwErr->n.u16DevId      = idDevice;
    pEvtDevTabHwErr->n.u1Intr        = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
    /** @todo IOMMU: Any other transaction type that can set read/write bit? */
    pEvtDevTabHwErr->n.u1ReadWrite   = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
    pEvtDevTabHwErr->n.u1Translation = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
    pEvtDevTabHwErr->n.u2Type        = enmOp == IOMMUOP_CMD ? HWEVTTYPE_DATA_ERROR : HWEVTTYPE_TARGET_ABORT;
    pEvtDevTabHwErr->n.u4EvtCode     = IOMMU_EVT_DEV_TAB_HW_ERROR;
    pEvtDevTabHwErr->n.u64Addr       = GCPhysDte;
}


/**
 * Raises a DEV_TAB_HARDWARE_ERROR event.
 *
 * @param   pDevIns             The IOMMU device instance.
 * @param   enmOp               The IOMMU operation being performed.
 * @param   pEvtDevTabHwErr     The device table hardware error event.
 *
 * @thread  Any.
 */
static void iommuAmdDevTabHwErrorEventRaise(PPDMDEVINS pDevIns, IOMMUOP enmOp, PEVT_DEV_TAB_HW_ERROR_T pEvtDevTabHwErr)
{
    AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_DEV_TAB_HW_ERROR_T));
    PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtDevTabHwErr;

    PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
    IOMMU_LOCK(pDevIns, pThisCC);

    iommuAmdHwErrorSet(pDevIns, (PCEVT_GENERIC_T)pEvent);
    iommuAmdEvtLogEntryWrite(pDevIns, (PCEVT_GENERIC_T)pEvent);
    if (enmOp != IOMMUOP_CMD)
        iommuAmdSetPciTargetAbort(pDevIns);

    IOMMU_UNLOCK(pDevIns, pThisCC);

    LogFunc(("Raised DEV_TAB_HARDWARE_ERROR. idDevice=%#x GCPhysDte=%#RGp enmOp=%u u2Type=%u\n", pEvtDevTabHwErr->n.u16DevId,
             pEvtDevTabHwErr->n.u64Addr, enmOp, pEvtDevTabHwErr->n.u2Type));
}


#ifdef IN_RING3
/**
 * Initializes an ILLEGAL_COMMAND_ERROR event.
 *
 * @param   GCPhysCmd       The system physical address of the failed command
 *                          access.
 * @param   pEvtIllegalCmd  Where to store the initialized event.
 */
static void iommuAmdIllegalCmdEventInit(RTGCPHYS GCPhysCmd, PEVT_ILLEGAL_CMD_ERR_T pEvtIllegalCmd)
{
    Assert(!(GCPhysCmd & UINT64_C(0xf)));
    memset(pEvtIllegalCmd, 0, sizeof(*pEvtIllegalCmd));
    pEvtIllegalCmd->n.u4EvtCode = IOMMU_EVT_ILLEGAL_CMD_ERROR;
    pEvtIllegalCmd->n.u64Addr   = GCPhysCmd;
}


/**
 * Raises an ILLEGAL_COMMAND_ERROR event.
 *
 * @param   pDevIns         The IOMMU device instance.
 * @param   pEvtIllegalCmd  The illegal command error event.
 */
static void iommuAmdIllegalCmdEventRaise(PPDMDEVINS pDevIns, PCEVT_ILLEGAL_CMD_ERR_T pEvtIllegalCmd)
{
    AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_ILLEGAL_DTE_T));
    PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtIllegalCmd;
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);

    iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
    ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_CMD_BUF_RUNNING);

    LogFunc(("Raised ILLEGAL_COMMAND_ERROR. Addr=%#RGp\n", pEvtIllegalCmd->n.u64Addr));
}
#endif  /* IN_RING3 */


/**
 * Initializes an ILLEGAL_DEV_TABLE_ENTRY event.
 *
 * @param   idDevice        The device ID (bus, device, function).
 * @param   uIova           The I/O virtual address.
 * @param   fRsvdNotZero    Whether reserved bits are not zero. Pass @c false if the
 *                          event was caused by an invalid level encoding in the
 *                          DTE.
 * @param   enmOp           The IOMMU operation being performed.
 * @param   pEvtIllegalDte  Where to store the initialized event.
 */
static void iommuAmdIllegalDteEventInit(uint16_t idDevice, uint64_t uIova, bool fRsvdNotZero, IOMMUOP enmOp,
                                        PEVT_ILLEGAL_DTE_T pEvtIllegalDte)
{
    memset(pEvtIllegalDte, 0, sizeof(*pEvtIllegalDte));
    pEvtIllegalDte->n.u16DevId      = idDevice;
    pEvtIllegalDte->n.u1Interrupt   = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
    pEvtIllegalDte->n.u1ReadWrite   = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
    pEvtIllegalDte->n.u1RsvdNotZero = fRsvdNotZero;
    pEvtIllegalDte->n.u1Translation = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
    pEvtIllegalDte->n.u4EvtCode     = IOMMU_EVT_ILLEGAL_DEV_TAB_ENTRY;
    pEvtIllegalDte->n.u64Addr       = uIova & ~UINT64_C(0x3);
    /** @todo r=ramshankar: Not sure why the last 2 bits are marked as reserved by the
     *        IOMMU spec here but not for this field for I/O page fault event. */
    Assert(!(uIova & UINT64_C(0x3)));
}


/**
 * Raises an ILLEGAL_DEV_TABLE_ENTRY event.
 *
 * @param   pDevIns         The IOMMU instance data.
 * @param   enmOp           The IOMMU operation being performed.
 * @param   pEvtIllegalDte  The illegal device table entry event.
 * @param   enmEvtType      The illegal device table entry event type.
 *
 * @thread  Any.
 */
static void iommuAmdIllegalDteEventRaise(PPDMDEVINS pDevIns, IOMMUOP enmOp, PCEVT_ILLEGAL_DTE_T pEvtIllegalDte,
                                         EVT_ILLEGAL_DTE_TYPE_T enmEvtType)
{
    AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_ILLEGAL_DTE_T));
    PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtIllegalDte;

    iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
    if (enmOp != IOMMUOP_CMD)
        iommuAmdSetPciTargetAbort(pDevIns);

    LogFunc(("Raised ILLEGAL_DTE_EVENT. idDevice=%#x uIova=%#RX64 enmOp=%u enmEvtType=%u\n", pEvtIllegalDte->n.u16DevId,
             pEvtIllegalDte->n.u64Addr, enmOp, enmEvtType));
    NOREF(enmEvtType);
}


/**
 * Initializes an IO_PAGE_FAULT event.
 *
 * @param   idDevice            The device ID (bus, device, function).
 * @param   idDomain            The domain ID.
 * @param   uIova               The I/O virtual address being accessed.
 * @param   fPresent            Transaction to a page marked as present (including
 *                              DTE.V=1) or interrupt marked as remapped
 *                              (IRTE.RemapEn=1).
 * @param   fRsvdNotZero        Whether reserved bits are not zero. Pass @c false if
 *                              the I/O page fault was caused by invalid level
 *                              encoding.
 * @param   fPermDenied         Permission denied for the address being accessed.
 * @param   enmOp               The IOMMU operation being performed.
 * @param   pEvtIoPageFault     Where to store the initialized event.
 */
static void iommuAmdIoPageFaultEventInit(uint16_t idDevice, uint16_t idDomain, uint64_t uIova, bool fPresent, bool fRsvdNotZero,
                                         bool fPermDenied, IOMMUOP enmOp, PEVT_IO_PAGE_FAULT_T pEvtIoPageFault)
{
    Assert(!fPermDenied || fPresent);
    memset(pEvtIoPageFault, 0, sizeof(*pEvtIoPageFault));
    pEvtIoPageFault->n.u16DevId            = idDevice;
    //pEvtIoPageFault->n.u4PasidHi         = 0;
    pEvtIoPageFault->n.u16DomainOrPasidLo  = idDomain;
    //pEvtIoPageFault->n.u1GuestOrNested   = 0;
    //pEvtIoPageFault->n.u1NoExecute       = 0;
    //pEvtIoPageFault->n.u1User            = 0;
    pEvtIoPageFault->n.u1Interrupt         = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
    pEvtIoPageFault->n.u1Present           = fPresent;
    pEvtIoPageFault->n.u1ReadWrite         = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
    pEvtIoPageFault->n.u1PermDenied        = fPermDenied;
    pEvtIoPageFault->n.u1RsvdNotZero       = fRsvdNotZero;
    pEvtIoPageFault->n.u1Translation       = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
    pEvtIoPageFault->n.u4EvtCode           = IOMMU_EVT_IO_PAGE_FAULT;
    pEvtIoPageFault->n.u64Addr             = uIova;
}


/**
 * Raises an IO_PAGE_FAULT event.
 *
 * @param   pDevIns             The IOMMU instance data.
 * @param   fIoDevFlags         The I/O device flags, see IOMMU_DTE_CACHE_F_XXX.
 * @param   pIrte               The interrupt remapping table entry, can be NULL.
 * @param   enmOp               The IOMMU operation being performed.
 * @param   pEvtIoPageFault     The I/O page fault event.
 * @param   enmEvtType          The I/O page fault event type.
 *
 * @thread  Any.
 */
static void iommuAmdIoPageFaultEventRaise(PPDMDEVINS pDevIns, uint16_t fIoDevFlags, PCIRTE_T pIrte, IOMMUOP enmOp,
                                          PCEVT_IO_PAGE_FAULT_T pEvtIoPageFault, EVT_IO_PAGE_FAULT_TYPE_T enmEvtType)
{
    AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_IO_PAGE_FAULT_T));
    PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtIoPageFault;
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    STAM_COUNTER_INC(&pThis->StatIopfs); NOREF(pThis);

#ifdef IOMMU_WITH_DTE_CACHE
# define IOMMU_DTE_CACHE_SET_PF_RAISED(a_pDevIns, a_DevId)  iommuAmdDteCacheUpdateFlags((a_pDevIns), (a_DevId), \
                                                                                        IOMMU_DTE_CACHE_F_IO_PAGE_FAULT_RAISED, \
                                                                                        0 /* fAndMask */)
#else
# define IOMMU_DTE_CACHE_SET_PF_RAISED(a_pDevIns, a_DevId)  do { } while (0)
#endif

    bool fSuppressEvtLogging = false;
    if (   enmOp == IOMMUOP_MEM_READ
        || enmOp == IOMMUOP_MEM_WRITE)
    {
        uint16_t const fSuppressIopf    = IOMMU_DTE_CACHE_F_VALID
                                        | IOMMU_DTE_CACHE_F_SUPPRESS_IOPF | IOMMU_DTE_CACHE_F_IO_PAGE_FAULT_RAISED;
        uint16_t const fSuppressAllIopf = IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_SUPPRESS_ALL_IOPF;
        if (   (fIoDevFlags & fSuppressAllIopf) == fSuppressAllIopf
            || (fIoDevFlags & fSuppressIopf) == fSuppressIopf)
        {
            fSuppressEvtLogging = true;
        }
    }
    else if (enmOp == IOMMUOP_INTR_REQ)
    {
        uint16_t const fSuppressIopf = IOMMU_DTE_CACHE_F_INTR_MAP_VALID | IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR;
        if ((fIoDevFlags & fSuppressIopf) == fSuppressIopf)
            fSuppressEvtLogging = true;
        else if (pIrte)     /** @todo Make this compulsary and assert if it isn't provided. */
            fSuppressEvtLogging = pIrte->n.u1SuppressIoPf;
    }
    /* else: Events are never suppressed for commands. */

    switch (enmEvtType)
    {
        case kIoPageFaultType_PermDenied:
        {
            /* Cannot be triggered by a command. */
            Assert(enmOp != IOMMUOP_CMD);
            RT_FALL_THRU();
        }
        case kIoPageFaultType_DteRsvdPagingMode:
        case kIoPageFaultType_PteInvalidPageSize:
        case kIoPageFaultType_PteInvalidLvlEncoding:
        case kIoPageFaultType_SkippedLevelIovaNotZero:
        case kIoPageFaultType_PteRsvdNotZero:
        case kIoPageFaultType_PteValidNotSet:
        case kIoPageFaultType_DteTranslationDisabled:
        case kIoPageFaultType_PasidInvalidRange:
        {
            /*
             * For a translation request, the IOMMU doesn't signal an I/O page fault nor does it
             * create an event log entry. See AMD IOMMU spec. 2.1.3.2 "I/O Page Faults".
             */
            if (enmOp != IOMMUOP_TRANSLATE_REQ)
            {
                if (!fSuppressEvtLogging)
                {
                    iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
                    IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
                }
                if (enmOp != IOMMUOP_CMD)
                    iommuAmdSetPciTargetAbort(pDevIns);
            }
            break;
        }

        case kIoPageFaultType_UserSupervisor:
        {
            /* Access is blocked and only creates an event log entry. */
            if (!fSuppressEvtLogging)
            {
                iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
                IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
            }
            break;
        }

        case kIoPageFaultType_IrteAddrInvalid:
        case kIoPageFaultType_IrteRsvdNotZero:
        case kIoPageFaultType_IrteRemapEn:
        case kIoPageFaultType_IrteRsvdIntType:
        case kIoPageFaultType_IntrReqAborted:
        case kIoPageFaultType_IntrWithPasid:
        {
            /* Only trigerred by interrupt requests. */
            Assert(enmOp == IOMMUOP_INTR_REQ);
            if (!fSuppressEvtLogging)
            {
                iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
                IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
            }
            iommuAmdSetPciTargetAbort(pDevIns);
            break;
        }

        case kIoPageFaultType_SmiFilterMismatch:
        {
            /* Not supported and probably will never be, assert. */
            AssertMsgFailed(("kIoPageFaultType_SmiFilterMismatch - Upstream SMI requests not supported/implemented."));
            break;
        }

        case kIoPageFaultType_DevId_Invalid:
        {
            /* Cannot be triggered by a command. */
            Assert(enmOp != IOMMUOP_CMD);
            Assert(enmOp != IOMMUOP_TRANSLATE_REQ); /** @todo IOMMU: We don't support translation requests yet. */
            if (!fSuppressEvtLogging)
            {
                iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
                IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
            }
            if (   enmOp == IOMMUOP_MEM_READ
                || enmOp == IOMMUOP_MEM_WRITE)
                iommuAmdSetPciTargetAbort(pDevIns);
            break;
        }
    }

#undef IOMMU_DTE_CACHE_SET_PF_RAISED
}


/**
 * Raises an IO_PAGE_FAULT event given the DTE.
 *
 * @param   pDevIns             The IOMMU instance data.
 * @param   pDte                The device table entry.
 * @param   pIrte               The interrupt remapping table entry, can be NULL.
 * @param   enmOp               The IOMMU operation being performed.
 * @param   pEvtIoPageFault     The I/O page fault event.
 * @param   enmEvtType          The I/O page fault event type.
 *
 * @thread  Any.
 */
static void iommuAmdIoPageFaultEventRaiseWithDte(PPDMDEVINS pDevIns, PCDTE_T pDte, PCIRTE_T pIrte, IOMMUOP enmOp,
                                                 PCEVT_IO_PAGE_FAULT_T pEvtIoPageFault, EVT_IO_PAGE_FAULT_TYPE_T enmEvtType)
{
    Assert(pDte);
    uint16_t const fIoDevFlags = iommuAmdGetBasicDevFlags(pDte);
    return iommuAmdIoPageFaultEventRaise(pDevIns, fIoDevFlags, pIrte, enmOp, pEvtIoPageFault, enmEvtType);
}


/**
 * Reads a device table entry for the given the device ID.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   enmOp       The IOMMU operation being performed.
 * @param   pDte        Where to store the device table entry.
 *
 * @thread  Any.
 */
static int iommuAmdDteRead(PPDMDEVINS pDevIns, uint16_t idDevice, IOMMUOP enmOp, PDTE_T pDte)
{
    PCIOMMU  pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);

    IOMMU_LOCK(pDevIns, pThisCC);

    /* Figure out which device table segment is being accessed. */
    uint8_t const idxSegsEn = pThis->Ctrl.n.u3DevTabSegEn;
    Assert(idxSegsEn < RT_ELEMENTS(g_auDevTabSegShifts));

    uint8_t const idxSeg = (idDevice & g_auDevTabSegMasks[idxSegsEn]) >> g_auDevTabSegShifts[idxSegsEn];
    Assert(idxSeg < RT_ELEMENTS(pThis->aDevTabBaseAddrs));
    AssertCompile(RT_ELEMENTS(g_auDevTabSegShifts) == RT_ELEMENTS(g_auDevTabSegMasks));

    RTGCPHYS const GCPhysDevTab = pThis->aDevTabBaseAddrs[idxSeg].n.u40Base << X86_PAGE_4K_SHIFT;
    uint32_t const offDte       = (idDevice & ~g_auDevTabSegMasks[idxSegsEn]) * sizeof(DTE_T);
    RTGCPHYS const GCPhysDte    = GCPhysDevTab + offDte;

    /* Ensure the DTE falls completely within the device table segment. */
    uint32_t const cbDevTabSeg  = (pThis->aDevTabBaseAddrs[idxSeg].n.u9Size + 1) << X86_PAGE_4K_SHIFT;

    IOMMU_UNLOCK(pDevIns, pThisCC);

    if (offDte + sizeof(DTE_T) <= cbDevTabSeg)
    {
        /* Read the device table entry from guest memory. */
        Assert(!(GCPhysDevTab & X86_PAGE_4K_OFFSET_MASK));
        int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysDte, pDte, sizeof(*pDte));
        if (RT_SUCCESS(rc))
            return VINF_SUCCESS;

        /* Raise a device table hardware error. */
        LogFunc(("Failed to read device table entry at %#RGp. rc=%Rrc -> DevTabHwError\n", GCPhysDte, rc));

        EVT_DEV_TAB_HW_ERROR_T EvtDevTabHwErr;
        iommuAmdDevTabHwErrorEventInit(idDevice, GCPhysDte, enmOp, &EvtDevTabHwErr);
        iommuAmdDevTabHwErrorEventRaise(pDevIns, enmOp, &EvtDevTabHwErr);
        return VERR_IOMMU_DTE_READ_FAILED;
    }

    /* Raise an I/O page fault for out-of-bounds acccess. */
    LogFunc(("Out-of-bounds device table entry. idDevice=%#x offDte=%u cbDevTabSeg=%u -> IOPF\n", idDevice, offDte, cbDevTabSeg));
    EVT_IO_PAGE_FAULT_T EvtIoPageFault;
    iommuAmdIoPageFaultEventInit(idDevice, 0 /* idDomain */, 0 /* uIova */, false /* fPresent */, false /* fRsvdNotZero */,
                                 false /* fPermDenied */, enmOp, &EvtIoPageFault);
    iommuAmdIoPageFaultEventRaise(pDevIns, 0 /* fIoDevFlags */, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                  kIoPageFaultType_DevId_Invalid);
    return VERR_IOMMU_DTE_BAD_OFFSET;
}


/**
 * Performs pre-translation checks for the given device table entry.
 *
 * @returns VBox status code.
 * @retval VINF_SUCCESS if the DTE is valid and supports address translation.
 * @retval VINF_IOMMU_ADDR_TRANSLATION_DISABLED if the DTE is valid but address
 *         translation is disabled.
 * @retval VERR_IOMMU_ADDR_TRANSLATION_FAILED if an error occurred and any
 *         corresponding event was raised.
 * @retval VERR_IOMMU_ADDR_ACCESS_DENIED if the DTE denies the requested
 *         permissions.
 *
 * @param   pDevIns         The IOMMU device instance.
 * @param   uIova           The I/O virtual address to translate.
 * @param   idDevice        The device ID (bus, device, function).
 * @param   fPerm           The I/O permissions for this access, see
 *                          IOMMU_IO_PERM_XXX.
 * @param   pDte            The device table entry.
 * @param   enmOp           The IOMMU operation being performed.
 *
 * @thread  Any.
 */
static int iommuAmdPreTranslateChecks(PPDMDEVINS pDevIns, uint16_t idDevice, uint64_t uIova, uint8_t fPerm, PCDTE_T pDte,
                                      IOMMUOP enmOp)
{
    /*
     * Check if the translation is valid, otherwise raise an I/O page fault.
     */
    if (pDte->n.u1TranslationValid)
    { /* likely */ }
    else
    {
        /** @todo r=ramshankar: The AMD IOMMU spec. says page walk is terminated but
         *        doesn't explicitly say whether an I/O page fault is raised. From other
         *        places in the spec. it seems early page walk terminations (starting with
         *        the DTE) return the state computed so far and raises an I/O page fault. So
         *        returning an invalid translation rather than skipping translation. */
        LogFunc(("Translation valid bit not set -> IOPF\n"));
        EVT_IO_PAGE_FAULT_T EvtIoPageFault;
        iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, false /* fPresent */, false /* fRsvdNotZero */,
                                     false /* fPermDenied */, enmOp, &EvtIoPageFault);
        iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                             kIoPageFaultType_DteTranslationDisabled);
        return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
    }

    /*
     * Check permissions bits in the DTE.
     * Note: This MUST be checked prior to checking the root page table level below!
     */
    uint8_t const fDtePerm = (pDte->au64[0] >> IOMMU_IO_PERM_SHIFT) & IOMMU_IO_PERM_MASK;
    if ((fPerm & fDtePerm) == fPerm)
    { /* likely */ }
    else
    {
        LogFunc(("Permission denied by DTE (fPerm=%#x fDtePerm=%#x) -> IOPF\n", fPerm, fDtePerm));
        EVT_IO_PAGE_FAULT_T EvtIoPageFault;
        iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
                                     true /* fPermDenied */, enmOp, &EvtIoPageFault);
        iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                             kIoPageFaultType_PermDenied);
        return VERR_IOMMU_ADDR_ACCESS_DENIED;
    }

    /*
     * If the root page table level is 0, translation is disabled and GPA=SPA and
     * the DTE.IR and DTE.IW bits control permissions (verified above).
     */
    uint8_t const uMaxLevel = pDte->n.u3Mode;
    if (uMaxLevel != 0)
    { /* likely */ }
    else
    {
        Assert((fPerm & fDtePerm) == fPerm);   /* Verify we've checked permissions. */
        return VINF_IOMMU_ADDR_TRANSLATION_DISABLED;
    }

    /*
     * If the root page table level exceeds the allowed host-address translation level,
     * page walk is terminated and translation fails.
     */
    if (uMaxLevel <= IOMMU_MAX_HOST_PT_LEVEL)
    { /* likely */ }
    else
    {
        /** @todo r=ramshankar: I cannot make out from the AMD IOMMU spec. if I should be
         *        raising an ILLEGAL_DEV_TABLE_ENTRY event or an IO_PAGE_FAULT event here.
         *        I'm just going with I/O page fault. */
        LogFunc(("Invalid root page table level %#x (idDevice=%#x) -> IOPF\n", uMaxLevel, idDevice));
        EVT_IO_PAGE_FAULT_T EvtIoPageFault;
        iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
                                     false /* fPermDenied */, enmOp, &EvtIoPageFault);
        iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                             kIoPageFaultType_PteInvalidLvlEncoding);
        return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
    }

    /* The DTE allows translations for this device. */
    return VINF_SUCCESS;
}


/**
 * Walks the I/O page table to translate the I/O virtual address to a system
 * physical address.
 *
 * @returns VBox status code.
 * @param   pDevIns         The IOMMU device instance.
 * @param   uIova           The I/O virtual address to translate. Must be 4K aligned.
 * @param   fPerm           The I/O permissions for this access, see
 *                          IOMMU_IO_PERM_XXX.
 * @param   idDevice        The device ID (bus, device, function).
 * @param   pDte            The device table entry.
 * @param   enmOp           The IOMMU operation being performed.
 * @param   pPageLookup     Where to store the results of the I/O page lookup. This
 *                          is only updated when VINF_SUCCESS is returned.
 *
 * @thread  Any.
 */
static int iommuAmdIoPageTableWalk(PPDMDEVINS pDevIns, uint64_t uIova, uint8_t fPerm, uint16_t idDevice, PCDTE_T pDte,
                                   IOMMUOP enmOp, PIOPAGELOOKUP pPageLookup)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    Assert(pDte->n.u1Valid);
    Assert(!(uIova & X86_PAGE_4K_OFFSET_MASK));

    /* The virtual address bits indexing table. */
    static uint8_t const  s_acIovaLevelShifts[] = { 0, 12, 21, 30, 39, 48, 57, 0 };
    AssertCompile(RT_ELEMENTS(s_acIovaLevelShifts) > IOMMU_MAX_HOST_PT_LEVEL);

    /*
     * Traverse the I/O page table starting with the page directory in the DTE.
     *
     * The Valid (Present bit), Translation Valid and Mode (Next-Level bits) in
     * the DTE have been validated already, see iommuAmdPreTranslateChecks.
     */
    IOPTENTITY_T PtEntity;
    PtEntity.u64 = pDte->au64[0];
    for (;;)
    {
        uint8_t const uLevel = PtEntity.n.u3NextLevel;

        /* Read the page table entity at the current level. */
        {
            Assert(uLevel > 0 && uLevel < RT_ELEMENTS(s_acIovaLevelShifts));
            Assert(uLevel <= IOMMU_MAX_HOST_PT_LEVEL);
            uint16_t const idxPte         = (uIova >> s_acIovaLevelShifts[uLevel]) & UINT64_C(0x1ff);
            uint64_t const offPte         = idxPte << 3;
            RTGCPHYS const GCPhysPtEntity = (PtEntity.u64 & IOMMU_PTENTITY_ADDR_MASK) + offPte;
            int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysPtEntity, &PtEntity.u64, sizeof(PtEntity));
            if (RT_FAILURE(rc))
            {
                LogFunc(("Failed to read page table entry at %#RGp. rc=%Rrc -> PageTabHwError\n", GCPhysPtEntity, rc));
                EVT_PAGE_TAB_HW_ERR_T EvtPageTabHwErr;
                iommuAmdPageTabHwErrorEventInit(idDevice, pDte->n.u16DomainId, GCPhysPtEntity, enmOp, &EvtPageTabHwErr);
                iommuAmdPageTabHwErrorEventRaise(pDevIns, enmOp, &EvtPageTabHwErr);
                return VERR_IOMMU_IPE_2;
            }
        }

        /* Check present bit. */
        if (PtEntity.n.u1Present)
        { /* likely */ }
        else
        {
            LogFunc(("Page table entry not present. idDevice=%#x uIova=%#RX64 -> IOPF\n", idDevice, uIova));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, false /* fPresent */, false /* fRsvdNotZero */,
                                         false /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                                 kIoPageFaultType_PermDenied);
            return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
        }

        /* Validate the encoding of the next level. */
        uint8_t const uNextLevel = PtEntity.n.u3NextLevel;
#if IOMMU_MAX_HOST_PT_LEVEL < 6
        if (uNextLevel <= IOMMU_MAX_HOST_PT_LEVEL)
        { /* likely */ }
        else
        {
            LogFunc(("Next-level/paging-mode field of the paging entity invalid. uNextLevel=%#x -> IOPF\n", uNextLevel));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, true /* fRsvdNotZero */,
                                         false /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                                 kIoPageFaultType_PteInvalidLvlEncoding);
            return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
        }
#endif

        /* Check reserved bits. */
        uint64_t const fRsvdMask = uNextLevel == 0 || uNextLevel == 7 ? IOMMU_PTE_RSVD_MASK : IOMMU_PDE_RSVD_MASK;
        if (!(PtEntity.u64 & fRsvdMask))
        { /* likely */ }
        else
        {
            LogFunc(("Page table entity (%#RX64 level=%u) reserved bits set -> IOPF\n", PtEntity.u64, uNextLevel));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, true /* fRsvdNotZero */,
                                         false /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                                 kIoPageFaultType_PteRsvdNotZero);
            return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
        }

        /* Check permission bits. */
        uint8_t const fPtePerm = (PtEntity.u64 >> IOMMU_IO_PERM_SHIFT) & IOMMU_IO_PERM_MASK;
        if ((fPerm & fPtePerm) == fPerm)
        { /* likely */ }
        else
        {
            LogFunc(("Page table entry access denied. idDevice=%#x fPerm=%#x fPtePerm=%#x -> IOPF\n", idDevice, fPerm, fPtePerm));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
                                         true /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                                 kIoPageFaultType_PermDenied);
            return VERR_IOMMU_ADDR_ACCESS_DENIED;
        }

        /* If the next level is 0 or 7, this is the final level PTE. */
        if (uNextLevel == 0)
        {
            /* The page size of the translation is the default size for the level. */
            uint8_t const  cShift    = s_acIovaLevelShifts[uLevel];
            RTGCPHYS const GCPhysPte = PtEntity.u64 & IOMMU_PTENTITY_ADDR_MASK;
            pPageLookup->GCPhysSpa = GCPhysPte & X86_GET_PAGE_BASE_MASK(cShift);
            pPageLookup->cShift    = cShift;
            pPageLookup->fPerm     = fPtePerm;
            return VINF_SUCCESS;
        }
        if (uNextLevel == 7)
        {
            /* The default page size of the translation is overridden. */
            uint8_t        cShift    = X86_PAGE_4K_SHIFT;
            RTGCPHYS const GCPhysPte = PtEntity.u64 & IOMMU_PTENTITY_ADDR_MASK;
            while (GCPhysPte & RT_BIT_64(cShift++))
                ;

            /* The page size must be larger than the default size and lower than the default size of the higher level. */
            if (   cShift > s_acIovaLevelShifts[uLevel]
                && cShift < s_acIovaLevelShifts[uLevel + 1])
            {
                pPageLookup->GCPhysSpa = GCPhysPte & X86_GET_PAGE_BASE_MASK(cShift);
                pPageLookup->cShift    = cShift;
                pPageLookup->fPerm     = fPtePerm;
                STAM_COUNTER_INC(&pThis->StatNonStdPageSize); NOREF(pThis);
                return VINF_SUCCESS;
            }

            LogFunc(("Page size invalid. idDevice=%#x cShift=%u -> IOPF\n", idDevice, cShift));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
                                         false /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                                 kIoPageFaultType_PteInvalidPageSize);
            return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
        }

        /* Validate level transition. */
        if (uNextLevel < uLevel)
        { /* likely */ }
        else
        {
            LogFunc(("Next level (%#x) must be less than the current level (%#x) -> IOPF\n", uNextLevel, uLevel));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
                                         false /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                                 kIoPageFaultType_PteInvalidLvlEncoding);
            return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
        }

        /* Ensure IOVA bits of skipped levels (if any) are zero. */
        uint64_t const fIovaSkipMask = IOMMU_GET_MAX_VALID_IOVA(uLevel - 1) - IOMMU_GET_MAX_VALID_IOVA(uNextLevel);
        if (!(uIova & fIovaSkipMask))
        { /* likely */ }
        else
        {
            LogFunc(("IOVA of skipped levels are not zero. uIova=%#RX64 fSkipMask=%#RX64 -> IOPF\n", uIova, fIovaSkipMask));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
                                         false /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                                 kIoPageFaultType_SkippedLevelIovaNotZero);
            return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
        }

        /* Traverse to the next level. */
    }
}


/**
 * Page lookup callback for finding an I/O page from guest memory.
 *
 * @returns VBox status code.
 * @retval  VINF_SUCCESS when the page is found and has the right permissions.
 * @retval  VERR_IOMMU_ADDR_TRANSLATION_FAILED when address translation fails.
 * @retval  VERR_IOMMU_ADDR_ACCESS_DENIED when the page is found but permissions are
 *          insufficient to what is requested.
 *
 * @param   pDevIns         The IOMMU instance data.
 * @param   uIovaPage       The I/O virtual address to lookup in the cache (must be
 *                          4K aligned).
 * @param   fPerm           The I/O permissions for this access, see
 *                          IOMMU_IO_PERM_XXX.
 * @param   pAux            The auxiliary information required during lookup.
 * @param   pPageLookup     Where to store the looked up I/O page.
 */
static DECLCALLBACK(int) iommuAmdDteLookupPage(PPDMDEVINS pDevIns, uint64_t uIovaPage, uint8_t fPerm, PCIOMMUOPAUX pAux,
                                               PIOPAGELOOKUP pPageLookup)
{
    AssertPtr(pAux);
    AssertPtr(pPageLookup);
    Assert(!(uIovaPage & X86_PAGE_4K_OFFSET_MASK));

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    STAM_PROFILE_ADV_START(&pThis->StatProfDteLookup, a);
    int rc = iommuAmdIoPageTableWalk(pDevIns, uIovaPage, fPerm, pAux->idDevice, pAux->pDte, pAux->enmOp, pPageLookup);
    STAM_PROFILE_ADV_STOP(&pThis->StatProfDteLookup, a); NOREF(pThis);
    return rc;
}


/**
 * Looks up a range of I/O virtual addresses.
 *
 * @returns VBox status code.
 * @param   pDevIns             The IOMMU instance data.
 * @param   pfnIoPageLookup     The lookup function to use.
 * @param   pAddrIn             The I/O address range to lookup.
 * @param   pAux                The auxiliary information required by the lookup
 *                              function.
 * @param   pAddrOut            Where to store the translated I/O address page
 *                              lookup.
 * @param   pcbContiguous       Where to store the size of the access.
 */
static int iommuAmdLookupIoAddrRange(PPDMDEVINS pDevIns, PFNIOPAGELOOKUP pfnIoPageLookup, PCIOADDRRANGE pAddrIn,
                                     PCIOMMUOPAUX pAux, PIOPAGELOOKUP pAddrOut, size_t *pcbContiguous)
{
    int            rc;
    size_t const   cbIova      = pAddrIn->cb;
    uint8_t const  fPerm       = pAddrIn->fPerm;
    uint64_t const uIova       = pAddrIn->uAddr;
    RTGCPHYS       GCPhysSpa   = NIL_RTGCPHYS;
    size_t         cbRemaining = cbIova;
    uint64_t       uIovaPage   = pAddrIn->uAddr & X86_PAGE_4K_BASE_MASK;
    uint64_t       offIova     = pAddrIn->uAddr & X86_PAGE_4K_OFFSET_MASK;
    size_t const   cbPage      = X86_PAGE_4K_SIZE;

    IOPAGELOOKUP PageLookupPrev;
    RT_ZERO(PageLookupPrev);
    for (;;)
    {
        /* Lookup the physical page corresponding to the I/O virtual address. */
        IOPAGELOOKUP PageLookup;
        rc = pfnIoPageLookup(pDevIns, uIovaPage, fPerm, pAux, &PageLookup);
        if (RT_SUCCESS(rc))
        {
            /*
             * Validate results of the translation.
             */
            /* The IOTLB cache preserves the original page sizes even though the IOVAs are split into 4K pages. */
            Assert(PageLookup.cShift >= X86_PAGE_4K_SHIFT && PageLookup.cShift <= 51);
            Assert(   pfnIoPageLookup != iommuAmdDteLookupPage
                   || !(PageLookup.GCPhysSpa & X86_GET_PAGE_OFFSET_MASK(PageLookup.cShift)));
            Assert((PageLookup.fPerm & fPerm) == fPerm);

            /* Store the translated address before continuing to access more pages. */
            if (cbRemaining == cbIova)
            {
                uint64_t const offSpa = uIova & X86_GET_PAGE_OFFSET_MASK(PageLookup.cShift);
                GCPhysSpa = PageLookup.GCPhysSpa | offSpa;
            }
            /*
             * Check if translated address results in a physically contiguous region.
             *
             * Also ensure that the permissions for all pages in this range are identical
             * because we specify a common permission while adding pages in this range
             * to the IOTLB cache.
             *
             * The page size must also be identical since we need to know how many offset
             * bits to copy into the final translated address (while retrieving 4K sized
             * pages from the IOTLB cache).
             */
            else if (   PageLookup.GCPhysSpa == PageLookupPrev.GCPhysSpa + cbPage
                     && PageLookup.fPerm     == PageLookupPrev.fPerm
                     && PageLookup.cShift    == PageLookupPrev.cShift)
            { /* likely */ }
            else
            {
                Assert(cbRemaining > 0);
                rc = VERR_OUT_OF_RANGE;
                break;
            }

            /* Store the page lookup result from the first/previous page. */
            PageLookupPrev = PageLookup;

            /* Check if we need to access more pages. */
            if (cbRemaining > cbPage - offIova)
            {
                cbRemaining -= (cbPage - offIova); /* Calculate how much more we need to access. */
                uIovaPage   += cbPage;             /* Update address of the next access. */
                offIova      = 0;                  /* After the first page, remaining pages are accessed from offset 0. */
            }
            else
            {
                /* Caller (PDM) doesn't expect more data accessed than what was requested. */
                cbRemaining = 0;
                break;
            }
        }
        else
            break;
    }

    pAddrOut->GCPhysSpa = GCPhysSpa;                  /* Update the translated address. */
    pAddrOut->cShift    = PageLookupPrev.cShift;      /* Update the page size of the lookup. */
    pAddrOut->fPerm     = PageLookupPrev.fPerm;       /* Update the allowed permissions for this access. */
    *pcbContiguous      = cbIova - cbRemaining;       /* Update the size of the contiguous memory region. */
    return rc;
}


/**
 * Looks up an I/O virtual address from the device table.
 *
 * @returns VBox status code.
 * @param   pDevIns         The IOMMU instance data.
 * @param   idDevice        The device ID (bus, device, function).
 * @param   uIova           The I/O virtual address to lookup.
 * @param   cbIova          The size of the access.
 * @param   fPerm           The I/O permissions for this access, see
 *                          IOMMU_IO_PERM_XXX.
 * @param   enmOp           The IOMMU operation being performed.
 * @param   pGCPhysSpa      Where to store the translated system physical address.
 * @param   pcbContiguous   Where to store the number of contiguous bytes translated
 *                          and permission-checked.
 *
 * @thread  Any.
 */
static int iommuAmdDteLookup(PPDMDEVINS pDevIns, uint16_t idDevice, uint64_t uIova, size_t cbIova, uint8_t fPerm, IOMMUOP enmOp,
                             PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    RTGCPHYS GCPhysSpa    = NIL_RTGCPHYS;
    size_t   cbContiguous = 0;

    /* Read the device table entry from memory. */
    DTE_T Dte;
    int rc = iommuAmdDteRead(pDevIns, idDevice, enmOp, &Dte);
    if (RT_SUCCESS(rc))
    {
        if (Dte.n.u1Valid)
        {
            /* Validate bits 127:0 of the device table entry when DTE.V is 1. */
            uint64_t const fRsvd0 = Dte.au64[0] & ~(IOMMU_DTE_QWORD_0_VALID_MASK & ~IOMMU_DTE_QWORD_0_FEAT_MASK);
            uint64_t const fRsvd1 = Dte.au64[1] & ~(IOMMU_DTE_QWORD_1_VALID_MASK & ~IOMMU_DTE_QWORD_1_FEAT_MASK);
            if (RT_LIKELY(!fRsvd0 && !fRsvd1))
            {
                /*
                 * Check if the DTE is configured for translating addresses.
                 * Note: Addresses cannot be subject to exclusion as we do -not- support remote IOTLBs,
                 *       so there's no need to check the address exclusion base/limit here.
                 */
                rc = iommuAmdPreTranslateChecks(pDevIns, idDevice, uIova, fPerm, &Dte, enmOp);
                if (rc == VINF_SUCCESS)
                {
                    IOADDRRANGE AddrIn;
                    AddrIn.uAddr = uIova;
                    AddrIn.cb    = cbIova;
                    AddrIn.fPerm = fPerm;

                    IOMMUOPAUX Aux;
                    Aux.enmOp    = enmOp;
                    Aux.pDte     = &Dte;
                    Aux.idDevice = idDevice;
                    Aux.idDomain = Dte.n.u16DomainId;

                    /* Lookup the address from the DTE and I/O page tables.*/
                    IOPAGELOOKUP AddrOut;
                    rc = iommuAmdLookupIoAddrRange(pDevIns, iommuAmdDteLookupPage, &AddrIn, &Aux, &AddrOut, &cbContiguous);
                    GCPhysSpa = AddrOut.GCPhysSpa;

                    /*
                     * If we stopped since translation resulted in non-contiguous physical addresses
                     * or permissions aren't identical for all pages in the access, what we translated
                     * thus far is still valid.
                     */
                    if (rc == VERR_OUT_OF_RANGE)
                    {
                        Assert(cbContiguous > 0 && cbContiguous < cbIova);
                        rc = VINF_SUCCESS;
                        STAM_COUNTER_INC(&pThis->StatAccessDteNonContig); NOREF(pThis);
                    }
                    else if (rc == VERR_IOMMU_ADDR_ACCESS_DENIED)
                        STAM_COUNTER_INC(&pThis->StatAccessDtePermDenied);

#ifdef IOMMU_WITH_IOTLBE_CACHE
                    if (RT_SUCCESS(rc))
                    {
                        /* Update that addresses requires translation (cumulative permissions of DTE and I/O page tables). */
                        iommuAmdDteCacheAddOrUpdateFlags(pDevIns, &Dte, idDevice, IOMMU_DTE_CACHE_F_ADDR_TRANSLATE,
                                                         0 /* fAndMask */);
                        /* Update IOTLB for the contiguous range of I/O virtual addresses. */
                        iommuAmdIotlbAddRange(pDevIns, Aux.idDomain, uIova & X86_PAGE_4K_BASE_MASK, cbContiguous, &AddrOut);
                    }
#endif
                }
                else if (rc == VINF_IOMMU_ADDR_TRANSLATION_DISABLED)
                {
                    /*
                     * Translation is disabled for this device (root paging mode is 0).
                     * GPA=SPA, but the permission bits are important and controls accesses.
                     */
                    GCPhysSpa    = uIova;
                    cbContiguous = cbIova;
                    rc = VINF_SUCCESS;

#ifdef IOMMU_WITH_IOTLBE_CACHE
                    /* Update that addresses permissions of DTE apply (but omit address translation). */
                    iommuAmdDteCacheAddOrUpdateFlags(pDevIns, &Dte, idDevice, IOMMU_DTE_CACHE_F_IO_PERM,
                                                     IOMMU_DTE_CACHE_F_ADDR_TRANSLATE);
#endif
                }
                else
                {
                    /* Address translation failed or access is denied. */
                    Assert(rc == VERR_IOMMU_ADDR_ACCESS_DENIED || rc == VERR_IOMMU_ADDR_TRANSLATION_FAILED);
                    GCPhysSpa    = NIL_RTGCPHYS;
                    cbContiguous = 0;
                    STAM_COUNTER_INC(&pThis->StatAccessDtePermDenied);
                }
            }
            else
            {
                /* Invalid reserved  bits in the DTE, raise an error event. */
                LogFunc(("Invalid DTE reserved bits (u64[0]=%#RX64 u64[1]=%#RX64) -> Illegal DTE\n", fRsvd0, fRsvd1));
                EVT_ILLEGAL_DTE_T Event;
                iommuAmdIllegalDteEventInit(idDevice, uIova, true /* fRsvdNotZero */, enmOp, &Event);
                iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdNotZero);
                rc = VERR_IOMMU_ADDR_TRANSLATION_FAILED;
            }
        }
        else
        {
            /*
             * The DTE is not valid, forward addresses untranslated.
             * See AMD IOMMU spec. "Table 5: Feature Enablement for Address Translation".
             */
            GCPhysSpa    = uIova;
            cbContiguous = cbIova;
        }
    }
    else
    {
        LogFunc(("Failed to read device table entry. idDevice=%#x rc=%Rrc\n", idDevice, rc));
        rc = VERR_IOMMU_ADDR_TRANSLATION_FAILED;
    }

    *pGCPhysSpa    = GCPhysSpa;
    *pcbContiguous = cbContiguous;
    AssertMsg(rc != VINF_SUCCESS || cbContiguous > 0, ("cbContiguous=%zu\n", cbContiguous));
    return rc;
}


#ifdef IOMMU_WITH_IOTLBE_CACHE
/**
 * I/O page lookup callback for finding an I/O page from the IOTLB.
 *
 * @returns VBox status code.
 * @retval  VINF_SUCCESS when the page is found and has the right permissions.
 * @retval  VERR_NOT_FOUND when the page is not found.
 * @retval  VERR_IOMMU_ADDR_ACCESS_DENIED when the page is found but permissions are
 *          insufficient to what is requested.
 *
 * @param   pDevIns         The IOMMU instance data.
 * @param   uIovaPage       The I/O virtual address to lookup in the cache (must be
 *                          4K aligned).
 * @param   fPerm           The I/O permissions for this access, see
 *                          IOMMU_IO_PERM_XXX.
 * @param   pAux            The auxiliary information required during lookup.
 * @param   pPageLookup     Where to store the looked up I/O page.
 */
static DECLCALLBACK(int) iommuAmdCacheLookupPage(PPDMDEVINS pDevIns, uint64_t uIovaPage, uint8_t fPerm, PCIOMMUOPAUX pAux,
                                                 PIOPAGELOOKUP pPageLookup)
{
    Assert(pAux);
    Assert(pPageLookup);
    Assert(!(uIovaPage & X86_PAGE_4K_OFFSET_MASK));

    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);

    STAM_PROFILE_ADV_START(&pThis->StatProfIotlbeLookup, a);
    PCIOTLBE pIotlbe = iommuAmdIotlbLookup(pThis, pThisR3, pAux->idDomain, uIovaPage);
    STAM_PROFILE_ADV_STOP(&pThis->StatProfIotlbeLookup, a);
    if (pIotlbe)
    {
        *pPageLookup = pIotlbe->PageLookup;
        if ((pPageLookup->fPerm & fPerm) == fPerm)
        {
            STAM_COUNTER_INC(&pThis->StatAccessCacheHit);
            return VINF_SUCCESS;
        }
        return VERR_IOMMU_ADDR_ACCESS_DENIED;
    }
    return VERR_NOT_FOUND;
}


/**
 * Lookups a memory access from the IOTLB cache.
 *
 * @returns VBox status code.
 * @retval  VINF_SUCCESS if the access was cached and permissions are verified.
 * @retval  VERR_OUT_OF_RANGE if the access resulted in a non-contiguous physical
 *          address region.
 * @retval  VERR_NOT_FOUND if the access was not cached.
 * @retval  VERR_IOMMU_ADDR_ACCESS_DENIED if the access was cached but permissions
 *          are insufficient.
 *
 * @param   pDevIns         The IOMMU instance data.
 * @param   idDevice        The device ID (bus, device, function).
 * @param   uIova           The I/O virtual address to lookup.
 * @param   cbIova          The size of the access.
 * @param   fPerm           The I/O permissions for this access, see
 *                          IOMMU_IO_PERM_XXX.
 * @param   enmOp           The IOMMU operation being performed.
 * @param   pGCPhysSpa      Where to store the translated system physical address.
 * @param   pcbContiguous   Where to store the number of contiguous bytes translated
 *                          and permission-checked.
 */
static int iommuAmdIotlbCacheLookup(PPDMDEVINS pDevIns, uint16_t idDevice, uint64_t uIova, size_t cbIova, uint8_t fPerm,
                                    IOMMUOP enmOp, PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
{
    int rc;
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);

#define IOMMU_IOTLB_LOOKUP_FAILED(a_rc) \
    do {                                \
        *pGCPhysSpa    = NIL_RTGCPHYS;  \
        *pcbContiguous = 0;             \
        rc = (a_rc);                    \
    } while (0)

    /*
     * We hold the cache lock across both the DTE and the IOTLB lookups (if any) because
     * we don't want the DTE cache to be invalidate while we perform IOTBL lookups.
     */
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    /* Lookup the DTE cache entry. */
    uint16_t const idxDteCache = iommuAmdDteCacheEntryLookup(pThis, idDevice);
    if (idxDteCache < RT_ELEMENTS(pThis->aDteCache))
    {
        PCDTECACHE pDteCache = &pThis->aDteCache[idxDteCache];
        if ((pDteCache->fFlags & (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_ADDR_TRANSLATE))
                              == (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_ADDR_TRANSLATE))
        {
            /* Lookup IOTLB entries. */
            IOADDRRANGE AddrIn;
            AddrIn.uAddr = uIova;
            AddrIn.cb    = cbIova;
            AddrIn.fPerm = fPerm;

            IOMMUOPAUX Aux;
            Aux.enmOp    = enmOp;
            Aux.pDte     = NULL;
            Aux.idDevice = idDevice;
            Aux.idDomain = pDteCache->idDomain;

            IOPAGELOOKUP AddrOut;
            rc = iommuAmdLookupIoAddrRange(pDevIns, iommuAmdCacheLookupPage, &AddrIn, &Aux, &AddrOut, pcbContiguous);
            *pGCPhysSpa = AddrOut.GCPhysSpa;
            Assert(*pcbContiguous <= cbIova);
        }
        else if ((pDteCache->fFlags & (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_IO_PERM))
                                   == (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_IO_PERM))
        {
            /* Address translation is disabled, but DTE permissions apply. */
            Assert(!(pDteCache->fFlags & IOMMU_DTE_CACHE_F_ADDR_TRANSLATE));
            uint8_t const fDtePerm = (pDteCache->fFlags >> IOMMU_DTE_CACHE_F_IO_PERM_SHIFT) & IOMMU_DTE_CACHE_F_IO_PERM_MASK;
            if ((fDtePerm & fPerm) == fPerm)
            {
                *pGCPhysSpa    = uIova;
                *pcbContiguous = cbIova;
                rc = VINF_SUCCESS;
            }
            else
                IOMMU_IOTLB_LOOKUP_FAILED(VERR_IOMMU_ADDR_ACCESS_DENIED);
        }
        else if (pDteCache->fFlags & IOMMU_DTE_CACHE_F_PRESENT)
        {
            /* Forward addresses untranslated, without checking permissions. */
            *pGCPhysSpa    = uIova;
            *pcbContiguous = cbIova;
            rc = VINF_SUCCESS;
        }
        else
            IOMMU_IOTLB_LOOKUP_FAILED(VERR_NOT_FOUND);
    }
    else
        IOMMU_IOTLB_LOOKUP_FAILED(VERR_NOT_FOUND);

    IOMMU_CACHE_UNLOCK(pDevIns, pThis);

    return rc;

#undef IOMMU_IOTLB_LOOKUP_FAILED
}
#endif /* IOMMU_WITH_IOTLBE_CACHE */


/**
 * Gets the I/O permission and IOMMU operation type for the given access flags.
 *
 * @param   pThis       The shared IOMMU device state.
 * @param   fFlags      The PDM IOMMU flags, PDMIOMMU_MEM_F_XXX.
 * @param   penmOp      Where to store the IOMMU operation.
 * @param   pfPerm      Where to store the IOMMU I/O permission.
 * @param   fBulk       Whether this is a bulk read or write.
 */
DECLINLINE(void) iommuAmdMemAccessGetPermAndOp(PIOMMU pThis, uint32_t fFlags, PIOMMUOP penmOp, uint8_t *pfPerm, bool fBulk)
{
    if (fFlags & PDMIOMMU_MEM_F_WRITE)
    {
        *penmOp = IOMMUOP_MEM_WRITE;
        *pfPerm = IOMMU_IO_PERM_WRITE;
#ifdef VBOX_WITH_STATISTICS
        if (!fBulk)
            STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMemWrite));
        else
            STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMemBulkWrite));
#else
        RT_NOREF2(pThis, fBulk);
#endif
    }
    else
    {
        Assert(fFlags & PDMIOMMU_MEM_F_READ);
        *penmOp = IOMMUOP_MEM_READ;
        *pfPerm = IOMMU_IO_PERM_READ;
#ifdef VBOX_WITH_STATISTICS
        if (!fBulk)
            STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMemRead));
        else
            STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMemBulkRead));
#else
        RT_NOREF2(pThis, fBulk);
#endif
    }
}


/**
 * Memory access transaction from a device.
 *
 * @returns VBox status code.
 * @param   pDevIns         The IOMMU device instance.
 * @param   idDevice        The device ID (bus, device, function).
 * @param   uIova           The I/O virtual address being accessed.
 * @param   cbIova          The size of the access.
 * @param   fFlags          The access flags, see PDMIOMMU_MEM_F_XXX.
 * @param   pGCPhysSpa      Where to store the translated system physical address.
 * @param   pcbContiguous   Where to store the number of contiguous bytes translated
 *                          and permission-checked.
 *
 * @thread  Any.
 */
static DECLCALLBACK(int) iommuAmdMemAccess(PPDMDEVINS pDevIns, uint16_t idDevice, uint64_t uIova, size_t cbIova,
                                           uint32_t fFlags, PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
{
    /* Validate. */
    AssertPtr(pDevIns);
    AssertPtr(pGCPhysSpa);
    Assert(cbIova > 0);
    Assert(!(fFlags & ~PDMIOMMU_MEM_F_VALID_MASK));

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CTRL_T const Ctrl = iommuAmdGetCtrlUnlocked(pThis);
    if (Ctrl.n.u1IommuEn)
    {
        IOMMUOP enmOp;
        uint8_t fPerm;
        iommuAmdMemAccessGetPermAndOp(pThis, fFlags, &enmOp, &fPerm, false /* fBulk */);
        LogFlowFunc(("%s: idDevice=%#x uIova=%#RX64 cb=%zu\n", iommuAmdMemAccessGetPermName(fPerm), idDevice, uIova, cbIova));

        int rc;
#ifdef IOMMU_WITH_IOTLBE_CACHE
        /* Lookup the IOVA from the cache. */
        rc = iommuAmdIotlbCacheLookup(pDevIns, idDevice, uIova, cbIova, fPerm, enmOp, pGCPhysSpa, pcbContiguous);
        if (rc == VINF_SUCCESS)
        {
            /* All pages in the access were found in the cache with sufficient permissions. */
            Assert(*pcbContiguous == cbIova);
            Assert(*pGCPhysSpa != NIL_RTGCPHYS);
            STAM_COUNTER_INC(&pThis->StatAccessCacheHitFull);
            return VINF_SUCCESS;
        }
        if (rc != VERR_OUT_OF_RANGE)
        { /* likely */ }
        else
        {
            /* Access stopped since translations resulted in non-contiguous memory, let caller resume access. */
            Assert(*pcbContiguous > 0 && *pcbContiguous < cbIova);
            STAM_COUNTER_INC(&pThis->StatAccessCacheNonContig);
            return VINF_SUCCESS;
        }

        /*
         * Access incomplete as not all pages were in the cache.
         * Or permissions were denied for the access (which typically doesn't happen)
         * so go through the slower path and raise the required event.
         */
        AssertMsg(*pcbContiguous < cbIova, ("Invalid size: cbContiguous=%zu cbIova=%zu\n", *pcbContiguous, cbIova));
        uIova  += *pcbContiguous;
        cbIova -= *pcbContiguous;
        /* We currently are including any permission denied pages as cache misses too.*/
        STAM_COUNTER_INC(&pThis->StatAccessCacheMiss);
#endif

        /* Lookup the IOVA from the device table. */
        rc = iommuAmdDteLookup(pDevIns, idDevice, uIova, cbIova, fPerm, enmOp, pGCPhysSpa, pcbContiguous);
        if (RT_SUCCESS(rc))
        { /* likely */ }
        else
        {
            Assert(rc != VERR_OUT_OF_RANGE);
            LogFunc(("DTE lookup failed! idDevice=%#x uIova=%#RX64 fPerm=%u cbIova=%zu rc=%#Rrc\n", idDevice, uIova, fPerm,
                     cbIova, rc));
        }

        return rc;
    }

    /* Addresses are forwarded without translation when the IOMMU is disabled. */
    *pGCPhysSpa    = uIova;
    *pcbContiguous = cbIova;
    return VINF_SUCCESS;
}


/**
 * Memory access bulk (one or more 4K pages) request from a device.
 *
 * @returns VBox status code.
 * @param   pDevIns         The IOMMU device instance.
 * @param   idDevice        The device ID (bus, device, function).
 * @param   cIovas          The number of addresses being accessed.
 * @param   pauIovas        The I/O virtual addresses for each page being accessed.
 * @param   fFlags          The access flags, see PDMIOMMU_MEM_F_XXX.
 * @param   paGCPhysSpa     Where to store the translated physical addresses.
 *
 * @thread  Any.
 */
static DECLCALLBACK(int) iommuAmdMemBulkAccess(PPDMDEVINS pDevIns, uint16_t idDevice, size_t cIovas, uint64_t const *pauIovas,
                                               uint32_t fFlags, PRTGCPHYS paGCPhysSpa)
{
    /* Validate. */
    AssertPtr(pDevIns);
    Assert(cIovas > 0);
    AssertPtr(pauIovas);
    AssertPtr(paGCPhysSpa);
    Assert(!(fFlags & ~PDMIOMMU_MEM_F_VALID_MASK));

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CTRL_T const Ctrl = iommuAmdGetCtrlUnlocked(pThis);
    if (Ctrl.n.u1IommuEn)
    {
        IOMMUOP enmOp;
        uint8_t fPerm;
        iommuAmdMemAccessGetPermAndOp(pThis, fFlags, &enmOp, &fPerm, true /* fBulk */);
        LogFlowFunc(("%s: idDevice=%#x cIovas=%zu\n", iommuAmdMemAccessGetPermName(fPerm), idDevice, cIovas));

        for (size_t i = 0; i < cIovas; i++)
        {
            int    rc;
            size_t cbContig;

#ifdef IOMMU_WITH_IOTLBE_CACHE
            /* Lookup the IOVA from the IOTLB cache. */
            rc = iommuAmdIotlbCacheLookup(pDevIns, idDevice, pauIovas[i], X86_PAGE_SIZE, fPerm, enmOp, &paGCPhysSpa[i],
                                          &cbContig);
            if (rc == VINF_SUCCESS)
            {
                Assert(cbContig == X86_PAGE_SIZE);
                Assert(paGCPhysSpa[i] != NIL_RTGCPHYS);
                STAM_COUNTER_INC(&pThis->StatAccessCacheHitFull);
                continue;
            }
            Assert(rc == VERR_NOT_FOUND || rc == VERR_IOMMU_ADDR_ACCESS_DENIED);
            STAM_COUNTER_INC(&pThis->StatAccessCacheMiss);
#endif

            /* Lookup the IOVA from the device table. */
            rc = iommuAmdDteLookup(pDevIns, idDevice, pauIovas[i], X86_PAGE_SIZE, fPerm, enmOp, &paGCPhysSpa[i], &cbContig);
            if (RT_SUCCESS(rc))
            { /* likely */ }
            else
            {
                LogFunc(("Failed! idDevice=%#x uIova=%#RX64 fPerm=%u rc=%Rrc\n", idDevice, pauIovas[i], fPerm, rc));
                return rc;
            }
            Assert(cbContig == X86_PAGE_SIZE);
        }
    }
    else
    {
        /* Addresses are forwarded without translation when the IOMMU is disabled. */
        for (size_t i = 0; i < cIovas; i++)
            paGCPhysSpa[i] = pauIovas[i];
    }

    return VINF_SUCCESS;
}


/**
 * Reads an interrupt remapping table entry from guest memory given its DTE.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   pDte        The device table entry.
 * @param   GCPhysIn    The source MSI address (used for reporting errors).
 * @param   uDataIn     The source MSI data.
 * @param   enmOp       The IOMMU operation being performed.
 * @param   pIrte       Where to store the interrupt remapping table entry.
 *
 * @thread  Any.
 */
static int iommuAmdIrteRead(PPDMDEVINS pDevIns, uint16_t idDevice, PCDTE_T pDte, RTGCPHYS GCPhysIn, uint32_t uDataIn,
                            IOMMUOP enmOp, PIRTE_T pIrte)
{
    /* Ensure the IRTE length is valid. */
    Assert(pDte->n.u4IntrTableLength < IOMMU_DTE_INTR_TAB_LEN_MAX);

    RTGCPHYS const GCPhysIntrTable = pDte->au64[2] & IOMMU_DTE_IRTE_ROOT_PTR_MASK;
    uint16_t const cbIntrTable     = IOMMU_DTE_GET_INTR_TAB_LEN(pDte);
    uint16_t const offIrte         = IOMMU_GET_IRTE_OFF(uDataIn);
    RTGCPHYS const GCPhysIrte      = GCPhysIntrTable + offIrte;

    /* Ensure the IRTE falls completely within the interrupt table. */
    if (offIrte + sizeof(IRTE_T) <= cbIntrTable)
    { /* likely */ }
    else
    {
        LogFunc(("IRTE exceeds table length (GCPhysIntrTable=%#RGp cbIntrTable=%u offIrte=%#x uDataIn=%#x) -> IOPF\n",
                 GCPhysIntrTable, cbIntrTable, offIrte, uDataIn));

        EVT_IO_PAGE_FAULT_T EvtIoPageFault;
        iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, GCPhysIn, false /* fPresent */, false /* fRsvdNotZero */,
                                     false /* fPermDenied */, enmOp, &EvtIoPageFault);
        iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
                                             kIoPageFaultType_IrteAddrInvalid);
        return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
    }

    /* Read the IRTE from memory. */
    Assert(!(GCPhysIrte & 3));
    int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysIrte, pIrte, sizeof(*pIrte));
    if (RT_SUCCESS(rc))
        return VINF_SUCCESS;

    /** @todo The IOMMU spec. does not tell what kind of error is reported in this
     *        situation. Is it an I/O page fault or a device table hardware error?
     *        There's no interrupt table hardware error event, but it's unclear what
     *        we should do here. */
    LogFunc(("Failed to read interrupt table entry at %#RGp. rc=%Rrc -> ???\n", GCPhysIrte, rc));
    return VERR_IOMMU_IPE_4;
}


/**
 * Remaps the interrupt using the interrupt remapping table.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   pDte        The device table entry.
 * @param   enmOp       The IOMMU operation being performed.
 * @param   pMsiIn      The source MSI.
 * @param   pMsiOut     Where to store the remapped MSI.
 *
 * @thread  Any.
 */
static int iommuAmdIntrRemap(PPDMDEVINS pDevIns, uint16_t idDevice, PCDTE_T pDte, IOMMUOP enmOp, PCMSIMSG pMsiIn,
                             PMSIMSG pMsiOut)
{
    Assert(pDte->n.u2IntrCtrl == IOMMU_INTR_CTRL_REMAP);

    IRTE_T Irte;
    uint32_t const uMsiInData = pMsiIn->Data.u32;
    int rc = iommuAmdIrteRead(pDevIns, idDevice, pDte, pMsiIn->Addr.u64, uMsiInData, enmOp, &Irte);
    if (RT_SUCCESS(rc))
    {
        if (Irte.n.u1RemapEnable)
        {
            if (!Irte.n.u1GuestMode)
            {
                if (Irte.n.u3IntrType <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO)
                {
                    iommuAmdIrteRemapMsi(pMsiIn, pMsiOut, &Irte);
#ifdef IOMMU_WITH_IRTE_CACHE
                    iommuAmdIrteCacheAdd(pDevIns, idDevice, IOMMU_GET_IRTE_OFF(uMsiInData), &Irte);
#endif
                    return VINF_SUCCESS;
                }

                LogFunc(("Interrupt type (%#x) invalid -> IOPF\n", Irte.n.u3IntrType));
                EVT_IO_PAGE_FAULT_T EvtIoPageFault;
                iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, pMsiIn->Addr.u64, Irte.n.u1RemapEnable,
                                             true /* fRsvdNotZero */, false /* fPermDenied */, enmOp, &EvtIoPageFault);
                iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, &Irte, enmOp, &EvtIoPageFault,
                                                     kIoPageFaultType_IrteRsvdIntType);
                return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
            }

            LogFunc(("Guest mode not supported -> IOPF\n"));
            EVT_IO_PAGE_FAULT_T EvtIoPageFault;
            iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, pMsiIn->Addr.u64, Irte.n.u1RemapEnable,
                                         true /* fRsvdNotZero */, false /* fPermDenied */, enmOp, &EvtIoPageFault);
            iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, &Irte, enmOp, &EvtIoPageFault, kIoPageFaultType_IrteRsvdNotZero);
            return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
        }

        LogFunc(("Remapping disabled -> IOPF\n"));
        EVT_IO_PAGE_FAULT_T EvtIoPageFault;
        iommuAmdIoPageFaultEventInit(idDevice, pDte->n.u16DomainId, pMsiIn->Addr.u64, Irte.n.u1RemapEnable,
                                     false /* fRsvdNotZero */, false /* fPermDenied */, enmOp, &EvtIoPageFault);
        iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, &Irte, enmOp, &EvtIoPageFault, kIoPageFaultType_IrteRemapEn);
        return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
    }

    return rc;
}


/**
 * Looks up an MSI interrupt from the interrupt remapping table.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU instance data.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   enmOp       The IOMMU operation being performed.
 * @param   pMsiIn      The source MSI.
 * @param   pMsiOut     Where to store the remapped MSI.
 *
 * @thread  Any.
 */
static int iommuAmdIntrTableLookup(PPDMDEVINS pDevIns, uint16_t idDevice, IOMMUOP enmOp, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
{
    LogFlowFunc(("idDevice=%#x (%#x:%#x:%#x) enmOp=%u\n", idDevice, ((idDevice >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK),
                 ((idDevice >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK), (idDevice & VBOX_PCI_DEVFN_FUN_MASK),
                 enmOp));

    /* Read the device table entry from memory. */
    DTE_T Dte;
    int rc = iommuAmdDteRead(pDevIns, idDevice, enmOp, &Dte);
    if (RT_SUCCESS(rc))
    {
#ifdef IOMMU_WITH_IRTE_CACHE
        iommuAmdDteCacheAdd(pDevIns, idDevice, &Dte);
#endif
        /* If the DTE is not valid, all interrupts are forwarded without remapping. */
        if (Dte.n.u1IntrMapValid)
        {
            /* Validate bits 255:128 of the device table entry when DTE.IV is 1. */
            uint64_t const fRsvd0 = Dte.au64[2] & ~IOMMU_DTE_QWORD_2_VALID_MASK;
            uint64_t const fRsvd1 = Dte.au64[3] & ~IOMMU_DTE_QWORD_3_VALID_MASK;
            if (RT_LIKELY(!fRsvd0 && !fRsvd1))
            { /* likely */ }
            else
            {
                LogFunc(("Invalid reserved bits in DTE (u64[2]=%#RX64 u64[3]=%#RX64) -> Illegal DTE\n", fRsvd0, fRsvd1));
                EVT_ILLEGAL_DTE_T Event;
                iommuAmdIllegalDteEventInit(idDevice, pMsiIn->Addr.u64, true /* fRsvdNotZero */, enmOp, &Event);
                iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdNotZero);
                return VERR_IOMMU_INTR_REMAP_FAILED;
            }

            /*
             * LINT0/LINT1 pins cannot be driven by PCI(e) devices. Perhaps for a Southbridge
             * that's connected through HyperTransport it might be possible; but for us, it
             * doesn't seem we need to specially handle these pins.
             */

            /*
             * Validate the MSI source address.
             *
             * 64-bit MSIs are supported by the PCI and AMD IOMMU spec. However as far as the
             * CPU is concerned, the MSI region is fixed and we must ensure no other device
             * claims the region as I/O space.
             *
             * See PCI spec. 6.1.4. "Message Signaled Interrupt (MSI) Support".
             * See AMD IOMMU spec. 2.8 "IOMMU Interrupt Support".
             * See Intel spec. 10.11.1 "Message Address Register Format".
             */
            if ((pMsiIn->Addr.u64 & VBOX_MSI_ADDR_ADDR_MASK) == VBOX_MSI_ADDR_BASE)
            {
                /*
                 * The IOMMU remaps fixed and arbitrated interrupts using the IRTE.
                 * See AMD IOMMU spec. "2.2.5.1 Interrupt Remapping Tables, Guest Virtual APIC Not Enabled".
                 */
                uint8_t const u8DeliveryMode = pMsiIn->Data.n.u3DeliveryMode;
                bool fPassThru = false;
                switch (u8DeliveryMode)
                {
                    case VBOX_MSI_DELIVERY_MODE_FIXED:
                    case VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO:
                    {
                        uint8_t const uIntrCtrl = Dte.n.u2IntrCtrl;
                        if (uIntrCtrl == IOMMU_INTR_CTRL_REMAP)
                        {
                            /* Validate the encoded interrupt table length when IntCtl specifies remapping. */
                            uint8_t const uIntrTabLen = Dte.n.u4IntrTableLength;
                            if (uIntrTabLen < IOMMU_DTE_INTR_TAB_LEN_MAX)
                            {
                                /*
                                 * We don't support guest interrupt remapping yet. When we do, we'll need to
                                 * check Ctrl.u1GstVirtApicEn and use the guest Virtual APIC Table Root Pointer
                                 * in the DTE rather than the Interrupt Root Table Pointer. Since the caller
                                 * already reads the control register, add that as a parameter when we eventually
                                 * support guest interrupt remapping. For now, just assert.
                                 */
                                PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
                                Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);
                                NOREF(pThis);

                                return iommuAmdIntrRemap(pDevIns, idDevice, &Dte, enmOp, pMsiIn, pMsiOut);
                            }

                            LogFunc(("Invalid interrupt table length %#x -> Illegal DTE\n", uIntrTabLen));
                            EVT_ILLEGAL_DTE_T Event;
                            iommuAmdIllegalDteEventInit(idDevice, pMsiIn->Addr.u64, false /* fRsvdNotZero */, enmOp, &Event);
                            iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdIntTabLen);
                            return VERR_IOMMU_INTR_REMAP_FAILED;
                        }

                        if (uIntrCtrl == IOMMU_INTR_CTRL_FWD_UNMAPPED)
                        {
                            fPassThru = true;
                            break;
                        }

                        if (uIntrCtrl == IOMMU_INTR_CTRL_TARGET_ABORT)
                        {
                            LogRelMax(10, ("%s: Remapping disallowed for fixed/arbitrated interrupt %#x -> Target abort\n",
                                           IOMMU_LOG_PFX, pMsiIn->Data.n.u8Vector));
                            iommuAmdSetPciTargetAbort(pDevIns);
                            return VERR_IOMMU_INTR_REMAP_DENIED;
                        }

                        Assert(uIntrCtrl == IOMMU_INTR_CTRL_RSVD); /* Paranoia. */
                        LogRelMax(10, ("%s: IntCtl mode invalid %#x -> Illegal DTE\n", IOMMU_LOG_PFX, uIntrCtrl));
                        EVT_ILLEGAL_DTE_T Event;
                        iommuAmdIllegalDteEventInit(idDevice, pMsiIn->Addr.u64, true /* fRsvdNotZero */, enmOp, &Event);
                        iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdIntCtl);
                        return VERR_IOMMU_INTR_REMAP_FAILED;
                    }

                    /* SMIs are passed through unmapped. We don't implement SMI filters. */
                    case VBOX_MSI_DELIVERY_MODE_SMI:        fPassThru = true;                   break;
                    case VBOX_MSI_DELIVERY_MODE_NMI:        fPassThru = Dte.n.u1NmiPassthru;    break;
                    case VBOX_MSI_DELIVERY_MODE_INIT:       fPassThru = Dte.n.u1InitPassthru;   break;
                    case VBOX_MSI_DELIVERY_MODE_EXT_INT:    fPassThru = Dte.n.u1ExtIntPassthru; break;
                    default:
                    {
                        LogRelMax(10, ("%s: MSI data delivery mode invalid %#x -> Target abort\n", IOMMU_LOG_PFX,
                                       u8DeliveryMode));
                        iommuAmdSetPciTargetAbort(pDevIns);
                        return VERR_IOMMU_INTR_REMAP_FAILED;
                    }
                }

                /*
                 * For those other than fixed and arbitrated interrupts, destination mode must be 0 (physical).
                 * See AMD IOMMU spec. The note below Table 19: "IOMMU Controls and Actions for Upstream Interrupts".
                 */
                if (   u8DeliveryMode <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO
                    || !pMsiIn->Addr.n.u1DestMode)
                {
                    if (fPassThru)
                    {
                        *pMsiOut = *pMsiIn;
                        return VINF_SUCCESS;
                    }
                    LogRelMax(10, ("%s: Remapping/passthru disallowed for interrupt %#x -> Target abort\n", IOMMU_LOG_PFX,
                                   pMsiIn->Data.n.u8Vector));
                }
                else
                    LogRelMax(10, ("%s: Logical destination mode invalid for delivery mode %#x\n -> Target abort\n",
                                   IOMMU_LOG_PFX, u8DeliveryMode));

                iommuAmdSetPciTargetAbort(pDevIns);
                return VERR_IOMMU_INTR_REMAP_DENIED;
            }
            else
            {
                /** @todo should be cause a PCI target abort here? */
                LogRelMax(10, ("%s: MSI address region invalid %#RX64\n", IOMMU_LOG_PFX, pMsiIn->Addr.u64));
                return VERR_IOMMU_INTR_REMAP_FAILED;
            }
        }
        else
        {
            LogFlowFunc(("DTE interrupt map not valid\n"));
            *pMsiOut = *pMsiIn;
            return VINF_SUCCESS;
        }
    }

    LogFunc(("Failed to read device table entry. idDevice=%#x rc=%Rrc\n", idDevice, rc));
    return VERR_IOMMU_INTR_REMAP_FAILED;
}


/**
 * Interrupt remap request from a device.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   idDevice    The device ID (bus, device, function).
 * @param   pMsiIn      The source MSI.
 * @param   pMsiOut     Where to store the remapped MSI.
 */
static DECLCALLBACK(int) iommuAmdMsiRemap(PPDMDEVINS pDevIns, uint16_t idDevice, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
{
    /* Validate. */
    Assert(pDevIns);
    Assert(pMsiIn);
    Assert(pMsiOut);

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);

    /* If this MSI was generated by the IOMMU itself, it's not subject to remapping, see @bugref{9654#c104}. */
    if (idDevice == pThis->uPciAddress)
        return VERR_IOMMU_CANNOT_CALL_SELF;

    /* Interrupts are forwarded with remapping when the IOMMU is disabled. */
    IOMMU_CTRL_T const Ctrl = iommuAmdGetCtrlUnlocked(pThis);
    if (Ctrl.n.u1IommuEn)
    {
        STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMsiRemap));

        int rc;
#ifdef IOMMU_WITH_IRTE_CACHE
        STAM_PROFILE_ADV_START(&pThis->StatProfIrteCacheLookup, a);
        rc = iommuAmdIrteCacheLookup(pDevIns, idDevice, IOMMUOP_INTR_REQ, pMsiIn, pMsiOut);
        STAM_PROFILE_ADV_STOP(&pThis->StatProfIrteCacheLookup, a);
        if (RT_SUCCESS(rc))
        {
            STAM_COUNTER_INC(&pThis->StatIntrCacheHit);
            return VINF_SUCCESS;
        }
        STAM_COUNTER_INC(&pThis->StatIntrCacheMiss);
#endif

        STAM_PROFILE_ADV_START(&pThis->StatProfIrteLookup, a);
        rc = iommuAmdIntrTableLookup(pDevIns, idDevice, IOMMUOP_INTR_REQ, pMsiIn, pMsiOut);
        STAM_PROFILE_ADV_STOP(&pThis->StatProfIrteLookup, a);
        return rc;
    }

    *pMsiOut = *pMsiIn;
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNIOMMMIONEWWRITE}
 */
static DECLCALLBACK(VBOXSTRICTRC) iommuAmdMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
{
    NOREF(pvUser);
    Assert(cb == 4 || cb == 8);
    Assert(!(off & (cb - 1)));

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioWrite)); NOREF(pThis);

    uint64_t const uValue = cb == 8 ? *(uint64_t const *)pv : *(uint32_t const *)pv;
    return iommuAmdRegisterWrite(pDevIns, off, cb, uValue);
}


/**
 * @callback_method_impl{FNIOMMMIONEWREAD}
 */
static DECLCALLBACK(VBOXSTRICTRC) iommuAmdMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
{
    NOREF(pvUser);
    Assert(cb == 4 || cb == 8);
    Assert(!(off & (cb - 1)));

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioRead)); NOREF(pThis);

    uint64_t uResult;
    VBOXSTRICTRC rcStrict = iommuAmdRegisterRead(pDevIns, off, &uResult);
    if (rcStrict == VINF_SUCCESS)
    {
        if (cb == 8)
            *(uint64_t *)pv = uResult;
        else
            *(uint32_t *)pv = (uint32_t)uResult;
    }

    return rcStrict;
}


#ifdef IN_RING3
/**
 * Processes an IOMMU command.
 *
 * @returns VBox status code.
 * @param   pDevIns         The IOMMU device instance.
 * @param   pCmd            The command to process.
 * @param   GCPhysCmd       The system physical address of the command.
 * @param   pEvtError       Where to store the error event in case of failures.
 *
 * @thread  Command thread.
 */
static int iommuAmdR3CmdProcess(PPDMDEVINS pDevIns, PCCMD_GENERIC_T pCmd, RTGCPHYS GCPhysCmd, PEVT_GENERIC_T pEvtError)
{
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);

    STAM_COUNTER_INC(&pThis->StatCmd);

    uint8_t const bCmd = pCmd->n.u4Opcode;
    switch (bCmd)
    {
        case IOMMU_CMD_COMPLETION_WAIT:
        {
            STAM_COUNTER_INC(&pThis->StatCmdCompWait);

            PCCMD_COMWAIT_T pCmdComWait = (PCCMD_COMWAIT_T)pCmd;
            AssertCompile(sizeof(*pCmdComWait) == sizeof(*pCmd));

            /* Validate reserved bits in the command. */
            if (!(pCmdComWait->au64[0] & ~IOMMU_CMD_COM_WAIT_QWORD_0_VALID_MASK))
            {
                /* If Completion Store is requested, write the StoreData to the specified address. */
                if (pCmdComWait->n.u1Store)
                {
                    RTGCPHYS const GCPhysStore = RT_MAKE_U64(pCmdComWait->n.u29StoreAddrLo << 3, pCmdComWait->n.u20StoreAddrHi);
                    uint64_t const u64Data     = pCmdComWait->n.u64StoreData;
                    int rc = PDMDevHlpPCIPhysWrite(pDevIns, GCPhysStore, &u64Data, sizeof(u64Data));
                    if (RT_FAILURE(rc))
                    {
                        LogFunc(("Cmd(%#x): Failed to write StoreData (%#RX64) to %#RGp, rc=%Rrc\n", bCmd, u64Data,
                             GCPhysStore, rc));
                        iommuAmdCmdHwErrorEventInit(GCPhysStore, (PEVT_CMD_HW_ERR_T)pEvtError);
                        return VERR_IOMMU_CMD_HW_ERROR;
                    }
                }

                /* If the command requests an interrupt and completion wait interrupts are enabled, raise it. */
                if (pCmdComWait->n.u1Interrupt)
                {
                    IOMMU_LOCK(pDevIns, pThisR3);
                    ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_COMPLETION_WAIT_INTR);
                    bool const fRaiseInt = pThis->Ctrl.n.u1CompWaitIntrEn;
                    IOMMU_UNLOCK(pDevIns, pThisR3);
                    if (fRaiseInt)
                        iommuAmdMsiInterruptRaise(pDevIns);
                }
                return VINF_SUCCESS;
            }
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_INVALID_FORMAT;
        }

        case IOMMU_CMD_INV_DEV_TAB_ENTRY:
        {
            STAM_COUNTER_INC(&pThis->StatCmdInvDte);
            PCCMD_INV_DTE_T pCmdInvDte = (PCCMD_INV_DTE_T)pCmd;
            AssertCompile(sizeof(*pCmdInvDte) == sizeof(*pCmd));

            /* Validate reserved bits in the command. */
            if (   !(pCmdInvDte->au64[0] & ~IOMMU_CMD_INV_DTE_QWORD_0_VALID_MASK)
                && !(pCmdInvDte->au64[1] & ~IOMMU_CMD_INV_DTE_QWORD_1_VALID_MASK))
            {
#ifdef IOMMU_WITH_DTE_CACHE
                iommuAmdDteCacheRemove(pDevIns, pCmdInvDte->n.u16DevId);
#endif
                return VINF_SUCCESS;
            }
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_INVALID_FORMAT;
        }

        case IOMMU_CMD_INV_IOMMU_PAGES:
        {
            STAM_COUNTER_INC(&pThis->StatCmdInvIommuPages);
            PCCMD_INV_IOMMU_PAGES_T pCmdInvPages = (PCCMD_INV_IOMMU_PAGES_T)pCmd;
            AssertCompile(sizeof(*pCmdInvPages) == sizeof(*pCmd));

            /* Validate reserved bits in the command. */
            if (   !(pCmdInvPages->au64[0] & ~IOMMU_CMD_INV_IOMMU_PAGES_QWORD_0_VALID_MASK)
                && !(pCmdInvPages->au64[1] & ~IOMMU_CMD_INV_IOMMU_PAGES_QWORD_1_VALID_MASK))
            {
#ifdef IOMMU_WITH_IOTLBE_CACHE
                uint64_t const uIova = RT_MAKE_U64(pCmdInvPages->n.u20AddrLo << X86_PAGE_4K_SHIFT, pCmdInvPages->n.u32AddrHi);
                uint16_t const idDomain  = pCmdInvPages->n.u16DomainId;
                uint8_t cShift;
                if (!pCmdInvPages->n.u1Size)
                    cShift = X86_PAGE_4K_SHIFT;
                else
                {
                    /* Find the first clear bit starting from bit 12 to 64 of the I/O virtual address. */
                    unsigned const uFirstZeroBit = ASMBitLastSetU64(~(uIova >> X86_PAGE_4K_SHIFT));
                    cShift = X86_PAGE_4K_SHIFT + uFirstZeroBit;

                    /*
                     * For the address 0x7ffffffffffff000, cShift would be 76 (12+64) and the code below
                     * would do the right thing by clearing the entire cache for the specified domain ID.
                     *
                     * However, for the address 0xfffffffffffff000, cShift would be computed as 12.
                     * IOMMU behavior is undefined in this case, so it's safe to invalidate just one page.
                     * A debug-time assert is in place here to let us know if any software tries this.
                     *
                     * See AMD IOMMU spec. 2.4.3 "INVALIDATE_IOMMU_PAGES".
                     * See AMD IOMMU spec. Table 14: "Example Page Size Encodings".
                     */
                    Assert(uIova != UINT64_C(0xfffffffffffff000));
                }

                /*
                 * Validate invalidation size.
                 * See AMD IOMMU spec. 2.2.3 "I/O Page Tables for Host Translations".
                 */
                if (   cShift >= 12 /* 4 KB */
                    && cShift <= 51 /* 2 PB */)
                {
                    /* Remove the range of I/O virtual addresses requesting to be invalidated. */
                    size_t const cbIova = RT_BIT_64(cShift);
                    iommuAmdIotlbRemoveRange(pDevIns, idDomain, uIova, cbIova);
                }
                else
                {
                    /*
                     * The guest provided size is invalid or exceeds the largest, meaningful page size.
                     * In such situations we must remove all ranges for the specified domain ID.
                     */
                    iommuAmdIotlbRemoveDomainId(pDevIns, idDomain);
                }
#endif
                return VINF_SUCCESS;
            }
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_INVALID_FORMAT;
        }

        case IOMMU_CMD_INV_IOTLB_PAGES:
        {
            STAM_COUNTER_INC(&pThis->StatCmdInvIotlbPages);

            uint32_t const uCapHdr = PDMPciDevGetDWord(pDevIns->apPciDevs[0], IOMMU_PCI_OFF_CAP_HDR);
            if (RT_BF_GET(uCapHdr, IOMMU_BF_CAPHDR_IOTLB_SUP))
            {
                /** @todo IOMMU: Implement remote IOTLB invalidation. */
                return VERR_NOT_IMPLEMENTED;
            }
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_NOT_SUPPORTED;
        }

        case IOMMU_CMD_INV_INTR_TABLE:
        {
            STAM_COUNTER_INC(&pThis->StatCmdInvIntrTable);

            PCCMD_INV_INTR_TABLE_T pCmdInvIntrTable = (PCCMD_INV_INTR_TABLE_T)pCmd;
            AssertCompile(sizeof(*pCmdInvIntrTable) == sizeof(*pCmd));

            /* Validate reserved bits in the command. */
            if (   !(pCmdInvIntrTable->au64[0] & ~IOMMU_CMD_INV_INTR_TABLE_QWORD_0_VALID_MASK)
                && !(pCmdInvIntrTable->au64[1] & ~IOMMU_CMD_INV_INTR_TABLE_QWORD_1_VALID_MASK))
            {
#ifdef IOMMU_WITH_IRTE_CACHE
                iommuAmdIrteCacheRemove(pDevIns, pCmdInvIntrTable->u.u16DevId);
#endif
                return VINF_SUCCESS;
            }
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_INVALID_FORMAT;
        }

        case IOMMU_CMD_PREFETCH_IOMMU_PAGES:
        {
            /* Linux doesn't use prefetching of IOMMU pages, so we don't bother for now. */
            STAM_COUNTER_INC(&pThis->StatCmdPrefIommuPages);
            Assert(!pThis->ExtFeat.n.u1PrefetchSup);
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_NOT_SUPPORTED;
        }

        case IOMMU_CMD_COMPLETE_PPR_REQ:
        {
            STAM_COUNTER_INC(&pThis->StatCmdCompletePprReq);

            /* We don't support PPR requests yet. */
            Assert(!pThis->ExtFeat.n.u1PprSup);
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_NOT_SUPPORTED;
        }

        case IOMMU_CMD_INV_IOMMU_ALL:
        {
            STAM_COUNTER_INC(&pThis->StatCmdInvIommuAll);
            if (pThis->ExtFeat.n.u1InvAllSup)
            {
                PCCMD_INV_IOMMU_ALL_T pCmdInvAll = (PCCMD_INV_IOMMU_ALL_T)pCmd;
                AssertCompile(sizeof(*pCmdInvAll) == sizeof(*pCmd));

                /* Validate reserved bits in the command. */
                if (   !(pCmdInvAll->au64[0] & ~IOMMU_CMD_INV_IOMMU_ALL_QWORD_0_VALID_MASK)
                    && !(pCmdInvAll->au64[1] & ~IOMMU_CMD_INV_IOMMU_ALL_QWORD_1_VALID_MASK))
                {
#ifdef IOMMU_WITH_DTE_CACHE
                    iommuAmdDteCacheRemoveAll(pDevIns);
#endif
#ifdef IOMMU_WITH_IOTLBE_CACHE
                    iommuAmdIotlbRemoveAll(pDevIns);
#endif
                    return VINF_SUCCESS;
                }
                iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
                return VERR_IOMMU_CMD_INVALID_FORMAT;
            }
            iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
            return VERR_IOMMU_CMD_NOT_SUPPORTED;
        }
    }

    STAM_COUNTER_DEC(&pThis->StatCmd);
    LogFunc(("Cmd(%#x): Unrecognized\n", bCmd));
    iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
    return VERR_IOMMU_CMD_NOT_SUPPORTED;
}


/**
 * The IOMMU command thread.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   pThread     The command thread.
 */
static DECLCALLBACK(int) iommuAmdR3CmdThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
{
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);

    if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
        return VINF_SUCCESS;

    /*
     * Pre-allocate the maximum command buffer size supported by the IOMMU.
     * This avoid trashing the heap as well as not wasting time allocating
     * and freeing buffers while processing commands.
     */
    size_t const cbMaxCmdBuf = sizeof(CMD_GENERIC_T) * iommuAmdGetBufMaxEntries(15);
    void *pvCmds = RTMemAllocZ(cbMaxCmdBuf);
    AssertPtrReturn(pvCmds, VERR_NO_MEMORY);

    while (pThread->enmState == PDMTHREADSTATE_RUNNING)
    {
        /*
         * Sleep perpetually until we are woken up to process commands.
         */
        bool const fSignaled = ASMAtomicXchgBool(&pThis->fCmdThreadSignaled, false);
        if (!fSignaled)
        {
            int rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEvtCmdThread, RT_INDEFINITE_WAIT);
            AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
            if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
                break;
            Log4Func(("Woken up with rc=%Rrc\n", rc));
            ASMAtomicWriteBool(&pThis->fCmdThreadSignaled, false);
        }

        /*
         * Fetch and process IOMMU commands.
         */
        /** @todo r=ramshankar: We currently copy all commands from guest memory into a
         *        temporary host buffer before processing them as a batch. If we want to
         *        save on host memory a bit, we could (once PGM has the necessary APIs)
         *        lock the page mappings page mappings and access them directly. */
        IOMMU_LOCK(pDevIns, pThisR3);

        if (pThis->Status.n.u1CmdBufRunning)
        {
            /* Get the offsets we need to read commands from memory (circular buffer offset). */
            uint32_t const cbCmdBuf = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
            uint32_t const offTail  = pThis->CmdBufTailPtr.n.off;
            uint32_t       offHead  = pThis->CmdBufHeadPtr.n.off;

            /* Validate. */
            Assert(!(offHead & ~IOMMU_CMD_BUF_HEAD_PTR_VALID_MASK));
            Assert(offHead < cbCmdBuf);
            Assert(cbCmdBuf <= cbMaxCmdBuf);

            if (offHead != offTail)
            {
                /* Read the entire command buffer from memory (avoids multiple PGM calls). */
                RTGCPHYS const GCPhysCmdBufBase = pThis->CmdBufBaseAddr.n.u40Base << X86_PAGE_4K_SHIFT;

                IOMMU_UNLOCK(pDevIns, pThisR3);
                int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysCmdBufBase, pvCmds, cbCmdBuf);
                IOMMU_LOCK(pDevIns, pThisR3);

                if (RT_SUCCESS(rc))
                {
                    /* Indicate to software we've fetched all commands from the buffer. */
                    pThis->CmdBufHeadPtr.n.off = offTail;

                    /* Allow IOMMU to do other work while we process commands. */
                    IOMMU_UNLOCK(pDevIns, pThisR3);

                    /* Process the fetched commands. */
                    EVT_GENERIC_T EvtError;
                    do
                    {
                        PCCMD_GENERIC_T pCmd = (PCCMD_GENERIC_T)((uintptr_t)pvCmds + offHead);
                        rc = iommuAmdR3CmdProcess(pDevIns, pCmd, GCPhysCmdBufBase + offHead, &EvtError);
                        if (RT_FAILURE(rc))
                        {
                            if (   rc == VERR_IOMMU_CMD_NOT_SUPPORTED
                                || rc == VERR_IOMMU_CMD_INVALID_FORMAT)
                            {
                                Assert(EvtError.n.u4EvtCode == IOMMU_EVT_ILLEGAL_CMD_ERROR);
                                iommuAmdIllegalCmdEventRaise(pDevIns, (PCEVT_ILLEGAL_CMD_ERR_T)&EvtError);
                            }
                            else if (rc == VERR_IOMMU_CMD_HW_ERROR)
                            {
                                Assert(EvtError.n.u4EvtCode == IOMMU_EVT_COMMAND_HW_ERROR);
                                LogFunc(("Raising command hardware error. Cmd=%#x -> COMMAND_HW_ERROR\n", pCmd->n.u4Opcode));
                                iommuAmdCmdHwErrorEventRaise(pDevIns, (PCEVT_CMD_HW_ERR_T)&EvtError);
                            }
                            break;
                        }

                        /* Move to the next command in the circular buffer. */
                        offHead = (offHead + sizeof(CMD_GENERIC_T)) % cbCmdBuf;
                    } while (offHead != offTail);
                }
                else
                {
                    LogFunc(("Failed to read command at %#RGp. rc=%Rrc -> COMMAND_HW_ERROR\n", GCPhysCmdBufBase, rc));
                    EVT_CMD_HW_ERR_T EvtCmdHwErr;
                    iommuAmdCmdHwErrorEventInit(GCPhysCmdBufBase, &EvtCmdHwErr);
                    iommuAmdCmdHwErrorEventRaise(pDevIns, &EvtCmdHwErr);

                    IOMMU_UNLOCK(pDevIns, pThisR3);
                }
            }
            else
                IOMMU_UNLOCK(pDevIns, pThisR3);
        }
        else
            IOMMU_UNLOCK(pDevIns, pThisR3);
    }

    RTMemFree(pvCmds);
    LogFlowFunc(("Command thread terminating\n"));
    return VINF_SUCCESS;
}


/**
 * Wakes up the command thread so it can respond to a state change.
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU device instance.
 * @param   pThread     The command thread.
 */
static DECLCALLBACK(int) iommuAmdR3CmdThreadWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
{
    RT_NOREF(pThread);
    Log4Func(("\n"));
    PCIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtCmdThread);
}


/**
 * @callback_method_impl{FNPCICONFIGREAD}
 */
static DECLCALLBACK(VBOXSTRICTRC) iommuAmdR3PciConfigRead(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t uAddress,
                                                          unsigned cb, uint32_t *pu32Value)
{
    /** @todo IOMMU: PCI config read stat counter. */
    VBOXSTRICTRC rcStrict = PDMDevHlpPCIConfigRead(pDevIns, pPciDev, uAddress, cb, pu32Value);
    Log3Func(("uAddress=%#x (cb=%u) -> %#x. rc=%Rrc\n", uAddress, cb, *pu32Value, VBOXSTRICTRC_VAL(rcStrict)));
    return rcStrict;
}


/**
 * Sets up the IOMMU MMIO region (usually in response to an IOMMU base address
 * register write).
 *
 * @returns VBox status code.
 * @param   pDevIns     The IOMMU instance data.
 *
 * @remarks Call this function only when the IOMMU BAR is enabled.
 */
static int iommuAmdR3MmioSetup(PPDMDEVINS pDevIns)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    Assert(pThis->IommuBar.n.u1Enable);
    Assert(pThis->hMmio != NIL_IOMMMIOHANDLE);     /* Paranoia. Ensure we have a valid IOM MMIO handle. */
    Assert(!pThis->ExtFeat.n.u1PerfCounterSup);    /* Base is 16K aligned when performance counters aren't supported. */
    RTGCPHYS const GCPhysMmioBase     = RT_MAKE_U64(pThis->IommuBar.au32[0] & 0xffffc000, pThis->IommuBar.au32[1]);
    RTGCPHYS const GCPhysMmioBasePrev = PDMDevHlpMmioGetMappingAddress(pDevIns, pThis->hMmio);

    /* If the MMIO region is already mapped at the specified address, we're done. */
    Assert(GCPhysMmioBase != NIL_RTGCPHYS);
    if (GCPhysMmioBasePrev == GCPhysMmioBase)
        return VINF_SUCCESS;

    /* Unmap the previous MMIO region (which is at a different address). */
    if (GCPhysMmioBasePrev != NIL_RTGCPHYS)
    {
        LogFlowFunc(("Unmapping previous MMIO region at %#RGp\n", GCPhysMmioBasePrev));
        int rc = PDMDevHlpMmioUnmap(pDevIns, pThis->hMmio);
        if (RT_FAILURE(rc))
        {
            LogFunc(("Failed to unmap MMIO region at %#RGp. rc=%Rrc\n", GCPhysMmioBasePrev, rc));
            return rc;
        }
    }

    /* Map the newly specified MMIO region. */
    LogFlowFunc(("Mapping MMIO region at %#RGp\n", GCPhysMmioBase));
    int rc = PDMDevHlpMmioMap(pDevIns, pThis->hMmio, GCPhysMmioBase);
    if (RT_FAILURE(rc))
    {
        LogFunc(("Failed to unmap MMIO region at %#RGp. rc=%Rrc\n", GCPhysMmioBase, rc));
        return rc;
    }

    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNPCICONFIGWRITE}
 */
static DECLCALLBACK(VBOXSTRICTRC) iommuAmdR3PciConfigWrite(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t uAddress,
                                                           unsigned cb, uint32_t u32Value)
{
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);

    /*
     * Discard writes to read-only registers that are specific to the IOMMU.
     * Other common PCI registers are handled by the generic code, see devpciR3IsConfigByteWritable().
     * See PCI spec. 6.1. "Configuration Space Organization".
     */
    switch (uAddress)
    {
        case IOMMU_PCI_OFF_CAP_HDR:         /* All bits are read-only. */
        case IOMMU_PCI_OFF_RANGE_REG:       /* We don't have any devices integrated with the IOMMU. */
        case IOMMU_PCI_OFF_MISCINFO_REG_0:  /* We don't support MSI-X. */
        case IOMMU_PCI_OFF_MISCINFO_REG_1:  /* We don't support guest-address translation. */
        {
            LogFunc(("PCI config write (%#RX32) to read-only register %#x -> Ignored\n", u32Value, uAddress));
            return VINF_SUCCESS;
        }
    }

    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    IOMMU_LOCK(pDevIns, pThisR3);

    VBOXSTRICTRC rcStrict;
    switch (uAddress)
    {
        case IOMMU_PCI_OFF_BASE_ADDR_REG_LO:
        {
            if (!pThis->IommuBar.n.u1Enable)
            {
                pThis->IommuBar.au32[0] = u32Value & IOMMU_BAR_VALID_MASK;
                if (pThis->IommuBar.n.u1Enable)
                    rcStrict = iommuAmdR3MmioSetup(pDevIns);
                else
                    rcStrict = VINF_SUCCESS;
            }
            else
            {
                LogFunc(("Writing Base Address (Lo) when it's already enabled -> Ignored\n"));
                rcStrict = VINF_SUCCESS;
            }
            break;
        }

        case IOMMU_PCI_OFF_BASE_ADDR_REG_HI:
        {
            if (!pThis->IommuBar.n.u1Enable)
            {
                AssertCompile((IOMMU_BAR_VALID_MASK >> 32) == 0xffffffff);
                pThis->IommuBar.au32[1] = u32Value;
            }
            else
                LogFunc(("Writing Base Address (Hi) when it's already enabled -> Ignored\n"));
            rcStrict = VINF_SUCCESS;
            break;
        }

        case IOMMU_PCI_OFF_MSI_CAP_HDR:
        {
            u32Value |= RT_BIT(23);     /* 64-bit MSI addressess must always be enabled for IOMMU. */
            RT_FALL_THRU();
        }
        default:
        {
            rcStrict = PDMDevHlpPCIConfigWrite(pDevIns, pPciDev, uAddress, cb, u32Value);
            break;
        }
    }

    IOMMU_UNLOCK(pDevIns, pThisR3);

    Log3Func(("uAddress=%#x (cb=%u) with %#x. rc=%Rrc\n", uAddress, cb, u32Value, VBOXSTRICTRC_VAL(rcStrict)));
    return rcStrict;
}


/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) iommuAmdR3DbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    PCIOMMU    pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PCPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);

    bool const fVerbose = RTStrCmp(pszArgs, "verbose") == 0;

    pHlp->pfnPrintf(pHlp, "AMD-IOMMU:\n");
    /* Device Table Base Addresses (all segments). */
    for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDevTabBaseAddrs); i++)
    {
        DEV_TAB_BAR_T const DevTabBar = pThis->aDevTabBaseAddrs[i];
        pHlp->pfnPrintf(pHlp, "  Device Table BAR %u                      = %#RX64\n", i, DevTabBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Size                                    = %#x (%u bytes)\n", DevTabBar.n.u9Size,
                            IOMMU_GET_DEV_TAB_LEN(&DevTabBar));
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            DevTabBar.n.u40Base << X86_PAGE_4K_SHIFT);
        }
    }
    /* Command Buffer Base Address Register. */
    {
        CMD_BUF_BAR_T const CmdBufBar = pThis->CmdBufBaseAddr;
        uint8_t const  uEncodedLen = CmdBufBar.n.u4Len;
        uint32_t const cEntries    = iommuAmdGetBufMaxEntries(uEncodedLen);
        uint32_t const cbBuffer    = iommuAmdGetTotalBufLength(uEncodedLen);
        pHlp->pfnPrintf(pHlp, "  Command Buffer BAR                      = %#RX64\n", CmdBufBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            CmdBufBar.n.u40Base << X86_PAGE_4K_SHIFT);
            pHlp->pfnPrintf(pHlp, "    Length                                  = %u (%u entries, %u bytes)\n", uEncodedLen,
                            cEntries, cbBuffer);
        }
    }
    /* Event Log Base Address Register. */
    {
        EVT_LOG_BAR_T const EvtLogBar = pThis->EvtLogBaseAddr;
        uint8_t const  uEncodedLen = EvtLogBar.n.u4Len;
        uint32_t const cEntries    = iommuAmdGetBufMaxEntries(uEncodedLen);
        uint32_t const cbBuffer    = iommuAmdGetTotalBufLength(uEncodedLen);
        pHlp->pfnPrintf(pHlp, "  Event Log BAR                           = %#RX64\n", EvtLogBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            EvtLogBar.n.u40Base << X86_PAGE_4K_SHIFT);
            pHlp->pfnPrintf(pHlp, "    Length                                  = %u (%u entries, %u bytes)\n", uEncodedLen,
                            cEntries, cbBuffer);
        }
    }
    /* IOMMU Control Register. */
    {
        IOMMU_CTRL_T const Ctrl = pThis->Ctrl;
        pHlp->pfnPrintf(pHlp, "  Control                                 = %#RX64\n", Ctrl.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    IOMMU enable                            = %RTbool\n", Ctrl.n.u1IommuEn);
            pHlp->pfnPrintf(pHlp, "    HT Tunnel translation enable            = %RTbool\n", Ctrl.n.u1HtTunEn);
            pHlp->pfnPrintf(pHlp, "    Event log enable                        = %RTbool\n", Ctrl.n.u1EvtLogEn);
            pHlp->pfnPrintf(pHlp, "    Event log interrupt enable              = %RTbool\n", Ctrl.n.u1EvtIntrEn);
            pHlp->pfnPrintf(pHlp, "    Completion wait interrupt enable        = %RTbool\n", Ctrl.n.u1EvtIntrEn);
            pHlp->pfnPrintf(pHlp, "    Invalidation timeout                    = %u\n",      Ctrl.n.u3InvTimeOut);
            pHlp->pfnPrintf(pHlp, "    Pass posted write                       = %RTbool\n", Ctrl.n.u1PassPW);
            pHlp->pfnPrintf(pHlp, "    Respose Pass posted write               = %RTbool\n", Ctrl.n.u1ResPassPW);
            pHlp->pfnPrintf(pHlp, "    Coherent                                = %RTbool\n", Ctrl.n.u1Coherent);
            pHlp->pfnPrintf(pHlp, "    Isochronous                             = %RTbool\n", Ctrl.n.u1Isoc);
            pHlp->pfnPrintf(pHlp, "    Command buffer enable                   = %RTbool\n", Ctrl.n.u1CmdBufEn);
            pHlp->pfnPrintf(pHlp, "    PPR log enable                          = %RTbool\n", Ctrl.n.u1PprLogEn);
            pHlp->pfnPrintf(pHlp, "    PPR interrupt enable                    = %RTbool\n", Ctrl.n.u1PprIntrEn);
            pHlp->pfnPrintf(pHlp, "    PPR enable                              = %RTbool\n", Ctrl.n.u1PprEn);
            pHlp->pfnPrintf(pHlp, "    Guest translation eanble                = %RTbool\n", Ctrl.n.u1GstTranslateEn);
            pHlp->pfnPrintf(pHlp, "    Guest virtual-APIC enable               = %RTbool\n", Ctrl.n.u1GstVirtApicEn);
            pHlp->pfnPrintf(pHlp, "    CRW                                     = %#x\n",     Ctrl.n.u4Crw);
            pHlp->pfnPrintf(pHlp, "    SMI filter enable                       = %RTbool\n", Ctrl.n.u1SmiFilterEn);
            pHlp->pfnPrintf(pHlp, "    Self-writeback disable                  = %RTbool\n", Ctrl.n.u1SelfWriteBackDis);
            pHlp->pfnPrintf(pHlp, "    SMI filter log enable                   = %RTbool\n", Ctrl.n.u1SmiFilterLogEn);
            pHlp->pfnPrintf(pHlp, "    Guest virtual-APIC mode enable          = %#x\n",     Ctrl.n.u3GstVirtApicModeEn);
            pHlp->pfnPrintf(pHlp, "    Guest virtual-APIC GA log enable        = %RTbool\n", Ctrl.n.u1GstLogEn);
            pHlp->pfnPrintf(pHlp, "    Guest virtual-APIC interrupt enable     = %RTbool\n", Ctrl.n.u1GstIntrEn);
            pHlp->pfnPrintf(pHlp, "    Dual PPR log enable                     = %#x\n",     Ctrl.n.u2DualPprLogEn);
            pHlp->pfnPrintf(pHlp, "    Dual event log enable                   = %#x\n",     Ctrl.n.u2DualEvtLogEn);
            pHlp->pfnPrintf(pHlp, "    Device table segmentation enable        = %#x\n",     Ctrl.n.u3DevTabSegEn);
            pHlp->pfnPrintf(pHlp, "    Privilege abort enable                  = %#x\n",     Ctrl.n.u2PrivAbortEn);
            pHlp->pfnPrintf(pHlp, "    PPR auto response enable                = %RTbool\n", Ctrl.n.u1PprAutoRespEn);
            pHlp->pfnPrintf(pHlp, "    MARC enable                             = %RTbool\n", Ctrl.n.u1MarcEn);
            pHlp->pfnPrintf(pHlp, "    Block StopMark enable                   = %RTbool\n", Ctrl.n.u1BlockStopMarkEn);
            pHlp->pfnPrintf(pHlp, "    PPR auto response always-on enable      = %RTbool\n", Ctrl.n.u1PprAutoRespAlwaysOnEn);
            pHlp->pfnPrintf(pHlp, "    Domain IDPNE                            = %RTbool\n", Ctrl.n.u1DomainIDPNE);
            pHlp->pfnPrintf(pHlp, "    Enhanced PPR handling                   = %RTbool\n", Ctrl.n.u1EnhancedPpr);
            pHlp->pfnPrintf(pHlp, "    Host page table access/dirty bit update = %#x\n",     Ctrl.n.u2HstAccDirtyBitUpdate);
            pHlp->pfnPrintf(pHlp, "    Guest page table dirty bit disable      = %RTbool\n", Ctrl.n.u1GstDirtyUpdateDis);
            pHlp->pfnPrintf(pHlp, "    x2APIC enable                           = %RTbool\n", Ctrl.n.u1X2ApicEn);
            pHlp->pfnPrintf(pHlp, "    x2APIC interrupt enable                 = %RTbool\n", Ctrl.n.u1X2ApicIntrGenEn);
            pHlp->pfnPrintf(pHlp, "    Guest page table access bit update      = %RTbool\n", Ctrl.n.u1GstAccessUpdateDis);
        }
    }
    /* Exclusion Base Address Register. */
    {
        IOMMU_EXCL_RANGE_BAR_T const ExclRangeBar = pThis->ExclRangeBaseAddr;
        pHlp->pfnPrintf(pHlp, "  Exclusion BAR                           = %#RX64\n", ExclRangeBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Exclusion enable                        = %RTbool\n", ExclRangeBar.n.u1ExclEnable);
            pHlp->pfnPrintf(pHlp, "    Allow all devices                       = %RTbool\n", ExclRangeBar.n.u1AllowAll);
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            ExclRangeBar.n.u40ExclRangeBase << X86_PAGE_4K_SHIFT);
        }
    }
    /* Exclusion Range Limit Register. */
    {
        IOMMU_EXCL_RANGE_LIMIT_T const ExclRangeLimit = pThis->ExclRangeLimit;
        pHlp->pfnPrintf(pHlp, "  Exclusion Range Limit                   = %#RX64\n", ExclRangeLimit.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Range limit                             = %#RX64\n",
                            (ExclRangeLimit.n.u40ExclRangeLimit << X86_PAGE_4K_SHIFT) | X86_PAGE_4K_OFFSET_MASK);
        }
    }
    /* Extended Feature Register. */
    {
        IOMMU_EXT_FEAT_T ExtFeat = pThis->ExtFeat;
        pHlp->pfnPrintf(pHlp, "  Extended Feature Register               = %#RX64\n", ExtFeat.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Prefetch support                        = %RTbool\n",  ExtFeat.n.u1PrefetchSup);
            pHlp->pfnPrintf(pHlp, "    PPR support                             = %RTbool\n",  ExtFeat.n.u1PprSup);
            pHlp->pfnPrintf(pHlp, "    x2APIC support                          = %RTbool\n",  ExtFeat.n.u1X2ApicSup);
            pHlp->pfnPrintf(pHlp, "    NX and privilege level support          = %RTbool\n",  ExtFeat.n.u1NoExecuteSup);
            pHlp->pfnPrintf(pHlp, "    Guest translation support               = %RTbool\n",  ExtFeat.n.u1GstTranslateSup);
            pHlp->pfnPrintf(pHlp, "    Invalidate-All command support          = %RTbool\n",  ExtFeat.n.u1InvAllSup);
            pHlp->pfnPrintf(pHlp, "    Guest virtual-APIC support              = %RTbool\n",  ExtFeat.n.u1GstVirtApicSup);
            pHlp->pfnPrintf(pHlp, "    Hardware error register support         = %RTbool\n",  ExtFeat.n.u1HwErrorSup);
            pHlp->pfnPrintf(pHlp, "    Performance counters support            = %RTbool\n",  ExtFeat.n.u1PerfCounterSup);
            pHlp->pfnPrintf(pHlp, "    Host address translation size           = %#x\n",      ExtFeat.n.u2HostAddrTranslateSize);
            pHlp->pfnPrintf(pHlp, "    Guest address translation size          = %#x\n",      ExtFeat.n.u2GstAddrTranslateSize);
            pHlp->pfnPrintf(pHlp, "    Guest CR3 root table level support      = %#x\n",      ExtFeat.n.u2GstCr3RootTblLevel);
            pHlp->pfnPrintf(pHlp, "    SMI filter register support             = %#x\n",      ExtFeat.n.u2SmiFilterSup);
            pHlp->pfnPrintf(pHlp, "    SMI filter register count               = %#x\n",      ExtFeat.n.u3SmiFilterCount);
            pHlp->pfnPrintf(pHlp, "    Guest virtual-APIC modes support        = %#x\n",      ExtFeat.n.u3GstVirtApicModeSup);
            pHlp->pfnPrintf(pHlp, "    Dual PPR log support                    = %#x\n",      ExtFeat.n.u2DualPprLogSup);
            pHlp->pfnPrintf(pHlp, "    Dual event log support                  = %#x\n",      ExtFeat.n.u2DualEvtLogSup);
            pHlp->pfnPrintf(pHlp, "    Maximum PASID                           = %#x\n",      ExtFeat.n.u5MaxPasidSup);
            pHlp->pfnPrintf(pHlp, "    User/supervisor page protection support = %RTbool\n",  ExtFeat.n.u1UserSupervisorSup);
            pHlp->pfnPrintf(pHlp, "    Device table segments supported         = %#x (%u)\n", ExtFeat.n.u2DevTabSegSup,
                            g_acDevTabSegs[ExtFeat.n.u2DevTabSegSup]);
            pHlp->pfnPrintf(pHlp, "    PPR log overflow early warning support  = %RTbool\n",  ExtFeat.n.u1PprLogOverflowWarn);
            pHlp->pfnPrintf(pHlp, "    PPR auto response support               = %RTbool\n",  ExtFeat.n.u1PprAutoRespSup);
            pHlp->pfnPrintf(pHlp, "    MARC support                            = %#x\n",      ExtFeat.n.u2MarcSup);
            pHlp->pfnPrintf(pHlp, "    Block StopMark message support          = %RTbool\n",  ExtFeat.n.u1BlockStopMarkSup);
            pHlp->pfnPrintf(pHlp, "    Performance optimization support        = %RTbool\n",  ExtFeat.n.u1PerfOptSup);
            pHlp->pfnPrintf(pHlp, "    MSI capability MMIO access support      = %RTbool\n",  ExtFeat.n.u1MsiCapMmioSup);
            pHlp->pfnPrintf(pHlp, "    Guest I/O protection support            = %RTbool\n",  ExtFeat.n.u1GstIoSup);
            pHlp->pfnPrintf(pHlp, "    Host access support                     = %RTbool\n",  ExtFeat.n.u1HostAccessSup);
            pHlp->pfnPrintf(pHlp, "    Enhanced PPR handling support           = %RTbool\n",  ExtFeat.n.u1EnhancedPprSup);
            pHlp->pfnPrintf(pHlp, "    Attribute forward supported             = %RTbool\n",  ExtFeat.n.u1AttrForwardSup);
            pHlp->pfnPrintf(pHlp, "    Host dirty support                      = %RTbool\n",  ExtFeat.n.u1HostDirtySup);
            pHlp->pfnPrintf(pHlp, "    Invalidate IOTLB type support           = %RTbool\n",  ExtFeat.n.u1InvIoTlbTypeSup);
            pHlp->pfnPrintf(pHlp, "    Guest page table access bit hw disable  = %RTbool\n",  ExtFeat.n.u1GstUpdateDisSup);
            pHlp->pfnPrintf(pHlp, "    Force physical dest for remapped intr.  = %RTbool\n",  ExtFeat.n.u1ForcePhysDstSup);
        }
    }
    /* PPR Log Base Address Register. */
    {
        PPR_LOG_BAR_T PprLogBar = pThis->PprLogBaseAddr;
        uint8_t const  uEncodedLen = PprLogBar.n.u4Len;
        uint32_t const cEntries    = iommuAmdGetBufMaxEntries(uEncodedLen);
        uint32_t const cbBuffer    = iommuAmdGetTotalBufLength(uEncodedLen);
        pHlp->pfnPrintf(pHlp, "  PPR Log BAR                             = %#RX64\n",   PprLogBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            PprLogBar.n.u40Base << X86_PAGE_4K_SHIFT);
            pHlp->pfnPrintf(pHlp, "    Length                                  = %u (%u entries, %u bytes)\n", uEncodedLen,
                            cEntries, cbBuffer);
        }
    }
    /* Hardware Event (Hi) Register. */
    {
        IOMMU_HW_EVT_HI_T HwEvtHi = pThis->HwEvtHi;
        pHlp->pfnPrintf(pHlp, "  Hardware Event (Hi)                     = %#RX64\n",   HwEvtHi.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    First operand                           = %#RX64\n", HwEvtHi.n.u60FirstOperand);
            pHlp->pfnPrintf(pHlp, "    Event code                              = %#RX8\n",  HwEvtHi.n.u4EvtCode);
        }
    }
    /* Hardware Event (Lo) Register. */
    pHlp->pfnPrintf(pHlp, "  Hardware Event (Lo)                     = %#RX64\n", pThis->HwEvtLo);
    /* Hardware Event Status. */
    {
        IOMMU_HW_EVT_STATUS_T HwEvtStatus = pThis->HwEvtStatus;
        pHlp->pfnPrintf(pHlp, "  Hardware Event Status                   = %#RX64\n",    HwEvtStatus.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Valid                                   = %RTbool\n", HwEvtStatus.n.u1Valid);
            pHlp->pfnPrintf(pHlp, "    Overflow                                = %RTbool\n", HwEvtStatus.n.u1Overflow);
        }
    }
    /* Guest Virtual-APIC Log Base Address Register. */
    {
        GALOG_BAR_T const GALogBar = pThis->GALogBaseAddr;
        uint8_t const  uEncodedLen = GALogBar.n.u4Len;
        uint32_t const cEntries    = iommuAmdGetBufMaxEntries(uEncodedLen);
        uint32_t const cbBuffer    = iommuAmdGetTotalBufLength(uEncodedLen);
        pHlp->pfnPrintf(pHlp, "  Guest Log BAR                           = %#RX64\n",    GALogBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            GALogBar.n.u40Base << X86_PAGE_4K_SHIFT);
            pHlp->pfnPrintf(pHlp, "    Length                                  = %u (%u entries, %u bytes)\n", uEncodedLen,
                            cEntries, cbBuffer);
        }
    }
    /* Guest Virtual-APIC Log Tail Address Register. */
    {
        GALOG_TAIL_ADDR_T GALogTail = pThis->GALogTailAddr;
        pHlp->pfnPrintf(pHlp, "  Guest Log Tail Address                  = %#RX64\n",   GALogTail.u64);
        if (fVerbose)
            pHlp->pfnPrintf(pHlp, "    Tail address                            = %#RX64\n", GALogTail.n.u40GALogTailAddr);
    }
    /* PPR Log B Base Address Register. */
    {
        PPR_LOG_B_BAR_T PprLogBBar = pThis->PprLogBBaseAddr;
        uint8_t const uEncodedLen  = PprLogBBar.n.u4Len;
        uint32_t const cEntries    = iommuAmdGetBufMaxEntries(uEncodedLen);
        uint32_t const cbBuffer    = iommuAmdGetTotalBufLength(uEncodedLen);
        pHlp->pfnPrintf(pHlp, "  PPR Log B BAR                           = %#RX64\n",   PprLogBBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            PprLogBBar.n.u40Base << X86_PAGE_4K_SHIFT);
            pHlp->pfnPrintf(pHlp, "    Length                                  = %u (%u entries, %u bytes)\n", uEncodedLen,
                            cEntries, cbBuffer);
        }
    }
    /* Event Log B Base Address Register. */
    {
        EVT_LOG_B_BAR_T EvtLogBBar = pThis->EvtLogBBaseAddr;
        uint8_t const  uEncodedLen = EvtLogBBar.n.u4Len;
        uint32_t const cEntries    = iommuAmdGetBufMaxEntries(uEncodedLen);
        uint32_t const cbBuffer    = iommuAmdGetTotalBufLength(uEncodedLen);
        pHlp->pfnPrintf(pHlp, "  Event Log B BAR                         = %#RX64\n",   EvtLogBBar.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Base address                            = %#RX64\n",
                            EvtLogBBar.n.u40Base << X86_PAGE_4K_SHIFT);
            pHlp->pfnPrintf(pHlp, "    Length                                  = %u (%u entries, %u bytes)\n", uEncodedLen,
                            cEntries, cbBuffer);
        }
    }
    /* Device-Specific Feature Extension Register. */
    {
        DEV_SPECIFIC_FEAT_T const DevSpecificFeat = pThis->DevSpecificFeat;
        pHlp->pfnPrintf(pHlp, "  Device-specific Feature                 = %#RX64\n",   DevSpecificFeat.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Feature                                 = %#RX32\n", DevSpecificFeat.n.u24DevSpecFeat);
            pHlp->pfnPrintf(pHlp, "    Minor revision ID                       = %#x\n",    DevSpecificFeat.n.u4RevMinor);
            pHlp->pfnPrintf(pHlp, "    Major revision ID                       = %#x\n",    DevSpecificFeat.n.u4RevMajor);
        }
    }
    /* Device-Specific Control Extension Register. */
    {
        DEV_SPECIFIC_CTRL_T const DevSpecificCtrl = pThis->DevSpecificCtrl;
        pHlp->pfnPrintf(pHlp, "  Device-specific Control                 = %#RX64\n",   DevSpecificCtrl.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Control                                 = %#RX32\n", DevSpecificCtrl.n.u24DevSpecCtrl);
            pHlp->pfnPrintf(pHlp, "    Minor revision ID                       = %#x\n",    DevSpecificCtrl.n.u4RevMinor);
            pHlp->pfnPrintf(pHlp, "    Major revision ID                       = %#x\n",    DevSpecificCtrl.n.u4RevMajor);
        }
    }
    /* Device-Specific Status Extension Register. */
    {
        DEV_SPECIFIC_STATUS_T const DevSpecificStatus = pThis->DevSpecificStatus;
        pHlp->pfnPrintf(pHlp, "  Device-specific Status                  = %#RX64\n",   DevSpecificStatus.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Status                                  = %#RX32\n", DevSpecificStatus.n.u24DevSpecStatus);
            pHlp->pfnPrintf(pHlp, "    Minor revision ID                       = %#x\n",    DevSpecificStatus.n.u4RevMinor);
            pHlp->pfnPrintf(pHlp, "    Major revision ID                       = %#x\n",    DevSpecificStatus.n.u4RevMajor);
        }
    }
    /* Miscellaneous Information Register (Lo and Hi). */
    {
        MSI_MISC_INFO_T const MiscInfo = pThis->MiscInfo;
        pHlp->pfnPrintf(pHlp, "  Misc. Info. Register                    = %#RX64\n",    MiscInfo.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Event Log MSI number                    = %#x\n",     MiscInfo.n.u5MsiNumEvtLog);
            pHlp->pfnPrintf(pHlp, "    Guest Virtual-Address Size              = %#x\n",     MiscInfo.n.u3GstVirtAddrSize);
            pHlp->pfnPrintf(pHlp, "    Physical Address Size                   = %#x\n",     MiscInfo.n.u7PhysAddrSize);
            pHlp->pfnPrintf(pHlp, "    Virtual-Address Size                    = %#x\n",     MiscInfo.n.u7VirtAddrSize);
            pHlp->pfnPrintf(pHlp, "    HT Transport ATS Range Reserved         = %RTbool\n", MiscInfo.n.u1HtAtsResv);
            pHlp->pfnPrintf(pHlp, "    PPR MSI number                          = %#x\n",     MiscInfo.n.u5MsiNumPpr);
            pHlp->pfnPrintf(pHlp, "    GA Log MSI number                       = %#x\n",     MiscInfo.n.u5MsiNumGa);
        }
    }
    /* MSI Capability Header. */
    {
        MSI_CAP_HDR_T MsiCapHdr;
        MsiCapHdr.u32 = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR);
        pHlp->pfnPrintf(pHlp, "  MSI Capability Header                   = %#RX32\n",    MsiCapHdr.u32);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Capability ID                           = %#x\n",     MsiCapHdr.n.u8MsiCapId);
            pHlp->pfnPrintf(pHlp, "    Capability Ptr (PCI config offset)      = %#x\n",     MsiCapHdr.n.u8MsiCapPtr);
            pHlp->pfnPrintf(pHlp, "    Enable                                  = %RTbool\n", MsiCapHdr.n.u1MsiEnable);
            pHlp->pfnPrintf(pHlp, "    Multi-message capability                = %#x\n",     MsiCapHdr.n.u3MsiMultiMessCap);
            pHlp->pfnPrintf(pHlp, "    Multi-message enable                    = %#x\n",     MsiCapHdr.n.u3MsiMultiMessEn);
        }
    }
    /* MSI Address Register (Lo and Hi). */
    {
        uint32_t const uMsiAddrLo = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO);
        uint32_t const uMsiAddrHi = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI);
        MSIADDR MsiAddr;
        MsiAddr.u64 = RT_MAKE_U64(uMsiAddrLo, uMsiAddrHi);
        pHlp->pfnPrintf(pHlp, "  MSI Address                             = %#RX64\n",   MsiAddr.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Destination mode                        = %#x\n",    MsiAddr.n.u1DestMode);
            pHlp->pfnPrintf(pHlp, "    Redirection hint                        = %#x\n",    MsiAddr.n.u1RedirHint);
            pHlp->pfnPrintf(pHlp, "    Destination Id                          = %#x\n",    MsiAddr.n.u8DestId);
            pHlp->pfnPrintf(pHlp, "    Address                                 = %#RX32\n", MsiAddr.n.u12Addr);
            pHlp->pfnPrintf(pHlp, "    Address (Hi) / Rsvd?                    = %#RX32\n", MsiAddr.n.u32Rsvd0);
        }
    }
    /* MSI Data. */
    {
        MSIDATA MsiData;
        MsiData.u32 = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA);
        pHlp->pfnPrintf(pHlp, "  MSI Data                                = %#RX32\n", MsiData.u32);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Vector                                  = %#x (%u)\n", MsiData.n.u8Vector,
                            MsiData.n.u8Vector);
            pHlp->pfnPrintf(pHlp, "    Delivery mode                           = %#x\n", MsiData.n.u3DeliveryMode);
            pHlp->pfnPrintf(pHlp, "    Level                                   = %#x\n", MsiData.n.u1Level);
            pHlp->pfnPrintf(pHlp, "    Trigger mode                            = %s\n",  MsiData.n.u1TriggerMode ?
                                                                                         "level" : "edge");
        }
    }
    /* MSI Mapping Capability Header (HyperTransport, reporting all 0s currently). */
    {
        MSI_MAP_CAP_HDR_T MsiMapCapHdr;
        MsiMapCapHdr.u32 = 0;
        pHlp->pfnPrintf(pHlp, "  MSI Mapping Capability Header           = %#RX32\n",    MsiMapCapHdr.u32);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Capability ID                           = %#x\n",     MsiMapCapHdr.n.u8MsiMapCapId);
            pHlp->pfnPrintf(pHlp, "    Map enable                              = %RTbool\n", MsiMapCapHdr.n.u1MsiMapEn);
            pHlp->pfnPrintf(pHlp, "    Map fixed                               = %RTbool\n", MsiMapCapHdr.n.u1MsiMapFixed);
            pHlp->pfnPrintf(pHlp, "    Map capability type                     = %#x\n",     MsiMapCapHdr.n.u5MapCapType);
        }
    }
    /* Performance Optimization Control Register. */
    {
        IOMMU_PERF_OPT_CTRL_T const PerfOptCtrl = pThis->PerfOptCtrl;
        pHlp->pfnPrintf(pHlp, "  Performance Optimization Control        = %#RX32\n",    PerfOptCtrl.u32);
        if (fVerbose)
            pHlp->pfnPrintf(pHlp, "    Enable                                  = %RTbool\n", PerfOptCtrl.n.u1PerfOptEn);
    }
    /* XT (x2APIC) General Interrupt Control Register. */
    {
        IOMMU_XT_GEN_INTR_CTRL_T const XtGenIntrCtrl = pThis->XtGenIntrCtrl;
        pHlp->pfnPrintf(pHlp, "  XT General Interrupt Control            = %#RX64\n", XtGenIntrCtrl.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Interrupt destination mode              = %s\n",
                            !XtGenIntrCtrl.n.u1X2ApicIntrDstMode ? "physical" : "logical");
            pHlp->pfnPrintf(pHlp, "    Interrupt destination                   = %#RX64\n",
                            RT_MAKE_U64(XtGenIntrCtrl.n.u24X2ApicIntrDstLo, XtGenIntrCtrl.n.u7X2ApicIntrDstHi));
            pHlp->pfnPrintf(pHlp, "    Interrupt vector                        = %#x\n", XtGenIntrCtrl.n.u8X2ApicIntrVector);
            pHlp->pfnPrintf(pHlp, "    Interrupt delivery mode                 = %s\n",
                            !XtGenIntrCtrl.n.u8X2ApicIntrVector ? "fixed" : "arbitrated");
        }
    }
    /* XT (x2APIC) PPR Interrupt Control Register. */
    {
        IOMMU_XT_PPR_INTR_CTRL_T const XtPprIntrCtrl = pThis->XtPprIntrCtrl;
        pHlp->pfnPrintf(pHlp, "  XT PPR Interrupt Control                = %#RX64\n", XtPprIntrCtrl.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "   Interrupt destination mode               = %s\n",
                            !XtPprIntrCtrl.n.u1X2ApicIntrDstMode ? "physical" : "logical");
            pHlp->pfnPrintf(pHlp, "   Interrupt destination                    = %#RX64\n",
                            RT_MAKE_U64(XtPprIntrCtrl.n.u24X2ApicIntrDstLo, XtPprIntrCtrl.n.u7X2ApicIntrDstHi));
            pHlp->pfnPrintf(pHlp, "   Interrupt vector                         = %#x\n", XtPprIntrCtrl.n.u8X2ApicIntrVector);
            pHlp->pfnPrintf(pHlp, "   Interrupt delivery mode                  = %s\n",
                            !XtPprIntrCtrl.n.u8X2ApicIntrVector ? "fixed" : "arbitrated");
        }
    }
    /* XT (X2APIC) GA Log Interrupt Control Register. */
    {
        IOMMU_XT_GALOG_INTR_CTRL_T const XtGALogIntrCtrl = pThis->XtGALogIntrCtrl;
        pHlp->pfnPrintf(pHlp, "  XT PPR Interrupt Control                = %#RX64\n", XtGALogIntrCtrl.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Interrupt destination mode              = %s\n",
                            !XtGALogIntrCtrl.n.u1X2ApicIntrDstMode ? "physical" : "logical");
            pHlp->pfnPrintf(pHlp, "    Interrupt destination                   = %#RX64\n",
                            RT_MAKE_U64(XtGALogIntrCtrl.n.u24X2ApicIntrDstLo, XtGALogIntrCtrl.n.u7X2ApicIntrDstHi));
            pHlp->pfnPrintf(pHlp, "    Interrupt vector                        = %#x\n", XtGALogIntrCtrl.n.u8X2ApicIntrVector);
            pHlp->pfnPrintf(pHlp, "    Interrupt delivery mode                 = %s\n",
                            !XtGALogIntrCtrl.n.u8X2ApicIntrVector ? "fixed" : "arbitrated");
        }
    }
    /* MARC Registers. */
    {
        for (unsigned i = 0; i < RT_ELEMENTS(pThis->aMarcApers); i++)
        {
            pHlp->pfnPrintf(pHlp, " MARC Aperature %u:\n", i);
            MARC_APER_BAR_T const MarcAperBar = pThis->aMarcApers[i].Base;
            pHlp->pfnPrintf(pHlp, "   Base    = %#RX64\n", MarcAperBar.n.u40MarcBaseAddr << X86_PAGE_4K_SHIFT);

            MARC_APER_RELOC_T const MarcAperReloc = pThis->aMarcApers[i].Reloc;
            pHlp->pfnPrintf(pHlp, "   Reloc   = %#RX64 (addr: %#RX64, read-only: %RTbool, enable: %RTbool)\n",
                            MarcAperReloc.u64, MarcAperReloc.n.u40MarcRelocAddr << X86_PAGE_4K_SHIFT,
                            MarcAperReloc.n.u1ReadOnly, MarcAperReloc.n.u1RelocEn);

            MARC_APER_LEN_T const MarcAperLen = pThis->aMarcApers[i].Length;
            pHlp->pfnPrintf(pHlp, "   Length  = %u pages\n", MarcAperLen.n.u40MarcLength);
        }
    }
    /* Reserved Register. */
    pHlp->pfnPrintf(pHlp, "  Reserved Register                       = %#RX64\n", pThis->RsvdReg);
    /* Command Buffer Head Pointer Register. */
    {
        CMD_BUF_HEAD_PTR_T const CmdBufHeadPtr = pThis->CmdBufHeadPtr;
        pHlp->pfnPrintf(pHlp, "  Command Buffer Head Pointer             = %#RX64 (off: %#x)\n", CmdBufHeadPtr.u64,
                        CmdBufHeadPtr.n.off);
    }
    /* Command Buffer Tail Pointer Register. */
    {
        CMD_BUF_HEAD_PTR_T const CmdBufTailPtr = pThis->CmdBufTailPtr;
        pHlp->pfnPrintf(pHlp, "  Command Buffer Tail Pointer             = %#RX64 (off: %#x)\n", CmdBufTailPtr.u64,
                        CmdBufTailPtr.n.off);
    }
    /* Event Log Head Pointer Register. */
    {
        EVT_LOG_HEAD_PTR_T const EvtLogHeadPtr = pThis->EvtLogHeadPtr;
        pHlp->pfnPrintf(pHlp, "  Event Log Head Pointer                  = %#RX64 (off: %#x)\n", EvtLogHeadPtr.u64,
                        EvtLogHeadPtr.n.off);
    }
    /* Event Log Tail Pointer Register. */
    {
        EVT_LOG_TAIL_PTR_T const EvtLogTailPtr = pThis->EvtLogTailPtr;
        pHlp->pfnPrintf(pHlp, "  Event Log Head Pointer                  = %#RX64 (off: %#x)\n", EvtLogTailPtr.u64,
                        EvtLogTailPtr.n.off);
    }
    /* Status Register. */
    {
        IOMMU_STATUS_T const Status = pThis->Status;
        pHlp->pfnPrintf(pHlp, "  Status Register                         = %#RX64\n", Status.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Event log overflow                      = %RTbool\n", Status.n.u1EvtOverflow);
            pHlp->pfnPrintf(pHlp, "    Event log interrupt                     = %RTbool\n", Status.n.u1EvtLogIntr);
            pHlp->pfnPrintf(pHlp, "    Completion wait interrupt               = %RTbool\n", Status.n.u1CompWaitIntr);
            pHlp->pfnPrintf(pHlp, "    Event log running                       = %RTbool\n", Status.n.u1EvtLogRunning);
            pHlp->pfnPrintf(pHlp, "    Command buffer running                  = %RTbool\n", Status.n.u1CmdBufRunning);
            pHlp->pfnPrintf(pHlp, "    PPR overflow                            = %RTbool\n", Status.n.u1PprOverflow);
            pHlp->pfnPrintf(pHlp, "    PPR interrupt                           = %RTbool\n", Status.n.u1PprIntr);
            pHlp->pfnPrintf(pHlp, "    PPR log running                         = %RTbool\n", Status.n.u1PprLogRunning);
            pHlp->pfnPrintf(pHlp, "    Guest log running                       = %RTbool\n", Status.n.u1GstLogRunning);
            pHlp->pfnPrintf(pHlp, "    Guest log interrupt                     = %RTbool\n", Status.n.u1GstLogIntr);
            pHlp->pfnPrintf(pHlp, "    PPR log B overflow                      = %RTbool\n", Status.n.u1PprOverflowB);
            pHlp->pfnPrintf(pHlp, "    PPR log active                          = %RTbool\n", Status.n.u1PprLogActive);
            pHlp->pfnPrintf(pHlp, "    Event log B overflow                    = %RTbool\n", Status.n.u1EvtOverflowB);
            pHlp->pfnPrintf(pHlp, "    Event log active                        = %RTbool\n", Status.n.u1EvtLogActive);
            pHlp->pfnPrintf(pHlp, "    PPR log B overflow early warning        = %RTbool\n", Status.n.u1PprOverflowEarlyB);
            pHlp->pfnPrintf(pHlp, "    PPR log overflow early warning          = %RTbool\n", Status.n.u1PprOverflowEarly);
        }
    }
    /* PPR Log Head Pointer. */
    {
        PPR_LOG_HEAD_PTR_T const PprLogHeadPtr = pThis->PprLogHeadPtr;
        pHlp->pfnPrintf(pHlp, "  PPR Log Head Pointer                    = %#RX64 (off: %#x)\n", PprLogHeadPtr.u64,
                        PprLogHeadPtr.n.off);
    }
    /* PPR Log Tail Pointer. */
    {
        PPR_LOG_TAIL_PTR_T const PprLogTailPtr = pThis->PprLogTailPtr;
        pHlp->pfnPrintf(pHlp, "  PPR Log Tail Pointer                    = %#RX64 (off: %#x)\n", PprLogTailPtr.u64,
                        PprLogTailPtr.n.off);
    }
    /* Guest Virtual-APIC Log Head Pointer. */
    {
        GALOG_HEAD_PTR_T const GALogHeadPtr = pThis->GALogHeadPtr;
        pHlp->pfnPrintf(pHlp, "  Guest Virtual-APIC Log Head Pointer     = %#RX64 (off: %#x)\n", GALogHeadPtr.u64,
                        GALogHeadPtr.n.u12GALogPtr);
    }
    /* Guest Virtual-APIC Log Tail Pointer. */
    {
        GALOG_HEAD_PTR_T const GALogTailPtr = pThis->GALogTailPtr;
        pHlp->pfnPrintf(pHlp, "  Guest Virtual-APIC Log Tail Pointer     = %#RX64 (off: %#x)\n", GALogTailPtr.u64,
                        GALogTailPtr.n.u12GALogPtr);
    }
    /* PPR Log B Head Pointer. */
    {
        PPR_LOG_B_HEAD_PTR_T const PprLogBHeadPtr = pThis->PprLogBHeadPtr;
        pHlp->pfnPrintf(pHlp, "  PPR Log B Head Pointer                  = %#RX64 (off: %#x)\n", PprLogBHeadPtr.u64,
                        PprLogBHeadPtr.n.off);
    }
    /* PPR Log B Tail Pointer. */
    {
        PPR_LOG_B_TAIL_PTR_T const PprLogBTailPtr = pThis->PprLogBTailPtr;
        pHlp->pfnPrintf(pHlp, "  PPR Log B Tail Pointer                  = %#RX64 (off: %#x)\n", PprLogBTailPtr.u64,
                        PprLogBTailPtr.n.off);
    }
    /* Event Log B Head Pointer. */
    {
        EVT_LOG_B_HEAD_PTR_T const EvtLogBHeadPtr = pThis->EvtLogBHeadPtr;
        pHlp->pfnPrintf(pHlp, "  Event Log B Head Pointer                = %#RX64 (off: %#x)\n", EvtLogBHeadPtr.u64,
                        EvtLogBHeadPtr.n.off);
    }
    /* Event Log B Tail Pointer. */
    {
        EVT_LOG_B_TAIL_PTR_T const EvtLogBTailPtr = pThis->EvtLogBTailPtr;
        pHlp->pfnPrintf(pHlp, "  Event Log B Tail Pointer                = %#RX64 (off: %#x)\n", EvtLogBTailPtr.u64,
                        EvtLogBTailPtr.n.off);
    }
    /* PPR Log Auto Response Register. */
    {
        PPR_LOG_AUTO_RESP_T const PprLogAutoResp = pThis->PprLogAutoResp;
        pHlp->pfnPrintf(pHlp, "  PPR Log Auto Response Register          = %#RX64\n",     PprLogAutoResp.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Code                                    = %#x\n",      PprLogAutoResp.n.u4AutoRespCode);
            pHlp->pfnPrintf(pHlp, "    Mask Gen.                               = %RTbool\n",  PprLogAutoResp.n.u1AutoRespMaskGen);
        }
    }
    /* PPR Log Overflow Early Warning Indicator Register. */
    {
        PPR_LOG_OVERFLOW_EARLY_T const PprLogOverflowEarly = pThis->PprLogOverflowEarly;
        pHlp->pfnPrintf(pHlp, "  PPR Log overflow early warning          = %#RX64\n",    PprLogOverflowEarly.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Threshold                               = %#x\n",     PprLogOverflowEarly.n.u15Threshold);
            pHlp->pfnPrintf(pHlp, "    Interrupt enable                        = %RTbool\n", PprLogOverflowEarly.n.u1IntrEn);
            pHlp->pfnPrintf(pHlp, "    Enable                                  = %RTbool\n", PprLogOverflowEarly.n.u1Enable);
        }
    }
    /* PPR Log Overflow Early Warning Indicator Register. */
    {
        PPR_LOG_OVERFLOW_EARLY_T const PprLogBOverflowEarly = pThis->PprLogBOverflowEarly;
        pHlp->pfnPrintf(pHlp, "  PPR Log B overflow early warning        = %#RX64\n",    PprLogBOverflowEarly.u64);
        if (fVerbose)
        {
            pHlp->pfnPrintf(pHlp, "    Threshold                               = %#x\n",     PprLogBOverflowEarly.n.u15Threshold);
            pHlp->pfnPrintf(pHlp, "    Interrupt enable                        = %RTbool\n", PprLogBOverflowEarly.n.u1IntrEn);
            pHlp->pfnPrintf(pHlp, "    Enable                                  = %RTbool\n", PprLogBOverflowEarly.n.u1Enable);
        }
    }
}


/**
 * Dumps the DTE via the info callback helper.
 *
 * @param   pHlp        The info helper.
 * @param   pDte        The device table entry.
 * @param   pszPrefix   The string prefix.
 */
static void iommuAmdR3DbgInfoDteWorker(PCDBGFINFOHLP pHlp, PCDTE_T pDte, const char *pszPrefix)
{
    AssertReturnVoid(pHlp);
    AssertReturnVoid(pDte);
    AssertReturnVoid(pszPrefix);

    pHlp->pfnPrintf(pHlp, "%sValid                      = %RTbool\n", pszPrefix, pDte->n.u1Valid);
    pHlp->pfnPrintf(pHlp, "%sTranslation Valid          = %RTbool\n", pszPrefix, pDte->n.u1TranslationValid);
    pHlp->pfnPrintf(pHlp, "%sHost Access Dirty          = %#x\n",     pszPrefix, pDte->n.u2Had);
    pHlp->pfnPrintf(pHlp, "%sPaging Mode                = %u\n",      pszPrefix, pDte->n.u3Mode);
    pHlp->pfnPrintf(pHlp, "%sPage Table Root Ptr        = %#RX64 (addr=%#RGp)\n", pszPrefix, pDte->n.u40PageTableRootPtrLo,
                    pDte->n.u40PageTableRootPtrLo << 12);
    pHlp->pfnPrintf(pHlp, "%sPPR enable                 = %RTbool\n", pszPrefix, pDte->n.u1Ppr);
    pHlp->pfnPrintf(pHlp, "%sGuest PPR Resp w/ PASID    = %RTbool\n", pszPrefix, pDte->n.u1GstPprRespPasid);
    pHlp->pfnPrintf(pHlp, "%sGuest I/O Prot Valid       = %RTbool\n", pszPrefix, pDte->n.u1GstIoValid);
    pHlp->pfnPrintf(pHlp, "%sGuest Translation Valid    = %RTbool\n", pszPrefix, pDte->n.u1GstTranslateValid);
    pHlp->pfnPrintf(pHlp, "%sGuest Levels Translated    = %#x\n",     pszPrefix, pDte->n.u2GstMode);
    pHlp->pfnPrintf(pHlp, "%sGuest Root Page Table Ptr  = %#x %#x %#x (addr=%#RGp)\n", pszPrefix,
                    pDte->n.u3GstCr3TableRootPtrLo, pDte->n.u16GstCr3TableRootPtrMid, pDte->n.u21GstCr3TableRootPtrHi,
                      (pDte->n.u21GstCr3TableRootPtrHi  << 31)
                    | (pDte->n.u16GstCr3TableRootPtrMid << 15)
                    | (pDte->n.u3GstCr3TableRootPtrLo   << 12));
    pHlp->pfnPrintf(pHlp, "%sI/O Read                   = %s\n",      pszPrefix, pDte->n.u1IoRead ? "allowed" : "denied");
    pHlp->pfnPrintf(pHlp, "%sI/O Write                  = %s\n",      pszPrefix, pDte->n.u1IoWrite ? "allowed" : "denied");
    pHlp->pfnPrintf(pHlp, "%sReserved (MBZ)             = %#x\n",     pszPrefix, pDte->n.u1Rsvd0);
    pHlp->pfnPrintf(pHlp, "%sDomain ID                  = %u (%#x)\n", pszPrefix, pDte->n.u16DomainId, pDte->n.u16DomainId);
    pHlp->pfnPrintf(pHlp, "%sIOTLB Enable               = %RTbool\n", pszPrefix, pDte->n.u1IoTlbEnable);
    pHlp->pfnPrintf(pHlp, "%sSuppress I/O PFs           = %RTbool\n", pszPrefix, pDte->n.u1SuppressPfEvents);
    pHlp->pfnPrintf(pHlp, "%sSuppress all I/O PFs       = %RTbool\n", pszPrefix, pDte->n.u1SuppressAllPfEvents);
    pHlp->pfnPrintf(pHlp, "%sPort I/O Control           = %#x\n",     pszPrefix, pDte->n.u2IoCtl);
    pHlp->pfnPrintf(pHlp, "%sIOTLB Cache Hint           = %s\n",      pszPrefix, pDte->n.u1Cache ? "no caching" : "cache");
    pHlp->pfnPrintf(pHlp, "%sSnoop Disable              = %RTbool\n", pszPrefix, pDte->n.u1SnoopDisable);
    pHlp->pfnPrintf(pHlp, "%sAllow Exclusion            = %RTbool\n", pszPrefix, pDte->n.u1AllowExclusion);
    pHlp->pfnPrintf(pHlp, "%sSysMgt Message Enable      = %RTbool\n", pszPrefix, pDte->n.u2SysMgt);
    pHlp->pfnPrintf(pHlp, "%sInterrupt Map Valid        = %RTbool\n", pszPrefix, pDte->n.u1IntrMapValid);
    uint8_t const uIntrTabLen = pDte->n.u4IntrTableLength;
    if (uIntrTabLen < IOMMU_DTE_INTR_TAB_LEN_MAX)
    {
        uint16_t const cEntries    = IOMMU_DTE_GET_INTR_TAB_ENTRIES(pDte);
        uint16_t const cbIntrTable = IOMMU_DTE_GET_INTR_TAB_LEN(pDte);
        pHlp->pfnPrintf(pHlp, "%sInterrupt Table Length     = %#x (%u entries, %u bytes)\n", pszPrefix, uIntrTabLen, cEntries,
                        cbIntrTable);
    }
    else
        pHlp->pfnPrintf(pHlp, "%sInterrupt Table Length     = %#x (invalid!)\n", pszPrefix, uIntrTabLen);
    pHlp->pfnPrintf(pHlp, "%sIgnore Unmapped Interrupts = %RTbool\n", pszPrefix, pDte->n.u1IgnoreUnmappedIntrs);
    pHlp->pfnPrintf(pHlp, "%sInterrupt Table Root Ptr   = %#RX64 (addr=%#RGp)\n", pszPrefix,
                    pDte->n.u46IntrTableRootPtr, pDte->au64[2] & IOMMU_DTE_IRTE_ROOT_PTR_MASK);
    pHlp->pfnPrintf(pHlp, "%sReserved (MBZ)             = %#x\n",     pszPrefix, pDte->n.u4Rsvd0);
    pHlp->pfnPrintf(pHlp, "%sINIT passthru              = %RTbool\n", pszPrefix, pDte->n.u1InitPassthru);
    pHlp->pfnPrintf(pHlp, "%sExtInt passthru            = %RTbool\n", pszPrefix, pDte->n.u1ExtIntPassthru);
    pHlp->pfnPrintf(pHlp, "%sNMI passthru               = %RTbool\n", pszPrefix, pDte->n.u1NmiPassthru);
    pHlp->pfnPrintf(pHlp, "%sReserved (MBZ)             = %#x\n",     pszPrefix, pDte->n.u1Rsvd2);
    pHlp->pfnPrintf(pHlp, "%sInterrupt Control          = %#x\n",     pszPrefix, pDte->n.u2IntrCtrl);
    pHlp->pfnPrintf(pHlp, "%sLINT0 passthru             = %RTbool\n", pszPrefix, pDte->n.u1Lint0Passthru);
    pHlp->pfnPrintf(pHlp, "%sLINT1 passthru             = %RTbool\n", pszPrefix, pDte->n.u1Lint1Passthru);
    pHlp->pfnPrintf(pHlp, "%sReserved (MBZ)             = %#x\n",     pszPrefix, pDte->n.u32Rsvd0);
    pHlp->pfnPrintf(pHlp, "%sReserved (MBZ)             = %#x\n",     pszPrefix, pDte->n.u22Rsvd0);
    pHlp->pfnPrintf(pHlp, "%sAttribute Override Valid   = %RTbool\n", pszPrefix, pDte->n.u1AttrOverride);
    pHlp->pfnPrintf(pHlp, "%sMode0FC                    = %#x\n",     pszPrefix, pDte->n.u1Mode0FC);
    pHlp->pfnPrintf(pHlp, "%sSnoop Attribute            = %#x\n",     pszPrefix, pDte->n.u8SnoopAttr);
    pHlp->pfnPrintf(pHlp, "\n");
}


/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) iommuAmdR3DbgInfoDte(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    if (pszArgs)
    {
        uint16_t idDevice = 0;
        int rc = RTStrToUInt16Full(pszArgs, 0 /* uBase */, &idDevice);
        if (RT_SUCCESS(rc))
        {
            DTE_T Dte;
            rc = iommuAmdDteRead(pDevIns, idDevice, IOMMUOP_TRANSLATE_REQ,  &Dte);
            if (RT_SUCCESS(rc))
            {
                pHlp->pfnPrintf(pHlp, "DTE for device %#x\n", idDevice);
                iommuAmdR3DbgInfoDteWorker(pHlp, &Dte, " ");
                return;
            }
            pHlp->pfnPrintf(pHlp, "Failed to read DTE for device ID %u (%#x). rc=%Rrc\n", idDevice, idDevice, rc);
        }
        else
            pHlp->pfnPrintf(pHlp, "Failed to parse a valid 16-bit device ID. rc=%Rrc\n", rc);
    }
    else
        pHlp->pfnPrintf(pHlp, "Missing device ID.\n");
}


# ifdef IOMMU_WITH_DTE_CACHE
/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) iommuAmdR3DbgInfoDteCache(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    RT_NOREF(pszArgs);
    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    uint16_t const cDteCache = RT_ELEMENTS(pThis->aDeviceIds);
    pHlp->pfnPrintf(pHlp, "DTE Cache: Capacity=%u entries\n", cDteCache);
    for (uint16_t i = 0; i < cDteCache; i++)
    {
        uint16_t const idDevice = pThis->aDeviceIds[i];
        if (idDevice)
        {
            pHlp->pfnPrintf(pHlp, " Entry[%u]: Device=%#x (BDF %02x:%02x.%d)\n", i, idDevice,
                            (idDevice >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK,
                            (idDevice >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK,
                            idDevice & VBOX_PCI_DEVFN_FUN_MASK);

            PCDTECACHE pDteCache = &pThis->aDteCache[i];
            pHlp->pfnPrintf(pHlp, "  Flags            = %#x\n", pDteCache->fFlags);
            pHlp->pfnPrintf(pHlp, "  Domain Id        = %u\n",  pDteCache->idDomain);
            pHlp->pfnPrintf(pHlp, "\n");
        }
    }
    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}
# endif /* IOMMU_WITH_DTE_CACHE */


# ifdef IOMMU_WITH_IOTLBE_CACHE
/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) iommuAmdR3DbgInfoIotlb(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    if (pszArgs)
    {
        uint16_t idDomain = 0;
        int rc = RTStrToUInt16Full(pszArgs, 0 /* uBase */, &idDomain);
        if (RT_SUCCESS(rc))
        {
            pHlp->pfnPrintf(pHlp, "IOTLBEs for domain %u (%#x):\n", idDomain, idDomain);
            PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
            PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
            IOTLBEINFOARG Args;
            Args.pIommuR3 = pThisR3;
            Args.pHlp     = pHlp;
            Args.idDomain = idDomain;

            IOMMU_CACHE_LOCK(pDevIns, pThis);
            RTAvlU64DoWithAll(&pThisR3->TreeIotlbe, true /* fFromLeft */, iommuAmdR3IotlbEntryInfo, &Args);
            IOMMU_CACHE_UNLOCK(pDevIns, pThis);
        }
        else
            pHlp->pfnPrintf(pHlp, "Failed to parse a valid 16-bit domain ID. rc=%Rrc\n", rc);
    }
    else
        pHlp->pfnPrintf(pHlp, "Missing domain ID.\n");
}
# endif  /* IOMMU_WITH_IOTLBE_CACHE */


# ifdef IOMMU_WITH_IRTE_CACHE
/**
 * Gets the interrupt type name for an interrupt type in the IRTE.
 *
 * @returns The interrupt type name.
 * @param   uIntrType       The interrupt type (as specified in the IRTE).
 */
static const char *iommuAmdIrteGetIntrTypeName(uint8_t uIntrType)
{
    switch (uIntrType)
    {
        case VBOX_MSI_DELIVERY_MODE_FIXED:          return "Fixed";
        case VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO:    return "Arbitrated";
        default:                                    return "<Reserved>";
    }
}


/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) iommuAmdR3DbgInfoIrteCache(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    RT_NOREF(pszArgs);

    PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    IOMMU_CACHE_LOCK(pDevIns, pThis);

    uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
    pHlp->pfnPrintf(pHlp, "IRTE Cache: Capacity=%u entries\n", cIrteCache);
    for (uint16_t idxIrte = 0; idxIrte < cIrteCache; idxIrte++)
    {
        PCIRTECACHE pIrteCache = &pThis->aIrteCache[idxIrte];
        uint32_t const uKey = pIrteCache->uKey;
        if (uKey != IOMMU_IRTE_CACHE_KEY_NIL)
        {
            uint16_t const idDevice = IOMMU_IRTE_CACHE_KEY_GET_DEVICE_ID(uKey);
            uint16_t const offIrte  = IOMMU_IRTE_CACHE_KEY_GET_OFF(uKey);
            pHlp->pfnPrintf(pHlp, " Entry[%u]: Offset=%#x Device=%#x (BDF %02x:%02x.%d)\n",
                            idxIrte, offIrte, idDevice,
                            (idDevice >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK,
                            (idDevice >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK,
                            idDevice & VBOX_PCI_DEVFN_FUN_MASK);

            PCIRTE_T pIrte = &pIrteCache->Irte;
            pHlp->pfnPrintf(pHlp, "  Remap Enable     = %RTbool\n", pIrte->n.u1RemapEnable);
            pHlp->pfnPrintf(pHlp, "  Suppress IOPF    = %RTbool\n", pIrte->n.u1SuppressIoPf);
            pHlp->pfnPrintf(pHlp, "  Interrupt Type   = %#x (%s)\n", pIrte->n.u3IntrType,
                            iommuAmdIrteGetIntrTypeName(pIrte->n.u3IntrType));
            pHlp->pfnPrintf(pHlp, "  Request EOI      = %RTbool\n", pIrte->n.u1ReqEoi);
            pHlp->pfnPrintf(pHlp, "  Destination mode = %s\n", pIrte->n.u1DestMode ? "Logical" : "Physical");
            pHlp->pfnPrintf(pHlp, "  Destination Id   = %u\n", pIrte->n.u8Dest);
            pHlp->pfnPrintf(pHlp, "  Vector           = %#x (%u)\n", pIrte->n.u8Vector, pIrte->n.u8Vector);
            pHlp->pfnPrintf(pHlp, "\n");
        }
    }
    IOMMU_CACHE_UNLOCK(pDevIns, pThis);
}
# endif /* IOMMU_WITH_IRTE_CACHE */


/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) iommuAmdR3DbgInfoDevTabs(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    RT_NOREF(pszArgs);

    PCIOMMU    pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PCPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
    NOREF(pPciDev);

    uint8_t cSegments = 0;
    for (uint8_t i = 0; i < RT_ELEMENTS(pThis->aDevTabBaseAddrs); i++)
    {
        DEV_TAB_BAR_T const DevTabBar = pThis->aDevTabBaseAddrs[i];
        RTGCPHYS const GCPhysDevTab   = DevTabBar.n.u40Base << X86_PAGE_4K_SHIFT;
        if (GCPhysDevTab)
            ++cSegments;
    }

    pHlp->pfnPrintf(pHlp, "AMD-IOMMU device tables with address translations enabled:\n");
    pHlp->pfnPrintf(pHlp, " DTE Segments=%u\n", cSegments);
    if (!cSegments)
        return;

    for (uint8_t i = 0; i < RT_ELEMENTS(pThis->aDevTabBaseAddrs); i++)
    {
        DEV_TAB_BAR_T const DevTabBar = pThis->aDevTabBaseAddrs[i];
        RTGCPHYS const GCPhysDevTab   = DevTabBar.n.u40Base << X86_PAGE_4K_SHIFT;
        if (GCPhysDevTab)
        {
            uint32_t const cbDevTab = IOMMU_GET_DEV_TAB_LEN(&DevTabBar);
            uint32_t const cDtes    = cbDevTab / sizeof(DTE_T);

            void *pvDevTab = RTMemAllocZ(cbDevTab);
            if (RT_LIKELY(pvDevTab))
            {
                int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysDevTab, pvDevTab, cbDevTab);
                if (RT_SUCCESS(rc))
                {
                    for (uint32_t idxDte = 0; idxDte < cDtes; idxDte++)
                    {
                        PCDTE_T pDte = (PCDTE_T)((uintptr_t)pvDevTab + idxDte * sizeof(DTE_T));
                        if (   pDte->n.u1Valid
                            && pDte->n.u1TranslationValid
                            && pDte->n.u3Mode != 0)
                        {
                            pHlp->pfnPrintf(pHlp, " DTE %u (BDF %02x:%02x.%d)\n", idxDte,
                                            (idxDte >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK,
                                            (idxDte >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK,
                                            idxDte & VBOX_PCI_DEVFN_FUN_MASK);
                            iommuAmdR3DbgInfoDteWorker(pHlp, pDte, " ");
                            pHlp->pfnPrintf(pHlp, "\n");
                        }
                    }
                    pHlp->pfnPrintf(pHlp, "\n");
                }
                else
                {
                    pHlp->pfnPrintf(pHlp, " Failed to read table at %#RGp of size %zu bytes. rc=%Rrc!\n", GCPhysDevTab,
                                    cbDevTab, rc);
                }

                RTMemFree(pvDevTab);
            }
            else
            {
                pHlp->pfnPrintf(pHlp, " Allocating %zu bytes for reading the device table failed!\n", cbDevTab);
                return;
            }
        }
    }
}


/**
 * @callback_method_impl{FNSSMDEVSAVEEXEC}
 */
static DECLCALLBACK(int) iommuAmdR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    PCIOMMU       pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PCPDMDEVHLPR3 pHlp  = pDevIns->pHlpR3;
    LogFlowFunc(("\n"));

    /* First, save ExtFeat and other registers that cannot be modified by the guest. */
    pHlp->pfnSSMPutU64(pSSM, pThis->ExtFeat.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificFeat.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificCtrl.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificStatus.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->MiscInfo.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->RsvdReg);

    /* Next, save all registers that can be modified by the guest. */
    pHlp->pfnSSMPutU64(pSSM, pThis->IommuBar.u64);

    uint8_t const cDevTabBaseAddrs = RT_ELEMENTS(pThis->aDevTabBaseAddrs);
    pHlp->pfnSSMPutU8(pSSM, cDevTabBaseAddrs);
    for (uint8_t i = 0; i < cDevTabBaseAddrs; i++)
        pHlp->pfnSSMPutU64(pSSM, pThis->aDevTabBaseAddrs[i].u64);

    AssertReturn(pThis->CmdBufBaseAddr.n.u4Len >= 8, VERR_IOMMU_IPE_4);
    pHlp->pfnSSMPutU64(pSSM, pThis->CmdBufBaseAddr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBaseAddr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->Ctrl.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->ExclRangeBaseAddr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->ExclRangeLimit.u64);
#if 0
    pHlp->pfnSSMPutU64(pSSM, pThis->ExtFeat.u64);  /* read-only, done already (above). */
#endif

    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBaseAddr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->HwEvtHi.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->HwEvtLo);
    pHlp->pfnSSMPutU64(pSSM, pThis->HwEvtStatus.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->GALogBaseAddr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->GALogTailAddr.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBBaseAddr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBBaseAddr.u64);

#if 0
    pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificFeat.u64);       /* read-only, done already (above). */
    pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificCtrl.u64);       /* read-only, done already (above). */
    pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificStatus.u64);     /* read-only, done already (above). */

    pHlp->pfnSSMPutU64(pSSM, pThis->MiscInfo.u64);              /* read-only, done already (above). */
#endif
    pHlp->pfnSSMPutU32(pSSM, pThis->PerfOptCtrl.u32);

    pHlp->pfnSSMPutU64(pSSM, pThis->XtGenIntrCtrl.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->XtPprIntrCtrl.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->XtGALogIntrCtrl.u64);

    size_t const cMarcApers = RT_ELEMENTS(pThis->aMarcApers);
    pHlp->pfnSSMPutU8(pSSM, cMarcApers);
    for (size_t i = 0; i < cMarcApers; i++)
    {
        pHlp->pfnSSMPutU64(pSSM, pThis->aMarcApers[i].Base.u64);
        pHlp->pfnSSMPutU64(pSSM, pThis->aMarcApers[i].Reloc.u64);
        pHlp->pfnSSMPutU64(pSSM, pThis->aMarcApers[i].Length.u64);
    }

#if 0
    pHlp->pfnSSMPutU64(pSSM, pThis->RsvdReg);       /* read-only, done already (above). */
#endif

    pHlp->pfnSSMPutU64(pSSM, pThis->CmdBufHeadPtr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->CmdBufTailPtr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogHeadPtr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogTailPtr.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->Status.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogHeadPtr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogTailPtr.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->GALogHeadPtr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->GALogTailPtr.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBHeadPtr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBTailPtr.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBHeadPtr.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBTailPtr.u64);

    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogAutoResp.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogOverflowEarly.u64);
    pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBOverflowEarly.u64);

    return pHlp->pfnSSMPutU32(pSSM, UINT32_MAX);
}


/**
 * @callback_method_impl{FNSSMDEVLOADEXEC}
 */
static DECLCALLBACK(int) iommuAmdR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
    PIOMMU        pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PCPDMDEVHLPR3 pHlp  = pDevIns->pHlpR3;
    int const     rcErr = VERR_SSM_UNEXPECTED_DATA;
    LogFlowFunc(("\n"));

    /* Validate. */
    AssertReturn(uPass == SSM_PASS_FINAL, VERR_WRONG_ORDER);
    if (uVersion != IOMMU_SAVED_STATE_VERSION)
    {
        LogRel(("%s: Invalid saved-state version %#x\n", IOMMU_LOG_PFX, uVersion));
        return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
    }

    /* Load ExtFeat and other read-only registers first. */
    int rc = pHlp->pfnSSMGetU64(pSSM, &pThis->ExtFeat.u64);
    AssertRCReturn(rc, rc);
    AssertLogRelMsgReturn(pThis->ExtFeat.n.u2HostAddrTranslateSize < 0x3,
                          ("ExtFeat.HATS register invalid %#RX64\n", pThis->ExtFeat.u64), rcErr);
    pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificFeat.u64);
    pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificCtrl.u64);
    pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificStatus.u64);
    pHlp->pfnSSMGetU64(pSSM, &pThis->MiscInfo.u64);
    pHlp->pfnSSMGetU64(pSSM, &pThis->RsvdReg);

    /* IOMMU base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->IommuBar.u64);
    AssertRCReturn(rc, rc);
    pThis->IommuBar.u64 &= IOMMU_BAR_VALID_MASK;

    /* Device table base address registers. */
    uint8_t cDevTabBaseAddrs;
    rc = pHlp->pfnSSMGetU8(pSSM, &cDevTabBaseAddrs);
    AssertRCReturn(rc, rc);
    AssertLogRelMsgReturn(cDevTabBaseAddrs > 0 && cDevTabBaseAddrs <= RT_ELEMENTS(pThis->aDevTabBaseAddrs),
                          ("Device table segment count invalid %#x\n", cDevTabBaseAddrs), rcErr);
    AssertCompile(RT_ELEMENTS(pThis->aDevTabBaseAddrs) == RT_ELEMENTS(g_auDevTabSegMaxSizes));
    for (uint8_t i = 0; i < cDevTabBaseAddrs; i++)
    {
        rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aDevTabBaseAddrs[i].u64);
        AssertRCReturn(rc, rc);
        pThis->aDevTabBaseAddrs[i].u64 &= IOMMU_DEV_TAB_BAR_VALID_MASK;
        uint16_t const uSegSize    = pThis->aDevTabBaseAddrs[i].n.u9Size;
        uint16_t const uMaxSegSize = g_auDevTabSegMaxSizes[i];
        AssertLogRelMsgReturn(uSegSize <= uMaxSegSize,
                              ("Device table [%u] segment size invalid %u (max %u)\n", i, uSegSize, uMaxSegSize), rcErr);
    }

    /* Command buffer base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->CmdBufBaseAddr.u64);
    AssertRCReturn(rc, rc);
    pThis->CmdBufBaseAddr.u64 &= IOMMU_CMD_BUF_BAR_VALID_MASK;
    AssertLogRelMsgReturn(pThis->CmdBufBaseAddr.n.u4Len >= 8,
                          ("Command buffer base address invalid %#RX64\n", pThis->CmdBufBaseAddr.u64), rcErr);

    /* Event log base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBaseAddr.u64);
    AssertRCReturn(rc, rc);
    pThis->EvtLogBaseAddr.u64 &= IOMMU_EVT_LOG_BAR_VALID_MASK;
    AssertLogRelMsgReturn(pThis->EvtLogBaseAddr.n.u4Len >= 8,
                          ("Event log base address invalid %#RX64\n", pThis->EvtLogBaseAddr.u64), rcErr);

    /* Control register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->Ctrl.u64);
    AssertRCReturn(rc, rc);
    pThis->Ctrl.u64 &= IOMMU_CTRL_VALID_MASK;
    AssertLogRelMsgReturn(pThis->Ctrl.n.u3DevTabSegEn <= pThis->ExtFeat.n.u2DevTabSegSup,
                          ("Control register invalid %#RX64\n", pThis->Ctrl.u64), rcErr);

    /* Exclusion range base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->ExclRangeBaseAddr.u64);
    AssertRCReturn(rc, rc);
    pThis->ExclRangeBaseAddr.u64 &= IOMMU_EXCL_RANGE_BAR_VALID_MASK;

    /* Exclusion range limit register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->ExclRangeLimit.u64);
    AssertRCReturn(rc, rc);
    pThis->ExclRangeLimit.u64 &= IOMMU_EXCL_RANGE_LIMIT_VALID_MASK;
    pThis->ExclRangeLimit.u64 |= UINT64_C(0xfff);

#if 0
    pHlp->pfnSSMGetU64(pSSM, &pThis->ExtFeat.u64);  /* read-only, done already (above). */
#endif

    /* PPR log base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBaseAddr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprSup);

    /* Hardware event (Hi) register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->HwEvtHi.u64);
    AssertRCReturn(rc, rc);

    /* Hardware event (Lo) register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->HwEvtLo);
    AssertRCReturn(rc, rc);

    /* Hardware event status register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->HwEvtStatus.u64);
    AssertRCReturn(rc, rc);
    pThis->HwEvtStatus.u64 &= IOMMU_HW_EVT_STATUS_VALID_MASK;

    /* Guest Virtual-APIC log base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogBaseAddr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);

    /* Guest Virtual-APIC log tail address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogTailAddr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);

    /* PPR log-B base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBBaseAddr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprSup);

    /* Event log-B base address register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBBaseAddr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u2DualPprLogSup);

#if 0
    pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificFeat.u64);       /* read-only, done already (above). */
    pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificCtrl.u64);       /* read-only, done already (above). */
    pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificStatus.u64);     /* read-only, done already (above). */

    pHlp->pfnSSMGetU64(pSSM, &pThis->MiscInfo.u64);              /* read-only, done already (above). */
#endif

    /* Performance optimization control register. */
    rc = pHlp->pfnSSMGetU32(pSSM, &pThis->PerfOptCtrl.u32);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PerfOptSup);

    /* x2APIC registers. */
    {
        Assert(!pThis->ExtFeat.n.u1X2ApicSup);

        /* x2APIC general interrupt control register. */
        pHlp->pfnSSMGetU64(pSSM, &pThis->XtGenIntrCtrl.u64);
        AssertRCReturn(rc, rc);

        /* x2APIC PPR interrupt control register. */
        rc = pHlp->pfnSSMGetU64(pSSM, &pThis->XtPprIntrCtrl.u64);
        AssertRCReturn(rc, rc);

        /* x2APIC GA log interrupt control register. */
        rc = pHlp->pfnSSMGetU64(pSSM, &pThis->XtGALogIntrCtrl.u64);
        AssertRCReturn(rc, rc);
    }

    /* MARC (Memory Access and Routing) registers. */
    {
        uint8_t cMarcApers;
        rc = pHlp->pfnSSMGetU8(pSSM, &cMarcApers);
        AssertRCReturn(rc, rc);
        AssertLogRelMsgReturn(cMarcApers > 0 && cMarcApers <= RT_ELEMENTS(pThis->aMarcApers),
                              ("MARC register count invalid %#x\n", cMarcApers), rcErr);
        for (uint8_t i = 0; i < cMarcApers; i++)
        {
            rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aMarcApers[i].Base.u64);
            AssertRCReturn(rc, rc);

            rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aMarcApers[i].Reloc.u64);
            AssertRCReturn(rc, rc);

            rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aMarcApers[i].Length.u64);
            AssertRCReturn(rc, rc);
        }
        Assert(!pThis->ExtFeat.n.u2MarcSup);
    }

#if 0
    pHlp->pfnSSMGetU64(pSSM, &pThis->RsvdReg);  /* read-only, done already (above). */
#endif

    /* Command buffer head pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->CmdBufHeadPtr.u64);
    AssertRCReturn(rc, rc);
    {
        /*
         * IOMMU behavior is undefined when software writes a value outside the buffer length.
         * In our emulation, since we ignore the write entirely (see iommuAmdCmdBufHeadPtr_w)
         * we shouldn't see such values in the saved state.
         */
        uint32_t const offBuf = pThis->CmdBufHeadPtr.u64 & IOMMU_CMD_BUF_HEAD_PTR_VALID_MASK;
        uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
        Assert(cbBuf <= _512K);
        AssertLogRelMsgReturn(offBuf < cbBuf,
                              ("Command buffer head pointer invalid %#x\n", pThis->CmdBufHeadPtr.u64), rcErr);
    }

    /* Command buffer tail pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->CmdBufTailPtr.u64);
    AssertRCReturn(rc, rc);
    {
        uint32_t const offBuf = pThis->CmdBufTailPtr.u64 & IOMMU_CMD_BUF_TAIL_PTR_VALID_MASK;
        uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
        Assert(cbBuf <= _512K);
        AssertLogRelMsgReturn(offBuf < cbBuf,
                              ("Command buffer tail pointer invalid %#x\n", pThis->CmdBufTailPtr.u64), rcErr);
    }

    /* Event log head pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogHeadPtr.u64);
    AssertRCReturn(rc, rc);
    {
        uint32_t const offBuf = pThis->EvtLogHeadPtr.u64 & IOMMU_EVT_LOG_HEAD_PTR_VALID_MASK;
        uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
        Assert(cbBuf <= _512K);
        AssertLogRelMsgReturn(offBuf < cbBuf,
                              ("Event log head pointer invalid %#x\n", pThis->EvtLogHeadPtr.u64), rcErr);
    }

    /* Event log tail pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogTailPtr.u64);
    AssertRCReturn(rc, rc);
    {
        uint32_t const offBuf = pThis->EvtLogTailPtr.u64 & IOMMU_EVT_LOG_TAIL_PTR_VALID_MASK;
        uint32_t const cbBuf  = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
        Assert(cbBuf <= _512K);
        AssertLogRelMsgReturn(offBuf < cbBuf,
                              ("Event log tail pointer invalid %#x\n", pThis->EvtLogTailPtr.u64), rcErr);
    }

    /* Status register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->Status.u64);
    AssertRCReturn(rc, rc);
    pThis->Status.u64 &= IOMMU_STATUS_VALID_MASK;

    /* PPR log head pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogHeadPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprSup);

    /* PPR log tail pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogTailPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprSup);

    /* Guest Virtual-APIC log head pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogHeadPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);

    /* Guest Virtual-APIC log tail pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogTailPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);

    /* PPR log-B head pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBHeadPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprSup);

    /* PPR log-B head pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBTailPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprSup);

    /* Event log-B head pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBHeadPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u2DualEvtLogSup);

    /* Event log-B tail pointer register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBTailPtr.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u2DualEvtLogSup);

    /* PPR log auto response register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogAutoResp.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprAutoRespSup);

    /* PPR log overflow early indicator register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogOverflowEarly.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprLogOverflowWarn);

    /* PPR log-B overflow early indicator register. */
    rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBOverflowEarly.u64);
    AssertRCReturn(rc, rc);
    Assert(!pThis->ExtFeat.n.u1PprLogOverflowWarn);

    /* End marker. */
    {
        uint32_t uEndMarker;
        rc = pHlp->pfnSSMGetU32(pSSM, &uEndMarker);
        AssertLogRelMsgRCReturn(rc, ("Failed to read end marker. rc=%Rrc\n", rc), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
        AssertLogRelMsgReturn(uEndMarker == UINT32_MAX, ("End marker invalid (%#x expected %#x)\n", uEndMarker, UINT32_MAX),
                              rcErr);
    }

    return rc;
}


/**
 * @callback_method_impl{FNSSMDEVLOADDONE}
 */
static DECLCALLBACK(int) iommuAmdR3LoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    RT_NOREF(pSSM);
    LogFlowFunc(("\n"));

    /* Sanity. */
    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
    AssertPtrReturn(pThisR3, VERR_INVALID_POINTER);

    int rc;
    IOMMU_LOCK(pDevIns, pThisR3);

    /* Map MMIO regions if the IOMMU BAR is enabled. */
    if (pThis->IommuBar.n.u1Enable)
        rc = iommuAmdR3MmioSetup(pDevIns);
    else
        rc = VINF_SUCCESS;

    /* Wake up the command thread if commands need processing. */
    iommuAmdCmdThreadWakeUpIfNeeded(pDevIns);

    IOMMU_UNLOCK(pDevIns, pThisR3);

    LogRel(("%s: Restored: DSFX=%u.%u DSCX=%u.%u DSSX=%u.%u ExtFeat=%#RX64\n", IOMMU_LOG_PFX,
            pThis->DevSpecificFeat.n.u4RevMajor, pThis->DevSpecificFeat.n.u4RevMinor,
            pThis->DevSpecificCtrl.n.u4RevMajor, pThis->DevSpecificCtrl.n.u4RevMinor,
            pThis->DevSpecificStatus.n.u4RevMajor, pThis->DevSpecificStatus.n.u4RevMinor,
            pThis->ExtFeat.u64));
    return rc;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnReset}
 */
static DECLCALLBACK(void) iommuAmdR3Reset(PPDMDEVINS pDevIns)
{
    /*
     * Resets read-write portion of the IOMMU state.
     *
     * NOTE! State not initialized here is expected to be initialized during
     * device construction and remain read-only through the lifetime of the VM.
     */
    PIOMMU     pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3   pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
    LogFlowFunc(("\n"));

    IOMMU_LOCK(pDevIns, pThisR3);

    RT_ZERO(pThis->aDevTabBaseAddrs);

    pThis->CmdBufBaseAddr.u64        = 0;
    pThis->CmdBufBaseAddr.n.u4Len    = 8;

    pThis->EvtLogBaseAddr.u64        = 0;
    pThis->EvtLogBaseAddr.n.u4Len    = 8;

    pThis->Ctrl.u64                  = 0;
    pThis->Ctrl.n.u1Coherent         = 1;
    Assert(!pThis->ExtFeat.n.u1BlockStopMarkSup);

    pThis->ExclRangeBaseAddr.u64     = 0;
    pThis->ExclRangeLimit.u64        = 0;

    pThis->PprLogBaseAddr.u64        = 0;
    pThis->PprLogBaseAddr.n.u4Len    = 8;

    pThis->HwEvtHi.u64               = 0;
    pThis->HwEvtLo                   = 0;
    pThis->HwEvtStatus.u64           = 0;

    pThis->GALogBaseAddr.u64         = 0;
    pThis->GALogBaseAddr.n.u4Len     = 8;
    pThis->GALogTailAddr.u64         = 0;

    pThis->PprLogBBaseAddr.u64       = 0;
    pThis->PprLogBBaseAddr.n.u4Len   = 8;

    pThis->EvtLogBBaseAddr.u64       = 0;
    pThis->EvtLogBBaseAddr.n.u4Len   = 8;

    pThis->PerfOptCtrl.u32           = 0;

    pThis->XtGenIntrCtrl.u64         = 0;
    pThis->XtPprIntrCtrl.u64         = 0;
    pThis->XtGALogIntrCtrl.u64       = 0;

    RT_ZERO(pThis->aMarcApers);

    pThis->CmdBufHeadPtr.u64         = 0;
    pThis->CmdBufTailPtr.u64         = 0;
    pThis->EvtLogHeadPtr.u64         = 0;
    pThis->EvtLogTailPtr.u64         = 0;

    pThis->Status.u64                = 0;

    pThis->PprLogHeadPtr.u64         = 0;
    pThis->PprLogTailPtr.u64         = 0;

    pThis->GALogHeadPtr.u64          = 0;
    pThis->GALogTailPtr.u64          = 0;

    pThis->PprLogBHeadPtr.u64        = 0;
    pThis->PprLogBTailPtr.u64        = 0;

    pThis->EvtLogBHeadPtr.u64        = 0;
    pThis->EvtLogBTailPtr.u64        = 0;

    pThis->PprLogAutoResp.u64        = 0;
    pThis->PprLogOverflowEarly.u64   = 0;
    pThis->PprLogBOverflowEarly.u64  = 0;

    pThis->IommuBar.u64              = 0;
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_LO, 0);
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_HI, 0);

    PDMPciDevSetCommand(pPciDev, VBOX_PCI_COMMAND_MASTER);

    IOMMU_UNLOCK(pDevIns, pThisR3);

#ifdef IOMMU_WITH_DTE_CACHE
    iommuAmdDteCacheRemoveAll(pDevIns);
#endif
#ifdef IOMMU_WITH_IOTLBE_CACHE
    iommuAmdIotlbRemoveAll(pDevIns);
#endif
#ifdef IOMMU_WITH_IRTE_CACHE
    iommuAmdIrteCacheRemoveAll(pDevIns);
#endif
}


/**
 * @interface_method_impl{PDMDEVREG,pfnDestruct}
 */
static DECLCALLBACK(int) iommuAmdR3Destruct(PPDMDEVINS pDevIns)
{
    PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    LogFlowFunc(("\n"));

    IOMMU_LOCK(pDevIns, pThisR3);

    if (pThis->hEvtCmdThread != NIL_SUPSEMEVENT)
    {
        PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEvtCmdThread);
        pThis->hEvtCmdThread = NIL_SUPSEMEVENT;
    }

#ifdef IOMMU_WITH_IOTLBE_CACHE
    if (pThisR3->paIotlbes)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThisR3->paIotlbes);
        pThisR3->paIotlbes = NULL;
        pThisR3->idxUnusedIotlbe = 0;
    }
#endif

    IOMMU_UNLOCK(pDevIns, pThisR3);
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnConstruct}
 */
static DECLCALLBACK(int) iommuAmdR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
    PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);

    PIOMMU        pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUR3      pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
    PCPDMDEVHLPR3 pHlp    = pDevIns->pHlpR3;

    pThis->u32Magic = IOMMU_MAGIC;
    pThisR3->pDevInsR3 = pDevIns;

    LogFlowFunc(("iInstance=%d\n", iInstance));

    /*
     * Validate and read the configuration.
     */
    PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "PCIAddress", "");
    int rc = pHlp->pfnCFGMQueryU32Def(pCfg, "PCIAddress", &pThis->uPciAddress, NIL_PCIBDF);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to query 32-bit integer \"PCIAddress\""));
    if (!PCIBDF_IS_VALID(pThis->uPciAddress))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed \"PCIAddress\" of the AMD IOMMU cannot be invalid"));

    /*
     * Register the IOMMU with PDM.
     */
    PDMIOMMUREGR3 IommuReg;
    RT_ZERO(IommuReg);
    IommuReg.u32Version       = PDM_IOMMUREGCC_VERSION;
    IommuReg.pfnMemAccess     = iommuAmdMemAccess;
    IommuReg.pfnMemBulkAccess = iommuAmdMemBulkAccess;
    IommuReg.pfnMsiRemap      = iommuAmdMsiRemap;
    IommuReg.u32TheEnd        = PDM_IOMMUREGCC_VERSION;
    rc = PDMDevHlpIommuRegister(pDevIns, &IommuReg, &pThisR3->CTX_SUFF(pIommuHlp), &pThis->idxIommu);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to register ourselves as an IOMMU device"));
    if (pThisR3->CTX_SUFF(pIommuHlp)->u32Version != PDM_IOMMUHLPR3_VERSION)
        return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
                                   N_("IOMMU helper version mismatch; got %#x expected %#x"),
                                   pThisR3->CTX_SUFF(pIommuHlp)->u32Version, PDM_IOMMUHLPR3_VERSION);
    if (pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd != PDM_IOMMUHLPR3_VERSION)
        return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
                                   N_("IOMMU helper end-version mismatch; got %#x expected %#x"),
                                   pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd, PDM_IOMMUHLPR3_VERSION);
    AssertPtr(pThisR3->pIommuHlpR3->pfnLock);
    AssertPtr(pThisR3->pIommuHlpR3->pfnUnlock);
    AssertPtr(pThisR3->pIommuHlpR3->pfnLockIsOwner);
    AssertPtr(pThisR3->pIommuHlpR3->pfnSendMsi);

    /*
     * We will use PDM's critical section (via helpers) for the IOMMU device.
     */
    rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
    AssertRCReturn(rc, rc);

    /*
     * Initialize read-only PCI configuration space.
     */
    PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
    PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);

    /* Header. */
    PDMPciDevSetVendorId(pPciDev,          IOMMU_PCI_VENDOR_ID);       /* AMD */
    PDMPciDevSetDeviceId(pPciDev,          IOMMU_PCI_DEVICE_ID);       /* VirtualBox IOMMU device */
    PDMPciDevSetCommand(pPciDev,           VBOX_PCI_COMMAND_MASTER);   /* Enable bus master (as we directly access main memory) */
    PDMPciDevSetStatus(pPciDev,            VBOX_PCI_STATUS_CAP_LIST);  /* Capability list supported */
    PDMPciDevSetRevisionId(pPciDev,        IOMMU_PCI_REVISION_ID);     /* VirtualBox specific device implementation revision */
    PDMPciDevSetClassBase(pPciDev,         VBOX_PCI_CLASS_SYSTEM);     /* System Base Peripheral */
    PDMPciDevSetClassSub(pPciDev,          VBOX_PCI_SUB_SYSTEM_IOMMU); /* IOMMU */
    PDMPciDevSetClassProg(pPciDev,         0x0);                       /* IOMMU Programming interface */
    PDMPciDevSetHeaderType(pPciDev,        0x0);                       /* Single function, type 0 */
    PDMPciDevSetSubSystemId(pPciDev,       IOMMU_PCI_DEVICE_ID);       /* AMD */
    PDMPciDevSetSubSystemVendorId(pPciDev, IOMMU_PCI_VENDOR_ID);       /* VirtualBox IOMMU device */
    PDMPciDevSetCapabilityList(pPciDev,    IOMMU_PCI_OFF_CAP_HDR);     /* Offset into capability registers */
    PDMPciDevSetInterruptPin(pPciDev,      0x1);                       /* INTA#. */
    PDMPciDevSetInterruptLine(pPciDev,     0x0);                       /* For software compatibility; no effect on hardware */

    /* Capability Header. */
    /* NOTE! Fields (e.g, EFR) must match what we expose in the ACPI tables. */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_CAP_HDR,
                               RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_ID,    0xf)     /* RO - Secure Device capability block */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_PTR,   IOMMU_PCI_OFF_MSI_CAP_HDR)  /* RO - Next capability offset */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_TYPE,  0x3)     /* RO - IOMMU capability block */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_REV,   0x1)     /* RO - IOMMU interface revision */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_IOTLB_SUP, 0x0)     /* RO - Remote IOTLB support */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_HT_TUNNEL, 0x0)     /* RO - HyperTransport Tunnel support */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_NP_CACHE,  0x0)     /* RO - Cache NP page table entries */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_EFR_SUP,   0x1)     /* RO - Extended Feature Register support */
                             | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_EXT,   0x1));   /* RO - Misc. Information Register support */

    /* Base Address Register. */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_LO, 0x0);   /* RW - Base address (Lo) and enable bit */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_HI, 0x0);   /* RW - Base address (Hi) */

    /* IOMMU Range Register. */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_RANGE_REG, 0x0);          /* RW - Range register (implemented as RO by us) */

    /* Misc. Information Register. */
    /* NOTE! Fields (e.g, GVA size) must match what we expose in the ACPI tables. */
    uint32_t const  uMiscInfoReg0 = RT_BF_MAKE(IOMMU_BF_MISCINFO_0_MSI_NUM,      0)   /* RO - MSI number */
                                  | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_GVA_SIZE,     2)   /* RO - Guest Virt. Addr size (2=48 bits) */
                                  | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_PA_SIZE,     48)   /* RO - Physical Addr size (48 bits) */
                                  | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_VA_SIZE,     64)   /* RO - Virt. Addr size (64 bits) */
                                  | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_HT_ATS_RESV,  0)   /* RW - HT ATS reserved */
                                  | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_MSI_NUM_PPR,  0);  /* RW - PPR interrupt number */
    uint32_t const uMiscInfoReg1  = 0;
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MISCINFO_REG_0, uMiscInfoReg0);
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MISCINFO_REG_1, uMiscInfoReg1);

    /* MSI Capability Header register. */
    PDMMSIREG MsiReg;
    RT_ZERO(MsiReg);
    MsiReg.cMsiVectors    = 1;
    MsiReg.iMsiCapOffset  = IOMMU_PCI_OFF_MSI_CAP_HDR;
    MsiReg.iMsiNextOffset = 0; /* IOMMU_PCI_OFF_MSI_MAP_CAP_HDR */
    MsiReg.fMsi64bit      = 1; /* 64-bit addressing support is mandatory; See AMD IOMMU spec. 2.8 "IOMMU Interrupt Support". */

    /* MSI Address (Lo, Hi) and MSI data are read-write PCI config registers handled by our generic PCI config space code. */
#if 0
    /* MSI Address Lo. */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO, 0);         /* RW - MSI message address (Lo) */
    /* MSI Address Hi. */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI, 0);         /* RW - MSI message address (Hi) */
    /* MSI Data. */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA, 0);            /* RW - MSI data */
#endif

#if 0
    /** @todo IOMMU: I don't know if we need to support this, enable later if
     *        required. */
    /* MSI Mapping Capability Header register. */
    PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_MAP_CAP_HDR,
                        RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_CAP_ID,   0x8)       /* RO - Capability ID */
                      | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_CAP_PTR,  0x0)       /* RO - Offset to next capability (NULL) */
                      | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_EN,       0x1)       /* RO - MSI mapping capability enable */
                      | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_FIXED,    0x1)       /* RO - MSI mapping range is fixed */
                      | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_CAP_TYPE, 0x15));    /* RO - MSI mapping capability */
    /* When implementing don't forget to copy this to its MMIO shadow register (MsiMapCapHdr) in iommuAmdR3Init. */
#endif

    /*
     * Register the PCI function with PDM.
     */
    rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
    AssertLogRelRCReturn(rc, rc);

    /*
     * Register MSI support for the PCI device.
     * This must be done -after- registering it as a PCI device!
     */
    rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
    AssertRCReturn(rc, rc);

    /*
     * Intercept PCI config. space accesses.
     */
    rc = PDMDevHlpPCIInterceptConfigAccesses(pDevIns, pPciDev, iommuAmdR3PciConfigRead, iommuAmdR3PciConfigWrite);
    AssertLogRelRCReturn(rc, rc);

    /*
     * Create the MMIO region.
     * Mapping of the region is done when software configures it via PCI config space.
     */
    rc = PDMDevHlpMmioCreate(pDevIns, IOMMU_MMIO_REGION_SIZE, pPciDev, 0 /* iPciRegion */, iommuAmdMmioWrite, iommuAmdMmioRead,
                             NULL /* pvUser */,
                               IOMMMIO_FLAGS_READ_DWORD_QWORD
                             | IOMMMIO_FLAGS_WRITE_DWORD_QWORD_READ_MISSING
                             | IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_READ
                             | IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_WRITE,
                             "AMD-IOMMU", &pThis->hMmio);
    AssertLogRelRCReturn(rc, rc);

    /*
     * Register saved state handlers.
     */
    rc = PDMDevHlpSSMRegisterEx(pDevIns, IOMMU_SAVED_STATE_VERSION, sizeof(IOMMU), NULL /* pszBefore */,
                                NULL /* pfnLivePrep */,  NULL /* pfnLiveExec */,  NULL /* pfnLiveVote */,
                                NULL /* pfnSavePrep */,  iommuAmdR3SaveExec, NULL /* pfnSaveDone */,
                                NULL /* pfnLoadPrep */,  iommuAmdR3LoadExec, iommuAmdR3LoadDone);
    AssertLogRelRCReturn(rc, rc);

    /*
     * Register debugger info items.
     */
    PDMDevHlpDBGFInfoRegister(pDevIns, "iommu",    "Display IOMMU state.", iommuAmdR3DbgInfo);
    PDMDevHlpDBGFInfoRegister(pDevIns, "iommudte", "Display the DTE for a device (from memory). Arguments: DeviceID.", iommuAmdR3DbgInfoDte);
    PDMDevHlpDBGFInfoRegister(pDevIns, "iommudevtabs", "Display I/O device tables with translation enabled.", iommuAmdR3DbgInfoDevTabs);
#ifdef IOMMU_WITH_IOTLBE_CACHE
    PDMDevHlpDBGFInfoRegister(pDevIns, "iommutlb", "Display IOTLBs for a domain. Arguments: DomainID.", iommuAmdR3DbgInfoIotlb);
#endif
#ifdef IOMMU_WITH_DTE_CACHE
    PDMDevHlpDBGFInfoRegister(pDevIns, "iommudtecache", "Display the DTE cache.", iommuAmdR3DbgInfoDteCache);
#endif
#ifdef IOMMU_WITH_IRTE_CACHE
    PDMDevHlpDBGFInfoRegister(pDevIns, "iommuirtecache", "Display the IRTE cache.", iommuAmdR3DbgInfoIrteCache);
#endif

# ifdef VBOX_WITH_STATISTICS
    /*
     * Statistics.
     */
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadR3,  STAMTYPE_COUNTER, "R3/MmioRead",  STAMUNIT_OCCURENCES, "Number of MMIO reads in R3");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadRZ,  STAMTYPE_COUNTER, "RZ/MmioRead",  STAMUNIT_OCCURENCES, "Number of MMIO reads in RZ.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteR3, STAMTYPE_COUNTER, "R3/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in R3.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteRZ, STAMTYPE_COUNTER, "RZ/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in RZ.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapR3, STAMTYPE_COUNTER, "R3/MsiRemap", STAMUNIT_OCCURENCES, "Number of interrupt remap requests in R3.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapRZ, STAMTYPE_COUNTER, "RZ/MsiRemap", STAMUNIT_OCCURENCES, "Number of interrupt remap requests in RZ.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadR3,  STAMTYPE_COUNTER, "R3/MemRead",  STAMUNIT_OCCURENCES, "Number of memory read translation requests in R3.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadRZ,  STAMTYPE_COUNTER, "RZ/MemRead",  STAMUNIT_OCCURENCES, "Number of memory read translation requests in RZ.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteR3,  STAMTYPE_COUNTER, "R3/MemWrite",  STAMUNIT_OCCURENCES, "Number of memory write translation requests in R3.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteRZ,  STAMTYPE_COUNTER, "RZ/MemWrite",  STAMUNIT_OCCURENCES, "Number of memory write translation requests in RZ.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadR3,  STAMTYPE_COUNTER, "R3/MemBulkRead",  STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in R3.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadRZ,  STAMTYPE_COUNTER, "RZ/MemBulkRead",  STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in RZ.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteR3, STAMTYPE_COUNTER, "R3/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in R3.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteRZ, STAMTYPE_COUNTER, "RZ/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in RZ.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmd, STAMTYPE_COUNTER, "R3/Commands", STAMUNIT_OCCURENCES, "Number of commands processed (total).");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdCompWait, STAMTYPE_COUNTER, "R3/Commands/CompWait", STAMUNIT_OCCURENCES, "Number of Completion Wait commands processed.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvDte, STAMTYPE_COUNTER, "R3/Commands/InvDte", STAMUNIT_OCCURENCES, "Number of Invalidate DTE commands processed.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIommuPages, STAMTYPE_COUNTER, "R3/Commands/InvIommuPages", STAMUNIT_OCCURENCES, "Number of Invalidate IOMMU Pages commands processed.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIotlbPages, STAMTYPE_COUNTER, "R3/Commands/InvIotlbPages", STAMUNIT_OCCURENCES, "Number of Invalidate IOTLB Pages commands processed.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIntrTable, STAMTYPE_COUNTER, "R3/Commands/InvIntrTable", STAMUNIT_OCCURENCES, "Number of Invalidate Interrupt Table commands processed.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdPrefIommuPages, STAMTYPE_COUNTER, "R3/Commands/PrefIommuPages", STAMUNIT_OCCURENCES, "Number of Prefetch IOMMU Pages commands processed.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdCompletePprReq, STAMTYPE_COUNTER, "R3/Commands/CompletePprReq", STAMUNIT_OCCURENCES, "Number of Complete PPR Requests commands processed.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIommuAll, STAMTYPE_COUNTER, "R3/Commands/InvIommuAll", STAMUNIT_OCCURENCES, "Number of Invalidate IOMMU All commands processed.");


    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIotlbeCached, STAMTYPE_COUNTER, "IOTLB/Cached", STAMUNIT_OCCURENCES, "Number of IOTLB entries in the cache.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIotlbeLazyEvictReuse, STAMTYPE_COUNTER, "IOTLB/LazyEvictReuse", STAMUNIT_OCCURENCES, "Number of IOTLB entries reused after lazy eviction.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfDteLookup, STAMTYPE_PROFILE, "Profile/DteLookup", STAMUNIT_TICKS_PER_CALL, "Profiling DTE lookup.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfIotlbeLookup, STAMTYPE_PROFILE, "Profile/IotlbeLookup", STAMUNIT_TICKS_PER_CALL, "Profiling IOTLBE lookup.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfIrteLookup, STAMTYPE_PROFILE, "Profile/IrteLookup", STAMUNIT_TICKS_PER_CALL, "Profiling IRTE lookup.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfIrteCacheLookup, STAMTYPE_PROFILE, "Profile/IrteCacheLookup", STAMUNIT_TICKS_PER_CALL, "Profiling IRTE cache lookup.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheHit, STAMTYPE_COUNTER, "MemAccess/CacheHit", STAMUNIT_OCCURENCES, "Number of cache hits.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheMiss, STAMTYPE_COUNTER, "MemAccess/CacheMiss", STAMUNIT_OCCURENCES, "Number of cache misses.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheHitFull, STAMTYPE_COUNTER, "MemAccess/CacheHitFull", STAMUNIT_OCCURENCES, "Number of accesses that was entirely in the cache.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheNonContig, STAMTYPE_COUNTER, "MemAccess/CacheNonContig", STAMUNIT_OCCURENCES, "Number of cache accesses that resulted in non-contiguous translated regions.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCachePermDenied, STAMTYPE_COUNTER, "MemAccess/CacheAddrDenied", STAMUNIT_OCCURENCES, "Number of cache accesses that resulted in denied permissions.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessDteNonContig, STAMTYPE_COUNTER, "MemAccess/DteNonContig", STAMUNIT_OCCURENCES, "Number of DTE accesses that resulted in non-contiguous translated regions.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessDtePermDenied, STAMTYPE_COUNTER, "MemAccess/DtePermDenied", STAMUNIT_OCCURENCES, "Number of DTE accesses that resulted in denied permissions.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrCacheHit, STAMTYPE_COUNTER, "Interrupt/CacheHit", STAMUNIT_OCCURENCES, "Number of cache hits.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrCacheMiss, STAMTYPE_COUNTER, "Interrupt/CacheMiss", STAMUNIT_OCCURENCES, "Number of cache misses.");

    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatNonStdPageSize, STAMTYPE_COUNTER, "MemAccess/NonStdPageSize", STAMUNIT_OCCURENCES, "Number of non-standard page size translations.");
    PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIopfs, STAMTYPE_COUNTER, "MemAccess/IOPFs", STAMUNIT_OCCURENCES, "Number of I/O page faults.");
# endif

    /*
     * Create the command thread and its event semaphore.
     */
    char szDevIommu[64];
    RT_ZERO(szDevIommu);
    RTStrPrintf(szDevIommu, sizeof(szDevIommu), "IOMMU-%u", iInstance);
    rc = PDMDevHlpThreadCreate(pDevIns, &pThisR3->pCmdThread, pThis, iommuAmdR3CmdThread, iommuAmdR3CmdThreadWakeUp,
                               0 /* cbStack */, RTTHREADTYPE_IO, szDevIommu);
    AssertLogRelRCReturn(rc, rc);

    rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEvtCmdThread);
    AssertLogRelRCReturn(rc, rc);

#ifdef IOMMU_WITH_DTE_CACHE
    /*
     * Initialize the critsect of the cache.
     */
    rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSectCache, RT_SRC_POS, "IOMMUCache-#%u", pDevIns->iInstance);
    AssertLogRelRCReturn(rc, rc);

    /* Several places in this code relies on this basic assumption - assert it! */
    AssertCompile(RT_ELEMENTS(pThis->aDeviceIds) == RT_ELEMENTS(pThis->aDteCache));
#endif

#ifdef IOMMU_WITH_IOTLBE_CACHE
    /*
     * Allocate IOTLB entries.
     * This is allocated upfront since we expect a relatively small number of entries,
     * is more cache-line efficient and easier to track least recently used entries for
     * eviction when the cache is full. This also avoids unpredictable behavior during
     * the lifetime of the VM if the hyperheap gets full.
     */
    size_t const cbIotlbes = sizeof(IOTLBE) * IOMMU_IOTLBE_MAX;
    pThisR3->paIotlbes = (PIOTLBE)PDMDevHlpMMHeapAllocZ(pDevIns, cbIotlbes);
    if (!pThisR3->paIotlbes)
        return PDMDevHlpVMSetError(pDevIns, VERR_NO_MEMORY, RT_SRC_POS,
                                   N_("Failed to allocate %zu bytes from the hyperheap for the IOTLB cache."), cbIotlbes);
    RTListInit(&pThisR3->LstLruIotlbe);
    LogRel(("%s: Allocated %zu bytes from the hyperheap for the IOTLB cache\n", IOMMU_LOG_PFX, cbIotlbes));
#endif

    /*
     * Initialize read-only registers.
     * NOTE! Fields here must match their corresponding field in the ACPI tables.
     */
    /* Don't remove the commented lines below as it lets us see all features at a glance. */
    pThis->ExtFeat.u64 = 0;
    //pThis->ExtFeat.n.u1PrefetchSup           = 0;
    //pThis->ExtFeat.n.u1PprSup                = 0;
    //pThis->ExtFeat.n.u1X2ApicSup             = 0;
    //pThis->ExtFeat.n.u1NoExecuteSup          = 0;
    //pThis->ExtFeat.n.u1GstTranslateSup       = 0;
    pThis->ExtFeat.n.u1InvAllSup               = 1;
    //pThis->ExtFeat.n.u1GstVirtApicSup        = 0;
    pThis->ExtFeat.n.u1HwErrorSup              = 1;
    //pThis->ExtFeat.n.u1PerfCounterSup        = 0;
    AssertCompile((IOMMU_MAX_HOST_PT_LEVEL & 0x3) < 3);
    pThis->ExtFeat.n.u2HostAddrTranslateSize = (IOMMU_MAX_HOST_PT_LEVEL & 0x3);
    //pThis->ExtFeat.n.u2GstAddrTranslateSize  = 0;   /* Requires GstTranslateSup */
    //pThis->ExtFeat.n.u2GstCr3RootTblLevel    = 0;   /* Requires GstTranslateSup */
    //pThis->ExtFeat.n.u2SmiFilterSup          = 0;
    //pThis->ExtFeat.n.u3SmiFilterCount        = 0;
    //pThis->ExtFeat.n.u3GstVirtApicModeSup    = 0;   /* Requires GstVirtApicSup */
    //pThis->ExtFeat.n.u2DualPprLogSup         = 0;
    //pThis->ExtFeat.n.u2DualEvtLogSup         = 0;
    //pThis->ExtFeat.n.u5MaxPasidSup           = 0;   /* Requires GstTranslateSup */
    //pThis->ExtFeat.n.u1UserSupervisorSup     = 0;
    AssertCompile(IOMMU_MAX_DEV_TAB_SEGMENTS <= 3);
    pThis->ExtFeat.n.u2DevTabSegSup            = IOMMU_MAX_DEV_TAB_SEGMENTS;
    //pThis->ExtFeat.n.u1PprLogOverflowWarn    = 0;
    //pThis->ExtFeat.n.u1PprAutoRespSup        = 0;
    //pThis->ExtFeat.n.u2MarcSup               = 0;
    //pThis->ExtFeat.n.u1BlockStopMarkSup      = 0;
    //pThis->ExtFeat.n.u1PerfOptSup            = 0;
    pThis->ExtFeat.n.u1MsiCapMmioSup           = 1;
    //pThis->ExtFeat.n.u1GstIoSup              = 0;
    //pThis->ExtFeat.n.u1HostAccessSup         = 0;
    //pThis->ExtFeat.n.u1EnhancedPprSup        = 0;
    //pThis->ExtFeat.n.u1AttrForwardSup        = 0;
    //pThis->ExtFeat.n.u1HostDirtySup          = 0;
    //pThis->ExtFeat.n.u1InvIoTlbTypeSup       = 0;
    //pThis->ExtFeat.n.u1GstUpdateDisSup       = 0;
    //pThis->ExtFeat.n.u1ForcePhysDstSup       = 0;

    pThis->DevSpecificFeat.u64   = 0;
    pThis->DevSpecificFeat.n.u4RevMajor = IOMMU_DEVSPEC_FEAT_MAJOR_VERSION;
    pThis->DevSpecificFeat.n.u4RevMinor = IOMMU_DEVSPEC_FEAT_MINOR_VERSION;

    pThis->DevSpecificCtrl.u64 = 0;
    pThis->DevSpecificCtrl.n.u4RevMajor = IOMMU_DEVSPEC_CTRL_MAJOR_VERSION;
    pThis->DevSpecificCtrl.n.u4RevMinor = IOMMU_DEVSPEC_CTRL_MINOR_VERSION;

    pThis->DevSpecificStatus.u64 = 0;
    pThis->DevSpecificStatus.n.u4RevMajor = IOMMU_DEVSPEC_STATUS_MAJOR_VERSION;
    pThis->DevSpecificStatus.n.u4RevMinor = IOMMU_DEVSPEC_STATUS_MINOR_VERSION;

    pThis->MiscInfo.u64 = RT_MAKE_U64(uMiscInfoReg0, uMiscInfoReg1);

    pThis->RsvdReg = 0;

    /*
     * Initialize parts of the IOMMU state as it would during reset.
     * Also initializes non-zero initial values like IRTE cache keys.
     * Must be called -after- initializing PCI config. space registers.
     */
    iommuAmdR3Reset(pDevIns);

    LogRel(("%s: DSFX=%u.%u DSCX=%u.%u DSSX=%u.%u ExtFeat=%#RX64\n", IOMMU_LOG_PFX,
            pThis->DevSpecificFeat.n.u4RevMajor, pThis->DevSpecificFeat.n.u4RevMinor,
            pThis->DevSpecificCtrl.n.u4RevMajor, pThis->DevSpecificCtrl.n.u4RevMinor,
            pThis->DevSpecificStatus.n.u4RevMajor, pThis->DevSpecificStatus.n.u4RevMinor,
            pThis->ExtFeat.u64));
    return VINF_SUCCESS;
}

#else

/**
 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
 */
static DECLCALLBACK(int) iommuAmdRZConstruct(PPDMDEVINS pDevIns)
{
    PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
    PIOMMU   pThis   = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
    PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
    pThisCC->CTX_SUFF(pDevIns) = pDevIns;

    /* We will use PDM's critical section (via helpers) for the IOMMU device. */
    int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
    AssertRCReturn(rc, rc);

    /* Set up the MMIO RZ handlers. */
    rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, iommuAmdMmioWrite, iommuAmdMmioRead, NULL /* pvUser */);
    AssertRCReturn(rc, rc);

    /* Set up the IOMMU RZ callbacks. */
    PDMIOMMUREGCC IommuReg;
    RT_ZERO(IommuReg);
    IommuReg.u32Version       = PDM_IOMMUREGCC_VERSION;
    IommuReg.idxIommu         = pThis->idxIommu;
    IommuReg.pfnMemAccess     = iommuAmdMemAccess;
    IommuReg.pfnMemBulkAccess = iommuAmdMemBulkAccess;
    IommuReg.pfnMsiRemap      = iommuAmdMsiRemap;
    IommuReg.u32TheEnd        = PDM_IOMMUREGCC_VERSION;
    rc = PDMDevHlpIommuSetUpContext(pDevIns, &IommuReg, &pThisCC->CTX_SUFF(pIommuHlp));
    AssertRCReturn(rc, rc);
    AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp), VERR_IOMMU_IPE_1);
    AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32Version == CTX_MID(PDM_IOMMUHLP,_VERSION), VERR_VERSION_MISMATCH);
    AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32TheEnd  == CTX_MID(PDM_IOMMUHLP,_VERSION), VERR_VERSION_MISMATCH);
    AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnLock);
    AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnUnlock);
    AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnLockIsOwner);
    AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnSendMsi);
    return VINF_SUCCESS;
}
#endif


/**
 * The device registration structure.
 */
const PDMDEVREG g_DeviceIommuAmd =
{
    /* .u32Version = */             PDM_DEVREG_VERSION,
    /* .uReserved0 = */             0,
    /* .szName = */                 "iommu-amd",
    /* .fFlags = */                 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
    /* .fClass = */                 PDM_DEVREG_CLASS_PCI_BUILTIN,
    /* .cMaxInstances = */          1,
    /* .uSharedVersion = */         42,
    /* .cbInstanceShared = */       sizeof(IOMMU),
    /* .cbInstanceCC = */           sizeof(IOMMUCC),
    /* .cbInstanceRC = */           sizeof(IOMMURC),
    /* .cMaxPciDevices = */         1,
    /* .cMaxMsixVectors = */        0,
    /* .pszDescription = */         "IOMMU (AMD)",
#if defined(IN_RING3)
    /* .pszRCMod = */               "VBoxDDRC.rc",
    /* .pszR0Mod = */               "VBoxDDR0.r0",
    /* .pfnConstruct = */           iommuAmdR3Construct,
    /* .pfnDestruct = */            iommuAmdR3Destruct,
    /* .pfnRelocate = */            NULL,
    /* .pfnMemSetup = */            NULL,
    /* .pfnPowerOn = */             NULL,
    /* .pfnReset = */               iommuAmdR3Reset,
    /* .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 = */           iommuAmdRZConstruct,
    /* .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 = */           iommuAmdRZConstruct,
    /* .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 */