[PATCH v3 13/22] iommu/amd: Assign IOMMU Private Address domain to IOMMU

From: Suravee Suthikulpanit

Date: Mon Jun 29 2026 - 11:59:32 EST


By setting the domain ID, pagetable mode, and IOMMU v1 page table in the
IOMMU Device Table Entry (DTE) indexed using the device ID of the
AMD IOMMU.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@xxxxxxx>
---
drivers/iommu/amd/amd_iommu.h | 2 ++
drivers/iommu/amd/amd_iommu_types.h | 1 +
drivers/iommu/amd/iommu.c | 23 +++++++++++++
drivers/iommu/amd/viommu.c | 51 +++++++++++++++++++++++++++--
4 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h
index 269d0f448b9d..aaa57840e904 100644
--- a/drivers/iommu/amd/amd_iommu.h
+++ b/drivers/iommu/amd/amd_iommu.h
@@ -57,6 +57,8 @@ extern unsigned long amd_iommu_pgsize_bitmap;
extern bool amd_iommu_hatdis;

struct iommu_dev_data *amd_iommu_alloc_dev_data(struct amd_iommu *iommu, u16 devid);
+void amd_iommu_free_dev_data(struct amd_iommu *iommu,
+ struct iommu_dev_data *dev_data);

/* 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 a5e2f32590d1..340929ae776a 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -823,6 +823,7 @@ struct amd_iommu {
bool gid_ida_inited;

/* HW vIOMMU support */
+ struct iommu_dev_data *viommu_dev_data;
struct protection_domain *viommu_pdom;
void *viommu_priv_region[VIOMMU_PRIV_SUBREGION_CNT];
};
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index b9063a7d6a6e..e5dc34959c6e 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -406,6 +406,29 @@ struct iommu_dev_data *amd_iommu_alloc_dev_data(struct amd_iommu *iommu, u16 dev
return dev_data;
}

+void amd_iommu_free_dev_data(struct amd_iommu *iommu,
+ struct iommu_dev_data *dev_data)
+{
+ struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
+ struct llist_node *prev = NULL, *node;
+
+ if (!dev_data)
+ return;
+
+ for (node = pci_seg->dev_data_list.first; node;
+ prev = node, node = node->next) {
+ if (node == &dev_data->dev_data_list) {
+ if (prev)
+ prev->next = node->next;
+ else
+ pci_seg->dev_data_list.first = node->next;
+ break;
+ }
+ }
+
+ kfree(dev_data);
+}
+
struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid)
{
struct iommu_dev_data *dev_data;
diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c
index c5c85e82c265..897355a143d6 100644
--- a/drivers/iommu/amd/viommu.c
+++ b/drivers/iommu/amd/viommu.c
@@ -71,8 +71,27 @@ static void __init amd_viommu_vf_vfcntl_unmap(struct amd_iommu *iommu)
release_mem_region(iommu->vf_base_phys, VIOMMU_VF_MMIO_MAP_SIZE);
}

+static void viommu_free_self_dev_data(struct amd_iommu *iommu, bool clear_dte)
+{
+ struct iommu_dev_data *dev_data = iommu->viommu_dev_data;
+
+ if (!dev_data)
+ return;
+
+ if (clear_dte) {
+ struct dev_table_entry new = {};
+
+ amd_iommu_make_clear_dte(iommu, dev_data->devid, &new);
+ amd_iommu_update_dte(iommu, dev_data, &new);
+ }
+
+ amd_iommu_free_dev_data(iommu, dev_data);
+ iommu->viommu_dev_data = NULL;
+}
+
void __init amd_viommu_uninit(struct amd_iommu *iommu)
{
+ viommu_free_self_dev_data(iommu, true);
amd_viommu_gid_ida_fini(iommu);
amd_viommu_vf_vfcntl_unmap(iommu);
}
@@ -303,21 +322,39 @@ u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid)
}
EXPORT_SYMBOL(amd_viommu_get_vfmmio_addr);

+static void set_dte_ipa(struct amd_iommu *iommu, struct dev_table_entry *new)
+{
+ struct pt_iommu_amdv1_hw_info pt_info;
+ struct protection_domain *pdom = iommu->viommu_pdom;
+
+ pt_iommu_amdv1_hw_info(&pdom->amdv1, &pt_info);
+ amd_iommu_set_dte_v1(iommu->viommu_dev_data, pdom, pdom->id, &pt_info, new);
+}
+
int __init amd_viommu_init(struct amd_iommu *iommu)
{
int ret;
+ bool dte_set = false;
+ struct dev_table_entry new = {};

if (!amd_iommu_viommu ||
!check_feature(FEATURE_VIOMMU))
return 0;

+ iommu->viommu_dev_data = amd_iommu_alloc_dev_data(iommu, iommu->devid);
+ if (!iommu->viommu_dev_data) {
+ pr_err("%s: Failed to allocate dev_data\n", __func__);
+ return -ENOMEM;
+ }
+ iommu->viommu_dev_data->dev = &iommu->dev->dev;
+
ret = viommu_init_pci_vsc(iommu);
if (ret)
- return ret;
+ goto err_dev_data;

ret = viommu_vf_vfcntl_init(iommu);
if (ret)
- return ret;
+ goto err_dev_data;

amd_viommu_gid_ida_init(iommu);

@@ -325,5 +362,15 @@ int __init amd_viommu_init(struct amd_iommu *iommu)
if (ret)
return ret;

+ /* Set DTE for IOMMU device */
+ amd_iommu_make_clear_dte(iommu, iommu->devid, &new);
+ set_dte_ipa(iommu, &new);
+ amd_iommu_update_dte(iommu, iommu->viommu_dev_data, &new);
+ dte_set = true;
+
return 0;
+
+err_dev_data:
+ viommu_free_self_dev_data(iommu, dte_set);
+ return ret;
}
--
2.34.1