Skip to contentSkip to conversation

AMD IOMMU

PATCH Superseded David Kiarie 3 participants 10 messages Aug 31, 2016 – Sep 17, 2016

v17 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v17 cover AMD IOMMU

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Changes since v16 - this is mainly supposed to come as a ping :-)
-minor endian-ness fixes

Changes since v15
-Endian-ness issue fix
-cleaned up unused macros
-removed guest frame number(gfn) from cache entry

Changes since v14
-MMIO register reading/write bug fix [Peter]
-Endian-ness issue fix[Peter]
-Bitfields layouts in IOMMU commands fix[Peter]
-IVRS changed IVHD device entry from type 3 to 1 to save a few bytes -coding style issues, comment grammer and other miscellaneous fixes.

Changes since v13
-Added an error to make AMD IOMMU incompatible with device assignment.[Alex] -Converted AMD IOMMU into a composite PCI and System Bus device. This helps with:
-We can now inherit from X86 IOMMU base class(which is implemented as a System Bus device). -We can now reserve MMIO region for IOMMU without a BAR register and without a hack.

Changes since v12

-Coding style fixes [Jan, Michael]
-Error logging fix to avoid using a macro[Jan] -moved some PCI macros to PCI header[Jan]
-Use a lookup table for MMIO register names when tracing[Jan]

Changes since V11
-AMD IOMMU is not started with -device amd-iommu (with a dependency on Marcel's patches). -IOMMU commands are represented using bitfields which is less error prone and more readable[Peter] -Changed from debug fprintfs to tracing[Jan]

Changes since V10

-Support for huge pages including some obscure AMD IOMMU feature that allows default page size override[Jan].
-Fixed an issue with generation of interrupts. We noted that AMD IOMMU has BusMaster- and is therefore not able to generate interrupts like any other PCI device. We have resulted in writing directly to system address but this could be fixed by some patches which have not been merged yet.

Changes since v9

-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 to ensure no DMA unless a device is configured to allow it. -An issue with the emulate IOMMU defaulting to AMD_IOMMU has been fixed[Marcel]

You can test[1] this patches by starting with parameters
qemu-system-x86_64 -M -device amd-iommu -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/pci: Prepare for AMD IOMMU hw/i386/trace-events: Add AMD IOMMU trace events hw/i386: Introduce AMD IOMMU
hw/i386: AMD IOMMU IVRS table

 hw/acpi/aml-build.c         |    2 +-
 hw/i386/Makefile.objs       |    1 +
 hw/i386/acpi-build.c        |   76 ++-
 hw/i386/amd_iommu.c         | 1383 +++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h         |  289 +++++++++
 hw/i386/intel_iommu.c       |    1 +
 hw/i386/trace-events        |   29 +
 hw/i386/x86-iommu.c         |    6 +
 include/hw/acpi/aml-build.h |    1 +
 include/hw/i386/x86-iommu.h |   12 +
 include/hw/pci/pci.h        |    4 +-
 11 files changed, 1793 insertions(+), 11 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 v17 1/4 hw/pci: Prepare for AMD IOMMU

Introduce PCI macros from for use by AMD IOMMU

---
 include/hw/pci/pci.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/hw/pci/pci.h +3 -1
index 929ec2f..5ff92de 100644
@@ -11,11 +11,13 @@
 #include "hw/pci/pcie.h"
 
 /* PCI bus */
-
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
+#define PCI_BUS_NUM(x)          (((x) >> 8) & 0xff)
 #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
 #define PCI_FUNC(devfn)         ((devfn) & 0x07)
 #define PCI_BUILD_BDF(bus, devfn)     ((bus << 8) | (devfn))
+#define PCI_BUS_MAX             256
+#define PCI_DEVFN_MAX           256
 #define PCI_SLOT_MAX            32
 #define PCI_FUNC_MAX            8
 
Signature · 1 line
2.1.4
#2 lore raw

PATCH v17 2/4 hw/i386/trace-events: Add AMD IOMMU trace events

In reply to David Kiarie
---
 hw/i386/trace-events | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
hw/i386/trace-events +29 -0
index 7735e46..60bdf6a 100644
@@ -13,3 +13,32 @@ mhp_pc_dimm_assigned_address(uint64_t addr) "0x%"PRIx64
 
 # hw/i386/x86-iommu.c
 x86_iommu_iec_notify(bool global, uint32_t index, uint32_t mask) "Notify IEC invalidation: global=%d index=%" PRIu32 " mask=%" PRIu32
+
+# hw/i386/amd_iommu.c
+amdvi_evntlog_fail(uint64_t addr, uint32_t head) "error: fail to write at addr 0x%"PRIx64 " +  offset 0x%"PRIx32
+amdvi_cache_update(uint16_t domid, uint32_t bus, uint32_t slot, uint32_t func, uint64_t gpa, uint64_t txaddr) " update iotlb domid 0x%"PRIx16" devid: %02x:%02x.%x gpa 0x%"PRIx64 " hpa 0x%"PRIx64
+amdvi_completion_wait_fail(uint64_t addr) "error: fail to write at address 0x%"PRIx64
+amdvi_mmio_write(const char *reg, uint64_t addr, unsigned size, uint64_t val, unsigned long offset) "%s write addr 0x%"PRIx64 ", size %d, val 0x%"PRIx64 ", offset 0x%"PRIx64
+amdvi_mmio_read(const char *reg, uint64_t addr, unsigned size, uint64_t offset) "%s read addr 0x%"PRIx64", size %d offset 0x%"PRIx64
+amdvi_command_error(uint64_t status) "error: Executing commands with command buffer disabled 0x%"PRIx64
+amdvi_command_read_fail(uint64_t addr, uint32_t head) "error: fail to access memory at 0x%"PRIx64" + 0x%"PRIu32
+amdvi_command_exec(uint32_t head, uint32_t tail, uint64_t buf) "command buffer head at 0x%"PRIx32 " command buffer tail at 0x%"PRIx32" command buffer base at 0x%" PRIx64
+amdvi_unhandled_command(uint8_t type) "unhandled command %d"
+amdvi_intr_inval(void) "Interrupt table invalidated"
+amdvi_iotlb_inval(void) "IOTLB pages invalidated"
+amdvi_prefetch_pages(void) "Pre-fetch of AMD-Vi pages requested"
+amdvi_pages_inval(uint16_t domid) "AMD-Vi pages for domain 0x%"PRIx16 " invalidated"
+amdvi_all_inval(void) "Invalidation of all AMD-Vi cache requested "
+amdvi_ppr_exec(void) "Execution of PPR queue requested "
+amdvi_devtab_inval(uint16_t bus, uint16_t slot, uint16_t func) "device table entry for devid: %02x:%02x.%x invalidated"
+amdvi_completion_wait(uint64_t addr, uint64_t data) "completion wait requested with store address 0x%"PRIx64" and store data 0x%"PRIx64
+amdvi_control_status(uint64_t val) "MMIO_STATUS state 0x%"PRIx64
+amdvi_iotlb_reset(void) "IOTLB exceed size limit - reset "
+amdvi_completion_wait_exec(uint64_t addr, uint64_t data) "completion wait requested with store address 0x%"PRIx64" and store data 0x%"PRIx64
+amdvi_dte_get_fail(uint64_t addr, uint32_t offset) "error: failed to access Device Entry devtab 0x%"PRIx64" offset 0x%"PRIx32
+amdvi_invalid_dte(uint64_t addr) "PTE entry at 0x%"PRIx64" is invalid "
+amdvi_get_pte_hwerror(uint64_t addr) "hardware error eccessing PTE at addr 0x%"PRIx64
+amdvi_mode_invalid(unsigned level, uint64_t addr)"error: translation level 0x%"PRIu8" translating addr 0x%"PRIx64
+amdvi_page_fault(uint64_t addr) "error: page fault accessing guest physical address 0x%"PRIx64
+amdvi_iotlb_hit(uint16_t bus, uint16_t slot, uint16_t func, uint64_t addr, uint64_t txaddr) "hit iotlb devid %02x:%02x.%x gpa 0x%"PRIx64 " hpa 0x%"PRIx64
+amdvi_translation_result(uint16_t bus, uint16_t slot, uint16_t func, uint64_t addr, uint64_t txaddr) "devid: %02x:%02x.%x gpa 0x%"PRIx64 " hpa 0x%"PRIx64
Signature · 1 line
2.1.4
#3 lore raw

PATCH v17 3/4 hw/i386: Introduce AMD IOMMU

In reply to David Kiarie

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 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   | 1381 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h   |  289 +++++++++++
 3 files changed, 1671 insertions(+)
 create mode 100644 hw/i386/amd_iommu.c
 create mode 100644 hw/i386/amd_iommu.h
hw/i386/Makefile.objs +1 -0
index 90e94ff..909ead6 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 += x86-iommu.o intel_iommu.o
+obj-y += amd_iommu.o
 obj-$(CONFIG_XEN) += ../xenpv/ xen/
 
 obj-y += kvmvapic.o
hw/i386/amd_iommu.c new file +1381 -0

1,381 lines not on this page · raw message

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

289 lines not on this page · raw message

Signature · 1 line
2.1.4
#4 lore raw

Re: [Qemu-devel] [V17 3/4] hw/i386: Introduce AMD IOMMU

Quoted 181 lines · David Kiarie
hw/i386/amd_iommu.c new file +181 -0
+/* serialize IOMMU command processing */
+typedef struct QEMU_PACKED {
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t type:4;               /* command type           */
+    uint64_t reserved:8;
+    uint64_t store_addr:49;        /* addr to write          */
+    uint64_t completion_flush:1;   /* allow more executions  */
+    uint64_t completion_int:1;     /* set MMIOWAITINT        */
+    uint64_t completion_store:1;   /* write data to address  */
+#else
+    uint64_t completion_store:1;
+    uint64_t completion_int:1;
+    uint64_t completion_flush:1;
+    uint64_t store_addr:49;
+    uint64_t reserved:8;
+    uint64_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+    uint64_t store_data;           /* data to write          */
+} CMDCompletionWait;
+
+/* invalidate internal caches for devid */
+typedef struct QEMU_PACKED {
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t devid:16;             /* device to invalidate   */
+    uint64_t reserved_1:44;
+    uint64_t type:4;               /* command type           */
+#else
+    uint64_t devid:16;
+    uint64_t reserved_1:44;
+    uint64_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+    uint64_t reserved_2;
+} CMDInvalDevEntry;
+
+/* invalidate a range of entries in IOMMU translation cache for devid */
+typedef struct QEMU_PACKED {
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t type:4;               /* command type           */
+    uint64_t reserved_2:12
+    uint64_t domid:16;             /* domain to inval for    */
+    uint64_t reserved_1:12;
+    uint64_t pasid:20;
+#else
+    uint64_t pasid:20;
+    uint64_t reserved_1:12;
+    uint64_t domid:16;
+    uint64_t reserved_2:12;
+    uint64_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t address:51;          /* address to invalidate   */
+    uint64_t reserved_3:10;
+    uint64_t guest:1;             /* G/N invalidation        */
+    uint64_t pde:1;               /* invalidate cached ptes  */
+    uint64_t size:1               /* size of invalidation    */
+#else
+    uint64_t size:1;
+    uint64_t pde:1;
+    uint64_t guest:1;
+    uint64_t reserved_3:10;
+    uint64_t address:51;
+#endif /* __BIG_ENDIAN_BITFIELD */
+} CMDInvalIommuPages;
+
+/* inval specified address for devid from remote IOTLB */
+typedef struct QEMU_PACKED {
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t type:4;            /* command type        */
+    uint64_t pasid_19_6:4;
+    uint64_t pasid_7_0:8;
+    uint64_t queuid:16;
+    uint64_t maxpend:8;
+    uint64_t pasid_15_8;
+    uint64_t devid:16;         /* related devid        */
+#else
+    uint64_t devid:16;
+    uint64_t pasid_15_8:8;
+    uint64_t maxpend:8;
+    uint64_t queuid:16;
+    uint64_t pasid_7_0:8;
+    uint64_t pasid_19_6:4;
+    uint64_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t address:52;       /* invalidate addr      */
+    uint64_t reserved_2:9;
+    uint64_t guest:1;          /* G/N invalidate       */
+    uint64_t reserved_1:1;
+    uint64_t size:1;           /* size of invalidation */
+#else
+    uint64_t size:1;
+    uint64_t reserved_1:1;
+    uint64_t guest:1;
+    uint64_t reserved_2:9;
+    uint64_t address:52;
+#endif /* __BIG_ENDIAN_BITFIELD */
+} CMDInvalIOTLBPages;
+
+/* invalidate all cached interrupt info for devid */
+typedef struct QEMU_PACKED {
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t type:4;          /* command type        */
+    uint64_t reserved_1:44;
+    uint64_t devid:16;        /* related devid       */
+#else
+    uint64_t devid:16;
+    uint64_t reserved_1:44;
+    uint64_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+    uint64_t reserved_2;
+} CMDInvalIntrTable;
+
+/* load adddress translation info for devid into translation cache */
+typedef struct QEMU_PACKED {
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t type:4;          /* command type       */
+    uint64_t reserved_2:8;
+    uint64_t pasid_19_0:20;
+    uint64_t pfcount_7_0:8;
+    uint64_t reserved_1:8;
+    uint64_t devid:16;        /* related devid      */
+#else
+    uint64_t devid:16;
+    uint64_t reserved_1:8;
+    uint64_t pfcount_7_0:8;
+    uint64_t pasid_19_0:20;
+    uint64_t reserved_2:8;
+    uint64_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t address:52;     /* invalidate address       */
+    uint64_t reserved_5:7;
+    uint64_t inval:1;        /* inval matching entries   */
+    uint64_t reserved_4:1;
+    uint64_t guest:1;        /* G/N invalidate           */
+    uint64_t reserved_3:1;
+    uint64_t size:1;         /* prefetched page size     */
+#else
+    uint64_t size:1;
+    uint64_t reserved_3:1;
+    uint64_t guest:1;
+    uint64_t reserved_4:1;
+    uint64_t inval:1;
+    uint64_t reserved_5:7;
+    uint64_t address:52;
+#endif /* __BIG_ENDIAN_BITFIELD */
+} CMDPrefetchPages;
+
+/* clear all address translation/interrupt remapping caches */
+typedef struct QEMU_PACKED {
+#ifdef HOST_WORDS_BIGENDIAN
+    uint64_t type:4;              /* command type       */
+    uint64_t reserved_1:60;
+#else
+    uint64_t reserved_1:60;
+    uint64_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+    uint64_t reserved_2;
+} CMDInvalIommuAll;
+
+/* issue a PCIe completion packet for devid */
+typedef struct QEMU_PACKED {
+    uint32_t reserved_1:16;
+    uint32_t devid:16;
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint32_t type:4;              /* command type       */
+    uint32_t reserved_2:8;
+    uint32_t pasid_19_0:20
+#else
+    uint32_t pasid_19_0:20;
+    uint32_t reserved_2:8;
+    uint32_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint32_t reserved_3:29;
+    uint32_t guest:1;

hw/i386/amd_iommu.c line 256

Quoted 10 lines
hw/i386/amd_iommu.c new file +10 -0
+    uint32_t reserved_4:2;
+#else
+    uint32_t reserved_3:2;
+    uint32_t guest:1;
+    uint32_t reserved_4:29;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+    uint32_t completion_tag:16;  /* PCIe PRI information */
+    uint32_t reserved_5:16;
+} CMDCompletePPR;

So as Peter points, out, we don't do this kind of
thing. Pls drop this ifdefery and rework using
masks and cpu_to_le. E.g.

hw/i386/amd_iommu.c line 252

Quoted 9 lines
hw/i386/amd_iommu.c new file +9 -0
+#ifdef HOST_WORDS_BIGENDIAN
+    uint32_t reserved_3:29;
+    uint32_t guest:1;
+    uint32_t reserved_4:2;
+#else
+    uint32_t reserved_3:2;
+    uint32_t guest:1;
+    uint32_t reserved_4:29;
+#endif /* __BIG_ENDIAN_BITFIELD */
    guest = 1;

->

#define GUEST cpu_to_le32(0x1 << 2)
uint32_t guest;

guest = GUEST;

Signature · 1 line
MST
#5 lore raw

Re: [Qemu-devel] [V17 3/4] hw/i386: Introduce AMD IOMMU

Quoted 1 line · Michael S. Tsirkin

On Wed, Aug 31, 2016 at 07:17:42PM +0300, David Kiarie wrote:

Hi Michael,

Quoted 45 lines
hw/i386/amd_iommu.c new file +29 -0
+
+/* issue a PCIe completion packet for devid */
+typedef struct QEMU_PACKED {
+    uint32_t reserved_1:16;
+    uint32_t devid:16;
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint32_t type:4;              /* command type       */
+    uint32_t reserved_2:8;
+    uint32_t pasid_19_0:20
+#else
+    uint32_t pasid_19_0:20;
+    uint32_t reserved_2:8;
+    uint32_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint32_t reserved_3:29;
+    uint32_t guest:1;
+    uint32_t reserved_4:2;
+#else
+    uint32_t reserved_3:2;
+    uint32_t guest:1;
+    uint32_t reserved_4:29;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+    uint32_t completion_tag:16;  /* PCIe PRI information */
+    uint32_t reserved_5:16;
+} CMDCompletePPR;

So as Peter points, out, we don't do this kind of
thing. Pls drop this ifdefery and rework using
masks and cpu_to_le. E.g.

     > +#ifdef HOST_WORDS_BIGENDIAN
     > +    uint32_t reserved_3:29;
     > +    uint32_t guest:1;
     > +    uint32_t reserved_4:2;
     > +#else
     > +    uint32_t reserved_3:2;
     > +    uint32_t guest:1;
     > +    uint32_t reserved_4:29;
     > +#endif /* __BIG_ENDIAN_BITFIELD */

     guest = 1;
Quoted 6 lines

->

#define GUEST cpu_to_le32(0x1 << 2)
uint32_t guest;

guest = GUEST;

This might not solve the problem we are encountering here. I don't intent to write any values to these fields. I only intend to read.

I read into these structs using 'dma_memory_read' which gives me a memory order dependent on the host. Byte swapping the read buffer will lead into some of the fields being permanently corrupted since they span across byte boundaries. I actually didn't have any bitfields(and was not taking care of BE) but then there were some complains that the code was barely readable hence the bitfields and later the ifdefinery but I've never checked that the BE code complies.

Quoted 2 lines
#6 lore raw

Re: [Qemu-devel] [V17 3/4] hw/i386: Introduce AMD IOMMU

Quoted 64 lines · David Kiarie
Quoted 2 lines · Michael S. Tsirkin

On Wed, Aug 31, 2016 at 07:17:42PM +0300, David Kiarie wrote:

Hi Michael,

+

Quoted 44 lines
hw/i386/amd_iommu.c new file +28 -0
+/* issue a PCIe completion packet for devid */
+typedef struct QEMU_PACKED {
+    uint32_t reserved_1:16;
+    uint32_t devid:16;
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint32_t type:4;              /* command type       */
+    uint32_t reserved_2:8;
+    uint32_t pasid_19_0:20
+#else
+    uint32_t pasid_19_0:20;
+    uint32_t reserved_2:8;
+    uint32_t type:4;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+#ifdef HOST_WORDS_BIGENDIAN
+    uint32_t reserved_3:29;
+    uint32_t guest:1;
+    uint32_t reserved_4:2;
+#else
+    uint32_t reserved_3:2;
+    uint32_t guest:1;
+    uint32_t reserved_4:29;
+#endif /* __BIG_ENDIAN_BITFIELD */
+
+    uint32_t completion_tag:16;  /* PCIe PRI information */
+    uint32_t reserved_5:16;
+} CMDCompletePPR;

So as Peter points, out, we don't do this kind of
thing. Pls drop this ifdefery and rework using
masks and cpu_to_le. E.g.

     > +#ifdef HOST_WORDS_BIGENDIAN
     > +    uint32_t reserved_3:29;
     > +    uint32_t guest:1;
     > +    uint32_t reserved_4:2;
     > +#else
     > +    uint32_t reserved_3:2;
     > +    uint32_t guest:1;
     > +    uint32_t reserved_4:29;
     > +#endif /* __BIG_ENDIAN_BITFIELD */

     guest = 1;
Quoted 7 lines

->

#define GUEST cpu_to_le32(0x1 << 2)
uint32_t guest;

guest = GUEST;

This might not solve the problem we are encountering here. I don't intent to write any values to these fields. I only intend to read.

Quoted 7 lines

I read into these structs using 'dma_memory_read' which gives me a memory order dependent on the host. Byte swapping the read buffer will lead into some of the fields being permanently corrupted since they span across byte boundaries. I actually didn't have any bitfields(and was not taking care of BE) but then there were some complains that the code was barely readable hence the bitfields and later the ifdefinery but I've never checked that the BE code complies.

'extract64' seems to solve everything though so I can get rid of all the host dependent defines: didn't know we had this before.

Quoted 1 line
#7 lore raw
In reply to David Kiarie
Quoted 3 lines · David Kiarie

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Jan, Igor, any comments on this one?
I suspect it's time we merged this and followed-up upstream.

Quoted 79 lines

Changes since v16 - this is mainly supposed to come as a ping :-)
-minor endian-ness fixes

Changes since v15
-Endian-ness issue fix
-cleaned up unused macros
-removed guest frame number(gfn) from cache entry

Changes since v14
-MMIO register reading/write bug fix [Peter]
-Endian-ness issue fix[Peter]
-Bitfields layouts in IOMMU commands fix[Peter]
-IVRS changed IVHD device entry from type 3 to 1 to save a few bytes -coding style issues, comment grammer and other miscellaneous fixes.

Changes since v13
-Added an error to make AMD IOMMU incompatible with device assignment.[Alex] -Converted AMD IOMMU into a composite PCI and System Bus device. This helps with:
-We can now inherit from X86 IOMMU base class(which is implemented as a System Bus device). -We can now reserve MMIO region for IOMMU without a BAR register and without a hack.

Changes since v12

-Coding style fixes [Jan, Michael]
-Error logging fix to avoid using a macro[Jan] -moved some PCI macros to PCI header[Jan]
-Use a lookup table for MMIO register names when tracing[Jan]

Changes since V11
-AMD IOMMU is not started with -device amd-iommu (with a dependency on Marcel's patches). -IOMMU commands are represented using bitfields which is less error prone and more readable[Peter] -Changed from debug fprintfs to tracing[Jan]

Changes since V10

-Support for huge pages including some obscure AMD IOMMU feature that allows default page size override[Jan].
-Fixed an issue with generation of interrupts. We noted that AMD IOMMU has BusMaster- and is therefore not able to generate interrupts like any other PCI device. We have resulted in writing directly to system address but this could be fixed by some patches which have not been merged yet.

Changes since v9

-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 to ensure no DMA unless a device is configured to allow it. -An issue with the emulate IOMMU defaulting to AMD_IOMMU has been fixed[Marcel]

You can test[1] this patches by starting with parameters
qemu-system-x86_64 -M -device amd-iommu -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/pci: Prepare for AMD IOMMU hw/i386/trace-events: Add AMD IOMMU trace events hw/i386: Introduce AMD IOMMU
hw/i386: AMD IOMMU IVRS table

 hw/acpi/aml-build.c         |    2 +-
 hw/i386/Makefile.objs       |    1 +
 hw/i386/acpi-build.c        |   76 ++-
 hw/i386/amd_iommu.c         | 1383 +++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h         |  289 +++++++++
 hw/i386/intel_iommu.c       |    1 +
 hw/i386/trace-events        |   29 +
 hw/i386/x86-iommu.c         |    6 +
 include/hw/acpi/aml-build.h |    1 +
 include/hw/i386/x86-iommu.h |   12 +
 include/hw/pci/pci.h        |    4 +-
 11 files changed, 1793 insertions(+), 11 deletions(-)
 create mode 100644 hw/i386/amd_iommu.c
 create mode 100644 hw/i386/amd_iommu.h
Signature · 1 line
2.1.4
#8 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 3 lines · David Kiarie

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Jan, Igor, any comments on this one?
I suspect it's time we merged this and followed-up upstream.

Yeah, I don't see any blocking point.

V17 - that's quite some history... There are always things to do even more elegantly and probably also some undiscovered bugs remain, but the series is non-invasive and should really better continue its life in-tree.

Jan

#9 lore raw
In reply to David Kiarie
Quoted 6 lines · David Kiarie

Hi all,

This patchset adds basic AMD IOMMU emulation support to Qemu.

Changes since v16 - this is mainly supposed to come as a ping :-)
-minor endian-ness fixes

This doesn't build on BE so I dropped this.

Quoted 76 lines

Changes since v15
-Endian-ness issue fix
-cleaned up unused macros
-removed guest frame number(gfn) from cache entry

Changes since v14
-MMIO register reading/write bug fix [Peter]
-Endian-ness issue fix[Peter]
-Bitfields layouts in IOMMU commands fix[Peter]
-IVRS changed IVHD device entry from type 3 to 1 to save a few bytes -coding style issues, comment grammer and other miscellaneous fixes.

Changes since v13
-Added an error to make AMD IOMMU incompatible with device assignment.[Alex] -Converted AMD IOMMU into a composite PCI and System Bus device. This helps with:
-We can now inherit from X86 IOMMU base class(which is implemented as a System Bus device). -We can now reserve MMIO region for IOMMU without a BAR register and without a hack.

Changes since v12

-Coding style fixes [Jan, Michael]
-Error logging fix to avoid using a macro[Jan] -moved some PCI macros to PCI header[Jan]
-Use a lookup table for MMIO register names when tracing[Jan]

Changes since V11
-AMD IOMMU is not started with -device amd-iommu (with a dependency on Marcel's patches). -IOMMU commands are represented using bitfields which is less error prone and more readable[Peter] -Changed from debug fprintfs to tracing[Jan]

Changes since V10

-Support for huge pages including some obscure AMD IOMMU feature that allows default page size override[Jan].
-Fixed an issue with generation of interrupts. We noted that AMD IOMMU has BusMaster- and is therefore not able to generate interrupts like any other PCI device. We have resulted in writing directly to system address but this could be fixed by some patches which have not been merged yet.

Changes since v9

-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 to ensure no DMA unless a device is configured to allow it. -An issue with the emulate IOMMU defaulting to AMD_IOMMU has been fixed[Marcel]

You can test[1] this patches by starting with parameters
qemu-system-x86_64 -M -device amd-iommu -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/pci: Prepare for AMD IOMMU hw/i386/trace-events: Add AMD IOMMU trace events hw/i386: Introduce AMD IOMMU
hw/i386: AMD IOMMU IVRS table

 hw/acpi/aml-build.c         |    2 +-
 hw/i386/Makefile.objs       |    1 +
 hw/i386/acpi-build.c        |   76 ++-
 hw/i386/amd_iommu.c         | 1383 +++++++++++++++++++++++++++++++++++++++++++
 hw/i386/amd_iommu.h         |  289 +++++++++
 hw/i386/intel_iommu.c       |    1 +
 hw/i386/trace-events        |   29 +
 hw/i386/x86-iommu.c         |    6 +
 include/hw/acpi/aml-build.h |    1 +
 include/hw/i386/x86-iommu.h |   12 +
 include/hw/pci/pci.h        |    4 +-
 11 files changed, 1793 insertions(+), 11 deletions(-)
 create mode 100644 hw/i386/amd_iommu.c
 create mode 100644 hw/i386/amd_iommu.h
Signature · 1 line
2.1.4