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