blob: 5b4c264752839c6b7f4b58b4abc8f160f88a608e [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * It was originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
5 *
6 * Modifications are:
7 * Copyright (C) 2003 Eric Biederman <ebiederm@xmission.com>
8 * Copyright (C) 2003-2004 Linux Networx
9 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
10 * Copyright (C) 2003 Ronald G. Minnich <rminnich@gmail.com>
11 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
12 * Copyright (C) 2005-2006 Tyan
13 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
14 * Copyright (C) 2005-2006 Stefan Reinauer <stepan@openbios.org>
Myles Watson29cc9ed2009-07-02 18:56:24 +000015 * Copyright (C) 2009 Myles Watson <mylesgw@gmail.com>
Martin Rothbb5953d2016-04-11 20:53:39 -060016 * Copyright (c) 1999--2000 Martin Mares <mj@suse.cz>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; version 2 of the License.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
Eric Biederman8ca8d762003-04-22 19:02:15 +000026 */
Uwe Hermanne4870472010-11-04 23:23:47 +000027
28/*
29 * Lots of mods by Ron Minnich <rminnich@lanl.gov>, with
30 * the final architecture guidance from Tom Merritt <tjm@codegen.com>.
31 *
Myles Watson032a9652009-05-11 22:24:53 +000032 * In particular, we changed from the one-pass original version to
33 * Tom's recommended multiple-pass version. I wasn't sure about doing
Eric Biederman8ca8d762003-04-22 19:02:15 +000034 * it with multiple passes, until I actually started doing it and saw
Uwe Hermanne4870472010-11-04 23:23:47 +000035 * the wisdom of Tom's recommendations...
Eric Biederman8ca8d762003-04-22 19:02:15 +000036 *
37 * Lots of cleanups by Eric Biederman to handle bridges, and to
Uwe Hermanne4870472010-11-04 23:23:47 +000038 * handle resource allocation for non-PCI devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +000039 */
40
41#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000042#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000043#include <device/device.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020044#include <device/pci_def.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000045#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000046#include <stdlib.h>
47#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000048#include <smp/spinlock.h>
Martin Rothb3b114c2017-06-24 14:00:01 -060049#if IS_ENABLED(CONFIG_ARCH_X86)
Duncan Laurieb4aaaa72012-01-17 09:03:11 -080050#include <arch/ebda.h>
51#endif
Aaron Durbin05294292013-04-30 15:41:13 -050052#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000053
Li-Ta Loe5266692004-03-23 21:28:05 +000054/** Linked list of ALL devices */
Eric Biederman5cd81732004-03-11 15:01:31 +000055struct device *all_devices = &dev_root;
Li-Ta Loe5266692004-03-23 21:28:05 +000056/** Pointer to the last device */
Myles Watson70679a02010-09-01 21:03:03 +000057extern struct device *last_dev;
Myles Watsonc25cc112010-05-21 14:33:48 +000058/** Linked list of free resources */
59struct resource *free_resources = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +000060
Nico Huberacd7d952012-07-25 10:33:05 +020061/**
62 * Initialize all chips of statically known devices.
63 *
64 * Will be called before bus enumeration to initialize chips stated in the
65 * device tree.
66 */
67void dev_initialize_chips(void)
68{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020069 const struct device *dev;
Nico Huberacd7d952012-07-25 10:33:05 +020070
71 for (dev = all_devices; dev; dev = dev->next) {
72 /* Initialize chip if we haven't yet. */
73 if (dev->chip_ops && dev->chip_ops->init &&
74 !dev->chip_ops->initialized) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -070075 post_log_path(dev);
Nico Huberacd7d952012-07-25 10:33:05 +020076 dev->chip_ops->init(dev->chip_info);
77 dev->chip_ops->initialized = 1;
78 }
79 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -070080 post_log_clear();
Nico Huberacd7d952012-07-25 10:33:05 +020081}
82
Marc Jones2a58ecd2013-10-29 17:32:00 -060083/**
84 * Finalize all chips of statically known devices.
85 *
86 * This is the last call before calling the payload. This is a good place
87 * to lock registers or other final cleanup.
88 */
89void dev_finalize_chips(void)
90{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020091 const struct device *dev;
Marc Jones2a58ecd2013-10-29 17:32:00 -060092
93 for (dev = all_devices; dev; dev = dev->next) {
94 /* Initialize chip if we haven't yet. */
95 if (dev->chip_ops && dev->chip_ops->final &&
96 !dev->chip_ops->finalized) {
97 dev->chip_ops->final(dev->chip_info);
98 dev->chip_ops->finalized = 1;
99 }
100 }
101}
102
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000103DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000104
Martin Rothb3b114c2017-06-24 14:00:01 -0600105#if IS_ENABLED(CONFIG_GFXUMA)
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300106/* IGD UMA memory */
107uint64_t uma_memory_base = 0;
108uint64_t uma_memory_size = 0;
Kyösti Mälkkib25374c2012-08-01 08:05:22 +0300109#endif
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300110
Li-Ta Loe5266692004-03-23 21:28:05 +0000111/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000112 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +0000113 *
Martin Roth63373ed2013-07-08 16:24:19 -0600114 * Allocate a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000115 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000116 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000117 * @param parent Parent bus the newly created device should be attached to.
118 * @param path Path to the device to be created.
119 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +0000120 *
121 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122 */
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300123static device_t __alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000124{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200125 struct device *dev, *child;
Li-Ta Loe5266692004-03-23 21:28:05 +0000126
Myles Watson29cc9ed2009-07-02 18:56:24 +0000127 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000128 for (child = parent->children; child && child->sibling; /* */ )
Eric Biedermane9a271e32003-09-02 03:36:25 +0000129 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +0000130
Eric Biedermane9a271e32003-09-02 03:36:25 +0000131 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000132 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +0000133 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000134
Eric Biedermane9a271e32003-09-02 03:36:25 +0000135 memset(dev, 0, sizeof(*dev));
136 memcpy(&dev->path, path, sizeof(*path));
137
Myles Watson29cc9ed2009-07-02 18:56:24 +0000138 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +0000139 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000140
Eric Biedermanb78c1972004-10-14 20:54:17 +0000141 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000142 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000143 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000144 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000145 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000146 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000147
Eric Biederman03acab62004-10-14 21:25:53 +0000148 /* Append a new device to the global device list.
149 * The list is used to find devices once everything is set up.
150 */
Myles Watson70679a02010-09-01 21:03:03 +0000151 last_dev->next = dev;
152 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000153
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300154 return dev;
155}
156
157device_t alloc_dev(struct bus *parent, struct device_path *path)
158{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200159 struct device *dev;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300160 spin_lock(&dev_lock);
161 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000162 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000163 return dev;
164}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000165
Li-Ta Loe5266692004-03-23 21:28:05 +0000166/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300167 * See if a device structure already exists and if not allocate it.
168 *
169 * @param parent The bus to find the device on.
170 * @param path The relative path from the bus to the appropriate device.
171 * @return Pointer to a device structure for the device on bus at path.
172 */
173device_t alloc_find_dev(struct bus *parent, struct device_path *path)
174{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200175 struct device *child;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300176 spin_lock(&dev_lock);
177 child = find_dev_path(parent, path);
178 if (!child)
179 child = __alloc_dev(parent, path);
180 spin_unlock(&dev_lock);
181 return child;
182}
183
184/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000185 * Round a number up to an alignment.
186 *
187 * @param val The starting value.
Martin Roth32bc6b62015-01-04 16:54:35 -0700188 * @param pow Alignment as a power of two.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000189 * @return Rounded up number.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000190 */
Eric Biederman448bd632004-10-14 22:52:15 +0000191static resource_t round(resource_t val, unsigned long pow)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000192{
Eric Biederman448bd632004-10-14 22:52:15 +0000193 resource_t mask;
194 mask = (1ULL << pow) - 1ULL;
195 val += mask;
196 val &= ~mask;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000197 return val;
198}
199
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200200static const char * resource2str(struct resource *res)
201{
202 if (res->flags & IORESOURCE_IO)
203 return "io";
204 if (res->flags & IORESOURCE_PREFETCH)
205 return "prefmem";
206 if (res->flags & IORESOURCE_MEM)
207 return "mem";
208 return "undefined";
209}
210
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000211/**
212 * Read the resources on all devices of a given bus.
213 *
214 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000215 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000216static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000217{
218 struct device *curdev;
219
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000220 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
221 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000222
Myles Watson29cc9ed2009-07-02 18:56:24 +0000223 /* Walk through all devices and find which resources they need. */
224 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000225 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000226
227 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000228 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000229
Eric Biedermane9a271e32003-09-02 03:36:25 +0000230 if (!curdev->ops || !curdev->ops->read_resources) {
Martin Roth7bc74ab2015-11-18 20:23:02 -0700231 if (curdev->path.type != DEVICE_PATH_APIC)
232 printk(BIOS_ERR, "%s missing read_resources\n",
233 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000234 continue;
235 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700236 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000237 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000238
239 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000240 for (link = curdev->link_list; link; link = link->next)
241 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000242 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700243 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000244 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000245 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000246}
247
Eric Biedermane9a271e32003-09-02 03:36:25 +0000248struct pick_largest_state {
249 struct resource *last;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200250 const struct device *result_dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000251 struct resource *result;
252 int seen_last;
253};
254
Myles Watson29cc9ed2009-07-02 18:56:24 +0000255static void pick_largest_resource(void *gp, struct device *dev,
256 struct resource *resource)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000257{
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000258 struct pick_largest_state *state = gp;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000259 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000260
Eric Biedermane9a271e32003-09-02 03:36:25 +0000261 last = state->last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000262
263 /* Be certain to pick the successor to last. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000264 if (resource == last) {
265 state->seen_last = 1;
266 return;
267 }
Myles Watson032a9652009-05-11 22:24:53 +0000268 if (resource->flags & IORESOURCE_FIXED)
Uwe Hermanne4870472010-11-04 23:23:47 +0000269 return; /* Skip it. */
Myles Watson032a9652009-05-11 22:24:53 +0000270 if (last && ((last->align < resource->align) ||
271 ((last->align == resource->align) &&
272 (last->size < resource->size)) ||
273 ((last->align == resource->align) &&
274 (last->size == resource->size) && (!state->seen_last)))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000275 return;
276 }
Myles Watson032a9652009-05-11 22:24:53 +0000277 if (!state->result ||
278 (state->result->align < resource->align) ||
279 ((state->result->align == resource->align) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000280 (state->result->size < resource->size))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000281 state->result_dev = dev;
282 state->result = resource;
Myles Watson032a9652009-05-11 22:24:53 +0000283 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000284}
285
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200286static const struct device *largest_resource(struct bus *bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000287 struct resource **result_res,
288 unsigned long type_mask,
289 unsigned long type)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000290{
291 struct pick_largest_state state;
292
293 state.last = *result_res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000294 state.result_dev = NULL;
295 state.result = NULL;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000296 state.seen_last = 0;
297
Myles Watson032a9652009-05-11 22:24:53 +0000298 search_bus_resources(bus, type_mask, type, pick_largest_resource,
299 &state);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000300
301 *result_res = state.result;
302 return state.result_dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000303}
304
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000305/**
Uwe Hermanne4870472010-11-04 23:23:47 +0000306 * This function is the guts of the resource allocator.
Myles Watson032a9652009-05-11 22:24:53 +0000307 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000308 * The problem.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000309 * - Allocate resource locations for every device.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000310 * - Don't overlap, and follow the rules of bridges.
311 * - Don't overlap with resources in fixed locations.
312 * - Be efficient so we don't have ugly strategies.
313 *
314 * The strategy.
315 * - Devices that have fixed addresses are the minority so don't
Myles Watson29cc9ed2009-07-02 18:56:24 +0000316 * worry about them too much. Instead only use part of the address
317 * space for devices with programmable addresses. This easily handles
Eric Biederman8ca8d762003-04-22 19:02:15 +0000318 * everything except bridges.
319 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000320 * - PCI devices are required to have their sizes and their alignments
321 * equal. In this case an optimal solution to the packing problem
322 * exists. Allocate all devices from highest alignment to least
323 * alignment or vice versa. Use this.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000324 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000325 * - So we can handle more than PCI run two allocation passes on bridges. The
326 * first to see how large the resources are behind the bridge, and what
327 * their alignment requirements are. The second to assign a safe address to
328 * the devices behind the bridge. This allows us to treat a bridge as just
329 * a device with a couple of resources, and not need to special case it in
330 * the allocator. Also this allows handling of other types of bridges.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000331 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000332 * @param bus The bus we are traversing.
333 * @param bridge The bridge resource which must contain the bus' resources.
334 * @param type_mask This value gets ANDed with the resource type.
335 * @param type This value must match the result of the AND.
336 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000337 */
Myles Watson54913b92009-10-13 20:00:09 +0000338static void compute_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000339 unsigned long type_mask, unsigned long type)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000340{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200341 const struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000342 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000343 resource_t base;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000344 base = round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000345
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200346 printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d"
347 " limit: %llx\n", dev_path(bus->dev), resource2str(bridge),
348 base, bridge->size, bridge->align,
Uwe Hermanne4870472010-11-04 23:23:47 +0000349 bridge->gran, bridge->limit);
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000350
Uwe Hermanne4870472010-11-04 23:23:47 +0000351 /* For each child which is a bridge, compute the resource needs. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000352 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000353 struct resource *child_bridge;
354
Myles Watson894a3472010-06-09 22:41:35 +0000355 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000356 continue;
357
358 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000359 for (child_bridge = dev->resource_list; child_bridge;
360 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000361 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000362
Uwe Hermanne4870472010-11-04 23:23:47 +0000363 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
364 || (child_bridge->flags & type_mask) != type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000365 continue;
366
Uwe Hermanne4870472010-11-04 23:23:47 +0000367 /*
368 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000369 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000370 * and non-prefetchable memory. Bridges below them need
371 * it separated. Add the PREFETCH flag to the type_mask
372 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000373 */
Myles Watson894a3472010-06-09 22:41:35 +0000374 link = dev->link_list;
375 while (link && link->link_num !=
376 IOINDEX_LINK(child_bridge->index))
377 link = link->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000378
379 if (link == NULL) {
Myles Watson894a3472010-06-09 22:41:35 +0000380 printk(BIOS_ERR, "link %ld not found on %s\n",
381 IOINDEX_LINK(child_bridge->index),
382 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000383 }
384
Myles Watson894a3472010-06-09 22:41:35 +0000385 compute_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000386 type_mask | IORESOURCE_PREFETCH,
387 type | (child_bridge->flags &
388 IORESOURCE_PREFETCH));
389 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000390 }
391
Myles Watson29cc9ed2009-07-02 18:56:24 +0000392 /* Remember we haven't found anything yet. */
393 resource = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000394
Uwe Hermanne4870472010-11-04 23:23:47 +0000395 /*
396 * Walk through all the resources on the current bus and compute the
397 * amount of address space taken by them. Take granularity and
Myles Watson29cc9ed2009-07-02 18:56:24 +0000398 * alignment into account.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000399 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000400 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000401
Myles Watson29cc9ed2009-07-02 18:56:24 +0000402 /* Size 0 resources can be skipped. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000403 if (!resource->size)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000404 continue;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000405
Myles Watson29cc9ed2009-07-02 18:56:24 +0000406 /* Propagate the resource alignment to the bridge resource. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000407 if (resource->align > bridge->align)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000408 bridge->align = resource->align;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000409
410 /* Propagate the resource limit to the bridge register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000411 if (bridge->limit > resource->limit)
Eric Biedermandbec2d42004-10-21 10:44:08 +0000412 bridge->limit = resource->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000413
414 /* Warn if it looks like APICs aren't declared. */
415 if ((resource->limit == 0xffffffff) &&
416 (resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000417 printk(BIOS_ERR,
418 "Resource limit looks wrong! (no APIC?)\n");
Patrick Georgi51615092012-03-11 19:31:03 +0100419 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000420 dev_path(dev), resource->index, resource->limit);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000421 }
Stefan Reinauer51754a32008-08-01 12:28:38 +0000422
Eric Biederman8ca8d762003-04-22 19:02:15 +0000423 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000424 /*
425 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000426 * expansion card addresses. The legacy PCI decodes
427 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
428 * 0x00 - 0xff can be used out of each 0x400 block of
429 * I/O space.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000430 */
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000431 if ((base & 0x300) != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432 base = (base & ~0x3ff) + 0x400;
433 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000434 /*
435 * Don't allow allocations in the VGA I/O range.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000436 * PCI has special cases for that.
437 */
438 else if ((base >= 0x3b0) && (base <= 0x3df)) {
439 base = 0x3e0;
440 }
441 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000442 /* Base must be aligned. */
443 base = round(base, resource->align);
444 resource->base = base;
445 base += resource->size;
Myles Watson032a9652009-05-11 22:24:53 +0000446
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000447 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000448 dev_path(dev), resource->index, resource->base,
449 resource->base + resource->size - 1,
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200450 resource2str(resource));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000451 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000452
453 /*
454 * A PCI bridge resource does not need to be a power of two size, but
455 * it does have a minimum granularity. Round the size up to that
456 * minimum granularity so we know not to place something else at an
Martin Roth63373ed2013-07-08 16:24:19 -0600457 * address positively decoded by the bridge.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000458 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000459 bridge->size = round(base, bridge->gran) -
460 round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000461
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200462 printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d"
463 " limit: %llx done\n", dev_path(bus->dev),
464 resource2str(bridge),
Uwe Hermanne4870472010-11-04 23:23:47 +0000465 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000466}
467
468/**
469 * This function is the second part of the resource allocator.
470 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000471 * See the compute_resources function for a more detailed explanation.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000472 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000473 * This function assigns the resources a value.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000474 *
475 * @param bus The bus we are traversing.
476 * @param bridge The bridge resource which must contain the bus' resources.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000477 * @param type_mask This value gets ANDed with the resource type.
478 * @param type This value must match the result of the AND.
Uwe Hermanne4870472010-11-04 23:23:47 +0000479 *
480 * @see compute_resources
Myles Watson29cc9ed2009-07-02 18:56:24 +0000481 */
Myles Watson54913b92009-10-13 20:00:09 +0000482static void allocate_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000483 unsigned long type_mask, unsigned long type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000484{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200485 const struct device *dev;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000486 struct resource *resource;
487 resource_t base;
488 base = bridge->base;
489
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200490 printk(BIOS_SPEW, "%s %s: base:%llx size:%llx align:%d gran:%d "
491 "limit:%llx\n", dev_path(bus->dev),
492 resource2str(bridge),
Myles Watson29cc9ed2009-07-02 18:56:24 +0000493 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
494
495 /* Remember we haven't found anything yet. */
496 resource = NULL;
497
Uwe Hermanne4870472010-11-04 23:23:47 +0000498 /*
499 * Walk through all the resources on the current bus and allocate them
Myles Watson29cc9ed2009-07-02 18:56:24 +0000500 * address space.
501 */
502 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
503
504 /* Propagate the bridge limit to the resource register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000505 if (resource->limit > bridge->limit)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000506 resource->limit = bridge->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000507
508 /* Size 0 resources can be skipped. */
509 if (!resource->size) {
510 /* Set the base to limit so it doesn't confuse tolm. */
511 resource->base = resource->limit;
512 resource->flags |= IORESOURCE_ASSIGNED;
513 continue;
514 }
515
516 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000517 /*
518 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000519 * expansion card addresses. The legacy PCI decodes
520 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
521 * 0x00 - 0xff can be used out of each 0x400 block of
522 * I/O space.
523 */
524 if ((base & 0x300) != 0) {
525 base = (base & ~0x3ff) + 0x400;
526 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000527 /*
528 * Don't allow allocations in the VGA I/O range.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000529 * PCI has special cases for that.
530 */
531 else if ((base >= 0x3b0) && (base <= 0x3df)) {
532 base = 0x3e0;
533 }
534 }
535
536 if ((round(base, resource->align) + resource->size - 1) <=
537 resource->limit) {
538 /* Base must be aligned. */
539 base = round(base, resource->align);
540 resource->base = base;
Kyösti Mälkki134b6162015-03-23 19:58:23 +0200541 resource->limit = resource->base + resource->size - 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000542 resource->flags |= IORESOURCE_ASSIGNED;
543 resource->flags &= ~IORESOURCE_STORED;
544 base += resource->size;
545 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000546 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000547 printk(BIOS_ERR, " aligned base %llx size %llx "
548 "limit %llx\n", round(base, resource->align),
549 resource->size, resource->limit);
550 printk(BIOS_ERR, " %llx needs to be <= %llx "
551 "(limit)\n", (round(base, resource->align) +
Myles Watson29cc9ed2009-07-02 18:56:24 +0000552 resource->size) - 1, resource->limit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000553 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
554 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
555 ? "Assigned: " : "", dev_path(dev),
556 resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000557 resource->base + resource->size - 1,
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200558 resource2str(resource));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000559 }
560
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200561 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
562 dev_path(dev), resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000563 resource->size ? resource->base + resource->size - 1 :
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200564 resource->base, resource2str(resource));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000565 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000566
567 /*
568 * A PCI bridge resource does not need to be a power of two size, but
Myles Watson29cc9ed2009-07-02 18:56:24 +0000569 * it does have a minimum granularity. Round the size up to that
570 * minimum granularity so we know not to place something else at an
571 * address positively decoded by the bridge.
572 */
573
574 bridge->flags |= IORESOURCE_ASSIGNED;
575
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200576 printk(BIOS_SPEW, "%s %s: next_base: %llx size: %llx align: %d "
577 "gran: %d done\n", dev_path(bus->dev),
578 resource2str(bridge), base, bridge->size, bridge->align,
Uwe Hermanne4870472010-11-04 23:23:47 +0000579 bridge->gran);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000580
581 /* For each child which is a bridge, allocate_resources. */
582 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000583 struct resource *child_bridge;
584
Myles Watson894a3472010-06-09 22:41:35 +0000585 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000586 continue;
587
588 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000589 for (child_bridge = dev->resource_list; child_bridge;
590 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000591 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000592
593 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
594 (child_bridge->flags & type_mask) != type)
595 continue;
596
Uwe Hermanne4870472010-11-04 23:23:47 +0000597 /*
598 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000599 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000600 * and non-prefetchable memory. Bridges below them need
601 * it separated. Add the PREFETCH flag to the type_mask
602 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000603 */
Myles Watson894a3472010-06-09 22:41:35 +0000604 link = dev->link_list;
605 while (link && link->link_num !=
606 IOINDEX_LINK(child_bridge->index))
607 link = link->next;
608 if (link == NULL)
609 printk(BIOS_ERR, "link %ld not found on %s\n",
610 IOINDEX_LINK(child_bridge->index),
611 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000612
Myles Watson894a3472010-06-09 22:41:35 +0000613 allocate_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000614 type_mask | IORESOURCE_PREFETCH,
615 type | (child_bridge->flags &
616 IORESOURCE_PREFETCH));
617 }
618 }
619}
620
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200621static int resource_is(struct resource *res, u32 type)
622{
623 return (res->flags & IORESOURCE_TYPE_MASK) == type;
624}
Myles Watson29cc9ed2009-07-02 18:56:24 +0000625
626struct constraints {
Kyösti Mälkkifcbebb62015-03-17 06:42:54 +0200627 struct resource io, mem;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000628};
629
Elyes HAOUASe3480662018-05-06 20:32:23 +0200630static struct resource *resource_limit(struct constraints *limits,
631 struct resource *res)
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200632{
633 struct resource *lim = NULL;
634
635 /* MEM, or I/O - skip any others. */
636 if (resource_is(res, IORESOURCE_MEM))
637 lim = &limits->mem;
638 else if (resource_is(res, IORESOURCE_IO))
639 lim = &limits->io;
640
641 return lim;
642}
643
Elyes HAOUASe3480662018-05-06 20:32:23 +0200644static void constrain_resources(const struct device *dev,
645 struct constraints* limits)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000646{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200647 const struct device *child;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000648 struct resource *res;
649 struct resource *lim;
Myles Watson894a3472010-06-09 22:41:35 +0000650 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000651
Myles Watson29cc9ed2009-07-02 18:56:24 +0000652 /* Constrain limits based on the fixed resources of this device. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000653 for (res = dev->resource_list; res; res = res->next) {
Patrick Georgi18c585b2009-08-28 12:48:02 +0000654 if (!(res->flags & IORESOURCE_FIXED))
655 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000656 if (!res->size) {
657 /* It makes no sense to have 0-sized, fixed resources.*/
Uwe Hermanne4870472010-11-04 23:23:47 +0000658 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
659 "size=0!\n", dev_path(dev), res->index);
Patrick Georgi6bd93f42009-08-19 17:29:41 +0000660 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000661 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000662
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200663 lim = resource_limit(limits, res);
664 if (!lim)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000665 continue;
666
Uwe Hermanne4870472010-11-04 23:23:47 +0000667 /*
668 * Is it a fixed resource outside the current known region?
669 * If so, we don't have to consider it - it will be handled
670 * correctly and doesn't affect current region's limits.
671 */
672 if (((res->base + res->size -1) < lim->base)
673 || (res->base > lim->limit))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000674 continue;
675
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200676 printk(BIOS_SPEW, "%s: %s %02lx base %08llx limit %08llx %s (fixed)\n",
677 __func__, dev_path(dev), res->index, res->base,
678 res->base + res->size - 1, resource2str(res));
679
Uwe Hermanne4870472010-11-04 23:23:47 +0000680 /*
681 * Choose to be above or below fixed resources. This check is
682 * signed so that "negative" amounts of space are handled
683 * correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000684 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000685 if ((signed long long)(lim->limit - (res->base + res->size -1))
686 > (signed long long)(res->base - lim->base))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000687 lim->base = res->base + res->size;
688 else
689 lim->limit = res->base -1;
690 }
691
692 /* Descend into every enabled child and look for fixed resources. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000693 for (link = dev->link_list; link; link = link->next) {
694 for (child = link->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000695 if (child->enabled)
696 constrain_resources(child, limits);
Uwe Hermanne4870472010-11-04 23:23:47 +0000697 }
698 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000699}
700
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200701static void avoid_fixed_resources(const struct device *dev)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000702{
703 struct constraints limits;
704 struct resource *res;
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200705 struct resource *lim;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000706
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000707 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000708
Uwe Hermanne4870472010-11-04 23:23:47 +0000709 /* Initialize constraints to maximum size. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000710 limits.io.base = 0;
711 limits.io.limit = 0xffffffffffffffffULL;
712 limits.mem.base = 0;
713 limits.mem.limit = 0xffffffffffffffffULL;
714
715 /* Constrain the limits to dev's initial resources. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000716 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000717 if ((res->flags & IORESOURCE_FIXED))
718 continue;
Patrick Georgi51615092012-03-11 19:31:03 +0100719 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
Uwe Hermanne4870472010-11-04 23:23:47 +0000720 dev_path(dev), res->index, res->limit);
Kyösti Mälkkifcbebb62015-03-17 06:42:54 +0200721
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200722 lim = resource_limit(&limits, res);
723 if (!lim)
724 continue;
725
726 if (res->base > lim->base)
727 lim->base = res->base;
Elyes HAOUAS1943f372018-05-04 16:30:39 +0200728 if (res->limit < lim->limit)
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200729 lim->limit = res->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000730 }
731
732 /* Look through the tree for fixed resources and update the limits. */
733 constrain_resources(dev, &limits);
734
735 /* Update dev's resources with new limits. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000736 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000737 if ((res->flags & IORESOURCE_FIXED))
738 continue;
739
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200740 lim = resource_limit(&limits, res);
741 if (!lim)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000742 continue;
743
Myles Watson29cc9ed2009-07-02 18:56:24 +0000744 /* Is the resource outside the limits? */
745 if (lim->base > res->base)
746 res->base = lim->base;
747 if (res->limit > lim->limit)
748 res->limit = lim->limit;
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200749
Kyösti Mälkki634899c2015-03-23 14:22:22 +0200750 /* MEM resources need to start at the highest address manageable. */
751 if (res->flags & IORESOURCE_MEM)
752 res->base = resource_max(res);
753
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200754 printk(BIOS_SPEW, "%s:@%s %02lx base %08llx limit %08llx\n",
755 __func__, dev_path(dev), res->index, res->base, res->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000756 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000757}
arch import user (historical)dc811182005-07-06 17:16:09 +0000758
Elyes HAOUASe3480662018-05-06 20:32:23 +0200759struct device *vga_pri = NULL;
Myles Watsonc7233e02009-07-02 19:02:33 +0000760static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000761{
Uwe Hermann312673c2009-10-27 21:49:33 +0000762 /*
Martin Roth63373ed2013-07-08 16:24:19 -0600763 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000764 * This function knows too much about PCI stuff, it should be just
765 * an iterator/visitor.
766 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000767
Myles Watson29cc9ed2009-07-02 18:56:24 +0000768 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200769 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000770 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000771
Eric Biedermanb78c1972004-10-14 20:54:17 +0000772 bus = 0;
773 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000774 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000775
Patrick Georgi557ecf22012-07-20 12:16:17 +0200776 dev = NULL;
777 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000778 if (!dev->enabled)
779 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000780
Patrick Georgi557ecf22012-07-20 12:16:17 +0200781 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000782
Patrick Georgi557ecf22012-07-20 12:16:17 +0200783 if (dev->on_mainboard) {
784 vga_onboard = dev;
Kostr1f0d3792012-10-06 13:27:58 +0400785 } else {
Patrick Georgi557ecf22012-07-20 12:16:17 +0200786 vga = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000787 }
Myles Watson032a9652009-05-11 22:24:53 +0000788
Patrick Georgi557ecf22012-07-20 12:16:17 +0200789 /* It isn't safe to enable all VGA cards. */
790 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200791 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000792
Uwe Hermanne4870472010-11-04 23:23:47 +0000793 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000794 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200795
796 if (CONFIG_ONBOARD_VGA_IS_PRIMARY && vga_onboard)
797 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000798
Patrick Georgi5869fa22012-07-20 12:29:33 +0200799 /* If we prefer plugin VGA over chipset VGA, the chipset might
800 want to know. */
801 if (!CONFIG_ONBOARD_VGA_IS_PRIMARY && (vga != vga_onboard) &&
802 vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) {
803 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
804 vga_onboard->ops->disable(vga_onboard);
805 }
806
arch import user (historical)dc811182005-07-06 17:16:09 +0000807 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000808 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000809 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000810 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000811 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
812 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000813 bus = vga->bus;
814 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000815
Myles Watson29cc9ed2009-07-02 18:56:24 +0000816 /* Now walk up the bridges setting the VGA enable. */
817 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000818 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000819 dev_path(bus->dev));
Li-Ta Lodb7f47c2004-01-08 21:15:49 +0000820 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000821 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000822 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000823}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000824
Li-Ta Lo04930692004-11-25 17:37:19 +0000825/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000826 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000827 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000828 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000829 * resources to hardware. For bridge devices, the set_resources() method
830 * has to recurse into every down stream buses.
831 *
832 * Mutual recursion:
833 * assign_resources() -> device_operation::set_resources()
834 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000835 *
836 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000837 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000838void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000839{
840 struct device *curdev;
841
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000842 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000843 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000844
Myles Watson29cc9ed2009-07-02 18:56:24 +0000845 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000846 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000847 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000848
Eric Biedermane9a271e32003-09-02 03:36:25 +0000849 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000850 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000851 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000852 continue;
853 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700854 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000855 curdev->ops->set_resources(curdev);
856 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700857 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000858 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000859 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000860}
861
Li-Ta Lo5782d272004-04-26 17:51:20 +0000862/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000863 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000864 *
865 * Enable resources of the device by calling the device specific
866 * enable_resources() method.
867 *
868 * The parent's resources should be enabled first to avoid having enabling
869 * order problem. This is done by calling the parent's enable_resources()
Martin Roth63373ed2013-07-08 16:24:19 -0600870 * method before its children's enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000871 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000872 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000873 */
Myles Watson7eac4452010-06-17 16:16:56 +0000874static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000875{
Myles Watson7eac4452010-06-17 16:16:56 +0000876 struct device *dev;
877 struct bus *c_link;
878
879 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700880 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
881 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000882 dev->ops->enable_resources(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700883 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000884 }
Myles Watson7eac4452010-06-17 16:16:56 +0000885
886 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000887 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000888 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000889 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700890 post_log_clear();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000891}
892
Myles Watson032a9652009-05-11 22:24:53 +0000893/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000894 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
895 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000896 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000897 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000898 */
899int reset_bus(struct bus *bus)
900{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000901 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000902 bus->dev->ops->reset_bus(bus);
903 bus->reset_needed = 0;
904 return 1;
905 }
906 return 0;
907}
908
Myles Watson032a9652009-05-11 22:24:53 +0000909/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000910 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000911 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000912 * If there are bridges on the bus, recursively scan the buses behind the
913 * bridges. If the setting up and tuning of the bus causes a reset to be
914 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000915 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000916 * @param busdev Pointer to the bus device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000917 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200918static void scan_bus(struct device *busdev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000919{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000920 int do_scan_bus;
Paul Menzel662380f2015-10-20 10:21:08 +0200921 struct stopwatch sw;
922
923 stopwatch_init(&sw);
Uwe Hermanne4870472010-11-04 23:23:47 +0000924
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200925 if (!busdev->enabled)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200926 return;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200927
928 printk(BIOS_SPEW, "%s scanning...\n", dev_path(busdev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000929
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700930 post_log_path(busdev);
931
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000932 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000933 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000934 struct bus *link;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200935 busdev->ops->scan_bus(busdev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000936 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000937 for (link = busdev->link_list; link; link = link->next) {
938 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000939 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000940 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000941 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000942 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000943 }
944 }
945 }
Paul Menzel662380f2015-10-20 10:21:08 +0200946
947 printk(BIOS_DEBUG, "%s: scanning of bus %s took %ld usecs\n",
948 __func__, dev_path(busdev), stopwatch_duration_usecs(&sw));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000949}
950
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200951void scan_bridges(struct bus *bus)
952{
953 struct device *child;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200954
955 for (child = bus->children; child; child = child->sibling) {
956 if (!child->ops || !child->ops->scan_bus)
957 continue;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200958 scan_bus(child);
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200959 }
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200960}
961
Li-Ta Loe5266692004-03-23 21:28:05 +0000962/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000963 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000964 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000965 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000966 * file. The device structures for these devices are generated at compile
967 * time by the config tool and are organized into the device tree. This
968 * function determines if the devices created at compile time actually exist
969 * in the physical system.
970 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000971 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000972 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000973 * device tree.
974 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000975 * This function starts from the root device 'dev_root', scans the buses in
976 * the system recursively, and modifies the device tree according to the
977 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000978 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000979 * This function has no idea how to scan and probe buses and devices at all.
980 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000981 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000982 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000983 */
984void dev_enumerate(void)
985{
986 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000987
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000988 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000989
Eric Biederman8ca8d762003-04-22 19:02:15 +0000990 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000991
Uwe Hermanne4870472010-11-04 23:23:47 +0000992 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000993 printk(BIOS_SPEW, "Compare with tree...\n");
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300994 show_devs_tree(root, BIOS_SPEW, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000995
Stefan Reinauera675d492012-08-07 14:50:47 -0700996 if (root->chip_ops && root->chip_ops->enable_dev)
997 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000998
Eric Biedermanb78c1972004-10-14 20:54:17 +0000999 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001000 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +00001001 return;
1002 }
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001003 scan_bus(root);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001004 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001005 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001006}
1007
Li-Ta Loe5266692004-03-23 21:28:05 +00001008/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001009 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +00001010 *
Li-Ta Lo04930692004-11-25 17:37:19 +00001011 * Starting at the root of the device tree, travel it recursively in two
1012 * passes. In the first pass, we compute and allocate resources (ranges)
Martin Roth63373ed2013-07-08 16:24:19 -06001013 * required by each device. In the second pass, the resources ranges are
Li-Ta Lo04930692004-11-25 17:37:19 +00001014 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +00001015 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001016 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +00001017 *
1018 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +00001019 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001020 */
1021void dev_configure(void)
1022{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001023 struct resource *res;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +02001024 const struct device *root;
1025 const struct device *child;
Li-Ta Loe5266692004-03-23 21:28:05 +00001026
Marc Jones80bd74c2012-02-21 17:44:35 +01001027 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +01001028
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001029 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001030
Eric Biedermanb78c1972004-10-14 20:54:17 +00001031 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +00001032
Uwe Hermanne4870472010-11-04 23:23:47 +00001033 /*
1034 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +00001035 * space for IO, MEM, and PREFMEM resources in the domain. The
1036 * allocation of device resources will be done from this address space.
1037 */
Myles Watsoncd5d7562009-05-12 13:43:34 +00001038
Myles Watson29cc9ed2009-07-02 18:56:24 +00001039 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +00001040
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001041 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001042 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001043 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001044
Stefan Reinauer39e72292009-10-26 16:47:05 +00001045 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +00001046
Myles Watson29cc9ed2009-07-02 18:56:24 +00001047 /* Compute resources for all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001048 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001049 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001050 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001051 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001052 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001053 if (res->flags & IORESOURCE_FIXED)
1054 continue;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001055 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001056 compute_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001057 res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001058 continue;
1059 }
1060 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001061 compute_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001062 res, IORESOURCE_TYPE_MASK, IORESOURCE_IO);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001063 continue;
1064 }
1065 }
1066 }
1067
1068 /* For all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001069 for (child = root->link_list->children; child; child=child->sibling)
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001070 if (child->path.type == DEVICE_PATH_DOMAIN)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001071 avoid_fixed_resources(child);
1072
Eric Biedermanb78c1972004-10-14 20:54:17 +00001073 /* Store the computed resource allocations into device registers ... */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001074 printk(BIOS_INFO, "Setting resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001075 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001076 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001077 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001078 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001079 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001080 if (res->flags & IORESOURCE_FIXED)
1081 continue;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001082 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001083 allocate_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001084 res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001085 continue;
1086 }
1087 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001088 allocate_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001089 res, IORESOURCE_TYPE_MASK, IORESOURCE_IO);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001090 continue;
1091 }
1092 }
1093 }
Myles Watson894a3472010-06-09 22:41:35 +00001094 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001095 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001096 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +00001097
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001098 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001099}
1100
Li-Ta Loe5266692004-03-23 21:28:05 +00001101/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001102 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +00001103 *
1104 * Starting at the root, walk the tree and enable all devices/bridges by
1105 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001106 */
1107void dev_enable(void)
1108{
Myles Watson7eac4452010-06-17 16:16:56 +00001109 struct bus *link;
1110
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001111 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001112
Uwe Hermanne4870472010-11-04 23:23:47 +00001113 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001114 for (link = dev_root.link_list; link; link = link->next)
1115 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +00001116
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001117 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001118}
1119
Li-Ta Loe5266692004-03-23 21:28:05 +00001120/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001121 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +00001122 *
Uwe Hermanne4870472010-11-04 23:23:47 +00001123 * The parent should be initialized first to avoid having an ordering problem.
Martin Roth63373ed2013-07-08 16:24:19 -06001124 * This is done by calling the parent's init() method before its children's
Uwe Hermanne4870472010-11-04 23:23:47 +00001125 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +00001126 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001127 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +00001128 */
1129static void init_dev(struct device *dev)
1130{
Uwe Hermanne4870472010-11-04 23:23:47 +00001131 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +00001132 return;
Myles Watson7eac4452010-06-17 16:16:56 +00001133
1134 if (!dev->initialized && dev->ops && dev->ops->init) {
Martin Rothb3b114c2017-06-24 14:00:01 -06001135#if IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)
Aaron Durbin46ba4802014-09-23 16:34:40 -05001136 struct stopwatch sw;
1137 stopwatch_init(&sw);
Aaron Durbin05294292013-04-30 15:41:13 -05001138#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001139 if (dev->path.type == DEVICE_PATH_I2C) {
1140 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1141 dev_path(dev->bus->dev), dev->bus->link_num);
1142 }
1143
Paul Menzeldb232152015-06-07 20:28:17 +02001144 printk(BIOS_DEBUG, "%s init ...\n", dev_path(dev));
Myles Watson7eac4452010-06-17 16:16:56 +00001145 dev->initialized = 1;
1146 dev->ops->init(dev);
Martin Rothb3b114c2017-06-24 14:00:01 -06001147#if IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)
Paul Menzeldb232152015-06-07 20:28:17 +02001148 printk(BIOS_DEBUG, "%s init finished in %ld usecs\n", dev_path(dev),
Aaron Durbin46ba4802014-09-23 16:34:40 -05001149 stopwatch_duration_usecs(&sw));
Aaron Durbin05294292013-04-30 15:41:13 -05001150#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001151 }
1152}
1153
1154static void init_link(struct bus *link)
1155{
1156 struct device *dev;
1157 struct bus *c_link;
1158
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001159 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Lauriecb73a842013-06-10 10:41:04 -07001160 post_code(POST_BS_DEV_INIT);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001161 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +00001162 init_dev(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001163 }
Myles Watson7eac4452010-06-17 16:16:56 +00001164
1165 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001166 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +00001167 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +00001168 }
1169}
1170
1171/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001172 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +00001173 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001174 * Starting at the root device, call the device's init() method to do
1175 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001176 */
1177void dev_initialize(void)
1178{
Myles Watson7eac4452010-06-17 16:16:56 +00001179 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001180
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001181 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +00001182
Martin Rothb3b114c2017-06-24 14:00:01 -06001183#if IS_ENABLED(CONFIG_ARCH_X86)
Subrata Banikc7590cd2017-09-04 18:44:38 +05301184 /*
1185 * Initialize EBDA area in ramstage if early
1186 * initialization is not done.
1187 */
1188 if (!IS_ENABLED(CONFIG_EARLY_EBDA_INIT))
1189 /* Ensure EBDA is prepared before Option ROMs. */
1190 setup_default_ebda();
Duncan Laurieb4aaaa72012-01-17 09:03:11 -08001191#endif
1192
Myles Watson1bd3fb72010-08-16 16:25:23 +00001193 /* First call the mainboard init. */
1194 init_dev(&dev_root);
1195
Uwe Hermanne4870472010-11-04 23:23:47 +00001196 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001197 for (link = dev_root.link_list; link; link = link->next)
1198 init_link(link);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001199 post_log_clear();
Myles Watson7eac4452010-06-17 16:16:56 +00001200
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001201 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001202 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001203}
Marc Jones2a58ecd2013-10-29 17:32:00 -06001204
1205/**
1206 * Finalize a specific device.
1207 *
1208 * The parent should be finalized first to avoid having an ordering problem.
1209 * This is done by calling the parent's final() method before its childrens'
1210 * final() methods.
1211 *
1212 * @param dev The device to be initialized.
1213 */
1214static void final_dev(struct device *dev)
1215{
1216 if (!dev->enabled)
1217 return;
1218
1219 if (dev->ops && dev->ops->final) {
1220 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
1221 dev->ops->final(dev);
1222 }
1223}
1224
1225static void final_link(struct bus *link)
1226{
1227 struct device *dev;
1228 struct bus *c_link;
1229
1230 for (dev = link->children; dev; dev = dev->sibling)
1231 final_dev(dev);
1232
1233 for (dev = link->children; dev; dev = dev->sibling) {
1234 for (c_link = dev->link_list; c_link; c_link = c_link->next)
1235 final_link(c_link);
1236 }
1237}
1238/**
1239 * Finalize all devices in the global device tree.
1240 *
1241 * Starting at the root device, call the device's final() method to do
1242 * device-specific cleanup, then call each child's final() method.
1243 */
1244void dev_finalize(void)
1245{
1246 struct bus *link;
1247
1248 printk(BIOS_INFO, "Finalize devices...\n");
1249
1250 /* First call the mainboard finalize. */
1251 final_dev(&dev_root);
1252
1253 /* Now finalize everything. */
1254 for (link = dev_root.link_list; link; link = link->next)
1255 final_link(link);
1256
1257 printk(BIOS_INFO, "Devices finalized\n");
1258}