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
23 * @EVT_NONE: This zero value is not used for events.
28 * @EVT_TEST: This event is used in unit tests.
33 * @EVT_DM_POST_INIT_F:
34 * This event is triggered after pre-relocation initialization of the
35 * driver model. Its parameter is NULL.
36 * A non-zero return code from the event handler let's the boot process
42 * @EVT_DM_POST_INIT_R:
43 * This event is triggered after post-relocation initialization of the
44 * driver model. Its parameter is NULL.
45 * A non-zero return code from the event handler let's the boot process
52 * This event is triggered before probing a device. Its parameter is the
53 * device to be probed.
54 * A non-zero return code from the event handler lets the device not
61 * This event is triggered after probing a device. Its parameter is the
62 * device that was probed.
63 * A non-zero return code from the event handler leaves the device in
64 * the unprobed state and therefore not usable.
70 * This event is triggered after removing a device. Its parameter is
71 * the device to be removed.
72 * A non-zero return code from the event handler stops the removal of
73 * the device before any changes.
78 * @EVT_DM_POST_REMOVE:
79 * This event is triggered before removing a device. Its parameter is
80 * the device that was removed.
81 * A non-zero return code stops from the event handler the removal of
82 * the device after all removal changes. The previous state is not
83 * restored. All children will be gone and the device may not be
90 * This event is triggered during the initialization sequence before
91 * relocation. Its parameter is NULL.
92 * A non-zero return code from the event handler let's the boot process
98 * Emitted before relocation to set up Firmware Support Package
100 * Where U-Boot relies on binary blobs to handle part of the system
101 * init, this event can be used to set up the blobs. This is used on
102 * some Intel platforms
107 * Emitted just before jumping to the main loop
109 * Some boards need to perform initialisation immediately before control
110 * is passed to the command-line interpreter (e.g. for init that depend
111 * on later phases in the init sequence).
113 * Some parts can be only initialized if all others (like Interrupts)
114 * are up and running (e.g. the PC-style ISA keyboard).
120 * The FPGA load hook is called after loading an FPGA with a new binary.
121 * Its parameter is of type struct event_fpga_load and contains
122 * information about the loaded image.
128 * This event is triggered during device-tree fix up after all
129 * other device-tree fixups have been executed.
130 * Its parameter is of type struct event_ft_fixup which contains
131 * the address of the device-tree to fix up and the list of images to be
133 * A non-zero return code from the event handler let's booting the
140 * This event is triggered immediately before calling main_loop() which
141 * is the entry point of the command line. Its parameter is NULL.
142 * A non-zero return value causes the boot to fail.
148 * This constants holds the maximum event number + 1 and is used when
149 * looping over all event classes.
156 * struct event_data_test - test data
158 * @signal: A value to update the state with
160 struct event_data_test {
165 * struct event_dm - driver model event
167 * @dev: Device this event relates to
174 * struct event_fpga_load - fpga load event
176 * @buf: The buffer that was loaded into the fpga
177 * @bsize: The size of the buffer that was loaded into the fpga
178 * @result: Result of the load operation
180 struct event_fpga_load {
187 * struct event_ft_fixup - FDT fixup before booting
189 * @tree: tree to update
190 * @images: images which are being booted
192 struct event_ft_fixup {
194 struct bootm_headers *images;
199 * struct event - an event that can be sent and received
202 * @data: Data for this particular event
206 union event_data data;
209 /* Flags for event spy */
211 EVSPYF_SIMPLE = 1 << 0,
214 /** Function type for event handlers */
215 typedef int (*event_handler_t)(void *ctx, struct event *event);
217 /** Function type for simple event handlers */
218 typedef int (*event_handler_simple_t)(void);
221 * struct evspy_info - information about an event spy
223 * @func: Function to call when the event is activated (must be first)
225 * @flag: Flags for this spy
226 * @id: Event id string
229 event_handler_t func;
232 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
238 * struct evspy_info_simple - information about an event spy
240 * THis is the 'simple' record, the only difference being the handler function
242 * @func: Function to call when the event is activated (must be first)
244 * @flag: Flags for this spy
245 * @id: Event id string
247 struct evspy_info_simple {
248 event_handler_simple_t func;
251 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
256 /* Declare a new event spy */
257 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
258 #define _ESPY_REC(_type, _func) { _func, _type, 0, #_func, }
259 #define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE, #_func, }
261 #define _ESPY_REC(_type, _func) { _func, _type, }
262 #define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE }
265 static inline const char *event_spy_id(struct evspy_info *spy)
267 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
275 * It seems that LTO will drop list entries if it decides they are not used,
276 * although the conditions that cause this are unclear.
278 * The example found is the following:
280 * static int sandbox_misc_init_f(void *ctx, struct event *event)
282 * return sandbox_early_getopt_check();
284 * EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
286 * where EVENT_SPY uses ll_entry_declare()
288 * In this case, LTO decides to drop the sandbox_misc_init_f() function
289 * (which is fine) but then drops the linker-list entry too. This means
290 * that the code no longer works, in this case sandbox no-longer checks its
291 * command-line arguments properly.
293 * Without LTO, the KEEP() command in the .lds file is enough to keep the
294 * entry around. But with LTO it seems that the entry has already been
295 * dropped before the link script is considered.
297 * The only solution I can think of is to mark linker-list entries as 'used'
298 * using an attribute. This should be safe, since we don't actually want to drop
299 * any of these. However this does slightly limit LTO's optimisation choices.
301 * Another issue has come up, only with clang: using 'static' makes it throw
302 * away the linker-list entry sometimes, e.g. with the EVT_FT_FIXUP entry in
303 * vbe_simple.c - so for now, make it global.
305 #define EVENT_SPY_FULL(_type, _func) \
306 __used ll_entry_declare(struct evspy_info, _type ## _3_ ## _func, \
307 evspy_info) = _ESPY_REC(_type, _func)
309 /* Simple spy with no function arguemnts */
310 #define EVENT_SPY_SIMPLE(_type, _func) \
311 __used ll_entry_declare(struct evspy_info_simple, \
312 _type ## _3_ ## _func, \
313 evspy_info) = _ESPY_REC_SIMPLE(_type, _func)
316 * event_register - register a new spy
319 * @type: Event type to subscribe to
320 * @func: Function to call when the event is sent
321 * @ctx: Context to pass to the function
322 * @return 0 if OK, -ve on error
324 int event_register(const char *id, enum event_t type, event_handler_t func,
327 /** event_show_spy_list( - Show a list of event spies */
328 void event_show_spy_list(void);
331 * event_manual_reloc() - Relocate event handler pointers
333 * Relocate event handler pointers for all static event spies. It is called
334 * during the generic board init sequence, after relocation.
338 int event_manual_reloc(void);
341 * event_type_name() - Get the name of an event type
343 * @type: Type to check
344 * Return: Name of event, or "(unknown)" if not known
346 const char *event_type_name(enum event_t type);
349 * event_notify() - notify spies about an event
351 * It is possible to pass in union event_data here but that may not be
352 * convenient if the data is elsewhere, or is one of the members of the union.
353 * So this uses a void * for @data, with a separate @size.
356 * @data: Event data to be sent (e.g. union_event_data)
357 * @size: Size of data in bytes
358 * @return 0 if OK, -ve on error
360 int event_notify(enum event_t type, void *data, int size);
362 #if CONFIG_IS_ENABLED(EVENT)
364 * event_notify_null() - notify spies about an event
366 * Data is NULL and the size is 0
369 * @return 0 if OK, -ve on error
371 int event_notify_null(enum event_t type);
373 static inline int event_notify_null(enum event_t type)
379 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
381 * event_uninit() - Clean up dynamic events
383 * This removes all dynamic event handlers
385 int event_uninit(void);
388 * event_uninit() - Set up dynamic events
390 * Init a list of dynamic event handlers, so that these can be added as
393 int event_init(void);
395 static inline int event_uninit(void)
400 static inline int event_init(void)