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> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 34 | #include <boot/elf.h> |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 35 | #include <cbfs.h> |
Ronald G. Minnich | 9764d4c | 2012-06-12 16:29:32 -0700 | [diff] [blame] | 36 | #include <lib.h> |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 37 | #if CONFIG_HAVE_ACPI_RESUME |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 38 | #include <arch/acpi.h> |
Stefan Reinauer | 3935b19 | 2009-04-14 06:38:15 +0000 | [diff] [blame] | 39 | #endif |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 40 | #include <cbmem.h> |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 41 | #include <coverage.h> |
Stefan Reinauer | bf729ba | 2011-11-04 12:31:58 -0700 | [diff] [blame] | 42 | #include <timestamp.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 43 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame^] | 44 | #if BOOT_STATE_DEBUG |
| 45 | #define BS_DEBUG_LVL BIOS_DEBUG |
| 46 | #else |
| 47 | #define BS_DEBUG_LVL BIOS_NEVER |
| 48 | #endif |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 49 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame^] | 50 | static boot_state_t bs_pre_device(void *arg); |
| 51 | static boot_state_t bs_dev_init_chips(void *arg); |
| 52 | static boot_state_t bs_dev_enumerate(void *arg); |
| 53 | static boot_state_t bs_dev_resources(void *arg); |
| 54 | static boot_state_t bs_dev_eanble(void *arg); |
| 55 | static boot_state_t bs_dev_init(void *arg); |
| 56 | static boot_state_t bs_post_device(void *arg); |
| 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 | |
| 62 | struct boot_state { |
| 63 | const char *name; |
| 64 | boot_state_t id; |
| 65 | struct boot_state_callback *seq_callbacks[2]; |
| 66 | boot_state_t (*run_state)(void *arg); |
| 67 | void *arg; |
| 68 | int complete; |
| 69 | }; |
| 70 | |
| 71 | #define BS_INIT(state_, run_func_) \ |
| 72 | { \ |
| 73 | .name = #state_, \ |
| 74 | .id = state_, \ |
| 75 | .seq_callbacks = { NULL, NULL },\ |
| 76 | .run_state = run_func_, \ |
| 77 | .arg = NULL, \ |
| 78 | .complete = 0 \ |
| 79 | } |
| 80 | #define BS_INIT_ENTRY(state_, run_func_) \ |
| 81 | [state_] = BS_INIT(state_, run_func_) |
| 82 | |
| 83 | static struct boot_state boot_states[] = { |
| 84 | BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device), |
| 85 | BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips), |
| 86 | BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate), |
| 87 | BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources), |
| 88 | BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_eanble), |
| 89 | BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init), |
| 90 | BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device), |
| 91 | BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume), |
| 92 | BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables), |
| 93 | BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load), |
| 94 | BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot), |
| 95 | }; |
| 96 | |
| 97 | static boot_state_t bs_pre_device(void *arg) |
| 98 | { |
| 99 | init_cbmem_pre_device(); |
| 100 | return BS_DEV_INIT_CHIPS; |
| 101 | } |
| 102 | |
| 103 | static boot_state_t bs_dev_init_chips(void *arg) |
| 104 | { |
| 105 | timestamp_stash(TS_DEVICE_ENUMERATE); |
| 106 | |
| 107 | /* Initialize chips early, they might disable unused devices. */ |
| 108 | dev_initialize_chips(); |
| 109 | |
| 110 | return BS_DEV_ENUMERATE; |
| 111 | } |
| 112 | |
| 113 | static boot_state_t bs_dev_enumerate(void *arg) |
| 114 | { |
| 115 | /* Find the devices we don't have hard coded knowledge about. */ |
| 116 | dev_enumerate(); |
| 117 | post_code(POST_DEVICE_ENUMERATION_COMPLETE); |
| 118 | |
| 119 | return BS_DEV_RESOURCES; |
| 120 | } |
| 121 | |
| 122 | static boot_state_t bs_dev_resources(void *arg) |
| 123 | { |
| 124 | timestamp_stash(TS_DEVICE_CONFIGURE); |
| 125 | /* Now compute and assign the bus resources. */ |
| 126 | dev_configure(); |
| 127 | post_code(POST_DEVICE_CONFIGURATION_COMPLETE); |
| 128 | |
| 129 | return BS_DEV_ENABLE; |
| 130 | } |
| 131 | |
| 132 | static boot_state_t bs_dev_eanble(void *arg) |
| 133 | { |
| 134 | timestamp_stash(TS_DEVICE_ENABLE); |
| 135 | /* Now actually enable devices on the bus */ |
| 136 | dev_enable(); |
| 137 | post_code(POST_DEVICES_ENABLED); |
| 138 | |
| 139 | return BS_DEV_INIT; |
| 140 | } |
| 141 | |
| 142 | static boot_state_t bs_dev_init(void *arg) |
| 143 | { |
| 144 | timestamp_stash(TS_DEVICE_INITIALIZE); |
| 145 | /* And of course initialize devices on the bus */ |
| 146 | dev_initialize(); |
| 147 | post_code(POST_DEVICES_INITIALIZED); |
| 148 | |
| 149 | return BS_POST_DEVICE; |
| 150 | } |
| 151 | |
| 152 | static boot_state_t bs_post_device(void *arg) |
| 153 | { |
| 154 | timestamp_stash(TS_DEVICE_DONE); |
| 155 | |
| 156 | init_cbmem_post_device(); |
| 157 | |
| 158 | timestamp_sync(); |
| 159 | |
| 160 | return BS_OS_RESUME; |
| 161 | } |
| 162 | |
| 163 | static boot_state_t bs_os_resume(void *arg) |
| 164 | { |
| 165 | #if CONFIG_HAVE_ACPI_RESUME |
| 166 | suspend_resume(); |
| 167 | post_code(0x8a); |
| 168 | #endif |
| 169 | |
| 170 | timestamp_add_now(TS_CBMEM_POST); |
| 171 | |
| 172 | return BS_WRITE_TABLES; |
| 173 | } |
| 174 | |
| 175 | static boot_state_t bs_write_tables(void *arg) |
| 176 | { |
| 177 | if (cbmem_post_handling) |
| 178 | cbmem_post_handling(); |
| 179 | |
| 180 | timestamp_add_now(TS_WRITE_TABLES); |
| 181 | |
| 182 | /* Now that we have collected all of our information |
| 183 | * write our configuration tables. |
| 184 | */ |
| 185 | write_tables(); |
| 186 | |
| 187 | return BS_PAYLOAD_LOAD; |
| 188 | } |
| 189 | |
| 190 | static boot_state_t bs_payload_load(void *arg) |
| 191 | { |
| 192 | void *payload; |
| 193 | |
| 194 | timestamp_add_now(TS_LOAD_PAYLOAD); |
| 195 | |
| 196 | payload = cbfs_load_payload(CBFS_DEFAULT_MEDIA, |
| 197 | CONFIG_CBFS_PREFIX "/payload"); |
| 198 | if (! payload) |
| 199 | die("Could not find a payload\n"); |
| 200 | |
| 201 | /* Pass the payload to the next state. */ |
| 202 | boot_states[BS_PAYLOAD_BOOT].arg = payload; |
| 203 | |
| 204 | return BS_PAYLOAD_BOOT; |
| 205 | } |
| 206 | |
| 207 | static boot_state_t bs_payload_boot(void *payload) |
| 208 | { |
| 209 | selfboot(get_lb_mem(), payload); |
| 210 | |
| 211 | printk(BIOS_EMERG, "Boot failed"); |
| 212 | /* Returning from this state will fail because the following signals |
| 213 | * return to a completed state. */ |
| 214 | return BS_PAYLOAD_BOOT; |
| 215 | } |
| 216 | |
| 217 | static void bs_call_callbacks(struct boot_state *state, |
| 218 | boot_state_sequence_t seq) |
| 219 | { |
| 220 | while (state->seq_callbacks[seq] != NULL) { |
| 221 | struct boot_state_callback *bscb; |
| 222 | |
| 223 | /* Remove the first callback. */ |
| 224 | bscb = state->seq_callbacks[seq]; |
| 225 | state->seq_callbacks[seq] = bscb->next; |
| 226 | bscb->next = NULL; |
| 227 | |
| 228 | #if BOOT_STATE_DEBUG |
| 229 | printk(BS_DEBUG_LVL, "BS: callback (%p) @ %s.\n", |
| 230 | bscb, bscb->location); |
| 231 | #endif |
| 232 | bscb->callback(bscb->arg); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static void bs_walk_state_machine(boot_state_t current_state_id) |
| 237 | { |
| 238 | |
| 239 | while (1) { |
| 240 | struct boot_state *state; |
| 241 | |
| 242 | state = &boot_states[current_state_id]; |
| 243 | |
| 244 | if (state->complete) { |
| 245 | printk(BIOS_EMERG, "BS: %s state already executed.\n", |
| 246 | state->name); |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | printk(BS_DEBUG_LVL, "BS: Entering %s state.\n", state->name); |
| 251 | bs_call_callbacks(state, BS_ON_ENTRY); |
| 252 | |
| 253 | current_state_id = state->run_state(state->arg); |
| 254 | |
| 255 | printk(BS_DEBUG_LVL, "BS: Exiting %s state.\n", state->name); |
| 256 | bs_call_callbacks(state, BS_ON_EXIT); |
| 257 | |
| 258 | state->complete = 1; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static int boot_state_sched_callback(struct boot_state *state, |
| 263 | struct boot_state_callback *bscb, |
| 264 | boot_state_sequence_t seq) |
| 265 | { |
| 266 | if (state->complete) { |
| 267 | printk(BIOS_WARNING, |
| 268 | "Tried to schedule callback on completed state %s.\n", |
| 269 | state->name); |
| 270 | |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | bscb->next = state->seq_callbacks[seq]; |
| 275 | state->seq_callbacks[seq] = bscb; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | int boot_state_sched_on_entry(struct boot_state_callback *bscb, |
| 281 | boot_state_t state_id) |
| 282 | { |
| 283 | struct boot_state *state = &boot_states[state_id]; |
| 284 | |
| 285 | return boot_state_sched_callback(state, bscb, BS_ON_ENTRY); |
| 286 | } |
| 287 | |
| 288 | int boot_state_sched_on_exit(struct boot_state_callback *bscb, |
| 289 | boot_state_t state_id) |
| 290 | { |
| 291 | struct boot_state *state = &boot_states[state_id]; |
| 292 | |
| 293 | return boot_state_sched_callback(state, bscb, BS_ON_EXIT); |
| 294 | } |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 295 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 296 | void hardwaremain(int boot_complete) |
| 297 | { |
Stefan Reinauer | 4221a19 | 2012-10-15 15:23:20 -0700 | [diff] [blame] | 298 | timestamp_stash(TS_START_RAMSTAGE); |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 299 | post_code(POST_ENTRY_RAMSTAGE); |
Li-Ta Lo | 6a8745ae | 2004-05-13 20:39:07 +0000 | [diff] [blame] | 300 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 301 | #if CONFIG_COVERAGE |
| 302 | coverage_init(); |
| 303 | #endif |
| 304 | |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 305 | /* console_init() MUST PRECEDE ALL printk()! */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 306 | console_init(); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 307 | |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 308 | post_code(POST_CONSOLE_READY); |
arch import user (historical) | 54d6b08 | 2005-07-06 17:17:41 +0000 | [diff] [blame] | 309 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 310 | printk(BIOS_NOTICE, "coreboot-%s%s %s %s...\n", |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 311 | coreboot_version, coreboot_extra_version, coreboot_build, |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 312 | (boot_complete)?"rebooting":"booting"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 313 | |
Alexandru Gagniuc | 5005bb06 | 2011-04-11 20:17:22 +0000 | [diff] [blame] | 314 | post_code(POST_CONSOLE_BOOT_MSG); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 315 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 316 | /* If we have already booted attempt a hard reboot */ |
| 317 | if (boot_complete) { |
| 318 | hard_reset(); |
| 319 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 320 | |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 321 | /* FIXME: Is there a better way to handle this? */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 322 | init_timer(); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 323 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame^] | 324 | bs_walk_state_machine(BS_PRE_DEVICE); |
| 325 | die("Boot state machine failure.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 326 | } |
| 327 | |