Skip to contentSkip to conversation

lib: sbi: sse: rework event availability

PATCH New Clément Léger 2 participants 11 messages Jan 10, 2025 – Jan 30, 2025

lore
Share & tools
lore
#0 lore raw

v6 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 allow adding new events at boot time using sbi_sse_add_event(). Aditionally, it now returns SBI_ERR_NOT_SUPPORTED if the requested event can not be handled by any driver.

---
Changes in V6
- Add double trap event if dbltrp extension is available

Changes in V5
- Remove a blank line before sse_event_info
- Move PMU call to sbi_sse_add_event() in cold_boot path.
- Move simply-linked list implementation to a separate header.

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 (7):
lib: sbi: pmu: fix usage of sbi_pmu_irq_bit()
lib: sbi: move sbi_double_trap_handler() to a dedicated header
lib: sbi: sse: return an error value from sse_event_get()
include: lib: add a simple simply linked list implementation
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_double_trap.h                 |  20 ++
 include/sbi/sbi_pmu.h                         |   3 +
 include/sbi/sbi_slist.h                       |  33 +++
 include/sbi/sbi_sse.h                         |   8 +-
 include/sbi/sbi_trap_ldst.h                   |   2 -
 lib/sbi/sbi_double_trap.c                     |   7 +
 lib/sbi/sbi_hart.c                            |   2 +-
 lib/sbi/sbi_init.c                            |  27 ++-
 lib/sbi/sbi_pmu.c                             |  47 ++--
 lib/sbi/sbi_sse.c                             | 209 ++++++++++++------
 lib/sbi/sbi_trap.c                            |  16 +-
 .../generic/include/thead/c9xx_encoding.h     |   1 -
 platform/generic/thead/thead_c9xx_pmu.c       |   2 +-
 13 files changed, 264 insertions(+), 113 deletions(-)
 create mode 100644 include/sbi/sbi_double_trap.h
 create mode 100644 include/sbi/sbi_slist.h
Signature · 1 line
2.47.1
#1 lore raw

PATCH v6 1/7 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 led 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
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 +9 -6
index d2de0c8f..242ffe9b 100644
@@ -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
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.47.1
#2 lore raw

PATCH v6 2/7 lib: sbi: move sbi_double_trap_handler() to a dedicated header

In reply to Clément Léger

We will add new functions to sbi_double_trap.c in order to register an SSE event, split this to a header as part of preparation work.

---
 include/sbi/sbi_double_trap.h | 20 ++++++++++++++++++++
 include/sbi/sbi_trap_ldst.h   |  2 --
 lib/sbi/sbi_trap.c            |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 include/sbi/sbi_double_trap.h
include/sbi/sbi_double_trap.h new file +20 -0
new file mode 100644
index 00000000..c3d64dfd
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 Rivos Inc.
+ *
+ * Authors:
+ *   Cl?ment L?ger <[email protected]>
+ */
+
+#ifndef __SBI_DOUBLE_TRAP_H__
+#define __SBI_DOUBLE_TRAP_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_trap.h>
+
+int sbi_double_trap_handler(struct sbi_trap_context *tcntx);
+
+void sbi_double_trap_init(struct sbi_scratch *scratch);
+
+#endif
include/sbi/sbi_trap_ldst.h +0 -2
index 34877ccc..a6a6c75b 100644
@@ -28,8 +28,6 @@ int sbi_load_access_handler(struct sbi_trap_context *tcntx);
 
 int sbi_store_access_handler(struct sbi_trap_context *tcntx);
 
-int sbi_double_trap_handler(struct sbi_trap_context *tcntx);
-
 ulong sbi_misaligned_tinst_fixup(ulong orig_tinst, ulong new_tinst,
 				 ulong addr_offset);
 
lib/sbi/sbi_trap.c +1 -0
index 242ffe9b..e63a563b 100644
@@ -11,6 +11,7 @@
 #include <sbi/riscv_encoding.h>
 #include <sbi/sbi_bitops.h>
 #include <sbi/sbi_console.h>
+#include <sbi/sbi_double_trap.h>
 #include <sbi/sbi_ecall.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_hart.h>
Signature · 1 line
2.47.1
#3 lore raw

PATCH v6 2/7 lib: sbi: move sbi_double_trap_handler() to a dedicated header

Quoted 5 lines · On Fri, Jan 10, 2025 at 6:55?PM Cl?ment L?ger <[email protected]> wrote:

We will add new functions to sbi_double_trap.c in order to register an SSE event, split this to a header as part of preparation work.

LGTM.

Regards,
Anup

Quoted 66 lines
---
 include/sbi/sbi_double_trap.h | 20 ++++++++++++++++++++
 include/sbi/sbi_trap_ldst.h   |  2 --
 lib/sbi/sbi_trap.c            |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 include/sbi/sbi_double_trap.h
include/sbi/sbi_double_trap.h new file +20 -0
new file mode 100644
index 00000000..c3d64dfd
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 Rivos Inc.
+ *
+ * Authors:
+ *   Cl?ment L?ger <[email protected]>
+ */
+
+#ifndef __SBI_DOUBLE_TRAP_H__
+#define __SBI_DOUBLE_TRAP_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_trap.h>
+
+int sbi_double_trap_handler(struct sbi_trap_context *tcntx);
+
+void sbi_double_trap_init(struct sbi_scratch *scratch);
+
+#endif
include/sbi/sbi_trap_ldst.h +0 -2
index 34877ccc..a6a6c75b 100644
@@ -28,8 +28,6 @@ int sbi_load_access_handler(struct sbi_trap_context *tcntx);
 
 int sbi_store_access_handler(struct sbi_trap_context *tcntx);
 
-int sbi_double_trap_handler(struct sbi_trap_context *tcntx);
-
 ulong sbi_misaligned_tinst_fixup(ulong orig_tinst, ulong new_tinst,
                                 ulong addr_offset);
 
lib/sbi/sbi_trap.c +1 -1
index 242ffe9b..e63a563b 100644
@@ -11,6 +11,7 @@
 #include <sbi/riscv_encoding.h>
 #include <sbi/sbi_bitops.h>
 #include <sbi/sbi_console.h>
+#include <sbi/sbi_double_trap.h>
 #include <sbi/sbi_ecall.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_hart.h>
--

2.47.1

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

#4 lore raw

PATCH v6 3/7 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.47.1
#5 lore raw

PATCH v6 4/7 include: lib: add a simple simply linked list implementation

In reply to Clément Léger

Add a simple linked list implementation when double linked list are not needed. This allows to easily have statically defined linked list that can be extended at runtime.

---
 include/sbi/sbi_slist.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 include/sbi/sbi_slist.h
include/sbi/sbi_slist.h new file +33 -0
new file mode 100644
index 00000000..e4b83cfd
@@ -0,0 +1,33 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Simple simply-linked list library.
+ *
+ * Copyright (c) 2025 Rivos Inc.
+ *
+ * Authors:
+ *   Cl?ment L?ger <[email protected]>
+ */
+
+#ifndef __SBI_SLIST_H__
+#define __SBI_SLIST_H__
+
+#include <sbi/sbi_types.h>
+
+#define SBI_SLIST_HEAD_INIT(_ptr)	(_ptr)
+#define SBI_SLIST_HEAD(_lname, _stype) struct _stype *_lname
+#define SBI_SLIST_NODE(_stype) SBI_SLIST_HEAD(next, _stype)
+#define SBI_SLIST_NODE_INIT(_ptr) .next = _ptr
+
+#define SBI_INIT_SLIST_HEAD(_head) (_head) = NULL
+
+#define SBI_SLIST_ADD(_ptr, _head) \
+do { \
+	(_ptr)->next = _head; \
+	(_head) = _ptr; \
+} while (0)
+
+#define SBI_SLIST_FOR_EACH_ENTRY(_ptr, _head) \
+	for (_ptr = _head; _ptr; _ptr = _ptr->next)
+
+#endif
Signature · 1 line
2.47.1
#6 lore raw

PATCH v6 4/7 include: lib: add a simple simply linked list implementation

Typo in patch subject "simple simply".

Quoted 6 lines · On Fri, Jan 10, 2025 at 6:55?PM Cl?ment L?ger <[email protected]> wrote:

Add a simple linked list implementation when double linked list are not needed. This allows to easily have statically defined linked list that can be extended at runtime.

Otherwise, it looks good to me. I will take care of the above comment at the time of merging this patch.

Regards,
Anup

Quoted 52 lines
---
 include/sbi/sbi_slist.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 include/sbi/sbi_slist.h
include/sbi/sbi_slist.h new file +33 -1
new file mode 100644
index 00000000..e4b83cfd
@@ -0,0 +1,33 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Simple simply-linked list library.
+ *
+ * Copyright (c) 2025 Rivos Inc.
+ *
+ * Authors:
+ *   Cl?ment L?ger <[email protected]>
+ */
+
+#ifndef __SBI_SLIST_H__
+#define __SBI_SLIST_H__
+
+#include <sbi/sbi_types.h>
+
+#define SBI_SLIST_HEAD_INIT(_ptr)      (_ptr)
+#define SBI_SLIST_HEAD(_lname, _stype) struct _stype *_lname
+#define SBI_SLIST_NODE(_stype) SBI_SLIST_HEAD(next, _stype)
+#define SBI_SLIST_NODE_INIT(_ptr) .next = _ptr
+
+#define SBI_INIT_SLIST_HEAD(_head) (_head) = NULL
+
+#define SBI_SLIST_ADD(_ptr, _head) \
+do { \
+       (_ptr)->next = _head; \
+       (_head) = _ptr; \
+} while (0)
+
+#define SBI_SLIST_FOR_EACH_ENTRY(_ptr, _head) \
+       for (_ptr = _head; _ptr; _ptr = _ptr->next)
+
+#endif
--

2.47.1

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

#7 lore raw

PATCH v6 5/7 lib: sbi: sse: allow adding new events

In reply to 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_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_double_trap.c |   7 +++
 lib/sbi/sbi_init.c        |  27 +++++----
 lib/sbi/sbi_pmu.c         |   4 +-
 lib/sbi/sbi_sse.c         | 119 ++++++++++++++++++++++++--------------
 5 files changed, 106 insertions(+), 59 deletions(-)
include/sbi/sbi_sse.h +4 -4
index 7419698a..fb796545 100644
@@ -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_double_trap.c +7 -0
index bab96fb7..d0ddb493 100644
@@ -10,6 +10,7 @@
 #include <sbi/sbi_console.h>
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
 #include <sbi/sbi_sse.h>
 #include <sbi/sbi_trap.h>
 
@@ -28,3 +29,9 @@ int sbi_double_trap_handler(struct sbi_trap_context *tcntx)
 
 	return sbi_sse_inject_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP);
 }
+
+void sbi_double_trap_init(struct sbi_scratch *scratch)
+{
+	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSDBLTRP))
+		sbi_sse_add_event(SBI_SSE_EVENT_LOCAL_DOUBLE_TRAP, NULL);
+}
\ No newline at end of file
lib/sbi/sbi_init.c +17 -10
index 58030cf2..3f654c80 100644
@@ -13,6 +13,7 @@
 #include <sbi/sbi_console.h>
 #include <sbi/sbi_cppc.h>
 #include <sbi/sbi_domain.h>
+#include <sbi/sbi_double_trap.h>
 #include <sbi/sbi_ecall.h>
 #include <sbi/sbi_fwft.h>
 #include <sbi/sbi_hart.h>
@@ -266,12 +267,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",
@@ -285,6 +280,8 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 
 	sbi_boot_print_banner(scratch);
 
+	sbi_double_trap_init(scratch);
+
 	rc = sbi_irqchip_init(scratch, true);
 	if (rc) {
 		sbi_printf("%s: irqchip init failed (error %d)\n",
@@ -346,6 +343,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
@@ -408,10 +415,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();
@@ -444,6 +447,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 c6df45a7..6e69a4dc 100644
@@ -1164,9 +1164,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_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 +76 -43

227 lines not on this page · raw message

Signature · 1 line
2.47.1
#8 lore raw

PATCH v6 6/7 lib: sbi: sse: return SBI_ENOTSUPP for unsupported events

In reply to 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.

---
 lib/sbi/sbi_sse.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
lib/sbi/sbi_sse.c +30 -0
index 005eb56e..9f22375f 100644
@@ -190,6 +190,31 @@ struct sse_event_info local_software_event = {
 static SBI_SLIST_HEAD(supported_events, sse_event_info) =
 				SBI_SLIST_HEAD_INIT(&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;
@@ -295,6 +320,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.47.1
#9 lore raw

PATCH v6 7/7 lib: sbi: pmu: add the PMU SSE event only if overflow IRQ is supported

In reply to Clément Léger

Add 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
index 6e69a4dc..40b898fe 100644
@@ -1165,7 +1165,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);
Signature · 1 line
2.47.1
Applied
#10 lore raw

v6 cover lib: sbi: sse: rework event availability

In reply to Clément Léger
Quoted 44 lines · On Fri, Jan 10, 2025 at 6:55?PM Cl?ment L?ger <[email protected]> wrote:

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 allow adding new events at boot time using sbi_sse_add_event(). Aditionally, it now returns SBI_ERR_NOT_SUPPORTED if the requested event can not be handled by any driver.

---
Changes in V6
- Add double trap event if dbltrp extension is available

Changes in V5
- Remove a blank line before sse_event_info
- Move PMU call to sbi_sse_add_event() in cold_boot path.
- Move simply-linked list implementation to a separate header.

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

Quoted 8 lines

Cl?ment L?ger (7):
lib: sbi: pmu: fix usage of sbi_pmu_irq_bit()
lib: sbi: move sbi_double_trap_handler() to a dedicated header
lib: sbi: sse: return an error value from sse_event_get()
include: lib: add a simple simply linked list implementation
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

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Quoted 26 lines
 include/sbi/sbi_double_trap.h                 |  20 ++
 include/sbi/sbi_pmu.h                         |   3 +
 include/sbi/sbi_slist.h                       |  33 +++
 include/sbi/sbi_sse.h                         |   8 +-
 include/sbi/sbi_trap_ldst.h                   |   2 -
 lib/sbi/sbi_double_trap.c                     |   7 +
 lib/sbi/sbi_hart.c                            |   2 +-
 lib/sbi/sbi_init.c                            |  27 ++-
 lib/sbi/sbi_pmu.c                             |  47 ++--
 lib/sbi/sbi_sse.c                             | 209 ++++++++++++------
 lib/sbi/sbi_trap.c                            |  16 +-
 .../generic/include/thead/c9xx_encoding.h     |   1 -
 platform/generic/thead/thead_c9xx_pmu.c       |   2 +-
 13 files changed, 264 insertions(+), 113 deletions(-)
 create mode 100644 include/sbi/sbi_double_trap.h
 create mode 100644 include/sbi/sbi_slist.h

--
2.47.1

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