Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | /* |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 5 | * C Bootstrap code for the coreboot |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Kyösti Mälkki | 61ab3fe | 2020-06-17 14:17:41 +0300 | [diff] [blame] | 8 | #include <acpi/acpi.h> |
Kyösti Mälkki | e0183d6 | 2020-06-17 13:45:16 +0300 | [diff] [blame] | 9 | #include <acpi/acpi_gnvs.h> |
Ricardo Quesada | 470ca571 | 2021-07-16 16:39:28 -0700 | [diff] [blame] | 10 | #include <adainit.h> |
Julius Werner | 85620db | 2013-11-13 18:22:15 -0800 | [diff] [blame] | 11 | #include <arch/exception.h> |
Ricardo Quesada | 470ca571 | 2021-07-16 16:39:28 -0700 | [diff] [blame] | 12 | #include <boot/tables.h> |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 13 | #include <bootstate.h> |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 14 | #include <cbmem.h> |
Ricardo Quesada | 470ca571 | 2021-07-16 16:39:28 -0700 | [diff] [blame] | 15 | #include <commonlib/console/post_codes.h> |
| 16 | #include <commonlib/helpers.h> |
| 17 | #include <console/console.h> |
| 18 | #include <delay.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 19 | #include <device/device.h> |
| 20 | #include <device/pci.h> |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 21 | #include <program_loading.h> |
Ricardo Quesada | 470ca571 | 2021-07-16 16:39:28 -0700 | [diff] [blame] | 22 | #include <thread.h> |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 23 | #include <timer.h> |
Stefan Reinauer | bf729ba | 2011-11-04 12:31:58 -0700 | [diff] [blame] | 24 | #include <timestamp.h> |
Felix Held | f107767 | 2021-10-01 19:53:39 +0200 | [diff] [blame] | 25 | #include <types.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 26 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 27 | static boot_state_t bs_pre_device(void *arg); |
| 28 | static boot_state_t bs_dev_init_chips(void *arg); |
| 29 | static boot_state_t bs_dev_enumerate(void *arg); |
| 30 | static boot_state_t bs_dev_resources(void *arg); |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 31 | static boot_state_t bs_dev_enable(void *arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 32 | static boot_state_t bs_dev_init(void *arg); |
| 33 | static boot_state_t bs_post_device(void *arg); |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 34 | static boot_state_t bs_os_resume_check(void *arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 35 | static boot_state_t bs_os_resume(void *arg); |
| 36 | static boot_state_t bs_write_tables(void *arg); |
| 37 | static boot_state_t bs_payload_load(void *arg); |
| 38 | static boot_state_t bs_payload_boot(void *arg); |
| 39 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 40 | /* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be |
| 41 | * blocked from transitioning to the next (state,seq) pair. When the blockers |
| 42 | * field is 0 a transition may occur. */ |
| 43 | struct boot_phase { |
| 44 | struct boot_state_callback *callbacks; |
| 45 | int blockers; |
| 46 | }; |
| 47 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 48 | struct boot_state { |
| 49 | const char *name; |
| 50 | boot_state_t id; |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 51 | u8 post_code; |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 52 | struct boot_phase phases[2]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 53 | boot_state_t (*run_state)(void *arg); |
| 54 | void *arg; |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 55 | int num_samples; |
Felix Held | 56da0b7 | 2021-10-01 20:03:02 +0200 | [diff] [blame] | 56 | bool complete; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 59 | #define BS_INIT(state_, run_func_) \ |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 60 | { \ |
| 61 | .name = #state_, \ |
| 62 | .id = state_, \ |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 63 | .post_code = POSTCODE_ ## state_, \ |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 64 | .phases = { { NULL, 0 }, { NULL, 0 } }, \ |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 65 | .run_state = run_func_, \ |
| 66 | .arg = NULL, \ |
Felix Held | 56da0b7 | 2021-10-01 20:03:02 +0200 | [diff] [blame] | 67 | .complete = false, \ |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 68 | } |
| 69 | #define BS_INIT_ENTRY(state_, run_func_) \ |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 70 | [state_] = BS_INIT(state_, run_func_) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 71 | |
| 72 | static struct boot_state boot_states[] = { |
| 73 | BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device), |
| 74 | BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips), |
| 75 | BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate), |
| 76 | BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources), |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 77 | BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable), |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 78 | BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init), |
| 79 | BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device), |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 80 | BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check), |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 81 | BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume), |
| 82 | BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables), |
| 83 | BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load), |
| 84 | BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot), |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 87 | void __weak arch_bootstate_coreboot_exit(void) { } |
Aaron Durbin | 16bd267 | 2016-12-07 11:58:20 -0600 | [diff] [blame] | 88 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 89 | static boot_state_t bs_pre_device(void *arg) |
| 90 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 91 | return BS_DEV_INIT_CHIPS; |
| 92 | } |
| 93 | |
| 94 | static boot_state_t bs_dev_init_chips(void *arg) |
| 95 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 96 | timestamp_add_now(TS_DEVICE_ENUMERATE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 97 | |
| 98 | /* Initialize chips early, they might disable unused devices. */ |
| 99 | dev_initialize_chips(); |
| 100 | |
| 101 | return BS_DEV_ENUMERATE; |
| 102 | } |
| 103 | |
| 104 | static boot_state_t bs_dev_enumerate(void *arg) |
| 105 | { |
| 106 | /* Find the devices we don't have hard coded knowledge about. */ |
| 107 | dev_enumerate(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 108 | |
| 109 | return BS_DEV_RESOURCES; |
| 110 | } |
| 111 | |
| 112 | static boot_state_t bs_dev_resources(void *arg) |
| 113 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 114 | timestamp_add_now(TS_DEVICE_CONFIGURE); |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 115 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 116 | /* Now compute and assign the bus resources. */ |
| 117 | dev_configure(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 118 | |
| 119 | return BS_DEV_ENABLE; |
| 120 | } |
| 121 | |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 122 | static boot_state_t bs_dev_enable(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 123 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 124 | timestamp_add_now(TS_DEVICE_ENABLE); |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 125 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 126 | /* Now actually enable devices on the bus */ |
| 127 | dev_enable(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 128 | |
| 129 | return BS_DEV_INIT; |
| 130 | } |
| 131 | |
| 132 | static boot_state_t bs_dev_init(void *arg) |
| 133 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 134 | timestamp_add_now(TS_DEVICE_INITIALIZE); |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 135 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 136 | /* And of course initialize devices on the bus */ |
| 137 | dev_initialize(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 138 | |
| 139 | return BS_POST_DEVICE; |
| 140 | } |
| 141 | |
| 142 | static boot_state_t bs_post_device(void *arg) |
| 143 | { |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 144 | dev_finalize(); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 145 | timestamp_add_now(TS_DEVICE_DONE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 146 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 147 | return BS_OS_RESUME_CHECK; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 150 | static boot_state_t bs_os_resume_check(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 151 | { |
Kyösti Mälkki | 61ab3fe | 2020-06-17 14:17:41 +0300 | [diff] [blame] | 152 | void *wake_vector = NULL; |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 153 | |
Kyösti Mälkki | 61ab3fe | 2020-06-17 14:17:41 +0300 | [diff] [blame] | 154 | if (CONFIG(HAVE_ACPI_RESUME)) |
| 155 | wake_vector = acpi_find_wakeup_vector(); |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 156 | |
| 157 | if (wake_vector != NULL) { |
| 158 | boot_states[BS_OS_RESUME].arg = wake_vector; |
| 159 | return BS_OS_RESUME; |
| 160 | } |
Kyösti Mälkki | 61ab3fe | 2020-06-17 14:17:41 +0300 | [diff] [blame] | 161 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 162 | timestamp_add_now(TS_CBMEM_POST); |
| 163 | |
| 164 | return BS_WRITE_TABLES; |
| 165 | } |
| 166 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 167 | static boot_state_t bs_os_resume(void *wake_vector) |
| 168 | { |
Kyösti Mälkki | 61ab3fe | 2020-06-17 14:17:41 +0300 | [diff] [blame] | 169 | if (CONFIG(HAVE_ACPI_RESUME)) { |
| 170 | arch_bootstate_coreboot_exit(); |
| 171 | acpi_resume(wake_vector); |
Kyösti Mälkki | a4c0e1a | 2020-06-18 08:28:12 +0300 | [diff] [blame] | 172 | /* We will not come back. */ |
Kyösti Mälkki | 61ab3fe | 2020-06-17 14:17:41 +0300 | [diff] [blame] | 173 | } |
Kyösti Mälkki | a4c0e1a | 2020-06-18 08:28:12 +0300 | [diff] [blame] | 174 | die("Failed OS resume\n"); |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 177 | static boot_state_t bs_write_tables(void *arg) |
| 178 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 179 | timestamp_add_now(TS_WRITE_TABLES); |
| 180 | |
| 181 | /* Now that we have collected all of our information |
| 182 | * write our configuration tables. |
| 183 | */ |
| 184 | write_tables(); |
| 185 | |
Paul Menzel | 00563bf | 2016-12-05 23:06:37 +0100 | [diff] [blame] | 186 | timestamp_add_now(TS_FINALIZE_CHIPS); |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 187 | dev_finalize_chips(); |
| 188 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 189 | return BS_PAYLOAD_LOAD; |
| 190 | } |
| 191 | |
| 192 | static boot_state_t bs_payload_load(void *arg) |
| 193 | { |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 194 | payload_load(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 195 | |
| 196 | return BS_PAYLOAD_BOOT; |
| 197 | } |
| 198 | |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 199 | static boot_state_t bs_payload_boot(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 200 | { |
Aaron Durbin | 16bd267 | 2016-12-07 11:58:20 -0600 | [diff] [blame] | 201 | arch_bootstate_coreboot_exit(); |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 202 | payload_run(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 203 | |
Jonathan Neuschäfer | 0a54fb5 | 2016-05-27 09:05:02 +0200 | [diff] [blame] | 204 | printk(BIOS_EMERG, "Boot failed\n"); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 205 | /* Returning from this state will fail because the following signals |
| 206 | * return to a completed state. */ |
| 207 | return BS_PAYLOAD_BOOT; |
| 208 | } |
| 209 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 210 | /* |
| 211 | * Typically a state will take 4 time samples: |
| 212 | * 1. Before state entry callbacks |
| 213 | * 2. After state entry callbacks / Before state function. |
| 214 | * 3. After state function / Before state exit callbacks. |
| 215 | * 4. After state exit callbacks. |
| 216 | */ |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 217 | static void bs_sample_time(struct boot_state *state) |
| 218 | { |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 219 | static const char *const sample_id[] = { "entry", "run", "exit" }; |
| 220 | static struct mono_time previous_sample; |
| 221 | struct mono_time this_sample; |
| 222 | long console; |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 223 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 224 | if (!CONFIG(HAVE_MONOTONIC_TIMER)) |
| 225 | return; |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 226 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 227 | console = console_time_get_and_reset(); |
| 228 | timer_monotonic_get(&this_sample); |
| 229 | state->num_samples++; |
| 230 | |
| 231 | int i = state->num_samples - 2; |
| 232 | if ((i >= 0) && (i < ARRAY_SIZE(sample_id))) { |
| 233 | long execution = mono_time_diff_microseconds(&previous_sample, &this_sample); |
| 234 | |
| 235 | /* Report with millisecond precision to reduce log diffs. */ |
| 236 | execution = DIV_ROUND_CLOSEST(execution, USECS_PER_MSEC); |
| 237 | console = DIV_ROUND_CLOSEST(console, USECS_PER_MSEC); |
| 238 | if (execution) { |
| 239 | printk(BIOS_DEBUG, "BS: %s %s times (exec / console): %ld / %ld ms\n", |
| 240 | state->name, sample_id[i], execution - console, console); |
| 241 | /* Reset again to ignore printk() time above. */ |
| 242 | console_time_get_and_reset(); |
| 243 | } |
| 244 | } |
| 245 | timer_monotonic_get(&previous_sample); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 248 | #if CONFIG(TIMER_QUEUE) |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 249 | static void bs_run_timers(int drain) |
| 250 | { |
| 251 | /* Drain all timer callbacks until none are left, if directed. |
| 252 | * Otherwise run the timers only once. */ |
| 253 | do { |
| 254 | if (!timers_run()) |
| 255 | break; |
| 256 | } while (drain); |
| 257 | } |
| 258 | #else |
| 259 | static void bs_run_timers(int drain) {} |
| 260 | #endif |
| 261 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 262 | static void bs_call_callbacks(struct boot_state *state, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 263 | boot_state_sequence_t seq) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 264 | { |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 265 | struct boot_phase *phase = &state->phases[seq]; |
Raul E Rangel | cb42874 | 2021-06-09 16:49:53 -0600 | [diff] [blame] | 266 | struct mono_time mt_start, mt_stop; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 267 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 268 | while (1) { |
| 269 | if (phase->callbacks != NULL) { |
| 270 | struct boot_state_callback *bscb; |
| 271 | |
| 272 | /* Remove the first callback. */ |
| 273 | bscb = phase->callbacks; |
| 274 | phase->callbacks = bscb->next; |
| 275 | bscb->next = NULL; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 276 | |
Angel Pons | 8315668 | 2021-06-10 13:57:35 +0200 | [diff] [blame] | 277 | if (CONFIG(DEBUG_BOOT_STATE)) { |
| 278 | printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n", |
| 279 | bscb, bscb_location(bscb)); |
Raul E Rangel | cb42874 | 2021-06-09 16:49:53 -0600 | [diff] [blame] | 280 | timer_monotonic_get(&mt_start); |
Angel Pons | 8315668 | 2021-06-10 13:57:35 +0200 | [diff] [blame] | 281 | } |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 282 | bscb->callback(bscb->arg); |
Raul E Rangel | cb42874 | 2021-06-09 16:49:53 -0600 | [diff] [blame] | 283 | if (CONFIG(DEBUG_BOOT_STATE)) { |
| 284 | timer_monotonic_get(&mt_stop); |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 285 | printk(BIOS_DEBUG, "BS: callback (%p) @ %s (%lld ms).\n", bscb, |
Raul E Rangel | cb42874 | 2021-06-09 16:49:53 -0600 | [diff] [blame] | 286 | bscb_location(bscb), |
| 287 | mono_time_diff_microseconds(&mt_start, &mt_stop) |
| 288 | / USECS_PER_MSEC); |
| 289 | } |
Raul E Rangel | b5811c0 | 2021-11-04 11:52:17 -0600 | [diff] [blame] | 290 | |
| 291 | bs_run_timers(0); |
| 292 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 293 | continue; |
| 294 | } |
| 295 | |
| 296 | /* All callbacks are complete and there are no blockers for |
| 297 | * this state. Therefore, this part of the state is complete. */ |
| 298 | if (!phase->blockers) |
| 299 | break; |
| 300 | |
| 301 | /* Something is blocking this state from transitioning. As |
| 302 | * there are no more callbacks a pending timer needs to be |
| 303 | * ran to unblock the state. */ |
| 304 | bs_run_timers(0); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 308 | /* Keep track of the current state. */ |
| 309 | static struct state_tracker { |
| 310 | boot_state_t state_id; |
| 311 | boot_state_sequence_t seq; |
| 312 | } current_phase = { |
| 313 | .state_id = BS_PRE_DEVICE, |
| 314 | .seq = BS_ON_ENTRY, |
| 315 | }; |
| 316 | |
| 317 | static void bs_walk_state_machine(void) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 318 | { |
| 319 | |
| 320 | while (1) { |
| 321 | struct boot_state *state; |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 322 | boot_state_t next_id; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 323 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 324 | state = &boot_states[current_phase.state_id]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 325 | |
| 326 | if (state->complete) { |
| 327 | printk(BIOS_EMERG, "BS: %s state already executed.\n", |
| 328 | state->name); |
| 329 | break; |
| 330 | } |
| 331 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 332 | if (CONFIG(DEBUG_BOOT_STATE)) |
Lee Leahy | 1060535 | 2016-02-14 17:01:40 -0800 | [diff] [blame] | 333 | printk(BIOS_DEBUG, "BS: Entering %s state.\n", |
| 334 | state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 335 | |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 336 | bs_run_timers(0); |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 337 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 338 | bs_sample_time(state); |
| 339 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 340 | bs_call_callbacks(state, current_phase.seq); |
| 341 | /* Update the current sequence so that any calls to block the |
| 342 | * current state from the run_state() function will place a |
| 343 | * block on the correct phase. */ |
| 344 | current_phase.seq = BS_ON_EXIT; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 345 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 346 | bs_sample_time(state); |
| 347 | |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 348 | post_code(state->post_code); |
| 349 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 350 | next_id = state->run_state(state->arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 351 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 352 | if (CONFIG(DEBUG_BOOT_STATE)) |
Lee Leahy | 1060535 | 2016-02-14 17:01:40 -0800 | [diff] [blame] | 353 | printk(BIOS_DEBUG, "BS: Exiting %s state.\n", |
| 354 | state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 355 | |
| 356 | bs_sample_time(state); |
| 357 | |
Raul E Rangel | b5811c0 | 2021-11-04 11:52:17 -0600 | [diff] [blame] | 358 | bs_run_timers(0); |
| 359 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 360 | bs_call_callbacks(state, current_phase.seq); |
| 361 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 362 | if (CONFIG(DEBUG_BOOT_STATE)) |
Lee Leahy | 1060535 | 2016-02-14 17:01:40 -0800 | [diff] [blame] | 363 | printk(BIOS_DEBUG, |
| 364 | "----------------------------------------\n"); |
| 365 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 366 | /* Update the current phase with new state id and sequence. */ |
| 367 | current_phase.state_id = next_id; |
| 368 | current_phase.seq = BS_ON_ENTRY; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 369 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 370 | bs_sample_time(state); |
| 371 | |
Felix Held | 56da0b7 | 2021-10-01 20:03:02 +0200 | [diff] [blame] | 372 | state->complete = true; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | static int boot_state_sched_callback(struct boot_state *state, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 377 | struct boot_state_callback *bscb, |
| 378 | boot_state_sequence_t seq) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 379 | { |
| 380 | if (state->complete) { |
| 381 | printk(BIOS_WARNING, |
| 382 | "Tried to schedule callback on completed state %s.\n", |
| 383 | state->name); |
| 384 | |
| 385 | return -1; |
| 386 | } |
| 387 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 388 | bscb->next = state->phases[seq].callbacks; |
| 389 | state->phases[seq].callbacks = bscb; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | int boot_state_sched_on_entry(struct boot_state_callback *bscb, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 395 | boot_state_t state_id) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 396 | { |
| 397 | struct boot_state *state = &boot_states[state_id]; |
| 398 | |
| 399 | return boot_state_sched_callback(state, bscb, BS_ON_ENTRY); |
| 400 | } |
| 401 | |
| 402 | int boot_state_sched_on_exit(struct boot_state_callback *bscb, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 403 | boot_state_t state_id) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 404 | { |
| 405 | struct boot_state *state = &boot_states[state_id]; |
| 406 | |
| 407 | return boot_state_sched_callback(state, bscb, BS_ON_EXIT); |
| 408 | } |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 409 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 410 | static void boot_state_schedule_static_entries(void) |
| 411 | { |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 412 | extern struct boot_state_init_entry *_bs_init_begin[]; |
| 413 | struct boot_state_init_entry **slot; |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 414 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 415 | for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) { |
| 416 | struct boot_state_init_entry *cur = *slot; |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 417 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 418 | if (cur->when == BS_ON_ENTRY) |
| 419 | boot_state_sched_on_entry(&cur->bscb, cur->state); |
| 420 | else |
| 421 | boot_state_sched_on_exit(&cur->bscb, cur->state); |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Stefan Reinauer | 6adef08 | 2013-05-09 16:30:06 -0700 | [diff] [blame] | 425 | void main(void) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 426 | { |
Nico Huber | e0ed902 | 2016-10-07 12:58:17 +0200 | [diff] [blame] | 427 | /* |
| 428 | * We can generally jump between C and Ada code back and forth |
| 429 | * without trouble. But since we don't have an Ada main() we |
| 430 | * have to do some Ada package initializations that GNAT would |
| 431 | * do there. This has to be done before calling any Ada code. |
| 432 | * |
| 433 | * The package initializations should not have any dependen- |
| 434 | * cies on C code. So we can call them here early, and don't |
| 435 | * have to worry at which point we can start to use Ada. |
| 436 | */ |
| 437 | ramstage_adainit(); |
| 438 | |
Duncan Laurie | a518151 | 2015-06-04 08:42:48 -0700 | [diff] [blame] | 439 | /* TODO: Understand why this is here and move to arch/platform code. */ |
| 440 | /* For MMIO UART this needs to be called before any other printk. */ |
Kyösti Mälkki | 7336f97 | 2020-06-08 06:05:03 +0300 | [diff] [blame] | 441 | if (ENV_X86) |
Duncan Laurie | a518151 | 2015-06-04 08:42:48 -0700 | [diff] [blame] | 442 | init_timer(); |
| 443 | |
Aaron Durbin | be34797 | 2015-04-21 11:57:18 -0500 | [diff] [blame] | 444 | /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure |
| 445 | * it is the very first thing done in ramstage.*/ |
| 446 | console_init(); |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 447 | post_code(POSTCODE_CONSOLE_READY); |
Aaron Durbin | be34797 | 2015-04-21 11:57:18 -0500 | [diff] [blame] | 448 | |
Asami Doi | 38a1286 | 2019-08-22 11:15:12 +0900 | [diff] [blame] | 449 | exception_init(); |
| 450 | |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 451 | /* |
Subrata Banik | 91160e1 | 2019-01-29 17:16:15 +0530 | [diff] [blame] | 452 | * CBMEM needs to be recovered because timestamps, ACPI, etc rely on |
Kyösti Mälkki | 513a1a8 | 2018-06-03 12:29:50 +0300 | [diff] [blame] | 453 | * the cbmem infrastructure being around. Explicitly recover it. |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 454 | */ |
Kyösti Mälkki | 513a1a8 | 2018-06-03 12:29:50 +0300 | [diff] [blame] | 455 | cbmem_initialize(); |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 456 | |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 457 | timestamp_add_now(TS_RAMSTAGE_START); |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 458 | post_code(POSTCODE_ENTRY_HARDWAREMAIN); |
Li-Ta Lo | 6a8745ae | 2004-05-13 20:39:07 +0000 | [diff] [blame] | 459 | |
Kyösti Mälkki | db8693b | 2014-06-19 23:29:07 +0300 | [diff] [blame] | 460 | /* Handoff sleep type from romstage. */ |
Kyösti Mälkki | b8c7ea0 | 2020-11-17 16:41:38 +0200 | [diff] [blame] | 461 | acpi_is_wakeup_s3(); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 462 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 463 | /* Schedule the static boot state entries. */ |
| 464 | boot_state_schedule_static_entries(); |
| 465 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 466 | bs_walk_state_machine(); |
| 467 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 468 | die("Boot state machine failure.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 471 | |
| 472 | int boot_state_block(boot_state_t state, boot_state_sequence_t seq) |
| 473 | { |
| 474 | struct boot_phase *bp; |
| 475 | |
| 476 | /* Blocking a previously ran state is not appropriate. */ |
| 477 | if (current_phase.state_id > state || |
Lee Leahy | d638ef4 | 2017-03-07 09:47:22 -0800 | [diff] [blame] | 478 | (current_phase.state_id == state && current_phase.seq > seq)) { |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 479 | printk(BIOS_WARNING, |
| 480 | "BS: Completed state (%d, %d) block attempted.\n", |
| 481 | state, seq); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | bp = &boot_states[state].phases[seq]; |
| 486 | bp->blockers++; |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq) |
| 492 | { |
| 493 | struct boot_phase *bp; |
| 494 | |
| 495 | /* Blocking a previously ran state is not appropriate. */ |
| 496 | if (current_phase.state_id > state || |
Lee Leahy | d638ef4 | 2017-03-07 09:47:22 -0800 | [diff] [blame] | 497 | (current_phase.state_id == state && current_phase.seq > seq)) { |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 498 | printk(BIOS_WARNING, |
| 499 | "BS: Completed state (%d, %d) unblock attempted.\n", |
| 500 | state, seq); |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | bp = &boot_states[state].phases[seq]; |
| 505 | |
| 506 | if (bp->blockers == 0) { |
| 507 | printk(BIOS_WARNING, |
| 508 | "BS: Unblock attempted on non-blocked state (%d, %d).\n", |
| 509 | state, seq); |
| 510 | return -1; |
| 511 | } |
| 512 | |
| 513 | bp->blockers--; |
| 514 | |
| 515 | return 0; |
| 516 | } |