Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | /* |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 6 | * C Bootstrap code for the coreboot |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Nico Huber | e0ed902 | 2016-10-07 12:58:17 +0200 | [diff] [blame] | 9 | #include <adainit.h> |
Julius Werner | 85620db | 2013-11-13 18:22:15 -0800 | [diff] [blame] | 10 | #include <arch/exception.h> |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 11 | #include <bootstate.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 12 | #include <console/console.h> |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 13 | #include <console/post_codes.h> |
Kyösti Mälkki | 463ad51 | 2019-11-01 09:12:34 +0200 | [diff] [blame] | 14 | #include <commonlib/helpers.h> |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 15 | #include <cbmem.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 16 | #include <version.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 17 | #include <device/device.h> |
| 18 | #include <device/pci.h> |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame] | 19 | #include <delay.h> |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
Stefan Reinauer | 7e9771c | 2009-04-22 08:17:38 +0000 | [diff] [blame] | 21 | #include <boot/tables.h> |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 22 | #include <program_loading.h> |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 23 | #if CONFIG(HAVE_ACPI_RESUME) |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 24 | #include <arch/acpi.h> |
Stefan Reinauer | 3935b19 | 2009-04-14 06:38:15 +0000 | [diff] [blame] | 25 | #endif |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 26 | #include <timer.h> |
Stefan Reinauer | bf729ba | 2011-11-04 12:31:58 -0700 | [diff] [blame] | 27 | #include <timestamp.h> |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 28 | #include <thread.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; |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 59 | int complete : 1; |
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, \ |
| 70 | .complete = 0, \ |
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 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 155 | #if CONFIG(HAVE_ACPI_RESUME) |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 156 | void *wake_vector; |
| 157 | |
| 158 | wake_vector = acpi_find_wakeup_vector(); |
| 159 | |
| 160 | if (wake_vector != NULL) { |
| 161 | boot_states[BS_OS_RESUME].arg = wake_vector; |
| 162 | return BS_OS_RESUME; |
| 163 | } |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 164 | #endif |
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 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 172 | #if CONFIG(HAVE_ACPI_RESUME) |
Aaron Durbin | 16bd267 | 2016-12-07 11:58:20 -0600 | [diff] [blame] | 173 | arch_bootstate_coreboot_exit(); |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 174 | acpi_resume(wake_vector); |
| 175 | #endif |
| 176 | return BS_WRITE_TABLES; |
| 177 | } |
| 178 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 179 | static boot_state_t bs_write_tables(void *arg) |
| 180 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 181 | timestamp_add_now(TS_WRITE_TABLES); |
| 182 | |
| 183 | /* Now that we have collected all of our information |
| 184 | * write our configuration tables. |
| 185 | */ |
| 186 | write_tables(); |
| 187 | |
Paul Menzel | 00563bf | 2016-12-05 23:06:37 +0100 | [diff] [blame] | 188 | timestamp_add_now(TS_FINALIZE_CHIPS); |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 189 | dev_finalize_chips(); |
| 190 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 191 | return BS_PAYLOAD_LOAD; |
| 192 | } |
| 193 | |
| 194 | static boot_state_t bs_payload_load(void *arg) |
| 195 | { |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 196 | payload_load(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 197 | |
| 198 | return BS_PAYLOAD_BOOT; |
| 199 | } |
| 200 | |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 201 | static boot_state_t bs_payload_boot(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 202 | { |
Aaron Durbin | 16bd267 | 2016-12-07 11:58:20 -0600 | [diff] [blame] | 203 | arch_bootstate_coreboot_exit(); |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 204 | payload_run(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 205 | |
Jonathan Neuschäfer | 0a54fb5 | 2016-05-27 09:05:02 +0200 | [diff] [blame] | 206 | printk(BIOS_EMERG, "Boot failed\n"); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 207 | /* Returning from this state will fail because the following signals |
| 208 | * return to a completed state. */ |
| 209 | return BS_PAYLOAD_BOOT; |
| 210 | } |
| 211 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 212 | /* |
| 213 | * Typically a state will take 4 time samples: |
| 214 | * 1. Before state entry callbacks |
| 215 | * 2. After state entry callbacks / Before state function. |
| 216 | * 3. After state function / Before state exit callbacks. |
| 217 | * 4. After state exit callbacks. |
| 218 | */ |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 219 | static void bs_sample_time(struct boot_state *state) |
| 220 | { |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 221 | static const char *const sample_id[] = { "entry", "run", "exit" }; |
| 222 | static struct mono_time previous_sample; |
| 223 | struct mono_time this_sample; |
| 224 | long console; |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 225 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 226 | if (!CONFIG(HAVE_MONOTONIC_TIMER)) |
| 227 | return; |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 228 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 229 | console = console_time_get_and_reset(); |
| 230 | timer_monotonic_get(&this_sample); |
| 231 | state->num_samples++; |
| 232 | |
| 233 | int i = state->num_samples - 2; |
| 234 | if ((i >= 0) && (i < ARRAY_SIZE(sample_id))) { |
| 235 | long execution = mono_time_diff_microseconds(&previous_sample, &this_sample); |
| 236 | |
| 237 | /* Report with millisecond precision to reduce log diffs. */ |
| 238 | execution = DIV_ROUND_CLOSEST(execution, USECS_PER_MSEC); |
| 239 | console = DIV_ROUND_CLOSEST(console, USECS_PER_MSEC); |
| 240 | if (execution) { |
| 241 | printk(BIOS_DEBUG, "BS: %s %s times (exec / console): %ld / %ld ms\n", |
| 242 | state->name, sample_id[i], execution - console, console); |
| 243 | /* Reset again to ignore printk() time above. */ |
| 244 | console_time_get_and_reset(); |
| 245 | } |
| 246 | } |
| 247 | timer_monotonic_get(&previous_sample); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 248 | } |
| 249 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 250 | #if CONFIG(TIMER_QUEUE) |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 251 | static void bs_run_timers(int drain) |
| 252 | { |
| 253 | /* Drain all timer callbacks until none are left, if directed. |
| 254 | * Otherwise run the timers only once. */ |
| 255 | do { |
| 256 | if (!timers_run()) |
| 257 | break; |
| 258 | } while (drain); |
| 259 | } |
| 260 | #else |
| 261 | static void bs_run_timers(int drain) {} |
| 262 | #endif |
| 263 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 264 | static void bs_call_callbacks(struct boot_state *state, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 265 | boot_state_sequence_t seq) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 266 | { |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 267 | struct boot_phase *phase = &state->phases[seq]; |
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 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 278 | #if CONFIG(DEBUG_BOOT_STATE) |
Lee Leahy | 1060535 | 2016-02-14 17:01:40 -0800 | [diff] [blame] | 279 | printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n", |
| 280 | bscb, bscb->location); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 281 | #endif |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 282 | bscb->callback(bscb->arg); |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 283 | continue; |
| 284 | } |
| 285 | |
| 286 | /* All callbacks are complete and there are no blockers for |
| 287 | * this state. Therefore, this part of the state is complete. */ |
| 288 | if (!phase->blockers) |
| 289 | break; |
| 290 | |
| 291 | /* Something is blocking this state from transitioning. As |
| 292 | * there are no more callbacks a pending timer needs to be |
| 293 | * ran to unblock the state. */ |
| 294 | bs_run_timers(0); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 298 | /* Keep track of the current state. */ |
| 299 | static struct state_tracker { |
| 300 | boot_state_t state_id; |
| 301 | boot_state_sequence_t seq; |
| 302 | } current_phase = { |
| 303 | .state_id = BS_PRE_DEVICE, |
| 304 | .seq = BS_ON_ENTRY, |
| 305 | }; |
| 306 | |
| 307 | static void bs_walk_state_machine(void) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 308 | { |
| 309 | |
| 310 | while (1) { |
| 311 | struct boot_state *state; |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 312 | boot_state_t next_id; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 313 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 314 | state = &boot_states[current_phase.state_id]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 315 | |
| 316 | if (state->complete) { |
| 317 | printk(BIOS_EMERG, "BS: %s state already executed.\n", |
| 318 | state->name); |
| 319 | break; |
| 320 | } |
| 321 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 322 | if (CONFIG(DEBUG_BOOT_STATE)) |
Lee Leahy | 1060535 | 2016-02-14 17:01:40 -0800 | [diff] [blame] | 323 | printk(BIOS_DEBUG, "BS: Entering %s state.\n", |
| 324 | state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 325 | |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 326 | bs_run_timers(0); |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 327 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 328 | bs_sample_time(state); |
| 329 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 330 | bs_call_callbacks(state, current_phase.seq); |
| 331 | /* Update the current sequence so that any calls to block the |
| 332 | * current state from the run_state() function will place a |
| 333 | * block on the correct phase. */ |
| 334 | current_phase.seq = BS_ON_EXIT; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 335 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 336 | bs_sample_time(state); |
| 337 | |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 338 | post_code(state->post_code); |
| 339 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 340 | next_id = state->run_state(state->arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 341 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 342 | if (CONFIG(DEBUG_BOOT_STATE)) |
Lee Leahy | 1060535 | 2016-02-14 17:01:40 -0800 | [diff] [blame] | 343 | printk(BIOS_DEBUG, "BS: Exiting %s state.\n", |
| 344 | state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 345 | |
| 346 | bs_sample_time(state); |
| 347 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 348 | bs_call_callbacks(state, current_phase.seq); |
| 349 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 350 | if (CONFIG(DEBUG_BOOT_STATE)) |
Lee Leahy | 1060535 | 2016-02-14 17:01:40 -0800 | [diff] [blame] | 351 | printk(BIOS_DEBUG, |
| 352 | "----------------------------------------\n"); |
| 353 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 354 | /* Update the current phase with new state id and sequence. */ |
| 355 | current_phase.state_id = next_id; |
| 356 | current_phase.seq = BS_ON_ENTRY; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 357 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 358 | bs_sample_time(state); |
| 359 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 360 | state->complete = 1; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static int boot_state_sched_callback(struct boot_state *state, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 365 | struct boot_state_callback *bscb, |
| 366 | boot_state_sequence_t seq) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 367 | { |
| 368 | if (state->complete) { |
| 369 | printk(BIOS_WARNING, |
| 370 | "Tried to schedule callback on completed state %s.\n", |
| 371 | state->name); |
| 372 | |
| 373 | return -1; |
| 374 | } |
| 375 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 376 | bscb->next = state->phases[seq].callbacks; |
| 377 | state->phases[seq].callbacks = bscb; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | int boot_state_sched_on_entry(struct boot_state_callback *bscb, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 383 | boot_state_t state_id) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 384 | { |
| 385 | struct boot_state *state = &boot_states[state_id]; |
| 386 | |
| 387 | return boot_state_sched_callback(state, bscb, BS_ON_ENTRY); |
| 388 | } |
| 389 | |
| 390 | int boot_state_sched_on_exit(struct boot_state_callback *bscb, |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 391 | boot_state_t state_id) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 392 | { |
| 393 | struct boot_state *state = &boot_states[state_id]; |
| 394 | |
| 395 | return boot_state_sched_callback(state, bscb, BS_ON_EXIT); |
| 396 | } |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 397 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 398 | static void boot_state_schedule_static_entries(void) |
| 399 | { |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 400 | extern struct boot_state_init_entry *_bs_init_begin[]; |
| 401 | struct boot_state_init_entry **slot; |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 402 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 403 | for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) { |
| 404 | struct boot_state_init_entry *cur = *slot; |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 405 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 406 | if (cur->when == BS_ON_ENTRY) |
| 407 | boot_state_sched_on_entry(&cur->bscb, cur->state); |
| 408 | else |
| 409 | boot_state_sched_on_exit(&cur->bscb, cur->state); |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Stefan Reinauer | 6adef08 | 2013-05-09 16:30:06 -0700 | [diff] [blame] | 413 | void main(void) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 414 | { |
Nico Huber | e0ed902 | 2016-10-07 12:58:17 +0200 | [diff] [blame] | 415 | /* |
| 416 | * We can generally jump between C and Ada code back and forth |
| 417 | * without trouble. But since we don't have an Ada main() we |
| 418 | * have to do some Ada package initializations that GNAT would |
| 419 | * do there. This has to be done before calling any Ada code. |
| 420 | * |
| 421 | * The package initializations should not have any dependen- |
| 422 | * cies on C code. So we can call them here early, and don't |
| 423 | * have to worry at which point we can start to use Ada. |
| 424 | */ |
| 425 | ramstage_adainit(); |
| 426 | |
Duncan Laurie | a518151 | 2015-06-04 08:42:48 -0700 | [diff] [blame] | 427 | /* TODO: Understand why this is here and move to arch/platform code. */ |
| 428 | /* For MMIO UART this needs to be called before any other printk. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 429 | if (CONFIG(ARCH_X86)) |
Duncan Laurie | a518151 | 2015-06-04 08:42:48 -0700 | [diff] [blame] | 430 | init_timer(); |
| 431 | |
Aaron Durbin | be34797 | 2015-04-21 11:57:18 -0500 | [diff] [blame] | 432 | /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure |
| 433 | * it is the very first thing done in ramstage.*/ |
| 434 | console_init(); |
Aaron Durbin | be34797 | 2015-04-21 11:57:18 -0500 | [diff] [blame] | 435 | post_code(POST_CONSOLE_READY); |
| 436 | |
Asami Doi | 38a1286 | 2019-08-22 11:15:12 +0900 | [diff] [blame] | 437 | exception_init(); |
| 438 | |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 439 | /* |
Subrata Banik | 91160e1 | 2019-01-29 17:16:15 +0530 | [diff] [blame] | 440 | * 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] | 441 | * the cbmem infrastructure being around. Explicitly recover it. |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 442 | */ |
Kyösti Mälkki | 513a1a8 | 2018-06-03 12:29:50 +0300 | [diff] [blame] | 443 | cbmem_initialize(); |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 444 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 445 | timestamp_add_now(TS_START_RAMSTAGE); |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 446 | post_code(POST_ENTRY_RAMSTAGE); |
Li-Ta Lo | 6a8745ae | 2004-05-13 20:39:07 +0000 | [diff] [blame] | 447 | |
Kyösti Mälkki | db8693b | 2014-06-19 23:29:07 +0300 | [diff] [blame] | 448 | /* Handoff sleep type from romstage. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 449 | #if CONFIG(HAVE_ACPI_RESUME) |
Kyösti Mälkki | db8693b | 2014-06-19 23:29:07 +0300 | [diff] [blame] | 450 | acpi_is_wakeup(); |
| 451 | #endif |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 452 | threads_initialize(); |
| 453 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 454 | /* Schedule the static boot state entries. */ |
| 455 | boot_state_schedule_static_entries(); |
| 456 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 457 | bs_walk_state_machine(); |
| 458 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 459 | die("Boot state machine failure.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 462 | |
| 463 | int boot_state_block(boot_state_t state, boot_state_sequence_t seq) |
| 464 | { |
| 465 | struct boot_phase *bp; |
| 466 | |
| 467 | /* Blocking a previously ran state is not appropriate. */ |
| 468 | if (current_phase.state_id > state || |
Lee Leahy | d638ef4 | 2017-03-07 09:47:22 -0800 | [diff] [blame] | 469 | (current_phase.state_id == state && current_phase.seq > seq)) { |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 470 | printk(BIOS_WARNING, |
| 471 | "BS: Completed state (%d, %d) block attempted.\n", |
| 472 | state, seq); |
| 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | bp = &boot_states[state].phases[seq]; |
| 477 | bp->blockers++; |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | int boot_state_unblock(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) unblock attempted.\n", |
| 491 | state, seq); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | bp = &boot_states[state].phases[seq]; |
| 496 | |
| 497 | if (bp->blockers == 0) { |
| 498 | printk(BIOS_WARNING, |
| 499 | "BS: Unblock attempted on non-blocked state (%d, %d).\n", |
| 500 | state, seq); |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | bp->blockers--; |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | void boot_state_current_block(void) |
| 510 | { |
| 511 | boot_state_block(current_phase.state_id, current_phase.seq); |
| 512 | } |
| 513 | |
| 514 | void boot_state_current_unblock(void) |
| 515 | { |
| 516 | boot_state_unblock(current_phase.state_id, current_phase.seq); |
| 517 | } |