Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #ifndef BOOTSTATE_H |
| 20 | #define BOOTSTATE_H |
| 21 | |
| 22 | #include <string.h> |
| 23 | |
| 24 | /* Control debugging of the boot state machine. */ |
| 25 | #define BOOT_STATE_DEBUG 0 |
| 26 | |
| 27 | /* |
| 28 | * The boot state machine provides a mechanism for calls to be made through- |
| 29 | * out the main boot process. The boot process is separated into discrete |
| 30 | * states. Upon a state's entry and exit and callbacks can be made. For |
| 31 | * example: |
| 32 | * |
| 33 | * Enter State |
| 34 | * + |
| 35 | * | |
| 36 | * V |
| 37 | * +-----------------+ |
| 38 | * | Entry callbacks | |
| 39 | * +-----------------+ |
| 40 | * | State Actions | |
| 41 | * +-----------------+ |
| 42 | * | Exit callbacks | |
| 43 | * +-------+---------+ |
| 44 | * | |
| 45 | * V |
| 46 | * Next State |
| 47 | * |
| 48 | * Below is the current flow from top to bottom: |
| 49 | * |
| 50 | * start |
| 51 | * | |
| 52 | * BS_PRE_DEVICE |
| 53 | * | |
| 54 | * BS_DEV_INIT_CHIPS |
| 55 | * | |
| 56 | * BS_DEV_ENUMERATE |
| 57 | * | |
| 58 | * BS_DEV_RESOURCES |
| 59 | * | |
| 60 | * BS_DEV_ENABLE |
| 61 | * | |
| 62 | * BS_DEV_INIT |
| 63 | * | |
| 64 | * BS_POST_DEVICE |
| 65 | * | |
| 66 | * BS_OS_RESUME_CHECK -------- BS_OS_RESUME |
| 67 | * | | |
| 68 | * BS_WRITE_TABLES os handoff |
| 69 | * | |
| 70 | * BS_PAYLOAD_LOAD |
| 71 | * | |
| 72 | * BS_PAYLOAD_BOOT |
| 73 | * | |
| 74 | * payload run |
| 75 | * |
| 76 | * Brief description of states: |
| 77 | * BS_PRE_DEVICE - before any device tree actions take place |
| 78 | * BS_DEV_INIT_CHIPS - init all chips in device tree |
| 79 | * BS_DEV_ENUMERATE - device tree probing |
| 80 | * BS_DEV_RESOURCES - device tree resource allocation and assignment |
| 81 | * BS_DEV_ENABLE - device tree enabling/disabling of devices |
| 82 | * BS_DEV_INIT - device tree device initialization |
| 83 | * BS_POST_DEVICE - all device tree actions performed |
| 84 | * BS_OS_RESUME_CHECK - check for OS resume |
| 85 | * BS_OS_RESUME - resume to OS |
| 86 | * BS_WRITE_TABLES - write coreboot tables |
| 87 | * BS_PAYLOAD_LOAD - Load payload into memory |
| 88 | * BS_PAYLOAD_BOOT - Boot to payload |
| 89 | */ |
| 90 | |
| 91 | typedef enum { |
| 92 | BS_PRE_DEVICE, |
| 93 | BS_DEV_INIT_CHIPS, |
| 94 | BS_DEV_ENUMERATE, |
| 95 | BS_DEV_RESOURCES, |
| 96 | BS_DEV_ENABLE, |
| 97 | BS_DEV_INIT, |
| 98 | BS_POST_DEVICE, |
Aaron Durbin | 0a6c20a | 2013-04-24 22:33:08 -0500 | [diff] [blame] | 99 | BS_OS_RESUME_CHECK, |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 100 | BS_OS_RESUME, |
| 101 | BS_WRITE_TABLES, |
| 102 | BS_PAYLOAD_LOAD, |
| 103 | BS_PAYLOAD_BOOT, |
| 104 | } boot_state_t; |
| 105 | |
| 106 | /* The boot_state_sequence_t describes when a callback is to be made. It is |
| 107 | * called either before a state is entered or when a state is exited. */ |
| 108 | typedef enum { |
| 109 | BS_ON_ENTRY, |
| 110 | BS_ON_EXIT |
| 111 | } boot_state_sequence_t; |
| 112 | |
| 113 | struct boot_state_callback { |
| 114 | void *arg; |
| 115 | void (*callback)(void *arg); |
| 116 | /* For use internal to the boot state machine. */ |
| 117 | struct boot_state_callback *next; |
| 118 | #if BOOT_STATE_DEBUG |
| 119 | const char *location; |
| 120 | #endif |
| 121 | }; |
| 122 | |
| 123 | #if BOOT_STATE_DEBUG |
| 124 | #define BOOT_STATE_CALLBACK_LOC __FILE__ ":" STRINGIFY(__LINE__) |
| 125 | #define BOOT_STATE_CALLBACK_INIT_DEBUG .location = BOOT_STATE_CALLBACK_LOC, |
| 126 | #define INIT_BOOT_STATE_CALLBACK_DEBUG(bscb_) \ |
| 127 | bscb_->location = BOOT_STATE_CALLBACK_LOC; |
| 128 | #else |
| 129 | #define BOOT_STATE_CALLBACK_INIT_DEBUG |
| 130 | #define INIT_BOOT_STATE_CALLBACK_DEBUG(bscb_) |
| 131 | #endif |
| 132 | |
| 133 | #define BOOT_STATE_CALLBACK_INIT(func_, arg_) \ |
| 134 | { \ |
| 135 | .arg = arg_, \ |
| 136 | .callback = func_, \ |
| 137 | .next = NULL, \ |
| 138 | BOOT_STATE_CALLBACK_INIT_DEBUG \ |
| 139 | } |
| 140 | |
| 141 | #define BOOT_STATE_CALLBACK(name_, func_, arg_) \ |
| 142 | struct boot_state_callback name_ = BOOT_STATE_CALLBACK_INIT(func_, arg_) |
| 143 | |
| 144 | /* Initialize an allocated boot_state_callback. */ |
| 145 | #define INIT_BOOT_STATE_CALLBACK(bscb_, func_, arg_) \ |
| 146 | INIT_BOOT_STATE_CALLBACK_DEBUG(bscb_) \ |
| 147 | bscb_->callback = func_; \ |
| 148 | bscb_->arg = arg_ |
| 149 | |
| 150 | /* The following 2 functions schedule a callback to be called on entry/exit |
| 151 | * to a given state. Note that thare are no ordering guarantees between the |
| 152 | * individual callbacks on a given state. 0 is returned on success < 0 on |
| 153 | * error. */ |
| 154 | int boot_state_sched_on_entry(struct boot_state_callback *bscb, |
| 155 | boot_state_t state); |
| 156 | int boot_state_sched_on_exit(struct boot_state_callback *bscb, |
| 157 | boot_state_t state); |
| 158 | |
Aaron Durbin | 0748d30 | 2013-05-06 10:50:19 -0500 | [diff] [blame^] | 159 | /* Block/Unblock the (state, seq) pair from transitioning. Returns 0 on |
| 160 | * success < 0 when the phase of the (state,seq) has already ran. */ |
| 161 | int boot_state_block(boot_state_t state, boot_state_sequence_t seq); |
| 162 | int boot_state_unblock(boot_state_t state, boot_state_sequence_t seq); |
| 163 | /* Block/Unblock current state phase from transitioning. */ |
| 164 | void boot_state_current_block(void); |
| 165 | void boot_state_current_unblock(void); |
| 166 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 167 | /* Entry into the boot state machine. */ |
| 168 | void hardwaremain(int boot_complete); |
| 169 | |
Aaron Durbin | a4feddf | 2013-04-24 16:12:52 -0500 | [diff] [blame] | 170 | /* In order to schedule boot state callbacks at compile-time specify the |
| 171 | * entries in an array using the BOOT_STATE_INIT_ENTRIES and |
| 172 | * BOOT_STATE_INIT_ENTRY macros below. */ |
| 173 | struct boot_state_init_entry { |
| 174 | boot_state_t state; |
| 175 | boot_state_sequence_t when; |
| 176 | struct boot_state_callback bscb; |
| 177 | }; |
| 178 | |
| 179 | #define BOOT_STATE_INIT_ATTR __attribute__ ((used,section (".bs_init"))) |
| 180 | |
| 181 | #define BOOT_STATE_INIT_ENTRIES(name_) \ |
| 182 | static struct boot_state_init_entry name_[] BOOT_STATE_INIT_ATTR |
| 183 | |
| 184 | #define BOOT_STATE_INIT_ENTRY(state_, when_, func_, arg_) \ |
| 185 | { \ |
| 186 | .state = state_, \ |
| 187 | .when = when_, \ |
| 188 | .bscb = BOOT_STATE_CALLBACK_INIT(func_, arg_), \ |
| 189 | } |
| 190 | |
Aaron Durbin | 7e35efa | 2013-04-24 15:14:01 -0500 | [diff] [blame] | 191 | #endif /* BOOTSTATE_H */ |