| 1 | # Device Operations in coreboot Firmware |
| 2 | |
| 3 | ## Introduction |
| 4 | |
| 5 | The `device_operations` structure is a cornerstone of coreboot's |
| 6 | hardware abstraction layer. It represents a set of function pointers |
| 7 | defining how the firmware interacts with a specific hardware device |
| 8 | during various boot stages. This structure lets the coreboot |
| 9 | architecture establish a consistent interface for device initialization, |
| 10 | configuration, resource allocation, and ACPI table generation, while |
| 11 | allowing device-specific implementations behind this common interface. |
| 12 | |
| 13 | At its core, `device_operations` applies the strategy pattern in |
| 14 | systems programming. It decouples algorithms (device operations) from |
| 15 | the core boot sequence, allowing devices to define their own behavior |
| 16 | while the boot process follows a predictable flow. This pattern enables |
| 17 | coreboot to support a wide range of hardware platforms with minimal |
| 18 | changes to the core boot sequence code. |
| 19 | |
| 20 | |
| 21 | ## Structure Definition |
| 22 | |
| 23 | The `device_operations` structure, defined in |
| 24 | `src/include/device/device.h`, consists of several function pointers, |
| 25 | each representing a specific operation performed on a device during |
| 26 | boot: |
| 27 | |
| 28 | ```c |
| 29 | struct device_operations { |
| 30 | void (*read_resources)(struct device *dev); |
| 31 | void (*set_resources)(struct device *dev); |
| 32 | void (*enable_resources)(struct device *dev); |
| 33 | void (*init)(struct device *dev); |
| 34 | void (*final)(struct device *dev); |
| 35 | void (*scan_bus)(struct device *bus); |
| 36 | void (*enable)(struct device *dev); |
| 37 | void (*vga_disable)(struct device *dev); |
| 38 | void (*reset_bus)(struct bus *bus); |
| 39 | |
| 40 | int (*get_smbios_data)(struct device *dev, int *handle, |
| 41 | unsigned long *current); |
| 42 | void (*get_smbios_strings)(struct device *dev, struct smbios_type11 *t); |
| 43 | |
| 44 | unsigned long (*write_acpi_tables)(const struct device *dev, |
| 45 | unsigned long start, struct acpi_rsdp *rsdp); |
| 46 | void (*acpi_fill_ssdt)(const struct device *dev); |
| 47 | const char *(*acpi_name)(const struct device *dev); |
| 48 | const char *(*acpi_hid)(const struct device *dev); |
| 49 | |
| 50 | const struct pci_operations *ops_pci; |
| 51 | const struct i2c_bus_operations *ops_i2c_bus; |
| 52 | const struct spi_bus_operations *ops_spi_bus; |
| 53 | const struct smbus_bus_operations *ops_smbus_bus; |
| 54 | const struct pnp_mode_ops *ops_pnp_mode; |
| 55 | const struct gpio_operations *ops_gpio; |
| 56 | const struct mdio_bus_operations *ops_mdio; |
| 57 | }; |
| 58 | ``` |
| 59 | |
| 60 | |
| 61 | ### Core Resource Management Functions |
| 62 | |
| 63 | * `read_resources`: Discovers and collects resources required by the |
| 64 | device (I/O ports, memory regions, IRQs, etc.). This typically |
| 65 | involves reading configuration registers or static device tree |
| 66 | information. |
| 67 | * `set_resources`: Assigns finalized resources to the device after the |
| 68 | resource allocation phase. This writes the assigned base addresses, |
| 69 | lengths, and other parameters back to the device structure, but not |
| 70 | necessarily to hardware registers yet. |
| 71 | * `enable_resources`: Activates the assigned resources in the device's |
| 72 | hardware registers (e.g., writing to PCI BARs, enabling memory |
| 73 | decoding). |
| 74 | |
| 75 | |
| 76 | ### Device Lifecycle Functions |
| 77 | |
| 78 | * `init`: Performs device-specific initialization, often requiring |
| 79 | access to the device's assigned resources. This is called after |
| 80 | resources have been enabled. |
| 81 | * `final`: Executes final setup or cleanup operations before the |
| 82 | payload is loaded. This is useful for tasks that depend on other |
| 83 | devices being initialized. |
| 84 | * `enable`: Enables the device in the hardware, often setting bits in |
| 85 | configuration registers to make the device active. Called after |
| 86 | `enable_resources`. |
| 87 | |
| 88 | |
| 89 | ### Bus Management Functions |
| 90 | |
| 91 | * `scan_bus`: Enumerates child devices on a bus device (e.g., scanning |
| 92 | a PCI bus for devices, probing I2C devices). Only applicable to |
| 93 | devices that act as buses. |
| 94 | * `reset_bus`: Resets all devices on a specific bus. |
| 95 | * `vga_disable`: Disables VGA decoding on a PCI device when another VGA |
| 96 | device is active. Used to manage legacy VGA resources. |
| 97 | |
| 98 | |
| 99 | ### System Table Generation Functions |
| 100 | |
| 101 | * `get_smbios_data`: Provides SMBIOS data specific to the device for |
| 102 | Type 9 (System Slots) or Type 41 (Onboard Devices Extended |
| 103 | Information). |
| 104 | * `get_smbios_strings`: Supplies string information for SMBIOS tables, |
| 105 | often related to the data provided by `get_smbios_data`. |
| 106 | * `write_acpi_tables`: Generates device-specific ACPI tables (like SSDTs) |
| 107 | or contributes data to system-wide tables. |
| 108 | * `acpi_fill_ssdt`: Adds device-specific objects (scopes, methods, data) |
| 109 | to the Secondary System Description Table (SSDT). |
| 110 | * `acpi_name`: Returns the ACPI name for the device (e.g., |
| 111 | `\_SB.PCI0.GFX0`). This defines the device's path in the ACPI |
| 112 | namespace. |
| 113 | * `acpi_hid`: Returns the ACPI Hardware ID (HID) for the device (e.g., |
| 114 | `PNP0A08`). Used by the OS to match drivers. |
| 115 | |
| 116 | |
| 117 | ### Bus-Specific Operation Pointers |
| 118 | |
| 119 | These fields point to bus-specific operation structures when a device |
| 120 | functions as a bus controller (or exposes bus-like functionality). See |
| 121 | the "Bus-Specific Operations" section for details. |
| 122 | |
| 123 | * `ops_pci`: Operations for PCI configuration space access. |
| 124 | * `ops_i2c_bus`: Operations for I2C bus transactions (read, write, |
| 125 | transfer). |
| 126 | * `ops_spi_bus`: Operations for SPI bus transactions. |
| 127 | * `ops_smbus_bus`: Operations for SMBus transactions. |
| 128 | * `ops_pnp_mode`: Operations for Plug-and-Play device configuration. |
| 129 | * `ops_gpio`: Operations for GPIO control (get, set, configure |
| 130 | direction/pulls). |
| 131 | * `ops_mdio`: Operations for MDIO (Management Data Input/Output) bus |
| 132 | access, used for Ethernet PHYs. |
| 133 | |
| 134 | |
| 135 | ## Device Lifecycle in coreboot |
| 136 | |
| 137 | The function pointers in `device_operations` are called at specific |
| 138 | stages during the boot process, following a sequence defined in |
| 139 | coreboot's boot state machine (`src/lib/hardwaremain.c`). Understanding |
| 140 | this lifecycle helps developers implement appropriate behavior for each |
| 141 | function pointer. |
| 142 | |
| 143 | |
| 144 | ### Boot Sequence and Device Operations |
| 145 | |
| 146 | coreboot's main device initialization sequence involves these boot |
| 147 | states: |
| 148 | |
| 149 | 1. **BS_DEV_INIT_CHIPS** (`dev_initialize_chips()`): Initializes chip |
| 150 | drivers (`chip_operations`). |
| 151 | 2. **BS_DEV_ENUMERATE** (`dev_enumerate()`): Discovers and enumerates |
| 152 | devices. |
| 153 | * Calls `scan_bus()` for each bus to detect child devices. |
| 154 | 3. **BS_DEV_RESOURCES** (`dev_configure()`): Allocates resources across |
| 155 | all enumerated devices. |
| 156 | * Calls `read_resources()` for each device to discover required |
| 157 | resources. |
| 158 | * Calls `set_resources()` for each device to assign allocated |
| 159 | resources back to the `struct device`. |
| 160 | 4. **BS_DEV_ENABLE** (`dev_enable()`): Enables devices and their |
| 161 | resources. |
| 162 | * Calls `enable_resources()` for each device to activate assigned |
| 163 | resources in hardware. |
| 164 | * Calls `enable()` for each device to perform general hardware |
| 165 | enablement. |
| 166 | 5. **BS_DEV_INIT** (`dev_initialize()`): Initializes devices. |
| 167 | * Calls `init()` for each device to perform device-specific setup. |
| 168 | 6. **BS_POST_DEVICE** (`dev_finalize()`): Finalizes devices before |
| 169 | payload loading. |
| 170 | * Calls `final()` for each device for any final cleanup or setup. |
| 171 | |
| 172 | The sequence is primarily driven by the `boot_states` array in |
| 173 | `src/lib/hardwaremain.c`: |
| 174 | |
| 175 | ```c |
| 176 | static struct boot_state boot_states[] = { |
| 177 | /* ... other states ... */ |
| 178 | BS_INIT_ENTRY(BS_PRE_DEVICE, bs_pre_device), |
| 179 | BS_INIT_ENTRY(BS_DEV_INIT_CHIPS, bs_dev_init_chips), |
| 180 | BS_INIT_ENTRY(BS_DEV_ENUMERATE, bs_dev_enumerate), |
| 181 | BS_INIT_ENTRY(BS_DEV_RESOURCES, bs_dev_resources), |
| 182 | BS_INIT_ENTRY(BS_DEV_ENABLE, bs_dev_enable), |
| 183 | BS_INIT_ENTRY(BS_DEV_INIT, bs_dev_init), |
| 184 | BS_INIT_ENTRY(BS_POST_DEVICE, bs_post_device), |
| 185 | /* ... other states ... */ |
| 186 | }; |
| 187 | ``` |
| 188 | |
| 189 | Later stages include ACPI and SMBIOS table generation, where functions |
| 190 | like `write_acpi_tables()`, `acpi_fill_ssdt()`, `get_smbios_data()`, and |
| 191 | `get_smbios_strings()` are invoked as part of the table construction |
| 192 | process. |
| 193 | |
| 194 | |
| 195 | ## Inheritance and Code Reuse Patterns |
| 196 | |
| 197 | The `device_operations` structure enables several patterns for code |
| 198 | reuse: |
| 199 | |
| 200 | |
| 201 | ### 1. Default Implementations |
| 202 | |
| 203 | coreboot provides default implementations for common device types (like |
| 204 | root devices, PCI devices, PCI bridges), which can be used directly or |
| 205 | extended. Chip or mainboard code often assigns these defaults if no |
| 206 | specific driver is found. |
| 207 | |
| 208 | ```c |
| 209 | /* From src/device/root_device.c */ |
| 210 | struct device_operations default_dev_ops_root = { |
| 211 | .read_resources = noop_read_resources, |
| 212 | .set_resources = noop_set_resources, |
| 213 | .scan_bus = scan_static_bus, |
| 214 | .reset_bus = root_dev_reset, |
| 215 | #if CONFIG(HAVE_ACPI_TABLES) |
| 216 | .acpi_name = root_dev_acpi_name, |
| 217 | #endif |
| 218 | }; |
| 219 | ``` |
| 220 | |
| 221 | |
| 222 | ### 2. No-op Functions |
| 223 | |
| 224 | Simple shim functions (often static inline) are provided for cases where |
| 225 | a device doesn't need to implement specific operations. Using these avoids |
| 226 | leaving function pointers NULL. |
| 227 | |
| 228 | ```c |
| 229 | /* From src/include/device/device.h */ |
| 230 | static inline void noop_read_resources(struct device *dev) {} |
| 231 | static inline void noop_set_resources(struct device *dev) {} |
| 232 | ``` |
| 233 | |
| 234 | |
| 235 | ### 3. Chain of Responsibility / Delegation |
| 236 | |
| 237 | Some implementations delegate to parent devices or use helper functions |
| 238 | when they can't handle an operation themselves or when common logic can |
| 239 | be shared. For example, ACPI name generation often traverses up the |
| 240 | device tree. |
| 241 | |
| 242 | ```c |
| 243 | /* Simplified example logic */ |
| 244 | const char *acpi_device_name(const struct device *dev) |
| 245 | { |
| 246 | const char *name = NULL; |
| 247 | const struct device *pdev = dev; |
| 248 | |
| 249 | /* Check for device specific handler */ |
| 250 | if (dev->ops && dev->ops->acpi_name) { |
| 251 | name = dev->ops->acpi_name(dev); |
| 252 | if (name) |
| 253 | return name; /* Device handled it */ |
| 254 | } |
| 255 | |
| 256 | /* Walk up the tree to find if any parent can provide a name */ |
| 257 | while (pdev->upstream && pdev->upstream->dev) { |
| 258 | pdev = pdev->upstream->dev; |
| 259 | if (pdev->ops && pdev->ops->acpi_name) { |
| 260 | /* Note: Parent's acpi_name might handle the original child 'dev' */ |
| 261 | name = pdev->ops->acpi_name(dev); |
| 262 | if (name) |
| 263 | return name; /* Parent handled it */ |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* Fallback or default logic if needed */ |
| 268 | return NULL; |
| 269 | } |
| 270 | ``` |
| 271 | This pattern allows parent devices (like buses) to provide default |
| 272 | behavior or naming schemes if a child device doesn't specify its own. |
| 273 | |
| 274 | |
| 275 | ## Implementation Examples |
| 276 | |
| 277 | These examples show typical `device_operations` assignments. Actual |
| 278 | implementations might involve more conditional compilation based on |
| 279 | Kconfig options. |
| 280 | |
| 281 | |
| 282 | ### PCI Device Operations (Default) |
| 283 | |
| 284 | ```c |
| 285 | /* From src/device/pci_device.c */ |
| 286 | struct device_operations default_pci_ops_dev = { |
| 287 | .read_resources = pci_dev_read_resources, |
| 288 | .set_resources = pci_dev_set_resources, |
| 289 | .enable_resources = pci_dev_enable_resources, |
| 290 | #if CONFIG(HAVE_ACPI_TABLES) |
| 291 | .write_acpi_tables = pci_rom_write_acpi_tables, |
| 292 | .acpi_fill_ssdt = pci_rom_ssdt, |
| 293 | #endif |
| 294 | .init = pci_dev_init, |
| 295 | /* Assigns PCI-specific operations */ |
| 296 | .ops_pci = &pci_dev_ops_pci, |
| 297 | }; |
| 298 | ``` |
| 299 | |
| 300 | |
| 301 | ### CPU Cluster Operations |
| 302 | |
| 303 | ```c |
| 304 | /* From src/soc/intel/alderlake/chip.c (representative example) */ |
| 305 | static struct device_operations cpu_bus_ops = { |
| 306 | .read_resources = noop_read_resources, |
| 307 | .set_resources = noop_set_resources, |
| 308 | .enable_resources = cpu_set_north_irqs, |
| 309 | #if CONFIG(HAVE_ACPI_TABLES) |
| 310 | .acpi_fill_ssdt = cpu_fill_ssdt, |
| 311 | #endif |
| 312 | /* CPU clusters often don't need scan_bus, init, etc. */ |
| 313 | }; |
| 314 | ``` |
| 315 | |
| 316 | |
| 317 | ### GPIO Controller Operations |
| 318 | |
| 319 | ```c |
| 320 | /* From src/soc/intel/common/block/gpio/gpio_dev.c */ |
| 321 | static struct gpio_operations gpio_ops = { |
| 322 | .get = gpio_get, |
| 323 | .set = gpio_set, |
| 324 | /* ... other GPIO functions ... */ |
| 325 | }; |
| 326 | |
| 327 | struct device_operations block_gpio_ops = { |
| 328 | .read_resources = noop_read_resources, |
| 329 | .set_resources = noop_set_resources, |
| 330 | /* Assigns GPIO-specific operations */ |
| 331 | .ops_gpio = &gpio_ops, |
| 332 | }; |
| 333 | ``` |
| 334 | |
| 335 | |
| 336 | ## Registration and Discovery |
| 337 | |
| 338 | How are `device_operations` structures associated with `struct device` |
| 339 | instances? |
| 340 | |
| 341 | |
| 342 | ### 1. Static Assignment (via `chip_operations`) |
| 343 | |
| 344 | For devices known at build time (defined in devicetree.cb), the |
| 345 | `device_operations` structure is often assigned in the SOC's or |
| 346 | mainboard's `chip_operations->enable_dev()` function based on the |
| 347 | device path type or other properties. |
| 348 | |
| 349 | ```c |
| 350 | /* Example from src/soc/intel/alderlake/chip.c */ |
| 351 | static void soc_enable(struct device *dev) |
| 352 | { |
| 353 | /* Assign ops based on the device's role in the tree */ |
| 354 | if (dev->path.type == DEVICE_PATH_DOMAIN) |
| 355 | dev->ops = &pci_domain_ops; /* Handles PCI domain resources */ |
| 356 | else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) |
| 357 | dev->ops = &cpu_bus_ops; /* Handles CPU cluster setup */ |
| 358 | else if (dev->path.type == DEVICE_PATH_GPIO) |
| 359 | block_gpio_enable(dev); /* Assigns block_gpio_ops */ |
| 360 | /* ... other assignments for specific PCI devices, etc. ... */ |
| 361 | } |
| 362 | ``` |
| 363 | The `enable_dev` function is part of `struct chip_operations`, which |
| 364 | handles broader chip-level initialization (see "Relationship with |
| 365 | `chip_operations`" section). |
| 366 | |
| 367 | |
| 368 | ### 2. Dynamic Detection (PCI Drivers) |
| 369 | |
| 370 | For PCI devices discovered during bus scanning (`scan_bus`), coreboot |
| 371 | looks through a list of registered PCI drivers (`_pci_drivers` array) |
| 372 | to find one matching the device's vendor and device IDs. |
| 373 | |
| 374 | ```c |
| 375 | /* Logic from src/device/pci_device.c::set_pci_ops() */ |
| 376 | static void set_pci_ops(struct device *dev) |
| 377 | { |
| 378 | struct pci_driver *driver; |
| 379 | |
| 380 | /* Check if ops already assigned (e.g., by chip_ops->enable_dev) */ |
| 381 | if (dev->ops) |
| 382 | return; |
| 383 | |
| 384 | /* Look through registered PCI drivers */ |
| 385 | for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) { |
| 386 | if ((driver->vendor == dev->vendor) && |
| 387 | device_id_match(driver, dev->device)) { |
| 388 | /* Found a matching driver, assign its ops */ |
| 389 | dev->ops = (struct device_operations *)driver->ops; |
| 390 | printk(BIOS_SPEW, "%s: Assigned ops from driver for %04x:%04x\n", |
| 391 | dev_path(dev), driver->vendor, driver->device); |
| 392 | return; /* Stop searching */ |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* Fall back to default operations if no specific driver found */ |
| 397 | if (!dev->ops) { |
| 398 | if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) { |
| 399 | dev->ops = get_pci_bridge_ops(dev); /* Special ops for bridges */ |
| 400 | } else { |
| 401 | dev->ops = &default_pci_ops_dev; /* Default for normal devices */ |
| 402 | } |
| 403 | printk(BIOS_SPEW, "%s: Assigned default PCI ops\n", dev_path(dev)); |
| 404 | } |
| 405 | } |
| 406 | ``` |
| 407 | |
| 408 | |
| 409 | ## Build Process Integration |
| 410 | |
| 411 | The `device_operations` structures are integrated into the coreboot |
| 412 | build process: |
| 413 | |
| 414 | 1. **Static Device Tree**: The mainboard's `devicetree.cb` defines the |
| 415 | initial device hierarchy. The build process converts this into C |
| 416 | code (`static.c`) containing `struct device` instances. |
| 417 | 2. **PCI Driver Registration**: PCI drivers (containing their own |
| 418 | `device_operations`) register themselves using the `__pci_driver` |
| 419 | linker set macro. |
| 420 | |
| 421 | ```c |
| 422 | /* Example pattern */ |
| 423 | struct pci_driver example_pci_driver __pci_driver = { |
| 424 | .ops = &example_device_ops, /* Pointer to device_operations */ |
| 425 | .vendor = VENDOR_ID, |
| 426 | .device = DEVICE_ID, /* Or .devices for a list */ |
| 427 | }; |
| 428 | ``` |
| 429 | 3. **Linking**: The build system collects all structures placed in the |
| 430 | `__pci_driver` section and creates the `_pci_drivers` array used by |
| 431 | `set_pci_ops()`. It ensures all necessary code (default ops, driver |
| 432 | ops, core device functions) is linked into the final firmware image. |
| 433 | |
| 434 | |
| 435 | ## Relationship with `chip_operations` |
| 436 | |
| 437 | It's important to distinguish `device_operations` from |
| 438 | `chip_operations` (defined in `src/include/chip.h`). |
| 439 | |
| 440 | * `chip_operations`: Defines operations related to the overall chipset |
| 441 | or mainboard logic. It includes functions called earlier in the boot |
| 442 | process, like `enable_dev`, `init`, and `final`. |
| 443 | * `chip_operations->enable_dev()` is crucial as it often performs |
| 444 | initial setup for static devices and is the primary place where |
| 445 | `device_operations` pointers are *assigned* for non-PCI devices |
| 446 | based on their path or type. |
| 447 | * `chip_operations->init()` runs during `BS_DEV_INIT_CHIPS`, before |
| 448 | most `device_operations` functions. |
| 449 | * `device_operations`: Defines operations for *individual* devices |
| 450 | within the device tree. These are called *after* the corresponding |
| 451 | `chip_operations` stage and operate on a specific `struct device`. |
| 452 | |
| 453 | Essentially, `chip_operations` sets the stage at the SoC/mainboard level, |
| 454 | including assigning the correct `device_operations` to static devices, |
| 455 | while `device_operations` handles the specific actions for each device |
| 456 | later in the boot process. |
| 457 | |
| 458 | |
| 459 | ## Bus-Specific Operations |
| 460 | |
| 461 | The `ops_*` pointers within `struct device_operations` (e.g., `ops_pci`, |
| 462 | `ops_i2c_bus`, `ops_spi_bus`, `ops_gpio`) provide a way for devices that |
| 463 | act as bus controllers or expose bus-like interfaces to offer |
| 464 | standardized access methods. |
| 465 | |
| 466 | * **Purpose:** They abstract the low-level details of interacting with |
| 467 | that specific bus type. For example, a PCI host bridge device will |
| 468 | implement `struct pci_operations` via its `ops_pci` pointer, |
| 469 | allowing other code to perform PCI config reads/writes through it |
| 470 | without knowing the exact hardware mechanism. Similarly, an I2C |
| 471 | controller device implements `struct i2c_bus_operations` via |
| 472 | `ops_i2c_bus` to provide standard `read`, `write`, and `transfer` |
| 473 | functions for that bus segment. |
| 474 | * **Usage:** Code needing to interact with a bus first finds the |
| 475 | controller `struct device` in the tree, then accesses the relevant |
| 476 | bus operations through the appropriate `ops_*` pointer, passing the |
| 477 | target address or parameters. For instance, to talk to an I2C device |
| 478 | at address `0x50` on the bus controlled by `i2c_controller_dev`, one |
| 479 | might call: |
| 480 | `i2c_controller_dev->ops->ops_i2c_bus->transfer(...)`. Helper |
| 481 | functions often wrap this access pattern. |
| 482 | * **Implementation:** The structures like `struct pci_operations`, |
| 483 | `struct i2c_bus_operations`, etc., are defined in corresponding |
| 484 | header files (e.g., `src/include/device/pci_ops.h`, |
| 485 | `src/include/drivers/i2c/i2c_bus.h`). Devices acting as controllers |
| 486 | provide concrete implementations of these functions, tailored to their |
| 487 | hardware. |
| 488 | |
| 489 | This mechanism allows coreboot to manage diverse bus types using a |
| 490 | consistent device model, where the controller device itself exposes the |
| 491 | necessary functions for interacting with devices on its bus. |
| 492 | |
| 493 | |
| 494 | ## Best Practices |
| 495 | |
| 496 | When implementing `device_operations`: |
| 497 | |
| 498 | 1. **Leverage Defaults/No-ops**: Use default or no-op implementations |
| 499 | whenever possible. Only override functions that require custom |
| 500 | behavior for your specific device. |
| 501 | 2. **Error Handling**: Check return values from functions called within |
| 502 | your ops implementations and handle errors gracefully (e.g., log an |
| 503 | error, return an error code if applicable). |
| 504 | 3. **Resource Management**: In `read_resources`, accurately declare all |
| 505 | resources (MMIO, I/O ports, IRQs) your device needs, specifying |
| 506 | flags like fixed vs. alignment, or bridge vs. standard device. |
| 507 | Incorrect resource declaration is a common source of issues. |
| 508 | 4. **Initialization Order**: Be mindful of dependencies in `init`. If |
| 509 | your device relies on another device being fully initialized, consider |
| 510 | deferring that part of the initialization to the `final` callback, |
| 511 | which runs later. |
| 512 | 5. **Minimal Implementation**: Only implement the functions relevant to |
| 513 | your device type. A simple MMIO device might only need |
| 514 | `read_resources`, `set_resources`, `enable_resources`, and perhaps |
| 515 | ACPI functions. A bus device additionally needs `scan_bus`. |
| 516 | 6. **Bus Operations**: If implementing a bus controller, correctly |
| 517 | implement the corresponding bus operations structure (e.g., |
| 518 | `struct pci_operations`, `struct i2c_bus_operations`) and assign it |
| 519 | to the appropriate `ops_*` field. |
| 520 | 7. **ACPI/SMBIOS**: If the device needs OS visibility via ACPI or |
| 521 | SMBIOS, implement the relevant functions (`acpi_name`, `acpi_hid`, |
| 522 | `acpi_fill_ssdt`, `get_smbios_data`, etc.). Ensure ACPI names and |
| 523 | HIDs are correct according to specifications and platform needs. |
| 524 | |
| 525 | |
| 526 | ## Logging and Debugging |
| 527 | |
| 528 | Use coreboot's logging facilities (`printk`) within your `device_operations` |
| 529 | functions to provide visibility during development and debugging. Use |
| 530 | appropriate log levels (e.g., `BIOS_DEBUG`, `BIOS_INFO`, `BIOS_ERR`). |
| 531 | |
| 532 | ```c |
| 533 | static void example_device_init(struct device *dev) |
| 534 | { |
| 535 | printk(BIOS_DEBUG, "%s: Initializing device at %s\n", __func__, |
| 536 | dev_path(dev)); |
| 537 | |
| 538 | /* ... Device initialization code ... */ |
| 539 | if (/* some condition */) { |
| 540 | printk(BIOS_SPEW, "%s: Condition met, applying setting X\n", |
| 541 | dev_path(dev)); |
| 542 | /* ... */ |
| 543 | } |
| 544 | |
| 545 | if (/* error condition */) { |
| 546 | printk(BIOS_ERR, "%s: Failed to initialize feature Y!\n", |
| 547 | dev_path(dev)); |
| 548 | /* Handle error */ |
| 549 | } |
| 550 | |
| 551 | printk(BIOS_DEBUG, "%s: Initialization complete for %s\n", __func__, |
| 552 | dev_path(dev)); |
| 553 | } |
| 554 | ``` |
| 555 | Consistent logging helps trace the boot process and pinpoint where issues |
| 556 | occur. |
| 557 | |
| 558 | |
| 559 | ## Common Troubleshooting |
| 560 | |
| 561 | * **Missing Resource Declarations**: |
| 562 | * *Problem*: Device fails to function, or conflicts arise because a |
| 563 | required resource (MMIO range, I/O port, IRQ) was not declared |
| 564 | in `read_resources`. The resource allocator is unaware of the |
| 565 | need. |
| 566 | * *Solution*: Verify that `read_resources` correctly calls functions |
| 567 | like `pci_dev_read_resources` or manually adds all necessary |
| 568 | resources using functions like `mmio_resource()`, |
| 569 | `io_resource()`, etc. Check PCI BARs or device datasheets. |
| 570 | * **Initialization Order Issues**: |
| 571 | * *Problem*: `init()` fails because it depends on another device |
| 572 | that hasn't been fully initialized yet (e.g., accessing a shared |
| 573 | resource like SMBus before the SMBus controller is ready). |
| 574 | * *Solution*: Move the dependent initialization code to the `final` |
| 575 | callback if possible. Alternatively, ensure the dependency is met |
| 576 | by careful ordering in the device tree or using boot state |
| 577 | callbacks if necessary for complex scenarios. |
| 578 | * **Resource Conflicts**: |
| 579 | * *Problem*: Boot fails during resource allocation, or devices |
| 580 | misbehave because multiple devices requested the same |
| 581 | non-sharable resource (e.g., conflicting fixed MMIO regions). |
| 582 | * *Solution*: Review resource declarations in `read_resources` across |
| 583 | all relevant devices. Ensure fixed resources don't overlap. Check |
| 584 | if bridge windows are correctly defined and large enough. Use |
| 585 | coreboot's resource reporting logs to identify overlaps. |
| 586 | * **ACPI Table Generation Errors**: |
| 587 | * *Problem*: The operating system fails to recognize the device, |
| 588 | assigns the wrong driver, or the device doesn't function correctly |
| 589 | (e.g., power management issues). |
| 590 | * *Solution*: Double-check the `acpi_name`, `acpi_hid`, `_CRS` |
| 591 | (generated from assigned resources), and `acpi_fill_ssdt` |
| 592 | implementations. Verify names match the ACPI hierarchy and HIDs |
| 593 | match expected driver bindings. Ensure SSDT methods correctly |
| 594 | access hardware. Use OS debugging tools (e.g., `acpidump`, Device |
| 595 | Manager errors) to diagnose. |
| 596 | * **Incorrect `ops` Pointer Assigned**: |
| 597 | * *Problem*: Device behaves incorrectly because the wrong |
| 598 | `device_operations` structure was assigned (e.g., default PCI ops |
| 599 | assigned to a device needing a specific driver's ops). |
| 600 | * *Solution*: Check the logic in `chip_operations->enable_dev` (for |
| 601 | static devices) or the PCI driver registration (`__pci_driver` |
| 602 | macro and `set_pci_ops` fallback logic) to ensure the correct |
| 603 | `ops` structure is being selected and assigned based on device |
| 604 | type, path, or PCI ID. Add debug prints to verify which `ops` |
| 605 | structure is assigned. |
| 606 | |
| 607 | |
| 608 | ## Advanced Usage |
| 609 | |
| 610 | ### Complex Device Hierarchies |
| 611 | |
| 612 | For devices with non-standard interactions or complex initialization, |
| 613 | custom `device_operations` can be created, often inheriting from defaults |
| 614 | but overriding specific functions. |
| 615 | |
| 616 | ```c |
| 617 | static void advanced_device_init(struct device *dev) |
| 618 | { |
| 619 | /* First, perform standard PCI init */ |
| 620 | pci_dev_init(dev); |
| 621 | |
| 622 | /* Then, add custom initialization steps */ |
| 623 | printk(BIOS_DEBUG, "%s: Performing advanced init\n", dev_path(dev)); |
| 624 | /* ... custom register writes, configuration ... */ |
| 625 | } |
| 626 | |
| 627 | static const char *advanced_device_acpi_name(const struct device *dev) |
| 628 | { |
| 629 | /* Provide a custom ACPI name based on some property */ |
| 630 | if (/* condition */) |
| 631 | return "ADV0001"; |
| 632 | else |
| 633 | return "ADV0002"; |
| 634 | } |
| 635 | |
| 636 | /* Combine default and custom operations */ |
| 637 | static struct device_operations advanced_device_ops = { |
| 638 | /* Inherit resource handling from default PCI ops */ |
| 639 | .read_resources = pci_dev_read_resources, |
| 640 | .set_resources = pci_dev_set_resources, |
| 641 | .enable_resources = pci_dev_enable_resources, |
| 642 | |
| 643 | /* Override init */ |
| 644 | .init = advanced_device_init, |
| 645 | |
| 646 | /* Override ACPI naming */ |
| 647 | .acpi_name = advanced_device_acpi_name, |
| 648 | /* Other functions might use defaults or no-ops */ |
| 649 | }; |
| 650 | ``` |
| 651 | |
| 652 | ### Dynamic Configuration based on Probing |
| 653 | |
| 654 | Some `init` or other op implementations might probe the device's |
| 655 | capabilities or read configuration data (e.g., from SPD, VPD, or straps) |
| 656 | and alter their behavior accordingly. |
| 657 | |
| 658 | ```c |
| 659 | static void conditional_device_init(struct device *dev) |
| 660 | { |
| 661 | uint8_t feature_flags; |
| 662 | |
| 663 | /* Read capability register from the device */ |
| 664 | feature_flags = pci_read_config8(dev, EXAMPLE_CAP_REG); |
| 665 | |
| 666 | printk(BIOS_DEBUG, "%s: Feature flags: 0x%02x\n", dev_path(dev), |
| 667 | feature_flags); |
| 668 | |
| 669 | /* Conditional initialization based on detected features */ |
| 670 | if (feature_flags & FEATURE_X_ENABLED) { |
| 671 | printk(BIOS_INFO, "%s: Initializing Feature X\n", dev_path(dev)); |
| 672 | init_feature_x(dev); |
| 673 | } |
| 674 | if (feature_flags & FEATURE_Y_ENABLED) { |
| 675 | printk(BIOS_INFO, "%s: Initializing Feature Y\n", dev_path(dev)); |
| 676 | init_feature_y(dev); |
| 677 | } |
| 678 | } |
| 679 | ``` |
| 680 | |
| 681 | |
| 682 | ## Conclusion |
| 683 | |
| 684 | The `device_operations` structure is a powerful abstraction mechanism in |
| 685 | coreboot. It enables consistent handling of diverse hardware while |
| 686 | allowing for device-specific behavior. By providing a standard interface |
| 687 | for device operations throughout the boot process, it simplifies the |
| 688 | codebase, enhances maintainability, and provides the extensibility needed |
| 689 | to support new hardware platforms. |
| 690 | |
| 691 | Understanding this structure, its relationship with `chip_operations`, |
| 692 | and its role in the boot process is essential for coreboot developers, |
| 693 | particularly when adding support for new devices or debugging hardware |
| 694 | initialization issues. By following the patterns and best practices |
| 695 | outlined here, developers can create robust and reusable device driver |
| 696 | implementations that integrate smoothly into the coreboot architecture. |