All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features
@ 2025-06-04  6:13 Ankit Soni
  2025-06-04  6:13 ` [PATCH v4 1/2] iommu/amd: Add HATDis feature support Ankit Soni
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ankit Soni @ 2025-06-04  6:13 UTC (permalink / raw)
  To: iommu
  Cc: vasant.hegde, joao.m.martins, suravee.suthikulpanit, joro, will,
	robin.murphy, linux-kernel

This series comprises two features:

HATDis: When host address translation is not supported by the IOMMU,
the IVHD->attr[HATDis] bit is set. For instance, QEMU emulated vIOMMU
does not support dma tranlation for VFIO passthrough device.
Support has been added to check this bit and switch to guest page table 
mode, if supported. This feature is useful for cases where only interrupt
remapping is required.

EFR[HATS]: These bits indicate the maximum level supported for host page table
by the IOMMU. Modifications have been made to adjust the maximum host page table
level, starting with level 4. If these bits are set to 11b, the driver will
attempt to use the guest page table.

If guest page table mode is not supported, the driver will fail to probe
devices for DMA translations.
The interrupt remapping will continue to be supported.

---
v4:
- Relocate sysfs PD_MODE_NONE check case after iommu_device_register()
  along with return check.
v3:
- Add check before populating sysfs entries for iommu groups.
v2:
- Addressed comments from Vasant:
	- Added/Modified comments and logs.
	- Removed v1 page table assignment hunk.

Ankit Soni (2):
  iommu/amd: Add HATDis feature support
  iommu/amd: Add efr[HATS] max v1 page table level

 drivers/iommu/amd/amd_iommu.h       |  2 ++
 drivers/iommu/amd/amd_iommu_types.h |  7 ++++-
 drivers/iommu/amd/init.c            | 47 +++++++++++++++++++++++++++--
 drivers/iommu/amd/io_pgtable.c      |  4 +--
 drivers/iommu/amd/iommu.c           | 15 ++++++++-
 5 files changed, 69 insertions(+), 6 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v4 1/2] iommu/amd: Add HATDis feature support
  2025-06-04  6:13 [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
@ 2025-06-04  6:13 ` Ankit Soni
  2025-06-04  6:13 ` [PATCH v4 2/2] iommu/amd: Add efr[HATS] max v1 page table level Ankit Soni
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ankit Soni @ 2025-06-04  6:13 UTC (permalink / raw)
  To: iommu
  Cc: vasant.hegde, joao.m.martins, suravee.suthikulpanit, joro, will,
	robin.murphy, linux-kernel

Current AMD IOMMU assumes Host Address Translation (HAT) is always
supported, and Linux kernel enables this capability by default. However,
in case of emulated and virtualized IOMMU, this might not be the case.
For example,current QEMU-emulated AMD vIOMMU does not support host
translation for VFIO pass-through device, but the interrupt remapping
support is required for x2APIC (i.e. kvm-msi-ext-dest-id is also not
supported by the guest OS). This would require the guest kernel to boot
with guest kernel option iommu=pt to by-pass the initialization of
host (v1) table.

The AMD I/O Virtualization Technology (IOMMU) Specification Rev 3.10 [1]
introduces a new flag 'HATDis' in the IVHD 11h IOMMU attributes to indicate
that HAT is not supported on a particular IOMMU instance.

Therefore, modifies the AMD IOMMU driver to detect the new HATDis
attributes, and disable host translation and switch to use guest
translation if it is available. Otherwise, the driver will disable DMA
translation.

[1] https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/specifications/48882_IOMMU.pdf

Reviewed-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Signed-off-by: Ankit Soni <Ankit.Soni@amd.com>
---
 drivers/iommu/amd/amd_iommu.h       |  1 +
 drivers/iommu/amd/amd_iommu_types.h |  6 +++++-
 drivers/iommu/amd/init.c            | 31 +++++++++++++++++++++++++++--
 drivers/iommu/amd/iommu.c           | 13 ++++++++++++
 4 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h
index 29a8864381c3..fddfad4a9009 100644
--- a/drivers/iommu/amd/amd_iommu.h
+++ b/drivers/iommu/amd/amd_iommu.h
@@ -43,6 +43,7 @@ extern int amd_iommu_guest_ir;
 extern enum protection_domain_mode amd_iommu_pgtable;
 extern int amd_iommu_gpt_level;
 extern unsigned long amd_iommu_pgsize_bitmap;
+extern bool amd_iommu_hatdis;
 
 /* Protection domain ops */
 void amd_iommu_init_identity_domain(void);
diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
index ccbab3a4811a..69291cef73f7 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -460,6 +460,9 @@
 /* IOMMU Feature Reporting Field (for IVHD type 10h */
 #define IOMMU_FEAT_GASUP_SHIFT	6
 
+/* IOMMU HATDIS for IVHD type 11h and 40h */
+#define IOMMU_IVHD_ATTR_HATDIS_SHIFT	0
+
 /* IOMMU Extended Feature Register (EFR) */
 #define IOMMU_EFR_XTSUP_SHIFT	2
 #define IOMMU_EFR_GASUP_SHIFT	7
@@ -558,7 +561,8 @@ struct amd_io_pgtable {
 };
 
 enum protection_domain_mode {
-	PD_MODE_V1 = 1,
+	PD_MODE_NONE,
+	PD_MODE_V1,
 	PD_MODE_V2,
 };
 
diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
index c06b62f87b9b..5dda0f6d2492 100644
--- a/drivers/iommu/amd/init.c
+++ b/drivers/iommu/amd/init.c
@@ -168,6 +168,9 @@ static int amd_iommu_target_ivhd_type;
 u64 amd_iommu_efr;
 u64 amd_iommu_efr2;
 
+/* Host (v1) page table is not supported*/
+bool amd_iommu_hatdis;
+
 /* SNP is enabled on the system? */
 bool amd_iommu_snp_en;
 EXPORT_SYMBOL(amd_iommu_snp_en);
@@ -1792,6 +1795,11 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
 		if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
 			amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
 
+		if (h->efr_attr & BIT(IOMMU_IVHD_ATTR_HATDIS_SHIFT)) {
+			pr_warn_once("Host Address Translation is not supported.\n");
+			amd_iommu_hatdis = true;
+		}
+
 		early_iommu_features_init(iommu, h);
 
 		break;
@@ -2115,7 +2123,15 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
 			return ret;
 	}
 
-	iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
+	ret = iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
+	if (ret || amd_iommu_pgtable == PD_MODE_NONE) {
+		/*
+		 * Remove sysfs if DMA translation is not supported by the
+		 * IOMMU. Do not return an error to enable IRQ remapping
+		 * in state_next(), DTE[V, TV] must eventually be set to 0.
+		 */
+		iommu_device_sysfs_remove(&iommu->iommu);
+	}
 
 	return pci_enable_device(iommu->dev);
 }
@@ -2576,7 +2592,7 @@ static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
 	u32 devid;
 	struct dev_table_entry *dev_table = pci_seg->dev_table;
 
-	if (dev_table == NULL)
+	if (!dev_table || amd_iommu_pgtable == PD_MODE_NONE)
 		return;
 
 	for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
@@ -3087,6 +3103,17 @@ static int __init early_amd_iommu_init(void)
 		}
 	}
 
+	if (amd_iommu_hatdis) {
+		/*
+		 * Host (v1) page table is not available. Attempt to use
+		 * Guest (v2) page table.
+		 */
+		if (amd_iommu_v2_pgtbl_supported())
+			amd_iommu_pgtable = PD_MODE_V2;
+		else
+			amd_iommu_pgtable = PD_MODE_NONE;
+	}
+
 	/* Disable any previously enabled IOMMUs */
 	if (!is_kdump_kernel() || amd_iommu_disabled)
 		disable_iommus();
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 3117d99cf83d..8a9babd6dfa7 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2424,6 +2424,13 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)
 					     pci_max_pasids(to_pci_dev(dev)));
 	}
 
+	if (amd_iommu_pgtable == PD_MODE_NONE) {
+		pr_warn_once("%s: DMA translation not supported by iommu.\n",
+			     __func__);
+		iommu_dev = ERR_PTR(-ENODEV);
+		goto out_err;
+	}
+
 out_err:
 
 	iommu_completion_wait(iommu);
@@ -2511,6 +2518,9 @@ static int pdom_setup_pgtable(struct protection_domain *domain,
 	case PD_MODE_V2:
 		fmt = AMD_IOMMU_V2;
 		break;
+	case PD_MODE_NONE:
+		WARN_ON_ONCE(1);
+		return -EPERM;
 	}
 
 	domain->iop.pgtbl.cfg.amd.nid = dev_to_node(dev);
@@ -2532,6 +2542,9 @@ static inline u64 dma_max_address(enum protection_domain_mode pgtable)
 
 static bool amd_iommu_hd_support(struct amd_iommu *iommu)
 {
+	if (amd_iommu_hatdis)
+		return false;
+
 	return iommu && (iommu->features & FEATURE_HDSUP);
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v4 2/2] iommu/amd: Add efr[HATS] max v1 page table level
  2025-06-04  6:13 [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
  2025-06-04  6:13 ` [PATCH v4 1/2] iommu/amd: Add HATDis feature support Ankit Soni
@ 2025-06-04  6:13 ` Ankit Soni
  2025-06-26 10:58 ` [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
  2025-06-27  6:51 ` Joerg Roedel
  3 siblings, 0 replies; 5+ messages in thread
From: Ankit Soni @ 2025-06-04  6:13 UTC (permalink / raw)
  To: iommu
  Cc: vasant.hegde, joao.m.martins, suravee.suthikulpanit, joro, will,
	robin.murphy, linux-kernel

The EFR[HATS] bits indicate maximum host translation level supported by
IOMMU. Adding support to set the maximum host page table level as indicated
by EFR[HATS]. If the HATS=11b (reserved), the driver will attempt to use
guest page table for DMA API.

Reviewed-by: Vasant Hegde <vasant.hegde@amd.com>
Reviewed-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Signed-off-by: Ankit Soni <Ankit.Soni@amd.com>
---
 drivers/iommu/amd/amd_iommu.h       |  1 +
 drivers/iommu/amd/amd_iommu_types.h |  1 +
 drivers/iommu/amd/init.c            | 16 ++++++++++++++++
 drivers/iommu/amd/io_pgtable.c      |  4 ++--
 drivers/iommu/amd/iommu.c           |  2 +-
 5 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h
index fddfad4a9009..0bf3744c7b3a 100644
--- a/drivers/iommu/amd/amd_iommu.h
+++ b/drivers/iommu/amd/amd_iommu.h
@@ -42,6 +42,7 @@ int amd_iommu_enable_faulting(unsigned int cpu);
 extern int amd_iommu_guest_ir;
 extern enum protection_domain_mode amd_iommu_pgtable;
 extern int amd_iommu_gpt_level;
+extern u8 amd_iommu_hpt_level;
 extern unsigned long amd_iommu_pgsize_bitmap;
 extern bool amd_iommu_hatdis;
 
diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
index 69291cef73f7..35ee7b0648af 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -94,6 +94,7 @@
 #define FEATURE_GA		BIT_ULL(7)
 #define FEATURE_HE		BIT_ULL(8)
 #define FEATURE_PC		BIT_ULL(9)
+#define FEATURE_HATS		GENMASK_ULL(11, 10)
 #define FEATURE_GATS		GENMASK_ULL(13, 12)
 #define FEATURE_GLX		GENMASK_ULL(15, 14)
 #define FEATURE_GAM_VAPIC	BIT_ULL(21)
diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
index 5dda0f6d2492..1b4f4c324e9c 100644
--- a/drivers/iommu/amd/init.c
+++ b/drivers/iommu/amd/init.c
@@ -152,6 +152,8 @@ bool amd_iommu_dump;
 bool amd_iommu_irq_remap __read_mostly;
 
 enum protection_domain_mode amd_iommu_pgtable = PD_MODE_V1;
+/* Host page table level */
+u8 amd_iommu_hpt_level;
 /* Guest page table level */
 int amd_iommu_gpt_level = PAGE_MODE_4_LEVEL;
 
@@ -3052,6 +3054,7 @@ static int __init early_amd_iommu_init(void)
 	struct acpi_table_header *ivrs_base;
 	int ret;
 	acpi_status status;
+	u8 efr_hats;
 
 	if (!amd_iommu_detected)
 		return -ENODEV;
@@ -3096,6 +3099,19 @@ static int __init early_amd_iommu_init(void)
 	    FIELD_GET(FEATURE_GATS, amd_iommu_efr) == GUEST_PGTABLE_5_LEVEL)
 		amd_iommu_gpt_level = PAGE_MODE_5_LEVEL;
 
+	efr_hats = FIELD_GET(FEATURE_HATS, amd_iommu_efr);
+	if (efr_hats != 0x3) {
+		/*
+		 * efr[HATS] bits specify the maximum host translation level
+		 * supported, with LEVEL 4 being initial max level.
+		 */
+		amd_iommu_hpt_level = efr_hats + PAGE_MODE_4_LEVEL;
+	} else {
+		pr_warn_once(FW_BUG "Disable host address translation due to invalid translation level (%#x).\n",
+			     efr_hats);
+		amd_iommu_hatdis = true;
+	}
+
 	if (amd_iommu_pgtable == PD_MODE_V2) {
 		if (!amd_iommu_v2_pgtbl_supported()) {
 			pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");
diff --git a/drivers/iommu/amd/io_pgtable.c b/drivers/iommu/amd/io_pgtable.c
index 4d308c071134..a91e71f981ef 100644
--- a/drivers/iommu/amd/io_pgtable.c
+++ b/drivers/iommu/amd/io_pgtable.c
@@ -125,7 +125,7 @@ static bool increase_address_space(struct amd_io_pgtable *pgtable,
 		goto out;
 
 	ret = false;
-	if (WARN_ON_ONCE(pgtable->mode == PAGE_MODE_6_LEVEL))
+	if (WARN_ON_ONCE(pgtable->mode == amd_iommu_hpt_level))
 		goto out;
 
 	*pte = PM_LEVEL_PDE(pgtable->mode, iommu_virt_to_phys(pgtable->root));
@@ -526,7 +526,7 @@ static void v1_free_pgtable(struct io_pgtable *iop)
 
 	/* Page-table is not visible to IOMMU anymore, so free it */
 	BUG_ON(pgtable->mode < PAGE_MODE_NONE ||
-	       pgtable->mode > PAGE_MODE_6_LEVEL);
+	       pgtable->mode > amd_iommu_hpt_level);
 
 	free_sub_pt(pgtable->root, pgtable->mode, &freelist);
 	iommu_put_pages_list(&freelist);
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 8a9babd6dfa7..9c67f0be2b35 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2534,7 +2534,7 @@ static int pdom_setup_pgtable(struct protection_domain *domain,
 static inline u64 dma_max_address(enum protection_domain_mode pgtable)
 {
 	if (pgtable == PD_MODE_V1)
-		return ~0ULL;
+		return PM_LEVEL_SIZE(amd_iommu_hpt_level);
 
 	/* V2 with 4/5 level page table */
 	return ((1ULL << PM_LEVEL_SHIFT(amd_iommu_gpt_level)) - 1);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features
  2025-06-04  6:13 [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
  2025-06-04  6:13 ` [PATCH v4 1/2] iommu/amd: Add HATDis feature support Ankit Soni
  2025-06-04  6:13 ` [PATCH v4 2/2] iommu/amd: Add efr[HATS] max v1 page table level Ankit Soni
@ 2025-06-26 10:58 ` Ankit Soni
  2025-06-27  6:51 ` Joerg Roedel
  3 siblings, 0 replies; 5+ messages in thread
From: Ankit Soni @ 2025-06-26 10:58 UTC (permalink / raw)
  To: iommu
  Cc: vasant.hegde, joao.m.martins, suravee.suthikulpanit, joro, will,
	robin.murphy, linux-kernel

On Wed, Jun 04, 2025 at 06:13:23AM +0000, Ankit Soni wrote:
> This series comprises two features:
> 
> HATDis: When host address translation is not supported by the IOMMU,
> the IVHD->attr[HATDis] bit is set. For instance, QEMU emulated vIOMMU
> does not support dma tranlation for VFIO passthrough device.
> Support has been added to check this bit and switch to guest page table 
> mode, if supported. This feature is useful for cases where only interrupt
> remapping is required.
> 
> EFR[HATS]: These bits indicate the maximum level supported for host page table
> by the IOMMU. Modifications have been made to adjust the maximum host page table
> level, starting with level 4. If these bits are set to 11b, the driver will
> attempt to use the guest page table.
> 
> If guest page table mode is not supported, the driver will fail to probe
> devices for DMA translations.
> The interrupt remapping will continue to be supported.
> 
> ---
> v4:
> - Relocate sysfs PD_MODE_NONE check case after iommu_device_register()
>   along with return check.
> v3:
> - Add check before populating sysfs entries for iommu groups.
> v2:
> - Addressed comments from Vasant:
> 	- Added/Modified comments and logs.
> 	- Removed v1 page table assignment hunk.
> 
> Ankit Soni (2):
>   iommu/amd: Add HATDis feature support
>   iommu/amd: Add efr[HATS] max v1 page table level
> 
>  drivers/iommu/amd/amd_iommu.h       |  2 ++
>  drivers/iommu/amd/amd_iommu_types.h |  7 ++++-
>  drivers/iommu/amd/init.c            | 47 +++++++++++++++++++++++++++--
>  drivers/iommu/amd/io_pgtable.c      |  4 +--
>  drivers/iommu/amd/iommu.c           | 15 ++++++++-
>  5 files changed, 69 insertions(+), 6 deletions(-)
> 
> -- 
> 2.43.0
> 

Gentle ping.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features
  2025-06-04  6:13 [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
                   ` (2 preceding siblings ...)
  2025-06-26 10:58 ` [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
@ 2025-06-27  6:51 ` Joerg Roedel
  3 siblings, 0 replies; 5+ messages in thread
From: Joerg Roedel @ 2025-06-27  6:51 UTC (permalink / raw)
  To: Ankit Soni
  Cc: iommu, vasant.hegde, joao.m.martins, suravee.suthikulpanit, will,
	robin.murphy, linux-kernel

On Wed, Jun 04, 2025 at 06:13:23AM +0000, Ankit Soni wrote:
> Ankit Soni (2):
>   iommu/amd: Add HATDis feature support
>   iommu/amd: Add efr[HATS] max v1 page table level
> 
>  drivers/iommu/amd/amd_iommu.h       |  2 ++
>  drivers/iommu/amd/amd_iommu_types.h |  7 ++++-
>  drivers/iommu/amd/init.c            | 47 +++++++++++++++++++++++++++--
>  drivers/iommu/amd/io_pgtable.c      |  4 +--
>  drivers/iommu/amd/iommu.c           | 15 ++++++++-
>  5 files changed, 69 insertions(+), 6 deletions(-)

Applied, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-06-27  6:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-04  6:13 [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
2025-06-04  6:13 ` [PATCH v4 1/2] iommu/amd: Add HATDis feature support Ankit Soni
2025-06-04  6:13 ` [PATCH v4 2/2] iommu/amd: Add efr[HATS] max v1 page table level Ankit Soni
2025-06-26 10:58 ` [PATCH v4 0/2] iommu/amd: Support for HATdis and HATS features Ankit Soni
2025-06-27  6:51 ` Joerg Roedel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.