[PATCH 02/10] gtrace: rvtrace: Add pre-ratified implementation callback
From: Leo Yu-Chi Liang
Date: Thu Nov 26 2026 - 04:07:42 EST
From: Eric Lin <eric.lin@xxxxxxxxxx>
For pre-ratified trace hardware, the implementation register
bitfields differ from the ratified RISC-V trace specification,
and the component type is not defined in the hardware
implementation register.
To address this, add a get_impl() callback function in the
driver data to translate the pre-ratified trace hardware
implementation register information and encode it into the
standard RISC-V trace implementation register format.
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/Kconfig | 12 ++++++++++
drivers/hwtracing/gtrace/Makefile | 4 +++-
drivers/hwtracing/gtrace/rvtrace-platform.c | 36 ++++++++++++++++++++++++++---
drivers/hwtracing/gtrace/rvtrace-v0.c | 34 +++++++++++++++++++++++++++
drivers/hwtracing/gtrace/rvtrace-v0.h | 17 ++++++++++++++
drivers/hwtracing/gtrace/rvtrace.h | 12 ++++++++++
6 files changed, 111 insertions(+), 4 deletions(-)
diff --git a/drivers/hwtracing/gtrace/Kconfig b/drivers/hwtracing/gtrace/Kconfig
index e5b62d65e2fd..b1f9ca8f73cb 100644
--- a/drivers/hwtracing/gtrace/Kconfig
+++ b/drivers/hwtracing/gtrace/Kconfig
@@ -57,3 +57,15 @@ config RVTRACE_RAMSINK
To compile this driver as a module, choose M here: the module
will be called rvtrace-ramsink.
+
+config RVTRACE_V0
+ bool "RISC-V pre-ratified trace support"
+ depends on RVTRACE
+ select RVTRACE_ENCODER
+ select RVTRACE_RAMSINK
+ default y
+ help
+ This provides support for pre-ratified (version 0.x) RISC-V trace
+ hardware, such as the SiFive trace encoder and trace funnel, whose
+ register layout differs from the RISC-V Trace Control Interface
+ specification.
diff --git a/drivers/hwtracing/gtrace/Makefile b/drivers/hwtracing/gtrace/Makefile
index 33274a1ed0b8..0a2e2d1732c2 100644
--- a/drivers/hwtracing/gtrace/Makefile
+++ b/drivers/hwtracing/gtrace/Makefile
@@ -4,6 +4,8 @@ obj-$(CONFIG_GTRACE) += gtrace.o
gtrace-y := gtrace-core.o gtrace-perf.o
gtrace-$(CONFIG_OF) += gtrace-of.o
-obj-$(CONFIG_RVTRACE) += rvtrace-platform.o
+obj-$(CONFIG_RVTRACE) += rvtrace.o
+rvtrace-y := rvtrace-platform.o
+rvtrace-$(CONFIG_RVTRACE_V0) += rvtrace-v0.o
obj-$(CONFIG_RVTRACE_ENCODER) += rvtrace-encoder.o
obj-$(CONFIG_RVTRACE_RAMSINK) += rvtrace-ramsink.o
diff --git a/drivers/hwtracing/gtrace/rvtrace-platform.c b/drivers/hwtracing/gtrace/rvtrace-platform.c
index a5655e983e51..8c1503bd8d98 100644
--- a/drivers/hwtracing/gtrace/rvtrace-platform.c
+++ b/drivers/hwtracing/gtrace/rvtrace-platform.c
@@ -11,6 +11,7 @@
#include <linux/property.h>
#include <linux/types.h>
#include "rvtrace.h"
+#include "rvtrace-v0.h"
static int rvtrace_hw_enable(struct gtrace_platform_data *pdata)
{
@@ -62,6 +63,7 @@ const struct gtrace_hw_ops rvtrace_hw_ops = {
static int rvtrace_platform_probe(struct platform_device *pdev)
{
+ const struct rvtrace_match_data *match_data;
struct gtrace_platform_data *pdata;
struct device *dev = &pdev->dev;
struct gtrace_component_id id;
@@ -72,13 +74,17 @@ static int rvtrace_platform_probe(struct platform_device *pdev)
int gtype;
int ret;
+ match_data = device_get_match_data(dev);
+ if (!match_data)
+ return -ENODEV;
+
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata)
return -ENOMEM;
pdata->dev = dev;
pdata->impid = RVTRACE_COMPONENT_IMPID_UNKNOWN;
pdata->hw_ops = &rvtrace_hw_ops;
- pdata->format = (uintptr_t)device_get_match_data(dev);
+ pdata->format = match_data->format;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res)
@@ -115,7 +121,11 @@ static int rvtrace_platform_probe(struct platform_device *pdev)
if (ret)
return dev_err_probe(dev, ret, "failed to reset component\n");
- impl = gtrace_read32(pdata, RVTRACE_COMPONENT_IMPL_OFFSET);
+ if (match_data->get_impl)
+ impl = match_data->get_impl(pdata);
+ else
+ impl = gtrace_read32(pdata, RVTRACE_COMPONENT_IMPL_OFFSET);
+
type = (impl >> RVTRACE_COMPONENT_IMPL_TYPE_SHIFT) &
RVTRACE_COMPONENT_IMPL_TYPE_MASK;
major = (impl >> RVTRACE_COMPONENT_IMPL_VERMAJOR_SHIFT) &
@@ -154,8 +164,28 @@ static void rvtrace_platform_remove(struct platform_device *pdev)
gtrace_unregister_component(comp);
}
+static const struct rvtrace_match_data rvtrace_std_data = {
+ .format = GTRACE_FORMAT_ETRACE,
+};
+
+#ifdef CONFIG_RVTRACE_V0
+static const struct rvtrace_match_data rvtrace_v0_encoder_data = {
+ .format = GTRACE_FORMAT_NTRACE,
+ .get_impl = rvtrace_v0_get_encoder_impl,
+};
+
+static const struct rvtrace_match_data rvtrace_v0_funnel_data = {
+ .format = GTRACE_FORMAT_NTRACE,
+ .get_impl = rvtrace_v0_get_funnel_impl,
+};
+#endif
+
static const struct of_device_id rvtrace_platform_match[] = {
- { .compatible = "riscv,trace-component", .data = (void *)GTRACE_FORMAT_ETRACE },
+ { .compatible = "riscv,trace-component", .data = &rvtrace_std_data },
+#ifdef CONFIG_RVTRACE_V0
+ { .compatible = "sifive,trace-encoder0", .data = &rvtrace_v0_encoder_data },
+ { .compatible = "sifive,trace-funnel0", .data = &rvtrace_v0_funnel_data },
+#endif
{}
};
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.c b/drivers/hwtracing/gtrace/rvtrace-v0.c
new file mode 100644
index 000000000000..a5ca9001bc08
--- /dev/null
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 SiFive Inc.
+ */
+
+#include <linux/gtrace.h>
+#include "rvtrace.h"
+#include "rvtrace-v0.h"
+
+static u32 rvtrace_v0_get_impl(struct gtrace_platform_data *pdata,
+ enum rvtrace_component_type type)
+{
+ u32 impl, major, minor;
+
+ impl = gtrace_read32(pdata, RVTRACE_COMPONENT_IMPL_OFFSET);
+ major = 0;
+ minor = (impl >> RVTRACE_COMPONENT_IMPL_VERMAJOR_SHIFT) &
+ RVTRACE_COMPONENT_IMPL_VERMAJOR_MASK;
+
+ /* Encode to standard rvtrace impl format */
+ return (type << RVTRACE_COMPONENT_IMPL_TYPE_SHIFT) |
+ (minor << RVTRACE_COMPONENT_IMPL_VERMINOR_SHIFT) |
+ (major << RVTRACE_COMPONENT_IMPL_VERMAJOR_SHIFT);
+}
+
+u32 rvtrace_v0_get_encoder_impl(struct gtrace_platform_data *pdata)
+{
+ return rvtrace_v0_get_impl(pdata, RVTRACE_COMPONENT_TYPE_ENCODER);
+}
+
+u32 rvtrace_v0_get_funnel_impl(struct gtrace_platform_data *pdata)
+{
+ return rvtrace_v0_get_impl(pdata, RVTRACE_COMPONENT_TYPE_FUNNEL);
+}
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.h b/drivers/hwtracing/gtrace/rvtrace-v0.h
new file mode 100644
index 000000000000..2d83c87936b9
--- /dev/null
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 SiFive, Inc.
+ *
+ * Support for pre-ratified (version 0.x) RISC-V trace components.
+ */
+
+#ifndef __RVTRACE_V0_H__
+#define __RVTRACE_V0_H__
+
+#include <linux/gtrace.h>
+#include <linux/types.h>
+
+u32 rvtrace_v0_get_encoder_impl(struct gtrace_platform_data *pdata);
+u32 rvtrace_v0_get_funnel_impl(struct gtrace_platform_data *pdata);
+
+#endif /* __RVTRACE_V0_H__ */
diff --git a/drivers/hwtracing/gtrace/rvtrace.h b/drivers/hwtracing/gtrace/rvtrace.h
index e6be1ce303cc..7f9e88d99b8f 100644
--- a/drivers/hwtracing/gtrace/rvtrace.h
+++ b/drivers/hwtracing/gtrace/rvtrace.h
@@ -109,6 +109,18 @@ static inline const char *rvtrace_type_name(enum gtrace_component_type type)
}
}
+/**
+ * struct rvtrace_match_data - Per-compatible data for RISC-V trace components
+ * @format: Trace format to be used by the component.
+ * @get_impl: Optional callback to retrieve the implementation register value
+ * encoded in the standard RISC-V trace IMPL register format. Used
+ * by pre-ratified components whose IMPL register layout differs.
+ */
+struct rvtrace_match_data {
+ enum gtrace_format format;
+ u32 (*get_impl)(struct gtrace_platform_data *pdata);
+};
+
static inline int rvtrace_comp_poll_empty(struct gtrace_component *comp)
{
return gtrace_poll_bit(comp->pdata, RVTRACE_COMPONENT_CTRL_OFFSET,
--
2.43.7