[RFC][PATCH v2 01/13] iommu: Add set_nesting_vmid/get_nesting_vmid functions

From: Nicolin Chen
Date: Mon Aug 30 2021 - 23:09:01 EST


VMID stands for Virtual Machine Identifier, being used to tag
TLB entries to indicate which VM they belong to. This is used
by some IOMMU like SMMUv3 for virtualization case, in nesting
mode.

So this patch adds a pair of new iommu_ops callback functions
with a pair of exported set/get functions to allow VFIO core
to get access of the VMID value in an IOMMU driver.

Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
---
drivers/iommu/iommu.c | 20 ++++++++++++++++++++
include/linux/iommu.h | 5 +++++
2 files changed, 25 insertions(+)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 3303d707bab4..051f2df36dc0 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2774,6 +2774,26 @@ int iommu_enable_nesting(struct iommu_domain *domain)
}
EXPORT_SYMBOL_GPL(iommu_enable_nesting);

+int iommu_set_nesting_vmid(struct iommu_domain *domain, u32 vmid)
+{
+ if (domain->type != IOMMU_DOMAIN_UNMANAGED)
+ return -EINVAL;
+ if (!domain->ops->set_nesting_vmid)
+ return -EINVAL;
+ return domain->ops->set_nesting_vmid(domain, vmid);
+}
+EXPORT_SYMBOL_GPL(iommu_set_nesting_vmid);
+
+int iommu_get_nesting_vmid(struct iommu_domain *domain, u32 *vmid)
+{
+ if (domain->type != IOMMU_DOMAIN_UNMANAGED)
+ return -EINVAL;
+ if (!domain->ops->get_nesting_vmid)
+ return -EINVAL;
+ return domain->ops->get_nesting_vmid(domain, vmid);
+}
+EXPORT_SYMBOL_GPL(iommu_get_nesting_vmid);
+
int iommu_set_pgtable_quirks(struct iommu_domain *domain,
unsigned long quirk)
{
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index d2f3435e7d17..bda6b3450909 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -163,6 +163,7 @@ enum iommu_dev_features {
};

#define IOMMU_PASID_INVALID (-1U)
+#define IOMMU_VMID_INVALID (-1U)

#ifdef CONFIG_IOMMU_API

@@ -269,6 +270,8 @@ struct iommu_ops {
void (*probe_finalize)(struct device *dev);
struct iommu_group *(*device_group)(struct device *dev);
int (*enable_nesting)(struct iommu_domain *domain);
+ int (*set_nesting_vmid)(struct iommu_domain *domain, u32 vmid);
+ int (*get_nesting_vmid)(struct iommu_domain *domain, u32 *vmid);
int (*set_pgtable_quirks)(struct iommu_domain *domain,
unsigned long quirks);

@@ -500,6 +503,8 @@ extern int iommu_group_id(struct iommu_group *group);
extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);

int iommu_enable_nesting(struct iommu_domain *domain);
+int iommu_set_nesting_vmid(struct iommu_domain *domain, u32 vmid);
+int iommu_get_nesting_vmid(struct iommu_domain *domain, u32 *vmid);
int iommu_set_pgtable_quirks(struct iommu_domain *domain,
unsigned long quirks);

--
2.17.1