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