iommu/amd: Introduce AMD Hardware-accelerated Virtualized IOMMU (vIOMMU) Support
Subsystems (3)
Suravee Suthikulpanit
reviewerv4 cover iommu/amd: Introduce AMD Hardware-accelerated Virtualized IOMMU (vIOMMU) Support
AMD IOMMU introduces the vIOMMU feature, which provides partial hardware acceleration when implementing Guest IOMMUs. This feature provides acceleration for guest Command Buffer, Event Log, and PPR Log. This eliminates the CPU overhead needed for the supporting HV intercepts and reduces the latency of these operations.
When a guest attempts to access guest IOMMU MMIO registers with offsets between 8KB and 12KB (i.e. the 3rd 4K region) such as the Command Buffer, Event Log and PPR Log head and tail pointer registers, this is serviced directly by the IOMMU. When the IOMMU accesses a Command Buffer, PPR Log or a COMPLETION_WAIT store location in memory, it directly accesses guest physical memory. The HV/VMM continues to trap and emulate the IOMMU configuration MMIO registers between 0KB and 4KB (i.e. the 1st 4K region), which are primarily used during initialization.
Additionally, the HV must initialize the vIOMMU feature, map MMIO resources between the VMs and the IOMMU, manage additional supporting data structures in memory (e.g. GPA->SPA translation DTE, Device ID and Domain ID mapping tables), and allocate/map vIOMMU Private Address region used as backing storage memory for the IOMMU. Support for new IOMMU command and events specifically for vIOMMU are also added.
Guest IOMMUs are IOMMUs exposed to VMs with additional support from VMM (QEMU) to generate guest ACPI IVRS table and define guest PCI topology for IOMMU and pass-through VFIO devices, which are not covered by this series.
For more detail, please see the vIOMMU section of the AMD IOMMU Specification[1].
This is version 4 of the AMD HW-vIOMMU series. It is implemented on top of the IOMMUFD vIOMMU, vDevice, and nested-domain framework in Linux v7.1.0-rc4.
The series is organized into the following subsets:
Patch 1-3 : Preparatory patches Patch 4-8 : Introduce IOMMUFD vIOMMU support and VF MMIO setup Patch 9-14 : Introduce and map vIOMMU Private Address (IPA) region
Patch 15-18 : Introduce IOMMUFD vDevice support for AMD Patch 19-24 : Translate-device-ID pool, per-vIOMMU translation DTE, and
PCI-reserve relocation
Changes since v3:
(https://lore.kernel.org/linux-iommu/[email protected]/)
Series scope:
* Grows from 22 to 24 patches with split of the per-vIOMMU translate-device-id work into three patches plus PCI-reserve relocation.
IPA / DTE infrastructure (patches 9-14):
* Patch 9: Register the owning IOMMU in viommu_pdom->iommu_array via amd_iommu_pdom_bind_iommu() so map/unmap use domain_flush_pages_v1(). Remove amd_iommu_flush_private_vm_region().
IOMMUFD vDevice support (patches 16-18):
* Drop gid/gDevId from struct iommu_dev_data; obtain guest device ID via iommufd_viommu_get_vdev_id() when programming nested DTEs.
* Use ndom->viommu->gid for domain and device ID mapping table updates.
* Validate vDevice virt_id against VIOMMU_MAX_GDEVID.
Translate device ID (TransDevID) - pool and relocation (patches 19-24):
* Track allocated pool slots as pointers to the owning struct amd_iommu_viommu instead of a separate ALLOCATED state enum.
* Move translate DTE / VFctrl helpers to patch 21; amd_iommu_set_translate_dte() takes struct iommufd_viommu * and reads trans_devid from struct amd_iommu_viommu.
* Patch 22: add amd_iommu_trans_devid_alloc/free() with vIOMMU owner tracking in the per-segment xarray.
* Patch 23: assign one translate-device-id per vIOMMU on init; program translation DTE and VFctrl TransDevID; add trans_devid_lock.
* Patch 24: when PCI attach reserves a BDF already allocated to a vIOMMU, relocate that vIOMMU to a new translate-device-id before marking the BDF reserved.
Testing done:
* Single/Multiple vIOMMU instances
* Single/Multiple VFIO devices per vIOMMU instance.
[1] IOMMU Specification: https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB
[2] Linux git tree: https://github.com/AMDESE/linux-iommu/tree/linux-7.1.0-rc4-amd-viommu_upstream_v4
Thank you,
Suravee
Suravee Suthikulpanit (24):
iommu/amd: Make amd_iommu_completion_wait() non-static iommu/amd: Introduce vIOMMU-specific events and event iommu/amd: Detect and initialize AMD vIOMMU feature iommu/amd: Introduce IOMMUFD vIOMMU support for AMD iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances iommu/amd: Map vIOMMU VF and VF Control MMIO BARs iommu/amd: Add support for AMD vIOMMU VF MMIO region iommu/amd: Introduce Reset vMMIO Command
iommu/amd: Introduce and map vIOMMU private IPA region iommu/amd: Pass iommu to device_flush_dte()
iommu/amd: Export amd_iommu_alloc_dev_data() helper iommu/amd: Pass iommu and devid to amd_iommu_make_clear_dte() iommu/amd: Assign IOMMU Private Address domain to IOMMU iommu/amd: Add per-VM private IPA alloc/map helpers iommu/amd: Add helper functions to manage DevID / DomID mapping tables iommu/amd: Introduce IOMMUFD vDevice support for AMD iommu/amd: Introduce helper function for updating domain ID mapping
table
iommu/amd: Introduce helper function for updating device ID mapping
table
iommu/amd: Add per-segment translate device ID pool iommu/amd: Reserve translate-device-id for PCI requestor aliases iommu/amd: Add translation DTE and VFctrl TransDevID helpers iommu/amd: Add translate-device-id alloc/free with vIOMMU owner iommu/amd: Assign per-vIOMMU translate device ID iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve
drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 51 ++- drivers/iommu/amd/amd_iommu_types.h | 105 ++++++ drivers/iommu/amd/amd_viommu.h | 60 +++ drivers/iommu/amd/init.c | 23 +- drivers/iommu/amd/iommu.c | 290 ++++++++++++--- drivers/iommu/amd/iommufd.c | 134 +++++++ drivers/iommu/amd/nested.c | 38 +- drivers/iommu/amd/trans_devid.c | 329 ++++++++++++++++ drivers/iommu/amd/viommu.c | 558 ++++++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 10 + 11 files changed, 1543 insertions(+), 57 deletions(-) create mode 100644 drivers/iommu/amd/amd_viommu.h create mode 100644 drivers/iommu/amd/trans_devid.c create mode 100644 drivers/iommu/amd/viommu.c
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v4 1/24 iommu/amd: Make amd_iommu_completion_wait() non-static
This will be reused in a new iommufd.c file for nested translation.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/iommu.c | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 14 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -199,6 +199,7 @@ void amd_iommu_set_dte_v1(struct iommu_dev_data *dev_data,void amd_iommu_update_dte (struct amd_iommu *iommu,struct iommu_dev_data *dev_data,struct dev_table_entry *new); +int amd_iommu_completion_wait (struct amd_iommu *iommu);static inline void amd_iommu_make_clear_dte (struct iommu_dev_data *dev_data,struct dev_table_entry *new)
drivers/iommu/amd/iommu.c +12 -14
@@ -90,8 +90,6 @@ static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,static void clone_aliases (struct amd_iommu *iommu,struct device *dev); -static int iommu_completion_wait (struct amd_iommu *iommu); -/**************************************************************************** * * Helper functions
@@ -216,7 +214,7 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,update_dte256 (iommu, dev_data, new);clone_aliases (iommu, dev_data->dev);device_flush_dte (dev_data); -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); } static void get_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
@@ -1449,7 +1447,7 @@ static u64 get_cmdsem_val(struct amd_iommu *iommu)* This function queues a completion wait command into the command * buffer of an IOMMU */ -static int iommu_completion_wait (struct amd_iommu *iommu) +int amd_iommu_completion_wait (struct amd_iommu *iommu) {struct iommu_cmd cmd;unsigned long flags;
@@ -1487,7 +1485,7 @@ static void domain_flush_complete(struct protection_domain *domain)* We need to wait for completion of all commands. */ xa_for_each (&domain->iommu_array, i, pdom_iommu_info) -iommu_completion_wait (pdom_iommu_info->iommu); +amd_iommu_completion_wait (pdom_iommu_info->iommu); }static int iommu_flush_dte (struct amd_iommu *iommu,u16 devid)
@@ -1505,7 +1503,7 @@ static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid) ret =iommu_flush_dte (iommu, devid);if (!ret) -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); }static void amd_iommu_flush_dte_all (struct amd_iommu *iommu)
@@ -1516,7 +1514,7 @@ static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)for (devid =0 ; devid <= last_bdf; ++devid)iommu_flush_dte (iommu, devid); -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); }/*
@@ -1535,7 +1533,7 @@ static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)iommu_queue_command (iommu, &cmd); } -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); }static void amd_iommu_flush_tlb_domid (struct amd_iommu *iommu,u32 dom_id)
@@ -1546,7 +1544,7 @@ static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id) dom_id, IOMMU_NO_PASID, false);iommu_queue_command (iommu, &cmd); -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); }static int iommu_flush_pages_v1_hdom_ids (struct protection_domain *pdom,u64 address,size_t size)
@@ -1582,7 +1580,7 @@ static void amd_iommu_flush_all(struct amd_iommu *iommu)build_inv_all (&cmd);iommu_queue_command (iommu, &cmd); -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); }static void iommu_flush_irt (struct amd_iommu *iommu,u16 devid)
@@ -1605,7 +1603,7 @@ static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)for (devid =0 ; devid <= last_bdf; devid++)iommu_flush_irt (iommu, devid); -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); }void amd_iommu_flush_all_caches (struct amd_iommu *iommu)
@@ -1841,7 +1839,7 @@ void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data,if (dev_data->ats_enabled)device_flush_iotlb (dev_data, address, size, pasid, true); -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); } static void dev_flush_pasid_all(struct iommu_dev_data *dev_data,
@@ -2495,7 +2493,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev) goto out_err; } -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu);if (FEATURE_NUM_INT_REMAP_SUP_2K (amd_iommu_efr2)) dev_data->max_irqs =MAX_IRQS_PER_TABLE_2K ;
@@ -3392,7 +3390,7 @@ static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,set_remap_table_entry (iommu, alias, table); out_wait: -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); out_unlock:spin_unlock_irqrestore (&iommu_table_lock, flags);
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 01/24] iommu/amd: Make amd_iommu_completion_wait() non-static
Quoted 1 line · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.
With all the changes, looks like amd_iommu_completion_wait() is not used outside iommu.c. May be drop this patch?
-Vasant
Suthikulpanit, Suravee
reviewerRe: [PATCH 01/24] iommu/amd: Make amd_iommu_completion_wait() non-static
Quoted 10 lines · Vasant Hegde
Quoted 1 line · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.With all the changes, looks like
amd_iommu_completion_wait()is not used outsideiommu.c. May be drop this patch?-Vasant
Good call. I'll remove this patch in V5.
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 2/24 iommu/amd: Introduce vIOMMU-specific events and event
In reply to Suravee SuthikulpanitAdding support for new vIOMMU events:
* Guest Event Fault event
* vIOMMU Hardware Error event
Also, adding support for the additional vIOMMU related flags in existing events.
--- drivers/iommu/amd/amd_iommu_types.h | 7 ++++ drivers/iommu/amd/iommu.c | 58 ++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 13 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +7 -0
@@ -149,6 +149,9 @@#define EVENT_TYPE_IOTLB_INV_TO 0x7#define EVENT_TYPE_INV_DEV_REQ 0x8#define EVENT_TYPE_INV_PPR_REQ 0x9 +#define EVENT_TYPE_GUEST_EVENT_FAULT 0xb +#define EVENT_TYPE_VIOMMU_HW_ERR 0xc +#define EVENT_TYPE_RMP_FAULT 0xd#define EVENT_TYPE_RMP_HW_ERR 0xe#define EVENT_DEVID_MASK 0xffff
@@ -261,6 +264,10 @@#define EVTLOG_SIZE_MAX SZ_512K/* 32K entries */ #define EVTLOG_LEN_MASK_MAX (0xFULL << EVTLOG_SIZE_SHIFT) +/* Constants for IO_PAGE_FAULT event */ +#define IO_PAGE_FAULT_VFLAGS_SHIFT 27 +#define IO_PAGE_FAULT_VFLAGS_MASK GENMASK_ULL(27, 23) +/* Constants for PPR Log handling */ #define PPRLOG_ENTRY_SIZE 0x10#define PPRLOG_SIZE_SHIFT 56
drivers/iommu/amd/iommu.c +45 -13
100 lines not on this page · raw message
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 02/24] iommu/amd: Introduce vIOMMU-specific events and event
Hi Suravee,
Quoted 18 lines · Suravee Suthikulpanit
Adding support for new vIOMMU events:
* Guest Event Fault event
* vIOMMU Hardware Error eventAlso, adding support for the additional vIOMMU related flags in existing events.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 7 ++++ drivers/iommu/amd/iommu.c | 58 ++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 13 deletions(-)drivers/iommu/amd/amd_iommu_types.h +0
-0
drivers/iommu/amd/amd_iommu_types.h line 152
Quoted 4 lines
drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -149,6 +149,9 @@#define EVENT_TYPE_IOTLB_INV_TO 0x7#define EVENT_TYPE_INV_DEV_REQ 0x8#define EVENT_TYPE_INV_PPR_REQ 0x9 +#define EVENT_TYPE_GUEST_EVENT_FAULT 0xb
Better EVENT_TYPE_GUEST_FAULT ?
drivers/iommu/amd/amd_iommu_types.h line 269
Quoted 12 lines
drivers/iommu/amd/amd_iommu_types.h +5
-0+#define EVENT_TYPE_VIOMMU_HW_ERR 0xc +#define EVENT_TYPE_RMP_FAULT 0xd#define EVENT_TYPE_RMP_HW_ERR 0xe#define EVENT_DEVID_MASK 0xffff@@ -261,6 +264,10 @@#define EVTLOG_SIZE_MAX SZ_512K/* 32K entries */ #define EVTLOG_LEN_MASK_MAX (0xFULL << EVTLOG_SIZE_SHIFT) +/* Constants for IO_PAGE_FAULT event */ +#define IO_PAGE_FAULT_VFLAGS_SHIFT 27 +#define IO_PAGE_FAULT_VFLAGS_MASK GENMASK_ULL(27, 23)
You mean (31, 27) ?
Quoted 36 lines
drivers/iommu/amd/amd_iommu_types.h +1
-0+/* Constants for PPR Log handling */ #define PPRLOG_ENTRY_SIZE 0x10#define PPRLOG_SIZE_SHIFT 56drivers/iommu/amd/iommu.c +5
-5@@ -854,7 +854,7 @@ static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *evstatic void amd_iommu_report_page_fault (struct amd_iommu *iommu,u16 devid,u16 domain_id, -u64 address,int flags) +u64 address,int flags,u8 vflags) {struct iommu_dev_data *dev_data =NULL ;struct pci_dev *pdev;@@ -889,13 +889,13 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu, }if (__ratelimit (&dev_data->rs)) { -pci_err (pdev,"Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n" , - domain_id, address, flags); +pci_err (pdev,"Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , + domain_id, address, flags, vflags); } }else { -pr_err_ratelimited ("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n" , +pr_err_ratelimited ("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - domain_id, address, flags); + domain_id, address, flags, vflags); } out:
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 935
Quoted 5 lines
drivers/iommu/amd/iommu.c +1
-1@@ -932,29 +932,42 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) }if (type ==EVENT_TYPE_IO_FAULT ) { -amd_iommu_report_page_fault (iommu, devid, pasid, address, flags); +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]);
Move this assignment to up where we extract all other fields from event so that we don't need to repeat multiple times?
Quoted 46 lines
drivers/iommu/amd/iommu.c +20
-7+ +amd_iommu_report_page_fault (iommu, devid, pasid, address, flags, vflags);return ; }switch (type) {case EVENT_TYPE_ILL_DEV : -dev_err (dev,"Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [ILLEGAL_DEV_TABLE_ENTRY deice=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - pasid, address, flags); + pasid, address, flags, vflags);dev_err (dev,"Control Reg : 0x%llx\n" , ctrl);dump_dte_entry (iommu, devid);break ; + }case EVENT_TYPE_DEV_TAB_ERR : -dev_err (dev,"Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x " -"address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x address=%#llx flags=%#04x vlfags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - address, flags); + address, flags, vflags);break ; + }case EVENT_TYPE_PAGE_TAB_ERR : -dev_err (dev,"Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - pasid, address, flags); + pasid, address, flags, vflags);break ; + }case EVENT_TYPE_ILL_CMD :dev_err (dev,"Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n" , address);dump_command (address);
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 1004
Quoted 6 lines
drivers/iommu/amd/iommu.c +3
-0@@ -986,6 +999,25 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), pasid, address, flags, tag);break ; +case EVENT_TYPE_GUEST_EVENT_FAULT : + { +u8 gid = event[1 ] &0xFFFF ;
As discussed offline, s/u8/u16/. Also may be add a macro to get the gid from event?
-Vasant
Suthikulpanit, Suravee
reviewerRe: [PATCH 02/24] iommu/amd: Introduce vIOMMU-specific events and event
Quoted 17 lines · Vasant Hegde
Hi Suravee,
Quoted 13 lines · Suravee Suthikulpanit
Adding support for new vIOMMU events:
* Guest Event Fault event
* vIOMMU Hardware Error eventAlso, adding support for the additional vIOMMU related flags in existing events.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 7 ++++ drivers/iommu/amd/iommu.c | 58 ++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 13 deletions(-)
Quoted 11 lines
drivers/iommu/amd/amd_iommu_types.h line 152
Quoted 9 lines
drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -149,6 +149,9 @@#define EVENT_TYPE_IOTLB_INV_TO 0x7#define EVENT_TYPE_INV_DEV_REQ 0x8#define EVENT_TYPE_INV_PPR_REQ 0x9 +#define EVENT_TYPE_GUEST_EVENT_FAULT 0xbBetter
EVENT_TYPE_GUEST_FAULT?
I want to keep this consistent with the IOMMU spec "GUEST_EVENT_FAULT" event.
Quoted 10 lines
Quoted 10 lines
drivers/iommu/amd/amd_iommu_types.h +2
-0+#define EVENT_TYPE_VIOMMU_HW_ERR 0xc +#define EVENT_TYPE_RMP_FAULT 0xd#define EVENT_TYPE_RMP_HW_ERR 0xe#define EVENT_DEVID_MASK 0xffff@@ -261,6 +264,10 @@#define EVTLOG_SIZE_MAX SZ_512K/* 32K entries */ #define EVTLOG_LEN_MASK_MAX (0xFULL << EVTLOG_SIZE_SHIFT)
Quoted 5 lines
drivers/iommu/amd/amd_iommu_types.h line 269
Quoted 3 lines
drivers/iommu/amd/amd_iommu_types.h +3
-0+/* Constants for IO_PAGE_FAULT event */ +#define IO_PAGE_FAULT_VFLAGS_SHIFT 27 +#define IO_PAGE_FAULT_VFLAGS_MASK GENMASK_ULL(27, 23)You mean (31, 27) ?
I'll fix the mask and remove the shift since it's no longer needed.
Quoted 39 lines
Quoted 39 lines
drivers/iommu/amd/amd_iommu_types.h +1
-0+/* Constants for PPR Log handling */ #define PPRLOG_ENTRY_SIZE 0x10#define PPRLOG_SIZE_SHIFT 56drivers/iommu/amd/iommu.c +5
-5@@ -854,7 +854,7 @@ static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *evstatic void amd_iommu_report_page_fault (struct amd_iommu *iommu,u16 devid,u16 domain_id, -u64 address,int flags) +u64 address,int flags,u8 vflags) {struct iommu_dev_data *dev_data =NULL ;struct pci_dev *pdev;@@ -889,13 +889,13 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu, }if (__ratelimit (&dev_data->rs)) { -pci_err (pdev,"Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n" , - domain_id, address, flags); +pci_err (pdev,"Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , + domain_id, address, flags, vflags); } }else { -pr_err_ratelimited ("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n" , +pr_err_ratelimited ("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - domain_id, address, flags); + domain_id, address, flags, vflags); } out:@@ -932,29 +932,42 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) }
Quoted 6 lines
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 935
Quoted 3 lines
drivers/iommu/amd/iommu.c +1
-1if (type ==EVENT_TYPE_IO_FAULT ) { -amd_iommu_report_page_fault (iommu, devid, pasid, address, flags); +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]);Move this assignment to up where we extract all other fields from event so that we don't need to repeat multiple times?
Since the vflags is only available in certain commands only. I'd rather keep them separate for better code readability.
Quoted 46 lines
Quoted 46 lines
drivers/iommu/amd/iommu.c +20
-7+ +amd_iommu_report_page_fault (iommu, devid, pasid, address, flags, vflags);return ; }switch (type) {case EVENT_TYPE_ILL_DEV : -dev_err (dev,"Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [ILLEGAL_DEV_TABLE_ENTRY deice=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - pasid, address, flags); + pasid, address, flags, vflags);dev_err (dev,"Control Reg : 0x%llx\n" , ctrl);dump_dte_entry (iommu, devid);break ; + }case EVENT_TYPE_DEV_TAB_ERR : -dev_err (dev,"Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x " -"address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x address=%#llx flags=%#04x vlfags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - address, flags); + address, flags, vflags);break ; + }case EVENT_TYPE_PAGE_TAB_ERR : -dev_err (dev,"Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - pasid, address, flags); + pasid, address, flags, vflags);break ; + }case EVENT_TYPE_ILL_CMD :dev_err (dev,"Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n" , address);dump_command (address);@@ -986,6 +999,25 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)
Quoted 8 lines
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 1004
Quoted 6 lines
drivers/iommu/amd/iommu.c +3
-0iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), pasid, address, flags, tag);break ; +case EVENT_TYPE_GUEST_EVENT_FAULT : + { +u8 gid = event[1 ] &0xFFFF ;As discussed offline, s/u8/u16/. Also may be add a macro to get the gid from event?
Note. Fixed in v5.
Thanks
Quoted 1 line
-Vasant
Re: [PATCH 02/24] iommu/amd: Introduce vIOMMU-specific events and event
In reply to Suravee SuthikulpanitQuoted 79 lines · Suravee Suthikulpanit
Adding support for new vIOMMU events:
* Guest Event Fault event
* vIOMMU Hardware Error eventAlso, adding support for the additional vIOMMU related flags in existing events.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 7 ++++ drivers/iommu/amd/iommu.c | 58 ++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 13 deletions(-)drivers/iommu/amd/amd_iommu_types.h +7
-0@@ -149,6 +149,9 @@#define EVENT_TYPE_IOTLB_INV_TO 0x7#define EVENT_TYPE_INV_DEV_REQ 0x8#define EVENT_TYPE_INV_PPR_REQ 0x9 +#define EVENT_TYPE_GUEST_EVENT_FAULT 0xb +#define EVENT_TYPE_VIOMMU_HW_ERR 0xc +#define EVENT_TYPE_RMP_FAULT 0xd#define EVENT_TYPE_RMP_HW_ERR 0xe#define EVENT_DEVID_MASK 0xffff@@ -261,6 +264,10 @@#define EVTLOG_SIZE_MAX SZ_512K/* 32K entries */ #define EVTLOG_LEN_MASK_MAX (0xFULL << EVTLOG_SIZE_SHIFT) +/* Constants for IO_PAGE_FAULT event */ +#define IO_PAGE_FAULT_VFLAGS_SHIFT 27 +#define IO_PAGE_FAULT_VFLAGS_MASK GENMASK_ULL(27, 23) +/* Constants for PPR Log handling */ #define PPRLOG_ENTRY_SIZE 0x10#define PPRLOG_SIZE_SHIFT 56drivers/iommu/amd/iommu.c +8
-6@@ -854,7 +854,7 @@ static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *evstatic void amd_iommu_report_page_fault (struct amd_iommu *iommu,u16 devid,u16 domain_id, -u64 address,int flags) +u64 address,int flags,u8 vflags) {struct iommu_dev_data *dev_data =NULL ;struct pci_dev *pdev;@@ -889,13 +889,13 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu, }if (__ratelimit (&dev_data->rs)) { -pci_err (pdev,"Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n" , - domain_id, address, flags); +pci_err (pdev,"Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , + domain_id, address, flags, vflags); } }else { -pr_err_ratelimited ("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n" , +pr_err_ratelimited ("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - domain_id, address, flags); + domain_id, address, flags, vflags); } out:@@ -932,29 +932,42 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) }if (type ==EVENT_TYPE_IO_FAULT ) { -amd_iommu_report_page_fault (iommu, devid, pasid, address, flags); +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +amd_iommu_report_page_fault (iommu, devid, pasid, address, flags, vflags);return ; }
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 943
Quoted 10 lines
drivers/iommu/amd/iommu.c +4
-2switch (type) {case EVENT_TYPE_ILL_DEV : -dev_err (dev,"Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + + dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY deice=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x vflags=%#x]\n", iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), - pasid, address, flags);
A minor typos in the log strings: "deice" -> "device"
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 958
Quoted 13 lines
drivers/iommu/amd/iommu.c +6
-2+ pasid, address, flags, vflags);dev_err (dev,"Control Reg : 0x%llx\n" , ctrl);dump_dte_entry (iommu, devid);break ; + }case EVENT_TYPE_DEV_TAB_ERR : - dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x " - "address=0x%llx flags=0x%04x]\n", + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + + dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x address=%#llx flags=%#04x vlfags=%#x]\n", iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid),
typos : "vlfags" -> "vflags".
Quoted 19 lines
drivers/iommu/amd/iommu.c +8
-3- address, flags); + address, flags, vflags);break ; + }case EVENT_TYPE_PAGE_TAB_ERR : -dev_err (dev,"Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n" , + { +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x vflags=%#x]\n" , iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), - pasid, address, flags); + pasid, address, flags, vflags);break ; + }case EVENT_TYPE_ILL_CMD :dev_err (dev,"Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n" , address);dump_command (address);
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 1005
Quoted 7 lines
drivers/iommu/amd/iommu.c +4
-0@@ -986,6 +999,25 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) iommu->pci_seg->id,PCI_BUS_NUM (devid),PCI_SLOT (devid),PCI_FUNC (devid), pasid, address, flags, tag);break ; +case EVENT_TYPE_GUEST_EVENT_FAULT : + { +u8 gid = event[1 ] &0xFFFF ; +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]);
The GuestID is declared as u8 but assigned a 16-bit value masked with 0xFFFF. This truncates the upper 8 bits. Per the AMD IOMMU Specification (Rev 3.10, Section 2.5.12, Table 68), the GuestID field is 16 bits wide [15:0]. This should be:
u16 gid = event[1] & 0xFFFF;
Note that the VIOMMU_HARDWARE_ERROR handler below correctly uses u16 for the same field, so this looks like an oversight.
Additionally, per Section 2.5.12, a GUEST_EVENT_FAULT occupies two log entries (paired event entry). The current iommu_poll_events() advances by a single entry per iteration, so the second (paired) entry will be misinterpreted as an unknown event type on the next iteration. The handler should consume/skip the second entry.
Quoted 18 lines
drivers/iommu/amd/iommu.c +15
-0+ +dev_err (dev,"Event logged [GUEST_EVENT_FAULT gid=#%x flags=0x%04x vflags=%#x]\n" , + gid, flags, vflags); +break ; + } +case EVENT_TYPE_VIOMMU_HW_ERR : + { +u16 gid = event[0 ] &0xFFFF ; +u8 src = (event[0 ] >>16 ) &0x3 ; +u8 vflags =FIELD_GET (IO_PAGE_FAULT_VFLAGS_MASK , event[0 ]); + +dev_err (dev,"Event logged [VIOMMU_HW_ERR gid=%#x address=%#llx src=%#x flags=%#x vflags=%#x]\n" , + gid, address, src, flags, vflags); +break ; + }default :dev_err (dev,"Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n" , event[0 ], event[1 ], event[2 ], event[3 ]);
Suthikulpanit, Suravee
reviewerRe: [PATCH 02/24] iommu/amd: Introduce vIOMMU-specific events and event
Quoted 9 lines · On 8/19/2026 3:17 PM, [email protected] wrote:
Quoted 1 line · Suravee Suthikulpanit
.....
Additionally, per Section 2.5.12, a
GUEST_EVENT_FAULToccupies two log entries (paired event entry). The currentiommu_poll_events()advances by a single entry per iteration, so the second (paired) entry will be misinterpreted as an unknown event type on the next iteration. The handler should consume/skip the second entry.
For GUEST_EVENT_FAULT, which consumes 2 entries, my expectation is the driver reports 2 events in dmesg, which are interpreted together.
1. GUEST_EVENT_FAULT: handled by the case EVENT_TYPE_GUEST_EVENT_FAULT.
2. The injected event: handled by the corresponding type. (not the unknown type)
However, I can modify this by letting the GUEST_EVENT_FAULT event handler consume two Event log entries and report raw value for the injected guest event (i.e. the second entry).
I'll send this out in v5.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 3/24 iommu/amd: Detect and initialize AMD vIOMMU feature
In reply to Suravee SuthikulpanitThe feature is advertised w/ EFR[VIOMMUSup]. Please see the AMD IOMMU specification[1] for more detail.
Introduce a new global variable amd_iommu_viommu, which is used to control the feature enablement in the driver. Currently, the feature is default to disabled. Once the feature is fully supported, it will be changed to enabled by default.
[1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB
--- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/amd_viommu.h | 22 ++++++++++++++++++++++ drivers/iommu/amd/init.c | 15 +++++++++++++++ drivers/iommu/amd/viommu.c | 29 +++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/amd/amd_viommu.h create mode 100644 drivers/iommu/amd/viommu.c
drivers/iommu/amd/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += iommu.o init.o quirks.o ppr.o pasid.o -obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o +obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu.h +2 -0
@@ -35,6 +35,8 @@ void amd_iommu_debugfs_setup(void);static inline void amd_iommu_debugfs_setup (void ) {}#endif +extern bool amd_iommu_viommu; +/* Needed for interrupt remapping */ int amd_iommu_prepare (void );int amd_iommu_enable (void );
drivers/iommu/amd/amd_iommu_types.h +1 -0
@@ -103,6 +103,7 @@#define FEATURE_HASUP BIT_ULL(49)#define FEATURE_EPHSUP BIT_ULL(50)#define FEATURE_HDSUP BIT_ULL(52) +#define FEATURE_VIOMMU BIT_ULL(55)#define FEATURE_SNP BIT_ULL(63)
drivers/iommu/amd/amd_viommu.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2026 Advanced Micro Devices, Inc. +*/ + +#ifndef AMD_VIOMMU_H +#define AMD_VIOMMU_H + +#if IS_ENABLED (CONFIG_AMD_IOMMU_IOMMUFD ) + +int amd_viommu_init (struct amd_iommu *iommu); + +#else + +static inline int amd_viommu_init (struct amd_iommu *iommu) +{ +return -EOPNOTSUPP ; +} + +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ + +#endif /* AMD_VIOMMU_H */
drivers/iommu/amd/init.c +15 -0
@@ -34,6 +34,7 @@#include <linux/crash_dump.h> #include "amd_iommu.h" +#include "amd_viommu.h" #include "../irq_remapping.h" #include "../iommu-pages.h"
@@ -196,6 +197,9 @@ bool amdr_ivrs_remap_support __read_mostly;bool amd_iommu_force_isolation__read_mostly ; +/* VIOMMU enabling flag */ +bool amd_iommu_viommu; +unsigned long amd_iommu_pgsize_bitmap__ro_after_init =AMD_IOMMU_PGSIZES ;enum iommu_init_state {
@@ -2188,6 +2192,12 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)if (check_feature (FEATURE_PPR ) &&amd_iommu_alloc_ppr_log (iommu))return -ENOMEM ; + ret =amd_viommu_init (iommu); +if (ret) { +pr_err ("Failed to initialize vIOMMU.\n" ); + amd_iommu_viommu = false; + } +if (iommu->cap & (1UL <<IOMMU_CAP_NPCACHE )) {pr_info ("Using strict mode due to virtualization\n" );iommu_set_dma_strict ();
@@ -2281,6 +2291,9 @@ static void print_iommu_info(void)if (check_feature2 (FEATURE_SEVSNPIO_SUP ))pr_cont (" SEV-TIO" ); +if (check_feature (FEATURE_VIOMMU )) +pr_cont (" vIOMMU" ); +pr_cont ("\n" ); }
@@ -2293,6 +2306,8 @@ static void print_iommu_info(void)pr_info ("V2 page table enabled (Paging mode : %d level)\n" , amd_iommu_gpt_level); } +if (amd_iommu_viommu) +pr_info ("AMD-Vi: vIOMMU enabled\n" ); }static int __init amd_iommu_init_pci (void )
drivers/iommu/amd/viommu.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2023 Advanced Micro Devices, Inc. +*/ + +#define pr_fmt (fmt) "AMD-Vi: " fmt +#define dev_fmt (fmt) pr_fmt(fmt) + +#include <linux/iommu.h> +#include <linux/iommufd.h> +#include <linux/amd-iommu.h> +#include <uapi/linux/iommufd.h> + +#include <asm/iommu.h> +#include <asm/set_memory.h> + +#include "iommufd.h" +#include "amd_iommu.h" +#include "amd_iommu_types.h" +#include "amd_viommu.h" + +int __init amd_viommu_init (struct amd_iommu *iommu) +{ +if (!amd_iommu_viommu || + !check_feature (FEATURE_VIOMMU )) +return 0 ; + +return 0 ; +}
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 03/24] iommu/amd: Detect and initialize AMD vIOMMU feature
Hi Suravee,
drivers/iommu/amd/Makefile
Quoted 23 lines · Suravee Suthikulpanit
The feature is advertised w/ EFR[VIOMMUSup]. Please see the AMD IOMMU specification[1] for more detail.
Introduce a new global variable
amd_iommu_viommu, which is used to control the feature enablement in the driver. Currently, the feature is default to disabled. Once the feature is fully supported, it will be changed to enabled by default.[1] https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/amd_viommu.h | 22 ++++++++++++++++++++++ drivers/iommu/amd/init.c | 15 +++++++++++++++ drivers/iommu/amd/viommu.c | 29 +++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/amd/amd_viommu.h create mode 100644 drivers/iommu/amd/viommu.cdrivers/iommu/amd/Makefile +0
-0
.../...
Quoted 11 lines
drivers/iommu/amd/init.c +1
-0@@ -34,6 +34,7 @@#include <linux/crash_dump.h> #include "amd_iommu.h" +#include "amd_viommu.h" #include "../irq_remapping.h" #include "../iommu-pages.h"
drivers/iommu/amd/init.c bool amdr_ivrs_remap_support __read_mostly; · line 201
Quoted 5 lines
drivers/iommu/amd/init.c +2
-0@@ -196,6 +197,9 @@ bool amdr_ivrs_remap_support __read_mostly;bool amd_iommu_force_isolation__read_mostly ; +/* VIOMMU enabling flag */ +bool amd_iommu_viommu;
may be add "__ro_after_init" ?
Quoted 28 lines
drivers/iommu/amd/init.c +10
-0+unsigned long amd_iommu_pgsize_bitmap__ro_after_init =AMD_IOMMU_PGSIZES ;enum iommu_init_state {@@ -2188,6 +2192,12 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)if (check_feature (FEATURE_PPR ) &&amd_iommu_alloc_ppr_log (iommu))return -ENOMEM ; + ret =amd_viommu_init (iommu); +if (ret) { +pr_err ("Failed to initialize vIOMMU.\n" ); + amd_iommu_viommu = false; + } +if (iommu->cap & (1UL <<IOMMU_CAP_NPCACHE )) {pr_info ("Using strict mode due to virtualization\n" );iommu_set_dma_strict ();@@ -2281,6 +2291,9 @@ static void print_iommu_info(void)if (check_feature2 (FEATURE_SEVSNPIO_SUP ))pr_cont (" SEV-TIO" ); +if (check_feature (FEATURE_VIOMMU )) +pr_cont (" vIOMMU" ); +pr_cont ("\n" ); }
drivers/iommu/amd/init.c static void print_iommu_info(void) · line 2310
Quoted 5 lines
drivers/iommu/amd/init.c +2
-0@@ -2293,6 +2306,8 @@ static void print_iommu_info(void)pr_info ("V2 page table enabled (Paging mode : %d level)\n" , amd_iommu_gpt_level); } +if (amd_iommu_viommu) +pr_info ("AMD-Vi: vIOMMU enabled\n" );
pr_fmt already prepends "AMD-Vi:". Please remove it from above pr_info.
Quoted 24 lines
drivers/iommu/amd/init.c +0
-0}static int __init amd_iommu_init_pci (void )drivers/iommu/amd/viommu.c new file +15
-0@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2023 Advanced Micro Devices, Inc. +*/ + +#define pr_fmt (fmt) "AMD-Vi: " fmt +#define dev_fmt (fmt) pr_fmt(fmt) + +#include <linux/iommu.h> +#include <linux/iommufd.h> +#include <linux/amd-iommu.h> +#include <uapi/linux/iommufd.h> + +#include <asm/iommu.h> +#include <asm/set_memory.h>
drivers/iommu/amd/viommu.c line 25
Quoted 10 lines
drivers/iommu/amd/viommu.c new file +10
-0+ +#include "iommufd.h" +#include "amd_iommu.h" +#include "amd_iommu_types.h" +#include "amd_viommu.h" + +int __init amd_viommu_init (struct amd_iommu *iommu) +{ +if (!amd_iommu_viommu || + !check_feature (FEATURE_VIOMMU ))
If check_feature returns false, then return -ENOSUPPORT so that called sets amd_iommu_viommu to false.
Also how about renaming it as "amd_iommu_viommu_present" ? Because following patches defines another structure with same name. It works, but add confusion.
-Vasant
Suthikulpanit, Suravee
reviewerRe: [PATCH 03/24] iommu/amd: Detect and initialize AMD vIOMMU feature
Hi
Quoted 30 lines · Vasant Hegde
.../...
Quoted 29 lines
}static int
__initamd_iommu_init_pci(void)drivers/iommu/amd/viommu.c new file +20
-0@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2023 Advanced Micro Devices, Inc. +*/ + +#define pr_fmt (fmt) "AMD-Vi: " fmt +#define dev_fmt (fmt) pr_fmt(fmt) + +#include <linux/iommu.h> +#include <linux/iommufd.h> +#include <linux/amd-iommu.h> +#include <uapi/linux/iommufd.h> + +#include <asm/iommu.h> +#include <asm/set_memory.h> + +#include "iommufd.h" +#include "amd_iommu.h" +#include "amd_iommu_types.h" +#include "amd_viommu.h"
Quoted 8 lines
drivers/iommu/amd/viommu.c line 25
Quoted 5 lines
drivers/iommu/amd/viommu.c new file +5
-0+ +int __init amd_viommu_init (struct amd_iommu *iommu) +{ +if (!amd_iommu_viommu || + !check_feature (FEATURE_VIOMMU ))If
check_featurereturns false, then return-ENOSUPPORTso that called setsamd_iommu_viommuto false.
Actually, that would print "Failed to initialize vIOMMU" in dmesg on older systems, which does not support the feature. We don't want that.
For v5, I will clean up this logic to make sure that the amd_iommu_viommu is false when the feature is not supporte.
Quoted 2 lines
Also how about renaming it as "
amd_iommu_viommu_present" ? Because following patches defines another structure with same name. It works, but add confusion.
I'll change this to amd_iommu_viommu_enabled in v5
Thanks,
Suravee
Quoted 2 lines
-Vasant
Suravee Suthikulpanit
reviewerPATCH v4 4/24 iommu/amd: Introduce IOMMUFD vIOMMU support for AMD
In reply to Suravee SuthikulpanitIntroduce a new enum iommu_viommu_type (IOMMU_VIOMMU_TYPE_AMD) for AMD vIOMMU along with the struct iommu_viommu_amd, which is used to initialize IOMMUFD vIOMMU instance when calling struct iommu_ops.viommu_init().
Also, hook up struct iomufd_viomu_ops.alloc_domain_nested to connect nested domain allocation with AMD vIOMMU implementation.
Additional initialization will be added in subsequent patches.
--- drivers/iommu/amd/iommufd.c | 4 ++++ include/uapi/linux/iommufd.h | 10 ++++++++++ 2 files changed, 14 insertions(+)
drivers/iommu/amd/iommufd.c +4 -0
@@ -34,6 +34,9 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *length, enum iommu_hw_info_tysize_t amd_iommufd_get_viommu_size (struct device *dev,enum iommu_viommu_type viommu_type) { +if (!amd_iommu_viommu || (viommu_type !=IOMMU_VIOMMU_TYPE_AMD )) +return 0 ; + return VIOMMU_STRUCT_SIZE(struct amd_iommu_viommu, core); }
@@ -73,5 +76,6 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)* struct iommufd_viommu_ops - vIOMMU specific operations */ static const struct iommufd_viommu_ops amd_viommu_ops = { + .alloc_domain_nested = amd_iommu_alloc_domain_nested, .destroy = amd_iommufd_viommu_destroy, };
include/uapi/linux/iommufd.h +10 -0
@@ -1052,6 +1052,7 @@ struct iommu_fault_alloc {* @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type * @IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV: NVIDIA Tegra241 CMDQV (extension for ARM * SMMUv3) enabled ARM SMMUv3 type +* @IOMMU_VIOMMU_TYPE_AMD: AMD HW-vIOMMU type */ enum iommu_viommu_type {IOMMU_VIOMMU_TYPE_DEFAULT =0 ,
@@ -1062,6 +1063,7 @@ enum iommu_viommu_type {* VMM must wire the HYP_OWN bit to 0 in guest VINTF_CONFIG register */ IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV =2 , + IOMMU_VIOMMU_TYPE_AMD = 3, };/**
@@ -1080,6 +1082,14 @@ struct iommu_viommu_tegra241_cmdqv {__aligned_u64 out_vintf_mmap_length; }; +/** +* struct iommu_viommu_amd - AMD vIOMMU Interface (IOMMU_VIOMMU_TYPE_AMD) +* @out_vfmmio_mmap_offset: (out) mmap offset for vIOMMU VF-MMIO +*/ +struct iommu_viommu_amd { +__aligned_u64 out_vfmmio_mmap_offset; +}; +/** * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC) * @size: sizeof(struct iommu_viommu_alloc)
Signature · 1 line
2.34.1
Re: [PATCH 04/24] iommu/amd: Introduce IOMMUFD vIOMMU support for AMD
Quoted 8 lines · Suravee Suthikulpanit
Introduce a new enum
iommu_viommu_type(IOMMU_VIOMMU_TYPE_AMD) for AMD vIOMMU along with the structiommu_viommu_amd, which is used to initialize IOMMUFD vIOMMU instance when calling structiommu_ops.viommu_init().Also, hook up struct iomufd_viomu_ops.alloc_domain_nested to connect nested domain allocation with AMD vIOMMU implementation.
[...]
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 5/24 iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances
In reply to Suravee SuthikulpanitHardware vIOMMU uses a 16-bit Guest ID (GID) per guest IOMMU to index driver and hardware state. Allocate one GID per IOMMUFD vIOMMU from a per-amd_iommu IDA (unique within that IOMMU; a VM behind multiple IOMMUs may hold more than one GID).
Add amd_iommu_gid_alloc() and amd_iommu_gid_free(), store the ID in amd_iommu_viommu::gid, and call them from amd_iommufd_viommu_init() and destroy after copying struct iommu_viommu_amd to userspace.
ida_init() for gid_ida is done when vIOMMU MMIO is mapped (next patch).
--- drivers/iommu/amd/amd_iommu.h | 4 ++++ drivers/iommu/amd/amd_iommu_types.h | 8 ++++++++ drivers/iommu/amd/iommu.c | 19 +++++++++++++++++ drivers/iommu/amd/iommufd.c | 32 +++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+)
drivers/iommu/amd/amd_iommu.h +4 -0
@@ -228,4 +228,8 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entrystruct iommu_domain *amd_iommu_alloc_domain_nested (struct iommufd_viommu *viommu,u32 flags,const struct iommu_user_data *user_data); + +/* Guest ID for vIOMMU */ +int amd_iommu_gid_alloc (struct amd_iommu *iommu); +void amd_iommu_gid_free (struct amd_iommu *iommu,int gid);#endif /* AMD_IOMMU_H */
drivers/iommu/amd/amd_iommu_types.h +8 -0
@@ -21,6 +21,7 @@#include <linux/iommufd.h> #include <linux/irqreturn.h> #include <linux/generic_pt/iommu.h> +#include <linux/idr.h> #include <uapi/linux/iommufd.h>
@@ -413,6 +414,9 @@#define MAX_DOMAIN_ID 65536 +/* For vIOMMU, the GID is 16-bit. */ +#define VIOMMU_MAX_GID 0xFFFF +/* Timeout stuff */ #define LOOP_TIMEOUT 100000#define MMIO_STATUS_TIMEOUT 2000000
@@ -509,6 +513,7 @@ struct amd_iommu_viommu {struct iommufd_viommu core;struct protection_domain *parent;/* nest parent domain for this viommu */ struct list_head pdom_list;/* For protection_domain->viommu_list */ +u16 gid;/* Guest ID for the vIOMMU */ /* * Per-vIOMMU guest domain ID to host domain ID mapping.
@@ -768,6 +773,9 @@ struct amd_iommu {/* IOPF support */ struct iopf_queue *iopf_queue;unsigned char iopfq_name[32 ]; + +struct ida gid_ida;/* guest IDs for this IOMMU */ +bool gid_ida_inited; };static inline struct amd_iommu *dev_to_amd_iommu (struct device *dev)
drivers/iommu/amd/iommu.c +19 -0
@@ -252,6 +252,25 @@ static inline bool pdom_is_sva_capable(struct protection_domain *pdom)return pdom_is_v2_pgtbl_mode (pdom) ||pdom_is_in_pt_mode (pdom); } +int amd_iommu_gid_alloc (struct amd_iommu *iommu) +{ +int ret =ida_alloc_range (&iommu->gid_ida,1 ,VIOMMU_MAX_GID ,GFP_KERNEL ); + +if (ret <0 ) +pr_err ("%s: Failed to allocate guest ID (devid=%#x)\n" , + __func__, iommu->devid); +else +pr_debug ("%s: iommu devid=%#x, gid=%u\n" , __func__, iommu->devid, ret); + +return ret; +} + +void amd_iommu_gid_free (struct amd_iommu *iommu,int gid) +{ +pr_debug ("%s: iommu devid=%#x, gid=%u\n" , __func__, iommu->devid, gid); +ida_free (&iommu->gid_ida, gid); +} +static inline int get_acpihid_device_id (struct device *dev,struct acpihid_map_entry **entry) {
drivers/iommu/amd/iommufd.c +32 -0
@@ -43,13 +43,37 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viint amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent,const struct iommu_user_data *user_data) { +int ret;unsigned long flags; +struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 ); aviommu->parent = pdom; +if (!user_data) +return -EINVAL ; + + ret =iommu_copy_struct_from_user (&data, user_data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) +return ret; + + ret =amd_iommu_gid_alloc (iommu); +if (ret <0 ) + goto err_gid; + aviommu->gid = ret; +pr_debug ("%s: gid=%#x" , __func__, aviommu->gid); + + ret =iommu_copy_struct_to_user (user_data, &data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) + goto err_init; + viommu->ops = &amd_viommu_ops;spin_lock_irqsave (&pdom->lock, flags);
@@ -57,6 +81,10 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *spin_unlock_irqrestore (&pdom->lock, flags);return 0 ; +err_init: +amd_iommu_gid_free (iommu, aviommu->gid); +err_gid: +return ret; }static void amd_iommufd_viommu_destroy (struct iommufd_viommu *viommu)
@@ -64,11 +92,15 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)unsigned long flags; struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);struct protection_domain *pdom = aviommu->parent; + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu); + +pr_debug ("%s: gid=%#x, iommu devid=%#x\n" , __func__, aviommu->gid, iommu->devid);spin_lock_irqsave (&pdom->lock, flags);list_del (&aviommu->pdom_list);spin_unlock_irqrestore (&pdom->lock, flags);xa_destroy (&aviommu->gdomid_array); +amd_iommu_gid_free (iommu, aviommu->gid); }/*
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 05/24] iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances
Suravee,
Quoted 63 lines · Suravee Suthikulpanit
Hardware vIOMMU uses a 16-bit Guest ID (GID) per guest IOMMU to index driver and hardware state. Allocate one GID per IOMMUFD vIOMMU from a per-amd_iommu IDA (unique within that IOMMU; a VM behind multiple IOMMUs may hold more than one GID).
Add
amd_iommu_gid_alloc()andamd_iommu_gid_free(), store the ID inamd_iommu_viommu::gid, and call them fromamd_iommufd_viommu_init()and destroy after copying structiommu_viommu_amdto userspace.
ida_init()forgid_idais done when vIOMMU MMIO is mapped (next patch).Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu.h | 4 ++++ drivers/iommu/amd/amd_iommu_types.h | 8 ++++++++ drivers/iommu/amd/iommu.c | 19 +++++++++++++++++ drivers/iommu/amd/iommufd.c | 32 +++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+)drivers/iommu/amd/amd_iommu.h +4
-0@@ -228,4 +228,8 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entrystruct iommu_domain *amd_iommu_alloc_domain_nested (struct iommufd_viommu *viommu,u32 flags,const struct iommu_user_data *user_data); + +/* Guest ID for vIOMMU */ +int amd_iommu_gid_alloc (struct amd_iommu *iommu); +void amd_iommu_gid_free (struct amd_iommu *iommu,int gid);#endif /* AMD_IOMMU_H */ drivers/iommu/amd/amd_iommu_types.h +5
-0@@ -21,6 +21,7 @@#include <linux/iommufd.h> #include <linux/irqreturn.h> #include <linux/generic_pt/iommu.h> +#include <linux/idr.h> #include <uapi/linux/iommufd.h> @@ -413,6 +414,9 @@#define MAX_DOMAIN_ID 65536 +/* For vIOMMU, the GID is 16-bit. */ +#define VIOMMU_MAX_GID 0xFFFF +/* Timeout stuff */ #define LOOP_TIMEOUT 100000#define MMIO_STATUS_TIMEOUT 2000000@@ -509,6 +513,7 @@ struct amd_iommu_viommu {struct iommufd_viommu core;struct protection_domain *parent;/* nest parent domain for this viommu */ struct list_head pdom_list;/* For protection_domain->viommu_list */ +u16 gid;/* Guest ID for the vIOMMU */ /* * Per-vIOMMU guest domain ID to host domain ID mapping.
drivers/iommu/amd/amd_iommu_types.h struct amd_iommu { · line 778
Quoted 6 lines
drivers/iommu/amd/amd_iommu_types.h +3
-0@@ -768,6 +773,9 @@ struct amd_iommu {/* IOPF support */ struct iopf_queue *iopf_queue;unsigned char iopfq_name[32 ]; + +struct ida gid_ida;/* guest IDs for this IOMMU */ +bool gid_ida_inited;
Redundant variable? May be remove gid_ida_inited.
Quoted 8 lines
drivers/iommu/amd/amd_iommu_types.h +0
-0};static inline struct amd_iommu *dev_to_amd_iommu (struct device *dev)drivers/iommu/amd/iommu.c +0
-0
drivers/iommu/amd/iommu.c static inline bool pdom_is_sva_capable(struct protection_domain *pdom) · line 257
Quoted 6 lines
drivers/iommu/amd/iommu.c +3
-0@@ -252,6 +252,25 @@ static inline bool pdom_is_sva_capable(struct protection_domain *pdom)return pdom_is_v2_pgtbl_mode (pdom) ||pdom_is_in_pt_mode (pdom); } +int amd_iommu_gid_alloc (struct amd_iommu *iommu) +{ +int ret =ida_alloc_range (&iommu->gid_ida,1 ,VIOMMU_MAX_GID ,GFP_KERNEL );
We know that top half is reserved for secure vIOMMU and we have to construct that differently. May be restrict max GID to 0x7FFF ?
Quoted 35 lines
drivers/iommu/amd/iommu.c +16
-0+ +if (ret <0 ) +pr_err ("%s: Failed to allocate guest ID (devid=%#x)\n" , + __func__, iommu->devid); +else +pr_debug ("%s: iommu devid=%#x, gid=%u\n" , __func__, iommu->devid, ret); + +return ret; +} + +void amd_iommu_gid_free (struct amd_iommu *iommu,int gid) +{ +pr_debug ("%s: iommu devid=%#x, gid=%u\n" , __func__, iommu->devid, gid); +ida_free (&iommu->gid_ida, gid); +} +static inline int get_acpihid_device_id (struct device *dev,struct acpihid_map_entry **entry) {drivers/iommu/amd/iommufd.c +3
-0@@ -43,13 +43,37 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viint amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent,const struct iommu_user_data *user_data) { +int ret;unsigned long flags; +struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 );
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 63
Quoted 10 lines
drivers/iommu/amd/iommufd.c +8
-0aviommu->parent = pdom; +if (!user_data) +return -EINVAL ; + + ret =iommu_copy_struct_from_user (&data, user_data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) +return ret;
May be move this before "xa_init_flags" ?
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 65
Quoted 2 lines
drivers/iommu/amd/iommufd.c +2
-0+ + ret =amd_iommu_gid_alloc (iommu);
Looks like you missed initializing "iommu->gid_ida" ?
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 69
Quoted 4 lines
drivers/iommu/amd/iommufd.c +4
-0+if (ret <0 ) + goto err_gid; + aviommu->gid = ret; +pr_debug ("%s: gid=%#x" , __func__, aviommu->gid);
Redundat as you already have pr_debug inside amd_iommu_gid_alloc()?
-Vasant
Suthikulpanit, Suravee
reviewerRe: [PATCH 05/24] iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances
Quoted 32 lines · Vasant Hegde
Suravee,
Quoted 29 lines · Suravee Suthikulpanit
.....
drivers/iommu/amd/amd_iommu_types.h +5
-0@@ -21,6 +21,7 @@#include <linux/iommufd.h> #include <linux/irqreturn.h> #include <linux/generic_pt/iommu.h> +#include <linux/idr.h> #include <uapi/linux/iommufd.h> @@ -413,6 +414,9 @@#define MAX_DOMAIN_ID 65536 +/* For vIOMMU, the GID is 16-bit. */ +#define VIOMMU_MAX_GID 0xFFFF +/* Timeout stuff */ #define LOOP_TIMEOUT 100000#define MMIO_STATUS_TIMEOUT 2000000@@ -509,6 +513,7 @@ struct amd_iommu_viommu {struct iommufd_viommu core;struct protection_domain *parent;/* nest parent domain for this viommu */ struct list_head pdom_list;/* For protection_domain->viommu_list */ +u16 gid;/* Guest ID for the vIOMMU */
Quoted 11 lines
drivers/iommu/amd/amd_iommu_types.h struct amd_iommu { · line 778
Quoted 9 lines
drivers/iommu/amd/amd_iommu_types.h +3
-0/* * Per-vIOMMU guest domain ID to host domain ID mapping. @@ -768,6 +773,9 @@ struct amd_iommu {/* IOPF support */ struct iopf_queue *iopf_queue;unsigned char iopfq_name[32 ]; + +struct ida gid_ida;/* guest IDs for this IOMMU */ +bool gid_ida_inited;Redundant variable? May be remove
gid_ida_inited.
Ok
Quoted 12 lines
Quoted 12 lines
drivers/iommu/amd/amd_iommu_types.h +0
-0};static inline struct amd_iommu *dev_to_amd_iommu (struct device *dev)drivers/iommu/amd/iommu.c +0
-0@@ -252,6 +252,25 @@ static inline bool pdom_is_sva_capable(struct protection_domain *pdom)return pdom_is_v2_pgtbl_mode (pdom) ||pdom_is_in_pt_mode (pdom); }
Quoted 7 lines
drivers/iommu/amd/iommu.c static inline bool pdom_is_sva_capable(struct protection_domain *pdom) · line 257
Quoted 3 lines
drivers/iommu/amd/iommu.c +3
-0+int amd_iommu_gid_alloc (struct amd_iommu *iommu) +{ +int ret =ida_alloc_range (&iommu->gid_ida,1 ,VIOMMU_MAX_GID ,GFP_KERNEL );We know that top half is reserved for secure vIOMMU and we have to construct that differently. May be restrict max GID to
0x7FFF?
Ok
Quoted 19 lines
Quoted 19 lines
.......
drivers/iommu/amd/iommufd.c +3
-0@@ -43,13 +43,37 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viint amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent,const struct iommu_user_data *user_data) { +int ret;unsigned long flags; +struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 ); aviommu->parent = pdom;
Quoted 11 lines
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 63
Quoted 8 lines
drivers/iommu/amd/iommufd.c +8
-0+if (!user_data) +return -EINVAL ; + + ret =iommu_copy_struct_from_user (&data, user_data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) +return ret;May be move this before "
xa_init_flags" ?
Ok
Quoted 5 lines
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 65
Quoted 2 lines
drivers/iommu/amd/iommufd.c +2
-0+ + ret =amd_iommu_gid_alloc (iommu);Looks like you missed initializing "
iommu->gid_ida" ?
It's being initialized at host IOMMU bring-up, from amd_viommu_init() in init.c (early_amd_iommu / per-IOMMU setup).
Quoted 9 lines
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 69
Quoted 4 lines
drivers/iommu/amd/iommufd.c +4
-0+if (ret <0 ) + goto err_gid; + aviommu->gid = ret; +pr_debug ("%s: gid=%#x" , __func__, aviommu->gid);Redundat as you already have
pr_debuginsideamd_iommu_gid_alloc()?-Vasant
Ok.
I'll update these in V5.
Thanks,
Suravee
Re: [PATCH 05/24] iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances
In reply to Suravee Suthikulpanitdrivers/iommu/amd/iommu.c static inline bool pdom_is_sva_capable(struct protection_domain *pdom) · line 261
Quoted 8 lines
[ ... 73 lines skipped ... ]
drivers/iommu/amd/iommu.c +7
-0+int amd_iommu_gid_alloc (struct amd_iommu *iommu) +{ +int ret =ida_alloc_range (&iommu->gid_ida,1 ,VIOMMU_MAX_GID ,GFP_KERNEL ); + +if (ret <0 ) +pr_err ("%s: Failed to allocate guest ID (devid=%#x)\n" , + __func__, iommu->devid);
Don't print on user triggerable failure paths
Quoted 25 lines
[ ... 19 lines skipped ... ]
drivers/iommu/amd/iommufd.c +13
-0@@ -43,13 +43,37 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viint amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent,const struct iommu_user_data *user_data) { +int ret;unsigned long flags; +struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 ); aviommu->parent = pdom; +if (!user_data) +return -EINVAL ; + + ret =iommu_copy_struct_from_user (&data, user_data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) +return ret; + + ret =amd_iommu_gid_alloc (iommu);
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 75
Quoted 10 lines
drivers/iommu/amd/iommufd.c +10
-0+if (ret <0 ) + goto err_gid; + aviommu->gid = ret; +pr_debug ("%s: gid=%#x" , __func__, aviommu->gid); + + ret =iommu_copy_struct_to_user (user_data, &data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) + goto err_init;
These hunks doing the copy struct are probably in the wrong patch since nothing touches out_vfmmio_mmap_offset.
Otherwise looks OK
Signature · 1 line
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH 05/24] iommu/amd: Allocate Guest IDs for IOMMUFD vIOMMU instances
Quoted 10 lines · Jason Gunthorpe
drivers/iommu/amd/iommu.c static inline bool pdom_is_sva_capable(struct protection_domain *pdom) · line 261
Quoted 8 lines
[ ... 73 lines skipped ... ]
drivers/iommu/amd/iommu.c +7
-0+int amd_iommu_gid_alloc (struct amd_iommu *iommu) +{ +int ret =ida_alloc_range (&iommu->gid_ida,1 ,VIOMMU_MAX_GID ,GFP_KERNEL ); + +if (ret <0 ) +pr_err ("%s: Failed to allocate guest ID (devid=%#x)\n" , + __func__, iommu->devid);Don't print on user triggerable failure paths
Ok.
Quoted 31 lines
Quoted 31 lines
[ ... 19 lines skipped ... ]
drivers/iommu/amd/iommufd.c +18
-0@@ -43,13 +43,37 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viint amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent,const struct iommu_user_data *user_data) { +int ret;unsigned long flags; +struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 ); aviommu->parent = pdom; +if (!user_data) +return -EINVAL ; + + ret =iommu_copy_struct_from_user (&data, user_data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) +return ret; + + ret =amd_iommu_gid_alloc (iommu); +if (ret <0 ) + goto err_gid; + aviommu->gid = ret; +pr_debug ("%s: gid=%#x" , __func__, aviommu->gid); +
Quoted 8 lines
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 75
Quoted 5 lines
drivers/iommu/amd/iommufd.c +5
-0+ ret =iommu_copy_struct_to_user (user_data, &data, +IOMMU_VIOMMU_TYPE_AMD , + out_vfmmio_mmap_offset); +if (ret) + goto err_init;These hunks doing the copy struct are probably in the wrong patch since nothing touches
out_vfmmio_mmap_offset.
I'll move this to later patch where the out_vfmmio_mmap_offset being changed.
Quoted 4 lines
Otherwise looks OK
Reviewed-by Jason Gunthorpe <[email protected]>
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 6/24 iommu/amd: Map vIOMMU VF and VF Control MMIO BARs
In reply to Suravee SuthikulpanitEnable hardware vIOMMU on an IOMMU by locating its PCI vendor-specific capability (VSC), reading the VF and VF Control BAR addresses, and mapping them for host access (256MB VF, 4MB VF Control).
VF Control covers the first 4K of guest IOMMU MMIO (control registers, trapped by QEMU). VF MMIO covers the third 4K (virtualized by the IOMMU). Per-guest bases use the Guest ID from the previous patch.
Initialize the per-amd_iommu gid_ida here so amd_iommu_gid_alloc() can run when IOMMUFD creates a vIOMMU instance. Export MMIO map helpers and call amd_viommu_uninit() from IOMMU teardown.
--- drivers/iommu/amd/amd_iommu.h | 2 + drivers/iommu/amd/amd_iommu_types.h | 34 ++++++++ drivers/iommu/amd/amd_viommu.h | 6 ++ drivers/iommu/amd/init.c | 5 +- drivers/iommu/amd/viommu.c | 124 ++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -0
@@ -28,6 +28,8 @@ void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid);void iommu_feature_enable (struct amd_iommu *iommu,u8 bit);void *__init iommu_alloc_4k_pages (struct amd_iommu *iommu,gfp_t gfp,size_t size); +u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end); +void __init iommu_unmap_mmio_space (struct amd_iommu *iommu);#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );
drivers/iommu/amd/amd_iommu_types.h +34 -0
@@ -40,6 +40,12 @@#define MMIO_RANGE_OFFSET 0x0c#define MMIO_MISC_OFFSET 0x10 +/* vIOMMU Capability offsets (from IOMMU Capability Header) */ +#define MMIO_VSC_VF_BAR_LO_OFFSET 0x08 +#define MMIO_VSC_VF_BAR_HI_OFFSET 0x0c +#define MMIO_VSC_VF_CNTL_BAR_LO_OFFSET 0x10 +#define MMIO_VSC_VF_CNTL_BAR_HI_OFFSET 0x14 +/* Masks, shifts and macros to parse the device range capability */ #define MMIO_RANGE_LD_MASK 0xff000000#define MMIO_RANGE_FD_MASK 0x00ff0000
@@ -473,6 +479,20 @@ extern bool amdr_ivrs_remap_support;#define for_each_ivhd_dte_flags (entry) \ list_for_each_entry((entry), &amd_ivhd_dev_flags_list, list) +/* VIOMMU stuff */ +#define VIOMMU_VF_MMIO_ENTRY_SIZE 4096 +#define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE 64 + +/* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */ +#define VIOMMU_VF_MMIO_MAP_SIZE 0x10000000UL +#define VIOMMU_VF_CNTL_MMIO_MAP_SIZE 0x400000UL + +#define VIOMMU_VF_MMIO_BASE (iommu, guestId) \ + (iommu->vf_base + (guestId * VIOMMU_VF_MMIO_ENTRY_SIZE)) + +#define VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId) \ + (iommu->vfctrl_base + (guestId * VIOMMU_VFCTRL_MMIO_ENTRY_SIZE)) +struct amd_iommu ;struct iommu_domain ;struct irq_domain ;
@@ -686,6 +706,20 @@ struct amd_iommu {*/ u16 cap_ptr; +/* Vendor-Specific Capability (VSC) pointer. */ +u16 vsc_offset; + +/* +* VF MMIO base physical address. This is needed to calculate/pass +* per guest VF MMIO address (3rd 4K of IOMMU MMIO space) +*/ +u64 vf_base_phys; +u64 vf_cntl_phys; + +/* virtual addresses of vIOMMU VF/VF_CNTL BAR */ +u8 __iomem *vf_base; +u8 __iomem *vfctrl_base; +/* pci domain of this IOMMU */ struct amd_iommu_pci_seg *pci_seg;
drivers/iommu/amd/amd_viommu.h +6 -0
@@ -10,6 +10,8 @@int amd_viommu_init (struct amd_iommu *iommu); +void __init amd_viommu_uninit (struct amd_iommu *iommu); +#else static inline int amd_viommu_init (struct amd_iommu *iommu)
@@ -17,6 +19,10 @@ static inline int amd_viommu_init(struct amd_iommu *iommu)return -EOPNOTSUPP ; } +static inline void amd_viommu_uninit (struct amd_iommu *iommu) +{ +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/init.c +3 -2
@@ -459,7 +459,7 @@ static void iommu_disable(struct amd_iommu *iommu)* mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in * the system has one. */ -static u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) +u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) {if (!request_mem_region (address, end,"amd_iommu" )) { pr_err("Can not reserve memory region %llx-%llx for mmio\n",
@@ -471,7 +471,7 @@ static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)return (u8 __iomem *)ioremap (address, end); } -static void __init iommu_unmap_mmio_space (struct amd_iommu *iommu) +void __init iommu_unmap_mmio_space (struct amd_iommu *iommu) {if (iommu->mmio_base)iounmap (iommu->mmio_base);
@@ -1790,6 +1790,7 @@ static void __init free_iommu_one(struct amd_iommu *iommu)free_iommu_buffers (iommu);amd_iommu_free_ppr_log (iommu);free_ga_log (iommu); +amd_viommu_uninit (iommu);iommu_unmap_mmio_space (iommu);amd_iommu_iopf_uninit (iommu); }
drivers/iommu/amd/viommu.c +124 -0
145 lines not on this page · raw message
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 06/24] iommu/amd: Map vIOMMU VF and VF Control MMIO BARs
Hi Suravee,
Quoted 74 lines · Suravee Suthikulpanit
Enable hardware vIOMMU on an IOMMU by locating its PCI vendor-specific capability (VSC), reading the VF and VF Control BAR addresses, and mapping them for host access (256MB VF, 4MB VF Control).
VF Control covers the first 4K of guest IOMMU MMIO (control registers, trapped by QEMU). VF MMIO covers the third 4K (virtualized by the IOMMU). Per-guest bases use the Guest ID from the previous patch.
Initialize the per-amd_iommu
gid_idahere soamd_iommu_gid_alloc()can run when IOMMUFD creates a vIOMMU instance. Export MMIO map helpers and callamd_viommu_uninit()from IOMMU teardown.Signed-off-by Vasant Hegde <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu.h | 2 + drivers/iommu/amd/amd_iommu_types.h | 34 ++++++++ drivers/iommu/amd/amd_viommu.h | 6 ++ drivers/iommu/amd/init.c | 5 +- drivers/iommu/amd/viommu.c | 124 ++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 2 deletions(-)drivers/iommu/amd/amd_iommu.h +2
-0@@ -28,6 +28,8 @@ void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid);void iommu_feature_enable (struct amd_iommu *iommu,u8 bit);void *__init iommu_alloc_4k_pages (struct amd_iommu *iommu,gfp_t gfp,size_t size); +u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end); +void __init iommu_unmap_mmio_space (struct amd_iommu *iommu);#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );drivers/iommu/amd/amd_iommu_types.h +20
-0@@ -40,6 +40,12 @@#define MMIO_RANGE_OFFSET 0x0c#define MMIO_MISC_OFFSET 0x10 +/* vIOMMU Capability offsets (from IOMMU Capability Header) */ +#define MMIO_VSC_VF_BAR_LO_OFFSET 0x08 +#define MMIO_VSC_VF_BAR_HI_OFFSET 0x0c +#define MMIO_VSC_VF_CNTL_BAR_LO_OFFSET 0x10 +#define MMIO_VSC_VF_CNTL_BAR_HI_OFFSET 0x14 +/* Masks, shifts and macros to parse the device range capability */ #define MMIO_RANGE_LD_MASK 0xff000000#define MMIO_RANGE_FD_MASK 0x00ff0000@@ -473,6 +479,20 @@ extern bool amdr_ivrs_remap_support;#define for_each_ivhd_dte_flags (entry) \ list_for_each_entry((entry), &amd_ivhd_dev_flags_list, list) +/* VIOMMU stuff */ +#define VIOMMU_VF_MMIO_ENTRY_SIZE 4096 +#define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE 64 + +/* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */ +#define VIOMMU_VF_MMIO_MAP_SIZE 0x10000000UL +#define VIOMMU_VF_CNTL_MMIO_MAP_SIZE 0x400000UL + +#define VIOMMU_VF_MMIO_BASE (iommu, guestId) \ + (iommu->vf_base + (guestId * VIOMMU_VF_MMIO_ENTRY_SIZE)) + +#define VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId) \ + (iommu->vfctrl_base + (guestId * VIOMMU_VFCTRL_MMIO_ENTRY_SIZE)) +struct amd_iommu ;struct iommu_domain ;struct irq_domain ;
drivers/iommu/amd/amd_iommu_types.h struct amd_iommu { · line 710
Quoted 5 lines
drivers/iommu/amd/amd_iommu_types.h +2
-0@@ -686,6 +706,20 @@ struct amd_iommu {*/ u16 cap_ptr; +/* Vendor-Specific Capability (VSC) pointer. */ +u16 vsc_offset;
I think we can remove tracking vsc_offset (see below).
drivers/iommu/amd/amd_viommu.h
Quoted 17 lines
drivers/iommu/amd/amd_iommu_types.h +12
-0+ +/* +* VF MMIO base physical address. This is needed to calculate/pass +* per guest VF MMIO address (3rd 4K of IOMMU MMIO space) +*/ +u64 vf_base_phys; +u64 vf_cntl_phys; + +/* virtual addresses of vIOMMU VF/VF_CNTL BAR */ +u8 __iomem *vf_base; +u8 __iomem *vfctrl_base; +/* pci domain of this IOMMU */ struct amd_iommu_pci_seg *pci_seg;drivers/iommu/amd/amd_viommu.h +0
-0
.../...
Quoted 21 lines
drivers/iommu/amd/viommu.c +6
-0@@ -7,9 +7,15 @@#define dev_fmt (fmt) pr_fmt(fmt)#include <linux/iommu.h> +#include <linux/amd-iommu.h> + +#include <linux/fs.h> +#include <linux/cdev.h> +#include <linux/ioctl.h> #include <linux/iommufd.h> #include <linux/amd-iommu.h> #include <uapi/linux/iommufd.h> +#include <linux/mem_encrypt.h> #include <asm/iommu.h> #include <asm/set_memory.h>
drivers/iommu/amd/viommu.c line 31
Quoted 8 lines
drivers/iommu/amd/viommu.c +5
-0@@ -18,12 +24,130 @@#include "amd_iommu.h" #include "amd_iommu_types.h" #include "amd_viommu.h" +#include "../iommu-pages.h" + +LIST_HEAD (viommu_devid_map); + +static int viommu_init_pci_vsc (struct amd_iommu *iommu)
May be viommu_find_pci_vsc as its not initializing, instead find the offset.
Also if we pass vsc_offset to viommu_vf_vfcntl_init() -OR- call this function from viommu_vf_vfcntl_init() then we can skip tracking vsc_offset in amd_iommu structure.
Quoted 4 lines
drivers/iommu/amd/viommu.c +4
-0+{ + iommu->vsc_offset =pci_find_capability (iommu->dev,PCI_CAP_ID_VNDR ); +if (!iommu->vsc_offset) +return -ENODEV ;
drivers/iommu/amd/viommu.c line 45
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+ +DUMP_printk ("device:%s, vsc offset:%04x\n" , +pci_name (iommu->dev), iommu->vsc_offset); +return 0 ; +} + +static void amd_viommu_gid_ida_init (struct amd_iommu *iommu) +{ +ida_init (&iommu->gid_ida); + iommu->gid_ida_inited = true;
Once we remove gid_ida_inited may be we can call ida_init inside amd_viommu_init() itself.
Quoted 52 lines
drivers/iommu/amd/viommu.c +52
-0+} + +static void amd_viommu_gid_ida_fini (struct amd_iommu *iommu) +{ +if (!iommu->gid_ida_inited) +return ; + +ida_destroy (&iommu->gid_ida); + iommu->gid_ida_inited = false; +} + +static void __init amd_viommu_vf_vfcntl_unmap (struct amd_iommu *iommu) +{ +if (iommu->vfctrl_base) { +iounmap (iommu->vfctrl_base); + iommu->vfctrl_base =NULL ; + } +if (iommu->vf_cntl_phys) +release_mem_region (iommu->vf_cntl_phys,VIOMMU_VF_CNTL_MMIO_MAP_SIZE ); + +if (iommu->vf_base) { +iounmap (iommu->vf_base); + iommu->vf_base =NULL ; + } +if (iommu->vf_base_phys) +release_mem_region (iommu->vf_base_phys,VIOMMU_VF_MMIO_MAP_SIZE ); +} + +void __init amd_viommu_uninit (struct amd_iommu *iommu) +{ +amd_viommu_gid_ida_fini (iommu); +amd_viommu_vf_vfcntl_unmap (iommu); +} + +static int __init viommu_vf_vfcntl_init (struct amd_iommu *iommu) +{ +u32 lo, hi; +u64 vf_phys, vf_cntl_phys; + +/* Setting up VF and VF_CNTL MMIOs */ +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_BAR_LO_OFFSET , &lo); +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_BAR_HI_OFFSET , &hi); + vf_phys = hi; + vf_phys = (vf_phys <<32 ) | lo; +if (!(vf_phys &1 )) { +pr_err (FW_BUG "vf_phys disabled\n" ); +return -EINVAL ; + } + +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_CNTL_BAR_LO_OFFSET , &lo); +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_CNTL_BAR_HI_OFFSET , &hi); + vf_cntl_phys = hi;
drivers/iommu/amd/viommu.c line 107
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+ vf_cntl_phys = (vf_cntl_phys <<32 ) | lo; +if (!(vf_cntl_phys &1 )) { +pr_err (FW_BUG "vf_cntl_phys disabled\n" ); +return -EINVAL ; + } + +if (!vf_phys || !vf_cntl_phys) { +pr_err (FW_BUG "AMD-Vi: Unassigned VF resources.\n" ); +return -ENOMEM ; + }
Redundant check as previous check confirms both vf_phys and vf_cntl_phys is enabled. If we want to check non-zero address then we have to remove bit zero and add check
Quoted 9 lines
drivers/iommu/amd/viommu.c +9
-0+ +/* Mapping 256MB of VF and 4MB of VF_CNTL BARs */ + vf_phys &= ~1ULL ; + iommu->vf_base =iommu_map_mmio_space (vf_phys,VIOMMU_VF_MMIO_MAP_SIZE ); +if (!iommu->vf_base) { +pr_err ("Can't reserve vf_base\n" ); +return -ENOMEM ; + } + iommu->vf_base_phys = vf_phys;
drivers/iommu/amd/viommu.c line 126
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+ + vf_cntl_phys &= ~1ULL ; + iommu->vfctrl_base =iommu_map_mmio_space (vf_cntl_phys,VIOMMU_VF_CNTL_MMIO_MAP_SIZE ); +if (!iommu->vfctrl_base) { +pr_err ("Can't reserve vfctrl_base\n" ); + goto err_out; + } + iommu->vf_cntl_phys = vf_cntl_phys; + + pr_debug("%s: IOMMU device:%s, vf_base:%#llx, vfctrl_base:%#llx\n",
better DUMP_printk ?
drivers/iommu/amd/viommu.c line 130
Quoted 4 lines
drivers/iommu/amd/viommu.c +4
-0+ __func__, pci_name(iommu->dev), vf_phys, vf_cntl_phys); +return 0 ; +err_out: +amd_viommu_uninit (iommu);
Its odd. We shouldn't call amd_viommu_uninit from here. May be call amd_viommu_vf_vfcntl_unmap() ?
-Vasant
Suthikulpanit, Suravee
reviewerRe: [PATCH 06/24] iommu/amd: Map vIOMMU VF and VF Control MMIO BARs
Quoted 22 lines · Vasant Hegde
Hi Suravee,
Quoted 20 lines
.....
drivers/iommu/amd/viommu.c +6
-0@@ -7,9 +7,15 @@#define dev_fmt (fmt) pr_fmt(fmt)#include <linux/iommu.h> +#include <linux/amd-iommu.h> + +#include <linux/fs.h> +#include <linux/cdev.h> +#include <linux/ioctl.h> #include <linux/iommufd.h> #include <linux/amd-iommu.h> #include <uapi/linux/iommufd.h> +#include <linux/mem_encrypt.h>
Quoted 13 lines
drivers/iommu/amd/viommu.c line 31
Quoted 11 lines
drivers/iommu/amd/viommu.c +5
-0#include <asm/iommu.h> #include <asm/set_memory.h> @@ -18,12 +24,130 @@#include "amd_iommu.h" #include "amd_iommu_types.h" #include "amd_viommu.h" +#include "../iommu-pages.h" + +LIST_HEAD (viommu_devid_map); + +static int viommu_init_pci_vsc (struct amd_iommu *iommu)May be
viommu_find_pci_vscas its not initializing, instead find the offset.
I'll remove this
Quoted 3 lines
Also if we pass
vsc_offsettoviommu_vf_vfcntl_init()-OR- call this function fromviommu_vf_vfcntl_init()then we can skip trackingvsc_offsetinamd_iommustructure.
Ok. I'll refactor to consolidate viommu_init_pci_vsc() and viommu_vf_vfcntl_init() and remove vsc_offset.
Quoted 9 lines
Quoted 9 lines
drivers/iommu/amd/viommu.c +9
-0+{ + iommu->vsc_offset =pci_find_capability (iommu->dev,PCI_CAP_ID_VNDR ); +if (!iommu->vsc_offset) +return -ENODEV ; + +DUMP_printk ("device:%s, vsc offset:%04x\n" , +pci_name (iommu->dev), iommu->vsc_offset); +return 0 ; +}
Quoted 8 lines
drivers/iommu/amd/viommu.c line 45
Quoted 5 lines
drivers/iommu/amd/viommu.c +5
-0+ +static void amd_viommu_gid_ida_init (struct amd_iommu *iommu) +{ +ida_init (&iommu->gid_ida); + iommu->gid_ida_inited = true;Once we remove
gid_ida_initedmay be we can callida_initinsideamd_viommu_init()itself.
Ok.
Quoted 58 lines
Quoted 58 lines
drivers/iommu/amd/viommu.c +58
-0+} + +static void amd_viommu_gid_ida_fini (struct amd_iommu *iommu) +{ +if (!iommu->gid_ida_inited) +return ; + +ida_destroy (&iommu->gid_ida); + iommu->gid_ida_inited = false; +} + +static void __init amd_viommu_vf_vfcntl_unmap (struct amd_iommu *iommu) +{ +if (iommu->vfctrl_base) { +iounmap (iommu->vfctrl_base); + iommu->vfctrl_base =NULL ; + } +if (iommu->vf_cntl_phys) +release_mem_region (iommu->vf_cntl_phys,VIOMMU_VF_CNTL_MMIO_MAP_SIZE ); + +if (iommu->vf_base) { +iounmap (iommu->vf_base); + iommu->vf_base =NULL ; + } +if (iommu->vf_base_phys) +release_mem_region (iommu->vf_base_phys,VIOMMU_VF_MMIO_MAP_SIZE ); +} + +void __init amd_viommu_uninit (struct amd_iommu *iommu) +{ +amd_viommu_gid_ida_fini (iommu); +amd_viommu_vf_vfcntl_unmap (iommu); +} + +static int __init viommu_vf_vfcntl_init (struct amd_iommu *iommu) +{ +u32 lo, hi; +u64 vf_phys, vf_cntl_phys; + +/* Setting up VF and VF_CNTL MMIOs */ +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_BAR_LO_OFFSET , &lo); +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_BAR_HI_OFFSET , &hi); + vf_phys = hi; + vf_phys = (vf_phys <<32 ) | lo; +if (!(vf_phys &1 )) { +pr_err (FW_BUG "vf_phys disabled\n" ); +return -EINVAL ; + } + +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_CNTL_BAR_LO_OFFSET , &lo); +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_CNTL_BAR_HI_OFFSET , &hi); + vf_cntl_phys = hi; + vf_cntl_phys = (vf_cntl_phys <<32 ) | lo; +if (!(vf_cntl_phys &1 )) { +pr_err (FW_BUG "vf_cntl_phys disabled\n" ); +return -EINVAL ; + } +
Quoted 8 lines
drivers/iommu/amd/viommu.c line 107
Quoted 4 lines
drivers/iommu/amd/viommu.c +4
-0+if (!vf_phys || !vf_cntl_phys) { +pr_err (FW_BUG "AMD-Vi: Unassigned VF resources.\n" ); +return -ENOMEM ; + }Redundant check as previous check confirms both
vf_physandvf_cntl_physis enabled. If we want to check non-zero address then we have to remove bit zero and add check
Good point. I'll remove this for now.
Quoted 13 lines
Quoted 13 lines
drivers/iommu/amd/viommu.c +13
-0+ +/* Mapping 256MB of VF and 4MB of VF_CNTL BARs */ + vf_phys &= ~1ULL ; + iommu->vf_base =iommu_map_mmio_space (vf_phys,VIOMMU_VF_MMIO_MAP_SIZE ); +if (!iommu->vf_base) { +pr_err ("Can't reserve vf_base\n" ); +return -ENOMEM ; + } + iommu->vf_base_phys = vf_phys; + + vf_cntl_phys &= ~1ULL ; + iommu->vfctrl_base =iommu_map_mmio_space (vf_cntl_phys,VIOMMU_VF_CNTL_MMIO_MAP_SIZE ); +if (!iommu->vfctrl_base) {
Quoted 8 lines
drivers/iommu/amd/viommu.c line 126
Quoted 6 lines
drivers/iommu/amd/viommu.c +6
-0+pr_err ("Can't reserve vfctrl_base\n" ); + goto err_out; + } + iommu->vf_cntl_phys = vf_cntl_phys; + + pr_debug("%s: IOMMU device:%s, vf_base:%#llx, vfctrl_base:%#llx\n",better
DUMP_printk?
Actually, I prefer pr_debug().
Quoted 7 lines
drivers/iommu/amd/viommu.c line 130
Quoted 4 lines
drivers/iommu/amd/viommu.c +4
-0+ __func__, pci_name(iommu->dev), vf_phys, vf_cntl_phys); +return 0 ; +err_out: +amd_viommu_uninit (iommu);Its odd. We shouldn't call
amd_viommu_uninitfrom here. May be callamd_viommu_vf_vfcntl_unmap()?
I'm fixing this in V5.
Thanks,
Suravee
Re: [PATCH 06/24] iommu/amd: Map vIOMMU VF and VF Control MMIO BARs
In reply to Suravee SuthikulpanitQuoted 132 lines · Suravee Suthikulpanit
Enable hardware vIOMMU on an IOMMU by locating its PCI vendor-specific capability (VSC), reading the VF and VF Control BAR addresses, and mapping them for host access (256MB VF, 4MB VF Control).
VF Control covers the first 4K of guest IOMMU MMIO (control registers, trapped by QEMU). VF MMIO covers the third 4K (virtualized by the IOMMU). Per-guest bases use the Guest ID from the previous patch.
Initialize the per-amd_iommu
gid_idahere soamd_iommu_gid_alloc()can run when IOMMUFD creates a vIOMMU instance. Export MMIO map helpers and callamd_viommu_uninit()from IOMMU teardown.Signed-off-by Vasant Hegde <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu.h | 2 + drivers/iommu/amd/amd_iommu_types.h | 34 ++++++++ drivers/iommu/amd/amd_viommu.h | 6 ++ drivers/iommu/amd/init.c | 5 +- drivers/iommu/amd/viommu.c | 124 ++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 2 deletions(-)drivers/iommu/amd/amd_iommu.h +2
-0@@ -28,6 +28,8 @@ void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid);void iommu_feature_enable (struct amd_iommu *iommu,u8 bit);void *__init iommu_alloc_4k_pages (struct amd_iommu *iommu,gfp_t gfp,size_t size); +u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end); +void __init iommu_unmap_mmio_space (struct amd_iommu *iommu);#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );drivers/iommu/amd/amd_iommu_types.h +34
-0@@ -40,6 +40,12 @@#define MMIO_RANGE_OFFSET 0x0c#define MMIO_MISC_OFFSET 0x10 +/* vIOMMU Capability offsets (from IOMMU Capability Header) */ +#define MMIO_VSC_VF_BAR_LO_OFFSET 0x08 +#define MMIO_VSC_VF_BAR_HI_OFFSET 0x0c +#define MMIO_VSC_VF_CNTL_BAR_LO_OFFSET 0x10 +#define MMIO_VSC_VF_CNTL_BAR_HI_OFFSET 0x14 +/* Masks, shifts and macros to parse the device range capability */ #define MMIO_RANGE_LD_MASK 0xff000000#define MMIO_RANGE_FD_MASK 0x00ff0000@@ -473,6 +479,20 @@ extern bool amdr_ivrs_remap_support;#define for_each_ivhd_dte_flags (entry) \ list_for_each_entry((entry), &amd_ivhd_dev_flags_list, list) +/* VIOMMU stuff */ +#define VIOMMU_VF_MMIO_ENTRY_SIZE 4096 +#define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE 64 + +/* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */ +#define VIOMMU_VF_MMIO_MAP_SIZE 0x10000000UL +#define VIOMMU_VF_CNTL_MMIO_MAP_SIZE 0x400000UL + +#define VIOMMU_VF_MMIO_BASE (iommu, guestId) \ + (iommu->vf_base + (guestId * VIOMMU_VF_MMIO_ENTRY_SIZE)) + +#define VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId) \ + (iommu->vfctrl_base + (guestId * VIOMMU_VFCTRL_MMIO_ENTRY_SIZE)) +struct amd_iommu ;struct iommu_domain ;struct irq_domain ;@@ -686,6 +706,20 @@ struct amd_iommu {*/ u16 cap_ptr; +/* Vendor-Specific Capability (VSC) pointer. */ +u16 vsc_offset; + +/* +* VF MMIO base physical address. This is needed to calculate/pass +* per guest VF MMIO address (3rd 4K of IOMMU MMIO space) +*/ +u64 vf_base_phys; +u64 vf_cntl_phys; + +/* virtual addresses of vIOMMU VF/VF_CNTL BAR */ +u8 __iomem *vf_base; +u8 __iomem *vfctrl_base; +/* pci domain of this IOMMU */ struct amd_iommu_pci_seg *pci_seg;drivers/iommu/amd/amd_viommu.h +6
-0@@ -10,6 +10,8 @@int amd_viommu_init (struct amd_iommu *iommu); +void __init amd_viommu_uninit (struct amd_iommu *iommu); +#else static inline int amd_viommu_init (struct amd_iommu *iommu)@@ -17,6 +19,10 @@ static inline int amd_viommu_init(struct amd_iommu *iommu)return -EOPNOTSUPP ; } +static inline void amd_viommu_uninit (struct amd_iommu *iommu) +{ +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */ drivers/iommu/amd/init.c +1
-1@@ -459,7 +459,7 @@ static void iommu_disable(struct amd_iommu *iommu)* mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in * the system has one. */ -static u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) +u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) {if (!request_mem_region (address, end,"amd_iommu" )) { pr_err("Can not reserve memory region %llx-%llx for mmio\n",
drivers/iommu/amd/init.c static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end) · line 473
Quoted 3 lines
drivers/iommu/amd/init.c +0
-0@@ -471,7 +471,7 @@ static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)return (u8 __iomem *)ioremap (address, end); }
iommu_map_mmio_space() calls request_mem_region() before ioremap(), but if ioremap() fails it returns NULL without releasing the region it just reserved. Please make iommu_map_mmio_space() release the region on ioremap() failure
Quoted 164 lines
drivers/iommu/amd/init.c +2
-1-static void __init iommu_unmap_mmio_space (struct amd_iommu *iommu) +void __init iommu_unmap_mmio_space (struct amd_iommu *iommu) {if (iommu->mmio_base)iounmap (iommu->mmio_base);@@ -1790,6 +1790,7 @@ static void __init free_iommu_one(struct amd_iommu *iommu)free_iommu_buffers (iommu);amd_iommu_free_ppr_log (iommu);free_ga_log (iommu); +amd_viommu_uninit (iommu);iommu_unmap_mmio_space (iommu);amd_iommu_iopf_uninit (iommu); }drivers/iommu/amd/viommu.c +124
-0@@ -7,9 +7,15 @@#define dev_fmt (fmt) pr_fmt(fmt)#include <linux/iommu.h> +#include <linux/amd-iommu.h> + +#include <linux/fs.h> +#include <linux/cdev.h> +#include <linux/ioctl.h> #include <linux/iommufd.h> #include <linux/amd-iommu.h> #include <uapi/linux/iommufd.h> +#include <linux/mem_encrypt.h> #include <asm/iommu.h> #include <asm/set_memory.h> @@ -18,12 +24,130 @@#include "amd_iommu.h" #include "amd_iommu_types.h" #include "amd_viommu.h" +#include "../iommu-pages.h" + +LIST_HEAD (viommu_devid_map); + +static int viommu_init_pci_vsc (struct amd_iommu *iommu) +{ + iommu->vsc_offset =pci_find_capability (iommu->dev,PCI_CAP_ID_VNDR ); +if (!iommu->vsc_offset) +return -ENODEV ; + +DUMP_printk ("device:%s, vsc offset:%04x\n" , +pci_name (iommu->dev), iommu->vsc_offset); +return 0 ; +} + +static void amd_viommu_gid_ida_init (struct amd_iommu *iommu) +{ +ida_init (&iommu->gid_ida); + iommu->gid_ida_inited = true; +} + +static void amd_viommu_gid_ida_fini (struct amd_iommu *iommu) +{ +if (!iommu->gid_ida_inited) +return ; + +ida_destroy (&iommu->gid_ida); + iommu->gid_ida_inited = false; +} + +static void __init amd_viommu_vf_vfcntl_unmap (struct amd_iommu *iommu) +{ +if (iommu->vfctrl_base) { +iounmap (iommu->vfctrl_base); + iommu->vfctrl_base =NULL ; + } +if (iommu->vf_cntl_phys) +release_mem_region (iommu->vf_cntl_phys,VIOMMU_VF_CNTL_MMIO_MAP_SIZE ); + +if (iommu->vf_base) { +iounmap (iommu->vf_base); + iommu->vf_base =NULL ; + } +if (iommu->vf_base_phys) +release_mem_region (iommu->vf_base_phys,VIOMMU_VF_MMIO_MAP_SIZE ); +} + +void __init amd_viommu_uninit (struct amd_iommu *iommu) +{ +amd_viommu_gid_ida_fini (iommu); +amd_viommu_vf_vfcntl_unmap (iommu); +} + +static int __init viommu_vf_vfcntl_init (struct amd_iommu *iommu) +{ +u32 lo, hi; +u64 vf_phys, vf_cntl_phys; + +/* Setting up VF and VF_CNTL MMIOs */ +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_BAR_LO_OFFSET , &lo); +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_BAR_HI_OFFSET , &hi); + vf_phys = hi; + vf_phys = (vf_phys <<32 ) | lo; +if (!(vf_phys &1 )) { +pr_err (FW_BUG "vf_phys disabled\n" ); +return -EINVAL ; + } + +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_CNTL_BAR_LO_OFFSET , &lo); +pci_read_config_dword (iommu->dev, iommu->vsc_offset +MMIO_VSC_VF_CNTL_BAR_HI_OFFSET , &hi); + vf_cntl_phys = hi; + vf_cntl_phys = (vf_cntl_phys <<32 ) | lo; +if (!(vf_cntl_phys &1 )) { +pr_err (FW_BUG "vf_cntl_phys disabled\n" ); +return -EINVAL ; + } + +if (!vf_phys || !vf_cntl_phys) { +pr_err (FW_BUG "AMD-Vi: Unassigned VF resources.\n" ); +return -ENOMEM ; + } + +/* Mapping 256MB of VF and 4MB of VF_CNTL BARs */ + vf_phys &= ~1ULL ; + iommu->vf_base =iommu_map_mmio_space (vf_phys,VIOMMU_VF_MMIO_MAP_SIZE ); +if (!iommu->vf_base) { +pr_err ("Can't reserve vf_base\n" ); +return -ENOMEM ; + } + iommu->vf_base_phys = vf_phys; + + vf_cntl_phys &= ~1ULL ; + iommu->vfctrl_base =iommu_map_mmio_space (vf_cntl_phys,VIOMMU_VF_CNTL_MMIO_MAP_SIZE ); +if (!iommu->vfctrl_base) { +pr_err ("Can't reserve vfctrl_base\n" ); + goto err_out; + } + iommu->vf_cntl_phys = vf_cntl_phys; + +pr_debug ("%s: IOMMU device:%s, vf_base:%#llx, vfctrl_base:%#llx\n" , + __func__,pci_name (iommu->dev), vf_phys, vf_cntl_phys); +return 0 ; +err_out: +amd_viommu_uninit (iommu); +return -ENOMEM ; +}int __init amd_viommu_init (struct amd_iommu *iommu) { +int ret; +if (!amd_iommu_viommu || !check_feature (FEATURE_VIOMMU ))return 0 ; + ret =viommu_init_pci_vsc (iommu); +if (ret) +return ret; + + ret =viommu_vf_vfcntl_init (iommu); +if (ret) +return ret; + +amd_viommu_gid_ida_init (iommu); +return 0 ; }
Suthikulpanit, Suravee
reviewerRe: [PATCH 06/24] iommu/amd: Map vIOMMU VF and VF Control MMIO BARs
Quoted 19 lines · On 8/19/2026 8:18 PM, [email protected] wrote:
Quoted 17 lines · Suravee Suthikulpanit
.....
drivers/iommu/amd/init.c +1
-1@@ -459,7 +459,7 @@ static void iommu_disable(struct amd_iommu *iommu)* mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in * the system has one. */ -static u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) +u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) { if (!request_mem_region(address, end, "amd_iommu")) { pr_err("Can not reserve memory region %llx-%llx for mmio\n",@@ -471,7 +471,7 @@ static u8 __iomem * __init
Quoted 10 lines
drivers/iommu/amd/init.c static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end) · line 472
Quoted 3 lines
drivers/iommu/amd/init.c +0
-0iommu_map_mmio_space(u64 address, u64 end) return (u8 __iomem *)ioremap(address, end); }
iommu_map_mmio_space()callsrequest_mem_region()beforeioremap(), but ifioremap()fails it returns NULL without releasing the region it just reserved. Please makeiommu_map_mmio_space()release the region onioremap()failure
This is unrelated to this series. I'll fix this separately.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 7/24 iommu/amd: Add support for AMD vIOMMU VF MMIO region
In reply to Suravee SuthikulpanitThe AMD vIOMMU virtualizes guest MMIO registers at the 3rd 4K region. This is achieved using the iommufd_viommu_alloc_mmap().
--- drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/amd_viommu.h | 7 +++++++ drivers/iommu/amd/iommufd.c | 17 ++++++++++++++++- drivers/iommu/amd/viommu.c | 10 ++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -540,6 +540,9 @@ struct amd_iommu_viommu {* Indexed by guest domain ID. */ struct xarray gdomid_array; + +/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */ +unsigned long vfmmio_mmap_offset; };/*
drivers/iommu/amd/amd_viommu.h +7 -0
@@ -12,6 +12,8 @@ int amd_viommu_init(struct amd_iommu *iommu);void __init amd_viommu_uninit (struct amd_iommu *iommu); +u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid); +#else static inline int amd_viommu_init (struct amd_iommu *iommu)
@@ -23,6 +25,11 @@ static inline void amd_viommu_uninit(struct amd_iommu *iommu) { } +static inline u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid) +{ +return 0 ; +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/iommufd.c +16 -1
@@ -7,6 +7,7 @@#include "iommufd.h" #include "amd_iommu.h" +#include "amd_viommu.h" #include "amd_iommu_types.h" static const struct iommufd_viommu_ops amd_viommu_ops;
@@ -44,11 +45,12 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * const struct iommu_user_data *user_data) {int ret; +phys_addr_t page_base;unsigned long flags;struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); - struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu); + struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 ); aviommu->parent = pdom;
@@ -68,6 +70,16 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * aviommu->gid = ret;pr_debug ("%s: gid=%#x" , __func__, aviommu->gid); + page_base =amd_viommu_get_vfmmio_addr (iommu, aviommu->gid); + + ret =iommufd_viommu_alloc_mmap (&aviommu->core, + page_base,SZ_4K , + &aviommu->vfmmio_mmap_offset); +if (ret) + goto err_mmap; + + data.out_vfmmio_mmap_offset = aviommu->vfmmio_mmap_offset; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , out_vfmmio_mmap_offset);
@@ -82,6 +94,8 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return 0 ; err_init: +iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); +err_mmap:amd_iommu_gid_free (iommu, aviommu->gid); err_gid:return ret;
@@ -100,6 +114,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)list_del (&aviommu->pdom_list);spin_unlock_irqrestore (&pdom->lock, flags);xa_destroy (&aviommu->gdomid_array); +iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);amd_iommu_gid_free (iommu, aviommu->gid); }
drivers/iommu/amd/viommu.c +10 -0
@@ -131,6 +131,16 @@ static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu)return -ENOMEM ; } +/* +* Returns VF MMIO BAR offset for the give guest ID which will be +* mapped to guest vIOMMU 3rd 4K MMIO address +*/ +u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid) +{ +/* TODO: Add check for sVIOMMU and set gid[bit 15] */ +return iommu->vf_base_phys + gid *VIOMMU_VF_MMIO_ENTRY_SIZE ; +} +int __init amd_viommu_init (struct amd_iommu *iommu) {int ret;
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 07/24] iommu/amd: Add support for AMD vIOMMU VF MMIO region
Quoted 6 lines · Suravee Suthikulpanit
The AMD vIOMMU virtualizes guest MMIO registers at the 3rd 4K region. This is achieved using the
iommufd_viommu_alloc_mmap().Co-developed-by Vasant Hegde <[email protected]>Signed-off-by Vasant Hegde <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
Quoted 7 lines
--- drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/amd_viommu.h | 7 +++++++ drivers/iommu/amd/iommufd.c | 17 ++++++++++++++++- drivers/iommu/amd/viommu.c | 10 ++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-)
.../...
Quoted 6 lines
drivers/iommu/amd/viommu.c +0
-0
drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 140
Quoted 10 lines
drivers/iommu/amd/viommu.c +7
-0@@ -131,6 +131,16 @@ static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu)return -ENOMEM ; } +/* +* Returns VF MMIO BAR offset for the give guest ID which will be +* mapped to guest vIOMMU 3rd 4K MMIO address +*/ +u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid) +{ +/* TODO: Add check for sVIOMMU and set gid[bit 15] */
We can remove this statement. For secure vIOMMU we should be fixing GID allocation path.
-Vasant
Re: [PATCH 07/24] iommu/amd: Add support for AMD vIOMMU VF MMIO region
In reply to Suravee SuthikulpanitQuoted 2 lines · Suravee Suthikulpanit
The AMD vIOMMU virtualizes guest MMIO registers at the 3rd 4K region. This is achieved using the
iommufd_viommu_alloc_mmap().
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 8/24 iommu/amd: Introduce Reset vMMIO Command
In reply to Suravee SuthikulpanitIntroduce new IOMMU commands for vIOMMU to reset
virtualized MMIO registers of a particular guest.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/iommu.c | 22 ++++++++++++++++++++++ drivers/iommu/amd/iommufd.c | 3 +++ 4 files changed, 27 insertions(+)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -11,6 +11,7 @@#include "amd_iommu_types.h" +void iommu_reset_vmmio (struct amd_iommu *iommu,u16 gid);extern int amd_iommu_evtlog_size;extern int amd_iommu_pprlog_size;
drivers/iommu/amd/amd_iommu_types.h +1 -0
@@ -218,6 +218,7 @@#define CMD_INV_IRT 0x05#define CMD_COMPLETE_PPR 0x07#define CMD_INV_ALL 0x08 +#define CMD_RESET_VMMIO 0x0A#define CMD_COMPL_WAIT_STORE_MASK 0x01#define CMD_COMPL_WAIT_INT_MASK 0x02
drivers/iommu/amd/iommu.c +22 -0
@@ -1428,6 +1428,18 @@ static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)CMD_SET_TYPE (cmd,CMD_INV_IRT ); } +static void build_reset_vmmio (struct iommu_cmd *cmd,u16 gid, +bool vcmd,bool all) +{ +memset (cmd,0 ,sizeof (*cmd)); + cmd->data[0 ] = gid; +if (all) + cmd->data[0 ] |= (1 <<28 ); +if (vcmd) + cmd->data[0 ] |= (1 <<31 ); +CMD_SET_TYPE (cmd,CMD_RESET_VMMIO ); +} +/* * Writes the command to the IOMMUs command buffer and informs the * hardware about the new command.
@@ -1668,6 +1680,16 @@ void amd_iommu_flush_all_caches(struct amd_iommu *iommu) } } +void iommu_reset_vmmio (struct amd_iommu *iommu,u16 gid) +{ +struct iommu_cmd cmd; + +build_reset_vmmio (&cmd, gid,1 ,1 ); + +iommu_queue_command (iommu, &cmd); +amd_iommu_completion_wait (iommu); +} +/* * Command send function for flushing on-device TLB */
drivers/iommu/amd/iommufd.c +3 -0
@@ -80,6 +80,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * data.out_vfmmio_mmap_offset = aviommu->vfmmio_mmap_offset; +/* Reset vIOMMU MMIOs to initialize the vIOMMU */ +iommu_reset_vmmio (iommu, aviommu->gid); + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , out_vfmmio_mmap_offset);
Signature · 1 line
2.34.1
Re: [PATCH 08/24] iommu/amd: Introduce Reset vMMIO Command
Quoted 45 lines · Suravee Suthikulpanit
Introduce new IOMMU commands for vIOMMU to reset
virtualized MMIO registers of a particular guest.Reviewed-by Weinan Liu <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/iommu.c | 22 ++++++++++++++++++++++ drivers/iommu/amd/iommufd.c | 3 +++ 4 files changed, 27 insertions(+)drivers/iommu/amd/amd_iommu.h +1
-0@@ -11,6 +11,7 @@#include "amd_iommu_types.h" +void iommu_reset_vmmio (struct amd_iommu *iommu,u16 gid);extern int amd_iommu_evtlog_size;extern int amd_iommu_pprlog_size;drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -218,6 +218,7 @@#define CMD_INV_IRT 0x05#define CMD_COMPLETE_PPR 0x07#define CMD_INV_ALL 0x08 +#define CMD_RESET_VMMIO 0x0A#define CMD_COMPL_WAIT_STORE_MASK 0x01#define CMD_COMPL_WAIT_INT_MASK 0x02drivers/iommu/amd/iommu.c +1
-0@@ -1428,6 +1428,18 @@ static void build_inv_irt(struct iommu_cmd *cmd, u16 devid)CMD_SET_TYPE (cmd,CMD_INV_IRT ); } +static void build_reset_vmmio(struct iommu_cmd *cmd, u16 gid,
drivers/iommu/amd/iommu.c static void build_inv_irt(struct iommu_cmd *cmd, u16 devid) · line 1441
Quoted 10 lines
drivers/iommu/amd/iommu.c +10
-0+ bool vcmd, bool all) +{ +memset (cmd,0 ,sizeof (*cmd)); + cmd->data[0 ] = gid; +if (all) + cmd->data[0 ] |= (1 <<28 ); +if (vcmd) + cmd->data[0 ] |= (1 <<31 ); +CMD_SET_TYPE (cmd,CMD_RESET_VMMIO ); +}
This looks like a bug. Per the AMD IOMMU Specification (Rev 3.10, Section 2.4.10), the 'All' bit of the RESET_VMMIO command is at bit position 27, not bit 28. Writing bit 28 targets a reserved field and may either trigger an ILLEGAL_COMMAND_ERROR or silently fail to reset the full vIOMMU context. This should be:
if (all)
cmd->data[0] |= (1 << 27);
Quoted 8 lines
drivers/iommu/amd/iommu.c +1
-0+/* * Writes the command to the IOMMUs command buffer and informs the * hardware about the new command. @@ -1668,6 +1680,16 @@ void amd_iommu_flush_all_caches(struct amd_iommu *iommu) } }
drivers/iommu/amd/iommu.c void amd_iommu_flush_all_caches(struct amd_iommu *iommu) · line 1692
Quoted 10 lines
drivers/iommu/amd/iommu.c +10
-0+void iommu_reset_vmmio (struct amd_iommu *iommu,u16 gid) +{ +struct iommu_cmd cmd; + +build_reset_vmmio (&cmd, gid,1 ,1 ); + +iommu_queue_command (iommu, &cmd); +amd_iommu_completion_wait (iommu); +} +
Both iommu_queue_command() and amd_iommu_completion_wait() return status, but iommu_reset_vmmio() returns void and ignores them. A failed reset during vIOMMU init would go unnoticed. Consider propagating the error to the caller.
Quoted 17 lines
drivers/iommu/amd/iommu.c +0
-0/* * Command send function for flushing on-device TLB */ drivers/iommu/amd/iommufd.c +3
-0@@ -80,6 +80,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * data.out_vfmmio_mmap_offset = aviommu->vfmmio_mmap_offset; +/* Reset vIOMMU MMIOs to initialize the vIOMMU */ +iommu_reset_vmmio (iommu, aviommu->gid); + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , out_vfmmio_mmap_offset);
Suthikulpanit, Suravee
reviewerRe: [PATCH 08/24] iommu/amd: Introduce Reset vMMIO Command
Quoted 60 lines · On 8/19/2026 3:34 PM, [email protected] wrote:
Quoted 41 lines · Suravee Suthikulpanit
Introduce new IOMMU commands for vIOMMU to reset
virtualized MMIO registers of a particular guest.Reviewed-by Weinan Liu <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>---
drivers/iommu/amd/amd_iommu.h | 1 +drivers/iommu/amd/amd_iommu_types.h| 1 + drivers/iommu/amd/iommu.c | 22 ++++++++++++++++++++++ drivers/iommu/amd/iommufd.c | 3 +++4 files changed, 27 insertions(+)drivers/iommu/amd/amd_iommu.h +1
-0@@ -11,6 +11,7 @@ #include "amd_iommu_types.h" +void iommu_reset_vmmio (struct amd_iommu *iommu,u16 gid); extern int amd_iommu_evtlog_size; extern int amd_iommu_pprlog_size;drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -218,6 +218,7 @@ #define CMD_INV_IRT 0x05 #define CMD_COMPLETE_PPR 0x07 #define CMD_INV_ALL 0x08 +#define CMD_RESET_VMMIO 0x0A #define CMD_COMPL_WAIT_STORE_MASK 0x01 #define CMD_COMPL_WAIT_INT_MASK 0x02drivers/iommu/amd/iommu.c +1
-0@@ -1428,6 +1428,18 @@ static void build_inv_irt(struct iommu_cmd *cmd, u16 devid) CMD_SET_TYPE(cmd, CMD_INV_IRT); } +static void build_reset_vmmio(struct iommu_cmd *cmd, u16 gid,drivers/iommu/amd/iommu.c static void build_inv_irt(struct iommu_cmd · line 1441
Quoted 10 lines
drivers/iommu/amd/iommu.c +10
-0+ bool vcmd, bool all) +{ + memset(cmd, 0, sizeof(*cmd)); + cmd->data[0] = gid; + if (all) + cmd->data[0] |= (1 << 28); + if (vcmd) + cmd->data[0] |= (1 << 31); + CMD_SET_TYPE(cmd, CMD_RESET_VMMIO); +}This looks like a bug. Per the AMD IOMMU Specification (Rev 3.10, Section 2.4.10), the 'All' bit of the
RESET_VMMIOcommand is at bit position 27, not bit 28. Writing bit 28 targets a reserved field and may either trigger anILLEGAL_COMMAND_ERRORor silently fail to reset the full vIOMMU context. This should be:
Quoted 2 lines
if (all)
cmd->data[0] |= (1 << 27);
Actually, this is a typo in the spec. AMD will update the spec in the next revision. The correct bit is 28.
Quoted 15 lines
Quoted 15 lines
drivers/iommu/amd/iommu.c +8
-0+ /** Writes the command to the IOMMUs command buffer and informs the * hardware about the new command. @@ -1668,6 +1680,16 @@ void amd_iommu_flush_all_caches(struct amd_iommu *iommu) } } +void iommu_reset_vmmio (struct amd_iommu *iommu,u16 gid) +{ + struct iommu_cmd cmd; + + build_reset_vmmio(&cmd, gid, 1, 1); + + iommu_queue_command(iommu, &cmd);
Quoted 8 lines
drivers/iommu/amd/iommu.c void amd_iommu_flush_all_caches(struct amd_iommu *iommu) · line 1692
Quoted 3 lines
drivers/iommu/amd/iommu.c +3
-0+ amd_iommu_completion_wait(iommu); +} +Both
iommu_queue_command()andamd_iommu_completion_wait()return status, butiommu_reset_vmmio()returns void and ignores them. A failed reset during vIOMMU init would go unnoticed. Consider propagating the error to the caller.
I'll handle the return of iommu_queue_command() in iommu_reset_vmmio(). The amd_iommu_completion_wait() does not normally handle error in this driver. This needs a separate clean up.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 9/24 iommu/amd: Introduce and map vIOMMU private IPA region
In reply to Suravee SuthikulpanitAMD vIOMMU introduces the IOMMU Private Address (IPA) region for guest-side IOMMU virtualization data structures.
Introduce a per-IOMMU v1 paging domain in viommu_pdom, allocate 8MB of backing memory as four 2MB subregions, map them into the domain, and add viommu_private_space_init() / viommu_private_space_uninit() as a matched pair.
Register the owning IOMMU in viommu_pdom->iommu_array via amd_iommu_pdom_bind_iommu() so map/unmap use the standard domain_flush_pages_v1() IOTLB invalidation path.
For more info, see section vIOMMU Private Address Space of the IOMMU specification [1].
[1] https://docs.amd.com/v/u/en-US/48882_3.10_PUB
--- drivers/iommu/amd/amd_iommu.h | 8 ++ drivers/iommu/amd/amd_iommu_types.h | 11 ++ drivers/iommu/amd/iommu.c | 19 +++- drivers/iommu/amd/viommu.c | 159 ++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +8 -0
@@ -31,6 +31,7 @@ void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu, gfp_t gfp, size_t size);u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end);void __init iommu_unmap_mmio_space (struct amd_iommu *iommu); +int iommu_flush_dte (struct amd_iommu *iommu,u16 devid);#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );
@@ -39,6 +40,8 @@ static inline void amd_iommu_debugfs_setup(void) {}#endif extern bool amd_iommu_viommu; +extern const struct pt_iommu_driver_ops amd_hw_driver_ops_v1; +extern const struct iommu_domain_ops amdv1_ops;/* Needed for interrupt remapping */ int amd_iommu_prepare (void );
@@ -56,6 +59,8 @@ extern bool amd_iommu_hatdis;/* Protection domain ops */ void amd_iommu_init_identity_domain (void );struct protection_domain *protection_domain_alloc (void ); +struct iommu_domain *amd_iommu_domain_alloc_paging_v1 (struct device *dev, +u32 flags);struct iommu_domain *amd_iommu_domain_alloc_sva (struct device *dev,struct mm_struct *mm);void amd_iommu_domain_free (struct iommu_domain *dom);
@@ -100,6 +105,9 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,void amd_iommu_dev_flush_pasid_pages (struct iommu_dev_data *dev_data,ioasid_t pasid,u64 address,size_t size); +int amd_iommu_pdom_bind_iommu (struct amd_iommu *iommu,struct protection_domain *pdom); +void amd_iommu_pdom_unbind_iommu (struct amd_iommu *iommu,struct protection_domain *pdom); +#ifdef CONFIG_IRQ_REMAP int amd_iommu_create_irq_domain (struct amd_iommu *iommu);#else
drivers/iommu/amd/amd_iommu_types.h +11 -0
@@ -424,6 +424,13 @@/* For vIOMMU, the GID is 16-bit. */ #define VIOMMU_MAX_GID 0xFFFF +/* +* Total IOMMU private region is 8MB (4 x 2MB-subregion) +*/ +#define VIOMMU_PRIV_REGION_BASE (0) +#define VIOMMU_PRIV_SUBREGION_CNT (4) +#define VIOMMU_PRIV_SUBREGION_SIZE (0x200000)/* 2MB */ +/* Timeout stuff */ #define LOOP_TIMEOUT 100000#define MMIO_STATUS_TIMEOUT 2000000
@@ -814,6 +821,10 @@ struct amd_iommu {struct ida gid_ida;/* guest IDs for this IOMMU */ bool gid_ida_inited; + +/* HW vIOMMU support */ +struct protection_domain *viommu_pdom; +void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT ]; };static inline struct amd_iommu *dev_to_amd_iommu (struct device *dev)
drivers/iommu/amd/iommu.c +14 -5
@@ -1551,7 +1551,7 @@ static void domain_flush_complete(struct protection_domain *domain)amd_iommu_completion_wait (pdom_iommu_info->iommu); } -static int iommu_flush_dte (struct amd_iommu *iommu,u16 devid) +int iommu_flush_dte (struct amd_iommu *iommu,u16 devid) {struct iommu_cmd cmd;
@@ -2399,6 +2399,16 @@ static void pdom_detach_iommu(struct amd_iommu *iommu,spin_unlock_irqrestore (&pdom->lock, flags); } +int amd_iommu_pdom_bind_iommu (struct amd_iommu *iommu,struct protection_domain *pdom) +{ +return pdom_attach_iommu (iommu, pdom); +} + +void amd_iommu_pdom_unbind_iommu (struct amd_iommu *iommu,struct protection_domain *pdom) +{ +pdom_detach_iommu (iommu, pdom); +} +/* * If a device is not yet associated with a domain, this function makes the * device visible in the domain
@@ -2726,12 +2736,12 @@ static void amd_iommu_iotlb_sync(struct iommu_domain *domain,iommu_put_pages_list (&gather->freelist); } -static const struct pt_iommu_driver_ops amd_hw_driver_ops_v1 = { +const struct pt_iommu_driver_ops amd_hw_driver_ops_v1 = { .get_top_lock = amd_iommu_get_top_lock, .change_top = amd_iommu_change_top, }; -static const struct iommu_domain_ops amdv1_ops = { +const struct iommu_domain_ops amdv1_ops = {IOMMU_PT_DOMAIN_OPS (amdv1), .iotlb_sync_map = amd_iommu_iotlb_sync_map, .flush_iotlb_all = amd_iommu_flush_iotlb_all,
@@ -2746,8 +2756,7 @@ static const struct iommu_dirty_ops amdv1_dirty_ops = { .set_dirty_tracking = amd_iommu_set_dirty_tracking, }; -static struct iommu_domain *amd_iommu_domain_alloc_paging_v1 (struct device *dev, -u32 flags) +struct iommu_domain *amd_iommu_domain_alloc_paging_v1 (struct device *dev,u32 flags) {struct pt_iommu_amdv1_cfg cfg = {};struct protection_domain *domain;
drivers/iommu/amd/viommu.c +159 -0
170 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 09/24] iommu/amd: Introduce and map vIOMMU private IPA region
Quoted 4 lines
[ ... 197 lines skipped ... ]
drivers/iommu/amd/viommu.c +3
-0+static void viommu_priv_unmap_free (struct amd_iommu *iommu,u64 iova,size_t size, +void *cpu_va) +{
drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 192
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+size_t unmapped; + +if (!cpu_va) +return ; +if (!iommu || !iommu->viommu_pdom) +return ; + + unmapped =iommu_unmap (&iommu->viommu_pdom->domain, iova, size); +if (unmapped != size) +pr_warn ("%s: unmapped %#zx of %#lx at %#llx\n" , __func__, unmapped, size, iova);
This should be a WARN_ON, the kernel has malfunctioned if the mapping it put there cannot be unmapped.
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 10/24 iommu/amd: Pass iommu to device_flush_dte()
In reply to Suravee SuthikulpanitTake struct amd_iommu explicitly so DTE invalidation is issued on the same IOMMU instance used to program the DTE. Also, flush each device in amd_iommu_change_top() immediately after updating its DTE.
--- drivers/iommu/amd/iommu.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/iommu/amd/iommu.c +5 -8
@@ -76,7 +76,7 @@ static void set_dte_entry(struct amd_iommu *iommu,struct iommu_dev_data *dev_data, phys_addr_t top_paddr, unsigned int top_level); -static int device_flush_dte (struct iommu_dev_data *dev_data); +static int device_flush_dte (struct amd_iommu *iommu,struct iommu_dev_data *dev_data);static void amd_iommu_change_top (struct pt_iommu *iommu_table,phys_addr_t top_paddr,unsigned int top_level);
@@ -213,7 +213,7 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, {update_dte256 (iommu, dev_data, new);clone_aliases (iommu, dev_data->dev); -device_flush_dte (dev_data); +device_flush_dte (iommu, dev_data);amd_iommu_completion_wait (iommu); }
@@ -1716,9 +1716,8 @@ static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)/* * Command send function for invalidating a device table entry */ -static int device_flush_dte (struct iommu_dev_data *dev_data) +static int device_flush_dte (struct amd_iommu *iommu,struct iommu_dev_data *dev_data) { -struct amd_iommu *iommu =get_amd_iommu_from_dev_data (dev_data);struct pci_dev *pdev =NULL ;struct amd_iommu_pci_seg *pci_seg;u16 alias;
@@ -2685,11 +2684,9 @@ static void amd_iommu_change_top(struct pt_iommu *iommu_table,/* Update the HW references with the new level and top ptr */ set_dte_entry (iommu, dev_data, top_paddr, top_level);clone_aliases (iommu, dev_data->dev); +device_flush_dte (iommu, dev_data); } -list_for_each_entry (dev_data, &pdom->dev_list, list) -device_flush_dte (dev_data); -domain_flush_complete (pdom); }
@@ -3121,7 +3118,7 @@ static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,spin_unlock (&dev_data->dte_lock);/* Flush device DTE */ -device_flush_dte (dev_data); +device_flush_dte (iommu, dev_data); domain_flush = true; }
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 10/24] iommu/amd: Pass iommu to device_flush_dte()
Quoted 44 lines · Suravee Suthikulpanit
Take struct
amd_iommuexplicitly so DTE invalidation is issued on the same IOMMU instance used to program the DTE. Also, flush each device inamd_iommu_change_top()immediately after updating its DTE.Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/iommu.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/iommu/amd/iommu.c +3
-4@@ -76,7 +76,7 @@ static void set_dte_entry(struct amd_iommu *iommu,struct iommu_dev_data *dev_data, phys_addr_t top_paddr, unsigned int top_level); -static int device_flush_dte (struct iommu_dev_data *dev_data); +static int device_flush_dte (struct amd_iommu *iommu,struct iommu_dev_data *dev_data);static void amd_iommu_change_top (struct pt_iommu *iommu_table,phys_addr_t top_paddr,unsigned int top_level);@@ -213,7 +213,7 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, {update_dte256 (iommu, dev_data, new);clone_aliases (iommu, dev_data->dev); -device_flush_dte (dev_data); +device_flush_dte (iommu, dev_data);amd_iommu_completion_wait (iommu); }@@ -1716,9 +1716,8 @@ static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)/* * Command send function for invalidating a device table entry */ -static int device_flush_dte (struct iommu_dev_data *dev_data) +static int device_flush_dte (struct amd_iommu *iommu,struct iommu_dev_data *dev_data) { -struct amd_iommu *iommu =get_amd_iommu_from_dev_data (dev_data);struct pci_dev *pdev =NULL ;struct amd_iommu_pci_seg *pci_seg;u16 alias;
drivers/iommu/amd/iommu.c static void amd_iommu_change_top(struct pt_iommu *iommu_table, · line 2690
Quoted 10 lines
drivers/iommu/amd/iommu.c +1
-3@@ -2685,11 +2684,9 @@ static void amd_iommu_change_top(struct pt_iommu *iommu_table,/* Update the HW references with the new level and top ptr */ set_dte_entry (iommu, dev_data, top_paddr, top_level);clone_aliases (iommu, dev_data->dev); +device_flush_dte (iommu, dev_data); } -list_for_each_entry (dev_data, &pdom->dev_list, list) -device_flush_dte (dev_data); -domain_flush_complete (pdom);
May be just replace all these w/ amd_iommu_update_dte()?
-Vasant
Re: [PATCH 10/24] iommu/amd: Pass iommu to device_flush_dte()
In reply to Suravee SuthikulpanitQuoted 3 lines · Suravee Suthikulpanit
Take struct
amd_iommuexplicitly so DTE invalidation is issued on the same IOMMU instance used to program the DTE. Also, flush each device inamd_iommu_change_top()immediately after updating its DTE.
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 11/24 iommu/amd: Export amd_iommu_alloc_dev_data() helper
In reply to Suravee SuthikulpanitAMD vIOMMU requires programming the DTE of the IOMMU device ID to set up IPA mapping, and current DTE helper functions (for setting / clearing) require struct iommu_dev_data.
However, the IOMMU device does not go through the normal PCI device attach path, which would allocate the struct iommu_dev_data. So, the driver needs to manually allocate and initialize the struct iommu_dev_data for IOMMU in order to reuse the helper functions.
Make alloc_dev_data() available outside iommu.c so callers can allocate struct iommu_dev_data for a given IOMMU instance and device ID without going through the PCI device attach path.
--- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/iommu.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -0
@@ -56,6 +56,8 @@ extern u8 amd_iommu_hpt_vasize;extern unsigned long amd_iommu_pgsize_bitmap;extern bool amd_iommu_hatdis; +struct iommu_dev_data *amd_iommu_alloc_dev_data (struct amd_iommu *iommu,u16 devid); +/* Protection domain ops */ void amd_iommu_init_identity_domain (void );struct protection_domain *protection_domain_alloc (void );
drivers/iommu/amd/iommu.c +2 -2
@@ -388,7 +388,7 @@ static struct amd_iommu *rlookup_amd_iommu(struct device *dev)return __rlookup_amd_iommu (seg,PCI_SBDF_TO_DEVID (devid)); } -static struct iommu_dev_data *alloc_dev_data (struct amd_iommu *iommu,u16 devid) +struct iommu_dev_data *amd_iommu_alloc_dev_data (struct amd_iommu *iommu,u16 devid) {struct iommu_dev_data *dev_data;struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
@@ -510,7 +510,7 @@ static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid) dev_data =search_dev_data (iommu, devid);if (dev_data ==NULL ) { - dev_data =alloc_dev_data (iommu, devid); + dev_data =amd_iommu_alloc_dev_data (iommu, devid);if (!dev_data)return NULL ;
Signature · 1 line
2.34.1
Re: [PATCH 11/24] iommu/amd: Export amd_iommu_alloc_dev_data() helper
Quoted 10 lines · Suravee Suthikulpanit
AMD vIOMMU requires programming the DTE of the IOMMU device ID to set up IPA mapping, and current DTE helper functions (for setting / clearing) require struct
iommu_dev_data.However, the IOMMU device does not go through the normal PCI device attach path, which would allocate the struct
iommu_dev_data. So, the driver needs to manually allocate and initialize the structiommu_dev_datafor IOMMU in order to reuse the helper functions.[...]
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 12/24 iommu/amd: Pass iommu and devid to amd_iommu_make_clear_dte()
In reply to Suravee SuthikulpanitTake struct amd_iommu and device ID explicitly instead of deriving the IOMMU from struct iommu_dev_data.
--- drivers/iommu/amd/amd_iommu.h | 6 +++--- drivers/iommu/amd/iommu.c | 4 ++-- drivers/iommu/amd/nested.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/amd/amd_iommu.h +3 -3
@@ -217,10 +217,10 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,int amd_iommu_completion_wait (struct amd_iommu *iommu);static inline void -amd_iommu_make_clear_dte (struct iommu_dev_data *dev_data,struct dev_table_entry *new) +amd_iommu_make_clear_dte (struct amd_iommu *iommu,u16 devid, +struct dev_table_entry *new) {struct dev_table_entry *initial_dte; -struct amd_iommu *iommu =get_amd_iommu_from_dev (dev_data->dev);/* All existing DTE must have V bit set */ new->data128[0 ] =DTE_FLAG_V ;
@@ -230,7 +230,7 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry* Restore cached persistent DTE bits, which can be set by information * in IVRS table. See set_dev_entry_from_acpi(). */ - initial_dte =amd_iommu_get_ivhd_dte_flags (iommu->pci_seg->id, dev_data->devid); + initial_dte =amd_iommu_get_ivhd_dte_flags (iommu->pci_seg->id, devid);if (initial_dte) { new->data128[0 ] |= initial_dte->data128[0 ]; new->data128[1 ] |= initial_dte->data128[1 ];
drivers/iommu/amd/iommu.c +2 -2
@@ -2237,7 +2237,7 @@ static void set_dte_entry(struct amd_iommu *iommu,struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;struct dev_table_entry *dte = &get_dev_table (iommu)[dev_data->devid]; -amd_iommu_make_clear_dte (dev_data, &new); +amd_iommu_make_clear_dte (iommu, dev_data->devid, &new); old_domid =READ_ONCE (dte->data[1 ]) &DTE_DOMID_MASK ;if (gcr3_info->gcr3_tbl)
@@ -2269,7 +2269,7 @@ static void clear_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_ {struct dev_table_entry new = {}; -amd_iommu_make_clear_dte (dev_data, &new); +amd_iommu_make_clear_dte (iommu, dev_data->devid, &new);amd_iommu_update_dte (iommu, dev_data, &new); }
drivers/iommu/amd/nested.c +1 -1
@@ -200,7 +200,7 @@ static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom,return ; parent = ndom->viommu->parent; -amd_iommu_make_clear_dte (dev_data, new); +amd_iommu_make_clear_dte (iommu, dev_data->devid, new);/* Retrieve the current pagetable info via the IOMMU PT API. */ pt_iommu_amdv1_hw_info (&parent->amdv1, &pt_info);
Signature · 1 line
2.34.1
Re: [PATCH 12/24] iommu/amd: Pass iommu and devid to amd_iommu_make_clear_dte()
Quoted 2 lines · Suravee Suthikulpanit
Take struct
amd_iommuand device ID explicitly instead of deriving the IOMMU from structiommu_dev_data.
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 13/24 iommu/amd: Assign IOMMU Private Address domain to IOMMU
In reply to Suravee SuthikulpanitBy setting the domain ID, pagetable mode, and IOMMU v1 page table in the IOMMU Device Table Entry (DTE) indexed using the device ID of the AMD IOMMU.
--- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/iommu.c | 23 +++++++++++++ drivers/iommu/amd/viommu.c | 51 +++++++++++++++++++++++++++-- 4 files changed, 75 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -0
@@ -57,6 +57,8 @@ extern unsigned long amd_iommu_pgsize_bitmap;extern bool amd_iommu_hatdis;struct iommu_dev_data *amd_iommu_alloc_dev_data (struct amd_iommu *iommu,u16 devid); +void amd_iommu_free_dev_data (struct amd_iommu *iommu, +struct iommu_dev_data *dev_data);/* Protection domain ops */ void amd_iommu_init_identity_domain (void );
drivers/iommu/amd/amd_iommu_types.h +1 -0
@@ -823,6 +823,7 @@ struct amd_iommu {bool gid_ida_inited;/* HW vIOMMU support */ +struct iommu_dev_data *viommu_dev_data;struct protection_domain *viommu_pdom;void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT ]; };
drivers/iommu/amd/iommu.c +23 -0
@@ -406,6 +406,29 @@ struct iommu_dev_data *amd_iommu_alloc_dev_data(struct amd_iommu *iommu, u16 devreturn dev_data; } +void amd_iommu_free_dev_data (struct amd_iommu *iommu, +struct iommu_dev_data *dev_data) +{ +struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; +struct llist_node *prev =NULL , *node; + +if (!dev_data) +return ; + +for (node = pci_seg->dev_data_list.first; node; + prev = node, node = node->next) { +if (node == &dev_data->dev_data_list) { +if (prev) + prev->next = node->next; +else + pci_seg->dev_data_list.first = node->next; +break ; + } + } + +kfree (dev_data); +} +struct iommu_dev_data *search_dev_data (struct amd_iommu *iommu,u16 devid) {struct iommu_dev_data *dev_data;
drivers/iommu/amd/viommu.c +49 -2
83 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH 13/24] iommu/amd: Assign IOMMU Private Address domain to IOMMU
Quoted 8 lines
[ ... 39 lines skipped ... ]
drivers/iommu/amd/iommu.c +7
-0+void amd_iommu_free_dev_data (struct amd_iommu *iommu, +struct iommu_dev_data *dev_data) +{ +struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; +struct llist_node *prev =NULL , *node; + +if (!dev_data)
drivers/iommu/amd/iommu.c struct iommu_dev_data *amd_iommu_alloc_dev_data(struct amd_iommu *iommu, u16 dev · line 425
Quoted 10 lines
drivers/iommu/amd/iommu.c +10
-0+return ; + +for (node = pci_seg->dev_data_list.first; node; + prev = node, node = node->next) { +if (node == &dev_data->dev_data_list) { +if (prev) + prev->next = node->next; +else + pci_seg->dev_data_list.first = node->next; +break ;
Why is this open coding llist manipulation? That stuff is tricky, if you can't do the mutation you want using the existing helpers then you probably shouldn't be using llist..
drivers/iommu/amd/viommu.c u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) · line 319
Quoted 3 lines
[ ... 45 lines skipped ... ]
drivers/iommu/amd/viommu.c +2
-0+static void set_dte_ipa (struct amd_iommu *iommu,struct dev_table_entry *new) +{
This is a strange name for a function that fills a DTE with the paging domain for the "viommu_pdom"
Quoted 31 lines
[ ... 7 lines skipped ... ]
int__initamd_viommu_init(struct amd_iommu *iommu){drivers/iommu/amd/viommu.c +11
-2int ret; +bool dte_set = false; +struct dev_table_entry new = {};if (!amd_iommu_viommu || !check_feature (FEATURE_VIOMMU ))return 0 ; + iommu->viommu_dev_data =amd_iommu_alloc_dev_data (iommu, iommu->devid); +if (!iommu->viommu_dev_data) { +pr_err ("%s: Failed to allocate dev_data\n" , __func__); +return -ENOMEM ; + } + iommu->viommu_dev_data->dev = &iommu->dev->dev; + ret =viommu_init_pci_vsc (iommu);if (ret) -return ret; + goto err_dev_data; ret =viommu_vf_vfcntl_init (iommu);if (ret) -return ret; + goto err_dev_data;amd_viommu_gid_ida_init (iommu);
drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 362
Quoted 8 lines
drivers/iommu/amd/viommu.c +5
-0@@ -318,5 +355,15 @@ int __init amd_viommu_init(struct amd_iommu *iommu)if (ret)return ret; +/* Set DTE for IOMMU device */ +amd_iommu_make_clear_dte (iommu, iommu->devid, &new); +set_dte_ipa (iommu, &new); +amd_iommu_update_dte (iommu, iommu->viommu_dev_data, &new); + dte_set = true;
Why split this so far from the alloc_dev_data() ? The dte set cannot even fail?
If you make the alloc and set one function then the free function viommu_free_self_dev_data() will naturally pair and no need for the weird dte_set
Signature · 1 line
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH 13/24] iommu/amd: Assign IOMMU Private Address domain to IOMMU
Quoted 14 lines · Jason Gunthorpe
Quoted 14 lines
[ ... 39 lines skipped ... ]
drivers/iommu/amd/iommu.c +13
-0+void amd_iommu_free_dev_data (struct amd_iommu *iommu, +struct iommu_dev_data *dev_data) +{ +struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; +struct llist_node *prev =NULL , *node; + +if (!dev_data) +return ; + +for (node = pci_seg->dev_data_list.first; node; + prev = node, node = node->next) { +if (node == &dev_data->dev_data_list) { +if (prev)
Quoted 8 lines
drivers/iommu/amd/iommu.c struct iommu_dev_data *amd_iommu_alloc_dev_data(struct amd_iommu *iommu, u16 dev · line 425
Quoted 4 lines
drivers/iommu/amd/iommu.c +4
-0+ prev->next = node->next; + else + pci_seg->dev_data_list.first = node->next; +break ;Why is this open coding llist manipulation? That stuff is tricky, if you can't do the mutation you want using the existing helpers then you probably shouldn't be using llist..
For v5, I'm replacing the per-segment llist of iommu_dev_data with an xarray keyed by devid so lookup is O(1) and a given id can be removed on teardown.
Quoted 8 lines
drivers/iommu/amd/viommu.c u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) · line 319
Quoted 4 lines
[ ... 45 lines skipped ... ]
drivers/iommu/amd/viommu.c +2
-0+static void set_dte_ipa (struct amd_iommu *iommu,struct dev_table_entry *new) +{This is a strange name for a function that fills a DTE with the paging domain for the "
viommu_pdom"
Removing this in V5.
Quoted 43 lines
Quoted 31 lines
[ ... 7 lines skipped ... ]
int__initamd_viommu_init(struct amd_iommu *iommu){
int ret;drivers/iommu/amd/viommu.c +11
-2+bool dte_set = false; +struct dev_table_entry new = {};if (!amd_iommu_viommu || !check_feature (FEATURE_VIOMMU ))return 0 ; + iommu->viommu_dev_data =amd_iommu_alloc_dev_data (iommu, iommu->devid); +if (!iommu->viommu_dev_data) { +pr_err ("%s: Failed to allocate dev_data\n" , __func__); +return -ENOMEM ; + } + iommu->viommu_dev_data->dev = &iommu->dev->dev; + ret =viommu_init_pci_vsc (iommu);if (ret) -return ret; + goto err_dev_data; ret =viommu_vf_vfcntl_init (iommu);if (ret) -return ret; + goto err_dev_data;amd_viommu_gid_ida_init (iommu);drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 362
Quoted 8 lines
drivers/iommu/amd/viommu.c +5
-0@@ -318,5 +355,15 @@ int __init amd_viommu_init(struct amd_iommu *iommu)if (ret)return ret; +/* Set DTE for IOMMU device */ +amd_iommu_make_clear_dte (iommu, iommu->devid, &new); +set_dte_ipa (iommu, &new); +amd_iommu_update_dte (iommu, iommu->viommu_dev_data, &new); + dte_set = true;Why split this so far from the
alloc_dev_data()? The dte set cannot even fail?
Quoted 3 lines
If you make the alloc and set one function then the free function
viommu_free_self_dev_data()will naturally pair and no need for the weirddte_set
Ok, I'll clean up this part in V5.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 14/24 iommu/amd: Add per-VM private IPA alloc/map helpers
In reply to Suravee SuthikulpanitGuest device ID and guest domain ID tables use dedicated slots in the vIOMMU private address (IPA) region, indexed by guest ID (GID).
Add alloc_private_vm_region() and free_private_vm_region() to allocate backing pages, map them through viommu_pdom, and tear down with an unmap flush on VM destroy.
--- drivers/iommu/amd/viommu.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
drivers/iommu/amd/viommu.c +38 -0
@@ -367,3 +367,41 @@ int __init amd_viommu_init(struct amd_iommu *iommu)viommu_free_self_dev_data (iommu, dte_set);return ret; } + +static int __maybe_unused alloc_private_vm_region (struct amd_iommu *iommu,u64 **entry, +u64 base,size_t size,u16 gid) +{ +int ret; +void *va =NULL ; +u64 addr = base + (gid * size); + + ret =viommu_priv_alloc_map (iommu, addr, size,GFP_KERNEL | __GFP_ZERO, &va); +if (ret) { + *entry =NULL ; +return ret; + } + + *entry = (u64 *)va; + +pr_debug ("%s: entry=%#llx(%#llx), addr=%#llx, size=%#lx\n" , __func__, + (unsigned long long )*entry,iommu_virt_to_phys (*entry), addr, size); + +return 0 ; +} + +static void __maybe_unused free_private_vm_region (struct amd_iommu *iommu,u64 **entry, +u64 base,size_t size,u16 gid) +{ +u64 addr = base + (gid * size); + +if (!iommu || !iommu->viommu_pdom || !*entry) +return ; + +pr_debug ("%s: entry=%#llx(%#llx), base=%#llx, addr=%#llx, size=%#lx\n" , + __func__, (unsigned long long )*entry, +iommu_virt_to_phys (*entry), base, addr, size); + +viommu_priv_unmap_free (iommu, addr, size, *entry); + + *entry =NULL ; +}
Signature · 1 line
2.34.1
Re: [PATCH 14/24] iommu/amd: Add per-VM private IPA alloc/map helpers
Quoted 8 lines · Suravee Suthikulpanit
Guest device ID and guest domain ID tables use dedicated slots in the vIOMMU private address (IPA) region, indexed by guest ID (GID).
Add
alloc_private_vm_region()andfree_private_vm_region()to allocate backing pages, map them throughviommu_pdom, and tear down with an unmap flush on VM destroy.[...]
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 15/24 iommu/amd: Add helper functions to manage DevID / DomID mapping tables
In reply to Suravee SuthikulpanitIntroduce amd_viommu_init_one() and amd_viommu_uninit_one(). These functions are called during IOMMUFD vIOMMU initialize and destroy. Currently, it manages the IPA mapping for Device ID and Domain ID mapping tables.
--- drivers/iommu/amd/amd_iommu_types.h | 3 ++ drivers/iommu/amd/amd_viommu.h | 13 ++++++++ drivers/iommu/amd/iommu.c | 1 + drivers/iommu/amd/iommufd.c | 5 +++ drivers/iommu/amd/viommu.c | 52 +++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -549,6 +549,9 @@ struct amd_iommu_viommu {*/ struct xarray gdomid_array; +u64 *devid_table; +u64 *domid_table; +/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */ unsigned long vfmmio_mmap_offset; };
drivers/iommu/amd/amd_viommu.h +13 -0
@@ -14,6 +14,10 @@ void __init amd_viommu_uninit(struct amd_iommu *iommu);u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid); +int amd_viommu_init_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu); + +void amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu); +#else static inline int amd_viommu_init (struct amd_iommu *iommu)
@@ -30,6 +34,15 @@ static inline u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)return 0 ; } +static inline int amd_viommu_init_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu) +{ +return -EOPNOTSUPP ; +} + +static inline void amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu) +{ +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/iommu.c +1 -0
@@ -43,6 +43,7 @@#include <linux/generic_pt/iommu.h> #include "amd_iommu.h" +#include "amd_viommu.h" #include "iommufd.h" #include "../irq_remapping.h" #include "../iommu-pages.h"
drivers/iommu/amd/iommufd.c +5 -0
@@ -83,6 +83,10 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain */* Reset vIOMMU MMIOs to initialize the vIOMMU */ iommu_reset_vmmio (iommu, aviommu->gid); + ret =amd_viommu_init_one (iommu, aviommu); +if (ret) + goto err_init; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , out_vfmmio_mmap_offset);
@@ -118,6 +122,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)spin_unlock_irqrestore (&pdom->lock, flags);xa_destroy (&aviommu->gdomid_array);iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); +amd_viommu_uninit_one (iommu, aviommu);amd_iommu_gid_free (iommu, aviommu->gid); }
drivers/iommu/amd/viommu.c +52 -0
61 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 15/24] iommu/amd: Add helper functions to manage DevID / DomID mapping tables
Quoted 151 lines · Suravee Suthikulpanit
Introduce
amd_viommu_init_one()andamd_viommu_uninit_one(). These functions are called during IOMMUFD vIOMMU initialize and destroy. Currently, it manages the IPA mapping for Device ID and Domain ID mapping tables.Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 3 ++ drivers/iommu/amd/amd_viommu.h | 13 ++++++++ drivers/iommu/amd/iommu.c | 1 + drivers/iommu/amd/iommufd.c | 5 +++ drivers/iommu/amd/viommu.c | 52 +++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+)drivers/iommu/amd/amd_iommu_types.h +3
-0@@ -549,6 +549,9 @@ struct amd_iommu_viommu {*/ struct xarray gdomid_array; +u64 *devid_table; +u64 *domid_table; +/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */ unsigned long vfmmio_mmap_offset; };drivers/iommu/amd/amd_viommu.h +13
-0@@ -14,6 +14,10 @@ void __init amd_viommu_uninit(struct amd_iommu *iommu);u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid); +int amd_viommu_init_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu); + +void amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu); +#else static inline int amd_viommu_init (struct amd_iommu *iommu)@@ -30,6 +34,15 @@ static inline u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)return 0 ; } +static inline int amd_viommu_init_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu) +{ +return -EOPNOTSUPP ; +} + +static inline void amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu) +{ +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */ drivers/iommu/amd/iommu.c +1
-0@@ -43,6 +43,7 @@#include <linux/generic_pt/iommu.h> #include "amd_iommu.h" +#include "amd_viommu.h" #include "iommufd.h" #include "../irq_remapping.h" #include "../iommu-pages.h" drivers/iommu/amd/iommufd.c +5
-0@@ -83,6 +83,10 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain */* Reset vIOMMU MMIOs to initialize the vIOMMU */ iommu_reset_vmmio (iommu, aviommu->gid); + ret =amd_viommu_init_one (iommu, aviommu); +if (ret) + goto err_init; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , out_vfmmio_mmap_offset);@@ -118,6 +122,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)spin_unlock_irqrestore (&pdom->lock, flags);xa_destroy (&aviommu->gdomid_array);iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); +amd_viommu_uninit_one (iommu, aviommu);amd_iommu_gid_free (iommu, aviommu->gid); }drivers/iommu/amd/viommu.c +42
-0@@ -26,6 +26,20 @@#include "amd_viommu.h" #include "../iommu-pages.h" +/* +* Guest Device ID Mapping Table +*/ +#define VIOMMU_MAX_GDEVID 0xFFFF +#define VIOMMU_DEVID_MAPPING_BASE 0x1000000000ULL +#define VIOMMU_DEVID_MAPPING_ENTRY_SIZE (1 << 20) + +/* +* Guest Domain ID Mapping Table +*/ +#define VIOMMU_MAX_GDOMID 0xFFFF +#define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL +#define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19) +LIST_HEAD (viommu_devid_map);static int viommu_init_pci_vsc (struct amd_iommu *iommu)@@ -405,3 +419,41 @@ static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * *entry =NULL ; } + +void amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *aviommu) +{ +pr_debug ("%s: gid=%u\n" , __func__, aviommu->gid); + +free_private_vm_region (iommu, &aviommu->devid_table, +VIOMMU_DEVID_MAPPING_BASE , +VIOMMU_DEVID_MAPPING_ENTRY_SIZE , + aviommu->gid); +free_private_vm_region (iommu, &aviommu->domid_table, +VIOMMU_DOMID_MAPPING_BASE , +VIOMMU_DOMID_MAPPING_ENTRY_SIZE , + aviommu->gid); +} + +int amd_viommu_init_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu) +{ +int ret; + + ret =alloc_private_vm_region (iommu, &viommu->devid_table, +VIOMMU_DEVID_MAPPING_BASE , +VIOMMU_DEVID_MAPPING_ENTRY_SIZE , + viommu->gid); +if (ret) + goto err_out; + + ret = alloc_private_vm_region(iommu, &viommu->domid_table, + VIOMMU_DOMID_MAPPING_BASE,
drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 459
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+VIOMMU_DOMID_MAPPING_ENTRY_SIZE , + viommu->gid); +if (ret) + goto err_out; + +return 0 ; +err_out: +amd_viommu_uninit_one (iommu, viommu); +return -ENOMEM ; +}
The err_out label overwrites the real error with -ENOMEM. alloc_private_vm_region() (via viommu_priv_alloc_map()) can also return -EINVAL when the pdom is not set up, and that information gets lost on the way to iommufd. Please just "return ret;" here.
Suthikulpanit, Suravee
reviewerRe: [PATCH 15/24] iommu/amd: Add helper functions to manage DevID / DomID mapping tables
Quoted 29 lines · On 8/19/2026 8:30 PM, [email protected] wrote:
Quoted 29 lines
drivers/iommu/amd/viommu.c +1
-0@@ -26,6 +26,20 @@ +int amd_viommu_init_one(struct amd_iommu *iommu, struct
amd_iommu_viommu*viommu)drivers/iommu/amd/viommu.c +19
-0+{ + int ret; + + ret = alloc_private_vm_region(iommu, &viommu->devid_table, + VIOMMU_DEVID_MAPPING_BASE, + VIOMMU_DEVID_MAPPING_ENTRY_SIZE, + viommu->gid); + if (ret) + goto err_out; + + ret = alloc_private_vm_region(iommu, &viommu->domid_table, + VIOMMU_DOMID_MAPPING_BASE, + VIOMMU_DOMID_MAPPING_ENTRY_SIZE, + viommu->gid); + if (ret) + goto err_out; + + return 0; +err_out:
Quoted 8 lines
Quoted 3 lines
+
amd_viommu_uninit_one(iommu, viommu);
+ return-ENOMEM;
+}The
err_outlabel overwrites the real error with-ENOMEM.alloc_private_vm_region()(viaviommu_priv_alloc_map()) can also return-EINVALwhen the pdom is not set up, and that information gets lost on the way to iommufd. Please just "return ret;" here.
Ok. I'll fix this in v5.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 16/24 iommu/amd: Introduce IOMMUFD vDevice support for AMD
In reply to Suravee SuthikulpanitInitialize vDevice for AMD vIOMMU by setting up the Device ID Mapping table using the guest device ID.
--- drivers/iommu/amd/amd_iommu_types.h | 10 ++++++++++ drivers/iommu/amd/iommufd.c | 31 +++++++++++++++++++++++++++++ drivers/iommu/amd/nested.c | 27 ++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 3 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +10 -0
@@ -388,6 +388,11 @@#define DTE_GPT_LEVEL_SHIFT 54#define DTE_GPT_LEVEL_MASK GENMASK_ULL(55, 54) +/* vIOMMU bit fields */ +#define DTE_VIOMMU_EN_SHIFT 15 +#define DTE_VIOMMU_GDEVICEID_MASK GENMASK_ULL(31, 16) +#define DTE_VIOMMU_GUESTID_MASK GENMASK_ULL(47, 32) +#define GCR3_VALID 0x01ULL/* DTE[128:179] | DTE[184:191] */
@@ -895,6 +900,7 @@ struct iommu_dev_data {bool defer_attach;struct ratelimit_state rs;/* Ratelimit IOPF messages */ + };/* Map HPET and IOAPIC ids to the devid used by the IOMMU */
@@ -1122,6 +1128,10 @@ struct amd_irte_ops {void (*clear_allocated)(struct irq_remap_table *,int ); }; +struct amd_iommu_vdevice { +struct iommufd_vdevice core; +}; +#ifdef CONFIG_IRQ_REMAP extern struct amd_irte_ops irte_32_ops;extern struct amd_irte_ops irte_128_ops;
drivers/iommu/amd/iommufd.c +31 -0
@@ -9,6 +9,7 @@#include "amd_iommu.h" #include "amd_viommu.h" #include "amd_iommu_types.h" +#include "../iommufd/iommufd_private.h" static const struct iommufd_viommu_ops amd_viommu_ops;
@@ -126,6 +127,34 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)amd_iommu_gid_free (iommu, aviommu->gid); } +/* +* Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl() +*/ +static int _amd_viommu_vdevice_init (struct iommufd_vdevice *vdev) +{ +struct iommu_dev_data *dev_data; +struct pci_dev *pdev =to_pci_dev (vdev->idev->dev); +struct iommufd_viommu *viommu = vdev->viommu; + struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + +if (!pdev) { +pr_err ("%s: not a PCI device\n" , __func__); +return -EINVAL ; + } + + dev_data =dev_iommu_priv_get (&pdev->dev); +if (!dev_data) { +pr_err ("%s: Device not found (devid=%#x)\n" , + __func__,pci_dev_id (pdev)); +return -EINVAL ; + } + +pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n" , __func__, + aviommu->gid,pci_dev_id (pdev), vdev->virt_id); + +return 0 ; +} +/* * See include/linux/iommufd.h * struct iommufd_viommu_ops - vIOMMU specific operations
@@ -133,4 +162,6 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)static const struct iommufd_viommu_ops amd_viommu_ops = { .alloc_domain_nested = amd_iommu_alloc_domain_nested, .destroy = amd_iommufd_viommu_destroy, + .vdevice_size = VDEVICE_STRUCT_SIZE(struct amd_iommu_vdevice, core), + .vdevice_init = _amd_viommu_vdevice_init, };
drivers/iommu/amd/nested.c +24 -3
@@ -10,6 +10,7 @@#include <uapi/linux/iommufd.h> #include "amd_iommu.h" +#include "amd_viommu.h" static const struct iommu_domain_ops nested_domain_ops;
@@ -183,13 +184,16 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,return ERR_PTR (ret); } -static void set_dte_nested (struct amd_iommu *iommu,struct iommu_domain *dom, -struct iommu_dev_data *dev_data,struct dev_table_entry *new) +static int set_dte_nested (struct amd_iommu *iommu,struct iommu_domain *dom, +struct iommu_dev_data *dev_data,struct dev_table_entry *new) { +int ret; +u16 gid;struct protection_domain *parent;struct nested_domain *ndom =to_ndomain (dom);struct iommu_hwpt_amd_guest *gdte = &ndom->gdte;struct pt_iommu_amdv1_hw_info pt_info; +unsigned long gDevId;/* * The nest parent domain is attached during the call to the
@@ -197,9 +201,15 @@ static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom,* of the struct amd_iommu_viommu.parent. */ if (WARN_ON (!ndom->viommu || !ndom->viommu->parent)) -return ; +return -EINVAL ; + gid = ndom->viommu->gid; parent = ndom->viommu->parent; + + ret =iommufd_viommu_get_vdev_id (&ndom->viommu->core, dev_data->dev, &gDevId); +if (ret) +return ret; +amd_iommu_make_clear_dte (iommu, dev_data->devid, new);/* Retrieve the current pagetable info via the IOMMU PT API. */
@@ -227,6 +237,17 @@ static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom,/* Guest paging mode */ new->data[2 ] |= gdte->dte[2 ] &DTE_GPT_LEVEL_MASK ; + +/* vImuEn */ + new->data[3 ] |=1ULL <<DTE_VIOMMU_EN_SHIFT ; + +/* GDeviceID */ + new->data[3 ] |=FIELD_PREP (DTE_VIOMMU_GDEVICEID_MASK , (u16 )gDevId); + +/* GuestID */ + new->data[3 ] |=FIELD_PREP (DTE_VIOMMU_GUESTID_MASK , gid); + +return 0 ; } static int nested_attach_device(struct iommu_domain *dom, struct device *dev,
Signature · 1 line
2.34.1
Re: [PATCH 16/24] iommu/amd: Introduce IOMMUFD vDevice support for AMD
Quoted 66 lines · Suravee Suthikulpanit
Initialize vDevice for AMD vIOMMU by setting up the Device ID Mapping table using the guest device ID.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 10 ++++++++++ drivers/iommu/amd/iommufd.c | 31 +++++++++++++++++++++++++++++ drivers/iommu/amd/nested.c | 27 ++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 3 deletions(-)drivers/iommu/amd/amd_iommu_types.h +10
-0@@ -388,6 +388,11 @@#define DTE_GPT_LEVEL_SHIFT 54#define DTE_GPT_LEVEL_MASK GENMASK_ULL(55, 54) +/* vIOMMU bit fields */ +#define DTE_VIOMMU_EN_SHIFT 15 +#define DTE_VIOMMU_GDEVICEID_MASK GENMASK_ULL(31, 16) +#define DTE_VIOMMU_GUESTID_MASK GENMASK_ULL(47, 32) +#define GCR3_VALID 0x01ULL/* DTE[128:179] | DTE[184:191] */ @@ -895,6 +900,7 @@ struct iommu_dev_data {bool defer_attach;struct ratelimit_state rs;/* Ratelimit IOPF messages */ + };/* Map HPET and IOAPIC ids to the devid used by the IOMMU */ @@ -1122,6 +1128,10 @@ struct amd_irte_ops {void (*clear_allocated)(struct irq_remap_table *,int ); }; +struct amd_iommu_vdevice { +struct iommufd_vdevice core; +}; +#ifdef CONFIG_IRQ_REMAP extern struct amd_irte_ops irte_32_ops;extern struct amd_irte_ops irte_128_ops;drivers/iommu/amd/iommufd.c +6
-0@@ -9,6 +9,7 @@#include "amd_iommu.h" #include "amd_viommu.h" #include "amd_iommu_types.h" +#include "../iommufd/iommufd_private.h" static const struct iommufd_viommu_ops amd_viommu_ops;@@ -126,6 +127,34 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)amd_iommu_gid_free (iommu, aviommu->gid); } +/* +* Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl() +*/ +static int _amd_viommu_vdevice_init (struct iommufd_vdevice *vdev) +{
drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 144
Quoted 10 lines
drivers/iommu/amd/iommufd.c +10
-0+struct iommu_dev_data *dev_data; +struct pci_dev *pdev =to_pci_dev (vdev->idev->dev); +struct iommufd_viommu *viommu = vdev->viommu; + struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + +if (!pdev) { +pr_err ("%s: not a PCI device\n" , __func__); +return -EINVAL ; + } +
to_pci_dev() is a container_of(), so it can never return NULL and the !pdev check is dead code. Worse, for a non-PCI device it yields a bogus pointer which is then dereferenced through dev_iommu_priv_get() below.
Quoted 89 lines
drivers/iommu/amd/iommufd.c +15
-0+ dev_data =dev_iommu_priv_get (&pdev->dev); +if (!dev_data) { +pr_err ("%s: Device not found (devid=%#x)\n" , + __func__,pci_dev_id (pdev)); +return -EINVAL ; + } + +pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n" , __func__, + aviommu->gid,pci_dev_id (pdev), vdev->virt_id); + +return 0 ; +} +/* * See include/linux/iommufd.h * struct iommufd_viommu_ops - vIOMMU specific operations @@ -133,4 +162,6 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)static const struct iommufd_viommu_ops amd_viommu_ops = { .alloc_domain_nested = amd_iommu_alloc_domain_nested, .destroy = amd_iommufd_viommu_destroy, + .vdevice_size = VDEVICE_STRUCT_SIZE(struct amd_iommu_vdevice, core), + .vdevice_init = _amd_viommu_vdevice_init, };drivers/iommu/amd/nested.c +24
-3@@ -10,6 +10,7 @@#include <uapi/linux/iommufd.h> #include "amd_iommu.h" +#include "amd_viommu.h" static const struct iommu_domain_ops nested_domain_ops;@@ -183,13 +184,16 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,return ERR_PTR (ret); } -static void set_dte_nested (struct amd_iommu *iommu,struct iommu_domain *dom, -struct iommu_dev_data *dev_data,struct dev_table_entry *new) +static int set_dte_nested (struct amd_iommu *iommu,struct iommu_domain *dom, +struct iommu_dev_data *dev_data,struct dev_table_entry *new) { +int ret; +u16 gid;struct protection_domain *parent;struct nested_domain *ndom =to_ndomain (dom);struct iommu_hwpt_amd_guest *gdte = &ndom->gdte;struct pt_iommu_amdv1_hw_info pt_info; +unsigned long gDevId;/* * The nest parent domain is attached during the call to the @@ -197,9 +201,15 @@ static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom,* of the struct amd_iommu_viommu.parent. */ if (WARN_ON (!ndom->viommu || !ndom->viommu->parent)) -return ; +return -EINVAL ; + gid = ndom->viommu->gid; parent = ndom->viommu->parent; + + ret =iommufd_viommu_get_vdev_id (&ndom->viommu->core, dev_data->dev, &gDevId); +if (ret) +return ret; +amd_iommu_make_clear_dte (iommu, dev_data->devid, new);/* Retrieve the current pagetable info via the IOMMU PT API. */ @@ -227,6 +237,17 @@ static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom,/* Guest paging mode */ new->data[2 ] |= gdte->dte[2 ] &DTE_GPT_LEVEL_MASK ; + +/* vImuEn */ + new->data[3 ] |=1ULL <<DTE_VIOMMU_EN_SHIFT ; + +/* GDeviceID */ + new->data[3 ] |=FIELD_PREP (DTE_VIOMMU_GDEVICEID_MASK , (u16 )gDevId); + +/* GuestID */ + new->data[3 ] |=FIELD_PREP (DTE_VIOMMU_GUESTID_MASK , gid); + +return 0 ; } static int nested_attach_device(struct iommu_domain *dom, struct device *dev,
Suthikulpanit, Suravee
reviewerRe: [PATCH 16/24] iommu/amd: Introduce IOMMUFD vDevice support for AMD
Quoted 41 lines · On 8/19/2026 8:34 PM, [email protected] wrote:
Quoted 39 lines · Suravee Suthikulpanit
Initialize vDevice for AMD vIOMMU by setting up the Device ID Mapping table using the guest device ID.
Signed-off-by Suravee Suthikulpanit <[email protected]>---
drivers/iommu/amd/amd_iommu_types.h| 10 ++++++++++ drivers/iommu/amd/iommufd.c | 31 +++++++++++++++++++++++++++++ drivers/iommu/amd/nested.c | 27 ++++++++++++++++++++++---3 files changed, 65 insertions(+), 3 deletions(-)....
drivers/iommu/amd/iommufd.c +12
-0@@ -9,6 +9,7 @@ #include "amd_iommu.h" #include "amd_viommu.h" #include "amd_iommu_types.h" +#include "../iommufd/iommufd_private.h" static const struct iommufd_viommu_ops amd_viommu_ops;@@ -126,6 +127,34 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) amd_iommu_gid_free(iommu, aviommu->gid); } +/* +* Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl() +*/ +static int _amd_viommu_vdevice_init (struct iommufd_vdevice *vdev) +{ + struct iommu_dev_data *dev_data; + struct pci_dev *pdev = to_pci_dev(vdev->idev->dev); + struct iommufd_viommu *viommu = vdev->viommu; + struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + + if (!pdev) {
Quoted 8 lines
drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 144
Quoted 4 lines
drivers/iommu/amd/iommufd.c +4
-0+ pr_err("%s: not a PCI device\n", __func__); + return -EINVAL; + } +
to_pci_dev()is acontainer_of(), so it can never return NULL and the !pdev check is dead code. Worse, for a non-PCI device it yields a bogus pointer which is then dereferenced throughdev_iommu_priv_get()below.
I will fix the code to check dev_is_pci() properly.
Thanks,
Suravee
Jason Gunthorpe
maintainerRe: [PATCH 16/24] iommu/amd: Introduce IOMMUFD vDevice support for AMD
In reply to Suravee SuthikulpanitQuoted 15 lines
[ ... 56 lines skipped ... ]
drivers/iommu/amd/iommufd.c +14
-0+/* +* Called from drivers/iommu/iommufd/viommu.c: iommufd_vdevice_alloc_ioctl() +*/ +static int _amd_viommu_vdevice_init (struct iommufd_vdevice *vdev) +{ +struct iommu_dev_data *dev_data; +struct pci_dev *pdev =to_pci_dev (vdev->idev->dev); +struct iommufd_viommu *viommu = vdev->viommu; + struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + +if (!pdev) { +pr_err ("%s: not a PCI device\n" , __func__); +return -EINVAL ; + }
drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 153
Quoted 10 lines
drivers/iommu/amd/iommufd.c +10
-0+ + dev_data =dev_iommu_priv_get (&pdev->dev); +if (!dev_data) { +pr_err ("%s: Device not found (devid=%#x)\n" , + __func__,pci_dev_id (pdev)); +return -EINVAL ; + } + +pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n" , __func__, + aviommu->gid,pci_dev_id (pdev), vdev->virt_id);
What is the point of this function? It doesn't do anything, and you cannot have a vdevice that isn't already bound to the iommu. Please don't add random !dev_data checks where it is impossible
drivers/iommu/amd/nested.c static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom, · line 245
Quoted 7 lines
[ ... 66 lines skipped ... ]
drivers/iommu/amd/nested.c +6
-0+ +/* vImuEn */ + new->data[3 ] |=1ULL <<DTE_VIOMMU_EN_SHIFT ; + +/* GDeviceID */ + new->data[3 ] |=FIELD_PREP (DTE_VIOMMU_GDEVICEID_MASK , (u16 )gDevId);
No need for a type cast?
drivers/iommu/amd/nested.c static void set_dte_nested(struct amd_iommu *iommu, struct iommu_domain *dom, · line 248
Quoted 3 lines
drivers/iommu/amd/nested.c +3
-0+ +/* GuestID */ + new->data[3 ] |=FIELD_PREP (DTE_VIOMMU_GUESTID_MASK , gid);
The comments are a little redundant with the field names :)
But the dte construction flow looks OK now
Signature · 1 line
Jason
Suravee Suthikulpanit
reviewerPATCH v4 17/24 iommu/amd: Introduce helper function for updating domain ID mapping table
In reply to Suravee SuthikulpanitAMD vIOMMU hardware uses the Domain ID mapping table to map Guest Domain ID (GDomID) to Host Domain ID when it virtualises guest IOMMU commands. It uses GID and GDomID to index into the table to look up host domain ID.
Linux IOMMU driver programs the table entry using VFCntlMMIO Guest Domain Map Control Register.
Introduce amd_viommu_domain_id_update(), which is used to set the entry when attaching the nested device. Clearing the entry is done during VM destroy.
--- drivers/iommu/amd/amd_viommu.h | 2 ++ drivers/iommu/amd/nested.c | 9 ++++++- drivers/iommu/amd/viommu.c | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_viommu.h +2 -0
@@ -18,6 +18,8 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommuvoid amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu); +int amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid, +u16 hdom_id,u16 gdom_id);#else static inline int amd_viommu_init (struct amd_iommu *iommu)
drivers/iommu/amd/nested.c +8 -1
@@ -254,6 +254,7 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev, struct iommu_domain *old) {struct dev_table_entry new = {0 }; +struct nested_domain *ndom =to_ndomain (dom);struct iommu_dev_data *dev_data =dev_iommu_priv_get (dev);struct amd_iommu *iommu =get_amd_iommu_from_dev_data (dev_data);int ret =0 ;
@@ -267,10 +268,16 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev,mutex_lock (&dev_data->mutex); -set_dte_nested (iommu, dom, dev_data, &new); + ret =set_dte_nested (iommu, dom, dev_data, &new); +if (ret) + goto out_err;amd_iommu_update_dte (iommu, dev_data, &new); + ret =amd_viommu_domain_id_update (iommu, ndom->viommu->gid, + ndom->gdom_info->hdom_id, ndom->gdom_id); + +out_err:mutex_unlock (&dev_data->mutex);return ret;
drivers/iommu/amd/viommu.c +45 -0
71 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 17/24] iommu/amd: Introduce helper function for updating domain ID mapping table
Quoted 98 lines · Suravee Suthikulpanit
AMD vIOMMU hardware uses the Domain ID mapping table to map Guest Domain ID (GDomID) to Host Domain ID when it virtualises guest IOMMU commands. It uses GID and GDomID to index into the table to look up host domain ID.
Linux IOMMU driver programs the table entry using VFCntlMMIO Guest Domain Map Control Register.
Introduce
amd_viommu_domain_id_update(), which is used to set the entry when attaching the nested device. Clearing the entry is done during VM destroy.Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_viommu.h | 2 ++ drivers/iommu/amd/nested.c | 9 ++++++- drivers/iommu/amd/viommu.c | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-)drivers/iommu/amd/amd_viommu.h +2
-0@@ -18,6 +18,8 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommuvoid amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu); +int amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid, +u16 hdom_id,u16 gdom_id);#else static inline int amd_viommu_init (struct amd_iommu *iommu)drivers/iommu/amd/nested.c +8
-1@@ -254,6 +254,7 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev, struct iommu_domain *old) {struct dev_table_entry new = {0 }; +struct nested_domain *ndom =to_ndomain (dom);struct iommu_dev_data *dev_data =dev_iommu_priv_get (dev);struct amd_iommu *iommu =get_amd_iommu_from_dev_data (dev_data);int ret =0 ;@@ -267,10 +268,16 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev,mutex_lock (&dev_data->mutex); -set_dte_nested (iommu, dom, dev_data, &new); + ret =set_dte_nested (iommu, dom, dev_data, &new); +if (ret) + goto out_err;amd_iommu_update_dte (iommu, dev_data, &new); + ret =amd_viommu_domain_id_update (iommu, ndom->viommu->gid, + ndom->gdom_info->hdom_id, ndom->gdom_id); + +out_err:mutex_unlock (&dev_data->mutex);return ret;drivers/iommu/amd/viommu.c +18
-0@@ -40,6 +40,8 @@#define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL#define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19) +#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET 0x08 +LIST_HEAD (viommu_devid_map);static int viommu_init_pci_vsc (struct amd_iommu *iommu)@@ -420,6 +422,22 @@ static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * *entry =NULL ; } +static void viommu_clear_mapping (struct amd_iommu *iommu, +struct amd_iommu_viommu *aviommu) +{ +int i; +u16 gid = aviommu->gid; + +/* +* IOMMU hardware uses the domain ID mapping table to map gdom ID to hdom ID. +* If the mapping does not exist, the hardware would generate error in the event log. +* Therefore, initialize all gdom ID entries to map to parent domain ID to prevent +* unknown mapping scenario. +*/ +for (i =0 ; i <=VIOMMU_MAX_GDOMID ; i++) +amd_viommu_domain_id_update (iommu, gid, aviommu->parent->id, i); +} +void amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *aviommu) {pr_debug ("%s: gid=%u\n" , __func__, aviommu->gid);
drivers/iommu/amd/viommu.c void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *avi · line 454
Quoted 5 lines
drivers/iommu/amd/viommu.c +1
-0@@ -432,6 +450,7 @@ void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *aviVIOMMU_DOMID_MAPPING_BASE ,VIOMMU_DOMID_MAPPING_ENTRY_SIZE , aviommu->gid); +viommu_clear_mapping (iommu, aviommu); }
Use-after-free in vIOMMU teardown ordering
The teardown path releases the DevID/DomID mapping table memory BEFORE resetting the vIOMMU state:
free_private_vm_region(iommu, &aviommu->devid_table, ...); free_private_vm_region(iommu, &aviommu->domid_table, ...);
...
viommu_clear_mapping(iommu, aviommu); /* too late */
Since the IOMMU hardware may still reference these tables until the guest vIOMMU state is reset, freeing the backing memory first can result in a use-after-free by hardware.
Quoted 37 lines
drivers/iommu/amd/viommu.c +26
-0int amd_viommu_init_one (struct amd_iommu *iommu,struct amd_iommu_viommu *viommu)@@ -452,8 +471,34 @@ int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommuif (ret) goto err_out; +viommu_clear_mapping (iommu, viommu); +return 0 ; err_out:amd_viommu_uninit_one (iommu, viommu);return -ENOMEM ; } + +/* +* Program the DomID via VFCTRL registers +* This function will be called during VM init via VFIO. +*/ + +#define DOMID_ENTRY_GDOMID_MASK GENMASK_ULL(61, 46) +#define DOMID_ENTRY_HDOMID_MASK GENMASK_ULL(29, 14) +#define DOMID_ENTRY_VALID BIT_ULL(0) +#define DOMID_ENTRY_WRITE BIT_ULL(63) + +int amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid, +u16 hdom_id,u16 gdom_id) +{ +u64 val; +u8 __iomem *vfctrl =VIOMMU_VFCTRL_MMIO_BASE (iommu, gid); + + val =FIELD_PREP (DOMID_ENTRY_GDOMID_MASK , gdom_id) | +FIELD_PREP (DOMID_ENTRY_HDOMID_MASK , hdom_id) | +DOMID_ENTRY_WRITE |DOMID_ENTRY_VALID ; + +writeq (val, vfctrl +VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET ); +return 0 ; +}
Suthikulpanit, Suravee
reviewerRe: [PATCH 17/24] iommu/amd: Introduce helper function for updating domain ID mapping table
Quoted 49 lines · On 8/19/2026 5:45 PM, [email protected] wrote:
Quoted 38 lines
drivers/iommu/amd/viommu.c +2
-0@@ -40,6 +40,8 @@ #define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL #define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19) +#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET 0x08 + LIST_HEAD(viommu_devid_map); static int viommu_init_pci_vsc(struct amd_iommu *iommu)@@ -420,6 +422,22 @@ static void __maybe_unusedfree_private_vm_region(struct
amd_iommu*iommu, u64 * *entry = NULL;
}drivers/iommu/amd/viommu.c +8
-0+static void viommu_clear_mapping (struct amd_iommu *iommu, + struct amd_iommu_viommu *aviommu) +{ + int i; + u16 gid = aviommu->gid; + + /* +* IOMMU hardware uses the domain ID mapping table to map gdom ID to hdom ID.
+ * If the mapping does not exist, the hardware would generate error in the event log.
+ * Therefore, initialize all gdom ID entries to map to parent domain ID to prevent
+ * unknown mapping scenario.
+ */
+ for (i = 0; i <=VIOMMU_MAX_GDOMID; i++)
+amd_viommu_domain_id_update(iommu, gid, aviommu->parent->id, i);drivers/iommu/amd/viommu.c +2
-0+} +void
amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *aviommu){
pr_debug("%s: gid=%u\n", __func__, aviommu->gid);drivers/iommu/amd/viommu.c void amd_viommu_uninit_one(struct amd_iommu · line 455
Quoted 6 lines
drivers/iommu/amd/viommu.c +1
-0@@ -432,6 +450,7 @@ void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *avi VIOMMU_DOMID_MAPPING_BASE, VIOMMU_DOMID_MAPPING_ENTRY_SIZE, aviommu->gid); + viommu_clear_mapping(iommu, aviommu); }Use-after-free in vIOMMU teardown ordering
The teardown path releases the DevID/DomID mapping table memory BEFORE resetting the vIOMMU state:
Quoted 8 lines
free_private_vm_region(iommu,
&aviommu->devid_table, ...);free_private_vm_region(iommu, &aviommu->domid_table, ...);
...
viommu_clear_mapping(iommu, aviommu); /* too late */Since the IOMMU hardware may still reference these tables until the guest vIOMMU state is reset, freeing the backing memory first can result in a use-after-free by hardware.
Thanks. Fixing in v5.
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 18/24 iommu/amd: Introduce helper function for updating device ID mapping table
In reply to Suravee SuthikulpanitAMD vIOMMU hardware uses the Device ID mapping table to map Guest Device ID (GDevID) to Host Device ID when it virtualises guest IOMMU commands. It uses GID and GDevID to indexe into the table to look up host device ID.
Linux IOMMU driver programs the table entry using VFCntlMMIO Guest Device Map Control Register.
Introduce amd_viommu_set/clear_device_mapping(), which are used to set the entry when initialize the IOMMUFD vDevice. Clearing the entry is done during VM destroy.
--- drivers/iommu/amd/amd_viommu.h | 10 +++++++ drivers/iommu/amd/iommufd.c | 9 ++++++ drivers/iommu/amd/viommu.c | 53 +++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_viommu.h +10 -0
@@ -6,6 +6,8 @@#ifndef AMD_VIOMMU_H #define AMD_VIOMMU_H +#define VIOMMU_MAX_GDEVID 0xFFFF +#if IS_ENABLED (CONFIG_AMD_IOMMU_IOMMUFD )int amd_viommu_init (struct amd_iommu *iommu);
@@ -20,6 +22,9 @@ void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *vioint amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid,u16 hdom_id,u16 gdom_id); + +void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId);#else static inline int amd_viommu_init (struct amd_iommu *iommu)
@@ -45,6 +50,11 @@ static inline void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iom { } +static inline void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId) +{ +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/iommufd.c +9 -0
@@ -136,12 +136,18 @@ static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)struct pci_dev *pdev =to_pci_dev (vdev->idev->dev);struct iommufd_viommu *viommu = vdev->viommu; struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);if (!pdev) {pr_err ("%s: not a PCI device\n" , __func__);return -EINVAL ; } +if (vdev->virt_id >VIOMMU_MAX_GDEVID ) { +pr_err ("%s: Invalid virtual ID (virt_id=%#llx)\n" , __func__, vdev->virt_id); +return -EINVAL ; + } + dev_data =dev_iommu_priv_get (&pdev->dev);if (!dev_data) { pr_err("%s: Device not found (devid=%#x)\n",
@@ -152,6 +158,9 @@ static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n" , __func__, aviommu->gid,pci_dev_id (pdev), vdev->virt_id); +amd_viommu_set_device_mapping (iommu,pci_dev_id (pdev), aviommu->gid, + (u16 )vdev->virt_id); +return 0 ; }
drivers/iommu/amd/viommu.c +52 -1
77 lines not on this page · raw message
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH 18/24] iommu/amd: Introduce helper function for updating device ID mapping table
Quoted 111 lines · Suravee Suthikulpanit
AMD vIOMMU hardware uses the Device ID mapping table to map Guest Device ID (GDevID) to Host Device ID when it virtualises guest IOMMU commands. It uses GID and GDevID to indexe into the table to look up host device ID.
Linux IOMMU driver programs the table entry using VFCntlMMIO Guest Device Map Control Register.
Introduce amd_viommu_set/clear_device_mapping(), which are used to set the entry when initialize the IOMMUFD vDevice. Clearing the entry is done during VM destroy.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_viommu.h | 10 +++++++ drivers/iommu/amd/iommufd.c | 9 ++++++ drivers/iommu/amd/viommu.c | 53 +++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-)drivers/iommu/amd/amd_viommu.h +10
-0@@ -6,6 +6,8 @@#ifndef AMD_VIOMMU_H #define AMD_VIOMMU_H +#define VIOMMU_MAX_GDEVID 0xFFFF +#if IS_ENABLED (CONFIG_AMD_IOMMU_IOMMUFD )int amd_viommu_init (struct amd_iommu *iommu);@@ -20,6 +22,9 @@ void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *vioint amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid,u16 hdom_id,u16 gdom_id); + +void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId);#else static inline int amd_viommu_init (struct amd_iommu *iommu)@@ -45,6 +50,11 @@ static inline void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iom { } +static inline void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId) +{ +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */ drivers/iommu/amd/iommufd.c +9
-0@@ -136,12 +136,18 @@ static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)struct pci_dev *pdev =to_pci_dev (vdev->idev->dev);struct iommufd_viommu *viommu = vdev->viommu; struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);if (!pdev) {pr_err ("%s: not a PCI device\n" , __func__);return -EINVAL ; } +if (vdev->virt_id >VIOMMU_MAX_GDEVID ) { +pr_err ("%s: Invalid virtual ID (virt_id=%#llx)\n" , __func__, vdev->virt_id); +return -EINVAL ; + } + dev_data =dev_iommu_priv_get (&pdev->dev);if (!dev_data) { pr_err("%s: Device not found (devid=%#x)\n",@@ -152,6 +158,9 @@ static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n" , __func__, aviommu->gid,pci_dev_id (pdev), vdev->virt_id); +amd_viommu_set_device_mapping (iommu,pci_dev_id (pdev), aviommu->gid, + (u16 )vdev->virt_id); +return 0 ; }drivers/iommu/amd/viommu.c +2
-1@@ -29,7 +29,6 @@/* * Guest Device ID Mapping Table */ -#define VIOMMU_MAX_GDEVID 0xFFFF#define VIOMMU_DEVID_MAPPING_BASE 0x1000000000ULL#define VIOMMU_DEVID_MAPPING_ENTRY_SIZE (1 << 20)@@ -40,6 +39,7 @@#define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL#define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19) +#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET 0x00#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET 0x08LIST_HEAD (viommu_devid_map);@@ -422,6 +422,53 @@ static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * *entry =NULL ; } +#define DEVID_ENTRY_GDEVID_MASK GENMASK_ULL(61, 46)
drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 435
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+#define DEVID_ENTRY_HDEVID_MASK GENMASK_ULL(29, 14) +#define DEVID_ENTRY_WRITE BIT_ULL(63) +#define DEVID_ENTRY_VALID BIT_ULL(0) + +/* +* Program the DevID via VFCTRL registers +* This function will be called during VM init via VFIO. +*/ +void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId)
Can we avoid CamelCase?
Quoted 21 lines
drivers/iommu/amd/viommu.c +21
-0+{ +u64 val; +u8 __iomem *vfctrl; + +pr_debug ("%s: iommu_devid=%#x, gid=%#x, hDevId=%#x, gDevId=%#x\n" , + __func__,pci_dev_id (iommu->dev), guestId, hDevId, gDevId); + + val =FIELD_PREP (DEVID_ENTRY_GDEVID_MASK , gDevId) | +FIELD_PREP (DEVID_ENTRY_HDEVID_MASK , hDevId) | +DEVID_ENTRY_WRITE |DEVID_ENTRY_VALID ; + + vfctrl =VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId); + +writeq (val, vfctrl +VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET ); +} + +/* +* Clear the DevID via VFCTRL registers +* This function will be called during VM destroy via VFIO. +*/ +static void clear_device_mapping (struct amd_iommu *iommu,u16 guestId,u16 gDevId)
drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 466
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+{ +u64 val; +u8 __iomem *vfctrl; + +/* +* Clear the DevID in VFCTRL registers +*/ + val =FIELD_PREP (DEVID_ENTRY_GDEVID_MASK , gDevId) | +FIELD_PREP (DEVID_ENTRY_HDEVID_MASK ,0 ) | +DEVID_ENTRY_WRITE |DEVID_ENTRY_VALID ;
May be just call amd_viommu_set_device_mapping() with appropriate variables/flags?
-Vasant
Suthikulpanit, Suravee
reviewerRe: [PATCH 18/24] iommu/amd: Introduce helper function for updating device ID mapping table
Quoted 28 lines · Vasant Hegde
Quoted 25 lines · Suravee Suthikulpanit
...
drivers/iommu/amd/viommu.c +1
-1@@ -29,7 +29,6 @@/* * Guest Device ID Mapping Table */ -#define VIOMMU_MAX_GDEVID 0xFFFF#define VIOMMU_DEVID_MAPPING_BASE 0x1000000000ULL#define VIOMMU_DEVID_MAPPING_ENTRY_SIZE (1 << 20)@@ -40,6 +39,7 @@#define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL#define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19) +#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET 0x00#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET 0x08LIST_HEAD (viommu_devid_map);@@ -422,6 +422,53 @@ static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * *entry =NULL ; }
Quoted 14 lines
drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 435
Quoted 11 lines
drivers/iommu/amd/viommu.c +11
-0+#define DEVID_ENTRY_GDEVID_MASK GENMASK_ULL(61, 46) +#define DEVID_ENTRY_HDEVID_MASK GENMASK_ULL(29, 14) +#define DEVID_ENTRY_WRITE BIT_ULL(63) +#define DEVID_ENTRY_VALID BIT_ULL(0) + +/* +* Program the DevID via VFCTRL registers +* This function will be called during VM init via VFIO. +*/ +void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId)Can we avoid CamelCase?
OK.
Quoted 25 lines
Quoted 25 lines
drivers/iommu/amd/viommu.c +25
-0+{ +u64 val; +u8 __iomem *vfctrl; + +pr_debug ("%s: iommu_devid=%#x, gid=%#x, hDevId=%#x, gDevId=%#x\n" , + __func__,pci_dev_id (iommu->dev), guestId, hDevId, gDevId); + + val =FIELD_PREP (DEVID_ENTRY_GDEVID_MASK , gDevId) | +FIELD_PREP (DEVID_ENTRY_HDEVID_MASK , hDevId) | +DEVID_ENTRY_WRITE |DEVID_ENTRY_VALID ; + + vfctrl =VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId); + +writeq (val, vfctrl +VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET ); +} + +/* +* Clear the DevID via VFCTRL registers +* This function will be called during VM destroy via VFIO. +*/ +static void clear_device_mapping (struct amd_iommu *iommu,u16 guestId,u16 gDevId) +{ +u64 val; +u8 __iomem *vfctrl; +
Quoted 8 lines
drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 466
Quoted 6 lines
drivers/iommu/amd/viommu.c +6
-0+/* +* Clear the DevID in VFCTRL registers +*/ + val =FIELD_PREP (DEVID_ENTRY_GDEVID_MASK , gDevId) | +FIELD_PREP (DEVID_ENTRY_HDEVID_MASK ,0 ) | +DEVID_ENTRY_WRITE |DEVID_ENTRY_VALID ;May be just call
amd_viommu_set_device_mapping()with appropriate variables/flags?
Ok. I'll update this in V5.
Thanks,
Suravee
Re: [PATCH 18/24] iommu/amd: Introduce helper function for updating device ID mapping table
In reply to Suravee SuthikulpanitQuoted 146 lines · Suravee Suthikulpanit
AMD vIOMMU hardware uses the Device ID mapping table to map Guest Device ID (GDevID) to Host Device ID when it virtualises guest IOMMU commands. It uses GID and GDevID to indexe into the table to look up host device ID.
Linux IOMMU driver programs the table entry using VFCntlMMIO Guest Device Map Control Register.
Introduce amd_viommu_set/clear_device_mapping(), which are used to set the entry when initialize the IOMMUFD vDevice. Clearing the entry is done during VM destroy.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_viommu.h | 10 +++++++ drivers/iommu/amd/iommufd.c | 9 ++++++ drivers/iommu/amd/viommu.c | 53 +++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-)drivers/iommu/amd/amd_viommu.h +10
-0@@ -6,6 +6,8 @@#ifndef AMD_VIOMMU_H #define AMD_VIOMMU_H +#define VIOMMU_MAX_GDEVID 0xFFFF +#if IS_ENABLED (CONFIG_AMD_IOMMU_IOMMUFD )int amd_viommu_init (struct amd_iommu *iommu);@@ -20,6 +22,9 @@ void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *vioint amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid,u16 hdom_id,u16 gdom_id); + +void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId);#else static inline int amd_viommu_init (struct amd_iommu *iommu)@@ -45,6 +50,11 @@ static inline void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iom { } +static inline void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId) +{ +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */ drivers/iommu/amd/iommufd.c +9
-0@@ -136,12 +136,18 @@ static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)struct pci_dev *pdev =to_pci_dev (vdev->idev->dev);struct iommufd_viommu *viommu = vdev->viommu; struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);if (!pdev) {pr_err ("%s: not a PCI device\n" , __func__);return -EINVAL ; } +if (vdev->virt_id >VIOMMU_MAX_GDEVID ) { +pr_err ("%s: Invalid virtual ID (virt_id=%#llx)\n" , __func__, vdev->virt_id); +return -EINVAL ; + } + dev_data =dev_iommu_priv_get (&pdev->dev);if (!dev_data) { pr_err("%s: Device not found (devid=%#x)\n",@@ -152,6 +158,9 @@ static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#llx\n" , __func__, aviommu->gid,pci_dev_id (pdev), vdev->virt_id); +amd_viommu_set_device_mapping (iommu,pci_dev_id (pdev), aviommu->gid, + (u16 )vdev->virt_id); +return 0 ; }drivers/iommu/amd/viommu.c +37
-1@@ -29,7 +29,6 @@/* * Guest Device ID Mapping Table */ -#define VIOMMU_MAX_GDEVID 0xFFFF#define VIOMMU_DEVID_MAPPING_BASE 0x1000000000ULL#define VIOMMU_DEVID_MAPPING_ENTRY_SIZE (1 << 20)@@ -40,6 +39,7 @@#define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL#define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19) +#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET 0x00#define VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET 0x08LIST_HEAD (viommu_devid_map);@@ -422,6 +422,53 @@ static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * *entry =NULL ; } +#define DEVID_ENTRY_GDEVID_MASK GENMASK_ULL(61, 46) +#define DEVID_ENTRY_HDEVID_MASK GENMASK_ULL(29, 14) +#define DEVID_ENTRY_WRITE BIT_ULL(63) +#define DEVID_ENTRY_VALID BIT_ULL(0) + +/* +* Program the DevID via VFCTRL registers +* This function will be called during VM init via VFIO. +*/ +void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId, +u16 guestId,u16 gDevId) +{ +u64 val; +u8 __iomem *vfctrl; + +pr_debug ("%s: iommu_devid=%#x, gid=%#x, hDevId=%#x, gDevId=%#x\n" , + __func__,pci_dev_id (iommu->dev), guestId, hDevId, gDevId); + + val =FIELD_PREP (DEVID_ENTRY_GDEVID_MASK , gDevId) | +FIELD_PREP (DEVID_ENTRY_HDEVID_MASK , hDevId) | +DEVID_ENTRY_WRITE |DEVID_ENTRY_VALID ; + + vfctrl =VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId); + +writeq (val, vfctrl +VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET ); +} + +/* +* Clear the DevID via VFCTRL registers +* This function will be called during VM destroy via VFIO. +*/ +static void clear_device_mapping (struct amd_iommu *iommu,u16 guestId,u16 gDevId) +{ +u64 val; +u8 __iomem *vfctrl; +
drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 470
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+/* +* Clear the DevID in VFCTRL registers +*/ + val =FIELD_PREP (DEVID_ENTRY_GDEVID_MASK , gDevId) | +FIELD_PREP (DEVID_ENTRY_HDEVID_MASK ,0 ) | +DEVID_ENTRY_WRITE |DEVID_ENTRY_VALID ; + + vfctrl =VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId); +writeq (val, vfctrl +VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET ); +}
The clear path sets Vld=1 with hDevID=0. This semantics is ambiguous:
Vld=1 marks the entry as a *valid* mapping, and hDevID=0 is a legal PCI
BDF (Bus 0, Dev 0, Func 0), instead of treating the entry as invalid. To actually invalidate the mapping, please use Vld=0
val = FIELD_PREP(DEVID_ENTRY_GDEVID_MASK, gDevId) |
FIELD_PREP(DEVID_ENTRY_HDEVID_MASK, 0) | DEVID_ENTRY_WRITE;
Alternatively, during the clear dev mapping process, you might directly specify the original hdevid being used.
Quoted 15 lines
drivers/iommu/amd/viommu.c +5
-0+static void viommu_clear_mapping (struct amd_iommu *iommu,struct amd_iommu_viommu *aviommu) {@@ -436,6 +483,10 @@ static void viommu_clear_mapping(struct amd_iommu *iommu,*/ for (i =0 ; i <=VIOMMU_MAX_GDOMID ; i++)amd_viommu_domain_id_update (iommu, gid, aviommu->parent->id, i); + +for (i =0 ; i <=VIOMMU_MAX_GDEVID ; i++) +clear_device_mapping (iommu, gid, i); + }void amd_viommu_uninit_one (struct amd_iommu *iommu,struct amd_iommu_viommu *aviommu)
Suthikulpanit, Suravee
reviewerRe: [PATCH 18/24] iommu/amd: Introduce helper function for updating device ID mapping table
Quoted 23 lines · On 8/19/2026 5:26 PM, [email protected] wrote:
Quoted 6 lines
+static void
clear_device_mapping(struct amd_iommu *iommu, u16 guestId, u16 gDevId)drivers/iommu/amd/viommu.c +4
-0+{ + u64 val; + u8 __iomem *vfctrl; +drivers/iommu/amd/viommu.c static void __maybe_unused free_private_vm_region(struct amd_iommu *iommu, u64 * · line 470
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+ /* + * Clear the DevID in VFCTRL registers + */ + val = FIELD_PREP(DEVID_ENTRY_GDEVID_MASK, gDevId) | + FIELD_PREP(DEVID_ENTRY_HDEVID_MASK, 0) | + DEVID_ENTRY_WRITE | DEVID_ENTRY_VALID; + + vfctrl = VIOMMU_VFCTRL_MMIO_BASE(iommu, guestId); + writeq(val, vfctrl + VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL0_OFFSET); +}The clear path sets Vld=1 with hDevID=0. This semantics is ambiguous:
Vld=1 marks the entry as a *valid* mapping, and hDevID=0 is a legal PCI
BDF (Bus 0, Dev 0, Func 0), instead of treating the entry as invalid. To actually invalidate the mapping, please use Vld=0
Quoted 5 lines
val =
FIELD_PREP(DEVID_ENTRY_GDEVID_MASK, gDevId)|FIELD_PREP(DEVID_ENTRY_HDEVID_MASK, 0)|DEVID_ENTRY_WRITE;Alternatively, during the clear dev mapping process, you might directly specify the original hdevid being used.
Based on the IOMMU implementation, it requires the valid bit to be set to 1. Otherwise, guest command INVALIDATE_IOMMU_ALL would trigger error ILLEGAL_COMMAND_ERROR event.
Generally, host device id BDF 0:00.0 is the host bridge, which is not part of the list of devices managed by IOMMU. So, it is safe.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 19/24 iommu/amd: Add per-segment translate device ID pool
In reply to Suravee SuthikulpanitTrack translate-device-id slots per PCI segment so real PCI device IDs can be reserved for normal DTE programming and excluded from dynamic allocation for vIOMMU translation DTEs. The pool is per-segment because the AMD IOMMU device table is per-segment.
Add amd_iommu_pci_seg_trans_devid_init/fini() during segment setup and teardown, amd_iommu_trans_devid_reserve() for attach-time reservation, and trans_devid.c implementing the xarray-backed pool. Allocated slots are recorded as pointers to the owning vIOMMU once allocation is added.
Call the reserve hook from amd_iommu_attach_device() before programming the DTE.
--- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 12 +++++ drivers/iommu/amd/amd_iommu_types.h | 17 +++++++ drivers/iommu/amd/init.c | 3 ++ drivers/iommu/amd/iommu.c | 12 +++++ drivers/iommu/amd/trans_devid.c | 78 +++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/amd/trans_devid.c
drivers/iommu/amd/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += iommu.o init.o quirks.o ppr.o pasid.o -obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o +obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o trans_devid.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu.h +12 -0
@@ -218,6 +218,18 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, struct dev_table_entry *new);int amd_iommu_completion_wait (struct amd_iommu *iommu); +/* Per-segment translate-device-id pool (CONFIG_AMD_IOMMU_IOMMUFD) */ +#ifdef CONFIG_AMD_IOMMU_IOMMUFD +void amd_iommu_pci_seg_trans_devid_init (struct amd_iommu_pci_seg *pci_seg); +void amd_iommu_pci_seg_trans_devid_fini (struct amd_iommu_pci_seg *pci_seg); +int amd_iommu_trans_devid_reserve (struct amd_iommu_pci_seg *pci_seg,u16 id); +#else +static inline void +amd_iommu_pci_seg_trans_devid_init (struct amd_iommu_pci_seg *pci_seg) { } +static inline void +amd_iommu_pci_seg_trans_devid_fini (struct amd_iommu_pci_seg *pci_seg) { } +#endif +static inline void amd_iommu_make_clear_dte (struct amd_iommu *iommu,u16 devid,struct dev_table_entry *new)
drivers/iommu/amd/amd_iommu_types.h +17 -0
@@ -612,6 +612,13 @@ PT_IOMMU_CHECK_DOMAIN(struct protection_domain, iommu, domain); PT_IOMMU_CHECK_DOMAIN(struct protection_domain, amdv1.iommu, domain); PT_IOMMU_CHECK_DOMAIN(struct protection_domain, amdv2.iommu, domain); +#ifdef CONFIG_AMD_IOMMU_IOMMUFD +enum trans_devid_state { +TRANS_DEVID_FREE =0 , +TRANS_DEVID_RESERVED , +}; +#endif +/* * This structure contains information about one PCI segment in the system. */
@@ -673,6 +680,16 @@ struct amd_iommu_pci_seg {* parsing time. */ struct list_head unity_map; + +#ifdef CONFIG_AMD_IOMMU_IOMMUFD +/* +* Per-segment translate-device-id pool indexed by id. Entries are: +* absent (FREE), xa value TRANS_DEVID_RESERVED, or a pointer to the +* owning struct amd_iommu_viommu (ALLOCATED). +*/ +struct mutex trans_devid_mutex; +struct xarray trans_devid_xa; +#endif };/*
drivers/iommu/amd/init.c +3 -0
@@ -1737,6 +1737,8 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,if (alloc_rlookup_table (pci_seg)) goto err_free_alias_table; +amd_iommu_pci_seg_trans_devid_init (pci_seg); +return pci_seg; err_free_alias_table:
@@ -1768,6 +1770,7 @@ static void __init free_pci_segments(void)for_each_pci_segment_safe (pci_seg, next) {list_del (&pci_seg->list); +amd_iommu_pci_seg_trans_devid_fini (pci_seg);free_irq_lookup_table (pci_seg);free_rlookup_table (pci_seg);free_alias_table (pci_seg);
drivers/iommu/amd/iommu.c +12 -0
@@ -3069,6 +3069,18 @@ static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,if (dom->dirty_ops && !amd_iommu_hd_support (iommu))return -EINVAL ; +#if IS_ENABLED (CONFIG_AMD_IOMMU_IOMMUFD ) +/* Translate-device-id reservation must be done before setting up +* the DTE for the device to make sure that the id has not been allocated +* yet. (See amd_iommu_trans_devid_alloc().) +*/ + ret =amd_iommu_trans_devid_reserve (iommu->pci_seg, dev_data->devid); +if (ret) { +pr_err ("%s: Failed to reserve device id %#x\n" , __func__, dev_data->devid); +return ret; + } +#endif +if (dev_data->domain)detach_device (dev);
drivers/iommu/amd/trans_devid.c new file +78 -0
78 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v4 20/24 iommu/amd: Reserve translate-device-id for PCI requestor aliases
In reply to Suravee SuthikulpanitThe per-segment translate-device-id pool tracks which device-table indices are reserved for real PCI functions versus available for dynamic allocation to vIOMMU translation DTEs. amd_iommu_attach_device() already calls amd_iommu_trans_devid_reserve() for the struct device's own PCI BDF so that id cannot later be returned by the allocator.
That is not sufficient on its own. The AMD IOMMU driver programs identical DMA translation device-table entries (DTEs) for every requestor ID that can issue DMA on behalf of the same PCI function: the IVRS alias from alias_table[] when it is not covered by the PCI DMA-alias walk (different bus than the device), and every alias visited by pci_for_each_dma_alias(). Those alternate BDFs are not separate struct device attach targets, so they never received a trans_devid reservation and could in principle collide with a dynamically allocated trans_devid.
Introduce amd_iommu_trans_devid_reserve_pci_aliases() in trans_devid.c and invoke it from amd_iommu_attach_device() immediately after the primary amd_iommu_trans_devid_reserve() succeeds. For PCI devices the helper reserves the IVRS alias when it differs from the device BDF, then walks pci_for_each_dma_alias() and reserves each alias BDF. Repeated attach and overlap with the primary BDF in the PCI walk are handled by the existing idempotency of amd_iommu_trans_devid_reserve() (a second reserve of an already-reserved id succeeds).
--- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/iommu.c | 7 +++++ drivers/iommu/amd/trans_devid.c | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+)
drivers/iommu/amd/amd_iommu.h +2 -0
@@ -223,6 +223,8 @@ int amd_iommu_completion_wait(struct amd_iommu *iommu);void amd_iommu_pci_seg_trans_devid_init (struct amd_iommu_pci_seg *pci_seg);void amd_iommu_pci_seg_trans_devid_fini (struct amd_iommu_pci_seg *pci_seg);int amd_iommu_trans_devid_reserve (struct amd_iommu_pci_seg *pci_seg,u16 id); +int amd_iommu_trans_devid_reserve_pci_aliases (struct amd_iommu *iommu, +struct device *dev);#else static inline void amd_iommu_pci_seg_trans_devid_init (struct amd_iommu_pci_seg *pci_seg) { }
drivers/iommu/amd/iommu.c +7 -0
@@ -3079,6 +3079,13 @@ static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,pr_err ("%s: Failed to reserve device id %#x\n" , __func__, dev_data->devid); return ret; } + + ret =amd_iommu_trans_devid_reserve_pci_aliases (iommu, dev); +if (ret) { +pr_err ("%s: Failed to reserve translate devid for alias of %#x (err %d)\n" , + __func__, dev_data->devid, ret); +return ret; + }#endif if (dev_data->domain)
drivers/iommu/amd/trans_devid.c +45 -0
54 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v4 21/24 iommu/amd: Add translation DTE and VFctrl TransDevID helpers
In reply to Suravee SuthikulpanitThe hardware vIOMMU uses a per-vIOMMU translate device ID (TransDevID) to index the host device table when guest IOMMU traffic needs GPA->SPA translation. The VF Control guest miscellaneous register tells the IOMMU which TransDevID to use; the corresponding device table entry (DTE) points at the nest parent v1 page table that performs the walk from guest physical to system physical addresses.
Add amd_iommu_set_translate_dte() and amd_iommu_clear_translate_dte() to install or clear that DTE for a given TransDevID slot through iommu_dev_data and the existing DTE update path, restoring IVRS persistent bits via amd_iommu_make_clear_dte() on teardown. Add amd_iommu_update_vfctrl_mmio_translate_devid() to publish the TransDevID in VFctrl guest-misc MMIO, and VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET for the register offset.
--- drivers/iommu/amd/amd_iommu.h | 5 ++ drivers/iommu/amd/amd_iommu_types.h | 2 + drivers/iommu/amd/iommu.c | 82 ++++++++++++++++++++++++++++- 3 files changed, 87 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +5 -0
@@ -232,6 +232,11 @@ static inline void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg) { }#endif +int amd_iommu_set_translate_dte (struct iommufd_viommu *viommu); +void amd_iommu_clear_translate_dte (struct amd_iommu *iommu,u32 trans_devid); +void amd_iommu_update_vfctrl_mmio_translate_devid (struct amd_iommu *iommu, +u16 gid,u32 trans_devid); +static inline void amd_iommu_make_clear_dte (struct amd_iommu *iommu,u16 devid,struct dev_table_entry *new)
drivers/iommu/amd/amd_iommu_types.h +2 -0
@@ -495,6 +495,7 @@ extern bool amdr_ivrs_remap_support;/* VIOMMU stuff */ #define VIOMMU_VF_MMIO_ENTRY_SIZE 4096#define VIOMMU_VFCTRL_MMIO_ENTRY_SIZE 64 +#define VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET 0x10/* Host ioremap/request_mem_region sizes for VF / VF_CNTL BARs */ #define VIOMMU_VF_MMIO_MAP_SIZE 0x10000000UL
@@ -556,6 +557,7 @@ struct amd_iommu_viommu {u64 *devid_table;u64 *domid_table; +u16 trans_devid;/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */ unsigned long vfmmio_mmap_offset;
drivers/iommu/amd/iommu.c +80 -2
100 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v4 22/24 iommu/amd: Add translate-device-id alloc/free with vIOMMU owner
In reply to Suravee SuthikulpanitExtend the per-segment translate-device-id pool to record the owning struct amd_iommu_viommu in the xarray. Add amd_iommu_trans_devid_alloc() and amd_iommu_trans_devid_free() to hand out ids from the high end of the range and return them on teardown.
--- drivers/iommu/amd/amd_iommu.h | 4 ++ drivers/iommu/amd/trans_devid.c | 100 ++++++++++++++++++++++++++++++-- 2 files changed, 100 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_iommu.h +4 -0
@@ -225,6 +225,10 @@ void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg);int amd_iommu_trans_devid_reserve (struct amd_iommu_pci_seg *pci_seg,u16 id);int amd_iommu_trans_devid_reserve_pci_aliases (struct amd_iommu *iommu,struct device *dev); +int amd_iommu_trans_devid_alloc (struct amd_iommu_pci_seg *pci_seg, +struct amd_iommu_viommu *aviommu); +void amd_iommu_trans_devid_free (struct amd_iommu_pci_seg *pci_seg,u16 id, +struct amd_iommu_viommu *aviommu);#else static inline void amd_iommu_pci_seg_trans_devid_init (struct amd_iommu_pci_seg *pci_seg) { }
drivers/iommu/amd/trans_devid.c +96 -4
123 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v4 23/24 iommu/amd: Assign per-vIOMMU translate device ID
In reply to Suravee SuthikulpanitAllocate one translate-device-id per IOMMUFD vIOMMU instance from the per-segment pool on init. Program translation DTE and VFctrl TransDevID after MMIO reset; clear both on init error and destroy.
Add per-vIOMMU trans_devid_lock to serialize DTE and VFctrl updates during teardown.
--- drivers/iommu/amd/amd_iommu_types.h | 7 ++++++ drivers/iommu/amd/iommufd.c | 35 +++++++++++++++++++++++++++++ drivers/iommu/amd/viommu.c | 2 ++ 3 files changed, 44 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +7 -0
@@ -559,6 +559,13 @@ struct amd_iommu_viommu {u64 *domid_table;u16 trans_devid; +/* +* Serializes translate-device-id hardware changes (DTE, VFctrl) and +* coordinates with pool relocation during PCI attach. Lock ordering: +* pci_seg->trans_devid_mutex, then trans_devid_lock. +*/ +struct mutex trans_devid_lock; +/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */ unsigned long vfmmio_mmap_offset; };
drivers/iommu/amd/iommufd.c +35 -0
@@ -48,11 +48,15 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *int ret;phys_addr_t page_base;unsigned long flags; +u16 trans_devid; +bool trans_devid_allocated = false; +bool trans_dte_set = false;struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu); struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); +mutex_init (&aviommu->trans_devid_lock);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 ); aviommu->parent = pdom;
@@ -81,9 +85,22 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * data.out_vfmmio_mmap_offset = aviommu->vfmmio_mmap_offset; + ret =amd_iommu_trans_devid_alloc (iommu->pci_seg, aviommu); +if (ret <0 ) + goto err_trans_devid; + trans_devid = ret; + trans_devid_allocated = true; + aviommu->trans_devid = trans_devid; +/* Reset vIOMMU MMIOs to initialize the vIOMMU */ iommu_reset_vmmio (iommu, aviommu->gid); + ret =amd_iommu_set_translate_dte (viommu); +if (ret) + goto err_init; + trans_dte_set = true; +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid, trans_devid); + ret =amd_viommu_init_one (iommu, aviommu);if (ret) goto err_init;
@@ -102,6 +119,18 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return 0 ; err_init: +if (trans_dte_set) { +mutex_lock (&aviommu->trans_devid_lock); +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid,0 ); +amd_iommu_clear_translate_dte (iommu, trans_devid); +mutex_unlock (&aviommu->trans_devid_lock); + } +if (trans_devid_allocated) { +mutex_lock (&aviommu->trans_devid_lock); +amd_iommu_trans_devid_free (iommu->pci_seg, trans_devid, aviommu); +mutex_unlock (&aviommu->trans_devid_lock); + } +err_trans_devid:iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); err_mmap:amd_iommu_gid_free (iommu, aviommu->gid);
@@ -124,6 +153,12 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)xa_destroy (&aviommu->gdomid_array);iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);amd_viommu_uninit_one (iommu, aviommu); + +mutex_lock (&aviommu->trans_devid_lock); +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid,0 ); +amd_iommu_clear_translate_dte (iommu, aviommu->trans_devid); +amd_iommu_trans_devid_free (iommu->pci_seg, aviommu->trans_devid, aviommu); +mutex_unlock (&aviommu->trans_devid_lock);amd_iommu_gid_free (iommu, aviommu->gid); }
drivers/iommu/amd/viommu.c +2 -0
@@ -501,6 +501,8 @@ void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *aviVIOMMU_DOMID_MAPPING_BASE ,VIOMMU_DOMID_MAPPING_ENTRY_SIZE , aviommu->gid); + +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid,0 );viommu_clear_mapping (iommu, aviommu); }
Signature · 1 line
2.34.1
Re: [PATCH 23/24] iommu/amd: Assign per-vIOMMU translate device ID
Quoted 96 lines · Suravee Suthikulpanit
Allocate one translate-device-id per IOMMUFD vIOMMU instance from the per-segment pool on init. Program translation DTE and VFctrl TransDevID after MMIO reset; clear both on init error and destroy.
Add per-vIOMMU
trans_devid_lockto serialize DTE and VFctrl updates during teardown.Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 7 ++++++ drivers/iommu/amd/iommufd.c | 35 +++++++++++++++++++++++++++++ drivers/iommu/amd/viommu.c | 2 ++ 3 files changed, 44 insertions(+)drivers/iommu/amd/amd_iommu_types.h +7
-0@@ -559,6 +559,13 @@ struct amd_iommu_viommu {u64 *domid_table;u16 trans_devid; +/* +* Serializes translate-device-id hardware changes (DTE, VFctrl) and +* coordinates with pool relocation during PCI attach. Lock ordering: +* pci_seg->trans_devid_mutex, then trans_devid_lock. +*/ +struct mutex trans_devid_lock; +/* Offset for mmap() of guest VF MMIO; set after iommufd_viommu_alloc_mmap(). */ unsigned long vfmmio_mmap_offset; };drivers/iommu/amd/iommufd.c +29
-0@@ -48,11 +48,15 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *int ret;phys_addr_t page_base;unsigned long flags; +u16 trans_devid; +bool trans_devid_allocated = false; +bool trans_dte_set = false;struct iommu_viommu_amd data = {};struct protection_domain *pdom =to_pdomain (parent); struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu); struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); +mutex_init (&aviommu->trans_devid_lock);xa_init_flags (&aviommu->gdomid_array,XA_FLAGS_ALLOC1 ); aviommu->parent = pdom;@@ -81,9 +85,22 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * data.out_vfmmio_mmap_offset = aviommu->vfmmio_mmap_offset; + ret =amd_iommu_trans_devid_alloc (iommu->pci_seg, aviommu); +if (ret <0 ) + goto err_trans_devid; + trans_devid = ret; + trans_devid_allocated = true; + aviommu->trans_devid = trans_devid; +/* Reset vIOMMU MMIOs to initialize the vIOMMU */ iommu_reset_vmmio (iommu, aviommu->gid); + ret =amd_iommu_set_translate_dte (viommu); +if (ret) + goto err_init; + trans_dte_set = true; +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid, trans_devid); + ret =amd_viommu_init_one (iommu, aviommu);if (ret) goto err_init;@@ -102,6 +119,18 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return 0 ; err_init: +if (trans_dte_set) { +mutex_lock (&aviommu->trans_devid_lock); +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid,0 ); +amd_iommu_clear_translate_dte (iommu, trans_devid); +mutex_unlock (&aviommu->trans_devid_lock); + } +if (trans_devid_allocated) { +mutex_lock (&aviommu->trans_devid_lock); +amd_iommu_trans_devid_free (iommu->pci_seg, trans_devid, aviommu); +mutex_unlock (&aviommu->trans_devid_lock); + } +err_trans_devid:iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); err_mmap:amd_iommu_gid_free (iommu, aviommu->gid);@@ -124,6 +153,12 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)xa_destroy (&aviommu->gdomid_array);
drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 163
Quoted 10 lines
drivers/iommu/amd/iommufd.c +6
-0iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);amd_viommu_uninit_one (iommu, aviommu); + +mutex_lock (&aviommu->trans_devid_lock); +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid,0 ); +amd_iommu_clear_translate_dte (iommu, aviommu->trans_devid); +amd_iommu_trans_devid_free (iommu->pci_seg, aviommu->trans_devid, aviommu); +mutex_unlock (&aviommu->trans_devid_lock);amd_iommu_gid_free (iommu, aviommu->gid); }
The comment states "pci_seg->trans_devid_mutex, then trans_devid_lock" as the lock ordering. However, examining the actual code paths:
In amd_iommufd_viommu_destroy():
mutex_lock(&aviommu->trans_devid_lock); /* viommu lock first */
...
amd_iommu_trans_devid_free(...); /* takes seg_mutex inside */ mutex_unlock(&aviommu->trans_devid_lock);
In amd_iommu_trans_devid_free():
mutex_lock(&pci_seg->trans_devid_mutex); /* seg_mutex nested */
In trans_devid_relocate() (Patch 24):
mutex_lock(&aviommu->trans_devid_lock); /* viommu lock first */ mutex_lock(&pci_seg->trans_devid_mutex); /* seg_mutex second */
All paths consistently take trans_devid_lock first, then trans_devid_mutex (nested). The actual ordering is the opposite of what the comment describes:
Actual: trans_devid_lock -> trans_devid_mutex
Comment: trans_devid_mutex -> trans_devid_lock
The code is consistent and there is no deadlock risk, but the comment should be corrected to avoid confusing future readers:
* Lock ordering: trans_devid_lock, then pci_seg->trans_devid_mutex.
Quoted 14 lines
drivers/iommu/amd/viommu.c +2
-0@@ -501,6 +501,8 @@ void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *aviVIOMMU_DOMID_MAPPING_BASE ,VIOMMU_DOMID_MAPPING_ENTRY_SIZE , aviommu->gid); + +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid,0 );viommu_clear_mapping (iommu, aviommu); }
Suthikulpanit, Suravee
reviewerRe: [PATCH 23/24] iommu/amd: Assign per-vIOMMU translate device ID
Quoted 72 lines · On 8/19/2026 5:18 PM, [email protected] wrote:
Quoted 37 lines · Suravee Suthikulpanit
Allocate one translate-device-id per IOMMUFD vIOMMU instance from the per-segment pool on init. Program translation DTE and VFctrl TransDevID after MMIO reset; clear both on init error and destroy.
Add per-vIOMMU
trans_devid_lockto serialize DTE and VFctrl updates during teardown.Signed-off-by Suravee Suthikulpanit <[email protected]>---
drivers/iommu/amd/amd_iommu_types.h| 7 ++++++ drivers/iommu/amd/iommufd.c | 35 +++++++++++++++++++++++++++++ drivers/iommu/amd/viommu.c | 2 ++3 files changed, 44 insertions(+)drivers/iommu/amd/amd_iommu_types.h +3
-0@@ -559,6 +559,13 @@ struct amd_iommu_viommu { u64 *domid_table; u16 trans_devid; + /* +* Serializes translate-device-id hardware changes (DTE, VFctrl) and +* coordinates with pool relocation during PCI attach. Lock ordering:
+ *pci_seg->trans_devid_mutex, thentrans_devid_lock. + */
+ struct mutextrans_devid_lock;drivers/iommu/amd/amd_iommu_types.h +1
-0+@@ -124,6 +153,12 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) xa_destroy(&aviommu->gdomid_array);iommufd_viommu_destroy_mmap(&aviommu->core, aviommu-
Quoted 1 line
vfmmio_mmap_offset);Quoted 11 lines
amd_viommu_uninit_one(iommu, aviommu);
+
+mutex_lock(&aviommu->trans_devid_lock);
+amd_iommu_update_vfctrl_mmio_translate_devid(iommu, aviommu->gid, 0);
+amd_iommu_clear_translate_dte(iommu, aviommu->trans_devid); +amd_iommu_trans_devid_free(iommu->pci_seg, aviommu->trans_devid, aviommu);
+mutex_unlock(&aviommu->trans_devid_lock);amd_iommu_gid_free(iommu, aviommu->gid);
}The comment states "
pci_seg->trans_devid_mutex, thentrans_devid_lock" as the lock ordering. However, examining the actual code paths:In
amd_iommufd_viommu_destroy():
mutex_lock(&aviommu->trans_devid_lock); /* viommu lock first */
...
amd_iommu_trans_devid_free(...); /* takesseg_mutex
inside */
mutex_unlock(&aviommu->trans_devid_lock);In
amd_iommu_trans_devid_free():
mutex_lock(&pci_seg->trans_devid_mutex); /*seg_mutexnested */In
trans_devid_relocate()(Patch 24):
mutex_lock(&aviommu->trans_devid_lock); /* viommu lock first */ mutex_lock(&pci_seg->trans_devid_mutex); /*seg_mutexsecond */All paths consistently take
trans_devid_lockfirst, thentrans_devid_mutex(nested). The actual ordering is the opposite of what the comment describes:
Quoted 7 lines
Actual: trans_devid_lock ->
trans_devid_mutexComment:trans_devid_mutex->trans_devid_lockThe code is consistent and there is no deadlock risk, but the comment should be corrected to avoid confusing future readers:
* Lock ordering:
trans_devid_lock, thenpci_seg->trans_devid_mutex.
I'll update the comment in v5.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 24/24 iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve
In reply to Suravee SuthikulpanitWhen PCI attach reserves a BDF already allocated to a vIOMMU, relocate that vIOMMU to a newly allocated translate-device-id before marking the BDF reserved for the device. Reprogram translation DTE and VFctrl under trans_devid_lock; roll back the pool on failure.
--- drivers/iommu/amd/trans_devid.c | 116 +++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-)
drivers/iommu/amd/trans_devid.c +115 -1
@@ -82,16 +82,120 @@ void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg)xa_destroy (&pci_seg->trans_devid_xa); } +/** +* trans_devid_do_relocate - move vIOMMU translation DTE from @old_id to @new_id +* +* Caller holds @aviommu->trans_devid_lock. Pool xarray already records @new_id +* as allocated to @aviommu and @old_id as reserved. +*/ +static int trans_devid_do_relocate (struct amd_iommu_viommu *aviommu, +u16 old_id,u16 new_id) +{ +struct iommufd_viommu *viommu = &aviommu->core; +struct amd_iommu *iommu = + container_of(viommu->iommu_dev, struct amd_iommu, iommu); +int ret; + + aviommu->trans_devid = new_id; + + ret =amd_iommu_set_translate_dte (viommu); +if (ret) + goto err_restore_id; + +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid, new_id); + +if (search_dev_data (iommu, old_id)) +amd_iommu_clear_translate_dte (iommu, old_id); + +return 0 ; + +err_restore_id: + aviommu->trans_devid = old_id; +return ret; +} + +/** +* trans_devid_relocate - move an allocated id to a new slot and reserve @from_id +* +* Called when PCI attach needs a BDF that a vIOMMU already owns. Updates the +* per-segment pool, then reprograms DTE and VFctrl on the owning vIOMMU. +* +* Locking: takes @aviommu->trans_devid_lock, then pci_seg->trans_devid_mutex +* (same order as destroy). Hardware steps run with the viommu lock held and +* the segment mutex dropped. +*/ +static int trans_devid_relocate (struct amd_iommu_pci_seg *pci_seg,u16 from_id, +struct amd_iommu_viommu *aviommu) +{ +u16 new_id; +int ret; + +mutex_lock (&aviommu->trans_devid_lock); + +mutex_lock (&pci_seg->trans_devid_mutex); +if (trans_devid_xa_owner (xa_load (&pci_seg->trans_devid_xa, from_id)) != + aviommu) { + ret = -ENOENT ; + goto unlock_seg; + } + +if (aviommu->trans_devid != from_id) { + ret = -EINVAL ; + goto unlock_seg; + } + + new_id =trans_devid_find_free_locked (pci_seg); +if (new_id <0 ) { + ret = new_id; + goto unlock_seg; + } + + ret =trans_devid_xa_install_allocated_locked (pci_seg, new_id, aviommu); +if (ret) + goto unlock_seg; + + ret =trans_devid_xa_install_reserved_locked (pci_seg, from_id); +if (ret) { +xa_erase (&pci_seg->trans_devid_xa, new_id); + goto unlock_seg; + } + +mutex_unlock (&pci_seg->trans_devid_mutex); + + ret =trans_devid_do_relocate (aviommu, from_id, new_id); +if (ret) { +mutex_lock (&pci_seg->trans_devid_mutex); +xa_erase (&pci_seg->trans_devid_xa, new_id); +trans_devid_xa_install_allocated_locked (pci_seg, from_id, + aviommu); +mutex_unlock (&pci_seg->trans_devid_mutex); + } + +mutex_unlock (&aviommu->trans_devid_lock); +return ret; + +unlock_seg: +mutex_unlock (&pci_seg->trans_devid_mutex); +mutex_unlock (&aviommu->trans_devid_lock); +return ret; +} +/** * amd_iommu_trans_devid_reserve - occupy @id so it is never returned by alloc * * Reservation is done when attaching device to a domain (see amd_iommu_attach_device()). * -* Return: 0 on success. A second reserve of an already-reserved @id succeeds. +* When @id is allocated to a vIOMMU (e.g. after PCI hot-plug), the driver relocates +* that vIOMMU to a newly allocated translate-device-id and reserves @id for the PCI +* function. +* +* Return: 0 on success, %-ENOSPC if relocation cannot find a free id, or another +* errno from relocation. A second reserve of an already-reserved @id succeeds. */ int amd_iommu_trans_devid_reserve (struct amd_iommu_pci_seg *pci_seg,u16 id) {void *entry; +struct amd_iommu_viommu *aviommu;int ret =0 ;mutex_lock (&pci_seg->trans_devid_mutex);
@@ -99,6 +203,16 @@ int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 id)if (trans_devid_xa_is_reserved (entry)) goto unlock; + aviommu =trans_devid_xa_owner (entry); +if (aviommu) { +mutex_unlock (&pci_seg->trans_devid_mutex); + ret =trans_devid_relocate (pci_seg, id, aviommu); +if (!ret) +pr_debug ("%s: Reserved trans_devid %#x after relocation (seg %#x)\n" , + __func__, id, pci_seg->id); +return ret; + } + ret =trans_devid_xa_install_reserved_locked (pci_seg, id); unlock:mutex_unlock (&pci_seg->trans_devid_mutex);
Signature · 1 line
2.34.1
Re: [PATCH 24/24] iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve
Quoted 75 lines · Suravee Suthikulpanit
When PCI attach reserves a BDF already allocated to a vIOMMU, relocate that vIOMMU to a newly allocated translate-device-id before marking the BDF reserved for the device. Reprogram translation DTE and VFctrl under
trans_devid_lock; roll back the pool on failure.Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/trans_devid.c | 116 +++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-)drivers/iommu/amd/trans_devid.c +57
-0@@ -82,16 +82,120 @@ void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg)xa_destroy (&pci_seg->trans_devid_xa); } +/** +* trans_devid_do_relocate - move vIOMMU translation DTE from @old_id to @new_id +* +* Caller holds @aviommu->trans_devid_lock. Pool xarray already records @new_id +* as allocated to @aviommu and @old_id as reserved. +*/ +static int trans_devid_do_relocate (struct amd_iommu_viommu *aviommu, +u16 old_id,u16 new_id) +{ +struct iommufd_viommu *viommu = &aviommu->core; +struct amd_iommu *iommu = + container_of(viommu->iommu_dev, struct amd_iommu, iommu); +int ret; + + aviommu->trans_devid = new_id; + + ret =amd_iommu_set_translate_dte (viommu); +if (ret) + goto err_restore_id; + +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid, new_id); + +if (search_dev_data (iommu, old_id)) +amd_iommu_clear_translate_dte (iommu, old_id); + +return 0 ; + +err_restore_id: + aviommu->trans_devid = old_id; +return ret; +} + +/** +* trans_devid_relocate - move an allocated id to a new slot and reserve @from_id +* +* Called when PCI attach needs a BDF that a vIOMMU already owns. Updates the +* per-segment pool, then reprograms DTE and VFctrl on the owning vIOMMU. +* +* Locking: takes @aviommu->trans_devid_lock, then pci_seg->trans_devid_mutex +* (same order as destroy). Hardware steps run with the viommu lock held and +* the segment mutex dropped. +*/ +static int trans_devid_relocate (struct amd_iommu_pci_seg *pci_seg,u16 from_id, +struct amd_iommu_viommu *aviommu) +{ +u16 new_id; +int ret; + +mutex_lock (&aviommu->trans_devid_lock); + +mutex_lock (&pci_seg->trans_devid_mutex); +if (trans_devid_xa_owner (xa_load (&pci_seg->trans_devid_xa, from_id)) != + aviommu) { + ret = -ENOENT ; + goto unlock_seg; + } +
drivers/iommu/amd/trans_devid.c void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg) · line 151
Quoted 10 lines
drivers/iommu/amd/trans_devid.c +10
-0+if (aviommu->trans_devid != from_id) { + ret = -EINVAL ; + goto unlock_seg; + } + + new_id =trans_devid_find_free_locked (pci_seg); +if (new_id <0 ) { + ret = new_id; + goto unlock_seg; + }
This has a type-truncation bug. trans_devid_find_free_locked() returns int and signals failure with -ENOSPC. Assigning it to a u16 truncates -ENOSPC (-28) to 65508 (0xFFDC), and the subsequent "if (new_id < 0)" is always false for an unsigned u16. On pool exhaustion the code would proceed to install and program a DTE with the bogus ID 65508. Please change new_id to int:
int new_id;
Quoted 68 lines
drivers/iommu/amd/trans_devid.c +48
-1+ + ret =trans_devid_xa_install_allocated_locked (pci_seg, new_id, aviommu); +if (ret) + goto unlock_seg; + + ret =trans_devid_xa_install_reserved_locked (pci_seg, from_id); +if (ret) { +xa_erase (&pci_seg->trans_devid_xa, new_id); + goto unlock_seg; + } + +mutex_unlock (&pci_seg->trans_devid_mutex); + + ret =trans_devid_do_relocate (aviommu, from_id, new_id); +if (ret) { +mutex_lock (&pci_seg->trans_devid_mutex); +xa_erase (&pci_seg->trans_devid_xa, new_id); +trans_devid_xa_install_allocated_locked (pci_seg, from_id, + aviommu); +mutex_unlock (&pci_seg->trans_devid_mutex); + } + +mutex_unlock (&aviommu->trans_devid_lock); +return ret; + +unlock_seg: +mutex_unlock (&pci_seg->trans_devid_mutex); +mutex_unlock (&aviommu->trans_devid_lock); +return ret; +} +/** * amd_iommu_trans_devid_reserve - occupy @id so it is never returned by alloc * * Reservation is done when attaching device to a domain (see amd_iommu_attach_device()). * -* Return: 0 on success. A second reserve of an already-reserved @id succeeds. +* When @id is allocated to a vIOMMU (e.g. after PCI hot-plug), the driver relocates +* that vIOMMU to a newly allocated translate-device-id and reserves @id for the PCI +* function. +* +* Return: 0 on success, %-ENOSPC if relocation cannot find a free id, or another +* errno from relocation. A second reserve of an already-reserved @id succeeds. */ int amd_iommu_trans_devid_reserve (struct amd_iommu_pci_seg *pci_seg,u16 id) {void *entry; +struct amd_iommu_viommu *aviommu;int ret =0 ;mutex_lock (&pci_seg->trans_devid_mutex);@@ -99,6 +203,16 @@ int amd_iommu_trans_devid_reserve(struct amd_iommu_pci_seg *pci_seg, u16 id)if (trans_devid_xa_is_reserved (entry)) goto unlock; + aviommu =trans_devid_xa_owner (entry); +if (aviommu) { +mutex_unlock (&pci_seg->trans_devid_mutex); + ret =trans_devid_relocate (pci_seg, id, aviommu); +if (!ret) +pr_debug ("%s: Reserved trans_devid %#x after relocation (seg %#x)\n" , + __func__, id, pci_seg->id); +return ret; + } + ret =trans_devid_xa_install_reserved_locked (pci_seg, id); unlock:mutex_unlock (&pci_seg->trans_devid_mutex);
Suthikulpanit, Suravee
reviewerRe: [PATCH 24/24] iommu/amd: Relocate vIOMMU translate-device-id on PCI reserve
Quoted 56 lines · On 8/19/2026 4:42 PM, [email protected] wrote:
Quoted 35 lines · Suravee Suthikulpanit
When PCI attach reserves a BDF already allocated to a vIOMMU, relocate that vIOMMU to a newly allocated translate-device-id before marking the BDF reserved for the device. Reprogram translation DTE and VFctrl under
trans_devid_lock; roll back the pool on failure.Signed-off-by Suravee Suthikulpanit <[email protected]>---
drivers/iommu/amd/trans_devid.c| 116 +++++++++++++++++++++++++++++++-1 file changed, 115 insertions(+), 1 deletion(-)drivers/iommu/amd/trans_devid.c +1
-0@@ -82,16 +82,120 @@ void amd_iommu_pci_seg_trans_devid_fini(struct +static int trans_devid_relocate(struct amd_iommu_pci_seg *pci_seg,u16
from_id,
+ structamd_iommu_viommu*aviommu)drivers/iommu/amd/trans_devid.c +8
-0+{ + u16 new_id; + int ret; + + mutex_lock(&aviommu->trans_devid_lock); + + mutex_lock(&pci_seg->trans_devid_mutex); + if (trans_devid_xa_owner(xa_load(&pci_seg->trans_devid_xa,
from_id)) !=
+ aviommu) {
+ ret =-ENOENT;
+ gotounlock_seg;
+ }drivers/iommu/amd/trans_devid.c +1
-0+drivers/iommu/amd/trans_devid.c void amd_iommu_pci_seg_trans_devid_fini(struct amd_iommu_pci_seg *pci_seg) · line 151
Quoted 10 lines
drivers/iommu/amd/trans_devid.c +10
-0+ if (aviommu->trans_devid != from_id) { + ret = -EINVAL; + goto unlock_seg; + } + + new_id = trans_devid_find_free_locked(pci_seg); + if (new_id < 0) { + ret = new_id; + goto unlock_seg; + }This has a type-truncation bug.
trans_devid_find_free_locked()returns int and signals failure with-ENOSPC. Assigning it to a u16 truncates-ENOSPC(-28) to 65508 (0xFFDC), and the subsequent "if (new_id< 0)" is always false for an unsigned u16. On pool exhaustion the code would proceed to install and program a DTE with the bogus ID 65508. Please changenew_idto int:
Quoted 2 lines
int
new_id;
I'll fix in v5.
Thanks,
Suravee
Tian, Kevin
maintainerQuoted 20 lines
From: Suravee Suthikulpanit <[email protected]>
Sent: Monday, July 27, 2026 9:29 PMAMD IOMMU introduces the vIOMMU feature, which provides partial hardware
acceleration when implementing Guest IOMMUs. This feature provides acceleration for guest Command Buffer, Event Log, and PPR Log. This eliminates the CPU overhead needed for the supporting HV intercepts and reduces the latency of these operations.When a guest attempts to access guest IOMMU MMIO registers with offsets between 8KB and 12KB (i.e. the 3rd 4K region) such as the Command Buffer, Event Log and PPR Log head and tail pointer registers, this is serviced directly by the IOMMU. When the IOMMU accesses a Command Buffer, PPR Log
or aCOMPLETION_WAITstore location in memory, it directly accesses guest physical memory. The HV/VMM continues to trap and emulate the IOMMU configuration MMIO registers between 0KB and 4KB (i.e. the 1st 4K region), which are primarily used during initialization.
Quoted 10 lines
Additionally, the HV must initialize the vIOMMU feature, map MMIO resources
between the VMs and the IOMMU, manage additional supporting data structures
in memory (e.g.GPA->SPAtranslation DTE, Device ID and Domain ID mapping
tables), and allocate/map vIOMMU Private Address region used as backing storage memory for the IOMMU. Support for new IOMMU command and events
specifically for vIOMMU are also added.
above tells the full architecture...
[...]
Quoted 4 lines
Testing done:
* Single/Multiple vIOMMU instances
* Single/Multiple VFIO devices per vIOMMU instance.
... and here seems to indicate it's a full/testable support.
But patch3 says:
"
Introduce a new global variable amd_iommu_viommu, which is used to control the feature enablement in the driver. Currently, the feature is default to disabled. Once the feature is fully supported, it will be changed to enabled by default.
"
then amd_iommu_viommu is never turned on in this series, indicating it's only a partial support.
It'd be good to clarify it plus listing the missing pieces. better with a git repo if one is interested in checking the entire logic.
btw it failed to apply in Sashiko. The review there is generally beneficial for such a big series.
Thanks,
Kevin
Jason Gunthorpe
maintainerQuoted 7 lines
[ ... 75 lines skipped ... ]
Testing done:
* Single/Multiple vIOMMU instances
* Single/Multiple VFIO devices per vIOMMU instance.[1] IOMMU Specification: https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB
[2] Linux git tree: https://github.com/AMDESE/linux-iommu/tree/linux-7.1.0-rc4-amd-viommu_upstream_v4
It failed to apply for sashiko, you should resend it after rc1.
Best practice for sending patch is to have git include a base commit header and base your series directly on some commit in linux-next. Then sashiko can find it.
Signature · 1 line
Jason