[PATCH v2 15/19] iommufd/viommu: Add iommufd_viommu_to_parent_domain helper

Nicolin Chen
2024-08-27T17:17:37Z

Allow an IOMMU driver to convert a core-managed viommu to a nested parent
domain for the info that the domain holds.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommufd/viommu_api.c | 14 ++++++++++++++
include/linux/iommufd.h | 8 ++++++++
2 files changed, 22 insertions(+)

diff --git a/drivers/iommu/iommufd/viommu_api.c b/drivers/iommu/iommufd/viommu_api.c
index e0ee592ce834..3772a5892a6c 100644
--- a/drivers/iommu/iommufd/viommu_api.c
+++ b/drivers/iommu/iommufd/viommu_api.c
@@ -37,3 +37,17 @@ struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id)
return vdev_id->idev->dev;
}
EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_device, IOMMUFD);
+
+/*
+ * Convert a viommu to its encapsulated nest parent domain. Caller must be aware
+ * of the lifecycle of the viommu pointer. Only call this function in a callback
+ * function where viommu is passed in by the iommu/iommufd core.
+ */
+struct iommu_domain *
+iommufd_viommu_to_parent_domain(struct iommufd_viommu *viommu)
+{
+ if (!viommu || !viommu->hwpt)
+ return NULL;
+ return viommu->hwpt->common.domain;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_viommu_to_parent_domain, IOMMUFD);
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 364f151d281d..f7c265c6de7c 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -92,6 +92,8 @@ int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx);
void iommufd_viommu_lock_vdev_id(struct iommufd_viommu *viommu);
void iommufd_viommu_unlock_vdev_id(struct iommufd_viommu *viommu);
struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id);
+struct iommu_domain *
+iommufd_viommu_to_parent_domain(struct iommufd_viommu *viommu);
#else /* !CONFIG_IOMMUFD */
static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file)
{
@@ -145,5 +147,11 @@ struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id)
{
return NULL;
}
+
+static inline struct iommu_domain *
+iommufd_viommu_to_parent_domain(struct iommufd_viommu *viommu)
+{
+ return NULL;
+}
#endif /* CONFIG_IOMMUFD */
#endif
--
2.43.0