Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Contains common pci routines for ALL ppc platform |
Kumar Gala | cf1d8a8 | 2007-06-28 22:56:24 -0500 | [diff] [blame] | 3 | * (based on pci_32.c and pci_64.c) |
| 4 | * |
| 5 | * Port for PPC64 David Engebretsen, IBM Corp. |
| 6 | * Contains common pci routines for ppc64 platform, pSeries and iSeries brands. |
| 7 | * |
| 8 | * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM |
| 9 | * Rework, based on alpha PCI code. |
| 10 | * |
| 11 | * Common pmac/prep/chrp pci routines. -- Cort |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version |
| 16 | * 2 of the License, or (at your option) any later version. |
| 17 | */ |
| 18 | |
| 19 | #undef DEBUG |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/bootmem.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/list.h> |
| 28 | #include <linux/syscalls.h> |
| 29 | #include <linux/irq.h> |
| 30 | #include <linux/vmalloc.h> |
| 31 | |
| 32 | #include <asm/processor.h> |
| 33 | #include <asm/io.h> |
| 34 | #include <asm/prom.h> |
| 35 | #include <asm/pci-bridge.h> |
| 36 | #include <asm/byteorder.h> |
| 37 | #include <asm/machdep.h> |
| 38 | #include <asm/ppc-pci.h> |
| 39 | #include <asm/firmware.h> |
| 40 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 41 | static DEFINE_SPINLOCK(hose_spinlock); |
| 42 | |
| 43 | /* XXX kill that some day ... */ |
Stephen Rothwell | ebfc00f | 2007-11-19 16:56:15 +1100 | [diff] [blame] | 44 | static int global_phb_number; /* Global phb counter */ |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 45 | |
Benjamin Herrenschmidt | 25e81f9 | 2007-12-11 14:48:17 +1100 | [diff] [blame] | 46 | /* ISA Memory physical address */ |
| 47 | resource_size_t isa_mem_base; |
| 48 | |
Benjamin Herrenschmidt | 1fd0f52 | 2008-10-02 14:12:51 +0000 | [diff] [blame] | 49 | /* Default PCI flags is 0 on ppc32, modified at boot on ppc64 */ |
| 50 | unsigned int ppc_pci_flags = 0; |
| 51 | |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 52 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 53 | static struct dma_mapping_ops *pci_dma_ops; |
| 54 | |
| 55 | void set_pci_dma_ops(struct dma_mapping_ops *dma_ops) |
| 56 | { |
| 57 | pci_dma_ops = dma_ops; |
| 58 | } |
| 59 | |
| 60 | struct dma_mapping_ops *get_pci_dma_ops(void) |
| 61 | { |
| 62 | return pci_dma_ops; |
| 63 | } |
| 64 | EXPORT_SYMBOL(get_pci_dma_ops); |
| 65 | |
| 66 | int pci_set_dma_mask(struct pci_dev *dev, u64 mask) |
| 67 | { |
| 68 | return dma_set_mask(&dev->dev, mask); |
| 69 | } |
| 70 | |
| 71 | int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask) |
| 72 | { |
| 73 | int rc; |
| 74 | |
| 75 | rc = dma_set_mask(&dev->dev, mask); |
| 76 | dev->dev.coherent_dma_mask = dev->dma_mask; |
| 77 | |
| 78 | return rc; |
| 79 | } |
| 80 | |
Stephen Rothwell | e60516e | 2007-12-11 11:02:07 +1100 | [diff] [blame] | 81 | struct pci_controller *pcibios_alloc_controller(struct device_node *dev) |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 82 | { |
| 83 | struct pci_controller *phb; |
| 84 | |
Stephen Rothwell | e60516e | 2007-12-11 11:02:07 +1100 | [diff] [blame] | 85 | phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL); |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 86 | if (phb == NULL) |
| 87 | return NULL; |
Stephen Rothwell | e60516e | 2007-12-11 11:02:07 +1100 | [diff] [blame] | 88 | spin_lock(&hose_spinlock); |
| 89 | phb->global_number = global_phb_number++; |
| 90 | list_add_tail(&phb->list_node, &hose_list); |
| 91 | spin_unlock(&hose_spinlock); |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 92 | phb->dn = dev; |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 93 | phb->is_dynamic = mem_init_done; |
| 94 | #ifdef CONFIG_PPC64 |
| 95 | if (dev) { |
| 96 | int nid = of_node_to_nid(dev); |
| 97 | |
| 98 | if (nid < 0 || !node_online(nid)) |
| 99 | nid = -1; |
| 100 | |
| 101 | PHB_SET_NODE(phb, nid); |
| 102 | } |
| 103 | #endif |
| 104 | return phb; |
| 105 | } |
| 106 | |
| 107 | void pcibios_free_controller(struct pci_controller *phb) |
| 108 | { |
| 109 | spin_lock(&hose_spinlock); |
| 110 | list_del(&phb->list_node); |
| 111 | spin_unlock(&hose_spinlock); |
| 112 | |
| 113 | if (phb->is_dynamic) |
| 114 | kfree(phb); |
| 115 | } |
| 116 | |
Benjamin Herrenschmidt | 6dfbde2 | 2007-07-26 14:07:13 +1000 | [diff] [blame] | 117 | int pcibios_vaddr_is_ioport(void __iomem *address) |
| 118 | { |
| 119 | int ret = 0; |
| 120 | struct pci_controller *hose; |
| 121 | unsigned long size; |
| 122 | |
| 123 | spin_lock(&hose_spinlock); |
| 124 | list_for_each_entry(hose, &hose_list, list_node) { |
| 125 | #ifdef CONFIG_PPC64 |
| 126 | size = hose->pci_io_size; |
| 127 | #else |
| 128 | size = hose->io_resource.end - hose->io_resource.start + 1; |
| 129 | #endif |
| 130 | if (address >= hose->io_base_virt && |
| 131 | address < (hose->io_base_virt + size)) { |
| 132 | ret = 1; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | spin_unlock(&hose_spinlock); |
| 137 | return ret; |
| 138 | } |
| 139 | |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 140 | /* |
| 141 | * Return the domain number for this bus. |
| 142 | */ |
| 143 | int pci_domain_nr(struct pci_bus *bus) |
| 144 | { |
Stephen Rothwell | 6207e81 | 2007-12-07 02:04:33 +1100 | [diff] [blame] | 145 | struct pci_controller *hose = pci_bus_to_host(bus); |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 146 | |
Stephen Rothwell | 6207e81 | 2007-12-07 02:04:33 +1100 | [diff] [blame] | 147 | return hose->global_number; |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 148 | } |
Kumar Gala | 5516b54 | 2007-06-27 01:17:57 -0500 | [diff] [blame] | 149 | EXPORT_SYMBOL(pci_domain_nr); |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 150 | |
| 151 | #ifdef CONFIG_PPC_OF |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 152 | |
| 153 | /* This routine is meant to be used early during boot, when the |
| 154 | * PCI bus numbers have not yet been assigned, and you need to |
| 155 | * issue PCI config cycles to an OF device. |
| 156 | * It could also be used to "fix" RTAS config cycles if you want |
| 157 | * to set pci_assign_all_buses to 1 and still use RTAS for PCI |
| 158 | * config cycles. |
| 159 | */ |
| 160 | struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node) |
| 161 | { |
| 162 | if (!have_of) |
| 163 | return NULL; |
| 164 | while(node) { |
| 165 | struct pci_controller *hose, *tmp; |
| 166 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 167 | if (hose->dn == node) |
Kumar Gala | a4c9e32 | 2007-06-27 13:09:43 -0500 | [diff] [blame] | 168 | return hose; |
| 169 | node = node->parent; |
| 170 | } |
| 171 | return NULL; |
| 172 | } |
| 173 | |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 174 | static ssize_t pci_show_devspec(struct device *dev, |
| 175 | struct device_attribute *attr, char *buf) |
| 176 | { |
| 177 | struct pci_dev *pdev; |
| 178 | struct device_node *np; |
| 179 | |
| 180 | pdev = to_pci_dev (dev); |
| 181 | np = pci_device_to_OF_node(pdev); |
| 182 | if (np == NULL || np->full_name == NULL) |
| 183 | return 0; |
| 184 | return sprintf(buf, "%s", np->full_name); |
| 185 | } |
| 186 | static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL); |
| 187 | #endif /* CONFIG_PPC_OF */ |
| 188 | |
| 189 | /* Add sysfs properties */ |
Tony Breeds | 4f3731d | 2007-07-18 11:03:55 +1000 | [diff] [blame] | 190 | int pcibios_add_platform_entries(struct pci_dev *pdev) |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 191 | { |
| 192 | #ifdef CONFIG_PPC_OF |
Tony Breeds | 4f3731d | 2007-07-18 11:03:55 +1000 | [diff] [blame] | 193 | return device_create_file(&pdev->dev, &dev_attr_devspec); |
| 194 | #else |
| 195 | return 0; |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 196 | #endif /* CONFIG_PPC_OF */ |
Tony Breeds | 4f3731d | 2007-07-18 11:03:55 +1000 | [diff] [blame] | 197 | |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Stephen Rothwell | a2b7390 | 2007-07-22 00:37:38 +1000 | [diff] [blame] | 200 | char __devinit *pcibios_setup(char *str) |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 201 | { |
| 202 | return str; |
| 203 | } |
| 204 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 205 | void __devinit pcibios_setup_new_device(struct pci_dev *dev) |
| 206 | { |
| 207 | struct dev_archdata *sd = &dev->dev.archdata; |
| 208 | |
| 209 | sd->of_node = pci_device_to_OF_node(dev); |
| 210 | |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 211 | pr_debug("PCI: device %s OF node: %s\n", pci_name(dev), |
| 212 | sd->of_node ? sd->of_node->full_name : "<none>"); |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 213 | |
| 214 | sd->dma_ops = pci_dma_ops; |
| 215 | #ifdef CONFIG_PPC32 |
| 216 | sd->dma_data = (void *)PCI_DRAM_OFFSET; |
| 217 | #endif |
| 218 | set_dev_node(&dev->dev, pcibus_to_node(dev->bus)); |
| 219 | |
| 220 | if (ppc_md.pci_dma_dev_setup) |
| 221 | ppc_md.pci_dma_dev_setup(dev); |
| 222 | } |
| 223 | EXPORT_SYMBOL(pcibios_setup_new_device); |
| 224 | |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 225 | /* |
| 226 | * Reads the interrupt pin to determine if interrupt is use by card. |
| 227 | * If the interrupt is used, then gets the interrupt line from the |
| 228 | * openfirmware and sets it in the pci_dev and pci_config line. |
| 229 | */ |
| 230 | int pci_read_irq_line(struct pci_dev *pci_dev) |
| 231 | { |
| 232 | struct of_irq oirq; |
| 233 | unsigned int virq; |
| 234 | |
Benjamin Herrenschmidt | 50c9bc2 | 2007-12-20 14:54:55 +1100 | [diff] [blame] | 235 | /* The current device-tree that iSeries generates from the HV |
| 236 | * PCI informations doesn't contain proper interrupt routing, |
| 237 | * and all the fallback would do is print out crap, so we |
| 238 | * don't attempt to resolve the interrupts here at all, some |
| 239 | * iSeries specific fixup does it. |
| 240 | * |
| 241 | * In the long run, we will hopefully fix the generated device-tree |
| 242 | * instead. |
| 243 | */ |
| 244 | #ifdef CONFIG_PPC_ISERIES |
| 245 | if (firmware_has_feature(FW_FEATURE_ISERIES)) |
| 246 | return -1; |
| 247 | #endif |
| 248 | |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 249 | pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev)); |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 250 | |
| 251 | #ifdef DEBUG |
| 252 | memset(&oirq, 0xff, sizeof(oirq)); |
| 253 | #endif |
| 254 | /* Try to get a mapping from the device-tree */ |
| 255 | if (of_irq_map_pci(pci_dev, &oirq)) { |
| 256 | u8 line, pin; |
| 257 | |
| 258 | /* If that fails, lets fallback to what is in the config |
| 259 | * space and map that through the default controller. We |
| 260 | * also set the type to level low since that's what PCI |
| 261 | * interrupts are. If your platform does differently, then |
| 262 | * either provide a proper interrupt tree or don't use this |
| 263 | * function. |
| 264 | */ |
| 265 | if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin)) |
| 266 | return -1; |
| 267 | if (pin == 0) |
| 268 | return -1; |
| 269 | if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) || |
Benjamin Herrenschmidt | 54a24cb | 2007-12-20 15:10:02 +1100 | [diff] [blame] | 270 | line == 0xff || line == 0) { |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 271 | return -1; |
| 272 | } |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 273 | pr_debug(" No map ! Using line %d (pin %d) from PCI config\n", |
| 274 | line, pin); |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 275 | |
| 276 | virq = irq_create_mapping(NULL, line); |
| 277 | if (virq != NO_IRQ) |
| 278 | set_irq_type(virq, IRQ_TYPE_LEVEL_LOW); |
| 279 | } else { |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 280 | pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n", |
| 281 | oirq.size, oirq.specifier[0], oirq.specifier[1], |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 282 | oirq.controller->full_name); |
| 283 | |
| 284 | virq = irq_create_of_mapping(oirq.controller, oirq.specifier, |
| 285 | oirq.size); |
| 286 | } |
| 287 | if(virq == NO_IRQ) { |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 288 | pr_debug(" Failed to map !\n"); |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 289 | return -1; |
| 290 | } |
| 291 | |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 292 | pr_debug(" Mapped to linux irq %d\n", virq); |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 293 | |
| 294 | pci_dev->irq = virq; |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | EXPORT_SYMBOL(pci_read_irq_line); |
| 299 | |
| 300 | /* |
| 301 | * Platform support for /proc/bus/pci/X/Y mmap()s, |
| 302 | * modelled on the sparc64 implementation by Dave Miller. |
| 303 | * -- paulus. |
| 304 | */ |
| 305 | |
| 306 | /* |
| 307 | * Adjust vm_pgoff of VMA such that it is the physical page offset |
| 308 | * corresponding to the 32-bit pci bus offset for DEV requested by the user. |
| 309 | * |
| 310 | * Basically, the user finds the base address for his device which he wishes |
| 311 | * to mmap. They read the 32-bit value from the config space base register, |
| 312 | * add whatever PAGE_SIZE multiple offset they wish, and feed this into the |
| 313 | * offset parameter of mmap on /proc/bus/pci/XXX for that device. |
| 314 | * |
| 315 | * Returns negative error code on failure, zero on success. |
| 316 | */ |
| 317 | static struct resource *__pci_mmap_make_offset(struct pci_dev *dev, |
| 318 | resource_size_t *offset, |
| 319 | enum pci_mmap_state mmap_state) |
| 320 | { |
| 321 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 322 | unsigned long io_offset = 0; |
| 323 | int i, res_bit; |
| 324 | |
| 325 | if (hose == 0) |
| 326 | return NULL; /* should never happen */ |
| 327 | |
| 328 | /* If memory, add on the PCI bridge address offset */ |
| 329 | if (mmap_state == pci_mmap_mem) { |
| 330 | #if 0 /* See comment in pci_resource_to_user() for why this is disabled */ |
| 331 | *offset += hose->pci_mem_offset; |
| 332 | #endif |
| 333 | res_bit = IORESOURCE_MEM; |
| 334 | } else { |
| 335 | io_offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 336 | *offset += io_offset; |
| 337 | res_bit = IORESOURCE_IO; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Check that the offset requested corresponds to one of the |
| 342 | * resources of the device. |
| 343 | */ |
| 344 | for (i = 0; i <= PCI_ROM_RESOURCE; i++) { |
| 345 | struct resource *rp = &dev->resource[i]; |
| 346 | int flags = rp->flags; |
| 347 | |
| 348 | /* treat ROM as memory (should be already) */ |
| 349 | if (i == PCI_ROM_RESOURCE) |
| 350 | flags |= IORESOURCE_MEM; |
| 351 | |
| 352 | /* Active and same type? */ |
| 353 | if ((flags & res_bit) == 0) |
| 354 | continue; |
| 355 | |
| 356 | /* In the range of this resource? */ |
| 357 | if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end) |
| 358 | continue; |
| 359 | |
| 360 | /* found it! construct the final physical address */ |
| 361 | if (mmap_state == pci_mmap_io) |
| 362 | *offset += hose->io_base_phys - io_offset; |
| 363 | return rp; |
| 364 | } |
| 365 | |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci |
| 371 | * device mapping. |
| 372 | */ |
| 373 | static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp, |
| 374 | pgprot_t protection, |
| 375 | enum pci_mmap_state mmap_state, |
| 376 | int write_combine) |
| 377 | { |
| 378 | unsigned long prot = pgprot_val(protection); |
| 379 | |
| 380 | /* Write combine is always 0 on non-memory space mappings. On |
| 381 | * memory space, if the user didn't pass 1, we check for a |
| 382 | * "prefetchable" resource. This is a bit hackish, but we use |
| 383 | * this to workaround the inability of /sysfs to provide a write |
| 384 | * combine bit |
| 385 | */ |
| 386 | if (mmap_state != pci_mmap_mem) |
| 387 | write_combine = 0; |
| 388 | else if (write_combine == 0) { |
| 389 | if (rp->flags & IORESOURCE_PREFETCH) |
| 390 | write_combine = 1; |
| 391 | } |
| 392 | |
| 393 | /* XXX would be nice to have a way to ask for write-through */ |
| 394 | prot |= _PAGE_NO_CACHE; |
| 395 | if (write_combine) |
| 396 | prot &= ~_PAGE_GUARDED; |
| 397 | else |
| 398 | prot |= _PAGE_GUARDED; |
| 399 | |
| 400 | return __pgprot(prot); |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * This one is used by /dev/mem and fbdev who have no clue about the |
| 405 | * PCI device, it tries to find the PCI device first and calls the |
| 406 | * above routine |
| 407 | */ |
| 408 | pgprot_t pci_phys_mem_access_prot(struct file *file, |
| 409 | unsigned long pfn, |
| 410 | unsigned long size, |
| 411 | pgprot_t protection) |
| 412 | { |
| 413 | struct pci_dev *pdev = NULL; |
| 414 | struct resource *found = NULL; |
| 415 | unsigned long prot = pgprot_val(protection); |
Benjamin Herrenschmidt | 7c12d90 | 2008-10-01 15:30:04 +0000 | [diff] [blame] | 416 | resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT; |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 417 | int i; |
| 418 | |
| 419 | if (page_is_ram(pfn)) |
| 420 | return __pgprot(prot); |
| 421 | |
| 422 | prot |= _PAGE_NO_CACHE | _PAGE_GUARDED; |
| 423 | |
| 424 | for_each_pci_dev(pdev) { |
| 425 | for (i = 0; i <= PCI_ROM_RESOURCE; i++) { |
| 426 | struct resource *rp = &pdev->resource[i]; |
| 427 | int flags = rp->flags; |
| 428 | |
| 429 | /* Active and same type? */ |
| 430 | if ((flags & IORESOURCE_MEM) == 0) |
| 431 | continue; |
| 432 | /* In the range of this resource? */ |
| 433 | if (offset < (rp->start & PAGE_MASK) || |
| 434 | offset > rp->end) |
| 435 | continue; |
| 436 | found = rp; |
| 437 | break; |
| 438 | } |
| 439 | if (found) |
| 440 | break; |
| 441 | } |
| 442 | if (found) { |
| 443 | if (found->flags & IORESOURCE_PREFETCH) |
| 444 | prot &= ~_PAGE_GUARDED; |
| 445 | pci_dev_put(pdev); |
| 446 | } |
| 447 | |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 448 | pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n", |
| 449 | (unsigned long long)offset, prot); |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 450 | |
| 451 | return __pgprot(prot); |
| 452 | } |
| 453 | |
| 454 | |
| 455 | /* |
| 456 | * Perform the actual remap of the pages for a PCI device mapping, as |
| 457 | * appropriate for this architecture. The region in the process to map |
| 458 | * is described by vm_start and vm_end members of VMA, the base physical |
| 459 | * address is found in vm_pgoff. |
| 460 | * The pci device structure is provided so that architectures may make mapping |
| 461 | * decisions on a per-device or per-bus basis. |
| 462 | * |
| 463 | * Returns a negative error code on failure, zero on success. |
| 464 | */ |
| 465 | int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, |
| 466 | enum pci_mmap_state mmap_state, int write_combine) |
| 467 | { |
Benjamin Herrenschmidt | 7c12d90 | 2008-10-01 15:30:04 +0000 | [diff] [blame] | 468 | resource_size_t offset = |
| 469 | ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT; |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 470 | struct resource *rp; |
| 471 | int ret; |
| 472 | |
| 473 | rp = __pci_mmap_make_offset(dev, &offset, mmap_state); |
| 474 | if (rp == NULL) |
| 475 | return -EINVAL; |
| 476 | |
| 477 | vma->vm_pgoff = offset >> PAGE_SHIFT; |
| 478 | vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp, |
| 479 | vma->vm_page_prot, |
| 480 | mmap_state, write_combine); |
| 481 | |
| 482 | ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
| 483 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
Benjamin Herrenschmidt | e9f82cb | 2008-10-14 11:55:31 +1100 | [diff] [blame] | 488 | /* This provides legacy IO read access on a bus */ |
| 489 | int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size) |
| 490 | { |
| 491 | unsigned long offset; |
| 492 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 493 | struct resource *rp = &hose->io_resource; |
| 494 | void __iomem *addr; |
| 495 | |
| 496 | /* Check if port can be supported by that bus. We only check |
| 497 | * the ranges of the PHB though, not the bus itself as the rules |
| 498 | * for forwarding legacy cycles down bridges are not our problem |
| 499 | * here. So if the host bridge supports it, we do it. |
| 500 | */ |
| 501 | offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 502 | offset += port; |
| 503 | |
| 504 | if (!(rp->flags & IORESOURCE_IO)) |
| 505 | return -ENXIO; |
| 506 | if (offset < rp->start || (offset + size) > rp->end) |
| 507 | return -ENXIO; |
| 508 | addr = hose->io_base_virt + port; |
| 509 | |
| 510 | switch(size) { |
| 511 | case 1: |
| 512 | *((u8 *)val) = in_8(addr); |
| 513 | return 1; |
| 514 | case 2: |
| 515 | if (port & 1) |
| 516 | return -EINVAL; |
| 517 | *((u16 *)val) = in_le16(addr); |
| 518 | return 2; |
| 519 | case 4: |
| 520 | if (port & 3) |
| 521 | return -EINVAL; |
| 522 | *((u32 *)val) = in_le32(addr); |
| 523 | return 4; |
| 524 | } |
| 525 | return -EINVAL; |
| 526 | } |
| 527 | |
| 528 | /* This provides legacy IO write access on a bus */ |
| 529 | int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size) |
| 530 | { |
| 531 | unsigned long offset; |
| 532 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 533 | struct resource *rp = &hose->io_resource; |
| 534 | void __iomem *addr; |
| 535 | |
| 536 | /* Check if port can be supported by that bus. We only check |
| 537 | * the ranges of the PHB though, not the bus itself as the rules |
| 538 | * for forwarding legacy cycles down bridges are not our problem |
| 539 | * here. So if the host bridge supports it, we do it. |
| 540 | */ |
| 541 | offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 542 | offset += port; |
| 543 | |
| 544 | if (!(rp->flags & IORESOURCE_IO)) |
| 545 | return -ENXIO; |
| 546 | if (offset < rp->start || (offset + size) > rp->end) |
| 547 | return -ENXIO; |
| 548 | addr = hose->io_base_virt + port; |
| 549 | |
| 550 | /* WARNING: The generic code is idiotic. It gets passed a pointer |
| 551 | * to what can be a 1, 2 or 4 byte quantity and always reads that |
| 552 | * as a u32, which means that we have to correct the location of |
| 553 | * the data read within those 32 bits for size 1 and 2 |
| 554 | */ |
| 555 | switch(size) { |
| 556 | case 1: |
| 557 | out_8(addr, val >> 24); |
| 558 | return 1; |
| 559 | case 2: |
| 560 | if (port & 1) |
| 561 | return -EINVAL; |
| 562 | out_le16(addr, val >> 16); |
| 563 | return 2; |
| 564 | case 4: |
| 565 | if (port & 3) |
| 566 | return -EINVAL; |
| 567 | out_le32(addr, val); |
| 568 | return 4; |
| 569 | } |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | |
| 573 | /* This provides legacy IO or memory mmap access on a bus */ |
| 574 | int pci_mmap_legacy_page_range(struct pci_bus *bus, |
| 575 | struct vm_area_struct *vma, |
| 576 | enum pci_mmap_state mmap_state) |
| 577 | { |
| 578 | struct pci_controller *hose = pci_bus_to_host(bus); |
| 579 | resource_size_t offset = |
| 580 | ((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT; |
| 581 | resource_size_t size = vma->vm_end - vma->vm_start; |
| 582 | struct resource *rp; |
| 583 | |
| 584 | pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n", |
| 585 | pci_domain_nr(bus), bus->number, |
| 586 | mmap_state == pci_mmap_mem ? "MEM" : "IO", |
| 587 | (unsigned long long)offset, |
| 588 | (unsigned long long)(offset + size - 1)); |
| 589 | |
| 590 | if (mmap_state == pci_mmap_mem) { |
| 591 | if ((offset + size) > hose->isa_mem_size) |
| 592 | return -ENXIO; |
| 593 | offset += hose->isa_mem_phys; |
| 594 | } else { |
| 595 | unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 596 | unsigned long roffset = offset + io_offset; |
| 597 | rp = &hose->io_resource; |
| 598 | if (!(rp->flags & IORESOURCE_IO)) |
| 599 | return -ENXIO; |
| 600 | if (roffset < rp->start || (roffset + size) > rp->end) |
| 601 | return -ENXIO; |
| 602 | offset += hose->io_base_phys; |
| 603 | } |
| 604 | pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset); |
| 605 | |
| 606 | vma->vm_pgoff = offset >> PAGE_SHIFT; |
David Gibson | 201bdc8 | 2008-10-20 17:55:29 +0000 | [diff] [blame] | 607 | vma->vm_page_prot = __pgprot(pgprot_val(vma->vm_page_prot) |
| 608 | | _PAGE_NO_CACHE | _PAGE_GUARDED); |
Benjamin Herrenschmidt | e9f82cb | 2008-10-14 11:55:31 +1100 | [diff] [blame] | 609 | return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
| 610 | vma->vm_end - vma->vm_start, |
| 611 | vma->vm_page_prot); |
| 612 | } |
| 613 | |
Kumar Gala | 58083da | 2007-06-27 11:07:51 -0500 | [diff] [blame] | 614 | void pci_resource_to_user(const struct pci_dev *dev, int bar, |
| 615 | const struct resource *rsrc, |
| 616 | resource_size_t *start, resource_size_t *end) |
| 617 | { |
| 618 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 619 | resource_size_t offset = 0; |
| 620 | |
| 621 | if (hose == NULL) |
| 622 | return; |
| 623 | |
| 624 | if (rsrc->flags & IORESOURCE_IO) |
| 625 | offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 626 | |
| 627 | /* We pass a fully fixed up address to userland for MMIO instead of |
| 628 | * a BAR value because X is lame and expects to be able to use that |
| 629 | * to pass to /dev/mem ! |
| 630 | * |
| 631 | * That means that we'll have potentially 64 bits values where some |
| 632 | * userland apps only expect 32 (like X itself since it thinks only |
| 633 | * Sparc has 64 bits MMIO) but if we don't do that, we break it on |
| 634 | * 32 bits CHRPs :-( |
| 635 | * |
| 636 | * Hopefully, the sysfs insterface is immune to that gunk. Once X |
| 637 | * has been fixed (and the fix spread enough), we can re-enable the |
| 638 | * 2 lines below and pass down a BAR value to userland. In that case |
| 639 | * we'll also have to re-enable the matching code in |
| 640 | * __pci_mmap_make_offset(). |
| 641 | * |
| 642 | * BenH. |
| 643 | */ |
| 644 | #if 0 |
| 645 | else if (rsrc->flags & IORESOURCE_MEM) |
| 646 | offset = hose->pci_mem_offset; |
| 647 | #endif |
| 648 | |
| 649 | *start = rsrc->start - offset; |
| 650 | *end = rsrc->end - offset; |
| 651 | } |
Benjamin Herrenschmidt | 13dccb9 | 2007-12-11 14:48:18 +1100 | [diff] [blame] | 652 | |
| 653 | /** |
| 654 | * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree |
| 655 | * @hose: newly allocated pci_controller to be setup |
| 656 | * @dev: device node of the host bridge |
| 657 | * @primary: set if primary bus (32 bits only, soon to be deprecated) |
| 658 | * |
| 659 | * This function will parse the "ranges" property of a PCI host bridge device |
| 660 | * node and setup the resource mapping of a pci controller based on its |
| 661 | * content. |
| 662 | * |
| 663 | * Life would be boring if it wasn't for a few issues that we have to deal |
| 664 | * with here: |
| 665 | * |
| 666 | * - We can only cope with one IO space range and up to 3 Memory space |
| 667 | * ranges. However, some machines (thanks Apple !) tend to split their |
| 668 | * space into lots of small contiguous ranges. So we have to coalesce. |
| 669 | * |
| 670 | * - We can only cope with all memory ranges having the same offset |
| 671 | * between CPU addresses and PCI addresses. Unfortunately, some bridges |
| 672 | * are setup for a large 1:1 mapping along with a small "window" which |
| 673 | * maps PCI address 0 to some arbitrary high address of the CPU space in |
| 674 | * order to give access to the ISA memory hole. |
| 675 | * The way out of here that I've chosen for now is to always set the |
| 676 | * offset based on the first resource found, then override it if we |
| 677 | * have a different offset and the previous was set by an ISA hole. |
| 678 | * |
| 679 | * - Some busses have IO space not starting at 0, which causes trouble with |
| 680 | * the way we do our IO resource renumbering. The code somewhat deals with |
| 681 | * it for 64 bits but I would expect problems on 32 bits. |
| 682 | * |
| 683 | * - Some 32 bits platforms such as 4xx can have physical space larger than |
| 684 | * 32 bits so we need to use 64 bits values for the parsing |
| 685 | */ |
| 686 | void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose, |
| 687 | struct device_node *dev, |
| 688 | int primary) |
| 689 | { |
| 690 | const u32 *ranges; |
| 691 | int rlen; |
| 692 | int pna = of_n_addr_cells(dev); |
| 693 | int np = pna + 5; |
| 694 | int memno = 0, isa_hole = -1; |
| 695 | u32 pci_space; |
| 696 | unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size; |
| 697 | unsigned long long isa_mb = 0; |
| 698 | struct resource *res; |
| 699 | |
| 700 | printk(KERN_INFO "PCI host bridge %s %s ranges:\n", |
| 701 | dev->full_name, primary ? "(primary)" : ""); |
| 702 | |
| 703 | /* Get ranges property */ |
| 704 | ranges = of_get_property(dev, "ranges", &rlen); |
| 705 | if (ranges == NULL) |
| 706 | return; |
| 707 | |
| 708 | /* Parse it */ |
| 709 | while ((rlen -= np * 4) >= 0) { |
| 710 | /* Read next ranges element */ |
| 711 | pci_space = ranges[0]; |
| 712 | pci_addr = of_read_number(ranges + 1, 2); |
| 713 | cpu_addr = of_translate_address(dev, ranges + 3); |
| 714 | size = of_read_number(ranges + pna + 3, 2); |
| 715 | ranges += np; |
Benjamin Herrenschmidt | e9f82cb | 2008-10-14 11:55:31 +1100 | [diff] [blame] | 716 | |
| 717 | /* If we failed translation or got a zero-sized region |
| 718 | * (some FW try to feed us with non sensical zero sized regions |
| 719 | * such as power3 which look like some kind of attempt at exposing |
| 720 | * the VGA memory hole) |
| 721 | */ |
Benjamin Herrenschmidt | 13dccb9 | 2007-12-11 14:48:18 +1100 | [diff] [blame] | 722 | if (cpu_addr == OF_BAD_ADDR || size == 0) |
| 723 | continue; |
| 724 | |
| 725 | /* Now consume following elements while they are contiguous */ |
| 726 | for (; rlen >= np * sizeof(u32); |
| 727 | ranges += np, rlen -= np * 4) { |
| 728 | if (ranges[0] != pci_space) |
| 729 | break; |
| 730 | pci_next = of_read_number(ranges + 1, 2); |
| 731 | cpu_next = of_translate_address(dev, ranges + 3); |
| 732 | if (pci_next != pci_addr + size || |
| 733 | cpu_next != cpu_addr + size) |
| 734 | break; |
| 735 | size += of_read_number(ranges + pna + 3, 2); |
| 736 | } |
| 737 | |
| 738 | /* Act based on address space type */ |
| 739 | res = NULL; |
| 740 | switch ((pci_space >> 24) & 0x3) { |
| 741 | case 1: /* PCI IO space */ |
| 742 | printk(KERN_INFO |
| 743 | " IO 0x%016llx..0x%016llx -> 0x%016llx\n", |
| 744 | cpu_addr, cpu_addr + size - 1, pci_addr); |
| 745 | |
| 746 | /* We support only one IO range */ |
| 747 | if (hose->pci_io_size) { |
| 748 | printk(KERN_INFO |
| 749 | " \\--> Skipped (too many) !\n"); |
| 750 | continue; |
| 751 | } |
| 752 | #ifdef CONFIG_PPC32 |
| 753 | /* On 32 bits, limit I/O space to 16MB */ |
| 754 | if (size > 0x01000000) |
| 755 | size = 0x01000000; |
| 756 | |
| 757 | /* 32 bits needs to map IOs here */ |
| 758 | hose->io_base_virt = ioremap(cpu_addr, size); |
| 759 | |
| 760 | /* Expect trouble if pci_addr is not 0 */ |
| 761 | if (primary) |
| 762 | isa_io_base = |
| 763 | (unsigned long)hose->io_base_virt; |
| 764 | #endif /* CONFIG_PPC32 */ |
| 765 | /* pci_io_size and io_base_phys always represent IO |
| 766 | * space starting at 0 so we factor in pci_addr |
| 767 | */ |
| 768 | hose->pci_io_size = pci_addr + size; |
| 769 | hose->io_base_phys = cpu_addr - pci_addr; |
| 770 | |
| 771 | /* Build resource */ |
| 772 | res = &hose->io_resource; |
| 773 | res->flags = IORESOURCE_IO; |
| 774 | res->start = pci_addr; |
| 775 | break; |
| 776 | case 2: /* PCI Memory space */ |
Benjamin Herrenschmidt | 67260ac | 2008-07-17 15:53:31 +1000 | [diff] [blame] | 777 | case 3: /* PCI 64 bits Memory space */ |
Benjamin Herrenschmidt | 13dccb9 | 2007-12-11 14:48:18 +1100 | [diff] [blame] | 778 | printk(KERN_INFO |
| 779 | " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n", |
| 780 | cpu_addr, cpu_addr + size - 1, pci_addr, |
| 781 | (pci_space & 0x40000000) ? "Prefetch" : ""); |
| 782 | |
| 783 | /* We support only 3 memory ranges */ |
| 784 | if (memno >= 3) { |
| 785 | printk(KERN_INFO |
| 786 | " \\--> Skipped (too many) !\n"); |
| 787 | continue; |
| 788 | } |
| 789 | /* Handles ISA memory hole space here */ |
| 790 | if (pci_addr == 0) { |
| 791 | isa_mb = cpu_addr; |
| 792 | isa_hole = memno; |
| 793 | if (primary || isa_mem_base == 0) |
| 794 | isa_mem_base = cpu_addr; |
Benjamin Herrenschmidt | e9f82cb | 2008-10-14 11:55:31 +1100 | [diff] [blame] | 795 | hose->isa_mem_phys = cpu_addr; |
| 796 | hose->isa_mem_size = size; |
Benjamin Herrenschmidt | 13dccb9 | 2007-12-11 14:48:18 +1100 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* We get the PCI/Mem offset from the first range or |
| 800 | * the, current one if the offset came from an ISA |
| 801 | * hole. If they don't match, bugger. |
| 802 | */ |
| 803 | if (memno == 0 || |
| 804 | (isa_hole >= 0 && pci_addr != 0 && |
| 805 | hose->pci_mem_offset == isa_mb)) |
| 806 | hose->pci_mem_offset = cpu_addr - pci_addr; |
| 807 | else if (pci_addr != 0 && |
| 808 | hose->pci_mem_offset != cpu_addr - pci_addr) { |
| 809 | printk(KERN_INFO |
| 810 | " \\--> Skipped (offset mismatch) !\n"); |
| 811 | continue; |
| 812 | } |
| 813 | |
| 814 | /* Build resource */ |
| 815 | res = &hose->mem_resources[memno++]; |
| 816 | res->flags = IORESOURCE_MEM; |
| 817 | if (pci_space & 0x40000000) |
| 818 | res->flags |= IORESOURCE_PREFETCH; |
| 819 | res->start = cpu_addr; |
| 820 | break; |
| 821 | } |
| 822 | if (res != NULL) { |
| 823 | res->name = dev->full_name; |
| 824 | res->end = res->start + size - 1; |
| 825 | res->parent = NULL; |
| 826 | res->sibling = NULL; |
| 827 | res->child = NULL; |
| 828 | } |
| 829 | } |
| 830 | |
Benjamin Herrenschmidt | 8db13a0 | 2008-07-31 15:24:13 +1000 | [diff] [blame] | 831 | /* If there's an ISA hole and the pci_mem_offset is -not- matching |
| 832 | * the ISA hole offset, then we need to remove the ISA hole from |
| 833 | * the resource list for that brige |
| 834 | */ |
| 835 | if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) { |
| 836 | unsigned int next = isa_hole + 1; |
| 837 | printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb); |
| 838 | if (next < memno) |
| 839 | memmove(&hose->mem_resources[isa_hole], |
| 840 | &hose->mem_resources[next], |
| 841 | sizeof(struct resource) * (memno - next)); |
| 842 | hose->mem_resources[--memno].flags = 0; |
Benjamin Herrenschmidt | 13dccb9 | 2007-12-11 14:48:18 +1100 | [diff] [blame] | 843 | } |
| 844 | } |
Benjamin Herrenschmidt | fa462f2 | 2007-12-20 14:54:49 +1100 | [diff] [blame] | 845 | |
| 846 | /* Decide whether to display the domain number in /proc */ |
| 847 | int pci_proc_domain(struct pci_bus *bus) |
| 848 | { |
| 849 | struct pci_controller *hose = pci_bus_to_host(bus); |
Benjamin Herrenschmidt | 1fd0f52 | 2008-10-02 14:12:51 +0000 | [diff] [blame] | 850 | |
Benjamin Herrenschmidt | fa462f2 | 2007-12-20 14:54:49 +1100 | [diff] [blame] | 851 | if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS)) |
| 852 | return 0; |
| 853 | if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0) |
| 854 | return hose->global_number != 0; |
| 855 | return 1; |
Benjamin Herrenschmidt | fa462f2 | 2007-12-20 14:54:49 +1100 | [diff] [blame] | 856 | } |
| 857 | |
Benjamin Herrenschmidt | fe2d338c | 2007-12-20 14:54:50 +1100 | [diff] [blame] | 858 | void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, |
| 859 | struct resource *res) |
| 860 | { |
| 861 | resource_size_t offset = 0, mask = (resource_size_t)-1; |
| 862 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 863 | |
| 864 | if (!hose) |
| 865 | return; |
| 866 | if (res->flags & IORESOURCE_IO) { |
| 867 | offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 868 | mask = 0xffffffffu; |
| 869 | } else if (res->flags & IORESOURCE_MEM) |
| 870 | offset = hose->pci_mem_offset; |
| 871 | |
| 872 | region->start = (res->start - offset) & mask; |
| 873 | region->end = (res->end - offset) & mask; |
| 874 | } |
| 875 | EXPORT_SYMBOL(pcibios_resource_to_bus); |
| 876 | |
| 877 | void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, |
| 878 | struct pci_bus_region *region) |
| 879 | { |
| 880 | resource_size_t offset = 0, mask = (resource_size_t)-1; |
| 881 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 882 | |
| 883 | if (!hose) |
| 884 | return; |
| 885 | if (res->flags & IORESOURCE_IO) { |
| 886 | offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 887 | mask = 0xffffffffu; |
| 888 | } else if (res->flags & IORESOURCE_MEM) |
| 889 | offset = hose->pci_mem_offset; |
| 890 | res->start = (region->start + offset) & mask; |
| 891 | res->end = (region->end + offset) & mask; |
| 892 | } |
| 893 | EXPORT_SYMBOL(pcibios_bus_to_resource); |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 894 | |
| 895 | /* Fixup a bus resource into a linux resource */ |
| 896 | static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev) |
| 897 | { |
| 898 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 899 | resource_size_t offset = 0, mask = (resource_size_t)-1; |
| 900 | |
| 901 | if (res->flags & IORESOURCE_IO) { |
| 902 | offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 903 | mask = 0xffffffffu; |
| 904 | } else if (res->flags & IORESOURCE_MEM) |
| 905 | offset = hose->pci_mem_offset; |
| 906 | |
| 907 | res->start = (res->start + offset) & mask; |
| 908 | res->end = (res->end + offset) & mask; |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | |
| 912 | /* This header fixup will do the resource fixup for all devices as they are |
| 913 | * probed, but not for bridge ranges |
| 914 | */ |
| 915 | static void __devinit pcibios_fixup_resources(struct pci_dev *dev) |
| 916 | { |
| 917 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
| 918 | int i; |
| 919 | |
| 920 | if (!hose) { |
| 921 | printk(KERN_ERR "No host bridge for PCI dev %s !\n", |
| 922 | pci_name(dev)); |
| 923 | return; |
| 924 | } |
| 925 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 926 | struct resource *res = dev->resource + i; |
| 927 | if (!res->flags) |
| 928 | continue; |
Benjamin Herrenschmidt | 7f17289 | 2008-02-29 14:58:03 +1100 | [diff] [blame] | 929 | /* On platforms that have PPC_PCI_PROBE_ONLY set, we don't |
| 930 | * consider 0 as an unassigned BAR value. It's technically |
| 931 | * a valid value, but linux doesn't like it... so when we can |
| 932 | * re-assign things, we do so, but if we can't, we keep it |
| 933 | * around and hope for the best... |
| 934 | */ |
| 935 | if (res->start == 0 && !(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) { |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 936 | pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n", |
| 937 | pci_name(dev), i, |
| 938 | (unsigned long long)res->start, |
| 939 | (unsigned long long)res->end, |
| 940 | (unsigned int)res->flags); |
| 941 | res->end -= res->start; |
| 942 | res->start = 0; |
| 943 | res->flags |= IORESOURCE_UNSET; |
| 944 | continue; |
| 945 | } |
| 946 | |
| 947 | pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n", |
| 948 | pci_name(dev), i, |
| 949 | (unsigned long long)res->start,\ |
| 950 | (unsigned long long)res->end, |
| 951 | (unsigned int)res->flags); |
| 952 | |
| 953 | fixup_resource(res, dev); |
Benjamin Herrenschmidt | b556151 | 2008-10-13 13:56:31 +0000 | [diff] [blame] | 954 | |
| 955 | pr_debug("PCI:%s %016llx-%016llx\n", |
| 956 | pci_name(dev), |
| 957 | (unsigned long long)res->start, |
| 958 | (unsigned long long)res->end); |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* Call machine specific resource fixup */ |
| 962 | if (ppc_md.pcibios_fixup_resources) |
| 963 | ppc_md.pcibios_fixup_resources(dev); |
| 964 | } |
| 965 | DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources); |
| 966 | |
Benjamin Herrenschmidt | b556151 | 2008-10-13 13:56:31 +0000 | [diff] [blame] | 967 | /* This function tries to figure out if a bridge resource has been initialized |
| 968 | * by the firmware or not. It doesn't have to be absolutely bullet proof, but |
| 969 | * things go more smoothly when it gets it right. It should covers cases such |
| 970 | * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges |
| 971 | */ |
| 972 | static int __devinit pcibios_uninitialized_bridge_resource(struct pci_bus *bus, |
| 973 | struct resource *res) |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 974 | { |
Benjamin Herrenschmidt | be8cbcd | 2007-12-20 14:55:04 +1100 | [diff] [blame] | 975 | struct pci_controller *hose = pci_bus_to_host(bus); |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 976 | struct pci_dev *dev = bus->self; |
Benjamin Herrenschmidt | b556151 | 2008-10-13 13:56:31 +0000 | [diff] [blame] | 977 | resource_size_t offset; |
| 978 | u16 command; |
| 979 | int i; |
| 980 | |
| 981 | /* We don't do anything if PCI_PROBE_ONLY is set */ |
| 982 | if (ppc_pci_flags & PPC_PCI_PROBE_ONLY) |
| 983 | return 0; |
| 984 | |
| 985 | /* Job is a bit different between memory and IO */ |
| 986 | if (res->flags & IORESOURCE_MEM) { |
| 987 | /* If the BAR is non-0 (res != pci_mem_offset) then it's probably been |
| 988 | * initialized by somebody |
| 989 | */ |
| 990 | if (res->start != hose->pci_mem_offset) |
| 991 | return 0; |
| 992 | |
| 993 | /* The BAR is 0, let's check if memory decoding is enabled on |
| 994 | * the bridge. If not, we consider it unassigned |
| 995 | */ |
| 996 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 997 | if ((command & PCI_COMMAND_MEMORY) == 0) |
| 998 | return 1; |
| 999 | |
| 1000 | /* Memory decoding is enabled and the BAR is 0. If any of the bridge |
| 1001 | * resources covers that starting address (0 then it's good enough for |
| 1002 | * us for memory |
| 1003 | */ |
| 1004 | for (i = 0; i < 3; i++) { |
| 1005 | if ((hose->mem_resources[i].flags & IORESOURCE_MEM) && |
| 1006 | hose->mem_resources[i].start == hose->pci_mem_offset) |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | /* Well, it starts at 0 and we know it will collide so we may as |
| 1011 | * well consider it as unassigned. That covers the Apple case. |
| 1012 | */ |
| 1013 | return 1; |
| 1014 | } else { |
| 1015 | /* If the BAR is non-0, then we consider it assigned */ |
| 1016 | offset = (unsigned long)hose->io_base_virt - _IO_BASE; |
| 1017 | if (((res->start - offset) & 0xfffffffful) != 0) |
| 1018 | return 0; |
| 1019 | |
| 1020 | /* Here, we are a bit different than memory as typically IO space |
| 1021 | * starting at low addresses -is- valid. What we do instead if that |
| 1022 | * we consider as unassigned anything that doesn't have IO enabled |
| 1023 | * in the PCI command register, and that's it. |
| 1024 | */ |
| 1025 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 1026 | if (command & PCI_COMMAND_IO) |
| 1027 | return 0; |
| 1028 | |
| 1029 | /* It's starting at 0 and IO is disabled in the bridge, consider |
| 1030 | * it unassigned |
| 1031 | */ |
| 1032 | return 1; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | /* Fixup resources of a PCI<->PCI bridge */ |
| 1037 | static void __devinit pcibios_fixup_bridge(struct pci_bus *bus) |
| 1038 | { |
| 1039 | struct resource *res; |
| 1040 | int i; |
| 1041 | |
| 1042 | struct pci_dev *dev = bus->self; |
| 1043 | |
| 1044 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) { |
| 1045 | if ((res = bus->resource[i]) == NULL) |
| 1046 | continue; |
| 1047 | if (!res->flags) |
| 1048 | continue; |
| 1049 | if (i >= 3 && bus->self->transparent) |
| 1050 | continue; |
| 1051 | |
| 1052 | pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n", |
| 1053 | pci_name(dev), i, |
| 1054 | (unsigned long long)res->start,\ |
| 1055 | (unsigned long long)res->end, |
| 1056 | (unsigned int)res->flags); |
| 1057 | |
| 1058 | /* Perform fixup */ |
| 1059 | fixup_resource(res, dev); |
| 1060 | |
| 1061 | /* Try to detect uninitialized P2P bridge resources, |
| 1062 | * and clear them out so they get re-assigned later |
| 1063 | */ |
| 1064 | if (pcibios_uninitialized_bridge_resource(bus, res)) { |
| 1065 | res->flags = 0; |
| 1066 | pr_debug("PCI:%s (unassigned)\n", pci_name(dev)); |
| 1067 | } else { |
| 1068 | |
| 1069 | pr_debug("PCI:%s %016llx-%016llx\n", |
| 1070 | pci_name(dev), |
| 1071 | (unsigned long long)res->start, |
| 1072 | (unsigned long long)res->end); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | static void __devinit __pcibios_fixup_bus(struct pci_bus *bus) |
| 1078 | { |
| 1079 | struct pci_dev *dev = bus->self; |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 1080 | |
| 1081 | pr_debug("PCI: Fixup bus %d (%s)\n", bus->number, dev ? pci_name(dev) : "PHB"); |
| 1082 | |
| 1083 | /* Fixup PCI<->PCI bridges. Host bridges are handled separately, for |
| 1084 | * now differently between 32 and 64 bits. |
| 1085 | */ |
Benjamin Herrenschmidt | b556151 | 2008-10-13 13:56:31 +0000 | [diff] [blame] | 1086 | if (dev != NULL) |
| 1087 | pcibios_fixup_bridge(bus); |
Benjamin Herrenschmidt | bf5e2ba | 2007-12-20 14:54:51 +1100 | [diff] [blame] | 1088 | |
| 1089 | /* Additional setup that is different between 32 and 64 bits for now */ |
| 1090 | pcibios_do_bus_setup(bus); |
| 1091 | |
| 1092 | /* Platform specific bus fixups */ |
| 1093 | if (ppc_md.pcibios_fixup_bus) |
| 1094 | ppc_md.pcibios_fixup_bus(bus); |
| 1095 | |
| 1096 | /* Read default IRQs and fixup if necessary */ |
| 1097 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1098 | pci_read_irq_line(dev); |
| 1099 | if (ppc_md.pci_irq_fixup) |
| 1100 | ppc_md.pci_irq_fixup(dev); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | void __devinit pcibios_fixup_bus(struct pci_bus *bus) |
| 1105 | { |
| 1106 | /* When called from the generic PCI probe, read PCI<->PCI bridge |
| 1107 | * bases before proceeding |
| 1108 | */ |
| 1109 | if (bus->self != NULL) |
| 1110 | pci_read_bridge_bases(bus); |
| 1111 | __pcibios_fixup_bus(bus); |
| 1112 | } |
| 1113 | EXPORT_SYMBOL(pcibios_fixup_bus); |
| 1114 | |
| 1115 | /* When building a bus from the OF tree rather than probing, we need a |
| 1116 | * slightly different version of the fixup which doesn't read the |
| 1117 | * bridge bases using config space accesses |
| 1118 | */ |
| 1119 | void __devinit pcibios_fixup_of_probed_bus(struct pci_bus *bus) |
| 1120 | { |
| 1121 | __pcibios_fixup_bus(bus); |
| 1122 | } |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1123 | |
| 1124 | static int skip_isa_ioresource_align(struct pci_dev *dev) |
| 1125 | { |
| 1126 | if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) && |
| 1127 | !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA)) |
| 1128 | return 1; |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * We need to avoid collisions with `mirrored' VGA ports |
| 1134 | * and other strange ISA hardware, so we always want the |
| 1135 | * addresses to be allocated in the 0x000-0x0ff region |
| 1136 | * modulo 0x400. |
| 1137 | * |
| 1138 | * Why? Because some silly external IO cards only decode |
| 1139 | * the low 10 bits of the IO address. The 0x00-0xff region |
| 1140 | * is reserved for motherboard devices that decode all 16 |
| 1141 | * bits, so it's ok to allocate at, say, 0x2800-0x28ff, |
| 1142 | * but we want to try to avoid allocating at 0x2900-0x2bff |
| 1143 | * which might have be mirrored at 0x0100-0x03ff.. |
| 1144 | */ |
| 1145 | void pcibios_align_resource(void *data, struct resource *res, |
| 1146 | resource_size_t size, resource_size_t align) |
| 1147 | { |
| 1148 | struct pci_dev *dev = data; |
| 1149 | |
| 1150 | if (res->flags & IORESOURCE_IO) { |
| 1151 | resource_size_t start = res->start; |
| 1152 | |
| 1153 | if (skip_isa_ioresource_align(dev)) |
| 1154 | return; |
| 1155 | if (start & 0x300) { |
| 1156 | start = (start + 0x3ff) & ~0x3ff; |
| 1157 | res->start = start; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | EXPORT_SYMBOL(pcibios_align_resource); |
| 1162 | |
| 1163 | /* |
| 1164 | * Reparent resource children of pr that conflict with res |
| 1165 | * under res, and make res replace those children. |
| 1166 | */ |
| 1167 | static int __init reparent_resources(struct resource *parent, |
| 1168 | struct resource *res) |
| 1169 | { |
| 1170 | struct resource *p, **pp; |
| 1171 | struct resource **firstpp = NULL; |
| 1172 | |
| 1173 | for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) { |
| 1174 | if (p->end < res->start) |
| 1175 | continue; |
| 1176 | if (res->end < p->start) |
| 1177 | break; |
| 1178 | if (p->start < res->start || p->end > res->end) |
| 1179 | return -1; /* not completely contained */ |
| 1180 | if (firstpp == NULL) |
| 1181 | firstpp = pp; |
| 1182 | } |
| 1183 | if (firstpp == NULL) |
| 1184 | return -1; /* didn't find any conflicting entries? */ |
| 1185 | res->parent = parent; |
| 1186 | res->child = *firstpp; |
| 1187 | res->sibling = *pp; |
| 1188 | *firstpp = res; |
| 1189 | *pp = NULL; |
| 1190 | for (p = res->child; p != NULL; p = p->sibling) { |
| 1191 | p->parent = res; |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 1192 | pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n", |
| 1193 | p->name, |
| 1194 | (unsigned long long)p->start, |
| 1195 | (unsigned long long)p->end, res->name); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1196 | } |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * Handle resources of PCI devices. If the world were perfect, we could |
| 1202 | * just allocate all the resource regions and do nothing more. It isn't. |
| 1203 | * On the other hand, we cannot just re-allocate all devices, as it would |
| 1204 | * require us to know lots of host bridge internals. So we attempt to |
| 1205 | * keep as much of the original configuration as possible, but tweak it |
| 1206 | * when it's found to be wrong. |
| 1207 | * |
| 1208 | * Known BIOS problems we have to work around: |
| 1209 | * - I/O or memory regions not configured |
| 1210 | * - regions configured, but not enabled in the command register |
| 1211 | * - bogus I/O addresses above 64K used |
| 1212 | * - expansion ROMs left enabled (this may sound harmless, but given |
| 1213 | * the fact the PCI specs explicitly allow address decoders to be |
| 1214 | * shared between expansion ROMs and other resource regions, it's |
| 1215 | * at least dangerous) |
| 1216 | * |
| 1217 | * Our solution: |
| 1218 | * (1) Allocate resources for all buses behind PCI-to-PCI bridges. |
| 1219 | * This gives us fixed barriers on where we can allocate. |
| 1220 | * (2) Allocate resources for all enabled devices. If there is |
| 1221 | * a collision, just mark the resource as unallocated. Also |
| 1222 | * disable expansion ROMs during this step. |
| 1223 | * (3) Try to allocate resources for disabled devices. If the |
| 1224 | * resources were assigned correctly, everything goes well, |
| 1225 | * if they weren't, they won't disturb allocation of other |
| 1226 | * resources. |
| 1227 | * (4) Assign new addresses to resources which were either |
| 1228 | * not configured at all or misconfigured. If explicitly |
| 1229 | * requested by the user, configure expansion ROM address |
| 1230 | * as well. |
| 1231 | */ |
| 1232 | |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1233 | void pcibios_allocate_bus_resources(struct pci_bus *bus) |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1234 | { |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1235 | struct pci_bus *b; |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1236 | int i; |
| 1237 | struct resource *res, *pr; |
| 1238 | |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1239 | for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) { |
| 1240 | if ((res = bus->resource[i]) == NULL || !res->flags |
| 1241 | || res->start > res->end) |
| 1242 | continue; |
| 1243 | if (bus->parent == NULL) |
| 1244 | pr = (res->flags & IORESOURCE_IO) ? |
| 1245 | &ioport_resource : &iomem_resource; |
| 1246 | else { |
| 1247 | /* Don't bother with non-root busses when |
| 1248 | * re-assigning all resources. We clear the |
| 1249 | * resource flags as if they were colliding |
| 1250 | * and as such ensure proper re-allocation |
| 1251 | * later. |
| 1252 | */ |
| 1253 | if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC) |
| 1254 | goto clear_resource; |
| 1255 | pr = pci_find_parent_resource(bus->self, res); |
| 1256 | if (pr == res) { |
| 1257 | /* this happens when the generic PCI |
| 1258 | * code (wrongly) decides that this |
| 1259 | * bridge is transparent -- paulus |
| 1260 | */ |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1261 | continue; |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1262 | } |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1263 | } |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1264 | |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 1265 | pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx " |
| 1266 | "[0x%x], parent %p (%s)\n", |
| 1267 | bus->self ? pci_name(bus->self) : "PHB", |
| 1268 | bus->number, i, |
| 1269 | (unsigned long long)res->start, |
| 1270 | (unsigned long long)res->end, |
| 1271 | (unsigned int)res->flags, |
| 1272 | pr, (pr && pr->name) ? pr->name : "nil"); |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1273 | |
| 1274 | if (pr && !(pr->flags & IORESOURCE_UNSET)) { |
| 1275 | if (request_resource(pr, res) == 0) |
| 1276 | continue; |
| 1277 | /* |
| 1278 | * Must be a conflict with an existing entry. |
| 1279 | * Move that entry (or entries) under the |
| 1280 | * bridge resource and try again. |
| 1281 | */ |
| 1282 | if (reparent_resources(pr, res) == 0) |
| 1283 | continue; |
| 1284 | } |
| 1285 | printk(KERN_WARNING "PCI: Cannot allocate resource region " |
| 1286 | "%d of PCI bridge %d, will remap\n", i, bus->number); |
| 1287 | clear_resource: |
| 1288 | res->flags = 0; |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1289 | } |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1290 | |
| 1291 | list_for_each_entry(b, &bus->children, node) |
| 1292 | pcibios_allocate_bus_resources(b); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1293 | } |
| 1294 | |
Paul Mackerras | 533b192 | 2007-12-31 10:04:15 +1100 | [diff] [blame] | 1295 | static inline void __devinit alloc_resource(struct pci_dev *dev, int idx) |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1296 | { |
| 1297 | struct resource *pr, *r = &dev->resource[idx]; |
| 1298 | |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 1299 | pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n", |
| 1300 | pci_name(dev), idx, |
| 1301 | (unsigned long long)r->start, |
| 1302 | (unsigned long long)r->end, |
| 1303 | (unsigned int)r->flags); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1304 | |
| 1305 | pr = pci_find_parent_resource(dev, r); |
| 1306 | if (!pr || (pr->flags & IORESOURCE_UNSET) || |
| 1307 | request_resource(pr, r) < 0) { |
| 1308 | printk(KERN_WARNING "PCI: Cannot allocate resource region %d" |
| 1309 | " of device %s, will remap\n", idx, pci_name(dev)); |
| 1310 | if (pr) |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 1311 | pr_debug("PCI: parent is %p: %016llx-%016llx [%x]\n", |
| 1312 | pr, |
| 1313 | (unsigned long long)pr->start, |
| 1314 | (unsigned long long)pr->end, |
| 1315 | (unsigned int)pr->flags); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1316 | /* We'll assign a new address later */ |
| 1317 | r->flags |= IORESOURCE_UNSET; |
| 1318 | r->end -= r->start; |
| 1319 | r->start = 0; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | static void __init pcibios_allocate_resources(int pass) |
| 1324 | { |
| 1325 | struct pci_dev *dev = NULL; |
| 1326 | int idx, disabled; |
| 1327 | u16 command; |
| 1328 | struct resource *r; |
| 1329 | |
| 1330 | for_each_pci_dev(dev) { |
| 1331 | pci_read_config_word(dev, PCI_COMMAND, &command); |
| 1332 | for (idx = 0; idx < 6; idx++) { |
| 1333 | r = &dev->resource[idx]; |
| 1334 | if (r->parent) /* Already allocated */ |
| 1335 | continue; |
| 1336 | if (!r->flags || (r->flags & IORESOURCE_UNSET)) |
| 1337 | continue; /* Not assigned at all */ |
| 1338 | if (r->flags & IORESOURCE_IO) |
| 1339 | disabled = !(command & PCI_COMMAND_IO); |
| 1340 | else |
| 1341 | disabled = !(command & PCI_COMMAND_MEMORY); |
Paul Mackerras | 533b192 | 2007-12-31 10:04:15 +1100 | [diff] [blame] | 1342 | if (pass == disabled) |
| 1343 | alloc_resource(dev, idx); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1344 | } |
| 1345 | if (pass) |
| 1346 | continue; |
| 1347 | r = &dev->resource[PCI_ROM_RESOURCE]; |
| 1348 | if (r->flags & IORESOURCE_ROM_ENABLE) { |
| 1349 | /* Turn the ROM off, leave the resource region, |
| 1350 | * but keep it unregistered. |
| 1351 | */ |
| 1352 | u32 reg; |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 1353 | pr_debug("PCI: Switching off ROM of %s\n", |
| 1354 | pci_name(dev)); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1355 | r->flags &= ~IORESOURCE_ROM_ENABLE; |
| 1356 | pci_read_config_dword(dev, dev->rom_base_reg, ®); |
| 1357 | pci_write_config_dword(dev, dev->rom_base_reg, |
| 1358 | reg & ~PCI_ROM_ADDRESS_ENABLE); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | void __init pcibios_resource_survey(void) |
| 1364 | { |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1365 | struct pci_bus *b; |
| 1366 | |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1367 | /* Allocate and assign resources. If we re-assign everything, then |
| 1368 | * we skip the allocate phase |
| 1369 | */ |
Nathan Fontenot | e90a131 | 2008-10-27 19:48:17 +0000 | [diff] [blame] | 1370 | list_for_each_entry(b, &pci_root_buses, node) |
| 1371 | pcibios_allocate_bus_resources(b); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1372 | |
| 1373 | if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) { |
| 1374 | pcibios_allocate_resources(0); |
| 1375 | pcibios_allocate_resources(1); |
| 1376 | } |
| 1377 | |
| 1378 | if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) { |
Benjamin Herrenschmidt | b0494bc | 2008-10-27 19:48:22 +0000 | [diff] [blame] | 1379 | pr_debug("PCI: Assigning unassigned resouces...\n"); |
Benjamin Herrenschmidt | 3fd94c6 | 2007-12-20 14:54:53 +1100 | [diff] [blame] | 1380 | pci_assign_unassigned_resources(); |
| 1381 | } |
| 1382 | |
| 1383 | /* Call machine dependent fixup */ |
| 1384 | if (ppc_md.pcibios_fixup) |
| 1385 | ppc_md.pcibios_fixup(); |
| 1386 | } |
| 1387 | |
| 1388 | #ifdef CONFIG_HOTPLUG |
| 1389 | /* This is used by the pSeries hotplug driver to allocate resource |
| 1390 | * of newly plugged busses. We can try to consolidate with the |
| 1391 | * rest of the code later, for now, keep it as-is |
| 1392 | */ |
| 1393 | void __devinit pcibios_claim_one_bus(struct pci_bus *bus) |
| 1394 | { |
| 1395 | struct pci_dev *dev; |
| 1396 | struct pci_bus *child_bus; |
| 1397 | |
| 1398 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1399 | int i; |
| 1400 | |
| 1401 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 1402 | struct resource *r = &dev->resource[i]; |
| 1403 | |
| 1404 | if (r->parent || !r->start || !r->flags) |
| 1405 | continue; |
| 1406 | pci_claim_resource(dev, i); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | list_for_each_entry(child_bus, &bus->children, node) |
| 1411 | pcibios_claim_one_bus(child_bus); |
| 1412 | } |
| 1413 | EXPORT_SYMBOL_GPL(pcibios_claim_one_bus); |
| 1414 | #endif /* CONFIG_HOTPLUG */ |
Benjamin Herrenschmidt | 549beb9 | 2007-12-20 14:54:57 +1100 | [diff] [blame] | 1415 | |
| 1416 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
| 1417 | { |
Benjamin Herrenschmidt | 549beb9 | 2007-12-20 14:54:57 +1100 | [diff] [blame] | 1418 | if (ppc_md.pcibios_enable_device_hook) |
| 1419 | if (ppc_md.pcibios_enable_device_hook(dev)) |
| 1420 | return -EINVAL; |
| 1421 | |
Bjorn Helgaas | 7cfb5f9 | 2008-03-04 11:56:56 -0700 | [diff] [blame] | 1422 | return pci_enable_resources(dev, mask); |
Benjamin Herrenschmidt | 549beb9 | 2007-12-20 14:54:57 +1100 | [diff] [blame] | 1423 | } |
Benjamin Herrenschmidt | 5328032 | 2008-10-27 19:48:29 +0000 | [diff] [blame^] | 1424 | |
| 1425 | void __devinit pcibios_setup_phb_resources(struct pci_controller *hose) |
| 1426 | { |
| 1427 | struct pci_bus *bus = hose->bus; |
| 1428 | struct resource *res; |
| 1429 | int i; |
| 1430 | |
| 1431 | /* Hookup PHB IO resource */ |
| 1432 | bus->resource[0] = res = &hose->io_resource; |
| 1433 | |
| 1434 | if (!res->flags) { |
| 1435 | printk(KERN_WARNING "PCI: I/O resource not set for host" |
| 1436 | " bridge %s (domain %d)\n", |
| 1437 | hose->dn->full_name, hose->global_number); |
| 1438 | #ifdef CONFIG_PPC32 |
| 1439 | /* Workaround for lack of IO resource only on 32-bit */ |
| 1440 | res->start = (unsigned long)hose->io_base_virt - isa_io_base; |
| 1441 | res->end = res->start + IO_SPACE_LIMIT; |
| 1442 | res->flags = IORESOURCE_IO; |
| 1443 | #endif /* CONFIG_PPC32 */ |
| 1444 | } |
| 1445 | |
| 1446 | pr_debug("PCI: PHB IO resource = %016llx-%016llx [%lx]\n", |
| 1447 | (unsigned long long)res->start, |
| 1448 | (unsigned long long)res->end, |
| 1449 | (unsigned long)res->flags); |
| 1450 | |
| 1451 | /* Hookup PHB Memory resources */ |
| 1452 | for (i = 0; i < 3; ++i) { |
| 1453 | res = &hose->mem_resources[i]; |
| 1454 | if (!res->flags) { |
| 1455 | if (i > 0) |
| 1456 | continue; |
| 1457 | printk(KERN_ERR "PCI: Memory resource 0 not set for " |
| 1458 | "host bridge %s (domain %d)\n", |
| 1459 | hose->dn->full_name, hose->global_number); |
| 1460 | #ifdef CONFIG_PPC32 |
| 1461 | /* Workaround for lack of MEM resource only on 32-bit */ |
| 1462 | res->start = hose->pci_mem_offset; |
| 1463 | res->end = (resource_size_t)-1LL; |
| 1464 | res->flags = IORESOURCE_MEM; |
| 1465 | #endif /* CONFIG_PPC32 */ |
| 1466 | } |
| 1467 | bus->resource[i+1] = res; |
| 1468 | |
| 1469 | pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n", i, |
| 1470 | (unsigned long long)res->start, |
| 1471 | (unsigned long long)res->end, |
| 1472 | (unsigned long)res->flags); |
| 1473 | } |
| 1474 | |
| 1475 | pr_debug("PCI: PHB MEM offset = %016llx\n", |
| 1476 | (unsigned long long)hose->pci_mem_offset); |
| 1477 | pr_debug("PCI: PHB IO offset = %08lx\n", |
| 1478 | (unsigned long)hose->io_base_virt - _IO_BASE); |
| 1479 | |
| 1480 | } |
| 1481 | |