1 /* SPDX-License-Identifier: GPL-2.0-only */
5 * C Bootstrap code for the coreboot
10 #include <arch/exception.h>
11 #include <bootstate.h>
12 #include <console/console.h>
13 #include <console/post_codes.h>
14 #include <commonlib/helpers.h>
17 #include <device/device.h>
18 #include <device/pci.h>
21 #include <boot/tables.h>
22 #include <program_loading.h>
24 #include <timestamp.h>
27 static boot_state_t
bs_pre_device(void *arg
);
28 static boot_state_t
bs_dev_init_chips(void *arg
);
29 static boot_state_t
bs_dev_enumerate(void *arg
);
30 static boot_state_t
bs_dev_resources(void *arg
);
31 static boot_state_t
bs_dev_enable(void *arg
);
32 static boot_state_t
bs_dev_init(void *arg
);
33 static boot_state_t
bs_post_device(void *arg
);
34 static boot_state_t
bs_os_resume_check(void *arg
);
35 static boot_state_t
bs_os_resume(void *arg
);
36 static boot_state_t
bs_write_tables(void *arg
);
37 static boot_state_t
bs_payload_load(void *arg
);
38 static boot_state_t
bs_payload_boot(void *arg
);
40 /* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
41 * blocked from transitioning to the next (state,seq) pair. When the blockers
42 * field is 0 a transition may occur. */
44 struct boot_state_callback
*callbacks
;
52 struct boot_phase phases
[2];
53 boot_state_t (*run_state
)(void *arg
);
59 #define BS_INIT(state_, run_func_) \
63 .post_code = POST_ ## state_, \
64 .phases = { { NULL, 0 }, { NULL, 0 } }, \
65 .run_state = run_func_, \
69 #define BS_INIT_ENTRY(state_, run_func_) \
70 [state_] = BS_INIT(state_, run_func_)
72 static struct boot_state boot_states
[] = {
73 BS_INIT_ENTRY(BS_PRE_DEVICE
, bs_pre_device
),
74 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS
, bs_dev_init_chips
),
75 BS_INIT_ENTRY(BS_DEV_ENUMERATE
, bs_dev_enumerate
),
76 BS_INIT_ENTRY(BS_DEV_RESOURCES
, bs_dev_resources
),
77 BS_INIT_ENTRY(BS_DEV_ENABLE
, bs_dev_enable
),
78 BS_INIT_ENTRY(BS_DEV_INIT
, bs_dev_init
),
79 BS_INIT_ENTRY(BS_POST_DEVICE
, bs_post_device
),
80 BS_INIT_ENTRY(BS_OS_RESUME_CHECK
, bs_os_resume_check
),
81 BS_INIT_ENTRY(BS_OS_RESUME
, bs_os_resume
),
82 BS_INIT_ENTRY(BS_WRITE_TABLES
, bs_write_tables
),
83 BS_INIT_ENTRY(BS_PAYLOAD_LOAD
, bs_payload_load
),
84 BS_INIT_ENTRY(BS_PAYLOAD_BOOT
, bs_payload_boot
),
87 void __weak
arch_bootstate_coreboot_exit(void) { }
89 static boot_state_t
bs_pre_device(void *arg
)
91 return BS_DEV_INIT_CHIPS
;
94 static boot_state_t
bs_dev_init_chips(void *arg
)
96 timestamp_add_now(TS_DEVICE_ENUMERATE
);
98 /* Initialize chips early, they might disable unused devices. */
99 dev_initialize_chips();
101 return BS_DEV_ENUMERATE
;
104 static boot_state_t
bs_dev_enumerate(void *arg
)
106 /* Find the devices we don't have hard coded knowledge about. */
109 return BS_DEV_RESOURCES
;
112 static boot_state_t
bs_dev_resources(void *arg
)
114 timestamp_add_now(TS_DEVICE_CONFIGURE
);
116 /* Now compute and assign the bus resources. */
119 return BS_DEV_ENABLE
;
122 static boot_state_t
bs_dev_enable(void *arg
)
124 timestamp_add_now(TS_DEVICE_ENABLE
);
126 /* Now actually enable devices on the bus */
132 static boot_state_t
bs_dev_init(void *arg
)
134 timestamp_add_now(TS_DEVICE_INITIALIZE
);
136 /* And of course initialize devices on the bus */
139 return BS_POST_DEVICE
;
142 static boot_state_t
bs_post_device(void *arg
)
145 timestamp_add_now(TS_DEVICE_DONE
);
147 return BS_OS_RESUME_CHECK
;
150 static boot_state_t
bs_os_resume_check(void *arg
)
152 void *wake_vector
= NULL
;
154 if (CONFIG(HAVE_ACPI_RESUME
))
155 wake_vector
= acpi_find_wakeup_vector();
157 if (wake_vector
!= NULL
) {
158 boot_states
[BS_OS_RESUME
].arg
= wake_vector
;
162 timestamp_add_now(TS_CBMEM_POST
);
164 return BS_WRITE_TABLES
;
167 static boot_state_t
bs_os_resume(void *wake_vector
)
169 if (CONFIG(HAVE_ACPI_RESUME
)) {
170 arch_bootstate_coreboot_exit();
171 acpi_resume(wake_vector
);
172 /* We will not come back. */
174 die("Failed OS resume\n");
177 static boot_state_t
bs_write_tables(void *arg
)
179 timestamp_add_now(TS_WRITE_TABLES
);
181 /* Now that we have collected all of our information
182 * write our configuration tables.
186 timestamp_add_now(TS_FINALIZE_CHIPS
);
187 dev_finalize_chips();
189 return BS_PAYLOAD_LOAD
;
192 static boot_state_t
bs_payload_load(void *arg
)
196 return BS_PAYLOAD_BOOT
;
199 static boot_state_t
bs_payload_boot(void *arg
)
201 arch_bootstate_coreboot_exit();
204 printk(BIOS_EMERG
, "Boot failed\n");
205 /* Returning from this state will fail because the following signals
206 * return to a completed state. */
207 return BS_PAYLOAD_BOOT
;
211 * Typically a state will take 4 time samples:
212 * 1. Before state entry callbacks
213 * 2. After state entry callbacks / Before state function.
214 * 3. After state function / Before state exit callbacks.
215 * 4. After state exit callbacks.
217 static void bs_sample_time(struct boot_state
*state
)
219 static const char *const sample_id
[] = { "entry", "run", "exit" };
220 static struct mono_time previous_sample
;
221 struct mono_time this_sample
;
224 if (!CONFIG(HAVE_MONOTONIC_TIMER
))
227 console
= console_time_get_and_reset();
228 timer_monotonic_get(&this_sample
);
229 state
->num_samples
++;
231 int i
= state
->num_samples
- 2;
232 if ((i
>= 0) && (i
< ARRAY_SIZE(sample_id
))) {
233 long execution
= mono_time_diff_microseconds(&previous_sample
, &this_sample
);
235 /* Report with millisecond precision to reduce log diffs. */
236 execution
= DIV_ROUND_CLOSEST(execution
, USECS_PER_MSEC
);
237 console
= DIV_ROUND_CLOSEST(console
, USECS_PER_MSEC
);
239 printk(BIOS_DEBUG
, "BS: %s %s times (exec / console): %ld / %ld ms\n",
240 state
->name
, sample_id
[i
], execution
- console
, console
);
241 /* Reset again to ignore printk() time above. */
242 console_time_get_and_reset();
245 timer_monotonic_get(&previous_sample
);
248 #if CONFIG(TIMER_QUEUE)
249 static void bs_run_timers(int drain
)
251 /* Drain all timer callbacks until none are left, if directed.
252 * Otherwise run the timers only once. */
259 static void bs_run_timers(int drain
) {}
262 static void bs_call_callbacks(struct boot_state
*state
,
263 boot_state_sequence_t seq
)
265 struct boot_phase
*phase
= &state
->phases
[seq
];
268 if (phase
->callbacks
!= NULL
) {
269 struct boot_state_callback
*bscb
;
271 /* Remove the first callback. */
272 bscb
= phase
->callbacks
;
273 phase
->callbacks
= bscb
->next
;
276 #if CONFIG(DEBUG_BOOT_STATE)
277 printk(BIOS_DEBUG
, "BS: callback (%p) @ %s.\n",
278 bscb
, bscb
->location
);
280 bscb
->callback(bscb
->arg
);
284 /* All callbacks are complete and there are no blockers for
285 * this state. Therefore, this part of the state is complete. */
286 if (!phase
->blockers
)
289 /* Something is blocking this state from transitioning. As
290 * there are no more callbacks a pending timer needs to be
291 * ran to unblock the state. */
296 /* Keep track of the current state. */
297 static struct state_tracker
{
298 boot_state_t state_id
;
299 boot_state_sequence_t seq
;
301 .state_id
= BS_PRE_DEVICE
,
305 static void bs_walk_state_machine(void)
309 struct boot_state
*state
;
310 boot_state_t next_id
;
312 state
= &boot_states
[current_phase
.state_id
];
314 if (state
->complete
) {
315 printk(BIOS_EMERG
, "BS: %s state already executed.\n",
320 if (CONFIG(DEBUG_BOOT_STATE
))
321 printk(BIOS_DEBUG
, "BS: Entering %s state.\n",
326 bs_sample_time(state
);
328 bs_call_callbacks(state
, current_phase
.seq
);
329 /* Update the current sequence so that any calls to block the
330 * current state from the run_state() function will place a
331 * block on the correct phase. */
332 current_phase
.seq
= BS_ON_EXIT
;
334 bs_sample_time(state
);
336 post_code(state
->post_code
);
338 next_id
= state
->run_state(state
->arg
);
340 if (CONFIG(DEBUG_BOOT_STATE
))
341 printk(BIOS_DEBUG
, "BS: Exiting %s state.\n",
344 bs_sample_time(state
);
346 bs_call_callbacks(state
, current_phase
.seq
);
348 if (CONFIG(DEBUG_BOOT_STATE
))
350 "----------------------------------------\n");
352 /* Update the current phase with new state id and sequence. */
353 current_phase
.state_id
= next_id
;
354 current_phase
.seq
= BS_ON_ENTRY
;
356 bs_sample_time(state
);
362 static int boot_state_sched_callback(struct boot_state
*state
,
363 struct boot_state_callback
*bscb
,
364 boot_state_sequence_t seq
)
366 if (state
->complete
) {
368 "Tried to schedule callback on completed state %s.\n",
374 bscb
->next
= state
->phases
[seq
].callbacks
;
375 state
->phases
[seq
].callbacks
= bscb
;
380 int boot_state_sched_on_entry(struct boot_state_callback
*bscb
,
381 boot_state_t state_id
)
383 struct boot_state
*state
= &boot_states
[state_id
];
385 return boot_state_sched_callback(state
, bscb
, BS_ON_ENTRY
);
388 int boot_state_sched_on_exit(struct boot_state_callback
*bscb
,
389 boot_state_t state_id
)
391 struct boot_state
*state
= &boot_states
[state_id
];
393 return boot_state_sched_callback(state
, bscb
, BS_ON_EXIT
);
396 static void boot_state_schedule_static_entries(void)
398 extern struct boot_state_init_entry
*_bs_init_begin
[];
399 struct boot_state_init_entry
**slot
;
401 for (slot
= &_bs_init_begin
[0]; *slot
!= NULL
; slot
++) {
402 struct boot_state_init_entry
*cur
= *slot
;
404 if (cur
->when
== BS_ON_ENTRY
)
405 boot_state_sched_on_entry(&cur
->bscb
, cur
->state
);
407 boot_state_sched_on_exit(&cur
->bscb
, cur
->state
);
414 * We can generally jump between C and Ada code back and forth
415 * without trouble. But since we don't have an Ada main() we
416 * have to do some Ada package initializations that GNAT would
417 * do there. This has to be done before calling any Ada code.
419 * The package initializations should not have any dependen-
420 * cies on C code. So we can call them here early, and don't
421 * have to worry at which point we can start to use Ada.
425 /* TODO: Understand why this is here and move to arch/platform code. */
426 /* For MMIO UART this needs to be called before any other printk. */
430 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
431 * it is the very first thing done in ramstage.*/
433 post_code(POST_CONSOLE_READY
);
438 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
439 * the cbmem infrastructure being around. Explicitly recover it.
443 timestamp_add_now(TS_START_RAMSTAGE
);
444 post_code(POST_ENTRY_RAMSTAGE
);
446 /* Handoff sleep type from romstage. */
448 threads_initialize();
450 /* Schedule the static boot state entries. */
451 boot_state_schedule_static_entries();
453 bs_walk_state_machine();
455 die("Boot state machine failure.\n");
459 int boot_state_block(boot_state_t state
, boot_state_sequence_t seq
)
461 struct boot_phase
*bp
;
463 /* Blocking a previously ran state is not appropriate. */
464 if (current_phase
.state_id
> state
||
465 (current_phase
.state_id
== state
&& current_phase
.seq
> seq
)) {
467 "BS: Completed state (%d, %d) block attempted.\n",
472 bp
= &boot_states
[state
].phases
[seq
];
478 int boot_state_unblock(boot_state_t state
, boot_state_sequence_t seq
)
480 struct boot_phase
*bp
;
482 /* Blocking a previously ran state is not appropriate. */
483 if (current_phase
.state_id
> state
||
484 (current_phase
.state_id
== state
&& current_phase
.seq
> seq
)) {
486 "BS: Completed state (%d, %d) unblock attempted.\n",
491 bp
= &boot_states
[state
].phases
[seq
];
493 if (bp
->blockers
== 0) {
495 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
505 void boot_state_current_block(void)
507 boot_state_block(current_phase
.state_id
, current_phase
.seq
);
510 void boot_state_current_unblock(void)
512 boot_state_unblock(current_phase
.state_id
, current_phase
.seq
);