1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * Originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
7 #include <console/console.h>
8 #include <device/device.h>
9 #include <device/pci_def.h>
10 #include <device/pci_ids.h>
14 #include <smp/spinlock.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
);
30 * Initialize all chips of statically known devices.
32 * Will be called before bus enumeration to initialize chips stated in the
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
) {
44 dev
->chip_ops
->init(dev
->chip_info
);
45 dev
->chip_ops
->initialized
= 1;
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
)
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.
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
));
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. */
103 /* Add the new device to the list of children of the bus. */
106 child
->sibling
= dev
;
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
;
119 struct device
*alloc_dev(struct bus
*parent
, struct device_path
*path
)
122 spin_lock(&dev_lock
);
123 dev
= __alloc_dev(parent
, path
);
124 spin_unlock(&dev_lock
);
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
);
141 child
= __alloc_dev(parent
, path
);
142 spin_unlock(&dev_lock
);
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
) {
162 if (!curdev
->enabled
)
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__
);
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
);
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
;
201 while ((dev
= dev_find_class(PCI_CLASS_DISPLAY_VGA
<< 8, dev
))) {
205 printk(BIOS_DEBUG
, "found VGA at %s\n", dev_path(dev
));
206 if (dev
->bus
->no_vga16
) {
208 "A bridge on the path doesn't support 16-bit VGA decoding!");
211 if (dev
->on_mainboard
)
216 /* It isn't safe to enable all VGA cards. */
217 dev
->command
&= ~(PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
);
223 if (CONFIG(ONBOARD_VGA_IS_PRIMARY
) && vga_onboard
)
226 /* If we prefer plugin VGA over chipset VGA, the chipset might
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
);
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
);
243 /* Now walk up the bridges setting the VGA enable. */
245 printk(BIOS_DEBUG
, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
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.
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
)
276 if (!curdev
->ops
|| !curdev
->ops
->set_resources
) {
277 printk(BIOS_ERR
, "%s missing set_resources\n",
281 post_log_path(curdev
);
282 curdev
->ops
->set_resources(curdev
);
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
)
306 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
307 if (dev
->enabled
&& dev
->ops
&& dev
->ops
->enable_resources
) {
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
);
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;
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
)
351 if (!busdev
->enabled
)
354 printk(BIOS_DEBUG
, "%s scanning...\n", dev_path(busdev
));
356 post_log_path(busdev
);
361 while (do_scan_bus
) {
363 busdev
->ops
->scan_bus(busdev
);
365 for (link
= busdev
->link_list
; link
; link
= link
->next
) {
366 if (link
->reset_needed
) {
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
)
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
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)
417 printk(BIOS_INFO
, "Enumerating buses...\n");
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");
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
450 void dev_configure(void)
452 const struct device
*root
;
454 set_vga_bridge_bits();
456 printk(BIOS_INFO
, "Allocating resources...\n");
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)
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
509 * @param dev The device to be initialized.
511 static void init_dev(struct device
*dev
)
516 if (!dev
->initialized
&& dev
->ops
&& dev
->ops
->init
) {
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
));
528 dev
->initialized
= 1;
531 init_time
= stopwatch_duration_msecs(&sw
);
532 printk(BIOS_DEBUG
, "%s init finished in %ld msecs\n", dev_path(dev
),
537 static void init_link(struct bus
*link
)
542 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
543 post_code(POST_BS_DEV_INIT
);
548 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
549 for (c_link
= dev
->link_list
; c_link
; c_link
= c_link
->next
)
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)
564 printk(BIOS_INFO
, "Initializing devices...\n");
566 /* First call the mainboard init. */
569 /* Now initialize everything. */
570 for (link
= dev_root
.link_list
; link
; link
= link
->next
)
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'
585 * @param dev The device to be initialized.
587 static void final_dev(struct device
*dev
)
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
)
603 for (dev
= link
->children
; dev
; dev
= dev
->sibling
)
606 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
607 for (c_link
= dev
->link_list
; c_link
; c_link
= c_link
->next
)
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)
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
)
630 printk(BIOS_INFO
, "Devices finalized\n");