Introduce the CB (compute context bank) device management layer. Each DSP domain node in the device tree may contain child nodes with compatible "qcom,fastrpc-compute-cb", each representing one IOMMU context bank. Enumerate those child nodes during RPMsg probe and create a corresponding device on the qda-compute-cb bus for each one, so that the IOMMU subsystem assigns each of them its own domain and DSP buffer mappings are isolated per session.
qda_cb_populate() walks the child nodes and creates one CB device each, unwinding everything it created if any single creation fails. qda_destroy_cb_device() removes a device from its IOMMU group before unregistering it so the domain is released cleanly. CB devices are created before the DRM device is registered and destroyed after it is unplugged, so user space cannot reach a CB that does not exist yet, and no ioctl can be in flight while one is torn down. Assisted-by: Claude:claude-sonnet-5 Signed-off-by: Ekansh Gupta <[email protected]> --- Changes in v2: - Use qda_cb_device_create() and pass the name as a format string instead of formatting into a stack buffer (Dmitry Baryshkov) - Fail probe on the first CB creation error and unwind, instead of counting successes and continuing (Dmitry Baryshkov) - Drop the impossible NULL check in qda_destroy_cb_device() (Dmitry Baryshkov) - Use drm_*() logging consistently instead of mixing with dev_*() (Dmitry Baryshkov) --- drivers/accel/qda/Makefile | 1 + drivers/accel/qda/qda_cb.c | 86 +++++++++++++++++++++++++++++++++++++++++++ drivers/accel/qda/qda_cb.h | 32 ++++++++++++++++ drivers/accel/qda/qda_drv.c | 1 + drivers/accel/qda/qda_drv.h | 3 ++ drivers/accel/qda/qda_rpmsg.c | 16 +++++++- 6 files changed, 138 insertions(+), 1 deletion(-) diff --git a/drivers/accel/qda/Makefile b/drivers/accel/qda/Makefile index 424176f652a5..143c9e4e789e 100644 --- a/drivers/accel/qda/Makefile +++ b/drivers/accel/qda/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_DRM_ACCEL_QDA) := qda.o qda-y := \ + qda_cb.o \ qda_drv.o \ qda_rpmsg.o diff --git a/drivers/accel/qda/qda_cb.c b/drivers/accel/qda/qda_cb.c new file mode 100644 index 000000000000..9c5ef32f351e --- /dev/null +++ b/drivers/accel/qda/qda_cb.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0-only +// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +#include <linux/dma-mapping.h> +#include <linux/device.h> +#include <linux/of.h> +#include <linux/iommu.h> +#include <linux/qda_compute_bus.h> +#include <linux/slab.h> +#include <drm/drm_print.h> +#include "qda_drv.h" +#include "qda_cb.h" + +int qda_create_cb_device(struct qda_dev *qdev, struct device_node *cb_node) +{ + struct qda_cb_dev *entry; + struct device *cb_dev; + u32 sid = 0; + + if (of_property_read_u32(cb_node, "reg", &sid)) + drm_info(&qdev->drm_dev, "No stream ID in DT node %pOFn, using 0\n", cb_node); + + cb_dev = qda_cb_device_create(qdev->dev, DMA_BIT_MASK(32), cb_node, + "qda-cb-%s-%u", qdev->dsp_name, sid); + if (IS_ERR(cb_dev)) { + drm_err(&qdev->drm_dev, "Failed to create CB device for SID %u: %ld\n", + sid, PTR_ERR(cb_dev)); + return PTR_ERR(cb_dev); + } + + entry = kzalloc_obj(*entry); + if (!entry) { + qda_destroy_cb_device(cb_dev); + return -ENOMEM; + } + + entry->dev = cb_dev; + list_add_tail(&entry->node, &qdev->cb_devs); + + return 0; +} + +void qda_cb_unpopulate(struct qda_dev *qdev) +{ + struct qda_cb_dev *entry, *tmp; + + list_for_each_entry_safe(entry, tmp, &qdev->cb_devs, node) { + list_del(&entry->node); + qda_destroy_cb_device(entry->dev); + kfree(entry); + } +} + +int qda_cb_populate(struct qda_dev *qdev, struct device_node *parent_node) +{ + struct device_node *child; + int ret; + + for_each_child_of_node(parent_node, child) { + if (!of_device_is_compatible(child, "qcom,fastrpc-compute-cb")) + continue; + + ret = qda_create_cb_device(qdev, child); + if (ret) { + drm_err(&qdev->drm_dev, "Failed to create CB device for: %s\n", + child->name); + of_node_put(child); + qda_cb_unpopulate(qdev); + return ret; + } + } + + return 0; +} + +void qda_destroy_cb_device(struct device *cb_dev) +{ + struct iommu_group *group; + + group = iommu_group_get(cb_dev); + if (group) { + iommu_group_remove_device(cb_dev); + iommu_group_put(group); + } + + device_unregister(cb_dev); +} diff --git a/drivers/accel/qda/qda_cb.h b/drivers/accel/qda/qda_cb.h new file mode 100644 index 000000000000..bd83d64fa425 --- /dev/null +++ b/drivers/accel/qda/qda_cb.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. + */ + +#ifndef __QDA_CB_H__ +#define __QDA_CB_H__ + +#include <linux/device.h> +#include <linux/list.h> +#include <linux/of.h> +#include "qda_drv.h" + +struct qda_cb_dev { + struct list_head node; + struct device *dev; +}; + +/* + * Compute bus (CB) device management + */ +int qda_create_cb_device(struct qda_dev *qdev, struct device_node *cb_node); +void qda_destroy_cb_device(struct device *cb_dev); + +/* + * Transport-agnostic CB device population/teardown. + * Called by any transport layer (RPMsg, etc.) during probe/remove. + */ +int qda_cb_populate(struct qda_dev *qdev, struct device_node *parent_node); +void qda_cb_unpopulate(struct qda_dev *qdev); + +#endif /* __QDA_CB_H__ */ diff --git a/drivers/accel/qda/qda_drv.c b/drivers/accel/qda/qda_drv.c index 9a64f7dfcb6e..09b6d5ac390b 100644 --- a/drivers/accel/qda/qda_drv.c +++ b/drivers/accel/qda/qda_drv.c @@ -52,6 +52,7 @@ struct qda_dev *qda_alloc_device(struct device *dev) if (IS_ERR(qdev)) return ERR_CAST(qdev); + INIT_LIST_HEAD(&qdev->cb_devs); return qdev; } diff --git a/drivers/accel/qda/qda_drv.h b/drivers/accel/qda/qda_drv.h index 4a27fb40c280..3bd41b9593fa 100644 --- a/drivers/accel/qda/qda_drv.h +++ b/drivers/accel/qda/qda_drv.h @@ -7,6 +7,7 @@ #define __QDA_DRV_H__ #include <linux/device.h> +#include <linux/list.h> #include <linux/rpmsg.h> #include <linux/types.h> #include <drm/drm_device.h> @@ -37,6 +38,8 @@ struct qda_dev { struct rpmsg_device *rpdev; /** @dev: Underlying Linux device */ struct device *dev; + /** @cb_devs: Compute context-bank (CB) child devices */ + struct list_head cb_devs; /** @dsp_name: Name of the DSP domain (e.g. "cdsp", "adsp") */ const char *dsp_name; }; diff --git a/drivers/accel/qda/qda_rpmsg.c b/drivers/accel/qda/qda_rpmsg.c index 6a6e58333a68..723b3e61bf0a 100644 --- a/drivers/accel/qda/qda_rpmsg.c +++ b/drivers/accel/qda/qda_rpmsg.c @@ -5,6 +5,7 @@ #include <linux/rpmsg.h> #include <drm/drm_print.h> +#include "qda_cb.h" #include "qda_drv.h" static struct qda_dev *qda_rpmsg_alloc_and_init_qdev(struct rpmsg_device *rpdev) @@ -40,6 +41,7 @@ static void qda_rpmsg_remove(struct rpmsg_device *rpdev) */ drm_dev_unplug(&qdev->drm_dev); qdev->rpdev = NULL; + qda_cb_unpopulate(qdev); } static int qda_rpmsg_probe(struct rpmsg_device *rpdev) @@ -57,7 +59,19 @@ static int qda_rpmsg_probe(struct rpmsg_device *rpdev) return ret; } - return qda_register_device(qdev); + ret = qda_cb_populate(qdev, rpdev->dev.of_node); + if (ret) + return ret; + + ret = qda_register_device(qdev); + if (ret) + goto err_unpopulate; + + return 0; + +err_unpopulate: + qda_cb_unpopulate(qdev); + return ret; } static const struct of_device_id qda_rpmsg_id_table[] = { -- 2.34.1
