blob: 85269aa317aa08f4b563f91330a8ddf4fd362dfb [file] [log] [blame]
Simon Glassaa4bce92022-03-04 08:43:00 -07001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Events provide a general-purpose way to react to / subscribe to changes
4 * within U-Boot
5 *
6 * Copyright 2021 Google LLC
7 * Written by Simon Glass <sjg@chromium.org>
8 */
9
10#ifndef __event_h
11#define __event_h
12
Simon Glass4305fe72022-07-30 15:52:31 -060013#include <dm/ofnode_decl.h>
Christian Taedckedf369812023-07-20 09:27:24 +020014#include <linux/types.h>
Simon Glass4305fe72022-07-30 15:52:31 -060015
Simon Glassaa4bce92022-03-04 08:43:00 -070016/**
17 * enum event_t - Types of events supported by U-Boot
18 *
19 * @EVT_DM_PRE_PROBE: Device is about to be probed
20 */
21enum event_t {
22 EVT_NONE,
23 EVT_TEST,
24
Simon Glass3ee337a2022-03-04 08:43:03 -070025 /* Events related to driver model */
Simon Glass93074012023-05-04 16:50:45 -060026 EVT_DM_POST_INIT_F,
Simon Glass3ee337a2022-03-04 08:43:03 -070027 EVT_DM_PRE_PROBE,
28 EVT_DM_POST_PROBE,
29 EVT_DM_PRE_REMOVE,
30 EVT_DM_POST_REMOVE,
31
Simon Glassf1c51912022-03-04 08:43:04 -070032 /* Init hooks */
33 EVT_MISC_INIT_F,
34
Simon Glassfcebb7b2023-08-21 21:16:59 -060035 /*
36 * Emitted before relocation to set up Firmware Support Package
37 *
38 * Where U-Boot relies on binary blobs to handle part of the system
39 * init, this event can be used to set up the blobs. This is used on
40 * some Intel platforms
41 */
42 EVT_FSP_INIT_F,
43
Christian Taedckedf369812023-07-20 09:27:24 +020044 /* Fpga load hook */
45 EVT_FPGA_LOAD,
46
Simon Glass4305fe72022-07-30 15:52:31 -060047 /* Device tree fixups before booting */
48 EVT_FT_FIXUP,
49
Sughosh Ganub780fa52022-10-21 18:16:01 +053050 /* To be called once, before calling main_loop() */
51 EVT_MAIN_LOOP,
52
Simon Glassaa4bce92022-03-04 08:43:00 -070053 EVT_COUNT
54};
55
56union event_data {
57 /**
58 * struct event_data_test - test data
59 *
60 * @signal: A value to update the state with
61 */
62 struct event_data_test {
63 int signal;
64 } test;
Simon Glass3ee337a2022-03-04 08:43:03 -070065
66 /**
67 * struct event_dm - driver model event
68 *
69 * @dev: Device this event relates to
70 */
71 struct event_dm {
72 struct udevice *dev;
73 } dm;
Simon Glass4305fe72022-07-30 15:52:31 -060074
75 /**
Christian Taedckedf369812023-07-20 09:27:24 +020076 * struct event_fpga_load - fpga load event
77 *
78 * @buf: The buffer that was loaded into the fpga
79 * @bsize: The size of the buffer that was loaded into the fpga
80 * @result: Result of the load operation
81 */
82 struct event_fpga_load {
83 const void *buf;
84 size_t bsize;
85 int result;
86 } fpga_load;
87
88 /**
Simon Glass4305fe72022-07-30 15:52:31 -060089 * struct event_ft_fixup - FDT fixup before booting
90 *
91 * @tree: tree to update
Simon Glass74ba8e62022-09-06 20:26:58 -060092 * @images: images which are being booted
Simon Glass4305fe72022-07-30 15:52:31 -060093 */
94 struct event_ft_fixup {
95 oftree tree;
Simon Glass74ba8e62022-09-06 20:26:58 -060096 struct bootm_headers *images;
Simon Glass4305fe72022-07-30 15:52:31 -060097 } ft_fixup;
Simon Glassaa4bce92022-03-04 08:43:00 -070098};
99
100/**
101 * struct event - an event that can be sent and received
102 *
103 * @type: Event type
104 * @data: Data for this particular event
105 */
106struct event {
107 enum event_t type;
108 union event_data data;
109};
110
Simon Glass3bfbedd2023-08-21 21:16:48 -0600111/* Flags for event spy */
112enum evspy_flags {
113 EVSPYF_SIMPLE = 1 << 0,
114};
115
Simon Glassaa4bce92022-03-04 08:43:00 -0700116/** Function type for event handlers */
117typedef int (*event_handler_t)(void *ctx, struct event *event);
118
Simon Glass3bfbedd2023-08-21 21:16:48 -0600119/** Function type for simple event handlers */
120typedef int (*event_handler_simple_t)(void);
121
Simon Glassaa4bce92022-03-04 08:43:00 -0700122/**
123 * struct evspy_info - information about an event spy
124 *
125 * @func: Function to call when the event is activated (must be first)
126 * @type: Event type
Simon Glass3bfbedd2023-08-21 21:16:48 -0600127 * @flag: Flags for this spy
Simon Glassaa4bce92022-03-04 08:43:00 -0700128 * @id: Event id string
129 */
130struct evspy_info {
131 event_handler_t func;
Simon Glass3bfbedd2023-08-21 21:16:48 -0600132 u8 type;
133 u8 flags;
134#if CONFIG_IS_ENABLED(EVENT_DEBUG)
135 const char *id;
136#endif
137};
138
139/**
140 * struct evspy_info_simple - information about an event spy
141 *
142 * THis is the 'simple' record, the only difference being the handler function
143 *
144 * @func: Function to call when the event is activated (must be first)
145 * @type: Event type
146 * @flag: Flags for this spy
147 * @id: Event id string
148 */
149struct evspy_info_simple {
150 event_handler_simple_t func;
151 u8 type;
152 u8 flags;
Simon Glassaa4bce92022-03-04 08:43:00 -0700153#if CONFIG_IS_ENABLED(EVENT_DEBUG)
154 const char *id;
155#endif
156};
157
158/* Declare a new event spy */
159#if CONFIG_IS_ENABLED(EVENT_DEBUG)
Simon Glass3bfbedd2023-08-21 21:16:48 -0600160#define _ESPY_REC(_type, _func) { _func, _type, 0, #_func, }
161#define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE, #_func, }
Simon Glassaa4bce92022-03-04 08:43:00 -0700162#else
163#define _ESPY_REC(_type, _func) { _func, _type, }
Simon Glass3bfbedd2023-08-21 21:16:48 -0600164#define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE }
Simon Glassaa4bce92022-03-04 08:43:00 -0700165#endif
166
167static inline const char *event_spy_id(struct evspy_info *spy)
168{
169#if CONFIG_IS_ENABLED(EVENT_DEBUG)
170 return spy->id;
171#else
172 return "?";
173#endif
174}
175
176/*
177 * It seems that LTO will drop list entries if it decides they are not used,
178 * although the conditions that cause this are unclear.
179 *
180 * The example found is the following:
181 *
182 * static int sandbox_misc_init_f(void *ctx, struct event *event)
183 * {
184 * return sandbox_early_getopt_check();
185 * }
186 * EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f);
187 *
188 * where EVENT_SPY uses ll_entry_declare()
189 *
190 * In this case, LTO decides to drop the sandbox_misc_init_f() function
191 * (which is fine) but then drops the linker-list entry too. This means
192 * that the code no longer works, in this case sandbox no-longer checks its
193 * command-line arguments properly.
194 *
195 * Without LTO, the KEEP() command in the .lds file is enough to keep the
196 * entry around. But with LTO it seems that the entry has already been
197 * dropped before the link script is considered.
198 *
199 * The only solution I can think of is to mark linker-list entries as 'used'
200 * using an attribute. This should be safe, since we don't actually want to drop
201 * any of these. However this does slightly limit LTO's optimisation choices.
Simon Glass1fd91682022-07-30 15:52:30 -0600202 *
203 * Another issue has come up, only with clang: using 'static' makes it throw
204 * away the linker-list entry sometimes, e.g. with the EVT_FT_FIXUP entry in
205 * vbe_simple.c - so for now, make it global.
Simon Glassaa4bce92022-03-04 08:43:00 -0700206 */
Simon Glassa41134f2023-08-21 21:16:57 -0600207#define EVENT_SPY_FULL(_type, _func) \
Simon Glassb169e212022-09-06 20:26:56 -0600208 __used ll_entry_declare(struct evspy_info, _type ## _3_ ## _func, \
209 evspy_info) = _ESPY_REC(_type, _func)
Simon Glassaa4bce92022-03-04 08:43:00 -0700210
Simon Glass3bfbedd2023-08-21 21:16:48 -0600211/* Simple spy with no function arguemnts */
212#define EVENT_SPY_SIMPLE(_type, _func) \
213 __used ll_entry_declare(struct evspy_info_simple, \
214 _type ## _3_ ## _func, \
215 evspy_info) = _ESPY_REC_SIMPLE(_type, _func)
216
Simon Glassaa4bce92022-03-04 08:43:00 -0700217/**
218 * event_register - register a new spy
219 *
220 * @id: Spy ID
221 * @type: Event type to subscribe to
222 * @func: Function to call when the event is sent
223 * @ctx: Context to pass to the function
224 * @return 0 if OK, -ve on error
225 */
226int event_register(const char *id, enum event_t type, event_handler_t func,
227 void *ctx);
228
Simon Glass3304fa82022-03-04 08:43:06 -0700229/** event_show_spy_list( - Show a list of event spies */
230void event_show_spy_list(void);
231
Simon Glassaa4bce92022-03-04 08:43:00 -0700232/**
Ovidiu Panait145f2002022-05-15 21:40:29 +0300233 * event_manual_reloc() - Relocate event handler pointers
234 *
235 * Relocate event handler pointers for all static event spies. It is called
236 * during the generic board init sequence, after relocation.
237 *
238 * Return: 0 if OK
239 */
240int event_manual_reloc(void);
241
242/**
Simon Glassd6db8942023-08-21 21:16:53 -0600243 * event_type_name() - Get the name of an event type
244 *
245 * @type: Type to check
246 * Return: Name of event, or "(unknown)" if not known
247 */
248const char *event_type_name(enum event_t type);
249
250/**
Simon Glassaa4bce92022-03-04 08:43:00 -0700251 * event_notify() - notify spies about an event
252 *
253 * It is possible to pass in union event_data here but that may not be
254 * convenient if the data is elsewhere, or is one of the members of the union.
255 * So this uses a void * for @data, with a separate @size.
256 *
257 * @type: Event type
258 * @data: Event data to be sent (e.g. union_event_data)
259 * @size: Size of data in bytes
260 * @return 0 if OK, -ve on error
261 */
262int event_notify(enum event_t type, void *data, int size);
263
Heinrich Schuchardtc95a50e2022-05-07 22:39:01 +0200264#if CONFIG_IS_ENABLED(EVENT)
Simon Glassaa4bce92022-03-04 08:43:00 -0700265/**
266 * event_notify_null() - notify spies about an event
267 *
268 * Data is NULL and the size is 0
269 *
270 * @type: Event type
271 * @return 0 if OK, -ve on error
272 */
273int event_notify_null(enum event_t type);
274#else
Simon Glassaa4bce92022-03-04 08:43:00 -0700275static inline int event_notify_null(enum event_t type)
276{
277 return 0;
278}
279#endif
280
281#if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
282/**
283 * event_uninit() - Clean up dynamic events
284 *
285 * This removes all dynamic event handlers
286 */
287int event_uninit(void);
288
289/**
290 * event_uninit() - Set up dynamic events
291 *
292 * Init a list of dynamic event handlers, so that these can be added as
293 * needed
294 */
295int event_init(void);
296#else
297static inline int event_uninit(void)
298{
299 return 0;
300}
301
302static inline int event_init(void)
303{
304 return 0;
305}
306#endif
307
308#endif