[PATCH v3 10/23] iommufd/viommu: Introduce IOMMUFD_OBJ_VQUEUE and its related struct

Nicolin Chen
2025-05-01T23:55:38Z

Add a new IOMMUFD_OBJ_VQUEUE with an iommufd_vqueue structure representing
a HW-accelerated queue type of physical IOMMU's passed to a user space VM.
This vQUEUE object, is a subset of vIOMMU resources of a physical IOMMU's,
such as:
- NVIDIA's Virtual Command Queue
- AMD vIOMMU's Command Buffer, Event Log Buffer, and PPR Log Buffer

Introduce a struct iommufd_vqueue and an allocator iommufd_vqueue_alloc().
Also, add a pair of viommu ops for iommufd to forward user space ioctls to
IOMMU drivers.

Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
include/linux/iommufd.h | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)

diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index f6f333eaaae3..676b54c5cf53 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -37,6 +37,7 @@ enum iommufd_object_type {
IOMMUFD_OBJ_VIOMMU,
IOMMUFD_OBJ_VDEVICE,
IOMMUFD_OBJ_VEVENTQ,
+ IOMMUFD_OBJ_VQUEUE,
#ifdef CONFIG_IOMMUFD_TEST
IOMMUFD_OBJ_SELFTEST,
#endif
@@ -112,6 +113,14 @@ struct iommufd_vdevice {
u64 id; /* per-vIOMMU virtual ID */
};

+struct iommufd_vqueue {
+ struct iommufd_object obj;
+ struct iommufd_ctx *ictx;
+ struct iommufd_viommu *viommu;
+ dma_addr_t addr; /* in guest physical address space */
+ size_t length;
+};
+
/**
* struct iommufd_viommu_ops - vIOMMU specific operations
* @destroy: Clean up all driver-specific parts of an iommufd_viommu. The memory
@@ -137,6 +146,14 @@ struct iommufd_vdevice {
* @vdevice_destroy: Clean up all driver-specific parts of an iommufd_vdevice.
* The memory of the vDEVICE will be free-ed by iommufd core
* after calling this op
+ * @vqueue_alloc: Allocate a vQUEUE object for a HW-accelerated queue given the
+ * @type (must be defined in include/uapi/linux/iommufd.h) for a
+ * @viommu. @index carries the logical vQUEUE ID per @viommu in a
+ * guest VM; @addr carries the guest physical base address of the
+ * queue memory; @length carries the size of the guest queue
+ * @vqueue_destroy: Clean up all driver-specific parts of an iommufd_vqueue. The
+ * memory of the iommufd_vqueue will be free-ed by iommufd core
+ * after calling this op
*/
struct iommufd_viommu_ops {
void (*destroy)(struct iommufd_viommu *viommu);
@@ -149,6 +166,10 @@ struct iommufd_viommu_ops {
struct device *dev,
u64 virt_id);
void (*vdevice_destroy)(struct iommufd_vdevice *vdev);
+ struct iommufd_vqueue *(*vqueue_alloc)(struct iommufd_viommu *viommu,
+ unsigned int type, u32 index,
+ dma_addr_t addr, size_t length);
+ void (*vqueue_destroy)(struct iommufd_vqueue *vqueue);
};

#if IS_ENABLED(CONFIG_IOMMUFD)
@@ -292,6 +313,23 @@ static inline int iommufd_viommu_report_event(struct iommufd_viommu *viommu,
ret; \
})

+#define iommufd_vqueue_alloc(viommu, drv_struct, member) \
+ ({ \
+ drv_struct *ret; \
+ \
+ static_assert(__same_type(struct iommufd_viommu, *viommu)); \
+ static_assert(__same_type(struct iommufd_vqueue, \
+ ((drv_struct *)NULL)->member)); \
+ static_assert(offsetof(drv_struct, member.obj) == 0); \
+ ret = (drv_struct *)_iommufd_object_alloc( \
+ viommu->ictx, sizeof(drv_struct), IOMMUFD_OBJ_VQUEUE); \
+ if (!IS_ERR(ret)) { \
+ ret->member.viommu = viommu; \
+ ret->member.ictx = viommu->ictx; \
+ } \
+ ret; \
+ })
+
/* Helper for IOMMU driver to destroy structures created by allocators above */
#define iommufd_struct_destroy(drv_struct, member) \
({ \
--
2.43.0