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 | |
Linus Torvalds | dc836b5 | 2005-08-08 18:46:09 -0700 | [diff] [blame] | 29 | static 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 | |
| 43 | pr_debug(" got res [%lx:%lx] bus [%lx:%lx] flags %lx for " |
| 44 | "BAR %d of %s\n", res->start, res->end, |
| 45 | region.start, region.end, res->flags, resno, pci_name(dev)); |
| 46 | |
| 47 | new = region.start | (res->flags & PCI_REGION_FLAG_MASK); |
| 48 | if (res->flags & IORESOURCE_IO) |
| 49 | mask = (u32)PCI_BASE_ADDRESS_IO_MASK; |
| 50 | else |
| 51 | mask = (u32)PCI_BASE_ADDRESS_MEM_MASK; |
| 52 | |
| 53 | if (resno < 6) { |
| 54 | reg = PCI_BASE_ADDRESS_0 + 4 * resno; |
| 55 | } else if (resno == PCI_ROM_RESOURCE) { |
Linus Torvalds | 755528c | 2005-08-26 10:49:22 -0700 | [diff] [blame] | 56 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) |
| 57 | return; |
| 58 | new |= PCI_ROM_ADDRESS_ENABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | reg = dev->rom_base_reg; |
| 60 | } else { |
| 61 | /* Hmm, non-standard resource. */ |
| 62 | |
| 63 | return; /* kill uninitialised var warning */ |
| 64 | } |
| 65 | |
| 66 | pci_write_config_dword(dev, reg, new); |
| 67 | pci_read_config_dword(dev, reg, &check); |
| 68 | |
| 69 | if ((new ^ check) & mask) { |
| 70 | printk(KERN_ERR "PCI: Error while updating region " |
| 71 | "%s/%d (%08x != %08x)\n", pci_name(dev), resno, |
| 72 | new, check); |
| 73 | } |
| 74 | |
| 75 | if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) == |
| 76 | (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) { |
Ivan Kokshaysky | cf7bee5 | 2005-08-07 13:49:59 +0400 | [diff] [blame] | 77 | new = region.start >> 16 >> 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | pci_write_config_dword(dev, reg + 4, new); |
| 79 | pci_read_config_dword(dev, reg + 4, &check); |
| 80 | if (check != new) { |
| 81 | printk(KERN_ERR "PCI: Error updating region " |
| 82 | "%s/%d (high %08x != %08x)\n", |
| 83 | pci_name(dev), resno, new, check); |
| 84 | } |
| 85 | } |
| 86 | res->flags &= ~IORESOURCE_UNSET; |
| 87 | pr_debug("PCI: moved device %s resource %d (%lx) to %x\n", |
| 88 | pci_name(dev), resno, res->flags, |
| 89 | new & ~PCI_REGION_FLAG_MASK); |
| 90 | } |
| 91 | |
| 92 | int __devinit |
| 93 | pci_claim_resource(struct pci_dev *dev, int resource) |
| 94 | { |
| 95 | struct resource *res = &dev->resource[resource]; |
| 96 | struct resource *root = NULL; |
| 97 | char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge"; |
| 98 | int err; |
| 99 | |
| 100 | if (res->flags & IORESOURCE_IO) |
| 101 | root = &ioport_resource; |
| 102 | if (res->flags & IORESOURCE_MEM) |
| 103 | root = &iomem_resource; |
| 104 | |
| 105 | err = -EINVAL; |
| 106 | if (root != NULL) |
| 107 | err = insert_resource(root, res); |
| 108 | |
| 109 | if (err) { |
| 110 | printk(KERN_ERR "PCI: %s region %d of %s %s [%lx:%lx]\n", |
| 111 | root ? "Address space collision on" : |
| 112 | "No parent found for", |
| 113 | resource, dtype, pci_name(dev), res->start, res->end); |
| 114 | } |
| 115 | |
| 116 | return err; |
| 117 | } |
| 118 | |
| 119 | int pci_assign_resource(struct pci_dev *dev, int resno) |
| 120 | { |
| 121 | struct pci_bus *bus = dev->bus; |
| 122 | struct resource *res = dev->resource + resno; |
| 123 | unsigned long size, min, align; |
| 124 | int ret; |
| 125 | |
| 126 | size = res->end - res->start + 1; |
| 127 | min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM; |
| 128 | /* The bridge resources are special, as their |
| 129 | size != alignment. Sizing routines return |
| 130 | required alignment in the "start" field. */ |
| 131 | align = (resno < PCI_BRIDGE_RESOURCES) ? size : res->start; |
| 132 | |
| 133 | /* First, try exact prefetching match.. */ |
| 134 | ret = pci_bus_alloc_resource(bus, res, size, align, min, |
| 135 | IORESOURCE_PREFETCH, |
| 136 | pcibios_align_resource, dev); |
| 137 | |
| 138 | if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) { |
| 139 | /* |
| 140 | * That failed. |
| 141 | * |
| 142 | * But a prefetching area can handle a non-prefetching |
| 143 | * window (it will just not perform as well). |
| 144 | */ |
| 145 | ret = pci_bus_alloc_resource(bus, res, size, align, min, 0, |
| 146 | pcibios_align_resource, dev); |
| 147 | } |
| 148 | |
| 149 | if (ret) { |
| 150 | printk(KERN_ERR "PCI: Failed to allocate %s resource #%d:%lx@%lx for %s\n", |
| 151 | res->flags & IORESOURCE_IO ? "I/O" : "mem", |
| 152 | resno, size, res->start, pci_name(dev)); |
| 153 | } else if (resno < PCI_BRIDGE_RESOURCES) { |
| 154 | pci_update_resource(dev, res, resno); |
| 155 | } |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /* Sort resources by alignment */ |
| 161 | void __devinit |
| 162 | pdev_sort_resources(struct pci_dev *dev, struct resource_list *head) |
| 163 | { |
| 164 | int i; |
| 165 | |
| 166 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 167 | struct resource *r; |
| 168 | struct resource_list *list, *tmp; |
| 169 | unsigned long r_align; |
| 170 | |
| 171 | r = &dev->resource[i]; |
| 172 | r_align = r->end - r->start; |
| 173 | |
| 174 | if (!(r->flags) || r->parent) |
| 175 | continue; |
| 176 | if (!r_align) { |
| 177 | printk(KERN_WARNING "PCI: Ignore bogus resource %d " |
| 178 | "[%lx:%lx] of %s\n", |
| 179 | i, r->start, r->end, pci_name(dev)); |
| 180 | continue; |
| 181 | } |
| 182 | r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start; |
| 183 | for (list = head; ; list = list->next) { |
| 184 | unsigned long align = 0; |
| 185 | struct resource_list *ln = list->next; |
| 186 | int idx; |
| 187 | |
| 188 | if (ln) { |
| 189 | idx = ln->res - &ln->dev->resource[0]; |
| 190 | align = (idx < PCI_BRIDGE_RESOURCES) ? |
| 191 | ln->res->end - ln->res->start + 1 : |
| 192 | ln->res->start; |
| 193 | } |
| 194 | if (r_align > align) { |
| 195 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 196 | if (!tmp) |
| 197 | panic("pdev_sort_resources(): " |
| 198 | "kmalloc() failed!\n"); |
| 199 | tmp->next = ln; |
| 200 | tmp->res = r; |
| 201 | tmp->dev = dev; |
| 202 | list->next = tmp; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |