ia64/linux-2.6.18-xen.hg

annotate drivers/pci/setup-res.c @ 701:16c549b3ba59

xen: conditionalize PCI reassign code by a config option, selected
only from privileged Xen configurations.

Also eliminate the pointless new macro ROUND_UP_TO_PAGESIZE().

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Oct 14 14:58:08 2008 +0100 (2008-10-14)
parents 2b5cc22ab406
children 8fbf8e2fd087
rev   line source
ian@0 1 /*
ian@0 2 * drivers/pci/setup-res.c
ian@0 3 *
ian@0 4 * Extruded from code written by
ian@0 5 * Dave Rusling (david.rusling@reo.mts.dec.com)
ian@0 6 * David Mosberger (davidm@cs.arizona.edu)
ian@0 7 * David Miller (davem@redhat.com)
ian@0 8 *
ian@0 9 * Support routines for initializing a PCI subsystem.
ian@0 10 */
ian@0 11
ian@0 12 /* fixed for multiple pci buses, 1999 Andrea Arcangeli <andrea@suse.de> */
ian@0 13
ian@0 14 /*
ian@0 15 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
ian@0 16 * Resource sorting
ian@0 17 */
ian@0 18
ian@0 19 #include <linux/init.h>
ian@0 20 #include <linux/kernel.h>
ian@0 21 #include <linux/pci.h>
ian@0 22 #include <linux/errno.h>
ian@0 23 #include <linux/ioport.h>
ian@0 24 #include <linux/cache.h>
ian@0 25 #include <linux/slab.h>
ian@0 26 #include "pci.h"
ian@0 27
ian@0 28
ian@0 29 void
ian@0 30 pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
ian@0 31 {
ian@0 32 struct pci_bus_region region;
ian@0 33 u32 new, check, mask;
ian@0 34 int reg;
ian@0 35
ian@0 36 /* Ignore resources for unimplemented BARs and unused resource slots
ian@0 37 for 64 bit BARs. */
ian@0 38 if (!res->flags)
ian@0 39 return;
ian@0 40
ian@0 41 pcibios_resource_to_bus(dev, &region, res);
ian@0 42
ian@0 43 pr_debug(" got res [%llx:%llx] bus [%lx:%lx] flags %lx for "
ian@0 44 "BAR %d of %s\n", (unsigned long long)res->start,
ian@0 45 (unsigned long long)res->end,
ian@0 46 region.start, region.end, res->flags, resno, pci_name(dev));
ian@0 47
ian@0 48 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
ian@0 49 if (res->flags & IORESOURCE_IO)
ian@0 50 mask = (u32)PCI_BASE_ADDRESS_IO_MASK;
ian@0 51 else
ian@0 52 mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
ian@0 53
ian@0 54 if (resno < 6) {
ian@0 55 reg = PCI_BASE_ADDRESS_0 + 4 * resno;
ian@0 56 } else if (resno == PCI_ROM_RESOURCE) {
ian@0 57 if (!(res->flags & IORESOURCE_ROM_ENABLE))
ian@0 58 return;
ian@0 59 new |= PCI_ROM_ADDRESS_ENABLE;
ian@0 60 reg = dev->rom_base_reg;
ian@0 61 } else {
ian@0 62 /* Hmm, non-standard resource. */
ian@0 63
ian@0 64 return; /* kill uninitialised var warning */
ian@0 65 }
ian@0 66
ian@0 67 pci_write_config_dword(dev, reg, new);
ian@0 68 pci_read_config_dword(dev, reg, &check);
ian@0 69
ian@0 70 if ((new ^ check) & mask) {
ian@0 71 printk(KERN_ERR "PCI: Error while updating region "
ian@0 72 "%s/%d (%08x != %08x)\n", pci_name(dev), resno,
ian@0 73 new, check);
ian@0 74 }
ian@0 75
ian@0 76 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
ian@0 77 (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64)) {
ian@0 78 new = region.start >> 16 >> 16;
ian@0 79 pci_write_config_dword(dev, reg + 4, new);
ian@0 80 pci_read_config_dword(dev, reg + 4, &check);
ian@0 81 if (check != new) {
ian@0 82 printk(KERN_ERR "PCI: Error updating region "
ian@0 83 "%s/%d (high %08x != %08x)\n",
ian@0 84 pci_name(dev), resno, new, check);
ian@0 85 }
ian@0 86 }
ian@0 87 res->flags &= ~IORESOURCE_UNSET;
ian@0 88 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n",
ian@0 89 pci_name(dev), resno, res->flags,
ian@0 90 new & ~PCI_REGION_FLAG_MASK);
ian@0 91 }
ian@0 92
ian@0 93 int __devinit
ian@0 94 pci_claim_resource(struct pci_dev *dev, int resource)
ian@0 95 {
ian@0 96 struct resource *res = &dev->resource[resource];
ian@0 97 struct resource *root = NULL;
ian@0 98 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
ian@0 99 int err;
ian@0 100
ian@0 101 root = pcibios_select_root(dev, res);
ian@0 102
ian@0 103 err = -EINVAL;
ian@0 104 if (root != NULL)
ian@0 105 err = insert_resource(root, res);
ian@0 106
ian@0 107 if (err) {
ian@0 108 printk(KERN_ERR "PCI: %s region %d of %s %s [%llx:%llx]\n",
ian@0 109 root ? "Address space collision on" :
ian@0 110 "No parent found for",
ian@0 111 resource, dtype, pci_name(dev),
ian@0 112 (unsigned long long)res->start,
ian@0 113 (unsigned long long)res->end);
ian@0 114 }
ian@0 115
ian@0 116 return err;
ian@0 117 }
ian@0 118 EXPORT_SYMBOL_GPL(pci_claim_resource);
ian@0 119
keir@701 120 #ifdef CONFIG_PCI_REASSIGN
keir@701 121 void
keir@696 122 pci_update_bridge(struct pci_dev *dev, int resno)
keir@696 123 {
keir@696 124 struct resource *res = &dev->resource[resno];
keir@696 125 struct pci_bus_region region;
keir@696 126 u32 l, dw, base_up32, limit_up32;
keir@696 127
keir@696 128 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE ||
keir@696 129 (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) {
keir@696 130 return;
keir@696 131 }
keir@696 132
keir@696 133 if (!res->flags)
keir@696 134 return;
keir@696 135
keir@696 136 switch (resno) {
keir@696 137 case 8 : /* MMIO Base/Limit */
keir@696 138 pcibios_resource_to_bus(dev, &region, res);
keir@696 139 if (res->flags & IORESOURCE_MEM &&
keir@696 140 !(res->flags & IORESOURCE_PREFETCH)) {
keir@696 141 l = (region.start >> 16) & 0xfff0;
keir@696 142 l |= region.end & 0xfff00000;
keir@696 143 } else {
keir@696 144 l = 0x0000fff0;
keir@696 145 }
keir@696 146 pci_write_config_dword(dev, PCI_MEMORY_BASE, l);
keir@696 147
keir@696 148 break;
keir@696 149
keir@696 150 case 9 : /* Prefetchable MMIO Base/Limit */
keir@696 151 /* Clear out the upper 32 bits of PREF limit.
keir@696 152 * If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
keir@696 153 * disables PREF range, which is ok.
keir@696 154 */
keir@696 155 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
keir@696 156
keir@696 157 /* Get PREF 32/64 bits Addressing mode */
keir@696 158 pci_read_config_dword(dev, PCI_PREF_MEMORY_BASE, &dw);
keir@696 159
keir@696 160 pcibios_resource_to_bus(dev, &region, res);
keir@696 161 if (res->flags & IORESOURCE_MEM &&
keir@696 162 res->flags & IORESOURCE_PREFETCH) {
keir@696 163 l = (region.start >> 16) & 0xfff0;
keir@696 164 l |= region.end & 0xfff00000;
keir@696 165
keir@696 166 if (dw & PCI_PREF_RANGE_TYPE_64) {
keir@696 167 base_up32 = (region.start >> 32) & 0xffffffff;
keir@696 168 limit_up32 = (region.end >> 32) & 0xffffffff;
keir@696 169 } else {
keir@696 170 base_up32 = 0;
keir@696 171 limit_up32 = 0;
keir@696 172 }
keir@696 173 } else {
keir@696 174 l = 0x0000fff0;
keir@696 175 base_up32 = 0xffffffff;
keir@696 176 limit_up32 = 0;
keir@696 177 }
keir@696 178 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, l);
keir@696 179 /* Set up the upper 32 bits of PREF base/limit. */
keir@696 180 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, base_up32);
keir@696 181 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, limit_up32);
keir@696 182 break;
keir@696 183 default :
keir@696 184 BUG();
keir@696 185 break;
keir@696 186 }
keir@696 187 }
keir@701 188 #endif
keir@696 189
ian@0 190 int pci_assign_resource(struct pci_dev *dev, int resno)
ian@0 191 {
ian@0 192 struct pci_bus *bus = dev->bus;
ian@0 193 struct resource *res = dev->resource + resno;
ian@0 194 resource_size_t size, min, align;
ian@0 195 int ret;
keir@696 196 int reassigndev = reassign_resources ? is_reassigndev(dev) : 0;
ian@0 197
ian@0 198 size = res->end - res->start + 1;
ian@0 199 min = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
ian@0 200 /* The bridge resources are special, as their
ian@0 201 size != alignment. Sizing routines return
ian@0 202 required alignment in the "start" field. */
keir@696 203 if (resno < PCI_BRIDGE_RESOURCES) {
keir@696 204 align = size;
keir@696 205 if ((reassigndev) &&
keir@696 206 (res->flags & IORESOURCE_MEM)) {
keir@701 207 align = ALIGN(align, PAGE_SIZE);
keir@696 208 }
keir@696 209 } else {
keir@696 210 align = res->start;
keir@696 211 }
ian@0 212
ian@0 213 /* First, try exact prefetching match.. */
ian@0 214 ret = pci_bus_alloc_resource(bus, res, size, align, min,
ian@0 215 IORESOURCE_PREFETCH,
ian@0 216 pcibios_align_resource, dev);
ian@0 217
ian@0 218 if (ret < 0 && (res->flags & IORESOURCE_PREFETCH)) {
ian@0 219 /*
ian@0 220 * That failed.
ian@0 221 *
ian@0 222 * But a prefetching area can handle a non-prefetching
ian@0 223 * window (it will just not perform as well).
ian@0 224 */
ian@0 225 ret = pci_bus_alloc_resource(bus, res, size, align, min, 0,
ian@0 226 pcibios_align_resource, dev);
ian@0 227 }
ian@0 228
ian@0 229 if (ret) {
ian@0 230 printk(KERN_ERR "PCI: Failed to allocate %s resource "
ian@0 231 "#%d:%llx@%llx for %s\n",
ian@0 232 res->flags & IORESOURCE_IO ? "I/O" : "mem",
ian@0 233 resno, (unsigned long long)size,
ian@0 234 (unsigned long long)res->start, pci_name(dev));
ian@0 235 } else if (resno < PCI_BRIDGE_RESOURCES) {
keir@701 236 if (reassign_resources && is_reassigndev(dev))
keir@701 237 printk(KERN_DEBUG "PCI: Assign resource(%d) on %s "
keir@701 238 "%016llx - %016llx\n", resno, pci_name(dev),
keir@701 239 (unsigned long long)res->start,
keir@701 240 (unsigned long long)res->end);
ian@0 241 pci_update_resource(dev, res, resno);
ian@0 242 }
ian@0 243
ian@0 244 return ret;
ian@0 245 }
ian@0 246
ian@0 247 #ifdef CONFIG_EMBEDDED
ian@0 248 int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
ian@0 249 {
ian@0 250 struct pci_bus *bus = dev->bus;
ian@0 251 struct resource *res = dev->resource + resno;
ian@0 252 unsigned int type_mask;
ian@0 253 int i, ret = -EBUSY;
ian@0 254
ian@0 255 type_mask = IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
ian@0 256
ian@0 257 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
ian@0 258 struct resource *r = bus->resource[i];
ian@0 259 if (!r)
ian@0 260 continue;
ian@0 261
ian@0 262 /* type_mask must match */
ian@0 263 if ((res->flags ^ r->flags) & type_mask)
ian@0 264 continue;
ian@0 265
ian@0 266 ret = request_resource(r, res);
ian@0 267
ian@0 268 if (ret == 0)
ian@0 269 break;
ian@0 270 }
ian@0 271
ian@0 272 if (ret) {
ian@0 273 printk(KERN_ERR "PCI: Failed to allocate %s resource "
ian@0 274 "#%d:%llx@%llx for %s\n",
ian@0 275 res->flags & IORESOURCE_IO ? "I/O" : "mem",
ian@0 276 resno, (unsigned long long)(res->end - res->start + 1),
ian@0 277 (unsigned long long)res->start, pci_name(dev));
ian@0 278 } else if (resno < PCI_BRIDGE_RESOURCES) {
ian@0 279 pci_update_resource(dev, res, resno);
ian@0 280 }
ian@0 281
ian@0 282 return ret;
ian@0 283 }
ian@0 284 EXPORT_SYMBOL_GPL(pci_assign_resource_fixed);
ian@0 285 #endif
ian@0 286
ian@0 287 /* Sort resources by alignment */
ian@0 288 void __devinit
ian@0 289 pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
ian@0 290 {
ian@0 291 int i;
ian@0 292
ian@0 293 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
ian@0 294 struct resource *r;
ian@0 295 struct resource_list *list, *tmp;
ian@0 296 resource_size_t r_align;
ian@0 297
ian@0 298 r = &dev->resource[i];
ian@0 299 r_align = r->end - r->start;
ian@0 300
ian@0 301 if (!(r->flags) || r->parent)
ian@0 302 continue;
ian@0 303 if (!r_align) {
ian@0 304 printk(KERN_WARNING "PCI: Ignore bogus resource %d "
ian@0 305 "[%llx:%llx] of %s\n",
ian@0 306 i, (unsigned long long)r->start,
ian@0 307 (unsigned long long)r->end, pci_name(dev));
ian@0 308 continue;
ian@0 309 }
ian@0 310 r_align = (i < PCI_BRIDGE_RESOURCES) ? r_align + 1 : r->start;
ian@0 311 for (list = head; ; list = list->next) {
ian@0 312 resource_size_t align = 0;
ian@0 313 struct resource_list *ln = list->next;
ian@0 314 int idx;
ian@0 315
ian@0 316 if (ln) {
ian@0 317 idx = ln->res - &ln->dev->resource[0];
ian@0 318 align = (idx < PCI_BRIDGE_RESOURCES) ?
ian@0 319 ln->res->end - ln->res->start + 1 :
ian@0 320 ln->res->start;
ian@0 321 }
ian@0 322 if (r_align > align) {
ian@0 323 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
ian@0 324 if (!tmp)
ian@0 325 panic("pdev_sort_resources(): "
ian@0 326 "kmalloc() failed!\n");
ian@0 327 tmp->next = ln;
ian@0 328 tmp->res = r;
ian@0 329 tmp->dev = dev;
ian@0 330 list->next = tmp;
ian@0 331 break;
ian@0 332 }
ian@0 333 }
ian@0 334 }
ian@0 335 }