Devicetree
 help / color / mirror / Atom feed
* [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks
@ 2026-11-30  7:11 Jie Gan
  2026-11-30  7:11 ` [PATCH v2 1/8] soc: qcom: add QDCP client library Jie Gan
                   ` (7 more replies)
  0 siblings, 8 replies; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

On Qualcomm Hawi/Maili SoCs, part of the CoreSight trace hardware is
owned by the Qualcomm Embedded Co-Processor (QECP), a separate on-die
processor running its own firmware: an STM (System Trace Macrocell)
source, a TMC (Trace Memory Controller) configured as an ETR (Embedded
Trace Router) sink, and the internal funnels/replicators that connect
them. Their control registers are protected by hardware access control
and cannot be accessed from the AP (application processor, the cores
running Linux).

Linux asks the QECP firmware to configure, enable and disable these
blocks over the Qualcomm Debug Control Protocol (QDCP), a QMI service
carried over QRTR like other Qualcomm firmware services. What stays with
Linux is what it can access directly: the STM stimulus ports, written
for trace data, and the ETR trace buffer, allocated in system memory and
read through the TMC device node as for any other ETR.

This series adds a QDCP client library, a driver for the QECP-owned STM
and a driver for the QECP-owned TMC, so that a QECP-owned STM -> ETR
session works through the existing CoreSight sysfs interface.

Design
------

Control path (QDCP, QMI over QRTR) and data path (hardware):

  AP (Linux)                                 QECP
  ----------                                 ----

  sysfs / char dev
        |
  +-----v------------+  typed   +-----------+  QMI/QRTR  +-------------+
  | QDCP STM source  |--------->| QDCP      |            |             |
  | QDCP TMC (ETR)   |  API     | client    |<---------->| QECP        |
  +------------------+          | library   |            | firmware    |
                                | (soc/qcom)|            |             |
                                +-----------+            +------+------+
                                                                |
                                       programs STM/TMC ctl regs,
                                       ATID filter, internal routing
                                                                |
  Data path                                                     v
  ---------
  STM stimulus   +-----+   (QECP-private funnels/    +---------------+
  aperture ----->| STM |----- replicators, no DT ---->| TMC (ETR)     |
  (AP writes)    +-----+      node, not visible)      +-------+-------+
                                                              | DMA
                                                      +-------v-------+
                                                      | etr_buf       |
                                                      | (Linux memory)|
                                                      +-------+-------+
                                                              |
                                       AP reads via /dev/<tmc> (sysfs)

 - QDCP is a client library under drivers/soc/qcom/, next to the other
   Qualcomm QMI helpers, selected by the drivers that use it. Each device
   creates its own QMI client with devm_qdcp_client_get(); the client is
   released after the driver's remove callback, so a driver can still
   stop its block from there. The library is not a CoreSight device and
   has no devicetree node.

 - The library API follows the protocol. Requests shared by every source
   or sink take its type: qdcp_source_set_atid(),
   qdcp_source_{enable,disable}() and qdcp_sink_{enable,disable}().
   Configuration specific to a block type has typed requests:
   qdcp_stm_set_port_mask(), qdcp_stm_get_param(),
   qdcp_tmc_etr_configure(), qdcp_tmc_get_param() and
   qdcp_tmc_{begin,end}_read().

 - The QECP-owned STM has its own driver, coresight-stm.c is untouched.
   It reads STMDEVID and STMSPFEAT2R through the firmware for its number
   of stimulus ports and fundamental data size, and writes trace data to
   the stimulus ports through the generic STM framework.

 - The QECP-owned TMC driver is a separate module, coresight-tmc-qdcp,
   built on a small set of TMC helpers exported for it, the same way
   CATU builds on the TMC driver. The generic TMC code only gains three
   preparation patches with no functional change. The driver reads DEVID
   through the firmware and selects the configuration specific handling
   from its configuration type, as the TMC driver does; only the ETR
   configuration is implemented, other configurations fail to probe.

 - QECP-internal topology is not modelled in DT: there is no node for
   the funnels/replicators between the STM and the ETR, since CoreSight
   only needs a logical source-to-sink path to build a session.

Devicetree and firmware identifiers
-----------------------------------

 - Each device has a SoC specific compatible, qcom,hawi-qecp-stm and
   qcom,hawi-qecp-tmc, Maili using the Hawi compatibles as fallback.
   One TMC compatible covers every TMC configuration, which is read from
   DEVID, as for arm,coresight-tmc.

 - reg describes the real register regions: the STM control registers
   ("stm-base") and stimulus ports ("stm-stimulus-base"), and the TMC
   registers. The firmware identifies each block by the base address of
   its registers, so the STM source ID and the TMC ID are taken from reg
   and no identifier property is needed. The access-controlled regions
   are never mapped; only the stimulus ports are.

 - The QMI service, version and instance used to reach the firmware are
   fixed for a SoC and the same for every QECP-owned block, so they are
   part of the driver match data (service 0x453, version 1, instance 32
   on Hawi). They are only used to look up the server.

 - Because firmware controls the blocks' clocks and power, the bindings
   describe no clocks, clock-names or power-domains, and the drivers do
   no clock handling or runtime PM. The TMC binding keeps the usual ETR
   properties (iommus, arm,buffer-size, arm,scatter-gather).

Register ownership
------------------

                            AP (Linux)          QECP firmware
                            ----------          -------------
  STM stimulus aperture     owned, mapped,
                            written directly
  STM control registers     read over QDCP      owned, access-
  (DEVID, SPFEAT2R)                             controlled from AP
  Funnel/replicator                             owned, private, not
  routing                                       visible to Linux
  TMC-ETR control registers read over QDCP      owned, access-
  (DEVID, AUTHSTATUS, FFCR)                     controlled from AP
  TMC-ETR trace buffer      owned, allocated,
                            read via sysfs
  TMC-ETR RRP/RWP/STS       read over QDCP      owned, returned over
                                                QDCP on request
  Block clocks and power                        owned, controlled by
                                                firmware only

QDCP is the only channel to the firmware-owned state; there is no direct
path and no fallback.

Sequences
---------

Enable, a trace session start (CoreSight enables sink first, then source):

  CoreSight   QDCP TMC     QDCP STM      QDCP          QECP
  core        (patch 8)    (patch 6)     library       firmware
     |            |            |            |             |
     |--enable--->|            |            |             |
     |  sink      | alloc/reuse etr_buf,    |             |
     |            | compute paddr + IOVA    |             |
     |            |--qdcp_tmc_get_param---->|--FFCR, STS->|
     |            |  (no pending flush, no  |<---values---|
     |            |   memory error)         |             |
     |            |--qdcp_tmc_etr_configure>|--SG mode--->|
     |            |            |            |--buffer---->|
     |            |            |            |--FFCR------>|
     |            |--qdcp_sink_enable------>|--start TMC->|
     |            |            |            |<---ok-------|
     |<--ok-------|            |            |             |
     |--enable------------>    |            |             |
     |  source                 |            |             |
     |            |            |--qdcp_source_set_atid()  |
     |            |            |  qdcp_stm_set_port_mask()|
     |            |            |  qdcp_source_enable()    |
     |            |            |            |--set ATID-->|
     |            |            |            |--ports on-->|
     |            |            |            |--start STM->|
     |            |            |            |<---ok-------|
     |<--ok------------------- |            |             |
     |            |            |            |             |
     |   trace flows STM -> (QECP-private routing) -> ETR -> etr_buf

  (SG: scatter-gather, FFCR: formatter and flush control register,
  IOVA: I/O virtual address, ATID: ATB trace ID.)

Disable and readback (source first, then sink):

  CoreSight   QDCP TMC     QDCP STM      QDCP          QECP
  core        (patch 8)    (patch 6)     library       firmware
     |            |            |            |             |
     |--disable--------------> |            |             |
     |  source                 |            |             |
     |            |            |--qdcp_stm_set_port_mask()|
     |            |            |  qdcp_source_disable()   |
     |            |            |  qdcp_source_set_atid(0) |
     |            |            |            |--ports off->|
     |            |            |            |--stop STM-->|
     |            |            |            |--clear ATID>|
     |--disable-->|            |            |             |
     |  sink      |--qdcp_tmc_begin_read--->|--flush and->|
     |            |            |            |  stop TMC   |
     |            |--qdcp_tmc_get_param(RRP/RWP/STS)----->|
     |            |<--values------------------------------|
     |            | validate, sync etr_buf  |             |
     |            |--qdcp_sink_disable----->|--disable--->|
     |            |            |            |<---ok-------|
     |            | stop confirmed? no -> keep etr_buf pinned and retry
     |            |   on the next enable, open() of /dev/<tmc> or removal
     |            | yes: release etr_buf    |             |
     |            |            |            |             |
  userspace reads trace from etr_buf through /dev/<tmc>, as for any ETR;
  closing it ends the read (qdcp_tmc_end_read) before a running session
  is resumed.

Lifecycle and safety rules
--------------------------

 - Firmware DMA into etr_buf is the main hazard. etr_buf is the buffer
   firmware may be writing into, and Linux clears it only after
   firmware has confirmed the TMC stop. If a stop is not confirmed,
   including a failed or timed-out enable request that firmware may
   still have executed, the buffer stays pinned: it is not freed or
   replaced, and the stop is retried on the next enable, the next open
   of /dev/<tmc>, or device removal. If firmware never confirms, the
   buffer is left allocated at removal rather than freed under possible
   DMA. The ETR is also stopped on shutdown, so that firmware does not
   keep writing into memory after a reboot or kexec.
 - The ETR is not enabled while a flush from the previous session is
   still pending or a memory error is reported, and it is flushed and
   stopped before its pointers are read.
 - The read/write pointers returned by firmware are only used if the
   ETR was enabled with the buffer and they lie inside it; otherwise
   the buffer is reported empty instead of leaving the previous capture
   readable.
 - The STM stimulus ports are only written once firmware has confirmed
   the STM is enabled, since firmware controls its clocks and power.
 - If firmware does not confirm an STM stop, Linux still marks the
   source disabled (the CoreSight source disable op cannot fail) and
   logs a warning. The trace ID is then kept allocated on removal, so
   that no other source reuses an ID firmware may still be applying.
 - Unbinding through sysfs is not supported. An enabled trace path and
   an open device node both hold a reference on the driver module, so
   the devices are always idle when a driver is removed.
 - No lock is held across a QMI transaction wait. A QMI reply that
   omits its result TLV, or the value TLV of a parameter read, is
   treated as a failure.

Sysfs ABI
---------

No new CoreSight-wide sysfs ABI. A QDCP TMC exposes the generic ETR/TMC
attributes for buffer sizing and sysfs mode enable/disable, except
trigger_cntr, as the driver does not program TMC_TRG through the
firmware. The QDCP STM exposes a read-only "traceid" attribute, like
other CoreSight sources. Userspace reads trace data through the same
/dev/<tmc> interface as for every other ETR.

Known limitations and scope
---------------------------

 - If QECP firmware resets while a session is active, Linux is not
   notified and does not reprogram the STM or ETR; trace stops until the
   session is disabled and enabled again.
 - The STM stimulus write path checks the firmware enable state without
   a lock, like coresight-stm.c checks the mode, as the packet callback
   is notrace and can run in atomic context.
 - QDCP-owned ETRs have no panic handler: the control registers are not
   accessible from the panic path.
 - Not included, and deferred until a platform needs it: the TMC ETF/ETB
   configurations (the TMC driver is structured for them), perf mode,
   STM hardware events, other QDCP sources such as TPDM and OCLA, and the
   TMC READ firmware data-copy command (the AP reads the ETR buffer
   directly).
 - The enable/disable requests are not modelled as a power domain: they
   start and stop the blocks' trace function rather than changing a
   power state, carry data (trace ID, port mask, buffer addresses, FFCR)
   and read state back, and follow the CoreSight path lifecycle rather
   than runtime PM.

Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
Changes in v2:
 - Dropped RFC prefix.

 - QDCP provider replaced by a client library (Krzysztof): v1 had a
   "qcom,qdcp" platform device with QMI connection child nodes, and
   consumers referenced a connection through a qcom,qdcp phandle. The
   provider has no resources of its own and QDCP is part of each device,
   so the qcom,qdcp binding, the phandle, the provider list and the
   device links are gone. This also removes the race between
   devm_qdcp_get() and provider unbind, and the use after free on an
   open STM device after a forced unbind through the device link
   (sashiko).

 - No firmware identifiers in DT (Krzysztof): qcom,service-id,
   qcom,instance-id, qcom,version, qcom,qdcp-source-id and
   qcom,qdcp-tmc-id are dropped. The QMI connection values are taken
   from the driver match data; the block IDs are the base addresses of
   the register regions described in reg, which the firmware uses as
   identifiers.

 - SoC specific compatibles (Krzysztof): qcom,coresight-stm-qdcp and
   qcom,coresight-tmc-qdcp are replaced with qcom,hawi-qecp-stm and
   qcom,hawi-qecp-tmc, Maili using them as fallback. The $nodename
   pattern and qcom,num-stimulus-ports are dropped.

 - QMI request lengths fixed (sashiko): v1 passed sizeof() of the
   request structure as the encoded length, which is shorter than the
   encoded message as each TLV adds a 3 byte header, so most requests
   would have failed with -ETOOSMALL.

 - TMC patch split (Suzuki): three preparation patches with no
   functional change, then the QDCP TMC driver as a separate module, so
   that the generic TMC code has no QDCP branches and does not depend on
   the Qualcomm QMI stack. The shared helpers are exported in the patch
   that first uses them.

 - csdev_access (Suzuki): both drivers provide csdev_access methods that
   warn and return 0 instead of leaving desc.access zeroed, so no
   generic helper has to be skipped. A firmware backed accessor does not
   fit the current callbacks, which take no device context, cannot
   report a failed request and are called under spinlocks, while a
   firmware request sleeps.

 - ETR: flushed and stopped (TMC BEGIN_READ) before its pointers are
   read, resumed after a read with TMC END_READ, not enabled while a
   flush is pending or a memory error is reported, buffer configured in
   the order buffer mode, buffer, FFCR, and AUTHSTATUS checked at probe.
   The pointers are not used after a failed enable and are checked
   against the buffer, the buffer is reported empty instead of keeping
   stale metadata, freed on removal once the stop is confirmed, and the
   ETR is stopped on shutdown (sashiko).

 - STM: trace ID set before the stimulus ports are enabled and the STM
   started, and cleared last when it is stopped; number of stimulus
   ports and data size read through the firmware; unused hardware event
   support removed. The stimulus ports are only written once the
   firmware confirmed the STM is enabled, a failed enable is followed by
   a stop, removal no longer disables the source behind the CoreSight
   core, and the trace ID is released after the device is unregistered
   (sashiko).

 - The TMC driver is generic over the TMC configuration, with only the
   ETR configuration implemented, so that an ETF can be added later.

- Link to RFC: https://lore.kernel.org/r/20261119-introduce-qcom-qecp-feature-v1-0-b1a730d2c57a@oss.qualcomm.com

---
Jie Gan (8):
      soc: qcom: add QDCP client library
      coresight: tmc: split ETR buffer sync from hardware state readback
      coresight: tmc: factor out common ETR probe initialisation
      coresight: tmc: move trigger_cntr into its own attribute group
      dt-bindings: arm: qcom: add QECP owned CoreSight STM
      coresight: qcom: add QECP owned STM driver
      dt-bindings: arm: qcom: add QECP owned CoreSight TMC
      coresight: tmc: add QECP owned TMC driver

 .../bindings/arm/qcom,coresight-qecp-stm.yaml      |   76 ++
 .../bindings/arm/qcom,coresight-qecp-tmc.yaml      |   83 ++
 drivers/hwtracing/coresight/Kconfig                |   34 +
 drivers/hwtracing/coresight/Makefile               |    3 +
 .../hwtracing/coresight/coresight-qcom-qdcp-stm.c  |  614 +++++++++++
 drivers/hwtracing/coresight/coresight-tmc-core.c   |  118 ++-
 drivers/hwtracing/coresight/coresight-tmc-etr.c    |   78 +-
 drivers/hwtracing/coresight/coresight-tmc-qdcp.c   |  734 +++++++++++++
 drivers/hwtracing/coresight/coresight-tmc.h        |   20 +
 drivers/soc/qcom/Kconfig                           |   10 +
 drivers/soc/qcom/Makefile                          |    1 +
 drivers/soc/qcom/qcom_qdcp.c                       | 1118 ++++++++++++++++++++
 include/linux/soc/qcom/qdcp.h                      |  120 +++
 13 files changed, 2946 insertions(+), 63 deletions(-)
---
base-commit: e8017d29067fd1310a65c3494ce1a1b492adbac6
change-id: 20261119-introduce-qcom-qecp-feature-f59c05a21179

Best regards,
-- 
Jie Gan <jie.gan@oss.qualcomm.com>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/8] soc: qcom: add QDCP client library
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:11 ` [PATCH v2 2/8] coresight: tmc: split ETR buffer sync from hardware state readback Jie Gan
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

On the Qualcomm Hawi and Maili SoCs, part of the CoreSight trace
hardware is owned by the Qualcomm Embedded Co-Processor (QECP). The
control registers of these blocks are protected by access control and
cannot be accessed from the application processor; the QECP firmware
programs them on request instead. The requests are carried by the
Qualcomm Debug Control Protocol (QDCP), a QMI service provided by the
QECP firmware.

Add a client library for QDCP, selected by the drivers of the
QECP-owned blocks. Each driver creates its own QMI client with
devm_qdcp_client_get(), passing the QMI service, version and instance
of the QDCP server, which are fixed by the platform firmware. The
server may register after the driver probes, so
qdcp_client_wait_for_service() lets a driver wait for it. The client is
released after the driver's remove callback, so that a driver can still
stop its block from there.

The firmware identifies each block by the base address of its
registers. The library provides:

 - requests common to all trace sources and sinks, taking their type:
   qdcp_source_set_atid() sets the trace ID of a source, and
   qdcp_source_{enable,disable}() and qdcp_sink_{enable,disable}()
   start and stop a source or a sink;

 - requests specific to a block type: qdcp_stm_set_port_mask() and
   qdcp_stm_get_param() for the STM, and qdcp_tmc_etr_configure(),
   qdcp_tmc_get_param() and qdcp_tmc_{begin,end}_read() for the TMC,
   which program the buffer mode, trace buffer and FFCR of an ETR, read
   TMC registers and parameters such as DEVID, STS and the read and
   write pointers, and flush and stop a TMC before its trace data is
   read.

The requests sleep and must not be issued from atomic context. A failed
or timed out request may still have been executed by the firmware, so
the caller has to stop the block again before releasing any resource
the block may still use.

Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
 drivers/soc/qcom/Kconfig      |   10 +
 drivers/soc/qcom/Makefile     |    1 +
 drivers/soc/qcom/qcom_qdcp.c  | 1118 +++++++++++++++++++++++++++++++++++++++++
 include/linux/soc/qcom/qdcp.h |  120 +++++
 4 files changed, 1249 insertions(+)

diff --git a/drivers/soc/qcom/Kconfig b/drivers/soc/qcom/Kconfig
index 535c8619197b..6f20a0ecde0a 100644
--- a/drivers/soc/qcom/Kconfig
+++ b/drivers/soc/qcom/Kconfig
@@ -330,6 +330,16 @@ 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
+	depends on NET
+	select QCOM_QMI_HELPERS
+	help
+	  Client library for the Qualcomm Debug Control Protocol (QDCP), the
+	  QMI service used to control debug and trace blocks owned by the
+	  Qualcomm Embedded Co-Processor (QECP) firmware. Drivers for such
+	  blocks select it.
+
 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..28be1480e21e
--- /dev/null
+++ b/drivers/soc/qcom/qcom_qdcp.c
@@ -0,0 +1,1118 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ *
+ * Client library for the Qualcomm Debug Control Protocol (QDCP), the QMI
+ * service used to control debug and trace blocks owned by the Qualcomm
+ * Embedded Co-Processor (QECP) firmware.
+ */
+
+#include <linux/device.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/soc/qcom/qdcp.h>
+#include <linux/soc/qcom/qmi.h>
+#include <linux/wait.h>
+
+#define QDCP_TIMEOUT_MS			10000
+#define QDCP_MAX_ATIDS			8
+
+#define QDCP_STM_SET_STATE		0x0003
+#define QDCP_STM_SET_PARAM		0x0007
+#define QDCP_STM_GET_PARAM		0x0008
+#define QDCP_TMC_SET_STATE		0x002b
+#define QDCP_TMC_ASSIGN_BUFFER		0x002d
+#define QDCP_TMC_BEGIN_READ		0x0030
+#define QDCP_TMC_END_READ		0x0032
+#define QDCP_TMC_SET_PARAM		0x0033
+#define QDCP_TMC_GET_PARAM		0x0034
+#define QDCP_ATID_SET			0x0036
+
+/*
+ * Maximum encoded request lengths. Each top level element is a TLV with a
+ * 1 byte type and a 2 byte length in front of its payload.
+ */
+#define QDCP_STATE_REQ_MAX_LEN			18
+#define QDCP_STM_SET_PARAM_REQ_MAX_LEN		25
+#define QDCP_STM_GET_PARAM_REQ_MAX_LEN		18
+#define QDCP_ATID_SET_REQ_MAX_LEN		30
+#define QDCP_TMC_ASSIGN_BUFFER_REQ_MAX_LEN	38
+#define QDCP_TMC_ID_REQ_MAX_LEN			11
+#define QDCP_TMC_SET_PARAM_REQ_MAX_LEN		29
+#define QDCP_TMC_GET_PARAM_REQ_MAX_LEN		18
+
+/*
+ * Maximum size of a message from the QDCP server, as defined by the QDCP
+ * interface, used as the size of the QMI receive buffer.
+ */
+#define QDCP_RECV_MAX_LEN			16401
+
+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.
+ */
+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 source_id;
+	u32 param;
+	u32 value;
+};
+
+struct qdcp_stm_get_param_req_v01 {
+	u64 source_id;
+	u32 param;
+};
+
+struct qdcp_stm_get_param_resp_v01 {
+	struct qmi_response_type_v01 resp;
+	u8 value_valid;
+	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_id_req_v01 {
+	u64 tmc_id;
+};
+
+struct qdcp_tmc_begin_read_resp_v01 {
+	struct qmi_response_type_v01 resp;
+	u8 write_ptr_valid;
+	u64 write_ptr;
+	u8 status_valid;
+	u32 status;
+};
+
+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);
+static_assert(offsetof(struct qdcp_stm_get_param_resp_v01, resp) == 0);
+static_assert(offsetof(struct qdcp_tmc_begin_read_resp_v01, resp) == 0);
+
+/**
+ * struct qdcp_client - QDCP connection owned by a client device
+ * @dev:	client device.
+ * @qmi:	QMI handle.
+ * @lock:	protects @sq and @connected.
+ * @sq:		address of the QDCP server.
+ * @connected:	the QDCP server is available.
+ * @wait:	woken up when the QDCP server becomes available.
+ */
+struct qdcp_client {
+	struct device *dev;
+	struct qmi_handle qmi;
+	/* Protects @sq and @connected */
+	struct mutex lock;
+	struct sockaddr_qrtr sq;
+	bool connected;
+	wait_queue_head_t wait;
+};
+
+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, source_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,
+	},
+	{
+		.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 const struct qmi_elem_info qdcp_stm_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_stm_get_param_req_v01, source_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_get_param_req_v01, param),
+	},
+	{
+		.data_type = QMI_EOTI,
+	},
+};
+
+static const struct qmi_elem_info qdcp_stm_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_stm_get_param_resp_v01, resp),
+		.ei_array = qmi_response_type_v01_ei,
+	},
+	{
+		.data_type = QMI_OPT_FLAG,
+		.elem_len = 1,
+		.elem_size = sizeof(u8),
+		.array_type = NO_ARRAY,
+		.tlv_type = 0x03,
+		.offset = offsetof(struct qdcp_stm_get_param_resp_v01,
+				   value_valid),
+	},
+	{
+		.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_get_param_resp_v01, value),
+	},
+	{
+		.data_type = QMI_EOTI,
+	},
+};
+
+static const struct qmi_elem_info qdcp_tmc_id_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_id_req_v01, tmc_id),
+	},
+	{
+		.data_type = QMI_EOTI,
+	},
+};
+
+/*
+ * The write pointer and status in the reply are not used, but must be
+ * described as qmi_decode_message() rejects unknown mandatory TLVs.
+ */
+static const struct qmi_elem_info qdcp_tmc_begin_read_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_begin_read_resp_v01, resp),
+		.ei_array = qmi_response_type_v01_ei,
+	},
+	{
+		.data_type = QMI_OPT_FLAG,
+		.elem_len = 1,
+		.elem_size = sizeof(u8),
+		.array_type = NO_ARRAY,
+		.tlv_type = 0x03,
+		.offset = offsetof(struct qdcp_tmc_begin_read_resp_v01,
+				   write_ptr_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_begin_read_resp_v01, write_ptr),
+	},
+	{
+		.data_type = QMI_OPT_FLAG,
+		.elem_len = 1,
+		.elem_size = sizeof(u8),
+		.array_type = NO_ARRAY,
+		.tlv_type = 0x04,
+		.offset = offsetof(struct qdcp_tmc_begin_read_resp_v01,
+				   status_valid),
+	},
+	{
+		.data_type = QMI_UNSIGNED_4_BYTE,
+		.elem_len = 1,
+		.elem_size = sizeof(u32),
+		.array_type = NO_ARRAY,
+		.tlv_type = 0x04,
+		.offset = offsetof(struct qdcp_tmc_begin_read_resp_v01, status),
+	},
+	{
+		.data_type = QMI_EOTI,
+	},
+};
+
+static int qdcp_check_resp(struct qdcp_client *client,
+			   const struct qmi_response_type_v01 *resp)
+{
+	if (resp->result == QMI_RESULT_SUCCESS_V01)
+		return 0;
+
+	dev_err(client->dev, "QDCP request failed: result=%u error=%u\n",
+		resp->result, resp->error);
+	return -EIO;
+}
+
+static int qdcp_send_resp(struct qdcp_client *client, 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;
+	int ret;
+
+	/*
+	 * Don't hold the lock across qmi_txn_wait(): the server callbacks
+	 * take it on the QMI workqueue, which must also deliver the response.
+	 */
+	mutex_lock(&client->lock);
+	connected = client->connected;
+	sq = client->sq;
+	mutex_unlock(&client->lock);
+
+	if (!connected)
+		return -EAGAIN;
+
+	/*
+	 * qmi_decode_message() does not fail on a missing TLV, so poison the
+	 * result to make a reply without the result TLV read as a failure.
+	 */
+	memset(resp, 0, resp_len);
+	qmi_resp->result = QMI_RESULT_FAILURE_V01;
+	qmi_resp->error = QMI_ERR_MALFORMED_MSG_V01;
+
+	ret = qmi_txn_init(&client->qmi, &txn, resp_ei, resp);
+	if (ret < 0)
+		return ret;
+
+	ret = qmi_send_request(&client->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(client, qmi_resp);
+}
+
+static int qdcp_send(struct qdcp_client *client, 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(client, msg_id, req_ei, req, req_len,
+			      qdcp_resp_ei, &resp, sizeof(resp));
+}
+
+static int qdcp_set_state(struct qdcp_client *client, unsigned int msg_id,
+			  u64 id, bool enable)
+{
+	struct qdcp_state_req_v01 req = {
+		.id = id,
+		.state = enable,
+	};
+
+	return qdcp_send(client, msg_id, qdcp_state_req_ei, &req,
+			 QDCP_STATE_REQ_MAX_LEN);
+}
+
+static int qdcp_set_atid(struct qdcp_client *client,
+			 enum qdcp_source_type type, u64 source_id, u8 atid)
+{
+	struct qdcp_atid_set_req_v01 req = {
+		.trace_source = {
+			.type = type,
+			.id = source_id,
+		},
+		.atids = { atid },
+		.num_atids = 1,
+	};
+
+	return qdcp_send(client, QDCP_ATID_SET, qdcp_atid_set_req_ei, &req,
+			 QDCP_ATID_SET_REQ_MAX_LEN);
+}
+
+/* SET_STATE message of each source and sink type */
+static const u16 qdcp_source_set_state[] = {
+	[QDCP_SOURCE_STM]	= QDCP_STM_SET_STATE,
+};
+
+static const u16 qdcp_sink_set_state[] = {
+	[QDCP_SINK_ETR]		= QDCP_TMC_SET_STATE,
+};
+
+static unsigned int qdcp_source_msg(enum qdcp_source_type type)
+{
+	if (type >= ARRAY_SIZE(qdcp_source_set_state))
+		return 0;
+
+	return qdcp_source_set_state[type];
+}
+
+static unsigned int qdcp_sink_msg(enum qdcp_sink_type type)
+{
+	if (type >= ARRAY_SIZE(qdcp_sink_set_state))
+		return 0;
+
+	return qdcp_sink_set_state[type];
+}
+
+/**
+ * qdcp_source_set_atid() - set the trace ID of a firmware owned trace source
+ * @client:	QDCP client.
+ * @type:	type of the trace source.
+ * @source_id:	firmware identifier of the source, the base address of its
+ *		registers.
+ * @atid:	trace ID to tag the trace with, or 0 to clear it.
+ *
+ * Return: 0 on success, -EINVAL for an unknown @type, -EAGAIN if the QDCP
+ * server is not available, or another negative error code.
+ */
+int qdcp_source_set_atid(struct qdcp_client *client,
+			 enum qdcp_source_type type, u64 source_id, u8 atid)
+{
+	if (!qdcp_source_msg(type))
+		return -EINVAL;
+
+	return qdcp_set_atid(client, type, source_id, atid);
+}
+EXPORT_SYMBOL_GPL(qdcp_source_set_atid);
+
+/**
+ * qdcp_source_enable() - enable a firmware owned trace source
+ * @client:	QDCP client.
+ * @type:	type of the trace source.
+ * @source_id:	firmware identifier of the source, the base address of its
+ *		registers.
+ *
+ * The trace ID and any source specific configuration must be set before.
+ *
+ * Return: 0 on success, -EINVAL for an unknown @type, -EAGAIN if the QDCP
+ * server is not available, or another negative error code.
+ */
+int qdcp_source_enable(struct qdcp_client *client, enum qdcp_source_type type,
+		       u64 source_id)
+{
+	unsigned int msg_id = qdcp_source_msg(type);
+
+	if (!msg_id)
+		return -EINVAL;
+
+	return qdcp_set_state(client, msg_id, source_id, true);
+}
+EXPORT_SYMBOL_GPL(qdcp_source_enable);
+
+/**
+ * qdcp_source_disable() - disable a firmware owned trace source
+ * @client:	QDCP client.
+ * @type:	type of the trace source.
+ * @source_id:	firmware identifier of the source, the base address of its
+ *		registers.
+ *
+ * Return: 0 once firmware confirmed the source is stopped, -EINVAL for an
+ * unknown @type, or another negative error code.
+ */
+int qdcp_source_disable(struct qdcp_client *client, enum qdcp_source_type type,
+			u64 source_id)
+{
+	unsigned int msg_id = qdcp_source_msg(type);
+
+	if (!msg_id)
+		return -EINVAL;
+
+	return qdcp_set_state(client, msg_id, source_id, false);
+}
+EXPORT_SYMBOL_GPL(qdcp_source_disable);
+
+/**
+ * qdcp_stm_set_port_mask() - set the stimulus ports of a firmware owned STM
+ * @client:	QDCP client.
+ * @source_id:	firmware identifier of the STM, the base address of its
+ *		registers.
+ * @port_mask:	stimulus port enable mask, 0 disables all the ports.
+ *
+ * Return: 0 on success, -EAGAIN if the QDCP server is not available, or
+ * another negative error code.
+ */
+int qdcp_stm_set_port_mask(struct qdcp_client *client, u64 source_id,
+			   u32 port_mask)
+{
+	struct qdcp_stm_set_param_req_v01 req = {
+		.source_id = source_id,
+		.param = QDCP_STM_PARAM_PORT_ENABLE,
+		.value = port_mask,
+	};
+
+	return qdcp_send(client, QDCP_STM_SET_PARAM, qdcp_stm_set_param_req_ei,
+			 &req, QDCP_STM_SET_PARAM_REQ_MAX_LEN);
+}
+EXPORT_SYMBOL_GPL(qdcp_stm_set_port_mask);
+
+/**
+ * qdcp_stm_get_param() - read an STM parameter from firmware
+ * @client:	QDCP client.
+ * @source_id:	firmware identifier of the STM, the base address of its
+ *		registers.
+ * @param:	parameter to read.
+ * @value:	returned parameter value.
+ *
+ * Return: 0 on success, -EAGAIN if the QDCP server is not available,
+ * -EPROTO if the reply has no value, or another negative error code.
+ */
+int qdcp_stm_get_param(struct qdcp_client *client, u64 source_id,
+		       enum qdcp_stm_param param, u32 *value)
+{
+	struct qdcp_stm_get_param_req_v01 req = {
+		.source_id = source_id,
+		.param = param,
+	};
+	struct qdcp_stm_get_param_resp_v01 resp;
+	int ret;
+
+	ret = qdcp_send_resp(client, QDCP_STM_GET_PARAM,
+			     qdcp_stm_get_param_req_ei, &req,
+			     QDCP_STM_GET_PARAM_REQ_MAX_LEN,
+			     qdcp_stm_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_stm_get_param);
+
+/**
+ * qdcp_sink_enable() - enable a firmware owned trace sink
+ * @client:	QDCP client.
+ * @type:	type of the trace sink.
+ * @sink_id:	firmware identifier of the sink, the base address of its
+ *		registers.
+ *
+ * Any sink specific configuration must be done before. A failed or timed
+ * out request may still have been executed by firmware, so the caller must
+ * call qdcp_sink_disable() before releasing the resources used by the sink.
+ *
+ * Return: 0 on success, -EINVAL for an unknown @type, -EAGAIN if the QDCP
+ * server is not available, or another negative error code.
+ */
+int qdcp_sink_enable(struct qdcp_client *client, enum qdcp_sink_type type,
+		     u64 sink_id)
+{
+	unsigned int msg_id = qdcp_sink_msg(type);
+
+	if (!msg_id)
+		return -EINVAL;
+
+	return qdcp_set_state(client, msg_id, sink_id, true);
+}
+EXPORT_SYMBOL_GPL(qdcp_sink_enable);
+
+/**
+ * qdcp_sink_disable() - disable a firmware owned trace sink
+ * @client:	QDCP client.
+ * @type:	type of the trace sink.
+ * @sink_id:	firmware identifier of the sink, the base address of its
+ *		registers.
+ *
+ * Return: 0 once firmware confirmed the sink is stopped, -EINVAL for an
+ * unknown @type, or another negative error code, in which case firmware may
+ * still be using the resources of the sink, such as its trace buffer.
+ */
+int qdcp_sink_disable(struct qdcp_client *client, enum qdcp_sink_type type,
+		      u64 sink_id)
+{
+	unsigned int msg_id = qdcp_sink_msg(type);
+
+	if (!msg_id)
+		return -EINVAL;
+
+	return qdcp_set_state(client, msg_id, sink_id, false);
+}
+EXPORT_SYMBOL_GPL(qdcp_sink_disable);
+
+static int qdcp_tmc_set_param(struct qdcp_client *client, 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,
+	};
+
+	return qdcp_send(client, QDCP_TMC_SET_PARAM, qdcp_tmc_set_param_req_ei,
+			 &req, QDCP_TMC_SET_PARAM_REQ_MAX_LEN);
+}
+
+/**
+ * qdcp_tmc_get_param() - read a TMC parameter from firmware
+ * @client:	QDCP client.
+ * @tmc_id:	firmware identifier of the TMC, the base address of its
+ *		registers.
+ * @param:	parameter to read.
+ * @value:	returned parameter value.
+ *
+ * Return: 0 on success, -EAGAIN if the QDCP server is not available,
+ * -EPROTO if the reply has no value, or another negative error code.
+ */
+int qdcp_tmc_get_param(struct qdcp_client *client, 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(client, QDCP_TMC_GET_PARAM,
+			     qdcp_tmc_get_param_req_ei, &req,
+			     QDCP_TMC_GET_PARAM_REQ_MAX_LEN,
+			     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_assign_buffer(struct qdcp_client *client, 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,
+		},
+	};
+
+	return qdcp_send(client, QDCP_TMC_ASSIGN_BUFFER,
+			 qdcp_tmc_assign_buffer_req_ei, &req,
+			 QDCP_TMC_ASSIGN_BUFFER_REQ_MAX_LEN);
+}
+
+/**
+ * qdcp_tmc_etr_configure() - configure a firmware owned TMC ETR
+ * @client:	QDCP client.
+ * @tmc_id:	firmware identifier of the TMC, the base address of its
+ *		registers.
+ * @cfg:	ETR configuration.
+ *
+ * Program the buffer mode, the trace buffer and the FFCR. The ETR is then
+ * enabled with qdcp_sink_enable(). The configuration is not rolled back on
+ * failure.
+ *
+ * Return: 0 on success, -EAGAIN if the QDCP server is not available, or
+ * another negative error code.
+ */
+int qdcp_tmc_etr_configure(struct qdcp_client *client, u64 tmc_id,
+			   const struct qdcp_tmc_cfg *cfg)
+{
+	int ret;
+
+	ret = qdcp_tmc_set_param(client, tmc_id, QDCP_TMC_PARAM_SG_MODE,
+				 cfg->sg_mode);
+	if (ret)
+		return ret;
+
+	ret = qdcp_tmc_assign_buffer(client, tmc_id, cfg->paddr, cfg->iaddr,
+				     cfg->size);
+	if (ret)
+		return ret;
+
+	return qdcp_tmc_set_param(client, tmc_id, QDCP_TMC_PARAM_FFCR,
+				  cfg->ffcr);
+}
+EXPORT_SYMBOL_GPL(qdcp_tmc_etr_configure);
+
+static int qdcp_tmc_send_id(struct qdcp_client *client, unsigned int msg_id,
+			    u64 tmc_id, const struct qmi_elem_info *resp_ei,
+			    void *resp, size_t resp_len)
+{
+	struct qdcp_tmc_id_req_v01 req = {
+		.tmc_id = tmc_id,
+	};
+
+	return qdcp_send_resp(client, msg_id, qdcp_tmc_id_req_ei, &req,
+			      QDCP_TMC_ID_REQ_MAX_LEN, resp_ei, resp, resp_len);
+}
+
+/**
+ * qdcp_tmc_begin_read() - flush and stop a firmware owned TMC for reading
+ * @client:	QDCP client.
+ * @tmc_id:	firmware identifier of the TMC, the base address of its
+ *		registers.
+ *
+ * Flush and stop the TMC, so that its read and write pointers describe the
+ * trace data. qdcp_tmc_end_read() is called before the TMC is enabled again
+ * for the same session.
+ *
+ * Return: 0 on success, -EAGAIN if the QDCP server is not available, or
+ * another negative error code.
+ */
+int qdcp_tmc_begin_read(struct qdcp_client *client, u64 tmc_id)
+{
+	struct qdcp_tmc_begin_read_resp_v01 resp;
+
+	return qdcp_tmc_send_id(client, QDCP_TMC_BEGIN_READ, tmc_id,
+				qdcp_tmc_begin_read_resp_ei, &resp,
+				sizeof(resp));
+}
+EXPORT_SYMBOL_GPL(qdcp_tmc_begin_read);
+
+/**
+ * qdcp_tmc_end_read() - end reading a firmware owned TMC
+ * @client:	QDCP client.
+ * @tmc_id:	firmware identifier of the TMC, the base address of its
+ *		registers.
+ *
+ * Return: 0 on success, -EAGAIN if the QDCP server is not available, or
+ * another negative error code.
+ */
+int qdcp_tmc_end_read(struct qdcp_client *client, u64 tmc_id)
+{
+	struct qdcp_resp_v01 resp;
+
+	return qdcp_tmc_send_id(client, QDCP_TMC_END_READ, tmc_id,
+				qdcp_resp_ei, &resp, sizeof(resp));
+}
+EXPORT_SYMBOL_GPL(qdcp_tmc_end_read);
+
+static int qdcp_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
+{
+	struct qdcp_client *client = container_of(qmi, struct qdcp_client,
+						  qmi);
+
+	mutex_lock(&client->lock);
+	client->sq.sq_family = AF_QIPCRTR;
+	client->sq.sq_node = svc->node;
+	client->sq.sq_port = svc->port;
+	WRITE_ONCE(client->connected, true);
+	svc->priv = client;
+	mutex_unlock(&client->lock);
+
+	wake_up_all(&client->wait);
+	dev_dbg(client->dev, "QDCP server %u:%u available\n", svc->node,
+		svc->port);
+	return 0;
+}
+
+static void qdcp_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
+{
+	struct qdcp_client *client = svc->priv;
+
+	if (!client)
+		return;
+
+	mutex_lock(&client->lock);
+	/*
+	 * A firmware restart can announce the new server before the old one
+	 * is removed, so only disconnect if this is the current server.
+	 */
+	if (client->connected && client->sq.sq_node == svc->node &&
+	    client->sq.sq_port == svc->port) {
+		WRITE_ONCE(client->connected, false);
+		memset(&client->sq, 0, sizeof(client->sq));
+		dev_dbg(client->dev, "QDCP server %u:%u removed\n", svc->node,
+			svc->port);
+	}
+	mutex_unlock(&client->lock);
+}
+
+static void qdcp_net_reset(struct qmi_handle *qmi)
+{
+	struct qdcp_client *client = container_of(qmi, struct qdcp_client,
+						  qmi);
+
+	mutex_lock(&client->lock);
+	WRITE_ONCE(client->connected, false);
+	memset(&client->sq, 0, sizeof(client->sq));
+	mutex_unlock(&client->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_client_release(void *data)
+{
+	struct qdcp_client *client = data;
+
+	qmi_handle_release(&client->qmi);
+}
+
+/**
+ * devm_qdcp_client_get() - create a QDCP client for a device
+ * @dev:	client device.
+ * @svc:	QDCP service to connect to.
+ *
+ * Create a QMI handle and look up the QDCP server described by @svc. The
+ * server may become available later, see qdcp_client_wait_for_service().
+ * The client is released when @dev is unbound, after its remove callback,
+ * so the driver can still stop the blocks it controls from there.
+ *
+ * Return: the client, or an error pointer on failure.
+ */
+struct qdcp_client *devm_qdcp_client_get(struct device *dev,
+					 const struct qdcp_service *svc)
+{
+	struct qdcp_client *client;
+	int ret;
+
+	client = devm_kzalloc(dev, sizeof(*client), GFP_KERNEL);
+	if (!client)
+		return ERR_PTR(-ENOMEM);
+
+	client->dev = dev;
+	init_waitqueue_head(&client->wait);
+	ret = devm_mutex_init(dev, &client->lock);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = qmi_handle_init(&client->qmi, QDCP_RECV_MAX_LEN, &qdcp_qmi_ops,
+			      NULL);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = devm_add_action_or_reset(dev, qdcp_client_release, client);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = qmi_add_lookup(&client->qmi, svc->service_id, svc->version,
+			     svc->instance_id);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return client;
+}
+EXPORT_SYMBOL_GPL(devm_qdcp_client_get);
+
+/**
+ * qdcp_client_wait_for_service() - wait for the QDCP server
+ * @client:	QDCP client.
+ * @timeout_ms:	maximum time to wait in milliseconds.
+ *
+ * Return: 0 if the server is available, -ETIMEDOUT otherwise.
+ */
+int qdcp_client_wait_for_service(struct qdcp_client *client,
+				 unsigned int timeout_ms)
+{
+	if (!wait_event_timeout(client->wait, READ_ONCE(client->connected),
+				msecs_to_jiffies(timeout_ms)))
+		return -ETIMEDOUT;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(qdcp_client_wait_for_service);
+
+MODULE_DESCRIPTION("Qualcomm Debug Control Protocol client library");
+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..4218a9ff63c6
--- /dev/null
+++ b/include/linux/soc/qcom/qdcp.h
@@ -0,0 +1,120 @@
+/* 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/types.h>
+
+struct device;
+struct qdcp_client;
+
+/**
+ * struct qdcp_service - QMI service implementing QDCP for a set of blocks
+ * @service_id:	QMI service identifier.
+ * @version:	QMI service version.
+ * @instance_id: QMI service instance identifier.
+ *
+ * The service is fixed by the platform firmware, so drivers provide it
+ * from their SoC specific match data. The blocks controlled through the
+ * service are identified by the base address of their registers: the
+ * source ID of a trace source such as the STM, and the TMC ID of a TMC.
+ */
+struct qdcp_service {
+	u32 service_id;
+	u32 version;
+	u32 instance_id;
+};
+
+/**
+ * enum qdcp_source_type - firmware owned trace source types
+ * @QDCP_SOURCE_STM:	System Trace Macrocell.
+ *
+ * The values are used by the firmware to identify the type of a source.
+ */
+enum qdcp_source_type {
+	QDCP_SOURCE_STM = 0,
+};
+
+/**
+ * enum qdcp_sink_type - firmware owned trace sink types
+ * @QDCP_SINK_ETR:	TMC configured as an Embedded Trace Router.
+ */
+enum qdcp_sink_type {
+	QDCP_SINK_ETR,
+};
+
+/*
+ * QDCP STM parameter identifiers used in QDCP_STM_GET_PARAM/SET_PARAM
+ * requests. Register parameters use the offset of the STM register, the
+ * firmware reads or writes the register on behalf of the caller.
+ */
+enum qdcp_stm_param {
+	QDCP_STM_PARAM_SPFEAT2R = 0xea4,
+	QDCP_STM_PARAM_DEVID = 0xfc8,
+	QDCP_STM_PARAM_PORT_ENABLE = 0x1002,
+};
+
+/*
+ * QDCP TMC parameter identifiers used in QDCP_TMC_GET_PARAM/SET_PARAM
+ * requests. Register parameters use the offset of the TMC register, the
+ * firmware reads or writes the register on behalf of the caller. SG_MODE,
+ * RRP and RWP are firmware only parameter IDs.
+ */
+enum qdcp_tmc_param {
+	QDCP_TMC_PARAM_STS = 0x00c,
+	QDCP_TMC_PARAM_TRG = 0x01c,
+	QDCP_TMC_PARAM_FFCR = 0x304,
+	QDCP_TMC_PARAM_AUTHSTATUS = 0xfb8,
+	QDCP_TMC_PARAM_DEVID = 0xfc8,
+	QDCP_TMC_PARAM_SG_MODE = 0x1002,
+	QDCP_TMC_PARAM_RRP = 0x1003,
+	QDCP_TMC_PARAM_RWP = 0x1004,
+};
+
+/**
+ * struct qdcp_tmc_cfg - TMC ETR configuration passed to firmware
+ * @paddr:	physical address of the trace buffer.
+ * @iaddr:	DMA address of the trace buffer as seen by the ETR.
+ * @size:	size of the trace buffer in bytes.
+ * @sg_mode:	the buffer uses the TMC ETR scatter-gather table format.
+ * @ffcr:	formatter and flush control register value.
+ */
+struct qdcp_tmc_cfg {
+	phys_addr_t paddr;
+	dma_addr_t iaddr;
+	size_t size;
+	bool sg_mode;
+	u32 ffcr;
+};
+
+struct qdcp_client *devm_qdcp_client_get(struct device *dev,
+					 const struct qdcp_service *svc);
+int qdcp_client_wait_for_service(struct qdcp_client *client,
+				 unsigned int timeout_ms);
+
+int qdcp_source_set_atid(struct qdcp_client *client,
+			 enum qdcp_source_type type, u64 source_id, u8 atid);
+int qdcp_source_enable(struct qdcp_client *client, enum qdcp_source_type type,
+		       u64 source_id);
+int qdcp_source_disable(struct qdcp_client *client, enum qdcp_source_type type,
+			u64 source_id);
+int qdcp_stm_set_port_mask(struct qdcp_client *client, u64 source_id,
+			   u32 port_mask);
+int qdcp_stm_get_param(struct qdcp_client *client, u64 source_id,
+		       enum qdcp_stm_param param, u32 *value);
+
+int qdcp_sink_enable(struct qdcp_client *client, enum qdcp_sink_type type,
+		     u64 sink_id);
+int qdcp_sink_disable(struct qdcp_client *client, enum qdcp_sink_type type,
+		      u64 sink_id);
+int qdcp_tmc_etr_configure(struct qdcp_client *client, u64 tmc_id,
+			   const struct qdcp_tmc_cfg *cfg);
+int qdcp_tmc_get_param(struct qdcp_client *client, u64 tmc_id,
+		       enum qdcp_tmc_param param, u64 *value);
+int qdcp_tmc_begin_read(struct qdcp_client *client, u64 tmc_id);
+int qdcp_tmc_end_read(struct qdcp_client *client, u64 tmc_id);
+
+#endif

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/8] coresight: tmc: split ETR buffer sync from hardware state readback
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
  2026-11-30  7:11 ` [PATCH v2 1/8] soc: qcom: add QDCP client library Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:11 ` [PATCH v2 3/8] coresight: tmc: factor out common ETR probe initialisation Jie Gan
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

tmc_sync_etr_buf() reads RRP, RWP and STS from the TMC registers and
uses them to update the ETR buffer offset, length and full flag. The
register reads and the buffer update are independent steps.

Add struct tmc_etr_hw_state for the RRP/RWP/STS triple and split the
sync into tmc_etr_read_hw_state(), which reads the registers, and
tmc_etr_sync_buf_from_state(), which updates the buffer from a given
state. Do the same for the sysfs buffer sync. tmc_sync_etr_buf() and
tmc_etr_sync_sysfs_buf() keep their behaviour by reading the registers
and calling the new helpers.

This allows an ETR whose RRP/RWP/STS are obtained by other means to
reuse the buffer sync logic.

No functional change intended.

Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 52 ++++++++++++++++++-------
 drivers/hwtracing/coresight/coresight-tmc.h     | 12 ++++++
 2 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c
index 361a433e6f0c..3f1b4f5b011c 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-etr.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c
@@ -1019,27 +1019,31 @@ tmc_etr_buf_insert_barrier_packet(struct etr_buf *etr_buf, u64 offset)
 	return offset + CORESIGHT_BARRIER_PKT_SIZE;
 }
 
+static void tmc_etr_read_hw_state(struct tmc_drvdata *drvdata,
+				  struct tmc_etr_hw_state *state)
+{
+	state->rrp = tmc_read_rrp(drvdata);
+	state->rwp = tmc_read_rwp(drvdata);
+	state->sts = readl_relaxed(drvdata->base + TMC_STS);
+}
+
 /*
- * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata.
+ * tmc_etr_sync_buf_from_state: Sync the trace buffer availability with
+ * the ETR hardware state in @state.
  * Makes sure the trace data is synced to the memory for consumption.
  * @etr_buf->offset will hold the offset to the beginning of the trace data
  * within the buffer, with @etr_buf->len bytes to consume.
  */
-static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
+static void tmc_etr_sync_buf_from_state(struct tmc_drvdata *drvdata,
+					const struct tmc_etr_hw_state *state)
 {
 	struct etr_buf *etr_buf = drvdata->etr_buf;
-	u64 rrp, rwp;
-	u32 status;
-
-	rrp = tmc_read_rrp(drvdata);
-	rwp = tmc_read_rwp(drvdata);
-	status = readl_relaxed(drvdata->base + TMC_STS);
 
 	/*
 	 * If there were memory errors in the session, truncate the
 	 * buffer.
 	 */
-	if (WARN_ON_ONCE(status & TMC_STS_MEMERR)) {
+	if (WARN_ON_ONCE(state->sts & TMC_STS_MEMERR)) {
 		dev_dbg(&drvdata->csdev->dev,
 			"tmc memory error detected, truncating buffer\n");
 		etr_buf->len = 0;
@@ -1047,11 +1051,23 @@ static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
 		return;
 	}
 
-	etr_buf->full = !!(status & TMC_STS_FULL);
+	etr_buf->full = !!(state->sts & TMC_STS_FULL);
 
 	WARN_ON(!etr_buf->ops || !etr_buf->ops->sync);
 
-	etr_buf->ops->sync(etr_buf, rrp, rwp);
+	etr_buf->ops->sync(etr_buf, state->rrp, state->rwp);
+}
+
+/*
+ * tmc_sync_etr_buf: Sync the trace buffer availability with drvdata,
+ * reading the ETR hardware state from the TMC registers.
+ */
+static void tmc_sync_etr_buf(struct tmc_drvdata *drvdata)
+{
+	struct tmc_etr_hw_state state;
+
+	tmc_etr_read_hw_state(drvdata, &state);
+	tmc_etr_sync_buf_from_state(drvdata, &state);
 }
 
 static int __tmc_etr_enable_hw(struct tmc_drvdata *drvdata)
@@ -1187,7 +1203,9 @@ tmc_etr_free_sysfs_buf(struct etr_buf *buf)
 		tmc_free_etr_buf(buf);
 }
 
-static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
+static 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;
 
@@ -1195,7 +1213,7 @@ static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
 		tmc_etr_free_sysfs_buf(drvdata->sysfs_buf);
 		drvdata->sysfs_buf = NULL;
 	} else {
-		tmc_sync_etr_buf(drvdata);
+		tmc_etr_sync_buf_from_state(drvdata, state);
 		/*
 		 * Insert barrier packets at the beginning, if there was
 		 * an overflow.
@@ -1206,6 +1224,14 @@ static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
 	}
 }
 
+static void tmc_etr_sync_sysfs_buf(struct tmc_drvdata *drvdata)
+{
+	struct tmc_etr_hw_state state;
+
+	tmc_etr_read_hw_state(drvdata, &state);
+	tmc_etr_sync_sysfs_buf_from_state(drvdata, &state);
+}
+
 static void __tmc_etr_disable_hw(struct tmc_drvdata *drvdata)
 {
 	CS_UNLOCK(drvdata->base);
diff --git a/drivers/hwtracing/coresight/coresight-tmc.h b/drivers/hwtracing/coresight/coresight-tmc.h
index 319a354ede9f..c3eec5e75fd0 100644
--- a/drivers/hwtracing/coresight/coresight-tmc.h
+++ b/drivers/hwtracing/coresight/coresight-tmc.h
@@ -167,6 +167,18 @@ enum etr_mode {
 
 struct etr_buf_operations;
 
+/**
+ * struct tmc_etr_hw_state - ETR hardware state needed to sync a trace buffer
+ * @rrp	: RAM read pointer.
+ * @rwp	: RAM write pointer.
+ * @sts	: TMC status.
+ */
+struct tmc_etr_hw_state {
+	u64	rrp;
+	u64	rwp;
+	u32	sts;
+};
+
 /**
  * struct etr_buf - Details of the buffer used by ETR
  * refcount	; Number of sources currently using this etr_buf.

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 3/8] coresight: tmc: factor out common ETR probe initialisation
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
  2026-11-30  7:11 ` [PATCH v2 1/8] soc: qcom: add QDCP client library Jie Gan
  2026-11-30  7:11 ` [PATCH v2 2/8] coresight: tmc: split ETR buffer sync from hardware state readback Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:20   ` sashiko-bot
  2026-11-30  7:11 ` [PATCH v2 4/8] coresight: tmc: move trigger_cntr into its own attribute group Jie Gan
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

The TMC ETR probe mixes configuration that needs access to the TMC
registers, the AUTHSTATUS check and the PID based unadvertised
capabilities, with configuration derived only from DEVID and the
firmware properties: the buffer and AXI burst sizes, scatter-gather
support, the DMA mask and the perf buffer IDR.

Move the latter into tmc_etr_probe_common() and leave
tmc_etr_setup_caps() with the register based checks only. The buffer
and burst sizes are now set together with the rest of the ETR
configuration, after the reserved region lookup, which does not depend
on them.

This allows an ETR whose registers are not accessible by the kernel to
share the common ETR initialisation.

No functional change intended.

Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
 drivers/hwtracing/coresight/coresight-tmc-core.c | 101 ++++++++++++++---------
 1 file changed, 60 insertions(+), 41 deletions(-)

diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
index 0ef4b817b297..ad2c99eae5ae 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-core.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
@@ -601,7 +601,7 @@ static bool tmc_etr_can_use_sg(struct device *dev)
 		return fwnode_property_present(dev->fwnode, "arm,scatter-gather");
 	} else if (is_acpi_device_node(dev->fwnode)) {
 		/*
-		 * TMC_DEVID_NOSCAT test in tmc_etr_setup_caps(), has already ensured
+		 * TMC_DEVID_NOSCAT test in tmc_etr_probe_common(), has already ensured
 		 * this property is only checked for Coresight SoC 400 TMC configured
 		 * as ETR.
 		 */
@@ -676,12 +676,11 @@ static void tmc_get_reserved_region(struct device *parent)
 	drvdata->crash_mdata.size  = resource_size(&res);
 }
 
-/* Detect and initialise the capabilities of a TMC ETR */
-static int tmc_etr_setup_caps(struct device *parent, u32 devid,
+/* Detect and initialise the unadvertised capabilities of a TMC ETR */
+static int tmc_etr_setup_caps(struct device *parent,
 			      struct csdev_access *access)
 {
-	int rc;
-	u32 tmc_pid, dma_mask = 0;
+	u32 tmc_pid;
 	struct tmc_drvdata *drvdata = dev_get_drvdata(parent);
 	void *dev_caps;
 
@@ -694,34 +693,7 @@ static int tmc_etr_setup_caps(struct device *parent, u32 devid,
 	/* Set the unadvertised capabilities */
 	tmc_etr_init_caps(drvdata, (u32)(unsigned long)dev_caps);
 
-	if (!(devid & TMC_DEVID_NOSCAT) && tmc_etr_can_use_sg(parent))
-		tmc_etr_set_cap(drvdata, TMC_ETR_SG);
-
-	/* Check if the AXI address width is available */
-	if (devid & TMC_DEVID_AXIAW_VALID)
-		dma_mask = ((devid >> TMC_DEVID_AXIAW_SHIFT) &
-				TMC_DEVID_AXIAW_MASK);
-
-	/*
-	 * Unless specified in the device configuration, ETR uses a 40-bit
-	 * AXI master in place of the embedded SRAM of ETB/ETF.
-	 */
-	switch (dma_mask) {
-	case 32:
-	case 40:
-	case 44:
-	case 48:
-	case 52:
-		dev_info(parent, "Detected dma mask %dbits\n", dma_mask);
-		break;
-	default:
-		dma_mask = 40;
-	}
-
-	rc = dma_set_mask_and_coherent(parent, DMA_BIT_MASK(dma_mask));
-	if (rc)
-		dev_err(parent, "Failed to setup DMA mask: %d\n", rc);
-	return rc;
+	return 0;
 }
 
 static u32 tmc_etr_get_default_buffer_size(struct device *dev)
@@ -748,6 +720,56 @@ static u32 tmc_etr_get_max_burst_size(struct device *dev)
 	return burst_size;
 }
 
+/*
+ * Initialise the TMC ETR configuration derived from @devid and the
+ * firmware properties: buffer and AXI burst sizes, scatter-gather
+ * 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 rc;
+	u32 dma_mask = 0;
+
+	drvdata->size = tmc_etr_get_default_buffer_size(dev);
+	drvdata->max_burst_size = tmc_etr_get_max_burst_size(dev);
+
+	if (!(devid & TMC_DEVID_NOSCAT) && tmc_etr_can_use_sg(dev))
+		tmc_etr_set_cap(drvdata, TMC_ETR_SG);
+
+	/* Check if the AXI address width is available */
+	if (devid & TMC_DEVID_AXIAW_VALID)
+		dma_mask = ((devid >> TMC_DEVID_AXIAW_SHIFT) &
+				TMC_DEVID_AXIAW_MASK);
+
+	/*
+	 * Unless specified in the device configuration, ETR uses a 40-bit
+	 * AXI master in place of the embedded SRAM of ETB/ETF.
+	 */
+	switch (dma_mask) {
+	case 32:
+	case 40:
+	case 44:
+	case 48:
+	case 52:
+		dev_info(dev, "Detected dma mask %dbits\n", dma_mask);
+		break;
+	default:
+		dma_mask = 40;
+	}
+
+	rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_mask));
+	if (rc) {
+		dev_err(dev, "Failed to setup DMA mask: %d\n", rc);
+		return rc;
+	}
+
+	idr_init(&drvdata->idr);
+	mutex_init(&drvdata->idr_mutex);
+	return 0;
+}
+
 static void register_crash_dev_interface(struct tmc_drvdata *drvdata,
 					 const char *name)
 {
@@ -805,12 +827,8 @@ static int __tmc_probe(struct device *dev, struct resource *res)
 	drvdata->pid = -1;
 	drvdata->etr_mode = ETR_MODE_AUTO;
 
-	if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
-		drvdata->size = tmc_etr_get_default_buffer_size(dev);
-		drvdata->max_burst_size = tmc_etr_get_max_burst_size(dev);
-	} else {
+	if (drvdata->config_type != TMC_CONFIG_TYPE_ETR)
 		drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
-	}
 
 	tmc_get_reserved_region(dev);
 
@@ -829,11 +847,12 @@ static int __tmc_probe(struct device *dev, struct resource *res)
 		desc.type = CORESIGHT_DEV_TYPE_SINK;
 		desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_SYSMEM;
 		desc.ops = &tmc_etr_cs_ops;
-		ret = tmc_etr_setup_caps(dev, devid, &desc.access);
+		ret = tmc_etr_setup_caps(dev, &desc.access);
+		if (ret)
+			goto out;
+		ret = tmc_etr_probe_common(dev, drvdata, devid);
 		if (ret)
 			goto out;
-		idr_init(&drvdata->idr);
-		mutex_init(&drvdata->idr_mutex);
 		dev_list = "tmc_etr";
 		break;
 	case TMC_CONFIG_TYPE_ETF:

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 4/8] coresight: tmc: move trigger_cntr into its own attribute group
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
                   ` (2 preceding siblings ...)
  2026-11-30  7:11 ` [PATCH v2 3/8] coresight: tmc: factor out common ETR probe initialisation Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:11 ` [PATCH v2 5/8] dt-bindings: arm: qcom: add QECP owned CoreSight STM Jie Gan
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

The trigger_cntr attribute sets the value programmed into the TMC_TRG
register when the TMC is enabled, while buffer_size and stop_on_flush
are consumed when the trace buffer and the formatter configuration
are set up.

Move trigger_cntr into a separate unnamed attribute group and add it
to both the ETB/ETF and the ETR group lists, so that a TMC driver which
does not program TMC_TRG can expose the remaining attributes without it.

The sysfs layout of existing TMC devices is unchanged.

Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
 drivers/hwtracing/coresight/coresight-tmc-core.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
index ad2c99eae5ae..2361bb3d652f 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-core.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
@@ -559,7 +559,6 @@ static DEVICE_ATTR_RW(stop_on_flush);
 
 
 static struct attribute *coresight_tmc_attrs[] = {
-	&dev_attr_trigger_cntr.attr,
 	&dev_attr_buffer_size.attr,
 	&dev_attr_stop_on_flush.attr,
 	NULL,
@@ -569,6 +568,16 @@ static const struct attribute_group coresight_tmc_group = {
 	.attrs = coresight_tmc_attrs,
 };
 
+/* trigger_cntr is programmed directly into the TMC_TRG register */
+static struct attribute *coresight_tmc_trg_attrs[] = {
+	&dev_attr_trigger_cntr.attr,
+	NULL,
+};
+
+static const struct attribute_group coresight_tmc_trg_group = {
+	.attrs = coresight_tmc_trg_attrs,
+};
+
 static const struct attribute_group coresight_tmc_mgmt_group = {
 	.attrs = coresight_tmc_mgmt_attrs,
 	.name = "mgmt",
@@ -576,6 +585,7 @@ static const struct attribute_group coresight_tmc_mgmt_group = {
 
 static const struct attribute_group *coresight_etf_groups[] = {
 	&coresight_tmc_group,
+	&coresight_tmc_trg_group,
 	&coresight_tmc_mgmt_group,
 	NULL,
 };
@@ -583,6 +593,7 @@ static const struct attribute_group *coresight_etf_groups[] = {
 static const struct attribute_group *coresight_etr_groups[] = {
 	&coresight_etr_group,
 	&coresight_tmc_group,
+	&coresight_tmc_trg_group,
 	&coresight_tmc_mgmt_group,
 	NULL,
 };

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 5/8] dt-bindings: arm: qcom: add QECP owned CoreSight STM
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
                   ` (3 preceding siblings ...)
  2026-11-30  7:11 ` [PATCH v2 4/8] coresight: tmc: move trigger_cntr into its own attribute group Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:11 ` [PATCH v2 6/8] coresight: qcom: add QECP owned STM driver Jie Gan
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

On the Qualcomm Hawi and Maili SoCs, a CoreSight System Trace Macrocell
(STM) is owned by the Qualcomm Embedded Co-Processor (QECP). Its control
registers are only accessible from the QECP, which configures, enables
and disables the STM on request of the operating system. The stimulus
ports remain accessible to the operating system for writing trace data.

Add a binding for this STM. Both the control register region and the
stimulus port region are described, as for the generic CoreSight STM.
The control registers are only accessible from the QECP, whose firmware
identifies the STM by their base address. The firmware service used to
control the STM is fixed for each SoC, so it is implied by the SoC
specific compatible rather than described in the devicetree.

The Maili STM is compatible with the Hawi one and uses the Hawi
compatible as fallback.

Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
 .../bindings/arm/qcom,coresight-qecp-stm.yaml      | 76 ++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/Documentation/devicetree/bindings/arm/qcom,coresight-qecp-stm.yaml b/Documentation/devicetree/bindings/arm/qcom,coresight-qecp-stm.yaml
new file mode 100644
index 000000000000..1461ee294739
--- /dev/null
+++ b/Documentation/devicetree/bindings/arm/qcom,coresight-qecp-stm.yaml
@@ -0,0 +1,76 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/arm/qcom,coresight-qecp-stm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Qualcomm QECP owned CoreSight STM
+
+maintainers:
+  - Jie Gan <jie.gan@oss.qualcomm.com>
+
+description: |
+  The System Trace Macrocell (STM) is a CoreSight trace source for software
+  instrumentation, made up of memory mapped writes to its stimulus ports.
+
+  On some Qualcomm SoCs the STM is owned by the Qualcomm Embedded
+  Co-Processor (QECP). Its control registers are only accessible from the
+  QECP, which configures, enables and disables the STM on request of the
+  operating system. The stimulus ports remain accessible to the operating
+  system for writing trace data.
+
+properties:
+  compatible:
+    oneOf:
+      - const: qcom,hawi-qecp-stm
+      - items:
+          - enum:
+              - qcom,maili-qecp-stm
+          - const: qcom,hawi-qecp-stm
+
+  reg:
+    items:
+      - description:
+          STM control register region. The registers are only accessible
+          from the QECP.
+      - description: STM stimulus port region.
+
+  reg-names:
+    items:
+      - const: stm-base
+      - const: stm-stimulus-base
+
+  out-ports:
+    $ref: /schemas/graph.yaml#/properties/ports
+    additionalProperties: false
+
+    properties:
+      port:
+        description: Output connection to the CoreSight trace bus.
+        $ref: /schemas/graph.yaml#/properties/port
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - out-ports
+
+additionalProperties: false
+
+examples:
+  - |
+    stm@10002000 {
+        compatible = "qcom,hawi-qecp-stm";
+        reg = <0x10002000 0x1000>,
+              <0x16280000 0x180000>;
+        reg-names = "stm-base", "stm-stimulus-base";
+
+        out-ports {
+            port {
+                endpoint {
+                    remote-endpoint = <&etr_in_stm>;
+                };
+            };
+        };
+    };
+...

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 6/8] coresight: qcom: add QECP owned STM driver
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
                   ` (4 preceding siblings ...)
  2026-11-30  7:11 ` [PATCH v2 5/8] dt-bindings: arm: qcom: add QECP owned CoreSight STM Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:11 ` [PATCH v2 7/8] dt-bindings: arm: qcom: add QECP owned CoreSight TMC Jie Gan
  2026-11-30  7:11 ` [PATCH v2 8/8] coresight: tmc: add QECP owned TMC driver Jie Gan
  7 siblings, 0 replies; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

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@oss.qualcomm.com>
---
 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@oss.qualcomm.com>");
+MODULE_LICENSE("GPL");

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 7/8] dt-bindings: arm: qcom: add QECP owned CoreSight TMC
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
                   ` (5 preceding siblings ...)
  2026-11-30  7:11 ` [PATCH v2 6/8] coresight: qcom: add QECP owned STM driver Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:11 ` [PATCH v2 8/8] coresight: tmc: add QECP owned TMC driver Jie Gan
  7 siblings, 0 replies; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

On the Qualcomm Hawi and Maili SoCs, a CoreSight TMC is owned by the
Qualcomm Embedded Co-Processor (QECP). Its registers are only accessible
from the QECP, which configures, enables and disables the TMC on request
of the operating system. For a TMC configured as an ETR, the trace
buffer is provided by the operating system.

Add a binding for this TMC. The register region is described as for any
other TMC, even though it is only accessible from the QECP, whose
firmware identifies the TMC by its base address. The firmware service
used to control the TMC is fixed for each SoC, so it is implied by the
SoC specific compatible rather than described in the devicetree.

The Maili TMC is compatible with the Hawi one and uses the Hawi
compatible as fallback.

Signed-off-by: Jie Gan <jie.gan@oss.qualcomm.com>
---
 .../bindings/arm/qcom,coresight-qecp-tmc.yaml      | 83 ++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/Documentation/devicetree/bindings/arm/qcom,coresight-qecp-tmc.yaml b/Documentation/devicetree/bindings/arm/qcom,coresight-qecp-tmc.yaml
new file mode 100644
index 000000000000..4d353c6cbaee
--- /dev/null
+++ b/Documentation/devicetree/bindings/arm/qcom,coresight-qecp-tmc.yaml
@@ -0,0 +1,83 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/arm/qcom,coresight-qecp-tmc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Qualcomm QECP owned CoreSight TMC
+
+maintainers:
+  - Jie Gan <jie.gan@oss.qualcomm.com>
+
+description: |
+  The CoreSight Trace Memory Controller (TMC) can be configured as an
+  Embedded Trace Router (ETR), writing trace data into a buffer in system
+  memory, or as an Embedded Trace FIFO (ETF) or Buffer (ETB).
+
+  On some Qualcomm SoCs a TMC is owned by the Qualcomm Embedded
+  Co-Processor (QECP). Its registers are only accessible from the QECP,
+  which configures, enables and disables the TMC on request of the
+  operating system. For an ETR, the trace buffer is provided by the
+  operating system.
+
+properties:
+  compatible:
+    oneOf:
+      - const: qcom,hawi-qecp-tmc
+      - items:
+          - enum:
+              - qcom,maili-qecp-tmc
+          - const: qcom,hawi-qecp-tmc
+
+  reg:
+    description:
+      TMC register region. The registers are only accessible from the QECP.
+    maxItems: 1
+
+  iommus:
+    maxItems: 1
+
+  arm,buffer-size:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      Size of the contiguous buffer space for a TMC configured as an ETR,
+      in bytes.
+
+  arm,scatter-gather:
+    type: boolean
+    description:
+      The TMC ETR can safely use its scatter-gather mode on this system.
+
+  in-ports:
+    $ref: /schemas/graph.yaml#/properties/ports
+    additionalProperties: false
+
+    properties:
+      port:
+        description: Input connection from the CoreSight trace bus.
+        $ref: /schemas/graph.yaml#/properties/port
+
+required:
+  - compatible
+  - reg
+  - in-ports
+
+additionalProperties: false
+
+examples:
+  - |
+    etr@10048000 {
+        compatible = "qcom,hawi-qecp-tmc";
+        reg = <0x10048000 0x1000>;
+        iommus = <&apps_smmu 0x04c0 0x0>;
+        arm,scatter-gather;
+
+        in-ports {
+            port {
+                endpoint {
+                    remote-endpoint = <&stm_out_etr>;
+                };
+            };
+        };
+    };
+...

-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 8/8] coresight: tmc: add QECP owned TMC driver
  2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
                   ` (6 preceding siblings ...)
  2026-11-30  7:11 ` [PATCH v2 7/8] dt-bindings: arm: qcom: add QECP owned CoreSight TMC Jie Gan
@ 2026-11-30  7:11 ` Jie Gan
  2026-11-30  7:33   ` sashiko-bot
  7 siblings, 1 reply; 11+ messages in thread
From: Jie Gan @ 2026-11-30  7:11 UTC (permalink / raw)
  To: Bjorn Andersson, Konrad Dybcio, Abel Vesa, Suzuki K Poulose,
	Mike Leach, James Clark, Leo Yan, Alexander Shishkin, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
	Alexandre Torgue, Tingwei Zhang
  Cc: linux-kernel, linux-arm-msm, coresight, linux-arm-kernel,
	devicetree, linux-stm32, Jie Gan

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@oss.qualcomm.com>
---
 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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 3/8] coresight: tmc: factor out common ETR probe initialisation
  2026-11-30  7:11 ` [PATCH v2 3/8] coresight: tmc: factor out common ETR probe initialisation Jie Gan
@ 2026-11-30  7:20   ` sashiko-bot
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-30  7:20 UTC (permalink / raw)
  To: Jie Gan; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] coresight: tmc: invalid csdev access and resource leaks in
  __tmc_probe() [1]
--

commit 287401d1d1c361e6772a54be5797715a0d04a2cd
Author: Jie Gan <jie.gan@oss.qualcomm.com>

coresight: tmc: factor out common ETR probe initialisation

This commit moves the ETR probe setup derived from DEVID and firmware
properties into tmc_etr_probe_common(), leaving tmc_etr_setup_caps() with
the register-based checks.

> diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
> index 0ef4b817b297d..ad2c99eae5aed 100644
> --- a/drivers/hwtracing/coresight/coresight-tmc-core.c
> +++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
[ ... ]
> @@ -829,11 +847,12 @@ static int __tmc_probe(struct device *dev, struct resource *res)
>  		desc.type = CORESIGHT_DEV_TYPE_SINK;
>  		desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_SYSMEM;
>  		desc.ops = &tmc_etr_cs_ops;
> -		ret = tmc_etr_setup_caps(dev, devid, &desc.access);
> +		ret = tmc_etr_setup_caps(dev, &desc.access);
> +		if (ret)
> +			goto out;
> +		ret = tmc_etr_probe_common(dev, drvdata, devid);
>  		if (ret)
>  			goto out;
> -		idr_init(&drvdata->idr);
> -		mutex_init(&drvdata->idr_mutex);
>  		dev_list = "tmc_etr";
>  		break;
>  	case TMC_CONFIG_TYPE_ETF:

[Severity: High]
This problem wasn't introduced by this patch, but can jumping to out
after tmc_get_reserved_region() dereference an invalid drvdata->csdev
[1]?

drivers/hwtracing/coresight/coresight-tmc-core.c:__tmc_probe() {
    ...
out:
	if (is_tmc_crashdata_valid(drvdata) &&
	    !tmc_prepare_crashdata(drvdata))
		register_crash_dev_interface(drvdata, desc.name);
	return ret;
}

When probe fails after tmc_get_reserved_region() maps valid crash trace
data, the out path runs is_tmc_crashdata_valid(), tmc_prepare_crashdata(),
and register_crash_dev_interface().  All three dereference
&drvdata->csdev->dev while drvdata->csdev is still NULL, an ERR_PTR()
from coresight_register(), or already freed by coresight_unregister().

If register_crash_dev_interface() succeeds on an error path,
misc_register() also links drvdata->crashdev into misc_list right before
devres frees drvdata.  In addition, the unmanaged memremap() mappings in
drvdata->resrv_buf.vaddr and drvdata->crash_mdata.vaddr are never
unmapped on probe failure or in __tmc_remove().

[1] https://sashiko.dev/#/bug/linux-f23df7b7-bae5-4fa0-96ef-4e83632058e8

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-introduce-qcom-qecp-feature-v2-0-dff9ae87327b@oss.qualcomm.com?part=3

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 8/8] coresight: tmc: add QECP owned TMC driver
  2026-11-30  7:11 ` [PATCH v2 8/8] coresight: tmc: add QECP owned TMC driver Jie Gan
@ 2026-11-30  7:33   ` sashiko-bot
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-30  7:33 UTC (permalink / raw)
  To: Jie Gan; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] coresight: tmc-qdcp: refcnt leak and ETR resume in
  tmc_qdcp_etr_disable_sink()
- [Medium] coresight: tmc-qdcp: missing RWP >= RRP check in
  tmc_qdcp_etr_hw_state_valid()
--

commit 568910aefc42f8c1badeac3b8f837013fcf6546b
Author: Jie Gan <jie.gan@oss.qualcomm.com>

coresight: tmc: add QECP owned TMC driver

This commit adds a driver for CoreSight TMC ETR devices managed by the
Qualcomm Embedded Co-Processor (QECP) firmware over the Qualcomm Debug
Control Protocol (QDCP), and exports common TMC ETR helpers for it.

> diff --git a/drivers/hwtracing/coresight/coresight-tmc-qdcp.c b/drivers/hwtracing/coresight/coresight-tmc-qdcp.c
> new file mode 100644
> index 0000000000000..0c73d2834be28
> --- /dev/null
> +++ b/drivers/hwtracing/coresight/coresight-tmc-qdcp.c

[ ... ]

> +/*
> + * 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;
> +}

[Severity: Medium]
Can tmc_qdcp_etr_hw_state_valid() accept state->rwp < state->rrp when
TMC_STS_FULL is not set in state->sts?

If the firmware returns start <= state->rwp < state->rrp < end without
TMC_STS_FULL, tmc_etr_sync_flat_buf() computes a negative etr_buf->len,
passes the wrap check because etr_buf->offset + etr_buf->len equals
rwp - etr_buf->hwaddr, and calls dma_sync_single_for_cpu() with a huge
size_t:

drivers/hwtracing/coresight/coresight-tmc-etr.c:tmc_etr_sync_flat_buf() {
	...
	etr_buf->offset = rrp - etr_buf->hwaddr;
	if (etr_buf->full)
		etr_buf->len = etr_buf->size;
	else
		etr_buf->len = rwp - rrp;

	if (etr_buf->offset + etr_buf->len > etr_buf->size)
		dma_sync_single_for_cpu(real_dev, flat_buf->daddr,
					etr_buf->size, DMA_FROM_DEVICE);
	else
		dma_sync_single_for_cpu(real_dev,
					flat_buf->daddr + etr_buf->offset,
					etr_buf->len, DMA_FROM_DEVICE);
}

[ ... ]

> +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;
> +}

[Severity: High]
Does returning -EBUSY when drvdata->reading is true leak csdev->refcnt and
leave csdev->mode set to CS_MODE_SYSFS?

When a trace source is disabled while the miscdevice is open for reading,
coresight_disable_sysfs() ignores the return value from sink_ops->disable()
and tears down the path.  Because tmc_qdcp_etr_disable_sink() returns before
decrementing csdev->refcnt or setting csdev->mode to CS_MODE_DISABLED, closing
the miscdevice in tmc_qdcp_etr_read_unprepare() sees CS_MODE_SYSFS and
re-enables the ETR in firmware with drvdata->sysfs_buf instead of freeing the
buffer.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-introduce-qcom-qecp-feature-v2-0-dff9ae87327b@oss.qualcomm.com?part=8

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-30  7:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  7:11 [PATCH v2 0/8] Coresight: Introduce Qualcomm QDCP support for QECP-owned trace blocks Jie Gan
2026-11-30  7:11 ` [PATCH v2 1/8] soc: qcom: add QDCP client library Jie Gan
2026-11-30  7:11 ` [PATCH v2 2/8] coresight: tmc: split ETR buffer sync from hardware state readback Jie Gan
2026-11-30  7:11 ` [PATCH v2 3/8] coresight: tmc: factor out common ETR probe initialisation Jie Gan
2026-11-30  7:20   ` sashiko-bot
2026-11-30  7:11 ` [PATCH v2 4/8] coresight: tmc: move trigger_cntr into its own attribute group Jie Gan
2026-11-30  7:11 ` [PATCH v2 5/8] dt-bindings: arm: qcom: add QECP owned CoreSight STM Jie Gan
2026-11-30  7:11 ` [PATCH v2 6/8] coresight: qcom: add QECP owned STM driver Jie Gan
2026-11-30  7:11 ` [PATCH v2 7/8] dt-bindings: arm: qcom: add QECP owned CoreSight TMC Jie Gan
2026-11-30  7:11 ` [PATCH v2 8/8] coresight: tmc: add QECP owned TMC driver Jie Gan
2026-11-30  7:33   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox