Skip to contentSkip to conversation

AMD IOMMU emulation

RFC PATCH In review Eduard - Gabriel Munteanu 3 participants 6 messages May 20, 2010 – May 25, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 AMD IOMMU emulation

This is preliminary work for AMD IOMMU emulation support.

---
 Makefile.target |    2 +
 configure       |    9 +
 hw/amd_iommu.c  |  442 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pc.c         |    2 +
 hw/pc.h         |    3 +
 hw/pci_ids.h    |    2 +
 hw/pci_regs.h   |    1 +
 7 files changed, 461 insertions(+), 0 deletions(-)
 create mode 100644 hw/amd_iommu.c
Makefile.target +2 -0
index 0bdb184..13f8086 100644
@@ -217,6 +217,8 @@ obj-i386-y += testdev.o
 obj-i386-$(CONFIG_KVM_PIT) += i8254-kvm.o
 obj-i386-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += device-assignment.o
 
+obj-i386-$(CONFIG_AMD_IOMMU) += amd_iommu.o
+
 # Hardware support
 obj-ia64-y += ide.o pckbd.o vga.o $(SOUND_HW) dma.o $(AUDIODRV)
 obj-ia64-y += fdc.o mc146818rtc.o serial.o i8259.o ipf.o
configure +9 -0
index ed8e17b..34e5194 100755
@@ -305,6 +305,7 @@ mixemu="no"
 kvm_trace="no"
 kvm_cap_pit=""
 kvm_cap_device_assignment=""
+amd_iommu="no"
 kerneldir=""
 aix="no"
 blobs="yes"
@@ -603,6 +604,8 @@ for opt do
   ;;
   --enable-kvm-device-assignment) kvm_cap_device_assignment="yes"
   ;;
+  --enable-amd-iommu-emul) amd_iommu="yes"
+  ;;
   --enable-profiler) profiler="yes"
   ;;
   --enable-cocoa)
@@ -829,6 +832,8 @@ echo "  --disable-kvm-pit        disable KVM pit support"
 echo "  --enable-kvm-pit         enable KVM pit support"
 echo "  --disable-kvm-device-assignment  disable KVM device assignment support"
 echo "  --enable-kvm-device-assignment   enable KVM device assignment support"
+echo "  --disable-amd-iommu-emul disable AMD IOMMU emulation"
+echo "  --enable-amd-iommu-emul  enable AMD IOMMU emulation"
 echo "  --disable-nptl           disable usermode NPTL support"
 echo "  --enable-nptl            enable usermode NPTL support"
 echo "  --enable-system          enable all system emulation targets"
@@ -2185,6 +2190,7 @@ echo "KVM support       $kvm"
 echo "KVM PIT support   $kvm_cap_pit"
 echo "KVM device assig. $kvm_cap_device_assignment"
 echo "KVM trace support $kvm_trace"
+echo "AMD IOMMU emul.   $amd_iommu"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
 echo "fdatasync         $fdatasync"
@@ -2599,6 +2605,9 @@ case "$target_arch2" in
   x86_64)
     TARGET_BASE_ARCH=i386
     target_phys_bits=64
+    if test "$amd_iommu" = "yes"; then
+      echo "CONFIG_AMD_IOMMU=y" >> $config_target_mak
+    fi
   ;;
   ia64)
     target_phys_bits=64
hw/amd_iommu.c new file +442 -0

442 lines not on this page · raw message

hw/pc.c +2 -0
index 4a4a706..1999530 100644
@@ -998,6 +998,8 @@ void pc_pci_device_init(PCIBus *pci_bus)
     int max_bus;
     int bus;
 
+    amd_iommu_init(pci_bus);
+
     max_bus = drive_get_max_bus(IF_SCSI);
     for (bus = 0; bus <= max_bus; bus++) {
         pci_create_simple(pci_bus, -1, "lsi53c895a");
hw/pc.h +3 -0
index 20f621d..af47f13 100644
@@ -190,4 +190,7 @@ void extboot_init(BlockDriverState *bs);
 
 int e820_add_entry(uint64_t, uint64_t, uint32_t);
 
+/* amd_iommu.c */
+void amd_iommu_init(PCIBus *bus);
+
 #endif
hw/pci_ids.h +2 -0
index fe7a121..ac34779 100644
@@ -26,6 +26,7 @@
 
 #define PCI_CLASS_MEMORY_RAM             0x0500
 
+#define PCI_CLASS_SYSTEM_IOMMU           0x0806
 #define PCI_CLASS_SYSTEM_OTHER           0x0880
 
 #define PCI_CLASS_SERIAL_USB             0x0c03
@@ -56,6 +57,7 @@
 
 #define PCI_VENDOR_ID_AMD                0x1022
 #define PCI_DEVICE_ID_AMD_LANCE          0x2000
+#define PCI_DEVICE_ID_AMD_IOMMU          0x0000     /* FIXME */
 
 #define PCI_VENDOR_ID_MOTOROLA           0x1057
 #define PCI_DEVICE_ID_MOTOROLA_MPC106    0x0002
hw/pci_regs.h +1 -0
index 1c675dc..6399b5d 100644
@@ -216,6 +216,7 @@
 #define  PCI_CAP_ID_SHPC 	0x0C	/* PCI Standard Hot-Plug Controller */
 #define  PCI_CAP_ID_SSVID	0x0D	/* Bridge subsystem vendor/device ID */
 #define  PCI_CAP_ID_AGP3	0x0E	/* AGP Target PCI-PCI bridge */
+#define  PCI_CAP_ID_SEC     0x0F    /* Secure Device (AMD IOMMU) */
 #define  PCI_CAP_ID_EXP 	0x10	/* PCI Express */
 #define  PCI_CAP_ID_MSIX	0x11	/* MSI-X */
 #define  PCI_CAP_ID_AF		0x13	/* PCI Advanced Features */
Signature · 1 line
1.6.4.4
#1 lore raw

Hi Eduard,

configure for opt do · line 608

Quoted 2 lines · Eduard - Gabriel Munteanu
configure +2 -0
+  --enable-amd-iommu-emul) amd_iommu="yes"
+  ;;

A compile-time option is a good idea.

Quoted 4 lines
hw/amd_iommu.c new file +4 -0
+/* MMIO registers */
+#define MMIO_DEVICE_TABLE       0x0000
+#define MMIO_COMMAND_BASE       0x0008
+#define MMIO_EVENT_BASE         0x0010

hw/amd_iommu.c line 64

Quoted 10 lines
hw/amd_iommu.c new file +10 -0
+#define MMIO_CONTROL            0x0018
+#define MMIO_EXCL_BASE          0x0020
+#define MMIO_EXCL_LIMIT         0x0028
+#define MMIO_COMMAND_HEAD       0x2000
+#define MMIO_COMMAND_TAIL       0x2008
+#define MMIO_EVENT_HEAD         0x2010
+#define MMIO_EVENT_TAIL         0x2018
+#define MMIO_STATUS             0x2020
+
+#define MMIO_SIZE               0x2028

This size should be a power-of-two value. In this case probably 0x4000.

hw/amd_iommu.c line 67

Quoted 2 lines
hw/amd_iommu.c new file +2 -0
+#define MMIO_DEVTAB_SIZE_MASK   ((1UL << 12) - 1)
+#define MMIO_DEVTAB_BASE_MASK   (((1UL << 52) - 1) & ~MMIO_DEVTAB_SIZE_MASK)

You must use ULL to be 32bit safe. This is also true for the defines below.

[...]

Otherwise the code looks good so far. Seems like the next step should be some work on a qemu dma-layer where you can hook the translation into.

Joerg

#2 lore raw
Quoted 8 lines · Joerg Roedel

Hi Eduard,

Quoted 2 lines · Eduard - Gabriel Munteanu

+  --enable-amd-iommu-emul) amd_iommu="yes"
+  ;;

A compile-time option is a good idea.

Quoted 16 lines
Quoted 4 lines
hw/amd_iommu.c new file +4 -0
+/* MMIO registers */
+#define MMIO_DEVICE_TABLE       0x0000
+#define MMIO_COMMAND_BASE       0x0008
+#define MMIO_EVENT_BASE         0x0010

hw/amd_iommu.c line 64

Quoted 10 lines
hw/amd_iommu.c new file +10 -0
+#define MMIO_CONTROL            0x0018
+#define MMIO_EXCL_BASE          0x0020
+#define MMIO_EXCL_LIMIT         0x0028
+#define MMIO_COMMAND_HEAD       0x2000
+#define MMIO_COMMAND_TAIL       0x2008
+#define MMIO_EVENT_HEAD         0x2010
+#define MMIO_EVENT_TAIL         0x2018
+#define MMIO_STATUS             0x2020
+
+#define MMIO_SIZE               0x2028

This size should be a power-of-two value. In this case probably 0x4000.

Not really, the devices can reserve regions of any size. There were some implementation deficiencies in earlier versions of QEMU, where the whole page would be reserved anyway, but this limitation has been removed long time ago.

Quoted 15 lines

hw/amd_iommu.c line 67

Quoted 2 lines
hw/amd_iommu.c new file +2 -0
+#define MMIO_DEVTAB_SIZE_MASK   ((1UL << 12) - 1)
+#define MMIO_DEVTAB_BASE_MASK   (((1UL << 52) - 1) & ~MMIO_DEVTAB_SIZE_MASK)

You must use ULL to be 32bit safe. This is also true for the defines below.

[...]

Otherwise the code looks good so far. Seems like the next step should be some work on a qemu dma-layer where you can hook the translation into.

       Joerg

#3 lore raw
Quoted 10 lines · Blue Swirl
Quoted 4 lines · Joerg Roedel

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define MMIO_SIZE               0x2028

This size should be a power-of-two value. In this case probably 0x4000.

Not really, the devices can reserve regions of any size. There were some implementation deficiencies in earlier versions of QEMU, where the whole page would be reserved anyway, but this limitation has been removed long time ago.

The drivers for AMD IOMMU expect that to be 0x4000. At least the Linux driver maps the MMIO region with this size. So the emulation should reserve this amount of MMIO space too.

Joerg

#4 lore raw
Quoted 12 lines · Joerg Roedel
Quoted 10 lines · Blue Swirl
Quoted 4 lines · Joerg Roedel

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define MMIO_SIZE ? ? ? ? ? ? ? 0x2028

This size should be a power-of-two value. In this case probably 0x4000.

Not really, the devices can reserve regions of any size. There were some implementation deficiencies in earlier versions of QEMU, where the whole page would be reserved anyway, but this limitation has been removed long time ago.

Quoted 5 lines

The drivers for AMD IOMMU expect that to be 0x4000. At least the Linux driver maps the MMIO region with this size. So the emulation should reserve this amount of MMIO space too.

Joerg

Yeah, I'll change that, since I already reserve 0x4000 bytes in SeaBIOS for it (I did that to deal with the 16 KiB alignment requirement).

Eduard

#5 lore raw
In reply to Joerg Roedel
Quoted 7 lines · Joerg Roedel
Quoted 6 lines · Blue Swirl
Quoted 4 lines · Joerg Roedel

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define MMIO_SIZE               0x2028

This size should be a power-of-two value. In this case probably 0x4000.

Quoted 8 lines
Quoted 4 lines

Not really, the devices can reserve regions of any size. There were some implementation deficiencies in earlier versions of QEMU, where the whole page would be reserved anyway, but this limitation has been removed long time ago.

The drivers for AMD IOMMU expect that to be 0x4000. At least the Linux driver maps the MMIO region with this size. So the emulation should reserve this amount of MMIO space too.

Well, Linux drivers may take a conservative approach so I'd check what's the value in the device specs. In practice, on x86 hardware the size doesn't matter too much, for example on Sparc an access beyond the end of the device region would trap.