📄 pci.c
字号:
/* * Common pmac/prep/chrp pci routines. -- Cort */#include <linux/config.h>#include <linux/kernel.h>#include <linux/pci.h>#include <linux/delay.h>#include <linux/string.h>#include <linux/init.h>#include <linux/capability.h>#include <linux/sched.h>#include <linux/errno.h>#include <linux/bootmem.h>#include <asm/processor.h>#include <asm/io.h>#include <asm/prom.h>#include <asm/sections.h>#include <asm/pci-bridge.h>#include <asm/byteorder.h>#include <asm/irq.h>#include <asm/uaccess.h>#undef DEBUG#ifdef DEBUG#define DBG(x...) printk(x)#else#define DBG(x...)#endifunsigned long isa_io_base = 0;unsigned long isa_mem_base = 0;unsigned long pci_dram_offset = 0;void pcibios_make_OF_bus_map(void);static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);static int probe_resource(struct pci_bus *parent, struct resource *pr, struct resource *res, struct resource **conflict);static void update_bridge_base(struct pci_bus *bus, int i);static void pcibios_fixup_resources(struct pci_dev* dev);static void fixup_broken_pcnet32(struct pci_dev* dev);static int reparent_resources(struct resource *parent, struct resource *res);static void fixup_rev1_53c810(struct pci_dev* dev);static void fixup_cpc710_pci64(struct pci_dev* dev);#ifdef CONFIG_ALL_PPCstatic void pcibios_fixup_cardbus(struct pci_dev* dev);static u8* pci_to_OF_bus_map;#endif/* By default, we don't re-assign bus numbers. We do this only on * some pmacs */int pci_assign_all_busses;struct pci_controller* hose_head;struct pci_controller** hose_tail = &hose_head;static int pci_bus_count;struct pci_fixup pcibios_fixups[] = { { PCI_FIXUP_HEADER, PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32 }, { PCI_FIXUP_HEADER, PCI_VENDOR_ID_NCR, PCI_DEVICE_ID_NCR_53C810, fixup_rev1_53c810 }, { PCI_FIXUP_HEADER, PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64}, { PCI_FIXUP_HEADER, PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources },#ifdef CONFIG_ALL_PPC /* We should add per-machine fixup support in xxx_setup.c or xxx_pci.c */ { PCI_FIXUP_FINAL, PCI_VENDOR_ID_TI, PCI_ANY_ID, pcibios_fixup_cardbus }, #endif /* CONFIG_ALL_PPC */ { 0 }};static voidfixup_rev1_53c810(struct pci_dev* dev){ /* rev 1 ncr53c810 chips don't set the class at all which means * they don't get their resources remapped. Fix that here. */ if ((dev->class == PCI_CLASS_NOT_DEFINED)) { printk("NCR 53c810 rev 1 detected, setting PCI class.\n"); dev->class = PCI_CLASS_STORAGE_SCSI; }}static voidfixup_broken_pcnet32(struct pci_dev* dev){ if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) { dev->vendor = PCI_VENDOR_ID_AMD; pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD); pci_name_device(dev); }}static voidfixup_cpc710_pci64(struct pci_dev* dev){ /* Hide the PCI64 BARs from the kernel as their content doesn't * fit well in the resource management */ dev->resource[0].start = dev->resource[0].end = 0; dev->resource[0].flags = 0; dev->resource[1].start = dev->resource[1].end = 0; dev->resource[1].flags = 0;}voidpcibios_update_resource(struct pci_dev *dev, struct resource *root, struct resource *res, int resource){ u32 new, check; int reg; struct pci_controller* hose = dev->sysdata; unsigned long io_offset; new = res->start; res->flags &= ~IORESOURCE_UNSET; if (hose && res->flags & IORESOURCE_IO) { io_offset = (unsigned long)hose->io_base_virt - isa_io_base; new -= io_offset; } if (hose && res->flags & IORESOURCE_MEM) new -= hose->pci_mem_offset; new |= (res->flags & PCI_REGION_FLAG_MASK); if (resource < 6) { reg = PCI_BASE_ADDRESS_0 + 4*resource; } else if (resource == PCI_ROM_RESOURCE) { res->flags |= PCI_ROM_ADDRESS_ENABLE; reg = dev->rom_base_reg; } else { /* Somebody might have asked allocation of a non-standard resource */ return; } pci_write_config_dword(dev, reg, new); pci_read_config_dword(dev, reg, &check); if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) { printk(KERN_ERR "PCI: Error while updating region " "%s/%d (%08x != %08x)\n", dev->slot_name, resource, new, check); } printk(KERN_INFO "PCI: moved device %s resource %d (%lx) to %x\n", dev->slot_name, resource, res->flags, new & ~PCI_REGION_FLAG_MASK);}static voidpcibios_fixup_resources(struct pci_dev *dev){ struct pci_controller* hose = (struct pci_controller *)dev->sysdata; int i; unsigned long offset; if (!hose) { printk(KERN_ERR "No hose for PCI dev %s!\n", dev->slot_name); return; } for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { struct resource *res = dev->resource + i; if (!res->flags) continue; if (!res->start || res->end == 0xffffffff) { DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n", dev->slot_name, i, res->start, res->end); res->end -= res->start; res->start = 0; res->flags |= IORESOURCE_UNSET; continue; } offset = 0; if (res->flags & IORESOURCE_MEM) { offset = hose->pci_mem_offset; } else if (res->flags & IORESOURCE_IO) { offset = (unsigned long) hose->io_base_virt - isa_io_base; } if (offset != 0) { res->start += offset; res->end += offset;#ifdef DEBUG printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n", i, res->flags, dev->slot_name, res->start - offset, res->start);#endif } } /* Call machine specific resource fixup */ if (ppc_md.pcibios_fixup_resources) ppc_md.pcibios_fixup_resources(dev);}#ifdef CONFIG_ALL_PPCstatic voidpcibios_fixup_cardbus(struct pci_dev* dev){ if (_machine != _MACH_Pmac) return; /* * Fix the interrupt routing on the various cardbus bridges * used on powerbooks */ if (dev->vendor != PCI_VENDOR_ID_TI) return; if (dev->device == PCI_DEVICE_ID_TI_1130 || dev->device == PCI_DEVICE_ID_TI_1131) { u8 val; /* Enable PCI interrupt */ if (pci_read_config_byte(dev, 0x91, &val) == 0) pci_write_config_byte(dev, 0x91, val | 0x30); /* Disable ISA interrupt mode */ if (pci_read_config_byte(dev, 0x92, &val) == 0) pci_write_config_byte(dev, 0x92, val & ~0x06); } if (dev->device == PCI_DEVICE_ID_TI_1210 || dev->device == PCI_DEVICE_ID_TI_1211 || dev->device == PCI_DEVICE_ID_TI_1410) { u8 val; /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA signal out the MFUNC0 pin */ if (pci_read_config_byte(dev, 0x8c, &val) == 0) pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2); /* Disable ISA interrupt mode */ if (pci_read_config_byte(dev, 0x92, &val) == 0) pci_write_config_byte(dev, 0x92, val & ~0x06); }}#endif /* CONFIG_ALL_PPC *//* * We need to avoid collisions with `mirrored' VGA ports * and other strange ISA hardware, so we always want the * addresses to be allocated in the 0x000-0x0ff region * modulo 0x400. * * Why? Because some silly external IO cards only decode * the low 10 bits of the IO address. The 0x00-0xff region * is reserved for motherboard devices that decode all 16 * bits, so it's ok to allocate at, say, 0x2800-0x28ff, * but we want to try to avoid allocating at 0x2900-0x2bff * which might have be mirrored at 0x0100-0x03ff.. */voidpcibios_align_resource(void *data, struct resource *res, unsigned long size, unsigned long align){ struct pci_dev *dev = data; if (res->flags & IORESOURCE_IO) { unsigned long start = res->start; if (size > 0x100) { printk(KERN_ERR "PCI: I/O Region %s/%d too large" " (%ld bytes)\n", dev->slot_name, dev->resource - res, size); } if (start & 0x300) { start = (start + 0x3ff) & ~0x3ff; res->start = start; } }}/* * Handle resources of PCI devices. If the world were perfect, we could * just allocate all the resource regions and do nothing more. It isn't. * On the other hand, we cannot just re-allocate all devices, as it would * require us to know lots of host bridge internals. So we attempt to * keep as much of the original configuration as possible, but tweak it * when it's found to be wrong. * * Known BIOS problems we have to work around: * - I/O or memory regions not configured * - regions configured, but not enabled in the command register * - bogus I/O addresses above 64K used * - expansion ROMs left enabled (this may sound harmless, but given * the fact the PCI specs explicitly allow address decoders to be * shared between expansion ROMs and other resource regions, it's * at least dangerous) * * Our solution: * (1) Allocate resources for all buses behind PCI-to-PCI bridges. * This gives us fixed barriers on where we can allocate. * (2) Allocate resources for all enabled devices. If there is * a collision, just mark the resource as unallocated. Also * disable expansion ROMs during this step. * (3) Try to allocate resources for disabled devices. If the * resources were assigned correctly, everything goes well, * if they weren't, they won't disturb allocation of other * resources. * (4) Assign new addresses to resources which were either * not configured at all or misconfigured. If explicitly * requested by the user, configure expansion ROM address * as well. */static void __initpcibios_allocate_bus_resources(struct list_head *bus_list){ struct list_head *ln; struct pci_bus *bus; int i; struct resource *res, *pr; /* Depth-First Search on bus tree */ for (ln = bus_list->next; ln != bus_list; ln=ln->next) { bus = pci_bus_b(ln); for (i = 0; i < 4; ++i) { if ((res = bus->resource[i]) == NULL || !res->flags || res->start > res->end) continue; if (bus->parent == NULL) pr = (res->flags & IORESOURCE_IO)? &ioport_resource: &iomem_resource; else { pr = pci_find_parent_resource(bus->self, res); if (pr == res) { /* this happens when the generic PCI * code (wrongly) decides that this * bridge is transparent -- paulus */ continue; } } DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n", res->start, res->end, res->flags, pr); if (pr) { if (request_resource(pr, res) == 0) continue; /* * Must be a conflict with an existing entry. * Move that entry (or entries) under the * bridge resource and try again. */ if (reparent_resources(pr, res) == 0) continue; } printk(KERN_ERR "PCI: Cannot allocate resource region " "%d of PCI bridge %d\n", i, bus->number); if (pci_relocate_bridge_resource(bus, i)) bus->resource[i] = NULL; } pcibios_allocate_bus_resources(&bus->children); }}/* * Reparent resource children of pr that conflict with res * under res, and make res replace those children. */static int __initreparent_resources(struct resource *parent, struct resource *res){ struct resource *p, **pp; struct resource **firstpp = NULL; for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) { if (p->end < res->start) continue; if (res->end < p->start) break; if (p->start < res->start || p->end > res->end) return -1; /* not completely contained */ if (firstpp == NULL) firstpp = pp; } if (firstpp == NULL) return -1; /* didn't find any conflicting entries? */ res->parent = parent; res->child = *firstpp; res->sibling = *pp; *firstpp = res; *pp = NULL; for (p = res->child; p != NULL; p = p->sibling) { p->parent = res; DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n", p->name, p->start, p->end, res->name); } return 0;}/* * A bridge has been allocated a range which is outside the range * of its parent bridge, so it needs to be moved. */static int __initpci_relocate_bridge_resource(struct pci_bus *bus, int i){ struct resource *res, *pr, *conflict; unsigned long try, size; int j; struct pci_bus *parent = bus->parent; if (parent == NULL) { /* shouldn't ever happen */ printk(KERN_ERR "PCI: can't move host bridge resource\n"); return -1; } res = bus->resource[i]; pr = NULL; for (j = 0; j < 4; j++) { struct resource *r = parent->resource[j]; if (!r) continue; if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM)) continue; if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) { pr = r; break; } if (res->flags & IORESOURCE_PREFETCH) pr = r; } if (pr == NULL) return -1; size = res->end - res->start; if (pr->start > pr->end || size > pr->end - pr->start) return -1; try = pr->end + 1; for (;;) { try &= ~0xfffUL; if (res->flags & IORESOURCE_MEM) try &= ~0xfffffUL; res->start = try - size - 1; res->end = try - 1; if (try <= size || res->start < pr->start) return -1; if (probe_resource(bus->parent, pr, res, &conflict) == 0) break; if (conflict->start <= pr->start + size) return -1; try = conflict->start; } if (request_resource(pr, res)) { DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n", res->start, res->end); return -1; /* "can't happen" */ } update_bridge_base(bus, i); printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n", bus->number, i, res->start, res->end); return 0;}static int __initprobe_resource(struct pci_bus *parent, struct resource *pr,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -