xref: /aosp_15_r20/external/coreboot/src/lib/hardwaremain.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 
4 /*
5  * C Bootstrap code for the coreboot
6  */
7 
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 
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);
39 
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. */
43 struct boot_phase {
44 	struct boot_state_callback *callbacks;
45 	int blockers;
46 };
47 
48 struct boot_state {
49 	const char *name;
50 	boot_state_t id;
51 	u8 post_code;
52 	struct boot_phase phases[2];
53 	boot_state_t (*run_state)(void *arg);
54 	void *arg;
55 	int num_samples;
56 	bool complete;
57 };
58 
59 #define BS_INIT(state_, run_func_)				\
60 	{							\
61 		.name = #state_,				\
62 		.id = state_,					\
63 		.post_code = POSTCODE_ ## state_,		\
64 		.phases = { { NULL, 0 }, { NULL, 0 } },		\
65 		.run_state = run_func_,				\
66 		.arg = NULL,					\
67 		.complete = false,				\
68 	}
69 #define BS_INIT_ENTRY(state_, run_func_)	\
70 	[state_] = BS_INIT(state_, run_func_)
71 
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),
85 };
86 
arch_bootstate_coreboot_exit(void)87 void __weak arch_bootstate_coreboot_exit(void) { }
88 
bs_pre_device(void * arg)89 static boot_state_t bs_pre_device(void *arg)
90 {
91 	return BS_DEV_INIT_CHIPS;
92 }
93 
bs_dev_init_chips(void * arg)94 static boot_state_t bs_dev_init_chips(void *arg)
95 {
96 	timestamp_add_now(TS_DEVICE_ENUMERATE);
97 
98 	/* Initialize chips early, they might disable unused devices. */
99 	dev_initialize_chips();
100 
101 	return BS_DEV_ENUMERATE;
102 }
103 
bs_dev_enumerate(void * arg)104 static boot_state_t bs_dev_enumerate(void *arg)
105 {
106 	/* Find the devices we don't have hard coded knowledge about. */
107 	dev_enumerate();
108 
109 	return BS_DEV_RESOURCES;
110 }
111 
bs_dev_resources(void * arg)112 static boot_state_t bs_dev_resources(void *arg)
113 {
114 	timestamp_add_now(TS_DEVICE_CONFIGURE);
115 
116 	/* Now compute and assign the bus resources. */
117 	dev_configure();
118 
119 	return BS_DEV_ENABLE;
120 }
121 
bs_dev_enable(void * arg)122 static boot_state_t bs_dev_enable(void *arg)
123 {
124 	timestamp_add_now(TS_DEVICE_ENABLE);
125 
126 	/* Now actually enable devices on the bus */
127 	dev_enable();
128 
129 	return BS_DEV_INIT;
130 }
131 
bs_dev_init(void * arg)132 static boot_state_t bs_dev_init(void *arg)
133 {
134 	timestamp_add_now(TS_DEVICE_INITIALIZE);
135 
136 	/* And of course initialize devices on the bus */
137 	dev_initialize();
138 
139 	return BS_POST_DEVICE;
140 }
141 
bs_post_device(void * arg)142 static boot_state_t bs_post_device(void *arg)
143 {
144 	dev_finalize();
145 	timestamp_add_now(TS_DEVICE_DONE);
146 
147 	return BS_OS_RESUME_CHECK;
148 }
149 
bs_os_resume_check(void * arg)150 static boot_state_t bs_os_resume_check(void *arg)
151 {
152 	void *wake_vector = NULL;
153 
154 	if (CONFIG(HAVE_ACPI_RESUME))
155 		wake_vector = acpi_find_wakeup_vector();
156 
157 	if (wake_vector != NULL) {
158 		boot_states[BS_OS_RESUME].arg = wake_vector;
159 		return BS_OS_RESUME;
160 	}
161 
162 	timestamp_add_now(TS_CBMEM_POST);
163 
164 	return BS_WRITE_TABLES;
165 }
166 
bs_os_resume(void * wake_vector)167 static boot_state_t bs_os_resume(void *wake_vector)
168 {
169 	if (CONFIG(HAVE_ACPI_RESUME)) {
170 		arch_bootstate_coreboot_exit();
171 		acpi_resume(wake_vector);
172 		/* We will not come back. */
173 	}
174 	die("Failed OS resume\n");
175 }
176 
bs_write_tables(void * arg)177 static boot_state_t bs_write_tables(void *arg)
178 {
179 	timestamp_add_now(TS_WRITE_TABLES);
180 
181 	/* Now that we have collected all of our information
182 	 * write our configuration tables.
183 	 */
184 	write_tables();
185 
186 	timestamp_add_now(TS_FINALIZE_CHIPS);
187 	dev_finalize_chips();
188 
189 	return BS_PAYLOAD_LOAD;
190 }
191 
bs_payload_load(void * arg)192 static boot_state_t bs_payload_load(void *arg)
193 {
194 	payload_load();
195 
196 	return BS_PAYLOAD_BOOT;
197 }
198 
bs_payload_boot(void * arg)199 static boot_state_t bs_payload_boot(void *arg)
200 {
201 	arch_bootstate_coreboot_exit();
202 	payload_run();
203 
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;
208 }
209 
210 /*
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.
216  */
bs_sample_time(struct boot_state * state)217 static void bs_sample_time(struct boot_state *state)
218 {
219 	static const char *const sample_id[] = { "entry", "run", "exit" };
220 	static struct mono_time previous_sample;
221 	struct mono_time this_sample;
222 	long console;
223 
224 	if (!CONFIG(HAVE_MONOTONIC_TIMER))
225 		return;
226 
227 	console = console_time_get_and_reset();
228 	timer_monotonic_get(&this_sample);
229 	state->num_samples++;
230 
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);
234 
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);
238 		if (execution) {
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();
243 		}
244 	}
245 	timer_monotonic_get(&previous_sample);
246 }
247 
248 #if CONFIG(TIMER_QUEUE)
bs_run_timers(int drain)249 static void bs_run_timers(int drain)
250 {
251 	/* Drain all timer callbacks until none are left, if directed.
252 	 * Otherwise run the timers only once. */
253 	do {
254 		if (!timers_run())
255 			break;
256 	} while (drain);
257 }
258 #else
bs_run_timers(int drain)259 static void bs_run_timers(int drain) {}
260 #endif
261 
bs_call_callbacks(struct boot_state * state,boot_state_sequence_t seq)262 static void bs_call_callbacks(struct boot_state *state,
263 			      boot_state_sequence_t seq)
264 {
265 	struct boot_phase *phase = &state->phases[seq];
266 	struct mono_time mt_start, mt_stop;
267 
268 	while (1) {
269 		if (phase->callbacks != NULL) {
270 			struct boot_state_callback *bscb;
271 
272 			/* Remove the first callback. */
273 			bscb = phase->callbacks;
274 			phase->callbacks = bscb->next;
275 			bscb->next = NULL;
276 
277 			if (CONFIG(DEBUG_BOOT_STATE)) {
278 				printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
279 					bscb, bscb_location(bscb));
280 				timer_monotonic_get(&mt_start);
281 			}
282 			bscb->callback(bscb->arg);
283 			if (CONFIG(DEBUG_BOOT_STATE)) {
284 				timer_monotonic_get(&mt_stop);
285 				printk(BIOS_DEBUG, "BS: callback (%p) @ %s (%lld ms).\n", bscb,
286 				       bscb_location(bscb),
287 				       mono_time_diff_microseconds(&mt_start, &mt_stop)
288 					       / USECS_PER_MSEC);
289 			}
290 
291 			bs_run_timers(0);
292 
293 			continue;
294 		}
295 
296 		/* All callbacks are complete and there are no blockers for
297 		 * this state. Therefore, this part of the state is complete. */
298 		if (!phase->blockers)
299 			break;
300 
301 		/* Something is blocking this state from transitioning. As
302 		 * there are no more callbacks a pending timer needs to be
303 		 * ran to unblock the state. */
304 		bs_run_timers(0);
305 	}
306 }
307 
308 /* Keep track of the current state. */
309 static struct state_tracker {
310 	boot_state_t state_id;
311 	boot_state_sequence_t seq;
312 } current_phase = {
313 	.state_id = BS_PRE_DEVICE,
314 	.seq = BS_ON_ENTRY,
315 };
316 
bs_walk_state_machine(void)317 static void bs_walk_state_machine(void)
318 {
319 	while (1) {
320 		struct boot_state *state;
321 		boot_state_t next_id;
322 
323 		state = &boot_states[current_phase.state_id];
324 
325 		if (state->complete) {
326 			printk(BIOS_EMERG, "BS: %s state already executed.\n",
327 			       state->name);
328 			break;
329 		}
330 
331 		if (CONFIG(DEBUG_BOOT_STATE))
332 			printk(BIOS_DEBUG, "BS: Entering %s state.\n",
333 				state->name);
334 
335 		bs_run_timers(0);
336 
337 		bs_sample_time(state);
338 
339 		bs_call_callbacks(state, current_phase.seq);
340 		/* Update the current sequence so that any calls to block the
341 		 * current state from the run_state() function will place a
342 		 * block on the correct phase. */
343 		current_phase.seq = BS_ON_EXIT;
344 
345 		bs_sample_time(state);
346 
347 		post_code(state->post_code);
348 
349 		next_id = state->run_state(state->arg);
350 
351 		if (CONFIG(DEBUG_BOOT_STATE))
352 			printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
353 			state->name);
354 
355 		bs_sample_time(state);
356 
357 		bs_run_timers(0);
358 
359 		bs_call_callbacks(state, current_phase.seq);
360 
361 		if (CONFIG(DEBUG_BOOT_STATE))
362 			printk(BIOS_DEBUG,
363 				"----------------------------------------\n");
364 
365 		/* Update the current phase with new state id and sequence. */
366 		current_phase.state_id = next_id;
367 		current_phase.seq = BS_ON_ENTRY;
368 
369 		bs_sample_time(state);
370 
371 		state->complete = true;
372 	}
373 }
374 
boot_state_sched_callback(struct boot_state * state,struct boot_state_callback * bscb,boot_state_sequence_t seq)375 static int boot_state_sched_callback(struct boot_state *state,
376 				     struct boot_state_callback *bscb,
377 				     boot_state_sequence_t seq)
378 {
379 	if (state->complete) {
380 		printk(BIOS_WARNING,
381 		       "Tried to schedule callback on completed state %s.\n",
382 		       state->name);
383 
384 		return -1;
385 	}
386 
387 	bscb->next = state->phases[seq].callbacks;
388 	state->phases[seq].callbacks = bscb;
389 
390 	return 0;
391 }
392 
boot_state_sched_on_entry(struct boot_state_callback * bscb,boot_state_t state_id)393 int boot_state_sched_on_entry(struct boot_state_callback *bscb,
394 			      boot_state_t state_id)
395 {
396 	struct boot_state *state = &boot_states[state_id];
397 
398 	return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
399 }
400 
boot_state_sched_on_exit(struct boot_state_callback * bscb,boot_state_t state_id)401 int boot_state_sched_on_exit(struct boot_state_callback *bscb,
402 			     boot_state_t state_id)
403 {
404 	struct boot_state *state = &boot_states[state_id];
405 
406 	return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
407 }
408 
boot_state_schedule_static_entries(void)409 static void boot_state_schedule_static_entries(void)
410 {
411 	extern struct boot_state_init_entry *_bs_init_begin[];
412 	struct boot_state_init_entry **slot;
413 
414 	for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
415 		struct boot_state_init_entry *cur = *slot;
416 
417 		if (cur->when == BS_ON_ENTRY)
418 			boot_state_sched_on_entry(&cur->bscb, cur->state);
419 		else
420 			boot_state_sched_on_exit(&cur->bscb, cur->state);
421 	}
422 }
423 
main(void)424 void main(void)
425 {
426 	/*
427 	 * We can generally jump between C and Ada code back and forth
428 	 * without trouble. But since we don't have an Ada main() we
429 	 * have to do some Ada package initializations that GNAT would
430 	 * do there. This has to be done before calling any Ada code.
431 	 *
432 	 * The package initializations should not have any dependen-
433 	 * cies on C code. So we can call them here early, and don't
434 	 * have to worry at which point we can start to use Ada.
435 	 */
436 	ramstage_adainit();
437 
438 	/* TODO: Understand why this is here and move to arch/platform code. */
439 	/* For MMIO UART this needs to be called before any other printk. */
440 	if (ENV_X86)
441 		init_timer();
442 
443 	/* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
444 	 * it is the very first thing done in ramstage.*/
445 	console_init();
446 	post_code(POSTCODE_CONSOLE_READY);
447 
448 	exception_init();
449 
450 	/*
451 	 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
452 	 * the cbmem infrastructure being around. Explicitly recover it.
453 	 */
454 	cbmem_initialize();
455 
456 	timestamp_add_now(TS_RAMSTAGE_START);
457 	post_code(POSTCODE_ENTRY_HARDWAREMAIN);
458 
459 	/* Handoff sleep type from romstage. */
460 	acpi_is_wakeup_s3();
461 
462 	/* Schedule the static boot state entries. */
463 	boot_state_schedule_static_entries();
464 
465 	bs_walk_state_machine();
466 
467 	die("Boot state machine failure.\n");
468 }
469 
470 
boot_state_block(boot_state_t state,boot_state_sequence_t seq)471 int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
472 {
473 	struct boot_phase *bp;
474 
475 	/* Blocking a previously ran state is not appropriate. */
476 	if (current_phase.state_id > state ||
477 	    (current_phase.state_id == state && current_phase.seq > seq)) {
478 		printk(BIOS_WARNING,
479 		       "BS: Completed state (%d, %d) block attempted.\n",
480 		       state, seq);
481 		return -1;
482 	}
483 
484 	bp = &boot_states[state].phases[seq];
485 	bp->blockers++;
486 
487 	return 0;
488 }
489 
boot_state_unblock(boot_state_t state,boot_state_sequence_t seq)490 int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
491 {
492 	struct boot_phase *bp;
493 
494 	/* Blocking a previously ran state is not appropriate. */
495 	if (current_phase.state_id > state ||
496 	    (current_phase.state_id == state && current_phase.seq > seq)) {
497 		printk(BIOS_WARNING,
498 		       "BS: Completed state (%d, %d) unblock attempted.\n",
499 		       state, seq);
500 		return -1;
501 	}
502 
503 	bp = &boot_states[state].phases[seq];
504 
505 	if (bp->blockers == 0) {
506 		printk(BIOS_WARNING,
507 		       "BS: Unblock attempted on non-blocked state (%d, %d).\n",
508 		       state, seq);
509 		return -1;
510 	}
511 
512 	bp->blockers--;
513 
514 	return 0;
515 }
516