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 */
40 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
43 static const char *event_type_name(enum event_t type)
45 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
46 return type_name[type];
52 static int notify_static(struct event *ev)
54 struct evspy_info *start =
55 ll_entry_start(struct evspy_info, evspy_info);
56 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
57 struct evspy_info *spy;
59 for (spy = start; spy != start + n_ents; spy++) {
60 if (spy->type == ev->type) {
63 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
64 event_type_name(ev->type), event_spy_id(spy));
65 ret = spy->func(NULL, ev);
68 * TODO: Handle various return codes to
70 * - claim an event (no others will see it)
71 * - return an error from the event
74 return log_msg_ret("spy", ret);
81 static int notify_dynamic(struct event *ev)
83 struct event_state *state = gd_event_state();
84 struct event_spy *spy, *next;
86 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
87 if (spy->type == ev->type) {
90 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
91 event_type_name(ev->type), spy->id);
92 ret = spy->func(spy->ctx, ev);
95 * TODO: Handle various return codes to
97 * - claim an event (no others will see it)
98 * - return an error from the event
101 return log_msg_ret("spy", ret);
108 int event_notify(enum event_t type, void *data, int size)
114 if (size > sizeof(event.data))
115 return log_msg_ret("size", -E2BIG);
116 memcpy(&event.data, data, size);
118 ret = notify_static(&event);
120 return log_msg_ret("dyn", ret);
122 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
123 ret = notify_dynamic(&event);
125 return log_msg_ret("dyn", ret);
131 int event_notify_null(enum event_t type)
133 return event_notify(type, NULL, 0);
136 void event_show_spy_list(void)
138 struct evspy_info *start =
139 ll_entry_start(struct evspy_info, evspy_info);
140 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
141 struct evspy_info *spy;
142 const int size = sizeof(ulong) * 2;
144 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
145 for (spy = start; spy != start + n_ents; spy++) {
146 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
147 spy->type, event_type_name(spy->type), size, spy->func,
152 #if CONFIG_IS_ENABLED(NEEDS_MANUAL_RELOC)
153 int event_manual_reloc(void)
155 struct evspy_info *spy, *end;
157 spy = ll_entry_start(struct evspy_info, evspy_info);
158 end = ll_entry_end(struct evspy_info, evspy_info);
159 for (; spy < end; spy++)
160 MANUAL_RELOC(spy->func);
166 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
167 static void spy_free(struct event_spy *spy)
169 list_del(&spy->sibling_node);
172 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
174 struct event_state *state = gd_event_state();
175 struct event_spy *spy;
177 spy = malloc(sizeof(*spy));
179 return log_msg_ret("alloc", -ENOMEM);
185 list_add_tail(&spy->sibling_node, &state->spy_head);
190 int event_uninit(void)
192 struct event_state *state = gd_event_state();
193 struct event_spy *spy, *next;
195 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
203 struct event_state *state = gd_event_state();
205 INIT_LIST_HEAD(&state->spy_head);
209 #endif /* EVENT_DYNAMIC */