Debian Codegraph @

Documentation/internals/chip_operations.md

raw symbols SCIP
1 # The `chip_operations` Structure in coreboot
2
3 ## Introduction
4
5 The `chip_operations` structure is a fundamental component of coreboot's
6 chipset abstraction layer. It provides a standardized interface for chipset-
7 specific code to interact with coreboot's device initialization framework.
8 This structure enables coreboot to support a wide variety of chipsets
9 while maintaining a consistent initialization flow across different hardware
10 platforms.
11
12 In coreboot's architecture, a "chip" refers to a collection of hardware
13 components that form a logical unit, such as a System-on-Chip (SoC),
14 a CPU, or a distinct southbridge/northbridge. The `chip_operations`
15 structure provides the hooks necessary for coreboot to discover, configure,
16 and initialize these components during the boot process.
17
18 The `chip_operations` structure is particularly crucial for the ramstage
19 portion of coreboot, where it connects the static device tree definitions
20 with the actual hardware initialization code. It serves as the bridge
21 between the declarative device descriptions and the imperative code that
22 brings those devices to life.
23
24
25 ## Structure Definition
26
27 The `chip_operations` structure is defined in `src/include/device/device.h`
28 as follows:
29
30 ```c
31 struct chip_operations {
32 void (*enable_dev)(struct device *dev);
33 void (*init)(void *chip_info);
34 void (*final)(void *chip_info);
35 unsigned int initialized : 1;
36 unsigned int finalized : 1;
37 const char *name;
38 };
39 ```
40
41
42 ### Field Descriptions
43
44 - **enable_dev**: A function pointer that takes a `struct device*`
45 parameter. This function is called for each device associated with the
46 chip during the device enumeration phase (specifically, within the
47 `scan_bus` operations triggered by `dev_enumerate`). Its primary
48 purpose is to set up device operations (`dev->ops`) based on the
49 device's role in the system.
50
51 - **init**: A function pointer that takes a `void*` parameter pointing to
52 the chip's configuration data (typically cast to a chip-specific struct).
53 This function is called during the chip initialization phase
54 (`BS_DEV_INIT_CHIPS`), before device enumeration. It usually performs
55 early hardware setup needed before individual devices can be configured.
56
57 - **final**: A function pointer that takes a `void*` parameter pointing to
58 the chip's configuration data (typically cast to a chip-specific struct).
59 This function is called during the final table writing phase of coreboot
60 initialization (`BS_WRITE_TABLES`), after all devices have been
61 initialized. It performs any necessary cleanup or late initialization
62 operations.
63
64 - **initialized**: A bit flag indicating whether the chip's init function
65 has been called.
66
67 - **finalized**: A bit flag indicating whether the chip's final function
68 has been called.
69
70 - **name**: A string containing the human-readable name of the chip, used
71 for debugging and logging purposes.
72
73
74 ## Initialization Sequence and `chip_operations`
75
76 The `chip_operations` structure integrates with coreboot's boot state
77 machine, which is defined in `src/lib/hardwaremain.c`. The functions in
78 this structure are called at specific points during the boot process:
79
80 1. **BS_DEV_INIT_CHIPS** state: The `init` function is called for each
81 chip in the device tree. This is handled by `dev_initialize_chips()`
82 which iterates through all devices, identifies unique chip instances,
83 and invokes their `init` functions.
84
85 2. **BS_DEV_ENUMERATE** state: During the execution of this state,
86 `dev_enumerate()` is called, which triggers bus scanning
87 (e.g., `pci_scan_bus`). Within these scan routines, the `enable_dev`
88 function is called for devices associated with a chip. This commonly
89 assigns the appropriate `device_operations` structure to each device
90 based on its type and purpose.
91
92 3. **BS_WRITE_TABLES** state: The `final` function is called for each
93 chip by `dev_finalize_chips()` after all devices have been initialized
94 and just before payloads are loaded.
95
96 This sequence ensures that chips can perform necessary setup before their
97 individual devices are configured, and also perform cleanup or finalization
98 after all devices have been initialized but before the final tables are
99 written and the payload is executed.
100
101
102 ## Relationship Between `chip_operations` and `device_operations`
103
104 It's important to understand the distinction and relationship between
105 `chip_operations` and `device_operations`:
106
107 - **chip_operations**: Operates at the chipset or SoC level, providing
108 hooks for chip-wide initialization. It's responsible for the overall
109 setup of a collection of devices that belong to the same logical chip.
110
111 - **device_operations**: Operates at the individual device level,
112 providing functions to manage specific devices within a chip. These
113 operations include resource allocation, device initialization, and device-
114 specific functionality.
115
116 The key relationship is that `chip_operations.enable_dev` is typically
117 responsible for assigning the appropriate `device_operations` structure
118 to each device based on its type and function. This is where the bridge
119 between the chip-level and device-level abstractions occurs.
120
121 For example, a typical implementation of the `enable_dev` function might
122 look like this:
123
124 ```c
125 static void soc_enable(struct device *dev)
126 {
127 if (dev->path.type == DEVICE_PATH_DOMAIN)
128 dev->ops = &pci_domain_ops;
129 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER)
130 dev->ops = &cpu_bus_ops;
131 else if (dev->path.type == DEVICE_PATH_GPIO)
132 block_gpio_enable(dev);
133 else if (dev->path.type == DEVICE_PATH_PCI &&
134 dev->path.pci.devfn == PCH_DEVFN_PMC)
135 dev->ops = &pmc_ops;
136 }
137 ```
138
139 This function examines each device's path type and assigns the appropriate
140 operations based on the device's role in the system.
141
142
143 ## Integration with the Devicetree
144
145 The `chip_operations` structure is tightly integrated with coreboot's
146 devicetree mechanism. The devicetree is a hierarchical description of the
147 hardware platform, defined in `.cb` files (typically `chipset.cb`,
148 `devicetree.cb`, and optionally `overridetree.cb`).
149
150 In the devicetree, a `chip` directive starts a collection of devices
151 associated with a particular chip driver. The path specified with the
152 `chip` directive corresponds to a directory in the coreboot source tree
153 that contains the chip driver code, including a `chip.c` file that defines
154 the `chip_operations` structure for that chip.
155
156 For example, a devicetree might contain:
157
158 ```
159 chip soc/intel/cannonlake
160 device domain 0 on
161 device pci 00.0 on end # Host Bridge
162 device pci 12.0 on end # Thermal Subsystem
163 # ... more devices ...
164 end
165 end
166 ```
167
168 This connects the devices under this chip directive with the
169 `chip_operations` structure defined in
170 `src/soc/intel/cannonlake/chip.c`:
171
172 ```c
173 struct chip_operations soc_intel_cannonlake_ops = {
174 .name = "Intel Cannonlake",
175 .enable_dev = &soc_enable,
176 .init = &soc_init_pre_device,
177 };
178 ```
179
180 During coreboot's build process, the `sconfig` utility processes the
181 devicetree files and generates code that links the devices defined in the
182 devicetree with their corresponding `chip_operations` structures.
183
184
185 ## Chip Configuration Data
186
187 Each chip typically defines a configuration structure in a `chip.h` file
188 within its source directory. This structure contains configuration settings
189 that can be specified in the devicetree using `register` directives.
190
191 For example, a chip might define a configuration structure like:
192
193 ```c
194 /* In src/soc/intel/cannonlake/chip.h */
195 struct soc_intel_cannonlake_config {
196 uint8_t pcie_rp_aspm[CONFIG_MAX_ROOT_PORTS];
197 uint8_t usb2_ports[16];
198 uint8_t usb3_ports[10];
199 /* ... more configuration options ... */
200 };
201 ```
202
203 In the devicetree, you would configure these options using register
204 directives:
205
206 ```
207 chip soc/intel/cannonlake
208 register "pcie_rp_aspm[0]" = "ASPM_AUTO"
209 register "usb2_ports[5]" = "USB2_PORT_MID(OC_SKIP)"
210 # ... more register settings ...
211
212 device domain 0 on
213 # ... devices ...
214 end
215 end
216 ```
217
218 These configuration values are made available to the chip's `init` and
219 `final` functions through the `chip_info` parameter, which points to
220 an instance of the chip's configuration structure (after appropriate
221 casting from `void *`).
222
223
224 ## Implementation Examples
225
226 ### Minimal Implementation
227
228 Some chips may not need extensive initialization and can provide a
229 minimal implementation of the `chip_operations` structure:
230
231 ```c
232 struct chip_operations soc_ucb_riscv_ops = {
233 .name = "UCB RISC-V",
234 };
235 ```
236
237 This implementation only provides a name for debugging purposes but
238 doesn't define any initialization functions.
239
240
241 ### Basic Implementation with Initialization
242
243 A more typical implementation includes at least initialization hooks:
244
245 ```c
246 struct chip_operations soc_amd_genoa_poc_ops = {
247 .name = "AMD Genoa SoC Proof of Concept",
248 .init = soc_init,
249 .final = soc_final,
250 };
251 ```
252
253 The `init` function might perform chip-wide initialization:
254
255 ```c
256 static void soc_init(void *chip_info)
257 {
258 default_dev_ops_root.write_acpi_tables = soc_acpi_write_tables;
259 amd_opensil_silicon_init();
260 data_fabric_print_mmio_conf();
261 fch_init(chip_info);
262 }
263 ```
264
265
266 ### Complete Implementation
267
268 A complete implementation includes all three function pointers:
269
270 ```c
271 struct chip_operations soc_intel_xeon_sp_cpx_ops = {
272 .name = "Intel Cooper Lake-SP",
273 .enable_dev = chip_enable_dev,
274 .init = chip_init,
275 .final = chip_final,
276 };
277 ```
278
279 The `enable_dev` function would typically assign device operations
280 based on device types:
281
282 ```c
283 static void chip_enable_dev(struct device *dev)
284 {
285 /* PCI root complex */
286 if (dev->path.type == DEVICE_PATH_DOMAIN)
287 dev->ops = &pci_domain_ops;
288 /* CPU cluster */
289 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER)
290 dev->ops = &cpu_cluster_ops;
291 /* PCIe root ports */
292 else if (dev->path.type == DEVICE_PATH_PCI &&
293 PCI_SLOT(dev->path.pci.devfn) == PCIE_PORT1_SLOT)
294 dev->ops = &pcie_rp_ops;
295 /* ... other device types ... */
296 }
297 ```
298
299
300 ### Mainboard Implementation
301
302 It's also common for the mainboard-specific code (e.g.,
303 `src/mainboard/vendor/board/mainboard.c`) to define its own
304 `chip_operations`, often named `mainboard_ops`. The `mainboard_ops.init`
305 can perform early board-level setup, and `mainboard_ops.enable_dev` can
306 assign operations for devices specific to the mainboard or set default
307 operations.
308
309 ```c
310 /* Example from src/mainboard/google/zork/mainboard.c */
311 struct chip_operations mainboard_ops = {
312 .enable_dev = mainboard_enable,
313 .init = mainboard_init,
314 .final = mainboard_final,
315 };
316 ```
317
318
319 ## Device Registration and Discovery
320
321 The `chip_operations` structure plays a key role in device registration
322 and discovery within coreboot. Here's how it fits into this process:
323
324 1. **Static Device Definition**: Devices are statically defined in the
325 devicetree files (`chipset.cb`, `devicetree.cb`, `overridetree.cb`).
326
327 2. **Code Generation**: The `sconfig` utility processes these files and
328 generates code in `build/static.c` that creates the device structures
329 and links them to their corresponding chip configuration data.
330
331 3. **Chip Initialization**: During the `BS_DEV_INIT_CHIPS` boot state,
332 `dev_initialize_chips()` calls each chip's `init` function to perform
333 chip-wide setup.
334
335 4. **Device Enumeration and Enabling**: During the `BS_DEV_ENUMERATE`
336 boot state, `dev_enumerate()` initiates bus scanning. The scan
337 functions call the associated chip's `enable_dev` function for each
338 device, which assigns the appropriate device operations (`dev->ops`).
339
340 5. **Device Configuration and Initialization**: Subsequent boot states
341 (`BS_DEV_RESOURCES`, `BS_DEV_ENABLE`, `BS_DEV_INIT`) configure and
342 initialize the devices according to their assigned device operations.
343
344 6. **Chip Finalization**: After all devices have been initialized,
345 `dev_finalize_chips()` calls each chip's `final` function during the
346 `BS_WRITE_TABLES` boot state.
347
348
349 ## Build Process Integration
350
351 The `chip_operations` structures are integrated into the coreboot build
352 process through several mechanisms:
353
354 1. **Devicetree Processing**: The `sconfig` utility processes the
355 devicetree files and generates code that creates and links the device
356 structures.
357
358 2. **Static Structure Declaration**: Each chip (and often the mainboard)
359 defines its `chip_operations` structure in its respective `.c` file.
360 These structures are collected during the build process.
361
362 3. **External References**: The generated code in `build/static.c`
363 includes external references to these `chip_operations` structures.
364
365 4. **Linking**: The linker collects all the `chip_operations` structures
366 and includes them in the final firmware image.
367
368 This process ensures that the appropriate chip operations are available
369 during the boot process for each chip included in the devicetree.
370
371
372 ## Best Practices for Implementing `chip_operations`
373
374 When implementing the `chip_operations` structure for a new chip,
375 follow these best practices:
376
377 1. **Provide a Meaningful Name**: The `name` field should be descriptive
378 and identify the chip clearly for debugging purposes.
379
380 2. **Implement `enable_dev` Correctly**: The `enable_dev` function should
381 assign the appropriate device operations based on device types and
382 functions. It should handle all device types that might be part of the chip.
383 Consider interactions with the mainboard `enable_dev`.
384
385 3. **Use Configuration Data**: The `init` and `final` functions should
386 make use of the chip configuration data passed via the `chip_info`
387 parameter (casting it to the correct type) to configure the chip
388 according to the settings specified in the devicetree.
389
390 4. **Minimize Dependencies**: The `init` function should minimize
391 dependencies on other chips being initialized, as the order of chip
392 initialization is not guaranteed.
393
394 5. **Handle Resources Properly**: If the chip manages resources (memory
395 regions, I/O ports, etc.), ensure that these are properly allocated and
396 assigned to devices, usually within the associated `device_operations`.
397
398 6. **Implement Error Handling**: Include appropriate error handling in
399 the initialization functions to handle hardware initialization failures
400 gracefully.
401
402 7. **Document Special Requirements**: If the chip has special
403 requirements or dependencies, document these clearly in comments or
404 accompanying documentation.
405
406
407 ## Troubleshooting `chip_operations` Issues
408
409 When implementing or debugging `chip_operations`, you might encounter
410 certain issues:
411
412 1. **Missing Device Operations**: If devices are not being initialized
413 properly, check that the `enable_dev` function is correctly
414 assigning device operations based on device types. Ensure it's being
415 called during bus scanning.
416
417 2. **Initialization Order Problems**: If a chip's initialization depends
418 on another chip being initialized first, you might need to adjust the
419 initialization sequence or add explicit dependencies, possibly using
420 boot state callbacks if necessary.
421
422 3. **Configuration Data Issues**: If chip configuration settings are not
423 being applied correctly, check that the configuration structure is
424 correctly defined in `chip.h`, that the register values in the
425 devicetree match the expected format, and that the `chip_info` pointer
426 is cast correctly in the `init`/`final` functions.
427
428 4. **Build Errors**: If you encounter build errors related to
429 `chip_operations`, check that the structure is correctly defined and
430 that all required symbols are properly exported and linked. Check for
431 conflicts if multiple files define the same symbol.
432
433 5. **Runtime Failures**: If the chip initialization fails at runtime,
434 add debug logging (using `printk`) to the `init`, `enable_dev`, and
435 `final` functions to identify the specific point of failure.
436
437
438 ## Advanced `chip_operations` Patterns
439
440 ### Hierarchical Chip Initialization
441
442 For complex chips with multiple components, you can implement a
443 hierarchical initialization pattern within the `init` function:
444
445 ```c
446 static void soc_init(void *chip_info)
447 {
448 /* Initialize common components first */
449 common_init(chip_info);
450
451 /* Initialize specific blocks */
452 pcie_init(chip_info);
453 usb_init(chip_info);
454 sata_init(chip_info);
455
456 /* Final SoC-wide configuration */
457 power_management_init(chip_info);
458 }
459 ```
460
461
462 ### Variant Support
463
464 For chips with multiple variants, you can implement variant detection
465 and specific initialization within the `init` function:
466
467 ```c
468 static void soc_init(void *chip_info)
469 {
470 uint32_t variant = read_chip_variant();
471
472 /* Common initialization */
473 common_init(chip_info);
474
475 /* Variant-specific initialization */
476 switch (variant) {
477 case VARIANT_A:
478 variant_a_init(chip_info);
479 break;
480 case VARIANT_B:
481 variant_b_init(chip_info);
482 break;
483 default:
484 printk(BIOS_WARNING, "Unknown variant %u\\n", variant);
485 break;
486 }
487 }
488 ```
489
490
491 ### Conditional Feature Initialization
492
493 You can conditionally initialize features based on configuration settings
494 passed via `chip_info`:
495
496 ```c
497 static void soc_init(void *chip_info)
498 {
499 struct soc_config *config = chip_info;
500
501 /* Always initialize core components */
502 core_init();
503
504 /* Conditionally initialize optional features */
505 if (config->enable_xhci)
506 xhci_init(config);
507
508 if (config->enable_sata)
509 sata_init(config);
510
511 if (config->enable_pcie)
512 pcie_init(config);
513 }
514 ```
515
516
517 ## Conclusion
518
519 The `chip_operations` structure is a fundamental component of coreboot's
520 chipset abstraction layer. It provides a standardized interface for chipset-
521 specific code to interact with coreboot's device initialization framework,
522 enabling support for a wide variety of chipsets while maintaining a
523 consistent initialization flow.
524
525 By implementing the `chip_operations` structure for a specific chipset
526 (and often for the mainboard), developers can integrate their
527 hardware-specific code with coreboot's device enumeration, configuration,
528 and initialization process. This structure serves as the bridge between
529 the declarative device descriptions in the devicetree and the imperative
530 code that initializes the hardware.
531
532 Understanding the `chip_operations` structure and its role in the
533 coreboot boot process is essential for anyone working on chipset or
534 mainboard support in coreboot. By following the best practices and
535 patterns outlined in this document, developers can create robust and
536 maintainable hardware support code that integrates seamlessly with the
537 coreboot firmware ecosystem.