iommu/amd: Introduce AMD Hardware-accelerated Virtualized IOMMU (vIOMMU) Support
Subsystems (3)
Suravee Suthikulpanit
reviewerv2 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 2 of the AMD HW-vIOMMU series previously posted as v1[2]. It is implemented on top of the IOMMUFD vIOMMU, vDevice, and hw_queue framework in Linux v7.1.0-rc4.
The series is organized into the following subsets:
Patch 1-3 : Preparatory patches Patch 4-5 : Introduce IOMMUFD vIOMMU support for AMD Patch 6-8 : Introduce AMD vIOMMU VF MMIO and VFCtrl MMIO Patch 9-12 : Introduce AMD vIOMMU Private Address support
Patch 13-16 : Introduce IOMMUFD vDevice support for AMD Patch 17-21 : Translation DTE, KVM FD, and translate-device-ID pool Patch 22-26 : IOMMUFD hw_queue, VIOMMU_COMMAND ioctl, enable vIOMMU
Changes since v1:
(https://lore.kernel.org/linux-iommu/[email protected]/)
Rebase and scope:
* Rebased on Linux v7.1.0-rc4.
Guest ID (GID) - design change (patch 5-6):
* v1: A single global IDA; each GID is unique across all AMD IOMMUs in the system.
* v2: A per-amd_iommu IDA (gid_ida), initialized when vIOMMU MMIO is set up on that IOMMU. GIDs are allocated in amd_iommufd_viommu_init() and freed on destroy - one GID per IOMMUFD vIOMMU object, unique within that IOMMU only. A VM with guest IOMMUs behind multiple IOMMUs may therefore hold multiple GIDs. This is separate from TransDevID, which remains one per VM (kvmfd) and is shared across vIOMMUs for that VM.
Translate device ID (TransDevID) - design change (patches 17-21):
* v1: VMM supplies trans_devid via struct iommu_viommu_amd; driver programs translation DTE and VFctrl guest-misc register.
* v2: Driver allocates trans_devid per VM from a per-PCI-segment pool (trans_devid.c), reserves IDs used by real PCI functions on attach, and keys allocation to kvmfd so multiple vIOMMU instances for one VM share a single GPA->SPA translation DTE.
* struct iommu_viommu_amd gains kvmfd; trans_devid removed from UAPI.
Userspace vIOMMU control path - design change (patches 24-25):
* v1: Extended IOMMU_OPTION with IOMMU_OPTION_VIOMMU and set_option/get_option ops.
* v2: New IOMMU_VIOMMU_COMMAND ioctl with set_command/get_command ops for indexed read/write of vIOMMU register arrays (e.g. guest MMIO shadow). AMD backend moves guest MMIO accessors to vfctrl_mmio.c.
Single-patch / implementation notes:
* Patch 4: Gate amd_iommufd_get_viommu_size() on HW vIOMMU capability.
* Patch 8: Reset-vMMIO helper declaration in amd_iommu.h (rebase).
* Patch 11-12: Per-VM IPA map/unmap via iommu_map/iommu_unmap (was pt_iommu_amdv1_map_pages in v1 per-VM helpers).
* Patch 26: iommu_feature_enable_and_check(); improved error unwind in
amd_viommu_init(); enable vIOMMU by default when supported.
Upcoming series (in subsequent submission):
* Extended Interrupt Remapping (guest Event / PPR log interrupts)
* KVM/AVIC integration and guest event injection support
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.10_PUB
[2] Linux git tree: https://github.com/AMDESE/linux-iommu/tree/linux-7.1.0-rc4-amd-viommu_upstream_v2
Thank you,
Suravee
Suravee Suthikulpanit (26):
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 domain for IOMMU Private Address (IPA) region iommu/amd: Assign IOMMU Private Address domain to IOMMU iommu/amd: Allocate and map vIOMMU private regions 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: Pass KVM FD from userspace when initializing vIOMMU iommu/amd: Add translation DTE and VFctrl TransDevID helpers iommu/amd: Add per-segment translate device ID pool iommu/amd: Reserve translate-device-id for PCI requestor aliases iommu/amd: Map kvmfd to shared translate device ID for vIOMMU
iommufd: Add hw_queue_init and split queue alloc paths iommu/amd: Add support for vIOMMU HW queues initialization
iommufd: Introduce vIOMMU command via VIOMMU_COMMAND ioctl iommu/amd: Handle set/get command for AMD vIOMMU iommu/amd: Introduce logic to check and enable vIOMMU feature
drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 46 +++ drivers/iommu/amd/amd_iommu_types.h | 129 ++++++ drivers/iommu/amd/amd_viommu.h | 73 ++++ drivers/iommu/amd/init.c | 33 +- drivers/iommu/amd/iommu.c | 225 ++++++++-- drivers/iommu/amd/iommufd.c | 245 +++++++++++ drivers/iommu/amd/nested.c | 18 + drivers/iommu/amd/trans_devid.c | 317 ++++++++++++++ drivers/iommu/amd/vfctrl_mmio.c | 146 +++++++ drivers/iommu/amd/viommu.c | 529 ++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 3 + drivers/iommu/iommufd/viommu.c | 150 +++++-- include/linux/iommufd.h | 10 +- include/uapi/linux/iommufd.h | 51 +++ 16 files changed, 1909 insertions(+), 69 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/vfctrl_mmio.c create mode 100644 drivers/iommu/amd/viommu.c
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 1/26 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
Suravee Suthikulpanit
reviewerPATCH v2 2/26 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 +0 -0
--- 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
Suravee Suthikulpanit
reviewerPATCH v2 3/26 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 along with a command-line option to disable if needed.
[1] https://docs.amd.com/v/u/en-US/48882_3.10_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
Jason Gunthorpe
maintainerRe: [PATCH v2 03/26] iommu/amd: Detect and initialize AMD vIOMMU feature
Quoted 8 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 along with a command-line option to disable if needed.
Still no to command line option. Just don't use iommufd if you don't want to use it. It must have no cost until iommufd activates it.
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 03/26] iommu/amd: Detect and initialize AMD vIOMMU feature
Quoted 10 lines · Jason Gunthorpe
Quoted 8 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 along with a command-line option to disable if needed.
Quoted 4 lines
Still no to command line option. Just don't use iommufd if you don't want to use it. It must have no cost until iommufd activates it.
Jason
Noted. Just do not set CONFIG_AMD_IOMMU_IOMMUFD to remove viommu support.
Suravee
Suravee Suthikulpanit
reviewerPATCH v2 4/26 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 +0 -0
--- 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) +* @reserved: Must be zero +*/ +struct iommu_viommu_amd { +__u32 reserved;/* must be last */ +}; +/** * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC) * @size: sizeof(struct iommu_viommu_alloc)
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v2 04/26] iommu/amd: Introduce IOMMUFD vIOMMU support for AMD
include/uapi/linux/iommufd.h struct iommu_viommu_tegra241_cmdqv { · line 1091
Quoted 7 lines · Suravee Suthikulpanit
include/uapi/linux/iommufd.h +7
-0+/** +* struct iommu_viommu_amd - AMD vIOMMU Interface (IOMMU_VIOMMU_TYPE_AMD) +* @reserved: Must be zero +*/ +struct iommu_viommu_amd { +__u32 reserved;/* must be last */ +};
Do not structure patches like this, introduce the full and complete ABI structure here.
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 04/26] iommu/amd: Introduce IOMMUFD vIOMMU support for AMD
Quoted 13 lines · Jason Gunthorpe
include/uapi/linux/iommufd.h struct iommu_viommu_tegra241_cmdqv { · line 1091
Quoted 7 lines · Suravee Suthikulpanit
include/uapi/linux/iommufd.h +7
-0+/** +* struct iommu_viommu_amd - AMD vIOMMU Interface (IOMMU_VIOMMU_TYPE_AMD) +* @reserved: Must be zero +*/ +struct iommu_viommu_amd { +__u32 reserved;/* must be last */ +};Do not structure patches like this, introduce the full and complete ABI structure here.
Quoted 1 line
Jason
Noted. Thanks.
Suravee
Suravee Suthikulpanit
reviewerPATCH v2 5/26 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 , + reserved); +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 , + reserved); +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
Suravee Suthikulpanit
reviewerPATCH v2 6/26 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
Suravee Suthikulpanit
reviewerPATCH v2 7/26 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 | 18 +++++++++++++++++- drivers/iommu/amd/viommu.c | 11 +++++++++++ include/uapi/linux/iommufd.h | 2 ++ 5 files changed, 40 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 +17 -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 , + (unsigned long *)&data.out_vfmmio_mmap_offset); +if (ret) + goto err_mmap; + + aviommu->vfmmio_mmap_offset = data.out_vfmmio_mmap_offset; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , reserved);
@@ -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,8 @@ 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); +if (aviommu->vfmmio_mmap_offset) +iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);amd_iommu_gid_free (iommu, aviommu->gid); }
drivers/iommu/amd/viommu.c +11 -0
@@ -131,6 +131,17 @@ 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 ; +} +EXPORT_SYMBOL (amd_viommu_get_vfmmio_addr); +int __init amd_viommu_init (struct amd_iommu *iommu) {int ret;
include/uapi/linux/iommufd.h +2 -0
@@ -1084,9 +1084,11 @@ struct iommu_viommu_tegra241_cmdqv {/** * struct iommu_viommu_amd - AMD vIOMMU Interface (IOMMU_VIOMMU_TYPE_AMD) +* @out_vfmmio_mmap_offset: (out) mmap offset for vIOMMU VF-MMIO * @reserved: Must be zero */ struct iommu_viommu_amd { +__aligned_u64 out_vfmmio_mmap_offset;__u32 reserved;/* must be last */ };
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v2 07/26] iommu/amd: Add support for AMD vIOMMU VF MMIO region
drivers/iommu/amd/iommufd.c int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * · line 77
Quoted 9 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +5
-0@@ -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 , + (unsigned long *)&data.out_vfmmio_mmap_offset);
This should not be casted like this.
Quoted 18 lines
drivers/iommu/amd/iommufd.c +7
-0+if (ret) + goto err_mmap; + + aviommu->vfmmio_mmap_offset = data.out_vfmmio_mmap_offset; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , reserved);@@ -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;
drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 118
Quoted 5 lines
drivers/iommu/amd/iommufd.c +2
-0@@ -100,6 +114,8 @@ 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); +if (aviommu->vfmmio_mmap_offset) +iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);
I guess mmap_offset could be zero legitimately
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 07/26] iommu/amd: Add support for AMD vIOMMU VF MMIO region
Quoted 39 lines · Jason Gunthorpe
drivers/iommu/amd/iommufd.c int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * · line 77
Quoted 9 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +5
-0@@ -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 , + (unsigned long *)&data.out_vfmmio_mmap_offset);This should not be casted like this.
Quoted 18 lines
drivers/iommu/amd/iommufd.c +7
-0+if (ret) + goto err_mmap; + + aviommu->vfmmio_mmap_offset = data.out_vfmmio_mmap_offset; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , reserved);@@ -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;drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 118
Quoted 5 lines
drivers/iommu/amd/iommufd.c +2
-0@@ -100,6 +114,8 @@ 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); +if (aviommu->vfmmio_mmap_offset) +iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);I guess
mmap_offsetcould be zero legitimately
Quoted 1 line
Jason
I'll fix these in v3.
Suravee
Suravee Suthikulpanit
reviewerPATCH v2 8/26 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 * aviommu->vfmmio_mmap_offset = data.out_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 , reserved);
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 9/26 iommu/amd: Introduce domain for IOMMU Private Address (IPA) region
In reply to Suravee SuthikulpanitAMD vIOMMU introduces the IOMMU Private Address (IPA) region, which is used to manage data structures necessary for IOMMU virtualization within the guest.
Introduce a new domain specifically for IPA region for each IOMMU, which is stored in struct amd_iommu.viommu_pdom. This domain uses AMD IOMMU v1 page table.
For more info, please 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 | 5 +++++ drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/iommu.c | 9 ++++---- drivers/iommu/amd/viommu.c | 35 +++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +5 -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);
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -814,6 +814,9 @@ struct amd_iommu {struct ida gid_ida;/* guest IDs for this IOMMU */ bool gid_ida_inited; + +/* HW vIOMMU support */ +struct protection_domain *viommu_pdom; };static inline struct amd_iommu *dev_to_amd_iommu (struct device *dev)
drivers/iommu/amd/iommu.c +4 -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;
@@ -2726,12 +2726,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 +2746,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 +35 -0
@@ -131,6 +131,37 @@ static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu)return -ENOMEM ; } +static int viommu_private_space_init (struct amd_iommu *iommu) +{ +struct iommu_domain *dom; +struct protection_domain *pdom; +struct pt_iommu_amdv1_hw_info pt_info; + +/* +* Setup page table root pointer, Guest MMIO and +* Cmdbuf Dirty Status regions. +*/ + dom =amd_iommu_domain_alloc_paging_v1 (&iommu->dev->dev,0 ); +if (!dom) { +pr_err ("%s: Failed to initialize private space\n" , __func__); + goto err_out; + } + + pdom =to_pdomain (dom); + iommu->viommu_pdom = pdom; + +pt_iommu_amdv1_hw_info (&pdom->amdv1, &pt_info); +pr_debug ("%s: devid=%#x, pte_root=%#llx\n" , + __func__, iommu->devid, + (unsigned long long )pt_info.host_pt_root); + +return 0 ; +err_out: +if (dom) +amd_iommu_domain_free (dom); +return -ENOMEM ; +} +/* * Returns VF MMIO BAR offset for the give guest ID which will be * mapped to guest vIOMMU 3rd 4K MMIO address
@@ -160,5 +191,9 @@ int __init amd_viommu_init(struct amd_iommu *iommu)amd_viommu_gid_ida_init (iommu); + ret =viommu_private_space_init (iommu); +if (ret) +return ret; +return 0 ; }
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 10/26 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/viommu.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
drivers/iommu/amd/viommu.c +31 -0
@@ -173,6 +173,35 @@ u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) }EXPORT_SYMBOL (amd_viommu_get_vfmmio_addr); +/* Set DTE for IOMMU device */ +static void set_iommu_dte (struct amd_iommu *iommu) +{ +u64 dte0, dte1; +u16 devid = iommu->devid; +struct pt_iommu_amdv1_hw_info pt_info; +struct protection_domain *pdom = iommu->viommu_pdom; +struct dev_table_entry *dev_table =get_dev_table (iommu); + +pt_iommu_amdv1_hw_info (&pdom->amdv1, &pt_info); + +pr_debug ("%s: host_pt_root=%#llx, mode=%#x\n" , + __func__, pt_info.host_pt_root, pt_info.mode); + + dte0 =FIELD_PREP (DTE_HOST_TRP , pt_info.host_pt_root >>12 ); + dte0 |= (pt_info.mode &DEV_ENTRY_MODE_MASK ) <<DEV_ENTRY_MODE_SHIFT ; + dte0 |=DTE_FLAG_IR |DTE_FLAG_IW |DTE_FLAG_V |DTE_FLAG_TV ; + + dte1 = dev_table[devid].data[1 ]; + dte1 &= ~DTE_DOMID_MASK ; + dte1 |= pdom->id; + + dev_table[devid].data[1 ] = dte1; + dev_table[devid].data[0 ] = dte0; + +iommu_flush_dte (iommu, devid); +amd_iommu_completion_wait (iommu); +} +int __init amd_viommu_init (struct amd_iommu *iommu) {int ret;
@@ -195,5 +224,7 @@ int __init amd_viommu_init(struct amd_iommu *iommu)if (ret)return ret; +set_iommu_dte (iommu); +return 0 ; }
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v2 10/26] iommu/amd: Assign IOMMU Private Address domain to IOMMU
Quoted 28 lines · Suravee Suthikulpanit
By 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.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/viommu.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)drivers/iommu/amd/viommu.c +11
-0@@ -173,6 +173,35 @@ u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) }EXPORT_SYMBOL (amd_viommu_get_vfmmio_addr); +/* Set DTE for IOMMU device */ +static void set_iommu_dte (struct amd_iommu *iommu) +{ +u64 dte0, dte1; +u16 devid = iommu->devid; +struct pt_iommu_amdv1_hw_info pt_info; +struct protection_domain *pdom = iommu->viommu_pdom; +struct dev_table_entry *dev_table =get_dev_table (iommu); + +pt_iommu_amdv1_hw_info (&pdom->amdv1, &pt_info); +
drivers/iommu/amd/viommu.c u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) · line 196
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+pr_debug ("%s: host_pt_root=%#llx, mode=%#x\n" , + __func__, pt_info.host_pt_root, pt_info.mode); + + dte0 =FIELD_PREP (DTE_HOST_TRP , pt_info.host_pt_root >>12 ); + dte0 |= (pt_info.mode &DEV_ENTRY_MODE_MASK ) <<DEV_ENTRY_MODE_SHIFT ; + dte0 |=DTE_FLAG_IR |DTE_FLAG_IW |DTE_FLAG_V |DTE_FLAG_TV ; + + dte1 = dev_table[devid].data[1 ]; + dte1 &= ~DTE_DOMID_MASK ; + dte1 |= pdom->id;
Why is this editing the DTE in place!?
The special DTE should be entirely deterministic. Call amd_iommu_make_clear_dte(), and use amd_iommu_update_dte() to write it like every other DTE.
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 10/26] iommu/amd: Assign IOMMU Private Address domain to IOMMU
Quoted 42 lines · Jason Gunthorpe
Quoted 28 lines · Suravee Suthikulpanit
By 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.
Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/viommu.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)drivers/iommu/amd/viommu.c +11
-0@@ -173,6 +173,35 @@ u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) }EXPORT_SYMBOL (amd_viommu_get_vfmmio_addr); +/* Set DTE for IOMMU device */ +static void set_iommu_dte (struct amd_iommu *iommu) +{ +u64 dte0, dte1; +u16 devid = iommu->devid; +struct pt_iommu_amdv1_hw_info pt_info; +struct protection_domain *pdom = iommu->viommu_pdom; +struct dev_table_entry *dev_table =get_dev_table (iommu); + +pt_iommu_amdv1_hw_info (&pdom->amdv1, &pt_info); +drivers/iommu/amd/viommu.c u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid) · line 196
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+pr_debug ("%s: host_pt_root=%#llx, mode=%#x\n" , + __func__, pt_info.host_pt_root, pt_info.mode); + + dte0 =FIELD_PREP (DTE_HOST_TRP , pt_info.host_pt_root >>12 ); + dte0 |= (pt_info.mode &DEV_ENTRY_MODE_MASK ) <<DEV_ENTRY_MODE_SHIFT ; + dte0 |=DTE_FLAG_IR |DTE_FLAG_IW |DTE_FLAG_V |DTE_FLAG_TV ; + + dte1 = dev_table[devid].data[1 ]; + dte1 &= ~DTE_DOMID_MASK ; + dte1 |= pdom->id;Why is this editing the DTE in place!?
Quoted 5 lines
The special DTE should be entirely deterministic. Call
amd_iommu_make_clear_dte(), and useamd_iommu_update_dte()to write it like every other DTE.Jason
I missed this one. I'll fix in v3.
Suravee
Suravee Suthikulpanit
reviewerPATCH v2 11/26 iommu/amd: Allocate and map vIOMMU private regions
In reply to Suravee SuthikulpanitThe AMD IOMMU Private Address (IPA) region is allocated and mapped during IOMMU driver initialization. According to the specification, 8MB is needed. Since the hardware does not require the IPA region to be physically contiguous, split the IPA region into 4 2MB subregions to match hugepage granularity and create mapping in the v1 page-table for each IOMMU.
--- drivers/iommu/amd/amd_iommu.h | 3 ++ drivers/iommu/amd/amd_iommu_types.h | 8 +++ drivers/iommu/amd/iommu.c | 16 ++++++ drivers/iommu/amd/viommu.c | 79 +++++++++++++++++++++++++++-- 4 files changed, 101 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +3 -0
@@ -105,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_flush_private_vm_region (struct amd_iommu *iommu,struct protection_domain *pdom, +u64 address,size_t size); +#ifdef CONFIG_IRQ_REMAP int amd_iommu_create_irq_domain (struct amd_iommu *iommu);#else
drivers/iommu/amd/amd_iommu_types.h +8 -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
@@ -817,6 +824,7 @@ struct amd_iommu {/* 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 +16 -0
@@ -1808,6 +1808,22 @@ static int domain_flush_pages_v1(struct protection_domain *pdom,return ret; } +int amd_iommu_flush_private_vm_region (struct amd_iommu *iommu,struct protection_domain *pdom, +u64 address,size_t size) +{ +int ret; +struct iommu_cmd cmd; + +build_inv_iommu_pages (&cmd, address, size, pdom->id,0 , false); + + ret =iommu_queue_command (iommu, &cmd); +if (ret) +return ret; + +amd_iommu_completion_wait (iommu); +return ret; +} +/* * TLB invalidation function which is called from the mapping functions. * It flushes range of PTEs of the domain.
drivers/iommu/amd/viommu.c +74 -5
104 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v2 11/26] iommu/amd: Allocate and map vIOMMU private regions
Quoted 9 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +3
-0@@ -131,8 +131,66 @@ static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu)return -ENOMEM ; } +static void *alloc_private_subregion (struct amd_iommu *iommu,u64 base,size_t size) +{ +int ret;
drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 146
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+void *region; +int nid = iommu && iommu->dev ?dev_to_node (&iommu->dev->dev) :NUMA_NO_NODE ; + + region = (void *)iommu_alloc_pages_node_sz (nid,GFP_KERNEL | __GFP_ZERO, size); +if (!region) +return NULL ; + + ret =set_memory_uc ((unsigned long )region, size >>PAGE_SHIFT ); +if (ret) + goto err_out;
Why?
drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 158
Quoted 11 lines
drivers/iommu/amd/viommu.c +11
-0+ ret =iommu_map (&iommu->viommu_pdom->domain, base, +iommu_virt_to_phys (region), size, +IOMMU_PROT_IR |IOMMU_PROT_IW ,GFP_KERNEL ); + +if (ret) + goto cleanup_mem_attr; + +pr_debug ("%s: base=%#llx, size=%#lx, subregion=%#llx(%#llx)\n" , + __func__, base, size, (unsigned long long )region,iommu_virt_to_phys (region)); + +amd_iommu_flush_private_vm_region (iommu, iommu->viommu_pdom, base, size);
Why? Is there suddenly negative caching for this mode?
Quoted 19 lines
drivers/iommu/amd/viommu.c +19
-0+return region; +cleanup_mem_attr: +set_memory_wb ((unsigned long )region, size >>PAGE_SHIFT ); +err_out: +iommu_free_pages (region); +return NULL ; +} + +static void viommu_private_space_uninit (struct amd_iommu *iommu) +{ +int i; +struct iommu_domain *dom; + +if (!iommu->viommu_pdom) +return ; + +for (i =0 ; i <VIOMMU_PRIV_SUBREGION_CNT ; i++) { +if (!iommu->viommu_priv_region[i]) +continue ;
drivers/iommu/amd/viommu.c static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu) · line 188
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+set_memory_wb ((unsigned long )iommu->viommu_priv_region[i], +VIOMMU_PRIV_SUBREGION_SIZE >>PAGE_SHIFT ); +iommu_free_pages (iommu->viommu_priv_region[i]); + iommu->viommu_priv_region[i] =NULL ; + } + + dom = &iommu->viommu_pdom->domain; +amd_iommu_domain_free (dom); + iommu->viommu_pdom =NULL ; +}
Shouldn't something flush the DID before freeing the domain?
Quoted 33 lines
drivers/iommu/amd/viommu.c +18
-1static int viommu_private_space_init (struct amd_iommu *iommu) { +int i; +u64 base;struct iommu_domain *dom;struct protection_domain *pdom;struct pt_iommu_amdv1_hw_info pt_info;@@ -144,22 +202,33 @@ static int viommu_private_space_init(struct amd_iommu *iommu) dom =amd_iommu_domain_alloc_paging_v1 (&iommu->dev->dev,0 );if (!dom) {pr_err ("%s: Failed to initialize private space\n" , __func__); - goto err_out; +return -ENOMEM ; } pdom =to_pdomain (dom); iommu->viommu_pdom = pdom; +/* +* Each private region requires to 8MB of memory to be allocated +* and mapped. Split the region into 4 x 2MB-subregion. +*/ +for (i =0 ; i <VIOMMU_PRIV_SUBREGION_CNT ; i++) { + base =VIOMMU_PRIV_REGION_BASE + (i *VIOMMU_PRIV_SUBREGION_SIZE ); + iommu->viommu_priv_region[i] =alloc_private_subregion (iommu, base, +VIOMMU_PRIV_SUBREGION_SIZE ); +if (!iommu->viommu_priv_region[i]) { +pr_err ("%s: Failed to allocate vIOMMU private subregion %d\n" , __func__, i); +viommu_private_space_uninit (iommu); +return -ENOMEM ; + } + } +
drivers/iommu/amd/viommu.c static int viommu_private_space_init(struct amd_iommu *iommu) · line 162
Quoted 10 lines
drivers/iommu/amd/viommu.c +0
-4pt_iommu_amdv1_hw_info (&pdom->amdv1, &pt_info);pr_debug ("%s: devid=%#x, pte_root=%#llx\n" , __func__, iommu->devid, (unsigned long long )pt_info.host_pt_root);return 0 ; -err_out: -if (dom) -amd_iommu_domain_free (dom); -return -ENOMEM ;
Why is the error handling being deleted now? You should organize your patches to avoid churn like this.
Jason
Suravee Suthikulpanit
reviewerPATCH v2 12/26 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, flush the private VM mapping, and tear down on VM destroy.
Export amd_iommu_iotlb_sync() and use it for nested domain iotlb_sync so nested attach paths can flush gathered IOTLB state.
--- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/iommu.c | 4 +-- drivers/iommu/amd/nested.c | 1 + drivers/iommu/amd/viommu.c | 59 +++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +0 -0
--- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/iommu.c | 4 +-- drivers/iommu/amd/nested.c | 1 + drivers/iommu/amd/viommu.c | 55 +++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -0
@@ -105,6 +105,8 @@ 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); +void amd_iommu_iotlb_sync (struct iommu_domain *domain, +struct iommu_iotlb_gather *gather);int amd_iommu_flush_private_vm_region (struct amd_iommu *iommu,struct protection_domain *pdom,u64 address,size_t size);
drivers/iommu/amd/iommu.c +2 -2
@@ -2729,8 +2729,8 @@ static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain)spin_unlock_irqrestore (&dom->lock, flags); } -static void amd_iommu_iotlb_sync (struct iommu_domain *domain, -struct iommu_iotlb_gather *gather) +void amd_iommu_iotlb_sync (struct iommu_domain *domain, +struct iommu_iotlb_gather *gather) {struct protection_domain *dom =to_pdomain (domain);unsigned long flags;
drivers/iommu/amd/nested.c +1 -0
@@ -291,4 +291,5 @@ static void nested_domain_free(struct iommu_domain *dom)static const struct iommu_domain_ops nested_domain_ops = { .attach_dev = nested_attach_device, .free = nested_domain_free, + .iotlb_sync = amd_iommu_iotlb_sync, };
drivers/iommu/amd/viommu.c +55 -0
58 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 12/26] iommu/amd: Add per-VM private IPA alloc/map helpers
drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 345
Quoted 13 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +12
-0+static void __maybe_unused free_private_vm_region (struct amd_iommu *iommu,u64 **entry, + u64 base, size_t size, u16 gid) +{ + size_t unmapped; + u64 addr = base + (gid * size); + + 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); + + if (!iommu || !iommu->viommu_pdom) + return;
Should check if the page pointer *entry is non-NULL before operating on it. *entry will be NULL if the caller encounters an error during alloc_private_vm_region(), it may attempt to unmap and free a NULL pointer below:
Quoted 9 lines
drivers/iommu/amd/viommu.c +9
-0+ + unmapped = iommu_unmap(&iommu->viommu_pdom->domain, addr, size); + if (unmapped != size) + pr_warn("%s: unmapped %#zx of %#lx at %#llx\n", __func__, unmapped, size, addr); + + set_memory_wb((unsigned long)*entry, size >> PAGE_SHIFT); + iommu_free_pages(*entry); + *entry = NULL; +}
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 12/26] iommu/amd: Add per-VM private IPA alloc/map helpers
Quoted 18 lines · Weinan Liu
drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 345
Quoted 13 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +12
-0+static void __maybe_unused free_private_vm_region (struct amd_iommu *iommu,u64 **entry, + u64 base, size_t size, u16 gid) +{ + size_t unmapped; + u64 addr = base + (gid * size); + + 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); + + if (!iommu || !iommu->viommu_pdom) + return;Should check if the page pointer *entry is non-NULL before operating on it. *entry will be NULL if the caller encounters an error during
alloc_private_vm_region(), it may attempt to unmap and free a NULL pointer below:
Thanks. I'll take care of this in V3.
Suravee
Jason Gunthorpe
maintainerRe: [PATCH v2 12/26] iommu/amd: Add per-VM private IPA alloc/map helpers
In reply to Suravee Suthikulpanitdrivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 310
Quoted 10 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +10
-0+static int __maybe_unused alloc_private_vm_region (struct amd_iommu *iommu,u64 **entry, +u64 base,size_t size,u16 gid) +{ +int ret; +u64 addr = base + (gid * size); +int nid = iommu && iommu->dev ?dev_to_node (&iommu->dev->dev) :NUMA_NO_NODE ; + + *entry = (void *)iommu_alloc_pages_node_sz (nid,GFP_KERNEL | __GFP_ZERO, size); +if (!*entry) +return -ENOMEM ;
No cast
Quoted 11 lines
drivers/iommu/amd/viommu.c +11
-0+ ret =set_memory_uc ((unsigned long )*entry, size >>PAGE_SHIFT ); +if (ret) + goto err_out; + +pr_debug ("%s: entry=%#llx(%#llx), addr=%#llx, size=%#lx\n" , __func__, + (unsigned long long )*entry,iommu_virt_to_phys (*entry), addr, size); + + ret =iommu_map (&iommu->viommu_pdom->domain, addr, +iommu_virt_to_phys (*entry), size, +IOMMU_PROT_IR |IOMMU_PROT_IW ,GFP_KERNEL ); +if (ret)
drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 332
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+ goto cleanup_mem_attr; + +return amd_iommu_flush_private_vm_region (iommu, iommu->viommu_pdom, addr, size); +cleanup_mem_attr: +set_memory_wb ((unsigned long )*entry, size >>PAGE_SHIFT ); +err_out: +iommu_free_pages (*entry); + *entry =NULL ; +return ret; +}
This all seems duplicated from the prior patch, you should try to make one helper that does this alloc and map sequence.
Quoted 6 lines
drivers/iommu/amd/viommu.c +6
-0+static void __maybe_unused free_private_vm_region (struct amd_iommu *iommu,u64 **entry, +u64 base,size_t size,u16 gid) +{ +size_t unmapped; +u64 addr = base + (gid * size); +
drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 349
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+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); + +if (!iommu || !iommu->viommu_pdom) +return ; + + unmapped =iommu_unmap (&iommu->viommu_pdom->domain, addr, size); +if (unmapped != size) +pr_warn ("%s: unmapped %#zx of %#lx at %#llx\n" , __func__, unmapped, size, addr);
No flush? Or does the gather work for the special domain?
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 12/26] iommu/amd: Add per-VM private IPA alloc/map helpers
Quoted 58 lines · Jason Gunthorpe
drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 310
Quoted 10 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +10
-0+static int __maybe_unused alloc_private_vm_region (struct amd_iommu *iommu,u64 **entry, +u64 base,size_t size,u16 gid) +{ +int ret; +u64 addr = base + (gid * size); +int nid = iommu && iommu->dev ?dev_to_node (&iommu->dev->dev) :NUMA_NO_NODE ; + + *entry = (void *)iommu_alloc_pages_node_sz (nid,GFP_KERNEL | __GFP_ZERO, size); +if (!*entry) +return -ENOMEM ;No cast
Quoted 11 lines
drivers/iommu/amd/viommu.c +11
-0+ ret =set_memory_uc ((unsigned long )*entry, size >>PAGE_SHIFT ); +if (ret) + goto err_out; + +pr_debug ("%s: entry=%#llx(%#llx), addr=%#llx, size=%#lx\n" , __func__, + (unsigned long long )*entry,iommu_virt_to_phys (*entry), addr, size); + + ret =iommu_map (&iommu->viommu_pdom->domain, addr, +iommu_virt_to_phys (*entry), size, +IOMMU_PROT_IR |IOMMU_PROT_IW ,GFP_KERNEL ); +if (ret)drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 332
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+ goto cleanup_mem_attr; + +return amd_iommu_flush_private_vm_region (iommu, iommu->viommu_pdom, addr, size); +cleanup_mem_attr: +set_memory_wb ((unsigned long )*entry, size >>PAGE_SHIFT ); +err_out: +iommu_free_pages (*entry); + *entry =NULL ; +return ret; +}This all seems duplicated from the prior patch, you should try to make one helper that does this alloc and map sequence.
Quoted 6 lines
drivers/iommu/amd/viommu.c +6
-0+static void __maybe_unused free_private_vm_region (struct amd_iommu *iommu,u64 **entry, +u64 base,size_t size,u16 gid) +{ +size_t unmapped; +u64 addr = base + (gid * size); +drivers/iommu/amd/viommu.c int __init amd_viommu_init(struct amd_iommu *iommu) · line 349
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+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); + +if (!iommu || !iommu->viommu_pdom) +return ; + + unmapped =iommu_unmap (&iommu->viommu_pdom->domain, addr, size); +if (unmapped != size) +pr_warn ("%s: unmapped %#zx of %#lx at %#llx\n" , __func__, unmapped, size, addr);No flush? Or does the gather work for the special domain?
Quoted 1 line
Jason
I'll take care of this in V3.
Suravee
Jason Gunthorpe
maintainerRe: [PATCH v2 12/26] iommu/amd: Add per-VM private IPA alloc/map helpers
In reply to Suravee SuthikulpanitQuoted 2 lines · Suravee Suthikulpanit
Export
amd_iommu_iotlb_sync()and use it for nested domainiotlb_syncso nested attach paths can flush gathered IOTLB state.
[ ..]
drivers/iommu/amd/nested.c static void nested_domain_free(struct iommu_domain *dom) · line 295
Quoted 8 lines
drivers/iommu/amd/nested.c +1
-0@@ -291,4 +291,5 @@ static void nested_domain_free(struct iommu_domain *dom)static const struct iommu_domain_ops nested_domain_ops = { .attach_dev = nested_attach_device, .free = nested_domain_free, + .iotlb_sync = amd_iommu_iotlb_sync, };
I don't get it, what is all of this for?
iotlb_sync should never be called on an IOMMU_DOMAIN_NESTED.
"nested attach paths" should never have gathered IOTLB state.
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 12/26] iommu/amd: Add per-VM private IPA alloc/map helpers
Quoted 18 lines · Jason Gunthorpe
Quoted 2 lines · Suravee Suthikulpanit
Export
amd_iommu_iotlb_sync()and use it for nested domainiotlb_syncso nested attach paths can flush gathered IOTLB state.[ ..]
drivers/iommu/amd/nested.c static void nested_domain_free(struct iommu_domain *dom) · line 295
Quoted 8 lines
drivers/iommu/amd/nested.c +1
-0@@ -291,4 +291,5 @@ static void nested_domain_free(struct iommu_domain *dom)static const struct iommu_domain_ops nested_domain_ops = { .attach_dev = nested_attach_device, .free = nested_domain_free, + .iotlb_sync = amd_iommu_iotlb_sync, };I don't get it, what is all of this for?
Quoted 5 lines
iotlb_syncshould never be called on anIOMMU_DOMAIN_NESTED."nested attach paths" should never have gathered IOTLB state.
Jason
Right. I missed this. I'll fix in v3.
Thanks.
Suravee
Re: [PATCH v2 12/26] iommu/amd: Add per-VM private IPA alloc/map helpers
In reply to Suravee SuthikulpanitQuoted 19 lines · Jason Gunthorpe
Quoted 2 lines · Suravee Suthikulpanit
Export
amd_iommu_iotlb_sync()and use it for nested domainiotlb_syncso nested attach paths can flush gathered IOTLB state.[ ..]
drivers/iommu/amd/nested.c static void nested_domain_free(struct iommu_domain *dom) · line 295
Quoted 8 lines
drivers/iommu/amd/nested.c +1
-0@@ -291,4 +291,5 @@ static void nested_domain_free(struct iommu_domain *dom)static const struct iommu_domain_ops nested_domain_ops = { .attach_dev = nested_attach_device, .free = nested_domain_free, + .iotlb_sync = amd_iommu_iotlb_sync, };I don't get it, what is all of this for?
Quoted 5 lines
iotlb_syncshould never be called on anIOMMU_DOMAIN_NESTED."nested attach paths" should never have gathered IOTLB state.
Jason
BTW, amd_iommu_iotlb_sync() is designed to flush a protection_domain and cannot be used for a nested_domain.
Specifically, the current implementation explicitly casts the domain to a protection_domain:
```
void amd_iommu_iotlb_sync(struct iommu_domain *domain,
struct iommu_iotlb_gather *gather)
{
struct protection_domain *dom = to_pdomain(domain);
```Suravee Suthikulpanit
reviewerPATCH v2 13/26 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 , reserved);
@@ -120,6 +124,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)if (aviommu->vfmmio_mmap_offset)iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);amd_iommu_gid_free (iommu, aviommu->gid); +amd_viommu_uninit_one (iommu, aviommu); }/*
drivers/iommu/amd/viommu.c +52 -0
61 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 13/26] iommu/amd: Add helper functions to manage DevID / DomID mapping tables
Quoted 16 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +4
-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, reserved);
drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 129
Quoted 6 lines
drivers/iommu/amd/iommufd.c +1
-0@@ -120,6 +124,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) if (aviommu->vfmmio_mmap_offset) iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); amd_iommu_gid_free(iommu, aviommu->gid); + amd_viommu_uninit_one(iommu, aviommu); }
The finalization order should be the reverse of the initialization order.
If amd_iommu_gid_free() is called before amd_viommu_uninit_one(), the gid could be reallocated to a new vIOMMU instance before the cleanup is complete.
Please consider moving amd_viommu_uninit_one() before the GID free call.
Quoted 5 lines
drivers/iommu/amd/viommu.c +1
-0+
[...]
Quoted 14 lines
drivers/iommu/amd/viommu.c +14
-0+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); +} +
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 13/26] iommu/amd: Add helper functions to manage DevID / DomID mapping tables
Quoted 26 lines · Weinan Liu
Quoted 16 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +4
-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, reserved);drivers/iommu/amd/iommufd.c static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) · line 129
Quoted 6 lines
drivers/iommu/amd/iommufd.c +1
-0@@ -120,6 +124,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu) if (aviommu->vfmmio_mmap_offset) iommufd_viommu_destroy_mmap(&aviommu->core, aviommu->vfmmio_mmap_offset); amd_iommu_gid_free(iommu, aviommu->gid); + amd_viommu_uninit_one(iommu, aviommu); }The finalization order should be the reverse of the initialization order.
Quoted 4 lines
If
amd_iommu_gid_free()is called beforeamd_viommu_uninit_one(), the gid could be reallocated to a new vIOMMU instance before the cleanup is complete.
Please consider movingamd_viommu_uninit_one()before the GID free call.
Thanks. I'll take care of this in V3.
Suravee
Suravee Suthikulpanit
reviewerPATCH v2 14/26 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 | 12 +++++++++++ drivers/iommu/amd/iommufd.c | 33 +++++++++++++++++++++++++++++ drivers/iommu/amd/nested.c | 12 +++++++++++ 3 files changed, 57 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +12 -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] */
@@ -894,6 +899,9 @@ struct iommu_dev_data {bool defer_attach;struct ratelimit_state rs;/* Ratelimit IOPF messages */ + +u16 gid;/* Guest ID */ +u16 gDevId;/* Guest Device ID */ };/* Map HPET and IOAPIC ids to the devid used by the IOMMU */
@@ -1121,6 +1129,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 +33 -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;
@@ -127,6 +128,36 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)amd_viommu_uninit_one (iommu, aviommu); } +/* +* 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 ; + } + + dev_data->gid = aviommu->gid; + dev_data->gDevId = vdev->virt_id; +pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#x\n" , __func__, + dev_data->gid,pci_dev_id (pdev), dev_data->gDevId); + +return 0 ; +} +/* * See include/linux/iommufd.h * struct iommufd_viommu_ops - vIOMMU specific operations
@@ -134,4 +165,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 +12 -0
@@ -227,6 +227,18 @@ 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 , + dev_data->gDevId); + +/* GuestID */ + new->data[3 ] |=FIELD_PREP (DTE_VIOMMU_GUESTID_MASK , + dev_data->gid); + } static int nested_attach_device(struct iommu_domain *dom, struct device *dev,
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 15/26 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 | 5 ++++ drivers/iommu/amd/viommu.c | 46 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+)
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 +5 -0
@@ -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;
@@ -245,6 +246,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 ;
@@ -262,6 +264,9 @@ static int nested_attach_device(struct iommu_domain *dom, struct device *dev,amd_iommu_update_dte (iommu, dev_data, &new); + ret =amd_viommu_domain_id_update (iommu, dev_data->gid, + ndom->gdom_info->hdom_id, ndom->gdom_id); +mutex_unlock (&dev_data->mutex);return ret;
drivers/iommu/amd/viommu.c +46 -0
72 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 16/26 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 | 8 ++++++ drivers/iommu/amd/iommufd.c | 3 ++ drivers/iommu/amd/viommu.c | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+)
drivers/iommu/amd/amd_viommu.h +8 -0
@@ -20,6 +20,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 +48,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 +3 -0
@@ -137,6 +137,7 @@ 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__);
@@ -155,6 +156,8 @@ static int _amd_viommu_vdevice_init(struct iommufd_vdevice *vdev)pr_debug ("%s: gid=%#x, hdev_id=%#x, gdev_id=%#x\n" , __func__, dev_data->gid,pci_dev_id (pdev), dev_data->gDevId); +amd_viommu_set_device_mapping (iommu,pci_dev_id (pdev), dev_data->gid, dev_data->gDevId); +return 0 ; }
drivers/iommu/amd/viommu.c +52 -0
70 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 17/26 iommu/amd: Pass KVM FD from userspace when initializing vIOMMU
In reply to Suravee SuthikulpanitAMD HW-vIOMMU feature requires IOMMU driver to keep track of each guest. This information is used by AMD IOMMU driver to manage the following:
* GID: Each guest is assigned a unique 16-bit Guest ID (GID), which is used to index into various data structures for configuring the hardware.
* Translation-devid: Each guest has one GPA->SPA mapping, which requires one trans_devid to program the v1 page table in the DTE[trans_devid].
* Extended interrupt remapping: The AMD HW-vIOMMU uses AVIC to implement extended interrupt remapping to virtualize Event/PPR log interrupts. A KVM reference is neeed to get the AVIC vm_id.
The KVM FD can be used for these purposes.
--- drivers/iommu/amd/amd_iommu_types.h | 2 ++ drivers/iommu/amd/iommufd.c | 27 ++++++++++++++++++++++++++- include/uapi/linux/iommufd.h | 2 ++ 3 files changed, 30 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +2 -0
@@ -547,6 +547,8 @@ struct amd_iommu_viommu {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 */ +u32 kvmfd;/* KVM fd from VMM */ +void *kvm;/* Hold struct kvm pointer */ /* * Per-vIOMMU guest domain ID to host domain ID mapping.
drivers/iommu/amd/iommufd.c +26 -1
@@ -4,6 +4,8 @@*/ #include <linux/iommu.h> +#include <linux/file.h> +#include <linux/amd-iommu.h> #include "iommufd.h" #include "amd_iommu.h"
@@ -42,13 +44,27 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi return VIOMMU_STRUCT_SIZE(struct amd_iommu_viommu, core); } +static void *get_kvm_handler (u32 kvmfd) +{ +struct fd f; + + f =fdget (kvmfd); + +if (fd_empty (f)) { +pr_warn ("%s: fdget failed\n" , __func__); +return NULL ; + } + +return fd_file (f)->private_data; +} +int amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent,const struct iommu_user_data *user_data) {int ret;phys_addr_t page_base;unsigned long flags; -struct iommu_viommu_amd data; +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);
@@ -81,6 +97,13 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * aviommu->vfmmio_mmap_offset = data.out_vfmmio_mmap_offset; + aviommu->kvm =get_kvm_handler (data.kvmfd); +if (aviommu->kvm ==NULL ) { +pr_err ("%s: Failed to get KVM handler (kvmfd=%#x)\n" , __func__, data.kvmfd); + ret = -EINVAL ; + goto err_kvmfd; + } +/* Reset vIOMMU MMIOs to initialize the vIOMMU */ iommu_reset_vmmio (iommu, aviommu->gid);
@@ -94,6 +117,7 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *if (ret) goto err_init; + aviommu->kvmfd = data.kvmfd; viommu->ops = &amd_viommu_ops;spin_lock_irqsave (&pdom->lock, flags);
@@ -102,6 +126,7 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return 0 ; err_init: +err_kvmfd:iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); err_mmap:amd_iommu_gid_free (iommu, aviommu->gid);
include/uapi/linux/iommufd.h +2 -0
@@ -1085,10 +1085,12 @@ struct iommu_viommu_tegra241_cmdqv {/** * struct iommu_viommu_amd - AMD vIOMMU Interface (IOMMU_VIOMMU_TYPE_AMD) * @out_vfmmio_mmap_offset: (out) mmap offset for vIOMMU VF-MMIO +* @kvmfd: KVM FD handler * @reserved: Must be zero */ struct iommu_viommu_amd {__aligned_u64 out_vfmmio_mmap_offset; +__u32 kvmfd;__u32 reserved;/* must be last */ };
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 18/26 iommu/amd: Add translation DTE and VFctrl TransDevID helpers
In reply to Suravee SuthikulpanitThe hardware vIOMMU uses a per-VM 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 nested IOMMU 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, using the AMDv1 page table root and mode from the protection domain. 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 | 7 ++++ drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/iommu.c | 51 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+)
drivers/iommu/amd/amd_iommu.h +7 -0
@@ -216,6 +216,13 @@ void amd_iommu_update_dte(struct amd_iommu *iommu, struct dev_table_entry *new);int amd_iommu_completion_wait (struct amd_iommu *iommu); +void amd_iommu_set_translate_dte (struct amd_iommu *iommu,u16 gid, +struct protection_domain *pdom, +u32 devid); +void amd_iommu_clear_translate_dte (struct amd_iommu *iommu,u16 gid,u32 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 iommu_dev_data *dev_data,struct dev_table_entry *new) {
drivers/iommu/amd/amd_iommu_types.h +1 -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
drivers/iommu/amd/iommu.c +51 -0
57 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v2 18/26] iommu/amd: Add translation DTE and VFctrl TransDevID helpers
Quoted 11 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommu.c +11
-0+void amd_iommu_set_translate_dte (struct amd_iommu *iommu,u16 gid, +struct protection_domain *pdom, +u32 devid) +{ +u64 tmp0 =0ULL , tmp1 =0ULL ; +struct pt_iommu_amdv1_hw_info pt_info; +struct dev_table_entry *dev_table =get_dev_table (iommu); + +pt_iommu_amdv1_hw_info (&pdom->amdv1, &pt_info); + + pr_debug("%s: gid=%#x, iommu_devid=%#x, devid=%#x, host_pt_root=%#llx, mode=%#x\n",
drivers/iommu/amd/iommu.c static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain) · line 3275
Quoted 10 lines
drivers/iommu/amd/iommu.c +10
-0+ __func__, gid, iommu->devid, devid, pt_info.host_pt_root, pt_info.mode); + +/* Setup DTE for v1 page table at the offset specified by devid */ + tmp0 |=FIELD_PREP (DTE_HOST_TRP , pt_info.host_pt_root >>12 ); + tmp0 |=FIELD_PREP (DTE_MODE_MASK , pt_info.mode); + tmp0 |= (DTE_FLAG_IR |DTE_FLAG_IW |DTE_FLAG_TV |DTE_FLAG_V ); + tmp1 |=FIELD_PREP (DTE_DOMID_MASK , pdom->id); + + dev_table[devid].data[0 ] = tmp0; + dev_table[devid].data[1 ] = tmp1;
Please stop editing DTEs in place.
This looks like it is writing some otherwise unused DTE, it should fully initialize it and use the normal flow to write it.
Jason
Suravee Suthikulpanit
reviewerPATCH v2 19/26 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.
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 state machine.
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 | 80 +++++++++++++++++++++++++++++ 6 files changed, 125 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
@@ -216,6 +216,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 +void amd_iommu_set_translate_dte (struct amd_iommu *iommu,u16 gid,struct protection_domain *pdom,u32 devid);
drivers/iommu/amd/amd_iommu_types.h +17 -0
@@ -615,6 +615,14 @@ 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 , +TRANS_DEVID_ALLOCATED , +}; +#endif +/* * This structure contains information about one PCI segment in the system. */
@@ -676,6 +684,15 @@ struct amd_iommu_pci_seg {* parsing time. */ struct list_head unity_map; + +#ifdef CONFIG_AMD_IOMMU_IOMMUFD +/* +* Per-segment translate-device-id allocation. The xarray is indexed by +* the translate-device-id. The value is the state (enum trans_devid_state). +*/ +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
@@ -3055,6 +3055,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 +80 -0
80 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 20/26 iommu/amd: Reserve translate-device-id for PCI requestor aliases
In reply to Suravee SuthikulpanitThe per-segment translate-device-id (trans_devid) pool hands out numeric device-table indices used by the AMD vIOMMU / iommufd path (e.g. mapping a KVM file descriptor to a shared trans_devid). 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
@@ -221,6 +221,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
@@ -3065,6 +3065,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 v2 21/26 iommu/amd: Map kvmfd to shared translate device ID for vIOMMU
In reply to Suravee SuthikulpanitAdd per-PCI-segment kvmfd xarray amd_iommu_kvmfd_trans_entry with refcounted so all vIOMMUs for one VM share one translate-device-id and GPA->SPA DTE.
Expose helper functions:
* amd_iommu_get_trans_devid_by_kvmfd()
* amd_iommu_free_trans_devid_by_kvmfd()
* amd_iommu_trans_devid_alloc()
and extend segment init/fini for the kvmfd map.
Wire iommufd vIOMMU init/destroy to obtain the ID, program the translation DTE and VFctrl TransDevID field, and tear down on error or uninit. Clear translation state in amd_viommu_uninit_one().
--- drivers/iommu/amd/amd_iommu.h | 4 + drivers/iommu/amd/amd_iommu_types.h | 13 ++ drivers/iommu/amd/iommu.c | 2 +- drivers/iommu/amd/iommufd.c | 18 ++- drivers/iommu/amd/trans_devid.c | 194 +++++++++++++++++++++++++++- drivers/iommu/amd/viommu.c | 3 + 6 files changed, 230 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_iommu.h +4 -0
@@ -223,6 +223,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_get_trans_devid_by_kvmfd (struct amd_iommu_pci_seg *pci_seg, +u32 kvmfd,u16 *trans_devid); +void amd_iommu_free_trans_devid_by_kvmfd (struct amd_iommu_pci_seg *pci_seg, +u32 kvmfd);#else static inline void amd_iommu_pci_seg_trans_devid_init (struct amd_iommu_pci_seg *pci_seg) { }
drivers/iommu/amd/amd_iommu_types.h +13 -0
@@ -559,6 +559,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;
@@ -621,6 +622,11 @@ enum trans_devid_state {TRANS_DEVID_RESERVED , TRANS_DEVID_ALLOCATED, }; + +struct amd_iommu_kvmfd_trans_entry { +refcount_t refs; +u16 trans_devid; +};#endif /*
@@ -692,6 +698,13 @@ struct amd_iommu_pci_seg {*/ struct mutex trans_devid_mutex;struct xarray trans_devid_xa; + +/* +* Per-segment kvmfd mapping. The xarray is indexed by the kvmfd. +* Values are struct amd_iommu_kvmfd_trans_entry * or xa_mk_value(trans_devid). +*/ +struct mutex kvmfd_xa_mutex; +struct xarray kvmfd_xa;#endif };
drivers/iommu/amd/iommu.c +1 -1
@@ -3058,7 +3058,7 @@ static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev,#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().) +* yet. (See trans_devid_alloc() in trans_devid.c.) */ ret =amd_iommu_trans_devid_reserve (iommu->pci_seg, dev_data->devid);if (ret) {
drivers/iommu/amd/iommufd.c +16 -2
@@ -64,6 +64,7 @@ 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;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);
@@ -104,9 +105,17 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * goto err_kvmfd; } + ret =amd_iommu_get_trans_devid_by_kvmfd (iommu->pci_seg, data.kvmfd, + &trans_devid); +if (ret) + goto err_kvmfd; +/* Reset vIOMMU MMIOs to initialize the vIOMMU */ iommu_reset_vmmio (iommu, aviommu->gid); +amd_iommu_set_translate_dte (iommu, aviommu->gid, pdom, trans_devid); +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid, trans_devid); + ret =amd_viommu_init_one (iommu, aviommu);if (ret) goto err_init;
@@ -117,6 +126,7 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *if (ret) goto err_init; + aviommu->trans_devid = trans_devid; aviommu->kvmfd = data.kvmfd; viommu->ops = &amd_viommu_ops;
@@ -126,6 +136,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return 0 ; err_init: +amd_iommu_update_vfctrl_mmio_translate_devid (iommu, aviommu->gid,0 ); +amd_iommu_clear_translate_dte (iommu, aviommu->gid, trans_devid); +amd_iommu_free_trans_devid_by_kvmfd (iommu->pci_seg, data.kvmfd); err_kvmfd:iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); err_mmap:
@@ -147,10 +160,11 @@ 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); -if (aviommu->vfmmio_mmap_offset) -iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset);amd_iommu_gid_free (iommu, aviommu->gid);amd_viommu_uninit_one (iommu, aviommu); +if (aviommu->vfmmio_mmap_offset) +iommufd_viommu_destroy_mmap (&aviommu->core, aviommu->vfmmio_mmap_offset); +amd_iommu_free_trans_devid_by_kvmfd (iommu->pci_seg, aviommu->kvmfd); }/*
drivers/iommu/amd/trans_devid.c +193 -1
221 lines not on this page · raw message
drivers/iommu/amd/viommu.c +3 -0
@@ -449,6 +449,9 @@ 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 ); +amd_iommu_clear_translate_dte (iommu, aviommu->gid, aviommu->trans_devid);viommu_clear_mapping (iommu, aviommu); }
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v2 21/26] iommu/amd: Map kvmfd to shared translate device ID for vIOMMU
Quoted 3 lines · Suravee Suthikulpanit
Add per-PCI-segment kvmfd xarray
amd_iommu_kvmfd_trans_entrywith refcounted so all vIOMMUs for one VM share one translate-device-id andGPA->SPADTE.
What? There should no KVM in the viommu implementation.
I can't understand why you did this.
AFAICT the HW needs some unused devid "TransDevID" for some purpose. This is pretty sketchy, but OK..
The ID should be affiliated with every nest parent domain, or maybe every vIOMMU.
It has nothing to do with KVM. You can't assume anything about iommufd is constructing the viommu based on the KVM.
The only acceptable use of KVM in iommufd land is to get some CPU specific shared information which this is not doing.
So no to any KVM stuff in this series.
In an AMD system there should be exactly one viommu per kvm per physical instance anyhow.
Jsaon
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 21/26] iommu/amd: Map kvmfd to shared translate device ID for vIOMMU
Jason,
Quoted 15 lines · Jason Gunthorpe
Quoted 3 lines · Suravee Suthikulpanit
Add per-PCI-segment kvmfd xarray
amd_iommu_kvmfd_trans_entrywith refcounted so all vIOMMUs for one VM share one translate-device-id andGPA->SPADTE.What? There should no KVM in the viommu implementation.
I can't understand why you did this.
AFAICT the HW needs some unused devid "TransDevID" for some purpose. This is pretty sketchy, but OK..
The ID should be affiliated with every nest parent domain, or maybe every vIOMMU.
Quoted 7 lines
It has nothing to do with KVM. You can't assume anything about iommufd is constructing the viommu based on the KVM.
The only acceptable use of KVM in iommufd land is to get some CPU specific shared information which this is not doing.
So no to any KVM stuff in this series.
I will remove kvmfd stuff from this series, and decouple it from TransDevID management.
However, in upcoming series, when introduces vIOMMU EventLog and PPRLog virtualization support, we need kvm to get SVM AVIC information in order to setup extended interrupt remapping for EventLog and PPRLog interrupts. Any suggestions on how to achieve this if you do not want to pass kvmfd from userspace?
Thanks,
Suravee
Jason Gunthorpe
maintainerRe: [PATCH v2 21/26] iommu/amd: Map kvmfd to shared translate device ID for vIOMMU
Quoted 5 lines · Suravee
However, in upcoming series, when introduces vIOMMU EventLog and PPRLog virtualization support, we need kvm to get SVM AVIC information in order to setup extended interrupt remapping for EventLog and PPRLog interrupts. Any suggestions on how to achieve this if you do not want to pass kvmfd from userspace?
That sounds like a more appropriate use of the kvm fd, to extract KVM specific information.
However, interrupt virtualization is its own distinct topic and you'd have to get the KVM and IRQ maintainers to agree to whatever you are doing. I would generally expect that the irq bypass is setup in a more general way, not by directly poking into AMD KVM stuff.
Jason
Suravee Suthikulpanit
reviewerPATCH v2 22/26 iommufd: Add hw_queue_init and split queue alloc paths
In reply to Suravee SuthikulpanitAdd iommufd_viommu_ops.hw_queue_init for vIOMMU backends whose hardware uses a guest physical queue base from hw_queue->base_addr instead of a host physical address.
Previously, HW queue alloc always went through iommufd_hw_queue_alloc_phys(), an iommufd_access, and hw_queue_init_phys(base_pa). AMD vIOMMU instead takes GPA from userspace in hw_queue->base_addr and programs hardware without host PA resolution. Splitting helpers and dispatching from the ioctl keeps one uAPI while making the contract explicit. Each vIOMMU driver should implement only one of hw_queue_init_phys or hw_queue_init.
Refactor iommufd_hw_queue_alloc_ioctl() so shared validation and viommu lookup stay in the ioctl, while setup is delegated to _iommufd_hw_queue_init_phys() or _iommufd_hw_queue_init().
--- drivers/iommu/iommufd/viommu.c | 125 +++++++++++++++++++++++---------- include/linux/iommufd.h | 5 +- 2 files changed, 91 insertions(+), 39 deletions(-)
drivers/iommu/iommufd/viommu.c +87 -38
@@ -353,62 +353,35 @@ iommufd_hw_queue_alloc_phys(struct iommu_hw_queue_alloc *cmd,return ERR_PTR (rc); } -int iommufd_hw_queue_alloc_ioctl (struct iommufd_ucmd *ucmd) +static int _iommufd_hw_queue_init_phys (struct iommufd_ucmd *ucmd, +struct iommufd_viommu *viommu) {struct iommu_hw_queue_alloc *cmd = ucmd->cmd;struct iommufd_hw_queue *hw_queue; -struct iommufd_viommu *viommu;struct iommufd_access *access;size_t hw_queue_size;phys_addr_t base_pa; -u64 last;int rc; -if (cmd->flags || cmd->type ==IOMMU_HW_QUEUE_TYPE_DEFAULT ) -return -EOPNOTSUPP ; -if (!cmd->length) -return -EINVAL ; -if (check_add_overflow (cmd->nesting_parent_iova, cmd->length -1 , - &last)) -return -EOVERFLOW ; - - viommu =iommufd_get_viommu (ucmd, cmd->viommu_id); -if (IS_ERR (viommu)) -return PTR_ERR (viommu); - -if (!viommu->ops || !viommu->ops->get_hw_queue_size || - !viommu->ops->hw_queue_init_phys) { - rc = -EOPNOTSUPP ; - goto out_put_viommu; - } - hw_queue_size = viommu->ops->get_hw_queue_size (viommu, cmd->type); -if (!hw_queue_size) { - rc = -EOPNOTSUPP ; - goto out_put_viommu; - } +if (!hw_queue_size) +return -EOPNOTSUPP ;/* * It is a driver bug for providing a hw_queue_size smaller than the * core HW queue structure size */ -if (WARN_ON_ONCE (hw_queue_size <sizeof (*hw_queue))) { - rc = -EOPNOTSUPP ; - goto out_put_viommu; - } +if (WARN_ON_ONCE (hw_queue_size <sizeof (*hw_queue))) +return -EOPNOTSUPP ; hw_queue = (struct iommufd_hw_queue *)_iommufd_object_alloc_ucmd ( ucmd, hw_queue_size,IOMMUFD_OBJ_HW_QUEUE ); -if (IS_ERR (hw_queue)) { - rc =PTR_ERR (hw_queue); - goto out_put_viommu; - } +if (IS_ERR (hw_queue)) +return PTR_ERR (hw_queue); access =iommufd_hw_queue_alloc_phys (cmd, viommu, &base_pa); -if (IS_ERR (access)) { - rc =PTR_ERR (access); - goto out_put_viommu; - } +if (IS_ERR (access)) +return PTR_ERR (access); hw_queue->viommu = viommu;refcount_inc (&viommu->obj.users);
@@ -419,9 +392,85 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd) rc = viommu->ops->hw_queue_init_phys (hw_queue, cmd->index, base_pa);if (rc) - goto out_put_viommu; +return rc; cmd->out_hw_queue_id = hw_queue->obj.id; +return rc; +} + +static int _iommufd_hw_queue_init (struct iommufd_ucmd *ucmd, +struct iommufd_viommu *viommu) +{ +struct iommu_hw_queue_alloc *cmd = ucmd->cmd; +struct iommufd_hw_queue *hw_queue; +size_t hw_queue_size; +int rc; + + hw_queue_size = viommu->ops->get_hw_queue_size (viommu, cmd->type); +if (!hw_queue_size) +return -EOPNOTSUPP ; + +/* +* It is a driver bug for providing a hw_queue_size smaller than the +* core HW queue structure size +*/ +if (WARN_ON_ONCE (hw_queue_size <sizeof (*hw_queue))) +return -EOPNOTSUPP ; + + hw_queue = (struct iommufd_hw_queue *)_iommufd_object_alloc_ucmd ( + ucmd, hw_queue_size,IOMMUFD_OBJ_HW_QUEUE ); +if (IS_ERR (hw_queue)) +return PTR_ERR (hw_queue); + + hw_queue->viommu = viommu; +refcount_inc (&viommu->obj.users); + hw_queue->access =NULL ; + hw_queue->type = cmd->type; + hw_queue->length = cmd->length; + hw_queue->base_addr = cmd->nesting_parent_iova; + + rc = viommu->ops->hw_queue_init (hw_queue, cmd->index); +if (rc) +return rc; + + cmd->out_hw_queue_id = hw_queue->obj.id; +return rc; +} + +int iommufd_hw_queue_alloc_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_hw_queue_alloc *cmd = ucmd->cmd; +struct iommufd_viommu *viommu; +u64 last; +int rc; + +if (cmd->flags || cmd->type ==IOMMU_HW_QUEUE_TYPE_DEFAULT ) +return -EOPNOTSUPP ; +if (!cmd->length) +return -EINVAL ; +if (check_add_overflow (cmd->nesting_parent_iova, cmd->length -1 , + &last)) +return -EOVERFLOW ; + + viommu =iommufd_get_viommu (ucmd, cmd->viommu_id); +if (IS_ERR (viommu)) +return PTR_ERR (viommu); + +if (!viommu->ops || !viommu->ops->get_hw_queue_size) { + rc = -EOPNOTSUPP ; + goto out_put_viommu; + } + +if (viommu->ops->hw_queue_init_phys) + rc =_iommufd_hw_queue_init_phys (ucmd, viommu); +else if (viommu->ops->hw_queue_init) + rc =_iommufd_hw_queue_init (ucmd, viommu); +else + rc = -EOPNOTSUPP ; + +if (rc) + goto out_put_viommu; + rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); out_put_viommu:
include/linux/iommufd.h +4 -1
@@ -180,6 +180,9 @@ struct iommufd_hw_queue {* the physical location of the guest queue * If driver has a deinit function to revert what this op * does, it should set it to the @hw_queue->destroy pointer +* @hw_queue_init: Similar to hw_queue_init_phys, but driver providing this op +* indicates that HW accesses the guest queue memory via +* @hw_queue->baseaddr. */ struct iommufd_viommu_ops {void (*destroy)(struct iommufd_viommu *viommu);
@@ -192,9 +195,9 @@ struct iommufd_viommu_ops {int (*vdevice_init)(struct iommufd_vdevice *vdev);size_t (*get_hw_queue_size)(struct iommufd_viommu *viommu,enum iommu_hw_queue_type queue_type); -/* AMD's HW will add hw_queue_init simply using @hw_queue->base_addr */ int (*hw_queue_init_phys)(struct iommufd_hw_queue *hw_queue,u32 index,phys_addr_t base_addr_pa); +int (*hw_queue_init)(struct iommufd_hw_queue *hw_queue,u32 index); };#if IS_ENABLED (CONFIG_IOMMUFD )
Signature · 1 line
2.34.1
Re: [PATCH v2 22/26] iommufd: Add hw_queue_init and split queue alloc paths
Quoted 2 lines · Suravee Suthikulpanit
Splitting helpers and dispatching from the ioctl keeps one uAPI while making the contract explicit.
Why split? Those two new ioctl functions look quite redundant..
I imagined that the original ioctl function just needed:
[...]
- struct iommufd_access *access;
+ struct iommufd_access *access = NULL;
[...]
if (!viommu->ops || !viommu->ops->get_hw_queue_size ||
- !viommu->ops->hw_queue_init_phys) {
+ (!viommu->ops->hw_queue_init_phys && !viommu->ops->hw_queue_init)) {
rc = -EOPNOTSUPP;
goto out_put_viommu;
}[...]
- access = iommufd_hw_queue_alloc_phys(cmd, viommu, &base_pa);
- if (IS_ERR(access)) {
- rc = PTR_ERR(access);
- goto out_put_viommu;
- }
+ if (viommu->ops->hw_queue_init_phys) {
+ access = iommufd_hw_queue_alloc_phys(cmd, viommu, &base_pa);
+ if (IS_ERR(access)) {
+ rc = PTR_ERR(access);
+ goto out_put_viommu;
+ }
+ }
[...]
rc = viommu->ops->hw_queue_init_phys(hw_queue, cmd->index, base_pa);
+ if (viommu->ops->hw_queue_init_phys)
+ rc = viommu->ops->hw_queue_init_phys(hw_queue, cmd->index,
+ base_pa);
+ else
+ rc = viommu->ops->hw_queue_init(hw_queue, cmd->index);
and then it should work?
Nicolin
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 22/26] iommufd: Add hw_queue_init and split queue alloc paths
Quoted 38 lines · Nicolin Chen
Quoted 2 lines · Suravee Suthikulpanit
Splitting helpers and dispatching from the ioctl keeps one uAPI while making the contract explicit.
Why split? Those two new ioctl functions look quite redundant..
I imagined that the original ioctl function just needed:
[...](quoted diff) +2
-2- struct iommufd_access *access; + struct iommufd_access *access = NULL; if (!viommu->ops || !viommu->ops->get_hw_queue_size || - !viommu->ops->hw_queue_init_phys) { + (!viommu->ops->hw_queue_init_phys && !viommu->ops->hw_queue_init)) {rc = -EOPNOTSUPP; goto out_put_viommu; }[...]
(quoted diff) +7
-5- access = iommufd_hw_queue_alloc_phys(cmd, viommu, &base_pa); - if (IS_ERR(access)) { - rc = PTR_ERR(access); - goto out_put_viommu; - } + if (viommu->ops->hw_queue_init_phys) { + access = iommufd_hw_queue_alloc_phys(cmd, viommu, &base_pa); + if (IS_ERR(access)) { + rc = PTR_ERR(access); + goto out_put_viommu; + } + }rc = viommu->ops->hw_queue_init_phys(hw_queue, cmd->index, base_pa);(quoted diff) +5
-0+ if (viommu->ops->hw_queue_init_phys) + rc = viommu->ops->hw_queue_init_phys(hw_queue, cmd->index, + base_pa); + else + rc = viommu->ops->hw_queue_init(hw_queue, cmd->index);
Quoted 1 line
and then it should work?
Good point. I'll clean this up and send out new version.
Thanks,
Suravee
Jason Gunthorpe
maintainerRe: [PATCH v2 22/26] iommufd: Add hw_queue_init and split queue alloc paths
In reply to Suravee SuthikulpanitQuoted 12 lines · Suravee Suthikulpanit
Add iommufd_viommu_ops.hw_queue_init for vIOMMU backends whose hardware uses a guest physical queue base from
hw_queue->base_addrinstead of a host physical address.Previously, HW queue alloc always went through
iommufd_hw_queue_alloc_phys(), aniommufd_access, andhw_queue_init_phys(base_pa). AMD vIOMMU instead takes GPA from userspace inhw_queue->base_addrand programs hardware without host PA resolution. Splitting helpers and dispatching from the ioctl keeps one uAPI while making the contract explicit. Each vIOMMU driver should implement only one ofhw_queue_init_physorhw_queue_init.
Quoted 3 lines
Refactor
iommufd_hw_queue_alloc_ioctl()so shared validation and viommu lookup stay in the ioctl, while setup is delegated to_iommufd_hw_queue_init_phys()or_iommufd_hw_queue_init().
This patch series is getting pretty big, getting the viommu working alone is enough for one series, I would split getting the direct assigned queues working to a following series
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 22/26] iommufd: Add hw_queue_init and split queue alloc paths
Quoted 17 lines · Jason Gunthorpe
Quoted 12 lines · Suravee Suthikulpanit
Add iommufd_viommu_ops.hw_queue_init for vIOMMU backends whose hardware uses a guest physical queue base from
hw_queue->base_addrinstead of a host physical address.Previously, HW queue alloc always went through
iommufd_hw_queue_alloc_phys(), aniommufd_access, andhw_queue_init_phys(base_pa). AMD vIOMMU instead takes GPA from userspace inhw_queue->base_addrand programs hardware without host PA resolution. Splitting helpers and dispatching from the ioctl keeps one uAPI while making the contract explicit. Each vIOMMU driver should implement only one ofhw_queue_init_physorhw_queue_init.Quoted 3 lines
Refactor
iommufd_hw_queue_alloc_ioctl()so shared validation and viommu lookup stay in the ioctl, while setup is delegated to_iommufd_hw_queue_init_phys()or_iommufd_hw_queue_init().
Quoted 5 lines
This patch series is getting pretty big, getting the viommu working alone is enough for one series, I would split getting the direct assigned queues working to a following series
Jason
Ok. I'll reduce the scope in v3.
Suravee.
Suravee Suthikulpanit
reviewerPATCH v2 23/26 iommu/amd: Add support for vIOMMU HW queues initialization
In reply to Suravee SuthikulpanitAMD HW vIOMMU supports virtualizing Command buffer, Event log, and PPR log. Each can be initialized using the struct iommufd_viommu_ops.hw_queue_init to communicate base address (GPA) and length of each queue to the AMD IOMMU driver in order to programe the corresponded VF control MMIO registers.
--- drivers/iommu/amd/amd_iommu_types.h | 8 +++ drivers/iommu/amd/iommufd.c | 108 ++++++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 3 + 3 files changed, 119 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +8 -0
@@ -507,6 +507,10 @@ extern bool amdr_ivrs_remap_support;#define VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId) \ (iommu->vfctrl_base + (guestId * VIOMMU_VFCTRL_MMIO_ENTRY_SIZE)) +#define VIOMMU_VFCTRL_MMIO_GUEST_COMMAND_CONTROL_OFFSET 0x20 +#define VIOMMU_VFCTRL_MMIO_GUEST_EVENT_CONTROL_OFFSET 0x28 +#define VIOMMU_VFCTRL_MMIO_GUEST_PPR_CONTROL_OFFSET 0x30 +struct amd_iommu ;struct iommu_domain ;struct irq_domain ;
@@ -1166,6 +1170,10 @@ struct amd_iommu_vdevice {struct iommufd_vdevice core; }; +struct amd_iommu_hw_queue { +struct iommufd_hw_queue 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 +108 -0
118 lines not on this page · raw message
include/uapi/linux/iommufd.h +3 -0
@@ -1326,6 +1326,9 @@ enum iommu_hw_queue_type {* emulated vSMMU's IDR1.CMDQS to log2(huge page size / 16 bytes) */ IOMMU_HW_QUEUE_TYPE_TEGRA241_CMDQV =1 , +IOMMU_HW_QUEUE_TYPE_AMD_CMD , +IOMMU_HW_QUEUE_TYPE_AMD_EVT , + IOMMU_HW_QUEUE_TYPE_AMD_PPR, };/**
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 24/26 iommufd: Introduce vIOMMU command via VIOMMU_COMMAND ioctl
In reply to Suravee SuthikulpanitIntroduce a new IOMMU_VIOMMU_COMMAND ioctl, to support vIOMMU-specific commands. Initially, the ioctl support set / get operations on a specified index to write / read value. This allows write / read access to a particular index of a data arrays such as hardware registers.
--- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 3 ++ drivers/iommu/iommufd/viommu.c | 39 +++++++++++++++++++++++++ include/linux/iommufd.h | 5 ++++ include/uapi/linux/iommufd.h | 34 +++++++++++++++++++++ 5 files changed, 82 insertions(+)
drivers/iommu/iommufd/iommufd_private.h +1 -0
@@ -692,6 +692,7 @@ iommufd_viommu_find_veventq(struct iommufd_viommu *viommu,int iommufd_viommu_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj); +int iommufd_viommu_command_ioctl (struct iommufd_ucmd *ucmd);int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_vdevice_destroy (struct iommufd_object *obj);void iommufd_vdevice_abort (struct iommufd_object *obj);
drivers/iommu/iommufd/main.c +3 -0
@@ -432,6 +432,7 @@ union ucmd_buffer {struct iommu_veventq_alloc veventq;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_viommu_command viommu_command;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif
@@ -493,6 +494,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved),IOCTL_OP (IOMMU_VIOMMU_ALLOC ,iommufd_viommu_alloc_ioctl ,struct iommu_viommu_alloc ,out_viommu_id ), +IOCTL_OP (IOMMU_VIOMMU_COMMAND ,iommufd_viommu_command_ioctl , +struct iommu_viommu_command ,val64 ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
drivers/iommu/iommufd/viommu.c +39 -0
@@ -477,3 +477,42 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &viommu->obj);return rc; } + +int iommufd_viommu_command_ioctl (struct iommufd_ucmd *ucmd) +{ +int rc =0 ; +struct iommu_viommu_command *cmd = ucmd->cmd; +struct iommufd_viommu *viommu =iommufd_get_viommu (ucmd, cmd->object_id); + +if (cmd->__reserved) { + rc = -EOPNOTSUPP ; + goto out_put_viommu; + } + +if (IS_ERR (viommu)) { + rc =PTR_ERR (viommu); + goto out_put_viommu; + } + +if (cmd->op ==IOMMU_VIOMMU_COMMAND_OP_SET ) { +if (!viommu->ops->set_command) + rc = -EOPNOTSUPP ; + rc = viommu->ops->set_command (viommu, cmd->index, cmd->val64); + }else if (cmd->op ==IOMMU_VIOMMU_COMMAND_OP_GET ) { +if (!viommu->ops->get_command) + rc = -EOPNOTSUPP ; + rc = viommu->ops->get_command (viommu, cmd->index, &cmd->val64); + }else { + rc = -EOPNOTSUPP ; + } + +if (rc) + goto out_put_viommu; + +if (copy_to_user (&((struct iommu_viommu_command __user *)ucmd->ubuffer)->val64, + &cmd->val64,sizeof (cmd->val64))) + rc = -EFAULT ; +out_put_viommu: +iommufd_put_object (ucmd->ictx, &viommu->obj); +return rc; +}
include/linux/iommufd.h +5 -0
@@ -183,6 +183,9 @@ struct iommufd_hw_queue {* @hw_queue_init: Similar to hw_queue_init_phys, but driver providing this op * indicates that HW accesses the guest queue memory via * @hw_queue->baseaddr. +* @set_command: Set data on the specified index of the specified vIOMMU. +* @get_command: Get data on the specified index of the specified vIOMMU. +* On success, the value is returned via the provided value. */ struct iommufd_viommu_ops {void (*destroy)(struct iommufd_viommu *viommu);
@@ -198,6 +201,8 @@ struct iommufd_viommu_ops {int (*hw_queue_init_phys)(struct iommufd_hw_queue *hw_queue,u32 index,phys_addr_t base_addr_pa);int (*hw_queue_init)(struct iommufd_hw_queue *hw_queue,u32 index); +int (*set_command)(struct iommufd_viommu *viommu,u16 index,u64 value); +int (*get_command)(struct iommufd_viommu *viommu,u16 index,u64 *value); };#if IS_ENABLED (CONFIG_IOMMUFD )
include/uapi/linux/iommufd.h +34 -0
@@ -57,6 +57,7 @@ enum {IOMMUFD_CMD_IOAS_CHANGE_PROCESS =0x92 ,IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 ,IOMMUFD_CMD_HW_QUEUE_ALLOC =0x94 , + IOMMUFD_CMD_VIOMMU_COMMAND = 0x95, };/**
@@ -1131,6 +1132,39 @@ struct iommu_viommu_alloc { };#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC) +/** +* enum viommu_command_ops - viommu command operations +* @IOMMU_VIOMMU_COMMAND_OP_SET: Set the command's data +* @IOMMU_VIOMMU_COMMAND_OP_GET: Get the command's data +*/ +enum viommu_command_ops { +IOMMU_VIOMMU_COMMAND_OP_SET =0 , +IOMMU_VIOMMU_COMMAND_OP_GET =1 , +}; + +/** +* struct iommu_viommu_command - iommu viommu command multiplexer +* @size: sizeof(struct iommu_viommu_command) +* @object_id: ID of the vIOMMU if required +* @op: One of enum viommu_command_ops +* @index: Command index to match with the value +* @__reserved: Must be 0 +* @val64: Command data to set or data returned on get +* +* This multiplexer allows controlling commands on vIOMMU. +* IOMMU_VIOMMU_COMMAND_OP_SET will load a command and +* IOMMU_VIOMMU_COMMAND_OP_GET will return the current value. +*/ +struct iommu_viommu_command { +__u32 size; +__u32 object_id; +__u16 op; +__u16 index; +__u32 __reserved; +__aligned_u64 val64; +}; +#define IOMMU_VIOMMU_COMMAND _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_COMMAND) +/** * struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC) * @size: sizeof(struct iommu_vdevice_alloc)
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v2 25/26 iommu/amd: Handle set/get command for AMD vIOMMU
In reply to Suravee SuthikulpanitGuest kernel programs guest Command Buffer, Event Log, and PPR Log (a.k.a hardware queue) settings via guest control MMIO register (guest MMIO offset 0x18). Accesses to the register is trapped by VMM (QEMU) and information is passed as IOMMU_VIOMMU_OPTION to host IOMMU driver via struct iommufd_viommu_ops set_command() and get_command().
Provides AMD IOMMU driver hooks to handle set/get operations for the guest control MMIO register, which uses key parameter as AMD IOMMU MMIO offset. The value parameter contains the value of the corresponding guest MMIO register, which is converted to the format of AMD vIOMMU VF Control MMIO registers then programed onto the hardware.
--- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu_types.h | 5 + drivers/iommu/amd/amd_viommu.h | 15 +++ drivers/iommu/amd/iommufd.c | 2 + drivers/iommu/amd/vfctrl_mmio.c | 146 ++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/amd/vfctrl_mmio.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 trans_devid.o +obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o trans_devid.o vfctrl_mmio.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu_types.h +5 -0
@@ -191,10 +191,15 @@#define CONTROL_GAM_EN 25#define CONTROL_GALOG_EN 28#define CONTROL_GAINT_EN 29 +#define CONTROL_DUALPPRLOG_EN 30 +#define CONTROL_DUALEVTLOG_EN 32 +#define CONTROL_PPR_AUTO_RSP_EN 39 +#define CONTROL_BLKSTOPMRK_EN 41#define CONTROL_NUM_INT_REMAP_MODE 43#define CONTROL_NUM_INT_REMAP_MODE_MASK 0x03#define CONTROL_NUM_INT_REMAP_MODE_2K 0x01#define CONTROL_EPH_EN 45 +#define CONTROL_PPR_AUTO_RSP_AON 48#define CONTROL_XT_EN 50#define CONTROL_INTCAPXT_EN 51#define CONTROL_GCR3TRPMODE 58
drivers/iommu/amd/amd_viommu.h +15 -0
@@ -23,6 +23,11 @@ int amd_viommu_domain_id_update(struct amd_iommu *iommu, u16 gid,void amd_viommu_set_device_mapping (struct amd_iommu *iommu,u16 hDevId,u16 guestId,u16 gDevId); + +int amd_viommu_guest_mmio_write (struct iommufd_viommu *viommu,u16 offset,u64 value); + +int amd_viommu_guest_mmio_read (struct iommufd_viommu *viommu,u16 offset,u64 *value); +#else static inline int amd_viommu_init (struct amd_iommu *iommu)
@@ -53,6 +58,16 @@ static inline void amd_viommu_set_device_mapping(struct amd_iommu *iommu, u16 hD { } +static inline int amd_viommu_guest_mmio_write (struct iommufd_viommu *viommu,u16 offset,u64 value) +{ +return -EOPNOTSUPP ; +} + +static inline int amd_viommu_guest_mmio_read (struct iommufd_viommu *viommu,u16 offset,u64 *value) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/iommufd.c +2 -0
@@ -317,4 +317,6 @@ static const struct iommufd_viommu_ops amd_viommu_ops = { .vdevice_init = _amd_viommu_vdevice_init, .get_hw_queue_size = _amd_viommu_get_hw_queue_size, .hw_queue_init = _amd_viommu_hw_queue_init, + .set_command = amd_viommu_guest_mmio_write, + .get_command = amd_viommu_guest_mmio_read, };
drivers/iommu/amd/vfctrl_mmio.c new file +146 -0
146 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 25/26] iommu/amd: Handle set/get command for AMD vIOMMU
Quoted 11 lines · Suravee Suthikulpanit
Guest kernel programs guest Command Buffer, Event Log, and PPR Log (a.k.a hardware queue) settings via guest control MMIO register (guest MMIO offset
0x18). Accesses to the register is trapped by VMM (QEMU) and information is passed asIOMMU_VIOMMU_OPTIONto host IOMMU driver via structiommufd_viommu_opsset_command()andget_command().Provides AMD IOMMU driver hooks to handle set/get operations for the guest control MMIO register, which uses key parameter as AMD IOMMU MMIO offset. The value parameter contains the value of the corresponding guest MMIO register, which is converted to the format of AMD vIOMMU VF Control MMIO registers then programed onto the hardware.
So the whole set_command/get_command thing is to MMIO a control register? I doubt this is a right ioctl(s) as it can be abused.
Looking at the details:
drivers/iommu/amd/vfctrl_mmio.c line 103
Quoted 8 lines
drivers/iommu/amd/vfctrl_mmio.c new file +8
-0+switch (offset) { +case MMIO_CONTROL_OFFSET : + { +/* VFCTRL offset 20h */ + val =readq (vfctrl +0x20 ); + val &= ~(0x3ULL <<8 ); + tmp =GET_CTRL_BITS (ctrl,CONTROL_CMDBUF_EN ,1 );// [12] + val |= (tmp <<8 );
This is to enable a hw_queue. So it could be marked when a hw_queue is allocated; IMHO, VMM should only allocate the hw_queue, when the guest really uses (enables) the buffer.
drivers/iommu/amd/vfctrl_mmio.c line 106
Quoted 3 lines
drivers/iommu/amd/vfctrl_mmio.c new file +3
-0+ tmp =GET_CTRL_BITS (ctrl,CONTROL_COMWAIT_EN ,1 );// [4] + val |= (tmp <<9 ); +writeq (val, vfctrl +0x20 );
The spec suggests to set this with CmdBufEn, so it may be a vendor (or hw_queue type) specific flag in structu iommu_hw_queue_alloc?
And similar comments to the event log and pri log buffers as well.
Overall, VMM should control the timings of allocating these viommu hw_queue objects; then kernel would just enable them accordingly.
Nicolin
Suthikulpanit, Suravee
reviewerRe: [PATCH v2 25/26] iommu/amd: Handle set/get command for AMD vIOMMU
Quoted 7 lines · Nicolin Chen
Quoted 6 lines · Suravee Suthikulpanit
Guest kernel programs guest Command Buffer, Event Log, and PPR Log (a.k.a hardware queue) settings via guest control MMIO register (guest MMIO offset
0x18). Accesses to the register is trapped by VMM (QEMU) and information is passed asIOMMU_VIOMMU_OPTIONto host IOMMU driver via structiommufd_viommu_opsset_command()andget_command().
Quoted 8 lines
Quoted 5 lines
Provides AMD IOMMU driver hooks to handle set/get operations for the guest control MMIO register, which uses key parameter as AMD IOMMU MMIO offset. The value parameter contains the value of the corresponding guest MMIO register, which is converted to the format of AMD vIOMMU VF Control MMIO registers then programed onto the hardware.
So the whole set_command/get_command thing is to MMIO a control register? I doubt this is a right ioctl(s) as it can be abused.
Correct. As part of the AMD vIOMMU architecture, MMIO registers in the 1st 4K range are control registers, which can be grouped into Command buffer, Event Log buffer, PPR Log buffer. These are trapped by QEMU and communicate to AMD IOMMU driver to configure the vIOMMU hardware.
Quoted 14 lines
Looking at the details:
drivers/iommu/amd/vfctrl_mmio.c line 103
Quoted 8 lines
drivers/iommu/amd/vfctrl_mmio.c new file +8
-0+switch (offset) { +case MMIO_CONTROL_OFFSET : + { +/* VFCTRL offset 20h */ + val =readq (vfctrl +0x20 ); + val &= ~(0x3ULL <<8 ); + tmp =GET_CTRL_BITS (ctrl,CONTROL_CMDBUF_EN ,1 );// [12] + val |= (tmp <<8 );This is to enable a
hw_queue. So it could be marked when ahw_queueis allocated; IMHO, VMM should only allocate thehw_queue, when the guest really uses (enables) the buffer.
There are more than one registers to control each buffer. Some of them we can assume that it will be set last (e.g. the CONTROL_CMDBUF_EN, CONTROL_EVT_LOG_EN, CONTROL_PPRLOG_EN bits), and use them to trigger hw_queue allocation.
My original thought is to be explicit and separate the programming into:
* Guest setup the buffer base/size -> IOMMUFD allocate the hw_queue
* Guest enable/disable the buffer -> IOMMUFD enable/disable the
control bits of each buffer.
Combine these steps into just hw_queue init/destroy might be okay.
Quoted 9 lines
drivers/iommu/amd/vfctrl_mmio.c line 106
Quoted 4 lines
drivers/iommu/amd/vfctrl_mmio.c new file +3
-0+ tmp =GET_CTRL_BITS (ctrl,CONTROL_COMWAIT_EN ,1 );// [4] + val |= (tmp <<9 ); +writeq (val, vfctrl +0x20 );The spec suggests to set this with CmdBufEn, so it may be a vendor (or
hw_queuetype) specific flag in structuiommu_hw_queue_alloc?And similar comments to the event log and pri log buffers as well.
Lemme see if we can integrate this for all hw_queue.
Quoted 5 lines
Overall, VMM should control the timings of allocating these viommu
hw_queueobjects; then kernel would just enable them accordingly.Nicolin
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v2 26/26 iommu/amd: Introduce logic to check and enable vIOMMU feature
In reply to Suravee SuthikulpanitAlso switch to enable vIOMMU by default.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/init.c | 12 +++++++++++- drivers/iommu/amd/viommu.c | 24 +++++++++++++++++++++++- 4 files changed, 38 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -27,6 +27,7 @@ void amd_iommu_restart_ga_log(struct amd_iommu *iommu);void amd_iommu_restart_ppr_log (struct amd_iommu *iommu);void amd_iommu_set_rlookup_table (struct amd_iommu *iommu,u16 devid);void iommu_feature_enable (struct amd_iommu *iommu,u8 bit); +bool iommu_feature_enable_and_check (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);
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -201,9 +201,12 @@#define CONTROL_EPH_EN 45#define CONTROL_PPR_AUTO_RSP_AON 48#define CONTROL_XT_EN 50 +#define CONTROL_VCMD_EN 52 +#define CONTROL_VIOMMU_EN 53#define CONTROL_INTCAPXT_EN 51#define CONTROL_GCR3TRPMODE 58#define CONTROL_IRTCACHEDIS 59 +#define CONTROL_GSTBUFFERTRPMODE 60#define CONTROL_SNPAVIC_EN 61#define CTRL_INV_TO_MASK 7
drivers/iommu/amd/init.c +11 -1
@@ -198,7 +198,7 @@ bool amdr_ivrs_remap_support __read_mostly;bool amd_iommu_force_isolation__read_mostly ;/* VIOMMU enabling flag */ -bool amd_iommu_viommu; +bool amd_iommu_viommu = true;unsigned long amd_iommu_pgsize_bitmap__ro_after_init =AMD_IOMMU_PGSIZES ;
@@ -417,6 +417,16 @@ void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)iommu_feature_set (iommu,1ULL ,1ULL , bit); } +bool iommu_feature_enable_and_check (struct amd_iommu *iommu,u8 bit) +{ +u64 ctrl; + +iommu_feature_enable (iommu, bit); + + ctrl =readq (iommu->mmio_base +MMIO_CONTROL_OFFSET ); +return (ctrl & (1ULL << bit)); +} +static void iommu_feature_disable (struct amd_iommu *iommu,u8 bit) {iommu_feature_set (iommu,0ULL ,1ULL , bit);
drivers/iommu/amd/viommu.c +23 -1
@@ -45,6 +45,18 @@LIST_HEAD (viommu_devid_map); +static int viommu_enable (struct amd_iommu *iommu) +{ +/* The GstBufferTRPMode feature is checked by set and test */ +if (!iommu_feature_enable_and_check (iommu,CONTROL_GSTBUFFERTRPMODE )) +return -EINVAL ; + +iommu_feature_enable (iommu,CONTROL_VCMD_EN ); +iommu_feature_enable (iommu,CONTROL_VIOMMU_EN ); + +return 0 ; +} +static int viommu_init_pci_vsc (struct amd_iommu *iommu) { iommu->vsc_offset =pci_find_capability (iommu->dev,PCI_CAP_ID_VNDR );
@@ -308,11 +320,21 @@ int __init amd_viommu_init(struct amd_iommu *iommu) ret =viommu_private_space_init (iommu);if (ret) -return ret; + goto err_unmap_vf;set_iommu_dte (iommu); + ret =viommu_enable (iommu); +if (ret) + goto err_private_space; +return 0 ; + +err_private_space: +viommu_private_space_uninit (iommu); +err_unmap_vf: +amd_viommu_uninit (iommu); +return ret; } static int __maybe_unused alloc_private_vm_region(struct amd_iommu *iommu, u64 **entry,
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerQuoted 1 line · Suravee Suthikulpanit
[1] IOMMU Specification: https://docs.amd.com/v/u/en-US/48882_3.10_PUB
This link doesn't work
And I'm not sure the older PDF has everything, I haven't figure out where in the spec this TransDevID stuff comes from
Quoted 1 line
[2] Linux git tree: https://github.com/AMDESE/linux-iommu/tree/linux-7.1.0-rc4-amd-viommu_upstream_v2
Build test all the kconfig combinations, I noticed problems.
Jason
Suthikulpanit, Suravee
reviewerQuoted 5 lines · Jason Gunthorpe
Quoted 1 line · Suravee Suthikulpanit
[1] IOMMU Specification: https://docs.amd.com/v/u/en-US/48882_3.10_PUB
This link doesn't work
AMD Documentation team have recently updated the doc and remove the old link :( Here is the most recent one.
https://docs.amd.com/v/u/en-US/48882_3.11_IOMMU_PUB
Quoted 2 lines
And I'm not sure the older PDF has everything, I haven't figure out where in the spec this TransDevID stuff comes from
The term TransDevID is not mentioning in the spec. It's basically what we need to program into the VFCntlMMIO Offset {16’b[GuestID], 6’b01_0000} Guest Miscellaneous Control Register bits [31:16] (DeviceID: The system deviceID which has the entire guest GPA to SPA translation).
The hardware expects driver to setup some unused DTE with the translation (Host Page Table Root Pointer [31:12], Mode[2:0], TV, V bits). So, I am referring to the device ID used as index of the unused DTE as TransDevID.
Suravee
Quoted 5 lines
Quoted 1 line
[2] Linux git tree: https://github.com/AMDESE/linux-iommu/tree/linux-7.1.0-rc4-amd-viommu_upstream_v2
Build test all the kconfig combinations, I noticed problems.
Jason
Suthikulpanit, Suravee
reviewerQuoted 4 lines · Jason Gunthorpe
Quoted 1 line
[2] Linux git tree:https://github.com/AMDESE/linux-iommu/tree/linux-7.1.0-rc4-amd-
viommu_upstream_v2Build test all the kconfig combinations, I noticed problems.
Jason
I have build tested w/:
- CONFIG_IOMMUFD (m/n)
- CONFIG_IOMMUFD (m) + CONFIG_AMD_IOMMU_IOMMUFD (y/n)
And they all have passed. Could you elaborate on "all the kconfig combinations"?
Thanks,
Suravee
Jason Gunthorpe
maintainerQuoted 8 lines · Suravee
Quoted 5 lines · Jason Gunthorpe
Quoted 3 lines
[2] Linux git tree:https://github.com/AMDESE/linux-iommu/tree/linux-7.1.0-rc4-amd-
viommu_upstream_v2Build test all the kconfig combinations, I noticed problems.
Quoted 8 lines
Quoted 1 line
Jason
I have build tested w/:
-CONFIG_IOMMUFD(m/n)
-CONFIG_IOMMUFD(m) +CONFIG_AMD_IOMMU_IOMMUFD(y/n)And they all have passed. Could you elaborate on "all the kconfig combinations"?
I don't remember, just my default built kconfig didn't work, there was a stray ; in one of the #ifdef'd off blocks
Jason
Suthikulpanit, Suravee
reviewerQuoted 8 lines · Suravee
Quoted 4 lines · Jason Gunthorpe
Quoted 1 line
[2] Linux git tree:https://github.com/AMDESE/linux-iommu/tree/ linux-7.1.0-rc4-amd-
viommu_upstream_v2Build test all the kconfig combinations, I noticed problems.
Jason
Quoted 8 lines
I have build tested w/:
-CONFIG_IOMMUFD(m/n)
-CONFIG_IOMMUFD(m) +CONFIG_AMD_IOMMU_IOMMUFD(y/n)And they all have passed. Could you elaborate on "all the kconfig combinations"?
Thanks,
Suravee
I found the issue. Please ignore this.
Suravee