blob: ab4d9f48e2f461b37a0cb98c7eec328537e95eef [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.
Eric Biederman8ca8d762003-04-22 19:02:15 +000014 */
15
16
17/*
Stefan Reinauerf8ee1802008-01-18 15:08:58 +000018 * C Bootstrap code for the coreboot
Eric Biederman8ca8d762003-04-22 19:02:15 +000019 */
20
Julius Werner85620db2013-11-13 18:22:15 -080021#include <arch/exception.h>
Aaron Durbin7e35efa2013-04-24 15:14:01 -050022#include <bootstate.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000023#include <console/console.h>
Duncan Lauriecb73a842013-06-10 10:41:04 -070024#include <console/post_codes.h>
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -050025#include <cbmem.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000026#include <version.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000027#include <device/device.h>
28#include <device/pci.h>
Eric Biederman9b4336c2003-07-19 04:28:22 +000029#include <delay.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000030#include <stdlib.h>
Stefan Reinauerde3206a2010-02-22 06:09:43 +000031#include <reset.h>
Stefan Reinauer7e9771c2009-04-22 08:17:38 +000032#include <boot/tables.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050033#include <program_loading.h>
Ronald G. Minnich9764d4c2012-06-12 16:29:32 -070034#include <lib.h>
Stefan Reinauer08670622009-06-30 15:17:49 +000035#if CONFIG_HAVE_ACPI_RESUME
Rudolf Mareka572f832009-04-13 17:57:44 +000036#include <arch/acpi.h>
Stefan Reinauer3935b192009-04-14 06:38:15 +000037#endif
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050038#include <timer.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070039#include <timestamp.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050040#include <thread.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000041
Aaron Durbin7e35efa2013-04-24 15:14:01 -050042static boot_state_t bs_pre_device(void *arg);
43static boot_state_t bs_dev_init_chips(void *arg);
44static boot_state_t bs_dev_enumerate(void *arg);
45static boot_state_t bs_dev_resources(void *arg);
David Hendrickscca68592013-06-20 14:08:27 -070046static boot_state_t bs_dev_enable(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050047static boot_state_t bs_dev_init(void *arg);
48static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050049static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050050static boot_state_t bs_os_resume(void *arg);
51static boot_state_t bs_write_tables(void *arg);
52static boot_state_t bs_payload_load(void *arg);
53static boot_state_t bs_payload_boot(void *arg);
54
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050055/*
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
63struct boot_state_times {
64 int num_samples;
65 struct mono_time samples[MAX_TIME_SAMPLES];
66};
67
Aaron Durbin0748d302013-05-06 10:50:19 -050068/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
69 * blocked from transitioning to the next (state,seq) pair. When the blockers
70 * field is 0 a transition may occur. */
71struct boot_phase {
72 struct boot_state_callback *callbacks;
73 int blockers;
74};
75
Aaron Durbin7e35efa2013-04-24 15:14:01 -050076struct boot_state {
77 const char *name;
78 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070079 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050080 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050081 boot_state_t (*run_state)(void *arg);
82 void *arg;
Aaron Durbin8fc41e12013-04-29 23:22:01 -050083 int complete : 1;
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050084#if CONFIG_HAVE_MONOTONIC_TIMER
85 struct boot_state_times times;
86#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -050087};
88
Aaron Durbin15c671e2013-05-06 10:52:24 -050089#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050090 { \
91 .name = #state_, \
92 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070093 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050094 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050095 .run_state = run_func_, \
96 .arg = NULL, \
97 .complete = 0, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050098 }
99#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -0500100 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500101
102static struct boot_state boot_states[] = {
103 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
104 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
105 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
106 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
David Hendrickscca68592013-06-20 14:08:27 -0700107 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500108 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
109 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500110 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -0500111 BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
112 BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
113 BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
114 BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500115};
116
117static boot_state_t bs_pre_device(void *arg)
118{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500119 return BS_DEV_INIT_CHIPS;
120}
121
122static boot_state_t bs_dev_init_chips(void *arg)
123{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300124 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500125
126 /* Initialize chips early, they might disable unused devices. */
127 dev_initialize_chips();
128
129 return BS_DEV_ENUMERATE;
130}
131
132static boot_state_t bs_dev_enumerate(void *arg)
133{
134 /* Find the devices we don't have hard coded knowledge about. */
135 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500136
137 return BS_DEV_RESOURCES;
138}
139
140static boot_state_t bs_dev_resources(void *arg)
141{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300142 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700143
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500144 /* Now compute and assign the bus resources. */
145 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500146
147 return BS_DEV_ENABLE;
148}
149
David Hendrickscca68592013-06-20 14:08:27 -0700150static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500151{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300152 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700153
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500154 /* Now actually enable devices on the bus */
155 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500156
157 return BS_DEV_INIT;
158}
159
160static boot_state_t bs_dev_init(void *arg)
161{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300162 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700163
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500164 /* And of course initialize devices on the bus */
165 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500166
167 return BS_POST_DEVICE;
168}
169
170static boot_state_t bs_post_device(void *arg)
171{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600172 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300173 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500174
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500175 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500176}
177
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500178static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500179{
180#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500181 void *wake_vector;
182
183 wake_vector = acpi_find_wakeup_vector();
184
185 if (wake_vector != NULL) {
186 boot_states[BS_OS_RESUME].arg = wake_vector;
187 return BS_OS_RESUME;
188 }
Kyösti Mälkki7b14f082014-10-16 20:58:47 +0300189
190 acpi_prepare_resume_backup();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500191#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500192 timestamp_add_now(TS_CBMEM_POST);
193
194 return BS_WRITE_TABLES;
195}
196
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500197static boot_state_t bs_os_resume(void *wake_vector)
198{
199#if CONFIG_HAVE_ACPI_RESUME
200 acpi_resume(wake_vector);
201#endif
202 return BS_WRITE_TABLES;
203}
204
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500205static boot_state_t bs_write_tables(void *arg)
206{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500207 timestamp_add_now(TS_WRITE_TABLES);
208
209 /* Now that we have collected all of our information
210 * write our configuration tables.
211 */
212 write_tables();
213
Marc Jones2a58ecd2013-10-29 17:32:00 -0600214 dev_finalize_chips();
215
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500216 return BS_PAYLOAD_LOAD;
217}
218
219static boot_state_t bs_payload_load(void *arg)
220{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500221 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500222
223 return BS_PAYLOAD_BOOT;
224}
225
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600226static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500227{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500228 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500229
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200230 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500231 /* Returning from this state will fail because the following signals
232 * return to a completed state. */
233 return BS_PAYLOAD_BOOT;
234}
235
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500236#if CONFIG_HAVE_MONOTONIC_TIMER
237static void bs_sample_time(struct boot_state *state)
238{
239 struct mono_time *mt;
240
241 mt = &state->times.samples[state->times.num_samples];
242 timer_monotonic_get(mt);
243 state->times.num_samples++;
244}
245
246static void bs_report_time(struct boot_state *state)
247{
Aaron Durbinad5a9092014-09-24 09:48:47 -0500248 long entry_time;
249 long run_time;
250 long exit_time;
251 struct mono_time *samples = &state->times.samples[0];
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500252
Aaron Durbinad5a9092014-09-24 09:48:47 -0500253 entry_time = mono_time_diff_microseconds(&samples[0], &samples[1]);
254 run_time = mono_time_diff_microseconds(&samples[1], &samples[2]);
255 exit_time = mono_time_diff_microseconds(&samples[2], &samples[3]);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500256
257 printk(BIOS_DEBUG, "BS: %s times (us): entry %ld run %ld exit %ld\n",
Aaron Durbinad5a9092014-09-24 09:48:47 -0500258 state->name, entry_time, run_time, exit_time);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500259}
260#else
261static inline void bs_sample_time(struct boot_state *state) {}
262static inline void bs_report_time(struct boot_state *state) {}
263#endif
264
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500265#if CONFIG_TIMER_QUEUE
266static void bs_run_timers(int drain)
267{
268 /* Drain all timer callbacks until none are left, if directed.
269 * Otherwise run the timers only once. */
270 do {
271 if (!timers_run())
272 break;
273 } while (drain);
274}
275#else
276static void bs_run_timers(int drain) {}
277#endif
278
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500279static void bs_call_callbacks(struct boot_state *state,
280 boot_state_sequence_t seq)
281{
Aaron Durbin0748d302013-05-06 10:50:19 -0500282 struct boot_phase *phase = &state->phases[seq];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500283
Aaron Durbin0748d302013-05-06 10:50:19 -0500284 while (1) {
285 if (phase->callbacks != NULL) {
286 struct boot_state_callback *bscb;
287
288 /* Remove the first callback. */
289 bscb = phase->callbacks;
290 phase->callbacks = bscb->next;
291 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500292
Lee Leahy10605352016-02-14 17:01:40 -0800293#if IS_ENABLED(CONFIG_DEBUG_BOOT_STATE)
294 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
295 bscb, bscb->location);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500296#endif
Aaron Durbin0748d302013-05-06 10:50:19 -0500297 bscb->callback(bscb->arg);
Aaron Durbin0748d302013-05-06 10:50:19 -0500298 continue;
299 }
300
301 /* All callbacks are complete and there are no blockers for
302 * this state. Therefore, this part of the state is complete. */
303 if (!phase->blockers)
304 break;
305
306 /* Something is blocking this state from transitioning. As
307 * there are no more callbacks a pending timer needs to be
308 * ran to unblock the state. */
309 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500310 }
311}
312
Aaron Durbin0748d302013-05-06 10:50:19 -0500313/* Keep track of the current state. */
314static struct state_tracker {
315 boot_state_t state_id;
316 boot_state_sequence_t seq;
317} current_phase = {
318 .state_id = BS_PRE_DEVICE,
319 .seq = BS_ON_ENTRY,
320};
321
322static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500323{
324
325 while (1) {
326 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500327 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500328
Aaron Durbin0748d302013-05-06 10:50:19 -0500329 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500330
331 if (state->complete) {
332 printk(BIOS_EMERG, "BS: %s state already executed.\n",
333 state->name);
334 break;
335 }
336
Lee Leahy10605352016-02-14 17:01:40 -0800337 if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
338 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
339 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500340
Aaron Durbin15c671e2013-05-06 10:52:24 -0500341 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500342
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500343 bs_sample_time(state);
344
Aaron Durbin0748d302013-05-06 10:50:19 -0500345 bs_call_callbacks(state, current_phase.seq);
346 /* Update the current sequence so that any calls to block the
347 * current state from the run_state() function will place a
348 * block on the correct phase. */
349 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500350
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500351 bs_sample_time(state);
352
Duncan Lauriecb73a842013-06-10 10:41:04 -0700353 post_code(state->post_code);
354
Aaron Durbin0748d302013-05-06 10:50:19 -0500355 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500356
Lee Leahy10605352016-02-14 17:01:40 -0800357 if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
358 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
359 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500360
361 bs_sample_time(state);
362
Aaron Durbin0748d302013-05-06 10:50:19 -0500363 bs_call_callbacks(state, current_phase.seq);
364
Lee Leahy10605352016-02-14 17:01:40 -0800365 if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
366 printk(BIOS_DEBUG,
367 "----------------------------------------\n");
368
Aaron Durbin0748d302013-05-06 10:50:19 -0500369 /* Update the current phase with new state id and sequence. */
370 current_phase.state_id = next_id;
371 current_phase.seq = BS_ON_ENTRY;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500372
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500373 bs_sample_time(state);
374
375 bs_report_time(state);
376
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500377 state->complete = 1;
378 }
379}
380
381static int boot_state_sched_callback(struct boot_state *state,
382 struct boot_state_callback *bscb,
383 boot_state_sequence_t seq)
384{
385 if (state->complete) {
386 printk(BIOS_WARNING,
387 "Tried to schedule callback on completed state %s.\n",
388 state->name);
389
390 return -1;
391 }
392
Aaron Durbin0748d302013-05-06 10:50:19 -0500393 bscb->next = state->phases[seq].callbacks;
394 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500395
396 return 0;
397}
398
399int boot_state_sched_on_entry(struct boot_state_callback *bscb,
400 boot_state_t state_id)
401{
402 struct boot_state *state = &boot_states[state_id];
403
404 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
405}
406
407int boot_state_sched_on_exit(struct boot_state_callback *bscb,
408 boot_state_t state_id)
409{
410 struct boot_state *state = &boot_states[state_id];
411
412 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
413}
Maciej Pijankaea921852009-10-27 14:29:29 +0000414
Aaron Durbina4feddf2013-04-24 16:12:52 -0500415static void boot_state_schedule_static_entries(void)
416{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500417 extern struct boot_state_init_entry *_bs_init_begin[];
418 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500419
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500420 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
421 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500422
Aaron Durbina4feddf2013-04-24 16:12:52 -0500423 if (cur->when == BS_ON_ENTRY)
424 boot_state_sched_on_entry(&cur->bscb, cur->state);
425 else
426 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500427 }
428}
429
Stefan Reinauer6adef082013-05-09 16:30:06 -0700430void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000431{
Duncan Lauriea5181512015-06-04 08:42:48 -0700432 /* TODO: Understand why this is here and move to arch/platform code. */
433 /* For MMIO UART this needs to be called before any other printk. */
434 if (IS_ENABLED(CONFIG_ARCH_X86))
435 init_timer();
436
Aaron Durbinbe347972015-04-21 11:57:18 -0500437 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
438 * it is the very first thing done in ramstage.*/
439 console_init();
440
441 post_code(POST_CONSOLE_READY);
442
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500443 /*
444 * CBMEM needs to be recovered in the EARLY_CBMEM_INIT case because
445 * timestamps, APCI, etc rely on the cbmem infrastructure being
446 * around. Explicitly recover it.
447 */
448 if (IS_ENABLED(CONFIG_EARLY_CBMEM_INIT))
449 cbmem_initialize();
450
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300451 /* Record current time, try to locate timestamps in CBMEM. */
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700452 timestamp_init(timestamp_get());
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300453
454 timestamp_add_now(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000455 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000456
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300457 /* Handoff sleep type from romstage. */
458#if CONFIG_HAVE_ACPI_RESUME
459 acpi_is_wakeup();
460#endif
461
Julius Werner85620db2013-11-13 18:22:15 -0800462 exception_init();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500463 threads_initialize();
464
Aaron Durbina4feddf2013-04-24 16:12:52 -0500465 /* Schedule the static boot state entries. */
466 boot_state_schedule_static_entries();
467
Aaron Durbin0748d302013-05-06 10:50:19 -0500468 bs_walk_state_machine();
469
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500470 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000471}
472
Aaron Durbin0748d302013-05-06 10:50:19 -0500473
474int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
475{
476 struct boot_phase *bp;
477
478 /* Blocking a previously ran state is not appropriate. */
479 if (current_phase.state_id > state ||
480 (current_phase.state_id == state && current_phase.seq > seq) ) {
481 printk(BIOS_WARNING,
482 "BS: Completed state (%d, %d) block attempted.\n",
483 state, seq);
484 return -1;
485 }
486
487 bp = &boot_states[state].phases[seq];
488 bp->blockers++;
489
490 return 0;
491}
492
493int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
494{
495 struct boot_phase *bp;
496
497 /* Blocking a previously ran state is not appropriate. */
498 if (current_phase.state_id > state ||
499 (current_phase.state_id == state && current_phase.seq > seq) ) {
500 printk(BIOS_WARNING,
501 "BS: Completed state (%d, %d) unblock attempted.\n",
502 state, seq);
503 return -1;
504 }
505
506 bp = &boot_states[state].phases[seq];
507
508 if (bp->blockers == 0) {
509 printk(BIOS_WARNING,
510 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
511 state, seq);
512 return -1;
513 }
514
515 bp->blockers--;
516
517 return 0;
518}
519
520void boot_state_current_block(void)
521{
522 boot_state_block(current_phase.state_id, current_phase.seq);
523}
524
525void boot_state_current_unblock(void)
526{
527 boot_state_unblock(current_phase.state_id, current_phase.seq);
528}