Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 1 | /* 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 Glass | 98887ab | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 13 | #include <dm/ofnode_decl.h> |
Christian Taedcke | a1190b4 | 2023-07-20 09:27:24 +0200 | [diff] [blame] | 14 | #include <linux/types.h> |
Simon Glass | 98887ab | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 15 | |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 16 | /** |
| 17 | * enum event_t - Types of events supported by U-Boot |
| 18 | * |
| 19 | * @EVT_DM_PRE_PROBE: Device is about to be probed |
| 20 | */ |
| 21 | enum event_t { |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 22 | /** |
| 23 | * @EVT_NONE: This zero value is not used for events. |
| 24 | */ |
| 25 | EVT_NONE = 0, |
| 26 | |
| 27 | /** |
| 28 | * @EVT_TEST: This event is used in unit tests. |
| 29 | */ |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 30 | EVT_TEST, |
| 31 | |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 32 | /** |
| 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 |
| 37 | * fail. |
| 38 | */ |
Simon Glass | 55171ae | 2023-05-04 16:50:45 -0600 | [diff] [blame] | 39 | EVT_DM_POST_INIT_F, |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 40 | |
| 41 | /** |
| 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 |
| 46 | * fail. |
| 47 | */ |
Chanho Park | 27c7a62 | 2023-08-18 14:11:02 +0900 | [diff] [blame] | 48 | EVT_DM_POST_INIT_R, |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * @EVT_DM_PRE_PROBE: |
| 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 |
| 55 | * being probed. |
| 56 | */ |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 57 | EVT_DM_PRE_PROBE, |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * @EVT_DM_POST_PROBE: |
| 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. |
| 65 | */ |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 66 | EVT_DM_POST_PROBE, |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * @EVT_DM_PRE_REMOVE: |
| 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. |
| 74 | */ |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 75 | EVT_DM_PRE_REMOVE, |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 76 | |
| 77 | /** |
| 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 |
| 84 | * functional. |
| 85 | */ |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 86 | EVT_DM_POST_REMOVE, |
| 87 | |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 88 | /** |
| 89 | * @EVT_MISC_INIT_F: |
| 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 |
| 93 | * fail. |
| 94 | */ |
Simon Glass | 42fdceb | 2022-03-04 08:43:04 -0700 | [diff] [blame] | 95 | EVT_MISC_INIT_F, |
| 96 | |
Tom Rini | b53ab97 | 2023-09-04 11:19:50 -0400 | [diff] [blame] | 97 | /** |
| 98 | * @EVT_FSP_INIT_F: |
| 99 | * This event is triggered before relocation to set up Firmware Support |
| 100 | * Package. |
Simon Glass | 13a7db9 | 2023-08-21 21:16:59 -0600 | [diff] [blame] | 101 | * Where U-Boot relies on binary blobs to handle part of the system |
| 102 | * init, this event can be used to set up the blobs. This is used on |
| 103 | * some Intel platforms |
| 104 | */ |
| 105 | EVT_FSP_INIT_F, |
| 106 | |
Tom Rini | b53ab97 | 2023-09-04 11:19:50 -0400 | [diff] [blame] | 107 | /** |
| 108 | * @EVT_LAST_STAGE_INIT: |
| 109 | * This event is triggered just before jumping to the main loop. |
Simon Glass | 91caa3b | 2023-08-21 21:17:01 -0600 | [diff] [blame] | 110 | * Some boards need to perform initialisation immediately before control |
| 111 | * is passed to the command-line interpreter (e.g. for init that depend |
| 112 | * on later phases in the init sequence). |
| 113 | * |
| 114 | * Some parts can be only initialized if all others (like Interrupts) |
| 115 | * are up and running (e.g. the PC-style ISA keyboard). |
| 116 | */ |
| 117 | EVT_LAST_STAGE_INIT, |
| 118 | |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 119 | /** |
| 120 | * @EVT_FPGA_LOAD: |
| 121 | * The FPGA load hook is called after loading an FPGA with a new binary. |
| 122 | * Its parameter is of type struct event_fpga_load and contains |
| 123 | * information about the loaded image. |
| 124 | */ |
Christian Taedcke | a1190b4 | 2023-07-20 09:27:24 +0200 | [diff] [blame] | 125 | EVT_FPGA_LOAD, |
| 126 | |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 127 | /** |
| 128 | * @EVT_FT_FIXUP: |
| 129 | * This event is triggered during device-tree fix up after all |
| 130 | * other device-tree fixups have been executed. |
| 131 | * Its parameter is of type struct event_ft_fixup which contains |
| 132 | * the address of the device-tree to fix up and the list of images to be |
| 133 | * booted. |
| 134 | * A non-zero return code from the event handler let's booting the |
| 135 | * images fail. |
| 136 | */ |
Simon Glass | 98887ab | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 137 | EVT_FT_FIXUP, |
| 138 | |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 139 | /** |
| 140 | * @EVT_MAIN_LOOP: |
| 141 | * This event is triggered immediately before calling main_loop() which |
| 142 | * is the entry point of the command line. Its parameter is NULL. |
| 143 | * A non-zero return value causes the boot to fail. |
| 144 | */ |
Sughosh Ganu | 467bad5 | 2022-10-21 18:16:01 +0530 | [diff] [blame] | 145 | EVT_MAIN_LOOP, |
| 146 | |
Heinrich Schuchardt | 6a40707 | 2023-08-28 21:13:32 +0200 | [diff] [blame] | 147 | /** |
| 148 | * @EVT_COUNT: |
| 149 | * This constants holds the maximum event number + 1 and is used when |
| 150 | * looping over all event classes. |
| 151 | */ |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 152 | EVT_COUNT |
| 153 | }; |
| 154 | |
| 155 | union event_data { |
| 156 | /** |
| 157 | * struct event_data_test - test data |
| 158 | * |
| 159 | * @signal: A value to update the state with |
| 160 | */ |
| 161 | struct event_data_test { |
| 162 | int signal; |
| 163 | } test; |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * struct event_dm - driver model event |
| 167 | * |
| 168 | * @dev: Device this event relates to |
| 169 | */ |
| 170 | struct event_dm { |
| 171 | struct udevice *dev; |
| 172 | } dm; |
Simon Glass | 98887ab | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 173 | |
| 174 | /** |
Christian Taedcke | a1190b4 | 2023-07-20 09:27:24 +0200 | [diff] [blame] | 175 | * struct event_fpga_load - fpga load event |
| 176 | * |
| 177 | * @buf: The buffer that was loaded into the fpga |
| 178 | * @bsize: The size of the buffer that was loaded into the fpga |
| 179 | * @result: Result of the load operation |
| 180 | */ |
| 181 | struct event_fpga_load { |
| 182 | const void *buf; |
| 183 | size_t bsize; |
| 184 | int result; |
| 185 | } fpga_load; |
| 186 | |
| 187 | /** |
Simon Glass | 98887ab | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 188 | * struct event_ft_fixup - FDT fixup before booting |
| 189 | * |
| 190 | * @tree: tree to update |
Simon Glass | b215b60 | 2022-09-06 20:26:58 -0600 | [diff] [blame] | 191 | * @images: images which are being booted |
Simon Glass | 98887ab | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 192 | */ |
| 193 | struct event_ft_fixup { |
| 194 | oftree tree; |
Simon Glass | b215b60 | 2022-09-06 20:26:58 -0600 | [diff] [blame] | 195 | struct bootm_headers *images; |
Simon Glass | 98887ab | 2022-07-30 15:52:31 -0600 | [diff] [blame] | 196 | } ft_fixup; |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | /** |
| 200 | * struct event - an event that can be sent and received |
| 201 | * |
| 202 | * @type: Event type |
| 203 | * @data: Data for this particular event |
| 204 | */ |
| 205 | struct event { |
| 206 | enum event_t type; |
| 207 | union event_data data; |
| 208 | }; |
| 209 | |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 210 | /* Flags for event spy */ |
| 211 | enum evspy_flags { |
| 212 | EVSPYF_SIMPLE = 1 << 0, |
| 213 | }; |
| 214 | |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 215 | /** Function type for event handlers */ |
| 216 | typedef int (*event_handler_t)(void *ctx, struct event *event); |
| 217 | |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 218 | /** Function type for simple event handlers */ |
| 219 | typedef int (*event_handler_simple_t)(void); |
| 220 | |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 221 | /** |
| 222 | * struct evspy_info - information about an event spy |
| 223 | * |
| 224 | * @func: Function to call when the event is activated (must be first) |
| 225 | * @type: Event type |
Tom Rini | b53ab97 | 2023-09-04 11:19:50 -0400 | [diff] [blame] | 226 | * @flags: Flags for this spy |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 227 | * @id: Event id string |
| 228 | */ |
| 229 | struct evspy_info { |
| 230 | event_handler_t func; |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 231 | u8 type; |
| 232 | u8 flags; |
| 233 | #if CONFIG_IS_ENABLED(EVENT_DEBUG) |
| 234 | const char *id; |
| 235 | #endif |
| 236 | }; |
| 237 | |
| 238 | /** |
| 239 | * struct evspy_info_simple - information about an event spy |
| 240 | * |
| 241 | * THis is the 'simple' record, the only difference being the handler function |
| 242 | * |
| 243 | * @func: Function to call when the event is activated (must be first) |
| 244 | * @type: Event type |
Tom Rini | b53ab97 | 2023-09-04 11:19:50 -0400 | [diff] [blame] | 245 | * @flags: Flags for this spy |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 246 | * @id: Event id string |
| 247 | */ |
| 248 | struct evspy_info_simple { |
| 249 | event_handler_simple_t func; |
| 250 | u8 type; |
| 251 | u8 flags; |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 252 | #if CONFIG_IS_ENABLED(EVENT_DEBUG) |
| 253 | const char *id; |
| 254 | #endif |
| 255 | }; |
| 256 | |
| 257 | /* Declare a new event spy */ |
| 258 | #if CONFIG_IS_ENABLED(EVENT_DEBUG) |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 259 | #define _ESPY_REC(_type, _func) { _func, _type, 0, #_func, } |
| 260 | #define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE, #_func, } |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 261 | #else |
| 262 | #define _ESPY_REC(_type, _func) { _func, _type, } |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 263 | #define _ESPY_REC_SIMPLE(_type, _func) { _func, _type, EVSPYF_SIMPLE } |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 264 | #endif |
| 265 | |
| 266 | static inline const char *event_spy_id(struct evspy_info *spy) |
| 267 | { |
| 268 | #if CONFIG_IS_ENABLED(EVENT_DEBUG) |
| 269 | return spy->id; |
| 270 | #else |
| 271 | return "?"; |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * It seems that LTO will drop list entries if it decides they are not used, |
| 277 | * although the conditions that cause this are unclear. |
| 278 | * |
| 279 | * The example found is the following: |
| 280 | * |
| 281 | * static int sandbox_misc_init_f(void *ctx, struct event *event) |
| 282 | * { |
| 283 | * return sandbox_early_getopt_check(); |
| 284 | * } |
| 285 | * EVENT_SPY(EVT_MISC_INIT_F, sandbox_misc_init_f); |
| 286 | * |
| 287 | * where EVENT_SPY uses ll_entry_declare() |
| 288 | * |
| 289 | * In this case, LTO decides to drop the sandbox_misc_init_f() function |
| 290 | * (which is fine) but then drops the linker-list entry too. This means |
| 291 | * that the code no longer works, in this case sandbox no-longer checks its |
| 292 | * command-line arguments properly. |
| 293 | * |
| 294 | * Without LTO, the KEEP() command in the .lds file is enough to keep the |
| 295 | * entry around. But with LTO it seems that the entry has already been |
| 296 | * dropped before the link script is considered. |
| 297 | * |
| 298 | * The only solution I can think of is to mark linker-list entries as 'used' |
| 299 | * using an attribute. This should be safe, since we don't actually want to drop |
| 300 | * any of these. However this does slightly limit LTO's optimisation choices. |
Simon Glass | 5695247 | 2022-07-30 15:52:30 -0600 | [diff] [blame] | 301 | * |
| 302 | * Another issue has come up, only with clang: using 'static' makes it throw |
| 303 | * away the linker-list entry sometimes, e.g. with the EVT_FT_FIXUP entry in |
| 304 | * vbe_simple.c - so for now, make it global. |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 305 | */ |
Simon Glass | 6c4cad7 | 2023-08-21 21:16:57 -0600 | [diff] [blame] | 306 | #define EVENT_SPY_FULL(_type, _func) \ |
Simon Glass | b5001cb | 2022-09-06 20:26:56 -0600 | [diff] [blame] | 307 | __used ll_entry_declare(struct evspy_info, _type ## _3_ ## _func, \ |
| 308 | evspy_info) = _ESPY_REC(_type, _func) |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 309 | |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 310 | /* Simple spy with no function arguemnts */ |
| 311 | #define EVENT_SPY_SIMPLE(_type, _func) \ |
| 312 | __used ll_entry_declare(struct evspy_info_simple, \ |
| 313 | _type ## _3_ ## _func, \ |
| 314 | evspy_info) = _ESPY_REC_SIMPLE(_type, _func) |
| 315 | |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 316 | /** |
| 317 | * event_register - register a new spy |
| 318 | * |
| 319 | * @id: Spy ID |
| 320 | * @type: Event type to subscribe to |
| 321 | * @func: Function to call when the event is sent |
| 322 | * @ctx: Context to pass to the function |
| 323 | * @return 0 if OK, -ve on error |
| 324 | */ |
| 325 | int event_register(const char *id, enum event_t type, event_handler_t func, |
| 326 | void *ctx); |
| 327 | |
Simon Glass | c81b460 | 2022-03-04 08:43:06 -0700 | [diff] [blame] | 328 | /** event_show_spy_list( - Show a list of event spies */ |
| 329 | void event_show_spy_list(void); |
| 330 | |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 331 | /** |
Simon Glass | fb7dfca | 2023-08-21 21:16:53 -0600 | [diff] [blame] | 332 | * event_type_name() - Get the name of an event type |
| 333 | * |
| 334 | * @type: Type to check |
| 335 | * Return: Name of event, or "(unknown)" if not known |
| 336 | */ |
| 337 | const char *event_type_name(enum event_t type); |
| 338 | |
| 339 | /** |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 340 | * event_notify() - notify spies about an event |
| 341 | * |
| 342 | * It is possible to pass in union event_data here but that may not be |
| 343 | * convenient if the data is elsewhere, or is one of the members of the union. |
| 344 | * So this uses a void * for @data, with a separate @size. |
| 345 | * |
| 346 | * @type: Event type |
| 347 | * @data: Event data to be sent (e.g. union_event_data) |
| 348 | * @size: Size of data in bytes |
| 349 | * @return 0 if OK, -ve on error |
| 350 | */ |
| 351 | int event_notify(enum event_t type, void *data, int size); |
| 352 | |
Heinrich Schuchardt | c5ef202 | 2022-05-07 22:39:01 +0200 | [diff] [blame] | 353 | #if CONFIG_IS_ENABLED(EVENT) |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 354 | /** |
| 355 | * event_notify_null() - notify spies about an event |
| 356 | * |
| 357 | * Data is NULL and the size is 0 |
| 358 | * |
| 359 | * @type: Event type |
| 360 | * @return 0 if OK, -ve on error |
| 361 | */ |
| 362 | int event_notify_null(enum event_t type); |
| 363 | #else |
Simon Glass | 87a5d1b | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 364 | static inline int event_notify_null(enum event_t type) |
| 365 | { |
| 366 | return 0; |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | #if CONFIG_IS_ENABLED(EVENT_DYNAMIC) |
| 371 | /** |
| 372 | * event_uninit() - Clean up dynamic events |
| 373 | * |
| 374 | * This removes all dynamic event handlers |
| 375 | */ |
| 376 | int event_uninit(void); |
| 377 | |
| 378 | /** |
| 379 | * event_uninit() - Set up dynamic events |
| 380 | * |
| 381 | * Init a list of dynamic event handlers, so that these can be added as |
| 382 | * needed |
| 383 | */ |
| 384 | int event_init(void); |
| 385 | #else |
| 386 | static inline int event_uninit(void) |
| 387 | { |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static inline int event_init(void) |
| 392 | { |
| 393 | return 0; |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | #endif |