Skip to contentSkip to conversation

riscv: add support for SBI Supervisor Software Events

PATCH Applied Clément Léger 5 participants 15 messages Nov 5, 2025 – Mar 4

Subsystems (4)

v8 of 11 Applied 5/5 Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 cover riscv: add support for SBI Supervisor Software Events

The SBI Supervisor Software Events (SSE) extensions provides a mechanism to inject software events from an SBI implementation to supervisor software such that it preempts all other supervisor level traps and interrupts. This extension is introduced by the SBI v3.0 specification[1].

Various events are defined and can be send asynchronously to supervisor software (RAS, PMU, DEBUG, Asynchronous page fault) from SBI as well as platform specific events. Events can be either local (per-hart) or global. Events can be nested on top of each other based on priority and can interrupt the kernel at any time.

First patch adds the SSE definitions. Second one adds support for SSE at arch level (entry code and stack allocations) and third one at driver level. Finally, the last patch add support for SSE events in the SBI PMU driver. Additional testing for that part is highly welcomed since there are a lot of possible path that needs to be exercised.

Amongst the specific points that needs to be handle is the interruption at any point of the kernel execution and more specifically at the beginning of exception handling. Due to the fact that the exception entry implementation uses the SCRATCH CSR as both the current task struct and as the temporary register to switch the stack and save register, it is difficult to reliably get the current task struct if we get interrupted at this specific moment (ie, it might contain 0, the task pointer or tp). A fixup-like mechanism is not possible due to the nested nature of SSE which makes it really hard to obtain the original interruption site. In order to retrieve the task in a reliable manner, add an additional __sse_entry_task per_cpu array which stores the current task. Ideally, we would need to modify the way we retrieve/store the current task in exception handling so that it does not depend on the place where it's interrupted.

Contrary to pseudo NMI [2], SSE does not modifies the way interrupts are handled and does not adds any overhead to existing code. Moreover, it provides "true" NMI-like interrupts which can interrupt the kernel at any time (even in exception handling). This is particularly crucial for RAS errors which needs to be handled as fast as possible to avoid any fault propagation.

A test suite is available as a separate kselftest module. In order to build it, you can use the following command:

$ KDIR=<build_dir> make O=build TARGETS="riscv/sse"-j $(($(nproc)-1)) \
  -C tools/testing/selftests

Then load the module using:

$ sh run_sse_test.sh

A KVM SBI SSE extension implementation is available at [2].

---

Changes in V8:
- Use GPL-2.0-or-later licensing for all files
- Use sse_get_event() in sse cpu offline path
- Remove useless sse_reset() function.
- Removed useless cast of stack to unsigned long
- Replace pr_err() with pr_info() when SSE is missing
- Remove SIE set in do_sse(), irq_queue_work() will send an IPI to the cpu if needed via arch_irq_work_raise() so that it will be treated right after reenabling interrupts in interrupted context.

Changes in v7:
- Check return values of sse_on_each_cpu()
- Fix typos in commit
- Rename SBI_SSE_EVENT_SIGNAL to SBI_SSE_EVENT_INJECT
- Rename SBI_SSE_EVENT_HART_UNMASK/MASK to SBI_SSE_HART_UNMASK/MASK
- Add tlb flush for vmap stack to avoid taking exception during sse handler upon stack access. (Alex)
- Move some assembly instruction to slow path
- Renamed sse.c to sbi_sse.c, ditto for other files
- Renamed RISCV_SSE to RISCV_SBI_SSE
- Renamed sse_event_handler to sse_event_handler_fn
- Put ifdef around sse_evt in PMU SBI driver

Changes in v6:
- Fix comment in assembly argument
- Check hart id to be the expected one in order to skip CPU id matching in sse assembly.

Changes in v5:
- Added a SSE test module in kselftests
- Removed an unused variable
- Applied checkpatch.pl --strict and fix all errors
- Use scope_guard(cpus_read_lock) instead of manual cpus_read_lock()
- Fix wrong variable returned in sse_get_event
- Remove useless init of events list
- Remove useless empty for loop on cpus
- Set sse_available as __ro_after_init
- Changed a few pr_debug to pr_warn
- Fix event enabled stated updated in case of failure
- Change no_lock to nolock
- Rename attr_buf to attr
- renamed sse_get_event_phys() to sse_event_get_attr_phys() and removed the second argument
- Simplify return value in sse_event_attr_set_nolock()
- Remove while loop(-EINVAL) for event cpu set call
- Renamed interrupted_state_phys to interrupted_phys
- Use scoped_guards/guard for sse_mutex
- Remove useless struct forward declaration in sse.h
- Add more explanations as to why we set SIE bit in IP
- Unconditionnally set SIE in SIP
- Move SSE_STACK_SIZE adjustement in sse_stack_alloc/free()
- Replace move instructions with mv
- Rename NR_CPUS asm symbol to ASM_NR_CPUS
- Restore SSTATUS first in sse_entry return path so that it works for double trap without any modification later.
- Implement proper per cpu revert if enable/register fails

Changes in v4:
- Fix a bug when using per_cpu ptr for local event (Andrew)
- Add sse_event_disable/enable_local()
- Add pmu_disable/pmu_enable() to disable/enable SSE event
- Update event ID description according to the latest spec
- Fix comment about arguments in handle_sse()
- Added Himanchu as a SSE reviewer
- Used SYM_DATA_*() macros instead of hardcoded labels
- Invoke softirqs only if not returning to kernel with irqs disabled
- Remove invalid state check for write attribute function.
- Remove useless bneq statement in sse_entry.S

Changes in v3:
- Split arch/driver support
- Fix potential register failure reporting
- Set a few pr_err as pr_debug
- Allow CONFIG_RISCV_SSE to be disabled
- Fix build without CONFIG_RISCV_SSE
- Remove fixup-like mechanism and use a per-cpu array
- Fixed SSCRATCH being corrupted when interrupting the kernel in early exception path.
- Split SSE assembly from entry.S
- Add Himanchu SSE mask/unmask and runtime PM support.
- Disable user memory access/floating point/vector in SSE handler
- Rebased on master

v2: https://lore.kernel.org/linux-riscv/[email protected]/

Changes in v2:
- Implemented specification v2
- Fix various error handling cases
- Added shadow stack support

v1: https://lore.kernel.org/linux-riscv/[email protected]/

Clément Léger (5):
riscv: add SBI SSE extension definitions
riscv: add support for SBI Supervisor Software Events extension
drivers: firmware: add riscv SSE support
perf: RISC-V: add support for SSE event selftests/riscv: add SSE test module

 MAINTAINERS                                   |  15 +
 arch/riscv/include/asm/asm.h                  |  14 +-
 arch/riscv/include/asm/sbi.h                  |  61 ++
 arch/riscv/include/asm/scs.h                  |   7 +
 arch/riscv/include/asm/sse.h                  |  47 ++
 arch/riscv/include/asm/switch_to.h            |  14 +
 arch/riscv/include/asm/thread_info.h          |   1 +
 arch/riscv/kernel/Makefile                    |   1 +
 arch/riscv/kernel/asm-offsets.c               |  14 +
 arch/riscv/kernel/sbi_sse.c                   | 162 ++++
 arch/riscv/kernel/sbi_sse_entry.S             | 178 +++++
 drivers/firmware/Kconfig                      |   1 +
 drivers/firmware/Makefile                     |   1 +
 drivers/firmware/riscv/Kconfig                |  15 +
 drivers/firmware/riscv/Makefile               |   3 +
 drivers/firmware/riscv/riscv_sbi_sse.c        | 694 ++++++++++++++++++
 drivers/perf/Kconfig                          |  10 +
 drivers/perf/riscv_pmu.c                      |  23 +
 drivers/perf/riscv_pmu_sbi.c                  |  71 +-
 include/linux/perf/riscv_pmu.h                |   5 +
 include/linux/riscv_sbi_sse.h                 |  57 ++
 tools/testing/selftests/riscv/Makefile        |   2 +-
 tools/testing/selftests/riscv/sse/Makefile    |   5 +
 .../selftests/riscv/sse/module/Makefile       |  16 +
 .../riscv/sse/module/riscv_sse_test.c         | 511 +++++++++++++
 .../selftests/riscv/sse/run_sse_test.sh       |  44 ++
 26 files changed, 1958 insertions(+), 14 deletions(-)
 create mode 100644 arch/riscv/include/asm/sse.h
 create mode 100644 arch/riscv/kernel/sbi_sse.c
 create mode 100644 arch/riscv/kernel/sbi_sse_entry.S
 create mode 100644 drivers/firmware/riscv/Kconfig
 create mode 100644 drivers/firmware/riscv/Makefile
 create mode 100644 drivers/firmware/riscv/riscv_sbi_sse.c
 create mode 100644 include/linux/riscv_sbi_sse.h
 create mode 100644 tools/testing/selftests/riscv/sse/Makefile
 create mode 100644 tools/testing/selftests/riscv/sse/module/Makefile
 create mode 100644 tools/testing/selftests/riscv/sse/module/riscv_sse_test.c
 create mode 100644 tools/testing/selftests/riscv/sse/run_sse_test.sh
Signature · 1 line
2.43.0
#1 lore raw

PATCH v8 1/5 riscv: add SBI SSE extension definitions

Add needed definitions for SBI Supervisor Software Events extension [1]. This extension enables the SBI to inject events into supervisor software much like ARM SDEI.

[1] https://lists.riscv.org/g/tech-prs/message/515

---
 arch/riscv/include/asm/sbi.h | 61 ++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
arch/riscv/include/asm/sbi.h +61 -0
index ccc77a89b1e2..0f47e6f03b7e 100644
@@ -37,6 +37,7 @@ enum sbi_ext_id {
 	SBI_EXT_NACL = 0x4E41434C,
 	SBI_EXT_FWFT = 0x46574654,
 	SBI_EXT_MPXY = 0x4D505859,
+	SBI_EXT_SSE = 0x535345,
 
 	/* Experimentals extensions must lie within this range */
 	SBI_EXT_EXPERIMENTAL_START = 0x08000000,
@@ -505,6 +506,66 @@ enum sbi_mpxy_rpmi_attribute_id {
 #define SBI_MPXY_CHAN_CAP_SEND_WITHOUT_RESP	BIT(4)
 #define SBI_MPXY_CHAN_CAP_GET_NOTIFICATIONS	BIT(5)
 
+enum sbi_ext_sse_fid {
+	SBI_SSE_EVENT_ATTR_READ = 0,
+	SBI_SSE_EVENT_ATTR_WRITE,
+	SBI_SSE_EVENT_REGISTER,
+	SBI_SSE_EVENT_UNREGISTER,
+	SBI_SSE_EVENT_ENABLE,
+	SBI_SSE_EVENT_DISABLE,
+	SBI_SSE_EVENT_COMPLETE,
+	SBI_SSE_EVENT_INJECT,
+	SBI_SSE_HART_UNMASK,
+	SBI_SSE_HART_MASK,
+};
+
+enum sbi_sse_state {
+	SBI_SSE_STATE_UNUSED     = 0,
+	SBI_SSE_STATE_REGISTERED = 1,
+	SBI_SSE_STATE_ENABLED    = 2,
+	SBI_SSE_STATE_RUNNING    = 3,
+};
+
+/* SBI SSE Event Attributes. */
+enum sbi_sse_attr_id {
+	SBI_SSE_ATTR_STATUS		= 0x00000000,
+	SBI_SSE_ATTR_PRIO		= 0x00000001,
+	SBI_SSE_ATTR_CONFIG		= 0x00000002,
+	SBI_SSE_ATTR_PREFERRED_HART	= 0x00000003,
+	SBI_SSE_ATTR_ENTRY_PC		= 0x00000004,
+	SBI_SSE_ATTR_ENTRY_ARG		= 0x00000005,
+	SBI_SSE_ATTR_INTERRUPTED_SEPC	= 0x00000006,
+	SBI_SSE_ATTR_INTERRUPTED_FLAGS	= 0x00000007,
+	SBI_SSE_ATTR_INTERRUPTED_A6	= 0x00000008,
+	SBI_SSE_ATTR_INTERRUPTED_A7	= 0x00000009,
+
+	SBI_SSE_ATTR_MAX		= 0x0000000A
+};
+
+#define SBI_SSE_ATTR_STATUS_STATE_OFFSET	0
+#define SBI_SSE_ATTR_STATUS_STATE_MASK		0x3
+#define SBI_SSE_ATTR_STATUS_PENDING_OFFSET	2
+#define SBI_SSE_ATTR_STATUS_INJECT_OFFSET	3
+
+#define SBI_SSE_ATTR_CONFIG_ONESHOT	BIT(0)
+
+#define SBI_SSE_ATTR_INTERRUPTED_FLAGS_SSTATUS_SPP	BIT(0)
+#define SBI_SSE_ATTR_INTERRUPTED_FLAGS_SSTATUS_SPIE	BIT(1)
+#define SBI_SSE_ATTR_INTERRUPTED_FLAGS_HSTATUS_SPV	BIT(2)
+#define SBI_SSE_ATTR_INTERRUPTED_FLAGS_HSTATUS_SPVP	BIT(3)
+
+#define SBI_SSE_EVENT_LOCAL_HIGH_PRIO_RAS	0x00000000
+#define SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP		0x00000001
+#define SBI_SSE_EVENT_GLOBAL_HIGH_PRIO_RAS	0x00008000
+#define SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW	0x00010000
+#define SBI_SSE_EVENT_LOCAL_LOW_PRIO_RAS	0x00100000
+#define SBI_SSE_EVENT_GLOBAL_LOW_PRIO_RAS	0x00108000
+#define SBI_SSE_EVENT_LOCAL_SOFTWARE_INJECTED	0xffff0000
+#define SBI_SSE_EVENT_GLOBAL_SOFTWARE_INJECTED	0xffff8000
+
+#define SBI_SSE_EVENT_PLATFORM		BIT(14)
+#define SBI_SSE_EVENT_GLOBAL		BIT(15)
+
 /* SBI spec version fields */
 #define SBI_SPEC_VERSION_DEFAULT	0x1
 #define SBI_SPEC_VERSION_MAJOR_SHIFT	24
Signature · 1 line
2.43.0
#2 lore raw

PATCH v8 2/5 riscv: add support for SBI Supervisor Software Events extension

In reply to Clément Léger

The SBI SSE extension allows the supervisor software to be notified by the SBI of specific events that are not maskable. The context switch is handled partially by the firmware which will save registers a6 and a7. When entering kernel we can rely on these 2 registers to setup the stack and save all the registers.

Since SSE events can be delivered at any time to the kernel (including during exception handling, we need a way to locate the current_task for context tracking. On RISC-V, it is sotred in scratch when in user space or tp when in kernel space (in which case SSCRATCH is zero). But at a at the beginning of exception handling, SSCRATCH is used to swap tp and check the origin of the exception. If interrupted at that point, then, there is no way to reliably know were is located the current task_struct. Even checking the interruption location won't work as SSE event can be nested on top of each other so the original interruption site might be lost at some point. In order to retrieve it reliably, store the current task in an additional __sse_entry_task per_cpu array. This array is then used to retrieve the current task based on the hart ID that is passed to the SSE event handler in a6.

That being said, the way the current task struct is stored should probably be reworked to find a better reliable alternative.

Since each events (and each CPU for local events) have their own context and can preempt each other, allocate a stack (and a shadow stack if needed for each of them (and for each cpu for local events).

When completing the event, if we were coming from kernel with interrupts disabled, simply return there. If coming from userspace or kernel with interrupts enabled, simulate an interrupt exception by setting IE_SIE in CSR_IP to allow delivery of signals to user task. For instance this can happen, when a RAS event has been generated by a user application and a SIGBUS has been sent to a task.

---
 arch/riscv/include/asm/asm.h         |  14 ++-
 arch/riscv/include/asm/scs.h         |   7 ++
 arch/riscv/include/asm/sse.h         |  47 +++++++
 arch/riscv/include/asm/switch_to.h   |  14 +++
 arch/riscv/include/asm/thread_info.h |   1 +
 arch/riscv/kernel/Makefile           |   1 +
 arch/riscv/kernel/asm-offsets.c      |  14 +++
 arch/riscv/kernel/sbi_sse.c          | 162 ++++++++++++++++++++++++
 arch/riscv/kernel/sbi_sse_entry.S    | 178 +++++++++++++++++++++++++++
 9 files changed, 435 insertions(+), 3 deletions(-)
 create mode 100644 arch/riscv/include/asm/sse.h
 create mode 100644 arch/riscv/kernel/sbi_sse.c
 create mode 100644 arch/riscv/kernel/sbi_sse_entry.S
arch/riscv/include/asm/asm.h +11 -3
index ac28066bb564..a981d520f6d3 100644
@@ -84,16 +84,24 @@
 .endm
 
 #ifdef CONFIG_SMP
-.macro asm_per_cpu dst sym tmp
-	lw    \tmp, TASK_TI_CPU_NUM(tp)
-	slli  \tmp, \tmp, RISCV_LGPTR
+.macro asm_per_cpu_with_cpu dst sym tmp cpu
+	slli  \tmp, \cpu, RISCV_LGPTR
 	la    \dst, __per_cpu_offset
 	add   \dst, \dst, \tmp
 	REG_L \tmp, 0(\dst)
 	la    \dst, \sym
 	add   \dst, \dst, \tmp
 .endm
+
+.macro asm_per_cpu dst sym tmp
+	lw \tmp, TASK_TI_CPU_NUM(tp)
+	asm_per_cpu_with_cpu \dst \sym \tmp \tmp
+.endm
 #else /* CONFIG_SMP */
+.macro asm_per_cpu_with_cpu dst sym tmp cpu
+	la    \dst, \sym
+.endm
+
 .macro asm_per_cpu dst sym tmp
 	la    \dst, \sym
 .endm
arch/riscv/include/asm/scs.h +7 -0
index ab7714aa93bd..fd8242c54e90 100644
@@ -18,6 +18,11 @@
 	load_per_cpu gp, irq_shadow_call_stack_ptr, \tmp
 .endm
 
+/* Load the per-CPU IRQ shadow call stack to gp. */
+.macro scs_load_sse_stack reg_evt
+	REG_L gp, SSE_REG_EVT_SHADOW_STACK(\reg_evt)
+.endm
+
 /* Load task_scs_sp(current) to gp. */
 .macro scs_load_current
 	REG_L	gp, TASK_TI_SCS_SP(tp)
@@ -41,6 +46,8 @@
 .endm
 .macro scs_load_irq_stack tmp
 .endm
+.macro scs_load_sse_stack reg_evt
+.endm
 .macro scs_load_current
 .endm
 .macro scs_load_current_if_task_changed prev
arch/riscv/include/asm/sse.h new file +47 -0

47 lines not on this page · raw message

arch/riscv/include/asm/switch_to.h +14 -0
index 0e71eb82f920..70e68e630216 100644
@@ -88,6 +88,19 @@ static inline void __switch_to_envcfg(struct task_struct *next)
 			:: "r" (next->thread.envcfg) : "memory");
 }
 
+#ifdef CONFIG_RISCV_SBI_SSE
+DECLARE_PER_CPU(struct task_struct *, __sbi_sse_entry_task);
+
+static inline void __switch_sbi_sse_entry_task(struct task_struct *next)
+{
+	__this_cpu_write(__sbi_sse_entry_task, next);
+}
+#else
+static inline void __switch_sbi_sse_entry_task(struct task_struct *next)
+{
+}
+#endif
+
 extern struct task_struct *__switch_to(struct task_struct *,
 				       struct task_struct *);
 
@@ -122,6 +135,7 @@ do {							\
 	if (switch_to_should_flush_icache(__next))	\
 		local_flush_icache_all();		\
 	__switch_to_envcfg(__next);			\
+	__switch_sbi_sse_entry_task(__next);			\
 	((last) = __switch_to(__prev, __next));		\
 } while (0)
 
arch/riscv/include/asm/thread_info.h +1 -0
index 836d80dd2921..c5e1159cedcf 100644
@@ -36,6 +36,7 @@
 #define OVERFLOW_STACK_SIZE     SZ_4K
 
 #define IRQ_STACK_SIZE		THREAD_SIZE
+#define SSE_STACK_SIZE		THREAD_SIZE
 
 #ifndef __ASSEMBLER__
 
arch/riscv/kernel/Makefile +1 -0
index f60fce69b725..22bc8bdac079 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE)	+= mcount-dyn.o
 obj-$(CONFIG_PERF_EVENTS)	+= perf_callchain.o
 obj-$(CONFIG_HAVE_PERF_REGS)	+= perf_regs.o
 obj-$(CONFIG_RISCV_SBI)		+= sbi.o sbi_ecall.o
+obj-$(CONFIG_RISCV_SBI_SSE)	+= sbi_sse.o sbi_sse_entry.o
 ifeq ($(CONFIG_RISCV_SBI), y)
 obj-$(CONFIG_SMP)		+= sbi-ipi.o
 obj-$(CONFIG_SMP) += cpu_ops_sbi.o
arch/riscv/kernel/asm-offsets.c +14 -0
index 7d42d3b8a32a..dc456a4a0080 100644
@@ -15,6 +15,8 @@
 #include <asm/ptrace.h>
 #include <asm/cpu_ops_sbi.h>
 #include <asm/stacktrace.h>
+#include <asm/sbi.h>
+#include <asm/sse.h>
 #include <asm/suspend.h>
 
 void asm_offsets(void);
@@ -529,4 +531,16 @@ void asm_offsets(void)
 	DEFINE(FREGS_A6,	    offsetof(struct __arch_ftrace_regs, a6));
 	DEFINE(FREGS_A7,	    offsetof(struct __arch_ftrace_regs, a7));
 #endif
+
+#ifdef CONFIG_RISCV_SBI_SSE
+	OFFSET(SSE_REG_EVT_STACK, sse_event_arch_data, stack);
+	OFFSET(SSE_REG_EVT_SHADOW_STACK, sse_event_arch_data, shadow_stack);
+	OFFSET(SSE_REG_EVT_TMP, sse_event_arch_data, tmp);
+	OFFSET(SSE_REG_HART_ID, sse_event_arch_data, hart_id);
+	OFFSET(SSE_REG_CPU_ID, sse_event_arch_data, cpu_id);
+
+	DEFINE(SBI_EXT_SSE, SBI_EXT_SSE);
+	DEFINE(SBI_SSE_EVENT_COMPLETE, SBI_SSE_EVENT_COMPLETE);
+	DEFINE(ASM_NR_CPUS, NR_CPUS);
+#endif
 }
arch/riscv/kernel/sbi_sse.c new file +162 -0

162 lines not on this page · raw message

arch/riscv/kernel/sbi_sse_entry.S new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

PATCH v8 3/5 drivers: firmware: add riscv SSE support

In reply to Clément Léger

Add driver level interface to use RISC-V SSE arch support. This interface allows registering SSE handlers, and receive them. This will be used by PMU and GHES driver.

---
 MAINTAINERS                            |  15 +
 drivers/firmware/Kconfig               |   1 +
 drivers/firmware/Makefile              |   1 +
 drivers/firmware/riscv/Kconfig         |  15 +
 drivers/firmware/riscv/Makefile        |   3 +
 drivers/firmware/riscv/riscv_sbi_sse.c | 694 +++++++++++++++++++++++++
 include/linux/riscv_sbi_sse.h          |  57 ++
 7 files changed, 786 insertions(+)
 create mode 100644 drivers/firmware/riscv/Kconfig
 create mode 100644 drivers/firmware/riscv/Makefile
 create mode 100644 drivers/firmware/riscv/riscv_sbi_sse.c
 create mode 100644 include/linux/riscv_sbi_sse.h
MAINTAINERS +15 -0
index 46bd8e033042..f664fca6c9df 100644
@@ -22105,6 +22105,13 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
 F:	Documentation/devicetree/bindings/iommu/riscv,iommu.yaml
 F:	drivers/iommu/riscv/
 
+RISC-V FIRMWARE DRIVERS
+M:	Conor Dooley <[email protected]>
+L:	[email protected]
+S:	Maintained
+T:	git git://git.kernel.org/pub/scm/linux/kernel/git/conor/linux.git
+F:	drivers/firmware/riscv/*
+
 RISC-V MICROCHIP FPGA SUPPORT
 M:	Conor Dooley <[email protected]>
 M:	Daire McNamara <[email protected]>
@@ -22184,6 +22191,14 @@ F:	arch/riscv/boot/dts/spacemit/
 N:	spacemit
 K:	spacemit
 
+RISC-V SSE DRIVER
+M:	Clément Léger <[email protected]>
+R:	Himanshu Chauhan <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/firmware/riscv/riscv_sse.c
+F:	include/linux/riscv_sse.h
+
 RISC-V THEAD SoC SUPPORT
 M:	Drew Fustini <[email protected]>
 M:	Guo Ren <[email protected]>
drivers/firmware/Kconfig +1 -0
index bbd2155d8483..1894df87b08e 100644
@@ -294,6 +294,7 @@ source "drivers/firmware/meson/Kconfig"
 source "drivers/firmware/microchip/Kconfig"
 source "drivers/firmware/psci/Kconfig"
 source "drivers/firmware/qcom/Kconfig"
+source "drivers/firmware/riscv/Kconfig"
 source "drivers/firmware/samsung/Kconfig"
 source "drivers/firmware/smccc/Kconfig"
 source "drivers/firmware/tegra/Kconfig"
drivers/firmware/Makefile +1 -0
index 4ddec2820c96..6cdd84570ea7 100644
@@ -34,6 +34,7 @@ obj-y				+= efi/
 obj-y				+= imx/
 obj-y				+= psci/
 obj-y				+= qcom/
+obj-y				+= riscv/
 obj-y				+= samsung/
 obj-y				+= smccc/
 obj-y				+= tegra/
drivers/firmware/riscv/Kconfig new file +15 -0
new file mode 100644
index 000000000000..ed5b663ac5f9
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+menu "Risc-V Specific firmware drivers"
+depends on RISCV
+
+config RISCV_SBI_SSE
+	bool "Enable SBI Supervisor Software Events support"
+	depends on RISCV_SBI
+	default y
+	help
+	  The Supervisor Software Events support allows the SBI to deliver
+	  NMI-like notifications to the supervisor mode software. When enabled,
+	  this option provides support to register callbacks on specific SSE
+	  events.
+
+endmenu
drivers/firmware/riscv/Makefile new file +3 -0
new file mode 100644
index 000000000000..c8795d4bbb2e
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_RISCV_SBI_SSE)		+= riscv_sbi_sse.o
drivers/firmware/riscv/riscv_sbi_sse.c new file +694 -0

694 lines not on this page · raw message

include/linux/riscv_sbi_sse.h new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v8 3/5] drivers: firmware: add riscv SSE support

Hi Clément,

Quoted 740 lines · Clément Léger

Add driver level interface to use RISC-V SSE arch support. This interface allows registering SSE handlers, and receive them. This will be used by PMU and GHES driver.

---
 MAINTAINERS                            |  15 +
 drivers/firmware/Kconfig               |   1 +
 drivers/firmware/Makefile              |   1 +
 drivers/firmware/riscv/Kconfig         |  15 +
 drivers/firmware/riscv/Makefile        |   3 +
 drivers/firmware/riscv/riscv_sbi_sse.c | 694 +++++++++++++++++++++++++
 include/linux/riscv_sbi_sse.h          |  57 ++
 7 files changed, 786 insertions(+)
 create mode 100644 drivers/firmware/riscv/Kconfig
 create mode 100644 drivers/firmware/riscv/Makefile
 create mode 100644 drivers/firmware/riscv/riscv_sbi_sse.c
 create mode 100644 include/linux/riscv_sbi_sse.h
MAINTAINERS +15 -0
index 46bd8e033042..f664fca6c9df 100644
@@ -22105,6 +22105,13 @@ T:     git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
 F:     Documentation/devicetree/bindings/iommu/riscv,iommu.yaml
 F:     drivers/iommu/riscv/
 
+RISC-V FIRMWARE DRIVERS
+M:     Conor Dooley <[email protected]>
+L:     [email protected]
+S:     Maintained
+T:     git git://git.kernel.org/pub/scm/linux/kernel/git/conor/linux.git
+F:     drivers/firmware/riscv/*
+
 RISC-V MICROCHIP FPGA SUPPORT
 M:     Conor Dooley <[email protected]>
 M:     Daire McNamara <[email protected]>
@@ -22184,6 +22191,14 @@ F:     arch/riscv/boot/dts/spacemit/
 N:     spacemit
 K:     spacemit
 
+RISC-V SSE DRIVER
+M:     Clément Léger <[email protected]>
+R:     Himanshu Chauhan <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     drivers/firmware/riscv/riscv_sse.c
+F:     include/linux/riscv_sse.h
+
 RISC-V THEAD SoC SUPPORT
 M:     Drew Fustini <[email protected]>
 M:     Guo Ren <[email protected]>
drivers/firmware/Kconfig +1 -0
index bbd2155d8483..1894df87b08e 100644
@@ -294,6 +294,7 @@ source "drivers/firmware/meson/Kconfig"
 source "drivers/firmware/microchip/Kconfig"
 source "drivers/firmware/psci/Kconfig"
 source "drivers/firmware/qcom/Kconfig"
+source "drivers/firmware/riscv/Kconfig"
 source "drivers/firmware/samsung/Kconfig"
 source "drivers/firmware/smccc/Kconfig"
 source "drivers/firmware/tegra/Kconfig"
drivers/firmware/Makefile +1 -0
index 4ddec2820c96..6cdd84570ea7 100644
@@ -34,6 +34,7 @@ obj-y                         += efi/
 obj-y                          += imx/
 obj-y                          += psci/
 obj-y                          += qcom/
+obj-y                          += riscv/
 obj-y                          += samsung/
 obj-y                          += smccc/
 obj-y                          += tegra/
drivers/firmware/riscv/Kconfig new file +15 -0
new file mode 100644
index 000000000000..ed5b663ac5f9
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+menu "Risc-V Specific firmware drivers"
+depends on RISCV
+
+config RISCV_SBI_SSE
+       bool "Enable SBI Supervisor Software Events support"
+       depends on RISCV_SBI
+       default y
+       help
+         The Supervisor Software Events support allows the SBI to deliver
+         NMI-like notifications to the supervisor mode software. When enabled,
+         this option provides support to register callbacks on specific SSE
+         events.
+
+endmenu
drivers/firmware/riscv/Makefile new file +3 -0
new file mode 100644
index 000000000000..c8795d4bbb2e
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_RISCV_SBI_SSE)            += riscv_sbi_sse.o
drivers/firmware/riscv/riscv_sbi_sse.c new file +624 -0
new file mode 100644
index 000000000000..c7f29b10cdfb
@@ -0,0 +1,694 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2025 Rivos Inc.
+ */
+
+#define pr_fmt(fmt) "sse: " fmt
+
+#include <linux/cpu.h>
+#include <linux/cpuhotplug.h>
+#include <linux/cpu_pm.h>
+#include <linux/hardirq.h>
+#include <linux/list.h>
+#include <linux/percpu-defs.h>
+#include <linux/reboot.h>
+#include <linux/riscv_sbi_sse.h>
+#include <linux/slab.h>
+
+#include <asm/sbi.h>
+#include <asm/sse.h>
+
+struct sse_event {
+       struct list_head list;
+       u32 evt_id;
+       u32 priority;
+       sse_event_handler_fn *handler;
+       void *handler_arg;
+       /* Only valid for global events */
+       unsigned int cpu;
+
+       union {
+               struct sse_registered_event *global;
+               struct sse_registered_event __percpu *local;
+       };
+};
+
+static int sse_hp_state;
+static bool sse_available __ro_after_init;
+static DEFINE_SPINLOCK(events_list_lock);
+static LIST_HEAD(events);
+static DEFINE_MUTEX(sse_mutex);
+
+struct sse_registered_event {
+       struct sse_event_arch_data arch;
+       struct sse_event *event;
+       unsigned long attr;
+       bool is_enabled;
+};
+
+void sse_handle_event(struct sse_event_arch_data *arch_event,
+                     struct pt_regs *regs)
+{
+       int ret;
+       struct sse_registered_event *reg_evt =
+               container_of(arch_event, struct sse_registered_event, arch);
+       struct sse_event *evt = reg_evt->event;
+
+       ret = evt->handler(evt->evt_id, evt->handler_arg, regs);
+       if (ret)
+               pr_warn("event %x handler failed with error %d\n", evt->evt_id, ret);
+}
+
+static struct sse_event *sse_event_get(u32 evt)
+{
+       struct sse_event *event = NULL;
+
+       scoped_guard(spinlock, &events_list_lock) {
+               list_for_each_entry(event, &events, list) {
+                       if (event->evt_id == evt)
+                               return event;
+               }
+       }
+
+       return NULL;
+}
+
+static phys_addr_t sse_event_get_attr_phys(struct sse_registered_event *reg_evt)
+{
+       phys_addr_t phys;
+       void *addr = &reg_evt->attr;
+
+       if (sse_event_is_global(reg_evt->event->evt_id))
+               phys = virt_to_phys(addr);
+       else
+               phys = per_cpu_ptr_to_phys(addr);
+
+       return phys;
+}
+
+static struct sse_registered_event *sse_get_reg_evt(struct sse_event *event)
+{
+       if (sse_event_is_global(event->evt_id))
+               return event->global;
+       else
+               return per_cpu_ptr(event->local, smp_processor_id());
+}
+
+static int sse_sbi_event_func(struct sse_event *event, unsigned long func)
+{
+       struct sbiret ret;
+       u32 evt = event->evt_id;
+       struct sse_registered_event *reg_evt = sse_get_reg_evt(event);
+
+       ret = sbi_ecall(SBI_EXT_SSE, func, evt, 0, 0, 0, 0, 0);
+       if (ret.error) {
+               pr_warn("Failed to execute func %lx, event %x, error %ld\n",
+                       func, evt, ret.error);
+               return sbi_err_map_linux_errno(ret.error);
+       }
+
+       if (func == SBI_SSE_EVENT_DISABLE)
+               reg_evt->is_enabled = false;
+       else if (func == SBI_SSE_EVENT_ENABLE)
+               reg_evt->is_enabled = true;
+
+       return 0;
+}
+
+int sse_event_disable_local(struct sse_event *event)
+{
+       return sse_sbi_event_func(event, SBI_SSE_EVENT_DISABLE);
+}
+EXPORT_SYMBOL_GPL(sse_event_disable_local);
+
+int sse_event_enable_local(struct sse_event *event)
+{
+       return sse_sbi_event_func(event, SBI_SSE_EVENT_ENABLE);
+}
+EXPORT_SYMBOL_GPL(sse_event_enable_local);
+
+static int sse_event_attr_get_no_lock(struct sse_registered_event *reg_evt,
+                                     unsigned long attr_id, unsigned long *val)
+{
+       struct sbiret sret;
+       u32 evt = reg_evt->event->evt_id;
+       unsigned long phys;
+
+       phys = sse_event_get_attr_phys(reg_evt);
+
+       sret = sbi_ecall(SBI_EXT_SSE, SBI_SSE_EVENT_ATTR_READ, evt, attr_id, 1,
+                        phys, 0, 0);
+       if (sret.error) {
+               pr_debug("Failed to get event %x attr %lx, error %ld\n", evt,
+                        attr_id, sret.error);
+               return sbi_err_map_linux_errno(sret.error);
+       }
+
+       *val = reg_evt->attr;
+
+       return 0;
+}
+
+static int sse_event_attr_set_nolock(struct sse_registered_event *reg_evt,
+                                    unsigned long attr_id, unsigned long val)
+{
+       struct sbiret sret;
+       u32 evt = reg_evt->event->evt_id;
+       unsigned long phys;
+
+       reg_evt->attr = val;
+       phys = sse_event_get_attr_phys(reg_evt);
+
+       sret = sbi_ecall(SBI_EXT_SSE, SBI_SSE_EVENT_ATTR_WRITE, evt, attr_id, 1,
+                        phys, 0, 0);
+       if (sret.error)
+               pr_debug("Failed to set event %x attr %lx, error %ld\n", evt,
+                        attr_id, sret.error);
+
+       return sbi_err_map_linux_errno(sret.error);
+}
+
+static void sse_global_event_update_cpu(struct sse_event *event,
+                                       unsigned int cpu)
+{
+       struct sse_registered_event *reg_evt = event->global;
+
+       event->cpu = cpu;
+       arch_sse_event_update_cpu(&reg_evt->arch, cpu);
+}
+
+static int sse_event_set_target_cpu_nolock(struct sse_event *event,
+                                          unsigned int cpu)
+{
+       unsigned long hart_id = cpuid_to_hartid_map(cpu);
+       struct sse_registered_event *reg_evt = event->global;
+       u32 evt = event->evt_id;
+       bool was_enabled;
+       int ret;
+
+       if (!sse_event_is_global(evt))
+               return -EINVAL;
+
+       was_enabled = reg_evt->is_enabled;
+       if (was_enabled)
+               sse_event_disable_local(event);
+
+       ret = sse_event_attr_set_nolock(reg_evt, SBI_SSE_ATTR_PREFERRED_HART,
+                                       hart_id);
+       if (ret == 0)
+               sse_global_event_update_cpu(event, cpu);
+
+       if (was_enabled)
+               sse_event_enable_local(event);
+
+       return 0;
+}
+
+int sse_event_set_target_cpu(struct sse_event *event, unsigned int cpu)
+{
+       int ret;
+
+       scoped_guard(mutex, &sse_mutex) {
+               scoped_guard(cpus_read_lock) {
+                       if (!cpu_online(cpu))
+                               return -EINVAL;
+
+                       ret = sse_event_set_target_cpu_nolock(event, cpu);
+               }
+       }
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(sse_event_set_target_cpu);
+
+static int sse_event_init_registered(unsigned int cpu,
+                                    struct sse_registered_event *reg_evt,
+                                    struct sse_event *event)
+{
+       reg_evt->event = event;
+
+       return arch_sse_init_event(&reg_evt->arch, event->evt_id, cpu);
+}
+
+static void sse_event_free_registered(struct sse_registered_event *reg_evt)
+{
+       arch_sse_free_event(&reg_evt->arch);
+}
+
+static int sse_event_alloc_global(struct sse_event *event)
+{
+       int err;
+       struct sse_registered_event *reg_evt;
+
+       reg_evt = kzalloc(sizeof(*reg_evt), GFP_KERNEL);
+       if (!reg_evt)
+               return -ENOMEM;
+
+       event->global = reg_evt;
+       err = sse_event_init_registered(smp_processor_id(), reg_evt, event);
+       if (err)
+               kfree(reg_evt);
+
+       return err;
+}
+
+static int sse_event_alloc_local(struct sse_event *event)
+{
+       int err;
+       unsigned int cpu, err_cpu;
+       struct sse_registered_event *reg_evt;
+       struct sse_registered_event __percpu *reg_evts;
+
+       reg_evts = alloc_percpu(struct sse_registered_event);
+       if (!reg_evts)
+               return -ENOMEM;
+
+       event->local = reg_evts;
+
+       for_each_possible_cpu(cpu) {
+               reg_evt = per_cpu_ptr(reg_evts, cpu);
+               err = sse_event_init_registered(cpu, reg_evt, event);
+               if (err) {
+                       err_cpu = cpu;
+                       goto err_free_per_cpu;
+               }
+       }
+
+       return 0;
+
+err_free_per_cpu:
+       for_each_possible_cpu(cpu) {
+               if (cpu == err_cpu)
+                       break;
+               reg_evt = per_cpu_ptr(reg_evts, cpu);
+               sse_event_free_registered(reg_evt);
+       }
+
+       free_percpu(reg_evts);
+
+       return err;
+}
+
+static struct sse_event *sse_event_alloc(u32 evt, u32 priority,
+                                        sse_event_handler_fn *handler,
+                                        void *arg)
+{
+       int err;
+       struct sse_event *event;
+
+       event = kzalloc(sizeof(*event), GFP_KERNEL);
+       if (!event)
+               return ERR_PTR(-ENOMEM);
+
+       event->evt_id = evt;
+       event->priority = priority;
+       event->handler_arg = arg;
+       event->handler = handler;
+
+       if (sse_event_is_global(evt))
+               err = sse_event_alloc_global(event);
+       else
+               err = sse_event_alloc_local(event);
+
+       if (err) {
+               kfree(event);
+               return ERR_PTR(err);
+       }
+
+       return event;
+}
+
+static int sse_sbi_register_event(struct sse_event *event,
+                                 struct sse_registered_event *reg_evt)
+{
+       int ret;
+
+       ret = sse_event_attr_set_nolock(reg_evt, SBI_SSE_ATTR_PRIO,
+                                       event->priority);
+       if (ret)
+               return ret;
+
+       return arch_sse_register_event(&reg_evt->arch);
+}
+
+static int sse_event_register_local(struct sse_event *event)
+{
+       int ret;
+       struct sse_registered_event *reg_evt;
+
+       reg_evt = per_cpu_ptr(event->local, smp_processor_id());
+       ret = sse_sbi_register_event(event, reg_evt);
+       if (ret)
+               pr_debug("Failed to register event %x: err %d\n", event->evt_id,
+                        ret);
+
+       return ret;
+}
+
+static int sse_sbi_unregister_event(struct sse_event *event)
+{
+       return sse_sbi_event_func(event, SBI_SSE_EVENT_UNREGISTER);
+}
+
+struct sse_per_cpu_evt {
+       struct sse_event *event;
+       unsigned long func;
+       cpumask_t error;
+};
+
+static void sse_event_per_cpu_func(void *info)
+{
+       int ret;
+       struct sse_per_cpu_evt *cpu_evt = info;
+
+       if (cpu_evt->func == SBI_SSE_EVENT_REGISTER)
+               ret = sse_event_register_local(cpu_evt->event);
+       else
+               ret = sse_sbi_event_func(cpu_evt->event, cpu_evt->func);
+
+       if (ret)
+               cpumask_set_cpu(smp_processor_id(), &cpu_evt->error);
+}
+
+static void sse_event_free(struct sse_event *event)
+{
+       unsigned int cpu;
+       struct sse_registered_event *reg_evt;
+
+       if (sse_event_is_global(event->evt_id)) {
+               sse_event_free_registered(event->global);
+               kfree(event->global);
+       } else {
+               for_each_possible_cpu(cpu) {
+                       reg_evt = per_cpu_ptr(event->local, cpu);
+                       sse_event_free_registered(reg_evt);
+               }
+               free_percpu(event->local);
+       }
+
+       kfree(event);
+}
+
+static int sse_on_each_cpu(struct sse_event *event, unsigned long func,
+                          unsigned long revert_func)
+{
+       struct sse_per_cpu_evt cpu_evt;
+
+       cpu_evt.event = event;
+       cpumask_clear(&cpu_evt.error);
+       cpu_evt.func = func;
+       on_each_cpu(sse_event_per_cpu_func, &cpu_evt, 1);
+       /*
+        * If there are some error reported by CPUs, revert event state on the
+        * other ones
+        */
+       if (!cpumask_empty(&cpu_evt.error)) {
+               cpumask_t revert;
+
+               cpumask_andnot(&revert, cpu_online_mask, &cpu_evt.error);
+               cpu_evt.func = revert_func;
+               on_each_cpu_mask(&revert, sse_event_per_cpu_func, &cpu_evt, 1);
+
+               return -EIO;
+       }
+
+       return 0;
+}
+
+int sse_event_enable(struct sse_event *event)
+{
+       int ret = 0;
+
+       scoped_guard(mutex, &sse_mutex) {
+               scoped_guard(cpus_read_lock) {
+                       if (sse_event_is_global(event->evt_id)) {
+                               ret = sse_event_enable_local(event);
+                       } else {
+                               ret = sse_on_each_cpu(event,
+                                                     SBI_SSE_EVENT_ENABLE,
+                                                     SBI_SSE_EVENT_DISABLE);
+                       }
+               }
+       }
+       return ret;
+}
+EXPORT_SYMBOL_GPL(sse_event_enable);
+
+static int sse_events_mask(void)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_SSE, SBI_SSE_HART_MASK, 0, 0, 0, 0, 0, 0);
+
+       return sbi_err_map_linux_errno(ret.error);
+}
+
+static int sse_events_unmask(void)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_SSE, SBI_SSE_HART_UNMASK, 0, 0, 0, 0, 0, 0);
+
+       return sbi_err_map_linux_errno(ret.error);
+}
+
+static void sse_event_disable_nolock(struct sse_event *event)
+{
+       struct sse_per_cpu_evt cpu_evt;
+
+       if (sse_event_is_global(event->evt_id)) {
+               sse_event_disable_local(event);
+       } else {
+               cpu_evt.event = event;
+               cpu_evt.func = SBI_SSE_EVENT_DISABLE;
+               on_each_cpu(sse_event_per_cpu_func, &cpu_evt, 1);
+       }
+}
+
+void sse_event_disable(struct sse_event *event)
+{
+       scoped_guard(mutex, &sse_mutex) {
+               scoped_guard(cpus_read_lock) {
+                       sse_event_disable_nolock(event);
+               }
+       }
+}
+EXPORT_SYMBOL_GPL(sse_event_disable);
+
+struct sse_event *sse_event_register(u32 evt, u32 priority,
+                                    sse_event_handler_fn *handler, void *arg)
+{
+       struct sse_event *event;
+       int cpu;
+       int ret = 0;
+
+       if (!sse_available)
+               return ERR_PTR(-EOPNOTSUPP);
+
+       guard(mutex)(&sse_mutex);
+       if (sse_event_get(evt))
+               return ERR_PTR(-EEXIST);
+
+       event = sse_event_alloc(evt, priority, handler, arg);
+       if (IS_ERR(event))
+               return event;
+
+       scoped_guard(cpus_read_lock) {
+               if (sse_event_is_global(evt)) {
+                       unsigned long preferred_hart;
+
+                       ret = sse_event_attr_get_no_lock(event->global,
+                                                        SBI_SSE_ATTR_PREFERRED_HART,
+                                                        &preferred_hart);
+                       if (ret)
+                               goto err_event_free;
+
+                       cpu = riscv_hartid_to_cpuid(preferred_hart);
+                       sse_global_event_update_cpu(event, cpu);
+
+                       ret = sse_sbi_register_event(event, event->global);
+                       if (ret)
+                               goto err_event_free;
+
+               } else {
+                       ret = sse_on_each_cpu(event, SBI_SSE_EVENT_REGISTER,
+                                             SBI_SSE_EVENT_DISABLE);
+                       if (ret)
+                               goto err_event_free;
+               }
+       }
+
+       scoped_guard(spinlock, &events_list_lock)
+               list_add(&event->list, &events);
+
+       return event;
+
+err_event_free:
+       sse_event_free(event);
+
+       return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(sse_event_register);
+
+static void sse_event_unregister_nolock(struct sse_event *event)
+{
+       struct sse_per_cpu_evt cpu_evt;
+
+       if (sse_event_is_global(event->evt_id)) {
+               sse_sbi_unregister_event(event);
+       } else {
+               cpu_evt.event = event;
+               cpu_evt.func = SBI_SSE_EVENT_UNREGISTER;
+               on_each_cpu(sse_event_per_cpu_func, &cpu_evt, 1);
+       }
+}
+
+void sse_event_unregister(struct sse_event *event)
+{
+       scoped_guard(mutex, &sse_mutex) {
+               scoped_guard(cpus_read_lock)
+                       sse_event_unregister_nolock(event);
+
+               scoped_guard(spinlock, &events_list_lock)
+                       list_del(&event->list);
+
+               sse_event_free(event);
+       }
+}
+EXPORT_SYMBOL_GPL(sse_event_unregister);
+
+static int sse_cpu_online(unsigned int cpu)
+{
+       struct sse_event *event;
+
+       scoped_guard(spinlock, &events_list_lock) {
+               list_for_each_entry(event, &events, list) {
+                       if (sse_event_is_global(event->evt_id))
+                               continue;
+
+                       sse_event_register_local(event);
+                       if (sse_get_reg_evt(event))
+                               sse_event_enable_local(event);
+               }
+       }
+
+       /* Ready to handle events. Unmask SSE. */
+       return sse_events_unmask();
+}
+
+static int sse_cpu_teardown(unsigned int cpu)
+{
+       int ret = 0;
+       unsigned int next_cpu;
+       struct sse_event *event;
+       struct sse_registered_event *reg_evt;
+
+       /* Mask the sse events */
+       ret = sse_events_mask();
+       if (ret)
+               return ret;
+
+       scoped_guard(spinlock, &events_list_lock) {
+               list_for_each_entry(event, &events, list) {
+                       /* Disable local event on current cpu */
+                       if (!sse_event_is_global(event->evt_id)) {
+                               reg_evt = sse_get_reg_evt(event);
+                               if (reg_evt->is_enabled)
+                                       sse_event_disable_local(event);
+
+                               sse_sbi_unregister_event(event);
+                               continue;
+                       }
+
+                       if (event->cpu != smp_processor_id())
+                               continue;
+
+                       /* Update destination hart for global event */
+                       next_cpu = cpumask_any_but(cpu_online_mask, cpu);
+                       ret = sse_event_set_target_cpu_nolock(event, next_cpu);
+               }
+       }
+
+       return ret;
+}
+
+static int sse_pm_notifier(struct notifier_block *nb, unsigned long action,
+                          void *data)
+{
+       WARN_ON_ONCE(preemptible());
+
+       switch (action) {
+       case CPU_PM_ENTER:
+               sse_events_mask();
+               break;
+       case CPU_PM_EXIT:

drivers/firmware/riscv/riscv_sbi_sse.c line 634

Quoted 10 lines
drivers/firmware/riscv/riscv_sbi_sse.c new file +10 -0
+       case CPU_PM_ENTER_FAILED:
+               sse_events_unmask();
+               break;
+       default:
+               return NOTIFY_DONE;
+       }
+
+       return NOTIFY_OK;
+}
+

During a CPU hotplug up event,
there is a chance the core enters cpuidle
before the hotplug thread executes.
core unmasks SSE upon exiting cpuidle,
the hotplug up callback will find the SSE already unmasked
and trigger a rollback to hotplug down.
To resolve this, you can abort cpuidle entry if it detects that SSE is masked by returning notifier_from_errno(sse_events_mask())

Also, may I ask if you plan to send any patches from your side? It has been several months since the last one.

Best regards,
Jimmy Ho

Quoted 131 lines
drivers/firmware/riscv/riscv_sbi_sse.c new file +60 -0
+static struct notifier_block sse_pm_nb = {
+       .notifier_call = sse_pm_notifier,
+};
+
+/*
+ * Mask all CPUs and unregister all events on panic, reboot or kexec.
+ */
+static int sse_reboot_notifier(struct notifier_block *nb, unsigned long action,
+                              void *data)
+{
+       cpuhp_remove_state(sse_hp_state);
+
+       return NOTIFY_OK;
+}
+
+static struct notifier_block sse_reboot_nb = {
+       .notifier_call = sse_reboot_notifier,
+};
+
+static int __init sse_init(void)
+{
+       int ret;
+
+       if (sbi_probe_extension(SBI_EXT_SSE) <= 0) {
+               pr_info("Missing SBI SSE extension\n");
+               return -EOPNOTSUPP;
+       }
+       pr_info("SBI SSE extension detected\n");
+
+       ret = cpu_pm_register_notifier(&sse_pm_nb);
+       if (ret) {
+               pr_warn("Failed to register CPU PM notifier...\n");
+               return ret;
+       }
+
+       ret = register_reboot_notifier(&sse_reboot_nb);
+       if (ret) {
+               pr_warn("Failed to register reboot notifier...\n");
+               goto remove_cpupm;
+       }
+
+       ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/sse:online",
+                               sse_cpu_online, sse_cpu_teardown);
+       if (ret < 0)
+               goto remove_reboot;
+
+       sse_hp_state = ret;
+       sse_available = true;
+
+       return 0;
+
+remove_reboot:
+       unregister_reboot_notifier(&sse_reboot_nb);
+
+remove_cpupm:
+       cpu_pm_unregister_notifier(&sse_pm_nb);
+
+       return ret;
+}
+arch_initcall(sse_init);
include/linux/riscv_sbi_sse.h new file +57 -1
new file mode 100644
index 000000000000..84165cb5f2d0
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2025 Rivos Inc.
+ */
+
+#ifndef __LINUX_RISCV_SBI_SSE_H
+#define __LINUX_RISCV_SBI_SSE_H
+
+#include <linux/types.h>
+#include <linux/linkage.h>
+
+struct sse_event;
+struct pt_regs;
+
+typedef int (sse_event_handler_fn)(u32 event_num, void *arg,
+                                  struct pt_regs *regs);
+
+#ifdef CONFIG_RISCV_SBI_SSE
+
+struct sse_event *sse_event_register(u32 event_num, u32 priority,
+                                    sse_event_handler_fn *handler, void *arg);
+
+void sse_event_unregister(struct sse_event *evt);
+
+int sse_event_set_target_cpu(struct sse_event *sse_evt, unsigned int cpu);
+
+int sse_event_enable(struct sse_event *sse_evt);
+
+void sse_event_disable(struct sse_event *sse_evt);
+
+int sse_event_enable_local(struct sse_event *sse_evt);
+int sse_event_disable_local(struct sse_event *sse_evt);
+
+#else
+static inline struct sse_event *sse_event_register(u32 event_num, u32 priority,
+                                                  sse_event_handler_fn *handler,
+                                                  void *arg)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void sse_event_unregister(struct sse_event *evt) {}
+
+static inline int sse_event_set_target_cpu(struct sse_event *sse_evt,
+                                          unsigned int cpu)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int sse_event_enable(struct sse_event *sse_evt)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline void sse_event_disable(struct sse_event *sse_evt) {}
+#endif
+#endif /* __LINUX_RISCV_SBI_SSE_H */
--

2.43.0

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

#5 lore raw

PATCH v8 4/5 perf: RISC-V: add support for SSE event

In reply to Clément Léger

In order to use SSE within PMU drivers, register a SSE handler for the local PMU event. Reuse the existing overflow IRQ handler and pass appropriate pt_regs. Add a config option RISCV_PMU_SSE to select event delivery via SSE events.

---
 drivers/perf/Kconfig           | 10 +++++
 drivers/perf/riscv_pmu.c       | 23 +++++++++++
 drivers/perf/riscv_pmu_sbi.c   | 71 +++++++++++++++++++++++++++++-----
 include/linux/perf/riscv_pmu.h |  5 +++
 4 files changed, 99 insertions(+), 10 deletions(-)
drivers/perf/Kconfig +10 -0
index 638321fc9800..d6ffc2b036e5 100644
@@ -105,6 +105,16 @@ config RISCV_PMU_SBI
 	  full perf feature support i.e. counter overflow, privilege mode
 	  filtering, counter configuration.
 
+config RISCV_PMU_SBI_SSE
+	depends on RISCV_PMU && RISCV_SBI_SSE
+	bool "RISC-V PMU SSE events"
+	default n
+	help
+	  Say y if you want to use SSE events to deliver PMU interrupts. This
+	  provides a way to profile the kernel at any level by using NMI-like
+	  SSE events. SSE events being really intrusive, this option allows
+	  to select it only if needed.
+
 config STARFIVE_STARLINK_PMU
 	depends on ARCH_STARFIVE || COMPILE_TEST
 	depends on 64BIT
drivers/perf/riscv_pmu.c +23 -0
index 7644147d50b4..dda2814801c0 100644
@@ -13,6 +13,7 @@
 #include <linux/irqdesc.h>
 #include <linux/perf/riscv_pmu.h>
 #include <linux/printk.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/smp.h>
 #include <linux/sched_clock.h>
 
@@ -254,6 +255,24 @@ void riscv_pmu_start(struct perf_event *event, int flags)
 	perf_event_update_userpage(event);
 }
 
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static void riscv_pmu_disable(struct pmu *pmu)
+{
+	struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+	if (rvpmu->sse_evt)
+		sse_event_disable_local(rvpmu->sse_evt);
+}
+
+static void riscv_pmu_enable(struct pmu *pmu)
+{
+	struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+	if (rvpmu->sse_evt)
+		sse_event_enable_local(rvpmu->sse_evt);
+}
+#endif
+
 static int riscv_pmu_add(struct perf_event *event, int flags)
 {
 	struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
@@ -411,6 +430,10 @@ struct riscv_pmu *riscv_pmu_alloc(void)
 		.event_mapped	= riscv_pmu_event_mapped,
 		.event_unmapped	= riscv_pmu_event_unmapped,
 		.event_idx	= riscv_pmu_event_idx,
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+		.pmu_enable	= riscv_pmu_enable,
+		.pmu_disable	= riscv_pmu_disable,
+#endif
 		.add		= riscv_pmu_add,
 		.del		= riscv_pmu_del,
 		.start		= riscv_pmu_start,
drivers/perf/riscv_pmu_sbi.c +61 -10

132 lines not on this page · raw message

include/linux/perf/riscv_pmu.h +5 -0
index f82a28040594..08fdcf6baf4e 100644
@@ -28,6 +28,8 @@
 
 #define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1
 
+struct sse_event;
+
 struct cpu_hw_events {
 	/* currently enabled events */
 	int			n_events;
@@ -54,6 +56,9 @@ struct riscv_pmu {
 	char		*name;
 
 	irqreturn_t	(*handle_irq)(int irq_num, void *dev);
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+	struct sse_event *sse_evt;
+#endif
 
 	unsigned long	cmask;
 	u64		(*ctr_read)(struct perf_event *event);
Signature · 1 line
2.43.0
#6 lore raw

Re: [External] [PATCH v8 4/5] perf: RISC-V: add support for SSE event

Hi Clément,

Quoted 173 lines · Clément Léger

In order to use SSE within PMU drivers, register a SSE handler for the local PMU event. Reuse the existing overflow IRQ handler and pass appropriate pt_regs. Add a config option RISCV_PMU_SSE to select event delivery via SSE events.

---
 drivers/perf/Kconfig           | 10 +++++
 drivers/perf/riscv_pmu.c       | 23 +++++++++++
 drivers/perf/riscv_pmu_sbi.c   | 71 +++++++++++++++++++++++++++++-----
 include/linux/perf/riscv_pmu.h |  5 +++
 4 files changed, 99 insertions(+), 10 deletions(-)
drivers/perf/Kconfig +10 -0
index 638321fc9800..d6ffc2b036e5 100644
@@ -105,6 +105,16 @@ config RISCV_PMU_SBI
          full perf feature support i.e. counter overflow, privilege mode
          filtering, counter configuration.
 
+config RISCV_PMU_SBI_SSE
+       depends on RISCV_PMU && RISCV_SBI_SSE
+       bool "RISC-V PMU SSE events"
+       default n
+       help
+         Say y if you want to use SSE events to deliver PMU interrupts. This
+         provides a way to profile the kernel at any level by using NMI-like
+         SSE events. SSE events being really intrusive, this option allows
+         to select it only if needed.
+
 config STARFIVE_STARLINK_PMU
        depends on ARCH_STARFIVE || COMPILE_TEST
        depends on 64BIT
drivers/perf/riscv_pmu.c +23 -0
index 7644147d50b4..dda2814801c0 100644
@@ -13,6 +13,7 @@
 #include <linux/irqdesc.h>
 #include <linux/perf/riscv_pmu.h>
 #include <linux/printk.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/smp.h>
 #include <linux/sched_clock.h>
 
@@ -254,6 +255,24 @@ void riscv_pmu_start(struct perf_event *event, int flags)
        perf_event_update_userpage(event);
 }
 
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static void riscv_pmu_disable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_disable_local(rvpmu->sse_evt);
+}
+
+static void riscv_pmu_enable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_enable_local(rvpmu->sse_evt);
+}
+#endif
+
 static int riscv_pmu_add(struct perf_event *event, int flags)
 {
        struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
@@ -411,6 +430,10 @@ struct riscv_pmu *riscv_pmu_alloc(void)
                .event_mapped   = riscv_pmu_event_mapped,
                .event_unmapped = riscv_pmu_event_unmapped,
                .event_idx      = riscv_pmu_event_idx,
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+               .pmu_enable     = riscv_pmu_enable,
+               .pmu_disable    = riscv_pmu_disable,
+#endif
                .add            = riscv_pmu_add,
                .del            = riscv_pmu_del,
                .start          = riscv_pmu_start,
drivers/perf/riscv_pmu_sbi.c +25 -9
index e255c1b069ec..c852f64a5022 100644
@@ -17,6 +17,7 @@
 #include <linux/irqdomain.h>
 #include <linux/of_irq.h>
 #include <linux/of.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/cpu_pm.h>
 #include <linux/sched/clock.h>
 #include <linux/soc/andes/irq.h>
@@ -1038,10 +1039,10 @@ static void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
                pmu_sbi_start_ovf_ctrs_sbi(cpu_hw_evt, ctr_ovf_mask);
 }
 
-static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
+static irqreturn_t pmu_sbi_ovf_handler(struct cpu_hw_events *cpu_hw_evt,
+                                      struct pt_regs *regs, bool from_sse)
 {
        struct perf_sample_data data;
-       struct pt_regs *regs;
        struct hw_perf_event *hw_evt;
        union sbi_pmu_ctr_info *info;
        int lidx, hidx, fidx;
@@ -1049,7 +1050,6 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        struct perf_event *event;
        u64 overflow;
        u64 overflowed_ctrs = 0;
-       struct cpu_hw_events *cpu_hw_evt = dev;
        u64 start_clock = sched_clock();
        struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
 
@@ -1059,13 +1059,15 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        /* Firmware counter don't support overflow yet */
        fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
        if (fidx == RISCV_MAX_COUNTERS) {
-               csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
+               if (!from_sse)
+                       csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
                return IRQ_NONE;
        }
 
        event = cpu_hw_evt->events[fidx];
        if (!event) {
-               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+               if (!from_sse)
+                       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
                return IRQ_NONE;
        }
 
@@ -1080,16 +1082,16 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
 
        /*
         * Overflow interrupt pending bit should only be cleared after stopping
-        * all the counters to avoid any race condition.
+        * all the counters to avoid any race condition. When using SSE,
+        * interrupt is cleared when stopping counters.
         */
-       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+       if (!from_sse)
+               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
 
        /* No overflow bit is set */
        if (!overflow)
                return IRQ_NONE;
 
-       regs = get_irq_regs();
-
        for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
                struct perf_event *event = cpu_hw_evt->events[lidx];
 
@@ -1145,6 +1147,51 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        return IRQ_HANDLED;
 }
 
+static irqreturn_t pmu_sbi_ovf_irq_handler(int irq, void *dev)
+{
+       return pmu_sbi_ovf_handler(dev, get_irq_regs(), false);
+}
+
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static int pmu_sbi_ovf_sse_handler(u32 evt, void *arg, struct pt_regs *regs)
+{
+       struct cpu_hw_events __percpu *hw_events = arg;
+       struct cpu_hw_events *hw_event = raw_cpu_ptr(hw_events);
+
+       pmu_sbi_ovf_handler(hw_event, regs, true);
+
+       return 0;

drivers/perf/riscv_pmu_sbi.c static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev) · line 1173

Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10 -0
+}
+
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       int ret;
+       struct sse_event *evt;
+       struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
+
+       evt = sse_event_register(SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW, 0,
+                                pmu_sbi_ovf_sse_handler, hw_events);

As Radim suggested, we had better preset a priority for different events, such as these events and future ones:
SBI_SSE_EVENT_LOCAL_HIGH_PRIO_RAS SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP SBI_SSE_EVENT_GLOBAL_HIGH_PRIO_RAS SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW SBI_SSE_EVENT_LOCAL_LOW_PRIO_RAS SBI_SSE_EVENT_GLOBAL_LOW_PRIO_RAS SBI_SSE_EVENT_LOCAL_SOFTWARE_INJECTED SBI_SSE_EVENT_LOCAL_UNKNOWN_NMI SBI_SSE_EVENT_GLOBAL_SOFTWARE_INJECTED

Could you please help add a "global priority enum"?

Quoted 68 lines
drivers/perf/riscv_pmu_sbi.c +26 -1
+       if (IS_ERR(evt))
+               return PTR_ERR(evt);
+
+       ret = sse_event_enable(evt);
+       if (ret) {
+               sse_event_unregister(evt);
+               return ret;
+       }
+
+       pr_info("using SSE for PMU event delivery\n");
+       pmu->sse_evt = evt;
+
+       return ret;
+}
+#else
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       return -EOPNOTSUPP;
+}
+#endif
+
 static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
 {
        struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
@@ -1195,6 +1242,10 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
        struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
        struct irq_domain *domain = NULL;
 
+       ret = pmu_sbi_setup_sse(pmu);
+       if (!ret)
+               return 0;
+
        if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
                riscv_pmu_irq_num = RV_IRQ_PMU;
                riscv_pmu_use_irq = true;
@@ -1229,7 +1280,7 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
                return -ENODEV;
        }
 
-       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
+       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_irq_handler, "riscv-pmu", hw_events);
        if (ret) {
                pr_err("registering percpu irq failed [%d]\n", ret);
                return ret;
include/linux/perf/riscv_pmu.h +5 -1
index f82a28040594..08fdcf6baf4e 100644
@@ -28,6 +28,8 @@
 
 #define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1
 
+struct sse_event;
+
 struct cpu_hw_events {
        /* currently enabled events */
        int                     n_events;
@@ -54,6 +56,9 @@ struct riscv_pmu {
        char            *name;
 
        irqreturn_t     (*handle_irq)(int irq_num, void *dev);
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+       struct sse_event *sse_evt;
+#endif
 
        unsigned long   cmask;
        u64             (*ctr_read)(struct perf_event *event);
--
Quoted 2 lines

2.43.0

Thanks,
Yunhui

#7 lore raw

Re: [External] [PATCH v8 4/5] perf: RISC-V: add support for SSE event

Quoted 191 lines · yunhui cui

Hi Clément,

Quoted 173 lines · Clément Léger

In order to use SSE within PMU drivers, register a SSE handler for the local PMU event. Reuse the existing overflow IRQ handler and pass appropriate pt_regs. Add a config option RISCV_PMU_SSE to select event delivery via SSE events.

---
 drivers/perf/Kconfig           | 10 +++++
 drivers/perf/riscv_pmu.c       | 23 +++++++++++
 drivers/perf/riscv_pmu_sbi.c   | 71 +++++++++++++++++++++++++++++-----
 include/linux/perf/riscv_pmu.h |  5 +++
 4 files changed, 99 insertions(+), 10 deletions(-)
drivers/perf/Kconfig +10 -0
index 638321fc9800..d6ffc2b036e5 100644
@@ -105,6 +105,16 @@ config RISCV_PMU_SBI
          full perf feature support i.e. counter overflow, privilege mode
          filtering, counter configuration.
 
+config RISCV_PMU_SBI_SSE
+       depends on RISCV_PMU && RISCV_SBI_SSE
+       bool "RISC-V PMU SSE events"
+       default n
+       help
+         Say y if you want to use SSE events to deliver PMU interrupts. This
+         provides a way to profile the kernel at any level by using NMI-like
+         SSE events. SSE events being really intrusive, this option allows
+         to select it only if needed.
+
 config STARFIVE_STARLINK_PMU
        depends on ARCH_STARFIVE || COMPILE_TEST
        depends on 64BIT
drivers/perf/riscv_pmu.c +23 -0
index 7644147d50b4..dda2814801c0 100644
@@ -13,6 +13,7 @@
 #include <linux/irqdesc.h>
 #include <linux/perf/riscv_pmu.h>
 #include <linux/printk.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/smp.h>
 #include <linux/sched_clock.h>
 
@@ -254,6 +255,24 @@ void riscv_pmu_start(struct perf_event *event, int flags)
        perf_event_update_userpage(event);
 }
 
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static void riscv_pmu_disable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_disable_local(rvpmu->sse_evt);
+}
+
+static void riscv_pmu_enable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_enable_local(rvpmu->sse_evt);
+}
+#endif
+
 static int riscv_pmu_add(struct perf_event *event, int flags)
 {
        struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
@@ -411,6 +430,10 @@ struct riscv_pmu *riscv_pmu_alloc(void)
                .event_mapped   = riscv_pmu_event_mapped,
                .event_unmapped = riscv_pmu_event_unmapped,
                .event_idx      = riscv_pmu_event_idx,
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+               .pmu_enable     = riscv_pmu_enable,
+               .pmu_disable    = riscv_pmu_disable,
+#endif
                .add            = riscv_pmu_add,
                .del            = riscv_pmu_del,
                .start          = riscv_pmu_start,
drivers/perf/riscv_pmu_sbi.c +25 -9
index e255c1b069ec..c852f64a5022 100644
@@ -17,6 +17,7 @@
 #include <linux/irqdomain.h>
 #include <linux/of_irq.h>
 #include <linux/of.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/cpu_pm.h>
 #include <linux/sched/clock.h>
 #include <linux/soc/andes/irq.h>
@@ -1038,10 +1039,10 @@ static void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
                pmu_sbi_start_ovf_ctrs_sbi(cpu_hw_evt, ctr_ovf_mask);
 }
 
-static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
+static irqreturn_t pmu_sbi_ovf_handler(struct cpu_hw_events *cpu_hw_evt,
+                                      struct pt_regs *regs, bool from_sse)
 {
        struct perf_sample_data data;
-       struct pt_regs *regs;
        struct hw_perf_event *hw_evt;
        union sbi_pmu_ctr_info *info;
        int lidx, hidx, fidx;
@@ -1049,7 +1050,6 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        struct perf_event *event;
        u64 overflow;
        u64 overflowed_ctrs = 0;
-       struct cpu_hw_events *cpu_hw_evt = dev;
        u64 start_clock = sched_clock();
        struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
 
@@ -1059,13 +1059,15 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        /* Firmware counter don't support overflow yet */
        fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
        if (fidx == RISCV_MAX_COUNTERS) {
-               csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
+               if (!from_sse)
+                       csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
                return IRQ_NONE;
        }
 
        event = cpu_hw_evt->events[fidx];
        if (!event) {
-               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+               if (!from_sse)
+                       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
                return IRQ_NONE;
        }
 
@@ -1080,16 +1082,16 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
 
        /*
         * Overflow interrupt pending bit should only be cleared after stopping
-        * all the counters to avoid any race condition.
+        * all the counters to avoid any race condition. When using SSE,
+        * interrupt is cleared when stopping counters.
         */
-       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+       if (!from_sse)
+               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
 
        /* No overflow bit is set */
        if (!overflow)
                return IRQ_NONE;
 
-       regs = get_irq_regs();
-
        for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
                struct perf_event *event = cpu_hw_evt->events[lidx];
 
@@ -1145,6 +1147,51 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        return IRQ_HANDLED;
 }
 
+static irqreturn_t pmu_sbi_ovf_irq_handler(int irq, void *dev)
+{
+       return pmu_sbi_ovf_handler(dev, get_irq_regs(), false);
+}
+
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static int pmu_sbi_ovf_sse_handler(u32 evt, void *arg, struct pt_regs *regs)
+{
+       struct cpu_hw_events __percpu *hw_events = arg;
+       struct cpu_hw_events *hw_event = raw_cpu_ptr(hw_events);
+
+       pmu_sbi_ovf_handler(hw_event, regs, true);
+
+       return 0;

drivers/perf/riscv_pmu_sbi.c static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev) · line 1173

Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10 -0
+}
+
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       int ret;
+       struct sse_event *evt;
+       struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
+
+       evt = sse_event_register(SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW, 0,
+                                pmu_sbi_ovf_sse_handler, hw_events);

As Radim suggested, we had better preset a priority for different events, such as these events and future ones:
SBI_SSE_EVENT_LOCAL_HIGH_PRIO_RAS

Quoted 8 lines

SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP
SBI_SSE_EVENT_GLOBAL_HIGH_PRIO_RAS
SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW
SBI_SSE_EVENT_LOCAL_LOW_PRIO_RAS
SBI_SSE_EVENT_GLOBAL_LOW_PRIO_RAS
SBI_SSE_EVENT_LOCAL_SOFTWARE_INJECTED
SBI_SSE_EVENT_LOCAL_UNKNOWN_NMI
SBI_SSE_EVENT_GLOBAL_SOFTWARE_INJECTED

Hey Yunhui,

What do you mean exactly ? Events have default priority based on their ID. However, the register call will set a priority = 0 for the event. I'll modify that to allow using an optional priority (or even remove it)

Thanks,

Clément

Quoted 77 lines

Could you please help add a "global priority enum"?

Quoted 68 lines
drivers/perf/riscv_pmu_sbi.c +26 -1
+       if (IS_ERR(evt))
+               return PTR_ERR(evt);
+
+       ret = sse_event_enable(evt);
+       if (ret) {
+               sse_event_unregister(evt);
+               return ret;
+       }
+
+       pr_info("using SSE for PMU event delivery\n");
+       pmu->sse_evt = evt;
+
+       return ret;
+}
+#else
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       return -EOPNOTSUPP;
+}
+#endif
+
 static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
 {
        struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
@@ -1195,6 +1242,10 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
        struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
        struct irq_domain *domain = NULL;
 
+       ret = pmu_sbi_setup_sse(pmu);
+       if (!ret)
+               return 0;
+
        if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
                riscv_pmu_irq_num = RV_IRQ_PMU;
                riscv_pmu_use_irq = true;
@@ -1229,7 +1280,7 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
                return -ENODEV;
        }
 
-       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
+       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_irq_handler, "riscv-pmu", hw_events);
        if (ret) {
                pr_err("registering percpu irq failed [%d]\n", ret);
                return ret;
include/linux/perf/riscv_pmu.h +5 -1
index f82a28040594..08fdcf6baf4e 100644
@@ -28,6 +28,8 @@
 
 #define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1
 
+struct sse_event;
+
 struct cpu_hw_events {
        /* currently enabled events */
        int                     n_events;
@@ -54,6 +56,9 @@ struct riscv_pmu {
        char            *name;
 
        irqreturn_t     (*handle_irq)(int irq_num, void *dev);
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+       struct sse_event *sse_evt;
+#endif
 
        unsigned long   cmask;
        u64             (*ctr_read)(struct perf_event *event);
--
Quoted 2 lines

2.43.0

Thanks,
Yunhui

#8 lore raw

Re: [External] [PATCH v8 4/5] perf: RISC-V: add support for SSE event

Hi Clément,

Quoted 202 lines · Clément Léger
Quoted 191 lines · yunhui cui

Hi Clément,

Quoted 173 lines · Clément Léger

In order to use SSE within PMU drivers, register a SSE handler for the local PMU event. Reuse the existing overflow IRQ handler and pass appropriate pt_regs. Add a config option RISCV_PMU_SSE to select event delivery via SSE events.

---
 drivers/perf/Kconfig           | 10 +++++
 drivers/perf/riscv_pmu.c       | 23 +++++++++++
 drivers/perf/riscv_pmu_sbi.c   | 71 +++++++++++++++++++++++++++++-----
 include/linux/perf/riscv_pmu.h |  5 +++
 4 files changed, 99 insertions(+), 10 deletions(-)
drivers/perf/Kconfig +10 -0
index 638321fc9800..d6ffc2b036e5 100644
@@ -105,6 +105,16 @@ config RISCV_PMU_SBI
          full perf feature support i.e. counter overflow, privilege mode
          filtering, counter configuration.
 
+config RISCV_PMU_SBI_SSE
+       depends on RISCV_PMU && RISCV_SBI_SSE
+       bool "RISC-V PMU SSE events"
+       default n
+       help
+         Say y if you want to use SSE events to deliver PMU interrupts. This
+         provides a way to profile the kernel at any level by using NMI-like
+         SSE events. SSE events being really intrusive, this option allows
+         to select it only if needed.
+
 config STARFIVE_STARLINK_PMU
        depends on ARCH_STARFIVE || COMPILE_TEST
        depends on 64BIT
drivers/perf/riscv_pmu.c +23 -0
index 7644147d50b4..dda2814801c0 100644
@@ -13,6 +13,7 @@
 #include <linux/irqdesc.h>
 #include <linux/perf/riscv_pmu.h>
 #include <linux/printk.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/smp.h>
 #include <linux/sched_clock.h>
 
@@ -254,6 +255,24 @@ void riscv_pmu_start(struct perf_event *event, int flags)
        perf_event_update_userpage(event);
 }
 
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static void riscv_pmu_disable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_disable_local(rvpmu->sse_evt);
+}
+
+static void riscv_pmu_enable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_enable_local(rvpmu->sse_evt);
+}
+#endif
+
 static int riscv_pmu_add(struct perf_event *event, int flags)
 {
        struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
@@ -411,6 +430,10 @@ struct riscv_pmu *riscv_pmu_alloc(void)
                .event_mapped   = riscv_pmu_event_mapped,
                .event_unmapped = riscv_pmu_event_unmapped,
                .event_idx      = riscv_pmu_event_idx,
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+               .pmu_enable     = riscv_pmu_enable,
+               .pmu_disable    = riscv_pmu_disable,
+#endif
                .add            = riscv_pmu_add,
                .del            = riscv_pmu_del,
                .start          = riscv_pmu_start,
drivers/perf/riscv_pmu_sbi.c +25 -9
index e255c1b069ec..c852f64a5022 100644
@@ -17,6 +17,7 @@
 #include <linux/irqdomain.h>
 #include <linux/of_irq.h>
 #include <linux/of.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/cpu_pm.h>
 #include <linux/sched/clock.h>
 #include <linux/soc/andes/irq.h>
@@ -1038,10 +1039,10 @@ static void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
                pmu_sbi_start_ovf_ctrs_sbi(cpu_hw_evt, ctr_ovf_mask);
 }
 
-static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
+static irqreturn_t pmu_sbi_ovf_handler(struct cpu_hw_events *cpu_hw_evt,
+                                      struct pt_regs *regs, bool from_sse)
 {
        struct perf_sample_data data;
-       struct pt_regs *regs;
        struct hw_perf_event *hw_evt;
        union sbi_pmu_ctr_info *info;
        int lidx, hidx, fidx;
@@ -1049,7 +1050,6 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        struct perf_event *event;
        u64 overflow;
        u64 overflowed_ctrs = 0;
-       struct cpu_hw_events *cpu_hw_evt = dev;
        u64 start_clock = sched_clock();
        struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
 
@@ -1059,13 +1059,15 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        /* Firmware counter don't support overflow yet */
        fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
        if (fidx == RISCV_MAX_COUNTERS) {
-               csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
+               if (!from_sse)
+                       csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
                return IRQ_NONE;
        }
 
        event = cpu_hw_evt->events[fidx];
        if (!event) {
-               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+               if (!from_sse)
+                       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
                return IRQ_NONE;
        }
 
@@ -1080,16 +1082,16 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
 
        /*
         * Overflow interrupt pending bit should only be cleared after stopping
-        * all the counters to avoid any race condition.
+        * all the counters to avoid any race condition. When using SSE,
+        * interrupt is cleared when stopping counters.
         */
-       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+       if (!from_sse)
+               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
 
        /* No overflow bit is set */
        if (!overflow)
                return IRQ_NONE;
 
-       regs = get_irq_regs();
-
        for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
                struct perf_event *event = cpu_hw_evt->events[lidx];
 
@@ -1145,6 +1147,51 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        return IRQ_HANDLED;
 }
 
+static irqreturn_t pmu_sbi_ovf_irq_handler(int irq, void *dev)
+{
+       return pmu_sbi_ovf_handler(dev, get_irq_regs(), false);
+}
+
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static int pmu_sbi_ovf_sse_handler(u32 evt, void *arg, struct pt_regs *regs)
+{
+       struct cpu_hw_events __percpu *hw_events = arg;
+       struct cpu_hw_events *hw_event = raw_cpu_ptr(hw_events);
+
+       pmu_sbi_ovf_handler(hw_event, regs, true);
+
+       return 0;

drivers/perf/riscv_pmu_sbi.c static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev) · line 1173

Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10 -0
+}
+
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       int ret;
+       struct sse_event *evt;
+       struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
+
+       evt = sse_event_register(SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW, 0,
+                                pmu_sbi_ovf_sse_handler, hw_events);

As Radim suggested, we had better preset a priority for different events, such as these events and future ones:
SBI_SSE_EVENT_LOCAL_HIGH_PRIO_RAS

Quoted 8 lines

SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP
SBI_SSE_EVENT_GLOBAL_HIGH_PRIO_RAS
SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW
SBI_SSE_EVENT_LOCAL_LOW_PRIO_RAS
SBI_SSE_EVENT_GLOBAL_LOW_PRIO_RAS
SBI_SSE_EVENT_LOCAL_SOFTWARE_INJECTED
SBI_SSE_EVENT_LOCAL_UNKNOWN_NMI
SBI_SSE_EVENT_GLOBAL_SOFTWARE_INJECTED

Quoted 6 lines

Hey Yunhui,

What do you mean exactly ? Events have default priority based on their ID. However, the register call will set a priority = 0 for the event. I'll modify that to allow using an optional priority (or even remove it)

Actually, each event needs to be assigned a reasonable priority.
Reference: https://lore.kernel.org/all/CAEEQ3wmEVVHC=S795dFsQ9iM5Xkz-KOBe_CxXbvzyDBUo3wU6A@mail.gmail.com/

Quoted 82 lines

Thanks,

Clément

Quoted 77 lines

Could you please help add a "global priority enum"?

Quoted 68 lines
drivers/perf/riscv_pmu_sbi.c +26 -1
+       if (IS_ERR(evt))
+               return PTR_ERR(evt);
+
+       ret = sse_event_enable(evt);
+       if (ret) {
+               sse_event_unregister(evt);
+               return ret;
+       }
+
+       pr_info("using SSE for PMU event delivery\n");
+       pmu->sse_evt = evt;
+
+       return ret;
+}
+#else
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       return -EOPNOTSUPP;
+}
+#endif
+
 static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
 {
        struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
@@ -1195,6 +1242,10 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
        struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
        struct irq_domain *domain = NULL;
 
+       ret = pmu_sbi_setup_sse(pmu);
+       if (!ret)
+               return 0;
+
        if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
                riscv_pmu_irq_num = RV_IRQ_PMU;
                riscv_pmu_use_irq = true;
@@ -1229,7 +1280,7 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
                return -ENODEV;
        }
 
-       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
+       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_irq_handler, "riscv-pmu", hw_events);
        if (ret) {
                pr_err("registering percpu irq failed [%d]\n", ret);
                return ret;
include/linux/perf/riscv_pmu.h +5 -1
index f82a28040594..08fdcf6baf4e 100644
@@ -28,6 +28,8 @@
 
 #define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1
 
+struct sse_event;
+
 struct cpu_hw_events {
        /* currently enabled events */
        int                     n_events;
@@ -54,6 +56,9 @@ struct riscv_pmu {
        char            *name;
 
        irqreturn_t     (*handle_irq)(int irq_num, void *dev);
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+       struct sse_event *sse_evt;
+#endif
 
        unsigned long   cmask;
        u64             (*ctr_read)(struct perf_event *event);
--
Quoted 2 lines

2.43.0

Thanks,
Yunhui

Thanks,
Yunhui

#9 lore raw

Re: [External] [PATCH v8 4/5] perf: RISC-V: add support for SSE event

Hi Yunhui,

The specification states that each event ID act as the default priority so there is no need to actually set another priority:
https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-sse.adoc#software-event-priority

As I said, currently, the SSE infra is requiring a priority to be set at registration time and most of the code passes 0 (which is wrong). I'll rework that for my next version.

Thanks,

Clément
Bu maybe I missed something

Quoted 301 lines · yunhui cui

Hi Clément,

Quoted 202 lines · Clément Léger
Quoted 191 lines · yunhui cui

Hi Clément,

Quoted 173 lines · Clément Léger

In order to use SSE within PMU drivers, register a SSE handler for the local PMU event. Reuse the existing overflow IRQ handler and pass appropriate pt_regs. Add a config option RISCV_PMU_SSE to select event delivery via SSE events.

---
 drivers/perf/Kconfig           | 10 +++++
 drivers/perf/riscv_pmu.c       | 23 +++++++++++
 drivers/perf/riscv_pmu_sbi.c   | 71 +++++++++++++++++++++++++++++-----
 include/linux/perf/riscv_pmu.h |  5 +++
 4 files changed, 99 insertions(+), 10 deletions(-)
drivers/perf/Kconfig +10 -0
index 638321fc9800..d6ffc2b036e5 100644
@@ -105,6 +105,16 @@ config RISCV_PMU_SBI
          full perf feature support i.e. counter overflow, privilege mode
          filtering, counter configuration.
 
+config RISCV_PMU_SBI_SSE
+       depends on RISCV_PMU && RISCV_SBI_SSE
+       bool "RISC-V PMU SSE events"
+       default n
+       help
+         Say y if you want to use SSE events to deliver PMU interrupts. This
+         provides a way to profile the kernel at any level by using NMI-like
+         SSE events. SSE events being really intrusive, this option allows
+         to select it only if needed.
+
 config STARFIVE_STARLINK_PMU
        depends on ARCH_STARFIVE || COMPILE_TEST
        depends on 64BIT
drivers/perf/riscv_pmu.c +23 -0
index 7644147d50b4..dda2814801c0 100644
@@ -13,6 +13,7 @@
 #include <linux/irqdesc.h>
 #include <linux/perf/riscv_pmu.h>
 #include <linux/printk.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/smp.h>
 #include <linux/sched_clock.h>
 
@@ -254,6 +255,24 @@ void riscv_pmu_start(struct perf_event *event, int flags)
        perf_event_update_userpage(event);
 }
 
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static void riscv_pmu_disable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_disable_local(rvpmu->sse_evt);
+}
+
+static void riscv_pmu_enable(struct pmu *pmu)
+{
+       struct riscv_pmu *rvpmu = to_riscv_pmu(pmu);
+
+       if (rvpmu->sse_evt)
+               sse_event_enable_local(rvpmu->sse_evt);
+}
+#endif
+
 static int riscv_pmu_add(struct perf_event *event, int flags)
 {
        struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
@@ -411,6 +430,10 @@ struct riscv_pmu *riscv_pmu_alloc(void)
                .event_mapped   = riscv_pmu_event_mapped,
                .event_unmapped = riscv_pmu_event_unmapped,
                .event_idx      = riscv_pmu_event_idx,
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+               .pmu_enable     = riscv_pmu_enable,
+               .pmu_disable    = riscv_pmu_disable,
+#endif
                .add            = riscv_pmu_add,
                .del            = riscv_pmu_del,
                .start          = riscv_pmu_start,
drivers/perf/riscv_pmu_sbi.c +25 -9
index e255c1b069ec..c852f64a5022 100644
@@ -17,6 +17,7 @@
 #include <linux/irqdomain.h>
 #include <linux/of_irq.h>
 #include <linux/of.h>
+#include <linux/riscv_sbi_sse.h>
 #include <linux/cpu_pm.h>
 #include <linux/sched/clock.h>
 #include <linux/soc/andes/irq.h>
@@ -1038,10 +1039,10 @@ static void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
                pmu_sbi_start_ovf_ctrs_sbi(cpu_hw_evt, ctr_ovf_mask);
 }
 
-static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
+static irqreturn_t pmu_sbi_ovf_handler(struct cpu_hw_events *cpu_hw_evt,
+                                      struct pt_regs *regs, bool from_sse)
 {
        struct perf_sample_data data;
-       struct pt_regs *regs;
        struct hw_perf_event *hw_evt;
        union sbi_pmu_ctr_info *info;
        int lidx, hidx, fidx;
@@ -1049,7 +1050,6 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        struct perf_event *event;
        u64 overflow;
        u64 overflowed_ctrs = 0;
-       struct cpu_hw_events *cpu_hw_evt = dev;
        u64 start_clock = sched_clock();
        struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
 
@@ -1059,13 +1059,15 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        /* Firmware counter don't support overflow yet */
        fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
        if (fidx == RISCV_MAX_COUNTERS) {
-               csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
+               if (!from_sse)
+                       csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
                return IRQ_NONE;
        }
 
        event = cpu_hw_evt->events[fidx];
        if (!event) {
-               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+               if (!from_sse)
+                       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
                return IRQ_NONE;
        }
 
@@ -1080,16 +1082,16 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
 
        /*
         * Overflow interrupt pending bit should only be cleared after stopping
-        * all the counters to avoid any race condition.
+        * all the counters to avoid any race condition. When using SSE,
+        * interrupt is cleared when stopping counters.
         */
-       ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
+       if (!from_sse)
+               ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
 
        /* No overflow bit is set */
        if (!overflow)
                return IRQ_NONE;
 
-       regs = get_irq_regs();
-
        for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
                struct perf_event *event = cpu_hw_evt->events[lidx];
 
@@ -1145,6 +1147,51 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
        return IRQ_HANDLED;
 }
 
+static irqreturn_t pmu_sbi_ovf_irq_handler(int irq, void *dev)
+{
+       return pmu_sbi_ovf_handler(dev, get_irq_regs(), false);
+}
+
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+static int pmu_sbi_ovf_sse_handler(u32 evt, void *arg, struct pt_regs *regs)
+{
+       struct cpu_hw_events __percpu *hw_events = arg;
+       struct cpu_hw_events *hw_event = raw_cpu_ptr(hw_events);
+
+       pmu_sbi_ovf_handler(hw_event, regs, true);
+
+       return 0;

drivers/perf/riscv_pmu_sbi.c static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev) · line 1173

Quoted 10 lines
drivers/perf/riscv_pmu_sbi.c +10 -0
+}
+
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       int ret;
+       struct sse_event *evt;
+       struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
+
+       evt = sse_event_register(SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW, 0,
+                                pmu_sbi_ovf_sse_handler, hw_events);

As Radim suggested, we had better preset a priority for different events, such as these events and future ones:
SBI_SSE_EVENT_LOCAL_HIGH_PRIO_RAS

Quoted 8 lines

SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP
SBI_SSE_EVENT_GLOBAL_HIGH_PRIO_RAS
SBI_SSE_EVENT_LOCAL_PMU_OVERFLOW
SBI_SSE_EVENT_LOCAL_LOW_PRIO_RAS
SBI_SSE_EVENT_GLOBAL_LOW_PRIO_RAS
SBI_SSE_EVENT_LOCAL_SOFTWARE_INJECTED
SBI_SSE_EVENT_LOCAL_UNKNOWN_NMI
SBI_SSE_EVENT_GLOBAL_SOFTWARE_INJECTED

Quoted 6 lines

Hey Yunhui,

What do you mean exactly ? Events have default priority based on their ID. However, the register call will set a priority = 0 for the event. I'll modify that to allow using an optional priority (or even remove it)

Actually, each event needs to be assigned a reasonable priority.
Reference: https://lore.kernel.org/all/CAEEQ3wmEVVHC=S795dFsQ9iM5Xkz-KOBe_CxXbvzyDBUo3wU6A@mail.gmail.com/

Quoted 82 lines

Thanks,

Clément

Quoted 77 lines

Could you please help add a "global priority enum"?

Quoted 68 lines
drivers/perf/riscv_pmu_sbi.c +26 -1
+       if (IS_ERR(evt))
+               return PTR_ERR(evt);
+
+       ret = sse_event_enable(evt);
+       if (ret) {
+               sse_event_unregister(evt);
+               return ret;
+       }
+
+       pr_info("using SSE for PMU event delivery\n");
+       pmu->sse_evt = evt;
+
+       return ret;
+}
+#else
+static int pmu_sbi_setup_sse(struct riscv_pmu *pmu)
+{
+       return -EOPNOTSUPP;
+}
+#endif
+
 static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
 {
        struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
@@ -1195,6 +1242,10 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
        struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
        struct irq_domain *domain = NULL;
 
+       ret = pmu_sbi_setup_sse(pmu);
+       if (!ret)
+               return 0;
+
        if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
                riscv_pmu_irq_num = RV_IRQ_PMU;
                riscv_pmu_use_irq = true;
@@ -1229,7 +1280,7 @@ static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pde
                return -ENODEV;
        }
 
-       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
+       ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_irq_handler, "riscv-pmu", hw_events);
        if (ret) {
                pr_err("registering percpu irq failed [%d]\n", ret);
                return ret;
include/linux/perf/riscv_pmu.h +5 -1
index f82a28040594..08fdcf6baf4e 100644
@@ -28,6 +28,8 @@
 
 #define RISCV_PMU_CONFIG1_GUEST_EVENTS 0x1
 
+struct sse_event;
+
 struct cpu_hw_events {
        /* currently enabled events */
        int                     n_events;
@@ -54,6 +56,9 @@ struct riscv_pmu {
        char            *name;
 
        irqreturn_t     (*handle_irq)(int irq_num, void *dev);
+#ifdef CONFIG_RISCV_PMU_SBI_SSE
+       struct sse_event *sse_evt;
+#endif
 
        unsigned long   cmask;
        u64             (*ctr_read)(struct perf_event *event);
--
Quoted 2 lines

2.43.0

Thanks,
Yunhui

Thanks,
Yunhui

#10 lore raw

PATCH v8 5/5 selftests/riscv: add SSE test module

In reply to Clément Léger

This module, once loaded, will execute a series of tests using the SSE framework. The provided script will check for any error reported by the test module.

---
 tools/testing/selftests/riscv/Makefile        |   2 +-
 tools/testing/selftests/riscv/sse/Makefile    |   5 +
 .../selftests/riscv/sse/module/Makefile       |  16 +
 .../riscv/sse/module/riscv_sse_test.c         | 511 ++++++++++++++++++
 .../selftests/riscv/sse/run_sse_test.sh       |  44 ++
 5 files changed, 577 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/riscv/sse/Makefile
 create mode 100644 tools/testing/selftests/riscv/sse/module/Makefile
 create mode 100644 tools/testing/selftests/riscv/sse/module/riscv_sse_test.c
 create mode 100644 tools/testing/selftests/riscv/sse/run_sse_test.sh
tools/testing/selftests/riscv/Makefile +1 -1
index 099b8c1f46f8..c62f58414b29 100644
@@ -5,7 +5,7 @@
 ARCH ?= $(shell uname -m 2>/dev/null || echo not)
 
 ifneq (,$(filter $(ARCH),riscv))
-RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector
+RISCV_SUBTARGETS ?= abi hwprobe mm sigreturn vector sse
 else
 RISCV_SUBTARGETS :=
 endif
tools/testing/selftests/riscv/sse/Makefile new file +5 -0
new file mode 100644
index 000000000000..67eaee06f213
@@ -0,0 +1,5 @@
+TEST_GEN_MODS_DIR := module
+
+TEST_FILES := run_sse_test.sh
+
+include ../../lib.mk
tools/testing/selftests/riscv/sse/module/Makefile new file +16 -0
new file mode 100644
index 000000000000..02018f083456
@@ -0,0 +1,16 @@
+TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+KDIR ?= /lib/modules/$(shell uname -r)/build
+
+obj-m += riscv_sse_test.o
+
+# Ensure that KDIR exists, otherwise skip the compilation
+modules:
+ifneq ("$(wildcard $(KDIR))", "")
+	$(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
+endif
+
+# Ensure that KDIR exists, otherwise skip the clean target
+clean:
+ifneq ("$(wildcard $(KDIR))", "")
+	$(Q)$(MAKE) -C $(KDIR) clean KBUILD_EXTMOD=$(TESTMODS_DIR)
+endif
tools/testing/selftests/riscv/sse/module/riscv_sse_test.c new file +511 -0

511 lines not on this page · raw message

tools/testing/selftests/riscv/sse/run_sse_test.sh new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw
In reply to Clément Léger
This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Quoted 5 lines · On Wed, 5 Nov 2025 08:26:32 +0000 you wrote:

The SBI Supervisor Software Events (SSE) extensions provides a mechanism to inject software events from an SBI implementation to supervisor software such that it preempts all other supervisor level traps and interrupts. This extension is introduced by the SBI v3.0 specification[1].

Quoted 7 lines

Various events are defined and can be send asynchronously to supervisor software (RAS, PMU, DEBUG, Asynchronous page fault) from SBI as well as platform specific events. Events can be either local (per-hart) or global. Events can be nested on top of each other based on priority and can interrupt the kernel at any time.

[...]

Here is the summary with links:
- [v8,1/5] riscv: add SBI SSE extension definitions

    https://git.kernel.org/riscv/c/7bba38249b8a
  - [v8,2/5] riscv: add support for SBI Supervisor Software Events extension
    https://git.kernel.org/riscv/c/b52179e6de7d
  - [v8,3/5] drivers: firmware: add riscv SSE support
    https://git.kernel.org/riscv/c/5ffe60d26107
  - [v8,4/5] perf: RISC-V: add support for SSE event
    https://git.kernel.org/riscv/c/c6f3f04d2a9c
  - [v8,5/5] selftests/riscv: add SSE test module
    https://git.kernel.org/riscv/c/a123316660af

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
maintainer
#12 lore raw

To add more context:

Quoted 19 lines · On Wed, 19 Nov 2025, [email protected] wrote:

This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Quoted 5 lines · On Wed, 5 Nov 2025 08:26:32 +0000 you wrote:

The SBI Supervisor Software Events (SSE) extensions provides a mechanism to inject software events from an SBI implementation to supervisor software such that it preempts all other supervisor level traps and interrupts. This extension is introduced by the SBI v3.0 specification[1].

Quoted 7 lines

Various events are defined and can be send asynchronously to supervisor software (RAS, PMU, DEBUG, Asynchronous page fault) from SBI as well as platform specific events. Events can be either local (per-hart) or global. Events can be nested on top of each other based on priority and can interrupt the kernel at any time.

[...]

Here is the summary with links:
- [v8,1/5] riscv: add SBI SSE extension definitions

Quoted 9 lines
    https://git.kernel.org/riscv/c/7bba38249b8a
  - [v8,2/5] riscv: add support for SBI Supervisor Software Events extension
    https://git.kernel.org/riscv/c/b52179e6de7d
  - [v8,3/5] drivers: firmware: add riscv SSE support
    https://git.kernel.org/riscv/c/5ffe60d26107
  - [v8,4/5] perf: RISC-V: add support for SSE event
    https://git.kernel.org/riscv/c/c6f3f04d2a9c
  - [v8,5/5] selftests/riscv: add SSE test module
    https://git.kernel.org/riscv/c/a123316660af

This is a pretty sophisticated series. I was hoping to get more test reports on it and reviews (as Clément requested as well). In the meantime I've pulled them into for-next in the hopes that it might increase the number of potential testers. Am still very much interested in any test reports or additional reviews; if they come in this week, I'll consider rebuilding for-next to add them.

- Paul

#13 lore raw
Quoted 34 lines · Paul Walmsley

To add more context:

Quoted 19 lines · On Wed, 19 Nov 2025, [email protected] wrote:

This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Quoted 5 lines · On Wed, 5 Nov 2025 08:26:32 +0000 you wrote:

The SBI Supervisor Software Events (SSE) extensions provides a mechanism to inject software events from an SBI implementation to supervisor software such that it preempts all other supervisor level traps and interrupts. This extension is introduced by the SBI v3.0 specification[1].

Quoted 7 lines

Various events are defined and can be send asynchronously to supervisor software (RAS, PMU, DEBUG, Asynchronous page fault) from SBI as well as platform specific events. Events can be either local (per-hart) or global. Events can be nested on top of each other based on priority and can interrupt the kernel at any time.

[...]

Here is the summary with links:
- [v8,1/5] riscv: add SBI SSE extension definitions

Quoted 9 lines
    https://git.kernel.org/riscv/c/7bba38249b8a
  - [v8,2/5] riscv: add support for SBI Supervisor Software Events extension
    https://git.kernel.org/riscv/c/b52179e6de7d
  - [v8,3/5] drivers: firmware: add riscv SSE support
    https://git.kernel.org/riscv/c/5ffe60d26107
  - [v8,4/5] perf: RISC-V: add support for SSE event
    https://git.kernel.org/riscv/c/c6f3f04d2a9c
  - [v8,5/5] selftests/riscv: add SSE test module
    https://git.kernel.org/riscv/c/a123316660af
Quoted 6 lines

This is a pretty sophisticated series. I was hoping to get more test reports on it and reviews (as Clément requested as well). In the meantime I've pulled them into for-next in the hopes that it might increase the number of potential testers. Am still very much interested in any test reports or additional reviews; if they come in this week, I'll consider rebuilding for-next to add them.

Hey Paul,

Since we gathered additional bug/starvation reports with SSE from Zhanpeng, it would be preferable to remove this series from your branch if still possible. I'm not currently really available (just had a newborn) so I won't be able to fix this quickly.

Thanks,

Clément

Quoted 3 lines

- Paul

maintainer
#14 lore raw

Hi Clément,

Quoted 42 lines · Clément Léger
Quoted 34 lines · Paul Walmsley

To add more context:

Quoted 19 lines · On Wed, 19 Nov 2025, [email protected] wrote:

This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Quoted 5 lines · On Wed, 5 Nov 2025 08:26:32 +0000 you wrote:

The SBI Supervisor Software Events (SSE) extensions provides a mechanism to inject software events from an SBI implementation to supervisor software such that it preempts all other supervisor level traps and interrupts. This extension is introduced by the SBI v3.0 specification[1].

Quoted 7 lines

Various events are defined and can be send asynchronously to supervisor software (RAS, PMU, DEBUG, Asynchronous page fault) from SBI as well as platform specific events. Events can be either local (per-hart) or global. Events can be nested on top of each other based on priority and can interrupt the kernel at any time.

[...]

Here is the summary with links:
- [v8,1/5] riscv: add SBI SSE extension definitions

Quoted 9 lines
    https://git.kernel.org/riscv/c/7bba38249b8a
  - [v8,2/5] riscv: add support for SBI Supervisor Software Events extension
    https://git.kernel.org/riscv/c/b52179e6de7d
  - [v8,3/5] drivers: firmware: add riscv SSE support
    https://git.kernel.org/riscv/c/5ffe60d26107
  - [v8,4/5] perf: RISC-V: add support for SSE event
    https://git.kernel.org/riscv/c/c6f3f04d2a9c
  - [v8,5/5] selftests/riscv: add SSE test module
    https://git.kernel.org/riscv/c/a123316660af
Quoted 6 lines

This is a pretty sophisticated series. I was hoping to get more test reports on it and reviews (as Clément requested as well). In the meantime I've pulled them into for-next in the hopes that it might increase the number of potential testers. Am still very much interested in any test reports or additional reviews; if they come in this week, I'll consider rebuilding for-next to add them.

Quoted 4 lines

Since we gathered additional bug/starvation reports with SSE from Zhanpeng, it would be preferable to remove this series from your branch if still possible. I'm not currently really available (just had a newborn) so I won't be able to fix this quickly.

No problem, we'll drop it.

Congratulations on the new addition to your family,

- Paul