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