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 | |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 29 | void pci_update_resource(struct pci_dev *dev, struct resource *res, 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; |
| 34 | |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 35 | /* |
| 36 | * Ignore resources for unimplemented BARs and unused resource slots |
| 37 | * for 64 bit BARs. |
| 38 | */ |
Ivan Kokshaysky | cf7bee5 | 2005-08-07 13:49:59 +0400 | [diff] [blame] | 39 | if (!res->flags) |
| 40 | return; |
| 41 | |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 42 | /* |
| 43 | * Ignore non-moveable resources. This might be legacy resources for |
| 44 | * which no functional BAR register exists or another important |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 45 | * system resource we shouldn't move around. |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 46 | */ |
| 47 | if (res->flags & IORESOURCE_PCI_FIXED) |
| 48 | return; |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | pcibios_resource_to_bus(dev, ®ion, res); |
| 51 | |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 52 | dev_dbg(&dev->dev, "BAR %d: got res [%#llx-%#llx] bus [%#llx-%#llx] " |
| 53 | "flags %#lx\n", resno, |
| 54 | (unsigned long long)res->start, |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 55 | (unsigned long long)res->end, |
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 | |
| 66 | if (resno < 6) { |
| 67 | reg = PCI_BASE_ADDRESS_0 + 4 * resno; |
| 68 | } else if (resno == PCI_ROM_RESOURCE) { |
Linus Torvalds | 755528c | 2005-08-26 10:49:22 -0700 | [diff] [blame] | 69 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
| 70 | return; |
| 71 | new |= PCI_ROM_ADDRESS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | reg = dev->rom_base_reg; |
| 73 | } else { |
| 74 | /* Hmm, non-standard resource. */ |
| 75 | |
| 76 | return; /* kill uninitialised var warning */ |
| 77 | } |
| 78 | |
| 79 | pci_write_config_dword(dev, reg, new); |
| 80 | pci_read_config_dword(dev, reg, &check); |
| 81 | |
| 82 | if ((new ^ check) & mask) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 83 | dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n", |
| 84 | resno, new, check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) == |
| 88 | (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) { |
Ivan Kokshaysky | cf7bee5 | 2005-08-07 13:49:59 +0400 | [diff] [blame] | 89 | new = region.start >> 16 >> 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | pci_write_config_dword(dev, reg + 4, new); |
| 91 | pci_read_config_dword(dev, reg + 4, &check); |
| 92 | if (check != new) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 93 | dev_err(&dev->dev, "BAR %d: error updating " |
| 94 | "(high %#08x != %#08x)\n", resno, new, check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | res->flags &= ~IORESOURCE_UNSET; |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 98 | dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n", |
| 99 | resno, (unsigned long long)region.start, |
| 100 | (unsigned long long)region.end, res->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 103 | int pci_claim_resource(struct pci_dev *dev, int resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | struct resource *res = &dev->resource[resource]; |
| 106 | struct resource *root = NULL; |
| 107 | char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge"; |
| 108 | int err; |
| 109 | |
David S. Miller | 085ae41 | 2005-08-08 13:19:08 -0700 | [diff] [blame] | 110 | root = pcibios_select_root(dev, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | err = -EINVAL; |
| 113 | if (root != NULL) |
| 114 | err = insert_resource(root, res); |
| 115 | |
| 116 | if (err) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 117 | dev_err(&dev->dev, "BAR %d: %s of %s [%#llx-%#llx]\n", |
| 118 | resource, |
| 119 | root ? "address space collision on" : |
| 120 | "no parent found for", |
| 121 | dtype, |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 122 | (unsigned long long)res->start, |
| 123 | (unsigned long long)res->end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return err; |
| 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | int pci_assign_resource(struct pci_dev *dev, int resno) |
| 130 | { |
| 131 | struct pci_bus *bus = dev->bus; |
| 132 | struct resource *res = dev->resource + resno; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 133 | resource_size_t size, min, align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | int ret; |
| 135 | |
| 136 | size = res->end - res->start + 1; |
| 137 | min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 138 | |
| 139 | align = resource_alignment(res); |
| 140 | if (!align) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 141 | dev_err(&dev->dev, "BAR %d: can't allocate resource (bogus " |
| 142 | "alignment) [%#llx-%#llx] flags %#lx\n", |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 143 | resno, (unsigned long long)res->start, |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 144 | (unsigned long long)res->end, res->flags); |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 145 | return -EINVAL; |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* First, try exact prefetching match.. */ |
| 149 | ret = pci_bus_alloc_resource(bus, res, size, align, min, |
| 150 | IORESOURCE_PREFETCH, |
| 151 | pcibios_align_resource, dev); |
| 152 | |
| 153 | if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) { |
| 154 | /* |
| 155 | * That failed. |
| 156 | * |
| 157 | * But a prefetching area can handle a non-prefetching |
| 158 | * window (it will just not perform as well). |
| 159 | */ |
| 160 | ret = pci_bus_alloc_resource(bus, res, size, align, min, 0, |
| 161 | pcibios_align_resource, dev); |
| 162 | } |
| 163 | |
| 164 | if (ret) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 165 | dev_err(&dev->dev, "BAR %d: can't allocate %s resource " |
| 166 | "[%#llx-%#llx]\n", resno, |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 167 | res->flags & IORESOURCE_IO ? "I/O" : "mem", |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 168 | (unsigned long long)res->start, |
| 169 | (unsigned long long)res->end); |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 170 | } else { |
| 171 | res->flags &= ~IORESOURCE_STARTALIGN; |
| 172 | if (resno < PCI_BRIDGE_RESOURCES) |
| 173 | pci_update_resource(dev, res, resno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
Adrian Bunk | 2baad5f | 2008-02-13 23:30:12 +0200 | [diff] [blame] | 179 | #if 0 |
Kumar Gala | 75acfeca | 2006-05-01 10:43:46 -0500 | [diff] [blame] | 180 | int pci_assign_resource_fixed(struct pci_dev *dev, int resno) |
| 181 | { |
| 182 | struct pci_bus *bus = dev->bus; |
| 183 | struct resource *res = dev->resource + resno; |
| 184 | unsigned int type_mask; |
| 185 | int i, ret = -EBUSY; |
| 186 | |
| 187 | type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH; |
| 188 | |
| 189 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { |
| 190 | struct resource *r = bus->resource[i]; |
| 191 | if (!r) |
| 192 | continue; |
| 193 | |
| 194 | /* type_mask must match */ |
| 195 | if ((res->flags ^ r->flags) & type_mask) |
| 196 | continue; |
| 197 | |
| 198 | ret = request_resource(r, res); |
| 199 | |
| 200 | if (ret == 0) |
| 201 | break; |
| 202 | } |
| 203 | |
| 204 | if (ret) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 205 | dev_err(&dev->dev, "BAR %d: can't allocate %s resource " |
| 206 | "[%#llx-%#llx\n]", resno, |
Kumar Gala | 75acfeca | 2006-05-01 10:43:46 -0500 | [diff] [blame] | 207 | res->flags & IORESOURCE_IO ? "I/O" : "mem", |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 208 | (unsigned long long)res->start, |
| 209 | (unsigned long long)res->end); |
Kumar Gala | 75acfeca | 2006-05-01 10:43:46 -0500 | [diff] [blame] | 210 | } else if (resno < PCI_BRIDGE_RESOURCES) { |
| 211 | pci_update_resource(dev, res, resno); |
| 212 | } |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(pci_assign_resource_fixed); |
| 217 | #endif |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* Sort resources by alignment */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 220 | void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | int i; |
| 223 | |
| 224 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 225 | struct resource *r; |
| 226 | struct resource_list *list, *tmp; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 227 | resource_size_t r_align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | r = &dev->resource[i]; |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 230 | |
| 231 | if (r->flags & IORESOURCE_PCI_FIXED) |
| 232 | continue; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | if (!(r->flags) || r->parent) |
| 235 | continue; |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 236 | |
| 237 | r_align = resource_alignment(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (!r_align) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 239 | dev_warn(&dev->dev, "BAR %d: bogus alignment " |
| 240 | "[%#llx-%#llx] flags %#lx\n", |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 241 | i, (unsigned long long)r->start, |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 242 | (unsigned long long)r->end, r->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | continue; |
| 244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | for (list = head; ; list = list->next) { |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 246 | resource_size_t align = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | struct resource_list *ln = list->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Ivan Kokshaysky | 8845256 | 2008-03-30 19:50:14 +0400 | [diff] [blame] | 249 | if (ln) |
| 250 | align = resource_alignment(ln->res); |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | if (r_align > align) { |
| 253 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 254 | if (!tmp) |
| 255 | panic("pdev_sort_resources(): " |
| 256 | "kmalloc() failed!\n"); |
| 257 | tmp->next = ln; |
| 258 | tmp->res = r; |
| 259 | tmp->dev = dev; |
| 260 | list->next = tmp; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
Bjorn Helgaas | 842de40 | 2008-03-04 11:56:47 -0700 | [diff] [blame] | 266 | |
| 267 | int pci_enable_resources(struct pci_dev *dev, int mask) |
| 268 | { |
| 269 | u16 cmd, old_cmd; |
| 270 | int i; |
| 271 | struct resource *r; |
| 272 | |
| 273 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 274 | old_cmd = cmd; |
| 275 | |
| 276 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 277 | if (!(mask & (1 << i))) |
| 278 | continue; |
| 279 | |
| 280 | r = &dev->resource[i]; |
| 281 | |
| 282 | if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) |
| 283 | continue; |
| 284 | if ((i == PCI_ROM_RESOURCE) && |
| 285 | (!(r->flags & IORESOURCE_ROM_ENABLE))) |
| 286 | continue; |
| 287 | |
| 288 | if (!r->parent) { |
| 289 | dev_err(&dev->dev, "device not available because of " |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 290 | "BAR %d [%#llx-%#llx] collisions\n", i, |
Bjorn Helgaas | 842de40 | 2008-03-04 11:56:47 -0700 | [diff] [blame] | 291 | (unsigned long long) r->start, |
| 292 | (unsigned long long) r->end); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | |
| 296 | if (r->flags & IORESOURCE_IO) |
| 297 | cmd |= PCI_COMMAND_IO; |
| 298 | if (r->flags & IORESOURCE_MEM) |
| 299 | cmd |= PCI_COMMAND_MEMORY; |
| 300 | } |
| 301 | |
| 302 | if (cmd != old_cmd) { |
| 303 | dev_info(&dev->dev, "enabling device (%04x -> %04x)\n", |
| 304 | old_cmd, cmd); |
| 305 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 306 | } |
| 307 | return 0; |
| 308 | } |