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