From: Sairaj Kodilkar <sarunkod@amd.com>
To: "Clément Mathieu--Drif" <clement.mathieu--drif@bull.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Alejandro Jimenez" <alejandro.j.jimenez@oracle.com>,
"Ani Sinha" <anisinha@redhat.com>,
"Igor Mammedov" <imammedo@redhat.com>,
"Jason Wang" <jasowangio@gmail.com>,
"Marcelo Tosatti" <mtosatti@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Sairaj Kodilkar" <sarunkod@amd.com>,
"Yi Liu" <yi.l.liu@intel.com>,
kvm@vger.kernel.org, qemu-devel@nongnu.org
Cc: <Vasant.Hegde@amd.com>, <Suravee.Suthikulpanit@amd.com>
Subject: [PATCH 4/6] amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs
Date: Mon, 30 Nov 2026 15:42:35 +0530 [thread overview]
Message-ID: <20261130101237.1416-5-sarunkod@amd.com> (raw)
In-Reply-To: <20261130101237.1416-1-sarunkod@amd.com>
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
next prev parent reply other threads:[~2026-11-30 10:17 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Sairaj Kodilkar [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130101237.1416-5-sarunkod@amd.com \
--to=sarunkod@amd.com \
--cc=Suravee.Suthikulpanit@amd.com \
--cc=Vasant.Hegde@amd.com \
--cc=alejandro.j.jimenez@oracle.com \
--cc=anisinha@redhat.com \
--cc=clement.mathieu--drif@bull.com \
--cc=imammedo@redhat.com \
--cc=jasowangio@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=mst@redhat.com \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=yi.l.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox