blob: 72e33760282e1d01583ac419f9db47130bfb487a [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 *
Aaron Durbin7e35efa2013-04-24 15:14:01 -05004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Eric Biederman8ca8d762003-04-22 19:02:15 +000013 */
14
15
16/*
Stefan Reinauerf8ee1802008-01-18 15:08:58 +000017 * C Bootstrap code for the coreboot
Eric Biederman8ca8d762003-04-22 19:02:15 +000018 */
19
Nico Hubere0ed9022016-10-07 12:58:17 +020020#include <adainit.h>
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>
Kyösti Mälkki463ad512019-11-01 09:12:34 +020025#include <commonlib/helpers.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 Reinauer7e9771c2009-04-22 08:17:38 +000032#include <boot/tables.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050033#include <program_loading.h>
Julius Wernercd49cce2019-03-05 16:53:33 -080034#if CONFIG(HAVE_ACPI_RESUME)
Rudolf Mareka572f832009-04-13 17:57:44 +000035#include <arch/acpi.h>
Stefan Reinauer3935b192009-04-14 06:38:15 +000036#endif
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050037#include <timer.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070038#include <timestamp.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050039#include <thread.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000040
Aaron Durbin7e35efa2013-04-24 15:14:01 -050041static boot_state_t bs_pre_device(void *arg);
42static boot_state_t bs_dev_init_chips(void *arg);
43static boot_state_t bs_dev_enumerate(void *arg);
44static boot_state_t bs_dev_resources(void *arg);
David Hendrickscca68592013-06-20 14:08:27 -070045static boot_state_t bs_dev_enable(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050046static boot_state_t bs_dev_init(void *arg);
47static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050048static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050049static boot_state_t bs_os_resume(void *arg);
50static boot_state_t bs_write_tables(void *arg);
51static boot_state_t bs_payload_load(void *arg);
52static boot_state_t bs_payload_boot(void *arg);
53
Aaron Durbin0748d302013-05-06 10:50:19 -050054/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
55 * blocked from transitioning to the next (state,seq) pair. When the blockers
56 * field is 0 a transition may occur. */
57struct boot_phase {
58 struct boot_state_callback *callbacks;
59 int blockers;
60};
61
Aaron Durbin7e35efa2013-04-24 15:14:01 -050062struct boot_state {
63 const char *name;
64 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070065 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050066 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050067 boot_state_t (*run_state)(void *arg);
68 void *arg;
Kyösti Mälkki94694a82019-11-02 18:14:31 +020069 int num_samples;
Aaron Durbin8fc41e12013-04-29 23:22:01 -050070 int complete : 1;
Aaron Durbin7e35efa2013-04-24 15:14:01 -050071};
72
Aaron Durbin15c671e2013-05-06 10:52:24 -050073#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050074 { \
75 .name = #state_, \
76 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070077 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050078 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050079 .run_state = run_func_, \
80 .arg = NULL, \
81 .complete = 0, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050082 }
83#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -050084 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -050085
86static struct boot_state boot_states[] = {
87 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
88 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
89 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
90 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
David Hendrickscca68592013-06-20 14:08:27 -070091 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050092 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
93 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050094 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -050095 BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
96 BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
97 BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
98 BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050099};
100
Aaron Durbin64031672018-04-21 14:45:32 -0600101void __weak arch_bootstate_coreboot_exit(void) { }
Aaron Durbin16bd2672016-12-07 11:58:20 -0600102
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500103static boot_state_t bs_pre_device(void *arg)
104{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500105 return BS_DEV_INIT_CHIPS;
106}
107
108static boot_state_t bs_dev_init_chips(void *arg)
109{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300110 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500111
112 /* Initialize chips early, they might disable unused devices. */
113 dev_initialize_chips();
114
115 return BS_DEV_ENUMERATE;
116}
117
118static boot_state_t bs_dev_enumerate(void *arg)
119{
120 /* Find the devices we don't have hard coded knowledge about. */
121 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500122
123 return BS_DEV_RESOURCES;
124}
125
126static boot_state_t bs_dev_resources(void *arg)
127{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300128 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700129
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500130 /* Now compute and assign the bus resources. */
131 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500132
133 return BS_DEV_ENABLE;
134}
135
David Hendrickscca68592013-06-20 14:08:27 -0700136static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500137{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300138 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700139
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500140 /* Now actually enable devices on the bus */
141 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500142
143 return BS_DEV_INIT;
144}
145
146static boot_state_t bs_dev_init(void *arg)
147{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300148 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700149
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500150 /* And of course initialize devices on the bus */
151 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500152
153 return BS_POST_DEVICE;
154}
155
156static boot_state_t bs_post_device(void *arg)
157{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600158 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300159 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500160
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500161 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500162}
163
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500164static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500165{
Julius Wernercd49cce2019-03-05 16:53:33 -0800166#if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500167 void *wake_vector;
168
169 wake_vector = acpi_find_wakeup_vector();
170
171 if (wake_vector != NULL) {
172 boot_states[BS_OS_RESUME].arg = wake_vector;
173 return BS_OS_RESUME;
174 }
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500175#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500176 timestamp_add_now(TS_CBMEM_POST);
177
178 return BS_WRITE_TABLES;
179}
180
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500181static boot_state_t bs_os_resume(void *wake_vector)
182{
Julius Wernercd49cce2019-03-05 16:53:33 -0800183#if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin16bd2672016-12-07 11:58:20 -0600184 arch_bootstate_coreboot_exit();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500185 acpi_resume(wake_vector);
186#endif
187 return BS_WRITE_TABLES;
188}
189
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500190static boot_state_t bs_write_tables(void *arg)
191{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500192 timestamp_add_now(TS_WRITE_TABLES);
193
194 /* Now that we have collected all of our information
195 * write our configuration tables.
196 */
197 write_tables();
198
Paul Menzel00563bf2016-12-05 23:06:37 +0100199 timestamp_add_now(TS_FINALIZE_CHIPS);
Marc Jones2a58ecd2013-10-29 17:32:00 -0600200 dev_finalize_chips();
201
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500202 return BS_PAYLOAD_LOAD;
203}
204
205static boot_state_t bs_payload_load(void *arg)
206{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500207 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500208
209 return BS_PAYLOAD_BOOT;
210}
211
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600212static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500213{
Aaron Durbin16bd2672016-12-07 11:58:20 -0600214 arch_bootstate_coreboot_exit();
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500215 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500216
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200217 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500218 /* Returning from this state will fail because the following signals
219 * return to a completed state. */
220 return BS_PAYLOAD_BOOT;
221}
222
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200223/*
224 * Typically a state will take 4 time samples:
225 * 1. Before state entry callbacks
226 * 2. After state entry callbacks / Before state function.
227 * 3. After state function / Before state exit callbacks.
228 * 4. After state exit callbacks.
229 */
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500230static void bs_sample_time(struct boot_state *state)
231{
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200232 static const char *const sample_id[] = { "entry", "run", "exit" };
233 static struct mono_time previous_sample;
234 struct mono_time this_sample;
235 long console;
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500236
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200237 if (!CONFIG(HAVE_MONOTONIC_TIMER))
238 return;
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200239
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200240 console = console_time_get_and_reset();
241 timer_monotonic_get(&this_sample);
242 state->num_samples++;
243
244 int i = state->num_samples - 2;
245 if ((i >= 0) && (i < ARRAY_SIZE(sample_id))) {
246 long execution = mono_time_diff_microseconds(&previous_sample, &this_sample);
247
248 /* Report with millisecond precision to reduce log diffs. */
249 execution = DIV_ROUND_CLOSEST(execution, USECS_PER_MSEC);
250 console = DIV_ROUND_CLOSEST(console, USECS_PER_MSEC);
251 if (execution) {
252 printk(BIOS_DEBUG, "BS: %s %s times (exec / console): %ld / %ld ms\n",
253 state->name, sample_id[i], execution - console, console);
254 /* Reset again to ignore printk() time above. */
255 console_time_get_and_reset();
256 }
257 }
258 timer_monotonic_get(&previous_sample);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500259}
260
Julius Wernercd49cce2019-03-05 16:53:33 -0800261#if CONFIG(TIMER_QUEUE)
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500262static void bs_run_timers(int drain)
263{
264 /* Drain all timer callbacks until none are left, if directed.
265 * Otherwise run the timers only once. */
266 do {
267 if (!timers_run())
268 break;
269 } while (drain);
270}
271#else
272static void bs_run_timers(int drain) {}
273#endif
274
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500275static void bs_call_callbacks(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800276 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500277{
Aaron Durbin0748d302013-05-06 10:50:19 -0500278 struct boot_phase *phase = &state->phases[seq];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500279
Aaron Durbin0748d302013-05-06 10:50:19 -0500280 while (1) {
281 if (phase->callbacks != NULL) {
282 struct boot_state_callback *bscb;
283
284 /* Remove the first callback. */
285 bscb = phase->callbacks;
286 phase->callbacks = bscb->next;
287 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500288
Julius Wernercd49cce2019-03-05 16:53:33 -0800289#if CONFIG(DEBUG_BOOT_STATE)
Lee Leahy10605352016-02-14 17:01:40 -0800290 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
291 bscb, bscb->location);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500292#endif
Aaron Durbin0748d302013-05-06 10:50:19 -0500293 bscb->callback(bscb->arg);
Aaron Durbin0748d302013-05-06 10:50:19 -0500294 continue;
295 }
296
297 /* All callbacks are complete and there are no blockers for
298 * this state. Therefore, this part of the state is complete. */
299 if (!phase->blockers)
300 break;
301
302 /* Something is blocking this state from transitioning. As
303 * there are no more callbacks a pending timer needs to be
304 * ran to unblock the state. */
305 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500306 }
307}
308
Aaron Durbin0748d302013-05-06 10:50:19 -0500309/* Keep track of the current state. */
310static struct state_tracker {
311 boot_state_t state_id;
312 boot_state_sequence_t seq;
313} current_phase = {
314 .state_id = BS_PRE_DEVICE,
315 .seq = BS_ON_ENTRY,
316};
317
318static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500319{
320
321 while (1) {
322 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500323 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500324
Aaron Durbin0748d302013-05-06 10:50:19 -0500325 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500326
327 if (state->complete) {
328 printk(BIOS_EMERG, "BS: %s state already executed.\n",
329 state->name);
330 break;
331 }
332
Julius Wernercd49cce2019-03-05 16:53:33 -0800333 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800334 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
335 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500336
Aaron Durbin15c671e2013-05-06 10:52:24 -0500337 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500338
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500339 bs_sample_time(state);
340
Aaron Durbin0748d302013-05-06 10:50:19 -0500341 bs_call_callbacks(state, current_phase.seq);
342 /* Update the current sequence so that any calls to block the
343 * current state from the run_state() function will place a
344 * block on the correct phase. */
345 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500346
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500347 bs_sample_time(state);
348
Duncan Lauriecb73a842013-06-10 10:41:04 -0700349 post_code(state->post_code);
350
Aaron Durbin0748d302013-05-06 10:50:19 -0500351 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500352
Julius Wernercd49cce2019-03-05 16:53:33 -0800353 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800354 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
355 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500356
357 bs_sample_time(state);
358
Aaron Durbin0748d302013-05-06 10:50:19 -0500359 bs_call_callbacks(state, current_phase.seq);
360
Julius Wernercd49cce2019-03-05 16:53:33 -0800361 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800362 printk(BIOS_DEBUG,
363 "----------------------------------------\n");
364
Aaron Durbin0748d302013-05-06 10:50:19 -0500365 /* Update the current phase with new state id and sequence. */
366 current_phase.state_id = next_id;
367 current_phase.seq = BS_ON_ENTRY;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500368
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500369 bs_sample_time(state);
370
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500371 state->complete = 1;
372 }
373}
374
375static int boot_state_sched_callback(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800376 struct boot_state_callback *bscb,
377 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500378{
379 if (state->complete) {
380 printk(BIOS_WARNING,
381 "Tried to schedule callback on completed state %s.\n",
382 state->name);
383
384 return -1;
385 }
386
Aaron Durbin0748d302013-05-06 10:50:19 -0500387 bscb->next = state->phases[seq].callbacks;
388 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500389
390 return 0;
391}
392
393int boot_state_sched_on_entry(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800394 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500395{
396 struct boot_state *state = &boot_states[state_id];
397
398 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
399}
400
401int boot_state_sched_on_exit(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800402 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500403{
404 struct boot_state *state = &boot_states[state_id];
405
406 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
407}
Maciej Pijankaea921852009-10-27 14:29:29 +0000408
Aaron Durbina4feddf2013-04-24 16:12:52 -0500409static void boot_state_schedule_static_entries(void)
410{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500411 extern struct boot_state_init_entry *_bs_init_begin[];
412 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500413
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500414 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
415 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500416
Aaron Durbina4feddf2013-04-24 16:12:52 -0500417 if (cur->when == BS_ON_ENTRY)
418 boot_state_sched_on_entry(&cur->bscb, cur->state);
419 else
420 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500421 }
422}
423
Stefan Reinauer6adef082013-05-09 16:30:06 -0700424void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000425{
Nico Hubere0ed9022016-10-07 12:58:17 +0200426 /*
427 * We can generally jump between C and Ada code back and forth
428 * without trouble. But since we don't have an Ada main() we
429 * have to do some Ada package initializations that GNAT would
430 * do there. This has to be done before calling any Ada code.
431 *
432 * The package initializations should not have any dependen-
433 * cies on C code. So we can call them here early, and don't
434 * have to worry at which point we can start to use Ada.
435 */
436 ramstage_adainit();
437
Duncan Lauriea5181512015-06-04 08:42:48 -0700438 /* TODO: Understand why this is here and move to arch/platform code. */
439 /* For MMIO UART this needs to be called before any other printk. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800440 if (CONFIG(ARCH_X86))
Duncan Lauriea5181512015-06-04 08:42:48 -0700441 init_timer();
442
Aaron Durbinbe347972015-04-21 11:57:18 -0500443 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
444 * it is the very first thing done in ramstage.*/
445 console_init();
Aaron Durbinbe347972015-04-21 11:57:18 -0500446 post_code(POST_CONSOLE_READY);
447
Asami Doi38a12862019-08-22 11:15:12 +0900448 exception_init();
449
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500450 /*
Subrata Banik91160e12019-01-29 17:16:15 +0530451 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300452 * the cbmem infrastructure being around. Explicitly recover it.
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500453 */
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300454 cbmem_initialize();
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500455
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300456 timestamp_add_now(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000457 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000458
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300459 /* Handoff sleep type from romstage. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800460#if CONFIG(HAVE_ACPI_RESUME)
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300461 acpi_is_wakeup();
462#endif
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 ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800480 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500481 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 ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800499 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500500 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}