blob: a971270eba3cf12a83715961a07bdaab66f5e95a [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>
38#include <device/pci.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) {
69 dev->chip_ops->init(dev->chip_info);
70 dev->chip_ops->initialized = 1;
71 }
72 }
73}
74
Uwe Hermannc1ee4292010-10-17 19:01:48 +000075DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000076
Kyösti Mälkkib25374c2012-08-01 08:05:22 +030077#if CONFIG_GFXUMA
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +030078/* IGD UMA memory */
79uint64_t uma_memory_base = 0;
80uint64_t uma_memory_size = 0;
Kyösti Mälkkib25374c2012-08-01 08:05:22 +030081#endif
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +030082
Li-Ta Loe5266692004-03-23 21:28:05 +000083/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000084 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +000085 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000086 * Allocte a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +000087 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +000088 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000089 * @param parent Parent bus the newly created device should be attached to.
90 * @param path Path to the device to be created.
91 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +000092 *
93 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +000094 */
Kyösti Mälkkia5650a42012-07-07 17:15:51 +030095static device_t __alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +000096{
Eric Biedermane9a271e32003-09-02 03:36:25 +000097 device_t dev, child;
Li-Ta Loe5266692004-03-23 21:28:05 +000098
Myles Watson29cc9ed2009-07-02 18:56:24 +000099 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000100 for (child = parent->children; child && child->sibling; /* */ )
Eric Biedermane9a271e32003-09-02 03:36:25 +0000101 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +0000102
Eric Biedermane9a271e32003-09-02 03:36:25 +0000103 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000104 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +0000105 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000106
Eric Biedermane9a271e32003-09-02 03:36:25 +0000107 memset(dev, 0, sizeof(*dev));
108 memcpy(&dev->path, path, sizeof(*path));
109
Myles Watson29cc9ed2009-07-02 18:56:24 +0000110 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +0000111 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000112
Eric Biedermanb78c1972004-10-14 20:54:17 +0000113 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000114 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000115 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000116 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000117 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000118 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000119
Eric Biederman03acab62004-10-14 21:25:53 +0000120 /* Append a new device to the global device list.
121 * The list is used to find devices once everything is set up.
122 */
Myles Watson70679a02010-09-01 21:03:03 +0000123 last_dev->next = dev;
124 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000125
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300126 return dev;
127}
128
129device_t alloc_dev(struct bus *parent, struct device_path *path)
130{
131 device_t dev;
132 spin_lock(&dev_lock);
133 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000134 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000135 return dev;
136}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000137
Li-Ta Loe5266692004-03-23 21:28:05 +0000138/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300139 * See if a device structure already exists and if not allocate it.
140 *
141 * @param parent The bus to find the device on.
142 * @param path The relative path from the bus to the appropriate device.
143 * @return Pointer to a device structure for the device on bus at path.
144 */
145device_t alloc_find_dev(struct bus *parent, struct device_path *path)
146{
147 device_t child;
148 spin_lock(&dev_lock);
149 child = find_dev_path(parent, path);
150 if (!child)
151 child = __alloc_dev(parent, path);
152 spin_unlock(&dev_lock);
153 return child;
154}
155
156/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000157 * Round a number up to an alignment.
158 *
159 * @param val The starting value.
160 * @param roundup Alignment as a power of two.
161 * @return Rounded up number.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000162 */
Eric Biederman448bd632004-10-14 22:52:15 +0000163static resource_t round(resource_t val, unsigned long pow)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000164{
Eric Biederman448bd632004-10-14 22:52:15 +0000165 resource_t mask;
166 mask = (1ULL << pow) - 1ULL;
167 val += mask;
168 val &= ~mask;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000169 return val;
170}
171
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000172/**
173 * Read the resources on all devices of a given bus.
174 *
175 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000177static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000178{
179 struct device *curdev;
180
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000181 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
182 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000183
Myles Watson29cc9ed2009-07-02 18:56:24 +0000184 /* Walk through all devices and find which resources they need. */
185 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000186 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000187
188 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000189 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000190
Eric Biedermane9a271e32003-09-02 03:36:25 +0000191 if (!curdev->ops || !curdev->ops->read_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000192 printk(BIOS_ERR, "%s missing read_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000193 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000194 continue;
195 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000196 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000197
198 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000199 for (link = curdev->link_list; link; link = link->next)
200 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000201 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000202 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000203 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000204}
205
Eric Biedermane9a271e32003-09-02 03:36:25 +0000206struct pick_largest_state {
207 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000208 struct device *result_dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000209 struct resource *result;
210 int seen_last;
211};
212
Myles Watson29cc9ed2009-07-02 18:56:24 +0000213static void pick_largest_resource(void *gp, struct device *dev,
214 struct resource *resource)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000215{
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000216 struct pick_largest_state *state = gp;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000217 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000218
Eric Biedermane9a271e32003-09-02 03:36:25 +0000219 last = state->last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000220
221 /* Be certain to pick the successor to last. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000222 if (resource == last) {
223 state->seen_last = 1;
224 return;
225 }
Myles Watson032a9652009-05-11 22:24:53 +0000226 if (resource->flags & IORESOURCE_FIXED)
Uwe Hermanne4870472010-11-04 23:23:47 +0000227 return; /* Skip it. */
Myles Watson032a9652009-05-11 22:24:53 +0000228 if (last && ((last->align < resource->align) ||
229 ((last->align == resource->align) &&
230 (last->size < resource->size)) ||
231 ((last->align == resource->align) &&
232 (last->size == resource->size) && (!state->seen_last)))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000233 return;
234 }
Myles Watson032a9652009-05-11 22:24:53 +0000235 if (!state->result ||
236 (state->result->align < resource->align) ||
237 ((state->result->align == resource->align) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000238 (state->result->size < resource->size))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000239 state->result_dev = dev;
240 state->result = resource;
Myles Watson032a9652009-05-11 22:24:53 +0000241 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000242}
243
Myles Watson29cc9ed2009-07-02 18:56:24 +0000244static struct device *largest_resource(struct bus *bus,
245 struct resource **result_res,
246 unsigned long type_mask,
247 unsigned long type)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000248{
249 struct pick_largest_state state;
250
251 state.last = *result_res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000252 state.result_dev = NULL;
253 state.result = NULL;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000254 state.seen_last = 0;
255
Myles Watson032a9652009-05-11 22:24:53 +0000256 search_bus_resources(bus, type_mask, type, pick_largest_resource,
257 &state);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000258
259 *result_res = state.result;
260 return state.result_dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000261}
262
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000263/**
Uwe Hermanne4870472010-11-04 23:23:47 +0000264 * This function is the guts of the resource allocator.
Myles Watson032a9652009-05-11 22:24:53 +0000265 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000266 * The problem.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000267 * - Allocate resource locations for every device.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000268 * - Don't overlap, and follow the rules of bridges.
269 * - Don't overlap with resources in fixed locations.
270 * - Be efficient so we don't have ugly strategies.
271 *
272 * The strategy.
273 * - Devices that have fixed addresses are the minority so don't
Myles Watson29cc9ed2009-07-02 18:56:24 +0000274 * worry about them too much. Instead only use part of the address
275 * space for devices with programmable addresses. This easily handles
Eric Biederman8ca8d762003-04-22 19:02:15 +0000276 * everything except bridges.
277 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000278 * - PCI devices are required to have their sizes and their alignments
279 * equal. In this case an optimal solution to the packing problem
280 * exists. Allocate all devices from highest alignment to least
281 * alignment or vice versa. Use this.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000282 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000283 * - So we can handle more than PCI run two allocation passes on bridges. The
284 * first to see how large the resources are behind the bridge, and what
285 * their alignment requirements are. The second to assign a safe address to
286 * the devices behind the bridge. This allows us to treat a bridge as just
287 * a device with a couple of resources, and not need to special case it in
288 * the allocator. Also this allows handling of other types of bridges.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000289 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000290 * @param bus The bus we are traversing.
291 * @param bridge The bridge resource which must contain the bus' resources.
292 * @param type_mask This value gets ANDed with the resource type.
293 * @param type This value must match the result of the AND.
294 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000295 */
Myles Watson54913b92009-10-13 20:00:09 +0000296static void compute_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000297 unsigned long type_mask, unsigned long type)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000298{
299 struct device *dev;
300 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000301 resource_t base;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000302 base = round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000303
Uwe Hermanne4870472010-11-04 23:23:47 +0000304 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
305 " limit: %llx\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000306 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
Uwe Hermanne4870472010-11-04 23:23:47 +0000307 "prefmem" : "mem", base, bridge->size, bridge->align,
308 bridge->gran, bridge->limit);
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000309
Uwe Hermanne4870472010-11-04 23:23:47 +0000310 /* For each child which is a bridge, compute the resource needs. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000311 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000312 struct resource *child_bridge;
313
Myles Watson894a3472010-06-09 22:41:35 +0000314 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000315 continue;
316
317 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000318 for (child_bridge = dev->resource_list; child_bridge;
319 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000320 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000321
Uwe Hermanne4870472010-11-04 23:23:47 +0000322 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
323 || (child_bridge->flags & type_mask) != type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000324 continue;
325
Uwe Hermanne4870472010-11-04 23:23:47 +0000326 /*
327 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000328 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000329 * and non-prefetchable memory. Bridges below them need
330 * it separated. Add the PREFETCH flag to the type_mask
331 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000332 */
Myles Watson894a3472010-06-09 22:41:35 +0000333 link = dev->link_list;
334 while (link && link->link_num !=
335 IOINDEX_LINK(child_bridge->index))
336 link = link->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000337
338 if (link == NULL) {
Myles Watson894a3472010-06-09 22:41:35 +0000339 printk(BIOS_ERR, "link %ld not found on %s\n",
340 IOINDEX_LINK(child_bridge->index),
341 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000342 }
343
Myles Watson894a3472010-06-09 22:41:35 +0000344 compute_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000345 type_mask | IORESOURCE_PREFETCH,
346 type | (child_bridge->flags &
347 IORESOURCE_PREFETCH));
348 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000349 }
350
Myles Watson29cc9ed2009-07-02 18:56:24 +0000351 /* Remember we haven't found anything yet. */
352 resource = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000353
Uwe Hermanne4870472010-11-04 23:23:47 +0000354 /*
355 * Walk through all the resources on the current bus and compute the
356 * amount of address space taken by them. Take granularity and
Myles Watson29cc9ed2009-07-02 18:56:24 +0000357 * alignment into account.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000358 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000359 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000360
Myles Watson29cc9ed2009-07-02 18:56:24 +0000361 /* Size 0 resources can be skipped. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000362 if (!resource->size)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000363 continue;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000364
Myles Watson29cc9ed2009-07-02 18:56:24 +0000365 /* Propagate the resource alignment to the bridge resource. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000366 if (resource->align > bridge->align)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000367 bridge->align = resource->align;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000368
369 /* Propagate the resource limit to the bridge register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000370 if (bridge->limit > resource->limit)
Eric Biedermandbec2d42004-10-21 10:44:08 +0000371 bridge->limit = resource->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000372
373 /* Warn if it looks like APICs aren't declared. */
374 if ((resource->limit == 0xffffffff) &&
375 (resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000376 printk(BIOS_ERR,
377 "Resource limit looks wrong! (no APIC?)\n");
Patrick Georgi51615092012-03-11 19:31:03 +0100378 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000379 dev_path(dev), resource->index, resource->limit);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000380 }
Stefan Reinauer51754a32008-08-01 12:28:38 +0000381
Eric Biederman8ca8d762003-04-22 19:02:15 +0000382 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000383 /*
384 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000385 * expansion card addresses. The legacy PCI decodes
386 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
387 * 0x00 - 0xff can be used out of each 0x400 block of
388 * I/O space.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000389 */
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000390 if ((base & 0x300) != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000391 base = (base & ~0x3ff) + 0x400;
392 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000393 /*
394 * Don't allow allocations in the VGA I/O range.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000395 * PCI has special cases for that.
396 */
397 else if ((base >= 0x3b0) && (base <= 0x3df)) {
398 base = 0x3e0;
399 }
400 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000401 /* Base must be aligned. */
402 base = round(base, resource->align);
403 resource->base = base;
404 base += resource->size;
Myles Watson032a9652009-05-11 22:24:53 +0000405
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000406 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000407 dev_path(dev), resource->index, resource->base,
408 resource->base + resource->size - 1,
409 (resource->flags & IORESOURCE_IO) ? "io" :
410 (resource->flags & IORESOURCE_PREFETCH) ?
411 "prefmem" : "mem");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000412 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000413
414 /*
415 * A PCI bridge resource does not need to be a power of two size, but
416 * it does have a minimum granularity. Round the size up to that
417 * minimum granularity so we know not to place something else at an
418 * address postitively decoded by the bridge.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000419 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000420 bridge->size = round(base, bridge->gran) -
421 round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000422
Uwe Hermanne4870472010-11-04 23:23:47 +0000423 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
424 " limit: %llx done\n", dev_path(bus->dev), __func__,
425 (bridge->flags & IORESOURCE_IO) ? "io" :
426 (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
427 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000428}
429
430/**
431 * This function is the second part of the resource allocator.
432 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000433 * See the compute_resources function for a more detailed explanation.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000434 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000435 * This function assigns the resources a value.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000436 *
437 * @param bus The bus we are traversing.
438 * @param bridge The bridge resource which must contain the bus' resources.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000439 * @param type_mask This value gets ANDed with the resource type.
440 * @param type This value must match the result of the AND.
Uwe Hermanne4870472010-11-04 23:23:47 +0000441 *
442 * @see compute_resources
Myles Watson29cc9ed2009-07-02 18:56:24 +0000443 */
Myles Watson54913b92009-10-13 20:00:09 +0000444static void allocate_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000445 unsigned long type_mask, unsigned long type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000446{
447 struct device *dev;
448 struct resource *resource;
449 resource_t base;
450 base = bridge->base;
451
Uwe Hermanne4870472010-11-04 23:23:47 +0000452 printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d "
453 "limit:%llx\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000454 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
455 "prefmem" : "mem",
456 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
457
458 /* Remember we haven't found anything yet. */
459 resource = NULL;
460
Uwe Hermanne4870472010-11-04 23:23:47 +0000461 /*
462 * Walk through all the resources on the current bus and allocate them
Myles Watson29cc9ed2009-07-02 18:56:24 +0000463 * address space.
464 */
465 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
466
467 /* Propagate the bridge limit to the resource register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000468 if (resource->limit > bridge->limit)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000469 resource->limit = bridge->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000470
471 /* Size 0 resources can be skipped. */
472 if (!resource->size) {
473 /* Set the base to limit so it doesn't confuse tolm. */
474 resource->base = resource->limit;
475 resource->flags |= IORESOURCE_ASSIGNED;
476 continue;
477 }
478
479 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000480 /*
481 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000482 * expansion card addresses. The legacy PCI decodes
483 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
484 * 0x00 - 0xff can be used out of each 0x400 block of
485 * I/O space.
486 */
487 if ((base & 0x300) != 0) {
488 base = (base & ~0x3ff) + 0x400;
489 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000490 /*
491 * Don't allow allocations in the VGA I/O range.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000492 * PCI has special cases for that.
493 */
494 else if ((base >= 0x3b0) && (base <= 0x3df)) {
495 base = 0x3e0;
496 }
497 }
498
499 if ((round(base, resource->align) + resource->size - 1) <=
500 resource->limit) {
501 /* Base must be aligned. */
502 base = round(base, resource->align);
503 resource->base = base;
504 resource->flags |= IORESOURCE_ASSIGNED;
505 resource->flags &= ~IORESOURCE_STORED;
506 base += resource->size;
507 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000508 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000509 printk(BIOS_ERR, " aligned base %llx size %llx "
510 "limit %llx\n", round(base, resource->align),
511 resource->size, resource->limit);
512 printk(BIOS_ERR, " %llx needs to be <= %llx "
513 "(limit)\n", (round(base, resource->align) +
Myles Watson29cc9ed2009-07-02 18:56:24 +0000514 resource->size) - 1, resource->limit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000515 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
516 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
517 ? "Assigned: " : "", dev_path(dev),
518 resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000519 resource->base + resource->size - 1,
Uwe Hermanne4870472010-11-04 23:23:47 +0000520 (resource->flags & IORESOURCE_IO) ? "io"
521 : (resource->flags & IORESOURCE_PREFETCH)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000522 ? "prefmem" : "mem");
523 }
524
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000525 printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000526 (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
Uwe Hermanne4870472010-11-04 23:23:47 +0000527 : "", dev_path(dev), resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000528 resource->size ? resource->base + resource->size - 1 :
Uwe Hermanne4870472010-11-04 23:23:47 +0000529 resource->base, (resource->flags & IORESOURCE_IO)
530 ? "io" : (resource->flags & IORESOURCE_PREFETCH)
531 ? "prefmem" : "mem");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000532 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000533
534 /*
535 * A PCI bridge resource does not need to be a power of two size, but
Myles Watson29cc9ed2009-07-02 18:56:24 +0000536 * it does have a minimum granularity. Round the size up to that
537 * minimum granularity so we know not to place something else at an
538 * address positively decoded by the bridge.
539 */
540
541 bridge->flags |= IORESOURCE_ASSIGNED;
542
Uwe Hermanne4870472010-11-04 23:23:47 +0000543 printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d "
544 "gran: %d done\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000545 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
Uwe Hermanne4870472010-11-04 23:23:47 +0000546 "prefmem" : "mem", base, bridge->size, bridge->align,
547 bridge->gran);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000548
549 /* For each child which is a bridge, allocate_resources. */
550 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000551 struct resource *child_bridge;
552
Myles Watson894a3472010-06-09 22:41:35 +0000553 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000554 continue;
555
556 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000557 for (child_bridge = dev->resource_list; child_bridge;
558 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000559 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000560
561 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
562 (child_bridge->flags & type_mask) != type)
563 continue;
564
Uwe Hermanne4870472010-11-04 23:23:47 +0000565 /*
566 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000567 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000568 * and non-prefetchable memory. Bridges below them need
569 * it separated. Add the PREFETCH flag to the type_mask
570 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000571 */
Myles Watson894a3472010-06-09 22:41:35 +0000572 link = dev->link_list;
573 while (link && link->link_num !=
574 IOINDEX_LINK(child_bridge->index))
575 link = link->next;
576 if (link == NULL)
577 printk(BIOS_ERR, "link %ld not found on %s\n",
578 IOINDEX_LINK(child_bridge->index),
579 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000580
Myles Watson894a3472010-06-09 22:41:35 +0000581 allocate_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000582 type_mask | IORESOURCE_PREFETCH,
583 type | (child_bridge->flags &
584 IORESOURCE_PREFETCH));
585 }
586 }
587}
588
Patrick Georgie1667822012-05-05 15:29:32 +0200589#if CONFIG_PCI_64BIT_PREF_MEM
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000590#define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000591#else
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000592#define MEM_MASK (IORESOURCE_MEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000593#endif
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000594
Uwe Hermanne4870472010-11-04 23:23:47 +0000595#define IO_MASK (IORESOURCE_IO)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000596#define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
Uwe Hermanne4870472010-11-04 23:23:47 +0000597#define MEM_TYPE (IORESOURCE_MEM)
598#define IO_TYPE (IORESOURCE_IO)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000599
600struct constraints {
601 struct resource pref, io, mem;
602};
603
604static void constrain_resources(struct device *dev, struct constraints* limits)
605{
606 struct device *child;
607 struct resource *res;
608 struct resource *lim;
Myles Watson894a3472010-06-09 22:41:35 +0000609 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000610
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000611 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000612
613 /* Constrain limits based on the fixed resources of this device. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000614 for (res = dev->resource_list; res; res = res->next) {
Patrick Georgi18c585b2009-08-28 12:48:02 +0000615 if (!(res->flags & IORESOURCE_FIXED))
616 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000617 if (!res->size) {
618 /* It makes no sense to have 0-sized, fixed resources.*/
Uwe Hermanne4870472010-11-04 23:23:47 +0000619 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
620 "size=0!\n", dev_path(dev), res->index);
Patrick Georgi6bd93f42009-08-19 17:29:41 +0000621 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000622 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000623
624 /* PREFETCH, MEM, or I/O - skip any others. */
625 if ((res->flags & MEM_MASK) == PREF_TYPE)
626 lim = &limits->pref;
627 else if ((res->flags & MEM_MASK) == MEM_TYPE)
628 lim = &limits->mem;
629 else if ((res->flags & IO_MASK) == IO_TYPE)
630 lim = &limits->io;
631 else
632 continue;
633
Uwe Hermanne4870472010-11-04 23:23:47 +0000634 /*
635 * Is it a fixed resource outside the current known region?
636 * If so, we don't have to consider it - it will be handled
637 * correctly and doesn't affect current region's limits.
638 */
639 if (((res->base + res->size -1) < lim->base)
640 || (res->base > lim->limit))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000641 continue;
642
Uwe Hermanne4870472010-11-04 23:23:47 +0000643 /*
644 * Choose to be above or below fixed resources. This check is
645 * signed so that "negative" amounts of space are handled
646 * correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000647 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000648 if ((signed long long)(lim->limit - (res->base + res->size -1))
649 > (signed long long)(res->base - lim->base))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000650 lim->base = res->base + res->size;
651 else
652 lim->limit = res->base -1;
653 }
654
655 /* Descend into every enabled child and look for fixed resources. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000656 for (link = dev->link_list; link; link = link->next) {
657 for (child = link->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000658 if (child->enabled)
659 constrain_resources(child, limits);
Uwe Hermanne4870472010-11-04 23:23:47 +0000660 }
661 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000662}
663
664static void avoid_fixed_resources(struct device *dev)
665{
666 struct constraints limits;
667 struct resource *res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000668
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000669 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000670
Uwe Hermanne4870472010-11-04 23:23:47 +0000671 /* Initialize constraints to maximum size. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000672 limits.pref.base = 0;
673 limits.pref.limit = 0xffffffffffffffffULL;
674 limits.io.base = 0;
675 limits.io.limit = 0xffffffffffffffffULL;
676 limits.mem.base = 0;
677 limits.mem.limit = 0xffffffffffffffffULL;
678
679 /* Constrain the limits to dev's initial resources. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000680 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000681 if ((res->flags & IORESOURCE_FIXED))
682 continue;
Patrick Georgi51615092012-03-11 19:31:03 +0100683 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
Uwe Hermanne4870472010-11-04 23:23:47 +0000684 dev_path(dev), res->index, res->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000685 if ((res->flags & MEM_MASK) == PREF_TYPE &&
686 (res->limit < limits.pref.limit))
687 limits.pref.limit = res->limit;
688 if ((res->flags & MEM_MASK) == MEM_TYPE &&
689 (res->limit < limits.mem.limit))
690 limits.mem.limit = res->limit;
691 if ((res->flags & IO_MASK) == IO_TYPE &&
692 (res->limit < limits.io.limit))
693 limits.io.limit = res->limit;
694 }
695
696 /* Look through the tree for fixed resources and update the limits. */
697 constrain_resources(dev, &limits);
698
699 /* Update dev's resources with new limits. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000700 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000701 struct resource *lim;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000702
703 if ((res->flags & IORESOURCE_FIXED))
704 continue;
705
706 /* PREFETCH, MEM, or I/O - skip any others. */
707 if ((res->flags & MEM_MASK) == PREF_TYPE)
708 lim = &limits.pref;
709 else if ((res->flags & MEM_MASK) == MEM_TYPE)
710 lim = &limits.mem;
711 else if ((res->flags & IO_MASK) == IO_TYPE)
712 lim = &limits.io;
713 else
714 continue;
715
Patrick Georgi51615092012-03-11 19:31:03 +0100716 printk(BIOS_SPEW, "%s2: %s@%02lx limit %08llx\n", __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000717 dev_path(dev), res->index, res->limit);
Patrick Georgi51615092012-03-11 19:31:03 +0100718 printk(BIOS_SPEW, "\tlim->base %08llx lim->limit %08llx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000719 lim->base, lim->limit);
720
721 /* Is the resource outside the limits? */
722 if (lim->base > res->base)
723 res->base = lim->base;
724 if (res->limit > lim->limit)
725 res->limit = lim->limit;
726 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000727}
arch import user (historical)dc811182005-07-06 17:16:09 +0000728
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000729device_t vga_pri = 0;
Myles Watsonc7233e02009-07-02 19:02:33 +0000730static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000731{
Uwe Hermann312673c2009-10-27 21:49:33 +0000732 /*
Uwe Hermanne4870472010-11-04 23:23:47 +0000733 * FIXME: Modify set_vga_bridge() so it is less PCI centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000734 * This function knows too much about PCI stuff, it should be just
735 * an iterator/visitor.
736 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000737
Myles Watson29cc9ed2009-07-02 18:56:24 +0000738 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200739 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000740 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000741
Eric Biedermanb78c1972004-10-14 20:54:17 +0000742 bus = 0;
743 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000744 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000745
Patrick Georgi557ecf22012-07-20 12:16:17 +0200746 dev = NULL;
747 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000748 if (!dev->enabled)
749 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000750
Patrick Georgi557ecf22012-07-20 12:16:17 +0200751 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000752
Patrick Georgi557ecf22012-07-20 12:16:17 +0200753 if (dev->on_mainboard) {
754 vga_onboard = dev;
Kostr1f0d3792012-10-06 13:27:58 +0400755 } else {
Patrick Georgi557ecf22012-07-20 12:16:17 +0200756 vga = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000757 }
Myles Watson032a9652009-05-11 22:24:53 +0000758
Patrick Georgi557ecf22012-07-20 12:16:17 +0200759 /* It isn't safe to enable all VGA cards. */
760 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200761 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000762
Uwe Hermanne4870472010-11-04 23:23:47 +0000763 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000764 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200765
766 if (CONFIG_ONBOARD_VGA_IS_PRIMARY && vga_onboard)
767 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000768
Patrick Georgi5869fa22012-07-20 12:29:33 +0200769 /* If we prefer plugin VGA over chipset VGA, the chipset might
770 want to know. */
771 if (!CONFIG_ONBOARD_VGA_IS_PRIMARY && (vga != vga_onboard) &&
772 vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) {
773 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
774 vga_onboard->ops->disable(vga_onboard);
775 }
776
arch import user (historical)dc811182005-07-06 17:16:09 +0000777 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000778 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000779 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000780 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000781 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
782 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000783 bus = vga->bus;
784 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000785
Myles Watson29cc9ed2009-07-02 18:56:24 +0000786 /* Now walk up the bridges setting the VGA enable. */
787 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000788 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000789 dev_path(bus->dev));
Li-Ta Lodb7f47c2004-01-08 21:15:49 +0000790 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000791 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000792 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000793}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000794
Li-Ta Lo04930692004-11-25 17:37:19 +0000795/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000796 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000797 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000798 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000799 * resources to hardware. For bridge devices, the set_resources() method
800 * has to recurse into every down stream buses.
801 *
802 * Mutual recursion:
803 * assign_resources() -> device_operation::set_resources()
804 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000805 *
806 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000807 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000808void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000809{
810 struct device *curdev;
811
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000812 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000813 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000814
Myles Watson29cc9ed2009-07-02 18:56:24 +0000815 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000816 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000817 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000818
Eric Biedermane9a271e32003-09-02 03:36:25 +0000819 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000820 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000821 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000822 continue;
823 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000824 curdev->ops->set_resources(curdev);
825 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000826 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000827 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000828}
829
Li-Ta Lo5782d272004-04-26 17:51:20 +0000830/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000831 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000832 *
833 * Enable resources of the device by calling the device specific
834 * enable_resources() method.
835 *
836 * The parent's resources should be enabled first to avoid having enabling
837 * order problem. This is done by calling the parent's enable_resources()
Myles Watson7eac4452010-06-17 16:16:56 +0000838 * method before its childrens' enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000839 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000840 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000841 */
Myles Watson7eac4452010-06-17 16:16:56 +0000842static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000843{
Myles Watson7eac4452010-06-17 16:16:56 +0000844 struct device *dev;
845 struct bus *c_link;
846
847 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000848 if (dev->enabled && dev->ops && dev->ops->enable_resources)
Myles Watson7eac4452010-06-17 16:16:56 +0000849 dev->ops->enable_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000850 }
Myles Watson7eac4452010-06-17 16:16:56 +0000851
852 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000853 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000854 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000855 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000856}
857
Myles Watson032a9652009-05-11 22:24:53 +0000858/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000859 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
860 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000861 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000862 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000863 */
864int reset_bus(struct bus *bus)
865{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000866 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000867 bus->dev->ops->reset_bus(bus);
868 bus->reset_needed = 0;
869 return 1;
870 }
871 return 0;
872}
873
Myles Watson032a9652009-05-11 22:24:53 +0000874/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000875 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000876 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000877 * If there are bridges on the bus, recursively scan the buses behind the
878 * bridges. If the setting up and tuning of the bus causes a reset to be
879 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000880 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000881 * @param busdev Pointer to the bus device.
882 * @param max Current bus number.
883 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000884 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000885unsigned int scan_bus(struct device *busdev, unsigned int max)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000886{
887 unsigned int new_max;
888 int do_scan_bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000889
Myles Watson29cc9ed2009-07-02 18:56:24 +0000890 if (!busdev || !busdev->enabled || !busdev->ops ||
891 !busdev->ops->scan_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000892 return max;
893 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000894
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000895 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000896 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000897 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000898 new_max = busdev->ops->scan_bus(busdev, max);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000899 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000900 for (link = busdev->link_list; link; link = link->next) {
901 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000902 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000903 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000904 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000905 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000906 }
907 }
908 }
909 return new_max;
910}
911
Li-Ta Loe5266692004-03-23 21:28:05 +0000912/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000913 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000914 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000915 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000916 * file. The device structures for these devices are generated at compile
917 * time by the config tool and are organized into the device tree. This
918 * function determines if the devices created at compile time actually exist
919 * in the physical system.
920 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000921 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000922 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000923 * device tree.
924 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000925 * This function starts from the root device 'dev_root', scans the buses in
926 * the system recursively, and modifies the device tree according to the
927 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000928 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000929 * This function has no idea how to scan and probe buses and devices at all.
930 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000931 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000932 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000933 */
934void dev_enumerate(void)
935{
936 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000937
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000938 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000939
Eric Biederman8ca8d762003-04-22 19:02:15 +0000940 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000941
Uwe Hermanne4870472010-11-04 23:23:47 +0000942 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000943 printk(BIOS_SPEW, "Compare with tree...\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000944 show_devs_tree(root, BIOS_SPEW, 0, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000945
Stefan Reinauera675d492012-08-07 14:50:47 -0700946 if (root->chip_ops && root->chip_ops->enable_dev)
947 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000948
Eric Biedermanb78c1972004-10-14 20:54:17 +0000949 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000950 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000951 return;
952 }
Stefan Reinauer6afcea82009-07-18 17:58:44 +0000953 scan_bus(root, 0);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000954 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000955}
956
Li-Ta Loe5266692004-03-23 21:28:05 +0000957/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000958 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +0000959 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000960 * Starting at the root of the device tree, travel it recursively in two
961 * passes. In the first pass, we compute and allocate resources (ranges)
962 * requried by each device. In the second pass, the resources ranges are
963 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000964 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000965 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000966 *
967 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +0000968 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000969 */
970void dev_configure(void)
971{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000972 struct resource *res;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000973 struct device *root;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000974 struct device *child;
Li-Ta Loe5266692004-03-23 21:28:05 +0000975
Marc Jones80bd74c2012-02-21 17:44:35 +0100976 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +0100977
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000978 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000979
Eric Biedermanb78c1972004-10-14 20:54:17 +0000980 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000981
Uwe Hermanne4870472010-11-04 23:23:47 +0000982 /*
983 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +0000984 * space for IO, MEM, and PREFMEM resources in the domain. The
985 * allocation of device resources will be done from this address space.
986 */
Myles Watsoncd5d7562009-05-12 13:43:34 +0000987
Myles Watson29cc9ed2009-07-02 18:56:24 +0000988 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +0000989
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000990 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +0000991 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000992 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000993
Stefan Reinauer39e72292009-10-26 16:47:05 +0000994 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +0000995
Myles Watson29cc9ed2009-07-02 18:56:24 +0000996 /* Compute resources for all domains. */
Myles Watson894a3472010-06-09 22:41:35 +0000997 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800998 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000999 continue;
Myles Watsonc25cc112010-05-21 14:33:48 +00001000 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001001 if (res->flags & IORESOURCE_FIXED)
1002 continue;
1003 if (res->flags & IORESOURCE_PREFETCH) {
Myles Watson894a3472010-06-09 22:41:35 +00001004 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001005 res, MEM_MASK, PREF_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001006 continue;
1007 }
1008 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001009 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001010 res, MEM_MASK, MEM_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001011 continue;
1012 }
1013 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001014 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001015 res, IO_MASK, IO_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001016 continue;
1017 }
1018 }
1019 }
1020
1021 /* For all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001022 for (child = root->link_list->children; child; child=child->sibling)
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001023 if (child->path.type == DEVICE_PATH_DOMAIN)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001024 avoid_fixed_resources(child);
1025
Uwe Hermanne4870472010-11-04 23:23:47 +00001026 /*
1027 * Now we need to adjust the resources. MEM resources need to start at
Myles Watson29cc9ed2009-07-02 18:56:24 +00001028 * the highest address managable.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001029 */
Myles Watson894a3472010-06-09 22:41:35 +00001030 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001031 if (child->path.type != DEVICE_PATH_DOMAIN)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001032 continue;
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_MEM) ||
1035 res->flags & IORESOURCE_FIXED)
1036 continue;
1037 res->base = resource_max(res);
1038 }
1039 }
Eric Biederman5cd81732004-03-11 15:01:31 +00001040
Eric Biedermanb78c1972004-10-14 20:54:17 +00001041 /* Store the computed resource allocations into device registers ... */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001042 printk(BIOS_INFO, "Setting resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001043 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001044 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001045 continue;
Myles Watsonc25cc112010-05-21 14:33:48 +00001046 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001047 if (res->flags & IORESOURCE_FIXED)
1048 continue;
1049 if (res->flags & IORESOURCE_PREFETCH) {
Myles Watson894a3472010-06-09 22:41:35 +00001050 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001051 res, MEM_MASK, PREF_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001052 continue;
1053 }
1054 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001055 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001056 res, MEM_MASK, MEM_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001057 continue;
1058 }
1059 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001060 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001061 res, IO_MASK, IO_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001062 continue;
1063 }
1064 }
1065 }
Myles Watson894a3472010-06-09 22:41:35 +00001066 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001067 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001068 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +00001069
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001070 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001071}
1072
Li-Ta Loe5266692004-03-23 21:28:05 +00001073/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001074 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +00001075 *
1076 * Starting at the root, walk the tree and enable all devices/bridges by
1077 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001078 */
1079void dev_enable(void)
1080{
Myles Watson7eac4452010-06-17 16:16:56 +00001081 struct bus *link;
1082
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001083 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001084
Uwe Hermanne4870472010-11-04 23:23:47 +00001085 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001086 for (link = dev_root.link_list; link; link = link->next)
1087 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +00001088
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001089 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001090}
1091
Li-Ta Loe5266692004-03-23 21:28:05 +00001092/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001093 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +00001094 *
Uwe Hermanne4870472010-11-04 23:23:47 +00001095 * The parent should be initialized first to avoid having an ordering problem.
1096 * This is done by calling the parent's init() method before its childrens'
1097 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +00001098 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001099 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +00001100 */
1101static void init_dev(struct device *dev)
1102{
Uwe Hermanne4870472010-11-04 23:23:47 +00001103 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +00001104 return;
Myles Watson7eac4452010-06-17 16:16:56 +00001105
1106 if (!dev->initialized && dev->ops && dev->ops->init) {
Aaron Durbin05294292013-04-30 15:41:13 -05001107#if CONFIG_HAVE_MONOTONIC_TIMER
1108 struct mono_time start_time;
1109 struct rela_time dev_init_time;
1110
1111 timer_monotonic_get(&start_time);
1112#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001113 if (dev->path.type == DEVICE_PATH_I2C) {
1114 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1115 dev_path(dev->bus->dev), dev->bus->link_num);
1116 }
1117
1118 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1119 dev->initialized = 1;
1120 dev->ops->init(dev);
Aaron Durbin05294292013-04-30 15:41:13 -05001121#if CONFIG_HAVE_MONOTONIC_TIMER
1122 dev_init_time = current_time_from(&start_time);
1123 printk(BIOS_DEBUG, "%s init %ld usecs\n", dev_path(dev),
1124 rela_time_in_microseconds(&dev_init_time));
1125#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001126 }
1127}
1128
1129static void init_link(struct bus *link)
1130{
1131 struct device *dev;
1132 struct bus *c_link;
1133
Uwe Hermanne4870472010-11-04 23:23:47 +00001134 for (dev = link->children; dev; dev = dev->sibling)
Myles Watson7eac4452010-06-17 16:16:56 +00001135 init_dev(dev);
Myles Watson7eac4452010-06-17 16:16:56 +00001136
1137 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001138 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +00001139 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +00001140 }
1141}
1142
1143/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001144 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +00001145 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001146 * Starting at the root device, call the device's init() method to do
1147 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001148 */
1149void dev_initialize(void)
1150{
Myles Watson7eac4452010-06-17 16:16:56 +00001151 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001152
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001153 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +00001154
Duncan Laurieb4aaaa72012-01-17 09:03:11 -08001155#if CONFIG_ARCH_X86
1156 /* Ensure EBDA is prepared before Option ROMs. */
1157 setup_default_ebda();
1158#endif
1159
Myles Watson1bd3fb72010-08-16 16:25:23 +00001160 /* First call the mainboard init. */
1161 init_dev(&dev_root);
1162
Uwe Hermanne4870472010-11-04 23:23:47 +00001163 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001164 for (link = dev_root.link_list; link; link = link->next)
1165 init_link(link);
1166
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001167 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001168 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001169}