[PATCH RFC 2/6] soc: qcom: add QDCP firmware provider

From: Jie Gan

Date: Thu Nov 19 2026 - 04:02:27 EST


Add a provider for the Qualcomm Debug Control Protocol (QDCP), the QMI
(Qualcomm MSM Interface) service over QRTR (Qualcomm IPC Router) that
Linux uses to control debug and trace blocks owned by the Qualcomm
Embedded Co-Processor (QECP) firmware. The application processor (AP)
cannot access the registers of those blocks, so consumers ask firmware
to enable, disable and query them through typed provider APIs.

The provider is a Qualcomm platform/QMI service driver rather than a
CoreSight graph device. Consumers look it up through the qcom,qdcp
phandle, which must reference a connection-N child of the provider
node. The lookup takes a core-managed device link, so unbinding the
provider first unbinds its consumers, and a consumer that probes while
the provider is already unbinding defers its probe.

Use the SoC-provider compatible qcom,qdcp and keep the public consumer
API under include/linux/soc/qcom/. QCOM_QDCP selects QCOM_QMI_HELPERS
because that symbol has no prompt and can only be enabled by a select.

Signed-off-by: Jie Gan <jie.gan@xxxxxxxxxxxxxxxx>
---
drivers/soc/qcom/Kconfig | 12 +
drivers/soc/qcom/Makefile | 1 +
drivers/soc/qcom/qcom_qdcp.c | 985 ++++++++++++++++++++++++++++++++++++++++++
include/linux/soc/qcom/qdcp.h | 128 ++++++
4 files changed, 1126 insertions(+)

diff --git a/drivers/soc/qcom/Kconfig b/drivers/soc/qcom/Kconfig
index 535c8619197b..5a7ad571cc58 100644
--- a/drivers/soc/qcom/Kconfig
+++ b/drivers/soc/qcom/Kconfig
@@ -330,6 +330,18 @@ config QCOM_QMI_TMD
TMD messaging and handles QMI communication with remote processors
to exchange mitigation state and apply thermal mitigation requests.

+config QCOM_QDCP
+ tristate "Qualcomm Debug Control Protocol driver"
+ depends on ARCH_QCOM || COMPILE_TEST
+ depends on OF
+ depends on NET && QRTR
+ select QCOM_QMI_HELPERS
+ help
+ This driver provides the Qualcomm Debug Control Protocol provider
+ used by QECP-owned debug and trace blocks. It is not registered as
+ a CoreSight device; subsystem drivers call its typed APIs when block
+ registers are owned by QECP firmware.
+
config QCOM_UBWC_CONFIG
tristate
depends on QCOM_SMEM
diff --git a/drivers/soc/qcom/Makefile b/drivers/soc/qcom/Makefile
index bddd6f0a5836..5c2b4e1c67dc 100644
--- a/drivers/soc/qcom/Makefile
+++ b/drivers/soc/qcom/Makefile
@@ -17,6 +17,7 @@ CFLAGS_pmic_pdcharger_ulog.o := -I$(src)
obj-$(CONFIG_QCOM_QMI_TMD) += qmi_tmd.o
obj-$(CONFIG_QCOM_QMI_HELPERS) += qmi_helpers.o
qmi_helpers-y += qmi_encdec.o qmi_interface.o
+obj-$(CONFIG_QCOM_QDCP) += qcom_qdcp.o
obj-$(CONFIG_QCOM_RAMP_CTRL) += ramp_controller.o
obj-$(CONFIG_QCOM_RMTFS_MEM) += rmtfs_mem.o
obj-$(CONFIG_QCOM_RPM_MASTER_STATS) += rpm_master_stats.o
diff --git a/drivers/soc/qcom/qcom_qdcp.c b/drivers/soc/qcom/qcom_qdcp.c
new file mode 100644
index 000000000000..295138b29881
--- /dev/null
+++ b/drivers/soc/qcom/qcom_qdcp.c
@@ -0,0 +1,985 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#include <linux/device.h>
+#include <linux/jiffies.h>
+#include <linux/kref.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/soc/qcom/qmi.h>
+
+#define QCOM_QDCP_PROVIDER
+#include <linux/soc/qcom/qdcp.h>
+
+#define QDCP_TIMEOUT_MS 10000
+#define QDCP_MAX_MSG_LEN 256
+#define QDCP_MAX_ATIDS 8
+
+#define QDCP_STM_SET_STATE 0x0003
+#define QDCP_STM_HWE_SET_STATE 0x0005
+#define QDCP_STM_SET_PARAM 0x0007
+#define QDCP_TMC_SET_STATE 0x002b
+#define QDCP_TMC_ASSIGN_BUFFER 0x002d
+#define QDCP_TMC_SET_PARAM 0x0033
+#define QDCP_TMC_GET_PARAM 0x0034
+#define QDCP_ATID_SET 0x0036
+
+#define QDCP_STM_PARAM_PORT_ENABLE 0x1002
+
+/* Firmware trace-source type used in QDCP_ATID_SET requests. */
+enum qdcp_trace_source {
+ QDCP_TRACE_SOURCE_STM = 0,
+};
+
+struct qdcp_trace_source_v01 {
+ s32 type;
+ u64 id;
+};
+
+/*
+ * Every QDCP response starts with the mandatory QMI result TLV;
+ * qdcp_send_resp() relies on that to check the result without being told
+ * where it lives.
+ */
+struct qdcp_resp_v01 {
+ struct qmi_response_type_v01 resp;
+};
+
+struct qdcp_state_req_v01 {
+ u64 id;
+ s32 state;
+};
+
+struct qdcp_stm_set_param_req_v01 {
+ u64 stm_id;
+ u32 param;
+ u32 value;
+};
+
+struct qdcp_atid_set_req_v01 {
+ struct qdcp_trace_source_v01 trace_source;
+ u8 atids[QDCP_MAX_ATIDS];
+ u8 num_atids;
+};
+
+struct qdcp_tmc_buffer_v01 {
+ u64 paddr;
+ u64 iaddr;
+ u64 size;
+};
+
+struct qdcp_tmc_assign_buffer_req_v01 {
+ u64 tmc_id;
+ struct qdcp_tmc_buffer_v01 buffer;
+};
+
+struct qdcp_tmc_set_param_req_v01 {
+ u64 tmc_id;
+ u32 param;
+ u64 value;
+};
+
+struct qdcp_tmc_get_param_req_v01 {
+ u64 tmc_id;
+ u32 param;
+};
+
+struct qdcp_tmc_get_param_resp_v01 {
+ struct qmi_response_type_v01 resp;
+ u8 value_valid;
+ u64 value;
+};
+
+static_assert(offsetof(struct qdcp_resp_v01, resp) == 0);
+static_assert(offsetof(struct qdcp_tmc_get_param_resp_v01, resp) == 0);
+
+/**
+ * struct qdcp_data - QDCP connection state.
+ * @ref: lifetime reference for connection consumers.
+ * @dev: parent provider device.
+ * @of_node: connection device tree node.
+ * @qmi: QMI service handle.
+ * @sq: QRTR server address.
+ * @lock: serializes QMI connection state and transactions.
+ * @node: entry in the global provider lookup list.
+ * @service_id: QDCP QMI service identifier.
+ * @version: QDCP QMI service version.
+ * @instance_id: QDCP QMI instance identifier.
+ * @connected: true when the QDCP service is available.
+ * @dying: true when provider removal has started.
+ */
+struct qdcp_data {
+ struct kref ref;
+ struct device *dev;
+ struct device_node *of_node;
+ struct qmi_handle qmi;
+ struct sockaddr_qrtr sq;
+ /* Serializes QMI connection state and transactions. */
+ struct mutex lock;
+ struct list_head node;
+ u32 service_id;
+ u32 version;
+ u32 instance_id;
+ bool connected;
+ bool dying;
+};
+
+static LIST_HEAD(qdcp_providers);
+static DEFINE_MUTEX(qdcp_providers_lock);
+
+static void qdcp_release(struct kref *ref)
+{
+ struct qdcp_data *qdcp = container_of(ref, struct qdcp_data,
+ ref);
+
+ of_node_put(qdcp->of_node);
+ put_device(qdcp->dev);
+ kfree(qdcp);
+}
+
+static const struct qmi_elem_info qdcp_resp_ei[] = {
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct qmi_response_type_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_resp_v01, resp),
+ .ei_array = qmi_response_type_v01_ei,
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_state_req_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qdcp_state_req_v01, id),
+ },
+ {
+ .data_type = QMI_SIGNED_4_BYTE_ENUM,
+ .elem_len = 1,
+ .elem_size = sizeof(s32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_state_req_v01, state),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_stm_set_param_req_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qdcp_stm_set_param_req_v01, stm_id),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_stm_set_param_req_v01, param),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x03,
+ .offset = offsetof(struct qdcp_stm_set_param_req_v01, value),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_trace_source_ei[] = {
+ {
+ .data_type = QMI_SIGNED_4_BYTE_ENUM,
+ .elem_len = 1,
+ .elem_size = sizeof(s32),
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ .offset = offsetof(struct qdcp_trace_source_v01, type),
+ },
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ .offset = offsetof(struct qdcp_trace_source_v01, id),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_atid_set_req_ei[] = {
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct qdcp_trace_source_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qdcp_atid_set_req_v01, trace_source),
+ .ei_array = qdcp_trace_source_ei,
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = QDCP_MAX_ATIDS,
+ .elem_size = sizeof(u8),
+ .array_type = STATIC_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_atid_set_req_v01, atids),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x03,
+ .offset = offsetof(struct qdcp_atid_set_req_v01, num_atids),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_tmc_buffer_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ .offset = offsetof(struct qdcp_tmc_buffer_v01, paddr),
+ },
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ .offset = offsetof(struct qdcp_tmc_buffer_v01, iaddr),
+ },
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ .offset = offsetof(struct qdcp_tmc_buffer_v01, size),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_tmc_assign_buffer_req_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qdcp_tmc_assign_buffer_req_v01, tmc_id),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct qdcp_tmc_buffer_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_tmc_assign_buffer_req_v01, buffer),
+ .ei_array = qdcp_tmc_buffer_ei,
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_tmc_get_param_resp_ei[] = {
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct qmi_response_type_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_tmc_get_param_resp_v01, resp),
+ .ei_array = qmi_response_type_v01_ei,
+ },
+ {
+ /* Decode sets value_valid only if the value TLV is present. */
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x03,
+ .offset = offsetof(struct qdcp_tmc_get_param_resp_v01,
+ value_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x03,
+ .offset = offsetof(struct qdcp_tmc_get_param_resp_v01, value),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_tmc_set_param_req_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qdcp_tmc_set_param_req_v01, tmc_id),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_tmc_set_param_req_v01, param),
+ },
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x03,
+ .offset = offsetof(struct qdcp_tmc_set_param_req_v01, value),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static const struct qmi_elem_info qdcp_tmc_get_param_req_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qdcp_tmc_get_param_req_v01, tmc_id),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qdcp_tmc_get_param_req_v01, param),
+ },
+ {
+ .data_type = QMI_EOTI,
+ },
+};
+
+static int qdcp_check_resp(struct qdcp_data *qdcp,
+ const struct qmi_response_type_v01 *resp)
+{
+ if (resp->result == QMI_RESULT_SUCCESS_V01)
+ return 0;
+
+ dev_err(qdcp->dev, "QMI request failed: result=%u error=%u\n",
+ resp->result, resp->error);
+ return -EIO;
+}
+
+static int qdcp_send_resp(struct qdcp_data *qdcp, unsigned int msg_id,
+ const struct qmi_elem_info *req_ei, const void *req,
+ size_t req_len, const struct qmi_elem_info *resp_ei,
+ void *resp, size_t resp_len)
+{
+ /* @resp starts with the QMI result TLV, see struct qdcp_resp_v01. */
+ struct qmi_response_type_v01 *qmi_resp = resp;
+ struct sockaddr_qrtr sq;
+ struct qmi_txn txn;
+ bool connected, dying;
+ int ret;
+
+ /*
+ * Only snapshot connection state under qdcp->lock; do not hold it
+ * across qmi_txn_wait(). qdcp_del_server()/qdcp_net_reset() also take
+ * qdcp->lock and run on qdcp->qmi's ordered workqueue, the same
+ * workqueue that must process the response completing this
+ * transaction. Holding the lock across the wait can deadlock: the
+ * workqueue thread blocks on qdcp->lock while this waiter blocks on
+ * a completion only that (now-blocked) workqueue thread can deliver.
+ */
+ mutex_lock(&qdcp->lock);
+ dying = qdcp->dying;
+ connected = qdcp->connected;
+ sq = qdcp->sq;
+ mutex_unlock(&qdcp->lock);
+
+ if (dying)
+ /* Provider is being removed: this is a permanent failure. */
+ return -ENODEV;
+
+ if (!connected)
+ /*
+ * The QDCP QMI service has not registered yet (or dropped
+ * off transiently). Report -EAGAIN rather than -ENODEV so
+ * callers can distinguish "retry later" from "never coming
+ * back", and defer probe instead of failing outright.
+ */
+ return -EAGAIN;
+
+ memset(resp, 0, resp_len);
+ /*
+ * QMI_RESULT_SUCCESS_V01 is 0, the same value memset() above just
+ * wrote into qmi_resp->result. A firmware reply that omits the
+ * mandatory response TLV (qmi_decode_message() does not treat a
+ * missing TLV as an error, it just decodes however many bytes are
+ * actually present) would otherwise leave that zero in place and
+ * read back as an implicit success. Poison it to a failure value
+ * first so decode must positively overwrite it with
+ * QMI_RESULT_SUCCESS_V01 for qdcp_check_resp() to accept the reply.
+ */
+ qmi_resp->result = QMI_RESULT_FAILURE_V01;
+ qmi_resp->error = QMI_ERR_MALFORMED_MSG_V01;
+
+ ret = qmi_txn_init(&qdcp->qmi, &txn, resp_ei, resp);
+ if (ret < 0)
+ return ret;
+
+ ret = qmi_send_request(&qdcp->qmi, &sq, &txn, msg_id, req_len, req_ei,
+ req);
+ if (ret < 0) {
+ qmi_txn_cancel(&txn);
+ return ret;
+ }
+
+ ret = qmi_txn_wait(&txn, msecs_to_jiffies(QDCP_TIMEOUT_MS));
+ if (ret < 0)
+ return ret;
+
+ return qdcp_check_resp(qdcp, qmi_resp);
+}
+
+static int qdcp_send(struct qdcp_data *qdcp, unsigned int msg_id,
+ const struct qmi_elem_info *req_ei, const void *req,
+ size_t req_len, struct qdcp_resp_v01 *resp)
+{
+ return qdcp_send_resp(qdcp, msg_id, req_ei, req, req_len, qdcp_resp_ei,
+ resp, sizeof(*resp));
+}
+
+static int qdcp_set_state(struct qdcp_data *qdcp, unsigned int msg_id,
+ u64 id, bool enable)
+{
+ struct qdcp_state_req_v01 req = {
+ .id = id,
+ .state = enable,
+ };
+ struct qdcp_resp_v01 resp;
+
+ return qdcp_send(qdcp, msg_id, qdcp_state_req_ei, &req, sizeof(req),
+ &resp);
+}
+
+struct qdcp_devres {
+ struct qdcp_data *qdcp;
+};
+
+static void qdcp_release_provider(void *data)
+{
+ struct qdcp_devres *devres = data;
+
+ put_device(devres->qdcp->dev);
+ kref_put(&devres->qdcp->ref, qdcp_release);
+}
+
+struct qdcp_data *devm_qdcp_get(struct device *dev, const char *phandle_name)
+{
+ struct qdcp_devres *devres;
+ struct device_link *link;
+ struct qdcp_data *qdcp;
+ struct device_node *np;
+ int ret;
+
+ if (!dev->of_node)
+ return ERR_PTR(-ENOENT);
+
+ np = of_parse_phandle(dev->of_node, phandle_name, 0);
+ if (!np)
+ return ERR_PTR(-ENOENT);
+
+ mutex_lock(&qdcp_providers_lock);
+ list_for_each_entry(qdcp, &qdcp_providers, node) {
+ if (qdcp->of_node != np || qdcp->dying)
+ continue;
+
+ if (!kref_get_unless_zero(&qdcp->ref))
+ continue;
+
+ get_device(qdcp->dev);
+ mutex_unlock(&qdcp_providers_lock);
+ of_node_put(np);
+
+ /*
+ * A managed link with DL_FLAG_AUTOREMOVE_CONSUMER (i.e. no
+ * DL_FLAG_STATELESS) is required so that the driver core's
+ * device_links_busy()/device_links_unbind_consumers()
+ * machinery forces this consumer to unbind before
+ * qdcp_remove() can proceed on the supplier, and so the link
+ * itself is dropped automatically by the core when this
+ * consumer unbinds or fails to probe. A stateless link
+ * carries neither guarantee: qdcp_remove() could release the
+ * QMI handle while a consumer still has outstanding or
+ * in-flight QDCP RPCs, and device_link_del() refuses to drop
+ * a non-stateless link while the consumer is still
+ * registered, which is always the case from inside a devm
+ * release callback.
+ */
+ link = device_link_add(dev, qdcp->dev,
+ DL_FLAG_AUTOREMOVE_CONSUMER);
+ if (!link) {
+ put_device(qdcp->dev);
+ kref_put(&qdcp->ref, qdcp_release);
+ return ERR_PTR(-EINVAL);
+ }
+
+ /*
+ * The core still creates a link to a supplier that is already
+ * unbinding, and will not unbind this consumer for it. Defer
+ * rather than bind to a provider that is going away.
+ */
+ if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
+ put_device(qdcp->dev);
+ kref_put(&qdcp->ref, qdcp_release);
+ return ERR_PTR(-EPROBE_DEFER);
+ }
+
+ devres = devm_kzalloc(dev, sizeof(*devres), GFP_KERNEL);
+ if (!devres) {
+ put_device(qdcp->dev);
+ kref_put(&qdcp->ref, qdcp_release);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ devres->qdcp = qdcp;
+ ret = devm_add_action_or_reset(dev, qdcp_release_provider, devres);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return qdcp;
+ }
+ mutex_unlock(&qdcp_providers_lock);
+ of_node_put(np);
+
+ return ERR_PTR(-EPROBE_DEFER);
+}
+EXPORT_SYMBOL_GPL(devm_qdcp_get);
+
+static int qdcp_set_atid(struct qdcp_data *qdcp,
+ enum qdcp_trace_source source, u64 source_id,
+ u8 atid)
+{
+ struct qdcp_atid_set_req_v01 req = {
+ .trace_source = {
+ .type = source,
+ .id = source_id,
+ },
+ };
+ struct qdcp_resp_v01 resp;
+
+ if (atid) {
+ req.atids[0] = atid;
+ req.num_atids = 1;
+ }
+
+ return qdcp_send(qdcp, QDCP_ATID_SET, qdcp_atid_set_req_ei, &req,
+ sizeof(req), &resp);
+}
+
+int qdcp_stm_source_enable(struct qdcp_data *qdcp, u64 stm_id,
+ u8 atid, u32 port_mask, bool hwevent)
+{
+ struct qdcp_stm_set_param_req_v01 port_req = {
+ .stm_id = stm_id,
+ .param = QDCP_STM_PARAM_PORT_ENABLE,
+ .value = port_mask,
+ };
+ struct qdcp_resp_v01 resp;
+ int ret;
+
+ ret = qdcp_set_atid(qdcp, QDCP_TRACE_SOURCE_STM, stm_id, atid);
+ if (ret)
+ return ret;
+
+ if (hwevent) {
+ ret = qdcp_set_state(qdcp, QDCP_STM_HWE_SET_STATE, stm_id, true);
+ if (ret)
+ goto clear_atid;
+ }
+
+ ret = qdcp_send(qdcp, QDCP_STM_SET_PARAM, qdcp_stm_set_param_req_ei,
+ &port_req, sizeof(port_req), &resp);
+ if (ret)
+ goto disable_hwevent;
+
+ ret = qdcp_set_state(qdcp, QDCP_STM_SET_STATE, stm_id, true);
+ if (ret)
+ goto disable_hwevent;
+
+ return 0;
+
+disable_hwevent:
+ if (hwevent)
+ qdcp_set_state(qdcp, QDCP_STM_HWE_SET_STATE, stm_id, false);
+clear_atid:
+ qdcp_set_atid(qdcp, QDCP_TRACE_SOURCE_STM, stm_id, 0);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(qdcp_stm_source_enable);
+
+int qdcp_stm_source_disable(struct qdcp_data *qdcp, u64 stm_id,
+ u8 atid, bool hwevent)
+{
+ int ret, err;
+
+ /* Best effort: run every step and report the first failure. */
+ ret = qdcp_set_state(qdcp, QDCP_STM_SET_STATE, stm_id, false);
+ if (hwevent) {
+ err = qdcp_set_state(qdcp, QDCP_STM_HWE_SET_STATE, stm_id,
+ false);
+ ret = ret ?: err;
+ }
+ err = qdcp_set_atid(qdcp, QDCP_TRACE_SOURCE_STM, stm_id, 0);
+
+ return ret ?: err;
+}
+EXPORT_SYMBOL_GPL(qdcp_stm_source_disable);
+
+static int qdcp_tmc_set_param(struct qdcp_data *qdcp, u64 tmc_id,
+ enum qdcp_tmc_param param, u64 value)
+{
+ struct qdcp_tmc_set_param_req_v01 req = {
+ .tmc_id = tmc_id,
+ .param = param,
+ .value = value,
+ };
+ struct qdcp_resp_v01 resp;
+
+ return qdcp_send(qdcp, QDCP_TMC_SET_PARAM, qdcp_tmc_set_param_req_ei,
+ &req, sizeof(req), &resp);
+}
+
+int qdcp_tmc_get_param(struct qdcp_data *qdcp, u64 tmc_id,
+ enum qdcp_tmc_param param, u64 *value)
+{
+ struct qdcp_tmc_get_param_req_v01 req = {
+ .tmc_id = tmc_id,
+ .param = param,
+ };
+ struct qdcp_tmc_get_param_resp_v01 resp;
+ int ret;
+
+ ret = qdcp_send_resp(qdcp, QDCP_TMC_GET_PARAM,
+ qdcp_tmc_get_param_req_ei, &req, sizeof(req),
+ qdcp_tmc_get_param_resp_ei, &resp, sizeof(resp));
+ if (ret)
+ return ret;
+
+ if (!resp.value_valid)
+ return -EPROTO;
+
+ *value = resp.value;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(qdcp_tmc_get_param);
+
+static int qdcp_tmc_enable(struct qdcp_data *qdcp, u64 tmc_id)
+{
+ return qdcp_set_state(qdcp, QDCP_TMC_SET_STATE, tmc_id, true);
+}
+
+static int qdcp_tmc_disable(struct qdcp_data *qdcp, u64 tmc_id)
+{
+ return qdcp_set_state(qdcp, QDCP_TMC_SET_STATE, tmc_id, false);
+}
+
+static int qdcp_tmc_assign_buffer(struct qdcp_data *qdcp, u64 tmc_id,
+ phys_addr_t paddr, dma_addr_t iaddr,
+ size_t size)
+{
+ struct qdcp_tmc_assign_buffer_req_v01 req = {
+ .tmc_id = tmc_id,
+ .buffer = {
+ .paddr = paddr,
+ .iaddr = iaddr,
+ .size = size,
+ },
+ };
+ struct qdcp_resp_v01 resp;
+
+ return qdcp_send(qdcp, QDCP_TMC_ASSIGN_BUFFER,
+ qdcp_tmc_assign_buffer_req_ei, &req, sizeof(req), &resp);
+}
+
+int qdcp_tmc_etr_enable(struct qdcp_data *qdcp, const struct qdcp_tmc_cfg *cfg)
+{
+ int ret;
+
+ ret = qdcp_tmc_set_param(qdcp, cfg->tmc_id,
+ QDCP_TMC_PARAM_SG_MODE,
+ cfg->sg_mode ? 1 : 0);
+ if (ret)
+ return ret;
+
+ if (cfg->ffcr) {
+ ret = qdcp_tmc_set_param(qdcp, cfg->tmc_id,
+ QDCP_TMC_PARAM_FFCR,
+ cfg->ffcr);
+ if (ret)
+ return ret;
+ }
+
+ ret = qdcp_tmc_assign_buffer(qdcp, cfg->tmc_id, cfg->paddr, cfg->iaddr,
+ cfg->size);
+ if (ret)
+ return ret;
+
+ return qdcp_tmc_enable(qdcp, cfg->tmc_id);
+}
+EXPORT_SYMBOL_GPL(qdcp_tmc_etr_enable);
+
+int qdcp_tmc_etr_disable(struct qdcp_data *qdcp, u64 tmc_id)
+{
+ return qdcp_tmc_disable(qdcp, tmc_id);
+}
+EXPORT_SYMBOL_GPL(qdcp_tmc_etr_disable);
+
+static int qdcp_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
+{
+ struct qdcp_data *qdcp = container_of(qmi, struct qdcp_data,
+ qmi);
+
+ if (svc->version != qdcp->version || svc->instance != qdcp->instance_id)
+ return -EINVAL;
+
+ mutex_lock(&qdcp->lock);
+ qdcp->sq.sq_family = AF_QIPCRTR;
+ qdcp->sq.sq_node = svc->node;
+ qdcp->sq.sq_port = svc->port;
+ qdcp->connected = true;
+ svc->priv = qdcp;
+ mutex_unlock(&qdcp->lock);
+
+ dev_info(qdcp->dev, "connected to QECP QDCP service %u instance %u\n",
+ qdcp->service_id, qdcp->instance_id);
+ return 0;
+}
+
+static void qdcp_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
+{
+ struct qdcp_data *qdcp = svc->priv;
+
+ if (!qdcp)
+ return;
+
+ mutex_lock(&qdcp->lock);
+ /*
+ * svc->priv only proves this svc was once matched by
+ * qdcp_new_server(); it doesn't prove svc is the currently active
+ * server. A firmware restart can register a new server (updating
+ * qdcp->sq) before the old server's DEL_SERVER arrives here, so
+ * only clear the connection if this del_server is for the node/port
+ * qdcp is actually connected to.
+ */
+ if (qdcp->connected && qdcp->sq.sq_node == svc->node &&
+ qdcp->sq.sq_port == svc->port) {
+ qdcp->connected = false;
+ memset(&qdcp->sq, 0, sizeof(qdcp->sq));
+ dev_info(qdcp->dev, "disconnected from QECP QDCP service\n");
+ }
+ mutex_unlock(&qdcp->lock);
+}
+
+static void qdcp_net_reset(struct qmi_handle *qmi)
+{
+ struct qdcp_data *qdcp = container_of(qmi, struct qdcp_data,
+ qmi);
+
+ mutex_lock(&qdcp->lock);
+ qdcp->connected = false;
+ memset(&qdcp->sq, 0, sizeof(qdcp->sq));
+ mutex_unlock(&qdcp->lock);
+}
+
+static const struct qmi_ops qdcp_qmi_ops = {
+ .new_server = qdcp_new_server,
+ .del_server = qdcp_del_server,
+ .net_reset = qdcp_net_reset,
+};
+
+static void qdcp_connection_free(struct qdcp_data *qdcp)
+{
+ qmi_handle_release(&qdcp->qmi);
+ kref_put(&qdcp->ref, qdcp_release);
+}
+
+static struct qdcp_data *qdcp_connection_create(struct device *dev,
+ struct device_node *np)
+{
+ struct qdcp_data *qdcp;
+ int ret;
+
+ qdcp = kzalloc_obj(*qdcp);
+ if (!qdcp)
+ return ERR_PTR(-ENOMEM);
+
+ kref_init(&qdcp->ref);
+ qdcp->dev = get_device(dev);
+ qdcp->of_node = of_node_get(np);
+ mutex_init(&qdcp->lock);
+ INIT_LIST_HEAD(&qdcp->node);
+
+ ret = of_property_read_u32(np, "qcom,service-id", &qdcp->service_id);
+ if (ret) {
+ dev_err(dev, "%pOFn: missing qcom,service-id: %d\n", np, ret);
+ goto err_release;
+ }
+
+ ret = of_property_read_u32(np, "qcom,instance-id", &qdcp->instance_id);
+ if (ret) {
+ dev_err(dev, "%pOFn: missing qcom,instance-id: %d\n", np, ret);
+ goto err_release;
+ }
+
+ /* qcom,version is optional; default to version 1 when absent. */
+ if (of_property_read_u32(np, "qcom,version", &qdcp->version))
+ qdcp->version = 1;
+
+ ret = qmi_handle_init(&qdcp->qmi, QDCP_MAX_MSG_LEN, &qdcp_qmi_ops, NULL);
+ if (ret) {
+ dev_err(dev, "%pOFn: failed to initialize QMI handle: %d\n",
+ np, ret);
+ goto err_release;
+ }
+
+ ret = qmi_add_lookup(&qdcp->qmi, qdcp->service_id, qdcp->version,
+ qdcp->instance_id);
+ if (ret) {
+ dev_err(dev, "%pOFn: failed to add QMI lookup: %d\n", np, ret);
+ qmi_handle_release(&qdcp->qmi);
+ goto err_release;
+ }
+
+ dev_info(dev, "%pOFn: registered QDCP connection service=%u version=%u instance=%u\n",
+ np, qdcp->service_id, qdcp->version, qdcp->instance_id);
+ return qdcp;
+
+err_release:
+ kref_put(&qdcp->ref, qdcp_release);
+ return ERR_PTR(ret);
+}
+
+static int qdcp_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct qdcp_data *qdcp, *tmp;
+ LIST_HEAD(conn_list);
+ int ret;
+
+ for_each_available_child_of_node_scoped(dev->of_node, np) {
+ qdcp = qdcp_connection_create(dev, np);
+ if (IS_ERR(qdcp)) {
+ ret = PTR_ERR(qdcp);
+ goto err_unwind;
+ }
+ list_add_tail(&qdcp->node, &conn_list);
+ }
+
+ /*
+ * A provider with no connection-N children is valid, since the binding
+ * cannot require one; it simply has nothing for consumers to reference.
+ */
+ if (list_empty(&conn_list))
+ dev_warn(dev, "no QDCP connection child nodes found\n");
+
+ mutex_lock(&qdcp_providers_lock);
+ list_splice_tail(&conn_list, &qdcp_providers);
+ mutex_unlock(&qdcp_providers_lock);
+
+ return 0;
+
+err_unwind:
+ list_for_each_entry_safe(qdcp, tmp, &conn_list, node) {
+ list_del(&qdcp->node);
+ qdcp_connection_free(qdcp);
+ }
+ return ret;
+}
+
+static void qdcp_remove(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct qdcp_data *qdcp, *tmp;
+ LIST_HEAD(removed);
+
+ mutex_lock(&qdcp_providers_lock);
+ list_for_each_entry_safe(qdcp, tmp, &qdcp_providers, node) {
+ if (qdcp->dev != dev)
+ continue;
+ list_move_tail(&qdcp->node, &removed);
+ }
+ mutex_unlock(&qdcp_providers_lock);
+
+ list_for_each_entry_safe(qdcp, tmp, &removed, node) {
+ mutex_lock(&qdcp->lock);
+ qdcp->dying = true;
+ qdcp->connected = false;
+ mutex_unlock(&qdcp->lock);
+
+ list_del(&qdcp->node);
+ qdcp_connection_free(qdcp);
+ }
+}
+
+static const struct of_device_id qdcp_match[] = {
+ { .compatible = "qcom,qdcp" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, qdcp_match);
+
+static struct platform_driver qdcp_driver = {
+ .probe = qdcp_probe,
+ .remove = qdcp_remove,
+ .driver = {
+ .name = "qcom-qdcp",
+ .of_match_table = qdcp_match,
+ },
+};
+module_platform_driver(qdcp_driver);
+
+MODULE_DESCRIPTION("Qualcomm Debug Control Protocol provider");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/soc/qcom/qdcp.h b/include/linux/soc/qcom/qdcp.h
new file mode 100644
index 000000000000..cff50bae3c15
--- /dev/null
+++ b/include/linux/soc/qcom/qdcp.h
@@ -0,0 +1,128 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#ifndef _LINUX_SOC_QCOM_QDCP_H
+#define _LINUX_SOC_QCOM_QDCP_H
+
+#include <linux/err.h>
+#include <linux/kconfig.h>
+#include <linux/errno.h>
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+struct device;
+struct qdcp_data;
+
+/**
+ * struct qdcp_consumer - per-consumer QDCP connection state.
+ * @provider: QDCP QMI connection this consumer was resolved against.
+ * @id: firmware identifier for this consumer within @provider (STM source
+ * id or TMC id, depending on which consumer embeds this struct).
+ * @lock: serializes this consumer's QDCP firmware calls together with
+ * whatever consumer-local state (mode, refcount, buffers, ...) must
+ * stay atomic with them. Needed because QDCP enable/disable makes a
+ * sleeping firmware RPC and therefore cannot be guarded by a spinlock.
+ *
+ * Embedded by Qualcomm CoreSight consumer drivers (QDCP STM source, QDCP
+ * TMC/ETR sink) that talk to a firmware-owned trace block through QDCP.
+ */
+struct qdcp_consumer {
+ struct qdcp_data *provider;
+ u64 id;
+ /* Serializes this consumer's QDCP firmware calls; see field comment. */
+ struct mutex lock;
+};
+
+/*
+ * QDCP TMC parameter identifiers used in QDCP_TMC_GET_PARAM/SET_PARAM
+ * requests. These are firmware-defined parameter IDs, not a mirror of the
+ * TMC MMIO register map: STS, FFCR, and DEVID happen to share their values
+ * with the corresponding TMC_STS/TMC_FFCR/CORESIGHT_DEVID register offsets
+ * because firmware proxies those specific parameters as literal register
+ * reads, but SG_MODE, RRP, and RWP are firmware-only parameter IDs with no
+ * corresponding MMIO offset. Do not "align" RRP/RWP to TMC_RRP/TMC_RWP —
+ * that would send the wrong parameter ID to firmware.
+ */
+enum qdcp_tmc_param {
+ QDCP_TMC_PARAM_STS = 0x00c,
+ QDCP_TMC_PARAM_FFCR = 0x304,
+ QDCP_TMC_PARAM_SG_MODE = 0x1002,
+ QDCP_TMC_PARAM_RRP = 0x1003,
+ QDCP_TMC_PARAM_RWP = 0x1004,
+ QDCP_TMC_PARAM_DEVID = 0xfc8,
+};
+
+struct qdcp_tmc_cfg {
+ u64 tmc_id;
+ phys_addr_t paddr;
+ dma_addr_t iaddr;
+ size_t size;
+ bool sg_mode;
+ u32 ffcr;
+};
+
+#if IS_REACHABLE(CONFIG_QCOM_QDCP) || defined(QCOM_QDCP_PROVIDER)
+/**
+ * devm_qdcp_get() - look up a QDCP connection by phandle
+ * @dev: consumer device whose DT node holds @phandle_name
+ * @phandle_name: name of the phandle property, e.g. "qcom,qdcp"
+ *
+ * @phandle_name must reference a "connection-N" child node of a
+ * "qcom,qdcp" provider (see
+ * Documentation/devicetree/bindings/soc/qcom/qcom,qdcp.yaml), not the
+ * top-level provider node itself. A phandle to the provider node will
+ * never match and the lookup will keep returning -EPROBE_DEFER.
+ *
+ * Return: connection handle, or an error pointer on failure.
+ */
+struct qdcp_data *devm_qdcp_get(struct device *dev, const char *phandle_name);
+int qdcp_stm_source_enable(struct qdcp_data *qdcp, u64 stm_id, u8 atid,
+ u32 port_mask, bool hwevent);
+int qdcp_stm_source_disable(struct qdcp_data *qdcp, u64 stm_id, u8 atid,
+ bool hwevent);
+int qdcp_tmc_get_param(struct qdcp_data *qdcp, u64 tmc_id,
+ enum qdcp_tmc_param param, u64 *value);
+int qdcp_tmc_etr_enable(struct qdcp_data *qdcp,
+ const struct qdcp_tmc_cfg *cfg);
+int qdcp_tmc_etr_disable(struct qdcp_data *qdcp, u64 tmc_id);
+#else
+static inline struct qdcp_data *
+devm_qdcp_get(struct device *dev, const char *phandle_name)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int qdcp_stm_source_enable(struct qdcp_data *qdcp, u64 stm_id,
+ u8 atid, u32 port_mask, bool hwevent)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int qdcp_stm_source_disable(struct qdcp_data *qdcp,
+ u64 stm_id, u8 atid, bool hwevent)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int
+qdcp_tmc_get_param(struct qdcp_data *qdcp, u64 tmc_id,
+ enum qdcp_tmc_param param, u64 *value)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int
+qdcp_tmc_etr_enable(struct qdcp_data *qdcp, const struct qdcp_tmc_cfg *cfg)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int qdcp_tmc_etr_disable(struct qdcp_data *qdcp, u64 tmc_id)
+{
+ return -EOPNOTSUPP;
+}
+#endif
+
+#endif

--
2.34.1