vserver 1.9.3
[linux-2.6.git] / arch / ppc64 / kernel / pci.c
1 /*
2  * Port for PPC64 David Engebretsen, IBM Corp.
3  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
4  * 
5  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6  *   Rework, based on alpha PCI code.
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/capability.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/bootmem.h>
24 #include <linux/module.h>
25 #include <linux/mm.h>
26 #include <linux/list.h>
27
28 #include <asm/processor.h>
29 #include <asm/io.h>
30 #include <asm/prom.h>
31 #include <asm/pci-bridge.h>
32 #include <asm/byteorder.h>
33 #include <asm/irq.h>
34 #include <asm/uaccess.h>
35 #include <asm/ppcdebug.h>
36 #include <asm/naca.h>
37 #include <asm/iommu.h>
38 #include <asm/machdep.h>
39
40 #include "pci.h"
41
42 unsigned long pci_probe_only = 1;
43 unsigned long pci_assign_all_buses = 0;
44
45 /* legal IO pages under MAX_ISA_PORT.  This is to ensure we don't touch
46    devices we don't have access to. */
47 unsigned long io_page_mask;
48
49 EXPORT_SYMBOL(io_page_mask);
50
51
52 unsigned int pcibios_assign_all_busses(void)
53 {
54         return pci_assign_all_buses;
55 }
56
57 /* pci_io_base -- the base address from which io bars are offsets.
58  * This is the lowest I/O base address (so bar values are always positive),
59  * and it *must* be the start of ISA space if an ISA bus exists because
60  * ISA drivers use hard coded offsets.  If no ISA bus exists a dummy
61  * page is mapped and isa_io_limit prevents access to it.
62  */
63 unsigned long isa_io_base;      /* NULL if no ISA bus */
64 unsigned long pci_io_base;
65
66 void iSeries_pcibios_init(void);
67
68 LIST_HEAD(hose_list);
69
70 struct pci_dma_ops pci_dma_ops;
71 EXPORT_SYMBOL(pci_dma_ops);
72
73 int global_phb_number;          /* Global phb counter */
74
75 /* Cached ISA bridge dev. */
76 struct pci_dev *ppc64_isabridge_dev = NULL;
77
78 static void fixup_broken_pcnet32(struct pci_dev* dev)
79 {
80         if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
81                 dev->vendor = PCI_VENDOR_ID_AMD;
82                 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
83                 pci_name_device(dev);
84         }
85 }
86 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
87
88 static void fixup_windbond_82c105(struct pci_dev* dev)
89 {
90         /* Assume the windbond 82c105 is the IDE controller on a
91          * p610.  We should probably be more careful in case
92          * someone tries to plug in a similar adapter.
93          */
94         int i;
95         unsigned int reg;
96
97         printk("Using INTC for W82c105 IDE controller.\n");
98         pci_read_config_dword(dev, 0x40, &reg);
99         /* Enable LEGIRQ to use INTC instead of ISA interrupts */
100         pci_write_config_dword(dev, 0x40, reg | (1<<11));
101
102         for (i = 0; i < DEVICE_COUNT_RESOURCE; ++i) {
103                 /* zap the 2nd function of the winbond chip */
104                 if (dev->resource[i].flags & IORESOURCE_IO
105                     && dev->bus->number == 0 && dev->devfn == 0x81)
106                         dev->resource[i].flags &= ~IORESOURCE_IO;
107         }
108 }
109 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105, fixup_windbond_82c105);
110
111 void 
112 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
113                         struct resource *res)
114 {
115         unsigned long offset = 0;
116         struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
117
118         if (!hose)
119                 return;
120
121         if (res->flags & IORESOURCE_IO)
122                 offset = (unsigned long)hose->io_base_virt - pci_io_base;
123
124         if (res->flags & IORESOURCE_MEM)
125                 offset = hose->pci_mem_offset;
126
127         region->start = res->start - offset;
128         region->end = res->end - offset;
129 }
130
131 #ifdef CONFIG_HOTPLUG
132 EXPORT_SYMBOL(pcibios_resource_to_bus);
133 #endif
134
135 /*
136  * We need to avoid collisions with `mirrored' VGA ports
137  * and other strange ISA hardware, so we always want the
138  * addresses to be allocated in the 0x000-0x0ff region
139  * modulo 0x400.
140  *
141  * Why? Because some silly external IO cards only decode
142  * the low 10 bits of the IO address. The 0x00-0xff region
143  * is reserved for motherboard devices that decode all 16
144  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
145  * but we want to try to avoid allocating at 0x2900-0x2bff
146  * which might have be mirrored at 0x0100-0x03ff..
147  */
148 void pcibios_align_resource(void *data, struct resource *res,
149                             unsigned long size, unsigned long align)
150 {
151         struct pci_dev *dev = data;
152         struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
153         unsigned long start = res->start;
154         unsigned long alignto;
155
156         if (res->flags & IORESOURCE_IO) {
157                 unsigned long offset = (unsigned long)hose->io_base_virt -
158                                         pci_io_base;
159                 /* Make sure we start at our min on all hoses */
160                 if (start - offset < PCIBIOS_MIN_IO)
161                         start = PCIBIOS_MIN_IO + offset;
162
163                 /*
164                  * Put everything into 0x00-0xff region modulo 0x400
165                  */
166                 if (start & 0x300)
167                         start = (start + 0x3ff) & ~0x3ff;
168
169         } else if (res->flags & IORESOURCE_MEM) {
170                 /* Make sure we start at our min on all hoses */
171                 if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
172                         start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
173
174                 /* Align to multiple of size of minimum base.  */
175                 alignto = max(0x1000UL, align);
176                 start = ALIGN(start, alignto);
177         }
178
179         res->start = start;
180 }
181
182 /* 
183  * Allocate pci_controller(phb) initialized common variables. 
184  */
185 struct pci_controller * __init
186 pci_alloc_pci_controller(enum phb_types controller_type)
187 {
188         struct pci_controller *hose;
189         char *model;
190
191 #ifdef CONFIG_PPC_ISERIES
192         hose = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
193 #else
194         hose = (struct pci_controller *)alloc_bootmem(sizeof(struct pci_controller));
195 #endif
196         if(hose == NULL) {
197                 printk(KERN_ERR "PCI: Allocate pci_controller failed.\n");
198                 return NULL;
199         }
200         memset(hose, 0, sizeof(struct pci_controller));
201
202         switch(controller_type) {
203 #ifdef CONFIG_PPC_ISERIES
204         case phb_type_hypervisor:
205                 model = "PHB HV";
206                 break;
207 #endif
208         case phb_type_python:
209                 model = "PHB PY";
210                 break;
211         case phb_type_speedwagon:
212                 model = "PHB SW";
213                 break;
214         case phb_type_winnipeg:
215                 model = "PHB WP";
216                 break;
217         case phb_type_apple:
218                 model = "PHB APPLE";
219                 break;
220         default:
221                 model = "PHB UK";
222                 break;
223         }
224
225         if(strlen(model) < 8)
226                 strcpy(hose->what,model);
227         else
228                 memcpy(hose->what,model,7);
229         hose->type = controller_type;
230         hose->global_number = global_phb_number++;
231
232         list_add_tail(&hose->list_node, &hose_list);
233
234         return hose;
235 }
236
237 static void __init pcibios_claim_one_bus(struct pci_bus *b)
238 {
239         struct list_head *ld;
240         struct pci_bus *child_bus;
241
242         for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
243                 struct pci_dev *dev = pci_dev_b(ld);
244                 int i;
245
246                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
247                         struct resource *r = &dev->resource[i];
248
249                         if (r->parent || !r->start || !r->flags)
250                                 continue;
251                         pci_claim_resource(dev, i);
252                 }
253         }
254
255         list_for_each_entry(child_bus, &b->children, node)
256                 pcibios_claim_one_bus(child_bus);
257 }
258
259 #ifndef CONFIG_PPC_ISERIES
260 static void __init pcibios_claim_of_setup(void)
261 {
262         struct list_head *lb;
263
264         for (lb = pci_root_buses.next; lb != &pci_root_buses; lb = lb->next) {
265                 struct pci_bus *b = pci_bus_b(lb);
266                 pcibios_claim_one_bus(b);
267         }
268 }
269 #endif
270
271 static int __init pcibios_init(void)
272 {
273         struct pci_controller *hose, *tmp;
274         struct pci_bus *bus;
275
276 #ifdef CONFIG_PPC_ISERIES
277         iSeries_pcibios_init(); 
278 #endif
279
280         printk("PCI: Probing PCI hardware\n");
281
282         /* Scan all of the recorded PCI controllers.  */
283         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
284                 hose->last_busno = 0xff;
285                 bus = pci_scan_bus(hose->first_busno, hose->ops,
286                                    hose->arch_data);
287                 hose->bus = bus;
288                 hose->last_busno = bus->subordinate;
289         }
290
291 #ifndef CONFIG_PPC_ISERIES
292         if (pci_probe_only)
293                 pcibios_claim_of_setup();
294         else
295                 /* FIXME: `else' will be removed when
296                    pci_assign_unassigned_resources() is able to work
297                    correctly with [partially] allocated PCI tree. */
298                 pci_assign_unassigned_resources();
299 #endif /* !CONFIG_PPC_ISERIES */
300
301         /* Call machine dependent final fixup */
302         if (ppc_md.pcibios_fixup)
303                 ppc_md.pcibios_fixup();
304
305         /* Cache the location of the ISA bridge (if we have one) */
306         ppc64_isabridge_dev = pci_find_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
307         if (ppc64_isabridge_dev != NULL)
308                 printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
309
310         printk("PCI: Probing PCI hardware done\n");
311
312         return 0;
313 }
314
315 subsys_initcall(pcibios_init);
316
317 char __init *pcibios_setup(char *str)
318 {
319         return str;
320 }
321
322 int pcibios_enable_device(struct pci_dev *dev, int mask)
323 {
324         u16 cmd, oldcmd;
325         int i;
326
327         pci_read_config_word(dev, PCI_COMMAND, &cmd);
328         oldcmd = cmd;
329
330         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
331                 struct resource *res = &dev->resource[i];
332
333                 /* Only set up the requested stuff */
334                 if (!(mask & (1<<i)))
335                         continue;
336
337                 if (res->flags & IORESOURCE_IO)
338                         cmd |= PCI_COMMAND_IO;
339                 if (res->flags & IORESOURCE_MEM)
340                         cmd |= PCI_COMMAND_MEMORY;
341         }
342
343         if (cmd != oldcmd) {
344                 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
345                        pci_name(dev), cmd);
346                 /* Enable the appropriate bits in the PCI command register.  */
347                 pci_write_config_word(dev, PCI_COMMAND, cmd);
348         }
349         return 0;
350 }
351
352 /*
353  * Return the domain number for this bus.
354  */
355 int pci_domain_nr(struct pci_bus *bus)
356 {
357 #ifdef CONFIG_PPC_ISERIES
358         return 0;
359 #else
360         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
361
362         return hose->global_number;
363 #endif
364 }
365
366 EXPORT_SYMBOL(pci_domain_nr);
367
368 /* Set the name of the bus as it appears in /proc/bus/pci */
369 int pci_name_bus(char *name, struct pci_bus *bus)
370 {
371 #ifndef CONFIG_PPC_ISERIES
372         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
373
374         if (hose->buid)
375                 sprintf(name, "%04x:%02x", pci_domain_nr(bus), bus->number);
376         else
377 #endif
378                 sprintf(name, "%02x", bus->number);
379
380         return 0;
381 }
382
383 /*
384  * Platform support for /proc/bus/pci/X/Y mmap()s,
385  * modelled on the sparc64 implementation by Dave Miller.
386  *  -- paulus.
387  */
388
389 /*
390  * Adjust vm_pgoff of VMA such that it is the physical page offset
391  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
392  *
393  * Basically, the user finds the base address for his device which he wishes
394  * to mmap.  They read the 32-bit value from the config space base register,
395  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
396  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
397  *
398  * Returns negative error code on failure, zero on success.
399  */
400 static __inline__ int
401 __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
402                        enum pci_mmap_state mmap_state)
403 {
404         struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
405         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
406         unsigned long io_offset = 0;
407         int i, res_bit;
408
409         if (hose == 0)
410                 return -EINVAL;         /* should never happen */
411
412         /* If memory, add on the PCI bridge address offset */
413         if (mmap_state == pci_mmap_mem) {
414                 offset += hose->pci_mem_offset;
415                 res_bit = IORESOURCE_MEM;
416         } else {
417                 io_offset = (unsigned long)hose->io_base_virt;
418                 offset += io_offset;
419                 res_bit = IORESOURCE_IO;
420         }
421
422         /*
423          * Check that the offset requested corresponds to one of the
424          * resources of the device.
425          */
426         for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
427                 struct resource *rp = &dev->resource[i];
428                 int flags = rp->flags;
429
430                 /* treat ROM as memory (should be already) */
431                 if (i == PCI_ROM_RESOURCE)
432                         flags |= IORESOURCE_MEM;
433
434                 /* Active and same type? */
435                 if ((flags & res_bit) == 0)
436                         continue;
437
438                 /* In the range of this resource? */
439                 if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
440                         continue;
441
442                 /* found it! construct the final physical address */
443                 if (mmap_state == pci_mmap_io)
444                         offset += hose->io_base_phys - io_offset;
445
446                 vma->vm_pgoff = offset >> PAGE_SHIFT;
447                 return 0;
448         }
449
450         return -EINVAL;
451 }
452
453 /*
454  * Set vm_flags of VMA, as appropriate for this architecture, for a pci device
455  * mapping.
456  */
457 static __inline__ void
458 __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
459                      enum pci_mmap_state mmap_state)
460 {
461         vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
462 }
463
464 /*
465  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
466  * device mapping.
467  */
468 static __inline__ void
469 __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
470                       enum pci_mmap_state mmap_state, int write_combine)
471 {
472         long prot = pgprot_val(vma->vm_page_prot);
473
474         /* XXX would be nice to have a way to ask for write-through */
475         prot |= _PAGE_NO_CACHE;
476         if (!write_combine)
477                 prot |= _PAGE_GUARDED;
478         vma->vm_page_prot = __pgprot(prot);
479 }
480
481 /*
482  * Perform the actual remap of the pages for a PCI device mapping, as
483  * appropriate for this architecture.  The region in the process to map
484  * is described by vm_start and vm_end members of VMA, the base physical
485  * address is found in vm_pgoff.
486  * The pci device structure is provided so that architectures may make mapping
487  * decisions on a per-device or per-bus basis.
488  *
489  * Returns a negative error code on failure, zero on success.
490  */
491 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
492                         enum pci_mmap_state mmap_state,
493                         int write_combine)
494 {
495         int ret;
496
497         ret = __pci_mmap_make_offset(dev, vma, mmap_state);
498         if (ret < 0)
499                 return ret;
500
501         __pci_mmap_set_flags(dev, vma, mmap_state);
502         __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
503
504         ret = remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
505                                vma->vm_end - vma->vm_start, vma->vm_page_prot);
506
507         return ret;
508 }
509
510 #ifdef CONFIG_PPC_MULTIPLATFORM
511 static ssize_t pci_show_devspec(struct device *dev, char *buf)
512 {
513         struct pci_dev *pdev;
514         struct device_node *np;
515
516         pdev = to_pci_dev (dev);
517         np = pci_device_to_OF_node(pdev);
518         if (np == NULL || np->full_name == NULL)
519                 return 0;
520         return sprintf(buf, "%s", np->full_name);
521 }
522 static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
523 #endif /* CONFIG_PPC_MULTIPLATFORM */
524
525 void pcibios_add_platform_entries(struct pci_dev *pdev)
526 {
527 #ifdef CONFIG_PPC_MULTIPLATFORM
528         device_create_file(&pdev->dev, &dev_attr_devspec);
529 #endif /* CONFIG_PPC_MULTIPLATFORM */
530 }
531
532 #ifdef CONFIG_PPC_MULTIPLATFORM
533
534 #define ISA_SPACE_MASK 0x1
535 #define ISA_SPACE_IO 0x1
536
537 static void pci_process_ISA_OF_ranges(struct device_node *isa_node,
538                                       unsigned long phb_io_base_phys,
539                                       void * phb_io_base_virt)
540 {
541         struct isa_range *range;
542         unsigned long pci_addr;
543         unsigned int isa_addr;
544         unsigned int size;
545         int rlen = 0;
546
547         range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
548         if (rlen < sizeof(struct isa_range)) {
549                 printk(KERN_ERR "unexpected isa range size: %s\n", 
550                                 __FUNCTION__);
551                 return; 
552         }
553         
554         /* From "ISA Binding to 1275"
555          * The ranges property is laid out as an array of elements,
556          * each of which comprises:
557          *   cells 0 - 1:       an ISA address
558          *   cells 2 - 4:       a PCI address 
559          *                      (size depending on dev->n_addr_cells)
560          *   cell 5:            the size of the range
561          */
562         if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
563                 isa_addr = range->isa_addr.a_lo;
564                 pci_addr = (unsigned long) range->pci_addr.a_mid << 32 | 
565                         range->pci_addr.a_lo;
566
567                 /* Assume these are both zero */
568                 if ((pci_addr != 0) || (isa_addr != 0)) {
569                         printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
570                                         __FUNCTION__);
571                         return;
572                 }
573                 
574                 size = PAGE_ALIGN(range->size);
575
576                 __ioremap_explicit(phb_io_base_phys, 
577                                    (unsigned long) phb_io_base_virt, 
578                                    size, _PAGE_NO_CACHE);
579         }
580 }
581
582 void __init pci_process_bridge_OF_ranges(struct pci_controller *hose,
583                                          struct device_node *dev, int primary)
584 {
585         unsigned int *ranges;
586         unsigned long size;
587         int rlen = 0;
588         int memno = 0;
589         struct resource *res;
590         int np, na = prom_n_addr_cells(dev);
591         unsigned long pci_addr, cpu_phys_addr;
592         struct device_node *isa_dn;
593
594         np = na + 5;
595
596         /* From "PCI Binding to 1275"
597          * The ranges property is laid out as an array of elements,
598          * each of which comprises:
599          *   cells 0 - 2:       a PCI address
600          *   cells 3 or 3+4:    a CPU physical address
601          *                      (size depending on dev->n_addr_cells)
602          *   cells 4+5 or 5+6:  the size of the range
603          */
604         rlen = 0;
605         hose->io_base_phys = 0;
606         ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
607         while ((rlen -= np * sizeof(unsigned int)) >= 0) {
608                 res = NULL;
609                 pci_addr = (unsigned long)ranges[1] << 32 | ranges[2];
610
611                 cpu_phys_addr = ranges[3];
612                 if (na == 2)
613                         cpu_phys_addr = cpu_phys_addr << 32 | ranges[4];
614
615                 size = (unsigned long)ranges[na+3] << 32 | ranges[na+4];
616
617                 switch (ranges[0] >> 24) {
618                 case 1:         /* I/O space */
619                         hose->io_base_phys = cpu_phys_addr;
620                         hose->io_base_virt = reserve_phb_iospace(size);
621                         PPCDBG(PPCDBG_PHBINIT, 
622                                "phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n", 
623                                hose->global_number, hose->io_base_phys, 
624                                (unsigned long) hose->io_base_virt);
625
626                         if (primary) {
627                                 pci_io_base = (unsigned long)hose->io_base_virt;
628                                 isa_dn = of_find_node_by_type(NULL, "isa");
629                                 if (isa_dn) {
630                                         isa_io_base = pci_io_base;
631                                         pci_process_ISA_OF_ranges(isa_dn,
632                                                 hose->io_base_phys,
633                                                 hose->io_base_virt);
634                                         of_node_put(isa_dn);
635                                         /* Allow all IO */
636                                         io_page_mask = -1;
637                                 }
638                         }
639
640                         res = &hose->io_resource;
641                         res->flags = IORESOURCE_IO;
642                         res->start = pci_addr;
643                         res->start += (unsigned long)hose->io_base_virt -
644                                 pci_io_base;
645                         break;
646                 case 2:         /* memory space */
647                         memno = 0;
648                         while (memno < 3 && hose->mem_resources[memno].flags)
649                                 ++memno;
650
651                         if (memno == 0)
652                                 hose->pci_mem_offset = cpu_phys_addr - pci_addr;
653                         if (memno < 3) {
654                                 res = &hose->mem_resources[memno];
655                                 res->flags = IORESOURCE_MEM;
656                                 res->start = cpu_phys_addr;
657                         }
658                         break;
659                 }
660                 if (res != NULL) {
661                         res->name = dev->full_name;
662                         res->end = res->start + size - 1;
663                         res->parent = NULL;
664                         res->sibling = NULL;
665                         res->child = NULL;
666                 }
667                 ranges += np;
668         }
669 }
670
671 /*********************************************************************** 
672  * pci_find_hose_for_OF_device
673  *
674  * This function finds the PHB that matching device_node in the 
675  * OpenFirmware by scanning all the pci_controllers.
676  * 
677  ***********************************************************************/
678 struct pci_controller* pci_find_hose_for_OF_device(struct device_node *node)
679 {
680         while (node) {
681                 struct pci_controller *hose, *tmp;
682                 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
683                         if (hose->arch_data == node)
684                                 return hose;
685                 node=node->parent;
686         }
687         return NULL;
688 }
689
690 /*
691  * ppc64 can have multifunction devices that do not respond to function 0.
692  * In this case we must scan all functions.
693  */
694 int pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
695 {
696        struct device_node *busdn, *dn;
697
698        if (bus->self)
699                busdn = pci_device_to_OF_node(bus->self);
700        else
701                busdn = bus->sysdata;   /* must be a phb */
702
703        /*
704         * Check to see if there is any of the 8 functions are in the
705         * device tree.  If they are then we need to scan all the
706         * functions of this slot.
707         */
708        for (dn = busdn->child; dn; dn = dn->sibling)
709                if ((dn->devfn >> 3) == (devfn >> 3))
710                        return 1;
711
712        return 0;
713 }
714
715
716 void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
717                                            struct pci_bus *bus)
718 {
719         /* Update device resources.  */
720         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
721         int i;
722
723         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
724                 if (dev->resource[i].flags & IORESOURCE_IO) {
725                         unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
726                         unsigned long start, end, mask;
727
728                         start = dev->resource[i].start += offset;
729                         end = dev->resource[i].end += offset;
730
731                         /* Need to allow IO access to pages that are in the
732                            ISA range */
733                         if (start < MAX_ISA_PORT) {
734                                 if (end > MAX_ISA_PORT)
735                                         end = MAX_ISA_PORT;
736
737                                 start >>= PAGE_SHIFT;
738                                 end >>= PAGE_SHIFT;
739
740                                 /* get the range of pages for the map */
741                                 mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
742                                 io_page_mask |= mask;
743                         }
744                 }
745                 else if (dev->resource[i].flags & IORESOURCE_MEM) {
746                         dev->resource[i].start += hose->pci_mem_offset;
747                         dev->resource[i].end += hose->pci_mem_offset;
748                 }
749         }
750 }
751 EXPORT_SYMBOL(pcibios_fixup_device_resources);
752
753 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
754 {
755         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
756         struct list_head *ln;
757
758         /* XXX or bus->parent? */
759         struct pci_dev *dev = bus->self;
760         struct resource *res;
761         int i;
762
763         if (!dev) {
764                 /* Root bus. */
765
766                 hose->bus = bus;
767                 bus->resource[0] = res = &hose->io_resource;
768                 if (!res->flags)
769                         BUG();  /* No I/O resource for this PHB? */
770
771                 if (request_resource(&ioport_resource, res))
772                         printk(KERN_ERR "Failed to request IO on "
773                                         "PCI domain %d\n", pci_domain_nr(bus));
774
775
776                 for (i = 0; i < 3; ++i) {
777                         res = &hose->mem_resources[i];
778                         if (!res->flags && i == 0)
779                                 BUG();  /* No memory resource for this PHB? */
780                         bus->resource[i+1] = res;
781                         if (res->flags && request_resource(&iomem_resource, res))
782                                 printk(KERN_ERR "Failed to request MEM on "
783                                                 "PCI domain %d\n",
784                                                 pci_domain_nr(bus));
785                 }
786         } else if (pci_probe_only &&
787                    (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
788                 /* This is a subordinate bridge */
789
790                 pci_read_bridge_bases(bus);
791                 pcibios_fixup_device_resources(dev, bus);
792         }
793
794         /* XXX Need to check why Alpha doesnt do this - Anton */
795         if (!pci_probe_only)
796                 return;
797
798         for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
799                 struct pci_dev *dev = pci_dev_b(ln);
800                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
801                         pcibios_fixup_device_resources(dev, bus);
802         }
803 }
804 EXPORT_SYMBOL(pcibios_fixup_bus);
805
806 /******************************************************************
807  * pci_read_irq_line
808  *
809  * Reads the Interrupt Pin to determine if interrupt is use by card.
810  * If the interrupt is used, then gets the interrupt line from the 
811  * openfirmware and sets it in the pci_dev and pci_config line.
812  *
813  ******************************************************************/
814 int pci_read_irq_line(struct pci_dev *pci_dev)
815 {
816         u8 intpin;
817         struct device_node *node;
818
819         pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
820
821         if (intpin == 0) {
822                 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s No Interrupt used by device.\n",
823                        pci_name(pci_dev));
824                 return 0;       
825         }
826
827         node = pci_device_to_OF_node(pci_dev);
828         if (node == NULL) { 
829                 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s Device Node not found.\n",
830                        pci_name(pci_dev));
831                 return -1;      
832         }
833         if (node->n_intrs == 0)         {
834                 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s No Device OF interrupts defined.\n",
835                        pci_name(pci_dev));
836                 return -1;      
837         }
838         pci_dev->irq = node->intrs[0].line;
839
840         pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
841         
842         PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s pci_dev->irq = 0x%02X\n",
843                pci_name(pci_dev), pci_dev->irq);
844         return 0;
845 }
846 EXPORT_SYMBOL(pci_read_irq_line);
847
848 #endif /* CONFIG_PPC_MULTIPLATFORM */