blob: 4276027a9fbea5150703cd15af446b777765bd8f [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Eric Biederman8ca8d762003-04-22 19:02:15 +00002
3
4/*
Stefan Reinauerf8ee1802008-01-18 15:08:58 +00005 * C Bootstrap code for the coreboot
Eric Biederman8ca8d762003-04-22 19:02:15 +00006 */
7
Nico Hubere0ed9022016-10-07 12:58:17 +02008#include <adainit.h>
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +03009#include <acpi/acpi.h>
Julius Werner85620db2013-11-13 18:22:15 -080010#include <arch/exception.h>
Aaron Durbin7e35efa2013-04-24 15:14:01 -050011#include <bootstate.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000012#include <console/console.h>
Duncan Lauriecb73a842013-06-10 10:41:04 -070013#include <console/post_codes.h>
Kyösti Mälkki463ad512019-11-01 09:12:34 +020014#include <commonlib/helpers.h>
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -050015#include <cbmem.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000016#include <version.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000017#include <device/device.h>
18#include <device/pci.h>
Eric Biederman9b4336c2003-07-19 04:28:22 +000019#include <delay.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000020#include <stdlib.h>
Stefan Reinauer7e9771c2009-04-22 08:17:38 +000021#include <boot/tables.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050022#include <program_loading.h>
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050023#include <timer.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070024#include <timestamp.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050025#include <thread.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000026
Aaron Durbin7e35efa2013-04-24 15:14:01 -050027static boot_state_t bs_pre_device(void *arg);
28static boot_state_t bs_dev_init_chips(void *arg);
29static boot_state_t bs_dev_enumerate(void *arg);
30static boot_state_t bs_dev_resources(void *arg);
David Hendrickscca68592013-06-20 14:08:27 -070031static boot_state_t bs_dev_enable(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050032static boot_state_t bs_dev_init(void *arg);
33static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050034static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050035static boot_state_t bs_os_resume(void *arg);
36static boot_state_t bs_write_tables(void *arg);
37static boot_state_t bs_payload_load(void *arg);
38static boot_state_t bs_payload_boot(void *arg);
39
Aaron Durbin0748d302013-05-06 10:50:19 -050040/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
41 * blocked from transitioning to the next (state,seq) pair. When the blockers
42 * field is 0 a transition may occur. */
43struct boot_phase {
44 struct boot_state_callback *callbacks;
45 int blockers;
46};
47
Aaron Durbin7e35efa2013-04-24 15:14:01 -050048struct boot_state {
49 const char *name;
50 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070051 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050052 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050053 boot_state_t (*run_state)(void *arg);
54 void *arg;
Kyösti Mälkki94694a82019-11-02 18:14:31 +020055 int num_samples;
Aaron Durbin8fc41e12013-04-29 23:22:01 -050056 int complete : 1;
Aaron Durbin7e35efa2013-04-24 15:14:01 -050057};
58
Aaron Durbin15c671e2013-05-06 10:52:24 -050059#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050060 { \
61 .name = #state_, \
62 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070063 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050064 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050065 .run_state = run_func_, \
66 .arg = NULL, \
67 .complete = 0, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050068 }
69#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -050070 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -050071
72static struct boot_state boot_states[] = {
73 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
74 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
75 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
76 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
David Hendrickscca68592013-06-20 14:08:27 -070077 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050078 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
79 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050080 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -050081 BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
82 BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
83 BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
84 BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050085};
86
Aaron Durbin64031672018-04-21 14:45:32 -060087void __weak arch_bootstate_coreboot_exit(void) { }
Aaron Durbin16bd2672016-12-07 11:58:20 -060088
Aaron Durbin7e35efa2013-04-24 15:14:01 -050089static boot_state_t bs_pre_device(void *arg)
90{
Aaron Durbin7e35efa2013-04-24 15:14:01 -050091 return BS_DEV_INIT_CHIPS;
92}
93
94static boot_state_t bs_dev_init_chips(void *arg)
95{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030096 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050097
98 /* Initialize chips early, they might disable unused devices. */
99 dev_initialize_chips();
100
101 return BS_DEV_ENUMERATE;
102}
103
104static boot_state_t bs_dev_enumerate(void *arg)
105{
106 /* Find the devices we don't have hard coded knowledge about. */
107 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500108
109 return BS_DEV_RESOURCES;
110}
111
112static boot_state_t bs_dev_resources(void *arg)
113{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300114 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700115
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500116 /* Now compute and assign the bus resources. */
117 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500118
119 return BS_DEV_ENABLE;
120}
121
David Hendrickscca68592013-06-20 14:08:27 -0700122static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500123{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300124 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700125
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500126 /* Now actually enable devices on the bus */
127 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500128
129 return BS_DEV_INIT;
130}
131
132static boot_state_t bs_dev_init(void *arg)
133{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300134 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700135
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500136 /* And of course initialize devices on the bus */
137 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500138
139 return BS_POST_DEVICE;
140}
141
142static boot_state_t bs_post_device(void *arg)
143{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600144 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300145 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500146
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500147 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500148}
149
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500150static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500151{
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300152 void *wake_vector = NULL;
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500153
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300154 if (CONFIG(HAVE_ACPI_RESUME))
155 wake_vector = acpi_find_wakeup_vector();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500156
157 if (wake_vector != NULL) {
158 boot_states[BS_OS_RESUME].arg = wake_vector;
159 return BS_OS_RESUME;
160 }
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300161
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500162 timestamp_add_now(TS_CBMEM_POST);
163
164 return BS_WRITE_TABLES;
165}
166
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500167static boot_state_t bs_os_resume(void *wake_vector)
168{
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300169 if (CONFIG(HAVE_ACPI_RESUME)) {
170 arch_bootstate_coreboot_exit();
171 acpi_resume(wake_vector);
172 }
173
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500174 return BS_WRITE_TABLES;
175}
176
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500177static boot_state_t bs_write_tables(void *arg)
178{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500179 timestamp_add_now(TS_WRITE_TABLES);
180
181 /* Now that we have collected all of our information
182 * write our configuration tables.
183 */
184 write_tables();
185
Paul Menzel00563bf2016-12-05 23:06:37 +0100186 timestamp_add_now(TS_FINALIZE_CHIPS);
Marc Jones2a58ecd2013-10-29 17:32:00 -0600187 dev_finalize_chips();
188
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500189 return BS_PAYLOAD_LOAD;
190}
191
192static boot_state_t bs_payload_load(void *arg)
193{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500194 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500195
196 return BS_PAYLOAD_BOOT;
197}
198
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600199static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500200{
Aaron Durbin16bd2672016-12-07 11:58:20 -0600201 arch_bootstate_coreboot_exit();
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500202 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500203
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200204 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500205 /* Returning from this state will fail because the following signals
206 * return to a completed state. */
207 return BS_PAYLOAD_BOOT;
208}
209
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200210/*
211 * Typically a state will take 4 time samples:
212 * 1. Before state entry callbacks
213 * 2. After state entry callbacks / Before state function.
214 * 3. After state function / Before state exit callbacks.
215 * 4. After state exit callbacks.
216 */
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500217static void bs_sample_time(struct boot_state *state)
218{
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200219 static const char *const sample_id[] = { "entry", "run", "exit" };
220 static struct mono_time previous_sample;
221 struct mono_time this_sample;
222 long console;
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500223
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200224 if (!CONFIG(HAVE_MONOTONIC_TIMER))
225 return;
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200226
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200227 console = console_time_get_and_reset();
228 timer_monotonic_get(&this_sample);
229 state->num_samples++;
230
231 int i = state->num_samples - 2;
232 if ((i >= 0) && (i < ARRAY_SIZE(sample_id))) {
233 long execution = mono_time_diff_microseconds(&previous_sample, &this_sample);
234
235 /* Report with millisecond precision to reduce log diffs. */
236 execution = DIV_ROUND_CLOSEST(execution, USECS_PER_MSEC);
237 console = DIV_ROUND_CLOSEST(console, USECS_PER_MSEC);
238 if (execution) {
239 printk(BIOS_DEBUG, "BS: %s %s times (exec / console): %ld / %ld ms\n",
240 state->name, sample_id[i], execution - console, console);
241 /* Reset again to ignore printk() time above. */
242 console_time_get_and_reset();
243 }
244 }
245 timer_monotonic_get(&previous_sample);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500246}
247
Julius Wernercd49cce2019-03-05 16:53:33 -0800248#if CONFIG(TIMER_QUEUE)
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500249static void bs_run_timers(int drain)
250{
251 /* Drain all timer callbacks until none are left, if directed.
252 * Otherwise run the timers only once. */
253 do {
254 if (!timers_run())
255 break;
256 } while (drain);
257}
258#else
259static void bs_run_timers(int drain) {}
260#endif
261
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500262static void bs_call_callbacks(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800263 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500264{
Aaron Durbin0748d302013-05-06 10:50:19 -0500265 struct boot_phase *phase = &state->phases[seq];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500266
Aaron Durbin0748d302013-05-06 10:50:19 -0500267 while (1) {
268 if (phase->callbacks != NULL) {
269 struct boot_state_callback *bscb;
270
271 /* Remove the first callback. */
272 bscb = phase->callbacks;
273 phase->callbacks = bscb->next;
274 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500275
Julius Wernercd49cce2019-03-05 16:53:33 -0800276#if CONFIG(DEBUG_BOOT_STATE)
Lee Leahy10605352016-02-14 17:01:40 -0800277 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
278 bscb, bscb->location);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500279#endif
Aaron Durbin0748d302013-05-06 10:50:19 -0500280 bscb->callback(bscb->arg);
Aaron Durbin0748d302013-05-06 10:50:19 -0500281 continue;
282 }
283
284 /* All callbacks are complete and there are no blockers for
285 * this state. Therefore, this part of the state is complete. */
286 if (!phase->blockers)
287 break;
288
289 /* Something is blocking this state from transitioning. As
290 * there are no more callbacks a pending timer needs to be
291 * ran to unblock the state. */
292 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500293 }
294}
295
Aaron Durbin0748d302013-05-06 10:50:19 -0500296/* Keep track of the current state. */
297static struct state_tracker {
298 boot_state_t state_id;
299 boot_state_sequence_t seq;
300} current_phase = {
301 .state_id = BS_PRE_DEVICE,
302 .seq = BS_ON_ENTRY,
303};
304
305static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500306{
307
308 while (1) {
309 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500310 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500311
Aaron Durbin0748d302013-05-06 10:50:19 -0500312 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500313
314 if (state->complete) {
315 printk(BIOS_EMERG, "BS: %s state already executed.\n",
316 state->name);
317 break;
318 }
319
Julius Wernercd49cce2019-03-05 16:53:33 -0800320 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800321 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
322 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500323
Aaron Durbin15c671e2013-05-06 10:52:24 -0500324 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500325
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500326 bs_sample_time(state);
327
Aaron Durbin0748d302013-05-06 10:50:19 -0500328 bs_call_callbacks(state, current_phase.seq);
329 /* Update the current sequence so that any calls to block the
330 * current state from the run_state() function will place a
331 * block on the correct phase. */
332 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500333
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500334 bs_sample_time(state);
335
Duncan Lauriecb73a842013-06-10 10:41:04 -0700336 post_code(state->post_code);
337
Aaron Durbin0748d302013-05-06 10:50:19 -0500338 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500339
Julius Wernercd49cce2019-03-05 16:53:33 -0800340 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800341 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
342 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500343
344 bs_sample_time(state);
345
Aaron Durbin0748d302013-05-06 10:50:19 -0500346 bs_call_callbacks(state, current_phase.seq);
347
Julius Wernercd49cce2019-03-05 16:53:33 -0800348 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800349 printk(BIOS_DEBUG,
350 "----------------------------------------\n");
351
Aaron Durbin0748d302013-05-06 10:50:19 -0500352 /* Update the current phase with new state id and sequence. */
353 current_phase.state_id = next_id;
354 current_phase.seq = BS_ON_ENTRY;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500355
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500356 bs_sample_time(state);
357
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500358 state->complete = 1;
359 }
360}
361
362static int boot_state_sched_callback(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800363 struct boot_state_callback *bscb,
364 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500365{
366 if (state->complete) {
367 printk(BIOS_WARNING,
368 "Tried to schedule callback on completed state %s.\n",
369 state->name);
370
371 return -1;
372 }
373
Aaron Durbin0748d302013-05-06 10:50:19 -0500374 bscb->next = state->phases[seq].callbacks;
375 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500376
377 return 0;
378}
379
380int boot_state_sched_on_entry(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800381 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500382{
383 struct boot_state *state = &boot_states[state_id];
384
385 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
386}
387
388int boot_state_sched_on_exit(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800389 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500390{
391 struct boot_state *state = &boot_states[state_id];
392
393 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
394}
Maciej Pijankaea921852009-10-27 14:29:29 +0000395
Aaron Durbina4feddf2013-04-24 16:12:52 -0500396static void boot_state_schedule_static_entries(void)
397{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500398 extern struct boot_state_init_entry *_bs_init_begin[];
399 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500400
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500401 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
402 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500403
Aaron Durbina4feddf2013-04-24 16:12:52 -0500404 if (cur->when == BS_ON_ENTRY)
405 boot_state_sched_on_entry(&cur->bscb, cur->state);
406 else
407 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500408 }
409}
410
Stefan Reinauer6adef082013-05-09 16:30:06 -0700411void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000412{
Nico Hubere0ed9022016-10-07 12:58:17 +0200413 /*
414 * We can generally jump between C and Ada code back and forth
415 * without trouble. But since we don't have an Ada main() we
416 * have to do some Ada package initializations that GNAT would
417 * do there. This has to be done before calling any Ada code.
418 *
419 * The package initializations should not have any dependen-
420 * cies on C code. So we can call them here early, and don't
421 * have to worry at which point we can start to use Ada.
422 */
423 ramstage_adainit();
424
Duncan Lauriea5181512015-06-04 08:42:48 -0700425 /* TODO: Understand why this is here and move to arch/platform code. */
426 /* For MMIO UART this needs to be called before any other printk. */
Kyösti Mälkki7336f972020-06-08 06:05:03 +0300427 if (ENV_X86)
Duncan Lauriea5181512015-06-04 08:42:48 -0700428 init_timer();
429
Aaron Durbinbe347972015-04-21 11:57:18 -0500430 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
431 * it is the very first thing done in ramstage.*/
432 console_init();
Aaron Durbinbe347972015-04-21 11:57:18 -0500433 post_code(POST_CONSOLE_READY);
434
Asami Doi38a12862019-08-22 11:15:12 +0900435 exception_init();
436
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500437 /*
Subrata Banik91160e12019-01-29 17:16:15 +0530438 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300439 * the cbmem infrastructure being around. Explicitly recover it.
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500440 */
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300441 cbmem_initialize();
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500442
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300443 timestamp_add_now(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000444 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000445
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300446 /* Handoff sleep type from romstage. */
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300447 acpi_is_wakeup();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500448 threads_initialize();
449
Aaron Durbina4feddf2013-04-24 16:12:52 -0500450 /* Schedule the static boot state entries. */
451 boot_state_schedule_static_entries();
452
Aaron Durbin0748d302013-05-06 10:50:19 -0500453 bs_walk_state_machine();
454
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500455 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000456}
457
Aaron Durbin0748d302013-05-06 10:50:19 -0500458
459int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
460{
461 struct boot_phase *bp;
462
463 /* Blocking a previously ran state is not appropriate. */
464 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800465 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500466 printk(BIOS_WARNING,
467 "BS: Completed state (%d, %d) block attempted.\n",
468 state, seq);
469 return -1;
470 }
471
472 bp = &boot_states[state].phases[seq];
473 bp->blockers++;
474
475 return 0;
476}
477
478int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
479{
480 struct boot_phase *bp;
481
482 /* Blocking a previously ran state is not appropriate. */
483 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800484 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500485 printk(BIOS_WARNING,
486 "BS: Completed state (%d, %d) unblock attempted.\n",
487 state, seq);
488 return -1;
489 }
490
491 bp = &boot_states[state].phases[seq];
492
493 if (bp->blockers == 0) {
494 printk(BIOS_WARNING,
495 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
496 state, seq);
497 return -1;
498 }
499
500 bp->blockers--;
501
502 return 0;
503}
504
505void boot_state_current_block(void)
506{
507 boot_state_block(current_phase.state_id, current_phase.seq);
508}
509
510void boot_state_current_unblock(void)
511{
512 boot_state_unblock(current_phase.state_id, current_phase.seq);
513}