lib: sbi: sse: rework event availability
v4 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 V4:
- Used unsigned long for pmu_rq_bit_mask()
- Moved pmu irq check in default case rather than hot path
- Changed the supported event array to be a list, allowing for drivers to register additional handled events.
- Check for supported events at init time.
- Remove the need for is_supported callback
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: allow adding new events
lib: sbi: sse: return SBI_ENOTSUPP for unsupported events
lib: sbi: pmu: add the PMU SSE event only if overflow IRQ is supported
include/sbi/sbi_pmu.h | 3 + include/sbi/sbi_sse.h | 8 +- lib/sbi/sbi_hart.c | 2 +- lib/sbi/sbi_init.c | 24 +- lib/sbi/sbi_pmu.c | 45 ++-- lib/sbi/sbi_sse.c | 211 ++++++++++++------ lib/sbi/sbi_trap.c | 15 +- .../generic/include/thead/c9xx_encoding.h | 1 - platform/generic/thead/thead_c9xx_pmu.c | 2 +- 9 files changed, 201 insertions(+), 110 deletions(-)
Signature · 1 line
2.45.2
PATCH v4 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/sbi_pmu.h | 3 ++ lib/sbi/sbi_hart.c | 2 +- lib/sbi/sbi_pmu.c | 42 ++++++++++++------- lib/sbi/sbi_trap.c | 15 ++++--- .../generic/include/thead/c9xx_encoding.h | 1 - platform/generic/thead/thead_c9xx_pmu.c | 2 +- 6 files changed, 41 insertions(+), 24 deletions(-)
include/sbi/sbi_pmu.h +3 -0
@@ -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
@@ -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 +9 -6
@@ -239,12 +239,13 @@ 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 : +if (irq ==sbi_pmu_irq_bit ()) { +sbi_pmu_ovf_irq (); +return 0 ; + }return SBI_ENOENT ; }
@@ -265,15 +266,17 @@ 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)return rc;break ;default : +if (mtopi ==sbi_pmu_irq_bit ()) { +sbi_pmu_ovf_irq (); +break ; + } +return SBI_ENOENT ; } }
platform/generic/include/thead/c9xx_encoding.h +0 -1
@@ -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
@@ -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
PATCH v4 1/5 lib: sbi: pmu: fix usage of sbi_pmu_irq_bit()
Quoted 2 lines · Cl?ment L?ger
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
typo: lead -> led
Quoted 7 lines
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.Signed-off-by Cl?ment L?ger <[email protected]>
Quoted 8 lines
--- include/sbi/sbi_pmu.h | 3 ++ lib/sbi/sbi_hart.c | 2 +- lib/sbi/sbi_pmu.c | 42 ++++++++++++------- lib/sbi/sbi_trap.c | 15 ++++--- .../generic/include/thead/c9xx_encoding.h | 1 - platform/generic/thead/thead_c9xx_pmu.c | 2 +- 6 files changed, 41 insertions(+), 24 deletions(-)
Looks great, thanks!
PATCH v4 1/5 lib: sbi: pmu: fix usage of sbi_pmu_irq_bit()
In reply to Clément LégerQuoted 203 lines · On Mon, Dec 16, 2024 at 1:22?PM 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 ofsbi_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.Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/sbi_pmu.h | 3 ++ lib/sbi/sbi_hart.c | 2 +- lib/sbi/sbi_pmu.c | 42 ++++++++++++------- lib/sbi/sbi_trap.c | 15 ++++--- .../generic/include/thead/c9xx_encoding.h | 1 - platform/generic/thead/thead_c9xx_pmu.c | 2 +- 6 files changed, 41 insertions(+), 24 deletions(-)include/sbi/sbi_pmu.h +3
-0@@ -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@@ -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@@ -309,10 +309,10 @@ int sbi_pmu_add_raw_event_counter_map(uint64_t select, uint64_t select_mask, u32void 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; }@@ -1087,26 +1097,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 (); +unsigned long irq_mask =sbi_pmu_irq_mask (); phs->sse_enabled = true; -csr_clear (CSR_MIDELEG ,sbi_pmu_irq_bit ()); -csr_clear (CSR_MIP ,MIP_LCOFIP ); -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 (); +unsigned long irq_mask =sbi_pmu_irq_mask (); -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 +9
-6@@ -239,12 +239,13 @@ 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 : +if (irq ==sbi_pmu_irq_bit ()) { +sbi_pmu_ovf_irq (); +return 0 ; + }return SBI_ENOENT ; }@@ -265,15 +266,17 @@ 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)return rc;break ;default : +if (mtopi ==sbi_pmu_irq_bit ()) { +sbi_pmu_ovf_irq (); +break ; + } +return SBI_ENOENT ; } }platform/generic/include/thead/c9xx_encoding.h +0
-1@@ -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 +0
-0
platform/generic/thead/thead_c9xx_pmu.c static void thead_c9xx_pmu_ctr_disable_irq(uint32_t ctr_idx) · line 58
Quoted 7 lines
platform/generic/thead/thead_c9xx_pmu.c +1
-1@@ -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 ; }
Quoted 4 lines
platform/generic/thead/thead_c9xx_pmu.c +0
-1static const struct sbi_pmu_device thead_c9xx_pmu_device = { --2.45.2
PATCH v4 2/5 lib: sbi: sse: return an error value from sse_event_get()
In reply to Clément LégerSince 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
@@ -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
PATCH v4 2/5 lib: sbi: sse: return an error value from sse_event_get()
Quoted 189 lines · On Mon, Dec 16, 2024 at 1:22?PM 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 reportSBI_ERR_NOT_SUPPORTEDwhen an event isn't supported.Signed-off-by Cl?ment L?ger <[email protected]>Reviewed-by Samuel Holland <[email protected]>--- lib/sbi/sbi_sse.c | 72 +++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 34 deletions(-)lib/sbi/sbi_sse.c +38
-35@@ -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 ()) --
Quoted 1 line
2.45.2
Quoted 1 line
PATCH v4 3/5 lib: sbi: sse: allow adding new events
In reply to Clément LégerIn order to allow events to be dynamically added, remove the existing static array of events and use a simply linked list of supported events. This allows us to move the cb_ops into this list and associated it with an event_id. Drivers can now register cb_ops before bringing up the sse core to handle additional events (platform ones for instance).
sbi_sse_init() now allocates as many events as present in the linked list. Events can now be added with sbi_sse_add_event() which allows to add new supported events with some callback operations if any. If an event is not to be supported, then sbi_sse_add_event() should not be called. This approach currently consider that local events are to be supported on all harts (ie, they all support the same ISA or dependencies). If per-hart event availability needs to be supported, then, an is_supported() callback could be added later and called for each hart.
--- include/sbi/sbi_sse.h | 8 +-- lib/sbi/sbi_init.c | 24 +++++---- lib/sbi/sbi_pmu.c | 2 +- lib/sbi/sbi_sse.c | 121 +++++++++++++++++++++++++++--------------- 4 files changed, 97 insertions(+), 58 deletions(-)
include/sbi/sbi_sse.h +4 -4
@@ -54,12 +54,12 @@ struct sbi_sse_cb_ops {void (*disable_cb)(uint32_t event_id); }; -/* Set the callback operations for an event -* @param event_id Event identifier (SBI_SSE_EVENT_*) -* @param cb_ops Callback operations +/* Add a supported event with associated callback operations +* @param event_id Event identifier (SBI_SSE_EVENT_* or a custom platform one) +* @param cb_ops Callback operations (Can be NULL if any) * @return 0 on success, error otherwise */ -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops); +int sbi_sse_add_event (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops);/* Inject an event to the current hard * @param event_id Event identifier (SBI_SSE_EVENT_*)
lib/sbi/sbi_init.c +14 -10
@@ -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",
@@ -342,6 +336,16 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang (); } +/* +* Note: SSE events callbacks can be registered by other drivers so +* sbi_sse_init() needs to be called after all drivers have been probed. +*/ + rc =sbi_sse_init (scratch, true); +if (rc) { +sbi_printf ("%s: sse init failed (error %d)\n" , __func__, rc); +sbi_hart_hang (); + } +/* * Note: Ecall initialization should be after platform final * initialization so that all available platform devices are
@@ -404,10 +408,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 +440,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 +1 -1
@@ -1166,7 +1166,7 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) 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_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); phs =pmu_get_hart_state_ptr (scratch);if (!phs) {
lib/sbi/sbi_sse.c +78 -43
223 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v4 3/5 lib: sbi: sse: allow adding new events
Quoted 130 lines · Cl?ment L?ger
In order to allow events to be dynamically added, remove the existing static array of events and use a simply linked list of supported events. This allows us to move the
cb_opsinto this list and associated it with anevent_id. Drivers can now registercb_opsbefore bringing up the sse core to handle additional events (platform ones for instance).
sbi_sse_init()now allocates as many events as present in the linked list. Events can now be added withsbi_sse_add_event()which allows to add new supported events with some callback operations if any. If an event is not to be supported, thensbi_sse_add_event()should not be called. This approach currently consider that local events are to be supported on all harts (ie, they all support the same ISA or dependencies). If per-hart event availability needs to be supported, then, anis_supported()callback could be added later and called for each hart.Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/sbi_sse.h | 8 +-- lib/sbi/sbi_init.c | 24 +++++---- lib/sbi/sbi_pmu.c | 2 +- lib/sbi/sbi_sse.c | 121 +++++++++++++++++++++++++++--------------- 4 files changed, 97 insertions(+), 58 deletions(-)include/sbi/sbi_sse.h +4
-4@@ -54,12 +54,12 @@ struct sbi_sse_cb_ops {void (*disable_cb)(uint32_t event_id); }; -/* Set the callback operations for an event -* @param event_id Event identifier (SBI_SSE_EVENT_*) -* @param cb_ops Callback operations +/* Add a supported event with associated callback operations +* @param event_id Event identifier (SBI_SSE_EVENT_* or a custom platform one) +* @param cb_ops Callback operations (Can be NULL if any) * @return 0 on success, error otherwise */ -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops); +int sbi_sse_add_event (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops);/* Inject an event to the current hard * @param event_id Event identifier (SBI_SSE_EVENT_*) lib/sbi/sbi_init.c +14
-10@@ -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",@@ -342,6 +336,16 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang (); } +/* +* Note: SSE events callbacks can be registered by other drivers so +* sbi_sse_init() needs to be called after all drivers have been probed. +*/ + rc =sbi_sse_init (scratch, true); +if (rc) { +sbi_printf ("%s: sse init failed (error %d)\n" , __func__, rc); +sbi_hart_hang (); + } +/* * Note: Ecall initialization should be after platform final * initialization so that all available platform devices are @@ -404,10 +408,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 +440,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 +1
-1@@ -1166,7 +1166,7 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) 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_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); phs =pmu_get_hart_state_ptr (scratch);if (!phs) {lib/sbi/sbi_sse.c +0
-8@@ -39,21 +39,11 @@#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 , -};
lib/sbi/sbi_sse.c line 46
Quoted 10 lines
lib/sbi/sbi_sse.c +3
-5- -#define EVENT_COUNT array_size(supported_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__); \
As maybe a future change, we could reduce pointer chasing here by putting the callback function pointers directly in struct sse_event_info, and pass that to
sbi_sse_add_event().
Quoted 13 lines
lib/sbi/sbi_sse.c +1
-1}struct sse_entry_state {@@ -110,7 +100,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; };
lib/sbi/sbi_sse.c struct sse_global_event { · line 159
Quoted 4 lines
lib/sbi/sbi_sse.c +1
-0@@ -166,6 +156,11 @@ struct sse_global_event {*/ spinlock_t lock; }; +struct sse_event_info {
nit: blank line above here.
Quoted 196 lines
lib/sbi/sbi_sse.c +73
-29+uint32_t event_id; +const struct sbi_sse_cb_ops *cb_ops; +struct sse_event_info *next; +};static unsigned int local_event_count;static unsigned int global_event_count;@@ -180,6 +175,30 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;static int sse_ipi_inject_send (unsigned long hartid,uint32_t event_id); +struct sse_event_info global_software_event = { + .event_id =SBI_SSE_EVENT_GLOBAL_SOFTWARE , + .next =NULL , +}; + +struct sse_event_info local_software_event = { + .event_id =SBI_SSE_EVENT_LOCAL_SOFTWARE , + .next = &global_software_event, +}; + +static struct sse_event_info *supported_events = &local_software_event; + +static struct sse_event_info *sse_event_info_get (uint32_t event_id) +{ +struct sse_event_info *info; + +for (info = supported_events; info; info = info->next) { +if (info->event_id == event_id) +return info; + } + +return NULL ; +} +static unsigned long sse_event_state (struct sbi_sse_event *e) {return e->attrs.status &SBI_SSE_ATTR_STATUS_STATE_MASK ;@@ -867,20 +886,31 @@ int sbi_sse_inject_event(uint32_t event_id)return sse_inject_event (event_id,current_hartid ()); } -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops) +int sbi_sse_add_event (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)) +/* Do not allow adding an event twice */ + 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 && cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL (event_id)) +return SBI_EINVAL ; - e->cb_ops = cb_ops; -sse_event_put (e); + info =sbi_zalloc (sizeof (*info)); +if (!info) +return SBI_ENOMEM ; + + info->cb_ops = cb_ops; + info->event_id = event_id; + +/* +* Add the new event at the head of the linked list of supported +* events. +*/ + info->next = supported_events; + supported_events = info;return SBI_OK ; }@@ -1063,9 +1093,10 @@ 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 */ @@ -1074,10 +1105,10 @@ static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)static void sse_event_count_init () { -unsigned int i; +struct sse_event_info *info; -for (i =0 ; i <EVENT_COUNT ; i++) { -if (EVENT_IS_GLOBAL (supported_events[i])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id)) global_event_count++;else local_event_count++;@@ -1087,18 +1118,19 @@ static void sse_event_count_init()static int sse_global_init () {struct sbi_sse_event *e; -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info; 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])) +for (info = supported_events; info; info = info->next) { +if (!EVENT_IS_GLOBAL (info->event_id))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++;@@ -1109,16 +1141,16 @@ static int sse_global_init()static void sse_local_init (struct sse_hart_state *shs) { -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info;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])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id))continue ; - -sse_event_init (&shs->local_events[ev++], supported_events[i]); +sse_event_init (&shs->local_events[ev++], info); } }@@ -1148,7 +1180,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) } sse_inject_fifo_mem_off =sbi_scratch_alloc_offset ( -EVENT_COUNT *sizeof (struct sse_ipi_inject_data )); + (global_event_count + local_event_count) * +sizeof (struct sse_ipi_inject_data ));if (!sse_inject_fifo_mem_off) {sbi_scratch_free_offset (sse_inject_fifo_off);sbi_scratch_free_offset (shs_ptr_off);@@ -1185,7 +1218,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) sse_inject_mem =sbi_scratch_offset_ptr (scratch, sse_inject_fifo_mem_off); -sbi_fifo_init (sse_inject_q, sse_inject_mem,EVENT_COUNT , +sbi_fifo_init (sse_inject_q, sse_inject_mem, + (global_event_count + local_event_count),sizeof (struct sse_ipi_inject_data ));return 0 ;@@ -1193,18 +1227,19 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)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)) +for (info = supported_events; info; info = info->next) { +if (sse_event_get (info->event_id, &e))continue ;if (e->attrs.hartid !=current_hartid ()) goto skip;if (sse_event_state (e) >SBI_SSE_STATE_REGISTERED ) { -sbi_printf ("Event %d in invalid state at exit" , i); +sbi_printf ("Event %d in invalid state at exit" , + info->event_id);sse_event_set_state (e,SBI_SSE_STATE_UNUSED ); }
PATCH v4 3/5 lib: sbi: sse: allow adding new events
Quoted 137 lines · Samuel Holland
Quoted 136 lines · Cl?ment L?ger
In order to allow events to be dynamically added, remove the existing static array of events and use a simply linked list of supported events. This allows us to move the
cb_opsinto this list and associated it with anevent_id. Drivers can now registercb_opsbefore bringing up the sse core to handle additional events (platform ones for instance).
sbi_sse_init()now allocates as many events as present in the linked list. Events can now be added withsbi_sse_add_event()which allows to add new supported events with some callback operations if any. If an event is not to be supported, thensbi_sse_add_event()should not be called. This approach currently consider that local events are to be supported on all harts (ie, they all support the same ISA or dependencies). If per-hart event availability needs to be supported, then, anis_supported()callback could be added later and called for each hart.Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/sbi_sse.h | 8 +-- lib/sbi/sbi_init.c | 24 +++++---- lib/sbi/sbi_pmu.c | 2 +- lib/sbi/sbi_sse.c | 121 +++++++++++++++++++++++++++--------------- 4 files changed, 97 insertions(+), 58 deletions(-)include/sbi/sbi_sse.h +4
-4@@ -54,12 +54,12 @@ struct sbi_sse_cb_ops {void (*disable_cb)(uint32_t event_id); }; -/* Set the callback operations for an event -* @param event_id Event identifier (SBI_SSE_EVENT_*) -* @param cb_ops Callback operations +/* Add a supported event with associated callback operations +* @param event_id Event identifier (SBI_SSE_EVENT_* or a custom platform one) +* @param cb_ops Callback operations (Can be NULL if any) * @return 0 on success, error otherwise */ -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops); +int sbi_sse_add_event (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops);/* Inject an event to the current hard * @param event_id Event identifier (SBI_SSE_EVENT_*) lib/sbi/sbi_init.c +14
-10@@ -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",@@ -342,6 +336,16 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang (); } +/* +* Note: SSE events callbacks can be registered by other drivers so +* sbi_sse_init() needs to be called after all drivers have been probed. +*/ + rc =sbi_sse_init (scratch, true); +if (rc) { +sbi_printf ("%s: sse init failed (error %d)\n" , __func__, rc); +sbi_hart_hang (); + } +/* * Note: Ecall initialization should be after platform final * initialization so that all available platform devices are @@ -404,10 +408,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 +440,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 +1
-1@@ -1166,7 +1166,7 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) 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_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); phs =pmu_get_hart_state_ptr (scratch);if (!phs) {lib/sbi/sbi_sse.c +0
-12@@ -39,21 +39,11 @@#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 , -}; - -#define EVENT_COUNT array_size(supported_events) -#define sse_event_invoke_cb (_event, _cb, ...) \ { \ - if (_event->cb_ops && _event->cb_ops->_cb) \
Quoted 8 lines
lib/sbi/sbi_sse.c line 46
Quoted 4 lines
lib/sbi/sbi_sse.c +3
-1- _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__); \As maybe a future change, we could reduce pointer chasing here by putting the callback function pointers directly in
struct sse_event_info, and pass that to
sbi_sse_add_event().
That seems like a good idea but one major drawback I could see is the memory usage if you have a large number of events using the same callback operations. This could be the case for RAS errors for instance. Some implementation might decide to have many RAS events but use the same callback implementation.
Quoted 13 lines
Quoted 13 lines
lib/sbi/sbi_sse.c +1
-1}struct sse_entry_state {@@ -110,7 +100,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; };
Quoted 7 lines
lib/sbi/sbi_sse.c struct sse_global_event { · line 159
Quoted 5 lines
lib/sbi/sbi_sse.c +1
-0@@ -166,6 +156,11 @@ struct sse_global_event {*/ spinlock_t lock; }; +struct sse_event_info {nit: blank line above here.
I'll send a v5 anyway so I'll fix that.
Thanks,
Cl?ment
Quoted 200 lines
Reviewed-by Samuel Holland <[email protected]>Quoted 196 lines
lib/sbi/sbi_sse.c +73
-29+uint32_t event_id; +const struct sbi_sse_cb_ops *cb_ops; +struct sse_event_info *next; +};static unsigned int local_event_count;static unsigned int global_event_count;@@ -180,6 +175,30 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;static int sse_ipi_inject_send (unsigned long hartid,uint32_t event_id); +struct sse_event_info global_software_event = { + .event_id =SBI_SSE_EVENT_GLOBAL_SOFTWARE , + .next =NULL , +}; + +struct sse_event_info local_software_event = { + .event_id =SBI_SSE_EVENT_LOCAL_SOFTWARE , + .next = &global_software_event, +}; + +static struct sse_event_info *supported_events = &local_software_event; + +static struct sse_event_info *sse_event_info_get (uint32_t event_id) +{ +struct sse_event_info *info; + +for (info = supported_events; info; info = info->next) { +if (info->event_id == event_id) +return info; + } + +return NULL ; +} +static unsigned long sse_event_state (struct sbi_sse_event *e) {return e->attrs.status &SBI_SSE_ATTR_STATUS_STATE_MASK ;@@ -867,20 +886,31 @@ int sbi_sse_inject_event(uint32_t event_id)return sse_inject_event (event_id,current_hartid ()); } -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops) +int sbi_sse_add_event (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)) +/* Do not allow adding an event twice */ + 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 && cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL (event_id)) +return SBI_EINVAL ; - e->cb_ops = cb_ops; -sse_event_put (e); + info =sbi_zalloc (sizeof (*info)); +if (!info) +return SBI_ENOMEM ; + + info->cb_ops = cb_ops; + info->event_id = event_id; + +/* +* Add the new event at the head of the linked list of supported +* events. +*/ + info->next = supported_events; + supported_events = info;return SBI_OK ; }@@ -1063,9 +1093,10 @@ 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 */ @@ -1074,10 +1105,10 @@ static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)static void sse_event_count_init () { -unsigned int i; +struct sse_event_info *info; -for (i =0 ; i <EVENT_COUNT ; i++) { -if (EVENT_IS_GLOBAL (supported_events[i])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id)) global_event_count++;else local_event_count++;@@ -1087,18 +1118,19 @@ static void sse_event_count_init()static int sse_global_init () {struct sbi_sse_event *e; -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info; 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])) +for (info = supported_events; info; info = info->next) { +if (!EVENT_IS_GLOBAL (info->event_id))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++;@@ -1109,16 +1141,16 @@ static int sse_global_init()static void sse_local_init (struct sse_hart_state *shs) { -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info;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])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id))continue ; - -sse_event_init (&shs->local_events[ev++], supported_events[i]); +sse_event_init (&shs->local_events[ev++], info); } }@@ -1148,7 +1180,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) } sse_inject_fifo_mem_off =sbi_scratch_alloc_offset ( -EVENT_COUNT *sizeof (struct sse_ipi_inject_data )); + (global_event_count + local_event_count) * +sizeof (struct sse_ipi_inject_data ));if (!sse_inject_fifo_mem_off) {sbi_scratch_free_offset (sse_inject_fifo_off);sbi_scratch_free_offset (shs_ptr_off);@@ -1185,7 +1218,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) sse_inject_mem =sbi_scratch_offset_ptr (scratch, sse_inject_fifo_mem_off); -sbi_fifo_init (sse_inject_q, sse_inject_mem,EVENT_COUNT , +sbi_fifo_init (sse_inject_q, sse_inject_mem, + (global_event_count + local_event_count),sizeof (struct sse_ipi_inject_data ));return 0 ;@@ -1193,18 +1227,19 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)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)) +for (info = supported_events; info; info = info->next) { +if (sse_event_get (info->event_id, &e))continue ;if (e->attrs.hartid !=current_hartid ()) goto skip;if (sse_event_state (e) >SBI_SSE_STATE_REGISTERED ) { -sbi_printf ("Event %d in invalid state at exit" , i); +sbi_printf ("Event %d in invalid state at exit" , + info->event_id);sse_event_set_state (e,SBI_SSE_STATE_UNUSED ); }
PATCH v4 3/5 lib: sbi: sse: allow adding new events
In reply to Clément LégerQuoted 154 lines · On Tue, Dec 17, 2024 at 2:52?AM Cl?ment L?ger <[email protected]> wrote:
In order to allow events to be dynamically added, remove the existing static array of events and use a simply linked list of supported events. This allows us to move the
cb_opsinto this list and associated it with anevent_id. Drivers can now registercb_opsbefore bringing up the sse core to handle additional events (platform ones for instance).
sbi_sse_init()now allocates as many events as present in the linked list. Events can now be added withsbi_sse_add_event()which allows to add new supported events with some callback operations if any. If an event is not to be supported, thensbi_sse_add_event()should not be called. This approach currently consider that local events are to be supported on all harts (ie, they all support the same ISA or dependencies). If per-hart event availability needs to be supported, then, anis_supported()callback could be added later and called for each hart.Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/sbi_sse.h | 8 +-- lib/sbi/sbi_init.c | 24 +++++---- lib/sbi/sbi_pmu.c | 2 +- lib/sbi/sbi_sse.c | 121 +++++++++++++++++++++++++++--------------- 4 files changed, 97 insertions(+), 58 deletions(-)include/sbi/sbi_sse.h +4
-4@@ -54,12 +54,12 @@ struct sbi_sse_cb_ops {void (*disable_cb)(uint32_t event_id); }; -/* Set the callback operations for an event -* @param event_id Event identifier (SBI_SSE_EVENT_*) -* @param cb_ops Callback operations +/* Add a supported event with associated callback operations +* @param event_id Event identifier (SBI_SSE_EVENT_* or a custom platform one) +* @param cb_ops Callback operations (Can be NULL if any) * @return 0 on success, error otherwise */ -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops); +int sbi_sse_add_event (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops);/* Inject an event to the current hard * @param event_id Event identifier (SBI_SSE_EVENT_*) lib/sbi/sbi_init.c +14
-10@@ -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",@@ -342,6 +336,16 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang (); } +/* +* Note: SSE events callbacks can be registered by other drivers so +* sbi_sse_init() needs to be called after all drivers have been probed. +*/ + rc =sbi_sse_init (scratch, true); +if (rc) { +sbi_printf ("%s: sse init failed (error %d)\n" , __func__, rc); +sbi_hart_hang (); + } +/* * Note: Ecall initialization should be after platform final * initialization so that all available platform devices are @@ -404,10 +408,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 +440,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 +1
-1@@ -1166,7 +1166,7 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) 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_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); phs =pmu_get_hart_state_ptr (scratch);if (!phs) {lib/sbi/sbi_sse.c +4
-14@@ -39,21 +39,11 @@#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 , -}; - -#define EVENT_COUNT array_size(supported_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 +100,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; };
lib/sbi/sbi_sse.c struct sse_global_event { · line 162
Quoted 7 lines
lib/sbi/sbi_sse.c +4
-0@@ -166,6 +156,11 @@ struct sse_global_event {*/ spinlock_t lock; }; +struct sse_event_info { +uint32_t event_id; +const struct sbi_sse_cb_ops *cb_ops; +struct sse_event_info *next;
Why not use sbi_dlist here ?
Regards,
Anup
Quoted 196 lines
lib/sbi/sbi_sse.c +70
-30+};static unsigned int local_event_count;static unsigned int global_event_count;@@ -180,6 +175,30 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;static int sse_ipi_inject_send (unsigned long hartid,uint32_t event_id); +struct sse_event_info global_software_event = { + .event_id =SBI_SSE_EVENT_GLOBAL_SOFTWARE , + .next =NULL , +}; + +struct sse_event_info local_software_event = { + .event_id =SBI_SSE_EVENT_LOCAL_SOFTWARE , + .next = &global_software_event, +}; + +static struct sse_event_info *supported_events = &local_software_event; + +static struct sse_event_info *sse_event_info_get (uint32_t event_id) +{ +struct sse_event_info *info; + +for (info = supported_events; info; info = info->next) { +if (info->event_id == event_id) +return info; + } + +return NULL ; +} +static unsigned long sse_event_state (struct sbi_sse_event *e) {return e->attrs.status &SBI_SSE_ATTR_STATUS_STATE_MASK ;@@ -867,20 +886,31 @@ int sbi_sse_inject_event(uint32_t event_id)return sse_inject_event (event_id,current_hartid ()); } -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops) +int sbi_sse_add_event (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)) +/* Do not allow adding an event twice */ + 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 && cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL (event_id)) +return SBI_EINVAL ; - e->cb_ops = cb_ops; -sse_event_put (e); + info =sbi_zalloc (sizeof (*info)); +if (!info) +return SBI_ENOMEM ; + + info->cb_ops = cb_ops; + info->event_id = event_id; + +/* +* Add the new event at the head of the linked list of supported +* events. +*/ + info->next = supported_events; + supported_events = info;return SBI_OK ; }@@ -1063,9 +1093,10 @@ 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 */ @@ -1074,10 +1105,10 @@ static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)static void sse_event_count_init () { -unsigned int i; +struct sse_event_info *info; -for (i =0 ; i <EVENT_COUNT ; i++) { -if (EVENT_IS_GLOBAL (supported_events[i])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id)) global_event_count++;else local_event_count++;@@ -1087,18 +1118,19 @@ static void sse_event_count_init()static int sse_global_init () {struct sbi_sse_event *e; -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info; 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])) +for (info = supported_events; info; info = info->next) { +if (!EVENT_IS_GLOBAL (info->event_id))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++;@@ -1109,16 +1141,16 @@ static int sse_global_init()static void sse_local_init (struct sse_hart_state *shs) { -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info;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])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id))continue ; - -sse_event_init (&shs->local_events[ev++], supported_events[i]); +sse_event_init (&shs->local_events[ev++], info); } }@@ -1148,7 +1180,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) } sse_inject_fifo_mem_off =sbi_scratch_alloc_offset ( -EVENT_COUNT *sizeof (struct sse_ipi_inject_data )); + (global_event_count + local_event_count) * +sizeof (struct sse_ipi_inject_data ));if (!sse_inject_fifo_mem_off) {sbi_scratch_free_offset (sse_inject_fifo_off);sbi_scratch_free_offset (shs_ptr_off);@@ -1185,7 +1218,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) sse_inject_mem =sbi_scratch_offset_ptr (scratch, sse_inject_fifo_mem_off); -sbi_fifo_init (sse_inject_q, sse_inject_mem,EVENT_COUNT , +sbi_fifo_init (sse_inject_q, sse_inject_mem, + (global_event_count + local_event_count),sizeof (struct sse_ipi_inject_data ));return 0 ;@@ -1193,18 +1227,19 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)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)) +for (info = supported_events; info; info = info->next) { +if (sse_event_get (info->event_id, &e))continue ;if (e->attrs.hartid !=current_hartid ()) goto skip;if (sse_event_state (e) >SBI_SSE_STATE_REGISTERED ) { -sbi_printf ("Event %d in invalid state at exit" , i); +sbi_printf ("Event %d in invalid state at exit" , + info->event_id);sse_event_set_state (e,SBI_SSE_STATE_UNUSED ); } --2.45.2
PATCH v4 3/5 lib: sbi: sse: allow adding new events
Quoted 154 lines · Anup Patel
Quoted 153 lines · On Tue, Dec 17, 2024 at 2:52?AM Cl?ment L?ger <[email protected]> wrote:
In order to allow events to be dynamically added, remove the existing static array of events and use a simply linked list of supported events. This allows us to move the
cb_opsinto this list and associated it with anevent_id. Drivers can now registercb_opsbefore bringing up the sse core to handle additional events (platform ones for instance).
sbi_sse_init()now allocates as many events as present in the linked list. Events can now be added withsbi_sse_add_event()which allows to add new supported events with some callback operations if any. If an event is not to be supported, thensbi_sse_add_event()should not be called. This approach currently consider that local events are to be supported on all harts (ie, they all support the same ISA or dependencies). If per-hart event availability needs to be supported, then, anis_supported()callback could be added later and called for each hart.Signed-off-by Cl?ment L?ger <[email protected]>--- include/sbi/sbi_sse.h | 8 +-- lib/sbi/sbi_init.c | 24 +++++---- lib/sbi/sbi_pmu.c | 2 +- lib/sbi/sbi_sse.c | 121 +++++++++++++++++++++++++++--------------- 4 files changed, 97 insertions(+), 58 deletions(-)include/sbi/sbi_sse.h +4
-4@@ -54,12 +54,12 @@ struct sbi_sse_cb_ops {void (*disable_cb)(uint32_t event_id); }; -/* Set the callback operations for an event -* @param event_id Event identifier (SBI_SSE_EVENT_*) -* @param cb_ops Callback operations +/* Add a supported event with associated callback operations +* @param event_id Event identifier (SBI_SSE_EVENT_* or a custom platform one) +* @param cb_ops Callback operations (Can be NULL if any) * @return 0 on success, error otherwise */ -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops); +int sbi_sse_add_event (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops);/* Inject an event to the current hard * @param event_id Event identifier (SBI_SSE_EVENT_*) lib/sbi/sbi_init.c +14
-10@@ -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",@@ -342,6 +336,16 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)sbi_hart_hang (); } +/* +* Note: SSE events callbacks can be registered by other drivers so +* sbi_sse_init() needs to be called after all drivers have been probed. +*/ + rc =sbi_sse_init (scratch, true); +if (rc) { +sbi_printf ("%s: sse init failed (error %d)\n" , __func__, rc); +sbi_hart_hang (); + } +/* * Note: Ecall initialization should be after platform final * initialization so that all available platform devices are @@ -404,10 +408,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 +440,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 +1
-1@@ -1166,7 +1166,7 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) 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_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); phs =pmu_get_hart_state_ptr (scratch);if (!phs) {lib/sbi/sbi_sse.c +4
-14@@ -39,21 +39,11 @@#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 , -}; - -#define EVENT_COUNT array_size(supported_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 +100,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; };
Quoted 10 lines
lib/sbi/sbi_sse.c struct sse_global_event { · line 162
Quoted 8 lines
lib/sbi/sbi_sse.c +4
-0@@ -166,6 +156,11 @@ struct sse_global_event {*/ spinlock_t lock; }; +struct sse_event_info { +uint32_t event_id; +const struct sbi_sse_cb_ops *cb_ops; +struct sse_event_info *next;Why not use
sbi_dlisthere ?
Hi Anup,
Two reasons:
1 - We don't need double linked list at all
2 - Static initialization is simpler using a simple linked list.
But I can add a sbi_slist implementation as well if you prefer, that would make it more reusable.
Thanks,
Cl?ment
Quoted 200 lines
Regards,
AnupQuoted 196 lines
lib/sbi/sbi_sse.c +70
-30+};static unsigned int local_event_count;static unsigned int global_event_count;@@ -180,6 +175,30 @@ static u32 sse_ipi_inject_event = SBI_IPI_EVENT_MAX;static int sse_ipi_inject_send (unsigned long hartid,uint32_t event_id); +struct sse_event_info global_software_event = { + .event_id =SBI_SSE_EVENT_GLOBAL_SOFTWARE , + .next =NULL , +}; + +struct sse_event_info local_software_event = { + .event_id =SBI_SSE_EVENT_LOCAL_SOFTWARE , + .next = &global_software_event, +}; + +static struct sse_event_info *supported_events = &local_software_event; + +static struct sse_event_info *sse_event_info_get (uint32_t event_id) +{ +struct sse_event_info *info; + +for (info = supported_events; info; info = info->next) { +if (info->event_id == event_id) +return info; + } + +return NULL ; +} +static unsigned long sse_event_state (struct sbi_sse_event *e) {return e->attrs.status &SBI_SSE_ATTR_STATUS_STATE_MASK ;@@ -867,20 +886,31 @@ int sbi_sse_inject_event(uint32_t event_id)return sse_inject_event (event_id,current_hartid ()); } -int sbi_sse_set_cb_ops (uint32_t event_id,const struct sbi_sse_cb_ops *cb_ops) +int sbi_sse_add_event (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)) +/* Do not allow adding an event twice */ + 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 && cb_ops->set_hartid_cb && !EVENT_IS_GLOBAL (event_id)) +return SBI_EINVAL ; - e->cb_ops = cb_ops; -sse_event_put (e); + info =sbi_zalloc (sizeof (*info)); +if (!info) +return SBI_ENOMEM ; + + info->cb_ops = cb_ops; + info->event_id = event_id; + +/* +* Add the new event at the head of the linked list of supported +* events. +*/ + info->next = supported_events; + supported_events = info;return SBI_OK ; }@@ -1063,9 +1093,10 @@ 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 */ @@ -1074,10 +1105,10 @@ static void sse_event_init(struct sbi_sse_event *e, uint32_t event_id)static void sse_event_count_init () { -unsigned int i; +struct sse_event_info *info; -for (i =0 ; i <EVENT_COUNT ; i++) { -if (EVENT_IS_GLOBAL (supported_events[i])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id)) global_event_count++;else local_event_count++;@@ -1087,18 +1118,19 @@ static void sse_event_count_init()static int sse_global_init () {struct sbi_sse_event *e; -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info; 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])) +for (info = supported_events; info; info = info->next) { +if (!EVENT_IS_GLOBAL (info->event_id))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++;@@ -1109,16 +1141,16 @@ static int sse_global_init()static void sse_local_init (struct sse_hart_state *shs) { -unsigned int i, ev =0 ; +unsigned int ev =0 ; +struct sse_event_info *info;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])) +for (info = supported_events; info; info = info->next) { +if (EVENT_IS_GLOBAL (info->event_id))continue ; - -sse_event_init (&shs->local_events[ev++], supported_events[i]); +sse_event_init (&shs->local_events[ev++], info); } }@@ -1148,7 +1180,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) } sse_inject_fifo_mem_off =sbi_scratch_alloc_offset ( -EVENT_COUNT *sizeof (struct sse_ipi_inject_data )); + (global_event_count + local_event_count) * +sizeof (struct sse_ipi_inject_data ));if (!sse_inject_fifo_mem_off) {sbi_scratch_free_offset (sse_inject_fifo_off);sbi_scratch_free_offset (shs_ptr_off);@@ -1185,7 +1218,8 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot) sse_inject_mem =sbi_scratch_offset_ptr (scratch, sse_inject_fifo_mem_off); -sbi_fifo_init (sse_inject_q, sse_inject_mem,EVENT_COUNT , +sbi_fifo_init (sse_inject_q, sse_inject_mem, + (global_event_count + local_event_count),sizeof (struct sse_ipi_inject_data ));return 0 ;@@ -1193,18 +1227,19 @@ int sbi_sse_init(struct sbi_scratch *scratch, bool cold_boot)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)) +for (info = supported_events; info; info = info->next) { +if (sse_event_get (info->event_id, &e))continue ;if (e->attrs.hartid !=current_hartid ()) goto skip;if (sse_event_state (e) >SBI_SSE_STATE_REGISTERED ) { -sbi_printf ("Event %d in invalid state at exit" , i); +sbi_printf ("Event %d in invalid state at exit" , + info->event_id);sse_event_set_state (e,SBI_SSE_STATE_UNUSED ); } --2.45.2
PATCH v4 4/5 lib: sbi: sse: return SBI_ENOTSUPP for unsupported events
In reply to Clément LégerIf a standard event was not found in the list of events that are handled by harts but belongs to the standard event list defined by the specification, return SBI_ENOTSUPP. Without that, we can not distinguish a non implemented standard event from a non valid one.
--- lib/sbi/sbi_sse.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
lib/sbi/sbi_sse.c +30 -0
@@ -187,6 +187,31 @@ struct sse_event_info local_software_event = {static struct sse_event_info *supported_events = &local_software_event; +/* +* This array is used to distinguish between standard event and platform +* events in order to return SBI_ERR_NOT_SUPPORTED for them. +*/ +static const uint32_t standard_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 , +}; + +static bool sse_is_standard_event (uint32_t event_id) +{ +int i; + +for (i =0 ; i <array_size (standard_events); i++) { +if (event_id == standard_events[i]) +return true; + } + +return false; +} +static struct sse_event_info *sse_event_info_get (uint32_t event_id) {struct sse_event_info *info;
@@ -292,6 +317,11 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret) } } +/* Check if the event is a standard one but not supported */ +if (sse_is_standard_event (event_id)) +return SBI_ENOTSUPP ; + +/* If not supported nor a standard event, it is invalid */ return SBI_EINVAL ; }
Signature · 1 line
2.45.2
PATCH v4 4/5 lib: sbi: sse: return SBI_ENOTSUPP for unsupported events
Quoted 9 lines · Cl?ment L?ger
If a standard event was not found in the list of events that are handled by harts but belongs to the standard event list defined by the specification, return
SBI_ENOTSUPP. Without that, we can not distinguish a non implemented standard event from a non valid one.Signed-off-by Cl?ment L?ger <[email protected]>--- lib/sbi/sbi_sse.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
PATCH v4 4/5 lib: sbi: sse: return SBI_ENOTSUPP for unsupported events
In reply to Clément LégerQuoted 49 lines · On Mon, Dec 16, 2024 at 1:22?PM Cl?ment L?ger <[email protected]> wrote:
If a standard event was not found in the list of events that are handled by harts but belongs to the standard event list defined by the specification, return
SBI_ENOTSUPP. Without that, we can not distinguish a non implemented standard event from a non valid one.Signed-off-by Cl?ment L?ger <[email protected]>--- lib/sbi/sbi_sse.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)lib/sbi/sbi_sse.c +25
-0@@ -187,6 +187,31 @@ struct sse_event_info local_software_event = {static struct sse_event_info *supported_events = &local_software_event; +/* +* This array is used to distinguish between standard event and platform +* events in order to return SBI_ERR_NOT_SUPPORTED for them. +*/ +static const uint32_t standard_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 , +}; + +static bool sse_is_standard_event (uint32_t event_id) +{ +int i; + +for (i =0 ; i <array_size (standard_events); i++) { +if (event_id == standard_events[i]) +return true; + } + +return false; +} +static struct sse_event_info *sse_event_info_get (uint32_t event_id) {struct sse_event_info *info;@@ -292,6 +317,11 @@ static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret) }
lib/sbi/sbi_sse.c static int sse_event_get(uint32_t event_id, struct sbi_sse_event **eret) · line 327
Quoted 10 lines
lib/sbi/sbi_sse.c +5
-0} +/* Check if the event is a standard one but not supported */ +if (sse_is_standard_event (event_id)) +return SBI_ENOTSUPP ; + +/* If not supported nor a standard event, it is invalid */ return SBI_EINVAL ; }
Quoted 3 lines
--
2.45.2
PATCH v4 5/5 lib: sbi: pmu: add the PMU SSE event only if overflow IRQ is supported
In reply to Clément LégerAdd the PMU SSE event only if an overflow irq bit is present.
--- lib/sbi/sbi_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +2 -1
@@ -1166,7 +1166,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) total_ctrs = num_hw_ctrs +SBI_PMU_FW_CTR_MAX ; } -sbi_sse_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); +if (sbi_pmu_irq_bit () >=0 ) +sbi_sse_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); phs =pmu_get_hart_state_ptr (scratch);if (!phs) {
Signature · 1 line
2.45.2
PATCH v4 5/5 lib: sbi: pmu: add the PMU SSE event only if overflow IRQ is supported
Quoted 6 lines · Cl?ment L?ger
Add the PMU SSE event only if an overflow irq bit is present.
Signed-off-by Cl?ment L?ger <[email protected]>--- lib/sbi/sbi_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
PATCH v4 5/5 lib: sbi: pmu: add the PMU SSE event only if overflow IRQ is supported
In reply to Clément LégerQuoted 13 lines · On Mon, Dec 16, 2024 at 1:22?PM Cl?ment L?ger <[email protected]> wrote:
Add the PMU SSE event only if an overflow irq bit is present.
Signed-off-by Cl?ment L?ger <[email protected]>--- lib/sbi/sbi_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/sbi/sbi_pmu.c +0
-0
lib/sbi/sbi_pmu.c int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) · line 1171
Quoted 7 lines
lib/sbi/sbi_pmu.c +2
-1@@ -1166,7 +1166,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) total_ctrs = num_hw_ctrs +SBI_PMU_FW_CTR_MAX ; } -sbi_sse_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); +if (sbi_pmu_irq_bit () >=0 ) +sbi_sse_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops);
Quoted 5 lines
lib/sbi/sbi_pmu.c +0
-1phs =pmu_get_hart_state_ptr (scratch);if (!phs) { --2.45.2
PATCH v4 5/5 lib: sbi: pmu: add the PMU SSE event only if overflow IRQ is supported
Quoted 17 lines · Atish Kumar Patra
Quoted 16 lines · On Mon, Dec 16, 2024 at 1:22?PM Cl?ment L?ger <[email protected]> wrote:
Add the PMU SSE event only if an overflow irq bit is present.
Signed-off-by Cl?ment L?ger <[email protected]>--- lib/sbi/sbi_pmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)lib/sbi/sbi_pmu.c +0
-0@@ -1166,7 +1166,8 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) total_ctrs = num_hw_ctrs +SBI_PMU_FW_CTR_MAX ; }
Quoted 4 lines
lib/sbi/sbi_pmu.c int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot) · line 1171
Quoted 4 lines
lib/sbi/sbi_pmu.c +2
-1-sbi_sse_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops); +if (sbi_pmu_irq_bit () >=0 ) +sbi_sse_add_event (SBI_SSE_EVENT_LOCAL_PMU , &pmu_sse_cb_ops);
Even though it works, I should have move the call to the coldboot section. I'll fix that in the V5.
Thanks,
Cl?ment
Quoted 9 lines
Reviewed-by Atish Patra <[email protected]>Quoted 5 lines
lib/sbi/sbi_pmu.c +0
-1phs =pmu_get_hart_state_ptr (scratch);if (!phs) { --2.45.2