blob: a4c154e7a809291103fa95945f90c478f2f9a956 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Aaron Durbin7e35efa2013-04-24 15:14:01 -05002 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Eric Biederman8ca8d762003-04-22 19:02:15 +000014 */
15
16
17/*
Stefan Reinauerf8ee1802008-01-18 15:08:58 +000018 * C Bootstrap code for the coreboot
Eric Biederman8ca8d762003-04-22 19:02:15 +000019 */
20
Nico Hubere0ed9022016-10-07 12:58:17 +020021#include <adainit.h>
Julius Werner85620db2013-11-13 18:22:15 -080022#include <arch/exception.h>
Aaron Durbin7e35efa2013-04-24 15:14:01 -050023#include <bootstate.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000024#include <console/console.h>
Duncan Lauriecb73a842013-06-10 10:41:04 -070025#include <console/post_codes.h>
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -050026#include <cbmem.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000027#include <version.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000028#include <device/device.h>
29#include <device/pci.h>
Eric Biederman9b4336c2003-07-19 04:28:22 +000030#include <delay.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000031#include <stdlib.h>
Stefan 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 Durbin6b0fb0d2013-04-26 20:54:16 -050054/*
55 * Typically a state will take 4 time samples:
56 * 1. Before state entry callbacks
57 * 2. After state entry callbacks / Before state function.
58 * 3. After state function / Before state exit callbacks.
59 * 4. After state exit callbacks.
60 */
61#define MAX_TIME_SAMPLES 4
62struct boot_state_times {
63 int num_samples;
64 struct mono_time samples[MAX_TIME_SAMPLES];
65};
66
Aaron Durbin0748d302013-05-06 10:50:19 -050067/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
68 * blocked from transitioning to the next (state,seq) pair. When the blockers
69 * field is 0 a transition may occur. */
70struct boot_phase {
71 struct boot_state_callback *callbacks;
72 int blockers;
73};
74
Aaron Durbin7e35efa2013-04-24 15:14:01 -050075struct boot_state {
76 const char *name;
77 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070078 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050079 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050080 boot_state_t (*run_state)(void *arg);
81 void *arg;
Aaron Durbin8fc41e12013-04-29 23:22:01 -050082 int complete : 1;
Julius Wernercd49cce2019-03-05 16:53:33 -080083#if CONFIG(HAVE_MONOTONIC_TIMER)
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050084 struct boot_state_times times;
85#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -050086};
87
Aaron Durbin15c671e2013-05-06 10:52:24 -050088#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050089 { \
90 .name = #state_, \
91 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070092 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050093 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050094 .run_state = run_func_, \
95 .arg = NULL, \
96 .complete = 0, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050097 }
98#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -050099 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500100
101static struct boot_state boot_states[] = {
102 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
103 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
104 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
105 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
David Hendrickscca68592013-06-20 14:08:27 -0700106 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500107 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
108 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500109 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -0500110 BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
111 BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
112 BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
113 BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500114};
115
Aaron Durbin64031672018-04-21 14:45:32 -0600116void __weak arch_bootstate_coreboot_exit(void) { }
Aaron Durbin16bd2672016-12-07 11:58:20 -0600117
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500118static boot_state_t bs_pre_device(void *arg)
119{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500120 return BS_DEV_INIT_CHIPS;
121}
122
123static boot_state_t bs_dev_init_chips(void *arg)
124{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300125 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500126
127 /* Initialize chips early, they might disable unused devices. */
128 dev_initialize_chips();
129
130 return BS_DEV_ENUMERATE;
131}
132
133static boot_state_t bs_dev_enumerate(void *arg)
134{
135 /* Find the devices we don't have hard coded knowledge about. */
136 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500137
138 return BS_DEV_RESOURCES;
139}
140
141static boot_state_t bs_dev_resources(void *arg)
142{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300143 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700144
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500145 /* Now compute and assign the bus resources. */
146 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500147
148 return BS_DEV_ENABLE;
149}
150
David Hendrickscca68592013-06-20 14:08:27 -0700151static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500152{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300153 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700154
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500155 /* Now actually enable devices on the bus */
156 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500157
158 return BS_DEV_INIT;
159}
160
161static boot_state_t bs_dev_init(void *arg)
162{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300163 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700164
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500165 /* And of course initialize devices on the bus */
166 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500167
168 return BS_POST_DEVICE;
169}
170
171static boot_state_t bs_post_device(void *arg)
172{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600173 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300174 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500175
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500176 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500177}
178
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500179static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500180{
Julius Wernercd49cce2019-03-05 16:53:33 -0800181#if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500182 void *wake_vector;
183
184 wake_vector = acpi_find_wakeup_vector();
185
186 if (wake_vector != NULL) {
187 boot_states[BS_OS_RESUME].arg = wake_vector;
188 return BS_OS_RESUME;
189 }
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500190#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500191 timestamp_add_now(TS_CBMEM_POST);
192
193 return BS_WRITE_TABLES;
194}
195
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500196static boot_state_t bs_os_resume(void *wake_vector)
197{
Julius Wernercd49cce2019-03-05 16:53:33 -0800198#if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin16bd2672016-12-07 11:58:20 -0600199 arch_bootstate_coreboot_exit();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500200 acpi_resume(wake_vector);
201#endif
202 return BS_WRITE_TABLES;
203}
204
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500205static boot_state_t bs_write_tables(void *arg)
206{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500207 timestamp_add_now(TS_WRITE_TABLES);
208
209 /* Now that we have collected all of our information
210 * write our configuration tables.
211 */
212 write_tables();
213
Paul Menzel00563bf2016-12-05 23:06:37 +0100214 timestamp_add_now(TS_FINALIZE_CHIPS);
Marc Jones2a58ecd2013-10-29 17:32:00 -0600215 dev_finalize_chips();
216
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500217 return BS_PAYLOAD_LOAD;
218}
219
220static boot_state_t bs_payload_load(void *arg)
221{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500222 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500223
224 return BS_PAYLOAD_BOOT;
225}
226
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600227static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500228{
Aaron Durbin16bd2672016-12-07 11:58:20 -0600229 arch_bootstate_coreboot_exit();
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500230 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500231
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200232 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500233 /* Returning from this state will fail because the following signals
234 * return to a completed state. */
235 return BS_PAYLOAD_BOOT;
236}
237
Julius Wernercd49cce2019-03-05 16:53:33 -0800238#if CONFIG(HAVE_MONOTONIC_TIMER)
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500239static void bs_sample_time(struct boot_state *state)
240{
241 struct mono_time *mt;
242
243 mt = &state->times.samples[state->times.num_samples];
244 timer_monotonic_get(mt);
245 state->times.num_samples++;
246}
247
248static void bs_report_time(struct boot_state *state)
249{
Aaron Durbinad5a9092014-09-24 09:48:47 -0500250 long entry_time;
251 long run_time;
252 long exit_time;
253 struct mono_time *samples = &state->times.samples[0];
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500254
Aaron Durbinad5a9092014-09-24 09:48:47 -0500255 entry_time = mono_time_diff_microseconds(&samples[0], &samples[1]);
256 run_time = mono_time_diff_microseconds(&samples[1], &samples[2]);
257 exit_time = mono_time_diff_microseconds(&samples[2], &samples[3]);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500258
259 printk(BIOS_DEBUG, "BS: %s times (us): entry %ld run %ld exit %ld\n",
Aaron Durbinad5a9092014-09-24 09:48:47 -0500260 state->name, entry_time, run_time, exit_time);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500261}
262#else
263static inline void bs_sample_time(struct boot_state *state) {}
264static inline void bs_report_time(struct boot_state *state) {}
265#endif
266
Julius Wernercd49cce2019-03-05 16:53:33 -0800267#if CONFIG(TIMER_QUEUE)
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500268static void bs_run_timers(int drain)
269{
270 /* Drain all timer callbacks until none are left, if directed.
271 * Otherwise run the timers only once. */
272 do {
273 if (!timers_run())
274 break;
275 } while (drain);
276}
277#else
278static void bs_run_timers(int drain) {}
279#endif
280
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500281static void bs_call_callbacks(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800282 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500283{
Aaron Durbin0748d302013-05-06 10:50:19 -0500284 struct boot_phase *phase = &state->phases[seq];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500285
Aaron Durbin0748d302013-05-06 10:50:19 -0500286 while (1) {
287 if (phase->callbacks != NULL) {
288 struct boot_state_callback *bscb;
289
290 /* Remove the first callback. */
291 bscb = phase->callbacks;
292 phase->callbacks = bscb->next;
293 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500294
Julius Wernercd49cce2019-03-05 16:53:33 -0800295#if CONFIG(DEBUG_BOOT_STATE)
Lee Leahy10605352016-02-14 17:01:40 -0800296 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
297 bscb, bscb->location);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500298#endif
Aaron Durbin0748d302013-05-06 10:50:19 -0500299 bscb->callback(bscb->arg);
Aaron Durbin0748d302013-05-06 10:50:19 -0500300 continue;
301 }
302
303 /* All callbacks are complete and there are no blockers for
304 * this state. Therefore, this part of the state is complete. */
305 if (!phase->blockers)
306 break;
307
308 /* Something is blocking this state from transitioning. As
309 * there are no more callbacks a pending timer needs to be
310 * ran to unblock the state. */
311 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500312 }
313}
314
Aaron Durbin0748d302013-05-06 10:50:19 -0500315/* Keep track of the current state. */
316static struct state_tracker {
317 boot_state_t state_id;
318 boot_state_sequence_t seq;
319} current_phase = {
320 .state_id = BS_PRE_DEVICE,
321 .seq = BS_ON_ENTRY,
322};
323
324static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500325{
326
327 while (1) {
328 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500329 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500330
Aaron Durbin0748d302013-05-06 10:50:19 -0500331 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500332
333 if (state->complete) {
334 printk(BIOS_EMERG, "BS: %s state already executed.\n",
335 state->name);
336 break;
337 }
338
Julius Wernercd49cce2019-03-05 16:53:33 -0800339 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800340 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
341 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500342
Aaron Durbin15c671e2013-05-06 10:52:24 -0500343 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500344
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500345 bs_sample_time(state);
346
Aaron Durbin0748d302013-05-06 10:50:19 -0500347 bs_call_callbacks(state, current_phase.seq);
348 /* Update the current sequence so that any calls to block the
349 * current state from the run_state() function will place a
350 * block on the correct phase. */
351 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500352
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500353 bs_sample_time(state);
354
Duncan Lauriecb73a842013-06-10 10:41:04 -0700355 post_code(state->post_code);
356
Aaron Durbin0748d302013-05-06 10:50:19 -0500357 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500358
Julius Wernercd49cce2019-03-05 16:53:33 -0800359 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800360 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
361 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500362
363 bs_sample_time(state);
364
Aaron Durbin0748d302013-05-06 10:50:19 -0500365 bs_call_callbacks(state, current_phase.seq);
366
Julius Wernercd49cce2019-03-05 16:53:33 -0800367 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800368 printk(BIOS_DEBUG,
369 "----------------------------------------\n");
370
Aaron Durbin0748d302013-05-06 10:50:19 -0500371 /* Update the current phase with new state id and sequence. */
372 current_phase.state_id = next_id;
373 current_phase.seq = BS_ON_ENTRY;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500374
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500375 bs_sample_time(state);
376
377 bs_report_time(state);
378
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500379 state->complete = 1;
380 }
381}
382
383static int boot_state_sched_callback(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800384 struct boot_state_callback *bscb,
385 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500386{
387 if (state->complete) {
388 printk(BIOS_WARNING,
389 "Tried to schedule callback on completed state %s.\n",
390 state->name);
391
392 return -1;
393 }
394
Aaron Durbin0748d302013-05-06 10:50:19 -0500395 bscb->next = state->phases[seq].callbacks;
396 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500397
398 return 0;
399}
400
401int boot_state_sched_on_entry(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_ENTRY);
407}
408
409int boot_state_sched_on_exit(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800410 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500411{
412 struct boot_state *state = &boot_states[state_id];
413
414 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
415}
Maciej Pijankaea921852009-10-27 14:29:29 +0000416
Aaron Durbina4feddf2013-04-24 16:12:52 -0500417static void boot_state_schedule_static_entries(void)
418{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500419 extern struct boot_state_init_entry *_bs_init_begin[];
420 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500421
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500422 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
423 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500424
Aaron Durbina4feddf2013-04-24 16:12:52 -0500425 if (cur->when == BS_ON_ENTRY)
426 boot_state_sched_on_entry(&cur->bscb, cur->state);
427 else
428 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500429 }
430}
431
Stefan Reinauer6adef082013-05-09 16:30:06 -0700432void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000433{
Nico Hubere0ed9022016-10-07 12:58:17 +0200434 /*
435 * We can generally jump between C and Ada code back and forth
436 * without trouble. But since we don't have an Ada main() we
437 * have to do some Ada package initializations that GNAT would
438 * do there. This has to be done before calling any Ada code.
439 *
440 * The package initializations should not have any dependen-
441 * cies on C code. So we can call them here early, and don't
442 * have to worry at which point we can start to use Ada.
443 */
444 ramstage_adainit();
445
Duncan Lauriea5181512015-06-04 08:42:48 -0700446 /* TODO: Understand why this is here and move to arch/platform code. */
447 /* For MMIO UART this needs to be called before any other printk. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800448 if (CONFIG(ARCH_X86))
Duncan Lauriea5181512015-06-04 08:42:48 -0700449 init_timer();
450
Aaron Durbinbe347972015-04-21 11:57:18 -0500451 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
452 * it is the very first thing done in ramstage.*/
453 console_init();
Aaron Durbinbe347972015-04-21 11:57:18 -0500454 post_code(POST_CONSOLE_READY);
455
Asami Doi38a12862019-08-22 11:15:12 +0900456 exception_init();
457
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500458 /*
Subrata Banik91160e12019-01-29 17:16:15 +0530459 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300460 * the cbmem infrastructure being around. Explicitly recover it.
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500461 */
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300462 cbmem_initialize();
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500463
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300464 /* Record current time, try to locate timestamps in CBMEM. */
Stefan Reinauer3a6550d2013-08-01 13:31:44 -0700465 timestamp_init(timestamp_get());
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300466
467 timestamp_add_now(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000468 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000469
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300470 /* Handoff sleep type from romstage. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800471#if CONFIG(HAVE_ACPI_RESUME)
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300472 acpi_is_wakeup();
473#endif
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500474 threads_initialize();
475
Aaron Durbina4feddf2013-04-24 16:12:52 -0500476 /* Schedule the static boot state entries. */
477 boot_state_schedule_static_entries();
478
Aaron Durbin0748d302013-05-06 10:50:19 -0500479 bs_walk_state_machine();
480
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500481 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000482}
483
Aaron Durbin0748d302013-05-06 10:50:19 -0500484
485int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
486{
487 struct boot_phase *bp;
488
489 /* Blocking a previously ran state is not appropriate. */
490 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800491 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500492 printk(BIOS_WARNING,
493 "BS: Completed state (%d, %d) block attempted.\n",
494 state, seq);
495 return -1;
496 }
497
498 bp = &boot_states[state].phases[seq];
499 bp->blockers++;
500
501 return 0;
502}
503
504int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
505{
506 struct boot_phase *bp;
507
508 /* Blocking a previously ran state is not appropriate. */
509 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800510 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500511 printk(BIOS_WARNING,
512 "BS: Completed state (%d, %d) unblock attempted.\n",
513 state, seq);
514 return -1;
515 }
516
517 bp = &boot_states[state].phases[seq];
518
519 if (bp->blockers == 0) {
520 printk(BIOS_WARNING,
521 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
522 state, seq);
523 return -1;
524 }
525
526 bp->blockers--;
527
528 return 0;
529}
530
531void boot_state_current_block(void)
532{
533 boot_state_block(current_phase.state_id, current_phase.seq);
534}
535
536void boot_state_current_unblock(void)
537{
538 boot_state_unblock(current_phase.state_id, current_phase.seq);
539}