cpu/x86/smm/pci_resource_store: Store DEV/VEN ID
[coreboot2.git] / src / device / device.c
blob88085f725a81e81be2f3dc4c87b3b738fa24a0aa
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->upstream = 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;
128 DECLARE_SPIN_LOCK(bus_lock)
131 * Allocate a new bus structure
133 * Allocate a new downstream bus structure below a device and attach it
134 * to the device tree if the device doesn't already have a downstream bus.
136 * @param parent Parent device the to-be-created bus should be attached to.
137 * @return Pointer to the newly created bus structure or the existing bus.
140 static struct bus *__alloc_bus(struct device *parent)
142 if (parent->downstream)
143 return parent->downstream;
145 struct bus *bus = calloc(1, sizeof(struct bus));
146 if (!bus)
147 die("Couldn't allocate downstream bus!\n");
148 parent->downstream = bus;
149 bus->dev = parent;
151 return bus;
154 struct bus *alloc_bus(struct device *parent)
156 struct bus *bus;
157 spin_lock(&bus_lock);
158 bus = __alloc_bus(parent);
159 spin_unlock(&bus_lock);
160 return bus;
164 * See if a device structure already exists and if not allocate it.
166 * @param parent The bus to find the device on.
167 * @param path The relative path from the bus to the appropriate device.
168 * @return Pointer to a device structure for the device on bus at path.
170 struct device *alloc_find_dev(struct bus *parent, struct device_path *path)
172 struct device *child;
173 spin_lock(&dev_lock);
174 child = find_dev_path(parent, path);
175 if (!child)
176 child = __alloc_dev(parent, path);
177 spin_unlock(&dev_lock);
178 return child;
182 * Read the resources on all devices of a given bus.
184 * @param bus Bus to read the resources on.
186 static void read_resources(struct bus *bus)
188 struct device *curdev;
190 printk(BIOS_SPEW, "%s %s segment group %d bus %d\n", dev_path(bus->dev),
191 __func__, bus->segment_group, bus->secondary);
193 /* Walk through all devices and find which resources they need. */
194 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
195 if (!curdev->enabled)
196 continue;
198 if (!curdev->ops || !curdev->ops->read_resources) {
199 if (curdev->path.type != DEVICE_PATH_APIC)
200 printk(BIOS_ERR, "%s missing %s\n",
201 dev_path(curdev), __func__);
202 continue;
204 post_log_path(curdev);
205 curdev->ops->read_resources(curdev);
207 /* Read in the resources behind the current device's links. */
208 if (curdev->downstream)
209 read_resources(curdev->downstream);
211 post_log_clear();
212 printk(BIOS_SPEW, "%s %s segment group %d bus %d done\n",
213 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary);
216 struct device *vga_pri = NULL;
217 static void set_vga_bridge_bits(void)
220 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
221 * This function knows too much about PCI stuff, it should be just
222 * an iterator/visitor.
225 /* FIXME: Handle the VGA palette snooping. */
226 struct device *dev, *vga, *vga_onboard;
227 struct bus *bus;
229 bus = 0;
230 vga = 0;
231 vga_onboard = 0;
233 dev = NULL;
234 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
235 if (!dev->enabled)
236 continue;
238 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
239 if (dev->upstream->no_vga16) {
240 printk(BIOS_WARNING,
241 "A bridge on the path doesn't support 16-bit VGA decoding!");
244 if (dev->on_mainboard)
245 vga_onboard = dev;
246 else
247 vga = dev;
249 /* It isn't safe to enable all VGA cards. */
250 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
253 if (!vga)
254 vga = vga_onboard;
256 if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard)
257 vga = vga_onboard;
259 /* If we prefer plugin VGA over chipset VGA, the chipset might
260 want to know. */
261 if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) &&
262 vga_onboard && vga_onboard->ops && vga_onboard->ops->vga_disable) {
263 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
264 vga_onboard->ops->vga_disable(vga_onboard);
267 if (vga) {
268 /* VGA is first add-on card or the only onboard VGA. */
269 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
270 /* All legacy VGA cards have MEM & I/O space registers. */
271 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
272 vga_pri = vga;
273 bus = vga->upstream;
276 /* Now walk up the bridges setting the VGA enable. */
277 while (bus) {
278 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
279 dev_path(bus->dev));
280 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16;
281 bus = (bus == bus->dev->upstream) ? 0 : bus->dev->upstream;
286 * Assign the computed resources to the devices on the bus.
288 * Use the device specific set_resources() method to store the computed
289 * resources to hardware. For bridge devices, the set_resources() method
290 * has to recurse into every down stream buses.
292 * Mutual recursion:
293 * assign_resources() -> device_operation::set_resources()
294 * device_operation::set_resources() -> assign_resources()
296 * @param bus Pointer to the structure for this bus.
298 void assign_resources(struct bus *bus)
300 struct device *curdev;
302 printk(BIOS_SPEW, "%s %s, segment group %d bus %d\n",
303 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary);
305 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
306 if (!curdev->enabled || !curdev->resource_list)
307 continue;
309 if (!curdev->ops || !curdev->ops->set_resources) {
310 printk(BIOS_ERR, "%s missing set_resources\n",
311 dev_path(curdev));
312 continue;
314 post_log_path(curdev);
315 curdev->ops->set_resources(curdev);
317 post_log_clear();
318 printk(BIOS_SPEW, "%s %s, segment group %d bus %d done\n",
319 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary);
323 * Enable the resources for devices on a link.
325 * Enable resources of the device by calling the device specific
326 * enable_resources() method.
328 * The parent's resources should be enabled first to avoid having enabling
329 * order problem. This is done by calling the parent's enable_resources()
330 * method before its children's enable_resources() methods.
332 * @param link The link whose devices' resources are to be enabled.
334 static void enable_resources(struct bus *link)
336 struct device *dev;
338 for (dev = link->children; dev; dev = dev->sibling) {
339 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
340 post_log_path(dev);
341 dev->ops->enable_resources(dev);
345 for (dev = link->children; dev; dev = dev->sibling) {
346 if (dev->downstream)
347 enable_resources(dev->downstream);
349 post_log_clear();
353 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
355 * @param bus Pointer to the bus structure.
356 * @return 1 if the bus was successfully reset, 0 otherwise.
358 int reset_bus(struct bus *bus)
360 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
361 bus->dev->ops->reset_bus(bus);
362 bus->reset_needed = 0;
363 return 1;
365 return 0;
369 * Scan for devices on a bus.
371 * If there are bridges on the bus, recursively scan the buses behind the
372 * bridges. If the setting up and tuning of the bus causes a reset to be
373 * required, reset the bus and scan it again.
375 * @param busdev Pointer to the bus device.
377 static void scan_bus(struct device *busdev)
379 int do_scan_bus;
380 struct stopwatch sw;
381 long scan_time;
383 if (!busdev->enabled)
384 return;
386 printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev));
388 post_log_path(busdev);
390 stopwatch_init(&sw);
392 do_scan_bus = 1;
393 while (do_scan_bus) {
394 struct bus *link = busdev->downstream;
395 busdev->ops->scan_bus(busdev);
396 do_scan_bus = 0;
397 if (!link || !link->reset_needed)
398 continue;
399 if (reset_bus(link))
400 do_scan_bus = 1;
401 else
402 busdev->upstream->reset_needed = 1;
405 scan_time = stopwatch_duration_msecs(&sw);
406 printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__,
407 dev_path(busdev), scan_time);
410 void scan_bridges(struct bus *bus)
412 struct device *child;
414 for (child = bus->children; child; child = child->sibling) {
415 if (!child->ops || !child->ops->scan_bus)
416 continue;
417 scan_bus(child);
422 * Determine the existence of devices and extend the device tree.
424 * Most of the devices in the system are listed in the mainboard devicetree.cb
425 * file. The device structures for these devices are generated at compile
426 * time by the config tool and are organized into the device tree. This
427 * function determines if the devices created at compile time actually exist
428 * in the physical system.
430 * For devices in the physical system but not listed in devicetree.cb,
431 * the device structures have to be created at run time and attached to the
432 * device tree.
434 * This function starts from the root device 'dev_root', scans the buses in
435 * the system recursively, and modifies the device tree according to the
436 * result of the probe.
438 * This function has no idea how to scan and probe buses and devices at all.
439 * It depends on the bus/device specific scan_bus() method to do it. The
440 * scan_bus() method also has to create the device structure and attach
441 * it to the device tree.
443 void dev_enumerate(void)
445 struct device *root;
447 printk(BIOS_INFO, "Enumerating buses...\n");
449 root = &dev_root;
451 show_all_devs(BIOS_SPEW, "Before device enumeration.");
452 printk(BIOS_SPEW, "Compare with tree...\n");
453 show_devs_tree(root, BIOS_SPEW, 0);
455 if (root->chip_ops && root->chip_ops->enable_dev)
456 root->chip_ops->enable_dev(root);
458 if (!root->ops || !root->ops->scan_bus) {
459 printk(BIOS_ERR, "dev_root missing scan_bus operation");
460 return;
462 scan_bus(root);
463 post_log_clear();
464 printk(BIOS_INFO, "done\n");
468 * Configure devices on the devices tree.
470 * Starting at the root of the device tree, travel it recursively in two
471 * passes. In the first pass, we compute and allocate resources (ranges)
472 * required by each device. In the second pass, the resources ranges are
473 * relocated to their final position and stored to the hardware.
475 * I/O resources grow upward. MEM resources grow downward.
477 * Since the assignment is hierarchical we set the values into the dev_root
478 * struct.
480 void dev_configure(void)
482 const struct device *root;
484 set_vga_bridge_bits();
486 printk(BIOS_INFO, "Allocating resources...\n");
488 root = &dev_root;
491 * Each domain should create resources which contain the entire address
492 * space for IO, MEM, and PREFMEM resources in the domain. The
493 * allocation of device resources will be done from this address space.
496 /* Read the resources for the entire tree. */
498 printk(BIOS_INFO, "Reading resources...\n");
499 read_resources(root->downstream);
500 printk(BIOS_INFO, "Done reading resources.\n");
502 print_resource_tree(root, BIOS_SPEW, "After reading.");
504 allocate_resources(root);
506 assign_resources(root->downstream);
507 printk(BIOS_INFO, "Done setting resources.\n");
508 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
510 printk(BIOS_INFO, "Done allocating resources.\n");
514 * Enable devices on the device tree.
516 * Starting at the root, walk the tree and enable all devices/bridges by
517 * calling the device's enable_resources() method.
519 void dev_enable(void)
521 printk(BIOS_INFO, "Enabling resources...\n");
523 /* Now enable everything. */
524 if (dev_root.downstream)
525 enable_resources(dev_root.downstream);
527 printk(BIOS_INFO, "done.\n");
531 * Initialize a specific device.
533 * The parent should be initialized first to avoid having an ordering problem.
534 * This is done by calling the parent's init() method before its children's
535 * init() methods.
537 * @param dev The device to be initialized.
539 static void init_dev(struct device *dev)
541 if (!dev->enabled)
542 return;
544 if (!dev->initialized && dev->ops && dev->ops->init) {
545 struct stopwatch sw;
546 long init_time;
548 if (dev->path.type == DEVICE_PATH_I2C) {
549 printk(BIOS_DEBUG, "smbus: %s->", dev_path(dev->upstream->dev));
552 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
554 stopwatch_init(&sw);
555 dev->initialized = 1;
556 dev->ops->init(dev);
558 init_time = stopwatch_duration_msecs(&sw);
559 printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev),
560 init_time);
564 static void init_link(struct bus *link)
566 struct device *dev;
568 for (dev = link->children; dev; dev = dev->sibling) {
569 post_code(POSTCODE_BS_DEV_INIT);
570 post_log_path(dev);
571 init_dev(dev);
574 for (dev = link->children; dev; dev = dev->sibling)
575 if (dev->downstream)
576 init_link(dev->downstream);
580 * Initialize all devices in the global device tree.
582 * Starting at the root device, call the device's init() method to do
583 * device-specific setup, then call each child's init() method.
585 void dev_initialize(void)
587 printk(BIOS_INFO, "Initializing devices...\n");
589 /* First call the mainboard init. */
590 init_dev(&dev_root);
592 /* Now initialize everything. */
593 if (dev_root.downstream)
594 init_link(dev_root.downstream);
595 post_log_clear();
597 printk(BIOS_INFO, "Devices initialized\n");
598 show_all_devs(BIOS_SPEW, "After init.");
602 * Finalize a specific device.
604 * The parent should be finalized first to avoid having an ordering problem.
605 * This is done by calling the parent's final() method before its children's
606 * final() methods.
608 * @param dev The device to be initialized.
610 static void final_dev(struct device *dev)
612 if (!dev->enabled)
613 return;
615 if (dev->ops && dev->ops->final) {
616 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
617 dev->ops->final(dev);
621 static void final_link(struct bus *link)
623 struct device *dev;
625 for (dev = link->children; dev; dev = dev->sibling)
626 final_dev(dev);
628 for (dev = link->children; dev; dev = dev->sibling)
629 if (dev->downstream)
630 final_link(dev->downstream);
633 * Finalize all devices in the global device tree.
635 * Starting at the root device, call the device's final() method to do
636 * device-specific cleanup, then call each child's final() method.
638 void dev_finalize(void)
640 printk(BIOS_INFO, "Finalize devices...\n");
642 /* First call the mainboard finalize. */
643 final_dev(&dev_root);
645 /* Now finalize everything. */
646 final_link(dev_root.downstream);
648 printk(BIOS_INFO, "Devices finalized\n");