blob: dba47a7348f8447e05f27be8e3de91fbf19bfaa8 [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 Reinauer3b314022009-10-26 17:04:28 +000040#include <cbmem.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080041#include <coverage.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070042#include <timestamp.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000043
Aaron Durbin7e35efa2013-04-24 15:14:01 -050044#if BOOT_STATE_DEBUG
45#define BS_DEBUG_LVL BIOS_DEBUG
46#else
47#define BS_DEBUG_LVL BIOS_NEVER
48#endif
Rudolf Mareka572f832009-04-13 17:57:44 +000049
Aaron Durbin7e35efa2013-04-24 15:14:01 -050050static boot_state_t bs_pre_device(void *arg);
51static boot_state_t bs_dev_init_chips(void *arg);
52static boot_state_t bs_dev_enumerate(void *arg);
53static boot_state_t bs_dev_resources(void *arg);
54static boot_state_t bs_dev_eanble(void *arg);
55static boot_state_t bs_dev_init(void *arg);
56static boot_state_t bs_post_device(void *arg);
57static boot_state_t bs_os_resume(void *arg);
58static boot_state_t bs_write_tables(void *arg);
59static boot_state_t bs_payload_load(void *arg);
60static boot_state_t bs_payload_boot(void *arg);
61
62struct 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
83static 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
97static boot_state_t bs_pre_device(void *arg)
98{
99 init_cbmem_pre_device();
100 return BS_DEV_INIT_CHIPS;
101}
102
103static 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
113static 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
122static 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
132static 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
142static 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
152static 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
163static 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
175static 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
190static 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
207static 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
217static 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
236static 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
262static 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
280int 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
288int 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 Pijankaea921852009-10-27 14:29:29 +0000295
Eric Biederman8ca8d762003-04-22 19:02:15 +0000296void hardwaremain(int boot_complete)
297{
Stefan Reinauer4221a192012-10-15 15:23:20 -0700298 timestamp_stash(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000299 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000300
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800301#if CONFIG_COVERAGE
302 coverage_init();
303#endif
304
Stefan Reinauer3b314022009-10-26 17:04:28 +0000305 /* console_init() MUST PRECEDE ALL printk()! */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000306 console_init();
Stefan Reinauer14e22772010-04-27 06:56:47 +0000307
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000308 post_code(POST_CONSOLE_READY);
arch import user (historical)54d6b082005-07-06 17:17:41 +0000309
Stefan Reinauer14e22772010-04-27 06:56:47 +0000310 printk(BIOS_NOTICE, "coreboot-%s%s %s %s...\n",
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000311 coreboot_version, coreboot_extra_version, coreboot_build,
Li-Ta Lo3a812852004-12-03 22:39:34 +0000312 (boot_complete)?"rebooting":"booting");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000313
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000314 post_code(POST_CONSOLE_BOOT_MSG);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000315
Eric Biederman8ca8d762003-04-22 19:02:15 +0000316 /* If we have already booted attempt a hard reboot */
317 if (boot_complete) {
318 hard_reset();
319 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000320
Eric Biederman7003ba42004-10-16 06:20:29 +0000321 /* FIXME: Is there a better way to handle this? */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000322 init_timer();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000323
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500324 bs_walk_state_machine(BS_PRE_DEVICE);
325 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000326}
327