From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
To: <linux-kernel@vger.kernel.org>, <iommu@lists.linux.dev>,
<joro@8bytes.org>, <jgg@nvidia.com>
Cc: <yi.l.liu@intel.com>, <kevin.tian@intel.com>,
<nicolinc@nvidia.com>, <vasant.hegde@amd.com>,
<jon.grimm@amd.com>, <santosh.shukla@amd.com>, <sairaj.k@amd.com>,
<jay.chen@amd.com>, <wvw@google.com>, <wnliu@google.com>,
<dantuluris@google.com>, <chriscli@google.com>,
<kpsingh@google.com>,
"Suravee Suthikulpanit" <suravee.suthikulpanit@amd.com>
Subject: [PATCH 1/2] iommufd: add hw_queue_init for guest-address queue setup
Date: Wed, 8 Jul 2026 01:56:27 +0000 [thread overview]
Message-ID: <20260708015628.255088-2-suravee.suthikulpanit@amd.com> (raw)
In-Reply-To: <20260708015628.255088-1-suravee.suthikulpanit@amd.com>
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.
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
drivers/iommu/iommufd/viommu.c | 94 ++++++++++++++++++++--------------
include/linux/iommufd.h | 5 +-
2 files changed, 59 insertions(+), 40 deletions(-)
diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c
index 4081deda9b33..b278cbde360e 100644
--- a/drivers/iommu/iommufd/viommu.c
+++ b/drivers/iommu/iommufd/viommu.c
@@ -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:
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 6e7efe83bc5d..5285d96f1d8c 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -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)
--
2.34.1
next prev parent reply other threads:[~2026-07-08 1:57 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-08 1:56 [PATCH 0/2] iommufd: Add AMD vIOMMU HW queue support Suravee Suthikulpanit
2026-07-08 1:56 ` Suravee Suthikulpanit [this message]
2026-07-08 2:39 ` [PATCH 1/2] iommufd: add hw_queue_init for guest-address queue setup Nicolin Chen
2026-07-08 1:56 ` [PATCH 2/2] iommu/amd: add vIOMMU HW queue initialization Suravee Suthikulpanit
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260708015628.255088-2-suravee.suthikulpanit@amd.com \
--to=suravee.suthikulpanit@amd.com \
--cc=chriscli@google.com \
--cc=dantuluris@google.com \
--cc=iommu@lists.linux.dev \
--cc=jay.chen@amd.com \
--cc=jgg@nvidia.com \
--cc=jon.grimm@amd.com \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=kpsingh@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=nicolinc@nvidia.com \
--cc=sairaj.k@amd.com \
--cc=santosh.shukla@amd.com \
--cc=vasant.hegde@amd.com \
--cc=wnliu@google.com \
--cc=wvw@google.com \
--cc=yi.l.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®