Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
| 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 Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 15 | * Copyright (C) 2009 Myles Watson <mylesgw@gmail.com> |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 19 | * (c) 1999--2000 Martin Mares <mj@suse.cz> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 20 | */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 21 | |
| 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 Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 26 | * 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 Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | * it with multiple passes, until I actually started doing it and saw |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 29 | * the wisdom of Tom's recommendations... |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | * |
| 31 | * Lots of cleanups by Eric Biederman to handle bridges, and to |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 32 | * handle resource allocation for non-PCI devices. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include <console/console.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 36 | #include <arch/io.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 37 | #include <device/device.h> |
| 38 | #include <device/pci.h> |
Li-Ta Lo | 54f05f6 | 2004-05-14 17:20:29 +0000 | [diff] [blame] | 39 | #include <device/pci_ids.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 42 | #include <smp/spinlock.h> |
Duncan Laurie | b4aaaa7 | 2012-01-17 09:03:11 -0800 | [diff] [blame] | 43 | #if CONFIG_ARCH_X86 |
| 44 | #include <arch/ebda.h> |
| 45 | #endif |
Aaron Durbin | 0529429 | 2013-04-30 15:41:13 -0500 | [diff] [blame] | 46 | #include <timer.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 47 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 48 | /** Linked list of ALL devices */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 49 | struct device *all_devices = &dev_root; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 50 | /** Pointer to the last device */ |
Myles Watson | 70679a0 | 2010-09-01 21:03:03 +0000 | [diff] [blame] | 51 | extern struct device *last_dev; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 52 | /** Linked list of free resources */ |
| 53 | struct resource *free_resources = NULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 54 | |
Nico Huber | acd7d95 | 2012-07-25 10:33:05 +0200 | [diff] [blame] | 55 | /** |
| 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 | */ |
| 61 | void dev_initialize_chips(void) |
| 62 | { |
| 63 | struct device *dev; |
| 64 | |
| 65 | for (dev = all_devices; dev; dev = dev->next) { |
| 66 | /* Initialize chip if we haven't yet. */ |
| 67 | if (dev->chip_ops && dev->chip_ops->init && |
| 68 | !dev->chip_ops->initialized) { |
| 69 | dev->chip_ops->init(dev->chip_info); |
| 70 | dev->chip_ops->initialized = 1; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 75 | DECLARE_SPIN_LOCK(dev_lock) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 76 | |
Kyösti Mälkki | b25374c | 2012-08-01 08:05:22 +0300 | [diff] [blame] | 77 | #if CONFIG_GFXUMA |
Kyösti Mälkki | cc55b9b | 2012-07-11 07:55:21 +0300 | [diff] [blame] | 78 | /* IGD UMA memory */ |
| 79 | uint64_t uma_memory_base = 0; |
| 80 | uint64_t uma_memory_size = 0; |
Kyösti Mälkki | b25374c | 2012-08-01 08:05:22 +0300 | [diff] [blame] | 81 | #endif |
Kyösti Mälkki | cc55b9b | 2012-07-11 07:55:21 +0300 | [diff] [blame] | 82 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 83 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 84 | * Allocate a new device structure. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 85 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 86 | * Allocte a new device structure and attach it to the device tree as a |
Li-Ta Lo | 9f0d0f9 | 2004-05-10 16:05:16 +0000 | [diff] [blame] | 87 | * child of the parent bus. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 88 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 89 | * @param parent Parent bus the newly created device should be attached to. |
| 90 | * @param path Path to the device to be created. |
| 91 | * @return Pointer to the newly created device structure. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 92 | * |
| 93 | * @see device_path |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 94 | */ |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 95 | static device_t __alloc_dev(struct bus *parent, struct device_path *path) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 96 | { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 97 | device_t dev, child; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 98 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 99 | /* Find the last child of our parent. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 100 | for (child = parent->children; child && child->sibling; /* */ ) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 101 | child = child->sibling; |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 102 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 103 | dev = malloc(sizeof(*dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 104 | if (dev == 0) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 105 | die("alloc_dev(): out of memory.\n"); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 106 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 107 | memset(dev, 0, sizeof(*dev)); |
| 108 | memcpy(&dev->path, path, sizeof(*path)); |
| 109 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 110 | /* By default devices are enabled. */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 111 | dev->enabled = 1; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 112 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 113 | /* Add the new device to the list of children of the bus. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 114 | dev->bus = parent; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 115 | if (child) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 116 | child->sibling = dev; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 117 | else |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 118 | parent->children = dev; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 119 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 120 | /* Append a new device to the global device list. |
| 121 | * The list is used to find devices once everything is set up. |
| 122 | */ |
Myles Watson | 70679a0 | 2010-09-01 21:03:03 +0000 | [diff] [blame] | 123 | last_dev->next = dev; |
| 124 | last_dev = dev; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 125 | |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 126 | return dev; |
| 127 | } |
| 128 | |
| 129 | device_t alloc_dev(struct bus *parent, struct device_path *path) |
| 130 | { |
| 131 | device_t dev; |
| 132 | spin_lock(&dev_lock); |
| 133 | dev = __alloc_dev(parent, path); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 134 | spin_unlock(&dev_lock); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 135 | return dev; |
| 136 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 137 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 138 | /** |
Kyösti Mälkki | a5650a4 | 2012-07-07 17:15:51 +0300 | [diff] [blame] | 139 | * See if a device structure already exists and if not allocate it. |
| 140 | * |
| 141 | * @param parent The bus to find the device on. |
| 142 | * @param path The relative path from the bus to the appropriate device. |
| 143 | * @return Pointer to a device structure for the device on bus at path. |
| 144 | */ |
| 145 | device_t alloc_find_dev(struct bus *parent, struct device_path *path) |
| 146 | { |
| 147 | device_t child; |
| 148 | spin_lock(&dev_lock); |
| 149 | child = find_dev_path(parent, path); |
| 150 | if (!child) |
| 151 | child = __alloc_dev(parent, path); |
| 152 | spin_unlock(&dev_lock); |
| 153 | return child; |
| 154 | } |
| 155 | |
| 156 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 157 | * Round a number up to an alignment. |
| 158 | * |
| 159 | * @param val The starting value. |
| 160 | * @param roundup Alignment as a power of two. |
| 161 | * @return Rounded up number. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 162 | */ |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 163 | static resource_t round(resource_t val, unsigned long pow) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 164 | { |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 165 | resource_t mask; |
| 166 | mask = (1ULL << pow) - 1ULL; |
| 167 | val += mask; |
| 168 | val &= ~mask; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 169 | return val; |
| 170 | } |
| 171 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 172 | /** |
| 173 | * Read the resources on all devices of a given bus. |
| 174 | * |
| 175 | * @param bus Bus to read the resources on. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 176 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 177 | static void read_resources(struct bus *bus) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 178 | { |
| 179 | struct device *curdev; |
| 180 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 181 | printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev), |
| 182 | __func__, bus->secondary, bus->link_num); |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 183 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 184 | /* Walk through all devices and find which resources they need. */ |
| 185 | for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 186 | struct bus *link; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 187 | |
| 188 | if (!curdev->enabled) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 189 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 190 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 191 | if (!curdev->ops || !curdev->ops->read_resources) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 192 | printk(BIOS_ERR, "%s missing read_resources\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 193 | dev_path(curdev)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 194 | continue; |
| 195 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 196 | curdev->ops->read_resources(curdev); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 197 | |
| 198 | /* Read in the resources behind the current device's links. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 199 | for (link = curdev->link_list; link; link = link->next) |
| 200 | read_resources(link); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 201 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 202 | printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 203 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 206 | struct pick_largest_state { |
| 207 | struct resource *last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 208 | struct device *result_dev; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 209 | struct resource *result; |
| 210 | int seen_last; |
| 211 | }; |
| 212 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 213 | static void pick_largest_resource(void *gp, struct device *dev, |
| 214 | struct resource *resource) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 215 | { |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 216 | struct pick_largest_state *state = gp; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 217 | struct resource *last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 218 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 219 | last = state->last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 220 | |
| 221 | /* Be certain to pick the successor to last. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 222 | if (resource == last) { |
| 223 | state->seen_last = 1; |
| 224 | return; |
| 225 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 226 | if (resource->flags & IORESOURCE_FIXED) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 227 | return; /* Skip it. */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 228 | if (last && ((last->align < resource->align) || |
| 229 | ((last->align == resource->align) && |
| 230 | (last->size < resource->size)) || |
| 231 | ((last->align == resource->align) && |
| 232 | (last->size == resource->size) && (!state->seen_last)))) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 233 | return; |
| 234 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 235 | if (!state->result || |
| 236 | (state->result->align < resource->align) || |
| 237 | ((state->result->align == resource->align) && |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 238 | (state->result->size < resource->size))) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 239 | state->result_dev = dev; |
| 240 | state->result = resource; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 241 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 244 | static struct device *largest_resource(struct bus *bus, |
| 245 | struct resource **result_res, |
| 246 | unsigned long type_mask, |
| 247 | unsigned long type) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 248 | { |
| 249 | struct pick_largest_state state; |
| 250 | |
| 251 | state.last = *result_res; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 252 | state.result_dev = NULL; |
| 253 | state.result = NULL; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 254 | state.seen_last = 0; |
| 255 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 256 | search_bus_resources(bus, type_mask, type, pick_largest_resource, |
| 257 | &state); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 258 | |
| 259 | *result_res = state.result; |
| 260 | return state.result_dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 263 | /** |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 264 | * This function is the guts of the resource allocator. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 265 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 266 | * The problem. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 267 | * - Allocate resource locations for every device. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 268 | * - Don't overlap, and follow the rules of bridges. |
| 269 | * - Don't overlap with resources in fixed locations. |
| 270 | * - Be efficient so we don't have ugly strategies. |
| 271 | * |
| 272 | * The strategy. |
| 273 | * - Devices that have fixed addresses are the minority so don't |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 274 | * worry about them too much. Instead only use part of the address |
| 275 | * space for devices with programmable addresses. This easily handles |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 276 | * everything except bridges. |
| 277 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 278 | * - PCI devices are required to have their sizes and their alignments |
| 279 | * equal. In this case an optimal solution to the packing problem |
| 280 | * exists. Allocate all devices from highest alignment to least |
| 281 | * alignment or vice versa. Use this. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 282 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 283 | * - So we can handle more than PCI run two allocation passes on bridges. The |
| 284 | * first to see how large the resources are behind the bridge, and what |
| 285 | * their alignment requirements are. The second to assign a safe address to |
| 286 | * the devices behind the bridge. This allows us to treat a bridge as just |
| 287 | * a device with a couple of resources, and not need to special case it in |
| 288 | * the allocator. Also this allows handling of other types of bridges. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 289 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 290 | * @param bus The bus we are traversing. |
| 291 | * @param bridge The bridge resource which must contain the bus' resources. |
| 292 | * @param type_mask This value gets ANDed with the resource type. |
| 293 | * @param type This value must match the result of the AND. |
| 294 | * @return TODO |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 295 | */ |
Myles Watson | 54913b9 | 2009-10-13 20:00:09 +0000 | [diff] [blame] | 296 | static void compute_resources(struct bus *bus, struct resource *bridge, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 297 | unsigned long type_mask, unsigned long type) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 298 | { |
| 299 | struct device *dev; |
| 300 | struct resource *resource; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 301 | resource_t base; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 302 | base = round(bridge->base, bridge->align); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 303 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 304 | printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d" |
| 305 | " limit: %llx\n", dev_path(bus->dev), __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 306 | (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ? |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 307 | "prefmem" : "mem", base, bridge->size, bridge->align, |
| 308 | bridge->gran, bridge->limit); |
Ronald G. Minnich | 99dcf23 | 2003-09-30 02:16:47 +0000 | [diff] [blame] | 309 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 310 | /* For each child which is a bridge, compute the resource needs. */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 311 | for (dev = bus->children; dev; dev = dev->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 312 | struct resource *child_bridge; |
| 313 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 314 | if (!dev->link_list) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 315 | continue; |
| 316 | |
| 317 | /* Find the resources with matching type flags. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 318 | for (child_bridge = dev->resource_list; child_bridge; |
| 319 | child_bridge = child_bridge->next) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 320 | struct bus* link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 321 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 322 | if (!(child_bridge->flags & IORESOURCE_BRIDGE) |
| 323 | || (child_bridge->flags & type_mask) != type) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 324 | continue; |
| 325 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 326 | /* |
| 327 | * Split prefetchable memory if combined. Many domains |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 328 | * use the same address space for prefetchable memory |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 329 | * and non-prefetchable memory. Bridges below them need |
| 330 | * it separated. Add the PREFETCH flag to the type_mask |
| 331 | * and type. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 332 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 333 | link = dev->link_list; |
| 334 | while (link && link->link_num != |
| 335 | IOINDEX_LINK(child_bridge->index)) |
| 336 | link = link->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 337 | |
| 338 | if (link == NULL) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 339 | printk(BIOS_ERR, "link %ld not found on %s\n", |
| 340 | IOINDEX_LINK(child_bridge->index), |
| 341 | dev_path(dev)); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 344 | compute_resources(link, child_bridge, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 345 | type_mask | IORESOURCE_PREFETCH, |
| 346 | type | (child_bridge->flags & |
| 347 | IORESOURCE_PREFETCH)); |
| 348 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 351 | /* Remember we haven't found anything yet. */ |
| 352 | resource = NULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 353 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 354 | /* |
| 355 | * Walk through all the resources on the current bus and compute the |
| 356 | * amount of address space taken by them. Take granularity and |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 357 | * alignment into account. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 358 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 359 | while ((dev = largest_resource(bus, &resource, type_mask, type))) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 360 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 361 | /* Size 0 resources can be skipped. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 362 | if (!resource->size) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 363 | continue; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 364 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 365 | /* Propagate the resource alignment to the bridge resource. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 366 | if (resource->align > bridge->align) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 367 | bridge->align = resource->align; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 368 | |
| 369 | /* Propagate the resource limit to the bridge register. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 370 | if (bridge->limit > resource->limit) |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 371 | bridge->limit = resource->limit; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 372 | |
| 373 | /* Warn if it looks like APICs aren't declared. */ |
| 374 | if ((resource->limit == 0xffffffff) && |
| 375 | (resource->flags & IORESOURCE_ASSIGNED)) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 376 | printk(BIOS_ERR, |
| 377 | "Resource limit looks wrong! (no APIC?)\n"); |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 378 | printk(BIOS_ERR, "%s %02lx limit %08llx\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 379 | dev_path(dev), resource->index, resource->limit); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 380 | } |
Stefan Reinauer | 51754a3 | 2008-08-01 12:28:38 +0000 | [diff] [blame] | 381 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 382 | if (resource->flags & IORESOURCE_IO) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 383 | /* |
| 384 | * Don't allow potential aliases over the legacy PCI |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 385 | * expansion card addresses. The legacy PCI decodes |
| 386 | * only 10 bits, uses 0x100 - 0x3ff. Therefore, only |
| 387 | * 0x00 - 0xff can be used out of each 0x400 block of |
| 388 | * I/O space. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 389 | */ |
Eric Biederman | bbb6d10 | 2003-08-04 19:54:48 +0000 | [diff] [blame] | 390 | if ((base & 0x300) != 0) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 391 | base = (base & ~0x3ff) + 0x400; |
| 392 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 393 | /* |
| 394 | * Don't allow allocations in the VGA I/O range. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 395 | * PCI has special cases for that. |
| 396 | */ |
| 397 | else if ((base >= 0x3b0) && (base <= 0x3df)) { |
| 398 | base = 0x3e0; |
| 399 | } |
| 400 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 401 | /* Base must be aligned. */ |
| 402 | base = round(base, resource->align); |
| 403 | resource->base = base; |
| 404 | base += resource->size; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 405 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 406 | printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 407 | dev_path(dev), resource->index, resource->base, |
| 408 | resource->base + resource->size - 1, |
| 409 | (resource->flags & IORESOURCE_IO) ? "io" : |
| 410 | (resource->flags & IORESOURCE_PREFETCH) ? |
| 411 | "prefmem" : "mem"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 412 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * A PCI bridge resource does not need to be a power of two size, but |
| 416 | * it does have a minimum granularity. Round the size up to that |
| 417 | * minimum granularity so we know not to place something else at an |
| 418 | * address postitively decoded by the bridge. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 419 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 420 | bridge->size = round(base, bridge->gran) - |
| 421 | round(bridge->base, bridge->align); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 422 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 423 | printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d" |
| 424 | " limit: %llx done\n", dev_path(bus->dev), __func__, |
| 425 | (bridge->flags & IORESOURCE_IO) ? "io" : |
| 426 | (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem", |
| 427 | base, bridge->size, bridge->align, bridge->gran, bridge->limit); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
| 431 | * This function is the second part of the resource allocator. |
| 432 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 433 | * See the compute_resources function for a more detailed explanation. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 434 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 435 | * This function assigns the resources a value. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 436 | * |
| 437 | * @param bus The bus we are traversing. |
| 438 | * @param bridge The bridge resource which must contain the bus' resources. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 439 | * @param type_mask This value gets ANDed with the resource type. |
| 440 | * @param type This value must match the result of the AND. |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 441 | * |
| 442 | * @see compute_resources |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 443 | */ |
Myles Watson | 54913b9 | 2009-10-13 20:00:09 +0000 | [diff] [blame] | 444 | static void allocate_resources(struct bus *bus, struct resource *bridge, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 445 | unsigned long type_mask, unsigned long type) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 446 | { |
| 447 | struct device *dev; |
| 448 | struct resource *resource; |
| 449 | resource_t base; |
| 450 | base = bridge->base; |
| 451 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 452 | printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d " |
| 453 | "limit:%llx\n", dev_path(bus->dev), __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 454 | (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ? |
| 455 | "prefmem" : "mem", |
| 456 | base, bridge->size, bridge->align, bridge->gran, bridge->limit); |
| 457 | |
| 458 | /* Remember we haven't found anything yet. */ |
| 459 | resource = NULL; |
| 460 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 461 | /* |
| 462 | * Walk through all the resources on the current bus and allocate them |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 463 | * address space. |
| 464 | */ |
| 465 | while ((dev = largest_resource(bus, &resource, type_mask, type))) { |
| 466 | |
| 467 | /* Propagate the bridge limit to the resource register. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 468 | if (resource->limit > bridge->limit) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 469 | resource->limit = bridge->limit; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 470 | |
| 471 | /* Size 0 resources can be skipped. */ |
| 472 | if (!resource->size) { |
| 473 | /* Set the base to limit so it doesn't confuse tolm. */ |
| 474 | resource->base = resource->limit; |
| 475 | resource->flags |= IORESOURCE_ASSIGNED; |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | if (resource->flags & IORESOURCE_IO) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 480 | /* |
| 481 | * Don't allow potential aliases over the legacy PCI |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 482 | * expansion card addresses. The legacy PCI decodes |
| 483 | * only 10 bits, uses 0x100 - 0x3ff. Therefore, only |
| 484 | * 0x00 - 0xff can be used out of each 0x400 block of |
| 485 | * I/O space. |
| 486 | */ |
| 487 | if ((base & 0x300) != 0) { |
| 488 | base = (base & ~0x3ff) + 0x400; |
| 489 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 490 | /* |
| 491 | * Don't allow allocations in the VGA I/O range. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 492 | * PCI has special cases for that. |
| 493 | */ |
| 494 | else if ((base >= 0x3b0) && (base <= 0x3df)) { |
| 495 | base = 0x3e0; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if ((round(base, resource->align) + resource->size - 1) <= |
| 500 | resource->limit) { |
| 501 | /* Base must be aligned. */ |
| 502 | base = round(base, resource->align); |
| 503 | resource->base = base; |
| 504 | resource->flags |= IORESOURCE_ASSIGNED; |
| 505 | resource->flags &= ~IORESOURCE_STORED; |
| 506 | base += resource->size; |
| 507 | } else { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 508 | printk(BIOS_ERR, "!! Resource didn't fit !!\n"); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 509 | printk(BIOS_ERR, " aligned base %llx size %llx " |
| 510 | "limit %llx\n", round(base, resource->align), |
| 511 | resource->size, resource->limit); |
| 512 | printk(BIOS_ERR, " %llx needs to be <= %llx " |
| 513 | "(limit)\n", (round(base, resource->align) + |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 514 | resource->size) - 1, resource->limit); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 515 | printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]" |
| 516 | " %s\n", (resource->flags & IORESOURCE_ASSIGNED) |
| 517 | ? "Assigned: " : "", dev_path(dev), |
| 518 | resource->index, resource->base, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 519 | resource->base + resource->size - 1, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 520 | (resource->flags & IORESOURCE_IO) ? "io" |
| 521 | : (resource->flags & IORESOURCE_PREFETCH) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 522 | ? "prefmem" : "mem"); |
| 523 | } |
| 524 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 525 | printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n", |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 526 | (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: " |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 527 | : "", dev_path(dev), resource->index, resource->base, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 528 | resource->size ? resource->base + resource->size - 1 : |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 529 | resource->base, (resource->flags & IORESOURCE_IO) |
| 530 | ? "io" : (resource->flags & IORESOURCE_PREFETCH) |
| 531 | ? "prefmem" : "mem"); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 532 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * A PCI bridge resource does not need to be a power of two size, but |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 536 | * it does have a minimum granularity. Round the size up to that |
| 537 | * minimum granularity so we know not to place something else at an |
| 538 | * address positively decoded by the bridge. |
| 539 | */ |
| 540 | |
| 541 | bridge->flags |= IORESOURCE_ASSIGNED; |
| 542 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 543 | printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d " |
| 544 | "gran: %d done\n", dev_path(bus->dev), __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 545 | (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ? |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 546 | "prefmem" : "mem", base, bridge->size, bridge->align, |
| 547 | bridge->gran); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 548 | |
| 549 | /* For each child which is a bridge, allocate_resources. */ |
| 550 | for (dev = bus->children; dev; dev = dev->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 551 | struct resource *child_bridge; |
| 552 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 553 | if (!dev->link_list) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 554 | continue; |
| 555 | |
| 556 | /* Find the resources with matching type flags. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 557 | for (child_bridge = dev->resource_list; child_bridge; |
| 558 | child_bridge = child_bridge->next) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 559 | struct bus* link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 560 | |
| 561 | if (!(child_bridge->flags & IORESOURCE_BRIDGE) || |
| 562 | (child_bridge->flags & type_mask) != type) |
| 563 | continue; |
| 564 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 565 | /* |
| 566 | * Split prefetchable memory if combined. Many domains |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 567 | * use the same address space for prefetchable memory |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 568 | * and non-prefetchable memory. Bridges below them need |
| 569 | * it separated. Add the PREFETCH flag to the type_mask |
| 570 | * and type. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 571 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 572 | link = dev->link_list; |
| 573 | while (link && link->link_num != |
| 574 | IOINDEX_LINK(child_bridge->index)) |
| 575 | link = link->next; |
| 576 | if (link == NULL) |
| 577 | printk(BIOS_ERR, "link %ld not found on %s\n", |
| 578 | IOINDEX_LINK(child_bridge->index), |
| 579 | dev_path(dev)); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 580 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 581 | allocate_resources(link, child_bridge, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 582 | type_mask | IORESOURCE_PREFETCH, |
| 583 | type | (child_bridge->flags & |
| 584 | IORESOURCE_PREFETCH)); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
Patrick Georgi | e166782 | 2012-05-05 15:29:32 +0200 | [diff] [blame] | 589 | #if CONFIG_PCI_64BIT_PREF_MEM |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 590 | #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 591 | #else |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 592 | #define MEM_MASK (IORESOURCE_MEM) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 593 | #endif |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 594 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 595 | #define IO_MASK (IORESOURCE_IO) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 596 | #define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 597 | #define MEM_TYPE (IORESOURCE_MEM) |
| 598 | #define IO_TYPE (IORESOURCE_IO) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 599 | |
| 600 | struct constraints { |
| 601 | struct resource pref, io, mem; |
| 602 | }; |
| 603 | |
| 604 | static void constrain_resources(struct device *dev, struct constraints* limits) |
| 605 | { |
| 606 | struct device *child; |
| 607 | struct resource *res; |
| 608 | struct resource *lim; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 609 | struct bus *link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 610 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 611 | printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 612 | |
| 613 | /* Constrain limits based on the fixed resources of this device. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 614 | for (res = dev->resource_list; res; res = res->next) { |
Patrick Georgi | 18c585b | 2009-08-28 12:48:02 +0000 | [diff] [blame] | 615 | if (!(res->flags & IORESOURCE_FIXED)) |
| 616 | continue; |
Myles Watson | ce9d864 | 2009-08-19 19:12:39 +0000 | [diff] [blame] | 617 | if (!res->size) { |
| 618 | /* It makes no sense to have 0-sized, fixed resources.*/ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 619 | printk(BIOS_ERR, "skipping %s@%lx fixed resource, " |
| 620 | "size=0!\n", dev_path(dev), res->index); |
Patrick Georgi | 6bd93f4 | 2009-08-19 17:29:41 +0000 | [diff] [blame] | 621 | continue; |
Myles Watson | ce9d864 | 2009-08-19 19:12:39 +0000 | [diff] [blame] | 622 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 623 | |
| 624 | /* PREFETCH, MEM, or I/O - skip any others. */ |
| 625 | if ((res->flags & MEM_MASK) == PREF_TYPE) |
| 626 | lim = &limits->pref; |
| 627 | else if ((res->flags & MEM_MASK) == MEM_TYPE) |
| 628 | lim = &limits->mem; |
| 629 | else if ((res->flags & IO_MASK) == IO_TYPE) |
| 630 | lim = &limits->io; |
| 631 | else |
| 632 | continue; |
| 633 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 634 | /* |
| 635 | * Is it a fixed resource outside the current known region? |
| 636 | * If so, we don't have to consider it - it will be handled |
| 637 | * correctly and doesn't affect current region's limits. |
| 638 | */ |
| 639 | if (((res->base + res->size -1) < lim->base) |
| 640 | || (res->base > lim->limit)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 641 | continue; |
| 642 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 643 | /* |
| 644 | * Choose to be above or below fixed resources. This check is |
| 645 | * signed so that "negative" amounts of space are handled |
| 646 | * correctly. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 647 | */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 648 | if ((signed long long)(lim->limit - (res->base + res->size -1)) |
| 649 | > (signed long long)(res->base - lim->base)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 650 | lim->base = res->base + res->size; |
| 651 | else |
| 652 | lim->limit = res->base -1; |
| 653 | } |
| 654 | |
| 655 | /* Descend into every enabled child and look for fixed resources. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 656 | for (link = dev->link_list; link; link = link->next) { |
| 657 | for (child = link->children; child; child = child->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 658 | if (child->enabled) |
| 659 | constrain_resources(child, limits); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static void avoid_fixed_resources(struct device *dev) |
| 665 | { |
| 666 | struct constraints limits; |
| 667 | struct resource *res; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 668 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 669 | printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 670 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 671 | /* Initialize constraints to maximum size. */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 672 | limits.pref.base = 0; |
| 673 | limits.pref.limit = 0xffffffffffffffffULL; |
| 674 | limits.io.base = 0; |
| 675 | limits.io.limit = 0xffffffffffffffffULL; |
| 676 | limits.mem.base = 0; |
| 677 | limits.mem.limit = 0xffffffffffffffffULL; |
| 678 | |
| 679 | /* Constrain the limits to dev's initial resources. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 680 | for (res = dev->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 681 | if ((res->flags & IORESOURCE_FIXED)) |
| 682 | continue; |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 683 | printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 684 | dev_path(dev), res->index, res->limit); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 685 | if ((res->flags & MEM_MASK) == PREF_TYPE && |
| 686 | (res->limit < limits.pref.limit)) |
| 687 | limits.pref.limit = res->limit; |
| 688 | if ((res->flags & MEM_MASK) == MEM_TYPE && |
| 689 | (res->limit < limits.mem.limit)) |
| 690 | limits.mem.limit = res->limit; |
| 691 | if ((res->flags & IO_MASK) == IO_TYPE && |
| 692 | (res->limit < limits.io.limit)) |
| 693 | limits.io.limit = res->limit; |
| 694 | } |
| 695 | |
| 696 | /* Look through the tree for fixed resources and update the limits. */ |
| 697 | constrain_resources(dev, &limits); |
| 698 | |
| 699 | /* Update dev's resources with new limits. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 700 | for (res = dev->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 701 | struct resource *lim; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 702 | |
| 703 | if ((res->flags & IORESOURCE_FIXED)) |
| 704 | continue; |
| 705 | |
| 706 | /* PREFETCH, MEM, or I/O - skip any others. */ |
| 707 | if ((res->flags & MEM_MASK) == PREF_TYPE) |
| 708 | lim = &limits.pref; |
| 709 | else if ((res->flags & MEM_MASK) == MEM_TYPE) |
| 710 | lim = &limits.mem; |
| 711 | else if ((res->flags & IO_MASK) == IO_TYPE) |
| 712 | lim = &limits.io; |
| 713 | else |
| 714 | continue; |
| 715 | |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 716 | printk(BIOS_SPEW, "%s2: %s@%02lx limit %08llx\n", __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 717 | dev_path(dev), res->index, res->limit); |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 718 | printk(BIOS_SPEW, "\tlim->base %08llx lim->limit %08llx\n", |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 719 | lim->base, lim->limit); |
| 720 | |
| 721 | /* Is the resource outside the limits? */ |
| 722 | if (lim->base > res->base) |
| 723 | res->base = lim->base; |
| 724 | if (res->limit > lim->limit) |
| 725 | res->limit = lim->limit; |
| 726 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 727 | } |
arch import user (historical) | dc81118 | 2005-07-06 17:16:09 +0000 | [diff] [blame] | 728 | |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 729 | device_t vga_pri = 0; |
Myles Watson | c7233e0 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 730 | static void set_vga_bridge_bits(void) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 731 | { |
Uwe Hermann | 312673c | 2009-10-27 21:49:33 +0000 | [diff] [blame] | 732 | /* |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 733 | * FIXME: Modify set_vga_bridge() so it is less PCI centric! |
Uwe Hermann | 312673c | 2009-10-27 21:49:33 +0000 | [diff] [blame] | 734 | * This function knows too much about PCI stuff, it should be just |
| 735 | * an iterator/visitor. |
| 736 | */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 737 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 738 | /* FIXME: Handle the VGA palette snooping. */ |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 739 | struct device *dev, *vga, *vga_onboard; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 740 | struct bus *bus; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 741 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 742 | bus = 0; |
| 743 | vga = 0; |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 744 | vga_onboard = 0; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 745 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 746 | dev = NULL; |
| 747 | while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 748 | if (!dev->enabled) |
| 749 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 750 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 751 | printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev)); |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 752 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 753 | if (dev->on_mainboard) { |
| 754 | vga_onboard = dev; |
Kostr | 1f0d379 | 2012-10-06 13:27:58 +0400 | [diff] [blame] | 755 | } else { |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 756 | vga = dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 757 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 758 | |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 759 | /* It isn't safe to enable all VGA cards. */ |
| 760 | dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
Patrick Georgi | 594473d | 2012-07-25 08:55:53 +0200 | [diff] [blame] | 761 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 762 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 763 | if (!vga) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 764 | vga = vga_onboard; |
Patrick Georgi | 557ecf2 | 2012-07-20 12:16:17 +0200 | [diff] [blame] | 765 | |
| 766 | if (CONFIG_ONBOARD_VGA_IS_PRIMARY && vga_onboard) |
| 767 | vga = vga_onboard; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 768 | |
Patrick Georgi | 5869fa2 | 2012-07-20 12:29:33 +0200 | [diff] [blame] | 769 | /* If we prefer plugin VGA over chipset VGA, the chipset might |
| 770 | want to know. */ |
| 771 | if (!CONFIG_ONBOARD_VGA_IS_PRIMARY && (vga != vga_onboard) && |
| 772 | vga_onboard && vga_onboard->ops && vga_onboard->ops->disable) { |
| 773 | printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n"); |
| 774 | vga_onboard->ops->disable(vga_onboard); |
| 775 | } |
| 776 | |
arch import user (historical) | dc81118 | 2005-07-06 17:16:09 +0000 | [diff] [blame] | 777 | if (vga) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 778 | /* VGA is first add-on card or the only onboard VGA. */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 779 | printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 780 | /* All legacy VGA cards have MEM & I/O space registers. */ |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 781 | vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 782 | vga_pri = vga; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 783 | bus = vga->bus; |
| 784 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 785 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 786 | /* Now walk up the bridges setting the VGA enable. */ |
| 787 | while (bus) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 788 | printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 789 | dev_path(bus->dev)); |
Li-Ta Lo | db7f47c | 2004-01-08 21:15:49 +0000 | [diff] [blame] | 790 | bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 791 | bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 792 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 793 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 794 | |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 795 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 796 | * Assign the computed resources to the devices on the bus. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 797 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 798 | * Use the device specific set_resources() method to store the computed |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 799 | * resources to hardware. For bridge devices, the set_resources() method |
| 800 | * has to recurse into every down stream buses. |
| 801 | * |
| 802 | * Mutual recursion: |
| 803 | * assign_resources() -> device_operation::set_resources() |
| 804 | * device_operation::set_resources() -> assign_resources() |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 805 | * |
| 806 | * @param bus Pointer to the structure for this bus. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 807 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 808 | void assign_resources(struct bus *bus) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 809 | { |
| 810 | struct device *curdev; |
| 811 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 812 | printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 813 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 814 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 815 | for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 816 | if (!curdev->enabled || !curdev->resource_list) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 817 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 818 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 819 | if (!curdev->ops || !curdev->ops->set_resources) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 820 | printk(BIOS_ERR, "%s missing set_resources\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 821 | dev_path(curdev)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 822 | continue; |
| 823 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 824 | curdev->ops->set_resources(curdev); |
| 825 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 826 | printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 827 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 830 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 831 | * Enable the resources for devices on a link. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 832 | * |
| 833 | * Enable resources of the device by calling the device specific |
| 834 | * enable_resources() method. |
| 835 | * |
| 836 | * The parent's resources should be enabled first to avoid having enabling |
| 837 | * order problem. This is done by calling the parent's enable_resources() |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 838 | * method before its childrens' enable_resources() methods. |
Li-Ta Lo | 9f0d0f9 | 2004-05-10 16:05:16 +0000 | [diff] [blame] | 839 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 840 | * @param link The link whose devices' resources are to be enabled. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 841 | */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 842 | static void enable_resources(struct bus *link) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 843 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 844 | struct device *dev; |
| 845 | struct bus *c_link; |
| 846 | |
| 847 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 848 | if (dev->enabled && dev->ops && dev->ops->enable_resources) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 849 | dev->ops->enable_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 850 | } |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 851 | |
| 852 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 853 | for (c_link = dev->link_list; c_link; c_link = c_link->next) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 854 | enable_resources(c_link); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 855 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 858 | /** |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 859 | * Reset all of the devices on a bus and clear the bus's reset_needed flag. |
| 860 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 861 | * @param bus Pointer to the bus structure. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 862 | * @return 1 if the bus was successfully reset, 0 otherwise. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 863 | */ |
| 864 | int reset_bus(struct bus *bus) |
| 865 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 866 | if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 867 | bus->dev->ops->reset_bus(bus); |
| 868 | bus->reset_needed = 0; |
| 869 | return 1; |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 874 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 875 | * Scan for devices on a bus. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 876 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 877 | * If there are bridges on the bus, recursively scan the buses behind the |
| 878 | * bridges. If the setting up and tuning of the bus causes a reset to be |
| 879 | * required, reset the bus and scan it again. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 880 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 881 | * @param busdev Pointer to the bus device. |
| 882 | * @param max Current bus number. |
| 883 | * @return The maximum bus number found, after scanning all subordinate buses. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 884 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 885 | unsigned int scan_bus(struct device *busdev, unsigned int max) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 886 | { |
| 887 | unsigned int new_max; |
| 888 | int do_scan_bus; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 889 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 890 | if (!busdev || !busdev->enabled || !busdev->ops || |
| 891 | !busdev->ops->scan_bus) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 892 | return max; |
| 893 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 894 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 895 | do_scan_bus = 1; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 896 | while (do_scan_bus) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 897 | struct bus *link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 898 | new_max = busdev->ops->scan_bus(busdev, max); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 899 | do_scan_bus = 0; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 900 | for (link = busdev->link_list; link; link = link->next) { |
| 901 | if (link->reset_needed) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 902 | if (reset_bus(link)) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 903 | do_scan_bus = 1; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 904 | else |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 905 | busdev->bus->reset_needed = 1; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | } |
| 909 | return new_max; |
| 910 | } |
| 911 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 912 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 913 | * Determine the existence of devices and extend the device tree. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 914 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 915 | * Most of the devices in the system are listed in the mainboard devicetree.cb |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 916 | * file. The device structures for these devices are generated at compile |
| 917 | * time by the config tool and are organized into the device tree. This |
| 918 | * function determines if the devices created at compile time actually exist |
| 919 | * in the physical system. |
| 920 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 921 | * For devices in the physical system but not listed in devicetree.cb, |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 922 | * the device structures have to be created at run time and attached to the |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 923 | * device tree. |
| 924 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 925 | * This function starts from the root device 'dev_root', scans the buses in |
| 926 | * the system recursively, and modifies the device tree according to the |
| 927 | * result of the probe. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 928 | * |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 929 | * This function has no idea how to scan and probe buses and devices at all. |
| 930 | * It depends on the bus/device specific scan_bus() method to do it. The |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 931 | * scan_bus() method also has to create the device structure and attach |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 932 | * it to the device tree. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 933 | */ |
| 934 | void dev_enumerate(void) |
| 935 | { |
| 936 | struct device *root; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 937 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 938 | printk(BIOS_INFO, "Enumerating buses...\n"); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 939 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 940 | root = &dev_root; |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 941 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 942 | show_all_devs(BIOS_SPEW, "Before device enumeration."); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 943 | printk(BIOS_SPEW, "Compare with tree...\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 944 | show_devs_tree(root, BIOS_SPEW, 0, 0); |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 945 | |
Stefan Reinauer | a675d49 | 2012-08-07 14:50:47 -0700 | [diff] [blame] | 946 | if (root->chip_ops && root->chip_ops->enable_dev) |
| 947 | root->chip_ops->enable_dev(root); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 948 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 949 | if (!root->ops || !root->ops->scan_bus) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 950 | printk(BIOS_ERR, "dev_root missing scan_bus operation"); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 951 | return; |
| 952 | } |
Stefan Reinauer | 6afcea8 | 2009-07-18 17:58:44 +0000 | [diff] [blame] | 953 | scan_bus(root, 0); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 954 | printk(BIOS_INFO, "done\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 957 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 958 | * Configure devices on the devices tree. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 959 | * |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 960 | * Starting at the root of the device tree, travel it recursively in two |
| 961 | * passes. In the first pass, we compute and allocate resources (ranges) |
| 962 | * requried by each device. In the second pass, the resources ranges are |
| 963 | * relocated to their final position and stored to the hardware. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 964 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 965 | * I/O resources grow upward. MEM resources grow downward. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 966 | * |
| 967 | * Since the assignment is hierarchical we set the values into the dev_root |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 968 | * struct. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 969 | */ |
| 970 | void dev_configure(void) |
| 971 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 972 | struct resource *res; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 973 | struct device *root; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 974 | struct device *child; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 975 | |
Marc Jones | 80bd74c | 2012-02-21 17:44:35 +0100 | [diff] [blame] | 976 | set_vga_bridge_bits(); |
Marc Jones | 80bd74c | 2012-02-21 17:44:35 +0100 | [diff] [blame] | 977 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 978 | printk(BIOS_INFO, "Allocating resources...\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 979 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 980 | root = &dev_root; |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 981 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 982 | /* |
| 983 | * Each domain should create resources which contain the entire address |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 984 | * space for IO, MEM, and PREFMEM resources in the domain. The |
| 985 | * allocation of device resources will be done from this address space. |
| 986 | */ |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 987 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 988 | /* Read the resources for the entire tree. */ |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 989 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 990 | printk(BIOS_INFO, "Reading resources...\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 991 | read_resources(root->link_list); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 992 | printk(BIOS_INFO, "Done reading resources.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 993 | |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 994 | print_resource_tree(root, BIOS_SPEW, "After reading."); |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 995 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 996 | /* Compute resources for all domains. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 997 | for (child = root->link_list->children; child; child = child->sibling) { |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 998 | if (!(child->path.type == DEVICE_PATH_DOMAIN)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 999 | continue; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 1000 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1001 | if (res->flags & IORESOURCE_FIXED) |
| 1002 | continue; |
| 1003 | if (res->flags & IORESOURCE_PREFETCH) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1004 | compute_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1005 | res, MEM_MASK, PREF_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1006 | continue; |
| 1007 | } |
| 1008 | if (res->flags & IORESOURCE_MEM) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1009 | compute_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1010 | res, MEM_MASK, MEM_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1011 | continue; |
| 1012 | } |
| 1013 | if (res->flags & IORESOURCE_IO) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1014 | compute_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1015 | res, IO_MASK, IO_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1016 | continue; |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | /* For all domains. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1022 | for (child = root->link_list->children; child; child=child->sibling) |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 1023 | if (child->path.type == DEVICE_PATH_DOMAIN) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1024 | avoid_fixed_resources(child); |
| 1025 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1026 | /* |
| 1027 | * Now we need to adjust the resources. MEM resources need to start at |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1028 | * the highest address managable. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1029 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1030 | for (child = root->link_list->children; child; child = child->sibling) { |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 1031 | if (child->path.type != DEVICE_PATH_DOMAIN) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1032 | continue; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 1033 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1034 | if (!(res->flags & IORESOURCE_MEM) || |
| 1035 | res->flags & IORESOURCE_FIXED) |
| 1036 | continue; |
| 1037 | res->base = resource_max(res); |
| 1038 | } |
| 1039 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 1040 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1041 | /* Store the computed resource allocations into device registers ... */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1042 | printk(BIOS_INFO, "Setting resources...\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1043 | for (child = root->link_list->children; child; child = child->sibling) { |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 1044 | if (!(child->path.type == DEVICE_PATH_DOMAIN)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1045 | continue; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 1046 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1047 | if (res->flags & IORESOURCE_FIXED) |
| 1048 | continue; |
| 1049 | if (res->flags & IORESOURCE_PREFETCH) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1050 | allocate_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1051 | res, MEM_MASK, PREF_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1052 | continue; |
| 1053 | } |
| 1054 | if (res->flags & IORESOURCE_MEM) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1055 | allocate_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1056 | res, MEM_MASK, MEM_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1057 | continue; |
| 1058 | } |
| 1059 | if (res->flags & IORESOURCE_IO) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1060 | allocate_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1061 | res, IO_MASK, IO_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1062 | continue; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1066 | assign_resources(root->link_list); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1067 | printk(BIOS_INFO, "Done setting resources.\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 1068 | print_resource_tree(root, BIOS_SPEW, "After assigning values."); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 1069 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1070 | printk(BIOS_INFO, "Done allocating resources.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1073 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1074 | * Enable devices on the device tree. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1075 | * |
| 1076 | * Starting at the root, walk the tree and enable all devices/bridges by |
| 1077 | * calling the device's enable_resources() method. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1078 | */ |
| 1079 | void dev_enable(void) |
| 1080 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1081 | struct bus *link; |
| 1082 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1083 | printk(BIOS_INFO, "Enabling resources...\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1084 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1085 | /* Now enable everything. */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1086 | for (link = dev_root.link_list; link; link = link->next) |
| 1087 | enable_resources(link); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1088 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1089 | printk(BIOS_INFO, "done.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1092 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1093 | * Initialize a specific device. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1094 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1095 | * The parent should be initialized first to avoid having an ordering problem. |
| 1096 | * This is done by calling the parent's init() method before its childrens' |
| 1097 | * init() methods. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1098 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1099 | * @param dev The device to be initialized. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1100 | */ |
| 1101 | static void init_dev(struct device *dev) |
| 1102 | { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1103 | if (!dev->enabled) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1104 | return; |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1105 | |
| 1106 | if (!dev->initialized && dev->ops && dev->ops->init) { |
Aaron Durbin | 0529429 | 2013-04-30 15:41:13 -0500 | [diff] [blame] | 1107 | #if CONFIG_HAVE_MONOTONIC_TIMER |
| 1108 | struct mono_time start_time; |
| 1109 | struct rela_time dev_init_time; |
| 1110 | |
| 1111 | timer_monotonic_get(&start_time); |
| 1112 | #endif |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1113 | if (dev->path.type == DEVICE_PATH_I2C) { |
| 1114 | printk(BIOS_DEBUG, "smbus: %s[%d]->", |
| 1115 | dev_path(dev->bus->dev), dev->bus->link_num); |
| 1116 | } |
| 1117 | |
| 1118 | printk(BIOS_DEBUG, "%s init\n", dev_path(dev)); |
| 1119 | dev->initialized = 1; |
| 1120 | dev->ops->init(dev); |
Aaron Durbin | 0529429 | 2013-04-30 15:41:13 -0500 | [diff] [blame] | 1121 | #if CONFIG_HAVE_MONOTONIC_TIMER |
| 1122 | dev_init_time = current_time_from(&start_time); |
| 1123 | printk(BIOS_DEBUG, "%s init %ld usecs\n", dev_path(dev), |
| 1124 | rela_time_in_microseconds(&dev_init_time)); |
| 1125 | #endif |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | static void init_link(struct bus *link) |
| 1130 | { |
| 1131 | struct device *dev; |
| 1132 | struct bus *c_link; |
| 1133 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1134 | for (dev = link->children; dev; dev = dev->sibling) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1135 | init_dev(dev); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1136 | |
| 1137 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1138 | for (c_link = dev->link_list; c_link; c_link = c_link->next) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1139 | init_link(c_link); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1144 | * Initialize all devices in the global device tree. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1145 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1146 | * Starting at the root device, call the device's init() method to do |
| 1147 | * device-specific setup, then call each child's init() method. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1148 | */ |
| 1149 | void dev_initialize(void) |
| 1150 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1151 | struct bus *link; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1152 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1153 | printk(BIOS_INFO, "Initializing devices...\n"); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1154 | |
Duncan Laurie | b4aaaa7 | 2012-01-17 09:03:11 -0800 | [diff] [blame] | 1155 | #if CONFIG_ARCH_X86 |
| 1156 | /* Ensure EBDA is prepared before Option ROMs. */ |
| 1157 | setup_default_ebda(); |
| 1158 | #endif |
| 1159 | |
Myles Watson | 1bd3fb7 | 2010-08-16 16:25:23 +0000 | [diff] [blame] | 1160 | /* First call the mainboard init. */ |
| 1161 | init_dev(&dev_root); |
| 1162 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1163 | /* Now initialize everything. */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1164 | for (link = dev_root.link_list; link; link = link->next) |
| 1165 | init_link(link); |
| 1166 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1167 | printk(BIOS_INFO, "Devices initialized\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 1168 | show_all_devs(BIOS_SPEW, "After init."); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1169 | } |