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