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 |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
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 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 25 | #include <bootstate.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 26 | #include <console/console.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 27 | #include <version.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 28 | #include <device/device.h> |
| 29 | #include <device/pci.h> |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame] | 30 | #include <delay.h> |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 32 | #include <reset.h> |
Stefan Reinauer | 7e9771c | 2009-04-22 08:17:38 +0000 | [diff] [blame] | 33 | #include <boot/tables.h> |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 34 | #include <cbfs.h> |
Ronald G. Minnich | 9764d4c | 2012-06-12 16:29:32 -0700 | [diff] [blame] | 35 | #include <lib.h> |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 36 | #if CONFIG_HAVE_ACPI_RESUME |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 37 | #include <arch/acpi.h> |
Stefan Reinauer | 3935b19 | 2009-04-14 06:38:15 +0000 | [diff] [blame] | 38 | #endif |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 39 | #include <timer.h> |
Stefan Reinauer | bf729ba | 2011-11-04 12:31:58 -0700 | [diff] [blame] | 40 | #include <timestamp.h> |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 41 | #include <thread.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 42 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 43 | #if BOOT_STATE_DEBUG |
| 44 | #define BS_DEBUG_LVL BIOS_DEBUG |
| 45 | #else |
| 46 | #define BS_DEBUG_LVL BIOS_NEVER |
| 47 | #endif |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 48 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 49 | static boot_state_t bs_pre_device(void *arg); |
| 50 | static boot_state_t bs_dev_init_chips(void *arg); |
| 51 | static boot_state_t bs_dev_enumerate(void *arg); |
| 52 | static boot_state_t bs_dev_resources(void *arg); |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 53 | static boot_state_t bs_dev_enable(void *arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 54 | static boot_state_t bs_dev_init(void *arg); |
| 55 | static boot_state_t bs_post_device(void *arg); |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 56 | static boot_state_t bs_os_resume_check(void *arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 57 | static boot_state_t bs_os_resume(void *arg); |
| 58 | static boot_state_t bs_write_tables(void *arg); |
| 59 | static boot_state_t bs_payload_load(void *arg); |
| 60 | static boot_state_t bs_payload_boot(void *arg); |
| 61 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 62 | /* |
| 63 | * Typically a state will take 4 time samples: |
| 64 | * 1. Before state entry callbacks |
| 65 | * 2. After state entry callbacks / Before state function. |
| 66 | * 3. After state function / Before state exit callbacks. |
| 67 | * 4. After state exit callbacks. |
| 68 | */ |
| 69 | #define MAX_TIME_SAMPLES 4 |
| 70 | struct boot_state_times { |
| 71 | int num_samples; |
| 72 | struct mono_time samples[MAX_TIME_SAMPLES]; |
| 73 | }; |
| 74 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 75 | /* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be |
| 76 | * blocked from transitioning to the next (state,seq) pair. When the blockers |
| 77 | * field is 0 a transition may occur. */ |
| 78 | struct boot_phase { |
| 79 | struct boot_state_callback *callbacks; |
| 80 | int blockers; |
| 81 | }; |
| 82 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 83 | struct boot_state { |
| 84 | const char *name; |
| 85 | boot_state_t id; |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 86 | struct boot_phase phases[2]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 87 | boot_state_t (*run_state)(void *arg); |
| 88 | void *arg; |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 89 | int complete : 1; |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 90 | #if CONFIG_HAVE_MONOTONIC_TIMER |
| 91 | struct boot_state_times times; |
| 92 | #endif |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 95 | #define BS_INIT(state_, run_func_) \ |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 96 | { \ |
| 97 | .name = #state_, \ |
| 98 | .id = state_, \ |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 99 | .phases = { { NULL, 0 }, { NULL, 0 } }, \ |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 100 | .run_state = run_func_, \ |
| 101 | .arg = NULL, \ |
| 102 | .complete = 0, \ |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 103 | } |
| 104 | #define BS_INIT_ENTRY(state_, run_func_) \ |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 105 | [state_] = BS_INIT(state_, run_func_) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 106 | |
| 107 | static struct boot_state boot_states[] = { |
| 108 | BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device), |
| 109 | BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips), |
| 110 | BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate), |
| 111 | BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources), |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 112 | BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable), |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 113 | BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init), |
| 114 | BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device), |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 115 | BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check), |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 116 | BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume), |
| 117 | BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables), |
| 118 | BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load), |
| 119 | BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot), |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static boot_state_t bs_pre_device(void *arg) |
| 123 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 124 | return BS_DEV_INIT_CHIPS; |
| 125 | } |
| 126 | |
| 127 | static boot_state_t bs_dev_init_chips(void *arg) |
| 128 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 129 | timestamp_add_now(TS_DEVICE_ENUMERATE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 130 | |
| 131 | /* Initialize chips early, they might disable unused devices. */ |
| 132 | dev_initialize_chips(); |
| 133 | |
| 134 | return BS_DEV_ENUMERATE; |
| 135 | } |
| 136 | |
| 137 | static boot_state_t bs_dev_enumerate(void *arg) |
| 138 | { |
| 139 | /* Find the devices we don't have hard coded knowledge about. */ |
| 140 | dev_enumerate(); |
| 141 | post_code(POST_DEVICE_ENUMERATION_COMPLETE); |
| 142 | |
| 143 | return BS_DEV_RESOURCES; |
| 144 | } |
| 145 | |
| 146 | static boot_state_t bs_dev_resources(void *arg) |
| 147 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 148 | timestamp_add_now(TS_DEVICE_CONFIGURE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 149 | /* Now compute and assign the bus resources. */ |
| 150 | dev_configure(); |
| 151 | post_code(POST_DEVICE_CONFIGURATION_COMPLETE); |
| 152 | |
| 153 | return BS_DEV_ENABLE; |
| 154 | } |
| 155 | |
David Hendricks | cca6859 | 2013-06-20 14:08:27 -0700 | [diff] [blame] | 156 | static boot_state_t bs_dev_enable(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 157 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 158 | timestamp_add_now(TS_DEVICE_ENABLE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 159 | /* Now actually enable devices on the bus */ |
| 160 | dev_enable(); |
| 161 | post_code(POST_DEVICES_ENABLED); |
| 162 | |
| 163 | return BS_DEV_INIT; |
| 164 | } |
| 165 | |
| 166 | static boot_state_t bs_dev_init(void *arg) |
| 167 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 168 | timestamp_add_now(TS_DEVICE_INITIALIZE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 169 | /* And of course initialize devices on the bus */ |
| 170 | dev_initialize(); |
| 171 | post_code(POST_DEVICES_INITIALIZED); |
| 172 | |
| 173 | return BS_POST_DEVICE; |
| 174 | } |
| 175 | |
| 176 | static boot_state_t bs_post_device(void *arg) |
| 177 | { |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame^] | 178 | dev_finalize(); |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 179 | timestamp_add_now(TS_DEVICE_DONE); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 180 | |
Kyösti Mälkki | cbf5bdf | 2013-09-10 00:07:21 +0300 | [diff] [blame] | 181 | timestamp_reinit(); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 182 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 183 | return BS_OS_RESUME_CHECK; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 186 | static boot_state_t bs_os_resume_check(void *arg) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 187 | { |
| 188 | #if CONFIG_HAVE_ACPI_RESUME |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 189 | void *wake_vector; |
| 190 | |
| 191 | wake_vector = acpi_find_wakeup_vector(); |
| 192 | |
| 193 | if (wake_vector != NULL) { |
| 194 | boot_states[BS_OS_RESUME].arg = wake_vector; |
| 195 | return BS_OS_RESUME; |
| 196 | } |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 197 | post_code(0x8a); |
| 198 | #endif |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 199 | timestamp_add_now(TS_CBMEM_POST); |
| 200 | |
| 201 | return BS_WRITE_TABLES; |
| 202 | } |
| 203 | |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 204 | static boot_state_t bs_os_resume(void *wake_vector) |
| 205 | { |
| 206 | #if CONFIG_HAVE_ACPI_RESUME |
| 207 | acpi_resume(wake_vector); |
| 208 | #endif |
| 209 | return BS_WRITE_TABLES; |
| 210 | } |
| 211 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 212 | static boot_state_t bs_write_tables(void *arg) |
| 213 | { |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 214 | timestamp_add_now(TS_WRITE_TABLES); |
| 215 | |
| 216 | /* Now that we have collected all of our information |
| 217 | * write our configuration tables. |
| 218 | */ |
| 219 | write_tables(); |
| 220 | |
Marc Jones | 2a58ecd | 2013-10-29 17:32:00 -0600 | [diff] [blame^] | 221 | dev_finalize_chips(); |
| 222 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 223 | return BS_PAYLOAD_LOAD; |
| 224 | } |
| 225 | |
| 226 | static boot_state_t bs_payload_load(void *arg) |
| 227 | { |
| 228 | void *payload; |
Aaron Durbin | 001de1a | 2013-04-24 22:59:45 -0500 | [diff] [blame] | 229 | void *entry; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 230 | |
| 231 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 232 | |
| 233 | payload = cbfs_load_payload(CBFS_DEFAULT_MEDIA, |
| 234 | CONFIG_CBFS_PREFIX "/payload"); |
| 235 | if (! payload) |
| 236 | die("Could not find a payload\n"); |
| 237 | |
Aaron Durbin | 001de1a | 2013-04-24 22:59:45 -0500 | [diff] [blame] | 238 | entry = selfload(get_lb_mem(), payload); |
| 239 | |
| 240 | if (! entry) |
| 241 | die("Could not load payload\n"); |
| 242 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 243 | /* Pass the payload to the next state. */ |
Aaron Durbin | 001de1a | 2013-04-24 22:59:45 -0500 | [diff] [blame] | 244 | boot_states[BS_PAYLOAD_BOOT].arg = entry; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 245 | |
| 246 | return BS_PAYLOAD_BOOT; |
| 247 | } |
| 248 | |
Aaron Durbin | 001de1a | 2013-04-24 22:59:45 -0500 | [diff] [blame] | 249 | static boot_state_t bs_payload_boot(void *entry) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 250 | { |
Aaron Durbin | 001de1a | 2013-04-24 22:59:45 -0500 | [diff] [blame] | 251 | selfboot(entry); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 252 | |
| 253 | printk(BIOS_EMERG, "Boot failed"); |
| 254 | /* Returning from this state will fail because the following signals |
| 255 | * return to a completed state. */ |
| 256 | return BS_PAYLOAD_BOOT; |
| 257 | } |
| 258 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 259 | #if CONFIG_HAVE_MONOTONIC_TIMER |
| 260 | static void bs_sample_time(struct boot_state *state) |
| 261 | { |
| 262 | struct mono_time *mt; |
| 263 | |
| 264 | mt = &state->times.samples[state->times.num_samples]; |
| 265 | timer_monotonic_get(mt); |
| 266 | state->times.num_samples++; |
| 267 | } |
| 268 | |
| 269 | static void bs_report_time(struct boot_state *state) |
| 270 | { |
| 271 | struct rela_time entry_time; |
| 272 | struct rela_time run_time; |
| 273 | struct rela_time exit_time; |
| 274 | struct boot_state_times *times; |
| 275 | |
| 276 | times = &state->times; |
| 277 | entry_time = mono_time_diff(×->samples[0], ×->samples[1]); |
| 278 | run_time = mono_time_diff(×->samples[1], ×->samples[2]); |
| 279 | exit_time = mono_time_diff(×->samples[2], ×->samples[3]); |
| 280 | |
| 281 | printk(BIOS_DEBUG, "BS: %s times (us): entry %ld run %ld exit %ld\n", |
| 282 | state->name, |
| 283 | rela_time_in_microseconds(&entry_time), |
| 284 | rela_time_in_microseconds(&run_time), |
| 285 | rela_time_in_microseconds(&exit_time)); |
| 286 | } |
| 287 | #else |
| 288 | static inline void bs_sample_time(struct boot_state *state) {} |
| 289 | static inline void bs_report_time(struct boot_state *state) {} |
| 290 | #endif |
| 291 | |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 292 | #if CONFIG_TIMER_QUEUE |
| 293 | static void bs_run_timers(int drain) |
| 294 | { |
| 295 | /* Drain all timer callbacks until none are left, if directed. |
| 296 | * Otherwise run the timers only once. */ |
| 297 | do { |
| 298 | if (!timers_run()) |
| 299 | break; |
| 300 | } while (drain); |
| 301 | } |
| 302 | #else |
| 303 | static void bs_run_timers(int drain) {} |
| 304 | #endif |
| 305 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 306 | static void bs_call_callbacks(struct boot_state *state, |
| 307 | boot_state_sequence_t seq) |
| 308 | { |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 309 | struct boot_phase *phase = &state->phases[seq]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 310 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 311 | while (1) { |
| 312 | if (phase->callbacks != NULL) { |
| 313 | struct boot_state_callback *bscb; |
| 314 | |
| 315 | /* Remove the first callback. */ |
| 316 | bscb = phase->callbacks; |
| 317 | phase->callbacks = bscb->next; |
| 318 | bscb->next = NULL; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 319 | |
| 320 | #if BOOT_STATE_DEBUG |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 321 | printk(BS_DEBUG_LVL, "BS: callback (%p) @ %s.\n", |
| 322 | bscb, bscb->location); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 323 | #endif |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 324 | bscb->callback(bscb->arg); |
| 325 | |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | /* All callbacks are complete and there are no blockers for |
| 330 | * this state. Therefore, this part of the state is complete. */ |
| 331 | if (!phase->blockers) |
| 332 | break; |
| 333 | |
| 334 | /* Something is blocking this state from transitioning. As |
| 335 | * there are no more callbacks a pending timer needs to be |
| 336 | * ran to unblock the state. */ |
| 337 | bs_run_timers(0); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 341 | /* Keep track of the current state. */ |
| 342 | static struct state_tracker { |
| 343 | boot_state_t state_id; |
| 344 | boot_state_sequence_t seq; |
| 345 | } current_phase = { |
| 346 | .state_id = BS_PRE_DEVICE, |
| 347 | .seq = BS_ON_ENTRY, |
| 348 | }; |
| 349 | |
| 350 | static void bs_walk_state_machine(void) |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 351 | { |
| 352 | |
| 353 | while (1) { |
| 354 | struct boot_state *state; |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 355 | boot_state_t next_id; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 356 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 357 | state = &boot_states[current_phase.state_id]; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 358 | |
| 359 | if (state->complete) { |
| 360 | printk(BIOS_EMERG, "BS: %s state already executed.\n", |
| 361 | state->name); |
| 362 | break; |
| 363 | } |
| 364 | |
| 365 | printk(BS_DEBUG_LVL, "BS: Entering %s state.\n", state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 366 | |
Aaron Durbin | 15c671e | 2013-05-06 10:52:24 -0500 | [diff] [blame] | 367 | bs_run_timers(0); |
Aaron Durbin | 8fc41e1 | 2013-04-29 23:22:01 -0500 | [diff] [blame] | 368 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 369 | bs_sample_time(state); |
| 370 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 371 | bs_call_callbacks(state, current_phase.seq); |
| 372 | /* Update the current sequence so that any calls to block the |
| 373 | * current state from the run_state() function will place a |
| 374 | * block on the correct phase. */ |
| 375 | current_phase.seq = BS_ON_EXIT; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 376 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 377 | bs_sample_time(state); |
| 378 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 379 | next_id = state->run_state(state->arg); |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 380 | |
| 381 | printk(BS_DEBUG_LVL, "BS: Exiting %s state.\n", state->name); |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 382 | |
| 383 | bs_sample_time(state); |
| 384 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 385 | bs_call_callbacks(state, current_phase.seq); |
| 386 | |
| 387 | /* Update the current phase with new state id and sequence. */ |
| 388 | current_phase.state_id = next_id; |
| 389 | current_phase.seq = BS_ON_ENTRY; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 390 | |
Aaron Durbin | 6b0fb0d | 2013-04-26 20:54:16 -0500 | [diff] [blame] | 391 | bs_sample_time(state); |
| 392 | |
| 393 | bs_report_time(state); |
| 394 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 395 | state->complete = 1; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static int boot_state_sched_callback(struct boot_state *state, |
| 400 | struct boot_state_callback *bscb, |
| 401 | boot_state_sequence_t seq) |
| 402 | { |
| 403 | if (state->complete) { |
| 404 | printk(BIOS_WARNING, |
| 405 | "Tried to schedule callback on completed state %s.\n", |
| 406 | state->name); |
| 407 | |
| 408 | return -1; |
| 409 | } |
| 410 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame] | 411 | bscb->next = state->phases[seq].callbacks; |
| 412 | state->phases[seq].callbacks = bscb; |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int boot_state_sched_on_entry(struct boot_state_callback *bscb, |
| 418 | boot_state_t state_id) |
| 419 | { |
| 420 | struct boot_state *state = &boot_states[state_id]; |
| 421 | |
| 422 | return boot_state_sched_callback(state, bscb, BS_ON_ENTRY); |
| 423 | } |
| 424 | |
| 425 | int boot_state_sched_on_exit(struct boot_state_callback *bscb, |
| 426 | boot_state_t state_id) |
| 427 | { |
| 428 | struct boot_state *state = &boot_states[state_id]; |
| 429 | |
| 430 | return boot_state_sched_callback(state, bscb, BS_ON_EXIT); |
| 431 | } |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 432 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 433 | static void boot_state_schedule_static_entries(void) |
| 434 | { |
| 435 | extern struct boot_state_init_entry _bs_init_begin; |
| 436 | extern struct boot_state_init_entry _bs_init_end; |
| 437 | struct boot_state_init_entry *cur; |
| 438 | |
| 439 | cur = &_bs_init_begin; |
| 440 | |
| 441 | while (cur != &_bs_init_end) { |
| 442 | if (cur->when == BS_ON_ENTRY) |
| 443 | boot_state_sched_on_entry(&cur->bscb, cur->state); |
| 444 | else |
| 445 | boot_state_sched_on_exit(&cur->bscb, cur->state); |
| 446 | cur++; |
| 447 | } |
| 448 | } |
| 449 | |
Stefan Reinauer | 6adef08 | 2013-05-09 16:30:06 -0700 | [diff] [blame] | 450 | void main(void) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 451 | { |
Kyösti Mälkki | b766b1c | 2013-09-07 17:26:08 +0300 | [diff] [blame] | 452 | /* Record current time, try to locate timestamps in CBMEM. */ |
| 453 | timestamp_init(rdtsc()); |
| 454 | |
| 455 | timestamp_add_now(TS_START_RAMSTAGE); |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 456 | post_code(POST_ENTRY_RAMSTAGE); |
Li-Ta Lo | 6a8745ae | 2004-05-13 20:39:07 +0000 | [diff] [blame] | 457 | |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 458 | /* console_init() MUST PRECEDE ALL printk()! */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 459 | console_init(); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 460 | |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 461 | post_code(POST_CONSOLE_READY); |
arch import user (historical) | 54d6b08 | 2005-07-06 17:17:41 +0000 | [diff] [blame] | 462 | |
Stefan Reinauer | 2a3c106 | 2013-05-06 16:49:56 -0700 | [diff] [blame] | 463 | printk(BIOS_NOTICE, "coreboot-%s%s %s booting...\n", |
| 464 | coreboot_version, coreboot_extra_version, coreboot_build); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 465 | |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 466 | post_code(POST_CONSOLE_BOOT_MSG); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 467 | |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 468 | threads_initialize(); |
| 469 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 470 | /* Schedule the static boot state entries. */ |
| 471 | boot_state_schedule_static_entries(); |
| 472 | |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 473 | /* FIXME: Is there a better way to handle this? */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 474 | init_timer(); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 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 || |
| 488 | (current_phase.state_id == state && current_phase.seq > seq) ) { |
| 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 || |
| 507 | (current_phase.state_id == state && current_phase.seq > seq) ) { |
| 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 | } |
| 527 | |
| 528 | void boot_state_current_block(void) |
| 529 | { |
| 530 | boot_state_block(current_phase.state_id, current_phase.seq); |
| 531 | } |
| 532 | |
| 533 | void boot_state_current_unblock(void) |
| 534 | { |
| 535 | boot_state_unblock(current_phase.state_id, current_phase.seq); |
| 536 | } |