2 * Common pmac/prep/chrp pci routines. -- Cort
5 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/init.h>
10 #include <linux/capability.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/bootmem.h>
14 #include <linux/irq.h>
15 #include <linux/list.h>
17 #include <asm/processor.h>
20 #include <asm/sections.h>
21 #include <asm/pci-bridge.h>
22 #include <asm/byteorder.h>
23 #include <asm/uaccess.h>
24 #include <asm/machdep.h>
29 #define DBG(x...) printk(x)
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;
39 void pcibios_make_OF_bus_map(void);
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);
50 static u8* pci_to_OF_bus_map;
53 /* By default, we don't re-assign bus numbers. We do this only on
56 int pci_assign_all_buses;
60 static int pci_bus_count;
63 fixup_broken_pcnet32(struct pci_dev* dev)
65 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
66 dev->vendor = PCI_VENDOR_ID_AMD;
67 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
70 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
73 fixup_cpc710_pci64(struct pci_dev* dev)
75 /* Hide the PCI64 BARs from the kernel as their content doesn't
76 * fit well in the resource management
78 dev->resource[0].start = dev->resource[0].end = 0;
79 dev->resource[0].flags = 0;
80 dev->resource[1].start = dev->resource[1].end = 0;
81 dev->resource[1].flags = 0;
83 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
86 pcibios_fixup_resources(struct pci_dev *dev)
88 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
93 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
96 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
97 struct resource *res = dev->resource + i;
100 if (res->end == 0xffffffff) {
101 DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
102 pci_name(dev), i, (u64)res->start, (u64)res->end);
103 res->end -= res->start;
105 res->flags |= IORESOURCE_UNSET;
109 if (res->flags & IORESOURCE_MEM) {
110 offset = hose->pci_mem_offset;
111 } else if (res->flags & IORESOURCE_IO) {
112 offset = (unsigned long) hose->io_base_virt
116 res->start += offset;
118 DBG("Fixup res %d (%lx) of dev %s: %llx -> %llx\n",
119 i, res->flags, pci_name(dev),
120 (u64)res->start - offset, (u64)res->start);
124 /* Call machine specific resource fixup */
125 if (ppc_md.pcibios_fixup_resources)
126 ppc_md.pcibios_fixup_resources(dev);
128 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
130 void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
131 struct resource *res)
133 unsigned long offset = 0;
134 struct pci_controller *hose = dev->sysdata;
136 if (hose && res->flags & IORESOURCE_IO)
137 offset = (unsigned long)hose->io_base_virt - isa_io_base;
138 else if (hose && res->flags & IORESOURCE_MEM)
139 offset = hose->pci_mem_offset;
140 region->start = res->start - offset;
141 region->end = res->end - offset;
143 EXPORT_SYMBOL(pcibios_resource_to_bus);
145 void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
146 struct pci_bus_region *region)
148 unsigned long offset = 0;
149 struct pci_controller *hose = dev->sysdata;
151 if (hose && res->flags & IORESOURCE_IO)
152 offset = (unsigned long)hose->io_base_virt - isa_io_base;
153 else if (hose && res->flags & IORESOURCE_MEM)
154 offset = hose->pci_mem_offset;
155 res->start = region->start + offset;
156 res->end = region->end + offset;
158 EXPORT_SYMBOL(pcibios_bus_to_resource);
161 * We need to avoid collisions with `mirrored' VGA ports
162 * and other strange ISA hardware, so we always want the
163 * addresses to be allocated in the 0x000-0x0ff region
166 * Why? Because some silly external IO cards only decode
167 * the low 10 bits of the IO address. The 0x00-0xff region
168 * is reserved for motherboard devices that decode all 16
169 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
170 * but we want to try to avoid allocating at 0x2900-0x2bff
171 * which might have be mirrored at 0x0100-0x03ff..
173 void pcibios_align_resource(void *data, struct resource *res,
174 resource_size_t size, resource_size_t align)
176 struct pci_dev *dev = data;
178 if (res->flags & IORESOURCE_IO) {
179 resource_size_t start = res->start;
182 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
183 " (%lld bytes)\n", pci_name(dev),
184 dev->resource - res, (unsigned long long)size);
188 start = (start + 0x3ff) & ~0x3ff;
193 EXPORT_SYMBOL(pcibios_align_resource);
196 * Handle resources of PCI devices. If the world were perfect, we could
197 * just allocate all the resource regions and do nothing more. It isn't.
198 * On the other hand, we cannot just re-allocate all devices, as it would
199 * require us to know lots of host bridge internals. So we attempt to
200 * keep as much of the original configuration as possible, but tweak it
201 * when it's found to be wrong.
203 * Known BIOS problems we have to work around:
204 * - I/O or memory regions not configured
205 * - regions configured, but not enabled in the command register
206 * - bogus I/O addresses above 64K used
207 * - expansion ROMs left enabled (this may sound harmless, but given
208 * the fact the PCI specs explicitly allow address decoders to be
209 * shared between expansion ROMs and other resource regions, it's
210 * at least dangerous)
213 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
214 * This gives us fixed barriers on where we can allocate.
215 * (2) Allocate resources for all enabled devices. If there is
216 * a collision, just mark the resource as unallocated. Also
217 * disable expansion ROMs during this step.
218 * (3) Try to allocate resources for disabled devices. If the
219 * resources were assigned correctly, everything goes well,
220 * if they weren't, they won't disturb allocation of other
222 * (4) Assign new addresses to resources which were either
223 * not configured at all or misconfigured. If explicitly
224 * requested by the user, configure expansion ROM address
229 pcibios_allocate_bus_resources(struct list_head *bus_list)
233 struct resource *res, *pr;
235 /* Depth-First Search on bus tree */
236 list_for_each_entry(bus, bus_list, node) {
237 for (i = 0; i < 4; ++i) {
238 if ((res = bus->resource[i]) == NULL || !res->flags
239 || res->start > res->end)
241 if (bus->parent == NULL)
242 pr = (res->flags & IORESOURCE_IO)?
243 &ioport_resource: &iomem_resource;
245 pr = pci_find_parent_resource(bus->self, res);
247 /* this happens when the generic PCI
248 * code (wrongly) decides that this
249 * bridge is transparent -- paulus
255 DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
256 (u64)res->start, (u64)res->end, res->flags, pr);
258 if (request_resource(pr, res) == 0)
261 * Must be a conflict with an existing entry.
262 * Move that entry (or entries) under the
263 * bridge resource and try again.
265 if (reparent_resources(pr, res) == 0)
268 printk(KERN_ERR "PCI: Cannot allocate resource region "
269 "%d of PCI bridge %d\n", i, bus->number);
270 if (pci_relocate_bridge_resource(bus, i))
271 bus->resource[i] = NULL;
273 pcibios_allocate_bus_resources(&bus->children);
278 * Reparent resource children of pr that conflict with res
279 * under res, and make res replace those children.
282 reparent_resources(struct resource *parent, struct resource *res)
284 struct resource *p, **pp;
285 struct resource **firstpp = NULL;
287 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
288 if (p->end < res->start)
290 if (res->end < p->start)
292 if (p->start < res->start || p->end > res->end)
293 return -1; /* not completely contained */
298 return -1; /* didn't find any conflicting entries? */
299 res->parent = parent;
300 res->child = *firstpp;
304 for (p = res->child; p != NULL; p = p->sibling) {
306 DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
307 p->name, (u64)p->start, (u64)p->end, res->name);
313 * A bridge has been allocated a range which is outside the range
314 * of its parent bridge, so it needs to be moved.
317 pci_relocate_bridge_resource(struct pci_bus *bus, int i)
319 struct resource *res, *pr, *conflict;
320 unsigned long try, size;
322 struct pci_bus *parent = bus->parent;
324 if (parent == NULL) {
325 /* shouldn't ever happen */
326 printk(KERN_ERR "PCI: can't move host bridge resource\n");
329 res = bus->resource[i];
333 for (j = 0; j < 4; j++) {
334 struct resource *r = parent->resource[j];
337 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
339 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
343 if (res->flags & IORESOURCE_PREFETCH)
348 size = res->end - res->start;
349 if (pr->start > pr->end || size > pr->end - pr->start)
353 res->start = try - size;
355 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
357 if (conflict->start <= pr->start + size)
359 try = conflict->start - 1;
361 if (request_resource(pr, res)) {
362 DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n",
363 (u64)res->start, (u64)res->end);
364 return -1; /* "can't happen" */
366 update_bridge_base(bus, i);
367 printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n",
368 bus->number, i, (unsigned long long)res->start,
369 (unsigned long long)res->end);
374 probe_resource(struct pci_bus *parent, struct resource *pr,
375 struct resource *res, struct resource **conflict)
382 for (r = pr->child; r != NULL; r = r->sibling) {
383 if (r->end >= res->start && res->end >= r->start) {
388 list_for_each_entry(bus, &parent->children, node) {
389 for (i = 0; i < 4; ++i) {
390 if ((r = bus->resource[i]) == NULL)
392 if (!r->flags || r->start > r->end || r == res)
394 if (pci_find_parent_resource(bus->self, r) != pr)
396 if (r->end >= res->start && res->end >= r->start) {
402 list_for_each_entry(dev, &parent->devices, bus_list) {
403 for (i = 0; i < 6; ++i) {
404 r = &dev->resource[i];
405 if (!r->flags || (r->flags & IORESOURCE_UNSET))
407 if (pci_find_parent_resource(dev, r) != pr)
409 if (r->end >= res->start && res->end >= r->start) {
419 update_bridge_resource(struct pci_dev *dev, struct resource *res)
421 u8 io_base_lo, io_limit_lo;
422 u16 mem_base, mem_limit;
424 unsigned long start, end, off;
425 struct pci_controller *hose = dev->sysdata;
428 printk("update_bridge_base: no hose?\n");
431 pci_read_config_word(dev, PCI_COMMAND, &cmd);
432 pci_write_config_word(dev, PCI_COMMAND,
433 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
434 if (res->flags & IORESOURCE_IO) {
435 off = (unsigned long) hose->io_base_virt - isa_io_base;
436 start = res->start - off;
437 end = res->end - off;
438 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
439 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
441 io_base_lo |= PCI_IO_RANGE_TYPE_32;
443 io_base_lo |= PCI_IO_RANGE_TYPE_16;
444 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
446 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
448 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
449 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
451 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
453 off = hose->pci_mem_offset;
454 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
455 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
456 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
457 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
459 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
460 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
461 off = hose->pci_mem_offset;
462 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
463 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
464 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
465 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
468 DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
469 pci_name(dev), res->flags);
471 pci_write_config_word(dev, PCI_COMMAND, cmd);
475 update_bridge_base(struct pci_bus *bus, int i)
477 struct resource *res = bus->resource[i];
478 struct pci_dev *dev = bus->self;
479 update_bridge_resource(dev, res);
482 static inline void alloc_resource(struct pci_dev *dev, int idx)
484 struct resource *pr, *r = &dev->resource[idx];
486 DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
487 pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
488 pr = pci_find_parent_resource(dev, r);
489 if (!pr || request_resource(pr, r) < 0) {
490 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
491 " of device %s\n", idx, pci_name(dev));
493 DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
494 pr, (u64)pr->start, (u64)pr->end, pr->flags);
495 /* We'll assign a new address later */
496 r->flags |= IORESOURCE_UNSET;
503 pcibios_allocate_resources(int pass)
505 struct pci_dev *dev = NULL;
510 for_each_pci_dev(dev) {
511 pci_read_config_word(dev, PCI_COMMAND, &command);
512 for (idx = 0; idx < 6; idx++) {
513 r = &dev->resource[idx];
514 if (r->parent) /* Already allocated */
516 if (!r->flags || (r->flags & IORESOURCE_UNSET))
517 continue; /* Not assigned at all */
518 if (r->flags & IORESOURCE_IO)
519 disabled = !(command & PCI_COMMAND_IO);
521 disabled = !(command & PCI_COMMAND_MEMORY);
522 if (pass == disabled)
523 alloc_resource(dev, idx);
527 r = &dev->resource[PCI_ROM_RESOURCE];
528 if (r->flags & IORESOURCE_ROM_ENABLE) {
529 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
531 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
532 r->flags &= ~IORESOURCE_ROM_ENABLE;
533 pci_read_config_dword(dev, dev->rom_base_reg, ®);
534 pci_write_config_dword(dev, dev->rom_base_reg,
535 reg & ~PCI_ROM_ADDRESS_ENABLE);
541 pcibios_assign_resources(void)
543 struct pci_dev *dev = NULL;
547 for_each_pci_dev(dev) {
548 int class = dev->class >> 8;
550 /* Don't touch classless devices and host bridges */
551 if (!class || class == PCI_CLASS_BRIDGE_HOST)
554 for (idx = 0; idx < 6; idx++) {
555 r = &dev->resource[idx];
558 * We shall assign a new address to this resource,
559 * either because the BIOS (sic) forgot to do so
560 * or because we have decided the old address was
561 * unusable for some reason.
563 if ((r->flags & IORESOURCE_UNSET) && r->end &&
564 (!ppc_md.pcibios_enable_device_hook ||
565 !ppc_md.pcibios_enable_device_hook(dev, 1))) {
566 r->flags &= ~IORESOURCE_UNSET;
567 pci_assign_resource(dev, idx);
571 #if 0 /* don't assign ROMs */
572 r = &dev->resource[PCI_ROM_RESOURCE];
576 pci_assign_resource(dev, PCI_ROM_RESOURCE);
583 * Functions below are used on OpenFirmware machines.
586 make_one_node_map(struct device_node* node, u8 pci_bus)
588 const int *bus_range;
591 if (pci_bus >= pci_bus_count)
593 bus_range = of_get_property(node, "bus-range", &len);
594 if (bus_range == NULL || len < 2 * sizeof(int)) {
595 printk(KERN_WARNING "Can't get bus-range for %s, "
596 "assuming it starts at 0\n", node->full_name);
597 pci_to_OF_bus_map[pci_bus] = 0;
599 pci_to_OF_bus_map[pci_bus] = bus_range[0];
601 for (node=node->child; node != 0;node = node->sibling) {
603 const unsigned int *class_code, *reg;
605 class_code = of_get_property(node, "class-code", NULL);
606 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
607 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
609 reg = of_get_property(node, "reg", NULL);
612 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
613 if (!dev || !dev->subordinate) {
617 make_one_node_map(node, dev->subordinate->number);
623 pcibios_make_OF_bus_map(void)
626 struct pci_controller *hose, *tmp;
627 struct property *map_prop;
628 struct device_node *dn;
630 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
631 if (!pci_to_OF_bus_map) {
632 printk(KERN_ERR "Can't allocate OF bus map !\n");
636 /* We fill the bus map with invalid values, that helps
639 for (i=0; i<pci_bus_count; i++)
640 pci_to_OF_bus_map[i] = 0xff;
642 /* For each hose, we begin searching bridges */
643 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
644 struct device_node* node;
645 node = (struct device_node *)hose->arch_data;
648 make_one_node_map(node, hose->first_busno);
650 dn = of_find_node_by_path("/");
651 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
653 BUG_ON(pci_bus_count > map_prop->length);
654 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
658 printk("PCI->OF bus map:\n");
659 for (i=0; i<pci_bus_count; i++) {
660 if (pci_to_OF_bus_map[i] == 0xff)
662 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
667 typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
669 static struct device_node*
670 scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
672 struct device_node* sub_node;
674 for (; node != 0;node = node->sibling) {
675 const unsigned int *class_code;
677 if (filter(node, data))
680 /* For PCI<->PCI bridges or CardBus bridges, we go down
681 * Note: some OFs create a parent node "multifunc-device" as
682 * a fake root for all functions of a multi-function device,
683 * we go down them as well.
685 class_code = of_get_property(node, "class-code", NULL);
686 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
687 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
688 strcmp(node->name, "multifunc-device"))
690 sub_node = scan_OF_pci_childs(node->child, filter, data);
697 static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
700 struct device_node *np = NULL;
704 while ((np = of_get_next_child(parent, np)) != NULL) {
705 reg = of_get_property(np, "reg", &psize);
706 if (reg == NULL || psize < 4)
708 if (((reg[0] >> 8) & 0xff) == devfn)
715 static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
717 struct device_node *parent, *np;
719 /* Are we a root bus ? */
720 if (bus->self == NULL || bus->parent == NULL) {
721 struct pci_controller *hose = pci_bus_to_host(bus);
724 return of_node_get(hose->arch_data);
727 /* not a root bus, we need to get our parent */
728 parent = scan_OF_for_pci_bus(bus->parent);
732 /* now iterate for children for a match */
733 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
740 * Scans the OF tree for a device node matching a PCI device
743 pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
745 struct device_node *parent, *np;
750 DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
751 parent = scan_OF_for_pci_bus(bus);
754 DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
755 np = scan_OF_for_pci_dev(parent, devfn);
757 DBG(" result is %s\n", np ? np->full_name : "<NULL>");
759 /* XXX most callers don't release the returned node
760 * mostly because ppc64 doesn't increase the refcount,
761 * we need to fix that.
765 EXPORT_SYMBOL(pci_busdev_to_OF_node);
768 pci_device_to_OF_node(struct pci_dev *dev)
770 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
772 EXPORT_SYMBOL(pci_device_to_OF_node);
775 find_OF_pci_device_filter(struct device_node* node, void* data)
777 return ((void *)node == data);
781 * Returns the PCI device matching a given OF node
784 pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
786 const unsigned int *reg;
787 struct pci_controller* hose;
788 struct pci_dev* dev = NULL;
792 /* Make sure it's really a PCI device */
793 hose = pci_find_hose_for_OF_device(node);
794 if (!hose || !hose->arch_data)
796 if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
797 find_OF_pci_device_filter, (void *)node))
799 reg = of_get_property(node, "reg", NULL);
802 *bus = (reg[0] >> 16) & 0xff;
803 *devfn = ((reg[0] >> 8) & 0xff);
805 /* Ok, here we need some tweak. If we have already renumbered
806 * all busses, we can't rely on the OF bus number any more.
807 * the pci_to_OF_bus_map is not enough as several PCI busses
808 * may match the same OF bus number.
810 if (!pci_to_OF_bus_map)
813 for_each_pci_dev(dev)
814 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
815 dev->devfn == *devfn) {
816 *bus = dev->bus->number;
823 EXPORT_SYMBOL(pci_device_from_OF_node);
826 pci_process_bridge_OF_ranges(struct pci_controller *hose,
827 struct device_node *dev, int primary)
829 static unsigned int static_lc_ranges[256] __initdata;
830 const unsigned int *dt_ranges;
831 unsigned int *lc_ranges, *ranges, *prev, size;
832 int rlen = 0, orig_rlen;
834 struct resource *res;
835 int np, na = of_n_addr_cells(dev);
838 /* First we try to merge ranges to fix a problem with some pmacs
839 * that can have more than 3 ranges, fortunately using contiguous
842 dt_ranges = of_get_property(dev, "ranges", &rlen);
845 /* Sanity check, though hopefully that never happens */
846 if (rlen > sizeof(static_lc_ranges)) {
847 printk(KERN_WARNING "OF ranges property too large !\n");
848 rlen = sizeof(static_lc_ranges);
850 lc_ranges = static_lc_ranges;
851 memcpy(lc_ranges, dt_ranges, rlen);
854 /* Let's work on a copy of the "ranges" property instead of damaging
855 * the device-tree image in memory
859 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
861 if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
862 (prev[2] + prev[na+4]) == ranges[2] &&
863 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
864 prev[na+4] += ranges[na+4];
875 * The ranges property is laid out as an array of elements,
876 * each of which comprises:
877 * cells 0 - 2: a PCI address
878 * cells 3 or 3+4: a CPU physical address
879 * (size depending on dev->n_addr_cells)
880 * cells 4+5 or 5+6: the size of the range
884 while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
887 switch ((ranges[0] >> 24) & 0x3) {
888 case 1: /* I/O space */
891 hose->io_base_phys = ranges[na+2];
892 /* limit I/O space to 16MB */
893 if (size > 0x01000000)
895 hose->io_base_virt = ioremap(ranges[na+2], size);
897 isa_io_base = (unsigned long) hose->io_base_virt;
898 res = &hose->io_resource;
899 res->flags = IORESOURCE_IO;
900 res->start = ranges[2];
901 DBG("PCI: IO 0x%llx -> 0x%llx\n",
902 (u64)res->start, (u64)res->start + size - 1);
904 case 2: /* memory space */
906 if (ranges[1] == 0 && ranges[2] == 0
907 && ranges[na+4] <= (16 << 20)) {
908 /* 1st 16MB, i.e. ISA memory area */
910 isa_mem_base = ranges[na+2];
913 while (memno < 3 && hose->mem_resources[memno].flags)
916 hose->pci_mem_offset = ranges[na+2] - ranges[2];
918 res = &hose->mem_resources[memno];
919 res->flags = IORESOURCE_MEM;
920 if(ranges[0] & 0x40000000)
921 res->flags |= IORESOURCE_PREFETCH;
922 res->start = ranges[na+2];
923 DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
924 (u64)res->start, (u64)res->start + size - 1);
929 res->name = dev->full_name;
930 res->end = res->start + size - 1;
939 /* We create the "pci-OF-bus-map" property now so it appears in the
943 pci_create_OF_bus_map(void)
945 struct property* of_prop;
946 struct device_node *dn;
948 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
951 dn = of_find_node_by_path("/");
953 memset(of_prop, -1, sizeof(struct property) + 256);
954 of_prop->name = "pci-OF-bus-map";
955 of_prop->length = 256;
956 of_prop->value = &of_prop[1];
957 prom_add_property(dn, of_prop);
962 #else /* CONFIG_PPC_OF */
963 void pcibios_make_OF_bus_map(void)
966 #endif /* CONFIG_PPC_OF */
968 #ifdef CONFIG_PPC_PMAC
970 * This set of routines checks for PCI<->PCI bridges that have closed
971 * IO resources and have child devices. It tries to re-open an IO
974 * This is a _temporary_ fix to workaround a problem with Apple's OF
975 * closing IO windows on P2P bridges when the OF drivers of cards
976 * below this bridge don't claim any IO range (typically ATI or
979 * A more complete fix would be to use drivers/pci/setup-bus.c, which
980 * involves a working pcibios_fixup_pbus_ranges(), some more care about
981 * ordering when creating the host bus resources, and maybe a few more
985 /* Initialize bridges with base/limit values we have collected */
987 do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
989 struct pci_dev *bridge = bus->self;
990 struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
995 if (bus->resource[0] == NULL)
997 res = *(bus->resource[0]);
999 DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
1000 res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
1001 res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
1002 DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
1004 /* Set up the top and bottom of the PCI I/O segment for this bus. */
1005 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
1007 l |= (res.start >> 8) & 0x00f0;
1008 l |= res.end & 0xf000;
1009 pci_write_config_dword(bridge, PCI_IO_BASE, l);
1011 if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
1012 l = (res.start >> 16) | (res.end & 0xffff0000);
1013 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
1016 pci_read_config_word(bridge, PCI_COMMAND, &w);
1017 w |= PCI_COMMAND_IO;
1018 pci_write_config_word(bridge, PCI_COMMAND, w);
1020 #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
1022 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
1023 w |= PCI_BRIDGE_CTL_VGA;
1024 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
1029 /* This function is pretty basic and actually quite broken for the
1030 * general case, it's enough for us right now though. It's supposed
1031 * to tell us if we need to open an IO range at all or not and what
1035 check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
1037 struct pci_dev *dev;
1041 #define push_end(res, mask) do { \
1042 BUG_ON((mask+1) & mask); \
1043 res->end = (res->end + mask) | mask; \
1046 list_for_each_entry(dev, &bus->devices, bus_list) {
1047 u16 class = dev->class >> 8;
1049 if (class == PCI_CLASS_DISPLAY_VGA ||
1050 class == PCI_CLASS_NOT_DEFINED_VGA)
1052 if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
1053 rc |= check_for_io_childs(dev->subordinate, res, found_vga);
1054 if (class == PCI_CLASS_BRIDGE_CARDBUS)
1055 push_end(res, 0xfff);
1057 for (i=0; i<PCI_NUM_RESOURCES; i++) {
1059 unsigned long r_size;
1061 if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
1062 && i >= PCI_BRIDGE_RESOURCES)
1064 r = &dev->resource[i];
1065 r_size = r->end - r->start;
1068 if (r->flags & IORESOURCE_IO && (r_size) != 0) {
1070 push_end(res, r_size);
1078 /* Here we scan all P2P bridges of a given level that have a closed
1079 * IO window. Note that the test for the presence of a VGA card should
1080 * be improved to take into account already configured P2P bridges,
1081 * currently, we don't see them and might end up configuring 2 bridges
1082 * with VGA pass through enabled
1085 do_fixup_p2p_level(struct pci_bus *bus)
1091 for (parent_io=0; parent_io<4; parent_io++)
1092 if (bus->resource[parent_io]
1093 && bus->resource[parent_io]->flags & IORESOURCE_IO)
1098 list_for_each_entry(b, &bus->children, node) {
1099 struct pci_dev *d = b->self;
1100 struct pci_controller* hose = (struct pci_controller *)d->sysdata;
1101 struct resource *res = b->resource[0];
1102 struct resource tmp_res;
1106 memset(&tmp_res, 0, sizeof(tmp_res));
1107 tmp_res.start = bus->resource[parent_io]->start;
1109 /* We don't let low addresses go through that closed P2P bridge, well,
1110 * that may not be necessary but I feel safer that way
1112 if (tmp_res.start == 0)
1113 tmp_res.start = 0x1000;
1115 if (!list_empty(&b->devices) && res && res->flags == 0 &&
1116 res != bus->resource[parent_io] &&
1117 (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
1118 check_for_io_childs(b, &tmp_res, &found_vga)) {
1121 printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
1125 printk(KERN_WARNING "Skipping VGA, already active"
1126 " on bus segment\n");
1131 pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
1133 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
1134 max = ((unsigned long) hose->io_base_virt
1135 - isa_io_base) + 0xffffffff;
1137 max = ((unsigned long) hose->io_base_virt
1138 - isa_io_base) + 0xffff;
1141 res->flags = IORESOURCE_IO;
1142 res->name = b->name;
1144 /* Find a resource in the parent where we can allocate */
1145 for (i = 0 ; i < 4; i++) {
1146 struct resource *r = bus->resource[i];
1149 if ((r->flags & IORESOURCE_IO) == 0)
1151 DBG("Trying to allocate from %016llx, size %016llx from parent"
1152 " res %d: %016llx -> %016llx\n",
1153 res->start, res->end, i, r->start, r->end);
1155 if (allocate_resource(r, res, res->end + 1, res->start, max,
1156 res->end + 1, NULL, NULL) < 0) {
1160 do_update_p2p_io_resource(b, found_vga);
1164 do_fixup_p2p_level(b);
1169 pcibios_fixup_p2p_bridges(void)
1173 list_for_each_entry(b, &pci_root_buses, node)
1174 do_fixup_p2p_level(b);
1177 #endif /* CONFIG_PPC_PMAC */
1182 struct pci_controller *hose, *tmp;
1183 struct pci_bus *bus;
1186 printk(KERN_INFO "PCI: Probing PCI hardware\n");
1188 /* Scan all of the recorded PCI controllers. */
1189 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1190 if (pci_assign_all_buses)
1191 hose->first_busno = next_busno;
1192 hose->last_busno = 0xff;
1193 bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
1196 pci_bus_add_devices(bus);
1197 hose->last_busno = bus->subordinate;
1198 if (pci_assign_all_buses || next_busno <= hose->last_busno)
1199 next_busno = hose->last_busno + pcibios_assign_bus_offset;
1201 pci_bus_count = next_busno;
1203 /* OpenFirmware based machines need a map of OF bus
1204 * numbers vs. kernel bus numbers since we may have to
1207 if (pci_assign_all_buses && have_of)
1208 pcibios_make_OF_bus_map();
1210 /* Call machine dependent fixup */
1211 if (ppc_md.pcibios_fixup)
1212 ppc_md.pcibios_fixup();
1214 /* Allocate and assign resources */
1215 pcibios_allocate_bus_resources(&pci_root_buses);
1216 pcibios_allocate_resources(0);
1217 pcibios_allocate_resources(1);
1218 #ifdef CONFIG_PPC_PMAC
1219 pcibios_fixup_p2p_bridges();
1220 #endif /* CONFIG_PPC_PMAC */
1221 pcibios_assign_resources();
1223 /* Call machine dependent post-init code */
1224 if (ppc_md.pcibios_after_init)
1225 ppc_md.pcibios_after_init();
1230 subsys_initcall(pcibios_init);
1232 void __init pcibios_fixup_bus(struct pci_bus *bus)
1234 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1235 unsigned long io_offset;
1236 struct resource *res;
1237 struct pci_dev *dev;
1240 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1241 if (bus->parent == NULL) {
1242 /* This is a host bridge - fill in its resources */
1245 bus->resource[0] = res = &hose->io_resource;
1248 printk(KERN_ERR "I/O resource not set for host"
1249 " bridge %d\n", hose->global_number);
1251 res->end = IO_SPACE_LIMIT;
1252 res->flags = IORESOURCE_IO;
1254 res->start += io_offset;
1255 res->end += io_offset;
1257 for (i = 0; i < 3; ++i) {
1258 res = &hose->mem_resources[i];
1262 printk(KERN_ERR "Memory resource not set for "
1263 "host bridge %d\n", hose->global_number);
1264 res->start = hose->pci_mem_offset;
1266 res->flags = IORESOURCE_MEM;
1268 bus->resource[i+1] = res;
1271 /* This is a subordinate bridge */
1272 pci_read_bridge_bases(bus);
1274 for (i = 0; i < 4; ++i) {
1275 if ((res = bus->resource[i]) == NULL)
1277 if (!res->flags || bus->self->transparent)
1279 if (io_offset && (res->flags & IORESOURCE_IO)) {
1280 res->start += io_offset;
1281 res->end += io_offset;
1282 } else if (hose->pci_mem_offset
1283 && (res->flags & IORESOURCE_MEM)) {
1284 res->start += hose->pci_mem_offset;
1285 res->end += hose->pci_mem_offset;
1290 /* Platform specific bus fixups */
1291 if (ppc_md.pcibios_fixup_bus)
1292 ppc_md.pcibios_fixup_bus(bus);
1294 /* Read default IRQs and fixup if necessary */
1295 list_for_each_entry(dev, &bus->devices, bus_list) {
1296 pci_read_irq_line(dev);
1297 if (ppc_md.pci_irq_fixup)
1298 ppc_md.pci_irq_fixup(dev);
1302 /* the next one is stolen from the alpha port... */
1304 pcibios_update_irq(struct pci_dev *dev, int irq)
1306 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1307 /* XXX FIXME - update OF device tree node interrupt property */
1310 int pcibios_enable_device(struct pci_dev *dev, int mask)
1316 if (ppc_md.pcibios_enable_device_hook)
1317 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1320 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1322 for (idx=0; idx<6; idx++) {
1323 r = &dev->resource[idx];
1324 if (r->flags & IORESOURCE_UNSET) {
1325 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1328 if (r->flags & IORESOURCE_IO)
1329 cmd |= PCI_COMMAND_IO;
1330 if (r->flags & IORESOURCE_MEM)
1331 cmd |= PCI_COMMAND_MEMORY;
1333 if (cmd != old_cmd) {
1334 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1335 pci_name(dev), old_cmd, cmd);
1336 pci_write_config_word(dev, PCI_COMMAND, cmd);
1341 static struct pci_controller*
1342 pci_bus_to_hose(int bus)
1344 struct pci_controller *hose, *tmp;
1346 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
1347 if (bus >= hose->first_busno && bus <= hose->last_busno)
1352 /* Provide information on locations of various I/O regions in physical
1353 * memory. Do this on a per-card basis so that we choose the right
1355 * Note that the returned IO or memory base is a physical address
1358 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1360 struct pci_controller* hose;
1361 long result = -EOPNOTSUPP;
1363 /* Argh ! Please forgive me for that hack, but that's the
1364 * simplest way to get existing XFree to not lockup on some
1365 * G5 machines... So when something asks for bus 0 io base
1366 * (bus 0 is HT root), we return the AGP one instead.
1368 #ifdef CONFIG_PPC_PMAC
1369 if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
1372 #endif /* CONFIG_PPC_PMAC */
1374 hose = pci_bus_to_hose(bus);
1379 case IOBASE_BRIDGE_NUMBER:
1380 return (long)hose->first_busno;
1382 return (long)hose->pci_mem_offset;
1384 return (long)hose->io_base_phys;
1386 return (long)isa_io_base;
1387 case IOBASE_ISA_MEM:
1388 return (long)isa_mem_base;
1394 unsigned long pci_address_to_pio(phys_addr_t address)
1396 struct pci_controller *hose, *tmp;
1398 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1399 unsigned int size = hose->io_resource.end -
1400 hose->io_resource.start + 1;
1401 if (address >= hose->io_base_phys &&
1402 address < (hose->io_base_phys + size)) {
1403 unsigned long base =
1404 (unsigned long)hose->io_base_virt - _IO_BASE;
1405 return base + (address - hose->io_base_phys);
1408 return (unsigned int)-1;
1410 EXPORT_SYMBOL(pci_address_to_pio);
1413 * Null PCI config access functions, for the case when we can't
1416 #define NULL_PCI_OP(rw, size, type) \
1418 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1420 return PCIBIOS_DEVICE_NOT_FOUND; \
1424 null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1427 return PCIBIOS_DEVICE_NOT_FOUND;
1431 null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1434 return PCIBIOS_DEVICE_NOT_FOUND;
1437 static struct pci_ops null_pci_ops =
1444 * These functions are used early on before PCI scanning is done
1445 * and all of the pci_dev and pci_bus structures have been created.
1447 static struct pci_bus *
1448 fake_pci_bus(struct pci_controller *hose, int busnr)
1450 static struct pci_bus bus;
1453 hose = pci_bus_to_hose(busnr);
1455 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1459 bus.ops = hose? hose->ops: &null_pci_ops;
1463 #define EARLY_PCI_OP(rw, size, type) \
1464 int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1465 int devfn, int offset, type value) \
1467 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1468 devfn, offset, value); \
1471 EARLY_PCI_OP(read, byte, u8 *)
1472 EARLY_PCI_OP(read, word, u16 *)
1473 EARLY_PCI_OP(read, dword, u32 *)
1474 EARLY_PCI_OP(write, byte, u8)
1475 EARLY_PCI_OP(write, word, u16)
1476 EARLY_PCI_OP(write, dword, u32)
1478 extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
1479 int early_find_capability(struct pci_controller *hose, int bus, int devfn,
1482 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);