mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support
@ 2026-11-19 20:22 Wei Huang
  2026-11-19 20:22 ` [PATCH v1 01/44] dmaengine: sdxi: cache hardware version for feature gating Wei Huang
                   ` (43 more replies)
  0 siblings, 44 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

This series extends the existing SDXI phase 1 "base" driver with versioned
userspace-facing interfaces for the native SDXI software stack: device
discovery and management, process-owned execution contexts, and a
constrained vendor-extension framework.

This series applies on top of Nathan Lynch's phase 1 "base" v4 patchset,
ending at commit b8b01d7ab1e5 ("dmaengine: sdxi: Add DMA engine provider").
This series is available at:

    https://github.com/AMDESE/linux-sdxi/tree/sdxi-phase2-upstream-v1

At a high level, this new series adds:

  - A versioned ioctl UAPI and character-device interface for discovery
    and management.
  - Userspace contexts with process-bound context fds, SVA-backed rings,
    per-context doorbell mappings, and eventfd interrupt delivery.
  - A typed vendor-extension path that accepts structured requests rather
    than raw descriptors, plus an AMD QoS implementation.
  - Test coverage (KUnit + kselftests) and MAINTAINERS updates.

Patch map:

  - Patches 1-9: hardware and admin groundwork (feature gating, hardware
    layouts, admin operations, context hierarchy, RKey setup, error logs).
  - Patches 10-13: userspace-context foundations (resource separation,
    process/SVA and context lifetimes, eventfd delivery, and fatal-error
    cleanup).
  - Patches 14-27: versioned ioctl UAPI + character device (discovery and
    management, context-fd operations, vendor plumbing, tracing,
    suspend/resume).
  - Patches 28-34: AMD QoS vendor feature (capabilities, configuration,
    assignment, telemetry), with KUnit coverage.
  - Patches 35-44: kselftests (context creation, dataplane basics, error
    paths, ioctls), plus MAINTAINERS updates.

The interface is structured to separate management from the dataplane. A
device fd represents one SDXI function and is used for discovery and
function-wide management. SDXI_CREATE_CXT binds the caller's mm through
IOMMU SVA, programs userspace-owned ring state, and returns a process-bound
context fd for context-scoped control and mapping only that context's
doorbell. Userspace submits native descriptors through the SVA-backed ring
and mapped doorbell; context interrupts are delivered through a bound
eventfd. The kernel owns and validates context and key tables and issues
administrative operations for lifecycle and management requests.

Function-wide mutation and RKey management require CAP_SYS_ADMIN. Context
ioctls and doorbell mappings are restricted to the creating thread group
and mm. AKeys written by non-admin callers are limited to the local
function and bound PASID. Vendor handlers authorize typed requests. AMD
QoS composes descriptors on the kernel-owned admin context and is PF-only
and CAP_SYS_ADMIN-gated.

Companion userspace projects are available at:

  - libsdxi: userspace library
    https://github.com/AMDESE/libsdxi/tree/sdxi-phase2-upstream-v1

  - sdxi-cli: command-line tool
    https://github.com/AMDESE/sdxi-cli/tree/sdxi-phase2-upstream-v1

libsdxi wraps the UAPI and provides device discovery, context lifecycle,
key management, event handling, and vendor helpers. Sample programs
provide quick tests:

  $ ./samples/memcopy --dev /dev/sdxi0
  $ ./samples/cxt_intr --dev sdxi1

sdxi-cli is an SDXI management tool built on libsdxi. Representative
device inspection commands are:

  $ sdxi devices
  $ sdxi show sdxi0 --verbose
  $ sdxi attrs sdxi0

SDXI supports vendor-specific extensions. This patchset adds AMD QoS
management through a separate vendor handler (sdxi-amd-qos when built as
a module). AMD QoS userspace management is provided in sdxi-cli under a
vendor namespace:

  $ sdxi amd qos cap sdxi0
  $ sdxi amd qos status sdxi0
  $ sdxi amd qos assign sdxi0 --pool 1 --cxt 1

This patchset has been tested on both bare metal and virtual machines with
a passed-through VF, with the SDXI core, PCI driver, and KUnit tests built
as modules.

SDXI (Smart Data Accelerator Interface) is a SNIA standard for a
memory-to-memory data mover and acceleration interface, developed by
industry contributors including Dell, AMD, VMware, etc. The SDXI
specifications and related material are available at:

  - SDXI v1.0a and v1.1 specifications: https://www.snia.org/sdxi

Thanks,
-Wei

Wei Huang (44):
  dmaengine: sdxi: cache hardware version for feature gating
  dmaengine: sdxi: define key and error MMIO registers
  dmaengine: sdxi: define SDXI hardware layouts
  dmaengine: sdxi: encode admin descriptors
  dmaengine: sdxi: support timeout-aware completion waits
  dmaengine: sdxi: support the full context table hierarchy
  dmaengine: sdxi: implement synchronous admin operations
  dmaengine: sdxi: add core RKey table management
  dmaengine: sdxi: handle hardware error logs
  dmaengine: sdxi: separate context and submission resources
  dmaengine: sdxi: support userspace context lifetimes
  dmaengine: sdxi: route context interrupts to eventfd
  dmaengine: sdxi: kill user contexts on fatal errors
  dmaengine: sdxi: define the ioctl UAPI surface
  dmaengine: sdxi: add internal ioctl definitions
  dmaengine: sdxi: register core character devices
  dmaengine: sdxi: implement fixed-size ioctl dispatch
  dmaengine: sdxi: implement userspace context file operations
  dmaengine: sdxi: validate and copy key ioctl payloads
  dmaengine: sdxi: implement RKey management ioctls
  dmaengine: sdxi: implement AKey management ioctls
  dmaengine: sdxi: expose operation-group discovery
  dmaengine: sdxi: implement typed vendor admin transport
  dmaengine: sdxi: expose character device attributes
  dmaengine: sdxi: add PF and VF device naming
  dmaengine: sdxi: add SDXI trace events
  dmaengine: sdxi: support suspend and resume
  dmaengine: sdxi: amd: define QoS request formats
  dmaengine: sdxi: amd: add QoS validation scaffolding
  dmaengine: sdxi: amd: build QoS admin descriptors
  dmaengine: sdxi: amd: prevalidate QoS operations
  dmaengine: sdxi: amd: execute QoS vendor operations
  dmaengine: sdxi: amd: register QoS vendor handler
  dmaengine: sdxi: amd: test QoS validation with KUnit
  selftests: sdxi: add build scaffold
  selftests: sdxi: add shared descriptor and context helpers
  selftests: sdxi: test context creation
  selftests: sdxi: test NOP descriptor submission
  selftests: sdxi: test copy submission and ring wrap
  selftests: sdxi: exercise dataplane error paths
  selftests: sdxi: test device ioctls
  selftests: sdxi: test vendor admin ioctls
  selftests: sdxi: test RKey ioctls
  MAINTAINERS: cover SDXI UAPI, vendor files, and selftests

 MAINTAINERS                                   |    3 +
 drivers/dma/sdxi/.kunitconfig                 |    1 +
 drivers/dma/sdxi/Kconfig                      |   25 +-
 drivers/dma/sdxi/Makefile                     |   13 +-
 drivers/dma/sdxi/admin.c                      |  290 ++++
 drivers/dma/sdxi/admin.h                      |   45 +
 drivers/dma/sdxi/amd/Makefile                 |    6 +
 drivers/dma/sdxi/amd/qos.c                    | 1135 ++++++++++++++
 drivers/dma/sdxi/amd/qos.h                    |   95 ++
 drivers/dma/sdxi/amd/qos_kunit.c              |  180 +++
 drivers/dma/sdxi/completion.c                 |   37 +-
 drivers/dma/sdxi/completion.h                 |    4 +
 drivers/dma/sdxi/context.c                    |  980 ++++++++++--
 drivers/dma/sdxi/context.h                    |   74 +-
 drivers/dma/sdxi/descriptor.c                 |   89 ++
 drivers/dma/sdxi/descriptor.h                 |   27 +
 drivers/dma/sdxi/descriptor_kunit.c           |  150 ++
 drivers/dma/sdxi/device.c                     |  423 +++++-
 drivers/dma/sdxi/dma.c                        |   22 +-
 drivers/dma/sdxi/error.c                      |  371 +++++
 drivers/dma/sdxi/error.h                      |   18 +
 drivers/dma/sdxi/hw.h                         |   80 +
 drivers/dma/sdxi/intr.c                       |  320 ++++
 drivers/dma/sdxi/intr.h                       |   21 +
 drivers/dma/sdxi/ioctl.c                      | 1331 +++++++++++++++++
 drivers/dma/sdxi/ioctl.h                      |   78 +
 drivers/dma/sdxi/keys.c                       |  577 +++++++
 drivers/dma/sdxi/keys.h                       |   29 +
 drivers/dma/sdxi/mmio.h                       |   27 +-
 drivers/dma/sdxi/pci.c                        |   91 +-
 drivers/dma/sdxi/process.c                    |  200 +++
 drivers/dma/sdxi/process.h                    |   43 +
 drivers/dma/sdxi/sdxi.h                       |   96 +-
 drivers/dma/sdxi/sysfs.c                      |   70 +
 drivers/dma/sdxi/sysfs.h                      |   13 +
 drivers/dma/sdxi/trace.c                      |    9 +
 drivers/dma/sdxi/trace.h                      |  137 ++
 drivers/dma/sdxi/vendor.c                     |  138 ++
 drivers/dma/sdxi/vendor.h                     |   42 +
 drivers/dma/sdxi/version.h                    |   32 +
 include/uapi/linux/sdxi.h                     |  569 +++++++
 tools/testing/selftests/Makefile              |    1 +
 tools/testing/selftests/sdxi/.gitignore       |    7 +
 tools/testing/selftests/sdxi/Makefile         |   35 +
 tools/testing/selftests/sdxi/admin_cmd.c      |  104 ++
 tools/testing/selftests/sdxi/config           |    2 +
 tools/testing/selftests/sdxi/context.c        |  343 +++++
 tools/testing/selftests/sdxi/copy.c           |   40 +
 tools/testing/selftests/sdxi/device.c         |  245 +++
 tools/testing/selftests/sdxi/error.c          |   24 +
 tools/testing/selftests/sdxi/lib/context.c    |  195 +++
 tools/testing/selftests/sdxi/lib/descriptor.h |   55 +
 .../selftests/sdxi/lib/fixture-context.h      |  126 ++
 .../selftests/sdxi/lib/fixture-device.h       |   44 +
 tools/testing/selftests/sdxi/lib/lib.c        |  206 +++
 tools/testing/selftests/sdxi/lib/lib.h        |   61 +
 tools/testing/selftests/sdxi/nop.c            |   30 +
 tools/testing/selftests/sdxi/rkey.c           |  413 +++++
 58 files changed, 9603 insertions(+), 219 deletions(-)
 create mode 100644 drivers/dma/sdxi/admin.c
 create mode 100644 drivers/dma/sdxi/admin.h
 create mode 100644 drivers/dma/sdxi/amd/Makefile
 create mode 100644 drivers/dma/sdxi/amd/qos.c
 create mode 100644 drivers/dma/sdxi/amd/qos.h
 create mode 100644 drivers/dma/sdxi/amd/qos_kunit.c
 create mode 100644 drivers/dma/sdxi/error.c
 create mode 100644 drivers/dma/sdxi/error.h
 create mode 100644 drivers/dma/sdxi/intr.c
 create mode 100644 drivers/dma/sdxi/intr.h
 create mode 100644 drivers/dma/sdxi/ioctl.c
 create mode 100644 drivers/dma/sdxi/ioctl.h
 create mode 100644 drivers/dma/sdxi/keys.c
 create mode 100644 drivers/dma/sdxi/keys.h
 create mode 100644 drivers/dma/sdxi/process.c
 create mode 100644 drivers/dma/sdxi/process.h
 create mode 100644 drivers/dma/sdxi/sysfs.c
 create mode 100644 drivers/dma/sdxi/sysfs.h
 create mode 100644 drivers/dma/sdxi/trace.c
 create mode 100644 drivers/dma/sdxi/trace.h
 create mode 100644 drivers/dma/sdxi/vendor.c
 create mode 100644 drivers/dma/sdxi/vendor.h
 create mode 100644 drivers/dma/sdxi/version.h
 create mode 100644 include/uapi/linux/sdxi.h
 create mode 100644 tools/testing/selftests/sdxi/.gitignore
 create mode 100644 tools/testing/selftests/sdxi/Makefile
 create mode 100644 tools/testing/selftests/sdxi/admin_cmd.c
 create mode 100644 tools/testing/selftests/sdxi/config
 create mode 100644 tools/testing/selftests/sdxi/context.c
 create mode 100644 tools/testing/selftests/sdxi/copy.c
 create mode 100644 tools/testing/selftests/sdxi/device.c
 create mode 100644 tools/testing/selftests/sdxi/error.c
 create mode 100644 tools/testing/selftests/sdxi/lib/context.c
 create mode 100644 tools/testing/selftests/sdxi/lib/descriptor.h
 create mode 100644 tools/testing/selftests/sdxi/lib/fixture-context.h
 create mode 100644 tools/testing/selftests/sdxi/lib/fixture-device.h
 create mode 100644 tools/testing/selftests/sdxi/lib/lib.c
 create mode 100644 tools/testing/selftests/sdxi/lib/lib.h
 create mode 100644 tools/testing/selftests/sdxi/nop.c
 create mode 100644 tools/testing/selftests/sdxi/rkey.c

-- 
2.56.1

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 01/44] dmaengine: sdxi: cache hardware version for feature gating
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 02/44] dmaengine: sdxi: define key and error MMIO registers Wei Huang
                   ` (42 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Cache the SDXI version reported by MMIO_VERSION and add a small
comparison helper for later feature gating. This prepares the core
for SDXI HW discovery without changing existing behavior.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/device.c  |  9 ++++++---
 drivers/dma/sdxi/sdxi.h    |  9 +++++++++
 drivers/dma/sdxi/version.h | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 3 deletions(-)
 create mode 100644 drivers/dma/sdxi/version.h

diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index 265ebea1e350..e3fa39ee9988 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -326,9 +326,12 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	int err;
 
 	version = sdxi_read64(sdxi, SDXI_MMIO_VERSION);
-	dev_info(sdxi->dev, "SDXI %llu.%llu device found\n",
-		  FIELD_GET(SDXI_MMIO_VERSION_MAJOR, version),
-		  FIELD_GET(SDXI_MMIO_VERSION_MINOR, version));
+	sdxi->sdxi_version = (struct sdxi_version) {
+		.major = FIELD_GET(SDXI_MMIO_VERSION_MAJOR, version),
+		.minor = FIELD_GET(SDXI_MMIO_VERSION_MINOR, version),
+	};
+	dev_info(sdxi->dev, "SDXI %u.%u device found\n",
+		 sdxi->sdxi_version.major, sdxi->sdxi_version.minor);
 
 	/* Read capabilities and features. */
 	cap0 = sdxi_read64(sdxi, SDXI_MMIO_CAP0);
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 07044d837d9a..0bc08fc6ae75 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -17,6 +17,7 @@
 #include <linux/xarray.h>
 
 #include "mmio.h"
+#include "version.h"
 
 #define ID_TO_L1_INDEX(id)	((id) & 0x7F)
 
@@ -66,6 +67,8 @@ struct sdxi_dev {
 	void __iomem *ctrl_regs;	/* virt addr of ctrl registers */
 	void __iomem *dbs;		/* virt addr of doorbells */
 
+	struct sdxi_version sdxi_version;
+
 	/* hardware capabilities (from cap0 & cap1) */
 	u32 db_stride;			/* doorbell stride in bytes */
 	u16 max_cxtid;			/* Maximum context ID allowed. */
@@ -90,6 +93,12 @@ struct sdxi_dev {
 	const struct sdxi_bus_ops *bus_ops;
 };
 
+static inline bool sdxi_dev_compatible(const struct sdxi_dev *sdxi,
+				       struct sdxi_version version)
+{
+	return sdxi_version_ge(sdxi->sdxi_version, version);
+}
+
 /**
  * sdxi_alloc_vector() - Allocate an interrupt vector.
  *
diff --git a/drivers/dma/sdxi/version.h b/drivers/dma/sdxi/version.h
new file mode 100644
index 000000000000..79a9c996335f
--- /dev/null
+++ b/drivers/dma/sdxi/version.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI version constants and comparison helpers.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_VERSION_H
+#define DMA_SDXI_VERSION_H
+
+#include <linux/types.h>
+
+struct sdxi_version {
+	u8 major;
+	u8 minor;
+};
+
+#define SDXI_VERSION_1_0 ((struct sdxi_version){ .major = 1, .minor = 0 })
+#define SDXI_VERSION_1_1 ((struct sdxi_version){ .major = 1, .minor = 1 })
+
+static inline u16 sdxi_version_as_u16(struct sdxi_version version)
+{
+	return (version.major << 8) | version.minor;
+}
+
+static inline bool sdxi_version_ge(struct sdxi_version version,
+				   struct sdxi_version minimum)
+{
+	return sdxi_version_as_u16(version) >= sdxi_version_as_u16(minimum);
+}
+
+#endif /* DMA_SDXI_VERSION_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 02/44] dmaengine: sdxi: define key and error MMIO registers
  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 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 03/44] dmaengine: sdxi: define SDXI hardware layouts Wei Huang
                   ` (41 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add the MMIO offsets and fields required for SDXI RKey management,
error logging, function identity, and VF reporting. Keep the
definitions limited to registers consumed by subsequent driver patches.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/mmio.h | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/drivers/dma/sdxi/mmio.h b/drivers/dma/sdxi/mmio.h
index b040d2283fb2..d1d0c9ff1058 100644
--- a/drivers/dma/sdxi/mmio.h
+++ b/drivers/dma/sdxi/mmio.h
@@ -40,6 +40,7 @@ enum sdxi_reg {
 #define SDXI_MMIO_CTL0_FN_PASID_VL    BIT_ULL(2)
 #define SDXI_MMIO_CTL0_FN_ERR_INTR_EN BIT_ULL(4)
 #define SDXI_MMIO_CTL0_FN_PASID       GENMASK_ULL(27, 8)
+#define SDXI_MMIO_CTL0_FN_GRP_ID      GENMASK_ULL(63, 32)
 
 /* SDXI 1.0 Table 9-4: MMIO_CTL2 */
 #define SDXI_MMIO_CTL2_MAX_BUFFER  GENMASK_ULL(3, 0)
@@ -52,17 +53,18 @@ enum sdxi_reg {
 
 /* SDXI 1.0 Table 9-6: MMIO_CAP0 */
 #define SDXI_MMIO_CAP0_SFUNC          GENMASK_ULL(15, 0)
+#define SDXI_MMIO_CAP0_VF             BIT_ULL(16)
 #define SDXI_MMIO_CAP0_DB_STRIDE      GENMASK_ULL(22, 20)
 #define SDXI_MMIO_CAP0_MAX_DS_RING_SZ GENMASK_ULL(28, 24)
 
 /* SDXI 1.0 Table 9-7: MMIO_CAP1 */
-#define SDXI_MMIO_CAP1_MAX_BUFFER  GENMASK_ULL(3, 0)
-#define SDXI_MMIO_CAP1_RKEY_CAP    BIT_ULL(4)
-#define SDXI_MMIO_CAP1_MMIO64      BIT_ULL(6)
-#define SDXI_MMIO_CAP1_MAX_AKEY_SZ GENMASK_ULL(15, 12)
-#define SDXI_MMIO_CAP1_MAX_CXT     GENMASK_ULL(31, 16)
-
-#define SDXI_MMIO_CAP1_OPB_000_CAP GENMASK_ULL(63, 32)
+#define SDXI_MMIO_CAP1_MAX_BUFFER    GENMASK_ULL(3, 0)
+#define SDXI_MMIO_CAP1_RKEY_CAP      BIT_ULL(4)
+#define SDXI_MMIO_CAP1_MMIO64        BIT_ULL(6)
+#define SDXI_MMIO_CAP1_MAX_ERRLOG_SZ GENMASK_ULL(11, 8)
+#define SDXI_MMIO_CAP1_MAX_AKEY_SZ   GENMASK_ULL(15, 12)
+#define SDXI_MMIO_CAP1_MAX_CXT       GENMASK_ULL(31, 16)
+#define SDXI_MMIO_CAP1_OPB_000_CAP   GENMASK_ULL(63, 32)
 
 /* SDXI 1.0 Table 9-8: MMIO_VERSION */
 #define SDXI_MMIO_VERSION_MINOR GENMASK_ULL(7, 0)
@@ -72,7 +74,12 @@ enum sdxi_reg {
 #define SDXI_MMIO_CXT_L2_PTR GENMASK_ULL(63, 12)
 
 /* SDXI 1.0 Table 9-10: MMIO_RKEY */
-#define SDXI_MMIO_RKEY_EN BIT_ULL(0)
+#define SDXI_MMIO_RKEY_EN  BIT_ULL(0)
+#define SDXI_MMIO_RKEY_SZ  GENMASK_ULL(4, 1)
+#define SDXI_MMIO_RKEY_PTR GENMASK_ULL(63, 12)
+
+/* SDXI 1.0 Table 9-11: MMIO_ERR_CTL */
+#define SDXI_MMIO_ERR_CTL_EN BIT_ULL(0)
 
 /* SDXI 1.0 Table 9-12: MMIO_ERR_STS (write one to clear) */
 #define SDXI_MMIO_ERR_STS_STS_BIT BIT_ULL(0)
@@ -80,6 +87,8 @@ enum sdxi_reg {
 #define SDXI_MMIO_ERR_STS_ERR_BIT BIT_ULL(3)
 
 /* SDXI 1.0 Table 9-13: MMIO_ERR_CFG */
-#define SDXI_MMIO_ERR_CFG_EN BIT_ULL(0)
+#define SDXI_MMIO_ERR_CFG_EN  BIT_ULL(0)
+#define SDXI_MMIO_ERR_CFG_SZ  GENMASK_ULL(5, 1)
+#define SDXI_MMIO_ERR_CFG_PTR GENMASK_ULL(63, 12)
 
 #endif  /* DMA_SDXI_MMIO_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 03/44] dmaengine: sdxi: define SDXI hardware layouts
  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 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 04/44] dmaengine: sdxi: encode admin descriptors Wei Huang
                   ` (40 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add the RKey and error-log entry formats, AKey fields, operation-group
masks, and AdminGrp descriptor layouts needed by the native SDXI
interface. Preserve the existing type names, alignment, and completion
definitions. No functional change is introduced.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/hw.h | 80 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

diff --git a/drivers/dma/sdxi/hw.h b/drivers/dma/sdxi/hw.h
index 71579eb3ac26..6f294f9b6637 100644
--- a/drivers/dma/sdxi/hw.h
+++ b/drivers/dma/sdxi/hw.h
@@ -45,6 +45,13 @@ struct sdxi_cxt_L1_ent {
 #define SDXI_CXT_L1_ENT_PASID          GENMASK(19, 0)
 #define SDXI_CXT_L1_ENT_MAX_BUFFER     GENMASK(23, 20)
 	__le32 opb_000_enb;
+#define SDXI_OPB_000_ADMIN             BIT(2)
+#define SDXI_OPB_000_ATOMIC_FULL       BIT(3)
+#define SDXI_OPB_000_INTR              BIT(4)
+#define SDXI_OPB_000_ATOMIC_MIN        BIT(5)
+#define SDXI_OPB_000_ATOMIC            (SDXI_OPB_000_ATOMIC_FULL | \
+					SDXI_OPB_000_ATOMIC_MIN)
+#define SDXI_OPB_000_DISC_MASK         GENMASK(31, 6)
 	__u8 rsvd_0[8];
 } __packed __aligned(32);
 static_assert(sizeof(struct sdxi_cxt_L1_ent) == 32);
@@ -100,9 +107,14 @@ struct sdxi_akey_ent {
 	__le16 intr_num;
 #define SDXI_AKEY_ENT_VL BIT(0)
 #define SDXI_AKEY_ENT_IV BIT(1)
+#define SDXI_AKEY_ENT_PV BIT(2)
+#define SDXI_AKEY_ENT_STE BIT(3)
 #define SDXI_AKEY_ENT_INTR_NUM GENMASK(14, 4)
 	__le16 tgt_sfunc;
 	__le32 pasid;
+#define SDXI_AKEY_ENT_PASID GENMASK(19, 0)
+#define SDXI_AKEY_ENT_PR    BIT(29)
+#define SDXI_AKEY_ENT_PH    GENMASK(31, 30)
 	__le16 stag;
 	__u8   rsvd_0[2];
 	__le16 rkey;
@@ -110,6 +122,37 @@ struct sdxi_akey_ent {
 } __packed __aligned(16);
 static_assert(sizeof(struct sdxi_akey_ent) == 16);
 
+/* SDXI 1.0 Table 3-8: RKey Table Entry (RKEY_ENT) */
+struct sdxi_rkey_ent {
+	__le16 intr_num;
+#define SDXI_RKEY_ENT_VL BIT(0)
+#define SDXI_RKEY_ENT_IV BIT(1)
+#define SDXI_RKEY_ENT_PV BIT(2)
+#define SDXI_RKEY_ENT_STE BIT(3)
+#define SDXI_RKEY_ENT_INTR_NUM GENMASK(14, 4)
+	__le16 req_sfunc;
+	__le32 pasid;
+#define SDXI_RKEY_ENT_PASID GENMASK(19, 0)
+#define SDXI_RKEY_ENT_PR    BIT(29)
+#define SDXI_RKEY_ENT_PH    GENMASK(31, 30)
+	__le16 stag;
+	__u8 rsvd_0[6];
+} __packed __aligned(16);
+static_assert(sizeof(struct sdxi_rkey_ent) == 16);
+
+/* SDXI 1.0 Table 3-9: Error Log Header Entry (ERRLOG_HD_ENT) */
+struct sdxi_errlog_hd_ent {
+	__le32 opcode;
+	__le16 misc0;
+	__le16 cxt_num;
+	__le64 dsc_index;
+	__u8 rsvd_0[28];
+	__le16 err_class;
+	__u8 rsvd_1[2];
+	__le32 vendor[4];
+} __packed __aligned(64);
+static_assert(sizeof(struct sdxi_errlog_hd_ent) == 64);
+
 /* SDXI 1.0 Table 6-4: CST_BLK (Completion Status Block) */
 struct sdxi_cst_blk {
 	__le64 signal;
@@ -141,7 +184,9 @@ struct sdxi_desc {
 #define SDXI_DSC_VL  BIT(0)
 #define SDXI_DSC_SE  BIT(1)
 #define SDXI_DSC_FE  BIT(2)
+#define SDXI_DSC_CH  BIT(3)
 #define SDXI_DSC_CSR BIT(4)
+#define SDXI_DSC_RB  BIT(5)
 #define SDXI_DSC_SUBTYPE GENMASK(15, 8)
 #define SDXI_DSC_TYPE    GENMASK(26, 16)
 
@@ -227,6 +272,38 @@ struct sdxi_desc {
 /* For use with sync.cflags */
 #define SDXI_DSC_SYNC_FLT GENMASK(2, 0)
 
+		/* SDXI 1.0 Table 6-19: DSC_AKEY_UPD Descriptor Format */
+		define_sdxi_dsc(sdxi_dsc_akey_upd, akey_upd,
+				__u8 rsvd_0;
+				__u8 vflags;
+				__le16 vf_num;
+				__le16 cxt_start;
+				__le16 cxt_end;
+				__le16 akey_start;
+				__le16 akey_end;
+				__u8 rsvd_1[40];
+		);
+
+		/* SDXI 1.0 Table 6-23: DSC_RKEY_UPD Descriptor Format */
+		define_sdxi_dsc(sdxi_dsc_rkey_upd, rkey_upd,
+				__u8 rsvd_0;
+				__u8 vflags;
+				__le16 vf_num;
+				__u8 rsvd_1[4];
+				__le16 rkey_start;
+				__le16 rkey_end;
+				__u8 rsvd_2[40];
+		);
+
+		/* SDXI 1.1 Table 6-27: DSC_DISC Descriptor Format */
+		define_sdxi_dsc(sdxi_dsc_disc, disc,
+				__le16 index_start;
+				__le16 index_end;
+				__u8 rsvd_0[8];
+				__le64 addr;
+				__u8 rsvd_1[32];
+		);
+
 #undef define_sdxi_dsc
 	};
 } __packed __aligned(64);
@@ -249,9 +326,12 @@ enum sdxi_dsc_subtype {
 
 	/* Administrative */
 	SDXI_DSC_OP_SUBTYPE_CXT_UPD      = 0x01,
+	SDXI_DSC_OP_SUBTYPE_AKEY_UPD     = 0x02,
 	SDXI_DSC_OP_SUBTYPE_CXT_START_NM = 0x03,
 	SDXI_DSC_OP_SUBTYPE_CXT_STOP     = 0x04,
 	SDXI_DSC_OP_SUBTYPE_SYNC         = 0x06,
+	SDXI_DSC_OP_SUBTYPE_RKEY_UPD     = 0x07,
+	SDXI_DSC_OP_SUBTYPE_DISC         = 0x09,
 };
 
 #endif /* DMA_SDXI_HW_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 04/44] dmaengine: sdxi: encode admin descriptors
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (2 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 03/44] dmaengine: sdxi: define SDXI hardware layouts Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 05/44] dmaengine: sdxi: support timeout-aware completion waits Wei Huang
                   ` (39 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Extend the existing descriptor helpers with key-range synchronization,
along with new AKey and RKey update operations. Also introduce a
descriptor discovery helper based on SDXI 1.1 Spec. For each layout
and validation path, add independent packing-based kunit tests.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/descriptor.c       |  89 +++++++++++++++++
 drivers/dma/sdxi/descriptor.h       |  27 +++++
 drivers/dma/sdxi/descriptor_kunit.c | 150 ++++++++++++++++++++++++++++
 3 files changed, 266 insertions(+)

diff --git a/drivers/dma/sdxi/descriptor.c b/drivers/dma/sdxi/descriptor.c
index 25b5d2862e79..5aa1752b0222 100644
--- a/drivers/dma/sdxi/descriptor.c
+++ b/drivers/dma/sdxi/descriptor.c
@@ -7,6 +7,7 @@
 
 #include <kunit/test-bug.h>
 #include <kunit/visibility.h>
+#include <linux/align.h>
 #include <linux/bitfield.h>
 #include <linux/bug.h>
 #include <linux/range.h>
@@ -211,6 +212,8 @@ int sdxi_encode_sync(struct sdxi_desc *desc, const struct sdxi_sync *params)
 			.cflags = cflags,
 			.cxt_start = cpu_to_le16(params->range.cxt_start),
 			.cxt_end = cpu_to_le16(params->range.cxt_end),
+			.key_start = cpu_to_le16(params->key_start),
+			.key_end = cpu_to_le16(params->key_end),
 			.csb_ptr = cpu_to_le64(csb_ptr),
 		},
 	};
@@ -218,3 +221,89 @@ int sdxi_encode_sync(struct sdxi_desc *desc, const struct sdxi_sync *params)
 	return 0;
 }
 EXPORT_SYMBOL_IF_KUNIT(sdxi_encode_sync);
+
+int sdxi_encode_disc(struct sdxi_desc *desc, const struct sdxi_disc *params)
+{
+	u64 csb_ptr;
+	u32 opcode;
+
+	if (params->index_start > params->index_end)
+		return -EINVAL;
+	if (params->index_end > 2047)
+		return -EINVAL;
+	if (!IS_ALIGNED(params->addr, 16))
+		return -EINVAL;
+
+	opcode = FIELD_PREP(SDXI_DSC_SUBTYPE, SDXI_DSC_OP_SUBTYPE_DISC) |
+		 FIELD_PREP(SDXI_DSC_TYPE, SDXI_DSC_OP_TYPE_ADMIN);
+	csb_ptr = FIELD_PREP(SDXI_DSC_NP, 1);
+
+	*desc = (typeof(*desc)) {
+		.disc = (typeof(desc->disc)) {
+			.opcode = cpu_to_le32(opcode),
+			.index_start = cpu_to_le16(params->index_start),
+			.index_end = cpu_to_le16(params->index_end),
+			.addr = cpu_to_le64(params->addr),
+			.csb_ptr = cpu_to_le64(csb_ptr),
+		},
+	};
+
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_encode_disc);
+
+int sdxi_encode_akey_upd(struct sdxi_desc *desc,
+			 const struct sdxi_akey_upd *params)
+{
+	u64 csb_ptr;
+	u32 opcode;
+
+	if (params->range.cxt_start > params->range.cxt_end)
+		return -EINVAL;
+	if (params->akey_start > params->akey_end)
+		return -EINVAL;
+
+	opcode = FIELD_PREP(SDXI_DSC_SUBTYPE, SDXI_DSC_OP_SUBTYPE_AKEY_UPD) |
+		 FIELD_PREP(SDXI_DSC_TYPE, SDXI_DSC_OP_TYPE_ADMIN);
+	csb_ptr = FIELD_PREP(SDXI_DSC_NP, 1);
+
+	*desc = (typeof(*desc)) {
+		.akey_upd = (typeof(desc->akey_upd)) {
+			.opcode = cpu_to_le32(opcode),
+			.cxt_start = cpu_to_le16(params->range.cxt_start),
+			.cxt_end = cpu_to_le16(params->range.cxt_end),
+			.akey_start = cpu_to_le16(params->akey_start),
+			.akey_end = cpu_to_le16(params->akey_end),
+			.csb_ptr = cpu_to_le64(csb_ptr),
+		},
+	};
+
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_encode_akey_upd);
+
+int sdxi_encode_rkey_upd(struct sdxi_desc *desc,
+			 const struct sdxi_rkey_upd *params)
+{
+	u64 csb_ptr;
+	u32 opcode;
+
+	if (params->rkey_start > params->rkey_end)
+		return -EINVAL;
+
+	opcode = FIELD_PREP(SDXI_DSC_SUBTYPE, SDXI_DSC_OP_SUBTYPE_RKEY_UPD) |
+		 FIELD_PREP(SDXI_DSC_TYPE, SDXI_DSC_OP_TYPE_ADMIN);
+	csb_ptr = FIELD_PREP(SDXI_DSC_NP, 1);
+
+	*desc = (typeof(*desc)) {
+		.rkey_upd = (typeof(desc->rkey_upd)) {
+			.opcode = cpu_to_le32(opcode),
+			.rkey_start = cpu_to_le16(params->rkey_start),
+			.rkey_end = cpu_to_le16(params->rkey_end),
+			.csb_ptr = cpu_to_le64(csb_ptr),
+		},
+	};
+
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_encode_rkey_upd);
diff --git a/drivers/dma/sdxi/descriptor.h b/drivers/dma/sdxi/descriptor.h
index cf071a281941..a1c3b6cf243b 100644
--- a/drivers/dma/sdxi/descriptor.h
+++ b/drivers/dma/sdxi/descriptor.h
@@ -138,8 +138,35 @@ struct sdxi_sync {
 		SDXI_SYNC_FLT_FN   = 0x4,
 	} filter;
 	struct sdxi_cxt_range range;
+	u16 key_start;
+	u16 key_end;
 };
 
 int sdxi_encode_sync(struct sdxi_desc *desc, const struct sdxi_sync *params);
 
+struct sdxi_disc {
+	u16 index_start;
+	u16 index_end;
+	dma_addr_t addr;
+};
+
+int sdxi_encode_disc(struct sdxi_desc *desc, const struct sdxi_disc *params);
+
+struct sdxi_akey_upd {
+	struct sdxi_cxt_range range;
+	u16 akey_start;
+	u16 akey_end;
+};
+
+int sdxi_encode_akey_upd(struct sdxi_desc *desc,
+			 const struct sdxi_akey_upd *params);
+
+struct sdxi_rkey_upd {
+	u16 rkey_start;
+	u16 rkey_end;
+};
+
+int sdxi_encode_rkey_upd(struct sdxi_desc *desc,
+			 const struct sdxi_rkey_upd *params);
+
 #endif /* DMA_SDXI_DESCRIPTOR_H */
diff --git a/drivers/dma/sdxi/descriptor_kunit.c b/drivers/dma/sdxi/descriptor_kunit.c
index fdca024e3e31..c5f3c9eb6224 100644
--- a/drivers/dma/sdxi/descriptor_kunit.c
+++ b/drivers/dma/sdxi/descriptor_kunit.c
@@ -163,6 +163,54 @@ static const struct packed_field_u16 cxt_upd_subfields[] = {
 	cxt_upd_field(95, 80, cxt_end),
 };
 
+/* DSC_AKEY_UPD */
+struct unpacked__akey_upd {
+	u16 cxt_start;
+	u16 cxt_end;
+	u16 akey_start;
+	u16 akey_end;
+};
+
+#define akey_upd_field(_high, _low, _member) \
+	desc_field(_high, _low, struct unpacked__akey_upd, _member)
+
+static const struct packed_field_u16 akey_upd_subfields[] = {
+	akey_upd_field(79, 64, cxt_start),
+	akey_upd_field(95, 80, cxt_end),
+	akey_upd_field(111, 96, akey_start),
+	akey_upd_field(127, 112, akey_end),
+};
+
+/* DSC_RKEY_UPD */
+struct unpacked__rkey_upd {
+	u16 rkey_start;
+	u16 rkey_end;
+};
+
+#define rkey_upd_field(_high, _low, _member) \
+	desc_field(_high, _low, struct unpacked__rkey_upd, _member)
+
+static const struct packed_field_u16 rkey_upd_subfields[] = {
+	rkey_upd_field(111, 96, rkey_start),
+	rkey_upd_field(127, 112, rkey_end),
+};
+
+/* DSC_DISC */
+struct unpacked__disc {
+	u16 index_start;
+	u16 index_end;
+	u64 addr;
+};
+
+#define disc_field(_high, _low, _member) \
+	desc_field(_high, _low, struct unpacked__disc, _member)
+
+static const struct packed_field_u16 disc_subfields[] = {
+	disc_field(42, 32, index_start),
+	disc_field(58, 48, index_end),
+	disc_field(191, 132, addr),
+};
+
 /* DSC_GENERIC */
 struct unpacked_desc {
 	u64 csb_ptr;
@@ -182,6 +230,9 @@ struct unpacked_desc {
 		struct unpacked__cxt_start cxt_start;
 		struct unpacked__cxt_stop cxt_stop;
 		struct unpacked__cxt_upd cxt_upd;
+		struct unpacked__akey_upd akey_upd;
+		struct unpacked__rkey_upd rkey_upd;
+		struct unpacked__disc disc;
 	};
 };
 
@@ -234,6 +285,9 @@ define_unpack_fn(sync)
 define_unpack_fn(cxt_start)
 define_unpack_fn(cxt_stop)
 define_unpack_fn(cxt_upd)
+define_unpack_fn(akey_upd)
+define_unpack_fn(rkey_upd)
+define_unpack_fn(disc)
 
 static void desc_poison(struct sdxi_desc *d)
 {
@@ -489,6 +543,8 @@ static void sync(struct kunit *t)
 	struct sdxi_sync sync = {
 		.filter = SDXI_SYNC_FLT_STOP,
 		.range = sdxi_cxt_range(1, U16_MAX),
+		.key_start = 2,
+		.key_end = 3,
 	};
 	struct sdxi_desc desc;
 	struct unpacked_desc unpacked;
@@ -503,6 +559,97 @@ static void sync(struct kunit *t)
 	KUNIT_EXPECT_EQ(t, unpacked.sync.flt, SDXI_SYNC_FLT_STOP);
 	KUNIT_EXPECT_EQ(t, unpacked.sync.cxt_start, 1);
 	KUNIT_EXPECT_EQ(t, unpacked.sync.cxt_end, U16_MAX);
+	KUNIT_EXPECT_EQ(t, unpacked.sync.key_start, 2);
+	KUNIT_EXPECT_EQ(t, unpacked.sync.key_end, 3);
+}
+
+static void akey_upd(struct kunit *t)
+{
+	struct sdxi_akey_upd update = {
+		.range = sdxi_cxt_range(3, 5),
+		.akey_start = 7,
+		.akey_end = 11,
+	};
+	struct unpacked_desc unpacked;
+	struct sdxi_desc desc;
+
+	desc_poison(&desc);
+	KUNIT_ASSERT_EQ(t, 0, sdxi_encode_akey_upd(&desc, &update));
+	unpack_akey_upd(&unpacked, &desc);
+
+	KUNIT_EXPECT_EQ(t, unpacked.type, SDXI_DSC_OP_TYPE_ADMIN);
+	KUNIT_EXPECT_EQ(t, unpacked.subtype, SDXI_DSC_OP_SUBTYPE_AKEY_UPD);
+	KUNIT_EXPECT_EQ(t, unpacked.akey_upd.cxt_start, 3);
+	KUNIT_EXPECT_EQ(t, unpacked.akey_upd.cxt_end, 5);
+	KUNIT_EXPECT_EQ(t, unpacked.akey_upd.akey_start, 7);
+	KUNIT_EXPECT_EQ(t, unpacked.akey_upd.akey_end, 11);
+
+	update.range = (struct sdxi_cxt_range) {
+		.cxt_start = 2,
+		.cxt_end = 1,
+	};
+	KUNIT_EXPECT_EQ(t, -EINVAL, sdxi_encode_akey_upd(&desc, &update));
+
+	update.range = sdxi_cxt_range_single(1);
+	update.akey_start = 2;
+	update.akey_end = 1;
+	KUNIT_EXPECT_EQ(t, -EINVAL, sdxi_encode_akey_upd(&desc, &update));
+}
+
+static void rkey_upd(struct kunit *t)
+{
+	struct sdxi_rkey_upd update = {
+		.rkey_start = 13,
+		.rkey_end = 17,
+	};
+	struct unpacked_desc unpacked;
+	struct sdxi_desc desc;
+
+	desc_poison(&desc);
+	KUNIT_ASSERT_EQ(t, 0, sdxi_encode_rkey_upd(&desc, &update));
+	unpack_rkey_upd(&unpacked, &desc);
+
+	KUNIT_EXPECT_EQ(t, unpacked.type, SDXI_DSC_OP_TYPE_ADMIN);
+	KUNIT_EXPECT_EQ(t, unpacked.subtype, SDXI_DSC_OP_SUBTYPE_RKEY_UPD);
+	KUNIT_EXPECT_EQ(t, unpacked.rkey_upd.rkey_start, 13);
+	KUNIT_EXPECT_EQ(t, unpacked.rkey_upd.rkey_end, 17);
+
+	update.rkey_start = 2;
+	update.rkey_end = 1;
+	KUNIT_EXPECT_EQ(t, -EINVAL, sdxi_encode_rkey_upd(&desc, &update));
+}
+
+static void disc(struct kunit *t)
+{
+	struct sdxi_disc discovery = {
+		.index_start = 1,
+		.index_end = 31,
+		.addr = 0x12340,
+	};
+	struct unpacked_desc unpacked;
+	struct sdxi_desc desc;
+
+	desc_poison(&desc);
+	KUNIT_ASSERT_EQ(t, 0, sdxi_encode_disc(&desc, &discovery));
+	unpack_disc(&unpacked, &desc);
+
+	KUNIT_EXPECT_EQ(t, unpacked.type, SDXI_DSC_OP_TYPE_ADMIN);
+	KUNIT_EXPECT_EQ(t, unpacked.subtype, SDXI_DSC_OP_SUBTYPE_DISC);
+	KUNIT_EXPECT_EQ(t, unpacked.disc.index_start, 1);
+	KUNIT_EXPECT_EQ(t, unpacked.disc.index_end, 31);
+	KUNIT_EXPECT_EQ(t, unpacked.disc.addr, 0x1234);
+
+	discovery.index_start = 2;
+	discovery.index_end = 1;
+	KUNIT_EXPECT_EQ(t, -EINVAL, sdxi_encode_disc(&desc, &discovery));
+
+	discovery.index_start = 0;
+	discovery.index_end = 2048;
+	KUNIT_EXPECT_EQ(t, -EINVAL, sdxi_encode_disc(&desc, &discovery));
+
+	discovery.index_end = 1;
+	discovery.addr = 0x12341;
+	KUNIT_EXPECT_EQ(t, -EINVAL, sdxi_encode_disc(&desc, &discovery));
 }
 
 static struct kunit_case generic_desc_tcs[] = {
@@ -513,6 +660,9 @@ static struct kunit_case generic_desc_tcs[] = {
 	KUNIT_CASE(cxt_stop),
 	KUNIT_CASE(cxt_upd),
 	KUNIT_CASE(sync),
+	KUNIT_CASE(akey_upd),
+	KUNIT_CASE(rkey_upd),
+	KUNIT_CASE(disc),
 	{}
 };
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 05/44] dmaengine: sdxi: support timeout-aware completion waits
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (3 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 04/44] dmaengine: sdxi: encode admin descriptors Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 06/44] dmaengine: sdxi: support the full context table hierarchy Wei Huang
                   ` (38 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Extend the completion status block API with interruptible and
non-interruptible timeout waits and raw flag access. These extensions
will be used by the descriptor submission code added later. In the
meanhile, keep existing CQ ownership and simple completion mode
unchanged.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/completion.c | 37 ++++++++++++++++++++++++++++-------
 drivers/dma/sdxi/completion.h |  4 ++++
 2 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/drivers/dma/sdxi/completion.c b/drivers/dma/sdxi/completion.c
index dacd1cac6303..555a24c31341 100644
--- a/drivers/dma/sdxi/completion.c
+++ b/drivers/dma/sdxi/completion.c
@@ -7,8 +7,10 @@
 #include <kunit/visibility.h>
 #include <linux/bitfield.h>
 #include <linux/bitmap.h>
+#include <linux/delay.h>
 #include <linux/dma-mapping.h>
-#include <linux/iopoll.h>
+#include <linux/jiffies.h>
+#include <linux/sched/signal.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/wait.h>
@@ -176,14 +178,35 @@ enum sdxi_completion_state sdxi_cst_blk_check(const struct sdxi_cst_blk *cst)
 
 int sdxi_cst_blk_poll(const struct sdxi_cst_blk *cst)
 {
+	return sdxi_cst_blk_wait_timeout(cst, USEC_PER_SEC / USEC_PER_MSEC,
+					 false);
+}
+
+int sdxi_cst_blk_wait_timeout(const struct sdxi_cst_blk *cst,
+			      unsigned int timeout_ms, bool interruptible)
+{
+	unsigned long deadline = 0;
 	enum sdxi_completion_state state;
-	int ret;
 
-	ret = read_poll_timeout(sdxi_cst_blk_check, state,
-				state != SDXI_COMPLETION_PENDING,
-				10, USEC_PER_SEC, false, cst);
-	if (ret)
-		return ret;		/* -ETIMEDOUT */
+	if (timeout_ms)
+		deadline = jiffies + msecs_to_jiffies(timeout_ms);
+
+	for (;;) {
+		state = sdxi_cst_blk_check(cst);
+		if (state != SDXI_COMPLETION_PENDING)
+			break;
+		if (interruptible && signal_pending(current))
+			return -ERESTARTSYS;
+		if (timeout_ms && time_after(jiffies, deadline))
+			return -ETIMEDOUT;
+		usleep_range(50, 200);
+	}
 
 	return state == SDXI_COMPLETION_ERROR ? -EIO : 0;
 }
+
+u32 sdxi_cst_blk_flags(const struct sdxi_cst_blk *cst)
+{
+	dma_rmb();
+	return le32_to_cpu(READ_ONCE(cst->flags));
+}
diff --git a/drivers/dma/sdxi/completion.h b/drivers/dma/sdxi/completion.h
index 18c89701d1d6..9a7a0838bb3d 100644
--- a/drivers/dma/sdxi/completion.h
+++ b/drivers/dma/sdxi/completion.h
@@ -47,6 +47,10 @@ static inline void sdxi_cst_blk_init(struct sdxi_cst_blk *cst)
 }
 
 int __must_check sdxi_cst_blk_poll(const struct sdxi_cst_blk *cst);
+int __must_check sdxi_cst_blk_wait_timeout(const struct sdxi_cst_blk *cst,
+					   unsigned int timeout_ms,
+					   bool interruptible);
 enum sdxi_completion_state sdxi_cst_blk_check(const struct sdxi_cst_blk *cst);
+u32 sdxi_cst_blk_flags(const struct sdxi_cst_blk *cst);
 
 #endif /* DMA_SDXI_COMPLETION_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 06/44] dmaengine: sdxi: support the full context table hierarchy
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (4 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 05/44] dmaengine: sdxi: support timeout-aware completion waits Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 07/44] dmaengine: sdxi: implement synchronous admin operations Wei Huang
                   ` (37 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Remove the single-L1-table context limit and allocate hardware L1 tables
on demand for the full context ID range reported by the function. Keep
the existing xarray as the authoritative context registry, and serialize
table publication and teardown separately.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/context.c | 78 +++++++++++++++++++++++++++++++++++---
 drivers/dma/sdxi/device.c  | 28 ++------------
 drivers/dma/sdxi/sdxi.h    | 11 ++++--
 3 files changed, 84 insertions(+), 33 deletions(-)

diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index 0979fa35346b..d75668e89e61 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -286,13 +286,51 @@ static void invalidate_L1_entry(struct sdxi_cxt_L1_ent *ent)
 	*ent = (typeof(*ent)) { 0 };
 }
 
+static struct sdxi_cxt_L1_table *
+sdxi_get_L1_table(struct sdxi_dev *sdxi, u16 l2_idx)
+{
+	struct sdxi_cxt_L1_table *table;
+	struct sdxi_cxt_L2_ent *L2_ent;
+	dma_addr_t dma;
+	u64 lv01_ptr;
+
+	lockdep_assert_held(&sdxi->cxt_lock);
+
+	table = sdxi->L1_tables[l2_idx];
+	if (table)
+		return table;
+
+	table = dmam_alloc_coherent(sdxi->dev, sizeof(*table), &dma, GFP_KERNEL);
+	if (!table)
+		return NULL;
+	memset(table, 0, sizeof(*table));
+
+	L2_ent = &sdxi->L2_table->entry[l2_idx];
+	lv01_ptr = FIELD_PREP(SDXI_CXT_L2_ENT_LV01_PTR,
+			      dma >> ilog2(SZ_4K));
+	L2_ent->lv01_ptr = cpu_to_le64(lv01_ptr);
+	dma_wmb();
+	FIELD_MODIFY(SDXI_CXT_L2_ENT_VL, &lv01_ptr, 1);
+	WRITE_ONCE(L2_ent->lv01_ptr, cpu_to_le64(lv01_ptr));
+
+	sdxi->L1_tables[l2_idx] = table;
+	sdxi->L1_dma[l2_idx] = dma;
+
+	return table;
+}
 
-/* Publish the context control block and L1 entry to hardware. */
+/*
+ * Publish the context control structure hierarchy to the SDXI
+ * implementation. L1 pages remain allocated until device teardown so a
+ * cached L2 pointer can never refer to freed memory.
+ */
 static int sdxi_publish_cxt(const struct sdxi_cxt *cxt)
 {
 	struct sdxi_cxt_ctl_cfg ctl_cfg;
 	struct sdxi_cxt_L1_cfg L1_cfg;
+	struct sdxi_cxt_L1_table *L1_table;
 	struct sdxi_cxt_L1_ent *ent;
+	u16 l2_idx;
 	u8 l1_idx;
 	int err;
 
@@ -314,9 +352,16 @@ static int sdxi_publish_cxt(const struct sdxi_cxt *cxt)
 	if (err)
 		return err;
 
+	l2_idx = ID_TO_L2_INDEX(cxt->id);
 	l1_idx = ID_TO_L1_INDEX(cxt->id);
 
-	ent = &cxt->sdxi->L1_table->entry[l1_idx];
+	mutex_lock(&cxt->sdxi->cxt_lock);
+	L1_table = sdxi_get_L1_table(cxt->sdxi, l2_idx);
+	if (!L1_table) {
+		err = -ENOMEM;
+		goto out_unlock;
+	}
+	ent = &L1_table->entry[l1_idx];
 
 	L1_cfg = (typeof(L1_cfg)) {
 		.ka          = 1,
@@ -329,7 +374,15 @@ static int sdxi_publish_cxt(const struct sdxi_cxt *cxt)
 		.opb_000_enb = cxt->sdxi->op_grp_cap,
 	};
 
-	return configure_L1_entry(ent, &L1_cfg);
+	err = configure_L1_entry(ent, &L1_cfg);
+	if (!err)
+		cxt->sdxi->L1_users[l2_idx]++;
+
+out_unlock:
+	mutex_unlock(&cxt->sdxi->cxt_lock);
+	if (err)
+		invalidate_cxt_ctl(cxt->cxt_ctl);
+	return err;
 }
 
 static void free_admin_cxt(void *ptr)
@@ -561,7 +614,9 @@ void sdxi_cxt_submit(struct sdxi_cxt *cxt, struct sdxi_slot slot,
 int sdxi_cxt_exit(struct sdxi_cxt *cxt)
 {
 	struct sdxi_dev *sdxi = cxt->sdxi;
-	struct sdxi_cxt_L1_ent *ent;
+	struct sdxi_cxt_L1_table *L1_table;
+	u16 l2_idx = ID_TO_L2_INDEX(cxt->id);
+	u8 l1_idx = ID_TO_L1_INDEX(cxt->id);
 	u8 state;
 	int err;
 
@@ -572,9 +627,15 @@ int sdxi_cxt_exit(struct sdxi_cxt *cxt)
 	if (state != CXTV_STOP_SW && state != CXTV_STOP_FN && state != CXTV_ERR_FN)
 		return -EBUSY;
 
-	ent = &sdxi->L1_table->entry[ID_TO_L1_INDEX(cxt->id)];
-	invalidate_L1_entry(ent);
+	mutex_lock(&sdxi->cxt_lock);
+	L1_table = sdxi->L1_tables[l2_idx];
+	if (WARN_ON_ONCE(!L1_table)) {
+		mutex_unlock(&sdxi->cxt_lock);
+		return -EIO;
+	}
+	invalidate_L1_entry(&L1_table->entry[l1_idx]);
 	invalidate_cxt_ctl(cxt->cxt_ctl);
+	mutex_unlock(&sdxi->cxt_lock);
 
 	err = sdxi_cxt_update(cxt);
 	if (err) {
@@ -584,6 +645,11 @@ int sdxi_cxt_exit(struct sdxi_cxt *cxt)
 		return err;
 	}
 
+	mutex_lock(&sdxi->cxt_lock);
+	if (!WARN_ON_ONCE(!sdxi->L1_users[l2_idx]))
+		sdxi->L1_users[l2_idx]--;
+	mutex_unlock(&sdxi->cxt_lock);
+
 	sdxi_free_cxt(cxt);
 	return 0;
 }
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index e3fa39ee9988..c5b5b39a7fe5 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -321,8 +321,7 @@ static int sdxi_fn_prepare(struct sdxi_dev *sdxi)
  */
 static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 {
-	u64 version, cap0, cap1, ctl2, cxt_l2, lv01_ptr;
-	struct sdxi_cxt_L2_ent *L2_ent;
+	u64 version, cap0, cap1, ctl2, cxt_l2;
 	int err;
 
 	version = sdxi_read64(sdxi, SDXI_MMIO_VERSION);
@@ -344,12 +343,7 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	dma_set_max_seg_size(sdxi->dev,
 			     min((u64)SZ_2M << sdxi->max_buffer, U32_MAX));
 
-	/*
-	 * Constrain the number of client contexts supported by the
-	 * driver to what fits in a single L1 table.
-	 */
-	sdxi->max_cxtid = min(SDXI_L1_TABLE_ENTRIES - 1,
-			      FIELD_GET(SDXI_MMIO_CAP1_MAX_CXT, cap1));
+	sdxi->max_cxtid = FIELD_GET(SDXI_MMIO_CAP1_MAX_CXT, cap1);
 
 	/* Apply our configuration. */
 	ctl2 = FIELD_PREP(SDXI_MMIO_CTL2_MAX_CXT, sdxi->max_cxtid);
@@ -367,26 +361,11 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 					     &sdxi->L2_dma, GFP_KERNEL);
 	if (!sdxi->L2_table)
 		return -ENOMEM;
+	memset(sdxi->L2_table, 0, sizeof(*sdxi->L2_table));
 
 	cxt_l2 = FIELD_PREP(SDXI_MMIO_CXT_L2_PTR, sdxi->L2_dma >> ilog2(SZ_4K));
 	sdxi_write64(sdxi, SDXI_MMIO_CXT_L2, cxt_l2);
 
-	/* SDXI 1.0 4.1.8.3 Context Level 1 Table Setup */
-	sdxi->L1_table = dmam_alloc_coherent(sdxi->dev,
-					     sizeof(*sdxi->L1_table),
-					     &sdxi->L1_dma, GFP_KERNEL);
-	if (!sdxi->L1_table)
-		return -ENOMEM;
-	/*
-	 * SDXI 1.0 4.1.8.3.c: Initialize the Context level 2 table to
-	 * point to the Context Level 1 [table].
-	 */
-	L2_ent = &sdxi->L2_table->entry[0];
-	lv01_ptr = FIELD_PREP(SDXI_CXT_L2_ENT_VL, 1) |
-		   FIELD_PREP(SDXI_CXT_L2_ENT_LV01_PTR,
-			      sdxi->L1_dma >> ilog2(SZ_4K));
-	L2_ent->lv01_ptr = cpu_to_le64(lv01_ptr);
-
 	/*
 	 * SDXI 1.0 4.1.8.4 Administrative Context
 	 *
@@ -445,6 +424,7 @@ int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 	sdxi->bus_ops = ops;
 	ida_init(&sdxi->vectors);
 	xa_init_flags(&sdxi->client_cxts, XA_FLAGS_ALLOC1);
+	mutex_init(&sdxi->cxt_lock);
 	dev_set_drvdata(dev, sdxi);
 
 	err = sdxi->bus_ops->init(sdxi);
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 0bc08fc6ae75..6d2b9ba88182 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -13,13 +13,16 @@
 #include <linux/dev_printk.h>
 #include <linux/idr.h>
 #include <linux/io.h>
+#include <linux/mutex.h>
 #include <linux/types.h>
 #include <linux/xarray.h>
 
+#include "hw.h"
 #include "mmio.h"
 #include "version.h"
 
-#define ID_TO_L1_INDEX(id)	((id) & 0x7F)
+#define ID_TO_L2_INDEX(id)	(((id) >> 7) & 0x1ff)
+#define ID_TO_L1_INDEX(id)	((id) & 0x7f)
 
 #define DESC_RING_BASE_PTR_SHIFT	6
 #define CXT_STATUS_PTR_SHIFT		4
@@ -77,8 +80,10 @@ struct sdxi_dev {
 
 	struct sdxi_cxt_L2_table *L2_table;
 	dma_addr_t L2_dma;
-	struct sdxi_cxt_L1_table *L1_table;
-	dma_addr_t L1_dma;
+	struct sdxi_cxt_L1_table *L1_tables[SDXI_L2_TABLE_ENTRIES];
+	dma_addr_t L1_dma[SDXI_L2_TABLE_ENTRIES];
+	u16 L1_users[SDXI_L2_TABLE_ENTRIES];
+	struct mutex cxt_lock;		/* context table serialization */
 
 	struct dma_pool *cxt_ctl_pool;
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 07/44] dmaengine: sdxi: implement synchronous admin operations
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (5 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 06/44] dmaengine: sdxi: support the full context table hierarchy Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 08/44] dmaengine: sdxi: add core RKey table management Wei Huang
                   ` (36 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Move context start and stop submission out of existing context
management code into new admin management files, and build the final
AdminGrp helper layer on the existing submission queue and completion
queue. Implement AKey and RKey sync helpers as part of AdminGrp. To
support vendor modules that will be introduced later, a discovery
operations plus a generic exported descriptor path are also added.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Makefile  |   1 +
 drivers/dma/sdxi/admin.c   | 281 +++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/admin.h   |  45 ++++++
 drivers/dma/sdxi/context.c | 136 +-----------------
 drivers/dma/sdxi/context.h |  13 --
 drivers/dma/sdxi/device.c  |  33 +++++
 drivers/dma/sdxi/dma.c     |  22 ++-
 drivers/dma/sdxi/pci.c     |  16 +++
 drivers/dma/sdxi/sdxi.h    |   8 ++
 9 files changed, 402 insertions(+), 153 deletions(-)
 create mode 100644 drivers/dma/sdxi/admin.c
 create mode 100644 drivers/dma/sdxi/admin.h

diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index 3a1d36997b44..bddd1278096c 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_SDXI_CORE) += sdxi-core.o
 sdxi-core-y := \
+	admin.o       \
 	completion.o  \
 	context.o     \
 	descriptor.o  \
diff --git a/drivers/dma/sdxi/admin.c b/drivers/dma/sdxi/admin.c
new file mode 100644
index 000000000000..ed3890de8cc1
--- /dev/null
+++ b/drivers/dma/sdxi/admin.c
@@ -0,0 +1,281 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI administrative command submission.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/export.h>
+#include <linux/kernel.h>
+#include <linux/types.h>
+
+#include "admin.h"
+#include "completion.h"
+#include "descriptor.h"
+#include "ring.h"
+
+#define SDXI_ADMIN_DEFAULT_TIMEOUT_MS	5000U
+#define SDXI_ADMIN_DRAIN_TIMEOUT_MS	1000U
+
+static int sdxi_adm_submit(struct sdxi_cxt *adm,
+			   const struct sdxi_desc *descs, size_t nr_descs,
+			   unsigned int timeout_ms, u32 *cst_flags,
+			   bool interruptible)
+{
+	struct sdxi_ring_resv resv;
+	struct sdxi_completion comp;
+	struct sdxi_slot slot;
+	bool completed = false;
+	int wait_ret;
+	size_t i;
+	int ret;
+
+	if (!adm || !sdxi_cxt_is_admin(adm) || !adm->ring_state)
+		return -ENODEV;
+	if (!nr_descs)
+		return -EINVAL;
+
+	/* Keep reservations from becoming visible before they are populated. */
+	mutex_lock(&adm->sdxi->admin_lock);
+	if (adm->sdxi->admin_dead) {
+		ret = -EIO;
+		goto out_unlock;
+	}
+	ret = sdxi_cst_pool_get_wait(adm->cst_pool, &comp);
+	if (ret)
+		goto out_unlock;
+	ret = sdxi_ring_reserve(adm->ring_state, nr_descs, &resv);
+	if (ret) {
+		sdxi_cst_pool_put(adm->cst_pool, comp.cst_blk);
+		goto out_unlock;
+	}
+
+	for (i = 0; i < nr_descs; i++) {
+		slot = sdxi_ring_resv_slot(&resv, i);
+		*sdxi_slot_desc(slot) = descs[i];
+		if (i + 1 == nr_descs)
+			sdxi_cxt_submit(adm, slot, &comp, SDXI_CXT_SUBMIT_KICK);
+		else
+			sdxi_cxt_submit(adm, slot, NULL, 0);
+	}
+
+	ret = sdxi_cst_blk_wait_timeout(comp.cst_blk, timeout_ms, interruptible);
+	if (ret == -ETIMEDOUT || ret == -ERESTARTSYS) {
+		/*
+		 * The descriptor can still access caller-owned DMA memory after
+		 * a timed or interrupted wait. Drain it before returning so the
+		 * caller can safely release any associated mappings.
+		 */
+		wait_ret = sdxi_cst_blk_wait_timeout(comp.cst_blk,
+						 SDXI_ADMIN_DRAIN_TIMEOUT_MS,
+						 false);
+		if (wait_ret == 0 || wait_ret == -EIO) {
+			completed = true;
+			if (cst_flags)
+				*cst_flags = sdxi_cst_blk_flags(comp.cst_blk);
+		}
+		if (wait_ret == -ETIMEDOUT) {
+			adm->sdxi->admin_dead = true;
+			dev_err(adm->sdxi->dev,
+				"admin command did not drain; quiescing function\n");
+			sdxi_dev_force_quiesce(adm->sdxi);
+		}
+	}
+	if (!ret || ret == -EIO) {
+		completed = true;
+		if (cst_flags)
+			*cst_flags = sdxi_cst_blk_flags(comp.cst_blk);
+	}
+	if (completed)
+		sdxi_cst_pool_put(adm->cst_pool, comp.cst_blk);
+	sdxi_ring_wake_up(adm->ring_state);
+
+out_unlock:
+	mutex_unlock(&adm->sdxi->admin_lock);
+	return ret;
+}
+
+int sdxi_adm_update_cxt(struct sdxi_cxt *cxt)
+{
+	struct sdxi_desc descs[2] = {};
+	int ret;
+
+	if (!cxt || sdxi_cxt_is_admin(cxt))
+		return -EINVAL;
+
+	ret = sdxi_encode_cxt_upd(&descs[0], &(const struct sdxi_cxt_upd) {
+			.level = SDXI_CXT_UPD_LVL_L1,
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	if (ret)
+		return ret;
+
+	ret = sdxi_encode_sync(&descs[1], &(const struct sdxi_sync) {
+			.filter = SDXI_SYNC_FLT_CXT,
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	if (ret)
+		return ret;
+
+	return sdxi_adm_submit(to_admin_cxt(cxt), descs, ARRAY_SIZE(descs),
+			       SDXI_ADMIN_DEFAULT_TIMEOUT_MS, NULL, false);
+}
+
+int sdxi_adm_start_cxt_sync(struct sdxi_cxt *cxt, unsigned int timeout_ms,
+			    u32 *cst_flags, bool interruptible)
+{
+	struct sdxi_desc descs[2] = {};
+	int ret;
+
+	if (!cxt || sdxi_cxt_is_admin(cxt))
+		return -EINVAL;
+
+	ret = sdxi_encode_cxt_start(&descs[0], &(const struct sdxi_cxt_start) {
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	if (ret)
+		return ret;
+
+	ret = sdxi_encode_sync(&descs[1], &(const struct sdxi_sync) {
+			.filter = SDXI_SYNC_FLT_CXT,
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	if (ret)
+		return ret;
+
+	return sdxi_adm_submit(to_admin_cxt(cxt), descs, ARRAY_SIZE(descs),
+			       timeout_ms, cst_flags, interruptible);
+}
+
+int sdxi_adm_stop_cxt_sync(struct sdxi_cxt *cxt, unsigned int timeout_ms,
+			   u32 *cst_flags, bool interruptible)
+{
+	struct sdxi_desc descs[2] = {};
+	int ret;
+
+	if (!cxt || sdxi_cxt_is_admin(cxt))
+		return -EINVAL;
+
+	ret = sdxi_encode_cxt_stop(&descs[0], &(const struct sdxi_cxt_stop) {
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	if (ret)
+		return ret;
+
+	ret = sdxi_encode_sync(&descs[1], &(const struct sdxi_sync) {
+			.filter = SDXI_SYNC_FLT_STOP,
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	if (ret)
+		return ret;
+
+	return sdxi_adm_submit(to_admin_cxt(cxt), descs, ARRAY_SIZE(descs),
+			       timeout_ms, cst_flags, interruptible);
+}
+
+int sdxi_adm_rkey_update_sync(struct sdxi_dev *sdxi, u16 rkey_start,
+			      u16 rkey_end, unsigned int timeout_ms,
+			      u32 *cst_flags)
+{
+	struct sdxi_desc descs[2] = {};
+	int ret;
+
+	if (!sdxi)
+		return -ENODEV;
+
+	ret = sdxi_encode_rkey_upd(&descs[0], &(const struct sdxi_rkey_upd) {
+			.rkey_start = rkey_start,
+			.rkey_end = rkey_end,
+		});
+	if (ret)
+		return ret;
+
+	ret = sdxi_encode_sync(&descs[1], &(const struct sdxi_sync) {
+			.filter = SDXI_SYNC_FLT_RKEY,
+			.key_start = rkey_start,
+			.key_end = rkey_end,
+		});
+	if (ret)
+		return ret;
+
+	return sdxi_adm_submit(sdxi->admin_cxt, descs, ARRAY_SIZE(descs),
+			       timeout_ms, cst_flags, false);
+}
+
+int sdxi_adm_akey_update_sync(struct sdxi_dev *sdxi, u16 cxt_start,
+			      u16 cxt_end, u16 akey_start, u16 akey_end,
+			      unsigned int timeout_ms, u32 *cst_flags)
+{
+	struct sdxi_cxt_range range = {
+		.cxt_start = cxt_start,
+		.cxt_end = cxt_end,
+	};
+	struct sdxi_desc descs[2] = {};
+	int ret;
+
+	if (!sdxi)
+		return -ENODEV;
+
+	ret = sdxi_encode_akey_upd(&descs[0], &(const struct sdxi_akey_upd) {
+			.range = range,
+			.akey_start = akey_start,
+			.akey_end = akey_end,
+		});
+	if (ret)
+		return ret;
+
+	ret = sdxi_encode_sync(&descs[1], &(const struct sdxi_sync) {
+			.filter = SDXI_SYNC_FLT_AKEY,
+			.range = range,
+			.key_start = akey_start,
+			.key_end = akey_end,
+		});
+	if (ret)
+		return ret;
+
+	return sdxi_adm_submit(sdxi->admin_cxt, descs, ARRAY_SIZE(descs),
+			       timeout_ms, cst_flags, false);
+}
+
+int sdxi_adm_disc(struct sdxi_dev *sdxi, u16 index_start, u16 index_end,
+		  dma_addr_t addr, unsigned int timeout_ms, u32 *cst_flags)
+{
+	struct sdxi_desc desc = {};
+	int ret;
+
+	if (!sdxi)
+		return -ENODEV;
+
+	ret = sdxi_encode_disc(&desc, &(const struct sdxi_disc) {
+			.index_start = index_start,
+			.index_end = index_end,
+			.addr = addr,
+		});
+	if (ret)
+		return ret;
+
+	return sdxi_adm_submit(sdxi->admin_cxt, &desc, 1, timeout_ms,
+			       cst_flags, false);
+}
+
+int sdxi_adm_submit_desc(struct sdxi_cxt *adm, const struct sdxi_desc *desc,
+			 unsigned int timeout_ms, u32 *cst_flags)
+{
+	if (!desc)
+		return -EINVAL;
+
+	return sdxi_adm_submit(adm, desc, 1, timeout_ms, cst_flags, false);
+}
+EXPORT_SYMBOL_NS_GPL(sdxi_adm_submit_desc, "SDXI");
+
+int sdxi_adm_start_cxt(struct sdxi_cxt *cxt)
+{
+	return sdxi_adm_start_cxt_sync(cxt, SDXI_ADMIN_DEFAULT_TIMEOUT_MS,
+				       NULL, false);
+}
+
+int sdxi_adm_stop_cxt(struct sdxi_cxt *cxt)
+{
+	return sdxi_adm_stop_cxt_sync(cxt, SDXI_ADMIN_DEFAULT_TIMEOUT_MS,
+				      NULL, false);
+}
diff --git a/drivers/dma/sdxi/admin.h b/drivers/dma/sdxi/admin.h
new file mode 100644
index 000000000000..175f8c28c757
--- /dev/null
+++ b/drivers/dma/sdxi/admin.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI administrative command helpers.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_ADMIN_H
+#define DMA_SDXI_ADMIN_H
+
+#include <linux/dma-mapping.h>
+#include <linux/types.h>
+
+#include "context.h"
+
+static inline struct sdxi_cxt *to_admin_cxt(const struct sdxi_cxt *cxt)
+{
+	return cxt->sdxi->admin_cxt;
+}
+
+static inline bool sdxi_cxt_is_admin(const struct sdxi_cxt *cxt)
+{
+	return cxt == to_admin_cxt(cxt);
+}
+
+int sdxi_adm_update_cxt(struct sdxi_cxt *cxt);
+int sdxi_adm_start_cxt_sync(struct sdxi_cxt *cxt, unsigned int timeout_ms,
+			    u32 *cst_flags, bool interruptible);
+int sdxi_adm_stop_cxt_sync(struct sdxi_cxt *cxt, unsigned int timeout_ms,
+			   u32 *cst_flags, bool interruptible);
+int sdxi_adm_rkey_update_sync(struct sdxi_dev *sdxi, u16 rkey_start,
+			      u16 rkey_end, unsigned int timeout_ms,
+			      u32 *cst_flags);
+int sdxi_adm_akey_update_sync(struct sdxi_dev *sdxi, u16 cxt_start,
+			      u16 cxt_end, u16 akey_start, u16 akey_end,
+			      unsigned int timeout_ms, u32 *cst_flags);
+int sdxi_adm_disc(struct sdxi_dev *sdxi, u16 index_start, u16 index_end,
+		  dma_addr_t addr, unsigned int timeout_ms, u32 *cst_flags);
+int sdxi_adm_submit_desc(struct sdxi_cxt *adm, const struct sdxi_desc *desc,
+			 unsigned int timeout_ms, u32 *cst_flags);
+
+int sdxi_adm_start_cxt(struct sdxi_cxt *cxt);
+int sdxi_adm_stop_cxt(struct sdxi_cxt *cxt);
+
+#endif /* DMA_SDXI_ADMIN_H */
diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index d75668e89e61..a8d065b57bac 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -22,6 +22,7 @@
 #include <asm/barrier.h>
 #include <asm/rwonce.h>
 
+#include "admin.h"
 #include "completion.h"
 #include "context.h"
 #include "descriptor.h"
@@ -106,78 +107,6 @@ static struct sdxi_cxt *sdxi_alloc_cxt(struct sdxi_dev *sdxi)
 	return_ptr(cxt);
 }
 
-/* Claim completion storage before making a ring reservation irrevocable. */
-static int sdxi_cxt_reserve(struct sdxi_cxt *cxt, size_t nr,
-			    struct sdxi_ring_resv *resv,
-			    struct sdxi_completion *comp)
-{
-	int err;
-
-	err = sdxi_cst_pool_get_wait(cxt->cst_pool, comp);
-	if (err)
-		return err;
-
-	err = sdxi_ring_reserve(cxt->ring_state, nr, resv);
-	if (err)
-		sdxi_cst_pool_put(cxt->cst_pool, comp->cst_blk);
-
-	return err;
-}
-
-static int sdxi_cxt_poll_completion(struct sdxi_cxt *cxt,
-				    const struct sdxi_completion *comp)
-{
-	int err = sdxi_cst_blk_poll(comp->cst_blk);
-
-	/*
-	 * A timeout does not end hardware ownership. Keep these administrative
-	 * blocks claimed until context teardown, even if they complete later.
-	 * Repeated timeouts can exhaust the pool; allocation then times out too.
-	 */
-	if (err != -ETIMEDOUT)
-		sdxi_cst_pool_put(cxt->cst_pool, comp->cst_blk);
-	sdxi_ring_wake_up(cxt->ring_state);
-
-	return err;
-}
-
-/*
- * Signal the function that a client context's control structures have
- * been modified so it discards any privately cached copies of them.
- * DSC_SYNC.CXT is required before the operation can be considered
- * complete.
- */
-static int sdxi_cxt_update(const struct sdxi_cxt *cxt)
-{
-	struct sdxi_cxt *adm = to_admin_cxt(cxt);
-	struct sdxi_slot upd, sync;
-	struct sdxi_ring_resv resv;
-	struct sdxi_completion comp;
-	int err;
-
-	might_sleep();
-
-	err = sdxi_cxt_reserve(adm, 2, &resv, &comp);
-	if (err)
-		return err;
-
-	upd = sdxi_ring_resv_slot(&resv, 0);
-	sync = sdxi_ring_resv_slot(&resv, 1);
-
-	sdxi_encode_cxt_upd(sdxi_slot_desc(upd), &(const struct sdxi_cxt_upd) {
-			.level = SDXI_CXT_UPD_LVL_L1,
-			.range = sdxi_cxt_range_single(cxt->id),
-		});
-	sdxi_encode_sync(sdxi_slot_desc(sync), &(const struct sdxi_sync) {
-			.filter = SDXI_SYNC_FLT_CXT,
-			.range = sdxi_cxt_range_single(cxt->id),
-		});
-	sdxi_cxt_submit(adm, upd, NULL, 0);
-	sdxi_cxt_submit(adm, sync, &comp, SDXI_CXT_SUBMIT_KICK);
-
-	return sdxi_cxt_poll_completion(adm, &comp);
-}
-
 struct sdxi_cxt_ctl_cfg {
 	dma_addr_t ds_ring_ptr;
 	dma_addr_t cxt_sts_ptr;
@@ -417,67 +346,6 @@ int sdxi_admin_cxt_init(struct sdxi_dev *sdxi)
 	return devm_add_action_or_reset(sdxi->dev, free_admin_cxt, sdxi);
 }
 
-int sdxi_start_cxt(struct sdxi_cxt *cxt)
-{
-	struct sdxi_cxt *adm = to_admin_cxt(cxt);
-	struct sdxi_ring_resv resv;
-	struct sdxi_completion comp;
-	struct sdxi_slot start;
-	int err;
-
-	might_sleep();
-
-	/* Administrative context activation is controlled by the function state. */
-	if (WARN_ON(adm == cxt))
-		return -EINVAL;
-
-	err = sdxi_cxt_reserve(adm, 1, &resv, &comp);
-	if (err)
-		return err;
-
-	start = sdxi_ring_resv_slot(&resv, 0);
-	sdxi_encode_cxt_start(sdxi_slot_desc(start), &(const struct sdxi_cxt_start) {
-			.range = sdxi_cxt_range_single(cxt->id),
-		});
-	sdxi_cxt_submit(adm, start, &comp, SDXI_CXT_SUBMIT_KICK);
-
-	return sdxi_cxt_poll_completion(adm, &comp);
-}
-
-void sdxi_stop_cxt(struct sdxi_cxt *cxt)
-{
-	struct sdxi_cxt *adm = to_admin_cxt(cxt);
-	struct sdxi_slot stop, sync;
-	struct sdxi_ring_resv resv;
-	struct sdxi_completion comp;
-	int err;
-
-	might_sleep();
-
-	/* Administrative context stopping is controlled by the function state. */
-	if (WARN_ON(adm == cxt))
-		return;
-
-	err = sdxi_cxt_reserve(adm, 2, &resv, &comp);
-	if (WARN_ON_ONCE(err))
-		return;
-
-	stop = sdxi_ring_resv_slot(&resv, 0);
-	sync = sdxi_ring_resv_slot(&resv, 1);
-
-	sdxi_encode_cxt_stop(sdxi_slot_desc(stop), &(const struct sdxi_cxt_stop) {
-			.range = sdxi_cxt_range_single(cxt->id),
-		});
-	sdxi_encode_sync(sdxi_slot_desc(sync), &(const struct sdxi_sync) {
-			.filter = SDXI_SYNC_FLT_STOP,
-			.range = sdxi_cxt_range_single(cxt->id),
-		});
-	sdxi_cxt_submit(adm, stop, NULL, 0);
-	sdxi_cxt_submit(adm, sync, &comp, SDXI_CXT_SUBMIT_KICK);
-
-	WARN_ON(sdxi_cxt_poll_completion(adm, &comp));
-}
-
 /*
  * Temporary owner for context id until it can be assigned to a
  * context object; enables scope-based cleanup.
@@ -637,7 +505,7 @@ int sdxi_cxt_exit(struct sdxi_cxt *cxt)
 	invalidate_cxt_ctl(cxt->cxt_ctl);
 	mutex_unlock(&sdxi->cxt_lock);
 
-	err = sdxi_cxt_update(cxt);
+	err = sdxi_adm_update_cxt(cxt);
 	if (err) {
 		dev_err(sdxi->dev,
 			"context %u release failed (%d), retaining resources until function shutdown\n",
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index acf41709c4de..196207cbca94 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -60,9 +60,6 @@ struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi);
 int sdxi_cxt_exit(struct sdxi_cxt *cxt);
 void sdxi_client_cxts_exit(struct sdxi_dev *sdxi);
 
-int sdxi_start_cxt(struct sdxi_cxt *cxt);
-void sdxi_stop_cxt(struct sdxi_cxt *cxt);
-
 enum sdxi_submit_flags {
 	SDXI_CXT_SUBMIT_KICK  = BIT(0),
 	SDXI_CXT_SUBMIT_FENCE = BIT(1),
@@ -73,16 +70,6 @@ void sdxi_cxt_attach_cst(struct sdxi_slot slot,
 void sdxi_cxt_submit(struct sdxi_cxt *cxt, struct sdxi_slot slot,
 		     const struct sdxi_completion *comp, enum sdxi_submit_flags flags);
 
-static inline struct sdxi_cxt *to_admin_cxt(const struct sdxi_cxt *cxt)
-{
-	return cxt->sdxi->admin_cxt;
-}
-
-static inline bool sdxi_cxt_is_admin(const struct sdxi_cxt *cxt)
-{
-	return cxt == to_admin_cxt(cxt);
-}
-
 static inline void sdxi_cxt_kick(struct sdxi_cxt *cxt)
 {
 	/*
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index c5b5b39a7fe5..9b6a89e469ad 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -245,6 +245,38 @@ static int sdxi_dev_start(struct sdxi_dev *sdxi)
 	return 0;
 }
 
+void sdxi_dev_force_quiesce(struct sdxi_dev *sdxi)
+{
+	enum sdxi_fn_gsv status = sdxi_dev_gsv(sdxi);
+	int ret;
+
+	if (status == SDXI_GSV_STOP)
+		return;
+
+	if (status != SDXI_GSV_ERROR) {
+		sdxi_write_fn_gsr(sdxi, SDXI_GSRV_STOP_HD);
+		ret = sdxi_dev_gsv_poll(sdxi, status,
+					status == SDXI_GSV_STOP ||
+					status == SDXI_GSV_ERROR);
+		if (!ret && status == SDXI_GSV_STOP)
+			return;
+	} else {
+		ret = 0;
+	}
+
+	if (!ret && status == SDXI_GSV_ERROR) {
+		sdxi_write_fn_gsr(sdxi, SDXI_GSRV_RESET);
+		ret = sdxi_dev_gsv_poll(sdxi, status,
+					status == SDXI_GSV_STOP);
+		if (!ret)
+			return;
+	}
+
+	dev_err(sdxi->dev, "failed to quiesce function; disabling bus DMA\n");
+	if (sdxi->bus_ops->force_quiesce)
+		sdxi->bus_ops->force_quiesce(sdxi);
+}
+
 /* Stop the function and discard inherited MMIO configuration. */
 static int sdxi_fn_prepare(struct sdxi_dev *sdxi)
 {
@@ -425,6 +457,7 @@ int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 	ida_init(&sdxi->vectors);
 	xa_init_flags(&sdxi->client_cxts, XA_FLAGS_ALLOC1);
 	mutex_init(&sdxi->cxt_lock);
+	mutex_init(&sdxi->admin_lock);
 	dev_set_drvdata(dev, sdxi);
 
 	err = sdxi->bus_ops->init(sdxi);
diff --git a/drivers/dma/sdxi/dma.c b/drivers/dma/sdxi/dma.c
index 512855c83da2..a4b254a3080d 100644
--- a/drivers/dma/sdxi/dma.c
+++ b/drivers/dma/sdxi/dma.c
@@ -20,6 +20,7 @@
 
 #include "../dmaengine.h"
 #include "../virt-dma.h"
+#include "admin.h"
 #include "completion.h"
 #include "context.h"
 #include "descriptor.h"
@@ -416,9 +417,12 @@ static int sdxi_dma_alloc_chan_resources(struct dma_chan *dma_chan)
 	if (err)
 		goto free_akey;
 
-	err = sdxi_start_cxt(sdchan->cxt);
-	if (err)
+	err = sdxi_adm_start_cxt(sdchan->cxt);
+	if (err) {
+		sdxi->admin_dead = true;
+		sdxi_dev_force_quiesce(sdxi);
 		goto free_irq;
+	}
 
 	return 0;
 free_irq:
@@ -428,25 +432,31 @@ static int sdxi_dma_alloc_chan_resources(struct dma_chan *dma_chan)
 free_vector:
 	sdxi_free_vector(sdxi, vector);
 exit_cxt:
-	sdxi_cxt_exit(sdchan->cxt);
+	WARN_ON(sdxi_cxt_exit(sdchan->cxt));
 	return err;
 }
 
 static void sdxi_dma_free_chan_resources(struct dma_chan *dma_chan)
 {
 	struct sdxi_dma_chan *sdchan = to_sdxi_dma_chan(dma_chan);
+	struct sdxi_dev *sdxi = sdchan->cxt->sdxi;
 	LIST_HEAD(head);
+	int err;
 
-	sdxi_stop_cxt(sdchan->cxt);
+	err = sdxi_adm_stop_cxt(sdchan->cxt);
+	if (WARN_ON(err)) {
+		sdxi->admin_dead = true;
+		sdxi_dev_force_quiesce(sdxi);
+	}
 	free_irq(sdchan->irq, sdchan);
-	sdxi_free_vector(sdchan->cxt->sdxi, sdchan->vector);
+	sdxi_free_vector(sdxi, sdchan->vector);
 	sdxi_free_akey(sdchan->cxt, sdchan->akey);
 	/* virt-dma does not know about the driver's pending list. */
 	scoped_guard(spinlock_irqsave, &sdchan->vchan.lock)
 		list_splice_tail_init(&sdchan->pending, &head);
 	vchan_dma_desc_free_list(&sdchan->vchan, &head);
 	vchan_free_chan_resources(to_virt_chan(dma_chan));
-	sdxi_cxt_exit(sdchan->cxt);
+	WARN_ON(sdxi_cxt_exit(sdchan->cxt));
 }
 
 int sdxi_dma_register(struct sdxi_dev *sdxi)
diff --git a/drivers/dma/sdxi/pci.c b/drivers/dma/sdxi/pci.c
index ef978ca22db1..f9f75ff5e179 100644
--- a/drivers/dma/sdxi/pci.c
+++ b/drivers/dma/sdxi/pci.c
@@ -113,10 +113,26 @@ static int sdxi_pci_get_irq(struct sdxi_dev *sdxi, unsigned int nr)
 	return pci_irq_vector(sdxi_to_pci_dev(sdxi), nr);
 }
 
+static void sdxi_pci_force_quiesce(struct sdxi_dev *sdxi)
+{
+	struct pci_dev *pdev = sdxi_to_pci_dev(sdxi);
+	int ret;
+
+	pci_clear_master(pdev);
+	ret = pci_try_reset_function(pdev);
+	if (!ret)
+		return;
+
+	dev_err(&pdev->dev, "function reset failed: %d\n", ret);
+	if (!pci_wait_for_pending_transaction(pdev))
+		dev_err(&pdev->dev, "pending PCI transactions did not drain\n");
+}
+
 static const struct sdxi_bus_ops sdxi_pci_ops = {
 	.init = sdxi_pci_init,
 	.reset = sdxi_pci_reset,
 	.get_irq = sdxi_pci_get_irq,
+	.force_quiesce = sdxi_pci_force_quiesce,
 };
 
 static int sdxi_pci_probe(struct pci_dev *pdev,
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 6d2b9ba88182..7b3cad785442 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -63,6 +63,11 @@ struct sdxi_bus_ops {
 	 * @get_irq: Map device interrupt index to Linux IRQ number.
 	 */
 	int (*get_irq)(struct sdxi_dev *sdxi, unsigned int index);
+	/**
+	 * @force_quiesce: Permanently prevent further bus-mastering DMA after
+	 *                  device recovery fails.
+	 */
+	void (*force_quiesce)(struct sdxi_dev *sdxi);
 };
 
 struct sdxi_dev {
@@ -91,6 +96,8 @@ struct sdxi_dev {
 	struct ida vectors;
 
 	struct sdxi_cxt *admin_cxt;
+	struct mutex admin_lock;		/* admin submission serialization */
+	bool admin_dead;
 	struct xarray client_cxts; /* context id -> (struct sdxi_cxt *) */
 
 	bool has_mailbox;
@@ -138,6 +145,7 @@ static inline int sdxi_vector_to_irq(struct sdxi_dev *sdxi, unsigned int nr)
 
 int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops);
 void sdxi_unregister(struct device *dev);
+void sdxi_dev_force_quiesce(struct sdxi_dev *sdxi);
 
 static inline u64 sdxi_read64(const struct sdxi_dev *sdxi, enum sdxi_reg reg)
 {
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 08/44] dmaengine: sdxi: add core RKey table management
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (6 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 07/44] dmaengine: sdxi: implement synchronous admin operations Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 09/44] dmaengine: sdxi: handle hardware error logs Wei Huang
                   ` (35 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Allocate the minimum supported function RKey table when the
capability is present, program MMIO_RKEY, and disable the table during
teardown. Keep userspace key operations for a later UAPI patch.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Makefile |  1 +
 drivers/dma/sdxi/device.c | 11 ++++--
 drivers/dma/sdxi/keys.c   | 70 +++++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/keys.h   | 18 ++++++++++
 drivers/dma/sdxi/sdxi.h   |  8 +++++
 5 files changed, 106 insertions(+), 2 deletions(-)
 create mode 100644 drivers/dma/sdxi/keys.c
 create mode 100644 drivers/dma/sdxi/keys.h

diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index bddd1278096c..96d013dcbefd 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -7,6 +7,7 @@ sdxi-core-y := \
 	descriptor.o  \
 	device.o      \
 	dma.o         \
+	keys.o        \
 	ring.o
 
 obj-$(CONFIG_SDXI_PCI) += sdxi-pci.o
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index 9b6a89e469ad..0e7d3711ba88 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -25,6 +25,7 @@
 #include "context.h"
 #include "dma.h"
 #include "hw.h"
+#include "keys.h"
 #include "mmio.h"
 #include "sdxi.h"
 
@@ -387,6 +388,10 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 			   FIELD_GET(SDXI_MMIO_CAP1_OPB_000_CAP, cap1));
 	sdxi_write64(sdxi, SDXI_MMIO_CTL2, ctl2);
 
+	err = sdxi_keys_init(sdxi, cap1);
+	if (err)
+		return err;
+
 	/* SDXI 1.0 4.1.8.2 Context Level 2 Table Setup */
 	sdxi->L2_table = dmam_alloc_coherent(sdxi->dev,
 					     sizeof(*sdxi->L2_table),
@@ -458,6 +463,7 @@ int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 	xa_init_flags(&sdxi->client_cxts, XA_FLAGS_ALLOC1);
 	mutex_init(&sdxi->cxt_lock);
 	mutex_init(&sdxi->admin_lock);
+	mutex_init(&sdxi->rkey_lock);
 	dev_set_drvdata(dev, sdxi);
 
 	err = sdxi->bus_ops->init(sdxi);
@@ -476,9 +482,10 @@ void sdxi_unregister(struct device *dev)
 {
 	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
 
-	/* Stop/reset is best effort if the hardware cannot be quiesced. */
-	sdxi_dev_stop(sdxi);
+	if (sdxi_dev_stop(sdxi))
+		sdxi_dev_force_quiesce(sdxi);
 	sdxi_client_cxts_exit(sdxi);
+	sdxi_keys_exit(sdxi);
 	ida_destroy(&sdxi->vectors);
 }
 EXPORT_SYMBOL_NS_GPL(sdxi_unregister, "SDXI");
diff --git a/drivers/dma/sdxi/keys.c b/drivers/dma/sdxi/keys.c
new file mode 100644
index 000000000000..2e25a9b806e6
--- /dev/null
+++ b/drivers/dma/sdxi/keys.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI key table management.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/device/devres.h>
+#include <linux/dma-mapping.h>
+#include <linux/log2.h>
+#include <linux/sizes.h>
+#include <linux/string.h>
+
+#include "hw.h"
+#include "keys.h"
+#include "mmio.h"
+#include "sdxi.h"
+
+static void sdxi_keys_release(void *data)
+{
+	sdxi_keys_exit(data);
+}
+
+int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1)
+{
+	size_t table_size;
+	u64 reg;
+	int ret;
+
+	sdxi->rkey_cap = FIELD_GET(SDXI_MMIO_CAP1_RKEY_CAP, cap1);
+	if (!sdxi->rkey_cap)
+		return 0;
+
+	sdxi->rkey_order = 0;
+	table_size = SZ_4K << sdxi->rkey_order;
+	sdxi->rkey_entries = table_size / sizeof(*sdxi->rkey_table);
+
+	sdxi->rkey_table = dmam_alloc_coherent(sdxi->dev, table_size,
+					       &sdxi->rkey_table_dma,
+					       GFP_KERNEL);
+	if (!sdxi->rkey_table)
+		return -ENOMEM;
+	memset(sdxi->rkey_table, 0, table_size);
+
+	reg = FIELD_PREP(SDXI_MMIO_RKEY_PTR,
+			 sdxi->rkey_table_dma >> ilog2(SZ_4K)) |
+	      FIELD_PREP(SDXI_MMIO_RKEY_SZ, sdxi->rkey_order) |
+	      SDXI_MMIO_RKEY_EN;
+	sdxi_write64(sdxi, SDXI_MMIO_RKEY, reg);
+
+	ret = devm_add_action_or_reset(sdxi->dev, sdxi_keys_release, sdxi);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+void sdxi_keys_exit(struct sdxi_dev *sdxi)
+{
+	if (!sdxi->rkey_cap)
+		return;
+
+	sdxi_write64(sdxi, SDXI_MMIO_RKEY, 0);
+	sdxi->rkey_table = NULL;
+	sdxi->rkey_table_dma = 0;
+	sdxi->rkey_entries = 0;
+	sdxi->rkey_order = 0;
+	sdxi->rkey_cap = false;
+}
diff --git a/drivers/dma/sdxi/keys.h b/drivers/dma/sdxi/keys.h
new file mode 100644
index 000000000000..c479da651526
--- /dev/null
+++ b/drivers/dma/sdxi/keys.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI key table management.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_KEYS_H
+#define DMA_SDXI_KEYS_H
+
+#include <linux/types.h>
+
+struct sdxi_dev;
+
+int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1);
+void sdxi_keys_exit(struct sdxi_dev *sdxi);
+
+#endif /* DMA_SDXI_KEYS_H */
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 7b3cad785442..48e4631c621c 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -90,6 +90,14 @@ struct sdxi_dev {
 	u16 L1_users[SDXI_L2_TABLE_ENTRIES];
 	struct mutex cxt_lock;		/* context table serialization */
 
+	/* RKey table */
+	struct mutex rkey_lock;
+	struct sdxi_rkey_ent *rkey_table;
+	dma_addr_t rkey_table_dma;
+	u32 rkey_entries;
+	u8 rkey_order;
+	bool rkey_cap;
+
 	struct dma_pool *cxt_ctl_pool;
 
 	unsigned int nr_vectors;
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 09/44] dmaengine: sdxi: handle hardware error logs
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (7 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 08/44] dmaengine: sdxi: add core RKey table management Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 10/44] dmaengine: sdxi: separate context and submission resources Wei Huang
                   ` (34 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Configure the architected error log on vector zero, decode entries
with the packing API, and report hardware failures from a threaded
interrupt.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Kconfig  |   2 +-
 drivers/dma/sdxi/Makefile |   1 +
 drivers/dma/sdxi/device.c |  14 ++-
 drivers/dma/sdxi/error.c  | 246 ++++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/error.h  |  16 +++
 drivers/dma/sdxi/sdxi.h   |   5 +
 6 files changed, 281 insertions(+), 3 deletions(-)
 create mode 100644 drivers/dma/sdxi/error.c
 create mode 100644 drivers/dma/sdxi/error.h

diff --git a/drivers/dma/sdxi/Kconfig b/drivers/dma/sdxi/Kconfig
index 85e23608405c..e01b5365c154 100644
--- a/drivers/dma/sdxi/Kconfig
+++ b/drivers/dma/sdxi/Kconfig
@@ -5,6 +5,7 @@ config SDXI_CORE
 	depends on 64BIT
 	select DMA_ENGINE
 	select DMA_VIRTUAL_CHANNELS
+	select PACKING
 	help
 	  Enable support for Smart Data Accelerator Interface (SDXI)
 	  Platform Data Mover devices. SDXI is a vendor-neutral
@@ -32,7 +33,6 @@ config SDXI_KUNIT_TEST
 	tristate "SDXI unit tests" if !KUNIT_ALL_TESTS
 	depends on SDXI_CORE && KUNIT
 	default KUNIT_ALL_TESTS
-	select PACKING
 	help
 	  KUnit tests for parts of the SDXI driver. Does not require
 	  SDXI hardware.
diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index 96d013dcbefd..3f49bc9af955 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -7,6 +7,7 @@ sdxi-core-y := \
 	descriptor.o  \
 	device.o      \
 	dma.o         \
+	error.o       \
 	keys.o        \
 	ring.o
 
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index 0e7d3711ba88..fa9421a5410b 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -24,6 +24,7 @@
 
 #include "context.h"
 #include "dma.h"
+#include "error.h"
 #include "hw.h"
 #include "keys.h"
 #include "mmio.h"
@@ -413,6 +414,10 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	if (err)
 		return err;
 
+	err = sdxi_error_init(sdxi);
+	if (err)
+		return err;
+
 	/*
 	 * SDXI 1.0 4.1.8.9: Set MMIO_CTL0.fn_gsr to GSRV_ACTIVE and
 	 * wait for MMIO_STS0.fn_gsv to reach GSV_ACTIVE or GSV_ERROR.
@@ -423,8 +428,11 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	 * code starts client contexts.
 	 */
 	err = sdxi_dev_start(sdxi);
-	if (err)
-		sdxi_dev_stop(sdxi);
+	if (err) {
+		sdxi_error_exit(sdxi);
+		if (sdxi_dev_stop(sdxi))
+			sdxi_dev_force_quiesce(sdxi);
+	}
 
 	return err;
 }
@@ -464,6 +472,7 @@ int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 	mutex_init(&sdxi->cxt_lock);
 	mutex_init(&sdxi->admin_lock);
 	mutex_init(&sdxi->rkey_lock);
+	sdxi->error_irq = -1;
 	dev_set_drvdata(dev, sdxi);
 
 	err = sdxi->bus_ops->init(sdxi);
@@ -482,6 +491,7 @@ void sdxi_unregister(struct device *dev)
 {
 	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
 
+	sdxi_error_exit(sdxi);
 	if (sdxi_dev_stop(sdxi))
 		sdxi_dev_force_quiesce(sdxi);
 	sdxi_client_cxts_exit(sdxi);
diff --git a/drivers/dma/sdxi/error.c b/drivers/dma/sdxi/error.c
new file mode 100644
index 000000000000..561e88d81f41
--- /dev/null
+++ b/drivers/dma/sdxi/error.c
@@ -0,0 +1,246 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI error log handling.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/device/devres.h>
+#include <linux/dma-mapping.h>
+#include <linux/interrupt.h>
+#include <linux/log2.h>
+#include <linux/packing.h>
+#include <linux/sizes.h>
+#include <linux/string.h>
+
+#include "error.h"
+#include "hw.h"
+#include "mmio.h"
+#include "sdxi.h"
+
+#define SDXI_ERRLOG_TYPE	0x7f7
+#define SDXI_ERRLOG_ENTRIES	64
+#define SDXI_ERRLOG_SIZE	(SDXI_ERRLOG_ENTRIES * \
+				 sizeof(struct sdxi_errlog_hd_ent))
+
+struct sdxi_errlog_entry {
+	u64 dsc_index;
+	u16 cxt_num;
+	u16 err_class;
+	u16 type;
+	u8 step;
+	u8 buf;
+	u8 sub_step;
+	u8 reaction;
+	bool vl;
+	bool cv;
+	bool div;
+	bool bv;
+};
+
+#define ERRLOG_FIELD(_high, _low, _member) \
+	PACKED_FIELD(_high, _low, struct sdxi_errlog_entry, _member)
+
+static const struct packed_field_u16 sdxi_errlog_fields[] = {
+	ERRLOG_FIELD(0, 0, vl),
+	ERRLOG_FIELD(13, 8, step),
+	ERRLOG_FIELD(26, 16, type),
+	ERRLOG_FIELD(32, 32, cv),
+	ERRLOG_FIELD(33, 33, div),
+	ERRLOG_FIELD(34, 34, bv),
+	ERRLOG_FIELD(38, 36, buf),
+	ERRLOG_FIELD(43, 40, sub_step),
+	ERRLOG_FIELD(46, 44, reaction),
+	ERRLOG_FIELD(63, 48, cxt_num),
+	ERRLOG_FIELD(127, 64, dsc_index),
+	ERRLOG_FIELD(367, 352, err_class),
+};
+
+enum {
+	SDXI_PACKING_QUIRKS = QUIRK_LITTLE_ENDIAN | QUIRK_LSW32_IS_FIRST,
+};
+
+static const char *const sdxi_err_steps[] = {
+	[1] = "internal",
+	[2] = "context L2",
+	[3] = "context L1",
+	[4] = "context control",
+	[5] = "context status",
+	[6] = "write index",
+	[7] = "descriptor",
+	[8] = "completion status",
+	[9] = "atomic return data",
+	[10] = "descriptor buffer",
+	[11] = "AKey lookup",
+	[12] = "RKey lookup",
+};
+
+static const char *sdxi_err_step_name(u8 step)
+{
+	if (step < ARRAY_SIZE(sdxi_err_steps) && sdxi_err_steps[step])
+		return sdxi_err_steps[step];
+
+	return "reserved";
+}
+
+static bool sdxi_errlog_decode(struct sdxi_dev *sdxi,
+			       const struct sdxi_errlog_hd_ent *raw,
+			       struct sdxi_errlog_entry *entry)
+{
+	unpack_fields(raw, sizeof(*raw), entry, sdxi_errlog_fields,
+		      SDXI_PACKING_QUIRKS);
+
+	if (!entry->vl) {
+		dev_err_ratelimited(sdxi->dev,
+				    "ignoring error log entry with vl=0\n");
+		return false;
+	}
+	if (entry->type != SDXI_ERRLOG_TYPE) {
+		dev_err_ratelimited(sdxi->dev,
+				    "ignoring error log type %#x\n",
+				    entry->type);
+		return false;
+	}
+
+	return true;
+}
+
+static void sdxi_errlog_report(struct sdxi_dev *sdxi, u64 read_index,
+			       const struct sdxi_errlog_entry *entry)
+{
+	dev_err(sdxi->dev,
+		"error log[%zu]: class=%#x step=%u (%s) substep=%u reaction=%u\n",
+		(size_t)(read_index % SDXI_ERRLOG_ENTRIES), entry->err_class,
+		entry->step, sdxi_err_step_name(entry->step), entry->sub_step,
+		entry->reaction);
+
+	if (entry->cv)
+		dev_err(sdxi->dev, "  context=%u\n", entry->cxt_num);
+	if (entry->div)
+		dev_err(sdxi->dev, "  descriptor index=%#llx\n",
+			entry->dsc_index);
+	if (entry->bv)
+		dev_err(sdxi->dev, "  buffer=%u\n", entry->buf);
+}
+
+static irqreturn_t sdxi_error_irq_thread(int irq, void *data)
+{
+	struct sdxi_dev *sdxi = data;
+	u64 ack_mask = SDXI_MMIO_ERR_STS_STS_BIT |
+		       SDXI_MMIO_ERR_STS_OVF_BIT |
+		       SDXI_MMIO_ERR_STS_ERR_BIT;
+	u64 write_index;
+	u64 read_index;
+	u64 status;
+	u64 count;
+
+	status = sdxi_read64(sdxi, SDXI_MMIO_ERR_STS);
+	if (!(status & ack_mask))
+		return IRQ_NONE;
+
+	if (status & SDXI_MMIO_ERR_STS_ERR_BIT)
+		dev_err(sdxi->dev, "hardware could not write the error log\n");
+	if (status & SDXI_MMIO_ERR_STS_OVF_BIT)
+		dev_err(sdxi->dev, "error log overflowed\n");
+	if (!(status & SDXI_MMIO_ERR_STS_STS_BIT)) {
+		sdxi_write64(sdxi, SDXI_MMIO_ERR_STS, status & ack_mask);
+		return IRQ_HANDLED;
+	}
+
+	read_index = sdxi_read64(sdxi, SDXI_MMIO_ERR_RD);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_STS, status & ack_mask);
+	write_index = sdxi_read64(sdxi, SDXI_MMIO_ERR_WRT);
+	dma_rmb();
+
+	if (write_index < read_index) {
+		dev_err(sdxi->dev,
+			"error log index mismatch: read=%#llx write=%#llx\n",
+			read_index, write_index);
+		read_index = write_index;
+	}
+
+	count = write_index - read_index;
+	if (count > SDXI_ERRLOG_ENTRIES)
+		read_index = write_index - SDXI_ERRLOG_ENTRIES;
+
+	while (read_index < write_index) {
+		struct sdxi_errlog_entry entry = {};
+		struct sdxi_errlog_hd_ent raw;
+		size_t index = read_index % SDXI_ERRLOG_ENTRIES;
+
+		memcpy(&raw, &sdxi->err_log[index], sizeof(raw));
+		if (sdxi_errlog_decode(sdxi, &raw, &entry))
+			sdxi_errlog_report(sdxi, read_index, &entry);
+		read_index++;
+	}
+
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_RD, read_index);
+	return IRQ_HANDLED;
+}
+
+int sdxi_error_init(struct sdxi_dev *sdxi)
+{
+	unsigned int size_order;
+	u64 reg;
+	int ret;
+
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CFG, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_STS,
+		     SDXI_MMIO_ERR_STS_STS_BIT |
+		     SDXI_MMIO_ERR_STS_OVF_BIT |
+		     SDXI_MMIO_ERR_STS_ERR_BIT);
+
+	sdxi->err_log = dmam_alloc_coherent(sdxi->dev, SDXI_ERRLOG_SIZE,
+					    &sdxi->err_log_dma, GFP_KERNEL);
+	if (!sdxi->err_log)
+		return -ENOMEM;
+	memset(sdxi->err_log, 0, SDXI_ERRLOG_SIZE);
+
+	sdxi->error_irq = sdxi->bus_ops->get_irq(sdxi, SDXI_ERROR_VECTOR);
+	if (sdxi->error_irq < 0)
+		return sdxi->error_irq;
+
+	ret = devm_request_threaded_irq(sdxi->dev, sdxi->error_irq, NULL,
+					sdxi_error_irq_thread,
+					IRQF_TRIGGER_NONE, "sdxi-error", sdxi);
+	if (ret)
+		return ret;
+
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_WRT, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_RD, 0);
+
+	reg = sdxi_read64(sdxi, SDXI_MMIO_ERR_CTL);
+	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 1);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
+
+	size_order = ilog2(SDXI_ERRLOG_SIZE) - ilog2(SZ_4K);
+	reg = FIELD_PREP(SDXI_MMIO_ERR_CFG_PTR,
+			 sdxi->err_log_dma >> ilog2(SZ_4K)) |
+	      FIELD_PREP(SDXI_MMIO_ERR_CFG_SZ, size_order) |
+	      SDXI_MMIO_ERR_CFG_EN;
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CFG, reg);
+
+	return 0;
+}
+
+void sdxi_error_exit(struct sdxi_dev *sdxi)
+{
+	u64 reg;
+
+	if (!sdxi->err_log)
+		return;
+
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CFG, 0);
+
+	reg = sdxi_read64(sdxi, SDXI_MMIO_ERR_CTL);
+	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
+
+	if (sdxi->error_irq >= 0)
+		devm_free_irq(sdxi->dev, sdxi->error_irq, sdxi);
+
+	sdxi->error_irq = -1;
+	sdxi->err_log = NULL;
+	sdxi->err_log_dma = 0;
+}
diff --git a/drivers/dma/sdxi/error.h b/drivers/dma/sdxi/error.h
new file mode 100644
index 000000000000..756a4fe45158
--- /dev/null
+++ b/drivers/dma/sdxi/error.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI error log handling.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_ERROR_H
+#define DMA_SDXI_ERROR_H
+
+struct sdxi_dev;
+
+int sdxi_error_init(struct sdxi_dev *sdxi);
+void sdxi_error_exit(struct sdxi_dev *sdxi);
+
+#endif /* DMA_SDXI_ERROR_H */
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 48e4631c621c..65e0191391db 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -98,6 +98,11 @@ struct sdxi_dev {
 	u8 rkey_order;
 	bool rkey_cap;
 
+	/* Error log */
+	int error_irq;
+	struct sdxi_errlog_hd_ent *err_log;
+	dma_addr_t err_log_dma;
+
 	struct dma_pool *cxt_ctl_pool;
 
 	unsigned int nr_vectors;
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 10/44] dmaengine: sdxi: separate context and submission resources
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (8 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 09/44] dmaengine: sdxi: handle hardware error logs Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 11/44] dmaengine: sdxi: support userspace context lifetimes Wei Huang
                   ` (33 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Split common context allocation from kernel submission queue
allocation and make context table programming consume an explicit
control configuration. Kernel and admin contexts retain the existing
SQ/CQ model while later userspace contexts can provide PASID-backed
ring addresses.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/context.c | 120 ++++++++++++++++++++++++++-----------
 drivers/dma/sdxi/context.h |   3 +
 drivers/dma/sdxi/device.c  |   2 +
 drivers/dma/sdxi/pci.c     |   1 +
 drivers/dma/sdxi/sdxi.h    |   1 +
 5 files changed, 91 insertions(+), 36 deletions(-)

diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index a8d065b57bac..834223e7b003 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -17,6 +17,7 @@
 #include <linux/errno.h>
 #include <linux/idr.h>
 #include <linux/iommu.h>
+#include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/types.h>
 #include <asm/barrier.h>
@@ -81,23 +82,14 @@ static struct sdxi_cxt *sdxi_alloc_cxt(struct sdxi_dev *sdxi)
 
 	cxt->sdxi = sdxi;
 	ida_init(&cxt->akey_ida);
-
-	cxt->ring_state = kzalloc_obj(*cxt->ring_state, GFP_KERNEL);
-	if (!cxt->ring_state)
-		return NULL;
-
-	cxt->sq = sdxi_sq_alloc(sdxi, DEFAULT_DESC_RING_ENTRIES);
-	if (!cxt->sq)
-		return NULL;
-
-	cxt->cst_pool = sdxi_cst_pool_create(sdxi, DEFAULT_DESC_RING_ENTRIES);
-	if (!cxt->cst_pool)
-		return NULL;
+	cxt->pasid = IOMMU_NO_PASID;
+	cxt->opb_000_enb = sdxi->op_grp_cap;
 
 	cxt->akey_table = dma_alloc_coherent(dev, sizeof(*cxt->akey_table),
 					     &cxt->akey_table_dma, GFP_KERNEL);
 	if (!cxt->akey_table)
 		return NULL;
+	memset(cxt->akey_table, 0, sizeof(*cxt->akey_table));
 
 	cxt->cxt_ctl = dma_pool_zalloc(sdxi->cxt_ctl_pool, GFP_KERNEL,
 				       &cxt->cxt_ctl_dma);
@@ -107,6 +99,25 @@ static struct sdxi_cxt *sdxi_alloc_cxt(struct sdxi_dev *sdxi)
 	return_ptr(cxt);
 }
 
+static int sdxi_alloc_kernel_submission(struct sdxi_cxt *cxt)
+{
+	cxt->ring_state = kzalloc_obj(*cxt->ring_state, GFP_KERNEL);
+	if (!cxt->ring_state)
+		return -ENOMEM;
+
+	cxt->sq = sdxi_sq_alloc(cxt->sdxi, DEFAULT_DESC_RING_ENTRIES);
+	if (!cxt->sq)
+		return -ENOMEM;
+
+	cxt->cst_pool = sdxi_cst_pool_create(cxt->sdxi,
+					     DEFAULT_DESC_RING_ENTRIES);
+	if (!cxt->cst_pool)
+		return -ENOMEM;
+
+	sdxi_ring_state_init(cxt->ring_state, cxt->sq);
+	return 0;
+}
+
 struct sdxi_cxt_ctl_cfg {
 	dma_addr_t ds_ring_ptr;
 	dma_addr_t cxt_sts_ptr;
@@ -253,9 +264,9 @@ sdxi_get_L1_table(struct sdxi_dev *sdxi, u16 l2_idx)
  * implementation. L1 pages remain allocated until device teardown so a
  * cached L2 pointer can never refer to freed memory.
  */
-static int sdxi_publish_cxt(const struct sdxi_cxt *cxt)
+static int sdxi_publish_cxt(const struct sdxi_cxt *cxt,
+			    const struct sdxi_cxt_ctl_cfg *ctl_cfg)
 {
-	struct sdxi_cxt_ctl_cfg ctl_cfg;
 	struct sdxi_cxt_L1_cfg L1_cfg;
 	struct sdxi_cxt_L1_table *L1_table;
 	struct sdxi_cxt_L1_ent *ent;
@@ -268,16 +279,7 @@ static int sdxi_publish_cxt(const struct sdxi_cxt *cxt)
 		      cxt->id, cxt->sdxi->max_cxtid))
 		return -EINVAL;
 
-	ctl_cfg = (typeof(ctl_cfg)) {
-		.se              = 1,
-		.csa             = 1,
-		.ds_ring_ptr     = sdxi_sq_ring_dma(cxt->sq),
-		.ds_ring_sz      = cxt->sq->count,
-		.cxt_sts_ptr     = sdxi_sq_cxt_sts_dma(cxt->sq),
-		.write_index_ptr = sdxi_sq_write_index_dma(cxt->sq),
-	};
-
-	err = configure_cxt_ctl(cxt->cxt_ctl, &ctl_cfg);
+	err = configure_cxt_ctl(cxt->cxt_ctl, ctl_cfg);
 	if (err)
 		return err;
 
@@ -294,13 +296,13 @@ static int sdxi_publish_cxt(const struct sdxi_cxt *cxt)
 
 	L1_cfg = (typeof(L1_cfg)) {
 		.ka          = 1,
-		.pv          = 0,
+		.pv          = cxt->pasid_valid,
 		.cxt_ctl_ptr = cxt->cxt_ctl_dma,
 		.akey_sz     = akey_table_order(cxt->akey_table),
 		.akey_ptr    = cxt->akey_table_dma,
-		.cxt_pasid   = IOMMU_NO_PASID,
+		.cxt_pasid   = cxt->pasid,
 		.max_buffer  = cxt->sdxi->max_buffer,
-		.opb_000_enb = cxt->sdxi->op_grp_cap,
+		.opb_000_enb = cxt->opb_000_enb,
 	};
 
 	err = configure_L1_entry(ent, &L1_cfg);
@@ -321,23 +323,39 @@ static void free_admin_cxt(void *ptr)
 	sdxi_free_cxt(sdxi->admin_cxt);
 }
 
+static int sdxi_cxt_set_doorbell(struct sdxi_cxt *cxt);
+
 int sdxi_admin_cxt_init(struct sdxi_dev *sdxi)
 {
-	int err;
+	struct sdxi_cxt_ctl_cfg ctl_cfg;
 	struct sdxi_sq *sq;
+	int err;
 
 	struct sdxi_cxt *cxt __free(sdxi_cxt) = sdxi_alloc_cxt(sdxi);
 	if (!cxt)
 		return -ENOMEM;
 
+	err = sdxi_alloc_kernel_submission(cxt);
+	if (err)
+		return err;
+
 	sq = cxt->sq;
 	/* SDXI 1.0 4.1.8.4.b: Set CXT_STS.state to CXTV_RUN. */
 	sq->cxt_sts.state = FIELD_PREP(SDXI_CXT_STS_STATE, CXTV_RUN);
 	cxt->id = SDXI_ADMIN_CXT_ID;
-	cxt->sq->doorbell = sdxi->dbs + cxt->id * sdxi->db_stride;
-	sdxi_ring_state_init(cxt->ring_state, sq);
+	err = sdxi_cxt_set_doorbell(cxt);
+	if (err)
+		return err;
 
-	err = sdxi_publish_cxt(cxt);
+	ctl_cfg = (typeof(ctl_cfg)) {
+		.se              = 1,
+		.csa             = 1,
+		.ds_ring_ptr     = sdxi_sq_ring_dma(cxt->sq),
+		.ds_ring_sz      = cxt->sq->count,
+		.cxt_sts_ptr     = sdxi_sq_cxt_sts_dma(cxt->sq),
+		.write_index_ptr = sdxi_sq_write_index_dma(cxt->sq),
+	};
+	err = sdxi_publish_cxt(cxt, &ctl_cfg);
 	if (err)
 		return err;
 
@@ -382,6 +400,23 @@ static bool sdxi_cxt_id_valid(const struct sdxi_cxt_id *cxt_id)
 	return cxt_id->index > 0;
 }
 
+static int sdxi_cxt_set_doorbell(struct sdxi_cxt *cxt)
+{
+	struct sdxi_dev *sdxi = cxt->sdxi;
+	size_t offset;
+
+	if (check_mul_overflow((size_t)cxt->id, (size_t)sdxi->db_stride,
+			       &offset) ||
+	    offset > sdxi->db_size ||
+	    sdxi->db_stride > sdxi->db_size - offset)
+		return -ERANGE;
+
+	if (WARN_ON_ONCE(!cxt->sq))
+		return -EINVAL;
+	cxt->sq->doorbell = sdxi->dbs + offset;
+	return 0;
+}
+
 /*
  * Transfer the reserved ID to the context and store its pointer in client_cxts.
  * sdxi_free_cxt() releases the ID.
@@ -401,6 +436,9 @@ static void sdxi_cxt_id_assign(struct sdxi_cxt *cxt, struct sdxi_cxt_id *cxt_id)
  */
 struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi)
 {
+	struct sdxi_cxt_ctl_cfg ctl_cfg;
+	int err;
+
 	/*
 	 * Ensure an ID is available before allocating memory for the
 	 * context and its control structures.
@@ -413,13 +451,23 @@ struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi)
 	if (!cxt)
 		return NULL;
 
-	sdxi_cxt_id_assign(cxt, &id);
-
-	cxt->sq->doorbell = sdxi->dbs + cxt->id * sdxi->db_stride;
+	err = sdxi_alloc_kernel_submission(cxt);
+	if (err)
+		return NULL;
 
-	sdxi_ring_state_init(cxt->ring_state, cxt->sq);
+	sdxi_cxt_id_assign(cxt, &id);
+	if (sdxi_cxt_set_doorbell(cxt))
+		return NULL;
 
-	if (sdxi_publish_cxt(cxt))
+	ctl_cfg = (typeof(ctl_cfg)) {
+		.se              = 1,
+		.csa             = 1,
+		.ds_ring_ptr     = sdxi_sq_ring_dma(cxt->sq),
+		.ds_ring_sz      = cxt->sq->count,
+		.cxt_sts_ptr     = sdxi_sq_cxt_sts_dma(cxt->sq),
+		.write_index_ptr = sdxi_sq_write_index_dma(cxt->sq),
+	};
+	if (sdxi_publish_cxt(cxt, &ctl_cfg))
 		return NULL;
 
 	return_ptr(cxt);
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index 196207cbca94..8e57565c0707 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -36,6 +36,9 @@ static inline u8 akey_table_order(const struct sdxi_akey_table *tbl)
 struct sdxi_cxt {
 	struct sdxi_dev *sdxi;
 	u16 id;
+	u32 pasid;
+	u32 opb_000_enb;
+	bool pasid_valid;
 
 	struct sdxi_cxt_ctl *cxt_ctl;
 	dma_addr_t cxt_ctl_dma;
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index fa9421a5410b..4b54b14fefc1 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -370,6 +370,8 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	cap0 = sdxi_read64(sdxi, SDXI_MMIO_CAP0);
 	sdxi->db_stride = SZ_4K;
 	sdxi->db_stride *= 1U << FIELD_GET(SDXI_MMIO_CAP0_DB_STRIDE, cap0);
+	if (sdxi->db_stride > sdxi->db_size)
+		return -ENODEV;
 
 	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/pci.c b/drivers/dma/sdxi/pci.c
index f9f75ff5e179..7b477b7bec55 100644
--- a/drivers/dma/sdxi/pci.c
+++ b/drivers/dma/sdxi/pci.c
@@ -59,6 +59,7 @@ static int sdxi_pci_init(struct sdxi_dev *sdxi)
 	if (IS_ERR(sdxi->dbs))
 		return dev_err_probe(dev, PTR_ERR(sdxi->dbs),
 				     "failed to map doorbell region\n");
+	sdxi->db_size = pci_resource_len(pdev, SDXI_PCI_BAR_DOORBELL);
 
 	/* Mailbox registers are reserved without SR-IOV support. */
 	sdxi->has_mailbox = pdev->is_virtfn ||
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 65e0191391db..fa6c3ef1c58f 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -74,6 +74,7 @@ struct sdxi_dev {
 	struct device *dev;
 	void __iomem *ctrl_regs;	/* virt addr of ctrl registers */
 	void __iomem *dbs;		/* virt addr of doorbells */
+	size_t db_size;			/* mapped doorbell region size */
 
 	struct sdxi_version sdxi_version;
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 11/44] dmaengine: sdxi: support userspace context lifetimes
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (9 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 10/44] dmaengine: sdxi: separate context and submission resources Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 12/44] dmaengine: sdxi: route context interrupts to eventfd Wei Huang
                   ` (32 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add per-open process state with ordered SVA and MMU-notifier teardown,
and extend contexts with refcounted fd/VMA lifetime, deferred quiesce,
fatal-state handling, and an internal PASID-backed userspace constructor.
These are used to support userspace context later. Note that we still
preserve the existing xarray registry and kernel SQ/CQ ownership.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/.kunitconfig |   1 +
 drivers/dma/sdxi/Kconfig      |   7 +-
 drivers/dma/sdxi/Makefile     |   1 +
 drivers/dma/sdxi/admin.c      |  17 +-
 drivers/dma/sdxi/context.c    | 731 +++++++++++++++++++++++++++++++---
 drivers/dma/sdxi/context.h    |  39 ++
 drivers/dma/sdxi/device.c     |  10 +
 drivers/dma/sdxi/process.c    | 186 +++++++++
 drivers/dma/sdxi/process.h    |  43 ++
 drivers/dma/sdxi/sdxi.h       |   2 +
 10 files changed, 971 insertions(+), 66 deletions(-)
 create mode 100644 drivers/dma/sdxi/process.c
 create mode 100644 drivers/dma/sdxi/process.h

diff --git a/drivers/dma/sdxi/.kunitconfig b/drivers/dma/sdxi/.kunitconfig
index 16a1ae04b156..f3c468fe26c6 100644
--- a/drivers/dma/sdxi/.kunitconfig
+++ b/drivers/dma/sdxi/.kunitconfig
@@ -1,4 +1,5 @@
 CONFIG_KUNIT=y
 CONFIG_DMADEVICES=y
+CONFIG_IOMMU_SUPPORT=y
 CONFIG_SDXI_CORE=y
 CONFIG_SDXI_KUNIT_TEST=y
diff --git a/drivers/dma/sdxi/Kconfig b/drivers/dma/sdxi/Kconfig
index e01b5365c154..9b8416304ee2 100644
--- a/drivers/dma/sdxi/Kconfig
+++ b/drivers/dma/sdxi/Kconfig
@@ -2,9 +2,12 @@ config SDXI_CORE
 	tristate "SDXI (Smart Data Accelerator Interface) support"
 	# Doorbell updates require naturally-aligned 64-bit writes, per
 	# SDXI 1.0 9 MMIO Control Registers.
-	depends on 64BIT
+	depends on 64BIT && IOMMU_SUPPORT
 	select DMA_ENGINE
 	select DMA_VIRTUAL_CHANNELS
+	select IOMMU_API
+	select IOMMU_SVA
+	select MMU_NOTIFIER
 	select PACKING
 	help
 	  Enable support for Smart Data Accelerator Interface (SDXI)
@@ -21,7 +24,7 @@ config SDXI_CORE
 
 config SDXI_PCI
 	tristate "SDXI (Smart Data Accelerator Interface) PCI driver"
-	depends on PCI_MSI && 64BIT
+	depends on PCI_MSI && 64BIT && IOMMU_SUPPORT
 	select SDXI_CORE
 	help
 	  Enable support for PCI-hosted SDXI devices.
diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index 3f49bc9af955..f20a7c39dfc9 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -9,6 +9,7 @@ sdxi-core-y := \
 	dma.o         \
 	error.o       \
 	keys.o        \
+	process.o     \
 	ring.o
 
 obj-$(CONFIG_SDXI_PCI) += sdxi-pci.o
diff --git a/drivers/dma/sdxi/admin.c b/drivers/dma/sdxi/admin.c
index ed3890de8cc1..b7c8d4ab18e7 100644
--- a/drivers/dma/sdxi/admin.c
+++ b/drivers/dma/sdxi/admin.c
@@ -143,8 +143,13 @@ int sdxi_adm_start_cxt_sync(struct sdxi_cxt *cxt, unsigned int timeout_ms,
 	if (ret)
 		return ret;
 
-	return sdxi_adm_submit(to_admin_cxt(cxt), descs, ARRAY_SIZE(descs),
-			       timeout_ms, cst_flags, interruptible);
+	/* A failed or interrupted wait can leave the context running. */
+	WRITE_ONCE(cxt->quiesced, false);
+	WRITE_ONCE(cxt->started, true);
+	ret = sdxi_adm_submit(to_admin_cxt(cxt), descs, ARRAY_SIZE(descs),
+			      timeout_ms, cst_flags, interruptible);
+
+	return ret;
 }
 
 int sdxi_adm_stop_cxt_sync(struct sdxi_cxt *cxt, unsigned int timeout_ms,
@@ -169,8 +174,12 @@ int sdxi_adm_stop_cxt_sync(struct sdxi_cxt *cxt, unsigned int timeout_ms,
 	if (ret)
 		return ret;
 
-	return sdxi_adm_submit(to_admin_cxt(cxt), descs, ARRAY_SIZE(descs),
-			       timeout_ms, cst_flags, interruptible);
+	ret = sdxi_adm_submit(to_admin_cxt(cxt), descs, ARRAY_SIZE(descs),
+			      timeout_ms, cst_flags, interruptible);
+	if (!ret)
+		WRITE_ONCE(cxt->started, false);
+
+	return ret;
 }
 
 int sdxi_adm_rkey_update_sync(struct sdxi_dev *sdxi, u16 rkey_start,
diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index 834223e7b003..fe7996a024d6 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -18,6 +18,7 @@
 #include <linux/idr.h>
 #include <linux/iommu.h>
 #include <linux/overflow.h>
+#include <linux/poll.h>
 #include <linux/slab.h>
 #include <linux/types.h>
 #include <asm/barrier.h>
@@ -28,11 +29,13 @@
 #include "context.h"
 #include "descriptor.h"
 #include "hw.h"
+#include "process.h"
 #include "ring.h"
 #include "sdxi.h"
 #include "submission.h"
 
 #define DEFAULT_DESC_RING_ENTRIES 1024
+#define SDXI_CXT_QUIESCE_TIMEOUT_MS 5000U
 
 enum {
 	/*
@@ -42,6 +45,8 @@ enum {
 	SDXI_ADMIN_CXT_ID = 0,
 };
 
+static void sdxi_cxt_cleanup_workfn(struct work_struct *work);
+
 /*
  * Free context and its resources. @cxt may be partially allocated but
  * must have ->sdxi set.
@@ -50,8 +55,8 @@ static void sdxi_free_cxt(struct sdxi_cxt *cxt)
 {
 	struct sdxi_dev *sdxi = cxt->sdxi;
 
-	/* Release the id if this is a client context. */
-	if (cxt->id)
+	/* Release the id if this is a registered client context. */
+	if (cxt->registered)
 		WARN_ON(xa_erase(&sdxi->client_cxts, cxt->id) != cxt);
 
 	if (cxt->cxt_ctl)
@@ -65,6 +70,8 @@ static void sdxi_free_cxt(struct sdxi_cxt *cxt)
 	if (cxt->sq)
 		sdxi_sq_free(sdxi, cxt->sq);
 	ida_destroy(&cxt->akey_ida);
+	mutex_destroy(&cxt->akey_lock);
+	mutex_destroy(&cxt->cleanup_lock);
 	kfree(cxt->ring_state);
 	kfree(cxt);
 }
@@ -81,6 +88,11 @@ static struct sdxi_cxt *sdxi_alloc_cxt(struct sdxi_dev *sdxi)
 		return NULL;
 
 	cxt->sdxi = sdxi;
+	refcount_set(&cxt->refs, 1);
+	mutex_init(&cxt->cleanup_lock);
+	mutex_init(&cxt->akey_lock);
+	init_waitqueue_head(&cxt->poll_wq);
+	INIT_DELAYED_WORK(&cxt->cleanup_work, sdxi_cxt_cleanup_workfn);
 	ida_init(&cxt->akey_ida);
 	cxt->pasid = IOMMU_NO_PASID;
 	cxt->opb_000_enb = sdxi->op_grp_cap;
@@ -99,6 +111,81 @@ static struct sdxi_cxt *sdxi_alloc_cxt(struct sdxi_dev *sdxi)
 	return_ptr(cxt);
 }
 
+/* Claim completion storage before making a ring reservation irrevocable. */
+static int sdxi_cxt_reserve(struct sdxi_cxt *cxt, size_t nr,
+			    struct sdxi_ring_resv *resv,
+			    struct sdxi_completion *comp)
+{
+	int err;
+
+	err = sdxi_cst_pool_get_wait(cxt->cst_pool, comp);
+	if (err)
+		return err;
+
+	err = sdxi_ring_reserve(cxt->ring_state, nr, resv);
+	if (err)
+		sdxi_cst_pool_put(cxt->cst_pool, comp->cst_blk);
+
+	return err;
+}
+
+static int sdxi_cxt_poll_completion(struct sdxi_cxt *cxt,
+				    const struct sdxi_completion *comp)
+{
+	int err = sdxi_cst_blk_poll(comp->cst_blk);
+
+	/*
+	 * A timeout does not end hardware ownership. Keep these administrative
+	 * blocks claimed until context teardown, even if they complete later.
+	 * Repeated timeouts can exhaust the pool; allocation then times out too.
+	 */
+	if (err != -ETIMEDOUT)
+		sdxi_cst_pool_put(cxt->cst_pool, comp->cst_blk);
+	sdxi_ring_wake_up(cxt->ring_state);
+
+	return err;
+}
+
+/*
+ * Signal the function that a client context's control structures have
+ * been modified so it discards any privately cached copies of them.
+ * DSC_SYNC.CXT is required before the operation can be considered
+ * complete.
+ */
+static int sdxi_cxt_update_locked(const struct sdxi_cxt *cxt)
+{
+	struct sdxi_cxt *adm = to_admin_cxt(cxt);
+	struct sdxi_slot upd, sync;
+	struct sdxi_ring_resv resv;
+	struct sdxi_completion comp;
+	int err;
+
+	might_sleep();
+	lockdep_assert_held(&cxt->sdxi->admin_lock);
+	if (cxt->sdxi->admin_dead)
+		return -EIO;
+
+	err = sdxi_cxt_reserve(adm, 2, &resv, &comp);
+	if (err)
+		return err;
+
+	upd = sdxi_ring_resv_slot(&resv, 0);
+	sync = sdxi_ring_resv_slot(&resv, 1);
+
+	sdxi_encode_cxt_upd(sdxi_slot_desc(upd), &(const struct sdxi_cxt_upd) {
+			.level = SDXI_CXT_UPD_LVL_L1,
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	sdxi_encode_sync(sdxi_slot_desc(sync), &(const struct sdxi_sync) {
+			.filter = SDXI_SYNC_FLT_CXT,
+			.range = sdxi_cxt_range_single(cxt->id),
+		});
+	sdxi_cxt_submit(adm, upd, NULL, 0);
+	sdxi_cxt_submit(adm, sync, &comp, SDXI_CXT_SUBMIT_KICK);
+
+	return sdxi_cxt_poll_completion(adm, &comp);
+}
+
 static int sdxi_alloc_kernel_submission(struct sdxi_cxt *cxt)
 {
 	cxt->ring_state = kzalloc_obj(*cxt->ring_state, GFP_KERNEL);
@@ -259,12 +346,33 @@ sdxi_get_L1_table(struct sdxi_dev *sdxi, u16 l2_idx)
 	return table;
 }
 
+static void sdxi_put_L1_table(struct sdxi_dev *sdxi, u16 l2_idx)
+{
+	struct sdxi_cxt_L1_table *table;
+	struct sdxi_cxt_L2_ent *L2_ent;
+
+	lockdep_assert_held(&sdxi->cxt_lock);
+
+	if (sdxi->L1_users[l2_idx])
+		return;
+
+	table = sdxi->L1_tables[l2_idx];
+	if (!table)
+		return;
+
+	L2_ent = &sdxi->L2_table->entry[l2_idx];
+	WRITE_ONCE(L2_ent->lv01_ptr, 0);
+	dma_wmb();
+
+	sdxi->L1_tables[l2_idx] = NULL;
+	sdxi->L1_dma[l2_idx] = 0;
+}
+
 /*
  * Publish the context control structure hierarchy to the SDXI
- * implementation. L1 pages remain allocated until device teardown so a
- * cached L2 pointer can never refer to freed memory.
+ * implementation. This may allocate an L1 table page, so it is fallible.
  */
-static int sdxi_publish_cxt(const struct sdxi_cxt *cxt,
+static int sdxi_publish_cxt(struct sdxi_cxt *cxt,
 			    const struct sdxi_cxt_ctl_cfg *ctl_cfg)
 {
 	struct sdxi_cxt_L1_cfg L1_cfg;
@@ -311,19 +419,325 @@ static int sdxi_publish_cxt(const struct sdxi_cxt *cxt,
 
 out_unlock:
 	mutex_unlock(&cxt->sdxi->cxt_lock);
-	if (err)
+	if (err) {
 		invalidate_cxt_ctl(cxt->cxt_ctl);
+	} else {
+		cxt->published = true;
+		cxt->update_pending = false;
+	}
+	return err;
+}
+
+/* Invalidate a context. */
+static void sdxi_rescind_cxt(struct sdxi_cxt *cxt)
+{
+	struct sdxi_cxt_L1_table *L1_table;
+	struct sdxi_dev *sdxi = cxt->sdxi;
+	u16 l2_idx = ID_TO_L2_INDEX(cxt->id);
+	u8 l1_idx = ID_TO_L1_INDEX(cxt->id);
+
+	if (!cxt->published)
+		return;
+
+	mutex_lock(&sdxi->cxt_lock);
+	L1_table = sdxi->L1_tables[l2_idx];
+	if (WARN_ON_ONCE(!L1_table))
+		goto out_unlock;
+
+	invalidate_L1_entry(&L1_table->entry[l1_idx]);
+	if (WARN_ON_ONCE(!sdxi->L1_users[l2_idx]))
+		goto out_unlock;
+
+	sdxi->L1_users[l2_idx]--;
+
+out_unlock:
+	mutex_unlock(&sdxi->cxt_lock);
+	invalidate_cxt_ctl(cxt->cxt_ctl);
+	cxt->published = false;
+	cxt->update_pending = true;
+}
+
+/* Empty L1 tables remain published until the function has stopped. */
+static void sdxi_cxt_finish_unpublish(struct sdxi_cxt *cxt,
+				      bool function_stopped)
+{
+	struct sdxi_dev *sdxi = cxt->sdxi;
+	u16 l2_idx = ID_TO_L2_INDEX(cxt->id);
+
+	if (!cxt->update_pending)
+		return;
+
+	if (function_stopped) {
+		mutex_lock(&sdxi->cxt_lock);
+		sdxi_put_L1_table(sdxi, l2_idx);
+		mutex_unlock(&sdxi->cxt_lock);
+	}
+	cxt->update_pending = false;
+}
+
+static int sdxi_cxt_unpublish(struct sdxi_cxt *cxt)
+{
+	int err;
+
+	mutex_lock(&cxt->sdxi->admin_lock);
+	if (cxt->published)
+		sdxi_rescind_cxt(cxt);
+	if (!cxt->update_pending) {
+		err = 0;
+		goto out_unlock;
+	}
+
+	err = sdxi_cxt_update_locked(cxt);
+	if (!err)
+		sdxi_cxt_finish_unpublish(cxt, false);
+
+out_unlock:
+	mutex_unlock(&cxt->sdxi->admin_lock);
 	return err;
 }
 
+/* Hardware has stopped, so no update/sync is needed before freeing tables. */
+static void sdxi_cxt_force_unpublish(struct sdxi_cxt *cxt)
+{
+	if (cxt->published)
+		sdxi_rescind_cxt(cxt);
+	sdxi_cxt_finish_unpublish(cxt, true);
+}
+
+void sdxi_cxt_get(struct sdxi_cxt *cxt)
+{
+	refcount_inc(&cxt->refs);
+}
+
+void sdxi_cxt_put(struct sdxi_cxt *cxt)
+{
+	if (cxt && refcount_dec_and_test(&cxt->refs))
+		sdxi_free_cxt(cxt);
+}
+
+struct sdxi_cxt *sdxi_cxt_get_by_id(struct sdxi_dev *sdxi, u16 cxt_id)
+{
+	struct sdxi_cxt *cxt;
+
+	xa_lock(&sdxi->client_cxts);
+	cxt = xa_load(&sdxi->client_cxts, cxt_id);
+	if (cxt && !refcount_inc_not_zero(&cxt->refs))
+		cxt = NULL;
+	xa_unlock(&sdxi->client_cxts);
+
+	return cxt;
+}
+
+/* Free resources only after update/sync or after the function has stopped. */
+static void sdxi_cxt_release_resources(struct sdxi_cxt *cxt)
+{
+	struct sdxi_dev *sdxi = cxt->sdxi;
+
+	if (cxt->registered) {
+		WARN_ON(xa_erase(&sdxi->client_cxts, cxt->id) != cxt);
+		cxt->registered = false;
+	}
+
+	if (cxt->cxt_ctl) {
+		dma_pool_free(sdxi->cxt_ctl_pool, cxt->cxt_ctl,
+			      cxt->cxt_ctl_dma);
+		cxt->cxt_ctl = NULL;
+		cxt->cxt_ctl_dma = 0;
+	}
+	if (cxt->akey_table) {
+		mutex_lock(&cxt->akey_lock);
+		dma_free_coherent(sdxi->dev, sizeof(*cxt->akey_table),
+				  cxt->akey_table, cxt->akey_table_dma);
+		cxt->akey_table = NULL;
+		cxt->akey_table_dma = 0;
+		mutex_unlock(&cxt->akey_lock);
+	}
+	if (cxt->cst_pool) {
+		sdxi_cst_pool_destroy(sdxi, cxt->cst_pool);
+		cxt->cst_pool = NULL;
+	}
+	if (cxt->sq) {
+		sdxi_sq_free(sdxi, cxt->sq);
+		cxt->sq = NULL;
+	}
+	kfree(cxt->ring_state);
+	cxt->ring_state = NULL;
+}
+
+static void sdxi_cxt_finish_release(struct sdxi_cxt *cxt,
+				    struct sdxi_process *process)
+{
+	struct iommu_sva *sva = NULL;
+	u32 pasid = 0;
+
+	wake_up_poll(&cxt->poll_wq, EPOLLHUP);
+	sdxi_cxt_release_resources(cxt);
+
+	if (process) {
+		mutex_lock(&process->mutex);
+		sva = sdxi_process_detach_sva_locked(process, &pasid);
+		mutex_unlock(&process->mutex);
+
+		sdxi_process_unbind_sva(process, sva, pasid);
+
+		/* Keep CREATE_CXT excluded until the old SVA is fully unbound. */
+		mutex_lock(&process->mutex);
+		if (process->cxt == cxt)
+			process->cxt = NULL;
+		mutex_unlock(&process->mutex);
+		sdxi_process_put(process);
+	}
+
+	/* Drop the context's base ownership reference. */
+	sdxi_cxt_put(cxt);
+}
+
+static bool sdxi_cxt_cleanup_requested(const struct sdxi_cxt *cxt)
+{
+	return cxt->handle_closed || cxt->mm_dead || READ_ONCE(cxt->fatal);
+}
+
+static void sdxi_cxt_queue_cleanup_locked(struct sdxi_cxt *cxt,
+					  unsigned long delay)
+{
+	lockdep_assert_held(&cxt->cleanup_lock);
+
+	if (!cxt->cleanup_stopping && !cxt->resources_released)
+		mod_delayed_work(system_percpu_wq, &cxt->cleanup_work, delay);
+}
+
+static int sdxi_cxt_quiesce_locked(struct sdxi_cxt *cxt,
+				   unsigned int timeout_ms, u32 *cst_flags,
+				   bool interruptible)
+{
+	u32 flags = 0;
+	int ret = 0;
+
+	lockdep_assert_held(&cxt->cleanup_lock);
+
+	if (cxt->resources_released)
+		return -ENODEV;
+	if (cxt->quiesced)
+		goto out;
+
+	if (!cxt->started || sdxi_cxt_is_admin(cxt))
+		goto quiesced;
+
+	if (!timeout_ms)
+		timeout_ms = SDXI_CXT_QUIESCE_TIMEOUT_MS;
+
+	ret = sdxi_adm_stop_cxt_sync(cxt, timeout_ms, &flags,
+				     interruptible);
+	if (ret)
+		return ret;
+
+quiesced:
+	cxt->started = false;
+	cxt->quiesced = true;
+	cxt->quiesce_cst_flags = flags;
+out:
+	if (cst_flags)
+		*cst_flags = cxt->quiesce_cst_flags;
+	return 0;
+}
+
+static void sdxi_cxt_cleanup_workfn(struct work_struct *work)
+{
+	struct sdxi_cxt *cxt = container_of(to_delayed_work(work),
+					     struct sdxi_cxt, cleanup_work);
+	struct sdxi_process *process = NULL;
+	bool force_release = false;
+	bool destroy = false;
+	int ret;
+
+	mutex_lock(&cxt->cleanup_lock);
+	if (cxt->resources_released || !sdxi_cxt_cleanup_requested(cxt))
+		goto out_unlock;
+
+	if (!cxt->quiesced) {
+		ret = sdxi_cxt_quiesce_locked(cxt, 0, NULL, false);
+		if (ret) {
+			dev_err(cxt->sdxi->dev,
+				"failed to quiesce context %u: %d; quiescing function\n",
+				cxt->id, ret);
+			sdxi_dev_fail(cxt->sdxi);
+			cxt->started = false;
+			cxt->quiesced = true;
+			cxt->quiesce_cst_flags = 0;
+			force_release = true;
+		}
+	}
+	if (cxt->vma_refs)
+		goto out_unlock;
+
+	if (!force_release) {
+		ret = sdxi_cxt_unpublish(cxt);
+		if (ret) {
+			dev_err(cxt->sdxi->dev,
+				"failed to synchronize context %u release: %d; quiescing function\n",
+				cxt->id, ret);
+			sdxi_dev_fail(cxt->sdxi);
+			force_release = true;
+		}
+	}
+	if (force_release)
+		sdxi_cxt_force_unpublish(cxt);
+
+	cxt->resources_released = true;
+	cxt->cleanup_stopping = true;
+	process = cxt->process;
+	cxt->process = NULL;
+	destroy = true;
+
+out_unlock:
+	mutex_unlock(&cxt->cleanup_lock);
+
+	if (destroy)
+		sdxi_cxt_finish_release(cxt, process);
+}
+
+/* Device teardown has already stopped hardware, so force resource release. */
+static void sdxi_cxt_force_exit(struct sdxi_cxt *cxt)
+{
+	struct sdxi_process *process = NULL;
+	bool destroy = false;
+
+	if (!cxt || !refcount_inc_not_zero(&cxt->refs))
+		return;
+
+	mutex_lock(&cxt->cleanup_lock);
+	cxt->cleanup_stopping = true;
+	mutex_unlock(&cxt->cleanup_lock);
+	cancel_delayed_work_sync(&cxt->cleanup_work);
+
+	mutex_lock(&cxt->cleanup_lock);
+	if (!cxt->resources_released) {
+		sdxi_cxt_force_unpublish(cxt);
+		cxt->started = false;
+		cxt->quiesced = true;
+		cxt->resources_released = true;
+		process = cxt->process;
+		cxt->process = NULL;
+		destroy = true;
+	}
+	mutex_unlock(&cxt->cleanup_lock);
+
+	if (destroy)
+		sdxi_cxt_finish_release(cxt, process);
+	/* Drop the temporary reference acquired above. */
+	sdxi_cxt_put(cxt);
+}
+
 static void free_admin_cxt(void *ptr)
 {
 	struct sdxi_dev *sdxi = ptr;
 
-	sdxi_free_cxt(sdxi->admin_cxt);
+	sdxi_cxt_force_exit(sdxi->admin_cxt);
 }
 
-static int sdxi_cxt_set_doorbell(struct sdxi_cxt *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);
 
 int sdxi_admin_cxt_init(struct sdxi_dev *sdxi)
 {
@@ -343,7 +757,7 @@ int sdxi_admin_cxt_init(struct sdxi_dev *sdxi)
 	/* SDXI 1.0 4.1.8.4.b: Set CXT_STS.state to CXTV_RUN. */
 	sq->cxt_sts.state = FIELD_PREP(SDXI_CXT_STS_STATE, CXTV_RUN);
 	cxt->id = SDXI_ADMIN_CXT_ID;
-	err = sdxi_cxt_set_doorbell(cxt);
+	err = sdxi_cxt_set_kernel_doorbell(cxt);
 	if (err)
 		return err;
 
@@ -359,6 +773,7 @@ int sdxi_admin_cxt_init(struct sdxi_dev *sdxi)
 	if (err)
 		return err;
 
+	cxt->started = true;
 	sdxi->admin_cxt = no_free_ptr(cxt);
 
 	return devm_add_action_or_reset(sdxi->dev, free_admin_cxt, sdxi);
@@ -400,20 +815,35 @@ static bool sdxi_cxt_id_valid(const struct sdxi_cxt_id *cxt_id)
 	return cxt_id->index > 0;
 }
 
-static int sdxi_cxt_set_doorbell(struct sdxi_cxt *cxt)
+static int sdxi_cxt_doorbell_offset(const struct sdxi_cxt *cxt,
+				    size_t *offset)
 {
 	struct sdxi_dev *sdxi = cxt->sdxi;
-	size_t offset;
+	size_t value;
 
 	if (check_mul_overflow((size_t)cxt->id, (size_t)sdxi->db_stride,
-			       &offset) ||
-	    offset > sdxi->db_size ||
-	    sdxi->db_stride > sdxi->db_size - offset)
+			       &value) ||
+	    value > sdxi->db_size ||
+	    sdxi->db_stride > sdxi->db_size - value)
 		return -ERANGE;
 
+	if (offset)
+		*offset = value;
+	return 0;
+}
+
+static int sdxi_cxt_set_kernel_doorbell(struct sdxi_cxt *cxt)
+{
+	size_t offset;
+	int err;
+
 	if (WARN_ON_ONCE(!cxt->sq))
 		return -EINVAL;
-	cxt->sq->doorbell = sdxi->dbs + offset;
+	err = sdxi_cxt_doorbell_offset(cxt, &offset);
+	if (err)
+		return err;
+
+	cxt->sq->doorbell = cxt->sdxi->dbs + offset;
 	return 0;
 }
 
@@ -427,9 +857,34 @@ static void sdxi_cxt_id_assign(struct sdxi_cxt *cxt, struct sdxi_cxt_id *cxt_id)
 	WARN_ON(xa_store(&cxt_id->sdxi->client_cxts,
 			 cxt_id->index, cxt, GFP_KERNEL));
 	cxt->id = cxt_id->index;
+	cxt->registered = true;
 	cxt_id->index = 0;
 }
 
+static struct sdxi_cxt *sdxi_alloc_client_cxt(struct sdxi_dev *sdxi)
+{
+	CLASS(sdxi_cxt_id, id)(sdxi);
+	struct sdxi_cxt *cxt __free(sdxi_cxt) = NULL;
+
+	if (!sdxi_cxt_id_valid(&id))
+		return NULL;
+
+	cxt = sdxi_alloc_cxt(sdxi);
+	if (!cxt)
+		return NULL;
+
+	sdxi_cxt_id_assign(cxt, &id);
+	if (sdxi_cxt_doorbell_offset(cxt, NULL))
+		return NULL;
+	mutex_lock(&sdxi->cxt_lock);
+	cxt->mmap_gen = ++sdxi->mmap_gen;
+	if (!cxt->mmap_gen)
+		cxt->mmap_gen = ++sdxi->mmap_gen;
+	mutex_unlock(&sdxi->cxt_lock);
+
+	return_ptr(cxt);
+}
+
 /*
  * Allocate a context for in-kernel use. Starting the context is the
  * caller's responsibility.
@@ -437,27 +892,19 @@ static void sdxi_cxt_id_assign(struct sdxi_cxt *cxt, struct sdxi_cxt_id *cxt_id)
 struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi)
 {
 	struct sdxi_cxt_ctl_cfg ctl_cfg;
+	struct sdxi_cxt *cxt;
 	int err;
 
-	/*
-	 * Ensure an ID is available before allocating memory for the
-	 * context and its control structures.
-	 */
-	CLASS(sdxi_cxt_id, id)(sdxi);
-	if (!sdxi_cxt_id_valid(&id))
-		return NULL;
-
-	struct sdxi_cxt *cxt __free(sdxi_cxt) = sdxi_alloc_cxt(sdxi);
+	cxt = sdxi_alloc_client_cxt(sdxi);
 	if (!cxt)
 		return NULL;
 
 	err = sdxi_alloc_kernel_submission(cxt);
 	if (err)
-		return NULL;
-
-	sdxi_cxt_id_assign(cxt, &id);
-	if (sdxi_cxt_set_doorbell(cxt))
-		return NULL;
+		goto put_cxt;
+	err = sdxi_cxt_set_kernel_doorbell(cxt);
+	if (err)
+		goto put_cxt;
 
 	ctl_cfg = (typeof(ctl_cfg)) {
 		.se              = 1,
@@ -468,9 +915,41 @@ struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi)
 		.write_index_ptr = sdxi_sq_write_index_dma(cxt->sq),
 	};
 	if (sdxi_publish_cxt(cxt, &ctl_cfg))
+		goto put_cxt;
+
+	return cxt;
+
+put_cxt:
+	sdxi_cxt_put(cxt);
+	return NULL;
+}
+
+struct sdxi_cxt *sdxi_ucxt_new(struct sdxi_dev *sdxi, u32 ring_entries,
+			       u64 ds_ring, u64 write_index, u64 cxt_sts,
+			       u32 pasid, u32 opb_000_enb)
+{
+	struct sdxi_cxt_ctl_cfg ctl_cfg = {
+		.ds_ring_ptr = ds_ring,
+		.ds_ring_sz = ring_entries,
+		.cxt_sts_ptr = cxt_sts,
+		.write_index_ptr = write_index,
+	};
+	struct sdxi_cxt *cxt;
+
+	cxt = sdxi_alloc_client_cxt(sdxi);
+	if (!cxt)
 		return NULL;
 
-	return_ptr(cxt);
+	cxt->pasid_valid = true;
+	cxt->pasid = pasid;
+	cxt->opb_000_enb = opb_000_enb;
+
+	if (sdxi_publish_cxt(cxt, &ctl_cfg)) {
+		sdxi_cxt_put(cxt);
+		return NULL;
+	}
+
+	return cxt;
 }
 
 /*
@@ -481,10 +960,17 @@ void sdxi_client_cxts_exit(struct sdxi_dev *sdxi)
 {
 	struct sdxi_cxt *cxt;
 	unsigned long index;
+	size_t l2_idx;
 
 	xa_for_each(&sdxi->client_cxts, index, cxt)
-		sdxi_free_cxt(cxt);
+		sdxi_cxt_force_exit(cxt);
 	xa_destroy(&sdxi->client_cxts);
+
+	/* Reclaim empty pages retained to keep L2 pointers stable at runtime. */
+	mutex_lock(&sdxi->cxt_lock);
+	for (l2_idx = 0; l2_idx < ARRAY_SIZE(sdxi->L1_tables); l2_idx++)
+		sdxi_put_L1_table(sdxi, l2_idx);
+	mutex_unlock(&sdxi->cxt_lock);
 }
 
 /*
@@ -529,43 +1015,168 @@ void sdxi_cxt_submit(struct sdxi_cxt *cxt, struct sdxi_slot slot,
  */
 int sdxi_cxt_exit(struct sdxi_cxt *cxt)
 {
-	struct sdxi_dev *sdxi = cxt->sdxi;
-	struct sdxi_cxt_L1_table *L1_table;
-	u16 l2_idx = ID_TO_L2_INDEX(cxt->id);
-	u8 l1_idx = ID_TO_L1_INDEX(cxt->id);
-	u8 state;
-	int err;
+	struct sdxi_process *process = NULL;
+	bool destroy = false;
+	int ret = 0;
 
+	if (!cxt)
+		return -EINVAL;
 	if (WARN_ON(sdxi_cxt_is_admin(cxt)))
 		return -EINVAL;
+	if (!refcount_inc_not_zero(&cxt->refs))
+		return -ENODEV;
 
-	state = FIELD_GET(SDXI_CXT_STS_STATE, READ_ONCE(cxt->sq->cxt_sts.state));
-	if (state != CXTV_STOP_SW && state != CXTV_STOP_FN && state != CXTV_ERR_FN)
-		return -EBUSY;
+	mutex_lock(&cxt->cleanup_lock);
+	cxt->cleanup_stopping = true;
+	mutex_unlock(&cxt->cleanup_lock);
+	cancel_delayed_work_sync(&cxt->cleanup_work);
 
-	mutex_lock(&sdxi->cxt_lock);
-	L1_table = sdxi->L1_tables[l2_idx];
-	if (WARN_ON_ONCE(!L1_table)) {
-		mutex_unlock(&sdxi->cxt_lock);
-		return -EIO;
+	mutex_lock(&cxt->cleanup_lock);
+	if (cxt->resources_released)
+		goto out_unlock;
+	if (cxt->vma_refs) {
+		ret = -EBUSY;
+		goto out_unlock;
 	}
-	invalidate_L1_entry(&L1_table->entry[l1_idx]);
-	invalidate_cxt_ctl(cxt->cxt_ctl);
-	mutex_unlock(&sdxi->cxt_lock);
 
-	err = sdxi_adm_update_cxt(cxt);
-	if (err) {
-		dev_err(sdxi->dev,
-			"context %u release failed (%d), retaining resources until function shutdown\n",
-			cxt->id, err);
-		return err;
+	ret = sdxi_cxt_quiesce_locked(cxt, 0, NULL, false);
+	if (ret)
+		goto out_retain;
+
+	ret = sdxi_cxt_unpublish(cxt);
+	if (ret)
+		goto out_retain;
+
+	cxt->resources_released = true;
+	process = cxt->process;
+	cxt->process = NULL;
+	destroy = true;
+	goto out_unlock;
+
+out_retain:
+	dev_err(cxt->sdxi->dev,
+		"context %u release failed (%d), retaining resources until function shutdown\n",
+		cxt->id, ret);
+out_unlock:
+	mutex_unlock(&cxt->cleanup_lock);
+
+	if (destroy)
+		sdxi_cxt_finish_release(cxt, process);
+	/* Drop the temporary reference acquired above. */
+	sdxi_cxt_put(cxt);
+	return ret;
+}
+
+int sdxi_cxt_quiesce(struct sdxi_cxt *cxt, unsigned int timeout_ms,
+		     u32 *cst_flags)
+{
+	int ret;
+
+	if (!cxt)
+		return -EINVAL;
+
+	mutex_lock(&cxt->cleanup_lock);
+	ret = sdxi_cxt_quiesce_locked(cxt, timeout_ms, cst_flags, true);
+	if (!ret && sdxi_cxt_cleanup_requested(cxt))
+		sdxi_cxt_queue_cleanup_locked(cxt, 0);
+	mutex_unlock(&cxt->cleanup_lock);
+
+	return ret;
+}
+
+bool sdxi_cxt_is_user(const struct sdxi_cxt *cxt)
+{
+	return cxt && cxt->id && READ_ONCE(cxt->pasid_valid) &&
+	       READ_ONCE(cxt->process);
+}
+
+bool sdxi_cxt_is_fatal(const struct sdxi_cxt *cxt)
+{
+	return !cxt || READ_ONCE(cxt->fatal);
+}
+
+void sdxi_cxt_kill(struct sdxi_cxt *cxt)
+{
+	if (!cxt)
+		return;
+
+	mutex_lock(&cxt->cleanup_lock);
+	if (cxt->fatal) {
+		mutex_unlock(&cxt->cleanup_lock);
+		return;
 	}
+	cxt->fatal = true;
+	if (sdxi_cxt_is_user(cxt))
+		sdxi_cxt_queue_cleanup_locked(cxt, 0);
+	mutex_unlock(&cxt->cleanup_lock);
 
-	mutex_lock(&sdxi->cxt_lock);
-	if (!WARN_ON_ONCE(!sdxi->L1_users[l2_idx]))
-		sdxi->L1_users[l2_idx]--;
-	mutex_unlock(&sdxi->cxt_lock);
+	wake_up_poll(&cxt->poll_wq, EPOLLERR);
+}
 
-	sdxi_free_cxt(cxt);
-	return 0;
+void sdxi_cxt_release_user_handle(struct sdxi_cxt *cxt)
+{
+	if (!cxt)
+		return;
+
+	mutex_lock(&cxt->cleanup_lock);
+	cxt->handle_closed = true;
+	if (!cxt->resources_released)
+		sdxi_cxt_queue_cleanup_locked(cxt, 0);
+	mutex_unlock(&cxt->cleanup_lock);
+}
+
+int sdxi_cxt_notify_mm_release(struct sdxi_cxt *cxt)
+{
+	int ret = 0;
+
+	if (!cxt)
+		return -EINVAL;
+
+	mutex_lock(&cxt->cleanup_lock);
+	cxt->mm_dead = true;
+	if (!cxt->resources_released) {
+		ret = sdxi_cxt_quiesce_locked(cxt, 0, NULL, false);
+		if (ret) {
+			dev_err(cxt->sdxi->dev,
+				"failed to quiesce context %u during mm release: %d; quiescing function\n",
+				cxt->id, ret);
+			sdxi_dev_fail(cxt->sdxi);
+			cxt->started = false;
+			cxt->quiesced = true;
+			cxt->quiesce_cst_flags = 0;
+		}
+		sdxi_cxt_queue_cleanup_locked(cxt, 0);
+	}
+	mutex_unlock(&cxt->cleanup_lock);
+
+	return ret;
+}
+
+void sdxi_cxt_vma_open(struct sdxi_cxt *cxt)
+{
+	if (!cxt)
+		return;
+
+	sdxi_cxt_get(cxt);
+	mutex_lock(&cxt->cleanup_lock);
+	cxt->vma_refs++;
+	mutex_unlock(&cxt->cleanup_lock);
+}
+
+void sdxi_cxt_vma_close(struct sdxi_cxt *cxt)
+{
+	if (!cxt)
+		return;
+
+	mutex_lock(&cxt->cleanup_lock);
+	if (WARN_ON_ONCE(!cxt->vma_refs))
+		goto out_unlock;
+
+	cxt->vma_refs--;
+	if (!cxt->resources_released && sdxi_cxt_cleanup_requested(cxt))
+		sdxi_cxt_queue_cleanup_locked(cxt, 0);
+
+out_unlock:
+	mutex_unlock(&cxt->cleanup_lock);
+	sdxi_cxt_put(cxt);
 }
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index 8e57565c0707..eed8cef453df 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -10,8 +10,12 @@
 #include <linux/dma-mapping.h>
 #include <linux/idr.h>
 #include <linux/io.h>
+#include <linux/mutex.h>
+#include <linux/refcount.h>
 #include <linux/string.h>
 #include <linux/types.h>
+#include <linux/wait.h>
+#include <linux/workqueue.h>
 
 #include "hw.h"
 #include "ring.h"
@@ -20,6 +24,7 @@
 
 struct sdxi_completion;
 struct sdxi_cst_pool;
+struct sdxi_process;
 
 /* Use a 4 KiB AKey table, the minimum architectural size. */
 struct sdxi_akey_table {
@@ -35,15 +40,33 @@ static inline u8 akey_table_order(const struct sdxi_akey_table *tbl)
 
 struct sdxi_cxt {
 	struct sdxi_dev *sdxi;
+	refcount_t refs;
+	struct mutex cleanup_lock;	/* lifetime state serialization */
+	wait_queue_head_t poll_wq;
+	struct delayed_work cleanup_work;
 	u16 id;
+	u32 mmap_gen;
 	u32 pasid;
 	u32 opb_000_enb;
+	u32 quiesce_cst_flags;
+	unsigned int vma_refs;
 	bool pasid_valid;
+	bool handle_closed;
+	bool mm_dead;
+	bool fatal;
+	bool quiesced;
+	bool cleanup_stopping;
+	bool resources_released;
+	bool registered;
+	bool published;
+	bool update_pending;
+	bool started;
 
 	struct sdxi_cxt_ctl *cxt_ctl;
 	dma_addr_t cxt_ctl_dma;
 
 	struct ida akey_ida;
+	struct mutex akey_lock;		/* AKey table serialization */
 	struct sdxi_akey_table *akey_table;
 	dma_addr_t akey_table_dma;
 
@@ -51,10 +74,14 @@ struct sdxi_cxt {
 	struct sdxi_cst_pool *cst_pool;
 
 	struct sdxi_ring_state *ring_state;
+	struct sdxi_process *process;
 };
 
 int sdxi_admin_cxt_init(struct sdxi_dev *sdxi);
 struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi);
+struct sdxi_cxt *sdxi_ucxt_new(struct sdxi_dev *sdxi, u32 ring_entries,
+			       u64 ds_ring, u64 write_index, u64 cxt_sts,
+			       u32 pasid, u32 opb_000_enb);
 /*
  * Caller must exclude users and stop the client before release. An update or
  * synchronization failure retains the invalidated context for device teardown;
@@ -62,6 +89,18 @@ struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi);
  */
 int sdxi_cxt_exit(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);
+struct sdxi_cxt *sdxi_cxt_get_by_id(struct sdxi_dev *sdxi, u16 cxt_id);
+int sdxi_cxt_quiesce(struct sdxi_cxt *cxt, unsigned int timeout_ms,
+		     u32 *cst_flags);
+bool sdxi_cxt_is_user(const struct sdxi_cxt *cxt);
+bool sdxi_cxt_is_fatal(const struct sdxi_cxt *cxt);
+void sdxi_cxt_kill(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);
+void sdxi_cxt_vma_close(struct sdxi_cxt *cxt);
 
 enum sdxi_submit_flags {
 	SDXI_CXT_SUBMIT_KICK  = BIT(0),
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index 4b54b14fefc1..e760c8a2603a 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -279,6 +279,16 @@ void sdxi_dev_force_quiesce(struct sdxi_dev *sdxi)
 		sdxi->bus_ops->force_quiesce(sdxi);
 }
 
+void sdxi_dev_fail(struct sdxi_dev *sdxi)
+{
+	mutex_lock(&sdxi->admin_lock);
+	if (!sdxi->admin_dead) {
+		sdxi->admin_dead = true;
+		sdxi_dev_force_quiesce(sdxi);
+	}
+	mutex_unlock(&sdxi->admin_lock);
+}
+
 /* Stop the function and discard inherited MMIO configuration. */
 static int sdxi_fn_prepare(struct sdxi_dev *sdxi)
 {
diff --git a/drivers/dma/sdxi/process.c b/drivers/dma/sdxi/process.c
new file mode 100644
index 000000000000..bc0552b554ba
--- /dev/null
+++ b/drivers/dma/sdxi/process.c
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI userspace process and SVA management.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/iommu.h>
+#include <linux/sched/mm.h>
+#include <linux/sched/task.h>
+#include <linux/slab.h>
+
+#include "context.h"
+#include "process.h"
+#include "sdxi.h"
+
+static void sdxi_process_cleanup(struct sdxi_process *process)
+{
+	struct sdxi_cxt *cxt;
+	int ret;
+
+	mutex_lock(&process->mutex);
+	cxt = process->cxt;
+	if (cxt)
+		sdxi_cxt_get(cxt);
+	mutex_unlock(&process->mutex);
+
+	if (!cxt)
+		return;
+
+	ret = sdxi_cxt_notify_mm_release(cxt);
+	if (ret)
+		dev_err(process->sdxi->dev,
+			"failed to quiesce context %u during mm release: %d\n",
+			cxt->id, ret);
+	sdxi_cxt_put(cxt);
+}
+
+static void sdxi_mmu_notifier_release(struct mmu_notifier *notifier,
+				      struct mm_struct *mm)
+{
+	struct sdxi_process *process;
+
+	process = container_of(notifier, struct sdxi_process, notifier);
+	sdxi_process_cleanup(process);
+}
+
+static const struct mmu_notifier_ops sdxi_mmu_notifier_ops = {
+	.release = sdxi_mmu_notifier_release,
+};
+
+static int sdxi_process_register_notifier(struct sdxi_process *process)
+{
+	int ret;
+
+	if (process->notifier_registered)
+		return 0;
+
+	ret = mmu_notifier_register(&process->notifier, process->mm);
+	if (ret)
+		return ret;
+
+	process->notifier_registered = true;
+	return 0;
+}
+
+struct iommu_sva *
+sdxi_process_detach_sva_locked(struct sdxi_process *process, u32 *pasid)
+{
+	struct iommu_sva *sva;
+
+	lockdep_assert_held(&process->mutex);
+
+	sva = process->sva;
+	if (!sva) {
+		if (pasid)
+			*pasid = 0;
+		return NULL;
+	}
+
+	if (pasid)
+		*pasid = process->pasid;
+	process->sva = NULL;
+	process->pasid = 0;
+
+	return sva;
+}
+
+void sdxi_process_unbind_sva(struct sdxi_process *process,
+			     struct iommu_sva *sva, u32 pasid)
+{
+	if (process->notifier_registered) {
+		process->notifier_registered = false;
+		mmu_notifier_unregister(&process->notifier, process->mm);
+	}
+
+	if (sva)
+		iommu_sva_unbind_device(sva);
+}
+
+static void sdxi_process_free(struct sdxi_process *process)
+{
+	struct iommu_sva *sva;
+	u32 pasid;
+
+	mutex_lock(&process->mutex);
+	sva = sdxi_process_detach_sva_locked(process, &pasid);
+	mutex_unlock(&process->mutex);
+
+	sdxi_process_unbind_sva(process, sva, pasid);
+	put_task_struct(process->lead_thread);
+	mmdrop(process->mm);
+	kfree(process);
+}
+
+void sdxi_process_get(struct sdxi_process *process)
+{
+	refcount_inc(&process->refs);
+}
+
+void sdxi_process_put(struct sdxi_process *process)
+{
+	if (process && refcount_dec_and_test(&process->refs))
+		sdxi_process_free(process);
+}
+
+int sdxi_process_bind(struct sdxi_process *process)
+{
+	struct iommu_sva *sva;
+	u32 pasid;
+	int ret;
+
+	if (!process->sdxi)
+		return -ENODEV;
+
+	sva = iommu_sva_bind_device(process->sdxi->dev, process->mm);
+	if (IS_ERR(sva))
+		return PTR_ERR(sva);
+
+	pasid = iommu_sva_get_pasid(sva);
+	if (pasid == IOMMU_PASID_INVALID) {
+		ret = -EINVAL;
+		goto unbind;
+	}
+
+	/*
+	 * Register after the IOMMU notifier so SDXI quiesces the context
+	 * before SVA translations disappear during exit_mmap().
+	 */
+	ret = sdxi_process_register_notifier(process);
+	if (ret)
+		goto unbind;
+
+	process->pasid = pasid;
+	process->sva = sva;
+	return 0;
+
+unbind:
+	iommu_sva_unbind_device(sva);
+	return ret;
+}
+
+struct sdxi_process *sdxi_process_create(struct sdxi_dev *sdxi)
+{
+	struct task_struct *task = current;
+	struct sdxi_process *process;
+
+	if (!task->mm)
+		return ERR_PTR(-EINVAL);
+
+	process = kzalloc_obj(*process, GFP_KERNEL);
+	if (!process)
+		return ERR_PTR(-ENOMEM);
+
+	refcount_set(&process->refs, 1);
+	mutex_init(&process->mutex);
+	process->lead_thread = task->group_leader;
+	get_task_struct(process->lead_thread);
+	process->mm = task->mm;
+	mmgrab(process->mm);
+	process->sdxi = sdxi;
+	process->notifier.ops = &sdxi_mmu_notifier_ops;
+	INIT_HLIST_NODE(&process->notifier.hlist);
+
+	return process;
+}
diff --git a/drivers/dma/sdxi/process.h b/drivers/dma/sdxi/process.h
new file mode 100644
index 000000000000..cb48cc3c20c9
--- /dev/null
+++ b/drivers/dma/sdxi/process.h
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI userspace process state.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_PROCESS_H
+#define DMA_SDXI_PROCESS_H
+
+#include <linux/mmu_notifier.h>
+#include <linux/mutex.h>
+#include <linux/refcount.h>
+#include <linux/types.h>
+
+struct iommu_sva;
+struct sdxi_cxt;
+struct sdxi_dev;
+
+struct sdxi_process {
+	refcount_t refs;
+	struct mutex mutex;		/* context and SVA serialization */
+	struct task_struct *lead_thread;
+	struct mm_struct *mm;
+	struct mmu_notifier notifier;
+	bool notifier_registered;
+
+	struct sdxi_dev *sdxi;
+	struct sdxi_cxt *cxt;
+	struct iommu_sva *sva;
+	u32 pasid;
+};
+
+struct sdxi_process *sdxi_process_create(struct sdxi_dev *sdxi);
+void sdxi_process_get(struct sdxi_process *process);
+void sdxi_process_put(struct sdxi_process *process);
+int sdxi_process_bind(struct sdxi_process *process);
+struct iommu_sva *
+sdxi_process_detach_sva_locked(struct sdxi_process *process, u32 *pasid);
+void sdxi_process_unbind_sva(struct sdxi_process *process,
+			     struct iommu_sva *sva, u32 pasid);
+
+#endif /* DMA_SDXI_PROCESS_H */
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index fa6c3ef1c58f..c000542b5ce9 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -113,6 +113,7 @@ struct sdxi_dev {
 	struct mutex admin_lock;		/* admin submission serialization */
 	bool admin_dead;
 	struct xarray client_cxts; /* context id -> (struct sdxi_cxt *) */
+	u32 mmap_gen;
 
 	bool has_mailbox;
 
@@ -160,6 +161,7 @@ static inline int sdxi_vector_to_irq(struct sdxi_dev *sdxi, unsigned int nr)
 int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops);
 void sdxi_unregister(struct device *dev);
 void sdxi_dev_force_quiesce(struct sdxi_dev *sdxi);
+void sdxi_dev_fail(struct sdxi_dev *sdxi);
 
 static inline u64 sdxi_read64(const struct sdxi_dev *sdxi, enum sdxi_reg reg)
 {
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 12/44] dmaengine: sdxi: route context interrupts to eventfd
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (10 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 11/44] dmaengine: sdxi: support userspace context lifetimes Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 13/44] dmaengine: sdxi: kill user contexts on fatal errors Wei Huang
                   ` (31 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Allocate userspace context interrupts from the existing vector IDA,
route them to eventfds, and reserve a dynamic AKey for interrupt
generation. Synchronize AKey cache updates on bind and unbind, and treat
an armed interrupt without an eventfd as a fatal context condition.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Makefile  |   1 +
 drivers/dma/sdxi/context.c |   6 +
 drivers/dma/sdxi/context.h |  11 ++
 drivers/dma/sdxi/intr.c    | 320 +++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/intr.h    |  21 +++
 5 files changed, 359 insertions(+)
 create mode 100644 drivers/dma/sdxi/intr.c
 create mode 100644 drivers/dma/sdxi/intr.h

diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index f20a7c39dfc9..a915982e76a7 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -8,6 +8,7 @@ sdxi-core-y := \
 	device.o      \
 	dma.o         \
 	error.o       \
+	intr.o        \
 	keys.o        \
 	process.o     \
 	ring.o
diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index fe7996a024d6..f5e31f9f9131 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -29,6 +29,7 @@
 #include "context.h"
 #include "descriptor.h"
 #include "hw.h"
+#include "intr.h"
 #include "process.h"
 #include "ring.h"
 #include "sdxi.h"
@@ -91,8 +92,11 @@ static struct sdxi_cxt *sdxi_alloc_cxt(struct sdxi_dev *sdxi)
 	refcount_set(&cxt->refs, 1);
 	mutex_init(&cxt->cleanup_lock);
 	mutex_init(&cxt->akey_lock);
+	spin_lock_init(&cxt->intr_lock);
 	init_waitqueue_head(&cxt->poll_wq);
 	INIT_DELAYED_WORK(&cxt->cleanup_work, sdxi_cxt_cleanup_workfn);
+	cxt->intr_vector = -1;
+	cxt->intr_irq = -1;
 	ida_init(&cxt->akey_ida);
 	cxt->pasid = IOMMU_NO_PASID;
 	cxt->opb_000_enb = sdxi->op_grp_cap;
@@ -533,6 +537,8 @@ static void sdxi_cxt_release_resources(struct sdxi_cxt *cxt)
 {
 	struct sdxi_dev *sdxi = cxt->sdxi;
 
+	sdxi_cxt_exit_intr(cxt);
+
 	if (cxt->registered) {
 		WARN_ON(xa_erase(&sdxi->client_cxts, cxt->id) != cxt);
 		cxt->registered = false;
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index eed8cef453df..b4a416509243 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -12,6 +12,7 @@
 #include <linux/io.h>
 #include <linux/mutex.h>
 #include <linux/refcount.h>
+#include <linux/spinlock.h>
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/wait.h>
@@ -31,6 +32,9 @@ struct sdxi_akey_table {
 	struct sdxi_akey_ent entry[SZ_4K / sizeof(struct sdxi_akey_ent)];
 };
 
+struct eventfd_ctx;
+struct sdxi_process;
+
 /* For encoding the akey table size in CXT_L1_ENT's akey_sz. */
 static inline u8 akey_table_order(const struct sdxi_akey_table *tbl)
 {
@@ -75,6 +79,13 @@ struct sdxi_cxt {
 
 	struct sdxi_ring_state *ring_state;
 	struct sdxi_process *process;
+
+	spinlock_t intr_lock;		/* eventfd and interrupt state */
+	struct eventfd_ctx *intr_eventfd;
+	int intr_vector;
+	int intr_irq;
+	u16 intr_akey;
+	bool intr_armed;
 };
 
 int sdxi_admin_cxt_init(struct sdxi_dev *sdxi);
diff --git a/drivers/dma/sdxi/intr.c b/drivers/dma/sdxi/intr.c
new file mode 100644
index 000000000000..7544aa0ba347
--- /dev/null
+++ b/drivers/dma/sdxi/intr.c
@@ -0,0 +1,320 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI userspace context interrupt routing.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/dma-mapping.h>
+#include <linux/eventfd.h>
+#include <linux/interrupt.h>
+#include <linux/string.h>
+
+#include "admin.h"
+#include "context.h"
+#include "hw.h"
+#include "intr.h"
+#include "sdxi.h"
+
+#define SDXI_UCXT_DEFAULT_AKEY		1
+#define SDXI_INTR_UPDATE_TIMEOUT_MS	5000U
+
+static int sdxi_cxt_alloc_intr_akey(struct sdxi_cxt *cxt, u16 *akey)
+{
+	u32 index;
+
+	for (index = SDXI_UCXT_DEFAULT_AKEY + 1;
+	     index < ARRAY_SIZE(cxt->akey_table->entry); index++) {
+		u16 intr = le16_to_cpu(cxt->akey_table->entry[index].intr_num);
+
+		if (!(intr & SDXI_AKEY_ENT_VL)) {
+			*akey = index;
+			return 0;
+		}
+	}
+
+	return -ENOSPC;
+}
+
+static void sdxi_cxt_clear_intr_akey(struct sdxi_cxt *cxt, u16 akey)
+{
+	struct sdxi_akey_ent *entry;
+
+	if (!cxt->akey_table || !akey ||
+	    akey >= ARRAY_SIZE(cxt->akey_table->entry))
+		return;
+
+	entry = &cxt->akey_table->entry[akey];
+	WRITE_ONCE(entry->intr_num, 0);
+	dma_wmb();
+	memset(entry, 0, sizeof(*entry));
+	dma_wmb();
+}
+
+static void sdxi_cxt_arm_intr_akey(struct sdxi_cxt *cxt, u16 akey)
+{
+	struct sdxi_akey_ent *entry = &cxt->akey_table->entry[akey];
+	u32 pasid = FIELD_PREP(SDXI_AKEY_ENT_PASID, cxt->pasid);
+	u16 intr;
+
+	intr = SDXI_AKEY_ENT_VL |
+	       SDXI_AKEY_ENT_IV |
+	       FIELD_PREP(SDXI_AKEY_ENT_PV, cxt->pasid_valid) |
+	       FIELD_PREP(SDXI_AKEY_ENT_INTR_NUM, cxt->intr_vector);
+
+	memset(entry, 0, sizeof(*entry));
+	entry->pasid = cpu_to_le32(pasid);
+	dma_wmb();
+	WRITE_ONCE(entry->intr_num, cpu_to_le16(intr));
+}
+
+static irqreturn_t sdxi_cxt_irq_handler(int irq, void *data)
+{
+	struct sdxi_cxt *cxt = data;
+	struct eventfd_ctx *eventfd;
+	unsigned long flags;
+	bool armed;
+
+	spin_lock_irqsave(&cxt->intr_lock, flags);
+	eventfd = cxt->intr_eventfd;
+	armed = cxt->intr_armed;
+	if (eventfd)
+		eventfd_signal(eventfd);
+	spin_unlock_irqrestore(&cxt->intr_lock, flags);
+
+	if (!eventfd && armed) {
+		dev_err(cxt->sdxi->dev,
+			"context %u interrupt without eventfd\n", cxt->id);
+		sdxi_cxt_kill(cxt);
+	}
+
+	return IRQ_HANDLED;
+}
+
+bool sdxi_cxt_has_intr_fd(const struct sdxi_cxt *cxt)
+{
+	return cxt && READ_ONCE(cxt->intr_eventfd);
+}
+
+int sdxi_cxt_init_intr(struct sdxi_cxt *cxt)
+{
+	int vector;
+	int irq;
+	int ret;
+
+	if (!cxt || !cxt->akey_table || !cxt->id)
+		return -EINVAL;
+	if (cxt->intr_vector >= 0)
+		return 0;
+
+	vector = sdxi_alloc_vector(cxt->sdxi);
+	if (vector < 0)
+		return vector;
+	if (!FIELD_FIT(SDXI_AKEY_ENT_INTR_NUM, vector)) {
+		ret = -ENOSPC;
+		goto free_vector;
+	}
+
+	irq = sdxi_vector_to_irq(cxt->sdxi, vector);
+	if (irq < 0) {
+		ret = irq;
+		goto free_vector;
+	}
+
+	ret = request_irq(irq, sdxi_cxt_irq_handler, IRQF_TRIGGER_NONE,
+			  "sdxi-context", cxt);
+	if (ret)
+		goto free_vector;
+
+	cxt->intr_vector = vector;
+	cxt->intr_irq = irq;
+	return 0;
+
+free_vector:
+	sdxi_free_vector(cxt->sdxi, vector);
+	return ret;
+}
+
+static int sdxi_cxt_clear_intr_fd(struct sdxi_cxt *cxt)
+{
+	struct eventfd_ctx *eventfd;
+	unsigned long flags;
+	u16 akey;
+	int ret;
+
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table || cxt->intr_vector < 0 || cxt->intr_irq < 0) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+
+	spin_lock_irqsave(&cxt->intr_lock, flags);
+	akey = cxt->intr_akey;
+	eventfd = cxt->intr_eventfd;
+	spin_unlock_irqrestore(&cxt->intr_lock, flags);
+
+	if (!akey) {
+		ret = 0;
+		goto out_unlock;
+	}
+
+	sdxi_cxt_clear_intr_akey(cxt, akey);
+	ret = sdxi_adm_akey_update_sync(cxt->sdxi, cxt->id, cxt->id,
+					akey, akey,
+					SDXI_INTR_UPDATE_TIMEOUT_MS, NULL);
+	if (ret) {
+		sdxi_cxt_arm_intr_akey(cxt, akey);
+		(void)sdxi_adm_akey_update_sync(cxt->sdxi, cxt->id, cxt->id,
+						akey, akey,
+						SDXI_INTR_UPDATE_TIMEOUT_MS,
+						NULL);
+		goto out_unlock;
+	}
+
+	spin_lock_irqsave(&cxt->intr_lock, flags);
+	cxt->intr_armed = false;
+	cxt->intr_akey = 0;
+	cxt->intr_eventfd = NULL;
+	spin_unlock_irqrestore(&cxt->intr_lock, flags);
+
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	if (!ret && eventfd)
+		eventfd_ctx_put(eventfd);
+	return ret;
+}
+
+void sdxi_cxt_exit_intr(struct sdxi_cxt *cxt)
+{
+	struct eventfd_ctx *eventfd;
+	unsigned long flags;
+	int vector;
+	int irq;
+	u16 akey;
+
+	if (!cxt)
+		return;
+
+	mutex_lock(&cxt->akey_lock);
+	spin_lock_irqsave(&cxt->intr_lock, flags);
+	akey = cxt->intr_akey;
+	eventfd = cxt->intr_eventfd;
+	cxt->intr_armed = false;
+	cxt->intr_akey = 0;
+	cxt->intr_eventfd = NULL;
+	spin_unlock_irqrestore(&cxt->intr_lock, flags);
+	sdxi_cxt_clear_intr_akey(cxt, akey);
+	irq = cxt->intr_irq;
+	vector = cxt->intr_vector;
+	cxt->intr_irq = -1;
+	cxt->intr_vector = -1;
+	mutex_unlock(&cxt->akey_lock);
+
+	if (eventfd)
+		eventfd_ctx_put(eventfd);
+	if (irq >= 0)
+		free_irq(irq, cxt);
+	if (vector >= 0)
+		sdxi_free_vector(cxt->sdxi, vector);
+}
+
+int sdxi_cxt_set_intr_fd(struct sdxi_cxt *cxt, int eventfd_fd,
+			 u16 *akey, u32 *intr_num)
+{
+	struct eventfd_ctx *old_eventfd;
+	struct eventfd_ctx *eventfd;
+	unsigned long flags;
+	u16 old_akey;
+	u16 intr_akey;
+	bool old_armed;
+	bool new_akey = false;
+	bool published = false;
+	int ret;
+
+	if (!cxt || !cxt->akey_table)
+		return -ENODEV;
+	if (sdxi_cxt_is_fatal(cxt))
+		return -ENODEV;
+	if (cxt->intr_vector < 0 || cxt->intr_irq < 0)
+		return -EOPNOTSUPP;
+
+	if (eventfd_fd < 0) {
+		ret = sdxi_cxt_clear_intr_fd(cxt);
+		if (!ret) {
+			if (akey)
+				*akey = 0;
+			if (intr_num)
+				*intr_num = 0;
+		}
+		return ret;
+	}
+
+	eventfd = eventfd_ctx_fdget(eventfd_fd);
+	if (IS_ERR(eventfd))
+		return PTR_ERR(eventfd);
+
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table || cxt->intr_vector < 0 || cxt->intr_irq < 0 ||
+	    sdxi_cxt_is_fatal(cxt)) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+
+	spin_lock_irqsave(&cxt->intr_lock, flags);
+	old_akey = cxt->intr_akey;
+	intr_akey = old_akey;
+	old_eventfd = cxt->intr_eventfd;
+	old_armed = cxt->intr_armed;
+	spin_unlock_irqrestore(&cxt->intr_lock, flags);
+
+	if (!intr_akey) {
+		ret = sdxi_cxt_alloc_intr_akey(cxt, &intr_akey);
+		if (ret)
+			goto out_unlock;
+		new_akey = true;
+	}
+
+	spin_lock_irqsave(&cxt->intr_lock, flags);
+	cxt->intr_eventfd = eventfd;
+	cxt->intr_akey = intr_akey;
+	cxt->intr_armed = true;
+	spin_unlock_irqrestore(&cxt->intr_lock, flags);
+	published = true;
+
+	sdxi_cxt_arm_intr_akey(cxt, intr_akey);
+	ret = sdxi_adm_akey_update_sync(cxt->sdxi, cxt->id, cxt->id,
+					intr_akey, intr_akey,
+					SDXI_INTR_UPDATE_TIMEOUT_MS, NULL);
+	if (ret) {
+		if (new_akey) {
+			sdxi_cxt_clear_intr_akey(cxt, intr_akey);
+			(void)sdxi_adm_akey_update_sync(cxt->sdxi,
+							cxt->id, cxt->id,
+							intr_akey, intr_akey,
+							SDXI_INTR_UPDATE_TIMEOUT_MS,
+							NULL);
+		}
+		spin_lock_irqsave(&cxt->intr_lock, flags);
+		cxt->intr_eventfd = old_eventfd;
+		cxt->intr_akey = old_akey;
+		cxt->intr_armed = old_armed;
+		spin_unlock_irqrestore(&cxt->intr_lock, flags);
+		goto out_unlock;
+	}
+	mutex_unlock(&cxt->akey_lock);
+
+	if (old_eventfd)
+		eventfd_ctx_put(old_eventfd);
+	if (akey)
+		*akey = intr_akey;
+	if (intr_num)
+		*intr_num = cxt->intr_vector;
+	return 0;
+
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	if (!published || ret)
+		eventfd_ctx_put(eventfd);
+	return ret;
+}
diff --git a/drivers/dma/sdxi/intr.h b/drivers/dma/sdxi/intr.h
new file mode 100644
index 000000000000..07e99fb772ea
--- /dev/null
+++ b/drivers/dma/sdxi/intr.h
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI userspace context interrupt routing.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_INTR_H
+#define DMA_SDXI_INTR_H
+
+#include <linux/types.h>
+
+struct sdxi_cxt;
+
+int sdxi_cxt_init_intr(struct sdxi_cxt *cxt);
+void sdxi_cxt_exit_intr(struct sdxi_cxt *cxt);
+int sdxi_cxt_set_intr_fd(struct sdxi_cxt *cxt, int eventfd_fd,
+			 u16 *akey, u32 *intr_num);
+bool sdxi_cxt_has_intr_fd(const struct sdxi_cxt *cxt);
+
+#endif /* DMA_SDXI_INTR_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 13/44] dmaengine: sdxi: kill user contexts on fatal errors
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (11 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 12/44] dmaengine: sdxi: route context interrupts to eventfd Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 14/44] dmaengine: sdxi: define the ioctl UAPI surface Wei Huang
                   ` (30 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Use context references from current xarray to translate error-log
stop reactions into fatal userspace context cleanup. Kernel and
administrative contexts remain under their existing owners.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/context.c | 21 +++++++++++
 drivers/dma/sdxi/context.h |  1 +
 drivers/dma/sdxi/device.c  |  5 +++
 drivers/dma/sdxi/error.c   | 71 ++++++++++++++++++++++++++++++++++++--
 drivers/dma/sdxi/sdxi.h    |  1 +
 5 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index f5e31f9f9131..2c3b6f5df4b5 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -1119,6 +1119,27 @@ void sdxi_cxt_kill(struct sdxi_cxt *cxt)
 	wake_up_poll(&cxt->poll_wq, EPOLLERR);
 }
 
+void sdxi_cxt_kill_stopped(struct sdxi_cxt *cxt)
+{
+	bool newly_fatal;
+
+	if (!cxt)
+		return;
+
+	mutex_lock(&cxt->cleanup_lock);
+	newly_fatal = !cxt->fatal;
+	cxt->fatal = true;
+	cxt->started = false;
+	cxt->quiesced = true;
+	cxt->quiesce_cst_flags = 0;
+	if (sdxi_cxt_is_user(cxt))
+		sdxi_cxt_queue_cleanup_locked(cxt, 0);
+	mutex_unlock(&cxt->cleanup_lock);
+
+	if (newly_fatal)
+		wake_up_poll(&cxt->poll_wq, EPOLLERR);
+}
+
 void sdxi_cxt_release_user_handle(struct sdxi_cxt *cxt)
 {
 	if (!cxt)
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index b4a416509243..bbe83e54b827 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -108,6 +108,7 @@ int sdxi_cxt_quiesce(struct sdxi_cxt *cxt, unsigned int timeout_ms,
 bool sdxi_cxt_is_user(const struct sdxi_cxt *cxt);
 bool sdxi_cxt_is_fatal(const struct sdxi_cxt *cxt);
 void sdxi_cxt_kill(struct sdxi_cxt *cxt);
+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);
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index e760c8a2603a..1587795c0a16 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -96,6 +96,11 @@ static enum sdxi_fn_gsv sdxi_dev_gsv(const struct sdxi_dev *sdxi)
 	return gsv;
 }
 
+bool sdxi_dev_in_error(const struct sdxi_dev *sdxi)
+{
+	return sdxi_dev_gsv(sdxi) == SDXI_GSV_ERROR;
+}
+
 static const unsigned long gsv_poll_interval_us = USEC_PER_MSEC * 100;
 static const unsigned long gsv_transition_timeout_us = USEC_PER_SEC * 1;
 
diff --git a/drivers/dma/sdxi/error.c b/drivers/dma/sdxi/error.c
index 561e88d81f41..4d8607b3bbec 100644
--- a/drivers/dma/sdxi/error.c
+++ b/drivers/dma/sdxi/error.c
@@ -14,6 +14,7 @@
 #include <linux/sizes.h>
 #include <linux/string.h>
 
+#include "context.h"
 #include "error.h"
 #include "hw.h"
 #include "mmio.h"
@@ -61,6 +62,11 @@ enum {
 	SDXI_PACKING_QUIRKS = QUIRK_LITTLE_ENDIAN | QUIRK_LSW32_IS_FIRST,
 };
 
+enum sdxi_errlog_reaction {
+	SDXI_ERRLOG_REACTION_CXT_STOP = 1,
+	SDXI_ERRLOG_REACTION_FN_STOP = 2,
+};
+
 static const char *const sdxi_err_steps[] = {
 	[1] = "internal",
 	[2] = "context L2",
@@ -124,6 +130,44 @@ static void sdxi_errlog_report(struct sdxi_dev *sdxi, u64 read_index,
 		dev_err(sdxi->dev, "  buffer=%u\n", entry->buf);
 }
 
+static bool sdxi_errlog_handle_reaction(struct sdxi_dev *sdxi,
+					const struct sdxi_errlog_entry *entry)
+{
+	struct sdxi_cxt *cxt;
+
+	if (entry->reaction == SDXI_ERRLOG_REACTION_FN_STOP)
+		return true;
+	if (entry->reaction != SDXI_ERRLOG_REACTION_CXT_STOP ||
+	    !entry->cv || !entry->cxt_num)
+		return false;
+
+	cxt = sdxi_cxt_get_by_id(sdxi, entry->cxt_num);
+	if (!cxt)
+		return false;
+
+	if (sdxi_cxt_is_user(cxt))
+		sdxi_cxt_kill_stopped(cxt);
+	sdxi_cxt_put(cxt);
+	return false;
+}
+
+static void sdxi_error_handle_function(struct sdxi_dev *sdxi)
+{
+	struct sdxi_cxt *cxt;
+	u32 cxt_id;
+
+	dev_err(sdxi->dev, "function stopped after hardware error\n");
+	sdxi_dev_fail(sdxi);
+
+	for (cxt_id = 1; cxt_id <= sdxi->max_cxtid; cxt_id++) {
+		cxt = sdxi_cxt_get_by_id(sdxi, cxt_id);
+		if (!cxt)
+			continue;
+		sdxi_cxt_kill_stopped(cxt);
+		sdxi_cxt_put(cxt);
+	}
+}
+
 static irqreturn_t sdxi_error_irq_thread(int irq, void *data)
 {
 	struct sdxi_dev *sdxi = data;
@@ -134,10 +178,16 @@ static irqreturn_t sdxi_error_irq_thread(int irq, void *data)
 	u64 read_index;
 	u64 status;
 	u64 count;
+	bool function_stopped;
 
 	status = sdxi_read64(sdxi, SDXI_MMIO_ERR_STS);
-	if (!(status & ack_mask))
-		return IRQ_NONE;
+	function_stopped = sdxi_dev_in_error(sdxi);
+	if (!(status & ack_mask)) {
+		if (!function_stopped)
+			return IRQ_NONE;
+		sdxi_error_handle_function(sdxi);
+		return IRQ_HANDLED;
+	}
 
 	if (status & SDXI_MMIO_ERR_STS_ERR_BIT)
 		dev_err(sdxi->dev, "hardware could not write the error log\n");
@@ -145,6 +195,8 @@ static irqreturn_t sdxi_error_irq_thread(int irq, void *data)
 		dev_err(sdxi->dev, "error log overflowed\n");
 	if (!(status & SDXI_MMIO_ERR_STS_STS_BIT)) {
 		sdxi_write64(sdxi, SDXI_MMIO_ERR_STS, status & ack_mask);
+		if (function_stopped)
+			sdxi_error_handle_function(sdxi);
 		return IRQ_HANDLED;
 	}
 
@@ -170,12 +222,17 @@ static irqreturn_t sdxi_error_irq_thread(int irq, void *data)
 		size_t index = read_index % SDXI_ERRLOG_ENTRIES;
 
 		memcpy(&raw, &sdxi->err_log[index], sizeof(raw));
-		if (sdxi_errlog_decode(sdxi, &raw, &entry))
+		if (sdxi_errlog_decode(sdxi, &raw, &entry)) {
 			sdxi_errlog_report(sdxi, read_index, &entry);
+			function_stopped |=
+				sdxi_errlog_handle_reaction(sdxi, &entry);
+		}
 		read_index++;
 	}
 
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_RD, read_index);
+	if (function_stopped)
+		sdxi_error_handle_function(sdxi);
 	return IRQ_HANDLED;
 }
 
@@ -214,6 +271,10 @@ int sdxi_error_init(struct sdxi_dev *sdxi)
 	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 1);
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
 
+	reg = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
+	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_ERR_INTR_EN, &reg, 1);
+	sdxi_write64(sdxi, SDXI_MMIO_CTL0, reg);
+
 	size_order = ilog2(SDXI_ERRLOG_SIZE) - ilog2(SZ_4K);
 	reg = FIELD_PREP(SDXI_MMIO_ERR_CFG_PTR,
 			 sdxi->err_log_dma >> ilog2(SZ_4K)) |
@@ -237,6 +298,10 @@ void sdxi_error_exit(struct sdxi_dev *sdxi)
 	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 0);
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
 
+	reg = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
+	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_ERR_INTR_EN, &reg, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_CTL0, reg);
+
 	if (sdxi->error_irq >= 0)
 		devm_free_irq(sdxi->dev, sdxi->error_irq, sdxi);
 
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index c000542b5ce9..fa34aaa36c16 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -160,6 +160,7 @@ static inline int sdxi_vector_to_irq(struct sdxi_dev *sdxi, unsigned int nr)
 
 int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops);
 void sdxi_unregister(struct device *dev);
+bool sdxi_dev_in_error(const struct sdxi_dev *sdxi);
 void sdxi_dev_force_quiesce(struct sdxi_dev *sdxi);
 void sdxi_dev_fail(struct sdxi_dev *sdxi);
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 14/44] dmaengine: sdxi: define the ioctl UAPI surface
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (12 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 13/44] dmaengine: sdxi: kill user contexts on fatal errors Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 15/44] dmaengine: sdxi: add internal ioctl definitions Wei Huang
                   ` (29 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add the SDXI ioctl UAPI header. It covers device attribute detection,
context creation, AKey/RKey management, and descriptor group discovery,
etc. The ABI uses argsz and reserved fields so future extensions can
be added without changing ioctl numbers.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 include/uapi/linux/sdxi.h | 569 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 569 insertions(+)
 create mode 100644 include/uapi/linux/sdxi.h

diff --git a/include/uapi/linux/sdxi.h b/include/uapi/linux/sdxi.h
new file mode 100644
index 000000000000..b6d214f79a99
--- /dev/null
+++ b/include/uapi/linux/sdxi.h
@@ -0,0 +1,569 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+#ifndef __LINUX_SDXI_H
+#define __LINUX_SDXI_H
+
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+/*
+ * SDXI userspace ABI.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ *
+ * All ioctls below use a struct whose first field is @argsz, the size of
+ * the struct in bytes. The kernel rejects @argsz smaller than the minimum
+ * it requires with -EINVAL, and uses @argsz to determine the layout
+ * version supplied by userspace. Fields not understood by an older kernel
+ * are ignored on input and zeroed on output, allowing additive UAPI
+ * extensions under a single ioctl number; incompatible changes bump
+ * SDXI_UAPI_MAJOR.
+ *
+ * Field direction is annotated with one of:
+ *   IN     - userspace -> kernel.
+ *   OUT    - kernel -> userspace.
+ *   IN/OUT - both directions in the same field.
+ * For fields whose direction depends on flags or ioctl, the per-call
+ * directions are called out in the kernel-doc block.
+ *
+ * Reserved fields must be zero on input; the kernel zeroes them on output.
+ * The @flags field of every struct must be zero unless documented bits are
+ * set; unknown bits return -EINVAL.
+ */
+
+/* UAPI version and capability discovery. */
+#define SDXI_UAPI_MAJOR		1
+#define SDXI_UAPI_MINOR		0
+
+#define SDXI_UAPI_CAP_WORDS	4U
+
+#define SDXI_UAPI_CAP_DEV_INFO		(1ULL << 0)
+#define SDXI_UAPI_CAP_DEV_ATTR_MGMT	(1ULL << 1)
+#define SDXI_UAPI_CAP_RKEY_MGMT		(1ULL << 2)
+#define SDXI_UAPI_CAP_CXT_CREATE	(1ULL << 3)
+#define SDXI_UAPI_CAP_CXT_EVENTFD	(1ULL << 4)
+#define SDXI_UAPI_CAP_AKEY_MGMT		(1ULL << 5)
+#define SDXI_UAPI_CAP_ADMIN_DISC	(1ULL << 6)
+#define SDXI_UAPI_CAP_ADMIN_VENDOR	(1ULL << 7)
+
+/**
+ * struct sdxi_uapi_info_args - argument for SDXI_GET_UAPI_INFO.
+ * @argsz:      IN/OUT: size of this struct in bytes.
+ * @flags:      IN: must be zero.
+ * @uapi_major: OUT: UAPI generation.
+ * @uapi_minor: OUT: UAPI revision.
+ * @cap_words:  OUT: number of valid 64-bit words in @caps.
+ * @rsvd0:      reserved, must be zero.
+ * @caps:       OUT: bitmap of SDXI_UAPI_CAP_* features advertised by the
+ *              kernel. @cap_words gives the number of words populated;
+ *              trailing words are zero.
+ */
+struct sdxi_uapi_info_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u16 uapi_major;			/* out */
+	__u16 uapi_minor;			/* out */
+	__u16 cap_words;			/* out */
+	__u16 rsvd0;				/* reserved */
+
+	__aligned_u64 caps[SDXI_UAPI_CAP_WORDS];/* out */
+};
+
+/***************/
+/* DEVICE INFO */
+/***************/
+/**
+ * struct sdxi_dev_info_args - argument for SDXI_GET_DEV_INFO.
+ * @argsz:    IN/OUT: size of this struct in bytes.
+ * @flags:    IN: must be zero.
+ * @hw_major: OUT: MMIO_VERSION.major.
+ * @hw_minor: OUT: MMIO_VERSION.minor.
+ * @rsvd0:    reserved, must be zero.
+ * @cap0:     OUT: raw MMIO_CAP0.
+ * @cap1:     OUT: raw MMIO_CAP1.
+ * @sts0:     OUT: raw MMIO_STS0, read upon ioctl entry.
+ * @rsvd1:    reserved for future fields, must be zero.
+ */
+struct sdxi_dev_info_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u16 hw_major;				/* out */
+	__u16 hw_minor;				/* out */
+	__u32 rsvd0;				/* reserved */
+
+	__aligned_u64 cap0;			/* out */
+	__aligned_u64 cap1;			/* out */
+	__aligned_u64 sts0;			/* out */
+
+	__aligned_u64 rsvd1[4];			/* reserved */
+};
+
+/*********************/
+/* DEVICE ATTRIBUTES */
+/*********************/
+/* Device attribute selectors used with SDXI_{GET,SET}_DEV_ATTR. */
+enum sdxi_dev_attr {
+	SDXI_DEV_ATTR_GSR         = 1,	/* MMIO_CTL0.fn_gsr */
+	SDXI_DEV_ATTR_GRP_ID      = 2,	/* MMIO_CTL0.fn_grp_id */
+	SDXI_DEV_ATTR_MAX_BUFFER  = 3,	/* MMIO_CTL2.max_buffer */
+	SDXI_DEV_ATTR_MAX_AKEY_SZ = 4,	/* MMIO_CTL2.max_akey_sz */
+	SDXI_DEV_ATTR_MAX_RKEY_SZ = 5,	/* MMIO_RKEY.max_rkey_sz */
+	SDXI_DEV_ATTR_MAX_CXT     = 6,	/* MMIO_CTL2.max_cxt */
+};
+
+/**
+ * struct sdxi_dev_attr_args - argument for SDXI_{GET,SET}_DEV_ATTR.
+ * @argsz: IN/OUT: size of this struct in bytes.
+ * @flags: IN: must be zero. Unknown bits return -EINVAL.
+ * @attr:  IN: attribute selector, see enum sdxi_dev_attr. Unknown values
+ *         return -EOPNOTSUPP.
+ * @val:   GET: OUT: current value.
+ *         SET: IN: requested value; OUT: value actually applied.
+ * @rsvd0: reserved, must be zero.
+ */
+struct sdxi_dev_attr_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__aligned_u64 attr;			/* in */
+	__aligned_u64 val;			/* in/out: see ioctl */
+	__aligned_u64 rsvd0[4];			/* reserved */
+};
+
+/******************/
+/* KEY MANAGEMENT */
+/******************/
+/*
+ * Key tables (RKey and AKey).
+ *
+ * RKey and AKey tables share the same wire format below; the table being
+ * addressed is selected by the fd and the ioctl number, not by any field
+ * in the struct:
+ *
+ *   RKey (per-device, CAP_SYS_ADMIN), device fd:
+ *     SDXI_GET_RKEY_LIST, SDXI_GET_RKEYS, SDXI_SET_RKEYS.
+ *   AKey (per-context, user-accessible), context fd:
+ *     SDXI_GET_AKEY_LIST, SDXI_GET_AKEYS, SDXI_SET_AKEYS.
+ *
+ * Typical usage:
+ *   1) *_LIST returns the table size and a bitmap of valid indices. Pass
+ *      buf_size = 0 to learn the required size, then re-issue with a
+ *      sized buffer.
+ *   2) GET_*KEYS: caller fills entries[i].index for each row of interest;
+ *      the kernel fills entries[i].data[] in place.
+ *   3) SET_*KEYS: caller supplies fully populated entries[]; the kernel
+ *      writes them to the table and reports a commit summary.
+ *
+ * For all key-table structs @argsz covers only the fixed header, not the
+ * trailing flexible array.
+ */
+
+/**
+ * struct sdxi_key_entry - one RKEY_ENT/AKEY_ENT exchanged with userspace.
+ * @index: IN: table index.
+ * @rsvd0: reserved, must be zero.
+ * @data:  raw entry dwords in little-endian wire order, used by GET/SET ioctls.
+ */
+struct sdxi_key_entry {
+	__u32 index;				/* in */
+	__u32 rsvd0;				/* reserved */
+	__u32 data[4];				/* in/out: see ioctl */
+};
+
+/**
+ * struct sdxi_key_list_args - argument for SDXI_GET_{R,A}KEY_LIST.
+ * @argsz:    IN/OUT: size of the fixed header in bytes (excludes @data).
+ * @flags:    IN: must be zero.
+ * @buf_size: IN: bytes available in @data; OUT: bytes required for the
+ *            full bitmap (i.e. ceil(@nentries / 8)).
+ * @nentries: OUT: total number of entries in the table.
+ * @nvalid:   OUT: number of valid entries (popcount of the full bitmap),
+ *            independent of how many bytes fit in @data.
+ * @rsvd0:    reserved, must be zero.
+ * @data:     OUT: valid bitmap. Index N is valid iff bit (N % 8) of
+ *            byte (N / 8) is set.
+ *
+ * If the supplied buffer is too small the ioctl returns -ENOSPC, leaves
+ * @data untouched and updates @buf_size, @nentries and @nvalid so the
+ * caller can resize and retry.
+ */
+struct sdxi_key_list_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u32 buf_size;				/* in/out */
+	__u32 nentries;				/* out */
+	__u32 nvalid;				/* out */
+	__u32 rsvd0;				/* reserved */
+
+	__u8  data[];				/* out */
+};
+
+/**
+ * struct sdxi_get_keys_args - argument for SDXI_GET_{R,A}KEYS.
+ * @argsz:   IN/OUT: size of the fixed header in bytes (excludes @entries).
+ * @flags:   IN: must be zero.
+ * @nkeys:   IN: number of entries requested; OUT: number of entries
+ *           actually populated. Entries beyond the returned @nkeys are
+ *           left untouched.
+ * @rsvd0:   reserved, must be zero.
+ * @entries: IN: caller writes @entries[i].index to select rows.
+ *           OUT: kernel fills @entries[i].data[] in place.
+ */
+struct sdxi_get_keys_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u32 nkeys;				/* in/out */
+	__u32 rsvd0;				/* reserved */
+
+	struct sdxi_key_entry entries[];	/* in/out */
+};
+
+/* Commit outcome reported by SDXI_SET_{R,A}KEYS in @commit_state. */
+enum sdxi_key_commit_state {
+	/* No entry bytes were written. Safe default for pre-zeroed args. */
+	SDXI_KEY_COMMIT_NONE	 = 0,
+	/* All requested entries committed. */
+	SDXI_KEY_COMMIT_ALL	 = 1,
+	/*
+	 * One or more entries were written but the driver could not confirm
+	 * the final hardware-visible state. @first_failed_index names the
+	 * lowest entries[] slot whose commit is unconfirmed; entries below
+	 * it are guaranteed committed.
+	 */
+	SDXI_KEY_COMMIT_PARTIAL	 = 2,
+};
+
+/* @timeout_ms sentinel: 0 selects the driver default. */
+#define SDXI_KEY_TIMEOUT_DEFAULT	0U
+
+/**
+ * struct sdxi_set_keys_args - argument for SDXI_SET_{R,A}KEYS.
+ * @argsz:              IN/OUT: size of the fixed header in bytes
+ *                      (excludes @entries).
+ * @flags:              IN: must be zero.
+ * @timeout_ms:         IN: per-call timeout, see SDXI_KEY_TIMEOUT_DEFAULT.
+ * @nkeys:              IN: number of entries to write.
+ * @cst_flags:          OUT: reserved for SDXI_KEY_CST_F_* completion bits;
+ *                      none defined yet. Kernel writes zero; userspace
+ *                      must ignore unknown bits.
+ * @commit_state:       OUT: enum sdxi_key_commit_state.
+ * @first_failed_index: OUT: valid only when @commit_state ==
+ *                      SDXI_KEY_COMMIT_PARTIAL.
+ * @rsvd0:              reserved, must be zero.
+ * @entries:            IN: entries to commit, written to the table in
+ *                      order. On partial failure, userspace can retry
+ *                      starting from @first_failed_index without
+ *                      re-issuing earlier entries. After updating the
+ *                      software table, the driver implicitly submits
+ *                      the matching DSC_{R,A}KEY_UPD followed by
+ *                      DSC_SYNC so userspace does not need separate
+ *                      admin update/sync ioctls.
+ */
+struct sdxi_set_keys_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u32 timeout_ms;			/* in */
+	__u32 nkeys;				/* in */
+
+	__u32 cst_flags;			/* out */
+	__u32 commit_state;			/* out */
+	__u32 first_failed_index;		/* out */
+	__u32 rsvd0[5];				/* reserved */
+
+	struct sdxi_key_entry entries[];	/* in */
+};
+
+/**********************/
+/* CONTEXT MANAGEMENT */
+/**********************/
+/**
+ * struct sdxi_create_cxt_args - argument for SDXI_CREATE_CXT.
+ * @argsz:              IN/OUT: size of this struct in bytes.
+ * @flags:              IN: must be zero.
+ * @opb_000_enb:        IN/OUT: requested operation-group enable mask for the
+ *                      context, using the raw SDXI opb_000 bit positions used
+ *                      by MMIO_CAP1.opb_000_cap, MMIO_CTL2.opb_000_avl, and
+ *                      CXT_L1_ENT.opb_000_enb. Bit N requests opcode type N
+ *                      for opcode types 0..31. Function-assigned opcode type
+ *                      values are discovered with SDXI_ADMIN_DISC. A value of
+ *                      0 requests the driver default; the kernel returns the
+ *                      applied raw mask.
+ * @pasid:              OUT: PASID bound to the owning process/mm for this
+ *                      context.
+ * @ds_ring_entries:    IN/OUT: requested descriptor-ring entry count. A value
+ *                      of 0 selects the driver default; the kernel returns
+ *                      the actual entry count.
+ * @rsvd0:              reserved, must be zero.
+ * @ds_ring_base:       IN: userspace descriptor-ring base address.
+ * @cxt_sts_base:       IN: userspace context-status base address.
+ * @write_index_base:   IN: userspace write-index base address.
+ * @doorbell_mmap_base: OUT: mmap token for this context's doorbell region.
+ * @cxt_fd:             OUT: context file descriptor returned on success.
+ * @rsvd1:              reserved, must be zero.
+ *
+ * Issued on the device fd to create a new userspace context. On success the
+ * driver has already programmed the context hierarchy, started the context,
+ * and issued an internal context-sync for the new context before returning
+ * the resulting cxt_fd to userspace.
+ */
+struct sdxi_create_cxt_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u32 opb_000_enb;			/* in/out */
+
+	__u32 pasid;				/* out */
+	__u32 ds_ring_entries;			/* in/out */
+	__u32 rsvd0;				/* reserved */
+	__aligned_u64 ds_ring_base;		/* in */
+	__aligned_u64 cxt_sts_base;		/* in */
+	__aligned_u64 write_index_base;		/* in */
+	__aligned_u64 doorbell_mmap_base;	/* out */
+
+	__s32 cxt_fd;				/* out */
+	__u32 rsvd1;				/* reserved */
+};
+
+/**
+ * struct sdxi_cxt_eventfd_args - argument for SDXI_CXT_EVENTFD.
+ * @argsz:     IN/OUT: size of this struct in bytes.
+ * @flags:     IN: must be zero.
+ * @eventfd:   IN: eventfd file descriptor used for context interrupt
+ *             delivery. A negative value disables the current binding.
+ * @intr_akey: OUT: driver-reserved AKey slot used for userspace interrupt
+ *             delivery when a binding is active; zero when disabled.
+ * @rsvd0:     reserved, must be zero.
+ * @intr_num:  OUT: MSI/MSI-X message/vector assigned to this context when a
+ *             binding is active; zero when disabled.
+ * @rsvd1:     reserved, must be zero.
+ *
+ * Issued on a context fd returned by SDXI_CREATE_CXT. Binds or unbinds the
+ * userspace eventfd used for context interrupts (for example DSC_INTR).
+ */
+struct sdxi_cxt_eventfd_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__s32 eventfd;				/* in */
+	__u16 intr_akey;			/* out */
+	__u16 rsvd0;				/* reserved */
+	__u32 intr_num;				/* out */
+
+	__u32 rsvd1[4];				/* reserved */
+};
+
+/* @timeout_ms sentinel: 0 selects the driver default. */
+#define SDXI_CXT_QUIESCE_TIMEOUT_DEFAULT	0U
+
+/**
+ * struct sdxi_quiesce_cxt_args - argument for SDXI_QUIESCE_CXT.
+ * @argsz:      IN/OUT: size of this struct in bytes.
+ * @flags:      IN: must be zero.
+ * @timeout_ms: IN: timeout in milliseconds, or
+ *              SDXI_CXT_QUIESCE_TIMEOUT_DEFAULT (0) for the driver default.
+ * @cst_flags:  OUT: completion status flags returned by the stop-sync path.
+ *
+ * Issued on a context fd returned by SDXI_CREATE_CXT. Synchronously stops
+ * the context so no further DMA traffic is expected from it. The driver
+ * issues the internal stop plus stop-sync sequence before reporting success.
+ * The ioctl does not destroy the context; close(2) drops the userspace
+ * handle and the driver completes final cleanup once the context is
+ * quiesced and no context mappings remain.
+ */
+struct sdxi_quiesce_cxt_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u32 timeout_ms;			/* in */
+	__u32 cst_flags;			/* out */
+};
+
+/******************/
+/* ADMIN DSC_DISC */
+/******************/
+/* @timeout_ms sentinel for admin ioctls: 0 selects the driver default. */
+#define SDXI_ADMIN_TIMEOUT_DEFAULT	0U
+
+/**
+ * struct sdxi_admin_range - inclusive 16-bit range for admin ioctls.
+ * @start: IN: first index in the range.
+ * @end:   IN: last index in the range.
+ */
+struct sdxi_admin_range {
+	__u16 start;				/* in */
+	__u16 end;				/* in */
+};
+
+/**
+ * struct sdxi_admin_disc_args - argument for SDXI_ADMIN_DISC.
+ * @argsz:      IN/OUT: size of this struct in bytes.
+ * @flags:      IN: SDXI_ADMIN_DISC_F_* (none defined yet); must be zero.
+ * @timeout_ms: IN: per-call timeout, see SDXI_ADMIN_TIMEOUT_DEFAULT.
+ * @cst_flags:  OUT: reserved for SDXI_ADMIN_DISC_CST_F_* completion bits;
+ *              none defined yet. Kernel writes zero; userspace must
+ *              ignore unknown bits.
+ * @index:      IN: inclusive [start, end] discovery index range.
+ * @buf_size:   IN: bytes available at @buf_uptr; OUT: bytes the kernel
+ *              would have written.
+ * @buf_uptr:   IN: userspace VA of the result buffer.
+ * @rsvd0:      reserved, must be zero.
+ *
+ * Issues an SDXI discovery command on the device's admin context and
+ * copies the resulting records describing installed components for the
+ * requested index range into @buf_uptr. This read-only discovery ioctl is
+ * valid only on the device fd and is supported only on SDXI 1.1+ devices.
+ * It is available to callers that can open the delegated device node;
+ * discovering an operation group does not by itself authorize a context to
+ * enable or submit that operation group.
+ *
+ * If @buf_size on input is smaller than the required size, the ioctl
+ * returns -ENOSPC, leaves @buf_uptr untouched, and updates @buf_size so
+ * the caller can resize and retry.
+ */
+struct sdxi_admin_disc_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u32 timeout_ms;			/* in */
+	__u32 cst_flags;			/* out */
+
+	struct sdxi_admin_range index;		/* in */
+
+	__u32 buf_size;				/* in/out */
+	__aligned_u64 buf_uptr;			/* in */
+
+	__u32 rsvd0[6];				/* reserved */
+};
+
+/**************************/
+/* ADMIN VENDOR OPERATION */
+/**************************/
+/* Reserved opcode for SDXI_ADMIN_VENDOR capability probing. */
+#define SDXI_ADMIN_VENDOR_PROBE		0U
+
+/**
+ * struct sdxi_admin_vendor_args - argument for SDXI_ADMIN_VENDOR.
+ * @argsz:      IN/OUT: size of this struct in bytes.
+ * @flags:      IN: SDXI_ADMIN_VENDOR_F_* (none defined yet); must be zero.
+ * @guid:       IN: 16-byte vendor feature GUID, as returned by
+ *              SDXI_ADMIN_DISC for an opcode type implementing this
+ *              feature. Stored in SDXI spec discovery record byte order;
+ *              userspace passes the bytes verbatim.
+ * @op:         IN: vendor-defined operation. SDXI_ADMIN_VENDOR_PROBE is
+ *              reserved for capability probing (see below).
+ * @timeout_ms: IN: per-call timeout, see SDXI_ADMIN_TIMEOUT_DEFAULT.
+ * @cst_flags:  OUT: reserved for SDXI_ADMIN_VENDOR_CST_F_* completion bits;
+ *              none defined yet. Kernel writes zero; userspace must
+ *              ignore unknown bits.
+ * @rsvd0:      reserved, must be zero.
+ * @in_uptr:    IN: userspace VA of structured input (0 if unused).
+ * @in_size:    IN: input size in bytes (0 if unused).
+ * @rsvd1:      reserved, must be zero.
+ * @out_uptr:   IN: userspace VA of the output buffer (0 if unused).
+ * @out_size:   IN: bytes available at @out_uptr; OUT: bytes required.
+ * @rsvd2:      reserved, must be zero.
+ *
+ * Typed transport for the SDXI vendor operation group. The kernel never
+ * accepts a wire descriptor from userspace: the caller passes (@guid,
+ * @op, structured input, output buffer); the kernel-resident vendor
+ * handler composes the SDXI descriptor, submits it on the device's admin
+ * context, and copies decoded results back. Reserved descriptor bits,
+ * function-targeting bits, and the completion-status block are not under
+ * userspace control.
+ *
+ * Privilege:
+ *   Holding the device fd is required. Per-op privilege escalation
+ *   is enforced by the vendor handler. Read-only telemetry ops MAY be
+ *   allowed without CAP_SYS_ADMIN at the handler's discretion.
+ *
+ * Discovery:
+ *   1. SDXI_ADMIN_DISC enumerates vendor GUIDs installed on the device.
+ *   2. SDXI_ADMIN_VENDOR with @op == SDXI_ADMIN_VENDOR_PROBE returns 0 if the
+ *      kernel has a registered handler for @guid on this device, or
+ *      -EOPNOTSUPP otherwise. PROBE has no in/out payload.
+ *   3. Per-op input/output schemas are vendor-specific.
+ *
+ * Output buffer sizing:
+ *   Pass @out_size = 0 to learn the required size; the ioctl returns
+ *   -ENOSPC, updates @out_size, and leaves @out_uptr untouched. If
+ *   @out_size is non-zero but smaller than required, the ioctl behaves
+ *   the same way.
+ *
+ * Return:
+ *   0          - descriptor completed; @out_uptr contains valid data.
+ *   -ENOSPC    - @out_size too small; @out_size updated, @out_uptr
+ *                untouched.
+ *   -EIO       - device reported a CSB error; @cst_flags may carry
+ *                vendor-specific details.
+ *   -ETIMEDOUT - submission may have reached the device but the kernel
+ *                could not confirm completion; callers should treat
+ *                device state as undefined and may retry idempotent ops.
+ *   other      - request was rejected before submission; device state
+ *                is unchanged.
+ */
+struct sdxi_admin_vendor_args {
+	__u32 argsz;				/* in/out */
+	__u32 flags;				/* in */
+
+	__u8  guid[16];				/* in */
+
+	__u32 op;				/* in */
+	__u32 timeout_ms;			/* in */
+
+	__u32 cst_flags;			/* out */
+	__u32 rsvd0;				/* reserved */
+
+	__aligned_u64 in_uptr;			/* in */
+	__u32 in_size;				/* in */
+	__u32 rsvd1;				/* reserved */
+
+	__aligned_u64 out_uptr;			/* in */
+	__u32 out_size;				/* in/out */
+	__u32 rsvd2[7];				/* reserved */
+};
+
+/*******************/
+/* IOCTL FUNCTIONS */
+/*******************/
+#define SDXI_IOCTL_BASE 'S'
+#define SDXI_IO(nr)			_IO(SDXI_IOCTL_BASE, nr)
+#define SDXI_IOR(nr, type)		_IOR(SDXI_IOCTL_BASE, nr, type)
+#define SDXI_IOW(nr, type)		_IOW(SDXI_IOCTL_BASE, nr, type)
+#define SDXI_IOWR(nr, type)		_IOWR(SDXI_IOCTL_BASE, nr, type)
+
+#define SDXI_GET_UAPI_INFO			\
+		SDXI_IOWR(0x01, struct sdxi_uapi_info_args)
+#define SDXI_GET_DEV_INFO			\
+		SDXI_IOWR(0x02, struct sdxi_dev_info_args)
+#define SDXI_GET_DEV_ATTR			\
+		SDXI_IOWR(0x03, struct sdxi_dev_attr_args)
+#define SDXI_SET_DEV_ATTR			\
+		SDXI_IOWR(0x04, struct sdxi_dev_attr_args)
+#define SDXI_GET_RKEY_LIST			\
+		SDXI_IOWR(0x05, struct sdxi_key_list_args)
+#define SDXI_GET_RKEYS				\
+		SDXI_IOWR(0x06, struct sdxi_get_keys_args)
+#define SDXI_SET_RKEYS				\
+		SDXI_IOWR(0x07, struct sdxi_set_keys_args)
+#define SDXI_CREATE_CXT				\
+		SDXI_IOWR(0x08, struct sdxi_create_cxt_args)
+#define SDXI_CXT_EVENTFD			\
+		SDXI_IOWR(0x09, struct sdxi_cxt_eventfd_args)
+#define SDXI_GET_AKEY_LIST			\
+		SDXI_IOWR(0x0A, struct sdxi_key_list_args)
+#define SDXI_GET_AKEYS				\
+		SDXI_IOWR(0x0B, struct sdxi_get_keys_args)
+#define SDXI_SET_AKEYS				\
+		SDXI_IOWR(0x0C, struct sdxi_set_keys_args)
+#define SDXI_ADMIN_DISC				\
+		SDXI_IOWR(0x0D, struct sdxi_admin_disc_args)
+#define SDXI_ADMIN_VENDOR			\
+		SDXI_IOWR(0x0E, struct sdxi_admin_vendor_args)
+#define SDXI_QUIESCE_CXT			\
+		SDXI_IOWR(0x0F, struct sdxi_quiesce_cxt_args)
+
+#endif /* __LINUX_SDXI_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 15/44] dmaengine: sdxi: add internal ioctl definitions
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (13 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 14/44] dmaengine: sdxi: define the ioctl UAPI surface Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 16/44] dmaengine: sdxi: register core character devices Wei Huang
                   ` (28 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Define device and context file classes, ioctl access classes, and the
page-aligned context mapping token used by the native SDXI character
device. This patch adds no public file operations yet.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/ioctl.h | 79 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 drivers/dma/sdxi/ioctl.h

diff --git a/drivers/dma/sdxi/ioctl.h b/drivers/dma/sdxi/ioctl.h
new file mode 100644
index 000000000000..8677f3e62e93
--- /dev/null
+++ b/drivers/dma/sdxi/ioctl.h
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI ioctl and context mapping definitions.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_IOCTL_H
+#define DMA_SDXI_IOCTL_H
+
+#include <linux/bits.h>
+#include <linux/types.h>
+#include <asm/page.h>
+
+#include "process.h"
+
+struct file;
+struct sdxi_cxt;
+
+enum sdxi_file_type {
+	SDXI_FILE_DEVICE,
+	SDXI_FILE_CONTEXT,
+};
+
+struct sdxi_file {
+	enum sdxi_file_type type;
+	struct sdxi_process *process;
+	struct sdxi_cxt *cxt;
+};
+
+typedef int sdxi_ioctl_t(struct file *file, struct sdxi_file *sf, void *data);
+
+enum sdxi_ioctl_access_class {
+	SDXI_IOCTL_A_PUBLIC_DEVICE,
+	SDXI_IOCTL_A_CONTEXT_OWNER,
+	SDXI_IOCTL_A_ADMIN_DEVICE,
+	SDXI_IOCTL_A_VENDOR_HANDLER,
+};
+
+struct sdxi_ioctl_desc {
+	unsigned int cmd;
+	unsigned int file_flags;
+	enum sdxi_ioctl_access_class access;
+	sdxi_ioctl_t *func;
+	const char *name;
+};
+
+#define SDXI_IOCTL_F_DEVICE	BIT(0)
+#define SDXI_IOCTL_F_CONTEXT	BIT(1)
+
+#define SDXI_MMAP_TYPE_SHIFT		40
+#define SDXI_MMAP_TYPE_DOORBELL		(0x4ULL << SDXI_MMAP_TYPE_SHIFT)
+#define SDXI_MMAP_TYPE_MASK		(0x7ULL << SDXI_MMAP_TYPE_SHIFT)
+
+#define SDXI_MMAP_CXT_ID_SHIFT		24
+#define SDXI_MMAP_CXT_ID_MASK		GENMASK_ULL(39, 24)
+#define SDXI_MMAP_GEN_SHIFT		PAGE_SHIFT
+#define SDXI_MMAP_GEN_MASK		GENMASK_ULL(23, PAGE_SHIFT)
+
+static inline u64 sdxi_mmap_token(u64 type, u32 cxt_id, u32 generation)
+{
+	return type |
+	       (((u64)cxt_id << SDXI_MMAP_CXT_ID_SHIFT) &
+		SDXI_MMAP_CXT_ID_MASK) |
+	       (((u64)generation << SDXI_MMAP_GEN_SHIFT) &
+		SDXI_MMAP_GEN_MASK);
+}
+
+static inline u32 sdxi_mmap_token_cxt_id(u64 token)
+{
+	return (token & SDXI_MMAP_CXT_ID_MASK) >> SDXI_MMAP_CXT_ID_SHIFT;
+}
+
+static inline u32 sdxi_mmap_token_gen(u64 token)
+{
+	return (token & SDXI_MMAP_GEN_MASK) >> SDXI_MMAP_GEN_SHIFT;
+}
+
+#endif /* DMA_SDXI_IOCTL_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 16/44] dmaengine: sdxi: register core character devices
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (14 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 15/44] dmaengine: sdxi: add internal ioctl definitions Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 17/44] dmaengine: sdxi: implement fixed-size ioctl dispatch Wei Huang
                   ` (27 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Create one core-owned SDXI character device per registered function and
bind each open file to process state. Preserve the existing core/PCI
split; functional ioctls and context mappings remain disabled until
their handlers are added later.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Makefile  |   1 +
 drivers/dma/sdxi/context.c |  70 ++++++++++++-----
 drivers/dma/sdxi/context.h |   1 +
 drivers/dma/sdxi/device.c  | 121 +++++++++++++++++++++++++----
 drivers/dma/sdxi/ioctl.c   | 155 +++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/keys.c    |  11 ---
 drivers/dma/sdxi/pci.c     |   1 +
 drivers/dma/sdxi/process.c |  12 ++-
 drivers/dma/sdxi/sdxi.h    |  16 ++++
 9 files changed, 340 insertions(+), 48 deletions(-)
 create mode 100644 drivers/dma/sdxi/ioctl.c

diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index a915982e76a7..a9eaf01df4aa 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -9,6 +9,7 @@ sdxi-core-y := \
 	dma.o         \
 	error.o       \
 	intr.o        \
+	ioctl.o       \
 	keys.o        \
 	process.o     \
 	ring.o
diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index 2c3b6f5df4b5..a4976f2b5e53 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -75,6 +75,7 @@ static void sdxi_free_cxt(struct sdxi_cxt *cxt)
 	mutex_destroy(&cxt->cleanup_lock);
 	kfree(cxt->ring_state);
 	kfree(cxt);
+	put_device(&sdxi->chardev);
 }
 
 DEFINE_FREE(sdxi_cxt, struct sdxi_cxt *, if (_T) sdxi_free_cxt(_T))
@@ -89,6 +90,7 @@ static struct sdxi_cxt *sdxi_alloc_cxt(struct sdxi_dev *sdxi)
 		return NULL;
 
 	cxt->sdxi = sdxi;
+	get_device(&sdxi->chardev);
 	refcount_set(&cxt->refs, 1);
 	mutex_init(&cxt->cleanup_lock);
 	mutex_init(&cxt->akey_lock);
@@ -647,10 +649,9 @@ static int sdxi_cxt_quiesce_locked(struct sdxi_cxt *cxt,
 	return 0;
 }
 
-static void sdxi_cxt_cleanup_workfn(struct work_struct *work)
+static bool sdxi_cxt_cleanup(struct sdxi_cxt *cxt,
+			     struct sdxi_process **process_out)
 {
-	struct sdxi_cxt *cxt = container_of(to_delayed_work(work),
-					     struct sdxi_cxt, cleanup_work);
 	struct sdxi_process *process = NULL;
 	bool force_release = false;
 	bool destroy = false;
@@ -697,6 +698,25 @@ static void sdxi_cxt_cleanup_workfn(struct work_struct *work)
 
 out_unlock:
 	mutex_unlock(&cxt->cleanup_lock);
+	if (!destroy)
+		return false;
+
+	*process_out = process;
+	return true;
+}
+
+static void sdxi_cxt_cleanup_workfn(struct work_struct *work)
+{
+	struct sdxi_cxt *cxt = container_of(to_delayed_work(work),
+					     struct sdxi_cxt, cleanup_work);
+	struct sdxi_process *process = NULL;
+	struct sdxi_dev *sdxi = cxt->sdxi;
+	bool destroy = false;
+
+	down_read(&sdxi->state_lock);
+	if (sdxi->online)
+		destroy = sdxi_cxt_cleanup(cxt, &process);
+	up_read(&sdxi->state_lock);
 
 	if (destroy)
 		sdxi_cxt_finish_release(cxt, process);
@@ -734,13 +754,6 @@ static void sdxi_cxt_force_exit(struct sdxi_cxt *cxt)
 	sdxi_cxt_put(cxt);
 }
 
-static void free_admin_cxt(void *ptr)
-{
-	struct sdxi_dev *sdxi = ptr;
-
-	sdxi_cxt_force_exit(sdxi->admin_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);
@@ -782,7 +795,18 @@ int sdxi_admin_cxt_init(struct sdxi_dev *sdxi)
 	cxt->started = true;
 	sdxi->admin_cxt = no_free_ptr(cxt);
 
-	return devm_add_action_or_reset(sdxi->dev, free_admin_cxt, sdxi);
+	return 0;
+}
+
+void sdxi_admin_cxt_exit(struct sdxi_dev *sdxi)
+{
+	struct sdxi_cxt *cxt = sdxi->admin_cxt;
+
+	if (!cxt)
+		return;
+
+	sdxi->admin_cxt = NULL;
+	sdxi_cxt_force_exit(cxt);
 }
 
 /*
@@ -1154,27 +1178,33 @@ void sdxi_cxt_release_user_handle(struct sdxi_cxt *cxt)
 
 int sdxi_cxt_notify_mm_release(struct sdxi_cxt *cxt)
 {
+	struct sdxi_dev *sdxi;
 	int ret = 0;
 
 	if (!cxt)
 		return -EINVAL;
+	sdxi = cxt->sdxi;
 
+	down_read(&sdxi->state_lock);
 	mutex_lock(&cxt->cleanup_lock);
 	cxt->mm_dead = true;
 	if (!cxt->resources_released) {
-		ret = sdxi_cxt_quiesce_locked(cxt, 0, NULL, false);
-		if (ret) {
-			dev_err(cxt->sdxi->dev,
-				"failed to quiesce context %u during mm release: %d; quiescing function\n",
-				cxt->id, ret);
-			sdxi_dev_fail(cxt->sdxi);
-			cxt->started = false;
-			cxt->quiesced = true;
-			cxt->quiesce_cst_flags = 0;
+		if (sdxi->online) {
+			ret = sdxi_cxt_quiesce_locked(cxt, 0, NULL, false);
+			if (ret) {
+				dev_err(cxt->sdxi->dev,
+					"failed to quiesce context %u during mm release: %d; quiescing function\n",
+					cxt->id, ret);
+				sdxi_dev_fail(cxt->sdxi);
+				cxt->started = false;
+				cxt->quiesced = true;
+				cxt->quiesce_cst_flags = 0;
+			}
 		}
 		sdxi_cxt_queue_cleanup_locked(cxt, 0);
 	}
 	mutex_unlock(&cxt->cleanup_lock);
+	up_read(&sdxi->state_lock);
 
 	return ret;
 }
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index bbe83e54b827..03cb5634fa09 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -89,6 +89,7 @@ struct sdxi_cxt {
 };
 
 int sdxi_admin_cxt_init(struct sdxi_dev *sdxi);
+void sdxi_admin_cxt_exit(struct sdxi_dev *sdxi);
 struct sdxi_cxt *sdxi_cxt_new(struct sdxi_dev *sdxi);
 struct sdxi_cxt *sdxi_ucxt_new(struct sdxi_dev *sdxi, u32 ring_entries,
 			       u64 ds_ring, u64 write_index, u64 cxt_sts,
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index 1587795c0a16..ff5b5d932bc9 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -414,8 +414,10 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	sdxi->L2_table = dmam_alloc_coherent(sdxi->dev,
 					     sizeof(*sdxi->L2_table),
 					     &sdxi->L2_dma, GFP_KERNEL);
-	if (!sdxi->L2_table)
-		return -ENOMEM;
+	if (!sdxi->L2_table) {
+		err = -ENOMEM;
+		goto exit_keys;
+	}
 	memset(sdxi->L2_table, 0, sizeof(*sdxi->L2_table));
 
 	cxt_l2 = FIELD_PREP(SDXI_MMIO_CXT_L2_PTR, sdxi->L2_dma >> ilog2(SZ_4K));
@@ -429,11 +431,11 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	 */
 	err = sdxi_admin_cxt_init(sdxi);
 	if (err)
-		return err;
+		goto exit_keys;
 
 	err = sdxi_error_init(sdxi);
 	if (err)
-		return err;
+		goto exit_admin;
 
 	/*
 	 * SDXI 1.0 4.1.8.9: Set MMIO_CTL0.fn_gsr to GSRV_ACTIVE and
@@ -449,8 +451,15 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 		sdxi_error_exit(sdxi);
 		if (sdxi_dev_stop(sdxi))
 			sdxi_dev_force_quiesce(sdxi);
+		goto exit_admin;
 	}
 
+	return 0;
+
+exit_admin:
+	sdxi_admin_cxt_exit(sdxi);
+exit_keys:
+	sdxi_keys_exit(sdxi);
 	return err;
 }
 
@@ -469,38 +478,100 @@ static int sdxi_device_init(struct sdxi_dev *sdxi)
 	if (err)
 		return err;
 
-	sdxi_dma_register(sdxi);
+	err = sdxi_dma_register(sdxi);
+	if (err) {
+		sdxi_error_exit(sdxi);
+		if (sdxi_dev_stop(sdxi))
+			sdxi_dev_force_quiesce(sdxi);
+		sdxi_admin_cxt_exit(sdxi);
+		sdxi_keys_exit(sdxi);
+		return err;
+	}
+
 	return 0;
 }
 
+static void sdxi_device_quiesce(struct sdxi_dev *sdxi)
+{
+	sdxi_error_exit(sdxi);
+	if (sdxi_dev_stop(sdxi))
+		sdxi_dev_force_quiesce(sdxi);
+}
+
+/* Hardware must already be quiesced before this releases DMA-visible state. */
+static void sdxi_device_exit(struct sdxi_dev *sdxi)
+{
+	sdxi_client_cxts_exit(sdxi);
+	sdxi_admin_cxt_exit(sdxi);
+	sdxi_keys_exit(sdxi);
+	xa_destroy(&sdxi->client_cxts);
+	ida_destroy(&sdxi->vectors);
+}
+
+static void sdxi_dev_release(struct device *chardev)
+{
+	struct sdxi_dev *sdxi = container_of(chardev, struct sdxi_dev,
+					     chardev);
+
+	put_device(sdxi->dev);
+	kfree(sdxi);
+}
+
 int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 {
 	struct sdxi_dev *sdxi;
 	int err;
 
-	sdxi = devm_kzalloc(dev, sizeof(*sdxi), GFP_KERNEL);
+	sdxi = kzalloc_obj(*sdxi, GFP_KERNEL);
 	if (!sdxi)
 		return -ENOMEM;
 
-	sdxi->dev = dev;
+	sdxi->dev = get_device(dev);
 	sdxi->bus_ops = ops;
+	device_initialize(&sdxi->chardev);
+	sdxi->chardev.release = sdxi_dev_release;
+	init_rwsem(&sdxi->state_lock);
 	ida_init(&sdxi->vectors);
 	xa_init_flags(&sdxi->client_cxts, XA_FLAGS_ALLOC1);
 	mutex_init(&sdxi->cxt_lock);
 	mutex_init(&sdxi->admin_lock);
 	mutex_init(&sdxi->rkey_lock);
 	sdxi->error_irq = -1;
+	sdxi->minor = -1;
 	dev_set_drvdata(dev, sdxi);
 
 	err = sdxi->bus_ops->init(sdxi);
 	if (err)
-		return err;
+		goto destroy_allocators;
 
 	err = sdxi_fn_prepare(sdxi);
 	if (err)
-		return err;
+		goto destroy_allocators;
+
+	err = sdxi_device_init(sdxi);
+	if (err)
+		goto destroy_allocators;
+
+	down_write(&sdxi->state_lock);
+	err = sdxi_chardev_register(sdxi);
+	if (!err)
+		sdxi->online = true;
+	up_write(&sdxi->state_lock);
+	if (err) {
+		sdxi_device_quiesce(sdxi);
+		sdxi_device_exit(sdxi);
+		goto put_chardev;
+	}
+
+	return 0;
 
-	return sdxi_device_init(sdxi);
+destroy_allocators:
+	xa_destroy(&sdxi->client_cxts);
+	ida_destroy(&sdxi->vectors);
+put_chardev:
+	dev_set_drvdata(dev, NULL);
+	put_device(&sdxi->chardev);
+	return err;
 }
 EXPORT_SYMBOL_NS_GPL(sdxi_register, "SDXI");
 
@@ -508,16 +579,34 @@ void sdxi_unregister(struct device *dev)
 {
 	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
 
-	sdxi_error_exit(sdxi);
-	if (sdxi_dev_stop(sdxi))
-		sdxi_dev_force_quiesce(sdxi);
-	sdxi_client_cxts_exit(sdxi);
-	sdxi_keys_exit(sdxi);
-	ida_destroy(&sdxi->vectors);
+	if (!sdxi)
+		return;
+
+	down_write(&sdxi->state_lock);
+	sdxi_chardev_unregister(sdxi);
+	sdxi_device_quiesce(sdxi);
+	sdxi->online = false;
+	up_write(&sdxi->state_lock);
+
+	sdxi_device_exit(sdxi);
+	dev_set_drvdata(dev, NULL);
+	put_device(&sdxi->chardev);
 }
 EXPORT_SYMBOL_NS_GPL(sdxi_unregister, "SDXI");
 
+static int __init sdxi_core_init(void)
+{
+	return sdxi_chardev_init();
+}
+
+static void __exit sdxi_core_exit(void)
+{
+	sdxi_chardev_exit();
+}
+
 MODULE_AUTHOR("Wei Huang");
 MODULE_AUTHOR("Nathan Lynch");
 MODULE_DESCRIPTION("SDXI core");
 MODULE_LICENSE("GPL");
+module_init(sdxi_core_init);
+module_exit(sdxi_core_exit);
diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
new file mode 100644
index 000000000000..17661fca54a7
--- /dev/null
+++ b/drivers/dma/sdxi/ioctl.c
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI character device support.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/cdev.h>
+#include <linux/device.h>
+#include <linux/fs.h>
+#include <linux/idr.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include "ioctl.h"
+#include "process.h"
+#include "sdxi.h"
+
+#define SDXI_MAX_MINORS	256
+
+static dev_t sdxi_devt;
+static struct class *sdxi_class;
+static DEFINE_IDA(sdxi_minor_ida);
+
+static long sdxi_ioctl_unsupported(struct file *file, unsigned int cmd,
+				   unsigned long arg)
+{
+	return -ENOTTY;
+}
+
+static int sdxi_open(struct inode *inode, struct file *file)
+{
+	struct sdxi_dev *sdxi;
+	struct sdxi_process *process;
+	struct sdxi_file *sf;
+
+	if (!inode->i_cdev)
+		return -ENODEV;
+
+	sdxi = container_of(inode->i_cdev, struct sdxi_dev, cdev);
+	down_read(&sdxi->state_lock);
+	if (!sdxi->online) {
+		up_read(&sdxi->state_lock);
+		return -ENODEV;
+	}
+	process = sdxi_process_create(sdxi);
+	up_read(&sdxi->state_lock);
+	if (IS_ERR(process))
+		return PTR_ERR(process);
+
+	sf = kzalloc_obj(*sf, GFP_KERNEL);
+	if (!sf) {
+		sdxi_process_put(process);
+		return -ENOMEM;
+	}
+
+	sf->type = SDXI_FILE_DEVICE;
+	sf->process = process;
+	file->private_data = sf;
+	return 0;
+}
+
+static int sdxi_release(struct inode *inode, struct file *file)
+{
+	struct sdxi_file *sf = file->private_data;
+
+	if (!sf)
+		return 0;
+
+	sdxi_process_put(sf->process);
+	kfree(sf);
+	file->private_data = NULL;
+	return 0;
+}
+
+static const struct file_operations sdxi_fops = {
+	.owner = THIS_MODULE,
+	.open = sdxi_open,
+	.release = sdxi_release,
+	.unlocked_ioctl = sdxi_ioctl_unsupported,
+	.compat_ioctl = sdxi_ioctl_unsupported,
+};
+
+int sdxi_chardev_init(void)
+{
+	int ret;
+
+	ret = alloc_chrdev_region(&sdxi_devt, 0, SDXI_MAX_MINORS, "sdxi");
+	if (ret)
+		return ret;
+
+	sdxi_class = class_create("sdxi");
+	if (IS_ERR(sdxi_class)) {
+		ret = PTR_ERR(sdxi_class);
+		sdxi_class = NULL;
+		unregister_chrdev_region(sdxi_devt, SDXI_MAX_MINORS);
+		return ret;
+	}
+
+	return 0;
+}
+
+void sdxi_chardev_exit(void)
+{
+	class_destroy(sdxi_class);
+	sdxi_class = NULL;
+	unregister_chrdev_region(sdxi_devt, SDXI_MAX_MINORS);
+	ida_destroy(&sdxi_minor_ida);
+}
+
+int sdxi_chardev_register(struct sdxi_dev *sdxi)
+{
+	dev_t devt;
+	int minor;
+	int ret;
+
+	minor = ida_alloc_max(&sdxi_minor_ida, SDXI_MAX_MINORS - 1,
+			      GFP_KERNEL);
+	if (minor < 0)
+		return minor;
+
+	sdxi->minor = minor;
+	devt = MKDEV(MAJOR(sdxi_devt), minor);
+	sdxi->chardev.devt = devt;
+	sdxi->chardev.class = sdxi_class;
+	sdxi->chardev.parent = sdxi->dev;
+	dev_set_drvdata(&sdxi->chardev, sdxi);
+	ret = dev_set_name(&sdxi->chardev, "sdxi%d", minor);
+	if (ret)
+		goto free_minor;
+
+	cdev_init(&sdxi->cdev, &sdxi_fops);
+	sdxi->cdev.owner = THIS_MODULE;
+
+	ret = cdev_device_add(&sdxi->cdev, &sdxi->chardev);
+	if (ret)
+		goto free_minor;
+
+	return 0;
+
+free_minor:
+	sdxi->minor = -1;
+	ida_free(&sdxi_minor_ida, minor);
+	return ret;
+}
+
+void sdxi_chardev_unregister(struct sdxi_dev *sdxi)
+{
+	if (sdxi->minor < 0)
+		return;
+
+	cdev_device_del(&sdxi->cdev, &sdxi->chardev);
+	ida_free(&sdxi_minor_ida, sdxi->minor);
+	sdxi->minor = -1;
+}
diff --git a/drivers/dma/sdxi/keys.c b/drivers/dma/sdxi/keys.c
index 2e25a9b806e6..c2f1db0d1594 100644
--- a/drivers/dma/sdxi/keys.c
+++ b/drivers/dma/sdxi/keys.c
@@ -6,7 +6,6 @@
  */
 
 #include <linux/bitfield.h>
-#include <linux/device/devres.h>
 #include <linux/dma-mapping.h>
 #include <linux/log2.h>
 #include <linux/sizes.h>
@@ -17,16 +16,10 @@
 #include "mmio.h"
 #include "sdxi.h"
 
-static void sdxi_keys_release(void *data)
-{
-	sdxi_keys_exit(data);
-}
-
 int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1)
 {
 	size_t table_size;
 	u64 reg;
-	int ret;
 
 	sdxi->rkey_cap = FIELD_GET(SDXI_MMIO_CAP1_RKEY_CAP, cap1);
 	if (!sdxi->rkey_cap)
@@ -49,10 +42,6 @@ int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1)
 	      SDXI_MMIO_RKEY_EN;
 	sdxi_write64(sdxi, SDXI_MMIO_RKEY, reg);
 
-	ret = devm_add_action_or_reset(sdxi->dev, sdxi_keys_release, sdxi);
-	if (ret)
-		return ret;
-
 	return 0;
 }
 
diff --git a/drivers/dma/sdxi/pci.c b/drivers/dma/sdxi/pci.c
index 7b477b7bec55..eca55c8d65f8 100644
--- a/drivers/dma/sdxi/pci.c
+++ b/drivers/dma/sdxi/pci.c
@@ -130,6 +130,7 @@ static void sdxi_pci_force_quiesce(struct sdxi_dev *sdxi)
 }
 
 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,
diff --git a/drivers/dma/sdxi/process.c b/drivers/dma/sdxi/process.c
index bc0552b554ba..b64f2307e4e3 100644
--- a/drivers/dma/sdxi/process.c
+++ b/drivers/dma/sdxi/process.c
@@ -6,6 +6,7 @@
  */
 
 #include <linux/iommu.h>
+#include <linux/module.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/task.h>
 #include <linux/slab.h>
@@ -100,6 +101,7 @@ void sdxi_process_unbind_sva(struct sdxi_process *process,
 
 static void sdxi_process_free(struct sdxi_process *process)
 {
+	struct sdxi_dev *sdxi = process->sdxi;
 	struct iommu_sva *sva;
 	u32 pasid;
 
@@ -111,6 +113,8 @@ static void sdxi_process_free(struct sdxi_process *process)
 	put_task_struct(process->lead_thread);
 	mmdrop(process->mm);
 	kfree(process);
+	module_put(sdxi->bus_ops->owner);
+	put_device(&sdxi->chardev);
 }
 
 void sdxi_process_get(struct sdxi_process *process)
@@ -167,10 +171,16 @@ struct sdxi_process *sdxi_process_create(struct sdxi_dev *sdxi)
 
 	if (!task->mm)
 		return ERR_PTR(-EINVAL);
+	if (!try_module_get(sdxi->bus_ops->owner))
+		return ERR_PTR(-ENODEV);
+	get_device(&sdxi->chardev);
 
 	process = kzalloc_obj(*process, GFP_KERNEL);
-	if (!process)
+	if (!process) {
+		put_device(&sdxi->chardev);
+		module_put(sdxi->bus_ops->owner);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	refcount_set(&process->refs, 1);
 	mutex_init(&process->mutex);
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index fa34aaa36c16..98d2baffbbdf 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -9,11 +9,14 @@
 #define DMA_SDXI_H
 
 #include <linux/bug.h>
+#include <linux/cdev.h>
 #include <linux/compiler_types.h>
+#include <linux/device.h>
 #include <linux/dev_printk.h>
 #include <linux/idr.h>
 #include <linux/io.h>
 #include <linux/mutex.h>
+#include <linux/rwsem.h>
 #include <linux/types.h>
 #include <linux/xarray.h>
 
@@ -44,11 +47,14 @@ enum {
 };
 
 struct sdxi_dev;
+struct module;
 
 /**
  * struct sdxi_bus_ops - Bus-specific methods for SDXI devices.
  */
 struct sdxi_bus_ops {
+	/** @owner: Module providing these operations, or NULL if built in. */
+	struct module *owner;
 	/**
 	 * @init: Map control registers and doorbell region, allocate
 	 *        IRQ ranges. Invoked before bus-agnostic SDXI
@@ -117,6 +123,12 @@ struct sdxi_dev {
 
 	bool has_mailbox;
 
+	struct cdev cdev;
+	struct device chardev;
+	int minor;
+	bool online;
+	struct rw_semaphore state_lock;	/* online state and user operations */
+
 	const struct sdxi_bus_ops *bus_ops;
 };
 
@@ -163,6 +175,10 @@ void sdxi_unregister(struct device *dev);
 bool sdxi_dev_in_error(const struct sdxi_dev *sdxi);
 void sdxi_dev_force_quiesce(struct sdxi_dev *sdxi);
 void sdxi_dev_fail(struct sdxi_dev *sdxi);
+int sdxi_chardev_init(void);
+void sdxi_chardev_exit(void);
+int sdxi_chardev_register(struct sdxi_dev *sdxi);
+void sdxi_chardev_unregister(struct sdxi_dev *sdxi);
 
 static inline u64 sdxi_read64(const struct sdxi_dev *sdxi, enum sdxi_reg reg)
 {
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 17/44] dmaengine: sdxi: implement fixed-size ioctl dispatch
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (15 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 16/44] dmaengine: sdxi: register core character devices Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 18/44] dmaengine: sdxi: implement userspace context file operations Wei Huang
                   ` (26 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add the final ioctl table and access-class checks, with unsupported
rows for features that are not wired yet. Implement UAPI discovery,
device information UAPI, and bounded device attributes while advertising
only those alive capabilities.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/device.c |   9 +-
 drivers/dma/sdxi/error.c  |   4 +
 drivers/dma/sdxi/ioctl.c  | 413 +++++++++++++++++++++++++++++++++++++-
 drivers/dma/sdxi/ioctl.h  |   1 -
 drivers/dma/sdxi/sdxi.h   |   1 +
 5 files changed, 420 insertions(+), 8 deletions(-)

diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index ff5b5d932bc9..b5960619d511 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -110,12 +110,14 @@ static const unsigned long gsv_transition_timeout_us = USEC_PER_SEC * 1;
 
 static void sdxi_write_fn_gsr(struct sdxi_dev *sdxi, enum sdxi_fn_gsr cmd)
 {
-	u64 ctl0 = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
+	u64 ctl0;
 
+	mutex_lock(&sdxi->reg_lock);
+	ctl0 = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
 	dev_dbg(sdxi->dev, "MMIO_CTL0.fn_gsr = %#x (%s)\n", cmd, gsr_str(cmd));
-
 	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_GSR, &ctl0, cmd);
 	sdxi_write64(sdxi, SDXI_MMIO_CTL0, ctl0);
+	mutex_unlock(&sdxi->reg_lock);
 }
 
 /*
@@ -319,11 +321,13 @@ static int sdxi_fn_prepare(struct sdxi_dev *sdxi)
 	if (err)
 		return err;
 
+	mutex_lock(&sdxi->reg_lock);
 	ctl0 = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
 	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_ERR_INTR_EN, &ctl0, 0);
 	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_PASID_VL, &ctl0, 0);
 	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_PASID, &ctl0, 0);
 	sdxi_write64(sdxi, SDXI_MMIO_CTL0, ctl0);
+	mutex_unlock(&sdxi->reg_lock);
 
 	/* Restore CTL2's reset limits, with operation groups disabled. */
 	cap1 = sdxi_read64(sdxi, SDXI_MMIO_CAP1);
@@ -534,6 +538,7 @@ int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 	ida_init(&sdxi->vectors);
 	xa_init_flags(&sdxi->client_cxts, XA_FLAGS_ALLOC1);
 	mutex_init(&sdxi->cxt_lock);
+	mutex_init(&sdxi->reg_lock);
 	mutex_init(&sdxi->admin_lock);
 	mutex_init(&sdxi->rkey_lock);
 	sdxi->error_irq = -1;
diff --git a/drivers/dma/sdxi/error.c b/drivers/dma/sdxi/error.c
index 4d8607b3bbec..d4c1e64dbf2b 100644
--- a/drivers/dma/sdxi/error.c
+++ b/drivers/dma/sdxi/error.c
@@ -267,6 +267,7 @@ int sdxi_error_init(struct sdxi_dev *sdxi)
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_WRT, 0);
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_RD, 0);
 
+	mutex_lock(&sdxi->reg_lock);
 	reg = sdxi_read64(sdxi, SDXI_MMIO_ERR_CTL);
 	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 1);
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
@@ -274,6 +275,7 @@ int sdxi_error_init(struct sdxi_dev *sdxi)
 	reg = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
 	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_ERR_INTR_EN, &reg, 1);
 	sdxi_write64(sdxi, SDXI_MMIO_CTL0, reg);
+	mutex_unlock(&sdxi->reg_lock);
 
 	size_order = ilog2(SDXI_ERRLOG_SIZE) - ilog2(SZ_4K);
 	reg = FIELD_PREP(SDXI_MMIO_ERR_CFG_PTR,
@@ -294,6 +296,7 @@ void sdxi_error_exit(struct sdxi_dev *sdxi)
 
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_CFG, 0);
 
+	mutex_lock(&sdxi->reg_lock);
 	reg = sdxi_read64(sdxi, SDXI_MMIO_ERR_CTL);
 	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 0);
 	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
@@ -301,6 +304,7 @@ void sdxi_error_exit(struct sdxi_dev *sdxi)
 	reg = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
 	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_ERR_INTR_EN, &reg, 0);
 	sdxi_write64(sdxi, SDXI_MMIO_CTL0, reg);
+	mutex_unlock(&sdxi->reg_lock);
 
 	if (sdxi->error_irq >= 0)
 		devm_free_irq(sdxi->dev, sdxi->error_irq, sdxi);
diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index 17661fca54a7..b25f8bc273c8 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -6,13 +6,21 @@
  */
 
 #include <linux/cdev.h>
+#include <linux/bitfield.h>
+#include <linux/capability.h>
+#include <linux/compat.h>
 #include <linux/device.h>
 #include <linux/fs.h>
 #include <linux/idr.h>
+#include <linux/mm.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/uaccess.h>
+#include <uapi/linux/sdxi.h>
 
 #include "ioctl.h"
+#include "mmio.h"
 #include "process.h"
 #include "sdxi.h"
 
@@ -22,10 +30,405 @@ static dev_t sdxi_devt;
 static struct class *sdxi_class;
 static DEFINE_IDA(sdxi_minor_ida);
 
-static long sdxi_ioctl_unsupported(struct file *file, unsigned int cmd,
-				   unsigned long arg)
+static int sdxi_file_check_owner(const struct sdxi_process *process)
 {
-	return -ENOTTY;
+	if (process->lead_thread != current->group_leader ||
+	    process->mm != current->mm)
+		return -EBADF;
+
+	return 0;
+}
+
+static int sdxi_ioctl_unsupported(struct file *file, struct sdxi_file *sf,
+				  void *data)
+{
+	return -EOPNOTSUPP;
+}
+
+static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
+				    void *data)
+{
+	struct sdxi_uapi_info_args *args = data;
+
+	if (args->argsz < sizeof(*args) || args->flags || args->rsvd0)
+		return -EINVAL;
+
+	*args = (typeof(*args)) {
+		.argsz = sizeof(*args),
+		.uapi_major = SDXI_UAPI_MAJOR,
+		.uapi_minor = SDXI_UAPI_MINOR,
+		.cap_words = SDXI_UAPI_CAP_WORDS,
+		.caps = {
+			SDXI_UAPI_CAP_DEV_INFO |
+			SDXI_UAPI_CAP_DEV_ATTR_MGMT,
+		},
+	};
+
+	return 0;
+}
+
+static int sdxi_ioctl_get_dev_info(struct file *file, struct sdxi_file *sf,
+				   void *data)
+{
+	struct sdxi_dev_info_args *args = data;
+	struct sdxi_dev *sdxi = sf->process->sdxi;
+	size_t i;
+
+	if (args->argsz < sizeof(*args) || args->flags)
+		return -EINVAL;
+	if (args->rsvd0)
+		return -EINVAL;
+	for (i = 0; i < ARRAY_SIZE(args->rsvd1); i++)
+		if (args->rsvd1[i])
+			return -EINVAL;
+
+	*args = (typeof(*args)) {
+		.argsz = sizeof(*args),
+		.hw_major = sdxi->sdxi_version.major,
+		.hw_minor = sdxi->sdxi_version.minor,
+		.cap0 = sdxi_read64(sdxi, SDXI_MMIO_CAP0),
+		.cap1 = sdxi_read64(sdxi, SDXI_MMIO_CAP1),
+		.sts0 = sdxi_read64(sdxi, SDXI_MMIO_STS0),
+	};
+
+	return 0;
+}
+
+enum {
+	SDXI_DEV_ATTR_F_RO = BIT(0),
+};
+
+struct sdxi_dev_attr_desc {
+	u32 reg;
+	u64 mask;
+	u32 cap_reg;
+	u64 cap_mask;
+	unsigned int flags;
+	int (*get)(struct sdxi_dev *sdxi, u64 *value);
+};
+
+static int sdxi_dev_attr_get_rkey_entries(struct sdxi_dev *sdxi, u64 *value)
+{
+	*value = sdxi->rkey_cap ? sdxi->rkey_entries : 0;
+	return 0;
+}
+
+static const struct sdxi_dev_attr_desc sdxi_dev_attrs[] = {
+	[SDXI_DEV_ATTR_GSR] = {
+		.reg = SDXI_MMIO_CTL0,
+		.mask = SDXI_MMIO_CTL0_FN_GSR,
+		.flags = SDXI_DEV_ATTR_F_RO,
+	},
+	[SDXI_DEV_ATTR_GRP_ID] = {
+		.reg = SDXI_MMIO_CTL0,
+		.mask = SDXI_MMIO_CTL0_FN_GRP_ID,
+	},
+	[SDXI_DEV_ATTR_MAX_BUFFER] = {
+		.reg = SDXI_MMIO_CTL2,
+		.mask = SDXI_MMIO_CTL2_MAX_BUFFER,
+		.cap_reg = SDXI_MMIO_CAP1,
+		.cap_mask = SDXI_MMIO_CAP1_MAX_BUFFER,
+	},
+	[SDXI_DEV_ATTR_MAX_AKEY_SZ] = {
+		.reg = SDXI_MMIO_CTL2,
+		.mask = SDXI_MMIO_CTL2_MAX_AKEY_SZ,
+		.cap_reg = SDXI_MMIO_CAP1,
+		.cap_mask = SDXI_MMIO_CAP1_MAX_AKEY_SZ,
+	},
+	[SDXI_DEV_ATTR_MAX_RKEY_SZ] = {
+		.flags = SDXI_DEV_ATTR_F_RO,
+		.get = sdxi_dev_attr_get_rkey_entries,
+	},
+	[SDXI_DEV_ATTR_MAX_CXT] = {
+		.reg = SDXI_MMIO_CTL2,
+		.mask = SDXI_MMIO_CTL2_MAX_CXT,
+		.cap_reg = SDXI_MMIO_CAP1,
+		.cap_mask = SDXI_MMIO_CAP1_MAX_CXT,
+	},
+};
+
+static const struct sdxi_dev_attr_desc *sdxi_dev_attr_lookup(u64 attr)
+{
+	const struct sdxi_dev_attr_desc *desc;
+
+	if (!attr || attr >= ARRAY_SIZE(sdxi_dev_attrs))
+		return NULL;
+
+	desc = &sdxi_dev_attrs[attr];
+	if (!desc->mask && !desc->get)
+		return NULL;
+
+	return desc;
+}
+
+static int sdxi_dev_attr_read(struct sdxi_dev *sdxi,
+			      const struct sdxi_dev_attr_desc *desc, u64 *value)
+{
+	unsigned int shift;
+
+	if (desc->get)
+		return desc->get(sdxi, value);
+
+	shift = __ffs64(desc->mask);
+	*value = (sdxi_read64(sdxi, desc->reg) & desc->mask) >> shift;
+	return 0;
+}
+
+static int sdxi_dev_attr_write(struct sdxi_dev *sdxi,
+			       const struct sdxi_dev_attr_desc *desc, u64 value)
+{
+	unsigned int shift;
+	u64 maximum;
+	u64 register_value;
+
+	if (desc->flags & SDXI_DEV_ATTR_F_RO)
+		return -EOPNOTSUPP;
+
+	shift = __ffs64(desc->mask);
+	maximum = desc->mask >> shift;
+	if (value > maximum)
+		return -EINVAL;
+
+	mutex_lock(&sdxi->reg_lock);
+	if (desc->cap_reg) {
+		u64 capability;
+
+		capability = (sdxi_read64(sdxi, desc->cap_reg) &
+			      desc->cap_mask) >> __ffs64(desc->cap_mask);
+		if (value > capability) {
+			mutex_unlock(&sdxi->reg_lock);
+			return -EINVAL;
+		}
+	}
+
+	register_value = sdxi_read64(sdxi, desc->reg);
+	register_value &= ~desc->mask;
+	register_value |= (value << shift) & desc->mask;
+	sdxi_write64(sdxi, desc->reg, register_value);
+	mutex_unlock(&sdxi->reg_lock);
+	return 0;
+}
+
+static bool sdxi_dev_attr_args_valid(const struct sdxi_dev_attr_args *args)
+{
+	size_t i;
+
+	if (args->argsz < sizeof(*args) || args->flags)
+		return false;
+	for (i = 0; i < ARRAY_SIZE(args->rsvd0); i++)
+		if (args->rsvd0[i])
+			return false;
+
+	return true;
+}
+
+static int sdxi_ioctl_get_dev_attr(struct file *file, struct sdxi_file *sf,
+				   void *data)
+{
+	struct sdxi_dev_attr_args *args = data;
+	const struct sdxi_dev_attr_desc *desc;
+	int ret;
+
+	if (!sdxi_dev_attr_args_valid(args))
+		return -EINVAL;
+
+	desc = sdxi_dev_attr_lookup(args->attr);
+	if (!desc)
+		return -EOPNOTSUPP;
+
+	ret = sdxi_dev_attr_read(sf->process->sdxi, desc, &args->val);
+	if (!ret)
+		args->argsz = sizeof(*args);
+	return ret;
+}
+
+static int sdxi_ioctl_set_dev_attr(struct file *file, struct sdxi_file *sf,
+				   void *data)
+{
+	struct sdxi_dev_attr_args *args = data;
+	const struct sdxi_dev_attr_desc *desc;
+	int ret;
+
+	if (!sdxi_dev_attr_args_valid(args))
+		return -EINVAL;
+
+	desc = sdxi_dev_attr_lookup(args->attr);
+	if (!desc)
+		return -EOPNOTSUPP;
+
+	ret = sdxi_dev_attr_write(sf->process->sdxi, desc, args->val);
+	if (ret)
+		return ret;
+
+	args->argsz = sizeof(*args);
+	return sdxi_dev_attr_read(sf->process->sdxi, desc, &args->val);
+}
+
+#define SDXI_IOCTL_DEF(_nr, _cmd, _func, _files, _access)	\
+	[_nr] = {						\
+		.cmd = (_cmd),					\
+		.file_flags = (_files),				\
+		.access = (_access),				\
+		.func = (_func),					\
+	}
+
+static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
+	SDXI_IOCTL_DEF(0x01, SDXI_GET_UAPI_INFO, sdxi_ioctl_get_uapi_info,
+		       SDXI_IOCTL_F_DEVICE | SDXI_IOCTL_F_CONTEXT,
+		       SDXI_IOCTL_A_PUBLIC_DEVICE),
+	SDXI_IOCTL_DEF(0x02, SDXI_GET_DEV_INFO, sdxi_ioctl_get_dev_info,
+		       SDXI_IOCTL_F_DEVICE | SDXI_IOCTL_F_CONTEXT,
+		       SDXI_IOCTL_A_PUBLIC_DEVICE),
+	SDXI_IOCTL_DEF(0x03, SDXI_GET_DEV_ATTR, sdxi_ioctl_get_dev_attr,
+		       SDXI_IOCTL_F_DEVICE | SDXI_IOCTL_F_CONTEXT,
+		       SDXI_IOCTL_A_PUBLIC_DEVICE),
+	SDXI_IOCTL_DEF(0x04, SDXI_SET_DEV_ATTR, sdxi_ioctl_set_dev_attr,
+		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
+	SDXI_IOCTL_DEF(0x05, SDXI_GET_RKEY_LIST, sdxi_ioctl_unsupported,
+		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
+	SDXI_IOCTL_DEF(0x06, SDXI_GET_RKEYS, sdxi_ioctl_unsupported,
+		       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_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
+	SDXI_IOCTL_DEF(0x09, SDXI_CXT_EVENTFD, sdxi_ioctl_unsupported,
+		       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),
+	SDXI_IOCTL_DEF(0x0b, SDXI_GET_AKEYS, sdxi_ioctl_unsupported,
+		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
+	SDXI_IOCTL_DEF(0x0c, SDXI_SET_AKEYS, sdxi_ioctl_unsupported,
+		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
+	SDXI_IOCTL_DEF(0x0d, SDXI_ADMIN_DISC, sdxi_ioctl_unsupported,
+		       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_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
+};
+
+static int sdxi_ioctl_permit(const struct sdxi_ioctl_desc *desc)
+{
+	switch (desc->access) {
+	case SDXI_IOCTL_A_PUBLIC_DEVICE:
+	case SDXI_IOCTL_A_CONTEXT_OWNER:
+		return 0;
+	case SDXI_IOCTL_A_ADMIN_DEVICE:
+		return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
+	case SDXI_IOCTL_A_VENDOR_HANDLER:
+		return -EOPNOTSUPP;
+	}
+
+	return -EINVAL;
+}
+
+static bool sdxi_ioctl_has_trailing_payload(unsigned int cmd)
+{
+	return cmd == SDXI_GET_RKEY_LIST || cmd == SDXI_GET_RKEYS ||
+	       cmd == SDXI_SET_RKEYS || cmd == SDXI_GET_AKEY_LIST ||
+	       cmd == SDXI_GET_AKEYS || cmd == SDXI_SET_AKEYS;
+}
+
+static long sdxi_ioctl_live(struct file *file, unsigned int cmd,
+			    unsigned long arg)
+{
+	const struct sdxi_ioctl_desc *desc;
+	struct sdxi_file *sf = file->private_data;
+	unsigned int canonical_size;
+	unsigned int user_size;
+	unsigned int size;
+	unsigned int nr;
+	char stack_data[128];
+	void *data = stack_data;
+	bool allowed;
+	int ret;
+
+	if (_IOC_TYPE(cmd) != SDXI_IOCTL_BASE)
+		return -ENOTTY;
+
+	nr = _IOC_NR(cmd);
+	if (nr >= ARRAY_SIZE(sdxi_ioctls) || !sdxi_ioctls[nr].cmd)
+		return -ENOTTY;
+
+	desc = &sdxi_ioctls[nr];
+	if (_IOC_DIR(cmd) != _IOC_DIR(desc->cmd))
+		return -ENOTTY;
+	if (!sf || !sf->process)
+		return -EINVAL;
+
+	ret = sdxi_file_check_owner(sf->process);
+	if (ret)
+		return ret;
+
+	switch (sf->type) {
+	case SDXI_FILE_DEVICE:
+		allowed = desc->file_flags & SDXI_IOCTL_F_DEVICE;
+		break;
+	case SDXI_FILE_CONTEXT:
+		allowed = desc->file_flags & SDXI_IOCTL_F_CONTEXT;
+		break;
+	default:
+		allowed = false;
+		break;
+	}
+	if (!allowed)
+		return -ENOTTY;
+
+	ret = sdxi_ioctl_permit(desc);
+	if (ret)
+		return ret;
+
+	canonical_size = _IOC_SIZE(desc->cmd);
+	user_size = _IOC_SIZE(cmd);
+	size = max(canonical_size, user_size);
+	if (size > PAGE_SIZE)
+		return -E2BIG;
+
+	if (size > sizeof(stack_data)) {
+		data = kzalloc(size, GFP_KERNEL);
+		if (!data)
+			return -ENOMEM;
+	} else {
+		memset(data, 0, size);
+	}
+
+	if ((_IOC_DIR(cmd) & _IOC_WRITE) &&
+	    copy_from_user(data, (void __user *)arg, user_size)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	ret = desc->func(file, sf, data);
+
+	if ((_IOC_DIR(cmd) & _IOC_READ) && user_size > canonical_size &&
+	    !sdxi_ioctl_has_trailing_payload(desc->cmd))
+		memset((u8 *)data + canonical_size, 0,
+		       user_size - canonical_size);
+	if ((_IOC_DIR(cmd) & _IOC_READ) &&
+	    copy_to_user((void __user *)arg, data, user_size))
+		ret = -EFAULT;
+
+out:
+	if (data != stack_data)
+		kfree(data);
+	return ret;
+}
+
+static long sdxi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct sdxi_file *sf = file->private_data;
+	struct sdxi_dev *sdxi;
+	long ret;
+
+	if (!sf || !sf->process)
+		return -EINVAL;
+	sdxi = sf->process->sdxi;
+
+	down_read(&sdxi->state_lock);
+	ret = sdxi->online ? sdxi_ioctl_live(file, cmd, arg) : -ENODEV;
+	up_read(&sdxi->state_lock);
+	return ret;
 }
 
 static int sdxi_open(struct inode *inode, struct file *file)
@@ -77,8 +480,8 @@ static const struct file_operations sdxi_fops = {
 	.owner = THIS_MODULE,
 	.open = sdxi_open,
 	.release = sdxi_release,
-	.unlocked_ioctl = sdxi_ioctl_unsupported,
-	.compat_ioctl = sdxi_ioctl_unsupported,
+	.unlocked_ioctl = sdxi_ioctl,
+	.compat_ioctl = compat_ptr_ioctl,
 };
 
 int sdxi_chardev_init(void)
diff --git a/drivers/dma/sdxi/ioctl.h b/drivers/dma/sdxi/ioctl.h
index 8677f3e62e93..a6c53758c8b9 100644
--- a/drivers/dma/sdxi/ioctl.h
+++ b/drivers/dma/sdxi/ioctl.h
@@ -42,7 +42,6 @@ struct sdxi_ioctl_desc {
 	unsigned int file_flags;
 	enum sdxi_ioctl_access_class access;
 	sdxi_ioctl_t *func;
-	const char *name;
 };
 
 #define SDXI_IOCTL_F_DEVICE	BIT(0)
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 98d2baffbbdf..03a85a1575e7 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -96,6 +96,7 @@ struct sdxi_dev {
 	dma_addr_t L1_dma[SDXI_L2_TABLE_ENTRIES];
 	u16 L1_users[SDXI_L2_TABLE_ENTRIES];
 	struct mutex cxt_lock;		/* context table serialization */
+	struct mutex reg_lock;		/* MMIO register updates */
 
 	/* RKey table */
 	struct mutex rkey_lock;
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 18/44] dmaengine: sdxi: implement userspace context file operations
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (16 preceding siblings ...)
  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
  2026-11-19 20:22 ` [PATCH v1 19/44] dmaengine: sdxi: validate and copy key ioctl payloads Wei Huang
                   ` (25 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

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


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 19/44] dmaengine: sdxi: validate and copy key ioctl payloads
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (17 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 18/44] dmaengine: sdxi: implement userspace context file operations Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 20/44] dmaengine: sdxi: implement RKey management ioctls Wei Huang
                   ` (24 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Size variable-length key bitmap and entry payloads from their fixed
UAPI headers, validate table bounds before allocation, and copy only
the bytes described by the shared AKey/RKey ABI. Key handlers, which
will be introduced later, remain unsupported in this patch.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/ioctl.c | 161 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 150 insertions(+), 11 deletions(-)

diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index 70dcde916c82..682b0b074f38 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -615,17 +615,85 @@ static bool sdxi_ioctl_has_trailing_payload(unsigned int cmd)
 	       cmd == SDXI_GET_AKEYS || cmd == SDXI_SET_AKEYS;
 }
 
+static bool sdxi_key_list_cmd(unsigned int cmd)
+{
+	return cmd == SDXI_GET_RKEY_LIST || cmd == SDXI_GET_AKEY_LIST;
+}
+
+static bool sdxi_key_entries_cmd(unsigned int cmd)
+{
+	return cmd == SDXI_GET_RKEYS || cmd == SDXI_SET_RKEYS ||
+	       cmd == SDXI_GET_AKEYS || cmd == SDXI_SET_AKEYS;
+}
+
+static u32 sdxi_key_table_entries(unsigned int cmd, struct sdxi_file *sf)
+{
+	if (cmd == SDXI_GET_RKEY_LIST || cmd == SDXI_GET_RKEYS ||
+	    cmd == SDXI_SET_RKEYS)
+		return sf->process->sdxi->rkey_entries;
+
+	if (!sf->cxt || !sf->cxt->akey_table)
+		return 0;
+	return ARRAY_SIZE(sf->cxt->akey_table->entry);
+}
+
+static int sdxi_key_ioctl_sizes(unsigned int cmd, struct sdxi_file *sf,
+				const void *header, size_t header_size,
+				size_t *copyin_size, size_t *copyout_size,
+				size_t *allocation_size)
+{
+	size_t payload_size;
+	u32 nentries;
+	u32 nkeys;
+
+	nentries = sdxi_key_table_entries(cmd, sf);
+
+	if (sdxi_key_list_cmd(cmd)) {
+		const struct sdxi_key_list_args *args = header;
+		size_t required = DIV_ROUND_UP(nentries, 8);
+
+		*copyin_size = header_size;
+		*copyout_size = header_size + min_t(size_t, args->buf_size,
+						    required);
+		*allocation_size = *copyout_size;
+		return 0;
+	}
+
+	if (cmd == SDXI_GET_RKEYS || cmd == SDXI_GET_AKEYS)
+		nkeys = ((const struct sdxi_get_keys_args *)header)->nkeys;
+	else
+		nkeys = ((const struct sdxi_set_keys_args *)header)->nkeys;
+
+	if (nkeys > min_t(u32, nentries, 4096))
+		return -EINVAL;
+	if (check_mul_overflow((size_t)nkeys, sizeof(struct sdxi_key_entry),
+			       &payload_size) ||
+	    check_add_overflow(header_size, payload_size, allocation_size))
+		return -EOVERFLOW;
+
+	*copyin_size = *allocation_size;
+	if (cmd == SDXI_GET_RKEYS || cmd == SDXI_GET_AKEYS)
+		*copyout_size = *allocation_size;
+	else
+		*copyout_size = header_size;
+	return 0;
+}
+
 static long sdxi_ioctl_live(struct file *file, unsigned int cmd,
 			    unsigned long arg)
 {
 	const struct sdxi_ioctl_desc *desc;
 	struct sdxi_file *sf = file->private_data;
+	size_t copyout_size;
+	size_t copyin_size;
 	unsigned int canonical_size;
 	unsigned int user_size;
-	unsigned int size;
+	size_t size;
 	unsigned int nr;
 	char stack_data[128];
 	void *data = stack_data;
+	bool header_copied = false;
+	bool variable;
 	bool allowed;
 	int ret;
 
@@ -666,32 +734,69 @@ static long sdxi_ioctl_live(struct file *file, unsigned int cmd,
 
 	canonical_size = _IOC_SIZE(desc->cmd);
 	user_size = _IOC_SIZE(cmd);
-	size = max(canonical_size, user_size);
-	if (size > PAGE_SIZE)
+	copyin_size = (_IOC_DIR(cmd) & _IOC_WRITE) ? user_size : 0;
+	copyout_size = (_IOC_DIR(cmd) & _IOC_READ) ? user_size : 0;
+	size = max_t(size_t, canonical_size, user_size);
+
+	variable = sdxi_key_list_cmd(desc->cmd) ||
+		   sdxi_key_entries_cmd(desc->cmd);
+	if (variable) {
+		if (user_size < canonical_size)
+			return -EINVAL;
+		if (canonical_size > sizeof(stack_data))
+			return -E2BIG;
+		if (copy_from_user(stack_data, (void __user *)arg,
+				   canonical_size))
+			return -EFAULT;
+		header_copied = true;
+		ret = sdxi_key_ioctl_sizes(desc->cmd, sf, stack_data,
+					   canonical_size, &copyin_size,
+					   &copyout_size, &size);
+		if (ret)
+			return ret;
+	}
+
+	if (size > SZ_1M)
 		return -E2BIG;
 
 	if (size > sizeof(stack_data)) {
 		data = kzalloc(size, GFP_KERNEL);
 		if (!data)
 			return -ENOMEM;
-	} else {
+	} else if (!header_copied) {
 		memset(data, 0, size);
+	} else if (size > canonical_size) {
+		memset(data + canonical_size, 0, size - canonical_size);
 	}
 
-	if ((_IOC_DIR(cmd) & _IOC_WRITE) &&
-	    copy_from_user(data, (void __user *)arg, user_size)) {
-		ret = -EFAULT;
-		goto out;
+	if (_IOC_DIR(cmd) & _IOC_WRITE) {
+		if (header_copied) {
+			if (data != stack_data)
+				memcpy(data, stack_data, canonical_size);
+			if (copyin_size > canonical_size &&
+			    copy_from_user(data + canonical_size,
+					   (void __user *)(arg + canonical_size),
+					   copyin_size - canonical_size)) {
+				ret = -EFAULT;
+				goto out;
+			}
+		} else if (copy_from_user(data, (void __user *)arg,
+					  copyin_size)) {
+			ret = -EFAULT;
+			goto out;
+		}
 	}
 
 	ret = desc->func(file, sf, data);
 
+	if (ret == -ENOSPC && sdxi_key_list_cmd(desc->cmd))
+		copyout_size = canonical_size;
 	if ((_IOC_DIR(cmd) & _IOC_READ) && user_size > canonical_size &&
 	    !sdxi_ioctl_has_trailing_payload(desc->cmd))
 		memset((u8 *)data + canonical_size, 0,
 		       user_size - canonical_size);
 	if ((_IOC_DIR(cmd) & _IOC_READ) &&
-	    copy_to_user((void __user *)arg, data, user_size))
+	    copy_to_user((void __user *)arg, data, copyout_size))
 		ret = -EFAULT;
 
 out:
@@ -716,7 +821,7 @@ 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)
+static int sdxi_mmap_live(struct file *file, struct vm_area_struct *vma)
 {
 	struct sdxi_file *sf = file->private_data;
 	struct sdxi_cxt *cxt;
@@ -781,7 +886,24 @@ static int sdxi_mmap(struct file *file, struct vm_area_struct *vma)
 	return ret;
 }
 
-static __poll_t sdxi_cxt_poll(struct file *file, poll_table *wait)
+static int sdxi_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct sdxi_file *sf = file->private_data;
+	struct sdxi_dev *sdxi;
+	int ret;
+
+	if (!sf || !sf->process)
+		return -EINVAL;
+	sdxi = sf->process->sdxi;
+
+	if (!down_read_trylock(&sdxi->state_lock))
+		return -EAGAIN;
+	ret = sdxi->online ? sdxi_mmap_live(file, vma) : -ENODEV;
+	up_read(&sdxi->state_lock);
+	return ret;
+}
+
+static __poll_t sdxi_cxt_poll_live(struct file *file, poll_table *wait)
 {
 	struct sdxi_file *sf = file->private_data;
 	struct sdxi_cxt *cxt;
@@ -805,6 +927,23 @@ static __poll_t sdxi_cxt_poll(struct file *file, poll_table *wait)
 	return mask;
 }
 
+static __poll_t sdxi_cxt_poll(struct file *file, poll_table *wait)
+{
+	struct sdxi_file *sf = file->private_data;
+	struct sdxi_dev *sdxi;
+	__poll_t mask;
+
+	if (!sf || !sf->process)
+		return EPOLLERR | EPOLLHUP;
+	sdxi = sf->process->sdxi;
+
+	down_read(&sdxi->state_lock);
+	mask = sdxi->online ? sdxi_cxt_poll_live(file, wait) :
+			      EPOLLERR | EPOLLHUP;
+	up_read(&sdxi->state_lock);
+	return mask;
+}
+
 static int sdxi_open(struct inode *inode, struct file *file)
 {
 	struct sdxi_dev *sdxi;
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 20/44] dmaengine: sdxi: implement RKey management ioctls
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (18 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 19/44] dmaengine: sdxi: validate and copy key ioctl payloads Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 21/44] dmaengine: sdxi: implement AKey " Wei Huang
                   ` (23 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Implement admin-only RKey bitmap, read, and write operations over
the shared key-table ABI. Validate complete batches before modifying
memory, publish valid bits last, and issue RKey update plus sync
before reporting commit state.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/ioctl.c |   8 +-
 drivers/dma/sdxi/keys.c  | 270 +++++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/keys.h  |   6 +
 3 files changed, 281 insertions(+), 3 deletions(-)

diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index 682b0b074f38..4f4b900d2542 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -31,6 +31,7 @@
 #include "context.h"
 #include "intr.h"
 #include "ioctl.h"
+#include "keys.h"
 #include "mmio.h"
 #include "process.h"
 #include "sdxi.h"
@@ -95,6 +96,7 @@ static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
 		.caps = {
 			SDXI_UAPI_CAP_DEV_INFO |
 			SDXI_UAPI_CAP_DEV_ATTR_MGMT |
+			SDXI_UAPI_CAP_RKEY_MGMT |
 			SDXI_UAPI_CAP_CXT_CREATE |
 			SDXI_UAPI_CAP_CXT_EVENTFD,
 		},
@@ -569,11 +571,11 @@ static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
 		       SDXI_IOCTL_A_PUBLIC_DEVICE),
 	SDXI_IOCTL_DEF(0x04, SDXI_SET_DEV_ATTR, sdxi_ioctl_set_dev_attr,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
-	SDXI_IOCTL_DEF(0x05, SDXI_GET_RKEY_LIST, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x05, SDXI_GET_RKEY_LIST, sdxi_ioctl_get_rkey_list,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
-	SDXI_IOCTL_DEF(0x06, SDXI_GET_RKEYS, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x06, SDXI_GET_RKEYS, sdxi_ioctl_get_rkeys,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
-	SDXI_IOCTL_DEF(0x07, SDXI_SET_RKEYS, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x07, SDXI_SET_RKEYS, sdxi_ioctl_set_rkeys,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_ADMIN_DEVICE),
 	SDXI_IOCTL_DEF(0x08, SDXI_CREATE_CXT, sdxi_ioctl_create_cxt,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
diff --git a/drivers/dma/sdxi/keys.c b/drivers/dma/sdxi/keys.c
index c2f1db0d1594..75a2855ed13d 100644
--- a/drivers/dma/sdxi/keys.c
+++ b/drivers/dma/sdxi/keys.c
@@ -7,15 +7,162 @@
 
 #include <linux/bitfield.h>
 #include <linux/dma-mapping.h>
+#include <linux/kernel.h>
 #include <linux/log2.h>
 #include <linux/sizes.h>
 #include <linux/string.h>
+#include <uapi/linux/sdxi.h>
 
+#include "admin.h"
 #include "hw.h"
+#include "ioctl.h"
 #include "keys.h"
 #include "mmio.h"
 #include "sdxi.h"
 
+#define SDXI_KEY_DEFAULT_TIMEOUT_MS	5000U
+
+static u32 sdxi_key_bitmap_bytes(u32 entries)
+{
+	return DIV_ROUND_UP(entries, 8);
+}
+
+static bool sdxi_key_entry_valid(const void *entry)
+{
+	__le16 intr_num;
+
+	memcpy(&intr_num, entry, sizeof(intr_num));
+	return le16_to_cpu(intr_num) & BIT(0);
+}
+
+static u32 sdxi_key_count_valid(const void *table, u32 entries, size_t stride)
+{
+	u32 count = 0;
+	u32 i;
+
+	for (i = 0; i < entries; i++)
+		if (sdxi_key_entry_valid((const u8 *)table + i * stride))
+			count++;
+
+	return count;
+}
+
+static u32 sdxi_key_build_bitmap(u8 *bitmap, const void *table, u32 entries,
+				 size_t stride)
+{
+	u32 bytes = sdxi_key_bitmap_bytes(entries);
+	u32 count = 0;
+	u32 i;
+
+	memset(bitmap, 0, bytes);
+	for (i = 0; i < entries; i++) {
+		if (!sdxi_key_entry_valid((const u8 *)table + i * stride))
+			continue;
+		bitmap[i / 8] |= BIT(i % 8);
+		count++;
+	}
+
+	return count;
+}
+
+static int sdxi_key_args_check(u32 argsz, size_t minimum, u32 flags,
+			       const u32 *reserved, size_t reserved_words)
+{
+	size_t i;
+
+	if (argsz < minimum || flags)
+		return -EINVAL;
+	for (i = 0; i < reserved_words; i++)
+		if (reserved[i])
+			return -EINVAL;
+
+	return 0;
+}
+
+static int sdxi_key_list_fill(struct sdxi_key_list_args *args,
+			      const void *table, u32 entries, size_t stride)
+{
+	u32 available = args->buf_size;
+	u32 required = sdxi_key_bitmap_bytes(entries);
+
+	args->argsz = sizeof(*args);
+	args->buf_size = required;
+	args->nentries = entries;
+	args->nvalid = sdxi_key_count_valid(table, entries, stride);
+
+	if (available < required)
+		return -ENOSPC;
+
+	args->nvalid = sdxi_key_build_bitmap(args->data, table, entries,
+					     stride);
+	return 0;
+}
+
+static int sdxi_key_check_indices(const struct sdxi_key_entry *keys,
+				  u32 nkeys, u32 entries,
+				  u16 *first, u16 *last)
+{
+	u16 minimum = U16_MAX;
+	u16 maximum = 0;
+	u32 i;
+
+	for (i = 0; i < nkeys; i++) {
+		if (keys[i].rsvd0 || keys[i].index >= entries)
+			return -EINVAL;
+		minimum = min_t(u16, minimum, keys[i].index);
+		maximum = max_t(u16, maximum, keys[i].index);
+	}
+
+	if (first)
+		*first = minimum;
+	if (last)
+		*last = maximum;
+	return 0;
+}
+
+static void sdxi_set_keys_init(struct sdxi_set_keys_args *args)
+{
+	args->cst_flags = 0;
+	args->commit_state = SDXI_KEY_COMMIT_NONE;
+	args->first_failed_index = 0;
+}
+
+static unsigned int
+sdxi_set_keys_timeout(const struct sdxi_set_keys_args *args)
+{
+	return args->timeout_ms ?: SDXI_KEY_DEFAULT_TIMEOUT_MS;
+}
+
+static int sdxi_rkey_validate(const struct sdxi_rkey_ent *entry)
+{
+	u16 intr_num = le16_to_cpu(entry->intr_num);
+	u32 pasid = le32_to_cpu(entry->pasid);
+
+	if (intr_num & BIT(15))
+		return -EINVAL;
+	if (pasid & ~(SDXI_RKEY_ENT_PASID | SDXI_RKEY_ENT_PR |
+		      SDXI_RKEY_ENT_PH))
+		return -EINVAL;
+	if (memchr_inv(entry->rsvd_0, 0, sizeof(entry->rsvd_0)))
+		return -EINVAL;
+
+	return 0;
+}
+
+static void sdxi_rkey_store(struct sdxi_rkey_ent *destination,
+			    const struct sdxi_rkey_ent *source)
+{
+	struct sdxi_rkey_ent entry = *source;
+	u16 intr_num = le16_to_cpu(source->intr_num);
+
+	entry.intr_num = cpu_to_le16(intr_num & ~SDXI_RKEY_ENT_VL);
+	WRITE_ONCE(destination->intr_num, 0);
+	dma_wmb();
+	memcpy(destination, &entry, sizeof(entry));
+	dma_wmb();
+	WRITE_ONCE(destination->intr_num, source->intr_num);
+}
+
 int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1)
 {
 	size_t table_size;
@@ -57,3 +204,126 @@ void sdxi_keys_exit(struct sdxi_dev *sdxi)
 	sdxi->rkey_order = 0;
 	sdxi->rkey_cap = false;
 }
+
+int sdxi_ioctl_get_rkey_list(struct file *file, struct sdxi_file *sf,
+			     void *data)
+{
+	struct sdxi_key_list_args *args = data;
+	struct sdxi_dev *sdxi = sf->process->sdxi;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  &args->rsvd0, 1);
+	if (ret)
+		return ret;
+	if (!sdxi->rkey_cap)
+		return -EOPNOTSUPP;
+	if (!sdxi->rkey_table)
+		return -ENODEV;
+
+	mutex_lock(&sdxi->rkey_lock);
+	ret = sdxi_key_list_fill(args, sdxi->rkey_table,
+				 sdxi->rkey_entries,
+				 sizeof(*sdxi->rkey_table));
+	mutex_unlock(&sdxi->rkey_lock);
+	return ret;
+}
+
+int sdxi_ioctl_get_rkeys(struct file *file, struct sdxi_file *sf, void *data)
+{
+	struct sdxi_get_keys_args *args = data;
+	struct sdxi_dev *sdxi = sf->process->sdxi;
+	u32 i;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  &args->rsvd0, 1);
+	if (ret)
+		return ret;
+	if (!sdxi->rkey_cap)
+		return -EOPNOTSUPP;
+	if (!sdxi->rkey_table)
+		return -ENODEV;
+	if (args->nkeys > sdxi->rkey_entries)
+		return -EINVAL;
+
+	ret = sdxi_key_check_indices(args->entries, args->nkeys,
+				     sdxi->rkey_entries, NULL, NULL);
+	if (ret)
+		return ret;
+
+	args->argsz = sizeof(*args);
+	mutex_lock(&sdxi->rkey_lock);
+	for (i = 0; i < args->nkeys; i++)
+		memcpy(args->entries[i].data,
+		       &sdxi->rkey_table[args->entries[i].index],
+		       sizeof(args->entries[i].data));
+	mutex_unlock(&sdxi->rkey_lock);
+
+	return 0;
+}
+
+int sdxi_ioctl_set_rkeys(struct file *file, struct sdxi_file *sf, void *data)
+{
+	struct sdxi_set_keys_args *args = data;
+	struct sdxi_dev *sdxi = sf->process->sdxi;
+	u16 first;
+	u16 last;
+	u32 i;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  args->rsvd0, ARRAY_SIZE(args->rsvd0));
+	if (ret)
+		return ret;
+
+	args->argsz = sizeof(*args);
+	sdxi_set_keys_init(args);
+
+	if (!sdxi->rkey_cap)
+		return -EOPNOTSUPP;
+	if (!sdxi->rkey_table)
+		return -ENODEV;
+	if (!args->nkeys) {
+		args->commit_state = SDXI_KEY_COMMIT_ALL;
+		return 0;
+	}
+	if (args->nkeys > sdxi->rkey_entries)
+		return -EINVAL;
+
+	ret = sdxi_key_check_indices(args->entries, args->nkeys,
+				     sdxi->rkey_entries, &first, &last);
+	if (ret)
+		return ret;
+
+	mutex_lock(&sdxi->rkey_lock);
+	for (i = 0; i < args->nkeys; i++) {
+		struct sdxi_rkey_ent entry;
+
+		memcpy(&entry, args->entries[i].data, sizeof(entry));
+		ret = sdxi_rkey_validate(&entry);
+		if (ret)
+			goto out_unlock;
+	}
+
+	for (i = 0; i < args->nkeys; i++) {
+		struct sdxi_rkey_ent entry;
+
+		memcpy(&entry, args->entries[i].data, sizeof(entry));
+		sdxi_rkey_store(&sdxi->rkey_table[args->entries[i].index],
+				&entry);
+	}
+	dma_wmb();
+
+	ret = sdxi_adm_rkey_update_sync(sdxi, first, last,
+					sdxi_set_keys_timeout(args), NULL);
+	if (ret) {
+		args->commit_state = SDXI_KEY_COMMIT_PARTIAL;
+		goto out_unlock;
+	}
+
+	args->commit_state = SDXI_KEY_COMMIT_ALL;
+out_unlock:
+	mutex_unlock(&sdxi->rkey_lock);
+	return ret;
+}
diff --git a/drivers/dma/sdxi/keys.h b/drivers/dma/sdxi/keys.h
index c479da651526..fed5c20a2cfc 100644
--- a/drivers/dma/sdxi/keys.h
+++ b/drivers/dma/sdxi/keys.h
@@ -11,8 +11,14 @@
 #include <linux/types.h>
 
 struct sdxi_dev;
+struct file;
+struct sdxi_file;
 
 int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1);
 void sdxi_keys_exit(struct sdxi_dev *sdxi);
+int sdxi_ioctl_get_rkey_list(struct file *file, struct sdxi_file *sf,
+			     void *data);
+int sdxi_ioctl_get_rkeys(struct file *file, struct sdxi_file *sf, void *data);
+int sdxi_ioctl_set_rkeys(struct file *file, struct sdxi_file *sf, void *data);
 
 #endif /* DMA_SDXI_KEYS_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 21/44] dmaengine: sdxi: implement AKey management ioctls
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (19 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 20/44] dmaengine: sdxi: implement RKey management ioctls Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 22/44] dmaengine: sdxi: expose operation-group discovery Wei Huang
                   ` (22 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Implement context-scoped AKey bitmap, read, and write operations. For
security reasons, we constraint caller's requests to local
non-privileged PASID and validate interrupt entries against the bound
eventfd vector.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/ioctl.c |   9 +-
 drivers/dma/sdxi/keys.c  | 234 +++++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/keys.h  |   4 +
 3 files changed, 243 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index 4f4b900d2542..f0b298b92430 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -98,7 +98,8 @@ static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
 			SDXI_UAPI_CAP_DEV_ATTR_MGMT |
 			SDXI_UAPI_CAP_RKEY_MGMT |
 			SDXI_UAPI_CAP_CXT_CREATE |
-			SDXI_UAPI_CAP_CXT_EVENTFD,
+			SDXI_UAPI_CAP_CXT_EVENTFD |
+			SDXI_UAPI_CAP_AKEY_MGMT,
 		},
 	};
 
@@ -581,11 +582,11 @@ static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
 	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_DEF(0x0a, SDXI_GET_AKEY_LIST, sdxi_ioctl_get_akey_list,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
-	SDXI_IOCTL_DEF(0x0b, SDXI_GET_AKEYS, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0b, SDXI_GET_AKEYS, sdxi_ioctl_get_akeys,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
-	SDXI_IOCTL_DEF(0x0c, SDXI_SET_AKEYS, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0c, SDXI_SET_AKEYS, sdxi_ioctl_set_akeys,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
 	SDXI_IOCTL_DEF(0x0d, SDXI_ADMIN_DISC, sdxi_ioctl_unsupported,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
diff --git a/drivers/dma/sdxi/keys.c b/drivers/dma/sdxi/keys.c
index 75a2855ed13d..0d570daefd34 100644
--- a/drivers/dma/sdxi/keys.c
+++ b/drivers/dma/sdxi/keys.c
@@ -6,6 +6,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/capability.h>
 #include <linux/dma-mapping.h>
 #include <linux/kernel.h>
 #include <linux/log2.h>
@@ -14,7 +15,9 @@
 #include <uapi/linux/sdxi.h>
 
 #include "admin.h"
+#include "context.h"
 #include "hw.h"
+#include "intr.h"
 #include "ioctl.h"
 #include "keys.h"
 #include "mmio.h"
@@ -163,6 +166,87 @@ static void sdxi_rkey_store(struct sdxi_rkey_ent *destination,
 	WRITE_ONCE(destination->intr_num, source->intr_num);
 }
 
+static int sdxi_akey_validate(const struct sdxi_akey_ent *entry)
+{
+	u16 intr_num = le16_to_cpu(entry->intr_num);
+	u32 pasid = le32_to_cpu(entry->pasid);
+
+	if (intr_num & BIT(15))
+		return -EINVAL;
+	if (pasid & ~(SDXI_AKEY_ENT_PASID | SDXI_AKEY_ENT_PR |
+		      SDXI_AKEY_ENT_PH))
+		return -EINVAL;
+	if (memchr_inv(entry->rsvd_0, 0, sizeof(entry->rsvd_0)) ||
+	    memchr_inv(entry->rsvd_1, 0, sizeof(entry->rsvd_1)))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int sdxi_akey_validate_user(const struct sdxi_cxt *cxt,
+				   const struct sdxi_akey_ent *entry)
+{
+	u16 intr_num = le16_to_cpu(entry->intr_num);
+	u32 pasid = le32_to_cpu(entry->pasid);
+
+	if (!(intr_num & SDXI_AKEY_ENT_VL)) {
+		if (intr_num || entry->tgt_sfunc || pasid || entry->stag ||
+		    entry->rkey)
+			return -EINVAL;
+		return 0;
+	}
+
+	if (entry->tgt_sfunc || !(intr_num & SDXI_AKEY_ENT_PV) ||
+	    intr_num & SDXI_AKEY_ENT_STE)
+		return -EPERM;
+	if (FIELD_GET(SDXI_AKEY_ENT_PASID, pasid) != cxt->pasid ||
+	    pasid & (SDXI_AKEY_ENT_PR | SDXI_AKEY_ENT_PH))
+		return -EPERM;
+	if (entry->stag || entry->rkey)
+		return -EPERM;
+	if (!(intr_num & SDXI_AKEY_ENT_IV) &&
+	    FIELD_GET(SDXI_AKEY_ENT_INTR_NUM, intr_num))
+		return -EINVAL;
+
+	return 0;
+}
+
+static bool sdxi_akey_driver_owned(const struct sdxi_cxt *cxt, u32 index)
+{
+	u16 intr_akey = READ_ONCE(cxt->intr_akey);
+
+	return index < 2 || (intr_akey && index == intr_akey);
+}
+
+static int sdxi_akey_validate_interrupt(const struct sdxi_cxt *cxt,
+					const struct sdxi_akey_ent *entry)
+{
+	u16 intr_num = le16_to_cpu(entry->intr_num);
+
+	if (!(intr_num & SDXI_AKEY_ENT_IV))
+		return 0;
+	if (!sdxi_cxt_has_intr_fd(cxt))
+		return -ENOTCONN;
+	if (FIELD_GET(SDXI_AKEY_ENT_INTR_NUM, intr_num) != cxt->intr_vector)
+		return -EINVAL;
+
+	return 0;
+}
+
+static void sdxi_akey_store(struct sdxi_akey_ent *destination,
+			    const struct sdxi_akey_ent *source)
+{
+	struct sdxi_akey_ent entry = *source;
+	u16 intr_num = le16_to_cpu(source->intr_num);
+
+	entry.intr_num = cpu_to_le16(intr_num & ~SDXI_AKEY_ENT_VL);
+	WRITE_ONCE(destination->intr_num, 0);
+	dma_wmb();
+	memcpy(destination, &entry, sizeof(entry));
+	dma_wmb();
+	WRITE_ONCE(destination->intr_num, source->intr_num);
+}
+
 int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1)
 {
 	size_t table_size;
@@ -327,3 +411,153 @@ int sdxi_ioctl_set_rkeys(struct file *file, struct sdxi_file *sf, void *data)
 	mutex_unlock(&sdxi->rkey_lock);
 	return ret;
 }
+
+int sdxi_ioctl_get_akey_list(struct file *file, struct sdxi_file *sf,
+			     void *data)
+{
+	struct sdxi_key_list_args *args = data;
+	struct sdxi_cxt *cxt = sf->cxt;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  &args->rsvd0, 1);
+	if (ret)
+		return ret;
+	if (!cxt || sdxi_cxt_is_fatal(cxt))
+		return -ENODEV;
+
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+	ret = sdxi_key_list_fill(args, cxt->akey_table,
+				 ARRAY_SIZE(cxt->akey_table->entry),
+				 sizeof(cxt->akey_table->entry[0]));
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	return ret;
+}
+
+int sdxi_ioctl_get_akeys(struct file *file, struct sdxi_file *sf, void *data)
+{
+	struct sdxi_get_keys_args *args = data;
+	struct sdxi_cxt *cxt = sf->cxt;
+	u32 entries;
+	u32 i;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  &args->rsvd0, 1);
+	if (ret)
+		return ret;
+	if (!cxt || sdxi_cxt_is_fatal(cxt))
+		return -ENODEV;
+
+	entries = ARRAY_SIZE(cxt->akey_table->entry);
+	if (args->nkeys > entries)
+		return -EINVAL;
+	ret = sdxi_key_check_indices(args->entries, args->nkeys, entries,
+				     NULL, NULL);
+	if (ret)
+		return ret;
+
+	args->argsz = sizeof(*args);
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+	for (i = 0; i < args->nkeys; i++)
+		memcpy(args->entries[i].data,
+		       &cxt->akey_table->entry[args->entries[i].index],
+		       sizeof(args->entries[i].data));
+	ret = 0;
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	return ret;
+}
+
+int sdxi_ioctl_set_akeys(struct file *file, struct sdxi_file *sf, void *data)
+{
+	struct sdxi_set_keys_args *args = data;
+	struct sdxi_cxt *cxt = sf->cxt;
+	u32 entries;
+	u16 first;
+	u16 last;
+	u32 i;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  args->rsvd0, ARRAY_SIZE(args->rsvd0));
+	if (ret)
+		return ret;
+
+	args->argsz = sizeof(*args);
+	sdxi_set_keys_init(args);
+
+	if (!cxt || sdxi_cxt_is_fatal(cxt))
+		return -ENODEV;
+	entries = ARRAY_SIZE(cxt->akey_table->entry);
+	if (!args->nkeys) {
+		args->commit_state = SDXI_KEY_COMMIT_ALL;
+		return 0;
+	}
+	if (args->nkeys > entries)
+		return -EINVAL;
+
+	ret = sdxi_key_check_indices(args->entries, args->nkeys, entries,
+				     &first, &last);
+	if (ret)
+		return ret;
+
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+
+	for (i = 0; i < args->nkeys; i++) {
+		struct sdxi_akey_ent entry;
+
+		if (sdxi_akey_driver_owned(cxt, args->entries[i].index)) {
+			ret = -EPERM;
+			goto out_unlock;
+		}
+
+		memcpy(&entry, args->entries[i].data, sizeof(entry));
+		ret = sdxi_akey_validate(&entry);
+		if (ret)
+			goto out_unlock;
+		if (!capable(CAP_SYS_ADMIN)) {
+			ret = sdxi_akey_validate_user(cxt, &entry);
+			if (ret)
+				goto out_unlock;
+		}
+		ret = sdxi_akey_validate_interrupt(cxt, &entry);
+		if (ret)
+			goto out_unlock;
+	}
+
+	for (i = 0; i < args->nkeys; i++) {
+		struct sdxi_akey_ent entry;
+
+		memcpy(&entry, args->entries[i].data, sizeof(entry));
+		sdxi_akey_store(&cxt->akey_table->entry[args->entries[i].index],
+				&entry);
+	}
+	dma_wmb();
+
+	ret = sdxi_adm_akey_update_sync(cxt->sdxi, cxt->id, cxt->id,
+					first, last,
+					sdxi_set_keys_timeout(args), NULL);
+	if (ret) {
+		args->commit_state = SDXI_KEY_COMMIT_PARTIAL;
+		goto out_unlock;
+	}
+
+	args->commit_state = SDXI_KEY_COMMIT_ALL;
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	return ret;
+}
diff --git a/drivers/dma/sdxi/keys.h b/drivers/dma/sdxi/keys.h
index fed5c20a2cfc..6e36527b610a 100644
--- a/drivers/dma/sdxi/keys.h
+++ b/drivers/dma/sdxi/keys.h
@@ -20,5 +20,9 @@ int sdxi_ioctl_get_rkey_list(struct file *file, struct sdxi_file *sf,
 			     void *data);
 int sdxi_ioctl_get_rkeys(struct file *file, struct sdxi_file *sf, void *data);
 int sdxi_ioctl_set_rkeys(struct file *file, struct sdxi_file *sf, void *data);
+int sdxi_ioctl_get_akey_list(struct file *file, struct sdxi_file *sf,
+			     void *data);
+int sdxi_ioctl_get_akeys(struct file *file, struct sdxi_file *sf, void *data);
+int sdxi_ioctl_set_akeys(struct file *file, struct sdxi_file *sf, void *data);
 
 #endif /* DMA_SDXI_KEYS_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 22/44] dmaengine: sdxi: expose operation-group discovery
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (20 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 21/44] dmaengine: sdxi: implement AKey " Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:22 ` [PATCH v1 23/44] dmaengine: sdxi: implement typed vendor admin transport Wei Huang
                   ` (21 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Expose read-only SDXI 1.1 discovery on device fds and use discovered
GUIDs to authorize raw opb_000 requests for user contexts. For security
reasons, deny unknown and admin-only groups. It also enables atomic
groups only when PCI requester support is available.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/ioctl.c | 177 ++++++++++++++++++++++++++++++++++++++-
 drivers/dma/sdxi/pci.c   |  12 +++
 drivers/dma/sdxi/sdxi.h  |   1 +
 3 files changed, 186 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index f0b298b92430..80648bed0e87 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -25,6 +25,7 @@
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <linux/uuid.h>
 #include <uapi/linux/sdxi.h>
 
 #include "admin.h"
@@ -99,7 +100,8 @@ static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
 			SDXI_UAPI_CAP_RKEY_MGMT |
 			SDXI_UAPI_CAP_CXT_CREATE |
 			SDXI_UAPI_CAP_CXT_EVENTFD |
-			SDXI_UAPI_CAP_AKEY_MGMT,
+			SDXI_UAPI_CAP_AKEY_MGMT |
+			SDXI_UAPI_CAP_ADMIN_DISC,
 		},
 	};
 
@@ -321,22 +323,134 @@ static bool sdxi_u32_array_zero(const u32 *values, size_t count)
 	return true;
 }
 
+enum sdxi_op_group_policy {
+	SDXI_OP_GROUP_UNKNOWN,
+	SDXI_OP_GROUP_USER,
+	SDXI_OP_GROUP_ADMIN_ONLY,
+};
+
+struct sdxi_op_group_policy_entry {
+	guid_t guid;
+	enum sdxi_op_group_policy policy;
+};
+
+static const struct sdxi_op_group_policy_entry sdxi_op_group_policy[] = {
+	{ GUID_INIT(0x193259fe, 0x150b, 0x4008, 0xa8, 0x3f, 0x6f, 0xaf, 0x59,
+		    0xf5, 0x27, 0x2a), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0xd9bc29a1, 0x91dc, 0x4243, 0x97, 0x51, 0xb6, 0xb0, 0x32,
+		    0x9d, 0xaf, 0xe1), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0x3ac7630e, 0x7009, 0x424b, 0x82, 0x99, 0x20, 0xe2, 0x90,
+		    0x1e, 0xcc, 0x5e), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0x7fb16347, 0x4c3a, 0x4d16, 0x84, 0x42, 0xcb, 0xa5, 0x1e,
+		    0x34, 0x08, 0x80), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0x0af23293, 0x7595, 0x4939, 0xb0, 0x8a, 0x1c, 0x72, 0x93,
+		    0xc2, 0xe9, 0x75), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0x989b9a64, 0x28d9, 0x4294, 0x9c, 0x7b, 0x9a, 0x3f, 0x27,
+		    0xaf, 0xaf, 0x5c), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0x15114db9, 0x4a7e, 0x47ab, 0x97, 0xdd, 0xea, 0x86, 0xed,
+		    0xa1, 0x0b, 0x6e), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0xb3e551a3, 0xf1a9, 0x4c9a, 0x82, 0x52, 0xda, 0xf5, 0xc0,
+		    0xd9, 0x1a, 0x44), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0x2595c4dc, 0x6fac, 0x49d2, 0xa3, 0x31, 0xb1, 0x40, 0x0a,
+		    0x7f, 0x69, 0x0f), SDXI_OP_GROUP_USER },
+	{ GUID_INIT(0xf5b7610b, 0x0dc6, 0x4c03, 0xab, 0x79, 0x65, 0xf1, 0xf4,
+		    0xef, 0xcf, 0x4c), SDXI_OP_GROUP_ADMIN_ONLY },
+};
+
+static enum sdxi_op_group_policy
+sdxi_op_group_lookup(const guid_t *guid)
+{
+	size_t i;
+
+	if (guid_is_null(guid))
+		return SDXI_OP_GROUP_UNKNOWN;
+
+	for (i = 0; i < ARRAY_SIZE(sdxi_op_group_policy); i++)
+		if (guid_equal(guid, &sdxi_op_group_policy[i].guid))
+			return sdxi_op_group_policy[i].policy;
+
+	return SDXI_OP_GROUP_UNKNOWN;
+}
+
+#define SDXI_DISC_TIMEOUT_MS	5000U
+
+static int sdxi_discover_user_op_groups(struct sdxi_dev *sdxi, u32 available,
+					u32 *allowed, u32 *denied)
+{
+	const u16 first = 1;
+	const u16 last = 31;
+	const size_t record_size = sizeof(guid_t);
+	const size_t size = (last - first + 1) * record_size;
+	dma_addr_t dma;
+	u8 *records;
+	u16 index;
+	int ret;
+
+	if (!sdxi_dev_compatible(sdxi, SDXI_VERSION_1_1) ||
+	    !(available & SDXI_OPB_000_DISC_MASK))
+		return 0;
+
+	records = dma_alloc_coherent(sdxi->dev, size, &dma, GFP_KERNEL);
+	if (!records)
+		return -ENOMEM;
+
+	ret = sdxi_adm_disc(sdxi, first, last, dma, SDXI_DISC_TIMEOUT_MS,
+			    NULL);
+	if (ret)
+		goto free_records;
+
+	dma_rmb();
+	for (index = first; index <= last; index++) {
+		enum sdxi_op_group_policy policy;
+		guid_t guid;
+		u32 bit = BIT(index);
+
+		if (!(available & bit))
+			continue;
+
+		import_guid(&guid, records + (index - first) * record_size);
+		policy = sdxi_op_group_lookup(&guid);
+		if (policy == SDXI_OP_GROUP_USER)
+			*allowed |= bit;
+		else if (policy == SDXI_OP_GROUP_ADMIN_ONLY)
+			*denied |= bit;
+	}
+
+free_records:
+	dma_free_coherent(sdxi->dev, size, records, dma);
+	return ret;
+}
+
 static int sdxi_user_op_groups(struct sdxi_dev *sdxi, u32 requested,
 			       u32 *applied)
 {
 	u32 available;
 	u32 allowed;
+	u32 denied = SDXI_OPB_000_ADMIN;
+	bool use_default = !requested;
+	int ret;
 
 	available = FIELD_GET(SDXI_MMIO_CTL2_OPB_000_AVL,
 			      sdxi_read64(sdxi, SDXI_MMIO_CTL2));
 	allowed = available & SDXI_OPB_000_INTR;
+	if (sdxi->atomic_ops_enabled)
+		allowed |= available & SDXI_OPB_000_ATOMIC;
+
+	ret = sdxi_discover_user_op_groups(sdxi, available, &allowed,
+					   &denied);
+	if (ret && !use_default &&
+	    (requested & SDXI_OPB_000_DISC_MASK))
+		return ret;
 
-	if (!requested)
+	if (use_default)
 		requested = allowed;
-	if (requested & SDXI_OPB_000_ADMIN)
+	if (requested & denied)
 		return -EPERM;
 	if (requested & ~available)
 		return -EINVAL;
+	if ((requested & SDXI_OPB_000_ATOMIC) &&
+	    !sdxi->atomic_ops_enabled)
+		return -EOPNOTSUPP;
 	if (requested & ~allowed)
 		return -EOPNOTSUPP;
 
@@ -552,6 +666,61 @@ static int sdxi_ioctl_quiesce_cxt(struct file *file, struct sdxi_file *sf,
 	return ret;
 }
 
+static int sdxi_ioctl_admin_disc(struct file *file, struct sdxi_file *sf,
+				 void *data)
+{
+	struct sdxi_admin_disc_args *args = data;
+	size_t required;
+	dma_addr_t dma;
+	void *buffer;
+	u16 start;
+	u16 end;
+	u32 timeout_ms;
+	int ret;
+
+	if (args->argsz < sizeof(*args) || args->flags ||
+	    !sdxi_u32_array_zero(args->rsvd0, ARRAY_SIZE(args->rsvd0)))
+		return -EINVAL;
+	if (!sdxi_dev_compatible(sf->process->sdxi, SDXI_VERSION_1_1))
+		return -EOPNOTSUPP;
+
+	start = args->index.start;
+	end = args->index.end;
+	if (start > end || end > 2047)
+		return -EINVAL;
+
+	required = (size_t)(end - start + 1) * sizeof(guid_t);
+	args->argsz = sizeof(*args);
+	args->cst_flags = 0;
+	if (args->buf_size < required) {
+		args->buf_size = required;
+		return -ENOSPC;
+	}
+	if (!args->buf_uptr)
+		return -EINVAL;
+
+	buffer = dma_alloc_coherent(sf->process->sdxi->dev, required, &dma,
+				    GFP_KERNEL);
+	if (!buffer)
+		return -ENOMEM;
+
+	timeout_ms = args->timeout_ms ?: SDXI_DISC_TIMEOUT_MS;
+	if (!capable(CAP_SYS_ADMIN))
+		timeout_ms = min(timeout_ms, SDXI_DISC_TIMEOUT_MS);
+	ret = sdxi_adm_disc(sf->process->sdxi, start, end, dma,
+			    timeout_ms, &args->cst_flags);
+	if (!ret) {
+		dma_rmb();
+		if (copy_to_user(u64_to_user_ptr(args->buf_uptr), buffer,
+				 required))
+			ret = -EFAULT;
+	}
+
+	dma_free_coherent(sf->process->sdxi->dev, required, buffer, dma);
+	args->buf_size = required;
+	return ret;
+}
+
 #define SDXI_IOCTL_DEF(_nr, _cmd, _func, _files, _access)	\
 	[_nr] = {						\
 		.cmd = (_cmd),					\
@@ -588,7 +757,7 @@ static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
 	SDXI_IOCTL_DEF(0x0c, SDXI_SET_AKEYS, sdxi_ioctl_set_akeys,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
-	SDXI_IOCTL_DEF(0x0d, SDXI_ADMIN_DISC, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0d, SDXI_ADMIN_DISC, sdxi_ioctl_admin_disc,
 		       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),
diff --git a/drivers/dma/sdxi/pci.c b/drivers/dma/sdxi/pci.c
index 010ed2c6ad74..52bdaeda08ff 100644
--- a/drivers/dma/sdxi/pci.c
+++ b/drivers/dma/sdxi/pci.c
@@ -14,6 +14,7 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/overflow.h>
+#include <linux/pci-ats.h>
 #include <linux/pci.h>
 
 #include "context.h"
@@ -68,6 +69,17 @@ static int sdxi_pci_init(struct sdxi_dev *sdxi)
 	sdxi->has_mailbox = pdev->is_virtfn ||
 		pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
 
+	/*
+	 * PCIe defines AtomicOp Requester Enable as a PF-owned control that
+	 * also applies to its VFs. Configure the physical function so VF
+	 * contexts inherit the transport capability correctly.
+	 */
+	ret = pci_enable_atomic_ops_to_root(pci_physfn(pdev),
+					    PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
+					    PCI_EXP_DEVCAP2_ATOMIC_COMP64);
+	if (!ret)
+		sdxi->atomic_ops_enabled = true;
+
 	/*
 	 * Allocate the minimum required set of vectors plus one for
 	 * each client context supported by the function.
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 0f7a63d6fc4b..55ca853d6d1c 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -135,6 +135,7 @@ struct sdxi_dev {
 	struct cdev cdev;
 	struct device chardev;
 	int minor;
+	bool atomic_ops_enabled;
 	bool online;
 	struct rw_semaphore state_lock;	/* online state and user operations */
 
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 23/44] dmaengine: sdxi: implement typed vendor admin transport
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (21 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 22/44] dmaengine: sdxi: expose operation-group discovery Wei Huang
@ 2026-11-19 20:22 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 24/44] dmaengine: sdxi: expose character device attributes Wei Huang
                   ` (20 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:22 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

SDXI allows vendors to define vendor-specific descriptors. Add a
module-pinned GUID handler registry and a typed vendor ioctl that never
accepts raw descriptors from userspace. It probes the vendor's opcode
through SDXI discovery, delegates authorization to the registered
handler, and keeps execution on the kernel-owned admin context.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Makefile |   3 +-
 drivers/dma/sdxi/ioctl.c  | 107 +++++++++++++++++++++++------
 drivers/dma/sdxi/vendor.c | 138 ++++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/vendor.h |  42 ++++++++++++
 4 files changed, 270 insertions(+), 20 deletions(-)
 create mode 100644 drivers/dma/sdxi/vendor.c
 create mode 100644 drivers/dma/sdxi/vendor.h

diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index a9eaf01df4aa..253398a45c74 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -12,7 +12,8 @@ sdxi-core-y := \
 	ioctl.o       \
 	keys.o        \
 	process.o     \
-	ring.o
+	ring.o        \
+	vendor.o
 
 obj-$(CONFIG_SDXI_PCI) += sdxi-pci.o
 sdxi-pci-y := pci.o
diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index 80648bed0e87..feef322a005e 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -36,6 +36,7 @@
 #include "mmio.h"
 #include "process.h"
 #include "sdxi.h"
+#include "vendor.h"
 
 #define SDXI_MAX_MINORS	256
 
@@ -75,12 +76,6 @@ static int sdxi_file_check_owner(const struct sdxi_process *process)
 	return 0;
 }
 
-static int sdxi_ioctl_unsupported(struct file *file, struct sdxi_file *sf,
-				  void *data)
-{
-	return -EOPNOTSUPP;
-}
-
 static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
 				    void *data)
 {
@@ -101,7 +96,8 @@ static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
 			SDXI_UAPI_CAP_CXT_CREATE |
 			SDXI_UAPI_CAP_CXT_EVENTFD |
 			SDXI_UAPI_CAP_AKEY_MGMT |
-			SDXI_UAPI_CAP_ADMIN_DISC,
+			SDXI_UAPI_CAP_ADMIN_DISC |
+			SDXI_UAPI_CAP_ADMIN_VENDOR,
 		},
 	};
 
@@ -721,6 +717,59 @@ static int sdxi_ioctl_admin_disc(struct file *file, struct sdxi_file *sf,
 	return ret;
 }
 
+static bool
+sdxi_admin_vendor_reserved_zero(const struct sdxi_admin_vendor_args *args)
+{
+	return !args->flags && !args->rsvd0 && !args->rsvd1 &&
+	       sdxi_u32_array_zero(args->rsvd2, ARRAY_SIZE(args->rsvd2));
+}
+
+static unsigned int sdxi_admin_timeout(u32 timeout_ms)
+{
+	return clamp_val(timeout_ms ?: 5000U, 1U, 60000U);
+}
+
+static int sdxi_ioctl_admin_vendor(struct file *file, struct sdxi_file *sf,
+				   void *data)
+{
+	struct sdxi_admin_vendor_args *args = data;
+	const struct sdxi_vendor_handler *handler;
+	struct sdxi_dev *sdxi = sf->process->sdxi;
+	unsigned int timeout_ms;
+	u16 opcode_type;
+	int ret;
+
+	if (args->argsz < sizeof(*args) ||
+	    !sdxi_admin_vendor_reserved_zero(args))
+		return -EINVAL;
+
+	args->argsz = sizeof(*args);
+	args->cst_flags = 0;
+	timeout_ms = sdxi_admin_timeout(args->timeout_ms);
+
+	ret = sdxi_vendor_get(args->guid, &handler);
+	if (ret)
+		return ret;
+
+	ret = sdxi_vendor_find_opcode_type(sdxi, args->guid, timeout_ms,
+					   &opcode_type);
+	if (ret)
+		goto put_handler;
+
+	if (args->op == SDXI_ADMIN_VENDOR_PROBE) {
+		if (args->in_size || args->in_uptr ||
+		    args->out_size || args->out_uptr)
+			ret = -EINVAL;
+		goto put_handler;
+	}
+
+	ret = handler->ops->exec(sdxi, opcode_type, args, timeout_ms);
+
+put_handler:
+	sdxi_vendor_put(handler);
+	return ret;
+}
+
 #define SDXI_IOCTL_DEF(_nr, _cmd, _func, _files, _access)	\
 	[_nr] = {						\
 		.cmd = (_cmd),					\
@@ -759,25 +808,41 @@ static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
 	SDXI_IOCTL_DEF(0x0d, SDXI_ADMIN_DISC, sdxi_ioctl_admin_disc,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
-	SDXI_IOCTL_DEF(0x0e, SDXI_ADMIN_VENDOR, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0e, SDXI_ADMIN_VENDOR, sdxi_ioctl_admin_vendor,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_VENDOR_HANDLER),
 	SDXI_IOCTL_DEF(0x0f, SDXI_QUIESCE_CXT, sdxi_ioctl_quiesce_cxt,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
 };
 
-static int sdxi_ioctl_permit(const struct sdxi_ioctl_desc *desc)
+static int sdxi_ioctl_pre_permit(const struct sdxi_ioctl_desc *desc)
 {
-	switch (desc->access) {
-	case SDXI_IOCTL_A_PUBLIC_DEVICE:
-	case SDXI_IOCTL_A_CONTEXT_OWNER:
-		return 0;
-	case SDXI_IOCTL_A_ADMIN_DEVICE:
+	if (desc->access == SDXI_IOCTL_A_ADMIN_DEVICE)
 		return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
-	case SDXI_IOCTL_A_VENDOR_HANDLER:
-		return -EOPNOTSUPP;
-	}
 
-	return -EINVAL;
+	return 0;
+}
+
+static int sdxi_ioctl_permit(const struct sdxi_ioctl_desc *desc,
+			     struct sdxi_file *sf, void *data)
+{
+	const struct sdxi_vendor_handler *handler;
+	struct sdxi_admin_vendor_args *args;
+	int ret;
+
+	if (desc->access != SDXI_IOCTL_A_VENDOR_HANDLER)
+		return 0;
+
+	args = data;
+	if (!args || args->argsz < sizeof(*args) ||
+	    !sdxi_admin_vendor_reserved_zero(args))
+		return -EINVAL;
+
+	ret = sdxi_vendor_get(args->guid, &handler);
+	if (ret)
+		return ret;
+	ret = handler->ops->authorize(sf->process->sdxi, args);
+	sdxi_vendor_put(handler);
+	return ret;
 }
 
 static bool sdxi_ioctl_has_trailing_payload(unsigned int cmd)
@@ -900,7 +965,7 @@ static long sdxi_ioctl_live(struct file *file, unsigned int cmd,
 	if (!allowed)
 		return -ENOTTY;
 
-	ret = sdxi_ioctl_permit(desc);
+	ret = sdxi_ioctl_pre_permit(desc);
 	if (ret)
 		return ret;
 
@@ -959,6 +1024,10 @@ static long sdxi_ioctl_live(struct file *file, unsigned int cmd,
 		}
 	}
 
+	ret = sdxi_ioctl_permit(desc, sf, data);
+	if (ret)
+		goto out;
+
 	ret = desc->func(file, sf, data);
 
 	if (ret == -ENOSPC && sdxi_key_list_cmd(desc->cmd))
diff --git a/drivers/dma/sdxi/vendor.c b/drivers/dma/sdxi/vendor.c
new file mode 100644
index 000000000000..6fb5967b0052
--- /dev/null
+++ b/drivers/dma/sdxi/vendor.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI vendor operation handler registry.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/dma-mapping.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/string.h>
+
+#include "admin.h"
+#include "sdxi.h"
+#include "vendor.h"
+
+static LIST_HEAD(sdxi_vendor_handlers);
+static DEFINE_MUTEX(sdxi_vendor_lock);
+
+int sdxi_vendor_register(struct sdxi_vendor_handler *handler)
+{
+	struct sdxi_vendor_handler *iter;
+	int ret = 0;
+
+	if (!handler || !handler->name || !handler->guid || !handler->ops ||
+	    !handler->ops->authorize || !handler->ops->exec)
+		return -EINVAL;
+
+	if (!handler->entry.next)
+		INIT_LIST_HEAD(&handler->entry);
+
+	mutex_lock(&sdxi_vendor_lock);
+	if (!list_empty(&handler->entry)) {
+		ret = -EBUSY;
+		goto out_unlock;
+	}
+
+	list_for_each_entry(iter, &sdxi_vendor_handlers, entry) {
+		if (!memcmp(iter->guid, handler->guid, 16)) {
+			ret = -EEXIST;
+			goto out_unlock;
+		}
+	}
+
+	list_add_tail(&handler->entry, &sdxi_vendor_handlers);
+
+out_unlock:
+	mutex_unlock(&sdxi_vendor_lock);
+	return ret;
+}
+EXPORT_SYMBOL_NS_GPL(sdxi_vendor_register, "SDXI");
+
+void sdxi_vendor_unregister(struct sdxi_vendor_handler *handler)
+{
+	if (!handler)
+		return;
+	if (!handler->entry.next)
+		INIT_LIST_HEAD(&handler->entry);
+
+	mutex_lock(&sdxi_vendor_lock);
+	if (!list_empty(&handler->entry))
+		list_del_init(&handler->entry);
+	mutex_unlock(&sdxi_vendor_lock);
+}
+EXPORT_SYMBOL_NS_GPL(sdxi_vendor_unregister, "SDXI");
+
+int sdxi_vendor_get(const u8 guid[16],
+		    const struct sdxi_vendor_handler **handler_out)
+{
+	const struct sdxi_vendor_handler *handler;
+	int ret = -EOPNOTSUPP;
+
+	if (!guid || !handler_out)
+		return -EINVAL;
+
+	mutex_lock(&sdxi_vendor_lock);
+	list_for_each_entry(handler, &sdxi_vendor_handlers, entry) {
+		if (memcmp(handler->guid, guid, 16))
+			continue;
+		if (!try_module_get(handler->owner))
+			break;
+
+		*handler_out = handler;
+		ret = 0;
+		break;
+	}
+	mutex_unlock(&sdxi_vendor_lock);
+
+	return ret;
+}
+
+void sdxi_vendor_put(const struct sdxi_vendor_handler *handler)
+{
+	if (handler)
+		module_put(handler->owner);
+}
+
+int sdxi_vendor_find_opcode_type(struct sdxi_dev *sdxi, const u8 guid[16],
+				 unsigned int timeout_ms, u16 *opcode_type)
+{
+	const u16 first = 1;
+	const u16 last = 2047;
+	const size_t record_size = 16;
+	const size_t size = (last - first + 1) * record_size;
+	dma_addr_t dma;
+	u8 *records;
+	u16 index;
+	int ret;
+
+	if (!sdxi || !guid || !opcode_type)
+		return -EINVAL;
+	if (!sdxi_dev_compatible(sdxi, SDXI_VERSION_1_1))
+		return -EOPNOTSUPP;
+
+	records = dma_alloc_coherent(sdxi->dev, size, &dma, GFP_KERNEL);
+	if (!records)
+		return -ENOMEM;
+
+	ret = sdxi_adm_disc(sdxi, first, last, dma, timeout_ms, NULL);
+	if (ret)
+		goto free_records;
+
+	dma_rmb();
+	for (index = first; index <= last; index++) {
+		const u8 *record = records + (index - first) * record_size;
+
+		if (!memcmp(record, guid, record_size)) {
+			*opcode_type = index;
+			ret = 0;
+			goto free_records;
+		}
+	}
+	ret = -EOPNOTSUPP;
+
+free_records:
+	dma_free_coherent(sdxi->dev, size, records, dma);
+	return ret;
+}
diff --git a/drivers/dma/sdxi/vendor.h b/drivers/dma/sdxi/vendor.h
new file mode 100644
index 000000000000..2a4e4582b21b
--- /dev/null
+++ b/drivers/dma/sdxi/vendor.h
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI vendor operation handler registry.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_VENDOR_H
+#define DMA_SDXI_VENDOR_H
+
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/types.h>
+
+struct sdxi_admin_vendor_args;
+struct sdxi_dev;
+
+struct sdxi_vendor_handler_ops {
+	int (*authorize)(struct sdxi_dev *sdxi,
+			 const struct sdxi_admin_vendor_args *args);
+	int (*exec)(struct sdxi_dev *sdxi, u16 opcode_type,
+		    struct sdxi_admin_vendor_args *args,
+		    unsigned int timeout_ms);
+};
+
+struct sdxi_vendor_handler {
+	struct list_head entry;
+	const char *name;
+	const u8 *guid;
+	struct module *owner;
+	const struct sdxi_vendor_handler_ops *ops;
+};
+
+int sdxi_vendor_register(struct sdxi_vendor_handler *handler);
+void sdxi_vendor_unregister(struct sdxi_vendor_handler *handler);
+int sdxi_vendor_get(const u8 guid[16],
+		    const struct sdxi_vendor_handler **handler);
+void sdxi_vendor_put(const struct sdxi_vendor_handler *handler);
+int sdxi_vendor_find_opcode_type(struct sdxi_dev *sdxi, const u8 guid[16],
+				 unsigned int timeout_ms, u16 *opcode_type);
+
+#endif /* DMA_SDXI_VENDOR_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 24/44] dmaengine: sdxi: expose character device attributes
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (22 preceding siblings ...)
  2026-11-19 20:22 ` [PATCH v1 23/44] dmaengine: sdxi: implement typed vendor admin transport Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 25/44] dmaengine: sdxi: add PF and VF device naming Wei Huang
                   ` (19 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add read-only version, context count, function identity, and NUMA
attributes as sysfs to each SDXI character device. Derive context
accounting from live sessions so users can retrieve accurate
information.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Makefile  |  1 +
 drivers/dma/sdxi/context.c |  9 ++++-
 drivers/dma/sdxi/context.h |  1 +
 drivers/dma/sdxi/device.c  |  1 +
 drivers/dma/sdxi/ioctl.c   |  2 ++
 drivers/dma/sdxi/sdxi.h    |  2 ++
 drivers/dma/sdxi/sysfs.c   | 70 ++++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/sysfs.h   | 13 +++++++
 8 files changed, 98 insertions(+), 1 deletion(-)
 create mode 100644 drivers/dma/sdxi/sysfs.c
 create mode 100644 drivers/dma/sdxi/sysfs.h

diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index 253398a45c74..019038cc91bc 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -13,6 +13,7 @@ sdxi-core-y := \
 	keys.o        \
 	process.o     \
 	ring.o        \
+	sysfs.o       \
 	vendor.o
 
 obj-$(CONFIG_SDXI_PCI) += sdxi-pci.o
diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index cdaa0f654429..43ffe132cf2e 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -424,8 +424,11 @@ static int sdxi_publish_cxt(struct sdxi_cxt *cxt,
 	};
 
 	err = configure_L1_entry(ent, &L1_cfg);
-	if (!err)
+	if (!err) {
 		cxt->sdxi->L1_users[l2_idx]++;
+		cxt->sdxi->cxt_count++;
+		cxt->counted = true;
+	}
 
 out_unlock:
 	mutex_unlock(&cxt->sdxi->cxt_lock);
@@ -459,6 +462,10 @@ static void sdxi_rescind_cxt(struct sdxi_cxt *cxt)
 		goto out_unlock;
 
 	sdxi->L1_users[l2_idx]--;
+	if (cxt->counted) {
+		sdxi->cxt_count--;
+		cxt->counted = false;
+	}
 
 out_unlock:
 	mutex_unlock(&sdxi->cxt_lock);
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index c245c7ee7841..22a33291020f 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -65,6 +65,7 @@ struct sdxi_cxt {
 	bool registered;
 	bool published;
 	bool update_pending;
+	bool counted;
 	bool started;
 
 	struct sdxi_cxt_ctl *cxt_ctl;
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index 7ebecae3c541..c6b690c39b05 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -387,6 +387,7 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 
 	/* Read capabilities and features. */
 	cap0 = sdxi_read64(sdxi, SDXI_MMIO_CAP0);
+	sdxi->sfunc = FIELD_GET(SDXI_MMIO_CAP0_SFUNC, cap0);
 	sdxi->db_stride = SZ_4K;
 	sdxi->db_stride *= 1U << FIELD_GET(SDXI_MMIO_CAP0_DB_STRIDE, cap0);
 	if (sdxi->db_stride > sdxi->db_size)
diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index feef322a005e..dbd6a916392d 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -36,6 +36,7 @@
 #include "mmio.h"
 #include "process.h"
 #include "sdxi.h"
+#include "sysfs.h"
 #include "vendor.h"
 
 #define SDXI_MAX_MINORS	256
@@ -1295,6 +1296,7 @@ int sdxi_chardev_register(struct sdxi_dev *sdxi)
 	sdxi->chardev.devt = devt;
 	sdxi->chardev.class = sdxi_class;
 	sdxi->chardev.parent = sdxi->dev;
+	sdxi->chardev.groups = sdxi_dev_groups;
 	dev_set_drvdata(&sdxi->chardev, sdxi);
 	ret = dev_set_name(&sdxi->chardev, "sdxi%d", minor);
 	if (ret)
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 55ca853d6d1c..062ba4be90ad 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -92,6 +92,7 @@ struct sdxi_dev {
 	struct sdxi_version sdxi_version;
 
 	/* hardware capabilities (from cap0 & cap1) */
+	u16 sfunc;
 	u32 db_stride;			/* doorbell stride in bytes */
 	u64 max_ring_entries;
 	u16 max_cxtid;			/* Maximum context ID allowed. */
@@ -129,6 +130,7 @@ struct sdxi_dev {
 	bool admin_dead;
 	struct xarray client_cxts; /* context id -> (struct sdxi_cxt *) */
 	u32 mmap_gen;
+	unsigned int cxt_count;
 
 	bool has_mailbox;
 
diff --git a/drivers/dma/sdxi/sysfs.c b/drivers/dma/sdxi/sysfs.c
new file mode 100644
index 000000000000..808712952f37
--- /dev/null
+++ b/drivers/dma/sdxi/sysfs.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI character device attributes.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/sysfs.h>
+
+#include "sdxi.h"
+
+static ssize_t version_show(struct device *dev,
+			    struct device_attribute *attr, char *buf)
+{
+	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, "%u.%u\n", sdxi->sdxi_version.major,
+			  sdxi->sdxi_version.minor);
+}
+static DEVICE_ATTR_RO(version);
+
+static ssize_t cxt_count_show(struct device *dev,
+			      struct device_attribute *attr, char *buf)
+{
+	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
+	unsigned int count;
+
+	mutex_lock(&sdxi->cxt_lock);
+	count = sdxi->cxt_count;
+	mutex_unlock(&sdxi->cxt_lock);
+
+	return sysfs_emit(buf, "%u\n", count);
+}
+static DEVICE_ATTR_RO(cxt_count);
+
+static ssize_t sfunc_show(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, "%u\n", sdxi->sfunc);
+}
+static DEVICE_ATTR_RO(sfunc);
+
+static ssize_t numa_node_show(struct device *dev,
+			      struct device_attribute *attr, char *buf)
+{
+	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, "%d\n", dev_to_node(sdxi->dev));
+}
+static DEVICE_ATTR_RO(numa_node);
+
+static struct attribute *sdxi_dev_attrs[] = {
+	&dev_attr_version.attr,
+	&dev_attr_cxt_count.attr,
+	&dev_attr_sfunc.attr,
+	&dev_attr_numa_node.attr,
+	NULL,
+};
+
+static const struct attribute_group sdxi_dev_group = {
+	.attrs = sdxi_dev_attrs,
+};
+
+const struct attribute_group *sdxi_dev_groups[] = {
+	&sdxi_dev_group,
+	NULL,
+};
diff --git a/drivers/dma/sdxi/sysfs.h b/drivers/dma/sdxi/sysfs.h
new file mode 100644
index 000000000000..91e672bb7adc
--- /dev/null
+++ b/drivers/dma/sdxi/sysfs.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI sysfs declarations.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#ifndef DMA_SDXI_SYSFS_H
+#define DMA_SDXI_SYSFS_H
+
+extern const struct attribute_group *sdxi_dev_groups[];
+
+#endif /* DMA_SDXI_SYSFS_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 25/44] dmaengine: sdxi: add PF and VF device naming
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (23 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 24/44] dmaengine: sdxi: expose character device attributes Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 26/44] dmaengine: sdxi: add SDXI trace events Wei Huang
                   ` (18 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Let the PCI transport format character device names after the core
allocates a minor. Physical functions use sdxiN, while SR-IOV virtual
functions inherit the PF minor and use sdxiNvM as device names. Also
defer VF probe until the PF is registered.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/device.c |  1 +
 drivers/dma/sdxi/ioctl.c  | 19 +++++++++++--------
 drivers/dma/sdxi/pci.c    | 29 ++++++++++++++++++++++++++++-
 drivers/dma/sdxi/sdxi.h   | 15 +++++++++++++++
 4 files changed, 55 insertions(+), 9 deletions(-)

diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index c6b690c39b05..cb7056916c3d 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -388,6 +388,7 @@ static int sdxi_fn_activate(struct sdxi_dev *sdxi)
 	/* Read capabilities and features. */
 	cap0 = sdxi_read64(sdxi, SDXI_MMIO_CAP0);
 	sdxi->sfunc = FIELD_GET(SDXI_MMIO_CAP0_SFUNC, cap0);
+	sdxi->is_vf = FIELD_GET(SDXI_MMIO_CAP0_VF, cap0);
 	sdxi->db_stride = SZ_4K;
 	sdxi->db_stride *= 1U << FIELD_GET(SDXI_MMIO_CAP0_DB_STRIDE, cap0);
 	if (sdxi->db_stride > sdxi->db_size)
diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index dbd6a916392d..98bba3a91ca7 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -8,7 +8,6 @@
 #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>
@@ -17,6 +16,7 @@
 #include <linux/fs.h>
 #include <linux/idr.h>
 #include <linux/limits.h>
+#include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/minmax.h>
 #include <linux/module.h>
@@ -1067,7 +1067,6 @@ static int sdxi_mmap_live(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;
@@ -1081,11 +1080,6 @@ static int sdxi_mmap_live(struct file *file, struct vm_area_struct *vma)
 	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;
@@ -1282,6 +1276,7 @@ void sdxi_chardev_exit(void)
 
 int sdxi_chardev_register(struct sdxi_dev *sdxi)
 {
+	char name[32];
 	dev_t devt;
 	int minor;
 	int ret;
@@ -1292,13 +1287,21 @@ int sdxi_chardev_register(struct sdxi_dev *sdxi)
 		return minor;
 
 	sdxi->minor = minor;
+	if (sdxi->bus_ops->chardev_name) {
+		ret = sdxi->bus_ops->chardev_name(sdxi, name, sizeof(name));
+		if (ret < 0)
+			goto free_minor;
+	} else {
+		scnprintf(name, sizeof(name), "sdxi%d", minor);
+	}
+
 	devt = MKDEV(MAJOR(sdxi_devt), minor);
 	sdxi->chardev.devt = devt;
 	sdxi->chardev.class = sdxi_class;
 	sdxi->chardev.parent = sdxi->dev;
 	sdxi->chardev.groups = sdxi_dev_groups;
 	dev_set_drvdata(&sdxi->chardev, sdxi);
-	ret = dev_set_name(&sdxi->chardev, "sdxi%d", minor);
+	ret = dev_set_name(&sdxi->chardev, "%s", name);
 	if (ret)
 		goto free_minor;
 
diff --git a/drivers/dma/sdxi/pci.c b/drivers/dma/sdxi/pci.c
index 52bdaeda08ff..abef318cfd7c 100644
--- a/drivers/dma/sdxi/pci.c
+++ b/drivers/dma/sdxi/pci.c
@@ -11,6 +11,7 @@
 #include <linux/err.h>
 #include <linux/io.h>
 #include <linux/iomap.h>
+#include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/overflow.h>
@@ -26,6 +27,8 @@ enum sdxi_mmio_bars {
 	SDXI_PCI_BAR_DOORBELL = 2,
 };
 
+static struct pci_driver sdxi_driver;
+
 static struct pci_dev *sdxi_to_pci_dev(const struct sdxi_dev *sdxi)
 {
 	return to_pci_dev(sdxi->dev);
@@ -167,6 +170,29 @@ static int sdxi_pci_mmap_doorbell(struct sdxi_dev *sdxi,
 				  sdxi->db_stride, vma->vm_page_prot);
 }
 
+static int sdxi_pci_chardev_name(struct sdxi_dev *sdxi, char *name,
+				 size_t size)
+{
+	struct pci_dev *pdev = sdxi_to_pci_dev(sdxi);
+	struct sdxi_dev *pf_sdxi;
+	int vf_id;
+
+	if (!pdev->is_virtfn)
+		return scnprintf(name, size, "sdxi%d", sdxi->minor);
+	if (!sdxi->is_vf)
+		return -EINVAL;
+
+	pf_sdxi = pci_iov_get_pf_drvdata(pdev, &sdxi_driver);
+	if (IS_ERR_OR_NULL(pf_sdxi) || pf_sdxi->minor < 0)
+		return -EPROBE_DEFER;
+
+	vf_id = pci_iov_vf_id(pdev);
+	if (vf_id < 0)
+		return vf_id;
+
+	return scnprintf(name, size, "sdxi%dv%d", pf_sdxi->minor, vf_id);
+}
+
 static const struct sdxi_bus_ops sdxi_pci_ops = {
 	.owner = THIS_MODULE,
 	.init = sdxi_pci_init,
@@ -174,6 +200,7 @@ static const struct sdxi_bus_ops sdxi_pci_ops = {
 	.get_irq = sdxi_pci_get_irq,
 	.force_quiesce = sdxi_pci_force_quiesce,
 	.mmap_doorbell = sdxi_pci_mmap_doorbell,
+	.chardev_name = sdxi_pci_chardev_name,
 };
 
 static int sdxi_pci_probe(struct pci_dev *pdev,
@@ -184,8 +211,8 @@ static int sdxi_pci_probe(struct pci_dev *pdev,
 
 static void sdxi_pci_remove(struct pci_dev *pdev)
 {
-	sdxi_unregister(&pdev->dev);
 	pci_disable_sriov(pdev);
+	sdxi_unregister(&pdev->dev);
 }
 
 static const struct pci_device_id sdxi_id_table[] = {
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 062ba4be90ad..93b4eb80358c 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -81,6 +81,10 @@ struct sdxi_bus_ops {
 	 */
 	int (*mmap_doorbell)(struct sdxi_dev *sdxi, struct sdxi_cxt *cxt,
 			     struct vm_area_struct *vma);
+	/**
+	 * @chardev_name: Format a bus-specific character device name.
+	 */
+	int (*chardev_name)(struct sdxi_dev *sdxi, char *name, size_t size);
 };
 
 struct sdxi_dev {
@@ -137,6 +141,7 @@ struct sdxi_dev {
 	struct cdev cdev;
 	struct device chardev;
 	int minor;
+	bool is_vf;
 	bool atomic_ops_enabled;
 	bool online;
 	struct rw_semaphore state_lock;	/* online state and user operations */
@@ -144,6 +149,16 @@ struct sdxi_dev {
 	const struct sdxi_bus_ops *bus_ops;
 };
 
+static inline struct device *sdxi_to_dev(const struct sdxi_dev *sdxi)
+{
+	return sdxi->dev;
+}
+
+static inline bool sdxi_fn_reports_vf(const struct sdxi_dev *sdxi)
+{
+	return sdxi->is_vf;
+}
+
 static inline bool sdxi_dev_compatible(const struct sdxi_dev *sdxi,
 				       struct sdxi_version version)
 {
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 26/44] dmaengine: sdxi: add SDXI trace events
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (24 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 25/44] dmaengine: sdxi: add PF and VF device naming Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 27/44] dmaengine: sdxi: support suspend and resume Wei Huang
                   ` (17 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add trace events for context and submission queue creation and
destruction, along with process SVA bind and unbind operations. Insert
the trace points into the corresponding call sites in SDXI driver.

Co-developed-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Makefile  |   1 +
 drivers/dma/sdxi/context.c |  31 ++++++---
 drivers/dma/sdxi/process.c |   6 +-
 drivers/dma/sdxi/trace.c   |   9 +++
 drivers/dma/sdxi/trace.h   | 137 +++++++++++++++++++++++++++++++++++++
 5 files changed, 173 insertions(+), 11 deletions(-)
 create mode 100644 drivers/dma/sdxi/trace.c
 create mode 100644 drivers/dma/sdxi/trace.h

diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index 019038cc91bc..34f62b7366eb 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -14,6 +14,7 @@ sdxi-core-y := \
 	process.o     \
 	ring.o        \
 	sysfs.o       \
+	trace.o       \
 	vendor.o
 
 obj-$(CONFIG_SDXI_PCI) += sdxi-pci.o
diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index 43ffe132cf2e..91c2837bbe82 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -36,6 +36,7 @@
 #include "ring.h"
 #include "sdxi.h"
 #include "submission.h"
+#include "trace.h"
 
 #define DEFAULT_DESC_RING_ENTRIES 1024
 #define SDXI_CXT_QUIESCE_TIMEOUT_MS 5000U
@@ -50,6 +51,16 @@ enum {
 
 static void sdxi_cxt_cleanup_workfn(struct work_struct *work);
 
+static void sdxi_cxt_free_sq(struct sdxi_cxt *cxt)
+{
+	if (!cxt->sq)
+		return;
+
+	trace_sdxi_free_sq(cxt, cxt->sq);
+	sdxi_sq_free(cxt->sdxi, cxt->sq);
+	cxt->sq = NULL;
+}
+
 /*
  * Free context and its resources. @cxt may be partially allocated but
  * must have ->sdxi set.
@@ -70,8 +81,7 @@ static void sdxi_free_cxt(struct sdxi_cxt *cxt)
 				  cxt->akey_table, cxt->akey_table_dma);
 	if (cxt->cst_pool)
 		sdxi_cst_pool_destroy(sdxi, cxt->cst_pool);
-	if (cxt->sq)
-		sdxi_sq_free(sdxi, cxt->sq);
+	sdxi_cxt_free_sq(cxt);
 	ida_destroy(&cxt->akey_ida);
 	mutex_destroy(&cxt->akey_lock);
 	mutex_destroy(&cxt->cleanup_lock);
@@ -205,6 +215,7 @@ static int sdxi_alloc_kernel_submission(struct sdxi_cxt *cxt)
 	cxt->sq = sdxi_sq_alloc(cxt->sdxi, DEFAULT_DESC_RING_ENTRIES);
 	if (!cxt->sq)
 		return -ENOMEM;
+	trace_sdxi_create_sq(cxt, cxt->sq);
 
 	cxt->cst_pool = sdxi_cst_pool_create(cxt->sdxi,
 					     DEFAULT_DESC_RING_ENTRIES);
@@ -434,11 +445,13 @@ static int sdxi_publish_cxt(struct sdxi_cxt *cxt,
 	mutex_unlock(&cxt->sdxi->cxt_lock);
 	if (err) {
 		invalidate_cxt_ctl(cxt->cxt_ctl);
-	} else {
-		cxt->published = true;
-		cxt->update_pending = false;
+		return err;
 	}
-	return err;
+
+	cxt->published = true;
+	cxt->update_pending = false;
+	trace_sdxi_create_cxt(cxt->sdxi, cxt);
+	return 0;
 }
 
 /* Invalidate a context. */
@@ -451,6 +464,7 @@ static void sdxi_rescind_cxt(struct sdxi_cxt *cxt)
 
 	if (!cxt->published)
 		return;
+	trace_sdxi_free_cxt(sdxi, cxt);
 
 	mutex_lock(&sdxi->cxt_lock);
 	L1_table = sdxi->L1_tables[l2_idx];
@@ -575,10 +589,7 @@ static void sdxi_cxt_release_resources(struct sdxi_cxt *cxt)
 		sdxi_cst_pool_destroy(sdxi, cxt->cst_pool);
 		cxt->cst_pool = NULL;
 	}
-	if (cxt->sq) {
-		sdxi_sq_free(sdxi, cxt->sq);
-		cxt->sq = NULL;
-	}
+	sdxi_cxt_free_sq(cxt);
 	kfree(cxt->ring_state);
 	cxt->ring_state = NULL;
 }
diff --git a/drivers/dma/sdxi/process.c b/drivers/dma/sdxi/process.c
index b64f2307e4e3..31847af18e83 100644
--- a/drivers/dma/sdxi/process.c
+++ b/drivers/dma/sdxi/process.c
@@ -14,6 +14,7 @@
 #include "context.h"
 #include "process.h"
 #include "sdxi.h"
+#include "trace.h"
 
 static void sdxi_process_cleanup(struct sdxi_process *process)
 {
@@ -95,8 +96,10 @@ void sdxi_process_unbind_sva(struct sdxi_process *process,
 		mmu_notifier_unregister(&process->notifier, process->mm);
 	}
 
-	if (sva)
+	if (sva) {
 		iommu_sva_unbind_device(sva);
+		trace_sdxi_unbind_process(process->sdxi, pasid);
+	}
 }
 
 static void sdxi_process_free(struct sdxi_process *process)
@@ -157,6 +160,7 @@ int sdxi_process_bind(struct sdxi_process *process)
 
 	process->pasid = pasid;
 	process->sva = sva;
+	trace_sdxi_bind_process(process->sdxi, pasid);
 	return 0;
 
 unbind:
diff --git a/drivers/dma/sdxi/trace.c b/drivers/dma/sdxi/trace.c
new file mode 100644
index 000000000000..8fcde4538c94
--- /dev/null
+++ b/drivers/dma/sdxi/trace.c
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI tracepoints.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#define CREATE_TRACE_POINTS
+#include "trace.h"
diff --git a/drivers/dma/sdxi/trace.h b/drivers/dma/sdxi/trace.h
new file mode 100644
index 000000000000..e39db0e3debd
--- /dev/null
+++ b/drivers/dma/sdxi/trace.h
@@ -0,0 +1,137 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI tracepoints.
+ *
+ * Copyright Advanced Micro Devices, Inc.
+ */
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM sdxi
+
+#if !defined(_TRACE_SDXI_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_SDXI_H
+
+#include <linux/tracepoint.h>
+
+#include "context.h"
+
+TRACE_EVENT(sdxi_create_cxt,
+	    TP_PROTO(struct sdxi_dev *sdxi, struct sdxi_cxt *cxt),
+	    TP_ARGS(sdxi, cxt),
+	    TP_STRUCT__entry(
+		    __field(u16, sfunc)
+		    __field(unsigned int, cxt_id)
+		    __field(void *, cce)
+		    __field(u64, cce_dma_addr)
+		    __field(void *, akey)
+		    __field(u64, akey_dma_addr)
+	    ),
+	    TP_fast_assign(
+		    __entry->sfunc = sdxi->sfunc;
+		    __entry->cxt_id = cxt->id;
+		    __entry->cce = cxt->cxt_ctl;
+		    __entry->cce_dma_addr = cxt->cxt_ctl_dma;
+		    __entry->akey = cxt->akey_table;
+		    __entry->akey_dma_addr = cxt->akey_table_dma;
+	    ),
+	    TP_printk("cxt=%u sfunc=%#x cxt_ctl=%p cxt_ctl_dma=%#llx akey=%p akey_dma=%#llx",
+		      __entry->cxt_id, __entry->sfunc, __entry->cce,
+		      __entry->cce_dma_addr, __entry->akey,
+		      __entry->akey_dma_addr)
+);
+
+TRACE_EVENT(sdxi_free_cxt,
+	    TP_PROTO(struct sdxi_dev *sdxi, struct sdxi_cxt *cxt),
+	    TP_ARGS(sdxi, cxt),
+	    TP_STRUCT__entry(
+		    __field(u16, sfunc)
+		    __field(unsigned int, cxt_id)
+	    ),
+	    TP_fast_assign(
+		    __entry->sfunc = sdxi->sfunc;
+		    __entry->cxt_id = cxt->id;
+	    ),
+	    TP_printk("cxt=%u sfunc=%#x", __entry->cxt_id, __entry->sfunc)
+);
+
+TRACE_EVENT(sdxi_create_sq,
+	    TP_PROTO(struct sdxi_cxt *cxt, struct sdxi_sq *sq),
+	    TP_ARGS(cxt, sq),
+	    TP_STRUCT__entry(
+		    __field(u16, sfunc)
+		    __field(unsigned int, cxt_id)
+		    __field(void *, desc_ring)
+		    __field(u64, desc_ring_addr)
+		    __field(void *, write_index)
+		    __field(u64, write_index_addr)
+		    __field(void *, cxt_status)
+		    __field(u64, cxt_status_addr)
+	    ),
+	    TP_fast_assign(
+		    __entry->sfunc = cxt->sdxi->sfunc;
+		    __entry->cxt_id = cxt->id;
+		    __entry->desc_ring = sq->ring;
+		    __entry->desc_ring_addr = sdxi_sq_ring_dma(sq);
+		    __entry->write_index = &sq->write_index;
+		    __entry->write_index_addr = sdxi_sq_write_index_dma(sq);
+		    __entry->cxt_status = &sq->cxt_sts;
+		    __entry->cxt_status_addr = sdxi_sq_cxt_sts_dma(sq);
+	    ),
+	    TP_printk("cxt=%u sfunc=%#x ring=%p/%#llx widx=%p/%#llx sts=%p/%#llx",
+		      __entry->cxt_id, __entry->sfunc, __entry->desc_ring,
+		      __entry->desc_ring_addr, __entry->write_index,
+		      __entry->write_index_addr, __entry->cxt_status,
+		      __entry->cxt_status_addr)
+);
+
+TRACE_EVENT(sdxi_free_sq,
+	    TP_PROTO(struct sdxi_cxt *cxt, struct sdxi_sq *sq),
+	    TP_ARGS(cxt, sq),
+	    TP_STRUCT__entry(
+		    __field(u16, sfunc)
+		    __field(unsigned int, cxt_id)
+	    ),
+	    TP_fast_assign(
+		    __entry->sfunc = cxt->sdxi->sfunc;
+		    __entry->cxt_id = cxt->id;
+	    ),
+	    TP_printk("cxt=%u sfunc=%#x", __entry->cxt_id, __entry->sfunc)
+);
+
+TRACE_EVENT(sdxi_bind_process,
+	    TP_PROTO(const struct sdxi_dev *sdxi, u32 pasid),
+	    TP_ARGS(sdxi, pasid),
+	    TP_STRUCT__entry(
+		    __string(devname, dev_name(sdxi_to_dev(sdxi)))
+		    __field(u32, pasid)
+	    ),
+	    TP_fast_assign(
+		    __assign_str(devname);
+		    __entry->pasid = pasid;
+	    ),
+	    TP_printk("device=%s pasid=%u", __get_str(devname), __entry->pasid)
+);
+
+TRACE_EVENT(sdxi_unbind_process,
+	    TP_PROTO(const struct sdxi_dev *sdxi, u32 pasid),
+	    TP_ARGS(sdxi, pasid),
+	    TP_STRUCT__entry(
+		    __string(devname, dev_name(sdxi_to_dev(sdxi)))
+		    __field(u32, pasid)
+	    ),
+	    TP_fast_assign(
+		    __assign_str(devname);
+		    __entry->pasid = pasid;
+	    ),
+	    TP_printk("device=%s pasid=%u", __get_str(devname), __entry->pasid)
+);
+
+#endif /* _TRACE_SDXI_H */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH ../../drivers/dma/sdxi
+
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE trace
+
+#include <trace/define_trace.h>
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 27/44] dmaengine: sdxi: support suspend and resume
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (25 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 26/44] dmaengine: sdxi: add SDXI trace events Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 28/44] dmaengine: sdxi: amd: define QoS request formats Wei Huang
                   ` (16 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

During suspend, save function control state, stop the function with
error interrupts disabled. Similarly restore context tables, RKeys,
error logging, the admin context, and previously running client contexts
on resume. Keep PCI responsible for wiring the core PM callbacks.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/context.c |  11 +++
 drivers/dma/sdxi/context.h |   1 +
 drivers/dma/sdxi/device.c  | 186 +++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/error.c   |  56 +++++++++++
 drivers/dma/sdxi/error.h   |   2 +
 drivers/dma/sdxi/keys.c    |  14 +++
 drivers/dma/sdxi/keys.h    |   1 +
 drivers/dma/sdxi/pci.c     |   5 +
 drivers/dma/sdxi/sdxi.h    |   8 ++
 9 files changed, 284 insertions(+)

diff --git a/drivers/dma/sdxi/context.c b/drivers/dma/sdxi/context.c
index 91c2837bbe82..12502021600d 100644
--- a/drivers/dma/sdxi/context.c
+++ b/drivers/dma/sdxi/context.c
@@ -1245,6 +1245,17 @@ int sdxi_cxt_notify_mm_release(struct sdxi_cxt *cxt)
 	return ret;
 }
 
+void sdxi_cxt_resume_cleanup(struct sdxi_cxt *cxt)
+{
+	if (!cxt)
+		return;
+
+	mutex_lock(&cxt->cleanup_lock);
+	if (sdxi_cxt_is_user(cxt) && sdxi_cxt_cleanup_requested(cxt))
+		sdxi_cxt_queue_cleanup_locked(cxt, 0);
+	mutex_unlock(&cxt->cleanup_lock);
+}
+
 void sdxi_cxt_vma_open(struct sdxi_cxt *cxt)
 {
 	if (!cxt)
diff --git a/drivers/dma/sdxi/context.h b/drivers/dma/sdxi/context.h
index 22a33291020f..c3f3353874a0 100644
--- a/drivers/dma/sdxi/context.h
+++ b/drivers/dma/sdxi/context.h
@@ -116,6 +116,7 @@ void sdxi_cxt_kill(struct sdxi_cxt *cxt);
 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_resume_cleanup(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);
diff --git a/drivers/dma/sdxi/device.c b/drivers/dma/sdxi/device.c
index cb7056916c3d..f0e447e7a81b 100644
--- a/drivers/dma/sdxi/device.c
+++ b/drivers/dma/sdxi/device.c
@@ -22,6 +22,7 @@
 #include <linux/time.h>
 #include <linux/xarray.h>
 
+#include "admin.h"
 #include "context.h"
 #include "dma.h"
 #include "error.h"
@@ -526,6 +527,190 @@ static void sdxi_dev_release(struct device *chardev)
 	kfree(sdxi);
 }
 
+static void sdxi_restore_admin_cxt(struct sdxi_dev *sdxi)
+{
+	struct sdxi_cxt *adm = sdxi->admin_cxt;
+	u64 doorbell;
+
+	if (!adm || !adm->sq)
+		return;
+
+	WRITE_ONCE(adm->sq->cxt_sts.state,
+		   FIELD_PREP(SDXI_CXT_STS_STATE, CXTV_RUN));
+	dma_wmb();
+	doorbell = le64_to_cpu(READ_ONCE(adm->sq->write_index));
+	writeq(doorbell, adm->sq->doorbell);
+}
+
+static int sdxi_restart_contexts(struct sdxi_dev *sdxi)
+{
+	struct sdxi_cxt *entry;
+	unsigned long index;
+	int result = 0;
+
+	xa_for_each(&sdxi->client_cxts, index, entry) {
+		struct sdxi_cxt *cxt;
+		int ret;
+
+		if (!entry)
+			continue;
+		cxt = sdxi_cxt_get_by_id(sdxi, index);
+		if (!cxt)
+			continue;
+
+		mutex_lock(&cxt->cleanup_lock);
+		if (cxt->fatal || cxt->handle_closed || cxt->mm_dead ||
+		    cxt->cleanup_stopping ||
+		    cxt->resources_released || !cxt->started) {
+			mutex_unlock(&cxt->cleanup_lock);
+			sdxi_cxt_put(cxt);
+			continue;
+		}
+
+		ret = sdxi_adm_start_cxt(cxt);
+		mutex_unlock(&cxt->cleanup_lock);
+		if (ret) {
+			dev_err(sdxi->dev, "failed to restart context %u: %d\n",
+				cxt->id, ret);
+			sdxi_cxt_kill(cxt);
+			result = ret;
+		}
+		sdxi_cxt_put(cxt);
+	}
+
+	return result;
+}
+
+static void sdxi_resume_cleanup_contexts(struct sdxi_dev *sdxi)
+{
+	struct sdxi_cxt *entry;
+	unsigned long index;
+
+	xa_for_each(&sdxi->client_cxts, index, entry) {
+		struct sdxi_cxt *cxt;
+
+		if (!entry)
+			continue;
+		cxt = sdxi_cxt_get_by_id(sdxi, index);
+		if (!cxt)
+			continue;
+		sdxi_cxt_resume_cleanup(cxt);
+		sdxi_cxt_put(cxt);
+	}
+}
+
+int sdxi_suspend(struct device *dev)
+{
+	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
+	int ret;
+
+	if (!sdxi)
+		return -ENODEV;
+
+	mutex_lock(&sdxi->pm_lock);
+	if (sdxi->pm_suspended) {
+		ret = 0;
+		goto out_unlock;
+	}
+	down_write(&sdxi->state_lock);
+	if (!sdxi->online) {
+		up_write(&sdxi->state_lock);
+		ret = -EIO;
+		goto out_unlock;
+	}
+	sdxi->online = false;
+
+	mutex_lock(&sdxi->reg_lock);
+	sdxi->pm_ctl0 = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
+	sdxi->pm_ctl2 = sdxi_read64(sdxi, SDXI_MMIO_CTL2);
+	mutex_unlock(&sdxi->reg_lock);
+	sdxi->pm_fn_was_active = sdxi_dev_gsv(sdxi) == SDXI_GSV_ACTIVE;
+
+	sdxi_error_suspend(sdxi);
+	ret = sdxi_dev_stop(sdxi);
+	if (ret) {
+		/* Recovery failed; keep users out and fence DMA. */
+		sdxi_dev_force_quiesce(sdxi);
+	} else {
+		sdxi->pm_suspended = true;
+	}
+	up_write(&sdxi->state_lock);
+
+out_unlock:
+	mutex_unlock(&sdxi->pm_lock);
+	return ret;
+}
+EXPORT_SYMBOL_NS_GPL(sdxi_suspend, "SDXI");
+
+int sdxi_resume(struct device *dev)
+{
+	struct sdxi_dev *sdxi = dev_get_drvdata(dev);
+	u64 cxt_l2;
+	u64 ctl0;
+	bool error_resumed = false;
+	bool make_online = false;
+	bool state_locked = false;
+	int ret = 0;
+
+	if (!sdxi)
+		return -ENODEV;
+
+	mutex_lock(&sdxi->pm_lock);
+	if (!sdxi->pm_suspended)
+		goto out_unlock;
+	down_write(&sdxi->state_lock);
+	state_locked = true;
+
+	ctl0 = sdxi->pm_ctl0;
+	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_GSR, &ctl0, SDXI_GSRV_STOP_SF);
+	mutex_lock(&sdxi->reg_lock);
+	sdxi_write64(sdxi, SDXI_MMIO_CTL0, ctl0);
+	mutex_unlock(&sdxi->reg_lock);
+
+	ret = sdxi_dev_stop(sdxi);
+	if (ret)
+		goto resume_failed;
+
+	mutex_lock(&sdxi->reg_lock);
+	sdxi_write64(sdxi, SDXI_MMIO_CTL2, sdxi->pm_ctl2);
+	mutex_unlock(&sdxi->reg_lock);
+	cxt_l2 = FIELD_PREP(SDXI_MMIO_CXT_L2_PTR,
+			    sdxi->L2_dma >> ilog2(SZ_4K));
+	sdxi_write64(sdxi, SDXI_MMIO_CXT_L2, cxt_l2);
+	sdxi_keys_resume(sdxi);
+	sdxi_error_resume(sdxi);
+	error_resumed = true;
+
+	if (sdxi->pm_fn_was_active) {
+		ret = sdxi_dev_start(sdxi);
+		if (ret)
+			goto resume_failed;
+		sdxi_restore_admin_cxt(sdxi);
+		ret = sdxi_restart_contexts(sdxi);
+		if (ret)
+			goto resume_failed;
+	}
+	sdxi->pm_suspended = false;
+	sdxi->online = true;
+	make_online = true;
+	goto out_unlock;
+
+resume_failed:
+	if (error_resumed)
+		sdxi_error_suspend(sdxi);
+	if (sdxi_dev_stop(sdxi))
+		sdxi_dev_force_quiesce(sdxi);
+
+out_unlock:
+	if (state_locked)
+		up_write(&sdxi->state_lock);
+	mutex_unlock(&sdxi->pm_lock);
+	if (make_online)
+		sdxi_resume_cleanup_contexts(sdxi);
+	return ret;
+}
+EXPORT_SYMBOL_NS_GPL(sdxi_resume, "SDXI");
+
 int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 {
 	struct sdxi_dev *sdxi;
@@ -546,6 +731,7 @@ int sdxi_register(struct device *dev, const struct sdxi_bus_ops *ops)
 	mutex_init(&sdxi->reg_lock);
 	mutex_init(&sdxi->admin_lock);
 	mutex_init(&sdxi->rkey_lock);
+	mutex_init(&sdxi->pm_lock);
 	sdxi->error_irq = -1;
 	sdxi->minor = -1;
 	dev_set_drvdata(dev, sdxi);
diff --git a/drivers/dma/sdxi/error.c b/drivers/dma/sdxi/error.c
index d4c1e64dbf2b..ae649985a424 100644
--- a/drivers/dma/sdxi/error.c
+++ b/drivers/dma/sdxi/error.c
@@ -313,3 +313,59 @@ void sdxi_error_exit(struct sdxi_dev *sdxi)
 	sdxi->err_log = NULL;
 	sdxi->err_log_dma = 0;
 }
+
+void sdxi_error_suspend(struct sdxi_dev *sdxi)
+{
+	u64 reg;
+
+	if (!sdxi->err_log)
+		return;
+
+	disable_irq(sdxi->error_irq);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CFG, 0);
+
+	mutex_lock(&sdxi->reg_lock);
+	reg = sdxi_read64(sdxi, SDXI_MMIO_ERR_CTL);
+	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
+
+	reg = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
+	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_ERR_INTR_EN, &reg, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_CTL0, reg);
+	mutex_unlock(&sdxi->reg_lock);
+}
+
+void sdxi_error_resume(struct sdxi_dev *sdxi)
+{
+	unsigned int size_order;
+	u64 reg;
+
+	if (!sdxi->err_log)
+		return;
+
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CFG, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_STS,
+		     SDXI_MMIO_ERR_STS_STS_BIT |
+		     SDXI_MMIO_ERR_STS_OVF_BIT |
+		     SDXI_MMIO_ERR_STS_ERR_BIT);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_WRT, 0);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_RD, 0);
+
+	mutex_lock(&sdxi->reg_lock);
+	reg = sdxi_read64(sdxi, SDXI_MMIO_ERR_CTL);
+	FIELD_MODIFY(SDXI_MMIO_ERR_CTL_EN, &reg, 1);
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CTL, reg);
+
+	reg = sdxi_read64(sdxi, SDXI_MMIO_CTL0);
+	FIELD_MODIFY(SDXI_MMIO_CTL0_FN_ERR_INTR_EN, &reg, 1);
+	sdxi_write64(sdxi, SDXI_MMIO_CTL0, reg);
+	mutex_unlock(&sdxi->reg_lock);
+
+	size_order = ilog2(SDXI_ERRLOG_SIZE) - ilog2(SZ_4K);
+	reg = FIELD_PREP(SDXI_MMIO_ERR_CFG_PTR,
+			 sdxi->err_log_dma >> ilog2(SZ_4K)) |
+	      FIELD_PREP(SDXI_MMIO_ERR_CFG_SZ, size_order) |
+	      SDXI_MMIO_ERR_CFG_EN;
+	sdxi_write64(sdxi, SDXI_MMIO_ERR_CFG, reg);
+	enable_irq(sdxi->error_irq);
+}
diff --git a/drivers/dma/sdxi/error.h b/drivers/dma/sdxi/error.h
index 756a4fe45158..866117559aa3 100644
--- a/drivers/dma/sdxi/error.h
+++ b/drivers/dma/sdxi/error.h
@@ -12,5 +12,7 @@ struct sdxi_dev;
 
 int sdxi_error_init(struct sdxi_dev *sdxi);
 void sdxi_error_exit(struct sdxi_dev *sdxi);
+void sdxi_error_suspend(struct sdxi_dev *sdxi);
+void sdxi_error_resume(struct sdxi_dev *sdxi);
 
 #endif /* DMA_SDXI_ERROR_H */
diff --git a/drivers/dma/sdxi/keys.c b/drivers/dma/sdxi/keys.c
index 0d570daefd34..8397d575c06a 100644
--- a/drivers/dma/sdxi/keys.c
+++ b/drivers/dma/sdxi/keys.c
@@ -289,6 +289,20 @@ void sdxi_keys_exit(struct sdxi_dev *sdxi)
 	sdxi->rkey_cap = false;
 }
 
+void sdxi_keys_resume(struct sdxi_dev *sdxi)
+{
+	u64 reg;
+
+	if (!sdxi->rkey_cap || !sdxi->rkey_table)
+		return;
+
+	reg = FIELD_PREP(SDXI_MMIO_RKEY_PTR,
+			 sdxi->rkey_table_dma >> ilog2(SZ_4K)) |
+	      FIELD_PREP(SDXI_MMIO_RKEY_SZ, sdxi->rkey_order) |
+	      SDXI_MMIO_RKEY_EN;
+	sdxi_write64(sdxi, SDXI_MMIO_RKEY, reg);
+}
+
 int sdxi_ioctl_get_rkey_list(struct file *file, struct sdxi_file *sf,
 			     void *data)
 {
diff --git a/drivers/dma/sdxi/keys.h b/drivers/dma/sdxi/keys.h
index 6e36527b610a..775dd381e3f3 100644
--- a/drivers/dma/sdxi/keys.h
+++ b/drivers/dma/sdxi/keys.h
@@ -16,6 +16,7 @@ struct sdxi_file;
 
 int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1);
 void sdxi_keys_exit(struct sdxi_dev *sdxi);
+void sdxi_keys_resume(struct sdxi_dev *sdxi);
 int sdxi_ioctl_get_rkey_list(struct file *file, struct sdxi_file *sf,
 			     void *data);
 int sdxi_ioctl_get_rkeys(struct file *file, struct sdxi_file *sf, void *data);
diff --git a/drivers/dma/sdxi/pci.c b/drivers/dma/sdxi/pci.c
index abef318cfd7c..fa080dc004ab 100644
--- a/drivers/dma/sdxi/pci.c
+++ b/drivers/dma/sdxi/pci.c
@@ -221,12 +221,17 @@ static const struct pci_device_id sdxi_id_table[] = {
 };
 MODULE_DEVICE_TABLE(pci, sdxi_id_table);
 
+static const struct dev_pm_ops sdxi_pm_ops = {
+	SYSTEM_SLEEP_PM_OPS(sdxi_suspend, sdxi_resume)
+};
+
 static struct pci_driver sdxi_driver = {
 	.name = "sdxi",
 	.id_table = sdxi_id_table,
 	.probe = sdxi_pci_probe,
 	.remove = sdxi_pci_remove,
 	.sriov_configure = pci_sriov_configure_simple,
+	.driver.pm = pm_sleep_ptr(&sdxi_pm_ops),
 };
 
 MODULE_IMPORT_NS("SDXI");
diff --git a/drivers/dma/sdxi/sdxi.h b/drivers/dma/sdxi/sdxi.h
index 93b4eb80358c..ce0959bbed90 100644
--- a/drivers/dma/sdxi/sdxi.h
+++ b/drivers/dma/sdxi/sdxi.h
@@ -146,6 +146,12 @@ struct sdxi_dev {
 	bool online;
 	struct rw_semaphore state_lock;	/* online state and user operations */
 
+	struct mutex pm_lock;		/* suspend and resume serialization */
+	bool pm_suspended;
+	bool pm_fn_was_active;
+	u64 pm_ctl0;
+	u64 pm_ctl2;
+
 	const struct sdxi_bus_ops *bus_ops;
 };
 
@@ -202,6 +208,8 @@ void sdxi_unregister(struct device *dev);
 bool sdxi_dev_in_error(const struct sdxi_dev *sdxi);
 void sdxi_dev_force_quiesce(struct sdxi_dev *sdxi);
 void sdxi_dev_fail(struct sdxi_dev *sdxi);
+int sdxi_suspend(struct device *dev);
+int sdxi_resume(struct device *dev);
 int sdxi_chardev_init(void);
 void sdxi_chardev_exit(void);
 int sdxi_chardev_register(struct sdxi_dev *sdxi);
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 28/44] dmaengine: sdxi: amd: define QoS request formats
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (26 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 27/44] dmaengine: sdxi: support suspend and resume Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 29/44] dmaengine: sdxi: amd: add QoS validation scaffolding Wei Huang
                   ` (15 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

AMD QoS monitors and controls resource utilization through the SDXI
physical function (PF). Add AMD QoS vendor request formats, operation
identifiers, and helper prototypes for code introduced later.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/amd/qos.h | 95 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
 create mode 100644 drivers/dma/sdxi/amd/qos.h

diff --git a/drivers/dma/sdxi/amd/qos.h b/drivers/dma/sdxi/amd/qos.h
new file mode 100644
index 000000000000..e5e3e5cf9a50
--- /dev/null
+++ b/drivers/dma/sdxi/amd/qos.h
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * AMD SDXI QoS internal definitions.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef __DMA_SDXI_AMD_QOS_H
+#define __DMA_SDXI_AMD_QOS_H
+
+#include <linux/bits.h>
+#include <linux/types.h>
+
+enum sdxi_amd_qos_admin_vendor_op {
+	SDXI_AMD_QOS_ADMIN_OP_CAP = 1,
+	SDXI_AMD_QOS_ADMIN_OP_DEVUPD = 2,
+	SDXI_AMD_QOS_ADMIN_OP_POOLUPD = 3,
+	SDXI_AMD_QOS_ADMIN_OP_FNUPD = 4,
+	SDXI_AMD_QOS_ADMIN_OP_TEL = 5,
+};
+
+enum sdxi_amd_qos_admin_target_flags {
+	SDXI_AMD_QOS_TARGET_F_CXT = BIT(0),
+	SDXI_AMD_QOS_TARGET_F_VF = BIT(1),
+};
+
+enum sdxi_amd_qos_feature_flags {
+	SDXI_AMD_QOS_FEATURE_DYNAMIC_POOL_RECONFIG = BIT(0),
+	SDXI_AMD_QOS_FEATURE_DYNAMIC_POOL_REASSIGN = BIT(1),
+	SDXI_AMD_QOS_FEATURE_TEL_TIMESTAMPS = BIT(2),
+	SDXI_AMD_QOS_FEATURE_FLEXIBLE_POOL_SCOPE = BIT(3),
+};
+
+struct sdxi_amd_qos_devupd_req {
+	u32 device_maxbw;
+};
+
+struct sdxi_amd_qos_poolupd_req {
+	u16 pool_num;
+	u8 pool_operation;
+	u8 rsvd0;
+	u32 pool_maxbw;
+	u32 pool_minbw;
+};
+
+struct sdxi_amd_qos_fnupd_req {
+	u16 pool_num;
+	u16 flags;
+	u16 vf_num;
+	u16 cxt_start;
+	u16 cxt_end;
+	u16 rsvd0;
+};
+
+struct sdxi_amd_qos_tel_req {
+	u16 pool_num;
+	u16 flags;
+	u16 vf_num;
+	u16 cxt_num;
+	u8 query_type;
+	u8 rsvd0[3];
+};
+
+/* AMD SDXI Quality-of-Service Extensions (ASQOSE). */
+#define SDXI_AMD_QOS_DSC_SUBTYPE_CAP		0x00
+#define SDXI_AMD_QOS_DSC_SUBTYPE_DEVUPD		0x01
+#define SDXI_AMD_QOS_DSC_SUBTYPE_POOLUPD	0x02
+#define SDXI_AMD_QOS_DSC_SUBTYPE_FNUPD		0x03
+#define SDXI_AMD_QOS_DSC_SUBTYPE_TEL		0x04
+
+#define SDXI_AMD_QOS_TEL_QTYPE_DEVICE		0x0
+#define SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG	0x1
+#define SDXI_AMD_QOS_TEL_QTYPE_POOL_ASSIGN	0x2
+#define SDXI_AMD_QOS_TEL_QTYPE_ACTIVE_POOLS	0x3
+#define SDXI_AMD_QOS_TEL_QTYPE_POOL_REPORT	0x4
+#define SDXI_AMD_QOS_TEL_QTYPE_POOL_VARIANCE	0x5
+
+#define SDXI_AMD_QOS_POOL_OP_CREATE		0x0
+#define SDXI_AMD_QOS_POOL_OP_UPDATE		0x1
+#define SDXI_AMD_QOS_POOL_OP_DESTROY		0x2
+
+u64 sdxi_amd_qos_query_selector(u16 cxt_num, u8 query_type);
+u32 sdxi_amd_qos_align_bw(u32 bw, u32 granularity);
+int sdxi_amd_qos_validate_devupd_req(u32 device_maxbw, u32 total_allocated_bw,
+				     u32 granularity_bw);
+int sdxi_amd_qos_validate_poolupd_req(const struct sdxi_amd_qos_poolupd_req *req,
+				      u16 max_pools, u32 granularity_bw);
+int sdxi_amd_qos_validate_fnupd_req(const struct sdxi_amd_qos_fnupd_req *req,
+				    u16 max_pools, u16 max_vfs, u32 max_cxts,
+				    u16 feature_flags);
+int sdxi_amd_qos_validate_tel_req(const struct sdxi_amd_qos_tel_req *req,
+				  u16 max_pools, u16 max_vfs, u32 max_cxts,
+				  u16 feature_flags);
+
+#endif /* __DMA_SDXI_AMD_QOS_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 29/44] dmaengine: sdxi: amd: add QoS validation scaffolding
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (27 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 28/44] dmaengine: sdxi: amd: define QoS request formats Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 30/44] dmaengine: sdxi: amd: build QoS admin descriptors Wei Huang
                   ` (14 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Create the AMD QoS handler source with common user-buffer helpers,
GUID discovery, and request validators. The added validators check
bandwidth alignment, device and pool updates, function assignments
and telemetry selectors.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/amd/qos.c | 266 +++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/amd/qos.h |   4 +-
 2 files changed, 268 insertions(+), 2 deletions(-)
 create mode 100644 drivers/dma/sdxi/amd/qos.c

diff --git a/drivers/dma/sdxi/amd/qos.c b/drivers/dma/sdxi/amd/qos.c
new file mode 100644
index 000000000000..b18ed727aee9
--- /dev/null
+++ b/drivers/dma/sdxi/amd/qos.c
@@ -0,0 +1,266 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * AMD SDXI QoS vendor-command handling.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/capability.h>
+#include <linux/dma-mapping.h>
+#include <linux/export.h>
+#include <kunit/visibility.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/sizes.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/unaligned.h>
+#include <linux/uaccess.h>
+#include <uapi/linux/sdxi.h>
+
+#include "../admin.h"
+#include "../sdxi.h"
+#include "../vendor.h"
+#include "qos.h"
+
+#define SDXI_AMD_QOS_CAP_BYTES			48U
+#define SDXI_AMD_QOS_TEL_FIXED_BYTES		48U
+#define SDXI_AMD_QOS_TEL_HDR_BYTES		48U
+#define SDXI_AMD_QOS_POOL_REPORT_ENTRY_BYTES	132U
+#define SDXI_AMD_QOS_MAX_TEL_BYTES		SZ_1M
+
+struct sdxi_amd_qos_cap_info {
+	u16 feature_flags;
+	u16 max_pools;
+	u32 device_maxbw_cap;
+	u32 granularity_bw;
+};
+
+struct sdxi_amd_qos_dev_info {
+	u32 device_maxbw;
+	u32 total_allocated_bw;
+};
+
+struct sdxi_amd_qos_pool_info {
+	u32 pool_minbw;
+	u32 pool_maxbw;
+	u16 assigned_functions;
+	u16 assigned_contexts;
+};
+
+/*
+ * SDXI DSC_DISC uses RFC4122-like GUIDs, but with time_low/time_mid/time_hi
+ * stored in little-endian order.
+ */
+static const u8 sdxi_amd_guid_qos[16] = {
+	0x0b, 0x61, 0xb7, 0xf5, 0xc6, 0x0d, 0x03, 0x4c,
+	0xab, 0x79, 0x65, 0xf1, 0xf4, 0xef, 0xcf, 0x4c,
+};
+
+static int
+sdxi_amd_qos_copy_input(const struct sdxi_admin_vendor_args *args,
+			void *dst, size_t size)
+{
+	void __user *ubuf;
+
+	if (!size) {
+		if (args->in_size || args->in_uptr)
+			return -EINVAL;
+		return 0;
+	}
+
+	if (args->in_size != size || !args->in_uptr)
+		return -EINVAL;
+
+	ubuf = u64_to_user_ptr(args->in_uptr);
+	if (!access_ok(ubuf, size))
+		return -EFAULT;
+	if (copy_from_user(dst, ubuf, size))
+		return -EFAULT;
+	if (args->op == SDXI_AMD_QOS_ADMIN_OP_FNUPD &&
+	    size == sizeof(struct sdxi_amd_qos_fnupd_req)) {
+		struct sdxi_amd_qos_fnupd_req *req = dst;
+
+		if (!(req->flags & SDXI_AMD_QOS_TARGET_F_VF))
+			req->vf_num = 0;
+		if (!(req->flags & SDXI_AMD_QOS_TARGET_F_CXT)) {
+			req->cxt_start = 0;
+			req->cxt_end = 0;
+		}
+	}
+
+	return 0;
+}
+
+static int
+sdxi_amd_qos_require_no_output(struct sdxi_admin_vendor_args *args)
+{
+	if (args->out_size || args->out_uptr)
+		return -EINVAL;
+
+	args->out_size = 0;
+
+	return 0;
+}
+
+static int
+sdxi_amd_qos_prepare_output(struct sdxi_admin_vendor_args *args,
+			    size_t required, void __user **ubuf)
+{
+	u32 available = args->out_size;
+
+	if (required > U32_MAX)
+		return -EOVERFLOW;
+
+	args->out_size = (u32)required;
+	if (available < required)
+		return -ENOSPC;
+	if (!args->out_uptr)
+		return -EINVAL;
+
+	*ubuf = u64_to_user_ptr(args->out_uptr);
+	if (!access_ok(*ubuf, required))
+		return -EFAULT;
+
+	return 0;
+}
+
+static int sdxi_amd_qos_admin_ready(const struct sdxi_cxt *adm)
+{
+	if (!adm || !adm->sq || !adm->ring_state || !adm->sq->cxt_sts)
+		return -ENODEV;
+	if (sdxi_cxt_sts_state(adm->sq->cxt_sts) != CXTV_RUN)
+		return -EIO;
+
+	return 0;
+}
+
+u64 sdxi_amd_qos_query_selector(u16 cxt_num, u8 query_type)
+{
+	return FIELD_PREP(GENMASK_ULL(15, 0), cxt_num) |
+	       FIELD_PREP(GENMASK_ULL(18, 16), query_type);
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_amd_qos_query_selector);
+
+u32 sdxi_amd_qos_align_bw(u32 bw, u32 granularity)
+{
+	if (!granularity)
+		return bw;
+
+	return (bw / granularity) * granularity;
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_amd_qos_align_bw);
+
+int sdxi_amd_qos_validate_devupd_req(u32 device_maxbw, u32 device_maxbw_cap,
+				     u32 total_allocated_bw, u32 granularity_bw)
+{
+	if (device_maxbw > device_maxbw_cap)
+		return -EINVAL;
+
+	if (sdxi_amd_qos_align_bw(device_maxbw, granularity_bw) <
+	    total_allocated_bw)
+		return -EINVAL;
+
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_amd_qos_validate_devupd_req);
+
+int sdxi_amd_qos_validate_poolupd_req(const struct sdxi_amd_qos_poolupd_req *req,
+				      u16 max_pools, u32 granularity_bw)
+{
+	u32 min_bw;
+	u32 max_bw;
+
+	if (!req || !max_pools)
+		return -EINVAL;
+	if (req->rsvd0)
+		return -EINVAL;
+	if (req->pool_num >= max_pools)
+		return -EINVAL;
+	if (req->pool_operation > SDXI_AMD_QOS_POOL_OP_DESTROY)
+		return -EINVAL;
+	if ((req->pool_operation == SDXI_AMD_QOS_POOL_OP_CREATE ||
+	     req->pool_operation == SDXI_AMD_QOS_POOL_OP_DESTROY) &&
+	    !req->pool_num)
+		return -EINVAL;
+	if (req->pool_operation == SDXI_AMD_QOS_POOL_OP_DESTROY)
+		return 0;
+	if (req->pool_minbw > req->pool_maxbw)
+		return -EINVAL;
+
+	min_bw = sdxi_amd_qos_align_bw(req->pool_minbw, granularity_bw);
+	max_bw = sdxi_amd_qos_align_bw(req->pool_maxbw, granularity_bw);
+	if (min_bw > max_bw)
+		return -EINVAL;
+
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_amd_qos_validate_poolupd_req);
+
+int sdxi_amd_qos_validate_fnupd_req(const struct sdxi_amd_qos_fnupd_req *req,
+				    u16 max_pools, u16 max_vfs, u32 max_cxts,
+				    u16 feature_flags)
+{
+	if (!req || !max_pools)
+		return -EINVAL;
+	if (req->rsvd0)
+		return -EINVAL;
+	if (req->pool_num >= max_pools)
+		return -EINVAL;
+	if (req->flags & ~(SDXI_AMD_QOS_TARGET_F_CXT |
+			   SDXI_AMD_QOS_TARGET_F_VF))
+		return -EINVAL;
+	if (req->flags & SDXI_AMD_QOS_TARGET_F_VF) {
+		if (!max_vfs || req->vf_num >= max_vfs)
+			return -EINVAL;
+	}
+
+	if (!(req->flags & SDXI_AMD_QOS_TARGET_F_CXT))
+		return 0;
+
+	(void)feature_flags;
+	if (req->cxt_start > req->cxt_end)
+		return -EINVAL;
+	if (req->cxt_start >= max_cxts || req->cxt_end >= max_cxts)
+		return -EINVAL;
+
+	return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_amd_qos_validate_fnupd_req);
+
+int sdxi_amd_qos_validate_tel_req(const struct sdxi_amd_qos_tel_req *req,
+				  u16 max_pools, u16 max_vfs, u32 max_cxts,
+				  u16 feature_flags)
+{
+	if (!req)
+		return -EINVAL;
+	if (req->rsvd0[0] || req->rsvd0[1] || req->rsvd0[2])
+		return -EINVAL;
+
+	switch (req->query_type) {
+	case SDXI_AMD_QOS_TEL_QTYPE_DEVICE:
+	case SDXI_AMD_QOS_TEL_QTYPE_ACTIVE_POOLS:
+		return 0;
+	case SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG:
+	case SDXI_AMD_QOS_TEL_QTYPE_POOL_REPORT:
+	case SDXI_AMD_QOS_TEL_QTYPE_POOL_VARIANCE:
+		if (!max_pools || req->pool_num >= max_pools)
+			return -EINVAL;
+		return 0;
+	case SDXI_AMD_QOS_TEL_QTYPE_POOL_ASSIGN:
+		if (req->flags & SDXI_AMD_QOS_TARGET_F_VF) {
+			if (!max_vfs || req->vf_num >= max_vfs)
+				return -EINVAL;
+		}
+		if (!(req->flags & SDXI_AMD_QOS_TARGET_F_CXT))
+			return 0;
+		if (!(feature_flags & SDXI_AMD_QOS_FEATURE_FLEXIBLE_POOL_SCOPE))
+			return -EOPNOTSUPP;
+		if (req->cxt_num >= max_cxts)
+			return -EINVAL;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+EXPORT_SYMBOL_IF_KUNIT(sdxi_amd_qos_validate_tel_req);
diff --git a/drivers/dma/sdxi/amd/qos.h b/drivers/dma/sdxi/amd/qos.h
index e5e3e5cf9a50..09c5ee417d9c 100644
--- a/drivers/dma/sdxi/amd/qos.h
+++ b/drivers/dma/sdxi/amd/qos.h
@@ -81,8 +81,8 @@ struct sdxi_amd_qos_tel_req {
 
 u64 sdxi_amd_qos_query_selector(u16 cxt_num, u8 query_type);
 u32 sdxi_amd_qos_align_bw(u32 bw, u32 granularity);
-int sdxi_amd_qos_validate_devupd_req(u32 device_maxbw, u32 total_allocated_bw,
-				     u32 granularity_bw);
+int sdxi_amd_qos_validate_devupd_req(u32 device_maxbw, u32 device_maxbw_cap,
+				     u32 total_allocated_bw, u32 granularity_bw);
 int sdxi_amd_qos_validate_poolupd_req(const struct sdxi_amd_qos_poolupd_req *req,
 				      u16 max_pools, u32 granularity_bw);
 int sdxi_amd_qos_validate_fnupd_req(const struct sdxi_amd_qos_fnupd_req *req,
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 30/44] dmaengine: sdxi: amd: build QoS admin descriptors
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (28 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 29/44] dmaengine: sdxi: amd: add QoS validation scaffolding Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 31/44] dmaengine: sdxi: amd: prevalidate QoS operations Wei Huang
                   ` (13 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add AMD QoS descriptor setup, capability reads, telemetry reads and
helper queries that will be used by execution code paths introduced
later.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/amd/qos.c | 326 +++++++++++++++++++++++++++++++++++++
 1 file changed, 326 insertions(+)

diff --git a/drivers/dma/sdxi/amd/qos.c b/drivers/dma/sdxi/amd/qos.c
index b18ed727aee9..1899f89bcb90 100644
--- a/drivers/dma/sdxi/amd/qos.c
+++ b/drivers/dma/sdxi/amd/qos.c
@@ -264,3 +264,329 @@ int sdxi_amd_qos_validate_tel_req(const struct sdxi_amd_qos_tel_req *req,
 	}
 }
 EXPORT_SYMBOL_IF_KUNIT(sdxi_amd_qos_validate_tel_req);
+
+static void sdxi_amd_qos_desc_init(struct sdxi_desc *desc, u16 opcode_type,
+				   u8 subtype)
+{
+	memset(desc, 0, sizeof(*desc));
+	desc->opcode = cpu_to_le32(FIELD_PREP(SDXI_DSC_SUBTYPE, subtype) |
+				   FIELD_PREP(SDXI_DSC_TYPE, opcode_type));
+	desc->csb_ptr = cpu_to_le64(FIELD_PREP(SDXI_DSC_NP, 1));
+}
+
+static int sdxi_amd_qos_target_scope(u16 pool_num, u16 flags, u16 vf_num,
+				     u32 *target_scope)
+{
+	u32 value;
+
+	if (pool_num > 0x0fff)
+		return -EINVAL;
+	if (flags & ~(SDXI_AMD_QOS_TARGET_F_CXT | SDXI_AMD_QOS_TARGET_F_VF))
+		return -EINVAL;
+	if (!(flags & SDXI_AMD_QOS_TARGET_F_VF) && vf_num)
+		return -EINVAL;
+
+	value = pool_num;
+	if (flags & SDXI_AMD_QOS_TARGET_F_CXT)
+		value |= BIT(12);
+	if (flags & SDXI_AMD_QOS_TARGET_F_VF)
+		value |= BIT(15);
+	value |= (u32)vf_num << 16;
+
+	*target_scope = value;
+	return 0;
+}
+
+static u16 sdxi_amd_qos_max_vfs(struct sdxi_dev *sdxi)
+{
+	struct pci_dev *pdev;
+	unsigned int totalvfs;
+
+	if (!sdxi || sdxi_fn_reports_vf(sdxi))
+		return 0;
+
+	pdev = to_pci_dev(sdxi_to_dev(sdxi));
+	totalvfs = pci_sriov_get_totalvfs(pdev);
+	if (totalvfs > U16_MAX)
+		return U16_MAX;
+
+	return (u16)totalvfs;
+}
+
+static bool sdxi_amd_qos_pool_active(const u8 *bitmap, u16 pool_num)
+{
+	if (!bitmap)
+		return false;
+
+	return bitmap[pool_num / 8] & BIT(pool_num % 8);
+}
+
+static int sdxi_amd_qos_get_cap_info(struct sdxi_dev *sdxi,
+				     struct sdxi_cxt *adm,
+				     u16 opcode_type,
+				     unsigned int timeout_ms,
+				     struct sdxi_amd_qos_cap_info *cap)
+{
+	struct sdxi_desc desc = {};
+	dma_addr_t dma;
+	u8 *buf;
+	int err;
+
+	if (!cap)
+		return -EINVAL;
+
+	err = sdxi_amd_qos_admin_ready(adm);
+	if (err)
+		return err;
+
+	buf = dma_alloc_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_CAP_BYTES, &dma,
+				 GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, 0, SDXI_AMD_QOS_CAP_BYTES);
+
+	if (!IS_ALIGNED(dma, SZ_4K)) {
+		err = -EIO;
+		goto out_free;
+	}
+
+	sdxi_amd_qos_desc_init(&desc, opcode_type,
+			       SDXI_AMD_QOS_DSC_SUBTYPE_CAP);
+
+	/* capability_ptr is descriptor bits[127:64], starting at byte offset 8. */
+	put_unaligned_le64(dma, &desc.generic.operation[4]);
+
+	err = sdxi_adm_submit_desc(adm, &desc, timeout_ms, NULL);
+	if (err)
+		goto out_free;
+
+	dma_rmb();
+	cap->feature_flags = get_unaligned_le16(buf + 2);
+	cap->device_maxbw_cap = get_unaligned_le32(buf + 4);
+	cap->max_pools = (u16)(get_unaligned_le32(buf + 8) & 0x0fff);
+	cap->granularity_bw = get_unaligned_le32(buf + 12);
+	if (!cap->max_pools)
+		err = -EIO;
+
+out_free:
+	dma_free_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_CAP_BYTES, buf, dma);
+	return err;
+}
+
+static int sdxi_amd_qos_get_dev_info(struct sdxi_dev *sdxi,
+				     struct sdxi_cxt *adm,
+				     u16 opcode_type,
+				     unsigned int timeout_ms,
+				     struct sdxi_amd_qos_dev_info *info)
+{
+	struct sdxi_desc desc = {};
+	dma_addr_t dma;
+	u8 *buf;
+	int err;
+
+	if (!info)
+		return -EINVAL;
+
+	err = sdxi_amd_qos_admin_ready(adm);
+	if (err)
+		return err;
+
+	buf = dma_alloc_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_FIXED_BYTES,
+				 &dma, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, 0, SDXI_AMD_QOS_TEL_FIXED_BYTES);
+
+	if (!IS_ALIGNED(dma, SZ_4K)) {
+		err = -EIO;
+		goto out_free_dev;
+	}
+
+	sdxi_amd_qos_desc_init(&desc, opcode_type,
+			       SDXI_AMD_QOS_DSC_SUBTYPE_TEL);
+	put_unaligned_le64(sdxi_amd_qos_query_selector(0,
+						       SDXI_AMD_QOS_TEL_QTYPE_DEVICE),
+			   &desc.generic.operation[4]);
+	put_unaligned_le64(dma, &desc.generic.operation[12]);
+
+	err = sdxi_adm_submit_desc(adm, &desc, timeout_ms, NULL);
+	if (err)
+		goto out_free_dev;
+
+	dma_rmb();
+	info->device_maxbw = get_unaligned_le32(buf + 4);
+	info->total_allocated_bw = get_unaligned_le32(buf + 12);
+
+out_free_dev:
+	dma_free_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_FIXED_BYTES,
+			  buf, dma);
+	return err;
+}
+
+static int sdxi_amd_qos_get_pool_info(struct sdxi_dev *sdxi,
+				      struct sdxi_cxt *adm,
+				      u16 opcode_type, u16 pool_num,
+				      unsigned int timeout_ms,
+				      struct sdxi_amd_qos_pool_info *info)
+{
+	struct sdxi_desc desc = {};
+	dma_addr_t dma;
+	u8 *buf;
+	u32 target_scope;
+	int err;
+
+	if (!info)
+		return -EINVAL;
+
+	err = sdxi_amd_qos_admin_ready(adm);
+	if (err)
+		return err;
+
+	buf = dma_alloc_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_FIXED_BYTES,
+				 &dma, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, 0, SDXI_AMD_QOS_TEL_FIXED_BYTES);
+
+	if (!IS_ALIGNED(dma, SZ_4K)) {
+		err = -EIO;
+		goto out_free_pool;
+	}
+
+	sdxi_amd_qos_desc_init(&desc, opcode_type,
+			       SDXI_AMD_QOS_DSC_SUBTYPE_TEL);
+	target_scope = FIELD_PREP(GENMASK(11, 0), pool_num);
+	put_unaligned_le32(target_scope, &desc.generic.operation[0]);
+	put_unaligned_le64(sdxi_amd_qos_query_selector(0,
+						       SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG),
+			   &desc.generic.operation[4]);
+	put_unaligned_le64(dma, &desc.generic.operation[12]);
+
+	err = sdxi_adm_submit_desc(adm, &desc, timeout_ms, NULL);
+	if (err)
+		goto out_free_pool;
+
+	dma_rmb();
+	info->pool_minbw = get_unaligned_le32(buf + 8);
+	info->pool_maxbw = get_unaligned_le32(buf + 12);
+	info->assigned_functions = get_unaligned_le16(buf + 16);
+	info->assigned_contexts = get_unaligned_le16(buf + 18);
+
+out_free_pool:
+	dma_free_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_FIXED_BYTES,
+			  buf, dma);
+	return err;
+}
+
+static int sdxi_amd_qos_get_active_pools(struct sdxi_dev *sdxi,
+					 struct sdxi_cxt *adm,
+					 u16 opcode_type,
+					 unsigned int timeout_ms,
+					 u16 max_pools, u8 *bitmap)
+{
+	struct sdxi_desc desc = {};
+	dma_addr_t dma;
+	u8 *buf;
+	size_t bytes;
+	int err;
+
+	if (!bitmap || !max_pools)
+		return -EINVAL;
+
+	err = sdxi_amd_qos_admin_ready(adm);
+	if (err)
+		return err;
+
+	bytes = (size_t)(max_pools + 7U) / 8U;
+	buf = dma_alloc_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_HDR_BYTES + bytes,
+				 &dma, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, 0, SDXI_AMD_QOS_TEL_HDR_BYTES + bytes);
+
+	if (!IS_ALIGNED(dma, SZ_4K)) {
+		err = -EIO;
+		goto out_free_active;
+	}
+
+	sdxi_amd_qos_desc_init(&desc, opcode_type,
+			       SDXI_AMD_QOS_DSC_SUBTYPE_TEL);
+	put_unaligned_le64(sdxi_amd_qos_query_selector(0,
+						       SDXI_AMD_QOS_TEL_QTYPE_ACTIVE_POOLS),
+			   &desc.generic.operation[4]);
+	put_unaligned_le64(dma, &desc.generic.operation[12]);
+
+	err = sdxi_adm_submit_desc(adm, &desc, timeout_ms, NULL);
+	if (err)
+		goto out_free_active;
+
+	dma_rmb();
+	memcpy(bitmap, buf + SDXI_AMD_QOS_TEL_HDR_BYTES, bytes);
+
+out_free_active:
+	dma_free_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_HDR_BYTES + bytes,
+			  buf, dma);
+	return err;
+}
+
+static int sdxi_amd_qos_get_assigned_functions(struct sdxi_dev *sdxi,
+					       struct sdxi_cxt *adm,
+					       u16 opcode_type, u16 pool_num,
+					       unsigned int timeout_ms,
+					       u16 *assigned_functions)
+{
+	struct sdxi_desc desc = {};
+	dma_addr_t dma;
+	u8 *buf;
+	u32 counts;
+	u32 target_scope;
+	int err;
+
+	if (!assigned_functions)
+		return -EINVAL;
+
+	err = sdxi_amd_qos_admin_ready(adm);
+	if (err)
+		return err;
+
+	buf = dma_alloc_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_FIXED_BYTES,
+				 &dma, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, 0, SDXI_AMD_QOS_TEL_FIXED_BYTES);
+
+	if (!IS_ALIGNED(dma, SZ_4K)) {
+		err = -EIO;
+		goto out_free;
+	}
+
+	sdxi_amd_qos_desc_init(&desc, opcode_type,
+			       SDXI_AMD_QOS_DSC_SUBTYPE_TEL);
+
+	target_scope = FIELD_PREP(GENMASK(11, 0), pool_num);
+	put_unaligned_le32(target_scope, &desc.generic.operation[0]);
+	put_unaligned_le64(sdxi_amd_qos_query_selector(0,
+						       SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG),
+			   &desc.generic.operation[4]);
+
+	/* telemetry_ptr is descriptor bits[191:128], starting at byte offset 16. */
+	put_unaligned_le64(dma, &desc.generic.operation[12]);
+
+	err = sdxi_adm_submit_desc(adm, &desc, timeout_ms, NULL);
+	if (err)
+		goto out_free;
+
+	dma_rmb();
+	counts = get_unaligned_le32(buf + 16);
+	*assigned_functions = (u16)(counts & 0xffff);
+
+out_free:
+	dma_free_coherent(sdxi_to_dev(sdxi), SDXI_AMD_QOS_TEL_FIXED_BYTES,
+			  buf, dma);
+	return err;
+}
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 31/44] dmaengine: sdxi: amd: prevalidate QoS operations
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (29 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 30/44] dmaengine: sdxi: amd: build QoS admin descriptors Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 32/44] dmaengine: sdxi: amd: execute QoS vendor operations Wei Huang
                   ` (12 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add software prevalidation for QoS pool updates, function assignments
and telemetry requests so predictable admin faults are rejected early.
These helpers will be used by code introduced later.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/amd/qos.c | 174 +++++++++++++++++++++++++++++++++++++
 1 file changed, 174 insertions(+)

diff --git a/drivers/dma/sdxi/amd/qos.c b/drivers/dma/sdxi/amd/qos.c
index 1899f89bcb90..746e4c8b11e7 100644
--- a/drivers/dma/sdxi/amd/qos.c
+++ b/drivers/dma/sdxi/amd/qos.c
@@ -590,3 +590,177 @@ static int sdxi_amd_qos_get_assigned_functions(struct sdxi_dev *sdxi,
 			  buf, dma);
 	return err;
 }
+
+static int sdxi_amd_qos_prevalidate_poolupd(struct sdxi_dev *sdxi,
+					    struct sdxi_cxt *adm,
+					    u16 opcode_type,
+					    unsigned int timeout_ms,
+					    const struct sdxi_amd_qos_cap_info *cap,
+					    const struct sdxi_amd_qos_poolupd_req *req)
+{
+	struct sdxi_amd_qos_dev_info dev_info;
+	struct sdxi_amd_qos_pool_info pool_info;
+	u32 new_minbw;
+	u32 total_minbw;
+	size_t bytes;
+	u8 *bitmap;
+	bool pool_known;
+	int err;
+
+	err = sdxi_amd_qos_validate_poolupd_req(req, cap->max_pools,
+						cap->granularity_bw);
+	if (err)
+		return err;
+
+	err = sdxi_amd_qos_get_dev_info(sdxi, adm, opcode_type, timeout_ms,
+					&dev_info);
+	if (err)
+		return err;
+
+	bytes = (size_t)(cap->max_pools + 7U) / 8U;
+	bitmap = kcalloc(bytes, sizeof(*bitmap), GFP_KERNEL);
+	if (!bitmap)
+		return -ENOMEM;
+
+	err = sdxi_amd_qos_get_active_pools(sdxi, adm, opcode_type,
+					    timeout_ms, cap->max_pools,
+					    bitmap);
+	if (err) {
+		kfree(bitmap);
+		return err;
+	}
+
+	pool_known = !req->pool_num ||
+		     sdxi_amd_qos_pool_active(bitmap, req->pool_num);
+
+	switch (req->pool_operation) {
+	case SDXI_AMD_QOS_POOL_OP_CREATE:
+		if (sdxi_amd_qos_pool_active(bitmap, req->pool_num))
+			err = -EEXIST;
+		kfree(bitmap);
+		if (err)
+			return err;
+
+		new_minbw = sdxi_amd_qos_align_bw(req->pool_minbw,
+						  cap->granularity_bw);
+		if (dev_info.total_allocated_bw > dev_info.device_maxbw)
+			return -EIO;
+		if (new_minbw > dev_info.device_maxbw - dev_info.total_allocated_bw)
+			return -EINVAL;
+		return 0;
+	case SDXI_AMD_QOS_POOL_OP_UPDATE:
+		kfree(bitmap);
+		if (!pool_known)
+			return -ENOENT;
+		err = sdxi_amd_qos_get_pool_info(sdxi, adm, opcode_type,
+						 req->pool_num, timeout_ms,
+						 &pool_info);
+		if (err)
+			return err;
+
+		if (pool_info.pool_minbw > dev_info.total_allocated_bw)
+			return -EIO;
+		if (dev_info.total_allocated_bw > dev_info.device_maxbw)
+			return -EIO;
+
+		new_minbw = sdxi_amd_qos_align_bw(req->pool_minbw,
+						  cap->granularity_bw);
+		total_minbw = dev_info.total_allocated_bw - pool_info.pool_minbw;
+		if (new_minbw > dev_info.device_maxbw - total_minbw)
+			return -EINVAL;
+		return 0;
+	case SDXI_AMD_QOS_POOL_OP_DESTROY:
+		kfree(bitmap);
+		if (!pool_known)
+			return -ENOENT;
+		err = sdxi_amd_qos_get_pool_info(sdxi, adm, opcode_type,
+						 req->pool_num, timeout_ms,
+						 &pool_info);
+		if (err)
+			return err;
+
+		return 0;
+	default:
+		kfree(bitmap);
+		return -EINVAL;
+	}
+}
+
+static int sdxi_amd_qos_prevalidate_fnupd(struct sdxi_dev *sdxi,
+					  struct sdxi_cxt *adm,
+					  u16 opcode_type,
+					  unsigned int timeout_ms,
+					  const struct sdxi_amd_qos_cap_info *cap,
+					  const struct sdxi_amd_qos_fnupd_req *req)
+{
+	size_t bytes;
+	u8 *bitmap;
+	int err;
+
+	err = sdxi_amd_qos_validate_fnupd_req(req, cap->max_pools,
+					      sdxi_amd_qos_max_vfs(sdxi),
+					      sdxi->max_cxts,
+					      cap->feature_flags);
+	if (err)
+		return err;
+
+	if (!req->pool_num)
+		return 0;
+
+	bytes = (size_t)(cap->max_pools + 7U) / 8U;
+	bitmap = kcalloc(bytes, sizeof(*bitmap), GFP_KERNEL);
+	if (!bitmap)
+		return -ENOMEM;
+
+	err = sdxi_amd_qos_get_active_pools(sdxi, adm, opcode_type, timeout_ms,
+					    cap->max_pools, bitmap);
+	if (!err && !sdxi_amd_qos_pool_active(bitmap, req->pool_num))
+		err = -ENOENT;
+	kfree(bitmap);
+	if (err)
+		return err;
+
+	return 0;
+}
+
+static int sdxi_amd_qos_prevalidate_tel(struct sdxi_dev *sdxi,
+					struct sdxi_cxt *adm,
+					u16 opcode_type,
+					unsigned int timeout_ms,
+					const struct sdxi_amd_qos_cap_info *cap,
+					const struct sdxi_amd_qos_tel_req *req)
+{
+	size_t bytes;
+	u8 *bitmap;
+	int err;
+
+	err = sdxi_amd_qos_validate_tel_req(req, cap->max_pools,
+					    sdxi_amd_qos_max_vfs(sdxi),
+					    sdxi->max_cxts,
+					    cap->feature_flags);
+	if (err)
+		return err;
+
+	switch (req->query_type) {
+	case SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG:
+	case SDXI_AMD_QOS_TEL_QTYPE_POOL_REPORT:
+	case SDXI_AMD_QOS_TEL_QTYPE_POOL_VARIANCE:
+		if (!req->pool_num)
+			return 0;
+
+		bytes = (size_t)(cap->max_pools + 7U) / 8U;
+		bitmap = kcalloc(bytes, sizeof(*bitmap), GFP_KERNEL);
+		if (!bitmap)
+			return -ENOMEM;
+
+		err = sdxi_amd_qos_get_active_pools(sdxi, adm, opcode_type,
+						    timeout_ms, cap->max_pools,
+						    bitmap);
+		if (!err && !sdxi_amd_qos_pool_active(bitmap, req->pool_num))
+			err = -ENOENT;
+		kfree(bitmap);
+		return err;
+	default:
+		return 0;
+	}
+}
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 32/44] dmaengine: sdxi: amd: execute QoS vendor operations
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (30 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 31/44] dmaengine: sdxi: amd: prevalidate QoS operations Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 33/44] dmaengine: sdxi: amd: register QoS vendor handler Wei Huang
                   ` (11 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add execution code for AMD QoS capability, device update, pool update,
function update and telemetry operations.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/amd/qos.c | 321 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 320 insertions(+), 1 deletion(-)

diff --git a/drivers/dma/sdxi/amd/qos.c b/drivers/dma/sdxi/amd/qos.c
index 746e4c8b11e7..7d5dc9b6b32c 100644
--- a/drivers/dma/sdxi/amd/qos.c
+++ b/drivers/dma/sdxi/amd/qos.c
@@ -27,7 +27,13 @@
 #define SDXI_AMD_QOS_TEL_FIXED_BYTES		48U
 #define SDXI_AMD_QOS_TEL_HDR_BYTES		48U
 #define SDXI_AMD_QOS_POOL_REPORT_ENTRY_BYTES	132U
-#define SDXI_AMD_QOS_MAX_TEL_BYTES		SZ_1M
+
+/* One PF entry plus all 10-bit VF-number entries. */
+#define SDXI_AMD_QOS_POOL_REPORT_MAX_FUNCTIONS	1025U
+#define SDXI_AMD_QOS_POOL_REPORT_MAX_BYTES				\
+	(SDXI_AMD_QOS_TEL_HDR_BYTES +					\
+	 SDXI_AMD_QOS_POOL_REPORT_MAX_FUNCTIONS *			\
+	 SDXI_AMD_QOS_POOL_REPORT_ENTRY_BYTES)
 
 struct sdxi_amd_qos_cap_info {
 	u16 feature_flags;
@@ -764,3 +770,316 @@ static int sdxi_amd_qos_prevalidate_tel(struct sdxi_dev *sdxi,
 		return 0;
 	}
 }
+
+static size_t sdxi_amd_qos_tel_bytes_active_pools(u16 max_pools)
+{
+	return SDXI_AMD_QOS_TEL_HDR_BYTES + (size_t)(max_pools + 7U) / 8U;
+}
+
+static int sdxi_amd_qos_pool_report_bytes(u16 assigned_functions,
+					  size_t *bytes)
+{
+	if (assigned_functions > SDXI_AMD_QOS_POOL_REPORT_MAX_FUNCTIONS)
+		return -E2BIG;
+
+	*bytes = SDXI_AMD_QOS_TEL_HDR_BYTES +
+		 (size_t)assigned_functions *
+		 SDXI_AMD_QOS_POOL_REPORT_ENTRY_BYTES;
+	return 0;
+}
+
+static int sdxi_amd_qos_exec(struct sdxi_dev *sdxi, u16 opcode_type,
+			     struct sdxi_admin_vendor_args *args,
+			     unsigned int timeout_ms)
+{
+	struct sdxi_cxt *adm = sdxi->admin_cxt;
+	struct sdxi_amd_qos_cap_info cap;
+	struct sdxi_desc desc;
+	void *buf = NULL;
+	dma_addr_t buf_dma = 0;
+	size_t dma_size = 0;
+	size_t required = 0;
+	void __user *ubuf = NULL;
+	u32 output_available = args->out_size;
+	bool pool_report = false;
+	int err;
+
+	err = sdxi_amd_qos_admin_ready(adm);
+	if (err)
+		return err;
+
+	switch (args->op) {
+	case SDXI_AMD_QOS_ADMIN_OP_CAP:
+		err = sdxi_amd_qos_copy_input(args, NULL, 0);
+		if (err)
+			return err;
+
+		required = SDXI_AMD_QOS_CAP_BYTES;
+		err = sdxi_amd_qos_prepare_output(args, required, &ubuf);
+		if (err)
+			return err;
+
+		sdxi_amd_qos_desc_init(&desc, opcode_type,
+				       SDXI_AMD_QOS_DSC_SUBTYPE_CAP);
+		break;
+	case SDXI_AMD_QOS_ADMIN_OP_DEVUPD: {
+		struct sdxi_amd_qos_devupd_req req;
+		struct sdxi_amd_qos_dev_info info;
+
+		err = sdxi_amd_qos_copy_input(args, &req, sizeof(req));
+		if (err)
+			return err;
+		err = sdxi_amd_qos_get_cap_info(sdxi, adm, opcode_type,
+						timeout_ms, &cap);
+		if (err)
+			return err;
+		err = sdxi_amd_qos_get_dev_info(sdxi, adm, opcode_type,
+						timeout_ms, &info);
+		if (err)
+			return err;
+		err = sdxi_amd_qos_validate_devupd_req(req.device_maxbw,
+						       cap.device_maxbw_cap,
+						       info.total_allocated_bw,
+						       cap.granularity_bw);
+		if (err)
+			return err;
+
+		err = sdxi_amd_qos_require_no_output(args);
+		if (err)
+			return err;
+
+		sdxi_amd_qos_desc_init(&desc, opcode_type,
+				       SDXI_AMD_QOS_DSC_SUBTYPE_DEVUPD);
+		put_unaligned_le32(req.device_maxbw, &desc.generic.operation[4]);
+
+		return sdxi_adm_submit_desc(adm, &desc, timeout_ms,
+					    &args->cst_flags);
+	}
+	case SDXI_AMD_QOS_ADMIN_OP_POOLUPD: {
+		struct sdxi_amd_qos_poolupd_req req;
+		u32 pool_ctl;
+
+		err = sdxi_amd_qos_copy_input(args, &req, sizeof(req));
+		if (err)
+			return err;
+		err = sdxi_amd_qos_get_cap_info(sdxi, adm, opcode_type,
+						timeout_ms, &cap);
+		if (err)
+			return err;
+		err = sdxi_amd_qos_prevalidate_poolupd(sdxi, adm, opcode_type,
+						       timeout_ms, &cap, &req);
+		if (err)
+			return err;
+
+		err = sdxi_amd_qos_require_no_output(args);
+		if (err)
+			return err;
+
+		pool_ctl = FIELD_PREP(GENMASK(11, 0), req.pool_num) |
+			   FIELD_PREP(GENMASK(13, 12), req.pool_operation);
+
+		sdxi_amd_qos_desc_init(&desc, opcode_type,
+				       SDXI_AMD_QOS_DSC_SUBTYPE_POOLUPD);
+		put_unaligned_le32(pool_ctl, &desc.generic.operation[0]);
+		put_unaligned_le32(req.pool_maxbw, &desc.generic.operation[4]);
+		put_unaligned_le32(req.pool_minbw, &desc.generic.operation[8]);
+
+		return sdxi_adm_submit_desc(adm, &desc, timeout_ms,
+					    &args->cst_flags);
+	}
+	case SDXI_AMD_QOS_ADMIN_OP_FNUPD: {
+		struct sdxi_amd_qos_fnupd_req req;
+		u32 target_scope;
+		u32 cxt_range;
+
+		err = sdxi_amd_qos_copy_input(args, &req, sizeof(req));
+		if (err)
+			return err;
+		err = sdxi_amd_qos_get_cap_info(sdxi, adm, opcode_type,
+						timeout_ms, &cap);
+		if (err)
+			return err;
+		err = sdxi_amd_qos_prevalidate_fnupd(sdxi, adm, opcode_type,
+						     timeout_ms, &cap, &req);
+		if (err)
+			return err;
+
+		err = sdxi_amd_qos_target_scope(req.pool_num, req.flags,
+						req.vf_num, &target_scope);
+		if (err)
+			return err;
+
+		err = sdxi_amd_qos_require_no_output(args);
+		if (err)
+			return err;
+
+		cxt_range = FIELD_PREP(GENMASK(15, 0), req.cxt_start) |
+			    ((u32)req.cxt_end << 16);
+
+		sdxi_amd_qos_desc_init(&desc, opcode_type,
+				       SDXI_AMD_QOS_DSC_SUBTYPE_FNUPD);
+		put_unaligned_le32(target_scope, &desc.generic.operation[0]);
+		put_unaligned_le32(cxt_range, &desc.generic.operation[4]);
+
+		return sdxi_adm_submit_desc(adm, &desc, timeout_ms,
+					    &args->cst_flags);
+	}
+	case SDXI_AMD_QOS_ADMIN_OP_TEL: {
+		struct sdxi_amd_qos_tel_req req;
+		u32 target_scope;
+		u64 query_selector;
+		u16 assigned_functions;
+
+		err = sdxi_amd_qos_copy_input(args, &req, sizeof(req));
+		if (err)
+			return err;
+		err = sdxi_amd_qos_get_cap_info(sdxi, adm, opcode_type,
+						timeout_ms, &cap);
+		if (err)
+			return err;
+		err = sdxi_amd_qos_prevalidate_tel(sdxi, adm, opcode_type,
+						   timeout_ms, &cap, &req);
+		if (err)
+			return err;
+
+		switch (req.query_type) {
+		case SDXI_AMD_QOS_TEL_QTYPE_DEVICE:
+			target_scope = 0;
+			query_selector = sdxi_amd_qos_query_selector(0,
+								     req.query_type);
+			required = SDXI_AMD_QOS_TEL_FIXED_BYTES;
+			break;
+		case SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG:
+			target_scope = FIELD_PREP(GENMASK(11, 0),
+						  req.pool_num);
+			query_selector = sdxi_amd_qos_query_selector(0,
+								     req.query_type);
+			required = SDXI_AMD_QOS_TEL_FIXED_BYTES;
+			break;
+		case SDXI_AMD_QOS_TEL_QTYPE_POOL_ASSIGN:
+			err = sdxi_amd_qos_target_scope(0, req.flags,
+							req.vf_num,
+							&target_scope);
+			if (err)
+				return err;
+			if (!(req.flags & SDXI_AMD_QOS_TARGET_F_CXT))
+				req.cxt_num = 0;
+			query_selector = sdxi_amd_qos_query_selector(req.cxt_num,
+								     req.query_type);
+			required = SDXI_AMD_QOS_TEL_FIXED_BYTES;
+			break;
+		case SDXI_AMD_QOS_TEL_QTYPE_ACTIVE_POOLS:
+			target_scope = 0;
+			query_selector = sdxi_amd_qos_query_selector(0,
+								     req.query_type);
+			required =
+				sdxi_amd_qos_tel_bytes_active_pools(cap.max_pools);
+			break;
+		case SDXI_AMD_QOS_TEL_QTYPE_POOL_REPORT:
+			target_scope = FIELD_PREP(GENMASK(11, 0),
+						  req.pool_num);
+			query_selector = sdxi_amd_qos_query_selector(0,
+								     req.query_type);
+			err = sdxi_amd_qos_get_assigned_functions(sdxi, adm,
+								  opcode_type,
+								  req.pool_num,
+								  timeout_ms,
+								  &assigned_functions);
+			if (err)
+				return err;
+
+			err = sdxi_amd_qos_pool_report_bytes(assigned_functions,
+							     &required);
+			if (err)
+				return err;
+			dma_size = SDXI_AMD_QOS_POOL_REPORT_MAX_BYTES;
+			pool_report = true;
+			break;
+		case SDXI_AMD_QOS_TEL_QTYPE_POOL_VARIANCE:
+			target_scope = FIELD_PREP(GENMASK(11, 0),
+						  req.pool_num);
+			query_selector = sdxi_amd_qos_query_selector(0,
+								     req.query_type);
+			required = SDXI_AMD_QOS_TEL_FIXED_BYTES;
+			break;
+		default:
+			return -EINVAL;
+		}
+
+		err = sdxi_amd_qos_prepare_output(args, required, &ubuf);
+		if (err)
+			return err;
+
+		sdxi_amd_qos_desc_init(&desc, opcode_type,
+				       SDXI_AMD_QOS_DSC_SUBTYPE_TEL);
+		put_unaligned_le32(target_scope, &desc.generic.operation[0]);
+		put_unaligned_le64(query_selector, &desc.generic.operation[4]);
+		break;
+	}
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	if (!dma_size)
+		dma_size = required;
+
+	buf = dma_alloc_coherent(sdxi_to_dev(sdxi), dma_size, &buf_dma,
+				 GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	memset(buf, 0, dma_size);
+
+	if (!IS_ALIGNED(buf_dma, SZ_4K)) {
+		err = -EIO;
+		goto out_free_submit;
+	}
+
+	switch (args->op) {
+	case SDXI_AMD_QOS_ADMIN_OP_CAP:
+		put_unaligned_le64(buf_dma, &desc.generic.operation[4]);
+		break;
+	case SDXI_AMD_QOS_ADMIN_OP_TEL:
+		put_unaligned_le64(buf_dma, &desc.generic.operation[12]);
+		break;
+	default:
+		err = -EINVAL;
+		goto out_free_submit;
+	}
+
+	err = sdxi_adm_submit_desc(adm, &desc, timeout_ms, &args->cst_flags);
+	if (err)
+		goto out_free_submit;
+
+	dma_rmb();
+	if (pool_report) {
+		size_t actual_required;
+		u32 data_length = get_unaligned_le32(buf + 20);
+		u16 assigned_functions = get_unaligned_le16(buf + 16);
+
+		err = sdxi_amd_qos_pool_report_bytes(assigned_functions,
+						     &actual_required);
+		if (err || data_length !=
+		    actual_required - SDXI_AMD_QOS_TEL_HDR_BYTES) {
+			err = -EIO;
+			goto out_free_submit;
+		}
+
+		args->out_size = actual_required;
+		if (output_available < actual_required) {
+			err = -ENOSPC;
+			goto out_free_submit;
+		}
+		if (!access_ok(ubuf, actual_required)) {
+			err = -EFAULT;
+			goto out_free_submit;
+		}
+		required = actual_required;
+	}
+	if (copy_to_user(ubuf, buf, required))
+		err = -EFAULT;
+
+out_free_submit:
+	dma_free_coherent(sdxi_to_dev(sdxi), dma_size, buf, buf_dma);
+	return err;
+}
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 33/44] dmaengine: sdxi: amd: register QoS vendor handler
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (31 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 32/44] dmaengine: sdxi: amd: execute QoS vendor operations Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 34/44] dmaengine: sdxi: amd: test QoS validation with KUnit Wei Huang
                   ` (10 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Register the AMD QoS GUID with the SDXI vendor framework and add the
Kconfig and module build glue for the handler.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Kconfig      | 11 +++++++
 drivers/dma/sdxi/Makefile     |  2 ++
 drivers/dma/sdxi/amd/Makefile |  4 +++
 drivers/dma/sdxi/amd/qos.c    | 58 ++++++++++++++++++++++++++++++++---
 4 files changed, 71 insertions(+), 4 deletions(-)
 create mode 100644 drivers/dma/sdxi/amd/Makefile

diff --git a/drivers/dma/sdxi/Kconfig b/drivers/dma/sdxi/Kconfig
index 9b8416304ee2..ec89d976c3d3 100644
--- a/drivers/dma/sdxi/Kconfig
+++ b/drivers/dma/sdxi/Kconfig
@@ -41,3 +41,14 @@ config SDXI_KUNIT_TEST
 	  SDXI hardware.
 
 	  If unsure, say N.
+
+config SDXI_AMD_QOS
+	tristate "AMD SDXI QoS vendor control"
+	depends on SDXI_CORE
+	help
+	  Enable AMD SDXI Quality-of-Service vendor-command handling
+	  through SDXI_ADMIN_VENDOR. This registers the AMD QoS GUID
+	  with the SDXI core and lets the kernel compose the QoS
+	  descriptors on the admin context on behalf of userspace.
+
+	  If built as a module, this will be called sdxi-amd-qos.
diff --git a/drivers/dma/sdxi/Makefile b/drivers/dma/sdxi/Makefile
index 34f62b7366eb..d808492c60f1 100644
--- a/drivers/dma/sdxi/Makefile
+++ b/drivers/dma/sdxi/Makefile
@@ -20,6 +20,8 @@ sdxi-core-y := \
 obj-$(CONFIG_SDXI_PCI) += sdxi-pci.o
 sdxi-pci-y := pci.o
 
+obj-y += amd/
+
 obj-$(CONFIG_SDXI_KUNIT_TEST) += \
 	completion_kunit.o    \
 	descriptor_kunit.o    \
diff --git a/drivers/dma/sdxi/amd/Makefile b/drivers/dma/sdxi/amd/Makefile
new file mode 100644
index 000000000000..5aee7e8a2414
--- /dev/null
+++ b/drivers/dma/sdxi/amd/Makefile
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_SDXI_AMD_QOS) += sdxi-amd-qos.o
+sdxi-amd-qos-objs := qos.o
diff --git a/drivers/dma/sdxi/amd/qos.c b/drivers/dma/sdxi/amd/qos.c
index 7d5dc9b6b32c..be5a03c00327 100644
--- a/drivers/dma/sdxi/amd/qos.c
+++ b/drivers/dma/sdxi/amd/qos.c
@@ -6,6 +6,7 @@
  */
 
 #include <linux/capability.h>
+#include <linux/bitfield.h>
 #include <linux/dma-mapping.h>
 #include <linux/export.h>
 #include <kunit/visibility.h>
@@ -133,9 +134,10 @@ sdxi_amd_qos_prepare_output(struct sdxi_admin_vendor_args *args,
 
 static int sdxi_amd_qos_admin_ready(const struct sdxi_cxt *adm)
 {
-	if (!adm || !adm->sq || !adm->ring_state || !adm->sq->cxt_sts)
+	if (!adm || !adm->sq || !adm->ring_state)
 		return -ENODEV;
-	if (sdxi_cxt_sts_state(adm->sq->cxt_sts) != CXTV_RUN)
+	if (FIELD_GET(SDXI_CXT_STS_STATE,
+		      READ_ONCE(adm->sq->cxt_sts.state)) != CXTV_RUN)
 		return -EIO;
 
 	return 0;
@@ -310,6 +312,8 @@ static u16 sdxi_amd_qos_max_vfs(struct sdxi_dev *sdxi)
 
 	if (!sdxi || sdxi_fn_reports_vf(sdxi))
 		return 0;
+	if (!dev_is_pci(sdxi_to_dev(sdxi)))
+		return 0;
 
 	pdev = to_pci_dev(sdxi_to_dev(sdxi));
 	totalvfs = pci_sriov_get_totalvfs(pdev);
@@ -705,7 +709,7 @@ static int sdxi_amd_qos_prevalidate_fnupd(struct sdxi_dev *sdxi,
 
 	err = sdxi_amd_qos_validate_fnupd_req(req, cap->max_pools,
 					      sdxi_amd_qos_max_vfs(sdxi),
-					      sdxi->max_cxts,
+					      (u32)sdxi->max_cxtid + 1,
 					      cap->feature_flags);
 	if (err)
 		return err;
@@ -742,7 +746,7 @@ static int sdxi_amd_qos_prevalidate_tel(struct sdxi_dev *sdxi,
 
 	err = sdxi_amd_qos_validate_tel_req(req, cap->max_pools,
 					    sdxi_amd_qos_max_vfs(sdxi),
-					    sdxi->max_cxts,
+					    (u32)sdxi->max_cxtid + 1,
 					    cap->feature_flags);
 	if (err)
 		return err;
@@ -1083,3 +1087,49 @@ static int sdxi_amd_qos_exec(struct sdxi_dev *sdxi, u16 opcode_type,
 	dma_free_coherent(sdxi_to_dev(sdxi), dma_size, buf, buf_dma);
 	return err;
 }
+
+static int sdxi_amd_qos_authorize(struct sdxi_dev *sdxi,
+				  const struct sdxi_admin_vendor_args *args)
+{
+	(void)args;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	/* ASQOSE is PF admin-context only, including vendor PROBE. */
+	if (sdxi_fn_reports_vf(sdxi))
+		return -EOPNOTSUPP;
+
+	return 0;
+}
+
+static const struct sdxi_vendor_handler_ops sdxi_amd_qos_ops = {
+	.authorize = sdxi_amd_qos_authorize,
+	.exec = sdxi_amd_qos_exec,
+};
+
+static struct sdxi_vendor_handler sdxi_amd_qos_handler = {
+	.name = "sdxi-amd-qos",
+	.guid = sdxi_amd_guid_qos,
+	.owner = THIS_MODULE,
+	.ops = &sdxi_amd_qos_ops,
+};
+
+static int __init sdxi_amd_qos_init(void)
+{
+	return sdxi_vendor_register(&sdxi_amd_qos_handler);
+}
+
+static void __exit sdxi_amd_qos_exit(void)
+{
+	sdxi_vendor_unregister(&sdxi_amd_qos_handler);
+}
+
+module_init(sdxi_amd_qos_init);
+module_exit(sdxi_amd_qos_exit);
+
+MODULE_AUTHOR("Wei Huang <wei.huang2@amd.com>");
+MODULE_DESCRIPTION("AMD SDXI QoS extension support");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("SDXI");
+MODULE_SOFTDEP("pre: sdxi-core");
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 34/44] dmaengine: sdxi: amd: test QoS validation with KUnit
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (32 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 33/44] dmaengine: sdxi: amd: register QoS vendor handler Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 35/44] selftests: sdxi: add build scaffold Wei Huang
                   ` (9 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add KUnit coverage for AMD QoS validation helpers. Note that hardware
execution paths still remain covered by other functional kunit tests.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/Kconfig         |   5 +
 drivers/dma/sdxi/amd/Makefile    |   2 +
 drivers/dma/sdxi/amd/qos_kunit.c | 180 +++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
 create mode 100644 drivers/dma/sdxi/amd/qos_kunit.c

diff --git a/drivers/dma/sdxi/Kconfig b/drivers/dma/sdxi/Kconfig
index ec89d976c3d3..fc71f1d3bc78 100644
--- a/drivers/dma/sdxi/Kconfig
+++ b/drivers/dma/sdxi/Kconfig
@@ -52,3 +52,8 @@ config SDXI_AMD_QOS
 	  descriptors on the admin context on behalf of userspace.
 
 	  If built as a module, this will be called sdxi-amd-qos.
+
+config SDXI_AMD_QOS_KUNIT_TEST
+	tristate
+	depends on SDXI_AMD_QOS
+	default SDXI_KUNIT_TEST
diff --git a/drivers/dma/sdxi/amd/Makefile b/drivers/dma/sdxi/amd/Makefile
index 5aee7e8a2414..7a7baad0247a 100644
--- a/drivers/dma/sdxi/amd/Makefile
+++ b/drivers/dma/sdxi/amd/Makefile
@@ -2,3 +2,5 @@
 
 obj-$(CONFIG_SDXI_AMD_QOS) += sdxi-amd-qos.o
 sdxi-amd-qos-objs := qos.o
+
+obj-$(CONFIG_SDXI_AMD_QOS_KUNIT_TEST) += qos_kunit.o
diff --git a/drivers/dma/sdxi/amd/qos_kunit.c b/drivers/dma/sdxi/amd/qos_kunit.c
new file mode 100644
index 000000000000..237e9ad79b19
--- /dev/null
+++ b/drivers/dma/sdxi/amd/qos_kunit.c
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * AMD SDXI QoS helper tests.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <kunit/test.h>
+#include <linux/bitfield.h>
+#include <linux/module.h>
+
+#include "qos.h"
+
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
+
+static void sdxi_kunit_qos_query_selector(struct kunit *t)
+{
+	u64 query_selector =
+		sdxi_amd_qos_query_selector(0x1234,
+					    SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG);
+
+	KUNIT_EXPECT_EQ(t, (u16)0x1234,
+			(u16)FIELD_GET(GENMASK_ULL(15, 0), query_selector));
+	KUNIT_EXPECT_EQ(t, (u8)SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG,
+			(u8)FIELD_GET(GENMASK_ULL(18, 16), query_selector));
+	KUNIT_EXPECT_EQ(t, 0U,
+			(u32)FIELD_GET(GENMASK_ULL(31, 19),
+					query_selector));
+}
+
+static void sdxi_kunit_qos_align_bw(struct kunit *t)
+{
+	KUNIT_EXPECT_EQ(t, 0U, sdxi_amd_qos_align_bw(39, 40));
+	KUNIT_EXPECT_EQ(t, 40U, sdxi_amd_qos_align_bw(40, 40));
+	KUNIT_EXPECT_EQ(t, 40U, sdxi_amd_qos_align_bw(79, 40));
+	KUNIT_EXPECT_EQ(t, 80U, sdxi_amd_qos_align_bw(80, 40));
+}
+
+static void sdxi_kunit_qos_validate_devupd(struct kunit *t)
+{
+	KUNIT_EXPECT_EQ(t, 0,
+			sdxi_amd_qos_validate_devupd_req(119, 120, 80, 40));
+	KUNIT_EXPECT_EQ(t, -EINVAL,
+			sdxi_amd_qos_validate_devupd_req(121, 120, 80, 40));
+	KUNIT_EXPECT_EQ(t, -EINVAL,
+			sdxi_amd_qos_validate_devupd_req(79, 120, 80, 40));
+}
+
+static void sdxi_kunit_qos_validate_poolupd(struct kunit *t)
+{
+	const struct sdxi_amd_qos_poolupd_req create = {
+		.pool_num = 1,
+		.pool_operation = SDXI_AMD_QOS_POOL_OP_CREATE,
+		.pool_minbw = 41,
+		.pool_maxbw = 79,
+	};
+	const struct sdxi_amd_qos_poolupd_req invalid_bw = {
+		.pool_num = 1,
+		.pool_operation = SDXI_AMD_QOS_POOL_OP_UPDATE,
+		.pool_minbw = 79,
+		.pool_maxbw = 41,
+	};
+	const struct sdxi_amd_qos_poolupd_req create_pool0 = {
+		.pool_operation = SDXI_AMD_QOS_POOL_OP_CREATE,
+	};
+	const struct sdxi_amd_qos_poolupd_req destroy_pool0 = {
+		.pool_operation = SDXI_AMD_QOS_POOL_OP_DESTROY,
+	};
+
+	KUNIT_EXPECT_EQ(t, 0,
+			sdxi_amd_qos_validate_poolupd_req(&create, 8, 40));
+	KUNIT_EXPECT_EQ(t, -EINVAL,
+			sdxi_amd_qos_validate_poolupd_req(&invalid_bw, 8, 40));
+	KUNIT_EXPECT_EQ(t, -EINVAL,
+			sdxi_amd_qos_validate_poolupd_req(&create_pool0, 8, 40));
+	KUNIT_EXPECT_EQ(t, -EINVAL,
+			sdxi_amd_qos_validate_poolupd_req(&destroy_pool0, 8, 40));
+}
+
+static void sdxi_kunit_qos_validate_fnupd(struct kunit *t)
+{
+	const struct sdxi_amd_qos_fnupd_req function = {
+		.pool_num = 1,
+		.vf_num = 4,
+		.cxt_start = 16,
+		.cxt_end = 16,
+	};
+	const struct sdxi_amd_qos_fnupd_req context = {
+		.pool_num = 1,
+		.flags = SDXI_AMD_QOS_TARGET_F_CXT,
+		.cxt_start = 1,
+		.cxt_end = 1,
+	};
+	const struct sdxi_amd_qos_fnupd_req invalid_context = {
+		.pool_num = 1,
+		.flags = SDXI_AMD_QOS_TARGET_F_CXT,
+		.cxt_start = 16,
+		.cxt_end = 16,
+	};
+	const struct sdxi_amd_qos_fnupd_req invalid_vf = {
+		.pool_num = 1,
+		.flags = SDXI_AMD_QOS_TARGET_F_VF,
+		.vf_num = 4,
+	};
+	const u16 features = SDXI_AMD_QOS_FEATURE_FLEXIBLE_POOL_SCOPE;
+	int ret;
+
+	KUNIT_EXPECT_EQ(t, 0,
+			sdxi_amd_qos_validate_fnupd_req(&function, 8, 0, 16, 0));
+	KUNIT_EXPECT_EQ(t, 0,
+			sdxi_amd_qos_validate_fnupd_req(&context, 8, 0, 16, 0));
+	ret = sdxi_amd_qos_validate_fnupd_req(&invalid_context, 8, 0, 16, features);
+	KUNIT_EXPECT_EQ(t, -EINVAL, ret);
+
+	ret = sdxi_amd_qos_validate_fnupd_req(&invalid_vf, 8, 4, 16, 0);
+	KUNIT_EXPECT_EQ(t, -EINVAL, ret);
+}
+
+static void sdxi_kunit_qos_validate_tel(struct kunit *t)
+{
+	const struct sdxi_amd_qos_tel_req device = {
+		.query_type = SDXI_AMD_QOS_TEL_QTYPE_DEVICE,
+	};
+	const struct sdxi_amd_qos_tel_req pool = {
+		.pool_num = 7,
+		.flags = SDXI_AMD_QOS_TARGET_F_VF,
+		.vf_num = 99,
+		.cxt_num = 99,
+		.query_type = SDXI_AMD_QOS_TEL_QTYPE_POOL_CONFIG,
+	};
+	const struct sdxi_amd_qos_tel_req unsupported = {
+		.flags = SDXI_AMD_QOS_TARGET_F_CXT,
+		.cxt_num = 1,
+		.query_type = SDXI_AMD_QOS_TEL_QTYPE_POOL_ASSIGN,
+	};
+	const struct sdxi_amd_qos_tel_req invalid_context = {
+		.flags = SDXI_AMD_QOS_TARGET_F_CXT,
+		.cxt_num = 16,
+		.query_type = SDXI_AMD_QOS_TEL_QTYPE_POOL_ASSIGN,
+	};
+	const struct sdxi_amd_qos_tel_req invalid_query = {
+		.query_type = 6,
+	};
+	const u16 features = SDXI_AMD_QOS_FEATURE_FLEXIBLE_POOL_SCOPE;
+	int ret;
+
+	KUNIT_EXPECT_EQ(t, 0,
+			sdxi_amd_qos_validate_tel_req(&device, 8, 4, 16, 0));
+	KUNIT_EXPECT_EQ(t, 0,
+			sdxi_amd_qos_validate_tel_req(&pool, 8, 4, 16, 0));
+	ret = sdxi_amd_qos_validate_tel_req(&unsupported, 8, 4, 16, 0);
+	KUNIT_EXPECT_EQ(t, -EOPNOTSUPP, ret);
+
+	ret = sdxi_amd_qos_validate_tel_req(&invalid_context, 8, 4, 16, features);
+	KUNIT_EXPECT_EQ(t, -EINVAL, ret);
+
+	ret = sdxi_amd_qos_validate_tel_req(&invalid_query, 8, 4, 16, 0);
+	KUNIT_EXPECT_EQ(t, -EINVAL, ret);
+}
+
+static struct kunit_case sdxi_qos_cases[] = {
+	KUNIT_CASE(sdxi_kunit_qos_query_selector),
+	KUNIT_CASE(sdxi_kunit_qos_align_bw),
+	KUNIT_CASE(sdxi_kunit_qos_validate_devupd),
+	KUNIT_CASE(sdxi_kunit_qos_validate_poolupd),
+	KUNIT_CASE(sdxi_kunit_qos_validate_fnupd),
+	KUNIT_CASE(sdxi_kunit_qos_validate_tel),
+	{}
+};
+
+static struct kunit_suite sdxi_qos_suite = {
+	.name = "AMD SDXI QoS helpers",
+	.test_cases = sdxi_qos_cases,
+};
+
+kunit_test_suite(sdxi_qos_suite);
+
+MODULE_DESCRIPTION("AMD SDXI QoS helper tests");
+MODULE_AUTHOR("Wei Huang");
+MODULE_LICENSE("GPL");
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 35/44] selftests: sdxi: add build scaffold
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (33 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 34/44] dmaengine: sdxi: amd: test QoS validation with KUnit Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 36/44] selftests: sdxi: add shared descriptor and context helpers Wei Huang
                   ` (8 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add the SDXI selftest directory and the initial build scaffold. Test
programs are added later by the patches that introduce individual test
sources.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/Makefile      | 1 +
 tools/testing/selftests/sdxi/Makefile | 5 +++++
 tools/testing/selftests/sdxi/config   | 2 ++
 3 files changed, 8 insertions(+)
 create mode 100644 tools/testing/selftests/sdxi/Makefile
 create mode 100644 tools/testing/selftests/sdxi/config

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8b04..fcc361c97224 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -122,6 +122,7 @@ TARGETS += rtc
 TARGETS += rust
 TARGETS += sched_ext
 TARGETS += seccomp
+TARGETS += sdxi
 TARGETS += sgx
 TARGETS += signal
 TARGETS += size
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
new file mode 100644
index 000000000000..107a9bd08f04
--- /dev/null
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -0,0 +1,5 @@
+werror := -Werror
+CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
+CPPFLAGS += -I../../../include/ -Ilib/
+
+include ../lib.mk
diff --git a/tools/testing/selftests/sdxi/config b/tools/testing/selftests/sdxi/config
new file mode 100644
index 000000000000..ac044d4da732
--- /dev/null
+++ b/tools/testing/selftests/sdxi/config
@@ -0,0 +1,2 @@
+CONFIG_SDXI_PCI=m
+CONFIG_SDXI_AMD_QOS=m
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 36/44] selftests: sdxi: add shared descriptor and context helpers
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (34 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 35/44] selftests: sdxi: add build scaffold Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 37/44] selftests: sdxi: test context creation Wei Huang
                   ` (7 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add shared helpers for opening SDXI devices, creating contexts and
building descriptors used by the functional selftests.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/sdxi/Makefile         |  28 +++
 tools/testing/selftests/sdxi/lib/context.c    | 195 +++++++++++++++++
 tools/testing/selftests/sdxi/lib/descriptor.h |  55 +++++
 .../selftests/sdxi/lib/fixture-context.h      | 126 +++++++++++
 .../selftests/sdxi/lib/fixture-device.h       |  44 ++++
 tools/testing/selftests/sdxi/lib/lib.c        | 206 ++++++++++++++++++
 tools/testing/selftests/sdxi/lib/lib.h        |  61 ++++++
 7 files changed, 715 insertions(+)
 create mode 100644 tools/testing/selftests/sdxi/lib/context.c
 create mode 100644 tools/testing/selftests/sdxi/lib/descriptor.h
 create mode 100644 tools/testing/selftests/sdxi/lib/fixture-context.h
 create mode 100644 tools/testing/selftests/sdxi/lib/fixture-device.h
 create mode 100644 tools/testing/selftests/sdxi/lib/lib.c
 create mode 100644 tools/testing/selftests/sdxi/lib/lib.h

diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index 107a9bd08f04..f2af46ac041f 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -3,3 +3,31 @@ CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUD
 CPPFLAGS += -I../../../include/ -Ilib/
 
 include ../lib.mk
+
+HOSTAR ?= ar
+
+LIBDIR	:= $(OUTPUT)/lib
+LIB	:= $(LIBDIR)/lib.a
+
+all: $(LIB)
+
+LIBDEPS := \
+	Makefile \
+	lib/descriptor.h \
+	lib/fixture-context.h \
+	lib/fixture-device.h \
+	lib/lib.h
+# Library
+LIBSRC	:= context.c lib.c
+LIBOBJ	:= $(LIBSRC:%.c=$(LIBDIR)/%.o)
+EXTRA_CLEAN += $(LIBOBJ) $(LIB)
+
+$(LIB): $(LIBOBJ)
+	$(HOSTAR) rcs $@ $^
+
+$(LIBDIR)/%.o: ./lib/%.c $(LIBDEPS)
+	mkdir -p $(LIBDIR)
+	$(CC) $< $(CFLAGS) $(CPPFLAGS) -o $@ -c
+
+$(TEST_GEN_PROGS): $(LIB)
+$(TEST_GEN_PROGS_EXTENDED): $(LIB)
diff --git a/tools/testing/selftests/sdxi/lib/context.c b/tools/testing/selftests/sdxi/lib/context.c
new file mode 100644
index 000000000000..0fde2d22f3b8
--- /dev/null
+++ b/tools/testing/selftests/sdxi/lib/context.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI selftest context helpers.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/kernel.h>
+#include <linux/sdxi.h>
+
+#include "lib.h"
+
+#define alloc(...)					\
+	({						\
+		size_t sz = sizeof(__VA_ARGS__);	\
+		void *ptr = malloc(sz);			\
+		if (ptr)				\
+			memset(ptr, 0, sz);		\
+		(typeof(__VA_ARGS__) *)ptr;		\
+	})
+
+enum {
+	SELFTEST_SDXI_CAP0_DB_STRIDE_SHIFT = 20,
+	SELFTEST_SDXI_CAP0_DB_STRIDE_MASK = 0x7,
+};
+
+static size_t sdxi_page_size(void)
+{
+	long page_size = sysconf(_SC_PAGESIZE);
+
+	if (page_size <= 0)
+		return 4096;
+
+	return (size_t)page_size;
+}
+
+size_t sdxi_context_db_stride_bytes(int device_fd)
+{
+	struct sdxi_dev_info_args info = {};
+	uint64_t shift;
+
+	info.argsz = sizeof(info);
+	if (ioctl(device_fd, SDXI_GET_DEV_INFO, &info) != 0)
+		return 0;
+
+	shift = (info.cap0 >> SELFTEST_SDXI_CAP0_DB_STRIDE_SHIFT) &
+		SELFTEST_SDXI_CAP0_DB_STRIDE_MASK;
+
+	return 4096ull << shift;
+}
+
+static void sdxi_context_munmap_safe(void *addr, size_t bytes)
+{
+	if (!addr || addr == MAP_FAILED || bytes == 0)
+		return;
+
+	(void)munmap(addr, bytes);
+}
+
+struct sdxi_context *sdxi_context_create(void)
+{
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(struct sdxi_create_cxt_args),
+		.ds_ring_entries = 1024,
+	};
+	struct sdxi_context *cxt;
+	void *doorbell = MAP_FAILED;
+	void *write_idx = MAP_FAILED;
+	void *cxt_sts = MAP_FAILED;
+	void *ring = MAP_FAILED;
+	size_t page_size;
+	size_t doorbell_bytes;
+	size_t ring_bytes;
+	int cxt_fd = -1;
+	int device_fd;
+	int err;
+
+	cxt = alloc(*cxt);
+	if (!cxt)
+		return NULL;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDWR | O_CLOEXEC);
+	if (device_fd < 0)
+		goto free_cxt;
+
+	page_size = sdxi_page_size();
+	doorbell_bytes = sdxi_context_db_stride_bytes(device_fd);
+	if (doorbell_bytes == 0)
+		goto close_fd;
+
+	ring_bytes = (size_t)arg.ds_ring_entries * sizeof(cxt->ring->entry[0]);
+	cxt_sts = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+		       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	write_idx = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+			 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	ring = mmap(NULL, ring_bytes, PROT_READ | PROT_WRITE,
+		    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	if (cxt_sts == MAP_FAILED || write_idx == MAP_FAILED ||
+	    ring == MAP_FAILED)
+		goto unmap_local;
+
+	arg.cxt_sts_base = (uint64_t)(uintptr_t)cxt_sts;
+	arg.write_index_base = (uint64_t)(uintptr_t)write_idx;
+	arg.ds_ring_base = (uint64_t)(uintptr_t)ring;
+
+	err = ioctl(device_fd, SDXI_CREATE_CXT, &arg);
+	if (err)
+		goto unmap_local;
+
+	cxt_fd = arg.cxt_fd;
+	if (cxt_fd < 0)
+		goto unmap_local;
+
+	ring_bytes = (size_t)arg.ds_ring_entries * sizeof(cxt->ring->entry[0]);
+
+	doorbell = mmap(NULL, doorbell_bytes, PROT_WRITE, MAP_SHARED,
+			cxt_fd, arg.doorbell_mmap_base);
+	if (doorbell == MAP_FAILED)
+		goto unmap;
+
+	*cxt = (struct sdxi_context) {
+		.device_fd = device_fd,
+		.cxt_fd = cxt_fd,
+		.pasid = arg.pasid,
+		.doorbell = doorbell,
+		.doorbell_bytes = doorbell_bytes,
+		.write_idx = write_idx,
+		.write_idx_bytes = page_size,
+		.cxt_sts = cxt_sts,
+		.cxt_sts_bytes = page_size,
+		.ring = ring,
+		.ring_bytes = ring_bytes,
+		.n_entries = arg.ds_ring_entries,
+	};
+
+	return cxt;
+
+unmap:
+	(void)ioctl(cxt_fd, SDXI_QUIESCE_CXT,
+	      &(struct sdxi_quiesce_cxt_args) {
+		      .argsz = sizeof(struct sdxi_quiesce_cxt_args),
+	      });
+	sdxi_context_munmap_safe(doorbell, doorbell_bytes);
+unmap_local:
+	sdxi_context_munmap_safe(write_idx, page_size);
+	sdxi_context_munmap_safe(cxt_sts, page_size);
+	sdxi_context_munmap_safe(ring,
+				 (size_t)arg.ds_ring_entries *
+				 sizeof(cxt->ring->entry[0]));
+close_fd:
+	if (cxt_fd >= 0)
+		(void)close(cxt_fd);
+	(void)close(device_fd);
+free_cxt:
+	free(cxt);
+	return NULL;
+}
+
+void sdxi_context_destroy(struct sdxi_context *cxt)
+{
+	if (!cxt)
+		return;
+
+	(void)ioctl(cxt->cxt_fd, SDXI_QUIESCE_CXT,
+	      &(struct sdxi_quiesce_cxt_args) {
+		      .argsz = sizeof(struct sdxi_quiesce_cxt_args),
+	      });
+	sdxi_context_munmap_safe(cxt->doorbell, cxt->doorbell_bytes);
+	sdxi_context_munmap_safe(cxt->write_idx, cxt->write_idx_bytes);
+	sdxi_context_munmap_safe(cxt->cxt_sts, cxt->cxt_sts_bytes);
+	sdxi_context_munmap_safe(cxt->ring, cxt->ring_bytes);
+	(void)close(cxt->cxt_fd);
+	(void)close(cxt->device_fd);
+	free(cxt);
+}
+
+bool sdxi_context_running(const struct sdxi_context *cxt)
+{
+	struct sdxi_cxt_sts *sts = cxt->cxt_sts;
+	enum {
+		CXTV_RUN = 1,
+	};
+
+	return (READ_ONCE(sts->state) & 0xf) == CXTV_RUN;
+}
diff --git a/tools/testing/selftests/sdxi/lib/descriptor.h b/tools/testing/selftests/sdxi/lib/descriptor.h
new file mode 100644
index 000000000000..9a0d88248dec
--- /dev/null
+++ b/tools/testing/selftests/sdxi/lib/descriptor.h
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI selftest descriptor encoders.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef SELFTEST_SDXI_DESCRIPTOR_H
+#define SELFTEST_SDXI_DESCRIPTOR_H
+
+#include "linux/build_bug.h"
+#include "linux/compiler.h"
+#include "linux/types.h"
+
+// Size of the "body" of each descriptor between the common opcode and
+// csb_ptr fields.
+#define SDXI_DESC_PAYLOAD_BYTES 52
+
+#define declare_sdxi_desc_payload(_name, _layout)			\
+	struct _name _layout __packed;					\
+	static_assert(sizeof(struct _name) == SDXI_DESC_PAYLOAD_BYTES,	\
+		      "descriptor payload size mismatch")
+
+declare_sdxi_desc_payload(sdxi_desc_nop, {
+		__u8 empty[SDXI_DESC_PAYLOAD_BYTES];
+	});
+
+declare_sdxi_desc_payload(sdxi_desc_copy, {
+		__le32 size;
+		__u8   attr;
+		__u8   rsvd_0[3];
+		__le16 akey0;
+		__le16 akey1;
+		__le64 addr0;
+		__le64 addr1;
+		__u8   rsvd_1[24];
+	});
+
+struct sdxi_desc {
+	union {
+		struct {
+			__le32 opcode;
+			union {
+				__u8   operation[SDXI_DESC_PAYLOAD_BYTES];
+				struct sdxi_desc_nop nop;
+				struct sdxi_desc_copy copy;
+			};
+			__le64 csb_ptr;
+		};
+		__le64 qw[8];
+	};
+} __packed __aligned(64);
+static_assert(sizeof(struct sdxi_desc) == 64, "descriptor size mismatch");
+
+#endif /* SELFTEST_SDXI_DESCRIPTOR_H */
diff --git a/tools/testing/selftests/sdxi/lib/fixture-context.h b/tools/testing/selftests/sdxi/lib/fixture-context.h
new file mode 100644
index 000000000000..6eb95e819236
--- /dev/null
+++ b/tools/testing/selftests/sdxi/lib/fixture-context.h
@@ -0,0 +1,126 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI selftest context fixture.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef SELFTEST_SDXI_FIXTURE_CONTEXT_H
+#define SELFTEST_SDXI_FIXTURE_CONTEXT_H
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/sdxi.h>
+
+#include "lib.h"
+
+#include "../../kselftest_harness.h"
+
+FIXTURE(sdxi_context) {
+	struct sdxi_context context;
+};
+
+FIXTURE_SETUP(sdxi_context)
+{
+	struct sdxi_context *cxt = &self->context;
+	int cxt_fd;
+	int device_fd;
+	long page_size;
+	size_t doorbell_bytes;
+	size_t ring_bytes;
+	void *cxt_sts;
+	void *ring;
+	void *write_idx;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDWR | O_CLOEXEC);
+	ASSERT_GE(device_fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return,
+			     "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return,
+			     "insufficient privileges");
+	}
+
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(struct sdxi_create_cxt_args),
+		.ds_ring_entries = 1024,
+	};
+
+	page_size = sysconf(_SC_PAGESIZE);
+	ASSERT_GT(page_size, 0);
+	ring_bytes = (size_t)arg.ds_ring_entries * sizeof(cxt->ring->entry[0]);
+
+	cxt_sts = mmap(NULL, (size_t)page_size, PROT_READ | PROT_WRITE,
+		       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	write_idx = mmap(NULL, (size_t)page_size, PROT_READ | PROT_WRITE,
+			 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	ring = mmap(NULL, ring_bytes, PROT_READ | PROT_WRITE,
+		    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+
+	ASSERT_NE(cxt_sts, MAP_FAILED);
+	ASSERT_NE(write_idx, MAP_FAILED);
+	ASSERT_NE(ring, MAP_FAILED);
+
+	/* Fault in and invalidate the ring before the device can fetch it. */
+	memset(ring, 0, ring_bytes);
+
+	arg.cxt_sts_base = (uint64_t)(uintptr_t)cxt_sts;
+	arg.write_index_base = (uint64_t)(uintptr_t)write_idx;
+	arg.ds_ring_base = (uint64_t)(uintptr_t)ring;
+
+	ASSERT_EQ(ioctl(device_fd, SDXI_CREATE_CXT, &arg), 0);
+	ASSERT_GE(arg.cxt_fd, 0);
+	cxt_fd = arg.cxt_fd;
+	doorbell_bytes = sdxi_context_db_stride_bytes(device_fd);
+	ASSERT_GT(doorbell_bytes, 0U);
+
+	*cxt = (struct sdxi_context) {
+		.device_fd = device_fd,
+		.cxt_fd = cxt_fd,
+		.pasid = arg.pasid,
+		.doorbell = mmap(NULL, doorbell_bytes,
+				 PROT_WRITE, MAP_SHARED,
+				 cxt_fd, arg.doorbell_mmap_base),
+		.doorbell_bytes = doorbell_bytes,
+		.write_idx = write_idx,
+		.write_idx_bytes = (size_t)page_size,
+		.cxt_sts = cxt_sts,
+		.cxt_sts_bytes = (size_t)page_size,
+		.ring = ring,
+		.ring_bytes = ring_bytes,
+		.n_entries = arg.ds_ring_entries,
+	};
+
+	EXPECT_NE(cxt->doorbell, MAP_FAILED);
+	EXPECT_NE(cxt->write_idx, MAP_FAILED);
+	EXPECT_NE(cxt->cxt_sts, MAP_FAILED);
+	EXPECT_NE(cxt->ring, MAP_FAILED);
+}
+
+FIXTURE_TEARDOWN(sdxi_context)
+{
+	struct sdxi_context *cxt = &self->context;
+	struct sdxi_quiesce_cxt_args quiesce = {};
+
+	quiesce.argsz = sizeof(quiesce);
+
+	EXPECT_EQ(ioctl(cxt->cxt_fd, SDXI_QUIESCE_CXT, &quiesce),
+		  0);
+	EXPECT_EQ(munmap(cxt->doorbell, cxt->doorbell_bytes), 0);
+	EXPECT_EQ(munmap(cxt->write_idx, cxt->write_idx_bytes), 0);
+	EXPECT_EQ(munmap(cxt->cxt_sts, cxt->cxt_sts_bytes), 0);
+	EXPECT_EQ(munmap(cxt->ring, cxt->ring_bytes), 0);
+	EXPECT_EQ(close(cxt->cxt_fd), 0);
+	EXPECT_EQ(close(cxt->device_fd), 0);
+}
+
+#endif /* SELFTEST_SDXI_FIXTURE_CONTEXT_H */
diff --git a/tools/testing/selftests/sdxi/lib/fixture-device.h b/tools/testing/selftests/sdxi/lib/fixture-device.h
new file mode 100644
index 000000000000..bd6a26151732
--- /dev/null
+++ b/tools/testing/selftests/sdxi/lib/fixture-device.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI selftest device fixture.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef SELFTEST_SDXI_FIXTURE_DEVICE_H
+#define SELFTEST_SDXI_FIXTURE_DEVICE_H
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
+#include <linux/sdxi.h>
+
+#include "lib.h"
+
+#include "../../kselftest_harness.h"
+
+FIXTURE(dev_sdxi) {
+	int fd;
+};
+
+FIXTURE_SETUP(dev_sdxi) {
+	errno = 0;
+	self->fd = open("/dev/sdxi0", O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(self->fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return,
+			     "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return,
+			     "insufficient privileges");
+	}
+}
+
+FIXTURE_TEARDOWN(dev_sdxi) {
+	EXPECT_EQ(close(self->fd), 0);
+}
+
+#endif /* SELFTEST_SDXI_FIXTURE_DEVICE_H */
diff --git a/tools/testing/selftests/sdxi/lib/lib.c b/tools/testing/selftests/sdxi/lib/lib.c
new file mode 100644
index 000000000000..26a9470d2c97
--- /dev/null
+++ b/tools/testing/selftests/sdxi/lib/lib.c
@@ -0,0 +1,206 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI selftest library helpers.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <stdatomic.h>
+#include <time.h>
+#include <errno.h>
+
+#include "linux/kernel.h"
+#include "linux/bitfield.h"
+#include "linux/bits.h"
+#include "linux/types.h"
+
+#include "descriptor.h"
+#include "lib.h"
+
+#define DESC_SUBTYPE GENMASK(15, 8)
+#define DESC_TYPE GENMASK(26, 16)
+#define DESC_VL  BIT(0)
+#define DESC_SE  BIT(1)
+#define DESC_FE  BIT(2)
+#define DESC_CH  BIT(3)
+#define DESC_CSR BIT(4)
+#define DESC_RB BIT(5)
+
+#define DESC_NP BIT_ULL(0)
+#define DESC_CSB_PTR GENMASK_ULL(63, 5)
+
+enum sdxi_op_group {
+	OPGRP_DMAB = 0x001,
+};
+
+enum sdxi_operation {
+	DMAB_NOP = 0x01,
+	DMAB_COPY = 0x03,
+};
+
+struct sdxi_desc sdxi_dsc_encode_rsvd(void)
+{
+	u32 opcode =
+		FIELD_PREP(DESC_VL, 1) |
+		FIELD_PREP(DESC_SE, 0) |
+		FIELD_PREP(DESC_FE, 1) |
+		FIELD_PREP(DESC_CH, 0) |
+		FIELD_PREP(DESC_CSR, 1) |
+		FIELD_PREP(DESC_SUBTYPE, 0) |
+		FIELD_PREP(DESC_TYPE, 0);
+	u64 csb_ptr =
+		FIELD_PREP(DESC_NP, 1) |
+		FIELD_PREP(DESC_CSB_PTR, 0);
+
+	return (struct sdxi_desc) {
+		.opcode = cpu_to_le32(opcode),
+		.csb_ptr = cpu_to_le64(csb_ptr),
+	};
+}
+
+struct sdxi_desc sdxi_dsc_encode_nop(void)
+{
+	u32 opcode =
+		FIELD_PREP(DESC_VL, 1) |
+		FIELD_PREP(DESC_SE, 0) |
+		FIELD_PREP(DESC_FE, 1) |
+		FIELD_PREP(DESC_CH, 0) |
+		FIELD_PREP(DESC_CSR, 1) |
+		FIELD_PREP(DESC_SUBTYPE, DMAB_NOP) |
+		FIELD_PREP(DESC_TYPE, OPGRP_DMAB);
+	u64 csb_ptr =
+		FIELD_PREP(DESC_NP, 1) |
+		FIELD_PREP(DESC_CSB_PTR, 0);
+
+	return (struct sdxi_desc) {
+		.opcode = cpu_to_le32(opcode),
+		.csb_ptr = cpu_to_le64(csb_ptr),
+	};
+}
+
+struct sdxi_desc sdxi_dsc_encode_copy(void *dest, const void *src, size_t n)
+{
+	u32 opcode =
+		FIELD_PREP(DESC_VL, 1) |
+		FIELD_PREP(DESC_SE, 0) |
+		FIELD_PREP(DESC_FE, 1) |
+		FIELD_PREP(DESC_CH, 0) |
+		FIELD_PREP(DESC_CSR, 1) |
+		FIELD_PREP(DESC_SUBTYPE, DMAB_COPY) |
+		FIELD_PREP(DESC_TYPE, OPGRP_DMAB);
+	u64 csb_ptr =
+		FIELD_PREP(DESC_NP, 1) |
+		FIELD_PREP(DESC_CSB_PTR, 0);
+
+	return (struct sdxi_desc) {
+		.opcode = cpu_to_le32(opcode),
+		.csb_ptr = cpu_to_le64(csb_ptr),
+
+		.copy = (struct sdxi_desc_copy) {
+			.size = cpu_to_le32(n - 1),
+
+			// Note that akeys are hardcoded to 1 here and
+			// in the driver.
+			.akey0 = cpu_to_le16(1),
+			.akey1 = cpu_to_le16(1),
+			.addr0 = cpu_to_le64((uintptr_t)src),
+			.addr1 = cpu_to_le64((uintptr_t)dest),
+		},
+	};
+}
+
+static void copy_desc_to_ring(struct descriptor_ring *ring, size_t idx,
+			      const struct sdxi_desc *src)
+{
+	struct sdxi_desc *dst;
+
+	idx = idx % ARRAY_SIZE(ring->entry);
+	dst = &ring->entry[idx];
+
+	for (size_t i = 1; i < ARRAY_SIZE(src->qw); ++i)
+		dst->qw[i] = src->qw[i];
+
+	atomic_thread_fence(memory_order_acq_rel);
+
+	dst->qw[0] = src->qw[0];
+}
+
+static u64 sample_read_index(const struct sdxi_context *cxt)
+{
+	return le64_to_cpu(READ_ONCE(cxt->cxt_sts->read_index));
+}
+
+int sdxi_submit_async(struct sdxi_context *cxt, const struct sdxi_desc *desc, u64 *pos)
+{
+	u64 read, old_write, new_write;
+
+	if (!sdxi_context_running(cxt))
+		return -EIO;
+
+	read = le64_to_cpu(READ_ONCE(cxt->cxt_sts->read_index));
+	atomic_thread_fence(memory_order_acq_rel);
+	old_write = le64_to_cpu(READ_ONCE(*cxt->write_idx));
+
+	if (read > old_write)
+		return -EINVAL;
+
+	new_write = old_write + 1;
+	if (new_write - read > cxt->n_entries)
+		return -ENOSPC;
+
+	WRITE_ONCE(*cxt->write_idx, cpu_to_le64(new_write));
+	atomic_thread_fence(memory_order_acq_rel);
+	copy_desc_to_ring(cxt->ring, old_write, desc);
+	WRITE_ONCE(*cxt->doorbell, cpu_to_le64(new_write));
+
+	if (pos)
+		*pos = old_write;
+	return 0;
+}
+
+// Submit nops until the function increments the read index or the
+// threshold is reached. Sleeps 1ms between each nop.
+static int force_read_index_advance(struct sdxi_context *cxt, u64 target)
+{
+	struct sdxi_desc nop = sdxi_dsc_encode_nop();
+
+	for (size_t tries = 1000; tries > 0; --tries) {
+		if (sample_read_index(cxt) >= target)
+			return 0;
+		sdxi_submit_async(cxt, &nop, NULL);
+		nanosleep(&(struct timespec) {
+				.tv_nsec = 1000000,
+			  }, NULL);
+	}
+
+	fprintf(stderr, "Failed to force read index advance to %llu\n",
+		(unsigned long long)target);
+	return -ETIMEDOUT;
+}
+
+int sdxi_submit_sync(struct sdxi_context *cxt, const struct sdxi_desc *desc)
+{
+	u64 pos;
+	int err;
+
+	err = sdxi_submit_async(cxt, desc, &pos);
+	if (err)
+		return err;
+
+	return force_read_index_advance(cxt, pos + 1);
+}
+
+int sdxi_submit_oneshot(const struct sdxi_desc *desc)
+{
+	struct sdxi_context *context = sdxi_context_create();
+	int err;
+
+	if (!context)
+		return -EIO;
+
+	err = sdxi_submit_async(context, desc, NULL);
+
+	sdxi_context_destroy(context);
+
+	return err;
+}
diff --git a/tools/testing/selftests/sdxi/lib/lib.h b/tools/testing/selftests/sdxi/lib/lib.h
new file mode 100644
index 000000000000..38e7d665085e
--- /dev/null
+++ b/tools/testing/selftests/sdxi/lib/lib.h
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SDXI selftest library helpers.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#ifndef SELFTEST_SDXI_LIB_H
+#define SELFTEST_SDXI_LIB_H
+
+#include <inttypes.h>
+#include <stdbool.h>
+
+#include "descriptor.h"
+#include "linux/build_bug.h"
+#include "linux/types.h"
+
+struct sdxi_cxt_sts {
+	__u8   state;
+	__u8   misc0;
+	__u8   rsvd_0[6];
+	__le64 read_index;
+};
+
+enum {
+	SDXI_DESC_RING_ENTRIES = 1024,
+};
+
+struct descriptor_ring {
+	struct sdxi_desc entry[SDXI_DESC_RING_ENTRIES];
+};
+
+struct sdxi_context {
+	int device_fd;
+	int cxt_fd;
+	__u32 pasid;
+	__le64 *doorbell;
+	size_t doorbell_bytes;
+	__le64 *write_idx;
+	size_t write_idx_bytes;
+	size_t n_entries;
+	struct sdxi_cxt_sts *cxt_sts;
+	size_t cxt_sts_bytes;
+	struct descriptor_ring *ring;
+	size_t ring_bytes;
+};
+
+size_t sdxi_context_db_stride_bytes(int device_fd);
+struct sdxi_context *sdxi_context_create(void);
+void sdxi_context_destroy(struct sdxi_context *cxt);
+bool sdxi_context_running(const struct sdxi_context *cxt);
+
+struct sdxi_desc sdxi_dsc_encode_copy(void *dest, const void *src, size_t n);
+struct sdxi_desc sdxi_dsc_encode_nop(void);
+struct sdxi_desc sdxi_dsc_encode_rsvd(void);
+
+int sdxi_submit_async(struct sdxi_context *cxt, const struct sdxi_desc *desc, u64 *pos);
+int sdxi_submit_sync(struct sdxi_context *cxt, const struct sdxi_desc *desc);
+int sdxi_submit_oneshot(const struct sdxi_desc *desc);
+
+#endif/* SELFTEST_SDXI_LIB_H */
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 37/44] selftests: sdxi: test context creation
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (35 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 36/44] selftests: sdxi: add shared descriptor and context helpers Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 38/44] selftests: sdxi: test NOP descriptor submission Wei Huang
                   ` (6 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add basic context creation smoke test for the SDXI selftest suite.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore |   1 +
 tools/testing/selftests/sdxi/Makefile   |   2 +
 tools/testing/selftests/sdxi/context.c  | 343 ++++++++++++++++++++++++
 3 files changed, 346 insertions(+)
 create mode 100644 tools/testing/selftests/sdxi/.gitignore
 create mode 100644 tools/testing/selftests/sdxi/context.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
new file mode 100644
index 000000000000..9e3f2c52330c
--- /dev/null
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -0,0 +1 @@
+/context
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index f2af46ac041f..dba5d7df3872 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,6 +2,8 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
+TEST_GEN_PROGS := context
+
 include ../lib.mk
 
 HOSTAR ?= ar
diff --git a/tools/testing/selftests/sdxi/context.c b/tools/testing/selftests/sdxi/context.c
new file mode 100644
index 000000000000..8649b76f60e3
--- /dev/null
+++ b/tools/testing/selftests/sdxi/context.c
@@ -0,0 +1,343 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI context ioctl selftests.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/eventfd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/sdxi.h>
+
+#include "lib.h"
+#include "lib/fixture-context.h"
+#include "../kselftest_harness.h"
+
+#define AKEY_ENT_VL		(1U << 0)
+#define AKEY_ENT_IV		(1U << 1)
+#define AKEY_ENT_PV		(1U << 2)
+#define AKEY_ENT_STE		(1U << 3)
+#define AKEY_ENT_PH_SHIFT	30
+#define AKEY_ENT_INTR_SHIFT	4
+#define AKEY_ENT_INTR_MAX	((1U << 11) - 1)
+
+static void akey_pack_intr(uint32_t data[4], uint32_t pasid,
+			   uint32_t intr_num, bool iv)
+{
+	memset(data, 0, sizeof(uint32_t) * 4);
+	data[0] = AKEY_ENT_VL | AKEY_ENT_PV;
+	if (iv)
+		data[0] |= AKEY_ENT_IV;
+	data[0] |= (intr_num & AKEY_ENT_INTR_MAX) << AKEY_ENT_INTR_SHIFT;
+	data[1] = pasid;
+}
+
+static int set_single_akey(int cxt_fd, uint16_t index, const uint32_t data[4])
+{
+	struct sdxi_set_keys_args *args;
+	size_t bytes = sizeof(*args) + sizeof(args->entries[0]);
+	int rc;
+
+	args = calloc(1, bytes);
+	if (!args)
+		return -ENOMEM;
+
+	args->argsz = sizeof(*args);
+	args->nkeys = 1;
+	args->entries[0].index = index;
+	memcpy(args->entries[0].data, data, sizeof(args->entries[0].data));
+
+	rc = ioctl(cxt_fd, SDXI_SET_AKEYS, args);
+	rc = rc ? -errno : 0;
+
+	free(args);
+	return rc;
+}
+
+static int get_single_akey(int cxt_fd, uint16_t index, uint32_t data[4])
+{
+	struct sdxi_get_keys_args *args;
+	size_t bytes = sizeof(*args) + sizeof(args->entries[0]);
+	int rc;
+
+	args = calloc(1, bytes);
+	if (!args)
+		return -ENOMEM;
+
+	args->argsz = sizeof(*args);
+	args->nkeys = 1;
+	args->entries[0].index = index;
+
+	rc = ioctl(cxt_fd, SDXI_GET_AKEYS, args);
+	rc = rc ? -errno : 0;
+	if (!rc)
+		memcpy(data, args->entries[0].data, sizeof(args->entries[0].data));
+
+	free(args);
+	return rc;
+}
+
+// This basically duplicates the normal context fixture, maybe this
+// could be fixed by using a variant?
+FIXTURE(sdxi_context_nomap) {
+	int cxt_fd;
+	int device_fd;
+	void *write_idx;
+	size_t write_idx_bytes;
+	void *cxt_sts;
+	size_t cxt_sts_bytes;
+	void *ring;
+	size_t ring_bytes;
+};
+
+FIXTURE_SETUP(sdxi_context_nomap)
+{
+	int device_fd;
+	long page_size;
+	size_t ring_bytes;
+	void *cxt_sts;
+	void *ring;
+	void *write_idx;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(device_fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return,
+			     "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return,
+			     "insufficient privileges");
+	}
+
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(struct sdxi_create_cxt_args),
+		.ds_ring_entries = 1024,
+	};
+
+	page_size = sysconf(_SC_PAGESIZE);
+	ASSERT_GT(page_size, 0);
+	ring_bytes = (size_t)arg.ds_ring_entries * sizeof(struct sdxi_desc);
+
+	cxt_sts = mmap(NULL, (size_t)page_size, PROT_READ | PROT_WRITE,
+		       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	write_idx = mmap(NULL, (size_t)page_size, PROT_READ | PROT_WRITE,
+			 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	ring = mmap(NULL, ring_bytes, PROT_READ | PROT_WRITE,
+		    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+
+	ASSERT_NE(cxt_sts, MAP_FAILED);
+	ASSERT_NE(write_idx, MAP_FAILED);
+	ASSERT_NE(ring, MAP_FAILED);
+
+	/* Fault in and invalidate the ring before the device can fetch it. */
+	memset(ring, 0, ring_bytes);
+
+	arg.cxt_sts_base = (uint64_t)(uintptr_t)cxt_sts;
+	arg.write_index_base = (uint64_t)(uintptr_t)write_idx;
+	arg.ds_ring_base = (uint64_t)(uintptr_t)ring;
+
+	ASSERT_EQ(ioctl(device_fd, SDXI_CREATE_CXT, &arg), 0);
+
+	self->cxt_fd = arg.cxt_fd;
+	self->device_fd = device_fd;
+	self->write_idx = write_idx;
+	self->write_idx_bytes = (size_t)page_size;
+	self->cxt_sts = cxt_sts;
+	self->cxt_sts_bytes = (size_t)page_size;
+	self->ring = ring;
+	self->ring_bytes = ring_bytes;
+}
+
+FIXTURE_TEARDOWN(sdxi_context_nomap) {
+	int cxt_fd = self->cxt_fd;
+	int device_fd = self->device_fd;
+	struct sdxi_quiesce_cxt_args quiesce = {};
+
+	quiesce.argsz = sizeof(quiesce);
+
+	EXPECT_EQ(ioctl(cxt_fd, SDXI_QUIESCE_CXT, &quiesce),
+		  0);
+	EXPECT_EQ(munmap(self->write_idx, self->write_idx_bytes), 0);
+	EXPECT_EQ(munmap(self->cxt_sts, self->cxt_sts_bytes), 0);
+	EXPECT_EQ(munmap(self->ring, self->ring_bytes), 0);
+	EXPECT_EQ(close(cxt_fd), 0);
+	EXPECT_EQ(close(device_fd), 0);
+}
+
+TEST_F(sdxi_context_nomap, context_create_and_destroy_nomap)
+{
+	// Just run the fixture setup and teardown
+}
+
+TEST(sdxi_create_cxt_rejects_nonzero_flags)
+{
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(arg),
+		.flags = 1,
+		.ds_ring_entries = 1024,
+	};
+	int device_fd;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(device_fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return, "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return, "insufficient privileges");
+	}
+
+	errno = 0;
+	EXPECT_EQ(ioctl(device_fd, SDXI_CREATE_CXT, &arg), -1);
+	EXPECT_EQ(errno, EINVAL);
+	EXPECT_EQ(close(device_fd), 0);
+}
+
+TEST(sdxi_create_cxt_requires_user_dataplane)
+{
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(arg),
+		.ds_ring_entries = 1024,
+	};
+	int device_fd;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(device_fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return, "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return, "insufficient privileges");
+	}
+
+	errno = 0;
+	EXPECT_EQ(ioctl(device_fd, SDXI_CREATE_CXT, &arg), -1);
+	EXPECT_EQ(errno, EINVAL);
+	EXPECT_EQ(close(device_fd), 0);
+}
+
+TEST_F(sdxi_context, context_create_and_destroy)
+{
+	EXPECT_TRUE(sdxi_context_running(&self->context));
+}
+
+TEST_F(sdxi_context, cxt_eventfd_bind_unbind)
+{
+	struct sdxi_cxt_eventfd_args args = {};
+	int efd;
+	int rc;
+
+	args.argsz = sizeof(args);
+
+	efd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
+	ASSERT_GE(efd, 0);
+
+	args.eventfd = efd;
+	rc = ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args);
+	if (rc != 0) {
+		if (errno == EOPNOTSUPP) {
+			close(efd);
+			SKIP(return, "context interrupts not supported");
+		}
+		ASSERT_EQ(rc, 0);
+	}
+
+	EXPECT_GE(args.intr_akey, 2);
+	EXPECT_GT(args.intr_num, 0U);
+
+	args.eventfd = -1;
+	args.argsz = sizeof(args);
+	EXPECT_EQ(ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args), 0);
+	EXPECT_EQ(args.intr_akey, 0);
+	EXPECT_EQ(args.intr_num, 0U);
+
+	close(efd);
+}
+
+TEST_F(sdxi_context, akey_iv_requires_cxt_eventfd)
+{
+	uint32_t data[4];
+	int rc;
+
+	akey_pack_intr(data, self->context.pasid, 1, true);
+	rc = set_single_akey(self->context.cxt_fd, 2, data);
+	if (rc == -EOPNOTSUPP)
+		SKIP(return, "context interrupts not supported");
+
+	EXPECT_EQ(rc, -ENOTCONN);
+}
+
+TEST_F(sdxi_context, akey_iv_requires_matching_intr_num)
+{
+	struct sdxi_cxt_eventfd_args args = {};
+	uint32_t data[4];
+	uint32_t wrong_intr;
+	uint16_t test_akey;
+	int efd;
+	int rc;
+
+	args.argsz = sizeof(args);
+
+	efd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
+	ASSERT_GE(efd, 0);
+
+	args.eventfd = efd;
+	rc = ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args);
+	if (rc != 0) {
+		if (errno == EOPNOTSUPP) {
+			close(efd);
+			SKIP(return, "context interrupts not supported");
+		}
+		ASSERT_EQ(rc, 0);
+	}
+
+	ASSERT_NE(args.intr_akey, UINT16_MAX);
+	test_akey = args.intr_akey + 1;
+
+	wrong_intr = (args.intr_num == 1U) ? 2U : 1U;
+	akey_pack_intr(data, self->context.pasid, wrong_intr, true);
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, test_akey, data),
+		  -EINVAL);
+
+	akey_pack_intr(data, self->context.pasid, args.intr_num, true);
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, test_akey, data), 0);
+
+	args.eventfd = -1;
+	args.argsz = sizeof(args);
+	EXPECT_EQ(ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args), 0);
+	close(efd);
+}
+
+TEST_F(sdxi_context, akey_user_rejects_dsh_fields)
+{
+	uint32_t data[4];
+
+	if (geteuid() == 0)
+		SKIP(return, "non-admin AKey validation test");
+
+	ASSERT_EQ(get_single_akey(self->context.cxt_fd, 1, data), 0);
+
+	data[0] |= AKEY_ENT_STE;
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, 2, data), -EPERM);
+
+	ASSERT_EQ(get_single_akey(self->context.cxt_fd, 1, data), 0);
+
+	data[1] |= 1U << AKEY_ENT_PH_SHIFT;
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, 2, data), -EPERM);
+
+	ASSERT_EQ(get_single_akey(self->context.cxt_fd, 1, data), 0);
+
+	data[2] = 1;
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, 2, data), -EPERM);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 38/44] selftests: sdxi: test NOP descriptor submission
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (36 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 37/44] selftests: sdxi: test context creation Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 39/44] selftests: sdxi: test copy submission and ring wrap Wei Huang
                   ` (5 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add a NOP descriptor submission and completion test to the SDXI
selftest suite.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore |  1 +
 tools/testing/selftests/sdxi/Makefile   |  2 +-
 tools/testing/selftests/sdxi/nop.c      | 30 +++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/sdxi/nop.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
index 9e3f2c52330c..1127a33c0c5b 100644
--- a/tools/testing/selftests/sdxi/.gitignore
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -1 +1,2 @@
 /context
+/nop
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index dba5d7df3872..8da832249c70 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,7 +2,7 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
-TEST_GEN_PROGS := context
+TEST_GEN_PROGS := context nop
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/sdxi/nop.c b/tools/testing/selftests/sdxi/nop.c
new file mode 100644
index 000000000000..fdddf28d269e
--- /dev/null
+++ b/tools/testing/selftests/sdxi/nop.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI NOP descriptor selftest.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <stdlib.h>
+
+#include "lib/descriptor.h"
+#include "lib/fixture-context.h"
+#include "lib/lib.h"
+#include "../kselftest_harness.h"
+
+TEST_F(sdxi_context, nop)
+{
+	struct sdxi_desc d = sdxi_dsc_encode_nop();
+
+	EXPECT_EQ(sdxi_submit_sync(&self->context, &d), 0);
+}
+
+TEST_F(sdxi_context, nops_wrap)
+{
+	struct sdxi_desc d = sdxi_dsc_encode_nop();
+
+	for (size_t i = 0; i < self->context.n_entries * 2; ++i)
+		EXPECT_EQ(sdxi_submit_sync(&self->context, &d), 0);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 39/44] selftests: sdxi: test copy submission and ring wrap
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (37 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 38/44] selftests: sdxi: test NOP descriptor submission Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 40/44] selftests: sdxi: exercise dataplane error paths Wei Huang
                   ` (4 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add SDXI copy test for the SDXI selftest suite.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore |  1 +
 tools/testing/selftests/sdxi/Makefile   |  2 +-
 tools/testing/selftests/sdxi/copy.c     | 40 +++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/sdxi/copy.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
index 1127a33c0c5b..d05f961b1c67 100644
--- a/tools/testing/selftests/sdxi/.gitignore
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -1,2 +1,3 @@
 /context
 /nop
+/copy
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index 8da832249c70..052b0a5fd194 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,7 +2,7 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
-TEST_GEN_PROGS := context nop
+TEST_GEN_PROGS := context nop copy
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/sdxi/copy.c b/tools/testing/selftests/sdxi/copy.c
new file mode 100644
index 000000000000..19486501dfff
--- /dev/null
+++ b/tools/testing/selftests/sdxi/copy.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI copy descriptor selftest.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <stdlib.h>
+
+#include "lib/descriptor.h"
+#include "lib/fixture-context.h"
+#include "lib/lib.h"
+#include "../kselftest_harness.h"
+
+TEST_F(sdxi_context, copy)
+{
+	const long src = 0xabcdef;
+	long dest = 0;
+	struct sdxi_desc copy = sdxi_dsc_encode_copy(&dest, &src, sizeof(src));
+
+	EXPECT_EQ(sdxi_submit_sync(&self->context, &copy), 0);
+
+	EXPECT_EQ(dest, src);
+}
+
+TEST_F(sdxi_context, copy_wrap)
+{
+
+	for (size_t i = 0; i < self->context.n_entries * 2; ++i) {
+		const long src = random();
+		long dest = 0;
+		struct sdxi_desc copy = sdxi_dsc_encode_copy(&dest, &src, sizeof(src));
+
+		EXPECT_EQ(sdxi_submit_sync(&self->context, &copy), 0);
+
+		EXPECT_EQ(dest, src);
+	}
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 40/44] selftests: sdxi: exercise dataplane error paths
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (38 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 39/44] selftests: sdxi: test copy submission and ring wrap Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 41/44] selftests: sdxi: test device ioctls Wei Huang
                   ` (3 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add SDXI driver error-path dataplane smoke test for SDXI selftest
suite.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore |  1 +
 tools/testing/selftests/sdxi/Makefile   |  2 +-
 tools/testing/selftests/sdxi/error.c    | 24 ++++++++++++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/sdxi/error.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
index d05f961b1c67..d891f7a24c40 100644
--- a/tools/testing/selftests/sdxi/.gitignore
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -1,3 +1,4 @@
 /context
 /nop
 /copy
+/error
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index 052b0a5fd194..c05390cb9d86 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,7 +2,7 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
-TEST_GEN_PROGS := context nop copy
+TEST_GEN_PROGS := context nop copy error
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/sdxi/error.c b/tools/testing/selftests/sdxi/error.c
new file mode 100644
index 000000000000..b4be54722fba
--- /dev/null
+++ b/tools/testing/selftests/sdxi/error.c
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI error path selftests.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <stdlib.h>
+
+#include "lib/descriptor.h"
+#include "lib/fixture-context.h"
+#include "lib/lib.h"
+#include "../kselftest_harness.h"
+
+TEST_F(sdxi_context, error)
+{
+	struct sdxi_desc d = sdxi_dsc_encode_rsvd();
+
+	// Attempt to make the error log wrap
+	for (size_t i = 0; i < 80; ++i)
+		EXPECT_EQ(sdxi_submit_oneshot(&d), 0);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 41/44] selftests: sdxi: test device ioctls
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (39 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 40/44] selftests: sdxi: exercise dataplane error paths Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 42/44] selftests: sdxi: test vendor admin ioctls Wei Huang
                   ` (2 subsequent siblings)
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add coverage for UAPI discovery, device information and device
attribute ioctls to SDXI selftest suite.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore |   1 +
 tools/testing/selftests/sdxi/Makefile   |   2 +-
 tools/testing/selftests/sdxi/device.c   | 245 ++++++++++++++++++++++++
 3 files changed, 247 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/sdxi/device.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
index d891f7a24c40..17855ea86222 100644
--- a/tools/testing/selftests/sdxi/.gitignore
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -2,3 +2,4 @@
 /nop
 /copy
 /error
+/device
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index c05390cb9d86..88e51b08f468 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,7 +2,7 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
-TEST_GEN_PROGS := context nop copy error
+TEST_GEN_PROGS := context nop copy error device
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/sdxi/device.c b/tools/testing/selftests/sdxi/device.c
new file mode 100644
index 000000000000..2fdec79e6bf5
--- /dev/null
+++ b/tools/testing/selftests/sdxi/device.c
@@ -0,0 +1,245 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI device ioctl selftests.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/sdxi.h>
+
+#include "lib.h"
+#include "lib/fixture-device.h"
+#include "../kselftest_harness.h"
+
+TEST_F(dev_sdxi, open_and_close_device)
+{
+	// Just run the fixture setup and teardown
+}
+
+TEST_F(dev_sdxi, get_uapi_info)
+{
+	struct sdxi_uapi_info_args info = {};
+
+	info.argsz = sizeof(info);
+
+	ASSERT_EQ(ioctl(self->fd, SDXI_GET_UAPI_INFO, &info), 0);
+	EXPECT_EQ(info.uapi_major, SDXI_UAPI_MAJOR);
+	EXPECT_EQ(info.uapi_minor, SDXI_UAPI_MINOR);
+	EXPECT_GT(info.cap_words, 0);
+	EXPECT_LE(info.cap_words, SDXI_UAPI_CAP_WORDS);
+	EXPECT_EQ(info.rsvd0, 0);
+	/* The driver must publish at least one capability bit. */
+	EXPECT_NE(info.caps[0], 0ULL);
+	EXPECT_NE(info.caps[0] & SDXI_UAPI_CAP_ADMIN_VENDOR, 0ULL);
+	EXPECT_NE(info.caps[0] & SDXI_UAPI_CAP_CXT_EVENTFD, 0ULL);
+}
+
+TEST_F(dev_sdxi, get_uapi_info_rejects_flags)
+{
+	struct sdxi_uapi_info_args info = {};
+
+	info.argsz = sizeof(info);
+	info.flags = 1;
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_UAPI_INFO, &info), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, get_dev_info)
+{
+	struct sdxi_dev_info_args info = {};
+
+	info.argsz = sizeof(info);
+
+	ASSERT_EQ(ioctl(self->fd, SDXI_GET_DEV_INFO, &info), 0);
+	EXPECT_EQ(info.argsz, sizeof(info));
+	/* SDXI 1.x devices report a non-zero major version. */
+	EXPECT_GE(info.hw_major, 1);
+	EXPECT_EQ(info.rsvd0, 0U);
+	EXPECT_EQ(info.rsvd1[0], 0ULL);
+	EXPECT_EQ(info.rsvd1[1], 0ULL);
+	EXPECT_EQ(info.rsvd1[2], 0ULL);
+	EXPECT_EQ(info.rsvd1[3], 0ULL);
+}
+
+TEST_F(dev_sdxi, get_dev_info_rejects_flags)
+{
+	struct sdxi_dev_info_args info = {};
+
+	info.argsz = sizeof(info);
+	info.flags = 1;
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_DEV_INFO, &info), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, get_dev_info_rejects_short_argsz)
+{
+	struct sdxi_dev_info_args info = {};
+
+	info.argsz = sizeof(info) - 1;
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_DEV_INFO, &info), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+static int sdxi_get_dev_attr(int fd, uint64_t attr, uint64_t *out)
+{
+	struct sdxi_dev_attr_args args = {};
+	int rc;
+
+	args.argsz = sizeof(args);
+	args.attr = attr;
+
+	rc = ioctl(fd, SDXI_GET_DEV_ATTR, &args);
+	if (rc == 0 && out)
+		*out = args.val;
+	return rc;
+}
+
+TEST_F(dev_sdxi, get_dev_attr)
+{
+	uint64_t val;
+
+	ASSERT_EQ(sdxi_get_dev_attr(self->fd, SDXI_DEV_ATTR_MAX_BUFFER, &val), 0);
+	EXPECT_LE(val, 11U);
+
+	ASSERT_EQ(sdxi_get_dev_attr(self->fd, SDXI_DEV_ATTR_MAX_AKEY_SZ, &val), 0);
+	EXPECT_LE(val, 8U);
+
+	ASSERT_EQ(sdxi_get_dev_attr(self->fd, SDXI_DEV_ATTR_MAX_CXT, &val), 0);
+	EXPECT_GT(val, 0U);
+
+	/* MAX_RKEY_SZ is read-only and may be 0 if RKEY isn't supported. */
+	EXPECT_EQ(sdxi_get_dev_attr(self->fd, SDXI_DEV_ATTR_MAX_RKEY_SZ, &val), 0);
+}
+
+TEST_F(dev_sdxi, get_dev_attr_rejects_unknown)
+{
+	struct sdxi_dev_attr_args args = {};
+
+	args.argsz = sizeof(args);
+	args.attr = 0;
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_DEV_ATTR, &args), -1);
+	EXPECT_EQ(errno, EOPNOTSUPP);
+
+	args.attr = 0xffff;
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_DEV_ATTR, &args), -1);
+	EXPECT_EQ(errno, EOPNOTSUPP);
+}
+
+TEST_F(dev_sdxi, get_dev_attr_rejects_flags)
+{
+	struct sdxi_dev_attr_args args = {};
+
+	args.argsz = sizeof(args);
+	args.attr = SDXI_DEV_ATTR_MAX_BUFFER;
+	args.flags = 1;
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_DEV_ATTR, &args), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, set_dev_attr_requires_admin)
+{
+	struct sdxi_dev_attr_args args = {};
+	uint64_t cur;
+	int rc;
+
+	if (geteuid() == 0)
+		SKIP(return, "needs non-root");
+
+	ASSERT_EQ(sdxi_get_dev_attr(self->fd, SDXI_DEV_ATTR_GRP_ID, &cur), 0);
+
+	args.argsz = sizeof(args);
+	args.attr = SDXI_DEV_ATTR_GRP_ID;
+	args.val = cur;
+
+	errno = 0;
+	rc = ioctl(self->fd, SDXI_SET_DEV_ATTR, &args);
+	EXPECT_EQ(rc, -1);
+	EXPECT_EQ(errno, EPERM);
+}
+
+TEST_F(dev_sdxi, set_dev_attr_round_trip)
+{
+	struct sdxi_dev_attr_args args = {};
+	uint64_t orig, applied;
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+
+	ASSERT_EQ(sdxi_get_dev_attr(self->fd, SDXI_DEV_ATTR_GRP_ID, &orig), 0);
+
+	args.argsz = sizeof(args);
+	args.attr = SDXI_DEV_ATTR_GRP_ID;
+	args.val = orig;
+
+	ASSERT_EQ(ioctl(self->fd, SDXI_SET_DEV_ATTR, &args), 0);
+	EXPECT_EQ(args.argsz, sizeof(args));
+	EXPECT_EQ(args.val, orig);
+
+	ASSERT_EQ(sdxi_get_dev_attr(self->fd, SDXI_DEV_ATTR_GRP_ID, &applied), 0);
+	EXPECT_EQ(applied, orig);
+}
+
+TEST_F(dev_sdxi, set_dev_attr_rejects_exceeding_caps)
+{
+	struct sdxi_dev_info_args info = {};
+	struct sdxi_dev_attr_args args = {};
+	uint64_t cap_max_buffer;
+	int rc;
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+
+	info.argsz = sizeof(info);
+	ASSERT_EQ(ioctl(self->fd, SDXI_GET_DEV_INFO, &info), 0);
+
+	cap_max_buffer = info.cap1 & 0xf;
+	if (cap_max_buffer == 0xf)
+		SKIP(return, "cap1.max_buffer already saturates the field");
+
+	args.argsz = sizeof(args);
+	args.attr = SDXI_DEV_ATTR_MAX_BUFFER;
+	args.val = cap_max_buffer + 1;
+
+	errno = 0;
+	rc = ioctl(self->fd, SDXI_SET_DEV_ATTR, &args);
+	EXPECT_EQ(rc, -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, set_dev_attr_rejects_read_only)
+{
+	struct sdxi_dev_attr_args args = {};
+	int rc;
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+
+	args.argsz = sizeof(args);
+	args.attr = SDXI_DEV_ATTR_MAX_RKEY_SZ;
+	args.val = 0;
+
+	errno = 0;
+	rc = ioctl(self->fd, SDXI_SET_DEV_ATTR, &args);
+	EXPECT_EQ(rc, -1);
+	EXPECT_EQ(errno, EOPNOTSUPP);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 42/44] selftests: sdxi: test vendor admin ioctls
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (40 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 41/44] selftests: sdxi: test device ioctls Wei Huang
@ 2026-11-19 20:23 ` 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
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add root-only coverage for the SDXI_ADMIN_VENDOR probe path and AMD
QoS capability command. Skip the test when the device does not advertise
the QoS vendor operation.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore  |   1 +
 tools/testing/selftests/sdxi/Makefile    |   2 +-
 tools/testing/selftests/sdxi/admin_cmd.c | 104 +++++++++++++++++++++++
 3 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/sdxi/admin_cmd.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
index 17855ea86222..022002a27306 100644
--- a/tools/testing/selftests/sdxi/.gitignore
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -3,3 +3,4 @@
 /copy
 /error
 /device
+/admin_cmd
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index 88e51b08f468..abdb33981d91 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,7 +2,7 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
-TEST_GEN_PROGS := context nop copy error device
+TEST_GEN_PROGS := context nop copy error device admin_cmd
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/sdxi/admin_cmd.c b/tools/testing/selftests/sdxi/admin_cmd.c
new file mode 100644
index 000000000000..f82395931bc6
--- /dev/null
+++ b/tools/testing/selftests/sdxi/admin_cmd.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI admin command selftests.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <linux/sdxi.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include "lib/fixture-device.h"
+#include "../kselftest_harness.h"
+
+/*
+ * SDXI DSC_DISC uses RFC4122-like GUIDs, but with time_low/time_mid/time_hi
+ * stored in little-endian order.
+ */
+static const uint8_t sdxi_amd_guid_qos[16] = {
+	0x0b, 0x61, 0xb7, 0xf5, 0xc6, 0x0d, 0x03, 0x4c,
+	0xab, 0x79, 0x65, 0xf1, 0xf4, 0xef, 0xcf, 0x4c,
+};
+
+/*
+ * AMD QoS vendor operations are GUID-scoped and not part of the generic
+ * UAPI header. The kernel maps these op ids onto the vendor descriptors
+ * internally.
+ */
+#define SDXI_AMD_QOS_ADMIN_OP_CAP	1U
+
+TEST_F(dev_sdxi, admin_disc_operation_groups)
+{
+	uint8_t disc_buf[31 * 16] = {0};
+	struct sdxi_admin_disc_args args = {
+		.argsz = sizeof(args),
+		.index = {
+			.start = 1,
+			.end = 31,
+		},
+		.buf_uptr = (uint64_t)(uintptr_t)disc_buf,
+		.buf_size = sizeof(disc_buf),
+	};
+	int rc;
+
+	rc = ioctl(self->fd, SDXI_ADMIN_DISC, &args);
+	if (rc != 0) {
+		if (errno == EOPNOTSUPP)
+			SKIP(return, "admin discovery not supported");
+		ASSERT_EQ(rc, 0);
+	}
+
+	EXPECT_EQ(args.buf_size, sizeof(disc_buf));
+}
+
+TEST_F(dev_sdxi, admin_vendor_qos_cap)
+{
+	uint8_t cap_buf[48] = {0};
+	uint32_t word;
+	int rc;
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+
+	struct sdxi_admin_vendor_args probe = {
+		.argsz = sizeof(probe),
+		.op = SDXI_ADMIN_VENDOR_PROBE,
+	};
+	struct sdxi_admin_vendor_args cap = {
+		.argsz = sizeof(cap),
+		.op = SDXI_AMD_QOS_ADMIN_OP_CAP,
+		.out_uptr = (uint64_t)(uintptr_t)cap_buf,
+		.out_size = sizeof(cap_buf),
+	};
+
+	memcpy(probe.guid, sdxi_amd_guid_qos, sizeof(probe.guid));
+	memcpy(cap.guid, sdxi_amd_guid_qos, sizeof(cap.guid));
+
+	rc = ioctl(self->fd, SDXI_ADMIN_VENDOR, &probe);
+	if (rc != 0) {
+		if (errno == EOPNOTSUPP)
+			SKIP(return, "QoS vendor path not supported");
+		EXPECT_EQ(0, rc);
+		return;
+	}
+
+	rc = ioctl(self->fd, SDXI_ADMIN_VENDOR, &cap);
+	if (rc != 0) {
+		if (errno == EOPNOTSUPP)
+			SKIP(return, "QoS CAP not supported");
+		EXPECT_EQ(0, rc);
+		return;
+	}
+
+	word = (uint32_t)cap_buf[8] |
+	       ((uint32_t)cap_buf[9] << 8) |
+	       ((uint32_t)cap_buf[10] << 16) |
+	       ((uint32_t)cap_buf[11] << 24);
+	EXPECT_NE(0U, word & 0x0fff);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 43/44] selftests: sdxi: test RKey ioctls
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (41 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 42/44] selftests: sdxi: test vendor admin ioctls Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  2026-11-19 20:23 ` [PATCH v1 44/44] MAINTAINERS: cover SDXI UAPI, vendor files, and selftests Wei Huang
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Add RKey list, get and set ioctl coverage, including sizing retries
and negative cases to SDXI selftest suite.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore |   1 +
 tools/testing/selftests/sdxi/Makefile   |   2 +-
 tools/testing/selftests/sdxi/rkey.c     | 413 ++++++++++++++++++++++++
 3 files changed, 415 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/sdxi/rkey.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
index 022002a27306..1c64e625c2bd 100644
--- a/tools/testing/selftests/sdxi/.gitignore
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -4,3 +4,4 @@
 /error
 /device
 /admin_cmd
+/rkey
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index abdb33981d91..dce0bcfc93d6 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,7 +2,7 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
-TEST_GEN_PROGS := context nop copy error device admin_cmd
+TEST_GEN_PROGS := context nop copy error device admin_cmd rkey
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/sdxi/rkey.c b/tools/testing/selftests/sdxi/rkey.c
new file mode 100644
index 000000000000..c6d09399d791
--- /dev/null
+++ b/tools/testing/selftests/sdxi/rkey.c
@@ -0,0 +1,413 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI RKey ioctl coverage.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ *
+ * Exercises SDXI_GET_RKEY_LIST / SDXI_GET_RKEYS / SDXI_SET_RKEYS against
+ * the device fd using the unified struct sdxi_key_entry +
+ * sdxi_{key_list,get_keys,set_keys}_args contract.
+ *
+ * All tests skip cleanly when the device does not advertise RKey support
+ * (MMIO_CAP1.rkey_cap == 0); root-only behavior is exercised when the
+ * test is invoked with sufficient privileges and otherwise just validated
+ * for the documented EPERM.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include <linux/sdxi.h>
+
+#include "lib.h"
+#include "lib/fixture-device.h"
+#include "../kselftest_harness.h"
+
+/*
+ * Wire-format bits for an RKEY_ENT, mirrored from drivers/dma/sdxi/hw.h
+ * so the test stays standalone (kernel-private headers aren't installed).
+ * Keep in sync with `struct sdxi_rkey_ent` and SDXI_RKEY_ENT_*.
+ */
+#define RKEY_ENT_VL		(1U << 0)
+#define RKEY_ENT_INTR_NUM_MAX	((1U << 11) - 1)	/* bits [14:4] */
+
+static void rkey_pack_minimal(uint32_t data[4])
+{
+	memset(data, 0, sizeof(uint32_t) * 4);
+	/* intr_num: only the valid bit; pasid/rsvd left zero. */
+	data[0] = RKEY_ENT_VL;
+}
+
+static int rkey_get_max(int fd, uint32_t *out)
+{
+	struct sdxi_dev_attr_args args = {};
+	int rc;
+
+	args.argsz = sizeof(args);
+	args.attr = SDXI_DEV_ATTR_MAX_RKEY_SZ;
+
+	rc = ioctl(fd, SDXI_GET_DEV_ATTR, &args);
+	if (rc != 0)
+		return -errno;
+
+	*out = (uint32_t)args.val;
+	return 0;
+}
+
+/*
+ * Check SDXI_DEV_ATTR_MAX_RKEY_SZ instead of issuing a list-with-zero-buffer
+ * probe so privileged tests skip deterministically before exercising an
+ * RKey ioctl.
+ */
+static bool rkey_available(struct __test_metadata *_metadata, int fd)
+{
+	uint32_t max = 0;
+	int rc;
+
+	rc = rkey_get_max(fd, &max);
+	if (rc != 0)
+		SKIP(return false,
+		     "SDXI_GET_DEV_ATTR(MAX_RKEY_SZ) failed: %d", rc);
+	if (max == 0)
+		SKIP(return false,
+		     "RKey table not present on this device");
+
+	return true;
+}
+
+static int rkey_read_bitmap(int fd, struct sdxi_key_list_args **out)
+{
+	struct sdxi_key_list_args probe = {};
+	struct sdxi_key_list_args *full;
+	size_t total;
+	int rc;
+
+	probe.argsz = sizeof(probe);
+	if (ioctl(fd, SDXI_GET_RKEY_LIST, &probe) == 0) {
+		/* Empty table is legal; emit a synthesized header for caller. */
+		*out = calloc(1, sizeof(probe));
+		if (!*out)
+			return -ENOMEM;
+		**out = probe;
+		return 0;
+	}
+	if (errno != ENOSPC)
+		return -errno;
+
+	total = sizeof(*full) + probe.buf_size;
+	full = calloc(1, total);
+	if (!full)
+		return -ENOMEM;
+
+	full->argsz = sizeof(*full);
+	full->buf_size = probe.buf_size;
+	rc = ioctl(fd, SDXI_GET_RKEY_LIST, full);
+	if (rc != 0) {
+		rc = -errno;
+		free(full);
+		return rc;
+	}
+
+	*out = full;
+	return 0;
+}
+
+/* ----------------------- SDXI_GET_RKEY_LIST ----------------------- */
+
+TEST_F(dev_sdxi, get_rkey_list_requires_admin)
+{
+	struct sdxi_key_list_args args = {};
+
+	if (geteuid() == 0)
+		SKIP(return, "needs non-root");
+
+	args.argsz = sizeof(args);
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_RKEY_LIST, &args), -1);
+	EXPECT_EQ(errno, EPERM);
+}
+
+TEST_F(dev_sdxi, get_rkey_list_rejects_flags)
+{
+	struct sdxi_key_list_args args = {};
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	args.argsz = sizeof(args);
+	args.flags = 1;
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_RKEY_LIST, &args), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, get_rkey_list_rejects_rsvd)
+{
+	struct sdxi_key_list_args args = {};
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	args.argsz = sizeof(args);
+	args.rsvd0 = 1;
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_RKEY_LIST, &args), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, get_rkey_list_resize_and_retry)
+{
+	struct sdxi_key_list_args probe = {};
+	struct sdxi_key_list_args *full;
+	uint32_t required;
+	uint32_t nentries;
+	size_t total;
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	/* Probe with buf_size = 0 to learn the required bitmap size. */
+	probe.argsz = sizeof(probe);
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_RKEY_LIST, &probe), -1);
+	EXPECT_EQ(errno, ENOSPC);
+	EXPECT_GT(probe.nentries, 0U);
+	EXPECT_EQ(probe.buf_size, (probe.nentries + 7U) / 8U);
+
+	required = probe.buf_size;
+	nentries = probe.nentries;
+
+	/* Re-issue with the requested capacity; must succeed. */
+	total = sizeof(*full) + required;
+	full = calloc(1, total);
+	ASSERT_NE(full, NULL);
+
+	full->argsz = sizeof(*full);
+	full->buf_size = required;
+
+	ASSERT_EQ(ioctl(self->fd, SDXI_GET_RKEY_LIST, full), 0);
+	EXPECT_EQ(full->nentries, nentries);
+	EXPECT_EQ(full->buf_size, required);
+	EXPECT_LE(full->nvalid, nentries);
+
+	free(full);
+}
+
+/* ------------------------- SDXI_GET_RKEYS ------------------------- */
+
+TEST_F(dev_sdxi, get_rkeys_requires_admin)
+{
+	struct sdxi_get_keys_args args = {};
+
+	if (geteuid() == 0)
+		SKIP(return, "needs non-root");
+
+	args.argsz = sizeof(args);
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_RKEYS, &args), -1);
+	EXPECT_EQ(errno, EPERM);
+}
+
+TEST_F(dev_sdxi, get_rkeys_rejects_oob_index)
+{
+	union {
+		struct sdxi_get_keys_args align;
+		unsigned char storage[sizeof(struct sdxi_get_keys_args) +
+				      sizeof(struct sdxi_key_entry)];
+	} buffer = {};
+	struct sdxi_get_keys_args *args = (void *)buffer.storage;
+	struct sdxi_key_entry *entry = (void *)(args + 1);
+	uint32_t nentries;
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	ASSERT_EQ(rkey_get_max(self->fd, &nentries), 0);
+
+	args->argsz = sizeof(*args);
+	args->nkeys = 1;
+	entry->index = nentries;	/* one past the end */
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_RKEYS, args), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, get_rkeys_rejects_entry_rsvd)
+{
+	union {
+		struct sdxi_get_keys_args align;
+		unsigned char storage[sizeof(struct sdxi_get_keys_args) +
+				      sizeof(struct sdxi_key_entry)];
+	} buffer = {};
+	struct sdxi_get_keys_args *args = (void *)buffer.storage;
+	struct sdxi_key_entry *entry = (void *)(args + 1);
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	args->argsz = sizeof(*args);
+	args->nkeys = 1;
+	entry->index = 0;
+	entry->rsvd0 = 1;
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_GET_RKEYS, args), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+/* ------------------------- SDXI_SET_RKEYS ------------------------- */
+
+TEST_F(dev_sdxi, set_rkeys_requires_admin)
+{
+	struct sdxi_set_keys_args args = {};
+
+	if (geteuid() == 0)
+		SKIP(return, "needs non-root");
+
+	args.argsz = sizeof(args);
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_SET_RKEYS, &args), -1);
+	EXPECT_EQ(errno, EPERM);
+}
+
+TEST_F(dev_sdxi, set_rkeys_zero_nkeys_reports_commit_all)
+{
+	struct sdxi_set_keys_args args = {};
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	args.argsz = sizeof(args);
+	args.nkeys = 0;
+
+	ASSERT_EQ(ioctl(self->fd, SDXI_SET_RKEYS, &args), 0);
+	EXPECT_EQ(args.commit_state, (uint32_t)SDXI_KEY_COMMIT_ALL);
+	EXPECT_EQ(args.cst_flags, 0U);
+	EXPECT_EQ(args.first_failed_index, 0U);
+}
+
+TEST_F(dev_sdxi, set_rkeys_round_trip)
+{
+	union {
+		struct sdxi_set_keys_args align;
+		unsigned char storage[sizeof(struct sdxi_set_keys_args) +
+				      sizeof(struct sdxi_key_entry)];
+	} set_buffer = {};
+	union {
+		struct sdxi_get_keys_args align;
+		unsigned char storage[sizeof(struct sdxi_get_keys_args) +
+				      sizeof(struct sdxi_key_entry)];
+	} get_buffer = {};
+	struct sdxi_set_keys_args *set_args = (void *)set_buffer.storage;
+	struct sdxi_get_keys_args *get_args = (void *)get_buffer.storage;
+	struct sdxi_key_entry *set_entry = (void *)(set_args + 1);
+	struct sdxi_key_entry *get_entry = (void *)(get_args + 1);
+	uint32_t nentries;
+	uint32_t target_index;
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	ASSERT_EQ(rkey_get_max(self->fd, &nentries), 0);
+	ASSERT_GT(nentries, 0U);
+
+	/*
+	 * Use the highest available index to minimize collisions with any
+	 * driver-managed reservations at the bottom of the table.
+	 */
+	target_index = nentries - 1;
+
+	/* Phase 1: write a minimal-but-valid RKey entry. */
+	set_args->argsz = sizeof(*set_args);
+	set_args->nkeys = 1;
+	set_entry->index = target_index;
+	rkey_pack_minimal(set_entry->data);
+
+	ASSERT_EQ(ioctl(self->fd, SDXI_SET_RKEYS, set_args), 0);
+	EXPECT_EQ(set_args->commit_state, (uint32_t)SDXI_KEY_COMMIT_ALL);
+	EXPECT_EQ(set_args->cst_flags, 0U);
+	EXPECT_EQ(set_args->first_failed_index, 0U);
+
+	/* Phase 2: read it back and check the wire bytes survived. */
+	get_args->argsz = sizeof(*get_args);
+	get_args->nkeys = 1;
+	get_entry->index = target_index;
+
+	ASSERT_EQ(ioctl(self->fd, SDXI_GET_RKEYS, get_args), 0);
+	EXPECT_EQ(get_args->nkeys, 1U);
+	EXPECT_EQ(get_entry->index, target_index);
+	EXPECT_EQ(get_entry->data[0], RKEY_ENT_VL);
+	EXPECT_EQ(get_entry->data[1], 0U);
+	EXPECT_EQ(get_entry->data[2], 0U);
+	EXPECT_EQ(get_entry->data[3], 0U);
+
+	/* Phase 3: bitmap reflects the new validity. */
+	struct sdxi_key_list_args *full = NULL;
+	uint8_t mask;
+
+	ASSERT_EQ(rkey_read_bitmap(self->fd, &full), 0);
+	ASSERT_NE(full, NULL);
+	ASSERT_GE(full->buf_size, (target_index / 8) + 1);
+
+	mask = full->data[target_index / 8] & (1U << (target_index % 8));
+	EXPECT_NE(mask, 0);
+	EXPECT_GT(full->nvalid, 0U);
+
+	free(full);
+}
+
+TEST_F(dev_sdxi, set_rkeys_rejects_flags)
+{
+	struct sdxi_set_keys_args args = {};
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	args.argsz = sizeof(args);
+	args.flags = 1;
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_SET_RKEYS, &args), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_F(dev_sdxi, set_rkeys_rejects_rsvd)
+{
+	struct sdxi_set_keys_args args = {};
+
+	if (geteuid() != 0)
+		SKIP(return, "requires root");
+	if (!rkey_available(_metadata, self->fd))
+		return;
+
+	args.argsz = sizeof(args);
+	args.rsvd0[0] = 1;
+
+	errno = 0;
+	EXPECT_EQ(ioctl(self->fd, SDXI_SET_RKEYS, &args), -1);
+	EXPECT_EQ(errno, EINVAL);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v1 44/44] MAINTAINERS: cover SDXI UAPI, vendor files, and selftests
  2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
                   ` (42 preceding siblings ...)
  2026-11-19 20:23 ` [PATCH v1 43/44] selftests: sdxi: test RKey ioctls Wei Huang
@ 2026-11-19 20:23 ` Wei Huang
  43 siblings, 0 replies; 45+ messages in thread
From: Wei Huang @ 2026-11-19 20:23 UTC (permalink / raw)
  To: dmaengine
  Cc: vkoul, Frank.Li, shuah, nathan.lynch, wei.huang2,
	mario.limonciello, Stephen.Bates, tycho, suravee.suthikulpanit,
	rientjes, John.Kariuki, jic23, weixugc, bhelgaas, Shyam.Iyer,
	linux-kernel, linux-kselftest, linux-pci

Extend the SDXI maintainer entry to cover the exported UAPI header, AMD
vendor support files and the SDXI selftest tree.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 721f06446435..63197b02b419 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25287,6 +25287,9 @@ M:	Wei Huang <wei.huang2@amd.com>
 L:	dmaengine@vger.kernel.org
 S:	Supported
 F:	drivers/dma/sdxi/
+F:	drivers/dma/sdxi/amd/
+F:	include/uapi/linux/sdxi.h
+F:	tools/testing/selftests/sdxi/
 
 SECO BOARDS CEC DRIVER
 M:	Ettore Chimenti <ek5.chimenti@gmail.com>
-- 
2.56.1


^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2026-11-19 20:34 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v1 18/44] dmaengine: sdxi: implement userspace context file operations Wei Huang
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

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®