[PATCH 05/22] iommu/amd: Introduce vIOMMU Guest-ID (GID)

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

AMD HW-vIOMMU feature requires IOMMU driver to specify a unique 16-bit
Guest ID (GID) for each VM. This ID is per-vIOMMU instance, and it is used
to index into various data structures for configuring the hardware.

Introduce helper functions amd_iommu_gid_alloc() / amd_iommu_gid_free(),
which will be called during vIOMMU initialize/destroy.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
drivers/iommu/amd/amd_iommu.h | 4 ++++
drivers/iommu/amd/amd_iommu_types.h | 4 ++++
drivers/iommu/amd/iommu.c | 20 ++++++++++++++++++++
drivers/iommu/amd/iommufd.c | 29 +++++++++++++++++++++++++++++
4 files changed, 57 insertions(+)

diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h
index c440f2745b38..51a7c3b3329f 100644
--- a/drivers/iommu/amd/amd_iommu.h
+++ b/drivers/iommu/amd/amd_iommu.h
@@ -226,4 +226,8 @@ amd_iommu_make_clear_dte(struct iommu_dev_data *dev_data, struct dev_table_entry
struct iommu_domain *
amd_iommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
const struct iommu_user_data *user_data);
+
+/* Guest ID for vIOMMU */
+int amd_iommu_gid_alloc(void);
+void amd_iommu_gid_free(int gid);
#endif /* AMD_IOMMU_H */
diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
index c5f779d76fc4..0b4d713d3337 100644
--- a/drivers/iommu/amd/amd_iommu_types.h
+++ b/drivers/iommu/amd/amd_iommu_types.h
@@ -413,6 +413,9 @@

#define MAX_DOMAIN_ID 65536

+/* For vIOMMU, the GID is 16-bit. */
+#define VIOMMU_MAX_GID 0xFFFF
+
/* Timeout stuff */
#define LOOP_TIMEOUT 100000
#define MMIO_STATUS_TIMEOUT 2000000
@@ -509,6 +512,7 @@ struct amd_iommu_viommu {
struct iommufd_viommu core;
struct protection_domain *parent; /* nest parent domain for this viommu */
struct list_head pdom_list; /* For protection_domain->viommu_list */
+ u16 gid; /* Guest ID for the vIOMMU */

/*
* Per-vIOMMU guest domain ID to host domain ID mapping.
diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index d1f997b9a90b..037a50397f31 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -61,6 +61,9 @@ LIST_HEAD(acpihid_map);

const struct iommu_ops amd_iommu_ops;

+/* Global guest ID */
+static DEFINE_IDA(amd_iommu_global_gid_ida);
+
int amd_iommu_max_glx_val = -1;

/*
@@ -252,6 +255,23 @@ static inline bool pdom_is_sva_capable(struct protection_domain *pdom)
return pdom_is_v2_pgtbl_mode(pdom) || pdom_is_in_pt_mode(pdom);
}

+int amd_iommu_gid_alloc(void)
+{
+ int ret = ida_alloc_range(&amd_iommu_global_gid_ida, 1, VIOMMU_MAX_GID, GFP_KERNEL);
+
+ if (ret < 0)
+ pr_err("%s: Failed to allocate guest ID\n", __func__);
+ else
+ pr_debug("%s: gid=%u\n", __func__, ret);
+
+ return ret;
+}
+
+void amd_iommu_gid_free(int gid)
+{
+ ida_free(&amd_iommu_global_gid_ida, gid);
+}
+
static inline int get_acpihid_device_id(struct device *dev,
struct acpihid_map_entry **entry)
{
diff --git a/drivers/iommu/amd/iommufd.c b/drivers/iommu/amd/iommufd.c
index e7ffa7e8915b..6fba5d9b1310 100644
--- a/drivers/iommu/amd/iommufd.c
+++ b/drivers/iommu/amd/iommufd.c
@@ -43,13 +43,35 @@ size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type vi
int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *parent,
const struct iommu_user_data *user_data)
{
+ int ret;
unsigned long flags;
+ struct iommu_viommu_amd data;
struct protection_domain *pdom = to_pdomain(parent);
struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);

xa_init_flags(&aviommu->gdomid_array, XA_FLAGS_ALLOC1);
aviommu->parent = pdom;

+ if (!user_data)
+ return -EINVAL;
+
+ ret = iommu_copy_struct_from_user(&data, user_data,
+ IOMMU_VIOMMU_TYPE_AMD,
+ reserved);
+ if (ret)
+ return ret;
+
+ aviommu->gid = amd_iommu_gid_alloc();
+ if (aviommu->gid < 0)
+ return aviommu->gid;
+ pr_debug("%s: gid=%#x", __func__, aviommu->gid);
+
+ ret = iommu_copy_struct_to_user(user_data, &data,
+ IOMMU_VIOMMU_TYPE_AMD,
+ reserved);
+ if (ret)
+ goto err_out;
+
viommu->ops = &amd_viommu_ops;

spin_lock_irqsave(&pdom->lock, flags);
@@ -57,6 +79,9 @@ int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *
spin_unlock_irqrestore(&pdom->lock, flags);

return 0;
+err_out:
+ amd_iommu_gid_free(aviommu->gid);
+ return ret;
}

static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
@@ -64,11 +89,15 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
unsigned long flags;
struct amd_iommu_viommu *aviommu = container_of(viommu, struct amd_iommu_viommu, core);
struct protection_domain *pdom = aviommu->parent;
+ struct amd_iommu *iommu = container_of(viommu->iommu_dev, struct amd_iommu, iommu);
+
+ pr_debug("%s: gid=%#x, iommu devid=%#x\n", __func__, aviommu->gid, iommu->devid);

spin_lock_irqsave(&pdom->lock, flags);
list_del(&aviommu->pdom_list);
spin_unlock_irqrestore(&pdom->lock, flags);
xa_destroy(&aviommu->gdomid_array);
+ amd_iommu_gid_free(aviommu->gid);
}

/*
--
2.34.1