iommu/amd: Introduce Nested Translation support
Subsystems (3)
Suravee Suthikulpanit
reviewerv4 cover iommu/amd: Introduce Nested Translation support
This series introduces support for AMD IOMMU nested page table translation with the host (v1) and guest (v2) page tables.
In this mode, the AMD IOMMU driver configures the Device Table Entry (DTE) with host page table root pointer, which is configured by allocating domain with page table type IOMMU_HWPT_ALLOC_NEST_PARENT.
The guest page tables and Guest CR3 (GCR3) tables are managed by Guest OS, and stored in the guest DTE (gDTE) in guest memory. VMM is responsible for passing gDTE information to the host IOMMU driver using struct iommu_hwpt_amd_guest when allocating a domain type IOMMU_DOMAIN_NESTED. Then, the gDTE is parsed and program onto host DTE by the AMD IOMMU driver.
In addition, this series introduces base code for IOMMUFD vIOMMU for AMD IOMMU, and implements vIOMMU-based nested domain allocation interface. The struct nested_domain to store nested domain information, and set_dte_nested() helper function to handle DTE programing for the nested domain.
The series is separated into two parts:
* Patch 1-7 are preparatory patches.
* Patch 8-16 implement nest-parent and nested domains support for IOMMUFD vIOMMU.
Note: This series is rebased on top of:
* [PATCH v5] iommu/amd: Add support for hw_info for iommu capability query
https://lore.kernel.org/linux-iommu/[email protected]/T/#u
Changes from V3:
(https://lore.kernel.org/linux-iommu/[email protected])
* (New) Patch 11 introduces struct amd_iommu_viommu and amd_iommufd_viommu_init(), which contain minimal code to store reference to nest parent domain for nested domain attachment. (Per Jason / Nicolin)
* (New) Patch 13 refactor hDomID-related logic, and invalidate host (S2) page table with host domain IDs stored in an xarray. (Per Jason)
* Patch 15: Clean up logic in set_dte_entry().
Thanks,
Suravee
Suravee Suthikulpanit (16):
iommu/amd: Rename DEV_DOMID_MASK to DTE_DOMID_MASK iommu/amd: Make amd_iommu_pdom_id_alloc() non-static iommu/amd: Make amd_iommu_pdom_id_free() non-static iommu/amd: Make amd_iommu_device_flush_dte() non-static iommu/amd: Make amd_iommu_update_dte256() non-static iommu/amd: Make amd_iommu_make_clear_dte() non-static inline iommu/amd: Make amd_iommu_completion_wait() non-static
iommufd: Introduce data struct for AMD nested domain allocation iommu/amd: Always enable GCR3TRPMode when supported. iommu/amd: Add support for nest parent domain allocation iommu/amd: Introduce struct amd_iommu_viommu iommu/amd: Add support for nested domain allocation iommu/amd: Track host Domain ID mapping for each guest Domain ID iommu/amd: Refactor persistent DTE bits programming into
amd_iommu_make_clear_dte()
iommu/amd: Refactor logic to program the host page table in DTE iommu/amd: Add support for nested domain attach/detach
drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 36 +++++ drivers/iommu/amd/amd_iommu_types.h | 28 +++- drivers/iommu/amd/init.c | 3 + drivers/iommu/amd/iommu.c | 200 ++++++++++++++---------- drivers/iommu/amd/iommufd.c | 17 ++ drivers/iommu/amd/iommufd.h | 5 + drivers/iommu/amd/nested.c | 233 ++++++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 11 ++ 9 files changed, 453 insertions(+), 82 deletions(-) create mode 100644 drivers/iommu/amd/nested.c
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH v4 1/16 iommu/amd: Rename DEV_DOMID_MASK to DTE_DOMID_MASK
Also change the define to use GENMASK_ULL instead. There is no functional change.
--- drivers/iommu/amd/amd_iommu_types.h | 2 +- drivers/iommu/amd/iommu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +1 -1
@@ -422,7 +422,7 @@#define DTE_FLAG_IOTLB BIT_ULL(32)#define DTE_FLAG_MASK (0x3ffULL << 32) -#define DEV_DOMID_MASK 0xffffULL +#define DTE_DOMID_MASK GENMASK_ULL(15, 0)#define DTE_GCR3_14_12 GENMASK_ULL(60, 58)#define DTE_GCR3_30_15 GENMASK_ULL(31, 16)
drivers/iommu/amd/iommu.c +1 -1
@@ -2094,7 +2094,7 @@ static void set_dte_entry(struct amd_iommu *iommu,if (dev_data->ats_enabled) new.data[1 ] |=DTE_FLAG_IOTLB ; - old_domid =READ_ONCE (dte->data[1 ]) &DEV_DOMID_MASK ; + old_domid =READ_ONCE (dte->data[1 ]) &DTE_DOMID_MASK ; new.data[1 ] |= domid;/*
Signature · 1 line
2.34.1
Re: [PATCH v4 01/16] iommu/amd: Rename DEV_DOMID_MASK to DTE_DOMID_MASK
Quoted 6 lines · Suravee Suthikulpanit
Also change the define to use
GENMASK_ULLinstead. There is no functional change.Reviewed-by Nicolin Chen <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
-Vasant
Suravee Suthikulpanit
reviewerPATCH v4 2/16 iommu/amd: Make amd_iommu_pdom_id_alloc() non-static
In reply to Suravee SuthikulpanitThis will be reused in a new iommufd.c file for nested translation.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/iommu.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-)
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); +int amd_iommu_pdom_id_alloc (void );#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );
drivers/iommu/amd/iommu.c +4 -4
@@ -1818,7 +1818,7 @@ int amd_iommu_complete_ppr(struct device *dev, u32 pasid, int status, int tag)* ****************************************************************************/ -static int pdom_id_alloc (void ) +int amd_iommu_pdom_id_alloc (void ) {return ida_alloc_range (&pdom_ids,1 ,MAX_DOMAIN_ID -1 ,GFP_ATOMIC ); }
@@ -1906,7 +1906,7 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,return -EBUSY ;/* Allocate per device domain ID */ - domid =pdom_id_alloc (); + domid =amd_iommu_pdom_id_alloc ();if (domid <=0 )return -ENOSPC ; gcr3_info->domid = domid;
@@ -2489,7 +2489,7 @@ struct protection_domain *protection_domain_alloc(void)if (!domain)return NULL ; - domid =pdom_id_alloc (); + domid =amd_iommu_pdom_id_alloc ();if (domid <=0 ) {kfree (domain);return NULL ;
@@ -2681,7 +2681,7 @@ void amd_iommu_init_identity_domain(void) domain->ops = &identity_domain_ops; domain->owner = &amd_iommu_ops; - identity_domain.id =pdom_id_alloc (); + identity_domain.id =amd_iommu_pdom_id_alloc ();protection_domain_init (&identity_domain); }
Signature · 1 line
2.34.1
Re: [PATCH v4 02/16] iommu/amd: Make amd_iommu_pdom_id_alloc() non-static
Quoted 1 line · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.
s/iommufd.c/nested.c/ -OR- something like "it will be used in nested translation support".
Ditto for next few patches.
Quoted 4 lines
Reviewed-by Jason Gunthorpe <[email protected]>Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
-Vasant
Quoted 57 lines
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/iommu.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-)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); +int amd_iommu_pdom_id_alloc (void );#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );drivers/iommu/amd/iommu.c +4
-4@@ -1818,7 +1818,7 @@ int amd_iommu_complete_ppr(struct device *dev, u32 pasid, int status, int tag)* ****************************************************************************/ -static int pdom_id_alloc (void ) +int amd_iommu_pdom_id_alloc (void ) {return ida_alloc_range (&pdom_ids,1 ,MAX_DOMAIN_ID -1 ,GFP_ATOMIC ); }@@ -1906,7 +1906,7 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,return -EBUSY ;/* Allocate per device domain ID */ - domid =pdom_id_alloc (); + domid =amd_iommu_pdom_id_alloc ();if (domid <=0 )return -ENOSPC ; gcr3_info->domid = domid;@@ -2489,7 +2489,7 @@ struct protection_domain *protection_domain_alloc(void)if (!domain)return NULL ; - domid =pdom_id_alloc (); + domid =amd_iommu_pdom_id_alloc ();if (domid <=0 ) {kfree (domain);return NULL ;@@ -2681,7 +2681,7 @@ void amd_iommu_init_identity_domain(void) domain->ops = &identity_domain_ops; domain->owner = &amd_iommu_ops; - identity_domain.id =pdom_id_alloc (); + identity_domain.id =amd_iommu_pdom_id_alloc ();protection_domain_init (&identity_domain); }
Suravee Suthikulpanit
reviewerPATCH v4 3/16 iommu/amd: Make amd_iommu_pdom_id_free() non-static
In reply to Suravee SuthikulpanitThis will be reused in a new iommufd.c file for nested translation.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/iommu.c | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -27,6 +27,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);int amd_iommu_pdom_id_alloc (void ); +void amd_iommu_pdom_id_free (int id);#ifdef CONFIG_AMD_IOMMU_DEBUGFS void amd_iommu_debugfs_setup (void );
drivers/iommu/amd/iommu.c +5 -5
@@ -1823,7 +1823,7 @@ int amd_iommu_pdom_id_alloc(void)return ida_alloc_range (&pdom_ids,1 ,MAX_DOMAIN_ID -1 ,GFP_ATOMIC ); } -static void pdom_id_free (int id) +void amd_iommu_pdom_id_free (int id) {ida_free (&pdom_ids, id); }
@@ -1870,7 +1870,7 @@ static void free_gcr3_table(struct gcr3_tbl_info *gcr3_info) gcr3_info->glx =0 ;/* Free per device domain ID */ -pdom_id_free (gcr3_info->domid); +amd_iommu_pdom_id_free (gcr3_info->domid);iommu_free_pages (gcr3_info->gcr3_tbl); gcr3_info->gcr3_tbl =NULL ;
@@ -1913,7 +1913,7 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info, gcr3_info->gcr3_tbl =iommu_alloc_pages_node_sz (nid,GFP_ATOMIC ,SZ_4K );if (gcr3_info->gcr3_tbl ==NULL ) { -pdom_id_free (domid); +amd_iommu_pdom_id_free (domid);return -ENOMEM ; }
@@ -2573,7 +2573,7 @@ do_iommu_domain_alloc(struct device *dev, u32 flags, domain->pd_mode = pgtable; ret =pdom_setup_pgtable (domain, dev);if (ret) { -pdom_id_free (domain->id); +amd_iommu_pdom_id_free (domain->id);kfree (domain);return ERR_PTR (ret); }
@@ -2631,7 +2631,7 @@ void amd_iommu_domain_free(struct iommu_domain *dom)WARN_ON (!list_empty (&domain->dev_list));if (domain->domain.type & __IOMMU_DOMAIN_PAGING)free_io_pgtable_ops (&domain->iop.pgtbl.ops); -pdom_id_free (domain->id); +amd_iommu_pdom_id_free (domain->id);kfree (domain); }
Signature · 1 line
2.34.1
Re: [PATCH v4 03/16] iommu/amd: Make amd_iommu_pdom_id_free() non-static
Quoted 5 lines · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.Reviewed-by Nicolin Chen <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
-Vasant
Suravee Suthikulpanit
reviewerPATCH v4 4/16 iommu/amd: Make amd_iommu_device_flush_dte() non-static
In reply to Suravee SuthikulpanitThis will be reused in a new iommufd.c file for nested translation.
--- drivers/iommu/amd/amd_iommu.h | 3 +++ drivers/iommu/amd/iommu.c | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_iommu.h +3 -0
@@ -188,4 +188,7 @@ void amd_iommu_domain_set_pgtable(struct protection_domain *domain,struct dev_table_entry *get_dev_table (struct amd_iommu *iommu);struct iommu_dev_data *search_dev_data (struct amd_iommu *iommu,u16 devid); +/* DTE */ +int amd_iommu_device_flush_dte (struct iommu_dev_data *dev_data); +#endif /* AMD_IOMMU_H */
drivers/iommu/amd/iommu.c +4 -4
@@ -1562,7 +1562,7 @@ static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data)/* * Command send function for invalidating a device table entry */ -static int device_flush_dte (struct iommu_dev_data *dev_data) +int amd_iommu_device_flush_dte (struct iommu_dev_data *dev_data) {struct amd_iommu *iommu =get_amd_iommu_from_dev_data (dev_data);struct pci_dev *pdev =NULL ;
@@ -1788,7 +1788,7 @@ void amd_iommu_update_and_flush_device_table(struct protection_domain *domain) }list_for_each_entry (dev_data, &domain->dev_list, list) -device_flush_dte (dev_data); +amd_iommu_device_flush_dte (dev_data);domain_flush_complete (domain); }
@@ -2144,7 +2144,7 @@ static void dev_update_dte(struct iommu_dev_data *dev_data, bool set)clear_dte_entry (iommu, dev_data);clone_aliases (iommu, dev_data->dev); -device_flush_dte (dev_data); +amd_iommu_device_flush_dte (dev_data);iommu_completion_wait (iommu); }
@@ -2874,7 +2874,7 @@ static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain,spin_unlock (&dev_data->dte_lock);/* Flush device DTE */ -device_flush_dte (dev_data); +amd_iommu_device_flush_dte (dev_data); domain_flush = true; }
Signature · 1 line
2.34.1
Re: [PATCH v4 04/16] iommu/amd: Make amd_iommu_device_flush_dte() non-static
Quoted 5 lines · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.Reviewed-by Nicolin Chen <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
-Vasant
Suravee Suthikulpanit
reviewerPATCH v4 5/16 iommu/amd: Make amd_iommu_update_dte256() non-static
In reply to Suravee SuthikulpanitThis will be reused in a new iommufd.c file for nested translation.
--- drivers/iommu/amd/amd_iommu.h | 3 +++ drivers/iommu/amd/iommu.c | 11 ++++++----- 2 files changed, 9 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +3 -0
@@ -190,5 +190,8 @@ struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid);/* DTE */ int amd_iommu_device_flush_dte (struct iommu_dev_data *dev_data); +void amd_iommu_update_dte256 (struct amd_iommu *iommu, +struct iommu_dev_data *dev_data, +struct dev_table_entry *new);#endif /* AMD_IOMMU_H */
drivers/iommu/amd/iommu.c +6 -5
@@ -132,8 +132,9 @@ static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_ent* This function is used only by code, which updates DMA translation part of the DTE. * So, only consider control bits related to DMA when updating the entry. */ -static void update_dte256 (struct amd_iommu *iommu,struct iommu_dev_data *dev_data, -struct dev_table_entry *new) +void amd_iommu_update_dte256 (struct amd_iommu *iommu, +struct iommu_dev_data *dev_data, +struct dev_table_entry *new) {unsigned long flags;struct dev_table_entry *dev_table =get_dev_table (iommu);
@@ -413,7 +414,7 @@ static int clone_alias(struct pci_dev *pdev, u16 alias, void *data) ret = -EINVAL ; goto out; } -update_dte256 (iommu, alias_data, &new); +amd_iommu_update_dte256 (iommu, alias_data, &new);amd_iommu_set_rlookup_table (iommu, alias); out:
@@ -2109,7 +2110,7 @@ static void set_dte_entry(struct amd_iommu *iommu,set_dte_gcr3_table (iommu, dev_data, &new); -update_dte256 (iommu, dev_data, &new); +amd_iommu_update_dte256 (iommu, dev_data, &new);/* * A kdump kernel might be replacing a domain ID that was copied from
@@ -2130,7 +2131,7 @@ static void clear_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_struct dev_table_entry *dte = &get_dev_table (iommu)[dev_data->devid];make_clear_dte (dev_data, dte, &new); -update_dte256 (iommu, dev_data, &new); +amd_iommu_update_dte256 (iommu, dev_data, &new); }/* Update and flush DTE for the given device */
Signature · 1 line
2.34.1
Re: [PATCH v4 05/16] iommu/amd: Make amd_iommu_update_dte256() non-static
Quoted 4 lines · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
-Vasant
Suravee Suthikulpanit
reviewerPATCH v4 6/16 iommu/amd: Make amd_iommu_make_clear_dte() non-static inline
In reply to Suravee SuthikulpanitThis will be reused in a new iommufd.c file for nested translation.
Also, remove unused function parameter ptr.
--- drivers/iommu/amd/amd_iommu.h | 7 +++++++ drivers/iommu/amd/iommu.c | 13 ++----------- 2 files changed, 9 insertions(+), 11 deletions(-)
drivers/iommu/amd/amd_iommu.h +7 -0
@@ -193,5 +193,12 @@ int amd_iommu_device_flush_dte(struct iommu_dev_data *dev_data);void amd_iommu_update_dte256 (struct amd_iommu *iommu,struct iommu_dev_data *dev_data,struct dev_table_entry *new); +static inline void +amd_iommu_make_clear_dte (struct iommu_dev_data *dev_data,struct dev_table_entry *new) +{ +/* All existing DTE must have V bit set */ + new->data128[0 ] =DTE_FLAG_V ; + new->data128[1 ] =0 ; +}#endif /* AMD_IOMMU_H */
drivers/iommu/amd/iommu.c +2 -11
@@ -2008,14 +2008,6 @@ int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid)return ret; } -static void make_clear_dte (struct iommu_dev_data *dev_data,struct dev_table_entry *ptr, -struct dev_table_entry *new) -{ -/* All existing DTE must have V bit set */ - new->data128[0 ] =DTE_FLAG_V ; - new->data128[1 ] =0 ; -} -/* * Note: * The old value for GCR3 table and GPT have been cleared from caller.
@@ -2068,7 +2060,7 @@ static void set_dte_entry(struct amd_iommu *iommu,else domid = domain->id; -make_clear_dte (dev_data, dte, &new); +amd_iommu_make_clear_dte (dev_data, &new);if (domain->iop.mode !=PAGE_MODE_NONE ) new.data[0 ] |=iommu_virt_to_phys (domain->iop.root);
@@ -2128,9 +2120,8 @@ static void set_dte_entry(struct amd_iommu *iommu,static void clear_dte_entry (struct amd_iommu *iommu,struct iommu_dev_data *dev_data) {struct dev_table_entry new = {}; -struct dev_table_entry *dte = &get_dev_table (iommu)[dev_data->devid]; -make_clear_dte (dev_data, dte, &new); +amd_iommu_make_clear_dte (dev_data, &new);amd_iommu_update_dte256 (iommu, dev_data, &new); }
Signature · 1 line
2.34.1
Re: [PATCH v4 06/16] iommu/amd: Make amd_iommu_make_clear_dte() non-static inline
Quoted 6 lines · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.Also, remove unused function parameter ptr.
Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
-Vasant
Suravee Suthikulpanit
reviewerPATCH v4 7/16 iommu/amd: Make amd_iommu_completion_wait() non-static
In reply to Suravee SuthikulpanitThis will be reused in a new iommufd.c file for nested translation.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/iommu.c | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -187,6 +187,7 @@ void amd_iommu_domain_set_pgtable(struct protection_domain *domain, u64 *root, int mode);struct dev_table_entry *get_dev_table (struct amd_iommu *iommu);struct iommu_dev_data *search_dev_data (struct amd_iommu *iommu,u16 devid); +int amd_iommu_completion_wait (struct amd_iommu *iommu);/* DTE */ int amd_iommu_device_flush_dte (struct iommu_dev_data *dev_data);
drivers/iommu/amd/iommu.c +12 -12
@@ -1392,7 +1392,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;
@@ -1431,7 +1431,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)
@@ -1449,7 +1449,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)
@@ -1460,7 +1460,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); }/*
@@ -1479,7 +1479,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)
@@ -1490,7 +1490,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 void amd_iommu_flush_all (struct amd_iommu *iommu)
@@ -1500,7 +1500,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)
@@ -1523,7 +1523,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)
@@ -1748,7 +1748,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,
@@ -2137,7 +2137,7 @@ static void dev_update_dte(struct iommu_dev_data *dev_data, bool set)clone_aliases (iommu, dev_data->dev);amd_iommu_device_flush_dte (dev_data); -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); }/*
@@ -2421,7 +2421,7 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev) 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 ;
@@ -3255,7 +3255,7 @@ static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu,set_remap_table_entry (iommu, alias, table); out_wait: -iommu_completion_wait (iommu); +amd_iommu_completion_wait (iommu); out_unlock:spin_unlock_irqrestore (&iommu_table_lock, flags);
Signature · 1 line
2.34.1
Vasant Hegde
maintainerRe: [PATCH v4 07/16] iommu/amd: Make amd_iommu_completion_wait() non-static
Quoted 4 lines · Suravee Suthikulpanit
This will be reused in a new
iommufd.cfile for nested translation.Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
Patch looks good. But like Jason mentioned in oatch 16, if we use dev_update_dte() we may not needs this patch.
-Vasant
Suravee Suthikulpanit
reviewerPATCH v4 8/16 iommufd: Introduce data struct for AMD nested domain allocation
In reply to Suravee SuthikulpanitIntroduce IOMMU_HWPT_DATA_AMD_GUEST data type for IOMMU guest page table, which is used for stage-1 in nested translation. The data structure contains information necessary for setting up the AMD HW-vIOMMU support.
--- include/uapi/linux/iommufd.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/uapi/linux/iommufd.h +11 -0
@@ -455,16 +455,27 @@ struct iommu_hwpt_arm_smmuv3 {__aligned_le64 ste[2 ]; }; +/** +* struct iommu_hwpt_amd_guest - AMD IOMMU guest I/O page table data +* (IOMMU_HWPT_DATA_AMD_GUEST) +* @dte: Guest Device Table Entry (DTE) +*/ +struct iommu_hwpt_amd_guest { +__aligned_u64 dte[4 ]; +}; +/** * enum iommu_hwpt_data_type - IOMMU HWPT Data Type * @IOMMU_HWPT_DATA_NONE: no data * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table * @IOMMU_HWPT_DATA_ARM_SMMUV3: ARM SMMUv3 Context Descriptor Table +* @IOMMU_HWPT_DATA_AMD_GUEST: AMD IOMMU guest page table */ enum iommu_hwpt_data_type {IOMMU_HWPT_DATA_NONE =0 ,IOMMU_HWPT_DATA_VTD_S1 =1 ,IOMMU_HWPT_DATA_ARM_SMMUV3 =2 , +IOMMU_HWPT_DATA_AMD_GUEST =3 , };/**
Signature · 1 line
2.34.1
Re: [PATCH v4 08/16] iommufd: Introduce data struct for AMD nested domain allocation
Quoted 7 lines · Suravee Suthikulpanit
Introduce
IOMMU_HWPT_DATA_AMD_GUESTdata type for IOMMU guest page table, which is used for stage-1 in nested translation. The data structure contains information necessary for setting up the AMD HW-vIOMMU support.Reviewed-by Jason Gunthorpe <[email protected]>Reviewed-by Nicolin Chen <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
-Vasant
Quoted 36 lines
--- include/uapi/linux/iommufd.h | 11 +++++++++++ 1 file changed, 11 insertions(+)include/uapi/linux/iommufd.h +11
-0@@ -455,16 +455,27 @@ struct iommu_hwpt_arm_smmuv3 {__aligned_le64 ste[2 ]; }; +/** +* struct iommu_hwpt_amd_guest - AMD IOMMU guest I/O page table data +* (IOMMU_HWPT_DATA_AMD_GUEST) +* @dte: Guest Device Table Entry (DTE) +*/ +struct iommu_hwpt_amd_guest { +__aligned_u64 dte[4 ]; +}; +/** * enum iommu_hwpt_data_type - IOMMU HWPT Data Type * @IOMMU_HWPT_DATA_NONE: no data * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table * @IOMMU_HWPT_DATA_ARM_SMMUV3: ARM SMMUv3 Context Descriptor Table +* @IOMMU_HWPT_DATA_AMD_GUEST: AMD IOMMU guest page table */ enum iommu_hwpt_data_type {IOMMU_HWPT_DATA_NONE =0 ,IOMMU_HWPT_DATA_VTD_S1 =1 ,IOMMU_HWPT_DATA_ARM_SMMUV3 =2 , +IOMMU_HWPT_DATA_AMD_GUEST =3 , };/**
Suravee Suthikulpanit
reviewerPATCH v4 9/16 iommu/amd: Always enable GCR3TRPMode when supported.
In reply to Suravee SuthikulpanitThe GCR3TRPMode feature allows the DTE[GCR3TRP] field to be configured with GPA (instead of SPA). This simplifies the implementation, and is a pre-requisite for nested translation support.
Therefore, always enable this feature if available.
--- drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/init.c | 3 +++ 2 files changed, 4 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +1 -0
@@ -185,6 +185,7 @@#define CONTROL_EPH_EN 45#define CONTROL_XT_EN 50#define CONTROL_INTCAPXT_EN 51 +#define CONTROL_GCR3TRPMODE 58#define CONTROL_IRTCACHEDIS 59#define CONTROL_SNPAVIC_EN 61
drivers/iommu/amd/init.c +3 -0
@@ -1122,6 +1122,9 @@ static void iommu_enable_gt(struct amd_iommu *iommu)return ;iommu_feature_enable (iommu,CONTROL_GT_EN ); + +if (check_feature2 (FEATURE_GCR3TRPMODE )) +iommu_feature_enable (iommu,CONTROL_GCR3TRPMODE ); }/* sets a specific bit in the device table entry. */
Signature · 1 line
2.34.1
Re: [PATCH v4 09/16] iommu/amd: Always enable GCR3TRPMode when supported.
Quoted 8 lines · Suravee Suthikulpanit
The GCR3TRPMode feature allows the DTE[GCR3TRP] field to be configured with GPA (instead of SPA). This simplifies the implementation, and is a pre-requisite for nested translation support.
Therefore, always enable this feature if available.
Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
Vasant Hegde
maintainerRe: [PATCH v4 09/16] iommu/amd: Always enable GCR3TRPMode when supported.
In reply to Suravee SuthikulpanitQuoted 30 lines · Suravee Suthikulpanit
The GCR3TRPMode feature allows the DTE[GCR3TRP] field to be configured with GPA (instead of SPA). This simplifies the implementation, and is a pre-requisite for nested translation support.
Therefore, always enable this feature if available.
Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/init.c | 3 +++ 2 files changed, 4 insertions(+)drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -185,6 +185,7 @@#define CONTROL_EPH_EN 45#define CONTROL_XT_EN 50#define CONTROL_INTCAPXT_EN 51 +#define CONTROL_GCR3TRPMODE 58#define CONTROL_IRTCACHEDIS 59#define CONTROL_SNPAVIC_EN 61drivers/iommu/amd/init.c +0
-0
drivers/iommu/amd/init.c static void iommu_enable_gt(struct amd_iommu *iommu) · line 1127
Quoted 6 lines
drivers/iommu/amd/init.c +3
-0@@ -1122,6 +1122,9 @@ static void iommu_enable_gt(struct amd_iommu *iommu)return ;iommu_feature_enable (iommu,CONTROL_GT_EN ); + +if (check_feature2 (FEATURE_GCR3TRPMODE )) +iommu_feature_enable (iommu,CONTROL_GCR3TRPMODE );
This works for now as iommu_snp_enable() enable is getting called later. But how about moving it to iommu_init_flags() ? Also we should probably add a comment here.
Not for this series, but may be we should rename FEATURE -> FEATURE2 so that its clear that these are coming from EFR2 register.
-Vasant
Suthikulpanit, Suravee
reviewerRe: [PATCH v4 09/16] iommu/amd: Always enable GCR3TRPMode when supported.
Quoted 35 lines · Vasant Hegde
Quoted 32 lines · Suravee Suthikulpanit
The GCR3TRPMode feature allows the DTE[GCR3TRP] field to be configured with GPA (instead of SPA). This simplifies the implementation, and is a pre-requisite for nested translation support.
Therefore, always enable this feature if available.
Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/init.c | 3 +++ 2 files changed, 4 insertions(+)drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -185,6 +185,7 @@#define CONTROL_EPH_EN 45#define CONTROL_XT_EN 50#define CONTROL_INTCAPXT_EN 51 +#define CONTROL_GCR3TRPMODE 58#define CONTROL_IRTCACHEDIS 59#define CONTROL_SNPAVIC_EN 61drivers/iommu/amd/init.c +0
-0@@ -1122,6 +1122,9 @@ static void iommu_enable_gt(struct amd_iommu *iommu)return ;
Quoted 7 lines
drivers/iommu/amd/init.c static void iommu_enable_gt(struct amd_iommu *iommu) · line 1127
Quoted 4 lines
drivers/iommu/amd/init.c +3
-0iommu_feature_enable (iommu,CONTROL_GT_EN ); + +if (check_feature2 (FEATURE_GCR3TRPMODE )) +iommu_feature_enable (iommu,CONTROL_GCR3TRPMODE );This works for now as
iommu_snp_enable()enable is getting called later. But how about moving it toiommu_init_flags()?
I put it here because the GCR3TRP feature depends on GT supported / enabled, which is checked in this function. This avoid duplicate checks.
Quoted 1 line
Also we should probably add a comment here.
I will note in the comment regarding this needs to be called prior to iommu_snp_enable()
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 10/16 iommu/amd: Add support for nest parent domain allocation
In reply to Suravee SuthikulpanitTo support nested translation, the nest parent domain is allocated with IOMMU_HWPT_ALLOC_NEST_PARENT flag, and stores information of the v1 page table for stage 2 (i.e. GPA->SPA).
Also, only support nest parent domain on AMD system, which can support the Guest CR3 Table (GCR3TRPMode) feature. This feature is required in order to program DTE[GCR3 Table Root Pointer] with the GPA.
--- drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/iommu.c | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +1 -0
@@ -107,6 +107,7 @@/* Extended Feature 2 Bits */ +#define FEATURE_GCR3TRPMODE BIT_ULL(3)#define FEATURE_SNPAVICSUP GENMASK_ULL(7, 5)#define FEATURE_SNPAVICSUP_GAM (x) \ (FIELD_GET(FEATURE_SNPAVICSUP, x) == 0x1)
drivers/iommu/amd/iommu.c +23 -3
@@ -2584,6 +2584,14 @@ do_iommu_domain_alloc(struct device *dev, u32 flags,return &domain->domain; } +static inline bool is_nest_parent_supported (u32 flags) +{ +/* Only allow nest parent when these features are supported */ +return check_feature (FEATURE_GT ) && +check_feature (FEATURE_GIOSUP ) && +check_feature2 (FEATURE_GCR3TRPMODE ); +} +static struct iommu_domain *amd_iommu_domain_alloc_paging_flags (struct device *dev,u32 flags,const struct iommu_user_data *user_data)
@@ -2591,16 +2599,28 @@ amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, {struct amd_iommu *iommu =get_amd_iommu_from_dev (dev);const u32 supported_flags =IOMMU_HWPT_ALLOC_DIRTY_TRACKING | -IOMMU_HWPT_ALLOC_PASID ; +IOMMU_HWPT_ALLOC_PASID | +IOMMU_HWPT_ALLOC_NEST_PARENT ;if ((flags & ~supported_flags) || user_data)return ERR_PTR (-EOPNOTSUPP );switch (flags & supported_flags) {case IOMMU_HWPT_ALLOC_DIRTY_TRACKING : -/* Allocate domain with v1 page table for dirty tracking */ -if (!amd_iommu_hd_support (iommu)) +case IOMMU_HWPT_ALLOC_NEST_PARENT : +case IOMMU_HWPT_ALLOC_DIRTY_TRACKING |IOMMU_HWPT_ALLOC_NEST_PARENT : +/* +* Allocate domain with v1 page table for dirty tracking +* and/or Nest parent. +*/ +if ((flags &IOMMU_HWPT_ALLOC_DIRTY_TRACKING ) && + !amd_iommu_hd_support (iommu)) +break ; + +if ((flags &IOMMU_HWPT_ALLOC_NEST_PARENT ) && + !is_nest_parent_supported (flags))break ; +return do_iommu_domain_alloc (dev, flags,PD_MODE_V1 );case IOMMU_HWPT_ALLOC_PASID :/* Allocate domain with v2 page table if IOMMU supports PASID. */
Signature · 1 line
2.34.1
Re: [PATCH v4 10/16] iommu/amd: Add support for nest parent domain allocation
drivers/iommu/amd/amd_iommu_types.h line 113
Quoted 12 lines · Suravee Suthikulpanit
drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -107,6 +107,7 @@/* Extended Feature 2 Bits */ +#define FEATURE_GCR3TRPMODE BIT_ULL(3)#define FEATURE_SNPAVICSUP GENMASK_ULL(7, 5)#define FEATURE_SNPAVICSUP_GAM (x) \ (FIELD_GET(FEATURE_SNPAVICSUP, x) == 0x1)
This now should be moved to the previous patch?
Nicolin
Suthikulpanit, Suravee
reviewerRe: [PATCH v4 10/16] iommu/amd: Add support for nest parent domain allocation
Quoted 16 lines · Nicolin Chen
drivers/iommu/amd/amd_iommu_types.h line 113
Quoted 12 lines · Suravee Suthikulpanit
drivers/iommu/amd/amd_iommu_types.h +1
-0@@ -107,6 +107,7 @@/* Extended Feature 2 Bits */ +#define FEATURE_GCR3TRPMODE BIT_ULL(3)#define FEATURE_SNPAVICSUP GENMASK_ULL(7, 5)#define FEATURE_SNPAVICSUP_GAM (x) \ (FIELD_GET(FEATURE_SNPAVICSUP, x) == 0x1)This now should be moved to the previous patch?
Quoted 1 line
Nicolin
Ah, I was moving stuff around and missed this.
Thanks,
Suravee
Suravee Suthikulpanit
reviewerPATCH v4 11/16 iommu/amd: Introduce struct amd_iommu_viommu
In reply to Suravee SuthikulpanitWhich stores reference to nested parent domain assigned during the call to struct iommu_ops.viommu_init(). Information in the nest parent is needed when setting up the nested translation.
Note that the viommu initialization will be introduced in subsequent commit.
--- drivers/iommu/amd/amd_iommu_types.h | 6 ++++++ drivers/iommu/amd/iommu.c | 4 +++- drivers/iommu/amd/iommufd.c | 17 +++++++++++++++++ drivers/iommu/amd/iommufd.h | 5 +++++ 4 files changed, 31 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +6 -0
@@ -17,6 +17,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/pci.h> +#include <linux/iommufd.h> #include <linux/irqreturn.h> #include <linux/io-pgtable.h>
@@ -586,6 +587,11 @@ struct pdom_iommu_info {u32 refcnt;/* Count of attached dev/pasid per domain/IOMMU */ }; +struct amd_iommu_viommu { +struct iommufd_viommu core; +struct protection_domain *parent;/* nest parent domain for this viommu */ +}; +/* * This structure contains generic data for IOMMU protection domains * independent of their use.
drivers/iommu/amd/iommu.c +3 -1
@@ -3077,7 +3077,9 @@ const struct iommu_ops amd_iommu_ops = { .iotlb_sync = amd_iommu_iotlb_sync, .free = amd_iommu_domain_free, .enforce_cache_coherency = amd_iommu_enforce_cache_coherency, - } + }, + .get_viommu_size = amd_iommufd_get_viommu_size, + .viommu_init = amd_iommufd_viommu_init, };#ifdef CONFIG_IRQ_REMAP
drivers/iommu/amd/iommufd.c +17 -0
@@ -29,3 +29,20 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *length, u32 *type)return hwinfo; } + +size_t amd_iommufd_get_viommu_size (struct device *dev,enum iommu_viommu_type viommu_type) +{ + return VIOMMU_STRUCT_SIZE(struct amd_iommu_viommu, core); +} + +int amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent, +const struct iommu_user_data *user_data) +{ + struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + +/* +*/ + aviommu->parent =to_pdomain (parent); + +return 0 ; +}
drivers/iommu/amd/iommufd.h +5 -0
@@ -8,8 +8,13 @@#if IS_ENABLED (CONFIG_AMD_IOMMU_IOMMUFD )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); +int amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent, +const struct iommu_user_data *user_data);#else #define amd_iommufd_hw_info NULL +#define amd_iommufd_viommu_init NULL +#define amd_iommufd_get_viommu_size NULL#endif /* CONFIG_AMD_IOMMU_IOMMUFD */ #endif /* AMD_IOMMUFD_H */
Signature · 1 line
2.34.1
Re: [PATCH v4 11/16] iommu/amd: Introduce struct amd_iommu_viommu
drivers/iommu/amd/iommufd.c void *amd_iommufd_hw_info(struct device *dev, u32 *length, u32 *type) · line 45
Quoted 8 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommufd.c +8
-0+int amd_iommufd_viommu_init (struct iommufd_viommu *viommu,struct iommu_domain *parent, +const struct iommu_user_data *user_data) +{ + struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core); + +/* +*/ + aviommu->parent =to_pdomain (parent);
Drop the /* */ - otherwise looks OK
Jason
Re: [PATCH v4 11/16] iommu/amd: Introduce struct amd_iommu_viommu
In reply to Suravee SuthikulpanitQuoted 8 lines · Suravee Suthikulpanit
Which stores reference to nested parent domain assigned during the call to struct
iommu_ops.viommu_init(). Information in the nest parent is needed when setting up the nested translation.Note that the viommu initialization will be introduced in subsequent commit.
Signed-off-by Suravee Suthikulpanit <[email protected]>
Suravee Suthikulpanit
reviewerPATCH v4 12/16 iommu/amd: Add support for nested domain allocation
In reply to Suravee SuthikulpanitThe nested domain is allocated with IOMMU_DOMAIN_NESTED type to store stage-1 translation (i.e. GVA->GPA). This includes the GCR3 root pointer table along with guest page tables. The struct iommu_hwpt_amd_guest contains this information, and is passed from user-space as a parameter of the struct iommu_ops.domain_alloc_nested().
--- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 4 + drivers/iommu/amd/amd_iommu_types.h | 14 ++++ drivers/iommu/amd/nested.c | 111 ++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/amd/nested.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 io_pgtable.o io_pgtable_v2.o ppr.o pasid.o -obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o +obj-$(CONFIG_AMD_IOMMU_IOMMUFD) += iommufd.o nested.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/amd/amd_iommu.h +4 -0
@@ -202,4 +202,8 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry new->data128[1] = 0; } +/* NESTED */ +struct iommu_domain * +amd_iommu_alloc_domain_nested (struct iommufd_viommu *viommu,u32 flags, +const struct iommu_user_data *user_data);#endif /* AMD_IOMMU_H */
drivers/iommu/amd/amd_iommu_types.h +14 -0
@@ -21,6 +21,8 @@#include <linux/irqreturn.h> #include <linux/io-pgtable.h> +#include <uapi/linux/iommufd.h> +/* * Maximum number of IOMMUs supported */
@@ -417,6 +419,8 @@#define DTE_FLAG_V BIT_ULL(0)#define DTE_FLAG_TV BIT_ULL(1)#define DTE_FLAG_HAD (3ULL << 7) +#define DTE_MODE_MASK GENMASK_ULL(11, 9) +#define DTE_HOST_TRP GENMASK_ULL(51, 12)#define DTE_FLAG_GIOV BIT_ULL(54)#define DTE_FLAG_GV BIT_ULL(55)#define DTE_GLX GENMASK_ULL(57, 56)
@@ -592,6 +596,16 @@ struct amd_iommu_viommu {struct protection_domain *parent;/* nest parent domain for this viommu */ }; +/* +* Nested domain is specifically used for nested translation +*/ +struct nested_domain { +struct iommu_domain domain;/* generic domain handle used by iommu core code */ +u16 gdom_id;/* domain ID from gDTE */ +struct iommu_hwpt_amd_guest gdte;/* Guest vIOMMU DTE */ +struct amd_iommu_viommu *viommu;/* AMD hw-viommu this nested domain belong to */ +}; +/* * This structure contains generic data for IOMMU protection domains * independent of their use.
drivers/iommu/amd/nested.c new file +111 -0
111 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v4 12/16] iommu/amd: Add support for nested domain allocation
Quoted 6 lines · Suravee Suthikulpanit
The nested domain is allocated with
IOMMU_DOMAIN_NESTEDtype to store stage-1 translation (i.e.GVA->GPA). This includes the GCR3 root pointer table along with guest page tables. The structiommu_hwpt_amd_guestcontains this information, and is passed from user-space as a parameter of the structiommu_ops.domain_alloc_nested().
Quoted 8 lines
Reviewed-by Nicolin Chen <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>--- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu.h | 4 + drivers/iommu/amd/amd_iommu_types.h | 14 ++++ drivers/iommu/amd/nested.c | 111 ++++++++++++++++++++++++++++
Might be nicer to put this in amd/iommufd.c, but up to you
Jason
Suravee Suthikulpanit
reviewerPATCH v4 13/16 iommu/amd: Track host Domain ID mapping for each guest Domain ID
In reply to Suravee SuthikulpanitEach nested domain is assigned guest domain ID (gDomID), which guest OS programs into guest Device Table Entry (gDTE). For each gDomID, the driver assigns a corresponding host domain ID (hDomID), which will be programmed into the host Device Table Entry (hDTE).
The gDTE to hDTE 1:1 mapping is stored in the nest parent domain using an xarray (struct protection_domain.gdomid_array). When invalidate the nest parent domain, the INVALIDATE_IOMMU_PAGES must be issued for each hDomID in the gdomid_array.
--- drivers/iommu/amd/amd_iommu_types.h | 3 ++ drivers/iommu/amd/iommu.c | 30 ++++++++++++++++ drivers/iommu/amd/nested.c | 53 ++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 1 deletion(-)
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -602,6 +602,7 @@ struct amd_iommu_viommu {struct nested_domain {struct iommu_domain domain;/* generic domain handle used by iommu core code */ u16 gdom_id;/* domain ID from gDTE */ +u16 hdom_id;/* domain ID written to the device table */ struct iommu_hwpt_amd_guest gdte;/* Guest vIOMMU DTE */ struct amd_iommu_viommu *viommu;/* AMD hw-viommu this nested domain belong to */ };
@@ -623,6 +624,8 @@ struct protection_domain {struct mmu_notifier mn;/* mmu notifier for the SVA domain */ struct list_head dev_data_list;/* List of pdom_dev_data */ + +struct xarray gdomid_array;/* gDomID mapping to this nest parent domain */ };/*
drivers/iommu/amd/iommu.c +30 -0
@@ -1493,6 +1493,23 @@ static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id)amd_iommu_completion_wait (iommu); } +static int iommu_flush_hdom_ids (struct amd_iommu *iommu, +u64 address,size_t size, +struct protection_domain *parent) +{ +int ret =0 ; +unsigned long i; +struct iommu_cmd cmd; +struct nested_domain *ndom; + +xa_for_each (&parent->gdomid_array, i, ndom) { +build_inv_iommu_pages (&cmd, address, size, ndom->hdom_id, +IOMMU_NO_PASID , false); + ret |=iommu_queue_command (iommu, &cmd); + } +return ret; +} +static void amd_iommu_flush_all (struct amd_iommu *iommu) {struct iommu_cmd cmd;
@@ -1639,6 +1656,18 @@ static int domain_flush_pages_v1(struct protection_domain *pdom,* We need a TLB flush */ ret |=iommu_queue_command (pdom_iommu_info->iommu, &cmd); + +/* +* A domain w/ v1 table can be a nest parent, which can have +* multiple nested domains. Each nested domain has 1:1 mapping +* between gDomID and hDomID, which is stored in the gdomid_array. +* Therefore, we also need to flush every hDomID associated to this +* nest parent domain. +* +* See drivers/iommu/amd/nested.c: amd_iommu_alloc_domain_nested() +*/ + ret |=iommu_flush_hdom_ids (pdom_iommu_info->iommu, address, + size, pdom); }return ret;
@@ -2470,6 +2499,7 @@ static void protection_domain_init(struct protection_domain *domain)INIT_LIST_HEAD (&domain->dev_list);INIT_LIST_HEAD (&domain->dev_data_list);xa_init (&domain->iommu_array); +xa_init (&domain->gdomid_array); }struct protection_domain *protection_domain_alloc (void )
drivers/iommu/amd/nested.c +52 -1
72 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v4 13/16] iommu/amd: Track host Domain ID mapping for each guest Domain ID
Quoted 9 lines · Suravee Suthikulpanit
Each nested domain is assigned guest domain ID (gDomID), which guest OS programs into guest Device Table Entry (gDTE). For each gDomID, the driver assigns a corresponding host domain ID (hDomID), which will be programmed into the host Device Table Entry (hDTE).
The gDTE to hDTE 1:1 mapping is stored in the nest parent domain using an xarray (struct protection_domain.gdomid_array). When invalidate the nest parent domain, the
INVALIDATE_IOMMU_PAGESmust be issued for each hDomID in thegdomid_array.
I think this should be stored in the viommu..
It is a small unrealistic detail but very pedantically the API allows creating two VIOMMU's from the same NEST PARENT domain and if someone did this then each of the VIOMMU should have its own private gDomID number space and own separated xarray.
Allowing two VIOMMUs to share the same hDomID could be problematic because we don't know the PASID layout is consistent.
drivers/iommu/amd/iommu.c static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id) · line 1505
Quoted 10 lines
drivers/iommu/amd/iommu.c +10
-0+static int iommu_flush_hdom_ids (struct amd_iommu *iommu, +u64 address,size_t size, +struct protection_domain *parent) +{ +int ret =0 ; +unsigned long i; +struct iommu_cmd cmd; +struct nested_domain *ndom; + +xa_for_each (&parent->gdomid_array, i, ndom) {
This doesn't seem right.. There could be many nested_domains sharing the same gDomID..
I expect this xarray to have a struct like
struct gdomid {
refcount_t users;
u32 hdomid;
};
And each nested_domain will go into the viommu and either allocate a new gdomid or ++users for the existing one. Inverse when destroying a nested_domain.
Quoted 11 lines
drivers/iommu/amd/nested.c +7
-0@@ -92,6 +92,49 @@ amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags, ndom->domain.type =IOMMU_DOMAIN_NESTED ; ndom->viommu = aviommu; +/* +* Normally, when a guest has multiple pass-through devices, +* the IOMMU driver setup DTEs with the same stage-2 table and +* use the same host domain ID (hDomId). In case of nested translation, +* if the guest setup different stage-1 tables with same PASID, +* IOMMU would use the same TLB tag. This will results in TLB +* aliasing issue.
drivers/iommu/amd/nested.c amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags, · line 111
Quoted 10 lines
drivers/iommu/amd/nested.c +10
-0+* +* The guest is assigning gDomIDs based on its own algorithm for managing +* cache tags of (DomID, PASID). Within a single viommu, the nest parent domain +* (w/ S2 table) is used by all DTEs. But we need to consistently map the gDomID +* to a single hDomID. This is done using an xarray in the nest parent domain to +* keep track of the gDomID mapping. When the S2 is changed, the INVALIDATE_IOMMU_PAGES +* command must be issued for each hDomID in the xarray. +* +* Since there is no invalidation support and no viommu yet, just always use a +* unique hDomID for now.
It is not "for now" anymore, this is the correct algorithm..
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v4 13/16] iommu/amd: Track host Domain ID mapping for each guest Domain ID
Jason
Quoted 11 lines · Jason Gunthorpe
Quoted 9 lines · Suravee Suthikulpanit
Each nested domain is assigned guest domain ID (gDomID), which guest OS programs into guest Device Table Entry (gDTE). For each gDomID, the driver assigns a corresponding host domain ID (hDomID), which will be programmed into the host Device Table Entry (hDTE).
The gDTE to hDTE 1:1 mapping is stored in the nest parent domain using an xarray (struct protection_domain.gdomid_array). When invalidate the nest parent domain, the
INVALIDATE_IOMMU_PAGESmust be issued for each hDomID in thegdomid_array.
Quoted 6 lines
I think this should be stored in the viommu..
It is a small unrealistic detail but very pedantically the API allows creating two VIOMMU's from the same NEST PARENT domain and if someone did this then each of the VIOMMU should have its own private gDomID number space and own separated xarray.
Actually, to support nested translation w/ HW-based vIOMMU support in the guest w/ two VFIO devices on two different physical IOMMUs, it would require setting up two iommufd_viommu structures (one for each IOMMU) and share the same parent domain (single GPA-SPA mapping). Also, AMD HW-vIOMMU use a single domain ID (gDomID-to-hDomID) mapping table per guest-ID. Since the table is indexed using gDomID, it requires single gDomID space per guest.
In this case, it makes more sense to store the gDomID-to-hDomID mapping in the parent domain since:
1. There is one gDomID-space per guest and there is one parent
domain per guest.
2. When host issues invalidation for a parent domain, IOMMU driver
needs to issue an invalidate command for each hDomId used for the same parent domain (on each IOMMU). We can't do this if we store xarray in the viommu. Otherwise, we would need to store a list of vIOMMUs per parent domain.
Quoted 2 lines
Allowing two VIOMMUs to share the same hDomID could be problematic because we don't know the PASID layout is consistent.
Not sure why PASID layout matters here?
Quoted 16 lines
drivers/iommu/amd/iommu.c static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id) · line 1505
Quoted 10 lines
drivers/iommu/amd/iommu.c +10
-0+static int iommu_flush_hdom_ids (struct amd_iommu *iommu, +u64 address,size_t size, +struct protection_domain *parent) +{ +int ret =0 ; +unsigned long i; +struct iommu_cmd cmd; +struct nested_domain *ndom; + +xa_for_each (&parent->gdomid_array, i, ndom) {This doesn't seem right.. There could be many
nested_domainssharing the same gDomID..I expect this xarray to have a struct like
Quoted 8 lines
struct gdomid { refcount_t users; u32 hdomid; };And each
nested_domainwill go into the viommu and either allocate a new gdomid or ++users for the existing one. Inverse when destroying anested_domain.
Got it. I have new code for this and will send out in v5 soon.
Thanks,
Suravee
Jason Gunthorpe
maintainerRe: [PATCH v4 13/16] iommu/amd: Track host Domain ID mapping for each guest Domain ID
Quoted 21 lines · Suravee
Jason
Quoted 11 lines · Jason Gunthorpe
Quoted 9 lines · Suravee Suthikulpanit
Each nested domain is assigned guest domain ID (gDomID), which guest OS programs into guest Device Table Entry (gDTE). For each gDomID, the driver assigns a corresponding host domain ID (hDomID), which will be programmed into the host Device Table Entry (hDTE).
The gDTE to hDTE 1:1 mapping is stored in the nest parent domain using an xarray (struct protection_domain.gdomid_array). When invalidate the nest parent domain, the
INVALIDATE_IOMMU_PAGESmust be issued for each hDomID in thegdomid_array.Quoted 6 lines
I think this should be stored in the viommu..
It is a small unrealistic detail but very pedantically the API allows creating two VIOMMU's from the same NEST PARENT domain and if someone did this then each of the VIOMMU should have its own private gDomID number space and own separated xarray.
Quoted 6 lines
Actually, to support nested translation w/ HW-based vIOMMU support in the guest w/ two VFIO devices on two different physical IOMMUs, it would require setting up two
iommufd_viommustructures (one for each IOMMU) and share the same parent domain (single GPA-SPA mapping). Also, AMD HW-vIOMMU use a single domain ID (gDomID-to-hDomID) mapping table per guest-ID. Since the table is indexed using gDomID, it requires single gDomID space per guest.
Yes, this is why the guest ID needs to come from the viommu object, not the parent domain.
Quoted 5 lines
In this case, it makes more sense to store the gDomID-to-hDomID mapping in the parent domain since:
1. There is one gDomID-space per guest and there is one parent domain per
guest.
No. There is one gDomID-space *per viommu*. The driver has no concept of what a guest is, that isn't part of our API.
For AMD hardware it is the viommu object that must hold the gDomID, and all the other related HW datastructures for a "guest".
One viommu object per what the AMD spec calls a "guest".
Quoted 4 lines
2. When host issues invalidation for a parent domain, IOMMU driver needs
to issue an invalidate command for each hDomId used for the same parent domain (on each IOMMU). We can't do this if we store xarray in the viommu. Otherwise, we would need to store a list of vIOMMUs per parent domain.
Correct, however this is just showing that the driver invalidation logic is incorrect. See how Nicolin is working to fix this same problem in the ARM driver. The driver cannot assume a domain has only a single domain id in a viommu world.
For now you should still store the xarray in the viommu and then prevent the parent domain from being used with more than one viommu somehow so it doesn't get into trouble with the limited invalidation logic.
Someday maybe we can fix the invalidation logic and allow domain sharing. But for now the code should be structured along its own limitations..
Quoted 4 lines
Quoted 2 lines
Allowing two VIOMMUs to share the same hDomID could be problematic because we don't know the PASID layout is consistent.
Not sure why PASID layout matters here?
I'm saying you cannot share hDomID across two VIOMMU objects, it is not allowed.
Jason
Suravee Suthikulpanit
reviewerPATCH v4 14/16 iommu/amd: Refactor persistent DTE bits programming into amd_iommu_make_clear_dte()
In reply to Suravee SuthikulpanitTo help avoid duplicate logic when programing DTE for nested translation.
Note that this commit changes behavior of when the IOMMU driver is switching domain during attach and the blocking domain, where DTE bit fields for interrupt pass-through (i.e. Lint0, Lint1, NMI, INIT, ExtInt) and System management message could be affected. These DTE bits are specified in the IVRS table for specific devices, and should be persistent.
--- drivers/iommu/amd/amd_iommu.h | 13 +++++++++++++ drivers/iommu/amd/iommu.c | 11 ----------- 2 files changed, 13 insertions(+), 11 deletions(-)
drivers/iommu/amd/amd_iommu.h +13 -0
@@ -197,9 +197,22 @@ void amd_iommu_update_dte256(struct amd_iommu *iommu,static inline void amd_iommu_make_clear_dte (struct iommu_dev_data *dev_data,struct dev_table_entry *new) { +struct dev_table_entry *initial_dte; +struct amd_iommu *iommu =get_amd_iommu_from_dev (dev_data->dev); +/* All existing DTE must have V bit set */ new->data128[0 ] =DTE_FLAG_V ; new->data128[1 ] =0 ; + +/* +* Restore cached persistent DTE bits, which can be set by information +* in IVRS table. See set_dev_entry_from_acpi(). +*/ + initial_dte =amd_iommu_get_ivhd_dte_flags (iommu->pci_seg->id, dev_data->devid); +if (initial_dte) { + new->data128[0 ] |= initial_dte->data128[0 ]; + new->data128[1 ] |= initial_dte->data128[1 ]; + } }/* NESTED */
drivers/iommu/amd/iommu.c +0 -11
@@ -2078,7 +2078,6 @@ static void set_dte_entry(struct amd_iommu *iommu, {u16 domid;u32 old_domid; -struct dev_table_entry *initial_dte;struct dev_table_entry new = {};struct protection_domain *domain = dev_data->domain;struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
@@ -2119,16 +2118,6 @@ static void set_dte_entry(struct amd_iommu *iommu, old_domid =READ_ONCE (dte->data[1 ]) &DTE_DOMID_MASK ; new.data[1 ] |= domid; -/* -* Restore cached persistent DTE bits, which can be set by information -* in IVRS table. See set_dev_entry_from_acpi(). -*/ - initial_dte =amd_iommu_get_ivhd_dte_flags (iommu->pci_seg->id, dev_data->devid); -if (initial_dte) { - new.data128[0 ] |= initial_dte->data128[0 ]; - new.data128[1 ] |= initial_dte->data128[1 ]; - } -set_dte_gcr3_table (iommu, dev_data, &new);amd_iommu_update_dte256 (iommu, dev_data, &new);
Signature · 1 line
2.34.1
Re: [PATCH v4 14/16] iommu/amd: Refactor persistent DTE bits programming into amd_iommu_make_clear_dte()
Quoted 11 lines · Suravee Suthikulpanit
To help avoid duplicate logic when programing DTE for nested translation.
Note that this commit changes behavior of when the IOMMU driver is switching domain during attach and the blocking domain, where DTE bit fields for interrupt pass-through (i.e. Lint0, Lint1, NMI, INIT, ExtInt) and System management message could be affected. These DTE bits are specified in the IVRS table for specific devices, and should be persistent.
Suggested-by Jason Gunthorpe <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Suravee Suthikulpanit <[email protected]>
Suravee Suthikulpanit
reviewerPATCH v4 15/16 iommu/amd: Refactor logic to program the host page table in DTE
In reply to Suravee SuthikulpanitIntroduce the set_dte_v1() helper function to configure IOMMU host (v1) page table into DTE.
There is no functional change.
--- drivers/iommu/amd/amd_iommu.h | 3 ++ drivers/iommu/amd/iommu.c | 57 ++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 25 deletions(-)
drivers/iommu/amd/amd_iommu.h +3 -0
@@ -190,6 +190,9 @@ struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid);int amd_iommu_completion_wait (struct amd_iommu *iommu);/* DTE */ +void amd_iommu_set_dte_v1 (struct iommu_dev_data *dev_data, +struct protection_domain *domain, +struct dev_table_entry *new);int amd_iommu_device_flush_dte (struct iommu_dev_data *dev_data); void amd_iommu_update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data,
drivers/iommu/amd/iommu.c +32 -25
91 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v4 15/16] iommu/amd: Refactor logic to program the host page table in DTE
Quoted 4 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommu.c +0
-0@@ -2088,37 +2104,28 @@ static void set_dte_entry(struct amd_iommu *iommu,else domid = domain->id;
drivers/iommu/amd/iommu.c static void set_dte_entry(struct amd_iommu *iommu, · line 2116
Quoted 10 lines
drivers/iommu/amd/iommu.c +2
-0/* * When SNP is enabled, we can only support TV=1 with non-zero domain ID. * This is prevented by the SNP-enable and IOMMU_DOMAIN_IDENTITY check in * do_iommu_domain_alloc(). */ WARN_ON (amd_iommu_snp_en && (domid ==0 )); +amd_iommu_make_clear_dte (dev_data, &new); + old_domid =READ_ONCE (dte->data[1 ]) &DTE_DOMID_MASK ;
This old_domid stuff doesn't make any sense. I think the commit that added it is bonkers: 36b7200f67df ("iommu/amd: Flush old domains in kdump kernel")
The problem is that the dom ids that are present in the re-used DTE table have to be reserved from the domain id alloctor at boot.
If the kdump kernel tries to create new DTEs it MUST NOT re-use any IDs that are actively being using in DTEs already or you get data corruption. Randomly flushing IDs is just getting lucky..
Not for this series, but something to think about.
drivers/iommu/amd/iommu.c static void set_dte_entry(struct amd_iommu *iommu, · line 2128
Quoted 11 lines
drivers/iommu/amd/iommu.c +9
-0+if (gcr3_info && gcr3_info->gcr3_tbl) +set_dte_gcr3_table (dev_data, &new); +else if (domain->iop.mode !=PAGE_MODE_NONE ) +amd_iommu_set_dte_v1 (dev_data, domain, &new); +/* Note: The IR, IW, TV, DOMID are needed for both v1 and gcr3 table */ + new.data[0 ] |=DTE_FLAG_IR |DTE_FLAG_IW |DTE_FLAG_TV ; + new.data[1 ] |=FIELD_PREP (DTE_DOMID_MASK , domid); +if (dev_data->ats_enabled) + new.data[1 ] |=DTE_FLAG_IOTLB ;
These three should be merged into the two functions so they stand alone. These sets have to be made in the next patch, doesn't make sense to open code them in callers.
Like this, it is simple and readable. It directly answers the question 'what bits are set when the driver creates this kind of DTE'
static void set_dte_gcr3_table(struct amd_iommu *iommu,
struct iommu_dev_data *dev_data, struct dev_table_entry *target)
{
struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
u64 gcr3 = iommu_virt_to_phys(gcr3_info->gcr3_tbl);
target->data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV |
DTE_FLAG_GV | FIELD_PREP(DTE_GLX, gcr3_info->glx) |
FIELD_PREP(DTE_GCR3_14_12, gcr3 >> 12) |
(dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0) |
(pdom_is_v2_pgtbl_mode(dev_data->domain) ?
target->data[0] |= DTE_FLAG_GIOV :
0);
target->data[1] |= FIELD_PREP(DTE_GCR3_30_15, gcr3 >> 15) |
FIELD_PREP(DTE_GCR3_51_31, gcr3 >> 31) |
FIELD_PREP(DTE_DOMID_MASK, dev_data->gcr3_info.domid);
/* Guest page table can only support 4 and 5 levels */
if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL)
target->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_5_LEVEL);
else
target->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_4_LEVEL);
}void amd_iommu_set_dte_v1(struct iommu_dev_data *dev_data,
struct protection_domain *domain, struct dev_table_entry *new)
{
u64 htrp = iommu_virt_to_phys(domain->iop.root);
new->data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV |
FIELD_PREP(DTE_MODE_MASK, domain->iop.mode) |
FIELD_PREP(DTE_HOST_TRP, htrp >> 12) |
(dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0) |
(domain->dirty_tracking ? DTE_FLAG_HAD : 0);
new.data[1] |= FIELD_PREP(DTE_DOMID_MASK, domain->id);
}(It is nice to sort the fields by the spec order, I didn't do that)
Looks like an identity one is needed too:
void set_dte_identity(struct dev_table_entry *new)
{
new->data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV |
(dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0);
}Then the whole function is pretty much just:
amd_iommu_make_clear_dte(dev_data, &new); if (gcr3_info && gcr3_info->gcr3_tbl) set_dte_gcr3_table(dev_data, &new); else if (domain->domain.type == IOMMU_DOMAIN_IDENTITY) set_dte_identity(&new) else if (domain->domain.type == IOMMU_DOMAIN_PAGING && io.mode == V1) amd_iommu_set_dte_v1(dev_data, domain, &new); else WARN_ON(true);
?
(though how does IDENTITY on a device with a PASID installed work?)
Jason
Vasant Hegde
maintainerRe: [PATCH v4 15/16] iommu/amd: Refactor logic to program the host page table in DTE
Jason,
Quoted 19 lines · Jason Gunthorpe
Quoted 4 lines · Suravee Suthikulpanit
drivers/iommu/amd/iommu.c +0
-0@@ -2088,37 +2104,28 @@ static void set_dte_entry(struct amd_iommu *iommu,else domid = domain->id;drivers/iommu/amd/iommu.c static void set_dte_entry(struct amd_iommu *iommu, · line 2116
Quoted 10 lines
drivers/iommu/amd/iommu.c +2
-0/* * When SNP is enabled, we can only support TV=1 with non-zero domain ID. * This is prevented by the SNP-enable and IOMMU_DOMAIN_IDENTITY check in * do_iommu_domain_alloc(). */ WARN_ON (amd_iommu_snp_en && (domid ==0 )); +amd_iommu_make_clear_dte (dev_data, &new); + old_domid =READ_ONCE (dte->data[1 ]) &DTE_DOMID_MASK ;This
old_domidstuff doesn't make any sense. I think the commit that added it is bonkers:36b7200f67df("iommu/amd: Flush old domains in kdump kernel")
Quoted 5 lines
The problem is that the dom ids that are present in the re-used DTE table have to be reserved from the domain id alloctor at boot.
Quoted 1 line
If the kdump kernel tries to create new DTEs it MUST NOT re-use any
IDs that are actively being using in DTEs already or you get data corruption. Randomly flushing IDs is just getting lucky..
Good catch. Thanks!
Looks like commit 38e5f33ee359 ("iommu/amd: Reuse device table for kdump") broke domain ID reservation in kdump path :-( We will fix it.
Quoted 84 lines
Not for this series, but something to think about.
drivers/iommu/amd/iommu.c static void set_dte_entry(struct amd_iommu *iommu, · line 2128
Quoted 11 lines
drivers/iommu/amd/iommu.c +9
-0+if (gcr3_info && gcr3_info->gcr3_tbl) +set_dte_gcr3_table (dev_data, &new); +else if (domain->iop.mode !=PAGE_MODE_NONE ) +amd_iommu_set_dte_v1 (dev_data, domain, &new); +/* Note: The IR, IW, TV, DOMID are needed for both v1 and gcr3 table */ + new.data[0 ] |=DTE_FLAG_IR |DTE_FLAG_IW |DTE_FLAG_TV ; + new.data[1 ] |=FIELD_PREP (DTE_DOMID_MASK , domid); +if (dev_data->ats_enabled) + new.data[1 ] |=DTE_FLAG_IOTLB ;These three should be merged into the two functions so they stand alone. These sets have to be made in the next patch, doesn't make sense to open code them in callers.
Like this, it is simple and readable. It directly answers the question 'what bits are set when the driver creates this kind of DTE'
static void set_dte_gcr3_table(struct
amd_iommu*iommu,
structiommu_dev_data*dev_data, structdev_table_entry*target){ struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; u64 gcr3 = iommu_virt_to_phys(gcr3_info->gcr3_tbl); target->data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV | DTE_FLAG_GV | FIELD_PREP(DTE_GLX, gcr3_info->glx) | FIELD_PREP(DTE_GCR3_14_12, gcr3 >> 12) | (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0) | (pdom_is_v2_pgtbl_mode(dev_data->domain) ? target->data[0] |= DTE_FLAG_GIOV : 0); target->data[1] |= FIELD_PREP(DTE_GCR3_30_15, gcr3 >> 15) | FIELD_PREP(DTE_GCR3_51_31, gcr3 >> 31) | FIELD_PREP(DTE_DOMID_MASK, dev_data->gcr3_info.domid); /* Guest page table can only support 4 and 5 levels */ if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL) target->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_5_LEVEL); else target->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_4_LEVEL); }void amd_iommu_set_dte_v1(struct
iommu_dev_data*dev_data,
structprotection_domain*domain, structdev_table_entry*new){ u64 htrp = iommu_virt_to_phys(domain->iop.root); new->data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV | FIELD_PREP(DTE_MODE_MASK, domain->iop.mode) | FIELD_PREP(DTE_HOST_TRP, htrp >> 12) | (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0) | (domain->dirty_tracking ? DTE_FLAG_HAD : 0); new.data[1] |= FIELD_PREP(DTE_DOMID_MASK, domain->id); }(It is nice to sort the fields by the spec order, I didn't do that)
Looks like an identity one is needed too:
void
set_dte_identity(struct dev_table_entry *new){ new->data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV | (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0); }Then the whole function is pretty much just:
amd_iommu_make_clear_dte(dev_data, &new); if (gcr3_info && gcr3_info->gcr3_tbl) set_dte_gcr3_table(dev_data, &new); else if (domain->domain.type == IOMMU_DOMAIN_IDENTITY) set_dte_identity(&new) else if (domain->domain.type == IOMMU_DOMAIN_PAGING && io.mode == V1) amd_iommu_set_dte_v1(dev_data, domain, &new); else WARN_ON(true);
Quoted 3 lines
?
(though how does IDENTITY on a device with a PASID installed work?)
Probably set_dte_identity() should call set_dte_gcr3_table () and update relevant fields.
-Vasant
Jason Gunthorpe
maintainerRe: [PATCH v4 15/16] iommu/amd: Refactor logic to program the host page table in DTE
Quoted 21 lines · Vasant Hegde
Quoted 21 lines · Suravee Suthikulpanit
Quoted 4 lines
drivers/iommu/amd/iommu.c +0
-0@@ -2088,37 +2104,28 @@ static void set_dte_entry(struct amd_iommu *iommu,else domid = domain->id;drivers/iommu/amd/iommu.c static void set_dte_entry(struct amd_iommu *iommu, · line 2116
Quoted 10 lines
drivers/iommu/amd/iommu.c +2
-0/* * When SNP is enabled, we can only support TV=1 with non-zero domain ID. * This is prevented by the SNP-enable and IOMMU_DOMAIN_IDENTITY check in * do_iommu_domain_alloc(). */ WARN_ON (amd_iommu_snp_en && (domid ==0 )); +amd_iommu_make_clear_dte (dev_data, &new); + old_domid =READ_ONCE (dte->data[1 ]) &DTE_DOMID_MASK ;This
old_domidstuff doesn't make any sense. I think the commit that added it is bonkers:36b7200f67df("iommu/amd: Flush old domains in kdump kernel")The problem is that the dom ids that are present in the re-used DTE table have to be reserved from the domain id alloctor at boot.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
If the kdump kernel tries to create new DTEs it MUST NOT re-use any
IDs that are actively being using in DTEs already or you get data corruption. Randomly flushing IDs is just getting lucky..
Good catch. Thanks!
Looks like commit
38e5f33ee359("iommu/amd: Reuse device table for kdump") broke domain ID reservation in kdump path :-( We will fix it.
I suggest just reading the whole DTE and permanently reserving any DomIDs from the ida. Just leak them forever, that is good enough for a kdump kernel. Then get rid of this stuff above.
Quoted 4 lines
Quoted 1 line
(though how does IDENTITY on a device with a PASID installed work?)
Probably
set_dte_identity()should callset_dte_gcr3_table() and update relevant fields.
Yeah, maybe so, though it might be best if the caller can detect it and have it call both functions instead of trying to bury it.
There is a fair number of variations here as you have blocked/identity, RID/PASID attach and PASID detach to all consider.
It would be nice if it worked properly, identity on RID while PASID is in use has a strong real use case.
Jason
Suthikulpanit, Suravee
reviewerRe: [PATCH v4 15/16] iommu/amd: Refactor logic to program the host page table in DTE
Quoted 8 lines · Jason Gunthorpe
Quoted 7 lines · Vasant Hegde
Quoted 3 lines · Jason Gunthorpe
On Tue, Oct 21, 2025 at 01:43:23AM +0000, Suravee Suthikulpanit wrote:
(though how does IDENTITY on a device with a PASID installed work?)
Probably
set_dte_identity()should callset_dte_gcr3_table() and update relevant fields.
Actually, PASID would not work with IDENTITY since it has no page table (i.e. iommu=pt means DTE[Mode]=0 and does not have host table pointer). PASID only work with GCR3 table.
Therefore, it does not make sense for set_dte_identity() to call set_dte_gcr3_table(). Each one should be stand alone.
Thanks,
Suravee
Quoted 10 lines
Yeah, maybe so, though it might be best if the caller can detect it and have it call both functions instead of trying to bury it.
There is a fair number of variations here as you have blocked/identity, RID/PASID attach and PASID detach to all consider.
It would be nice if it worked properly, identity on RID while PASID is in use has a strong real use case.
Jason
Jason Gunthorpe
maintainerRe: [PATCH v4 15/16] iommu/amd: Refactor logic to program the host page table in DTE
Quoted 12 lines · Suravee
Quoted 8 lines · Jason Gunthorpe
Quoted 7 lines · Vasant Hegde
Quoted 3 lines · Jason Gunthorpe
On Tue, Oct 21, 2025 at 01:43:23AM +0000, Suravee Suthikulpanit wrote:
(though how does IDENTITY on a device with a PASID installed work?)
Probably
set_dte_identity()should callset_dte_gcr3_table() and update relevant fields.
Quoted 6 lines
Actually, PASID would not work with IDENTITY since it has no page table (i.e. iommu=pt means DTE[Mode]=0 and does not have host table pointer). PASID only work with GCR3 table.
Therefore, it does not make sense for
set_dte_identity()to callset_dte_gcr3_table(). Each one should be stand alone.
OK, so the HW cannot do this?
On SMMUv3 there are bits in the "DTE" (S1DSS) that control how no-PASID, ie PASID 0 transactions are handled which allow it to be set to indentity. Intel can do the same.
IMHO this is a HW gap that AMD probably should fix.
In the mean time the driver should be blocking this unsupported combination, I don't remember seeing that code? It will make it more difficult to use SVA on AMD platforms, but not much that can be done about that :\
Jason
Suravee Suthikulpanit
reviewerPATCH v4 16/16 iommu/amd: Add support for nested domain attach/detach
In reply to Suravee SuthikulpanitIntroduce set_dte_nested() to program guest translation settings in the host DTE when attaches the nested domain to a device.
--- drivers/iommu/amd/amd_iommu_types.h | 1 + drivers/iommu/amd/nested.c | 71 +++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +1 -0
@@ -421,6 +421,7 @@#define DTE_FLAG_HAD (3ULL << 7)#define DTE_MODE_MASK GENMASK_ULL(11, 9)#define DTE_HOST_TRP GENMASK_ULL(51, 12) +#define DTE_FLAG_PPR BIT_ULL(52)#define DTE_FLAG_GIOV BIT_ULL(54)#define DTE_FLAG_GV BIT_ULL(55)#define DTE_GLX GENMASK_ULL(57, 56)
drivers/iommu/amd/nested.c +71 -0
83 lines not on this page · raw message
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v4 16/16] iommu/amd: Add support for nested domain attach/detach
Quoted 5 lines · Suravee Suthikulpanit
drivers/iommu/amd/nested.c +5
-0+static int nested_attach_device (struct iommu_domain *dom,struct device *dev) +{ +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 ;
drivers/iommu/amd/nested.c amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags, · line 207
Quoted 10 lines
drivers/iommu/amd/nested.c +10
-0+ +if (WARN_ON (dom->type !=IOMMU_DOMAIN_NESTED )) +return -EINVAL ; + +mutex_lock (&dev_data->mutex); + +/* Update device table entry */ +set_dte_nested (iommu, dom, dev_data); +amd_iommu_device_flush_dte (dev_data); +amd_iommu_completion_wait (iommu);
The structure is still upside down and it should probably be fixed rather than duplicate this code (and missing the clone_aliases!)
dev_update_dte() should take in the struct dev_table_entry *:
static void dev_update_dte(struct iommu_dev_data *dev_data,
struct dev_table_entry *dte)
{
struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
amd_iommu_update_dte256(iommu, dev_data, dte);
clone_aliases(iommu, dev_data->dev);
device_flush_dte(dev_data);
iommu_completion_wait(iommu);
}And this function should be exported and called instead of open coding it above.
Rework set_dte_entry() to return new instead of calling update_dte256().
Jason