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