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