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 */
46 /* main loop events */
50 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
53 const char *event_type_name(enum event_t type)
55 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
56 return type_name[type];
62 static int notify_static(struct event *ev)
64 struct evspy_info *start =
65 ll_entry_start(struct evspy_info, evspy_info);
66 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
67 struct evspy_info *spy;
69 for (spy = start; spy != start + n_ents; spy++) {
70 if (spy->type == ev->type) {
73 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
74 event_type_name(ev->type), event_spy_id(spy));
75 if (spy->flags & EVSPYF_SIMPLE) {
76 const struct evspy_info_simple *simple;
78 simple = (struct evspy_info_simple *)spy;
81 ret = spy->func(NULL, ev);
85 * TODO: Handle various return codes to
87 * - claim an event (no others will see it)
88 * - return an error from the event
91 return log_msg_ret("spy", ret);
98 static int notify_dynamic(struct event *ev)
100 struct event_state *state = gd_event_state();
101 struct event_spy *spy, *next;
103 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
104 if (spy->type == ev->type) {
107 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
108 event_type_name(ev->type), spy->id);
109 ret = spy->func(spy->ctx, ev);
112 * TODO: Handle various return codes to
114 * - claim an event (no others will see it)
115 * - return an error from the event
118 return log_msg_ret("spy", ret);
125 int event_notify(enum event_t type, void *data, int size)
131 if (size > sizeof(event.data))
132 return log_msg_ret("size", -E2BIG);
133 memcpy(&event.data, data, size);
135 ret = notify_static(&event);
137 return log_msg_ret("sta", ret);
139 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
140 ret = notify_dynamic(&event);
142 return log_msg_ret("dyn", ret);
148 int event_notify_null(enum event_t type)
150 return event_notify(type, NULL, 0);
153 void event_show_spy_list(void)
155 struct evspy_info *start =
156 ll_entry_start(struct evspy_info, evspy_info);
157 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
158 struct evspy_info *spy;
159 const int size = sizeof(ulong) * 2;
161 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
162 for (spy = start; spy != start + n_ents; spy++) {
163 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
164 spy->type, event_type_name(spy->type), size, spy->func,
169 #if IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)
170 int event_manual_reloc(void)
172 struct evspy_info *spy, *end;
174 spy = ll_entry_start(struct evspy_info, evspy_info);
175 end = ll_entry_end(struct evspy_info, evspy_info);
176 for (; spy < end; spy++)
177 MANUAL_RELOC(spy->func);
183 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
184 static void spy_free(struct event_spy *spy)
186 list_del(&spy->sibling_node);
189 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
191 struct event_state *state = gd_event_state();
192 struct event_spy *spy;
194 spy = malloc(sizeof(*spy));
196 return log_msg_ret("alloc", -ENOMEM);
202 list_add_tail(&spy->sibling_node, &state->spy_head);
207 int event_uninit(void)
209 struct event_state *state = gd_event_state();
210 struct event_spy *spy, *next;
212 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
220 struct event_state *state = gd_event_state();
222 INIT_LIST_HEAD(&state->spy_head);
226 #endif /* EVENT_DYNAMIC */