blob: ed2a51637299cc92d5ba57d17bf51034e5c4f774 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Aaron Durbin7e35efa2013-04-24 15:14:01 -05002 * 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 Biederman8ca8d762003-04-22 19:02:15 +000018 */
19
20
21/*
Stefan Reinauerf8ee1802008-01-18 15:08:58 +000022 * C Bootstrap code for the coreboot
Eric Biederman8ca8d762003-04-22 19:02:15 +000023 */
24
Aaron Durbin7e35efa2013-04-24 15:14:01 -050025#include <bootstate.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000026#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000027#include <version.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000028#include <device/device.h>
29#include <device/pci.h>
Eric Biederman9b4336c2003-07-19 04:28:22 +000030#include <delay.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000031#include <stdlib.h>
Stefan Reinauerde3206a2010-02-22 06:09:43 +000032#include <reset.h>
Stefan Reinauer7e9771c2009-04-22 08:17:38 +000033#include <boot/tables.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000034#include <boot/elf.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000035#include <cbfs.h>
Ronald G. Minnich9764d4c2012-06-12 16:29:32 -070036#include <lib.h>
Stefan Reinauer08670622009-06-30 15:17:49 +000037#if CONFIG_HAVE_ACPI_RESUME
Rudolf Mareka572f832009-04-13 17:57:44 +000038#include <arch/acpi.h>
Stefan Reinauer3935b192009-04-14 06:38:15 +000039#endif
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070040#include <timestamp.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000041
Aaron Durbin7e35efa2013-04-24 15:14:01 -050042#if BOOT_STATE_DEBUG
43#define BS_DEBUG_LVL BIOS_DEBUG
44#else
45#define BS_DEBUG_LVL BIOS_NEVER
46#endif
Rudolf Mareka572f832009-04-13 17:57:44 +000047
Aaron Durbin7e35efa2013-04-24 15:14:01 -050048static boot_state_t bs_pre_device(void *arg);
49static boot_state_t bs_dev_init_chips(void *arg);
50static boot_state_t bs_dev_enumerate(void *arg);
51static boot_state_t bs_dev_resources(void *arg);
52static boot_state_t bs_dev_eanble(void *arg);
53static boot_state_t bs_dev_init(void *arg);
54static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050055static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050056static boot_state_t bs_os_resume(void *arg);
57static boot_state_t bs_write_tables(void *arg);
58static boot_state_t bs_payload_load(void *arg);
59static boot_state_t bs_payload_boot(void *arg);
60
61struct boot_state {
62 const char *name;
63 boot_state_t id;
64 struct boot_state_callback *seq_callbacks[2];
65 boot_state_t (*run_state)(void *arg);
66 void *arg;
67 int complete;
68};
69
70#define BS_INIT(state_, run_func_) \
71 { \
72 .name = #state_, \
73 .id = state_, \
74 .seq_callbacks = { NULL, NULL },\
75 .run_state = run_func_, \
76 .arg = NULL, \
77 .complete = 0 \
78 }
79#define BS_INIT_ENTRY(state_, run_func_) \
80 [state_] = BS_INIT(state_, run_func_)
81
82static struct boot_state boot_states[] = {
83 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
84 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
85 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
86 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
87 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_eanble),
88 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
89 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050090 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050091 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
97static boot_state_t bs_pre_device(void *arg)
98{
Aaron Durbin7e35efa2013-04-24 15:14:01 -050099 return BS_DEV_INIT_CHIPS;
100}
101
102static boot_state_t bs_dev_init_chips(void *arg)
103{
104 timestamp_stash(TS_DEVICE_ENUMERATE);
105
106 /* Initialize chips early, they might disable unused devices. */
107 dev_initialize_chips();
108
109 return BS_DEV_ENUMERATE;
110}
111
112static boot_state_t bs_dev_enumerate(void *arg)
113{
114 /* Find the devices we don't have hard coded knowledge about. */
115 dev_enumerate();
116 post_code(POST_DEVICE_ENUMERATION_COMPLETE);
117
118 return BS_DEV_RESOURCES;
119}
120
121static boot_state_t bs_dev_resources(void *arg)
122{
123 timestamp_stash(TS_DEVICE_CONFIGURE);
124 /* Now compute and assign the bus resources. */
125 dev_configure();
126 post_code(POST_DEVICE_CONFIGURATION_COMPLETE);
127
128 return BS_DEV_ENABLE;
129}
130
131static boot_state_t bs_dev_eanble(void *arg)
132{
133 timestamp_stash(TS_DEVICE_ENABLE);
134 /* Now actually enable devices on the bus */
135 dev_enable();
136 post_code(POST_DEVICES_ENABLED);
137
138 return BS_DEV_INIT;
139}
140
141static boot_state_t bs_dev_init(void *arg)
142{
143 timestamp_stash(TS_DEVICE_INITIALIZE);
144 /* And of course initialize devices on the bus */
145 dev_initialize();
146 post_code(POST_DEVICES_INITIALIZED);
147
148 return BS_POST_DEVICE;
149}
150
151static boot_state_t bs_post_device(void *arg)
152{
153 timestamp_stash(TS_DEVICE_DONE);
154
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500155 timestamp_sync();
156
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500157 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500158}
159
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500160static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500161{
162#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500163 void *wake_vector;
164
165 wake_vector = acpi_find_wakeup_vector();
166
167 if (wake_vector != NULL) {
168 boot_states[BS_OS_RESUME].arg = wake_vector;
169 return BS_OS_RESUME;
170 }
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500171 post_code(0x8a);
172#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500173 timestamp_add_now(TS_CBMEM_POST);
174
175 return BS_WRITE_TABLES;
176}
177
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500178static boot_state_t bs_os_resume(void *wake_vector)
179{
180#if CONFIG_HAVE_ACPI_RESUME
181 acpi_resume(wake_vector);
182#endif
183 return BS_WRITE_TABLES;
184}
185
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500186static boot_state_t bs_write_tables(void *arg)
187{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500188 timestamp_add_now(TS_WRITE_TABLES);
189
190 /* Now that we have collected all of our information
191 * write our configuration tables.
192 */
193 write_tables();
194
195 return BS_PAYLOAD_LOAD;
196}
197
198static boot_state_t bs_payload_load(void *arg)
199{
200 void *payload;
Aaron Durbin001de1a2013-04-24 22:59:45 -0500201 void *entry;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500202
203 timestamp_add_now(TS_LOAD_PAYLOAD);
204
205 payload = cbfs_load_payload(CBFS_DEFAULT_MEDIA,
206 CONFIG_CBFS_PREFIX "/payload");
207 if (! payload)
208 die("Could not find a payload\n");
209
Aaron Durbin001de1a2013-04-24 22:59:45 -0500210 entry = selfload(get_lb_mem(), payload);
211
212 if (! entry)
213 die("Could not load payload\n");
214
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500215 /* Pass the payload to the next state. */
Aaron Durbin001de1a2013-04-24 22:59:45 -0500216 boot_states[BS_PAYLOAD_BOOT].arg = entry;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500217
218 return BS_PAYLOAD_BOOT;
219}
220
Aaron Durbin001de1a2013-04-24 22:59:45 -0500221static boot_state_t bs_payload_boot(void *entry)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500222{
Aaron Durbin001de1a2013-04-24 22:59:45 -0500223 selfboot(entry);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500224
225 printk(BIOS_EMERG, "Boot failed");
226 /* Returning from this state will fail because the following signals
227 * return to a completed state. */
228 return BS_PAYLOAD_BOOT;
229}
230
231static void bs_call_callbacks(struct boot_state *state,
232 boot_state_sequence_t seq)
233{
234 while (state->seq_callbacks[seq] != NULL) {
235 struct boot_state_callback *bscb;
236
237 /* Remove the first callback. */
238 bscb = state->seq_callbacks[seq];
239 state->seq_callbacks[seq] = bscb->next;
240 bscb->next = NULL;
241
242#if BOOT_STATE_DEBUG
243 printk(BS_DEBUG_LVL, "BS: callback (%p) @ %s.\n",
244 bscb, bscb->location);
245#endif
246 bscb->callback(bscb->arg);
247 }
248}
249
250static void bs_walk_state_machine(boot_state_t current_state_id)
251{
252
253 while (1) {
254 struct boot_state *state;
255
256 state = &boot_states[current_state_id];
257
258 if (state->complete) {
259 printk(BIOS_EMERG, "BS: %s state already executed.\n",
260 state->name);
261 break;
262 }
263
264 printk(BS_DEBUG_LVL, "BS: Entering %s state.\n", state->name);
265 bs_call_callbacks(state, BS_ON_ENTRY);
266
267 current_state_id = state->run_state(state->arg);
268
269 printk(BS_DEBUG_LVL, "BS: Exiting %s state.\n", state->name);
270 bs_call_callbacks(state, BS_ON_EXIT);
271
272 state->complete = 1;
273 }
274}
275
276static int boot_state_sched_callback(struct boot_state *state,
277 struct boot_state_callback *bscb,
278 boot_state_sequence_t seq)
279{
280 if (state->complete) {
281 printk(BIOS_WARNING,
282 "Tried to schedule callback on completed state %s.\n",
283 state->name);
284
285 return -1;
286 }
287
288 bscb->next = state->seq_callbacks[seq];
289 state->seq_callbacks[seq] = bscb;
290
291 return 0;
292}
293
294int boot_state_sched_on_entry(struct boot_state_callback *bscb,
295 boot_state_t state_id)
296{
297 struct boot_state *state = &boot_states[state_id];
298
299 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
300}
301
302int boot_state_sched_on_exit(struct boot_state_callback *bscb,
303 boot_state_t state_id)
304{
305 struct boot_state *state = &boot_states[state_id];
306
307 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
308}
Maciej Pijankaea921852009-10-27 14:29:29 +0000309
Aaron Durbina4feddf2013-04-24 16:12:52 -0500310static void boot_state_schedule_static_entries(void)
311{
312 extern struct boot_state_init_entry _bs_init_begin;
313 extern struct boot_state_init_entry _bs_init_end;
314 struct boot_state_init_entry *cur;
315
316 cur = &_bs_init_begin;
317
318 while (cur != &_bs_init_end) {
319 if (cur->when == BS_ON_ENTRY)
320 boot_state_sched_on_entry(&cur->bscb, cur->state);
321 else
322 boot_state_sched_on_exit(&cur->bscb, cur->state);
323 cur++;
324 }
325}
326
Eric Biederman8ca8d762003-04-22 19:02:15 +0000327void hardwaremain(int boot_complete)
328{
Stefan Reinauer4221a192012-10-15 15:23:20 -0700329 timestamp_stash(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000330 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000331
Stefan Reinauer3b314022009-10-26 17:04:28 +0000332 /* console_init() MUST PRECEDE ALL printk()! */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000333 console_init();
Stefan Reinauer14e22772010-04-27 06:56:47 +0000334
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000335 post_code(POST_CONSOLE_READY);
arch import user (historical)54d6b082005-07-06 17:17:41 +0000336
Stefan Reinauer14e22772010-04-27 06:56:47 +0000337 printk(BIOS_NOTICE, "coreboot-%s%s %s %s...\n",
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000338 coreboot_version, coreboot_extra_version, coreboot_build,
Li-Ta Lo3a812852004-12-03 22:39:34 +0000339 (boot_complete)?"rebooting":"booting");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000340
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000341 post_code(POST_CONSOLE_BOOT_MSG);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000342
Eric Biederman8ca8d762003-04-22 19:02:15 +0000343 /* If we have already booted attempt a hard reboot */
344 if (boot_complete) {
345 hard_reset();
346 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000347
Aaron Durbina4feddf2013-04-24 16:12:52 -0500348 /* Schedule the static boot state entries. */
349 boot_state_schedule_static_entries();
350
Eric Biederman7003ba42004-10-16 06:20:29 +0000351 /* FIXME: Is there a better way to handle this? */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000352 init_timer();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000353
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500354 bs_walk_state_machine(BS_PRE_DEVICE);
355 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000356}
357