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>
13 #include <dm/ofnode_decl.h>
16 * enum event_t - Types of events supported by U-Boot
18 * @EVT_DM_PRE_PROBE: Device is about to be probed
24 /* Events related to driver model */
34 /* Device tree fixups before booting */
42 * struct event_data_test - test data
44 * @signal: A value to update the state with
46 struct event_data_test {
51 * struct event_dm - driver model event
53 * @dev: Device this event relates to
60 * struct event_ft_fixup - FDT fixup before booting
62 * @tree: tree to update
64 struct event_ft_fixup {
70 * struct event - an event that can be sent and received
73 * @data: Data for this particular event
77 union event_data data;
80 /** Function type for event handlers */
81 typedef int (*event_handler_t)(void *ctx, struct event *event);
84 * struct evspy_info - information about an event spy
86 * @func: Function to call when the event is activated (must be first)
88 * @id: Event id string
93 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
98 /* Declare a new event spy */
99 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
100 #define _ESPY_REC(_type, _func) { _func, _type, #_func, }
102 #define _ESPY_REC(_type, _func) { _func, _type, }
105 static inline const char *event_spy_id(struct evspy_info *spy)
107 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
115 * It seems that LTO will drop list entries if it decides they are not used,
116 * although the conditions that cause this are unclear.
118 * The example found is the following:
120 * static int sandbox_misc_init_f(void *ctx, struct event *event)
122 * return sandbox_early_getopt_check();
124 * EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
126 * where EVENT_SPY uses ll_entry_declare()
128 * In this case, LTO decides to drop the sandbox_misc_init_f() function
129 * (which is fine) but then drops the linker-list entry too. This means
130 * that the code no longer works, in this case sandbox no-longer checks its
131 * command-line arguments properly.
133 * Without LTO, the KEEP() command in the .lds file is enough to keep the
134 * entry around. But with LTO it seems that the entry has already been
135 * dropped before the link script is considered.
137 * The only solution I can think of is to mark linker-list entries as 'used'
138 * using an attribute. This should be safe, since we don't actually want to drop
139 * any of these. However this does slightly limit LTO's optimisation choices.
141 * Another issue has come up, only with clang: using 'static' makes it throw
142 * away the linker-list entry sometimes, e.g. with the EVT_FT_FIXUP entry in
143 * vbe_simple.c - so for now, make it global.
145 #define EVENT_SPY(_type, _func) \
146 __used ll_entry_declare(struct evspy_info, _type ## _3_ ## _func, \
147 evspy_info) = _ESPY_REC(_type, _func)
150 * event_register - register a new spy
153 * @type: Event type to subscribe to
154 * @func: Function to call when the event is sent
155 * @ctx: Context to pass to the function
156 * @return 0 if OK, -ve on error
158 int event_register(const char *id, enum event_t type, event_handler_t func,
161 /** event_show_spy_list( - Show a list of event spies */
162 void event_show_spy_list(void);
165 * event_manual_reloc() - Relocate event handler pointers
167 * Relocate event handler pointers for all static event spies. It is called
168 * during the generic board init sequence, after relocation.
172 int event_manual_reloc(void);
175 * event_notify() - notify spies about an event
177 * It is possible to pass in union event_data here but that may not be
178 * convenient if the data is elsewhere, or is one of the members of the union.
179 * So this uses a void * for @data, with a separate @size.
182 * @data: Event data to be sent (e.g. union_event_data)
183 * @size: Size of data in bytes
184 * @return 0 if OK, -ve on error
186 int event_notify(enum event_t type, void *data, int size);
188 #if CONFIG_IS_ENABLED(EVENT)
190 * event_notify_null() - notify spies about an event
192 * Data is NULL and the size is 0
195 * @return 0 if OK, -ve on error
197 int event_notify_null(enum event_t type);
199 static inline int event_notify_null(enum event_t type)
205 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
207 * event_uninit() - Clean up dynamic events
209 * This removes all dynamic event handlers
211 int event_uninit(void);
214 * event_uninit() - Set up dynamic events
216 * Init a list of dynamic event handlers, so that these can be added as
219 int event_init(void);
221 static inline int event_uninit(void)
226 static inline int event_init(void)