[PATCH v2 2/8] coresight: tmc: split ETR buffer sync from hardware state readback
From: Jie Gan
Date: Mon Nov 30 2026 - 02:12:30 EST
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@xxxxxxxxxxxxxxxx>
---
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