soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / lib / hardwaremain.c
blob714452df20c1daab242e293e0e73ba709842d96f
1 /* SPDX-License-Identifier: GPL-2.0-only */
4 /*
5 * C Bootstrap code for the coreboot
6 */
8 #include <acpi/acpi.h>
9 #include <acpi/acpi_gnvs.h>
10 #include <adainit.h>
11 #include <arch/exception.h>
12 #include <boot/tables.h>
13 #include <bootstate.h>
14 #include <cbmem.h>
15 #include <commonlib/console/post_codes.h>
16 #include <commonlib/helpers.h>
17 #include <console/console.h>
18 #include <delay.h>
19 #include <device/device.h>
20 #include <device/pci.h>
21 #include <program_loading.h>
22 #include <thread.h>
23 #include <timer.h>
24 #include <timestamp.h>
25 #include <types.h>
26 #include <version.h>
28 static boot_state_t bs_pre_device(void *arg);
29 static boot_state_t bs_dev_init_chips(void *arg);
30 static boot_state_t bs_dev_enumerate(void *arg);
31 static boot_state_t bs_dev_resources(void *arg);
32 static boot_state_t bs_dev_enable(void *arg);
33 static boot_state_t bs_dev_init(void *arg);
34 static boot_state_t bs_post_device(void *arg);
35 static boot_state_t bs_os_resume_check(void *arg);
36 static boot_state_t bs_os_resume(void *arg);
37 static boot_state_t bs_write_tables(void *arg);
38 static boot_state_t bs_payload_load(void *arg);
39 static boot_state_t bs_payload_boot(void *arg);
41 /* 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. */
44 struct boot_phase {
45 struct boot_state_callback *callbacks;
46 int blockers;
49 struct boot_state {
50 const char *name;
51 boot_state_t id;
52 u8 post_code;
53 struct boot_phase phases[2];
54 boot_state_t (*run_state)(void *arg);
55 void *arg;
56 int num_samples;
57 bool complete;
60 #define BS_INIT(state_, run_func_) \
61 { \
62 .name = #state_, \
63 .id = state_, \
64 .post_code = POST_ ## state_, \
65 .phases = { { NULL, 0 }, { NULL, 0 } }, \
66 .run_state = run_func_, \
67 .arg = NULL, \
68 .complete = false, \
70 #define BS_INIT_ENTRY(state_, run_func_) \
71 [state_] = BS_INIT(state_, run_func_)
73 static 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),
78 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
79 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
80 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
81 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
82 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),
88 void __weak arch_bootstate_coreboot_exit(void) { }
90 static boot_state_t bs_pre_device(void *arg)
92 return BS_DEV_INIT_CHIPS;
95 static boot_state_t bs_dev_init_chips(void *arg)
97 timestamp_add_now(TS_DEVICE_ENUMERATE);
99 /* Initialize chips early, they might disable unused devices. */
100 dev_initialize_chips();
102 return BS_DEV_ENUMERATE;
105 static boot_state_t bs_dev_enumerate(void *arg)
107 /* Find the devices we don't have hard coded knowledge about. */
108 dev_enumerate();
110 return BS_DEV_RESOURCES;
113 static boot_state_t bs_dev_resources(void *arg)
115 timestamp_add_now(TS_DEVICE_CONFIGURE);
117 /* Now compute and assign the bus resources. */
118 dev_configure();
120 return BS_DEV_ENABLE;
123 static boot_state_t bs_dev_enable(void *arg)
125 timestamp_add_now(TS_DEVICE_ENABLE);
127 /* Now actually enable devices on the bus */
128 dev_enable();
130 return BS_DEV_INIT;
133 static boot_state_t bs_dev_init(void *arg)
135 timestamp_add_now(TS_DEVICE_INITIALIZE);
137 /* And of course initialize devices on the bus */
138 dev_initialize();
140 return BS_POST_DEVICE;
143 static boot_state_t bs_post_device(void *arg)
145 dev_finalize();
146 timestamp_add_now(TS_DEVICE_DONE);
148 return BS_OS_RESUME_CHECK;
151 static boot_state_t bs_os_resume_check(void *arg)
153 void *wake_vector = NULL;
155 if (CONFIG(HAVE_ACPI_RESUME))
156 wake_vector = acpi_find_wakeup_vector();
158 if (wake_vector != NULL) {
159 boot_states[BS_OS_RESUME].arg = wake_vector;
160 return BS_OS_RESUME;
163 timestamp_add_now(TS_CBMEM_POST);
165 return BS_WRITE_TABLES;
168 static boot_state_t bs_os_resume(void *wake_vector)
170 if (CONFIG(HAVE_ACPI_RESUME)) {
171 arch_bootstate_coreboot_exit();
172 acpi_resume(wake_vector);
173 /* We will not come back. */
175 die("Failed OS resume\n");
178 static boot_state_t bs_write_tables(void *arg)
180 timestamp_add_now(TS_WRITE_TABLES);
182 /* Now that we have collected all of our information
183 * write our configuration tables.
185 write_tables();
187 timestamp_add_now(TS_FINALIZE_CHIPS);
188 dev_finalize_chips();
190 return BS_PAYLOAD_LOAD;
193 static boot_state_t bs_payload_load(void *arg)
195 payload_load();
197 return BS_PAYLOAD_BOOT;
200 static boot_state_t bs_payload_boot(void *arg)
202 arch_bootstate_coreboot_exit();
203 payload_run();
205 printk(BIOS_EMERG, "Boot failed\n");
206 /* Returning from this state will fail because the following signals
207 * return to a completed state. */
208 return BS_PAYLOAD_BOOT;
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.
218 static void bs_sample_time(struct boot_state *state)
220 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;
225 if (!CONFIG(HAVE_MONOTONIC_TIMER))
226 return;
228 console = console_time_get_and_reset();
229 timer_monotonic_get(&this_sample);
230 state->num_samples++;
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);
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();
246 timer_monotonic_get(&previous_sample);
249 #if CONFIG(TIMER_QUEUE)
250 static void bs_run_timers(int drain)
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);
259 #else
260 static void bs_run_timers(int drain) {}
261 #endif
263 static void bs_call_callbacks(struct boot_state *state,
264 boot_state_sequence_t seq)
266 struct boot_phase *phase = &state->phases[seq];
267 struct mono_time mt_start, mt_stop;
269 while (1) {
270 if (phase->callbacks != NULL) {
271 struct boot_state_callback *bscb;
273 /* Remove the first callback. */
274 bscb = phase->callbacks;
275 phase->callbacks = bscb->next;
276 bscb->next = NULL;
278 if (CONFIG(DEBUG_BOOT_STATE)) {
279 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
280 bscb, bscb_location(bscb));
281 timer_monotonic_get(&mt_start);
283 bscb->callback(bscb->arg);
284 if (CONFIG(DEBUG_BOOT_STATE)) {
285 timer_monotonic_get(&mt_stop);
286 printk(BIOS_DEBUG, "BS: callback (%p) @ %s (%lld ms).\n", bscb,
287 bscb_location(bscb),
288 mono_time_diff_microseconds(&mt_start, &mt_stop)
289 / USECS_PER_MSEC);
292 bs_run_timers(0);
294 continue;
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;
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);
309 /* Keep track of the current state. */
310 static 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,
318 static void bs_walk_state_machine(void)
321 while (1) {
322 struct boot_state *state;
323 boot_state_t next_id;
325 state = &boot_states[current_phase.state_id];
327 if (state->complete) {
328 printk(BIOS_EMERG, "BS: %s state already executed.\n",
329 state->name);
330 break;
333 if (CONFIG(DEBUG_BOOT_STATE))
334 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
335 state->name);
337 bs_run_timers(0);
339 bs_sample_time(state);
341 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;
347 bs_sample_time(state);
349 post_code(state->post_code);
351 next_id = state->run_state(state->arg);
353 if (CONFIG(DEBUG_BOOT_STATE))
354 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
355 state->name);
357 bs_sample_time(state);
359 bs_run_timers(0);
361 bs_call_callbacks(state, current_phase.seq);
363 if (CONFIG(DEBUG_BOOT_STATE))
364 printk(BIOS_DEBUG,
365 "----------------------------------------\n");
367 /* Update the current phase with new state id and sequence. */
368 current_phase.state_id = next_id;
369 current_phase.seq = BS_ON_ENTRY;
371 bs_sample_time(state);
373 state->complete = true;
377 static int boot_state_sched_callback(struct boot_state *state,
378 struct boot_state_callback *bscb,
379 boot_state_sequence_t seq)
381 if (state->complete) {
382 printk(BIOS_WARNING,
383 "Tried to schedule callback on completed state %s.\n",
384 state->name);
386 return -1;
389 bscb->next = state->phases[seq].callbacks;
390 state->phases[seq].callbacks = bscb;
392 return 0;
395 int boot_state_sched_on_entry(struct boot_state_callback *bscb,
396 boot_state_t state_id)
398 struct boot_state *state = &boot_states[state_id];
400 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
403 int boot_state_sched_on_exit(struct boot_state_callback *bscb,
404 boot_state_t state_id)
406 struct boot_state *state = &boot_states[state_id];
408 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
411 static void boot_state_schedule_static_entries(void)
413 extern struct boot_state_init_entry *_bs_init_begin[];
414 struct boot_state_init_entry **slot;
416 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
417 struct boot_state_init_entry *cur = *slot;
419 if (cur->when == BS_ON_ENTRY)
420 boot_state_sched_on_entry(&cur->bscb, cur->state);
421 else
422 boot_state_sched_on_exit(&cur->bscb, cur->state);
426 void main(void)
429 * We can generally jump between C and Ada code back and forth
430 * without trouble. But since we don't have an Ada main() we
431 * have to do some Ada package initializations that GNAT would
432 * do there. This has to be done before calling any Ada code.
434 * The package initializations should not have any dependen-
435 * cies on C code. So we can call them here early, and don't
436 * have to worry at which point we can start to use Ada.
438 ramstage_adainit();
440 /* TODO: Understand why this is here and move to arch/platform code. */
441 /* For MMIO UART this needs to be called before any other printk. */
442 if (ENV_X86)
443 init_timer();
445 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
446 * it is the very first thing done in ramstage.*/
447 console_init();
448 post_code(POST_CONSOLE_READY);
450 exception_init();
453 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
454 * the cbmem infrastructure being around. Explicitly recover it.
456 cbmem_initialize();
458 timestamp_add_now(TS_RAMSTAGE_START);
459 post_code(POST_ENTRY_HARDWAREMAIN);
461 /* Handoff sleep type from romstage. */
462 acpi_is_wakeup_s3();
464 /* Schedule the static boot state entries. */
465 boot_state_schedule_static_entries();
467 bs_walk_state_machine();
469 die("Boot state machine failure.\n");
473 int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
475 struct boot_phase *bp;
477 /* Blocking a previously ran state is not appropriate. */
478 if (current_phase.state_id > state ||
479 (current_phase.state_id == state && current_phase.seq > seq)) {
480 printk(BIOS_WARNING,
481 "BS: Completed state (%d, %d) block attempted.\n",
482 state, seq);
483 return -1;
486 bp = &boot_states[state].phases[seq];
487 bp->blockers++;
489 return 0;
492 int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
494 struct boot_phase *bp;
496 /* Blocking a previously ran state is not appropriate. */
497 if (current_phase.state_id > state ||
498 (current_phase.state_id == state && current_phase.seq > seq)) {
499 printk(BIOS_WARNING,
500 "BS: Completed state (%d, %d) unblock attempted.\n",
501 state, seq);
502 return -1;
505 bp = &boot_states[state].phases[seq];
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;
514 bp->blockers--;
516 return 0;