2 * Common prep/chrp pci routines. -- Cort
5 #include <linux/config.h>
6 #include <linux/kernel.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>
16 #include <asm/processor.h>
19 #include <asm/sections.h>
20 #include <asm/pci-bridge.h>
21 #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 /* By default, we don't re-assign bus numbers.
52 int pci_assign_all_buses;
54 struct pci_controller* hose_head;
55 struct pci_controller** hose_tail = &hose_head;
57 static int pci_bus_count;
60 fixup_broken_pcnet32(struct pci_dev* dev)
62 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
63 dev->vendor = PCI_VENDOR_ID_AMD;
64 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
67 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
70 fixup_cpc710_pci64(struct pci_dev* dev)
72 /* Hide the PCI64 BARs from the kernel as their content doesn't
73 * fit well in the resource management
75 dev->resource[0].start = dev->resource[0].end = 0;
76 dev->resource[0].flags = 0;
77 dev->resource[1].start = dev->resource[1].end = 0;
78 dev->resource[1].flags = 0;
80 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
83 pcibios_fixup_resources(struct pci_dev *dev)
85 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
90 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
93 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
94 struct resource *res = dev->resource + i;
97 if (res->end == 0xffffffff) {
98 DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
100 (unsigned long long)res->start,
101 (unsigned long long)res->end);
102 res->end -= res->start;
104 res->flags |= IORESOURCE_UNSET;
108 if (res->flags & IORESOURCE_MEM) {
109 offset = hose->pci_mem_offset;
110 } else if (res->flags & IORESOURCE_IO) {
111 offset = (unsigned long) hose->io_base_virt
115 res->start += offset;
118 printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
119 i, res->flags, pci_name(dev),
120 res->start - offset, res->start);
125 /* Call machine specific resource fixup */
126 if (ppc_md.pcibios_fixup_resources)
127 ppc_md.pcibios_fixup_resources(dev);
129 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
131 void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
132 struct resource *res)
134 unsigned long offset = 0;
135 struct pci_controller *hose = dev->sysdata;
137 if (hose && res->flags & IORESOURCE_IO)
138 offset = (unsigned long)hose->io_base_virt - isa_io_base;
139 else if (hose && res->flags & IORESOURCE_MEM)
140 offset = hose->pci_mem_offset;
141 region->start = res->start - offset;
142 region->end = res->end - offset;
144 EXPORT_SYMBOL(pcibios_resource_to_bus);
146 void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
147 struct pci_bus_region *region)
149 unsigned long offset = 0;
150 struct pci_controller *hose = dev->sysdata;
152 if (hose && res->flags & IORESOURCE_IO)
153 offset = (unsigned long)hose->io_base_virt - isa_io_base;
154 else if (hose && res->flags & IORESOURCE_MEM)
155 offset = hose->pci_mem_offset;
156 res->start = region->start + offset;
157 res->end = region->end + offset;
159 EXPORT_SYMBOL(pcibios_bus_to_resource);
162 * We need to avoid collisions with `mirrored' VGA ports
163 * and other strange ISA hardware, so we always want the
164 * addresses to be allocated in the 0x000-0x0ff region
167 * Why? Because some silly external IO cards only decode
168 * the low 10 bits of the IO address. The 0x00-0xff region
169 * is reserved for motherboard devices that decode all 16
170 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
171 * but we want to try to avoid allocating at 0x2900-0x2bff
172 * which might have be mirrored at 0x0100-0x03ff..
174 void pcibios_align_resource(void *data, struct resource *res, unsigned long size,
177 struct pci_dev *dev = data;
179 if (res->flags & IORESOURCE_IO) {
180 unsigned long start = res->start;
183 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
184 " (%lld bytes)\n", pci_name(dev),
185 dev->resource - res, (unsigned long long)size);
189 start = (start + 0x3ff) & ~0x3ff;
194 EXPORT_SYMBOL(pcibios_align_resource);
197 * Handle resources of PCI devices. If the world were perfect, we could
198 * just allocate all the resource regions and do nothing more. It isn't.
199 * On the other hand, we cannot just re-allocate all devices, as it would
200 * require us to know lots of host bridge internals. So we attempt to
201 * keep as much of the original configuration as possible, but tweak it
202 * when it's found to be wrong.
204 * Known BIOS problems we have to work around:
205 * - I/O or memory regions not configured
206 * - regions configured, but not enabled in the command register
207 * - bogus I/O addresses above 64K used
208 * - expansion ROMs left enabled (this may sound harmless, but given
209 * the fact the PCI specs explicitly allow address decoders to be
210 * shared between expansion ROMs and other resource regions, it's
211 * at least dangerous)
214 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
215 * This gives us fixed barriers on where we can allocate.
216 * (2) Allocate resources for all enabled devices. If there is
217 * a collision, just mark the resource as unallocated. Also
218 * disable expansion ROMs during this step.
219 * (3) Try to allocate resources for disabled devices. If the
220 * resources were assigned correctly, everything goes well,
221 * if they weren't, they won't disturb allocation of other
223 * (4) Assign new addresses to resources which were either
224 * not configured at all or misconfigured. If explicitly
225 * requested by the user, configure expansion ROM address
230 pcibios_allocate_bus_resources(struct list_head *bus_list)
234 struct resource *res, *pr;
236 /* Depth-First Search on bus tree */
237 list_for_each_entry(bus, bus_list, node) {
238 for (i = 0; i < 4; ++i) {
239 if ((res = bus->resource[i]) == NULL || !res->flags
240 || res->start > res->end)
242 if (bus->parent == NULL)
243 pr = (res->flags & IORESOURCE_IO)?
244 &ioport_resource: &iomem_resource;
246 pr = pci_find_parent_resource(bus->self, res);
248 /* this happens when the generic PCI
249 * code (wrongly) decides that this
250 * bridge is transparent -- paulus
256 DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
257 (unsigned long long)res->start,
258 (unsigned long long)res->end, res->flags, pr);
260 if (request_resource(pr, res) == 0)
263 * Must be a conflict with an existing entry.
264 * Move that entry (or entries) under the
265 * bridge resource and try again.
267 if (reparent_resources(pr, res) == 0)
270 printk(KERN_ERR "PCI: Cannot allocate resource region "
271 "%d of PCI bridge %d\n", i, bus->number);
272 if (pci_relocate_bridge_resource(bus, i))
273 bus->resource[i] = NULL;
275 pcibios_allocate_bus_resources(&bus->children);
280 * Reparent resource children of pr that conflict with res
281 * under res, and make res replace those children.
284 reparent_resources(struct resource *parent, struct resource *res)
286 struct resource *p, **pp;
287 struct resource **firstpp = NULL;
289 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
290 if (p->end < res->start)
292 if (res->end < p->start)
294 if (p->start < res->start || p->end > res->end)
295 return -1; /* not completely contained */
300 return -1; /* didn't find any conflicting entries? */
301 res->parent = parent;
302 res->child = *firstpp;
306 for (p = res->child; p != NULL; p = p->sibling) {
308 DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
309 p->name, (unsigned long long)p->start,
310 (unsigned long long)p->end, res->name);
316 * A bridge has been allocated a range which is outside the range
317 * of its parent bridge, so it needs to be moved.
320 pci_relocate_bridge_resource(struct pci_bus *bus, int i)
322 struct resource *res, *pr, *conflict;
323 unsigned long try, size;
325 struct pci_bus *parent = bus->parent;
327 if (parent == NULL) {
328 /* shouldn't ever happen */
329 printk(KERN_ERR "PCI: can't move host bridge resource\n");
332 res = bus->resource[i];
336 for (j = 0; j < 4; j++) {
337 struct resource *r = parent->resource[j];
340 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
342 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
346 if (res->flags & IORESOURCE_PREFETCH)
351 size = res->end - res->start;
352 if (pr->start > pr->end || size > pr->end - pr->start)
356 res->start = try - size;
358 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
360 if (conflict->start <= pr->start + size)
362 try = conflict->start - 1;
364 if (request_resource(pr, res)) {
365 DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n",
366 (unsigned long long)res->start,
367 (unsigned long long)res->end);
368 return -1; /* "can't happen" */
370 update_bridge_base(bus, i);
371 printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n",
372 bus->number, i, (unsigned long long)res->start,
373 (unsigned long long)res->end);
378 probe_resource(struct pci_bus *parent, struct resource *pr,
379 struct resource *res, struct resource **conflict)
386 for (r = pr->child; r != NULL; r = r->sibling) {
387 if (r->end >= res->start && res->end >= r->start) {
392 list_for_each_entry(bus, &parent->children, node) {
393 for (i = 0; i < 4; ++i) {
394 if ((r = bus->resource[i]) == NULL)
396 if (!r->flags || r->start > r->end || r == res)
398 if (pci_find_parent_resource(bus->self, r) != pr)
400 if (r->end >= res->start && res->end >= r->start) {
406 list_for_each_entry(dev, &parent->devices, bus_list) {
407 for (i = 0; i < 6; ++i) {
408 r = &dev->resource[i];
409 if (!r->flags || (r->flags & IORESOURCE_UNSET))
411 if (pci_find_parent_resource(dev, r) != pr)
413 if (r->end >= res->start && res->end >= r->start) {
423 update_bridge_base(struct pci_bus *bus, int i)
425 struct resource *res = bus->resource[i];
426 u8 io_base_lo, io_limit_lo;
427 u16 mem_base, mem_limit;
429 unsigned long start, end, off;
430 struct pci_dev *dev = bus->self;
431 struct pci_controller *hose = dev->sysdata;
434 printk("update_bridge_base: no hose?\n");
437 pci_read_config_word(dev, PCI_COMMAND, &cmd);
438 pci_write_config_word(dev, PCI_COMMAND,
439 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
440 if (res->flags & IORESOURCE_IO) {
441 off = (unsigned long) hose->io_base_virt - isa_io_base;
442 start = res->start - off;
443 end = res->end - off;
444 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
445 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
447 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
449 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
451 io_base_lo |= PCI_IO_RANGE_TYPE_32;
453 io_base_lo |= PCI_IO_RANGE_TYPE_16;
454 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
455 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
457 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
459 off = hose->pci_mem_offset;
460 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
461 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
462 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
463 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
465 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
466 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
467 off = hose->pci_mem_offset;
468 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
469 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
470 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
471 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
474 DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
475 pci_name(dev), i, res->flags);
477 pci_write_config_word(dev, PCI_COMMAND, cmd);
480 static inline void alloc_resource(struct pci_dev *dev, int idx)
482 struct resource *pr, *r = &dev->resource[idx];
484 DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
485 pci_name(dev), idx, (unsigned long long)r->start,
486 (unsigned long long)r->end, r->flags);
487 pr = pci_find_parent_resource(dev, r);
488 if (!pr || request_resource(pr, r) < 0) {
489 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
490 " of device %s\n", idx, pci_name(dev));
492 DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
493 pr, (unsigned long long)pr->start,
494 (unsigned long long)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 pcibios_enable_resources(struct pci_dev *dev, int mask)
589 pci_read_config_word(dev, PCI_COMMAND, &cmd);
591 for (idx=0; idx<6; idx++) {
592 /* Only set up the requested stuff */
593 if (!(mask & (1<<idx)))
596 r = &dev->resource[idx];
597 if (r->flags & IORESOURCE_UNSET) {
598 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
601 if (r->flags & IORESOURCE_IO)
602 cmd |= PCI_COMMAND_IO;
603 if (r->flags & IORESOURCE_MEM)
604 cmd |= PCI_COMMAND_MEMORY;
606 if (dev->resource[PCI_ROM_RESOURCE].start)
607 cmd |= PCI_COMMAND_MEMORY;
608 if (cmd != old_cmd) {
609 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
610 pci_write_config_word(dev, PCI_COMMAND, cmd);
615 static int next_controller_index;
617 struct pci_controller * __init
618 pcibios_alloc_controller(void)
620 struct pci_controller *hose;
622 hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
623 memset(hose, 0, sizeof(struct pci_controller));
626 hose_tail = &hose->next;
628 hose->index = next_controller_index++;
633 void pcibios_make_OF_bus_map(void)
637 /* Add sysfs properties */
638 void pcibios_add_platform_entries(struct pci_dev *pdev)
646 struct pci_controller *hose;
650 printk(KERN_INFO "PCI: Probing PCI hardware\n");
652 /* Scan all of the recorded PCI controllers. */
653 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
654 if (pci_assign_all_buses)
655 hose->first_busno = next_busno;
656 hose->last_busno = 0xff;
657 bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
658 hose->last_busno = bus->subordinate;
659 if (pci_assign_all_buses || next_busno <= hose->last_busno)
660 next_busno = hose->last_busno + pcibios_assign_bus_offset;
662 pci_bus_count = next_busno;
664 /* OpenFirmware based machines need a map of OF bus
665 * numbers vs. kernel bus numbers since we may have to
668 if (pci_assign_all_buses && have_of)
669 pcibios_make_OF_bus_map();
671 /* Do machine dependent PCI interrupt routing */
672 if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
673 pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
675 /* Call machine dependent fixup */
676 if (ppc_md.pcibios_fixup)
677 ppc_md.pcibios_fixup();
679 /* Allocate and assign resources */
680 pcibios_allocate_bus_resources(&pci_root_buses);
681 pcibios_allocate_resources(0);
682 pcibios_allocate_resources(1);
683 pcibios_assign_resources();
685 /* Call machine dependent post-init code */
686 if (ppc_md.pcibios_after_init)
687 ppc_md.pcibios_after_init();
692 subsys_initcall(pcibios_init);
695 common_swizzle(struct pci_dev *dev, unsigned char *pinp)
697 struct pci_controller *hose = dev->sysdata;
699 if (dev->bus->number != hose->first_busno) {
702 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
703 /* Move up the chain of bridges. */
704 dev = dev->bus->self;
705 } while (dev->bus->self);
708 /* The slot is the idsel of the last bridge. */
710 return PCI_SLOT(dev->devfn);
713 unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
714 unsigned long start, unsigned long size)
719 void __init pcibios_fixup_bus(struct pci_bus *bus)
721 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
722 unsigned long io_offset;
723 struct resource *res;
726 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
727 if (bus->parent == NULL) {
728 /* This is a host bridge - fill in its resources */
731 bus->resource[0] = res = &hose->io_resource;
734 printk(KERN_ERR "I/O resource not set for host"
735 " bridge %d\n", hose->index);
737 res->end = IO_SPACE_LIMIT;
738 res->flags = IORESOURCE_IO;
740 res->start += io_offset;
741 res->end += io_offset;
743 for (i = 0; i < 3; ++i) {
744 res = &hose->mem_resources[i];
748 printk(KERN_ERR "Memory resource not set for "
749 "host bridge %d\n", hose->index);
750 res->start = hose->pci_mem_offset;
752 res->flags = IORESOURCE_MEM;
754 bus->resource[i+1] = res;
757 /* This is a subordinate bridge */
758 pci_read_bridge_bases(bus);
760 for (i = 0; i < 4; ++i) {
761 if ((res = bus->resource[i]) == NULL)
765 if (io_offset && (res->flags & IORESOURCE_IO)) {
766 res->start += io_offset;
767 res->end += io_offset;
768 } else if (hose->pci_mem_offset
769 && (res->flags & IORESOURCE_MEM)) {
770 res->start += hose->pci_mem_offset;
771 res->end += hose->pci_mem_offset;
776 if (ppc_md.pcibios_fixup_bus)
777 ppc_md.pcibios_fixup_bus(bus);
780 char __init *pcibios_setup(char *str)
785 /* the next one is stolen from the alpha port... */
787 pcibios_update_irq(struct pci_dev *dev, int irq)
789 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
790 /* XXX FIXME - update OF device tree node interrupt property */
793 int pcibios_enable_device(struct pci_dev *dev, int mask)
799 if (ppc_md.pcibios_enable_device_hook)
800 if (ppc_md.pcibios_enable_device_hook(dev, 0))
803 pci_read_config_word(dev, PCI_COMMAND, &cmd);
805 for (idx=0; idx<6; idx++) {
806 r = &dev->resource[idx];
807 if (r->flags & IORESOURCE_UNSET) {
808 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
811 if (r->flags & IORESOURCE_IO)
812 cmd |= PCI_COMMAND_IO;
813 if (r->flags & IORESOURCE_MEM)
814 cmd |= PCI_COMMAND_MEMORY;
816 if (cmd != old_cmd) {
817 printk("PCI: Enabling device %s (%04x -> %04x)\n",
818 pci_name(dev), old_cmd, cmd);
819 pci_write_config_word(dev, PCI_COMMAND, cmd);
824 struct pci_controller*
825 pci_bus_to_hose(int bus)
827 struct pci_controller* hose = hose_head;
829 for (; hose; hose = hose->next)
830 if (bus >= hose->first_busno && bus <= hose->last_busno)
836 pci_bus_io_base(unsigned int bus)
838 struct pci_controller *hose;
840 hose = pci_bus_to_hose(bus);
843 return hose->io_base_virt;
847 pci_bus_io_base_phys(unsigned int bus)
849 struct pci_controller *hose;
851 hose = pci_bus_to_hose(bus);
854 return hose->io_base_phys;
858 pci_bus_mem_base_phys(unsigned int bus)
860 struct pci_controller *hose;
862 hose = pci_bus_to_hose(bus);
865 return hose->pci_mem_offset;
869 pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
871 /* Hack alert again ! See comments in chrp_pci.c
873 struct pci_controller* hose =
874 (struct pci_controller *)pdev->sysdata;
875 if (hose && res->flags & IORESOURCE_MEM)
876 return res->start - hose->pci_mem_offset;
877 /* We may want to do something with IOs here... */
882 static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
883 unsigned long *offset,
884 enum pci_mmap_state mmap_state)
886 struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
887 unsigned long io_offset = 0;
891 return NULL; /* should never happen */
893 /* If memory, add on the PCI bridge address offset */
894 if (mmap_state == pci_mmap_mem) {
895 *offset += hose->pci_mem_offset;
896 res_bit = IORESOURCE_MEM;
898 io_offset = hose->io_base_virt - ___IO_BASE;
899 *offset += io_offset;
900 res_bit = IORESOURCE_IO;
904 * Check that the offset requested corresponds to one of the
905 * resources of the device.
907 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
908 struct resource *rp = &dev->resource[i];
909 int flags = rp->flags;
911 /* treat ROM as memory (should be already) */
912 if (i == PCI_ROM_RESOURCE)
913 flags |= IORESOURCE_MEM;
915 /* Active and same type? */
916 if ((flags & res_bit) == 0)
919 /* In the range of this resource? */
920 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
923 /* found it! construct the final physical address */
924 if (mmap_state == pci_mmap_io)
925 *offset += hose->io_base_phys - io_offset;
933 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
936 static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
938 enum pci_mmap_state mmap_state,
941 unsigned long prot = pgprot_val(protection);
943 /* Write combine is always 0 on non-memory space mappings. On
944 * memory space, if the user didn't pass 1, we check for a
945 * "prefetchable" resource. This is a bit hackish, but we use
946 * this to workaround the inability of /sysfs to provide a write
949 if (mmap_state != pci_mmap_mem)
951 else if (write_combine == 0) {
952 if (rp->flags & IORESOURCE_PREFETCH)
956 /* XXX would be nice to have a way to ask for write-through */
957 prot |= _PAGE_NO_CACHE;
959 prot &= ~_PAGE_GUARDED;
961 prot |= _PAGE_GUARDED;
963 printk("PCI map for %s:%llx, prot: %llx\n", pci_name(dev), rp->start,
966 return __pgprot(prot);
970 * This one is used by /dev/mem and fbdev who have no clue about the
971 * PCI device, it tries to find the PCI device first and calls the
974 pgprot_t pci_phys_mem_access_prot(struct file *file,
979 struct pci_dev *pdev = NULL;
980 struct resource *found = NULL;
981 unsigned long prot = pgprot_val(protection);
982 unsigned long offset = pfn << PAGE_SHIFT;
985 if (page_is_ram(pfn))
988 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
990 for_each_pci_dev(pdev) {
991 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
992 struct resource *rp = &pdev->resource[i];
993 int flags = rp->flags;
995 /* Active and same type? */
996 if ((flags & IORESOURCE_MEM) == 0)
998 /* In the range of this resource? */
999 if (offset < (rp->start & PAGE_MASK) ||
1009 if (found->flags & IORESOURCE_PREFETCH)
1010 prot &= ~_PAGE_GUARDED;
1014 DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
1016 return __pgprot(prot);
1021 * Perform the actual remap of the pages for a PCI device mapping, as
1022 * appropriate for this architecture. The region in the process to map
1023 * is described by vm_start and vm_end members of VMA, the base physical
1024 * address is found in vm_pgoff.
1025 * The pci device structure is provided so that architectures may make mapping
1026 * decisions on a per-device or per-bus basis.
1028 * Returns a negative error code on failure, zero on success.
1030 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1031 enum pci_mmap_state mmap_state,
1034 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1035 struct resource *rp;
1038 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
1042 vma->vm_pgoff = offset >> PAGE_SHIFT;
1043 vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
1045 mmap_state, write_combine);
1047 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1048 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1053 /* Obsolete functions. Should be removed once the symbios driver
1057 phys_to_bus(unsigned long pa)
1059 struct pci_controller *hose;
1062 for (hose = hose_head; hose; hose = hose->next) {
1063 for (i = 0; i < 3; ++i) {
1064 if (pa >= hose->mem_resources[i].start
1065 && pa <= hose->mem_resources[i].end) {
1067 * XXX the hose->pci_mem_offset really
1068 * only applies to mem_resources[0].
1069 * We need a way to store an offset for
1070 * the others. -- paulus
1073 pa -= hose->pci_mem_offset;
1078 /* hmmm, didn't find it */
1083 pci_phys_to_bus(unsigned long pa, int busnr)
1085 struct pci_controller* hose = pci_bus_to_hose(busnr);
1088 return pa - hose->pci_mem_offset;
1092 pci_bus_to_phys(unsigned int ba, int busnr)
1094 struct pci_controller* hose = pci_bus_to_hose(busnr);
1097 return ba + hose->pci_mem_offset;
1100 /* Provide information on locations of various I/O regions in physical
1101 * memory. Do this on a per-card basis so that we choose the right
1103 * Note that the returned IO or memory base is a physical address
1106 long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1108 struct pci_controller* hose;
1109 long result = -EOPNOTSUPP;
1111 hose = pci_bus_to_hose(bus);
1116 case IOBASE_BRIDGE_NUMBER:
1117 return (long)hose->first_busno;
1119 return (long)hose->pci_mem_offset;
1121 return (long)hose->io_base_phys;
1123 return (long)isa_io_base;
1124 case IOBASE_ISA_MEM:
1125 return (long)isa_mem_base;
1131 void pci_resource_to_user(const struct pci_dev *dev, int bar,
1132 const struct resource *rsrc,
1133 u64 *start, u64 *end)
1135 struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
1136 unsigned long offset = 0;
1141 if (rsrc->flags & IORESOURCE_IO)
1142 offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
1144 *start = rsrc->start + offset;
1145 *end = rsrc->end + offset;
1149 pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
1150 int flags, char *name)
1157 res->sibling = NULL;
1161 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
1163 unsigned long start = pci_resource_start(dev, bar);
1164 unsigned long len = pci_resource_len(dev, bar);
1165 unsigned long flags = pci_resource_flags(dev, bar);
1169 if (max && len > max)
1171 if (flags & IORESOURCE_IO)
1172 return ioport_map(start, len);
1173 if (flags & IORESOURCE_MEM)
1174 /* Not checking IORESOURCE_CACHEABLE because PPC does
1175 * not currently distinguish between ioremap and
1178 return ioremap(start, len);
1183 void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
1187 EXPORT_SYMBOL(pci_iomap);
1188 EXPORT_SYMBOL(pci_iounmap);
1190 unsigned long pci_address_to_pio(phys_addr_t address)
1192 struct pci_controller* hose = hose_head;
1194 for (; hose; hose = hose->next) {
1195 unsigned int size = hose->io_resource.end -
1196 hose->io_resource.start + 1;
1197 if (address >= hose->io_base_phys &&
1198 address < (hose->io_base_phys + size)) {
1199 unsigned long base =
1200 (unsigned long)hose->io_base_virt - _IO_BASE;
1201 return base + (address - hose->io_base_phys);
1204 return (unsigned int)-1;
1206 EXPORT_SYMBOL(pci_address_to_pio);
1209 * Null PCI config access functions, for the case when we can't
1212 #define NULL_PCI_OP(rw, size, type) \
1214 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1216 return PCIBIOS_DEVICE_NOT_FOUND; \
1220 null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1223 return PCIBIOS_DEVICE_NOT_FOUND;
1227 null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1230 return PCIBIOS_DEVICE_NOT_FOUND;
1233 static struct pci_ops null_pci_ops =
1240 * These functions are used early on before PCI scanning is done
1241 * and all of the pci_dev and pci_bus structures have been created.
1243 static struct pci_bus *
1244 fake_pci_bus(struct pci_controller *hose, int busnr)
1246 static struct pci_bus bus;
1249 hose = pci_bus_to_hose(busnr);
1251 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1255 bus.ops = hose? hose->ops: &null_pci_ops;
1259 #define EARLY_PCI_OP(rw, size, type) \
1260 int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1261 int devfn, int offset, type value) \
1263 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1264 devfn, offset, value); \
1267 EARLY_PCI_OP(read, byte, u8 *)
1268 EARLY_PCI_OP(read, word, u16 *)
1269 EARLY_PCI_OP(read, dword, u32 *)
1270 EARLY_PCI_OP(write, byte, u8)
1271 EARLY_PCI_OP(write, word, u16)
1272 EARLY_PCI_OP(write, dword, u32)