Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs
@ 2026-11-30 10:12 Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 1/6] hw/i386: Track the IOMMU to which IOAPIC is attached Sairaj Kodilkar
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Sairaj Kodilkar @ 2026-11-30 10:12 UTC (permalink / raw)
  To: Clément Mathieu--Drif, Michael S. Tsirkin, Alejandro Jimenez,
	Ani Sinha, Igor Mammedov, Jason Wang, Marcelo Tosatti,
	Paolo Bonzini, Richard Henderson, Sairaj Kodilkar, Yi Liu, kvm,
	qemu-devel
  Cc: Vasant.Hegde, Suravee.Suthikulpanit

Overview
--------
This series lets a guest have more than one AMD vIOMMU. Each vIOMMU takes
care of the devices under its own PCIe root bus (pcie.0 or a pxb-pcie).
It builds on the root-bus series [1], which allows an AMD vIOMMU to sit
on a root bus other than pcie.0.

Motivation
----------
With the upcoming hardware-accelerated vIOMMU, each guest vIOMMU is tied
to one host IOMMU through that host IOMMU's VFMMIO region. Because of
this, VFIO devices that sit behind different host IOMMUs cannot share a
single guest vIOMMU. If a guest gets passthrough devices from more than
one host IOMMU, it needs one vIOMMU for each of them.

Solution
--------
1. Track all vIOMMUs in a list in the PC machine, in creation order.
2. Build one IVHD per AMD vIOMMU. Its device entries are the devices under
   that vIOMMU's root bus; a child bus served by another vIOMMU is skipped,
   so IVHDs never overlap.
3. Give each vIOMMU its own 16 KiB MMIO window starting at 0xfed80000,
   indexed by creation order. At most 8 vIOMMUs are allowed, so the windows
   stay below smram-open-high at 0xfeda0000.
4. Track the IOMMU that the IOAPIC is bound to, and register the KVM IEC
   notifier on every vIOMMU instead of only the first one.
5. Allow multiple AMD vIOMMUs in pc_iommu_pre_plug(). They cannot be mixed
   with Intel or virtio IOMMUs.

Every amd-iommu in a multi-vIOMMU configuration must use 'pci-id' to name
its AMDVI-PCI device, and each must be on a different root bus.

Command-line
------------
qemu-system-x86_64 \
-enable-kvm -m 10G -smp 8  \
-netdev user,id=USER0,hostfwd=tcp::3333-:22 \
-netdev user,id=USER1 \
-cpu EPYC-Genoa,x2apic=on,kvm-msi-ext-dest-id=on,+kvm-pv-unhalt,kvm-pv-tlb-flush,kvm-pv-ipi,kvm-pv-sched-yield  \
-machine q35,kernel_irqchip=split \
-global kvm-pit.lost_tick_policy=discard \
-nographic \
-kernel <vmlinuz> \
-initrd <initrd.img> \
-blockdev node-name=drive0,driver=qcow2,file.driver=file,file.filename=<IMG> \
-device virtio-blk-pci,num-queues=8,drive=drive0 \
-device pxb-pcie,id=pcie.1,bus=pcie.0,bus_nr=0x40 \
-device pcie-root-port,id=rp1.1,bus=pcie.1,chassis=1,slot=1 \
-device pcie-root-port,id=rp1.2,bus=pcie.1,chassis=2,slot=1 \
-device AMDVI-PCI,id=iommupci0,bus=rp1.1 \
-device amd-iommu,intremap=on,dma-remap=on,intremap=off,xtsup=off,pci-id=iommupci0 \
-device virtio-net-pci,id=vnet0,iommu_platform=on,disable-legacy=on,romfile=,netdev=USER0,bus=rp1.2 \
-device pxb-pcie,id=pcie.2,bus=pcie.0,bus_nr=0x50 \
-device pcie-root-port,id=rp2.1,bus=pcie.2,chassis=3,slot=1 \
-device pcie-root-port,id=rp2.2,bus=pcie.2,chassis=4,slot=1 \
-device AMDVI-PCI,id=iommupci1,bus=rp2.1 \
-device amd-iommu,intremap=on,dma-remap=on,intremap=off,xtsup=off,pci-id=iommupci1 \
-device virtio-net-pci,id=vnet1,iommu_platform=on,disable-legacy=on,romfile=,netdev=USER1,bus=rp2.2

Examples
--------
Example1, For below PCI configuration AMD IOMMU 1 will serve all the devices
under pxb-pci1 and AMD IOMMU 2 will serve all the devices under pxb-pci2.

pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1
        |            +- pcie-root-port2.1 --- Device1
        |            \- pcie-root-port3.1 --- Device2
        \- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2
                     +- pcie-root-port2.2 --- Device3
                     \- pcie-root-port2.3 --- Device4

Example2, For below PCI configuration, AMD IOMMU 3 will only serve Device5 and
Device6 and devices under pxb-pci buses will be served by IOMMUs of respective
buses.

pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1
        |            +- pcie-root-port2.1 --- Device1
        |            \- pcie-root-port3.1 --- Device2
        +- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2
        |            +- pcie-root-port2.2 --- Device3
        |            \- pcie-root-port2.3 --- Device4
        +--- AMD IOMMU 3
        +--- Device5
        \--- Device6

Example3, The below PCI configuration is invalid since both IOMMUs share same
root bus i.e. pxb-pci1.

[A] pcie.0 -+- pxb-pci1 -+- pcie-root-port1 --- AMD IOMMU 1
                         +- pcie-root-port2 --- AMD IOMMU 2
                         +- pcie-root-port3 --- Device2
                         \- pcie-root-port4 --- Device3

[B] amd-iommu devices with and without 'pci-id' mixed.
[C] More than 8 amd-iommu devices.
[D] amd-iommu mixed with intel-iommu or virtio-iommu.

---
Patches are based on top of 9cda597a1a39 and patch [1]

[1] https://lore.kernel.org/all/20261116134209.30331-1-sarunkod@amd.com/
---

Sairaj Kodilkar (6):
  hw/i386: Track the IOMMU to which IOAPIC is attached
  i386/kvm: x86_iommu: Introduce per-iommu notify_list_inited flag
  acpi-build: i386/pc: Track vIOMMUs in a list
  amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs
  amd_iommu: Introduce AMDIVRSInfo for shared IVRS construction
  x86-iommu: Allow multiple AMD vIOMMU devices

 hw/i386/acpi-build.c        | 209 ++++++++++++++++++++----------------
 hw/i386/amd_iommu-stub.c    |  26 -----
 hw/i386/amd_iommu.c         |  76 ++++++++++---
 hw/i386/amd_iommu.h         |  15 ++-
 hw/i386/intel_iommu.c       |   1 +
 hw/i386/meson.build         |   3 +-
 hw/i386/pc.c                |  36 +++++--
 hw/i386/x86-iommu-stub.c    |   5 -
 hw/i386/x86-iommu.c         |  16 +--
 hw/intc/ioapic.c            |  18 ++--
 include/hw/i386/pc.h        |   9 +-
 include/hw/i386/x86-iommu.h |   8 +-
 include/hw/i386/x86.h       |   4 +
 target/i386/kvm/kvm.c       |  19 ++--
 14 files changed, 252 insertions(+), 193 deletions(-)
 delete mode 100644 hw/i386/amd_iommu-stub.c


-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/6] hw/i386: Track the IOMMU to which IOAPIC is attached
  2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
@ 2026-11-30 10:12 ` Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 2/6] i386/kvm: x86_iommu: Introduce per-iommu notify_list_inited flag Sairaj Kodilkar
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sairaj Kodilkar @ 2026-11-30 10:12 UTC (permalink / raw)
  To: Clément Mathieu--Drif, Michael S. Tsirkin, Alejandro Jimenez,
	Ani Sinha, Igor Mammedov, Jason Wang, Marcelo Tosatti,
	Paolo Bonzini, Richard Henderson, Sairaj Kodilkar, Yi Liu, kvm,
	qemu-devel
  Cc: Vasant.Hegde, Suravee.Suthikulpanit

Hardware accelerated vIOMMU on AMD requires one vIOMMU per host IOMMU
that backs assigned devices, so a guest can have multiple AMD vIOMMUs.

The IOAPIC sits outside the PCI hierarchy, so its IOMMU cannot be
looked up from a PCI bus. With multiple vIOMMUs, x86_iommu_get_default()
no longer identifies it, yet KVM MSI route fixup, IVRS construction and
the IEC notifier registration all need the IOMMU that remaps IOAPIC
interrupts.

Record it in the x86 machine state when the IOMMU is realized.

Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
 hw/i386/acpi-build.c  |  4 +++-
 hw/i386/amd_iommu.c   |  4 +++-
 hw/i386/intel_iommu.c |  1 +
 hw/i386/pc.c          |  1 +
 hw/intc/ioapic.c      | 18 +++++++++---------
 include/hw/i386/x86.h |  4 ++++
 target/i386/kvm/kvm.c |  4 ++--
 7 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index 84a8b14d83a6..a2a04de15ba1 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -1885,6 +1885,8 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
 {
     AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
     GArray *ivhd_blob = g_array_new(false, true, 1);
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+    X86MachineState *x86ms = X86_MACHINE(qdev_get_machine());
     AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
                         .oem_table_id = oem_table_id };
     uint16_t iommu_devid = pci_get_bdf(&s->pci->dev);
@@ -1930,7 +1932,7 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
      * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
      * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
      */
-    if (x86_iommu_ir_supported(x86_iommu_get_default())) {
+    if (x86ms->ioapic_iommu == x86_iommu) {
         build_append_int_noprefix(ivhd_blob,
                                  (0x1ull << 56) |           /* type IOAPIC */
                                  (IOAPIC_SB_DEVID << 40) |  /* IOAPIC devid */
diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index be4b9b28ef60..031ecbdb3de5 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -2732,6 +2732,7 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
     MachineState *ms = MACHINE(qdev_get_machine());
     PCMachineState *pcms = PC_MACHINE(ms);
     X86MachineState *x86ms = X86_MACHINE(ms);
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
     PCIBus *iommu_bus;
 
     if (s->pci_id) {
@@ -2796,9 +2797,10 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
      * AMD IOMMU IVRS, hence create a ioapic_as with root bus even though AMD
      * IOMMU is not serving devices attached to root bus.
      */
-    if (x86_iommu_ir_supported(X86_IOMMU_DEVICE(s))) {
+    if (!x86ms->ioapic_iommu && x86_iommu_ir_supported(X86_IOMMU_DEVICE(s))) {
         x86ms->ioapic_as = amdvi_host_dma_iommu(pcms->pcibus, s,
                                                 AMDVI_IOAPIC_SB_DEVID);
+        x86ms->ioapic_iommu = x86_iommu;
     }
 
     if (kvm_enabled() && x86ms->apic_id_limit > 255 && !s->xtsup) {
diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index 01befad9a55b..5c1afeaa3940 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -5704,6 +5704,7 @@ static void vtd_realize(DeviceState *dev, Error **errp)
     pci_setup_iommu(bus, &vtd_iommu_ops, dev);
     /* Pseudo address space under root PCI bus. */
     x86ms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
+    x86ms->ioapic_iommu = X86_IOMMU_DEVICE(dev);
 }
 
 static void vtd_class_init(ObjectClass *klass, const void *data)
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 4737eb471c14..f36fd02e9823 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -920,6 +920,7 @@ void pc_memory_init(PCMachineState *pcms,
 
     /* Init default IOAPIC address space */
     x86ms->ioapic_as = &address_space_memory;
+    x86ms->ioapic_iommu = NULL;
 
     /* Init ACPI memory hotplug IO base address */
     pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
diff --git a/hw/intc/ioapic.c b/hw/intc/ioapic.c
index e171cf843247..2f0b4b3dcce4 100644
--- a/hw/intc/ioapic.c
+++ b/hw/intc/ioapic.c
@@ -439,15 +439,15 @@ static void ioapic_machine_done_notify(Notifier *notifier, void *data)
     IOAPICCommonState *s = container_of(notifier, IOAPICCommonState,
                                         machine_done);
     X86MachineState *x86ms = X86_MACHINE(qdev_get_machine());
-
-    if (accel_irqchip_is_split() && x86ms->ioapic_as != &address_space_memory) {
-        X86IOMMUState *iommu = x86_iommu_get_default();
-        if (iommu) {
-            /* Register this IOAPIC with IOMMU IEC notifier, so that
-             * when there are IR invalidates, we can be notified to
-             * update kernel IR cache. */
-            x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
-        }
+    X86IOMMUState *iommu = x86ms->ioapic_iommu;
+
+    if (accel_irqchip_is_split() && iommu) {
+        /*
+         * Register this IOAPIC with IOMMU IEC notifier, so that
+         * when there are IR invalidates, we can be notified to
+         * update kernel IR cache.
+         */
+        x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
     }
 #endif
 }
diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h
index abb073ca47d3..43da8ced1933 100644
--- a/include/hw/i386/x86.h
+++ b/include/hw/i386/x86.h
@@ -26,6 +26,7 @@
 #include "hw/isa/isa.h"
 #include "qom/object.h"
 #include "system/igvm-cfg.h"
+#include "hw/i386/x86-iommu.h"
 
 struct X86MachineClass {
     MachineClass parent;
@@ -85,6 +86,9 @@ struct X86MachineState {
      */
     AddressSpace *ioapic_as;
 
+    /* IOMMU used by IOAPIC */
+    X86IOMMUState *ioapic_iommu;
+
     /*
      * Ratelimit enforced on detected bus locks in guest.
      * The default value of the bus_lock_ratelimit is 0 per second,
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 76528f1123f4..9bedf1fe680b 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -6771,8 +6771,8 @@ int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
 
     if (dev) {
         iommu = x86_iommu_device_get(dev);
-    } else if (x86ms->ioapic_as != &address_space_memory) {
-        iommu = x86_iommu_get_default();
+    } else if (x86ms->ioapic_iommu) {
+        iommu = x86ms->ioapic_iommu;
     }
 
     if (iommu) {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/6] i386/kvm: x86_iommu: Introduce per-iommu notify_list_inited flag
  2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 1/6] hw/i386: Track the IOMMU to which IOAPIC is attached Sairaj Kodilkar
@ 2026-11-30 10:12 ` Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 3/6] acpi-build: i386/pc: Track vIOMMUs in a list Sairaj Kodilkar
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sairaj Kodilkar @ 2026-11-30 10:12 UTC (permalink / raw)
  To: Clément Mathieu--Drif, Michael S. Tsirkin, Alejandro Jimenez,
	Ani Sinha, Igor Mammedov, Jason Wang, Marcelo Tosatti,
	Paolo Bonzini, Richard Henderson, Sairaj Kodilkar, Yi Liu, kvm,
	qemu-devel
  Cc: Vasant.Hegde, Suravee.Suthikulpanit

When interrupt remapping is enabled with a split irqchip, KVM MSI routes
carry the result of IOMMU interrupt remapping. Whenever the guest
invalidates the interrupt remapping cache of an IOMMU, the IEC notifier
kvm_update_msi_routes_all() rebuilds these routes.

kvm_arch_add_msi_route_post() registers that notifier on the first MSI
route update, and a static notify_list_inited flag blocks any further
registration. With multiple vIOMMUs, only one IOMMU gets the notifier.
Invalidations on the other IOMMUs leave KVM with stale MSI routes, so
interrupts of devices behind those IOMMUs keep using the old remapping
entries.

Move the flag into X86IOMMUState, so the notifier is registered once on
each IOMMU, when that IOMMU sees its first MSI route.

Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
 include/hw/i386/x86-iommu.h |  1 +
 target/i386/kvm/kvm.c       | 15 ++++++---------
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/include/hw/i386/x86-iommu.h b/include/hw/i386/x86-iommu.h
index eb575271e9d7..32cae9facfcd 100644
--- a/include/hw/i386/x86-iommu.h
+++ b/include/hw/i386/x86-iommu.h
@@ -65,6 +65,7 @@ struct X86IOMMUState {
     bool dt_supported;          /* Whether vIOMMU supports DT */
     bool dma_translation;       /* Whether vIOMMU supports DMA translation */
     QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */
+    bool notify_list_inited;    /* Whether IEC notifier is initiated*/
 };
 
 bool x86_iommu_ir_supported(X86IOMMUState *s);
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 9bedf1fe680b..6714c51d406e 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -6873,7 +6873,6 @@ void kvm_update_msi_routes_all(void *private, bool global,
 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
                                 int vector, PCIDevice *dev)
 {
-    static bool notify_list_inited = false;
     MSIRouteEntry *entry;
     X86IOMMUState *iommu = NULL;
 
@@ -6892,16 +6891,14 @@ int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
 
     trace_kvm_x86_add_msi_route(route->gsi);
 
-    if (!notify_list_inited) {
+    iommu = x86_iommu_device_get(dev);
+    if (iommu && !iommu->notify_list_inited) {
         /* For the first time we do add route, add ourselves into
          * IOMMU's IEC notify list if needed. */
-        iommu = x86_iommu_device_get(dev);
-        if (iommu) {
-            x86_iommu_iec_register_notifier(iommu,
-                                            kvm_update_msi_routes_all,
-                                            NULL);
-            notify_list_inited = true;
-        }
+        x86_iommu_iec_register_notifier(iommu,
+                                        kvm_update_msi_routes_all,
+                                        NULL);
+        iommu->notify_list_inited = true;
     }
     return 0;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/6] acpi-build: i386/pc: Track vIOMMUs in a list
  2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 1/6] hw/i386: Track the IOMMU to which IOAPIC is attached Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 2/6] i386/kvm: x86_iommu: Introduce per-iommu notify_list_inited flag Sairaj Kodilkar
@ 2026-11-30 10:12 ` Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 4/6] amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs Sairaj Kodilkar
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sairaj Kodilkar @ 2026-11-30 10:12 UTC (permalink / raw)
  To: Clément Mathieu--Drif, Michael S. Tsirkin, Alejandro Jimenez,
	Ani Sinha, Igor Mammedov, Jason Wang, Marcelo Tosatti,
	Paolo Bonzini, Richard Henderson, Sairaj Kodilkar, Yi Liu, kvm,
	qemu-devel
  Cc: Vasant.Hegde, Suravee.Suthikulpanit

Hardware accelerated vIOMMU on AMD requires one vIOMMU per host IOMMU
that backs assigned devices, so a guest needs multiple vIOMMUs.

Replace the single pcms->iommu pointer with a list of vIOMMUs in
creation order, so that the following patches can iterate over it to
build the IVRS IVHD entries per IOMMU. Replace the
x86_iommu_get_default() with the first IOMMU in the list to create ACPI
tables.

Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
 hw/i386/acpi-build.c        | 13 +++++++++----
 hw/i386/pc.c                | 31 +++++++++++++++++++++++--------
 hw/i386/x86-iommu-stub.c    |  5 -----
 hw/i386/x86-iommu.c         | 13 -------------
 include/hw/i386/pc.h        |  9 ++++++++-
 include/hw/i386/x86-iommu.h |  7 +------
 6 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index a2a04de15ba1..0aa31af8de5f 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -1650,7 +1650,8 @@ build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
     /* Root complex IOAPIC uses one path only */
     const size_t ioapic_scope_size = 6 /* device scope structure */ +
                                      2 /* 1 path entry */;
-    X86IOMMUState *iommu = x86_iommu_get_default();
+    PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
+    X86IOMMUState *iommu = X86_IOMMU_DEVICE(QTAILQ_FIRST(&pcms->iommus)->dev);
     IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
     GArray *scope_blob = g_array_new(false, true, 1);
 
@@ -1883,9 +1884,11 @@ static void
 build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
                 const char *oem_table_id)
 {
-    AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
+    PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
+    PCIOMMUEntry *first = QTAILQ_FIRST(&pcms->iommus);
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(first->dev);
+    AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu);
     GArray *ivhd_blob = g_array_new(false, true, 1);
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
     X86MachineState *x86ms = X86_MACHINE(qdev_get_machine());
     AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
                         .oem_table_id = oem_table_id };
@@ -2050,7 +2053,8 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
 {
     PCMachineState *pcms = PC_MACHINE(machine);
     X86MachineState *x86ms = X86_MACHINE(machine);
-    DeviceState *iommu = pcms->iommu;
+    PCIOMMUEntry *iommu_entry = QTAILQ_FIRST(&pcms->iommus);
+    DeviceState *iommu = iommu_entry ? iommu_entry->dev : NULL;
     GArray *table_offsets;
     unsigned facs, dsdt, rsdt;
     AcpiPmInfo pm;
@@ -2177,6 +2181,7 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
         build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
                    x86ms->oem_table_id);
     }
+    /* All vIOMMUs have the same type, so the first one selects the table */
     if (object_dynamic_cast(OBJECT(iommu), TYPE_AMD_IOMMU_DEVICE)) {
         acpi_add_table(table_offsets, tables_blob);
         build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index f36fd02e9823..1b1101af0c79 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -1318,6 +1318,27 @@ static void pc_sp_mem_plug(const HotplugHandler *hotplug_dev,
     e820_add_entry(addr, size, E820_SOFT_RESERVED);
 }
 
+static void pc_iommu_pre_plug(PCMachineState *pcms, DeviceState *dev,
+                              Error **errp)
+{
+    PCIOMMUEntry *entry;
+
+    if (!QTAILQ_EMPTY(&pcms->iommus)) {
+        error_setg(errp, "QEMU does not support multiple vIOMMUs "
+                   "for x86 yet.");
+        return;
+    }
+
+    if (object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE)) {
+        X86_IOMMU_DEVICE(dev)->index = pcms->nr_iommus;
+    }
+
+    entry = g_new0(PCIOMMUEntry, 1);
+    entry->dev = dev;
+    QTAILQ_INSERT_TAIL(&pcms->iommus, entry, next);
+    pcms->nr_iommus++;
+}
+
 static void pc_machine_device_pre_plug_cb(const HotplugHandler *hotplug_dev,
                                           DeviceState *dev, Error **errp)
 {
@@ -1343,14 +1364,7 @@ static void pc_machine_device_pre_plug_cb(const HotplugHandler *hotplug_dev,
 
     if (object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE) ||
         object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
-        PCMachineState *pcms = PC_MACHINE(hotplug_dev);
-
-        if (pcms->iommu) {
-            error_setg(errp, "QEMU does not support multiple vIOMMUs "
-                       "for x86 yet.");
-            return;
-        }
-        pcms->iommu = dev;
+        pc_iommu_pre_plug(PC_MACHINE(hotplug_dev), dev, errp);
     } else if (object_dynamic_cast(OBJECT(dev), TYPE_HV_BALLOON)) {
         pc_hv_balloon_pre_plug(hotplug_dev, dev, errp);
     }
@@ -1621,6 +1635,7 @@ static void pc_machine_initfn(Object *obj)
     pcms->vmport = ON_OFF_AUTO_OFF;
 #endif /* CONFIG_VMPORT */
     pcms->max_ram_below_4g = 0; /* use default */
+    QTAILQ_INIT(&pcms->iommus);
     pcms->smbios_entry_point_type = pcmc->default_smbios_ep_type;
     pcms->south_bridge = pcmc->default_south_bridge;
 
diff --git a/hw/i386/x86-iommu-stub.c b/hw/i386/x86-iommu-stub.c
index b0383d2290b1..c32d8266bdf6 100644
--- a/hw/i386/x86-iommu-stub.c
+++ b/hw/i386/x86-iommu-stub.c
@@ -27,11 +27,6 @@ void x86_iommu_iec_register_notifier(X86IOMMUState *iommu,
 {
 }
 
-X86IOMMUState *x86_iommu_get_default(void)
-{
-    return NULL;
-}
-
 X86IOMMUState *x86_iommu_device_get(PCIDevice *dev)
 {
     return NULL;
diff --git a/hw/i386/x86-iommu.c b/hw/i386/x86-iommu.c
index 601db5cfea09..160bdf9df93a 100644
--- a/hw/i386/x86-iommu.c
+++ b/hw/i386/x86-iommu.c
@@ -78,19 +78,6 @@ void x86_iommu_irq_to_msi_message(X86IOMMUIrq *irq, MSIMessage *msg_out)
     msg_out->data = msg.msi_data;
 }
 
-X86IOMMUState *x86_iommu_get_default(void)
-{
-    MachineState *ms = MACHINE(qdev_get_machine());
-    PCMachineState *pcms =
-        PC_MACHINE(object_dynamic_cast(OBJECT(ms), TYPE_PC_MACHINE));
-
-    if (pcms &&
-        object_dynamic_cast(OBJECT(pcms->iommu), TYPE_X86_IOMMU_DEVICE)) {
-        return X86_IOMMU_DEVICE(pcms->iommu);
-    }
-    return NULL;
-}
-
 X86IOMMUState *x86_iommu_device_get(PCIDevice *dev)
 {
     PCIBus *iommu_bus;
diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h
index 844ae835784f..278a28e56d5a 100644
--- a/include/hw/i386/pc.h
+++ b/include/hw/i386/pc.h
@@ -16,6 +16,11 @@
 
 #define MAX_IDE_BUS 2
 
+typedef struct PCIOMMUEntry {
+    DeviceState *dev;
+    QTAILQ_ENTRY(PCIOMMUEntry) next;
+} PCIOMMUEntry;
+
 /**
  * PCMachineState:
  * @acpi_dev: link to ACPI PM device that performs ACPI hotplug handling
@@ -35,7 +40,9 @@ typedef struct PCMachineState {
     I2CBus *smbus;
     PFlashCFI01 *flash[2];
     ISADevice *pcspk;
-    DeviceState *iommu;
+    /* vIOMMUs in creation order; all of them have the same type */
+    QTAILQ_HEAD(, PCIOMMUEntry) iommus;
+    unsigned int nr_iommus;
     BusState *idebus[MAX_IDE_BUS];
 
     Object *alias_pcspk;
diff --git a/include/hw/i386/x86-iommu.h b/include/hw/i386/x86-iommu.h
index 32cae9facfcd..05feff5c925c 100644
--- a/include/hw/i386/x86-iommu.h
+++ b/include/hw/i386/x86-iommu.h
@@ -66,6 +66,7 @@ struct X86IOMMUState {
     bool dma_translation;       /* Whether vIOMMU supports DMA translation */
     QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */
     bool notify_list_inited;    /* Whether IEC notifier is initiated*/
+    unsigned int index;         /* Instance number, set by the machine */
 };
 
 bool x86_iommu_ir_supported(X86IOMMUState *s);
@@ -129,12 +130,6 @@ struct X86IOMMU_MSIMessage {
     };
 };
 
-/**
- * x86_iommu_get_default - get default IOMMU device
- * @return: pointer to default IOMMU device
- */
-X86IOMMUState *x86_iommu_get_default(void);
-
 /**
  * x86_iommu_device_get - get IOMMU device for given PCI device
  * @return: pointer to default IOMMU device
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/6] amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs
  2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
                   ` (2 preceding siblings ...)
  2026-11-30 10:12 ` [PATCH 3/6] acpi-build: i386/pc: Track vIOMMUs in a list Sairaj Kodilkar
@ 2026-11-30 10:12 ` Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 5/6] amd_iommu: Introduce AMDIVRSInfo for shared IVRS construction Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 6/6] x86-iommu: Allow multiple AMD vIOMMU devices Sairaj Kodilkar
  5 siblings, 0 replies; 7+ messages in thread
From: Sairaj Kodilkar @ 2026-11-30 10:12 UTC (permalink / raw)
  To: Clément Mathieu--Drif, Michael S. Tsirkin, Alejandro Jimenez,
	Ani Sinha, Igor Mammedov, Jason Wang, Marcelo Tosatti,
	Paolo Bonzini, Richard Henderson, Sairaj Kodilkar, Yi Liu, kvm,
	qemu-devel
  Cc: Vasant.Hegde, Suravee.Suthikulpanit

Walk the IOMMU list when constructing the IVRS so each IOMMU is added as
IVHD. When emitting an IVHD entry, skip a child PCI bus if another IOMMU is
already attached to it. That lets a default IOMMU cover pcie.0 while
additional IOMMUs isolate selected downstream buses without overlapping
IVHD coverage.

Assign each IOMMU a distinct MMIO window beginning at 0xfed80000,
stepped by the x86 IOMMU instance index, so every device has a unique
register base.

amd-iommu devices with and without 'pci-id' cannot be mixed, because
the vmsd (migratable or not) is shared by the class.

Examples, once multiple vIOMMUs are allowed later in the series:

Example1, For below PCI configuration AMD IOMMU 1 will serve all the devices
under pxb-pci1 and AMD IOMMU 2 will serve all the devices under pxb-pci2.

pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1
        |            +- pcie-root-port2.1 --- Device1
        |            \- pcie-root-port3.1 --- Device2
        \- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2
                     +- pcie-root-port2.2 --- Device3
                     \- pcie-root-port2.3 --- Device4

Example2, For below PCI configuration, AMD IOMMU 3 will only serve Device5 and
Device6 and devices under pxb-pci buses will be served by IOMMUs of respective
buses.

pcie.0 -+- pxb-pci1 -+- pcie-root-port1.1 --- AMD IOMMU 1
        |            +- pcie-root-port2.1 --- Device1
        |            \- pcie-root-port3.1 --- Device2
        +- pxb-pci2 -+- pcie-root-port2.1 --- AMD IOMMU 2
        |            +- pcie-root-port2.2 --- Device3
        |            \- pcie-root-port2.3 --- Device4
        +--- AMD IOMMU 3
        +--- Device5
        \--- Device6

Example3, The below PCI configuration is invalid since both IOMMUs share same
root bus i.e. pxb-pci1.

pcie.0 -+- pxb-pci1 -+- pcie-root-port1 --- AMD IOMMU 1
                     +- pcie-root-port2 --- AMD IOMMU 2
                     +- pcie-root-port3 --- Device2
                     \- pcie-root-port4 --- Device3

Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
 hw/i386/acpi-build.c | 196 ++++++++++++++++++++++---------------------
 hw/i386/amd_iommu.c  |  56 ++++++++++---
 hw/i386/amd_iommu.h  |   3 +
 3 files changed, 148 insertions(+), 107 deletions(-)

diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index 0aa31af8de5f..188c97f0e916 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -1845,7 +1845,10 @@ select_devices:
     pci_for_each_device_under_bus(bus, insert_ivhd, table_data);
 
     QLIST_FOREACH(child_bus, &bus->child, sibling) {
-        add_ivhd_entries_from_bus(child_bus, table_data);
+        /* In case of multiple IOMMUs, Child bus can have a separate iommu */
+        if (!child_bus->iommu_ops) {
+            add_ivhd_entries_from_bus(child_bus, table_data);
+        }
     }
 }
 
@@ -1885,15 +1888,10 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
                 const char *oem_table_id)
 {
     PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
-    PCIOMMUEntry *first = QTAILQ_FIRST(&pcms->iommus);
-    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(first->dev);
-    AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu);
-    GArray *ivhd_blob = g_array_new(false, true, 1);
-    X86MachineState *x86ms = X86_MACHINE(qdev_get_machine());
+    X86MachineState *x86ms = X86_MACHINE(pcms);
+    PCIOMMUEntry *entry;
     AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
                         .oem_table_id = oem_table_id };
-    uint16_t iommu_devid = pci_get_bdf(&s->pci->dev);
-    uint64_t extended_feature = amdvi_extended_feature_register(s);
 
     acpi_table_begin(&table, table_data);
     /* IVinfo - IO virtualization information common to all
@@ -1915,99 +1913,109 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
      * blob further below.  Fall back to an entry covering all devices, which
      * is sufficient when no aliases are present.
      */
-    assert(s->root_bus);
-    add_ivhd_entries_from_bus(s->root_bus, ivhd_blob);
 
-    if (!ivhd_blob->len) {
-        /*
-         *   Type 1 device entry reporting all devices
-         *   These are 4-byte device entries currently reporting the range of
-         *   Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
-         */
-        build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
-    }
+    QTAILQ_FOREACH(entry, &pcms->iommus, next) {
+        X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(entry->dev);
+        AMDVIState *s = AMD_IOMMU_DEVICE(entry->dev);
+        GArray *ivhd_blob = g_array_new(false, true, 1);
+        uint16_t iommu_devid = pci_get_bdf(&s->pci->dev);
+        uint64_t extended_feature = amdvi_extended_feature_register(s);
 
-    /*
-     * When interrupt remapping is supported, we add a special IVHD device
-     * for type IO-APIC
-     * Refer to spec - Table 95: IVHD device entry type codes
-     *
-     * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
-     * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
-     */
-    if (x86ms->ioapic_iommu == x86_iommu) {
-        build_append_int_noprefix(ivhd_blob,
-                                 (0x1ull << 56) |           /* type IOAPIC */
-                                 (IOAPIC_SB_DEVID << 40) |  /* IOAPIC devid */
-                                 0x48,                      /* special device */
-                                 8);
-    }
+        assert(s->root_bus);
+        add_ivhd_entries_from_bus(s->root_bus, ivhd_blob);
 
-    /* IVHD definition - type 10h */
-    build_append_int_noprefix(table_data, 0x10, 1);
-    /* virtualization flags */
-    build_append_int_noprefix(table_data,
-                             (1UL << 0) | /* HtTunEn      */
-                             (1UL << 4) | /* iotblSup     */
-                             (1UL << 6),  /* PrefSup      */
-                             1);
-
-    /* IVHD length */
-    build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2);
-    /* DeviceID */
-    build_append_int_noprefix(table_data, iommu_devid, 2);
-    /* Capability offset */
-    build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
-    /* IOMMU base address */
-    build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
-    /* PCI Segment Group */
-    build_append_int_noprefix(table_data, 0, 2);
-    /* IOMMU info */
-    build_append_int_noprefix(table_data, 0, 2);
-    /* IOMMU Feature Reporting */
-    build_append_int_noprefix(table_data,
-                              get_amd_ivhd_feature_report(extended_feature),
-                              4);
+        if (!ivhd_blob->len) {
+            /*
+             *   Type 1 device entry reporting all devices
+             *   These are 4-byte device entries currently reporting the range
+             *   of Refer to Spec - Table 95:IVHD Device Entry Type Codes
+             *   (4-byte)
+             */
+            build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
+        }
 
-    /* IVHD entries as found above */
-    g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
+        /*
+         * When interrupt remapping is supported, we add a special IVHD device
+         * for type IO-APIC
+         * Refer to spec - Table 95: IVHD device entry type codes
+         *
+         * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
+         * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
+         */
+        if (x86ms->ioapic_iommu == x86_iommu) {
+            build_append_int_noprefix(ivhd_blob,
+                                     (0x1ull << 56) |           /* type IOAPIC */
+                                     (IOAPIC_SB_DEVID << 40) |  /* IOAPIC devid */
+                                     0x48,                      /* special device */
+                                     8);
+        }
 
-   /* IVHD definition - type 11h */
-    build_append_int_noprefix(table_data, 0x11, 1);
-    /* virtualization flags */
-    build_append_int_noprefix(table_data,
-                             (1UL << 0) | /* HtTunEn      */
-                             (1UL << 4),  /* iotblSup     */
-                             1);
-
-    /* IVHD length */
-    build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2);
-
-    /* DeviceID */
-    build_append_int_noprefix(table_data, iommu_devid, 2);
-    /* Capability offset */
-    build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
-    /* IOMMU base address */
-    build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
-    /* PCI Segment Group */
-    build_append_int_noprefix(table_data, 0, 2);
-    /* IOMMU info */
-    build_append_int_noprefix(table_data, 0, 2);
-    /* IOMMU Attributes */
-    if (!s->iommu.dma_translation) {
-        build_append_int_noprefix(table_data, (1UL << 0) /* HATDis */, 4);
-    } else {
-        build_append_int_noprefix(table_data, 0, 4);
-    }
-    /* EFR Register Image */
-    build_append_int_noprefix(table_data, extended_feature, 8);
-    /* EFR Register Image 2 */
-    build_append_int_noprefix(table_data, 0, 8);
+        /* IVHD definition - type 10h */
+        build_append_int_noprefix(table_data, 0x10, 1);
+        /* virtualization flags */
+        build_append_int_noprefix(table_data,
+                                 (1UL << 0) | /* HtTunEn      */
+                                 (1UL << 4) | /* iotblSup     */
+                                 (1UL << 6),  /* PrefSup      */
+                                 1);
+
+        /* IVHD length */
+        build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2);
+        /* DeviceID */
+        build_append_int_noprefix(table_data, iommu_devid, 2);
+        /* Capability offset */
+        build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
+        /* IOMMU base address */
+        build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
+        /* PCI Segment Group */
+        build_append_int_noprefix(table_data, 0, 2);
+        /* IOMMU info */
+        build_append_int_noprefix(table_data, 0, 2);
+        /* IOMMU Feature Reporting */
+        build_append_int_noprefix(table_data,
+                                  get_amd_ivhd_feature_report(extended_feature),
+                                  4);
+
+        /* IVHD entries as found above */
+        g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
+
+        /* IVHD definition - type 11h */
+        build_append_int_noprefix(table_data, 0x11, 1);
+        /* virtualization flags */
+        build_append_int_noprefix(table_data,
+                                 (1UL << 0) | /* HtTunEn      */
+                                 (1UL << 4),  /* iotblSup     */
+                                 1);
+
+        /* IVHD length */
+        build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2);
+
+        /* DeviceID */
+        build_append_int_noprefix(table_data, iommu_devid, 2);
+        /* Capability offset */
+        build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
+        /* IOMMU base address */
+        build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
+        /* PCI Segment Group */
+        build_append_int_noprefix(table_data, 0, 2);
+        /* IOMMU info */
+        build_append_int_noprefix(table_data, 0, 2);
+        /* IOMMU Attributes */
+        if (!s->iommu.dma_translation) {
+            build_append_int_noprefix(table_data, (1UL << 0) /* HATDis */, 4);
+        } else {
+            build_append_int_noprefix(table_data, 0, 4);
+        }
+        /* EFR Register Image */
+        build_append_int_noprefix(table_data, extended_feature, 8);
+        /* EFR Register Image 2 */
+        build_append_int_noprefix(table_data, 0, 8);
 
-    /* IVHD entries as found above */
-    g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
+        /* IVHD entries as found above */
+        g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
 
-    g_array_free(ivhd_blob, TRUE);
+        g_array_free(ivhd_blob, TRUE);
+    }
     acpi_table_end(linker, &table);
 }
 
diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index 031ecbdb3de5..79d3fcf9fd9b 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -2596,10 +2596,6 @@ static void amdvi_pci_realize(PCIDevice *pdev, Error **errp)
 
     /* reset AMDVI specific capabilities, all r/o */
     pci_set_long(pdev->config + s->capab_offset, AMDVI_CAPAB_FEATURES);
-    pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_BAR_LOW,
-                 AMDVI_BASE_ADDR & MAKE_64BIT_MASK(14, 18));
-    pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_BAR_HIGH,
-                AMDVI_BASE_ADDR >> 32);
     pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_RANGE,
                  0xff000000);
     pci_set_long(pdev->config + s->capab_offset + AMDVI_CAPAB_MISC, 0);
@@ -2725,6 +2721,11 @@ out:
     return bus;
 }
 
+static const VMStateDescription vmstate_amdvi_sysbus = {
+    .name = "amd-iommu",
+    .unmigratable = 1
+};
+
 static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = (DeviceClass *) object_get_class(OBJECT(dev));
@@ -2734,8 +2735,22 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
     X86MachineState *x86ms = X86_MACHINE(ms);
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
     PCIBus *iommu_bus;
+    uint64_t base_addr;
+    uint8_t *pci_config;
 
-    if (s->pci_id) {
+    if (x86_iommu->index >= AMDVI_MAX_IOMMUS) {
+        /*
+         * Limit the maximum number of IOMMUs to 8 because of limited MMIO
+         * address. With more than 8 IOMMUs the MMIO address exceeds 0xfeda0000
+         * and overlaps with smram-open-high. Keep the MMIO address space in
+         * range 0xfed80000-0xfed9ffff.
+         */
+        error_setg(errp, "at most %d amd-iommu devices are supported",
+                   AMDVI_MAX_IOMMUS);
+        return;
+    }
+
+    if (s->pci_id && dc->vmsd != &vmstate_amdvi_sysbus) {
         PCIDevice *pdev = NULL;
         int ret = pci_qdev_find_device(s->pci_id, &pdev);
 
@@ -2752,15 +2767,28 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
         iommu_bus = pci_get_bus(pdev);
         s->pci = AMD_IOMMU_PCI(pdev);
         dc->vmsd = &vmstate_amdvi_sysbus_migratable;
-    } else {
+    } else if (!s->pci_id && dc->vmsd != &vmstate_amdvi_sysbus_migratable) {
         iommu_bus = pcms->pcibus;
         s->pci = AMD_IOMMU_PCI(object_new(TYPE_AMD_IOMMU_PCI));
+        dc->vmsd = &vmstate_amdvi_sysbus;
         /* This device should take care of IOMMU PCI properties */
         if (!qdev_realize(DEVICE(s->pci), &iommu_bus->qbus, errp)) {
             return;
         }
+    } else {
+        error_setg(errp, "Cannot mix amd-iommu devices with and without 'pci-id'");
+        return;
     }
 
+    base_addr = AMDVI_GET_BASE_ADDR(x86_iommu->index);
+
+    pci_config = s->pci->dev.config + s->pci->capab_offset;
+
+    /* Set up PCI Capability base address */
+    pci_set_long(pci_config + AMDVI_CAPAB_BAR_LOW,
+                 base_addr & MAKE_64BIT_MASK(14, 18));
+    pci_set_long(pci_config + AMDVI_CAPAB_BAR_HIGH, base_addr >> 32);
+
     s->root_bus = get_root_bus(iommu_bus);
 
     s->iotlb = g_hash_table_new_full(amdvi_iotlb_hash,
@@ -2772,7 +2800,7 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
     /* set up MMIO */
     memory_region_init_io(&s->mr_mmio, OBJECT(s), &mmio_mem_ops, s,
                           "amdvi-mmio", AMDVI_MMIO_SIZE);
-    memory_region_add_subregion(get_system_memory(), AMDVI_BASE_ADDR,
+    memory_region_add_subregion(get_system_memory(), base_addr,
                                 &s->mr_mmio);
 
     /* Create the share memory regions by all devices */
@@ -2796,6 +2824,14 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
      * The linux kernel disables the intremap when it cannot find IOAPIC under
      * AMD IOMMU IVRS, hence create a ioapic_as with root bus even though AMD
      * IOMMU is not serving devices attached to root bus.
+     *
+     * The IOAPIC is not a PCI device, so it has no natural IOMMU. Bind it to
+     * the first AMD IOMMU, in command-line order, that has interrupt
+     * remapping enabled. That IOMMU translates and remaps IOAPIC interrupts
+     * using the IOAPIC's southbridge device ID (00:14.0), even if it serves a
+     * root bus other than pcie.0. The IVRS stays consistent, because only the
+     * IVHD of this IOMMU (x86ms->ioapic_iommu) carries the IOAPIC special
+     * device entry, and the guest driver follows that entry.
      */
     if (!x86ms->ioapic_iommu && x86_iommu_ir_supported(X86_IOMMU_DEVICE(s))) {
         x86ms->ioapic_as = amdvi_host_dma_iommu(pcms->pcibus, s,
@@ -2826,18 +2862,12 @@ static const Property amdvi_properties[] = {
     DEFINE_PROP_BOOL("dma-remap", AMDVIState, dma_remap, false),
 };
 
-static const VMStateDescription vmstate_amdvi_sysbus = {
-    .name = "amd-iommu",
-    .unmigratable = 1
-};
-
 static void amdvi_sysbus_class_init(ObjectClass *klass, const void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     X86IOMMUClass *dc_class = X86_IOMMU_DEVICE_CLASS(klass);
 
     device_class_set_legacy_reset(dc, amdvi_sysbus_reset);
-    dc->vmsd = &vmstate_amdvi_sysbus;
     dc->hotpluggable = false;
     dc_class->realize = amdvi_sysbus_realize;
     dc_class->int_remap = amdvi_int_remap;
diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h
index 008e882724c1..383605c38e6c 100644
--- a/hw/i386/amd_iommu.h
+++ b/hw/i386/amd_iommu.h
@@ -64,6 +64,7 @@
 #define AMDVI_MMIO_PPR_TAIL           0x2038
 
 #define AMDVI_MMIO_SIZE               0x4000
+#define AMDVI_MAX_IOMMUS              8
 
 #define AMDVI_MMIO_DEVTAB_SIZE_MASK     GENMASK64(8, 0)
 #define AMDVI_MMIO_DEVTAB_BASE_MASK     GENMASK64(51, 12)
@@ -239,6 +240,8 @@
 
 /* AMDVI default address */
 #define AMDVI_BASE_ADDR 0xfed80000ULL
+#define AMDVI_GET_BASE_ADDR(index) \
+        (AMDVI_BASE_ADDR + ((index) * AMDVI_MMIO_SIZE))
 
 /* page management constants */
 #define AMDVI_PAGE_SHIFT 12
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 5/6] amd_iommu: Introduce AMDIVRSInfo for shared IVRS construction
  2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
                   ` (3 preceding siblings ...)
  2026-11-30 10:12 ` [PATCH 4/6] amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs Sairaj Kodilkar
@ 2026-11-30 10:12 ` Sairaj Kodilkar
  2026-11-30 10:12 ` [PATCH 6/6] x86-iommu: Allow multiple AMD vIOMMU devices Sairaj Kodilkar
  5 siblings, 0 replies; 7+ messages in thread
From: Sairaj Kodilkar @ 2026-11-30 10:12 UTC (permalink / raw)
  To: Clément Mathieu--Drif, Michael S. Tsirkin, Alejandro Jimenez,
	Ani Sinha, Igor Mammedov, Jason Wang, Marcelo Tosatti,
	Paolo Bonzini, Richard Henderson, Sairaj Kodilkar, Yi Liu, kvm,
	qemu-devel
  Cc: Vasant.Hegde, Suravee.Suthikulpanit

IVRS construction will be shared by the both emulated IOMMU and upcoming
hardware vIOMMU. The hardware model does not use AMDVIState and will
introduce new struct "AMDVIOMMUState", so there is need to have a common API
which IVRS builder can use independently.

Add AMDIVRSInfo for that. Each model supplies the IOMMU PCI device, the
root bus, the MMIO base address, and a extended_feature. acpi-build reads
those fields when it emits IVHD type 10h and 11h entries. The struct
also includes dma_translation because upcoming AMD HW-VIOMMU model
doesn't support V1 page tables, and must always set the HATDis=1
(dma_translation=0).

The emulated model fills AMDIVRSInfo from AMDVIState. Drop the exported
helper and amd_iommu-stub.c, which existed only to provide that helper
when CONFIG_AMD_IOMMU is off.

Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
 hw/i386/acpi-build.c     | 26 +++++++++++++++++---------
 hw/i386/amd_iommu-stub.c | 26 --------------------------
 hw/i386/amd_iommu.c      | 16 +++++++++++++---
 hw/i386/amd_iommu.h      | 12 ++++++++++--
 hw/i386/meson.build      |  3 +--
 5 files changed, 41 insertions(+), 42 deletions(-)
 delete mode 100644 hw/i386/amd_iommu-stub.c

diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index 188c97f0e916..d4be50ade22f 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -1918,11 +1918,19 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
         X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(entry->dev);
         AMDVIState *s = AMD_IOMMU_DEVICE(entry->dev);
         GArray *ivhd_blob = g_array_new(false, true, 1);
-        uint16_t iommu_devid = pci_get_bdf(&s->pci->dev);
-        uint64_t extended_feature = amdvi_extended_feature_register(s);
+        uint16_t iommu_devid;
+        uint64_t extended_feature;
+        AMDIVRSInfo ivrs_info;
 
-        assert(s->root_bus);
-        add_ivhd_entries_from_bus(s->root_bus, ivhd_blob);
+        s->get_ivrs_info(s, &ivrs_info);
+
+        assert(ivrs_info.pci);
+        assert(ivrs_info.root_bus);
+
+        add_ivhd_entries_from_bus(ivrs_info.root_bus, ivhd_blob);
+
+        iommu_devid = pci_get_bdf(&ivrs_info.pci->dev);
+        extended_feature = ivrs_info.extended_feature;
 
         if (!ivhd_blob->len) {
             /*
@@ -1964,9 +1972,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
         /* DeviceID */
         build_append_int_noprefix(table_data, iommu_devid, 2);
         /* Capability offset */
-        build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
+        build_append_int_noprefix(table_data, ivrs_info.pci->capab_offset, 2);
         /* IOMMU base address */
-        build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
+        build_append_int_noprefix(table_data, ivrs_info.base_addr, 8);
         /* PCI Segment Group */
         build_append_int_noprefix(table_data, 0, 2);
         /* IOMMU info */
@@ -1993,15 +2001,15 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
         /* DeviceID */
         build_append_int_noprefix(table_data, iommu_devid, 2);
         /* Capability offset */
-        build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
+        build_append_int_noprefix(table_data, ivrs_info.pci->capab_offset, 2);
         /* IOMMU base address */
-        build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
+        build_append_int_noprefix(table_data, ivrs_info.base_addr, 8);
         /* PCI Segment Group */
         build_append_int_noprefix(table_data, 0, 2);
         /* IOMMU info */
         build_append_int_noprefix(table_data, 0, 2);
         /* IOMMU Attributes */
-        if (!s->iommu.dma_translation) {
+        if (!ivrs_info.dma_translation) {
             build_append_int_noprefix(table_data, (1UL << 0) /* HATDis */, 4);
         } else {
             build_append_int_noprefix(table_data, 0, 4);
diff --git a/hw/i386/amd_iommu-stub.c b/hw/i386/amd_iommu-stub.c
deleted file mode 100644
index fb36dee903ee..000000000000
--- a/hw/i386/amd_iommu-stub.c
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Stubs for AMD IOMMU emulation
- *
- * Copyright (C) 2023 Bui Quang Minh <minhquangbui99@gmail.com>
- *
- * 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; either version 2 of the License, or
- * (at your option) any later version.
-
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-#include "qemu/osdep.h"
-#include "amd_iommu.h"
-
-uint64_t amdvi_extended_feature_register(AMDVIState *s)
-{
-    return 0;
-}
diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index 79d3fcf9fd9b..64009f4908d2 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -133,7 +133,7 @@ FIELD(AMDVI_IRTE_GA_LO, DESTINATION, 8, 24)
 FIELD(AMDVI_IRTE_GA_HI, VECTOR, 0, 8)
 FIELD(AMDVI_IRTE_GA_HI, DESTINATION_HI, 56, 8)
 
-uint64_t amdvi_extended_feature_register(AMDVIState *s)
+static uint64_t amdvi_extended_feature_register(AMDVIState *s)
 {
     uint64_t feature = AMDVI_DEFAULT_EXT_FEATURES;
     if (s->xtsup) {
@@ -2538,6 +2538,15 @@ static int amdvi_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
     return 0;
 }
 
+static void amdvi_get_ivrs_info(AMDVIState *s, AMDIVRSInfo *ivrs_info)
+{
+    ivrs_info->pci = s->pci;
+    ivrs_info->root_bus = s->root_bus;
+    ivrs_info->base_addr = AMDVI_GET_BASE_ADDR(s->iommu.index);
+    ivrs_info->extended_feature = s->extended_feature;
+    ivrs_info->dma_translation = s->iommu.dma_translation;
+}
+
 static void amdvi_init(AMDVIState *s)
 {
     amdvi_iotlb_reset(s);
@@ -2555,12 +2564,13 @@ static void amdvi_init(AMDVIState *s)
     s->cmdbuf_enabled = false;
     s->xten = false;
     s->intcapxten = false;
+    s->extended_feature = amdvi_extended_feature_register(s);
+    s->get_ivrs_info = amdvi_get_ivrs_info;
 
     /* reset MMIO */
     memset(s->mmior, 0, AMDVI_MMIO_SIZE);
     amdvi_set_quad(s, AMDVI_MMIO_EXT_FEATURES,
-                   amdvi_extended_feature_register(s),
-                   0xffffffffffffffef, 0);
+                   s->extended_feature, 0xffffffffffffffef, 0);
     amdvi_set_quad(s, AMDVI_MMIO_STATUS, 0, 0x98, 0x67);
 }
 
diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h
index 383605c38e6c..38ab7aa97c47 100644
--- a/hw/i386/amd_iommu.h
+++ b/hw/i386/amd_iommu.h
@@ -326,6 +326,14 @@ struct AMDVIPCIState {
     uint32_t capab_offset;       /* capability offset pointer    */
 };
 
+typedef struct AMDIVRSInfo {
+    AMDVIPCIState *pci;         /* IOMMU PCI device               */
+    PCIBus *root_bus;           /* root bus to register IOMMU ops */
+    uint64_t base_addr;         /* MMIO region                    */
+    uint64_t extended_feature;  /* Extended feature bits          */
+    bool dma_translation;       /* Host address translation enabled */
+} AMDIVRSInfo;
+
 struct AMDVIState {
     X86IOMMUState iommu;        /* IOMMU bus device             */
     AMDVIPCIState *pci;         /* IOMMU PCI device             */
@@ -395,8 +403,8 @@ struct AMDVIState {
 
     /* root bus to register IOMMU ops */
     PCIBus *root_bus;
+    uint64_t extended_feature;
+    void (*get_ivrs_info)(AMDVIState *s, AMDIVRSInfo *ivrs_info);
 };
 
-uint64_t amdvi_extended_feature_register(AMDVIState *s);
-
 #endif
diff --git a/hw/i386/meson.build b/hw/i386/meson.build
index 6bb73b8731cc..39a2886e7292 100644
--- a/hw/i386/meson.build
+++ b/hw/i386/meson.build
@@ -12,8 +12,7 @@ i386_ss.add(files(
 i386_ss.add(when: 'CONFIG_APIC', if_true: files('vapic.c'))
 i386_ss.add(when: 'CONFIG_X86_IOMMU', if_true: files('x86-iommu.c'),
                                       if_false: files('x86-iommu-stub.c'))
-i386_ss.add(when: 'CONFIG_AMD_IOMMU', if_true: files('amd_iommu.c'),
-                                      if_false: files('amd_iommu-stub.c'))
+i386_ss.add(when: 'CONFIG_AMD_IOMMU', if_true: files('amd_iommu.c'))
 i386_ss.add(when: 'CONFIG_I440FX', if_true: files('pc_piix.c'))
 i386_ss.add(when: 'CONFIG_ISAPC', if_true: files('isapc.c'))
 i386_ss.add(when: 'CONFIG_MICROVM', if_true: files('x86-common.c', 'microvm.c', 'acpi-microvm.c', 'microvm-dt.c'))
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 6/6] x86-iommu: Allow multiple AMD vIOMMU devices
  2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
                   ` (4 preceding siblings ...)
  2026-11-30 10:12 ` [PATCH 5/6] amd_iommu: Introduce AMDIVRSInfo for shared IVRS construction Sairaj Kodilkar
@ 2026-11-30 10:12 ` Sairaj Kodilkar
  5 siblings, 0 replies; 7+ messages in thread
From: Sairaj Kodilkar @ 2026-11-30 10:12 UTC (permalink / raw)
  To: Clément Mathieu--Drif, Michael S. Tsirkin, Alejandro Jimenez,
	Ani Sinha, Igor Mammedov, Jason Wang, Marcelo Tosatti,
	Paolo Bonzini, Richard Henderson, Sairaj Kodilkar, Yi Liu, kvm,
	qemu-devel
  Cc: Vasant.Hegde, Suravee.Suthikulpanit

pc_iommu_pre_plug() used to reject any second vIOMMU unconditionally.
Relax that for TYPE_AMD_IOMMU_DEVICE so several AMD IOMMU instances can
be plugged into the machine.

The check looks at the first vIOMMU as well as the new one, so an AMD
IOMMU cannot be combined with an Intel or virtio IOMMU in either order.
Intel and virtio vIOMMUs are still limited to a single instance.

Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
 hw/i386/pc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 1b1101af0c79..21135f984e8c 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -56,6 +56,7 @@
 #include "hw/cxl/cxl_host.h"
 #include "hw/usb/usb.h"
 #include "hw/i386/intel_iommu.h"
+#include "amd_iommu.h"
 #include "hw/net/ne2000-isa.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/virtio/virtio-md-pci.h"
@@ -1321,11 +1322,14 @@ static void pc_sp_mem_plug(const HotplugHandler *hotplug_dev,
 static void pc_iommu_pre_plug(PCMachineState *pcms, DeviceState *dev,
                               Error **errp)
 {
+    PCIOMMUEntry *first = QTAILQ_FIRST(&pcms->iommus);
     PCIOMMUEntry *entry;
 
-    if (!QTAILQ_EMPTY(&pcms->iommus)) {
-        error_setg(errp, "QEMU does not support multiple vIOMMUs "
-                   "for x86 yet.");
+    if (first &&
+        !(object_dynamic_cast(OBJECT(dev), TYPE_AMD_IOMMU_DEVICE) &&
+          object_dynamic_cast(OBJECT(first->dev), TYPE_AMD_IOMMU_DEVICE))) {
+        error_setg(errp, "QEMU supports multiple vIOMMUs for x86 only with "
+                   "AMD IOMMU, and they cannot be mixed with other types");
         return;
     }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-30 10:18 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 1/6] hw/i386: Track the IOMMU to which IOAPIC is attached Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 2/6] i386/kvm: x86_iommu: Introduce per-iommu notify_list_inited flag Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 3/6] acpi-build: i386/pc: Track vIOMMUs in a list Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 4/6] amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 5/6] amd_iommu: Introduce AMDIVRSInfo for shared IVRS construction Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 6/6] x86-iommu: Allow multiple AMD vIOMMU devices Sairaj Kodilkar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox