Debian Codegraph @

src/include/device/device.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #ifndef DEVICE_H
4
5 #define DEVICE_H
6
7 #include <console/console.h>
8 #include <device/path.h> /* IWYU pragma: export */
9 #include <device/pci_type.h>
10 #include <device/resource.h> /* IWYU pragma: export */
11 #include <smbios.h>
12 #include <stdlib.h>
13 #include <types.h>
14
15 struct fw_config;
16 struct device;
17 struct pci_operations;
18 struct i2c_bus_operations;
19 struct smbus_bus_operations;
20 struct pnp_mode_ops;
21 struct spi_bus_operations;
22 struct usb_bus_operations;
23 struct gpio_operations;
24 struct mdio_bus_operations;
25
26 /* Chip operations */
27 struct chip_operations {
28 void (*enable_dev)(struct device *dev);
29 void (*init)(void *chip_info);
30 void (*final)(void *chip_info);
31 unsigned int initialized : 1;
32 unsigned int finalized : 1;
33 const char *name;
34 };
35
36 struct bus;
37
38 struct acpi_rsdp;
39 struct rom_header;
40
41 struct device_operations {
42 void (*read_resources)(struct device *dev);
43 void (*set_resources)(struct device *dev);
44 void (*enable_resources)(struct device *dev);
45 void (*init)(struct device *dev);
46 void (*final)(struct device *dev);
47 void (*scan_bus)(struct device *bus);
48 void (*enable)(struct device *dev);
49 void (*vga_disable)(struct device *dev);
50 void (*reset_bus)(struct bus *bus);
51
52 int (*get_smbios_data)(struct device *dev, int *handle,
53 unsigned long *current);
54 void (*get_smbios_strings)(struct device *dev, struct smbios_type11 *t);
55
56 unsigned long (*write_acpi_tables)(const struct device *dev,
57 unsigned long start, struct acpi_rsdp *rsdp);
58 void (*acpi_fill_ssdt)(const struct device *dev);
59 const char *(*acpi_name)(const struct device *dev);
60 /* Returns the optional _HID (Hardware ID) */
61 const char *(*acpi_hid)(const struct device *dev);
62
63 const struct pci_operations *ops_pci;
64 const struct i2c_bus_operations *ops_i2c_bus;
65 const struct spi_bus_operations *ops_spi_bus;
66 const struct smbus_bus_operations *ops_smbus_bus;
67 const struct pnp_mode_ops *ops_pnp_mode;
68 const struct gpio_operations *ops_gpio;
69 const struct mdio_bus_operations *ops_mdio;
70 };
71
72 /**
73 * Standard device operations function pointers shims.
74 */
75 static inline void noop_read_resources(struct device *dev) {}
76 static inline void noop_set_resources(struct device *dev) {}
77
78 struct bus {
79 DEVTREE_CONST struct device *dev; /* This bridge device */
80 DEVTREE_CONST struct device *children; /* devices behind this bridge */
81 unsigned int bridge_ctrl; /* Bridge control register */
82 uint16_t bridge_cmd; /* Bridge command register */
83 uint16_t secondary; /* secondary bus number */
84 uint16_t subordinate; /* subordinate bus number */
85 uint16_t max_subordinate; /* max subordinate bus number */
86 uint8_t segment_group; /* PCI segment group */
87
88 unsigned int reset_needed : 1;
89 unsigned int no_vga16 : 1; /* No support for 16-bit VGA decoding */
90 };
91
92 /*
93 * There is one device structure for each slot-number/function-number
94 * combination:
95 */
96
97 struct device {
98 DEVTREE_CONST struct bus *upstream;
99 DEVTREE_CONST struct bus *downstream;
100
101 DEVTREE_CONST struct device *sibling; /* next device on this bus */
102
103 DEVTREE_CONST struct device *next; /* chain of all devices */
104
105 struct device_path path;
106 unsigned int vendor;
107 unsigned int device;
108 u16 subsystem_vendor;
109 u16 subsystem_device;
110 unsigned int class; /* 3 bytes: (base, sub, prog-if) */
111 unsigned int hdr_type; /* PCI header type */
112 unsigned int enabled : 1; /* set if we should enable the device */
113 unsigned int initialized : 1; /* 1 if we have initialized the device */
114 unsigned int on_mainboard : 1;
115 unsigned int disable_pcie_aspm : 1;
116 /* set if we should hide from UI */
117 unsigned int hidden : 1;
118 /* set if this device is used even in minimum PCI cases */
119 unsigned int mandatory : 1;
120 unsigned int hotplug_port : 1;
121 u8 command;
122 uint16_t hotplug_buses; /* Number of hotplug buses to allocate */
123
124 /* Base registers for this device. I/O, MEM and Expansion ROM */
125 DEVTREE_CONST struct resource *resource_list;
126
127 #if !DEVTREE_EARLY
128 struct device_operations *ops;
129 struct chip_operations *chip_ops;
130 const char *name;
131 /*
132 * A pointer to the corresponding PCI Option ROM.
133 *
134 * When set the Option ROM has been placed in usable DRAM in
135 * an area that is marked as reserved. This can be for example
136 * the legacy C-segment or a CBMEM area. The Option ROM is
137 * read writeable and guaranteed to be used by the device.
138 * The PCIR data header might still have a different vendor and
139 * device ID.
140 */
141 struct rom_header *pci_vga_option_rom;
142 #if CONFIG(GENERATE_SMBIOS_TABLES)
143 u8 smbios_slot_type;
144 u8 smbios_slot_data_width;
145 u8 smbios_slot_length;
146 const char *smbios_slot_designation;
147
148 #if CONFIG(SMBIOS_TYPE41_PROVIDED_BY_DEVTREE)
149 /*
150 * These fields are intentionally guarded so that attempts to use
151 * the corresponding devicetree syntax without selecting the Kconfig
152 * option result in build-time errors. Smaller size is a side effect.
153 */
154 bool smbios_instance_id_valid;
155 u8 smbios_instance_id;
156 const char *smbios_refdes;
157 #endif
158 #endif
159 #endif
160 DEVTREE_CONST void *chip_info;
161
162 /* Zero-terminated array of fields and options to probe. */
163 DEVTREE_CONST struct fw_config *probe_list;
164 bool enable_on_unprovisioned_fw_config;
165 };
166
167 /**
168 * This is the root of the device tree. The device tree is defined in the
169 * static.c file and is generated by the config tool at compile time.
170 */
171 extern DEVTREE_CONST struct device dev_root;
172 /* list of all devices */
173 extern DEVTREE_CONST struct device * DEVTREE_CONST all_devices;
174 extern struct resource *free_resources;
175 extern struct bus *free_links;
176
177 /* Generic device interface functions */
178 struct device *alloc_dev(struct bus *parent, struct device_path *path);
179 struct bus *alloc_bus(struct device *parent);
180 void dev_initialize_chips(void);
181 void dev_enumerate(void);
182 void dev_configure(void);
183 void dev_enable(void);
184 void dev_initialize(void);
185 void dev_finalize(void);
186 void dev_finalize_chips(void);
187 /* Function used to override device state */
188 void devfn_disable(const struct bus *bus, unsigned int devfn);
189
190 /* Generic device helper functions */
191 int reset_bus(struct bus *bus);
192 void scan_bridges(struct bus *bus);
193 void assign_resources(struct bus *bus);
194 const char *dev_name(const struct device *dev);
195 const char *dev_path(const struct device *dev);
196 u32 dev_path_encode(const struct device *dev);
197 const struct device *dev_get_domain(const struct device *dev);
198 unsigned int dev_get_domain_id(const struct device *dev);
199 void dev_set_enabled(struct device *dev, int enable);
200 void disable_children(struct bus *bus);
201 bool dev_is_active_bridge(const struct device *dev);
202 bool is_dev_enabled(const struct device *const dev);
203 bool is_devfn_enabled(unsigned int devfn);
204 bool is_cpu(const struct device *cpu);
205 bool is_enabled_cpu(const struct device *cpu);
206 bool is_pci(const struct device *pci);
207 bool is_enabled_pci(const struct device *pci);
208 bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus);
209 bool is_pci_bridge(const struct device *pci);
210 bool is_pci_ioapic(const struct device *pci);
211 bool is_domain0(const struct device *dev);
212 bool is_dev_on_domain0(const struct device *dev);
213
214 /* Returns whether there is a hotplug port on the path to the given device. */
215 bool dev_path_hotplug(const struct device *);
216
217 /* Option ROM helper functions */
218 void run_bios(struct device *dev, unsigned long addr);
219
220 /* Helper functions */
221 DEVTREE_CONST struct device *find_dev_path(
222 const struct bus *parent,
223 const struct device_path *path);
224 DEVTREE_CONST struct device *find_dev_nested_path(
225 const struct bus *parent,
226 const struct device_path nested_path[],
227 size_t nested_path_length);
228 struct device *alloc_find_dev(struct bus *parent, struct device_path *path);
229 struct device *dev_find_device(u16 vendor, u16 device, struct device *from);
230 struct device *dev_find_class(unsigned int class, struct device *from);
231 DEVTREE_CONST struct device *dev_find_path(
232 DEVTREE_CONST struct device *prev_match,
233 enum device_path_type path_type);
234 struct device *dev_find_lapic(unsigned int apic_id);
235 int dev_count_cpu(void);
236 struct device *add_cpu_device(struct bus *cpu_bus, unsigned int apic_id,
237 int enabled);
238 void mp_init_cpus(DEVTREE_CONST struct bus *cpu_bus);
239 static inline void mp_cpu_bus_init(struct device *dev)
240 {
241 /* Make sure the cpu cluster has a downstream bus for LAPICs to be allocated. */
242 struct bus *bus = alloc_bus(dev);
243
244 mp_init_cpus(bus);
245 }
246
247 /* Debug functions */
248 void print_resource_tree(const struct device *root, int debug_level,
249 const char *msg);
250 void show_devs_tree(const struct device *dev, int debug_level, int depth);
251 void show_devs_subtree(struct device *root, int debug_level, const char *msg);
252 void show_all_devs(int debug_level, const char *msg);
253 void show_all_devs_tree(int debug_level, const char *msg);
254 void show_one_resource(int debug_level, struct device *dev,
255 struct resource *resource, const char *comment);
256 void show_all_devs_resources(int debug_level, const char *msg);
257
258 /* Debug macros */
259 #if CONFIG(DEBUG_FUNC)
260 #define DEV_FUNC_ENTER(dev) \
261 printk(BIOS_SPEW, "%s:%s:%d: ENTER (dev: %s)\n", \
262 __FILE__, __func__, __LINE__, dev_path(dev))
263
264 #define DEV_FUNC_EXIT(dev) \
265 printk(BIOS_SPEW, "%s:%s:%d: EXIT (dev: %s)\n", __FILE__, \
266 __func__, __LINE__, dev_path(dev))
267 #else /* DEBUG_FUNC */
268 #define DEV_FUNC_ENTER(dev)
269 #define DEV_FUNC_EXIT(dev)
270 #endif /* DEBUG_FUNC */
271
272 extern struct device_operations default_dev_ops_root;
273 void pci_domain_read_resources(struct device *dev);
274 void pci_domain_set_resources(struct device *dev);
275 void pci_host_bridge_scan_bus(struct device *dev);
276
277 void mmconf_resource(struct device *dev, unsigned long index);
278
279 /* These are temporary resource constructors to get us through the
280 migration away from open-coding all the IORESOURCE_FLAGS. */
281
282 const struct resource *resource_range_idx(struct device *dev, unsigned long index,
283 uint64_t base, uint64_t size,
284 unsigned long flags);
285
286 static inline
287 const struct resource *fixed_mem_range_flags(struct device *dev, unsigned long index,
288 uint64_t base, uint64_t size,
289 unsigned long flags)
290 {
291 return resource_range_idx(dev, index, base, size,
292 IORESOURCE_FIXED | IORESOURCE_MEM | flags);
293 }
294
295 static inline
296 const struct resource *fixed_mem_from_to_flags(struct device *dev, unsigned long index,
297 uint64_t base, uint64_t end, unsigned long flags)
298 {
299 if (end <= base)
300 return NULL;
301 return fixed_mem_range_flags(dev, index, base, end - base, flags);
302 }
303
304 static inline
305 const struct resource *domain_mem_window_range(struct device *dev, unsigned long index,
306 uint64_t base, uint64_t size)
307 {
308 return resource_range_idx(dev, index, base, size,
309 IORESOURCE_MEM | IORESOURCE_BRIDGE);
310 }
311
312 static inline
313 const struct resource *domain_mem_window_from_to(struct device *dev, unsigned long index,
314 uint64_t base, uint64_t end)
315 {
316 if (end <= base)
317 return NULL;
318 return domain_mem_window_range(dev, index, base, end - base);
319 }
320
321
322 static inline
323 const struct resource *ram_range(struct device *dev, unsigned long index, uint64_t base,
324 uint64_t size)
325 {
326 return fixed_mem_range_flags(dev, index, base, size, IORESOURCE_CACHEABLE | IORESOURCE_STORED);
327 }
328
329 static inline
330 const struct resource *ram_from_to(struct device *dev, unsigned long index, uint64_t base,
331 uint64_t end)
332 {
333 if (end <= base)
334 return NULL;
335 return ram_range(dev, index, base, end - base);
336 }
337
338 static inline
339 const struct resource *reserved_ram_range(struct device *dev, unsigned long index,
340 uint64_t base, uint64_t size)
341 {
342 return fixed_mem_range_flags(dev, index, base, size, IORESOURCE_CACHEABLE |
343 IORESOURCE_RESERVE | IORESOURCE_STORED);
344 }
345
346 static inline
347 const struct resource *reserved_ram_from_to(struct device *dev, unsigned long index,
348 uint64_t base, uint64_t end)
349 {
350 if (end <= base)
351 return NULL;
352 return reserved_ram_range(dev, index, base, end - base);
353 }
354
355 static inline
356 const struct resource *mmio_range(struct device *dev, unsigned long index, uint64_t base,
357 uint64_t size)
358 {
359 return fixed_mem_range_flags(dev, index, base, size, IORESOURCE_RESERVE | IORESOURCE_STORED);
360 }
361
362 static inline
363 const struct resource *mmio_from_to(struct device *dev, unsigned long index, uint64_t base,
364 uint64_t end)
365 {
366 if (end <= base)
367 return NULL;
368 return mmio_range(dev, index, base, end - base);
369 }
370
371 const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end);
372 const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end);
373
374 #define bad_ram_range(...) reserved_ram_range(__VA_ARGS__)
375 #define uma_range(...) mmio_range(__VA_ARGS__)
376 #define uma_from_to(...) mmio_from_to(__VA_ARGS__)
377
378 static inline
379 const struct resource *fixed_io_range_flags(struct device *dev, unsigned long index,
380 uint16_t base, uint16_t size, unsigned long flags)
381 {
382 return resource_range_idx(dev, index, base, size,
383 IORESOURCE_FIXED | IORESOURCE_IO | flags);
384 }
385
386 static inline
387 const struct resource *fixed_io_from_to_flags(struct device *dev, unsigned long index,
388 uint16_t base, uint32_t end, unsigned long flags)
389 {
390 if (end <= base)
391 return NULL;
392 if (end > UINT16_MAX + 1)
393 return NULL;
394 return fixed_io_range_flags(dev, index, base, end - base, flags);
395 }
396
397 static inline
398 const struct resource *fixed_io_range_reserved(struct device *dev, unsigned long index,
399 uint16_t base, uint16_t size)
400 {
401 return fixed_io_range_flags(dev, index, base, size, IORESOURCE_RESERVE);
402 }
403
404 static inline
405 const struct resource *domain_io_window_range(struct device *dev, unsigned long index,
406 uint16_t base, uint16_t size)
407 {
408 return resource_range_idx(dev, index, base, size,
409 IORESOURCE_IO | IORESOURCE_BRIDGE);
410 }
411
412 static inline
413 const struct resource *domain_io_window_from_to(struct device *dev, unsigned long index,
414 uint16_t base, uint32_t end)
415 {
416 if (end <= base)
417 return NULL;
418 if (end > UINT16_MAX + 1)
419 return NULL;
420 return domain_io_window_range(dev, index, base, end - base);
421 }
422
423 /* Compatibility code */
424
425 static inline void fixed_mem_resource_kb(struct device *dev, unsigned long index,
426 unsigned long basek, unsigned long sizek,
427 unsigned long flags)
428 {
429 fixed_mem_range_flags(dev, index, ((uint64_t)basek) << 10,
430 ((uint64_t)sizek) << 10, IORESOURCE_STORED | flags);
431 }
432
433 /* It is the caller's responsibility to adjust regions such that ram_resource_kb()
434 * and mmio_resource_kb() do not overlap.
435 */
436 #define ram_resource_kb(dev, idx, basek, sizek) \
437 fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_CACHEABLE)
438
439 #define reserved_ram_resource_kb(dev, idx, basek, sizek) \
440 fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_CACHEABLE \
441 | IORESOURCE_RESERVE)
442
443 #define bad_ram_resource_kb(dev, idx, basek, sizek) \
444 reserved_ram_resource_kb((dev), (idx), (basek), (sizek))
445
446 #define uma_resource_kb(dev, idx, basek, sizek) \
447 fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_RESERVE)
448
449 #define mmio_resource_kb(dev, idx, basek, sizek) \
450 fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_RESERVE)
451
452 void tolm_test(void *gp, struct device *dev, struct resource *new);
453 u32 find_pci_tolm(struct bus *bus);
454
455 DEVTREE_CONST struct device *dev_find_next_pci_device(
456 DEVTREE_CONST struct device *previous_dev);
457 DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
458 unsigned int addr);
459 DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device);
460 DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
461 DEVTREE_CONST struct device *prev_child);
462
463 DEVTREE_CONST struct device *pcidev_path_behind(const struct bus *parent,
464 pci_devfn_t devfn);
465 DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn);
466 DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn);
467 DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn);
468 DEVTREE_CONST struct bus *pci_root_bus(void);
469 /* Find PCI device with given D#:F# sitting behind the given PCI-to-PCI bridge device. */
470 DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge(
471 const struct device *bridge,
472 pci_devfn_t devfn);
473
474 /* To be deprecated, avoid using.
475 *
476 * Note that this function can return the incorrect device prior
477 * to PCI enumeration because the secondary field of the bus object
478 * is 0. The failing scenario is determined by the order of the
479 * devices in all_devices singly-linked list as well as the time
480 * when this function is called (secondary reflecting topology).
481 */
482 DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func);
483
484 /* Robust discovery of chip_info. */
485 void devtree_bug(const char *func, pci_devfn_t devfn);
486 void __noreturn devtree_die(void);
487
488 /*
489 * Dies if `dev` or `dev->chip_info` are NULL. Returns `dev->chip_info` otherwise.
490 *
491 * Only use if missing `chip_info` is fatal and we can't boot. If it's
492 * not fatal, please handle the NULL case gracefully.
493 */
494 static inline DEVTREE_CONST void *config_of(const struct device *dev)
495 {
496 if (dev && dev->chip_info)
497 return dev->chip_info;
498
499 devtree_die();
500 }
501
502 static inline bool is_root_device(const struct device *dev)
503 {
504 if (!dev || !dev->upstream)
505 return false;
506
507 return (dev->path.type == DEVICE_PATH_ROOT) ||
508 (dev->upstream->dev == dev);
509 }
510
511 void enable_static_device(struct device *dev);
512 void enable_static_devices(struct device *bus);
513 void scan_smbus(struct device *bus);
514 void scan_generic_bus(struct device *bus);
515 void scan_static_bus(struct device *bus);
516
517 #endif /* DEVICE_H */