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
14 * enum event_t - Types of events supported by U-Boot
16 * @EVT_DM_PRE_PROBE: Device is about to be probed
27 * struct event_data_test - test data
29 * @signal: A value to update the state with
31 struct event_data_test {
37 * struct event - an event that can be sent and received
40 * @data: Data for this particular event
44 union event_data data;
47 /** Function type for event handlers */
48 typedef int (*event_handler_t)(void *ctx, struct event *event);
51 * struct evspy_info - information about an event spy
53 * @func: Function to call when the event is activated (must be first)
55 * @id: Event id string
60 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
65 /* Declare a new event spy */
66 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
67 #define _ESPY_REC(_type, _func) { _func, _type, #_func, }
69 #define _ESPY_REC(_type, _func) { _func, _type, }
72 static inline const char *event_spy_id(struct evspy_info *spy)
74 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
82 * It seems that LTO will drop list entries if it decides they are not used,
83 * although the conditions that cause this are unclear.
85 * The example found is the following:
87 * static int sandbox_misc_init_f(void *ctx, struct event *event)
89 * return sandbox_early_getopt_check();
91 * EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
93 * where EVENT_SPY uses ll_entry_declare()
95 * In this case, LTO decides to drop the sandbox_misc_init_f() function
96 * (which is fine) but then drops the linker-list entry too. This means
97 * that the code no longer works, in this case sandbox no-longer checks its
98 * command-line arguments properly.
100 * Without LTO, the KEEP() command in the .lds file is enough to keep the
101 * entry around. But with LTO it seems that the entry has already been
102 * dropped before the link script is considered.
104 * The only solution I can think of is to mark linker-list entries as 'used'
105 * using an attribute. This should be safe, since we don't actually want to drop
106 * any of these. However this does slightly limit LTO's optimisation choices.
108 #define EVENT_SPY(_type, _func) \
109 static __attribute__((used)) ll_entry_declare(struct evspy_info, \
110 _type, evspy_info) = \
111 _ESPY_REC(_type, _func)
114 * event_register - register a new spy
117 * @type: Event type to subscribe to
118 * @func: Function to call when the event is sent
119 * @ctx: Context to pass to the function
120 * @return 0 if OK, -ve on error
122 int event_register(const char *id, enum event_t type, event_handler_t func,
125 #if CONFIG_IS_ENABLED(EVENT)
127 * event_notify() - notify spies about an event
129 * It is possible to pass in union event_data here but that may not be
130 * convenient if the data is elsewhere, or is one of the members of the union.
131 * So this uses a void * for @data, with a separate @size.
134 * @data: Event data to be sent (e.g. union_event_data)
135 * @size: Size of data in bytes
136 * @return 0 if OK, -ve on error
138 int event_notify(enum event_t type, void *data, int size);
141 * event_notify_null() - notify spies about an event
143 * Data is NULL and the size is 0
146 * @return 0 if OK, -ve on error
148 int event_notify_null(enum event_t type);
150 static inline int event_notify(enum event_t type, void *data, int size)
155 static inline int event_notify_null(enum event_t type)
161 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
163 * event_uninit() - Clean up dynamic events
165 * This removes all dynamic event handlers
167 int event_uninit(void);
170 * event_uninit() - Set up dynamic events
172 * Init a list of dynamic event handlers, so that these can be added as
175 int event_init(void);
177 static inline int event_uninit(void)
182 static inline int event_init(void)