[PATCH v2 8/8] coresight: tmc: add QECP owned TMC driver

From: Jie Gan

Date: Mon Nov 30 2026 - 02:13:08 EST


On the Qualcomm Hawi and Maili SoCs, a CoreSight TMC is owned by the
Qualcomm Embedded Co-Processor (QECP). Its registers are not accessible
from the application processor; the QECP firmware configures, enables
and disables the TMC on request over the Qualcomm Debug Control Protocol
(QDCP).

Add a driver for this TMC, as a separate module using the QDCP client
library. The TMC configuration is read from DEVID through the firmware,
and only the ETR configuration is supported, in sysfs mode. The trace
buffer is allocated by the kernel, handed to the firmware, and read
through the TMC device node with the generic ETR sysfs buffer handling.
The firmware identifies the TMC by the base address of its register
region, which is not mapped. The trigger_cntr attribute is not exposed,
as the driver does not program TMC_TRG.

Export the sysfs buffer helpers, the common ETR probe initialisation and
the ETR and TMC attribute groups of the TMC driver for the new driver,
and add tmc_etr_get_ffcr() for the FFCR value programmed when enabling
an ETR.

The firmware may write into the trace buffer until it confirms a stop,
so the buffer is only released once the stop is confirmed, and the ETR
is also stopped on shutdown.

Signed-off-by: Jie Gan <jie.gan@xxxxxxxxxxxxxxxx>
---
drivers/hwtracing/coresight/Kconfig | 17 +
drivers/hwtracing/coresight/Makefile | 1 +
drivers/hwtracing/coresight/coresight-tmc-core.c | 8 +-
drivers/hwtracing/coresight/coresight-tmc-etr.c | 32 +-
drivers/hwtracing/coresight/coresight-tmc-qdcp.c | 734 +++++++++++++++++++++++
drivers/hwtracing/coresight/coresight-tmc.h | 8 +
6 files changed, 787 insertions(+), 13 deletions(-)

diff --git a/drivers/hwtracing/coresight/Kconfig b/drivers/hwtracing/coresight/Kconfig
index 0f47b7da37f4..48ebc4b93d43 100644
--- a/drivers/hwtracing/coresight/Kconfig
+++ b/drivers/hwtracing/coresight/Kconfig
@@ -59,6 +59,23 @@ config CORESIGHT_CATU
To compile this driver as a module, choose M here: the
module will be called coresight-catu.

+config CORESIGHT_TMC_QDCP
+ tristate "Qualcomm QECP owned TMC driver"
+ depends on CORESIGHT_LINK_AND_SINK_TMC
+ depends on ARCH_QCOM || COMPILE_TEST
+ depends on NET
+ select QCOM_QDCP
+ help
+ This driver supports the CoreSight TMC owned by the Qualcomm
+ Embedded Co-Processor (QECP) on some Qualcomm SoCs. The TMC is
+ configured, enabled and disabled by the QECP firmware over the
+ Qualcomm Debug Control Protocol (QDCP). Only a TMC configured as an
+ ETR is supported, in sysfs mode, with its trace buffer allocated by
+ the kernel.
+
+ To compile this driver as a module, choose M here: the module will
+ be called coresight-tmc-qdcp.
+
config CORESIGHT_SINK_TPIU
tristate "Coresight generic TPIU driver"
depends on CORESIGHT_LINKS_AND_SINKS
diff --git a/drivers/hwtracing/coresight/Makefile b/drivers/hwtracing/coresight/Makefile
index d97e334fe1fc..1bc462e965a6 100644
--- a/drivers/hwtracing/coresight/Makefile
+++ b/drivers/hwtracing/coresight/Makefile
@@ -48,6 +48,7 @@ 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
+obj-$(CONFIG_CORESIGHT_TMC_QDCP) += coresight-tmc-qdcp.o
obj-$(CONFIG_CORESIGHT_CTI) += coresight-cti.o
obj-$(CONFIG_CORESIGHT_TRBE) += coresight-trbe.o
obj-$(CONFIG_CORESIGHT_TPDM) += coresight-tpdm.o
diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
index 2361bb3d652f..27be21f6bc95 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-core.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
@@ -564,9 +564,10 @@ static struct attribute *coresight_tmc_attrs[] = {
NULL,
};

-static const struct attribute_group coresight_tmc_group = {
+const struct attribute_group coresight_tmc_group = {
.attrs = coresight_tmc_attrs,
};
+EXPORT_SYMBOL_GPL(coresight_tmc_group);

/* trigger_cntr is programmed directly into the TMC_TRG register */
static struct attribute *coresight_tmc_trg_attrs[] = {
@@ -737,8 +738,8 @@ static u32 tmc_etr_get_max_burst_size(struct device *dev)
* support and DMA mask. The unadvertised capabilities must already be
* set with tmc_etr_init_caps().
*/
-static int tmc_etr_probe_common(struct device *dev,
- struct tmc_drvdata *drvdata, u32 devid)
+int tmc_etr_probe_common(struct device *dev, struct tmc_drvdata *drvdata,
+ u32 devid)
{
int rc;
u32 dma_mask = 0;
@@ -780,6 +781,7 @@ static int tmc_etr_probe_common(struct device *dev,
mutex_init(&drvdata->idr_mutex);
return 0;
}
+EXPORT_SYMBOL_GPL(tmc_etr_probe_common);

static void register_crash_dev_interface(struct tmc_drvdata *drvdata,
const char *name)
diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c
index 3f1b4f5b011c..a247edeafc0a 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-etr.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c
@@ -1070,6 +1070,18 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
tmc_etr_sync_buf_from_state(drvdata, &state);
}

+/* FFCR value to program when enabling the ETR */
+u32 tmc_etr_get_ffcr(struct tmc_drvdata *drvdata)
+{
+ u32 ffcr = TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI | TMC_FFCR_FON_FLIN |
+ TMC_FFCR_FON_TRIG_EVT | TMC_FFCR_TRIGON_TRIGIN;
+
+ if (drvdata->stop_on_flush)
+ ffcr |= TMC_FFCR_STOP_ON_FLUSH;
+ return ffcr;
+}
+EXPORT_SYMBOL_GPL(tmc_etr_get_ffcr);
+
static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
{
u32 axictl, sts, ffcr;
@@ -1118,10 +1130,7 @@ static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
writel_relaxed(sts, drvdata->base + TMC_STS);
}

- ffcr = TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI | TMC_FFCR_FON_FLIN |
- TMC_FFCR_FON_TRIG_EVT | TMC_FFCR_TRIGON_TRIGIN;
- if (drvdata->stop_on_flush)
- ffcr |= TMC_FFCR_STOP_ON_FLUSH;
+ ffcr = tmc_etr_get_ffcr(drvdata);
writel_relaxed(ffcr, drvdata->base + TMC_FFCR);

writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
@@ -1188,6 +1197,7 @@ ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
offset -= etr_buf->size;
return tmc_etr_buf_get_data(etr_buf, offset, actual, bufpp);
}
+EXPORT_SYMBOL_GPL(tmc_etr_get_sysfs_trace);

static struct etr_buf *
tmc_etr_setup_sysfs_buf(struct tmc_drvdata *drvdata)
@@ -1196,16 +1206,15 @@ tmc_etr_setup_sysfs_buf(struct tmc_drvdata *drvdata)
0, cpu_to_node(0), NULL);
}

-static void
-tmc_etr_free_sysfs_buf(struct etr_buf *buf)
+void tmc_etr_free_sysfs_buf(struct etr_buf *buf)
{
if (buf)
tmc_free_etr_buf(buf);
}
+EXPORT_SYMBOL_GPL(tmc_etr_free_sysfs_buf);

-static void
-tmc_etr_sync_sysfs_buf_from_state(struct tmc_drvdata *drvdata,
- const struct tmc_etr_hw_state *state)
+void tmc_etr_sync_sysfs_buf_from_state(struct tmc_drvdata *drvdata,
+ const struct tmc_etr_hw_state *state)
{
struct etr_buf *etr_buf = drvdata->etr_buf;

@@ -1223,6 +1232,7 @@ tmc_etr_sync_sysfs_buf_from_state(struct tmc_drvdata *drvdata,
etr_buf->offset);
}
}
+EXPORT_SYMBOL_GPL(tmc_etr_sync_sysfs_buf_from_state);

static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
{
@@ -1258,7 +1268,7 @@ void tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
drvdata->etr_buf = NULL;
}

-static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
+struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
{
int ret = 0;
unsigned long flags;
@@ -1317,6 +1327,7 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
tmc_etr_free_sysfs_buf(free_buf);
return ret ? ERR_PTR(ret) : drvdata->sysfs_buf;
}
+EXPORT_SYMBOL_GPL(tmc_etr_get_sysfs_buffer);

static int tmc_enable_etr_sink_sysfs(struct coresight_device *csdev)
{
@@ -2110,3 +2121,4 @@ static struct attribute *coresight_etr_attrs[] = {
const struct attribute_group coresight_etr_group = {
.attrs = coresight_etr_attrs,
};
+EXPORT_SYMBOL_GPL(coresight_etr_group);
diff --git a/drivers/hwtracing/coresight/coresight-tmc-qdcp.c b/drivers/hwtracing/coresight/coresight-tmc-qdcp.c
new file mode 100644
index 000000000000..0c73d2834be2
--- /dev/null
+++ b/drivers/hwtracing/coresight/coresight-tmc-qdcp.c
@@ -0,0 +1,734 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ *
+ * Qualcomm QECP owned CoreSight TMC driver. The TMC registers are owned by
+ * the QECP firmware, which configures, enables and disables the TMC over
+ * QDCP. Only the ETR configuration is supported, its trace buffer is
+ * allocated and read through the generic TMC ETR sysfs buffer handling.
+ */
+
+#include <linux/coresight.h>
+#include <linux/fs.h>
+#include <linux/iommu.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/soc/qcom/qdcp.h>
+#include <linux/uaccess.h>
+
+#include "coresight-priv.h"
+#include "coresight-tmc.h"
+
+#define TMC_QDCP_SERVICE_TIMEOUT_MS 10000
+
+/**
+ * struct tmc_qdcp_match_data - SoC specific QECP TMC description
+ * @svc: QDCP service controlling the TMC.
+ */
+struct tmc_qdcp_match_data {
+ struct qdcp_service svc;
+};
+
+/**
+ * struct tmc_qdcp_drvdata - QECP owned TMC data
+ * @tmc: generic TMC data, used by the shared TMC helpers.
+ * @client: QDCP client used to control the TMC.
+ * @tmc_id: firmware identifier of the TMC, the base address of its
+ * registers.
+ * @lock: serializes the firmware requests with the sink state.
+ * @fw_started: ETR only: firmware confirmed the ETR was enabled with
+ * @tmc.etr_buf, so its read and write pointers describe the
+ * trace data.
+ *
+ * For an ETR, @tmc.etr_buf is the buffer the firmware may be writing into.
+ * It is only cleared once the firmware confirmed the ETR is stopped, so a
+ * non NULL @tmc.etr_buf with the sink disabled means a stop is pending and
+ * the buffer must not be freed or replaced.
+ */
+struct tmc_qdcp_drvdata {
+ struct tmc_drvdata tmc;
+ struct qdcp_client *client;
+ u64 tmc_id;
+ /* Serializes the firmware requests with the sink state */
+ struct mutex lock;
+ bool fw_started;
+};
+
+static struct tmc_qdcp_drvdata *to_tmc_qdcp(struct tmc_drvdata *drvdata)
+{
+ return container_of(drvdata, struct tmc_qdcp_drvdata, tmc);
+}
+
+/*
+ * The TMC registers are only accessible from the QECP, nothing may access
+ * them through csdev->access.
+ */
+static u64 tmc_qdcp_access_read(u32 offset, bool relaxed, bool _64bit)
+{
+ WARN_ONCE(1, "QDCP TMC register 0x%x is not accessible\n", offset);
+ return 0;
+}
+
+static void tmc_qdcp_access_write(u64 val, u32 offset, bool relaxed,
+ bool _64bit)
+{
+ WARN_ONCE(1, "QDCP TMC register 0x%x is not accessible\n", offset);
+}
+
+/* ETR configuration */
+
+static const struct attribute_group *tmc_qdcp_etr_groups[] = {
+ &coresight_etr_group,
+ &coresight_tmc_group,
+ NULL,
+};
+
+static void tmc_qdcp_etr_invalidate_buf(struct etr_buf *etr_buf)
+{
+ etr_buf->offset = 0;
+ etr_buf->len = 0;
+ etr_buf->full = false;
+}
+
+static int tmc_qdcp_etr_read_hw_state(struct tmc_qdcp_drvdata *qdata,
+ struct tmc_etr_hw_state *state)
+{
+ u64 sts;
+ int ret;
+
+ ret = qdcp_tmc_get_param(qdata->client, qdata->tmc_id,
+ QDCP_TMC_PARAM_RRP, &state->rrp);
+ if (ret)
+ return ret;
+
+ ret = qdcp_tmc_get_param(qdata->client, qdata->tmc_id,
+ QDCP_TMC_PARAM_RWP, &state->rwp);
+ if (ret)
+ return ret;
+
+ ret = qdcp_tmc_get_param(qdata->client, qdata->tmc_id,
+ QDCP_TMC_PARAM_STS, &sts);
+ if (ret)
+ return ret;
+
+ state->sts = sts;
+ return 0;
+}
+
+/*
+ * The pointers come from the firmware, make sure they are inside a flat
+ * buffer before using them as offsets. The scatter-gather sync already
+ * rejects pointers outside of its table.
+ */
+static bool tmc_qdcp_etr_hw_state_valid(struct etr_buf *etr_buf,
+ const struct tmc_etr_hw_state *state)
+{
+ u64 start = etr_buf->hwaddr, end = etr_buf->hwaddr + etr_buf->size;
+
+ if (etr_buf->mode != ETR_MODE_FLAT)
+ return true;
+
+ return state->rrp >= start && state->rrp < end &&
+ state->rwp >= start && state->rwp < end;
+}
+
+static void tmc_qdcp_etr_get_buffer_addr(struct tmc_drvdata *drvdata,
+ struct etr_buf *etr_buf,
+ phys_addr_t *paddr, dma_addr_t *iaddr)
+{
+ struct device *dev = drvdata->csdev->dev.parent;
+ struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
+
+ /*
+ * etr_buf->hwaddr is the address used by the ETR, an IOVA when an
+ * IOMMU translates its accesses. The firmware also needs the
+ * physical address of the buffer.
+ */
+ *iaddr = etr_buf->hwaddr;
+ if (domain)
+ *paddr = iommu_iova_to_phys(domain, etr_buf->hwaddr);
+ else
+ *paddr = etr_buf->hwaddr;
+}
+
+static int tmc_qdcp_etr_enable_hw(struct tmc_qdcp_drvdata *qdata,
+ struct etr_buf *etr_buf)
+{
+ struct tmc_drvdata *drvdata = &qdata->tmc;
+ struct qdcp_tmc_cfg cfg = {
+ .size = etr_buf->size,
+ .sg_mode = etr_buf->mode == ETR_MODE_ETR_SG,
+ .ffcr = tmc_etr_get_ffcr(drvdata),
+ };
+ u64 ffcr, sts;
+ int ret;
+
+ if (WARN_ON(drvdata->etr_buf))
+ return -EBUSY;
+
+ /*
+ * Don't enable on top of a flush still in progress from the previous
+ * session: the read and write pointers could be inconsistent and, in
+ * scatter-gather mode, the ETR could access memory outside of the
+ * buffer.
+ */
+ ret = qdcp_tmc_get_param(qdata->client, qdata->tmc_id,
+ QDCP_TMC_PARAM_FFCR, &ffcr);
+ if (ret)
+ return ret;
+ if (ffcr & BIT(TMC_FFCR_FLUSHMAN_BIT)) {
+ dev_err(&drvdata->csdev->dev,
+ "QDCP ETR flush still pending, FFCR %#llx\n", ffcr);
+ return -EBUSY;
+ }
+
+ ret = qdcp_tmc_get_param(qdata->client, qdata->tmc_id,
+ QDCP_TMC_PARAM_STS, &sts);
+ if (ret)
+ return ret;
+ if (sts & TMC_STS_MEMERR) {
+ dev_err(&drvdata->csdev->dev, "QDCP ETR memory error detected\n");
+ return -EINVAL;
+ }
+
+ tmc_qdcp_etr_get_buffer_addr(drvdata, etr_buf, &cfg.paddr, &cfg.iaddr);
+
+ drvdata->etr_buf = etr_buf;
+ ret = qdcp_tmc_etr_configure(qdata->client, qdata->tmc_id, &cfg);
+ if (!ret)
+ ret = qdcp_sink_enable(qdata->client, QDCP_SINK_ETR,
+ qdata->tmc_id);
+ if (!ret) {
+ qdata->fw_started = true;
+ return 0;
+ }
+
+ /*
+ * A failed or timed out request may still have been executed, so the
+ * firmware may be writing into the buffer. Only release it once the
+ * stop is confirmed, otherwise keep it pinned for a later retry.
+ */
+ if (!qdcp_sink_disable(qdata->client, QDCP_SINK_ETR, qdata->tmc_id)) {
+ tmc_qdcp_etr_invalidate_buf(etr_buf);
+ drvdata->etr_buf = NULL;
+ }
+ return ret;
+}
+
+/* Sync the trace buffer with the pointers of the flushed and stopped ETR */
+static int tmc_qdcp_etr_sync_buf(struct tmc_qdcp_drvdata *qdata)
+{
+ struct tmc_drvdata *drvdata = &qdata->tmc;
+ struct device *dev = &drvdata->csdev->dev;
+ struct tmc_etr_hw_state state;
+ int ret;
+
+ ret = tmc_qdcp_etr_read_hw_state(qdata, &state);
+ if (ret) {
+ dev_err(dev, "failed to read QDCP ETR state: %d\n", ret);
+ return ret;
+ }
+
+ if (!tmc_qdcp_etr_hw_state_valid(drvdata->etr_buf, &state)) {
+ dev_err(dev, "invalid QDCP ETR pointers RRP %#llx RWP %#llx\n",
+ state.rrp, state.rwp);
+ return -EINVAL;
+ }
+
+ tmc_etr_sync_sysfs_buf_from_state(drvdata, &state);
+ return 0;
+}
+
+static int tmc_qdcp_etr_disable_hw(struct tmc_qdcp_drvdata *qdata)
+{
+ struct tmc_drvdata *drvdata = &qdata->tmc;
+ struct etr_buf *etr_buf = drvdata->etr_buf;
+ struct device *dev = &drvdata->csdev->dev;
+ int ret;
+
+ if (!etr_buf)
+ return 0;
+
+ /*
+ * Flush and stop the ETR first, so that its read and write pointers
+ * describe the trace data, then disable it. The buffer is reported
+ * empty if the ETR never ran with it or its state can't be trusted.
+ */
+ ret = qdcp_tmc_begin_read(qdata->client, qdata->tmc_id);
+ if (ret)
+ dev_err(dev, "failed to flush QDCP ETR: %d\n", ret);
+ else if (qdata->fw_started)
+ ret = tmc_qdcp_etr_sync_buf(qdata);
+ if (ret || !qdata->fw_started)
+ tmc_qdcp_etr_invalidate_buf(etr_buf);
+
+ ret = qdcp_sink_disable(qdata->client, QDCP_SINK_ETR, qdata->tmc_id);
+ if (ret) {
+ dev_err(dev, "failed to stop QDCP ETR: %d\n", ret);
+ return ret;
+ }
+
+ drvdata->etr_buf = NULL;
+ qdata->fw_started = false;
+ return 0;
+}
+
+static int tmc_qdcp_etr_enable_sink_sysfs(struct coresight_device *csdev)
+{
+ struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct tmc_qdcp_drvdata *qdata = to_tmc_qdcp(drvdata);
+ struct etr_buf *sysfs_buf;
+ int ret = 0;
+
+ mutex_lock(&qdata->lock);
+
+ /* Retry a pending stop before the buffer can be replaced */
+ if (coresight_get_mode(csdev) == CS_MODE_DISABLED) {
+ ret = tmc_qdcp_etr_disable_hw(qdata);
+ if (ret)
+ goto out;
+ }
+
+ sysfs_buf = tmc_etr_get_sysfs_buffer(csdev);
+ if (IS_ERR(sysfs_buf)) {
+ ret = PTR_ERR(sysfs_buf);
+ goto out;
+ }
+
+ if (coresight_get_mode(csdev) == CS_MODE_SYSFS) {
+ csdev->refcnt++;
+ goto out;
+ }
+
+ ret = tmc_qdcp_etr_enable_hw(qdata, sysfs_buf);
+ if (!ret) {
+ coresight_set_mode(csdev, CS_MODE_SYSFS);
+ csdev->refcnt++;
+ }
+
+out:
+ mutex_unlock(&qdata->lock);
+ if (!ret)
+ dev_dbg(&csdev->dev, "TMC-ETR enabled\n");
+ return ret;
+}
+
+static int tmc_qdcp_etr_enable_sink(struct coresight_device *csdev,
+ enum cs_mode mode,
+ struct coresight_path *path)
+{
+ if (mode != CS_MODE_SYSFS)
+ return -EOPNOTSUPP;
+
+ return tmc_qdcp_etr_enable_sink_sysfs(csdev);
+}
+
+static int tmc_qdcp_etr_disable_sink(struct coresight_device *csdev)
+{
+ struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
+ struct tmc_qdcp_drvdata *qdata = to_tmc_qdcp(drvdata);
+ int ret;
+
+ mutex_lock(&qdata->lock);
+ if (drvdata->reading) {
+ mutex_unlock(&qdata->lock);
+ return -EBUSY;
+ }
+
+ csdev->refcnt--;
+ if (csdev->refcnt) {
+ mutex_unlock(&qdata->lock);
+ return -EBUSY;
+ }
+
+ /*
+ * The path is torn down whatever is returned here. If the stop is not
+ * confirmed, the buffer stays pinned and the stop is retried on the
+ * next enable, read or removal.
+ */
+ ret = tmc_qdcp_etr_disable_hw(qdata);
+ drvdata->pid = -1;
+ coresight_set_mode(csdev, CS_MODE_DISABLED);
+ mutex_unlock(&qdata->lock);
+
+ if (!ret)
+ dev_dbg(&csdev->dev, "TMC-ETR disabled\n");
+ return ret;
+}
+
+static const struct coresight_ops_sink tmc_qdcp_etr_sink_ops = {
+ .enable = tmc_qdcp_etr_enable_sink,
+ .disable = tmc_qdcp_etr_disable_sink,
+};
+
+static const struct coresight_ops tmc_qdcp_etr_cs_ops = {
+ .sink_ops = &tmc_qdcp_etr_sink_ops,
+};
+
+static int tmc_qdcp_etr_read_prepare(struct tmc_qdcp_drvdata *qdata)
+{
+ struct tmc_drvdata *drvdata = &qdata->tmc;
+ int ret;
+
+ mutex_lock(&qdata->lock);
+ if (drvdata->reading) {
+ ret = -EBUSY;
+ goto out;
+ }
+
+ /* If drvdata->sysfs_buf is NULL the trace data has been read already */
+ if (!drvdata->sysfs_buf) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ /* Stop a running session, or retry a pending stop */
+ ret = tmc_qdcp_etr_disable_hw(qdata);
+ if (ret)
+ goto out;
+
+ drvdata->reading = true;
+out:
+ mutex_unlock(&qdata->lock);
+ return ret;
+}
+
+static int tmc_qdcp_etr_read_unprepare(struct tmc_qdcp_drvdata *qdata)
+{
+ struct tmc_drvdata *drvdata = &qdata->tmc;
+ struct etr_buf *sysfs_buf = NULL;
+ int ret = 0;
+
+ mutex_lock(&qdata->lock);
+ if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS) {
+ /*
+ * Resume the session with the same buffer. If that fails the
+ * sink stays enabled, its final disable drops the reference.
+ */
+ ret = qdcp_tmc_end_read(qdata->client, qdata->tmc_id);
+ if (!ret)
+ ret = tmc_qdcp_etr_enable_hw(qdata, drvdata->sysfs_buf);
+ if (ret)
+ dev_err(&drvdata->csdev->dev,
+ "failed to resume QDCP ETR: %d\n", ret);
+ } else {
+ /* The buffer was read and is no longer used, free it */
+ sysfs_buf = drvdata->sysfs_buf;
+ drvdata->sysfs_buf = NULL;
+ }
+ drvdata->reading = false;
+ mutex_unlock(&qdata->lock);
+
+ tmc_etr_free_sysfs_buf(sysfs_buf);
+ return ret;
+}
+
+/*
+ * An enabled trace path and an open device both hold a reference on this
+ * module, and unbinding through sysfs is not allowed, so the sink is
+ * disabled when the driver is removed. Only a pending stop may be left.
+ */
+static void tmc_qdcp_etr_remove(struct tmc_qdcp_drvdata *qdata)
+{
+ struct tmc_drvdata *drvdata = &qdata->tmc;
+ struct etr_buf *sysfs_buf = NULL;
+
+ mutex_lock(&qdata->lock);
+ if (tmc_qdcp_etr_disable_hw(qdata)) {
+ dev_warn(&drvdata->csdev->dev,
+ "QDCP ETR not confirmed stopped, leaking trace buffer\n");
+ } else {
+ sysfs_buf = drvdata->sysfs_buf;
+ drvdata->sysfs_buf = NULL;
+ }
+ mutex_unlock(&qdata->lock);
+
+ tmc_etr_free_sysfs_buf(sysfs_buf);
+}
+
+/* Make sure the firmware stops writing into the trace buffer */
+static void tmc_qdcp_etr_shutdown(struct tmc_qdcp_drvdata *qdata)
+{
+ mutex_lock(&qdata->lock);
+ if (tmc_qdcp_etr_disable_hw(qdata))
+ dev_warn(&qdata->tmc.csdev->dev,
+ "QDCP ETR not confirmed stopped\n");
+ mutex_unlock(&qdata->lock);
+}
+
+static int tmc_qdcp_etr_setup(struct device *dev,
+ struct tmc_qdcp_drvdata *qdata, u32 devid,
+ struct coresight_desc *desc)
+{
+ struct tmc_drvdata *drvdata = &qdata->tmc;
+ u64 auth;
+ int ret;
+
+ ret = qdcp_tmc_get_param(qdata->client, qdata->tmc_id,
+ QDCP_TMC_PARAM_AUTHSTATUS, &auth);
+ if (ret)
+ return ret;
+ if ((auth & TMC_AUTH_NSID_MASK) != 0x3)
+ return -EACCES;
+
+ drvdata->etr_mode = ETR_MODE_AUTO;
+ tmc_etr_init_caps(drvdata, 0);
+
+ desc->type = CORESIGHT_DEV_TYPE_SINK;
+ desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_SYSMEM;
+ desc->ops = &tmc_qdcp_etr_cs_ops;
+ desc->groups = tmc_qdcp_etr_groups;
+
+ return tmc_etr_probe_common(dev, drvdata, devid);
+}
+
+static int tmc_qdcp_read_prepare(struct tmc_qdcp_drvdata *qdata)
+{
+ switch (qdata->tmc.config_type) {
+ case TMC_CONFIG_TYPE_ETR:
+ return tmc_qdcp_etr_read_prepare(qdata);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int tmc_qdcp_read_unprepare(struct tmc_qdcp_drvdata *qdata)
+{
+ switch (qdata->tmc.config_type) {
+ case TMC_CONFIG_TYPE_ETR:
+ return tmc_qdcp_etr_read_unprepare(qdata);
+ default:
+ return -EINVAL;
+ }
+}
+
+static ssize_t tmc_qdcp_get_sysfs_trace(struct tmc_drvdata *drvdata,
+ loff_t pos, size_t len, char **bufpp)
+{
+ switch (drvdata->config_type) {
+ case TMC_CONFIG_TYPE_ETR:
+ return tmc_etr_get_sysfs_trace(drvdata, pos, len, bufpp);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int tmc_qdcp_open(struct inode *inode, struct file *file)
+{
+ struct tmc_drvdata *drvdata = container_of(file->private_data,
+ struct tmc_drvdata, miscdev);
+ int ret;
+
+ ret = tmc_qdcp_read_prepare(to_tmc_qdcp(drvdata));
+ if (ret)
+ return ret;
+
+ nonseekable_open(inode, file);
+ return 0;
+}
+
+static ssize_t tmc_qdcp_read(struct file *file, char __user *data,
+ size_t len, loff_t *ppos)
+{
+ struct tmc_drvdata *drvdata = container_of(file->private_data,
+ struct tmc_drvdata, miscdev);
+ ssize_t actual;
+ char *bufp;
+
+ actual = tmc_qdcp_get_sysfs_trace(drvdata, *ppos, len, &bufp);
+ if (actual <= 0)
+ return 0;
+
+ if (copy_to_user(data, bufp, actual))
+ return -EFAULT;
+
+ *ppos += actual;
+ return actual;
+}
+
+static int tmc_qdcp_release(struct inode *inode, struct file *file)
+{
+ struct tmc_drvdata *drvdata = container_of(file->private_data,
+ struct tmc_drvdata, miscdev);
+
+ return tmc_qdcp_read_unprepare(to_tmc_qdcp(drvdata));
+}
+
+static const struct file_operations tmc_qdcp_fops = {
+ .owner = THIS_MODULE,
+ .open = tmc_qdcp_open,
+ .read = tmc_qdcp_read,
+ .release = tmc_qdcp_release,
+};
+
+static int tmc_qdcp_probe(struct platform_device *pdev)
+{
+ const struct tmc_qdcp_match_data *data;
+ struct coresight_platform_data *pdata;
+ struct coresight_desc desc = { 0 };
+ struct device *dev = &pdev->dev;
+ struct tmc_qdcp_drvdata *qdata;
+ struct tmc_drvdata *drvdata;
+ const char *dev_list;
+ struct resource *res;
+ u64 devid;
+ int ret;
+
+ data = device_get_match_data(dev);
+ if (!data)
+ return -EINVAL;
+
+ /*
+ * The firmware identifies the TMC by the base address of its
+ * registers. They are only accessible from the QECP, so the region is
+ * not mapped.
+ */
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res)
+ return dev_err_probe(dev, -EINVAL, "missing TMC register region\n");
+
+ qdata = devm_kzalloc(dev, sizeof(*qdata), GFP_KERNEL);
+ if (!qdata)
+ return -ENOMEM;
+
+ drvdata = &qdata->tmc;
+ dev_set_drvdata(dev, drvdata);
+
+ /*
+ * Get the client first, so that it is released last and can still be
+ * used from tmc_qdcp_remove().
+ */
+ qdata->client = devm_qdcp_client_get(dev, &data->svc);
+ if (IS_ERR(qdata->client))
+ return dev_err_probe(dev, PTR_ERR(qdata->client),
+ "failed to create QDCP client\n");
+ qdata->tmc_id = res->start;
+
+ ret = devm_mutex_init(dev, &qdata->lock);
+ if (ret)
+ return ret;
+
+ raw_spin_lock_init(&drvdata->spinlock);
+
+ /* DEVID describes the TMC configuration, it is needed to probe */
+ ret = qdcp_client_wait_for_service(qdata->client,
+ TMC_QDCP_SERVICE_TIMEOUT_MS);
+ if (ret)
+ return dev_err_probe(dev, -EPROBE_DEFER,
+ "QDCP service not available\n");
+
+ ret = qdcp_tmc_get_param(qdata->client, qdata->tmc_id,
+ QDCP_TMC_PARAM_DEVID, &devid);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to read QDCP TMC DEVID\n");
+
+ drvdata->config_type = BMVAL(devid, 6, 7);
+ /* This device is not associated with a session */
+ drvdata->pid = -1;
+
+ switch (drvdata->config_type) {
+ case TMC_CONFIG_TYPE_ETR:
+ ret = tmc_qdcp_etr_setup(dev, qdata, devid, &desc);
+ dev_list = "tmc_etr";
+ break;
+ default:
+ return dev_err_probe(dev, -ENODEV,
+ "unsupported QDCP TMC configuration %u\n",
+ drvdata->config_type);
+ }
+ if (ret)
+ return ret;
+
+ desc.name = coresight_alloc_device_name(dev_list, dev);
+ if (!desc.name)
+ return -ENOMEM;
+
+ pdata = coresight_get_platform_data(dev);
+ if (IS_ERR(pdata))
+ return PTR_ERR(pdata);
+ dev->platform_data = pdata;
+
+ desc.pdata = pdata;
+ desc.dev = dev;
+ desc.access.read = tmc_qdcp_access_read;
+ desc.access.write = tmc_qdcp_access_write;
+ drvdata->csdev = coresight_register(&desc);
+ if (IS_ERR(drvdata->csdev))
+ return PTR_ERR(drvdata->csdev);
+
+ drvdata->miscdev.name = desc.name;
+ drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
+ drvdata->miscdev.fops = &tmc_qdcp_fops;
+ ret = misc_register(&drvdata->miscdev);
+ if (ret) {
+ coresight_unregister(drvdata->csdev);
+ return ret;
+ }
+
+ return 0;
+}
+
+static void tmc_qdcp_remove(struct platform_device *pdev)
+{
+ struct tmc_drvdata *drvdata = platform_get_drvdata(pdev);
+ struct tmc_qdcp_drvdata *qdata = to_tmc_qdcp(drvdata);
+
+ misc_deregister(&drvdata->miscdev);
+
+ switch (drvdata->config_type) {
+ case TMC_CONFIG_TYPE_ETR:
+ tmc_qdcp_etr_remove(qdata);
+ break;
+ default:
+ break;
+ }
+
+ coresight_unregister(drvdata->csdev);
+}
+
+static void tmc_qdcp_shutdown(struct platform_device *pdev)
+{
+ struct tmc_drvdata *drvdata = platform_get_drvdata(pdev);
+
+ switch (drvdata->config_type) {
+ case TMC_CONFIG_TYPE_ETR:
+ tmc_qdcp_etr_shutdown(to_tmc_qdcp(drvdata));
+ break;
+ default:
+ break;
+ }
+}
+
+static const struct tmc_qdcp_match_data hawi_tmc_qdcp_data = {
+ .svc = {
+ .service_id = 0x453,
+ .version = 1,
+ .instance_id = 32,
+ },
+};
+
+static const struct of_device_id tmc_qdcp_match[] = {
+ { .compatible = "qcom,hawi-qecp-tmc", .data = &hawi_tmc_qdcp_data },
+ {}
+};
+MODULE_DEVICE_TABLE(of, tmc_qdcp_match);
+
+static struct platform_driver tmc_qdcp_driver = {
+ .probe = tmc_qdcp_probe,
+ .remove = tmc_qdcp_remove,
+ .shutdown = tmc_qdcp_shutdown,
+ .driver = {
+ .name = "coresight-tmc-qdcp",
+ .of_match_table = tmc_qdcp_match,
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
+ .suppress_bind_attrs = true,
+ },
+};
+module_platform_driver(tmc_qdcp_driver);
+
+MODULE_DESCRIPTION("Qualcomm QECP owned CoreSight TMC driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/hwtracing/coresight/coresight-tmc.h b/drivers/hwtracing/coresight/coresight-tmc.h
index c3eec5e75fd0..065702454ee1 100644
--- a/drivers/hwtracing/coresight/coresight-tmc.h
+++ b/drivers/hwtracing/coresight/coresight-tmc.h
@@ -353,6 +353,14 @@ void tmc_etr_disable_hw(struct tmc_drvdata *drvdata);
extern const struct coresight_ops tmc_etr_cs_ops;
ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
loff_t pos, size_t len, char **bufpp);
+struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev);
+void tmc_etr_free_sysfs_buf(struct etr_buf *buf);
+void tmc_etr_sync_sysfs_buf_from_state(struct tmc_drvdata *drvdata,
+ const struct tmc_etr_hw_state *state);
+u32 tmc_etr_get_ffcr(struct tmc_drvdata *drvdata);
+int tmc_etr_probe_common(struct device *dev, struct tmc_drvdata *drvdata,
+ u32 devid);
+extern const struct attribute_group coresight_tmc_group;


#define TMC_REG_PAIR(name, lo_off, hi_off) \

--
2.34.1