blob: dc61b9672f32763d2545efec0f22e87ec8a7178d [file] [log] [blame]
Simon Glass87a5d1b2022-03-04 08:43:00 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Events provide a general-purpose way to react to / subscribe to changes
4 * within U-Boot
5 *
6 * Copyright 2021 Google LLC
7 * Written by Simon Glass <sjg@chromium.org>
8 */
9
10#define LOG_CATEGORY LOGC_EVENT
11
12#include <common.h>
13#include <event.h>
14#include <event_internal.h>
15#include <log.h>
16#include <linker_lists.h>
17#include <malloc.h>
18#include <asm/global_data.h>
19#include <linux/list.h>
Ovidiu Panaitcebc8162022-05-15 21:40:29 +030020#include <relocate.h>
Simon Glass87a5d1b2022-03-04 08:43:00 -070021
22DECLARE_GLOBAL_DATA_PTR;
23
24#if CONFIG_IS_ENABLED(EVENT_DEBUG)
25const char *const type_name[] = {
26 "none",
27 "test",
Simon Glass5b896ed2022-03-04 08:43:03 -070028
29 /* Events related to driver model */
Jaehoon Chungb4966202023-08-01 19:17:00 +090030 "dm_post_init_f",
Chanho Park27c7a622023-08-18 14:11:02 +090031 "dm_post_init_r",
Simon Glass5b896ed2022-03-04 08:43:03 -070032 "dm_pre_probe",
33 "dm_post_probe",
34 "dm_pre_remove",
35 "dm_post_remove",
Simon Glass42fdceb2022-03-04 08:43:04 -070036
37 /* init hooks */
38 "misc_init_f",
Simon Glass13a7db92023-08-21 21:16:59 -060039 "fsp_init_r",
Artur Rojek6092ce52023-10-18 16:00:57 +020040 "settings_r",
Simon Glass91caa3b2023-08-21 21:17:01 -060041 "last_stage_init",
Simon Glass98887ab2022-07-30 15:52:31 -060042
Christian Taedckea1190b42023-07-20 09:27:24 +020043 /* Fpga load hook */
44 "fpga_load",
45
Simon Glass98887ab2022-07-30 15:52:31 -060046 /* fdt hooks */
47 "ft_fixup",
Sughosh Ganu467bad52022-10-21 18:16:01 +053048
49 /* main loop events */
50 "main_loop",
Simon Glass87a5d1b2022-03-04 08:43:00 -070051};
52
53_Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
54#endif
55
Simon Glassfb7dfca2023-08-21 21:16:53 -060056const char *event_type_name(enum event_t type)
Simon Glass87a5d1b2022-03-04 08:43:00 -070057{
58#if CONFIG_IS_ENABLED(EVENT_DEBUG)
59 return type_name[type];
60#else
61 return "(unknown)";
62#endif
63}
64
65static int notify_static(struct event *ev)
66{
67 struct evspy_info *start =
68 ll_entry_start(struct evspy_info, evspy_info);
69 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
70 struct evspy_info *spy;
71
72 for (spy = start; spy != start + n_ents; spy++) {
73 if (spy->type == ev->type) {
74 int ret;
75
76 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
77 event_type_name(ev->type), event_spy_id(spy));
Simon Glassba5e3e12023-08-21 21:16:48 -060078 if (spy->flags & EVSPYF_SIMPLE) {
79 const struct evspy_info_simple *simple;
80
81 simple = (struct evspy_info_simple *)spy;
82 ret = simple->func();
83 } else {
84 ret = spy->func(NULL, ev);
85 }
Simon Glass87a5d1b2022-03-04 08:43:00 -070086
87 /*
88 * TODO: Handle various return codes to
89 *
90 * - claim an event (no others will see it)
91 * - return an error from the event
92 */
93 if (ret)
94 return log_msg_ret("spy", ret);
95 }
96 }
97
98 return 0;
99}
100
101static int notify_dynamic(struct event *ev)
102{
103 struct event_state *state = gd_event_state();
104 struct event_spy *spy, *next;
105
106 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
107 if (spy->type == ev->type) {
108 int ret;
109
110 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
111 event_type_name(ev->type), spy->id);
112 ret = spy->func(spy->ctx, ev);
113
114 /*
115 * TODO: Handle various return codes to
116 *
117 * - claim an event (no others will see it)
118 * - return an error from the event
119 */
120 if (ret)
121 return log_msg_ret("spy", ret);
122 }
123 }
124
125 return 0;
126}
127
128int event_notify(enum event_t type, void *data, int size)
129{
130 struct event event;
131 int ret;
132
133 event.type = type;
134 if (size > sizeof(event.data))
135 return log_msg_ret("size", -E2BIG);
136 memcpy(&event.data, data, size);
137
138 ret = notify_static(&event);
139 if (ret)
Simon Glass19efd432023-01-17 10:47:31 -0700140 return log_msg_ret("sta", ret);
Simon Glass87a5d1b2022-03-04 08:43:00 -0700141
142 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
143 ret = notify_dynamic(&event);
144 if (ret)
145 return log_msg_ret("dyn", ret);
146 }
147
148 return 0;
149}
150
151int event_notify_null(enum event_t type)
152{
153 return event_notify(type, NULL, 0);
154}
155
156void event_show_spy_list(void)
157{
158 struct evspy_info *start =
159 ll_entry_start(struct evspy_info, evspy_info);
160 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
161 struct evspy_info *spy;
162 const int size = sizeof(ulong) * 2;
163
164 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
165 for (spy = start; spy != start + n_ents; spy++) {
166 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
167 spy->type, event_type_name(spy->type), size, spy->func,
168 event_spy_id(spy));
169 }
170}
171
172#if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
173static void spy_free(struct event_spy *spy)
174{
175 list_del(&spy->sibling_node);
176}
177
178int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
179{
180 struct event_state *state = gd_event_state();
181 struct event_spy *spy;
182
Simon Glass87a5d1b2022-03-04 08:43:00 -0700183 spy = malloc(sizeof(*spy));
184 if (!spy)
185 return log_msg_ret("alloc", -ENOMEM);
186
187 spy->id = id;
188 spy->type = type;
189 spy->func = func;
190 spy->ctx = ctx;
191 list_add_tail(&spy->sibling_node, &state->spy_head);
192
193 return 0;
194}
195
196int event_uninit(void)
197{
198 struct event_state *state = gd_event_state();
199 struct event_spy *spy, *next;
200
201 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
202 spy_free(spy);
203
204 return 0;
205}
206
207int event_init(void)
208{
209 struct event_state *state = gd_event_state();
210
211 INIT_LIST_HEAD(&state->spy_head);
212
213 return 0;
214}
215#endif /* EVENT_DYNAMIC */