Add IOMMUFD_OBJ_HW_QUEUE with an iommufd_hw_queue structure, representing
a HW-accelerated queue type of IOMMU's physical queue that can be passed
through to a user space VM for direct hardware control, such as:
 - NVIDIA's Virtual Command Queue
 - AMD vIOMMU's Command Buffer, Event Log Buffer, and PPR Log Buffer

Introduce an allocator iommufd_hw_queue_alloc(). And add a pair of viommu
ops for iommufd to forward user space ioctls to IOMMU drivers.

Given that the first user of this HW QUEUE (tegra241-cmdqv) will need to
ensure the queue memory to be physically contiguous, add a flag property
in iommufd_viommu_ops and IOMMUFD_VIOMMU_FLAG_HW_QUEUE_READS_PA to allow
driver to flag it so that the core will validate the physical pages of a
given guest queue.

Reviewed-by: Lu Baolu <baolu...@linux.intel.com>
Reviewed-by: Pranjal Shrivastava <pr...@google.com>
Reviewed-by: Vasant Hegde <vasant.he...@amd.com>
Signed-off-by: Nicolin Chen <nicol...@nvidia.com>
---
 include/linux/iommufd.h | 45 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index f6f333eaaae3..0b5bea7286b8 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_HW_QUEUE,
 #ifdef CONFIG_IOMMUFD_TEST
        IOMMUFD_OBJ_SELFTEST,
 #endif
@@ -112,6 +113,18 @@ struct iommufd_vdevice {
        u64 id; /* per-vIOMMU virtual ID */
 };
 
+struct iommufd_hw_queue {
+       struct iommufd_object obj;
+       struct iommufd_ctx *ictx;
+       struct iommufd_viommu *viommu;
+       u64 base_addr; /* in guest physical address space */
+       size_t length;
+};
+
+enum iommufd_viommu_flags {
+       IOMMUFD_VIOMMU_FLAG_HW_QUEUE_READS_PA = 1 << 0,
+};
+
 /**
  * struct iommufd_viommu_ops - vIOMMU specific operations
  * @destroy: Clean up all driver-specific parts of an iommufd_viommu. The 
memory
@@ -137,8 +150,18 @@ 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
+ * @hw_queue_alloc: Allocate a HW QUEUE object for a HW-accelerated queue given
+ *                  the @type (must be defined in include/uapi/linux/iommufd.h)
+ *                  for the @viommu. @index carries the logical HW QUEUE ID per
+ *                  @viommu in a guest VM, for a multi-queue case; @addr 
carries
+ *                  the guest physical base address of the queue memory; 
@length
+ *                  carries the size of the queue
+ * @hw_queue_destroy: Clean up all driver-specific parts of an 
iommufd_hw_queue.
+ *                    The memory of the HW QUEUE will be free-ed by iommufd 
core
+ *                    after calling this op
  */
 struct iommufd_viommu_ops {
+       u32 flags;
        void (*destroy)(struct iommufd_viommu *viommu);
        struct iommu_domain *(*alloc_domain_nested)(
                struct iommufd_viommu *viommu, u32 flags,
@@ -149,6 +172,10 @@ struct iommufd_viommu_ops {
                                                 struct device *dev,
                                                 u64 virt_id);
        void (*vdevice_destroy)(struct iommufd_vdevice *vdev);
+       struct iommufd_hw_queue *(*hw_queue_alloc)(
+               struct iommufd_viommu *viommu, unsigned int type, u32 index,
+               u64 base_addr, size_t length);
+       void (*hw_queue_destroy)(struct iommufd_hw_queue *hw_queue);
 };
 
 #if IS_ENABLED(CONFIG_IOMMUFD)
@@ -292,6 +319,24 @@ static inline int iommufd_viommu_report_event(struct 
iommufd_viommu *viommu,
                ret;                                                           \
        })
 
+#define iommufd_hw_queue_alloc(viommu, drv_struct, member)                     
\
+       ({                                                                     \
+               drv_struct *ret;                                               \
+                                                                              \
+               static_assert(__same_type(struct iommufd_viommu, *viommu));    \
+               static_assert(__same_type(struct iommufd_hw_queue,             \
+                                         ((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_HW_QUEUE);                                 \
+               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


Reply via email to