Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
47ce956
RISC-V: perf: fix resource cleanup on driver probe failure
Jul 1, 2026
2f28cbc
RISC-V: Add Sxcsrind ISA extension CSR definitions
KevinRSX Jul 1, 2026
2272168
RISC-V: Add Sxcsrind ISA extension definition and parsing
atishp04 Jul 1, 2026
cb83f66
dt-bindings: riscv: add Sxcsrind ISA extension description
atishp04 Jul 1, 2026
1350eff
RISC-V: Define indirect CSR access helpers
atishp04 Jul 1, 2026
11d0d5f
RISC-V: Add Smcntrpmf extension parsing
atishp04 Jul 1, 2026
210ad8f
dt-bindings: riscv: add Smcntrpmf ISA extension description
atishp04 Jul 1, 2026
aa06bc3
RISC-V: Add Sscfg extension CSR definition
KevinRSX Jul 1, 2026
22922ad
RISC-V: Add Ssccfg/Smcdeleg ISA extension definition and parsing
atishp04 Jul 1, 2026
078c94d
dt-bindings: riscv: add Counter delegation ISA extensions description
atishp04 Jul 1, 2026
50bd70d
RISC-V: perf: Restructure the SBI PMU code
atishp04 Jul 1, 2026
ccfbb7a
RISC-V: perf: Modify the counter discovery mechanism
atishp04 Jul 1, 2026
915abd5
RISC-V: perf: Add a mechanism to defined legacy event encoding
atishp04 Jul 1, 2026
35f8006
RISC-V: perf: Implement supervisor counter delegation support
atishp04 Jul 1, 2026
d1452fe
RISC-V: perf: Skip PMU SBI extension when not implemented
charlie-rivos Jul 1, 2026
8345939
RISC-V: perf: Use config2/vendor table for event to counter mapping
atishp04 Jul 1, 2026
9a9f321
RISC-V: perf: Add legacy event encodings via sysfs
atishp04 Jul 1, 2026
7a14fcf
RISC-V: perf: Add Qemu virt machine events
atishp04 Jul 1, 2026
aa34008
tools/perf: Support event code for arch standard events
atishp04 Jul 1, 2026
4ac07be
tools/perf: Add RISC-V CounterIDMask event field
atishp04 Jul 1, 2026
16a6e9e
TEST(do-not-upstream): fake qemu-virt PMU events for cdeleg counter-m…
Jul 1, 2026
76c5d46
TEST(do-not-upstream): fake qemu vendor JSON + mapfile entry for Coun…
Jul 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Documentation/devicetree/bindings/riscv/extensions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ properties:
changes to interrupts as frozen at commit ccbddab ("Merge pull
request #42 from riscv/jhauser-2023-RC4") of riscv-aia.

- const: smcdeleg
description: |
The standard Smcdeleg machine-level extension for the machine mode
to delegate the hpmcounters to supervisor mode so that they are
directly accessible in the supervisor mode as ratified in the
20240213 version of the privileged ISA specification.

- const: smcntrpmf
description: |
The standard Smcntrpmf machine-level extension for the machine mode
to enable privilege mode filtering for cycle and instret counters as
ratified in the 20240326 version of the privileged ISA specification.

- const: smcsrind
description: |
The standard Smcsrind machine-level extension extends the
indirect CSR access mechanism defined by the Smaia extension. This
extension allows other ISA extensions to use indirect CSR access
mechanism in M-mode as ratified in the 20240326 version of the
privileged ISA specification.

- const: smmpm
description: |
The standard Smmpm extension for M-mode pointer masking as
Expand All @@ -206,6 +227,14 @@ properties:
behavioural changes to interrupts as frozen at commit ccbddab
("Merge pull request #42 from riscv/jhauser-2023-RC4") of riscv-aia.

- const: ssccfg
description: |
The standard Ssccfg supervisor-level extension for configuring
the delegated hpmcounters to be accessible directly in supervisor
mode as ratified in the 20240213 version of the privileged ISA
specification. This extension depends on Sscsrind, Smcdeleg, Zihpm,
Zicntr extensions.

- const: ssccptr
description: |
The standard Ssccptr extension for main memory (cacheability and
Expand All @@ -226,6 +255,14 @@ properties:
Profiles Version 1.0, with commit b1d806605f87 ("Updated to
ratified state.")

- const: sscsrind
description: |
The standard Sscsrind supervisor-level extension extends the
indirect CSR access mechanism defined by the Ssaia extension. This
extension allows other ISA extensions to use indirect CSR access
mechanism in S-mode as ratified in the 20240326 version of the
privileged ISA specification.

- const: ssnpm
description: |
The standard Ssnpm extension for next-mode pointer masking as
Expand Down Expand Up @@ -1113,6 +1150,32 @@ properties:
allOf:
- const: zilsd
- const: zca
# Smcdeleg depends on Sscsrind, Zihpm, Zicntr
- if:
contains:
const: smcdeleg
then:
allOf:
- contains:
const: sscsrind
- contains:
const: zihpm
- contains:
const: zicntr
# Ssccfg depends on Smcdeleg, Sscsrind, Zihpm, Zicntr
- if:
contains:
const: ssccfg
then:
allOf:
- contains:
const: smcdeleg
- contains:
const: sscsrind
- contains:
const: zihpm
- contains:
const: zicntr

allOf:
# Zcf extension does not exist on rv64
Expand Down
49 changes: 49 additions & 0 deletions arch/riscv/include/asm/csr.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@
#define SMSTATEEN0_HSENVCFG (_ULL(1) << SMSTATEEN0_HSENVCFG_SHIFT)
#define SMSTATEEN0_SSTATEEN0_SHIFT 63
#define SMSTATEEN0_SSTATEEN0 (_ULL(1) << SMSTATEEN0_SSTATEEN0_SHIFT)
/* HPMEVENT bits. These are accessible in S-mode via Smcdeleg/Ssccfg */
#define HPMEVENT_OF (BIT_ULL(63))
#define HPMEVENT_MINH (BIT_ULL(62))
#define HPMEVENT_SINH (BIT_ULL(61))
#define HPMEVENT_UINH (BIT_ULL(60))
#define HPMEVENT_VSINH (BIT_ULL(59))
#define HPMEVENT_VUINH (BIT_ULL(58))
#ifndef CONFIG_64BIT
#define HPMEVENTH_OF (BIT(31))
#define HPMEVENTH_MINH (BIT(30))
#define HPMEVENTH_SINH (BIT(29))
#define HPMEVENTH_UINH (BIT(28))
#define HPMEVENTH_VSINH (BIT(27))
#define HPMEVENTH_VUINH (BIT(26))
#endif

#define SISELECT_SSCCFG_BASE 0x40
#define HPMEVENT_MASK GENMASK_ULL(63, 56)

/* mseccfg bits */
#define MSECCFG_PMM ENVCFG_PMM
Expand Down Expand Up @@ -322,6 +340,7 @@
#define CSR_SCOUNTEREN 0x106
#define CSR_SENVCFG 0x10a
#define CSR_SSTATEEN0 0x10c
#define CSR_SCOUNTINHIBIT 0x120
#define CSR_SSCRATCH 0x140
#define CSR_SEPC 0x141
#define CSR_SCAUSE 0x142
Expand All @@ -347,6 +366,12 @@
/* Supervisor-Level Window to Indirectly Accessed Registers (AIA) */
#define CSR_SISELECT 0x150
#define CSR_SIREG 0x151
/* Supervisor-Level Window to Indirectly Accessed Registers (Sxcsrind) */
#define CSR_SIREG2 0x152
#define CSR_SIREG3 0x153
#define CSR_SIREG4 0x155
#define CSR_SIREG5 0x156
#define CSR_SIREG6 0x157

/* Supervisor-Level Interrupts (AIA) */
#define CSR_STOPEI 0x15c
Expand Down Expand Up @@ -394,6 +419,14 @@
/* VS-Level Window to Indirectly Accessed Registers (H-extension with AIA) */
#define CSR_VSISELECT 0x250
#define CSR_VSIREG 0x251
/*
* VS-Level Window to Indirectly Accessed Registers (H-extension with Sxcsrind)
*/
#define CSR_VSIREG2 0x252
#define CSR_VSIREG3 0x253
#define CSR_VSIREG4 0x255
#define CSR_VSIREG5 0x256
#define CSR_VSIREG6 0x257

/* VS-Level Interrupts (H-extension with AIA) */
#define CSR_VSTOPEI 0x25c
Expand Down Expand Up @@ -436,6 +469,12 @@
/* Machine-Level Window to Indirectly Accessed Registers (AIA) */
#define CSR_MISELECT 0x350
#define CSR_MIREG 0x351
/* Machine-Level Window to Indirectly Accessed Registers (Sxcsrind) */
#define CSR_MIREG2 0x352
#define CSR_MIREG3 0x353
#define CSR_MIREG4 0x355
#define CSR_MIREG5 0x356
#define CSR_MIREG6 0x357

/* Machine-Level Interrupts (AIA) */
#define CSR_MTOPEI 0x35c
Expand Down Expand Up @@ -498,6 +537,11 @@
# define CSR_IEH CSR_MIEH
# define CSR_ISELECT CSR_MISELECT
# define CSR_IREG CSR_MIREG
# define CSR_IREG2 CSR_MIREG2
# define CSR_IREG3 CSR_MIREG3
# define CSR_IREG4 CSR_MIREG4
# define CSR_IREG5 CSR_MIREG5
# define CSR_IREG6 CSR_MIREG6
# define CSR_IPH CSR_MIPH
# define CSR_TOPEI CSR_MTOPEI
# define CSR_TOPI CSR_MTOPI
Expand All @@ -523,6 +567,11 @@
# define CSR_IEH CSR_SIEH
# define CSR_ISELECT CSR_SISELECT
# define CSR_IREG CSR_SIREG
# define CSR_IREG2 CSR_SIREG2
# define CSR_IREG3 CSR_SIREG3
# define CSR_IREG4 CSR_SIREG4
# define CSR_IREG5 CSR_SIREG5
# define CSR_IREG6 CSR_SIREG6
# define CSR_IPH CSR_SIPH
# define CSR_TOPEI CSR_STOPEI
# define CSR_TOPI CSR_STOPI
Expand Down
41 changes: 41 additions & 0 deletions arch/riscv/include/asm/csr_ind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* SPDX-License-Identifier: GPL-2.0-only */

#ifndef _ASM_RISCV_CSR_IND_H
#define _ASM_RISCV_CSR_IND_H

#include <linux/irqflags.h>

#include <asm/csr.h>

#define csr_ind_read(iregcsr, iselbase, iseloff) ({ \
unsigned long __value = 0; \
unsigned long __flags; \
local_irq_save(__flags); \
csr_write(CSR_ISELECT, (iselbase) + (iseloff)); \
__value = csr_read(iregcsr); \
local_irq_restore(__flags); \
__value; \
})

#define csr_ind_write(iregcsr, iselbase, iseloff, value) ({ \
unsigned long __flags; \
local_irq_save(__flags); \
csr_write(CSR_ISELECT, (iselbase) + (iseloff)); \
csr_write(iregcsr, (value)); \
local_irq_restore(__flags); \
})

#define csr_ind_warl(iregcsr, iselbase, iseloff, warl_val) ({ \
unsigned long __old_val = 0, __value = 0; \
unsigned long __flags; \
local_irq_save(__flags); \
csr_write(CSR_ISELECT, (iselbase) + (iseloff)); \
__old_val = csr_read(iregcsr); \
csr_write(iregcsr, (warl_val)); \
__value = csr_read(iregcsr); \
csr_write(iregcsr, __old_val); \
local_irq_restore(__flags); \
__value; \
})

#endif
7 changes: 7 additions & 0 deletions arch/riscv/include/asm/hwcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
#define RISCV_ISA_EXT_ZCLSD 103
#define RISCV_ISA_EXT_ZICFILP 104
#define RISCV_ISA_EXT_ZICFISS 105
#define RISCV_ISA_EXT_SSCSRIND 106
#define RISCV_ISA_EXT_SMCSRIND 107
#define RISCV_ISA_EXT_SMCNTRPMF 108
#define RISCV_ISA_EXT_SSCCFG 109
#define RISCV_ISA_EXT_SMCDELEG 110

#define RISCV_ISA_EXT_XLINUXENVCFG 127

Expand All @@ -121,9 +126,11 @@
#ifdef CONFIG_RISCV_M_MODE
#define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA
#define RISCV_ISA_EXT_SUPM RISCV_ISA_EXT_SMNPM
#define RISCV_ISA_EXT_SxCSRIND RISCV_ISA_EXT_SMCSRIND
#else
#define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SSAIA
#define RISCV_ISA_EXT_SUPM RISCV_ISA_EXT_SSNPM
#define RISCV_ISA_EXT_SxCSRIND RISCV_ISA_EXT_SSCSRIND
#endif

#endif /* _ASM_RISCV_HWCAP_H */
6 changes: 6 additions & 0 deletions arch/riscv/include/asm/vendorid_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@
#define SIFIVE_VENDOR_ID 0x489
#define THEAD_VENDOR_ID 0x5b7

#define QEMU_VIRT_VENDOR_ID 0x000
#define QEMU_VIRT_IMPL_ID 0x000
#define QEMU_VIRT_ARCH_ID 0x000
/* Newer Qemu reports the spec-allocated marchid 0x2a (42) for non-vendor CPUs */
#define QEMU_VIRT_ARCH_ID_SPEC 0x2a

#endif
27 changes: 27 additions & 0 deletions arch/riscv/kernel/cpufeature.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,27 @@ static const unsigned int riscv_a_exts[] = {
RISCV_ISA_EXT_ZKNE, \
RISCV_ISA_EXT_ZKNH

static int riscv_ext_smcdeleg_validate(const struct riscv_isa_ext_data *data,
const unsigned long *isa_bitmap)
{
if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_SSCSRIND) &&
__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZIHPM) &&
__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZICNTR))
return 0;

return -EPROBE_DEFER;
}

static int riscv_ext_ssccfg_validate(const struct riscv_isa_ext_data *data,
const unsigned long *isa_bitmap)
{
if (!riscv_ext_smcdeleg_validate(data, isa_bitmap) &&
__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_SMCDELEG))
return 0;

return -EPROBE_DEFER;
}

static const unsigned int riscv_zk_bundled_exts[] = {
RISCV_ISA_EXT_ZKN,
RISCV_ISA_EXT_ZKR,
Expand Down Expand Up @@ -576,11 +597,17 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {
__RISCV_ISA_EXT_BUNDLE_VALIDATE(zvksg, riscv_zvksg_bundled_exts, riscv_ext_vector_crypto_validate),
__RISCV_ISA_EXT_DATA_VALIDATE(zvkt, RISCV_ISA_EXT_ZVKT, riscv_ext_vector_crypto_validate),
__RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
__RISCV_ISA_EXT_DATA_VALIDATE(smcdeleg, RISCV_ISA_EXT_SMCDELEG,
riscv_ext_smcdeleg_validate),
__RISCV_ISA_EXT_DATA(smcntrpmf, RISCV_ISA_EXT_SMCNTRPMF),
__RISCV_ISA_EXT_DATA(smcsrind, RISCV_ISA_EXT_SMCSRIND),
__RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM),
__RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts),
__RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
__RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
__RISCV_ISA_EXT_DATA_VALIDATE(ssccfg, RISCV_ISA_EXT_SSCCFG, riscv_ext_ssccfg_validate),
__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
__RISCV_ISA_EXT_DATA(sscsrind, RISCV_ISA_EXT_SSCSRIND),
__RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts),
__RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
__RISCV_ISA_EXT_DATA(svade, RISCV_ISA_EXT_SVADE),
Expand Down
14 changes: 9 additions & 5 deletions drivers/perf/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ config RISCV_PMU_LEGACY

config RISCV_PMU_SBI
depends on RISCV_PMU && RISCV_SBI
bool "RISC-V PMU based on SBI PMU extension"
bool "RISC-V PMU based on SBI PMU extension and/or counter delegation"
default y
help
Say y if you want to use the CPU performance monitor
using SBI PMU extension on RISC-V based systems. This option provides
full perf feature support i.e. counter overflow, privilege mode
filtering, counter configuration.
Say y if you want to use the CPU performance monitor on RISC-V based
systems. This single driver supports both hardware counter access
mechanisms: it uses the counter delegation (Smcdeleg/Ssccfg) ISA
extension to program and read the hpmcounters directly in supervisor
mode when available, and uses the SBI PMU extension for firmware
counters and when counter delegation is not present. This option
provides full perf feature support i.e. counter overflow, privilege
mode filtering, counter configuration.

config STARFIVE_STARLINK_PMU
depends on ARCH_STARFIVE || COMPILE_TEST
Expand Down
Loading
Loading