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