blob: e23c9def5bb3348851a012ca222d1618d30a0d4b [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * It was originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
5 *
6 * Modifications are:
7 * Copyright (C) 2003 Eric Biederman <ebiederm@xmission.com>
8 * Copyright (C) 2003-2004 Linux Networx
9 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
10 * Copyright (C) 2003 Ronald G. Minnich <rminnich@gmail.com>
11 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
12 * Copyright (C) 2005-2006 Tyan
13 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
14 * Copyright (C) 2005-2006 Stefan Reinauer <stepan@openbios.org>
Myles Watson29cc9ed2009-07-02 18:56:24 +000015 * Copyright (C) 2009 Myles Watson <mylesgw@gmail.com>
Uwe Hermannb80dbf02007-04-22 19:08:13 +000016 */
17
18/*
Eric Biederman8ca8d762003-04-22 19:02:15 +000019 * (c) 1999--2000 Martin Mares <mj@suse.cz>
Eric Biederman8ca8d762003-04-22 19:02:15 +000020 */
Uwe Hermanne4870472010-11-04 23:23:47 +000021
22/*
23 * Lots of mods by Ron Minnich <rminnich@lanl.gov>, with
24 * the final architecture guidance from Tom Merritt <tjm@codegen.com>.
25 *
Myles Watson032a9652009-05-11 22:24:53 +000026 * In particular, we changed from the one-pass original version to
27 * Tom's recommended multiple-pass version. I wasn't sure about doing
Eric Biederman8ca8d762003-04-22 19:02:15 +000028 * it with multiple passes, until I actually started doing it and saw
Uwe Hermanne4870472010-11-04 23:23:47 +000029 * the wisdom of Tom's recommendations...
Eric Biederman8ca8d762003-04-22 19:02:15 +000030 *
31 * Lots of cleanups by Eric Biederman to handle bridges, and to
Uwe Hermanne4870472010-11-04 23:23:47 +000032 * handle resource allocation for non-PCI devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +000033 */
34
35#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000036#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000037#include <device/device.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020038#include <device/pci_def.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000039#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000040#include <stdlib.h>
41#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000042#include <smp/spinlock.h>
Duncan Laurieb4aaaa72012-01-17 09:03:11 -080043#if CONFIG_ARCH_X86
44#include <arch/ebda.h>
45#endif
Aaron Durbin05294292013-04-30 15:41:13 -050046#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000047
Li-Ta Loe5266692004-03-23 21:28:05 +000048/** Linked list of ALL devices */
Eric Biederman5cd81732004-03-11 15:01:31 +000049struct device *all_devices = &dev_root;
Li-Ta Loe5266692004-03-23 21:28:05 +000050/** Pointer to the last device */
Myles Watson70679a02010-09-01 21:03:03 +000051extern struct device *last_dev;
Myles Watsonc25cc112010-05-21 14:33:48 +000052/** Linked list of free resources */
53struct resource *free_resources = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +000054
Nico Huberacd7d952012-07-25 10:33:05 +020055/**
56 * Initialize all chips of statically known devices.
57 *
58 * Will be called before bus enumeration to initialize chips stated in the
59 * device tree.
60 */
61void dev_initialize_chips(void)
62{
63 struct device *dev;
64
65 for (dev = all_devices; dev; dev = dev->next) {
66 /* Initialize chip if we haven't yet. */
67 if (dev->chip_ops && dev->chip_ops->init &&
68 !dev->chip_ops->initialized) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -070069 post_log_path(dev);
Nico Huberacd7d952012-07-25 10:33:05 +020070 dev->chip_ops->init(dev->chip_info);
71 dev->chip_ops->initialized = 1;
72 }
73 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -070074 post_log_clear();
Nico Huberacd7d952012-07-25 10:33:05 +020075}
76
Marc Jones2a58ecd2013-10-29 17:32:00 -060077/**
78 * Finalize all chips of statically known devices.
79 *
80 * This is the last call before calling the payload. This is a good place
81 * to lock registers or other final cleanup.
82 */
83void dev_finalize_chips(void)
84{
85 struct device *dev;
86
87 for (dev = all_devices; dev; dev = dev->next) {
88 /* Initialize chip if we haven't yet. */
89 if (dev->chip_ops && dev->chip_ops->final &&
90 !dev->chip_ops->finalized) {
91 dev->chip_ops->final(dev->chip_info);
92 dev->chip_ops->finalized = 1;
93 }
94 }
95}
96
Uwe Hermannc1ee4292010-10-17 19:01:48 +000097DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000098
Kyösti Mälkkib25374c2012-08-01 08:05:22 +030099#if CONFIG_GFXUMA
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300100/* IGD UMA memory */
101uint64_t uma_memory_base = 0;
102uint64_t uma_memory_size = 0;
Kyösti Mälkkib25374c2012-08-01 08:05:22 +0300103#endif
Kyösti Mälkkicc55b9b2012-07-11 07:55:21 +0300104
Li-Ta Loe5266692004-03-23 21:28:05 +0000105/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000106 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +0000107 *
Martin Roth63373ed2013-07-08 16:24:19 -0600108 * Allocate a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000109 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000110 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000111 * @param parent Parent bus the newly created device should be attached to.
112 * @param path Path to the device to be created.
113 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +0000114 *
115 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +0000116 */
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300117static device_t __alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000118{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000119 device_t dev, child;
Li-Ta Loe5266692004-03-23 21:28:05 +0000120
Myles Watson29cc9ed2009-07-02 18:56:24 +0000121 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000122 for (child = parent->children; child && child->sibling; /* */ )
Eric Biedermane9a271e32003-09-02 03:36:25 +0000123 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +0000124
Eric Biedermane9a271e32003-09-02 03:36:25 +0000125 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000126 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +0000127 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +0000128
Eric Biedermane9a271e32003-09-02 03:36:25 +0000129 memset(dev, 0, sizeof(*dev));
130 memcpy(&dev->path, path, sizeof(*path));
131
Myles Watson29cc9ed2009-07-02 18:56:24 +0000132 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +0000133 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000134
Eric Biedermanb78c1972004-10-14 20:54:17 +0000135 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000136 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000137 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000138 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000139 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000140 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000141
Eric Biederman03acab62004-10-14 21:25:53 +0000142 /* Append a new device to the global device list.
143 * The list is used to find devices once everything is set up.
144 */
Myles Watson70679a02010-09-01 21:03:03 +0000145 last_dev->next = dev;
146 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000147
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300148 return dev;
149}
150
151device_t alloc_dev(struct bus *parent, struct device_path *path)
152{
153 device_t dev;
154 spin_lock(&dev_lock);
155 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000156 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000157 return dev;
158}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000159
Li-Ta Loe5266692004-03-23 21:28:05 +0000160/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300161 * See if a device structure already exists and if not allocate it.
162 *
163 * @param parent The bus to find the device on.
164 * @param path The relative path from the bus to the appropriate device.
165 * @return Pointer to a device structure for the device on bus at path.
166 */
167device_t alloc_find_dev(struct bus *parent, struct device_path *path)
168{
169 device_t child;
170 spin_lock(&dev_lock);
171 child = find_dev_path(parent, path);
172 if (!child)
173 child = __alloc_dev(parent, path);
174 spin_unlock(&dev_lock);
175 return child;
176}
177
178/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000179 * Round a number up to an alignment.
180 *
181 * @param val The starting value.
Martin Roth32bc6b62015-01-04 16:54:35 -0700182 * @param pow Alignment as a power of two.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000183 * @return Rounded up number.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184 */
Eric Biederman448bd632004-10-14 22:52:15 +0000185static resource_t round(resource_t val, unsigned long pow)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186{
Eric Biederman448bd632004-10-14 22:52:15 +0000187 resource_t mask;
188 mask = (1ULL << pow) - 1ULL;
189 val += mask;
190 val &= ~mask;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000191 return val;
192}
193
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200194static const char * resource2str(struct resource *res)
195{
196 if (res->flags & IORESOURCE_IO)
197 return "io";
198 if (res->flags & IORESOURCE_PREFETCH)
199 return "prefmem";
200 if (res->flags & IORESOURCE_MEM)
201 return "mem";
202 return "undefined";
203}
204
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000205/**
206 * Read the resources on all devices of a given bus.
207 *
208 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000209 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000210static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000211{
212 struct device *curdev;
213
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000214 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
215 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000216
Myles Watson29cc9ed2009-07-02 18:56:24 +0000217 /* Walk through all devices and find which resources they need. */
218 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000219 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000220
221 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000223
Eric Biedermane9a271e32003-09-02 03:36:25 +0000224 if (!curdev->ops || !curdev->ops->read_resources) {
Martin Roth7bc74ab2015-11-18 20:23:02 -0700225 if (curdev->path.type != DEVICE_PATH_APIC)
226 printk(BIOS_ERR, "%s missing read_resources\n",
227 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000228 continue;
229 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700230 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000232
233 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000234 for (link = curdev->link_list; link; link = link->next)
235 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000236 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700237 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000239 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000240}
241
Eric Biedermane9a271e32003-09-02 03:36:25 +0000242struct pick_largest_state {
243 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000244 struct device *result_dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000245 struct resource *result;
246 int seen_last;
247};
248
Myles Watson29cc9ed2009-07-02 18:56:24 +0000249static void pick_largest_resource(void *gp, struct device *dev,
250 struct resource *resource)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000251{
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000252 struct pick_largest_state *state = gp;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000253 struct resource *last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000254
Eric Biedermane9a271e32003-09-02 03:36:25 +0000255 last = state->last;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000256
257 /* Be certain to pick the successor to last. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000258 if (resource == last) {
259 state->seen_last = 1;
260 return;
261 }
Myles Watson032a9652009-05-11 22:24:53 +0000262 if (resource->flags & IORESOURCE_FIXED)
Uwe Hermanne4870472010-11-04 23:23:47 +0000263 return; /* Skip it. */
Myles Watson032a9652009-05-11 22:24:53 +0000264 if (last && ((last->align < resource->align) ||
265 ((last->align == resource->align) &&
266 (last->size < resource->size)) ||
267 ((last->align == resource->align) &&
268 (last->size == resource->size) && (!state->seen_last)))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000269 return;
270 }
Myles Watson032a9652009-05-11 22:24:53 +0000271 if (!state->result ||
272 (state->result->align < resource->align) ||
273 ((state->result->align == resource->align) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000274 (state->result->size < resource->size))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000275 state->result_dev = dev;
276 state->result = resource;
Myles Watson032a9652009-05-11 22:24:53 +0000277 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000278}
279
Myles Watson29cc9ed2009-07-02 18:56:24 +0000280static struct device *largest_resource(struct bus *bus,
281 struct resource **result_res,
282 unsigned long type_mask,
283 unsigned long type)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000284{
285 struct pick_largest_state state;
286
287 state.last = *result_res;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000288 state.result_dev = NULL;
289 state.result = NULL;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000290 state.seen_last = 0;
291
Myles Watson032a9652009-05-11 22:24:53 +0000292 search_bus_resources(bus, type_mask, type, pick_largest_resource,
293 &state);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000294
295 *result_res = state.result;
296 return state.result_dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000297}
298
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000299/**
Uwe Hermanne4870472010-11-04 23:23:47 +0000300 * This function is the guts of the resource allocator.
Myles Watson032a9652009-05-11 22:24:53 +0000301 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000302 * The problem.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000303 * - Allocate resource locations for every device.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000304 * - Don't overlap, and follow the rules of bridges.
305 * - Don't overlap with resources in fixed locations.
306 * - Be efficient so we don't have ugly strategies.
307 *
308 * The strategy.
309 * - Devices that have fixed addresses are the minority so don't
Myles Watson29cc9ed2009-07-02 18:56:24 +0000310 * worry about them too much. Instead only use part of the address
311 * space for devices with programmable addresses. This easily handles
Eric Biederman8ca8d762003-04-22 19:02:15 +0000312 * everything except bridges.
313 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000314 * - PCI devices are required to have their sizes and their alignments
315 * equal. In this case an optimal solution to the packing problem
316 * exists. Allocate all devices from highest alignment to least
317 * alignment or vice versa. Use this.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000318 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000319 * - So we can handle more than PCI run two allocation passes on bridges. The
320 * first to see how large the resources are behind the bridge, and what
321 * their alignment requirements are. The second to assign a safe address to
322 * the devices behind the bridge. This allows us to treat a bridge as just
323 * a device with a couple of resources, and not need to special case it in
324 * the allocator. Also this allows handling of other types of bridges.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000325 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000326 * @param bus The bus we are traversing.
327 * @param bridge The bridge resource which must contain the bus' resources.
328 * @param type_mask This value gets ANDed with the resource type.
329 * @param type This value must match the result of the AND.
330 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000331 */
Myles Watson54913b92009-10-13 20:00:09 +0000332static void compute_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000333 unsigned long type_mask, unsigned long type)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000334{
335 struct device *dev;
336 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000337 resource_t base;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000338 base = round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000339
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200340 printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d"
341 " limit: %llx\n", dev_path(bus->dev), resource2str(bridge),
342 base, bridge->size, bridge->align,
Uwe Hermanne4870472010-11-04 23:23:47 +0000343 bridge->gran, bridge->limit);
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000344
Uwe Hermanne4870472010-11-04 23:23:47 +0000345 /* For each child which is a bridge, compute the resource needs. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000346 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000347 struct resource *child_bridge;
348
Myles Watson894a3472010-06-09 22:41:35 +0000349 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000350 continue;
351
352 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000353 for (child_bridge = dev->resource_list; child_bridge;
354 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000355 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000356
Uwe Hermanne4870472010-11-04 23:23:47 +0000357 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
358 || (child_bridge->flags & type_mask) != type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000359 continue;
360
Uwe Hermanne4870472010-11-04 23:23:47 +0000361 /*
362 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000363 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000364 * and non-prefetchable memory. Bridges below them need
365 * it separated. Add the PREFETCH flag to the type_mask
366 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000367 */
Myles Watson894a3472010-06-09 22:41:35 +0000368 link = dev->link_list;
369 while (link && link->link_num !=
370 IOINDEX_LINK(child_bridge->index))
371 link = link->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000372
373 if (link == NULL) {
Myles Watson894a3472010-06-09 22:41:35 +0000374 printk(BIOS_ERR, "link %ld not found on %s\n",
375 IOINDEX_LINK(child_bridge->index),
376 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000377 }
378
Myles Watson894a3472010-06-09 22:41:35 +0000379 compute_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000380 type_mask | IORESOURCE_PREFETCH,
381 type | (child_bridge->flags &
382 IORESOURCE_PREFETCH));
383 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000384 }
385
Myles Watson29cc9ed2009-07-02 18:56:24 +0000386 /* Remember we haven't found anything yet. */
387 resource = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000388
Uwe Hermanne4870472010-11-04 23:23:47 +0000389 /*
390 * Walk through all the resources on the current bus and compute the
391 * amount of address space taken by them. Take granularity and
Myles Watson29cc9ed2009-07-02 18:56:24 +0000392 * alignment into account.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000393 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000394 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000395
Myles Watson29cc9ed2009-07-02 18:56:24 +0000396 /* Size 0 resources can be skipped. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000397 if (!resource->size)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000398 continue;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000399
Myles Watson29cc9ed2009-07-02 18:56:24 +0000400 /* Propagate the resource alignment to the bridge resource. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000401 if (resource->align > bridge->align)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000402 bridge->align = resource->align;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000403
404 /* Propagate the resource limit to the bridge register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000405 if (bridge->limit > resource->limit)
Eric Biedermandbec2d42004-10-21 10:44:08 +0000406 bridge->limit = resource->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000407
408 /* Warn if it looks like APICs aren't declared. */
409 if ((resource->limit == 0xffffffff) &&
410 (resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000411 printk(BIOS_ERR,
412 "Resource limit looks wrong! (no APIC?)\n");
Patrick Georgi51615092012-03-11 19:31:03 +0100413 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000414 dev_path(dev), resource->index, resource->limit);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000415 }
Stefan Reinauer51754a32008-08-01 12:28:38 +0000416
Eric Biederman8ca8d762003-04-22 19:02:15 +0000417 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000418 /*
419 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000420 * expansion card addresses. The legacy PCI decodes
421 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
422 * 0x00 - 0xff can be used out of each 0x400 block of
423 * I/O space.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000424 */
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000425 if ((base & 0x300) != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000426 base = (base & ~0x3ff) + 0x400;
427 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000428 /*
429 * Don't allow allocations in the VGA I/O range.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000430 * PCI has special cases for that.
431 */
432 else if ((base >= 0x3b0) && (base <= 0x3df)) {
433 base = 0x3e0;
434 }
435 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000436 /* Base must be aligned. */
437 base = round(base, resource->align);
438 resource->base = base;
439 base += resource->size;
Myles Watson032a9652009-05-11 22:24:53 +0000440
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000441 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000442 dev_path(dev), resource->index, resource->base,
443 resource->base + resource->size - 1,
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200444 resource2str(resource));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000445 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000446
447 /*
448 * A PCI bridge resource does not need to be a power of two size, but
449 * it does have a minimum granularity. Round the size up to that
450 * minimum granularity so we know not to place something else at an
Martin Roth63373ed2013-07-08 16:24:19 -0600451 * address positively decoded by the bridge.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000452 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000453 bridge->size = round(base, bridge->gran) -
454 round(bridge->base, bridge->align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000455
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200456 printk(BIOS_SPEW, "%s %s: base: %llx size: %llx align: %d gran: %d"
457 " limit: %llx done\n", dev_path(bus->dev),
458 resource2str(bridge),
Uwe Hermanne4870472010-11-04 23:23:47 +0000459 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000460}
461
462/**
463 * This function is the second part of the resource allocator.
464 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000465 * See the compute_resources function for a more detailed explanation.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000466 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000467 * This function assigns the resources a value.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000468 *
469 * @param bus The bus we are traversing.
470 * @param bridge The bridge resource which must contain the bus' resources.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000471 * @param type_mask This value gets ANDed with the resource type.
472 * @param type This value must match the result of the AND.
Uwe Hermanne4870472010-11-04 23:23:47 +0000473 *
474 * @see compute_resources
Myles Watson29cc9ed2009-07-02 18:56:24 +0000475 */
Myles Watson54913b92009-10-13 20:00:09 +0000476static void allocate_resources(struct bus *bus, struct resource *bridge,
Uwe Hermanne4870472010-11-04 23:23:47 +0000477 unsigned long type_mask, unsigned long type)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000478{
479 struct device *dev;
480 struct resource *resource;
481 resource_t base;
482 base = bridge->base;
483
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200484 printk(BIOS_SPEW, "%s %s: base:%llx size:%llx align:%d gran:%d "
485 "limit:%llx\n", dev_path(bus->dev),
486 resource2str(bridge),
Myles Watson29cc9ed2009-07-02 18:56:24 +0000487 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
488
489 /* Remember we haven't found anything yet. */
490 resource = NULL;
491
Uwe Hermanne4870472010-11-04 23:23:47 +0000492 /*
493 * Walk through all the resources on the current bus and allocate them
Myles Watson29cc9ed2009-07-02 18:56:24 +0000494 * address space.
495 */
496 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
497
498 /* Propagate the bridge limit to the resource register. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000499 if (resource->limit > bridge->limit)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000500 resource->limit = bridge->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000501
502 /* Size 0 resources can be skipped. */
503 if (!resource->size) {
504 /* Set the base to limit so it doesn't confuse tolm. */
505 resource->base = resource->limit;
506 resource->flags |= IORESOURCE_ASSIGNED;
507 continue;
508 }
509
510 if (resource->flags & IORESOURCE_IO) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000511 /*
512 * Don't allow potential aliases over the legacy PCI
Myles Watson29cc9ed2009-07-02 18:56:24 +0000513 * expansion card addresses. The legacy PCI decodes
514 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
515 * 0x00 - 0xff can be used out of each 0x400 block of
516 * I/O space.
517 */
518 if ((base & 0x300) != 0) {
519 base = (base & ~0x3ff) + 0x400;
520 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000521 /*
522 * Don't allow allocations in the VGA I/O range.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000523 * PCI has special cases for that.
524 */
525 else if ((base >= 0x3b0) && (base <= 0x3df)) {
526 base = 0x3e0;
527 }
528 }
529
530 if ((round(base, resource->align) + resource->size - 1) <=
531 resource->limit) {
532 /* Base must be aligned. */
533 base = round(base, resource->align);
534 resource->base = base;
Kyösti Mälkki134b6162015-03-23 19:58:23 +0200535 resource->limit = resource->base + resource->size - 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000536 resource->flags |= IORESOURCE_ASSIGNED;
537 resource->flags &= ~IORESOURCE_STORED;
538 base += resource->size;
539 } else {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000540 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000541 printk(BIOS_ERR, " aligned base %llx size %llx "
542 "limit %llx\n", round(base, resource->align),
543 resource->size, resource->limit);
544 printk(BIOS_ERR, " %llx needs to be <= %llx "
545 "(limit)\n", (round(base, resource->align) +
Myles Watson29cc9ed2009-07-02 18:56:24 +0000546 resource->size) - 1, resource->limit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000547 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
548 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
549 ? "Assigned: " : "", dev_path(dev),
550 resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000551 resource->base + resource->size - 1,
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200552 resource2str(resource));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000553 }
554
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200555 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
556 dev_path(dev), resource->index, resource->base,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000557 resource->size ? resource->base + resource->size - 1 :
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200558 resource->base, resource2str(resource));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000559 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000560
561 /*
562 * A PCI bridge resource does not need to be a power of two size, but
Myles Watson29cc9ed2009-07-02 18:56:24 +0000563 * it does have a minimum granularity. Round the size up to that
564 * minimum granularity so we know not to place something else at an
565 * address positively decoded by the bridge.
566 */
567
568 bridge->flags |= IORESOURCE_ASSIGNED;
569
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200570 printk(BIOS_SPEW, "%s %s: next_base: %llx size: %llx align: %d "
571 "gran: %d done\n", dev_path(bus->dev),
572 resource2str(bridge), base, bridge->size, bridge->align,
Uwe Hermanne4870472010-11-04 23:23:47 +0000573 bridge->gran);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000574
575 /* For each child which is a bridge, allocate_resources. */
576 for (dev = bus->children; dev; dev = dev->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000577 struct resource *child_bridge;
578
Myles Watson894a3472010-06-09 22:41:35 +0000579 if (!dev->link_list)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000580 continue;
581
582 /* Find the resources with matching type flags. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000583 for (child_bridge = dev->resource_list; child_bridge;
584 child_bridge = child_bridge->next) {
Myles Watson894a3472010-06-09 22:41:35 +0000585 struct bus* link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000586
587 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
588 (child_bridge->flags & type_mask) != type)
589 continue;
590
Uwe Hermanne4870472010-11-04 23:23:47 +0000591 /*
592 * Split prefetchable memory if combined. Many domains
Myles Watson29cc9ed2009-07-02 18:56:24 +0000593 * use the same address space for prefetchable memory
Uwe Hermanne4870472010-11-04 23:23:47 +0000594 * and non-prefetchable memory. Bridges below them need
595 * it separated. Add the PREFETCH flag to the type_mask
596 * and type.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000597 */
Myles Watson894a3472010-06-09 22:41:35 +0000598 link = dev->link_list;
599 while (link && link->link_num !=
600 IOINDEX_LINK(child_bridge->index))
601 link = link->next;
602 if (link == NULL)
603 printk(BIOS_ERR, "link %ld not found on %s\n",
604 IOINDEX_LINK(child_bridge->index),
605 dev_path(dev));
Uwe Hermanne4870472010-11-04 23:23:47 +0000606
Myles Watson894a3472010-06-09 22:41:35 +0000607 allocate_resources(link, child_bridge,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000608 type_mask | IORESOURCE_PREFETCH,
609 type | (child_bridge->flags &
610 IORESOURCE_PREFETCH));
611 }
612 }
613}
614
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200615static int resource_is(struct resource *res, u32 type)
616{
617 return (res->flags & IORESOURCE_TYPE_MASK) == type;
618}
Myles Watson29cc9ed2009-07-02 18:56:24 +0000619
620struct constraints {
Kyösti Mälkkifcbebb62015-03-17 06:42:54 +0200621 struct resource io, mem;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000622};
623
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200624static struct resource * resource_limit(struct constraints *limits, struct resource *res)
625{
626 struct resource *lim = NULL;
627
628 /* MEM, or I/O - skip any others. */
629 if (resource_is(res, IORESOURCE_MEM))
630 lim = &limits->mem;
631 else if (resource_is(res, IORESOURCE_IO))
632 lim = &limits->io;
633
634 return lim;
635}
636
Myles Watson29cc9ed2009-07-02 18:56:24 +0000637static void constrain_resources(struct device *dev, struct constraints* limits)
638{
639 struct device *child;
640 struct resource *res;
641 struct resource *lim;
Myles Watson894a3472010-06-09 22:41:35 +0000642 struct bus *link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000643
Myles Watson29cc9ed2009-07-02 18:56:24 +0000644 /* Constrain limits based on the fixed resources of this device. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000645 for (res = dev->resource_list; res; res = res->next) {
Patrick Georgi18c585b2009-08-28 12:48:02 +0000646 if (!(res->flags & IORESOURCE_FIXED))
647 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000648 if (!res->size) {
649 /* It makes no sense to have 0-sized, fixed resources.*/
Uwe Hermanne4870472010-11-04 23:23:47 +0000650 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
651 "size=0!\n", dev_path(dev), res->index);
Patrick Georgi6bd93f42009-08-19 17:29:41 +0000652 continue;
Myles Watsonce9d8642009-08-19 19:12:39 +0000653 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000654
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200655 lim = resource_limit(limits, res);
656 if (!lim)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000657 continue;
658
Uwe Hermanne4870472010-11-04 23:23:47 +0000659 /*
660 * Is it a fixed resource outside the current known region?
661 * If so, we don't have to consider it - it will be handled
662 * correctly and doesn't affect current region's limits.
663 */
664 if (((res->base + res->size -1) < lim->base)
665 || (res->base > lim->limit))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000666 continue;
667
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200668 printk(BIOS_SPEW, "%s: %s %02lx base %08llx limit %08llx %s (fixed)\n",
669 __func__, dev_path(dev), res->index, res->base,
670 res->base + res->size - 1, resource2str(res));
671
Uwe Hermanne4870472010-11-04 23:23:47 +0000672 /*
673 * Choose to be above or below fixed resources. This check is
674 * signed so that "negative" amounts of space are handled
675 * correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000676 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000677 if ((signed long long)(lim->limit - (res->base + res->size -1))
678 > (signed long long)(res->base - lim->base))
Myles Watson29cc9ed2009-07-02 18:56:24 +0000679 lim->base = res->base + res->size;
680 else
681 lim->limit = res->base -1;
682 }
683
684 /* Descend into every enabled child and look for fixed resources. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000685 for (link = dev->link_list; link; link = link->next) {
686 for (child = link->children; child; child = child->sibling) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000687 if (child->enabled)
688 constrain_resources(child, limits);
Uwe Hermanne4870472010-11-04 23:23:47 +0000689 }
690 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000691}
692
693static void avoid_fixed_resources(struct device *dev)
694{
695 struct constraints limits;
696 struct resource *res;
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200697 struct resource *lim;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000698
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000699 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000700
Uwe Hermanne4870472010-11-04 23:23:47 +0000701 /* Initialize constraints to maximum size. */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000702 limits.io.base = 0;
703 limits.io.limit = 0xffffffffffffffffULL;
704 limits.mem.base = 0;
705 limits.mem.limit = 0xffffffffffffffffULL;
706
707 /* Constrain the limits to dev's initial resources. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000708 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000709 if ((res->flags & IORESOURCE_FIXED))
710 continue;
Patrick Georgi51615092012-03-11 19:31:03 +0100711 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
Uwe Hermanne4870472010-11-04 23:23:47 +0000712 dev_path(dev), res->index, res->limit);
Kyösti Mälkkifcbebb62015-03-17 06:42:54 +0200713
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200714 lim = resource_limit(&limits, res);
715 if (!lim)
716 continue;
717
718 if (res->base > lim->base)
719 lim->base = res->base;
720 if (res->limit < lim->limit)
721 lim->limit = res->limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000722 }
723
724 /* Look through the tree for fixed resources and update the limits. */
725 constrain_resources(dev, &limits);
726
727 /* Update dev's resources with new limits. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000728 for (res = dev->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000729 if ((res->flags & IORESOURCE_FIXED))
730 continue;
731
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +0200732 lim = resource_limit(&limits, res);
733 if (!lim)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000734 continue;
735
Myles Watson29cc9ed2009-07-02 18:56:24 +0000736 /* Is the resource outside the limits? */
737 if (lim->base > res->base)
738 res->base = lim->base;
739 if (res->limit > lim->limit)
740 res->limit = lim->limit;
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200741
Kyösti Mälkki634899c2015-03-23 14:22:22 +0200742 /* MEM resources need to start at the highest address manageable. */
743 if (res->flags & IORESOURCE_MEM)
744 res->base = resource_max(res);
745
Kyösti Mälkkie6a9290f2015-03-23 19:37:38 +0200746 printk(BIOS_SPEW, "%s:@%s %02lx base %08llx limit %08llx\n",
747 __func__, dev_path(dev), res->index, res->base, res->limit);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000748 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000749}
arch import user (historical)dc811182005-07-06 17:16:09 +0000750
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000751device_t vga_pri = 0;
Myles Watsonc7233e02009-07-02 19:02:33 +0000752static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000753{
Uwe Hermann312673c2009-10-27 21:49:33 +0000754 /*
Martin Roth63373ed2013-07-08 16:24:19 -0600755 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000756 * This function knows too much about PCI stuff, it should be just
757 * an iterator/visitor.
758 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000759
Myles Watson29cc9ed2009-07-02 18:56:24 +0000760 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200761 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000762 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000763
Eric Biedermanb78c1972004-10-14 20:54:17 +0000764 bus = 0;
765 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000766 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000767
Patrick Georgi557ecf22012-07-20 12:16:17 +0200768 dev = NULL;
769 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000770 if (!dev->enabled)
771 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000772
Patrick Georgi557ecf22012-07-20 12:16:17 +0200773 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000774
Patrick Georgi557ecf22012-07-20 12:16:17 +0200775 if (dev->on_mainboard) {
776 vga_onboard = dev;
Kostr1f0d3792012-10-06 13:27:58 +0400777 } else {
Patrick Georgi557ecf22012-07-20 12:16:17 +0200778 vga = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000779 }
Myles Watson032a9652009-05-11 22:24:53 +0000780
Patrick Georgi557ecf22012-07-20 12:16:17 +0200781 /* It isn't safe to enable all VGA cards. */
782 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200783 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000784
Uwe Hermanne4870472010-11-04 23:23:47 +0000785 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000786 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200787
788 if (CONFIG_ONBOARD_VGA_IS_PRIMARY && vga_onboard)
789 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000790
Patrick Georgi5869fa22012-07-20 12:29:33 +0200791 /* If we prefer plugin VGA over chipset VGA, the chipset might
792 want to know. */
793 if (!CONFIG_ONBOARD_VGA_IS_PRIMARY && (vga != vga_onboard) &&
794 vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) {
795 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
796 vga_onboard->ops->disable(vga_onboard);
797 }
798
arch import user (historical)dc811182005-07-06 17:16:09 +0000799 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000800 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000801 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000802 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000803 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
804 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000805 bus = vga->bus;
806 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000807
Myles Watson29cc9ed2009-07-02 18:56:24 +0000808 /* Now walk up the bridges setting the VGA enable. */
809 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000810 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000811 dev_path(bus->dev));
Li-Ta Lodb7f47c2004-01-08 21:15:49 +0000812 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000813 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000814 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000815}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000816
Li-Ta Lo04930692004-11-25 17:37:19 +0000817/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000818 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000819 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000820 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000821 * resources to hardware. For bridge devices, the set_resources() method
822 * has to recurse into every down stream buses.
823 *
824 * Mutual recursion:
825 * assign_resources() -> device_operation::set_resources()
826 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000827 *
828 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000829 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000830void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000831{
832 struct device *curdev;
833
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000834 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000835 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000836
Myles Watson29cc9ed2009-07-02 18:56:24 +0000837 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000838 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000839 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000840
Eric Biedermane9a271e32003-09-02 03:36:25 +0000841 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000842 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000843 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000844 continue;
845 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700846 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000847 curdev->ops->set_resources(curdev);
848 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700849 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000850 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000851 dev_path(bus->dev), bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000852}
853
Li-Ta Lo5782d272004-04-26 17:51:20 +0000854/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000855 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000856 *
857 * Enable resources of the device by calling the device specific
858 * enable_resources() method.
859 *
860 * The parent's resources should be enabled first to avoid having enabling
861 * order problem. This is done by calling the parent's enable_resources()
Martin Roth63373ed2013-07-08 16:24:19 -0600862 * method before its children's enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000863 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000864 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000865 */
Myles Watson7eac4452010-06-17 16:16:56 +0000866static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000867{
Myles Watson7eac4452010-06-17 16:16:56 +0000868 struct device *dev;
869 struct bus *c_link;
870
871 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700872 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
873 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000874 dev->ops->enable_resources(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700875 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000876 }
Myles Watson7eac4452010-06-17 16:16:56 +0000877
878 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000879 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000880 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000881 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700882 post_log_clear();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000883}
884
Myles Watson032a9652009-05-11 22:24:53 +0000885/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000886 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
887 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000888 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000889 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000890 */
891int reset_bus(struct bus *bus)
892{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000893 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000894 bus->dev->ops->reset_bus(bus);
895 bus->reset_needed = 0;
896 return 1;
897 }
898 return 0;
899}
900
Myles Watson032a9652009-05-11 22:24:53 +0000901/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000902 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000903 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000904 * If there are bridges on the bus, recursively scan the buses behind the
905 * bridges. If the setting up and tuning of the bus causes a reset to be
906 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000907 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000908 * @param busdev Pointer to the bus device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000909 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200910static void scan_bus(struct device *busdev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000911{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000912 int do_scan_bus;
Paul Menzel662380f2015-10-20 10:21:08 +0200913 struct stopwatch sw;
914
915 stopwatch_init(&sw);
Uwe Hermanne4870472010-11-04 23:23:47 +0000916
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200917 if (!busdev->enabled)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200918 return;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200919
920 printk(BIOS_SPEW, "%s scanning...\n", dev_path(busdev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000921
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700922 post_log_path(busdev);
923
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000924 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000925 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000926 struct bus *link;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200927 busdev->ops->scan_bus(busdev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000928 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000929 for (link = busdev->link_list; link; link = link->next) {
930 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000931 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000932 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000933 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000934 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000935 }
936 }
937 }
Paul Menzel662380f2015-10-20 10:21:08 +0200938
939 printk(BIOS_DEBUG, "%s: scanning of bus %s took %ld usecs\n",
940 __func__, dev_path(busdev), stopwatch_duration_usecs(&sw));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000941}
942
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200943void scan_bridges(struct bus *bus)
944{
945 struct device *child;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200946
947 for (child = bus->children; child; child = child->sibling) {
948 if (!child->ops || !child->ops->scan_bus)
949 continue;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200950 scan_bus(child);
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200951 }
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200952}
953
Li-Ta Loe5266692004-03-23 21:28:05 +0000954/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000955 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000956 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000957 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000958 * file. The device structures for these devices are generated at compile
959 * time by the config tool and are organized into the device tree. This
960 * function determines if the devices created at compile time actually exist
961 * in the physical system.
962 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000963 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000964 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000965 * device tree.
966 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000967 * This function starts from the root device 'dev_root', scans the buses in
968 * the system recursively, and modifies the device tree according to the
969 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000970 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000971 * This function has no idea how to scan and probe buses and devices at all.
972 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000973 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000974 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000975 */
976void dev_enumerate(void)
977{
978 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000979
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000980 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000981
Eric Biederman8ca8d762003-04-22 19:02:15 +0000982 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000983
Uwe Hermanne4870472010-11-04 23:23:47 +0000984 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000985 printk(BIOS_SPEW, "Compare with tree...\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000986 show_devs_tree(root, BIOS_SPEW, 0, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000987
Stefan Reinauera675d492012-08-07 14:50:47 -0700988 if (root->chip_ops && root->chip_ops->enable_dev)
989 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000990
Eric Biedermanb78c1972004-10-14 20:54:17 +0000991 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000992 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000993 return;
994 }
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200995 scan_bus(root);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700996 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000997 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000998}
999
Li-Ta Loe5266692004-03-23 21:28:05 +00001000/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001001 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +00001002 *
Li-Ta Lo04930692004-11-25 17:37:19 +00001003 * Starting at the root of the device tree, travel it recursively in two
1004 * passes. In the first pass, we compute and allocate resources (ranges)
Martin Roth63373ed2013-07-08 16:24:19 -06001005 * required by each device. In the second pass, the resources ranges are
Li-Ta Lo04930692004-11-25 17:37:19 +00001006 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +00001007 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001008 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +00001009 *
1010 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +00001011 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001012 */
1013void dev_configure(void)
1014{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001015 struct resource *res;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001016 struct device *root;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001017 struct device *child;
Li-Ta Loe5266692004-03-23 21:28:05 +00001018
Marc Jones80bd74c2012-02-21 17:44:35 +01001019 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +01001020
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001021 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001022
Eric Biedermanb78c1972004-10-14 20:54:17 +00001023 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +00001024
Uwe Hermanne4870472010-11-04 23:23:47 +00001025 /*
1026 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +00001027 * space for IO, MEM, and PREFMEM resources in the domain. The
1028 * allocation of device resources will be done from this address space.
1029 */
Myles Watsoncd5d7562009-05-12 13:43:34 +00001030
Myles Watson29cc9ed2009-07-02 18:56:24 +00001031 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +00001032
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001033 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001034 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001035 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001036
Stefan Reinauer39e72292009-10-26 16:47:05 +00001037 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +00001038
Myles Watson29cc9ed2009-07-02 18:56:24 +00001039 /* Compute resources for all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001040 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001041 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001042 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001043 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001044 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001045 if (res->flags & IORESOURCE_FIXED)
1046 continue;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001047 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001048 compute_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001049 res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001050 continue;
1051 }
1052 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001053 compute_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001054 res, IORESOURCE_TYPE_MASK, IORESOURCE_IO);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001055 continue;
1056 }
1057 }
1058 }
1059
1060 /* For all domains. */
Myles Watson894a3472010-06-09 22:41:35 +00001061 for (child = root->link_list->children; child; child=child->sibling)
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001062 if (child->path.type == DEVICE_PATH_DOMAIN)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001063 avoid_fixed_resources(child);
1064
Eric Biedermanb78c1972004-10-14 20:54:17 +00001065 /* Store the computed resource allocations into device registers ... */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001066 printk(BIOS_INFO, "Setting resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +00001067 for (child = root->link_list->children; child; child = child->sibling) {
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001068 if (!(child->path.type == DEVICE_PATH_DOMAIN))
Myles Watson29cc9ed2009-07-02 18:56:24 +00001069 continue;
Duncan Laurie7ed39762013-07-09 10:46:52 -07001070 post_log_path(child);
Myles Watsonc25cc112010-05-21 14:33:48 +00001071 for (res = child->resource_list; res; res = res->next) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001072 if (res->flags & IORESOURCE_FIXED)
1073 continue;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001074 if (res->flags & IORESOURCE_MEM) {
Myles Watson894a3472010-06-09 22:41:35 +00001075 allocate_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001076 res, IORESOURCE_TYPE_MASK, IORESOURCE_MEM);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001077 continue;
1078 }
1079 if (res->flags & IORESOURCE_IO) {
Myles Watson894a3472010-06-09 22:41:35 +00001080 allocate_resources(child->link_list,
Kyösti Mälkkifdc0a902015-03-26 20:04:38 +02001081 res, IORESOURCE_TYPE_MASK, IORESOURCE_IO);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001082 continue;
1083 }
1084 }
1085 }
Myles Watson894a3472010-06-09 22:41:35 +00001086 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001087 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001088 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +00001089
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001090 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001091}
1092
Li-Ta Loe5266692004-03-23 21:28:05 +00001093/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001094 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +00001095 *
1096 * Starting at the root, walk the tree and enable all devices/bridges by
1097 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001098 */
1099void dev_enable(void)
1100{
Myles Watson7eac4452010-06-17 16:16:56 +00001101 struct bus *link;
1102
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001103 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001104
Uwe Hermanne4870472010-11-04 23:23:47 +00001105 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001106 for (link = dev_root.link_list; link; link = link->next)
1107 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +00001108
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001109 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001110}
1111
Li-Ta Loe5266692004-03-23 21:28:05 +00001112/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001113 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +00001114 *
Uwe Hermanne4870472010-11-04 23:23:47 +00001115 * The parent should be initialized first to avoid having an ordering problem.
Martin Roth63373ed2013-07-08 16:24:19 -06001116 * This is done by calling the parent's init() method before its children's
Uwe Hermanne4870472010-11-04 23:23:47 +00001117 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +00001118 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001119 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +00001120 */
1121static void init_dev(struct device *dev)
1122{
Uwe Hermanne4870472010-11-04 23:23:47 +00001123 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +00001124 return;
Myles Watson7eac4452010-06-17 16:16:56 +00001125
1126 if (!dev->initialized && dev->ops && dev->ops->init) {
Aaron Durbin05294292013-04-30 15:41:13 -05001127#if CONFIG_HAVE_MONOTONIC_TIMER
Aaron Durbin46ba4802014-09-23 16:34:40 -05001128 struct stopwatch sw;
1129 stopwatch_init(&sw);
Aaron Durbin05294292013-04-30 15:41:13 -05001130#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001131 if (dev->path.type == DEVICE_PATH_I2C) {
1132 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1133 dev_path(dev->bus->dev), dev->bus->link_num);
1134 }
1135
Paul Menzeldb232152015-06-07 20:28:17 +02001136 printk(BIOS_DEBUG, "%s init ...\n", dev_path(dev));
Myles Watson7eac4452010-06-17 16:16:56 +00001137 dev->initialized = 1;
1138 dev->ops->init(dev);
Aaron Durbin05294292013-04-30 15:41:13 -05001139#if CONFIG_HAVE_MONOTONIC_TIMER
Paul Menzeldb232152015-06-07 20:28:17 +02001140 printk(BIOS_DEBUG, "%s init finished in %ld usecs\n", dev_path(dev),
Aaron Durbin46ba4802014-09-23 16:34:40 -05001141 stopwatch_duration_usecs(&sw));
Aaron Durbin05294292013-04-30 15:41:13 -05001142#endif
Myles Watson7eac4452010-06-17 16:16:56 +00001143 }
1144}
1145
1146static void init_link(struct bus *link)
1147{
1148 struct device *dev;
1149 struct bus *c_link;
1150
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001151 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Lauriecb73a842013-06-10 10:41:04 -07001152 post_code(POST_BS_DEV_INIT);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001153 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +00001154 init_dev(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001155 }
Myles Watson7eac4452010-06-17 16:16:56 +00001156
1157 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001158 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +00001159 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +00001160 }
1161}
1162
1163/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001164 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +00001165 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001166 * Starting at the root device, call the device's init() method to do
1167 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001168 */
1169void dev_initialize(void)
1170{
Myles Watson7eac4452010-06-17 16:16:56 +00001171 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001172
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001173 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +00001174
Duncan Laurieb4aaaa72012-01-17 09:03:11 -08001175#if CONFIG_ARCH_X86
1176 /* Ensure EBDA is prepared before Option ROMs. */
1177 setup_default_ebda();
1178#endif
1179
Myles Watson1bd3fb72010-08-16 16:25:23 +00001180 /* First call the mainboard init. */
1181 init_dev(&dev_root);
1182
Uwe Hermanne4870472010-11-04 23:23:47 +00001183 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +00001184 for (link = dev_root.link_list; link; link = link->next)
1185 init_link(link);
Duncan Laurie8adf7a22013-06-10 10:34:20 -07001186 post_log_clear();
Myles Watson7eac4452010-06-17 16:16:56 +00001187
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001188 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +00001189 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +00001190}
Marc Jones2a58ecd2013-10-29 17:32:00 -06001191
1192/**
1193 * Finalize a specific device.
1194 *
1195 * The parent should be finalized first to avoid having an ordering problem.
1196 * This is done by calling the parent's final() method before its childrens'
1197 * final() methods.
1198 *
1199 * @param dev The device to be initialized.
1200 */
1201static void final_dev(struct device *dev)
1202{
1203 if (!dev->enabled)
1204 return;
1205
1206 if (dev->ops && dev->ops->final) {
1207 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
1208 dev->ops->final(dev);
1209 }
1210}
1211
1212static void final_link(struct bus *link)
1213{
1214 struct device *dev;
1215 struct bus *c_link;
1216
1217 for (dev = link->children; dev; dev = dev->sibling)
1218 final_dev(dev);
1219
1220 for (dev = link->children; dev; dev = dev->sibling) {
1221 for (c_link = dev->link_list; c_link; c_link = c_link->next)
1222 final_link(c_link);
1223 }
1224}
1225/**
1226 * Finalize all devices in the global device tree.
1227 *
1228 * Starting at the root device, call the device's final() method to do
1229 * device-specific cleanup, then call each child's final() method.
1230 */
1231void dev_finalize(void)
1232{
1233 struct bus *link;
1234
1235 printk(BIOS_INFO, "Finalize devices...\n");
1236
1237 /* First call the mainboard finalize. */
1238 final_dev(&dev_root);
1239
1240 /* Now finalize everything. */
1241 for (link = dev_root.link_list; link; link = link->next)
1242 final_link(link);
1243
1244 printk(BIOS_INFO, "Devices finalized\n");
1245}