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 */
48 /* main loop events */
52 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
55 const char *event_type_name(enum event_t type)
57 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
58 return type_name[type];
64 static int notify_static(struct event *ev)
66 struct evspy_info *start =
67 ll_entry_start(struct evspy_info, evspy_info);
68 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
69 struct evspy_info *spy;
71 for (spy = start; spy != start + n_ents; spy++) {
72 if (spy->type == ev->type) {
75 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
76 event_type_name(ev->type), event_spy_id(spy));
77 if (spy->flags & EVSPYF_SIMPLE) {
78 const struct evspy_info_simple *simple;
80 simple = (struct evspy_info_simple *)spy;
83 ret = spy->func(NULL, ev);
87 * TODO: Handle various return codes to
89 * - claim an event (no others will see it)
90 * - return an error from the event
93 return log_msg_ret("spy", ret);
100 static int notify_dynamic(struct event *ev)
102 struct event_state *state = gd_event_state();
103 struct event_spy *spy, *next;
105 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
106 if (spy->type == ev->type) {
109 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
110 event_type_name(ev->type), spy->id);
111 ret = spy->func(spy->ctx, ev);
114 * TODO: Handle various return codes to
116 * - claim an event (no others will see it)
117 * - return an error from the event
120 return log_msg_ret("spy", ret);
127 int event_notify(enum event_t type, void *data, int size)
133 if (size > sizeof(event.data))
134 return log_msg_ret("size", -E2BIG);
135 memcpy(&event.data, data, size);
137 ret = notify_static(&event);
139 return log_msg_ret("sta", ret);
141 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
142 ret = notify_dynamic(&event);
144 return log_msg_ret("dyn", ret);
150 int event_notify_null(enum event_t type)
152 return event_notify(type, NULL, 0);
155 void event_show_spy_list(void)
157 struct evspy_info *start =
158 ll_entry_start(struct evspy_info, evspy_info);
159 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
160 struct evspy_info *spy;
161 const int size = sizeof(ulong) * 2;
163 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
164 for (spy = start; spy != start + n_ents; spy++) {
165 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
166 spy->type, event_type_name(spy->type), size, spy->func,
171 #if IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)
172 int event_manual_reloc(void)
174 struct evspy_info *spy, *end;
176 spy = ll_entry_start(struct evspy_info, evspy_info);
177 end = ll_entry_end(struct evspy_info, evspy_info);
178 for (; spy < end; spy++)
179 MANUAL_RELOC(spy->func);
185 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
186 static void spy_free(struct event_spy *spy)
188 list_del(&spy->sibling_node);
191 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
193 struct event_state *state = gd_event_state();
194 struct event_spy *spy;
196 spy = malloc(sizeof(*spy));
198 return log_msg_ret("alloc", -ENOMEM);
204 list_add_tail(&spy->sibling_node, &state->spy_head);
209 int event_uninit(void)
211 struct event_state *state = gd_event_state();
212 struct event_spy *spy, *next;
214 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
222 struct event_state *state = gd_event_state();
224 INIT_LIST_HEAD(&state->spy_head);
228 #endif /* EVENT_DYNAMIC */