blob: 527298ce1646ff65960bf1a524693ec3b3768c50 [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 Biederman5899fd82003-04-24 06:25:08 +000042#include <device/device.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020043#include <device/pci_def.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000044#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000045#include <stdlib.h>
46#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000047#include <smp/spinlock.h>
Martin Rothb3b114c2017-06-24 14:00:01 -060048#if IS_ENABLED(CONFIG_ARCH_X86)
Duncan Laurieb4aaaa72012-01-17 09:03:11 -080049#include <arch/ebda.h>
50#endif
Aaron Durbin05294292013-04-30 15:41:13 -050051#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000052
Li-Ta Loe5266692004-03-23 21:28:05 +000053/** Pointer to the last device */
Myles Watson70679a02010-09-01 21:03:03 +000054extern struct device *last_dev;
Myles Watsonc25cc112010-05-21 14:33:48 +000055/** Linked list of free resources */
56struct resource *free_resources = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +000057
Nico Huberacd7d952012-07-25 10:33:05 +020058/**
59 * Initialize all chips of statically known devices.
60 *
61 * Will be called before bus enumeration to initialize chips stated in the
62 * device tree.
63 */
64void dev_initialize_chips(void)
65{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020066 const struct device *dev;
Nico Huberacd7d952012-07-25 10:33:05 +020067
68 for (dev = all_devices; dev; dev = dev->next) {
69 /* Initialize chip if we haven't yet. */
70 if (dev->chip_ops && dev->chip_ops->init &&
71 !dev->chip_ops->initialized) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -070072 post_log_path(dev);
Nico Huberacd7d952012-07-25 10:33:05 +020073 dev->chip_ops->init(dev->chip_info);
74 dev->chip_ops->initialized = 1;
75 }
76 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -070077 post_log_clear();
Nico Huberacd7d952012-07-25 10:33:05 +020078}
79
Marc Jones2a58ecd2013-10-29 17:32:00 -060080/**
81 * Finalize all chips of statically known devices.
82 *
83 * This is the last call before calling the payload. This is a good place
84 * to lock registers or other final cleanup.
85 */
86void dev_finalize_chips(void)
87{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020088 const struct device *dev;
Marc Jones2a58ecd2013-10-29 17:32:00 -060089
90 for (dev = all_devices; dev; dev = dev->next) {
91 /* Initialize chip if we haven't yet. */
92 if (dev->chip_ops && dev->chip_ops->final &&
93 !dev->chip_ops->finalized) {
94 dev->chip_ops->final(dev->chip_info);
95 dev->chip_ops->finalized = 1;
96 }
97 }
98}
99
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000100DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000101
Martin Rothb3b114c2017-06-24 14:00:01 -0600102#if IS_ENABLED(CONFIG_GFXUMA)
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300103/* IGD UMA memory */
104uint64_t uma_memory_base = 0;
105uint64_t uma_memory_size = 0;
Kyösti Mälkkib25374c2012-08-01 08:05:22 +0300106#endif
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300107
Li-Ta Loe5266692004-03-23 21:28:05 +0000108/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000109 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +0000110 *
Martin Roth63373ed2013-07-08 16:24:19 -0600111 * Allocate a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000112 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000113 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000114 * @param parent Parent bus the newly created device should be attached to.
115 * @param path Path to the device to be created.
116 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +0000117 *
118 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200120static struct device *__alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000121{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200122 struct device *dev, *child;
Li-Ta Loe5266692004-03-23 21:28:05 +0000123
Myles Watson29cc9ed2009-07-02 18:56:24 +0000124 /* Find the last child of our parent. */
Elyes HAOUASa342f392018-10-17 10:56:26 +0200125 for (child = parent->children; child && child->sibling; /* */)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000126 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +0000127
Eric Biedermane9a271e32003-09-02 03:36:25 +0000128 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000129 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +0000130 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000131
Eric Biedermane9a271e32003-09-02 03:36:25 +0000132 memset(dev, 0, sizeof(*dev));
133 memcpy(&dev->path, path, sizeof(*path));
134
Myles Watson29cc9ed2009-07-02 18:56:24 +0000135 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +0000136 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000137
Eric Biedermanb78c1972004-10-14 20:54:17 +0000138 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000139 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000140 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000141 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000142 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000143 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000144
Eric Biederman03acab62004-10-14 21:25:53 +0000145 /* Append a new device to the global device list.
146 * The list is used to find devices once everything is set up.
147 */
Myles Watson70679a02010-09-01 21:03:03 +0000148 last_dev->next = dev;
149 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000150
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300151 return dev;
152}
153
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200154struct device *alloc_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300155{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200156 struct device *dev;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300157 spin_lock(&dev_lock);
158 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000159 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000160 return dev;
161}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000162
Li-Ta Loe5266692004-03-23 21:28:05 +0000163/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300164 * See if a device structure already exists and if not allocate it.
165 *
166 * @param parent The bus to find the device on.
167 * @param path The relative path from the bus to the appropriate device.
168 * @return Pointer to a device structure for the device on bus at path.
169 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200170struct device *alloc_find_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300171{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200172 struct device *child;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300173 spin_lock(&dev_lock);
174 child = find_dev_path(parent, path);
175 if (!child)
176 child = __alloc_dev(parent, path);
177 spin_unlock(&dev_lock);
178 return child;
179}
180
181/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000182 * Round a number up to an alignment.
183 *
184 * @param val The starting value.
Martin Roth32bc6b62015-01-04 16:54:35 -0700185 * @param pow Alignment as a power of two.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000186 * @return Rounded up number.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000187 */
Eric Biederman448bd632004-10-14 22:52:15 +0000188static resource_t round(resource_t val, unsigned long pow)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000189{
Eric Biederman448bd632004-10-14 22:52:15 +0000190 resource_t mask;
191 mask = (1ULL << pow) - 1ULL;
192 val += mask;
193 val &= ~mask;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000194 return val;
195}
196
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +0200197static const char *resource2str(struct resource *res)
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200198{
199 if (res->flags & IORESOURCE_IO)
200 return "io";
201 if (res->flags & IORESOURCE_PREFETCH)
202 return "prefmem";
203 if (res->flags & IORESOURCE_MEM)
204 return "mem";
205 return "undefined";
206}
207
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000208/**
209 * Read the resources on all devices of a given bus.
210 *
211 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000212 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000213static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000214{
215 struct device *curdev;
216
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000217 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
218 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000219
Myles Watson29cc9ed2009-07-02 18:56:24 +0000220 /* Walk through all devices and find which resources they need. */
221 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000222 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000223
224 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000225 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000226
Eric Biedermane9a271e32003-09-02 03:36:25 +0000227 if (!curdev->ops || !curdev->ops->read_resources) {
Martin Roth7bc74ab2015-11-18 20:23:02 -0700228 if (curdev->path.type != DEVICE_PATH_APIC)
229 printk(BIOS_ERR, "%s missing read_resources\n",
230 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000231 continue;
232 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700233 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000234 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000235
236 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000237 for (link = curdev->link_list; link; link = link->next)
238 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000239 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700240 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000241 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000242 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000243}
244
Eric Biedermane9a271e32003-09-02 03:36:25 +0000245struct pick_largest_state {
246 struct resource *last;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200247 const struct device *result_dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000248 struct resource *result;
249 int seen_last;
250};
251
Myles Watson29cc9ed2009-07-02 18:56:24 +0000252static void pick_largest_resource(void *gp, struct device *dev,
253 struct resource *resource)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000254{
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000255 struct pick_largest_state *state = gp;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000256 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000257
Eric Biedermane9a271e32003-09-02 03:36:25 +0000258 last = state->last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000259
260 /* Be certain to pick the successor to last. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000261 if (resource == last) {
262 state->seen_last = 1;
263 return;
264 }
Myles Watson032a9652009-05-11 22:24:53 +0000265 if (resource->flags & IORESOURCE_FIXED)
Uwe Hermanne4870472010-11-04 23:23:47 +0000266 return; /* Skip it. */
Myles Watson032a9652009-05-11 22:24:53 +0000267 if (last && ((last->align < resource->align) ||
268 ((last->align == resource->align) &&
269 (last->size < resource->size)) ||
270 ((last->align == resource->align) &&
271 (last->size == resource->size) && (!state->seen_last)))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000272 return;
273 }
Myles Watson032a9652009-05-11 22:24:53 +0000274 if (!state->result ||
275 (state->result->align < resource->align) ||
276 ((state->result->align == resource->align) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000277 (state->result->size < resource->size))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000278 state->result_dev = dev;
279 state->result = resource;
Myles Watson032a9652009-05-11 22:24:53 +0000280 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000281}
282
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200283static const struct device *largest_resource(struct bus *bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000284 struct resource **result_res,
285 unsigned long type_mask,
286 unsigned long type)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000287{
288 struct pick_largest_state state;
289
290 state.last = *result_res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000291 state.result_dev = NULL;
292 state.result = NULL;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000293 state.seen_last = 0;
294
Myles Watson032a9652009-05-11 22:24:53 +0000295 search_bus_resources(bus, type_mask, type, pick_largest_resource,
296 &state);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000297
298 *result_res = state.result;
299 return state.result_dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000300}
301
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000302/**
Uwe Hermanne4870472010-11-04 23:23:47 +0000303 * This function is the guts of the resource allocator.
Myles Watson032a9652009-05-11 22:24:53 +0000304 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000305 * The problem.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000306 * - Allocate resource locations for every device.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000307 * - Don't overlap, and follow the rules of bridges.
308 * - Don't overlap with resources in fixed locations.
309 * - Be efficient so we don't have ugly strategies.
310 *
311 * The strategy.
312 * - Devices that have fixed addresses are the minority so don't
Myles Watson29cc9ed2009-07-02 18:56:24 +0000313 * worry about them too much. Instead only use part of the address
314 * space for devices with programmable addresses. This easily handles
Eric Biederman8ca8d762003-04-22 19:02:15 +0000315 * everything except bridges.
316 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000317 * - PCI devices are required to have their sizes and their alignments
318 * equal. In this case an optimal solution to the packing problem
319 * exists. Allocate all devices from highest alignment to least
320 * alignment or vice versa. Use this.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000321 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000322 * - So we can handle more than PCI run two allocation passes on bridges. The
323 * first to see how large the resources are behind the bridge, and what
324 * their alignment requirements are. The second to assign a safe address to
325 * the devices behind the bridge. This allows us to treat a bridge as just
326 * a device with a couple of resources, and not need to special case it in
327 * the allocator. Also this allows handling of other types of bridges.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000328 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000329 * @param bus The bus we are traversing.
330 * @param bridge The bridge resource which must contain the bus' resources.
331 * @param type_mask This value gets ANDed with the resource type.
332 * @param type This value must match the result of the AND.
333 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000334 */
Myles Watson54913b92009-10-13 20:00:09 +0000335static void compute_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000336 unsigned long type_mask, unsigned long type)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000337{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200338 const struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000339 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000340 resource_t base;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000341 base = round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000342
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200343 printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d"
344 " limit: %llx\n", dev_path(bus->dev), resource2str(bridge),
345 base, bridge->size, bridge->align,
Uwe Hermanne4870472010-11-04 23:23:47 +0000346 bridge->gran, bridge->limit);
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000347
Uwe Hermanne4870472010-11-04 23:23:47 +0000348 /* For each child which is a bridge, compute the resource needs. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000349 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000350 struct resource *child_bridge;
351
Myles Watson894a3472010-06-09 22:41:35 +0000352 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000353 continue;
354
355 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000356 for (child_bridge = dev->resource_list; child_bridge;
357 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000358 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000359
Uwe Hermanne4870472010-11-04 23:23:47 +0000360 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
361 || (child_bridge->flags & type_mask) != type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000362 continue;
363
Uwe Hermanne4870472010-11-04 23:23:47 +0000364 /*
365 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000366 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000367 * and non-prefetchable memory. Bridges below them need
368 * it separated. Add the PREFETCH flag to the type_mask
369 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000370 */
Myles Watson894a3472010-06-09 22:41:35 +0000371 link = dev->link_list;
372 while (link && link->link_num !=
373 IOINDEX_LINK(child_bridge->index))
374 link = link->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000375
376 if (link == NULL) {
Myles Watson894a3472010-06-09 22:41:35 +0000377 printk(BIOS_ERR, "link %ld not found on %s\n",
378 IOINDEX_LINK(child_bridge->index),
379 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000380 }
381
Myles Watson894a3472010-06-09 22:41:35 +0000382 compute_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000383 type_mask | IORESOURCE_PREFETCH,
384 type | (child_bridge->flags &
385 IORESOURCE_PREFETCH));
386 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000387 }
388
Myles Watson29cc9ed2009-07-02 18:56:24 +0000389 /* Remember we haven't found anything yet. */
390 resource = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000391
Uwe Hermanne4870472010-11-04 23:23:47 +0000392 /*
393 * Walk through all the resources on the current bus and compute the
394 * amount of address space taken by them. Take granularity and
Myles Watson29cc9ed2009-07-02 18:56:24 +0000395 * alignment into account.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000396 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000397 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000398
Myles Watson29cc9ed2009-07-02 18:56:24 +0000399 /* Size 0 resources can be skipped. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000400 if (!resource->size)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000401 continue;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000402
Myles Watson29cc9ed2009-07-02 18:56:24 +0000403 /* Propagate the resource alignment to the bridge resource. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000404 if (resource->align > bridge->align)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000405 bridge->align = resource->align;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000406
407 /* Propagate the resource limit to the bridge register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000408 if (bridge->limit > resource->limit)
Eric Biedermandbec2d42004-10-21 10:44:08 +0000409 bridge->limit = resource->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000410
411 /* Warn if it looks like APICs aren't declared. */
412 if ((resource->limit == 0xffffffff) &&
413 (resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000414 printk(BIOS_ERR,
415 "Resource limit looks wrong! (no APIC?)\n");
Patrick Georgi51615092012-03-11 19:31:03 +0100416 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000417 dev_path(dev), resource->index, resource->limit);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000418 }
Stefan Reinauer51754a32008-08-01 12:28:38 +0000419
Eric Biederman8ca8d762003-04-22 19:02:15 +0000420 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000421 /*
422 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000423 * expansion card addresses. The legacy PCI decodes
424 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
425 * 0x00 - 0xff can be used out of each 0x400 block of
426 * I/O space.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000427 */
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000428 if ((base & 0x300) != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000429 base = (base & ~0x3ff) + 0x400;
430 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000431 /*
432 * Don't allow allocations in the VGA I/O range.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000433 * PCI has special cases for that.
434 */
435 else if ((base >= 0x3b0) && (base <= 0x3df)) {
436 base = 0x3e0;
437 }
438 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000439 /* Base must be aligned. */
440 base = round(base, resource->align);
441 resource->base = base;
442 base += resource->size;
Myles Watson032a9652009-05-11 22:24:53 +0000443
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000444 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000445 dev_path(dev), resource->index, resource->base,
446 resource->base + resource->size - 1,
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200447 resource2str(resource));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000448 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000449
450 /*
451 * A PCI bridge resource does not need to be a power of two size, but
452 * it does have a minimum granularity. Round the size up to that
453 * minimum granularity so we know not to place something else at an
Martin Roth63373ed2013-07-08 16:24:19 -0600454 * address positively decoded by the bridge.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000455 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000456 bridge->size = round(base, bridge->gran) -
457 round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000458
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200459 printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d"
460 " limit: %llx done\n", dev_path(bus->dev),
461 resource2str(bridge),
Uwe Hermanne4870472010-11-04 23:23:47 +0000462 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000463}
464
465/**
466 * This function is the second part of the resource allocator.
467 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000468 * See the compute_resources function for a more detailed explanation.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000469 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000470 * This function assigns the resources a value.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000471 *
472 * @param bus The bus we are traversing.
473 * @param bridge The bridge resource which must contain the bus' resources.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000474 * @param type_mask This value gets ANDed with the resource type.
475 * @param type This value must match the result of the AND.
Uwe Hermanne4870472010-11-04 23:23:47 +0000476 *
477 * @see compute_resources
Myles Watson29cc9ed2009-07-02 18:56:24 +0000478 */
Myles Watson54913b92009-10-13 20:00:09 +0000479static void allocate_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000480 unsigned long type_mask, unsigned long type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000481{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200482 const struct device *dev;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000483 struct resource *resource;
484 resource_t base;
485 base = bridge->base;
486
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200487 printk(BIOS_SPEW, "%s %s: base:%llx size:%llx align:%d gran:%d "
488 "limit:%llx\n", dev_path(bus->dev),
489 resource2str(bridge),
Myles Watson29cc9ed2009-07-02 18:56:24 +0000490 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
491
492 /* Remember we haven't found anything yet. */
493 resource = NULL;
494
Uwe Hermanne4870472010-11-04 23:23:47 +0000495 /*
496 * Walk through all the resources on the current bus and allocate them
Myles Watson29cc9ed2009-07-02 18:56:24 +0000497 * address space.
498 */
499 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
500
501 /* Propagate the bridge limit to the resource register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000502 if (resource->limit > bridge->limit)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000503 resource->limit = bridge->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000504
505 /* Size 0 resources can be skipped. */
506 if (!resource->size) {
507 /* Set the base to limit so it doesn't confuse tolm. */
508 resource->base = resource->limit;
509 resource->flags |= IORESOURCE_ASSIGNED;
510 continue;
511 }
512
513 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000514 /*
515 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000516 * expansion card addresses. The legacy PCI decodes
517 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
518 * 0x00 - 0xff can be used out of each 0x400 block of
519 * I/O space.
520 */
521 if ((base & 0x300) != 0) {
522 base = (base & ~0x3ff) + 0x400;
523 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000524 /*
525 * Don't allow allocations in the VGA I/O range.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000526 * PCI has special cases for that.
527 */
528 else if ((base >= 0x3b0) && (base <= 0x3df)) {
529 base = 0x3e0;
530 }
531 }
532
533 if ((round(base, resource->align) + resource->size - 1) <=
534 resource->limit) {
535 /* Base must be aligned. */
536 base = round(base, resource->align);
537 resource->base = base;
Kyösti Mälkki134b6162015-03-23 19:58:23 +0200538 resource->limit = resource->base + resource->size - 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000539 resource->flags |= IORESOURCE_ASSIGNED;
540 resource->flags &= ~IORESOURCE_STORED;
541 base += resource->size;
542 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000543 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000544 printk(BIOS_ERR, " aligned base %llx size %llx "
545 "limit %llx\n", round(base, resource->align),
546 resource->size, resource->limit);
547 printk(BIOS_ERR, " %llx needs to be <= %llx "
548 "(limit)\n", (round(base, resource->align) +
Myles Watson29cc9ed2009-07-02 18:56:24 +0000549 resource->size) - 1, resource->limit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000550 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
551 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
552 ? "Assigned: " : "", dev_path(dev),
553 resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000554 resource->base + resource->size - 1,
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200555 resource2str(resource));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000556 }
557
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200558 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
559 dev_path(dev), resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000560 resource->size ? resource->base + resource->size - 1 :
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200561 resource->base, resource2str(resource));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000562 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000563
564 /*
565 * A PCI bridge resource does not need to be a power of two size, but
Myles Watson29cc9ed2009-07-02 18:56:24 +0000566 * it does have a minimum granularity. Round the size up to that
567 * minimum granularity so we know not to place something else at an
568 * address positively decoded by the bridge.
569 */
570
571 bridge->flags |= IORESOURCE_ASSIGNED;
572
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200573 printk(BIOS_SPEW, "%s %s: next_base: %llx size: %llx align: %d "
574 "gran: %d done\n", dev_path(bus->dev),
575 resource2str(bridge), base, bridge->size, bridge->align,
Uwe Hermanne4870472010-11-04 23:23:47 +0000576 bridge->gran);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000577
578 /* For each child which is a bridge, allocate_resources. */
579 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000580 struct resource *child_bridge;
581
Myles Watson894a3472010-06-09 22:41:35 +0000582 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000583 continue;
584
585 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000586 for (child_bridge = dev->resource_list; child_bridge;
587 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000588 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000589
590 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
591 (child_bridge->flags & type_mask) != type)
592 continue;
593
Uwe Hermanne4870472010-11-04 23:23:47 +0000594 /*
595 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000596 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000597 * and non-prefetchable memory. Bridges below them need
598 * it separated. Add the PREFETCH flag to the type_mask
599 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000600 */
Myles Watson894a3472010-06-09 22:41:35 +0000601 link = dev->link_list;
602 while (link && link->link_num !=
603 IOINDEX_LINK(child_bridge->index))
604 link = link->next;
605 if (link == NULL)
606 printk(BIOS_ERR, "link %ld not found on %s\n",
607 IOINDEX_LINK(child_bridge->index),
608 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000609
Myles Watson894a3472010-06-09 22:41:35 +0000610 allocate_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000611 type_mask | IORESOURCE_PREFETCH,
612 type | (child_bridge->flags &
613 IORESOURCE_PREFETCH));
614 }
615 }
616}
617
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200618static int resource_is(struct resource *res, u32 type)
619{
620 return (res->flags & IORESOURCE_TYPE_MASK) == type;
621}
Myles Watson29cc9ed2009-07-02 18:56:24 +0000622
623struct constraints {
Kyösti Mälkkifcbebb62015-03-17 06:42:54 +0200624 struct resource io, mem;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000625};
626
Elyes HAOUASe3480662018-05-06 20:32:23 +0200627static struct resource *resource_limit(struct constraints *limits,
628 struct resource *res)
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200629{
630 struct resource *lim = NULL;
631
632 /* MEM, or I/O - skip any others. */
633 if (resource_is(res, IORESOURCE_MEM))
634 lim = &limits->mem;
635 else if (resource_is(res, IORESOURCE_IO))
636 lim = &limits->io;
637
638 return lim;
639}
640
Elyes HAOUASe3480662018-05-06 20:32:23 +0200641static void constrain_resources(const struct device *dev,
642 struct constraints* limits)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000643{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200644 const struct device *child;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000645 struct resource *res;
646 struct resource *lim;
Myles Watson894a3472010-06-09 22:41:35 +0000647 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000648
Myles Watson29cc9ed2009-07-02 18:56:24 +0000649 /* Constrain limits based on the fixed resources of this device. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000650 for (res = dev->resource_list; res; res = res->next) {
Patrick Georgi18c585b2009-08-28 12:48:02 +0000651 if (!(res->flags & IORESOURCE_FIXED))
652 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000653 if (!res->size) {
654 /* It makes no sense to have 0-sized, fixed resources.*/
Uwe Hermanne4870472010-11-04 23:23:47 +0000655 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
656 "size=0!\n", dev_path(dev), res->index);
Patrick Georgi6bd93f42009-08-19 17:29:41 +0000657 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000658 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000659
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200660 lim = resource_limit(limits, res);
661 if (!lim)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000662 continue;
663
Uwe Hermanne4870472010-11-04 23:23:47 +0000664 /*
665 * Is it a fixed resource outside the current known region?
666 * If so, we don't have to consider it - it will be handled
667 * correctly and doesn't affect current region's limits.
668 */
669 if (((res->base + res->size -1) < lim->base)
670 || (res->base > lim->limit))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000671 continue;
672
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200673 printk(BIOS_SPEW, "%s: %s %02lx base %08llx limit %08llx %s (fixed)\n",
674 __func__, dev_path(dev), res->index, res->base,
675 res->base + res->size - 1, resource2str(res));
676
Uwe Hermanne4870472010-11-04 23:23:47 +0000677 /*
678 * Choose to be above or below fixed resources. This check is
679 * signed so that "negative" amounts of space are handled
680 * correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000681 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000682 if ((signed long long)(lim->limit - (res->base + res->size -1))
683 > (signed long long)(res->base - lim->base))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000684 lim->base = res->base + res->size;
685 else
686 lim->limit = res->base -1;
687 }
688
689 /* Descend into every enabled child and look for fixed resources. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000690 for (link = dev->link_list; link; link = link->next) {
691 for (child = link->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000692 if (child->enabled)
693 constrain_resources(child, limits);
Uwe Hermanne4870472010-11-04 23:23:47 +0000694 }
695 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000696}
697
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200698static void avoid_fixed_resources(const struct device *dev)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000699{
700 struct constraints limits;
701 struct resource *res;
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200702 struct resource *lim;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000703
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000704 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000705
Uwe Hermanne4870472010-11-04 23:23:47 +0000706 /* Initialize constraints to maximum size. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000707 limits.io.base = 0;
708 limits.io.limit = 0xffffffffffffffffULL;
709 limits.mem.base = 0;
710 limits.mem.limit = 0xffffffffffffffffULL;
711
712 /* Constrain the limits to dev's initial resources. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000713 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000714 if ((res->flags & IORESOURCE_FIXED))
715 continue;
Patrick Georgi51615092012-03-11 19:31:03 +0100716 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
Uwe Hermanne4870472010-11-04 23:23:47 +0000717 dev_path(dev), res->index, res->limit);
Kyösti Mälkkifcbebb62015-03-17 06:42:54 +0200718
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200719 lim = resource_limit(&limits, res);
720 if (!lim)
721 continue;
722
723 if (res->base > lim->base)
724 lim->base = res->base;
Elyes HAOUAS1943f372018-05-04 16:30:39 +0200725 if (res->limit < lim->limit)
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200726 lim->limit = res->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000727 }
728
729 /* Look through the tree for fixed resources and update the limits. */
730 constrain_resources(dev, &limits);
731
732 /* Update dev's resources with new limits. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000733 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000734 if ((res->flags & IORESOURCE_FIXED))
735 continue;
736
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200737 lim = resource_limit(&limits, res);
738 if (!lim)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000739 continue;
740
Myles Watson29cc9ed2009-07-02 18:56:24 +0000741 /* Is the resource outside the limits? */
742 if (lim->base > res->base)
743 res->base = lim->base;
744 if (res->limit > lim->limit)
745 res->limit = lim->limit;
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200746
Kyösti Mälkki634899c2015-03-23 14:22:22 +0200747 /* MEM resources need to start at the highest address manageable. */
748 if (res->flags & IORESOURCE_MEM)
749 res->base = resource_max(res);
750
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200751 printk(BIOS_SPEW, "%s:@%s %02lx base %08llx limit %08llx\n",
752 __func__, dev_path(dev), res->index, res->base, res->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000753 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000754}
arch import user (historical)dc811182005-07-06 17:16:09 +0000755
Elyes HAOUASe3480662018-05-06 20:32:23 +0200756struct device *vga_pri = NULL;
Myles Watsonc7233e02009-07-02 19:02:33 +0000757static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000758{
Uwe Hermann312673c2009-10-27 21:49:33 +0000759 /*
Martin Roth63373ed2013-07-08 16:24:19 -0600760 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000761 * This function knows too much about PCI stuff, it should be just
762 * an iterator/visitor.
763 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000764
Myles Watson29cc9ed2009-07-02 18:56:24 +0000765 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200766 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000767 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000768
Eric Biedermanb78c1972004-10-14 20:54:17 +0000769 bus = 0;
770 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000771 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000772
Patrick Georgi557ecf22012-07-20 12:16:17 +0200773 dev = NULL;
774 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000775 if (!dev->enabled)
776 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000777
Patrick Georgi557ecf22012-07-20 12:16:17 +0200778 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000779
Patrick Georgi557ecf22012-07-20 12:16:17 +0200780 if (dev->on_mainboard) {
781 vga_onboard = dev;
Kostr1f0d3792012-10-06 13:27:58 +0400782 } else {
Patrick Georgi557ecf22012-07-20 12:16:17 +0200783 vga = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000784 }
Myles Watson032a9652009-05-11 22:24:53 +0000785
Patrick Georgi557ecf22012-07-20 12:16:17 +0200786 /* It isn't safe to enable all VGA cards. */
787 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200788 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000789
Uwe Hermanne4870472010-11-04 23:23:47 +0000790 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000791 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200792
793 if (CONFIG_ONBOARD_VGA_IS_PRIMARY && vga_onboard)
794 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000795
Patrick Georgi5869fa22012-07-20 12:29:33 +0200796 /* If we prefer plugin VGA over chipset VGA, the chipset might
797 want to know. */
798 if (!CONFIG_ONBOARD_VGA_IS_PRIMARY && (vga != vga_onboard) &&
799 vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) {
800 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
801 vga_onboard->ops->disable(vga_onboard);
802 }
803
arch import user (historical)dc811182005-07-06 17:16:09 +0000804 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000805 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000806 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000807 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000808 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
809 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000810 bus = vga->bus;
811 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000812
Myles Watson29cc9ed2009-07-02 18:56:24 +0000813 /* Now walk up the bridges setting the VGA enable. */
814 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000815 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000816 dev_path(bus->dev));
Li-Ta Lodb7f47c2004-01-08 21:15:49 +0000817 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000818 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000819 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000820}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000821
Li-Ta Lo04930692004-11-25 17:37:19 +0000822/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000823 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000824 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000825 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000826 * resources to hardware. For bridge devices, the set_resources() method
827 * has to recurse into every down stream buses.
828 *
829 * Mutual recursion:
830 * assign_resources() -> device_operation::set_resources()
831 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000832 *
833 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000834 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000835void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000836{
837 struct device *curdev;
838
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000839 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000840 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000841
Myles Watson29cc9ed2009-07-02 18:56:24 +0000842 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000843 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000844 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000845
Eric Biedermane9a271e32003-09-02 03:36:25 +0000846 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000847 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000848 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000849 continue;
850 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700851 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000852 curdev->ops->set_resources(curdev);
853 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700854 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000855 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000856 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000857}
858
Li-Ta Lo5782d272004-04-26 17:51:20 +0000859/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000860 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000861 *
862 * Enable resources of the device by calling the device specific
863 * enable_resources() method.
864 *
865 * The parent's resources should be enabled first to avoid having enabling
866 * order problem. This is done by calling the parent's enable_resources()
Martin Roth63373ed2013-07-08 16:24:19 -0600867 * method before its children's enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000868 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000869 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000870 */
Myles Watson7eac4452010-06-17 16:16:56 +0000871static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000872{
Myles Watson7eac4452010-06-17 16:16:56 +0000873 struct device *dev;
874 struct bus *c_link;
875
876 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700877 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
878 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000879 dev->ops->enable_resources(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700880 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000881 }
Myles Watson7eac4452010-06-17 16:16:56 +0000882
883 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000884 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000885 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000886 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700887 post_log_clear();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000888}
889
Myles Watson032a9652009-05-11 22:24:53 +0000890/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000891 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
892 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000893 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000894 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000895 */
896int reset_bus(struct bus *bus)
897{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000898 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000899 bus->dev->ops->reset_bus(bus);
900 bus->reset_needed = 0;
901 return 1;
902 }
903 return 0;
904}
905
Myles Watson032a9652009-05-11 22:24:53 +0000906/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000907 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000908 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000909 * If there are bridges on the bus, recursively scan the buses behind the
910 * bridges. If the setting up and tuning of the bus causes a reset to be
911 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000912 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000913 * @param busdev Pointer to the bus device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000914 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200915static void scan_bus(struct device *busdev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000916{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000917 int do_scan_bus;
Paul Menzel662380f2015-10-20 10:21:08 +0200918 struct stopwatch sw;
919
920 stopwatch_init(&sw);
Uwe Hermanne4870472010-11-04 23:23:47 +0000921
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200922 if (!busdev->enabled)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200923 return;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200924
925 printk(BIOS_SPEW, "%s scanning...\n", dev_path(busdev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000926
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700927 post_log_path(busdev);
928
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000929 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000930 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000931 struct bus *link;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200932 busdev->ops->scan_bus(busdev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000933 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000934 for (link = busdev->link_list; link; link = link->next) {
935 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000936 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000937 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000938 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000939 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000940 }
941 }
942 }
Paul Menzel662380f2015-10-20 10:21:08 +0200943
944 printk(BIOS_DEBUG, "%s: scanning of bus %s took %ld usecs\n",
945 __func__, dev_path(busdev), stopwatch_duration_usecs(&sw));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000946}
947
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200948void scan_bridges(struct bus *bus)
949{
950 struct device *child;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200951
952 for (child = bus->children; child; child = child->sibling) {
953 if (!child->ops || !child->ops->scan_bus)
954 continue;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200955 scan_bus(child);
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200956 }
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200957}
958
Li-Ta Loe5266692004-03-23 21:28:05 +0000959/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000960 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000961 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000962 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000963 * file. The device structures for these devices are generated at compile
964 * time by the config tool and are organized into the device tree. This
965 * function determines if the devices created at compile time actually exist
966 * in the physical system.
967 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000968 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000969 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000970 * device tree.
971 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000972 * This function starts from the root device 'dev_root', scans the buses in
973 * the system recursively, and modifies the device tree according to the
974 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000975 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000976 * This function has no idea how to scan and probe buses and devices at all.
977 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000978 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000979 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000980 */
981void dev_enumerate(void)
982{
983 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000984
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000985 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000986
Eric Biederman8ca8d762003-04-22 19:02:15 +0000987 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000988
Uwe Hermanne4870472010-11-04 23:23:47 +0000989 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000990 printk(BIOS_SPEW, "Compare with tree...\n");
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300991 show_devs_tree(root, BIOS_SPEW, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000992
Stefan Reinauera675d492012-08-07 14:50:47 -0700993 if (root->chip_ops && root->chip_ops->enable_dev)
994 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000995
Eric Biedermanb78c1972004-10-14 20:54:17 +0000996 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000997 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000998 return;
999 }
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001000 scan_bus(root);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001001 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001002 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001003}
1004
Li-Ta Loe5266692004-03-23 21:28:05 +00001005/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001006 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +00001007 *
Li-Ta Lo04930692004-11-25 17:37:19 +00001008 * Starting at the root of the device tree, travel it recursively in two
1009 * passes. In the first pass, we compute and allocate resources (ranges)
Martin Roth63373ed2013-07-08 16:24:19 -06001010 * required by each device. In the second pass, the resources ranges are
Li-Ta Lo04930692004-11-25 17:37:19 +00001011 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +00001012 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001013 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +00001014 *
1015 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +00001016 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001017 */
1018void dev_configure(void)
1019{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001020 struct resource *res;
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +02001021 const struct device *root;
1022 const struct device *child;
Li-Ta Loe5266692004-03-23 21:28:05 +00001023
Marc Jones80bd74c2012-02-21 17:44:35 +01001024 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +01001025
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001026 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001027
Eric Biedermanb78c1972004-10-14 20:54:17 +00001028 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +00001029
Uwe Hermanne4870472010-11-04 23:23:47 +00001030 /*
1031 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +00001032 * space for IO, MEM, and PREFMEM resources in the domain. The
1033 * allocation of device resources will be done from this address space.
1034 */
Myles Watsoncd5d7562009-05-12 13:43:34 +00001035
Myles Watson29cc9ed2009-07-02 18:56:24 +00001036 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +00001037
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001038 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001039 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001040 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001041
Stefan Reinauer39e72292009-10-26 16:47:05 +00001042 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +00001043
Myles Watson29cc9ed2009-07-02 18:56:24 +00001044 /* Compute resources for all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001045 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001046 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001047 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001048 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001049 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001050 if (res->flags & IORESOURCE_FIXED)
1051 continue;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001052 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001053 compute_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001054 res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001055 continue;
1056 }
1057 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001058 compute_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001059 res, IORESOURCE_TYPE_MASK, IORESOURCE_IO);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001060 continue;
1061 }
1062 }
1063 }
1064
1065 /* For all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001066 for (child = root->link_list->children; child; child=child->sibling)
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001067 if (child->path.type == DEVICE_PATH_DOMAIN)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001068 avoid_fixed_resources(child);
1069
Eric Biedermanb78c1972004-10-14 20:54:17 +00001070 /* Store the computed resource allocations into device registers ... */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001071 printk(BIOS_INFO, "Setting resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001072 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001073 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001074 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001075 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001076 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001077 if (res->flags & IORESOURCE_FIXED)
1078 continue;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001079 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001080 allocate_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001081 res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001082 continue;
1083 }
1084 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001085 allocate_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001086 res, IORESOURCE_TYPE_MASK, IORESOURCE_IO);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001087 continue;
1088 }
1089 }
1090 }
Myles Watson894a3472010-06-09 22:41:35 +00001091 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001092 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001093 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +00001094
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001095 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001096}
1097
Li-Ta Loe5266692004-03-23 21:28:05 +00001098/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001099 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +00001100 *
1101 * Starting at the root, walk the tree and enable all devices/bridges by
1102 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001103 */
1104void dev_enable(void)
1105{
Myles Watson7eac4452010-06-17 16:16:56 +00001106 struct bus *link;
1107
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001108 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001109
Uwe Hermanne4870472010-11-04 23:23:47 +00001110 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001111 for (link = dev_root.link_list; link; link = link->next)
1112 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +00001113
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001114 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001115}
1116
Li-Ta Loe5266692004-03-23 21:28:05 +00001117/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001118 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +00001119 *
Uwe Hermanne4870472010-11-04 23:23:47 +00001120 * The parent should be initialized first to avoid having an ordering problem.
Martin Roth63373ed2013-07-08 16:24:19 -06001121 * This is done by calling the parent's init() method before its children's
Uwe Hermanne4870472010-11-04 23:23:47 +00001122 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +00001123 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001124 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +00001125 */
1126static void init_dev(struct device *dev)
1127{
Uwe Hermanne4870472010-11-04 23:23:47 +00001128 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +00001129 return;
Myles Watson7eac4452010-06-17 16:16:56 +00001130
1131 if (!dev->initialized && dev->ops && dev->ops->init) {
Martin Rothb3b114c2017-06-24 14:00:01 -06001132#if IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)
Aaron Durbin46ba4802014-09-23 16:34:40 -05001133 struct stopwatch sw;
1134 stopwatch_init(&sw);
Aaron Durbin05294292013-04-30 15:41:13 -05001135#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001136 if (dev->path.type == DEVICE_PATH_I2C) {
1137 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1138 dev_path(dev->bus->dev), dev->bus->link_num);
1139 }
1140
Paul Menzeldb232152015-06-07 20:28:17 +02001141 printk(BIOS_DEBUG, "%s init ...\n", dev_path(dev));
Myles Watson7eac4452010-06-17 16:16:56 +00001142 dev->initialized = 1;
1143 dev->ops->init(dev);
Martin Rothb3b114c2017-06-24 14:00:01 -06001144#if IS_ENABLED(CONFIG_HAVE_MONOTONIC_TIMER)
Paul Menzeldb232152015-06-07 20:28:17 +02001145 printk(BIOS_DEBUG, "%s init finished in %ld usecs\n", dev_path(dev),
Aaron Durbin46ba4802014-09-23 16:34:40 -05001146 stopwatch_duration_usecs(&sw));
Aaron Durbin05294292013-04-30 15:41:13 -05001147#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001148 }
1149}
1150
1151static void init_link(struct bus *link)
1152{
1153 struct device *dev;
1154 struct bus *c_link;
1155
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001156 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Lauriecb73a842013-06-10 10:41:04 -07001157 post_code(POST_BS_DEV_INIT);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001158 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +00001159 init_dev(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001160 }
Myles Watson7eac4452010-06-17 16:16:56 +00001161
1162 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001163 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +00001164 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +00001165 }
1166}
1167
1168/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001169 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +00001170 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001171 * Starting at the root device, call the device's init() method to do
1172 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001173 */
1174void dev_initialize(void)
1175{
Myles Watson7eac4452010-06-17 16:16:56 +00001176 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001177
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001178 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +00001179
Martin Rothb3b114c2017-06-24 14:00:01 -06001180#if IS_ENABLED(CONFIG_ARCH_X86)
Subrata Banikc7590cd2017-09-04 18:44:38 +05301181 /*
1182 * Initialize EBDA area in ramstage if early
1183 * initialization is not done.
1184 */
1185 if (!IS_ENABLED(CONFIG_EARLY_EBDA_INIT))
1186 /* Ensure EBDA is prepared before Option ROMs. */
1187 setup_default_ebda();
Duncan Laurieb4aaaa72012-01-17 09:03:11 -08001188#endif
1189
Myles Watson1bd3fb72010-08-16 16:25:23 +00001190 /* First call the mainboard init. */
1191 init_dev(&dev_root);
1192
Uwe Hermanne4870472010-11-04 23:23:47 +00001193 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001194 for (link = dev_root.link_list; link; link = link->next)
1195 init_link(link);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001196 post_log_clear();
Myles Watson7eac4452010-06-17 16:16:56 +00001197
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001198 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001199 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001200}
Marc Jones2a58ecd2013-10-29 17:32:00 -06001201
1202/**
1203 * Finalize a specific device.
1204 *
1205 * The parent should be finalized first to avoid having an ordering problem.
1206 * This is done by calling the parent's final() method before its childrens'
1207 * final() methods.
1208 *
1209 * @param dev The device to be initialized.
1210 */
1211static void final_dev(struct device *dev)
1212{
1213 if (!dev->enabled)
1214 return;
1215
1216 if (dev->ops && dev->ops->final) {
1217 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
1218 dev->ops->final(dev);
1219 }
1220}
1221
1222static void final_link(struct bus *link)
1223{
1224 struct device *dev;
1225 struct bus *c_link;
1226
1227 for (dev = link->children; dev; dev = dev->sibling)
1228 final_dev(dev);
1229
1230 for (dev = link->children; dev; dev = dev->sibling) {
1231 for (c_link = dev->link_list; c_link; c_link = c_link->next)
1232 final_link(c_link);
1233 }
1234}
1235/**
1236 * Finalize all devices in the global device tree.
1237 *
1238 * Starting at the root device, call the device's final() method to do
1239 * device-specific cleanup, then call each child's final() method.
1240 */
1241void dev_finalize(void)
1242{
1243 struct bus *link;
1244
1245 printk(BIOS_INFO, "Finalize devices...\n");
1246
1247 /* First call the mainboard finalize. */
1248 final_dev(&dev_root);
1249
1250 /* Now finalize everything. */
1251 for (link = dev_root.link_list; link; link = link->next)
1252 final_link(link);
1253
1254 printk(BIOS_INFO, "Devices finalized\n");
1255}