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 */
47 /* main loop events */
51 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
54 const char *event_type_name(enum event_t type)
56 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
57 return type_name[type];
63 static int notify_static(struct event *ev)
65 struct evspy_info *start =
66 ll_entry_start(struct evspy_info, evspy_info);
67 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
68 struct evspy_info *spy;
70 for (spy = start; spy != start + n_ents; spy++) {
71 if (spy->type == ev->type) {
74 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
75 event_type_name(ev->type), event_spy_id(spy));
76 if (spy->flags & EVSPYF_SIMPLE) {
77 const struct evspy_info_simple *simple;
79 simple = (struct evspy_info_simple *)spy;
82 ret = spy->func(NULL, ev);
86 * TODO: Handle various return codes to
88 * - claim an event (no others will see it)
89 * - return an error from the event
92 return log_msg_ret("spy", ret);
99 static int notify_dynamic(struct event *ev)
101 struct event_state *state = gd_event_state();
102 struct event_spy *spy, *next;
104 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
105 if (spy->type == ev->type) {
108 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
109 event_type_name(ev->type), spy->id);
110 ret = spy->func(spy->ctx, ev);
113 * TODO: Handle various return codes to
115 * - claim an event (no others will see it)
116 * - return an error from the event
119 return log_msg_ret("spy", ret);
126 int event_notify(enum event_t type, void *data, int size)
132 if (size > sizeof(event.data))
133 return log_msg_ret("size", -E2BIG);
134 memcpy(&event.data, data, size);
136 ret = notify_static(&event);
138 return log_msg_ret("sta", ret);
140 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
141 ret = notify_dynamic(&event);
143 return log_msg_ret("dyn", ret);
149 int event_notify_null(enum event_t type)
151 return event_notify(type, NULL, 0);
154 void event_show_spy_list(void)
156 struct evspy_info *start =
157 ll_entry_start(struct evspy_info, evspy_info);
158 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
159 struct evspy_info *spy;
160 const int size = sizeof(ulong) * 2;
162 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
163 for (spy = start; spy != start + n_ents; spy++) {
164 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
165 spy->type, event_type_name(spy->type), size, spy->func,
170 #if IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)
171 int event_manual_reloc(void)
173 struct evspy_info *spy, *end;
175 spy = ll_entry_start(struct evspy_info, evspy_info);
176 end = ll_entry_end(struct evspy_info, evspy_info);
177 for (; spy < end; spy++)
178 MANUAL_RELOC(spy->func);
184 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
185 static void spy_free(struct event_spy *spy)
187 list_del(&spy->sibling_node);
190 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
192 struct event_state *state = gd_event_state();
193 struct event_spy *spy;
195 spy = malloc(sizeof(*spy));
197 return log_msg_ret("alloc", -ENOMEM);
203 list_add_tail(&spy->sibling_node, &state->spy_head);
208 int event_uninit(void)
210 struct event_state *state = gd_event_state();
211 struct event_spy *spy, *next;
213 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
221 struct event_state *state = gd_event_state();
223 INIT_LIST_HEAD(&state->spy_head);
227 #endif /* EVENT_DYNAMIC */