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 --- 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