Skip to contentSkip to conversation

Add Linux RISC-V trace support via CoreSight

RFC PATCH In review Zane Leung 4 participants 21 messages Apr 14 – Jun 8

Subsystems (4)
lore
Share & tools
lore

Zane Leung

author
#0 lore raw

cover Add Linux RISC-V trace support via CoreSight

From: liangzhen <[email protected]>

This series adds Linux RISC-V trace support via CoreSight, implementing RISC-V trace drivers within the CoreSight framework and integrating them with perf tools. The K3 SoC contains RISC-V Encoder, Funnel, ATB, CoreSight Funnel, and CoreSight TMC components, which can be directly leveraged through the existing CoreSight infrastructure.

Linux RISC-V trace support form Anup Patel:
(https://patchwork.kernel.org/project/linux-riscv/cover/[email protected]/)
which currently lacks ATB component support and guidance on reusing CoreSight components.

The series includes:
- RISC-V trace driver implementation within the CoreSight framework
- RISC-V Trace Encoder, Funnel, and ATB Bridge drivers as CoreSight devices
- RISC-V trace PMU record capabilities and parsing events in perf.
- RISC-V Nexus Trace decoder for perf tools

Any comments or suggestions are welcome.

Verification on K3 SoC:
To verify this patch series on K3 hardware, the following device tree are required:
1. RISC-V Trace Encoder node (8)
2. RISC-V ATB Bridge node (8)
3. RISC-V Trace Funnel node (2)
3. CoreSight Funnel configuration for RISC-V (1)
4. CoreSight TMC configuration for trace buffer (1)

/{
        dummy_clk: apb-pclk {
                compatible = "fixed-clock";
                #clock-cells = <0x0>;
                clock-output-names = "clk14mhz";
                clock-frequency = <14000000>;
        };


        soc: soc {
                #address-cells = <2>;
                #size-cells = <2>;

                encoder0: encoder@d9002000 {
                        compatible = "riscv,trace-encoder";
                        reg = <0x0 0xd9002000 0x0 0x1000>;
                        cpus = <&cpu_0>;
                        out-ports {
                                port {
                                        cluster0_encoder0_out_port: endpoint {
                                                remote-endpoint = <&cluster0_bridge0_in_port>;
                                        };
                                };
                        };
                };

                bridge0: bridge@d9003000 {
                        compatible = "riscv,trace-atbbridge";
                        reg = <0x0 0xd9003000 0x0 0x1000>;
                        cpus = <&cpu_0>;
                        out-ports {
                                port {
                                        cluster0_bridge0_out_port: endpoint {
                                                remote-endpoint = <&cluster0_funnel_in_port0>;
                                        };
                                };
                        };
                        in-ports {
                                port {
                                        cluster0_bridge0_in_port: endpoint {
                                                remote-endpoint = <&cluster0_encoder0_out_port>;
                                        };
                                };
                        };
                };

...

                cluster0_funnel: funnel@d9000000 {
                        compatible = "riscv,trace-funnel";
                        reg = <0x0 0xd9000000 0x0 0x1000>;
                        cpus = <&cpu_0>, <&cpu_1>, <&cpu_2>, <&cpu_3>;
                        riscv,timestamp-present;
                        out-ports {
                                port {
                                        cluster0_funnel_out_port: endpoint {
                                                remote-endpoint = <&main_funnel_in_port0>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        cluster0_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster0_bridge0_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        cluster0_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster0_bridge1_out_port>;
                                        };
                                };

                                port@2 {
                                        reg = <2>;
                                        cluster0_funnel_in_port2: endpoint {
                                                remote-endpoint = <&cluster0_bridge2_out_port>;
                                        };
                                };

                                port@3 {
                                        reg = <3>;
                                        cluster0_funnel_in_port3: endpoint {
                                                remote-endpoint = <&cluster0_bridge3_out_port>;
                                        };
                                };
                        };
                };

                cluster1_funnel: funnel@d9010000 {
                        compatible = "riscv,trace-funnel";
                        reg = <0x0 0xd9010000 0x0 0x1000>;
                        cpus = <&cpu_4>, <&cpu_5>, <&cpu_6>, <&cpu_7>;
                        riscv,timestamp-present;
                        out-ports {
                                port {
                                        cluster1_funnel_out_port: endpoint {
                                                remote-endpoint = <&main_funnel_in_port1>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        cluster1_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster1_bridge0_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        cluster1_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster1_bridge1_out_port>;
                                        };
                                };

                                port@2 {
                                        reg = <2>;
                                        cluster1_funnel_in_port2: endpoint {
                                                remote-endpoint = <&cluster1_bridge2_out_port>;
                                        };
                                };

                                port@3 {
                                        reg = <3>;
                                        cluster1_funnel_in_port3: endpoint {
                                                remote-endpoint = <&cluster1_bridge3_out_port>;
                                        };
                                };
                        };
                };

                main_funnel: funnel@d9042000 {
                        compatible = "arm,coresight-dynamic-funnel", "arm,primecell";
                        reg = <0x0 0xd9042000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        out-ports {
                                port {
                                        main_funnel_out_port: endpoint {
                                                remote-endpoint = <&etf_in_port>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        main_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster0_funnel_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        main_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster1_funnel_out_port>;
                                        };
                                };
                        };
                };

                etf: etf@d9043000 {
                        compatible = "arm,coresight-tmc", "arm,primecell";
                        reg = <0x0 0xd9043000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        out-ports {
                                port {
                                        etf_out_port: endpoint {
                                                remote-endpoint = <&etr_in_port>;
                                        };
                                };
                        };
                        in-ports {
                                port {
                                        etf_in_port: endpoint {
                                                remote-endpoint = <&main_funnel_out_port>;
                                        };
                                };
                        };
                };

                etr: etr@d9044000 {
                        compatible = "arm,coresight-tmc", "arm,primecell";
                        reg = <0x0 0xd9044000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        arm,scatter-gather;
                        in-ports {
                                port {
                                        etr_in_port: endpoint {
                                                remote-endpoint = <&etf_out_port>;
                                        };
                                };
                        };
                };
        };
};

Verification case:

~ # perf list pmu
rvtrace// [Kernel PMU event]

~ # perf record -e rvtrace/@tmc_etr0/ --per-thread uname Linux
[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.191 MB perf.data ] ~ # perf script

           uname     137 [003]          1           branches:  ffffffff80931470 rvtrace_poll_bit+0x38 ([kernel.kallsyms]) => ffffffff80931492 rvtrace_poll_bit+0x5a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff809328a6 encoder_enable_hw+0x252 ([kernel.kallsyms]) => ffffffff809328ba encoder_enable_hw+0x266 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff80932c4a encoder_enable+0x82 ([kernel.kallsyms]) => ffffffff80932c36 encoder_enable+0x6e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff80928198 etm_event_start+0xf0 ([kernel.kallsyms]) => ffffffff809281aa etm_event_start+0x102 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff809281e6 etm_event_start+0x13e ([kernel.kallsyms]) => ffffffff8092755e coresight_get_sink_id+0x16 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8092820e etm_event_start+0x166 ([kernel.kallsyms]) => ffffffff80928226 etm_event_start+0x17e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3bb4 perf_report_aux_output_id+0x0 ([kernel.kallsyms]) => ffffffff801c3bd6 perf_report_aux_output_id+0x22 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3c5a perf_report_aux_output_id+0xa6 ([kernel.kallsyms]) => ffffffff801c3bf0 perf_report_aux_output_id+0x3c ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3c40 perf_report_aux_output_id+0x8c ([kernel.kallsyms]) => ffffffff801c3aea __perf_event_header__init_id+0x2a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3b42 __perf_event_header__init_id+0x82 ([kernel.kallsyms]) => ffffffff801c3b4a __perf_event_header__init_id+0x8a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3bb0 __perf_event_header__init_id+0xf0 ([kernel.kallsyms]) => ffffffff801c3b58 __perf_event_header__init_id+0x98 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c658 __task_pid_nr_ns+0x0 ([kernel.kallsyms]) => ffffffff8004c696 __task_pid_nr_ns+0x3e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c71e __task_pid_nr_ns+0xc6 ([kernel.kallsyms]) => ffffffff8004c6a4 __task_pid_nr_ns+0x4c ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms]) => ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms])

...

liangzhen (12):
coresight: Add RISC-V support to CoreSight tracing
coresight: Initial implementation of RISC-V trace driver
coresight: Add RISC-V Trace Encoder driver
coresight: Add RISC-V Trace Funnel driver
coresight: Add RISC-V Trace ATB Bridge driver
coresight rvtrace: Add timestamp component support for encoder and
funnel
coresight: Add RISC-V PMU name support
perf tools: riscv: making rvtrace PMU listable
perf tools: Add RISC-V trace PMU record capabilities perf tools: Add Nexus RISC-V Trace decoder
perf symbols: Add RISC-V PLT entry sizes
perf tools: Integrate RISC-V trace decoder into auxtrace

 drivers/hwtracing/Kconfig                     |    2 +
 drivers/hwtracing/coresight/Kconfig           |   46 +-
 drivers/hwtracing/coresight/Makefile          |    6 +
 drivers/hwtracing/coresight/coresight-core.c  |    8 +
 .../hwtracing/coresight/coresight-etm-perf.c  |    1 -
 .../hwtracing/coresight/coresight-etm-perf.h  |   21 +
 .../hwtracing/coresight/coresight-platform.c  |    1 -
 .../hwtracing/coresight/coresight-tmc-etf.c   |    4 +
 .../hwtracing/coresight/coresight-tmc-etr.c   |    4 +
 .../hwtracing/coresight/rvtrace-atbbridge.c   |  239 +++
 drivers/hwtracing/coresight/rvtrace-core.c    |  135 ++
 .../coresight/rvtrace-encoder-core.c          |  562 +++++++
 .../coresight/rvtrace-encoder-sysfs.c         |  363 +++++
 drivers/hwtracing/coresight/rvtrace-encoder.h |  151 ++
 drivers/hwtracing/coresight/rvtrace-funnel.c  |  337 ++++
 drivers/hwtracing/coresight/rvtrace-funnel.h  |   39 +
 .../hwtracing/coresight/rvtrace-timestamp.c   |  278 ++++
 .../hwtracing/coresight/rvtrace-timestamp.h   |   64 +
 include/linux/coresight-pmu.h                 |    4 +
 include/linux/rvtrace.h                       |  116 ++
 tools/arch/riscv/include/asm/insn.h           |  645 ++++++++
 tools/perf/arch/riscv/util/Build              |    2 +
 tools/perf/arch/riscv/util/auxtrace.c         |  490 ++++++
 tools/perf/arch/riscv/util/pmu.c              |   20 +
 tools/perf/util/Build                         |    3 +
 tools/perf/util/auxtrace.c                    |    4 +
 tools/perf/util/auxtrace.h                    |    1 +
 tools/perf/util/nexus-rv-decoder/Build        |    1 +
 .../util/nexus-rv-decoder/nexus-rv-decoder.c  | 1364 +++++++++++++++++
 .../util/nexus-rv-decoder/nexus-rv-decoder.h  |  139 ++
 .../perf/util/nexus-rv-decoder/nexus-rv-msg.h |  190 +++
 tools/perf/util/rvtrace-decoder.c             | 1039 +++++++++++++
 tools/perf/util/rvtrace.h                     |   40 +
 tools/perf/util/symbol-elf.c                  |    4 +
 34 files changed, 6320 insertions(+), 3 deletions(-)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-atbbridge.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-core.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-core.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder.h
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.h
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.h
 create mode 100644 include/linux/rvtrace.h
 create mode 100644 tools/arch/riscv/include/asm/insn.h
 create mode 100644 tools/perf/arch/riscv/util/auxtrace.c
 create mode 100644 tools/perf/arch/riscv/util/pmu.c
 create mode 100644 tools/perf/util/nexus-rv-decoder/Build
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h
 create mode 100644 tools/perf/util/rvtrace-decoder.c
 create mode 100644 tools/perf/util/rvtrace.h
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#1 lore raw

RFC PATCH 1/12 coresight: Add RISC-V support to CoreSight tracing

From: liangzhen <[email protected]>

Enable CoreSight tracing support on RISC-V architecture by:
- Adding RISC-V to Kconfig dependencies for CoreSight
- Replacing ARM-specific memory barriers (isb, dmb) with RISC-V equivalents (local_flush_icache_all, __mb)
- Removing ARM-specific header dependencies:
perf/arm_pmu.h, asm/smp_plat.h
- Adding PMU format attribute macros for cross-architecture support

This allows CoreSight tracing infrastructure to work on RISC-V systems while maintaining compatibility with existing ARM/ARM64 implementations.

---
 drivers/hwtracing/Kconfig                     |  2 ++
 drivers/hwtracing/coresight/Kconfig           |  2 +-
 drivers/hwtracing/coresight/coresight-core.c  |  8 +++++++
 .../hwtracing/coresight/coresight-etm-perf.c  |  1 -
 .../hwtracing/coresight/coresight-etm-perf.h  | 21 +++++++++++++++++++
 .../hwtracing/coresight/coresight-platform.c  |  1 -
 .../hwtracing/coresight/coresight-tmc-etf.c   |  4 ++++
 .../hwtracing/coresight/coresight-tmc-etr.c   |  4 ++++
 8 files changed, 40 insertions(+), 3 deletions(-)
drivers/hwtracing/Kconfig +2 -0
index 911ee977103c..167ff172dd72 100644
@@ -1,6 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0-only
 menu "HW tracing support"
 
+source "drivers/hwtracing/coresight/Kconfig"
+
 source "drivers/hwtracing/stm/Kconfig"
 
 source "drivers/hwtracing/intel_th/Kconfig"
drivers/hwtracing/coresight/Kconfig +1 -1
index 6a4239ebb582..2b1ebe3f614d 100644
@@ -4,7 +4,7 @@
 #
 menuconfig CORESIGHT
 	tristate "CoreSight Tracing Support"
-	depends on ARM || ARM64
+	depends on ARM || ARM64 || RISCV
 	depends on OF || ACPI
 	select ARM_AMBA
 	select PERF_EVENTS
drivers/hwtracing/coresight/coresight-core.c +8 -0
index 80e26396ad0a..1ca202153cc4 100644
@@ -141,7 +141,11 @@ static void coresight_set_self_claim_tag_unlocked(struct coresight_device *csdev
 {
 	csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED,
 				     CORESIGHT_CLAIMSET);
+#if defined(__riscv)
+	local_flush_icache_all();
+#else
 	isb();
+#endif
 }
 
 void coresight_clear_self_claim_tag(struct csdev_access *csa)
@@ -158,7 +162,11 @@ void coresight_clear_self_claim_tag_unlocked(struct csdev_access *csa)
 {
 	csdev_access_relaxed_write32(csa, CORESIGHT_CLAIM_SELF_HOSTED,
 				     CORESIGHT_CLAIMCLR);
+#if defined(__riscv)
+	local_flush_icache_all();
+#else
 	isb();
+#endif
 }
 EXPORT_SYMBOL_GPL(coresight_clear_self_claim_tag_unlocked);
 
drivers/hwtracing/coresight/coresight-etm-perf.c +0 -1
index 72017dcc3b7f..70a6aaffbf9d 100644
@@ -13,7 +13,6 @@
 #include <linux/mm.h>
 #include <linux/init.h>
 #include <linux/perf_event.h>
-#include <linux/perf/arm_pmu.h>
 #include <linux/percpu-defs.h>
 #include <linux/slab.h>
 #include <linux/stringhash.h>
drivers/hwtracing/coresight/coresight-etm-perf.h +21 -0
index 24d929428633..e48c0ad46db1 100644
@@ -58,6 +58,27 @@ struct cscfg_config_desc;
 #define ATTR_CFG_FLD_cc_threshold_LO		0
 #define ATTR_CFG_FLD_cc_threshold_HI		11
 
+#define __GEN_PMU_FORMAT_ATTR(cfg, lo, hi)			\
+	(lo) == (hi) ? #cfg ":" #lo "\n" : #cfg ":" #lo "-" #hi
+
+#define _GEN_PMU_FORMAT_ATTR(cfg, lo, hi)			\
+	__GEN_PMU_FORMAT_ATTR(cfg, lo, hi)
+
+#define GEN_PMU_FORMAT_ATTR(name)				\
+	PMU_FORMAT_ATTR(name,					\
+	_GEN_PMU_FORMAT_ATTR(ATTR_CFG_FLD_##name##_CFG,		\
+			     ATTR_CFG_FLD_##name##_LO,		\
+			     ATTR_CFG_FLD_##name##_HI))
+
+#define _ATTR_CFG_GET_FLD(attr, cfg, lo, hi)			\
+	((((attr)->cfg) >> lo) & GENMASK_ULL(hi - lo, 0))
+
+#define ATTR_CFG_GET_FLD(attr, name)				\
+	_ATTR_CFG_GET_FLD(attr,					\
+			  ATTR_CFG_FLD_##name##_CFG,		\
+			  ATTR_CFG_FLD_##name##_LO,		\
+			  ATTR_CFG_FLD_##name##_HI)
+
 /**
  * struct etm_filter - single instruction range or start/stop configuration.
  * @start_addr:	The address to start tracing on.
drivers/hwtracing/coresight/coresight-platform.c +0 -1
index 0db64c5f4995..261ba6a75b86 100644
@@ -14,7 +14,6 @@
 #include <linux/amba/bus.h>
 #include <linux/coresight.h>
 #include <linux/cpumask.h>
-#include <asm/smp_plat.h>
 
 #include "coresight-priv.h"
 
drivers/hwtracing/coresight/coresight-tmc-etf.c +4 -0
index 8882b1c4cdc0..dc366f4a5ca8 100644
@@ -661,7 +661,11 @@ static int tmc_panic_sync_etf(struct coresight_device *csdev)
 	 * Make sure all previous writes are ordered,
 	 * before we mark valid
 	 */
+#if defined(__riscv)
+	__mb();
+#else
 	dmb(sy);
+#endif
 	mdata->valid = true;
 	/*
 	 * Below order need to maintained, since crc of metadata
drivers/hwtracing/coresight/coresight-tmc-etr.c +4 -0
index 4dc1defe27a5..ac379d1751e6 100644
@@ -1883,7 +1883,11 @@ static int tmc_panic_sync_etr(struct coresight_device *csdev)
 	 * Make sure all previous writes are ordered,
 	 * before we mark valid
 	 */
+#if defined(__riscv)
+	__mb();
+#else
 	dmb(sy);
+#endif
 	mdata->valid = true;
 	/*
 	 * Below order need to maintained, since crc of metadata
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#2 lore raw

RFC PATCH 2/12 coresight: Initial implementation of RISC-V trace driver

In reply to Zane Leung

From: liangzhen <[email protected]>

Implement some common driver interfaces RISC-V trace where RISC-V trace components are instantiated by a common platform driver and a separate RISC-V trace driver for each type of RISC-V trace component.

---
 drivers/hwtracing/coresight/Kconfig        |   8 ++
 drivers/hwtracing/coresight/Makefile       |   2 +
 drivers/hwtracing/coresight/rvtrace-core.c | 135 +++++++++++++++++++++
 include/linux/rvtrace.h                    | 116 ++++++++++++++++++
 4 files changed, 261 insertions(+)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-core.c
 create mode 100644 include/linux/rvtrace.h
drivers/hwtracing/coresight/Kconfig +8 -0
index 2b1ebe3f614d..5adeaf78a080 100644
@@ -280,4 +280,12 @@ config CORESIGHT_TNOC
 	  To compile this driver as a module, choose M here: the module will be
 	  called coresight-tnoc.
 
+config RVTRACE
+	bool "RISC-V Trace Support"
+	help
+	  This enables support for the RISC-V trace drivers. drivers
+	  (including Trace Encoder, Trace Funnel and  ATB Bridge) are
+	  dynamically aggregated with CoreSight trace infrastructure
+	  at run time to form a complete trace path.
+
 endif
drivers/hwtracing/coresight/Makefile +2 -0
index ab16d06783a5..c21a9e25e148 100644
@@ -57,3 +57,5 @@ obj-$(CONFIG_CORESIGHT_DUMMY) += coresight-dummy.o
 obj-$(CONFIG_CORESIGHT_CTCU) += coresight-ctcu.o
 coresight-ctcu-y := coresight-ctcu-core.o
 obj-$(CONFIG_CORESIGHT_KUNIT_TESTS) += coresight-kunit-tests.o
+obj-$(CONFIG_RVTRACE) += rvtrace.o
+rvtrace-y := rvtrace-core.o
drivers/hwtracing/coresight/rvtrace-core.c new file +135 -0

135 lines not on this page · raw message

include/linux/rvtrace.h new file +116 -0

116 lines not on this page · raw message

Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#3 lore raw

RFC PATCH 3/12 coresight: Add RISC-V Trace Encoder driver

In reply to Zane Leung

From: liangzhen <[email protected]>

Add initial implementation of RISC-V trace encoder driver. The encoder is defined in the RISC-V Trace Control Interface specification.

---
 drivers/hwtracing/coresight/Kconfig           |  12 +
 drivers/hwtracing/coresight/Makefile          |   2 +
 .../coresight/rvtrace-encoder-core.c          | 524 ++++++++++++++++++
 .../coresight/rvtrace-encoder-sysfs.c         | 313 +++++++++++
 drivers/hwtracing/coresight/rvtrace-encoder.h | 143 +++++
 5 files changed, 994 insertions(+)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-core.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder.h
drivers/hwtracing/coresight/Kconfig +12 -0
index 5adeaf78a080..3e46728f2482 100644
@@ -288,4 +288,16 @@ config RVTRACE
 	  dynamically aggregated with CoreSight trace infrastructure
 	  at run time to form a complete trace path.
 
+config RVTRACE_ENCODER
+	tristate "RISCV Trace Encoder driver"
+	depends on RVTRACE
+	help
+	  This driver provides support for the Trace Encoder module, tracing
+	  the instructions that a processor is executing. This is primarily
+	  useful for instruction level tracing. Depending on the implemented
+	  version data tracing may also be available.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called rvtrace-encoder.
+
 endif
drivers/hwtracing/coresight/Makefile +2 -0
index c21a9e25e148..9a526b1fb95a 100644
@@ -59,3 +59,5 @@ coresight-ctcu-y := coresight-ctcu-core.o
 obj-$(CONFIG_CORESIGHT_KUNIT_TESTS) += coresight-kunit-tests.o
 obj-$(CONFIG_RVTRACE) += rvtrace.o
 rvtrace-y := rvtrace-core.o
+obj-$(CONFIG_RVTRACE_ENCODER) += rvtrace-encoder.o
+rvtrace-encoder-y := rvtrace-encoder-core.o rvtrace-encoder-sysfs.o
drivers/hwtracing/coresight/rvtrace-encoder-core.c new file +524 -0

524 lines not on this page · raw message

drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c new file +313 -0

313 lines not on this page · raw message

drivers/hwtracing/coresight/rvtrace-encoder.h new file +143 -0

143 lines not on this page · raw message

Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#4 lore raw

RFC PATCH 4/12 coresight: Add RISC-V Trace Funnel driver

In reply to Zane Leung

From: liangzhen <[email protected]>

Add initial implementation of RISC-V trace funnel
driver. The funnel is defined in the RISC-V Trace
Control Interface specification.

---
 drivers/hwtracing/coresight/Kconfig          |  12 +
 drivers/hwtracing/coresight/Makefile         |   1 +
 drivers/hwtracing/coresight/rvtrace-funnel.c | 244 +++++++++++++++++++
 drivers/hwtracing/coresight/rvtrace-funnel.h |  31 +++
 4 files changed, 288 insertions(+)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.h
drivers/hwtracing/coresight/Kconfig +12 -0
index 3e46728f2482..8381a84e2699 100644
@@ -288,9 +288,21 @@ config RVTRACE
 	  dynamically aggregated with CoreSight trace infrastructure
 	  at run time to form a complete trace path.
 
+config RVTRACE_FUNNEL
+	tristate "RISCV Trace Funnel driver"
+	depends on RVTRACE
+	help
+	  his driver provides support for the Trace Funnel driver. The Funnel
+	  aggregates the trace from each of its inputs and sends the combined
+	  trace stream to its designated Trace Sink or ATB Bridge.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called rvtrace-funnel.
+
 config RVTRACE_ENCODER
 	tristate "RISCV Trace Encoder driver"
 	depends on RVTRACE
+	select RVTRACE_FUNNEL
 	help
 	  This driver provides support for the Trace Encoder module, tracing
 	  the instructions that a processor is executing. This is primarily
drivers/hwtracing/coresight/Makefile +1 -0
index 9a526b1fb95a..409a0abf0642 100644
@@ -59,5 +59,6 @@ coresight-ctcu-y := coresight-ctcu-core.o
 obj-$(CONFIG_CORESIGHT_KUNIT_TESTS) += coresight-kunit-tests.o
 obj-$(CONFIG_RVTRACE) += rvtrace.o
 rvtrace-y := rvtrace-core.o
+obj-$(CONFIG_RVTRACE_FUNNEL) += rvtrace-funnel.o
 obj-$(CONFIG_RVTRACE_ENCODER) += rvtrace-encoder.o
 rvtrace-encoder-y := rvtrace-encoder-core.o rvtrace-encoder-sysfs.o
drivers/hwtracing/coresight/rvtrace-funnel.c new file +244 -0

244 lines not on this page · raw message

drivers/hwtracing/coresight/rvtrace-funnel.h new file +31 -0
new file mode 100644
index 000000000000..e5247e6c1bf5
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright(C) 2026 Spacemit Limited. All rights reserved.
+ */
+
+#ifndef _RVTRACE_FUNNEL_H
+#define _RVTRACE_FUNNEL_H
+
+#include <linux/spinlock.h>
+#include <linux/coresight.h>
+
+/* Disable Individual Funnel Inputs */
+#define RVTRACE_FUNNEL_DISINPUT_OFFSET			0x008
+#define RVTRACE_FUNNEL_DISINPUT_MASK			0xffff
+
+/**
+ * struct funnel_data - specifics associated to a Trace Funnel component
+ * @csdev:        Component vitals needed by the framework.
+ * @spinlock:     Only one at a time pls.
+ * @was_enabled:  Flag showing whether the Trace Funnel was enabled.
+ * @input_refcnt: Record the number of funnel inputs
+ */
+struct funnel_data {
+	struct coresight_device	*csdev;
+	spinlock_t		spinlock;
+	bool			was_enabled;
+	u32			input_refcnt;
+	u32			disintput;
+};
+
+#endif
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#5 lore raw

RFC PATCH 5/12 coresight: Add RISC-V Trace ATB Bridge driver

In reply to Zane Leung

From: liangzhen <[email protected]>

Add initial implementation of RISC-V trace ATB
Bridge driver. The ATB Bridge is defined in the
RISC-V Trace Control Interface specification.

---
 drivers/hwtracing/coresight/Kconfig           |  12 +
 drivers/hwtracing/coresight/Makefile          |   1 +
 .../hwtracing/coresight/rvtrace-atbbridge.c   | 239 ++++++++++++++++++
 3 files changed, 252 insertions(+)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-atbbridge.c
drivers/hwtracing/coresight/Kconfig +12 -0
index 8381a84e2699..1cd88a1a7d08 100644
@@ -288,6 +288,17 @@ config RVTRACE
 	  dynamically aggregated with CoreSight trace infrastructure
 	  at run time to form a complete trace path.
 
+config RVTRACE_ATBBRIDGE
+	tristate "RISCV Trace ATB Bridge driver"
+	depends on RVTRACE
+	help
+	  This driver provides support for the ATB Bridge driver. The ATB
+	  Bridge allows sending RISC-V trace to Arm CoreSight infrastructure
+	  (instead of RISC-V compliant sink) as an ATB initiator.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called rvtrace-atbbridge.
+
 config RVTRACE_FUNNEL
 	tristate "RISCV Trace Funnel driver"
 	depends on RVTRACE
@@ -303,6 +314,7 @@ config RVTRACE_ENCODER
 	tristate "RISCV Trace Encoder driver"
 	depends on RVTRACE
 	select RVTRACE_FUNNEL
+	select RVTRACE_ATBBRIDGE
 	help
 	  This driver provides support for the Trace Encoder module, tracing
 	  the instructions that a processor is executing. This is primarily
drivers/hwtracing/coresight/Makefile +1 -0
index 409a0abf0642..2264a313a773 100644
@@ -59,6 +59,7 @@ coresight-ctcu-y := coresight-ctcu-core.o
 obj-$(CONFIG_CORESIGHT_KUNIT_TESTS) += coresight-kunit-tests.o
 obj-$(CONFIG_RVTRACE) += rvtrace.o
 rvtrace-y := rvtrace-core.o
+obj-$(CONFIG_RVTRACE_ATBBRIDGE) += rvtrace-atbbridge.o
 obj-$(CONFIG_RVTRACE_FUNNEL) += rvtrace-funnel.o
 obj-$(CONFIG_RVTRACE_ENCODER) += rvtrace-encoder.o
 rvtrace-encoder-y := rvtrace-encoder-core.o rvtrace-encoder-sysfs.o
drivers/hwtracing/coresight/rvtrace-atbbridge.c new file +239 -0

239 lines not on this page · raw message

Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#6 lore raw

RFC PATCH 6/12 coresight rvtrace: Add timestamp component support for encoder and funnel

In reply to Zane Leung

From: liangzhen <[email protected]>

Implement timestamp as a configurable sub-component for both encoder and funnel drivers.

---
 drivers/hwtracing/coresight/Makefile          |   2 +-
 .../coresight/rvtrace-encoder-core.c          |  42 ++-
 .../coresight/rvtrace-encoder-sysfs.c         |  50 ++++
 drivers/hwtracing/coresight/rvtrace-encoder.h |   8 +
 drivers/hwtracing/coresight/rvtrace-funnel.c  |  95 +++++-
 drivers/hwtracing/coresight/rvtrace-funnel.h  |   8 +
 .../hwtracing/coresight/rvtrace-timestamp.c   | 278 ++++++++++++++++++
 .../hwtracing/coresight/rvtrace-timestamp.h   |  64 ++++
 8 files changed, 543 insertions(+), 4 deletions(-)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.h
drivers/hwtracing/coresight/Makefile +1 -1
index 2264a313a773..3ad1b0399f86 100644
@@ -58,7 +58,7 @@ obj-$(CONFIG_CORESIGHT_CTCU) += coresight-ctcu.o
 coresight-ctcu-y := coresight-ctcu-core.o
 obj-$(CONFIG_CORESIGHT_KUNIT_TESTS) += coresight-kunit-tests.o
 obj-$(CONFIG_RVTRACE) += rvtrace.o
-rvtrace-y := rvtrace-core.o
+rvtrace-y := rvtrace-core.o rvtrace-timestamp.o
 obj-$(CONFIG_RVTRACE_ATBBRIDGE) += rvtrace-atbbridge.o
 obj-$(CONFIG_RVTRACE_FUNNEL) += rvtrace-funnel.o
 obj-$(CONFIG_RVTRACE_ENCODER) += rvtrace-encoder.o
drivers/hwtracing/coresight/rvtrace-encoder-core.c +40 -2

81 lines not on this page · raw message

drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c +50 -0

81 lines not on this page · raw message

drivers/hwtracing/coresight/rvtrace-encoder.h +8 -0
index 44f2066ad869..7a0ada11de8d 100644
@@ -9,6 +9,7 @@
 #include <asm/local.h>
 #include <linux/spinlock.h>
 #include "coresight-priv.h"
+#include "rvtrace-timestamp.h"
 
 /* Trace Encoder Control Register */
 #define RVTRACE_ENCODER_ITRACE				    BIT(2)
@@ -126,7 +127,10 @@ struct encoder_config {
  * @spinlock:	    Only one at a time pls.
  * @sticky_enable:  True if trace encoder base configuration has been done.
  * @boot_enable:    True if we should start tracing at boot time.
+ * @has_timestamp:  True if this encoder has timestamp component.
+ * @ts_ctrl:        Controls the insertion of global timestamps in the trace streams.
  * @config:	    Structure holding configuration parameters.
+ * @ts_config:	    Timestamp configuration.
  */
 
 struct encoder_data {
@@ -134,10 +138,14 @@ struct encoder_data {
 	spinlock_t			spinlock;
 	bool				sticky_enable;
 	bool				boot_enable;
+	bool				has_timestamp;
+	bool				ts_ctrl;
 	struct encoder_config		config;
+	struct timestamp_config		ts_config;
 };
 
 extern const struct attribute_group *trace_encoder_groups[];
+extern const struct attribute *timestamp_attrs[];
 void encoder_set_default(struct rvtrace_component *comp);
 
 #endif
drivers/hwtracing/coresight/rvtrace-funnel.c +94 -1

136 lines not on this page · raw message

drivers/hwtracing/coresight/rvtrace-funnel.h +8 -0
index e5247e6c1bf5..34394f775faa 100644
@@ -19,6 +19,9 @@
  * @spinlock:     Only one at a time pls.
  * @was_enabled:  Flag showing whether the Trace Funnel was enabled.
  * @input_refcnt: Record the number of funnel inputs
+ * @has_timestamp: True if this funnel has timestamp component.
+ * @ts_ctrl:      Controls the insertion of global timestamps in the trace streams.
+ * @ts_config:    Timestamp configuration.
  */
 struct funnel_data {
 	struct coresight_device	*csdev;
@@ -26,6 +29,11 @@ struct funnel_data {
 	bool			was_enabled;
 	u32			input_refcnt;
 	u32			disintput;
+	bool			has_timestamp;
+	bool			ts_ctrl;
+	struct timestamp_config	ts_config;
 };
 
+extern const struct attribute *timestamp_attrs[];
+
 #endif
drivers/hwtracing/coresight/rvtrace-timestamp.c new file +278 -0

278 lines not on this page · raw message

drivers/hwtracing/coresight/rvtrace-timestamp.h new file +64 -0

64 lines not on this page · raw message

Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#7 lore raw

RFC PATCH 7/12 coresight: Add RISC-V PMU name support

In reply to Zane Leung

From: liangzhen <[email protected]>

Define CORESIGHT_ETM_PMU_NAME based on architecture:
- Set to "rvtrace" when CONFIG_RVTRACE is enabled
- Default to "cs_etm" for ARM/ARM64 systems

---
 include/linux/coresight-pmu.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/coresight-pmu.h +4 -0
index 2e179abe472a..119565500ec5 100644
@@ -9,7 +9,11 @@
 
 #include <linux/bits.h>
 
+#if IS_ENABLED(CONFIG_RVTRACE)
+#define CORESIGHT_ETM_PMU_NAME "rvtrace"
+#else
 #define CORESIGHT_ETM_PMU_NAME "cs_etm"
+#endif
 
 /*
  * The legacy Trace ID system based on fixed calculation from the cpu
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#8 lore raw

RFC PATCH 8/12 perf tools: riscv: making rvtrace PMU listable

In reply to Zane Leung

From: liangzhen <[email protected]>

This commit enhances the RISC-V performance tools by allowing the rvtrace PMU to be listed and selected when auxiliary trace support is enabled.

---
 tools/perf/arch/riscv/util/Build |  2 ++
 tools/perf/arch/riscv/util/pmu.c | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 tools/perf/arch/riscv/util/pmu.c
tools/perf/arch/riscv/util/Build +2 -0
index 2328fb9a30a3..748068a3a5c5 100644
@@ -1 +1,3 @@
 perf-util-y += header.o
+
+perf-util-y += pmu.o
tools/perf/arch/riscv/util/pmu.c new file +20 -0
new file mode 100644
index 000000000000..71ad1c8884d0
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright(C) 2026 Spacemit Limited. All rights reserved.
+ */
+
+#include <string.h>
+#include <linux/perf_event.h>
+
+#include "../../../util/pmu.h"
+
+#define RVTRACE_PMU_NAME "rvtrace"
+
+void perf_pmu__arch_init(struct perf_pmu *pmu)
+{
+	if (!strcmp(pmu->name, RVTRACE_PMU_NAME)) {
+		pmu->auxtrace = true;
+		pmu->selectable = true;
+	}
+}
+
Signature · 1 line
2.34.1
author
#9 lore raw

RFC PATCH 9/12 perf tools: Add RISC-V trace PMU record capabilities

In reply to Zane Leung

From: liangzhen <[email protected]>

Introduce the required auxiliary API functions allowing the perf core to interact with RISC-V trace perf driver.

---
 tools/perf/arch/riscv/util/Build      |   2 +-
 tools/perf/arch/riscv/util/auxtrace.c | 489 ++++++++++++++++++++++++++
 tools/perf/util/auxtrace.c            |   1 +
 tools/perf/util/auxtrace.h            |   1 +
 tools/perf/util/rvtrace.h             |  38 ++
 5 files changed, 530 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/arch/riscv/util/auxtrace.c
 create mode 100644 tools/perf/util/rvtrace.h
tools/perf/arch/riscv/util/Build +1 -1
index 748068a3a5c5..10f41f97bc2e 100644
@@ -1,3 +1,3 @@
 perf-util-y += header.o
 
-perf-util-y += pmu.o
+perf-util-y += pmu.o auxtrace.o
tools/perf/arch/riscv/util/auxtrace.c new file +489 -0

489 lines not on this page · raw message

tools/perf/util/auxtrace.c +1 -0
index a224687ffbc1..944a43d48739 100644
@@ -1411,6 +1411,7 @@ int perf_event__process_auxtrace_info(const struct perf_tool *tool __maybe_unuse
 	case PERF_AUXTRACE_VPA_DTL:
 		err = powerpc_vpadtl_process_auxtrace_info(event, session);
 		break;
+	case PERF_AUXTRACE_RISCV_TRACE:
 	case PERF_AUXTRACE_UNKNOWN:
 	default:
 		return -EINVAL;
tools/perf/util/auxtrace.h +1 -0
index 6947f3f284c0..4f4714c1b53f 100644
@@ -46,6 +46,7 @@ enum auxtrace_type {
 	PERF_AUXTRACE_S390_CPUMSF,
 	PERF_AUXTRACE_HISI_PTT,
 	PERF_AUXTRACE_VPA_DTL,
+	PERF_AUXTRACE_RISCV_TRACE,
 };
 
 enum itrace_period_type {
tools/perf/util/rvtrace.h new file +38 -0
new file mode 100644
index 000000000000..1e48ed989dd7
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright(C) 2026 Spacemit Limited. All rights reserved.
+ */
+
+#ifndef INCLUDE__UTIL_PERF_RVTRACE_H__
+#define INCLUDE__UTIL_PERF_RVTRACE_H__
+
+#include "debug.h"
+#include "auxtrace.h"
+#include "util/event.h"
+#include "util/session.h"
+#include <linux/bits.h>
+
+enum {
+	/* PMU->type (32 bit), total # of CPUs (32 bit) */
+	RVTRACE_PMU_TYPE_CPUS,
+	RVTRACE_HEADER_MAX,
+};
+
+/* Trace Encoder metadata */
+enum {
+	RVTRACE_ENCODER_CPU,
+	RVTRACE_ENCODER_NR_TRC_PARAMS,
+	RVTRACE_ENCODER_FORMAT,
+	RVTRACE_ENCODER_CONTEXT,
+	RVTRACE_ENCODER_INHB_SRC,
+	RVTRACE_ENCODER_SRCBITS,
+	RVTRACE_ENCODER_SRCID,
+	RVTRACE_ENCODER_PRIV_MAX,
+};
+
+#define RVTRACE_ENCODER_NR_TRC_PARAMS_LENGTH (RVTRACE_ENCODER_PRIV_MAX - RVTRACE_ENCODER_FORMAT)
+
+#define RVTRACE_HEADER_SIZE		(RVTRACE_HEADER_MAX * sizeof(u64))
+#define RVTRACE_ENCODER_PRIV_SIZE	(RVTRACE_ENCODER_PRIV_MAX * sizeof(u64))
+
+#endif
Signature · 1 line
2.34.1
#10 lore raw

Re: [RFC PATCH 09/12] perf tools: Add RISC-V trace PMU record capabilities

NACK. The file is largely copied from tools/perf/arch/x86/util/intel-pt.c There's even markers like "Intel Processor Trace" in file auxtrace.c. There's no mentioning of authorship, but instead Spacemit claming its copyright over it.

Bo

Quoted 588 lines · Zane Leung

From: liangzhen <[email protected]>

Introduce the required auxiliary API functions allowing the perf core to interact with RISC-V trace perf driver.

---
  tools/perf/arch/riscv/util/Build      |   2 +-
  tools/perf/arch/riscv/util/auxtrace.c | 489 ++++++++++++++++++++++++++
  tools/perf/util/auxtrace.c            |   1 +
  tools/perf/util/auxtrace.h            |   1 +
  tools/perf/util/rvtrace.h             |  38 ++
  5 files changed, 530 insertions(+), 1 deletion(-)

create mode 100644 tools/perf/arch/riscv/util/auxtrace.c create mode 100644 tools/perf/util/rvtrace.h

tools/perf/arch/riscv/util/Build +1 -1
index 748068a3a5c5..10f41f97bc2e 100644
@@ -1,3 +1,3 @@
  perf-util-y += header.o
 
-perf-util-y += pmu.o
+perf-util-y += pmu.o auxtrace.o
tools/perf/arch/riscv/util/auxtrace.c new file +489 -0
new file mode 100644
index 000000000000..5feee198ef97
@@ -0,0 +1,489 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright(C) 2026 Spacemit Limited. All rights reserved.
+ */
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/bitops.h>
+#include <linux/log2.h>
+#include <linux/zalloc.h>
+#include <linux/string.h>
+#include <time.h>
+
+#include <internal/lib.h> // page_size
+#include "../../../util/auxtrace.h"
+#include "../../../util/cpumap.h"
+#include "../../../util/debug.h"
+#include "../../../util/event.h"
+#include "../../../util/evlist.h"
+#include "../../../util/evsel.h"
+#include "../../../util/rvtrace.h"
+#include "../../../util/pmu.h"
+#include "../../../util/record.h"
+#include "../../../util/session.h"
+#include "../../../util/tsc.h"
+#include "../../../util/evsel_config.h"
+
+#define RVTRACE_PMU_NAME "rvtrace"
+#define KiB(x) ((x) * 1024)
+#define MiB(x) ((x) * 1024 * 1024)
+
+static const char * const metadata_encoder_ro[] = {
+	[RVTRACE_ENCODER_FORMAT]    = "control/format",
+	[RVTRACE_ENCODER_CONTEXT]   = "control/context",
+	[RVTRACE_ENCODER_INHB_SRC]  = "control/inhb_src",
+	[RVTRACE_ENCODER_SRCBITS]   = "features/srcb",
+	[RVTRACE_ENCODER_SRCID]	    = "features/srcid"
+};
+
+struct rvtrace_recording {
+	struct auxtrace_record	itr;
+	struct perf_pmu *rvtrace_pmu;
+	struct evlist *evlist;
+	bool snapshot_mode;
+	size_t snapshot_size;
+};
+
+static int rvtrace_parse_snapshot_options(struct auxtrace_record *itr,
+					  struct record_opts *opts,
+					  const char *str)
+{
+	struct rvtrace_recording *ptr =
+				container_of(itr, struct rvtrace_recording, itr);
+	unsigned long long snapshot_size = 0;
+	char *endptr;
+
+	if (str) {
+		snapshot_size = strtoull(str, &endptr, 0);
+		if (*endptr || snapshot_size > SIZE_MAX)
+			return -1;
+	}
+
+	opts->auxtrace_snapshot_mode = true;
+	opts->auxtrace_snapshot_size = snapshot_size;
+	ptr->snapshot_size = snapshot_size;
+
+	return 0;
+}
+
+static size_t rvtrace_info_priv_size(struct auxtrace_record *itr __maybe_unused,
+				     struct evlist *evlist __maybe_unused)
+{
+	int encoder;
+	struct perf_cpu_map *event_cpus = evlist->core.user_requested_cpus;
+	struct perf_cpu_map *intersect_cpus;
+
+	if (!perf_cpu_map__has_any_cpu(event_cpus)) {
+		/* cpu map is not "any" CPU , we have specific CPUs to work with */
+		struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+
+		intersect_cpus = perf_cpu_map__intersect(event_cpus, online_cpus);
+		perf_cpu_map__put(online_cpus);
+	} else {
+		/* Event can be "any" CPU so count all online CPUs. */
+		intersect_cpus = perf_cpu_map__new_online_cpus();
+	}
+
+	encoder = perf_cpu_map__nr(intersect_cpus);
+	perf_cpu_map__put(intersect_cpus);
+
+	return (RVTRACE_HEADER_SIZE + encoder * RVTRACE_ENCODER_PRIV_SIZE);
+}
+
+static int rvtrace_get_ro(struct perf_pmu *pmu, struct perf_cpu cpu, const char *path, __u64 *val)
+{
+	char pmu_path[PATH_MAX];
+	int scan;
+
+	/* Get RO metadata from sysfs */
+	snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu.cpu, path);
+
+	scan = perf_pmu__scan_file(pmu, pmu_path, "%llx", val);
+	if (scan != 1) {
+		pr_err("%s: error reading: %s\n", __func__, pmu_path);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static void rvtrace_get_metadata(struct perf_cpu cpu, u32 *offset,
+				 struct auxtrace_record *itr,
+				 struct perf_record_auxtrace_info *info)
+{
+	struct rvtrace_recording *ptr = container_of(itr, struct rvtrace_recording, itr);
+	struct perf_pmu *rvtrace_pmu = ptr->rvtrace_pmu;
+
+	info->priv[*offset + RVTRACE_ENCODER_CPU] = cpu.cpu;
+	info->priv[*offset + RVTRACE_ENCODER_NR_TRC_PARAMS] = RVTRACE_ENCODER_NR_TRC_PARAMS_LENGTH;
+
+	/* Get read-only information from sysFS */
+	rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_FORMAT],
+			&info->priv[*offset + RVTRACE_ENCODER_FORMAT]);
+	rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_CONTEXT],
+			&info->priv[*offset + RVTRACE_ENCODER_CONTEXT]);
+	rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_INHB_SRC],
+			&info->priv[*offset + RVTRACE_ENCODER_INHB_SRC]);
+	rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_SRCBITS],
+			&info->priv[*offset + RVTRACE_ENCODER_SRCBITS]);
+	rvtrace_get_ro(rvtrace_pmu, cpu, metadata_encoder_ro[RVTRACE_ENCODER_SRCID],
+			&info->priv[*offset + RVTRACE_ENCODER_SRCID]);
+
+	/* Where the next CPU entry should start from */
+	*offset += RVTRACE_ENCODER_PRIV_MAX;
+}
+
+static int rvtrace_info_fill(struct auxtrace_record *itr, struct perf_session *session,
+			     struct perf_record_auxtrace_info *auxtrace_info, size_t priv_size)
+{
+	int i;
+	u32 offset;
+	u64 nr_cpu, type;
+	struct perf_cpu_map *cpu_map;
+	struct perf_cpu_map *event_cpus = session->evlist->core.user_requested_cpus;
+	struct perf_cpu_map *online_cpus = perf_cpu_map__new_online_cpus();
+	struct rvtrace_recording *ptr = container_of(itr, struct rvtrace_recording, itr);
+	struct perf_pmu *rvtrace_pmu = ptr->rvtrace_pmu;
+	struct perf_cpu cpu;
+
+	if (priv_size != rvtrace_info_priv_size(itr, session->evlist))
+		return -EINVAL;
+
+	if (!session->evlist->core.nr_mmaps)
+		return -EINVAL;
+
+	/* If the cpu_map has the "any" CPU all online CPUs are involved */
+	if (perf_cpu_map__has_any_cpu(event_cpus)) {
+		cpu_map = online_cpus;
+	} else {
+		/* Make sure all specified CPUs are online */
+		perf_cpu_map__for_each_cpu(cpu, i, event_cpus) {
+			if (!perf_cpu_map__has(online_cpus, cpu))
+				return -EINVAL;
+		}
+
+		cpu_map = event_cpus;
+	}
+
+	nr_cpu = perf_cpu_map__nr(cpu_map);
+	type = rvtrace_pmu->type;
+
+	/* First fill out the session header */
+	auxtrace_info->type = PERF_AUXTRACE_RISCV_TRACE;
+	auxtrace_info->priv[RVTRACE_PMU_TYPE_CPUS] = type << 32;
+	auxtrace_info->priv[RVTRACE_PMU_TYPE_CPUS] |= nr_cpu;
+
+	offset = RVTRACE_HEADER_MAX;
+
+	perf_cpu_map__for_each_cpu(cpu, i, cpu_map) {
+		assert(offset < priv_size);
+		rvtrace_get_metadata(cpu, &offset, itr, auxtrace_info);
+	}
+
+	perf_cpu_map__put(online_cpus);
+
+	return 0;
+}
+
+static int rvtrace_set_sink_attr(struct perf_pmu *pmu,
+				 struct evsel *evsel)
+{
+	char msg[BUFSIZ], path[PATH_MAX], *sink;
+	struct evsel_config_term *term;
+	int ret = -EINVAL;
+	u32 hash;
+
+	if (evsel->core.attr.config2 & GENMASK(31, 0))
+		return 0;
+
+	list_for_each_entry(term, &evsel->config_terms, list) {
+		if (term->type != EVSEL__CONFIG_TERM_DRV_CFG)
+			continue;
+
+		sink = term->val.str;
+		snprintf(path, PATH_MAX, "sinks/%s", sink);
+
+		ret = perf_pmu__scan_file(pmu, path, "%x", &hash);
+		if (ret != 1) {
+			if (errno == ENOENT)
+				pr_err("Couldn't find sink \"%s\" on event %s\n"
+				       "Missing kernel or device support?\n\n"
+				       "Hint: An appropriate sink will be picked automatically if one isn't specified.\n",
+				       sink, evsel__name(evsel));
+			else
+				pr_err("Failed to set sink \"%s\" on event %s with %d (%s)\n",
+				       sink, evsel__name(evsel), errno,
+				       str_error_r(errno, msg, sizeof(msg)));
+			return ret;
+		}
+
+		evsel->core.attr.config2 |= hash;
+		return 0;
+	}
+
+	/*
+	 * No sink was provided on the command line - allow the CoreSight
+	 * system to look for a default
+	 */
+	return 0;
+}
+
+static int rvtrace_recording_options(struct auxtrace_record *itr, struct evlist *evlist,
+				     struct record_opts *opts)
+{
+	struct rvtrace_recording *ptr = container_of(itr, struct rvtrace_recording, itr);
+	struct perf_pmu *rvtrace_pmu = ptr->rvtrace_pmu;
+	struct evsel *evsel, *rvtrace_evsel = NULL;
+	struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
+	bool privileged = perf_event_paranoid_check(-1);
+	struct evsel *tracking_evsel;
+	int err;
+
+	ptr->evlist = evlist;
+	ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
+	evlist__for_each_entry(evlist, evsel) {
+		if (evsel->core.attr.type == rvtrace_pmu->type) {
+			if (rvtrace_evsel) {
+				pr_err("There may be only one " RVTRACE_PMU_NAME "x event\n");
+				return -EINVAL;
+			}
+			evsel->core.attr.freq = 0;
+			evsel->core.attr.sample_period = 1;
+			evsel->needs_auxtrace_mmap = true;
+			rvtrace_evsel = evsel;
+			opts->full_auxtrace = true;
+		}
+	}
+
+	if (!opts->full_auxtrace)
+		return 0;
+
+	err = rvtrace_set_sink_attr(rvtrace_pmu, rvtrace_evsel);
+	if (err)
+		return err;
+
+	/* we are in snapshot mode */
+	if (opts->auxtrace_snapshot_mode) {
+		/*
+		 * No size were given to '-S' or '-m,', so go with
+		 * the default
+		 */
+		if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
+			if (privileged) {
+				opts->auxtrace_mmap_pages = MiB(4) / page_size;
+			} else {
+				opts->auxtrace_mmap_pages = KiB(128) / page_size;
+				if (opts->mmap_pages == UINT_MAX)
+					opts->mmap_pages = KiB(256) / page_size;
+			}
+		} else if (!opts->auxtrace_mmap_pages && !privileged &&
+						opts->mmap_pages == UINT_MAX) {
+			opts->mmap_pages = KiB(256) / page_size;
+		}
+
+		/*
+		 * '-m,xyz' was specified but no snapshot size, so make the
+		 * snapshot size as big as the auxtrace mmap area.
+		 */
+		if (!opts->auxtrace_snapshot_size) {
+			opts->auxtrace_snapshot_size =
+				opts->auxtrace_mmap_pages * (size_t)page_size;
+		}
+
+		/*
+		 * -Sxyz was specified but no auxtrace mmap area, so make the
+		 * auxtrace mmap area big enough to fit the requested snapshot
+		 * size.
+		 */
+		if (!opts->auxtrace_mmap_pages) {
+			size_t sz = opts->auxtrace_snapshot_size;
+
+			sz = round_up(sz, page_size) / page_size;
+			opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
+		}
+
+		/* Snapshot size can't be bigger than the auxtrace area */
+		if (opts->auxtrace_snapshot_size >
+				opts->auxtrace_mmap_pages * (size_t)page_size) {
+			pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
+			       opts->auxtrace_snapshot_size,
+			       opts->auxtrace_mmap_pages * (size_t)page_size);
+			return -EINVAL;
+		}
+
+		/* Something went wrong somewhere - this shouldn't happen */
+		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
+			pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
+			return -EINVAL;
+		}
+
+		pr_debug2("%s snapshot size: %zu\n", RVTRACE_PMU_NAME,
+			  opts->auxtrace_snapshot_size);
+	}
+
+	/* Buffer sizes weren't specified with '-m,xyz' so give some defaults */
+	if (!opts->auxtrace_mmap_pages) {
+		if (privileged) {
+			opts->auxtrace_mmap_pages = MiB(4) / page_size;
+		} else {
+			opts->auxtrace_mmap_pages = KiB(128) / page_size;
+			if (opts->mmap_pages == UINT_MAX)
+				opts->mmap_pages = KiB(256) / page_size;
+		}
+	}
+
+	/* Validate auxtrace_mmap_pages */
+	if (opts->auxtrace_mmap_pages) {
+		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
+		size_t min_sz;
+
+		if (opts->auxtrace_snapshot_mode)
+			min_sz = KiB(4);
+		else
+			min_sz = KiB(8);
+
+		if (sz < min_sz || !is_power_of_2(sz)) {
+			pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
+			       min_sz / 1024);
+			return -EINVAL;
+		}
+	}
+
+	/*
+	 * To obtain the auxtrace buffer file descriptor, the auxtrace event
+	 * must come first.
+	 */
+	evlist__to_front(evlist, rvtrace_evsel);
+
+	/*
+	 * get the CPU on the sample - need it to associate trace ID in the
+	 * AUX_OUTPUT_HW_ID event, and the AUX event for per-cpu mmaps.
+	 */
+	evsel__set_sample_bit(rvtrace_evsel, CPU);
+
+	/* Add dummy event to keep tracking */
+	err = parse_event(evlist, "dummy:u");
+	if (err)
+		return err;
+
+	tracking_evsel = evlist__last(evlist);
+	evlist__set_tracking_event(evlist, tracking_evsel);
+
+	tracking_evsel->core.attr.freq = 0;
+	tracking_evsel->core.attr.sample_period = 1;
+
+	/* In per-cpu case, always need the time of mmap events etc */
+	if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
+		evsel__set_sample_bit(tracking_evsel, TIME);
+
+	return 0;
+}
+
+static int rvtrace_snapshot_start(struct auxtrace_record *itr)
+{
+	struct rvtrace_recording *ptr =
+			container_of(itr, struct rvtrace_recording, itr);
+	struct evsel *evsel;
+
+	evlist__for_each_entry(ptr->evlist, evsel) {
+		if (evsel->core.attr.type == ptr->rvtrace_pmu->type)
+			return evsel__disable(evsel);
+	}
+	return -EINVAL;
+}
+
+static int rvtrace_snapshot_finish(struct auxtrace_record *itr)
+{
+	struct rvtrace_recording *ptr =
+			container_of(itr, struct rvtrace_recording, itr);
+	struct evsel *evsel;
+
+	evlist__for_each_entry(ptr->evlist, evsel) {
+		if (evsel->core.attr.type == ptr->rvtrace_pmu->type)
+			return evsel__enable(evsel);
+	}
+	return -EINVAL;
+}
+
+static u64 rvtrace_reference(struct auxtrace_record *itr __maybe_unused)
+{
+	return rdtsc();
+}
+
+static void rvtrace_recording_free(struct auxtrace_record *itr)
+{
+	struct rvtrace_recording *ptr =
+			container_of(itr, struct rvtrace_recording, itr);
+
+	free(ptr);
+}
+
+static struct auxtrace_record *rvtrace_recording_init(int *err, struct perf_pmu *rvtrace_pmu)
+{
+	struct rvtrace_recording *ptr;
+
+	if (!rvtrace_pmu) {
+		*err = -ENODEV;
+		return NULL;
+	}
+
+	ptr = zalloc(sizeof(*ptr));
+	if (!ptr) {
+		*err = -ENOMEM;
+		return NULL;
+	}
+
+	ptr->rvtrace_pmu = rvtrace_pmu;
+	ptr->itr.parse_snapshot_options = rvtrace_parse_snapshot_options;
+	ptr->itr.recording_options = rvtrace_recording_options;
+	ptr->itr.info_priv_size = rvtrace_info_priv_size;
+	ptr->itr.info_fill = rvtrace_info_fill;
+	ptr->itr.snapshot_start = rvtrace_snapshot_start;
+	ptr->itr.snapshot_finish = rvtrace_snapshot_finish;
+	ptr->itr.free = rvtrace_recording_free;
+	ptr->itr.reference = rvtrace_reference;
+	ptr->itr.read_finish = auxtrace_record__read_finish;
+	ptr->itr.alignment = 0;
+
+	*err = 0;
+	return &ptr->itr;
+}
+
+static struct perf_pmu *find_pmu_for_event(struct perf_pmu **pmus,
+					   int pmu_nr, struct evsel *evsel)
+{
+	int i;
+
+	if (!pmus)
+		return NULL;
+
+	for (i = 0; i < pmu_nr; i++) {
+		if (evsel->core.attr.type == pmus[i]->type)
+			return pmus[i];
+	}
+
+	return NULL;
+}
+
+struct auxtrace_record *auxtrace_record__init(struct evlist *evlist, int *err)
+{
+	struct perf_pmu	*rvtrace_pmu = NULL;
+	struct perf_pmu *found_rvtrace = NULL;
+	struct evsel *evsel;
+
+	if (!evlist)
+		return NULL;
+
+	rvtrace_pmu = perf_pmus__find(RVTRACE_PMU_NAME);
+	evlist__for_each_entry(evlist, evsel) {
+		if (rvtrace_pmu && !found_rvtrace)
+			found_rvtrace = find_pmu_for_event(&rvtrace_pmu, 1, evsel);
+	}
+
+	if (found_rvtrace)
+		return rvtrace_recording_init(err, rvtrace_pmu);
+
+	*err = 0;
+	return NULL;
+}
tools/perf/util/auxtrace.c +1 -0
index a224687ffbc1..944a43d48739 100644
@@ -1411,6 +1411,7 @@ int perf_event__process_auxtrace_info(const struct perf_tool *tool __maybe_unuse
  	case PERF_AUXTRACE_VPA_DTL:
  		err = powerpc_vpadtl_process_auxtrace_info(event, session);
  		break;
+	case PERF_AUXTRACE_RISCV_TRACE:
  	case PERF_AUXTRACE_UNKNOWN:
  	default:
  		return -EINVAL;
tools/perf/util/auxtrace.h +1 -0
index 6947f3f284c0..4f4714c1b53f 100644
@@ -46,6 +46,7 @@ enum auxtrace_type {
  	PERF_AUXTRACE_S390_CPUMSF,
  	PERF_AUXTRACE_HISI_PTT,
  	PERF_AUXTRACE_VPA_DTL,
+	PERF_AUXTRACE_RISCV_TRACE,
  };
 
  enum itrace_period_type {
tools/perf/util/rvtrace.h new file +38 -0
new file mode 100644
index 000000000000..1e48ed989dd7
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright(C) 2026 Spacemit Limited. All rights reserved.
+ */
+
+#ifndef INCLUDE__UTIL_PERF_RVTRACE_H__
+#define INCLUDE__UTIL_PERF_RVTRACE_H__
+
+#include "debug.h"
+#include "auxtrace.h"
+#include "util/event.h"
+#include "util/session.h"
+#include <linux/bits.h>
+
+enum {
+	/* PMU->type (32 bit), total # of CPUs (32 bit) */
+	RVTRACE_PMU_TYPE_CPUS,
+	RVTRACE_HEADER_MAX,
+};
+
+/* Trace Encoder metadata */
+enum {
+	RVTRACE_ENCODER_CPU,
+	RVTRACE_ENCODER_NR_TRC_PARAMS,
+	RVTRACE_ENCODER_FORMAT,
+	RVTRACE_ENCODER_CONTEXT,
+	RVTRACE_ENCODER_INHB_SRC,
+	RVTRACE_ENCODER_SRCBITS,
+	RVTRACE_ENCODER_SRCID,
+	RVTRACE_ENCODER_PRIV_MAX,
+};
+
+#define RVTRACE_ENCODER_NR_TRC_PARAMS_LENGTH (RVTRACE_ENCODER_PRIV_MAX - RVTRACE_ENCODER_FORMAT)
+
+#define RVTRACE_HEADER_SIZE		(RVTRACE_HEADER_MAX * sizeof(u64))
+#define RVTRACE_ENCODER_PRIV_SIZE	(RVTRACE_ENCODER_PRIV_MAX * sizeof(u64))
+
+#endif
author
#11 lore raw

RFC PATCH 10/12 perf tools: Add Nexus RISC-V Trace decoder

In reply to Zane Leung

From: liangzhen <[email protected]>

Add support for RISC-V Nexus Trace decoder based on the reference implementation from the RISC-V Nexus Trace specification. This includes a CoreSight frame deformatter to remove the trace formatter overhead.

---
 tools/arch/riscv/include/asm/insn.h           |  645 ++++++++
 tools/perf/arch/riscv/util/auxtrace.c         |    1 +
 tools/perf/util/Build                         |    2 +
 tools/perf/util/nexus-rv-decoder/Build        |    1 +
 .../util/nexus-rv-decoder/nexus-rv-decoder.c  | 1364 +++++++++++++++++
 .../util/nexus-rv-decoder/nexus-rv-decoder.h  |  139 ++
 .../perf/util/nexus-rv-decoder/nexus-rv-msg.h |  190 +++
 7 files changed, 2342 insertions(+)
 create mode 100644 tools/arch/riscv/include/asm/insn.h
 create mode 100644 tools/perf/util/nexus-rv-decoder/Build
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h
tools/arch/riscv/include/asm/insn.h new file +645 -0
new file mode 100644
index 000000000000..6c020afe926b
@@ -0,0 +1,645 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020 SiFive
+ */
+
+#ifndef _ASM_RISCV_INSN_H
+#define _ASM_RISCV_INSN_H
+
+#include <linux/bits.h>
+
+#define RV_INSN_FUNCT3_MASK	GENMASK(14, 12)
+#define RV_INSN_FUNCT3_OPOFF	12
+#define RV_INSN_OPCODE_MASK	GENMASK(6, 0)
+#define RV_INSN_OPCODE_OPOFF	0
+#define RV_INSN_FUNCT12_OPOFF	20
+
+#define RV_ENCODE_FUNCT3(f_)	(RVG_FUNCT3_##f_ << RV_INSN_FUNCT3_OPOFF)
+#define RV_ENCODE_FUNCT12(f_)	(RVG_FUNCT12_##f_ << RV_INSN_FUNCT12_OPOFF)
+
+/* The bit field of immediate value in I-type instruction */
+#define RV_I_IMM_SIGN_OPOFF	31
+#define RV_I_IMM_11_0_OPOFF	20
+#define RV_I_IMM_SIGN_OFF	12
+#define RV_I_IMM_11_0_OFF	0
+#define RV_I_IMM_11_0_MASK	GENMASK(11, 0)
+
+/* The bit field of immediate value in J-type instruction */
+#define RV_J_IMM_SIGN_OPOFF	31
+#define RV_J_IMM_10_1_OPOFF	21
+#define RV_J_IMM_11_OPOFF	20
+#define RV_J_IMM_19_12_OPOFF	12
+#define RV_J_IMM_SIGN_OFF	20
+#define RV_J_IMM_10_1_OFF	1
+#define RV_J_IMM_11_OFF		11
+#define RV_J_IMM_19_12_OFF	12
+#define RV_J_IMM_10_1_MASK	GENMASK(9, 0)
+#define RV_J_IMM_11_MASK	GENMASK(0, 0)
+#define RV_J_IMM_19_12_MASK	GENMASK(7, 0)
+
+/*
+ * U-type IMMs contain the upper 20bits [31:20] of an immediate with
+ * the rest filled in by zeros, so no shifting required. Similarly,
+ * bit31 contains the signed state, so no sign extension necessary.
+ */
+#define RV_U_IMM_SIGN_OPOFF	31
+#define RV_U_IMM_31_12_OPOFF	0
+#define RV_U_IMM_31_12_MASK	GENMASK(31, 12)
+
+/* The bit field of immediate value in B-type instruction */
+#define RV_B_IMM_SIGN_OPOFF	31
+#define RV_B_IMM_10_5_OPOFF	25
+#define RV_B_IMM_4_1_OPOFF	8
+#define RV_B_IMM_11_OPOFF	7
+#define RV_B_IMM_SIGN_OFF	12
+#define RV_B_IMM_10_5_OFF	5
+#define RV_B_IMM_4_1_OFF	1
+#define RV_B_IMM_11_OFF		11
+#define RV_B_IMM_10_5_MASK	GENMASK(5, 0)
+#define RV_B_IMM_4_1_MASK	GENMASK(3, 0)
+#define RV_B_IMM_11_MASK	GENMASK(0, 0)
+
+/* The register offset in RVG instruction */
+#define RVG_RS1_OPOFF		15
+#define RVG_RS2_OPOFF		20
+#define RVG_RD_OPOFF		7
+#define RVG_RS1_MASK		GENMASK(4, 0)
+#define RVG_RS2_MASK		GENMASK(4, 0)
+#define RVG_RD_MASK		GENMASK(4, 0)
+
+/* The bit field of immediate value in RVC J instruction */
+#define RVC_J_IMM_SIGN_OPOFF	12
+#define RVC_J_IMM_4_OPOFF	11
+#define RVC_J_IMM_9_8_OPOFF	9
+#define RVC_J_IMM_10_OPOFF	8
+#define RVC_J_IMM_6_OPOFF	7
+#define RVC_J_IMM_7_OPOFF	6
+#define RVC_J_IMM_3_1_OPOFF	3
+#define RVC_J_IMM_5_OPOFF	2
+#define RVC_J_IMM_SIGN_OFF	11
+#define RVC_J_IMM_4_OFF		4
+#define RVC_J_IMM_9_8_OFF	8
+#define RVC_J_IMM_10_OFF	10
+#define RVC_J_IMM_6_OFF		6
+#define RVC_J_IMM_7_OFF		7
+#define RVC_J_IMM_3_1_OFF	1
+#define RVC_J_IMM_5_OFF		5
+#define RVC_J_IMM_4_MASK	GENMASK(0, 0)
+#define RVC_J_IMM_9_8_MASK	GENMASK(1, 0)
+#define RVC_J_IMM_10_MASK	GENMASK(0, 0)
+#define RVC_J_IMM_6_MASK	GENMASK(0, 0)
+#define RVC_J_IMM_7_MASK	GENMASK(0, 0)
+#define RVC_J_IMM_3_1_MASK	GENMASK(2, 0)
+#define RVC_J_IMM_5_MASK	GENMASK(0, 0)
+
+/* The bit field of immediate value in RVC B instruction */
+#define RVC_B_IMM_SIGN_OPOFF	12
+#define RVC_B_IMM_4_3_OPOFF	10
+#define RVC_B_IMM_7_6_OPOFF	5
+#define RVC_B_IMM_2_1_OPOFF	3
+#define RVC_B_IMM_5_OPOFF	2
+#define RVC_B_IMM_SIGN_OFF	8
+#define RVC_B_IMM_4_3_OFF	3
+#define RVC_B_IMM_7_6_OFF	6
+#define RVC_B_IMM_2_1_OFF	1
+#define RVC_B_IMM_5_OFF		5
+#define RVC_B_IMM_4_3_MASK	GENMASK(1, 0)
+#define RVC_B_IMM_7_6_MASK	GENMASK(1, 0)
+#define RVC_B_IMM_2_1_MASK	GENMASK(1, 0)
+#define RVC_B_IMM_5_MASK	GENMASK(0, 0)
+
+#define RVC_INSN_FUNCT4_MASK	GENMASK(15, 12)
+#define RVC_INSN_FUNCT4_OPOFF	12
+#define RVC_INSN_FUNCT3_MASK	GENMASK(15, 13)
+#define RVC_INSN_FUNCT3_OPOFF	13
+#define RVC_INSN_J_RS1_MASK	GENMASK(11, 7)
+#define RVC_INSN_J_RS2_MASK	GENMASK(6, 2)
+#define RVC_INSN_OPCODE_MASK	GENMASK(1, 0)
+#define RVC_ENCODE_FUNCT3(f_)	(RVC_FUNCT3_##f_ << RVC_INSN_FUNCT3_OPOFF)
+#define RVC_ENCODE_FUNCT4(f_)	(RVC_FUNCT4_##f_ << RVC_INSN_FUNCT4_OPOFF)
+
+/* The register offset in RVC op=C0 instruction */
+#define RVC_C0_RS1_OPOFF	7
+#define RVC_C0_RS2_OPOFF	2
+#define RVC_C0_RD_OPOFF		2
+
+/* The register offset in RVC op=C1 instruction */
+#define RVC_C1_RS1_OPOFF	7
+#define RVC_C1_RS2_OPOFF	2
+#define RVC_C1_RD_OPOFF		7
+
+/* The register offset in RVC op=C2 instruction */
+#define RVC_C2_RS1_OPOFF	7
+#define RVC_C2_RS2_OPOFF	2
+#define RVC_C2_RD_OPOFF		7
+#define RVC_C2_RS1_MASK		GENMASK(4, 0)
+
+/* parts of opcode for RVG*/
+#define RVG_OPCODE_FENCE	0x0f
+#define RVG_OPCODE_AUIPC	0x17
+#define RVG_OPCODE_BRANCH	0x63
+#define RVG_OPCODE_JALR		0x67
+#define RVG_OPCODE_JAL		0x6f
+#define RVG_OPCODE_SYSTEM	0x73
+#define RVG_SYSTEM_CSR_OFF	20
+#define RVG_SYSTEM_CSR_MASK	GENMASK(12, 0)
+
+/* parts of opcode for RVF, RVD and RVQ */
+#define RVFDQ_FL_FS_WIDTH_OFF	12
+#define RVFDQ_FL_FS_WIDTH_MASK	GENMASK(2, 0)
+#define RVFDQ_FL_FS_WIDTH_W	2
+#define RVFDQ_FL_FS_WIDTH_D	3
+#define RVFDQ_LS_FS_WIDTH_Q	4
+#define RVFDQ_OPCODE_FL		0x07
+#define RVFDQ_OPCODE_FS		0x27
+
+/* parts of opcode for RVV */
+#define RVV_OPCODE_VECTOR	0x57
+#define RVV_VL_VS_WIDTH_8	0
+#define RVV_VL_VS_WIDTH_16	5
+#define RVV_VL_VS_WIDTH_32	6
+#define RVV_VL_VS_WIDTH_64	7
+#define RVV_OPCODE_VL		RVFDQ_OPCODE_FL
+#define RVV_OPCODE_VS		RVFDQ_OPCODE_FS
+
+/* parts of opcode for RVC*/
+#define RVC_OPCODE_C0		0x0
+#define RVC_OPCODE_C1		0x1
+#define RVC_OPCODE_C2		0x2
+
+/* parts of funct3 code for I, M, A extension*/
+#define RVG_FUNCT3_JALR		0x0
+#define RVG_FUNCT3_BEQ		0x0
+#define RVG_FUNCT3_BNE		0x1
+#define RVG_FUNCT3_BLT		0x4
+#define RVG_FUNCT3_BGE		0x5
+#define RVG_FUNCT3_BLTU		0x6
+#define RVG_FUNCT3_BGEU		0x7
+
+/* parts of funct3 code for C extension*/
+#define RVC_FUNCT3_C_BEQZ	0x6
+#define RVC_FUNCT3_C_BNEZ	0x7
+#define RVC_FUNCT3_C_J		0x5
+#define RVC_FUNCT3_C_JAL	0x1
+#define RVC_FUNCT4_C_JR		0x8
+#define RVC_FUNCT4_C_JALR	0x9
+#define RVC_FUNCT4_C_EBREAK	0x9
+
+#define RVG_FUNCT12_ECALL	0x0
+#define RVG_FUNCT12_EBREAK	0x1
+#define RVG_FUNCT12_SRET	0x102
+#define RVG_FUNCT12_MRET	0x303
+
+#define RVG_MATCH_AUIPC		(RVG_OPCODE_AUIPC)
+#define RVG_MATCH_JALR		(RV_ENCODE_FUNCT3(JALR) | RVG_OPCODE_JALR)
+#define RVG_MATCH_JAL		(RVG_OPCODE_JAL)
+#define RVG_MATCH_FENCE		(RVG_OPCODE_FENCE)
+#define RVG_MATCH_BEQ		(RV_ENCODE_FUNCT3(BEQ) | RVG_OPCODE_BRANCH)
+#define RVG_MATCH_BNE		(RV_ENCODE_FUNCT3(BNE) | RVG_OPCODE_BRANCH)
+#define RVG_MATCH_BLT		(RV_ENCODE_FUNCT3(BLT) | RVG_OPCODE_BRANCH)
+#define RVG_MATCH_BGE		(RV_ENCODE_FUNCT3(BGE) | RVG_OPCODE_BRANCH)
+#define RVG_MATCH_BLTU		(RV_ENCODE_FUNCT3(BLTU) | RVG_OPCODE_BRANCH)
+#define RVG_MATCH_BGEU		(RV_ENCODE_FUNCT3(BGEU) | RVG_OPCODE_BRANCH)
+#define RVG_MATCH_ECALL		(RV_ENCODE_FUNCT12(EBREAK) | RVG_OPCODE_SYSTEM)
+#define RVG_MATCH_EBREAK	(RV_ENCODE_FUNCT12(EBREAK) | RVG_OPCODE_SYSTEM)
+#define RVG_MATCH_SRET		(RV_ENCODE_FUNCT12(SRET) | RVG_OPCODE_SYSTEM)
+#define RVG_MATCH_MRET		(RV_ENCODE_FUNCT12(MRET) | RVG_OPCODE_SYSTEM)
+#define RVC_MATCH_C_BEQZ	(RVC_ENCODE_FUNCT3(C_BEQZ) | RVC_OPCODE_C1)
+#define RVC_MATCH_C_BNEZ	(RVC_ENCODE_FUNCT3(C_BNEZ) | RVC_OPCODE_C1)
+#define RVC_MATCH_C_J		(RVC_ENCODE_FUNCT3(C_J) | RVC_OPCODE_C1)
+#define RVC_MATCH_C_JAL		(RVC_ENCODE_FUNCT3(C_JAL) | RVC_OPCODE_C1)
+#define RVC_MATCH_C_JR		(RVC_ENCODE_FUNCT4(C_JR) | RVC_OPCODE_C2)
+#define RVC_MATCH_C_JALR	(RVC_ENCODE_FUNCT4(C_JALR) | RVC_OPCODE_C2)
+#define RVC_MATCH_C_EBREAK	(RVC_ENCODE_FUNCT4(C_EBREAK) | RVC_OPCODE_C2)
+
+#define RVG_MASK_AUIPC		(RV_INSN_OPCODE_MASK)
+#define RVG_MASK_JALR		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVG_MASK_JAL		(RV_INSN_OPCODE_MASK)
+#define RVG_MASK_FENCE		(RV_INSN_OPCODE_MASK)
+#define RVC_MASK_C_JALR		(RVC_INSN_FUNCT4_MASK | RVC_INSN_J_RS2_MASK | RVC_INSN_OPCODE_MASK)
+#define RVC_MASK_C_JR		(RVC_INSN_FUNCT4_MASK | RVC_INSN_J_RS2_MASK | RVC_INSN_OPCODE_MASK)
+#define RVC_MASK_C_JAL		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
+#define RVC_MASK_C_J		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
+#define RVG_MASK_BEQ		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVG_MASK_BNE		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVG_MASK_BLT		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVG_MASK_BGE		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVG_MASK_BLTU		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVG_MASK_BGEU		(RV_INSN_FUNCT3_MASK | RV_INSN_OPCODE_MASK)
+#define RVC_MASK_C_BEQZ		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
+#define RVC_MASK_C_BNEZ		(RVC_INSN_FUNCT3_MASK | RVC_INSN_OPCODE_MASK)
+#define RVC_MASK_C_EBREAK	0xffff
+#define RVG_MASK_EBREAK		0xffffffff
+#define RVG_MASK_ECALL		0xffffffff
+#define RVG_MASK_SRET		0xffffffff
+#define RVG_MASK_MRET		0xffffffff
+
+#define __INSN_LENGTH_MASK	_UL(0x3)
+#define __INSN_LENGTH_GE_32	_UL(0x3)
+#define __INSN_OPCODE_MASK	_UL(0x7F)
+#define __INSN_BRANCH_OPCODE	_UL(RVG_OPCODE_BRANCH)
+
+#define __RISCV_INSN_FUNCS(name, mask, val)				\
+static __always_inline bool riscv_insn_is_##name(u32 code)		\
+{									\
+	BUILD_BUG_ON(~(mask) & (val));					\
+	return (code & (mask)) == (val);				\
+}									\
+
+#if __riscv_xlen == 32
+/* C.JAL is an RV32C-only instruction */
+__RISCV_INSN_FUNCS(c_jal, RVC_MASK_C_JAL, RVC_MATCH_C_JAL)
+#else
+#define riscv_insn_is_c_jal(opcode) 0
+#endif
+__RISCV_INSN_FUNCS(auipc, RVG_MASK_AUIPC, RVG_MATCH_AUIPC)
+__RISCV_INSN_FUNCS(jalr, RVG_MASK_JALR, RVG_MATCH_JALR)
+__RISCV_INSN_FUNCS(jal, RVG_MASK_JAL, RVG_MATCH_JAL)
+__RISCV_INSN_FUNCS(c_j, RVC_MASK_C_J, RVC_MATCH_C_J)
+__RISCV_INSN_FUNCS(beq, RVG_MASK_BEQ, RVG_MATCH_BEQ)
+__RISCV_INSN_FUNCS(bne, RVG_MASK_BNE, RVG_MATCH_BNE)
+__RISCV_INSN_FUNCS(blt, RVG_MASK_BLT, RVG_MATCH_BLT)
+__RISCV_INSN_FUNCS(bge, RVG_MASK_BGE, RVG_MATCH_BGE)
+__RISCV_INSN_FUNCS(bltu, RVG_MASK_BLTU, RVG_MATCH_BLTU)
+__RISCV_INSN_FUNCS(bgeu, RVG_MASK_BGEU, RVG_MATCH_BGEU)
+__RISCV_INSN_FUNCS(c_beqz, RVC_MASK_C_BEQZ, RVC_MATCH_C_BEQZ)
+__RISCV_INSN_FUNCS(c_bnez, RVC_MASK_C_BNEZ, RVC_MATCH_C_BNEZ)
+__RISCV_INSN_FUNCS(c_ebreak, RVC_MASK_C_EBREAK, RVC_MATCH_C_EBREAK)
+__RISCV_INSN_FUNCS(ebreak, RVG_MASK_EBREAK, RVG_MATCH_EBREAK)
+__RISCV_INSN_FUNCS(ecall, RVG_MASK_EBREAK, RVG_MATCH_EBREAK)
+__RISCV_INSN_FUNCS(sret, RVG_MASK_SRET, RVG_MATCH_SRET)
+__RISCV_INSN_FUNCS(mret, RVG_MASK_MRET, RVG_MATCH_MRET)
+__RISCV_INSN_FUNCS(fence, RVG_MASK_FENCE, RVG_MATCH_FENCE);
+
+/* special case to catch _any_ system instruction */
+static __always_inline bool riscv_insn_is_system(u32 code)
+{
+	return (code & RV_INSN_OPCODE_MASK) == RVG_OPCODE_SYSTEM;
+}
+
+/* special case to catch _any_ branch instruction */
+static __always_inline bool riscv_insn_is_branch(u32 code)
+{
+	return (code & RV_INSN_OPCODE_MASK) == RVG_OPCODE_BRANCH;
+}
+
+static __always_inline bool riscv_insn_is_c_jr(u32 code)
+{
+	return (code & RVC_MASK_C_JR) == RVC_MATCH_C_JR &&
+	       (code & RVC_INSN_J_RS1_MASK) != 0;
+}
+
+static __always_inline bool riscv_insn_is_c_jalr(u32 code)
+{
+	return (code & RVC_MASK_C_JALR) == RVC_MATCH_C_JALR &&
+	       (code & RVC_INSN_J_RS1_MASK) != 0;
+}
+
+#define INSN_MATCH_LB		0x3
+#define INSN_MASK_LB		0x707f
+#define INSN_MATCH_LH		0x1003
+#define INSN_MASK_LH		0x707f
+#define INSN_MATCH_LW		0x2003
+#define INSN_MASK_LW		0x707f
+#define INSN_MATCH_LD		0x3003
+#define INSN_MASK_LD		0x707f
+#define INSN_MATCH_LBU		0x4003
+#define INSN_MASK_LBU		0x707f
+#define INSN_MATCH_LHU		0x5003
+#define INSN_MASK_LHU		0x707f
+#define INSN_MATCH_LWU		0x6003
+#define INSN_MASK_LWU		0x707f
+#define INSN_MATCH_SB		0x23
+#define INSN_MASK_SB		0x707f
+#define INSN_MATCH_SH		0x1023
+#define INSN_MASK_SH		0x707f
+#define INSN_MATCH_SW		0x2023
+#define INSN_MASK_SW		0x707f
+#define INSN_MATCH_SD		0x3023
+#define INSN_MASK_SD		0x707f
+
+#define INSN_MATCH_C_LD		0x6000
+#define INSN_MASK_C_LD		0xe003
+#define INSN_MATCH_C_SD		0xe000
+#define INSN_MASK_C_SD		0xe003
+#define INSN_MATCH_C_LW		0x4000
+#define INSN_MASK_C_LW		0xe003
+#define INSN_MATCH_C_SW		0xc000
+#define INSN_MASK_C_SW		0xe003
+#define INSN_MATCH_C_LDSP	0x6002
+#define INSN_MASK_C_LDSP	0xe003
+#define INSN_MATCH_C_SDSP	0xe002
+#define INSN_MASK_C_SDSP	0xe003
+#define INSN_MATCH_C_LWSP	0x4002
+#define INSN_MASK_C_LWSP	0xe003
+#define INSN_MATCH_C_SWSP	0xc002
+#define INSN_MASK_C_SWSP	0xe003
+
+#define INSN_OPCODE_MASK	0x007c
+#define INSN_OPCODE_SHIFT	2
+#define INSN_OPCODE_SYSTEM	28
+
+#define INSN_MASK_WFI		0xffffffff
+#define INSN_MATCH_WFI		0x10500073
+
+#define INSN_MASK_WRS		0xffffffff
+#define INSN_MATCH_WRS		0x00d00073
+
+#define INSN_MATCH_CSRRW	0x1073
+#define INSN_MASK_CSRRW		0x707f
+#define INSN_MATCH_CSRRS	0x2073
+#define INSN_MASK_CSRRS		0x707f
+#define INSN_MATCH_CSRRC	0x3073
+#define INSN_MASK_CSRRC		0x707f
+#define INSN_MATCH_CSRRWI	0x5073
+#define INSN_MASK_CSRRWI	0x707f
+#define INSN_MATCH_CSRRSI	0x6073
+#define INSN_MASK_CSRRSI	0x707f
+#define INSN_MATCH_CSRRCI	0x7073
+#define INSN_MASK_CSRRCI	0x707f
+
+#define INSN_MATCH_FLW		0x2007
+#define INSN_MASK_FLW		0x707f
+#define INSN_MATCH_FLD		0x3007
+#define INSN_MASK_FLD		0x707f
+#define INSN_MATCH_FLQ		0x4007
+#define INSN_MASK_FLQ		0x707f
+#define INSN_MATCH_FSW		0x2027
+#define INSN_MASK_FSW		0x707f
+#define INSN_MATCH_FSD		0x3027
+#define INSN_MASK_FSD		0x707f
+#define INSN_MATCH_FSQ		0x4027
+#define INSN_MASK_FSQ		0x707f
+
+#define INSN_MATCH_C_FLD	0x2000
+#define INSN_MASK_C_FLD		0xe003
+#define INSN_MATCH_C_FLW	0x6000
+#define INSN_MASK_C_FLW		0xe003
+#define INSN_MATCH_C_FSD	0xa000
+#define INSN_MASK_C_FSD		0xe003
+#define INSN_MATCH_C_FSW	0xe000
+#define INSN_MASK_C_FSW		0xe003
+#define INSN_MATCH_C_FLDSP	0x2002
+#define INSN_MASK_C_FLDSP	0xe003
+#define INSN_MATCH_C_FSDSP	0xa002
+#define INSN_MASK_C_FSDSP	0xe003
+#define INSN_MATCH_C_FLWSP	0x6002
+#define INSN_MASK_C_FLWSP	0xe003
+#define INSN_MATCH_C_FSWSP	0xe002
+#define INSN_MASK_C_FSWSP	0xe003
+
+#define INSN_MATCH_C_LHU		0x8400
+#define INSN_MASK_C_LHU			0xfc43
+#define INSN_MATCH_C_LH			0x8440
+#define INSN_MASK_C_LH			0xfc43
+#define INSN_MATCH_C_SH			0x8c00
+#define INSN_MASK_C_SH			0xfc43
+
+#define INSN_16BIT_MASK		0x3
+#define INSN_IS_16BIT(insn)	(((insn) & INSN_16BIT_MASK) != INSN_16BIT_MASK)
+#define INSN_LEN(insn)		(INSN_IS_16BIT(insn) ? 2 : 4)
+
+#define SHIFT_RIGHT(x, y)		\
+	((y) < 0 ? ((x) << -(y)) : ((x) >> (y)))
+
+#define REG_MASK			\
+	((1 << (5 + LOG_REGBYTES)) - (1 << LOG_REGBYTES))
+
+#define REG_OFFSET(insn, pos)		\
+	(SHIFT_RIGHT((insn), (pos) - LOG_REGBYTES) & REG_MASK)
+
+#define REG_PTR(insn, pos, regs)	\
+	((ulong *)((ulong)(regs) + REG_OFFSET(insn, pos)))
+
+#define GET_RS1(insn, regs)	(*REG_PTR(insn, SH_RS1, regs))
+#define GET_RS2(insn, regs)	(*REG_PTR(insn, SH_RS2, regs))
+#define GET_RS1S(insn, regs)	(*REG_PTR(RVC_RS1S(insn), 0, regs))
+#define GET_RS2S(insn, regs)	(*REG_PTR(RVC_RS2S(insn), 0, regs))
+#define GET_RS2C(insn, regs)	(*REG_PTR(insn, SH_RS2C, regs))
+#define GET_SP(regs)		(*REG_PTR(2, 0, regs))
+#define SET_RD(insn, regs, val)	(*REG_PTR(insn, SH_RD, regs) = (val))
+#define IMM_I(insn)		((s32)(insn) >> 20)
+#define IMM_S(insn)		(((s32)(insn) >> 25 << 5) | \
+				 (s32)(((insn) >> 7) & 0x1f))
+
+#define SH_RD			7
+#define SH_RS1			15
+#define SH_RS2			20
+#define SH_RS2C			2
+#define MASK_RX			0x1f
+
+#if defined(CONFIG_64BIT)
+#define LOG_REGBYTES		3
+#else
+#define LOG_REGBYTES		2
+#endif
+
+#define MASK_FUNCT3		0x7000
+
+#define GET_FUNCT3(insn)	(((insn) >> 12) & 7)
+
+#define RV_IMM_SIGN(x)		(-(((x) >> 31) & 1))
+#define RVC_IMM_SIGN(x)		(-(((x) >> 12) & 1))
+#define RV_X_MASK(X, s, mask)	(((X) >> (s)) & (mask))
+#define RV_X(X, s, n)		RV_X_MASK(X, s, ((1 << (n)) - 1))
+#define RVC_LW_IMM(x)		((RV_X(x, 6, 1) << 2) | \
+				 (RV_X(x, 10, 3) << 3) | \
+				 (RV_X(x, 5, 1) << 6))
+#define RVC_LD_IMM(x)		((RV_X(x, 10, 3) << 3) | \
+				 (RV_X(x, 5, 2) << 6))
+#define RVC_LWSP_IMM(x)		((RV_X(x, 4, 3) << 2) | \
+				 (RV_X(x, 12, 1) << 5) | \
+				 (RV_X(x, 2, 2) << 6))
+#define RVC_LDSP_IMM(x)		((RV_X(x, 5, 2) << 3) | \
+				 (RV_X(x, 12, 1) << 5) | \
+				 (RV_X(x, 2, 3) << 6))
+#define RVC_SWSP_IMM(x)		((RV_X(x, 9, 4) << 2) | \
+				 (RV_X(x, 7, 2) << 6))
+#define RVC_SDSP_IMM(x)		((RV_X(x, 10, 3) << 3) | \
+				 (RV_X(x, 7, 3) << 6))
+#define RVC_RS1S(insn)		(8 + RV_X(insn, SH_RD, 3))
+#define RVC_RS2S(insn)		(8 + RV_X(insn, SH_RS2C, 3))
+#define RVC_RS2(insn)		RV_X(insn, SH_RS2C, 5)
+#define RVC_X(X, s, mask)	RV_X_MASK(X, s, mask)
+
+#define RV_EXTRACT_FUNCT3(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RV_INSN_FUNCT3_OPOFF, \
+		   RV_INSN_FUNCT3_MASK >> RV_INSN_FUNCT3_OPOFF)); })
+
+#define RV_EXTRACT_RS1_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RVG_RS1_OPOFF, RVG_RS1_MASK)); })
+
+#define RV_EXTRACT_RS2_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); })
+
+#define RV_EXTRACT_RD_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
+
+#define RV_EXTRACT_UTYPE_IMM(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RV_U_IMM_31_12_OPOFF, RV_U_IMM_31_12_MASK)); })
+
+#define RV_EXTRACT_JTYPE_IMM(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RV_J_IMM_10_1_OPOFF, RV_J_IMM_10_1_MASK) << RV_J_IMM_10_1_OFF) | \
+	(RV_X_MASK(x_, RV_J_IMM_11_OPOFF, RV_J_IMM_11_MASK) << RV_J_IMM_11_OFF) | \
+	(RV_X_MASK(x_, RV_J_IMM_19_12_OPOFF, RV_J_IMM_19_12_MASK) << RV_J_IMM_19_12_OFF) | \
+	(RV_IMM_SIGN(x_) << RV_J_IMM_SIGN_OFF); })
+
+#define RV_EXTRACT_ITYPE_IMM(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RV_I_IMM_11_0_OPOFF, RV_I_IMM_11_0_MASK)) | \
+	(RV_IMM_SIGN(x_) << RV_I_IMM_SIGN_OFF); })
+
+#define RV_EXTRACT_BTYPE_IMM(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RV_B_IMM_4_1_OPOFF, RV_B_IMM_4_1_MASK) << RV_B_IMM_4_1_OFF) | \
+	(RV_X_MASK(x_, RV_B_IMM_10_5_OPOFF, RV_B_IMM_10_5_MASK) << RV_B_IMM_10_5_OFF) | \
+	(RV_X_MASK(x_, RV_B_IMM_11_OPOFF, RV_B_IMM_11_MASK) << RV_B_IMM_11_OFF) | \
+	(RV_IMM_SIGN(x_) << RV_B_IMM_SIGN_OFF); })
+
+#define RVC_EXTRACT_C2_RS1_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X_MASK(x_, RVC_C2_RS1_OPOFF, RVC_C2_RS1_MASK)); })
+
+#define RVC_EXTRACT_JTYPE_IMM(x) \
+	({typeof(x) x_ = (x); \
+	(RVC_X(x_, RVC_J_IMM_3_1_OPOFF, RVC_J_IMM_3_1_MASK) << RVC_J_IMM_3_1_OFF) | \
+	(RVC_X(x_, RVC_J_IMM_4_OPOFF, RVC_J_IMM_4_MASK) << RVC_J_IMM_4_OFF) | \
+	(RVC_X(x_, RVC_J_IMM_5_OPOFF, RVC_J_IMM_5_MASK) << RVC_J_IMM_5_OFF) | \
+	(RVC_X(x_, RVC_J_IMM_6_OPOFF, RVC_J_IMM_6_MASK) << RVC_J_IMM_6_OFF) | \
+	(RVC_X(x_, RVC_J_IMM_7_OPOFF, RVC_J_IMM_7_MASK) << RVC_J_IMM_7_OFF) | \
+	(RVC_X(x_, RVC_J_IMM_9_8_OPOFF, RVC_J_IMM_9_8_MASK) << RVC_J_IMM_9_8_OFF) | \
+	(RVC_X(x_, RVC_J_IMM_10_OPOFF, RVC_J_IMM_10_MASK) << RVC_J_IMM_10_OFF) | \
+	(RVC_IMM_SIGN(x_) << RVC_J_IMM_SIGN_OFF); })
+
+#define RVC_EXTRACT_BTYPE_IMM(x) \
+	({typeof(x) x_ = (x); \
+	(RVC_X(x_, RVC_B_IMM_2_1_OPOFF, RVC_B_IMM_2_1_MASK) << RVC_B_IMM_2_1_OFF) | \
+	(RVC_X(x_, RVC_B_IMM_4_3_OPOFF, RVC_B_IMM_4_3_MASK) << RVC_B_IMM_4_3_OFF) | \
+	(RVC_X(x_, RVC_B_IMM_5_OPOFF, RVC_B_IMM_5_MASK) << RVC_B_IMM_5_OFF) | \
+	(RVC_X(x_, RVC_B_IMM_7_6_OPOFF, RVC_B_IMM_7_6_MASK) << RVC_B_IMM_7_6_OFF) | \
+	(RVC_IMM_SIGN(x_) << RVC_B_IMM_SIGN_OFF); })
+
+#define RVG_EXTRACT_SYSTEM_CSR(x) \
+	({typeof(x) x_ = (x); RV_X_MASK(x_, RVG_SYSTEM_CSR_OFF, RVG_SYSTEM_CSR_MASK); })
+
+#define RVFDQ_EXTRACT_FL_FS_WIDTH(x) \
+	({typeof(x) x_ = (x); RV_X_MASK(x_, RVFDQ_FL_FS_WIDTH_OFF, \
+				   RVFDQ_FL_FS_WIDTH_MASK); })
+
+#define RVV_EXTRACT_VL_VS_WIDTH(x) RVFDQ_EXTRACT_FL_FS_WIDTH(x)
+
+/*
+ * Get the immediate from a J-type instruction.
+ *
+ * @insn: instruction to process
+ * Return: immediate
+ */
+static inline s32 riscv_insn_extract_jtype_imm(u32 insn)
+{
+	return RV_EXTRACT_JTYPE_IMM(insn);
+}
+
+/*
+ * Update a J-type instruction with an immediate value.
+ *
+ * @insn: pointer to the jtype instruction
+ * @imm: the immediate to insert into the instruction
+ */
+static inline void riscv_insn_insert_jtype_imm(u32 *insn, s32 imm)
+{
+	/* drop the old IMMs, all jal IMM bits sit at 31:12 */
+	*insn &= ~GENMASK(31, 12);
+	*insn |= (RV_X_MASK(imm, RV_J_IMM_10_1_OFF, RV_J_IMM_10_1_MASK) << RV_J_IMM_10_1_OPOFF) |
+		 (RV_X_MASK(imm, RV_J_IMM_11_OFF, RV_J_IMM_11_MASK) << RV_J_IMM_11_OPOFF) |
+		 (RV_X_MASK(imm, RV_J_IMM_19_12_OFF, RV_J_IMM_19_12_MASK) << RV_J_IMM_19_12_OPOFF) |
+		 (RV_X_MASK(imm, RV_J_IMM_SIGN_OFF, 1) << RV_J_IMM_SIGN_OPOFF);
+}
+
+/*
+ * Put together one immediate from a U-type and I-type instruction pair.
+ *
+ * The U-type contains an upper immediate, meaning bits[31:12] with [11:0]
+ * being zero, while the I-type contains a 12bit immediate.
+ * Combined these can encode larger 32bit values and are used for example
+ * in auipc + jalr pairs to allow larger jumps.
+ *
+ * @utype_insn: instruction containing the upper immediate
+ * @itype_insn: instruction
+ * Return: combined immediate
+ */
+static inline s32 riscv_insn_extract_utype_itype_imm(u32 utype_insn, u32 itype_insn)
+{
+	s32 imm;
+
+	imm = RV_EXTRACT_UTYPE_IMM(utype_insn);
+	imm += RV_EXTRACT_ITYPE_IMM(itype_insn);
+
+	return imm;
+}
+
+/*
+ * Update a set of two instructions (U-type + I-type) with an immediate value.
+ *
+ * Used for example in auipc+jalrs pairs the U-type instructions contains
+ * a 20bit upper immediate representing bits[31:12], while the I-type
+ * instruction contains a 12bit immediate representing bits[11:0].
+ *
+ * This also takes into account that both separate immediates are
+ * considered as signed values, so if the I-type immediate becomes
+ * negative (BIT(11) set) the U-type part gets adjusted.
+ *
+ * @utype_insn: pointer to the utype instruction of the pair
+ * @itype_insn: pointer to the itype instruction of the pair
+ * @imm: the immediate to insert into the two instructions
+ */
+static inline void riscv_insn_insert_utype_itype_imm(u32 *utype_insn, u32 *itype_insn, s32 imm)
+{
+	/* drop possible old IMM values */
+	*utype_insn &= ~(RV_U_IMM_31_12_MASK);
+	*itype_insn &= ~(RV_I_IMM_11_0_MASK << RV_I_IMM_11_0_OPOFF);
+
+	/* add the adapted IMMs */
+	*utype_insn |= (imm & RV_U_IMM_31_12_MASK) + ((imm & BIT(11)) << 1);
+	*itype_insn |= ((imm & RV_I_IMM_11_0_MASK) << RV_I_IMM_11_0_OPOFF);
+}
+
+/*
+ * Get the immediate from a B-type instruction.
+ *
+ * @insn: instruction to process
+ * Return: immediate
+ */
+static inline s32 riscv_insn_extract_btype_imm(u32 insn)
+{
+	return RV_EXTRACT_BTYPE_IMM(insn);
+}
+
+/*
+ * Get the immediate from a RVC B-type instruction.
+ *
+ * @insn: instruction to process
+ * Return: immediate
+ */
+static inline s32 riscv_insn_extract_cbtype_imm(u32 insn)
+{
+	return RVC_EXTRACT_BTYPE_IMM(insn);
+}
+
+/*
+ * Get the immediate from a RVC J-type instruction.
+ *
+ * @insn: instruction to process
+ * Return: immediate
+ */
+static inline s32 riscv_insn_extract_cjtype_imm(u16 insn)
+{
+	return RVC_EXTRACT_JTYPE_IMM(insn);
+}
+
+#endif /* _ASM_RISCV_INSN_H */
tools/perf/arch/riscv/util/auxtrace.c +1 -0
index 5feee198ef97..a5450bef1a8a 100644
@@ -10,6 +10,7 @@
 #include <linux/zalloc.h>
 #include <linux/string.h>
 #include <time.h>
+#include <errno.h>
 
 #include <internal/lib.h> // page_size
 #include "../../../util/auxtrace.h"
tools/perf/util/Build +2 -0
index bcccad7487a9..648a8552df9e 100644
@@ -148,6 +148,8 @@ perf-util-y += cs-etm-decoder/
 endif
 perf-util-y += cs-etm-base.o
 
+perf-util-y += nexus-rv-decoder/
+
 perf-util-y += parse-branch-options.o
 perf-util-y += dump-insn.o
 perf-util-y += parse-regs-options.o
tools/perf/util/nexus-rv-decoder/Build new file +1 -0
new file mode 100644
index 000000000000..61cc960b1d04
@@ -0,0 +1 @@
+perf-util-y += nexus-rv-decoder.o
tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c new file +1364 -0

1,364 lines not on this page · raw message

tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h new file +139 -0

139 lines not on this page · raw message

tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h new file +190 -0

190 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#12 lore raw

RFC PATCH 11/12 perf symbols: Add RISC-V PLT entry sizes

In reply to Zane Leung

From: liangzhen <[email protected]>

Add PLT header and entry size definitions for RISC-V architecture.

RISC-V uses:
- PLT header size: 32 bytes
- PLT entry size: 16 bytes

This allows perf to correctly identify and display PLT symbols instead of showing them as [unknown].

---
 tools/perf/util/symbol-elf.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/perf/util/symbol-elf.c +4 -0
index 76912c62b6a0..e54ff59eb6ef 100644
@@ -379,6 +379,10 @@ static bool get_plt_sizes(struct dso *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr_plt,
 		*plt_header_size = 32;
 		*plt_entry_size = 16;
 		return true;
+	case EM_RISCV:
+		*plt_header_size = 32;
+		*plt_entry_size = 16;
+		return true;
 	case EM_SPARC:
 		*plt_header_size = 48;
 		*plt_entry_size = 12;
Signature · 1 line
2.34.1
author
#13 lore raw

RFC PATCH 12/12 perf tools: Integrate RISC-V trace decoder into auxtrace

In reply to Zane Leung

From: liangzhen <[email protected]>

Add RISC-V trace decoder implementation and integrate it into perf's auxtrace infrastructure. This enables processing of RISC-V Nexus trace data through the standard perf auxtrace pipeline.

---
 tools/perf/util/Build             |    1 +
 tools/perf/util/auxtrace.c        |    3 +
 tools/perf/util/rvtrace-decoder.c | 1039 +++++++++++++++++++++++++++++
 tools/perf/util/rvtrace.h         |    2 +
 4 files changed, 1045 insertions(+)
 create mode 100644 tools/perf/util/rvtrace-decoder.c
tools/perf/util/Build +1 -0
index 648a8552df9e..5913eec9b847 100644
@@ -148,6 +148,7 @@ perf-util-y += cs-etm-decoder/
 endif
 perf-util-y += cs-etm-base.o
 
+perf-util-y += rvtrace-decoder.o
 perf-util-y += nexus-rv-decoder/
 
 perf-util-y += parse-branch-options.o
tools/perf/util/auxtrace.c +3 -0
index 944a43d48739..7539252a4543 100644
@@ -56,6 +56,7 @@
 #include "s390-cpumsf.h"
 #include "util/mmap.h"
 #include "powerpc-vpadtl.h"
+#include "rvtrace.h"
 
 #include <linux/ctype.h>
 #include "symbol/kallsyms.h"
@@ -1412,6 +1413,8 @@ int perf_event__process_auxtrace_info(const struct perf_tool *tool __maybe_unuse
 		err = powerpc_vpadtl_process_auxtrace_info(event, session);
 		break;
 	case PERF_AUXTRACE_RISCV_TRACE:
+		err = rvtrace_process_auxtrace_info(event, session);
+		break;
 	case PERF_AUXTRACE_UNKNOWN:
 	default:
 		return -EINVAL;
tools/perf/util/rvtrace-decoder.c new file +1039 -0

1,039 lines not on this page · raw message

tools/perf/util/rvtrace.h +2 -0
index 1e48ed989dd7..84a59d3dc296 100644
@@ -35,4 +35,6 @@ enum {
 #define RVTRACE_HEADER_SIZE		(RVTRACE_HEADER_MAX * sizeof(u64))
 #define RVTRACE_ENCODER_PRIV_SIZE	(RVTRACE_ENCODER_PRIV_MAX * sizeof(u64))
 
+int rvtrace_process_auxtrace_info(union perf_event *event, struct perf_session *session);
+
 #endif
Signature · 1 line
2.34.1
#14 lore raw
In reply to Zane Leung
Quoted 2 lines · Zane Leung

From: liangzhen <[email protected]>

I dont think you have looped relevant maintainers and reviewers for the Coresight subsystem.

Thanks,
Jie

Quoted 331 lines

This series adds Linux RISC-V trace support via CoreSight, implementing RISC-V trace drivers within the CoreSight framework and integrating them with perf tools. The K3 SoC contains RISC-V Encoder, Funnel, ATB, CoreSight Funnel, and CoreSight TMC components, which can be directly leveraged through the existing CoreSight infrastructure.

Linux RISC-V trace support form Anup Patel:
(https://patchwork.kernel.org/project/linux-riscv/cover/[email protected]/)
which currently lacks ATB component support and guidance on reusing CoreSight components.

The series includes:
- RISC-V trace driver implementation within the CoreSight framework
- RISC-V Trace Encoder, Funnel, and ATB Bridge drivers as CoreSight devices
- RISC-V trace PMU record capabilities and parsing events in perf.
- RISC-V Nexus Trace decoder for perf tools

Any comments or suggestions are welcome.

Verification on K3 SoC:
To verify this patch series on K3 hardware, the following device tree are required:
1. RISC-V Trace Encoder node (8)
2. RISC-V ATB Bridge node (8)
3. RISC-V Trace Funnel node (2)
3. CoreSight Funnel configuration for RISC-V (1)
4. CoreSight TMC configuration for trace buffer (1)

/{
         dummy_clk: apb-pclk {
                 compatible = "fixed-clock";
                 #clock-cells = <0x0>;
                 clock-output-names = "clk14mhz";
                 clock-frequency = <14000000>;
         };


         soc: soc {
                 #address-cells = <2>;
                 #size-cells = <2>;

                 encoder0: encoder@d9002000 {
                         compatible = "riscv,trace-encoder";
                         reg = <0x0 0xd9002000 0x0 0x1000>;
                         cpus = <&cpu_0>;
                         out-ports {
                                 port {
                                         cluster0_encoder0_out_port: endpoint {
                                                 remote-endpoint = <&cluster0_bridge0_in_port>;
                                         };
                                 };
                         };
                 };

                 bridge0: bridge@d9003000 {
                         compatible = "riscv,trace-atbbridge";
                         reg = <0x0 0xd9003000 0x0 0x1000>;
                         cpus = <&cpu_0>;
                         out-ports {
                                 port {
                                         cluster0_bridge0_out_port: endpoint {
                                                 remote-endpoint = <&cluster0_funnel_in_port0>;
                                         };
                                 };
                         };
                         in-ports {
                                 port {
                                         cluster0_bridge0_in_port: endpoint {
                                                 remote-endpoint = <&cluster0_encoder0_out_port>;
                                         };
                                 };
                         };
                 };

...

                 cluster0_funnel: funnel@d9000000 {
                         compatible = "riscv,trace-funnel";
                         reg = <0x0 0xd9000000 0x0 0x1000>;
                         cpus = <&cpu_0>, <&cpu_1>, <&cpu_2>, <&cpu_3>;
                         riscv,timestamp-present;
                         out-ports {
                                 port {
                                         cluster0_funnel_out_port: endpoint {
                                                 remote-endpoint = <&main_funnel_in_port0>;
                                         };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         cluster0_funnel_in_port0: endpoint {
                                                 remote-endpoint = <&cluster0_bridge0_out_port>;
                                         };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         cluster0_funnel_in_port1: endpoint {
                                                 remote-endpoint = <&cluster0_bridge1_out_port>;
                                         };
                                 };

                                 port@2 {
                                         reg = <2>;
                                         cluster0_funnel_in_port2: endpoint {
                                                 remote-endpoint = <&cluster0_bridge2_out_port>;
                                         };
                                 };

                                 port@3 {
                                         reg = <3>;
                                         cluster0_funnel_in_port3: endpoint {
                                                 remote-endpoint = <&cluster0_bridge3_out_port>;
                                         };
                                 };
                         };
                 };

                 cluster1_funnel: funnel@d9010000 {
                         compatible = "riscv,trace-funnel";
                         reg = <0x0 0xd9010000 0x0 0x1000>;
                         cpus = <&cpu_4>, <&cpu_5>, <&cpu_6>, <&cpu_7>;
                         riscv,timestamp-present;
                         out-ports {
                                 port {
                                         cluster1_funnel_out_port: endpoint {
                                                 remote-endpoint = <&main_funnel_in_port1>;
                                         };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         cluster1_funnel_in_port0: endpoint {
                                                 remote-endpoint = <&cluster1_bridge0_out_port>;
                                         };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         cluster1_funnel_in_port1: endpoint {
                                                 remote-endpoint = <&cluster1_bridge1_out_port>;
                                         };
                                 };

                                 port@2 {
                                         reg = <2>;
                                         cluster1_funnel_in_port2: endpoint {
                                                 remote-endpoint = <&cluster1_bridge2_out_port>;
                                         };
                                 };

                                 port@3 {
                                         reg = <3>;
                                         cluster1_funnel_in_port3: endpoint {
                                                 remote-endpoint = <&cluster1_bridge3_out_port>;
                                         };
                                 };
                         };
                 };

                 main_funnel: funnel@d9042000 {
                         compatible = "arm,coresight-dynamic-funnel", "arm,primecell";
                         reg = <0x0 0xd9042000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         out-ports {
                                 port {
                                         main_funnel_out_port: endpoint {
                                                 remote-endpoint = <&etf_in_port>;
                                         };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         main_funnel_in_port0: endpoint {
                                                 remote-endpoint = <&cluster0_funnel_out_port>;
                                         };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         main_funnel_in_port1: endpoint {
                                                 remote-endpoint = <&cluster1_funnel_out_port>;
                                         };
                                 };
                         };
                 };

                 etf: etf@d9043000 {
                         compatible = "arm,coresight-tmc", "arm,primecell";
                         reg = <0x0 0xd9043000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         out-ports {
                                 port {
                                         etf_out_port: endpoint {
                                                 remote-endpoint = <&etr_in_port>;
                                         };
                                 };
                         };
                         in-ports {
                                 port {
                                         etf_in_port: endpoint {
                                                 remote-endpoint = <&main_funnel_out_port>;
                                         };
                                 };
                         };
                 };

                 etr: etr@d9044000 {
                         compatible = "arm,coresight-tmc", "arm,primecell";
                         reg = <0x0 0xd9044000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         arm,scatter-gather;
                         in-ports {
                                 port {
                                         etr_in_port: endpoint {
                                                 remote-endpoint = <&etf_out_port>;
                                         };
                                 };
                         };
                 };
         };
};

Verification case:

~ # perf list pmu
rvtrace// [Kernel PMU event]

~ # perf record -e rvtrace/@tmc_etr0/ --per-thread uname Linux
[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.191 MB perf.data ] ~ # perf script

            uname     137 [003]          1           branches:  ffffffff80931470 rvtrace_poll_bit+0x38 ([kernel.kallsyms]) => ffffffff80931492 rvtrace_poll_bit+0x5a ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff809328a6 encoder_enable_hw+0x252 ([kernel.kallsyms]) => ffffffff809328ba encoder_enable_hw+0x266 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff80932c4a encoder_enable+0x82 ([kernel.kallsyms]) => ffffffff80932c36 encoder_enable+0x6e ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff80928198 etm_event_start+0xf0 ([kernel.kallsyms]) => ffffffff809281aa etm_event_start+0x102 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff809281e6 etm_event_start+0x13e ([kernel.kallsyms]) => ffffffff8092755e coresight_get_sink_id+0x16 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8092820e etm_event_start+0x166 ([kernel.kallsyms]) => ffffffff80928226 etm_event_start+0x17e ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3bb4 perf_report_aux_output_id+0x0 ([kernel.kallsyms]) => ffffffff801c3bd6 perf_report_aux_output_id+0x22 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3c5a perf_report_aux_output_id+0xa6 ([kernel.kallsyms]) => ffffffff801c3bf0 perf_report_aux_output_id+0x3c ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3c40 perf_report_aux_output_id+0x8c ([kernel.kallsyms]) => ffffffff801c3aea __perf_event_header__init_id+0x2a ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3b42 __perf_event_header__init_id+0x82 ([kernel.kallsyms]) => ffffffff801c3b4a __perf_event_header__init_id+0x8a ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3bb0 __perf_event_header__init_id+0xf0 ([kernel.kallsyms]) => ffffffff801c3b58 __perf_event_header__init_id+0x98 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8004c658 __task_pid_nr_ns+0x0 ([kernel.kallsyms]) => ffffffff8004c696 __task_pid_nr_ns+0x3e ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8004c71e __task_pid_nr_ns+0xc6 ([kernel.kallsyms]) => ffffffff8004c6a4 __task_pid_nr_ns+0x4c ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms]) => ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms])

...

liangzhen (12):
coresight: Add RISC-V support to CoreSight tracing
coresight: Initial implementation of RISC-V trace driver
coresight: Add RISC-V Trace Encoder driver
coresight: Add RISC-V Trace Funnel driver
coresight: Add RISC-V Trace ATB Bridge driver
coresight rvtrace: Add timestamp component support for encoder and
funnel
coresight: Add RISC-V PMU name support
perf tools: riscv: making rvtrace PMU listable
perf tools: Add RISC-V trace PMU record capabilities perf tools: Add Nexus RISC-V Trace decoder
perf symbols: Add RISC-V PLT entry sizes
perf tools: Integrate RISC-V trace decoder into auxtrace

  drivers/hwtracing/Kconfig                     |    2 +
  drivers/hwtracing/coresight/Kconfig           |   46 +-
  drivers/hwtracing/coresight/Makefile          |    6 +
  drivers/hwtracing/coresight/coresight-core.c  |    8 +
  .../hwtracing/coresight/coresight-etm-perf.c  |    1 -
  .../hwtracing/coresight/coresight-etm-perf.h  |   21 +
  .../hwtracing/coresight/coresight-platform.c  |    1 -
  .../hwtracing/coresight/coresight-tmc-etf.c   |    4 +
  .../hwtracing/coresight/coresight-tmc-etr.c   |    4 +
  .../hwtracing/coresight/rvtrace-atbbridge.c   |  239 +++
  drivers/hwtracing/coresight/rvtrace-core.c    |  135 ++
  .../coresight/rvtrace-encoder-core.c          |  562 +++++++
  .../coresight/rvtrace-encoder-sysfs.c         |  363 +++++
  drivers/hwtracing/coresight/rvtrace-encoder.h |  151 ++
  drivers/hwtracing/coresight/rvtrace-funnel.c  |  337 ++++
  drivers/hwtracing/coresight/rvtrace-funnel.h  |   39 +
  .../hwtracing/coresight/rvtrace-timestamp.c   |  278 ++++
  .../hwtracing/coresight/rvtrace-timestamp.h   |   64 +
  include/linux/coresight-pmu.h                 |    4 +
  include/linux/rvtrace.h                       |  116 ++
  tools/arch/riscv/include/asm/insn.h           |  645 ++++++++
  tools/perf/arch/riscv/util/Build              |    2 +
  tools/perf/arch/riscv/util/auxtrace.c         |  490 ++++++
  tools/perf/arch/riscv/util/pmu.c              |   20 +
  tools/perf/util/Build                         |    3 +
  tools/perf/util/auxtrace.c                    |    4 +
  tools/perf/util/auxtrace.h                    |    1 +
  tools/perf/util/nexus-rv-decoder/Build        |    1 +
  .../util/nexus-rv-decoder/nexus-rv-decoder.c  | 1364 +++++++++++++++++
  .../util/nexus-rv-decoder/nexus-rv-decoder.h  |  139 ++
  .../perf/util/nexus-rv-decoder/nexus-rv-msg.h |  190 +++
  tools/perf/util/rvtrace-decoder.c             | 1039 +++++++++++++
  tools/perf/util/rvtrace.h                     |   40 +
  tools/perf/util/symbol-elf.c                  |    4 +
  34 files changed, 6320 insertions(+), 3 deletions(-)

create mode 100644 drivers/hwtracing/coresight/rvtrace-atbbridge.c create mode 100644 drivers/hwtracing/coresight/rvtrace-core.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-core.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder.h create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.c create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.h create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.c create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.h create mode 100644 include/linux/rvtrace.h
create mode 100644 tools/arch/riscv/include/asm/insn.h create mode 100644 tools/perf/arch/riscv/util/auxtrace.c create mode 100644 tools/perf/arch/riscv/util/pmu.c
create mode 100644 tools/perf/util/nexus-rv-decoder/Build create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h create mode 100644 tools/perf/util/rvtrace-decoder.c create mode 100644 tools/perf/util/rvtrace.h

author
#15 lore raw
Quoted 5 lines · Jie Gan

I dont think you have looped relevant maintainers and reviewers for the Coresight subsystem.

Thanks,
Jie

I will send it to all relevant maintainers and reviewers for the Coresight subsystem in next revision.

Thanks,
Zane

#16 lore raw
In reply to Zane Leung
Quoted 11 lines · Zane Leung

From: liangzhen <[email protected]>

This series adds Linux RISC-V trace support via CoreSight, implementing RISC-V trace drivers within the CoreSight framework and integrating them with perf tools. The K3 SoC contains RISC-V Encoder, Funnel, ATB, CoreSight Funnel, and CoreSight TMC components, which can be directly leveraged through the existing CoreSight infrastructure.

Linux RISC-V trace support form Anup Patel:
(https://patchwork.kernel.org/project/linux-riscv/cover/[email protected]/)
which currently lacks ATB component support and guidance on reusing CoreSight components.

What stops you from adding RISC-V trace funnel and ATB bridge drivers on top of this series ?

Quoted 3 lines

The series includes:
- RISC-V trace driver implementation within the CoreSight framework

The RISC-V trace very different from the CoreSight framework in many ways:
1) Types of components supported
2) Trace packet formats
3) The way MMIO based components are discoverd
4) ... and more ...

Quoted 148 lines

- RISC-V Trace Encoder, Funnel, and ATB Bridge drivers as CoreSight devices
- RISC-V trace PMU record capabilities and parsing events in perf.
- RISC-V Nexus Trace decoder for perf tools

Any comments or suggestions are welcome.

Verification on K3 SoC:
To verify this patch series on K3 hardware, the following device tree are required:
1. RISC-V Trace Encoder node (8)
2. RISC-V ATB Bridge node (8)
3. RISC-V Trace Funnel node (2)
3. CoreSight Funnel configuration for RISC-V (1)
4. CoreSight TMC configuration for trace buffer (1)

/{
        dummy_clk: apb-pclk {
                compatible = "fixed-clock";
                #clock-cells = <0x0>;
                clock-output-names = "clk14mhz";
                clock-frequency = <14000000>;
        };


        soc: soc {
                #address-cells = <2>;
                #size-cells = <2>;

                encoder0: encoder@d9002000 {
                        compatible = "riscv,trace-encoder";
                        reg = <0x0 0xd9002000 0x0 0x1000>;
                        cpus = <&cpu_0>;
                        out-ports {
                                port {
                                        cluster0_encoder0_out_port: endpoint {
                                                remote-endpoint = <&cluster0_bridge0_in_port>;
                                        };
                                };
                        };
                };

                bridge0: bridge@d9003000 {
                        compatible = "riscv,trace-atbbridge";
                        reg = <0x0 0xd9003000 0x0 0x1000>;
                        cpus = <&cpu_0>;
                        out-ports {
                                port {
                                        cluster0_bridge0_out_port: endpoint {
                                                remote-endpoint = <&cluster0_funnel_in_port0>;
                                        };
                                };
                        };
                        in-ports {
                                port {
                                        cluster0_bridge0_in_port: endpoint {
                                                remote-endpoint = <&cluster0_encoder0_out_port>;
                                        };
                                };
                        };
                };

...

                cluster0_funnel: funnel@d9000000 {
                        compatible = "riscv,trace-funnel";
                        reg = <0x0 0xd9000000 0x0 0x1000>;
                        cpus = <&cpu_0>, <&cpu_1>, <&cpu_2>, <&cpu_3>;
                        riscv,timestamp-present;
                        out-ports {
                                port {
                                        cluster0_funnel_out_port: endpoint {
                                                remote-endpoint = <&main_funnel_in_port0>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        cluster0_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster0_bridge0_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        cluster0_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster0_bridge1_out_port>;
                                        };
                                };

                                port@2 {
                                        reg = <2>;
                                        cluster0_funnel_in_port2: endpoint {
                                                remote-endpoint = <&cluster0_bridge2_out_port>;
                                        };
                                };

                                port@3 {
                                        reg = <3>;
                                        cluster0_funnel_in_port3: endpoint {
                                                remote-endpoint = <&cluster0_bridge3_out_port>;
                                        };
                                };
                        };
                };

                cluster1_funnel: funnel@d9010000 {
                        compatible = "riscv,trace-funnel";
                        reg = <0x0 0xd9010000 0x0 0x1000>;
                        cpus = <&cpu_4>, <&cpu_5>, <&cpu_6>, <&cpu_7>;
                        riscv,timestamp-present;
                        out-ports {
                                port {
                                        cluster1_funnel_out_port: endpoint {
                                                remote-endpoint = <&main_funnel_in_port1>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        cluster1_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster1_bridge0_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        cluster1_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster1_bridge1_out_port>;
                                        };
                                };

                                port@2 {
                                        reg = <2>;
                                        cluster1_funnel_in_port2: endpoint {
                                                remote-endpoint = <&cluster1_bridge2_out_port>;
                                        };
                                };

                                port@3 {
                                        reg = <3>;
                                        cluster1_funnel_in_port3: endpoint {
Quoted 8 lines
                                                remote-endpoint = <&cluster1_bridge3_out_port>;
                                        };
                                };
                        };
                };

                main_funnel: funnel@d9042000 {
                        compatible = "arm,coresight-dynamic-funnel", "arm,primecell";

Is it legally allowed to mix and match ARM coresight IPs with RISC-V trace components at hardware level ?

Quoted 164 lines
                        reg = <0x0 0xd9042000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        out-ports {
                                port {
                                        main_funnel_out_port: endpoint {
                                                remote-endpoint = <&etf_in_port>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        main_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster0_funnel_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        main_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster1_funnel_out_port>;
                                        };
                                };
                        };
                };

                etf: etf@d9043000 {
                        compatible = "arm,coresight-tmc", "arm,primecell";
                        reg = <0x0 0xd9043000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        out-ports {
                                port {
                                        etf_out_port: endpoint {
                                                remote-endpoint = <&etr_in_port>;
                                        };
                                };
                        };
                        in-ports {
                                port {
                                        etf_in_port: endpoint {
                                                remote-endpoint = <&main_funnel_out_port>;
                                        };
                                };
                        };
                };

                etr: etr@d9044000 {
                        compatible = "arm,coresight-tmc", "arm,primecell";
                        reg = <0x0 0xd9044000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        arm,scatter-gather;
                        in-ports {
                                port {
                                        etr_in_port: endpoint {
                                                remote-endpoint = <&etf_out_port>;
                                        };
                                };
                        };
                };
        };
};

Verification case:

~ # perf list pmu
rvtrace// [Kernel PMU event]

~ # perf record -e rvtrace/@tmc_etr0/ --per-thread uname Linux
[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.191 MB perf.data ] ~ # perf script

           uname     137 [003]          1           branches:  ffffffff80931470 rvtrace_poll_bit+0x38 ([kernel.kallsyms]) => ffffffff80931492 rvtrace_poll_bit+0x5a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff809328a6 encoder_enable_hw+0x252 ([kernel.kallsyms]) => ffffffff809328ba encoder_enable_hw+0x266 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff80932c4a encoder_enable+0x82 ([kernel.kallsyms]) => ffffffff80932c36 encoder_enable+0x6e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff80928198 etm_event_start+0xf0 ([kernel.kallsyms]) => ffffffff809281aa etm_event_start+0x102 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff809281e6 etm_event_start+0x13e ([kernel.kallsyms]) => ffffffff8092755e coresight_get_sink_id+0x16 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8092820e etm_event_start+0x166 ([kernel.kallsyms]) => ffffffff80928226 etm_event_start+0x17e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3bb4 perf_report_aux_output_id+0x0 ([kernel.kallsyms]) => ffffffff801c3bd6 perf_report_aux_output_id+0x22 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3c5a perf_report_aux_output_id+0xa6 ([kernel.kallsyms]) => ffffffff801c3bf0 perf_report_aux_output_id+0x3c ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3c40 perf_report_aux_output_id+0x8c ([kernel.kallsyms]) => ffffffff801c3aea __perf_event_header__init_id+0x2a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3b42 __perf_event_header__init_id+0x82 ([kernel.kallsyms]) => ffffffff801c3b4a __perf_event_header__init_id+0x8a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3bb0 __perf_event_header__init_id+0xf0 ([kernel.kallsyms]) => ffffffff801c3b58 __perf_event_header__init_id+0x98 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c658 __task_pid_nr_ns+0x0 ([kernel.kallsyms]) => ffffffff8004c696 __task_pid_nr_ns+0x3e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c71e __task_pid_nr_ns+0xc6 ([kernel.kallsyms]) => ffffffff8004c6a4 __task_pid_nr_ns+0x4c ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms]) => ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms])

...

liangzhen (12):
coresight: Add RISC-V support to CoreSight tracing
coresight: Initial implementation of RISC-V trace driver
coresight: Add RISC-V Trace Encoder driver
coresight: Add RISC-V Trace Funnel driver
coresight: Add RISC-V Trace ATB Bridge driver
coresight rvtrace: Add timestamp component support for encoder and
funnel
coresight: Add RISC-V PMU name support
perf tools: riscv: making rvtrace PMU listable
perf tools: Add RISC-V trace PMU record capabilities perf tools: Add Nexus RISC-V Trace decoder
perf symbols: Add RISC-V PLT entry sizes
perf tools: Integrate RISC-V trace decoder into auxtrace

 drivers/hwtracing/Kconfig                     |    2 +
 drivers/hwtracing/coresight/Kconfig           |   46 +-
 drivers/hwtracing/coresight/Makefile          |    6 +
 drivers/hwtracing/coresight/coresight-core.c  |    8 +
 .../hwtracing/coresight/coresight-etm-perf.c  |    1 -
 .../hwtracing/coresight/coresight-etm-perf.h  |   21 +
 .../hwtracing/coresight/coresight-platform.c  |    1 -
 .../hwtracing/coresight/coresight-tmc-etf.c   |    4 +
 .../hwtracing/coresight/coresight-tmc-etr.c   |    4 +
 .../hwtracing/coresight/rvtrace-atbbridge.c   |  239 +++
 drivers/hwtracing/coresight/rvtrace-core.c    |  135 ++
 .../coresight/rvtrace-encoder-core.c          |  562 +++++++
 .../coresight/rvtrace-encoder-sysfs.c         |  363 +++++
 drivers/hwtracing/coresight/rvtrace-encoder.h |  151 ++
 drivers/hwtracing/coresight/rvtrace-funnel.c  |  337 ++++
 drivers/hwtracing/coresight/rvtrace-funnel.h  |   39 +
 .../hwtracing/coresight/rvtrace-timestamp.c   |  278 ++++
 .../hwtracing/coresight/rvtrace-timestamp.h   |   64 +
 include/linux/coresight-pmu.h                 |    4 +
 include/linux/rvtrace.h                       |  116 ++
 tools/arch/riscv/include/asm/insn.h           |  645 ++++++++
 tools/perf/arch/riscv/util/Build              |    2 +
 tools/perf/arch/riscv/util/auxtrace.c         |  490 ++++++
 tools/perf/arch/riscv/util/pmu.c              |   20 +
 tools/perf/util/Build                         |    3 +
 tools/perf/util/auxtrace.c                    |    4 +
 tools/perf/util/auxtrace.h                    |    1 +
 tools/perf/util/nexus-rv-decoder/Build        |    1 +
 .../util/nexus-rv-decoder/nexus-rv-decoder.c  | 1364 +++++++++++++++++
 .../util/nexus-rv-decoder/nexus-rv-decoder.h  |  139 ++
 .../perf/util/nexus-rv-decoder/nexus-rv-msg.h |  190 +++
 tools/perf/util/rvtrace-decoder.c             | 1039 +++++++++++++
 tools/perf/util/rvtrace.h                     |   40 +
 tools/perf/util/symbol-elf.c                  |    4 +
 34 files changed, 6320 insertions(+), 3 deletions(-)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-atbbridge.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-core.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-core.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder.h
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.h
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.h
 create mode 100644 include/linux/rvtrace.h
 create mode 100644 tools/arch/riscv/include/asm/insn.h
 create mode 100644 tools/perf/arch/riscv/util/auxtrace.c
 create mode 100644 tools/perf/arch/riscv/util/pmu.c
 create mode 100644 tools/perf/util/nexus-rv-decoder/Build
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h
 create mode 100644 tools/perf/util/rvtrace-decoder.c
 create mode 100644 tools/perf/util/rvtrace.h
Quoted 3 lines

--
2.34.1

NACK to this approach of retrofitting RISC-V trace into ARM coresight.

Regards,
Anup

author
#17 lore raw
Quoted 8 lines · Anup Patel
Quoted 7 lines · Zane Leung

From: liangzhen <[email protected]>

This series adds Linux RISC-V trace support via CoreSight, implementing RISC-V trace drivers within the CoreSight framework and integrating them with perf tools. The K3 SoC contains RISC-V Encoder, Funnel, ATB, CoreSight Funnel, and CoreSight TMC components, which can be directly leveraged through the existing CoreSight infrastructure.

Quoted 6 lines
Quoted 3 lines

Linux RISC-V trace support form Anup Patel:
(https://patchwork.kernel.org/project/linux-riscv/cover/[email protected]/)
which currently lacks ATB component support and guidance on reusing CoreSight components.

What stops you from adding RISC-V trace funnel and ATB bridge drivers on top of this series ?

Firstly, my works started much earlier than this series. Secondly, it is difficult to add the coresight funnel and coresight tmc components to this series. Based on the coresight framework, I can directly reuse them.

Quoted 8 lines
Quoted 3 lines

The series includes:
- RISC-V trace driver implementation within the CoreSight framework

The RISC-V trace very different from the CoreSight framework in many ways:
1) Types of components supported
2) Trace packet formats
3) The way MMIO based components are discoverd
4) ... and more ...

1) I believe that RISC-V tracing is coresight-alike, where have encoders/funnel/sink/bridge that are described through DT and controlled by MMIO.
2) I think the difference in package format is nothing more than the coresight frame generated by the ATB component to distinguish the trace source. After removing it, it becomes riscv trace data.
3) The current CoreSight framework code does not introduce this mechanism, it is described through DT.

Quoted 154 lines
Quoted 154 lines

- RISC-V Trace Encoder, Funnel, and ATB Bridge drivers as CoreSight devices
- RISC-V trace PMU record capabilities and parsing events in perf.
- RISC-V Nexus Trace decoder for perf tools

Any comments or suggestions are welcome.

Verification on K3 SoC:
To verify this patch series on K3 hardware, the following device tree are required:
1. RISC-V Trace Encoder node (8)
2. RISC-V ATB Bridge node (8)
3. RISC-V Trace Funnel node (2)
3. CoreSight Funnel configuration for RISC-V (1)
4. CoreSight TMC configuration for trace buffer (1)

/{
        dummy_clk: apb-pclk {
                compatible = "fixed-clock";
                #clock-cells = <0x0>;
                clock-output-names = "clk14mhz";
                clock-frequency = <14000000>;
        };


        soc: soc {
                #address-cells = <2>;
                #size-cells = <2>;

                encoder0: encoder@d9002000 {
                        compatible = "riscv,trace-encoder";
                        reg = <0x0 0xd9002000 0x0 0x1000>;
                        cpus = <&cpu_0>;
                        out-ports {
                                port {
                                        cluster0_encoder0_out_port: endpoint {
                                                remote-endpoint = <&cluster0_bridge0_in_port>;
                                        };
                                };
                        };
                };

                bridge0: bridge@d9003000 {
                        compatible = "riscv,trace-atbbridge";
                        reg = <0x0 0xd9003000 0x0 0x1000>;
                        cpus = <&cpu_0>;
                        out-ports {
                                port {
                                        cluster0_bridge0_out_port: endpoint {
                                                remote-endpoint = <&cluster0_funnel_in_port0>;
                                        };
                                };
                        };
                        in-ports {
                                port {
                                        cluster0_bridge0_in_port: endpoint {
                                                remote-endpoint = <&cluster0_encoder0_out_port>;
                                        };
                                };
                        };
                };

...

                cluster0_funnel: funnel@d9000000 {
                        compatible = "riscv,trace-funnel";
                        reg = <0x0 0xd9000000 0x0 0x1000>;
                        cpus = <&cpu_0>, <&cpu_1>, <&cpu_2>, <&cpu_3>;
                        riscv,timestamp-present;
                        out-ports {
                                port {
                                        cluster0_funnel_out_port: endpoint {
                                                remote-endpoint = <&main_funnel_in_port0>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        cluster0_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster0_bridge0_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        cluster0_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster0_bridge1_out_port>;
                                        };
                                };

                                port@2 {
                                        reg = <2>;
                                        cluster0_funnel_in_port2: endpoint {
                                                remote-endpoint = <&cluster0_bridge2_out_port>;
                                        };
                                };

                                port@3 {
                                        reg = <3>;
                                        cluster0_funnel_in_port3: endpoint {
                                                remote-endpoint = <&cluster0_bridge3_out_port>;
                                        };
                                };
                        };
                };

                cluster1_funnel: funnel@d9010000 {
                        compatible = "riscv,trace-funnel";
                        reg = <0x0 0xd9010000 0x0 0x1000>;
                        cpus = <&cpu_4>, <&cpu_5>, <&cpu_6>, <&cpu_7>;
                        riscv,timestamp-present;
                        out-ports {
                                port {
                                        cluster1_funnel_out_port: endpoint {
                                                remote-endpoint = <&main_funnel_in_port1>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        cluster1_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster1_bridge0_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        cluster1_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster1_bridge1_out_port>;
                                        };
                                };

                                port@2 {
                                        reg = <2>;
                                        cluster1_funnel_in_port2: endpoint {
                                                remote-endpoint = <&cluster1_bridge2_out_port>;
                                        };
                                };

                                port@3 {
                                        reg = <3>;
                                        cluster1_funnel_in_port3: endpoint {
                                                remote-endpoint = <&cluster1_bridge3_out_port>;
                                        };
                                };
                        };
                };
Quoted 4 lines
Quoted 2 lines
                main_funnel: funnel@d9042000 {
                        compatible = "arm,coresight-dynamic-funnel", "arm,primecell";

Is it legally allowed to mix and match ARM coresight IPs with RISC-V trace components at hardware level ?

The ATB Bridge allows sending RISC-V trace to Arm CoreSight infrastructure (instead of RISC-V compliant sink defined in this document) as an ATB initiator.  see:
https://github.com/riscv-non-isa/riscv-nexus-trace/blob/105dc1c349556622e4d202d22b584a887ded462f/docs/RISC-V-Trace-Control-Interface.adoc#L184
For ATB Bridge, read trace using Coresight components (ETB/TMC/TPIU). see:
https://github.com/riscv-non-isa/riscv-nexus-trace/blob/105dc1c349556622e4d202d22b584a887ded462f/docs/RISC-V-Trace-Control-Interface.adoc#L1684

Quoted 165 lines
Quoted 165 lines
                        reg = <0x0 0xd9042000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        out-ports {
                                port {
                                        main_funnel_out_port: endpoint {
                                                remote-endpoint = <&etf_in_port>;
                                        };
                                };
                        };
                        in-ports {
                                #address-cells = <1>;
                                #size-cells = <0>;

                                port@0 {
                                        reg = <0>;
                                        main_funnel_in_port0: endpoint {
                                                remote-endpoint = <&cluster0_funnel_out_port>;
                                        };
                                };

                                port@1 {
                                        reg = <1>;
                                        main_funnel_in_port1: endpoint {
                                                remote-endpoint = <&cluster1_funnel_out_port>;
                                        };
                                };
                        };
                };

                etf: etf@d9043000 {
                        compatible = "arm,coresight-tmc", "arm,primecell";
                        reg = <0x0 0xd9043000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        out-ports {
                                port {
                                        etf_out_port: endpoint {
                                                remote-endpoint = <&etr_in_port>;
                                        };
                                };
                        };
                        in-ports {
                                port {
                                        etf_in_port: endpoint {
                                                remote-endpoint = <&main_funnel_out_port>;
                                        };
                                };
                        };
                };

                etr: etr@d9044000 {
                        compatible = "arm,coresight-tmc", "arm,primecell";
                        reg = <0x0 0xd9044000 0x0 0x1000>;
                        clocks = <&dummy_clk>;
                        clock-names = "apb_pclk";
                        arm,scatter-gather;
                        in-ports {
                                port {
                                        etr_in_port: endpoint {
                                                remote-endpoint = <&etf_out_port>;
                                        };
                                };
                        };
                };
        };
};

Verification case:

~ # perf list pmu
rvtrace// [Kernel PMU event]

~ # perf record -e rvtrace/@tmc_etr0/ --per-thread uname Linux
[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.191 MB perf.data ] ~ # perf script

           uname     137 [003]          1           branches:  ffffffff80931470 rvtrace_poll_bit+0x38 ([kernel.kallsyms]) => ffffffff80931492 rvtrace_poll_bit+0x5a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff809328a6 encoder_enable_hw+0x252 ([kernel.kallsyms]) => ffffffff809328ba encoder_enable_hw+0x266 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff80932c4a encoder_enable+0x82 ([kernel.kallsyms]) => ffffffff80932c36 encoder_enable+0x6e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff80928198 etm_event_start+0xf0 ([kernel.kallsyms]) => ffffffff809281aa etm_event_start+0x102 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff809281e6 etm_event_start+0x13e ([kernel.kallsyms]) => ffffffff8092755e coresight_get_sink_id+0x16 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8092820e etm_event_start+0x166 ([kernel.kallsyms]) => ffffffff80928226 etm_event_start+0x17e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3bb4 perf_report_aux_output_id+0x0 ([kernel.kallsyms]) => ffffffff801c3bd6 perf_report_aux_output_id+0x22 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3c5a perf_report_aux_output_id+0xa6 ([kernel.kallsyms]) => ffffffff801c3bf0 perf_report_aux_output_id+0x3c ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3c40 perf_report_aux_output_id+0x8c ([kernel.kallsyms]) => ffffffff801c3aea __perf_event_header__init_id+0x2a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3b42 __perf_event_header__init_id+0x82 ([kernel.kallsyms]) => ffffffff801c3b4a __perf_event_header__init_id+0x8a ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff801c3bb0 __perf_event_header__init_id+0xf0 ([kernel.kallsyms]) => ffffffff801c3b58 __perf_event_header__init_id+0x98 ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c658 __task_pid_nr_ns+0x0 ([kernel.kallsyms]) => ffffffff8004c696 __task_pid_nr_ns+0x3e ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c71e __task_pid_nr_ns+0xc6 ([kernel.kallsyms]) => ffffffff8004c6a4 __task_pid_nr_ns+0x4c ([kernel.kallsyms])
           uname     137 [003]          1           branches:  ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms]) => ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms])

...

liangzhen (12):
coresight: Add RISC-V support to CoreSight tracing
coresight: Initial implementation of RISC-V trace driver
coresight: Add RISC-V Trace Encoder driver
coresight: Add RISC-V Trace Funnel driver
coresight: Add RISC-V Trace ATB Bridge driver
coresight rvtrace: Add timestamp component support for encoder and
funnel
coresight: Add RISC-V PMU name support
perf tools: riscv: making rvtrace PMU listable
perf tools: Add RISC-V trace PMU record capabilities perf tools: Add Nexus RISC-V Trace decoder
perf symbols: Add RISC-V PLT entry sizes
perf tools: Integrate RISC-V trace decoder into auxtrace

 drivers/hwtracing/Kconfig                     |    2 +
 drivers/hwtracing/coresight/Kconfig           |   46 +-
 drivers/hwtracing/coresight/Makefile          |    6 +
 drivers/hwtracing/coresight/coresight-core.c  |    8 +
 .../hwtracing/coresight/coresight-etm-perf.c  |    1 -
 .../hwtracing/coresight/coresight-etm-perf.h  |   21 +
 .../hwtracing/coresight/coresight-platform.c  |    1 -
 .../hwtracing/coresight/coresight-tmc-etf.c   |    4 +
 .../hwtracing/coresight/coresight-tmc-etr.c   |    4 +
 .../hwtracing/coresight/rvtrace-atbbridge.c   |  239 +++
 drivers/hwtracing/coresight/rvtrace-core.c    |  135 ++
 .../coresight/rvtrace-encoder-core.c          |  562 +++++++
 .../coresight/rvtrace-encoder-sysfs.c         |  363 +++++
 drivers/hwtracing/coresight/rvtrace-encoder.h |  151 ++
 drivers/hwtracing/coresight/rvtrace-funnel.c  |  337 ++++
 drivers/hwtracing/coresight/rvtrace-funnel.h  |   39 +
 .../hwtracing/coresight/rvtrace-timestamp.c   |  278 ++++
 .../hwtracing/coresight/rvtrace-timestamp.h   |   64 +
 include/linux/coresight-pmu.h                 |    4 +
 include/linux/rvtrace.h                       |  116 ++
 tools/arch/riscv/include/asm/insn.h           |  645 ++++++++
 tools/perf/arch/riscv/util/Build              |    2 +
 tools/perf/arch/riscv/util/auxtrace.c         |  490 ++++++
 tools/perf/arch/riscv/util/pmu.c              |   20 +
 tools/perf/util/Build                         |    3 +
 tools/perf/util/auxtrace.c                    |    4 +
 tools/perf/util/auxtrace.h                    |    1 +
 tools/perf/util/nexus-rv-decoder/Build        |    1 +
 .../util/nexus-rv-decoder/nexus-rv-decoder.c  | 1364 +++++++++++++++++
 .../util/nexus-rv-decoder/nexus-rv-decoder.h  |  139 ++
 .../perf/util/nexus-rv-decoder/nexus-rv-msg.h |  190 +++
 tools/perf/util/rvtrace-decoder.c             | 1039 +++++++++++++
 tools/perf/util/rvtrace.h                     |   40 +
 tools/perf/util/symbol-elf.c                  |    4 +
 34 files changed, 6320 insertions(+), 3 deletions(-)
 create mode 100644 drivers/hwtracing/coresight/rvtrace-atbbridge.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-core.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-core.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder.h
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.h
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.c
 create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.h
 create mode 100644 include/linux/rvtrace.h
 create mode 100644 tools/arch/riscv/include/asm/insn.h
 create mode 100644 tools/perf/arch/riscv/util/auxtrace.c
 create mode 100644 tools/perf/arch/riscv/util/pmu.c
 create mode 100644 tools/perf/util/nexus-rv-decoder/Build
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h
 create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h
 create mode 100644 tools/perf/util/rvtrace-decoder.c
 create mode 100644 tools/perf/util/rvtrace.h
Quoted 4 lines
Quoted 3 lines

--
2.34.1

NACK to this approach of retrofitting RISC-V trace into ARM coresight.

I agree that integrating RISC-V trace directly into CoreSight is not a good approach, so I think we should abstract some of the logic of coresight and reuse it in RISC-V Trace.

Quoted 3 lines

Regards,
Anup

Thanks,
Zane

#18 lore raw

Hi Zane, Anup,

Quoted 356 lines · Zane Leung
Quoted 8 lines · Anup Patel
Quoted 7 lines · Zane Leung

From: liangzhen <[email protected]>

This series adds Linux RISC-V trace support via CoreSight, implementing RISC-V trace drivers within the CoreSight framework and integrating them with perf tools. The K3 SoC contains RISC-V Encoder, Funnel, ATB, CoreSight Funnel, and CoreSight TMC components, which can be directly leveraged through the existing CoreSight infrastructure.

Quoted 6 lines
Quoted 3 lines

Linux RISC-V trace support form Anup Patel:
(https://patchwork.kernel.org/project/linux-riscv/cover/[email protected]/)
which currently lacks ATB component support and guidance on reusing CoreSight components.

What stops you from adding RISC-V trace funnel and ATB bridge drivers on top of this series ?

Firstly, my works started much earlier than this series. Secondly, it is difficult to add the coresight funnel and coresight tmc components to this series. Based on the coresight framework, I can directly reuse them.

Quoted 8 lines
Quoted 3 lines

The series includes:
- RISC-V trace driver implementation within the CoreSight framework

The RISC-V trace very different from the CoreSight framework in many ways:
1) Types of components supported
2) Trace packet formats
3) The way MMIO based components are discoverd
4) ... and more ...

1) I believe that RISC-V tracing is coresight-alike, where have encoders/funnel/sink/bridge that are described through DT and controlled by MMIO.
2) I think the difference in package format is nothing more than the coresight frame generated by the ATB component to distinguish the trace source. After removing it, it becomes riscv trace data.
3) The current CoreSight framework code does not introduce this mechanism, it is described through DT.

Quoted 154 lines
Quoted 154 lines

- RISC-V Trace Encoder, Funnel, and ATB Bridge drivers as CoreSight devices
- RISC-V trace PMU record capabilities and parsing events in perf.
- RISC-V Nexus Trace decoder for perf tools

Any comments or suggestions are welcome.

Verification on K3 SoC:
To verify this patch series on K3 hardware, the following device tree are required:
1. RISC-V Trace Encoder node (8)
2. RISC-V ATB Bridge node (8)
3. RISC-V Trace Funnel node (2)
3. CoreSight Funnel configuration for RISC-V (1)
4. CoreSight TMC configuration for trace buffer (1)

/{
         dummy_clk: apb-pclk {
                 compatible = "fixed-clock";
                 #clock-cells = <0x0>;
                 clock-output-names = "clk14mhz";
                 clock-frequency = <14000000>;
         };


         soc: soc {
                 #address-cells = <2>;
                 #size-cells = <2>;

                 encoder0: encoder@d9002000 {
                         compatible = "riscv,trace-encoder";
                         reg = <0x0 0xd9002000 0x0 0x1000>;
                         cpus = <&cpu_0>;
                         out-ports {
                                 port {
                                         cluster0_encoder0_out_port: endpoint {
                                                 remote-endpoint = <&cluster0_bridge0_in_port>;
                                         };
                                 };
                         };
                 };

                 bridge0: bridge@d9003000 {
                         compatible = "riscv,trace-atbbridge";
                         reg = <0x0 0xd9003000 0x0 0x1000>;
                         cpus = <&cpu_0>;
                         out-ports {
                                 port {
                                         cluster0_bridge0_out_port: endpoint {
                                                 remote-endpoint = <&cluster0_funnel_in_port0>;
                                         };
                                 };
                         };
                         in-ports {
                                 port {
                                         cluster0_bridge0_in_port: endpoint {
                                                 remote-endpoint = <&cluster0_encoder0_out_port>;
                                         };
                                 };
                         };
                 };

...

                 cluster0_funnel: funnel@d9000000 {
                         compatible = "riscv,trace-funnel";
                         reg = <0x0 0xd9000000 0x0 0x1000>;
                         cpus = <&cpu_0>, <&cpu_1>, <&cpu_2>, <&cpu_3>;
                         riscv,timestamp-present;
                         out-ports {
                                 port {
                                         cluster0_funnel_out_port: endpoint {
                                                 remote-endpoint = <&main_funnel_in_port0>;
                                         };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         cluster0_funnel_in_port0: endpoint {
                                                 remote-endpoint = <&cluster0_bridge0_out_port>;
                                         };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         cluster0_funnel_in_port1: endpoint {
                                                 remote-endpoint = <&cluster0_bridge1_out_port>;
                                         };
                                 };

                                 port@2 {
                                         reg = <2>;
                                         cluster0_funnel_in_port2: endpoint {
                                                 remote-endpoint = <&cluster0_bridge2_out_port>;
                                         };
                                 };

                                 port@3 {
                                         reg = <3>;
                                         cluster0_funnel_in_port3: endpoint {
                                                 remote-endpoint = <&cluster0_bridge3_out_port>;
                                         };
                                 };
                         };
                 };

                 cluster1_funnel: funnel@d9010000 {
                         compatible = "riscv,trace-funnel";
                         reg = <0x0 0xd9010000 0x0 0x1000>;
                         cpus = <&cpu_4>, <&cpu_5>, <&cpu_6>, <&cpu_7>;
                         riscv,timestamp-present;
                         out-ports {
                                 port {
                                         cluster1_funnel_out_port: endpoint {
                                                 remote-endpoint = <&main_funnel_in_port1>;
                                         };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         cluster1_funnel_in_port0: endpoint {
                                                 remote-endpoint = <&cluster1_bridge0_out_port>;
                                         };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         cluster1_funnel_in_port1: endpoint {
                                                 remote-endpoint = <&cluster1_bridge1_out_port>;
                                         };
                                 };

                                 port@2 {
                                         reg = <2>;
                                         cluster1_funnel_in_port2: endpoint {
                                                 remote-endpoint = <&cluster1_bridge2_out_port>;
                                         };
                                 };

                                 port@3 {
                                         reg = <3>;
                                         cluster1_funnel_in_port3: endpoint {
                                                 remote-endpoint = <&cluster1_bridge3_out_port>;
                                         };
                                 };
                         };
                 };
Quoted 4 lines
Quoted 2 lines
                 main_funnel: funnel@d9042000 {
                         compatible = "arm,coresight-dynamic-funnel", "arm,primecell";

Is it legally allowed to mix and match ARM coresight IPs with RISC-V trace components at hardware level ?

The ATB Bridge allows sending RISC-V trace to Arm CoreSight infrastructure (instead of RISC-V compliant sink defined in this document) as an ATB initiator.  see:
https://github.com/riscv-non-isa/riscv-nexus-trace/blob/105dc1c349556622e4d202d22b584a887ded462f/docs/RISC-V-Trace-Control-Interface.adoc#L184
For ATB Bridge, read trace using Coresight components (ETB/TMC/TPIU). see:
https://github.com/riscv-non-isa/riscv-nexus-trace/blob/105dc1c349556622e4d202d22b584a887ded462f/docs/RISC-V-Trace-Control-Interface.adoc#L1684

Quoted 165 lines
Quoted 165 lines
                         reg = <0x0 0xd9042000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         out-ports {
                                 port {
                                         main_funnel_out_port: endpoint {
                                                 remote-endpoint = <&etf_in_port>;
                                         };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         main_funnel_in_port0: endpoint {
                                                 remote-endpoint = <&cluster0_funnel_out_port>;
                                         };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         main_funnel_in_port1: endpoint {
                                                 remote-endpoint = <&cluster1_funnel_out_port>;
                                         };
                                 };
                         };
                 };

                 etf: etf@d9043000 {
                         compatible = "arm,coresight-tmc", "arm,primecell";
                         reg = <0x0 0xd9043000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         out-ports {
                                 port {
                                         etf_out_port: endpoint {
                                                 remote-endpoint = <&etr_in_port>;
                                         };
                                 };
                         };
                         in-ports {
                                 port {
                                         etf_in_port: endpoint {
                                                 remote-endpoint = <&main_funnel_out_port>;
                                         };
                                 };
                         };
                 };

                 etr: etr@d9044000 {
                         compatible = "arm,coresight-tmc", "arm,primecell";
                         reg = <0x0 0xd9044000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         arm,scatter-gather;
                         in-ports {
                                 port {
                                         etr_in_port: endpoint {
                                                 remote-endpoint = <&etf_out_port>;
                                         };
                                 };
                         };
                 };
         };
};

Verification case:

~ # perf list pmu
rvtrace// [Kernel PMU event]

~ # perf record -e rvtrace/@tmc_etr0/ --per-thread uname Linux
[ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.191 MB perf.data ] ~ # perf script

            uname     137 [003]          1           branches:  ffffffff80931470 rvtrace_poll_bit+0x38 ([kernel.kallsyms]) => ffffffff80931492 rvtrace_poll_bit+0x5a ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff809328a6 encoder_enable_hw+0x252 ([kernel.kallsyms]) => ffffffff809328ba encoder_enable_hw+0x266 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff80932c4a encoder_enable+0x82 ([kernel.kallsyms]) => ffffffff80932c36 encoder_enable+0x6e ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff80928198 etm_event_start+0xf0 ([kernel.kallsyms]) => ffffffff809281aa etm_event_start+0x102 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff809281e6 etm_event_start+0x13e ([kernel.kallsyms]) => ffffffff8092755e coresight_get_sink_id+0x16 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8092820e etm_event_start+0x166 ([kernel.kallsyms]) => ffffffff80928226 etm_event_start+0x17e ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3bb4 perf_report_aux_output_id+0x0 ([kernel.kallsyms]) => ffffffff801c3bd6 perf_report_aux_output_id+0x22 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3c5a perf_report_aux_output_id+0xa6 ([kernel.kallsyms]) => ffffffff801c3bf0 perf_report_aux_output_id+0x3c ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3c40 perf_report_aux_output_id+0x8c ([kernel.kallsyms]) => ffffffff801c3aea __perf_event_header__init_id+0x2a ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3b42 __perf_event_header__init_id+0x82 ([kernel.kallsyms]) => ffffffff801c3b4a __perf_event_header__init_id+0x8a ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff801c3bb0 __perf_event_header__init_id+0xf0 ([kernel.kallsyms]) => ffffffff801c3b58 __perf_event_header__init_id+0x98 ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8004c658 __task_pid_nr_ns+0x0 ([kernel.kallsyms]) => ffffffff8004c696 __task_pid_nr_ns+0x3e ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8004c71e __task_pid_nr_ns+0xc6 ([kernel.kallsyms]) => ffffffff8004c6a4 __task_pid_nr_ns+0x4c ([kernel.kallsyms])
            uname     137 [003]          1           branches:  ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms]) => ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms])

...

liangzhen (12):
coresight: Add RISC-V support to CoreSight tracing
coresight: Initial implementation of RISC-V trace driver
coresight: Add RISC-V Trace Encoder driver
coresight: Add RISC-V Trace Funnel driver
coresight: Add RISC-V Trace ATB Bridge driver
coresight rvtrace: Add timestamp component support for encoder and
funnel
coresight: Add RISC-V PMU name support
perf tools: riscv: making rvtrace PMU listable
perf tools: Add RISC-V trace PMU record capabilities perf tools: Add Nexus RISC-V Trace decoder
perf symbols: Add RISC-V PLT entry sizes
perf tools: Integrate RISC-V trace decoder into auxtrace

  drivers/hwtracing/Kconfig                     |    2 +
  drivers/hwtracing/coresight/Kconfig           |   46 +-
  drivers/hwtracing/coresight/Makefile          |    6 +
  drivers/hwtracing/coresight/coresight-core.c  |    8 +
  .../hwtracing/coresight/coresight-etm-perf.c  |    1 -
  .../hwtracing/coresight/coresight-etm-perf.h  |   21 +
  .../hwtracing/coresight/coresight-platform.c  |    1 -
  .../hwtracing/coresight/coresight-tmc-etf.c   |    4 +
  .../hwtracing/coresight/coresight-tmc-etr.c   |    4 +
  .../hwtracing/coresight/rvtrace-atbbridge.c   |  239 +++
  drivers/hwtracing/coresight/rvtrace-core.c    |  135 ++
  .../coresight/rvtrace-encoder-core.c          |  562 +++++++
  .../coresight/rvtrace-encoder-sysfs.c         |  363 +++++
  drivers/hwtracing/coresight/rvtrace-encoder.h |  151 ++
  drivers/hwtracing/coresight/rvtrace-funnel.c  |  337 ++++
  drivers/hwtracing/coresight/rvtrace-funnel.h  |   39 +
  .../hwtracing/coresight/rvtrace-timestamp.c   |  278 ++++
  .../hwtracing/coresight/rvtrace-timestamp.h   |   64 +
  include/linux/coresight-pmu.h                 |    4 +
  include/linux/rvtrace.h                       |  116 ++
  tools/arch/riscv/include/asm/insn.h           |  645 ++++++++
  tools/perf/arch/riscv/util/Build              |    2 +
  tools/perf/arch/riscv/util/auxtrace.c         |  490 ++++++
  tools/perf/arch/riscv/util/pmu.c              |   20 +
  tools/perf/util/Build                         |    3 +
  tools/perf/util/auxtrace.c                    |    4 +
  tools/perf/util/auxtrace.h                    |    1 +
  tools/perf/util/nexus-rv-decoder/Build        |    1 +
  .../util/nexus-rv-decoder/nexus-rv-decoder.c  | 1364 +++++++++++++++++
  .../util/nexus-rv-decoder/nexus-rv-decoder.h  |  139 ++
  .../perf/util/nexus-rv-decoder/nexus-rv-msg.h |  190 +++
  tools/perf/util/rvtrace-decoder.c             | 1039 +++++++++++++
  tools/perf/util/rvtrace.h                     |   40 +
  tools/perf/util/symbol-elf.c                  |    4 +
  34 files changed, 6320 insertions(+), 3 deletions(-)

create mode 100644 drivers/hwtracing/coresight/rvtrace-atbbridge.c create mode 100644 drivers/hwtracing/coresight/rvtrace-core.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-core.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder.h create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.c create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.h create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.c create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.h create mode 100644 include/linux/rvtrace.h
create mode 100644 tools/arch/riscv/include/asm/insn.h create mode 100644 tools/perf/arch/riscv/util/auxtrace.c create mode 100644 tools/perf/arch/riscv/util/pmu.c
create mode 100644 tools/perf/util/nexus-rv-decoder/Build create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h create mode 100644 tools/perf/util/rvtrace-decoder.c create mode 100644 tools/perf/util/rvtrace.h

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

--
2.34.1

NACK to this approach of retrofitting RISC-V trace into ARM coresight.

I agree that integrating RISC-V trace directly into CoreSight is not a good approach, so I think we should abstract some of the logic of coresight and reuse it in RISC-V Trace.

I think we should indeed abstract out some coresight logic so it can be reused by both coresight/rvtrace, especially the way of handling trace component topology. The generic logic of building a path from source -> sink should be common among coresight/rvtrace and for future architecture. To support ATB use case like the K3, I think the best way to approach this is to model the atb driver as a separate rvtrace-atb drive and let it drive the ARM coresight components. Don't treat risc-v trace as another type of coresight.

@Zane, can you also clarify that only the 2 X100 clusters have trace encoders, and the A100 clusters do not? I don't see the 2 A100 clusters listed in your sample devicetree. If that's the case, then we'll be faced with heterogeneous trace setup, making it even more complicated.

Quoted 6 lines
Quoted 3 lines

Regards,
Anup

Thanks,
Zane

Bo

author
#19 lore raw

Hi Bo,

Quoted 374 lines · Bo Gan

Hi Zane, Anup,

Quoted 356 lines · Zane Leung
Quoted 8 lines · Anup Patel
Quoted 7 lines · Zane Leung

From: liangzhen <[email protected]>

This series adds Linux RISC-V trace support via CoreSight, implementing RISC-V trace drivers within the CoreSight framework and integrating them with perf tools. The K3 SoC contains RISC-V Encoder, Funnel, ATB, CoreSight Funnel, and CoreSight TMC components, which can be directly leveraged through the existing CoreSight infrastructure.

Quoted 6 lines
Quoted 3 lines

Linux RISC-V trace support form Anup Patel:
(https://patchwork.kernel.org/project/linux-riscv/cover/[email protected]/)
which currently lacks ATB component support and guidance on reusing CoreSight components.

What stops you from adding RISC-V trace funnel and ATB bridge drivers on top of this series ?

Firstly, my works started much earlier than this series. Secondly, it is difficult to add the coresight funnel and coresight tmc components to this series. Based on the coresight framework, I can directly reuse them.

Quoted 8 lines
Quoted 3 lines

The series includes:
- RISC-V trace driver implementation within the CoreSight framework

The RISC-V trace very different from the CoreSight framework in many ways:
1) Types of components supported
2) Trace packet formats
3) The way MMIO based components are discoverd
4) ... and more ...

1) I believe that RISC-V tracing is coresight-alike, where have encoders/funnel/sink/bridge that are described through DT and controlled by MMIO.
2) I think the difference in package format is nothing more than the coresight frame generated by the ATB component to distinguish the trace source. After removing it, it becomes riscv trace data.
3) The current CoreSight framework code does not introduce this mechanism, it is described through DT.

Quoted 154 lines
Quoted 154 lines

- RISC-V Trace Encoder, Funnel, and ATB Bridge drivers as CoreSight devices
- RISC-V trace PMU record capabilities and parsing events in perf.
- RISC-V Nexus Trace decoder for perf tools

Any comments or suggestions are welcome.

Verification on K3 SoC:
To verify this patch series on K3 hardware, the following device tree are required:
1. RISC-V Trace Encoder node (8)
2. RISC-V ATB Bridge node (8)
3. RISC-V Trace Funnel node (2)
3. CoreSight Funnel configuration for RISC-V (1)
4. CoreSight TMC configuration for trace buffer (1)

/{
         dummy_clk: apb-pclk {
                 compatible = "fixed-clock";
                 #clock-cells = <0x0>;
                 clock-output-names = "clk14mhz";
                 clock-frequency = <14000000>;
         };

         soc: soc {
                 #address-cells = <2>;
                 #size-cells = <2>;

                 encoder0: encoder@d9002000 {
                         compatible = "riscv,trace-encoder";
                         reg = <0x0 0xd9002000 0x0 0x1000>;
                         cpus = <&cpu_0>;
                         out-ports {
                                 port {
                                         cluster0_encoder0_out_port: endpoint { remote-endpoint = <&cluster0_bridge0_in_port>; };
                                 };
                         };
                 };

                 bridge0: bridge@d9003000 {
                         compatible = "riscv,trace-atbbridge";
                         reg = <0x0 0xd9003000 0x0 0x1000>;
                         cpus = <&cpu_0>;
                         out-ports {
                                 port {
                                         cluster0_bridge0_out_port: endpoint { remote-endpoint = <&cluster0_funnel_in_port0>; };
                                 };
                         };
                         in-ports {
                                 port {
                                         cluster0_bridge0_in_port: endpoint { remote-endpoint = <&cluster0_encoder0_out_port>; };
                                 };
                         };
                 };

...

                 cluster0_funnel: funnel@d9000000 {
                         compatible = "riscv,trace-funnel";
                         reg = <0x0 0xd9000000 0x0 0x1000>;
                         cpus = <&cpu_0>, <&cpu_1>, <&cpu_2>, <&cpu_3>; riscv,timestamp-present;
                         out-ports {
                                 port {
                                         cluster0_funnel_out_port: endpoint { remote-endpoint = <&main_funnel_in_port0>; };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         cluster0_funnel_in_port0: endpoint { remote-endpoint = <&cluster0_bridge0_out_port>; };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         cluster0_funnel_in_port1: endpoint { remote-endpoint = <&cluster0_bridge1_out_port>; };
                                 };

                                 port@2 {
                                         reg = <2>;
                                         cluster0_funnel_in_port2: endpoint { remote-endpoint = <&cluster0_bridge2_out_port>; };
                                 };

                                 port@3 {
                                         reg = <3>;
                                         cluster0_funnel_in_port3: endpoint { remote-endpoint = <&cluster0_bridge3_out_port>; };
                                 };
                         };
                 };

                 cluster1_funnel: funnel@d9010000 {
                         compatible = "riscv,trace-funnel";
                         reg = <0x0 0xd9010000 0x0 0x1000>;
                         cpus = <&cpu_4>, <&cpu_5>, <&cpu_6>, <&cpu_7>; riscv,timestamp-present;
                         out-ports {
                                 port {
                                         cluster1_funnel_out_port: endpoint { remote-endpoint = <&main_funnel_in_port1>; };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         cluster1_funnel_in_port0: endpoint { remote-endpoint = <&cluster1_bridge0_out_port>; };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         cluster1_funnel_in_port1: endpoint { remote-endpoint = <&cluster1_bridge1_out_port>; };
                                 };

                                 port@2 {
                                         reg = <2>;
                                         cluster1_funnel_in_port2: endpoint { remote-endpoint = <&cluster1_bridge2_out_port>; };
                                 };

                                 port@3 {
                                         reg = <3>;
                                         cluster1_funnel_in_port3: endpoint { remote-endpoint = <&cluster1_bridge3_out_port>; };
                                 };
                         };
                 };

Quoted 4 lines
Quoted 2 lines

                 main_funnel: funnel@d9042000 {
                         compatible = "arm,coresight-dynamic-funnel", "arm,primecell";

Is it legally allowed to mix and match ARM coresight IPs with RISC-V trace components at hardware level ?

The ATB Bridge allows sending RISC-V trace to Arm CoreSight infrastructure (instead of RISC-V compliant sink defined in this document) as an ATB initiator.  see:
https://github.com/riscv-non-isa/riscv-nexus-trace/blob/105dc1c349556622e4d202d22b584a887ded462f/docs/RISC-V-Trace-Control-Interface.adoc#L184
For ATB Bridge, read trace using Coresight components (ETB/TMC/TPIU). see:
https://github.com/riscv-non-isa/riscv-nexus-trace/blob/105dc1c349556622e4d202d22b584a887ded462f/docs/RISC-V-Trace-Control-Interface.adoc#L1684

Quoted 165 lines
Quoted 165 lines

                         reg = <0x0 0xd9042000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         out-ports {
                                 port {
                                         main_funnel_out_port: endpoint { remote-endpoint = <&etf_in_port>; };
                                 };
                         };
                         in-ports {
                                 #address-cells = <1>;
                                 #size-cells = <0>;

                                 port@0 {
                                         reg = <0>;
                                         main_funnel_in_port0: endpoint { remote-endpoint = <&cluster0_funnel_out_port>; };
                                 };

                                 port@1 {
                                         reg = <1>;
                                         main_funnel_in_port1: endpoint { remote-endpoint = <&cluster1_funnel_out_port>; };
                                 };
                         };
                 };

                 etf: etf@d9043000 {
                         compatible = "arm,coresight-tmc", "arm,primecell"; reg = <0x0 0xd9043000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         out-ports {
                                 port {
                                         etf_out_port: endpoint { remote-endpoint = <&etr_in_port>; };
                                 };
                         };
                         in-ports {
                                 port {
                                         etf_in_port: endpoint { remote-endpoint = <&main_funnel_out_port>; };
                                 };
                         };
                 };

                 etr: etr@d9044000 {
                         compatible = "arm,coresight-tmc", "arm,primecell"; reg = <0x0 0xd9044000 0x0 0x1000>;
                         clocks = <&dummy_clk>;
                         clock-names = "apb_pclk";
                         arm,scatter-gather;
                         in-ports {
                                 port {
                                         etr_in_port: endpoint { remote-endpoint = <&etf_out_port>; };
                                 };
                         };
                 };
         };
};

Verification case:

~ # perf list pmu
   rvtrace//                                          [Kernel PMU event]

~ # perf record -e rvtrace/@tmc_etr0/ --per-thread uname
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.191 MB perf.data ]
~ # perf script
            uname     137 [003]          1           branches:  ffffffff80931470 rvtrace_poll_bit+0x38 ([kernel.kallsyms]) => ffffffff80931492 rvtrace_poll_bit+0x5a ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff809328a6 encoder_enable_hw+0x252 ([kernel.kallsyms]) => ffffffff809328ba encoder_enable_hw+0x266 ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff80932c4a encoder_enable+0x82 ([kernel.kallsyms]) => ffffffff80932c36 encoder_enable+0x6e ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff80928198 etm_event_start+0xf0 ([kernel.kallsyms]) => ffffffff809281aa etm_event_start+0x102 ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff809281e6 etm_event_start+0x13e ([kernel.kallsyms]) => ffffffff8092755e coresight_get_sink_id+0x16 ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff8092820e etm_event_start+0x166 ([kernel.kallsyms]) => ffffffff80928226 etm_event_start+0x17e ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff801c3bb4 perf_report_aux_output_id+0x0 ([kernel.kallsyms]) => ffffffff801c3bd6 perf_report_aux_output_id+0x22 ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff801c3c5a perf_report_aux_output_id+0xa6 ([kernel.kallsyms]) => ffffffff801c3bf0 perf_report_aux_output_id+0x3c ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff801c3c40 perf_report_aux_output_id+0x8c ([kernel.kallsyms]) => ffffffff801c3aea __perf_event_header__init_id+0x2a ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff801c3b42 __perf_event_header__init_id+0x82 ([kernel.kallsyms]) => ffffffff801c3b4a __perf_event_header__init_id+0x8a ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff801c3bb0 __perf_event_header__init_id+0xf0 ([kernel.kallsyms]) => ffffffff801c3b58 __perf_event_header__init_id+0x98 ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff8004c658 __task_pid_nr_ns+0x0 ([kernel.kallsyms]) => ffffffff8004c696 __task_pid_nr_ns+0x3e ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff8004c71e __task_pid_nr_ns+0xc6 ([kernel.kallsyms]) => ffffffff8004c6a4 __task_pid_nr_ns+0x4c ([kernel.kallsyms]) uname     137 [003]          1           branches:  ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms]) => ffffffff8004c6e4 __task_pid_nr_ns+0x8c ([kernel.kallsyms])
...

liangzhen (12):
   coresight: Add RISC-V support to CoreSight tracing
   coresight: Initial implementation of RISC-V trace driver
   coresight: Add RISC-V Trace Encoder driver
   coresight: Add RISC-V Trace Funnel driver
   coresight: Add RISC-V Trace ATB Bridge driver
   coresight rvtrace: Add timestamp component support for encoder and funnel
   coresight: Add RISC-V PMU name support
   perf tools: riscv: making rvtrace PMU listable
   perf tools: Add RISC-V trace PMU record capabilities perf tools: Add Nexus RISC-V Trace decoder
   perf symbols: Add RISC-V PLT entry sizes
   perf tools: Integrate RISC-V trace decoder into auxtrace

  drivers/hwtracing/Kconfig                     |    2 + drivers/hwtracing/coresight/Kconfig           |   46 +- drivers/hwtracing/coresight/Makefile          |    6 +

  drivers/hwtracing/coresight/coresight-core.c  |    8 +
  .../hwtracing/coresight/coresight-etm-perf.c  |    1 -
  .../hwtracing/coresight/coresight-etm-perf.h  |   21 +
  .../hwtracing/coresight/coresight-platform.c  |    1 -
  .../hwtracing/coresight/coresight-tmc-etf.c   |    4 +
  .../hwtracing/coresight/coresight-tmc-etr.c   |    4 +
  .../hwtracing/coresight/rvtrace-atbbridge.c   |  239 +++

  drivers/hwtracing/coresight/rvtrace-core.c    |  135 ++ .../coresight/rvtrace-encoder-core.c          |  562 +++++++ .../coresight/rvtrace-encoder-sysfs.c         |  363 +++++ drivers/hwtracing/coresight/rvtrace-encoder.h |  151 ++ drivers/hwtracing/coresight/rvtrace-funnel.c  |  337 ++++ drivers/hwtracing/coresight/rvtrace-funnel.h  |   39 + .../hwtracing/coresight/rvtrace-timestamp.c   |  278 ++++ .../hwtracing/coresight/rvtrace-timestamp.h   |   64 + include/linux/coresight-pmu.h                 |    4 + include/linux/rvtrace.h                       |  116 ++ tools/arch/riscv/include/asm/insn.h           |  645 ++++++++ tools/perf/arch/riscv/util/Build              |    2 + tools/perf/arch/riscv/util/auxtrace.c         |  490 ++++++ tools/perf/arch/riscv/util/pmu.c              |   20 + tools/perf/util/Build                         |    3 + tools/perf/util/auxtrace.c                    |    4 + tools/perf/util/auxtrace.h                    |    1 + tools/perf/util/nexus-rv-decoder/Build        |    1 + .../util/nexus-rv-decoder/nexus-rv-decoder.c  | 1364 +++++++++++++++++ .../util/nexus-rv-decoder/nexus-rv-decoder.h  |  139 ++ .../perf/util/nexus-rv-decoder/nexus-rv-msg.h |  190 +++ tools/perf/util/rvtrace-decoder.c             | 1039 +++++++++++++ tools/perf/util/rvtrace.h                     |   40 + tools/perf/util/symbol-elf.c                  |    4 +

  34 files changed, 6320 insertions(+), 3 deletions(-)

  create mode 100644 drivers/hwtracing/coresight/rvtrace-atbbridge.c create mode 100644 drivers/hwtracing/coresight/rvtrace-core.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-core.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder-sysfs.c create mode 100644 drivers/hwtracing/coresight/rvtrace-encoder.h create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.c create mode 100644 drivers/hwtracing/coresight/rvtrace-funnel.h create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.c create mode 100644 drivers/hwtracing/coresight/rvtrace-timestamp.h create mode 100644 include/linux/rvtrace.h
  create mode 100644 tools/arch/riscv/include/asm/insn.h create mode 100644 tools/perf/arch/riscv/util/auxtrace.c create mode 100644 tools/perf/arch/riscv/util/pmu.c
  create mode 100644 tools/perf/util/nexus-rv-decoder/Build create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.c create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-decoder.h create mode 100644 tools/perf/util/nexus-rv-decoder/nexus-rv-msg.h create mode 100644 tools/perf/util/rvtrace-decoder.c create mode 100644 tools/perf/util/rvtrace.h

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines
Signature · 1 line
2.34.1

NACK to this approach of retrofitting RISC-V trace into ARM coresight.

I agree that integrating RISC-V trace directly into CoreSight is not a good approach, so I think we should abstract some of the logic of coresight and reuse it in RISC-V Trace.

I think we should indeed abstract out some coresight logic so it can be reused by both coresight/rvtrace, especially the way of handling trace component topology. The generic logic of building a path from source -> sink should be common among coresight/rvtrace and for future architecture. To support ATB use case like the K3, I think the best way to approach this is to model the atb driver as a separate rvtrace-atb drive and let it drive the ARM coresight components. Don't treat risc-v trace as another type of coresight.

Quoted 5 lines

@Zane, can you also clarify that only the 2 X100 clusters have trace encoders, and the A100 clusters do not? I don't see the 2 A100 clusters listed in your sample devicetree. If that's the case, then we'll be faced with heterogeneous trace setup, making it even more complicated.

The A100 clusters also have trace encoders, just like the X100 clusters. The reason I didn't include the two A100 clusters in the sample devicetree is to maintain consistency with the upstream k3.dtsi.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Regards,
Anup

Thanks,
Zane

Bo

Thanks,

Zane

author
#20 lore raw
In reply to Zane Leung

Hi, all
I plan to extracting CoreSight's architecture-independent framework code into a shared layer (drivers/hwtracing/trace-common/) to support multiple trace implementations (ARM CoreSight, RISC-V trace, etc.).

  ## Proposed Structure

  drivers/hwtracing/

  ├── trace-common/          # New: Generic framework layer
  │   ├── trace-core.c       # Device reg, path building/enable/disable
  │   ├── trace-core.h       # Public interfaces
  │   └── Makefile/Kconfig (CONFIG_TRACE_FRAMEWORK)
  │
  ├── coresight/             # Modified: ARM-specific only
  │   ├── coresight-core.c   # ARM CTI, claim/disclaim, module init
  │   ├── Makefile/Kconfig   # select TRACE_FRAMEWORK
  │   └── [existing ARM drivers]
  │
  └── rvtrace/               # New: RISC-V trace
      ├── [RISC-V components]
      └── Makefile/Kconfig   # select TRACE_FRAMEWORK

Is this architectural direction acceptable? Any comments or suggestions are welcome.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv