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