]> err.no Git - linux-2.6/blob - arch/powerpc/kernel/pci_64.c
[POWERPC] Merge pcibios_resource_to_bus/bus_to_resource
[linux-2.6] / arch / powerpc / kernel / pci_64.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 #undef DEBUG
15
16 #include <linux/kernel.h>
17 #include <linux/pci.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/bootmem.h>
21 #include <linux/mm.h>
22 #include <linux/list.h>
23 #include <linux/syscalls.h>
24 #include <linux/irq.h>
25 #include <linux/vmalloc.h>
26
27 #include <asm/processor.h>
28 #include <asm/io.h>
29 #include <asm/prom.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/byteorder.h>
32 #include <asm/machdep.h>
33 #include <asm/ppc-pci.h>
34
35 #ifdef DEBUG
36 #include <asm/udbg.h>
37 #define DBG(fmt...) printk(fmt)
38 #else
39 #define DBG(fmt...)
40 #endif
41
42 unsigned long pci_probe_only = 1;
43
44 static void fixup_resource(struct resource *res, struct pci_dev *dev);
45 static void do_bus_setup(struct pci_bus *bus);
46
47 /* pci_io_base -- the base address from which io bars are offsets.
48  * This is the lowest I/O base address (so bar values are always positive),
49  * and it *must* be the start of ISA space if an ISA bus exists because
50  * ISA drivers use hard coded offsets.  If no ISA bus exists nothing
51  * is mapped on the first 64K of IO space
52  */
53 unsigned long pci_io_base = ISA_IO_BASE;
54 EXPORT_SYMBOL(pci_io_base);
55
56 LIST_HEAD(hose_list);
57
58 static struct dma_mapping_ops *pci_dma_ops;
59
60 void set_pci_dma_ops(struct dma_mapping_ops *dma_ops)
61 {
62         pci_dma_ops = dma_ops;
63 }
64
65 struct dma_mapping_ops *get_pci_dma_ops(void)
66 {
67         return pci_dma_ops;
68 }
69 EXPORT_SYMBOL(get_pci_dma_ops);
70
71
72 int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
73 {
74         return dma_set_mask(&dev->dev, mask);
75 }
76
77 int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
78 {
79         int rc;
80
81         rc = dma_set_mask(&dev->dev, mask);
82         dev->dev.coherent_dma_mask = dev->dma_mask;
83
84         return rc;
85 }
86
87 static void fixup_broken_pcnet32(struct pci_dev* dev)
88 {
89         if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
90                 dev->vendor = PCI_VENDOR_ID_AMD;
91                 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
92         }
93 }
94 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
95
96
97 /*
98  * We need to avoid collisions with `mirrored' VGA ports
99  * and other strange ISA hardware, so we always want the
100  * addresses to be allocated in the 0x000-0x0ff region
101  * modulo 0x400.
102  *
103  * Why? Because some silly external IO cards only decode
104  * the low 10 bits of the IO address. The 0x00-0xff region
105  * is reserved for motherboard devices that decode all 16
106  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
107  * but we want to try to avoid allocating at 0x2900-0x2bff
108  * which might have be mirrored at 0x0100-0x03ff..
109  */
110 void pcibios_align_resource(void *data, struct resource *res,
111                             resource_size_t size, resource_size_t align)
112 {
113         struct pci_dev *dev = data;
114         struct pci_controller *hose = pci_bus_to_host(dev->bus);
115         resource_size_t start = res->start;
116         unsigned long alignto;
117
118         if (res->flags & IORESOURCE_IO) {
119                 unsigned long offset = (unsigned long)hose->io_base_virt -
120                                         _IO_BASE;
121                 /* Make sure we start at our min on all hoses */
122                 if (start - offset < PCIBIOS_MIN_IO)
123                         start = PCIBIOS_MIN_IO + offset;
124
125                 /*
126                  * Put everything into 0x00-0xff region modulo 0x400
127                  */
128                 if (start & 0x300)
129                         start = (start + 0x3ff) & ~0x3ff;
130
131         } else if (res->flags & IORESOURCE_MEM) {
132                 /* Make sure we start at our min on all hoses */
133                 if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
134                         start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
135
136                 /* Align to multiple of size of minimum base.  */
137                 alignto = max(0x1000UL, align);
138                 start = ALIGN(start, alignto);
139         }
140
141         res->start = start;
142 }
143
144 void __devinit pcibios_claim_one_bus(struct pci_bus *b)
145 {
146         struct pci_dev *dev;
147         struct pci_bus *child_bus;
148
149         list_for_each_entry(dev, &b->devices, bus_list) {
150                 int i;
151
152                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
153                         struct resource *r = &dev->resource[i];
154
155                         if (r->parent || !r->start || !r->flags)
156                                 continue;
157                         pci_claim_resource(dev, i);
158                 }
159         }
160
161         list_for_each_entry(child_bus, &b->children, node)
162                 pcibios_claim_one_bus(child_bus);
163 }
164 #ifdef CONFIG_HOTPLUG
165 EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
166 #endif
167
168 static void __init pcibios_claim_of_setup(void)
169 {
170         struct pci_bus *b;
171
172         list_for_each_entry(b, &pci_root_buses, node)
173                 pcibios_claim_one_bus(b);
174 }
175
176 static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
177 {
178         const u32 *prop;
179         int len;
180
181         prop = of_get_property(np, name, &len);
182         if (prop && len >= 4)
183                 return *prop;
184         return def;
185 }
186
187 static unsigned int pci_parse_of_flags(u32 addr0)
188 {
189         unsigned int flags = 0;
190
191         if (addr0 & 0x02000000) {
192                 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
193                 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
194                 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
195                 if (addr0 & 0x40000000)
196                         flags |= IORESOURCE_PREFETCH
197                                  | PCI_BASE_ADDRESS_MEM_PREFETCH;
198         } else if (addr0 & 0x01000000)
199                 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
200         return flags;
201 }
202
203
204 static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
205 {
206         u64 base, size;
207         unsigned int flags;
208         struct resource *res;
209         const u32 *addrs;
210         u32 i;
211         int proplen;
212
213         addrs = of_get_property(node, "assigned-addresses", &proplen);
214         if (!addrs)
215                 return;
216         DBG("    parse addresses (%d bytes) @ %p\n", proplen, addrs);
217         for (; proplen >= 20; proplen -= 20, addrs += 5) {
218                 flags = pci_parse_of_flags(addrs[0]);
219                 if (!flags)
220                         continue;
221                 base = of_read_number(&addrs[1], 2);
222                 size = of_read_number(&addrs[3], 2);
223                 if (!size)
224                         continue;
225                 i = addrs[0] & 0xff;
226                 DBG("  base: %llx, size: %llx, i: %x\n",
227                     (unsigned long long)base, (unsigned long long)size, i);
228
229                 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
230                         res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
231                 } else if (i == dev->rom_base_reg) {
232                         res = &dev->resource[PCI_ROM_RESOURCE];
233                         flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
234                 } else {
235                         printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
236                         continue;
237                 }
238                 res->start = base;
239                 res->end = base + size - 1;
240                 res->flags = flags;
241                 res->name = pci_name(dev);
242                 fixup_resource(res, dev);
243         }
244 }
245
246 struct pci_dev *of_create_pci_dev(struct device_node *node,
247                                  struct pci_bus *bus, int devfn)
248 {
249         struct pci_dev *dev;
250         const char *type;
251
252         dev = alloc_pci_dev();
253         if (!dev)
254                 return NULL;
255         type = of_get_property(node, "device_type", NULL);
256         if (type == NULL)
257                 type = "";
258
259         DBG("    create device, devfn: %x, type: %s\n", devfn, type);
260
261         dev->bus = bus;
262         dev->sysdata = node;
263         dev->dev.parent = bus->bridge;
264         dev->dev.bus = &pci_bus_type;
265         dev->devfn = devfn;
266         dev->multifunction = 0;         /* maybe a lie? */
267
268         dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
269         dev->device = get_int_prop(node, "device-id", 0xffff);
270         dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
271         dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
272
273         dev->cfg_size = pci_cfg_space_size(dev);
274
275         sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
276                 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
277         dev->class = get_int_prop(node, "class-code", 0);
278         dev->revision = get_int_prop(node, "revision-id", 0);
279
280         DBG("    class: 0x%x\n", dev->class);
281         DBG("    revision: 0x%x\n", dev->revision);
282
283         dev->current_state = 4;         /* unknown power state */
284         dev->error_state = pci_channel_io_normal;
285         dev->dma_mask = 0xffffffff;
286
287         if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
288                 /* a PCI-PCI bridge */
289                 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
290                 dev->rom_base_reg = PCI_ROM_ADDRESS1;
291         } else if (!strcmp(type, "cardbus")) {
292                 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
293         } else {
294                 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
295                 dev->rom_base_reg = PCI_ROM_ADDRESS;
296                 /* Maybe do a default OF mapping here */
297                 dev->irq = NO_IRQ;
298         }
299
300         pci_parse_of_addrs(node, dev);
301
302         DBG("    adding to system ...\n");
303
304         pci_device_add(dev, bus);
305
306         return dev;
307 }
308 EXPORT_SYMBOL(of_create_pci_dev);
309
310 void __devinit of_scan_bus(struct device_node *node,
311                                   struct pci_bus *bus)
312 {
313         struct device_node *child = NULL;
314         const u32 *reg;
315         int reglen, devfn;
316         struct pci_dev *dev;
317
318         DBG("of_scan_bus(%s) bus no %d... \n", node->full_name, bus->number);
319
320         while ((child = of_get_next_child(node, child)) != NULL) {
321                 DBG("  * %s\n", child->full_name);
322                 reg = of_get_property(child, "reg", &reglen);
323                 if (reg == NULL || reglen < 20)
324                         continue;
325                 devfn = (reg[0] >> 8) & 0xff;
326
327                 /* create a new pci_dev for this device */
328                 dev = of_create_pci_dev(child, bus, devfn);
329                 if (!dev)
330                         continue;
331                 DBG("dev header type: %x\n", dev->hdr_type);
332
333                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
334                     dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
335                         of_scan_pci_bridge(child, dev);
336         }
337
338         do_bus_setup(bus);
339 }
340 EXPORT_SYMBOL(of_scan_bus);
341
342 void __devinit of_scan_pci_bridge(struct device_node *node,
343                                 struct pci_dev *dev)
344 {
345         struct pci_bus *bus;
346         const u32 *busrange, *ranges;
347         int len, i, mode;
348         struct resource *res;
349         unsigned int flags;
350         u64 size;
351
352         DBG("of_scan_pci_bridge(%s)\n", node->full_name);
353
354         /* parse bus-range property */
355         busrange = of_get_property(node, "bus-range", &len);
356         if (busrange == NULL || len != 8) {
357                 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
358                        node->full_name);
359                 return;
360         }
361         ranges = of_get_property(node, "ranges", &len);
362         if (ranges == NULL) {
363                 printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
364                        node->full_name);
365                 return;
366         }
367
368         bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
369         if (!bus) {
370                 printk(KERN_ERR "Failed to create pci bus for %s\n",
371                        node->full_name);
372                 return;
373         }
374
375         bus->primary = dev->bus->number;
376         bus->subordinate = busrange[1];
377         bus->bridge_ctl = 0;
378         bus->sysdata = node;
379
380         /* parse ranges property */
381         /* PCI #address-cells == 3 and #size-cells == 2 always */
382         res = &dev->resource[PCI_BRIDGE_RESOURCES];
383         for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
384                 res->flags = 0;
385                 bus->resource[i] = res;
386                 ++res;
387         }
388         i = 1;
389         for (; len >= 32; len -= 32, ranges += 8) {
390                 flags = pci_parse_of_flags(ranges[0]);
391                 size = of_read_number(&ranges[6], 2);
392                 if (flags == 0 || size == 0)
393                         continue;
394                 if (flags & IORESOURCE_IO) {
395                         res = bus->resource[0];
396                         if (res->flags) {
397                                 printk(KERN_ERR "PCI: ignoring extra I/O range"
398                                        " for bridge %s\n", node->full_name);
399                                 continue;
400                         }
401                 } else {
402                         if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
403                                 printk(KERN_ERR "PCI: too many memory ranges"
404                                        " for bridge %s\n", node->full_name);
405                                 continue;
406                         }
407                         res = bus->resource[i];
408                         ++i;
409                 }
410                 res->start = of_read_number(&ranges[1], 2);
411                 res->end = res->start + size - 1;
412                 res->flags = flags;
413                 fixup_resource(res, dev);
414         }
415         sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
416                 bus->number);
417         DBG("    bus name: %s\n", bus->name);
418
419         mode = PCI_PROBE_NORMAL;
420         if (ppc_md.pci_probe_mode)
421                 mode = ppc_md.pci_probe_mode(bus);
422         DBG("    probe mode: %d\n", mode);
423
424         if (mode == PCI_PROBE_DEVTREE)
425                 of_scan_bus(node, bus);
426         else if (mode == PCI_PROBE_NORMAL)
427                 pci_scan_child_bus(bus);
428 }
429 EXPORT_SYMBOL(of_scan_pci_bridge);
430
431 void __devinit scan_phb(struct pci_controller *hose)
432 {
433         struct pci_bus *bus;
434         struct device_node *node = hose->dn;
435         int i, mode;
436         struct resource *res;
437
438         DBG("Scanning PHB %s\n", node ? node->full_name : "<NO NAME>");
439
440         bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, node);
441         if (bus == NULL) {
442                 printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
443                        hose->global_number);
444                 return;
445         }
446         bus->secondary = hose->first_busno;
447         hose->bus = bus;
448
449         pcibios_map_io_space(bus);
450
451         bus->resource[0] = res = &hose->io_resource;
452         if (res->flags && request_resource(&ioport_resource, res)) {
453                 printk(KERN_ERR "Failed to request PCI IO region "
454                        "on PCI domain %04x\n", hose->global_number);
455                 DBG("res->start = 0x%016lx, res->end = 0x%016lx\n",
456                     res->start, res->end);
457         }
458
459         for (i = 0; i < 3; ++i) {
460                 res = &hose->mem_resources[i];
461                 bus->resource[i+1] = res;
462                 if (res->flags && request_resource(&iomem_resource, res))
463                         printk(KERN_ERR "Failed to request PCI memory region "
464                                "on PCI domain %04x\n", hose->global_number);
465         }
466
467         mode = PCI_PROBE_NORMAL;
468
469         if (node && ppc_md.pci_probe_mode)
470                 mode = ppc_md.pci_probe_mode(bus);
471         DBG("    probe mode: %d\n", mode);
472         if (mode == PCI_PROBE_DEVTREE) {
473                 bus->subordinate = hose->last_busno;
474                 of_scan_bus(node, bus);
475         }
476
477         if (mode == PCI_PROBE_NORMAL)
478                 hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
479 }
480
481 static int __init pcibios_init(void)
482 {
483         struct pci_controller *hose, *tmp;
484
485         /* For now, override phys_mem_access_prot. If we need it,
486          * later, we may move that initialization to each ppc_md
487          */
488         ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
489
490         printk(KERN_DEBUG "PCI: Probing PCI hardware\n");
491
492         /* Scan all of the recorded PCI controllers.  */
493         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
494                 scan_phb(hose);
495                 pci_bus_add_devices(hose->bus);
496         }
497
498         if (pci_probe_only)
499                 pcibios_claim_of_setup();
500         else
501                 /* FIXME: `else' will be removed when
502                    pci_assign_unassigned_resources() is able to work
503                    correctly with [partially] allocated PCI tree. */
504                 pci_assign_unassigned_resources();
505
506         /* Call machine dependent final fixup */
507         if (ppc_md.pcibios_fixup)
508                 ppc_md.pcibios_fixup();
509
510         printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
511
512         return 0;
513 }
514
515 subsys_initcall(pcibios_init);
516
517 int pcibios_enable_device(struct pci_dev *dev, int mask)
518 {
519         u16 cmd, oldcmd;
520         int i;
521
522         pci_read_config_word(dev, PCI_COMMAND, &cmd);
523         oldcmd = cmd;
524
525         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
526                 struct resource *res = &dev->resource[i];
527
528                 /* Only set up the requested stuff */
529                 if (!(mask & (1<<i)))
530                         continue;
531
532                 if (res->flags & IORESOURCE_IO)
533                         cmd |= PCI_COMMAND_IO;
534                 if (res->flags & IORESOURCE_MEM)
535                         cmd |= PCI_COMMAND_MEMORY;
536         }
537
538         if (cmd != oldcmd) {
539                 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
540                        pci_name(dev), cmd);
541                 /* Enable the appropriate bits in the PCI command register.  */
542                 pci_write_config_word(dev, PCI_COMMAND, cmd);
543         }
544         return 0;
545 }
546
547 #ifdef CONFIG_HOTPLUG
548
549 int pcibios_unmap_io_space(struct pci_bus *bus)
550 {
551         struct pci_controller *hose;
552
553         WARN_ON(bus == NULL);
554
555         /* If this is not a PHB, we only flush the hash table over
556          * the area mapped by this bridge. We don't play with the PTE
557          * mappings since we might have to deal with sub-page alignemnts
558          * so flushing the hash table is the only sane way to make sure
559          * that no hash entries are covering that removed bridge area
560          * while still allowing other busses overlapping those pages
561          */
562         if (bus->self) {
563                 struct resource *res = bus->resource[0];
564
565                 DBG("IO unmapping for PCI-PCI bridge %s\n",
566                     pci_name(bus->self));
567
568                 __flush_hash_table_range(&init_mm, res->start + _IO_BASE,
569                                          res->end - res->start + 1);
570                 return 0;
571         }
572
573         /* Get the host bridge */
574         hose = pci_bus_to_host(bus);
575
576         /* Check if we have IOs allocated */
577         if (hose->io_base_alloc == 0)
578                 return 0;
579
580         DBG("IO unmapping for PHB %s\n", hose->dn->full_name);
581         DBG("  alloc=0x%p\n", hose->io_base_alloc);
582
583         /* This is a PHB, we fully unmap the IO area */
584         vunmap(hose->io_base_alloc);
585
586         return 0;
587 }
588 EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
589
590 #endif /* CONFIG_HOTPLUG */
591
592 int __devinit pcibios_map_io_space(struct pci_bus *bus)
593 {
594         struct vm_struct *area;
595         unsigned long phys_page;
596         unsigned long size_page;
597         unsigned long io_virt_offset;
598         struct pci_controller *hose;
599
600         WARN_ON(bus == NULL);
601
602         /* If this not a PHB, nothing to do, page tables still exist and
603          * thus HPTEs will be faulted in when needed
604          */
605         if (bus->self) {
606                 DBG("IO mapping for PCI-PCI bridge %s\n",
607                     pci_name(bus->self));
608                 DBG("  virt=0x%016lx...0x%016lx\n",
609                     bus->resource[0]->start + _IO_BASE,
610                     bus->resource[0]->end + _IO_BASE);
611                 return 0;
612         }
613
614         /* Get the host bridge */
615         hose = pci_bus_to_host(bus);
616         phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
617         size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE);
618
619         /* Make sure IO area address is clear */
620         hose->io_base_alloc = NULL;
621
622         /* If there's no IO to map on that bus, get away too */
623         if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
624                 return 0;
625
626         /* Let's allocate some IO space for that guy. We don't pass
627          * VM_IOREMAP because we don't care about alignment tricks that
628          * the core does in that case. Maybe we should due to stupid card
629          * with incomplete address decoding but I'd rather not deal with
630          * those outside of the reserved 64K legacy region.
631          */
632         area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
633         if (area == NULL)
634                 return -ENOMEM;
635         hose->io_base_alloc = area->addr;
636         hose->io_base_virt = (void __iomem *)(area->addr +
637                                               hose->io_base_phys - phys_page);
638
639         DBG("IO mapping for PHB %s\n", hose->dn->full_name);
640         DBG("  phys=0x%016lx, virt=0x%p (alloc=0x%p)\n",
641             hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
642         DBG("  size=0x%016lx (alloc=0x%016lx)\n",
643             hose->pci_io_size, size_page);
644
645         /* Establish the mapping */
646         if (__ioremap_at(phys_page, area->addr, size_page,
647                          _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)
648                 return -ENOMEM;
649
650         /* Fixup hose IO resource */
651         io_virt_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
652         hose->io_resource.start += io_virt_offset;
653         hose->io_resource.end += io_virt_offset;
654
655         DBG("  hose->io_resource=0x%016lx...0x%016lx\n",
656             hose->io_resource.start, hose->io_resource.end);
657
658         return 0;
659 }
660 EXPORT_SYMBOL_GPL(pcibios_map_io_space);
661
662 static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
663 {
664         struct pci_controller *hose = pci_bus_to_host(dev->bus);
665         unsigned long offset;
666
667         if (res->flags & IORESOURCE_IO) {
668                 offset = (unsigned long)hose->io_base_virt - _IO_BASE;
669                 res->start += offset;
670                 res->end += offset;
671         } else if (res->flags & IORESOURCE_MEM) {
672                 res->start += hose->pci_mem_offset;
673                 res->end += hose->pci_mem_offset;
674         }
675 }
676
677 void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
678                                               struct pci_bus *bus)
679 {
680         /* Update device resources.  */
681         int i;
682
683         DBG("%s: Fixup resources:\n", pci_name(dev));
684         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
685                 struct resource *res = &dev->resource[i];
686                 if (!res->flags)
687                         continue;
688
689                 DBG("  0x%02x < %08lx:0x%016lx...0x%016lx\n",
690                     i, res->flags, res->start, res->end);
691
692                 fixup_resource(res, dev);
693
694                 DBG("       > %08lx:0x%016lx...0x%016lx\n",
695                     res->flags, res->start, res->end);
696         }
697 }
698 EXPORT_SYMBOL(pcibios_fixup_device_resources);
699
700 void __devinit pcibios_setup_new_device(struct pci_dev *dev)
701 {
702         struct dev_archdata *sd = &dev->dev.archdata;
703
704         sd->of_node = pci_device_to_OF_node(dev);
705
706         DBG("PCI device %s OF node: %s\n", pci_name(dev),
707             sd->of_node ? sd->of_node->full_name : "<none>");
708
709         sd->dma_ops = pci_dma_ops;
710 #ifdef CONFIG_NUMA
711         sd->numa_node = pcibus_to_node(dev->bus);
712 #else
713         sd->numa_node = -1;
714 #endif
715         if (ppc_md.pci_dma_dev_setup)
716                 ppc_md.pci_dma_dev_setup(dev);
717 }
718 EXPORT_SYMBOL(pcibios_setup_new_device);
719
720 static void __devinit do_bus_setup(struct pci_bus *bus)
721 {
722         struct pci_dev *dev;
723
724         if (ppc_md.pci_dma_bus_setup)
725                 ppc_md.pci_dma_bus_setup(bus);
726
727         list_for_each_entry(dev, &bus->devices, bus_list)
728                 pcibios_setup_new_device(dev);
729
730         /* Read default IRQs and fixup if necessary */
731         list_for_each_entry(dev, &bus->devices, bus_list) {
732                 pci_read_irq_line(dev);
733                 if (ppc_md.pci_irq_fixup)
734                         ppc_md.pci_irq_fixup(dev);
735         }
736 }
737
738 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
739 {
740         struct pci_dev *dev = bus->self;
741         struct device_node *np;
742
743         np = pci_bus_to_OF_node(bus);
744
745         DBG("pcibios_fixup_bus(%s)\n", np ? np->full_name : "<???>");
746
747         if (dev && pci_probe_only &&
748             (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
749                 /* This is a subordinate bridge */
750
751                 pci_read_bridge_bases(bus);
752                 pcibios_fixup_device_resources(dev, bus);
753         }
754
755         do_bus_setup(bus);
756
757         if (!pci_probe_only)
758                 return;
759
760         list_for_each_entry(dev, &bus->devices, bus_list)
761                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
762                         pcibios_fixup_device_resources(dev, bus);
763 }
764 EXPORT_SYMBOL(pcibios_fixup_bus);
765
766 unsigned long pci_address_to_pio(phys_addr_t address)
767 {
768         struct pci_controller *hose, *tmp;
769
770         list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
771                 if (address >= hose->io_base_phys &&
772                     address < (hose->io_base_phys + hose->pci_io_size)) {
773                         unsigned long base =
774                                 (unsigned long)hose->io_base_virt - _IO_BASE;
775                         return base + (address - hose->io_base_phys);
776                 }
777         }
778         return (unsigned int)-1;
779 }
780 EXPORT_SYMBOL_GPL(pci_address_to_pio);
781
782
783 #define IOBASE_BRIDGE_NUMBER    0
784 #define IOBASE_MEMORY           1
785 #define IOBASE_IO               2
786 #define IOBASE_ISA_IO           3
787 #define IOBASE_ISA_MEM          4
788
789 long sys_pciconfig_iobase(long which, unsigned long in_bus,
790                           unsigned long in_devfn)
791 {
792         struct pci_controller* hose;
793         struct list_head *ln;
794         struct pci_bus *bus = NULL;
795         struct device_node *hose_node;
796
797         /* Argh ! Please forgive me for that hack, but that's the
798          * simplest way to get existing XFree to not lockup on some
799          * G5 machines... So when something asks for bus 0 io base
800          * (bus 0 is HT root), we return the AGP one instead.
801          */
802         if (machine_is_compatible("MacRISC4"))
803                 if (in_bus == 0)
804                         in_bus = 0xf0;
805
806         /* That syscall isn't quite compatible with PCI domains, but it's
807          * used on pre-domains setup. We return the first match
808          */
809
810         for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
811                 bus = pci_bus_b(ln);
812                 if (in_bus >= bus->number && in_bus <= bus->subordinate)
813                         break;
814                 bus = NULL;
815         }
816         if (bus == NULL || bus->sysdata == NULL)
817                 return -ENODEV;
818
819         hose_node = (struct device_node *)bus->sysdata;
820         hose = PCI_DN(hose_node)->phb;
821
822         switch (which) {
823         case IOBASE_BRIDGE_NUMBER:
824                 return (long)hose->first_busno;
825         case IOBASE_MEMORY:
826                 return (long)hose->pci_mem_offset;
827         case IOBASE_IO:
828                 return (long)hose->io_base_phys;
829         case IOBASE_ISA_IO:
830                 return (long)isa_io_base;
831         case IOBASE_ISA_MEM:
832                 return -EINVAL;
833         }
834
835         return -EOPNOTSUPP;
836 }
837
838 #ifdef CONFIG_NUMA
839 int pcibus_to_node(struct pci_bus *bus)
840 {
841         struct pci_controller *phb = pci_bus_to_host(bus);
842         return phb->node;
843 }
844 EXPORT_SYMBOL(pcibus_to_node);
845 #endif