[PATCH v1 12/44] dmaengine: sdxi: route context interrupts to eventfd
From: Wei Huang
Date: Thu Nov 19 2026 - 15:26:37 EST
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@xxxxxxx>
---
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