[PATCH 13/22] iommu/amd: Add helper functions to manage DevID / DomID mapping tables

Suravee Suthikulpanit
2026-03-30T08:43:48Z

Introduce amd_viommu_init_one() and amd_viommu_uninit_one().
These functions are called during IOMMUFD vIOMMU initialize and destroy.
Currently, it manages the IPA mapping for Device ID and Domain ID mapping
tables.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
drivers/iommu/amd/amd_iommu_types.h | 3 ++
drivers/iommu/amd/amd_viommu.h | 15 +++++++++
drivers/iommu/amd/iommu.c | 1 +
drivers/iommu/amd/iommufd.c | 5 +++
drivers/iommu/amd/viommu.c | 52 +++++++++++++++++++++++++++++
5 files changed, 76 insertions(+)

diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
index 2d96c72ba45f..fda7109766f3 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -543,6 +543,9 @@ struct amd_iommu_viommu {
* Indexed by guest domain ID.
*/
struct xarray gdomid_array;
+
+ u64 *devid_table;
+ u64 *domid_table;
};

/*
diff --git a/drivers/iommu/amd/amd_viommu.h b/drivers/iommu/amd/amd_viommu.h
index 8dbb12241e8d..ccdd1cbcec36 100644
--- a/drivers/iommu/amd/amd_viommu.h
+++ b/drivers/iommu/amd/amd_viommu.h
@@ -11,6 +11,11 @@
int amd_viommu_init(struct amd_iommu *iommu);

u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid);
+
+int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu);
+
+void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu);
+
#else

static inline int amd_viommu_init(struct amd_iommu *iommu)
@@ -22,6 +27,16 @@ u64 amd_viommu_get_vfmmio_addr(struct amd_iommu *iommu, u16 gid);
{
return 0;
}
+
+static inline int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu)
+{
+}
+
#endif /* CONFIG_AMD_IOMMU_IOMMUFD */

#endif /* AMD_VIOMMU_H */
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index 4be3589b9393..8d8f4f374d5f 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -43,6 +43,7 @@
#include <linux/generic_pt/iommu.h>

#include "amd_iommu.h"
+#include "amd_viommu.h"
#include "iommufd.h"
#include "../irq_remapping.h"
#include "../iommu-pages.h"
diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c
index 06d6aa87cdcd..c2281557b3bf 100644
--- a/drivers/iommu/amd/iommufd.c
+++ b/drivers/iommu/amd/iommufd.c
@@ -82,6 +82,10 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *
/* Reset vIOMMU MMIOs to initialize the vIOMMU */
iommu_reset_vmmio(iommu, aviommu->gid);

+ ret = amd_viommu_init_one(iommu, aviommu);
+ if (ret)
+ goto err_out;
+
ret = iommu_copy_struct_to_user(user_data, &data,
IOMMU_VIOMMU_TYPE_AMD,
reserved);
@@ -116,6 +120,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
spin_unlock_irqrestore(&pdom->lock, flags);
xa_destroy(&aviommu->gdomid_array);
amd_iommu_gid_free(aviommu->gid);
+ amd_viommu_uninit_one(iommu, aviommu);
}

/*
diff --git a/drivers/iommu/amd/viommu.c b/drivers/iommu/amd/viommu.c
index fbc6b37b2517..53cf6ab2db04 100644
--- a/drivers/iommu/amd/viommu.c
+++ b/drivers/iommu/amd/viommu.c
@@ -26,6 +26,20 @@
#include "amd_viommu.h"
#include "../iommu-pages.h"

+/*
+ * Guest Device ID Mapping Table
+ */
+#define VIOMMU_MAX_GDEVID 0xFFFF
+#define VIOMMU_DEVID_MAPPING_BASE 0x1000000000ULL
+#define VIOMMU_DEVID_MAPPING_ENTRY_SIZE (1 << 20)
+
+/*
+ * Guest Domain ID Mapping Table
+ */
+#define VIOMMU_MAX_GDOMID 0xFFFF
+#define VIOMMU_DOMID_MAPPING_BASE 0x2000000000ULL
+#define VIOMMU_DOMID_MAPPING_ENTRY_SIZE (1 << 19)
+
LIST_HEAD(viommu_devid_map);

static int viommu_init_pci_vsc(struct amd_iommu *iommu)
@@ -335,3 +349,41 @@ static void free_private_vm_region(struct amd_iommu *iommu, u64 **entry,
iommu_free_pages(*entry);
*entry = NULL;
}
+
+void amd_viommu_uninit_one(struct amd_iommu *iommu, struct amd_iommu_viommu *aviommu)
+{
+ pr_debug("%s: gid=%u\n", __func__, aviommu->gid);
+
+ free_private_vm_region(iommu, &aviommu->devid_table,
+ VIOMMU_DEVID_MAPPING_BASE,
+ VIOMMU_DEVID_MAPPING_ENTRY_SIZE,
+ aviommu->gid);
+ free_private_vm_region(iommu, &aviommu->domid_table,
+ VIOMMU_DOMID_MAPPING_BASE,
+ VIOMMU_DOMID_MAPPING_ENTRY_SIZE,
+ aviommu->gid);
+}
+
+int amd_viommu_init_one(struct amd_iommu *iommu, struct amd_iommu_viommu *viommu)
+{
+ int ret;
+
+ ret = alloc_private_vm_region(iommu, &viommu->devid_table,
+ VIOMMU_DEVID_MAPPING_BASE,
+ VIOMMU_DEVID_MAPPING_ENTRY_SIZE,
+ viommu->gid);
+ if (ret)
+ goto err_out;
+
+ ret = alloc_private_vm_region(iommu, &viommu->domid_table,
+ VIOMMU_DOMID_MAPPING_BASE,
+ VIOMMU_DOMID_MAPPING_ENTRY_SIZE,
+ viommu->gid);
+ if (ret)
+ goto err_out;
+
+ return 0;
+err_out:
+ amd_viommu_uninit_one(iommu, viommu);
+ return -ENOMEM;
+}
--
2.34.1