mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wei Huang <wei.huang2@amd.com>
To: <dmaengine@vger.kernel.org>
Cc: <vkoul@kernel.org>, <Frank.Li@kernel.org>, <shuah@kernel.org>,
	<nathan.lynch@amd.com>, <wei.huang2@amd.com>,
	<mario.limonciello@amd.com>, <Stephen.Bates@amd.com>,
	<tycho@kernel.org>, <suravee.suthikulpanit@amd.com>,
	<rientjes@google.com>, <John.Kariuki@amd.com>, <jic23@kernel.org>,
	<weixugc@google.com>, <bhelgaas@google.com>,
	<Shyam.Iyer@dell.com>, <linux-kernel@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <linux-pci@vger.kernel.org>
Subject: [PATCH v1 18/44] dmaengine: sdxi: implement userspace context file operations
Date: Thu, 19 Nov 2026 14:22:54 -0600	[thread overview]
Message-ID: <20261119202321.1734355-19-wei.huang2@amd.com> (raw)
In-Reply-To: <20261119202321.1734355-1-wei.huang2@amd.com>

Implement user-space context creation, same-mm context fds, quiesce,
eventfd binding, poll, and token-validated doorbell mappings. Let PCI
perform the final BAR remap checking against doorbell resource bounds.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/context.c |  45 ++++-
 drivers/dma/sdxi/context.h |   4 +
 drivers/dma/sdxi/device.c  |   3 +
 drivers/dma/sdxi/ioctl.c   | 396 ++++++++++++++++++++++++++++++++++++-
 drivers/dma/sdxi/pci.c     |  27 +++
 drivers/dma/sdxi/sdxi.h    |   8 +
 6 files changed, 475 insertions(+), 8 deletions(-)

diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index a4976f2b5e53..cdaa0f654429 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -15,8 +15,10 @@
 #include <linux/dma-mapping.h>
 #include <linux/dmapool.h>
 #include <linux/errno.h>
+#include <linux/fs.h>
 #include <linux/idr.h>
 #include <linux/iommu.h>
+#include <linux/mm.h>
 #include <linux/overflow.h>
 #include <linux/poll.h>
 #include <linux/slab.h>
@@ -73,6 +75,8 @@ static void sdxi_free_cxt(struct sdxi_cxt *cxt)
 	ida_destroy(&cxt->akey_ida);
 	mutex_destroy(&cxt->akey_lock);
 	mutex_destroy(&cxt->cleanup_lock);
+	if (cxt->mapping_inode)
+		iput(cxt->mapping_inode);
 	kfree(cxt->ring_state);
 	kfree(cxt);
 	put_device(&sdxi->chardev);
@@ -388,9 +392,9 @@ static int sdxi_publish_cxt(struct sdxi_cxt *cxt,
 	u8 l1_idx;
 	int err;
 
-	if (WARN_ONCE(cxt->id > cxt->sdxi->max_cxtid,
+	if (WARN_ONCE(cxt->id > READ_ONCE(cxt->sdxi->max_cxtid),
 		      "can't install cxt with id %u (limit %u)",
-		      cxt->id, cxt->sdxi->max_cxtid))
+		      cxt->id, READ_ONCE(cxt->sdxi->max_cxtid)))
 		return -EINVAL;
 
 	err = configure_cxt_ctl(cxt->cxt_ctl, ctl_cfg);
@@ -415,7 +419,7 @@ static int sdxi_publish_cxt(struct sdxi_cxt *cxt,
 		.akey_sz     = akey_table_order(cxt->akey_table),
 		.akey_ptr    = cxt->akey_table_dma,
 		.cxt_pasid   = cxt->pasid,
-		.max_buffer  = cxt->sdxi->max_buffer,
+		.max_buffer  = READ_ONCE(cxt->sdxi->max_buffer),
 		.opb_000_enb = cxt->opb_000_enb,
 	};
 
@@ -734,6 +738,8 @@ static void sdxi_cxt_force_exit(struct sdxi_cxt *cxt)
 	mutex_lock(&cxt->cleanup_lock);
 	cxt->cleanup_stopping = true;
 	mutex_unlock(&cxt->cleanup_lock);
+	if (cxt->mapping_inode)
+		unmap_mapping_range(cxt->mapping_inode->i_mapping, 0, 0, 1);
 	cancel_delayed_work_sync(&cxt->cleanup_work);
 
 	mutex_lock(&cxt->cleanup_lock);
@@ -754,6 +760,16 @@ static void sdxi_cxt_force_exit(struct sdxi_cxt *cxt)
 	sdxi_cxt_put(cxt);
 }
 
+void sdxi_cxt_abort(struct sdxi_cxt *cxt)
+{
+	if (!cxt || !sdxi_cxt_exit(cxt))
+		return;
+
+	/* Normal release retained the context; fence DMA before forcing it. */
+	sdxi_dev_fail(cxt->sdxi);
+	sdxi_cxt_force_exit(cxt);
+}
+
 static int sdxi_cxt_doorbell_offset(const struct sdxi_cxt *cxt,
 				    size_t *offset);
 static int sdxi_cxt_set_kernel_doorbell(struct sdxi_cxt *cxt);
@@ -827,7 +843,7 @@ static void sdxi_cxt_id_dtor(const struct sdxi_cxt_id *cxt_id)
 
 static struct sdxi_cxt_id sdxi_cxt_id_ctor(struct sdxi_dev *sdxi)
 {
-	struct xa_limit limit = XA_LIMIT(1, sdxi->max_cxtid);
+	struct xa_limit limit = XA_LIMIT(1, READ_ONCE(sdxi->max_cxtid));
 	u32 index;
 
 	return (struct sdxi_cxt_id) {
@@ -1059,6 +1075,8 @@ int sdxi_cxt_exit(struct sdxi_cxt *cxt)
 	mutex_lock(&cxt->cleanup_lock);
 	cxt->cleanup_stopping = true;
 	mutex_unlock(&cxt->cleanup_lock);
+	if (cxt->mapping_inode)
+		unmap_mapping_range(cxt->mapping_inode->i_mapping, 0, 0, 1);
 	cancel_delayed_work_sync(&cxt->cleanup_work);
 
 	mutex_lock(&cxt->cleanup_lock);
@@ -1220,6 +1238,25 @@ void sdxi_cxt_vma_open(struct sdxi_cxt *cxt)
 	mutex_unlock(&cxt->cleanup_lock);
 }
 
+int sdxi_cxt_vma_acquire(struct sdxi_cxt *cxt)
+{
+	int ret = 0;
+
+	if (!cxt)
+		return -EINVAL;
+
+	mutex_lock(&cxt->cleanup_lock);
+	if (cxt->fatal || cxt->cleanup_stopping || cxt->resources_released) {
+		ret = -ENODEV;
+	} else {
+		sdxi_cxt_get(cxt);
+		cxt->vma_refs++;
+	}
+	mutex_unlock(&cxt->cleanup_lock);
+
+	return ret;
+}
+
 void sdxi_cxt_vma_close(struct sdxi_cxt *cxt)
 {
 	if (!cxt)
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index 03cb5634fa09..c245c7ee7841 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -33,6 +33,7 @@ struct sdxi_akey_table {
 };
 
 struct eventfd_ctx;
+struct inode;
 struct sdxi_process;
 
 /* For encoding the akey table size in CXT_L1_ENT's akey_sz. */
@@ -79,6 +80,7 @@ struct sdxi_cxt {
 
 	struct sdxi_ring_state *ring_state;
 	struct sdxi_process *process;
+	struct inode *mapping_inode;
 
 	spinlock_t intr_lock;		/* eventfd and interrupt state */
 	struct eventfd_ctx *intr_eventfd;
@@ -100,6 +102,7 @@ struct sdxi_cxt *sdxi_ucxt_new(struct sdxi_dev *sdxi, u32 ring_entries,
  * the caller must not reuse it.
  */
 int sdxi_cxt_exit(struct sdxi_cxt *cxt);
+void sdxi_cxt_abort(struct sdxi_cxt *cxt);
 void sdxi_client_cxts_exit(struct sdxi_dev *sdxi);
 void sdxi_cxt_get(struct sdxi_cxt *cxt);
 void sdxi_cxt_put(struct sdxi_cxt *cxt);
@@ -113,6 +116,7 @@ void sdxi_cxt_kill_stopped(struct sdxi_cxt *cxt);
 void sdxi_cxt_release_user_handle(struct sdxi_cxt *cxt);
 int sdxi_cxt_notify_mm_release(struct sdxi_cxt *cxt);
 void sdxi_cxt_vma_open(struct sdxi_cxt *cxt);
+int sdxi_cxt_vma_acquire(struct sdxi_cxt *cxt);
 void sdxi_cxt_vma_close(struct sdxi_cxt *cxt);
 
 enum sdxi_submit_flags {
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index b5960619d511..7ebecae3c541 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -391,6 +391,9 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	sdxi->db_stride *= 1U << FIELD_GET(SDXI_MMIO_CAP0_DB_STRIDE, cap0);
 	if (sdxi->db_stride > sdxi->db_size)
 		return -ENODEV;
+	sdxi->max_ring_entries = SZ_1K;
+	sdxi->max_ring_entries <<=
+		FIELD_GET(SDXI_MMIO_CAP0_MAX_DS_RING_SZ, cap0);
 
 	cap1 = sdxi_read64(sdxi, SDXI_MMIO_CAP1);
 	sdxi->op_grp_cap = FIELD_GET(SDXI_MMIO_CAP1_OPB_000_CAP, cap1);
diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index b25f8bc273c8..70dcde916c82 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -8,17 +8,28 @@
 #include <linux/cdev.h>
 #include <linux/bitfield.h>
 #include <linux/capability.h>
+#include <linux/cleanup.h>
 #include <linux/compat.h>
+#include <linux/anon_inodes.h>
 #include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/idr.h>
+#include <linux/limits.h>
 #include <linux/mm.h>
 #include <linux/minmax.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
+#include <linux/poll.h>
+#include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include <uapi/linux/sdxi.h>
 
+#include "admin.h"
+#include "context.h"
+#include "intr.h"
 #include "ioctl.h"
 #include "mmio.h"
 #include "process.h"
@@ -29,6 +40,29 @@
 static dev_t sdxi_devt;
 static struct class *sdxi_class;
 static DEFINE_IDA(sdxi_minor_ida);
+static const struct file_operations sdxi_cxt_fops;
+
+static void sdxi_cxt_vma_open_cb(struct vm_area_struct *vma)
+{
+	sdxi_cxt_vma_open(vma->vm_private_data);
+}
+
+static void sdxi_cxt_vma_close_cb(struct vm_area_struct *vma)
+{
+	sdxi_cxt_vma_close(vma->vm_private_data);
+}
+
+static const struct vm_operations_struct sdxi_cxt_vm_ops = {
+	.open = sdxi_cxt_vma_open_cb,
+	.close = sdxi_cxt_vma_close_cb,
+};
+
+static void sdxi_cxt_vma_attach(struct vm_area_struct *vma,
+				struct sdxi_cxt *cxt)
+{
+	vma->vm_ops = &sdxi_cxt_vm_ops;
+	vma->vm_private_data = cxt;
+}
 
 static int sdxi_file_check_owner(const struct sdxi_process *process)
 {
@@ -60,7 +94,9 @@ static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
 		.cap_words = SDXI_UAPI_CAP_WORDS,
 		.caps = {
 			SDXI_UAPI_CAP_DEV_INFO |
-			SDXI_UAPI_CAP_DEV_ATTR_MGMT,
+			SDXI_UAPI_CAP_DEV_ATTR_MGMT |
+			SDXI_UAPI_CAP_CXT_CREATE |
+			SDXI_UAPI_CAP_CXT_EVENTFD,
 		},
 	};
 
@@ -205,6 +241,13 @@ static int sdxi_dev_attr_write(struct sdxi_dev *sdxi,
 	register_value &= ~desc->mask;
 	register_value |= (value << shift) & desc->mask;
 	sdxi_write64(sdxi, desc->reg, register_value);
+	if (desc == &sdxi_dev_attrs[SDXI_DEV_ATTR_MAX_BUFFER]) {
+		WRITE_ONCE(sdxi->max_buffer, (u8)value);
+		dma_set_max_seg_size(sdxi->dev,
+				     min((u64)SZ_2M << value, (u64)U32_MAX));
+	} else if (desc == &sdxi_dev_attrs[SDXI_DEV_ATTR_MAX_CXT]) {
+		WRITE_ONCE(sdxi->max_cxtid, (u16)value);
+	}
 	mutex_unlock(&sdxi->reg_lock);
 	return 0;
 }
@@ -264,6 +307,248 @@ static int sdxi_ioctl_set_dev_attr(struct file *file, struct sdxi_file *sf,
 	return sdxi_dev_attr_read(sf->process->sdxi, desc, &args->val);
 }
 
+static bool sdxi_u32_array_zero(const u32 *values, size_t count)
+{
+	size_t i;
+
+	for (i = 0; i < count; i++)
+		if (values[i])
+			return false;
+
+	return true;
+}
+
+static int sdxi_user_op_groups(struct sdxi_dev *sdxi, u32 requested,
+			       u32 *applied)
+{
+	u32 available;
+	u32 allowed;
+
+	available = FIELD_GET(SDXI_MMIO_CTL2_OPB_000_AVL,
+			      sdxi_read64(sdxi, SDXI_MMIO_CTL2));
+	allowed = available & SDXI_OPB_000_INTR;
+
+	if (!requested)
+		requested = allowed;
+	if (requested & SDXI_OPB_000_ADMIN)
+		return -EPERM;
+	if (requested & ~available)
+		return -EINVAL;
+	if (requested & ~allowed)
+		return -EOPNOTSUPP;
+
+	*applied = requested;
+	return 0;
+}
+
+static int sdxi_ioctl_create_cxt(struct file *file, struct sdxi_file *sf,
+				 void *data)
+{
+	struct sdxi_create_cxt_args *args = data;
+	struct sdxi_process *process = sf->process;
+	struct iommu_sva *unbind_sva = NULL;
+	struct sdxi_file *cxt_sf = NULL;
+	struct sdxi_cxt *cxt = NULL;
+	struct file *cxt_file;
+	u32 unbind_pasid = 0;
+	u32 ring_entries;
+	u32 op_groups;
+	size_t ring_bytes;
+	bool bound = false;
+	u16 akey_flags;
+	int cxt_fd;
+	int ret;
+
+	if (args->argsz < sizeof(*args) || args->flags)
+		return -EINVAL;
+	if (process->sdxi->db_stride < PAGE_SIZE ||
+	    !IS_ALIGNED(process->sdxi->db_stride, PAGE_SIZE))
+		return -EOPNOTSUPP;
+
+	ret = sdxi_user_op_groups(process->sdxi, args->opb_000_enb,
+				  &op_groups);
+	if (ret)
+		return ret;
+
+	ring_entries = args->ds_ring_entries ?: SZ_1K;
+	if (ring_entries < SZ_1K ||
+	    ring_entries > process->sdxi->max_ring_entries)
+		return -EINVAL;
+	if (check_mul_overflow((size_t)ring_entries,
+			       sizeof(struct sdxi_desc), &ring_bytes))
+		return -EOVERFLOW;
+
+	if (!args->ds_ring_base || !args->write_index_base ||
+	    !args->cxt_sts_base)
+		return -EINVAL;
+	if (!IS_ALIGNED(args->ds_ring_base,
+			BIT_ULL(DESC_RING_BASE_PTR_SHIFT)) ||
+	    !IS_ALIGNED(args->write_index_base,
+			BIT_ULL(WRT_INDEX_PTR_SHIFT)) ||
+	    !IS_ALIGNED(args->cxt_sts_base,
+			BIT_ULL(CXT_STATUS_PTR_SHIFT)))
+		return -EINVAL;
+	if (!access_ok(u64_to_user_ptr(args->ds_ring_base), ring_bytes) ||
+	    !access_ok(u64_to_user_ptr(args->write_index_base),
+		       sizeof(__le64)) ||
+	    !access_ok(u64_to_user_ptr(args->cxt_sts_base),
+		       sizeof(struct sdxi_cxt_sts)))
+		return -EFAULT;
+
+	if (clear_user(u64_to_user_ptr(args->write_index_base),
+		       sizeof(__le64)) ||
+	    clear_user(u64_to_user_ptr(args->cxt_sts_base),
+		       sizeof(struct sdxi_cxt_sts)))
+		return -EFAULT;
+
+	mutex_lock(&process->mutex);
+	if (process->cxt) {
+		ret = -EBUSY;
+		goto out_unlock;
+	}
+
+	if (!process->sva) {
+		ret = sdxi_process_bind(process);
+		if (ret)
+			goto out_unlock;
+		bound = true;
+	}
+	if (!FIELD_FIT(SDXI_AKEY_ENT_PASID, process->pasid)) {
+		ret = -ERANGE;
+		goto out_unbind;
+	}
+
+	cxt = sdxi_ucxt_new(process->sdxi, ring_entries,
+			    args->ds_ring_base, args->write_index_base,
+			    args->cxt_sts_base, process->pasid, op_groups);
+	if (!cxt) {
+		ret = -ENOMEM;
+		goto out_unbind;
+	}
+
+	/* Subsequent failure cleanup now owns the context and SVA lifetime. */
+	cxt->process = process;
+	process->cxt = cxt;
+	sdxi_process_get(process);
+
+	akey_flags = SDXI_AKEY_ENT_VL | SDXI_AKEY_ENT_PV;
+	cxt->akey_table->entry[1] = (struct sdxi_akey_ent) {
+		.intr_num = cpu_to_le16(akey_flags),
+		.pasid = cpu_to_le32(FIELD_PREP(SDXI_AKEY_ENT_PASID,
+						process->pasid)),
+	};
+	dma_wmb();
+
+	if (op_groups & SDXI_OPB_000_INTR) {
+		ret = sdxi_cxt_init_intr(cxt);
+		if (ret)
+			goto out_exit_cxt;
+	}
+
+	ret = sdxi_adm_start_cxt(cxt);
+	if (ret)
+		goto out_exit_cxt;
+
+	cxt_sf = kzalloc_obj(*cxt_sf, GFP_KERNEL);
+	if (!cxt_sf) {
+		ret = -ENOMEM;
+		goto out_exit_cxt;
+	}
+
+	cxt_sf->type = SDXI_FILE_CONTEXT;
+	cxt_sf->process = process;
+	cxt_sf->cxt = cxt;
+
+	cxt_fd = get_unused_fd_flags(O_CLOEXEC);
+	if (cxt_fd < 0) {
+		ret = cxt_fd;
+		goto out_exit_cxt;
+	}
+
+	cxt_file = anon_inode_create_getfile("sdxi-context", &sdxi_cxt_fops,
+					     cxt_sf, O_RDWR, NULL);
+	if (IS_ERR(cxt_file)) {
+		put_unused_fd(cxt_fd);
+		ret = PTR_ERR(cxt_file);
+		goto out_exit_cxt;
+	}
+	cxt->mapping_inode = file_inode(cxt_file);
+	ihold(cxt->mapping_inode);
+
+	sdxi_process_get(process);
+	sdxi_cxt_get(cxt);
+	fd_install(cxt_fd, cxt_file);
+
+	*args = (typeof(*args)) {
+		.argsz = sizeof(*args),
+		.opb_000_enb = op_groups,
+		.pasid = process->pasid,
+		.ds_ring_entries = ring_entries,
+		.ds_ring_base = args->ds_ring_base,
+		.cxt_sts_base = args->cxt_sts_base,
+		.write_index_base = args->write_index_base,
+		.doorbell_mmap_base =
+			sdxi_mmap_token(SDXI_MMAP_TYPE_DOORBELL, cxt->id,
+					cxt->mmap_gen),
+		.cxt_fd = cxt_fd,
+	};
+	mutex_unlock(&process->mutex);
+	return 0;
+
+out_exit_cxt:
+	mutex_unlock(&process->mutex);
+	sdxi_cxt_abort(cxt);
+	kfree(cxt_sf);
+	return ret;
+out_unbind:
+	if (bound)
+		unbind_sva = sdxi_process_detach_sva_locked(process,
+							    &unbind_pasid);
+out_unlock:
+	mutex_unlock(&process->mutex);
+	if (unbind_sva)
+		sdxi_process_unbind_sva(process, unbind_sva, unbind_pasid);
+	kfree(cxt_sf);
+	return ret;
+}
+
+static int sdxi_ioctl_cxt_eventfd(struct file *file, struct sdxi_file *sf,
+				  void *data)
+{
+	struct sdxi_cxt_eventfd_args *args = data;
+	int ret;
+
+	if (args->argsz < sizeof(*args) || args->flags || args->rsvd0 ||
+	    !sdxi_u32_array_zero(args->rsvd1, ARRAY_SIZE(args->rsvd1)))
+		return -EINVAL;
+	if (!sf->cxt)
+		return -ENODEV;
+
+	ret = sdxi_cxt_set_intr_fd(sf->cxt, args->eventfd,
+				   &args->intr_akey, &args->intr_num);
+	if (!ret)
+		args->argsz = sizeof(*args);
+	return ret;
+}
+
+static int sdxi_ioctl_quiesce_cxt(struct file *file, struct sdxi_file *sf,
+				  void *data)
+{
+	struct sdxi_quiesce_cxt_args *args = data;
+	int ret;
+
+	if (args->argsz < sizeof(*args) || args->flags)
+		return -EINVAL;
+	if (!sf->cxt)
+		return -ENODEV;
+
+	ret = sdxi_cxt_quiesce(sf->cxt, args->timeout_ms,
+			       &args->cst_flags);
+	if (!ret)
+		args->argsz = sizeof(*args);
+	return ret;
+}
+
 #define SDXI_IOCTL_DEF(_nr, _cmd, _func, _files, _access)	\
 	[_nr] = {						\
 		.cmd = (_cmd),					\
@@ -290,9 +575,9 @@ static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
 	SDXI_IOCTL_DEF(0x07, SDXI_SET_RKEYS, sdxi_ioctl_unsupported,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
-	SDXI_IOCTL_DEF(0x08, SDXI_CREATE_CXT, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x08, SDXI_CREATE_CXT, sdxi_ioctl_create_cxt,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
-	SDXI_IOCTL_DEF(0x09, SDXI_CXT_EVENTFD, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x09, SDXI_CXT_EVENTFD, sdxi_ioctl_cxt_eventfd,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
 	SDXI_IOCTL_DEF(0x0a, SDXI_GET_AKEY_LIST, sdxi_ioctl_unsupported,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
@@ -304,7 +589,7 @@ static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
 	SDXI_IOCTL_DEF(0x0e, SDXI_ADMIN_VENDOR, sdxi_ioctl_unsupported,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_VENDOR_HANDLER),
-	SDXI_IOCTL_DEF(0x0f, SDXI_QUIESCE_CXT, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0f, SDXI_QUIESCE_CXT, sdxi_ioctl_quiesce_cxt,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
 };
 
@@ -431,6 +716,95 @@ static long sdxi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	return ret;
 }
 
+static int sdxi_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct sdxi_file *sf = file->private_data;
+	struct sdxi_cxt *cxt;
+	struct sdxi_dev *sdxi;
+	u64 allowed_mask;
+	u64 token;
+	u64 type;
+	u32 generation;
+	int ret;
+
+	if (!sf || sf->type != SDXI_FILE_CONTEXT || !sf->process)
+		return -EINVAL;
+
+	ret = sdxi_file_check_owner(sf->process);
+	if (ret)
+		return ret;
+
+	sdxi = sf->process->sdxi;
+	guard(rwsem_read)(&sdxi->state_lock);
+	if (!sdxi->online)
+		return -ENODEV;
+
+	cxt = sf->cxt;
+	if (!cxt || !cxt->id)
+		return -EINVAL;
+	if (sdxi_cxt_is_fatal(cxt) || READ_ONCE(cxt->resources_released))
+		return -ENODEV;
+
+	token = (u64)vma->vm_pgoff << PAGE_SHIFT;
+	allowed_mask = SDXI_MMAP_TYPE_MASK | SDXI_MMAP_CXT_ID_MASK |
+		       SDXI_MMAP_GEN_MASK;
+	if (token & ~allowed_mask)
+		return -EINVAL;
+
+	type = token & SDXI_MMAP_TYPE_MASK;
+	generation = cxt->mmap_gen &
+		     (SDXI_MMAP_GEN_MASK >> SDXI_MMAP_GEN_SHIFT);
+	if (type != SDXI_MMAP_TYPE_DOORBELL ||
+	    sdxi_mmap_token_cxt_id(token) != cxt->id ||
+	    sdxi_mmap_token_gen(token) != generation)
+		return -EINVAL;
+
+	if (vma->vm_end - vma->vm_start != cxt->sdxi->db_stride)
+		return -EINVAL;
+	if (!(vma->vm_flags & VM_WRITE) || vma->vm_flags & VM_EXEC)
+		return -EPERM;
+	if (!cxt->sdxi->bus_ops->mmap_doorbell)
+		return -EOPNOTSUPP;
+	ret = sdxi_cxt_vma_acquire(cxt);
+	if (ret)
+		return ret;
+
+	vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND |
+		     VM_DONTDUMP | VM_PFNMAP);
+	vm_flags_clear(vma, VM_MAYEXEC);
+	vma->vm_pgoff = 0;
+	ret = cxt->sdxi->bus_ops->mmap_doorbell(cxt->sdxi, cxt, vma);
+	if (!ret)
+		sdxi_cxt_vma_attach(vma, cxt);
+	else
+		sdxi_cxt_vma_close(cxt);
+	return ret;
+}
+
+static __poll_t sdxi_cxt_poll(struct file *file, poll_table *wait)
+{
+	struct sdxi_file *sf = file->private_data;
+	struct sdxi_cxt *cxt;
+	__poll_t mask = 0;
+
+	if (!sf || sf->type != SDXI_FILE_CONTEXT || !sf->process)
+		return EPOLLERR;
+	if (sdxi_file_check_owner(sf->process))
+		return EPOLLERR;
+
+	cxt = sf->cxt;
+	if (!cxt)
+		return EPOLLERR | EPOLLHUP;
+
+	poll_wait(file, &cxt->poll_wq, wait);
+	if (sdxi_cxt_is_fatal(cxt))
+		mask |= EPOLLERR;
+	if (READ_ONCE(cxt->resources_released))
+		mask |= EPOLLHUP;
+
+	return mask;
+}
+
 static int sdxi_open(struct inode *inode, struct file *file)
 {
 	struct sdxi_dev *sdxi;
@@ -470,6 +844,11 @@ static int sdxi_release(struct inode *inode, struct file *file)
 	if (!sf)
 		return 0;
 
+	if (sf->type == SDXI_FILE_CONTEXT && sf->cxt) {
+		sdxi_cxt_release_user_handle(sf->cxt);
+		sdxi_cxt_put(sf->cxt);
+		sf->cxt = NULL;
+	}
 	sdxi_process_put(sf->process);
 	kfree(sf);
 	file->private_data = NULL;
@@ -484,6 +863,15 @@ static const struct file_operations sdxi_fops = {
 	.compat_ioctl = compat_ptr_ioctl,
 };
 
+static const struct file_operations sdxi_cxt_fops = {
+	.owner = THIS_MODULE,
+	.release = sdxi_release,
+	.unlocked_ioctl = sdxi_ioctl,
+	.compat_ioctl = compat_ptr_ioctl,
+	.mmap = sdxi_mmap,
+	.poll = sdxi_cxt_poll,
+};
+
 int sdxi_chardev_init(void)
 {
 	int ret;
diff --git a/drivers/dma/sdxi/pci.c b/drivers/dma/sdxi/pci.c
index eca55c8d65f8..010ed2c6ad74 100644
--- a/drivers/dma/sdxi/pci.c
+++ b/drivers/dma/sdxi/pci.c
@@ -11,9 +11,12 @@
 #include <linux/err.h>
 #include <linux/io.h>
 #include <linux/iomap.h>
+#include <linux/mm.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
 #include <linux/pci.h>
 
+#include "context.h"
 #include "mmio.h"
 #include "sdxi.h"
 
@@ -129,12 +132,36 @@ static void sdxi_pci_force_quiesce(struct sdxi_dev *sdxi)
 		dev_err(&pdev->dev, "pending PCI transactions did not drain\n");
 }
 
+static int sdxi_pci_mmap_doorbell(struct sdxi_dev *sdxi,
+				  struct sdxi_cxt *cxt,
+				  struct vm_area_struct *vma)
+{
+	struct pci_dev *pdev = sdxi_to_pci_dev(sdxi);
+	resource_size_t offset;
+	resource_size_t address;
+
+	if (check_mul_overflow((resource_size_t)cxt->id,
+			       (resource_size_t)sdxi->db_stride, &offset))
+		return -EOVERFLOW;
+	if (offset > pci_resource_len(pdev, SDXI_PCI_BAR_DOORBELL) ||
+	    sdxi->db_stride >
+	    pci_resource_len(pdev, SDXI_PCI_BAR_DOORBELL) - offset)
+		return -EINVAL;
+
+	address = pci_resource_start(pdev, SDXI_PCI_BAR_DOORBELL) + offset;
+	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+	return io_remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
+				  sdxi->db_stride, vma->vm_page_prot);
+}
+
 static const struct sdxi_bus_ops sdxi_pci_ops = {
 	.owner = THIS_MODULE,
 	.init = sdxi_pci_init,
 	.reset = sdxi_pci_reset,
 	.get_irq = sdxi_pci_get_irq,
 	.force_quiesce = sdxi_pci_force_quiesce,
+	.mmap_doorbell = sdxi_pci_mmap_doorbell,
 };
 
 static int sdxi_pci_probe(struct pci_dev *pdev,
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 03a85a1575e7..0f7a63d6fc4b 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -47,7 +47,9 @@ enum {
 };
 
 struct sdxi_dev;
+struct sdxi_cxt;
 struct module;
+struct vm_area_struct;
 
 /**
  * struct sdxi_bus_ops - Bus-specific methods for SDXI devices.
@@ -74,6 +76,11 @@ struct sdxi_bus_ops {
 	 *                  device recovery fails.
 	 */
 	void (*force_quiesce)(struct sdxi_dev *sdxi);
+	/**
+	 * @mmap_doorbell: Map one context's doorbell region.
+	 */
+	int (*mmap_doorbell)(struct sdxi_dev *sdxi, struct sdxi_cxt *cxt,
+			     struct vm_area_struct *vma);
 };
 
 struct sdxi_dev {
@@ -86,6 +93,7 @@ struct sdxi_dev {
 
 	/* hardware capabilities (from cap0 & cap1) */
 	u32 db_stride;			/* doorbell stride in bytes */
+	u64 max_ring_entries;
 	u16 max_cxtid;			/* Maximum context ID allowed. */
 	u8 max_buffer;			/* Configured log2 buffer size minus 21. */
 	u32 op_grp_cap;			/* supported operation group cap */
-- 
2.56.1


  parent reply	other threads:[~2026-11-19 20:27 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
2026-11-19 20:22 ` [PATCH v1 01/44] dmaengine: sdxi: cache hardware version for feature gating Wei Huang
2026-11-19 20:22 ` [PATCH v1 02/44] dmaengine: sdxi: define key and error MMIO registers Wei Huang
2026-11-19 20:22 ` [PATCH v1 03/44] dmaengine: sdxi: define SDXI hardware layouts Wei Huang
2026-11-19 20:22 ` [PATCH v1 04/44] dmaengine: sdxi: encode admin descriptors Wei Huang
2026-11-19 20:22 ` [PATCH v1 05/44] dmaengine: sdxi: support timeout-aware completion waits Wei Huang
2026-11-19 20:22 ` [PATCH v1 06/44] dmaengine: sdxi: support the full context table hierarchy Wei Huang
2026-11-19 20:22 ` [PATCH v1 07/44] dmaengine: sdxi: implement synchronous admin operations Wei Huang
2026-11-19 20:22 ` [PATCH v1 08/44] dmaengine: sdxi: add core RKey table management Wei Huang
2026-11-19 20:22 ` [PATCH v1 09/44] dmaengine: sdxi: handle hardware error logs Wei Huang
2026-11-19 20:22 ` [PATCH v1 10/44] dmaengine: sdxi: separate context and submission resources Wei Huang
2026-11-19 20:22 ` [PATCH v1 11/44] dmaengine: sdxi: support userspace context lifetimes Wei Huang
2026-11-19 20:22 ` [PATCH v1 12/44] dmaengine: sdxi: route context interrupts to eventfd Wei Huang
2026-11-19 20:22 ` [PATCH v1 13/44] dmaengine: sdxi: kill user contexts on fatal errors Wei Huang
2026-11-19 20:22 ` [PATCH v1 14/44] dmaengine: sdxi: define the ioctl UAPI surface Wei Huang
2026-11-19 20:22 ` [PATCH v1 15/44] dmaengine: sdxi: add internal ioctl definitions Wei Huang
2026-11-19 20:22 ` [PATCH v1 16/44] dmaengine: sdxi: register core character devices Wei Huang
2026-11-19 20:22 ` [PATCH v1 17/44] dmaengine: sdxi: implement fixed-size ioctl dispatch Wei Huang
2026-11-19 20:22 ` Wei Huang [this message]
2026-11-19 20:22 ` [PATCH v1 19/44] dmaengine: sdxi: validate and copy key ioctl payloads Wei Huang
2026-11-19 20:22 ` [PATCH v1 20/44] dmaengine: sdxi: implement RKey management ioctls Wei Huang
2026-11-19 20:22 ` [PATCH v1 21/44] dmaengine: sdxi: implement AKey " Wei Huang
2026-11-19 20:22 ` [PATCH v1 22/44] dmaengine: sdxi: expose operation-group discovery Wei Huang
2026-11-19 20:22 ` [PATCH v1 23/44] dmaengine: sdxi: implement typed vendor admin transport Wei Huang
2026-11-19 20:23 ` [PATCH v1 24/44] dmaengine: sdxi: expose character device attributes Wei Huang
2026-11-19 20:23 ` [PATCH v1 25/44] dmaengine: sdxi: add PF and VF device naming Wei Huang
2026-11-19 20:23 ` [PATCH v1 26/44] dmaengine: sdxi: add SDXI trace events Wei Huang
2026-11-19 20:23 ` [PATCH v1 27/44] dmaengine: sdxi: support suspend and resume Wei Huang
2026-11-19 20:23 ` [PATCH v1 28/44] dmaengine: sdxi: amd: define QoS request formats Wei Huang
2026-11-19 20:23 ` [PATCH v1 29/44] dmaengine: sdxi: amd: add QoS validation scaffolding Wei Huang
2026-11-19 20:23 ` [PATCH v1 30/44] dmaengine: sdxi: amd: build QoS admin descriptors Wei Huang
2026-11-19 20:23 ` [PATCH v1 31/44] dmaengine: sdxi: amd: prevalidate QoS operations Wei Huang
2026-11-19 20:23 ` [PATCH v1 32/44] dmaengine: sdxi: amd: execute QoS vendor operations Wei Huang
2026-11-19 20:23 ` [PATCH v1 33/44] dmaengine: sdxi: amd: register QoS vendor handler Wei Huang
2026-11-19 20:23 ` [PATCH v1 34/44] dmaengine: sdxi: amd: test QoS validation with KUnit Wei Huang
2026-11-19 20:23 ` [PATCH v1 35/44] selftests: sdxi: add build scaffold Wei Huang
2026-11-19 20:23 ` [PATCH v1 36/44] selftests: sdxi: add shared descriptor and context helpers Wei Huang
2026-11-19 20:23 ` [PATCH v1 37/44] selftests: sdxi: test context creation Wei Huang
2026-11-19 20:23 ` [PATCH v1 38/44] selftests: sdxi: test NOP descriptor submission Wei Huang
2026-11-19 20:23 ` [PATCH v1 39/44] selftests: sdxi: test copy submission and ring wrap Wei Huang
2026-11-19 20:23 ` [PATCH v1 40/44] selftests: sdxi: exercise dataplane error paths Wei Huang
2026-11-19 20:23 ` [PATCH v1 41/44] selftests: sdxi: test device ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 42/44] selftests: sdxi: test vendor admin ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 43/44] selftests: sdxi: test RKey ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 44/44] MAINTAINERS: cover SDXI UAPI, vendor files, and selftests Wei Huang

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=20261119202321.1734355-19-wei.huang2@amd.com \
    --to=wei.huang2@amd.com \
    --cc=Frank.Li@kernel.org \
    --cc=John.Kariuki@amd.com \
    --cc=Shyam.Iyer@dell.com \
    --cc=Stephen.Bates@amd.com \
    --cc=bhelgaas@google.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=nathan.lynch@amd.com \
    --cc=rientjes@google.com \
    --cc=shuah@kernel.org \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tycho@kernel.org \
    --cc=vkoul@kernel.org \
    --cc=weixugc@google.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®