iommufd: Add AMD vIOMMU HW queue support
Subsystems (3)
Suravee Suthikulpanit
reviewercover iommufd: Add AMD vIOMMU HW queue support
Notes:
* This series depends on the "[PATCH v3 00/22] iommu/amd: Introduce AMD Hardware-accelerated Virtualized IOMMU (vIOMMU) Support"
(https://lore.kernel.org/linux-iommu/[email protected]/)
* Previously, this series was part of the AMD HW vIOMMU series, and now is being split into its own series to make it easier to review.
Overview:
AMD HW-accelerated Virtualized IOMMU (vIOMMU) guests own command, event, and PPR queues in guest physical address space. The existing iommufd HW queue path pins host pages and passes a physical address to the driver via hw_queue_init_phys(). vIOMMU hardware instead consumes guest queue GPAs supplied by userspace through IOMMU_HW_QUEUE_ALLOC.
This series adds a guest-address initialization path in the iommufd core and wires AMD vIOMMU VFCTRL programming for command, event, and PPR queues.
Patch 1 factors HW queue allocation into _iommufd_hw_queue_init() and introduces hw_queue_init in iommufd_viommu_ops. Drivers that implement hw_queue_init receive the queue GPA in hw_queue->base_addr without the core pinning host backing pages.
Patch 2 extends the IOMMU_HW_QUEUE_ALLOC UAPI with AMD queue types and per-queue enable flags, stores flags on iommufd_hw_queue, and programs guest command, event, and PPR control registers in VFCTRL during _amd_viommu_hw_queue_init().
* In this version, the CommandBuf, EventLog, and PPRLog are expected to be allocated when the corresponding enable bits in the guest IOMMU Control MMIO register are set, which is different from the previous version where the HW queue is allocated when the buffers base-address/size are programmed.
Thanks,
Suravee
Suravee Suthikulpanit (2):
iommufd: add hw_queue_init for guest-address queue setup iommu/amd: add vIOMMU HW queue initialization
drivers/iommu/amd/amd_iommu_types.h | 8 ++ drivers/iommu/amd/iommufd.c | 129 ++++++++++++++++++++++++++++ drivers/iommu/iommufd/viommu.c | 95 +++++++++++--------- include/linux/iommufd.h | 6 +- include/uapi/linux/iommufd.h | 31 ++++++- 5 files changed, 228 insertions(+), 41 deletions(-)
Signature · 1 line
2.34.1
Suravee Suthikulpanit
reviewerPATCH 1/2 iommufd: add hw_queue_init for guest-address queue setup
Factor HW queue allocation into _iommufd_hw_queue_init() and allow drivers to provide hw_queue_init when the device uses guest physical addresses instead of pinned host pages.
--- drivers/iommu/iommufd/viommu.c | 94 ++++++++++++++++++++-------------- include/linux/iommufd.h | 5 +- 2 files changed, 59 insertions(+), 40 deletions(-)
drivers/iommu/iommufd/viommu.c +55 -39
@@ -353,61 +353,39 @@ iommufd_hw_queue_alloc_phys(struct iommu_hw_queue_alloc *cmd,return ERR_PTR (rc); } -int iommufd_hw_queue_alloc_ioctl (struct iommufd_ucmd *ucmd) +static int _iommufd_hw_queue_init (struct iommufd_ucmd *ucmd, +struct iommufd_viommu *viommu) {struct iommu_hw_queue_alloc *cmd = ucmd->cmd;struct iommufd_hw_queue *hw_queue; -struct iommufd_viommu *viommu; -struct iommufd_access *access; +struct iommufd_access *access =NULL ; +phys_addr_t base_pa =0 ;size_t hw_queue_size; -phys_addr_t base_pa; -u64 last;int rc; -if (cmd->flags || cmd->type ==IOMMU_HW_QUEUE_TYPE_DEFAULT ) +if (!viommu->ops->hw_queue_init_phys && !viommu->ops->hw_queue_init)return -EOPNOTSUPP ; -if (!cmd->length) -return -EINVAL ; -if (check_add_overflow (cmd->nesting_parent_iova, cmd->length -1 , - &last)) -return -EOVERFLOW ; - - viommu =iommufd_get_viommu (ucmd, cmd->viommu_id); -if (IS_ERR (viommu)) -return PTR_ERR (viommu); - -if (!viommu->ops || !viommu->ops->get_hw_queue_size || - !viommu->ops->hw_queue_init_phys) { - rc = -EOPNOTSUPP ; - goto out_put_viommu; - } hw_queue_size = viommu->ops->get_hw_queue_size (viommu, cmd->type); -if (!hw_queue_size) { - rc = -EOPNOTSUPP ; - goto out_put_viommu; - } +if (!hw_queue_size) +return -EOPNOTSUPP ;/* * It is a driver bug for providing a hw_queue_size smaller than the * core HW queue structure size */ -if (WARN_ON_ONCE (hw_queue_size <sizeof (*hw_queue))) { - rc = -EOPNOTSUPP ; - goto out_put_viommu; - } +if (WARN_ON_ONCE (hw_queue_size <sizeof (*hw_queue))) +return -EOPNOTSUPP ; hw_queue = (struct iommufd_hw_queue *)_iommufd_object_alloc_ucmd ( ucmd, hw_queue_size,IOMMUFD_OBJ_HW_QUEUE ); -if (IS_ERR (hw_queue)) { - rc =PTR_ERR (hw_queue); - goto out_put_viommu; - } +if (IS_ERR (hw_queue)) +return PTR_ERR (hw_queue); - access =iommufd_hw_queue_alloc_phys (cmd, viommu, &base_pa); -if (IS_ERR (access)) { - rc =PTR_ERR (access); - goto out_put_viommu; +if (viommu->ops->hw_queue_init_phys) { + access =iommufd_hw_queue_alloc_phys (cmd, viommu, &base_pa); +if (IS_ERR (access)) +return PTR_ERR (access); } hw_queue->viommu = viommu;
@@ -417,11 +395,49 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd) hw_queue->length = cmd->length; hw_queue->base_addr = cmd->nesting_parent_iova; - rc = viommu->ops->hw_queue_init_phys (hw_queue, cmd->index, base_pa); +if (viommu->ops->hw_queue_init_phys) + rc = viommu->ops->hw_queue_init_phys (hw_queue, cmd->index, + base_pa); +else if (viommu->ops->hw_queue_init) + rc = viommu->ops->hw_queue_init (hw_queue, cmd->index); +else +return -EOPNOTSUPP ; +if (rc) - goto out_put_viommu; +return rc; cmd->out_hw_queue_id = hw_queue->obj.id; +return 0 ; +} + +int iommufd_hw_queue_alloc_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_hw_queue_alloc *cmd = ucmd->cmd; +struct iommufd_viommu *viommu; +u64 last; +int rc; + +if (cmd->flags || cmd->type ==IOMMU_HW_QUEUE_TYPE_DEFAULT ) +return -EOPNOTSUPP ; +if (!cmd->length) +return -EINVAL ; +if (check_add_overflow (cmd->nesting_parent_iova, cmd->length -1 , + &last)) +return -EOVERFLOW ; + + viommu =iommufd_get_viommu (ucmd, cmd->viommu_id); +if (IS_ERR (viommu)) +return PTR_ERR (viommu); + +if (!viommu->ops || !viommu->ops->get_hw_queue_size) { + rc = -EOPNOTSUPP ; + goto out_put_viommu; + } + + rc =_iommufd_hw_queue_init (ucmd, viommu); +if (rc) + goto out_put_viommu; + rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); out_put_viommu:
include/linux/iommufd.h +4 -1
@@ -180,6 +180,9 @@ struct iommufd_hw_queue {* the physical location of the guest queue * If driver has a deinit function to revert what this op * does, it should set it to the @hw_queue->destroy pointer +* @hw_queue_init: Similar to hw_queue_init_phys, but driver providing this op +* indicates that HW accesses the guest queue memory via +* @hw_queue->base_addr. */ struct iommufd_viommu_ops {void (*destroy)(struct iommufd_viommu *viommu);
@@ -192,9 +195,9 @@ struct iommufd_viommu_ops {int (*vdevice_init)(struct iommufd_vdevice *vdev);size_t (*get_hw_queue_size)(struct iommufd_viommu *viommu,enum iommu_hw_queue_type queue_type); -/* AMD's HW will add hw_queue_init simply using @hw_queue->base_addr */ int (*hw_queue_init_phys)(struct iommufd_hw_queue *hw_queue,u32 index,phys_addr_t base_addr_pa); +int (*hw_queue_init)(struct iommufd_hw_queue *hw_queue,u32 index); };#if IS_ENABLED (CONFIG_IOMMUFD )
Signature · 1 line
2.34.1
Re: [PATCH 1/2] iommufd: add hw_queue_init for guest-address queue setup
Quoted 1 line · Suravee Suthikulpanit
Factor HW queue allocation into
_iommufd_hw_queue_init()
Why do we need this factor? There is only one call site. Nothing really benefits from this _iommufd_hw_queue_init(). Can we unwrap it and keep everything in one function?
It could be a smaller patch, as I recall I suggested previously..
Nicolin
Suravee Suthikulpanit
reviewerPATCH 2/2 iommu/amd: add vIOMMU HW queue initialization
In reply to Suravee SuthikulpanitPlumb HW queue flags through IOMMU_HW_QUEUE_ALLOC and add AMD HW queue types and flags to the UAPI. Program VFCTRL guest command, event, and PPR queue base addresses and lengths during hw_queue_init.
--- drivers/iommu/amd/amd_iommu_types.h | 8 ++ drivers/iommu/amd/iommufd.c | 129 ++++++++++++++++++++++++++++ drivers/iommu/iommufd/viommu.c | 3 +- include/linux/iommufd.h | 1 + include/uapi/linux/iommufd.h | 31 ++++++- 5 files changed, 170 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +8 -0
@@ -507,6 +507,10 @@ extern bool amdr_ivrs_remap_support;#define VIOMMU_VFCTRL_MMIO_BASE (iommu, guestId) \ (iommu->vfctrl_base + (guestId * VIOMMU_VFCTRL_MMIO_ENTRY_SIZE)) +#define VIOMMU_VFCTRL_MMIO_GUEST_COMMAND_CONTROL_OFFSET 0x20 +#define VIOMMU_VFCTRL_MMIO_GUEST_EVENT_CONTROL_OFFSET 0x28 +#define VIOMMU_VFCTRL_MMIO_GUEST_PPR_CONTROL_OFFSET 0x30 +struct amd_iommu ;struct iommu_domain ;struct irq_domain ;
@@ -1153,6 +1157,10 @@ struct amd_iommu_vdevice {struct iommufd_vdevice core; }; +struct amd_iommu_hw_queue { +struct iommufd_hw_queue core; +}; +#ifdef CONFIG_IRQ_REMAP extern struct amd_irte_ops irte_32_ops;extern struct amd_irte_ops irte_128_ops;
drivers/iommu/amd/iommufd.c +129 -0
145 lines not on this page · raw message
drivers/iommu/iommufd/viommu.c +2 -1
@@ -392,6 +392,7 @@ static int _iommufd_hw_queue_init(struct iommufd_ucmd *ucmd,refcount_inc (&viommu->obj.users); hw_queue->access = access; hw_queue->type = cmd->type; + hw_queue->flags = cmd->flags; hw_queue->length = cmd->length; hw_queue->base_addr = cmd->nesting_parent_iova;
@@ -417,7 +418,7 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd)u64 last;int rc; -if (cmd->flags || cmd->type ==IOMMU_HW_QUEUE_TYPE_DEFAULT ) +if (cmd->type ==IOMMU_HW_QUEUE_TYPE_DEFAULT )return -EOPNOTSUPP ;if (!cmd->length)return -EINVAL ;
include/linux/iommufd.h +1 -0
@@ -135,6 +135,7 @@ struct iommufd_hw_queue {size_t length;enum iommu_hw_queue_type type; +u32 flags;/* Clean up all driver-specific parts of an iommufd_hw_queue */ void (*destroy)(struct iommufd_hw_queue *hw_queue);
include/uapi/linux/iommufd.h +30 -1
@@ -1322,12 +1322,41 @@ enum iommu_hw_queue_type {* emulated vSMMU's IDR1.CMDQS to log2(huge page size / 16 bytes) */ IOMMU_HW_QUEUE_TYPE_TEGRA241_CMDQV =1 , +IOMMU_HW_QUEUE_TYPE_AMD_CMD , +IOMMU_HW_QUEUE_TYPE_AMD_EVT , + IOMMU_HW_QUEUE_TYPE_AMD_PPR, +}; + +/** +* enum iommu_hw_queue_flags_amd - AMD HW Queue Flags +* @IOMMU_HW_QUEUE_FLAG_AMD_CMDBUF_EN : Command buffer enable +* @IOMMU_HW_QUEUE_FLAG_AMD_COMWAIT_EN : Command wait enable +* @IOMMU_HW_QUEUE_FLAG_AMD_EVT_LOG_EN : Event log enable +* @IOMMU_HW_QUEUE_FLAG_AMD_EVT_INT_EN : Event interrupt enable +* @IOMMU_HW_QUEUE_FLAG_AMD_PPRLOG_EN : PPR log enable +* @IOMMU_HW_QUEUE_FLAG_AMD_PPRINT_EN : PPR print enable +* @IOMMU_HW_QUEUE_FLAG_AMD_PPR_EN : PPR enable +* @IOMMU_HW_QUEUE_FLAG_AMD_PPR_AUTO_RSP_EN : PPR auto response enable +* @IOMMU_HW_QUEUE_FLAG_AMD_BLKSTOPMRK_EN : Block stop mark enable +* @IOMMU_HW_QUEUE_FLAG_AMD_PPR_AUTO_RSP_AON : PPR auto response on next enable +*/ +enum iommu_hw_queue_flags_amd { +IOMMU_HW_QUEUE_FLAG_AMD_CMDBUF_EN =1 <<0 , +IOMMU_HW_QUEUE_FLAG_AMD_COMWAIT_EN =1 <<1 , +IOMMU_HW_QUEUE_FLAG_AMD_EVT_LOG_EN =1 <<2 , +IOMMU_HW_QUEUE_FLAG_AMD_EVT_INT_EN =1 <<3 , +IOMMU_HW_QUEUE_FLAG_AMD_PPRLOG_EN =1 <<4 , +IOMMU_HW_QUEUE_FLAG_AMD_PPRINT_EN =1 <<5 , +IOMMU_HW_QUEUE_FLAG_AMD_PPR_EN =1 <<6 , +IOMMU_HW_QUEUE_FLAG_AMD_PPR_AUTO_RSP_EN =1 <<7 , +IOMMU_HW_QUEUE_FLAG_AMD_BLKSTOPMRK_EN =1 <<8 , +IOMMU_HW_QUEUE_FLAG_AMD_PPR_AUTO_RSP_AON =1 <<9 , };/** * struct iommu_hw_queue_alloc - ioctl(IOMMU_HW_QUEUE_ALLOC) * @size: sizeof(struct iommu_hw_queue_alloc) -* @flags: Must be 0 +* @flags: HW queue flags based on enum iommu_hw_queue_type * @viommu_id: Virtual IOMMU ID to associate the HW queue with * @type: One of enum iommu_hw_queue_type * @index: The logical index to the HW queue per virtual IOMMU for a multi-queue
Signature · 1 line
2.34.1