1 /* SPDX-License-Identifier: GPL-2.0-only */
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 */
18 struct pci_operations
;
19 struct i2c_bus_operations
;
20 struct smbus_bus_operations
;
22 struct spi_bus_operations
;
23 struct usb_bus_operations
;
24 struct gpio_operations
;
25 struct mdio_bus_operations
;
28 struct chip_operations
{
29 void (*enable_dev
)(struct device
*dev
);
30 void (*init
)(void *chip_info
);
31 void (*final
)(void *chip_info
);
32 unsigned int initialized
: 1;
33 unsigned int finalized
: 1;
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
);
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
);
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
);
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
;
73 * Standard device operations function pointers shims.
75 static inline void noop_read_resources(struct device
*dev
) {}
76 static inline void noop_set_resources(struct device
*dev
) {}
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 */
88 unsigned int reset_needed
: 1;
89 unsigned int no_vga16
: 1; /* No support for 16-bit VGA decoding */
93 * There is one device structure for each slot-number/function-number
98 DEVTREE_CONST
struct bus
*upstream
;
99 DEVTREE_CONST
struct bus
*downstream
;
101 DEVTREE_CONST
struct device
*sibling
; /* next device on this bus */
103 DEVTREE_CONST
struct device
*next
; /* chain of all devices */
105 struct device_path path
;
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;
122 uint16_t hotplug_buses
; /* Number of hotplug buses to allocate */
124 /* Base registers for this device. I/O, MEM and Expansion ROM */
125 DEVTREE_CONST
struct resource
*resource_list
;
128 struct device_operations
*ops
;
129 struct chip_operations
*chip_ops
;
131 #if CONFIG(GENERATE_SMBIOS_TABLES)
133 u8 smbios_slot_data_width
;
134 u8 smbios_slot_length
;
135 const char *smbios_slot_designation
;
137 #if CONFIG(SMBIOS_TYPE41_PROVIDED_BY_DEVTREE)
139 * These fields are intentionally guarded so that attempts to use
140 * the corresponding devicetree syntax without selecting the Kconfig
141 * option result in build-time errors. Smaller size is a side effect.
143 bool smbios_instance_id_valid
;
144 u8 smbios_instance_id
;
145 const char *smbios_refdes
;
149 DEVTREE_CONST
void *chip_info
;
151 /* Zero-terminated array of fields and options to probe. */
152 DEVTREE_CONST
struct fw_config
*probe_list
;
153 bool enable_on_unprovisioned_fw_config
;
157 * This is the root of the device tree. The device tree is defined in the
158 * static.c file and is generated by the config tool at compile time.
160 extern DEVTREE_CONST
struct device dev_root
;
161 /* list of all devices */
162 extern DEVTREE_CONST
struct device
* DEVTREE_CONST all_devices
;
163 extern struct resource
*free_resources
;
164 extern struct bus
*free_links
;
166 /* Generic device interface functions */
167 struct device
*alloc_dev(struct bus
*parent
, struct device_path
*path
);
168 struct bus
*alloc_bus(struct device
*parent
);
169 void dev_initialize_chips(void);
170 void dev_enumerate(void);
171 void dev_configure(void);
172 void dev_enable(void);
173 void dev_initialize(void);
174 void dev_finalize(void);
175 void dev_finalize_chips(void);
176 /* Function used to override device state */
177 void devfn_disable(const struct bus
*bus
, unsigned int devfn
);
179 /* Generic device helper functions */
180 int reset_bus(struct bus
*bus
);
181 void scan_bridges(struct bus
*bus
);
182 void assign_resources(struct bus
*bus
);
183 const char *dev_name(const struct device
*dev
);
184 const char *dev_path(const struct device
*dev
);
185 u32
dev_path_encode(const struct device
*dev
);
186 const struct device
*dev_get_domain(const struct device
*dev
);
187 unsigned int dev_get_domain_id(const struct device
*dev
);
188 void dev_set_enabled(struct device
*dev
, int enable
);
189 void disable_children(struct bus
*bus
);
190 bool dev_is_active_bridge(struct device
*dev
);
191 bool is_dev_enabled(const struct device
*const dev
);
192 bool is_devfn_enabled(unsigned int devfn
);
193 bool is_cpu(const struct device
*cpu
);
194 bool is_enabled_cpu(const struct device
*cpu
);
195 bool is_pci(const struct device
*pci
);
196 bool is_enabled_pci(const struct device
*pci
);
197 bool is_pci_dev_on_bus(const struct device
*pci
, unsigned int bus
);
198 bool is_pci_bridge(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
,
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
);
235 /* Debug functions */
236 void print_resource_tree(const struct device
*root
, int debug_level
,
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
);
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
);
275 const struct resource
*fixed_mem_range_flags(struct device
*dev
, unsigned long index
,
276 uint64_t base
, uint64_t size
,
279 return resource_range_idx(dev
, index
, base
, size
,
280 IORESOURCE_FIXED
| IORESOURCE_MEM
| flags
);
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
)
289 return fixed_mem_range_flags(dev
, index
, base
, end
- base
, flags
);
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
);
301 const struct resource
*domain_mem_window_from_to(struct device
*dev
, unsigned long index
,
302 uint64_t base
, uint64_t end
)
306 return domain_mem_window_range(dev
, index
, base
, end
- base
);
311 const struct resource
*ram_range(struct device
*dev
, unsigned long index
, uint64_t base
,
314 return fixed_mem_range_flags(dev
, index
, base
, size
, IORESOURCE_CACHEABLE
| IORESOURCE_STORED
);
318 const struct resource
*ram_from_to(struct device
*dev
, unsigned long index
, uint64_t base
,
323 return ram_range(dev
, index
, base
, end
- base
);
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
);
335 const struct resource
*reserved_ram_from_to(struct device
*dev
, unsigned long index
,
336 uint64_t base
, uint64_t end
)
340 return reserved_ram_range(dev
, index
, base
, end
- base
);
344 const struct resource
*mmio_range(struct device
*dev
, unsigned long index
, uint64_t base
,
347 return fixed_mem_range_flags(dev
, index
, base
, size
, IORESOURCE_RESERVE
| IORESOURCE_STORED
);
351 const struct resource
*mmio_from_to(struct device
*dev
, unsigned long index
, uint64_t base
,
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__)
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
);
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
)
380 if (end
> UINT16_MAX
+ 1)
382 return fixed_io_range_flags(dev
, index
, base
, end
- base
, flags
);
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
);
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
);
401 const struct resource
*domain_io_window_from_to(struct device
*dev
, unsigned long index
,
402 uint16_t base
, uint32_t end
)
406 if (end
> UINT16_MAX
+ 1)
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
,
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
,
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
,
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
,
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
;
490 static inline bool is_root_device(const struct device
*dev
)
492 if (!dev
|| !dev
->upstream
)
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 /* Macro to generate `struct device *` name that points to a device with the given alias. */
506 #define DEV_PTR(_alias) _dev_##_alias##_ptr
508 /* Macro to generate weak `struct device *` definition that points to a device with the given
510 #define WEAK_DEV_PTR(_alias) \
511 __weak DEVTREE_CONST struct device *const DEV_PTR(_alias)
513 #endif /* DEVICE_H */