blob: 92a4447a89339fe3b43f11b87128a6c46b8148ca [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>
36#include <bitops.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000037#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000038#include <device/device.h>
39#include <device/pci.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000040#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000041#include <stdlib.h>
42#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000043#include <smp/spinlock.h>
Duncan Laurieb4aaaa72012-01-17 09:03:11 -080044#if CONFIG_ARCH_X86
45#include <arch/ebda.h>
46#endif
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
Uwe Hermannc1ee4292010-10-17 19:01:48 +000055DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000056
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +030057
58/* IGD UMA memory */
59uint64_t uma_memory_base = 0;
60uint64_t uma_memory_size = 0;
61
Li-Ta Loe5266692004-03-23 21:28:05 +000062/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000063 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +000064 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000065 * Allocte a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +000066 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +000067 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000068 * @param parent Parent bus the newly created device should be attached to.
69 * @param path Path to the device to be created.
70 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +000071 *
72 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +000073 */
Kyösti Mälkkia5650a42012-07-07 17:15:51 +030074static device_t __alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +000075{
Eric Biedermane9a271e32003-09-02 03:36:25 +000076 device_t dev, child;
Li-Ta Loe5266692004-03-23 21:28:05 +000077
Myles Watson29cc9ed2009-07-02 18:56:24 +000078 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +000079 for (child = parent->children; child && child->sibling; /* */ )
Eric Biedermane9a271e32003-09-02 03:36:25 +000080 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +000081
Eric Biedermane9a271e32003-09-02 03:36:25 +000082 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +000083 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +000084 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +000085
Eric Biedermane9a271e32003-09-02 03:36:25 +000086 memset(dev, 0, sizeof(*dev));
87 memcpy(&dev->path, path, sizeof(*path));
88
Myles Watson29cc9ed2009-07-02 18:56:24 +000089 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +000090 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +000091
Eric Biedermanb78c1972004-10-14 20:54:17 +000092 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +000093 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +000094 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +000095 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +000096 else
Eric Biedermane9a271e32003-09-02 03:36:25 +000097 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +000098
Eric Biederman03acab62004-10-14 21:25:53 +000099 /* Append a new device to the global device list.
100 * The list is used to find devices once everything is set up.
101 */
Myles Watson70679a02010-09-01 21:03:03 +0000102 last_dev->next = dev;
103 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000104
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300105 return dev;
106}
107
108device_t alloc_dev(struct bus *parent, struct device_path *path)
109{
110 device_t dev;
111 spin_lock(&dev_lock);
112 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000113 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000114 return dev;
115}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000116
Li-Ta Loe5266692004-03-23 21:28:05 +0000117/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300118 * See if a device structure already exists and if not allocate it.
119 *
120 * @param parent The bus to find the device on.
121 * @param path The relative path from the bus to the appropriate device.
122 * @return Pointer to a device structure for the device on bus at path.
123 */
124device_t alloc_find_dev(struct bus *parent, struct device_path *path)
125{
126 device_t child;
127 spin_lock(&dev_lock);
128 child = find_dev_path(parent, path);
129 if (!child)
130 child = __alloc_dev(parent, path);
131 spin_unlock(&dev_lock);
132 return child;
133}
134
135/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000136 * Round a number up to an alignment.
137 *
138 * @param val The starting value.
139 * @param roundup Alignment as a power of two.
140 * @return Rounded up number.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000141 */
Eric Biederman448bd632004-10-14 22:52:15 +0000142static resource_t round(resource_t val, unsigned long pow)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000143{
Eric Biederman448bd632004-10-14 22:52:15 +0000144 resource_t mask;
145 mask = (1ULL << pow) - 1ULL;
146 val += mask;
147 val &= ~mask;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000148 return val;
149}
150
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000151/**
152 * Read the resources on all devices of a given bus.
153 *
154 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000155 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000156static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000157{
158 struct device *curdev;
159
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000160 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
161 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000162
Myles Watson29cc9ed2009-07-02 18:56:24 +0000163 /* Walk through all devices and find which resources they need. */
164 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000165 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000166
167 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000169
Eric Biedermane9a271e32003-09-02 03:36:25 +0000170 if (!curdev->ops || !curdev->ops->read_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000171 printk(BIOS_ERR, "%s missing read_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000172 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000173 continue;
174 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000175 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000176
177 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000178 for (link = curdev->link_list; link; link = link->next)
179 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000181 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000182 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183}
184
Eric Biedermane9a271e32003-09-02 03:36:25 +0000185struct pick_largest_state {
186 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000187 struct device *result_dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000188 struct resource *result;
189 int seen_last;
190};
191
Myles Watson29cc9ed2009-07-02 18:56:24 +0000192static void pick_largest_resource(void *gp, struct device *dev,
193 struct resource *resource)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000194{
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000195 struct pick_largest_state *state = gp;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000196 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000197
Eric Biedermane9a271e32003-09-02 03:36:25 +0000198 last = state->last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000199
200 /* Be certain to pick the successor to last. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000201 if (resource == last) {
202 state->seen_last = 1;
203 return;
204 }
Myles Watson032a9652009-05-11 22:24:53 +0000205 if (resource->flags & IORESOURCE_FIXED)
Uwe Hermanne4870472010-11-04 23:23:47 +0000206 return; /* Skip it. */
Myles Watson032a9652009-05-11 22:24:53 +0000207 if (last && ((last->align < resource->align) ||
208 ((last->align == resource->align) &&
209 (last->size < resource->size)) ||
210 ((last->align == resource->align) &&
211 (last->size == resource->size) && (!state->seen_last)))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000212 return;
213 }
Myles Watson032a9652009-05-11 22:24:53 +0000214 if (!state->result ||
215 (state->result->align < resource->align) ||
216 ((state->result->align == resource->align) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000217 (state->result->size < resource->size))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000218 state->result_dev = dev;
219 state->result = resource;
Myles Watson032a9652009-05-11 22:24:53 +0000220 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000221}
222
Myles Watson29cc9ed2009-07-02 18:56:24 +0000223static struct device *largest_resource(struct bus *bus,
224 struct resource **result_res,
225 unsigned long type_mask,
226 unsigned long type)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000227{
228 struct pick_largest_state state;
229
230 state.last = *result_res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000231 state.result_dev = NULL;
232 state.result = NULL;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000233 state.seen_last = 0;
234
Myles Watson032a9652009-05-11 22:24:53 +0000235 search_bus_resources(bus, type_mask, type, pick_largest_resource,
236 &state);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000237
238 *result_res = state.result;
239 return state.result_dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000240}
241
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000242/**
Uwe Hermanne4870472010-11-04 23:23:47 +0000243 * This function is the guts of the resource allocator.
Myles Watson032a9652009-05-11 22:24:53 +0000244 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000245 * The problem.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000246 * - Allocate resource locations for every device.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000247 * - Don't overlap, and follow the rules of bridges.
248 * - Don't overlap with resources in fixed locations.
249 * - Be efficient so we don't have ugly strategies.
250 *
251 * The strategy.
252 * - Devices that have fixed addresses are the minority so don't
Myles Watson29cc9ed2009-07-02 18:56:24 +0000253 * worry about them too much. Instead only use part of the address
254 * space for devices with programmable addresses. This easily handles
Eric Biederman8ca8d762003-04-22 19:02:15 +0000255 * everything except bridges.
256 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000257 * - PCI devices are required to have their sizes and their alignments
258 * equal. In this case an optimal solution to the packing problem
259 * exists. Allocate all devices from highest alignment to least
260 * alignment or vice versa. Use this.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000261 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000262 * - So we can handle more than PCI run two allocation passes on bridges. The
263 * first to see how large the resources are behind the bridge, and what
264 * their alignment requirements are. The second to assign a safe address to
265 * the devices behind the bridge. This allows us to treat a bridge as just
266 * a device with a couple of resources, and not need to special case it in
267 * the allocator. Also this allows handling of other types of bridges.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000268 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000269 * @param bus The bus we are traversing.
270 * @param bridge The bridge resource which must contain the bus' resources.
271 * @param type_mask This value gets ANDed with the resource type.
272 * @param type This value must match the result of the AND.
273 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000274 */
Myles Watson54913b92009-10-13 20:00:09 +0000275static void compute_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000276 unsigned long type_mask, unsigned long type)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000277{
278 struct device *dev;
279 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000280 resource_t base;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000281 base = round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000282
Uwe Hermanne4870472010-11-04 23:23:47 +0000283 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
284 " limit: %llx\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000285 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
Uwe Hermanne4870472010-11-04 23:23:47 +0000286 "prefmem" : "mem", base, bridge->size, bridge->align,
287 bridge->gran, bridge->limit);
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000288
Uwe Hermanne4870472010-11-04 23:23:47 +0000289 /* For each child which is a bridge, compute the resource needs. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000290 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000291 struct resource *child_bridge;
292
Myles Watson894a3472010-06-09 22:41:35 +0000293 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000294 continue;
295
296 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000297 for (child_bridge = dev->resource_list; child_bridge;
298 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000299 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000300
Uwe Hermanne4870472010-11-04 23:23:47 +0000301 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
302 || (child_bridge->flags & type_mask) != type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000303 continue;
304
Uwe Hermanne4870472010-11-04 23:23:47 +0000305 /*
306 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000307 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000308 * and non-prefetchable memory. Bridges below them need
309 * it separated. Add the PREFETCH flag to the type_mask
310 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000311 */
Myles Watson894a3472010-06-09 22:41:35 +0000312 link = dev->link_list;
313 while (link && link->link_num !=
314 IOINDEX_LINK(child_bridge->index))
315 link = link->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000316
317 if (link == NULL) {
Myles Watson894a3472010-06-09 22:41:35 +0000318 printk(BIOS_ERR, "link %ld not found on %s\n",
319 IOINDEX_LINK(child_bridge->index),
320 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000321 }
322
Myles Watson894a3472010-06-09 22:41:35 +0000323 compute_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000324 type_mask | IORESOURCE_PREFETCH,
325 type | (child_bridge->flags &
326 IORESOURCE_PREFETCH));
327 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000328 }
329
Myles Watson29cc9ed2009-07-02 18:56:24 +0000330 /* Remember we haven't found anything yet. */
331 resource = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000332
Uwe Hermanne4870472010-11-04 23:23:47 +0000333 /*
334 * Walk through all the resources on the current bus and compute the
335 * amount of address space taken by them. Take granularity and
Myles Watson29cc9ed2009-07-02 18:56:24 +0000336 * alignment into account.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000337 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000338 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000339
Myles Watson29cc9ed2009-07-02 18:56:24 +0000340 /* Size 0 resources can be skipped. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000341 if (!resource->size)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000342 continue;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000343
Myles Watson29cc9ed2009-07-02 18:56:24 +0000344 /* Propagate the resource alignment to the bridge resource. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000345 if (resource->align > bridge->align)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000346 bridge->align = resource->align;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000347
348 /* Propagate the resource limit to the bridge register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000349 if (bridge->limit > resource->limit)
Eric Biedermandbec2d42004-10-21 10:44:08 +0000350 bridge->limit = resource->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000351
352 /* Warn if it looks like APICs aren't declared. */
353 if ((resource->limit == 0xffffffff) &&
354 (resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000355 printk(BIOS_ERR,
356 "Resource limit looks wrong! (no APIC?)\n");
Patrick Georgi51615092012-03-11 19:31:03 +0100357 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000358 dev_path(dev), resource->index, resource->limit);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000359 }
Stefan Reinauer51754a32008-08-01 12:28:38 +0000360
Eric Biederman8ca8d762003-04-22 19:02:15 +0000361 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000362 /*
363 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000364 * expansion card addresses. The legacy PCI decodes
365 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
366 * 0x00 - 0xff can be used out of each 0x400 block of
367 * I/O space.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000368 */
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000369 if ((base & 0x300) != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000370 base = (base & ~0x3ff) + 0x400;
371 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000372 /*
373 * Don't allow allocations in the VGA I/O range.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000374 * PCI has special cases for that.
375 */
376 else if ((base >= 0x3b0) && (base <= 0x3df)) {
377 base = 0x3e0;
378 }
379 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000380 /* Base must be aligned. */
381 base = round(base, resource->align);
382 resource->base = base;
383 base += resource->size;
Myles Watson032a9652009-05-11 22:24:53 +0000384
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000385 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000386 dev_path(dev), resource->index, resource->base,
387 resource->base + resource->size - 1,
388 (resource->flags & IORESOURCE_IO) ? "io" :
389 (resource->flags & IORESOURCE_PREFETCH) ?
390 "prefmem" : "mem");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000391 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000392
393 /*
394 * A PCI bridge resource does not need to be a power of two size, but
395 * it does have a minimum granularity. Round the size up to that
396 * minimum granularity so we know not to place something else at an
397 * address postitively decoded by the bridge.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000398 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000399 bridge->size = round(base, bridge->gran) -
400 round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000401
Uwe Hermanne4870472010-11-04 23:23:47 +0000402 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
403 " limit: %llx done\n", dev_path(bus->dev), __func__,
404 (bridge->flags & IORESOURCE_IO) ? "io" :
405 (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
406 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000407}
408
409/**
410 * This function is the second part of the resource allocator.
411 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000412 * See the compute_resources function for a more detailed explanation.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000413 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000414 * This function assigns the resources a value.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000415 *
416 * @param bus The bus we are traversing.
417 * @param bridge The bridge resource which must contain the bus' resources.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000418 * @param type_mask This value gets ANDed with the resource type.
419 * @param type This value must match the result of the AND.
Uwe Hermanne4870472010-11-04 23:23:47 +0000420 *
421 * @see compute_resources
Myles Watson29cc9ed2009-07-02 18:56:24 +0000422 */
Myles Watson54913b92009-10-13 20:00:09 +0000423static void allocate_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000424 unsigned long type_mask, unsigned long type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000425{
426 struct device *dev;
427 struct resource *resource;
428 resource_t base;
429 base = bridge->base;
430
Uwe Hermanne4870472010-11-04 23:23:47 +0000431 printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d "
432 "limit:%llx\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000433 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
434 "prefmem" : "mem",
435 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
436
437 /* Remember we haven't found anything yet. */
438 resource = NULL;
439
Uwe Hermanne4870472010-11-04 23:23:47 +0000440 /*
441 * Walk through all the resources on the current bus and allocate them
Myles Watson29cc9ed2009-07-02 18:56:24 +0000442 * address space.
443 */
444 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
445
446 /* Propagate the bridge limit to the resource register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000447 if (resource->limit > bridge->limit)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000448 resource->limit = bridge->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000449
450 /* Size 0 resources can be skipped. */
451 if (!resource->size) {
452 /* Set the base to limit so it doesn't confuse tolm. */
453 resource->base = resource->limit;
454 resource->flags |= IORESOURCE_ASSIGNED;
455 continue;
456 }
457
458 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000459 /*
460 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000461 * expansion card addresses. The legacy PCI decodes
462 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
463 * 0x00 - 0xff can be used out of each 0x400 block of
464 * I/O space.
465 */
466 if ((base & 0x300) != 0) {
467 base = (base & ~0x3ff) + 0x400;
468 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000469 /*
470 * Don't allow allocations in the VGA I/O range.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000471 * PCI has special cases for that.
472 */
473 else if ((base >= 0x3b0) && (base <= 0x3df)) {
474 base = 0x3e0;
475 }
476 }
477
478 if ((round(base, resource->align) + resource->size - 1) <=
479 resource->limit) {
480 /* Base must be aligned. */
481 base = round(base, resource->align);
482 resource->base = base;
483 resource->flags |= IORESOURCE_ASSIGNED;
484 resource->flags &= ~IORESOURCE_STORED;
485 base += resource->size;
486 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000487 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000488 printk(BIOS_ERR, " aligned base %llx size %llx "
489 "limit %llx\n", round(base, resource->align),
490 resource->size, resource->limit);
491 printk(BIOS_ERR, " %llx needs to be <= %llx "
492 "(limit)\n", (round(base, resource->align) +
Myles Watson29cc9ed2009-07-02 18:56:24 +0000493 resource->size) - 1, resource->limit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000494 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
495 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
496 ? "Assigned: " : "", dev_path(dev),
497 resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000498 resource->base + resource->size - 1,
Uwe Hermanne4870472010-11-04 23:23:47 +0000499 (resource->flags & IORESOURCE_IO) ? "io"
500 : (resource->flags & IORESOURCE_PREFETCH)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000501 ? "prefmem" : "mem");
502 }
503
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000504 printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000505 (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
Uwe Hermanne4870472010-11-04 23:23:47 +0000506 : "", dev_path(dev), resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000507 resource->size ? resource->base + resource->size - 1 :
Uwe Hermanne4870472010-11-04 23:23:47 +0000508 resource->base, (resource->flags & IORESOURCE_IO)
509 ? "io" : (resource->flags & IORESOURCE_PREFETCH)
510 ? "prefmem" : "mem");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000511 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000512
513 /*
514 * A PCI bridge resource does not need to be a power of two size, but
Myles Watson29cc9ed2009-07-02 18:56:24 +0000515 * it does have a minimum granularity. Round the size up to that
516 * minimum granularity so we know not to place something else at an
517 * address positively decoded by the bridge.
518 */
519
520 bridge->flags |= IORESOURCE_ASSIGNED;
521
Uwe Hermanne4870472010-11-04 23:23:47 +0000522 printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d "
523 "gran: %d done\n", dev_path(bus->dev), __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000524 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
Uwe Hermanne4870472010-11-04 23:23:47 +0000525 "prefmem" : "mem", base, bridge->size, bridge->align,
526 bridge->gran);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000527
528 /* For each child which is a bridge, allocate_resources. */
529 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000530 struct resource *child_bridge;
531
Myles Watson894a3472010-06-09 22:41:35 +0000532 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000533 continue;
534
535 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000536 for (child_bridge = dev->resource_list; child_bridge;
537 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000538 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000539
540 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
541 (child_bridge->flags & type_mask) != type)
542 continue;
543
Uwe Hermanne4870472010-11-04 23:23:47 +0000544 /*
545 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000546 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000547 * and non-prefetchable memory. Bridges below them need
548 * it separated. Add the PREFETCH flag to the type_mask
549 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000550 */
Myles Watson894a3472010-06-09 22:41:35 +0000551 link = dev->link_list;
552 while (link && link->link_num !=
553 IOINDEX_LINK(child_bridge->index))
554 link = link->next;
555 if (link == NULL)
556 printk(BIOS_ERR, "link %ld not found on %s\n",
557 IOINDEX_LINK(child_bridge->index),
558 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000559
Myles Watson894a3472010-06-09 22:41:35 +0000560 allocate_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000561 type_mask | IORESOURCE_PREFETCH,
562 type | (child_bridge->flags &
563 IORESOURCE_PREFETCH));
564 }
565 }
566}
567
Patrick Georgie1667822012-05-05 15:29:32 +0200568#if CONFIG_PCI_64BIT_PREF_MEM
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000569#define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000570#else
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000571#define MEM_MASK (IORESOURCE_MEM)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000572#endif
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000573
Uwe Hermanne4870472010-11-04 23:23:47 +0000574#define IO_MASK (IORESOURCE_IO)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000575#define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
Uwe Hermanne4870472010-11-04 23:23:47 +0000576#define MEM_TYPE (IORESOURCE_MEM)
577#define IO_TYPE (IORESOURCE_IO)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000578
579struct constraints {
580 struct resource pref, io, mem;
581};
582
583static void constrain_resources(struct device *dev, struct constraints* limits)
584{
585 struct device *child;
586 struct resource *res;
587 struct resource *lim;
Myles Watson894a3472010-06-09 22:41:35 +0000588 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000589
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000590 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000591
592 /* Constrain limits based on the fixed resources of this device. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000593 for (res = dev->resource_list; res; res = res->next) {
Patrick Georgi18c585b2009-08-28 12:48:02 +0000594 if (!(res->flags & IORESOURCE_FIXED))
595 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000596 if (!res->size) {
597 /* It makes no sense to have 0-sized, fixed resources.*/
Uwe Hermanne4870472010-11-04 23:23:47 +0000598 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
599 "size=0!\n", dev_path(dev), res->index);
Patrick Georgi6bd93f42009-08-19 17:29:41 +0000600 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000601 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000602
603 /* PREFETCH, MEM, or I/O - skip any others. */
604 if ((res->flags & MEM_MASK) == PREF_TYPE)
605 lim = &limits->pref;
606 else if ((res->flags & MEM_MASK) == MEM_TYPE)
607 lim = &limits->mem;
608 else if ((res->flags & IO_MASK) == IO_TYPE)
609 lim = &limits->io;
610 else
611 continue;
612
Uwe Hermanne4870472010-11-04 23:23:47 +0000613 /*
614 * Is it a fixed resource outside the current known region?
615 * If so, we don't have to consider it - it will be handled
616 * correctly and doesn't affect current region's limits.
617 */
618 if (((res->base + res->size -1) < lim->base)
619 || (res->base > lim->limit))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000620 continue;
621
Uwe Hermanne4870472010-11-04 23:23:47 +0000622 /*
623 * Choose to be above or below fixed resources. This check is
624 * signed so that "negative" amounts of space are handled
625 * correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000626 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000627 if ((signed long long)(lim->limit - (res->base + res->size -1))
628 > (signed long long)(res->base - lim->base))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000629 lim->base = res->base + res->size;
630 else
631 lim->limit = res->base -1;
632 }
633
634 /* Descend into every enabled child and look for fixed resources. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000635 for (link = dev->link_list; link; link = link->next) {
636 for (child = link->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000637 if (child->enabled)
638 constrain_resources(child, limits);
Uwe Hermanne4870472010-11-04 23:23:47 +0000639 }
640 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000641}
642
643static void avoid_fixed_resources(struct device *dev)
644{
645 struct constraints limits;
646 struct resource *res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000647
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000648 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000649
Uwe Hermanne4870472010-11-04 23:23:47 +0000650 /* Initialize constraints to maximum size. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000651 limits.pref.base = 0;
652 limits.pref.limit = 0xffffffffffffffffULL;
653 limits.io.base = 0;
654 limits.io.limit = 0xffffffffffffffffULL;
655 limits.mem.base = 0;
656 limits.mem.limit = 0xffffffffffffffffULL;
657
658 /* Constrain the limits to dev's initial resources. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000659 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000660 if ((res->flags & IORESOURCE_FIXED))
661 continue;
Patrick Georgi51615092012-03-11 19:31:03 +0100662 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
Uwe Hermanne4870472010-11-04 23:23:47 +0000663 dev_path(dev), res->index, res->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000664 if ((res->flags & MEM_MASK) == PREF_TYPE &&
665 (res->limit < limits.pref.limit))
666 limits.pref.limit = res->limit;
667 if ((res->flags & MEM_MASK) == MEM_TYPE &&
668 (res->limit < limits.mem.limit))
669 limits.mem.limit = res->limit;
670 if ((res->flags & IO_MASK) == IO_TYPE &&
671 (res->limit < limits.io.limit))
672 limits.io.limit = res->limit;
673 }
674
675 /* Look through the tree for fixed resources and update the limits. */
676 constrain_resources(dev, &limits);
677
678 /* Update dev's resources with new limits. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000679 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000680 struct resource *lim;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000681
682 if ((res->flags & IORESOURCE_FIXED))
683 continue;
684
685 /* PREFETCH, MEM, or I/O - skip any others. */
686 if ((res->flags & MEM_MASK) == PREF_TYPE)
687 lim = &limits.pref;
688 else if ((res->flags & MEM_MASK) == MEM_TYPE)
689 lim = &limits.mem;
690 else if ((res->flags & IO_MASK) == IO_TYPE)
691 lim = &limits.io;
692 else
693 continue;
694
Patrick Georgi51615092012-03-11 19:31:03 +0100695 printk(BIOS_SPEW, "%s2: %s@%02lx limit %08llx\n", __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000696 dev_path(dev), res->index, res->limit);
Patrick Georgi51615092012-03-11 19:31:03 +0100697 printk(BIOS_SPEW, "\tlim->base %08llx lim->limit %08llx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000698 lim->base, lim->limit);
699
700 /* Is the resource outside the limits? */
701 if (lim->base > res->base)
702 res->base = lim->base;
703 if (res->limit > lim->limit)
704 res->limit = lim->limit;
705 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000706}
arch import user (historical)dc811182005-07-06 17:16:09 +0000707
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000708device_t vga_pri = 0;
Myles Watsonc7233e02009-07-02 19:02:33 +0000709static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000710{
Uwe Hermann312673c2009-10-27 21:49:33 +0000711 /*
Uwe Hermanne4870472010-11-04 23:23:47 +0000712 * FIXME: Modify set_vga_bridge() so it is less PCI centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000713 * This function knows too much about PCI stuff, it should be just
714 * an iterator/visitor.
715 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000716
Myles Watson29cc9ed2009-07-02 18:56:24 +0000717 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200718 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000719 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000720
Eric Biedermanb78c1972004-10-14 20:54:17 +0000721 bus = 0;
722 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000723 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000724
Patrick Georgi557ecf22012-07-20 12:16:17 +0200725 dev = NULL;
726 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000727 if (!dev->enabled)
728 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000729
Patrick Georgi557ecf22012-07-20 12:16:17 +0200730 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000731
Patrick Georgi557ecf22012-07-20 12:16:17 +0200732 if (dev->on_mainboard) {
733 vga_onboard = dev;
734 } else if (!vga) {
735 vga = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000736 }
Myles Watson032a9652009-05-11 22:24:53 +0000737
Patrick Georgi557ecf22012-07-20 12:16:17 +0200738 /* It isn't safe to enable all VGA cards. */
739 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200740 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000741
Uwe Hermanne4870472010-11-04 23:23:47 +0000742 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000743 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200744
745 if (CONFIG_ONBOARD_VGA_IS_PRIMARY && vga_onboard)
746 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000747
Patrick Georgi5869fa22012-07-20 12:29:33 +0200748 /* If we prefer plugin VGA over chipset VGA, the chipset might
749 want to know. */
750 if (!CONFIG_ONBOARD_VGA_IS_PRIMARY && (vga != vga_onboard) &&
751 vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) {
752 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
753 vga_onboard->ops->disable(vga_onboard);
754 }
755
arch import user (historical)dc811182005-07-06 17:16:09 +0000756 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000757 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000758 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000759 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000760 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
761 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000762 bus = vga->bus;
763 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000764
Myles Watson29cc9ed2009-07-02 18:56:24 +0000765 /* Now walk up the bridges setting the VGA enable. */
766 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000767 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000768 dev_path(bus->dev));
Li-Ta Lodb7f47c2004-01-08 21:15:49 +0000769 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000770 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000771 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000772}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000773
Li-Ta Lo04930692004-11-25 17:37:19 +0000774/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000775 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000776 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000777 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000778 * resources to hardware. For bridge devices, the set_resources() method
779 * has to recurse into every down stream buses.
780 *
781 * Mutual recursion:
782 * assign_resources() -> device_operation::set_resources()
783 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000784 *
785 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000786 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000787void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000788{
789 struct device *curdev;
790
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000791 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000792 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000793
Myles Watson29cc9ed2009-07-02 18:56:24 +0000794 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000795 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000796 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000797
Eric Biedermane9a271e32003-09-02 03:36:25 +0000798 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000799 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000800 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000801 continue;
802 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000803 curdev->ops->set_resources(curdev);
804 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000805 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000806 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000807}
808
Li-Ta Lo5782d272004-04-26 17:51:20 +0000809/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000810 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000811 *
812 * Enable resources of the device by calling the device specific
813 * enable_resources() method.
814 *
815 * The parent's resources should be enabled first to avoid having enabling
816 * order problem. This is done by calling the parent's enable_resources()
Myles Watson7eac4452010-06-17 16:16:56 +0000817 * method before its childrens' enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000818 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000819 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000820 */
Myles Watson7eac4452010-06-17 16:16:56 +0000821static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000822{
Myles Watson7eac4452010-06-17 16:16:56 +0000823 struct device *dev;
824 struct bus *c_link;
825
826 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000827 if (dev->enabled && dev->ops && dev->ops->enable_resources)
Myles Watson7eac4452010-06-17 16:16:56 +0000828 dev->ops->enable_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000829 }
Myles Watson7eac4452010-06-17 16:16:56 +0000830
831 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000832 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000833 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000834 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000835}
836
Myles Watson032a9652009-05-11 22:24:53 +0000837/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000838 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
839 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000840 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000841 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000842 */
843int reset_bus(struct bus *bus)
844{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000845 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000846 bus->dev->ops->reset_bus(bus);
847 bus->reset_needed = 0;
848 return 1;
849 }
850 return 0;
851}
852
Myles Watson032a9652009-05-11 22:24:53 +0000853/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000854 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000855 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000856 * If there are bridges on the bus, recursively scan the buses behind the
857 * bridges. If the setting up and tuning of the bus causes a reset to be
858 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000859 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000860 * @param busdev Pointer to the bus device.
861 * @param max Current bus number.
862 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000863 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000864unsigned int scan_bus(struct device *busdev, unsigned int max)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000865{
866 unsigned int new_max;
867 int do_scan_bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000868
Myles Watson29cc9ed2009-07-02 18:56:24 +0000869 if (!busdev || !busdev->enabled || !busdev->ops ||
870 !busdev->ops->scan_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000871 return max;
872 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000873
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000874 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000875 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000876 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000877 new_max = busdev->ops->scan_bus(busdev, max);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000878 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000879 for (link = busdev->link_list; link; link = link->next) {
880 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000881 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000882 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000883 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000884 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000885 }
886 }
887 }
888 return new_max;
889}
890
Li-Ta Loe5266692004-03-23 21:28:05 +0000891/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000892 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000893 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000894 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000895 * file. The device structures for these devices are generated at compile
896 * time by the config tool and are organized into the device tree. This
897 * function determines if the devices created at compile time actually exist
898 * in the physical system.
899 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000900 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000901 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000902 * device tree.
903 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000904 * This function starts from the root device 'dev_root', scans the buses in
905 * the system recursively, and modifies the device tree according to the
906 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000907 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000908 * This function has no idea how to scan and probe buses and devices at all.
909 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000910 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000911 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000912 */
913void dev_enumerate(void)
914{
915 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000916
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000917 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000918
Eric Biederman8ca8d762003-04-22 19:02:15 +0000919 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000920
Uwe Hermanne4870472010-11-04 23:23:47 +0000921 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000922 printk(BIOS_SPEW, "Compare with tree...\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000923 show_devs_tree(root, BIOS_SPEW, 0, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000924
Uwe Hermanne4870472010-11-04 23:23:47 +0000925 if (root->chip_ops && root->chip_ops->enable_dev)
Eric Biederman7003ba42004-10-16 06:20:29 +0000926 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000927
Eric Biedermanb78c1972004-10-14 20:54:17 +0000928 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000929 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000930 return;
931 }
Stefan Reinauer6afcea82009-07-18 17:58:44 +0000932 scan_bus(root, 0);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000933 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000934}
935
Li-Ta Loe5266692004-03-23 21:28:05 +0000936/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000937 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +0000938 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000939 * Starting at the root of the device tree, travel it recursively in two
940 * passes. In the first pass, we compute and allocate resources (ranges)
941 * requried by each device. In the second pass, the resources ranges are
942 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000943 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000944 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000945 *
946 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +0000947 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000948 */
949void dev_configure(void)
950{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000951 struct resource *res;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000952 struct device *root;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000953 struct device *child;
Li-Ta Loe5266692004-03-23 21:28:05 +0000954
Marc Jones80bd74c2012-02-21 17:44:35 +0100955 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +0100956
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000957 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000958
Eric Biedermanb78c1972004-10-14 20:54:17 +0000959 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000960
Uwe Hermanne4870472010-11-04 23:23:47 +0000961 /*
962 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +0000963 * space for IO, MEM, and PREFMEM resources in the domain. The
964 * allocation of device resources will be done from this address space.
965 */
Myles Watsoncd5d7562009-05-12 13:43:34 +0000966
Myles Watson29cc9ed2009-07-02 18:56:24 +0000967 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +0000968
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000969 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +0000970 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000971 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000972
Stefan Reinauer39e72292009-10-26 16:47:05 +0000973 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +0000974
Myles Watson29cc9ed2009-07-02 18:56:24 +0000975 /* Compute resources for all domains. */
Myles Watson894a3472010-06-09 22:41:35 +0000976 for (child = root->link_list->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000977 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
978 continue;
Myles Watsonc25cc112010-05-21 14:33:48 +0000979 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000980 if (res->flags & IORESOURCE_FIXED)
981 continue;
982 if (res->flags & IORESOURCE_PREFETCH) {
Myles Watson894a3472010-06-09 22:41:35 +0000983 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +0000984 res, MEM_MASK, PREF_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000985 continue;
986 }
987 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +0000988 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +0000989 res, MEM_MASK, MEM_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000990 continue;
991 }
992 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +0000993 compute_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +0000994 res, IO_MASK, IO_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000995 continue;
996 }
997 }
998 }
999
1000 /* For all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001001 for (child = root->link_list->children; child; child=child->sibling)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001002 if (child->path.type == DEVICE_PATH_PCI_DOMAIN)
1003 avoid_fixed_resources(child);
1004
Uwe Hermanne4870472010-11-04 23:23:47 +00001005 /*
1006 * Now we need to adjust the resources. MEM resources need to start at
Myles Watson29cc9ed2009-07-02 18:56:24 +00001007 * the highest address managable.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001008 */
Myles Watson894a3472010-06-09 22:41:35 +00001009 for (child = root->link_list->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001010 if (child->path.type != DEVICE_PATH_PCI_DOMAIN)
1011 continue;
Myles Watsonc25cc112010-05-21 14:33:48 +00001012 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001013 if (!(res->flags & IORESOURCE_MEM) ||
1014 res->flags & IORESOURCE_FIXED)
1015 continue;
1016 res->base = resource_max(res);
1017 }
1018 }
Eric Biederman5cd81732004-03-11 15:01:31 +00001019
Eric Biedermanb78c1972004-10-14 20:54:17 +00001020 /* Store the computed resource allocations into device registers ... */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001021 printk(BIOS_INFO, "Setting resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001022 for (child = root->link_list->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001023 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
1024 continue;
Myles Watsonc25cc112010-05-21 14:33:48 +00001025 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001026 if (res->flags & IORESOURCE_FIXED)
1027 continue;
1028 if (res->flags & IORESOURCE_PREFETCH) {
Myles Watson894a3472010-06-09 22:41:35 +00001029 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001030 res, MEM_MASK, PREF_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001031 continue;
1032 }
1033 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001034 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001035 res, MEM_MASK, MEM_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001036 continue;
1037 }
1038 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001039 allocate_resources(child->link_list,
Uwe Hermanne4870472010-11-04 23:23:47 +00001040 res, IO_MASK, IO_TYPE);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001041 continue;
1042 }
1043 }
1044 }
Myles Watson894a3472010-06-09 22:41:35 +00001045 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001046 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001047 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +00001048
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001049 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001050}
1051
Li-Ta Loe5266692004-03-23 21:28:05 +00001052/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001053 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +00001054 *
1055 * Starting at the root, walk the tree and enable all devices/bridges by
1056 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001057 */
1058void dev_enable(void)
1059{
Myles Watson7eac4452010-06-17 16:16:56 +00001060 struct bus *link;
1061
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001062 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001063
Uwe Hermanne4870472010-11-04 23:23:47 +00001064 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001065 for (link = dev_root.link_list; link; link = link->next)
1066 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +00001067
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001068 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001069}
1070
Li-Ta Loe5266692004-03-23 21:28:05 +00001071/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001072 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +00001073 *
Uwe Hermanne4870472010-11-04 23:23:47 +00001074 * The parent should be initialized first to avoid having an ordering problem.
1075 * This is done by calling the parent's init() method before its childrens'
1076 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +00001077 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001078 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +00001079 */
1080static void init_dev(struct device *dev)
1081{
Uwe Hermanne4870472010-11-04 23:23:47 +00001082 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +00001083 return;
Myles Watson7eac4452010-06-17 16:16:56 +00001084
1085 if (!dev->initialized && dev->ops && dev->ops->init) {
1086 if (dev->path.type == DEVICE_PATH_I2C) {
1087 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1088 dev_path(dev->bus->dev), dev->bus->link_num);
1089 }
1090
1091 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1092 dev->initialized = 1;
1093 dev->ops->init(dev);
1094 }
1095}
1096
1097static void init_link(struct bus *link)
1098{
1099 struct device *dev;
1100 struct bus *c_link;
1101
Uwe Hermanne4870472010-11-04 23:23:47 +00001102 for (dev = link->children; dev; dev = dev->sibling)
Myles Watson7eac4452010-06-17 16:16:56 +00001103 init_dev(dev);
Myles Watson7eac4452010-06-17 16:16:56 +00001104
1105 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001106 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +00001107 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +00001108 }
1109}
1110
1111/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001112 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +00001113 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001114 * Starting at the root device, call the device's init() method to do
1115 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001116 */
1117void dev_initialize(void)
1118{
Myles Watson7eac4452010-06-17 16:16:56 +00001119 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001120
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001121 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +00001122
Duncan Laurieb4aaaa72012-01-17 09:03:11 -08001123#if CONFIG_ARCH_X86
1124 /* Ensure EBDA is prepared before Option ROMs. */
1125 setup_default_ebda();
1126#endif
1127
Myles Watson1bd3fb72010-08-16 16:25:23 +00001128 /* First call the mainboard init. */
1129 init_dev(&dev_root);
1130
Uwe Hermanne4870472010-11-04 23:23:47 +00001131 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001132 for (link = dev_root.link_list; link; link = link->next)
1133 init_link(link);
1134
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001135 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001136 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001137}