Skip to contentSkip to conversation

riscv: sbi: Add support to test PMU extension

PATCH In review James Raphael Tiovalen 2 participants 7 messages Dec 13, 2025 – Dec 23, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover riscv: sbi: Add support to test PMU extension

This patch series adds support for testing most of the SBI PMU extension functions. The functions related to shared memory (FID #7 and #8) are not tested yet.

The first 3 patches add the required support for SBI PMU and some helper functions, while the last patch adds the actual tests.

James Raphael Tiovalen (4):
lib: riscv: Add SBI PMU CSRs and enums
lib: riscv: Add SBI PMU support
lib: riscv: Add SBI PMU helper functions
riscv: sbi: Add tests for PMU extension

 riscv/Makefile      |   2 +
 lib/riscv/asm/csr.h |  31 +++
 lib/riscv/asm/pmu.h | 167 ++++++++++++++++
 lib/riscv/asm/sbi.h | 104 ++++++++++
 lib/riscv/pmu.c     | 169 ++++++++++++++++
 lib/riscv/sbi.c     |  73 +++++++
 riscv/sbi-tests.h   |   1 +
 riscv/sbi-pmu.c     | 461 ++++++++++++++++++++++++++++++++++++++++++++
 riscv/sbi.c         |   2 +
 9 files changed, 1010 insertions(+)
 create mode 100644 lib/riscv/asm/pmu.h
 create mode 100644 lib/riscv/pmu.c
 create mode 100644 riscv/sbi-pmu.c

--
2.43.0

#1 lore raw

PATCH 1/4 lib: riscv: Add SBI PMU CSRs and enums

Add the CSRs and enum values used by the RISC-V SBI PMU extension.

---
 lib/riscv/asm/csr.h | 31 +++++++++++++++++
 lib/riscv/asm/sbi.h | 82 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+)
lib/riscv/asm/csr.h +31 -0
index 6a8e0578..d5b1c7cc 100644
@@ -14,7 +14,38 @@
 #define CSR_STIMECMP		0x14d
 #define CSR_STIMECMPH		0x15d
 #define CSR_SATP		0x180
+#define CSR_CYCLE		0xc00
 #define CSR_TIME		0xc01
+#define CSR_INSTRET		0xc02
+#define CSR_HPMCOUNTER3		0xc03
+#define CSR_HPMCOUNTER4		0xc04
+#define CSR_HPMCOUNTER5		0xc05
+#define CSR_HPMCOUNTER6		0xc06
+#define CSR_HPMCOUNTER7		0xc07
+#define CSR_HPMCOUNTER8		0xc08
+#define CSR_HPMCOUNTER9		0xc09
+#define CSR_HPMCOUNTER10	0xc0a
+#define CSR_HPMCOUNTER11	0xc0b
+#define CSR_HPMCOUNTER12	0xc0c
+#define CSR_HPMCOUNTER13	0xc0d
+#define CSR_HPMCOUNTER14	0xc0e
+#define CSR_HPMCOUNTER15	0xc0f
+#define CSR_HPMCOUNTER16	0xc10
+#define CSR_HPMCOUNTER17	0xc11
+#define CSR_HPMCOUNTER18	0xc12
+#define CSR_HPMCOUNTER19	0xc13
+#define CSR_HPMCOUNTER20	0xc14
+#define CSR_HPMCOUNTER21	0xc15
+#define CSR_HPMCOUNTER22	0xc16
+#define CSR_HPMCOUNTER23	0xc17
+#define CSR_HPMCOUNTER24	0xc18
+#define CSR_HPMCOUNTER25	0xc19
+#define CSR_HPMCOUNTER26	0xc1a
+#define CSR_HPMCOUNTER27	0xc1b
+#define CSR_HPMCOUNTER28	0xc1c
+#define CSR_HPMCOUNTER29	0xc1d
+#define CSR_HPMCOUNTER30	0xc1e
+#define CSR_HPMCOUNTER31	0xc1f
 
 #define SR_SIE			_AC(0x00000002, UL)
 #define SR_SPP			_AC(0x00000100, UL)
lib/riscv/asm/sbi.h +82 -0

94 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 lib: riscv: Add SBI PMU support

In reply to James Raphael Tiovalen

Add support for all of the SBI PMU functions, which will be used by the SBI tests.

---
 lib/riscv/asm/sbi.h | 22 ++++++++++++++
 lib/riscv/sbi.c     | 73 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)
lib/riscv/asm/sbi.h +22 -0
index 35dbf508..8794c126 100644
@@ -390,5 +390,27 @@ struct sbiret sbi_fwft_set(uint32_t feature, unsigned long value, unsigned long
 struct sbiret sbi_fwft_get_raw(unsigned long feature);
 struct sbiret sbi_fwft_get(uint32_t feature);
 
+struct sbiret sbi_pmu_num_counters(void);
+struct sbiret sbi_pmu_counter_get_info(unsigned long counter_idx);
+struct sbiret sbi_pmu_counter_config_matching(unsigned long counter_idx_base,
+					      unsigned long counter_idx_mask,
+					      unsigned long config_flags,
+					      unsigned long event_idx,
+					      unsigned long event_data);
+struct sbiret sbi_pmu_counter_start(unsigned long counter_idx_base, unsigned long counter_idx_mask,
+				    unsigned long start_flags, unsigned long initial_value);
+struct sbiret sbi_pmu_counter_stop(unsigned long counter_idx_base, unsigned long counter_idx_mask,
+				   unsigned long stop_flags);
+struct sbiret sbi_pmu_counter_fw_read(unsigned long counter_idx);
+struct sbiret sbi_pmu_counter_fw_read_hi(unsigned long counter_idx);
+struct sbiret sbi_pmu_snapshot_set_shmem_raw(unsigned long shmem_phys_lo,
+					     unsigned long shmem_phys_hi,
+					     unsigned long flags);
+struct sbiret sbi_pmu_snapshot_set_shmem(unsigned long *shmem, unsigned long flags);
+struct sbiret sbi_pmu_event_get_info_raw(unsigned long shmem_phys_lo, unsigned long shmem_phys_hi,
+					 unsigned long num_entries, unsigned long flags);
+struct sbiret sbi_pmu_event_get_info(unsigned long *shmem, unsigned long num_entries,
+				     unsigned long flags);
+
 #endif /* !__ASSEMBLER__ */
 #endif /* _ASMRISCV_SBI_H_ */
lib/riscv/sbi.c +73 -0

79 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/4 lib: riscv: Add SBI PMU helper functions

In reply to James Raphael Tiovalen

Add some helper macros to handle event types and codes and some helper functions to access the FDT. These will be used by the SBI tests.

---
 riscv/Makefile      |   1 +
 lib/riscv/asm/pmu.h | 167 +++++++++++++++++++++++++++++++++++++++++++
 lib/riscv/pmu.c     | 169 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 337 insertions(+)
 create mode 100644 lib/riscv/asm/pmu.h
 create mode 100644 lib/riscv/pmu.c
riscv/Makefile +1 -0
index 64720c38..c0dd5465 100644
@@ -42,6 +42,7 @@ cflatobjs += lib/riscv/delay.o
 cflatobjs += lib/riscv/io.o
 cflatobjs += lib/riscv/isa.o
 cflatobjs += lib/riscv/mmu.o
+cflatobjs += lib/riscv/pmu.o
 cflatobjs += lib/riscv/processor.o
 cflatobjs += lib/riscv/sbi.o
 cflatobjs += lib/riscv/setjmp.o
lib/riscv/asm/pmu.h new file +167 -0

167 lines not on this page · raw message

lib/riscv/pmu.c new file +169 -0

169 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/4 riscv: sbi: Add tests for PMU extension

In reply to James Raphael Tiovalen

Add the actual tests for the SBI PMU extension. Functions related to shared memory (FID #7 and #8) are untested for now.

---
 riscv/Makefile    |   1 +
 riscv/sbi-tests.h |   1 +
 riscv/sbi-pmu.c   | 461 ++++++++++++++++++++++++++++++++++++++++++++++
 riscv/sbi.c       |   2 +
 4 files changed, 465 insertions(+)
 create mode 100644 riscv/sbi-pmu.c
riscv/Makefile +1 -0
index c0dd5465..75a108c1 100644
@@ -21,6 +21,7 @@ all: $(tests)
 sbi-deps += $(TEST_DIR)/sbi-asm.o
 sbi-deps += $(TEST_DIR)/sbi-dbtr.o
 sbi-deps += $(TEST_DIR)/sbi-fwft.o
+sbi-deps += $(TEST_DIR)/sbi-pmu.o
 sbi-deps += $(TEST_DIR)/sbi-sse.o
 
 all_deps += $(sbi-deps)
riscv/sbi-tests.h +1 -0
index c1ebf016..509ec547 100644
@@ -99,6 +99,7 @@ static inline bool env_enabled(const char *env)
 
 void split_phys_addr(phys_addr_t paddr, unsigned long *hi, unsigned long *lo);
 void sbi_bad_fid(int ext);
+void check_pmu(void);
 void check_sse(void);
 void check_dbtr(void);
 
riscv/sbi-pmu.c new file +461 -0

461 lines not on this page · raw message

riscv/sbi.c +2 -0
index 3b8aadce..fdb6a38a 100644
@@ -32,6 +32,7 @@
 
 #define	HIGH_ADDR_BOUNDARY	((phys_addr_t)1 << 32)
 
+void check_pmu(void);
 void check_sse(void);
 void check_fwft(void);
 
@@ -1557,6 +1558,7 @@ int main(int argc, char **argv)
 	check_time();
 	check_ipi();
 	check_hsm();
+	check_pmu();
 	check_dbcn();
 	check_susp();
 	check_sse();
Signature · 1 line
2.43.0
#5 lore raw
In reply to James Raphael Tiovalen
Quoted 27 lines · James Raphael Tiovalen

This patch series adds support for testing most of the SBI PMU extension functions. The functions related to shared memory (FID #7 and #8) are not tested yet.

The first 3 patches add the required support for SBI PMU and some helper functions, while the last patch adds the actual tests.

James Raphael Tiovalen (4):
lib: riscv: Add SBI PMU CSRs and enums
lib: riscv: Add SBI PMU support
lib: riscv: Add SBI PMU helper functions
riscv: sbi: Add tests for PMU extension

 riscv/Makefile      |   2 +
 lib/riscv/asm/csr.h |  31 +++
 lib/riscv/asm/pmu.h | 167 ++++++++++++++++
 lib/riscv/asm/sbi.h | 104 ++++++++++
 lib/riscv/pmu.c     | 169 ++++++++++++++++
 lib/riscv/sbi.c     |  73 +++++++
 riscv/sbi-tests.h   |   1 +
 riscv/sbi-pmu.c     | 461 ++++++++++++++++++++++++++++++++++++++++++++
 riscv/sbi.c         |   2 +
 9 files changed, 1010 insertions(+)
 create mode 100644 lib/riscv/asm/pmu.h
 create mode 100644 lib/riscv/pmu.c
 create mode 100644 riscv/sbi-pmu.c
Quoted 3 lines

--
2.43.0

Hi James,

Thanks for posting this. I'll look at it as soon as possible, but I'm juggling some other stuff right now and also plan to be on vacation for a week starting tomorrow.

Thanks,
drew

author
#6 lore raw
Quoted 33 lines · Andrew Jones
Quoted 27 lines · James Raphael Tiovalen

This patch series adds support for testing most of the SBI PMU extension functions. The functions related to shared memory (FID #7 and #8) are not tested yet.

The first 3 patches add the required support for SBI PMU and some helper functions, while the last patch adds the actual tests.

James Raphael Tiovalen (4):
lib: riscv: Add SBI PMU CSRs and enums
lib: riscv: Add SBI PMU support
lib: riscv: Add SBI PMU helper functions
riscv: sbi: Add tests for PMU extension

 riscv/Makefile      |   2 +
 lib/riscv/asm/csr.h |  31 +++
 lib/riscv/asm/pmu.h | 167 ++++++++++++++++
 lib/riscv/asm/sbi.h | 104 ++++++++++
 lib/riscv/pmu.c     | 169 ++++++++++++++++
 lib/riscv/sbi.c     |  73 +++++++
 riscv/sbi-tests.h   |   1 +
 riscv/sbi-pmu.c     | 461 ++++++++++++++++++++++++++++++++++++++++++++
 riscv/sbi.c         |   2 +
 9 files changed, 1010 insertions(+)
 create mode 100644 lib/riscv/asm/pmu.h
 create mode 100644 lib/riscv/pmu.c
 create mode 100644 riscv/sbi-pmu.c
Quoted 3 lines

--
2.43.0

Quoted 8 lines

Hi James,

Thanks for posting this. I'll look at it as soon as possible, but I'm juggling some other stuff right now and also plan to be on vacation for a week starting tomorrow.

Thanks,
drew

Hi Andrew,

No worries. Feel free to look at the series whenever you manage to find some time to do so.

Best regards,
James Raphael Tiovalen