[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <3ab48d4978318938911d91833654b296947668ad.1724777091.git.nicolinc@nvidia.com>
Date: Tue, 27 Aug 2024 10:02:06 -0700
From: Nicolin Chen <nicolinc@...dia.com>
To: <jgg@...dia.com>, <kevin.tian@...el.com>, <will@...nel.org>
CC: <joro@...tes.org>, <suravee.suthikulpanit@....com>,
<robin.murphy@....com>, <dwmw2@...radead.org>, <baolu.lu@...ux.intel.com>,
<shuah@...nel.org>, <linux-kernel@...r.kernel.org>, <iommu@...ts.linux.dev>,
<linux-arm-kernel@...ts.infradead.org>, <linux-kselftest@...r.kernel.org>,
<eric.auger@...hat.com>, <jean-philippe@...aro.org>, <mdf@...nel.org>,
<mshavit@...gle.com>, <shameerali.kolothum.thodi@...wei.com>,
<smostafa@...gle.com>, <yi.l.liu@...el.com>
Subject: [PATCH v1 04/10] iommufd/viommu: Allow drivers to control vdev_id lifecycle
The iommufd core provides a lookup helper for an IOMMU driver to find a
device pointer by device's per-viommu virtual ID. Yet a driver may need
an inverted lookup to find a device's per-viommu virtual ID by a device
pointer, e.g. when reporting virtual IRQs/events back to the user space.
In this case, it'd be unsafe for iommufd core to do an inverted lookup,
as the driver can't track the lifecycle of a viommu object or a vdev_id
object.
Meanwhile, some HW can even support virtual device ID lookup by its HW-
accelerated virtualization feature. E.g. Tegra241 CMDQV HW supports to
execute vanilla guest-issued SMMU commands containing virtual Stream ID
but requires software to configure a link between virtual Stream ID and
physical Stream ID via HW registers. So not only the iommufd core needs
a vdev_id lookup table, drivers will want one too.
Given the two justifications above, it's the best practice to provide a
a pair of set_vdev_id/unset_vdev_id ops in the viommu ops, so a driver
can implement them to control a vdev_id's lifecycle, and configure the
HW properly if required.
Signed-off-by: Nicolin Chen <nicolinc@...dia.com>
---
drivers/iommu/iommufd/device.c | 2 ++
drivers/iommu/iommufd/iommufd_private.h | 6 ------
drivers/iommu/iommufd/viommu.c | 23 +++++++++++++++++++----
include/linux/iommufd.h | 13 +++++++++++++
4 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 3ad759971b32..01bb5c9f415b 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -145,6 +145,8 @@ void iommufd_device_destroy(struct iommufd_object *obj)
old = xa_cmpxchg(&viommu->vdev_ids, vdev_id->id, vdev_id, NULL,
GFP_KERNEL);
WARN_ON(old != vdev_id);
+ if (vdev_id->viommu->ops && vdev_id->viommu->ops->unset_vdev_id)
+ vdev_id->viommu->ops->unset_vdev_id(vdev_id);
kfree(vdev_id);
idev->vdev_id = NULL;
}
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index be1f1813672e..4cb1555991b8 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -621,12 +621,6 @@ struct iommufd_viommu {
unsigned int type;
};
-struct iommufd_vdev_id {
- struct iommufd_viommu *viommu;
- struct iommufd_device *idev;
- u64 id;
-};
-
static inline struct iommufd_viommu *
iommufd_get_viommu(struct iommufd_ucmd *ucmd, u32 id)
{
diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
index 9adc9c62ada9..b1eb900b7fbf 100644
--- a/drivers/iommu/iommufd/viommu.c
+++ b/drivers/iommu/iommufd/viommu.c
@@ -13,6 +13,8 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
xa_for_each(&viommu->vdev_ids, index, vdev_id) {
/* Unlocked since there should be no race in a destroy() */
+ if (viommu->ops && viommu->ops->unset_vdev_id)
+ viommu->ops->unset_vdev_id(vdev_id);
vdev_id->idev->vdev_id = NULL;
kfree(vdev_id);
}
@@ -116,10 +118,18 @@ int iommufd_viommu_set_vdev_id(struct iommufd_ucmd *ucmd)
goto out_unlock_igroup;
}
- vdev_id = kzalloc(sizeof(*vdev_id), GFP_KERNEL);
- if (!vdev_id) {
- rc = -ENOMEM;
- goto out_unlock_igroup;
+ if (viommu->ops && viommu->ops->set_vdev_id) {
+ vdev_id = viommu->ops->set_vdev_id(viommu, idev->dev, cmd->vdev_id);
+ if (IS_ERR(vdev_id)) {
+ rc = PTR_ERR(vdev_id);
+ goto out_unlock_igroup;
+ }
+ } else {
+ vdev_id = kzalloc(sizeof(*vdev_id), GFP_KERNEL);
+ if (!vdev_id) {
+ rc = -ENOMEM;
+ goto out_unlock_igroup;
+ }
}
vdev_id->idev = idev;
@@ -137,6 +147,8 @@ int iommufd_viommu_set_vdev_id(struct iommufd_ucmd *ucmd)
goto out_unlock_igroup;
out_free:
+ if (viommu->ops && viommu->ops->unset_vdev_id)
+ viommu->ops->unset_vdev_id(vdev_id);
kfree(vdev_id);
out_unlock_igroup:
mutex_unlock(&idev->igroup->lock);
@@ -185,6 +197,9 @@ int iommufd_viommu_unset_vdev_id(struct iommufd_ucmd *ucmd)
rc = xa_err(old);
goto out_unlock_igroup;
}
+
+ if (viommu->ops && viommu->ops->unset_vdev_id)
+ viommu->ops->unset_vdev_id(old);
kfree(old);
idev->vdev_id = NULL;
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index f7c265c6de7c..c89583c7c792 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -56,8 +56,18 @@ void iommufd_access_detach(struct iommufd_access *access);
void iommufd_ctx_get(struct iommufd_ctx *ictx);
+struct iommufd_vdev_id {
+ struct iommufd_viommu *viommu;
+ struct iommufd_device *idev;
+ u64 id;
+};
+
/**
* struct iommufd_viommu_ops - viommu specific operations
+ * @set_vdev_id: Set a virtual device id for a device assigned to a viommu.
+ * Driver allocates an iommufd_vdev_id and return its pointer.
+ * @unset_vdev_id: Unset a virtual device id for a device assigned to a viommu.
+ * iommufd core frees the memory pointed by an iommufd_vdev_id.
* @cache_invalidate: Flush hardware cache used by a viommu. It can be used for
* any IOMMU hardware specific cache as long as a viommu has
* enough information to identify it: for example, a VMID or
@@ -69,6 +79,9 @@ void iommufd_ctx_get(struct iommufd_ctx *ictx);
* include/uapi/linux/iommufd.h
*/
struct iommufd_viommu_ops {
+ struct iommufd_vdev_id *(*set_vdev_id)(struct iommufd_viommu *viommu,
+ struct device *dev, u64 id);
+ void (*unset_vdev_id)(struct iommufd_vdev_id *vdev_id);
int (*cache_invalidate)(struct iommufd_viommu *viommu,
struct iommu_user_data_array *array);
};
--
2.43.0
Powered by blists - more mailing lists