iommu/amd: Introduce AMD Hardware-accelerated Virtualized IOMMU (vIOMMU) Support
Subsystems (3)
Suravee Suthikulpanit
reviewercover 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. 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 trap and to emulate the IOMMU configuration MMIO registers between 0KB and 4KB (i.e. 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 section vIOMMU of the AMD IOMMU Specification[1].
This series is implemented on top of IOMMUFD vIOMMU, vDevice, and HW-queue framework. It is organized into the following subset of patches:
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-13 : Introduce AMD vIOMMU Private Address support
Patch 14-16 : Introduce IOMMUFD vDevice support for AMD
Patch 17 : Introduce AMD vIOMMU Translation DTE
Patch 18-21 : Introduce IOMMUFD hw_queue support for AMD
Patch 22 : Enable AMD vIOMMU support
Upcoming Series:
* Add Extended Interrupt Remapping support (for guest Event / PPR log interrupts)
* Add Guest Event Injection support
Note:
* This series is rebased on top of v6.19. See [2] for git repo.
[1] IOMMU Specification: https://docs.amd.com/v/u/en-US/48882_3.10_PUB
[2] GIT Repo: https://github.com/AMDESE/linux-iommu/tree/wip/linux-6.19-amd-viommu_upstream_v1
Thank you,
Suravee
Suravee Suthikulpanit (22):
iommu/amd: Make amd_iommu_completion_wait() non-static iommu/amd: Introduce vIOMMU-specific events and event info iommu/amd: Detect and initialize AMD vIOMMU feature iommu/amd: Introduce IOMMUFD vIOMMU support for AMD iommu/amd: Introduce vIOMMU Guest-ID (GID)
iommu/amd: Map vIOMMU VF MMIO and VF Control MMIO regions 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 helper functions to manage private address region for
each VM
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: Introduce vIOMMU Translation DTE
iommufd: Introduce iommufd_viommu_ops.hw_queue_init iommu/amd: Add support for vIOMMU HW queues initialization
iommufd: Introduce vIOMMU option via IOMMU_OPTION ioctl iommu/amd: Handle set/get option for AMD vIOMMU hw_queue iommu/amd: Introduce logic to check and enable vIOMMU feature
drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 26 ++ drivers/iommu/amd/amd_iommu_types.h | 78 ++++ drivers/iommu/amd/amd_viommu.h | 67 ++++ drivers/iommu/amd/init.c | 27 +- drivers/iommu/amd/iommu.c | 204 ++++++++-- drivers/iommu/amd/iommufd.c | 170 ++++++++ drivers/iommu/amd/nested.c | 18 + drivers/iommu/amd/vfctrl_mmio.c | 145 +++++++ drivers/iommu/amd/viommu.c | 498 ++++++++++++++++++++++++ drivers/iommu/iommufd/ioas.c | 2 +- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 6 +- drivers/iommu/iommufd/viommu.c | 143 +++++-- include/linux/iommufd.h | 10 +- include/uapi/linux/iommufd.h | 25 +- 16 files changed, 1343 insertions(+), 79 deletions(-) create mode 100644 drivers/iommu/amd/amd_viommu.h 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 1/22 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
@@ -197,6 +197,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,
@@ -1443,7 +1441,7 @@ static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd)* 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;
@@ -1481,7 +1479,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)
@@ -1499,7 +1497,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)
@@ -1510,7 +1508,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); }/*
@@ -1529,7 +1527,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)
@@ -1540,7 +1538,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)
@@ -1576,7 +1574,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)
@@ -1599,7 +1597,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)
@@ -1835,7 +1833,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,
@@ -2488,7 +2486,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 ;
@@ -3354,7 +3352,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
Re: [PATCH 01/22] iommu/amd: Make amd_iommu_completion_wait() non-static
Quoted 3 lines · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.Signed-off-by Suravee Suthikulpanit <[email protected]>
Suravee Suthikulpanit
reviewerPATCH 2/22 iommu/amd: Introduce vIOMMU-specific events and event info
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 | 3 ++ drivers/iommu/amd/iommu.c | 58 ++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 13 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -153,6 +153,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
drivers/iommu/amd/iommu.c +45 -13
100 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 02/22] iommu/amd: Introduce vIOMMU-specific events and event info
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 930
Quoted 6 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommu.c +1
-1@@ -927,29 +927,42 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) }if (type ==EVENT_TYPE_IO_FAULT ) { -amd_iommu_report_page_fault (iommu, devid, pasid, address, flags); +u8 vflags = (event[0 ] >>27 ) &0x1F ;
Hmm, I don't see this vflags field in the AMD vIOMMU spec (48882-PUB—Rev 3.10—Feb 2025).
Anyway, perhaps define a pair of SHIFT/MASK macros like other fields?
Nicolin
Suthikulpanit, Suravee
reviewerRe: [PATCH 02/22] iommu/amd: Introduce vIOMMU-specific events and event info
Hi
Quoted 10 lines · Nicolin Chen
drivers/iommu/amd/iommu.c static void iommu_print_event(struct amd_iommu *iommu, void *__evt) · line 930
Quoted 6 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommu.c +1
-1@@ -927,29 +927,42 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) }if (type ==EVENT_TYPE_IO_FAULT ) { -amd_iommu_report_page_fault (iommu, devid, pasid, address, flags); +u8 vflags = (event[0 ] >>27 ) &0x1F ;Hmm, I don't see this vflags field in the AMD vIOMMU spec (48882-PUB—Rev 3.10—Feb 2025).
I have checked with the documentation team, and they are in the process of publishing the new document, which will have the information.
Quoted 2 lines
Anyway, perhaps define a pair of SHIFT/MASK macros like other fields?
Ok
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH 3/22 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 | 13 +++++++++++++ drivers/iommu/amd/viommu.c | 29 +++++++++++++++++++++++++++++ 6 files changed, 68 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
@@ -33,6 +33,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
@@ -106,6 +106,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 0 ; +} + +#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ + +#endif /* AMD_VIOMMU_H */
drivers/iommu/amd/init.c +13 -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"
@@ -193,6 +194,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 {
@@ -2198,6 +2202,10 @@ 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" ); +if (iommu->cap & (1UL <<IOMMU_CAP_NPCACHE )) {pr_info ("Using strict mode due to virtualization\n" );iommu_set_dma_strict ();
@@ -2291,6 +2299,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" ); }
@@ -2303,6 +2314,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
Re: iommu/amd: Initialize IOMMU Private Address (IPA) space mapping region
drivers/iommu/amd/init.c static int __init iommu_init_pci(struct amd_iommu *iommu) · line 2207
Quoted 8 lines
drivers/iommu/amd/init.c +3
-0@@ -2198,6 +2202,10 @@ 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" );
Should amd_iommu_viommu be set to false if vIOMMU initialization fails? Currently, the following logic will indicate that vIOMMU is enabled even if the initialization process does not succeed:
if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
pr_info("Using strict mode due to virtualization\n"); iommu_set_dma_strict();
@@ -2291,6 +2299,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");
}
@@ -2303,6 +2314,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");
}
Suthikulpanit, Suravee
reviewerRe: iommu/amd: Initialize IOMMU Private Address (IPA) space mapping region
Quoted 27 lines · Weinan Liu
drivers/iommu/amd/init.c static int __init iommu_init_pci(struct amd_iommu *iommu) · line 2207
Quoted 8 lines
drivers/iommu/amd/init.c +3
-0@@ -2198,6 +2202,10 @@ 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" );Should
amd_iommu_viommube set to false if vIOMMU initialization fails? Currently, the following logic will indicate that vIOMMU is enabled even if the initialization process does not succeed:if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) { pr_info("Using strict mode due to virtualization\n"); iommu_set_dma_strict();drivers/iommu/amd/init.c +3
-0@@ -2291,6 +2299,9 @@ static void print_iommu_info(void)if (check_feature2 (FEATURE_SEVSNPIO_SUP ))pr_cont (" SEV-TIO" ); +if (check_feature (FEATURE_VIOMMU )) +pr_cont (" vIOMMU" ); +pr_cont ("\n" ); }
drivers/iommu/amd/init.c static void print_iommu_info(void) · line 2319
Quoted 6 lines
drivers/iommu/amd/init.c +2
-0@@ -2303,6 +2314,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" ); }
Good point. I'll fix this in V2.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH 4/22 iommu/amd: Introduce IOMMUFD vIOMMU support for AMD
In reply to Suravee SuthikulpanitIntroduce a new enum iommu_viommu_type (IOMMU_VIOMMU_TYPE_AMD) for AMD vIOMMU along with the struct iommu_viommu_amd, which is used to initialize IOMMUFD vIOMMU instance when calling struct iommu_ops.viommu_init().
Also, hook up struct iomufd_viomu_ops.alloc_domain_nested to connect nested domain allocation with AMD vIOMMU implementation.
Additional initialization will be added in subsequent patches.
--- drivers/iommu/amd/iommufd.c | 4 ++++ include/uapi/linux/iommufd.h | 10 ++++++++++ 2 files changed, 14 insertions(+)
drivers/iommu/amd/iommufd.c +4 -0
@@ -34,6 +34,9 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *length, u32 *type)size_t amd_iommufd_get_viommu_size (struct device *dev,enum iommu_viommu_type viommu_type) { +if (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
@@ -1048,11 +1048,13 @@ 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 ,IOMMU_VIOMMU_TYPE_ARM_SMMUV3 =1 ,IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV =2 , +IOMMU_VIOMMU_TYPE_AMD =3 , };/**
@@ -1071,6 +1073,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
Re: [PATCH 4/22] iommu/amd: Introduce IOMMUFD vIOMMU support for AMD
drivers/iommu/amd/iommufd.c void *amd_iommufd_hw_info(struct device *dev, u32 *length, u32 *type) · line 39
Quoted 11 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +3
-0@@ -34,6 +34,9 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *length, u32 *type)size_t amd_iommufd_get_viommu_size (struct device *dev,enum iommu_viommu_type viommu_type) { +if (viommu_type !=IOMMU_VIOMMU_TYPE_AMD ) +return 0 ; +
It should also return 0 here if amd_viommu_init() fails or if amd_iommu_viommu is false.
Suthikulpanit, Suravee
reviewerRe: [PATCH 4/22] iommu/amd: Introduce IOMMUFD vIOMMU support for AMD
Quoted 8 lines · Weinan Liu
Quoted 6 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +0
-0@@ -34,6 +34,9 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *length, u32 *type)
Quoted 7 lines
drivers/iommu/amd/iommufd.c void *amd_iommufd_hw_info(struct device *dev, u32 *length, u32 *type) · line 39
Quoted 5 lines
drivers/iommu/amd/iommufd.c +3
-0size_t amd_iommufd_get_viommu_size (struct device *dev,enum iommu_viommu_type viommu_type) { +if (viommu_type !=IOMMU_VIOMMU_TYPE_AMD ) +return 0 ; +It should also return 0 here if
amd_viommu_init()fails or ifamd_iommu_viommuis false.
I'll fix this in V2.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH 5/22 iommu/amd: Introduce vIOMMU Guest-ID (GID)
In reply to Suravee SuthikulpanitAMD HW-vIOMMU feature requires IOMMU driver to specify a unique 16-bit Guest ID (GID) for each VM. This ID is per-vIOMMU instance, and it is used to index into various data structures for configuring the hardware.
Introduce helper functions amd_iommu_gid_alloc() / amd_iommu_gid_free(), which will be called during vIOMMU initialize/destroy.
--- drivers/iommu/amd/amd_iommu.h | 4 ++++ drivers/iommu/amd/amd_iommu_types.h | 4 ++++ drivers/iommu/amd/iommu.c | 20 ++++++++++++++++++++ drivers/iommu/amd/iommufd.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+)
drivers/iommu/amd/amd_iommu.h +4 -0
@@ -226,4 +226,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 (void ); +void amd_iommu_gid_free (int gid);#endif /* AMD_IOMMU_H */
drivers/iommu/amd/amd_iommu_types.h +4 -0
@@ -413,6 +413,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 +512,7 @@ struct amd_iommu_viommu {struct iommufd_viommu core;struct protection_domain *parent;/* nest parent domain for this viommu */ struct list_head pdom_list;/* For protection_domain->viommu_list */ +u16 gid;/* Guest ID for the vIOMMU */ /* * Per-vIOMMU guest domain ID to host domain ID mapping.
drivers/iommu/amd/iommu.c +20 -0
@@ -61,6 +61,9 @@ LIST_HEAD(acpihid_map);const struct iommu_ops amd_iommu_ops; +/* Global guest ID */ +static DEFINE_IDA (amd_iommu_global_gid_ida); +int amd_iommu_max_glx_val =-1 ;/*
@@ -252,6 +255,23 @@ 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 (void ) +{ +int ret =ida_alloc_range (&amd_iommu_global_gid_ida,1 ,VIOMMU_MAX_GID ,GFP_KERNEL ); + +if (ret <0 ) +pr_err ("%s: Failed to allocate guest ID\n" , __func__); +else +pr_debug ("%s: gid=%u\n" , __func__, ret); + +return ret; +} + +void amd_iommu_gid_free (int gid) +{ +ida_free (&amd_iommu_global_gid_ida, gid); +} +static inline int get_acpihid_device_id (struct device *dev,struct acpihid_map_entry **entry) {
drivers/iommu/amd/iommufd.c +29 -0
@@ -43,13 +43,35 @@ 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);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; + + aviommu->gid =amd_iommu_gid_alloc (); +if (aviommu->gid <0 ) +return aviommu->gid; +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_out; + viommu->ops = &amd_viommu_ops;spin_lock_irqsave (&pdom->lock, flags);
@@ -57,6 +79,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *spin_unlock_irqrestore (&pdom->lock, flags);return 0 ; +err_out: +amd_iommu_gid_free (aviommu->gid); +return ret; }static void amd_iommufd_viommu_destroy (struct iommufd_viommu *viommu)
@@ -64,11 +89,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 (aviommu->gid); }/*
Signature · 1 line
2.34.1
Re: [PATCH 05/22] iommu/amd: Introduce vIOMMU Guest-ID (GID)
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 46
Quoted 9 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +1
-0@@ -43,13 +43,35 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *parent, const struct iommu_user_data *user_data) { + aviommu->gid = amd_iommu_gid_alloc();
Since the gid is defined as a u16, the return value from amd_iommu_gid_alloc() will cast any negative error codes to a large positive value.
Quoted 2 lines
drivers/iommu/amd/iommufd.c +2
-0+ if (aviommu->gid < 0) + return aviommu->gid;
Suthikulpanit, Suravee
reviewerRe: [PATCH 05/22] iommu/amd: Introduce vIOMMU Guest-ID (GID)
Quoted 15 lines · Weinan Liu
drivers/iommu/amd/iommufd.c size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi · line 46
Quoted 9 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +1
-0@@ -43,13 +43,35 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *parent, const struct iommu_user_data *user_data) { + aviommu->gid = amd_iommu_gid_alloc();Since the gid is defined as a u16, the return value from
amd_iommu_gid_alloc()will cast any negative error codes to a large positive value.Quoted 2 lines
drivers/iommu/amd/iommufd.c +2
-0+ if (aviommu->gid < 0) + return aviommu->gid;
I will fix this in v2.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH 6/22 iommu/amd: Map vIOMMU VF MMIO and VF Control MMIO regions
In reply to Suravee SuthikulpanitTo enable the vIOMMU feature, the AMD IOMMU driver needs to map vIOMMU VF MMIO and VF Control MMIO regions using information in the PCI vendor-specific capability (VSC).
The VF Control MMIO region represents the 1st 4K of guest IOMMU mmio region, which contains IOMMU control registers, and will be trapped and handled by QEMU.
The VF MMIO region represents the 3rd 4K of guest IOMMU mmio region, which will be virtualized by the IOMMU hardware.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 29 ++++++++++ drivers/iommu/amd/init.c | 2 +- drivers/iommu/amd/viommu.c | 83 +++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -26,6 +26,7 @@ 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);#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );
drivers/iommu/amd/amd_iommu_types.h +29 -0
@@ -38,6 +38,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
@@ -472,6 +478,16 @@ 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 + +#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 ;
@@ -685,6 +701,19 @@ 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; + +/* 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/init.c +1 -1
@@ -478,7 +478,7 @@ static void iommu_disable(struct amd_iommu *iommu)* mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in * the system has one. */ -static u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) +u8 __iomem *__init iommu_map_mmio_space (u64 address,u64 end) {if (!request_mem_region (address, end,"amd_iommu" )) { pr_err("Can not reserve memory region %llx-%llx for mmio\n",
drivers/iommu/amd/viommu.c +83 -0
104 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 06/22] iommu/amd: Map vIOMMU VF MMIO and VF Control MMIO regions
Quoted 8 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +4
-0+static int __init viommu_vf_vfcntl_init (struct amd_iommu *iommu) +{... + /* Mapping 256MB of VF and 4MB of VF_CNTL BARs */ + vf_phys &= ~1ULL;
drivers/iommu/amd/viommu.c line 82
Quoted 10 lines
drivers/iommu/amd/viommu.c +10
-0+ iommu->vf_base = iommu_map_mmio_space(vf_phys, 0x10000000); + if (!iommu->vf_base) { + pr_err("Can't reserve vf_base\n"); + return -ENOMEM; + } + + vf_cntl_phys &= ~1ULL; + iommu->vfctrl_base = iommu_map_mmio_space(vf_cntl_phys, 0x400000); + + if (!iommu->vfctrl_base) {
If this mapping fails, the previously successful mapping of iommu->vf_base need to be clean up
Quoted 4 lines
+
pr_err("Can't reserve vfctrl_base\n");
+ return-ENOMEM;
+ }
+
Suthikulpanit, Suravee
reviewerRe: [PATCH 06/22] iommu/amd: Map vIOMMU VF MMIO and VF Control MMIO regions
Quoted 13 lines · Weinan Liu
Quoted 11 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +7
-0+static int __init viommu_vf_vfcntl_init (struct amd_iommu *iommu) +{... + /* Mapping 256MB of VF and 4MB of VF_CNTL BARs */ + vf_phys &= ~1ULL; + iommu->vf_base = iommu_map_mmio_space(vf_phys, 0x10000000); + if (!iommu->vf_base) { + pr_err("Can't reserve vf_base\n");
Quoted 8 lines
drivers/iommu/amd/viommu.c line 82
Quoted 7 lines
+ return
-ENOMEM;
+ }drivers/iommu/amd/viommu.c +5
-0+ + vf_cntl_phys &= ~1ULL; + iommu->vfctrl_base = iommu_map_mmio_space(vf_cntl_phys, 0x400000); + + if (!iommu->vfctrl_base) {If this mapping fails, the previously successful mapping of
iommu->vf_baseneed to be clean up
Thank for catching. I'll fix this in V2.
Suravee
Quoted 4 lines
Quoted 4 lines
+
pr_err("Can't reserve vfctrl_base\n");
+ return-ENOMEM;
+ }
+
Suravee Suthikulpanit
reviewerPATCH 7/22 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_viommu.h | 5 +++++ drivers/iommu/amd/iommufd.c | 17 ++++++++++++++++- drivers/iommu/amd/viommu.c | 11 +++++++++++ include/uapi/linux/iommufd.h | 2 ++ 4 files changed, 34 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_viommu.h +5 -0
@@ -10,6 +10,7 @@int amd_viommu_init (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)
@@ -17,6 +18,10 @@ static inline int amd_viommu_init(struct amd_iommu *iommu)return 0 ; } +u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid); +{ +return 0 ; +}#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_VIOMMU_H */
drivers/iommu/amd/iommufd.c +16 -1
@@ -7,6 +7,7 @@#include "iommufd.h" #include "amd_iommu.h" +#include "amd_viommu.h" #include "amd_iommu_types.h" static const struct iommufd_viommu_ops amd_viommu_ops;
@@ -44,9 +45,11 @@ 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 *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 );
@@ -66,11 +69,21 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return aviommu->gid;pr_debug ("%s: gid=%#x" , __func__, aviommu->gid); + page_base =amd_viommu_get_vfmmio_addr (iommu, aviommu->gid); +if (page_base <=0 ) +return -ENODEV ; + + ret =iommufd_viommu_alloc_mmap (&aviommu->core, + page_base,SZ_4K , + (unsigned long *)&data.out_vfmmio_mmap_offset); +if (ret) + goto err_out; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , reserved);if (ret) - goto err_out; + goto free_mmap; viommu->ops = &amd_viommu_ops;
@@ -79,6 +92,8 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *spin_unlock_irqrestore (&pdom->lock, flags);return 0 ; +free_mmap: +iommufd_viommu_destroy_mmap (&aviommu->core, data.out_vfmmio_mmap_offset); err_out:amd_iommu_gid_free (aviommu->gid);return ret;
drivers/iommu/amd/viommu.c +11 -0
@@ -92,6 +92,17 @@ static int __init viommu_vf_vfcntl_init(struct amd_iommu *iommu)return 0 ; } +/* +* 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
@@ -1075,9 +1075,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
Re: [PATCH 07/22] 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 72
Quoted 10 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +1
-0@@ -66,11 +69,21 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return aviommu->gid;pr_debug ("%s: gid=%#x" , __func__, aviommu->gid); + page_base =amd_viommu_get_vfmmio_addr (iommu, aviommu->gid);
According to the below implementation and prototype, amd_viommu_get_vfmmio_addr() won't return negative value.
drivers/iommu/amd/iommufd.c int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * · line 74
Quoted 2 lines
drivers/iommu/amd/iommufd.c +2
-0+if (page_base <=0 ) +return -ENODEV ;
goto err_out here as well to avoid leaking the gid allocated above.
Quoted 38 lines
drivers/iommu/amd/iommufd.c +10
-1+ + ret =iommufd_viommu_alloc_mmap (&aviommu->core, + page_base,SZ_4K , + (unsigned long *)&data.out_vfmmio_mmap_offset); +if (ret) + goto err_out; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , reserved);if (ret) - goto err_out; + goto free_mmap; viommu->ops = &amd_viommu_ops;@@ -79,6 +92,8 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *spin_unlock_irqrestore (&pdom->lock, flags);return 0 ; +free_mmap: +iommufd_viommu_destroy_mmap (&aviommu->core, data.out_vfmmio_mmap_offset); err_out:amd_iommu_gid_free (aviommu->gid);return ret;drivers/iommu/amd/viommu.c +10
-0+/* +* 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);
Suthikulpanit, Suravee
reviewerRe: [PATCH 07/22] iommu/amd: Add support for AMD vIOMMU VF MMIO region
Quoted 11 lines · Weinan Liu
Quoted 9 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +0
-0@@ -66,11 +69,21 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *return aviommu->gid;pr_debug ("%s: gid=%#x" , __func__, aviommu->gid);
Quoted 2 lines
drivers/iommu/amd/iommufd.c int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * · line 72
Quoted 1 line
drivers/iommu/amd/iommufd.c +1
-0+ page_base =amd_viommu_get_vfmmio_addr (iommu, aviommu->gid);According to the below implementation and prototype,
amd_viommu_get_vfmmio_addr()won't return negative value.
Good point. I will remove the check below.
Quoted 3 lines
drivers/iommu/amd/iommufd.c int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain * · line 74
Quoted 2 lines
drivers/iommu/amd/iommufd.c +2
-0+if (page_base <=0 ) +return -ENODEV ;goto
err_outhere as well to avoid leaking the gid allocated above.
This should no longer be needed.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH 8/22 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);irqreturn_t amd_iommu_int_thread (int irq,void *data);irqreturn_t amd_iommu_int_thread_evtlog (int irq,void *data);irqreturn_t amd_iommu_int_thread_pprlog (int irq,void *data);
drivers/iommu/amd/amd_iommu_types.h +1 -0
@@ -221,6 +221,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
@@ -1429,6 +1429,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.
@@ -1663,6 +1675,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
@@ -79,6 +79,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *if (ret) goto err_out; +/* 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
Re: [PATCH 8/22] iommu/amd: Introduce Reset vMMIO Command
Quoted 5 lines · Suravee Suthikulpanit
Introduce new IOMMU commands for vIOMMU to reset
virtualized MMIO registers of a particular guest.Signed-off-by Suravee Suthikulpanit <[email protected]>
Suravee Suthikulpanit
reviewerPATCH 9/22 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 | 3 ++ drivers/iommu/amd/amd_iommu_types.h | 3 ++ drivers/iommu/amd/iommu.c | 6 +-- drivers/iommu/amd/viommu.c | 76 +++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-)
drivers/iommu/amd/amd_iommu.h +3 -0
@@ -28,6 +28,7 @@ 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); +int iommu_flush_dte (struct amd_iommu *iommu,u16 devid);#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );
@@ -36,6 +37,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 );
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -807,6 +807,9 @@ struct amd_iommu {/* IOPF support */ struct iopf_queue *iopf_queue;unsigned char iopfq_name[32 ]; + +/* 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 +3 -3
@@ -1546,7 +1546,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;
@@ -2720,12 +2720,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,
drivers/iommu/amd/viommu.c +76 -0
87 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH 09/22] iommu/amd: Introduce domain for IOMMU Private Address (IPA) region
Quoted 7 lines · Suravee Suthikulpanit
AMD 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.
Why can't you use a normal v1 domain with all the normal existing code?
Afaict it is not special at all other than it has a specific dte index?
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH 09/22] iommu/amd: Introduce domain for IOMMU Private Address (IPA) region
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Suravee Suthikulpanit
AMD 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.
Quoted 5 lines
Why can't you use a normal v1 domain with all the normal existing code?
Afaict it is not special at all other than it has a specific dte index?
Jason
Actually, this is quite different from the normal GPA->SPA, but rather IPA->SPA (for case GstBufferTRPMode=1). It is used specifically for vIOMMU.
The current mapping is:
IOMMU General Backing Storatge: IPA = 0x0 - 0x80_0000 (8MB) Domain ID Mapping Table: IPA = 0x10_0000_0000 - 0x1F_FFFF_FFFF (64GB) Device ID Mapping Table: IPA = 0x20_0000_0000 - 0x27_FFFF_FFFF (32GB)
The v1 table containing this mapping is programmed onto the Device Table Entry index of the IOMMU device ID.
Thanks,
Suravee
Jason Gunthorpe
maintainerRe: [PATCH 09/22] iommu/amd: Introduce domain for IOMMU Private Address (IPA) region
Quoted 21 lines · Suravee
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · Suravee Suthikulpanit
AMD 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.
Quoted 5 lines
Why can't you use a normal v1 domain with all the normal existing code?
Afaict it is not special at all other than it has a specific dte index?
Jason
Actually, this is quite different from the normal
GPA->SPA, but ratherIPA->SPA(for case GstBufferTRPMode=1). It is used specifically for vIOMMU.
Quoted 8 lines
The current mapping is:
IOMMU General Backing Storatge: IPA =
0x0- 0x80_0000 (8MB) Domain ID Mapping Table: IPA = 0x10_0000_0000 - 0x1F_FFFF_FFFF (64GB) Device ID Mapping Table: IPA = 0x20_0000_0000 - 0x27_FFFF_FFFF (32GB)The v1 table containing this mapping is programmed onto the Device Table Entry index of the IOMMU device ID.
I understand what it is, I'm asking why so much code is duplicated when this is just a normal domain with a straightforward mapping put in it?
Jason
Suravee Suthikulpanit
reviewerPATCH 10/22 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
@@ -175,6 +175,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
Suravee Suthikulpanit
reviewerPATCH 11/22 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 | 57 ++++++++++++++++++++++++++++- 4 files changed, 83 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu.h +3 -0
@@ -100,6 +100,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
@@ -423,6 +423,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
@@ -810,6 +817,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
@@ -1803,6 +1803,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 +56 -1
82 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH 12/22 iommu/amd: Add helper functions to manage private address region for each VM
In reply to Suravee SuthikulpanitSome parts of vIOMMU Private Address (IPA) region (i.e. Guest Device ID mappng and Guest Domain ID mapping) are managed per-VM during VM create and destroy.
Introduce helper functions to allocate and map, free and upmap per-VM IPA region.
--- drivers/iommu/amd/amd_iommu.h | 2 ++ drivers/iommu/amd/iommu.c | 4 +-- drivers/iommu/amd/nested.c | 1 + drivers/iommu/amd/viommu.c | 52 +++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -0
@@ -100,6 +100,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
@@ -2723,8 +2723,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 +52 -0
55 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH 13/22 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 | 15 +++++++++ drivers/iommu/amd/iommu.c | 1 + drivers/iommu/amd/iommufd.c | 5 +++ drivers/iommu/amd/viommu.c | 52 +++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -543,6 +543,9 @@ struct amd_iommu_viommu {* Indexed by guest domain ID. */ struct xarray gdomid_array; + +u64 *devid_table; +u64 *domid_table; };/*
drivers/iommu/amd/amd_viommu.h +15 -0
@@ -11,6 +11,11 @@int amd_viommu_init (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)
@@ -22,6 +27,16 @@ 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
@@ -82,6 +82,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_out; + ret =iommu_copy_struct_to_user (user_data, &data,IOMMU_VIOMMU_TYPE_AMD , reserved);
@@ -116,6 +120,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)spin_unlock_irqrestore (&pdom->lock, flags);xa_destroy (&aviommu->gdomid_array);amd_iommu_gid_free (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
Suravee Suthikulpanit
reviewerPATCH 14/22 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
@@ -386,6 +386,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] */
@@ -887,6 +892,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 */
@@ -1113,6 +1121,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;
@@ -123,6 +124,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 (); +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
@@ -130,4 +161,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 15/22 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 | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+)
drivers/iommu/amd/amd_viommu.h +2 -0
@@ -16,6 +16,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 +42 -0
68 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH 15/22] iommu/amd: Introduce helper function for updating domain ID mapping table
drivers/iommu/amd/viommu.c int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu · line 427
Quoted 12 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +12
-0+int amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid, +u16 hdom_id,u16 gdom_id) +{ +u64 val, tmp1, tmp2; +u8 __iomem *vfctrl =VIOMMU_VFCTRL_MMIO_BASE (iommu, gid); + + tmp1 = gdom_id; + tmp1 = ((tmp1 &0xFFFFULL ) <<46 ); + tmp2 = hdom_id; + tmp2 = ((tmp2 &0xFFFFULL ) <<14 ); + val = tmp1 | tmp2 |0x8000000000000001UL ; +writeq (val, vfctrl +VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET );
Use genmask and field prep. Check all the patches for stuff like this and fix it..
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH 15/22] iommu/amd: Introduce helper function for updating domain ID mapping table
Quoted 16 lines · Jason Gunthorpe
drivers/iommu/amd/viommu.c int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu · line 427
Quoted 12 lines · Suravee Suthikulpanit
drivers/iommu/amd/viommu.c +12
-0+int amd_viommu_domain_id_update (struct amd_iommu *iommu,u16 gid, +u16 hdom_id,u16 gdom_id) +{ +u64 val, tmp1, tmp2; +u8 __iomem *vfctrl =VIOMMU_VFCTRL_MMIO_BASE (iommu, gid); + + tmp1 = gdom_id; + tmp1 = ((tmp1 &0xFFFFULL ) <<46 ); + tmp2 = hdom_id; + tmp2 = ((tmp2 &0xFFFFULL ) <<14 ); + val = tmp1 | tmp2 |0x8000000000000001UL ; +writeq (val, vfctrl +VIOMMU_VFCTRL_GUEST_DID_MAP_CONTROL1_OFFSET );Use genmask and field prep. Check all the patches for stuff like this and fix it.
Got it. I missed this one.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH 16/22 iommu/amd: Introduce helper function for updating device ID mapping table
In reply to Suravee SuthikulpanitAMD vIOMMU hardware uses the Device ID mapping table to map Guest Device ID (GDevID) to Host Device ID when it virtualises guest IOMMU commands. It uses GID and GDevID to indexe into the table to look up host device ID.
Linux IOMMU driver programs the table entry using VFCntlMMIO Guest Device Map Control Register.
Introduce amd_viommu_set/clear_device_mapping(), which are used to set the entry when initialize the IOMMUFD vDevice. Clearing the entry is done during VM destroy.
--- drivers/iommu/amd/amd_viommu.h | 10 ++++++- drivers/iommu/amd/iommufd.c | 3 +++ drivers/iommu/amd/viommu.c | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_viommu.h +9 -1
@@ -18,6 +18,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)
@@ -25,7 +28,7 @@ static inline int amd_viommu_init(struct amd_iommu *iommu)return 0 ; } -u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid); +static inline u64 amd_viommu_get_vfmmio_addr (struct amd_iommu *iommu,u16 gid); {return 0 ; }
@@ -39,6 +42,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
@@ -133,6 +133,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 ();
@@ -151,6 +152,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 +48 -0
66 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH 17/22 iommu/amd: Introduce vIOMMU Translation DTE
In reply to Suravee SuthikulpanitAMD vIOMMU requires hypervisor to setup a special DTE to store GPA->SPA
mapping for each VM. This is used to translate base addresses of guest
IOMMU data structures (i.g. Command Buffer, Event Log, PPR Log) and the Completion-wait data store (as part of the Completion-wait command).
In Linux kernel, this is referred as vIOMMU Translation DTE (TransDTE), which is unique per VM. (Note that all IOMMU instances within a PCI segment share the same Device Table.) The device ID for the TransDTE is referred to as TransDevID, which must be an unused PCI BDF and must be unique for each VM.
Currently, TransDevID is specified by VMM, and passed to the IOMMU driver via struct iommu_viommu_amd during vIOMMU initialization. The driver programs the ID into the DeviceID field of the VFCntlMMIO Guest Miscellaneous Control Register. The register is cleared during VM destroy.
--- drivers/iommu/amd/amd_iommu.h | 8 +++++ drivers/iommu/amd/amd_iommu_types.h | 2 ++ drivers/iommu/amd/iommu.c | 51 +++++++++++++++++++++++++++++ drivers/iommu/amd/iommufd.c | 8 +++++ drivers/iommu/amd/viommu.c | 3 ++ include/uapi/linux/iommufd.h | 2 ++ 6 files changed, 74 insertions(+)
drivers/iommu/amd/amd_iommu.h +8 -0
@@ -209,8 +209,16 @@ 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); +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 +2 -0
@@ -494,6 +494,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#define VIOMMU_VF_MMIO_BASE (iommu, guestId) \ (iommu->vf_base + (guestId * VIOMMU_VF_MMIO_ENTRY_SIZE))
@@ -551,6 +552,7 @@ struct amd_iommu_viommu {u64 *devid_table;u64 *domid_table; +u16 trans_devid; };/*
drivers/iommu/amd/iommu.c +51 -0
57 lines not on this page · raw message
drivers/iommu/amd/iommufd.c +8 -0
@@ -83,6 +83,13 @@ 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); +amd_iommu_set_translate_dte (iommu, aviommu->gid, pdom, data.trans_devid); + +/* Set translate devid in vfctrl mmio */ +writeq ((data.trans_devid &0xFFFFULL ) <<16 , +VIOMMU_VFCTRL_MMIO_BASE (iommu, aviommu->gid) + +VIOMMU_VFCTRL_GUEST_MISC_CONTROL_OFFSET ); + ret =amd_viommu_init_one (iommu, aviommu);if (ret) goto err_out;
@@ -93,6 +100,7 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *if (ret) goto free_mmap; + aviommu->trans_devid = data.trans_devid; viommu->ops = &amd_viommu_ops;spin_lock_irqsave (&pdom->lock, flags);
drivers/iommu/amd/viommu.c +3 -0
@@ -428,6 +428,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); }
include/uapi/linux/iommufd.h +2 -0
@@ -1076,10 +1076,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 +* @trans_devid: GPA->GVA translation device ID (host) * @reserved: Must be zero */ struct iommu_viommu_amd {__aligned_u64 out_vfmmio_mmap_offset; +__u32 trans_devid;__u32 reserved;/* must be last */ };
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH 18/22 iommufd: Introduce iommufd_viommu_ops.hw_queue_init
In reply to Suravee SuthikulpanitThis is mainly used by AMD vIOMMU to initialize virtualize hardware queue using guest physical address where the address is specified via @hw_queue->base_addr.
--- 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
Suravee Suthikulpanit
reviewerPATCH 19/22 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 | 4 ++ drivers/iommu/amd/iommufd.c | 68 +++++++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 3 ++ 3 files changed, 75 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +4 -0
@@ -1127,6 +1127,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 +68 -0
78 lines not on this page · raw message
include/uapi/linux/iommufd.h +3 -0
@@ -1317,6 +1317,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 20/22 iommufd: Introduce vIOMMU option via IOMMU_OPTION ioctl
In reply to Suravee SuthikulpanitIntroduce a new iommufd_option enum value, IOMMU_OPTION_VIOMMU, to support vIOMMU-specific options. Extend the existing IOMMU_OPTION ioctl to accept key-value style input, enabling access to data arrays such as hardware registers.
Additionally, introduce struct iommufd_viommu_ops with set_option and get_option callbacks to support IOMMUFD vIOMMU options.
--- drivers/iommu/iommufd/ioas.c | 2 +- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 6 +++--- drivers/iommu/iommufd/viommu.c | 18 ++++++++++++++++++ include/linux/iommufd.h | 5 +++++ include/uapi/linux/iommufd.h | 8 ++++++-- 6 files changed, 34 insertions(+), 6 deletions(-)
drivers/iommu/iommufd/ioas.c +1 -1
@@ -643,7 +643,7 @@ int iommufd_ioas_option(struct iommufd_ucmd *ucmd)struct iommufd_ioas *ioas;int rc =0 ; -if (cmd->__reserved) +if (cmd->key)return -EOPNOTSUPP ; ioas =iommufd_get_ioas (ucmd->ictx, cmd->object_id);
drivers/iommu/iommufd/iommufd_private.h +1 -0
@@ -698,6 +698,7 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj);void iommufd_vdevice_abort (struct iommufd_object *obj);int iommufd_hw_queue_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_hw_queue_destroy (struct iommufd_object *obj); +int iommufd_viommu_option (struct iommufd_ucmd *ucmd);static inline struct iommufd_vdevice *iommufd_get_vdevice (struct iommufd_ctx *ictx,u32 id)
drivers/iommu/iommufd/main.c +3 -3
@@ -391,9 +391,6 @@ static int iommufd_option(struct iommufd_ucmd *ucmd)struct iommu_option *cmd = ucmd->cmd;int rc; -if (cmd->__reserved) -return -EOPNOTSUPP ; -switch (cmd->option_id) {case IOMMU_OPTION_RLIMIT_MODE : rc =iommufd_option_rlimit_mode (cmd, ucmd->ictx);
@@ -401,6 +398,9 @@ static int iommufd_option(struct iommufd_ucmd *ucmd)case IOMMU_OPTION_HUGE_PAGES : rc =iommufd_ioas_option (ucmd);break ; +case IOMMU_OPTION_VIOMMU : + rc =iommufd_viommu_option (ucmd); +break ;default :return -EOPNOTSUPP ; }
drivers/iommu/iommufd/viommu.c +18 -0
@@ -477,3 +477,21 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &viommu->obj);return rc; } + +int iommufd_viommu_option (struct iommufd_ucmd *ucmd) +{ +int ret = -EOPNOTSUPP ; +struct iommu_option *cmd = ucmd->cmd; +struct iommufd_viommu *viommu =iommufd_get_viommu (ucmd, cmd->object_id); + +if (cmd->op ==IOMMU_OPTION_OP_SET ) { +if (!viommu->ops->set_option) +return ret; + ret = viommu->ops->set_option (viommu, cmd->key, cmd->val64); + }else if (cmd->op ==IOMMU_OPTION_OP_GET ) { +if (!viommu->ops->get_option) +return ret; + ret = viommu->ops->get_option (viommu, cmd->key, &cmd->val64); + } +return ret; +}
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_option: Set the key-value option for the specified vIOMMU. +* @get_option: Get the key-value option for 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_option)(struct iommufd_viommu *viommu,u16 key,u64 value); +int (*get_option)(struct iommufd_viommu *viommu,u16 key,u64 *value); };#if IS_ENABLED (CONFIG_IOMMUFD )
include/uapi/linux/iommufd.h +6 -2
@@ -306,10 +306,14 @@ struct iommu_ioas_unmap {* iommu mappings. Value 0 disables combining, everything is mapped to * PAGE_SIZE. This can be useful for benchmarking. This is a per-IOAS * option, the object_id must be the IOAS ID. +* @IOMMU_KEY_VAL_OPTION_VIOMMU: +* Specifies key-value option for vIOMMU object. The caller must specify +* vIOMMU ID for object_id. The allowed ops are set and get. */ enum iommufd_option {IOMMU_OPTION_RLIMIT_MODE =0 ,IOMMU_OPTION_HUGE_PAGES =1 , +IOMMU_OPTION_VIOMMU =2 , };/**
@@ -328,7 +332,7 @@ enum iommufd_option_ops {* @size: sizeof(struct iommu_option) * @option_id: One of enum iommufd_option * @op: One of enum iommufd_option_ops -* @__reserved: Must be 0 +* @key: Option key to match with the value * @object_id: ID of the object if required * @val64: Option value to set or value returned on get *
@@ -340,7 +344,7 @@ struct iommu_option {__u32 size;__u32 option_id;__u16 op; -__u16 __reserved; +__u16 key;__u32 object_id;__aligned_u64 val64; };
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH 20/22] iommufd: Introduce vIOMMU option via IOMMU_OPTION ioctl
Quoted 7 lines · Suravee Suthikulpanit
Introduce a new
iommufd_optionenum value,IOMMU_OPTION_VIOMMU, to support vIOMMU-specific options. Extend the existingIOMMU_OPTIONioctl to accept key-value style input, enabling access to data arrays such as hardware registers.Additionally, introduce struct
iommufd_viommu_opswithset_optionandget_optioncallbacks to support IOMMUFD vIOMMU options.
This doesn't seem like the right API to use use for this. You should probably add a new ioctl for viommu "commands" or something like that which is better structured..
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH 20/22] iommufd: Introduce vIOMMU option via IOMMU_OPTION ioctl
Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Suravee Suthikulpanit
Introduce a new
iommufd_optionenum value,IOMMU_OPTION_VIOMMU, to support vIOMMU-specific options. Extend the existingIOMMU_OPTIONioctl to accept key-value style input, enabling access to data arrays such as hardware registers.
Quoted 8 lines
Quoted 2 lines
Additionally, introduce struct
iommufd_viommu_opswithset_optionandget_optioncallbacks to support IOMMUFD vIOMMU options.This doesn't seem like the right API to use use for this. You should probably add a new ioctl for viommu "commands" or something like that which is better structured..
Jason
Ok. I'll introduce a new IOCTL.
Suravee
Suravee Suthikulpanit
reviewerPATCH 21/22 iommu/amd: Handle set/get option for AMD vIOMMU hw_queue
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_option() and get_option().
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 | 145 ++++++++++++++++++++++++++++ 5 files changed, 168 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 +obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o viommu.o vfctrl_mmio.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu_types.h +5 -0
@@ -194,10 +194,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
@@ -21,6 +21,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)
@@ -47,6 +52,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
@@ -242,4 +242,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_option = amd_viommu_guest_mmio_write, + .get_option = amd_viommu_guest_mmio_read, };
drivers/iommu/amd/vfctrl_mmio.c new file +145 -0
145 lines not on this page · raw message
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH 22/22 iommu/amd: Introduce logic to check and enable vIOMMU feature
In reply to Suravee SuthikulpanitAlso switch to enable vIOMMU by default, and provide kernel boot option to disable the feature.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/init.c | 14 +++++++++++++- drivers/iommu/amd/viommu.c | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -25,6 +25,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
@@ -204,9 +204,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 +13 -1
@@ -195,7 +195,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 ;
@@ -436,6 +436,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);
@@ -3702,6 +3712,8 @@ static int __init parse_amd_iommu_options(char *str) }else if (strncmp (str,"v2_pgsizes_only" ,15 ) ==0 ) {pr_info ("Restricting V1 page-sizes to 4KiB/2MiB/1GiB" ); amd_iommu_pgsize_bitmap =AMD_IOMMU_PGSIZES_V2 ; + }else if (strncmp (str,"viommu_disable" ,14 ) ==0 ) { + amd_iommu_viommu = false; }else {pr_notice ("Unknown option - '%s'\n" , str); }
drivers/iommu/amd/viommu.c +16 -0
@@ -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 );
@@ -298,6 +310,10 @@ int __init amd_viommu_init(struct amd_iommu *iommu)set_iommu_dte (iommu); + ret =viommu_enable (iommu); +if (ret) +return ret; +return 0 ; }
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH 22/22] iommu/amd: Introduce logic to check and enable vIOMMU feature
Quoted 2 lines · Suravee Suthikulpanit
Also switch to enable vIOMMU by default, and provide kernel boot option to disable the feature.
Why? userspace is free to not use it, lets not pollute the kernel command line.
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH 22/22] iommu/amd: Introduce logic to check and enable vIOMMU feature
Quoted 8 lines · Jason Gunthorpe
Quoted 2 lines · Suravee Suthikulpanit
Also switch to enable vIOMMU by default, and provide kernel boot option to disable the feature.
Why? userspace is free to not use it, lets not pollute the kernel command line.
Jason
I just wanted an option to disable this feature in case it is ever needed. I can remove this.
Thanks,
Suravee