Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/pci/setup-res.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 | /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */ |
| 13 | |
| 14 | /* |
| 15 | * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru> |
| 16 | * Resource sorting |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/cache.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include "pci.h" |
| 27 | |
| 28 | |
Yu Zhao | 14add80 | 2008-11-22 02:38:52 +0800 | [diff] [blame] | 29 | void pci_update_resource(struct pci_dev *dev, int resno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | struct pci_bus_region region; |
| 32 | u32 new, check, mask; |
| 33 | int reg; |
Yu Zhao | 613e7ed | 2008-11-22 02:41:27 +0800 | [diff] [blame] | 34 | enum pci_bar_type type; |
Yu Zhao | 14add80 | 2008-11-22 02:38:52 +0800 | [diff] [blame] | 35 | struct resource *res = dev->resource + resno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 37 | /* |
| 38 | * Ignore resources for unimplemented BARs and unused resource slots |
| 39 | * for 64 bit BARs. |
| 40 | */ |
Ivan Kokshaysky | cf7bee5 | 2005-08-07 13:49:59 +0400 | [diff] [blame] | 41 | if (!res->flags) |
| 42 | return; |
| 43 | |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 44 | /* |
| 45 | * Ignore non-moveable resources. This might be legacy resources for |
| 46 | * which no functional BAR register exists or another important |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 47 | * system resource we shouldn't move around. |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 48 | */ |
| 49 | if (res->flags & IORESOURCE_PCI_FIXED) |
| 50 | return; |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | pcibios_resource_to_bus(dev, ®ion, res); |
| 53 | |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 54 | dev_dbg(&dev->dev, "BAR %d: got res %pR bus [%#llx-%#llx] " |
| 55 | "flags %#lx\n", resno, res, |
Benjamin Herrenschmidt | 6015fbe | 2007-12-10 17:32:16 +1100 | [diff] [blame] | 56 | (unsigned long long)region.start, |
| 57 | (unsigned long long)region.end, |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 58 | (unsigned long)res->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | new = region.start | (res->flags & PCI_REGION_FLAG_MASK); |
| 61 | if (res->flags & IORESOURCE_IO) |
| 62 | mask = (u32)PCI_BASE_ADDRESS_IO_MASK; |
| 63 | else |
| 64 | mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; |
| 65 | |
Yu Zhao | 613e7ed | 2008-11-22 02:41:27 +0800 | [diff] [blame] | 66 | reg = pci_resource_bar(dev, resno, &type); |
| 67 | if (!reg) |
| 68 | return; |
| 69 | if (type != pci_bar_unknown) { |
Linus Torvalds | 755528c | 2005-08-26 10:49:22 -0700 | [diff] [blame] | 70 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
| 71 | return; |
| 72 | new |= PCI_ROM_ADDRESS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | pci_write_config_dword(dev, reg, new); |
| 76 | pci_read_config_dword(dev, reg, &check); |
| 77 | |
| 78 | if ((new ^ check) & mask) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 79 | dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n", |
| 80 | resno, new, check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) == |
| 84 | (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) { |
Ivan Kokshaysky | cf7bee5 | 2005-08-07 13:49:59 +0400 | [diff] [blame] | 85 | new = region.start >> 16 >> 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | pci_write_config_dword(dev, reg + 4, new); |
| 87 | pci_read_config_dword(dev, reg + 4, &check); |
| 88 | if (check != new) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 89 | dev_err(&dev->dev, "BAR %d: error updating " |
| 90 | "(high %#08x != %#08x)\n", resno, new, check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | res->flags &= ~IORESOURCE_UNSET; |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 94 | dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n", |
| 95 | resno, (unsigned long long)region.start, |
| 96 | (unsigned long long)region.end, res->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 99 | int pci_claim_resource(struct pci_dev *dev, int resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | struct resource *res = &dev->resource[resource]; |
Matthew Wilcox | cebd78a | 2009-06-17 16:33:33 -0400 | [diff] [blame] | 102 | struct resource *root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge"; |
| 104 | int err; |
| 105 | |
Matthew Wilcox | cebd78a | 2009-06-17 16:33:33 -0400 | [diff] [blame] | 106 | root = pci_find_parent_resource(dev, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | err = -EINVAL; |
| 109 | if (root != NULL) |
| 110 | err = insert_resource(root, res); |
| 111 | |
| 112 | if (err) { |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 113 | dev_err(&dev->dev, "BAR %d: %s of %s %pR\n", |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 114 | resource, |
| 115 | root ? "address space collision on" : |
| 116 | "no parent found for", |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 117 | dtype, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return err; |
| 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Yuji Shimada | 32a9a682 | 2009-03-16 17:13:39 +0900 | [diff] [blame] | 123 | #ifdef CONFIG_PCI_QUIRKS |
| 124 | void pci_disable_bridge_window(struct pci_dev *dev) |
| 125 | { |
| 126 | dev_dbg(&dev->dev, "Disabling bridge window.\n"); |
| 127 | |
| 128 | /* MMIO Base/Limit */ |
| 129 | pci_write_config_dword(dev, PCI_MEMORY_BASE, 0x0000fff0); |
| 130 | |
| 131 | /* Prefetchable MMIO Base/Limit */ |
| 132 | pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0); |
| 133 | pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0); |
| 134 | pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff); |
| 135 | } |
| 136 | #endif /* CONFIG_PCI_QUIRKS */ |
| 137 | |
Yinghai Lu | d09ee968 | 2009-04-23 20:49:25 -0700 | [diff] [blame] | 138 | static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev, |
| 139 | int resno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct resource *res = dev->resource + resno; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 142 | resource_size_t size, min, align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | int ret; |
| 144 | |
Zhao, Yu | 022edd8 | 2008-10-13 19:24:28 +0800 | [diff] [blame] | 145 | size = resource_size(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 147 | align = resource_alignment(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | /* First, try exact prefetching match.. */ |
| 150 | ret = pci_bus_alloc_resource(bus, res, size, align, min, |
| 151 | IORESOURCE_PREFETCH, |
| 152 | pcibios_align_resource, dev); |
| 153 | |
| 154 | if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) { |
| 155 | /* |
| 156 | * That failed. |
| 157 | * |
| 158 | * But a prefetching area can handle a non-prefetching |
| 159 | * window (it will just not perform as well). |
| 160 | */ |
| 161 | ret = pci_bus_alloc_resource(bus, res, size, align, min, 0, |
| 162 | pcibios_align_resource, dev); |
| 163 | } |
| 164 | |
Yinghai Lu | d09ee968 | 2009-04-23 20:49:25 -0700 | [diff] [blame] | 165 | if (!ret) { |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 166 | res->flags &= ~IORESOURCE_STARTALIGN; |
| 167 | if (resno < PCI_BRIDGE_RESOURCES) |
Yu Zhao | 14add80 | 2008-11-22 02:38:52 +0800 | [diff] [blame] | 168 | pci_update_resource(dev, resno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
Yinghai Lu | d09ee968 | 2009-04-23 20:49:25 -0700 | [diff] [blame] | 174 | int pci_assign_resource(struct pci_dev *dev, int resno) |
| 175 | { |
| 176 | struct resource *res = dev->resource + resno; |
| 177 | resource_size_t align; |
| 178 | struct pci_bus *bus; |
| 179 | int ret; |
| 180 | |
| 181 | align = resource_alignment(res); |
| 182 | if (!align) { |
| 183 | dev_info(&dev->dev, "BAR %d: can't allocate resource (bogus " |
| 184 | "alignment) %pR flags %#lx\n", |
| 185 | resno, res, res->flags); |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | bus = dev->bus; |
| 190 | while ((ret = __pci_assign_resource(bus, dev, resno))) { |
| 191 | if (bus->parent && bus->self->transparent) |
| 192 | bus = bus->parent; |
| 193 | else |
| 194 | bus = NULL; |
| 195 | if (bus) |
| 196 | continue; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | if (ret) |
| 201 | dev_info(&dev->dev, "BAR %d: can't allocate %s resource %pR\n", |
| 202 | resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res); |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
Adrian Bunk | 2baad5f | 2008-02-13 23:30:12 +0200 | [diff] [blame] | 207 | #if 0 |
Kumar Gala | 75acfeca | 2006-05-01 10:43:46 -0500 | [diff] [blame] | 208 | int pci_assign_resource_fixed(struct pci_dev *dev, int resno) |
| 209 | { |
| 210 | struct pci_bus *bus = dev->bus; |
| 211 | struct resource *res = dev->resource + resno; |
| 212 | unsigned int type_mask; |
| 213 | int i, ret = -EBUSY; |
| 214 | |
| 215 | type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH; |
| 216 | |
| 217 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { |
| 218 | struct resource *r = bus->resource[i]; |
| 219 | if (!r) |
| 220 | continue; |
| 221 | |
| 222 | /* type_mask must match */ |
| 223 | if ((res->flags ^ r->flags) & type_mask) |
| 224 | continue; |
| 225 | |
| 226 | ret = request_resource(r, res); |
| 227 | |
| 228 | if (ret == 0) |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | if (ret) { |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 233 | dev_err(&dev->dev, "BAR %d: can't allocate %s resource %pR\n", |
| 234 | resno, res->flags & IORESOURCE_IO ? "I/O" : "mem", res); |
Kumar Gala | 75acfeca | 2006-05-01 10:43:46 -0500 | [diff] [blame] | 235 | } else if (resno < PCI_BRIDGE_RESOURCES) { |
Yu Zhao | 14add80 | 2008-11-22 02:38:52 +0800 | [diff] [blame] | 236 | pci_update_resource(dev, resno); |
Kumar Gala | 75acfeca | 2006-05-01 10:43:46 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | EXPORT_SYMBOL_GPL(pci_assign_resource_fixed); |
| 242 | #endif |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | /* Sort resources by alignment */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 245 | void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | int i; |
| 248 | |
| 249 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 250 | struct resource *r; |
| 251 | struct resource_list *list, *tmp; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 252 | resource_size_t r_align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | r = &dev->resource[i]; |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 255 | |
| 256 | if (r->flags & IORESOURCE_PCI_FIXED) |
| 257 | continue; |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (!(r->flags) || r->parent) |
| 260 | continue; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 261 | |
| 262 | r_align = resource_alignment(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (!r_align) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 264 | dev_warn(&dev->dev, "BAR %d: bogus alignment " |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 265 | "%pR flags %#lx\n", |
| 266 | i, r, r->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | continue; |
| 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | for (list = head; ; list = list->next) { |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 270 | resource_size_t align = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | struct resource_list *ln = list->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 273 | if (ln) |
| 274 | align = resource_alignment(ln->res); |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (r_align > align) { |
| 277 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 278 | if (!tmp) |
| 279 | panic("pdev_sort_resources(): " |
| 280 | "kmalloc() failed!\n"); |
| 281 | tmp->next = ln; |
| 282 | tmp->res = r; |
| 283 | tmp->dev = dev; |
| 284 | list->next = tmp; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
Bjorn Helgaas | 842de40 | 2008-03-04 11:56:47 -0700 | [diff] [blame] | 290 | |
| 291 | int pci_enable_resources(struct pci_dev *dev, int mask) |
| 292 | { |
| 293 | u16 cmd, old_cmd; |
| 294 | int i; |
| 295 | struct resource *r; |
| 296 | |
| 297 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 298 | old_cmd = cmd; |
| 299 | |
| 300 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 301 | if (!(mask & (1 << i))) |
| 302 | continue; |
| 303 | |
| 304 | r = &dev->resource[i]; |
| 305 | |
| 306 | if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) |
| 307 | continue; |
| 308 | if ((i == PCI_ROM_RESOURCE) && |
| 309 | (!(r->flags & IORESOURCE_ROM_ENABLE))) |
| 310 | continue; |
| 311 | |
| 312 | if (!r->parent) { |
| 313 | dev_err(&dev->dev, "device not available because of " |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 314 | "BAR %d %pR collisions\n", i, r); |
Bjorn Helgaas | 842de40 | 2008-03-04 11:56:47 -0700 | [diff] [blame] | 315 | return -EINVAL; |
| 316 | } |
| 317 | |
| 318 | if (r->flags & IORESOURCE_IO) |
| 319 | cmd |= PCI_COMMAND_IO; |
| 320 | if (r->flags & IORESOURCE_MEM) |
| 321 | cmd |= PCI_COMMAND_MEMORY; |
| 322 | } |
| 323 | |
| 324 | if (cmd != old_cmd) { |
| 325 | dev_info(&dev->dev, "enabling device (%04x -> %04x)\n", |
| 326 | old_cmd, cmd); |
| 327 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 328 | } |
| 329 | return 0; |
| 330 | } |