aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2022-03-04 08:43:00 -0700
committerTom Rini <trini@konsulko.com>2022-03-10 08:28:36 -0500
commit87a5d1b5d012b0663517bfa36f5e01c8028f121a (patch)
tree4512a25b7200e1c84cb07aa64a17f98c46a1a13f /common
parent64defba4ea55736926c5152a245164ace58fa04e (diff)
downloadu-boot-87a5d1b5d012b0663517bfa36f5e01c8028f121a.zip
u-boot-87a5d1b5d012b0663517bfa36f5e01c8028f121a.tar.gz
u-boot-87a5d1b5d012b0663517bfa36f5e01c8028f121a.tar.bz2
event: Add basic support for events
Add a way to create and dispatch events without needing to allocate memory. Also add a way to 'spy' on events, thus allowing 'hooks' to be created. Use a linker list for static events, which we can use to replace functions like arch_cpu_init_f(). Allow an EVENT_DEBUG option which makes it easier to see what is going on at runtime, but uses more code space. Dynamic events allow the creation of a spy at runtime. This is not always necessary, but can be enabled with EVENT_DYNAMIC if needed. A 'test' event is the only option for now. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'common')
-rw-r--r--common/Kconfig31
-rw-r--r--common/Makefile2
-rw-r--r--common/board_r.c1
-rw-r--r--common/event.c186
-rw-r--r--common/log.c1
5 files changed, 221 insertions, 0 deletions
diff --git a/common/Kconfig b/common/Kconfig
index add4cda..cabc24f 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -492,6 +492,37 @@ config DISPLAY_BOARDINFO_LATE
menu "Start-up hooks"
+config EVENT
+ bool "General-purpose event-handling mechanism"
+ default y if SANDBOX
+ help
+ This enables sending and processing of events, to allow interested
+ parties to be alerted when something happens. This is an attempt to
+ step the flow of weak functions, hooks, functions in board_f.c
+ and board_r.c and the Kconfig options below.
+
+ See doc/develop/event.rst for more information.
+
+if EVENT
+
+config EVENT_DYNAMIC
+ bool "Support event registration at runtime"
+ default y if SANDBOX
+ help
+ Enable this to support adding an event spy at runtime, without adding
+ it to the EVENT_SPy() linker list. This increases code size slightly
+ but provides more flexibility for boards and subsystems that need it.
+
+config EVENT_DEBUG
+ bool "Enable event debugging assistance"
+ default y if SANDBOX
+ help
+ Enable this get usefui features for seeing what is happening with
+ events, such as event-type names. This adds to the code size of
+ U-Boot so can be turned off for production builds.
+
+endif # EVENT
+
config ARCH_EARLY_INIT_R
bool "Call arch-specific init soon after relocation"
help
diff --git a/common/Makefile b/common/Makefile
index 3eff719..cc2ba30 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -89,6 +89,8 @@ obj-y += malloc_simple.o
endif
endif
+obj-$(CONFIG_$(SPL_TPL_)EVENT) += event.o
+
obj-$(CONFIG_$(SPL_TPL_)HASH) += hash.o
obj-$(CONFIG_IO_TRACE) += iotrace.o
obj-y += memsize.o
diff --git a/common/board_r.c b/common/board_r.c
index c24d9b4..b92c1bb 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -594,6 +594,7 @@ static int run_main_loop(void)
static init_fnc_t init_sequence_r[] = {
initr_trace,
initr_reloc,
+ event_init,
/* TODO: could x86/PPC have this also perhaps? */
#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
initr_caches,
diff --git a/common/event.c b/common/event.c
new file mode 100644
index 0000000..366be24
--- /dev/null
+++ b/common/event.c
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Events provide a general-purpose way to react to / subscribe to changes
+ * within U-Boot
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#define LOG_CATEGORY LOGC_EVENT
+
+#include <common.h>
+#include <event.h>
+#include <event_internal.h>
+#include <log.h>
+#include <linker_lists.h>
+#include <malloc.h>
+#include <asm/global_data.h>
+#include <linux/list.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#if CONFIG_IS_ENABLED(EVENT_DEBUG)
+const char *const type_name[] = {
+ "none",
+ "test",
+};
+
+_Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
+#endif
+
+static const char *event_type_name(enum event_t type)
+{
+#if CONFIG_IS_ENABLED(EVENT_DEBUG)
+ return type_name[type];
+#else
+ return "(unknown)";
+#endif
+}
+
+static int notify_static(struct event *ev)
+{
+ struct evspy_info *start =
+ ll_entry_start(struct evspy_info, evspy_info);
+ const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
+ struct evspy_info *spy;
+
+ for (spy = start; spy != start + n_ents; spy++) {
+ if (spy->type == ev->type) {
+ int ret;
+
+ log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
+ event_type_name(ev->type), event_spy_id(spy));
+ ret = spy->func(NULL, ev);
+
+ /*
+ * TODO: Handle various return codes to
+ *
+ * - claim an event (no others will see it)
+ * - return an error from the event
+ */
+ if (ret)
+ return log_msg_ret("spy", ret);
+ }
+ }
+
+ return 0;
+}
+
+static int notify_dynamic(struct event *ev)
+{
+ struct event_state *state = gd_event_state();
+ struct event_spy *spy, *next;
+
+ list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
+ if (spy->type == ev->type) {
+ int ret;
+
+ log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
+ event_type_name(ev->type), spy->id);
+ ret = spy->func(spy->ctx, ev);
+
+ /*
+ * TODO: Handle various return codes to
+ *
+ * - claim an event (no others will see it)
+ * - return an error from the event
+ */
+ if (ret)
+ return log_msg_ret("spy", ret);
+ }
+ }
+
+ return 0;
+}
+
+int event_notify(enum event_t type, void *data, int size)
+{
+ struct event event;
+ int ret;
+
+ event.type = type;
+ if (size > sizeof(event.data))
+ return log_msg_ret("size", -E2BIG);
+ memcpy(&event.data, data, size);
+
+ ret = notify_static(&event);
+ if (ret)
+ return log_msg_ret("dyn", ret);
+
+ if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
+ ret = notify_dynamic(&event);
+ if (ret)
+ return log_msg_ret("dyn", ret);
+ }
+
+ return 0;
+}
+
+int event_notify_null(enum event_t type)
+{
+ return event_notify(type, NULL, 0);
+}
+
+void event_show_spy_list(void)
+{
+ struct evspy_info *start =
+ ll_entry_start(struct evspy_info, evspy_info);
+ const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
+ struct evspy_info *spy;
+ const int size = sizeof(ulong) * 2;
+
+ printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
+ for (spy = start; spy != start + n_ents; spy++) {
+ printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
+ spy->type, event_type_name(spy->type), size, spy->func,
+ event_spy_id(spy));
+ }
+}
+
+#if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
+static void spy_free(struct event_spy *spy)
+{
+ list_del(&spy->sibling_node);
+}
+
+int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
+{
+ struct event_state *state = gd_event_state();
+ struct event_spy *spy;
+
+ if (!CONFIG_IS_ENABLED(EVENT_DYNAMIC))
+ return -ENOSYS;
+ spy = malloc(sizeof(*spy));
+ if (!spy)
+ return log_msg_ret("alloc", -ENOMEM);
+
+ spy->id = id;
+ spy->type = type;
+ spy->func = func;
+ spy->ctx = ctx;
+ list_add_tail(&spy->sibling_node, &state->spy_head);
+
+ return 0;
+}
+
+int event_uninit(void)
+{
+ struct event_state *state = gd_event_state();
+ struct event_spy *spy, *next;
+
+ list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
+ spy_free(spy);
+
+ return 0;
+}
+
+int event_init(void)
+{
+ struct event_state *state = gd_event_state();
+
+ INIT_LIST_HEAD(&state->spy_head);
+
+ return 0;
+}
+#endif /* EVENT_DYNAMIC */
diff --git a/common/log.c b/common/log.c
index f7e0c0f..7254aa7 100644
--- a/common/log.c
+++ b/common/log.c
@@ -28,6 +28,7 @@ static const char *const log_cat_name[] = {
"devres",
"acpi",
"boot",
+ "event",
};
_Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,