Skip to contentSkip to conversation

lib: sbi: pmu: Rework SSE callbacks

PATCH Applied Clément Léger 4 participants 10 messages May 19, 2025 – Jun 15, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover lib: sbi: pmu: Rework SSE callbacks

While testing the PMU SSE support in Linux, a few problem were spotted. The first one was due to interrupts happing during some PMU critical sections. Indeed, the PMU core actually disable interrupts when entering these sections. However, while this works when interrupts are targeting the S-mode, this is not the case with SSE. Fixing this required to implement pmu_disable/enable() in Linux [1]. These two callbacks now call sse_event_disable/enable() which now just needs to set/clear MIE/MIP. Second problem was that reported PMU events count were quite low. This proved to be due to some interrupts being losed when reenabling SSE event. We still need to clear the pending PMU interrupts upon disabling otherwise we might get some spurious interrupts when reenabling it.

Clément Léger (2):
lib: sbi: pmu: Add SSE register/unregister() callbacks
lib: sbi: pmu: Remove MIP clearing from pmu_sse_enable()

 lib/sbi/sbi_pmu.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
Signature · 7 lines
2.49.0


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

PATCH v2 1/2 lib: sbi: pmu: Add SSE register/unregister() callbacks

As soon as the SSE event is registered, there is no reason not to delegate the interrupt. Split the PMU SSE enable/disable() callbacks by moving MIDELEG setting to register/unregister().

---
 lib/sbi/sbi_pmu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
lib/sbi/sbi_pmu.c +18 -6
index 5983a784..dee50b9b 100644
@@ -1097,24 +1097,18 @@ void sbi_pmu_exit(struct sbi_scratch *scratch)
 
 static void pmu_sse_enable(uint32_t event_id)
 {
-	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
 	unsigned long irq_mask = sbi_pmu_irq_mask();
 
-	phs->sse_enabled = true;
-	csr_clear(CSR_MIDELEG, irq_mask);
 	csr_clear(CSR_MIP, irq_mask);
 	csr_set(CSR_MIE, irq_mask);
 }
 
 static void pmu_sse_disable(uint32_t event_id)
 {
-	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
 	unsigned long irq_mask = sbi_pmu_irq_mask();
 
 	csr_clear(CSR_MIE, irq_mask);
 	csr_clear(CSR_MIP, irq_mask);
-	csr_set(CSR_MIDELEG, irq_mask);
-	phs->sse_enabled = false;
 }
 
 static void pmu_sse_complete(uint32_t event_id)
@@ -1122,7 +1116,25 @@ static void pmu_sse_complete(uint32_t event_id)
 	csr_set(CSR_MIE, sbi_pmu_irq_mask());
 }
 
+static void pmu_sse_register(uint32_t event_id)
+{
+	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
+
+	phs->sse_enabled = true;
+	csr_clear(CSR_MIDELEG, sbi_pmu_irq_mask());
+}
+
+static void pmu_sse_unregister(uint32_t event_id)
+{
+	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
+
+	phs->sse_enabled = false;
+	csr_set(CSR_MIDELEG, sbi_pmu_irq_mask());
+}
+
 static const struct sbi_sse_cb_ops pmu_sse_cb_ops = {
+	.register_cb = pmu_sse_register,
+	.unregister_cb = pmu_sse_unregister,
 	.enable_cb = pmu_sse_enable,
 	.disable_cb = pmu_sse_disable,
 	.complete_cb = pmu_sse_complete,
Signature · 7 lines
2.49.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] lib: sbi: pmu: Add SSE register/unregister() callbacks

Quoted 66 lines · Clément Léger

As soon as the SSE event is registered, there is no reason not to delegate the interrupt. Split the PMU SSE enable/disable() callbacks by moving MIDELEG setting to register/unregister().

---
 lib/sbi/sbi_pmu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
lib/sbi/sbi_pmu.c +18 -7
index 5983a784..dee50b9b 100644
@@ -1097,24 +1097,18 @@ void sbi_pmu_exit(struct sbi_scratch *scratch)
 
 static void pmu_sse_enable(uint32_t event_id)
 {
-       struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
        unsigned long irq_mask = sbi_pmu_irq_mask();
 
-       phs->sse_enabled = true;
-       csr_clear(CSR_MIDELEG, irq_mask);
        csr_clear(CSR_MIP, irq_mask);
        csr_set(CSR_MIE, irq_mask);
 }
 
 static void pmu_sse_disable(uint32_t event_id)
 {
-       struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
        unsigned long irq_mask = sbi_pmu_irq_mask();
 
        csr_clear(CSR_MIE, irq_mask);
        csr_clear(CSR_MIP, irq_mask);
-       csr_set(CSR_MIDELEG, irq_mask);
-       phs->sse_enabled = false;
 }
 
 static void pmu_sse_complete(uint32_t event_id)
@@ -1122,7 +1116,25 @@ static void pmu_sse_complete(uint32_t event_id)
        csr_set(CSR_MIE, sbi_pmu_irq_mask());
 }
 
+static void pmu_sse_register(uint32_t event_id)
+{
+       struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
+
+       phs->sse_enabled = true;
+       csr_clear(CSR_MIDELEG, sbi_pmu_irq_mask());
+}
+
+static void pmu_sse_unregister(uint32_t event_id)
+{
+       struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
+
+       phs->sse_enabled = false;
+       csr_set(CSR_MIDELEG, sbi_pmu_irq_mask());
+}
+
 static const struct sbi_sse_cb_ops pmu_sse_cb_ops = {
+       .register_cb = pmu_sse_register,
+       .unregister_cb = pmu_sse_unregister,
        .enable_cb = pmu_sse_enable,
        .disable_cb = pmu_sse_disable,
        .complete_cb = pmu_sse_complete,
--
Quoted 2 lines

2.49.0

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] lib: sbi: pmu: Add SSE register/unregister() callbacks

In reply to Clément Léger
Quoted 1 line · Clément Léger

As soon as the SSE event is registered, there is no reason not to

typo: remove "not". Otherwise,

Quoted 63 lines

delegate the interrupt. Split the PMU SSE enable/disable() callbacks by moving MIDELEG setting to register/unregister().

---
 lib/sbi/sbi_pmu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
lib/sbi/sbi_pmu.c +18 -6
index 5983a784..dee50b9b 100644
@@ -1097,24 +1097,18 @@ void sbi_pmu_exit(struct sbi_scratch *scratch)
 
 static void pmu_sse_enable(uint32_t event_id)
 {
-	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
 	unsigned long irq_mask = sbi_pmu_irq_mask();
 
-	phs->sse_enabled = true;
-	csr_clear(CSR_MIDELEG, irq_mask);
 	csr_clear(CSR_MIP, irq_mask);
 	csr_set(CSR_MIE, irq_mask);
 }
 
 static void pmu_sse_disable(uint32_t event_id)
 {
-	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
 	unsigned long irq_mask = sbi_pmu_irq_mask();
 
 	csr_clear(CSR_MIE, irq_mask);
 	csr_clear(CSR_MIP, irq_mask);
-	csr_set(CSR_MIDELEG, irq_mask);
-	phs->sse_enabled = false;
 }
 
 static void pmu_sse_complete(uint32_t event_id)
@@ -1122,7 +1116,25 @@ static void pmu_sse_complete(uint32_t event_id)
 	csr_set(CSR_MIE, sbi_pmu_irq_mask());
 }
 
+static void pmu_sse_register(uint32_t event_id)
+{
+	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
+
+	phs->sse_enabled = true;
+	csr_clear(CSR_MIDELEG, sbi_pmu_irq_mask());
+}
+
+static void pmu_sse_unregister(uint32_t event_id)
+{
+	struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
+
+	phs->sse_enabled = false;
+	csr_set(CSR_MIDELEG, sbi_pmu_irq_mask());
+}
+
 static const struct sbi_sse_cb_ops pmu_sse_cb_ops = {
+	.register_cb = pmu_sse_register,
+	.unregister_cb = pmu_sse_unregister,
 	.enable_cb = pmu_sse_enable,
 	.disable_cb = pmu_sse_disable,
 	.complete_cb = pmu_sse_complete,
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

PATCH v2 2/2 lib: sbi: pmu: Remove MIP clearing from pmu_sse_enable()

In reply to Clément Léger

Clearing MIP at that point means that we can probably lose a pending interrupt. This should not happen, remove MIP clearing from there.

---
 lib/sbi/sbi_pmu.c | 1 -
 1 file changed, 1 deletion(-)
lib/sbi/sbi_pmu.c +0 -1
index dee50b9b..50476d7a 100644
@@ -1099,7 +1099,6 @@ static void pmu_sse_enable(uint32_t event_id)
 {
 	unsigned long irq_mask = sbi_pmu_irq_mask();
 
-	csr_clear(CSR_MIP, irq_mask);
 	csr_set(CSR_MIE, irq_mask);
 }
 
Signature · 7 lines
2.49.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] lib: sbi: pmu: Remove MIP clearing from pmu_sse_enable()

Quoted 14 lines · Clément Léger

Clearing MIP at that point means that we can probably lose a pending interrupt. This should not happen, remove MIP clearing from there.

---
 lib/sbi/sbi_pmu.c | 1 -
 1 file changed, 1 deletion(-)
lib/sbi/sbi_pmu.c +0 -0
index dee50b9b..50476d7a 100644

lib/sbi/sbi_pmu.c static void pmu_sse_enable(uint32_t event_id) · line 1104

Quoted 7 lines
lib/sbi/sbi_pmu.c +0 -1
@@ -1099,7 +1099,6 @@ static void pmu_sse_enable(uint32_t event_id)
 {
        unsigned long irq_mask = sbi_pmu_irq_mask();
 
-       csr_clear(CSR_MIP, irq_mask);
        csr_set(CSR_MIE, irq_mask);
 }
 
Quoted 3 lines

--
2.49.0

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] lib: sbi: pmu: Remove MIP clearing from pmu_sse_enable()

In reply to Clément Léger
Quoted 13 lines · Clément Léger

Clearing MIP at that point means that we can probably lose a pending interrupt. This should not happen, remove MIP clearing from there.

---
 lib/sbi/sbi_pmu.c | 1 -
 1 file changed, 1 deletion(-)
lib/sbi/sbi_pmu.c +0 -0
index dee50b9b..50476d7a 100644

lib/sbi/sbi_pmu.c static void pmu_sse_enable(uint32_t event_id) · line 1104

Quoted 7 lines
lib/sbi/sbi_pmu.c +0 -1
@@ -1099,7 +1099,6 @@ static void pmu_sse_enable(uint32_t event_id)
 {
 	unsigned long irq_mask = sbi_pmu_irq_mask();
 
-	csr_clear(CSR_MIP, irq_mask);
 	csr_set(CSR_MIE, irq_mask);
 }
 

This is now the same content as pmu_sse_complete(), so the two functions could be combined, if that does not hurt clarity.

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#7 lore raw
In reply to Clément Léger

Hi Clément,

Quoted 4 lines · Clément Léger

While testing the PMU SSE support in Linux, a few problem were spotted. The first one was due to interrupts happing during some PMU critical sections. Indeed, the PMU core actually disable interrupts when entering these sections. However, while this works when interrupts are targeting

Quoted 8 lines

the S-mode, this is not the case with SSE. Fixing this required to implement pmu_disable/enable() in Linux [1]. These two callbacks now call sse_event_disable/enable() which now just needs to set/clear MIE/MIP. Second problem was that reported PMU events count were quite low. This proved to be due to some interrupts being losed when reenabling SSE event. We still need to clear the pending PMU interrupts upon disabling otherwise we might get some spurious interrupts when reenabling it.

Could you clarify what could cause spurious interrupts here? Disabling the SSE event should just prevent it from being delivered to S-mode, right? It doesn't affect the event's pending status. My reading of the privileged spec is that mip.LCOFIP is edge-triggered by the individual hpmcounter OF bits. So if any counter is counting, and mip.LCOFIP is cleared without handing the overflow, S-mode would (erroneously) lose an overflow event. This could happen if for example an overflow occurs and pmu_sse_disable() was called both from within the SSE handler.

If my understanding is correct, to match the edge interrupt semantics (clear LCOFIP before handling, as noted in the privileged spec), I think the only place that should clear mip.LCOFIP is sbi_pmu_ovf_irq(), not sbi_pmu_ctr_stop() or pmu_sse_disable().

Regards,
Samuel

Quoted 9 lines

Clément Léger (2):
lib: sbi: pmu: Add SSE register/unregister() callbacks
lib: sbi: pmu: Remove MIP clearing from pmu_sse_enable()

 lib/sbi/sbi_pmu.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#8 lore raw
Quoted 16 lines · Samuel Holland

Hi Clément,

Quoted 4 lines · Clément Léger

While testing the PMU SSE support in Linux, a few problem were spotted. The first one was due to interrupts happing during some PMU critical sections. Indeed, the PMU core actually disable interrupts when entering these sections. However, while this works when interrupts are targeting

Quoted 8 lines

the S-mode, this is not the case with SSE. Fixing this required to implement pmu_disable/enable() in Linux [1]. These two callbacks now call sse_event_disable/enable() which now just needs to set/clear MIE/MIP. Second problem was that reported PMU events count were quite low. This proved to be due to some interrupts being losed when reenabling SSE event. We still need to clear the pending PMU interrupts upon disabling otherwise we might get some spurious interrupts when reenabling it.

Quoted 8 lines

Could you clarify what could cause spurious interrupts here? Disabling the SSE event should just prevent it from being delivered to S-mode, right? It doesn't affect the event's pending status. My reading of the privileged spec is that mip.LCOFIP is edge-triggered by the individual hpmcounter OF bits. So if any counter is counting, and mip.LCOFIP is cleared without handing the overflow, S-mode would (erroneously) lose an overflow event. This could happen if for example an overflow occurs and pmu_sse_disable() was called both from within the SSE handler.

Hi Samuel,

That's a good point and I must admit I based my comprehension of this on what the Linux driver was doing. ie: it stops the counters and then clears MIP (cf this comment from riscv_sbi_pmu.c)

/*
* Overflow interrupt pending bit should only be cleared after stopping
* all the counters to avoid any race condition.
*/

So in SSE, after discussing with Atish, we decided to clear MIP.LCOFIP in ctr_stop() (which makes sense to me). Regarding the MIP.LCOFIP clearing in sbi_sse_disable(), I actually added it so that we do not end up with a spurious IRQ as well right after disabling it. Thinking about it, even if we have an interrupt after that, the sbi_pmu_stop() code in Linux is stopping all the counters by calling the SBI so that should clear MIP as well. But I'll take a better look at it just to be sure I'm not missing anything.

Thanks,

Clément

Quoted 19 lines

If my understanding is correct, to match the edge interrupt semantics (clear LCOFIP before handling, as noted in the privileged spec), I think the only place that should clear mip.LCOFIP is sbi_pmu_ovf_irq(), not sbi_pmu_ctr_stop() or pmu_sse_disable().

Regards,
Samuel

Quoted 9 lines

Clément Léger (2):
lib: sbi: pmu: Add SSE register/unregister() callbacks
lib: sbi: pmu: Remove MIP clearing from pmu_sse_enable()

 lib/sbi/sbi_pmu.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Applied
#9 lore raw
In reply to Clément Léger
Quoted 14 lines · Clément Léger

While testing the PMU SSE support in Linux, a few problem were spotted. The first one was due to interrupts happing during some PMU critical sections. Indeed, the PMU core actually disable interrupts when entering these sections. However, while this works when interrupts are targeting the S-mode, this is not the case with SSE. Fixing this required to implement pmu_disable/enable() in Linux [1]. These two callbacks now call sse_event_disable/enable() which now just needs to set/clear MIE/MIP. Second problem was that reported PMU events count were quite low. This proved to be due to some interrupts being losed when reenabling SSE event. We still need to clear the pending PMU interrupts upon disabling otherwise we might get some spurious interrupts when reenabling it.

Quoted 5 lines

Clément Léger (2):
lib: sbi: pmu: Add SSE register/unregister() callbacks
lib: sbi: pmu: Remove MIP clearing from pmu_sse_enable()

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi