soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / device / device.c
blobffda588d631d666a88b9863c23f5e6e5cfd1a227
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /*
4 * Originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
5 */
7 #include <console/console.h>
8 #include <device/device.h>
9 #include <device/pci_def.h>
10 #include <device/pci_ids.h>
11 #include <post.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <smp/spinlock.h>
15 #include <timer.h>
17 /** Pointer to the last device */
18 extern struct device *last_dev;
19 /** Linked list of free resources */
20 struct resource *free_resources = NULL;
21 /* Disable a PCI device based on bus, device and function. */
22 void devfn_disable(const struct bus *bus, unsigned int devfn)
24 struct device *dev = pcidev_path_behind(bus, devfn);
25 if (dev)
26 dev->enabled = 0;
29 /**
30 * Initialize all chips of statically known devices.
32 * Will be called before bus enumeration to initialize chips stated in the
33 * device tree.
35 void dev_initialize_chips(void)
37 const struct device *dev;
39 for (dev = all_devices; dev; dev = dev->next) {
40 /* Initialize chip if we haven't yet. */
41 if (dev->chip_ops && dev->chip_ops->init &&
42 !dev->chip_ops->initialized) {
43 post_log_path(dev);
44 dev->chip_ops->init(dev->chip_info);
45 dev->chip_ops->initialized = 1;
48 post_log_clear();
51 /**
52 * Finalize all chips of statically known devices.
54 * This is the last call before calling the payload. This is a good place
55 * to lock registers or other final cleanup.
57 void dev_finalize_chips(void)
59 const struct device *dev;
61 for (dev = all_devices; dev; dev = dev->next) {
62 /* Initialize chip if we haven't yet. */
63 if (dev->chip_ops && dev->chip_ops->final &&
64 !dev->chip_ops->finalized) {
65 dev->chip_ops->final(dev->chip_info);
66 dev->chip_ops->finalized = 1;
71 DECLARE_SPIN_LOCK(dev_lock)
73 /**
74 * Allocate a new device structure.
76 * Allocate a new device structure and attach it to the device tree as a
77 * child of the parent bus.
79 * @param parent Parent bus the newly created device should be attached to.
80 * @param path Path to the device to be created.
81 * @return Pointer to the newly created device structure.
83 * @see device_path
85 static struct device *__alloc_dev(struct bus *parent, struct device_path *path)
87 struct device *dev, *child;
89 /* Find the last child of our parent. */
90 for (child = parent->children; child && child->sibling; /* */)
91 child = child->sibling;
93 dev = malloc(sizeof(*dev));
94 if (dev == 0)
95 die("alloc_dev(): out of memory.\n");
97 memset(dev, 0, sizeof(*dev));
98 memcpy(&dev->path, path, sizeof(*path));
100 /* By default devices are enabled. */
101 dev->enabled = 1;
103 /* Add the new device to the list of children of the bus. */
104 dev->bus = parent;
105 if (child)
106 child->sibling = dev;
107 else
108 parent->children = dev;
110 /* Append a new device to the global device list.
111 * The list is used to find devices once everything is set up.
113 last_dev->next = dev;
114 last_dev = dev;
116 return dev;
119 struct device *alloc_dev(struct bus *parent, struct device_path *path)
121 struct device *dev;
122 spin_lock(&dev_lock);
123 dev = __alloc_dev(parent, path);
124 spin_unlock(&dev_lock);
125 return dev;
129 * See if a device structure already exists and if not allocate it.
131 * @param parent The bus to find the device on.
132 * @param path The relative path from the bus to the appropriate device.
133 * @return Pointer to a device structure for the device on bus at path.
135 struct device *alloc_find_dev(struct bus *parent, struct device_path *path)
137 struct device *child;
138 spin_lock(&dev_lock);
139 child = find_dev_path(parent, path);
140 if (!child)
141 child = __alloc_dev(parent, path);
142 spin_unlock(&dev_lock);
143 return child;
147 * Read the resources on all devices of a given bus.
149 * @param bus Bus to read the resources on.
151 static void read_resources(struct bus *bus)
153 struct device *curdev;
155 printk(BIOS_SPEW, "%s %s bus %d link: %d\n", dev_path(bus->dev),
156 __func__, bus->secondary, bus->link_num);
158 /* Walk through all devices and find which resources they need. */
159 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
160 struct bus *link;
162 if (!curdev->enabled)
163 continue;
165 if (!curdev->ops || !curdev->ops->read_resources) {
166 if (curdev->path.type != DEVICE_PATH_APIC)
167 printk(BIOS_ERR, "%s missing %s\n",
168 dev_path(curdev), __func__);
169 continue;
171 post_log_path(curdev);
172 curdev->ops->read_resources(curdev);
174 /* Read in the resources behind the current device's links. */
175 for (link = curdev->link_list; link; link = link->next)
176 read_resources(link);
178 post_log_clear();
179 printk(BIOS_SPEW, "%s %s bus %d link: %d done\n",
180 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
183 struct device *vga_pri = NULL;
184 static void set_vga_bridge_bits(void)
187 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
188 * This function knows too much about PCI stuff, it should be just
189 * an iterator/visitor.
192 /* FIXME: Handle the VGA palette snooping. */
193 struct device *dev, *vga, *vga_onboard;
194 struct bus *bus;
196 bus = 0;
197 vga = 0;
198 vga_onboard = 0;
200 dev = NULL;
201 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
202 if (!dev->enabled)
203 continue;
205 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
206 if (dev->bus->no_vga16) {
207 printk(BIOS_WARNING,
208 "A bridge on the path doesn't support 16-bit VGA decoding!");
211 if (dev->on_mainboard)
212 vga_onboard = dev;
213 else
214 vga = dev;
216 /* It isn't safe to enable all VGA cards. */
217 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
220 if (!vga)
221 vga = vga_onboard;
223 if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard)
224 vga = vga_onboard;
226 /* If we prefer plugin VGA over chipset VGA, the chipset might
227 want to know. */
228 if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) &&
229 vga_onboard && vga_onboard->ops && vga_onboard->ops->vga_disable) {
230 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
231 vga_onboard->ops->vga_disable(vga_onboard);
234 if (vga) {
235 /* VGA is first add-on card or the only onboard VGA. */
236 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
237 /* All legacy VGA cards have MEM & I/O space registers. */
238 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
239 vga_pri = vga;
240 bus = vga->bus;
243 /* Now walk up the bridges setting the VGA enable. */
244 while (bus) {
245 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
246 dev_path(bus->dev));
247 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16;
248 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
253 * Assign the computed resources to the devices on the bus.
255 * Use the device specific set_resources() method to store the computed
256 * resources to hardware. For bridge devices, the set_resources() method
257 * has to recurse into every down stream buses.
259 * Mutual recursion:
260 * assign_resources() -> device_operation::set_resources()
261 * device_operation::set_resources() -> assign_resources()
263 * @param bus Pointer to the structure for this bus.
265 void assign_resources(struct bus *bus)
267 struct device *curdev;
269 printk(BIOS_SPEW, "%s %s, bus %d link: %d\n",
270 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
272 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
273 if (!curdev->enabled || !curdev->resource_list)
274 continue;
276 if (!curdev->ops || !curdev->ops->set_resources) {
277 printk(BIOS_ERR, "%s missing set_resources\n",
278 dev_path(curdev));
279 continue;
281 post_log_path(curdev);
282 curdev->ops->set_resources(curdev);
284 post_log_clear();
285 printk(BIOS_SPEW, "%s %s, bus %d link: %d done\n",
286 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
290 * Enable the resources for devices on a link.
292 * Enable resources of the device by calling the device specific
293 * enable_resources() method.
295 * The parent's resources should be enabled first to avoid having enabling
296 * order problem. This is done by calling the parent's enable_resources()
297 * method before its children's enable_resources() methods.
299 * @param link The link whose devices' resources are to be enabled.
301 static void enable_resources(struct bus *link)
303 struct device *dev;
304 struct bus *c_link;
306 for (dev = link->children; dev; dev = dev->sibling) {
307 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
308 post_log_path(dev);
309 dev->ops->enable_resources(dev);
313 for (dev = link->children; dev; dev = dev->sibling) {
314 for (c_link = dev->link_list; c_link; c_link = c_link->next)
315 enable_resources(c_link);
317 post_log_clear();
321 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
323 * @param bus Pointer to the bus structure.
324 * @return 1 if the bus was successfully reset, 0 otherwise.
326 int reset_bus(struct bus *bus)
328 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
329 bus->dev->ops->reset_bus(bus);
330 bus->reset_needed = 0;
331 return 1;
333 return 0;
337 * Scan for devices on a bus.
339 * If there are bridges on the bus, recursively scan the buses behind the
340 * bridges. If the setting up and tuning of the bus causes a reset to be
341 * required, reset the bus and scan it again.
343 * @param busdev Pointer to the bus device.
345 static void scan_bus(struct device *busdev)
347 int do_scan_bus;
348 struct stopwatch sw;
349 long scan_time;
351 if (!busdev->enabled)
352 return;
354 printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev));
356 post_log_path(busdev);
358 stopwatch_init(&sw);
360 do_scan_bus = 1;
361 while (do_scan_bus) {
362 struct bus *link;
363 busdev->ops->scan_bus(busdev);
364 do_scan_bus = 0;
365 for (link = busdev->link_list; link; link = link->next) {
366 if (link->reset_needed) {
367 if (reset_bus(link))
368 do_scan_bus = 1;
369 else
370 busdev->bus->reset_needed = 1;
375 scan_time = stopwatch_duration_msecs(&sw);
376 printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__,
377 dev_path(busdev), scan_time);
380 void scan_bridges(struct bus *bus)
382 struct device *child;
384 for (child = bus->children; child; child = child->sibling) {
385 if (!child->ops || !child->ops->scan_bus)
386 continue;
387 scan_bus(child);
392 * Determine the existence of devices and extend the device tree.
394 * Most of the devices in the system are listed in the mainboard devicetree.cb
395 * file. The device structures for these devices are generated at compile
396 * time by the config tool and are organized into the device tree. This
397 * function determines if the devices created at compile time actually exist
398 * in the physical system.
400 * For devices in the physical system but not listed in devicetree.cb,
401 * the device structures have to be created at run time and attached to the
402 * device tree.
404 * This function starts from the root device 'dev_root', scans the buses in
405 * the system recursively, and modifies the device tree according to the
406 * result of the probe.
408 * This function has no idea how to scan and probe buses and devices at all.
409 * It depends on the bus/device specific scan_bus() method to do it. The
410 * scan_bus() method also has to create the device structure and attach
411 * it to the device tree.
413 void dev_enumerate(void)
415 struct device *root;
417 printk(BIOS_INFO, "Enumerating buses...\n");
419 root = &dev_root;
421 show_all_devs(BIOS_SPEW, "Before device enumeration.");
422 printk(BIOS_SPEW, "Compare with tree...\n");
423 show_devs_tree(root, BIOS_SPEW, 0);
425 if (root->chip_ops && root->chip_ops->enable_dev)
426 root->chip_ops->enable_dev(root);
428 if (!root->ops || !root->ops->scan_bus) {
429 printk(BIOS_ERR, "dev_root missing scan_bus operation");
430 return;
432 scan_bus(root);
433 post_log_clear();
434 printk(BIOS_INFO, "done\n");
438 * Configure devices on the devices tree.
440 * Starting at the root of the device tree, travel it recursively in two
441 * passes. In the first pass, we compute and allocate resources (ranges)
442 * required by each device. In the second pass, the resources ranges are
443 * relocated to their final position and stored to the hardware.
445 * I/O resources grow upward. MEM resources grow downward.
447 * Since the assignment is hierarchical we set the values into the dev_root
448 * struct.
450 void dev_configure(void)
452 const struct device *root;
454 set_vga_bridge_bits();
456 printk(BIOS_INFO, "Allocating resources...\n");
458 root = &dev_root;
461 * Each domain should create resources which contain the entire address
462 * space for IO, MEM, and PREFMEM resources in the domain. The
463 * allocation of device resources will be done from this address space.
466 /* Read the resources for the entire tree. */
468 printk(BIOS_INFO, "Reading resources...\n");
469 read_resources(root->link_list);
470 printk(BIOS_INFO, "Done reading resources.\n");
472 print_resource_tree(root, BIOS_SPEW, "After reading.");
474 allocate_resources(root);
476 assign_resources(root->link_list);
477 printk(BIOS_INFO, "Done setting resources.\n");
478 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
480 printk(BIOS_INFO, "Done allocating resources.\n");
484 * Enable devices on the device tree.
486 * Starting at the root, walk the tree and enable all devices/bridges by
487 * calling the device's enable_resources() method.
489 void dev_enable(void)
491 struct bus *link;
493 printk(BIOS_INFO, "Enabling resources...\n");
495 /* Now enable everything. */
496 for (link = dev_root.link_list; link; link = link->next)
497 enable_resources(link);
499 printk(BIOS_INFO, "done.\n");
503 * Initialize a specific device.
505 * The parent should be initialized first to avoid having an ordering problem.
506 * This is done by calling the parent's init() method before its children's
507 * init() methods.
509 * @param dev The device to be initialized.
511 static void init_dev(struct device *dev)
513 if (!dev->enabled)
514 return;
516 if (!dev->initialized && dev->ops && dev->ops->init) {
517 struct stopwatch sw;
518 long init_time;
520 if (dev->path.type == DEVICE_PATH_I2C) {
521 printk(BIOS_DEBUG, "smbus: %s[%d]->",
522 dev_path(dev->bus->dev), dev->bus->link_num);
525 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
527 stopwatch_init(&sw);
528 dev->initialized = 1;
529 dev->ops->init(dev);
531 init_time = stopwatch_duration_msecs(&sw);
532 printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev),
533 init_time);
537 static void init_link(struct bus *link)
539 struct device *dev;
540 struct bus *c_link;
542 for (dev = link->children; dev; dev = dev->sibling) {
543 post_code(POST_BS_DEV_INIT);
544 post_log_path(dev);
545 init_dev(dev);
548 for (dev = link->children; dev; dev = dev->sibling) {
549 for (c_link = dev->link_list; c_link; c_link = c_link->next)
550 init_link(c_link);
555 * Initialize all devices in the global device tree.
557 * Starting at the root device, call the device's init() method to do
558 * device-specific setup, then call each child's init() method.
560 void dev_initialize(void)
562 struct bus *link;
564 printk(BIOS_INFO, "Initializing devices...\n");
566 /* First call the mainboard init. */
567 init_dev(&dev_root);
569 /* Now initialize everything. */
570 for (link = dev_root.link_list; link; link = link->next)
571 init_link(link);
572 post_log_clear();
574 printk(BIOS_INFO, "Devices initialized\n");
575 show_all_devs(BIOS_SPEW, "After init.");
579 * Finalize a specific device.
581 * The parent should be finalized first to avoid having an ordering problem.
582 * This is done by calling the parent's final() method before its childrens'
583 * final() methods.
585 * @param dev The device to be initialized.
587 static void final_dev(struct device *dev)
589 if (!dev->enabled)
590 return;
592 if (dev->ops && dev->ops->final) {
593 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
594 dev->ops->final(dev);
598 static void final_link(struct bus *link)
600 struct device *dev;
601 struct bus *c_link;
603 for (dev = link->children; dev; dev = dev->sibling)
604 final_dev(dev);
606 for (dev = link->children; dev; dev = dev->sibling) {
607 for (c_link = dev->link_list; c_link; c_link = c_link->next)
608 final_link(c_link);
612 * Finalize all devices in the global device tree.
614 * Starting at the root device, call the device's final() method to do
615 * device-specific cleanup, then call each child's final() method.
617 void dev_finalize(void)
619 struct bus *link;
621 printk(BIOS_INFO, "Finalize devices...\n");
623 /* First call the mainboard finalize. */
624 final_dev(&dev_root);
626 /* Now finalize everything. */
627 for (link = dev_root.link_list; link; link = link->next)
628 final_link(link);
630 printk(BIOS_INFO, "Devices finalized\n");