[PATCH v2 25/28] coresight: trbe: Save and restore state across CPU low power state
From: Leo Yan
Date: Tue Jul 01 2025 - 11:01:26 EST
From: Yabin Cui <yabinc@xxxxxxxxxx>
Similar to ETE, TRBE may lose its context when a CPU enters low power
state. To make things worse, if ETE is restored without TRBE being
restored, an enabled source device with no enabled sink devices can
cause CPU hang on some devices (e.g., Pixel 9).
The save and restore flows are described in the section K5.5 "Context
switching" of Arm ARM (ARM DDI 0487 L.a). This commit adds save and
restore callbacks with following the software usages defined in the
architecture manual.
Signed-off-by: Yabin Cui <yabinc@xxxxxxxxxx>
Co-developed-by: Leo Yan <leo.yan@xxxxxxx>
Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
---
drivers/hwtracing/coresight/coresight-trbe.c | 84 ++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
diff --git a/drivers/hwtracing/coresight/coresight-trbe.c b/drivers/hwtracing/coresight/coresight-trbe.c
index 28450447693147b9afd207679832ac83e2a5848a..7f3d233f70b33058bd235ea0b798ce3884788856 100644
--- a/drivers/hwtracing/coresight/coresight-trbe.c
+++ b/drivers/hwtracing/coresight/coresight-trbe.c
@@ -115,6 +115,20 @@ static int trbe_errata_cpucaps[] = {
*/
#define TRBE_WORKAROUND_OVERWRITE_FILL_MODE_SKIP_BYTES 256
+/*
+ * struct trbe_save_state: Register values representing TRBE state
+ * @trblimitr - Trace Buffer Limit Address Register value
+ * @trbbaser - Trace Buffer Base Register value
+ * @trbptr - Trace Buffer Write Pointer Register value
+ * @trbsr - Trace Buffer Status Register value
+ */
+struct trbe_save_state {
+ u64 trblimitr;
+ u64 trbbaser;
+ u64 trbptr;
+ u64 trbsr;
+};
+
/*
* struct trbe_cpudata: TRBE instance specific data
* @trbe_flag - TRBE dirty/access flag support
@@ -133,6 +147,7 @@ struct trbe_cpudata {
enum cs_mode mode;
struct trbe_buf *buf;
struct trbe_drvdata *drvdata;
+ struct trbe_save_state save_state;
DECLARE_BITMAP(errata, TRBE_ERRATA_MAX);
};
@@ -1187,12 +1202,81 @@ static irqreturn_t arm_trbe_irq_handler(int irq, void *dev)
return IRQ_HANDLED;
}
+static int arm_trbe_save(struct coresight_device *csdev)
+{
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct trbe_save_state *state = &cpudata->save_state;
+
+ if (cpudata->mode == CS_MODE_DISABLED)
+ return 0;
+
+ /*
+ * According to the section K5.5 Context switching, Arm ARM (ARM DDI
+ * 0487 L.a), the software usage VKHHY requires a TSB CSYNC instruction
+ * to ensure the program-flow trace is flushed, which has been executed
+ * in ETM driver.
+ */
+
+ /* Disable trace buffer unit */
+ state->trblimitr = read_sysreg_s(SYS_TRBLIMITR_EL1);
+ write_sysreg_s(state->trblimitr & ~TRBLIMITR_EL1_E, SYS_TRBLIMITR_EL1);
+
+ /*
+ * Execute a further Context synchronization event. Ensure the writes to
+ * memory are complete.
+ */
+ trbe_drain_buffer();
+
+ /* Synchronize the TRBE disabling */
+ isb();
+
+ state->trbbaser = read_sysreg_s(SYS_TRBBASER_EL1);
+ state->trbptr = read_sysreg_s(SYS_TRBPTR_EL1);
+ state->trbsr = read_sysreg_s(SYS_TRBSR_EL1);
+ return 0;
+}
+
+static int arm_trbe_restore(struct coresight_device *csdev)
+{
+ struct trbe_cpudata *cpudata = dev_get_drvdata(&csdev->dev);
+ struct trbe_save_state *state = &cpudata->save_state;
+
+ if (cpudata->mode == CS_MODE_DISABLED)
+ return 0;
+
+ write_sysreg_s(state->trbbaser, SYS_TRBBASER_EL1);
+ write_sysreg_s(state->trbptr, SYS_TRBPTR_EL1);
+ write_sysreg_s(state->trbsr, SYS_TRBSR_EL1);
+ write_sysreg_s(state->trblimitr & ~TRBLIMITR_EL1_E, SYS_TRBLIMITR_EL1);
+
+ /*
+ * According to the section K5.5 Context switching, Arm ARM (ARM DDI
+ * 0487 L.a), the software usage PKLXF requires a Context
+ * synchronization event to guarantee the Trace Buffer Unit will observe
+ * the new values of the System registers.
+ */
+ isb();
+
+ /* Enable the Trace Buffer Unit */
+ write_sysreg_s(state->trblimitr, SYS_TRBLIMITR_EL1);
+
+ /* Synchronize the TRBE enable event */
+ isb();
+
+ if (trbe_needs_ctxt_sync_after_enable(cpudata))
+ isb();
+
+ return 0;
+}
+
static const struct coresight_ops_sink arm_trbe_sink_ops = {
.enable = arm_trbe_enable,
.disable = arm_trbe_disable,
.alloc_buffer = arm_trbe_alloc_buffer,
.free_buffer = arm_trbe_free_buffer,
.update_buffer = arm_trbe_update_buffer,
+ .save = arm_trbe_save,
+ .restore = arm_trbe_restore,
};
static const struct coresight_ops arm_trbe_cs_ops = {
--
2.34.1