| 1 | # Firmware Configuration Interface in coreboot |
| 2 | |
| 3 | ## Motivation |
| 4 | |
| 5 | The firmware configuration interface in coreboot is designed to support |
| 6 | a wide variety of configuration options in that are dictated by the |
| 7 | hardware at runtime. This allows a single BIOS image to be used across a |
| 8 | wide variety of devices which may have key differences but are otherwise |
| 9 | similar enough to use the same coreboot build target. |
| 10 | |
| 11 | The initial implementation is designed to take advantage of a bitmask |
| 12 | returned by the Embedded Controller on Google ChromeOS devices which |
| 13 | allows the manufacturer to use the same firmware image across multiple |
| 14 | devices by selecting various options at runtime. See the ChromiumOS |
| 15 | [Firmware Config][1] documentation for more information. |
| 16 | |
| 17 | This firmware configuration interface differs from the CMOS option |
| 18 | interface in that this bitmask value is not intended as a |
| 19 | user-configurable setting as the configuration values must match the |
| 20 | actual hardware. In the case where a user was to swap their hardware |
| 21 | this value would need to be updated or overridden. |
| 22 | |
| 23 | |
| 24 | ## Device Presence |
| 25 | |
| 26 | One common example of why a firmware configuration interface is |
| 27 | important is determining if a device is present in the system. With some |
| 28 | bus topologies and hardware mechanisms it is possible to probe and |
| 29 | enumerate this at runtime: |
| 30 | |
| 31 | - PCI is a self-discoverable bus and is very easy to handle. |
| 32 | - I2C devices can often be probed with a combination of bus and address. |
| 33 | - The use of GPIOs with external strap to ground or different voltages |
| 34 | can be used to detect presence of a device. |
| 35 | |
| 36 | However there are several cases where this is insufficient: |
| 37 | |
| 38 | - I2C peripherals that require different drivers but have the same bus |
| 39 | address cannot be uniquely identified at runtime. |
| 40 | - A mainboard may be designed with multiple daughter board combinations |
| 41 | which contain devices and configurations that cannot be detected. |
| 42 | - While presence detect GPIOs are a convenient way for a single device |
| 43 | presence, they are unable to distinguish between different devices so |
| 44 | it can require a large number of GPIOs to support relatively few |
| 45 | options. |
| 46 | |
| 47 | This presence detection can impact different stages of boot: |
| 48 | |
| 49 | |
| 50 | ### ACPI |
| 51 | |
| 52 | Devices that are not present should not provide an ACPI device |
| 53 | indicating that they are present or the operating system may not be able |
| 54 | to handle it correctly. |
| 55 | |
| 56 | The ACPI devices are largely driven by chips defined in the mainboard |
| 57 | `devicetree.cb` and the variant overridetree.cb. This means it is |
| 58 | important to be able to specify when a device is present or not directly |
| 59 | in `devicetree.cb` itself. Otherwise each mainboard needs custom code to |
| 60 | parse the tree and disable unused devices. |
| 61 | |
| 62 | |
| 63 | ### GPIO |
| 64 | |
| 65 | GPIOs with multiple functions may need to be configured correctly |
| 66 | depending on the attached device. Given the wide variety of GPIO |
| 67 | configuration possibilities it is not feasible to specify all |
| 68 | combinations directly in `devicetree.cb` and it is best left to code |
| 69 | provided by the mainboard. |
| 70 | |
| 71 | |
| 72 | ### FSP UPD |
| 73 | |
| 74 | Enabling and disabling devices may require altering FSP UPD values that |
| 75 | are provided to the various stages of FSP. These options are also not |
| 76 | easy to specify multiple times for different configurations in |
| 77 | `devicetree.cb` and can be provided by the mainboard as code. |
| 78 | |
| 79 | |
| 80 | ## Firmware Configuration Interface |
| 81 | |
| 82 | The firmware configuration interface can be enabled by selecting |
| 83 | `CONFIG_FW_CONFIG` and also providing a source for the value by defining |
| 84 | an additional Kconfig option defined below. |
| 85 | |
| 86 | If the firmware configuration interface is disabled via Kconfig then all |
| 87 | probe attempts will return true. |
| 88 | |
| 89 | |
| 90 | ## Firmware Configuration Value |
| 91 | |
| 92 | The 64-bit value used as the firmware configuration bitmask is meant to |
| 93 | be determined at runtime but could also be defined at compile time if |
| 94 | needed. |
| 95 | |
| 96 | There are two supported sources for providing this information to |
| 97 | coreboot. |
| 98 | |
| 99 | |
| 100 | ### CBFS |
| 101 | |
| 102 | The value can be provided with a 64-bit raw value in CBFS that is read |
| 103 | by coreboot. The value can be set at build time but also adjusted in an |
| 104 | existing image with `cbfstool`. |
| 105 | |
| 106 | To enable this select the `CONFIG_FW_CONFIG_CBFS` option in the build |
| 107 | configuration and add a raw 64-bit value to CBFS with the name of the |
| 108 | current prefix at `CONFIG_FW_PREFIX/fw_config`. |
| 109 | |
| 110 | When `fw_config_probe_device()` or `fw_config_probe()` is called it will |
| 111 | look for the specified file in CBFS use the value it contains when |
| 112 | matching fields and options. |
| 113 | |
| 114 | |
| 115 | ### Embedded Controller |
| 116 | |
| 117 | Google ChromeOS devices support an Embedded Controller interface for |
| 118 | reading and writing the firmware configuration value, along with other |
| 119 | board-specific information. It is possible for coreboot to read this |
| 120 | value at boot on systems that support this feature. |
| 121 | |
| 122 | This option is selected by default for the mainboards that use it with |
| 123 | `CONFIG_FW_CONFIG_CHROME_EC_CBI` and it is not typically necessary to |
| 124 | adjust the value. It is possible by enabling the CBFS source and |
| 125 | coreboot will look in CBFS first for a valid value before asking the |
| 126 | embedded controller. |
| 127 | |
| 128 | It is also possible to adjust the value in the embedded controller |
| 129 | *(after disabling write protection)* with the `ectool` command in a |
| 130 | ChromeOS environment. |
| 131 | |
| 132 | For more information on the firmware configuration field on ChromeOS |
| 133 | devices see the Chromium documentation for [Firmware Config][1] and |
| 134 | [Board Info][2]. |
| 135 | |
| 136 | [1]: http://chromium.googlesource.com/chromiumos/docs/+/HEAD/design_docs/firmware_config.md |
| 137 | [2]: http://chromium.googlesource.com/chromiumos/docs/+/HEAD/design_docs/cros_board_info.md |
| 138 | |
| 139 | |
| 140 | ## Firmware Configuration Table |
| 141 | |
| 142 | The firmware configuration table itself is defined in the mainboard |
| 143 | `devicetree.cb` with special tokens for defining fields and options. |
| 144 | |
| 145 | The table itself is enclosed in a `fw_config` token and terminated with |
| 146 | `end` and it contains a mix of field and option definitions. |
| 147 | |
| 148 | Each field is defined by providing the field name and the start and end |
| 149 | bit marking the exact location in the bitmask. Field names must be at |
| 150 | least three characters long in order to satisfy the sconfig parser |
| 151 | requirements and they must be unique with non-overlapping masks. |
| 152 | |
| 153 | ```text |
| 154 | field <name> <start-bit> <end-bit> [option...] end |
| 155 | ``` |
| 156 | |
| 157 | For single-bit fields only one number is needed: |
| 158 | |
| 159 | ```text |
| 160 | field <name> <bit> [option...] end |
| 161 | ``` |
| 162 | |
| 163 | A field definition can also contain multiple sets of bit masks, which |
| 164 | can be dis-contiguous. They are treated as if they are contiguous when |
| 165 | defining option values. This allows for extending fields even after the |
| 166 | bits after its current masks are occupied. |
| 167 | |
| 168 | ```text |
| 169 | field <name> <start-bit0> <end-bit0> | <start-bit1> <end-bit1> | ... |
| 170 | ``` |
| 171 | |
| 172 | For example, if more audio options need to be supported: |
| 173 | |
| 174 | ```text |
| 175 | field AUDIO 3 3 |
| 176 | option AUDIO_0 0 |
| 177 | option AUDIO_1 1 |
| 178 | end |
| 179 | field OTHER 4 4 |
| 180 | ... |
| 181 | end |
| 182 | ``` |
| 183 | |
| 184 | the following can be done: |
| 185 | |
| 186 | ```text |
| 187 | field AUDIO 3 3 | 5 5 |
| 188 | option AUDIO_FOO 0 |
| 189 | option AUDIO_BLAH 1 |
| 190 | option AUDIO_BAR 2 |
| 191 | option AUDIO_BAZ 3 |
| 192 | end |
| 193 | field OTHER 4 4 |
| 194 | ... |
| 195 | end |
| 196 | ``` |
| 197 | |
| 198 | In that case, the AUDIO masks are extended like so: |
| 199 | |
| 200 | ```c |
| 201 | #define FW_CONFIG_FIELD_AUDIO_MASK 0x28 |
| 202 | #define FW_CONFIG_FIELD_AUDIO_OPTION_AUDIO_FOO_VALUE 0x0 |
| 203 | #define FW_CONFIG_FIELD_AUDIO_OPTION_AUDIO_BLAH_VALUE 0x8 |
| 204 | #define FW_CONFIG_FIELD_AUDIO_OPTION_AUDIO_BAR_VALUE 0x20 |
| 205 | #define FW_CONFIG_FIELD_AUDIO_OPTION_AUDIO_BAz_VALUE 0x28 |
| 206 | ``` |
| 207 | |
| 208 | Each `field` definition starts a new block that can be composed of zero |
| 209 | or more field options, and it is terminated with `end`. |
| 210 | |
| 211 | Inside the field block the options can be defined by providing the |
| 212 | option name and the field value that this option represents when the bit |
| 213 | offsets are used to apply a mask and shift. Option names must also be at |
| 214 | least three characters for the sconfig parser. |
| 215 | |
| 216 | ```text |
| 217 | option <name> <value> |
| 218 | ``` |
| 219 | |
| 220 | It is possible for there to be multiple `fw_config` blocks and for |
| 221 | subsequent `field` blocks to add additional `option` definitions to the |
| 222 | existing field. These subsequent definitions should not provide the |
| 223 | field bitmask as it has already been defined earlier in the file and |
| 224 | this is just matching an existing field by name. |
| 225 | |
| 226 | ```text |
| 227 | field <name> [option...] end |
| 228 | ``` |
| 229 | |
| 230 | This allows a baseboard to define the major fields and options in |
| 231 | `devicetree.cb` and a board variant to add specific options to fields in |
| 232 | or define new fields in the unused bitmask in `overridetree.cb`. |
| 233 | |
| 234 | It is not possible to redefine a field mask or override the value of an |
| 235 | existing option this way, only to add new options to a field or new |
| 236 | fields to the table. |
| 237 | |
| 238 | |
| 239 | ### Firmware Configuration Table Example |
| 240 | |
| 241 | In this example a baseboard defines a simple boolean feature that is |
| 242 | enabled or disabled depending on the value of bit 0, and a field at bits |
| 243 | 1-2 that indicates which daughter board is attached. |
| 244 | |
| 245 | The baseboard itself defines one daughter board and the variant adds two |
| 246 | more possibilities. This way each variant can support multiple possible |
| 247 | daughter boards in addition to the one that was defined by the |
| 248 | baseboard. |
| 249 | |
| 250 | |
| 251 | #### devicetree.cb |
| 252 | |
| 253 | ```text |
| 254 | fw_config |
| 255 | field FEATURE 0 |
| 256 | option DISABLED 0 |
| 257 | option ENABLED 1 |
| 258 | end |
| 259 | field DAUGHTER_BOARD 1 2 |
| 260 | option NONE 0 |
| 261 | option REFERENCE_DB 1 |
| 262 | end |
| 263 | end |
| 264 | ``` |
| 265 | |
| 266 | |
| 267 | #### overridetree.cb |
| 268 | |
| 269 | ```text |
| 270 | fw_config |
| 271 | field DAUGHTER_BOARD |
| 272 | option VARIANT_DB_ONE 2 |
| 273 | option VARIANT_DB_TWO 3 |
| 274 | end |
| 275 | end |
| 276 | ``` |
| 277 | |
| 278 | The result of this table defined in `devicetree.cb` is a list of |
| 279 | constants that can be used to check if fields match the firmware |
| 280 | configuration options determined at runtime with a simple check of the |
| 281 | field mask and the option value. |
| 282 | |
| 283 | |
| 284 | #### static.h |
| 285 | |
| 286 | ```c |
| 287 | /* field: FEATURE */ |
| 288 | #define FW_CONFIG_FIELD_FEATURE_NAME "FEATURE" |
| 289 | #define FW_CONFIG_FIELD_FEATURE_MASK 0x00000001 |
| 290 | #define FW_CONFIG_FIELD_FEATURE_OPTION_DISABLED_NAME "DISABLED" |
| 291 | #define FW_CONFIG_FIELD_FEATURE_OPTION_DISABLED_VALUE 0x00000000 |
| 292 | #define FW_CONFIG_FIELD_FEATURE_OPTION_ENABLED_NAME "ENABLED" |
| 293 | #define FW_CONFIG_FIELD_FEATURE_OPTION_ENABLED_VALUE 0x00000001 |
| 294 | |
| 295 | /* field: DAUGHTER_BOARD */ |
| 296 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_NAME "DAUGHTER_BOARD" |
| 297 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_MASK 0x00000006 |
| 298 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_NONE_NAME "NONE" |
| 299 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_NONE_VALUE 0x00000000 |
| 300 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_REFERENCE_DB_NAME "REFERENCE_DB" |
| 301 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_REFERENCE_DB_VALUE 0x00000002 |
| 302 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_ONE_NAME "VARIANT_DB_ONE" |
| 303 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_ONE_VALUE 0x00000004 |
| 304 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_TWO_NAME "VARIANT_DB_TWO" |
| 305 | #define FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_TWO_VALUE 0x00000006 |
| 306 | ``` |
| 307 | |
| 308 | |
| 309 | ## Device Probing |
| 310 | |
| 311 | One use of the firmware configuration interface in devicetree is to |
| 312 | allow device probing to be specified directly with the devices |
| 313 | themselves. A new `probe` token is introduced to allow a device to be |
| 314 | probed by field and option name. Multiple `probe` entries may be present |
| 315 | for each device and any successful probe will consider the device to be |
| 316 | present. |
| 317 | |
| 318 | |
| 319 | ### Probing Example |
| 320 | |
| 321 | Continuing with the previous example this device would be considered |
| 322 | present if the field `DAUGHTER_BOARD` was set to either `VARIANT_DB_ONE` |
| 323 | or `VARIANT_DB_TWO`: |
| 324 | |
| 325 | |
| 326 | #### overridetree.cb |
| 327 | |
| 328 | ```text |
| 329 | chip drivers/generic/example |
| 330 | device generic 0 on |
| 331 | probe DAUGHTER_BOARD VARIANT_DB_ONE |
| 332 | probe DAUGHTER_BOARD VARIANT_DB_TWO |
| 333 | end |
| 334 | end |
| 335 | ``` |
| 336 | |
| 337 | If the field were set to any other option, including `NONE` and |
| 338 | `REFERENCE_DB` and any undefined value then the device would be |
| 339 | disabled. |
| 340 | |
| 341 | |
| 342 | ### Probe Overrides |
| 343 | |
| 344 | When a device is declared with a probe in the baseboard `devicetree.cb` |
| 345 | and the same device is also present in the `overridetree.cb` then the |
| 346 | probing information from the baseboard is discarded and the override |
| 347 | device must provide all necessary probing information. |
| 348 | |
| 349 | In this example a device is listed in the baseboard with |
| 350 | `DAUGHTER_BOARD` field probing for `REFERENCE_DB` as a field option, It |
| 351 | is also defined as an override device with the field probing for the |
| 352 | `VARIANT_DB_ONE` option instead. |
| 353 | |
| 354 | In this case only the probe listed in the override is checked and a |
| 355 | field option of `REFERENCE_DB` will not mark this device present. If |
| 356 | both options are desired then the override device must list both. This |
| 357 | allows an override device to remove a probe entry that was defined in |
| 358 | the baseboard. |
| 359 | |
| 360 | |
| 361 | #### devicetree.cb |
| 362 | |
| 363 | ```text |
| 364 | chip drivers/generic/example |
| 365 | device generic 0 on |
| 366 | probe DAUGHTER_BOARD REFERENCE_DB |
| 367 | end |
| 368 | end |
| 369 | ``` |
| 370 | |
| 371 | |
| 372 | #### overridetree.cb |
| 373 | |
| 374 | ```text |
| 375 | chip drivers/generic/example |
| 376 | device generic 0 on |
| 377 | probe DAUGHTER_BOARD VARIANT_DB_ONE |
| 378 | end |
| 379 | end |
| 380 | ``` |
| 381 | |
| 382 | |
| 383 | ### Automatic Device Probing |
| 384 | |
| 385 | At boot time the firmware configuration interface will walk the device |
| 386 | tree and apply any probe entries that were defined in `devicetree.cb`. |
| 387 | This probing takes effect before the `BS_DEV_ENUMERATE` step during the |
| 388 | boot state machine in ramstage. |
| 389 | |
| 390 | Devices that have a probe list but do do not find a match are disabled |
| 391 | by setting `dev->enabled = 0` but the chip `enable_dev()` and device |
| 392 | `enable()` handlers will still be executed to allow any device disable |
| 393 | code to execute. |
| 394 | |
| 395 | The result of this probe definition is to provide an array of structures |
| 396 | describing each field and option to check. |
| 397 | |
| 398 | |
| 399 | #### fw_config.h |
| 400 | |
| 401 | ```c |
| 402 | /** |
| 403 | * struct fw_config - Firmware configuration field and option. |
| 404 | * @field_name: Name of the field that this option belongs to. |
| 405 | * @option_name: Name of the option within this field. |
| 406 | * @mask: Bitmask of the field. |
| 407 | * @value: Value of the option within the mask. |
| 408 | */ |
| 409 | struct fw_config { |
| 410 | const char *field_name; |
| 411 | const char *option_name; |
| 412 | uint64_t mask; |
| 413 | uint64_t value; |
| 414 | }; |
| 415 | ``` |
| 416 | |
| 417 | |
| 418 | #### static.c |
| 419 | |
| 420 | ```c |
| 421 | STORAGE struct fw_config __devN_probe_list[] = { |
| 422 | { |
| 423 | .field_name = FW_CONFIG_FIELD_DAUGHTER_BOARD_NAME, |
| 424 | .option_name = FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_ONE_NAME, |
| 425 | .mask = FW_CONFIG_FIELD_DAUGHTER_BOARD_MASK, |
| 426 | .value = FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_ONE_VALUE |
| 427 | }, |
| 428 | { |
| 429 | .field_name = FW_CONFIG_FIELD_DAUGHTER_BOARD_NAME, |
| 430 | .option_name = FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_TWO_NAME, |
| 431 | .mask = FW_CONFIG_FIELD_DAUGHTER_BOARD_MASK, |
| 432 | .value = FW_CONFIG_FIELD_DAUGHTER_BOARD_OPTION_VARIANT_DB_TWO_VALUE |
| 433 | }, |
| 434 | { } |
| 435 | }; |
| 436 | ``` |
| 437 | |
| 438 | |
| 439 | ### Runtime Probing |
| 440 | |
| 441 | The device driver probing allows for seamless integration with the |
| 442 | mainboard but it is only effective in ramstage and for specific devices |
| 443 | declared in devicetree.cb. There are other situations where code may |
| 444 | need to probe or check the value of a field in romstage or at other |
| 445 | points in ramstage. For this reason it is also possible to use the |
| 446 | firmware configuration interface directly. |
| 447 | |
| 448 | ```c |
| 449 | /** |
| 450 | * fw_config_probe() - Check if field and option matches. |
| 451 | * @match: Structure containing field and option to probe. |
| 452 | * |
| 453 | * Return %true if match is found, %false if match is not found. |
| 454 | */ |
| 455 | bool fw_config_probe(const struct fw_config *match); |
| 456 | ``` |
| 457 | |
| 458 | The argument provided to this function can be created from a macro for easy use: |
| 459 | |
| 460 | ```text |
| 461 | FW_CONFIG(field, option) |
| 462 | ``` |
| 463 | |
| 464 | This example has a mainboard check if a feature is disabled and set an |
| 465 | FSP UPD before memory training. This example expects that the default |
| 466 | value of this `register` is set to `true` in `devicetree.cb` and this |
| 467 | code is disabling that feature before FSP is executed. |
| 468 | |
| 469 | ```c |
| 470 | #include <fw_config.h> |
| 471 | |
| 472 | void mainboard_memory_init_params(FSPM_UPD *mupd) |
| 473 | { |
| 474 | if (fw_config_probe(FW_CONFIG(FEATURE, DISABLED)) |
| 475 | mupd->ExampleFeature = false; |
| 476 | } |
| 477 | ``` |