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