blob: 28c2b82fedf26c839e2165536c7f25474c53ce68 [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
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +03008#include <acpi/acpi.h>
Kyösti Mälkkie0183d62020-06-17 13:45:16 +03009#include <acpi/acpi_gnvs.h>
Ricardo Quesada470ca5712021-07-16 16:39:28 -070010#include <adainit.h>
Julius Werner85620db2013-11-13 18:22:15 -080011#include <arch/exception.h>
Ricardo Quesada470ca5712021-07-16 16:39:28 -070012#include <boot/tables.h>
Aaron Durbin7e35efa2013-04-24 15:14:01 -050013#include <bootstate.h>
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -050014#include <cbmem.h>
Ricardo Quesada470ca5712021-07-16 16:39:28 -070015#include <commonlib/console/post_codes.h>
16#include <commonlib/helpers.h>
17#include <console/console.h>
18#include <delay.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000019#include <device/device.h>
20#include <device/pci.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050021#include <program_loading.h>
Ricardo Quesada470ca5712021-07-16 16:39:28 -070022#include <stdlib.h>
23#include <thread.h>
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050024#include <timer.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070025#include <timestamp.h>
Felix Heldf1077672021-10-01 19:53:39 +020026#include <types.h>
Kyösti Mälkki3dc17922021-03-16 19:01:48 +020027#include <vendorcode/google/chromeos/gnvs.h>
Ricardo Quesada470ca5712021-07-16 16:39:28 -070028#include <version.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000029
Aaron Durbin7e35efa2013-04-24 15:14:01 -050030static boot_state_t bs_pre_device(void *arg);
31static boot_state_t bs_dev_init_chips(void *arg);
32static boot_state_t bs_dev_enumerate(void *arg);
33static boot_state_t bs_dev_resources(void *arg);
David Hendrickscca68592013-06-20 14:08:27 -070034static boot_state_t bs_dev_enable(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050035static boot_state_t bs_dev_init(void *arg);
36static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050037static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050038static boot_state_t bs_os_resume(void *arg);
39static boot_state_t bs_write_tables(void *arg);
40static boot_state_t bs_payload_load(void *arg);
41static boot_state_t bs_payload_boot(void *arg);
42
Aaron Durbin0748d302013-05-06 10:50:19 -050043/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
44 * blocked from transitioning to the next (state,seq) pair. When the blockers
45 * field is 0 a transition may occur. */
46struct boot_phase {
47 struct boot_state_callback *callbacks;
48 int blockers;
49};
50
Aaron Durbin7e35efa2013-04-24 15:14:01 -050051struct boot_state {
52 const char *name;
53 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070054 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050055 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050056 boot_state_t (*run_state)(void *arg);
57 void *arg;
Kyösti Mälkki94694a82019-11-02 18:14:31 +020058 int num_samples;
Felix Held56da0b72021-10-01 20:03:02 +020059 bool complete;
Aaron Durbin7e35efa2013-04-24 15:14:01 -050060};
61
Aaron Durbin15c671e2013-05-06 10:52:24 -050062#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050063 { \
64 .name = #state_, \
65 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070066 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050067 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050068 .run_state = run_func_, \
69 .arg = NULL, \
Felix Held56da0b72021-10-01 20:03:02 +020070 .complete = false, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050071 }
72#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -050073 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -050074
75static struct boot_state boot_states[] = {
76 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
77 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
78 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
79 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
David Hendrickscca68592013-06-20 14:08:27 -070080 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050081 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
82 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050083 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -050084 BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
85 BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
86 BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
87 BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050088};
89
Aaron Durbin64031672018-04-21 14:45:32 -060090void __weak arch_bootstate_coreboot_exit(void) { }
Aaron Durbin16bd2672016-12-07 11:58:20 -060091
Aaron Durbin7e35efa2013-04-24 15:14:01 -050092static boot_state_t bs_pre_device(void *arg)
93{
Aaron Durbin7e35efa2013-04-24 15:14:01 -050094 return BS_DEV_INIT_CHIPS;
95}
96
97static boot_state_t bs_dev_init_chips(void *arg)
98{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030099 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500100
101 /* Initialize chips early, they might disable unused devices. */
102 dev_initialize_chips();
103
104 return BS_DEV_ENUMERATE;
105}
106
107static boot_state_t bs_dev_enumerate(void *arg)
108{
109 /* Find the devices we don't have hard coded knowledge about. */
110 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500111
112 return BS_DEV_RESOURCES;
113}
114
115static boot_state_t bs_dev_resources(void *arg)
116{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300117 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700118
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500119 /* Now compute and assign the bus resources. */
120 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500121
122 return BS_DEV_ENABLE;
123}
124
David Hendrickscca68592013-06-20 14:08:27 -0700125static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500126{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300127 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700128
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500129 /* Now actually enable devices on the bus */
130 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500131
132 return BS_DEV_INIT;
133}
134
135static boot_state_t bs_dev_init(void *arg)
136{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300137 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700138
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500139 /* And of course initialize devices on the bus */
140 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500141
142 return BS_POST_DEVICE;
143}
144
145static boot_state_t bs_post_device(void *arg)
146{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600147 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300148 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500149
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500150 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500151}
152
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500153static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500154{
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300155 void *wake_vector = NULL;
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500156
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300157 if (CONFIG(HAVE_ACPI_RESUME))
158 wake_vector = acpi_find_wakeup_vector();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500159
160 if (wake_vector != NULL) {
161 boot_states[BS_OS_RESUME].arg = wake_vector;
162 return BS_OS_RESUME;
163 }
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300164
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500165 timestamp_add_now(TS_CBMEM_POST);
166
167 return BS_WRITE_TABLES;
168}
169
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500170static boot_state_t bs_os_resume(void *wake_vector)
171{
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300172 if (CONFIG(HAVE_ACPI_RESUME)) {
173 arch_bootstate_coreboot_exit();
174 acpi_resume(wake_vector);
Kyösti Mälkkia4c0e1a2020-06-18 08:28:12 +0300175 /* We will not come back. */
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300176 }
Kyösti Mälkkia4c0e1a2020-06-18 08:28:12 +0300177 die("Failed OS resume\n");
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500178}
179
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500180static boot_state_t bs_write_tables(void *arg)
181{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500182 timestamp_add_now(TS_WRITE_TABLES);
183
184 /* Now that we have collected all of our information
185 * write our configuration tables.
186 */
187 write_tables();
188
Paul Menzel00563bf2016-12-05 23:06:37 +0100189 timestamp_add_now(TS_FINALIZE_CHIPS);
Marc Jones2a58ecd2013-10-29 17:32:00 -0600190 dev_finalize_chips();
191
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500192 return BS_PAYLOAD_LOAD;
193}
194
195static boot_state_t bs_payload_load(void *arg)
196{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500197 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500198
199 return BS_PAYLOAD_BOOT;
200}
201
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600202static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500203{
Aaron Durbin16bd2672016-12-07 11:58:20 -0600204 arch_bootstate_coreboot_exit();
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500205 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500206
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200207 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500208 /* Returning from this state will fail because the following signals
209 * return to a completed state. */
210 return BS_PAYLOAD_BOOT;
211}
212
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200213/*
214 * Typically a state will take 4 time samples:
215 * 1. Before state entry callbacks
216 * 2. After state entry callbacks / Before state function.
217 * 3. After state function / Before state exit callbacks.
218 * 4. After state exit callbacks.
219 */
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500220static void bs_sample_time(struct boot_state *state)
221{
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200222 static const char *const sample_id[] = { "entry", "run", "exit" };
223 static struct mono_time previous_sample;
224 struct mono_time this_sample;
225 long console;
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500226
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200227 if (!CONFIG(HAVE_MONOTONIC_TIMER))
228 return;
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200229
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200230 console = console_time_get_and_reset();
231 timer_monotonic_get(&this_sample);
232 state->num_samples++;
233
234 int i = state->num_samples - 2;
235 if ((i >= 0) && (i < ARRAY_SIZE(sample_id))) {
236 long execution = mono_time_diff_microseconds(&previous_sample, &this_sample);
237
238 /* Report with millisecond precision to reduce log diffs. */
239 execution = DIV_ROUND_CLOSEST(execution, USECS_PER_MSEC);
240 console = DIV_ROUND_CLOSEST(console, USECS_PER_MSEC);
241 if (execution) {
242 printk(BIOS_DEBUG, "BS: %s %s times (exec / console): %ld / %ld ms\n",
243 state->name, sample_id[i], execution - console, console);
244 /* Reset again to ignore printk() time above. */
245 console_time_get_and_reset();
246 }
247 }
248 timer_monotonic_get(&previous_sample);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500249}
250
Julius Wernercd49cce2019-03-05 16:53:33 -0800251#if CONFIG(TIMER_QUEUE)
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500252static void bs_run_timers(int drain)
253{
254 /* Drain all timer callbacks until none are left, if directed.
255 * Otherwise run the timers only once. */
256 do {
257 if (!timers_run())
258 break;
259 } while (drain);
260}
261#else
262static void bs_run_timers(int drain) {}
263#endif
264
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500265static void bs_call_callbacks(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800266 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500267{
Aaron Durbin0748d302013-05-06 10:50:19 -0500268 struct boot_phase *phase = &state->phases[seq];
Raul E Rangelcb428742021-06-09 16:49:53 -0600269 struct mono_time mt_start, mt_stop;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500270
Aaron Durbin0748d302013-05-06 10:50:19 -0500271 while (1) {
272 if (phase->callbacks != NULL) {
273 struct boot_state_callback *bscb;
274
275 /* Remove the first callback. */
276 bscb = phase->callbacks;
277 phase->callbacks = bscb->next;
278 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500279
Angel Pons83156682021-06-10 13:57:35 +0200280 if (CONFIG(DEBUG_BOOT_STATE)) {
281 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
282 bscb, bscb_location(bscb));
Raul E Rangelcb428742021-06-09 16:49:53 -0600283 timer_monotonic_get(&mt_start);
Angel Pons83156682021-06-10 13:57:35 +0200284 }
Aaron Durbin0748d302013-05-06 10:50:19 -0500285 bscb->callback(bscb->arg);
Raul E Rangelcb428742021-06-09 16:49:53 -0600286 if (CONFIG(DEBUG_BOOT_STATE)) {
287 timer_monotonic_get(&mt_stop);
288 printk(BIOS_DEBUG, "BS: callback (%p) @ %s (%ld ms).\n", bscb,
289 bscb_location(bscb),
290 mono_time_diff_microseconds(&mt_start, &mt_stop)
291 / USECS_PER_MSEC);
292 }
Raul E Rangelb5811c02021-11-04 11:52:17 -0600293
294 bs_run_timers(0);
295
Aaron Durbin0748d302013-05-06 10:50:19 -0500296 continue;
297 }
298
299 /* All callbacks are complete and there are no blockers for
300 * this state. Therefore, this part of the state is complete. */
301 if (!phase->blockers)
302 break;
303
304 /* Something is blocking this state from transitioning. As
305 * there are no more callbacks a pending timer needs to be
306 * ran to unblock the state. */
307 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500308 }
309}
310
Aaron Durbin0748d302013-05-06 10:50:19 -0500311/* Keep track of the current state. */
312static struct state_tracker {
313 boot_state_t state_id;
314 boot_state_sequence_t seq;
315} current_phase = {
316 .state_id = BS_PRE_DEVICE,
317 .seq = BS_ON_ENTRY,
318};
319
320static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500321{
322
323 while (1) {
324 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500325 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500326
Aaron Durbin0748d302013-05-06 10:50:19 -0500327 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500328
329 if (state->complete) {
330 printk(BIOS_EMERG, "BS: %s state already executed.\n",
331 state->name);
332 break;
333 }
334
Julius Wernercd49cce2019-03-05 16:53:33 -0800335 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800336 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
337 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500338
Aaron Durbin15c671e2013-05-06 10:52:24 -0500339 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500340
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500341 bs_sample_time(state);
342
Aaron Durbin0748d302013-05-06 10:50:19 -0500343 bs_call_callbacks(state, current_phase.seq);
344 /* Update the current sequence so that any calls to block the
345 * current state from the run_state() function will place a
346 * block on the correct phase. */
347 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500348
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500349 bs_sample_time(state);
350
Duncan Lauriecb73a842013-06-10 10:41:04 -0700351 post_code(state->post_code);
352
Aaron Durbin0748d302013-05-06 10:50:19 -0500353 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500354
Julius Wernercd49cce2019-03-05 16:53:33 -0800355 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800356 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
357 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500358
359 bs_sample_time(state);
360
Raul E Rangelb5811c02021-11-04 11:52:17 -0600361 bs_run_timers(0);
362
Aaron Durbin0748d302013-05-06 10:50:19 -0500363 bs_call_callbacks(state, current_phase.seq);
364
Julius Wernercd49cce2019-03-05 16:53:33 -0800365 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800366 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
Felix Held56da0b72021-10-01 20:03:02 +0200375 state->complete = true;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500376 }
377}
378
379static int boot_state_sched_callback(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800380 struct boot_state_callback *bscb,
381 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500382{
383 if (state->complete) {
384 printk(BIOS_WARNING,
385 "Tried to schedule callback on completed state %s.\n",
386 state->name);
387
388 return -1;
389 }
390
Aaron Durbin0748d302013-05-06 10:50:19 -0500391 bscb->next = state->phases[seq].callbacks;
392 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500393
394 return 0;
395}
396
397int boot_state_sched_on_entry(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800398 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500399{
400 struct boot_state *state = &boot_states[state_id];
401
402 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
403}
404
405int boot_state_sched_on_exit(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800406 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500407{
408 struct boot_state *state = &boot_states[state_id];
409
410 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
411}
Maciej Pijankaea921852009-10-27 14:29:29 +0000412
Aaron Durbina4feddf2013-04-24 16:12:52 -0500413static void boot_state_schedule_static_entries(void)
414{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500415 extern struct boot_state_init_entry *_bs_init_begin[];
416 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500417
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500418 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
419 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500420
Aaron Durbina4feddf2013-04-24 16:12:52 -0500421 if (cur->when == BS_ON_ENTRY)
422 boot_state_sched_on_entry(&cur->bscb, cur->state);
423 else
424 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500425 }
426}
427
Stefan Reinauer6adef082013-05-09 16:30:06 -0700428void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000429{
Nico Hubere0ed9022016-10-07 12:58:17 +0200430 /*
431 * We can generally jump between C and Ada code back and forth
432 * without trouble. But since we don't have an Ada main() we
433 * have to do some Ada package initializations that GNAT would
434 * do there. This has to be done before calling any Ada code.
435 *
436 * The package initializations should not have any dependen-
437 * cies on C code. So we can call them here early, and don't
438 * have to worry at which point we can start to use Ada.
439 */
440 ramstage_adainit();
441
Duncan Lauriea5181512015-06-04 08:42:48 -0700442 /* TODO: Understand why this is here and move to arch/platform code. */
443 /* For MMIO UART this needs to be called before any other printk. */
Kyösti Mälkki7336f972020-06-08 06:05:03 +0300444 if (ENV_X86)
Duncan Lauriea5181512015-06-04 08:42:48 -0700445 init_timer();
446
Aaron Durbinbe347972015-04-21 11:57:18 -0500447 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
448 * it is the very first thing done in ramstage.*/
449 console_init();
Aaron Durbinbe347972015-04-21 11:57:18 -0500450 post_code(POST_CONSOLE_READY);
451
Asami Doi38a12862019-08-22 11:15:12 +0900452 exception_init();
453
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500454 /*
Subrata Banik91160e12019-01-29 17:16:15 +0530455 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300456 * the cbmem infrastructure being around. Explicitly recover it.
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500457 */
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300458 cbmem_initialize();
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500459
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300460 timestamp_add_now(TS_START_RAMSTAGE);
Subrata Banik8d2b0dc2021-05-05 19:46:09 +0530461 post_code(POST_ENTRY_HARDWAREMAIN);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000462
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300463 /* Handoff sleep type from romstage. */
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +0200464 acpi_is_wakeup_s3();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500465
Kyösti Mälkkie0183d62020-06-17 13:45:16 +0300466 /* Initialise GNVS early. */
Kyösti Mälkki661ad462020-12-29 06:26:21 +0200467 if (CONFIG(ACPI_SOC_NVS))
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +0200468 acpi_create_gnvs();
Kyösti Mälkkie0183d62020-06-17 13:45:16 +0300469
Kyösti Mälkki3dc17922021-03-16 19:01:48 +0200470 if (CONFIG(CHROMEOS_NVS))
471 chromeos_init_chromeos_acpi();
472
Aaron Durbina4feddf2013-04-24 16:12:52 -0500473 /* Schedule the static boot state entries. */
474 boot_state_schedule_static_entries();
475
Aaron Durbin0748d302013-05-06 10:50:19 -0500476 bs_walk_state_machine();
477
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500478 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000479}
480
Aaron Durbin0748d302013-05-06 10:50:19 -0500481
482int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
483{
484 struct boot_phase *bp;
485
486 /* Blocking a previously ran state is not appropriate. */
487 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800488 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500489 printk(BIOS_WARNING,
490 "BS: Completed state (%d, %d) block attempted.\n",
491 state, seq);
492 return -1;
493 }
494
495 bp = &boot_states[state].phases[seq];
496 bp->blockers++;
497
498 return 0;
499}
500
501int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
502{
503 struct boot_phase *bp;
504
505 /* Blocking a previously ran state is not appropriate. */
506 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800507 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500508 printk(BIOS_WARNING,
509 "BS: Completed state (%d, %d) unblock attempted.\n",
510 state, seq);
511 return -1;
512 }
513
514 bp = &boot_states[state].phases[seq];
515
516 if (bp->blockers == 0) {
517 printk(BIOS_WARNING,
518 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
519 state, seq);
520 return -1;
521 }
522
523 bp->blockers--;
524
525 return 0;
526}