blob: 00e323a66821dab30603ed629035c8b0f870ef31 [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>
Uwe Hermannb80dbf02007-04-22 19:08:13 +000016 */
17
18/*
Eric Biederman8ca8d762003-04-22 19:02:15 +000019 * (c) 1999--2000 Martin Mares <mj@suse.cz>
Eric Biederman8ca8d762003-04-22 19:02:15 +000020 */
Uwe Hermanne4870472010-11-04 23:23:47 +000021
22/*
23 * Lots of mods by Ron Minnich <rminnich@lanl.gov>, with
24 * the final architecture guidance from Tom Merritt <tjm@codegen.com>.
25 *
Myles Watson032a9652009-05-11 22:24:53 +000026 * In particular, we changed from the one-pass original version to
27 * Tom's recommended multiple-pass version. I wasn't sure about doing
Eric Biederman8ca8d762003-04-22 19:02:15 +000028 * it with multiple passes, until I actually started doing it and saw
Uwe Hermanne4870472010-11-04 23:23:47 +000029 * the wisdom of Tom's recommendations...
Eric Biederman8ca8d762003-04-22 19:02:15 +000030 *
31 * Lots of cleanups by Eric Biederman to handle bridges, and to
Uwe Hermanne4870472010-11-04 23:23:47 +000032 * handle resource allocation for non-PCI devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +000033 */
34
35#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000036#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000037#include <device/device.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020038#include <device/pci_def.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000039#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000040#include <stdlib.h>
41#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000042#include <smp/spinlock.h>
Duncan Laurieb4aaaa72012-01-17 09:03:11 -080043#if CONFIG_ARCH_X86
44#include <arch/ebda.h>
45#endif
Aaron Durbin05294292013-04-30 15:41:13 -050046#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000047
Li-Ta Loe5266692004-03-23 21:28:05 +000048/** Linked list of ALL devices */
Eric Biederman5cd81732004-03-11 15:01:31 +000049struct device *all_devices = &dev_root;
Li-Ta Loe5266692004-03-23 21:28:05 +000050/** Pointer to the last device */
Myles Watson70679a02010-09-01 21:03:03 +000051extern struct device *last_dev;
Myles Watsonc25cc112010-05-21 14:33:48 +000052/** Linked list of free resources */
53struct resource *free_resources = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +000054
Nico Huberacd7d952012-07-25 10:33:05 +020055/**
56 * Initialize all chips of statically known devices.
57 *
58 * Will be called before bus enumeration to initialize chips stated in the
59 * device tree.
60 */
61void dev_initialize_chips(void)
62{
63 struct device *dev;
64
65 for (dev = all_devices; dev; dev = dev->next) {
66 /* Initialize chip if we haven't yet. */
67 if (dev->chip_ops && dev->chip_ops->init &&
68 !dev->chip_ops->initialized) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -070069 post_log_path(dev);
Nico Huberacd7d952012-07-25 10:33:05 +020070 dev->chip_ops->init(dev->chip_info);
71 dev->chip_ops->initialized = 1;
72 }
73 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -070074 post_log_clear();
Nico Huberacd7d952012-07-25 10:33:05 +020075}
76
Marc Jones2a58ecd2013-10-29 17:32:00 -060077/**
78 * Finalize all chips of statically known devices.
79 *
80 * This is the last call before calling the payload. This is a good place
81 * to lock registers or other final cleanup.
82 */
83void dev_finalize_chips(void)
84{
85 struct device *dev;
86
87 for (dev = all_devices; dev; dev = dev->next) {
88 /* Initialize chip if we haven't yet. */
89 if (dev->chip_ops && dev->chip_ops->final &&
90 !dev->chip_ops->finalized) {
91 dev->chip_ops->final(dev->chip_info);
92 dev->chip_ops->finalized = 1;
93 }
94 }
95}
96
Uwe Hermannc1ee4292010-10-17 19:01:48 +000097DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000098
Kyösti Mälkkib25374c2012-08-01 08:05:22 +030099#if CONFIG_GFXUMA
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300100/* IGD UMA memory */
101uint64_t uma_memory_base = 0;
102uint64_t uma_memory_size = 0;
Kyösti Mälkkib25374c2012-08-01 08:05:22 +0300103#endif
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300104
Li-Ta Loe5266692004-03-23 21:28:05 +0000105/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000106 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +0000107 *
Martin Roth63373ed2013-07-08 16:24:19 -0600108 * Allocate a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000109 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000110 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000111 * @param parent Parent bus the newly created device should be attached to.
112 * @param path Path to the device to be created.
113 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +0000114 *
115 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +0000116 */
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300117static device_t __alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000118{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000119 device_t dev, child;
Li-Ta Loe5266692004-03-23 21:28:05 +0000120
Myles Watson29cc9ed2009-07-02 18:56:24 +0000121 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000122 for (child = parent->children; child && child->sibling; /* */ )
Eric Biedermane9a271e32003-09-02 03:36:25 +0000123 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +0000124
Eric Biedermane9a271e32003-09-02 03:36:25 +0000125 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000126 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +0000127 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000128
Eric Biedermane9a271e32003-09-02 03:36:25 +0000129 memset(dev, 0, sizeof(*dev));
130 memcpy(&dev->path, path, sizeof(*path));
131
Myles Watson29cc9ed2009-07-02 18:56:24 +0000132 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +0000133 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000134
Eric Biedermanb78c1972004-10-14 20:54:17 +0000135 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000136 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000137 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000138 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000139 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000140 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000141
Eric Biederman03acab62004-10-14 21:25:53 +0000142 /* Append a new device to the global device list.
143 * The list is used to find devices once everything is set up.
144 */
Myles Watson70679a02010-09-01 21:03:03 +0000145 last_dev->next = dev;
146 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000147
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300148 return dev;
149}
150
151device_t alloc_dev(struct bus *parent, struct device_path *path)
152{
153 device_t dev;
154 spin_lock(&dev_lock);
155 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000156 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000157 return dev;
158}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000159
Li-Ta Loe5266692004-03-23 21:28:05 +0000160/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300161 * See if a device structure already exists and if not allocate it.
162 *
163 * @param parent The bus to find the device on.
164 * @param path The relative path from the bus to the appropriate device.
165 * @return Pointer to a device structure for the device on bus at path.
166 */
167device_t alloc_find_dev(struct bus *parent, struct device_path *path)
168{
169 device_t child;
170 spin_lock(&dev_lock);
171 child = find_dev_path(parent, path);
172 if (!child)
173 child = __alloc_dev(parent, path);
174 spin_unlock(&dev_lock);
175 return child;
176}
177
178/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000179 * Round a number up to an alignment.
180 *
181 * @param val The starting value.
Martin Roth32bc6b62015-01-04 16:54:35 -0700182 * @param pow Alignment as a power of two.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000183 * @return Rounded up number.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184 */
Eric Biederman448bd632004-10-14 22:52:15 +0000185static resource_t round(resource_t val, unsigned long pow)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186{
Eric Biederman448bd632004-10-14 22:52:15 +0000187 resource_t mask;
188 mask = (1ULL << pow) - 1ULL;
189 val += mask;
190 val &= ~mask;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000191 return val;
192}
193
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000194/**
195 * Read the resources on all devices of a given bus.
196 *
197 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000198 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000199static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000200{
201 struct device *curdev;
202
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000203 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
204 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000205
Myles Watson29cc9ed2009-07-02 18:56:24 +0000206 /* Walk through all devices and find which resources they need. */
207 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000208 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000209
210 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000211 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000212
Eric Biedermane9a271e32003-09-02 03:36:25 +0000213 if (!curdev->ops || !curdev->ops->read_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000214 printk(BIOS_ERR, "%s missing read_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000215 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000216 continue;
217 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700218 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000219 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000220
221 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000222 for (link = curdev->link_list; link; link = link->next)
223 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000224 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700225 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000226 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000227 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000228}
229
Eric Biedermane9a271e32003-09-02 03:36:25 +0000230struct pick_largest_state {
231 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000232 struct device *result_dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000233 struct resource *result;
234 int seen_last;
235};
236
Myles Watson29cc9ed2009-07-02 18:56:24 +0000237static void pick_largest_resource(void *gp, struct device *dev,
238 struct resource *resource)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000239{
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000240 struct pick_largest_state *state = gp;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000241 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000242
Eric Biedermane9a271e32003-09-02 03:36:25 +0000243 last = state->last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000244
245 /* Be certain to pick the successor to last. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000246 if (resource == last) {
247 state->seen_last = 1;
248 return;
249 }
Myles Watson032a9652009-05-11 22:24:53 +0000250 if (resource->flags & IORESOURCE_FIXED)
Uwe Hermanne4870472010-11-04 23:23:47 +0000251 return; /* Skip it. */
Myles Watson032a9652009-05-11 22:24:53 +0000252 if (last && ((last->align < resource->align) ||
253 ((last->align == resource->align) &&
254 (last->size < resource->size)) ||
255 ((last->align == resource->align) &&
256 (last->size == resource->size) && (!state->seen_last)))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000257 return;
258 }
Myles Watson032a9652009-05-11 22:24:53 +0000259 if (!state->result ||
260 (state->result->align < resource->align) ||
261 ((state->result->align == resource->align) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000262 (state->result->size < resource->size))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000263 state->result_dev = dev;
264 state->result = resource;
Myles Watson032a9652009-05-11 22:24:53 +0000265 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000266}
267
Myles Watson29cc9ed2009-07-02 18:56:24 +0000268static struct device *largest_resource(struct bus *bus,
269 struct resource **result_res,
270 unsigned long type_mask,
271 unsigned long type)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000272{
273 struct pick_largest_state state;
274
275 state.last = *result_res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000276 state.result_dev = NULL;
277 state.result = NULL;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000278 state.seen_last = 0;
279
Myles Watson032a9652009-05-11 22:24:53 +0000280 search_bus_resources(bus, type_mask, type, pick_largest_resource,
281 &state);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000282
283 *result_res = state.result;
284 return state.result_dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000285}
286
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000287/**
Uwe Hermanne4870472010-11-04 23:23:47 +0000288 * This function is the guts of the resource allocator.
Myles Watson032a9652009-05-11 22:24:53 +0000289 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000290 * The problem.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000291 * - Allocate resource locations for every device.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000292 * - Don't overlap, and follow the rules of bridges.
293 * - Don't overlap with resources in fixed locations.
294 * - Be efficient so we don't have ugly strategies.
295 *
296 * The strategy.
297 * - Devices that have fixed addresses are the minority so don't
Myles Watson29cc9ed2009-07-02 18:56:24 +0000298 * worry about them too much. Instead only use part of the address
299 * space for devices with programmable addresses. This easily handles
Eric Biederman8ca8d762003-04-22 19:02:15 +0000300 * everything except bridges.
301 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000302 * - PCI devices are required to have their sizes and their alignments
303 * equal. In this case an optimal solution to the packing problem
304 * exists. Allocate all devices from highest alignment to least
305 * alignment or vice versa. Use this.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000306 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000307 * - So we can handle more than PCI run two allocation passes on bridges. The
308 * first to see how large the resources are behind the bridge, and what
309 * their alignment requirements are. The second to assign a safe address to
310 * the devices behind the bridge. This allows us to treat a bridge as just
311 * a device with a couple of resources, and not need to special case it in
312 * the allocator. Also this allows handling of other types of bridges.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000313 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000314 * @param bus The bus we are traversing.
315 * @param bridge The bridge resource which must contain the bus' resources.
316 * @param type_mask This value gets ANDed with the resource type.
317 * @param type This value must match the result of the AND.
318 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000319 */
Myles Watson54913b92009-10-13 20:00:09 +0000320static void compute_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000321 unsigned long type_mask, unsigned long type)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000322{
323 struct device *dev;
324 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000325 resource_t base;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000326 base = round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000327
Uwe Hermanne4870472010-11-04 23:23:47 +0000328 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
329 " limit: %llx\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000330 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
Uwe Hermanne4870472010-11-04 23:23:47 +0000331 "prefmem" : "mem", base, bridge->size, bridge->align,
332 bridge->gran, bridge->limit);
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000333
Uwe Hermanne4870472010-11-04 23:23:47 +0000334 /* For each child which is a bridge, compute the resource needs. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000335 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000336 struct resource *child_bridge;
337
Myles Watson894a3472010-06-09 22:41:35 +0000338 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000339 continue;
340
341 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000342 for (child_bridge = dev->resource_list; child_bridge;
343 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000344 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000345
Uwe Hermanne4870472010-11-04 23:23:47 +0000346 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
347 || (child_bridge->flags & type_mask) != type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000348 continue;
349
Uwe Hermanne4870472010-11-04 23:23:47 +0000350 /*
351 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000352 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000353 * and non-prefetchable memory. Bridges below them need
354 * it separated. Add the PREFETCH flag to the type_mask
355 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000356 */
Myles Watson894a3472010-06-09 22:41:35 +0000357 link = dev->link_list;
358 while (link && link->link_num !=
359 IOINDEX_LINK(child_bridge->index))
360 link = link->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000361
362 if (link == NULL) {
Myles Watson894a3472010-06-09 22:41:35 +0000363 printk(BIOS_ERR, "link %ld not found on %s\n",
364 IOINDEX_LINK(child_bridge->index),
365 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000366 }
367
Myles Watson894a3472010-06-09 22:41:35 +0000368 compute_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000369 type_mask | IORESOURCE_PREFETCH,
370 type | (child_bridge->flags &
371 IORESOURCE_PREFETCH));
372 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000373 }
374
Myles Watson29cc9ed2009-07-02 18:56:24 +0000375 /* Remember we haven't found anything yet. */
376 resource = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000377
Uwe Hermanne4870472010-11-04 23:23:47 +0000378 /*
379 * Walk through all the resources on the current bus and compute the
380 * amount of address space taken by them. Take granularity and
Myles Watson29cc9ed2009-07-02 18:56:24 +0000381 * alignment into account.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000382 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000383 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000384
Myles Watson29cc9ed2009-07-02 18:56:24 +0000385 /* Size 0 resources can be skipped. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000386 if (!resource->size)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000387 continue;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000388
Myles Watson29cc9ed2009-07-02 18:56:24 +0000389 /* Propagate the resource alignment to the bridge resource. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000390 if (resource->align > bridge->align)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000391 bridge->align = resource->align;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000392
393 /* Propagate the resource limit to the bridge register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000394 if (bridge->limit > resource->limit)
Eric Biedermandbec2d42004-10-21 10:44:08 +0000395 bridge->limit = resource->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000396
397 /* Warn if it looks like APICs aren't declared. */
398 if ((resource->limit == 0xffffffff) &&
399 (resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000400 printk(BIOS_ERR,
401 "Resource limit looks wrong! (no APIC?)\n");
Patrick Georgi51615092012-03-11 19:31:03 +0100402 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000403 dev_path(dev), resource->index, resource->limit);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000404 }
Stefan Reinauer51754a32008-08-01 12:28:38 +0000405
Eric Biederman8ca8d762003-04-22 19:02:15 +0000406 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000407 /*
408 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000409 * expansion card addresses. The legacy PCI decodes
410 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
411 * 0x00 - 0xff can be used out of each 0x400 block of
412 * I/O space.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000413 */
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000414 if ((base & 0x300) != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000415 base = (base & ~0x3ff) + 0x400;
416 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000417 /*
418 * Don't allow allocations in the VGA I/O range.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000419 * PCI has special cases for that.
420 */
421 else if ((base >= 0x3b0) && (base <= 0x3df)) {
422 base = 0x3e0;
423 }
424 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000425 /* Base must be aligned. */
426 base = round(base, resource->align);
427 resource->base = base;
428 base += resource->size;
Myles Watson032a9652009-05-11 22:24:53 +0000429
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000430 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000431 dev_path(dev), resource->index, resource->base,
432 resource->base + resource->size - 1,
433 (resource->flags & IORESOURCE_IO) ? "io" :
434 (resource->flags & IORESOURCE_PREFETCH) ?
435 "prefmem" : "mem");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000436 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000437
438 /*
439 * A PCI bridge resource does not need to be a power of two size, but
440 * it does have a minimum granularity. Round the size up to that
441 * minimum granularity so we know not to place something else at an
Martin Roth63373ed2013-07-08 16:24:19 -0600442 * address positively decoded by the bridge.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000443 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000444 bridge->size = round(base, bridge->gran) -
445 round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000446
Uwe Hermanne4870472010-11-04 23:23:47 +0000447 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
448 " limit: %llx done\n", dev_path(bus->dev), __func__,
449 (bridge->flags & IORESOURCE_IO) ? "io" :
450 (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
451 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000452}
453
454/**
455 * This function is the second part of the resource allocator.
456 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000457 * See the compute_resources function for a more detailed explanation.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000458 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000459 * This function assigns the resources a value.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000460 *
461 * @param bus The bus we are traversing.
462 * @param bridge The bridge resource which must contain the bus' resources.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000463 * @param type_mask This value gets ANDed with the resource type.
464 * @param type This value must match the result of the AND.
Uwe Hermanne4870472010-11-04 23:23:47 +0000465 *
466 * @see compute_resources
Myles Watson29cc9ed2009-07-02 18:56:24 +0000467 */
Myles Watson54913b92009-10-13 20:00:09 +0000468static void allocate_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000469 unsigned long type_mask, unsigned long type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000470{
471 struct device *dev;
472 struct resource *resource;
473 resource_t base;
474 base = bridge->base;
475
Uwe Hermanne4870472010-11-04 23:23:47 +0000476 printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d "
477 "limit:%llx\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000478 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
479 "prefmem" : "mem",
480 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
481
482 /* Remember we haven't found anything yet. */
483 resource = NULL;
484
Uwe Hermanne4870472010-11-04 23:23:47 +0000485 /*
486 * Walk through all the resources on the current bus and allocate them
Myles Watson29cc9ed2009-07-02 18:56:24 +0000487 * address space.
488 */
489 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
490
491 /* Propagate the bridge limit to the resource register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000492 if (resource->limit > bridge->limit)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000493 resource->limit = bridge->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000494
495 /* Size 0 resources can be skipped. */
496 if (!resource->size) {
497 /* Set the base to limit so it doesn't confuse tolm. */
498 resource->base = resource->limit;
499 resource->flags |= IORESOURCE_ASSIGNED;
500 continue;
501 }
502
503 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000504 /*
505 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000506 * expansion card addresses. The legacy PCI decodes
507 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
508 * 0x00 - 0xff can be used out of each 0x400 block of
509 * I/O space.
510 */
511 if ((base & 0x300) != 0) {
512 base = (base & ~0x3ff) + 0x400;
513 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000514 /*
515 * Don't allow allocations in the VGA I/O range.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000516 * PCI has special cases for that.
517 */
518 else if ((base >= 0x3b0) && (base <= 0x3df)) {
519 base = 0x3e0;
520 }
521 }
522
523 if ((round(base, resource->align) + resource->size - 1) <=
524 resource->limit) {
525 /* Base must be aligned. */
526 base = round(base, resource->align);
527 resource->base = base;
528 resource->flags |= IORESOURCE_ASSIGNED;
529 resource->flags &= ~IORESOURCE_STORED;
530 base += resource->size;
531 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000532 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000533 printk(BIOS_ERR, " aligned base %llx size %llx "
534 "limit %llx\n", round(base, resource->align),
535 resource->size, resource->limit);
536 printk(BIOS_ERR, " %llx needs to be <= %llx "
537 "(limit)\n", (round(base, resource->align) +
Myles Watson29cc9ed2009-07-02 18:56:24 +0000538 resource->size) - 1, resource->limit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000539 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
540 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
541 ? "Assigned: " : "", dev_path(dev),
542 resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000543 resource->base + resource->size - 1,
Uwe Hermanne4870472010-11-04 23:23:47 +0000544 (resource->flags & IORESOURCE_IO) ? "io"
545 : (resource->flags & IORESOURCE_PREFETCH)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000546 ? "prefmem" : "mem");
547 }
548
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000549 printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000550 (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
Uwe Hermanne4870472010-11-04 23:23:47 +0000551 : "", dev_path(dev), resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000552 resource->size ? resource->base + resource->size - 1 :
Uwe Hermanne4870472010-11-04 23:23:47 +0000553 resource->base, (resource->flags & IORESOURCE_IO)
554 ? "io" : (resource->flags & IORESOURCE_PREFETCH)
555 ? "prefmem" : "mem");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000556 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000557
558 /*
559 * A PCI bridge resource does not need to be a power of two size, but
Myles Watson29cc9ed2009-07-02 18:56:24 +0000560 * it does have a minimum granularity. Round the size up to that
561 * minimum granularity so we know not to place something else at an
562 * address positively decoded by the bridge.
563 */
564
565 bridge->flags |= IORESOURCE_ASSIGNED;
566
Uwe Hermanne4870472010-11-04 23:23:47 +0000567 printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d "
568 "gran: %d done\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000569 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
Uwe Hermanne4870472010-11-04 23:23:47 +0000570 "prefmem" : "mem", base, bridge->size, bridge->align,
571 bridge->gran);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000572
573 /* For each child which is a bridge, allocate_resources. */
574 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000575 struct resource *child_bridge;
576
Myles Watson894a3472010-06-09 22:41:35 +0000577 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000578 continue;
579
580 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000581 for (child_bridge = dev->resource_list; child_bridge;
582 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000583 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000584
585 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
586 (child_bridge->flags & type_mask) != type)
587 continue;
588
Uwe Hermanne4870472010-11-04 23:23:47 +0000589 /*
590 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000591 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000592 * and non-prefetchable memory. Bridges below them need
593 * it separated. Add the PREFETCH flag to the type_mask
594 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000595 */
Myles Watson894a3472010-06-09 22:41:35 +0000596 link = dev->link_list;
597 while (link && link->link_num !=
598 IOINDEX_LINK(child_bridge->index))
599 link = link->next;
600 if (link == NULL)
601 printk(BIOS_ERR, "link %ld not found on %s\n",
602 IOINDEX_LINK(child_bridge->index),
603 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000604
Myles Watson894a3472010-06-09 22:41:35 +0000605 allocate_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000606 type_mask | IORESOURCE_PREFETCH,
607 type | (child_bridge->flags &
608 IORESOURCE_PREFETCH));
609 }
610 }
611}
612
Patrick Georgie1667822012-05-05 15:29:32 +0200613#if CONFIG_PCI_64BIT_PREF_MEM
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000614#define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000615#else
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000616#define MEM_MASK (IORESOURCE_MEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000617#endif
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000618
Uwe Hermanne4870472010-11-04 23:23:47 +0000619#define IO_MASK (IORESOURCE_IO)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000620#define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
Uwe Hermanne4870472010-11-04 23:23:47 +0000621#define MEM_TYPE (IORESOURCE_MEM)
622#define IO_TYPE (IORESOURCE_IO)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000623
624struct constraints {
625 struct resource pref, io, mem;
626};
627
628static void constrain_resources(struct device *dev, struct constraints* limits)
629{
630 struct device *child;
631 struct resource *res;
632 struct resource *lim;
Myles Watson894a3472010-06-09 22:41:35 +0000633 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000634
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000635 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000636
637 /* Constrain limits based on the fixed resources of this device. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000638 for (res = dev->resource_list; res; res = res->next) {
Patrick Georgi18c585b2009-08-28 12:48:02 +0000639 if (!(res->flags & IORESOURCE_FIXED))
640 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000641 if (!res->size) {
642 /* It makes no sense to have 0-sized, fixed resources.*/
Uwe Hermanne4870472010-11-04 23:23:47 +0000643 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
644 "size=0!\n", dev_path(dev), res->index);
Patrick Georgi6bd93f42009-08-19 17:29:41 +0000645 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000646 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000647
648 /* PREFETCH, MEM, or I/O - skip any others. */
649 if ((res->flags & MEM_MASK) == PREF_TYPE)
650 lim = &limits->pref;
651 else if ((res->flags & MEM_MASK) == MEM_TYPE)
652 lim = &limits->mem;
653 else if ((res->flags & IO_MASK) == IO_TYPE)
654 lim = &limits->io;
655 else
656 continue;
657
Uwe Hermanne4870472010-11-04 23:23:47 +0000658 /*
659 * Is it a fixed resource outside the current known region?
660 * If so, we don't have to consider it - it will be handled
661 * correctly and doesn't affect current region's limits.
662 */
663 if (((res->base + res->size -1) < lim->base)
664 || (res->base > lim->limit))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000665 continue;
666
Uwe Hermanne4870472010-11-04 23:23:47 +0000667 /*
668 * Choose to be above or below fixed resources. This check is
669 * signed so that "negative" amounts of space are handled
670 * correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000671 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000672 if ((signed long long)(lim->limit - (res->base + res->size -1))
673 > (signed long long)(res->base - lim->base))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000674 lim->base = res->base + res->size;
675 else
676 lim->limit = res->base -1;
677 }
678
679 /* Descend into every enabled child and look for fixed resources. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000680 for (link = dev->link_list; link; link = link->next) {
681 for (child = link->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000682 if (child->enabled)
683 constrain_resources(child, limits);
Uwe Hermanne4870472010-11-04 23:23:47 +0000684 }
685 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000686}
687
688static void avoid_fixed_resources(struct device *dev)
689{
690 struct constraints limits;
691 struct resource *res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000692
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000693 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000694
Uwe Hermanne4870472010-11-04 23:23:47 +0000695 /* Initialize constraints to maximum size. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000696 limits.pref.base = 0;
697 limits.pref.limit = 0xffffffffffffffffULL;
698 limits.io.base = 0;
699 limits.io.limit = 0xffffffffffffffffULL;
700 limits.mem.base = 0;
701 limits.mem.limit = 0xffffffffffffffffULL;
702
703 /* Constrain the limits to dev's initial resources. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000704 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000705 if ((res->flags & IORESOURCE_FIXED))
706 continue;
Patrick Georgi51615092012-03-11 19:31:03 +0100707 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
Uwe Hermanne4870472010-11-04 23:23:47 +0000708 dev_path(dev), res->index, res->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000709 if ((res->flags & MEM_MASK) == PREF_TYPE &&
710 (res->limit < limits.pref.limit))
711 limits.pref.limit = res->limit;
712 if ((res->flags & MEM_MASK) == MEM_TYPE &&
713 (res->limit < limits.mem.limit))
714 limits.mem.limit = res->limit;
715 if ((res->flags & IO_MASK) == IO_TYPE &&
716 (res->limit < limits.io.limit))
717 limits.io.limit = res->limit;
718 }
719
720 /* Look through the tree for fixed resources and update the limits. */
721 constrain_resources(dev, &limits);
722
723 /* Update dev's resources with new limits. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000724 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000725 struct resource *lim;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000726
727 if ((res->flags & IORESOURCE_FIXED))
728 continue;
729
730 /* PREFETCH, MEM, or I/O - skip any others. */
731 if ((res->flags & MEM_MASK) == PREF_TYPE)
732 lim = &limits.pref;
733 else if ((res->flags & MEM_MASK) == MEM_TYPE)
734 lim = &limits.mem;
735 else if ((res->flags & IO_MASK) == IO_TYPE)
736 lim = &limits.io;
737 else
738 continue;
739
Patrick Georgi51615092012-03-11 19:31:03 +0100740 printk(BIOS_SPEW, "%s2: %s@%02lx limit %08llx\n", __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000741 dev_path(dev), res->index, res->limit);
Patrick Georgi51615092012-03-11 19:31:03 +0100742 printk(BIOS_SPEW, "\tlim->base %08llx lim->limit %08llx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000743 lim->base, lim->limit);
744
745 /* Is the resource outside the limits? */
746 if (lim->base > res->base)
747 res->base = lim->base;
748 if (res->limit > lim->limit)
749 res->limit = lim->limit;
750 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000751}
arch import user (historical)dc811182005-07-06 17:16:09 +0000752
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000753device_t vga_pri = 0;
Myles Watsonc7233e02009-07-02 19:02:33 +0000754static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000755{
Uwe Hermann312673c2009-10-27 21:49:33 +0000756 /*
Martin Roth63373ed2013-07-08 16:24:19 -0600757 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000758 * This function knows too much about PCI stuff, it should be just
759 * an iterator/visitor.
760 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000761
Myles Watson29cc9ed2009-07-02 18:56:24 +0000762 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200763 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000764 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000765
Eric Biedermanb78c1972004-10-14 20:54:17 +0000766 bus = 0;
767 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000768 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000769
Patrick Georgi557ecf22012-07-20 12:16:17 +0200770 dev = NULL;
771 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000772 if (!dev->enabled)
773 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000774
Patrick Georgi557ecf22012-07-20 12:16:17 +0200775 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000776
Patrick Georgi557ecf22012-07-20 12:16:17 +0200777 if (dev->on_mainboard) {
778 vga_onboard = dev;
Kostr1f0d3792012-10-06 13:27:58 +0400779 } else {
Patrick Georgi557ecf22012-07-20 12:16:17 +0200780 vga = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000781 }
Myles Watson032a9652009-05-11 22:24:53 +0000782
Patrick Georgi557ecf22012-07-20 12:16:17 +0200783 /* It isn't safe to enable all VGA cards. */
784 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200785 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000786
Uwe Hermanne4870472010-11-04 23:23:47 +0000787 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000788 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200789
790 if (CONFIG_ONBOARD_VGA_IS_PRIMARY && vga_onboard)
791 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000792
Patrick Georgi5869fa22012-07-20 12:29:33 +0200793 /* If we prefer plugin VGA over chipset VGA, the chipset might
794 want to know. */
795 if (!CONFIG_ONBOARD_VGA_IS_PRIMARY && (vga != vga_onboard) &&
796 vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) {
797 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
798 vga_onboard->ops->disable(vga_onboard);
799 }
800
arch import user (historical)dc811182005-07-06 17:16:09 +0000801 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000802 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000803 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000804 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000805 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
806 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000807 bus = vga->bus;
808 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000809
Myles Watson29cc9ed2009-07-02 18:56:24 +0000810 /* Now walk up the bridges setting the VGA enable. */
811 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000812 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000813 dev_path(bus->dev));
Li-Ta Lodb7f47c2004-01-08 21:15:49 +0000814 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000815 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000816 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000817}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000818
Li-Ta Lo04930692004-11-25 17:37:19 +0000819/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000820 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000821 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000822 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000823 * resources to hardware. For bridge devices, the set_resources() method
824 * has to recurse into every down stream buses.
825 *
826 * Mutual recursion:
827 * assign_resources() -> device_operation::set_resources()
828 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000829 *
830 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000831 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000832void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000833{
834 struct device *curdev;
835
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000836 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000837 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000838
Myles Watson29cc9ed2009-07-02 18:56:24 +0000839 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000840 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000841 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000842
Eric Biedermane9a271e32003-09-02 03:36:25 +0000843 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000844 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000845 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000846 continue;
847 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700848 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000849 curdev->ops->set_resources(curdev);
850 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700851 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000852 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000853 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000854}
855
Li-Ta Lo5782d272004-04-26 17:51:20 +0000856/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000857 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000858 *
859 * Enable resources of the device by calling the device specific
860 * enable_resources() method.
861 *
862 * The parent's resources should be enabled first to avoid having enabling
863 * order problem. This is done by calling the parent's enable_resources()
Martin Roth63373ed2013-07-08 16:24:19 -0600864 * method before its children's enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000865 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000866 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000867 */
Myles Watson7eac4452010-06-17 16:16:56 +0000868static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000869{
Myles Watson7eac4452010-06-17 16:16:56 +0000870 struct device *dev;
871 struct bus *c_link;
872
873 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700874 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
875 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000876 dev->ops->enable_resources(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700877 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000878 }
Myles Watson7eac4452010-06-17 16:16:56 +0000879
880 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000881 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000882 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000883 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700884 post_log_clear();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000885}
886
Myles Watson032a9652009-05-11 22:24:53 +0000887/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000888 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
889 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000890 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000891 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000892 */
893int reset_bus(struct bus *bus)
894{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000895 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000896 bus->dev->ops->reset_bus(bus);
897 bus->reset_needed = 0;
898 return 1;
899 }
900 return 0;
901}
902
Myles Watson032a9652009-05-11 22:24:53 +0000903/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000904 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000905 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000906 * If there are bridges on the bus, recursively scan the buses behind the
907 * bridges. If the setting up and tuning of the bus causes a reset to be
908 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000909 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000910 * @param busdev Pointer to the bus device.
911 * @param max Current bus number.
912 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000913 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000914unsigned int scan_bus(struct device *busdev, unsigned int max)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000915{
916 unsigned int new_max;
917 int do_scan_bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000918
Myles Watson29cc9ed2009-07-02 18:56:24 +0000919 if (!busdev || !busdev->enabled || !busdev->ops ||
920 !busdev->ops->scan_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000921 return max;
922 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000923
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700924 post_log_path(busdev);
925
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000926 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000927 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000928 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000929 new_max = busdev->ops->scan_bus(busdev, max);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000930 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000931 for (link = busdev->link_list; link; link = link->next) {
932 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000933 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000934 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000935 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000936 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000937 }
938 }
939 }
940 return new_max;
941}
942
Li-Ta Loe5266692004-03-23 21:28:05 +0000943/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000944 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000945 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000946 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000947 * file. The device structures for these devices are generated at compile
948 * time by the config tool and are organized into the device tree. This
949 * function determines if the devices created at compile time actually exist
950 * in the physical system.
951 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000952 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000953 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000954 * device tree.
955 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000956 * This function starts from the root device 'dev_root', scans the buses in
957 * the system recursively, and modifies the device tree according to the
958 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000959 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000960 * This function has no idea how to scan and probe buses and devices at all.
961 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000962 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000963 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000964 */
965void dev_enumerate(void)
966{
967 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000968
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000969 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000970
Eric Biederman8ca8d762003-04-22 19:02:15 +0000971 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000972
Uwe Hermanne4870472010-11-04 23:23:47 +0000973 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000974 printk(BIOS_SPEW, "Compare with tree...\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000975 show_devs_tree(root, BIOS_SPEW, 0, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000976
Stefan Reinauera675d492012-08-07 14:50:47 -0700977 if (root->chip_ops && root->chip_ops->enable_dev)
978 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000979
Eric Biedermanb78c1972004-10-14 20:54:17 +0000980 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000981 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000982 return;
983 }
Stefan Reinauer6afcea82009-07-18 17:58:44 +0000984 scan_bus(root, 0);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700985 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000986 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000987}
988
Li-Ta Loe5266692004-03-23 21:28:05 +0000989/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000990 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +0000991 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000992 * Starting at the root of the device tree, travel it recursively in two
993 * passes. In the first pass, we compute and allocate resources (ranges)
Martin Roth63373ed2013-07-08 16:24:19 -0600994 * required by each device. In the second pass, the resources ranges are
Li-Ta Lo04930692004-11-25 17:37:19 +0000995 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000996 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000997 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000998 *
999 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +00001000 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001001 */
1002void dev_configure(void)
1003{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001004 struct resource *res;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001005 struct device *root;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001006 struct device *child;
Li-Ta Loe5266692004-03-23 21:28:05 +00001007
Marc Jones80bd74c2012-02-21 17:44:35 +01001008 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +01001009
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001010 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001011
Eric Biedermanb78c1972004-10-14 20:54:17 +00001012 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +00001013
Uwe Hermanne4870472010-11-04 23:23:47 +00001014 /*
1015 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +00001016 * space for IO, MEM, and PREFMEM resources in the domain. The
1017 * allocation of device resources will be done from this address space.
1018 */
Myles Watsoncd5d7562009-05-12 13:43:34 +00001019
Myles Watson29cc9ed2009-07-02 18:56:24 +00001020 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +00001021
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001022 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001023 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001024 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001025
Stefan Reinauer39e72292009-10-26 16:47:05 +00001026 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +00001027
Myles Watson29cc9ed2009-07-02 18:56:24 +00001028 /* Compute resources for all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001029 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001030 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001031 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001032 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001033 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001034 if (res->flags & IORESOURCE_FIXED)
1035 continue;
1036 if (res->flags & IORESOURCE_PREFETCH) {
Myles Watson894a3472010-06-09 22:41:35 +00001037 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001038 res, MEM_MASK, PREF_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001039 continue;
1040 }
1041 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001042 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001043 res, MEM_MASK, MEM_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001044 continue;
1045 }
1046 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001047 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001048 res, IO_MASK, IO_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001049 continue;
1050 }
1051 }
1052 }
1053
1054 /* For all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001055 for (child = root->link_list->children; child; child=child->sibling)
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001056 if (child->path.type == DEVICE_PATH_DOMAIN)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001057 avoid_fixed_resources(child);
1058
Uwe Hermanne4870472010-11-04 23:23:47 +00001059 /*
1060 * Now we need to adjust the resources. MEM resources need to start at
Martin Roth63373ed2013-07-08 16:24:19 -06001061 * the highest address manageable.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001062 */
Myles Watson894a3472010-06-09 22:41:35 +00001063 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001064 if (child->path.type != DEVICE_PATH_DOMAIN)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001065 continue;
Myles Watsonc25cc112010-05-21 14:33:48 +00001066 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001067 if (!(res->flags & IORESOURCE_MEM) ||
1068 res->flags & IORESOURCE_FIXED)
1069 continue;
1070 res->base = resource_max(res);
1071 }
1072 }
Eric Biederman5cd81732004-03-11 15:01:31 +00001073
Eric Biedermanb78c1972004-10-14 20:54:17 +00001074 /* Store the computed resource allocations into device registers ... */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001075 printk(BIOS_INFO, "Setting resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001076 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001077 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001078 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001079 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001080 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001081 if (res->flags & IORESOURCE_FIXED)
1082 continue;
1083 if (res->flags & IORESOURCE_PREFETCH) {
Myles Watson894a3472010-06-09 22:41:35 +00001084 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001085 res, MEM_MASK, PREF_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001086 continue;
1087 }
1088 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001089 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001090 res, MEM_MASK, MEM_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001091 continue;
1092 }
1093 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001094 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001095 res, IO_MASK, IO_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001096 continue;
1097 }
1098 }
1099 }
Myles Watson894a3472010-06-09 22:41:35 +00001100 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001101 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001102 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +00001103
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001104 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001105}
1106
Li-Ta Loe5266692004-03-23 21:28:05 +00001107/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001108 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +00001109 *
1110 * Starting at the root, walk the tree and enable all devices/bridges by
1111 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001112 */
1113void dev_enable(void)
1114{
Myles Watson7eac4452010-06-17 16:16:56 +00001115 struct bus *link;
1116
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001117 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001118
Uwe Hermanne4870472010-11-04 23:23:47 +00001119 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001120 for (link = dev_root.link_list; link; link = link->next)
1121 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +00001122
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001123 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001124}
1125
Li-Ta Loe5266692004-03-23 21:28:05 +00001126/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001127 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +00001128 *
Uwe Hermanne4870472010-11-04 23:23:47 +00001129 * The parent should be initialized first to avoid having an ordering problem.
Martin Roth63373ed2013-07-08 16:24:19 -06001130 * This is done by calling the parent's init() method before its children's
Uwe Hermanne4870472010-11-04 23:23:47 +00001131 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +00001132 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001133 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +00001134 */
1135static void init_dev(struct device *dev)
1136{
Uwe Hermanne4870472010-11-04 23:23:47 +00001137 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +00001138 return;
Myles Watson7eac4452010-06-17 16:16:56 +00001139
1140 if (!dev->initialized && dev->ops && dev->ops->init) {
Aaron Durbin05294292013-04-30 15:41:13 -05001141#if CONFIG_HAVE_MONOTONIC_TIMER
1142 struct mono_time start_time;
1143 struct rela_time dev_init_time;
1144
1145 timer_monotonic_get(&start_time);
1146#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001147 if (dev->path.type == DEVICE_PATH_I2C) {
1148 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1149 dev_path(dev->bus->dev), dev->bus->link_num);
1150 }
1151
1152 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1153 dev->initialized = 1;
1154 dev->ops->init(dev);
Aaron Durbin05294292013-04-30 15:41:13 -05001155#if CONFIG_HAVE_MONOTONIC_TIMER
1156 dev_init_time = current_time_from(&start_time);
1157 printk(BIOS_DEBUG, "%s init %ld usecs\n", dev_path(dev),
1158 rela_time_in_microseconds(&dev_init_time));
1159#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001160 }
1161}
1162
1163static void init_link(struct bus *link)
1164{
1165 struct device *dev;
1166 struct bus *c_link;
1167
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001168 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Lauriecb73a842013-06-10 10:41:04 -07001169 post_code(POST_BS_DEV_INIT);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001170 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +00001171 init_dev(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001172 }
Myles Watson7eac4452010-06-17 16:16:56 +00001173
1174 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001175 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +00001176 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +00001177 }
1178}
1179
1180/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001181 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +00001182 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001183 * Starting at the root device, call the device's init() method to do
1184 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001185 */
1186void dev_initialize(void)
1187{
Myles Watson7eac4452010-06-17 16:16:56 +00001188 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001189
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001190 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +00001191
Duncan Laurieb4aaaa72012-01-17 09:03:11 -08001192#if CONFIG_ARCH_X86
1193 /* Ensure EBDA is prepared before Option ROMs. */
1194 setup_default_ebda();
1195#endif
1196
Myles Watson1bd3fb72010-08-16 16:25:23 +00001197 /* First call the mainboard init. */
1198 init_dev(&dev_root);
1199
Uwe Hermanne4870472010-11-04 23:23:47 +00001200 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001201 for (link = dev_root.link_list; link; link = link->next)
1202 init_link(link);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001203 post_log_clear();
Myles Watson7eac4452010-06-17 16:16:56 +00001204
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001205 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001206 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001207}
Marc Jones2a58ecd2013-10-29 17:32:00 -06001208
1209/**
1210 * Finalize a specific device.
1211 *
1212 * The parent should be finalized first to avoid having an ordering problem.
1213 * This is done by calling the parent's final() method before its childrens'
1214 * final() methods.
1215 *
1216 * @param dev The device to be initialized.
1217 */
1218static void final_dev(struct device *dev)
1219{
1220 if (!dev->enabled)
1221 return;
1222
1223 if (dev->ops && dev->ops->final) {
1224 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
1225 dev->ops->final(dev);
1226 }
1227}
1228
1229static void final_link(struct bus *link)
1230{
1231 struct device *dev;
1232 struct bus *c_link;
1233
1234 for (dev = link->children; dev; dev = dev->sibling)
1235 final_dev(dev);
1236
1237 for (dev = link->children; dev; dev = dev->sibling) {
1238 for (c_link = dev->link_list; c_link; c_link = c_link->next)
1239 final_link(c_link);
1240 }
1241}
1242/**
1243 * Finalize all devices in the global device tree.
1244 *
1245 * Starting at the root device, call the device's final() method to do
1246 * device-specific cleanup, then call each child's final() method.
1247 */
1248void dev_finalize(void)
1249{
1250 struct bus *link;
1251
1252 printk(BIOS_INFO, "Finalize devices...\n");
1253
1254 /* First call the mainboard finalize. */
1255 final_dev(&dev_root);
1256
1257 /* Now finalize everything. */
1258 for (link = dev_root.link_list; link; link = link->next)
1259 final_link(link);
1260
1261 printk(BIOS_INFO, "Devices finalized\n");
1262}