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 | |
Andrew Morton | ea74155 | 2009-02-18 10:44:29 -0800 | [diff] [blame] | 30 | static void pbus_assign_resources_sorted(const struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
| 32 | struct pci_dev *dev; |
| 33 | struct resource *res; |
| 34 | struct resource_list head, *list, *tmp; |
| 35 | int idx; |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | head.next = NULL; |
| 38 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 39 | u16 class = dev->class >> 8; |
| 40 | |
Kenji Kaneshige | 9bded00 | 2006-10-04 02:15:34 -0700 | [diff] [blame] | 41 | /* Don't touch classless devices or host bridges or ioapics. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (class == PCI_CLASS_NOT_DEFINED || |
Satoru Takeuchi | 2318627 | 2006-09-12 10:21:44 -0700 | [diff] [blame] | 43 | class == PCI_CLASS_BRIDGE_HOST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | continue; |
| 45 | |
Kenji Kaneshige | 9bded00 | 2006-10-04 02:15:34 -0700 | [diff] [blame] | 46 | /* Don't touch ioapic devices already enabled by firmware */ |
Satoru Takeuchi | 2318627 | 2006-09-12 10:21:44 -0700 | [diff] [blame] | 47 | if (class == PCI_CLASS_SYSTEM_PIC) { |
Kenji Kaneshige | 9bded00 | 2006-10-04 02:15:34 -0700 | [diff] [blame] | 48 | u16 command; |
| 49 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 50 | if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
Satoru Takeuchi | 2318627 | 2006-09-12 10:21:44 -0700 | [diff] [blame] | 51 | continue; |
| 52 | } |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | pdev_sort_resources(dev, &head); |
| 55 | } |
| 56 | |
| 57 | for (list = head.next; list;) { |
| 58 | res = list->res; |
| 59 | idx = res - &list->dev->resource[0]; |
Rajesh Shah | 542df5d | 2005-04-28 00:25:50 -0700 | [diff] [blame] | 60 | if (pci_assign_resource(list->dev, idx)) { |
| 61 | res->start = 0; |
Ivan Kokshaysky | 960b846 | 2005-07-07 03:07:56 +0400 | [diff] [blame] | 62 | res->end = 0; |
Rajesh Shah | 542df5d | 2005-04-28 00:25:50 -0700 | [diff] [blame] | 63 | res->flags = 0; |
| 64 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | tmp = list; |
| 66 | list = list->next; |
| 67 | kfree(tmp); |
| 68 | } |
| 69 | } |
| 70 | |
Dominik Brodowski | b3743fa | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 71 | void pci_setup_cardbus(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | struct pci_dev *bridge = bus->self; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 74 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | struct pci_bus_region region; |
| 76 | |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 77 | dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n", |
| 78 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 80 | res = bus->resource[0]; |
| 81 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 82 | if (res->flags & IORESOURCE_IO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
| 84 | * The IO resource is allocated a range twice as large as it |
| 85 | * would normally need. This allows us to set both IO regs. |
| 86 | */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 87 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, |
| 89 | region.start); |
| 90 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0, |
| 91 | region.end); |
| 92 | } |
| 93 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 94 | res = bus->resource[1]; |
| 95 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 96 | if (res->flags & IORESOURCE_IO) { |
| 97 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, |
| 99 | region.start); |
| 100 | pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1, |
| 101 | region.end); |
| 102 | } |
| 103 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 104 | res = bus->resource[2]; |
| 105 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 106 | if (res->flags & IORESOURCE_MEM) { |
| 107 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, |
| 109 | region.start); |
| 110 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0, |
| 111 | region.end); |
| 112 | } |
| 113 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 114 | res = bus->resource[3]; |
| 115 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 116 | if (res->flags & IORESOURCE_MEM) { |
| 117 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, |
| 119 | region.start); |
| 120 | pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1, |
| 121 | region.end); |
| 122 | } |
| 123 | } |
Dominik Brodowski | b3743fa | 2005-09-09 13:03:23 -0700 | [diff] [blame] | 124 | EXPORT_SYMBOL(pci_setup_cardbus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* Initialize bridges with base/limit values we have collected. |
| 127 | PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998) |
| 128 | requires that if there is no I/O ports or memory behind the |
| 129 | bridge, corresponding range must be turned off by writing base |
| 130 | value greater than limit to the bridge's base/limit registers. |
| 131 | |
| 132 | Note: care must be taken when updating I/O base/limit registers |
| 133 | of bridges which support 32-bit I/O. This update requires two |
| 134 | config space writes, so it's quite possible that an I/O window of |
| 135 | the bridge will have some undesirable address (e.g. 0) after the |
| 136 | first write. Ditto 64-bit prefetchable MMIO. */ |
Adrian Bunk | a391f19 | 2008-04-18 13:53:57 -0700 | [diff] [blame] | 137 | static void pci_setup_bridge(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | struct pci_dev *bridge = bus->self; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 140 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct pci_bus_region region; |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 142 | u32 l, bu, lu, io_upper16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Yuji Shimada | 296ccb0 | 2009-04-03 16:41:46 +0900 | [diff] [blame] | 144 | if (pci_is_enabled(bridge)) |
Alex Chiang | b73e97d | 2009-03-20 14:56:15 -0600 | [diff] [blame] | 145 | return; |
| 146 | |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 147 | dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n", |
| 148 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | /* 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] | 151 | res = bus->resource[0]; |
| 152 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 153 | if (res->flags & IORESOURCE_IO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | pci_read_config_dword(bridge, PCI_IO_BASE, &l); |
| 155 | l &= 0xffff0000; |
| 156 | l |= (region.start >> 8) & 0x00f0; |
| 157 | l |= region.end & 0xf000; |
| 158 | /* Set up upper 16 bits of I/O base/limit. */ |
| 159 | io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 160 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | else { |
| 163 | /* Clear upper 16 bits of I/O base/limit. */ |
| 164 | io_upper16 = 0; |
| 165 | l = 0x00f0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 166 | dev_info(&bridge->dev, " bridge window [io disabled]\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ |
| 169 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); |
| 170 | /* Update lower 16 bits of I/O base/limit. */ |
| 171 | pci_write_config_dword(bridge, PCI_IO_BASE, l); |
| 172 | /* Update upper 16 bits of I/O base/limit. */ |
| 173 | pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16); |
| 174 | |
| 175 | /* Set up the top and bottom of the PCI Memory segment |
| 176 | for this bus. */ |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 177 | res = bus->resource[1]; |
| 178 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 179 | if (res->flags & IORESOURCE_MEM) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | l = (region.start >> 16) & 0xfff0; |
| 181 | l |= region.end & 0xfff00000; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 182 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | else { |
| 185 | l = 0x0000fff0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 186 | dev_info(&bridge->dev, " bridge window [mem disabled]\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); |
| 189 | |
| 190 | /* Clear out the upper 32 bits of PREF limit. |
| 191 | If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily |
| 192 | disables PREF range, which is ok. */ |
| 193 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0); |
| 194 | |
| 195 | /* Set up PREF base/limit. */ |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 196 | bu = lu = 0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 197 | res = bus->resource[2]; |
| 198 | pcibios_resource_to_bus(bridge, ®ion, res); |
| 199 | if (res->flags & IORESOURCE_PREFETCH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | l = (region.start >> 16) & 0xfff0; |
| 201 | l |= region.end & 0xfff00000; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 202 | if (res->flags & IORESOURCE_MEM_64) { |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 203 | bu = upper_32_bits(region.start); |
| 204 | lu = upper_32_bits(region.end); |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 205 | } |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 206 | dev_info(&bridge->dev, " bridge window %pR\n", res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | else { |
| 209 | l = 0x0000fff0; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 210 | dev_info(&bridge->dev, " bridge window [mem pref disabled]\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); |
| 213 | |
Alex Williamson | 59353ea | 2009-11-30 14:51:44 -0700 | [diff] [blame] | 214 | /* Set the upper 32 bits of PREF base & limit. */ |
| 215 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu); |
| 216 | pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl); |
| 219 | } |
| 220 | |
| 221 | /* Check whether the bridge supports optional I/O and |
| 222 | prefetchable memory ranges. If not, the respective |
| 223 | base/limit registers must be read-only and read as 0. */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 224 | static void pci_bridge_check_ranges(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | u16 io; |
| 227 | u32 pmem; |
| 228 | struct pci_dev *bridge = bus->self; |
| 229 | struct resource *b_res; |
| 230 | |
| 231 | b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; |
| 232 | b_res[1].flags |= IORESOURCE_MEM; |
| 233 | |
| 234 | pci_read_config_word(bridge, PCI_IO_BASE, &io); |
| 235 | if (!io) { |
| 236 | pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0); |
| 237 | pci_read_config_word(bridge, PCI_IO_BASE, &io); |
| 238 | pci_write_config_word(bridge, PCI_IO_BASE, 0x0); |
| 239 | } |
| 240 | if (io) |
| 241 | b_res[0].flags |= IORESOURCE_IO; |
| 242 | /* DECchip 21050 pass 2 errata: the bridge may miss an address |
| 243 | disconnect boundary by one PCI data phase. |
| 244 | Workaround: do not use prefetching on this device. */ |
| 245 | if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001) |
| 246 | return; |
| 247 | pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); |
| 248 | if (!pmem) { |
| 249 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, |
| 250 | 0xfff0fff0); |
| 251 | pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem); |
| 252 | pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0); |
| 253 | } |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 254 | if (pmem) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 256 | if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) |
| 257 | b_res[2].flags |= IORESOURCE_MEM_64; |
| 258 | } |
| 259 | |
| 260 | /* double check if bridge does support 64 bit pref */ |
| 261 | if (b_res[2].flags & IORESOURCE_MEM_64) { |
| 262 | u32 mem_base_hi, tmp; |
| 263 | pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 264 | &mem_base_hi); |
| 265 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 266 | 0xffffffff); |
| 267 | pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp); |
| 268 | if (!tmp) |
| 269 | b_res[2].flags &= ~IORESOURCE_MEM_64; |
| 270 | pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, |
| 271 | mem_base_hi); |
| 272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Helper function for sizing routines: find first available |
| 276 | bus resource of a given type. Note: we intentionally skip |
| 277 | the bus resources which have already been assigned (that is, |
| 278 | have non-NULL parent resource). */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 279 | 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] | 280 | { |
| 281 | int i; |
| 282 | struct resource *r; |
| 283 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | |
| 284 | IORESOURCE_PREFETCH; |
| 285 | |
| 286 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { |
| 287 | r = bus->resource[i]; |
Ivan Kokshaysky | 299de03 | 2005-06-15 18:59:27 +0400 | [diff] [blame] | 288 | if (r == &ioport_resource || r == &iomem_resource) |
| 289 | continue; |
Jesse Barnes | 55a1098 | 2009-10-27 09:39:18 -0700 | [diff] [blame] | 290 | if (r && (r->flags & type_mask) == type && !r->parent) |
| 291 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | /* Sizing the IO windows of the PCI-PCI bridge is trivial, |
| 297 | since these windows have 4K granularity and the IO ranges |
| 298 | of non-bridge PCI devices are limited to 256 bytes. |
| 299 | We must be careful with the ISA aliasing though. */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 300 | 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] | 301 | { |
| 302 | struct pci_dev *dev; |
| 303 | struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); |
| 304 | unsigned long size = 0, size1 = 0; |
| 305 | |
| 306 | if (!b_res) |
| 307 | return; |
| 308 | |
| 309 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 310 | int i; |
| 311 | |
| 312 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 313 | struct resource *r = &dev->resource[i]; |
| 314 | unsigned long r_size; |
| 315 | |
| 316 | if (r->parent || !(r->flags & IORESOURCE_IO)) |
| 317 | continue; |
Zhao, Yu | 022edd8 | 2008-10-13 19:24:28 +0800 | [diff] [blame] | 318 | r_size = resource_size(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | if (r_size < 0x400) |
| 321 | /* Might be re-aligned for ISA */ |
| 322 | size += r_size; |
| 323 | else |
| 324 | size1 += r_size; |
| 325 | } |
| 326 | } |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 327 | if (size < min_size) |
| 328 | size = min_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | /* To be fixed in 2.5: we should have sort of HAVE_ISA |
| 330 | flag in the struct pci_bus. */ |
| 331 | #if defined(CONFIG_ISA) || defined(CONFIG_EISA) |
| 332 | size = (size & 0xff) + ((size & ~0xffUL) << 2); |
| 333 | #endif |
Milind Arun Choudhary | 6f6f8c2 | 2007-07-09 11:55:51 -0700 | [diff] [blame] | 334 | size = ALIGN(size + size1, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | if (!size) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 336 | if (b_res->start || b_res->end) |
| 337 | dev_info(&bus->self->dev, "disabling bridge window " |
| 338 | "%pR to [bus %02x-%02x] (unused)\n", b_res, |
| 339 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | b_res->flags = 0; |
| 341 | return; |
| 342 | } |
| 343 | /* Alignment of the IO window is always 4K */ |
| 344 | b_res->start = 4096; |
| 345 | b_res->end = b_res->start + size - 1; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 346 | b_res->flags |= IORESOURCE_STARTALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* Calculate the size of the bus and minimal alignment which |
| 350 | guarantees that all child resources fit in this size. */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 351 | static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, |
| 352 | unsigned long type, resource_size_t min_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct pci_dev *dev; |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 355 | resource_size_t min_align, align, size; |
| 356 | resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | int order, max_order; |
| 358 | struct resource *b_res = find_free_bus_resource(bus, type); |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 359 | unsigned int mem64_mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | if (!b_res) |
| 362 | return 0; |
| 363 | |
| 364 | memset(aligns, 0, sizeof(aligns)); |
| 365 | max_order = 0; |
| 366 | size = 0; |
| 367 | |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 368 | mem64_mask = b_res->flags & IORESOURCE_MEM_64; |
| 369 | b_res->flags &= ~IORESOURCE_MEM_64; |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 372 | int i; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 375 | struct resource *r = &dev->resource[i]; |
Benjamin Herrenschmidt | c40a22e | 2007-12-10 17:32:15 +1100 | [diff] [blame] | 376 | resource_size_t r_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | if (r->parent || (r->flags & mask) != type) |
| 379 | continue; |
Zhao, Yu | 022edd8 | 2008-10-13 19:24:28 +0800 | [diff] [blame] | 380 | r_size = resource_size(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | /* For bridges size != alignment */ |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 382 | align = pci_resource_alignment(dev, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | order = __ffs(align) - 20; |
| 384 | if (order > 11) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 385 | dev_warn(&dev->dev, "disabling BAR %d: %pR " |
| 386 | "(bad alignment %#llx)\n", i, r, |
| 387 | (unsigned long long) align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | r->flags = 0; |
| 389 | continue; |
| 390 | } |
| 391 | size += r_size; |
| 392 | if (order < 0) |
| 393 | order = 0; |
| 394 | /* Exclude ranges with size > align from |
| 395 | calculation of the alignment. */ |
| 396 | if (r_size == align) |
| 397 | aligns[order] += align; |
| 398 | if (order > max_order) |
| 399 | max_order = order; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 400 | mem64_mask &= r->flags & IORESOURCE_MEM_64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 403 | if (size < min_size) |
| 404 | size = min_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | align = 0; |
| 407 | min_align = 0; |
| 408 | for (order = 0; order <= max_order; order++) { |
Jeremy Fitzhardinge | 8308c54 | 2008-09-11 01:31:50 -0700 | [diff] [blame] | 409 | resource_size_t align1 = 1; |
| 410 | |
| 411 | align1 <<= (order + 20); |
| 412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (!align) |
| 414 | min_align = align1; |
Milind Arun Choudhary | 6f6f8c2 | 2007-07-09 11:55:51 -0700 | [diff] [blame] | 415 | else if (ALIGN(align + min_align, min_align) < align1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | min_align = align1 >> 1; |
| 417 | align += aligns[order]; |
| 418 | } |
Milind Arun Choudhary | 6f6f8c2 | 2007-07-09 11:55:51 -0700 | [diff] [blame] | 419 | size = ALIGN(size, min_align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (!size) { |
Bjorn Helgaas | 865df57 | 2009-11-04 10:32:57 -0700 | [diff] [blame] | 421 | if (b_res->start || b_res->end) |
| 422 | dev_info(&bus->self->dev, "disabling bridge window " |
| 423 | "%pR to [bus %02x-%02x] (unused)\n", b_res, |
| 424 | bus->secondary, bus->subordinate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | b_res->flags = 0; |
| 426 | return 1; |
| 427 | } |
| 428 | b_res->start = min_align; |
| 429 | b_res->end = size + min_align - 1; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 430 | b_res->flags |= IORESOURCE_STARTALIGN; |
Yinghai Lu | 1f82de1 | 2009-04-23 20:48:32 -0700 | [diff] [blame] | 431 | b_res->flags |= mem64_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return 1; |
| 433 | } |
| 434 | |
Adrian Bunk | 5468ae6 | 2008-04-18 13:53:56 -0700 | [diff] [blame] | 435 | static void pci_bus_size_cardbus(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
| 437 | struct pci_dev *bridge = bus->self; |
| 438 | struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; |
| 439 | u16 ctrl; |
| 440 | |
| 441 | /* |
| 442 | * Reserve some resources for CardBus. We reserve |
| 443 | * a fixed amount of bus space for CardBus bridges. |
| 444 | */ |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 445 | b_res[0].start = 0; |
| 446 | b_res[0].end = pci_cardbus_io_size - 1; |
| 447 | b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 449 | b_res[1].start = 0; |
| 450 | b_res[1].end = pci_cardbus_io_size - 1; |
| 451 | b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * Check whether prefetchable memory is supported |
| 455 | * by this bridge. |
| 456 | */ |
| 457 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 458 | if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) { |
| 459 | ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0; |
| 460 | pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl); |
| 461 | pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * If we have prefetchable memory support, allocate |
| 466 | * two regions. Otherwise, allocate one region of |
| 467 | * twice the size. |
| 468 | */ |
| 469 | if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 470 | b_res[2].start = 0; |
| 471 | b_res[2].end = pci_cardbus_mem_size - 1; |
| 472 | b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 474 | b_res[3].start = 0; |
| 475 | b_res[3].end = pci_cardbus_mem_size - 1; |
| 476 | b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } else { |
Linus Torvalds | 934b702 | 2008-04-22 18:16:30 -0700 | [diff] [blame] | 478 | b_res[3].start = 0; |
| 479 | b_res[3].end = pci_cardbus_mem_size * 2 - 1; |
| 480 | b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
Sam Ravnborg | 451124a | 2008-02-02 22:33:43 +0100 | [diff] [blame] | 484 | void __ref pci_bus_size_bridges(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | struct pci_dev *dev; |
| 487 | unsigned long mask, prefmask; |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 488 | resource_size_t min_mem_size = 0, min_io_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 491 | struct pci_bus *b = dev->subordinate; |
| 492 | if (!b) |
| 493 | continue; |
| 494 | |
| 495 | switch (dev->class >> 8) { |
| 496 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 497 | pci_bus_size_cardbus(b); |
| 498 | break; |
| 499 | |
| 500 | case PCI_CLASS_BRIDGE_PCI: |
| 501 | default: |
| 502 | pci_bus_size_bridges(b); |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* The root bus? */ |
| 508 | if (!bus->self) |
| 509 | return; |
| 510 | |
| 511 | switch (bus->self->class >> 8) { |
| 512 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 513 | /* don't size cardbuses yet. */ |
| 514 | break; |
| 515 | |
| 516 | case PCI_CLASS_BRIDGE_PCI: |
| 517 | pci_bridge_check_ranges(bus); |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 518 | if (bus->self->is_hotplug_bridge) { |
| 519 | min_io_size = pci_hotplug_io_size; |
| 520 | min_mem_size = pci_hotplug_mem_size; |
| 521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | default: |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 523 | pbus_size_io(bus, min_io_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* If the bridge supports prefetchable range, size it |
| 525 | separately. If it doesn't, or its prefetchable window |
| 526 | has already been allocated by arch code, try |
| 527 | non-prefetchable range for both types of PCI memory |
| 528 | resources. */ |
| 529 | mask = IORESOURCE_MEM; |
| 530 | prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 531 | if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | mask = prefmask; /* Success, size non-prefetch only. */ |
Eric W. Biederman | 2876048 | 2009-09-09 14:09:24 -0700 | [diff] [blame] | 533 | else |
| 534 | min_mem_size += min_mem_size; |
| 535 | pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | } |
| 539 | EXPORT_SYMBOL(pci_bus_size_bridges); |
| 540 | |
Andrew Morton | ea74155 | 2009-02-18 10:44:29 -0800 | [diff] [blame] | 541 | void __ref pci_bus_assign_resources(const struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
| 543 | struct pci_bus *b; |
| 544 | struct pci_dev *dev; |
| 545 | |
| 546 | pbus_assign_resources_sorted(bus); |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 549 | b = dev->subordinate; |
| 550 | if (!b) |
| 551 | continue; |
| 552 | |
| 553 | pci_bus_assign_resources(b); |
| 554 | |
| 555 | switch (dev->class >> 8) { |
| 556 | case PCI_CLASS_BRIDGE_PCI: |
| 557 | pci_setup_bridge(b); |
| 558 | break; |
| 559 | |
| 560 | case PCI_CLASS_BRIDGE_CARDBUS: |
| 561 | pci_setup_cardbus(b); |
| 562 | break; |
| 563 | |
| 564 | default: |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 565 | dev_info(&dev->dev, "not setting up bridge for bus " |
| 566 | "%04x:%02x\n", pci_domain_nr(b), b->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | EXPORT_SYMBOL(pci_bus_assign_resources); |
| 572 | |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 573 | static void pci_bus_dump_res(struct pci_bus *bus) |
| 574 | { |
| 575 | int i; |
| 576 | |
| 577 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { |
| 578 | struct resource *res = bus->resource[i]; |
Yinghai Lu | 681bf59 | 2009-04-13 18:28:54 -0700 | [diff] [blame] | 579 | if (!res || !res->end) |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 580 | continue; |
| 581 | |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 582 | dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res); |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | static void pci_bus_dump_resources(struct pci_bus *bus) |
| 587 | { |
| 588 | struct pci_bus *b; |
| 589 | struct pci_dev *dev; |
| 590 | |
| 591 | |
| 592 | pci_bus_dump_res(bus); |
| 593 | |
| 594 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 595 | b = dev->subordinate; |
| 596 | if (!b) |
| 597 | continue; |
| 598 | |
| 599 | pci_bus_dump_resources(b); |
| 600 | } |
| 601 | } |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | void __init |
| 604 | pci_assign_unassigned_resources(void) |
| 605 | { |
| 606 | struct pci_bus *bus; |
| 607 | |
| 608 | /* Depth first, calculate sizes and alignments of all |
| 609 | subordinate buses. */ |
| 610 | list_for_each_entry(bus, &pci_root_buses, node) { |
| 611 | pci_bus_size_bridges(bus); |
| 612 | } |
| 613 | /* Depth last, allocate resources and update the hardware. */ |
| 614 | list_for_each_entry(bus, &pci_root_buses, node) { |
| 615 | pci_bus_assign_resources(bus); |
| 616 | pci_enable_bridges(bus); |
| 617 | } |
Yinghai Lu | 76fbc26 | 2008-06-23 20:33:06 +0200 | [diff] [blame] | 618 | |
| 619 | /* dump the resource on buses */ |
| 620 | list_for_each_entry(bus, &pci_root_buses, node) { |
| 621 | pci_bus_dump_resources(bus); |
| 622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |