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>
14 * enum event_t - Types of events supported by U-Boot
16 * @EVT_DM_PRE_PROBE: Device is about to be probed
22 /* Events related to driver model */
37 * struct event_data_test - test data
39 * @signal: A value to update the state with
41 struct event_data_test {
46 * struct event_dm - driver model event
48 * @dev: Device this event relates to
56 * struct event - an event that can be sent and received
59 * @data: Data for this particular event
63 union event_data data;
66 /** Function type for event handlers */
67 typedef int (*event_handler_t)(void *ctx, struct event *event);
70 * struct evspy_info - information about an event spy
72 * @func: Function to call when the event is activated (must be first)
74 * @id: Event id string
79 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
84 /* Declare a new event spy */
85 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
86 #define _ESPY_REC(_type, _func) { _func, _type, #_func, }
88 #define _ESPY_REC(_type, _func) { _func, _type, }
91 static inline const char *event_spy_id(struct evspy_info *spy)
93 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
101 * It seems that LTO will drop list entries if it decides they are not used,
102 * although the conditions that cause this are unclear.
104 * The example found is the following:
106 * static int sandbox_misc_init_f(void *ctx, struct event *event)
108 * return sandbox_early_getopt_check();
110 * EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
112 * where EVENT_SPY uses ll_entry_declare()
114 * In this case, LTO decides to drop the sandbox_misc_init_f() function
115 * (which is fine) but then drops the linker-list entry too. This means
116 * that the code no longer works, in this case sandbox no-longer checks its
117 * command-line arguments properly.
119 * Without LTO, the KEEP() command in the .lds file is enough to keep the
120 * entry around. But with LTO it seems that the entry has already been
121 * dropped before the link script is considered.
123 * The only solution I can think of is to mark linker-list entries as 'used'
124 * using an attribute. This should be safe, since we don't actually want to drop
125 * any of these. However this does slightly limit LTO's optimisation choices.
127 #define EVENT_SPY(_type, _func) \
128 static __attribute__((used)) ll_entry_declare(struct evspy_info, \
129 _type, evspy_info) = \
130 _ESPY_REC(_type, _func)
133 * event_register - register a new spy
136 * @type: Event type to subscribe to
137 * @func: Function to call when the event is sent
138 * @ctx: Context to pass to the function
139 * @return 0 if OK, -ve on error
141 int event_register(const char *id, enum event_t type, event_handler_t func,
144 /** event_show_spy_list( - Show a list of event spies */
145 void event_show_spy_list(void);
148 * event_manual_reloc() - Relocate event handler pointers
150 * Relocate event handler pointers for all static event spies. It is called
151 * during the generic board init sequence, after relocation.
155 int event_manual_reloc(void);
158 * event_notify() - notify spies about an event
160 * It is possible to pass in union event_data here but that may not be
161 * convenient if the data is elsewhere, or is one of the members of the union.
162 * So this uses a void * for @data, with a separate @size.
165 * @data: Event data to be sent (e.g. union_event_data)
166 * @size: Size of data in bytes
167 * @return 0 if OK, -ve on error
169 int event_notify(enum event_t type, void *data, int size);
171 #if CONFIG_IS_ENABLED(EVENT)
173 * event_notify_null() - notify spies about an event
175 * Data is NULL and the size is 0
178 * @return 0 if OK, -ve on error
180 int event_notify_null(enum event_t type);
182 static inline int event_notify_null(enum event_t type)
188 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
190 * event_uninit() - Clean up dynamic events
192 * This removes all dynamic event handlers
194 int event_uninit(void);
197 * event_uninit() - Set up dynamic events
199 * Init a list of dynamic event handlers, so that these can be added as
202 int event_init(void);
204 static inline int event_uninit(void)
209 static inline int event_init(void)