Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame^] | 17 | * Foundation, Inc. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | |
| 21 | /* |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 22 | * C Bootstrap code for the coreboot |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
Julius Werner | 85620db | 2013-11-13 18:22:15 -0800 | [diff] [blame] | 25 | #include <arch/exception.h> |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 26 | #include <bootstate.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 27 | #include <console/console.h> |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 28 | #include <console/post_codes.h> |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 29 | #include <cbmem.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | #include <version.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 31 | #include <device/device.h> |
| 32 | #include <device/pci.h> |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame] | 33 | #include <delay.h> |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 35 | #include <reset.h> |
Stefan Reinauer | 7e9771c | 2009-04-22 08:17:38 +0000 | [diff] [blame] | 36 | #include <boot/tables.h> |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 37 | #include <program_loading.h> |
Ronald G. Minnich | 9764d4c | 2012-06-12 16:29:32 -0700 | [diff] [blame] | 38 | #include <lib.h> |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 39 | #if CONFIG_HAVE_ACPI_RESUME |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 40 | #include <arch/acpi.h> |
Stefan Reinauer | 3935b19 | 2009-04-14 06:38:15 +0000 | [diff] [blame] | 41 | #endif |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 42 | #include <timer.h> |
Stefan Reinauer | bf729ba | 2011-11-04 12:31:58 -0700 | [diff] [blame] | 43 | #include <timestamp.h> |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 44 | #include <thread.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 45 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 46 | #if BOOT_STATE_DEBUG |
| 47 | #define BS_DEBUG_LVL BIOS_DEBUG |
| 48 | #else |
| 49 | #define BS_DEBUG_LVL BIOS_NEVER |
| 50 | #endif |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 51 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 52 | static boot_state_t bs_pre_device(void *arg); |
| 53 | static boot_state_t bs_dev_init_chips(void *arg); |
| 54 | static boot_state_t bs_dev_enumerate(void *arg); |
| 55 | static boot_state_t bs_dev_resources(void *arg); |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 56 | static boot_state_t bs_dev_enable(void *arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 57 | static boot_state_t bs_dev_init(void *arg); |
| 58 | static boot_state_t bs_post_device(void *arg); |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 59 | static boot_state_t bs_os_resume_check(void *arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 60 | static boot_state_t bs_os_resume(void *arg); |
| 61 | static boot_state_t bs_write_tables(void *arg); |
| 62 | static boot_state_t bs_payload_load(void *arg); |
| 63 | static boot_state_t bs_payload_boot(void *arg); |
| 64 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 65 | /* |
| 66 | * Typically a state will take 4 time samples: |
| 67 | * 1. Before state entry callbacks |
| 68 | * 2. After state entry callbacks / Before state function. |
| 69 | * 3. After state function / Before state exit callbacks. |
| 70 | * 4. After state exit callbacks. |
| 71 | */ |
| 72 | #define MAX_TIME_SAMPLES 4 |
| 73 | struct boot_state_times { |
| 74 | int num_samples; |
| 75 | struct mono_time samples[MAX_TIME_SAMPLES]; |
| 76 | }; |
| 77 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 78 | /* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be |
| 79 | * blocked from transitioning to the next (state,seq) pair. When the blockers |
| 80 | * field is 0 a transition may occur. */ |
| 81 | struct boot_phase { |
| 82 | struct boot_state_callback *callbacks; |
| 83 | int blockers; |
| 84 | }; |
| 85 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 86 | struct boot_state { |
| 87 | const char *name; |
| 88 | boot_state_t id; |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 89 | u8 post_code; |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 90 | struct boot_phase phases[2]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 91 | boot_state_t (*run_state)(void *arg); |
| 92 | void *arg; |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 93 | int complete : 1; |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 94 | #if CONFIG_HAVE_MONOTONIC_TIMER |
| 95 | struct boot_state_times times; |
| 96 | #endif |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 97 | }; |
| 98 | |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 99 | #define BS_INIT(state_, run_func_) \ |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 100 | { \ |
| 101 | .name = #state_, \ |
| 102 | .id = state_, \ |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 103 | .post_code = POST_ ## state_, \ |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 104 | .phases = { { NULL, 0 }, { NULL, 0 } }, \ |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 105 | .run_state = run_func_, \ |
| 106 | .arg = NULL, \ |
| 107 | .complete = 0, \ |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 108 | } |
| 109 | #define BS_INIT_ENTRY(state_, run_func_) \ |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 110 | [state_] = BS_INIT(state_, run_func_) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 111 | |
| 112 | static struct boot_state boot_states[] = { |
| 113 | BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device), |
| 114 | BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips), |
| 115 | BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate), |
| 116 | BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources), |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 117 | BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable), |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 118 | BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init), |
| 119 | BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device), |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 120 | BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check), |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 121 | BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume), |
| 122 | BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables), |
| 123 | BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load), |
| 124 | BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot), |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static boot_state_t bs_pre_device(void *arg) |
| 128 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 129 | return BS_DEV_INIT_CHIPS; |
| 130 | } |
| 131 | |
| 132 | static boot_state_t bs_dev_init_chips(void *arg) |
| 133 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 134 | timestamp_add_now(TS_DEVICE_ENUMERATE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 135 | |
| 136 | /* Initialize chips early, they might disable unused devices. */ |
| 137 | dev_initialize_chips(); |
| 138 | |
| 139 | return BS_DEV_ENUMERATE; |
| 140 | } |
| 141 | |
| 142 | static boot_state_t bs_dev_enumerate(void *arg) |
| 143 | { |
| 144 | /* Find the devices we don't have hard coded knowledge about. */ |
| 145 | dev_enumerate(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 146 | |
| 147 | return BS_DEV_RESOURCES; |
| 148 | } |
| 149 | |
| 150 | static boot_state_t bs_dev_resources(void *arg) |
| 151 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 152 | timestamp_add_now(TS_DEVICE_CONFIGURE); |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 153 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 154 | /* Now compute and assign the bus resources. */ |
| 155 | dev_configure(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 156 | |
| 157 | return BS_DEV_ENABLE; |
| 158 | } |
| 159 | |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 160 | static boot_state_t bs_dev_enable(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 161 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 162 | timestamp_add_now(TS_DEVICE_ENABLE); |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 163 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 164 | /* Now actually enable devices on the bus */ |
| 165 | dev_enable(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 166 | |
| 167 | return BS_DEV_INIT; |
| 168 | } |
| 169 | |
| 170 | static boot_state_t bs_dev_init(void *arg) |
| 171 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 172 | timestamp_add_now(TS_DEVICE_INITIALIZE); |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 173 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 174 | /* And of course initialize devices on the bus */ |
| 175 | dev_initialize(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 176 | |
| 177 | return BS_POST_DEVICE; |
| 178 | } |
| 179 | |
| 180 | static boot_state_t bs_post_device(void *arg) |
| 181 | { |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 182 | dev_finalize(); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 183 | timestamp_add_now(TS_DEVICE_DONE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 184 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 185 | return BS_OS_RESUME_CHECK; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 188 | static boot_state_t bs_os_resume_check(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 189 | { |
| 190 | #if CONFIG_HAVE_ACPI_RESUME |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 191 | void *wake_vector; |
| 192 | |
| 193 | wake_vector = acpi_find_wakeup_vector(); |
| 194 | |
| 195 | if (wake_vector != NULL) { |
| 196 | boot_states[BS_OS_RESUME].arg = wake_vector; |
| 197 | return BS_OS_RESUME; |
| 198 | } |
Kyösti Mälkki | 7b14f08 | 2014-10-16 20:58:47 +0300 | [diff] [blame] | 199 | |
| 200 | acpi_prepare_resume_backup(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 201 | #endif |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 202 | timestamp_add_now(TS_CBMEM_POST); |
| 203 | |
| 204 | return BS_WRITE_TABLES; |
| 205 | } |
| 206 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 207 | static boot_state_t bs_os_resume(void *wake_vector) |
| 208 | { |
| 209 | #if CONFIG_HAVE_ACPI_RESUME |
| 210 | acpi_resume(wake_vector); |
| 211 | #endif |
| 212 | return BS_WRITE_TABLES; |
| 213 | } |
| 214 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 215 | static boot_state_t bs_write_tables(void *arg) |
| 216 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 217 | timestamp_add_now(TS_WRITE_TABLES); |
| 218 | |
| 219 | /* Now that we have collected all of our information |
| 220 | * write our configuration tables. |
| 221 | */ |
| 222 | write_tables(); |
| 223 | |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame] | 224 | dev_finalize_chips(); |
| 225 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 226 | return BS_PAYLOAD_LOAD; |
| 227 | } |
| 228 | |
| 229 | static boot_state_t bs_payload_load(void *arg) |
| 230 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 231 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 232 | |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 233 | payload_load(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 234 | |
| 235 | return BS_PAYLOAD_BOOT; |
| 236 | } |
| 237 | |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 238 | static boot_state_t bs_payload_boot(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 239 | { |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 240 | payload_run(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 241 | |
| 242 | printk(BIOS_EMERG, "Boot failed"); |
| 243 | /* Returning from this state will fail because the following signals |
| 244 | * return to a completed state. */ |
| 245 | return BS_PAYLOAD_BOOT; |
| 246 | } |
| 247 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 248 | #if CONFIG_HAVE_MONOTONIC_TIMER |
| 249 | static void bs_sample_time(struct boot_state *state) |
| 250 | { |
| 251 | struct mono_time *mt; |
| 252 | |
| 253 | mt = &state->times.samples[state->times.num_samples]; |
| 254 | timer_monotonic_get(mt); |
| 255 | state->times.num_samples++; |
| 256 | } |
| 257 | |
| 258 | static void bs_report_time(struct boot_state *state) |
| 259 | { |
Aaron Durbin | ad5a909 | 2014-09-24 09:48:47 -0500 | [diff] [blame] | 260 | long entry_time; |
| 261 | long run_time; |
| 262 | long exit_time; |
| 263 | struct mono_time *samples = &state->times.samples[0]; |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 264 | |
Aaron Durbin | ad5a909 | 2014-09-24 09:48:47 -0500 | [diff] [blame] | 265 | entry_time = mono_time_diff_microseconds(&samples[0], &samples[1]); |
| 266 | run_time = mono_time_diff_microseconds(&samples[1], &samples[2]); |
| 267 | exit_time = mono_time_diff_microseconds(&samples[2], &samples[3]); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 268 | |
| 269 | printk(BIOS_DEBUG, "BS: %s times (us): entry %ld run %ld exit %ld\n", |
Aaron Durbin | ad5a909 | 2014-09-24 09:48:47 -0500 | [diff] [blame] | 270 | state->name, entry_time, run_time, exit_time); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 271 | } |
| 272 | #else |
| 273 | static inline void bs_sample_time(struct boot_state *state) {} |
| 274 | static inline void bs_report_time(struct boot_state *state) {} |
| 275 | #endif |
| 276 | |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 277 | #if CONFIG_TIMER_QUEUE |
| 278 | static void bs_run_timers(int drain) |
| 279 | { |
| 280 | /* Drain all timer callbacks until none are left, if directed. |
| 281 | * Otherwise run the timers only once. */ |
| 282 | do { |
| 283 | if (!timers_run()) |
| 284 | break; |
| 285 | } while (drain); |
| 286 | } |
| 287 | #else |
| 288 | static void bs_run_timers(int drain) {} |
| 289 | #endif |
| 290 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 291 | static void bs_call_callbacks(struct boot_state *state, |
| 292 | boot_state_sequence_t seq) |
| 293 | { |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 294 | struct boot_phase *phase = &state->phases[seq]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 295 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 296 | while (1) { |
| 297 | if (phase->callbacks != NULL) { |
| 298 | struct boot_state_callback *bscb; |
| 299 | |
| 300 | /* Remove the first callback. */ |
| 301 | bscb = phase->callbacks; |
| 302 | phase->callbacks = bscb->next; |
| 303 | bscb->next = NULL; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 304 | |
| 305 | #if BOOT_STATE_DEBUG |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 306 | printk(BS_DEBUG_LVL, "BS: callback (%p) @ %s.\n", |
| 307 | bscb, bscb->location); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 308 | #endif |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 309 | bscb->callback(bscb->arg); |
| 310 | |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | /* All callbacks are complete and there are no blockers for |
| 315 | * this state. Therefore, this part of the state is complete. */ |
| 316 | if (!phase->blockers) |
| 317 | break; |
| 318 | |
| 319 | /* Something is blocking this state from transitioning. As |
| 320 | * there are no more callbacks a pending timer needs to be |
| 321 | * ran to unblock the state. */ |
| 322 | bs_run_timers(0); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 326 | /* Keep track of the current state. */ |
| 327 | static struct state_tracker { |
| 328 | boot_state_t state_id; |
| 329 | boot_state_sequence_t seq; |
| 330 | } current_phase = { |
| 331 | .state_id = BS_PRE_DEVICE, |
| 332 | .seq = BS_ON_ENTRY, |
| 333 | }; |
| 334 | |
| 335 | static void bs_walk_state_machine(void) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 336 | { |
| 337 | |
| 338 | while (1) { |
| 339 | struct boot_state *state; |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 340 | boot_state_t next_id; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 341 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 342 | state = &boot_states[current_phase.state_id]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 343 | |
| 344 | if (state->complete) { |
| 345 | printk(BIOS_EMERG, "BS: %s state already executed.\n", |
| 346 | state->name); |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | printk(BS_DEBUG_LVL, "BS: Entering %s state.\n", state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 351 | |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 352 | bs_run_timers(0); |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 353 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 354 | bs_sample_time(state); |
| 355 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 356 | bs_call_callbacks(state, current_phase.seq); |
| 357 | /* Update the current sequence so that any calls to block the |
| 358 | * current state from the run_state() function will place a |
| 359 | * block on the correct phase. */ |
| 360 | current_phase.seq = BS_ON_EXIT; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 361 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 362 | bs_sample_time(state); |
| 363 | |
Duncan Laurie | cb73a84 | 2013-06-10 10:41:04 -0700 | [diff] [blame] | 364 | post_code(state->post_code); |
| 365 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 366 | next_id = state->run_state(state->arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 367 | |
| 368 | printk(BS_DEBUG_LVL, "BS: Exiting %s state.\n", state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 369 | |
| 370 | bs_sample_time(state); |
| 371 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 372 | bs_call_callbacks(state, current_phase.seq); |
| 373 | |
| 374 | /* Update the current phase with new state id and sequence. */ |
| 375 | current_phase.state_id = next_id; |
| 376 | current_phase.seq = BS_ON_ENTRY; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 377 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 378 | bs_sample_time(state); |
| 379 | |
| 380 | bs_report_time(state); |
| 381 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 382 | state->complete = 1; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static int boot_state_sched_callback(struct boot_state *state, |
| 387 | struct boot_state_callback *bscb, |
| 388 | boot_state_sequence_t seq) |
| 389 | { |
| 390 | if (state->complete) { |
| 391 | printk(BIOS_WARNING, |
| 392 | "Tried to schedule callback on completed state %s.\n", |
| 393 | state->name); |
| 394 | |
| 395 | return -1; |
| 396 | } |
| 397 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 398 | bscb->next = state->phases[seq].callbacks; |
| 399 | state->phases[seq].callbacks = bscb; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | int boot_state_sched_on_entry(struct boot_state_callback *bscb, |
| 405 | boot_state_t state_id) |
| 406 | { |
| 407 | struct boot_state *state = &boot_states[state_id]; |
| 408 | |
| 409 | return boot_state_sched_callback(state, bscb, BS_ON_ENTRY); |
| 410 | } |
| 411 | |
| 412 | int boot_state_sched_on_exit(struct boot_state_callback *bscb, |
| 413 | boot_state_t state_id) |
| 414 | { |
| 415 | struct boot_state *state = &boot_states[state_id]; |
| 416 | |
| 417 | return boot_state_sched_callback(state, bscb, BS_ON_EXIT); |
| 418 | } |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 419 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 420 | static void boot_state_schedule_static_entries(void) |
| 421 | { |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 422 | extern struct boot_state_init_entry *_bs_init_begin[]; |
| 423 | struct boot_state_init_entry **slot; |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 424 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 425 | for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) { |
| 426 | struct boot_state_init_entry *cur = *slot; |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 427 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 428 | if (cur->when == BS_ON_ENTRY) |
| 429 | boot_state_sched_on_entry(&cur->bscb, cur->state); |
| 430 | else |
| 431 | boot_state_sched_on_exit(&cur->bscb, cur->state); |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Stefan Reinauer | 6adef08 | 2013-05-09 16:30:06 -0700 | [diff] [blame] | 435 | void main(void) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 436 | { |
Aaron Durbin | be34797 | 2015-04-21 11:57:18 -0500 | [diff] [blame] | 437 | /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure |
| 438 | * it is the very first thing done in ramstage.*/ |
| 439 | console_init(); |
| 440 | |
| 441 | post_code(POST_CONSOLE_READY); |
| 442 | |
Aaron Durbin | b0d8f5e | 2015-04-06 16:12:58 -0500 | [diff] [blame] | 443 | /* |
| 444 | * CBMEM needs to be recovered in the EARLY_CBMEM_INIT case because |
| 445 | * timestamps, APCI, etc rely on the cbmem infrastructure being |
| 446 | * around. Explicitly recover it. |
| 447 | */ |
| 448 | if (IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)) |
| 449 | cbmem_initialize(); |
| 450 | |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 451 | /* Record current time, try to locate timestamps in CBMEM. */ |
Stefan Reinauer | 3a6550d | 2013-08-01 13:31:44 -0700 | [diff] [blame] | 452 | timestamp_init(timestamp_get()); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 453 | |
| 454 | timestamp_add_now(TS_START_RAMSTAGE); |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 455 | post_code(POST_ENTRY_RAMSTAGE); |
Li-Ta Lo | 6a8745ae | 2004-05-13 20:39:07 +0000 | [diff] [blame] | 456 | |
Kyösti Mälkki | db8693b | 2014-06-19 23:29:07 +0300 | [diff] [blame] | 457 | /* Handoff sleep type from romstage. */ |
| 458 | #if CONFIG_HAVE_ACPI_RESUME |
| 459 | acpi_is_wakeup(); |
| 460 | #endif |
| 461 | |
Julius Werner | 85620db | 2013-11-13 18:22:15 -0800 | [diff] [blame] | 462 | exception_init(); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 463 | threads_initialize(); |
| 464 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 465 | /* Schedule the static boot state entries. */ |
| 466 | boot_state_schedule_static_entries(); |
| 467 | |
Julius Werner | 44cf870 | 2014-12-08 13:39:14 -0800 | [diff] [blame] | 468 | /* TODO: Understand why this is here and move to arch/platform code. */ |
| 469 | if (IS_ENABLED(CONFIG_ARCH_X86)) |
| 470 | init_timer(); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 471 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 472 | bs_walk_state_machine(); |
| 473 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 474 | die("Boot state machine failure.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 477 | |
| 478 | int boot_state_block(boot_state_t state, boot_state_sequence_t seq) |
| 479 | { |
| 480 | struct boot_phase *bp; |
| 481 | |
| 482 | /* Blocking a previously ran state is not appropriate. */ |
| 483 | if (current_phase.state_id > state || |
| 484 | (current_phase.state_id == state && current_phase.seq > seq) ) { |
| 485 | printk(BIOS_WARNING, |
| 486 | "BS: Completed state (%d, %d) block attempted.\n", |
| 487 | state, seq); |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | bp = &boot_states[state].phases[seq]; |
| 492 | bp->blockers++; |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq) |
| 498 | { |
| 499 | struct boot_phase *bp; |
| 500 | |
| 501 | /* Blocking a previously ran state is not appropriate. */ |
| 502 | if (current_phase.state_id > state || |
| 503 | (current_phase.state_id == state && current_phase.seq > seq) ) { |
| 504 | printk(BIOS_WARNING, |
| 505 | "BS: Completed state (%d, %d) unblock attempted.\n", |
| 506 | state, seq); |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | bp = &boot_states[state].phases[seq]; |
| 511 | |
| 512 | if (bp->blockers == 0) { |
| 513 | printk(BIOS_WARNING, |
| 514 | "BS: Unblock attempted on non-blocked state (%d, %d).\n", |
| 515 | state, seq); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | bp->blockers--; |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | void boot_state_current_block(void) |
| 525 | { |
| 526 | boot_state_block(current_phase.state_id, current_phase.seq); |
| 527 | } |
| 528 | |
| 529 | void boot_state_current_unblock(void) |
| 530 | { |
| 531 | boot_state_unblock(current_phase.state_id, current_phase.seq); |
| 532 | } |