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
13 #include <dm/ofnode_decl.h>
14 #include <linux/types.h>
17 * enum event_t - Types of events supported by U-Boot
19 * @EVT_DM_PRE_PROBE: Device is about to be probed
25 /* Events related to driver model */
38 /* Device tree fixups before booting */
41 /* To be called once, before calling main_loop() */
49 * struct event_data_test - test data
51 * @signal: A value to update the state with
53 struct event_data_test {
58 * struct event_dm - driver model event
60 * @dev: Device this event relates to
67 * struct event_fpga_load - fpga load event
69 * @buf: The buffer that was loaded into the fpga
70 * @bsize: The size of the buffer that was loaded into the fpga
71 * @result: Result of the load operation
73 struct event_fpga_load {
80 * struct event_ft_fixup - FDT fixup before booting
82 * @tree: tree to update
83 * @images: images which are being booted
85 struct event_ft_fixup {
87 struct bootm_headers *images;
92 * struct event - an event that can be sent and received
95 * @data: Data for this particular event
99 union event_data data;
102 /* Flags for event spy */
104 EVSPYF_SIMPLE = 1 << 0,
107 /** Function type for event handlers */
108 typedef int (*event_handler_t)(void *ctx, struct event *event);
110 /** Function type for simple event handlers */
111 typedef int (*event_handler_simple_t)(void);
114 * struct evspy_info - information about an event spy
116 * @func: Function to call when the event is activated (must be first)
118 * @flag: Flags for this spy
119 * @id: Event id string
122 event_handler_t func;
125 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
131 * struct evspy_info_simple - information about an event spy
133 * THis is the 'simple' record, the only difference being the handler function
135 * @func: Function to call when the event is activated (must be first)
137 * @flag: Flags for this spy
138 * @id: Event id string
140 struct evspy_info_simple {
141 event_handler_simple_t func;
144 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
149 /* Declare a new event spy */
150 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
151 #define _ESPY_REC(_type, _func) { _func, _type, 0, #_func, }
152 #define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE, #_func, }
154 #define _ESPY_REC(_type, _func) { _func, _type, }
155 #define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE }
158 static inline const char *event_spy_id(struct evspy_info *spy)
160 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
168 * It seems that LTO will drop list entries if it decides they are not used,
169 * although the conditions that cause this are unclear.
171 * The example found is the following:
173 * static int sandbox_misc_init_f(void *ctx, struct event *event)
175 * return sandbox_early_getopt_check();
177 * EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
179 * where EVENT_SPY uses ll_entry_declare()
181 * In this case, LTO decides to drop the sandbox_misc_init_f() function
182 * (which is fine) but then drops the linker-list entry too. This means
183 * that the code no longer works, in this case sandbox no-longer checks its
184 * command-line arguments properly.
186 * Without LTO, the KEEP() command in the .lds file is enough to keep the
187 * entry around. But with LTO it seems that the entry has already been
188 * dropped before the link script is considered.
190 * The only solution I can think of is to mark linker-list entries as 'used'
191 * using an attribute. This should be safe, since we don't actually want to drop
192 * any of these. However this does slightly limit LTO's optimisation choices.
194 * Another issue has come up, only with clang: using 'static' makes it throw
195 * away the linker-list entry sometimes, e.g. with the EVT_FT_FIXUP entry in
196 * vbe_simple.c - so for now, make it global.
198 #define EVENT_SPY_FULL(_type, _func) \
199 __used ll_entry_declare(struct evspy_info, _type ## _3_ ## _func, \
200 evspy_info) = _ESPY_REC(_type, _func)
202 /* Simple spy with no function arguemnts */
203 #define EVENT_SPY_SIMPLE(_type, _func) \
204 __used ll_entry_declare(struct evspy_info_simple, \
205 _type ## _3_ ## _func, \
206 evspy_info) = _ESPY_REC_SIMPLE(_type, _func)
209 * event_register - register a new spy
212 * @type: Event type to subscribe to
213 * @func: Function to call when the event is sent
214 * @ctx: Context to pass to the function
215 * @return 0 if OK, -ve on error
217 int event_register(const char *id, enum event_t type, event_handler_t func,
220 /** event_show_spy_list( - Show a list of event spies */
221 void event_show_spy_list(void);
224 * event_manual_reloc() - Relocate event handler pointers
226 * Relocate event handler pointers for all static event spies. It is called
227 * during the generic board init sequence, after relocation.
231 int event_manual_reloc(void);
234 * event_type_name() - Get the name of an event type
236 * @type: Type to check
237 * Return: Name of event, or "(unknown)" if not known
239 const char *event_type_name(enum event_t type);
242 * event_notify() - notify spies about an event
244 * It is possible to pass in union event_data here but that may not be
245 * convenient if the data is elsewhere, or is one of the members of the union.
246 * So this uses a void * for @data, with a separate @size.
249 * @data: Event data to be sent (e.g. union_event_data)
250 * @size: Size of data in bytes
251 * @return 0 if OK, -ve on error
253 int event_notify(enum event_t type, void *data, int size);
255 #if CONFIG_IS_ENABLED(EVENT)
257 * event_notify_null() - notify spies about an event
259 * Data is NULL and the size is 0
262 * @return 0 if OK, -ve on error
264 int event_notify_null(enum event_t type);
266 static inline int event_notify_null(enum event_t type)
272 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
274 * event_uninit() - Clean up dynamic events
276 * This removes all dynamic event handlers
278 int event_uninit(void);
281 * event_uninit() - Set up dynamic events
283 * Init a list of dynamic event handlers, so that these can be added as
286 int event_init(void);
288 static inline int event_uninit(void)
293 static inline int event_init(void)