Skip to contentSkip to conversation

lib: sbi: sse: rework event availability

PATCH Superseded Clément Léger 2 participants 12 messages Dec 12, 2024 – Dec 16, 2024 RISC-V

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover lib: sbi: sse: rework event availability

While reworking the Linux SSE support, it appears that the PMU event should not be available if the SSCOFPMF extension is not available since there would be no IRQ. A request to modify the specification has been send in order to add the possibility to return SBI_ENOTSUPP (see [1]). This small series modifies the SSE core to detect event support at boot time using a new callback and return SBI_ENOTSUPP if needed.

---

Changes in V3:
- Rework is_supported_cb() to allow per-hart local event availability
- Remove now useless cached_version of is_supported.
- Fix eret check + potential empty eret in case event_is_supported() returns SBI_SUCCESS.
- Split commit to set all non software events as non injectable at the end of the series.
- Fix usage of sbi_pmu_irq_bit() in sbi_pmu.c

Changes in v2:
- Add an sse_event_info struct that stores 'cb_ops' as well as 'supported'
- Move SSE initialization after all other "drivers" init.
- Split changes in multiple commits
- Added an is_supported callback to check for event availability

Cl?ment L?ger (5):
lib: sbi: pmu: fix usage of sbi_pmu_irq_bit()
lib: sbi: sse: return an error value from sse_event_get()
lib: sbi: sse: rework event availability
lib: sbi: pmu: add an sse .is_supported_cb callback implementation
lib: sbi: sse: set non-software events as unsupported

 include/sbi/riscv_encoding.h                  |   1 -
 include/sbi/sbi_pmu.h                         |   3 +
 include/sbi/sbi_sse.h                         |   5 +
 lib/sbi/sbi_hart.c                            |   2 +-
 lib/sbi/sbi_init.c                            |  21 +-
 lib/sbi/sbi_pmu.c                             |  55 ++--
 lib/sbi/sbi_sse.c                             | 240 +++++++++++++-----
 lib/sbi/sbi_trap.c                            |  17 +-
 .../generic/include/thead/c9xx_encoding.h     |   1 -
 platform/generic/thead/thead_c9xx_pmu.c       |   2 +-
 10 files changed, 240 insertions(+), 107 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH v3 1/5 lib: sbi: pmu: fix usage of sbi_pmu_irq_bit()

While sbi_pmu_irq_bit() was used to delegate irq to S-mode, LCOFIP usage was still hardcoded in various places. This lead to change the returned value of sbi_pmu_irq_bit() to be a bit number rather than a bit mask since it returns an 'int' and we need to obtain the bit number itself to handle it in the IRQs handlers. Add a similar function to return the irq mask which can also be used where the mask is required rather than the bit itself.

---
 include/sbi/riscv_encoding.h                  |  1 -
 include/sbi/sbi_pmu.h                         |  3 ++
 lib/sbi/sbi_hart.c                            |  2 +-
 lib/sbi/sbi_pmu.c                             | 42 ++++++++++++-------
 lib/sbi/sbi_trap.c                            | 17 +++++---
 .../generic/include/thead/c9xx_encoding.h     |  1 -
 platform/generic/thead/thead_c9xx_pmu.c       |  2 +-
 7 files changed, 43 insertions(+), 25 deletions(-)
include/sbi/riscv_encoding.h +0 -1
index 38997ef4..3e834a8a 100644
@@ -109,7 +109,6 @@
 #define MIP_VSEIP			(_UL(1) << IRQ_VS_EXT)
 #define MIP_MEIP			(_UL(1) << IRQ_M_EXT)
 #define MIP_SGEIP			(_UL(1) << IRQ_S_GEXT)
-#define MIP_LCOFIP			(_UL(1) << IRQ_PMU_OVF)
 
 #define SIP_SSIP			MIP_SSIP
 #define SIP_STIP			MIP_STIP
include/sbi/sbi_pmu.h +3 -0
index 3649d476..c0e25f5a 100644
@@ -114,6 +114,9 @@ void sbi_pmu_exit(struct sbi_scratch *scratch);
 /** Return the pmu irq bit depending on extension existence */
 int sbi_pmu_irq_bit(void);
 
+/** Return the pmu irq mask or 0 if the pmu overflow irq is not supported */
+unsigned long sbi_pmu_irq_mask(void);
+
 /**
  * Add the hardware event to counter mapping information. This should be called
  * from the platform code to update the mapping table.
lib/sbi/sbi_hart.c +1 -1
index 0451fcb5..8e2979b5 100644
@@ -203,7 +203,7 @@ static int delegate_traps(struct sbi_scratch *scratch)
 
 	/* Send M-mode interrupts and most exceptions to S-mode */
 	interrupts = MIP_SSIP | MIP_STIP | MIP_SEIP;
-	interrupts |= sbi_pmu_irq_bit();
+	interrupts |= sbi_pmu_irq_mask();
 
 	exceptions = (1U << CAUSE_MISALIGNED_FETCH) | (1U << CAUSE_BREAKPOINT) |
 		     (1U << CAUSE_USER_ECALL);
lib/sbi/sbi_pmu.c +27 -15

90 lines not on this page · raw message

lib/sbi/sbi_trap.c +11 -6
index d2de0c8f..e29b13f9 100644
@@ -232,6 +232,11 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 
 static int sbi_trap_nonaia_irq(unsigned long irq)
 {
+	if (irq == sbi_pmu_irq_bit()) {
+		sbi_pmu_ovf_irq();
+		return 0;
+	}
+
 	switch (irq) {
 	case IRQ_M_TIMER:
 		sbi_timer_process();
@@ -239,9 +244,6 @@ static int sbi_trap_nonaia_irq(unsigned long irq)
 	case IRQ_M_SOFT:
 		sbi_ipi_process();
 		break;
-	case IRQ_PMU_OVF:
-		sbi_pmu_ovf_irq();
-		break;
 	case IRQ_M_EXT:
 		return sbi_irqchip_process();
 	default:
@@ -258,6 +260,12 @@ static int sbi_trap_aia_irq(void)
 
 	while ((mtopi = csr_read(CSR_MTOPI))) {
 		mtopi = mtopi >> TOPI_IID_SHIFT;
+
+		if (mtopi == sbi_pmu_irq_bit()) {
+			sbi_pmu_ovf_irq();
+			continue;
+		}
+
 		switch (mtopi) {
 		case IRQ_M_TIMER:
 			sbi_timer_process();
@@ -265,9 +273,6 @@ static int sbi_trap_aia_irq(void)
 		case IRQ_M_SOFT:
 			sbi_ipi_process();
 			break;
-		case IRQ_PMU_OVF:
-			sbi_pmu_ovf_irq();
-			break;
 		case IRQ_M_EXT:
 			rc = sbi_irqchip_process();
 			if (rc)
platform/generic/include/thead/c9xx_encoding.h +0 -1
index 58adbef0..a45b171e 100644
@@ -122,6 +122,5 @@
 
 /* T-HEAD C9xx MIP CSR extension */
 #define THEAD_C9XX_IRQ_PMU_OVF		17
-#define THEAD_C9XX_MIP_MOIP		(_UL(1) << THEAD_C9XX_IRQ_PMU_OVF)
 
 #endif
platform/generic/thead/thead_c9xx_pmu.c +1 -1
index d2058022..0b61e92a 100644
@@ -53,7 +53,7 @@ static void thead_c9xx_pmu_ctr_disable_irq(uint32_t ctr_idx)
 
 static int thead_c9xx_pmu_irq_bit(void)
 {
-	return THEAD_C9XX_MIP_MOIP;
+	return THEAD_C9XX_IRQ_PMU_OVF;
 }
 
 static const struct sbi_pmu_device thead_c9xx_pmu_device = {
Signature · 1 line
2.45.2
#2 lore raw

PATCH v3 1/5 lib: sbi: pmu: fix usage of sbi_pmu_irq_bit()

Quoted 25 lines · On Fri, Dec 13, 2024 at 2:04?AM Cl?ment L?ger <[email protected]> wrote:

While sbi_pmu_irq_bit() was used to delegate irq to S-mode, LCOFIP usage was still hardcoded in various places. This lead to change the returned value of sbi_pmu_irq_bit() to be a bit number rather than a bit mask since it returns an 'int' and we need to obtain the bit number itself to handle it in the IRQs handlers. Add a similar function to return the irq mask which can also be used where the mask is required rather than the bit itself.

---
 include/sbi/riscv_encoding.h                  |  1 -
 include/sbi/sbi_pmu.h                         |  3 ++
 lib/sbi/sbi_hart.c                            |  2 +-
 lib/sbi/sbi_pmu.c                             | 42 ++++++++++++-------
 lib/sbi/sbi_trap.c                            | 17 +++++---
 .../generic/include/thead/c9xx_encoding.h     |  1 -
 platform/generic/thead/thead_c9xx_pmu.c       |  2 +-
 7 files changed, 43 insertions(+), 25 deletions(-)
include/sbi/riscv_encoding.h +0 -0
index 38997ef4..3e834a8a 100644

include/sbi/riscv_encoding.h line 112

Quoted 4 lines
include/sbi/riscv_encoding.h +0 -1
@@ -109,7 +109,6 @@
 #define MIP_VSEIP                      (_UL(1) << IRQ_VS_EXT)
 #define MIP_MEIP                       (_UL(1) << IRQ_M_EXT)
 #define MIP_SGEIP                      (_UL(1) << IRQ_S_GEXT)
-#define MIP_LCOFIP                     (_UL(1) << IRQ_PMU_OVF)

No need to drop this define.

Quoted 94 lines
include/sbi/riscv_encoding.h +0 -0
 #define SIP_SSIP                       MIP_SSIP
 #define SIP_STIP                       MIP_STIP
include/sbi/sbi_pmu.h +3 -0
index 3649d476..c0e25f5a 100644
@@ -114,6 +114,9 @@ void sbi_pmu_exit(struct sbi_scratch *scratch);
 /** Return the pmu irq bit depending on extension existence */
 int sbi_pmu_irq_bit(void);
 
+/** Return the pmu irq mask or 0 if the pmu overflow irq is not supported */
+unsigned long sbi_pmu_irq_mask(void);
+
 /**
  * Add the hardware event to counter mapping information. This should be called
  * from the platform code to update the mapping table.
lib/sbi/sbi_hart.c +1 -1
index 0451fcb5..8e2979b5 100644
@@ -203,7 +203,7 @@ static int delegate_traps(struct sbi_scratch *scratch)
 
        /* Send M-mode interrupts and most exceptions to S-mode */
        interrupts = MIP_SSIP | MIP_STIP | MIP_SEIP;
-       interrupts |= sbi_pmu_irq_bit();
+       interrupts |= sbi_pmu_irq_mask();
 
        exceptions = (1U << CAUSE_MISALIGNED_FETCH) | (1U << CAUSE_BREAKPOINT) |
                     (1U << CAUSE_USER_ECALL);
lib/sbi/sbi_pmu.c +18 -8
index 0696ab5e..1eab0629 100644
@@ -309,10 +309,10 @@ int sbi_pmu_add_raw_event_counter_map(uint64_t select, uint64_t select_mask, u32
 void sbi_pmu_ovf_irq()
 {
        /*
-        * We need to disable LCOFIP before returning to S-mode or we will loop
-        * on LCOFIP being triggered
+        * We need to disable the overflow irq before returning to S-mode or we will loop
+        * on an irq being triggered
         */
-       csr_clear(CSR_MIE, MIP_LCOFIP);
+       csr_clear(CSR_MIE, sbi_pmu_irq_mask());
        sbi_sse_inject_event(SBI_SSE_EVENT_LOCAL_PMU);
 }
 
@@ -344,7 +344,7 @@ static int pmu_ctr_enable_irq_hw(int ctr_idx)
         * Otherwise, there will be race conditions where we may clear the bit
         * the software is yet to handle the interrupt.
         */
-       if (!(mip_val & MIP_LCOFIP)) {
+       if (!(mip_val & sbi_pmu_irq_mask())) {
                mhpmevent_curr &= of_mask;
                csr_write_num(mhpmevent_csr, mhpmevent_curr);
        }
@@ -405,11 +405,21 @@ int sbi_pmu_irq_bit(void)
        struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 
        if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
-               return MIP_LCOFIP;
+               return IRQ_PMU_OVF;
        if (pmu_dev && pmu_dev->hw_counter_irq_bit)
                return pmu_dev->hw_counter_irq_bit();
 
-       return 0;
+       return -1;
+}
+
+unsigned long sbi_pmu_irq_mask(void)
+{
+       int irq_bit = sbi_pmu_irq_bit();
+
+       if (irq_bit < 0)
+               return 0;
+
+       return BIT(irq_bit);
 }
 
 static int pmu_ctr_start_fw(struct sbi_pmu_hart_state *phs,
@@ -591,9 +601,9 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,
                }
        }
 
-       /* Clear MIP_LCOFIP to avoid spurious interrupts */
+       /* Clear PMU overflow interrupt to avoid spurious ones */
        if (phs->sse_enabled)
-               csr_clear(CSR_MIP, MIP_LCOFIP);
+               csr_clear(CSR_MIP, sbi_pmu_irq_mask());
 
        return ret;
 }

lib/sbi/sbi_pmu.c void sbi_pmu_exit(struct sbi_scratch *scratch) · line 1101

Quoted 4 lines
lib/sbi/sbi_pmu.c +1 -0
@@ -1088,26 +1098,28 @@ 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();
+       int irq_mask = sbi_pmu_irq_mask();

unsigned long ?

Quoted 4 lines
lib/sbi/sbi_pmu.c +0 -2
        phs->sse_enabled = true;
-       csr_clear(CSR_MIDELEG, sbi_pmu_irq_bit());
-       csr_clear(CSR_MIP, MIP_LCOFIP);

lib/sbi/sbi_pmu.c void sbi_pmu_exit(struct sbi_scratch *scratch) · line 1112

Quoted 10 lines
lib/sbi/sbi_pmu.c +4 -1
-       csr_set(CSR_MIE, MIP_LCOFIP);
+       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();
+       int irq_mask = sbi_pmu_irq_mask();

unsigned long ?

Quoted 22 lines
lib/sbi/sbi_pmu.c +4 -4
-       csr_clear(CSR_MIE, MIP_LCOFIP);
-       csr_clear(CSR_MIP, MIP_LCOFIP);
-       csr_set(CSR_MIDELEG, sbi_pmu_irq_bit());
+       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)
 {
-       csr_set(CSR_MIE, MIP_LCOFIP);
+       csr_set(CSR_MIE, sbi_pmu_irq_mask());
 }
 
 static const struct sbi_sse_cb_ops pmu_sse_cb_ops = {
lib/sbi/sbi_trap.c +0 -0
index d2de0c8f..e29b13f9 100644

lib/sbi/sbi_trap.c int sbi_trap_redirect(struct sbi_trap_regs *regs, · line 239

Quoted 8 lines
lib/sbi/sbi_trap.c +5 -0
@@ -232,6 +232,11 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
 
 static int sbi_trap_nonaia_irq(unsigned long irq)
 {
+       if (irq == sbi_pmu_irq_bit()) {
+               sbi_pmu_ovf_irq();
+               return 0;
+       }
+

This would cause the "if ()" to be done upon every irq.

Better move under the default case of the below switch case.

Quoted 14 lines
lib/sbi/sbi_trap.c +0 -3
        switch (irq) {
        case IRQ_M_TIMER:
                sbi_timer_process();
@@ -239,9 +244,6 @@ static int sbi_trap_nonaia_irq(unsigned long irq)
        case IRQ_M_SOFT:
                sbi_ipi_process();
                break;
-       case IRQ_PMU_OVF:
-               sbi_pmu_ovf_irq();
-               break;
        case IRQ_M_EXT:
                return sbi_irqchip_process();
        default:

lib/sbi/sbi_trap.c static int sbi_trap_aia_irq(void) · line 268

Quoted 9 lines
lib/sbi/sbi_trap.c +6 -0
@@ -258,6 +260,12 @@ static int sbi_trap_aia_irq(void)
 
        while ((mtopi = csr_read(CSR_MTOPI))) {
                mtopi = mtopi >> TOPI_IID_SHIFT;
+
+               if (mtopi == sbi_pmu_irq_bit()) {
+                       sbi_pmu_ovf_irq();
+                       continue;
+               }
+

Same comment as above.

Quoted 41 lines
lib/sbi/sbi_trap.c +0 -3
                switch (mtopi) {
                case IRQ_M_TIMER:
                        sbi_timer_process();
@@ -265,9 +273,6 @@ static int sbi_trap_aia_irq(void)
                case IRQ_M_SOFT:
                        sbi_ipi_process();
                        break;
-               case IRQ_PMU_OVF:
-                       sbi_pmu_ovf_irq();
-                       break;
                case IRQ_M_EXT:
                        rc = sbi_irqchip_process();
                        if (rc)
platform/generic/include/thead/c9xx_encoding.h +0 -1
index 58adbef0..a45b171e 100644
@@ -122,6 +122,5 @@
 
 /* T-HEAD C9xx MIP CSR extension */
 #define THEAD_C9XX_IRQ_PMU_OVF         17
-#define THEAD_C9XX_MIP_MOIP            (_UL(1) << THEAD_C9XX_IRQ_PMU_OVF)
 
 #endif
platform/generic/thead/thead_c9xx_pmu.c +1 -2
index d2058022..0b61e92a 100644
@@ -53,7 +53,7 @@ static void thead_c9xx_pmu_ctr_disable_irq(uint32_t ctr_idx)
 
 static int thead_c9xx_pmu_irq_bit(void)
 {
-       return THEAD_C9XX_MIP_MOIP;
+       return THEAD_C9XX_IRQ_PMU_OVF;
 }
 
 static const struct sbi_pmu_device thead_c9xx_pmu_device = {
--

2.45.2

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

#3 lore raw

PATCH v3 2/5 lib: sbi: sse: return an error value from sse_event_get()

In reply to Clément Léger

Since event support will be checked in the next commits, return a value from sse_event_get() to allow propagating it. This will be used to report SBI_ERR_NOT_SUPPORTED when an event isn't supported.

---
 lib/sbi/sbi_sse.c | 72 +++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 34 deletions(-)
lib/sbi/sbi_sse.c +38 -34
index c9f9a9dd..f710338e 100644
@@ -244,30 +244,36 @@ static void sse_event_set_state(struct sbi_sse_event *e,
 	e->attrs.status |= new_state;
 }
 
-static struct sbi_sse_event *sse_event_get(uint32_t event_id)
+static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
 {
 	unsigned int i;
 	struct sbi_sse_event *e;
 	struct sse_hart_state *shs;
 
+	if (!eret)
+		return SBI_EINVAL;
+
 	if (EVENT_IS_GLOBAL(event_id)) {
 		for (i = 0; i < global_event_count; i++) {
 			e = &global_events[i].event;
 			if (e->event_id == event_id) {
 				spin_lock(&global_events[i].lock);
-				return e;
+				*eret = e;
+				return SBI_SUCCESS;
 			}
 		}
 	} else {
 		shs = sse_thishart_state_ptr();
 		for (i = 0; i < local_event_count; i++) {
 			e = &shs->local_events[i];
-			if (e->event_id == event_id)
-				return e;
+			if (e->event_id == event_id) {
+				*eret = e;
+				return SBI_SUCCESS;
+			}
 		}
 	}
 
-	return NULL;
+	return SBI_EINVAL;
 }
 
 static void sse_event_put(struct sbi_sse_event *e)
@@ -653,8 +659,7 @@ static void sse_ipi_inject_process(struct sbi_scratch *scratch)
 
 	/* Mark all queued events as pending */
 	while (!sbi_fifo_dequeue(sse_inject_fifo_r, &evt)) {
-		e = sse_event_get(evt.event_id);
-		if (!e)
+		if (sse_event_get(evt.event_id, &e))
 			continue;
 
 		sse_event_set_pending(e);
@@ -696,10 +701,9 @@ static int sse_inject_event(uint32_t event_id, unsigned long hartid)
 	int ret;
 	struct sbi_sse_event *e;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
-
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	/* In case of global event, provided hart_id is ignored */
 	if (sse_event_is_global(e))
@@ -788,9 +792,9 @@ int sbi_sse_enable(uint32_t event_id)
 	int ret;
 	struct sbi_sse_event *e;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	sse_enabled_event_lock(e);
 	ret = sse_event_enable(e);
@@ -805,9 +809,9 @@ int sbi_sse_disable(uint32_t event_id)
 	int ret;
 	struct sbi_sse_event *e;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	sse_enabled_event_lock(e);
 	ret = sse_event_disable(e);
@@ -865,14 +869,15 @@ int sbi_sse_inject_event(uint32_t event_id)
 
 int sbi_sse_set_cb_ops(uint32_t event_id, const struct sbi_sse_cb_ops *cb_ops)
 {
+	int ret;
 	struct sbi_sse_event *e;
 
 	if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
 		return SBI_EINVAL;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	e->cb_ops = cb_ops;
 	sse_event_put(e);
@@ -943,9 +948,9 @@ int sbi_sse_read_attrs(uint32_t event_id, uint32_t base_attr_id,
 	if (ret)
 		return ret;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	sbi_hart_map_saddr(output_phys_lo, sizeof(unsigned long) * attr_count);
 
@@ -1008,9 +1013,9 @@ int sbi_sse_write_attrs(uint32_t event_id, uint32_t base_attr_id,
 	if (ret)
 		return ret;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	ret = sse_write_attrs(e, base_attr_id, attr_count, input_phys_lo);
 	sse_event_put(e);
@@ -1033,9 +1038,9 @@ int sbi_sse_register(uint32_t event_id, unsigned long handler_entry_pc,
 					 SBI_DOMAIN_EXECUTE))
 		return SBI_EINVALID_ADDR;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	ret = sse_event_register(e, handler_entry_pc, handler_entry_arg);
 	sse_event_put(e);
@@ -1048,9 +1053,9 @@ int sbi_sse_unregister(uint32_t event_id)
 	int ret;
 	struct sbi_sse_event *e;
 
-	e = sse_event_get(event_id);
-	if (!e)
-		return SBI_EINVAL;
+	ret = sse_event_get(event_id, &e);
+	if (ret)
+		return ret;
 
 	ret = sse_event_unregister(e);
 	sse_event_put(e);
@@ -1192,8 +1197,7 @@ void sbi_sse_exit(struct sbi_scratch *scratch)
 	struct sbi_sse_event *e;
 
 	for (i = 0; i < EVENT_COUNT; i++) {
-		e = sse_event_get(supported_events[i]);
-		if (!e)
+		if (sse_event_get(supported_events[i], &e))
 			continue;
 
 		if (e->attrs.hartid != current_hartid())
Signature · 1 line
2.45.2
#4 lore raw

PATCH v3 2/5 lib: sbi: sse: return an error value from sse_event_get()

Quoted 7 lines · On Fri, Dec 13, 2024 at 2:04?AM Cl?ment L?ger <[email protected]> wrote:

Since event support will be checked in the next commits, return a value from sse_event_get() to allow propagating it. This will be used to report SBI_ERR_NOT_SUPPORTED when an event isn't supported.

LGTM.

Regards,
Anup

Quoted 189 lines
---
 lib/sbi/sbi_sse.c | 72 +++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 34 deletions(-)
lib/sbi/sbi_sse.c +38 -35
index c9f9a9dd..f710338e 100644
@@ -244,30 +244,36 @@ static void sse_event_set_state(struct sbi_sse_event *e,
        e->attrs.status |= new_state;
 }
 
-static struct sbi_sse_event *sse_event_get(uint32_t event_id)
+static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
 {
        unsigned int i;
        struct sbi_sse_event *e;
        struct sse_hart_state *shs;
 
+       if (!eret)
+               return SBI_EINVAL;
+
        if (EVENT_IS_GLOBAL(event_id)) {
                for (i = 0; i < global_event_count; i++) {
                        e = &global_events[i].event;
                        if (e->event_id == event_id) {
                                spin_lock(&global_events[i].lock);
-                               return e;
+                               *eret = e;
+                               return SBI_SUCCESS;
                        }
                }
        } else {
                shs = sse_thishart_state_ptr();
                for (i = 0; i < local_event_count; i++) {
                        e = &shs->local_events[i];
-                       if (e->event_id == event_id)
-                               return e;
+                       if (e->event_id == event_id) {
+                               *eret = e;
+                               return SBI_SUCCESS;
+                       }
                }
        }
 
-       return NULL;
+       return SBI_EINVAL;
 }
 
 static void sse_event_put(struct sbi_sse_event *e)
@@ -653,8 +659,7 @@ static void sse_ipi_inject_process(struct sbi_scratch *scratch)
 
        /* Mark all queued events as pending */
        while (!sbi_fifo_dequeue(sse_inject_fifo_r, &evt)) {
-               e = sse_event_get(evt.event_id);
-               if (!e)
+               if (sse_event_get(evt.event_id, &e))
                        continue;
 
                sse_event_set_pending(e);
@@ -696,10 +701,9 @@ static int sse_inject_event(uint32_t event_id, unsigned long hartid)
        int ret;
        struct sbi_sse_event *e;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
-
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        /* In case of global event, provided hart_id is ignored */
        if (sse_event_is_global(e))
@@ -788,9 +792,9 @@ int sbi_sse_enable(uint32_t event_id)
        int ret;
        struct sbi_sse_event *e;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        sse_enabled_event_lock(e);
        ret = sse_event_enable(e);
@@ -805,9 +809,9 @@ int sbi_sse_disable(uint32_t event_id)
        int ret;
        struct sbi_sse_event *e;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        sse_enabled_event_lock(e);
        ret = sse_event_disable(e);
@@ -865,14 +869,15 @@ int sbi_sse_inject_event(uint32_t event_id)
 
 int sbi_sse_set_cb_ops(uint32_t event_id, const struct sbi_sse_cb_ops *cb_ops)
 {
+       int ret;
        struct sbi_sse_event *e;
 
        if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
                return SBI_EINVAL;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        e->cb_ops = cb_ops;
        sse_event_put(e);
@@ -943,9 +948,9 @@ int sbi_sse_read_attrs(uint32_t event_id, uint32_t base_attr_id,
        if (ret)
                return ret;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        sbi_hart_map_saddr(output_phys_lo, sizeof(unsigned long) * attr_count);
 
@@ -1008,9 +1013,9 @@ int sbi_sse_write_attrs(uint32_t event_id, uint32_t base_attr_id,
        if (ret)
                return ret;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        ret = sse_write_attrs(e, base_attr_id, attr_count, input_phys_lo);
        sse_event_put(e);
@@ -1033,9 +1038,9 @@ int sbi_sse_register(uint32_t event_id, unsigned long handler_entry_pc,
                                         SBI_DOMAIN_EXECUTE))
                return SBI_EINVALID_ADDR;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        ret = sse_event_register(e, handler_entry_pc, handler_entry_arg);
        sse_event_put(e);
@@ -1048,9 +1053,9 @@ int sbi_sse_unregister(uint32_t event_id)
        int ret;
        struct sbi_sse_event *e;
 
-       e = sse_event_get(event_id);
-       if (!e)
-               return SBI_EINVAL;
+       ret = sse_event_get(event_id, &e);
+       if (ret)
+               return ret;
 
        ret = sse_event_unregister(e);
        sse_event_put(e);
@@ -1192,8 +1197,7 @@ void sbi_sse_exit(struct sbi_scratch *scratch)
        struct sbi_sse_event *e;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               e = sse_event_get(supported_events[i]);
-               if (!e)
+               if (sse_event_get(supported_events[i], &e))
                        continue;
 
                if (e->attrs.hartid != current_hartid())
--

2.45.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#5 lore raw

PATCH v3 3/5 lib: sbi: sse: rework event availability

In reply to Clément Léger

In order to detect events that are available and supported, add a new is_supported_cb() callback and move cb_ops in event_info.

The cb_ops are now global for a specific event_id and can be filled by drivers before calling sse init. sse_init() will then the callback to determine which events are available. Global events are checked only once and local_events are checked per hart. Thanks to that, we can now allocate only the supported global and local events and defer sse probing. All events are assigned a default callback operations that reports true for the is_supported_cb() to keep the existing behavior. Since the callback operations can now be registered once for all harts, move the pmu sse_set_cb_ops() callback to the cold_boot 'if'.

Checking the new additional is_supported_cb() is only done when we fail from getting an event so that there is no performance cost.

lib: sbi: sse: add a is_supported_cb() callback

This callback will be used to expose the availability of events based on the presence of the dependencies (PMU Overflow IRQ for the PMU Overflow SSE event for instance). Drivers that want to handle an SSE event will need to register callbacks for the SSE event with at least the is_supported_cb() callback implemented. Now that this callback is implemented, set all non software events as not supported by default.

---
 include/sbi/sbi_sse.h |   5 ++
 lib/sbi/sbi_init.c    |  21 ++---
 lib/sbi/sbi_pmu.c     |   4 +-
 lib/sbi/sbi_sse.c     | 182 ++++++++++++++++++++++++++++++++----------
 4 files changed, 157 insertions(+), 55 deletions(-)
include/sbi/sbi_sse.h +5 -0
index 7419698a..1eab4f8b 100644
@@ -52,6 +52,11 @@ struct sbi_sse_cb_ops {
 	 * Called when the SBI_EXT_SSE_DISABLE is invoked on the event.
 	 */
 	void (*disable_cb)(uint32_t event_id);
+
+	/**
+	 * Called at init to check if an event is supported.
+	 */
+	bool (*is_supported_cb)(uint32_t event_id);
 };
 
 /* Set the callback operations for an event
lib/sbi/sbi_init.c +11 -10
index 0736345d..b2627e60 100644
@@ -262,12 +262,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_sse_init(scratch, true);
-	if (rc) {
-		sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
-		sbi_hart_hang();
-	}
-
 	rc = sbi_pmu_init(scratch, true);
 	if (rc) {
 		sbi_printf("%s: pmu init failed (error %d)\n",
@@ -317,6 +311,13 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 		sbi_printf("%s: mpxy init failed (error %d)\n", __func__, rc);
 		sbi_hart_hang();
 	}
+
+	rc = sbi_sse_init(scratch, true);
+	if (rc) {
+		sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
+		sbi_hart_hang();
+	}
+
 	/*
 	 * Note: Finalize domains after HSM initialization so that we
 	 * can startup non-root domains.
@@ -404,10 +405,6 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
 	if (rc)
 		sbi_hart_hang();
 
-	rc = sbi_sse_init(scratch, false);
-	if (rc)
-		sbi_hart_hang();
-
 	rc = sbi_pmu_init(scratch, false);
 	if (rc)
 		sbi_hart_hang();
@@ -440,6 +437,10 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
 	if (rc)
 		sbi_hart_hang();
 
+	rc = sbi_sse_init(scratch, false);
+	if (rc)
+		sbi_hart_hang();
+
 	/*
 	 * Configure PMP at last because if SMEPMP is detected,
 	 * M-mode access to the S/U space will be rescinded.
lib/sbi/sbi_pmu.c +2 -2
index 1eab0629..182d8db9 100644
@@ -1165,9 +1165,9 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
 			return SBI_EINVAL;
 
 		total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
-	}
 
-	sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+		sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+	}
 
 	phs = pmu_get_hart_state_ptr(scratch);
 	if (!phs) {
lib/sbi/sbi_sse.c +139 -43

318 lines not on this page · raw message

Signature · 1 line
2.45.2
#6 lore raw

PATCH v3 3/5 lib: sbi: sse: rework event availability

Quoted 95 lines · On Fri, Dec 13, 2024 at 2:04?AM Cl?ment L?ger <[email protected]> wrote:

In order to detect events that are available and supported, add a new is_supported_cb() callback and move cb_ops in event_info.

The cb_ops are now global for a specific event_id and can be filled by drivers before calling sse init. sse_init() will then the callback to determine which events are available. Global events are checked only once and local_events are checked per hart. Thanks to that, we can now allocate only the supported global and local events and defer sse probing. All events are assigned a default callback operations that reports true for the is_supported_cb() to keep the existing behavior. Since the callback operations can now be registered once for all harts, move the pmu sse_set_cb_ops() callback to the cold_boot 'if'.

Checking the new additional is_supported_cb() is only done when we fail from getting an event so that there is no performance cost.

lib: sbi: sse: add a is_supported_cb() callback

This callback will be used to expose the availability of events based on the presence of the dependencies (PMU Overflow IRQ for the PMU Overflow SSE event for instance). Drivers that want to handle an SSE event will need to register callbacks for the SSE event with at least the is_supported_cb() callback implemented. Now that this callback is implemented, set all non software events as not supported by default.

---
 include/sbi/sbi_sse.h |   5 ++
 lib/sbi/sbi_init.c    |  21 ++---
 lib/sbi/sbi_pmu.c     |   4 +-
 lib/sbi/sbi_sse.c     | 182 ++++++++++++++++++++++++++++++++----------
 4 files changed, 157 insertions(+), 55 deletions(-)
include/sbi/sbi_sse.h +5 -0
index 7419698a..1eab4f8b 100644
@@ -52,6 +52,11 @@ struct sbi_sse_cb_ops {
         * Called when the SBI_EXT_SSE_DISABLE is invoked on the event.
         */
        void (*disable_cb)(uint32_t event_id);
+
+       /**
+        * Called at init to check if an event is supported.
+        */
+       bool (*is_supported_cb)(uint32_t event_id);
 };
 
 /* Set the callback operations for an event
lib/sbi/sbi_init.c +7 -10
index 0736345d..b2627e60 100644
@@ -262,12 +262,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, true);
-       if (rc) {
-               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
-               sbi_hart_hang();
-       }
-
        rc = sbi_pmu_init(scratch, true);
        if (rc) {
                sbi_printf("%s: pmu init failed (error %d)\n",
@@ -317,6 +311,13 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_printf("%s: mpxy init failed (error %d)\n", __func__, rc);
                sbi_hart_hang();
        }
+
+       rc = sbi_sse_init(scratch, true);
+       if (rc) {
+               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
+               sbi_hart_hang();
+       }
+
        /*
         * Note: Finalize domains after HSM initialization so that we
         * can startup non-root domains.
@@ -404,10 +405,6 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, false);
-       if (rc)
-               sbi_hart_hang();
-
        rc = sbi_pmu_init(scratch, false);
        if (rc)
                sbi_hart_hang();

lib/sbi/sbi_init.c static void __noreturn init_warm_startup(struct sbi_scratch *scratch, · line 443

Quoted 7 lines
lib/sbi/sbi_init.c +4 -0
@@ -440,6 +437,10 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
+       rc = sbi_sse_init(scratch, false);
+       if (rc)
+               sbi_hart_hang();
+

This re-ordering of sbi_sse_init() is not going to fly because in the future we might have SSE events being registered by drivers from sbi_platform_final_init().

Quoted 45 lines
lib/sbi/sbi_init.c +0 -0
        /*
         * Configure PMP at last because if SMEPMP is detected,
         * M-mode access to the S/U space will be rescinded.
lib/sbi/sbi_pmu.c +2 -2
index 1eab0629..182d8db9 100644
@@ -1165,9 +1165,9 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
                        return SBI_EINVAL;
 
                total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
-       }
 
-       sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+               sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+       }
 
        phs = pmu_get_hart_state_ptr(scratch);
        if (!phs) {
lib/sbi/sbi_sse.c +8 -8
index f710338e..f2a9d791 100644
@@ -39,21 +39,33 @@
 
 #define EVENT_IS_GLOBAL(__event_id) ((__event_id) & SBI_SSE_EVENT_GLOBAL_BIT)
 
-static const uint32_t supported_events[] = {
-       SBI_SSE_EVENT_LOCAL_RAS,
-       SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP,
-       SBI_SSE_EVENT_GLOBAL_RAS,
-       SBI_SSE_EVENT_LOCAL_PMU,
-       SBI_SSE_EVENT_LOCAL_SOFTWARE,
-       SBI_SSE_EVENT_GLOBAL_SOFTWARE,
+struct sse_event_info {
+       uint32_t event_id;
+       const struct sbi_sse_cb_ops *cb_ops;
 };
 
-#define EVENT_COUNT array_size(supported_events)
+static bool event_supported_true(uint32_t event_id);
+
+static const struct sbi_sse_cb_ops supported_event_ops = {
+       .is_supported_cb = &event_supported_true
+};

lib/sbi/sbi_sse.c line 61

Quoted 10 lines
lib/sbi/sbi_sse.c +10 -0
+
+static struct sse_event_info valid_events[] = {
+       {.event_id = SBI_SSE_EVENT_LOCAL_RAS, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_RAS, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_PMU, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_SOFTWARE, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_SOFTWARE, .cb_ops = &supported_event_ops},
+};
+

This static table approach is not scalable and does not allow platform specific SSE events to be registered from drivers.

Initially you can start with a global linked list of supported SSE events which any one can register in the cold boot path.

The sbi_sse_init() should be simplified to not depend on supported SSE events to be pre-populated.

Quoted 304 lines
lib/sbi/sbi_sse.c +121 -36
+#define EVENT_COUNT array_size(valid_events)
 
 #define sse_event_invoke_cb(_event, _cb, ...)                                 \
        {                                                                     \
-               if (_event->cb_ops && _event->cb_ops->_cb)                    \
-                       _event->cb_ops->_cb(_event->event_id, ##__VA_ARGS__); \
+               const struct sbi_sse_cb_ops *__ops = _event->info->cb_ops;    \
+               if (__ops && __ops->_cb)                                          \
+                       __ops->_cb(_event->event_id, ##__VA_ARGS__);            \
        }
 
 struct sse_entry_state {
@@ -110,7 +122,7 @@ struct sbi_sse_event {
        struct sbi_sse_event_attrs attrs;
        uint32_t event_id;
        u32 hartindex;
-       const struct sbi_sse_cb_ops *cb_ops;
+       struct sse_event_info *info;
        struct sbi_dlist node;
 };
 
@@ -144,6 +156,11 @@ struct sse_hart_state {
         */
        struct sbi_sse_event *local_events;
 
+       /**
+        * Number of events in the local_events array.
+        */
+       unsigned int local_event_count;
+
        /**
         * State to track if the hart is ready to take sse events.
         * One hart cannot modify this state of another hart.
@@ -167,7 +184,6 @@ struct sse_global_event {
        spinlock_t lock;
 };
 
-static unsigned int local_event_count;
 static unsigned int global_event_count;
 static struct sse_global_event *global_events;
 
@@ -180,6 +196,11 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;
 
 static int sse_ipi_inject_send(unsigned long hartid, uint32_t event_id);
 
+static bool event_supported_true(uint32_t event_id)
+{
+       return true;
+}
+
 static unsigned long sse_event_state(struct sbi_sse_event *e)
 {
        return e->attrs.status & SBI_SSE_ATTR_STATUS_STATE_MASK;
@@ -244,8 +265,42 @@ static void sse_event_set_state(struct sbi_sse_event *e,
        e->attrs.status |= new_state;
 }
 
+static struct sse_event_info *sse_event_info_get(uint32_t event_id)
+{
+       struct sse_event_info *info;
+       int i;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+               if (info->event_id == event_id)
+                       return info;
+       }
+
+       return NULL;
+}
+
+static int sse_event_info_is_supported(struct sse_event_info *info)
+{
+       if (!info)
+               return SBI_EINVAL;
+
+       if (!info->cb_ops || !info->cb_ops->is_supported_cb)
+               return SBI_ENOTSUPP;
+
+       return info->cb_ops->is_supported_cb(info->event_id) ? SBI_SUCCESS :
+                                                              SBI_ENOTSUPP;
+}
+
+static int sse_event_is_supported(uint32_t event_id)
+{
+       struct sse_event_info *info = sse_event_info_get(event_id);
+
+       return sse_event_info_is_supported(info);
+}
+
 static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
 {
+       int ret;
        unsigned int i;
        struct sbi_sse_event *e;
        struct sse_hart_state *shs;
@@ -264,7 +319,7 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        } else {
                shs = sse_thishart_state_ptr();
-               for (i = 0; i < local_event_count; i++) {
+               for (i = 0; i < shs->local_event_count; i++) {
                        e = &shs->local_events[i];
                        if (e->event_id == event_id) {
                                *eret = e;
@@ -273,7 +328,16 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        }
 
-       return SBI_EINVAL;
+       ret = sse_event_is_supported(event_id);
+       /*
+        * This should never happen since if an event is supported it should
+        * have been handled by the previous chunk of code. But better be safe
+        * than sorry and return EFAIL.
+        */
+       if (ret == SBI_SUCCESS)
+               return SBI_EFAIL;
+
+       return ret;
 }
 
 static void sse_event_put(struct sbi_sse_event *e)
@@ -869,18 +933,16 @@ int sbi_sse_inject_event(uint32_t event_id)
 
 int sbi_sse_set_cb_ops(uint32_t event_id, const struct sbi_sse_cb_ops *cb_ops)
 {
-       int ret;
-       struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
-       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+       info = sse_event_info_get(event_id);
+       if (!info)
                return SBI_EINVAL;
 
-       ret = sse_event_get(event_id, &e);
-       if (ret)
-               return ret;
+       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+               return SBI_EINVAL;
 
-       e->cb_ops = cb_ops;
-       sse_event_put(e);
+       info->cb_ops = cb_ops;
 
        return SBI_OK;
 }
@@ -1063,42 +1125,47 @@ int sbi_sse_unregister(uint32_t event_id)
        return ret;
 }
 
-static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)
+static void sse_event_init(struct sbi_sse_event *e, struct sse_event_info *info)
 {
-       e->event_id = event_id;
+       e->event_id = info->event_id;
+       e->info = info;
        e->hartindex = current_hartindex();
        e->attrs.hartid = current_hartid();
        /* Declare all events as injectable */
        e->attrs.status |= BIT(SBI_SSE_ATTR_STATUS_INJECT_OFFSET);
 }
 
-static void sse_event_count_init()
+static int sse_global_init(void)
 {
-       unsigned int i;
+       struct sse_event_info *info;
+       struct sbi_sse_event *e;
+       unsigned int i, ev = 0;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
-                       global_event_count++;
-               else
-                       local_event_count++;
+               info = &valid_events[i];
+
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               global_event_count++;
        }
-}
 
-static int sse_global_init()
-{
-       struct sbi_sse_event *e;
-       unsigned int i, ev = 0;
+       if (!global_event_count)
+               return 0;
 
        global_events = sbi_zalloc(sizeof(*global_events) * global_event_count);
        if (!global_events)
                return SBI_ENOMEM;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (!EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
                e = &global_events[ev].event;
-               sse_event_init(e, supported_events[i]);
+               sse_event_init(e, info);
                SPIN_LOCK_INIT(global_events[ev].lock);
 
                ev++;
@@ -1107,18 +1174,40 @@ static int sse_global_init()
        return 0;
 }
 
+static unsigned int sse_get_local_event_count(void)
+{
+       int i;
+       struct sse_event_info *info;
+       unsigned int local_event_count = 0;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               local_event_count++;
+       }
+
+       return local_event_count;
+}
+
 static void sse_local_init(struct sse_hart_state *shs)
 {
+       struct sse_event_info *info;
        unsigned int i, ev = 0;
 
        SBI_INIT_LIST_HEAD(&shs->enabled_event_list);
        SPIN_LOCK_INIT(shs->enabled_event_lock);
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
-               sse_event_init(&shs->local_events[ev++], supported_events[i]);
+               sse_event_init(&shs->local_events[ev++], info);
        }
 }
 
@@ -1128,10 +1217,9 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
        void *sse_inject_mem;
        struct sse_hart_state *shs;
        struct sbi_fifo *sse_inject_q;
+       unsigned int local_event_count = 0;
 
        if (cold_boot) {
-               sse_event_count_init();
-
                ret = sse_global_init();
                if (ret)
                        return ret;
@@ -1165,13 +1253,16 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
 
        shs = sse_get_hart_state_ptr(scratch);
        if (!shs) {
+               local_event_count = sse_get_local_event_count();
                /* Allocate per hart state and local events at once */
                shs = sbi_zalloc(sizeof(*shs) + sizeof(struct sbi_sse_event) *
                                                        local_event_count);
                if (!shs)
                        return SBI_ENOMEM;
 
-               shs->local_events = (struct sbi_sse_event *)(shs + 1);
+               shs->local_event_count = local_event_count;
+               if (local_event_count)
+                       shs->local_events = (struct sbi_sse_event *)(shs + 1);
 
                /* SSE events are masked until hart unmasks them */
                shs->masked = true;
@@ -1195,9 +1286,14 @@ void sbi_sse_exit(struct sbi_scratch *scratch)
 {
        int i;
        struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (sse_event_get(supported_events[i], &e))
+               info = &valid_events[i];
+               if (sse_event_info_is_supported(info) != SBI_SUCCESS)
+                       continue;
+
+               if (sse_event_get(info->event_id, &e))
                        continue;
 
                if (e->attrs.hartid != current_hartid())
--

2.45.2

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

This patch needs to be heavily re-worked.

Regards,
Anup

#7 lore raw

PATCH v3 3/5 lib: sbi: sse: rework event availability

Quoted 158 lines · Anup Patel
Quoted 95 lines · On Fri, Dec 13, 2024 at 2:04?AM Cl?ment L?ger <[email protected]> wrote:

In order to detect events that are available and supported, add a new is_supported_cb() callback and move cb_ops in event_info.

The cb_ops are now global for a specific event_id and can be filled by drivers before calling sse init. sse_init() will then the callback to determine which events are available. Global events are checked only once and local_events are checked per hart. Thanks to that, we can now allocate only the supported global and local events and defer sse probing. All events are assigned a default callback operations that reports true for the is_supported_cb() to keep the existing behavior. Since the callback operations can now be registered once for all harts, move the pmu sse_set_cb_ops() callback to the cold_boot 'if'.

Checking the new additional is_supported_cb() is only done when we fail from getting an event so that there is no performance cost.

lib: sbi: sse: add a is_supported_cb() callback

This callback will be used to expose the availability of events based on the presence of the dependencies (PMU Overflow IRQ for the PMU Overflow SSE event for instance). Drivers that want to handle an SSE event will need to register callbacks for the SSE event with at least the is_supported_cb() callback implemented. Now that this callback is implemented, set all non software events as not supported by default.

---
 include/sbi/sbi_sse.h |   5 ++
 lib/sbi/sbi_init.c    |  21 ++---
 lib/sbi/sbi_pmu.c     |   4 +-
 lib/sbi/sbi_sse.c     | 182 ++++++++++++++++++++++++++++++++----------
 4 files changed, 157 insertions(+), 55 deletions(-)
include/sbi/sbi_sse.h +5 -0
index 7419698a..1eab4f8b 100644
@@ -52,6 +52,11 @@ struct sbi_sse_cb_ops {
         * Called when the SBI_EXT_SSE_DISABLE is invoked on the event.
         */
        void (*disable_cb)(uint32_t event_id);
+
+       /**
+        * Called at init to check if an event is supported.
+        */
+       bool (*is_supported_cb)(uint32_t event_id);
 };
 
 /* Set the callback operations for an event
lib/sbi/sbi_init.c +7 -10
index 0736345d..b2627e60 100644
@@ -262,12 +262,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, true);
-       if (rc) {
-               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
-               sbi_hart_hang();
-       }
-
        rc = sbi_pmu_init(scratch, true);
        if (rc) {
                sbi_printf("%s: pmu init failed (error %d)\n",
@@ -317,6 +311,13 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_printf("%s: mpxy init failed (error %d)\n", __func__, rc);
                sbi_hart_hang();
        }
+
+       rc = sbi_sse_init(scratch, true);
+       if (rc) {
+               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
+               sbi_hart_hang();
+       }
+
        /*
         * Note: Finalize domains after HSM initialization so that we
         * can startup non-root domains.
@@ -404,10 +405,6 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, false);
-       if (rc)
-               sbi_hart_hang();
-
        rc = sbi_pmu_init(scratch, false);
        if (rc)
                sbi_hart_hang();

lib/sbi/sbi_init.c static void __noreturn init_warm_startup(struct sbi_scratch *scratch, · line 443

Quoted 7 lines
lib/sbi/sbi_init.c +4 -0
@@ -440,6 +437,10 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
+       rc = sbi_sse_init(scratch, false);
+       if (rc)
+               sbi_hart_hang();
+

This re-ordering of sbi_sse_init() is not going to fly because in the future we might have SSE events being registered by drivers from sbi_platform_final_init().

Quoted 50 lines
lib/sbi/sbi_init.c +0 -0
        /*
         * Configure PMP at last because if SMEPMP is detected,
         * M-mode access to the S/U space will be rescinded.
lib/sbi/sbi_pmu.c +2 -2
index 1eab0629..182d8db9 100644
@@ -1165,9 +1165,9 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
                        return SBI_EINVAL;
 
                total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
-       }
 
-       sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+               sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+       }
 
        phs = pmu_get_hart_state_ptr(scratch);
        if (!phs) {
lib/sbi/sbi_sse.c +13 -8
index f710338e..f2a9d791 100644
@@ -39,21 +39,33 @@
 
 #define EVENT_IS_GLOBAL(__event_id) ((__event_id) & SBI_SSE_EVENT_GLOBAL_BIT)
 
-static const uint32_t supported_events[] = {
-       SBI_SSE_EVENT_LOCAL_RAS,
-       SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP,
-       SBI_SSE_EVENT_GLOBAL_RAS,
-       SBI_SSE_EVENT_LOCAL_PMU,
-       SBI_SSE_EVENT_LOCAL_SOFTWARE,
-       SBI_SSE_EVENT_GLOBAL_SOFTWARE,
+struct sse_event_info {
+       uint32_t event_id;
+       const struct sbi_sse_cb_ops *cb_ops;
 };
 
-#define EVENT_COUNT array_size(supported_events)
+static bool event_supported_true(uint32_t event_id);
+
+static const struct sbi_sse_cb_ops supported_event_ops = {
+       .is_supported_cb = &event_supported_true
+};
+
+static struct sse_event_info valid_events[] = {
+       {.event_id = SBI_SSE_EVENT_LOCAL_RAS, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_RAS, .cb_ops = &supported_event_ops},
Quoted 8 lines

lib/sbi/sbi_sse.c line 61

Quoted 5 lines
lib/sbi/sbi_sse.c +5 -0
+       {.event_id = SBI_SSE_EVENT_LOCAL_PMU, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_SOFTWARE, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_SOFTWARE, .cb_ops = &supported_event_ops},
+};
+

This static table approach is not scalable and does not allow platform specific SSE events to be registered from drivers.

Agreed.

Quoted 6 lines

Initially you can start with a global linked list of supported SSE events which any one can register in the cold boot path.

The sbi_sse_init() should be simplified to not depend on supported SSE events to be pre-populated.

IMHO, all the events that will be handled by drivers should be available by the time sbi_sse_init() is called or the complexity will be deferred to some other functions as well. This call can even be moved after sbi_platform_final_init(). Except if you have another reason not to do so but that would be the best way to go.

To clarify, I'll modify the current patch to use a linked list of events populated only with the GLOBAL_SOFTWARE_EVENT and LOCAL_SOFTWARE_EVENT at compile time. This list can be extended at boot time by registering cb_ops for events with sbi_sse_set_cb_ops() from drivers. sbi_sse_init() will now allocate space for all events that are in the linked list of events (no matter if they are supported or not). Event availability will be then tested using the is_supported() callback and stored in the event struct itself. Finally, sbi_sse_init() will be moved after all other driver init (ie after sbi_platform_final_init()). If you envision something else, please detail it.

Thanks,

Cl?ment

Quoted 314 lines
Quoted 305 lines
lib/sbi/sbi_sse.c +121 -36
+#define EVENT_COUNT array_size(valid_events)
 
 #define sse_event_invoke_cb(_event, _cb, ...)                                 \
        {                                                                     \
-               if (_event->cb_ops && _event->cb_ops->_cb)                    \
-                       _event->cb_ops->_cb(_event->event_id, ##__VA_ARGS__); \
+               const struct sbi_sse_cb_ops *__ops = _event->info->cb_ops;    \
+               if (__ops && __ops->_cb)                                          \
+                       __ops->_cb(_event->event_id, ##__VA_ARGS__);            \
        }
 
 struct sse_entry_state {
@@ -110,7 +122,7 @@ struct sbi_sse_event {
        struct sbi_sse_event_attrs attrs;
        uint32_t event_id;
        u32 hartindex;
-       const struct sbi_sse_cb_ops *cb_ops;
+       struct sse_event_info *info;
        struct sbi_dlist node;
 };
 
@@ -144,6 +156,11 @@ struct sse_hart_state {
         */
        struct sbi_sse_event *local_events;
 
+       /**
+        * Number of events in the local_events array.
+        */
+       unsigned int local_event_count;
+
        /**
         * State to track if the hart is ready to take sse events.
         * One hart cannot modify this state of another hart.
@@ -167,7 +184,6 @@ struct sse_global_event {
        spinlock_t lock;
 };
 
-static unsigned int local_event_count;
 static unsigned int global_event_count;
 static struct sse_global_event *global_events;
 
@@ -180,6 +196,11 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;
 
 static int sse_ipi_inject_send(unsigned long hartid, uint32_t event_id);
 
+static bool event_supported_true(uint32_t event_id)
+{
+       return true;
+}
+
 static unsigned long sse_event_state(struct sbi_sse_event *e)
 {
        return e->attrs.status & SBI_SSE_ATTR_STATUS_STATE_MASK;
@@ -244,8 +265,42 @@ static void sse_event_set_state(struct sbi_sse_event *e,
        e->attrs.status |= new_state;
 }
 
+static struct sse_event_info *sse_event_info_get(uint32_t event_id)
+{
+       struct sse_event_info *info;
+       int i;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+               if (info->event_id == event_id)
+                       return info;
+       }
+
+       return NULL;
+}
+
+static int sse_event_info_is_supported(struct sse_event_info *info)
+{
+       if (!info)
+               return SBI_EINVAL;
+
+       if (!info->cb_ops || !info->cb_ops->is_supported_cb)
+               return SBI_ENOTSUPP;
+
+       return info->cb_ops->is_supported_cb(info->event_id) ? SBI_SUCCESS :
+                                                              SBI_ENOTSUPP;
+}
+
+static int sse_event_is_supported(uint32_t event_id)
+{
+       struct sse_event_info *info = sse_event_info_get(event_id);
+
+       return sse_event_info_is_supported(info);
+}
+
 static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
 {
+       int ret;
        unsigned int i;
        struct sbi_sse_event *e;
        struct sse_hart_state *shs;
@@ -264,7 +319,7 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        } else {
                shs = sse_thishart_state_ptr();
-               for (i = 0; i < local_event_count; i++) {
+               for (i = 0; i < shs->local_event_count; i++) {
                        e = &shs->local_events[i];
                        if (e->event_id == event_id) {
                                *eret = e;
@@ -273,7 +328,16 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        }
 
-       return SBI_EINVAL;
+       ret = sse_event_is_supported(event_id);
+       /*
+        * This should never happen since if an event is supported it should
+        * have been handled by the previous chunk of code. But better be safe
+        * than sorry and return EFAIL.
+        */
+       if (ret == SBI_SUCCESS)
+               return SBI_EFAIL;
+
+       return ret;
 }
 
 static void sse_event_put(struct sbi_sse_event *e)
@@ -869,18 +933,16 @@ int sbi_sse_inject_event(uint32_t event_id)
 
 int sbi_sse_set_cb_ops(uint32_t event_id, const struct sbi_sse_cb_ops *cb_ops)
 {
-       int ret;
-       struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
-       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+       info = sse_event_info_get(event_id);
+       if (!info)
                return SBI_EINVAL;
 
-       ret = sse_event_get(event_id, &e);
-       if (ret)
-               return ret;
+       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+               return SBI_EINVAL;
 
-       e->cb_ops = cb_ops;
-       sse_event_put(e);
+       info->cb_ops = cb_ops;
 
        return SBI_OK;
 }
@@ -1063,42 +1125,47 @@ int sbi_sse_unregister(uint32_t event_id)
        return ret;
 }
 
-static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)
+static void sse_event_init(struct sbi_sse_event *e, struct sse_event_info *info)
 {
-       e->event_id = event_id;
+       e->event_id = info->event_id;
+       e->info = info;
        e->hartindex = current_hartindex();
        e->attrs.hartid = current_hartid();
        /* Declare all events as injectable */
        e->attrs.status |= BIT(SBI_SSE_ATTR_STATUS_INJECT_OFFSET);
 }
 
-static void sse_event_count_init()
+static int sse_global_init(void)
 {
-       unsigned int i;
+       struct sse_event_info *info;
+       struct sbi_sse_event *e;
+       unsigned int i, ev = 0;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
-                       global_event_count++;
-               else
-                       local_event_count++;
+               info = &valid_events[i];
+
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               global_event_count++;
        }
-}
 
-static int sse_global_init()
-{
-       struct sbi_sse_event *e;
-       unsigned int i, ev = 0;
+       if (!global_event_count)
+               return 0;
 
        global_events = sbi_zalloc(sizeof(*global_events) * global_event_count);
        if (!global_events)
                return SBI_ENOMEM;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (!EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
                e = &global_events[ev].event;
-               sse_event_init(e, supported_events[i]);
+               sse_event_init(e, info);
                SPIN_LOCK_INIT(global_events[ev].lock);
 
                ev++;
@@ -1107,18 +1174,40 @@ static int sse_global_init()
        return 0;
 }
 
+static unsigned int sse_get_local_event_count(void)
+{
+       int i;
+       struct sse_event_info *info;
+       unsigned int local_event_count = 0;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               local_event_count++;
+       }
+
+       return local_event_count;
+}
+
 static void sse_local_init(struct sse_hart_state *shs)
 {
+       struct sse_event_info *info;
        unsigned int i, ev = 0;
 
        SBI_INIT_LIST_HEAD(&shs->enabled_event_list);
        SPIN_LOCK_INIT(shs->enabled_event_lock);
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
-               sse_event_init(&shs->local_events[ev++], supported_events[i]);
+               sse_event_init(&shs->local_events[ev++], info);
        }
 }
 
@@ -1128,10 +1217,9 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
        void *sse_inject_mem;
        struct sse_hart_state *shs;
        struct sbi_fifo *sse_inject_q;
+       unsigned int local_event_count = 0;
 
        if (cold_boot) {
-               sse_event_count_init();
-
                ret = sse_global_init();
                if (ret)
                        return ret;
@@ -1165,13 +1253,16 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
 
        shs = sse_get_hart_state_ptr(scratch);
        if (!shs) {
+               local_event_count = sse_get_local_event_count();
                /* Allocate per hart state and local events at once */
                shs = sbi_zalloc(sizeof(*shs) + sizeof(struct sbi_sse_event) *
                                                        local_event_count);
                if (!shs)
                        return SBI_ENOMEM;
 
-               shs->local_events = (struct sbi_sse_event *)(shs + 1);
+               shs->local_event_count = local_event_count;
+               if (local_event_count)
+                       shs->local_events = (struct sbi_sse_event *)(shs + 1);
 
                /* SSE events are masked until hart unmasks them */
                shs->masked = true;
@@ -1195,9 +1286,14 @@ void sbi_sse_exit(struct sbi_scratch *scratch)
 {
        int i;
        struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (sse_event_get(supported_events[i], &e))
+               info = &valid_events[i];
+               if (sse_event_info_is_supported(info) != SBI_SUCCESS)
+                       continue;
+
+               if (sse_event_get(info->event_id, &e))
                        continue;
 
                if (e->attrs.hartid != current_hartid())
--

2.45.2

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

This patch needs to be heavily re-worked.

Regards,
Anup

#8 lore raw

PATCH v3 3/5 lib: sbi: sse: rework event availability

Quoted 177 lines · On Mon, Dec 16, 2024 at 2:55?PM Cl?ment L?ger <[email protected]> wrote:
Quoted 158 lines · Anup Patel
Quoted 95 lines · On Fri, Dec 13, 2024 at 2:04?AM Cl?ment L?ger <[email protected]> wrote:

In order to detect events that are available and supported, add a new is_supported_cb() callback and move cb_ops in event_info.

The cb_ops are now global for a specific event_id and can be filled by drivers before calling sse init. sse_init() will then the callback to determine which events are available. Global events are checked only once and local_events are checked per hart. Thanks to that, we can now allocate only the supported global and local events and defer sse probing. All events are assigned a default callback operations that reports true for the is_supported_cb() to keep the existing behavior. Since the callback operations can now be registered once for all harts, move the pmu sse_set_cb_ops() callback to the cold_boot 'if'.

Checking the new additional is_supported_cb() is only done when we fail from getting an event so that there is no performance cost.

lib: sbi: sse: add a is_supported_cb() callback

This callback will be used to expose the availability of events based on the presence of the dependencies (PMU Overflow IRQ for the PMU Overflow SSE event for instance). Drivers that want to handle an SSE event will need to register callbacks for the SSE event with at least the is_supported_cb() callback implemented. Now that this callback is implemented, set all non software events as not supported by default.

---
 include/sbi/sbi_sse.h |   5 ++
 lib/sbi/sbi_init.c    |  21 ++---
 lib/sbi/sbi_pmu.c     |   4 +-
 lib/sbi/sbi_sse.c     | 182 ++++++++++++++++++++++++++++++++----------
 4 files changed, 157 insertions(+), 55 deletions(-)
include/sbi/sbi_sse.h +5 -0
index 7419698a..1eab4f8b 100644
@@ -52,6 +52,11 @@ struct sbi_sse_cb_ops {
         * Called when the SBI_EXT_SSE_DISABLE is invoked on the event.
         */
        void (*disable_cb)(uint32_t event_id);
+
+       /**
+        * Called at init to check if an event is supported.
+        */
+       bool (*is_supported_cb)(uint32_t event_id);
 };
 
 /* Set the callback operations for an event
lib/sbi/sbi_init.c +7 -10
index 0736345d..b2627e60 100644
@@ -262,12 +262,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, true);
-       if (rc) {
-               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
-               sbi_hart_hang();
-       }
-
        rc = sbi_pmu_init(scratch, true);
        if (rc) {
                sbi_printf("%s: pmu init failed (error %d)\n",
@@ -317,6 +311,13 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_printf("%s: mpxy init failed (error %d)\n", __func__, rc);
                sbi_hart_hang();
        }
+
+       rc = sbi_sse_init(scratch, true);
+       if (rc) {
+               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
+               sbi_hart_hang();
+       }
+
        /*
         * Note: Finalize domains after HSM initialization so that we
         * can startup non-root domains.
@@ -404,10 +405,6 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, false);
-       if (rc)
-               sbi_hart_hang();
-
        rc = sbi_pmu_init(scratch, false);
        if (rc)
                sbi_hart_hang();

lib/sbi/sbi_init.c static void __noreturn init_warm_startup(struct sbi_scratch *scratch, · line 443

Quoted 7 lines
lib/sbi/sbi_init.c +4 -0
@@ -440,6 +437,10 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
+       rc = sbi_sse_init(scratch, false);
+       if (rc)
+               sbi_hart_hang();
+

This re-ordering of sbi_sse_init() is not going to fly because in the future we might have SSE events being registered by drivers from sbi_platform_final_init().

Quoted 50 lines
lib/sbi/sbi_init.c +0 -0
        /*
         * Configure PMP at last because if SMEPMP is detected,
         * M-mode access to the S/U space will be rescinded.
lib/sbi/sbi_pmu.c +2 -2
index 1eab0629..182d8db9 100644
@@ -1165,9 +1165,9 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
                        return SBI_EINVAL;
 
                total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
-       }
 
-       sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+               sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+       }
 
        phs = pmu_get_hart_state_ptr(scratch);
        if (!phs) {
lib/sbi/sbi_sse.c +13 -8
index f710338e..f2a9d791 100644
@@ -39,21 +39,33 @@
 
 #define EVENT_IS_GLOBAL(__event_id) ((__event_id) & SBI_SSE_EVENT_GLOBAL_BIT)
 
-static const uint32_t supported_events[] = {
-       SBI_SSE_EVENT_LOCAL_RAS,
-       SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP,
-       SBI_SSE_EVENT_GLOBAL_RAS,
-       SBI_SSE_EVENT_LOCAL_PMU,
-       SBI_SSE_EVENT_LOCAL_SOFTWARE,
-       SBI_SSE_EVENT_GLOBAL_SOFTWARE,
+struct sse_event_info {
+       uint32_t event_id;
+       const struct sbi_sse_cb_ops *cb_ops;
 };
 
-#define EVENT_COUNT array_size(supported_events)
+static bool event_supported_true(uint32_t event_id);
+
+static const struct sbi_sse_cb_ops supported_event_ops = {
+       .is_supported_cb = &event_supported_true
+};
+
+static struct sse_event_info valid_events[] = {
+       {.event_id = SBI_SSE_EVENT_LOCAL_RAS, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_RAS, .cb_ops = &supported_event_ops},
Quoted 8 lines

lib/sbi/sbi_sse.c line 61

Quoted 5 lines
lib/sbi/sbi_sse.c +5 -0
+       {.event_id = SBI_SSE_EVENT_LOCAL_PMU, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_SOFTWARE, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_SOFTWARE, .cb_ops = &supported_event_ops},
+};
+

This static table approach is not scalable and does not allow platform specific SSE events to be registered from drivers.

Agreed.

Quoted 4 lines

Initially you can start with a global linked list of supported SSE events which any one can register in the cold boot path.

Quoted 8 lines
Quoted 2 lines

The sbi_sse_init() should be simplified to not depend on supported SSE events to be pre-populated.

IMHO, all the events that will be handled by drivers should be available by the time sbi_sse_init() is called or the complexity will be deferred to some other functions as well. This call can even be moved after sbi_platform_final_init(). Except if you have another reason not to do so but that would be the best way to go.

Yes, its fine to move sbi_sse_init() after sbi_platform_final_init() but please add comments about why it has to be after sbi_platform_final_init(). We have already added similar comments for sbi_ecall_init().

Quoted 11 lines

To clarify, I'll modify the current patch to use a linked list of events populated only with the GLOBAL_SOFTWARE_EVENT and LOCAL_SOFTWARE_EVENT at compile time. This list can be extended at boot time by registering cb_ops for events with sbi_sse_set_cb_ops() from drivers. sbi_sse_init() will now allocate space for all events that are in the linked list of events (no matter if they are supported or not). Event availability will be then tested using the is_supported() callback and stored in the event struct itself. Finally, sbi_sse_init() will be moved after all other driver init (ie after sbi_platform_final_init()). If you envision something else, please detail it.

I think this should be fine.

Regards,
Anup

Quoted 320 lines

Thanks,

Cl?ment

Quoted 314 lines
Quoted 305 lines
lib/sbi/sbi_sse.c +121 -36
+#define EVENT_COUNT array_size(valid_events)
 
 #define sse_event_invoke_cb(_event, _cb, ...)                                 \
        {                                                                     \
-               if (_event->cb_ops && _event->cb_ops->_cb)                    \
-                       _event->cb_ops->_cb(_event->event_id, ##__VA_ARGS__); \
+               const struct sbi_sse_cb_ops *__ops = _event->info->cb_ops;    \
+               if (__ops && __ops->_cb)                                          \
+                       __ops->_cb(_event->event_id, ##__VA_ARGS__);            \
        }
 
 struct sse_entry_state {
@@ -110,7 +122,7 @@ struct sbi_sse_event {
        struct sbi_sse_event_attrs attrs;
        uint32_t event_id;
        u32 hartindex;
-       const struct sbi_sse_cb_ops *cb_ops;
+       struct sse_event_info *info;
        struct sbi_dlist node;
 };
 
@@ -144,6 +156,11 @@ struct sse_hart_state {
         */
        struct sbi_sse_event *local_events;
 
+       /**
+        * Number of events in the local_events array.
+        */
+       unsigned int local_event_count;
+
        /**
         * State to track if the hart is ready to take sse events.
         * One hart cannot modify this state of another hart.
@@ -167,7 +184,6 @@ struct sse_global_event {
        spinlock_t lock;
 };
 
-static unsigned int local_event_count;
 static unsigned int global_event_count;
 static struct sse_global_event *global_events;
 
@@ -180,6 +196,11 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;
 
 static int sse_ipi_inject_send(unsigned long hartid, uint32_t event_id);
 
+static bool event_supported_true(uint32_t event_id)
+{
+       return true;
+}
+
 static unsigned long sse_event_state(struct sbi_sse_event *e)
 {
        return e->attrs.status & SBI_SSE_ATTR_STATUS_STATE_MASK;
@@ -244,8 +265,42 @@ static void sse_event_set_state(struct sbi_sse_event *e,
        e->attrs.status |= new_state;
 }
 
+static struct sse_event_info *sse_event_info_get(uint32_t event_id)
+{
+       struct sse_event_info *info;
+       int i;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+               if (info->event_id == event_id)
+                       return info;
+       }
+
+       return NULL;
+}
+
+static int sse_event_info_is_supported(struct sse_event_info *info)
+{
+       if (!info)
+               return SBI_EINVAL;
+
+       if (!info->cb_ops || !info->cb_ops->is_supported_cb)
+               return SBI_ENOTSUPP;
+
+       return info->cb_ops->is_supported_cb(info->event_id) ? SBI_SUCCESS :
+                                                              SBI_ENOTSUPP;
+}
+
+static int sse_event_is_supported(uint32_t event_id)
+{
+       struct sse_event_info *info = sse_event_info_get(event_id);
+
+       return sse_event_info_is_supported(info);
+}
+
 static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
 {
+       int ret;
        unsigned int i;
        struct sbi_sse_event *e;
        struct sse_hart_state *shs;
@@ -264,7 +319,7 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        } else {
                shs = sse_thishart_state_ptr();
-               for (i = 0; i < local_event_count; i++) {
+               for (i = 0; i < shs->local_event_count; i++) {
                        e = &shs->local_events[i];
                        if (e->event_id == event_id) {
                                *eret = e;
@@ -273,7 +328,16 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        }
 
-       return SBI_EINVAL;
+       ret = sse_event_is_supported(event_id);
+       /*
+        * This should never happen since if an event is supported it should
+        * have been handled by the previous chunk of code. But better be safe
+        * than sorry and return EFAIL.
+        */
+       if (ret == SBI_SUCCESS)
+               return SBI_EFAIL;
+
+       return ret;
 }
 
 static void sse_event_put(struct sbi_sse_event *e)
@@ -869,18 +933,16 @@ int sbi_sse_inject_event(uint32_t event_id)
 
 int sbi_sse_set_cb_ops(uint32_t event_id, const struct sbi_sse_cb_ops *cb_ops)
 {
-       int ret;
-       struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
-       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+       info = sse_event_info_get(event_id);
+       if (!info)
                return SBI_EINVAL;
 
-       ret = sse_event_get(event_id, &e);
-       if (ret)
-               return ret;
+       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+               return SBI_EINVAL;
 
-       e->cb_ops = cb_ops;
-       sse_event_put(e);
+       info->cb_ops = cb_ops;
 
        return SBI_OK;
 }
@@ -1063,42 +1125,47 @@ int sbi_sse_unregister(uint32_t event_id)
        return ret;
 }
 
-static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)
+static void sse_event_init(struct sbi_sse_event *e, struct sse_event_info *info)
 {
-       e->event_id = event_id;
+       e->event_id = info->event_id;
+       e->info = info;
        e->hartindex = current_hartindex();
        e->attrs.hartid = current_hartid();
        /* Declare all events as injectable */
        e->attrs.status |= BIT(SBI_SSE_ATTR_STATUS_INJECT_OFFSET);
 }
 
-static void sse_event_count_init()
+static int sse_global_init(void)
 {
-       unsigned int i;
+       struct sse_event_info *info;
+       struct sbi_sse_event *e;
+       unsigned int i, ev = 0;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
-                       global_event_count++;
-               else
-                       local_event_count++;
+               info = &valid_events[i];
+
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               global_event_count++;
        }
-}
 
-static int sse_global_init()
-{
-       struct sbi_sse_event *e;
-       unsigned int i, ev = 0;
+       if (!global_event_count)
+               return 0;
 
        global_events = sbi_zalloc(sizeof(*global_events) * global_event_count);
        if (!global_events)
                return SBI_ENOMEM;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (!EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
                e = &global_events[ev].event;
-               sse_event_init(e, supported_events[i]);
+               sse_event_init(e, info);
                SPIN_LOCK_INIT(global_events[ev].lock);
 
                ev++;
@@ -1107,18 +1174,40 @@ static int sse_global_init()
        return 0;
 }
 
+static unsigned int sse_get_local_event_count(void)
+{
+       int i;
+       struct sse_event_info *info;
+       unsigned int local_event_count = 0;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               local_event_count++;
+       }
+
+       return local_event_count;
+}
+
 static void sse_local_init(struct sse_hart_state *shs)
 {
+       struct sse_event_info *info;
        unsigned int i, ev = 0;
 
        SBI_INIT_LIST_HEAD(&shs->enabled_event_list);
        SPIN_LOCK_INIT(shs->enabled_event_lock);
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
-               sse_event_init(&shs->local_events[ev++], supported_events[i]);
+               sse_event_init(&shs->local_events[ev++], info);
        }
 }
 
@@ -1128,10 +1217,9 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
        void *sse_inject_mem;
        struct sse_hart_state *shs;
        struct sbi_fifo *sse_inject_q;
+       unsigned int local_event_count = 0;
 
        if (cold_boot) {
-               sse_event_count_init();
-
                ret = sse_global_init();
                if (ret)
                        return ret;
@@ -1165,13 +1253,16 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
 
        shs = sse_get_hart_state_ptr(scratch);
        if (!shs) {
+               local_event_count = sse_get_local_event_count();
                /* Allocate per hart state and local events at once */
                shs = sbi_zalloc(sizeof(*shs) + sizeof(struct sbi_sse_event) *
                                                        local_event_count);
                if (!shs)
                        return SBI_ENOMEM;
 
-               shs->local_events = (struct sbi_sse_event *)(shs + 1);
+               shs->local_event_count = local_event_count;
+               if (local_event_count)
+                       shs->local_events = (struct sbi_sse_event *)(shs + 1);
 
                /* SSE events are masked until hart unmasks them */
                shs->masked = true;
@@ -1195,9 +1286,14 @@ void sbi_sse_exit(struct sbi_scratch *scratch)
 {
        int i;
        struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (sse_event_get(supported_events[i], &e))
+               info = &valid_events[i];
+               if (sse_event_info_is_supported(info) != SBI_SUCCESS)
+                       continue;
+
+               if (sse_event_get(info->event_id, &e))
                        continue;
 
                if (e->attrs.hartid != current_hartid())
--

2.45.2

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

This patch needs to be heavily re-worked.

Regards,
Anup

#9 lore raw

PATCH v3 3/5 lib: sbi: sse: rework event availability

Quoted 187 lines · Anup Patel
Quoted 177 lines · On Mon, Dec 16, 2024 at 2:55?PM Cl?ment L?ger <[email protected]> wrote:
Quoted 158 lines · Anup Patel
Quoted 95 lines · On Fri, Dec 13, 2024 at 2:04?AM Cl?ment L?ger <[email protected]> wrote:

In order to detect events that are available and supported, add a new is_supported_cb() callback and move cb_ops in event_info.

The cb_ops are now global for a specific event_id and can be filled by drivers before calling sse init. sse_init() will then the callback to determine which events are available. Global events are checked only once and local_events are checked per hart. Thanks to that, we can now allocate only the supported global and local events and defer sse probing. All events are assigned a default callback operations that reports true for the is_supported_cb() to keep the existing behavior. Since the callback operations can now be registered once for all harts, move the pmu sse_set_cb_ops() callback to the cold_boot 'if'.

Checking the new additional is_supported_cb() is only done when we fail from getting an event so that there is no performance cost.

lib: sbi: sse: add a is_supported_cb() callback

This callback will be used to expose the availability of events based on the presence of the dependencies (PMU Overflow IRQ for the PMU Overflow SSE event for instance). Drivers that want to handle an SSE event will need to register callbacks for the SSE event with at least the is_supported_cb() callback implemented. Now that this callback is implemented, set all non software events as not supported by default.

---
 include/sbi/sbi_sse.h |   5 ++
 lib/sbi/sbi_init.c    |  21 ++---
 lib/sbi/sbi_pmu.c     |   4 +-
 lib/sbi/sbi_sse.c     | 182 ++++++++++++++++++++++++++++++++----------
 4 files changed, 157 insertions(+), 55 deletions(-)
include/sbi/sbi_sse.h +5 -0
index 7419698a..1eab4f8b 100644
@@ -52,6 +52,11 @@ struct sbi_sse_cb_ops {
         * Called when the SBI_EXT_SSE_DISABLE is invoked on the event.
         */
        void (*disable_cb)(uint32_t event_id);
+
+       /**
+        * Called at init to check if an event is supported.
+        */
+       bool (*is_supported_cb)(uint32_t event_id);
 };
 
 /* Set the callback operations for an event
lib/sbi/sbi_init.c +7 -10
index 0736345d..b2627e60 100644
@@ -262,12 +262,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, true);
-       if (rc) {
-               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
-               sbi_hart_hang();
-       }
-
        rc = sbi_pmu_init(scratch, true);
        if (rc) {
                sbi_printf("%s: pmu init failed (error %d)\n",
@@ -317,6 +311,13 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_printf("%s: mpxy init failed (error %d)\n", __func__, rc);
                sbi_hart_hang();
        }
+
+       rc = sbi_sse_init(scratch, true);
+       if (rc) {
+               sbi_printf("%s: sse init failed (error %d)\n", __func__, rc);
+               sbi_hart_hang();
+       }
+
        /*
         * Note: Finalize domains after HSM initialization so that we
         * can startup non-root domains.
@@ -404,10 +405,6 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
-       rc = sbi_sse_init(scratch, false);
-       if (rc)
-               sbi_hart_hang();
-
        rc = sbi_pmu_init(scratch, false);
        if (rc)
                sbi_hart_hang();

lib/sbi/sbi_init.c static void __noreturn init_warm_startup(struct sbi_scratch *scratch, · line 443

Quoted 7 lines
lib/sbi/sbi_init.c +4 -0
@@ -440,6 +437,10 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
+       rc = sbi_sse_init(scratch, false);
+       if (rc)
+               sbi_hart_hang();
+

This re-ordering of sbi_sse_init() is not going to fly because in the future we might have SSE events being registered by drivers from sbi_platform_final_init().

Quoted 50 lines
lib/sbi/sbi_init.c +0 -0
        /*
         * Configure PMP at last because if SMEPMP is detected,
         * M-mode access to the S/U space will be rescinded.
lib/sbi/sbi_pmu.c +2 -2
index 1eab0629..182d8db9 100644
@@ -1165,9 +1165,9 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)
                        return SBI_EINVAL;
 
                total_ctrs = num_hw_ctrs + SBI_PMU_FW_CTR_MAX;
-       }
 
-       sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+               sbi_sse_set_cb_ops(SBI_SSE_EVENT_LOCAL_PMU, &pmu_sse_cb_ops);
+       }
 
        phs = pmu_get_hart_state_ptr(scratch);
        if (!phs) {
lib/sbi/sbi_sse.c +13 -8
index f710338e..f2a9d791 100644
@@ -39,21 +39,33 @@
 
 #define EVENT_IS_GLOBAL(__event_id) ((__event_id) & SBI_SSE_EVENT_GLOBAL_BIT)
 
-static const uint32_t supported_events[] = {
-       SBI_SSE_EVENT_LOCAL_RAS,
-       SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP,
-       SBI_SSE_EVENT_GLOBAL_RAS,
-       SBI_SSE_EVENT_LOCAL_PMU,
-       SBI_SSE_EVENT_LOCAL_SOFTWARE,
-       SBI_SSE_EVENT_GLOBAL_SOFTWARE,
+struct sse_event_info {
+       uint32_t event_id;
+       const struct sbi_sse_cb_ops *cb_ops;
 };
 
-#define EVENT_COUNT array_size(supported_events)
+static bool event_supported_true(uint32_t event_id);
+
+static const struct sbi_sse_cb_ops supported_event_ops = {
+       .is_supported_cb = &event_supported_true
+};
+
+static struct sse_event_info valid_events[] = {
+       {.event_id = SBI_SSE_EVENT_LOCAL_RAS, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_RAS, .cb_ops = &supported_event_ops},
Quoted 8 lines

lib/sbi/sbi_sse.c line 61

Quoted 5 lines
lib/sbi/sbi_sse.c +5 -0
+       {.event_id = SBI_SSE_EVENT_LOCAL_PMU, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_LOCAL_SOFTWARE, .cb_ops = &supported_event_ops},
+       {.event_id = SBI_SSE_EVENT_GLOBAL_SOFTWARE, .cb_ops = &supported_event_ops},
+};
+

This static table approach is not scalable and does not allow platform specific SSE events to be registered from drivers.

Agreed.

Quoted 4 lines

Initially you can start with a global linked list of supported SSE events which any one can register in the cold boot path.

Quoted 8 lines
Quoted 2 lines

The sbi_sse_init() should be simplified to not depend on supported SSE events to be pre-populated.

IMHO, all the events that will be handled by drivers should be available by the time sbi_sse_init() is called or the complexity will be deferred to some other functions as well. This call can even be moved after sbi_platform_final_init(). Except if you have another reason not to do so but that would be the best way to go.

Quoted 4 lines

Yes, its fine to move sbi_sse_init() after sbi_platform_final_init() but please add comments about why it has to be after sbi_platform_final_init(). We have already added similar comments for sbi_ecall_init().

Yes sure.

Quoted 13 lines
Quoted 12 lines

To clarify, I'll modify the current patch to use a linked list of events populated only with the GLOBAL_SOFTWARE_EVENT and LOCAL_SOFTWARE_EVENT at compile time. This list can be extended at boot time by registering cb_ops for events with sbi_sse_set_cb_ops() from drivers. sbi_sse_init() will now allocate space for all events that are in the linked list of events (no matter if they are supported or not). Event availability will be then tested using the is_supported() callback and stored in the event struct itself. Finally, sbi_sse_init() will be moved after all other driver init (ie after sbi_platform_final_init()). If you envision something else, please detail it.

Quoted 1 line

I think this should be fine.

Ok I'll give it a try.

Thanks,

Cl?ment

Quoted 324 lines

Regards,
Anup

Quoted 320 lines

Thanks,

Cl?ment

Quoted 314 lines
Quoted 305 lines
lib/sbi/sbi_sse.c +121 -36
+#define EVENT_COUNT array_size(valid_events)
 
 #define sse_event_invoke_cb(_event, _cb, ...)                                 \
        {                                                                     \
-               if (_event->cb_ops && _event->cb_ops->_cb)                    \
-                       _event->cb_ops->_cb(_event->event_id, ##__VA_ARGS__); \
+               const struct sbi_sse_cb_ops *__ops = _event->info->cb_ops;    \
+               if (__ops && __ops->_cb)                                          \
+                       __ops->_cb(_event->event_id, ##__VA_ARGS__);            \
        }
 
 struct sse_entry_state {
@@ -110,7 +122,7 @@ struct sbi_sse_event {
        struct sbi_sse_event_attrs attrs;
        uint32_t event_id;
        u32 hartindex;
-       const struct sbi_sse_cb_ops *cb_ops;
+       struct sse_event_info *info;
        struct sbi_dlist node;
 };
 
@@ -144,6 +156,11 @@ struct sse_hart_state {
         */
        struct sbi_sse_event *local_events;
 
+       /**
+        * Number of events in the local_events array.
+        */
+       unsigned int local_event_count;
+
        /**
         * State to track if the hart is ready to take sse events.
         * One hart cannot modify this state of another hart.
@@ -167,7 +184,6 @@ struct sse_global_event {
        spinlock_t lock;
 };
 
-static unsigned int local_event_count;
 static unsigned int global_event_count;
 static struct sse_global_event *global_events;
 
@@ -180,6 +196,11 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;
 
 static int sse_ipi_inject_send(unsigned long hartid, uint32_t event_id);
 
+static bool event_supported_true(uint32_t event_id)
+{
+       return true;
+}
+
 static unsigned long sse_event_state(struct sbi_sse_event *e)
 {
        return e->attrs.status & SBI_SSE_ATTR_STATUS_STATE_MASK;
@@ -244,8 +265,42 @@ static void sse_event_set_state(struct sbi_sse_event *e,
        e->attrs.status |= new_state;
 }
 
+static struct sse_event_info *sse_event_info_get(uint32_t event_id)
+{
+       struct sse_event_info *info;
+       int i;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+               if (info->event_id == event_id)
+                       return info;
+       }
+
+       return NULL;
+}
+
+static int sse_event_info_is_supported(struct sse_event_info *info)
+{
+       if (!info)
+               return SBI_EINVAL;
+
+       if (!info->cb_ops || !info->cb_ops->is_supported_cb)
+               return SBI_ENOTSUPP;
+
+       return info->cb_ops->is_supported_cb(info->event_id) ? SBI_SUCCESS :
+                                                              SBI_ENOTSUPP;
+}
+
+static int sse_event_is_supported(uint32_t event_id)
+{
+       struct sse_event_info *info = sse_event_info_get(event_id);
+
+       return sse_event_info_is_supported(info);
+}
+
 static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
 {
+       int ret;
        unsigned int i;
        struct sbi_sse_event *e;
        struct sse_hart_state *shs;
@@ -264,7 +319,7 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        } else {
                shs = sse_thishart_state_ptr();
-               for (i = 0; i < local_event_count; i++) {
+               for (i = 0; i < shs->local_event_count; i++) {
                        e = &shs->local_events[i];
                        if (e->event_id == event_id) {
                                *eret = e;
@@ -273,7 +328,16 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret)
                }
        }
 
-       return SBI_EINVAL;
+       ret = sse_event_is_supported(event_id);
+       /*
+        * This should never happen since if an event is supported it should
+        * have been handled by the previous chunk of code. But better be safe
+        * than sorry and return EFAIL.
+        */
+       if (ret == SBI_SUCCESS)
+               return SBI_EFAIL;
+
+       return ret;
 }
 
 static void sse_event_put(struct sbi_sse_event *e)
@@ -869,18 +933,16 @@ int sbi_sse_inject_event(uint32_t event_id)
 
 int sbi_sse_set_cb_ops(uint32_t event_id, const struct sbi_sse_cb_ops *cb_ops)
 {
-       int ret;
-       struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
-       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+       info = sse_event_info_get(event_id);
+       if (!info)
                return SBI_EINVAL;
 
-       ret = sse_event_get(event_id, &e);
-       if (ret)
-               return ret;
+       if (cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL(event_id))
+               return SBI_EINVAL;
 
-       e->cb_ops = cb_ops;
-       sse_event_put(e);
+       info->cb_ops = cb_ops;
 
        return SBI_OK;
 }
@@ -1063,42 +1125,47 @@ int sbi_sse_unregister(uint32_t event_id)
        return ret;
 }
 
-static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)
+static void sse_event_init(struct sbi_sse_event *e, struct sse_event_info *info)
 {
-       e->event_id = event_id;
+       e->event_id = info->event_id;
+       e->info = info;
        e->hartindex = current_hartindex();
        e->attrs.hartid = current_hartid();
        /* Declare all events as injectable */
        e->attrs.status |= BIT(SBI_SSE_ATTR_STATUS_INJECT_OFFSET);
 }
 
-static void sse_event_count_init()
+static int sse_global_init(void)
 {
-       unsigned int i;
+       struct sse_event_info *info;
+       struct sbi_sse_event *e;
+       unsigned int i, ev = 0;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
-                       global_event_count++;
-               else
-                       local_event_count++;
+               info = &valid_events[i];
+
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               global_event_count++;
        }
-}
 
-static int sse_global_init()
-{
-       struct sbi_sse_event *e;
-       unsigned int i, ev = 0;
+       if (!global_event_count)
+               return 0;
 
        global_events = sbi_zalloc(sizeof(*global_events) * global_event_count);
        if (!global_events)
                return SBI_ENOMEM;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (!EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (!EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
                e = &global_events[ev].event;
-               sse_event_init(e, supported_events[i]);
+               sse_event_init(e, info);
                SPIN_LOCK_INIT(global_events[ev].lock);
 
                ev++;
@@ -1107,18 +1174,40 @@ static int sse_global_init()
        return 0;
 }
 
+static unsigned int sse_get_local_event_count(void)
+{
+       int i;
+       struct sse_event_info *info;
+       unsigned int local_event_count = 0;
+
+       for (i = 0; i < EVENT_COUNT; i++) {
+               info = &valid_events[i];
+
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
+                       continue;
+
+               local_event_count++;
+       }
+
+       return local_event_count;
+}
+
 static void sse_local_init(struct sse_hart_state *shs)
 {
+       struct sse_event_info *info;
        unsigned int i, ev = 0;
 
        SBI_INIT_LIST_HEAD(&shs->enabled_event_list);
        SPIN_LOCK_INIT(shs->enabled_event_lock);
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (EVENT_IS_GLOBAL(supported_events[i]))
+               info = &valid_events[i];
+               if (EVENT_IS_GLOBAL(info->event_id) ||
+                   sse_event_is_supported(info->event_id) != SBI_SUCCESS)
                        continue;
 
-               sse_event_init(&shs->local_events[ev++], supported_events[i]);
+               sse_event_init(&shs->local_events[ev++], info);
        }
 }
 
@@ -1128,10 +1217,9 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
        void *sse_inject_mem;
        struct sse_hart_state *shs;
        struct sbi_fifo *sse_inject_q;
+       unsigned int local_event_count = 0;
 
        if (cold_boot) {
-               sse_event_count_init();
-
                ret = sse_global_init();
                if (ret)
                        return ret;
@@ -1165,13 +1253,16 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)
 
        shs = sse_get_hart_state_ptr(scratch);
        if (!shs) {
+               local_event_count = sse_get_local_event_count();
                /* Allocate per hart state and local events at once */
                shs = sbi_zalloc(sizeof(*shs) + sizeof(struct sbi_sse_event) *
                                                        local_event_count);
                if (!shs)
                        return SBI_ENOMEM;
 
-               shs->local_events = (struct sbi_sse_event *)(shs + 1);
+               shs->local_event_count = local_event_count;
+               if (local_event_count)
+                       shs->local_events = (struct sbi_sse_event *)(shs + 1);
 
                /* SSE events are masked until hart unmasks them */
                shs->masked = true;
@@ -1195,9 +1286,14 @@ void sbi_sse_exit(struct sbi_scratch *scratch)
 {
        int i;
        struct sbi_sse_event *e;
+       struct sse_event_info *info;
 
        for (i = 0; i < EVENT_COUNT; i++) {
-               if (sse_event_get(supported_events[i], &e))
+               info = &valid_events[i];
+               if (sse_event_info_is_supported(info) != SBI_SUCCESS)
+                       continue;
+
+               if (sse_event_get(info->event_id, &e))
                        continue;
 
                if (e->attrs.hartid != current_hartid())
--

2.45.2

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

This patch needs to be heavily re-worked.

Regards,
Anup

#10 lore raw

PATCH v3 4/5 lib: sbi: pmu: add an sse .is_supported_cb callback implementation

In reply to Clément Léger

Since the PMU overflow event can only be generated if a PMU overflow irq is implemented, add a pmu_sse_is_supported() function to check for the PMU irq presence.

---
 lib/sbi/sbi_pmu.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/sbi/sbi_pmu.c +9 -0
index 182d8db9..dae587ab 100644
@@ -1122,7 +1122,16 @@ static void pmu_sse_complete(uint32_t event_id)
 	csr_set(CSR_MIE, sbi_pmu_irq_mask());
 }
 
+static bool pmu_sse_is_supported(uint32_t event_id)
+{
+	if (sbi_pmu_irq_bit() < 0)
+		return false;
+
+	return true;
+}
+
 static const struct sbi_sse_cb_ops pmu_sse_cb_ops = {
+	.is_supported_cb = pmu_sse_is_supported,
 	.enable_cb = pmu_sse_enable,
 	.disable_cb = pmu_sse_disable,
 	.complete_cb = pmu_sse_complete,
Signature · 1 line
2.45.2
#11 lore raw

PATCH v3 5/5 lib: sbi: sse: set non-software events as unsupported

In reply to Clément Léger

Now that a callback was added to check if events are supported, set all non software events as unsupported by default. Drivers that wants to enable them will need to register an is_supproted_cb() SSE callback.

---
 lib/sbi/sbi_sse.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/sbi/sbi_sse.c +4 -4
index f2a9d791..4ec12052 100644
@@ -51,10 +51,10 @@ static const struct sbi_sse_cb_ops supported_event_ops = {
 };
 
 static struct sse_event_info valid_events[] = {
-	{.event_id = SBI_SSE_EVENT_LOCAL_RAS, .cb_ops = &supported_event_ops},
-	{.event_id = SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, .cb_ops = &supported_event_ops},
-	{.event_id = SBI_SSE_EVENT_GLOBAL_RAS, .cb_ops = &supported_event_ops},
-	{.event_id = SBI_SSE_EVENT_LOCAL_PMU, .cb_ops = &supported_event_ops},
+	{.event_id = SBI_SSE_EVENT_LOCAL_RAS, .cb_ops = NULL},
+	{.event_id = SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, .cb_ops = NULL},
+	{.event_id = SBI_SSE_EVENT_GLOBAL_RAS, .cb_ops = NULL},
+	{.event_id = SBI_SSE_EVENT_LOCAL_PMU, .cb_ops = NULL},
 	{.event_id = SBI_SSE_EVENT_LOCAL_SOFTWARE, .cb_ops = &supported_event_ops},
 	{.event_id = SBI_SSE_EVENT_GLOBAL_SOFTWARE, .cb_ops = &supported_event_ops},
 };
Signature · 1 line
2.45.2