Skip to contentSkip to conversation

AMD IOMMU

PATCH Superseded David Kiarie 2 participants 8 messages May 9, 2016 – May 16, 2016

v10 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v10 cover AMD IOMMU

Hi all,

This patches series adds basic AMD IOMMU emulation support to Qemu. It's currently in it's 10th version. Changes since V9 include

-amd_iommu prefixes have been renamed to a shorter 'amdvi' both in the macros
and in the functions/code. The register macros have not been moved to the implementation file since almost the macros there are basically macros and I reckoned renaming them should suffice.
-taken care of byte order in the use of 'dma_memory_read'[Michael] -Taken care of invalid DTE entries which is still subject discussion. I will
make any necessary changes based on discusion outcome.[Jan]
-An issue with the emulate IOMMU defaulting to AMD_IOMMU has be fixed[Marcel]

You can test this patches by starting with parameters
qemu-system-x86_64 -M q35,iommu=on,x-iommu-type=amd -m 2G -enable-kvm -smp 4 -cpu host -hda file.img -soundhw ac97
emulating whatever devices you want.

Not passing any command line parameters to linux should be enough to test this patches since the devices are basically passes-through but to the 'host' (l1 guest). You can still go ahead pass command line parameter 'iommu=pt iommu=1' and try to pass a device to L2 guest. This can also done without passing any iommu related parameters to the kernel.

David Kiarie (4):
hw/i386: Introduce AMD IOMMU
hw/i386: ACPI IVRS table
hw/core: provision for overriding emulated IOMMU hw/pci-host: Emulate AMD IOMMU

 hw/acpi/aml-build.c           |    2 +-
 hw/core/machine.c             |   29 +-
 hw/i386/Makefile.objs         |    1 +
 hw/i386/acpi-build.c          |   93 ++-
 hw/i386/amd_iommu.c           | 1405 +++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h           |  340 ++++++++++
 hw/pci-host/q35.c             |   25 +-
 include/hw/acpi/acpi-defs.h   |   13 +
 include/hw/acpi/aml-build.h   |    1 +
 include/hw/boards.h           |    7 +
 include/hw/i386/intel_iommu.h |    1 +
 include/hw/pci/pci.h          |    2 +
 qemu-options.hx               |    7 +-
 util/qemu-config.c            |    8 +-
 14 files changed, 1912 insertions(+), 22 deletions(-)
 create mode 100644 hw/i386/amd_iommu.c
 create mode 100644 hw/i386/amd_iommu.h
Signature · 1 line
2.1.4
#1 lore raw

PATCH v10 1/4 hw/i386: Introduce AMD IOMMU

Add AMD IOMMU emulaton to Qemu in addition to Intel IOMMU The IOMMU does basic translation, error checking and has a minimal IOTLB implementation. This IOMMU bypassed the need for target aborts by responding with IOMMU_NONE access rights and exempts the region 0xfee00000-0xfeefffff from translation as it is the q35 interrupt region. We also advertise features that are not yet implemented to please the Linux IOMMU driver.

IOTLB aims at implementing commands on real IOMMUs which is essential for debugging and may not offer any performance benefits.

---
 hw/i386/Makefile.objs |    1 +
 hw/i386/amd_iommu.c   | 1405 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h   |  340 ++++++++++++
 include/hw/pci/pci.h  |    2 +
 4 files changed, 1748 insertions(+)
 create mode 100644 hw/i386/amd_iommu.c
 create mode 100644 hw/i386/amd_iommu.h
hw/i386/Makefile.objs +1 -0
index b52d5b8..2f1a265 100644
@@ -3,6 +3,7 @@ obj-y += multiboot.o
 obj-y += pc.o pc_piix.o pc_q35.o
 obj-y += pc_sysfw.o
 obj-y += intel_iommu.o
+obj-y += amd_iommu.o
 obj-$(CONFIG_XEN) += ../xenpv/ xen/
 
 obj-y += kvmvapic.o
hw/i386/amd_iommu.c new file +1405 -0

1,405 lines not on this page · raw message

hw/i386/amd_iommu.h new file +340 -0

340 lines not on this page · raw message

include/hw/pci/pci.h +2 -0
index ef6ba51..266cd7e 100644
@@ -12,6 +12,8 @@
 
 /* PCI bus */
 
+#define PCI_BUS_NUM(x)          (((x) >> 8) & 0xff)
+#define PCI_DEVID(bus, devfn)   ((((uint16_t)(bus)) << 8) | (devfn))
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
 #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
 #define PCI_FUNC(devfn)         ((devfn) & 0x07)
Signature · 1 line
2.1.4
#2 lore raw

Re: [Qemu-devel] [V10 1/4] hw/i386: Introduce AMD IOMMU

Quoted 27 lines · David Kiarie
hw/i386/amd_iommu.c new file +27 -0
+static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
+                           IOMMUTLBEntry *ret, unsigned perms,
+                           hwaddr addr)
+{
+    unsigned level, present, pte_perms;
+    uint64_t pte = dte[0], pte_addr;
+
+    /* make sure the DTE has TV = 1 */
+    if (pte & AMDVI_DEV_TRANSLATION_VALID) {
+        level = get_pte_translation_mode(pte);
+        if (level >= 7) {
+            AMDVI_DPRINTF(MMU, "error: translation level 0x%"PRIu8 " detected"
+                    " while translating 0x%"PRIx64, level, addr);
+            return;
+        }
+        if (level == 0) {
+            goto no_remap;
+        }
+
+        while (level > 0) {
+            pte_perms = amdvi_get_perms(pte);
+            present = pte & 1;
+            if (!present || perms != (perms & pte_perms)) {
+                amdvi_page_fault(as->iommu_state, as->devfn, addr, perms);
+                AMDVI_DPRINTF(MMU, "error: page fault accessing virtual addr "
+                              "0x%"PRIx64, addr);
+                return;

hw/i386/amd_iommu.c line 1170

Quoted 10 lines
hw/i386/amd_iommu.c new file +10 -0
+            }
+
+            /* go to the next lower level */
+            pte_addr = pte & AMDVI_DEV_PT_ROOT_MASK;
+            /* add offset and load pte */
+            pte_addr += ((addr >> (3 + 9 * level)) & 0x1FF) << 3;
+            pte = ldq_phys(&address_space_memory, pte_addr);
+            level = get_pte_translation_mode(pte);
+        }
+        /* get access permissions from pte */

That comment is only addressing the last assignment of the followings.

hw/i386/amd_iommu.c line 1174

Quoted 4 lines
hw/i386/amd_iommu.c new file +4 -0
+        ret->iova = addr & AMDVI_PAGE_MASK_4K;
+        ret->translated_addr = (pte & AMDVI_DEV_PT_ROOT_MASK) &
+                                AMDVI_PAGE_MASK_4K;
+        ret->addr_mask = ~AMDVI_PAGE_MASK_4K;

This does not take huge pages (2M, 1G, ...) into account. Jailhouse creates them, and its Linux guest goes mad. You need to use the correct page size here, analogously to intel_iommu.c.

Quoted 50 lines
hw/i386/amd_iommu.c new file +50 -0
+        ret->perm = amdvi_get_perms(pte);
+        return;
+    }
+
+no_remap:
+    ret->iova = addr & AMDVI_PAGE_MASK_4K;
+    ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
+    ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
+    ret->perm = amdvi_get_perms(pte);
+
+}
+
+/* TODO : Mark addresses as Accessed and Dirty */
+static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
+                               bool is_write, IOMMUTLBEntry *ret)
+{
+    AMDVIState *s = as->iommu_state;
+    uint16_t devid = PCI_DEVID(as->bus_num, as->devfn);
+    AMDVIIOTLBEntry *iotlb_entry = amdvi_iotlb_lookup(s, addr, as->devfn);
+    uint64_t entry[4];
+
+    if (iotlb_entry) {
+        AMDVI_DPRINTF(CACHE, "hit  iotlb devid: %02x:%02x.%x gpa 0x%"PRIx64
+                      " hpa 0x%"PRIx64, PCI_BUS_NUM(devid), PCI_SLOT(devid),
+                      PCI_FUNC(devid), addr, iotlb_entry->translated_addr);
+        ret->iova = addr & AMDVI_PAGE_MASK_4K;
+        ret->translated_addr = iotlb_entry->translated_addr;
+        ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
+        ret->perm = iotlb_entry->perms;
+        return;
+    }
+
+    /* devices with V = 0 are not translated */
+    if (!amdvi_get_dte(s, devid, entry)) {
+        goto out;
+    }
+
+    amdvi_page_walk(as, entry, ret,
+                    is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr);
+
+    amdvi_update_iotlb(s, as->devfn, addr, ret->translated_addr,
+                       ret->perm, entry[1] & AMDVI_DEV_DOMID_ID_MASK);
+    return;
+
+out:
+    ret->iova = addr & AMDVI_PAGE_MASK_4K;
+    ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
+    ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
+    ret->perm = IOMMU_RW;
+}

hw/i386/amd_iommu.c line 1234

Quoted 10 lines
hw/i386/amd_iommu.c new file +10 -0
+
+static inline bool amdvi_is_interrupt_addr(hwaddr addr)
+{
+    return addr >= AMDVI_INT_ADDR_FIRST && addr <= AMDVI_INT_ADDR_LAST;
+}
+
+static IOMMUTLBEntry amdvi_translate(MemoryRegion *iommu, hwaddr addr,
+                                     bool is_write)
+{
+    AMDVI_DPRINTF(GENERAL, "");

Not a very helpful instrumentation, I would say.

Quoted 16 lines
hw/i386/amd_iommu.c new file +16 -0
+
+    AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
+    AMDVIState *s = as->iommu_state;
+    IOMMUTLBEntry ret = {
+        .target_as = &address_space_memory,
+        .iova = addr,
+        .translated_addr = 0,
+        .addr_mask = ~(hwaddr)0,
+        .perm = IOMMU_NONE
+    };
+
+    if (!s->enabled || amdvi_is_interrupt_addr(addr)) {
+        /* AMDVI disabled - corresponds to iommu=off not
+         * failure to provide any parameter
+         */
+        ret.iova = addr & AMDVI_PAGE_MASK_4K;

hw/i386/amd_iommu.c line 1260

Quoted 10 lines
hw/i386/amd_iommu.c new file +10 -0
+        ret.translated_addr = addr & AMDVI_PAGE_MASK_4K;
+        ret.addr_mask = ~AMDVI_PAGE_MASK_4K;
+        ret.perm = IOMMU_RW;
+        return ret;
+    }
+
+    amdvi_do_translate(as, addr, is_write, &ret);
+    AMDVI_DPRINTF(MMU, "devid: %02x:%02x.%x gpa 0x%"PRIx64 " hpa 0x%"PRIx64,
+                  as->bus_num, PCI_SLOT(as->devfn), PCI_FUNC(as->devfn), addr,
+                  ret.translated_addr);

Tracing permission here in addition would be good.

Jan

#3 lore raw

Re: [Qemu-devel] [V10 1/4] hw/i386: Introduce AMD IOMMU

Quoted 2 lines · Jan Kiszka
Quoted 1 line · David Kiarie

+

Thanks for review and testing!

Quoted 11 lines

hw/i386/amd_iommu.c line 1170

Quoted 8 lines
hw/i386/amd_iommu.c new file +8 -0
+            /* go to the next lower level */
+            pte_addr = pte & AMDVI_DEV_PT_ROOT_MASK;
+            /* add offset and load pte */
+            pte_addr += ((addr >> (3 + 9 * level)) & 0x1FF) << 3;
+            pte = ldq_phys(&address_space_memory, pte_addr);
+            level = get_pte_translation_mode(pte);
+        }
+        /* get access permissions from pte */

That comment is only addressing the last assignment of the followings.

Quoted 8 lines

hw/i386/amd_iommu.c line 1174

Quoted 4 lines
hw/i386/amd_iommu.c new file +4 -0
+        ret->iova = addr & AMDVI_PAGE_MASK_4K;
+        ret->translated_addr = (pte & AMDVI_DEV_PT_ROOT_MASK) &
+                                AMDVI_PAGE_MASK_4K;
+        ret->addr_mask = ~AMDVI_PAGE_MASK_4K;

This does not take huge pages (2M, 1G, ...) into account. Jailhouse creates them, and its Linux guest goes mad. You need to use the correct page size here, analogously to intel_iommu.c.

Yes, this was meant to work with normal pages only. Until recently intel iommu supported 4k pages only so I figured I could as well work with 4k pages. Anyway, will fix this.

Quoted 55 lines
Quoted 55 lines
hw/i386/amd_iommu.c new file +54 -0
+        ret->perm = amdvi_get_perms(pte);
+        return;
+    }
+
+no_remap:
+    ret->iova = addr & AMDVI_PAGE_MASK_4K;
+    ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
+    ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
+    ret->perm = amdvi_get_perms(pte);
+
+}
+
+/* TODO : Mark addresses as Accessed and Dirty */
+static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
+                               bool is_write, IOMMUTLBEntry *ret)
+{
+    AMDVIState *s = as->iommu_state;
+    uint16_t devid = PCI_DEVID(as->bus_num, as->devfn);
+    AMDVIIOTLBEntry *iotlb_entry = amdvi_iotlb_lookup(s, addr, as->devfn);
+    uint64_t entry[4];
+
+    if (iotlb_entry) {
+        AMDVI_DPRINTF(CACHE, "hit  iotlb devid: %02x:%02x.%x gpa 0x%"PRIx64
+                      " hpa 0x%"PRIx64, PCI_BUS_NUM(devid), PCI_SLOT(devid),
+                      PCI_FUNC(devid), addr, iotlb_entry->translated_addr);
+        ret->iova = addr & AMDVI_PAGE_MASK_4K;
+        ret->translated_addr = iotlb_entry->translated_addr;
+        ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
+        ret->perm = iotlb_entry->perms;
+        return;
+    }
+
+    /* devices with V = 0 are not translated */
+    if (!amdvi_get_dte(s, devid, entry)) {
+        goto out;
+    }
+
+    amdvi_page_walk(as, entry, ret,
+                    is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr);
+
+    amdvi_update_iotlb(s, as->devfn, addr, ret->translated_addr,
+                       ret->perm, entry[1] & AMDVI_DEV_DOMID_ID_MASK);
+    return;
+
+out:
+    ret->iova = addr & AMDVI_PAGE_MASK_4K;
+    ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
+    ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
+    ret->perm = IOMMU_RW;
+}
+
+static inline bool amdvi_is_interrupt_addr(hwaddr addr)
+{
+    return addr >= AMDVI_INT_ADDR_FIRST && addr <= AMDVI_INT_ADDR_LAST;
Quoted 8 lines

hw/i386/amd_iommu.c line 1234

Quoted 6 lines
hw/i386/amd_iommu.c new file +6 -0
+}
+
+static IOMMUTLBEntry amdvi_translate(MemoryRegion *iommu, hwaddr addr,
+                                     bool is_write)
+{
+    AMDVI_DPRINTF(GENERAL, "");

Not a very helpful instrumentation, I would say.

It was helpful in the initial stages of development, not very helpful now. I could get rid of such.

Quoted 32 lines
Quoted 17 lines
hw/i386/amd_iommu.c new file +16 -0
+
+    AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
+    AMDVIState *s = as->iommu_state;
+    IOMMUTLBEntry ret = {
+        .target_as = &address_space_memory,
+        .iova = addr,
+        .translated_addr = 0,
+        .addr_mask = ~(hwaddr)0,
+        .perm = IOMMU_NONE
+    };
+
+    if (!s->enabled || amdvi_is_interrupt_addr(addr)) {
+        /* AMDVI disabled - corresponds to iommu=off not
+         * failure to provide any parameter
+         */
+        ret.iova = addr & AMDVI_PAGE_MASK_4K;

hw/i386/amd_iommu.c line 1260

Quoted 10 lines
hw/i386/amd_iommu.c new file +10 -0
+        ret.translated_addr = addr & AMDVI_PAGE_MASK_4K;
+        ret.addr_mask = ~AMDVI_PAGE_MASK_4K;
+        ret.perm = IOMMU_RW;
+        return ret;
+    }
+
+    amdvi_do_translate(as, addr, is_write, &ret);
+    AMDVI_DPRINTF(MMU, "devid: %02x:%02x.%x gpa 0x%"PRIx64 " hpa 0x%"PRIx64,
+                  as->bus_num, PCI_SLOT(as->devfn), PCI_FUNC(as->devfn), addr,
+                  ret.translated_addr);

Tracing permission here in addition would be good.

Jan

#4 lore raw

Re: [Qemu-devel] [V10 1/4] hw/i386: Introduce AMD IOMMU

Quoted 7 lines · David Kiarie
Quoted 6 lines · Jan Kiszka
Quoted 4 lines · David Kiarie
hw/i386/amd_iommu.c new file +4 -0
+        ret->iova = addr & AMDVI_PAGE_MASK_4K;
+        ret->translated_addr = (pte & AMDVI_DEV_PT_ROOT_MASK) &
+                                AMDVI_PAGE_MASK_4K;
+        ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
Quoted 7 lines
Quoted 3 lines

This does not take huge pages (2M, 1G, ...) into account. Jailhouse creates them, and its Linux guest goes mad. You need to use the correct page size here, analogously to intel_iommu.c.

Yes, this was meant to work with normal pages only. Until recently intel iommu supported 4k pages only so I figured I could as well work with 4k pages. Anyway, will fix this.

Huge pages are optional on Intel. Not AMD.

Jan

#5 lore raw

PATCH v10 2/4 hw/i386: ACPI IVRS table

In reply to David Kiarie

Add IVRS table for AMD IOMMU. Generate IVRS or DMAR depending on emulated IOMMU.

---
 hw/acpi/aml-build.c         |  2 +-
 hw/i386/acpi-build.c        | 93 +++++++++++++++++++++++++++++++++++++++------
 include/hw/acpi/acpi-defs.h | 13 +++++++
 include/hw/acpi/aml-build.h |  1 +
 include/hw/boards.h         |  6 +++
 5 files changed, 103 insertions(+), 12 deletions(-)
hw/acpi/aml-build.c +1 -1
index ab89ca6..da11bf8 100644
@@ -227,7 +227,7 @@ static void build_extop_package(GArray *package, uint8_t op)
     build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
 }
 
-static void build_append_int_noprefix(GArray *table, uint64_t value, int size)
+void build_append_int_noprefix(GArray *table, uint64_t value, int size)
 {
     int i;
 
hw/i386/acpi-build.c +82 -11

129 lines not on this page · raw message

include/hw/acpi/acpi-defs.h +13 -0
index c7a03d4..8736165 100644
@@ -570,4 +570,17 @@ typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit;
 /* Masks for Flags field above */
 #define ACPI_DMAR_INCLUDE_PCI_ALL   1
 
+/* IVRS constants */
+#define AMD_IOMMU_HOST_ADDRESS_WIDTH 40UL
+
+/* flags in the IVHD headers */
+#define IVHD_HT_TUNEN    (1UL << 0) /* recommended setting for HtTunEn */
+#define IVHD_IOTLBSUP    (1UL << 4) /* remote IOTLB support            */
+#define IVHD_PREFSUP     (1UL << 6) /* page prefetch support           */
+#define IVHD_PPRSUP      (1UL << 7) /* peripheral page service support */
+
+#define IVHD_EFR_HATS       48      /* host address translation size  */
+#define IVHD_EFR_GATS       48      /* guest address translation size */
+#define IVHD_EFR_GTSUP  (1UL << 2)  /* guest translation support      */
+
 #endif
include/hw/acpi/aml-build.h +1 -0
index 2c994b3..d25e9a5 100644
@@ -355,6 +355,7 @@ Aml *aml_derefof(Aml *arg);
 Aml *aml_sizeof(Aml *arg);
 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target);
 
+void build_append_int_noprefix(GArray *table, uint64_t value, int size);
 void
 build_header(GArray *linker, GArray *table_data,
              AcpiTableHeader *h, const char *sig, int len, uint8_t rev,
include/hw/boards.h +6 -0
index 8d4fe56..dbe6745 100644
@@ -61,6 +61,12 @@ typedef struct {
     CPUArchId cpus[0];
 } CPUArchIdList;
 
+typedef enum IommuType {
+    TYPE_AMD,
+    TYPE_INTEL,
+    TYPE_NONE
+} IommuType;
+
 /**
  * MachineClass:
  * @get_hotplug_handler: this function is called during bus-less
Signature · 1 line
2.1.4
#6 lore raw

PATCH v10 3/4 hw/core: provision for overriding emulated IOMMU

In reply to David Kiarie

Added an enum, subject to review, to machine properties which it used to override iommu emulated from Intel to AMD.

---
 hw/core/machine.c             | 29 ++++++++++++++++++++++++++---
 include/hw/boards.h           |  1 +
 include/hw/i386/intel_iommu.h |  1 +
 qemu-options.hx               |  7 +++++--
 util/qemu-config.c            |  8 ++++++--
 5 files changed, 39 insertions(+), 7 deletions(-)
hw/core/machine.c +26 -3
index 6dbbc85..fe44e25 100644
@@ -15,6 +15,8 @@
 #include "qapi/error.h"
 #include "qapi-visit.h"
 #include "qapi/visitor.h"
+#include "hw/i386/amd_iommu.h"
+#include "hw/i386/intel_iommu.h"
 #include "hw/sysbus.h"
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
@@ -297,9 +299,26 @@ static void machine_set_iommu(Object *obj, bool value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
 
+    ms->iommu_type = TYPE_INTEL;
     ms->iommu = value;
 }
 
+static void machine_set_iommu_override(Object *obj, const char *value,
+                                       Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+
+    /* ensure a valid iommu type */
+    if (g_strcmp0(value, AMD_IOMMU_STR) == 0) {
+        ms->iommu_type = TYPE_AMD;
+    } else if (g_strcmp0(value, INTEL_IOMMU_STR) == 0) {
+        ms->iommu_type = TYPE_INTEL;
+    } else {
+        error_setg(errp, "Invalid IOMMU type %s", value);
+        return;
+    }
+}
+
 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
@@ -473,10 +492,14 @@ static void machine_initfn(Object *obj)
                                     "Firmware image",
                                     NULL);
     object_property_add_bool(obj, "iommu",
-                             machine_get_iommu,
-                             machine_set_iommu, NULL);
+                             machine_get_iommu, machine_set_iommu, NULL);
     object_property_set_description(obj, "iommu",
-                                    "Set on/off to enable/disable Intel IOMMU (VT-d)",
+                                    "Set on to enable IOMMU emulation",
+                                    NULL);
+    object_property_add_str(obj, "x-iommu-type",
+                            NULL, machine_set_iommu_override, NULL);
+    object_property_set_description(obj, "x-iommu-type",
+                                    "Set on to override emulated IOMMU to AMD IOMMU",
                                     NULL);
     object_property_add_bool(obj, "suppress-vmdesc",
                              machine_get_suppress_vmdesc,
include/hw/boards.h +1 -0
index dbe6745..5b7eeda 100644
@@ -158,6 +158,7 @@ struct MachineState {
     bool igd_gfx_passthru;
     char *firmware;
     bool iommu;
+    IommuType iommu_type;
     bool suppress_vmdesc;
     bool enforce_config_section;
 
include/hw/i386/intel_iommu.h +1 -0
index b024ffa..539530c 100644
@@ -27,6 +27,7 @@
 #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"
 #define INTEL_IOMMU_DEVICE(obj) \
      OBJECT_CHECK(IntelIOMMUState, (obj), TYPE_INTEL_IOMMU_DEVICE)
+#define INTEL_IOMMU_STR "intel"
 
 /* DMAR Hardware Unit Definition address (IOMMU unit) */
 #define Q35_HOST_BRIDGE_IOMMU_ADDR  0xfed90000ULL
qemu-options.hx +5 -2
index 6106520..81217d3 100644
@@ -38,7 +38,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                kvm_shadow_mem=size of KVM shadow MMU\n"
     "                dump-guest-core=on|off include guest memory in a core dump (default=on)\n"
     "                mem-merge=on|off controls memory merge support (default: on)\n"
-    "                iommu=on|off controls emulated Intel IOMMU (VT-d) support (default=off)\n"
+    "                iommu=on|off controls emulated IOMMU support(default: off)\n"
+    "                x-iommu-type=amd|intel overrides emulated IOMMU to AMD IOMMU (default: intel)\n"
     "                igd-passthru=on|off controls IGD GFX passthrough support (default=off)\n"
     "                aes-key-wrap=on|off controls support for AES key wrapping (default=on)\n"
     "                dea-key-wrap=on|off controls support for DEA key wrapping (default=on)\n"
@@ -74,7 +75,9 @@ Enables or disables memory merge support. This feature, when supported by
 the host, de-duplicates identical memory pages among VMs instances
 (enabled by default).
 @item iommu=on|off
-Enables or disables emulated Intel IOMMU (VT-d) support. The default is off.
+Enables and disables IOMMU emulation. The default is off.
+@item x-iommu-type=on|off
+Overrides emulated IOMMU from AMD IOMMU. By default Intel IOMMU is emulated.
 @item aes-key-wrap=on|off
 Enables or disables AES key wrapping support on s390-ccw hosts. This feature
 controls whether AES wrapping keys will be created to allow
util/qemu-config.c +6 -2
index fb97307..8886abf 100644
@@ -213,8 +213,12 @@ static QemuOptsList machine_opts = {
             .help = "firmware image",
         },{
             .name = "iommu",
-            .type = QEMU_OPT_BOOL,
-            .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
+            .type =  QEMU_OPT_BOOL,
+            .help = "Set on/off to enable iommu",
+        },{
+            .name = "x-iommu-type",
+            .type =  QEMU_OPT_STRING,
+            .help = "Overrides emulated IOMMU from Intel to AMD",
         },{
             .name = "suppress-vmdesc",
             .type = QEMU_OPT_BOOL,
Signature · 1 line
2.1.4
#7 lore raw

PATCH v10 4/4 hw/pci-host: Emulate AMD IOMMU

In reply to David Kiarie

Add AMD IOMMU emulation support to q35 chipset

---
 hw/pci-host/q35.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
hw/pci-host/q35.c +22 -3
index 70f897e..26fea0e 100644
@@ -32,6 +32,7 @@
 #include "hw/pci-host/q35.h"
 #include "qapi/error.h"
 #include "qapi/visitor.h"
+#include "hw/i386/amd_iommu.h"
 
 /****************************************************************************
  * Q35 host
@@ -448,6 +449,19 @@ static void mch_init_dmar(MCHPCIState *mch)
     pci_setup_iommu(pci_bus, q35_host_dma_iommu, mch->iommu);
 }
 
+static void mch_init_amdvi(MCHPCIState *mch)
+{
+    AMDVIState *iommu_state;
+    PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(mch)));
+    PCIDevice *iommu;
+
+    iommu = pci_create_simple(bus, 0x20, TYPE_AMD_IOMMU_DEVICE);
+
+    iommu_state = AMD_IOMMU_DEVICE(iommu);
+
+    pci_setup_iommu(bus, bridge_host_amdvi, iommu_state);
+}
+
 static void mch_realize(PCIDevice *d, Error **errp)
 {
     int i;
@@ -506,9 +520,14 @@ static void mch_realize(PCIDevice *d, Error **errp)
                  mch->pci_address_space, &mch->pam_regions[i+1],
                  PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE, PAM_EXPAN_SIZE);
     }
-    /* Intel IOMMU (VT-d) */
-    if (object_property_get_bool(qdev_get_machine(), "iommu", NULL)) {
-        mch_init_dmar(mch);
+
+    MachineState *machine = MACHINE(qdev_get_machine());
+    if (machine->iommu) {
+        if (machine->iommu_type == TYPE_AMD) {
+            mch_init_amdvi(mch);
+        } else {
+            mch_init_dmar(mch);
+        }
     }
 }
 
Signature · 1 line
2.1.4