hardwaremain.c 13.2 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2013 Google, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 */


/*
18
 * C Bootstrap code for the coreboot
19 20
 */

21
#include <adainit.h>
22
#include <arch/exception.h>
23
#include <bootstate.h>
Aaron Durbin's avatar
Aaron Durbin committed
24
#include <compiler.h>
25
#include <console/console.h>
26
#include <console/post_codes.h>
27
#include <cbmem.h>
28
#include <version.h>
29 30
#include <device/device.h>
#include <device/pci.h>
31
#include <delay.h>
32
#include <stdlib.h>
33
#include <reset.h>
34
#include <boot/tables.h>
35
#include <program_loading.h>
36
#include <lib.h>
37
#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)
38
#include <arch/acpi.h>
Stefan Reinauer's avatar
Stefan Reinauer committed
39
#endif
40
#include <timer.h>
41
#include <timestamp.h>
42
#include <thread.h>
43

44 45 46 47
static boot_state_t bs_pre_device(void *arg);
static boot_state_t bs_dev_init_chips(void *arg);
static boot_state_t bs_dev_enumerate(void *arg);
static boot_state_t bs_dev_resources(void *arg);
48
static boot_state_t bs_dev_enable(void *arg);
49 50
static boot_state_t bs_dev_init(void *arg);
static boot_state_t bs_post_device(void *arg);
51
static boot_state_t bs_os_resume_check(void *arg);
52 53 54 55 56
static boot_state_t bs_os_resume(void *arg);
static boot_state_t bs_write_tables(void *arg);
static boot_state_t bs_payload_load(void *arg);
static boot_state_t bs_payload_boot(void *arg);

57 58 59 60 61 62 63 64 65 66 67 68 69
/*
 * Typically a state will take 4 time samples:
 *   1. Before state entry callbacks
 *   2. After state entry callbacks / Before state function.
 *   3. After state function / Before state exit callbacks.
 *   4. After state exit callbacks.
 */
#define MAX_TIME_SAMPLES 4
struct boot_state_times {
	int num_samples;
	struct mono_time samples[MAX_TIME_SAMPLES];
};

70 71 72 73 74 75 76 77
/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
 * blocked from transitioning to the next (state,seq) pair. When the blockers
 * field is 0 a transition may occur. */
struct boot_phase {
	struct boot_state_callback *callbacks;
	int blockers;
};

78 79 80
struct boot_state {
	const char *name;
	boot_state_t id;
81
	u8 post_code;
82
	struct boot_phase phases[2];
83 84
	boot_state_t (*run_state)(void *arg);
	void *arg;
85
	int complete : 1;
86
#if IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)
87 88
	struct boot_state_times times;
#endif
89 90
};

91
#define BS_INIT(state_, run_func_)				\
92 93 94
	{							\
		.name = #state_,				\
		.id = state_,					\
95
		.post_code = POST_ ## state_,			\
96
		.phases = { { NULL, 0 }, { NULL, 0 } },		\
97 98 99
		.run_state = run_func_,				\
		.arg = NULL,					\
		.complete = 0,					\
100
	}
101
#define BS_INIT_ENTRY(state_, run_func_)	\
102
	[state_] = BS_INIT(state_, run_func_)
103 104 105 106 107 108

static struct boot_state boot_states[] = {
	BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
	BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
	BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
	BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
109
	BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
110 111
	BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
	BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
112
	BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
113 114 115 116
	BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
	BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
	BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
	BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
117 118
};

Aaron Durbin's avatar
Aaron Durbin committed
119
void __weak arch_bootstate_coreboot_exit(void) { }
120

121 122 123 124
static boot_state_t bs_pre_device(void *arg)
{
	return BS_DEV_INIT_CHIPS;
}
125

126 127
static boot_state_t bs_dev_init_chips(void *arg)
{
128
	timestamp_add_now(TS_DEVICE_ENUMERATE);
129 130 131 132

	/* Initialize chips early, they might disable unused devices. */
	dev_initialize_chips();

133 134 135 136 137
	return BS_DEV_ENUMERATE;
}

static boot_state_t bs_dev_enumerate(void *arg)
{
138
	/* Find the devices we don't have hard coded knowledge about. */
139
	dev_enumerate();
140

141 142 143 144 145
	return BS_DEV_RESOURCES;
}

static boot_state_t bs_dev_resources(void *arg)
{
146
	timestamp_add_now(TS_DEVICE_CONFIGURE);
147

148
	/* Now compute and assign the bus resources. */
149
	dev_configure();
150

151 152 153
	return BS_DEV_ENABLE;
}

154
static boot_state_t bs_dev_enable(void *arg)
155
{
156
	timestamp_add_now(TS_DEVICE_ENABLE);
157

158
	/* Now actually enable devices on the bus */
159
	dev_enable();
160

161 162 163 164 165
	return BS_DEV_INIT;
}

static boot_state_t bs_dev_init(void *arg)
{
166
	timestamp_add_now(TS_DEVICE_INITIALIZE);
167

168
	/* And of course initialize devices on the bus */
169 170
	dev_initialize();

171 172 173 174 175
	return BS_POST_DEVICE;
}

static boot_state_t bs_post_device(void *arg)
{
176
	dev_finalize();
177
	timestamp_add_now(TS_DEVICE_DONE);
178

179
	return BS_OS_RESUME_CHECK;
180 181
}

182
static boot_state_t bs_os_resume_check(void *arg)
183
{
184
#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)
185 186 187 188 189 190 191 192
	void *wake_vector;

	wake_vector = acpi_find_wakeup_vector();

	if (wake_vector != NULL) {
		boot_states[BS_OS_RESUME].arg = wake_vector;
		return BS_OS_RESUME;
	}
193 194

	acpi_prepare_resume_backup();
195
#endif
Stefan Reinauer's avatar
Stefan Reinauer committed
196 197
	timestamp_add_now(TS_CBMEM_POST);

198 199 200
	return BS_WRITE_TABLES;
}

201 202
static boot_state_t bs_os_resume(void *wake_vector)
{
203
#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)
204
	arch_bootstate_coreboot_exit();
205 206 207 208 209
	acpi_resume(wake_vector);
#endif
	return BS_WRITE_TABLES;
}

210 211
static boot_state_t bs_write_tables(void *arg)
{
212 213
	timestamp_add_now(TS_WRITE_TABLES);

214 215 216
	/* Now that we have collected all of our information
	 * write our configuration tables.
	 */
217 218
	write_tables();

219
	timestamp_add_now(TS_FINALIZE_CHIPS);
220 221
	dev_finalize_chips();

222 223 224 225 226
	return BS_PAYLOAD_LOAD;
}

static boot_state_t bs_payload_load(void *arg)
{
227
	payload_load();
228 229 230 231

	return BS_PAYLOAD_BOOT;
}

232
static boot_state_t bs_payload_boot(void *arg)
233
{
234
	arch_bootstate_coreboot_exit();
235
	payload_run();
236

237
	printk(BIOS_EMERG, "Boot failed\n");
238 239 240 241 242
	/* Returning from this state will fail because the following signals
	 * return to a completed state. */
	return BS_PAYLOAD_BOOT;
}

243
#if IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)
244 245 246 247 248 249 250 251 252 253 254
static void bs_sample_time(struct boot_state *state)
{
	struct mono_time *mt;

	mt = &state->times.samples[state->times.num_samples];
	timer_monotonic_get(mt);
	state->times.num_samples++;
}

static void bs_report_time(struct boot_state *state)
{
255 256 257 258
	long entry_time;
	long run_time;
	long exit_time;
	struct mono_time *samples = &state->times.samples[0];
259

260 261 262
	entry_time = mono_time_diff_microseconds(&samples[0], &samples[1]);
	run_time = mono_time_diff_microseconds(&samples[1], &samples[2]);
	exit_time = mono_time_diff_microseconds(&samples[2], &samples[3]);
263 264

	printk(BIOS_DEBUG, "BS: %s times (us): entry %ld run %ld exit %ld\n",
265
	       state->name, entry_time, run_time, exit_time);
266 267 268 269 270 271
}
#else
static inline void bs_sample_time(struct boot_state *state) {}
static inline void bs_report_time(struct boot_state *state) {}
#endif

272
#if IS_ENABLED(CONFIG_TIMER_QUEUE)
273 274 275 276 277 278 279 280 281 282 283 284 285
static void bs_run_timers(int drain)
{
	/* Drain all timer callbacks until none are left, if directed.
	 * Otherwise run the timers only once. */
	do {
		if (!timers_run())
			break;
	} while (drain);
}
#else
static void bs_run_timers(int drain) {}
#endif

286
static void bs_call_callbacks(struct boot_state *state,
287
			      boot_state_sequence_t seq)
288
{
289 290 291 292 293
	struct boot_phase *phase = &state->phases[seq];

	while (1) {
		if (phase->callbacks != NULL) {
			struct boot_state_callback *bscb;
294

295 296 297 298
			/* Remove the first callback. */
			bscb = phase->callbacks;
			phase->callbacks = bscb->next;
			bscb->next = NULL;
299

300 301 302
#if IS_ENABLED(CONFIG_DEBUG_BOOT_STATE)
			printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
				bscb, bscb->location);
303
#endif
304 305 306 307 308 309 310 311 312 313 314 315 316
			bscb->callback(bscb->arg);
			continue;
		}

		/* All callbacks are complete and there are no blockers for
		 * this state. Therefore, this part of the state is complete. */
		if (!phase->blockers)
			break;

		/* Something is blocking this state from transitioning. As
		 * there are no more callbacks a pending timer needs to be
		 * ran to unblock the state. */
		bs_run_timers(0);
317 318 319
	}
}

320 321 322 323 324 325 326 327 328 329
/* Keep track of the current state. */
static struct state_tracker {
	boot_state_t state_id;
	boot_state_sequence_t seq;
} current_phase = {
	.state_id = BS_PRE_DEVICE,
	.seq = BS_ON_ENTRY,
};

static void bs_walk_state_machine(void)
330 331 332 333
{

	while (1) {
		struct boot_state *state;
334
		boot_state_t next_id;
335

336
		state = &boot_states[current_phase.state_id];
337 338 339 340 341 342 343

		if (state->complete) {
			printk(BIOS_EMERG, "BS: %s state already executed.\n",
			       state->name);
			break;
		}

344 345 346
		if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
			printk(BIOS_DEBUG, "BS: Entering %s state.\n",
				state->name);
347

348
		bs_run_timers(0);
349

350 351
		bs_sample_time(state);

352 353 354 355 356
		bs_call_callbacks(state, current_phase.seq);
		/* Update the current sequence so that any calls to block the
		 * current state from the run_state() function will place a
		 * block on the correct phase. */
		current_phase.seq = BS_ON_EXIT;
357

358 359
		bs_sample_time(state);

360 361
		post_code(state->post_code);

362
		next_id = state->run_state(state->arg);
363

364 365 366
		if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
			printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
			state->name);
367 368 369

		bs_sample_time(state);

370 371
		bs_call_callbacks(state, current_phase.seq);

372 373 374 375
		if (IS_ENABLED(CONFIG_DEBUG_BOOT_STATE))
			printk(BIOS_DEBUG,
				"----------------------------------------\n");

376 377 378
		/* Update the current phase with new state id and sequence. */
		current_phase.state_id = next_id;
		current_phase.seq = BS_ON_ENTRY;
379

380 381 382 383
		bs_sample_time(state);

		bs_report_time(state);

384 385 386 387 388
		state->complete = 1;
	}
}

static int boot_state_sched_callback(struct boot_state *state,
389 390
				     struct boot_state_callback *bscb,
				     boot_state_sequence_t seq)
391 392 393 394 395 396 397 398 399
{
	if (state->complete) {
		printk(BIOS_WARNING,
		       "Tried to schedule callback on completed state %s.\n",
		       state->name);

		return -1;
	}

400 401
	bscb->next = state->phases[seq].callbacks;
	state->phases[seq].callbacks = bscb;
402 403 404 405 406

	return 0;
}

int boot_state_sched_on_entry(struct boot_state_callback *bscb,
407
			      boot_state_t state_id)
408 409 410 411 412 413 414
{
	struct boot_state *state = &boot_states[state_id];

	return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
}

int boot_state_sched_on_exit(struct boot_state_callback *bscb,
415
			     boot_state_t state_id)
416 417 418 419 420 421
{
	struct boot_state *state = &boot_states[state_id];

	return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
}

422 423
static void boot_state_schedule_static_entries(void)
{
424 425
	extern struct boot_state_init_entry *_bs_init_begin[];
	struct boot_state_init_entry **slot;
426

427 428
	for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
		struct boot_state_init_entry *cur = *slot;
429 430 431 432 433 434 435 436

		if (cur->when == BS_ON_ENTRY)
			boot_state_sched_on_entry(&cur->bscb, cur->state);
		else
			boot_state_sched_on_exit(&cur->bscb, cur->state);
	}
}

437
void main(void)
438
{
439 440 441 442 443 444 445 446 447 448 449 450
	/*
	 * We can generally jump between C and Ada code back and forth
	 * without trouble. But since we don't have an Ada main() we
	 * have to do some Ada package initializations that GNAT would
	 * do there. This has to be done before calling any Ada code.
	 *
	 * The package initializations should not have any dependen-
	 * cies on C code. So we can call them here early, and don't
	 * have to worry at which point we can start to use Ada.
	 */
	ramstage_adainit();

451 452 453 454 455
	/* TODO: Understand why this is here and move to arch/platform code. */
	/* For MMIO UART this needs to be called before any other printk. */
	if (IS_ENABLED(CONFIG_ARCH_X86))
		init_timer();

456 457 458 459 460 461
	/* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
	 * it is the very first thing done in ramstage.*/
	console_init();

	post_code(POST_CONSOLE_READY);

462 463 464 465 466 467 468 469
	/*
	 * CBMEM needs to be recovered in the EARLY_CBMEM_INIT case because
	 * timestamps, APCI, etc rely on the cbmem infrastructure being
	 * around. Explicitly recover it.
	 */
	if (IS_ENABLED(CONFIG_EARLY_CBMEM_INIT))
		cbmem_initialize();

470
	/* Record current time, try to locate timestamps in CBMEM. */
471
	timestamp_init(timestamp_get());
472 473

	timestamp_add_now(TS_START_RAMSTAGE);
474 475
	post_code(POST_ENTRY_RAMSTAGE);

476
	/* Handoff sleep type from romstage. */
477
#if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)
478 479 480
	acpi_is_wakeup();
#endif

481
	exception_init();
482 483
	threads_initialize();

484 485 486
	/* Schedule the static boot state entries. */
	boot_state_schedule_static_entries();

487 488
	bs_walk_state_machine();

489
	die("Boot state machine failure.\n");
490 491
}

492 493 494 495 496 497 498

int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
{
	struct boot_phase *bp;

	/* Blocking a previously ran state is not appropriate. */
	if (current_phase.state_id > state ||
499
	    (current_phase.state_id == state && current_phase.seq > seq)) {
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
		printk(BIOS_WARNING,
		       "BS: Completed state (%d, %d) block attempted.\n",
		       state, seq);
		return -1;
	}

	bp = &boot_states[state].phases[seq];
	bp->blockers++;

	return 0;
}

int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
{
	struct boot_phase *bp;

	/* Blocking a previously ran state is not appropriate. */
	if (current_phase.state_id > state ||
518
	    (current_phase.state_id == state && current_phase.seq > seq)) {
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
		printk(BIOS_WARNING,
		       "BS: Completed state (%d, %d) unblock attempted.\n",
		       state, seq);
		return -1;
	}

	bp = &boot_states[state].phases[seq];

	if (bp->blockers == 0) {
		printk(BIOS_WARNING,
		       "BS: Unblock attempted on non-blocked state (%d, %d).\n",
		       state, seq);
		return -1;
	}

	bp->blockers--;

	return 0;
}

void boot_state_current_block(void)
{
	boot_state_block(current_phase.state_id, current_phase.seq);
}

void boot_state_current_unblock(void)
{
	boot_state_unblock(current_phase.state_id, current_phase.seq);
}