On Fri, 1 Oct 2021 18:33:55 +0100 Jean-Philippe Brucker <jean-phili...@linaro.org> wrote:
> Allow instantiating a virtio-iommu device by adding an ACPI Virtual I/O > Translation table (VIOT), which describes the relation between the > virtio-iommu and the endpoints it manages. > > Add a hotplug handler for virtio-iommu on x86 and set the necessary > reserved region property. On x86, the [0xfee00000, 0xfeefffff] DMA > region is reserved for MSIs. DMA transactions to this range either > trigger IRQ remapping in the IOMMU or bypasses IOMMU translation. > > Although virtio-iommu does not support IRQ remapping it must be informed > of the reserved region so that it can forward DMA transactions targeting > this region. > > Signed-off-by: Jean-Philippe Brucker <jean-phili...@linaro.org> > --- > include/hw/i386/pc.h | 2 ++ > hw/i386/acpi-build.c | 5 +++++ > hw/i386/pc.c | 24 ++++++++++++++++++++++-- > hw/i386/Kconfig | 1 + > 4 files changed, 30 insertions(+), 2 deletions(-) > > diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h > index 82cf7b7e30..f3ba1ee4c0 100644 > --- a/include/hw/i386/pc.h > +++ b/include/hw/i386/pc.h > @@ -45,6 +45,8 @@ typedef struct PCMachineState { > bool pit_enabled; > bool hpet_enabled; > bool default_bus_bypass_iommu; > + bool virtio_iommu; > + uint16_t virtio_iommu_bdf; > uint64_t max_fw_size; > > /* ACPI Memory hotplug IO base address */ > diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c > index d1c28440f4..4e46585709 100644 > --- a/hw/i386/acpi-build.c > +++ b/hw/i386/acpi-build.c > @@ -71,6 +71,7 @@ > > #include "hw/acpi/ipmi.h" > #include "hw/acpi/hmat.h" > +#include "hw/acpi/viot.h" > > /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and > * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows > @@ -2593,6 +2594,10 @@ void acpi_build(AcpiBuildTables *tables, MachineState > *machine) > build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id, > x86ms->oem_table_id); > } > + } else if (pcms->virtio_iommu) { > + acpi_add_table(table_offsets, tables_blob); > + build_viot(machine, tables_blob, tables->linker, > pcms->virtio_iommu_bdf, I'd drop PCMachineState::virtio_iommu_bdf and s/pcms->virtio_iommu_bdf/pci_get_bdf(iommu)/ > + x86ms->oem_id, x86ms->oem_table_id); > } > if (machine->nvdimms_state->is_enabled) { > nvdimm_build_acpi(table_offsets, tables_blob, tables->linker, > diff --git a/hw/i386/pc.c b/hw/i386/pc.c > index 789ccb6ef4..31710bc4fb 100644 > --- a/hw/i386/pc.c > +++ b/hw/i386/pc.c > @@ -83,6 +83,7 @@ > #include "hw/i386/intel_iommu.h" > #include "hw/net/ne2000-isa.h" > #include "standard-headers/asm-x86/bootparam.h" > +#include "hw/virtio/virtio-iommu.h" > #include "hw/virtio/virtio-pmem-pci.h" > #include "hw/virtio/virtio-mem-pci.h" > #include "hw/mem/memory-device.h" > @@ -1367,8 +1368,11 @@ static void pc_virtio_md_pci_unplug(HotplugHandler > *hotplug_dev, > static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev, > DeviceState *dev, Error **errp) > { > - if (object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE) && > - x86_iommu_get_default()) { > + PCMachineState *pcms = PC_MACHINE(hotplug_dev); > + > + if ((object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE) || > + object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) && > + (x86_iommu_get_default() || pcms->virtio_iommu)) { this check is getting uglier, may be instead of introducing pcms->virtio_iommu boolean, better approach would be adding 'Device* PCMachineState::iommu' and setting it to IOMMU so the check would reduce to: if ((object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE) || object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI))) { if (pcms->iommu) err else set pcms->iommu in plug handler or here } that also will let to cleanup/get rid of x86_iommu_[s|g]et_default() and x86_iommu_default 'global'. Maybe replace previous patch with one that would remove x86_iommu_[s|g]et_default(). > error_setg(errp, "QEMU does not support multiple vIOMMUs " > "for x86 yet."); > return; > @@ -1381,6 +1385,15 @@ static void > pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev, > } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) || > object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) { > pc_virtio_md_pci_pre_plug(hotplug_dev, dev, errp); > + } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { > + /* Declare the APIC range as the reserved MSI region */ > + char *resv_prop_str = g_strdup_printf("0xfee00000:0xfeefffff:%d", > + VIRTIO_IOMMU_RESV_MEM_T_MSI); > + > + object_property_set_uint(OBJECT(dev), "len-reserved-regions", 1, > errp); > + object_property_set_str(OBJECT(dev), "reserved-regions[0]", > + resv_prop_str, errp); > + g_free(resv_prop_str); > } > } > > @@ -1394,6 +1407,12 @@ static void pc_machine_device_plug_cb(HotplugHandler > *hotplug_dev, > } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) || > object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI)) { > pc_virtio_md_pci_plug(hotplug_dev, dev, errp); > + } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) { > + PCMachineState *pcms = PC_MACHINE(hotplug_dev); > + PCIDevice *pdev = PCI_DEVICE(dev); > + > + pcms->virtio_iommu = true; > + pcms->virtio_iommu_bdf = pci_get_bdf(pdev); > } > } > > @@ -1436,6 +1455,7 @@ static HotplugHandler > *pc_get_hotplug_handler(MachineState *machine, > object_dynamic_cast(OBJECT(dev), TYPE_CPU) || > object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI) || > object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MEM_PCI) || > + object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) || > object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE)) { > return HOTPLUG_HANDLER(machine); > } > diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig > index ddedcef0b2..13db05d557 100644 > --- a/hw/i386/Kconfig > +++ b/hw/i386/Kconfig > @@ -54,6 +54,7 @@ config PC_ACPI > select ACPI_X86 > select ACPI_CPU_HOTPLUG > select ACPI_MEMORY_HOTPLUG > + select ACPI_VIOT > select SMBUS_EEPROM > select PFLASH_CFI01 > depends on ACPI_SMBUS