[PATCH v3 14/15] iommu/amd: Refactor logic to program the host page table in DTE

From: Suravee Suthikulpanit
Date: Thu Oct 09 2025 - 20:01:39 EST


Introduce the set_dte_v1() helper function to configure IOMMU host (v1)
page table into DTE.

There is no functional change.

Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx>
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@xxxxxxx>
---
drivers/iommu/amd/iommu.c | 54 +++++++++++++++++++++------------------
1 file changed, 29 insertions(+), 25 deletions(-)

diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index ffb1adfd75c0..2a536d02aeab 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2044,6 +2044,32 @@ static void set_dte_gcr3_table(struct amd_iommu *iommu,
target->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_4_LEVEL);
}

+static void set_dte_v1(struct iommu_dev_data *dev_data,
+ struct protection_domain *domain, u16 domid,
+ struct dev_table_entry *new)
+{
+ /*
+ * 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));
+
+ if (domain->iop.mode != PAGE_MODE_NONE)
+ new->data[0] |= iommu_virt_to_phys(domain->iop.root);
+
+ new->data[0] |= FIELD_PREP(DTE_MODE_MASK, domain->iop.mode);
+ new->data[0] |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_TV;
+
+ if (domain->dirty_tracking)
+ new->data[0] |= DTE_FLAG_HAD;
+
+ if (dev_data->ats_enabled)
+ new->data[1] |= DTE_FLAG_IOTLB;
+
+ new->data[1] |= domid;
+}
+
static void set_dte_entry(struct amd_iommu *iommu,
struct iommu_dev_data *dev_data)
{
@@ -2061,36 +2087,14 @@ static void set_dte_entry(struct amd_iommu *iommu,

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);
-
- new.data[0] |= (domain->iop.mode & DEV_ENTRY_MODE_MASK)
- << DEV_ENTRY_MODE_SHIFT;
-
- new.data[0] |= DTE_FLAG_IR | DTE_FLAG_IW;
+ old_domid = READ_ONCE(dte->data[1]) & DTE_DOMID_MASK;

- /*
- * 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));
- new.data[0] |= DTE_FLAG_TV;
+ set_dte_v1(dev_data, domain, domid, &new);
+ set_dte_gcr3_table(iommu, dev_data, &new);

if (dev_data->ppr)
new.data[0] |= 1ULL << DEV_ENTRY_PPR;

- if (domain->dirty_tracking)
- new.data[0] |= DTE_FLAG_HAD;
-
- if (dev_data->ats_enabled)
- new.data[1] |= DTE_FLAG_IOTLB;
-
- old_domid = READ_ONCE(dte->data[1]) & DTE_DOMID_MASK;
- new.data[1] |= domid;
-
- set_dte_gcr3_table(iommu, dev_data, &new);
-
amd_iommu_update_dte256(iommu, dev_data, &new);

/*
--
2.34.1