1 // SPDX-License-Identifier: GPL-2.0+
3 * Events provide a general-purpose way to react to / subscribe to changes
6 * Copyright 2021 Google LLC
7 * Written by Simon Glass <sjg@chromium.org>
10 #define LOG_CATEGORY LOGC_EVENT
14 #include <event_internal.h>
16 #include <linker_lists.h>
18 #include <asm/global_data.h>
19 #include <linux/list.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
25 const char *const type_name[] = {
29 /* Events related to driver model */
42 /* main loop events */
46 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
49 static const char *event_type_name(enum event_t type)
51 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
52 return type_name[type];
58 static int notify_static(struct event *ev)
60 struct evspy_info *start =
61 ll_entry_start(struct evspy_info, evspy_info);
62 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
63 struct evspy_info *spy;
65 for (spy = start; spy != start + n_ents; spy++) {
66 if (spy->type == ev->type) {
69 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
70 event_type_name(ev->type), event_spy_id(spy));
71 ret = spy->func(NULL, ev);
74 * TODO: Handle various return codes to
76 * - claim an event (no others will see it)
77 * - return an error from the event
80 return log_msg_ret("spy", ret);
87 static int notify_dynamic(struct event *ev)
89 struct event_state *state = gd_event_state();
90 struct event_spy *spy, *next;
92 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
93 if (spy->type == ev->type) {
96 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
97 event_type_name(ev->type), spy->id);
98 ret = spy->func(spy->ctx, ev);
101 * TODO: Handle various return codes to
103 * - claim an event (no others will see it)
104 * - return an error from the event
107 return log_msg_ret("spy", ret);
114 int event_notify(enum event_t type, void *data, int size)
120 if (size > sizeof(event.data))
121 return log_msg_ret("size", -E2BIG);
122 memcpy(&event.data, data, size);
124 ret = notify_static(&event);
126 return log_msg_ret("sta", ret);
128 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
129 ret = notify_dynamic(&event);
131 return log_msg_ret("dyn", ret);
137 int event_notify_null(enum event_t type)
139 return event_notify(type, NULL, 0);
142 void event_show_spy_list(void)
144 struct evspy_info *start =
145 ll_entry_start(struct evspy_info, evspy_info);
146 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
147 struct evspy_info *spy;
148 const int size = sizeof(ulong) * 2;
150 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
151 for (spy = start; spy != start + n_ents; spy++) {
152 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
153 spy->type, event_type_name(spy->type), size, spy->func,
158 #if CONFIG_IS_ENABLED(NEEDS_MANUAL_RELOC)
159 int event_manual_reloc(void)
161 struct evspy_info *spy, *end;
163 spy = ll_entry_start(struct evspy_info, evspy_info);
164 end = ll_entry_end(struct evspy_info, evspy_info);
165 for (; spy < end; spy++)
166 MANUAL_RELOC(spy->func);
172 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
173 static void spy_free(struct event_spy *spy)
175 list_del(&spy->sibling_node);
178 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
180 struct event_state *state = gd_event_state();
181 struct event_spy *spy;
183 spy = malloc(sizeof(*spy));
185 return log_msg_ret("alloc", -ENOMEM);
191 list_add_tail(&spy->sibling_node, &state->spy_head);
196 int event_uninit(void)
198 struct event_state *state = gd_event_state();
199 struct event_spy *spy, *next;
201 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
209 struct event_state *state = gd_event_state();
211 INIT_LIST_HEAD(&state->spy_head);
215 #endif /* EVENT_DYNAMIC */