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