blob: 90c910a320b178838aa61bb3274ff5537f09efd3 [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>
Kyösti Mälkkie0183d62020-06-17 13:45:16 +030010#include <acpi/acpi_gnvs.h>
Julius Werner85620db2013-11-13 18:22:15 -080011#include <arch/exception.h>
Aaron Durbin7e35efa2013-04-24 15:14:01 -050012#include <bootstate.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000013#include <console/console.h>
Duncan Lauriecb73a842013-06-10 10:41:04 -070014#include <console/post_codes.h>
Kyösti Mälkki463ad512019-11-01 09:12:34 +020015#include <commonlib/helpers.h>
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -050016#include <cbmem.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000017#include <version.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000018#include <device/device.h>
19#include <device/pci.h>
Eric Biederman9b4336c2003-07-19 04:28:22 +000020#include <delay.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000021#include <stdlib.h>
Stefan Reinauer7e9771c2009-04-22 08:17:38 +000022#include <boot/tables.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050023#include <program_loading.h>
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050024#include <timer.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070025#include <timestamp.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050026#include <thread.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000027
Aaron Durbin7e35efa2013-04-24 15:14:01 -050028static boot_state_t bs_pre_device(void *arg);
29static boot_state_t bs_dev_init_chips(void *arg);
30static boot_state_t bs_dev_enumerate(void *arg);
31static boot_state_t bs_dev_resources(void *arg);
David Hendrickscca68592013-06-20 14:08:27 -070032static boot_state_t bs_dev_enable(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050033static boot_state_t bs_dev_init(void *arg);
34static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050035static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050036static boot_state_t bs_os_resume(void *arg);
37static boot_state_t bs_write_tables(void *arg);
38static boot_state_t bs_payload_load(void *arg);
39static boot_state_t bs_payload_boot(void *arg);
40
Aaron Durbin0748d302013-05-06 10:50:19 -050041/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
42 * blocked from transitioning to the next (state,seq) pair. When the blockers
43 * field is 0 a transition may occur. */
44struct boot_phase {
45 struct boot_state_callback *callbacks;
46 int blockers;
47};
48
Aaron Durbin7e35efa2013-04-24 15:14:01 -050049struct boot_state {
50 const char *name;
51 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070052 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050053 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050054 boot_state_t (*run_state)(void *arg);
55 void *arg;
Kyösti Mälkki94694a82019-11-02 18:14:31 +020056 int num_samples;
Aaron Durbin8fc41e12013-04-29 23:22:01 -050057 int complete : 1;
Aaron Durbin7e35efa2013-04-24 15:14:01 -050058};
59
Aaron Durbin15c671e2013-05-06 10:52:24 -050060#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050061 { \
62 .name = #state_, \
63 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070064 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050065 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050066 .run_state = run_func_, \
67 .arg = NULL, \
68 .complete = 0, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050069 }
70#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -050071 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -050072
73static struct boot_state boot_states[] = {
74 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
75 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
76 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
77 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
David Hendrickscca68592013-06-20 14:08:27 -070078 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050079 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
80 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050081 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -050082 BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
83 BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
84 BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
85 BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
Aaron Durbin7e35efa2013-04-24 15:14:01 -050086};
87
Aaron Durbin64031672018-04-21 14:45:32 -060088void __weak arch_bootstate_coreboot_exit(void) { }
Aaron Durbin16bd2672016-12-07 11:58:20 -060089
Aaron Durbin7e35efa2013-04-24 15:14:01 -050090static boot_state_t bs_pre_device(void *arg)
91{
Aaron Durbin7e35efa2013-04-24 15:14:01 -050092 return BS_DEV_INIT_CHIPS;
93}
94
95static boot_state_t bs_dev_init_chips(void *arg)
96{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +030097 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050098
99 /* Initialize chips early, they might disable unused devices. */
100 dev_initialize_chips();
101
102 return BS_DEV_ENUMERATE;
103}
104
105static boot_state_t bs_dev_enumerate(void *arg)
106{
107 /* Find the devices we don't have hard coded knowledge about. */
108 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500109
110 return BS_DEV_RESOURCES;
111}
112
113static boot_state_t bs_dev_resources(void *arg)
114{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300115 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700116
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500117 /* Now compute and assign the bus resources. */
118 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500119
120 return BS_DEV_ENABLE;
121}
122
David Hendrickscca68592013-06-20 14:08:27 -0700123static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500124{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300125 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700126
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500127 /* Now actually enable devices on the bus */
128 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500129
130 return BS_DEV_INIT;
131}
132
133static boot_state_t bs_dev_init(void *arg)
134{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300135 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700136
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500137 /* And of course initialize devices on the bus */
138 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500139
140 return BS_POST_DEVICE;
141}
142
143static boot_state_t bs_post_device(void *arg)
144{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600145 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300146 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500147
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500148 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500149}
150
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500151static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500152{
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300153 void *wake_vector = NULL;
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500154
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300155 if (CONFIG(HAVE_ACPI_RESUME))
156 wake_vector = acpi_find_wakeup_vector();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500157
158 if (wake_vector != NULL) {
159 boot_states[BS_OS_RESUME].arg = wake_vector;
160 return BS_OS_RESUME;
161 }
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300162
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500163 timestamp_add_now(TS_CBMEM_POST);
164
165 return BS_WRITE_TABLES;
166}
167
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500168static boot_state_t bs_os_resume(void *wake_vector)
169{
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300170 if (CONFIG(HAVE_ACPI_RESUME)) {
171 arch_bootstate_coreboot_exit();
172 acpi_resume(wake_vector);
Kyösti Mälkkia4c0e1a2020-06-18 08:28:12 +0300173 /* We will not come back. */
Kyösti Mälkki61ab3fe2020-06-17 14:17:41 +0300174 }
Kyösti Mälkkia4c0e1a2020-06-18 08:28:12 +0300175 die("Failed OS resume\n");
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500176}
177
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500178static boot_state_t bs_write_tables(void *arg)
179{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500180 timestamp_add_now(TS_WRITE_TABLES);
181
182 /* Now that we have collected all of our information
183 * write our configuration tables.
184 */
185 write_tables();
186
Paul Menzel00563bf2016-12-05 23:06:37 +0100187 timestamp_add_now(TS_FINALIZE_CHIPS);
Marc Jones2a58ecd2013-10-29 17:32:00 -0600188 dev_finalize_chips();
189
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500190 return BS_PAYLOAD_LOAD;
191}
192
193static boot_state_t bs_payload_load(void *arg)
194{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500195 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500196
197 return BS_PAYLOAD_BOOT;
198}
199
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600200static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500201{
Aaron Durbin16bd2672016-12-07 11:58:20 -0600202 arch_bootstate_coreboot_exit();
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500203 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500204
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200205 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500206 /* Returning from this state will fail because the following signals
207 * return to a completed state. */
208 return BS_PAYLOAD_BOOT;
209}
210
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200211/*
212 * Typically a state will take 4 time samples:
213 * 1. Before state entry callbacks
214 * 2. After state entry callbacks / Before state function.
215 * 3. After state function / Before state exit callbacks.
216 * 4. After state exit callbacks.
217 */
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500218static void bs_sample_time(struct boot_state *state)
219{
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200220 static const char *const sample_id[] = { "entry", "run", "exit" };
221 static struct mono_time previous_sample;
222 struct mono_time this_sample;
223 long console;
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500224
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200225 if (!CONFIG(HAVE_MONOTONIC_TIMER))
226 return;
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200227
Kyösti Mälkki94694a82019-11-02 18:14:31 +0200228 console = console_time_get_and_reset();
229 timer_monotonic_get(&this_sample);
230 state->num_samples++;
231
232 int i = state->num_samples - 2;
233 if ((i >= 0) && (i < ARRAY_SIZE(sample_id))) {
234 long execution = mono_time_diff_microseconds(&previous_sample, &this_sample);
235
236 /* Report with millisecond precision to reduce log diffs. */
237 execution = DIV_ROUND_CLOSEST(execution, USECS_PER_MSEC);
238 console = DIV_ROUND_CLOSEST(console, USECS_PER_MSEC);
239 if (execution) {
240 printk(BIOS_DEBUG, "BS: %s %s times (exec / console): %ld / %ld ms\n",
241 state->name, sample_id[i], execution - console, console);
242 /* Reset again to ignore printk() time above. */
243 console_time_get_and_reset();
244 }
245 }
246 timer_monotonic_get(&previous_sample);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500247}
248
Julius Wernercd49cce2019-03-05 16:53:33 -0800249#if CONFIG(TIMER_QUEUE)
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500250static void bs_run_timers(int drain)
251{
252 /* Drain all timer callbacks until none are left, if directed.
253 * Otherwise run the timers only once. */
254 do {
255 if (!timers_run())
256 break;
257 } while (drain);
258}
259#else
260static void bs_run_timers(int drain) {}
261#endif
262
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500263static void bs_call_callbacks(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800264 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500265{
Aaron Durbin0748d302013-05-06 10:50:19 -0500266 struct boot_phase *phase = &state->phases[seq];
Raul E Rangelcb428742021-06-09 16:49:53 -0600267 struct mono_time mt_start, mt_stop;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500268
Aaron Durbin0748d302013-05-06 10:50:19 -0500269 while (1) {
270 if (phase->callbacks != NULL) {
271 struct boot_state_callback *bscb;
272
273 /* Remove the first callback. */
274 bscb = phase->callbacks;
275 phase->callbacks = bscb->next;
276 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500277
Angel Pons83156682021-06-10 13:57:35 +0200278 if (CONFIG(DEBUG_BOOT_STATE)) {
279 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
280 bscb, bscb_location(bscb));
Raul E Rangelcb428742021-06-09 16:49:53 -0600281 timer_monotonic_get(&mt_start);
Angel Pons83156682021-06-10 13:57:35 +0200282 }
Aaron Durbin0748d302013-05-06 10:50:19 -0500283 bscb->callback(bscb->arg);
Raul E Rangelcb428742021-06-09 16:49:53 -0600284 if (CONFIG(DEBUG_BOOT_STATE)) {
285 timer_monotonic_get(&mt_stop);
286 printk(BIOS_DEBUG, "BS: callback (%p) @ %s (%ld ms).\n", bscb,
287 bscb_location(bscb),
288 mono_time_diff_microseconds(&mt_start, &mt_stop)
289 / USECS_PER_MSEC);
290 }
Aaron Durbin0748d302013-05-06 10:50:19 -0500291 continue;
292 }
293
294 /* All callbacks are complete and there are no blockers for
295 * this state. Therefore, this part of the state is complete. */
296 if (!phase->blockers)
297 break;
298
299 /* Something is blocking this state from transitioning. As
300 * there are no more callbacks a pending timer needs to be
301 * ran to unblock the state. */
302 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500303 }
304}
305
Aaron Durbin0748d302013-05-06 10:50:19 -0500306/* Keep track of the current state. */
307static struct state_tracker {
308 boot_state_t state_id;
309 boot_state_sequence_t seq;
310} current_phase = {
311 .state_id = BS_PRE_DEVICE,
312 .seq = BS_ON_ENTRY,
313};
314
315static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500316{
317
318 while (1) {
319 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500320 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500321
Aaron Durbin0748d302013-05-06 10:50:19 -0500322 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500323
324 if (state->complete) {
325 printk(BIOS_EMERG, "BS: %s state already executed.\n",
326 state->name);
327 break;
328 }
329
Julius Wernercd49cce2019-03-05 16:53:33 -0800330 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800331 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
332 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500333
Aaron Durbin15c671e2013-05-06 10:52:24 -0500334 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500335
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500336 bs_sample_time(state);
337
Aaron Durbin0748d302013-05-06 10:50:19 -0500338 bs_call_callbacks(state, current_phase.seq);
339 /* Update the current sequence so that any calls to block the
340 * current state from the run_state() function will place a
341 * block on the correct phase. */
342 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500343
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500344 bs_sample_time(state);
345
Duncan Lauriecb73a842013-06-10 10:41:04 -0700346 post_code(state->post_code);
347
Aaron Durbin0748d302013-05-06 10:50:19 -0500348 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500349
Julius Wernercd49cce2019-03-05 16:53:33 -0800350 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800351 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
352 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500353
354 bs_sample_time(state);
355
Aaron Durbin0748d302013-05-06 10:50:19 -0500356 bs_call_callbacks(state, current_phase.seq);
357
Julius Wernercd49cce2019-03-05 16:53:33 -0800358 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800359 printk(BIOS_DEBUG,
360 "----------------------------------------\n");
361
Aaron Durbin0748d302013-05-06 10:50:19 -0500362 /* Update the current phase with new state id and sequence. */
363 current_phase.state_id = next_id;
364 current_phase.seq = BS_ON_ENTRY;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500365
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500366 bs_sample_time(state);
367
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500368 state->complete = 1;
369 }
370}
371
372static int boot_state_sched_callback(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800373 struct boot_state_callback *bscb,
374 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500375{
376 if (state->complete) {
377 printk(BIOS_WARNING,
378 "Tried to schedule callback on completed state %s.\n",
379 state->name);
380
381 return -1;
382 }
383
Aaron Durbin0748d302013-05-06 10:50:19 -0500384 bscb->next = state->phases[seq].callbacks;
385 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500386
387 return 0;
388}
389
390int boot_state_sched_on_entry(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800391 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500392{
393 struct boot_state *state = &boot_states[state_id];
394
395 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
396}
397
398int boot_state_sched_on_exit(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800399 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500400{
401 struct boot_state *state = &boot_states[state_id];
402
403 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
404}
Maciej Pijankaea921852009-10-27 14:29:29 +0000405
Aaron Durbina4feddf2013-04-24 16:12:52 -0500406static void boot_state_schedule_static_entries(void)
407{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500408 extern struct boot_state_init_entry *_bs_init_begin[];
409 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500410
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500411 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
412 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500413
Aaron Durbina4feddf2013-04-24 16:12:52 -0500414 if (cur->when == BS_ON_ENTRY)
415 boot_state_sched_on_entry(&cur->bscb, cur->state);
416 else
417 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500418 }
419}
420
Stefan Reinauer6adef082013-05-09 16:30:06 -0700421void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000422{
Nico Hubere0ed9022016-10-07 12:58:17 +0200423 /*
424 * We can generally jump between C and Ada code back and forth
425 * without trouble. But since we don't have an Ada main() we
426 * have to do some Ada package initializations that GNAT would
427 * do there. This has to be done before calling any Ada code.
428 *
429 * The package initializations should not have any dependen-
430 * cies on C code. So we can call them here early, and don't
431 * have to worry at which point we can start to use Ada.
432 */
433 ramstage_adainit();
434
Duncan Lauriea5181512015-06-04 08:42:48 -0700435 /* TODO: Understand why this is here and move to arch/platform code. */
436 /* For MMIO UART this needs to be called before any other printk. */
Kyösti Mälkki7336f972020-06-08 06:05:03 +0300437 if (ENV_X86)
Duncan Lauriea5181512015-06-04 08:42:48 -0700438 init_timer();
439
Aaron Durbinbe347972015-04-21 11:57:18 -0500440 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
441 * it is the very first thing done in ramstage.*/
442 console_init();
Aaron Durbinbe347972015-04-21 11:57:18 -0500443 post_code(POST_CONSOLE_READY);
444
Asami Doi38a12862019-08-22 11:15:12 +0900445 exception_init();
446
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500447 /*
Subrata Banik91160e12019-01-29 17:16:15 +0530448 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300449 * the cbmem infrastructure being around. Explicitly recover it.
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500450 */
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300451 cbmem_initialize();
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500452
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300453 timestamp_add_now(TS_START_RAMSTAGE);
Subrata Banik8d2b0dc2021-05-05 19:46:09 +0530454 post_code(POST_ENTRY_HARDWAREMAIN);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000455
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300456 /* Handoff sleep type from romstage. */
Kyösti Mälkkib8c7ea02020-11-17 16:41:38 +0200457 acpi_is_wakeup_s3();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500458 threads_initialize();
459
Kyösti Mälkkie0183d62020-06-17 13:45:16 +0300460 /* Initialise GNVS early. */
Kyösti Mälkki661ad462020-12-29 06:26:21 +0200461 if (CONFIG(ACPI_SOC_NVS))
Kyösti Mälkki37eb24b2021-01-11 18:40:37 +0200462 acpi_create_gnvs();
Kyösti Mälkkie0183d62020-06-17 13:45:16 +0300463
Aaron Durbina4feddf2013-04-24 16:12:52 -0500464 /* Schedule the static boot state entries. */
465 boot_state_schedule_static_entries();
466
Aaron Durbin0748d302013-05-06 10:50:19 -0500467 bs_walk_state_machine();
468
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500469 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000470}
471
Aaron Durbin0748d302013-05-06 10:50:19 -0500472
473int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
474{
475 struct boot_phase *bp;
476
477 /* Blocking a previously ran state is not appropriate. */
478 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800479 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500480 printk(BIOS_WARNING,
481 "BS: Completed state (%d, %d) block attempted.\n",
482 state, seq);
483 return -1;
484 }
485
486 bp = &boot_states[state].phases[seq];
487 bp->blockers++;
488
489 return 0;
490}
491
492int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
493{
494 struct boot_phase *bp;
495
496 /* Blocking a previously ran state is not appropriate. */
497 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800498 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500499 printk(BIOS_WARNING,
500 "BS: Completed state (%d, %d) unblock attempted.\n",
501 state, seq);
502 return -1;
503 }
504
505 bp = &boot_states[state].phases[seq];
506
507 if (bp->blockers == 0) {
508 printk(BIOS_WARNING,
509 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
510 state, seq);
511 return -1;
512 }
513
514 bp->blockers--;
515
516 return 0;
517}
518
519void boot_state_current_block(void)
520{
521 boot_state_block(current_phase.state_id, current_phase.seq);
522}
523
524void boot_state_current_unblock(void)
525{
526 boot_state_unblock(current_phase.state_id, current_phase.seq);
527}