xref: /aosp_15_r20/external/coreboot/src/include/device/device.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
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 <static.h>
13 #include <stdlib.h>
14 #include <types.h>
15 
16 struct fw_config;
17 struct device;
18 struct pci_operations;
19 struct i2c_bus_operations;
20 struct smbus_bus_operations;
21 struct pnp_mode_ops;
22 struct spi_bus_operations;
23 struct usb_bus_operations;
24 struct gpio_operations;
25 struct mdio_bus_operations;
26 
27 /* Chip 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;
34 	const char *name;
35 };
36 
37 struct bus;
38 
39 struct acpi_rsdp;
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  */
noop_read_resources(struct device * dev)75 static inline void noop_read_resources(struct device *dev) {}
noop_set_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 #if CONFIG(GENERATE_SMBIOS_TABLES)
132 	u8 smbios_slot_type;
133 	u8 smbios_slot_data_width;
134 	u8 smbios_slot_length;
135 	const char *smbios_slot_designation;
136 
137 #if CONFIG(SMBIOS_TYPE41_PROVIDED_BY_DEVTREE)
138 	/*
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.
142 	 */
143 	bool smbios_instance_id_valid;
144 	u8 smbios_instance_id;
145 	const char *smbios_refdes;
146 #endif
147 #endif
148 #endif
149 	DEVTREE_CONST void *chip_info;
150 
151 	/* Zero-terminated array of fields and options to probe. */
152 	DEVTREE_CONST struct fw_config *probe_list;
153 };
154 
155 /**
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.
158  */
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;
164 
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);
177 
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 void dev_set_enabled(struct device *dev, int enable);
187 void disable_children(struct bus *bus);
188 bool dev_is_active_bridge(struct device *dev);
189 bool is_dev_enabled(const struct device *const dev);
190 bool is_devfn_enabled(unsigned int devfn);
191 bool is_cpu(const struct device *cpu);
192 bool is_enabled_cpu(const struct device *cpu);
193 bool is_pci(const struct device *pci);
194 bool is_enabled_pci(const struct device *pci);
195 bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus);
196 bool is_pci_bridge(const struct device *pci);
197 
198 /* Returns whether there is a hotplug port on the path to the given device. */
199 bool dev_path_hotplug(const struct device *);
200 
201 /* Option ROM helper functions */
202 void run_bios(struct device *dev, unsigned long addr);
203 
204 /* Helper functions */
205 DEVTREE_CONST struct device *find_dev_path(
206 		const struct bus *parent,
207 		const struct device_path *path);
208 DEVTREE_CONST struct device *find_dev_nested_path(
209 		const struct bus *parent,
210 		const struct device_path nested_path[],
211 		size_t nested_path_length);
212 struct device *alloc_find_dev(struct bus *parent, struct device_path *path);
213 struct device *dev_find_device(u16 vendor, u16 device, struct device *from);
214 struct device *dev_find_class(unsigned int class, struct device *from);
215 DEVTREE_CONST struct device *dev_find_path(
216 		DEVTREE_CONST struct device *prev_match,
217 		enum device_path_type path_type);
218 struct device *dev_find_lapic(unsigned int apic_id);
219 int dev_count_cpu(void);
220 struct device *add_cpu_device(struct bus *cpu_bus, unsigned int apic_id,
221 				int enabled);
222 void mp_init_cpus(DEVTREE_CONST struct bus *cpu_bus);
mp_cpu_bus_init(struct device * dev)223 static inline void mp_cpu_bus_init(struct device *dev)
224 {
225 	/* Make sure the cpu cluster has a downstream bus for LAPICs to be allocated. */
226 	struct bus *bus = alloc_bus(dev);
227 
228 	mp_init_cpus(bus);
229 }
230 
231 /* Debug functions */
232 void print_resource_tree(const struct device *root, int debug_level,
233 			 const char *msg);
234 void show_devs_tree(const struct device *dev, int debug_level, int depth);
235 void show_devs_subtree(struct device *root, int debug_level, const char *msg);
236 void show_all_devs(int debug_level, const char *msg);
237 void show_all_devs_tree(int debug_level, const char *msg);
238 void show_one_resource(int debug_level, struct device *dev,
239 		       struct resource *resource, const char *comment);
240 void show_all_devs_resources(int debug_level, const char *msg);
241 
242 /* Debug macros */
243 #if CONFIG(DEBUG_FUNC)
244 #define DEV_FUNC_ENTER(dev) \
245 	printk(BIOS_SPEW, "%s:%s:%d: ENTER (dev: %s)\n", \
246 		__FILE__, __func__, __LINE__, dev_path(dev))
247 
248 #define DEV_FUNC_EXIT(dev) \
249 	printk(BIOS_SPEW, "%s:%s:%d: EXIT (dev: %s)\n", __FILE__, \
250 		__func__, __LINE__, dev_path(dev))
251 #else /* DEBUG_FUNC */
252 #define DEV_FUNC_ENTER(dev)
253 #define DEV_FUNC_EXIT(dev)
254 #endif /* DEBUG_FUNC */
255 
256 extern struct device_operations default_dev_ops_root;
257 void pci_domain_read_resources(struct device *dev);
258 void pci_domain_set_resources(struct device *dev);
259 void pci_host_bridge_scan_bus(struct device *dev);
260 
261 void mmconf_resource(struct device *dev, unsigned long index);
262 
263 /* These are temporary resource constructors to get us through the
264    migration away from open-coding all the IORESOURCE_FLAGS. */
265 
266 const struct resource *resource_range_idx(struct device *dev, unsigned long index,
267 					    uint64_t base, uint64_t size,
268 					    unsigned long flags);
269 
270 static inline
fixed_mem_range_flags(struct device * dev,unsigned long index,uint64_t base,uint64_t size,unsigned long flags)271 const struct resource *fixed_mem_range_flags(struct device *dev, unsigned long index,
272 					    uint64_t base, uint64_t size,
273 					    unsigned long flags)
274 {
275 	return resource_range_idx(dev, index, base, size,
276 				IORESOURCE_FIXED | IORESOURCE_MEM | flags);
277 }
278 
279 static inline
fixed_mem_from_to_flags(struct device * dev,unsigned long index,uint64_t base,uint64_t end,unsigned long flags)280 const struct resource *fixed_mem_from_to_flags(struct device *dev, unsigned long index,
281 					uint64_t base, uint64_t end, unsigned long flags)
282 {
283 	if (end <= base)
284 		return NULL;
285 	return fixed_mem_range_flags(dev, index, base, end - base, flags);
286 }
287 
288 static inline
domain_mem_window_range(struct device * dev,unsigned long index,uint64_t base,uint64_t size)289 const struct resource *domain_mem_window_range(struct device *dev, unsigned long index,
290 					uint64_t base, uint64_t size)
291 {
292 	return resource_range_idx(dev, index, base, size,
293 				IORESOURCE_MEM | IORESOURCE_BRIDGE);
294 }
295 
296 static inline
domain_mem_window_from_to(struct device * dev,unsigned long index,uint64_t base,uint64_t end)297 const struct resource *domain_mem_window_from_to(struct device *dev, unsigned long index,
298 					uint64_t base, uint64_t end)
299 {
300 	if (end <= base)
301 		return NULL;
302 	return domain_mem_window_range(dev, index, base, end - base);
303 }
304 
305 
306 static inline
ram_range(struct device * dev,unsigned long index,uint64_t base,uint64_t size)307 const struct resource *ram_range(struct device *dev, unsigned long index, uint64_t base,
308 				      uint64_t size)
309 {
310 	return fixed_mem_range_flags(dev, index, base, size, IORESOURCE_CACHEABLE | IORESOURCE_STORED);
311 }
312 
313 static inline
ram_from_to(struct device * dev,unsigned long index,uint64_t base,uint64_t end)314 const struct resource *ram_from_to(struct device *dev, unsigned long index, uint64_t base,
315 				 uint64_t end)
316 {
317 	if (end <= base)
318 		return NULL;
319 	return ram_range(dev, index, base, end - base);
320 }
321 
322 static inline
reserved_ram_range(struct device * dev,unsigned long index,uint64_t base,uint64_t size)323 const struct resource *reserved_ram_range(struct device *dev, unsigned long index,
324 					       uint64_t base, uint64_t size)
325 {
326 	return fixed_mem_range_flags(dev, index, base, size, IORESOURCE_CACHEABLE |
327 				    IORESOURCE_RESERVE | IORESOURCE_STORED);
328 }
329 
330 static inline
reserved_ram_from_to(struct device * dev,unsigned long index,uint64_t base,uint64_t end)331 const struct resource *reserved_ram_from_to(struct device *dev, unsigned long index,
332 					  uint64_t base, uint64_t end)
333 {
334 	if (end <= base)
335 		return NULL;
336 	return reserved_ram_range(dev, index, base, end - base);
337 }
338 
339 static inline
mmio_range(struct device * dev,unsigned long index,uint64_t base,uint64_t size)340 const struct resource *mmio_range(struct device *dev, unsigned long index, uint64_t base,
341 				       uint64_t size)
342 {
343 	return fixed_mem_range_flags(dev, index, base, size, IORESOURCE_RESERVE | IORESOURCE_STORED);
344 }
345 
346 static inline
mmio_from_to(struct device * dev,unsigned long index,uint64_t base,uint64_t end)347 const struct resource *mmio_from_to(struct device *dev, unsigned long index, uint64_t base,
348 				  uint64_t end)
349 {
350 	if (end <= base)
351 		return NULL;
352 	return mmio_range(dev, index, base, end - base);
353 }
354 
355 const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end);
356 const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end);
357 
358 #define bad_ram_range(...)	reserved_ram_range(__VA_ARGS__)
359 #define uma_range(...)		mmio_range(__VA_ARGS__)
360 #define uma_from_to(...)	mmio_from_to(__VA_ARGS__)
361 
362 static inline
fixed_io_range_flags(struct device * dev,unsigned long index,uint16_t base,uint16_t size,unsigned long flags)363 const struct resource *fixed_io_range_flags(struct device *dev, unsigned long index,
364 			uint16_t base, uint16_t size, unsigned long flags)
365 {
366 	return resource_range_idx(dev, index, base, size,
367 				IORESOURCE_FIXED | IORESOURCE_IO | flags);
368 }
369 
370 static inline
fixed_io_from_to_flags(struct device * dev,unsigned long index,uint16_t base,uint32_t end,unsigned long flags)371 const struct resource *fixed_io_from_to_flags(struct device *dev, unsigned long index,
372 				      uint16_t base, uint32_t end, unsigned long flags)
373 {
374 	if (end <= base)
375 		return NULL;
376 	if (end > UINT16_MAX + 1)
377 		return NULL;
378 	return fixed_io_range_flags(dev, index, base, end - base, flags);
379 }
380 
381 static inline
fixed_io_range_reserved(struct device * dev,unsigned long index,uint16_t base,uint16_t size)382 const struct resource *fixed_io_range_reserved(struct device *dev, unsigned long index,
383 				      uint16_t base, uint16_t size)
384 {
385 	return fixed_io_range_flags(dev, index, base, size, IORESOURCE_RESERVE);
386 }
387 
388 static inline
domain_io_window_range(struct device * dev,unsigned long index,uint16_t base,uint16_t size)389 const struct resource *domain_io_window_range(struct device *dev, unsigned long index,
390 			uint16_t base, uint16_t size)
391 {
392 	return resource_range_idx(dev, index, base, size,
393 				IORESOURCE_IO | IORESOURCE_BRIDGE);
394 }
395 
396 static inline
domain_io_window_from_to(struct device * dev,unsigned long index,uint16_t base,uint32_t end)397 const struct resource *domain_io_window_from_to(struct device *dev, unsigned long index,
398 				      uint16_t base, uint32_t end)
399 {
400 	if (end <= base)
401 		return NULL;
402 	if (end > UINT16_MAX + 1)
403 		return NULL;
404 	return domain_io_window_range(dev, index, base, end - base);
405 }
406 
407 /* Compatibility code */
408 
fixed_mem_resource_kb(struct device * dev,unsigned long index,unsigned long basek,unsigned long sizek,unsigned long flags)409 static inline void fixed_mem_resource_kb(struct device *dev, unsigned long index,
410 			unsigned long basek, unsigned long sizek,
411 			unsigned long flags)
412 {
413 	fixed_mem_range_flags(dev, index, ((uint64_t)basek) << 10,
414 		((uint64_t)sizek) << 10, IORESOURCE_STORED | flags);
415 }
416 
417 /* It is the caller's responsibility to adjust regions such that ram_resource_kb()
418  * and mmio_resource_kb() do not overlap.
419  */
420 #define ram_resource_kb(dev, idx, basek, sizek) \
421 	fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_CACHEABLE)
422 
423 #define reserved_ram_resource_kb(dev, idx, basek, sizek) \
424 	fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_CACHEABLE \
425 		| IORESOURCE_RESERVE)
426 
427 #define bad_ram_resource_kb(dev, idx, basek, sizek) \
428 	reserved_ram_resource_kb((dev), (idx), (basek), (sizek))
429 
430 #define uma_resource_kb(dev, idx, basek, sizek) \
431 	fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_RESERVE)
432 
433 #define mmio_resource_kb(dev, idx, basek, sizek) \
434 	fixed_mem_resource_kb(dev, idx, basek, sizek, IORESOURCE_RESERVE)
435 
436 void tolm_test(void *gp, struct device *dev, struct resource *new);
437 u32 find_pci_tolm(struct bus *bus);
438 
439 DEVTREE_CONST struct device *dev_find_next_pci_device(
440 				DEVTREE_CONST struct device *previous_dev);
441 DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
442 							unsigned int addr);
443 DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device);
444 DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
445 				DEVTREE_CONST struct device *prev_child);
446 
447 DEVTREE_CONST struct device *pcidev_path_behind(const struct bus *parent,
448 		pci_devfn_t devfn);
449 DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn);
450 DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn);
451 DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn);
452 DEVTREE_CONST struct bus *pci_root_bus(void);
453 /* Find PCI device with given D#:F# sitting behind the given PCI-to-PCI bridge device. */
454 DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge(
455 							const struct device *bridge,
456 							pci_devfn_t devfn);
457 
458 /* To be deprecated, avoid using.
459  *
460  * Note that this function can return the incorrect device prior
461  * to PCI enumeration because the secondary field of the bus object
462  * is 0. The failing scenario is determined by the order of the
463  * devices in all_devices singly-linked list as well as the time
464  * when this function is called (secondary reflecting topology).
465  */
466 DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func);
467 
468 /* Robust discovery of chip_info. */
469 void devtree_bug(const char *func, pci_devfn_t devfn);
470 void __noreturn devtree_die(void);
471 
472 /*
473  * Dies if `dev` or `dev->chip_info` are NULL. Returns `dev->chip_info` otherwise.
474  *
475  * Only use if missing `chip_info` is fatal and we can't boot. If it's
476  * not fatal, please handle the NULL case gracefully.
477  */
config_of(const struct device * dev)478 static inline DEVTREE_CONST void *config_of(const struct device *dev)
479 {
480 	if (dev && dev->chip_info)
481 		return dev->chip_info;
482 
483 	devtree_die();
484 }
485 
486 /*
487  * Returns pointer to config structure of root device (B:D:F = 0:00:0) defined by
488  * sconfig in static.{h/c}.
489  */
490 #define config_of_soc()		__pci_0_00_0_config
491 
is_root_device(const struct device * dev)492 static inline bool is_root_device(const struct device *dev)
493 {
494 	if (!dev || !dev->upstream)
495 		return false;
496 
497 	return (dev->path.type == DEVICE_PATH_ROOT) ||
498 	       (dev->upstream->dev == dev);
499 }
500 
501 void enable_static_device(struct device *dev);
502 void enable_static_devices(struct device *bus);
503 void scan_smbus(struct device *bus);
504 void scan_generic_bus(struct device *bus);
505 void scan_static_bus(struct device *bus);
506 
507 /* Macro to generate `struct device *` name that points to a device with the given alias. */
508 #define DEV_PTR(_alias)		_dev_##_alias##_ptr
509 
510 /* Macro to generate weak `struct device *` definition that points to a device with the given
511    alias. */
512 #define WEAK_DEV_PTR(_alias)			\
513 	__weak DEVTREE_CONST struct device *const DEV_PTR(_alias)
514 
515 #endif /* DEVICE_H */
516