Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/pci/setup-bus.c |
| 3 | * |
| 4 | * Extruded from code written by |
| 5 | * Dave Rusling (david.rusling@reo.mts.dec.com) |
| 6 | * David Mosberger (davidm@cs.arizona.edu) |
| 7 | * David Miller (davem@redhat.com) |
| 8 | * |
| 9 | * Support routines for initializing a PCI subsystem. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> |
| 14 | * PCI-PCI bridges cleanup, sorted resource allocation. |
| 15 | * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru> |
| 16 | * Converted to allocation in 3 passes, which gives |
| 17 | * tighter packing. Prefetchable range support. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/ioport.h> |
| 26 | #include <linux/cache.h> |
| 27 | #include <linux/slab.h> |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 28 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 30 | struct resource_list_x { |
| 31 | struct resource_list_x *next; |
| 32 | struct resource *res; |
| 33 | struct pci_dev *dev; |
| 34 | resource_size_t start; |
| 35 | resource_size_t end; |
| 36 | unsigned long flags; |
| 37 | }; |
| 38 | |
| 39 | static void add_to_failed_list(struct resource_list_x *head, |
| 40 | struct pci_dev *dev, struct resource *res) |
| 41 | { |
| 42 | struct resource_list_x *list = head; |
| 43 | struct resource_list_x *ln = list->next; |
| 44 | struct resource_list_x *tmp; |
| 45 | |
| 46 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 47 | if (!tmp) { |
| 48 | pr_warning("add_to_failed_list: kmalloc() failed!\n"); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | tmp->next = ln; |
| 53 | tmp->res = res; |
| 54 | tmp->dev = dev; |
| 55 | tmp->start = res->start; |
| 56 | tmp->end = res->end; |
| 57 | tmp->flags = res->flags; |
| 58 | list->next = tmp; |
| 59 | } |
| 60 | |
| 61 | static void free_failed_list(struct resource_list_x *head) |
| 62 | { |
| 63 | struct resource_list_x *list, *tmp; |
| 64 | |
| 65 | for (list = head->next; list;) { |
| 66 | tmp = list; |
| 67 | list = list->next; |
| 68 | kfree(tmp); |
| 69 | } |
| 70 | |
| 71 | head->next = NULL; |
| 72 | } |
| 73 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 74 | static void __dev_sort_resources(struct pci_dev *dev, |
| 75 | struct resource_list *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 77 | u16 class = dev->class >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 79 | /* Don't touch classless devices or host bridges or ioapics. */ |
| 80 | if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST) |
| 81 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 83 | /* Don't touch ioapic devices already enabled by firmware */ |
| 84 | if (class == PCI_CLASS_SYSTEM_PIC) { |
| 85 | u16 command; |
| 86 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 87 | if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
| 88 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 91 | pdev_sort_resources(dev, head); |
| 92 | } |
| 93 | |
| 94 | static void __assign_resources_sorted(struct resource_list *head, |
| 95 | struct resource_list_x *fail_head) |
| 96 | { |
| 97 | struct resource *res; |
| 98 | struct resource_list *list, *tmp; |
| 99 | int idx; |
| 100 | |
| 101 | for (list = head->next; list;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | res = list->res; |
| 103 | idx = res - &list->dev->resource[0]; |
Yinghai Lu | 9a92866 | 2010-02-28 15:49:39 -0800 | [diff] [blame] | 104 | |
Rajesh Shah | 542df5d | 2005-04-28 00:25:50 -0700 | [diff] [blame] | 105 | if (pci_assign_resource(list->dev, idx)) { |
Yinghai Lu | 9a92866 | 2010-02-28 15:49:39 -0800 | [diff] [blame] | 106 | if (fail_head && !pci_is_root_bus(list->dev->bus)) { |
| 107 | /* |
| 108 | * if the failed res is for ROM BAR, and it will |
| 109 | * be enabled later, don't add it to the list |
| 110 | */ |
| 111 | if (!((idx == PCI_ROM_RESOURCE) && |
| 112 | (!(res->flags & IORESOURCE_ROM_ENABLE)))) |
| 113 | add_to_failed_list(fail_head, list->dev, res); |
| 114 | } |
Rajesh Shah | 542df5d | 2005-04-28 00:25:50 -0700 | [diff] [blame] | 115 | res->start = 0; |
Ivan Kokshaysky | 960b846 | 2005-07-07 03:07:56 +0400 | [diff] [blame] | 116 | res->end = 0; |
Rajesh Shah | 542df5d | 2005-04-28 00:25:50 -0700 | [diff] [blame] | 117 | res->flags = 0; |
| 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | tmp = list; |
| 120 | list = list->next; |
| 121 | kfree(tmp); |
| 122 | } |
| 123 | } |
| 124 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 125 | static void pdev_assign_resources_sorted(struct pci_dev *dev, |
| 126 | struct resource_list_x *fail_head) |
| 127 | { |
| 128 | struct resource_list head; |
| 129 | |
| 130 | head.next = NULL; |
| 131 | __dev_sort_resources(dev, &head); |
| 132 | __assign_resources_sorted(&head, fail_head); |
| 133 | |
| 134 | } |
| 135 | |
| 136 | static void pbus_assign_resources_sorted(const struct pci_bus *bus, |
| 137 | struct resource_list_x *fail_head) |
| 138 | { |
| 139 | struct pci_dev *dev; |
| 140 | struct resource_list head; |
| 141 | |
| 142 | head.next = NULL; |
| 143 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 144 | __dev_sort_resources(dev, &head); |
| 145 | |
| 146 | __assign_resources_sorted(&head, fail_head); |
| 147 | } |
| 148 | |
Dominik Brodowski | b3743fa | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 149 | void pci_setup_cardbus(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct pci_dev *bridge = bus->self; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 152 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | struct pci_bus_region region; |
| 154 | |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 155 | dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n", |
| 156 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 158 | res = bus->resource[0]; |
| 159 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 160 | if (res->flags & IORESOURCE_IO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* |
| 162 | * The IO resource is allocated a range twice as large as it |
| 163 | * would normally need. This allows us to set both IO regs. |
| 164 | */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 165 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, |
| 167 | region.start); |
| 168 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, |
| 169 | region.end); |
| 170 | } |
| 171 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 172 | res = bus->resource[1]; |
| 173 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 174 | if (res->flags & IORESOURCE_IO) { |
| 175 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, |
| 177 | region.start); |
| 178 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, |
| 179 | region.end); |
| 180 | } |
| 181 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 182 | res = bus->resource[2]; |
| 183 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 184 | if (res->flags & IORESOURCE_MEM) { |
| 185 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, |
| 187 | region.start); |
| 188 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, |
| 189 | region.end); |
| 190 | } |
| 191 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 192 | res = bus->resource[3]; |
| 193 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 194 | if (res->flags & IORESOURCE_MEM) { |
| 195 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, |
| 197 | region.start); |
| 198 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, |
| 199 | region.end); |
| 200 | } |
| 201 | } |
Dominik Brodowski | b3743fa | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 202 | EXPORT_SYMBOL(pci_setup_cardbus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | /* Initialize bridges with base/limit values we have collected. |
| 205 | PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) |
| 206 | requires that if there is no I/O ports or memory behind the |
| 207 | bridge, corresponding range must be turned off by writing base |
| 208 | value greater than limit to the bridge's base/limit registers. |
| 209 | |
| 210 | Note: care must be taken when updating I/O base/limit registers |
| 211 | of bridges which support 32-bit I/O. This update requires two |
| 212 | config space writes, so it's quite possible that an I/O window of |
| 213 | the bridge will have some undesirable address (e.g. 0) after the |
| 214 | first write. Ditto 64-bit prefetchable MMIO. */ |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 215 | static void pci_setup_bridge_io(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | struct pci_dev *bridge = bus->self; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 218 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | struct pci_bus_region region; |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 220 | u32 l, io_upper16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | /* Set up the top and bottom of the PCI I/O segment for this bus. */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 223 | res = bus->resource[0]; |
| 224 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 225 | if (res->flags & IORESOURCE_IO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | pci_read_config_dword(bridge, PCI_IO_BASE, &l); |
| 227 | l &= 0xffff0000; |
| 228 | l |= (region.start >> 8) & 0x00f0; |
| 229 | l |= region.end & 0xf000; |
| 230 | /* Set up upper 16 bits of I/O base/limit. */ |
| 231 | io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 232 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 233 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* Clear upper 16 bits of I/O base/limit. */ |
| 235 | io_upper16 = 0; |
| 236 | l = 0x00f0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 237 | dev_info(&bridge->dev, " bridge window [io disabled]\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ |
| 240 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); |
| 241 | /* Update lower 16 bits of I/O base/limit. */ |
| 242 | pci_write_config_dword(bridge, PCI_IO_BASE, l); |
| 243 | /* Update upper 16 bits of I/O base/limit. */ |
| 244 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 247 | static void pci_setup_bridge_mmio(struct pci_bus *bus) |
| 248 | { |
| 249 | struct pci_dev *bridge = bus->self; |
| 250 | struct resource *res; |
| 251 | struct pci_bus_region region; |
| 252 | u32 l; |
| 253 | |
| 254 | /* Set up the top and bottom of the PCI Memory segment for this bus. */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 255 | res = bus->resource[1]; |
| 256 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 257 | if (res->flags & IORESOURCE_MEM) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | l = (region.start >> 16) & 0xfff0; |
| 259 | l |= region.end & 0xfff00000; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 260 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 261 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | l = 0x0000fff0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 263 | dev_info(&bridge->dev, " bridge window [mem disabled]\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void pci_setup_bridge_mmio_pref(struct pci_bus *bus) |
| 269 | { |
| 270 | struct pci_dev *bridge = bus->self; |
| 271 | struct resource *res; |
| 272 | struct pci_bus_region region; |
| 273 | u32 l, bu, lu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | /* Clear out the upper 32 bits of PREF limit. |
| 276 | If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily |
| 277 | disables PREF range, which is ok. */ |
| 278 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); |
| 279 | |
| 280 | /* Set up PREF base/limit. */ |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 281 | bu = lu = 0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 282 | res = bus->resource[2]; |
| 283 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 284 | if (res->flags & IORESOURCE_PREFETCH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | l = (region.start >> 16) & 0xfff0; |
| 286 | l |= region.end & 0xfff00000; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 287 | if (res->flags & IORESOURCE_MEM_64) { |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 288 | bu = upper_32_bits(region.start); |
| 289 | lu = upper_32_bits(region.end); |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 290 | } |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 291 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 292 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | l = 0x0000fff0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 294 | dev_info(&bridge->dev, " bridge window [mem pref disabled]\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); |
| 297 | |
Alex Williamson | 59353ea | 2009-11-30 14:51:44 -0700 | [diff] [blame] | 298 | /* Set the upper 32 bits of PREF base & limit. */ |
| 299 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); |
| 300 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type) |
| 304 | { |
| 305 | struct pci_dev *bridge = bus->self; |
| 306 | |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 307 | dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n", |
| 308 | bus->secondary, bus->subordinate); |
| 309 | |
| 310 | if (type & IORESOURCE_IO) |
| 311 | pci_setup_bridge_io(bus); |
| 312 | |
| 313 | if (type & IORESOURCE_MEM) |
| 314 | pci_setup_bridge_mmio(bus); |
| 315 | |
| 316 | if (type & IORESOURCE_PREFETCH) |
| 317 | pci_setup_bridge_mmio_pref(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); |
| 320 | } |
| 321 | |
Yinghai Lu | 7cc5997 | 2009-12-22 15:02:21 -0800 | [diff] [blame] | 322 | static void pci_setup_bridge(struct pci_bus *bus) |
| 323 | { |
| 324 | unsigned long type = IORESOURCE_IO | IORESOURCE_MEM | |
| 325 | IORESOURCE_PREFETCH; |
| 326 | |
| 327 | __pci_setup_bridge(bus, type); |
| 328 | } |
| 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | /* Check whether the bridge supports optional I/O and |
| 331 | prefetchable memory ranges. If not, the respective |
| 332 | base/limit registers must be read-only and read as 0. */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 333 | static void pci_bridge_check_ranges(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | u16 io; |
| 336 | u32 pmem; |
| 337 | struct pci_dev *bridge = bus->self; |
| 338 | struct resource *b_res; |
| 339 | |
| 340 | b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; |
| 341 | b_res[1].flags |= IORESOURCE_MEM; |
| 342 | |
| 343 | pci_read_config_word(bridge, PCI_IO_BASE, &io); |
| 344 | if (!io) { |
| 345 | pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); |
| 346 | pci_read_config_word(bridge, PCI_IO_BASE, &io); |
| 347 | pci_write_config_word(bridge, PCI_IO_BASE, 0x0); |
| 348 | } |
| 349 | if (io) |
| 350 | b_res[0].flags |= IORESOURCE_IO; |
| 351 | /* DECchip 21050 pass 2 errata: the bridge may miss an address |
| 352 | disconnect boundary by one PCI data phase. |
| 353 | Workaround: do not use prefetching on this device. */ |
| 354 | if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) |
| 355 | return; |
| 356 | pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); |
| 357 | if (!pmem) { |
| 358 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, |
| 359 | 0xfff0fff0); |
| 360 | pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); |
| 361 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); |
| 362 | } |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 363 | if (pmem) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; |
Yinghai Lu | 9958610 | 2010-01-22 01:02:28 -0800 | [diff] [blame] | 365 | if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == |
| 366 | PCI_PREF_RANGE_TYPE_64) { |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 367 | b_res[2].flags |= IORESOURCE_MEM_64; |
Yinghai Lu | 9958610 | 2010-01-22 01:02:28 -0800 | [diff] [blame] | 368 | b_res[2].flags |= PCI_PREF_RANGE_TYPE_64; |
| 369 | } |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* double check if bridge does support 64 bit pref */ |
| 373 | if (b_res[2].flags & IORESOURCE_MEM_64) { |
| 374 | u32 mem_base_hi, tmp; |
| 375 | pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 376 | &mem_base_hi); |
| 377 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 378 | 0xffffffff); |
| 379 | pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); |
| 380 | if (!tmp) |
| 381 | b_res[2].flags &= ~IORESOURCE_MEM_64; |
| 382 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 383 | mem_base_hi); |
| 384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* Helper function for sizing routines: find first available |
| 388 | bus resource of a given type. Note: we intentionally skip |
| 389 | the bus resources which have already been assigned (that is, |
| 390 | have non-NULL parent resource). */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 391 | static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
| 393 | int i; |
| 394 | struct resource *r; |
| 395 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 396 | IORESOURCE_PREFETCH; |
| 397 | |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame] | 398 | pci_bus_for_each_resource(bus, r, i) { |
Ivan Kokshaysky | 299de03 | 2005-06-15 18:59:27 +0400 | [diff] [blame] | 399 | if (r == &ioport_resource || r == &iomem_resource) |
| 400 | continue; |
Jesse Barnes | 55a1098 | 2009-10-27 09:39:18 -0700 | [diff] [blame] | 401 | if (r && (r->flags & type_mask) == type && !r->parent) |
| 402 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | return NULL; |
| 405 | } |
| 406 | |
| 407 | /* Sizing the IO windows of the PCI-PCI bridge is trivial, |
| 408 | since these windows have 4K granularity and the IO ranges |
| 409 | of non-bridge PCI devices are limited to 256 bytes. |
| 410 | We must be careful with the ISA aliasing though. */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 411 | static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | struct pci_dev *dev; |
| 414 | struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); |
Yinghai Lu | d65245c | 2010-01-22 01:02:23 -0800 | [diff] [blame] | 415 | unsigned long size = 0, size1 = 0, old_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | if (!b_res) |
| 418 | return; |
| 419 | |
| 420 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 421 | int i; |
| 422 | |
| 423 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 424 | struct resource *r = &dev->resource[i]; |
| 425 | unsigned long r_size; |
| 426 | |
| 427 | if (r->parent || !(r->flags & IORESOURCE_IO)) |
| 428 | continue; |
Zhao, Yu | 022edd8 | 2008-10-13 19:24:28 +0800 | [diff] [blame] | 429 | r_size = resource_size(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | if (r_size < 0x400) |
| 432 | /* Might be re-aligned for ISA */ |
| 433 | size += r_size; |
| 434 | else |
| 435 | size1 += r_size; |
| 436 | } |
| 437 | } |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 438 | if (size < min_size) |
| 439 | size = min_size; |
Yinghai Lu | d65245c | 2010-01-22 01:02:23 -0800 | [diff] [blame] | 440 | old_size = resource_size(b_res); |
| 441 | if (old_size == 1) |
| 442 | old_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | /* To be fixed in 2.5: we should have sort of HAVE_ISA |
| 444 | flag in the struct pci_bus. */ |
| 445 | #if defined(CONFIG_ISA) || defined(CONFIG_EISA) |
| 446 | size = (size & 0xff) + ((size & ~0xffUL) << 2); |
| 447 | #endif |
Milind Arun Choudhary | 6f6f8c2 | 2007-07-09 11:55:51 -0700 | [diff] [blame] | 448 | size = ALIGN(size + size1, 4096); |
Yinghai Lu | d65245c | 2010-01-22 01:02:23 -0800 | [diff] [blame] | 449 | if (size < old_size) |
| 450 | size = old_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (!size) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 452 | if (b_res->start || b_res->end) |
| 453 | dev_info(&bus->self->dev, "disabling bridge window " |
| 454 | "%pR to [bus %02x-%02x] (unused)\n", b_res, |
| 455 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | b_res->flags = 0; |
| 457 | return; |
| 458 | } |
| 459 | /* Alignment of the IO window is always 4K */ |
| 460 | b_res->start = 4096; |
| 461 | b_res->end = b_res->start + size - 1; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 462 | b_res->flags |= IORESOURCE_STARTALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Calculate the size of the bus and minimal alignment which |
| 466 | guarantees that all child resources fit in this size. */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 467 | static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, |
| 468 | unsigned long type, resource_size_t min_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | struct pci_dev *dev; |
Yinghai Lu | d65245c | 2010-01-22 01:02:23 -0800 | [diff] [blame] | 471 | resource_size_t min_align, align, size, old_size; |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 472 | resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | int order, max_order; |
| 474 | struct resource *b_res = find_free_bus_resource(bus, type); |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 475 | unsigned int mem64_mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | if (!b_res) |
| 478 | return 0; |
| 479 | |
| 480 | memset(aligns, 0, sizeof(aligns)); |
| 481 | max_order = 0; |
| 482 | size = 0; |
| 483 | |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 484 | mem64_mask = b_res->flags & IORESOURCE_MEM_64; |
| 485 | b_res->flags &= ~IORESOURCE_MEM_64; |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 488 | int i; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 491 | struct resource *r = &dev->resource[i]; |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 492 | resource_size_t r_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | if (r->parent || (r->flags & mask) != type) |
| 495 | continue; |
Zhao, Yu | 022edd8 | 2008-10-13 19:24:28 +0800 | [diff] [blame] | 496 | r_size = resource_size(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | /* For bridges size != alignment */ |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 498 | align = pci_resource_alignment(dev, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | order = __ffs(align) - 20; |
| 500 | if (order > 11) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 501 | dev_warn(&dev->dev, "disabling BAR %d: %pR " |
| 502 | "(bad alignment %#llx)\n", i, r, |
| 503 | (unsigned long long) align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | r->flags = 0; |
| 505 | continue; |
| 506 | } |
| 507 | size += r_size; |
| 508 | if (order < 0) |
| 509 | order = 0; |
| 510 | /* Exclude ranges with size > align from |
| 511 | calculation of the alignment. */ |
| 512 | if (r_size == align) |
| 513 | aligns[order] += align; |
| 514 | if (order > max_order) |
| 515 | max_order = order; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 516 | mem64_mask &= r->flags & IORESOURCE_MEM_64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 519 | if (size < min_size) |
| 520 | size = min_size; |
Yinghai Lu | d65245c | 2010-01-22 01:02:23 -0800 | [diff] [blame] | 521 | old_size = resource_size(b_res); |
| 522 | if (old_size == 1) |
| 523 | old_size = 0; |
| 524 | if (size < old_size) |
| 525 | size = old_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | align = 0; |
| 528 | min_align = 0; |
| 529 | for (order = 0; order <= max_order; order++) { |
Jeremy Fitzhardinge | 8308c54 | 2008-09-11 01:31:50 -0700 | [diff] [blame] | 530 | resource_size_t align1 = 1; |
| 531 | |
| 532 | align1 <<= (order + 20); |
| 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (!align) |
| 535 | min_align = align1; |
Milind Arun Choudhary | 6f6f8c2 | 2007-07-09 11:55:51 -0700 | [diff] [blame] | 536 | else if (ALIGN(align + min_align, min_align) < align1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | min_align = align1 >> 1; |
| 538 | align += aligns[order]; |
| 539 | } |
Milind Arun Choudhary | 6f6f8c2 | 2007-07-09 11:55:51 -0700 | [diff] [blame] | 540 | size = ALIGN(size, min_align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (!size) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 542 | if (b_res->start || b_res->end) |
| 543 | dev_info(&bus->self->dev, "disabling bridge window " |
| 544 | "%pR to [bus %02x-%02x] (unused)\n", b_res, |
| 545 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | b_res->flags = 0; |
| 547 | return 1; |
| 548 | } |
| 549 | b_res->start = min_align; |
| 550 | b_res->end = size + min_align - 1; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 551 | b_res->flags |= IORESOURCE_STARTALIGN; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 552 | b_res->flags |= mem64_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return 1; |
| 554 | } |
| 555 | |
Adrian Bunk | 5468ae6 | 2008-04-18 13:53:56 -0700 | [diff] [blame] | 556 | static void pci_bus_size_cardbus(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | struct pci_dev *bridge = bus->self; |
| 559 | struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; |
| 560 | u16 ctrl; |
| 561 | |
| 562 | /* |
| 563 | * Reserve some resources for CardBus. We reserve |
| 564 | * a fixed amount of bus space for CardBus bridges. |
| 565 | */ |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 566 | b_res[0].start = 0; |
| 567 | b_res[0].end = pci_cardbus_io_size - 1; |
| 568 | b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 570 | b_res[1].start = 0; |
| 571 | b_res[1].end = pci_cardbus_io_size - 1; |
| 572 | b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * Check whether prefetchable memory is supported |
| 576 | * by this bridge. |
| 577 | */ |
| 578 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 579 | if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { |
| 580 | ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; |
| 581 | pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); |
| 582 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * If we have prefetchable memory support, allocate |
| 587 | * two regions. Otherwise, allocate one region of |
| 588 | * twice the size. |
| 589 | */ |
| 590 | if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 591 | b_res[2].start = 0; |
| 592 | b_res[2].end = pci_cardbus_mem_size - 1; |
| 593 | b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 595 | b_res[3].start = 0; |
| 596 | b_res[3].end = pci_cardbus_mem_size - 1; |
| 597 | b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } else { |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 599 | b_res[3].start = 0; |
| 600 | b_res[3].end = pci_cardbus_mem_size * 2 - 1; |
| 601 | b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Sam Ravnborg | 451124a | 2008-02-02 22:33:43 +0100 | [diff] [blame] | 605 | void __ref pci_bus_size_bridges(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | struct pci_dev *dev; |
| 608 | unsigned long mask, prefmask; |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 609 | resource_size_t min_mem_size = 0, min_io_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 612 | struct pci_bus *b = dev->subordinate; |
| 613 | if (!b) |
| 614 | continue; |
| 615 | |
| 616 | switch (dev->class >> 8) { |
| 617 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 618 | pci_bus_size_cardbus(b); |
| 619 | break; |
| 620 | |
| 621 | case PCI_CLASS_BRIDGE_PCI: |
| 622 | default: |
| 623 | pci_bus_size_bridges(b); |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* The root bus? */ |
| 629 | if (!bus->self) |
| 630 | return; |
| 631 | |
| 632 | switch (bus->self->class >> 8) { |
| 633 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 634 | /* don't size cardbuses yet. */ |
| 635 | break; |
| 636 | |
| 637 | case PCI_CLASS_BRIDGE_PCI: |
| 638 | pci_bridge_check_ranges(bus); |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 639 | if (bus->self->is_hotplug_bridge) { |
| 640 | min_io_size = pci_hotplug_io_size; |
| 641 | min_mem_size = pci_hotplug_mem_size; |
| 642 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | default: |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 644 | pbus_size_io(bus, min_io_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | /* If the bridge supports prefetchable range, size it |
| 646 | separately. If it doesn't, or its prefetchable window |
| 647 | has already been allocated by arch code, try |
| 648 | non-prefetchable range for both types of PCI memory |
| 649 | resources. */ |
| 650 | mask = IORESOURCE_MEM; |
| 651 | prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 652 | if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | mask = prefmask; /* Success, size non-prefetch only. */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 654 | else |
| 655 | min_mem_size += min_mem_size; |
| 656 | pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | } |
| 660 | EXPORT_SYMBOL(pci_bus_size_bridges); |
| 661 | |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 662 | static void __ref __pci_bus_assign_resources(const struct pci_bus *bus, |
| 663 | struct resource_list_x *fail_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
| 665 | struct pci_bus *b; |
| 666 | struct pci_dev *dev; |
| 667 | |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 668 | pbus_assign_resources_sorted(bus, fail_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 671 | b = dev->subordinate; |
| 672 | if (!b) |
| 673 | continue; |
| 674 | |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 675 | __pci_bus_assign_resources(b, fail_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
| 677 | switch (dev->class >> 8) { |
| 678 | case PCI_CLASS_BRIDGE_PCI: |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 679 | if (!pci_is_enabled(dev)) |
| 680 | pci_setup_bridge(b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 684 | pci_setup_cardbus(b); |
| 685 | break; |
| 686 | |
| 687 | default: |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 688 | dev_info(&dev->dev, "not setting up bridge for bus " |
| 689 | "%04x:%02x\n", pci_domain_nr(b), b->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | break; |
| 691 | } |
| 692 | } |
| 693 | } |
Yinghai Lu | 568ddef | 2010-01-22 01:02:21 -0800 | [diff] [blame] | 694 | |
| 695 | void __ref pci_bus_assign_resources(const struct pci_bus *bus) |
| 696 | { |
| 697 | __pci_bus_assign_resources(bus, NULL); |
| 698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | EXPORT_SYMBOL(pci_bus_assign_resources); |
| 700 | |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 701 | static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, |
| 702 | struct resource_list_x *fail_head) |
| 703 | { |
| 704 | struct pci_bus *b; |
| 705 | |
| 706 | pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head); |
| 707 | |
| 708 | b = bridge->subordinate; |
| 709 | if (!b) |
| 710 | return; |
| 711 | |
| 712 | __pci_bus_assign_resources(b, fail_head); |
| 713 | |
| 714 | switch (bridge->class >> 8) { |
| 715 | case PCI_CLASS_BRIDGE_PCI: |
| 716 | pci_setup_bridge(b); |
| 717 | break; |
| 718 | |
| 719 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 720 | pci_setup_cardbus(b); |
| 721 | break; |
| 722 | |
| 723 | default: |
| 724 | dev_info(&bridge->dev, "not setting up bridge for bus " |
| 725 | "%04x:%02x\n", pci_domain_nr(b), b->number); |
| 726 | break; |
| 727 | } |
| 728 | } |
Yinghai Lu | 5009b46 | 2010-01-22 01:02:20 -0800 | [diff] [blame] | 729 | static void pci_bridge_release_resources(struct pci_bus *bus, |
| 730 | unsigned long type) |
| 731 | { |
| 732 | int idx; |
| 733 | bool changed = false; |
| 734 | struct pci_dev *dev; |
| 735 | struct resource *r; |
| 736 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 737 | IORESOURCE_PREFETCH; |
| 738 | |
| 739 | dev = bus->self; |
| 740 | for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END; |
| 741 | idx++) { |
| 742 | r = &dev->resource[idx]; |
| 743 | if ((r->flags & type_mask) != type) |
| 744 | continue; |
| 745 | if (!r->parent) |
| 746 | continue; |
| 747 | /* |
| 748 | * if there are children under that, we should release them |
| 749 | * all |
| 750 | */ |
| 751 | release_child_resources(r); |
| 752 | if (!release_resource(r)) { |
| 753 | dev_printk(KERN_DEBUG, &dev->dev, |
| 754 | "resource %d %pR released\n", idx, r); |
| 755 | /* keep the old size */ |
| 756 | r->end = resource_size(r) - 1; |
| 757 | r->start = 0; |
| 758 | r->flags = 0; |
| 759 | changed = true; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | if (changed) { |
| 764 | /* avoiding touch the one without PREF */ |
| 765 | if (type & IORESOURCE_PREFETCH) |
| 766 | type = IORESOURCE_PREFETCH; |
| 767 | __pci_setup_bridge(bus, type); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | enum release_type { |
| 772 | leaf_only, |
| 773 | whole_subtree, |
| 774 | }; |
| 775 | /* |
| 776 | * try to release pci bridge resources that is from leaf bridge, |
| 777 | * so we can allocate big new one later |
| 778 | */ |
| 779 | static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus, |
| 780 | unsigned long type, |
| 781 | enum release_type rel_type) |
| 782 | { |
| 783 | struct pci_dev *dev; |
| 784 | bool is_leaf_bridge = true; |
| 785 | |
| 786 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 787 | struct pci_bus *b = dev->subordinate; |
| 788 | if (!b) |
| 789 | continue; |
| 790 | |
| 791 | is_leaf_bridge = false; |
| 792 | |
| 793 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 794 | continue; |
| 795 | |
| 796 | if (rel_type == whole_subtree) |
| 797 | pci_bus_release_bridge_resources(b, type, |
| 798 | whole_subtree); |
| 799 | } |
| 800 | |
| 801 | if (pci_is_root_bus(bus)) |
| 802 | return; |
| 803 | |
| 804 | if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 805 | return; |
| 806 | |
| 807 | if ((rel_type == whole_subtree) || is_leaf_bridge) |
| 808 | pci_bridge_release_resources(bus, type); |
| 809 | } |
| 810 | |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 811 | static void pci_bus_dump_res(struct pci_bus *bus) |
| 812 | { |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame] | 813 | struct resource *res; |
| 814 | int i; |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 815 | |
Bjorn Helgaas | 89a74ec | 2010-02-23 10:24:31 -0700 | [diff] [blame] | 816 | pci_bus_for_each_resource(bus, res, i) { |
Yinghai Lu | 7c9342b | 2009-12-22 15:02:24 -0800 | [diff] [blame] | 817 | if (!res || !res->end || !res->flags) |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 818 | continue; |
| 819 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 820 | dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | static void pci_bus_dump_resources(struct pci_bus *bus) |
| 825 | { |
| 826 | struct pci_bus *b; |
| 827 | struct pci_dev *dev; |
| 828 | |
| 829 | |
| 830 | pci_bus_dump_res(bus); |
| 831 | |
| 832 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 833 | b = dev->subordinate; |
| 834 | if (!b) |
| 835 | continue; |
| 836 | |
| 837 | pci_bus_dump_resources(b); |
| 838 | } |
| 839 | } |
| 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | void __init |
| 842 | pci_assign_unassigned_resources(void) |
| 843 | { |
| 844 | struct pci_bus *bus; |
| 845 | |
| 846 | /* Depth first, calculate sizes and alignments of all |
| 847 | subordinate buses. */ |
| 848 | list_for_each_entry(bus, &pci_root_buses, node) { |
| 849 | pci_bus_size_bridges(bus); |
| 850 | } |
| 851 | /* Depth last, allocate resources and update the hardware. */ |
| 852 | list_for_each_entry(bus, &pci_root_buses, node) { |
Linus Torvalds | 769d996 | 2010-05-12 18:39:45 -0700 | [diff] [blame] | 853 | pci_bus_assign_resources(bus); |
Yinghai Lu | 977d17b | 2010-01-22 01:02:24 -0800 | [diff] [blame] | 854 | pci_enable_bridges(bus); |
Linus Torvalds | 769d996 | 2010-05-12 18:39:45 -0700 | [diff] [blame] | 855 | } |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 856 | |
| 857 | /* dump the resource on buses */ |
| 858 | list_for_each_entry(bus, &pci_root_buses, node) { |
| 859 | pci_bus_dump_resources(bus); |
| 860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 862 | |
| 863 | void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) |
| 864 | { |
| 865 | struct pci_bus *parent = bridge->subordinate; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 866 | int tried_times = 0; |
| 867 | struct resource_list_x head, *list; |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 868 | int retval; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 869 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 870 | IORESOURCE_PREFETCH; |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 871 | |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 872 | head.next = NULL; |
| 873 | |
| 874 | again: |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 875 | pci_bus_size_bridges(parent); |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 876 | __pci_bridge_assign_resources(bridge, &head); |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 877 | |
| 878 | tried_times++; |
| 879 | |
| 880 | if (!head.next) |
Yinghai Lu | 3f579c3 | 2010-05-21 14:35:06 -0700 | [diff] [blame] | 881 | goto enable_all; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 882 | |
| 883 | if (tried_times >= 2) { |
| 884 | /* still fail, don't need to try more */ |
| 885 | free_failed_list(&head); |
Yinghai Lu | 3f579c3 | 2010-05-21 14:35:06 -0700 | [diff] [blame] | 886 | goto enable_all; |
Yinghai Lu | 32180e4 | 2010-01-22 01:02:27 -0800 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n", |
| 890 | tried_times + 1); |
| 891 | |
| 892 | /* |
| 893 | * Try to release leaf bridge's resources that doesn't fit resource of |
| 894 | * child device under that bridge |
| 895 | */ |
| 896 | for (list = head.next; list;) { |
| 897 | struct pci_bus *bus = list->dev->bus; |
| 898 | unsigned long flags = list->flags; |
| 899 | |
| 900 | pci_bus_release_bridge_resources(bus, flags & type_mask, |
| 901 | whole_subtree); |
| 902 | list = list->next; |
| 903 | } |
| 904 | /* restore size and flags */ |
| 905 | for (list = head.next; list;) { |
| 906 | struct resource *res = list->res; |
| 907 | |
| 908 | res->start = list->start; |
| 909 | res->end = list->end; |
| 910 | res->flags = list->flags; |
| 911 | if (list->dev->subordinate) |
| 912 | res->flags = 0; |
| 913 | |
| 914 | list = list->next; |
| 915 | } |
| 916 | free_failed_list(&head); |
| 917 | |
| 918 | goto again; |
Yinghai Lu | 3f579c3 | 2010-05-21 14:35:06 -0700 | [diff] [blame] | 919 | |
| 920 | enable_all: |
| 921 | retval = pci_reenable_device(bridge); |
| 922 | pci_set_master(bridge); |
| 923 | pci_enable_bridges(parent); |
Yinghai Lu | 6841ec6 | 2010-01-22 01:02:25 -0800 | [diff] [blame] | 924 | } |
| 925 | EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); |