blob: ef789601e6d549f54b2b56bf5262ecd5cdd74048 [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
Nico Hubere0ed9022016-10-07 12:58:17 +020021#include <adainit.h>
Julius Werner85620db2013-11-13 18:22:15 -080022#include <arch/exception.h>
Aaron Durbin7e35efa2013-04-24 15:14:01 -050023#include <bootstate.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000024#include <console/console.h>
Duncan Lauriecb73a842013-06-10 10:41:04 -070025#include <console/post_codes.h>
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -050026#include <cbmem.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>
Aaron Durbin04654a22015-03-17 11:43:44 -050034#include <program_loading.h>
Ronald G. Minnich9764d4c2012-06-12 16:29:32 -070035#include <lib.h>
Stefan Reinauer08670622009-06-30 15:17:49 +000036#if CONFIG_HAVE_ACPI_RESUME
Rudolf Mareka572f832009-04-13 17:57:44 +000037#include <arch/acpi.h>
Stefan Reinauer3935b192009-04-14 06:38:15 +000038#endif
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050039#include <timer.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070040#include <timestamp.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050041#include <thread.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000042
Aaron Durbin7e35efa2013-04-24 15:14:01 -050043static boot_state_t bs_pre_device(void *arg);
44static boot_state_t bs_dev_init_chips(void *arg);
45static boot_state_t bs_dev_enumerate(void *arg);
46static boot_state_t bs_dev_resources(void *arg);
David Hendrickscca68592013-06-20 14:08:27 -070047static boot_state_t bs_dev_enable(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050048static boot_state_t bs_dev_init(void *arg);
49static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050050static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050051static boot_state_t bs_os_resume(void *arg);
52static boot_state_t bs_write_tables(void *arg);
53static boot_state_t bs_payload_load(void *arg);
54static boot_state_t bs_payload_boot(void *arg);
55
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050056/*
57 * Typically a state will take 4 time samples:
58 * 1. Before state entry callbacks
59 * 2. After state entry callbacks / Before state function.
60 * 3. After state function / Before state exit callbacks.
61 * 4. After state exit callbacks.
62 */
63#define MAX_TIME_SAMPLES 4
64struct boot_state_times {
65 int num_samples;
66 struct mono_time samples[MAX_TIME_SAMPLES];
67};
68
Aaron Durbin0748d302013-05-06 10:50:19 -050069/* 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. */
72struct boot_phase {
73 struct boot_state_callback *callbacks;
74 int blockers;
75};
76
Aaron Durbin7e35efa2013-04-24 15:14:01 -050077struct boot_state {
78 const char *name;
79 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070080 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050081 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050082 boot_state_t (*run_state)(void *arg);
83 void *arg;
Aaron Durbin8fc41e12013-04-29 23:22:01 -050084 int complete : 1;
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050085#if CONFIG_HAVE_MONOTONIC_TIMER
86 struct boot_state_times times;
87#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -050088};
89
Aaron Durbin15c671e2013-05-06 10:52:24 -050090#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050091 { \
92 .name = #state_, \
93 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070094 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050095 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050096 .run_state = run_func_, \
97 .arg = NULL, \
98 .complete = 0, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050099 }
100#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -0500101 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500102
103static 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 Hendrickscca68592013-06-20 14:08:27 -0700108 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500109 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
110 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500111 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -0500112 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 Durbin7e35efa2013-04-24 15:14:01 -0500116};
117
118static boot_state_t bs_pre_device(void *arg)
119{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500120 return BS_DEV_INIT_CHIPS;
121}
122
123static boot_state_t bs_dev_init_chips(void *arg)
124{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300125 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500126
127 /* Initialize chips early, they might disable unused devices. */
128 dev_initialize_chips();
129
130 return BS_DEV_ENUMERATE;
131}
132
133static boot_state_t bs_dev_enumerate(void *arg)
134{
135 /* Find the devices we don't have hard coded knowledge about. */
136 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500137
138 return BS_DEV_RESOURCES;
139}
140
141static boot_state_t bs_dev_resources(void *arg)
142{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300143 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700144
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500145 /* Now compute and assign the bus resources. */
146 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500147
148 return BS_DEV_ENABLE;
149}
150
David Hendrickscca68592013-06-20 14:08:27 -0700151static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500152{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300153 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700154
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500155 /* Now actually enable devices on the bus */
156 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500157
158 return BS_DEV_INIT;
159}
160
161static boot_state_t bs_dev_init(void *arg)
162{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300163 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700164
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500165 /* And of course initialize devices on the bus */
166 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500167
168 return BS_POST_DEVICE;
169}
170
171static boot_state_t bs_post_device(void *arg)
172{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600173 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300174 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500175
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500176 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500177}
178
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500179static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500180{
181#if CONFIG_HAVE_ACPI_RESUME
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500182 void *wake_vector;
183
184 wake_vector = acpi_find_wakeup_vector();
185
186 if (wake_vector != NULL) {
187 boot_states[BS_OS_RESUME].arg = wake_vector;
188 return BS_OS_RESUME;
189 }
Kyösti Mälkki7b14f082014-10-16 20:58:47 +0300190
191 acpi_prepare_resume_backup();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500192#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500193 timestamp_add_now(TS_CBMEM_POST);
194
195 return BS_WRITE_TABLES;
196}
197
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500198static boot_state_t bs_os_resume(void *wake_vector)
199{
200#if CONFIG_HAVE_ACPI_RESUME
201 acpi_resume(wake_vector);
202#endif
203 return BS_WRITE_TABLES;
204}
205
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500206static boot_state_t bs_write_tables(void *arg)
207{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500208 timestamp_add_now(TS_WRITE_TABLES);
209
210 /* Now that we have collected all of our information
211 * write our configuration tables.
212 */
213 write_tables();
214
Marc Jones2a58ecd2013-10-29 17:32:00 -0600215 dev_finalize_chips();
216
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500217 return BS_PAYLOAD_LOAD;
218}
219
220static boot_state_t bs_payload_load(void *arg)
221{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500222 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500223
224 return BS_PAYLOAD_BOOT;
225}
226
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600227static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500228{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500229 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500230
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200231 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500232 /* Returning from this state will fail because the following signals
233 * return to a completed state. */
234 return BS_PAYLOAD_BOOT;
235}
236
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500237#if CONFIG_HAVE_MONOTONIC_TIMER
238static void bs_sample_time(struct boot_state *state)
239{
240 struct mono_time *mt;
241
242 mt = &state->times.samples[state->times.num_samples];
243 timer_monotonic_get(mt);
244 state->times.num_samples++;
245}
246
247static void bs_report_time(struct boot_state *state)
248{
Aaron Durbinad5a9092014-09-24 09:48:47 -0500249 long entry_time;
250 long run_time;
251 long exit_time;
252 struct mono_time *samples = &state->times.samples[0];
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500253
Aaron Durbinad5a9092014-09-24 09:48:47 -0500254 entry_time = mono_time_diff_microseconds(&samples[0], &samples[1]);
255 run_time = mono_time_diff_microseconds(&samples[1], &samples[2]);
256 exit_time = mono_time_diff_microseconds(&samples[2], &samples[3]);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500257
258 printk(BIOS_DEBUG, "BS: %s times (us): entry %ld run %ld exit %ld\n",
Aaron Durbinad5a9092014-09-24 09:48:47 -0500259 state->name, entry_time, run_time, exit_time);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500260}
261#else
262static inline void bs_sample_time(struct boot_state *state) {}
263static inline void bs_report_time(struct boot_state *state) {}
264#endif
265
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500266#if CONFIG_TIMER_QUEUE
267static void bs_run_timers(int drain)
268{
269 /* Drain all timer callbacks until none are left, if directed.
270 * Otherwise run the timers only once. */
271 do {
272 if (!timers_run())
273 break;
274 } while (drain);
275}
276#else
277static void bs_run_timers(int drain) {}
278#endif
279
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500280static void bs_call_callbacks(struct boot_state *state,
281 boot_state_sequence_t seq)
282{
Aaron Durbin0748d302013-05-06 10:50:19 -0500283 struct boot_phase *phase = &state->phases[seq];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500284
Aaron Durbin0748d302013-05-06 10:50:19 -0500285 while (1) {
286 if (phase->callbacks != NULL) {
287 struct boot_state_callback *bscb;
288
289 /* Remove the first callback. */
290 bscb = phase->callbacks;
291 phase->callbacks = bscb->next;
292 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500293
Lee Leahy10605352016-02-14 17:01:40 -0800294#if IS_ENABLED(CONFIG_DEBUG_BOOT_STATE)
295 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
296 bscb, bscb->location);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500297#endif
Aaron Durbin0748d302013-05-06 10:50:19 -0500298 bscb->callback(bscb->arg);
Aaron Durbin0748d302013-05-06 10:50:19 -0500299 continue;
300 }
301
302 /* All callbacks are complete and there are no blockers for
303 * this state. Therefore, this part of the state is complete. */
304 if (!phase->blockers)
305 break;
306
307 /* Something is blocking this state from transitioning. As
308 * there are no more callbacks a pending timer needs to be
309 * ran to unblock the state. */
310 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500311 }
312}
313
Aaron Durbin0748d302013-05-06 10:50:19 -0500314/* Keep track of the current state. */
315static struct state_tracker {
316 boot_state_t state_id;
317 boot_state_sequence_t seq;
318} current_phase = {
319 .state_id = BS_PRE_DEVICE,
320 .seq = BS_ON_ENTRY,
321};
322
323static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500324{
325
326 while (1) {
327 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500328 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500329
Aaron Durbin0748d302013-05-06 10:50:19 -0500330 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500331
332 if (state->complete) {
333 printk(BIOS_EMERG, "BS: %s state already executed.\n",
334 state->name);
335 break;
336 }
337
Lee Leahy10605352016-02-14 17:01:40 -0800338 if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
339 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
340 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500341
Aaron Durbin15c671e2013-05-06 10:52:24 -0500342 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500343
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500344 bs_sample_time(state);
345
Aaron Durbin0748d302013-05-06 10:50:19 -0500346 bs_call_callbacks(state, current_phase.seq);
347 /* Update the current sequence so that any calls to block the
348 * current state from the run_state() function will place a
349 * block on the correct phase. */
350 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500351
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500352 bs_sample_time(state);
353
Duncan Lauriecb73a842013-06-10 10:41:04 -0700354 post_code(state->post_code);
355
Aaron Durbin0748d302013-05-06 10:50:19 -0500356 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500357
Lee Leahy10605352016-02-14 17:01:40 -0800358 if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
359 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
360 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500361
362 bs_sample_time(state);
363
Aaron Durbin0748d302013-05-06 10:50:19 -0500364 bs_call_callbacks(state, current_phase.seq);
365
Lee Leahy10605352016-02-14 17:01:40 -0800366 if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
367 printk(BIOS_DEBUG,
368 "----------------------------------------\n");
369
Aaron Durbin0748d302013-05-06 10:50:19 -0500370 /* Update the current phase with new state id and sequence. */
371 current_phase.state_id = next_id;
372 current_phase.seq = BS_ON_ENTRY;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500373
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500374 bs_sample_time(state);
375
376 bs_report_time(state);
377
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500378 state->complete = 1;
379 }
380}
381
382static int boot_state_sched_callback(struct boot_state *state,
383 struct boot_state_callback *bscb,
384 boot_state_sequence_t seq)
385{
386 if (state->complete) {
387 printk(BIOS_WARNING,
388 "Tried to schedule callback on completed state %s.\n",
389 state->name);
390
391 return -1;
392 }
393
Aaron Durbin0748d302013-05-06 10:50:19 -0500394 bscb->next = state->phases[seq].callbacks;
395 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500396
397 return 0;
398}
399
400int boot_state_sched_on_entry(struct boot_state_callback *bscb,
401 boot_state_t state_id)
402{
403 struct boot_state *state = &boot_states[state_id];
404
405 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
406}
407
408int boot_state_sched_on_exit(struct boot_state_callback *bscb,
409 boot_state_t state_id)
410{
411 struct boot_state *state = &boot_states[state_id];
412
413 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
414}
Maciej Pijankaea921852009-10-27 14:29:29 +0000415
Aaron Durbina4feddf2013-04-24 16:12:52 -0500416static void boot_state_schedule_static_entries(void)
417{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500418 extern struct boot_state_init_entry *_bs_init_begin[];
419 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500420
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500421 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
422 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500423
Aaron Durbina4feddf2013-04-24 16:12:52 -0500424 if (cur->when == BS_ON_ENTRY)
425 boot_state_sched_on_entry(&cur->bscb, cur->state);
426 else
427 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500428 }
429}
430
Stefan Reinauer6adef082013-05-09 16:30:06 -0700431void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432{
Nico Hubere0ed9022016-10-07 12:58:17 +0200433 /*
434 * We can generally jump between C and Ada code back and forth
435 * without trouble. But since we don't have an Ada main() we
436 * have to do some Ada package initializations that GNAT would
437 * do there. This has to be done before calling any Ada code.
438 *
439 * The package initializations should not have any dependen-
440 * cies on C code. So we can call them here early, and don't
441 * have to worry at which point we can start to use Ada.
442 */
443 ramstage_adainit();
444
Duncan Lauriea5181512015-06-04 08:42:48 -0700445 /* TODO: Understand why this is here and move to arch/platform code. */
446 /* For MMIO UART this needs to be called before any other printk. */
447 if (IS_ENABLED(CONFIG_ARCH_X86))
448 init_timer();
449
Aaron Durbinbe347972015-04-21 11:57:18 -0500450 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
451 * it is the very first thing done in ramstage.*/
452 console_init();
453
454 post_code(POST_CONSOLE_READY);
455
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500456 /*
457 * CBMEM needs to be recovered in the EARLY_CBMEM_INIT case because
458 * timestamps, APCI, etc rely on the cbmem infrastructure being
459 * around. Explicitly recover it.
460 */
461 if (IS_ENABLED(CONFIG_EARLY_CBMEM_INIT))
462 cbmem_initialize();
463
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300464 /* Record current time, try to locate timestamps in CBMEM. */
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700465 timestamp_init(timestamp_get());
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300466
467 timestamp_add_now(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000468 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000469
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300470 /* Handoff sleep type from romstage. */
471#if CONFIG_HAVE_ACPI_RESUME
472 acpi_is_wakeup();
473#endif
474
Julius Werner85620db2013-11-13 18:22:15 -0800475 exception_init();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500476 threads_initialize();
477
Aaron Durbina4feddf2013-04-24 16:12:52 -0500478 /* Schedule the static boot state entries. */
479 boot_state_schedule_static_entries();
480
Aaron Durbin0748d302013-05-06 10:50:19 -0500481 bs_walk_state_machine();
482
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500483 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000484}
485
Aaron Durbin0748d302013-05-06 10:50:19 -0500486
487int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
488{
489 struct boot_phase *bp;
490
491 /* Blocking a previously ran state is not appropriate. */
492 if (current_phase.state_id > state ||
493 (current_phase.state_id == state && current_phase.seq > seq) ) {
494 printk(BIOS_WARNING,
495 "BS: Completed state (%d, %d) block attempted.\n",
496 state, seq);
497 return -1;
498 }
499
500 bp = &boot_states[state].phases[seq];
501 bp->blockers++;
502
503 return 0;
504}
505
506int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
507{
508 struct boot_phase *bp;
509
510 /* Blocking a previously ran state is not appropriate. */
511 if (current_phase.state_id > state ||
512 (current_phase.state_id == state && current_phase.seq > seq) ) {
513 printk(BIOS_WARNING,
514 "BS: Completed state (%d, %d) unblock attempted.\n",
515 state, seq);
516 return -1;
517 }
518
519 bp = &boot_states[state].phases[seq];
520
521 if (bp->blockers == 0) {
522 printk(BIOS_WARNING,
523 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
524 state, seq);
525 return -1;
526 }
527
528 bp->blockers--;
529
530 return 0;
531}
532
533void boot_state_current_block(void)
534{
535 boot_state_block(current_phase.state_id, current_phase.seq);
536}
537
538void boot_state_current_unblock(void)
539{
540 boot_state_unblock(current_phase.state_id, current_phase.seq);
541}