amd_iommu fixes for 10.2
cover amd_iommu fixes for 10.2
Hi,
Please queue these AMD vIOMMU fixes for 10.2, with the following changes:
- Devices now work correctly when placed on PCI bus id != 0
- Fixed IOTLB aliasing when using emulated devices and forcedac=1
- Updated maintainer entry
I edited the commit messages in Sairaj's series to correct minor issues in addition to the ones you pointed out.
Thank you,
Alejandro
---
Also available on github:
The following changes since commit a8e63c013016f9ff981689189c5b063551d04559:
Merge tag 'igvm-20251103--pull-request' of https://gitlab.com/kraxel/qemu into staging (2025-11-03 10:21:01 +0100)
are available in the Git repository at:
https://github.com/aljimenezb/qemu.git tags/amdvi-10.2-110325
for you to fetch changes up to 6a242996080d8312855511600cb93cc84d495e4c:
amd_iommu: Support 64-bit address for IOTLB lookup (2025-11-03 19:44:00 +0000)
Alejandro Jimenez (1):
MAINTAINERS: Update entry for AMD-Vi Emulation
Sairaj Kodilkar (2):
amd_iommu: Fix handling of devices on buses != 0
amd_iommu: Support 64-bit address for IOTLB lookup
MAINTAINERS | 6 +- hw/i386/amd_iommu.c | 179 +++++++++++++++++++++++++++----------------- hw/i386/amd_iommu.h | 6 +- 3 files changed, 117 insertions(+), 74 deletions(-)
base-commit: a8e63c013016f9ff981689189c5b063551d04559
Signature · 1 line
2.43.5
PATCH 1/3 MAINTAINERS: Update entry for AMD-Vi Emulation
Add myself as maintainer and Sairaj Kodilkar as reviewer.
--- MAINTAINERS | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
MAINTAINERS +4 -2
@@ -3932,8 +3932,10 @@ F: tests/functional/x86_64/test_intel_iommu.py F: tests/qtest/intel-iommu-test.c AMD-Vi Emulation -S: Orphan -F: hw/i386/amd_iommu.? +M: Alejandro Jimenez <[email protected]> +R: Sairaj Kodilkar <[email protected]> +S: Supported +F: hw/i386/amd_iommu* OpenSBI Firmware L: [email protected]
Signature · 1 line
2.43.5
PATCH 2/3 amd_iommu: Fix handling of devices on buses != 0
In reply to Alejandro JimenezFrom: Sairaj Kodilkar <[email protected]>
The AMD IOMMU is set up at boot time and uses PCI bus numbers + devfn for indexing into the DTE. The problem is that before the guest starts, all PCI bus numbers are 0 as no PCI discovery has happened yet (BIOS and/or kernel will do that later), so relying on the bus number is wrong.
The immediate effect is that emulated devices cannot do DMA when placed on a bus other than 0.
Replace the static address_space array with a hash table keyed by devfn and PCIBus*, since these values do not change after the guest boots.
--- hw/i386/amd_iommu.c | 134 ++++++++++++++++++++++++++------------------ hw/i386/amd_iommu.h | 2 +- 2 files changed, 79 insertions(+), 57 deletions(-)
hw/i386/amd_iommu.c +78 -56
@@ -59,7 +59,7 @@ const char *amdvi_mmio_high[] = { };struct AMDVIAddressSpace { -uint8_t bus_num;/* bus number */ +PCIBus *bus;/* PCIBus (for bus number) */ uint8_t devfn;/* device function */ AMDVIState *iommu_state;/* AMDVI - one per machine */ MemoryRegion root;/* AMDVI Root memory map region */
@@ -101,6 +101,11 @@ typedef enum AMDVIFaultReason {AMDVI_FR_PT_ENTRY_INV ,/* Failure to read PTE from guest memory */ } AMDVIFaultReason; +typedef struct AMDVIAsKey { +PCIBus *bus; +uint8_t devfn; +}AMDVIAsKey ; +uint64_t amdvi_extended_feature_register (AMDVIState *s) {uint64_t feature =AMDVI_DEFAULT_EXT_FEATURES ;
@@ -382,6 +387,44 @@ static guint amdvi_uint64_hash(gconstpointer v)return (guint)*(const uint64_t *)v; } +static gboolean amdvi_as_equal (gconstpointer v1,gconstpointer v2) +{ +const AMDVIAsKey *key1 = v1; +const AMDVIAsKey *key2 = v2; + +return key1->bus == key2->bus && key1->devfn == key2->devfn; +} + +static guint amdvi_as_hash (gconstpointer v) +{ +const AMDVIAsKey *key = v; +guint bus = (guint )(uintptr_t )key->bus; + +return (guint)(bus <<8 | (uint )key->devfn); +} + +static AMDVIAddressSpace *amdvi_as_lookup (AMDVIState *s,PCIBus *bus, +uint8_t devfn) +{ +const AMDVIAsKey key = { .bus = bus, .devfn = devfn }; +return g_hash_table_lookup (s->address_spaces, &key); +} + +static gboolean amdvi_find_as_by_devid (gpointer key,gpointer value, +gpointer user_data) +{ +const AMDVIAsKey *as = key; +const uint16_t *devidp = user_data; + +return *devidp ==PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn); +} + +static AMDVIAddressSpace *amdvi_get_as_by_devid (AMDVIState *s,uint16_t devid) +{ +return g_hash_table_find (s->address_spaces, + amdvi_find_as_by_devid, &devid); +} +static AMDVIIOTLBEntry *amdvi_iotlb_lookup (AMDVIState *s,hwaddr addr,uint64_t devid) {
@@ -551,7 +594,7 @@ static inline uint64_t amdvi_get_pte_entry(AMDVIState *s, uint64_t pte_addr,static int amdvi_as_to_dte (AMDVIAddressSpace *as,uint64_t *dte) { -uint16_t devid =PCI_BUILD_BDF (as->bus_num, as->devfn); +uint16_t devid =PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn);AMDVIState *s = as->iommu_state;if (!amdvi_get_dte (s, devid, dte)) {
@@ -1011,25 +1054,15 @@ static void amdvi_switch_address_space(AMDVIAddressSpace *amdvi_as)*/ static void amdvi_reset_address_translation_all (AMDVIState *s) { -AMDVIAddressSpace **iommu_as; - -for (int bus_num =0 ; bus_num <PCI_BUS_MAX ; bus_num++) { +AMDVIAddressSpace *iommu_as; +GHashTableIter as_it; -/* Nothing to do if there are no devices on the current bus */ -if (!s->address_spaces[bus_num]) { -continue ; - } - iommu_as = s->address_spaces[bus_num]; +g_hash_table_iter_init (&as_it, s->address_spaces); -for (int devfn =0 ; devfn <PCI_DEVFN_MAX ; devfn++) { - -if (!iommu_as[devfn]) { -continue ; - } -/* Use passthrough as default mode after reset */ - iommu_as[devfn]->addr_translation = false; -amdvi_switch_address_space (iommu_as[devfn]); - } +while (g_hash_table_iter_next (&as_it,NULL , (void **)&iommu_as)) { +/* Use passthrough as default mode after reset */ + iommu_as->addr_translation = false; +amdvi_switch_address_space (iommu_as); } }
@@ -1089,27 +1122,15 @@ static void enable_nodma_mode(AMDVIAddressSpace *as)*/ static void amdvi_update_addr_translation_mode (AMDVIState *s,uint16_t devid) { -uint8_t bus_num, devfn, dte_mode; +uint8_t dte_mode;AMDVIAddressSpace *as;uint64_t dte[4 ] = {0 };int ret; -/* -* Convert the devid encoded in the command to a bus and devfn in -* order to retrieve the corresponding address space. -*/ - bus_num =PCI_BUS_NUM (devid); - devfn = devid &0xff ; - -/* -* The main buffer of size (AMDVIAddressSpace *) * (PCI_BUS_MAX) has already -* been allocated within AMDVIState, but must be careful to not access -* unallocated devfn. -*/ -if (!s->address_spaces[bus_num] || !s->address_spaces[bus_num][devfn]) { + as =amdvi_get_as_by_devid (s, devid); +if (!as) {return ; } - as = s->address_spaces[bus_num][devfn]; ret =amdvi_as_to_dte (as, dte);
@@ -1783,7 +1804,7 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr, bool is_write, IOMMUTLBEntry *ret) { AMDVIState *s = as->iommu_state; -uint16_t devid =PCI_BUILD_BDF (as->bus_num, as->devfn); +uint16_t devid =PCI_BUILD_BDF (pci_bus_num (as->bus), as->devfn);AMDVIIOTLBEntry *iotlb_entry =amdvi_iotlb_lookup (s, addr, devid);uint64_t entry[4 ];int dte_ret;
@@ -1858,7 +1879,7 @@ static IOMMUTLBEntry amdvi_translate(IOMMUMemoryRegion *iommu, hwaddr addr, }amdvi_do_translate (as, addr, flag &IOMMU_WO , &ret); -trace_amdvi_translation_result (as->bus_num,PCI_SLOT (as->devfn), +trace_amdvi_translation_result (pci_bus_num (as->bus),PCI_SLOT (as->devfn),PCI_FUNC (as->devfn), addr, ret.translated_addr);return ret; }
@@ -2222,30 +2243,28 @@ static AddressSpace *amdvi_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) {char name[128 ];AMDVIState *s = opaque; -AMDVIAddressSpace **iommu_as, *amdvi_dev_as; -int bus_num =pci_bus_num (bus); +AMDVIAddressSpace *amdvi_dev_as; +AMDVIAsKey *key; - iommu_as = s->address_spaces[bus_num]; + amdvi_dev_as =amdvi_as_lookup (s, bus, devfn);/* allocate memory during the first run */ -if (!iommu_as) { - iommu_as = g_new0(AMDVIAddressSpace *, PCI_DEVFN_MAX); - s->address_spaces[bus_num] = iommu_as; - } - -/* set up AMD-Vi region */ -if (!iommu_as[devfn]) { +if (!amdvi_dev_as) {snprintf (name,sizeof (name),"amd_iommu_devfn_%d" , devfn); - iommu_as[devfn] =g_new0 (AMDVIAddressSpace,1 ); - iommu_as[devfn]->bus_num = (uint8_t )bus_num; - iommu_as[devfn]->devfn = (uint8_t )devfn; - iommu_as[devfn]->iommu_state = s; - iommu_as[devfn]->notifier_flags =IOMMU_NOTIFIER_NONE ; - iommu_as[devfn]->iova_tree =iova_tree_new (); - iommu_as[devfn]->addr_translation = false; + amdvi_dev_as =g_new0 (AMDVIAddressSpace,1 ); + key =g_new0 (AMDVIAsKey,1 ); - amdvi_dev_as = iommu_as[devfn]; + amdvi_dev_as->bus = bus; + amdvi_dev_as->devfn = (uint8_t )devfn; + amdvi_dev_as->iommu_state = s; + amdvi_dev_as->notifier_flags =IOMMU_NOTIFIER_NONE ; + amdvi_dev_as->iova_tree =iova_tree_new (); + amdvi_dev_as->addr_translation = false; + key->bus = bus; + key->devfn = devfn; + +g_hash_table_insert (s->address_spaces, key, amdvi_dev_as);/* * Memory region relationships looks like (Address range shows
@@ -2288,7 +2307,7 @@ static AddressSpace *amdvi_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)amdvi_switch_address_space (amdvi_dev_as); } -return &iommu_as[devfn]->as; +return &amdvi_dev_as->as; }static const PCIIOMMUOps amdvi_iommu_ops = {
@@ -2329,7 +2348,7 @@ static int amdvi_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,if (!s->dma_remap && (new &IOMMU_NOTIFIER_MAP )) {error_setg_errno (errp,ENOTSUP ,"device %02x.%02x.%x requires dma-remap=1" , - as->bus_num,PCI_SLOT (as->devfn),PCI_FUNC (as->devfn)); +pci_bus_num (as->bus),PCI_SLOT (as->devfn),PCI_FUNC (as->devfn));return -ENOTSUP ; }
@@ -2510,6 +2529,9 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp) s->iotlb =g_hash_table_new_full (amdvi_uint64_hash, amdvi_uint64_equal, g_free, g_free); + s->address_spaces =g_hash_table_new_full (amdvi_as_hash, + amdvi_as_equal, g_free, g_free); +/* set up MMIO */ memory_region_init_io (&s->mr_mmio,OBJECT (s), &mmio_mem_ops, s,"amdvi-mmio" ,AMDVI_MMIO_SIZE );
hw/i386/amd_iommu.h +1 -1
@@ -408,7 +408,7 @@ struct AMDVIState {bool mmio_enabled;/* for each served device */ -AMDVIAddressSpace **address_spaces[PCI_BUS_MAX ]; +GHashTable *address_spaces;/* list of address spaces with registered notifiers */ QLIST_HEAD(, AMDVIAddressSpace) amdvi_as_with_notifiers;
Signature · 1 line
2.43.5
PATCH 3/3 amd_iommu: Support 64-bit address for IOTLB lookup
In reply to Alejandro JimenezFrom: Sairaj Kodilkar <[email protected]>
The physical AMD IOMMU supports up to 64 bits of IOVA. When a device tries to read or write from a given DMA address, the IOMMU translates the address using the I/O page tables assigned to that device. Since the emulated IOMMU uses per-device page tables, an ideal cache tag would need to be 68 bits (64-bit address - 12-bit page alignment + 16-bit device ID).
The current software IOTLB implementation uses a GLib hash table with a 64-bit key to hash both the IOVA and device ID, which limits the IOVA to 60 bits. This causes a failure while setting up the device when a guest is booted with "iommu.forcedac=1", which forces the use of DMA addresses at the top of the 64-bit address space.
To address this issue, construct the 64-bit hash key using the upper 52 bits of IOVA (GFN) and lower 12 bits of the device ID to avoid truncation as much as possible (reducing hash collisions).
--- hw/i386/amd_iommu.c | 57 ++++++++++++++++++++++++++++++--------------- hw/i386/amd_iommu.h | 4 ++-- 2 files changed, 40 insertions(+), 21 deletions(-)
hw/i386/amd_iommu.c +38 -19
@@ -106,6 +106,11 @@ typedef struct AMDVIAsKey {uint8_t devfn; } AMDVIAsKey; +typedef struct AMDVIIOTLBKey { +uint64_t gfn; +uint16_t devid; +}AMDVIIOTLBKey ; +uint64_t amdvi_extended_feature_register (AMDVIState *s) {uint64_t feature =AMDVI_DEFAULT_EXT_FEATURES ;
@@ -377,16 +382,6 @@ static void amdvi_log_pagetab_error(AMDVIState *s, uint16_t devid, PCI_STATUS_SIG_TARGET_ABORT); } -static gboolean amdvi_uint64_equal (gconstpointer v1,gconstpointer v2) -{ -return *((const uint64_t *)v1) == *((const uint64_t *)v2); -} - -static guint amdvi_uint64_hash (gconstpointer v) -{ -return (guint)*(const uint64_t *)v; -} -static gboolean amdvi_as_equal (gconstpointer v1,gconstpointer v2) {const AMDVIAsKey *key1 = v1;
@@ -425,11 +420,30 @@ static AMDVIAddressSpace *amdvi_get_as_by_devid(AMDVIState *s, uint16_t devid) amdvi_find_as_by_devid, &devid); } +static gboolean amdvi_iotlb_equal (gconstpointer v1,gconstpointer v2) +{ +const AMDVIIOTLBKey *key1 = v1; +const AMDVIIOTLBKey *key2 = v2; + +return key1->devid == key2->devid && key1->gfn == key2->gfn; +} + +static guint amdvi_iotlb_hash (gconstpointer v) +{ +const AMDVIIOTLBKey *key = v; +/* Use GPA and DEVID to find the bucket */ +return (guint)(key->gfn <<AMDVI_PAGE_SHIFT_4K | + (key->devid & ~AMDVI_PAGE_MASK_4K )); +} + +static AMDVIIOTLBEntry *amdvi_iotlb_lookup (AMDVIState *s,hwaddr addr,uint64_t devid) { -uint64_t key = (addr >>AMDVI_PAGE_SHIFT_4K ) | - ((uint64_t )(devid) <<AMDVI_DEVID_SHIFT ); +AMDVIIOTLBKey key = { + .gfn =AMDVI_GET_IOTLB_GFN (addr), + .devid = devid, + };return g_hash_table_lookup (s->iotlb, &key); }
@@ -451,8 +465,10 @@ static gboolean amdvi_iotlb_remove_by_devid(gpointer key, gpointer value,static void amdvi_iotlb_remove_page (AMDVIState *s,hwaddr addr,uint64_t devid) { -uint64_t key = (addr >>AMDVI_PAGE_SHIFT_4K ) | - ((uint64_t )(devid) <<AMDVI_DEVID_SHIFT ); +AMDVIIOTLBKey key = { + .gfn =AMDVI_GET_IOTLB_GFN (addr), + .devid = devid, + };g_hash_table_remove (s->iotlb, &key); }
@@ -463,8 +479,10 @@ static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,/* don't cache erroneous translations */ if (to_cache.perm !=IOMMU_NONE ) {AMDVIIOTLBEntry *entry =g_new (AMDVIIOTLBEntry,1 ); -uint64_t *key =g_new (uint64_t,1 ); -uint64_t gfn = gpa >>AMDVI_PAGE_SHIFT_4K ; +AMDVIIOTLBKey *key =g_new (AMDVIIOTLBKey,1 ); + + key->gfn =AMDVI_GET_IOTLB_GFN (gpa); + key->devid = devid;trace_amdvi_cache_update (domid,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), gpa, to_cache.translated_addr);
@@ -477,7 +495,8 @@ static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid, entry->perms = to_cache.perm; entry->translated_addr = to_cache.translated_addr; entry->page_mask = to_cache.addr_mask; - *key = gfn | ((uint64_t )(devid) <<AMDVI_DEVID_SHIFT ); + entry->devid = devid; +g_hash_table_replace (s->iotlb, key, entry); } }
@@ -2526,8 +2545,8 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp) } } - s->iotlb =g_hash_table_new_full (amdvi_uint64_hash, - amdvi_uint64_equal, g_free, g_free); + s->iotlb =g_hash_table_new_full (amdvi_iotlb_hash, + amdvi_iotlb_equal, g_free, g_free); s->address_spaces =g_hash_table_new_full (amdvi_as_hash, amdvi_as_equal, g_free, g_free);
hw/i386/amd_iommu.h +2 -2
@@ -220,8 +220,8 @@#define PAGE_SIZE_PTE_COUNT (pgsz) (1ULL << ((ctz64(pgsz) - 12) % 9))/* IOTLB */ -#define AMDVI_IOTLB_MAX_SIZE 1024 -#define AMDVI_DEVID_SHIFT 36 +#define AMDVI_IOTLB_MAX_SIZE 1024 +#define AMDVI_GET_IOTLB_GFN (addr) (addr >> AMDVI_PAGE_SHIFT_4K)/* default extended feature */ #define AMDVI_DEFAULT_EXT_FEATURES \
Signature · 1 line
2.43.5