[PATCH 10/10] gtrace: rvtrace: Add copyto_auxbuf callback for pre-ratified encoder and funnel

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


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

Unlike the standard RISC-V trace ramsink, which is an independent
device, the pre-ratified ramsink are embedded within the trace encoder
or funnel MMIO registers.

When pre-ratified encoder and funnel components act as a sink, the
trace data must be copied from the trace buffer to the perf AUX buffer
after tracing is stopped. To support this, add
rvtrace_v0_ramsink_copyto_auxbuf() which shares the copy logic with the
standard ramsink, and use it to implement the copyto_auxbuf callback of
the encoder and funnel drivers. The callback copies nothing when the
component doesn't act as a sink.

Note: an encoder or funnel component may be in the middle of a given
path, so gtrace_path_copyto_auxbuf() now only invokes the copyto_auxbuf()
callback of the path's sink instead of the first component in the path
implementing it.

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     | 19 ++++++++-------
 drivers/hwtracing/gtrace/rvtrace-encoder.c | 10 ++++++++
 drivers/hwtracing/gtrace/rvtrace-funnel.c  | 10 ++++++++
 drivers/hwtracing/gtrace/rvtrace-ramsink.c | 37 ++++++++++++++++++++++++++----
 drivers/hwtracing/gtrace/rvtrace-ramsink.h | 12 ++++++++++
 5 files changed, 74 insertions(+), 14 deletions(-)

diff --git a/drivers/hwtracing/gtrace/gtrace-core.c b/drivers/hwtracing/gtrace/gtrace-core.c
index 6fe71c5cd0cf..7a818d647372 100644
--- a/drivers/hwtracing/gtrace/gtrace-core.c
+++ b/drivers/hwtracing/gtrace/gtrace-core.c
@@ -714,10 +714,9 @@ int gtrace_path_copyto_auxbuf(struct gtrace_path *path,
 			      struct gtrace_perf_auxbuf *buf,
 			      size_t *bytes_copied, u64 *format)
 {
-	struct gtrace_comp_priv *sink = to_gtrace_comp_priv(gtrace_path_sink(path));
+	struct gtrace_component *comp = gtrace_path_sink(path);
+	struct gtrace_comp_priv *sink = to_gtrace_comp_priv(comp);
 	const struct gtrace_driver *gtdrv;
-	struct gtrace_component *comp;
-	struct gtrace_path_node *node;
 	int ret = -EOPNOTSUPP;
 	unsigned long flags;
 
@@ -735,15 +734,15 @@ int gtrace_path_copyto_auxbuf(struct gtrace_path *path,
 		goto out;
 	}
 
-	list_for_each_entry(node, &path->comp_list, head) {
-		comp = node->comp;
-		gtdrv = to_gtrace_driver(comp->dev.driver);
-		if (!gtdrv || !gtdrv->copyto_auxbuf)
-			continue;
-
+	/*
+	 * Only the sink of the path holds the trace data. Other components in
+	 * the path may implement copyto_auxbuf() as well, e.g. when they embed
+	 * a sink which is only used when they are at the end of a path.
+	 */
+	gtdrv = to_gtrace_driver(comp->dev.driver);
+	if (gtdrv && gtdrv->copyto_auxbuf) {
 		*bytes_copied = gtdrv->copyto_auxbuf(comp, buf, format);
 		ret = 0;
-		break;
 	}
 out:
 	raw_spin_unlock_irqrestore(&sink->lock, flags);
diff --git a/drivers/hwtracing/gtrace/rvtrace-encoder.c b/drivers/hwtracing/gtrace/rvtrace-encoder.c
index 8d2f7ac5e0fe..a854c4695351 100644
--- a/drivers/hwtracing/gtrace/rvtrace-encoder.c
+++ b/drivers/hwtracing/gtrace/rvtrace-encoder.c
@@ -164,6 +164,15 @@ static int rvtrace_encoder_probe(struct gtrace_component *comp)
 	return 0;
 }
 
+static size_t rvtrace_encoder_copyto_auxbuf(struct gtrace_component *comp,
+					    struct gtrace_perf_auxbuf *buf,
+					    u64 *format)
+{
+	struct rvtrace_encoder_priv *priv = dev_get_drvdata(&comp->dev);
+
+	return rvtrace_v0_ramsink_copyto_auxbuf(comp, priv->sink, buf, format);
+}
+
 static void rvtrace_encoder_remove(struct gtrace_component *comp)
 {
 	struct rvtrace_encoder_priv *priv = dev_get_drvdata(&comp->dev);
@@ -261,6 +270,7 @@ static const struct gtrace_component_id rvtrace_encoder_ids[] = {
 
 static struct gtrace_driver rvtrace_encoder_driver = {
 	.id_table = rvtrace_encoder_ids,
+	.copyto_auxbuf = rvtrace_encoder_copyto_auxbuf,
 	.probe = rvtrace_encoder_probe,
 	.remove = rvtrace_encoder_remove,
 	.start = rvtrace_encoder_start,
diff --git a/drivers/hwtracing/gtrace/rvtrace-funnel.c b/drivers/hwtracing/gtrace/rvtrace-funnel.c
index 463e504aaa92..48d1084957c1 100644
--- a/drivers/hwtracing/gtrace/rvtrace-funnel.c
+++ b/drivers/hwtracing/gtrace/rvtrace-funnel.c
@@ -75,6 +75,15 @@ static int rvtrace_funnel_probe(struct gtrace_component *comp)
 	return 0;
 }
 
+static size_t rvtrace_funnel_copyto_auxbuf(struct gtrace_component *comp,
+					   struct gtrace_perf_auxbuf *buf,
+					   u64 *format)
+{
+	struct rvtrace_funnel_priv *priv = dev_get_drvdata(&comp->dev);
+
+	return rvtrace_v0_ramsink_copyto_auxbuf(comp, priv->sink, buf, format);
+}
+
 static void rvtrace_funnel_remove(struct gtrace_component *comp)
 {
 	struct rvtrace_funnel_priv *priv = dev_get_drvdata(&comp->dev);
@@ -95,6 +104,7 @@ static const struct gtrace_component_id rvtrace_funnel_ids[] = {
 
 static struct gtrace_driver rvtrace_funnel_driver = {
 	.id_table = rvtrace_funnel_ids,
+	.copyto_auxbuf = rvtrace_funnel_copyto_auxbuf,
 	.start = rvtrace_funnel_start,
 	.stop = rvtrace_funnel_stop,
 	.probe = rvtrace_funnel_probe,
diff --git a/drivers/hwtracing/gtrace/rvtrace-ramsink.c b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
index f183dcd71c7c..cc0a5785bda4 100644
--- a/drivers/hwtracing/gtrace/rvtrace-ramsink.c
+++ b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
@@ -188,11 +188,11 @@ static void tbuf_to_pbuf_copy(struct trace_buf *src, struct trace_buf *dst, size
 	}
 }
 
-static size_t rvtrace_ramsink_copyto_auxbuf(struct gtrace_component *comp,
-					    struct gtrace_perf_auxbuf *buf,
-					    u64 *format)
+static size_t __rvtrace_ramsink_copyto_auxbuf(struct gtrace_component *comp,
+					      struct rvtrace_ramsink_priv *priv,
+					      struct gtrace_perf_auxbuf *buf,
+					      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;
@@ -231,6 +231,13 @@ static size_t rvtrace_ramsink_copyto_auxbuf(struct gtrace_component *comp,
 	return bytes;
 }
 
+static size_t rvtrace_ramsink_copyto_auxbuf(struct gtrace_component *comp,
+					    struct gtrace_perf_auxbuf *buf,
+					    u64 *format)
+{
+	return __rvtrace_ramsink_copyto_auxbuf(comp, dev_get_drvdata(&comp->dev), buf, format);
+}
+
 static int rvtrace_ramsink_setup_buf(struct gtrace_component *comp,
 				     struct rvtrace_ramsink_priv *priv)
 {
@@ -448,6 +455,28 @@ void rvtrace_v0_ramsink_release(struct gtrace_component *comp,
 	of_reserved_mem_device_release(comp->pdata->dev);
 }
 EXPORT_SYMBOL_GPL(rvtrace_v0_ramsink_release);
+
+/**
+ * rvtrace_v0_ramsink_copyto_auxbuf() - Copy trace data from the sink embedded
+ * in a pre-ratified trace encoder/funnel into the perf AUX buffer.
+ * @comp:   Pre-ratified trace encoder/funnel component.
+ * @priv:   Sink private data returned by rvtrace_v0_ramsink_setup().
+ * @buf:    Perf AUX buffer.
+ * @format: Trace format of the copied data.
+ *
+ * Return: Number of bytes copied, 0 if the component doesn't act as a sink.
+ */
+size_t rvtrace_v0_ramsink_copyto_auxbuf(struct gtrace_component *comp,
+					struct rvtrace_ramsink_priv *priv,
+					struct gtrace_perf_auxbuf *buf,
+					u64 *format)
+{
+	if (!priv)
+		return 0;
+
+	return __rvtrace_ramsink_copyto_auxbuf(comp, priv, buf, format);
+}
+EXPORT_SYMBOL_GPL(rvtrace_v0_ramsink_copyto_auxbuf);
 #endif /* CONFIG_RVTRACE_V0 */
 
 static int rvtrace_ramsink_probe(struct gtrace_component *comp)
diff --git a/drivers/hwtracing/gtrace/rvtrace-ramsink.h b/drivers/hwtracing/gtrace/rvtrace-ramsink.h
index 0c1b346af8c0..ff767da7f861 100644
--- a/drivers/hwtracing/gtrace/rvtrace-ramsink.h
+++ b/drivers/hwtracing/gtrace/rvtrace-ramsink.h
@@ -15,6 +15,10 @@ struct rvtrace_ramsink_priv;
 struct rvtrace_ramsink_priv *rvtrace_v0_ramsink_setup(struct gtrace_component *comp);
 void rvtrace_v0_ramsink_release(struct gtrace_component *comp,
 				struct rvtrace_ramsink_priv *priv);
+size_t rvtrace_v0_ramsink_copyto_auxbuf(struct gtrace_component *comp,
+					struct rvtrace_ramsink_priv *priv,
+					struct gtrace_perf_auxbuf *buf,
+					u64 *format);
 #else
 static inline struct rvtrace_ramsink_priv *
 rvtrace_v0_ramsink_setup(struct gtrace_component *comp)
@@ -26,6 +30,14 @@ static inline void rvtrace_v0_ramsink_release(struct gtrace_component *comp,
 					      struct rvtrace_ramsink_priv *priv)
 {
 }
+
+static inline size_t rvtrace_v0_ramsink_copyto_auxbuf(struct gtrace_component *comp,
+						      struct rvtrace_ramsink_priv *priv,
+						      struct gtrace_perf_auxbuf *buf,
+						      u64 *format)
+{
+	return 0;
+}
 #endif
 
 #endif /* __RVTRACE_RAMSINK_H__ */

-- 
2.43.7




More information about the linux-riscv mailing list