[PATCH 06/10] gtrace: rvtrace: Add pre-ratified trace sink config

Leo Yu-Chi Liang leo.liang at sifive.com
Thu Nov 26 01:07:12 PST 2026


From: Eric Lin <eric.lin at sifive.com>

To decide which sink to send trace data to, pre-ratified trace
hardware (eg., SiFive, Eswin) sets a sink bitfield directly within the
current component's control MMIO register.

Routing trace data from a source to an output component requires
configuring the connections between components, and the mechanism
varies between hardware implementations:

  - Standard (ratified) RISC-V trace configures routing at the
    receiving end. For example, a funnel uses the trFunnelDisInput
    register to control which source input is accepted.
  - Pre-ratified hardware configures routing at the sending end,
    selecting the output destination by setting a bitfield directly
    within the current component's control MMIO register.

To support both, component drivers need the path topology when
starting, rather than just the component itself, so update the
gtrace_driver start() callback to take a struct gtrace_path_node.

Introduce rvtrace_v0_sink_config() for pre-ratified trace hardware to
configure the destination sink based on the hardware capabilities and
the current trace path before starting the trace.

Co-developed-by: Nick Hu <nick.hu at sifive.com>
Signed-off-by: Nick Hu <nick.hu at sifive.com>
Co-developed-by: Vincent Chen <vincent.chen at sifive.com>
Signed-off-by: Vincent Chen <vincent.chen at sifive.com>
Signed-off-by: Eric Lin <eric.lin at sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang at sifive.com>
---
 drivers/hwtracing/gtrace/gtrace-core.c     | 13 +++------
 drivers/hwtracing/gtrace/rvtrace-encoder.c | 10 ++++++-
 drivers/hwtracing/gtrace/rvtrace-ramsink.c |  3 ++-
 drivers/hwtracing/gtrace/rvtrace-v0.c      | 43 ++++++++++++++++++++++++++++++
 drivers/hwtracing/gtrace/rvtrace-v0.h      | 23 ++++++++++++++++
 include/linux/gtrace.h                     | 19 +++++++++++--
 6 files changed, 98 insertions(+), 13 deletions(-)

diff --git a/drivers/hwtracing/gtrace/gtrace-core.c b/drivers/hwtracing/gtrace/gtrace-core.c
index 09b200bd1690..6fe71c5cd0cf 100644
--- a/drivers/hwtracing/gtrace/gtrace-core.c
+++ b/drivers/hwtracing/gtrace/gtrace-core.c
@@ -498,12 +498,6 @@ void gtrace_unregister_component(struct gtrace_component *comp)
 }
 EXPORT_SYMBOL_GPL(gtrace_unregister_component);
 
-struct gtrace_path_node {
-	struct list_head		head;
-	struct gtrace_component		*comp;
-	struct gtrace_connection	*conn;
-};
-
 struct gtrace_component *gtrace_path_source(struct gtrace_path *path)
 {
 	struct gtrace_path_node *node;
@@ -621,8 +615,9 @@ static void gtrace_release_path_nodes(struct gtrace_path *path)
 	}
 }
 
-static int __gtrace_comp_start(struct gtrace_component *comp, pid_t owner)
+static int __gtrace_comp_start(struct gtrace_path_node *node, pid_t owner)
 {
+	struct gtrace_component *comp = node->comp;
 	struct gtrace_comp_priv *cpriv = to_gtrace_comp_priv(comp);
 	const struct gtrace_driver *gtdrv = to_gtrace_driver(comp->dev.driver);
 	unsigned long flags;
@@ -639,7 +634,7 @@ static int __gtrace_comp_start(struct gtrace_component *comp, pid_t owner)
 		}
 	} else {
 		if (gtdrv->start) {
-			ret = gtdrv->start(comp);
+			ret = gtdrv->start(node);
 			if (ret)
 				goto out;
 		}
@@ -686,7 +681,7 @@ int gtrace_path_start(struct gtrace_path *path)
 	int ret = 0;
 
 	list_for_each_entry_reverse(node, &path->comp_list, head) {
-		ret = __gtrace_comp_start(node->comp, path->owner);
+		ret = __gtrace_comp_start(node, path->owner);
 		if (ret)
 			break;
 	}
diff --git a/drivers/hwtracing/gtrace/rvtrace-encoder.c b/drivers/hwtracing/gtrace/rvtrace-encoder.c
index 0a23c78c0caf..92672f1e7098 100644
--- a/drivers/hwtracing/gtrace/rvtrace-encoder.c
+++ b/drivers/hwtracing/gtrace/rvtrace-encoder.c
@@ -155,8 +155,9 @@ static int rvtrace_encoder_probe(struct gtrace_component *comp)
 	return 0;
 }
 
-static int rvtrace_encoder_start(struct gtrace_component *comp)
+static int rvtrace_encoder_start(struct gtrace_path_node *node)
 {
+	struct gtrace_component *comp = node->comp;
 	struct rvtrace_encoder_priv *priv = dev_get_drvdata(&comp->dev);
 	struct gtrace_platform_data *pdata = comp->pdata;
 	int ret;
@@ -171,6 +172,13 @@ static int rvtrace_encoder_start(struct gtrace_component *comp)
 			dev_err(&comp->dev, "failed to set format %d\n", pdata->format);
 			return ret;
 		}
+	} else {
+		/* Set pre-ratified comp's next sink */
+		ret = rvtrace_v0_sink_config(node);
+		if (ret) {
+			dev_err(&comp->dev, "failed to set next sink.\n");
+			return ret;
+		}
 	}
 
 	ret = gtrace_enable_component(comp);
diff --git a/drivers/hwtracing/gtrace/rvtrace-ramsink.c b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
index 67b6779aacee..52aeb7626a55 100644
--- a/drivers/hwtracing/gtrace/rvtrace-ramsink.c
+++ b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
@@ -72,8 +72,9 @@ static void rvtrace_ramsink_write_pair(struct gtrace_platform_data *pdata, u64 v
 	gtrace_write32(pdata, upper_32_bits(val), low_off + 4);
 }
 
-static int rvtrace_ramsink_start(struct gtrace_component *comp)
+static int rvtrace_ramsink_start(struct gtrace_path_node *node)
 {
+	struct gtrace_component *comp = node->comp;
 	struct rvtrace_ramsink_priv *priv = dev_get_drvdata(&comp->dev);
 	u32 trram_ctrl;
 	int ret;
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.c b/drivers/hwtracing/gtrace/rvtrace-v0.c
index 01e62642c245..c9254e71a972 100644
--- a/drivers/hwtracing/gtrace/rvtrace-v0.c
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.c
@@ -5,10 +5,53 @@
 
 #include <linux/device.h>
 #include <linux/err.h>
+#include <linux/export.h>
 #include <linux/gtrace.h>
 #include "rvtrace.h"
 #include "rvtrace-v0.h"
 
+/*
+ * Pre-ratified trace hardware selects the destination of the trace data
+ * with the teSink/tfSink field in the component's own control register,
+ * i.e. routing is configured at the sending end. The destination is
+ * derived from the trace path: the last node of the path (no outgoing
+ * connection) writes to system memory (SBA), otherwise the component
+ * feeds the funnel the path connection leads to.
+ */
+int rvtrace_v0_sink_config(struct gtrace_path_node *node)
+{
+	struct gtrace_component *comp = node->comp;
+	struct gtrace_platform_data *pdata = comp->pdata;
+	struct rvtrace_v0_comp_features *data = comp->id.data;
+	struct gtrace_component *next;
+	u32 val;
+
+	if (!data)
+		return -EINVAL;
+
+	val = gtrace_read32(pdata, RVTRACE_COMPONENT_CTRL_OFFSET);
+	val &= ~RVTRACE_V0_CTRL_SINK_MASK;
+
+	if (!node->conn) {
+		/* Last node in the path */
+		if (data->has_sba_sink)
+			val |= TE_SINK_SBA << RVTRACE_V0_CTRL_SINK_SHIFT;
+		else
+			dev_warn(&comp->dev,
+				 "Component is last node but doesn't support SBA sink\n");
+	} else {
+		next = node->conn->dest_comp;
+		if (next && next->id.type == GTRACE_RVTRACE_FUNNEL &&
+		    data->has_funnel_sink)
+			val |= TE_SINK_FUNNEL << RVTRACE_V0_CTRL_SINK_SHIFT;
+	}
+
+	gtrace_write32(pdata, val, RVTRACE_COMPONENT_CTRL_OFFSET);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(rvtrace_v0_sink_config);
+
 void *rvtrace_v0_get_comp_data(struct gtrace_platform_data *pdata)
 {
 	struct rvtrace_v0_comp_features *data;
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.h b/drivers/hwtracing/gtrace/rvtrace-v0.h
index 5abdc4788f64..d1dac2034a14 100644
--- a/drivers/hwtracing/gtrace/rvtrace-v0.h
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.h
@@ -10,6 +10,7 @@
 
 #include <linux/gtrace.h>
 #include <linux/bits.h>
+#include <linux/errno.h>
 #include <linux/types.h>
 
 #define RVTRACE_V0_IMPL_HAS_SRAM_SINK_BIT	4
@@ -24,6 +25,19 @@
 #define RVTRACE_V0_IMPL_HAS_SBA_SINK_MASK	BIT(RVTRACE_V0_IMPL_HAS_SBA_SINK_BIT)
 #define RVTRACE_V0_IMPL_HAS_FUNNEL_SINK_MASK	BIT(RVTRACE_V0_IMPL_HAS_FUNNEL_SINK_BIT)
 
+/* teControl/tfControl sink selection */
+#define RVTRACE_V0_CTRL_SINK_SHIFT		28
+#define RVTRACE_V0_CTRL_SINK_MASK		GENMASK(31, RVTRACE_V0_CTRL_SINK_SHIFT)
+
+enum rvtrace_v0_sink {
+	TE_SINK_DEFAULT = 0,
+	TE_SINK_SRAM = 4,
+	TE_SINK_ATB = 5,
+	TE_SINK_PIB = 6,
+	TE_SINK_SBA = 7,
+	TE_SINK_FUNNEL = 8,
+};
+
 /* Number of SRC bits preset by hardware in the Nexus messages */
 #define RVTRACE_V0_IMPL_NSRCBITS_SHIFT		24
 #define RVTRACE_V0_IMPL_NSRCBITS_MASK		0xf
@@ -40,4 +54,13 @@ u32 rvtrace_v0_get_encoder_impl(struct gtrace_platform_data *pdata);
 u32 rvtrace_v0_get_funnel_impl(struct gtrace_platform_data *pdata);
 void *rvtrace_v0_get_comp_data(struct gtrace_platform_data *pdata);
 
+#ifdef CONFIG_RVTRACE_V0
+int rvtrace_v0_sink_config(struct gtrace_path_node *node);
+#else
+static inline int rvtrace_v0_sink_config(struct gtrace_path_node *node)
+{
+	return -EOPNOTSUPP;
+}
+#endif
+
 #endif /* __RVTRACE_V0_H__ */
diff --git a/include/linux/gtrace.h b/include/linux/gtrace.h
index b8826b25d35a..6b2ff2a63694 100644
--- a/include/linux/gtrace.h
+++ b/include/linux/gtrace.h
@@ -258,6 +258,18 @@ struct gtrace_path {
 	pid_t				owner;
 };
 
+/**
+ * struct gtrace_path_node - Representation of a node in the trace path.
+ * @head: List head for linking nodes in the path.
+ * @comp: Pointer to the trace component at this node.
+ * @conn: Pointer to the connection leading to the next node (NULL for sink).
+ */
+struct gtrace_path_node {
+	struct list_head		head;
+	struct gtrace_component		*comp;
+	struct gtrace_connection	*conn;
+};
+
 struct gtrace_component *gtrace_path_source(struct gtrace_path *path);
 struct gtrace_component *gtrace_path_sink(struct gtrace_path *path);
 struct gtrace_path *gtrace_create_path(struct gtrace_component *source,
@@ -291,7 +303,10 @@ int gtrace_path_copyto_auxbuf(struct gtrace_path *path,
  * @copyto_auxbuf: Callback to copy data into perf AUX buffer. The driver
  *                 reports the PMU specific trace format of the copied data
  *                 via @format (see PERF_AUX_FLAG_PMU_FORMAT_TYPE_MASK).
- * @start:         Callback to start tracing.
+ * @start:         Callback to start tracing. The path node gives access to
+ *                 the component and to the connection leading to the next
+ *                 component of the path, so that drivers can program their
+ *                 routing registers.
  * @stop:          Callback to stop tracing.
  * @probe:         Driver probe() function.
  * @remove:        Driver remove() function.
@@ -304,7 +319,7 @@ struct gtrace_driver {
 	size_t			(*copyto_auxbuf)(struct gtrace_component *comp,
 						 struct gtrace_perf_auxbuf *buf,
 						 u64 *format);
-	int			(*start)(struct gtrace_component *comp);
+	int			(*start)(struct gtrace_path_node *node);
 	int			(*stop)(struct gtrace_component *comp);
 	int			(*probe)(struct gtrace_component *comp);
 	void			(*remove)(struct gtrace_component *comp);

-- 
2.43.7




More information about the linux-riscv mailing list