ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / pSeries_pci.c
1 /*
2  * pSeries_pci.c
3  *
4  * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
5  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6  *
7  * pSeries specific routines for PCI.
8  * 
9  * Based on code from pci.c and chrp_pci.c
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *    
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/threads.h>
28 #include <linux/pci.h>
29 #include <linux/delay.h>
30 #include <linux/string.h>
31 #include <linux/init.h>
32 #include <linux/bootmem.h>
33
34 #include <asm/io.h>
35 #include <asm/pgtable.h>
36 #include <asm/irq.h>
37 #include <asm/prom.h>
38 #include <asm/machdep.h>
39 #include <asm/pci-bridge.h>
40 #include <asm/ppcdebug.h>
41 #include <asm/naca.h>
42 #include <asm/iommu.h>
43
44 #include "open_pic.h"
45 #include "pci.h"
46
47 /* legal IO pages under MAX_ISA_PORT.  This is to ensure we don't touch
48    devices we don't have access to. */
49 unsigned long io_page_mask;
50
51 EXPORT_SYMBOL(io_page_mask);
52
53 /* RTAS tokens */
54 static int read_pci_config;
55 static int write_pci_config;
56 static int ibm_read_pci_config;
57 static int ibm_write_pci_config;
58
59 static int s7a_workaround;
60
61 extern unsigned long pci_probe_only;
62
63 static int rtas_read_config(struct device_node *dn, int where, int size, u32 *val)
64 {
65         unsigned long returnval = ~0L;
66         unsigned long buid, addr;
67         int ret;
68
69         if (!dn)
70                 return -2;
71
72         addr = (dn->busno << 16) | (dn->devfn << 8) | where;
73         buid = dn->phb->buid;
74         if (buid) {
75                 ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval, addr, buid >> 32, buid & 0xffffffff, size);
76         } else {
77                 ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size);
78         }
79         *val = returnval;
80         return ret;
81 }
82
83 static int rtas_pci_read_config(struct pci_bus *bus,
84                                 unsigned int devfn,
85                                 int where, int size, u32 *val)
86 {
87         struct device_node *busdn, *dn;
88
89         if (bus->self)
90                 busdn = pci_device_to_OF_node(bus->self);
91         else
92                 busdn = bus->sysdata;   /* must be a phb */
93
94         /* Search only direct children of the bus */
95         for (dn = busdn->child; dn; dn = dn->sibling)
96                 if (dn->devfn == devfn)
97                         return rtas_read_config(dn, where, size, val);
98         return PCIBIOS_DEVICE_NOT_FOUND;
99 }
100
101 static int rtas_write_config(struct device_node *dn, int where, int size, u32 val)
102 {
103         unsigned long buid, addr;
104         int ret;
105
106         if (!dn)
107                 return -2;
108
109         addr = (dn->busno << 16) | (dn->devfn << 8) | where;
110         buid = dn->phb->buid;
111         if (buid) {
112                 ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, buid >> 32, buid & 0xffffffff, size, (ulong) val);
113         } else {
114                 ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val);
115         }
116         return ret;
117 }
118
119 static int rtas_pci_write_config(struct pci_bus *bus,
120                                  unsigned int devfn,
121                                  int where, int size, u32 val)
122 {
123         struct device_node *busdn, *dn;
124
125         if (bus->self)
126                 busdn = pci_device_to_OF_node(bus->self);
127         else
128                 busdn = bus->sysdata;   /* must be a phb */
129
130         /* Search only direct children of the bus */
131         for (dn = busdn->child; dn; dn = dn->sibling)
132                 if (dn->devfn == devfn)
133                         return rtas_write_config(dn, where, size, val);
134         return PCIBIOS_DEVICE_NOT_FOUND;
135 }
136
137 struct pci_ops rtas_pci_ops = {
138         rtas_pci_read_config,
139         rtas_pci_write_config
140 };
141
142 /******************************************************************
143  * pci_read_irq_line
144  *
145  * Reads the Interrupt Pin to determine if interrupt is use by card.
146  * If the interrupt is used, then gets the interrupt line from the 
147  * openfirmware and sets it in the pci_dev and pci_config line.
148  *
149  ******************************************************************/
150 int pci_read_irq_line(struct pci_dev *pci_dev)
151 {
152         u8 intpin;
153         struct device_node *node;
154
155         pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
156
157         if (intpin == 0) {
158                 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s No Interrupt used by device.\n", pci_name(pci_dev));
159                 return 0;       
160         }
161
162         node = pci_device_to_OF_node(pci_dev);
163         if (node == NULL) { 
164                 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s Device Node not found.\n",
165                        pci_name(pci_dev));
166                 return -1;      
167         }
168         if (node->n_intrs == 0)         {
169                 PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s No Device OF interrupts defined.\n", pci_name(pci_dev));
170                 return -1;      
171         }
172         pci_dev->irq = node->intrs[0].line;
173
174         if (s7a_workaround) {
175                 if (pci_dev->irq > 16)
176                         pci_dev->irq -= 3;
177         }
178
179         pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
180         
181         PPCDBG(PPCDBG_BUSWALK,"\tDevice: %s pci_dev->irq = 0x%02X\n",
182                pci_name(pci_dev), pci_dev->irq);
183         return 0;
184 }
185 EXPORT_SYMBOL(pci_read_irq_line);
186
187 #define ISA_SPACE_MASK 0x1
188 #define ISA_SPACE_IO 0x1
189
190 static void pci_process_ISA_OF_ranges(struct device_node *isa_node,
191                                       unsigned long phb_io_base_phys,
192                                       void * phb_io_base_virt)
193 {
194         struct isa_range *range;
195         unsigned long pci_addr;
196         unsigned int isa_addr;
197         unsigned int size;
198         int rlen = 0;
199
200         range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
201         if (rlen < sizeof(struct isa_range)) {
202                 printk(KERN_ERR "unexpected isa range size: %s\n", 
203                                 __FUNCTION__);
204                 return; 
205         }
206         
207         /* From "ISA Binding to 1275"
208          * The ranges property is laid out as an array of elements,
209          * each of which comprises:
210          *   cells 0 - 1:       an ISA address
211          *   cells 2 - 4:       a PCI address 
212          *                      (size depending on dev->n_addr_cells)
213          *   cell 5:            the size of the range
214          */
215         if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
216                 isa_addr = range->isa_addr.a_lo;
217                 pci_addr = (unsigned long) range->pci_addr.a_mid << 32 | 
218                         range->pci_addr.a_lo;
219
220                 /* Assume these are both zero */
221                 if ((pci_addr != 0) || (isa_addr != 0)) {
222                         printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
223                                         __FUNCTION__);
224                         return;
225                 }
226                 
227                 size = PAGE_ALIGN(range->size);
228
229                 __ioremap_explicit(phb_io_base_phys, 
230                                    (unsigned long) phb_io_base_virt, 
231                                    size, _PAGE_NO_CACHE);
232         }
233 }
234
235 static void __init pci_process_bridge_OF_ranges(struct pci_controller *hose,
236                                                 struct device_node *dev,
237                                                 int primary)
238 {
239         unsigned int *ranges;
240         unsigned long size;
241         int rlen = 0;
242         int memno = 0;
243         struct resource *res;
244         int np, na = prom_n_addr_cells(dev);
245         unsigned long pci_addr, cpu_phys_addr;
246         struct device_node *isa_dn;
247
248         np = na + 5;
249
250         /* From "PCI Binding to 1275"
251          * The ranges property is laid out as an array of elements,
252          * each of which comprises:
253          *   cells 0 - 2:       a PCI address
254          *   cells 3 or 3+4:    a CPU physical address
255          *                      (size depending on dev->n_addr_cells)
256          *   cells 4+5 or 5+6:  the size of the range
257          */
258         rlen = 0;
259         hose->io_base_phys = 0;
260         ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
261         while ((rlen -= np * sizeof(unsigned int)) >= 0) {
262                 res = NULL;
263                 pci_addr = (unsigned long)ranges[1] << 32 | ranges[2];
264
265                 cpu_phys_addr = ranges[3];
266                 if (na == 2)
267                         cpu_phys_addr = cpu_phys_addr << 32 | ranges[4];
268
269                 size = (unsigned long)ranges[na+3] << 32 | ranges[na+4];
270
271                 switch (ranges[0] >> 24) {
272                 case 1:         /* I/O space */
273                         hose->io_base_phys = cpu_phys_addr;
274                         hose->io_base_virt = reserve_phb_iospace(size);
275                         PPCDBG(PPCDBG_PHBINIT, 
276                                "phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n", 
277                                hose->global_number, hose->io_base_phys, 
278                                (unsigned long) hose->io_base_virt);
279
280                         if (primary) {
281                                 pci_io_base = (unsigned long)hose->io_base_virt;
282                                 isa_dn = of_find_node_by_type(NULL, "isa");
283                                 if (isa_dn) {
284                                         isa_io_base = pci_io_base;
285                                         of_node_put(isa_dn);
286                                         pci_process_ISA_OF_ranges(isa_dn,
287                                                 hose->io_base_phys,
288                                                 hose->io_base_virt);
289                                         /* Allow all IO */
290                                         io_page_mask = -1;
291                                 }
292                         }
293
294                         res = &hose->io_resource;
295                         res->flags = IORESOURCE_IO;
296                         res->start = pci_addr;
297                         res->start += (unsigned long)hose->io_base_virt -
298                                 pci_io_base;
299                         break;
300                 case 2:         /* memory space */
301                         memno = 0;
302                         while (memno < 3 && hose->mem_resources[memno].flags)
303                                 ++memno;
304
305                         if (memno == 0)
306                                 hose->pci_mem_offset = cpu_phys_addr - pci_addr;
307                         if (memno < 3) {
308                                 res = &hose->mem_resources[memno];
309                                 res->flags = IORESOURCE_MEM;
310                                 res->start = cpu_phys_addr;
311                         }
312                         break;
313                 }
314                 if (res != NULL) {
315                         res->name = dev->full_name;
316                         res->end = res->start + size - 1;
317                         res->parent = NULL;
318                         res->sibling = NULL;
319                         res->child = NULL;
320                 }
321                 ranges += np;
322         }
323 }
324
325 static void python_countermeasures(unsigned long addr)
326 {
327         void *chip_regs;
328         volatile u32 *tmp, i;
329
330         /* Python's register file is 1 MB in size. */
331         chip_regs = ioremap(addr & ~(0xfffffUL), 0x100000); 
332
333         /* 
334          * Firmware doesn't always clear this bit which is critical
335          * for good performance - Anton
336          */
337
338 #define PRG_CL_RESET_VALID 0x00010000
339
340         tmp = (u32 *)((unsigned long)chip_regs + 0xf6030);
341
342         if (*tmp & PRG_CL_RESET_VALID) {
343                 printk(KERN_INFO "Python workaround: ");
344                 *tmp &= ~PRG_CL_RESET_VALID;
345                 /*
346                  * We must read it back for changes to
347                  * take effect
348                  */
349                 i = *tmp;
350                 printk("reg0: %x\n", i);
351         }
352
353         iounmap(chip_regs);
354 }
355
356 struct pci_controller *alloc_phb(struct device_node *dev,
357                                  unsigned int addr_size_words)
358 {
359         struct pci_controller *phb;
360         unsigned int *ui_ptr = NULL, len;
361         struct reg_property64 reg_struct;
362         int *bus_range;
363         int *buid_vals;
364         char *model;
365         enum phb_types phb_type;
366         struct property *of_prop;
367
368         model = (char *)get_property(dev, "model", NULL);
369
370         if (!model) {
371                 printk(KERN_ERR "alloc_phb: phb has no model property\n");
372                 model = "<empty>";
373         }
374
375         /* Found a PHB, now figure out where his registers are mapped. */
376         ui_ptr = (unsigned int *) get_property(dev, "reg", &len);
377         if (ui_ptr == NULL) {
378                 PPCDBG(PPCDBG_PHBINIT, "\tget reg failed.\n"); 
379                 return NULL;
380         }
381
382         if (addr_size_words == 1) {
383                 reg_struct.address = ((struct reg_property32 *)ui_ptr)->address;
384                 reg_struct.size    = ((struct reg_property32 *)ui_ptr)->size;
385         } else {
386                 reg_struct = *((struct reg_property64 *)ui_ptr);
387         }
388
389         if (strstr(model, "Python")) {
390                 phb_type = phb_type_python;
391         } else if (strstr(model, "Speedwagon")) {
392                 phb_type = phb_type_speedwagon;
393         } else if (strstr(model, "Winnipeg")) {
394                 phb_type = phb_type_winnipeg;
395         } else {
396                 printk(KERN_ERR "alloc_phb: unknown PHB %s\n", model);
397                 phb_type = phb_type_unknown;
398         }
399
400         phb = pci_alloc_pci_controller(phb_type);
401         if (phb == NULL)
402                 return NULL;
403
404         if (phb_type == phb_type_python)
405                 python_countermeasures(reg_struct.address);
406
407         bus_range = (int *) get_property(dev, "bus-range", &len);
408         if (bus_range == NULL || len < 2 * sizeof(int)) {
409                 kfree(phb);
410                 return NULL;
411         }
412
413         of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
414                         sizeof(phb->global_number));        
415
416         if (!of_prop) {
417                 kfree(phb);
418                 return NULL;
419         }
420
421         memset(of_prop, 0, sizeof(struct property));
422         of_prop->name = "linux,pci-domain";
423         of_prop->length = sizeof(phb->global_number);
424         of_prop->value = (unsigned char *)&of_prop[1];
425         memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
426         prom_add_property(dev, of_prop);
427
428         phb->first_busno =  bus_range[0];
429         phb->last_busno  =  bus_range[1];
430
431         phb->arch_data   = dev;
432         phb->ops = &rtas_pci_ops;
433
434         buid_vals = (int *) get_property(dev, "ibm,fw-phb-id", &len);
435
436         if (buid_vals == NULL) {
437                 phb->buid = 0;
438         } else {
439                 if (len < 2 * sizeof(int))
440                         // Support for new OF that only has 1 integer for buid.
441                         phb->buid = (unsigned long)buid_vals[0];
442                 else
443                         phb->buid = (((unsigned long)buid_vals[0]) << 32UL) |
444                                 (((unsigned long)buid_vals[1]) & 0xffffffff);
445         }
446
447         return phb;
448 }
449
450 unsigned long __init find_and_init_phbs(void)
451 {
452         struct device_node *node;
453         struct pci_controller *phb;
454         unsigned int root_size_cells = 0;
455         unsigned int index;
456         unsigned int *opprop;
457         struct device_node *root = of_find_node_by_path("/");
458
459         read_pci_config = rtas_token("read-pci-config");
460         write_pci_config = rtas_token("write-pci-config");
461         ibm_read_pci_config = rtas_token("ibm,read-pci-config");
462         ibm_write_pci_config = rtas_token("ibm,write-pci-config");
463
464         if (naca->interrupt_controller == IC_OPEN_PIC) {
465                 opprop = (unsigned int *)get_property(root,
466                                 "platform-open-pic", NULL);
467         }
468
469         root_size_cells = prom_n_size_cells(root);
470
471         index = 0;
472
473         for (node = of_get_next_child(root, NULL);
474              node != NULL;
475              node = of_get_next_child(root, node)) {
476                 if (node->type == NULL || strcmp(node->type, "pci") != 0)
477                         continue;
478
479                 phb = alloc_phb(node, root_size_cells);
480                 if (!phb)
481                         continue;
482
483                 pci_process_bridge_OF_ranges(phb, node, index == 0);
484
485                 if (naca->interrupt_controller == IC_OPEN_PIC) {
486                         int addr = root_size_cells * (index + 2) - 1;
487                         openpic_setup_ISU(index, opprop[addr]); 
488                 }
489
490                 index++;
491         }
492
493         of_node_put(root);
494         pci_devs_phb_init();
495
496         return 0;
497 }
498
499 void pcibios_name_device(struct pci_dev *dev)
500 {
501 #if 0
502         struct device_node *dn;
503
504         /*
505          * Add IBM loc code (slot) as a prefix to the device names for service
506          */
507         dn = pci_device_to_OF_node(dev);
508         if (dn) {
509                 char *loc_code = get_property(dn, "ibm,loc-code", 0);
510                 if (loc_code) {
511                         int loc_len = strlen(loc_code);
512                         if (loc_len < sizeof(dev->dev.name)) {
513                                 memmove(dev->dev.name+loc_len+1, dev->dev.name,
514                                         sizeof(dev->dev.name)-loc_len-1);
515                                 memcpy(dev->dev.name, loc_code, loc_len);
516                                 dev->dev.name[loc_len] = ' ';
517                                 dev->dev.name[sizeof(dev->dev.name)-1] = '\0';
518                         }
519                 }
520         }
521 #endif
522 }   
523
524 void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
525                                            struct pci_bus *bus)
526 {
527         /* Update device resources.  */
528         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
529         int i;
530
531         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
532                 if (dev->resource[i].flags & IORESOURCE_IO) {
533                         unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
534                         unsigned long start, end, mask;
535
536                         start = dev->resource[i].start += offset;
537                         end = dev->resource[i].end += offset;
538
539                         /* Need to allow IO access to pages that are in the
540                            ISA range */
541                         if (start < MAX_ISA_PORT) {
542                                 if (end > MAX_ISA_PORT)
543                                         end = MAX_ISA_PORT;
544
545                                 start >>= PAGE_SHIFT;
546                                 end >>= PAGE_SHIFT;
547
548                                 /* get the range of pages for the map */
549                                 mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
550                                 io_page_mask |= mask;
551                         }
552                 }
553                 else if (dev->resource[i].flags & IORESOURCE_MEM) {
554                         dev->resource[i].start += hose->pci_mem_offset;
555                         dev->resource[i].end += hose->pci_mem_offset;
556                 }
557         }
558 }
559 EXPORT_SYMBOL(pcibios_fixup_device_resources);
560
561 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
562 {
563         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
564         struct list_head *ln;
565
566         /* XXX or bus->parent? */
567         struct pci_dev *dev = bus->self;
568         struct resource *res;
569         int i;
570
571         if (!dev) {
572                 /* Root bus. */
573
574                 hose->bus = bus;
575                 bus->resource[0] = res = &hose->io_resource;
576                 if (!res->flags)
577                         BUG();  /* No I/O resource for this PHB? */
578
579                 if (request_resource(&ioport_resource, res))
580                         printk(KERN_ERR "Failed to request IO"
581                                         "on hose %d\n", 0 /* FIXME */);
582
583                 for (i = 0; i < 3; ++i) {
584                         res = &hose->mem_resources[i];
585                         if (!res->flags && i == 0)
586                                 BUG();  /* No memory resource for this PHB? */
587                         bus->resource[i+1] = res;
588                         if (res->flags && request_resource(&iomem_resource, res))
589                                 printk(KERN_ERR "Failed to request MEM"
590                                                 "on hose %d\n", 0 /* FIXME */);
591                 }
592         } else if (pci_probe_only &&
593                    (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
594                 /* This is a subordinate bridge */
595
596                 pci_read_bridge_bases(bus);
597                 pcibios_fixup_device_resources(dev, bus);
598         }
599
600         /* XXX Need to check why Alpha doesnt do this - Anton */
601         if (!pci_probe_only)
602                 return;
603
604         for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
605                 struct pci_dev *dev = pci_dev_b(ln);
606                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
607                         pcibios_fixup_device_resources(dev, bus);
608         }
609 }
610 EXPORT_SYMBOL(pcibios_fixup_bus);
611
612 static void check_s7a(void)
613 {
614         struct device_node *root;
615         char *model;
616
617         root = of_find_node_by_path("/");
618         if (root) {
619                 model = get_property(root, "model", NULL);
620                 if (model && !strcmp(model, "IBM,7013-S7A"))
621                         s7a_workaround = 1;
622                 of_node_put(root);
623         }
624 }
625
626 static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
627                                 unsigned long *start_virt, unsigned long *size)
628 {
629         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
630         struct pci_bus_region region;
631         struct resource *res;
632
633         if (bus->self) {
634                 res = bus->resource[0];
635                 pcibios_resource_to_bus(bus->self, &region, res);
636                 *start_phys = hose->io_base_phys + region.start;
637                 *start_virt = (unsigned long) hose->io_base_virt + 
638                                 region.start;
639                 if (region.end > region.start) 
640                         *size = region.end - region.start + 1;
641                 else {
642                         printk("%s(): unexpected region 0x%lx->0x%lx\n", 
643                                         __FUNCTION__, region.start, region.end);
644                         return 1;
645                 }
646                 
647         } else {
648                 /* Root Bus */
649                 res = &hose->io_resource;
650                 *start_phys = hose->io_base_phys;
651                 *start_virt = (unsigned long) hose->io_base_virt;
652                 if (res->end > res->start)
653                         *size = res->end - res->start + 1;
654                 else {
655                         printk("%s(): unexpected region 0x%lx->0x%lx\n", 
656                                         __FUNCTION__, res->start, res->end);
657                         return 1;
658                 }
659         }
660
661         return 0;
662 }
663
664 int unmap_bus_range(struct pci_bus *bus)
665 {
666         unsigned long start_phys;
667         unsigned long start_virt;
668         unsigned long size;
669
670         if (!bus) {
671                 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
672                 return 1;
673         }
674         
675         if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
676                 return 1;
677         if (iounmap_explicit((void *) start_virt, size))
678                 return 1;
679
680         return 0;
681 }
682 EXPORT_SYMBOL(unmap_bus_range);
683
684 int remap_bus_range(struct pci_bus *bus)
685 {
686         unsigned long start_phys;
687         unsigned long start_virt;
688         unsigned long size;
689
690         if (!bus) {
691                 printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
692                 return 1;
693         }
694         
695         if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
696                 return 1;
697         if (__ioremap_explicit(start_phys, start_virt, size, _PAGE_NO_CACHE))
698                 return 1;
699
700         return 0;
701 }
702 EXPORT_SYMBOL(remap_bus_range);
703
704 static void phbs_fixup_io(void)
705 {
706         struct pci_controller *hose;
707
708         for (hose=hose_head;hose;hose=hose->next) 
709                 remap_bus_range(hose->bus);
710 }
711
712 extern void chrp_request_regions(void);
713
714 void __init pSeries_final_fixup(void)
715 {
716         struct pci_dev *dev = NULL;
717
718         check_s7a();
719
720         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
721                 pci_read_irq_line(dev);
722
723         phbs_fixup_io();
724         chrp_request_regions();
725         pci_fix_bus_sysdata();
726         if (!ppc64_iommu_off)
727                 iommu_setup_pSeries();
728 }
729
730 /*********************************************************************** 
731  * pci_find_hose_for_OF_device
732  *
733  * This function finds the PHB that matching device_node in the 
734  * OpenFirmware by scanning all the pci_controllers.
735  * 
736  ***********************************************************************/
737 struct pci_controller*
738 pci_find_hose_for_OF_device(struct device_node *node)
739 {
740         while (node) {
741                 struct pci_controller *hose;
742                 for (hose=hose_head;hose;hose=hose->next)
743                         if (hose->arch_data == node)
744                                 return hose;
745                 node=node->parent;
746         }
747         return NULL;
748 }
749
750 /*
751  * ppc64 can have multifunction devices that do not respond to function 0.
752  * In this case we must scan all functions.
753  */
754 int
755 pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
756 {
757        struct device_node *busdn, *dn;
758
759        if (bus->self)
760                busdn = pci_device_to_OF_node(bus->self);
761        else
762                busdn = bus->sysdata;   /* must be a phb */
763
764        /*
765         * Check to see if there is any of the 8 functions are in the
766         * device tree.  If they are then we need to scan all the
767         * functions of this slot.
768         */
769        for (dn = busdn->child; dn; dn = dn->sibling)
770                if ((dn->devfn >> 3) == (devfn >> 3))
771                        return 1;
772
773        return 0;
774 }