blob: 51ff330d849bc5a2388ad02b12c4a3d333704368 [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>
Kyösti Mälkki463ad512019-11-01 09:12:34 +020026#include <commonlib/helpers.h>
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -050027#include <cbmem.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000028#include <version.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000029#include <device/device.h>
30#include <device/pci.h>
Eric Biederman9b4336c2003-07-19 04:28:22 +000031#include <delay.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +000032#include <stdlib.h>
Stefan Reinauer7e9771c2009-04-22 08:17:38 +000033#include <boot/tables.h>
Aaron Durbin04654a22015-03-17 11:43:44 -050034#include <program_loading.h>
Julius Wernercd49cce2019-03-05 16:53:33 -080035#if CONFIG(HAVE_ACPI_RESUME)
Rudolf Mareka572f832009-04-13 17:57:44 +000036#include <arch/acpi.h>
Stefan Reinauer3935b192009-04-14 06:38:15 +000037#endif
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050038#include <timer.h>
Stefan Reinauerbf729ba2011-11-04 12:31:58 -070039#include <timestamp.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050040#include <thread.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000041
Aaron Durbin7e35efa2013-04-24 15:14:01 -050042static boot_state_t bs_pre_device(void *arg);
43static boot_state_t bs_dev_init_chips(void *arg);
44static boot_state_t bs_dev_enumerate(void *arg);
45static boot_state_t bs_dev_resources(void *arg);
David Hendrickscca68592013-06-20 14:08:27 -070046static boot_state_t bs_dev_enable(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050047static boot_state_t bs_dev_init(void *arg);
48static boot_state_t bs_post_device(void *arg);
Aaron Durbin0a6c20a2013-04-24 22:33:08 -050049static boot_state_t bs_os_resume_check(void *arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -050050static boot_state_t bs_os_resume(void *arg);
51static boot_state_t bs_write_tables(void *arg);
52static boot_state_t bs_payload_load(void *arg);
53static boot_state_t bs_payload_boot(void *arg);
54
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050055/*
56 * Typically a state will take 4 time samples:
57 * 1. Before state entry callbacks
58 * 2. After state entry callbacks / Before state function.
59 * 3. After state function / Before state exit callbacks.
60 * 4. After state exit callbacks.
61 */
62#define MAX_TIME_SAMPLES 4
63struct boot_state_times {
64 int num_samples;
65 struct mono_time samples[MAX_TIME_SAMPLES];
66};
67
Aaron Durbin0748d302013-05-06 10:50:19 -050068/* The prologue (BS_ON_ENTRY) and epilogue (BS_ON_EXIT) of a state can be
69 * blocked from transitioning to the next (state,seq) pair. When the blockers
70 * field is 0 a transition may occur. */
71struct boot_phase {
72 struct boot_state_callback *callbacks;
73 int blockers;
74};
75
Aaron Durbin7e35efa2013-04-24 15:14:01 -050076struct boot_state {
77 const char *name;
78 boot_state_t id;
Duncan Lauriecb73a842013-06-10 10:41:04 -070079 u8 post_code;
Aaron Durbin0748d302013-05-06 10:50:19 -050080 struct boot_phase phases[2];
Aaron Durbin7e35efa2013-04-24 15:14:01 -050081 boot_state_t (*run_state)(void *arg);
82 void *arg;
Aaron Durbin8fc41e12013-04-29 23:22:01 -050083 int complete : 1;
Julius Wernercd49cce2019-03-05 16:53:33 -080084#if CONFIG(HAVE_MONOTONIC_TIMER)
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -050085 struct boot_state_times times;
86#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -050087};
88
Aaron Durbin15c671e2013-05-06 10:52:24 -050089#define BS_INIT(state_, run_func_) \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050090 { \
91 .name = #state_, \
92 .id = state_, \
Duncan Lauriecb73a842013-06-10 10:41:04 -070093 .post_code = POST_ ## state_, \
Aaron Durbin0748d302013-05-06 10:50:19 -050094 .phases = { { NULL, 0 }, { NULL, 0 } }, \
Aaron Durbin8fc41e12013-04-29 23:22:01 -050095 .run_state = run_func_, \
96 .arg = NULL, \
97 .complete = 0, \
Aaron Durbin7e35efa2013-04-24 15:14:01 -050098 }
99#define BS_INIT_ENTRY(state_, run_func_) \
Aaron Durbin15c671e2013-05-06 10:52:24 -0500100 [state_] = BS_INIT(state_, run_func_)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500101
102static struct boot_state boot_states[] = {
103 BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device),
104 BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips),
105 BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate),
106 BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources),
David Hendrickscca68592013-06-20 14:08:27 -0700107 BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable),
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500108 BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init),
109 BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device),
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500110 BS_INIT_ENTRY(BS_OS_RESUME_CHECK, bs_os_resume_check),
Aaron Durbin15c671e2013-05-06 10:52:24 -0500111 BS_INIT_ENTRY(BS_OS_RESUME, bs_os_resume),
112 BS_INIT_ENTRY(BS_WRITE_TABLES, bs_write_tables),
113 BS_INIT_ENTRY(BS_PAYLOAD_LOAD, bs_payload_load),
114 BS_INIT_ENTRY(BS_PAYLOAD_BOOT, bs_payload_boot),
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500115};
116
Aaron Durbin64031672018-04-21 14:45:32 -0600117void __weak arch_bootstate_coreboot_exit(void) { }
Aaron Durbin16bd2672016-12-07 11:58:20 -0600118
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500119static boot_state_t bs_pre_device(void *arg)
120{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500121 return BS_DEV_INIT_CHIPS;
122}
123
124static boot_state_t bs_dev_init_chips(void *arg)
125{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300126 timestamp_add_now(TS_DEVICE_ENUMERATE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500127
128 /* Initialize chips early, they might disable unused devices. */
129 dev_initialize_chips();
130
131 return BS_DEV_ENUMERATE;
132}
133
134static boot_state_t bs_dev_enumerate(void *arg)
135{
136 /* Find the devices we don't have hard coded knowledge about. */
137 dev_enumerate();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500138
139 return BS_DEV_RESOURCES;
140}
141
142static boot_state_t bs_dev_resources(void *arg)
143{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300144 timestamp_add_now(TS_DEVICE_CONFIGURE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700145
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500146 /* Now compute and assign the bus resources. */
147 dev_configure();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500148
149 return BS_DEV_ENABLE;
150}
151
David Hendrickscca68592013-06-20 14:08:27 -0700152static boot_state_t bs_dev_enable(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500153{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300154 timestamp_add_now(TS_DEVICE_ENABLE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700155
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500156 /* Now actually enable devices on the bus */
157 dev_enable();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500158
159 return BS_DEV_INIT;
160}
161
162static boot_state_t bs_dev_init(void *arg)
163{
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300164 timestamp_add_now(TS_DEVICE_INITIALIZE);
Duncan Lauriecb73a842013-06-10 10:41:04 -0700165
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500166 /* And of course initialize devices on the bus */
167 dev_initialize();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500168
169 return BS_POST_DEVICE;
170}
171
172static boot_state_t bs_post_device(void *arg)
173{
Marc Jones2a58ecd2013-10-29 17:32:00 -0600174 dev_finalize();
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300175 timestamp_add_now(TS_DEVICE_DONE);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500176
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500177 return BS_OS_RESUME_CHECK;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500178}
179
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500180static boot_state_t bs_os_resume_check(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500181{
Julius Wernercd49cce2019-03-05 16:53:33 -0800182#if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500183 void *wake_vector;
184
185 wake_vector = acpi_find_wakeup_vector();
186
187 if (wake_vector != NULL) {
188 boot_states[BS_OS_RESUME].arg = wake_vector;
189 return BS_OS_RESUME;
190 }
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500191#endif
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500192 timestamp_add_now(TS_CBMEM_POST);
193
194 return BS_WRITE_TABLES;
195}
196
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500197static boot_state_t bs_os_resume(void *wake_vector)
198{
Julius Wernercd49cce2019-03-05 16:53:33 -0800199#if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin16bd2672016-12-07 11:58:20 -0600200 arch_bootstate_coreboot_exit();
Aaron Durbin0a6c20a2013-04-24 22:33:08 -0500201 acpi_resume(wake_vector);
202#endif
203 return BS_WRITE_TABLES;
204}
205
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500206static boot_state_t bs_write_tables(void *arg)
207{
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500208 timestamp_add_now(TS_WRITE_TABLES);
209
210 /* Now that we have collected all of our information
211 * write our configuration tables.
212 */
213 write_tables();
214
Paul Menzel00563bf2016-12-05 23:06:37 +0100215 timestamp_add_now(TS_FINALIZE_CHIPS);
Marc Jones2a58ecd2013-10-29 17:32:00 -0600216 dev_finalize_chips();
217
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500218 return BS_PAYLOAD_LOAD;
219}
220
221static boot_state_t bs_payload_load(void *arg)
222{
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500223 payload_load();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500224
225 return BS_PAYLOAD_BOOT;
226}
227
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600228static boot_state_t bs_payload_boot(void *arg)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500229{
Aaron Durbin16bd2672016-12-07 11:58:20 -0600230 arch_bootstate_coreboot_exit();
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500231 payload_run();
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500232
Jonathan Neuschäfer0a54fb52016-05-27 09:05:02 +0200233 printk(BIOS_EMERG, "Boot failed\n");
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500234 /* Returning from this state will fail because the following signals
235 * return to a completed state. */
236 return BS_PAYLOAD_BOOT;
237}
238
Julius Wernercd49cce2019-03-05 16:53:33 -0800239#if CONFIG(HAVE_MONOTONIC_TIMER)
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500240static void bs_sample_time(struct boot_state *state)
241{
242 struct mono_time *mt;
243
244 mt = &state->times.samples[state->times.num_samples];
245 timer_monotonic_get(mt);
246 state->times.num_samples++;
247}
248
249static void bs_report_time(struct boot_state *state)
250{
Aaron Durbinad5a9092014-09-24 09:48:47 -0500251 long entry_time;
252 long run_time;
253 long exit_time;
254 struct mono_time *samples = &state->times.samples[0];
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500255
Aaron Durbinad5a9092014-09-24 09:48:47 -0500256 entry_time = mono_time_diff_microseconds(&samples[0], &samples[1]);
257 run_time = mono_time_diff_microseconds(&samples[1], &samples[2]);
258 exit_time = mono_time_diff_microseconds(&samples[2], &samples[3]);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500259
Kyösti Mälkki463ad512019-11-01 09:12:34 +0200260 /* Report with millisecond precision to reduce log diffs. */
261 entry_time = DIV_ROUND_CLOSEST(entry_time, USECS_PER_MSEC);
262 run_time = DIV_ROUND_CLOSEST(run_time, USECS_PER_MSEC);
263 exit_time = DIV_ROUND_CLOSEST(exit_time, USECS_PER_MSEC);
264
265 printk(BIOS_DEBUG, "BS: %s times (ms): entry %ld run %ld exit %ld\n",
Aaron Durbinad5a9092014-09-24 09:48:47 -0500266 state->name, entry_time, run_time, exit_time);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500267}
268#else
269static inline void bs_sample_time(struct boot_state *state) {}
270static inline void bs_report_time(struct boot_state *state) {}
271#endif
272
Julius Wernercd49cce2019-03-05 16:53:33 -0800273#if CONFIG(TIMER_QUEUE)
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500274static void bs_run_timers(int drain)
275{
276 /* Drain all timer callbacks until none are left, if directed.
277 * Otherwise run the timers only once. */
278 do {
279 if (!timers_run())
280 break;
281 } while (drain);
282}
283#else
284static void bs_run_timers(int drain) {}
285#endif
286
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500287static void bs_call_callbacks(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800288 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500289{
Aaron Durbin0748d302013-05-06 10:50:19 -0500290 struct boot_phase *phase = &state->phases[seq];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500291
Aaron Durbin0748d302013-05-06 10:50:19 -0500292 while (1) {
293 if (phase->callbacks != NULL) {
294 struct boot_state_callback *bscb;
295
296 /* Remove the first callback. */
297 bscb = phase->callbacks;
298 phase->callbacks = bscb->next;
299 bscb->next = NULL;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500300
Julius Wernercd49cce2019-03-05 16:53:33 -0800301#if CONFIG(DEBUG_BOOT_STATE)
Lee Leahy10605352016-02-14 17:01:40 -0800302 printk(BIOS_DEBUG, "BS: callback (%p) @ %s.\n",
303 bscb, bscb->location);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500304#endif
Aaron Durbin0748d302013-05-06 10:50:19 -0500305 bscb->callback(bscb->arg);
Aaron Durbin0748d302013-05-06 10:50:19 -0500306 continue;
307 }
308
309 /* All callbacks are complete and there are no blockers for
310 * this state. Therefore, this part of the state is complete. */
311 if (!phase->blockers)
312 break;
313
314 /* Something is blocking this state from transitioning. As
315 * there are no more callbacks a pending timer needs to be
316 * ran to unblock the state. */
317 bs_run_timers(0);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500318 }
319}
320
Aaron Durbin0748d302013-05-06 10:50:19 -0500321/* Keep track of the current state. */
322static struct state_tracker {
323 boot_state_t state_id;
324 boot_state_sequence_t seq;
325} current_phase = {
326 .state_id = BS_PRE_DEVICE,
327 .seq = BS_ON_ENTRY,
328};
329
330static void bs_walk_state_machine(void)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500331{
332
333 while (1) {
334 struct boot_state *state;
Aaron Durbin0748d302013-05-06 10:50:19 -0500335 boot_state_t next_id;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500336
Aaron Durbin0748d302013-05-06 10:50:19 -0500337 state = &boot_states[current_phase.state_id];
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500338
339 if (state->complete) {
340 printk(BIOS_EMERG, "BS: %s state already executed.\n",
341 state->name);
342 break;
343 }
344
Julius Wernercd49cce2019-03-05 16:53:33 -0800345 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800346 printk(BIOS_DEBUG, "BS: Entering %s state.\n",
347 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500348
Aaron Durbin15c671e2013-05-06 10:52:24 -0500349 bs_run_timers(0);
Aaron Durbin8fc41e12013-04-29 23:22:01 -0500350
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500351 bs_sample_time(state);
352
Aaron Durbin0748d302013-05-06 10:50:19 -0500353 bs_call_callbacks(state, current_phase.seq);
354 /* Update the current sequence so that any calls to block the
355 * current state from the run_state() function will place a
356 * block on the correct phase. */
357 current_phase.seq = BS_ON_EXIT;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500358
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500359 bs_sample_time(state);
360
Duncan Lauriecb73a842013-06-10 10:41:04 -0700361 post_code(state->post_code);
362
Aaron Durbin0748d302013-05-06 10:50:19 -0500363 next_id = state->run_state(state->arg);
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500364
Julius Wernercd49cce2019-03-05 16:53:33 -0800365 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800366 printk(BIOS_DEBUG, "BS: Exiting %s state.\n",
367 state->name);
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500368
369 bs_sample_time(state);
370
Aaron Durbin0748d302013-05-06 10:50:19 -0500371 bs_call_callbacks(state, current_phase.seq);
372
Julius Wernercd49cce2019-03-05 16:53:33 -0800373 if (CONFIG(DEBUG_BOOT_STATE))
Lee Leahy10605352016-02-14 17:01:40 -0800374 printk(BIOS_DEBUG,
375 "----------------------------------------\n");
376
Aaron Durbin0748d302013-05-06 10:50:19 -0500377 /* Update the current phase with new state id and sequence. */
378 current_phase.state_id = next_id;
379 current_phase.seq = BS_ON_ENTRY;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500380
Aaron Durbin6b0fb0d2013-04-26 20:54:16 -0500381 bs_sample_time(state);
382
383 bs_report_time(state);
384
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500385 state->complete = 1;
386 }
387}
388
389static int boot_state_sched_callback(struct boot_state *state,
Lee Leahye20a3192017-03-09 16:21:34 -0800390 struct boot_state_callback *bscb,
391 boot_state_sequence_t seq)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500392{
393 if (state->complete) {
394 printk(BIOS_WARNING,
395 "Tried to schedule callback on completed state %s.\n",
396 state->name);
397
398 return -1;
399 }
400
Aaron Durbin0748d302013-05-06 10:50:19 -0500401 bscb->next = state->phases[seq].callbacks;
402 state->phases[seq].callbacks = bscb;
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500403
404 return 0;
405}
406
407int boot_state_sched_on_entry(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800408 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500409{
410 struct boot_state *state = &boot_states[state_id];
411
412 return boot_state_sched_callback(state, bscb, BS_ON_ENTRY);
413}
414
415int boot_state_sched_on_exit(struct boot_state_callback *bscb,
Lee Leahye20a3192017-03-09 16:21:34 -0800416 boot_state_t state_id)
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500417{
418 struct boot_state *state = &boot_states[state_id];
419
420 return boot_state_sched_callback(state, bscb, BS_ON_EXIT);
421}
Maciej Pijankaea921852009-10-27 14:29:29 +0000422
Aaron Durbina4feddf2013-04-24 16:12:52 -0500423static void boot_state_schedule_static_entries(void)
424{
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500425 extern struct boot_state_init_entry *_bs_init_begin[];
426 struct boot_state_init_entry **slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500427
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500428 for (slot = &_bs_init_begin[0]; *slot != NULL; slot++) {
429 struct boot_state_init_entry *cur = *slot;
Aaron Durbina4feddf2013-04-24 16:12:52 -0500430
Aaron Durbina4feddf2013-04-24 16:12:52 -0500431 if (cur->when == BS_ON_ENTRY)
432 boot_state_sched_on_entry(&cur->bscb, cur->state);
433 else
434 boot_state_sched_on_exit(&cur->bscb, cur->state);
Aaron Durbina4feddf2013-04-24 16:12:52 -0500435 }
436}
437
Stefan Reinauer6adef082013-05-09 16:30:06 -0700438void main(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000439{
Nico Hubere0ed9022016-10-07 12:58:17 +0200440 /*
441 * We can generally jump between C and Ada code back and forth
442 * without trouble. But since we don't have an Ada main() we
443 * have to do some Ada package initializations that GNAT would
444 * do there. This has to be done before calling any Ada code.
445 *
446 * The package initializations should not have any dependen-
447 * cies on C code. So we can call them here early, and don't
448 * have to worry at which point we can start to use Ada.
449 */
450 ramstage_adainit();
451
Duncan Lauriea5181512015-06-04 08:42:48 -0700452 /* TODO: Understand why this is here and move to arch/platform code. */
453 /* For MMIO UART this needs to be called before any other printk. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800454 if (CONFIG(ARCH_X86))
Duncan Lauriea5181512015-06-04 08:42:48 -0700455 init_timer();
456
Aaron Durbinbe347972015-04-21 11:57:18 -0500457 /* console_init() MUST PRECEDE ALL printk()! Additionally, ensure
458 * it is the very first thing done in ramstage.*/
459 console_init();
Aaron Durbinbe347972015-04-21 11:57:18 -0500460 post_code(POST_CONSOLE_READY);
461
Asami Doi38a12862019-08-22 11:15:12 +0900462 exception_init();
463
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500464 /*
Subrata Banik91160e12019-01-29 17:16:15 +0530465 * CBMEM needs to be recovered because timestamps, ACPI, etc rely on
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300466 * the cbmem infrastructure being around. Explicitly recover it.
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500467 */
Kyösti Mälkki513a1a82018-06-03 12:29:50 +0300468 cbmem_initialize();
Aaron Durbinb0d8f5e2015-04-06 16:12:58 -0500469
Kyösti Mälkkib766b1c2013-09-07 17:26:08 +0300470 timestamp_add_now(TS_START_RAMSTAGE);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000471 post_code(POST_ENTRY_RAMSTAGE);
Li-Ta Lo6a8745ae2004-05-13 20:39:07 +0000472
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300473 /* Handoff sleep type from romstage. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800474#if CONFIG(HAVE_ACPI_RESUME)
Kyösti Mälkkidb8693b2014-06-19 23:29:07 +0300475 acpi_is_wakeup();
476#endif
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500477 threads_initialize();
478
Aaron Durbina4feddf2013-04-24 16:12:52 -0500479 /* Schedule the static boot state entries. */
480 boot_state_schedule_static_entries();
481
Aaron Durbin0748d302013-05-06 10:50:19 -0500482 bs_walk_state_machine();
483
Aaron Durbin7e35efa2013-04-24 15:14:01 -0500484 die("Boot state machine failure.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000485}
486
Aaron Durbin0748d302013-05-06 10:50:19 -0500487
488int boot_state_block(boot_state_t state, boot_state_sequence_t seq)
489{
490 struct boot_phase *bp;
491
492 /* Blocking a previously ran state is not appropriate. */
493 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800494 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500495 printk(BIOS_WARNING,
496 "BS: Completed state (%d, %d) block attempted.\n",
497 state, seq);
498 return -1;
499 }
500
501 bp = &boot_states[state].phases[seq];
502 bp->blockers++;
503
504 return 0;
505}
506
507int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq)
508{
509 struct boot_phase *bp;
510
511 /* Blocking a previously ran state is not appropriate. */
512 if (current_phase.state_id > state ||
Lee Leahyd638ef42017-03-07 09:47:22 -0800513 (current_phase.state_id == state && current_phase.seq > seq)) {
Aaron Durbin0748d302013-05-06 10:50:19 -0500514 printk(BIOS_WARNING,
515 "BS: Completed state (%d, %d) unblock attempted.\n",
516 state, seq);
517 return -1;
518 }
519
520 bp = &boot_states[state].phases[seq];
521
522 if (bp->blockers == 0) {
523 printk(BIOS_WARNING,
524 "BS: Unblock attempted on non-blocked state (%d, %d).\n",
525 state, seq);
526 return -1;
527 }
528
529 bp->blockers--;
530
531 return 0;
532}
533
534void boot_state_current_block(void)
535{
536 boot_state_block(current_phase.state_id, current_phase.seq);
537}
538
539void boot_state_current_unblock(void)
540{
541 boot_state_unblock(current_phase.state_id, current_phase.seq);
542}