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
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 */
43 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
46 static const char *event_type_name(enum event_t type)
48 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
49 return type_name[type];
55 static int notify_static(struct event *ev)
57 struct evspy_info *start =
58 ll_entry_start(struct evspy_info, evspy_info);
59 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
60 struct evspy_info *spy;
62 for (spy = start; spy != start + n_ents; spy++) {
63 if (spy->type == ev->type) {
66 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
67 event_type_name(ev->type), event_spy_id(spy));
68 ret = spy->func(NULL, ev);
71 * TODO: Handle various return codes to
73 * - claim an event (no others will see it)
74 * - return an error from the event
77 return log_msg_ret("spy", ret);
84 static int notify_dynamic(struct event *ev)
86 struct event_state *state = gd_event_state();
87 struct event_spy *spy, *next;
89 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
90 if (spy->type == ev->type) {
93 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
94 event_type_name(ev->type), spy->id);
95 ret = spy->func(spy->ctx, ev);
98 * TODO: Handle various return codes to
100 * - claim an event (no others will see it)
101 * - return an error from the event
104 return log_msg_ret("spy", ret);
111 int event_notify(enum event_t type, void *data, int size)
117 if (size > sizeof(event.data))
118 return log_msg_ret("size", -E2BIG);
119 memcpy(&event.data, data, size);
121 ret = notify_static(&event);
123 return log_msg_ret("dyn", ret);
125 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
126 ret = notify_dynamic(&event);
128 return log_msg_ret("dyn", ret);
134 int event_notify_null(enum event_t type)
136 return event_notify(type, NULL, 0);
139 void event_show_spy_list(void)
141 struct evspy_info *start =
142 ll_entry_start(struct evspy_info, evspy_info);
143 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
144 struct evspy_info *spy;
145 const int size = sizeof(ulong) * 2;
147 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
148 for (spy = start; spy != start + n_ents; spy++) {
149 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
150 spy->type, event_type_name(spy->type), size, spy->func,
155 #if CONFIG_IS_ENABLED(NEEDS_MANUAL_RELOC)
156 int event_manual_reloc(void)
158 struct evspy_info *spy, *end;
160 spy = ll_entry_start(struct evspy_info, evspy_info);
161 end = ll_entry_end(struct evspy_info, evspy_info);
162 for (; spy < end; spy++)
163 MANUAL_RELOC(spy->func);
169 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
170 static void spy_free(struct event_spy *spy)
172 list_del(&spy->sibling_node);
175 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
177 struct event_state *state = gd_event_state();
178 struct event_spy *spy;
180 spy = malloc(sizeof(*spy));
182 return log_msg_ret("alloc", -ENOMEM);
188 list_add_tail(&spy->sibling_node, &state->spy_head);
193 int event_uninit(void)
195 struct event_state *state = gd_event_state();
196 struct event_spy *spy, *next;
198 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
206 struct event_state *state = gd_event_state();
208 INIT_LIST_HEAD(&state->spy_head);
212 #endif /* EVENT_DYNAMIC */