Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 2 | #ifndef BOOTSTATE_H |
| 3 | #define BOOTSTATE_H |
| 4 | |
| 5 | #include <string.h> |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 6 | #include <stddef.h> |
| 7 | #include <stdint.h> |
Aaron Durbin | 622eea7 | 2016-02-10 23:15:07 -0600 | [diff] [blame] | 8 | /* Only declare main() when in ramstage. */ |
| 9 | #if ENV_RAMSTAGE |
| 10 | #include <main_decl.h> |
| 11 | #endif |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 12 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 13 | /* |
| 14 | * The boot state machine provides a mechanism for calls to be made through- |
| 15 | * out the main boot process. The boot process is separated into discrete |
| 16 | * states. Upon a state's entry and exit and callbacks can be made. For |
| 17 | * example: |
| 18 | * |
| 19 | * Enter State |
| 20 | * + |
| 21 | * | |
| 22 | * V |
| 23 | * +-----------------+ |
| 24 | * | Entry callbacks | |
| 25 | * +-----------------+ |
| 26 | * | State Actions | |
| 27 | * +-----------------+ |
| 28 | * | Exit callbacks | |
| 29 | * +-------+---------+ |
| 30 | * | |
| 31 | * V |
| 32 | * Next State |
| 33 | * |
| 34 | * Below is the current flow from top to bottom: |
| 35 | * |
| 36 | * start |
| 37 | * | |
| 38 | * BS_PRE_DEVICE |
| 39 | * | |
| 40 | * BS_DEV_INIT_CHIPS |
| 41 | * | |
| 42 | * BS_DEV_ENUMERATE |
| 43 | * | |
| 44 | * BS_DEV_RESOURCES |
| 45 | * | |
| 46 | * BS_DEV_ENABLE |
| 47 | * | |
| 48 | * BS_DEV_INIT |
| 49 | * | |
| 50 | * BS_POST_DEVICE |
| 51 | * | |
| 52 | * BS_OS_RESUME_CHECK -------- BS_OS_RESUME |
| 53 | * | | |
| 54 | * BS_WRITE_TABLES os handoff |
| 55 | * | |
| 56 | * BS_PAYLOAD_LOAD |
| 57 | * | |
| 58 | * BS_PAYLOAD_BOOT |
| 59 | * | |
| 60 | * payload run |
| 61 | * |
| 62 | * Brief description of states: |
| 63 | * BS_PRE_DEVICE - before any device tree actions take place |
| 64 | * BS_DEV_INIT_CHIPS - init all chips in device tree |
| 65 | * BS_DEV_ENUMERATE - device tree probing |
| 66 | * BS_DEV_RESOURCES - device tree resource allocation and assignment |
| 67 | * BS_DEV_ENABLE - device tree enabling/disabling of devices |
| 68 | * BS_DEV_INIT - device tree device initialization |
| 69 | * BS_POST_DEVICE - all device tree actions performed |
| 70 | * BS_OS_RESUME_CHECK - check for OS resume |
| 71 | * BS_OS_RESUME - resume to OS |
| 72 | * BS_WRITE_TABLES - write coreboot tables |
| 73 | * BS_PAYLOAD_LOAD - Load payload into memory |
| 74 | * BS_PAYLOAD_BOOT - Boot to payload |
| 75 | */ |
| 76 | |
| 77 | typedef enum { |
| 78 | BS_PRE_DEVICE, |
| 79 | BS_DEV_INIT_CHIPS, |
| 80 | BS_DEV_ENUMERATE, |
| 81 | BS_DEV_RESOURCES, |
| 82 | BS_DEV_ENABLE, |
| 83 | BS_DEV_INIT, |
| 84 | BS_POST_DEVICE, |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 85 | BS_OS_RESUME_CHECK, |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 86 | BS_OS_RESUME, |
| 87 | BS_WRITE_TABLES, |
| 88 | BS_PAYLOAD_LOAD, |
| 89 | BS_PAYLOAD_BOOT, |
| 90 | } boot_state_t; |
| 91 | |
| 92 | /* The boot_state_sequence_t describes when a callback is to be made. It is |
| 93 | * called either before a state is entered or when a state is exited. */ |
| 94 | typedef enum { |
| 95 | BS_ON_ENTRY, |
| 96 | BS_ON_EXIT |
| 97 | } boot_state_sequence_t; |
| 98 | |
| 99 | struct boot_state_callback { |
| 100 | void *arg; |
| 101 | void (*callback)(void *arg); |
| 102 | /* For use internal to the boot state machine. */ |
| 103 | struct boot_state_callback *next; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 104 | #if CONFIG(DEBUG_BOOT_STATE) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 105 | const char *location; |
| 106 | #endif |
| 107 | }; |
| 108 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 109 | #if CONFIG(DEBUG_BOOT_STATE) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 110 | #define BOOT_STATE_CALLBACK_LOC __FILE__ ":" STRINGIFY(__LINE__) |
| 111 | #define BOOT_STATE_CALLBACK_INIT_DEBUG .location = BOOT_STATE_CALLBACK_LOC, |
Elyes HAOUAS | b0f1988 | 2018-06-09 11:59:00 +0200 | [diff] [blame] | 112 | #define INIT_BOOT_STATE_CALLBACK_DEBUG(bscb_) \ |
Lee Leahy | bb4ae07 | 2017-03-07 12:01:02 -0800 | [diff] [blame] | 113 | do { \ |
| 114 | bscb_->location = BOOT_STATE_CALLBACK_LOC; \ |
| 115 | } while (0) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 116 | #else |
| 117 | #define BOOT_STATE_CALLBACK_INIT_DEBUG |
| 118 | #define INIT_BOOT_STATE_CALLBACK_DEBUG(bscb_) |
| 119 | #endif |
| 120 | |
| 121 | #define BOOT_STATE_CALLBACK_INIT(func_, arg_) \ |
| 122 | { \ |
| 123 | .arg = arg_, \ |
| 124 | .callback = func_, \ |
| 125 | .next = NULL, \ |
| 126 | BOOT_STATE_CALLBACK_INIT_DEBUG \ |
| 127 | } |
| 128 | |
| 129 | #define BOOT_STATE_CALLBACK(name_, func_, arg_) \ |
| 130 | struct boot_state_callback name_ = BOOT_STATE_CALLBACK_INIT(func_, arg_) |
| 131 | |
| 132 | /* Initialize an allocated boot_state_callback. */ |
| 133 | #define INIT_BOOT_STATE_CALLBACK(bscb_, func_, arg_) \ |
Lee Leahy | bb4ae07 | 2017-03-07 12:01:02 -0800 | [diff] [blame] | 134 | do { \ |
| 135 | INIT_BOOT_STATE_CALLBACK_DEBUG(bscb_) \ |
| 136 | bscb_->callback = func_; \ |
| 137 | bscb_->arg = arg_ \ |
Lee Leahy | 86f60a9 | 2017-03-07 12:06:44 -0800 | [diff] [blame] | 138 | } while (0) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 139 | |
| 140 | /* The following 2 functions schedule a callback to be called on entry/exit |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 141 | * to a given state. Note that there are no ordering guarantees between the |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 142 | * individual callbacks on a given state. 0 is returned on success < 0 on |
| 143 | * error. */ |
| 144 | int boot_state_sched_on_entry(struct boot_state_callback *bscb, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 145 | boot_state_t state); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 146 | int boot_state_sched_on_exit(struct boot_state_callback *bscb, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 147 | boot_state_t state); |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 148 | /* Schedule an array of entries of size num. */ |
| 149 | struct boot_state_init_entry; |
| 150 | void boot_state_sched_entries(struct boot_state_init_entry *entries, |
| 151 | size_t num); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 152 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 153 | /* Block/Unblock the (state, seq) pair from transitioning. Returns 0 on |
| 154 | * success < 0 when the phase of the (state,seq) has already ran. */ |
| 155 | int boot_state_block(boot_state_t state, boot_state_sequence_t seq); |
| 156 | int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq); |
| 157 | /* Block/Unblock current state phase from transitioning. */ |
| 158 | void boot_state_current_block(void); |
| 159 | void boot_state_current_unblock(void); |
| 160 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 161 | /* In order to schedule boot state callbacks at compile-time specify the |
| 162 | * entries in an array using the BOOT_STATE_INIT_ENTRIES and |
| 163 | * BOOT_STATE_INIT_ENTRY macros below. */ |
| 164 | struct boot_state_init_entry { |
| 165 | boot_state_t state; |
| 166 | boot_state_sequence_t when; |
| 167 | struct boot_state_callback bscb; |
| 168 | }; |
| 169 | |
Aaron Durbin | 4d3de7e | 2015-09-02 17:34:04 -0500 | [diff] [blame] | 170 | #if ENV_RAMSTAGE |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 171 | #define BOOT_STATE_INIT_ATTR __attribute__((used, section(".bs_init"))) |
Aaron Durbin | 4d3de7e | 2015-09-02 17:34:04 -0500 | [diff] [blame] | 172 | #else |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 173 | #define BOOT_STATE_INIT_ATTR __attribute__((unused)) |
Aaron Durbin | 4d3de7e | 2015-09-02 17:34:04 -0500 | [diff] [blame] | 174 | #endif |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 175 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 176 | #define BOOT_STATE_INIT_ENTRY(state_, when_, func_, arg_) \ |
| 177 | static struct boot_state_init_entry func_ ##_## state_ ##_## when_ = \ |
| 178 | { \ |
| 179 | .state = state_, \ |
| 180 | .when = when_, \ |
| 181 | .bscb = BOOT_STATE_CALLBACK_INIT(func_, arg_), \ |
| 182 | }; \ |
| 183 | static struct boot_state_init_entry * \ |
| 184 | bsie_ ## func_ ##_## state_ ##_## when_ BOOT_STATE_INIT_ATTR = \ |
Lee Leahy | 18cb7e6 | 2017-03-07 13:00:08 -0800 | [diff] [blame] | 185 | &func_ ##_## state_ ##_## when_; |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 186 | |
Aaron Durbin | 16bd267 | 2016-12-07 11:58:20 -0600 | [diff] [blame] | 187 | /* Hook per arch when coreboot is exiting to payload or ACPI OS resume. It's |
| 188 | * the very last thing done before the transition. */ |
| 189 | void arch_bootstate_coreboot_exit(void); |
| 190 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 191 | #endif /* BOOTSTATE_H */ |