[PATCH v2 6/8] coresight: qcom: add QECP owned STM driver
From: Jie Gan
Date: Mon Nov 30 2026 - 02:12:56 EST
On the Qualcomm Hawi and Maili SoCs, a CoreSight STM is owned by the
Qualcomm Embedded Co-Processor (QECP). Its control registers are not
accessible from the application processor; the QECP firmware enables
and disables the STM on request over the Qualcomm Debug Control
Protocol (QDCP). The stimulus ports remain accessible for writing trace
data.
Add a driver for this STM using the QDCP client library. Trace data is
written to the stimulus ports through the generic STM framework, once
the firmware has confirmed the STM is enabled, as the firmware owns its
clocks and power. The number of stimulus ports and the fundamental data
size are read from STMDEVID and STMSPFEAT2R through the firmware. The
firmware identifies the STM by the base address of its control register
region, which is not mapped.
If the firmware does not confirm a stop, the trace ID is kept allocated
on removal, so that no other source reuses an ID the firmware may still
apply.
Signed-off-by: Jie Gan <jie.gan@xxxxxxxxxxxxxxxx>
---
drivers/hwtracing/coresight/Kconfig | 17 +
drivers/hwtracing/coresight/Makefile | 2 +
.../hwtracing/coresight/coresight-qcom-qdcp-stm.c | 614 +++++++++++++++++++++
3 files changed, 633 insertions(+)
diff --git a/drivers/hwtracing/coresight/Kconfig b/drivers/hwtracing/coresight/Kconfig
index 6a4239ebb582..0f47b7da37f4 100644
--- a/drivers/hwtracing/coresight/Kconfig
+++ b/drivers/hwtracing/coresight/Kconfig
@@ -119,6 +119,23 @@ config ETM4X_IMPDEF_FEATURE
ETM 4.x tracer module that can't reduce commit rate automatically.
This avoids overflow between the ETM tracer module and the cpu core.
+config CORESIGHT_QCOM_QDCP_STM
+ tristate "Qualcomm QECP owned CoreSight STM driver"
+ depends on ARCH_QCOM || COMPILE_TEST
+ depends on NET
+ select CORESIGHT_LINKS_AND_SINKS
+ select QCOM_QDCP
+ select STM
+ help
+ This driver supports the CoreSight System Trace Macrocell (STM) owned
+ by the Qualcomm Embedded Co-Processor (QECP) on some Qualcomm SoCs.
+ Trace data is written to the STM stimulus ports directly, while the
+ STM is enabled and disabled by the QECP firmware over the Qualcomm
+ Debug Control Protocol (QDCP).
+
+ To compile this driver as a module, choose M here: the module will
+ be called coresight-qcom-qdcp-stm.
+
config CORESIGHT_STM
tristate "CoreSight System Trace Macrocell driver"
depends on (ARM && !(CPU_32v3 || CPU_32v4 || CPU_32v4T)) || ARM64
diff --git a/drivers/hwtracing/coresight/Makefile b/drivers/hwtracing/coresight/Makefile
index ab16d06783a5..d97e334fe1fc 100644
--- a/drivers/hwtracing/coresight/Makefile
+++ b/drivers/hwtracing/coresight/Makefile
@@ -23,6 +23,7 @@ condflags := \
subdir-ccflags-y += $(condflags)
CFLAGS_coresight-stm.o := -D__DISABLE_TRACE_MMIO__
+CFLAGS_coresight-qcom-qdcp-stm.o := -D__DISABLE_TRACE_MMIO__
obj-$(CONFIG_CORESIGHT) += coresight.o
coresight-y := coresight-core.o coresight-etm-perf.o coresight-platform.o \
@@ -43,6 +44,7 @@ coresight-etm3x-y := coresight-etm3x-core.o coresight-etm-cp14.o \
obj-$(CONFIG_CORESIGHT_SOURCE_ETM4X) += coresight-etm4x.o
coresight-etm4x-y := coresight-etm4x-core.o coresight-etm4x-sysfs.o \
coresight-etm4x-cfg.o
+obj-$(CONFIG_CORESIGHT_QCOM_QDCP_STM) += coresight-qcom-qdcp-stm.o
obj-$(CONFIG_CORESIGHT_STM) += coresight-stm.o
obj-$(CONFIG_CORESIGHT_CPU_DEBUG) += coresight-cpu-debug.o
obj-$(CONFIG_CORESIGHT_CATU) += coresight-catu.o
diff --git a/drivers/hwtracing/coresight/coresight-qcom-qdcp-stm.c b/drivers/hwtracing/coresight/coresight-qcom-qdcp-stm.c
new file mode 100644
index 000000000000..f8539bffb291
--- /dev/null
+++ b/drivers/hwtracing/coresight/coresight-qcom-qdcp-stm.c
@@ -0,0 +1,614 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ *
+ * Qualcomm QECP owned CoreSight STM driver. The STM control registers are
+ * owned by the QECP firmware, which enables and disables the STM over QDCP.
+ * The stimulus ports are written directly.
+ */
+
+#include <linux/bitmap.h>
+#include <linux/coresight.h>
+#include <linux/coresight-stm.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/log2.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/perf_event.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/stm.h>
+#include <linux/string.h>
+
+#include "coresight-priv.h"
+#include <linux/soc/qcom/qdcp.h>
+#include "coresight-trace-id.h"
+
+#define QDCP_STM_BYTES_PER_CHANNEL 256
+#define QDCP_STM_SERVICE_TIMEOUT_MS 10000
+#define QDCP_STM_PKT_TYPE_DATA 0x98
+#define QDCP_STM_PKT_TYPE_FLAG 0xe8
+
+#define qdcp_stm_channel_addr(drvdata, channel) \
+ ((drvdata)->stimulus_base + \
+ ((channel) * QDCP_STM_BYTES_PER_CHANNEL))
+#define qdcp_stm_channel_off(type, options) ((type) & ~(options))
+
+/**
+ * struct qdcp_stm_match_data - SoC specific QECP STM description
+ * @svc: QDCP service controlling the STM.
+ */
+struct qdcp_stm_match_data {
+ struct qdcp_service svc;
+};
+
+/**
+ * struct qcom_qdcp_stm_drvdata - QDCP STM source data.
+ * @stm: generic STM interface handle.
+ * @csdev: CoreSight source device.
+ * @stimulus_base: mapped AP-visible STM stimulus aperture.
+ * @stimulus_phys: physical base of the STM stimulus aperture.
+ * @guaranteed: per-channel guaranteed/invariant delivery bitmap.
+ * @client: QDCP client used to control the STM.
+ * @source_id: firmware identifier of the STM, the base address of its
+ * control registers.
+ * @lock: serializes the firmware requests and @fw_stop_failed.
+ * @fw_enabled: firmware confirmed the STM is enabled, so the stimulus ports
+ * can be written.
+ * @traceid: CoreSight ATID assigned to this source.
+ * @fw_stop_failed: the last firmware stop was not confirmed, so firmware may
+ * still be tagging trace with @traceid.
+ * @numsp: number of stimulus ports.
+ * @write_bytes: maximum naturally aligned stimulus write size.
+ * @port_mask: stimulus port enable mask sent to firmware. Like the STMSPER
+ * register it mirrors, this is a 32-bit mask applied per 32-port group,
+ * so enabling every bit covers all ports regardless of @numsp.
+ */
+struct qcom_qdcp_stm_drvdata {
+ struct stm_data stm;
+ struct coresight_device *csdev;
+ void __iomem *stimulus_base;
+ phys_addr_t stimulus_phys;
+ unsigned long *guaranteed;
+ struct qdcp_client *client;
+ u64 source_id;
+ /* Serializes the firmware requests and @fw_stop_failed */
+ struct mutex lock;
+ bool fw_enabled;
+ u8 traceid;
+ bool fw_stop_failed;
+ u32 numsp;
+ u32 write_bytes;
+ u32 port_mask;
+};
+
+static bool qdcp_stm_addr_unaligned(const void *addr, u8 write_bytes)
+{
+ return (unsigned long)addr & (write_bytes - 1);
+}
+
+static void qdcp_stm_send(void __iomem *addr, const void *data,
+ u32 size, u8 write_bytes)
+{
+ u8 payload[8];
+
+ if (qdcp_stm_addr_unaligned(data, write_bytes)) {
+ memcpy(payload, data, size);
+ data = payload;
+ }
+
+ switch (size) {
+#ifdef CONFIG_64BIT
+ case 8:
+ writeq_relaxed(*(u64 *)data, addr);
+ break;
+#endif
+ case 4:
+ writel_relaxed(*(u32 *)data, addr);
+ break;
+ case 2:
+ writew_relaxed(*(u16 *)data, addr);
+ break;
+ case 1:
+ writeb_relaxed(*(u8 *)data, addr);
+ break;
+ default:
+ break;
+ }
+}
+
+static ssize_t notrace qdcp_stm_packet(struct stm_data *stm_data,
+ unsigned int master, unsigned int channel,
+ unsigned int packet, unsigned int flags,
+ unsigned int size,
+ const unsigned char *payload)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+ void __iomem *channel_addr;
+ unsigned int stm_flags;
+
+ drvdata = container_of(stm_data, struct qcom_qdcp_stm_drvdata, stm);
+ /*
+ * The firmware owns the STM clocks and power, only write to the
+ * stimulus ports once it has confirmed the STM is enabled.
+ */
+ if (!READ_ONCE(drvdata->fw_enabled))
+ return -EACCES;
+
+ if (channel >= drvdata->numsp)
+ return -EINVAL;
+
+ channel_addr = qdcp_stm_channel_addr(drvdata, channel);
+ stm_flags = flags & STP_PACKET_TIMESTAMPED ? STM_FLAG_TIMESTAMPED : 0;
+ stm_flags |= test_bit(channel, drvdata->guaranteed) ?
+ STM_FLAG_GUARANTEED : 0;
+
+ switch (packet) {
+ case STP_PACKET_FLAG:
+ channel_addr += qdcp_stm_channel_off(QDCP_STM_PKT_TYPE_FLAG,
+ stm_flags);
+ qdcp_stm_send(channel_addr, payload, 1, drvdata->write_bytes);
+ return 1;
+ case STP_PACKET_DATA:
+ if (!size)
+ return 0;
+ if (size > drvdata->write_bytes)
+ size = drvdata->write_bytes;
+ else
+ size = rounddown_pow_of_two(size);
+
+ stm_flags |= flags & STP_PACKET_MARKED ? STM_FLAG_MARKED : 0;
+ channel_addr += qdcp_stm_channel_off(QDCP_STM_PKT_TYPE_DATA,
+ stm_flags);
+ qdcp_stm_send(channel_addr, payload, size, drvdata->write_bytes);
+ return size;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static phys_addr_t qdcp_stm_mmio_addr(struct stm_data *stm_data,
+ unsigned int master, unsigned int channel,
+ unsigned int nr_chans)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+ phys_addr_t addr;
+
+ drvdata = container_of(stm_data, struct qcom_qdcp_stm_drvdata, stm);
+ addr = drvdata->stimulus_phys +
+ channel * QDCP_STM_BYTES_PER_CHANNEL;
+
+ if (offset_in_page(addr) ||
+ offset_in_page(nr_chans * QDCP_STM_BYTES_PER_CHANNEL))
+ return 0;
+
+ return addr;
+}
+
+static int qdcp_stm_link(struct stm_data *stm_data, unsigned int master,
+ unsigned int channel)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+
+ drvdata = container_of(stm_data, struct qcom_qdcp_stm_drvdata, stm);
+ if (!drvdata->csdev)
+ return -EINVAL;
+
+ return coresight_enable_sysfs(drvdata->csdev);
+}
+
+static void qdcp_stm_unlink(struct stm_data *stm_data, unsigned int master,
+ unsigned int channel)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+
+ drvdata = container_of(stm_data, struct qcom_qdcp_stm_drvdata, stm);
+ if (drvdata->csdev)
+ coresight_disable_sysfs(drvdata->csdev);
+}
+
+static long qdcp_stm_set_options(struct stm_data *stm_data,
+ unsigned int master, unsigned int channel,
+ unsigned int nr_chans, unsigned long options)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+
+ drvdata = container_of(stm_data, struct qcom_qdcp_stm_drvdata, stm);
+ if (!coresight_get_mode(drvdata->csdev))
+ return -EINVAL;
+
+ if (channel >= drvdata->numsp)
+ return -EINVAL;
+
+ switch (options) {
+ case STM_OPTION_GUARANTEED:
+ set_bit(channel, drvdata->guaranteed);
+ break;
+ case STM_OPTION_INVARIANT:
+ clear_bit(channel, drvdata->guaranteed);
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+/*
+ * Stop the STM in the reverse order of qdcp_stm_enable(): disable the
+ * stimulus ports, the STM and clear its trace ID. Every step is attempted,
+ * the result tells whether the firmware confirmed the STM no longer uses
+ * the trace ID. Called with drvdata->lock held.
+ */
+static int qdcp_stm_fw_stop(struct qcom_qdcp_stm_drvdata *drvdata)
+{
+ int ret, err;
+
+ qdcp_stm_set_port_mask(drvdata->client, drvdata->source_id, 0);
+ ret = qdcp_source_disable(drvdata->client, QDCP_SOURCE_STM,
+ drvdata->source_id);
+ err = qdcp_source_set_atid(drvdata->client, QDCP_SOURCE_STM,
+ drvdata->source_id, 0);
+
+ return ret ?: err;
+}
+
+static int qdcp_stm_enable(struct coresight_device *csdev,
+ struct perf_event *event, enum cs_mode mode,
+ struct coresight_path *path)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+ int ret;
+
+ if (mode != CS_MODE_SYSFS)
+ return -EINVAL;
+
+ if (!coresight_take_mode(csdev, mode))
+ return -EBUSY;
+
+ drvdata = dev_get_drvdata(csdev->dev.parent);
+
+ mutex_lock(&drvdata->lock);
+ ret = qdcp_source_set_atid(drvdata->client, QDCP_SOURCE_STM,
+ drvdata->source_id, drvdata->traceid);
+ if (!ret)
+ ret = qdcp_stm_set_port_mask(drvdata->client,
+ drvdata->source_id,
+ drvdata->port_mask);
+ if (!ret)
+ ret = qdcp_source_enable(drvdata->client, QDCP_SOURCE_STM,
+ drvdata->source_id);
+ if (ret) {
+ /*
+ * A failed or timed out request may still have been executed
+ * by firmware, make sure the STM is stopped.
+ */
+ drvdata->fw_stop_failed = !!qdcp_stm_fw_stop(drvdata);
+ } else {
+ drvdata->fw_stop_failed = false;
+ WRITE_ONCE(drvdata->fw_enabled, true);
+ }
+ mutex_unlock(&drvdata->lock);
+
+ if (ret) {
+ coresight_set_mode(csdev, CS_MODE_DISABLED);
+ return ret;
+ }
+
+ dev_dbg(&csdev->dev, "QDCP STM tracing enabled\n");
+ return 0;
+}
+
+static void qdcp_stm_disable(struct coresight_device *csdev,
+ struct perf_event *event)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+ int ret;
+
+ if (coresight_get_mode(csdev) != CS_MODE_SYSFS)
+ return;
+
+ drvdata = dev_get_drvdata(csdev->dev.parent);
+ WRITE_ONCE(drvdata->fw_enabled, false);
+
+ mutex_lock(&drvdata->lock);
+ ret = qdcp_stm_fw_stop(drvdata);
+ drvdata->fw_stop_failed = !!ret;
+ mutex_unlock(&drvdata->lock);
+
+ /*
+ * The source disable operation cannot fail, the source is disabled
+ * from the kernel point of view either way.
+ */
+ coresight_set_mode(csdev, CS_MODE_DISABLED);
+ if (ret)
+ dev_warn(&csdev->dev,
+ "firmware did not confirm QDCP STM disable: %d\n", ret);
+ else
+ dev_dbg(&csdev->dev, "QDCP STM tracing disabled\n");
+}
+
+static int qdcp_stm_trace_id(struct coresight_device *csdev,
+ enum cs_mode mode, struct coresight_device *sink)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata;
+
+ drvdata = dev_get_drvdata(csdev->dev.parent);
+ return drvdata->traceid;
+}
+
+static const struct coresight_ops_source qdcp_stm_source_ops = {
+ .enable = qdcp_stm_enable,
+ .disable = qdcp_stm_disable,
+};
+
+static const struct coresight_ops qdcp_stm_cs_ops = {
+ .trace_id = qdcp_stm_trace_id,
+ .source_ops = &qdcp_stm_source_ops,
+};
+
+static ssize_t traceid_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata = dev_get_drvdata(dev->parent);
+
+ return sysfs_emit(buf, "%#x\n", drvdata->traceid);
+}
+static DEVICE_ATTR_RO(traceid);
+
+static struct attribute *qdcp_stm_attrs[] = {
+ &dev_attr_traceid.attr,
+ NULL,
+};
+
+static const struct attribute_group qdcp_stm_group = {
+ .attrs = qdcp_stm_attrs,
+};
+
+static const struct attribute_group *qdcp_stm_groups[] = {
+ &qdcp_stm_group,
+ NULL,
+};
+
+/*
+ * The STM control registers are only accessible from the QECP, nothing may
+ * access them through csdev->access.
+ */
+static u64 qdcp_stm_access_read(u32 offset, bool relaxed, bool _64bit)
+{
+ WARN_ONCE(1, "QDCP STM register 0x%x is not accessible\n", offset);
+ return 0;
+}
+
+static void qdcp_stm_access_write(u64 val, u32 offset, bool relaxed,
+ bool _64bit)
+{
+ WARN_ONCE(1, "QDCP STM register 0x%x is not accessible\n", offset);
+}
+
+static void qdcp_stm_init_stm_data(struct qcom_qdcp_stm_drvdata *drvdata,
+ const char *name)
+{
+ struct stm_data *stm = &drvdata->stm;
+
+ stm->name = name;
+ stm->sw_start = 1;
+ stm->sw_end = 1;
+ stm->hw_override = true;
+ stm->sw_nchannels = drvdata->numsp;
+ stm->sw_mmiosz = QDCP_STM_BYTES_PER_CHANNEL;
+ stm->packet = qdcp_stm_packet;
+ stm->mmio_addr = qdcp_stm_mmio_addr;
+ stm->link = qdcp_stm_link;
+ stm->unlink = qdcp_stm_unlink;
+ stm->set_options = qdcp_stm_set_options;
+}
+
+static int qdcp_stm_probe(struct platform_device *pdev)
+{
+ const struct qdcp_stm_match_data *data;
+ struct qcom_qdcp_stm_drvdata *drvdata;
+ struct coresight_platform_data *pdata;
+ struct coresight_desc desc = { 0 };
+ struct device *dev = &pdev->dev;
+ struct resource *ctrl, *res;
+ u32 devid, spfeat2r;
+ const char *name;
+ u32 numsp;
+ int trace_id;
+ int ret;
+
+ data = device_get_match_data(dev);
+ if (!data)
+ return -EINVAL;
+
+ /*
+ * The firmware identifies the STM by the base address of its control
+ * registers. They are only accessible from the QECP, so the region is
+ * not mapped.
+ */
+ ctrl = platform_get_resource_byname(pdev, IORESOURCE_MEM, "stm-base");
+ if (!ctrl)
+ return dev_err_probe(dev, -EINVAL, "missing STM control region\n");
+
+ res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
+ "stm-stimulus-base");
+ if (!res)
+ return dev_err_probe(dev, -EINVAL, "missing stimulus port region\n");
+
+ drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
+ if (!drvdata)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, drvdata);
+
+ /*
+ * Get the client first, so that it is released last and can still be
+ * used from qdcp_stm_remove().
+ */
+ drvdata->client = devm_qdcp_client_get(dev, &data->svc);
+ if (IS_ERR(drvdata->client))
+ return dev_err_probe(dev, PTR_ERR(drvdata->client),
+ "failed to create QDCP client\n");
+ drvdata->source_id = ctrl->start;
+
+ drvdata->stimulus_base = devm_ioremap_resource(dev, res);
+ if (IS_ERR(drvdata->stimulus_base))
+ return PTR_ERR(drvdata->stimulus_base);
+ drvdata->stimulus_phys = res->start;
+
+ /* The STM configuration is read from its registers through firmware */
+ ret = qdcp_client_wait_for_service(drvdata->client,
+ QDCP_STM_SERVICE_TIMEOUT_MS);
+ if (ret)
+ return dev_err_probe(dev, -EPROBE_DEFER,
+ "QDCP service not available\n");
+
+ ret = qdcp_stm_get_param(drvdata->client, drvdata->source_id,
+ QDCP_STM_PARAM_DEVID, &devid);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to read QDCP STM DEVID\n");
+
+ ret = qdcp_stm_get_param(drvdata->client, drvdata->source_id,
+ QDCP_STM_PARAM_SPFEAT2R, &spfeat2r);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "failed to read QDCP STM SPFEAT2R\n");
+
+ /* NUMSP is 17 bits wide, 0 means 32 stimulus ports */
+ numsp = devid & GENMASK(16, 0);
+ if (!numsp)
+ numsp = 32;
+ if ((u64)numsp * QDCP_STM_BYTES_PER_CHANNEL > resource_size(res))
+ return dev_err_probe(dev, -EINVAL,
+ "%u stimulus ports do not fit in %pR\n",
+ numsp, res);
+
+ drvdata->numsp = numsp;
+ /* SPFEAT2R[15:12] is the fundamental data size, 0 for 32 bits */
+ drvdata->write_bytes = IS_ENABLED(CONFIG_64BIT) &&
+ BMVAL(spfeat2r, 12, 15) ? 8 : 4;
+ /*
+ * STMSPER-style mask: enable all ports in every 32-port group. The
+ * firmware applies it to whichever group is active, so this covers
+ * all ports irrespective of numsp.
+ */
+ drvdata->port_mask = U32_MAX;
+ drvdata->guaranteed = devm_bitmap_zalloc(dev, numsp, GFP_KERNEL);
+ if (!drvdata->guaranteed)
+ return -ENOMEM;
+
+ ret = devm_mutex_init(dev, &drvdata->lock);
+ if (ret)
+ return ret;
+
+ name = coresight_alloc_device_name("qdcp_stm", dev);
+ if (!name)
+ return -ENOMEM;
+
+ trace_id = coresight_trace_id_get_system_id();
+ if (trace_id < 0)
+ return trace_id;
+ drvdata->traceid = trace_id;
+
+ pdata = coresight_get_platform_data(dev);
+ if (IS_ERR(pdata)) {
+ ret = PTR_ERR(pdata);
+ goto err_trace_id_put;
+ }
+ dev->platform_data = pdata;
+
+ desc.name = name;
+ desc.type = CORESIGHT_DEV_TYPE_SOURCE;
+ desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE;
+ desc.ops = &qdcp_stm_cs_ops;
+ desc.pdata = pdata;
+ desc.dev = dev;
+ desc.groups = qdcp_stm_groups;
+ desc.access.read = qdcp_stm_access_read;
+ desc.access.write = qdcp_stm_access_write;
+ drvdata->csdev = coresight_register(&desc);
+ if (IS_ERR(drvdata->csdev)) {
+ ret = PTR_ERR(drvdata->csdev);
+ goto err_trace_id_put;
+ }
+
+ /*
+ * The STM callbacks use drvdata->csdev and drvdata->traceid and are
+ * reachable as soon as the STM device is registered.
+ */
+ qdcp_stm_init_stm_data(drvdata, name);
+ ret = stm_register_device(dev, &drvdata->stm, THIS_MODULE);
+ if (ret) {
+ dev_err(dev, "failed to register STM: %d\n", ret);
+ goto err_coresight_unregister;
+ }
+
+ return 0;
+
+err_coresight_unregister:
+ coresight_unregister(drvdata->csdev);
+err_trace_id_put:
+ coresight_trace_id_put_system_id(drvdata->traceid);
+ return ret;
+}
+
+static void qdcp_stm_remove(struct platform_device *pdev)
+{
+ struct qcom_qdcp_stm_drvdata *drvdata = platform_get_drvdata(pdev);
+
+ /*
+ * An enabled trace path and an open STM device both hold a reference
+ * on this module, and unbinding through sysfs is not allowed, so the
+ * STM is already disabled here.
+ */
+ WARN_ON_ONCE(coresight_get_mode(drvdata->csdev) != CS_MODE_DISABLED);
+
+ stm_unregister_device(&drvdata->stm);
+ coresight_unregister(drvdata->csdev);
+
+ /*
+ * If the last stop was not confirmed, the firmware may still tag
+ * trace with this ID. Keep it allocated rather than let another
+ * source reuse it.
+ */
+ if (drvdata->fw_stop_failed)
+ dev_warn(&pdev->dev,
+ "QDCP STM not confirmed stopped, leaking trace ID %u\n",
+ drvdata->traceid);
+ else
+ coresight_trace_id_put_system_id(drvdata->traceid);
+}
+
+static const struct qdcp_stm_match_data hawi_qdcp_stm_data = {
+ .svc = {
+ .service_id = 0x453,
+ .version = 1,
+ .instance_id = 32,
+ },
+};
+
+static const struct of_device_id qdcp_stm_match[] = {
+ { .compatible = "qcom,hawi-qecp-stm", .data = &hawi_qdcp_stm_data },
+ {}
+};
+MODULE_DEVICE_TABLE(of, qdcp_stm_match);
+
+static struct platform_driver qdcp_stm_driver = {
+ .probe = qdcp_stm_probe,
+ .remove = qdcp_stm_remove,
+ .driver = {
+ .name = "coresight-qcom-qdcp-stm",
+ .of_match_table = qdcp_stm_match,
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
+ .suppress_bind_attrs = true,
+ },
+};
+module_platform_driver(qdcp_stm_driver);
+
+MODULE_DESCRIPTION("Qualcomm QDCP CoreSight STM source driver");
+MODULE_AUTHOR("Jie Gan <jie.gan@xxxxxxxxxxxxxxxx>");
+MODULE_LICENSE("GPL");
--
2.34.1