Skip to contentSkip to conversation

AMD IOMMU

PATCH Superseded David Kiarie 3 participants 9 messages Apr 1, 2016 – Apr 6, 2016

v8 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v8 cover AMD IOMMU

Hello all,

Long time no see, at usual ;)

AMD IOMMU patches fixing a few issues mentioned in previous version, formatting errors and commit messages

David Kiarie (4):
hw/i386: Introduce AMD IOMMU
hw/i386: ACPI table for AMD IOMMU
hw/core: Add AMD IOMMU to machine properties
hw/pci-host: Emulate AMD IOMMU

 hw/core/machine.c             |   32 +-
 hw/i386/Makefile.objs         |    1 +
 hw/i386/acpi-build.c          |   98 ++-
 hw/i386/amd_iommu.c           | 1426 +++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h           |  398 ++++++++++++
 hw/pci-host/q35.c             |   21 +-
 include/hw/acpi/acpi-defs.h   |   55 ++
 include/hw/boards.h           |    1 +
 include/hw/i386/intel_iommu.h |    2 +
 include/hw/pci/pci.h          |    2 +
 qemu-options.hx               |    7 +-
 util/qemu-config.c            |    8 +-
 12 files changed, 2031 insertions(+), 20 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 v8 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

---
 hw/i386/Makefile.objs |    1 +
 hw/i386/amd_iommu.c   | 1426 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h   |  398 ++++++++++++++
 include/hw/pci/pci.h  |    2 +
 4 files changed, 1827 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 +1426 -0

1,426 lines not on this page · raw message

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

398 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

PATCH v8 2/4 hw/i386: ACPI table for AMD IOMMU

In reply to David Kiarie

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

---
 hw/i386/acpi-build.c          | 98 ++++++++++++++++++++++++++++++++++++++-----
 include/hw/acpi/acpi-defs.h   | 55 ++++++++++++++++++++++++
 include/hw/i386/intel_iommu.h |  1 +
 3 files changed, 143 insertions(+), 11 deletions(-)
hw/i386/acpi-build.c +87 -11
index 35180ef..a54a22c 100644
@@ -52,6 +52,7 @@
 #include "hw/pci/pci_bus.h"
 #include "hw/pci-host/q35.h"
 #include "hw/i386/intel_iommu.h"
+#include "hw/i386/amd_iommu.h"
 #include "hw/timer/hpet.h"
 
 #include "hw/acpi/aml-build.h"
@@ -118,6 +119,12 @@ typedef struct AcpiBuildPciBusHotplugState {
     bool pcihp_bridge_en;
 } AcpiBuildPciBusHotplugState;
 
+typedef enum iommu_type {
+    TYPE_AMD,
+    TYPE_INTEL,
+    TYPE_NONE
+} iommu_type;
+
 static void acpi_get_pm_info(AcpiPmInfo *pm)
 {
     Object *piix = piix4_pm_find();
@@ -2588,6 +2595,78 @@ build_dmar_q35(GArray *table_data, GArray *linker)
                  "DMAR", table_data->len - dmar_start, 1, NULL, NULL);
 }
 
+static void
+build_amd_iommu(GArray *table_data, GArray *linker)
+{
+    int iommu_start = table_data->len;
+    bool iommu_ambig;
+
+    AcpiAMDIOMMUIVRS *ivrs;
+    AcpiAMDIOMMUHardwareUnit *iommu;
+
+    /* IVRS definition */
+    ivrs = acpi_data_push(table_data, sizeof(*ivrs));
+    ivrs->revision = cpu_to_le16(ACPI_IOMMU_IVRS_TYPE);
+    ivrs->length = cpu_to_le16((sizeof(*ivrs) + sizeof(*iommu)));
+    ivrs->v_common_info = cpu_to_le64(AMD_IOMMU_HOST_ADDRESS_WIDTH << 8);
+
+    AMDIOMMUState *s = (AMDIOMMUState *)object_resolve_path_type("",
+                        TYPE_AMD_IOMMU_DEVICE, &iommu_ambig);
+
+    /* IVDB definition - type 10h */
+    iommu = acpi_data_push(table_data, sizeof(*iommu));
+    if (!iommu_ambig) {
+        iommu->type = cpu_to_le16(0x10);
+        /* IVHD flags */
+        iommu->flags = cpu_to_le16(iommu->flags);
+        iommu->flags = cpu_to_le16(IVHD_HT_TUNEN | IVHD_PPRSUP | IVHD_IOTLBSUP
+                       | IVHD_PREFSUP);
+        iommu->length = cpu_to_le16(sizeof(*iommu));
+        iommu->device_id = cpu_to_le16(PCI_DEVICE_ID_RD890_IOMMU);
+        iommu->capability_offset = cpu_to_le16(s->capab_offset);
+        iommu->mmio_base = cpu_to_le64(s->mmio.addr);
+        iommu->pci_segment = 0;
+        iommu->interrupt_info = 0;
+        /* EFR features */
+        iommu->efr_register = cpu_to_le64(IVHD_EFR_GTSUP | IVHD_EFR_HATS
+                              | IVHD_EFR_GATS);
+        iommu->efr_register = cpu_to_le64(iommu->efr_register);
+        /* device entries */
+        memset(iommu->dev_entries, 0, 20);
+        /* Add device flags here
+         *  This is are 4-byte device entries currently reporting the range of
+         *  devices 00h - ffffh; all devices
+         *
+         *  Device setting affecting all devices should be made here
+         *
+         *  Refer to
+         *  (http://developer.amd.com/wordpress/media/2012/10/488821.pdf)
+         *  5.2.2.1
+         */
+        iommu->dev_entries[12] = 3;
+        iommu->dev_entries[16] = 4;
+        iommu->dev_entries[17] = 0xff;
+        iommu->dev_entries[18] = 0xff;
+    }
+
+    build_header(linker, table_data, (void *)(table_data->data + iommu_start),
+                 "IVRS", table_data->len - iommu_start, 1, NULL, NULL);
+}
+
+static iommu_type has_iommu(void)
+{
+    bool ambiguous;
+
+    if (object_resolve_path_type("", TYPE_AMD_IOMMU_DEVICE, &ambiguous)
+            && !ambiguous)
+        return TYPE_AMD;
+    else if (object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE, &ambiguous)
+            && !ambiguous)
+        return TYPE_INTEL;
+    else
+        return TYPE_NONE;
+}
+
 static GArray *
 build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
 {
@@ -2646,16 +2725,6 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
     return true;
 }
 
-static bool acpi_has_iommu(void)
-{
-    bool ambiguous;
-    Object *intel_iommu;
-
-    intel_iommu = object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE,
-                                           &ambiguous);
-    return intel_iommu && !ambiguous;
-}
-
 static
 void acpi_build(AcpiBuildTables *tables, MachineState *machine)
 {
@@ -2668,6 +2737,7 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
     AcpiMcfgInfo mcfg;
     PcPciInfo pci;
     uint8_t *u;
+    iommu_type type = has_iommu();
     size_t aml_len = 0;
     GArray *tables_blob = tables->table_data;
     AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
@@ -2733,7 +2803,13 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
         acpi_add_table(table_offsets, tables_blob);
         build_mcfg_q35(tables_blob, tables->linker, &mcfg);
     }
-    if (acpi_has_iommu()) {
+
+    if (type == TYPE_AMD) {
+        acpi_add_table(table_offsets, tables_blob);
+        build_amd_iommu(tables_blob, tables->linker);
+    }
+
+    if (type == TYPE_INTEL) {
         acpi_add_table(table_offsets, tables_blob);
         build_dmar_q35(tables_blob, tables->linker);
     }
include/hw/acpi/acpi-defs.h +55 -0

59 lines not on this page · raw message

include/hw/i386/intel_iommu.h +1 -0
index b024ffa..7e511e1 100644
@@ -24,6 +24,7 @@
 #include "hw/qdev.h"
 #include "sysemu/dma.h"
 
+#define INTEL_IOMMU_STR "intel"
 #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"
 #define INTEL_IOMMU_DEVICE(obj) \
      OBJECT_CHECK(IntelIOMMUState, (obj), TYPE_INTEL_IOMMU_DEVICE)
Signature · 1 line
2.1.4
#3 lore raw

Re: [Qemu-devel] [V8 2/4] hw/i386: ACPI table for AMD IOMMU

Quoted 23 lines · David Kiarie

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

---
  hw/i386/acpi-build.c          | 98 ++++++++++++++++++++++++++++++++++++++-----
  include/hw/acpi/acpi-defs.h   | 55 ++++++++++++++++++++++++
  include/hw/i386/intel_iommu.h |  1 +
  3 files changed, 143 insertions(+), 11 deletions(-)
hw/i386/acpi-build.c +1 -0
index 35180ef..a54a22c 100644
@@ -52,6 +52,7 @@
  #include "hw/pci/pci_bus.h"
  #include "hw/pci-host/q35.h"
  #include "hw/i386/intel_iommu.h"
+#include "hw/i386/amd_iommu.h"
  #include "hw/timer/hpet.h"
 
  #include "hw/acpi/aml-build.h"

hw/i386/acpi-build.c typedef struct AcpiBuildPciBusHotplugState { · line 127

Quoted 9 lines
hw/i386/acpi-build.c +6 -0
@@ -118,6 +119,12 @@ typedef struct AcpiBuildPciBusHotplugState {
      bool pcihp_bridge_en;
  } AcpiBuildPciBusHotplugState;
 
+typedef enum iommu_type {
+    TYPE_AMD,
+    TYPE_INTEL,
+    TYPE_NONE
+} iommu_type;
+

Hi,

You can move the above enum into include/hw/boards.h and re-use it as the type of iommu_type Machine field. Of course, the default value would be TYPE_NONE.

Quoted 18 lines
hw/i386/acpi-build.c +11 -0
  static void acpi_get_pm_info(AcpiPmInfo *pm)
  {
      Object *piix = piix4_pm_find();
@@ -2588,6 +2595,78 @@ build_dmar_q35(GArray *table_data, GArray *linker)
                   "DMAR", table_data->len - dmar_start, 1, NULL, NULL);
  }
 
+static void
+build_amd_iommu(GArray *table_data, GArray *linker)
+{
+    int iommu_start = table_data->len;
+    bool iommu_ambig;
+
+    AcpiAMDIOMMUIVRS *ivrs;
+    AcpiAMDIOMMUHardwareUnit *iommu;
+
+    /* IVRS definition */
+    ivrs = acpi_data_push(table_data, sizeof(*ivrs));

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2618

Quoted 10 lines
hw/i386/acpi-build.c +10 -0
+    ivrs->revision = cpu_to_le16(ACPI_IOMMU_IVRS_TYPE);
+    ivrs->length = cpu_to_le16((sizeof(*ivrs) + sizeof(*iommu)));
+    ivrs->v_common_info = cpu_to_le64(AMD_IOMMU_HOST_ADDRESS_WIDTH << 8);
+
+    AMDIOMMUState *s = (AMDIOMMUState *)object_resolve_path_type("",
+                        TYPE_AMD_IOMMU_DEVICE, &iommu_ambig);
+
+    /* IVDB definition - type 10h */
+    iommu = acpi_data_push(table_data, sizeof(*iommu));
+    if (!iommu_ambig) {

You should assert on iommu_ambig. It is caller responsibility to be sure the AMD IOMMU is there.

Quoted 25 lines
hw/i386/acpi-build.c +25 -0
+        iommu->type = cpu_to_le16(0x10);
+        /* IVHD flags */
+        iommu->flags = cpu_to_le16(iommu->flags);
+        iommu->flags = cpu_to_le16(IVHD_HT_TUNEN | IVHD_PPRSUP | IVHD_IOTLBSUP
+                       | IVHD_PREFSUP);
+        iommu->length = cpu_to_le16(sizeof(*iommu));
+        iommu->device_id = cpu_to_le16(PCI_DEVICE_ID_RD890_IOMMU);
+        iommu->capability_offset = cpu_to_le16(s->capab_offset);
+        iommu->mmio_base = cpu_to_le64(s->mmio.addr);
+        iommu->pci_segment = 0;
+        iommu->interrupt_info = 0;
+        /* EFR features */
+        iommu->efr_register = cpu_to_le64(IVHD_EFR_GTSUP | IVHD_EFR_HATS
+                              | IVHD_EFR_GATS);
+        iommu->efr_register = cpu_to_le64(iommu->efr_register);
+        /* device entries */
+        memset(iommu->dev_entries, 0, 20);
+        /* Add device flags here
+         *  This is are 4-byte device entries currently reporting the range of
+         *  devices 00h - ffffh; all devices
+         *
+         *  Device setting affecting all devices should be made here
+         *
+         *  Refer to
+         *  (http://developer.amd.com/wordpress/media/2012/10/488821.pdf)

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2653

Quoted 10 lines
hw/i386/acpi-build.c +10 -0
+         *  5.2.2.1
+         */
+        iommu->dev_entries[12] = 3;
+        iommu->dev_entries[16] = 4;
+        iommu->dev_entries[17] = 0xff;
+        iommu->dev_entries[18] = 0xff;
+    }
+
+    build_header(linker, table_data, (void *)(table_data->data + iommu_start),
+                 "IVRS", table_data->len - iommu_start, 1, NULL, NULL);

CCed Igor to get his OK on the ACPI code.

Quoted 4 lines
hw/i386/acpi-build.c +4 -0
+}
+
+static iommu_type has_iommu(void)
+{

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2667

Quoted 10 lines
hw/i386/acpi-build.c +10 -0
+    bool ambiguous;
+
+    if (object_resolve_path_type("", TYPE_AMD_IOMMU_DEVICE, &ambiguous)
+            && !ambiguous)
+        return TYPE_AMD;
+    else if (object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE, &ambiguous)
+            && !ambiguous)
+        return TYPE_INTEL;
+    else
+        return TYPE_NONE;

here you could query machine's fields:
return MACHINE(qdev_get_machine())->iommu_type;

Quoted 115 lines
hw/i386/acpi-build.c +10 -11
+}
+
  static GArray *
  build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)
  {
@@ -2646,16 +2725,6 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
      return true;
  }
 
-static bool acpi_has_iommu(void)
-{
-    bool ambiguous;
-    Object *intel_iommu;
-
-    intel_iommu = object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE,
-                                           &ambiguous);
-    return intel_iommu && !ambiguous;
-}
-
  static
  void acpi_build(AcpiBuildTables *tables, MachineState *machine)
  {
@@ -2668,6 +2737,7 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
      AcpiMcfgInfo mcfg;
      PcPciInfo pci;
      uint8_t *u;
+    iommu_type type = has_iommu();
      size_t aml_len = 0;
      GArray *tables_blob = tables->table_data;
      AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
@@ -2733,7 +2803,13 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
          acpi_add_table(table_offsets, tables_blob);
          build_mcfg_q35(tables_blob, tables->linker, &mcfg);
      }
-    if (acpi_has_iommu()) {
+
+    if (type == TYPE_AMD) {
+        acpi_add_table(table_offsets, tables_blob);
+        build_amd_iommu(tables_blob, tables->linker);
+    }
+
+    if (type == TYPE_INTEL) {
          acpi_add_table(table_offsets, tables_blob);
          build_dmar_q35(tables_blob, tables->linker);
      }
include/hw/acpi/acpi-defs.h +55 -0
index c7a03d4..a161358 100644
@@ -570,4 +570,59 @@ typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit;
  /* Masks for Flags field above */
  #define ACPI_DMAR_INCLUDE_PCI_ALL   1
 
+/* IVRS constants */
+#define ACPI_IOMMU_HARDWAREUNIT_TYPE 0x10
+#define ACPI_IOMMU_IVRS_TYPE 0x1
+#define AMD_IOMMU_HOST_ADDRESS_WIDTH 39UL
+
+/* AMD IOMMU IVRS table */
+struct AcpiAMDIOMMUIVRS {
+    ACPI_TABLE_HEADER_DEF
+    uint32_t v_common_info; /* common virtualization information */
+    uint64_t reserved;      /* reserved                          */
+} QEMU_PACKED;
+typedef struct AcpiAMDIOMMUIVRS AcpiAMDIOMMUIVRS;
+
+/* flags in the IVHD headers */
+#define IVHD_HT_TUNEN    (1UL << 0)
+#define IVHD_PASS_PW     (1UL << 1)
+#define IVHD_RESPASS_PW  (1UL << 2)
+#define IVHD_ISOC        (1UL << 3)
+#define IVHD_IOTLBSUP    (1UL << 4)
+#define IVHD_COHERENT    (1UL << 5)
+#define IVHD_PREFSUP     (1UL << 6)
+#define IVHD_PPRSUP      (1UL << 7)
+
+/* features in the IVHD headers */
+#define IVHD_EFR_HATS       48
+#define IVHD_EFR_GATS       48
+#define IVHD_EFR_MSI_NUM
+#define IVHD_EFR_PNBANKS
+#define IVHD_EFR_PNCOUNTERS
+#define IVHD_EFR_PASMAX
+#define IVHD_EFR_HESUP  (1UL << 7)
+#define IVHD_EFR_GASUP  (1UL << 6)
+#define IVHD_EFR_IASUP  (1UL << 5)
+#define IVHD_EFR_GLXSUP (3UL << 3)
+#define IVHD_EFR_GTSUP  (1UL << 2)
+#define IVHD_EFR_NXSUP  (1UL << 1)
+#define IVHD_EFR_XTSUP  (1UL << 0)
+
+/* IVDB type 10h */
+struct AcpiAMDIOMMUHardwareUnit {
+    uint8_t type;
+    uint8_t flags;
+    uint16_t length;
+    uint16_t device_id;
+    uint16_t capability_offset;
+    uint64_t mmio_base;
+    uint16_t pci_segment;
+    uint16_t interrupt_info;
+    uint32_t features;
+    uint64_t efr_register;
+    uint64_t reserved;
+    uint8_t dev_entries[20];
+} QEMU_PACKED;
+typedef struct AcpiAMDIOMMUHardwareUnit AcpiAMDIOMMUHardwareUnit;
+
  #endif
include/hw/i386/intel_iommu.h +0 -0
index b024ffa..7e511e1 100644

include/hw/i386/intel_iommu.h line 30

Quoted 7 lines
include/hw/i386/intel_iommu.h +1 -0
@@ -24,6 +24,7 @@
  #include "hw/qdev.h"
  #include "sysemu/dma.h"
 
+#define INTEL_IOMMU_STR "intel"
  #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"
  #define INTEL_IOMMU_DEVICE(obj) \
       OBJECT_CHECK(IntelIOMMUState, (obj), TYPE_INTEL_IOMMU_DEVICE)

Thanks,
Marcel

#4 lore raw

Re: [Qemu-devel] [V8 2/4] hw/i386: ACPI table for AMD IOMMU

In reply to David Kiarie
Quoted 41 lines · On Fri, 1 Apr 2016 21:39:52 +0300 David Kiarie <[email protected]> wrote:

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

---
 hw/i386/acpi-build.c          | 98 ++++++++++++++++++++++++++++++++++++++-----
 include/hw/acpi/acpi-defs.h   | 55 ++++++++++++++++++++++++
 include/hw/i386/intel_iommu.h |  1 +
 3 files changed, 143 insertions(+), 11 deletions(-)
hw/i386/acpi-build.c +9 -0
index 35180ef..a54a22c 100644
@@ -52,6 +52,7 @@
 #include "hw/pci/pci_bus.h"
 #include "hw/pci-host/q35.h"
 #include "hw/i386/intel_iommu.h"
+#include "hw/i386/amd_iommu.h"
 #include "hw/timer/hpet.h"
 
 #include "hw/acpi/aml-build.h"
@@ -118,6 +119,12 @@ typedef struct AcpiBuildPciBusHotplugState {
     bool pcihp_bridge_en;
 } AcpiBuildPciBusHotplugState;
 
+typedef enum iommu_type {
+    TYPE_AMD,
+    TYPE_INTEL,
+    TYPE_NONE
+} iommu_type;
+
 static void acpi_get_pm_info(AcpiPmInfo *pm)
 {
     Object *piix = piix4_pm_find();
@@ -2588,6 +2595,78 @@ build_dmar_q35(GArray *table_data, GArray *linker)
                  "DMAR", table_data->len - dmar_start, 1, NULL, NULL);
 }
 
+static void
+build_amd_iommu(GArray *table_data, GArray *linker)

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2609

Quoted 10 lines
hw/i386/acpi-build.c +10 -0
+{
+    int iommu_start = table_data->len;
+    bool iommu_ambig;
+
+    AcpiAMDIOMMUIVRS *ivrs;
+    AcpiAMDIOMMUHardwareUnit *iommu;
+
+    /* IVRS definition */
+    ivrs = acpi_data_push(table_data, sizeof(*ivrs));
+    ivrs->revision = cpu_to_le16(ACPI_IOMMU_IVRS_TYPE);

s/ACPI_IOMMU_IVRS_TYPE/1/ it's the only usage site and well it's not type but revision number,
Pls try to keep names close to spec, so that is would be easy to review code.

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2610

Quoted 1 line
hw/i386/acpi-build.c +1 -0
+    ivrs->length = cpu_to_le16((sizeof(*ivrs) + sizeof(*iommu)));

you are expecting length be ^^^ from starters so push above should be for this size.

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2611

Quoted 1 line
hw/i386/acpi-build.c +1 -0
+    ivrs->v_common_info = cpu_to_le64(AMD_IOMMU_HOST_ADDRESS_WIDTH << 8);

shouldn't above block go below (AMDIOMMUState *)object_resolve_path_type(..., i.e. is there point in pushing empty table?

s/v_common_info/iv_info/ as in spec
also it's 32 bit in size so using here cpu_to_le64() is wrong.

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2614

Quoted 3 lines
hw/i386/acpi-build.c +3 -0
+
+    AMDIOMMUState *s = (AMDIOMMUState *)object_resolve_path_type("",
+                        TYPE_AMD_IOMMU_DEVICE, &iommu_ambig);

Maybe it should assert on iommu_ambig == true
and get rid of if(!iommu_ambig) condition

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2616

Quoted 2 lines
hw/i386/acpi-build.c +2 -0
+
+    /* IVDB definition - type 10h */

s/IVDB definition - type 10h/Create IVHD entry/

Quoted 8 lines
hw/i386/acpi-build.c +7 -0
+    iommu = acpi_data_push(table_data, sizeof(*iommu));
+    if (!iommu_ambig) {
 
+        iommu->type = cpu_to_le16(0x10);
+        /* IVHD flags */
+        iommu->flags = cpu_to_le16(iommu->flags);
+        iommu->flags = cpu_to_le16(IVHD_HT_TUNEN | IVHD_PPRSUP | IVHD_IOTLBSUP
+                       | IVHD_PREFSUP);

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2633

Quoted 10 lines
hw/i386/acpi-build.c +10 -0
+        iommu->length = cpu_to_le16(sizeof(*iommu));
+        iommu->device_id = cpu_to_le16(PCI_DEVICE_ID_RD890_IOMMU);
+        iommu->capability_offset = cpu_to_le16(s->capab_offset);
+        iommu->mmio_base = cpu_to_le64(s->mmio.addr);
+        iommu->pci_segment = 0;
+        iommu->interrupt_info = 0;
+        /* EFR features */
+        iommu->efr_register = cpu_to_le64(IVHD_EFR_GTSUP | IVHD_EFR_HATS
+                              | IVHD_EFR_GATS);
+        iommu->efr_register = cpu_to_le64(iommu->efr_register);

double assignment looks rather weird here, pls explain why are you doing it.

Actually I'd prefer if you'd use table declaration style for creating IVRS table instead of AcpiAMDIOMMUIVRS/AcpiAMDIOMMUHardwareUnit structures, that would simplify review greatly as it would be mostly comparison with spec's tables.
You won't have to care about endianess, it also will help to avoid many mistakes with usage of wrong cpu_to_le* functions as it's the most common mistake in this patch.
something like this:

/* IVRS fields */
uint8_t *ivrs_tbl = acpi_data_push(table_data, sizeof(acpi_table_header)); /* reserve space for header */ build_append_int_noprefix(table_data, AMD_IOMMU_HOST_ADDRESS_WIDTH << 8, 4); /* IVinfo */ build_append_int_noprefix(table_data, 0, 8); /* Reserved */

/* build IVHD entry */
build_append_int_noprefix(table_data, 0x10 /* IVHD entry */, 1); /* Type */ build_append_int_noprefix(table_data, iommu->flags, 1); /* Flags */ build_append_int_noprefix(table_data, ivhd_len, 2); /* Length */
...
build_4byte_ivhd_device_entry(table_data,...); /* if it's only one entry no need in separate function, just inline it */
...
build_header(linker, table_data, (void *), ivrs_tbl,

             "IVRS", ivrs_tbl - table_data->data, 1 /* IVRS Revision */, NULL, NULL);

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2644

Quoted 11 lines
hw/i386/acpi-build.c +11 -0
+        /* device entries */
+        memset(iommu->dev_entries, 0, 20);
+        /* Add device flags here
+         *  This is are 4-byte device entries currently reporting the range of
+         *  devices 00h - ffffh; all devices
+         *
+         *  Device setting affecting all devices should be made here
+         *
+         *  Refer to
+         *  (http://developer.amd.com/wordpress/media/2012/10/488821.pdf)
+         *  5.2.2.1

in linked pdf it's not 5.2.2 but
"5.3 IOMMU ACPI Table Definitions"

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2649

Quoted 5 lines
hw/i386/acpi-build.c +5 -0
+         */
+        iommu->dev_entries[12] = 3;
+        iommu->dev_entries[16] = 4;
+        iommu->dev_entries[17] = 0xff;
+        iommu->dev_entries[18] = 0xff;

this totally unreadable, I can't tell which fields you are filling in without doing math, see above table style declaration suggestion,
with comments matching spec along with it, it should be easy to verify entry vs spec.

Also maybe use "all devices entry" (1, as per Table 75: IVHD device entry type codes) instead of declaring a range with help of several entries.

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2656

Quoted 7 lines
hw/i386/acpi-build.c +7 -0
+    }
+
+    build_header(linker, table_data, (void *)(table_data->data + iommu_start),
+                 "IVRS", table_data->len - iommu_start, 1, NULL, NULL);
+}
+
+static iommu_type has_iommu(void)

s/has_iommu/iommu_type/

hw/i386/acpi-build.c build_dmar_q35(GArray *table_data, GArray *linker) · line 2661

Quoted 5 lines
hw/i386/acpi-build.c +5 -0
+{
+    bool ambiguous;
+
+    if (object_resolve_path_type("", TYPE_AMD_IOMMU_DEVICE, &ambiguous)
+            && !ambiguous)

you probably should assert on ambiguous == true

Quoted 29 lines
hw/i386/acpi-build.c +8 -0
+        return TYPE_AMD;
+    else if (object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE, &ambiguous)
+            && !ambiguous)
+        return TYPE_INTEL;
+    else
+        return TYPE_NONE;
+}
+
 static GArray *
 build_rsdp(GArray *rsdp_table, GArray *linker, unsigned rsdt)

{

hw/i386/acpi-build.c +0 -10
@@ -2646,16 +2725,6 @@ static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
     return true;
 }
 
-static bool acpi_has_iommu(void)
-{
-    bool ambiguous;
-    Object *intel_iommu;
-
-    intel_iommu = object_resolve_path_type("", TYPE_INTEL_IOMMU_DEVICE,
-                                           &ambiguous);
-    return intel_iommu && !ambiguous;
-}
-
 static
 void acpi_build(AcpiBuildTables *tables, MachineState *machine)
 {

hw/i386/acpi-build.c void acpi_build(AcpiBuildTables *tables, MachineState *machine) · line 2740

Quoted 4 lines
hw/i386/acpi-build.c +1 -0
@@ -2668,6 +2737,7 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
     AcpiMcfgInfo mcfg;
     PcPciInfo pci;
     uint8_t *u;
+    iommu_type type = has_iommu();

s/iommu_type/IOMMUType/

s/type/iommu_type/

Quoted 53 lines
hw/i386/acpi-build.c +7 -1
     size_t aml_len = 0;
     GArray *tables_blob = tables->table_data;
     AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
@@ -2733,7 +2803,13 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
         acpi_add_table(table_offsets, tables_blob);
         build_mcfg_q35(tables_blob, tables->linker, &mcfg);
     }
-    if (acpi_has_iommu()) {
+
+    if (type == TYPE_AMD) {
+        acpi_add_table(table_offsets, tables_blob);
+        build_amd_iommu(tables_blob, tables->linker);
+    }
+
+    if (type == TYPE_INTEL) {
         acpi_add_table(table_offsets, tables_blob);
         build_dmar_q35(tables_blob, tables->linker);
     }
include/hw/acpi/acpi-defs.h +27 -0
index c7a03d4..a161358 100644
@@ -570,4 +570,59 @@ typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit;
 /* Masks for Flags field above */
 #define ACPI_DMAR_INCLUDE_PCI_ALL   1
 
+/* IVRS constants */
+#define ACPI_IOMMU_HARDWAREUNIT_TYPE 0x10
+#define ACPI_IOMMU_IVRS_TYPE 0x1
+#define AMD_IOMMU_HOST_ADDRESS_WIDTH 39UL
+
+/* AMD IOMMU IVRS table */
+struct AcpiAMDIOMMUIVRS {
+    ACPI_TABLE_HEADER_DEF
+    uint32_t v_common_info; /* common virtualization information */
+    uint64_t reserved;      /* reserved                          */
+} QEMU_PACKED;
+typedef struct AcpiAMDIOMMUIVRS AcpiAMDIOMMUIVRS;
+
+/* flags in the IVHD headers */
+#define IVHD_HT_TUNEN    (1UL << 0)
+#define IVHD_PASS_PW     (1UL << 1)
+#define IVHD_RESPASS_PW  (1UL << 2)
+#define IVHD_ISOC        (1UL << 3)
+#define IVHD_IOTLBSUP    (1UL << 4)
+#define IVHD_COHERENT    (1UL << 5)
+#define IVHD_PREFSUP     (1UL << 6)
+#define IVHD_PPRSUP      (1UL << 7)
+
+/* features in the IVHD headers */
+#define IVHD_EFR_HATS       48
+#define IVHD_EFR_GATS       48
+#define IVHD_EFR_MSI_NUM

include/hw/acpi/acpi-defs.h typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit; · line 609

Quoted 10 lines
include/hw/acpi/acpi-defs.h +10 -0
+#define IVHD_EFR_PNBANKS
+#define IVHD_EFR_PNCOUNTERS
+#define IVHD_EFR_PASMAX
+#define IVHD_EFR_HESUP  (1UL << 7)
+#define IVHD_EFR_GASUP  (1UL << 6)
+#define IVHD_EFR_IASUP  (1UL << 5)
+#define IVHD_EFR_GLXSUP (3UL << 3)
+#define IVHD_EFR_GTSUP  (1UL << 2)
+#define IVHD_EFR_NXSUP  (1UL << 1)
+#define IVHD_EFR_XTSUP  (1UL << 0)

some of above defines aren't used in this patch,
pls move them to patch where they are first used or drop them.

Quoted 4 lines
include/hw/acpi/acpi-defs.h +4 -0
+
+/* IVDB type 10h */
+struct AcpiAMDIOMMUHardwareUnit {
+    uint8_t type;

include/hw/acpi/acpi-defs.h typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit; · line 623

Quoted 10 lines
include/hw/acpi/acpi-defs.h +10 -0
+    uint8_t flags;
+    uint16_t length;
+    uint16_t device_id;
+    uint16_t capability_offset;
+    uint64_t mmio_base;
+    uint16_t pci_segment;
+    uint16_t interrupt_info;
+    uint32_t features;
+    uint64_t efr_register;
+    uint64_t reserved;

this field shouldn't be here, per spec efr_register is the last before dev_entries

Quoted 10 lines
include/hw/acpi/acpi-defs.h +4 -0
+    uint8_t dev_entries[20];
+} QEMU_PACKED;
+typedef struct AcpiAMDIOMMUHardwareUnit AcpiAMDIOMMUHardwareUnit;
+
 #endif
include/hw/i386/intel_iommu.h +0 -0
index b024ffa..7e511e1 100644

include/hw/i386/intel_iommu.h line 27

Quoted 4 lines
include/hw/i386/intel_iommu.h +1 -0
@@ -24,6 +24,7 @@
 #include "hw/qdev.h"
 #include "sysemu/dma.h"
 
+#define INTEL_IOMMU_STR "intel"

not related to patch/series change?

Quoted 3 lines
include/hw/i386/intel_iommu.h +0 -0
 #define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"
 #define INTEL_IOMMU_DEVICE(obj) \
      OBJECT_CHECK(IntelIOMMUState, (obj), TYPE_INTEL_IOMMU_DEVICE)
#5 lore raw

PATCH v8 3/4 hw/core: Add AMD IOMMU to machine properties

In reply to David Kiarie

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

---
 hw/core/machine.c   | 32 +++++++++++++++++++++++++++++---
 include/hw/boards.h |  1 +
 qemu-options.hx     |  7 +++++--
 util/qemu-config.c  |  8 ++++++--
 4 files changed, 41 insertions(+), 7 deletions(-)
hw/core/machine.c +29 -3
index 6dbbc85..792641b 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"
@@ -300,6 +302,26 @@ static void machine_set_iommu(Object *obj, bool value, Error **errp)
     ms->iommu = value;
 }
 
+static void machine_set_iommu_override(Object *obj, const char *value,
+                                       Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+    Error *err = NULL;
+
+    ms->x_iommu_type = false;
+    /* ensure a valid iommu type */
+    if (g_strcmp0(value, AMD_IOMMU_STR) &&
+            g_strcmp0(value, INTEL_IOMMU_STR)) {
+        error_setg(errp, "Invalid IOMMU type %s", value);
+        error_propagate(errp, err);
+        return;
+    }
+
+    if ((g_strcmp0(value, AMD_IOMMU_STR) == 0)) {
+        ms->x_iommu_type = true;
+    }
+}
+
 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
 {
     MachineState *ms = MACHINE(obj);
@@ -473,10 +495,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 aad5f2a..07788fc 100644
@@ -151,6 +151,7 @@ struct MachineState {
     bool igd_gfx_passthru;
     char *firmware;
     bool iommu;
+    bool x_iommu_type;
     bool suppress_vmdesc;
     bool enforce_config_section;
 
qemu-options.hx +5 -2
index a770086..14d30b7 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
#6 lore raw

Re: [Qemu-devel] [V8 3/4] hw/core: Add AMD IOMMU to machine properties

Quoted 3 lines · David Kiarie

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

Hi David,
The patch is looking good, thanks.
One minor comment below.

Quoted 71 lines
---
  hw/core/machine.c   | 32 +++++++++++++++++++++++++++++---
  include/hw/boards.h |  1 +
  qemu-options.hx     |  7 +++++--
  util/qemu-config.c  |  8 ++++++--
  4 files changed, 41 insertions(+), 7 deletions(-)
hw/core/machine.c +29 -3
index 6dbbc85..792641b 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"
@@ -300,6 +302,26 @@ static void machine_set_iommu(Object *obj, bool value, Error **errp)
      ms->iommu = value;
  }
 
+static void machine_set_iommu_override(Object *obj, const char *value,
+                                       Error **errp)
+{
+    MachineState *ms = MACHINE(obj);
+    Error *err = NULL;
+
+    ms->x_iommu_type = false;
+    /* ensure a valid iommu type */
+    if (g_strcmp0(value, AMD_IOMMU_STR) &&
+            g_strcmp0(value, INTEL_IOMMU_STR)) {
+        error_setg(errp, "Invalid IOMMU type %s", value);
+        error_propagate(errp, err);
+        return;
+    }
+
+    if ((g_strcmp0(value, AMD_IOMMU_STR) == 0)) {
+        ms->x_iommu_type = true;
+    }
+}
+
  static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
  {
      MachineState *ms = MACHINE(obj);
@@ -473,10 +495,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 +0 -0
index aad5f2a..07788fc 100644

include/hw/boards.h struct MachineState { · line 154

Quoted 4 lines
include/hw/boards.h +1 -0
@@ -151,6 +151,7 @@ struct MachineState {
      bool igd_gfx_passthru;
      char *firmware;
      bool iommu;
+    bool x_iommu_type;

The name is a little weird for a boolean variable.
You can either change the name to iommu_amd (true when AMD, false when INTEL) or make it an enum. 0 - Intel, 1 - AMD, ...

Other than that the patch is ready in my opinion.
Thanks,
Marcel

Quoted 48 lines
include/hw/boards.h +0 -0
      bool suppress_vmdesc;
      bool enforce_config_section;
 
qemu-options.hx +5 -2
index a770086..14d30b7 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,
#7 lore raw

PATCH v8 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             | 21 +++++++++++++++++++--
 include/hw/i386/intel_iommu.h |  1 +
 2 files changed, 20 insertions(+), 2 deletions(-)
hw/pci-host/q35.c +19 -2
index 70f897e..37f8a84 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)
+{
+    AMDIOMMUState *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_amd_iommu, iommu_state);
+}
+
 static void mch_realize(PCIDevice *d, Error **errp)
 {
     int i;
@@ -506,8 +520,11 @@ 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)) {
+
+    if (object_property_get_bool(qdev_get_machine(), "iommu", NULL) &&
+        MACHINE(qdev_get_machine())->x_iommu_type) {
+        mch_init_amdvi(mch);
+    } else if (object_property_get_bool(qdev_get_machine(), "iommu", NULL)) {
         mch_init_dmar(mch);
     }
 }
include/hw/i386/intel_iommu.h +1 -0
index 7e511e1..5a520f3 100644
@@ -28,6 +28,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
Signature · 1 line
2.1.4
#8 lore raw

Re: [Qemu-devel] [V8 4/4] hw/pci-host: Emulate AMD IOMMU

Quoted 41 lines · David Kiarie

Add AMD IOMMU emulation support to q35 chipset

---
  hw/pci-host/q35.c             | 21 +++++++++++++++++++--
  include/hw/i386/intel_iommu.h |  1 +
  2 files changed, 20 insertions(+), 2 deletions(-)
hw/pci-host/q35.c +14 -0
index 70f897e..37f8a84 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)
+{
+    AMDIOMMUState *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_amd_iommu, iommu_state);
+}
+
  static void mch_realize(PCIDevice *d, Error **errp)
  {
      int i;

hw/pci-host/q35.c static void mch_realize(PCIDevice *d, Error **errp) · line 525

Quoted 8 lines
hw/pci-host/q35.c +3 -2
@@ -506,8 +520,11 @@ 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)) {
+
+    if (object_property_get_bool(qdev_get_machine(), "iommu", NULL) &&
+        MACHINE(qdev_get_machine())->x_iommu_type) {

Here you don't need to check twice the iommu property, and even use the long object_property_get_bool.

You can use something like :

machine = MACHINE(qdev_get_machine());

  if (machine->iommu) {
    if (machine->amd_iommu) {  /* or use a switch if you opt for enum */
      ...
    } else { /* intel */
      ...
    }

}

Other than that the patch is ready.

Thanks,
Marcel

Quoted 18 lines
hw/pci-host/q35.c +2 -0
+        mch_init_amdvi(mch);
+    } else if (object_property_get_bool(qdev_get_machine(), "iommu", NULL)) {
          mch_init_dmar(mch);
      }
  }
include/hw/i386/intel_iommu.h +1 -0
index 7e511e1..5a520f3 100644
@@ -28,6 +28,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