[PATCH 08/10] gtrace: rvtrace: Introduce struct rvtrace_ramsink_regs to abstract register offsets

From: Leo Yu-Chi Liang

Date: Thu Nov 26 2026 - 04:07:58 EST


From: Eric Lin <eric.lin@xxxxxxxxxx>

The pre-ratified trace ramsink implementation uses different register
offsets compared to the ratified RISC-V trace specification. To allow
the pre-ratified trace ramsink to leverage the rvtrace ramsink setup
and copy functions, introduce 'struct rvtrace_ramsink_regs'. This
abstracts the register map, enabling both pre-ratified and ratified
trace hardware to provide their own register layouts.

The read/write pair helpers no longer assume that the high register
is located right after the low register, and take both offsets.

Co-developed-by: Nick Hu <nick.hu@xxxxxxxxxx>
Signed-off-by: Nick Hu <nick.hu@xxxxxxxxxx>
Co-developed-by: Vincent Chen <vincent.chen@xxxxxxxxxx>
Signed-off-by: Vincent Chen <vincent.chen@xxxxxxxxxx>
Signed-off-by: Eric Lin <eric.lin@xxxxxxxxxx>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@xxxxxxxxxx>
---
drivers/hwtracing/gtrace/rvtrace-ramsink.c | 69 ++++++++++++++++++++++++------
1 file changed, 56 insertions(+), 13 deletions(-)

diff --git a/drivers/hwtracing/gtrace/rvtrace-ramsink.c b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
index 52aeb7626a55..a164db57167f 100644
--- a/drivers/hwtracing/gtrace/rvtrace-ramsink.c
+++ b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
@@ -15,8 +15,11 @@
#include "rvtrace.h"

#define RVTRACE_RAMSINK_STARTLOW_OFF 0x010
+#define RVTRACE_RAMSINK_STARTHIGH_OFF 0x014
#define RVTRACE_RAMSINK_LIMITLOW_OFF 0x018
+#define RVTRACE_RAMSINK_LIMITHIGH_OFF 0x01c
#define RVTRACE_RAMSINK_WPLOW_OFF 0x020
+#define RVTRACE_RAMSINK_WPHIGH_OFF 0x024
#define RVTRACE_RAMSINK_WPLOW_WRAP 0x1
#define RVTRACE_RAMSINK_CTRL_MODE_MASK 0x1
#define RVTRACE_RAMSINK_CTRL_MODE_SHIFT 0x4
@@ -33,7 +36,39 @@ enum rvtrace_ramsink_mode {
MODE_SMEM
};

+/**
+ * struct rvtrace_ramsink_regs - Register offsets for RAM sink implementations
+ * @start_low: Offset of the start address low register
+ * @start_high: Offset of the start address high register
+ * @limit_low: Offset of the limit address low register
+ * @limit_high: Offset of the limit address high register
+ * @wp_low: Offset of the write pointer low register
+ * @wp_high: Offset of the write pointer high register
+ *
+ * Different RAM sink implementations may have different register layouts.
+ * This structure allows the common code to work with any layout.
+ */
+struct rvtrace_ramsink_regs {
+ u32 start_low;
+ u32 start_high;
+ u32 limit_low;
+ u32 limit_high;
+ u32 wp_low;
+ u32 wp_high;
+};
+
+/* Register offsets for the standard RISC-V trace ramsink */
+static const struct rvtrace_ramsink_regs rvtrace_std_ramsink_regs = {
+ .start_low = RVTRACE_RAMSINK_STARTLOW_OFF,
+ .start_high = RVTRACE_RAMSINK_STARTHIGH_OFF,
+ .limit_low = RVTRACE_RAMSINK_LIMITLOW_OFF,
+ .limit_high = RVTRACE_RAMSINK_LIMITHIGH_OFF,
+ .wp_low = RVTRACE_RAMSINK_WPLOW_OFF,
+ .wp_high = RVTRACE_RAMSINK_WPHIGH_OFF,
+};
+
struct rvtrace_ramsink_priv {
+ const struct rvtrace_ramsink_regs *regs;
size_t size;
void *va;
dma_addr_t start;
@@ -56,20 +91,22 @@ struct trace_buf {
};

/* Helper to access start, limit, wp pair registers */
-static u64 rvtrace_ramsink_read_pair(struct gtrace_platform_data *pdata, u32 low_off)
+static u64 rvtrace_ramsink_read_pair(struct gtrace_platform_data *pdata,
+ u32 low_off, u32 high_off)
{
u32 low, high;

low = gtrace_read32(pdata, low_off);
- high = gtrace_read32(pdata, low_off + 4);
+ high = gtrace_read32(pdata, high_off);

return (u64)high << 32 | low;
}

-static void rvtrace_ramsink_write_pair(struct gtrace_platform_data *pdata, u64 val, u32 low_off)
+static void rvtrace_ramsink_write_pair(struct gtrace_platform_data *pdata, u64 val,
+ u32 low_off, u32 high_off)
{
gtrace_write32(pdata, lower_32_bits(val), low_off);
- gtrace_write32(pdata, upper_32_bits(val), low_off + 4);
+ gtrace_write32(pdata, upper_32_bits(val), high_off);
}

static int rvtrace_ramsink_start(struct gtrace_path_node *node)
@@ -152,6 +189,7 @@ static size_t rvtrace_ramsink_copyto_auxbuf(struct gtrace_component *comp,
u64 *format)
{
struct rvtrace_ramsink_priv *priv = dev_get_drvdata(&comp->dev);
+ const struct rvtrace_ramsink_regs *regs = priv->regs;
struct trace_buf src, dst;
size_t bytes = 0;
bool wrap;
@@ -163,12 +201,13 @@ static size_t rvtrace_ramsink_copyto_auxbuf(struct gtrace_component *comp,
src.base = priv->va;
src.len = priv->size;
*format = PERF_AUX_FLAG_GTRACE_FORMAT_ETRACE;
- wp = rvtrace_ramsink_read_pair(comp->pdata, RVTRACE_RAMSINK_WPLOW_OFF);
+ wp = rvtrace_ramsink_read_pair(comp->pdata, regs->wp_low, regs->wp_high);
wrap = wp & RVTRACE_RAMSINK_WPLOW_WRAP;
wp &= ~RVTRACE_RAMSINK_WPLOW_WRAP;

if (wrap) {
- rvtrace_ramsink_write_pair(comp->pdata, priv->start, RVTRACE_RAMSINK_WPLOW_OFF);
+ rvtrace_ramsink_write_pair(comp->pdata, priv->start,
+ regs->wp_low, regs->wp_high);
src.cur = wp - priv->start;
priv->prev_wp = priv->start;
/*
@@ -191,13 +230,14 @@ static size_t rvtrace_ramsink_copyto_auxbuf(struct gtrace_component *comp,
static int rvtrace_ramsink_setup_buf(struct gtrace_component *comp,
struct rvtrace_ramsink_priv *priv)
{
+ const struct rvtrace_ramsink_regs *regs = priv->regs;
struct device *pdev = comp->pdata->dev;
u64 limit_max, end;
int ret;

/* Probe max value for limit register */
- rvtrace_ramsink_write_pair(comp->pdata, U64_MAX, RVTRACE_RAMSINK_LIMITLOW_OFF);
- limit_max = rvtrace_ramsink_read_pair(comp->pdata, RVTRACE_RAMSINK_LIMITLOW_OFF);
+ rvtrace_ramsink_write_pair(comp->pdata, U64_MAX, regs->limit_low, regs->limit_high);
+ limit_max = rvtrace_ramsink_read_pair(comp->pdata, regs->limit_low, regs->limit_high);
if (!limit_max) {
dev_err(&comp->dev, "trRamLimit register not implemented\n");
return -EINVAL;
@@ -218,13 +258,14 @@ static int rvtrace_ramsink_setup_buf(struct gtrace_component *comp,
* Check if DMA start and end addresses allocated by the kernel can be set in the ramsink
* start, limit registers respectively.
*/
- rvtrace_ramsink_write_pair(comp->pdata, priv->alloc_start, RVTRACE_RAMSINK_STARTLOW_OFF);
+ rvtrace_ramsink_write_pair(comp->pdata, priv->alloc_start,
+ regs->start_low, regs->start_high);
/* Limit address needs to be set to end - mem_access_width to avoid overflow */
end = priv->alloc_start + priv->alloc_size - priv->mem_acc_width;
- rvtrace_ramsink_write_pair(comp->pdata, end, RVTRACE_RAMSINK_LIMITLOW_OFF);
+ rvtrace_ramsink_write_pair(comp->pdata, end, regs->limit_low, regs->limit_high);

- priv->start = rvtrace_ramsink_read_pair(comp->pdata, RVTRACE_RAMSINK_STARTLOW_OFF);
- priv->end = rvtrace_ramsink_read_pair(comp->pdata, RVTRACE_RAMSINK_LIMITLOW_OFF) +
+ priv->start = rvtrace_ramsink_read_pair(comp->pdata, regs->start_low, regs->start_high);
+ priv->end = rvtrace_ramsink_read_pair(comp->pdata, regs->limit_low, regs->limit_high) +
priv->mem_acc_width;

/* Sanity check start, end addresses */
@@ -245,7 +286,7 @@ static int rvtrace_ramsink_setup_buf(struct gtrace_component *comp,
priv->va = priv->alloc_va + (priv->start - priv->alloc_start);
priv->size = priv->end - priv->start;
priv->prev_wp = priv->start;
- rvtrace_ramsink_write_pair(comp->pdata, priv->start, RVTRACE_RAMSINK_WPLOW_OFF);
+ rvtrace_ramsink_write_pair(comp->pdata, priv->start, regs->wp_low, regs->wp_high);

return 0;

@@ -275,6 +316,8 @@ static int rvtrace_ramsink_setup(struct gtrace_component *comp)
break;
}

+ priv->regs = &rvtrace_std_ramsink_regs;
+
if (priv->mode != MODE_SMEM) {
dev_err(&comp->dev, "only smem mode is supported\n");
return -EOPNOTSUPP;

--
2.43.7