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