mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components
@ 2026-11-26  9:07 Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 01/10] dt-bindings: riscv: Add SiFive pre-ratified " Leo Yu-Chi Liang
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

This series adds support for pre-ratified (version 0.x) RISC-V trace
hardware, such as the SiFive trace encoder and trace funnel as integrated
in the ESWIN EIC7700 (HiFive Premier P550), on top of the gtrace framework
posted by Mayuresh Chitale and Anup Patel [1].

The gtrace framework and the rvtrace drivers in [1] target the ratified
RISC-V Trace Control Interface. Hardware that predates the ratified
specification is functionally the same (encoder -> funnel -> sink), but
differs in how it is discovered, routed and where the sink lives. The
specification of the pre-ratified trace hardware, is described in the
EIC7700X SoC Technical Reference Manual [3].
This series makes the existing framework drivers handle both flavours
instead of adding a second, parallel driver stack.

Background: how pre-ratified hardware differs
---------------------------------------------

 1. Discovery. The IMPL register layout differs and does not encode the
    component type. The sink availability is also reported there. Specific
    compatible strings are therefore needed: "sifive,trace-encoder0" and
    "sifive,trace-funnel0".

 2. Source ID. The number of SRC bits is preset by hardware (teImpl.nSrcBits)
    and offset 0x8 is the read-only teImplExtra register, not
    trTeInstFeatures. The trTeInstMode encoding also differs.

 3. Routing. Ratified hardware routes at the receiving end (e.g. a funnel
    masks inputs with trFunnelDisInput). Pre-ratified hardware routes at the
    sending end: the destination sink is chosen by a bitfield in the sending
    component's own control register.

 4. Sink. A ratified RAM sink is an independent component with its own MMIO
    block. A pre-ratified RAM sink is embedded in the encoder or funnel
    register block, with a different register map.

 5. Buffer placement. The sink limit and write-pointer registers share their
    upper 32 bits with the sink base register, so the trace buffer must not
    cross a 4 GiB boundary.

These touch the gtrace framework from [1], so I would like feedback on them
in particular. Do you think supporting pre-ratified spec by the following
modification is the way to go?

Architectural changes
---------------------

 * Per-compatible match data (struct rvtrace_match_data) carries two optional
   callbacks. get_impl() translates the pre-ratified IMPL register into the
   standard IMPL layout, so the common probe code needs no special case.
   get_data() returns component private data (the available sinks), stored in
   gtrace_component_id.data.

 * Version 0 is handled inside the existing drivers. Components are
   identified by major version 0 (rvtrace_comp_is_v0()), and the
   pre-ratified specifics are isolated in a new rvtrace-v0.{c,h}, selected
   by CONFIG_RVTRACE_V0. Ratified-only configurations are not affected.

 * Routing is configured at start() time from the path topology
   (rvtrace_v0_sink_config()). This needs the next hop of the path, which the
   old start() callback could not see; see the API changes below.

 * The embedded RAM sink reuses the ratified RAM sink setup and copy
   functions. The register offsets are abstracted into struct
   rvtrace_ramsink_regs, and the standard and v0 layouts each provide one.
   An encoder or funnel only sets up its sink when it supports the SBA sink
   and has no output connection, i.e. when it is the end of a path.

 * Because an encoder or funnel can now be in the middle of a path and also
   implement copyto_auxbuf(), gtrace_path_copyto_auxbuf() now only calls the
   callback of the path's sink. Previously it called the first component in
   the path that implemented it.

Function signature and API changes
----------------------------------

 * struct gtrace_driver::start()
     int (*start)(struct gtrace_component *comp);
   becomes
     int (*start)(struct gtrace_path_node *node);
   Pre-ratified hardware needs the information of the next component to pick
   the sink at the sending end. The node gives access to both the component
   (node->comp) and the connection (node->conn).

 * gtrace_match_id()
   Now ends id_table iteration on GTRACE_COMPONENT_TYPE_NONE instead of a
   zero version. A non-zero version as the end-of-table marker cannot work
   with version 0 entries such as rvtrace_component_mkversion(0, 0).

 * rvtrace_ramsink_read_pair() / rvtrace_ramsink_write_pair()
     (pdata, low_off)           ->  (pdata, low_off, high_off)
     (pdata, val, low_off)      ->  (pdata, val, low_off, high_off)
   Zane also suggested this and Mayuresh said to fix this in v7.

 * rvtrace_ramsink copyto_auxbuf()
   Split into __rvtrace_ramsink_copyto_auxbuf(comp, priv, buf, format), which
   takes the private data explicitly, and a thin wrapper that keeps the
   existing callback signature. The v0 encoder and funnel call the helper
   through the new rvtrace_v0_ramsink_copyto_auxbuf().

Testing
-------

 * Boot tested on a HiFive Premier P550 (ESWIN EIC7700, four P550 cores).
   Tracing was verified to work on this board:

     perf record -e gtrace/event=0x1/ ls

   and the decoded trace content was checked.

 * Firmware: U-Boot from [2] (branch eic7x-dt-fix-hfp) and OpenSBI v1.9.

 * For this testing I reverted commit a7b9ce39fbe4 ("serial: 8250_dw: Ensure
   BUSY is deasserted") locally, because without it the P550 hits an init
   hang. This also happens on v7.4-rc4. See the note at the end.
   The revert is not part of this series.

Dependencies
------------

This series applies on top of [1] (v6, 14 patches), rebased onto v7.4-rc4.
Zane has a patchset implementing RISC-V Linux trace support [4], which add
RISC-V trace funnel support. I will try to review and merge the pre-ratified
funnel accordingly.

[1] https://lore.kernel.org/all/20261001052243.3042627-1-mayuresh.chitale@oss.qualcomm.com/
[2] https://github.com/ganboing/u-boot-eic7x/tree/eic7x-dt-fix-hfp
[3] https://github.com/eswincomputing/EIC7700X-SoC-Technical-Reference-Manual/releases
[4] https://lore.kernel.org/all/20261023031505.2020925-1-liangzhen@linux.spacemit.com/

Side note: UART init hang on EIC7700 (question for the ESWIN folks)
--------------------------------------------------------------------

I added the people working on upstreaming EIC7700 / P550 support to Cc for
this one, as it is unrelated to the trace series.

On the HiFive Premier P550, booting v7.0 or later to a built-in initramfs
(busybox) hangs at init. I bisected it between v6.19 and v7.0 to

  a7b9ce39fbe4 serial: 8250_dw: Ensure BUSY is deasserted

and the hang is still there on v7.4-rc4. Reverting that commit makes the
board boot, and so does adding "snps,uart-16550-compatible" to the DW UART
nodes (the latter I only tried on v7.0).
The commit adds a BUSY workaround. I have not found the root cause.

Do you see the same hang with an initramfs on your boards?

Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
Eric Lin (9):
      dt-bindings: riscv: Add SiFive pre-ratified trace components
      gtrace: rvtrace: Add pre-ratified implementation callback
      gtrace: rvtrace: Add pre-ratified private data callback
      gtrace: rvtrace: Add pre-ratified encoder support
      gtrace: rvtrace: Add pre-ratified trace sink config
      gtrace: rvtrace: Add pre-ratified trace funnel driver
      gtrace: rvtrace: Introduce struct rvtrace_ramsink_regs to abstract register offsets
      gtrace: rvtrace: Add pre-ratified ramsink setup
      gtrace: rvtrace: Add copyto_auxbuf callback for pre-ratified encoder and funnel

Leo Yu-Chi Liang (1):
      gtrace: Terminate id_table iteration on GTRACE_COMPONENT_TYPE_NONE

 .../bindings/riscv/riscv,trace-component.yaml      |  96 +++++++++-
 drivers/hwtracing/gtrace/Kconfig                   |  25 +++
 drivers/hwtracing/gtrace/Makefile                  |   5 +-
 drivers/hwtracing/gtrace/gtrace-core.c             |  34 ++--
 drivers/hwtracing/gtrace/rvtrace-encoder.c         |  79 +++++++-
 drivers/hwtracing/gtrace/rvtrace-funnel.c          | 132 +++++++++++++
 drivers/hwtracing/gtrace/rvtrace-platform.c        |  46 ++++-
 drivers/hwtracing/gtrace/rvtrace-ramsink.c         | 211 +++++++++++++++++++--
 drivers/hwtracing/gtrace/rvtrace-ramsink.h         |  43 +++++
 drivers/hwtracing/gtrace/rvtrace-v0.c              |  99 ++++++++++
 drivers/hwtracing/gtrace/rvtrace-v0.h              |  80 ++++++++
 drivers/hwtracing/gtrace/rvtrace.h                 |  22 +++
 include/linux/gtrace.h                             |  20 +-
 13 files changed, 833 insertions(+), 59 deletions(-)
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261124-send-preratified-trace-e32dc2b34a0a
prerequisite-message-id: 20261001052243.3042627-1-mayuresh.chitale@oss.qualcomm.com
prerequisite-patch-id: 1d7b783884bfe7e1d91063e0f8a232b86cdf4c31
prerequisite-patch-id: 8f59df8d7a27f0b3d3f34a0abb35e6c893c0858b
prerequisite-patch-id: ef326ac6eba908afdd7a49a264094367ed03a1c0
prerequisite-patch-id: dae8df7640ac89ff801741dba7e6d8e66432d54b
prerequisite-patch-id: 56b583656f75c0bf3eec5dac4113782110bbcf0a
prerequisite-patch-id: eacbdac5b4875ee7c84d9878f5a3cccfb79f7476
prerequisite-patch-id: 81535ef70fc7c3823810f14dcd1a069a05e6e2ac
prerequisite-patch-id: 65f827c9235a732612499b9ef95355872dc12a9f
prerequisite-patch-id: 53d04850a79d6b6fa1d88b6b0d2cebc4348a41d7
prerequisite-patch-id: 7a9c5eaccb41e1e63169edc3605f2dde7c373da4
prerequisite-patch-id: 6e0bd370e542f4cd0db70ff88b6634be6a446aec
prerequisite-patch-id: 509f41312fbcaa6a2bd0d9255cfd2116dc241adb
prerequisite-patch-id: 0ede4337d462b0cec2b67c10533768c7de762408
prerequisite-patch-id: 6b8939aa81ca09a16895a9ecf4de71e377a04ea6

Best regards,
--  
Leo Yu-Chi Liang <leo.liang@sifive.com>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 01/10] dt-bindings: riscv: Add SiFive pre-ratified trace components
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 02/10] gtrace: rvtrace: Add pre-ratified implementation callback Leo Yu-Chi Liang
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

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

Add compatible strings for the SiFive pre-ratified encoder and funnel
trace components. These implement version 0.x of the RISC-V trace
specification and do not support the generic riscv,trace-component
fallback.

A pre-ratified component with an embedded RAM sink can use a reserved
memory region for its trace buffer, as the sink registers restrict the
buffer to a single 4GB window. Add an optional memory-region property
for this.

Co-developed-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 .../bindings/riscv/riscv,trace-component.yaml      | 96 ++++++++++++++++++++--
 1 file changed, 89 insertions(+), 7 deletions(-)

diff --git a/Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml b/Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml
index bb519bc4a163..10660b8ce071 100644
--- a/Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml
+++ b/Documentation/devicetree/bindings/riscv/riscv,trace-component.yaml
@@ -16,16 +16,23 @@ description:
   The RISC-V trace components have implementation specific directed acyclic
   graph style interdependency where output of one component serves as input
   to another component and certain components (such as funnel) can take inputs
-  from multiple components. The type and version of a RISC-V trace component
-  can be discovered from it's IMPL memory mapped register hence component
-  specific compatible strings are not needed.
+  from multiple components. While the type and version of a RISC-V trace component
+  can be discovered from it's IMPL memory mapped register, specific compatible strings
+  are used for pre-ratified trace components.
 
 properties:
   compatible:
-    items:
-      - enum:
-          - qemu,trace-component
-      - const: riscv,trace-component
+    oneOf:
+      - items:
+          - enum:
+              - qemu,trace-component
+          - const: riscv,trace-component
+      - items:
+          - const: sifive,trace-encoder0
+        description: SiFive Pre-ratified Encoder Trace Component
+      - items:
+          - const: sifive,trace-funnel0
+        description: SiFive Pre-ratified Funnel Trace Component
 
   reg:
     maxItems: 1
@@ -35,6 +42,16 @@ properties:
     description:
       phandle to the cpu to which the RISC-V trace component is bound.
 
+  memory-region:
+    maxItems: 1
+    description:
+      phandle to a reserved memory region (shared-dma-pool) used for the
+      trace buffer of a pre-ratified trace component which has an embedded
+      RAM sink. The sink limit and write pointer registers of such a
+      component share their upper 32 bits with the sink base address
+      register, so the trace buffer must not cross a 4GB boundary. The
+      region must be aligned and sized accordingly.
+
   in-ports:
     $ref: /schemas/graph.yaml#/properties/ports
     patternProperties:
@@ -117,4 +134,69 @@ examples:
       };
     };
 
+  - |
+    // Example 2: SiFive trace encoder and funnel connection
+    reserved-memory {
+        #address-cells = <1>;
+        #size-cells = <1>;
+        ranges;
+
+        trace_buf: trace@80000000 {
+            compatible = "shared-dma-pool";
+            reg = <0x80000000 0x1000000>;
+            reusable;
+        };
+    };
+
+    encoder-0@2000000 {
+        compatible = "sifive,trace-encoder0";
+        reg = <0x2000000 0x1000>;
+        cpus = <&CPU0>;
+
+        out-ports {
+            port {
+                encoder0_out: endpoint {
+                  remote-endpoint = <&funnel_in0>;
+                };
+            };
+        };
+    };
+
+    encoder-1@2001000 {
+        compatible = "sifive,trace-encoder0";
+        reg = <0x2001000 0x1000>;
+        cpus = <&CPU1>;
+
+        out-ports {
+            port {
+                encoder1_out: endpoint {
+                  remote-endpoint = <&funnel_in1>;
+                };
+            };
+        };
+    };
+
+    funnel-0@2002000 {
+        compatible = "sifive,trace-funnel0";
+        reg = <0x2002000 0x1000>;
+        memory-region = <&trace_buf>;
+
+        in-ports {
+            #address-cells = <1>;
+            #size-cells = <0>;
+            port@0 {
+                reg = <0x0>;
+                funnel_in0: endpoint {
+                    remote-endpoint = <&encoder0_out>;
+                };
+            };
+
+            port@1 {
+                reg = <0x1>;
+                funnel_in1: endpoint {
+                    remote-endpoint = <&encoder1_out>;
+                };
+            };
+        };
+    };
 ...

-- 
2.43.7


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 02/10] gtrace: rvtrace: Add pre-ratified implementation callback
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 01/10] dt-bindings: riscv: Add SiFive pre-ratified " Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 03/10] gtrace: rvtrace: Add pre-ratified private data callback Leo Yu-Chi Liang
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

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

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@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 03/10] gtrace: rvtrace: Add pre-ratified private data callback
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 01/10] dt-bindings: riscv: Add SiFive pre-ratified " Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 02/10] gtrace: rvtrace: Add pre-ratified implementation callback Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 04/10] gtrace: rvtrace: Add pre-ratified encoder support Leo Yu-Chi Liang
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

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

For pre-ratified trace hardware, a trace component might support
different trace sinks, and its trace sink availability information
is embedded within the implementation register.

Introduce a get_data() callback function in 'struct rvtrace_match_data'
to retrieve the component's supported trace sink features and store
this information in the gtrace component ID data (comp->id.data). This
allows the driver to access the pre-ratified hardware capabilities
during setup.

While at it, zero-initialize the on-stack gtrace_component_id so that
id.data is never left uninitialized for ratified components.

Co-developed-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 drivers/hwtracing/gtrace/rvtrace-platform.c | 10 ++++++++++
 drivers/hwtracing/gtrace/rvtrace-v0.c       | 22 ++++++++++++++++++++++
 drivers/hwtracing/gtrace/rvtrace-v0.h       | 22 ++++++++++++++++++++++
 drivers/hwtracing/gtrace/rvtrace.h          |  4 ++++
 4 files changed, 58 insertions(+)

diff --git a/drivers/hwtracing/gtrace/rvtrace-platform.c b/drivers/hwtracing/gtrace/rvtrace-platform.c
index 8c1503bd8d98..b9b256d469e2 100644
--- a/drivers/hwtracing/gtrace/rvtrace-platform.c
+++ b/drivers/hwtracing/gtrace/rvtrace-platform.c
@@ -71,6 +71,7 @@ static int rvtrace_platform_probe(struct platform_device *pdev)
 	u32 impl, type, major, minor;
 	struct device_node *node;
 	struct resource *res;
+	void *data = NULL;
 	int gtype;
 	int ret;
 
@@ -126,6 +127,12 @@ static int rvtrace_platform_probe(struct platform_device *pdev)
 	else
 		impl = gtrace_read32(pdata, RVTRACE_COMPONENT_IMPL_OFFSET);
 
+	if (match_data->get_data) {
+		data = match_data->get_data(pdata);
+		if (IS_ERR(data))
+			return PTR_ERR(data);
+	}
+
 	type = (impl >> RVTRACE_COMPONENT_IMPL_TYPE_SHIFT) &
 		RVTRACE_COMPONENT_IMPL_TYPE_MASK;
 	major = (impl >> RVTRACE_COMPONENT_IMPL_VERMAJOR_SHIFT) &
@@ -139,6 +146,7 @@ static int rvtrace_platform_probe(struct platform_device *pdev)
 
 	id.type = gtype;
 	id.version = rvtrace_component_mkversion(major, minor);
+	id.data = data;
 
 	comp = gtrace_register_component(&id, rvtrace_type_name(gtype), pdata);
 	if (IS_ERR(comp))
@@ -172,11 +180,13 @@ static const struct rvtrace_match_data rvtrace_std_data = {
 static const struct rvtrace_match_data rvtrace_v0_encoder_data = {
 	.format = GTRACE_FORMAT_NTRACE,
 	.get_impl = rvtrace_v0_get_encoder_impl,
+	.get_data = rvtrace_v0_get_comp_data,
 };
 
 static const struct rvtrace_match_data rvtrace_v0_funnel_data = {
 	.format = GTRACE_FORMAT_NTRACE,
 	.get_impl = rvtrace_v0_get_funnel_impl,
+	.get_data = rvtrace_v0_get_comp_data,
 };
 #endif
 
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.c b/drivers/hwtracing/gtrace/rvtrace-v0.c
index a5ca9001bc08..01e62642c245 100644
--- a/drivers/hwtracing/gtrace/rvtrace-v0.c
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.c
@@ -3,10 +3,32 @@
  * Copyright (c) 2026 SiFive Inc.
  */
 
+#include <linux/device.h>
+#include <linux/err.h>
 #include <linux/gtrace.h>
 #include "rvtrace.h"
 #include "rvtrace-v0.h"
 
+void *rvtrace_v0_get_comp_data(struct gtrace_platform_data *pdata)
+{
+	struct rvtrace_v0_comp_features *data;
+	u32 impl;
+
+	data = devm_kzalloc(pdata->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return ERR_PTR(-ENOMEM);
+
+	impl = gtrace_read32(pdata, RVTRACE_COMPONENT_IMPL_OFFSET);
+
+	data->has_sram_sink = impl & RVTRACE_V0_IMPL_HAS_SRAM_SINK_MASK;
+	data->has_atb_sink = impl & RVTRACE_V0_IMPL_HAS_ATB_SINK_MASK;
+	data->has_pib_sink = impl & RVTRACE_V0_IMPL_HAS_PIB_SINK_MASK;
+	data->has_sba_sink = impl & RVTRACE_V0_IMPL_HAS_SBA_SINK_MASK;
+	data->has_funnel_sink = impl & RVTRACE_V0_IMPL_HAS_FUNNEL_SINK_MASK;
+
+	return data;
+}
+
 static u32 rvtrace_v0_get_impl(struct gtrace_platform_data *pdata,
 			       enum rvtrace_component_type type)
 {
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.h b/drivers/hwtracing/gtrace/rvtrace-v0.h
index 2d83c87936b9..0681405fa414 100644
--- a/drivers/hwtracing/gtrace/rvtrace-v0.h
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.h
@@ -9,9 +9,31 @@
 #define __RVTRACE_V0_H__
 
 #include <linux/gtrace.h>
+#include <linux/bits.h>
 #include <linux/types.h>
 
+#define RVTRACE_V0_IMPL_HAS_SRAM_SINK_BIT	4
+#define RVTRACE_V0_IMPL_HAS_ATB_SINK_BIT	5
+#define RVTRACE_V0_IMPL_HAS_PIB_SINK_BIT	6
+#define RVTRACE_V0_IMPL_HAS_SBA_SINK_BIT	7
+#define RVTRACE_V0_IMPL_HAS_FUNNEL_SINK_BIT	8
+
+#define RVTRACE_V0_IMPL_HAS_SRAM_SINK_MASK	BIT(RVTRACE_V0_IMPL_HAS_SRAM_SINK_BIT)
+#define RVTRACE_V0_IMPL_HAS_ATB_SINK_MASK	BIT(RVTRACE_V0_IMPL_HAS_ATB_SINK_BIT)
+#define RVTRACE_V0_IMPL_HAS_PIB_SINK_MASK	BIT(RVTRACE_V0_IMPL_HAS_PIB_SINK_BIT)
+#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)
+
+struct rvtrace_v0_comp_features {
+	bool has_sram_sink;
+	bool has_atb_sink;
+	bool has_pib_sink;
+	bool has_sba_sink;
+	bool has_funnel_sink;
+};
+
 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);
 
 #endif /* __RVTRACE_V0_H__ */
diff --git a/drivers/hwtracing/gtrace/rvtrace.h b/drivers/hwtracing/gtrace/rvtrace.h
index 7f9e88d99b8f..b0cf2e1d875f 100644
--- a/drivers/hwtracing/gtrace/rvtrace.h
+++ b/drivers/hwtracing/gtrace/rvtrace.h
@@ -115,10 +115,14 @@ static inline const char *rvtrace_type_name(enum gtrace_component_type type)
  * @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.
+ * @get_data: Optional callback to retrieve component specific private data
+ *            which is stored in gtrace_component_id.data. Returns an ERR_PTR()
+ *            on failure.
  */
 struct rvtrace_match_data {
 	enum gtrace_format format;
 	u32 (*get_impl)(struct gtrace_platform_data *pdata);
+	void *(*get_data)(struct gtrace_platform_data *pdata);
 };
 
 static inline int rvtrace_comp_poll_empty(struct gtrace_component *comp)

-- 
2.43.7


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 04/10] gtrace: rvtrace: Add pre-ratified encoder support
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
                   ` (2 preceding siblings ...)
  2026-11-26  9:07 ` [PATCH 03/10] gtrace: rvtrace: Add pre-ratified private data callback Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 05/10] gtrace: Terminate id_table iteration on GTRACE_COMPONENT_TYPE_NONE Leo Yu-Chi Liang
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

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

Add pre-ratified version support to the encoder driver. This allows
pre-ratified trace hardware to leverage the gtrace driver framework.

The pre-ratified encoder has a different register layout for the
source ID: the SRC field width (nSrcBits) and value are preset by
hardware in teImpl, while offset 0x8 is the read-only teImplExtra
register rather than trTeInstFeatures. Add rvtrace_comp_is_v0() and,
for pre-ratified encoders, derive srcbits from teImpl and skip
programming the trTeInstFeatures source ID fields.

Another difference is the encoding of the trTeInstMode field,
so set the corresponding "Optimized intruction trace" for
the pre-ratified encoder.

Co-developed-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 drivers/hwtracing/gtrace/rvtrace-encoder.c | 42 +++++++++++++++++++++++++-----
 drivers/hwtracing/gtrace/rvtrace-v0.h      |  4 +++
 drivers/hwtracing/gtrace/rvtrace.h         |  6 +++++
 3 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/drivers/hwtracing/gtrace/rvtrace-encoder.c b/drivers/hwtracing/gtrace/rvtrace-encoder.c
index 1f06b1a2b412..0a23c78c0caf 100644
--- a/drivers/hwtracing/gtrace/rvtrace-encoder.c
+++ b/drivers/hwtracing/gtrace/rvtrace-encoder.c
@@ -11,6 +11,9 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 #include "rvtrace.h"
+#include "rvtrace-v0.h"
+
+#define RVTRACE_V0_COMPONENT_CTRL_INSTMODE_OPIT	0x7
 
 #define RVTRACE_COMPONENT_CTRL_ITRACE_SHIFT	2
 #define RVTRACE_COMPONENT_CTRL_INSTMODE_MASK	0x7
@@ -95,6 +98,23 @@ static int rvtrace_encoder_probe(struct gtrace_component *comp)
 	if (!priv)
 		return -ENOMEM;
 
+	if (rvtrace_comp_is_v0(comp)) {
+		/*
+		 * Pre-ratified encoders have the SRC field width and value preset
+		 * by hardware in teImpl. Offset 0x8 is the read-only teImplExtra
+		 * register rather than trTeInstFeatures, so don't program it.
+		 */
+		val = gtrace_read32(pdata, RVTRACE_COMPONENT_IMPL_OFFSET);
+		priv->srcbits = (val >> RVTRACE_V0_IMPL_NSRCBITS_SHIFT) &
+				RVTRACE_V0_IMPL_NSRCBITS_MASK;
+		if (priv->srcbits) {
+			val = gtrace_read32(pdata, RVTRACE_COMPONENT_CTRL_OFFSET);
+			val &= ~BIT(RVTRACE_COMPONENT_CTRL_INHIBITSRC_SHIFT);
+			gtrace_write32(pdata, val, RVTRACE_COMPONENT_CTRL_OFFSET);
+		}
+		goto out;
+	}
+
 	ret = rvtrace_encoder_set_format(pdata);
 	if (ret) {
 		dev_err(&comp->dev, "failed to set format %d\n", pdata->format);
@@ -123,6 +143,7 @@ static int rvtrace_encoder_probe(struct gtrace_component *comp)
 			priv->srcbits = srcbits;
 		}
 	}
+out:
 	if (!priv->srcbits) {
 		dev_warn(&comp->dev, "cpu %d trace will not carry source id\n",
 			 pdata->bound_cpu);
@@ -141,13 +162,15 @@ static int rvtrace_encoder_start(struct gtrace_component *comp)
 	int ret;
 	u32 val;
 
-	if (priv->srcbits)
-		rvtrace_encoder_set_srcid(pdata, priv->srcbits);
+	if (!rvtrace_comp_is_v0(comp)) {
+		if (priv->srcbits)
+			rvtrace_encoder_set_srcid(pdata, priv->srcbits);
 
-	ret = rvtrace_encoder_set_format(pdata);
-	if (ret) {
-		dev_err(&comp->dev, "failed to set format %d\n", pdata->format);
-		return ret;
+		ret = rvtrace_encoder_set_format(pdata);
+		if (ret) {
+			dev_err(&comp->dev, "failed to set format %d\n", pdata->format);
+			return ret;
+		}
 	}
 
 	ret = gtrace_enable_component(comp);
@@ -159,7 +182,10 @@ static int rvtrace_encoder_start(struct gtrace_component *comp)
 	/* set mode */
 	val = gtrace_read32(pdata, RVTRACE_COMPONENT_CTRL_OFFSET);
 	val &= ~(RVTRACE_COMPONENT_CTRL_INSTMODE_MASK << RVTRACE_COMPONENT_CTRL_INSTMODE_SHIFT);
-	val |= (RVTRACE_COMPONENT_CTRL_INSTMODE_OPIT << RVTRACE_COMPONENT_CTRL_INSTMODE_SHIFT);
+	if (!rvtrace_comp_is_v0(comp))
+		val |= (RVTRACE_COMPONENT_CTRL_INSTMODE_OPIT << RVTRACE_COMPONENT_CTRL_INSTMODE_SHIFT);
+	else
+		val |= (RVTRACE_V0_COMPONENT_CTRL_INSTMODE_OPIT << RVTRACE_COMPONENT_CTRL_INSTMODE_SHIFT);
 	gtrace_write32(pdata, val, RVTRACE_COMPONENT_CTRL_OFFSET);
 
 	val = gtrace_read32(pdata, RVTRACE_COMPONENT_CTRL_OFFSET);
@@ -204,6 +230,8 @@ static int rvtrace_encoder_stop(struct gtrace_component *comp)
 static const struct gtrace_component_id rvtrace_encoder_ids[] = {
 	{ .type = GTRACE_RVTRACE_ENCODER,
 	  .version = rvtrace_component_mkversion(1, 0), },
+	{ .type = GTRACE_RVTRACE_ENCODER,
+	  .version = rvtrace_component_mkversion(0, 0), },
 	{},
 };
 
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.h b/drivers/hwtracing/gtrace/rvtrace-v0.h
index 0681405fa414..5abdc4788f64 100644
--- a/drivers/hwtracing/gtrace/rvtrace-v0.h
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.h
@@ -24,6 +24,10 @@
 #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)
 
+/* 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
+
 struct rvtrace_v0_comp_features {
 	bool has_sram_sink;
 	bool has_atb_sink;
diff --git a/drivers/hwtracing/gtrace/rvtrace.h b/drivers/hwtracing/gtrace/rvtrace.h
index b0cf2e1d875f..308de9fe39ae 100644
--- a/drivers/hwtracing/gtrace/rvtrace.h
+++ b/drivers/hwtracing/gtrace/rvtrace.h
@@ -72,6 +72,12 @@ enum rvtrace_component_impid {
 #define rvtrace_component_mkversion(__major, __minor)	\
 	((((__major) & 0xffff) << 16) | ((__minor) & 0xffff))
 
+/* Pre-ratified (version 0.x) components are identified by major version 0 */
+static inline bool rvtrace_comp_is_v0(struct gtrace_component *comp)
+{
+	return rvtrace_component_version_major(comp->id.version) == 0;
+}
+
 /* Map a RISC-V hardware component type to the global gtrace type */
 static inline int rvtrace_type_to_gtrace(enum rvtrace_component_type type)
 {

-- 
2.43.7


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 05/10] gtrace: Terminate id_table iteration on GTRACE_COMPONENT_TYPE_NONE
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
                   ` (3 preceding siblings ...)
  2026-11-26  9:07 ` [PATCH 04/10] gtrace: rvtrace: Add pre-ratified encoder support Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 06/10] gtrace: rvtrace: Add pre-ratified trace sink config Leo Yu-Chi Liang
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

gtrace_match_id() uses a non-zero id->version as the end-of-table
sentinel when walking a driver's id_table. This does not work for
pre-ratified components, whose version is 0, i.e.
rvtrace_component_mkversion(0, 0): an id_table holding only such an
entry is treated as empty, so the component never matches its driver.

Add GTRACE_COMPONENT_TYPE_NONE as the zero value of enum
gtrace_component_type and use it as the terminator instead. Existing
id_tables end with an empty entry, which maps to this value.

Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 drivers/hwtracing/gtrace/gtrace-core.c | 2 +-
 include/linux/gtrace.h                 | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/hwtracing/gtrace/gtrace-core.c b/drivers/hwtracing/gtrace/gtrace-core.c
index 034987237abc..09b200bd1690 100644
--- a/drivers/hwtracing/gtrace/gtrace-core.c
+++ b/drivers/hwtracing/gtrace/gtrace-core.c
@@ -103,7 +103,7 @@ const struct gtrace_component_id *gtrace_match_id(struct gtrace_component *comp,
 {
 	const struct gtrace_component_id *id;
 
-	for (id = ids; id->version; id++) {
+	for (id = ids; id->type != GTRACE_COMPONENT_TYPE_NONE; id++) {
 		if (comp->id.type != id->type)
 			continue;
 
diff --git a/include/linux/gtrace.h b/include/linux/gtrace.h
index da8ba3779193..b8826b25d35a 100644
--- a/include/linux/gtrace.h
+++ b/include/linux/gtrace.h
@@ -19,6 +19,7 @@
  * uses the entries relevant to it.
  */
 enum gtrace_component_type {
+	GTRACE_COMPONENT_TYPE_NONE,
 	GTRACE_RVTRACE_ENCODER,
 	GTRACE_RVTRACE_FUNNEL,
 	GTRACE_RVTRACE_RAMSINK,

-- 
2.43.7


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 06/10] gtrace: rvtrace: Add pre-ratified trace sink config
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
                   ` (4 preceding siblings ...)
  2026-11-26  9:07 ` [PATCH 05/10] gtrace: Terminate id_table iteration on GTRACE_COMPONENT_TYPE_NONE Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 07/10] gtrace: rvtrace: Add pre-ratified trace funnel driver Leo Yu-Chi Liang
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

From: Eric Lin <eric.lin@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@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 07/10] gtrace: rvtrace: Add pre-ratified trace funnel driver
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
                   ` (5 preceding siblings ...)
  2026-11-26  9:07 ` [PATCH 06/10] gtrace: rvtrace: Add pre-ratified trace sink config Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 08/10] gtrace: rvtrace: Introduce struct rvtrace_ramsink_regs to abstract register offsets Leo Yu-Chi Liang
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

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

Add the initial implementation to support the pre-ratified RISC-V
trace funnel. This implementation provides basic functionality to
enable the trace funnel, configure its next sink when tracing starts,
and disable the funnel when tracing stops.

Co-developed-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 drivers/hwtracing/gtrace/Kconfig          | 13 ++++
 drivers/hwtracing/gtrace/Makefile         |  1 +
 drivers/hwtracing/gtrace/rvtrace-funnel.c | 98 +++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)

diff --git a/drivers/hwtracing/gtrace/Kconfig b/drivers/hwtracing/gtrace/Kconfig
index b1f9ca8f73cb..66fc2791d8f4 100644
--- a/drivers/hwtracing/gtrace/Kconfig
+++ b/drivers/hwtracing/gtrace/Kconfig
@@ -43,6 +43,19 @@ config RVTRACE_ENCODER
 	  To compile this driver as a module, choose M here: the module
 	  will be called rvtrace-encoder.
 
+config RVTRACE_FUNNEL
+	tristate "RISC-V Trace Funnel driver"
+	depends on RVTRACE_V0
+	default y
+	help
+	  This driver provides support for the RISC-V trace funnel component
+	  which combines the trace data from multiple upstream components
+	  into a single trace stream. Only pre-ratified trace funnels are
+	  supported for now.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called rvtrace-funnel.
+
 config RVTRACE_RAMSINK
 	tristate "RISC-V Trace Ramsink driver"
 	depends on RVTRACE
diff --git a/drivers/hwtracing/gtrace/Makefile b/drivers/hwtracing/gtrace/Makefile
index 0a2e2d1732c2..38dd96b3dc34 100644
--- a/drivers/hwtracing/gtrace/Makefile
+++ b/drivers/hwtracing/gtrace/Makefile
@@ -8,4 +8,5 @@ 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_FUNNEL) += rvtrace-funnel.o
 obj-$(CONFIG_RVTRACE_RAMSINK) += rvtrace-ramsink.o
diff --git a/drivers/hwtracing/gtrace/rvtrace-funnel.c b/drivers/hwtracing/gtrace/rvtrace-funnel.c
new file mode 100644
index 000000000000..2e0843d233ab
--- /dev/null
+++ b/drivers/hwtracing/gtrace/rvtrace-funnel.c
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 SiFive Inc.
+ *
+ */
+
+#include <linux/device.h>
+#include <linux/gtrace.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include "rvtrace.h"
+#include "rvtrace-v0.h"
+
+static int rvtrace_funnel_start(struct gtrace_path_node *node)
+{
+	struct gtrace_component *comp = node->comp;
+	int ret;
+
+	/* 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);
+	if (ret)
+		dev_err(&comp->dev, "failed to enable funnel.\n");
+
+	return ret;
+}
+
+static int rvtrace_funnel_stop(struct gtrace_component *comp)
+{
+	int ret;
+
+	ret = gtrace_disable_component(comp);
+	if (ret) {
+		dev_err(&comp->dev, "failed to disable funnel.\n");
+		return ret;
+	}
+
+	return rvtrace_comp_poll_empty(comp);
+}
+
+static int rvtrace_funnel_probe(struct gtrace_component *comp)
+{
+	int ret;
+
+	ret = gtrace_enable_component(comp);
+	if (ret)
+		return dev_err_probe(&comp->dev, ret, "failed to enable funnel.\n");
+
+	return 0;
+}
+
+static void rvtrace_funnel_remove(struct gtrace_component *comp)
+{
+	int ret;
+
+	ret = gtrace_disable_component(comp);
+	if (ret)
+		dev_err(&comp->dev, "failed to disable funnel.\n");
+}
+
+static const struct gtrace_component_id rvtrace_funnel_ids[] = {
+	{ .type = GTRACE_RVTRACE_FUNNEL,
+	  .version = rvtrace_component_mkversion(0, 0), },
+	{},
+};
+
+static struct gtrace_driver rvtrace_funnel_driver = {
+	.id_table = rvtrace_funnel_ids,
+	.start = rvtrace_funnel_start,
+	.stop = rvtrace_funnel_stop,
+	.probe = rvtrace_funnel_probe,
+	.remove = rvtrace_funnel_remove,
+	.driver = {
+		.name = "rvtrace-funnel",
+	},
+};
+
+static int __init rvtrace_funnel_init(void)
+{
+	return gtrace_register_driver(&rvtrace_funnel_driver);
+}
+
+static void __exit rvtrace_funnel_exit(void)
+{
+	gtrace_unregister_driver(&rvtrace_funnel_driver);
+}
+
+module_init(rvtrace_funnel_init);
+module_exit(rvtrace_funnel_exit);
+
+/* Module information */
+MODULE_DESCRIPTION("RISC-V Trace Funnel Driver");
+MODULE_LICENSE("GPL");

-- 
2.43.7


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 08/10] gtrace: rvtrace: Introduce struct rvtrace_ramsink_regs to abstract register offsets
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
                   ` (6 preceding siblings ...)
  2026-11-26  9:07 ` [PATCH 07/10] gtrace: rvtrace: Add pre-ratified trace funnel driver Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 09/10] gtrace: rvtrace: Add pre-ratified ramsink setup Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 10/10] gtrace: rvtrace: Add copyto_auxbuf callback for pre-ratified encoder and funnel Leo Yu-Chi Liang
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

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

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@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 09/10] gtrace: rvtrace: Add pre-ratified ramsink setup
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
                   ` (7 preceding siblings ...)
  2026-11-26  9:07 ` [PATCH 08/10] gtrace: rvtrace: Introduce struct rvtrace_ramsink_regs to abstract register offsets Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  2026-11-26  9:07 ` [PATCH 10/10] gtrace: rvtrace: Add copyto_auxbuf callback for pre-ratified encoder and funnel Leo Yu-Chi Liang
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

From: Eric Lin <eric.lin@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 and utilize a different register layout.

The sink is only set up when the component supports the SBA sink and
has no output connections, matching the sink selection done by
rvtrace_v0_sink_config().

Additionally, handle a hardware constraint where the pre-ratified
ramsink's sink limit and write pointer registers share their upper
32 bits with the sink base address register. This restricts the trace
buffer from crossing a 4GB memory boundary. To satisfy this, invoke
of_reserved_mem_device_init() during setup to allow the device to utilize
a suitably aligned reserved memory region defined in the Device Tree.

Co-developed-by: Nick Hu <nick.hu@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@sifive.com>
---
 drivers/hwtracing/gtrace/rvtrace-encoder.c |  17 +++++
 drivers/hwtracing/gtrace/rvtrace-funnel.c  |  26 +++++++-
 drivers/hwtracing/gtrace/rvtrace-ramsink.c | 102 +++++++++++++++++++++++++++++
 drivers/hwtracing/gtrace/rvtrace-ramsink.h |  31 +++++++++
 drivers/hwtracing/gtrace/rvtrace-v0.h      |  14 ++++
 5 files changed, 189 insertions(+), 1 deletion(-)

diff --git a/drivers/hwtracing/gtrace/rvtrace-encoder.c b/drivers/hwtracing/gtrace/rvtrace-encoder.c
index 92672f1e7098..8d2f7ac5e0fe 100644
--- a/drivers/hwtracing/gtrace/rvtrace-encoder.c
+++ b/drivers/hwtracing/gtrace/rvtrace-encoder.c
@@ -11,6 +11,7 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 #include "rvtrace.h"
+#include "rvtrace-ramsink.h"
 #include "rvtrace-v0.h"
 
 #define RVTRACE_V0_COMPONENT_CTRL_INSTMODE_OPIT	0x7
@@ -34,6 +35,8 @@
 
 struct rvtrace_encoder_priv {
 	u32 srcbits;
+	/* Sink embedded in a pre-ratified encoder, NULL if not used */
+	struct rvtrace_ramsink_priv *sink;
 };
 
 /*
@@ -112,6 +115,11 @@ static int rvtrace_encoder_probe(struct gtrace_component *comp)
 			val &= ~BIT(RVTRACE_COMPONENT_CTRL_INHIBITSRC_SHIFT);
 			gtrace_write32(pdata, val, RVTRACE_COMPONENT_CTRL_OFFSET);
 		}
+
+		priv->sink = rvtrace_v0_ramsink_setup(comp);
+		if (IS_ERR(priv->sink))
+			return dev_err_probe(&comp->dev, PTR_ERR(priv->sink),
+					     "failed to setup ramsink.\n");
 		goto out;
 	}
 
@@ -151,10 +159,18 @@ static int rvtrace_encoder_probe(struct gtrace_component *comp)
 		val |= BIT(RVTRACE_COMPONENT_CTRL_INHIBITSRC_SHIFT);
 		gtrace_write32(pdata, val, RVTRACE_COMPONENT_CTRL_OFFSET);
 	}
+
 	dev_set_drvdata(&comp->dev, priv);
 	return 0;
 }
 
+static void rvtrace_encoder_remove(struct gtrace_component *comp)
+{
+	struct rvtrace_encoder_priv *priv = dev_get_drvdata(&comp->dev);
+
+	rvtrace_v0_ramsink_release(comp, priv->sink);
+}
+
 static int rvtrace_encoder_start(struct gtrace_path_node *node)
 {
 	struct gtrace_component *comp = node->comp;
@@ -246,6 +262,7 @@ static const struct gtrace_component_id rvtrace_encoder_ids[] = {
 static struct gtrace_driver rvtrace_encoder_driver = {
 	.id_table = rvtrace_encoder_ids,
 	.probe = rvtrace_encoder_probe,
+	.remove = rvtrace_encoder_remove,
 	.start = rvtrace_encoder_start,
 	.stop = rvtrace_encoder_stop,
 	.driver = {
diff --git a/drivers/hwtracing/gtrace/rvtrace-funnel.c b/drivers/hwtracing/gtrace/rvtrace-funnel.c
index 2e0843d233ab..463e504aaa92 100644
--- a/drivers/hwtracing/gtrace/rvtrace-funnel.c
+++ b/drivers/hwtracing/gtrace/rvtrace-funnel.c
@@ -7,10 +7,17 @@
 #include <linux/device.h>
 #include <linux/gtrace.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/types.h>
 #include "rvtrace.h"
+#include "rvtrace-ramsink.h"
 #include "rvtrace-v0.h"
 
+struct rvtrace_funnel_priv {
+	/* Sink embedded in a pre-ratified funnel, NULL if not used */
+	struct rvtrace_ramsink_priv *sink;
+};
+
 static int rvtrace_funnel_start(struct gtrace_path_node *node)
 {
 	struct gtrace_component *comp = node->comp;
@@ -45,22 +52,39 @@ static int rvtrace_funnel_stop(struct gtrace_component *comp)
 
 static int rvtrace_funnel_probe(struct gtrace_component *comp)
 {
+	struct rvtrace_funnel_priv *priv;
 	int ret;
 
+	priv = devm_kzalloc(&comp->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	/* Pre-ratified ramsink setup */
+	priv->sink = rvtrace_v0_ramsink_setup(comp);
+	if (IS_ERR(priv->sink))
+		return dev_err_probe(&comp->dev, PTR_ERR(priv->sink),
+				     "failed to setup ramsink.\n");
+
 	ret = gtrace_enable_component(comp);
-	if (ret)
+	if (ret) {
+		rvtrace_v0_ramsink_release(comp, priv->sink);
 		return dev_err_probe(&comp->dev, ret, "failed to enable funnel.\n");
+	}
 
+	dev_set_drvdata(&comp->dev, priv);
 	return 0;
 }
 
 static void rvtrace_funnel_remove(struct gtrace_component *comp)
 {
+	struct rvtrace_funnel_priv *priv = dev_get_drvdata(&comp->dev);
 	int ret;
 
 	ret = gtrace_disable_component(comp);
 	if (ret)
 		dev_err(&comp->dev, "failed to disable funnel.\n");
+
+	rvtrace_v0_ramsink_release(comp, priv->sink);
 }
 
 static const struct gtrace_component_id rvtrace_funnel_ids[] = {
diff --git a/drivers/hwtracing/gtrace/rvtrace-ramsink.c b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
index a164db57167f..f183dcd71c7c 100644
--- a/drivers/hwtracing/gtrace/rvtrace-ramsink.c
+++ b/drivers/hwtracing/gtrace/rvtrace-ramsink.c
@@ -7,12 +7,16 @@
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/dma-mapping.h>
+#include <linux/export.h>
 #include <linux/gtrace.h>
 #include <linux/minmax.h>
+#include <linux/of_reserved_mem.h>
 #include <linux/types.h>
 #include <linux/sizes.h>
 #include <uapi/linux/perf_event.h>
 #include "rvtrace.h"
+#include "rvtrace-ramsink.h"
+#include "rvtrace-v0.h"
 
 #define RVTRACE_RAMSINK_STARTLOW_OFF		0x010
 #define RVTRACE_RAMSINK_STARTHIGH_OFF		0x014
@@ -348,6 +352,104 @@ static void rvtrace_ramsink_cleanup(struct gtrace_component *comp)
 	dma_free_coherent(comp->pdata->dev, priv->alloc_size, priv->alloc_va, priv->alloc_start);
 }
 
+#ifdef CONFIG_RVTRACE_V0
+/*
+ * Register offsets for the pre-ratified system memory sink embedded in the
+ * trace encoder/funnel. teSinkBaseHigh holds the upper 32 bits of the base,
+ * the limit and the write pointer (shared register).
+ */
+static const struct rvtrace_ramsink_regs rvtrace_v0_sink_regs = {
+	.start_low	= RVTRACE_V0_SINK_BASE_OFF,
+	.start_high	= RVTRACE_V0_SINK_BASE_HIGH_OFF,
+	.limit_low	= RVTRACE_V0_SINK_LIMIT_OFF,
+	.limit_high	= RVTRACE_V0_SINK_BASE_HIGH_OFF,
+	.wp_low		= RVTRACE_V0_SINK_WP_OFF,
+	.wp_high	= RVTRACE_V0_SINK_BASE_HIGH_OFF,
+};
+
+/**
+ * rvtrace_v0_ramsink_setup() - Setup the sink embedded in a pre-ratified
+ * trace encoder/funnel.
+ * @comp: Pre-ratified trace encoder/funnel component.
+ *
+ * Return: Pointer to the sink private data, NULL if the component doesn't
+ * act as a system memory sink, or an ERR_PTR() on failure.
+ */
+struct rvtrace_ramsink_priv *rvtrace_v0_ramsink_setup(struct gtrace_component *comp)
+{
+	struct rvtrace_v0_comp_features *data = comp->id.data;
+	struct gtrace_platform_data *pdata = comp->pdata;
+	struct rvtrace_ramsink_priv *priv;
+	u32 val, sink_bytes;
+	int ret;
+
+	if (!data)
+		return ERR_PTR(-EINVAL);
+
+	/*
+	 * Only components at the end of the topology write trace data to
+	 * system memory, see rvtrace_v0_sink_config().
+	 */
+	if (!data->has_sba_sink || !gtrace_is_sink(pdata))
+		return NULL;
+
+	priv = devm_kzalloc(&comp->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return ERR_PTR(-ENOMEM);
+
+	val = gtrace_read32(pdata, RVTRACE_COMPONENT_IMPL_OFFSET);
+	sink_bytes = (val >> RVTRACE_V0_IMPL_SINKBYTES_SHIFT) & RVTRACE_V0_IMPL_SINKBYTES_MASK;
+
+	priv->regs = &rvtrace_v0_sink_regs;
+	priv->size = SZ_1M;
+	priv->mode = MODE_SMEM;
+	priv->stop_on_wrap = false;
+	priv->mem_acc_width = RVTRACE_RAMSINK_MEM_ACC_WIDTH_MIN << sink_bytes;
+
+	/*
+	 * The pre-ratified ramsink's sink limit and write pointer registers share
+	 * their upper 32 bits with the sink base address register. This hardware
+	 * constraint prevents the trace buffer from crossing a 4GB memory boundary.
+	 *
+	 * Use the optional reserved memory region defined in the Device Tree to
+	 * ensure the buffer allocation adheres to this constraint. A buffer which
+	 * crosses a 4GB boundary is rejected by rvtrace_ramsink_setup_buf().
+	 */
+	ret = of_reserved_mem_device_init(pdata->dev);
+	if (ret && ret != -ENODEV)
+		return ERR_PTR(dev_err_probe(&comp->dev, ret,
+					     "failed to get reserved memory region\n"));
+
+	/* Set comp sink to SBA (system memory) */
+	val = gtrace_read32(pdata, RVTRACE_COMPONENT_CTRL_OFFSET);
+	val &= ~RVTRACE_V0_CTRL_SINK_MASK;
+	val |= TE_SINK_SBA << RVTRACE_V0_CTRL_SINK_SHIFT;
+	val &= ~BIT(RVTRACE_V0_CTRL_STOP_ON_WRAP_SHIFT);
+	val |= priv->stop_on_wrap << RVTRACE_V0_CTRL_STOP_ON_WRAP_SHIFT;
+	gtrace_write32(pdata, val, RVTRACE_COMPONENT_CTRL_OFFSET);
+
+	ret = rvtrace_ramsink_setup_buf(comp, priv);
+	if (ret) {
+		of_reserved_mem_device_release(pdata->dev);
+		return ERR_PTR(ret);
+	}
+
+	return priv;
+}
+EXPORT_SYMBOL_GPL(rvtrace_v0_ramsink_setup);
+
+void rvtrace_v0_ramsink_release(struct gtrace_component *comp,
+				struct rvtrace_ramsink_priv *priv)
+{
+	if (!priv)
+		return;
+
+	rvtrace_ramsink_cleanup(comp);
+	of_reserved_mem_device_release(comp->pdata->dev);
+}
+EXPORT_SYMBOL_GPL(rvtrace_v0_ramsink_release);
+#endif /* CONFIG_RVTRACE_V0 */
+
 static int rvtrace_ramsink_probe(struct gtrace_component *comp)
 {
 	int ret;
diff --git a/drivers/hwtracing/gtrace/rvtrace-ramsink.h b/drivers/hwtracing/gtrace/rvtrace-ramsink.h
new file mode 100644
index 000000000000..0c1b346af8c0
--- /dev/null
+++ b/drivers/hwtracing/gtrace/rvtrace-ramsink.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2026 SiFive, Inc.
+ */
+
+#ifndef __RVTRACE_RAMSINK_H__
+#define __RVTRACE_RAMSINK_H__
+
+#include <linux/err.h>
+#include <linux/gtrace.h>
+
+struct rvtrace_ramsink_priv;
+
+#ifdef CONFIG_RVTRACE_V0
+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);
+#else
+static inline struct rvtrace_ramsink_priv *
+rvtrace_v0_ramsink_setup(struct gtrace_component *comp)
+{
+	return NULL;
+}
+
+static inline void rvtrace_v0_ramsink_release(struct gtrace_component *comp,
+					      struct rvtrace_ramsink_priv *priv)
+{
+}
+#endif
+
+#endif /* __RVTRACE_RAMSINK_H__ */
diff --git a/drivers/hwtracing/gtrace/rvtrace-v0.h b/drivers/hwtracing/gtrace/rvtrace-v0.h
index d1dac2034a14..a554ff938533 100644
--- a/drivers/hwtracing/gtrace/rvtrace-v0.h
+++ b/drivers/hwtracing/gtrace/rvtrace-v0.h
@@ -25,6 +25,16 @@
 #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)
 
+/* Register offsets of the sink embedded in the encoder/funnel */
+#define RVTRACE_V0_SINK_BASE_OFF		0x010
+#define RVTRACE_V0_SINK_BASE_HIGH_OFF		0x014
+#define RVTRACE_V0_SINK_LIMIT_OFF		0x018
+#define RVTRACE_V0_SINK_WP_OFF			0x01c
+#define RVTRACE_V0_SINK_RP_OFF			0x020
+#define RVTRACE_V0_SINK_DATA_OFF		0x024
+
+#define RVTRACE_V0_CTRL_STOP_ON_WRAP_SHIFT	14
+
 /* teControl/tfControl sink selection */
 #define RVTRACE_V0_CTRL_SINK_SHIFT		28
 #define RVTRACE_V0_CTRL_SINK_MASK		GENMASK(31, RVTRACE_V0_CTRL_SINK_SHIFT)
@@ -38,6 +48,10 @@ enum rvtrace_v0_sink {
 	TE_SINK_FUNNEL = 8,
 };
 
+/* Width of the trace sink: 0=32 bit, 1=64 bit, 2=128 bit, 3=256 bit */
+#define RVTRACE_V0_IMPL_SINKBYTES_SHIFT		16
+#define RVTRACE_V0_IMPL_SINKBYTES_MASK		0x3
+
 /* 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

-- 
2.43.7


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 10/10] gtrace: rvtrace: Add copyto_auxbuf callback for pre-ratified encoder and funnel
  2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
                   ` (8 preceding siblings ...)
  2026-11-26  9:07 ` [PATCH 09/10] gtrace: rvtrace: Add pre-ratified ramsink setup Leo Yu-Chi Liang
@ 2026-11-26  9:07 ` Leo Yu-Chi Liang
  9 siblings, 0 replies; 11+ messages in thread
From: Leo Yu-Chi Liang @ 2026-11-26  9:07 UTC (permalink / raw)
  To: Mayuresh Chitale, Anup Patel, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Alexandre Ghiti, Samuel Holland, Alexander Shishkin
  Cc: linux-riscv, devicetree, linux-kernel, Leo Yu-Chi Liang, linmin,
	vincent.chen, luyulin, pritesh.patel, ganboing, dongxuyang,
	pinkesh.vaghela, nick.hu, eric.lin, huangyifeng,
	darshan.prajapati

From: Eric Lin <eric.lin@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@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
Co-developed-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Eric Lin <eric.lin@sifive.com>
Signed-off-by: Leo Yu-Chi Liang <leo.liang@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


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-26  9:07 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  9:07 [PATCH 00/10] gtrace: Add support for SiFive pre-ratified RISC-V trace components Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 01/10] dt-bindings: riscv: Add SiFive pre-ratified " Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 02/10] gtrace: rvtrace: Add pre-ratified implementation callback Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 03/10] gtrace: rvtrace: Add pre-ratified private data callback Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 04/10] gtrace: rvtrace: Add pre-ratified encoder support Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 05/10] gtrace: Terminate id_table iteration on GTRACE_COMPONENT_TYPE_NONE Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 06/10] gtrace: rvtrace: Add pre-ratified trace sink config Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 07/10] gtrace: rvtrace: Add pre-ratified trace funnel driver Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 08/10] gtrace: rvtrace: Introduce struct rvtrace_ramsink_regs to abstract register offsets Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 09/10] gtrace: rvtrace: Add pre-ratified ramsink setup Leo Yu-Chi Liang
2026-11-26  9:07 ` [PATCH 10/10] gtrace: rvtrace: Add copyto_auxbuf callback for pre-ratified encoder and funnel Leo Yu-Chi Liang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®