blob: fe1ced5805ba2b075d67bf9c2c828593554253ee [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Uwe Hermanne4870472010-11-04 23:23:47 +00002
3/*
Martin Roth99f83bb2019-09-15 20:57:18 -07004 * Originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
Eric Biederman8ca8d762003-04-22 19:02:15 +00005 */
6
7#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +00008#include <device/device.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +02009#include <device/pci_def.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000010#include <device/pci_ids.h>
Kyösti Mälkkice39ba92020-01-04 16:15:50 +020011#include <post.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000012#include <stdlib.h>
13#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000014#include <smp/spinlock.h>
Kyösti Mälkki7336f972020-06-08 06:05:03 +030015#if ENV_X86
Duncan Laurieb4aaaa72012-01-17 09:03:11 -080016#include <arch/ebda.h>
17#endif
Aaron Durbin05294292013-04-30 15:41:13 -050018#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000019
Li-Ta Loe5266692004-03-23 21:28:05 +000020/** Pointer to the last device */
Myles Watson70679a02010-09-01 21:03:03 +000021extern struct device *last_dev;
Myles Watsonc25cc112010-05-21 14:33:48 +000022/** Linked list of free resources */
23struct resource *free_resources = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +000024
Nico Huberacd7d952012-07-25 10:33:05 +020025/**
26 * Initialize all chips of statically known devices.
27 *
28 * Will be called before bus enumeration to initialize chips stated in the
29 * device tree.
30 */
31void dev_initialize_chips(void)
32{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020033 const struct device *dev;
Nico Huberacd7d952012-07-25 10:33:05 +020034
35 for (dev = all_devices; dev; dev = dev->next) {
36 /* Initialize chip if we haven't yet. */
37 if (dev->chip_ops && dev->chip_ops->init &&
38 !dev->chip_ops->initialized) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -070039 post_log_path(dev);
Nico Huberacd7d952012-07-25 10:33:05 +020040 dev->chip_ops->init(dev->chip_info);
41 dev->chip_ops->initialized = 1;
42 }
43 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -070044 post_log_clear();
Nico Huberacd7d952012-07-25 10:33:05 +020045}
46
Marc Jones2a58ecd2013-10-29 17:32:00 -060047/**
48 * Finalize all chips of statically known devices.
49 *
50 * This is the last call before calling the payload. This is a good place
51 * to lock registers or other final cleanup.
52 */
53void dev_finalize_chips(void)
54{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020055 const struct device *dev;
Marc Jones2a58ecd2013-10-29 17:32:00 -060056
57 for (dev = all_devices; dev; dev = dev->next) {
58 /* Initialize chip if we haven't yet. */
59 if (dev->chip_ops && dev->chip_ops->final &&
60 !dev->chip_ops->finalized) {
61 dev->chip_ops->final(dev->chip_info);
62 dev->chip_ops->finalized = 1;
63 }
64 }
65}
66
Uwe Hermannc1ee4292010-10-17 19:01:48 +000067DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000068
Julius Wernercd49cce2019-03-05 16:53:33 -080069#if CONFIG(GFXUMA)
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +030070/* IGD UMA memory */
71uint64_t uma_memory_base = 0;
72uint64_t uma_memory_size = 0;
Kyösti Mälkkib25374c2012-08-01 08:05:22 +030073#endif
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +030074
Li-Ta Loe5266692004-03-23 21:28:05 +000075/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000076 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +000077 *
Martin Roth63373ed2013-07-08 16:24:19 -060078 * Allocate a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +000079 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +000080 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000081 * @param parent Parent bus the newly created device should be attached to.
82 * @param path Path to the device to be created.
83 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +000084 *
85 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +000086 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +020087static struct device *__alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +000088{
Elyes HAOUASe3480662018-05-06 20:32:23 +020089 struct device *dev, *child;
Li-Ta Loe5266692004-03-23 21:28:05 +000090
Myles Watson29cc9ed2009-07-02 18:56:24 +000091 /* Find the last child of our parent. */
Elyes HAOUASa342f392018-10-17 10:56:26 +020092 for (child = parent->children; child && child->sibling; /* */)
Eric Biedermane9a271e32003-09-02 03:36:25 +000093 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +000094
Eric Biedermane9a271e32003-09-02 03:36:25 +000095 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +000096 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +000097 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +000098
Eric Biedermane9a271e32003-09-02 03:36:25 +000099 memset(dev, 0, sizeof(*dev));
100 memcpy(&dev->path, path, sizeof(*path));
101
Myles Watson29cc9ed2009-07-02 18:56:24 +0000102 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +0000103 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000104
Eric Biedermanb78c1972004-10-14 20:54:17 +0000105 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000106 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000107 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000108 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000109 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000110 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000111
Eric Biederman03acab62004-10-14 21:25:53 +0000112 /* Append a new device to the global device list.
113 * The list is used to find devices once everything is set up.
114 */
Myles Watson70679a02010-09-01 21:03:03 +0000115 last_dev->next = dev;
116 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000117
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300118 return dev;
119}
120
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200121struct device *alloc_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300122{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200123 struct device *dev;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300124 spin_lock(&dev_lock);
125 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000126 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000127 return dev;
128}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000129
Li-Ta Loe5266692004-03-23 21:28:05 +0000130/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300131 * See if a device structure already exists and if not allocate it.
132 *
133 * @param parent The bus to find the device on.
134 * @param path The relative path from the bus to the appropriate device.
135 * @return Pointer to a device structure for the device on bus at path.
136 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200137struct device *alloc_find_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300138{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200139 struct device *child;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300140 spin_lock(&dev_lock);
141 child = find_dev_path(parent, path);
142 if (!child)
143 child = __alloc_dev(parent, path);
144 spin_unlock(&dev_lock);
145 return child;
146}
147
148/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000149 * Read the resources on all devices of a given bus.
150 *
151 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000152 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000153static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000154{
155 struct device *curdev;
156
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000157 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
158 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000159
Myles Watson29cc9ed2009-07-02 18:56:24 +0000160 /* Walk through all devices and find which resources they need. */
161 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000162 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000163
164 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000165 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000166
Eric Biedermane9a271e32003-09-02 03:36:25 +0000167 if (!curdev->ops || !curdev->ops->read_resources) {
Martin Roth7bc74ab2015-11-18 20:23:02 -0700168 if (curdev->path.type != DEVICE_PATH_APIC)
Frans Hendriksa9caa502021-02-01 11:44:37 +0100169 printk(BIOS_ERR, "%s missing %s\n",
170 dev_path(curdev), __func__);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000171 continue;
172 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700173 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000175
176 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000177 for (link = curdev->link_list; link; link = link->next)
178 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700180 post_log_clear();
Frans Hendriksa9caa502021-02-01 11:44:37 +0100181 printk(BIOS_SPEW, "%s %s bus %d link: %d done\n",
182 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183}
184
Elyes HAOUASe3480662018-05-06 20:32:23 +0200185struct device *vga_pri = NULL;
Myles Watsonc7233e02009-07-02 19:02:33 +0000186static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000187{
Uwe Hermann312673c2009-10-27 21:49:33 +0000188 /*
Martin Roth63373ed2013-07-08 16:24:19 -0600189 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000190 * This function knows too much about PCI stuff, it should be just
191 * an iterator/visitor.
192 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000193
Myles Watson29cc9ed2009-07-02 18:56:24 +0000194 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200195 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000196 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000197
Eric Biedermanb78c1972004-10-14 20:54:17 +0000198 bus = 0;
199 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000200 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000201
Patrick Georgi557ecf22012-07-20 12:16:17 +0200202 dev = NULL;
203 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000204 if (!dev->enabled)
205 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000206
Patrick Georgi557ecf22012-07-20 12:16:17 +0200207 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Nico Huber061b9052019-09-21 15:58:23 +0200208 if (dev->bus->no_vga16) {
209 printk(BIOS_WARNING,
210 "A bridge on the path doesn't support 16-bit VGA decoding!");
211 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000212
Frans Hendriksa9caa502021-02-01 11:44:37 +0100213 if (dev->on_mainboard)
Patrick Georgi557ecf22012-07-20 12:16:17 +0200214 vga_onboard = dev;
Frans Hendriksa9caa502021-02-01 11:44:37 +0100215 else
Patrick Georgi557ecf22012-07-20 12:16:17 +0200216 vga = dev;
Myles Watson032a9652009-05-11 22:24:53 +0000217
Patrick Georgi557ecf22012-07-20 12:16:17 +0200218 /* It isn't safe to enable all VGA cards. */
219 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200220 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000221
Uwe Hermanne4870472010-11-04 23:23:47 +0000222 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000223 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200224
Julius Werner5d1f9a02019-03-07 17:07:26 -0800225 if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard)
Patrick Georgi557ecf22012-07-20 12:16:17 +0200226 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000227
Patrick Georgi5869fa22012-07-20 12:29:33 +0200228 /* If we prefer plugin VGA over chipset VGA, the chipset might
229 want to know. */
Julius Werner5d1f9a02019-03-07 17:07:26 -0800230 if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) &&
Patrick Georgi5869fa22012-07-20 12:29:33 +0200231 vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) {
232 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
233 vga_onboard->ops->disable(vga_onboard);
234 }
235
arch import user (historical)dc811182005-07-06 17:16:09 +0000236 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000237 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000239 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000240 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
241 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000242 bus = vga->bus;
243 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000244
Myles Watson29cc9ed2009-07-02 18:56:24 +0000245 /* Now walk up the bridges setting the VGA enable. */
246 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000247 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000248 dev_path(bus->dev));
Nico Huber061b9052019-09-21 15:58:23 +0200249 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000250 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000251 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000252}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000253
Li-Ta Lo04930692004-11-25 17:37:19 +0000254/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000255 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000256 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000257 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000258 * resources to hardware. For bridge devices, the set_resources() method
259 * has to recurse into every down stream buses.
260 *
261 * Mutual recursion:
262 * assign_resources() -> device_operation::set_resources()
263 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000264 *
265 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000266 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000267void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000268{
269 struct device *curdev;
270
Frans Hendriksa9caa502021-02-01 11:44:37 +0100271 printk(BIOS_SPEW, "%s %s, bus %d link: %d\n",
272 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000273
Myles Watson29cc9ed2009-07-02 18:56:24 +0000274 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000275 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000276 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000277
Eric Biedermane9a271e32003-09-02 03:36:25 +0000278 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000279 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000280 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000281 continue;
282 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700283 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000284 curdev->ops->set_resources(curdev);
285 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700286 post_log_clear();
Frans Hendriksa9caa502021-02-01 11:44:37 +0100287 printk(BIOS_SPEW, "%s %s, bus %d link: %d\n",
288 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000289}
290
Li-Ta Lo5782d272004-04-26 17:51:20 +0000291/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000292 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000293 *
294 * Enable resources of the device by calling the device specific
295 * enable_resources() method.
296 *
297 * The parent's resources should be enabled first to avoid having enabling
298 * order problem. This is done by calling the parent's enable_resources()
Martin Roth63373ed2013-07-08 16:24:19 -0600299 * method before its children's enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000300 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000301 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000302 */
Myles Watson7eac4452010-06-17 16:16:56 +0000303static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000304{
Myles Watson7eac4452010-06-17 16:16:56 +0000305 struct device *dev;
306 struct bus *c_link;
307
308 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700309 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
310 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000311 dev->ops->enable_resources(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700312 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000313 }
Myles Watson7eac4452010-06-17 16:16:56 +0000314
315 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000316 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000317 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000318 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700319 post_log_clear();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000320}
321
Myles Watson032a9652009-05-11 22:24:53 +0000322/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000323 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
324 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000325 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000326 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000327 */
328int reset_bus(struct bus *bus)
329{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000330 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000331 bus->dev->ops->reset_bus(bus);
332 bus->reset_needed = 0;
333 return 1;
334 }
335 return 0;
336}
337
Myles Watson032a9652009-05-11 22:24:53 +0000338/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000339 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000340 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000341 * If there are bridges on the bus, recursively scan the buses behind the
342 * bridges. If the setting up and tuning of the bus causes a reset to be
343 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000344 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000345 * @param busdev Pointer to the bus device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000346 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200347static void scan_bus(struct device *busdev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000348{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000349 int do_scan_bus;
Paul Menzel662380f2015-10-20 10:21:08 +0200350 struct stopwatch sw;
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200351 long scan_time;
Uwe Hermanne4870472010-11-04 23:23:47 +0000352
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200353 if (!busdev->enabled)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200354 return;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200355
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200356 printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000357
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700358 post_log_path(busdev);
359
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200360 stopwatch_init(&sw);
361
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000362 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000363 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000364 struct bus *link;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200365 busdev->ops->scan_bus(busdev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000366 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000367 for (link = busdev->link_list; link; link = link->next) {
368 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000369 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000370 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000371 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000372 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000373 }
374 }
375 }
Paul Menzel662380f2015-10-20 10:21:08 +0200376
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200377 scan_time = stopwatch_duration_msecs(&sw);
378 printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__,
379 dev_path(busdev), scan_time);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000380}
381
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200382void scan_bridges(struct bus *bus)
383{
384 struct device *child;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200385
386 for (child = bus->children; child; child = child->sibling) {
387 if (!child->ops || !child->ops->scan_bus)
388 continue;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200389 scan_bus(child);
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200390 }
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200391}
392
Li-Ta Loe5266692004-03-23 21:28:05 +0000393/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000394 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000395 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000396 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000397 * file. The device structures for these devices are generated at compile
398 * time by the config tool and are organized into the device tree. This
399 * function determines if the devices created at compile time actually exist
400 * in the physical system.
401 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000402 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000403 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000404 * device tree.
405 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000406 * This function starts from the root device 'dev_root', scans the buses in
407 * the system recursively, and modifies the device tree according to the
408 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000409 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000410 * This function has no idea how to scan and probe buses and devices at all.
411 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000412 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000413 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000414 */
415void dev_enumerate(void)
416{
417 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000418
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000419 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000420
Eric Biederman8ca8d762003-04-22 19:02:15 +0000421 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000422
Uwe Hermanne4870472010-11-04 23:23:47 +0000423 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000424 printk(BIOS_SPEW, "Compare with tree...\n");
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300425 show_devs_tree(root, BIOS_SPEW, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000426
Stefan Reinauera675d492012-08-07 14:50:47 -0700427 if (root->chip_ops && root->chip_ops->enable_dev)
428 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000429
Eric Biedermanb78c1972004-10-14 20:54:17 +0000430 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000431 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000432 return;
433 }
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200434 scan_bus(root);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700435 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000436 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000437}
438
Li-Ta Loe5266692004-03-23 21:28:05 +0000439/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000440 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +0000441 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000442 * Starting at the root of the device tree, travel it recursively in two
443 * passes. In the first pass, we compute and allocate resources (ranges)
Martin Roth63373ed2013-07-08 16:24:19 -0600444 * required by each device. In the second pass, the resources ranges are
Li-Ta Lo04930692004-11-25 17:37:19 +0000445 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000446 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000447 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000448 *
449 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +0000450 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000451 */
452void dev_configure(void)
453{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200454 const struct device *root;
Li-Ta Loe5266692004-03-23 21:28:05 +0000455
Marc Jones80bd74c2012-02-21 17:44:35 +0100456 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +0100457
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000458 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000459
Eric Biedermanb78c1972004-10-14 20:54:17 +0000460 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000461
Uwe Hermanne4870472010-11-04 23:23:47 +0000462 /*
463 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +0000464 * space for IO, MEM, and PREFMEM resources in the domain. The
465 * allocation of device resources will be done from this address space.
466 */
Myles Watsoncd5d7562009-05-12 13:43:34 +0000467
Myles Watson29cc9ed2009-07-02 18:56:24 +0000468 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +0000469
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000470 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +0000471 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000472 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000473
Stefan Reinauer39e72292009-10-26 16:47:05 +0000474 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +0000475
Furquan Shaikh69395742020-05-15 15:43:15 -0700476 allocate_resources(root);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000477
Myles Watson894a3472010-06-09 22:41:35 +0000478 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000479 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000480 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +0000481
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000482 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000483}
484
Li-Ta Loe5266692004-03-23 21:28:05 +0000485/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000486 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +0000487 *
488 * Starting at the root, walk the tree and enable all devices/bridges by
489 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000490 */
491void dev_enable(void)
492{
Myles Watson7eac4452010-06-17 16:16:56 +0000493 struct bus *link;
494
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000495 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000496
Uwe Hermanne4870472010-11-04 23:23:47 +0000497 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +0000498 for (link = dev_root.link_list; link; link = link->next)
499 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +0000500
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000501 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000502}
503
Li-Ta Loe5266692004-03-23 21:28:05 +0000504/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000505 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +0000506 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000507 * The parent should be initialized first to avoid having an ordering problem.
Martin Roth63373ed2013-07-08 16:24:19 -0600508 * This is done by calling the parent's init() method before its children's
Uwe Hermanne4870472010-11-04 23:23:47 +0000509 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +0000510 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000511 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +0000512 */
513static void init_dev(struct device *dev)
514{
Uwe Hermanne4870472010-11-04 23:23:47 +0000515 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +0000516 return;
Myles Watson7eac4452010-06-17 16:16:56 +0000517
518 if (!dev->initialized && dev->ops && dev->ops->init) {
Aaron Durbin46ba4802014-09-23 16:34:40 -0500519 struct stopwatch sw;
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200520 long init_time;
521
Myles Watson7eac4452010-06-17 16:16:56 +0000522 if (dev->path.type == DEVICE_PATH_I2C) {
523 printk(BIOS_DEBUG, "smbus: %s[%d]->",
524 dev_path(dev->bus->dev), dev->bus->link_num);
525 }
526
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200527 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
528
529 stopwatch_init(&sw);
Myles Watson7eac4452010-06-17 16:16:56 +0000530 dev->initialized = 1;
531 dev->ops->init(dev);
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200532
533 init_time = stopwatch_duration_msecs(&sw);
534 printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev),
535 init_time);
Myles Watson7eac4452010-06-17 16:16:56 +0000536 }
537}
538
539static void init_link(struct bus *link)
540{
541 struct device *dev;
542 struct bus *c_link;
543
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700544 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Lauriecb73a842013-06-10 10:41:04 -0700545 post_code(POST_BS_DEV_INIT);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700546 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000547 init_dev(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700548 }
Myles Watson7eac4452010-06-17 16:16:56 +0000549
550 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000551 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000552 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +0000553 }
554}
555
556/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000557 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +0000558 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000559 * Starting at the root device, call the device's init() method to do
560 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000561 */
562void dev_initialize(void)
563{
Myles Watson7eac4452010-06-17 16:16:56 +0000564 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000565
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000566 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +0000567
Kyösti Mälkki7336f972020-06-08 06:05:03 +0300568#if ENV_X86
Arthur Heymans8b7cd432019-10-26 20:31:41 +0200569 /* Ensure EBDA is prepared before Option ROMs. */
570 setup_default_ebda();
Duncan Laurieb4aaaa72012-01-17 09:03:11 -0800571#endif
572
Myles Watson1bd3fb72010-08-16 16:25:23 +0000573 /* First call the mainboard init. */
574 init_dev(&dev_root);
575
Uwe Hermanne4870472010-11-04 23:23:47 +0000576 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +0000577 for (link = dev_root.link_list; link; link = link->next)
578 init_link(link);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700579 post_log_clear();
Myles Watson7eac4452010-06-17 16:16:56 +0000580
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000581 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000582 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000583}
Marc Jones2a58ecd2013-10-29 17:32:00 -0600584
585/**
586 * Finalize a specific device.
587 *
588 * The parent should be finalized first to avoid having an ordering problem.
589 * This is done by calling the parent's final() method before its childrens'
590 * final() methods.
591 *
592 * @param dev The device to be initialized.
593 */
594static void final_dev(struct device *dev)
595{
596 if (!dev->enabled)
597 return;
598
599 if (dev->ops && dev->ops->final) {
600 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
601 dev->ops->final(dev);
602 }
603}
604
605static void final_link(struct bus *link)
606{
607 struct device *dev;
608 struct bus *c_link;
609
610 for (dev = link->children; dev; dev = dev->sibling)
611 final_dev(dev);
612
613 for (dev = link->children; dev; dev = dev->sibling) {
614 for (c_link = dev->link_list; c_link; c_link = c_link->next)
615 final_link(c_link);
616 }
617}
618/**
619 * Finalize all devices in the global device tree.
620 *
621 * Starting at the root device, call the device's final() method to do
622 * device-specific cleanup, then call each child's final() method.
623 */
624void dev_finalize(void)
625{
626 struct bus *link;
627
628 printk(BIOS_INFO, "Finalize devices...\n");
629
630 /* First call the mainboard finalize. */
631 final_dev(&dev_root);
632
633 /* Now finalize everything. */
634 for (link = dev_root.link_list; link; link = link->next)
635 final_link(link);
636
637 printk(BIOS_INFO, "Devices finalized\n");
638}