[PATCH v4 10/25] accel/qda: Add FastRPC invocation contexts

From: Ekansh Gupta

Date: Sun Nov 01 2026 - 17:32:23 EST


Running something on the DSP means sending it a message and waiting for
the reply. Add the bookkeeping for an invocation that has been sent and
not yet answered, before there is anything to send.

The DSP identifies an invocation by a context value it echoes back in
its response, so each invocation gets an entry in a per-device lookup
table and the entry's index is placed in that value. The index is only 8
bits wide and is reused as soon as its invocation is freed, so a late or
duplicate response could otherwise name an ID that now belongs to a
different invocation and complete it early, with its caller then reading
a buffer the DSP is still writing. To tell the two apart, each context
also carries a sequence number that does not repeat in practice, for the
response handler to check.

An invocation is removed from the table only when its last reference is
dropped, never when its caller gives up waiting, so an ID cannot be
reused while the old invocation can still be found and completed. The
number of invocations in flight on one device is bounded by the size of
the ID space, which also bounds what they can allocate.

Signed-off-by: Ekansh Gupta <ekansh.gupta@xxxxxxxxxxxxxxxx>
---
drivers/accel/qda/Makefile | 1 +
drivers/accel/qda/qda_drv.c | 3 ++
drivers/accel/qda/qda_drv.h | 6 ++++
drivers/accel/qda/qda_fastrpc.c | 63 +++++++++++++++++++++++++++++++++++++++++
drivers/accel/qda/qda_fastrpc.h | 54 +++++++++++++++++++++++++++++++++++
5 files changed, 127 insertions(+)

diff --git a/drivers/accel/qda/Makefile b/drivers/accel/qda/Makefile
index 0996e4ed5798..aee69cd7d150 100644
--- a/drivers/accel/qda/Makefile
+++ b/drivers/accel/qda/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_DRM_ACCEL_QDA) := qda.o
qda-y := \
qda_cb.o \
qda_drv.o \
+ qda_fastrpc.o \
qda_gem.o \
qda_ioctl.o \
qda_legacy_ops.o \
diff --git a/drivers/accel/qda/qda_drv.c b/drivers/accel/qda/qda_drv.c
index 21113deceaad..bdfb23c3c0b1 100644
--- a/drivers/accel/qda/qda_drv.c
+++ b/drivers/accel/qda/qda_drv.c
@@ -100,6 +100,8 @@ struct qda_dev *qda_alloc_device(struct device *dev)

INIT_LIST_HEAD(&qdev->cb_devs);
mutex_init(&qdev->import_lock);
+ xa_init_flags(&qdev->ctx_xa, XA_FLAGS_ALLOC1);
+ atomic64_set(&qdev->ctx_seq, 0);

return qdev;
}
@@ -122,6 +124,7 @@ int qda_init_device(struct qda_dev *qdev, int num_cbs)
void qda_deinit_device(struct qda_dev *qdev)
{
qda_memory_manager_exit(qdev->iommu_mgr);
+ xa_destroy(&qdev->ctx_xa);
mutex_destroy(&qdev->import_lock);
}

diff --git a/drivers/accel/qda/qda_drv.h b/drivers/accel/qda/qda_drv.h
index c04f19e5a91b..b7cbb1667adb 100644
--- a/drivers/accel/qda/qda_drv.h
+++ b/drivers/accel/qda/qda_drv.h
@@ -6,12 +6,14 @@
#ifndef __QDA_DRV_H__
#define __QDA_DRV_H__

+#include <linux/atomic.h>
#include <linux/auxiliary_bus.h>
#include <linux/device.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/rpmsg.h>
#include <linux/types.h>
+#include <linux/xarray.h>
#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_file.h>
@@ -75,6 +77,10 @@ struct qda_dev {
* calling file to.
*/
struct qda_session *importing_session;
+ /** @ctx_xa: Invocations in flight, keyed by context ID */
+ struct xarray ctx_xa;
+ /** @ctx_seq: Distinguishes invocations that reuse a context ID */
+ atomic64_t ctx_seq;
/** @soc_data: Per-SoC DSP integration parameters */
const struct qda_soc_data *soc_data;
/** @dsp_name: Name of the DSP domain (e.g. "cdsp", "adsp") */
diff --git a/drivers/accel/qda/qda_fastrpc.c b/drivers/accel/qda/qda_fastrpc.c
new file mode 100644
index 000000000000..14d9da8c20b3
--- /dev/null
+++ b/drivers/accel/qda/qda_fastrpc.c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+#include <linux/bitfield.h>
+#include <linux/slab.h>
+#include "qda_drv.h"
+#include "qda_fastrpc.h"
+
+/**
+ * qda_fastrpc_context_free() - Release an invocation
+ * @ref: Reference counter embedded in the invocation
+ *
+ * Runs when the last reference goes. The invocation leaves the lookup table
+ * here and not earlier, so that its ID cannot be handed to a new invocation
+ * while this one can still be found and completed.
+ */
+void qda_fastrpc_context_free(struct kref *ref)
+{
+ struct qda_fastrpc_invoke_ctx *ctx =
+ container_of(ref, struct qda_fastrpc_invoke_ctx, refcount);
+
+ xa_erase(&ctx->qdev->ctx_xa, FIELD_GET(QDA_FASTRPC_CTXID_MASK, ctx->ctxid));
+ kfree(ctx);
+}
+
+/**
+ * qda_fastrpc_context_alloc() - Allocate an invocation and give it an ID
+ * @qdev: Device the invocation is issued on
+ *
+ * The caller holds the initial reference and drops it with
+ * qda_fastrpc_context_free().
+ *
+ * Return: The new invocation, or ERR_PTR on failure
+ */
+struct qda_fastrpc_invoke_ctx *qda_fastrpc_context_alloc(struct qda_dev *qdev)
+{
+ struct qda_fastrpc_invoke_ctx *ctx;
+ u32 id;
+ int ret;
+
+ ctx = kzalloc_obj(*ctx);
+ if (!ctx)
+ return ERR_PTR(-ENOMEM);
+
+ init_completion(&ctx->work);
+ kref_init(&ctx->refcount);
+ ctx->qdev = qdev;
+ /* Anything but a status the DSP could send, until it answers */
+ ctx->retval = -1;
+
+ /* ID 0 is never used, so a zeroed context in a response matches nothing */
+ ret = xa_alloc(&qdev->ctx_xa, &id, ctx, XA_LIMIT(1, QDA_FASTRPC_CTXID_MAX),
+ GFP_KERNEL);
+ if (ret) {
+ kfree(ctx);
+ return ERR_PTR(ret);
+ }
+
+ ctx->ctxid = FIELD_PREP(QDA_FASTRPC_CTXID_MASK, id) |
+ FIELD_PREP(QDA_FASTRPC_CTXID_SEQ_MASK,
+ (u64)atomic64_inc_return(&qdev->ctx_seq));
+
+ return ctx;
+}
diff --git a/drivers/accel/qda/qda_fastrpc.h b/drivers/accel/qda/qda_fastrpc.h
new file mode 100644
index 000000000000..face3b3336b6
--- /dev/null
+++ b/drivers/accel/qda/qda_fastrpc.h
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#ifndef __QDA_FASTRPC_H__
+#define __QDA_FASTRPC_H__
+
+#include <linux/bits.h>
+#include <linux/completion.h>
+#include <linux/kref.h>
+#include <linux/types.h>
+
+struct qda_dev;
+
+/*
+ * Layout of the context the DSP echoes back in its response:
+ * [63:16] sequence number
+ * [15:8] context ID, the key in the device's lookup table
+ * [7:0] reserved for the protection domain, set when the message is built
+ *
+ * The context ID is narrow and is reused as soon as its invocation is freed,
+ * so a late or duplicate response could name an ID that now belongs to a
+ * newer invocation. The sequence number, which never repeats in practice,
+ * lets the response handler tell the two apart.
+ */
+#define QDA_FASTRPC_CTXID_MASK GENMASK_ULL(15, 8)
+#define QDA_FASTRPC_CTXID_SEQ_MASK GENMASK_ULL(63, 16)
+#define QDA_FASTRPC_CTXID_MAX (QDA_FASTRPC_CTXID_MASK >> 8)
+
+/**
+ * struct qda_fastrpc_invoke_ctx - One invocation in flight
+ */
+struct qda_fastrpc_invoke_ctx {
+ /** @qdev: Device the invocation was issued on */
+ struct qda_dev *qdev;
+ /** @ctxid: Wire context with the protection domain bits clear */
+ u64 ctxid;
+ /** @retval: Status the DSP reported, or an error if it never answered */
+ int retval;
+ /** @work: Completed once the DSP has answered */
+ struct completion work;
+ /**
+ * @refcount: Keeps the invocation alive for everyone who may still
+ * touch it: the caller and, once a message is sent, the response
+ * handler.
+ */
+ struct kref refcount;
+};
+
+struct qda_fastrpc_invoke_ctx *qda_fastrpc_context_alloc(struct qda_dev *qdev);
+void qda_fastrpc_context_free(struct kref *ref);
+
+#endif /* __QDA_FASTRPC_H__ */

--
2.34.1