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