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