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