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 | |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 54 | pr_debug(" got res [%llx:%llx] bus [%lx:%lx] flags %lx for " |
| 55 | "BAR %d of %s\n", (unsigned long long)res->start, |
| 56 | (unsigned long long)res->end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | region.start, region.end, res->flags, resno, pci_name(dev)); |
| 58 | |
| 59 | new = region.start | (res->flags & PCI_REGION_FLAG_MASK); |
| 60 | if (res->flags & IORESOURCE_IO) |
| 61 | mask = (u32)PCI_BASE_ADDRESS_IO_MASK; |
| 62 | else |
| 63 | mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; |
| 64 | |
| 65 | if (resno < 6) { |
| 66 | reg = PCI_BASE_ADDRESS_0 + 4 * resno; |
| 67 | } else if (resno == PCI_ROM_RESOURCE) { |
Linus Torvalds | 755528c | 2005-08-26 10:49:22 -0700 | [diff] [blame] | 68 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
| 69 | return; |
| 70 | new |= PCI_ROM_ADDRESS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | reg = dev->rom_base_reg; |
| 72 | } else { |
| 73 | /* Hmm, non-standard resource. */ |
| 74 | |
| 75 | return; /* kill uninitialised var warning */ |
| 76 | } |
| 77 | |
| 78 | pci_write_config_dword(dev, reg, new); |
| 79 | pci_read_config_dword(dev, reg, &check); |
| 80 | |
| 81 | if ((new ^ check) & mask) { |
| 82 | printk(KERN_ERR "PCI: Error while updating region " |
| 83 | "%s/%d (%08x != %08x)\n", pci_name(dev), resno, |
| 84 | new, check); |
| 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) { |
| 93 | printk(KERN_ERR "PCI: Error updating region " |
| 94 | "%s/%d (high %08x != %08x)\n", |
| 95 | pci_name(dev), resno, new, check); |
| 96 | } |
| 97 | } |
| 98 | res->flags &= ~IORESOURCE_UNSET; |
| 99 | pr_debug("PCI: moved device %s resource %d (%lx) to %x\n", |
| 100 | pci_name(dev), resno, res->flags, |
| 101 | new & ~PCI_REGION_FLAG_MASK); |
| 102 | } |
| 103 | |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 104 | int pci_claim_resource(struct pci_dev *dev, int resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | struct resource *res = &dev->resource[resource]; |
| 107 | struct resource *root = NULL; |
| 108 | char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge"; |
| 109 | int err; |
| 110 | |
David S. Miller | 085ae41 | 2005-08-08 13:19:08 -0700 | [diff] [blame] | 111 | root = pcibios_select_root(dev, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | err = -EINVAL; |
| 114 | if (root != NULL) |
| 115 | err = insert_resource(root, res); |
| 116 | |
| 117 | if (err) { |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 118 | printk(KERN_ERR "PCI: %s region %d of %s %s [%llx:%llx]\n", |
| 119 | root ? "Address space collision on" : |
| 120 | "No parent found for", |
| 121 | resource, dtype, pci_name(dev), |
| 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 | } |
linas | dd8c499 | 2006-01-10 15:15:47 -0600 | [diff] [blame] | 128 | EXPORT_SYMBOL_GPL(pci_claim_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | int pci_assign_resource(struct pci_dev *dev, int resno) |
| 131 | { |
| 132 | struct pci_bus *bus = dev->bus; |
| 133 | struct resource *res = dev->resource + resno; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 134 | resource_size_t size, min, align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | int ret; |
| 136 | |
| 137 | size = res->end - res->start + 1; |
| 138 | min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; |
| 139 | /* The bridge resources are special, as their |
| 140 | size != alignment. Sizing routines return |
| 141 | required alignment in the "start" field. */ |
| 142 | align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start; |
| 143 | |
| 144 | /* First, try exact prefetching match.. */ |
| 145 | ret = pci_bus_alloc_resource(bus, res, size, align, min, |
| 146 | IORESOURCE_PREFETCH, |
| 147 | pcibios_align_resource, dev); |
| 148 | |
| 149 | if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) { |
| 150 | /* |
| 151 | * That failed. |
| 152 | * |
| 153 | * But a prefetching area can handle a non-prefetching |
| 154 | * window (it will just not perform as well). |
| 155 | */ |
| 156 | ret = pci_bus_alloc_resource(bus, res, size, align, min, 0, |
| 157 | pcibios_align_resource, dev); |
| 158 | } |
| 159 | |
| 160 | if (ret) { |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 161 | printk(KERN_ERR "PCI: Failed to allocate %s resource " |
| 162 | "#%d:%llx@%llx for %s\n", |
| 163 | res->flags & IORESOURCE_IO ? "I/O" : "mem", |
| 164 | resno, (unsigned long long)size, |
| 165 | (unsigned long long)res->start, pci_name(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } else if (resno < PCI_BRIDGE_RESOURCES) { |
| 167 | pci_update_resource(dev, res, resno); |
| 168 | } |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Kumar Gala | 75acfeca | 2006-05-01 10:43:46 -0500 | [diff] [blame] | 173 | #ifdef CONFIG_EMBEDDED |
| 174 | int pci_assign_resource_fixed(struct pci_dev *dev, int resno) |
| 175 | { |
| 176 | struct pci_bus *bus = dev->bus; |
| 177 | struct resource *res = dev->resource + resno; |
| 178 | unsigned int type_mask; |
| 179 | int i, ret = -EBUSY; |
| 180 | |
| 181 | type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH; |
| 182 | |
| 183 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { |
| 184 | struct resource *r = bus->resource[i]; |
| 185 | if (!r) |
| 186 | continue; |
| 187 | |
| 188 | /* type_mask must match */ |
| 189 | if ((res->flags ^ r->flags) & type_mask) |
| 190 | continue; |
| 191 | |
| 192 | ret = request_resource(r, res); |
| 193 | |
| 194 | if (ret == 0) |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | if (ret) { |
| 199 | printk(KERN_ERR "PCI: Failed to allocate %s resource " |
| 200 | "#%d:%llx@%llx for %s\n", |
| 201 | res->flags & IORESOURCE_IO ? "I/O" : "mem", |
| 202 | resno, (unsigned long long)(res->end - res->start + 1), |
| 203 | (unsigned long long)res->start, pci_name(dev)); |
| 204 | } else if (resno < PCI_BRIDGE_RESOURCES) { |
| 205 | pci_update_resource(dev, res, resno); |
| 206 | } |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | EXPORT_SYMBOL_GPL(pci_assign_resource_fixed); |
| 211 | #endif |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* Sort resources by alignment */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 214 | void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | int i; |
| 217 | |
| 218 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 219 | struct resource *r; |
| 220 | struct resource_list *list, *tmp; |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 221 | resource_size_t r_align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | r = &dev->resource[i]; |
Ralf Baechle | fb0f2b4 | 2006-12-19 13:12:08 -0800 | [diff] [blame] | 224 | |
| 225 | if (r->flags & IORESOURCE_PCI_FIXED) |
| 226 | continue; |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | r_align = r->end - r->start; |
| 229 | |
| 230 | if (!(r->flags) || r->parent) |
| 231 | continue; |
| 232 | if (!r_align) { |
| 233 | printk(KERN_WARNING "PCI: Ignore bogus resource %d " |
Greg Kroah-Hartman | 1396a8c | 2006-06-12 15:14:29 -0700 | [diff] [blame] | 234 | "[%llx:%llx] of %s\n", |
| 235 | i, (unsigned long long)r->start, |
| 236 | (unsigned long long)r->end, pci_name(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | continue; |
| 238 | } |
| 239 | r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start; |
| 240 | for (list = head; ; list = list->next) { |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 241 | resource_size_t align = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | struct resource_list *ln = list->next; |
| 243 | int idx; |
| 244 | |
| 245 | if (ln) { |
| 246 | idx = ln->res - &ln->dev->resource[0]; |
| 247 | align = (idx < PCI_BRIDGE_RESOURCES) ? |
| 248 | ln->res->end - ln->res->start + 1 : |
| 249 | ln->res->start; |
| 250 | } |
| 251 | if (r_align > align) { |
| 252 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 253 | if (!tmp) |
| 254 | panic("pdev_sort_resources(): " |
| 255 | "kmalloc() failed!\n"); |
| 256 | tmp->next = ln; |
| 257 | tmp->res = r; |
| 258 | tmp->dev = dev; |
| 259 | list->next = tmp; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |