ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / kernel / pci.c
1 /*
2  * Common pmac/prep/chrp pci routines. -- Cort
3  */
4
5 #include <linux/config.h>
6 #include <linux/kernel.h>
7 #include <linux/pci.h>
8 #include <linux/delay.h>
9 #include <linux/string.h>
10 #include <linux/init.h>
11 #include <linux/capability.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/bootmem.h>
15
16 #include <asm/processor.h>
17 #include <asm/io.h>
18 #include <asm/prom.h>
19 #include <asm/sections.h>
20 #include <asm/pci-bridge.h>
21 #include <asm/byteorder.h>
22 #include <asm/irq.h>
23 #include <asm/uaccess.h>
24
25 #undef DEBUG
26
27 #ifdef DEBUG
28 #define DBG(x...) printk(x)
29 #else
30 #define DBG(x...)
31 #endif
32
33 unsigned long isa_io_base     = 0;
34 unsigned long isa_mem_base    = 0;
35 unsigned long pci_dram_offset = 0;
36
37 void pcibios_make_OF_bus_map(void);
38
39 static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
40 static int probe_resource(struct pci_bus *parent, struct resource *pr,
41                           struct resource *res, struct resource **conflict);
42 static void update_bridge_base(struct pci_bus *bus, int i);
43 static void pcibios_fixup_resources(struct pci_dev* dev);
44 static void fixup_broken_pcnet32(struct pci_dev* dev);
45 static int reparent_resources(struct resource *parent, struct resource *res);
46 static void fixup_rev1_53c810(struct pci_dev* dev);
47 static void fixup_cpc710_pci64(struct pci_dev* dev);
48 #ifdef CONFIG_PPC_PMAC
49 extern void pmac_pci_fixup_cardbus(struct pci_dev* dev);
50 extern void pmac_pci_fixup_pciata(struct pci_dev* dev);
51 extern void pmac_pci_fixup_k2_sata(struct pci_dev* dev);
52 #endif
53 #ifdef CONFIG_PPC_OF
54 static u8* pci_to_OF_bus_map;
55 #endif
56
57 /* By default, we don't re-assign bus numbers. We do this only on
58  * some pmacs
59  */
60 int pci_assign_all_busses;
61
62 struct pci_controller* hose_head;
63 struct pci_controller** hose_tail = &hose_head;
64
65 static int pci_bus_count;
66
67 struct pci_fixup pcibios_fixups[] = {
68         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_TRIDENT,  PCI_ANY_ID,                     fixup_broken_pcnet32 },
69         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_NCR,      PCI_DEVICE_ID_NCR_53C810,       fixup_rev1_53c810 },
70         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_IBM,      PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64},
71         { PCI_FIXUP_HEADER,     PCI_ANY_ID,             PCI_ANY_ID,                     pcibios_fixup_resources },
72 #ifdef CONFIG_PPC_PMAC
73         /* We should add per-machine fixup support in xxx_setup.c or xxx_pci.c */
74         { PCI_FIXUP_FINAL,      PCI_VENDOR_ID_TI,       PCI_ANY_ID,                     pmac_pci_fixup_cardbus },
75         { PCI_FIXUP_FINAL,      PCI_ANY_ID,             PCI_ANY_ID,                     pmac_pci_fixup_pciata },
76         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_SERVERWORKS, 0x0240,                      pmac_pci_fixup_k2_sata },
77 #endif /* CONFIG_PPC_PMAC */
78         { 0 }
79 };
80
81 static void
82 fixup_rev1_53c810(struct pci_dev* dev)
83 {
84         /* rev 1 ncr53c810 chips don't set the class at all which means
85          * they don't get their resources remapped. Fix that here.
86          */
87
88         if ((dev->class == PCI_CLASS_NOT_DEFINED)) {
89                 printk("NCR 53c810 rev 1 detected, setting PCI class.\n");
90                 dev->class = PCI_CLASS_STORAGE_SCSI;
91         }
92 }
93
94 static void
95 fixup_broken_pcnet32(struct pci_dev* dev)
96 {
97         if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
98                 dev->vendor = PCI_VENDOR_ID_AMD;
99                 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
100                 pci_name_device(dev);
101         }
102 }
103
104 static void
105 fixup_cpc710_pci64(struct pci_dev* dev)
106 {
107         /* Hide the PCI64 BARs from the kernel as their content doesn't
108          * fit well in the resource management
109          */
110         dev->resource[0].start = dev->resource[0].end = 0;
111         dev->resource[0].flags = 0;
112         dev->resource[1].start = dev->resource[1].end = 0;
113         dev->resource[1].flags = 0;
114 }
115
116 static void
117 pcibios_fixup_resources(struct pci_dev *dev)
118 {
119         struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
120         int i;
121         unsigned long offset;
122
123         if (!hose) {
124                 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
125                 return;
126         }
127         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
128                 struct resource *res = dev->resource + i;
129                 if (!res->flags)
130                         continue;
131                 if (!res->start || res->end == 0xffffffff) {
132                         DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
133                             pci_name(dev), i, res->start, res->end);
134                         res->end -= res->start;
135                         res->start = 0;
136                         res->flags |= IORESOURCE_UNSET;
137                         continue;
138                 }
139                 offset = 0;
140                 if (res->flags & IORESOURCE_MEM) {
141                         offset = hose->pci_mem_offset;
142                 } else if (res->flags & IORESOURCE_IO) {
143                         offset = (unsigned long) hose->io_base_virt
144                                 - isa_io_base;
145                 }
146                 if (offset != 0) {
147                         res->start += offset;
148                         res->end += offset;
149 #ifdef DEBUG
150                         printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
151                                i, res->flags, pci_name(dev),
152                                res->start - offset, res->start);
153 #endif
154                 }
155         }
156
157         /* Call machine specific resource fixup */
158         if (ppc_md.pcibios_fixup_resources)
159                 ppc_md.pcibios_fixup_resources(dev);
160 }
161
162 void
163 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
164                         struct resource *res)
165 {
166         unsigned long offset = 0;
167         struct pci_controller *hose = dev->sysdata;
168
169         if (hose && res->flags & IORESOURCE_IO)
170                 offset = (unsigned long)hose->io_base_virt - isa_io_base;
171         else if (hose && res->flags & IORESOURCE_MEM)
172                 offset = hose->pci_mem_offset;
173         region->start = res->start - offset;
174         region->end = res->end - offset;
175 }
176
177 /*
178  * We need to avoid collisions with `mirrored' VGA ports
179  * and other strange ISA hardware, so we always want the
180  * addresses to be allocated in the 0x000-0x0ff region
181  * modulo 0x400.
182  *
183  * Why? Because some silly external IO cards only decode
184  * the low 10 bits of the IO address. The 0x00-0xff region
185  * is reserved for motherboard devices that decode all 16
186  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
187  * but we want to try to avoid allocating at 0x2900-0x2bff
188  * which might have be mirrored at 0x0100-0x03ff..
189  */
190 void
191 pcibios_align_resource(void *data, struct resource *res, unsigned long size,
192                        unsigned long align)
193 {
194         struct pci_dev *dev = data;
195
196         if (res->flags & IORESOURCE_IO) {
197                 unsigned long start = res->start;
198
199                 if (size > 0x100) {
200                         printk(KERN_ERR "PCI: I/O Region %s/%d too large"
201                                " (%ld bytes)\n", pci_name(dev),
202                                dev->resource - res, size);
203                 }
204
205                 if (start & 0x300) {
206                         start = (start + 0x3ff) & ~0x3ff;
207                         res->start = start;
208                 }
209         }
210 }
211
212
213 /*
214  *  Handle resources of PCI devices.  If the world were perfect, we could
215  *  just allocate all the resource regions and do nothing more.  It isn't.
216  *  On the other hand, we cannot just re-allocate all devices, as it would
217  *  require us to know lots of host bridge internals.  So we attempt to
218  *  keep as much of the original configuration as possible, but tweak it
219  *  when it's found to be wrong.
220  *
221  *  Known BIOS problems we have to work around:
222  *      - I/O or memory regions not configured
223  *      - regions configured, but not enabled in the command register
224  *      - bogus I/O addresses above 64K used
225  *      - expansion ROMs left enabled (this may sound harmless, but given
226  *        the fact the PCI specs explicitly allow address decoders to be
227  *        shared between expansion ROMs and other resource regions, it's
228  *        at least dangerous)
229  *
230  *  Our solution:
231  *      (1) Allocate resources for all buses behind PCI-to-PCI bridges.
232  *          This gives us fixed barriers on where we can allocate.
233  *      (2) Allocate resources for all enabled devices.  If there is
234  *          a collision, just mark the resource as unallocated. Also
235  *          disable expansion ROMs during this step.
236  *      (3) Try to allocate resources for disabled devices.  If the
237  *          resources were assigned correctly, everything goes well,
238  *          if they weren't, they won't disturb allocation of other
239  *          resources.
240  *      (4) Assign new addresses to resources which were either
241  *          not configured at all or misconfigured.  If explicitly
242  *          requested by the user, configure expansion ROM address
243  *          as well.
244  */
245
246 static void __init
247 pcibios_allocate_bus_resources(struct list_head *bus_list)
248 {
249         struct list_head *ln;
250         struct pci_bus *bus;
251         int i;
252         struct resource *res, *pr;
253
254         /* Depth-First Search on bus tree */
255         for (ln = bus_list->next; ln != bus_list; ln=ln->next) {
256                 bus = pci_bus_b(ln);
257                 for (i = 0; i < 4; ++i) {
258                         if ((res = bus->resource[i]) == NULL || !res->flags
259                             || res->start > res->end)
260                                 continue;
261                         if (bus->parent == NULL)
262                                 pr = (res->flags & IORESOURCE_IO)?
263                                         &ioport_resource: &iomem_resource;
264                         else {
265                                 pr = pci_find_parent_resource(bus->self, res);
266                                 if (pr == res) {
267                                         /* this happens when the generic PCI
268                                          * code (wrongly) decides that this
269                                          * bridge is transparent  -- paulus
270                                          */
271                                         continue;
272                                 }
273                         }
274
275                         DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
276                             res->start, res->end, res->flags, pr);
277                         if (pr) {
278                                 if (request_resource(pr, res) == 0)
279                                         continue;
280                                 /*
281                                  * Must be a conflict with an existing entry.
282                                  * Move that entry (or entries) under the
283                                  * bridge resource and try again.
284                                  */
285                                 if (reparent_resources(pr, res) == 0)
286                                         continue;
287                         }
288                         printk(KERN_ERR "PCI: Cannot allocate resource region "
289                                "%d of PCI bridge %d\n", i, bus->number);
290                         if (pci_relocate_bridge_resource(bus, i))
291                                 bus->resource[i] = NULL;
292                 }
293                 pcibios_allocate_bus_resources(&bus->children);
294         }
295 }
296
297 /*
298  * Reparent resource children of pr that conflict with res
299  * under res, and make res replace those children.
300  */
301 static int __init
302 reparent_resources(struct resource *parent, struct resource *res)
303 {
304         struct resource *p, **pp;
305         struct resource **firstpp = NULL;
306
307         for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
308                 if (p->end < res->start)
309                         continue;
310                 if (res->end < p->start)
311                         break;
312                 if (p->start < res->start || p->end > res->end)
313                         return -1;      /* not completely contained */
314                 if (firstpp == NULL)
315                         firstpp = pp;
316         }
317         if (firstpp == NULL)
318                 return -1;      /* didn't find any conflicting entries? */
319         res->parent = parent;
320         res->child = *firstpp;
321         res->sibling = *pp;
322         *firstpp = res;
323         *pp = NULL;
324         for (p = res->child; p != NULL; p = p->sibling) {
325                 p->parent = res;
326                 DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
327                     p->name, p->start, p->end, res->name);
328         }
329         return 0;
330 }
331
332 /*
333  * A bridge has been allocated a range which is outside the range
334  * of its parent bridge, so it needs to be moved.
335  */
336 static int __init
337 pci_relocate_bridge_resource(struct pci_bus *bus, int i)
338 {
339         struct resource *res, *pr, *conflict;
340         unsigned long try, size;
341         int j;
342         struct pci_bus *parent = bus->parent;
343
344         if (parent == NULL) {
345                 /* shouldn't ever happen */
346                 printk(KERN_ERR "PCI: can't move host bridge resource\n");
347                 return -1;
348         }
349         res = bus->resource[i];
350         pr = NULL;
351         for (j = 0; j < 4; j++) {
352                 struct resource *r = parent->resource[j];
353                 if (!r)
354                         continue;
355                 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
356                         continue;
357                 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
358                         pr = r;
359                         break;
360                 }
361                 if (res->flags & IORESOURCE_PREFETCH)
362                         pr = r;
363         }
364         if (pr == NULL)
365                 return -1;
366         size = res->end - res->start;
367         if (pr->start > pr->end || size > pr->end - pr->start)
368                 return -1;
369         try = pr->end;
370         for (;;) {
371                 res->start = try - size;
372                 res->end = try;
373                 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
374                         break;
375                 if (conflict->start <= pr->start + size)
376                         return -1;
377                 try = conflict->start - 1;
378         }
379         if (request_resource(pr, res)) {
380                 DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
381                     res->start, res->end);
382                 return -1;              /* "can't happen" */
383         }
384         update_bridge_base(bus, i);
385         printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
386                bus->number, i, res->start, res->end);
387         return 0;
388 }
389
390 static int __init
391 probe_resource(struct pci_bus *parent, struct resource *pr,
392                struct resource *res, struct resource **conflict)
393 {
394         struct pci_bus *bus;
395         struct pci_dev *dev;
396         struct resource *r;
397         struct list_head *ln;
398         int i;
399
400         for (r = pr->child; r != NULL; r = r->sibling) {
401                 if (r->end >= res->start && res->end >= r->start) {
402                         *conflict = r;
403                         return 1;
404                 }
405         }
406         for (ln = parent->children.next; ln != &parent->children;
407              ln = ln->next) {
408                 bus = pci_bus_b(ln);
409                 for (i = 0; i < 4; ++i) {
410                         if ((r = bus->resource[i]) == NULL)
411                                 continue;
412                         if (!r->flags || r->start > r->end || r == res)
413                                 continue;
414                         if (pci_find_parent_resource(bus->self, r) != pr)
415                                 continue;
416                         if (r->end >= res->start && res->end >= r->start) {
417                                 *conflict = r;
418                                 return 1;
419                         }
420                 }
421         }
422         for (ln = parent->devices.next; ln != &parent->devices; ln=ln->next) {
423                 dev = pci_dev_b(ln);
424                 for (i = 0; i < 6; ++i) {
425                         r = &dev->resource[i];
426                         if (!r->flags || (r->flags & IORESOURCE_UNSET))
427                                 continue;
428                         if (pci_find_parent_resource(bus->self, r) != pr)
429                                 continue;
430                         if (r->end >= res->start && res->end >= r->start) {
431                                 *conflict = r;
432                                 return 1;
433                         }
434                 }
435         }
436         return 0;
437 }
438
439 static void __init
440 update_bridge_base(struct pci_bus *bus, int i)
441 {
442         struct resource *res = bus->resource[i];
443         u8 io_base_lo, io_limit_lo;
444         u16 mem_base, mem_limit;
445         u16 cmd;
446         unsigned long start, end, off;
447         struct pci_dev *dev = bus->self;
448         struct pci_controller *hose = dev->sysdata;
449
450         if (!hose) {
451                 printk("update_bridge_base: no hose?\n");
452                 return;
453         }
454         pci_read_config_word(dev, PCI_COMMAND, &cmd);
455         pci_write_config_word(dev, PCI_COMMAND,
456                               cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
457         if (res->flags & IORESOURCE_IO) {
458                 off = (unsigned long) hose->io_base_virt - isa_io_base;
459                 start = res->start - off;
460                 end = res->end - off;
461                 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
462                 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
463                 if (end > 0xffff) {
464                         pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
465                                               start >> 16);
466                         pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
467                                               end >> 16);
468                         io_base_lo |= PCI_IO_RANGE_TYPE_32;
469                 } else
470                         io_base_lo |= PCI_IO_RANGE_TYPE_16;
471                 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
472                 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
473
474         } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
475                    == IORESOURCE_MEM) {
476                 off = hose->pci_mem_offset;
477                 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
478                 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
479                 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
480                 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
481
482         } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
483                    == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
484                 off = hose->pci_mem_offset;
485                 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
486                 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
487                 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
488                 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
489
490         } else {
491                 DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
492                     pci_name(dev), i, res->flags);
493         }
494         pci_write_config_word(dev, PCI_COMMAND, cmd);
495 }
496
497 static inline void alloc_resource(struct pci_dev *dev, int idx)
498 {
499         struct resource *pr, *r = &dev->resource[idx];
500
501         DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
502             pci_name(dev), idx, r->start, r->end, r->flags);
503         pr = pci_find_parent_resource(dev, r);
504         if (!pr || request_resource(pr, r) < 0) {
505                 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
506                        " of device %s\n", idx, pci_name(dev));
507                 if (pr)
508                         DBG("PCI:  parent is %p: %08lx-%08lx (f=%lx)\n",
509                             pr, pr->start, pr->end, pr->flags);
510                 /* We'll assign a new address later */
511                 r->flags |= IORESOURCE_UNSET;
512                 r->end -= r->start;
513                 r->start = 0;
514         }
515 }
516
517 static void __init
518 pcibios_allocate_resources(int pass)
519 {
520         struct pci_dev *dev = NULL;
521         int idx, disabled;
522         u16 command;
523         struct resource *r;
524
525         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
526                 pci_read_config_word(dev, PCI_COMMAND, &command);
527                 for (idx = 0; idx < 6; idx++) {
528                         r = &dev->resource[idx];
529                         if (r->parent)          /* Already allocated */
530                                 continue;
531                         if (!r->flags || (r->flags & IORESOURCE_UNSET))
532                                 continue;       /* Not assigned at all */
533                         if (r->flags & IORESOURCE_IO)
534                                 disabled = !(command & PCI_COMMAND_IO);
535                         else
536                                 disabled = !(command & PCI_COMMAND_MEMORY);
537                         if (pass == disabled)
538                                 alloc_resource(dev, idx);
539                 }
540                 if (pass)
541                         continue;
542                 r = &dev->resource[PCI_ROM_RESOURCE];
543                 if (r->flags & PCI_ROM_ADDRESS_ENABLE) {
544                         /* Turn the ROM off, leave the resource region, but keep it unregistered. */
545                         u32 reg;
546                         DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
547                         r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
548                         pci_read_config_dword(dev, dev->rom_base_reg, &reg);
549                         pci_write_config_dword(dev, dev->rom_base_reg,
550                                                reg & ~PCI_ROM_ADDRESS_ENABLE);
551                 }
552         }
553 }
554
555 static void __init
556 pcibios_assign_resources(void)
557 {
558         struct pci_dev *dev = NULL;
559         int idx;
560         struct resource *r;
561
562         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
563                 int class = dev->class >> 8;
564
565                 /* Don't touch classless devices and host bridges */
566                 if (!class || class == PCI_CLASS_BRIDGE_HOST)
567                         continue;
568
569                 for (idx = 0; idx < 6; idx++) {
570                         r = &dev->resource[idx];
571
572                         /*
573                          * We shall assign a new address to this resource,
574                          * either because the BIOS (sic) forgot to do so
575                          * or because we have decided the old address was
576                          * unusable for some reason.
577                          */
578                         if ((r->flags & IORESOURCE_UNSET) && r->end &&
579                             (!ppc_md.pcibios_enable_device_hook ||
580                              !ppc_md.pcibios_enable_device_hook(dev, 1))) {
581                                 r->flags &= ~IORESOURCE_UNSET;
582                                 pci_assign_resource(dev, idx);
583                         }
584                 }
585
586 #if 0 /* don't assign ROMs */
587                 r = &dev->resource[PCI_ROM_RESOURCE];
588                 r->end -= r->start;
589                 r->start = 0;
590                 if (r->end)
591                         pci_assign_resource(dev, PCI_ROM_RESOURCE);
592 #endif
593         }
594 }
595
596
597 int
598 pcibios_enable_resources(struct pci_dev *dev, int mask)
599 {
600         u16 cmd, old_cmd;
601         int idx;
602         struct resource *r;
603
604         pci_read_config_word(dev, PCI_COMMAND, &cmd);
605         old_cmd = cmd;
606         for (idx=0; idx<6; idx++) {
607                 /* Only set up the requested stuff */
608                 if (!(mask & (1<<idx)))
609                         continue;
610         
611                 r = &dev->resource[idx];
612                 if (r->flags & IORESOURCE_UNSET) {
613                         printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
614                         return -EINVAL;
615                 }
616                 if (r->flags & IORESOURCE_IO)
617                         cmd |= PCI_COMMAND_IO;
618                 if (r->flags & IORESOURCE_MEM)
619                         cmd |= PCI_COMMAND_MEMORY;
620         }
621         if (dev->resource[PCI_ROM_RESOURCE].start)
622                 cmd |= PCI_COMMAND_MEMORY;
623         if (cmd != old_cmd) {
624                 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
625                 pci_write_config_word(dev, PCI_COMMAND, cmd);
626         }
627         return 0;
628 }
629
630 static int next_controller_index;
631
632 struct pci_controller * __init
633 pcibios_alloc_controller(void)
634 {
635         struct pci_controller *hose;
636
637         hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
638         memset(hose, 0, sizeof(struct pci_controller));
639
640         *hose_tail = hose;
641         hose_tail = &hose->next;
642
643         hose->index = next_controller_index++;
644
645         return hose;
646 }
647
648 #ifdef CONFIG_PPC_OF
649 /*
650  * Functions below are used on OpenFirmware machines.
651  */
652 static void __openfirmware
653 make_one_node_map(struct device_node* node, u8 pci_bus)
654 {
655         int *bus_range;
656         int len;
657
658         if (pci_bus >= pci_bus_count)
659                 return;
660         bus_range = (int *) get_property(node, "bus-range", &len);
661         if (bus_range == NULL || len < 2 * sizeof(int)) {
662                 printk(KERN_WARNING "Can't get bus-range for %s\n",
663                        node->full_name);
664                 return;
665         }
666         pci_to_OF_bus_map[pci_bus] = bus_range[0];
667
668         for (node=node->child; node != 0;node = node->sibling) {
669                 struct pci_dev* dev;
670                 unsigned int *class_code, *reg;
671         
672                 class_code = (unsigned int *) get_property(node, "class-code", 0);
673                 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
674                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
675                         continue;
676                 reg = (unsigned int *)get_property(node, "reg", 0);
677                 if (!reg)
678                         continue;
679                 dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
680                 if (!dev || !dev->subordinate)
681                         continue;
682                 make_one_node_map(node, dev->subordinate->number);
683         }
684 }
685         
686 void __openfirmware
687 pcibios_make_OF_bus_map(void)
688 {
689         int i;
690         struct pci_controller* hose;
691         u8* of_prop_map;
692
693         pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
694         if (!pci_to_OF_bus_map) {
695                 printk(KERN_ERR "Can't allocate OF bus map !\n");
696                 return;
697         }
698
699         /* We fill the bus map with invalid values, that helps
700          * debugging.
701          */
702         for (i=0; i<pci_bus_count; i++)
703                 pci_to_OF_bus_map[i] = 0xff;
704
705         /* For each hose, we begin searching bridges */
706         for(hose=hose_head; hose; hose=hose->next) {
707                 struct device_node* node;       
708                 node = (struct device_node *)hose->arch_data;
709                 if (!node)
710                         continue;
711                 make_one_node_map(node, hose->first_busno);
712         }
713         of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", 0);
714         if (of_prop_map)
715                 memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
716 #ifdef DEBUG
717         printk("PCI->OF bus map:\n");
718         for (i=0; i<pci_bus_count; i++) {
719                 if (pci_to_OF_bus_map[i] == 0xff)
720                         continue;
721                 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
722         }
723 #endif
724 }
725
726 typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
727
728 static struct device_node* __openfirmware
729 scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
730 {
731         struct device_node* sub_node;
732
733         for (; node != 0;node = node->sibling) {
734                 unsigned int *class_code;
735         
736                 if (filter(node, data))
737                         return node;
738
739                 /* For PCI<->PCI bridges or CardBus bridges, we go down
740                  * Note: some OFs create a parent node "multifunc-device" as
741                  * a fake root for all functions of a multi-function device,
742                  * we go down them as well.
743                  */
744                 class_code = (unsigned int *) get_property(node, "class-code", 0);
745                 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
746                         (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
747                         strcmp(node->name, "multifunc-device"))
748                         continue;
749                 sub_node = scan_OF_pci_childs(node->child, filter, data);
750                 if (sub_node)
751                         return sub_node;
752         }
753         return NULL;
754 }
755
756 static int
757 scan_OF_pci_childs_iterator(struct device_node* node, void* data)
758 {
759         unsigned int *reg;
760         u8* fdata = (u8*)data;
761         
762         reg = (unsigned int *) get_property(node, "reg", 0);
763         if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
764                 && ((reg[0] >> 16) & 0xff) == fdata[0])
765                 return 1;
766         return 0;
767 }
768
769 static struct device_node* __openfirmware
770 scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
771 {
772         u8 filter_data[2] = {bus, dev_fn};
773
774         return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
775 }
776
777 /*
778  * Scans the OF tree for a device node matching a PCI device
779  */
780 struct device_node *
781 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
782 {
783         struct pci_controller *hose;
784         struct device_node *node;
785         int busnr;
786
787         if (!have_of)
788                 return NULL;
789         
790         /* Lookup the hose */
791         busnr = bus->number;
792         hose = pci_bus_to_hose(busnr);
793         if (!hose)
794                 return NULL;
795
796         /* Check it has an OF node associated */
797         node = (struct device_node *) hose->arch_data;
798         if (!node)
799                 return NULL;
800
801         /* Fixup bus number according to what OF think it is. */
802 #ifdef CONFIG_PPC_PMAC
803         /* The G5 need a special case here. Basically, we don't remap all
804          * busses on it so we don't create the pci-OF-map. However, we do
805          * remap the AGP bus and so have to deal with it. A future better
806          * fix has to be done by making the remapping per-host and always
807          * filling the pci_to_OF map. --BenH
808          */
809         if (_machine == _MACH_Pmac && busnr >= 0xf0)
810                 busnr -= 0xf0;
811         else
812 #endif
813         if (pci_to_OF_bus_map)
814                 busnr = pci_to_OF_bus_map[busnr];
815         if (busnr == 0xff)
816                 return NULL;
817         
818         /* Now, lookup childs of the hose */
819         return scan_OF_childs_for_device(node->child, busnr, devfn);
820 }
821
822 struct device_node*
823 pci_device_to_OF_node(struct pci_dev *dev)
824 {
825         return pci_busdev_to_OF_node(dev->bus, dev->devfn);
826 }
827
828 /* This routine is meant to be used early during boot, when the
829  * PCI bus numbers have not yet been assigned, and you need to
830  * issue PCI config cycles to an OF device.
831  * It could also be used to "fix" RTAS config cycles if you want
832  * to set pci_assign_all_busses to 1 and still use RTAS for PCI
833  * config cycles.
834  */
835 struct pci_controller*
836 pci_find_hose_for_OF_device(struct device_node* node)
837 {
838         if (!have_of)
839                 return NULL;
840         while(node) {
841                 struct pci_controller* hose;
842                 for (hose=hose_head;hose;hose=hose->next)
843                         if (hose->arch_data == node)
844                                 return hose;
845                 node=node->parent;
846         }
847         return NULL;
848 }
849
850 static int __openfirmware
851 find_OF_pci_device_filter(struct device_node* node, void* data)
852 {
853         return ((void *)node == data);
854 }
855
856 /*
857  * Returns the PCI device matching a given OF node
858  */
859 int
860 pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
861 {
862         unsigned int *reg;
863         struct pci_controller* hose;
864         struct pci_dev* dev = NULL;
865         
866         if (!have_of)
867                 return -ENODEV;
868         /* Make sure it's really a PCI device */
869         hose = pci_find_hose_for_OF_device(node);
870         if (!hose || !hose->arch_data)
871                 return -ENODEV;
872         if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
873                         find_OF_pci_device_filter, (void *)node))
874                 return -ENODEV;
875         reg = (unsigned int *) get_property(node, "reg", 0);
876         if (!reg)
877                 return -ENODEV;
878         *bus = (reg[0] >> 16) & 0xff;
879         *devfn = ((reg[0] >> 8) & 0xff);
880
881         /* Ok, here we need some tweak. If we have already renumbered
882          * all busses, we can't rely on the OF bus number any more.
883          * the pci_to_OF_bus_map is not enough as several PCI busses
884          * may match the same OF bus number.
885          */
886         if (!pci_to_OF_bus_map)
887                 return 0;
888         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
889                 if (pci_to_OF_bus_map[dev->bus->number] != *bus)
890                         continue;
891                 if (dev->devfn != *devfn)
892                         continue;
893                 *bus = dev->bus->number;
894                 return 0;
895         }
896         return -ENODEV;
897 }
898
899 void __init
900 pci_process_bridge_OF_ranges(struct pci_controller *hose,
901                            struct device_node *dev, int primary)
902 {
903         static unsigned int static_lc_ranges[256] __initdata;
904         unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
905         unsigned int size;
906         int rlen = 0, orig_rlen;
907         int memno = 0;
908         struct resource *res;
909         int np, na = prom_n_addr_cells(dev);
910         np = na + 5;
911
912         /* First we try to merge ranges to fix a problem with some pmacs
913          * that can have more than 3 ranges, fortunately using contiguous
914          * addresses -- BenH
915          */
916         dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
917         if (!dt_ranges)
918                 return;
919         /* Sanity check, though hopefully that never happens */
920         if (rlen > sizeof(static_lc_ranges)) {
921                 printk(KERN_WARNING "OF ranges property too large !\n");
922                 rlen = sizeof(static_lc_ranges);
923         }
924         lc_ranges = static_lc_ranges;
925         memcpy(lc_ranges, dt_ranges, rlen);
926         orig_rlen = rlen;
927
928         /* Let's work on a copy of the "ranges" property instead of damaging
929          * the device-tree image in memory
930          */
931         ranges = lc_ranges;
932         prev = NULL;
933         while ((rlen -= np * sizeof(unsigned int)) >= 0) {
934                 if (prev) {
935                         if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
936                                 (prev[2] + prev[na+4]) == ranges[2] &&
937                                 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
938                                 prev[na+4] += ranges[na+4];
939                                 ranges[0] = 0;
940                                 ranges += np;
941                                 continue;
942                         }
943                 }
944                 prev = ranges;
945                 ranges += np;
946         }
947
948         /*
949          * The ranges property is laid out as an array of elements,
950          * each of which comprises:
951          *   cells 0 - 2:       a PCI address
952          *   cells 3 or 3+4:    a CPU physical address
953          *                      (size depending on dev->n_addr_cells)
954          *   cells 4+5 or 5+6:  the size of the range
955          */
956         ranges = lc_ranges;
957         rlen = orig_rlen;
958         while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
959                 res = NULL;
960                 size = ranges[na+4];
961                 switch (ranges[0] >> 24) {
962                 case 1:         /* I/O space */
963                         if (ranges[2] != 0)
964                                 break;
965                         hose->io_base_phys = ranges[na+2];
966                         /* limit I/O space to 16MB */
967                         if (size > 0x01000000)
968                                 size = 0x01000000;
969                         hose->io_base_virt = ioremap(ranges[na+2], size);
970                         if (primary)
971                                 isa_io_base = (unsigned long) hose->io_base_virt;
972                         res = &hose->io_resource;
973                         res->flags = IORESOURCE_IO;
974                         res->start = ranges[2];
975                         break;
976                 case 2:         /* memory space */
977                         memno = 0;
978                         if (ranges[1] == 0 && ranges[2] == 0
979                             && ranges[na+4] <= (16 << 20)) {
980                                 /* 1st 16MB, i.e. ISA memory area */
981                                 if (primary)
982                                         isa_mem_base = ranges[na+2];
983                                 memno = 1;
984                         }
985                         while (memno < 3 && hose->mem_resources[memno].flags)
986                                 ++memno;
987                         if (memno == 0)
988                                 hose->pci_mem_offset = ranges[na+2] - ranges[2];
989                         if (memno < 3) {
990                                 res = &hose->mem_resources[memno];
991                                 res->flags = IORESOURCE_MEM;
992                                 res->start = ranges[na+2];
993                         }
994                         break;
995                 }
996                 if (res != NULL) {
997                         res->name = dev->full_name;
998                         res->end = res->start + size - 1;
999                         res->parent = NULL;
1000                         res->sibling = NULL;
1001                         res->child = NULL;
1002                 }
1003                 ranges += np;
1004         }
1005 }
1006
1007 /* We create the "pci-OF-bus-map" property now so it appears in the
1008  * /proc device tree
1009  */
1010 void __init
1011 pci_create_OF_bus_map(void)
1012 {
1013         struct property* of_prop;
1014         
1015         of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
1016         if (of_prop && find_path_device("/")) {
1017                 memset(of_prop, -1, sizeof(struct property) + 256);
1018                 of_prop->name = "pci-OF-bus-map";
1019                 of_prop->length = 256;
1020                 of_prop->value = (unsigned char *)&of_prop[1];
1021                 prom_add_property(find_path_device("/"), of_prop);
1022         }
1023 }
1024
1025 static ssize_t pci_show_devspec(struct device *dev, char *buf)
1026 {
1027         struct pci_dev *pdev;
1028         struct device_node *np;
1029
1030         pdev = to_pci_dev (dev);
1031         np = pci_device_to_OF_node(pdev);
1032         if (np == NULL || np->full_name == NULL)
1033                 return 0;
1034         return sprintf(buf, "%s", np->full_name);
1035 }
1036 static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
1037
1038 #endif /* CONFIG_PPC_OF */
1039
1040 /* Add sysfs properties */
1041 void pcibios_add_platform_entries(struct pci_dev *pdev)
1042 {
1043 #ifdef CONFIG_PPC_OF
1044         device_create_file(&pdev->dev, &dev_attr_devspec);
1045 #endif /* CONFIG_PPC_OF */
1046 }
1047
1048
1049 #ifdef CONFIG_PPC_PMAC
1050 /*
1051  * This set of routines checks for PCI<->PCI bridges that have closed
1052  * IO resources and have child devices. It tries to re-open an IO
1053  * window on them.
1054  *
1055  * This is a _temporary_ fix to workaround a problem with Apple's OF
1056  * closing IO windows on P2P bridges when the OF drivers of cards
1057  * below this bridge don't claim any IO range (typically ATI or
1058  * Adaptec).
1059  *
1060  * A more complete fix would be to use drivers/pci/setup-bus.c, which
1061  * involves a working pcibios_fixup_pbus_ranges(), some more care about
1062  * ordering when creating the host bus resources, and maybe a few more
1063  * minor tweaks
1064  */
1065
1066 /* Initialize bridges with base/limit values we have collected */
1067 static void __init
1068 do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
1069 {
1070         struct pci_dev *bridge = bus->self;
1071         struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
1072         u32 l;
1073         u16 w;
1074         struct resource res;
1075
1076         res = *(bus->resource[0]);
1077
1078         DBG("Remapping Bus %d, bridge: %s\n", bus->number, bridge->slot_name);
1079         res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
1080         res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
1081         DBG("  IO window: %08lx-%08lx\n", res.start, res.end);
1082
1083         /* Set up the top and bottom of the PCI I/O segment for this bus. */
1084         pci_read_config_dword(bridge, PCI_IO_BASE, &l);
1085         l &= 0xffff000f;
1086         l |= (res.start >> 8) & 0x00f0;
1087         l |= res.end & 0xf000;
1088         pci_write_config_dword(bridge, PCI_IO_BASE, l);
1089
1090         if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
1091                 l = (res.start >> 16) | (res.end & 0xffff0000);
1092                 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
1093         }
1094
1095         pci_read_config_word(bridge, PCI_COMMAND, &w);
1096         w |= PCI_COMMAND_IO;
1097         pci_write_config_word(bridge, PCI_COMMAND, w);
1098
1099 #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
1100         if (enable_vga) {
1101                 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
1102                 w |= PCI_BRIDGE_CTL_VGA;
1103                 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
1104         }
1105 #endif
1106 }
1107
1108 /* This function is pretty basic and actually quite broken for the
1109  * general case, it's enough for us right now though. It's supposed
1110  * to tell us if we need to open an IO range at all or not and what
1111  * size.
1112  */
1113 static int __init
1114 check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
1115 {
1116         struct list_head *ln;
1117         int     i;
1118         int     rc = 0;
1119
1120 #define push_end(res, size) do { unsigned long __sz = (size) ; \
1121         res->end = ((res->end + __sz) / (__sz + 1)) * (__sz + 1) + __sz; \
1122     } while (0)
1123
1124         for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
1125                 struct pci_dev *dev = pci_dev_b(ln);
1126                 u16 class = dev->class >> 8;
1127
1128                 if (class == PCI_CLASS_DISPLAY_VGA ||
1129                     class == PCI_CLASS_NOT_DEFINED_VGA)
1130                         *found_vga = 1;
1131                 if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
1132                         rc |= check_for_io_childs(dev->subordinate, res, found_vga);
1133                 if (class == PCI_CLASS_BRIDGE_CARDBUS)
1134                         push_end(res, 0xfff);
1135
1136                 for (i=0; i<PCI_NUM_RESOURCES; i++) {
1137                         struct resource *r;
1138                         unsigned long r_size;
1139
1140                         if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
1141                             && i >= PCI_BRIDGE_RESOURCES)
1142                                 continue;
1143                         r = &dev->resource[i];
1144                         r_size = r->end - r->start;
1145                         if (r_size < 0xfff)
1146                                 r_size = 0xfff;
1147                         if (r->flags & IORESOURCE_IO && (r_size) != 0) {
1148                                 rc = 1;
1149                                 push_end(res, r_size);
1150                         }
1151                 }
1152         }
1153
1154         return rc;
1155 }
1156
1157 /* Here we scan all P2P bridges of a given level that have a closed
1158  * IO window. Note that the test for the presence of a VGA card should
1159  * be improved to take into account already configured P2P bridges,
1160  * currently, we don't see them and might end up configuring 2 bridges
1161  * with VGA pass through enabled
1162  */
1163 static void __init
1164 do_fixup_p2p_level(struct pci_bus *bus)
1165 {
1166         struct list_head *ln;
1167         int i, parent_io;
1168         int has_vga = 0;
1169
1170         for (parent_io=0; parent_io<4; parent_io++)
1171                 if (bus->resource[parent_io]->flags & IORESOURCE_IO)
1172                         break;
1173         if (parent_io >= 4)
1174                 return;
1175
1176         for (ln=bus->children.next; ln != &bus->children; ln=ln->next) {
1177                 struct pci_bus *b = pci_bus_b(ln);
1178                 struct pci_dev *d = b->self;
1179                 struct pci_controller* hose = (struct pci_controller *)d->sysdata;
1180                 struct resource *res = b->resource[0];
1181                 struct resource tmp_res;
1182                 unsigned long max;
1183                 int found_vga = 0;
1184
1185                 memset(&tmp_res, 0, sizeof(tmp_res));
1186                 tmp_res.start = bus->resource[parent_io]->start;
1187
1188                 /* We don't let low addresses go through that closed P2P bridge, well,
1189                  * that may not be necessary but I feel safer that way
1190                  */
1191                 if (tmp_res.start == 0)
1192                         tmp_res.start = 0x1000;
1193         
1194                 if (!list_empty(&b->devices) && res && res->flags == 0 &&
1195                     res != bus->resource[parent_io] &&
1196                     (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
1197                     check_for_io_childs(b, &tmp_res, &found_vga)) {
1198                         u8 io_base_lo;
1199
1200                         printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
1201
1202                         if (found_vga) {
1203                                 if (has_vga) {
1204                                         printk(KERN_WARNING "Skipping VGA, already active"
1205                                             " on bus segment\n");
1206                                         found_vga = 0;
1207                                 } else
1208                                         has_vga = 1;
1209                         }
1210                         pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
1211
1212                         if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
1213                                 max = ((unsigned long) hose->io_base_virt
1214                                         - isa_io_base) + 0xffffffff;
1215                         else
1216                                 max = ((unsigned long) hose->io_base_virt
1217                                         - isa_io_base) + 0xffff;
1218
1219                         *res = tmp_res;
1220                         res->flags = IORESOURCE_IO;
1221                         res->name = b->name;
1222                 
1223                         /* Find a resource in the parent where we can allocate */
1224                         for (i = 0 ; i < 4; i++) {
1225                                 struct resource *r = bus->resource[i];
1226                                 if (!r)
1227                                         continue;
1228                                 if ((r->flags & IORESOURCE_IO) == 0)
1229                                         continue;
1230                                 DBG("Trying to allocate from %08lx, size %08lx from parent"
1231                                     " res %d: %08lx -> %08lx\n",
1232                                         res->start, res->end, i, r->start, r->end);
1233                         
1234                                 if (allocate_resource(r, res, res->end + 1, res->start, max,
1235                                     res->end + 1, NULL, NULL) < 0) {
1236                                         DBG("Failed !\n");
1237                                         continue;
1238                                 }
1239                                 do_update_p2p_io_resource(b, found_vga);
1240                                 break;
1241                         }
1242                 }
1243                 do_fixup_p2p_level(b);
1244         }
1245 }
1246
1247 static void
1248 pcibios_fixup_p2p_bridges(void)
1249 {
1250         struct list_head *ln;
1251
1252         for(ln=pci_root_buses.next; ln != &pci_root_buses; ln=ln->next) {
1253                 struct pci_bus *b = pci_bus_b(ln);
1254                 do_fixup_p2p_level(b);
1255         }
1256 }
1257
1258 #endif /* CONFIG_PPC_PMAC */
1259
1260 static int __init
1261 pcibios_init(void)
1262 {
1263         struct pci_controller *hose;
1264         struct pci_bus *bus;
1265         int next_busno;
1266
1267         printk(KERN_INFO "PCI: Probing PCI hardware\n");
1268
1269         /* Scan all of the recorded PCI controllers.  */
1270         for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
1271                 if (pci_assign_all_busses)
1272                         hose->first_busno = next_busno;
1273                 hose->last_busno = 0xff;
1274                 bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
1275                 hose->last_busno = bus->subordinate;
1276                 if (pci_assign_all_busses || next_busno <= hose->last_busno)
1277                         next_busno = hose->last_busno+1;
1278         }
1279         pci_bus_count = next_busno;
1280
1281         /* OpenFirmware based machines need a map of OF bus
1282          * numbers vs. kernel bus numbers since we may have to
1283          * remap them.
1284          */
1285         if (pci_assign_all_busses && have_of)
1286                 pcibios_make_OF_bus_map();
1287
1288         /* Do machine dependent PCI interrupt routing */
1289         if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
1290                 pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
1291
1292         /* Call machine dependent fixup */
1293         if (ppc_md.pcibios_fixup)
1294                 ppc_md.pcibios_fixup();
1295
1296         /* Allocate and assign resources */
1297         pcibios_allocate_bus_resources(&pci_root_buses);
1298         pcibios_allocate_resources(0);
1299         pcibios_allocate_resources(1);
1300 #ifdef CONFIG_PPC_PMAC
1301         pcibios_fixup_p2p_bridges();
1302 #endif /* CONFIG_PPC_PMAC */
1303         pcibios_assign_resources();
1304
1305         /* Call machine dependent post-init code */
1306         if (ppc_md.pcibios_after_init)
1307                 ppc_md.pcibios_after_init();
1308
1309         return 0;
1310 }
1311
1312 subsys_initcall(pcibios_init);
1313
1314 unsigned char __init
1315 common_swizzle(struct pci_dev *dev, unsigned char *pinp)
1316 {
1317         struct pci_controller *hose = dev->sysdata;
1318
1319         if (dev->bus->number != hose->first_busno) {
1320                 u8 pin = *pinp;
1321                 do {
1322                         pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
1323                         /* Move up the chain of bridges. */
1324                         dev = dev->bus->self;
1325                 } while (dev->bus->self);
1326                 *pinp = pin;
1327
1328                 /* The slot is the idsel of the last bridge. */
1329         }
1330         return PCI_SLOT(dev->devfn);
1331 }
1332
1333 unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
1334                              unsigned long start, unsigned long size)
1335 {
1336         return start;
1337 }
1338
1339 void __init pcibios_fixup_bus(struct pci_bus *bus)
1340 {
1341         struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1342         unsigned long io_offset;
1343         struct resource *res;
1344         int i;
1345
1346         io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1347         if (bus->parent == NULL) {
1348                 /* This is a host bridge - fill in its resources */
1349                 hose->bus = bus;
1350
1351                 bus->resource[0] = res = &hose->io_resource;
1352                 if (!res->flags) {
1353                         if (io_offset)
1354                                 printk(KERN_ERR "I/O resource not set for host"
1355                                        " bridge %d\n", hose->index);
1356                         res->start = 0;
1357                         res->end = IO_SPACE_LIMIT;
1358                         res->flags = IORESOURCE_IO;
1359                 }
1360                 res->start += io_offset;
1361                 res->end += io_offset;
1362
1363                 for (i = 0; i < 3; ++i) {
1364                         res = &hose->mem_resources[i];
1365                         if (!res->flags) {
1366                                 if (i > 0)
1367                                         continue;
1368                                 printk(KERN_ERR "Memory resource not set for "
1369                                        "host bridge %d\n", hose->index);
1370                                 res->start = hose->pci_mem_offset;
1371                                 res->end = ~0U;
1372                                 res->flags = IORESOURCE_MEM;
1373                         }
1374                         bus->resource[i+1] = res;
1375                 }
1376         } else {
1377                 /* This is a subordinate bridge */
1378                 pci_read_bridge_bases(bus);
1379
1380                 for (i = 0; i < 4; ++i) {
1381                         if ((res = bus->resource[i]) == NULL)
1382                                 continue;
1383                         if (!res->flags)
1384                                 continue;
1385                         if (io_offset && (res->flags & IORESOURCE_IO)) {
1386                                 res->start += io_offset;
1387                                 res->end += io_offset;
1388                         } else if (hose->pci_mem_offset
1389                                    && (res->flags & IORESOURCE_MEM)) {
1390                                 res->start += hose->pci_mem_offset;
1391                                 res->end += hose->pci_mem_offset;
1392                         }
1393                 }
1394         }
1395
1396         if (ppc_md.pcibios_fixup_bus)
1397                 ppc_md.pcibios_fixup_bus(bus);
1398 }
1399
1400 char __init *pcibios_setup(char *str)
1401 {
1402         return str;
1403 }
1404
1405 /* the next one is stolen from the alpha port... */
1406 void __init
1407 pcibios_update_irq(struct pci_dev *dev, int irq)
1408 {
1409         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1410         /* XXX FIXME - update OF device tree node interrupt property */
1411 }
1412
1413 int pcibios_enable_device(struct pci_dev *dev, int mask)
1414 {
1415         u16 cmd, old_cmd;
1416         int idx;
1417         struct resource *r;
1418
1419         if (ppc_md.pcibios_enable_device_hook)
1420                 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1421                         return -EINVAL;
1422                 
1423         pci_read_config_word(dev, PCI_COMMAND, &cmd);
1424         old_cmd = cmd;
1425         for (idx=0; idx<6; idx++) {
1426                 r = &dev->resource[idx];
1427                 if (r->flags & IORESOURCE_UNSET) {
1428                         printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1429                         return -EINVAL;
1430                 }
1431                 if (r->flags & IORESOURCE_IO)
1432                         cmd |= PCI_COMMAND_IO;
1433                 if (r->flags & IORESOURCE_MEM)
1434                         cmd |= PCI_COMMAND_MEMORY;
1435         }
1436         if (cmd != old_cmd) {
1437                 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1438                        pci_name(dev), old_cmd, cmd);
1439                 pci_write_config_word(dev, PCI_COMMAND, cmd);
1440         }
1441         return 0;
1442 }
1443
1444 struct pci_controller*
1445 pci_bus_to_hose(int bus)
1446 {
1447         struct pci_controller* hose = hose_head;
1448
1449         for (; hose; hose = hose->next)
1450                 if (bus >= hose->first_busno && bus <= hose->last_busno)
1451                         return hose;
1452         return NULL;
1453 }
1454
1455 void*
1456 pci_bus_io_base(unsigned int bus)
1457 {
1458         struct pci_controller *hose;
1459
1460         hose = pci_bus_to_hose(bus);
1461         if (!hose)
1462                 return NULL;
1463         return hose->io_base_virt;
1464 }
1465
1466 unsigned long
1467 pci_bus_io_base_phys(unsigned int bus)
1468 {
1469         struct pci_controller *hose;
1470
1471         hose = pci_bus_to_hose(bus);
1472         if (!hose)
1473                 return 0;
1474         return hose->io_base_phys;
1475 }
1476
1477 unsigned long
1478 pci_bus_mem_base_phys(unsigned int bus)
1479 {
1480         struct pci_controller *hose;
1481
1482         hose = pci_bus_to_hose(bus);
1483         if (!hose)
1484                 return 0;
1485         return hose->pci_mem_offset;
1486 }
1487
1488 unsigned long
1489 pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
1490 {
1491         /* Hack alert again ! See comments in chrp_pci.c
1492          */
1493         struct pci_controller* hose =
1494                 (struct pci_controller *)pdev->sysdata;
1495         if (hose && res->flags & IORESOURCE_MEM)
1496                 return res->start - hose->pci_mem_offset;
1497         /* We may want to do something with IOs here... */
1498         return res->start;
1499 }
1500
1501 /*
1502  * Platform support for /proc/bus/pci/X/Y mmap()s,
1503  * modelled on the sparc64 implementation by Dave Miller.
1504  *  -- paulus.
1505  */
1506
1507 /*
1508  * Adjust vm_pgoff of VMA such that it is the physical page offset
1509  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
1510  *
1511  * Basically, the user finds the base address for his device which he wishes
1512  * to mmap.  They read the 32-bit value from the config space base register,
1513  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
1514  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
1515  *
1516  * Returns negative error code on failure, zero on success.
1517  */
1518 static __inline__ int
1519 __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
1520                        enum pci_mmap_state mmap_state)
1521 {
1522         struct pci_controller *hose = (struct pci_controller *) dev->sysdata;
1523         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1524         unsigned long size = vma->vm_end - vma->vm_start;
1525         unsigned long base;
1526         struct resource *res;
1527         int i;
1528         int ret = -EINVAL;
1529
1530         if (hose == 0)
1531                 return -EINVAL;         /* should never happen */
1532         if (offset + size <= offset)
1533                 return -EINVAL;
1534
1535         if (mmap_state == pci_mmap_mem) {
1536                 /* PCI memory space */
1537                 base = hose->pci_mem_offset;
1538                 for (i = 0; i < 3; ++i) {
1539                         res = &hose->mem_resources[i];
1540                         if (res->flags == 0)
1541                                 continue;
1542                         if (offset >= res->start - base
1543                             && offset + size - 1 <= res->end - base) {
1544                                 ret = 0;
1545                                 break;
1546                         }
1547                 }
1548                 offset += hose->pci_mem_offset;
1549         } else {
1550                 /* PCI I/O space */
1551                 base = (unsigned long)hose->io_base_virt - isa_io_base;
1552                 res = &hose->io_resource;
1553                 if (offset >= res->start - base
1554                     && offset + size - 1 <= res->end - base)
1555                         ret = 0;
1556                 offset += hose->io_base_phys;
1557         }
1558
1559         vma->vm_pgoff = offset >> PAGE_SHIFT;
1560         return ret;
1561 }
1562
1563 /*
1564  * Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1565  * mapping.
1566  */
1567 static __inline__ void
1568 __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1569                      enum pci_mmap_state mmap_state)
1570 {
1571         vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
1572 }
1573
1574 /*
1575  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1576  * device mapping.
1577  */
1578 static __inline__ void
1579 __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1580                       enum pci_mmap_state mmap_state, int write_combine)
1581 {
1582         int prot = pgprot_val(vma->vm_page_prot);
1583
1584         /* XXX would be nice to have a way to ask for write-through */
1585         prot |= _PAGE_NO_CACHE;
1586         if (!write_combine)
1587                 prot |= _PAGE_GUARDED;
1588         vma->vm_page_prot = __pgprot(prot);
1589 }
1590
1591 /*
1592  * Perform the actual remap of the pages for a PCI device mapping, as
1593  * appropriate for this architecture.  The region in the process to map
1594  * is described by vm_start and vm_end members of VMA, the base physical
1595  * address is found in vm_pgoff.
1596  * The pci device structure is provided so that architectures may make mapping
1597  * decisions on a per-device or per-bus basis.
1598  *
1599  * Returns a negative error code on failure, zero on success.
1600  */
1601 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1602                         enum pci_mmap_state mmap_state,
1603                         int write_combine)
1604 {
1605         int ret;
1606
1607         ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1608         if (ret < 0)
1609                 return ret;
1610
1611         __pci_mmap_set_flags(dev, vma, mmap_state);
1612         __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
1613
1614         ret = remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
1615                                vma->vm_end - vma->vm_start, vma->vm_page_prot);
1616
1617         return ret;
1618 }
1619
1620 /* Obsolete functions. Should be removed once the symbios driver
1621  * is fixed
1622  */
1623 unsigned long
1624 phys_to_bus(unsigned long pa)
1625 {
1626         struct pci_controller *hose;
1627         int i;
1628
1629         for (hose = hose_head; hose; hose = hose->next) {
1630                 for (i = 0; i < 3; ++i) {
1631                         if (pa >= hose->mem_resources[i].start
1632                             && pa <= hose->mem_resources[i].end) {
1633                                 /*
1634                                  * XXX the hose->pci_mem_offset really
1635                                  * only applies to mem_resources[0].
1636                                  * We need a way to store an offset for
1637                                  * the others.  -- paulus
1638                                  */
1639                                 if (i == 0)
1640                                         pa -= hose->pci_mem_offset;
1641                                 return pa;
1642                         }
1643                 }
1644         }
1645         /* hmmm, didn't find it */
1646         return 0;
1647 }
1648
1649 unsigned long
1650 pci_phys_to_bus(unsigned long pa, int busnr)
1651 {
1652         struct pci_controller* hose = pci_bus_to_hose(busnr);
1653         if (!hose)
1654                 return pa;
1655         return pa - hose->pci_mem_offset;
1656 }
1657
1658 unsigned long
1659 pci_bus_to_phys(unsigned int ba, int busnr)
1660 {
1661         struct pci_controller* hose = pci_bus_to_hose(busnr);
1662         if (!hose)
1663                 return ba;
1664         return ba + hose->pci_mem_offset;
1665 }
1666
1667 /* Provide information on locations of various I/O regions in physical
1668  * memory.  Do this on a per-card basis so that we choose the right
1669  * root bridge.
1670  * Note that the returned IO or memory base is a physical address
1671  */
1672
1673 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1674 {
1675         struct pci_controller* hose;
1676         long result = -EOPNOTSUPP;
1677
1678         /* Argh ! Please forgive me for that hack, but that's the
1679          * simplest way to get existing XFree to not lockup on some
1680          * G5 machines... So when something asks for bus 0 io base
1681          * (bus 0 is HT root), we return the AGP one instead.
1682          */
1683 #ifdef CONFIG_PPC_PMAC
1684         if (_machine == _MACH_Pmac && machine_is_compatible("MacRISC4"))
1685                 if (bus == 0)
1686                         bus = 0xf0;
1687 #endif /* CONFIG_PPC_PMAC */
1688
1689         hose = pci_bus_to_hose(bus);
1690         if (!hose)
1691                 return -ENODEV;
1692
1693         switch (which) {
1694         case IOBASE_BRIDGE_NUMBER:
1695                 return (long)hose->first_busno;
1696         case IOBASE_MEMORY:
1697                 return (long)hose->pci_mem_offset;
1698         case IOBASE_IO:
1699                 return (long)hose->io_base_phys;
1700         case IOBASE_ISA_IO:
1701                 return (long)isa_io_base;
1702         case IOBASE_ISA_MEM:
1703                 return (long)isa_mem_base;
1704         }
1705
1706         return result;
1707 }
1708
1709 void __init
1710 pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
1711                   int flags, char *name)
1712 {
1713         res->start = start;
1714         res->end = end;
1715         res->flags = flags;
1716         res->name = name;
1717         res->parent = NULL;
1718         res->sibling = NULL;
1719         res->child = NULL;
1720 }
1721
1722 /*
1723  * Null PCI config access functions, for the case when we can't
1724  * find a hose.
1725  */
1726 #define NULL_PCI_OP(rw, size, type)                                     \
1727 static int                                                              \
1728 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)    \
1729 {                                                                       \
1730         return PCIBIOS_DEVICE_NOT_FOUND;                                \
1731 }
1732
1733 static int
1734 null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1735                  int len, u32 *val)
1736 {
1737         return PCIBIOS_DEVICE_NOT_FOUND;
1738 }
1739
1740 static int
1741 null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1742                   int len, u32 val)
1743 {
1744         return PCIBIOS_DEVICE_NOT_FOUND;
1745 }
1746
1747 static struct pci_ops null_pci_ops =
1748 {
1749         null_read_config,
1750         null_write_config
1751 };
1752
1753 /*
1754  * These functions are used early on before PCI scanning is done
1755  * and all of the pci_dev and pci_bus structures have been created.
1756  */
1757 static struct pci_bus *
1758 fake_pci_bus(struct pci_controller *hose, int busnr)
1759 {
1760         static struct pci_bus bus;
1761
1762         if (hose == 0) {
1763                 hose = pci_bus_to_hose(busnr);
1764                 if (hose == 0)
1765                         printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1766         }
1767         bus.number = busnr;
1768         bus.sysdata = hose;
1769         bus.ops = hose? hose->ops: &null_pci_ops;
1770         return &bus;
1771 }
1772
1773 #define EARLY_PCI_OP(rw, size, type)                                    \
1774 int early_##rw##_config_##size(struct pci_controller *hose, int bus,    \
1775                                int devfn, int offset, type value)       \
1776 {                                                                       \
1777         return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),    \
1778                                             devfn, offset, value);      \
1779 }
1780
1781 EARLY_PCI_OP(read, byte, u8 *)
1782 EARLY_PCI_OP(read, word, u16 *)
1783 EARLY_PCI_OP(read, dword, u32 *)
1784 EARLY_PCI_OP(write, byte, u8)
1785 EARLY_PCI_OP(write, word, u16)
1786 EARLY_PCI_OP(write, dword, u32)