15516b540SKumar Gala /* 25516b540SKumar Gala * Contains common pci routines for ALL ppc platform 3cf1d8a8aSKumar Gala * (based on pci_32.c and pci_64.c) 4cf1d8a8aSKumar Gala * 5cf1d8a8aSKumar Gala * Port for PPC64 David Engebretsen, IBM Corp. 6cf1d8a8aSKumar Gala * Contains common pci routines for ppc64 platform, pSeries and iSeries brands. 7cf1d8a8aSKumar Gala * 8cf1d8a8aSKumar Gala * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM 9cf1d8a8aSKumar Gala * Rework, based on alpha PCI code. 10cf1d8a8aSKumar Gala * 11cf1d8a8aSKumar Gala * Common pmac/prep/chrp pci routines. -- Cort 125516b540SKumar Gala * 135516b540SKumar Gala * This program is free software; you can redistribute it and/or 145516b540SKumar Gala * modify it under the terms of the GNU General Public License 155516b540SKumar Gala * as published by the Free Software Foundation; either version 165516b540SKumar Gala * 2 of the License, or (at your option) any later version. 175516b540SKumar Gala */ 185516b540SKumar Gala 195516b540SKumar Gala #undef DEBUG 205516b540SKumar Gala 215516b540SKumar Gala #include <linux/kernel.h> 225516b540SKumar Gala #include <linux/pci.h> 235516b540SKumar Gala #include <linux/string.h> 245516b540SKumar Gala #include <linux/init.h> 255516b540SKumar Gala #include <linux/bootmem.h> 265516b540SKumar Gala #include <linux/mm.h> 275516b540SKumar Gala #include <linux/list.h> 285516b540SKumar Gala #include <linux/syscalls.h> 295516b540SKumar Gala #include <linux/irq.h> 305516b540SKumar Gala #include <linux/vmalloc.h> 315516b540SKumar Gala 325516b540SKumar Gala #include <asm/processor.h> 335516b540SKumar Gala #include <asm/io.h> 345516b540SKumar Gala #include <asm/prom.h> 355516b540SKumar Gala #include <asm/pci-bridge.h> 365516b540SKumar Gala #include <asm/byteorder.h> 375516b540SKumar Gala #include <asm/machdep.h> 385516b540SKumar Gala #include <asm/ppc-pci.h> 395516b540SKumar Gala #include <asm/firmware.h> 405516b540SKumar Gala 415516b540SKumar Gala #ifdef DEBUG 425516b540SKumar Gala #include <asm/udbg.h> 435516b540SKumar Gala #define DBG(fmt...) printk(fmt) 445516b540SKumar Gala #else 455516b540SKumar Gala #define DBG(fmt...) 465516b540SKumar Gala #endif 475516b540SKumar Gala 48a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock); 49a4c9e328SKumar Gala 50a4c9e328SKumar Gala /* XXX kill that some day ... */ 51ebfc00f7SStephen Rothwell static int global_phb_number; /* Global phb counter */ 52a4c9e328SKumar Gala 5325e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */ 5425e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base; 5525e81f92SBenjamin Herrenschmidt 563fd94c6bSBenjamin Herrenschmidt /* Default PCI flags is 0 */ 573fd94c6bSBenjamin Herrenschmidt unsigned int ppc_pci_flags; 58a4c9e328SKumar Gala 592d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev) 60a4c9e328SKumar Gala { 61a4c9e328SKumar Gala struct pci_controller *phb; 62a4c9e328SKumar Gala 63e60516e3SStephen Rothwell phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL); 64a4c9e328SKumar Gala if (phb == NULL) 65a4c9e328SKumar Gala return NULL; 66e60516e3SStephen Rothwell spin_lock(&hose_spinlock); 67e60516e3SStephen Rothwell phb->global_number = global_phb_number++; 68e60516e3SStephen Rothwell list_add_tail(&phb->list_node, &hose_list); 69e60516e3SStephen Rothwell spin_unlock(&hose_spinlock); 7044ef3390SStephen Rothwell phb->dn = dev; 71a4c9e328SKumar Gala phb->is_dynamic = mem_init_done; 72a4c9e328SKumar Gala #ifdef CONFIG_PPC64 73a4c9e328SKumar Gala if (dev) { 74a4c9e328SKumar Gala int nid = of_node_to_nid(dev); 75a4c9e328SKumar Gala 76a4c9e328SKumar Gala if (nid < 0 || !node_online(nid)) 77a4c9e328SKumar Gala nid = -1; 78a4c9e328SKumar Gala 79a4c9e328SKumar Gala PHB_SET_NODE(phb, nid); 80a4c9e328SKumar Gala } 81a4c9e328SKumar Gala #endif 82a4c9e328SKumar Gala return phb; 83a4c9e328SKumar Gala } 84a4c9e328SKumar Gala 85a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb) 86a4c9e328SKumar Gala { 87a4c9e328SKumar Gala spin_lock(&hose_spinlock); 88a4c9e328SKumar Gala list_del(&phb->list_node); 89a4c9e328SKumar Gala spin_unlock(&hose_spinlock); 90a4c9e328SKumar Gala 91a4c9e328SKumar Gala if (phb->is_dynamic) 92a4c9e328SKumar Gala kfree(phb); 93a4c9e328SKumar Gala } 94a4c9e328SKumar Gala 956dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address) 966dfbde20SBenjamin Herrenschmidt { 976dfbde20SBenjamin Herrenschmidt int ret = 0; 986dfbde20SBenjamin Herrenschmidt struct pci_controller *hose; 996dfbde20SBenjamin Herrenschmidt unsigned long size; 1006dfbde20SBenjamin Herrenschmidt 1016dfbde20SBenjamin Herrenschmidt spin_lock(&hose_spinlock); 1026dfbde20SBenjamin Herrenschmidt list_for_each_entry(hose, &hose_list, list_node) { 1036dfbde20SBenjamin Herrenschmidt #ifdef CONFIG_PPC64 1046dfbde20SBenjamin Herrenschmidt size = hose->pci_io_size; 1056dfbde20SBenjamin Herrenschmidt #else 1066dfbde20SBenjamin Herrenschmidt size = hose->io_resource.end - hose->io_resource.start + 1; 1076dfbde20SBenjamin Herrenschmidt #endif 1086dfbde20SBenjamin Herrenschmidt if (address >= hose->io_base_virt && 1096dfbde20SBenjamin Herrenschmidt address < (hose->io_base_virt + size)) { 1106dfbde20SBenjamin Herrenschmidt ret = 1; 1116dfbde20SBenjamin Herrenschmidt break; 1126dfbde20SBenjamin Herrenschmidt } 1136dfbde20SBenjamin Herrenschmidt } 1146dfbde20SBenjamin Herrenschmidt spin_unlock(&hose_spinlock); 1156dfbde20SBenjamin Herrenschmidt return ret; 1166dfbde20SBenjamin Herrenschmidt } 1176dfbde20SBenjamin Herrenschmidt 1185516b540SKumar Gala /* 1195516b540SKumar Gala * Return the domain number for this bus. 1205516b540SKumar Gala */ 1215516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus) 1225516b540SKumar Gala { 1235516b540SKumar Gala struct pci_controller *hose = pci_bus_to_host(bus); 1245516b540SKumar Gala 1255516b540SKumar Gala return hose->global_number; 1265516b540SKumar Gala } 1275516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr); 12858083dadSKumar Gala 12958083dadSKumar Gala #ifdef CONFIG_PPC_OF 130a4c9e328SKumar Gala 131a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the 132a4c9e328SKumar Gala * PCI bus numbers have not yet been assigned, and you need to 133a4c9e328SKumar Gala * issue PCI config cycles to an OF device. 134a4c9e328SKumar Gala * It could also be used to "fix" RTAS config cycles if you want 135a4c9e328SKumar Gala * to set pci_assign_all_buses to 1 and still use RTAS for PCI 136a4c9e328SKumar Gala * config cycles. 137a4c9e328SKumar Gala */ 138a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node) 139a4c9e328SKumar Gala { 140a4c9e328SKumar Gala if (!have_of) 141a4c9e328SKumar Gala return NULL; 142a4c9e328SKumar Gala while(node) { 143a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 144a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) 14544ef3390SStephen Rothwell if (hose->dn == node) 146a4c9e328SKumar Gala return hose; 147a4c9e328SKumar Gala node = node->parent; 148a4c9e328SKumar Gala } 149a4c9e328SKumar Gala return NULL; 150a4c9e328SKumar Gala } 151a4c9e328SKumar Gala 15258083dadSKumar Gala static ssize_t pci_show_devspec(struct device *dev, 15358083dadSKumar Gala struct device_attribute *attr, char *buf) 15458083dadSKumar Gala { 15558083dadSKumar Gala struct pci_dev *pdev; 15658083dadSKumar Gala struct device_node *np; 15758083dadSKumar Gala 15858083dadSKumar Gala pdev = to_pci_dev (dev); 15958083dadSKumar Gala np = pci_device_to_OF_node(pdev); 16058083dadSKumar Gala if (np == NULL || np->full_name == NULL) 16158083dadSKumar Gala return 0; 16258083dadSKumar Gala return sprintf(buf, "%s", np->full_name); 16358083dadSKumar Gala } 16458083dadSKumar Gala static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL); 16558083dadSKumar Gala #endif /* CONFIG_PPC_OF */ 16658083dadSKumar Gala 16758083dadSKumar Gala /* Add sysfs properties */ 1684f3731daSTony Breeds int pcibios_add_platform_entries(struct pci_dev *pdev) 16958083dadSKumar Gala { 17058083dadSKumar Gala #ifdef CONFIG_PPC_OF 1714f3731daSTony Breeds return device_create_file(&pdev->dev, &dev_attr_devspec); 1724f3731daSTony Breeds #else 1734f3731daSTony Breeds return 0; 17458083dadSKumar Gala #endif /* CONFIG_PPC_OF */ 1754f3731daSTony Breeds 17658083dadSKumar Gala } 17758083dadSKumar Gala 178a2b7390aSStephen Rothwell char __devinit *pcibios_setup(char *str) 17958083dadSKumar Gala { 18058083dadSKumar Gala return str; 18158083dadSKumar Gala } 18258083dadSKumar Gala 18358083dadSKumar Gala /* 18458083dadSKumar Gala * Reads the interrupt pin to determine if interrupt is use by card. 18558083dadSKumar Gala * If the interrupt is used, then gets the interrupt line from the 18658083dadSKumar Gala * openfirmware and sets it in the pci_dev and pci_config line. 18758083dadSKumar Gala */ 18858083dadSKumar Gala int pci_read_irq_line(struct pci_dev *pci_dev) 18958083dadSKumar Gala { 19058083dadSKumar Gala struct of_irq oirq; 19158083dadSKumar Gala unsigned int virq; 19258083dadSKumar Gala 19350c9bc2fSBenjamin Herrenschmidt /* The current device-tree that iSeries generates from the HV 19450c9bc2fSBenjamin Herrenschmidt * PCI informations doesn't contain proper interrupt routing, 19550c9bc2fSBenjamin Herrenschmidt * and all the fallback would do is print out crap, so we 19650c9bc2fSBenjamin Herrenschmidt * don't attempt to resolve the interrupts here at all, some 19750c9bc2fSBenjamin Herrenschmidt * iSeries specific fixup does it. 19850c9bc2fSBenjamin Herrenschmidt * 19950c9bc2fSBenjamin Herrenschmidt * In the long run, we will hopefully fix the generated device-tree 20050c9bc2fSBenjamin Herrenschmidt * instead. 20150c9bc2fSBenjamin Herrenschmidt */ 20250c9bc2fSBenjamin Herrenschmidt #ifdef CONFIG_PPC_ISERIES 20350c9bc2fSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_ISERIES)) 20450c9bc2fSBenjamin Herrenschmidt return -1; 20550c9bc2fSBenjamin Herrenschmidt #endif 20650c9bc2fSBenjamin Herrenschmidt 20758083dadSKumar Gala DBG("Try to map irq for %s...\n", pci_name(pci_dev)); 20858083dadSKumar Gala 20958083dadSKumar Gala #ifdef DEBUG 21058083dadSKumar Gala memset(&oirq, 0xff, sizeof(oirq)); 21158083dadSKumar Gala #endif 21258083dadSKumar Gala /* Try to get a mapping from the device-tree */ 21358083dadSKumar Gala if (of_irq_map_pci(pci_dev, &oirq)) { 21458083dadSKumar Gala u8 line, pin; 21558083dadSKumar Gala 21658083dadSKumar Gala /* If that fails, lets fallback to what is in the config 21758083dadSKumar Gala * space and map that through the default controller. We 21858083dadSKumar Gala * also set the type to level low since that's what PCI 21958083dadSKumar Gala * interrupts are. If your platform does differently, then 22058083dadSKumar Gala * either provide a proper interrupt tree or don't use this 22158083dadSKumar Gala * function. 22258083dadSKumar Gala */ 22358083dadSKumar Gala if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin)) 22458083dadSKumar Gala return -1; 22558083dadSKumar Gala if (pin == 0) 22658083dadSKumar Gala return -1; 22758083dadSKumar Gala if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) || 22854a24cbbSBenjamin Herrenschmidt line == 0xff || line == 0) { 22958083dadSKumar Gala return -1; 23058083dadSKumar Gala } 23154a24cbbSBenjamin Herrenschmidt DBG(" -> no map ! Using line %d (pin %d) from PCI config\n", 23254a24cbbSBenjamin Herrenschmidt line, pin); 23358083dadSKumar Gala 23458083dadSKumar Gala virq = irq_create_mapping(NULL, line); 23558083dadSKumar Gala if (virq != NO_IRQ) 23658083dadSKumar Gala set_irq_type(virq, IRQ_TYPE_LEVEL_LOW); 23758083dadSKumar Gala } else { 23858083dadSKumar Gala DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n", 23958083dadSKumar Gala oirq.size, oirq.specifier[0], oirq.specifier[1], 24058083dadSKumar Gala oirq.controller->full_name); 24158083dadSKumar Gala 24258083dadSKumar Gala virq = irq_create_of_mapping(oirq.controller, oirq.specifier, 24358083dadSKumar Gala oirq.size); 24458083dadSKumar Gala } 24558083dadSKumar Gala if(virq == NO_IRQ) { 24658083dadSKumar Gala DBG(" -> failed to map !\n"); 24758083dadSKumar Gala return -1; 24858083dadSKumar Gala } 24958083dadSKumar Gala 25058083dadSKumar Gala DBG(" -> mapped to linux irq %d\n", virq); 25158083dadSKumar Gala 25258083dadSKumar Gala pci_dev->irq = virq; 25358083dadSKumar Gala 25458083dadSKumar Gala return 0; 25558083dadSKumar Gala } 25658083dadSKumar Gala EXPORT_SYMBOL(pci_read_irq_line); 25758083dadSKumar Gala 25858083dadSKumar Gala /* 25958083dadSKumar Gala * Platform support for /proc/bus/pci/X/Y mmap()s, 26058083dadSKumar Gala * modelled on the sparc64 implementation by Dave Miller. 26158083dadSKumar Gala * -- paulus. 26258083dadSKumar Gala */ 26358083dadSKumar Gala 26458083dadSKumar Gala /* 26558083dadSKumar Gala * Adjust vm_pgoff of VMA such that it is the physical page offset 26658083dadSKumar Gala * corresponding to the 32-bit pci bus offset for DEV requested by the user. 26758083dadSKumar Gala * 26858083dadSKumar Gala * Basically, the user finds the base address for his device which he wishes 26958083dadSKumar Gala * to mmap. They read the 32-bit value from the config space base register, 27058083dadSKumar Gala * add whatever PAGE_SIZE multiple offset they wish, and feed this into the 27158083dadSKumar Gala * offset parameter of mmap on /proc/bus/pci/XXX for that device. 27258083dadSKumar Gala * 27358083dadSKumar Gala * Returns negative error code on failure, zero on success. 27458083dadSKumar Gala */ 27558083dadSKumar Gala static struct resource *__pci_mmap_make_offset(struct pci_dev *dev, 27658083dadSKumar Gala resource_size_t *offset, 27758083dadSKumar Gala enum pci_mmap_state mmap_state) 27858083dadSKumar Gala { 27958083dadSKumar Gala struct pci_controller *hose = pci_bus_to_host(dev->bus); 28058083dadSKumar Gala unsigned long io_offset = 0; 28158083dadSKumar Gala int i, res_bit; 28258083dadSKumar Gala 28358083dadSKumar Gala if (hose == 0) 28458083dadSKumar Gala return NULL; /* should never happen */ 28558083dadSKumar Gala 28658083dadSKumar Gala /* If memory, add on the PCI bridge address offset */ 28758083dadSKumar Gala if (mmap_state == pci_mmap_mem) { 28858083dadSKumar Gala #if 0 /* See comment in pci_resource_to_user() for why this is disabled */ 28958083dadSKumar Gala *offset += hose->pci_mem_offset; 29058083dadSKumar Gala #endif 29158083dadSKumar Gala res_bit = IORESOURCE_MEM; 29258083dadSKumar Gala } else { 29358083dadSKumar Gala io_offset = (unsigned long)hose->io_base_virt - _IO_BASE; 29458083dadSKumar Gala *offset += io_offset; 29558083dadSKumar Gala res_bit = IORESOURCE_IO; 29658083dadSKumar Gala } 29758083dadSKumar Gala 29858083dadSKumar Gala /* 29958083dadSKumar Gala * Check that the offset requested corresponds to one of the 30058083dadSKumar Gala * resources of the device. 30158083dadSKumar Gala */ 30258083dadSKumar Gala for (i = 0; i <= PCI_ROM_RESOURCE; i++) { 30358083dadSKumar Gala struct resource *rp = &dev->resource[i]; 30458083dadSKumar Gala int flags = rp->flags; 30558083dadSKumar Gala 30658083dadSKumar Gala /* treat ROM as memory (should be already) */ 30758083dadSKumar Gala if (i == PCI_ROM_RESOURCE) 30858083dadSKumar Gala flags |= IORESOURCE_MEM; 30958083dadSKumar Gala 31058083dadSKumar Gala /* Active and same type? */ 31158083dadSKumar Gala if ((flags & res_bit) == 0) 31258083dadSKumar Gala continue; 31358083dadSKumar Gala 31458083dadSKumar Gala /* In the range of this resource? */ 31558083dadSKumar Gala if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end) 31658083dadSKumar Gala continue; 31758083dadSKumar Gala 31858083dadSKumar Gala /* found it! construct the final physical address */ 31958083dadSKumar Gala if (mmap_state == pci_mmap_io) 32058083dadSKumar Gala *offset += hose->io_base_phys - io_offset; 32158083dadSKumar Gala return rp; 32258083dadSKumar Gala } 32358083dadSKumar Gala 32458083dadSKumar Gala return NULL; 32558083dadSKumar Gala } 32658083dadSKumar Gala 32758083dadSKumar Gala /* 32858083dadSKumar Gala * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci 32958083dadSKumar Gala * device mapping. 33058083dadSKumar Gala */ 33158083dadSKumar Gala static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp, 33258083dadSKumar Gala pgprot_t protection, 33358083dadSKumar Gala enum pci_mmap_state mmap_state, 33458083dadSKumar Gala int write_combine) 33558083dadSKumar Gala { 33658083dadSKumar Gala unsigned long prot = pgprot_val(protection); 33758083dadSKumar Gala 33858083dadSKumar Gala /* Write combine is always 0 on non-memory space mappings. On 33958083dadSKumar Gala * memory space, if the user didn't pass 1, we check for a 34058083dadSKumar Gala * "prefetchable" resource. This is a bit hackish, but we use 34158083dadSKumar Gala * this to workaround the inability of /sysfs to provide a write 34258083dadSKumar Gala * combine bit 34358083dadSKumar Gala */ 34458083dadSKumar Gala if (mmap_state != pci_mmap_mem) 34558083dadSKumar Gala write_combine = 0; 34658083dadSKumar Gala else if (write_combine == 0) { 34758083dadSKumar Gala if (rp->flags & IORESOURCE_PREFETCH) 34858083dadSKumar Gala write_combine = 1; 34958083dadSKumar Gala } 35058083dadSKumar Gala 35158083dadSKumar Gala /* XXX would be nice to have a way to ask for write-through */ 35258083dadSKumar Gala prot |= _PAGE_NO_CACHE; 35358083dadSKumar Gala if (write_combine) 35458083dadSKumar Gala prot &= ~_PAGE_GUARDED; 35558083dadSKumar Gala else 35658083dadSKumar Gala prot |= _PAGE_GUARDED; 35758083dadSKumar Gala 35858083dadSKumar Gala return __pgprot(prot); 35958083dadSKumar Gala } 36058083dadSKumar Gala 36158083dadSKumar Gala /* 36258083dadSKumar Gala * This one is used by /dev/mem and fbdev who have no clue about the 36358083dadSKumar Gala * PCI device, it tries to find the PCI device first and calls the 36458083dadSKumar Gala * above routine 36558083dadSKumar Gala */ 36658083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file, 36758083dadSKumar Gala unsigned long pfn, 36858083dadSKumar Gala unsigned long size, 36958083dadSKumar Gala pgprot_t protection) 37058083dadSKumar Gala { 37158083dadSKumar Gala struct pci_dev *pdev = NULL; 37258083dadSKumar Gala struct resource *found = NULL; 37358083dadSKumar Gala unsigned long prot = pgprot_val(protection); 37458083dadSKumar Gala unsigned long offset = pfn << PAGE_SHIFT; 37558083dadSKumar Gala int i; 37658083dadSKumar Gala 37758083dadSKumar Gala if (page_is_ram(pfn)) 37858083dadSKumar Gala return __pgprot(prot); 37958083dadSKumar Gala 38058083dadSKumar Gala prot |= _PAGE_NO_CACHE | _PAGE_GUARDED; 38158083dadSKumar Gala 38258083dadSKumar Gala for_each_pci_dev(pdev) { 38358083dadSKumar Gala for (i = 0; i <= PCI_ROM_RESOURCE; i++) { 38458083dadSKumar Gala struct resource *rp = &pdev->resource[i]; 38558083dadSKumar Gala int flags = rp->flags; 38658083dadSKumar Gala 38758083dadSKumar Gala /* Active and same type? */ 38858083dadSKumar Gala if ((flags & IORESOURCE_MEM) == 0) 38958083dadSKumar Gala continue; 39058083dadSKumar Gala /* In the range of this resource? */ 39158083dadSKumar Gala if (offset < (rp->start & PAGE_MASK) || 39258083dadSKumar Gala offset > rp->end) 39358083dadSKumar Gala continue; 39458083dadSKumar Gala found = rp; 39558083dadSKumar Gala break; 39658083dadSKumar Gala } 39758083dadSKumar Gala if (found) 39858083dadSKumar Gala break; 39958083dadSKumar Gala } 40058083dadSKumar Gala if (found) { 40158083dadSKumar Gala if (found->flags & IORESOURCE_PREFETCH) 40258083dadSKumar Gala prot &= ~_PAGE_GUARDED; 40358083dadSKumar Gala pci_dev_put(pdev); 40458083dadSKumar Gala } 40558083dadSKumar Gala 40658083dadSKumar Gala DBG("non-PCI map for %lx, prot: %lx\n", offset, prot); 40758083dadSKumar Gala 40858083dadSKumar Gala return __pgprot(prot); 40958083dadSKumar Gala } 41058083dadSKumar Gala 41158083dadSKumar Gala 41258083dadSKumar Gala /* 41358083dadSKumar Gala * Perform the actual remap of the pages for a PCI device mapping, as 41458083dadSKumar Gala * appropriate for this architecture. The region in the process to map 41558083dadSKumar Gala * is described by vm_start and vm_end members of VMA, the base physical 41658083dadSKumar Gala * address is found in vm_pgoff. 41758083dadSKumar Gala * The pci device structure is provided so that architectures may make mapping 41858083dadSKumar Gala * decisions on a per-device or per-bus basis. 41958083dadSKumar Gala * 42058083dadSKumar Gala * Returns a negative error code on failure, zero on success. 42158083dadSKumar Gala */ 42258083dadSKumar Gala int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, 42358083dadSKumar Gala enum pci_mmap_state mmap_state, int write_combine) 42458083dadSKumar Gala { 42558083dadSKumar Gala resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT; 42658083dadSKumar Gala struct resource *rp; 42758083dadSKumar Gala int ret; 42858083dadSKumar Gala 42958083dadSKumar Gala rp = __pci_mmap_make_offset(dev, &offset, mmap_state); 43058083dadSKumar Gala if (rp == NULL) 43158083dadSKumar Gala return -EINVAL; 43258083dadSKumar Gala 43358083dadSKumar Gala vma->vm_pgoff = offset >> PAGE_SHIFT; 43458083dadSKumar Gala vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp, 43558083dadSKumar Gala vma->vm_page_prot, 43658083dadSKumar Gala mmap_state, write_combine); 43758083dadSKumar Gala 43858083dadSKumar Gala ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, 43958083dadSKumar Gala vma->vm_end - vma->vm_start, vma->vm_page_prot); 44058083dadSKumar Gala 44158083dadSKumar Gala return ret; 44258083dadSKumar Gala } 44358083dadSKumar Gala 44458083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar, 44558083dadSKumar Gala const struct resource *rsrc, 44658083dadSKumar Gala resource_size_t *start, resource_size_t *end) 44758083dadSKumar Gala { 44858083dadSKumar Gala struct pci_controller *hose = pci_bus_to_host(dev->bus); 44958083dadSKumar Gala resource_size_t offset = 0; 45058083dadSKumar Gala 45158083dadSKumar Gala if (hose == NULL) 45258083dadSKumar Gala return; 45358083dadSKumar Gala 45458083dadSKumar Gala if (rsrc->flags & IORESOURCE_IO) 45558083dadSKumar Gala offset = (unsigned long)hose->io_base_virt - _IO_BASE; 45658083dadSKumar Gala 45758083dadSKumar Gala /* We pass a fully fixed up address to userland for MMIO instead of 45858083dadSKumar Gala * a BAR value because X is lame and expects to be able to use that 45958083dadSKumar Gala * to pass to /dev/mem ! 46058083dadSKumar Gala * 46158083dadSKumar Gala * That means that we'll have potentially 64 bits values where some 46258083dadSKumar Gala * userland apps only expect 32 (like X itself since it thinks only 46358083dadSKumar Gala * Sparc has 64 bits MMIO) but if we don't do that, we break it on 46458083dadSKumar Gala * 32 bits CHRPs :-( 46558083dadSKumar Gala * 46658083dadSKumar Gala * Hopefully, the sysfs insterface is immune to that gunk. Once X 46758083dadSKumar Gala * has been fixed (and the fix spread enough), we can re-enable the 46858083dadSKumar Gala * 2 lines below and pass down a BAR value to userland. In that case 46958083dadSKumar Gala * we'll also have to re-enable the matching code in 47058083dadSKumar Gala * __pci_mmap_make_offset(). 47158083dadSKumar Gala * 47258083dadSKumar Gala * BenH. 47358083dadSKumar Gala */ 47458083dadSKumar Gala #if 0 47558083dadSKumar Gala else if (rsrc->flags & IORESOURCE_MEM) 47658083dadSKumar Gala offset = hose->pci_mem_offset; 47758083dadSKumar Gala #endif 47858083dadSKumar Gala 47958083dadSKumar Gala *start = rsrc->start - offset; 48058083dadSKumar Gala *end = rsrc->end - offset; 48158083dadSKumar Gala } 48213dccb9eSBenjamin Herrenschmidt 48313dccb9eSBenjamin Herrenschmidt /** 48413dccb9eSBenjamin Herrenschmidt * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree 48513dccb9eSBenjamin Herrenschmidt * @hose: newly allocated pci_controller to be setup 48613dccb9eSBenjamin Herrenschmidt * @dev: device node of the host bridge 48713dccb9eSBenjamin Herrenschmidt * @primary: set if primary bus (32 bits only, soon to be deprecated) 48813dccb9eSBenjamin Herrenschmidt * 48913dccb9eSBenjamin Herrenschmidt * This function will parse the "ranges" property of a PCI host bridge device 49013dccb9eSBenjamin Herrenschmidt * node and setup the resource mapping of a pci controller based on its 49113dccb9eSBenjamin Herrenschmidt * content. 49213dccb9eSBenjamin Herrenschmidt * 49313dccb9eSBenjamin Herrenschmidt * Life would be boring if it wasn't for a few issues that we have to deal 49413dccb9eSBenjamin Herrenschmidt * with here: 49513dccb9eSBenjamin Herrenschmidt * 49613dccb9eSBenjamin Herrenschmidt * - We can only cope with one IO space range and up to 3 Memory space 49713dccb9eSBenjamin Herrenschmidt * ranges. However, some machines (thanks Apple !) tend to split their 49813dccb9eSBenjamin Herrenschmidt * space into lots of small contiguous ranges. So we have to coalesce. 49913dccb9eSBenjamin Herrenschmidt * 50013dccb9eSBenjamin Herrenschmidt * - We can only cope with all memory ranges having the same offset 50113dccb9eSBenjamin Herrenschmidt * between CPU addresses and PCI addresses. Unfortunately, some bridges 50213dccb9eSBenjamin Herrenschmidt * are setup for a large 1:1 mapping along with a small "window" which 50313dccb9eSBenjamin Herrenschmidt * maps PCI address 0 to some arbitrary high address of the CPU space in 50413dccb9eSBenjamin Herrenschmidt * order to give access to the ISA memory hole. 50513dccb9eSBenjamin Herrenschmidt * The way out of here that I've chosen for now is to always set the 50613dccb9eSBenjamin Herrenschmidt * offset based on the first resource found, then override it if we 50713dccb9eSBenjamin Herrenschmidt * have a different offset and the previous was set by an ISA hole. 50813dccb9eSBenjamin Herrenschmidt * 50913dccb9eSBenjamin Herrenschmidt * - Some busses have IO space not starting at 0, which causes trouble with 51013dccb9eSBenjamin Herrenschmidt * the way we do our IO resource renumbering. The code somewhat deals with 51113dccb9eSBenjamin Herrenschmidt * it for 64 bits but I would expect problems on 32 bits. 51213dccb9eSBenjamin Herrenschmidt * 51313dccb9eSBenjamin Herrenschmidt * - Some 32 bits platforms such as 4xx can have physical space larger than 51413dccb9eSBenjamin Herrenschmidt * 32 bits so we need to use 64 bits values for the parsing 51513dccb9eSBenjamin Herrenschmidt */ 51613dccb9eSBenjamin Herrenschmidt void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose, 51713dccb9eSBenjamin Herrenschmidt struct device_node *dev, 51813dccb9eSBenjamin Herrenschmidt int primary) 51913dccb9eSBenjamin Herrenschmidt { 52013dccb9eSBenjamin Herrenschmidt const u32 *ranges; 52113dccb9eSBenjamin Herrenschmidt int rlen; 52213dccb9eSBenjamin Herrenschmidt int pna = of_n_addr_cells(dev); 52313dccb9eSBenjamin Herrenschmidt int np = pna + 5; 52413dccb9eSBenjamin Herrenschmidt int memno = 0, isa_hole = -1; 52513dccb9eSBenjamin Herrenschmidt u32 pci_space; 52613dccb9eSBenjamin Herrenschmidt unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size; 52713dccb9eSBenjamin Herrenschmidt unsigned long long isa_mb = 0; 52813dccb9eSBenjamin Herrenschmidt struct resource *res; 52913dccb9eSBenjamin Herrenschmidt 53013dccb9eSBenjamin Herrenschmidt printk(KERN_INFO "PCI host bridge %s %s ranges:\n", 53113dccb9eSBenjamin Herrenschmidt dev->full_name, primary ? "(primary)" : ""); 53213dccb9eSBenjamin Herrenschmidt 53313dccb9eSBenjamin Herrenschmidt /* Get ranges property */ 53413dccb9eSBenjamin Herrenschmidt ranges = of_get_property(dev, "ranges", &rlen); 53513dccb9eSBenjamin Herrenschmidt if (ranges == NULL) 53613dccb9eSBenjamin Herrenschmidt return; 53713dccb9eSBenjamin Herrenschmidt 53813dccb9eSBenjamin Herrenschmidt /* Parse it */ 53913dccb9eSBenjamin Herrenschmidt while ((rlen -= np * 4) >= 0) { 54013dccb9eSBenjamin Herrenschmidt /* Read next ranges element */ 54113dccb9eSBenjamin Herrenschmidt pci_space = ranges[0]; 54213dccb9eSBenjamin Herrenschmidt pci_addr = of_read_number(ranges + 1, 2); 54313dccb9eSBenjamin Herrenschmidt cpu_addr = of_translate_address(dev, ranges + 3); 54413dccb9eSBenjamin Herrenschmidt size = of_read_number(ranges + pna + 3, 2); 54513dccb9eSBenjamin Herrenschmidt ranges += np; 54613dccb9eSBenjamin Herrenschmidt if (cpu_addr == OF_BAD_ADDR || size == 0) 54713dccb9eSBenjamin Herrenschmidt continue; 54813dccb9eSBenjamin Herrenschmidt 54913dccb9eSBenjamin Herrenschmidt /* Now consume following elements while they are contiguous */ 55013dccb9eSBenjamin Herrenschmidt for (; rlen >= np * sizeof(u32); 55113dccb9eSBenjamin Herrenschmidt ranges += np, rlen -= np * 4) { 55213dccb9eSBenjamin Herrenschmidt if (ranges[0] != pci_space) 55313dccb9eSBenjamin Herrenschmidt break; 55413dccb9eSBenjamin Herrenschmidt pci_next = of_read_number(ranges + 1, 2); 55513dccb9eSBenjamin Herrenschmidt cpu_next = of_translate_address(dev, ranges + 3); 55613dccb9eSBenjamin Herrenschmidt if (pci_next != pci_addr + size || 55713dccb9eSBenjamin Herrenschmidt cpu_next != cpu_addr + size) 55813dccb9eSBenjamin Herrenschmidt break; 55913dccb9eSBenjamin Herrenschmidt size += of_read_number(ranges + pna + 3, 2); 56013dccb9eSBenjamin Herrenschmidt } 56113dccb9eSBenjamin Herrenschmidt 56213dccb9eSBenjamin Herrenschmidt /* Act based on address space type */ 56313dccb9eSBenjamin Herrenschmidt res = NULL; 56413dccb9eSBenjamin Herrenschmidt switch ((pci_space >> 24) & 0x3) { 56513dccb9eSBenjamin Herrenschmidt case 1: /* PCI IO space */ 56613dccb9eSBenjamin Herrenschmidt printk(KERN_INFO 56713dccb9eSBenjamin Herrenschmidt " IO 0x%016llx..0x%016llx -> 0x%016llx\n", 56813dccb9eSBenjamin Herrenschmidt cpu_addr, cpu_addr + size - 1, pci_addr); 56913dccb9eSBenjamin Herrenschmidt 57013dccb9eSBenjamin Herrenschmidt /* We support only one IO range */ 57113dccb9eSBenjamin Herrenschmidt if (hose->pci_io_size) { 57213dccb9eSBenjamin Herrenschmidt printk(KERN_INFO 57313dccb9eSBenjamin Herrenschmidt " \\--> Skipped (too many) !\n"); 57413dccb9eSBenjamin Herrenschmidt continue; 57513dccb9eSBenjamin Herrenschmidt } 57613dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32 57713dccb9eSBenjamin Herrenschmidt /* On 32 bits, limit I/O space to 16MB */ 57813dccb9eSBenjamin Herrenschmidt if (size > 0x01000000) 57913dccb9eSBenjamin Herrenschmidt size = 0x01000000; 58013dccb9eSBenjamin Herrenschmidt 58113dccb9eSBenjamin Herrenschmidt /* 32 bits needs to map IOs here */ 58213dccb9eSBenjamin Herrenschmidt hose->io_base_virt = ioremap(cpu_addr, size); 58313dccb9eSBenjamin Herrenschmidt 58413dccb9eSBenjamin Herrenschmidt /* Expect trouble if pci_addr is not 0 */ 58513dccb9eSBenjamin Herrenschmidt if (primary) 58613dccb9eSBenjamin Herrenschmidt isa_io_base = 58713dccb9eSBenjamin Herrenschmidt (unsigned long)hose->io_base_virt; 58813dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */ 58913dccb9eSBenjamin Herrenschmidt /* pci_io_size and io_base_phys always represent IO 59013dccb9eSBenjamin Herrenschmidt * space starting at 0 so we factor in pci_addr 59113dccb9eSBenjamin Herrenschmidt */ 59213dccb9eSBenjamin Herrenschmidt hose->pci_io_size = pci_addr + size; 59313dccb9eSBenjamin Herrenschmidt hose->io_base_phys = cpu_addr - pci_addr; 59413dccb9eSBenjamin Herrenschmidt 59513dccb9eSBenjamin Herrenschmidt /* Build resource */ 59613dccb9eSBenjamin Herrenschmidt res = &hose->io_resource; 59713dccb9eSBenjamin Herrenschmidt res->flags = IORESOURCE_IO; 59813dccb9eSBenjamin Herrenschmidt res->start = pci_addr; 59913dccb9eSBenjamin Herrenschmidt break; 60013dccb9eSBenjamin Herrenschmidt case 2: /* PCI Memory space */ 60167260ac9SBenjamin Herrenschmidt case 3: /* PCI 64 bits Memory space */ 60213dccb9eSBenjamin Herrenschmidt printk(KERN_INFO 60313dccb9eSBenjamin Herrenschmidt " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n", 60413dccb9eSBenjamin Herrenschmidt cpu_addr, cpu_addr + size - 1, pci_addr, 60513dccb9eSBenjamin Herrenschmidt (pci_space & 0x40000000) ? "Prefetch" : ""); 60613dccb9eSBenjamin Herrenschmidt 60713dccb9eSBenjamin Herrenschmidt /* We support only 3 memory ranges */ 60813dccb9eSBenjamin Herrenschmidt if (memno >= 3) { 60913dccb9eSBenjamin Herrenschmidt printk(KERN_INFO 61013dccb9eSBenjamin Herrenschmidt " \\--> Skipped (too many) !\n"); 61113dccb9eSBenjamin Herrenschmidt continue; 61213dccb9eSBenjamin Herrenschmidt } 61313dccb9eSBenjamin Herrenschmidt /* Handles ISA memory hole space here */ 61413dccb9eSBenjamin Herrenschmidt if (pci_addr == 0) { 61513dccb9eSBenjamin Herrenschmidt isa_mb = cpu_addr; 61613dccb9eSBenjamin Herrenschmidt isa_hole = memno; 61713dccb9eSBenjamin Herrenschmidt if (primary || isa_mem_base == 0) 61813dccb9eSBenjamin Herrenschmidt isa_mem_base = cpu_addr; 61913dccb9eSBenjamin Herrenschmidt } 62013dccb9eSBenjamin Herrenschmidt 62113dccb9eSBenjamin Herrenschmidt /* We get the PCI/Mem offset from the first range or 62213dccb9eSBenjamin Herrenschmidt * the, current one if the offset came from an ISA 62313dccb9eSBenjamin Herrenschmidt * hole. If they don't match, bugger. 62413dccb9eSBenjamin Herrenschmidt */ 62513dccb9eSBenjamin Herrenschmidt if (memno == 0 || 62613dccb9eSBenjamin Herrenschmidt (isa_hole >= 0 && pci_addr != 0 && 62713dccb9eSBenjamin Herrenschmidt hose->pci_mem_offset == isa_mb)) 62813dccb9eSBenjamin Herrenschmidt hose->pci_mem_offset = cpu_addr - pci_addr; 62913dccb9eSBenjamin Herrenschmidt else if (pci_addr != 0 && 63013dccb9eSBenjamin Herrenschmidt hose->pci_mem_offset != cpu_addr - pci_addr) { 63113dccb9eSBenjamin Herrenschmidt printk(KERN_INFO 63213dccb9eSBenjamin Herrenschmidt " \\--> Skipped (offset mismatch) !\n"); 63313dccb9eSBenjamin Herrenschmidt continue; 63413dccb9eSBenjamin Herrenschmidt } 63513dccb9eSBenjamin Herrenschmidt 63613dccb9eSBenjamin Herrenschmidt /* Build resource */ 63713dccb9eSBenjamin Herrenschmidt res = &hose->mem_resources[memno++]; 63813dccb9eSBenjamin Herrenschmidt res->flags = IORESOURCE_MEM; 63913dccb9eSBenjamin Herrenschmidt if (pci_space & 0x40000000) 64013dccb9eSBenjamin Herrenschmidt res->flags |= IORESOURCE_PREFETCH; 64113dccb9eSBenjamin Herrenschmidt res->start = cpu_addr; 64213dccb9eSBenjamin Herrenschmidt break; 64313dccb9eSBenjamin Herrenschmidt } 64413dccb9eSBenjamin Herrenschmidt if (res != NULL) { 64513dccb9eSBenjamin Herrenschmidt res->name = dev->full_name; 64613dccb9eSBenjamin Herrenschmidt res->end = res->start + size - 1; 64713dccb9eSBenjamin Herrenschmidt res->parent = NULL; 64813dccb9eSBenjamin Herrenschmidt res->sibling = NULL; 64913dccb9eSBenjamin Herrenschmidt res->child = NULL; 65013dccb9eSBenjamin Herrenschmidt } 65113dccb9eSBenjamin Herrenschmidt } 65213dccb9eSBenjamin Herrenschmidt 653*8db13a0eSBenjamin Herrenschmidt /* If there's an ISA hole and the pci_mem_offset is -not- matching 654*8db13a0eSBenjamin Herrenschmidt * the ISA hole offset, then we need to remove the ISA hole from 655*8db13a0eSBenjamin Herrenschmidt * the resource list for that brige 656*8db13a0eSBenjamin Herrenschmidt */ 657*8db13a0eSBenjamin Herrenschmidt if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) { 658*8db13a0eSBenjamin Herrenschmidt unsigned int next = isa_hole + 1; 659*8db13a0eSBenjamin Herrenschmidt printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb); 660*8db13a0eSBenjamin Herrenschmidt if (next < memno) 661*8db13a0eSBenjamin Herrenschmidt memmove(&hose->mem_resources[isa_hole], 662*8db13a0eSBenjamin Herrenschmidt &hose->mem_resources[next], 663*8db13a0eSBenjamin Herrenschmidt sizeof(struct resource) * (memno - next)); 664*8db13a0eSBenjamin Herrenschmidt hose->mem_resources[--memno].flags = 0; 66513dccb9eSBenjamin Herrenschmidt } 66613dccb9eSBenjamin Herrenschmidt } 667fa462f2dSBenjamin Herrenschmidt 668fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */ 669fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus) 670fa462f2dSBenjamin Herrenschmidt { 671fa462f2dSBenjamin Herrenschmidt struct pci_controller *hose = pci_bus_to_host(bus); 672fa462f2dSBenjamin Herrenschmidt #ifdef CONFIG_PPC64 673fa462f2dSBenjamin Herrenschmidt return hose->buid != 0; 674fa462f2dSBenjamin Herrenschmidt #else 675fa462f2dSBenjamin Herrenschmidt if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS)) 676fa462f2dSBenjamin Herrenschmidt return 0; 677fa462f2dSBenjamin Herrenschmidt if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0) 678fa462f2dSBenjamin Herrenschmidt return hose->global_number != 0; 679fa462f2dSBenjamin Herrenschmidt return 1; 680fa462f2dSBenjamin Herrenschmidt #endif 681fa462f2dSBenjamin Herrenschmidt } 682fa462f2dSBenjamin Herrenschmidt 683fe2d338cSBenjamin Herrenschmidt void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, 684fe2d338cSBenjamin Herrenschmidt struct resource *res) 685fe2d338cSBenjamin Herrenschmidt { 686fe2d338cSBenjamin Herrenschmidt resource_size_t offset = 0, mask = (resource_size_t)-1; 687fe2d338cSBenjamin Herrenschmidt struct pci_controller *hose = pci_bus_to_host(dev->bus); 688fe2d338cSBenjamin Herrenschmidt 689fe2d338cSBenjamin Herrenschmidt if (!hose) 690fe2d338cSBenjamin Herrenschmidt return; 691fe2d338cSBenjamin Herrenschmidt if (res->flags & IORESOURCE_IO) { 692fe2d338cSBenjamin Herrenschmidt offset = (unsigned long)hose->io_base_virt - _IO_BASE; 693fe2d338cSBenjamin Herrenschmidt mask = 0xffffffffu; 694fe2d338cSBenjamin Herrenschmidt } else if (res->flags & IORESOURCE_MEM) 695fe2d338cSBenjamin Herrenschmidt offset = hose->pci_mem_offset; 696fe2d338cSBenjamin Herrenschmidt 697fe2d338cSBenjamin Herrenschmidt region->start = (res->start - offset) & mask; 698fe2d338cSBenjamin Herrenschmidt region->end = (res->end - offset) & mask; 699fe2d338cSBenjamin Herrenschmidt } 700fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_resource_to_bus); 701fe2d338cSBenjamin Herrenschmidt 702fe2d338cSBenjamin Herrenschmidt void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, 703fe2d338cSBenjamin Herrenschmidt struct pci_bus_region *region) 704fe2d338cSBenjamin Herrenschmidt { 705fe2d338cSBenjamin Herrenschmidt resource_size_t offset = 0, mask = (resource_size_t)-1; 706fe2d338cSBenjamin Herrenschmidt struct pci_controller *hose = pci_bus_to_host(dev->bus); 707fe2d338cSBenjamin Herrenschmidt 708fe2d338cSBenjamin Herrenschmidt if (!hose) 709fe2d338cSBenjamin Herrenschmidt return; 710fe2d338cSBenjamin Herrenschmidt if (res->flags & IORESOURCE_IO) { 711fe2d338cSBenjamin Herrenschmidt offset = (unsigned long)hose->io_base_virt - _IO_BASE; 712fe2d338cSBenjamin Herrenschmidt mask = 0xffffffffu; 713fe2d338cSBenjamin Herrenschmidt } else if (res->flags & IORESOURCE_MEM) 714fe2d338cSBenjamin Herrenschmidt offset = hose->pci_mem_offset; 715fe2d338cSBenjamin Herrenschmidt res->start = (region->start + offset) & mask; 716fe2d338cSBenjamin Herrenschmidt res->end = (region->end + offset) & mask; 717fe2d338cSBenjamin Herrenschmidt } 718fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_bus_to_resource); 719bf5e2ba2SBenjamin Herrenschmidt 720bf5e2ba2SBenjamin Herrenschmidt /* Fixup a bus resource into a linux resource */ 721bf5e2ba2SBenjamin Herrenschmidt static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev) 722bf5e2ba2SBenjamin Herrenschmidt { 723bf5e2ba2SBenjamin Herrenschmidt struct pci_controller *hose = pci_bus_to_host(dev->bus); 724bf5e2ba2SBenjamin Herrenschmidt resource_size_t offset = 0, mask = (resource_size_t)-1; 725bf5e2ba2SBenjamin Herrenschmidt 726bf5e2ba2SBenjamin Herrenschmidt if (res->flags & IORESOURCE_IO) { 727bf5e2ba2SBenjamin Herrenschmidt offset = (unsigned long)hose->io_base_virt - _IO_BASE; 728bf5e2ba2SBenjamin Herrenschmidt mask = 0xffffffffu; 729bf5e2ba2SBenjamin Herrenschmidt } else if (res->flags & IORESOURCE_MEM) 730bf5e2ba2SBenjamin Herrenschmidt offset = hose->pci_mem_offset; 731bf5e2ba2SBenjamin Herrenschmidt 732bf5e2ba2SBenjamin Herrenschmidt res->start = (res->start + offset) & mask; 733bf5e2ba2SBenjamin Herrenschmidt res->end = (res->end + offset) & mask; 734bf5e2ba2SBenjamin Herrenschmidt 735bf5e2ba2SBenjamin Herrenschmidt pr_debug("PCI:%s %016llx-%016llx\n", 736bf5e2ba2SBenjamin Herrenschmidt pci_name(dev), 737bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->start, 738bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->end); 739bf5e2ba2SBenjamin Herrenschmidt } 740bf5e2ba2SBenjamin Herrenschmidt 741bf5e2ba2SBenjamin Herrenschmidt 742bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are 743bf5e2ba2SBenjamin Herrenschmidt * probed, but not for bridge ranges 744bf5e2ba2SBenjamin Herrenschmidt */ 745bf5e2ba2SBenjamin Herrenschmidt static void __devinit pcibios_fixup_resources(struct pci_dev *dev) 746bf5e2ba2SBenjamin Herrenschmidt { 747bf5e2ba2SBenjamin Herrenschmidt struct pci_controller *hose = pci_bus_to_host(dev->bus); 748bf5e2ba2SBenjamin Herrenschmidt int i; 749bf5e2ba2SBenjamin Herrenschmidt 750bf5e2ba2SBenjamin Herrenschmidt if (!hose) { 751bf5e2ba2SBenjamin Herrenschmidt printk(KERN_ERR "No host bridge for PCI dev %s !\n", 752bf5e2ba2SBenjamin Herrenschmidt pci_name(dev)); 753bf5e2ba2SBenjamin Herrenschmidt return; 754bf5e2ba2SBenjamin Herrenschmidt } 755bf5e2ba2SBenjamin Herrenschmidt for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 756bf5e2ba2SBenjamin Herrenschmidt struct resource *res = dev->resource + i; 757bf5e2ba2SBenjamin Herrenschmidt if (!res->flags) 758bf5e2ba2SBenjamin Herrenschmidt continue; 7597f172890SBenjamin Herrenschmidt /* On platforms that have PPC_PCI_PROBE_ONLY set, we don't 7607f172890SBenjamin Herrenschmidt * consider 0 as an unassigned BAR value. It's technically 7617f172890SBenjamin Herrenschmidt * a valid value, but linux doesn't like it... so when we can 7627f172890SBenjamin Herrenschmidt * re-assign things, we do so, but if we can't, we keep it 7637f172890SBenjamin Herrenschmidt * around and hope for the best... 7647f172890SBenjamin Herrenschmidt */ 7657f172890SBenjamin Herrenschmidt if (res->start == 0 && !(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) { 766bf5e2ba2SBenjamin Herrenschmidt pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n", 767bf5e2ba2SBenjamin Herrenschmidt pci_name(dev), i, 768bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->start, 769bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->end, 770bf5e2ba2SBenjamin Herrenschmidt (unsigned int)res->flags); 771bf5e2ba2SBenjamin Herrenschmidt res->end -= res->start; 772bf5e2ba2SBenjamin Herrenschmidt res->start = 0; 773bf5e2ba2SBenjamin Herrenschmidt res->flags |= IORESOURCE_UNSET; 774bf5e2ba2SBenjamin Herrenschmidt continue; 775bf5e2ba2SBenjamin Herrenschmidt } 776bf5e2ba2SBenjamin Herrenschmidt 777bf5e2ba2SBenjamin Herrenschmidt pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n", 778bf5e2ba2SBenjamin Herrenschmidt pci_name(dev), i, 779bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->start,\ 780bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->end, 781bf5e2ba2SBenjamin Herrenschmidt (unsigned int)res->flags); 782bf5e2ba2SBenjamin Herrenschmidt 783bf5e2ba2SBenjamin Herrenschmidt fixup_resource(res, dev); 784bf5e2ba2SBenjamin Herrenschmidt } 785bf5e2ba2SBenjamin Herrenschmidt 786bf5e2ba2SBenjamin Herrenschmidt /* Call machine specific resource fixup */ 787bf5e2ba2SBenjamin Herrenschmidt if (ppc_md.pcibios_fixup_resources) 788bf5e2ba2SBenjamin Herrenschmidt ppc_md.pcibios_fixup_resources(dev); 789bf5e2ba2SBenjamin Herrenschmidt } 790bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources); 791bf5e2ba2SBenjamin Herrenschmidt 792bf5e2ba2SBenjamin Herrenschmidt static void __devinit __pcibios_fixup_bus(struct pci_bus *bus) 793bf5e2ba2SBenjamin Herrenschmidt { 794be8cbcd8SBenjamin Herrenschmidt struct pci_controller *hose = pci_bus_to_host(bus); 795bf5e2ba2SBenjamin Herrenschmidt struct pci_dev *dev = bus->self; 796bf5e2ba2SBenjamin Herrenschmidt 797bf5e2ba2SBenjamin Herrenschmidt pr_debug("PCI: Fixup bus %d (%s)\n", bus->number, dev ? pci_name(dev) : "PHB"); 798bf5e2ba2SBenjamin Herrenschmidt 799bf5e2ba2SBenjamin Herrenschmidt /* Fixup PCI<->PCI bridges. Host bridges are handled separately, for 800bf5e2ba2SBenjamin Herrenschmidt * now differently between 32 and 64 bits. 801bf5e2ba2SBenjamin Herrenschmidt */ 802bf5e2ba2SBenjamin Herrenschmidt if (dev != NULL) { 803bf5e2ba2SBenjamin Herrenschmidt struct resource *res; 804bf5e2ba2SBenjamin Herrenschmidt int i; 805bf5e2ba2SBenjamin Herrenschmidt 806bf5e2ba2SBenjamin Herrenschmidt for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) { 807bf5e2ba2SBenjamin Herrenschmidt if ((res = bus->resource[i]) == NULL) 808bf5e2ba2SBenjamin Herrenschmidt continue; 809b188b2aeSKumar Gala if (!res->flags) 810bf5e2ba2SBenjamin Herrenschmidt continue; 811b188b2aeSKumar Gala if (i >= 3 && bus->self->transparent) 812b188b2aeSKumar Gala continue; 813be8cbcd8SBenjamin Herrenschmidt /* On PowerMac, Apple leaves bridge windows open over 814be8cbcd8SBenjamin Herrenschmidt * an inaccessible region of memory space (0...fffff) 815be8cbcd8SBenjamin Herrenschmidt * which is somewhat bogus, but that's what they think 816be8cbcd8SBenjamin Herrenschmidt * means disabled... 817be8cbcd8SBenjamin Herrenschmidt * 818be8cbcd8SBenjamin Herrenschmidt * We clear those to force them to be reallocated later 819be8cbcd8SBenjamin Herrenschmidt * 820be8cbcd8SBenjamin Herrenschmidt * We detect such regions by the fact that the base is 821be8cbcd8SBenjamin Herrenschmidt * equal to the pci_mem_offset of the host bridge and 822be8cbcd8SBenjamin Herrenschmidt * their size is smaller than 1M. 823be8cbcd8SBenjamin Herrenschmidt */ 82496d69c31SKumar Gala if (res->flags & IORESOURCE_MEM && 82596d69c31SKumar Gala res->start == hose->pci_mem_offset && 826be8cbcd8SBenjamin Herrenschmidt res->end < 0x100000) { 827be8cbcd8SBenjamin Herrenschmidt printk(KERN_INFO 828be8cbcd8SBenjamin Herrenschmidt "PCI: Closing bogus Apple Firmware" 829be8cbcd8SBenjamin Herrenschmidt " region %d on bus 0x%02x\n", 830be8cbcd8SBenjamin Herrenschmidt i, bus->number); 831be8cbcd8SBenjamin Herrenschmidt res->flags = 0; 832be8cbcd8SBenjamin Herrenschmidt continue; 833be8cbcd8SBenjamin Herrenschmidt } 834be8cbcd8SBenjamin Herrenschmidt 835bf5e2ba2SBenjamin Herrenschmidt pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n", 836bf5e2ba2SBenjamin Herrenschmidt pci_name(dev), i, 837bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->start,\ 838bf5e2ba2SBenjamin Herrenschmidt (unsigned long long)res->end, 839bf5e2ba2SBenjamin Herrenschmidt (unsigned int)res->flags); 840bf5e2ba2SBenjamin Herrenschmidt 841bf5e2ba2SBenjamin Herrenschmidt fixup_resource(res, dev); 842bf5e2ba2SBenjamin Herrenschmidt } 843bf5e2ba2SBenjamin Herrenschmidt } 844bf5e2ba2SBenjamin Herrenschmidt 845bf5e2ba2SBenjamin Herrenschmidt /* Additional setup that is different between 32 and 64 bits for now */ 846bf5e2ba2SBenjamin Herrenschmidt pcibios_do_bus_setup(bus); 847bf5e2ba2SBenjamin Herrenschmidt 848bf5e2ba2SBenjamin Herrenschmidt /* Platform specific bus fixups */ 849bf5e2ba2SBenjamin Herrenschmidt if (ppc_md.pcibios_fixup_bus) 850bf5e2ba2SBenjamin Herrenschmidt ppc_md.pcibios_fixup_bus(bus); 851bf5e2ba2SBenjamin Herrenschmidt 852bf5e2ba2SBenjamin Herrenschmidt /* Read default IRQs and fixup if necessary */ 853bf5e2ba2SBenjamin Herrenschmidt list_for_each_entry(dev, &bus->devices, bus_list) { 854bf5e2ba2SBenjamin Herrenschmidt pci_read_irq_line(dev); 855bf5e2ba2SBenjamin Herrenschmidt if (ppc_md.pci_irq_fixup) 856bf5e2ba2SBenjamin Herrenschmidt ppc_md.pci_irq_fixup(dev); 857bf5e2ba2SBenjamin Herrenschmidt } 858bf5e2ba2SBenjamin Herrenschmidt } 859bf5e2ba2SBenjamin Herrenschmidt 860bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_fixup_bus(struct pci_bus *bus) 861bf5e2ba2SBenjamin Herrenschmidt { 862bf5e2ba2SBenjamin Herrenschmidt /* When called from the generic PCI probe, read PCI<->PCI bridge 863bf5e2ba2SBenjamin Herrenschmidt * bases before proceeding 864bf5e2ba2SBenjamin Herrenschmidt */ 865bf5e2ba2SBenjamin Herrenschmidt if (bus->self != NULL) 866bf5e2ba2SBenjamin Herrenschmidt pci_read_bridge_bases(bus); 867bf5e2ba2SBenjamin Herrenschmidt __pcibios_fixup_bus(bus); 868bf5e2ba2SBenjamin Herrenschmidt } 869bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus); 870bf5e2ba2SBenjamin Herrenschmidt 871bf5e2ba2SBenjamin Herrenschmidt /* When building a bus from the OF tree rather than probing, we need a 872bf5e2ba2SBenjamin Herrenschmidt * slightly different version of the fixup which doesn't read the 873bf5e2ba2SBenjamin Herrenschmidt * bridge bases using config space accesses 874bf5e2ba2SBenjamin Herrenschmidt */ 875bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_fixup_of_probed_bus(struct pci_bus *bus) 876bf5e2ba2SBenjamin Herrenschmidt { 877bf5e2ba2SBenjamin Herrenschmidt __pcibios_fixup_bus(bus); 878bf5e2ba2SBenjamin Herrenschmidt } 8793fd94c6bSBenjamin Herrenschmidt 8803fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev) 8813fd94c6bSBenjamin Herrenschmidt { 8823fd94c6bSBenjamin Herrenschmidt if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) && 8833fd94c6bSBenjamin Herrenschmidt !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA)) 8843fd94c6bSBenjamin Herrenschmidt return 1; 8853fd94c6bSBenjamin Herrenschmidt return 0; 8863fd94c6bSBenjamin Herrenschmidt } 8873fd94c6bSBenjamin Herrenschmidt 8883fd94c6bSBenjamin Herrenschmidt /* 8893fd94c6bSBenjamin Herrenschmidt * We need to avoid collisions with `mirrored' VGA ports 8903fd94c6bSBenjamin Herrenschmidt * and other strange ISA hardware, so we always want the 8913fd94c6bSBenjamin Herrenschmidt * addresses to be allocated in the 0x000-0x0ff region 8923fd94c6bSBenjamin Herrenschmidt * modulo 0x400. 8933fd94c6bSBenjamin Herrenschmidt * 8943fd94c6bSBenjamin Herrenschmidt * Why? Because some silly external IO cards only decode 8953fd94c6bSBenjamin Herrenschmidt * the low 10 bits of the IO address. The 0x00-0xff region 8963fd94c6bSBenjamin Herrenschmidt * is reserved for motherboard devices that decode all 16 8973fd94c6bSBenjamin Herrenschmidt * bits, so it's ok to allocate at, say, 0x2800-0x28ff, 8983fd94c6bSBenjamin Herrenschmidt * but we want to try to avoid allocating at 0x2900-0x2bff 8993fd94c6bSBenjamin Herrenschmidt * which might have be mirrored at 0x0100-0x03ff.. 9003fd94c6bSBenjamin Herrenschmidt */ 9013fd94c6bSBenjamin Herrenschmidt void pcibios_align_resource(void *data, struct resource *res, 9023fd94c6bSBenjamin Herrenschmidt resource_size_t size, resource_size_t align) 9033fd94c6bSBenjamin Herrenschmidt { 9043fd94c6bSBenjamin Herrenschmidt struct pci_dev *dev = data; 9053fd94c6bSBenjamin Herrenschmidt 9063fd94c6bSBenjamin Herrenschmidt if (res->flags & IORESOURCE_IO) { 9073fd94c6bSBenjamin Herrenschmidt resource_size_t start = res->start; 9083fd94c6bSBenjamin Herrenschmidt 9093fd94c6bSBenjamin Herrenschmidt if (skip_isa_ioresource_align(dev)) 9103fd94c6bSBenjamin Herrenschmidt return; 9113fd94c6bSBenjamin Herrenschmidt if (start & 0x300) { 9123fd94c6bSBenjamin Herrenschmidt start = (start + 0x3ff) & ~0x3ff; 9133fd94c6bSBenjamin Herrenschmidt res->start = start; 9143fd94c6bSBenjamin Herrenschmidt } 9153fd94c6bSBenjamin Herrenschmidt } 9163fd94c6bSBenjamin Herrenschmidt } 9173fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource); 9183fd94c6bSBenjamin Herrenschmidt 9193fd94c6bSBenjamin Herrenschmidt /* 9203fd94c6bSBenjamin Herrenschmidt * Reparent resource children of pr that conflict with res 9213fd94c6bSBenjamin Herrenschmidt * under res, and make res replace those children. 9223fd94c6bSBenjamin Herrenschmidt */ 9233fd94c6bSBenjamin Herrenschmidt static int __init reparent_resources(struct resource *parent, 9243fd94c6bSBenjamin Herrenschmidt struct resource *res) 9253fd94c6bSBenjamin Herrenschmidt { 9263fd94c6bSBenjamin Herrenschmidt struct resource *p, **pp; 9273fd94c6bSBenjamin Herrenschmidt struct resource **firstpp = NULL; 9283fd94c6bSBenjamin Herrenschmidt 9293fd94c6bSBenjamin Herrenschmidt for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) { 9303fd94c6bSBenjamin Herrenschmidt if (p->end < res->start) 9313fd94c6bSBenjamin Herrenschmidt continue; 9323fd94c6bSBenjamin Herrenschmidt if (res->end < p->start) 9333fd94c6bSBenjamin Herrenschmidt break; 9343fd94c6bSBenjamin Herrenschmidt if (p->start < res->start || p->end > res->end) 9353fd94c6bSBenjamin Herrenschmidt return -1; /* not completely contained */ 9363fd94c6bSBenjamin Herrenschmidt if (firstpp == NULL) 9373fd94c6bSBenjamin Herrenschmidt firstpp = pp; 9383fd94c6bSBenjamin Herrenschmidt } 9393fd94c6bSBenjamin Herrenschmidt if (firstpp == NULL) 9403fd94c6bSBenjamin Herrenschmidt return -1; /* didn't find any conflicting entries? */ 9413fd94c6bSBenjamin Herrenschmidt res->parent = parent; 9423fd94c6bSBenjamin Herrenschmidt res->child = *firstpp; 9433fd94c6bSBenjamin Herrenschmidt res->sibling = *pp; 9443fd94c6bSBenjamin Herrenschmidt *firstpp = res; 9453fd94c6bSBenjamin Herrenschmidt *pp = NULL; 9463fd94c6bSBenjamin Herrenschmidt for (p = res->child; p != NULL; p = p->sibling) { 9473fd94c6bSBenjamin Herrenschmidt p->parent = res; 9483fd94c6bSBenjamin Herrenschmidt DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n", 9493fd94c6bSBenjamin Herrenschmidt p->name, 9503fd94c6bSBenjamin Herrenschmidt (unsigned long long)p->start, 9513fd94c6bSBenjamin Herrenschmidt (unsigned long long)p->end, res->name); 9523fd94c6bSBenjamin Herrenschmidt } 9533fd94c6bSBenjamin Herrenschmidt return 0; 9543fd94c6bSBenjamin Herrenschmidt } 9553fd94c6bSBenjamin Herrenschmidt 9563fd94c6bSBenjamin Herrenschmidt /* 9573fd94c6bSBenjamin Herrenschmidt * Handle resources of PCI devices. If the world were perfect, we could 9583fd94c6bSBenjamin Herrenschmidt * just allocate all the resource regions and do nothing more. It isn't. 9593fd94c6bSBenjamin Herrenschmidt * On the other hand, we cannot just re-allocate all devices, as it would 9603fd94c6bSBenjamin Herrenschmidt * require us to know lots of host bridge internals. So we attempt to 9613fd94c6bSBenjamin Herrenschmidt * keep as much of the original configuration as possible, but tweak it 9623fd94c6bSBenjamin Herrenschmidt * when it's found to be wrong. 9633fd94c6bSBenjamin Herrenschmidt * 9643fd94c6bSBenjamin Herrenschmidt * Known BIOS problems we have to work around: 9653fd94c6bSBenjamin Herrenschmidt * - I/O or memory regions not configured 9663fd94c6bSBenjamin Herrenschmidt * - regions configured, but not enabled in the command register 9673fd94c6bSBenjamin Herrenschmidt * - bogus I/O addresses above 64K used 9683fd94c6bSBenjamin Herrenschmidt * - expansion ROMs left enabled (this may sound harmless, but given 9693fd94c6bSBenjamin Herrenschmidt * the fact the PCI specs explicitly allow address decoders to be 9703fd94c6bSBenjamin Herrenschmidt * shared between expansion ROMs and other resource regions, it's 9713fd94c6bSBenjamin Herrenschmidt * at least dangerous) 9723fd94c6bSBenjamin Herrenschmidt * 9733fd94c6bSBenjamin Herrenschmidt * Our solution: 9743fd94c6bSBenjamin Herrenschmidt * (1) Allocate resources for all buses behind PCI-to-PCI bridges. 9753fd94c6bSBenjamin Herrenschmidt * This gives us fixed barriers on where we can allocate. 9763fd94c6bSBenjamin Herrenschmidt * (2) Allocate resources for all enabled devices. If there is 9773fd94c6bSBenjamin Herrenschmidt * a collision, just mark the resource as unallocated. Also 9783fd94c6bSBenjamin Herrenschmidt * disable expansion ROMs during this step. 9793fd94c6bSBenjamin Herrenschmidt * (3) Try to allocate resources for disabled devices. If the 9803fd94c6bSBenjamin Herrenschmidt * resources were assigned correctly, everything goes well, 9813fd94c6bSBenjamin Herrenschmidt * if they weren't, they won't disturb allocation of other 9823fd94c6bSBenjamin Herrenschmidt * resources. 9833fd94c6bSBenjamin Herrenschmidt * (4) Assign new addresses to resources which were either 9843fd94c6bSBenjamin Herrenschmidt * not configured at all or misconfigured. If explicitly 9853fd94c6bSBenjamin Herrenschmidt * requested by the user, configure expansion ROM address 9863fd94c6bSBenjamin Herrenschmidt * as well. 9873fd94c6bSBenjamin Herrenschmidt */ 9883fd94c6bSBenjamin Herrenschmidt 9893fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_bus_resources(struct list_head *bus_list) 9903fd94c6bSBenjamin Herrenschmidt { 9913fd94c6bSBenjamin Herrenschmidt struct pci_bus *bus; 9923fd94c6bSBenjamin Herrenschmidt int i; 9933fd94c6bSBenjamin Herrenschmidt struct resource *res, *pr; 9943fd94c6bSBenjamin Herrenschmidt 9953fd94c6bSBenjamin Herrenschmidt /* Depth-First Search on bus tree */ 9963fd94c6bSBenjamin Herrenschmidt list_for_each_entry(bus, bus_list, node) { 9973fd94c6bSBenjamin Herrenschmidt for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) { 9983fd94c6bSBenjamin Herrenschmidt if ((res = bus->resource[i]) == NULL || !res->flags 9993fd94c6bSBenjamin Herrenschmidt || res->start > res->end) 10003fd94c6bSBenjamin Herrenschmidt continue; 10013fd94c6bSBenjamin Herrenschmidt if (bus->parent == NULL) 10023fd94c6bSBenjamin Herrenschmidt pr = (res->flags & IORESOURCE_IO) ? 10033fd94c6bSBenjamin Herrenschmidt &ioport_resource : &iomem_resource; 10043fd94c6bSBenjamin Herrenschmidt else { 10053fd94c6bSBenjamin Herrenschmidt /* Don't bother with non-root busses when 10063fd94c6bSBenjamin Herrenschmidt * re-assigning all resources. We clear the 10073fd94c6bSBenjamin Herrenschmidt * resource flags as if they were colliding 10083fd94c6bSBenjamin Herrenschmidt * and as such ensure proper re-allocation 10093fd94c6bSBenjamin Herrenschmidt * later. 10103fd94c6bSBenjamin Herrenschmidt */ 10113fd94c6bSBenjamin Herrenschmidt if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC) 10123fd94c6bSBenjamin Herrenschmidt goto clear_resource; 10133fd94c6bSBenjamin Herrenschmidt pr = pci_find_parent_resource(bus->self, res); 10143fd94c6bSBenjamin Herrenschmidt if (pr == res) { 10153fd94c6bSBenjamin Herrenschmidt /* this happens when the generic PCI 10163fd94c6bSBenjamin Herrenschmidt * code (wrongly) decides that this 10173fd94c6bSBenjamin Herrenschmidt * bridge is transparent -- paulus 10183fd94c6bSBenjamin Herrenschmidt */ 10193fd94c6bSBenjamin Herrenschmidt continue; 10203fd94c6bSBenjamin Herrenschmidt } 10213fd94c6bSBenjamin Herrenschmidt } 10223fd94c6bSBenjamin Herrenschmidt 10233fd94c6bSBenjamin Herrenschmidt DBG("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx " 10243fd94c6bSBenjamin Herrenschmidt "[0x%x], parent %p (%s)\n", 10253fd94c6bSBenjamin Herrenschmidt bus->self ? pci_name(bus->self) : "PHB", 10263fd94c6bSBenjamin Herrenschmidt bus->number, i, 10273fd94c6bSBenjamin Herrenschmidt (unsigned long long)res->start, 10283fd94c6bSBenjamin Herrenschmidt (unsigned long long)res->end, 10293fd94c6bSBenjamin Herrenschmidt (unsigned int)res->flags, 10303fd94c6bSBenjamin Herrenschmidt pr, (pr && pr->name) ? pr->name : "nil"); 10313fd94c6bSBenjamin Herrenschmidt 10323fd94c6bSBenjamin Herrenschmidt if (pr && !(pr->flags & IORESOURCE_UNSET)) { 10333fd94c6bSBenjamin Herrenschmidt if (request_resource(pr, res) == 0) 10343fd94c6bSBenjamin Herrenschmidt continue; 10353fd94c6bSBenjamin Herrenschmidt /* 10363fd94c6bSBenjamin Herrenschmidt * Must be a conflict with an existing entry. 10373fd94c6bSBenjamin Herrenschmidt * Move that entry (or entries) under the 10383fd94c6bSBenjamin Herrenschmidt * bridge resource and try again. 10393fd94c6bSBenjamin Herrenschmidt */ 10403fd94c6bSBenjamin Herrenschmidt if (reparent_resources(pr, res) == 0) 10413fd94c6bSBenjamin Herrenschmidt continue; 10423fd94c6bSBenjamin Herrenschmidt } 10433fd94c6bSBenjamin Herrenschmidt printk(KERN_WARNING 10443fd94c6bSBenjamin Herrenschmidt "PCI: Cannot allocate resource region " 10453fd94c6bSBenjamin Herrenschmidt "%d of PCI bridge %d, will remap\n", 10463fd94c6bSBenjamin Herrenschmidt i, bus->number); 10473fd94c6bSBenjamin Herrenschmidt clear_resource: 10483fd94c6bSBenjamin Herrenschmidt res->flags = 0; 10493fd94c6bSBenjamin Herrenschmidt } 10503fd94c6bSBenjamin Herrenschmidt pcibios_allocate_bus_resources(&bus->children); 10513fd94c6bSBenjamin Herrenschmidt } 10523fd94c6bSBenjamin Herrenschmidt } 10533fd94c6bSBenjamin Herrenschmidt 1054533b1928SPaul Mackerras static inline void __devinit alloc_resource(struct pci_dev *dev, int idx) 10553fd94c6bSBenjamin Herrenschmidt { 10563fd94c6bSBenjamin Herrenschmidt struct resource *pr, *r = &dev->resource[idx]; 10573fd94c6bSBenjamin Herrenschmidt 10583fd94c6bSBenjamin Herrenschmidt DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n", 10593fd94c6bSBenjamin Herrenschmidt pci_name(dev), idx, 10603fd94c6bSBenjamin Herrenschmidt (unsigned long long)r->start, 10613fd94c6bSBenjamin Herrenschmidt (unsigned long long)r->end, 10623fd94c6bSBenjamin Herrenschmidt (unsigned int)r->flags); 10633fd94c6bSBenjamin Herrenschmidt 10643fd94c6bSBenjamin Herrenschmidt pr = pci_find_parent_resource(dev, r); 10653fd94c6bSBenjamin Herrenschmidt if (!pr || (pr->flags & IORESOURCE_UNSET) || 10663fd94c6bSBenjamin Herrenschmidt request_resource(pr, r) < 0) { 10673fd94c6bSBenjamin Herrenschmidt printk(KERN_WARNING "PCI: Cannot allocate resource region %d" 10683fd94c6bSBenjamin Herrenschmidt " of device %s, will remap\n", idx, pci_name(dev)); 10693fd94c6bSBenjamin Herrenschmidt if (pr) 10703fd94c6bSBenjamin Herrenschmidt DBG("PCI: parent is %p: %016llx-%016llx [%x]\n", pr, 10713fd94c6bSBenjamin Herrenschmidt (unsigned long long)pr->start, 10723fd94c6bSBenjamin Herrenschmidt (unsigned long long)pr->end, 10733fd94c6bSBenjamin Herrenschmidt (unsigned int)pr->flags); 10743fd94c6bSBenjamin Herrenschmidt /* We'll assign a new address later */ 10753fd94c6bSBenjamin Herrenschmidt r->flags |= IORESOURCE_UNSET; 10763fd94c6bSBenjamin Herrenschmidt r->end -= r->start; 10773fd94c6bSBenjamin Herrenschmidt r->start = 0; 10783fd94c6bSBenjamin Herrenschmidt } 10793fd94c6bSBenjamin Herrenschmidt } 10803fd94c6bSBenjamin Herrenschmidt 10813fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass) 10823fd94c6bSBenjamin Herrenschmidt { 10833fd94c6bSBenjamin Herrenschmidt struct pci_dev *dev = NULL; 10843fd94c6bSBenjamin Herrenschmidt int idx, disabled; 10853fd94c6bSBenjamin Herrenschmidt u16 command; 10863fd94c6bSBenjamin Herrenschmidt struct resource *r; 10873fd94c6bSBenjamin Herrenschmidt 10883fd94c6bSBenjamin Herrenschmidt for_each_pci_dev(dev) { 10893fd94c6bSBenjamin Herrenschmidt pci_read_config_word(dev, PCI_COMMAND, &command); 10903fd94c6bSBenjamin Herrenschmidt for (idx = 0; idx < 6; idx++) { 10913fd94c6bSBenjamin Herrenschmidt r = &dev->resource[idx]; 10923fd94c6bSBenjamin Herrenschmidt if (r->parent) /* Already allocated */ 10933fd94c6bSBenjamin Herrenschmidt continue; 10943fd94c6bSBenjamin Herrenschmidt if (!r->flags || (r->flags & IORESOURCE_UNSET)) 10953fd94c6bSBenjamin Herrenschmidt continue; /* Not assigned at all */ 10963fd94c6bSBenjamin Herrenschmidt if (r->flags & IORESOURCE_IO) 10973fd94c6bSBenjamin Herrenschmidt disabled = !(command & PCI_COMMAND_IO); 10983fd94c6bSBenjamin Herrenschmidt else 10993fd94c6bSBenjamin Herrenschmidt disabled = !(command & PCI_COMMAND_MEMORY); 1100533b1928SPaul Mackerras if (pass == disabled) 1101533b1928SPaul Mackerras alloc_resource(dev, idx); 11023fd94c6bSBenjamin Herrenschmidt } 11033fd94c6bSBenjamin Herrenschmidt if (pass) 11043fd94c6bSBenjamin Herrenschmidt continue; 11053fd94c6bSBenjamin Herrenschmidt r = &dev->resource[PCI_ROM_RESOURCE]; 11063fd94c6bSBenjamin Herrenschmidt if (r->flags & IORESOURCE_ROM_ENABLE) { 11073fd94c6bSBenjamin Herrenschmidt /* Turn the ROM off, leave the resource region, 11083fd94c6bSBenjamin Herrenschmidt * but keep it unregistered. 11093fd94c6bSBenjamin Herrenschmidt */ 11103fd94c6bSBenjamin Herrenschmidt u32 reg; 11113fd94c6bSBenjamin Herrenschmidt DBG("PCI: Switching off ROM of %s\n", pci_name(dev)); 11123fd94c6bSBenjamin Herrenschmidt r->flags &= ~IORESOURCE_ROM_ENABLE; 11133fd94c6bSBenjamin Herrenschmidt pci_read_config_dword(dev, dev->rom_base_reg, ®); 11143fd94c6bSBenjamin Herrenschmidt pci_write_config_dword(dev, dev->rom_base_reg, 11153fd94c6bSBenjamin Herrenschmidt reg & ~PCI_ROM_ADDRESS_ENABLE); 11163fd94c6bSBenjamin Herrenschmidt } 11173fd94c6bSBenjamin Herrenschmidt } 11183fd94c6bSBenjamin Herrenschmidt } 11193fd94c6bSBenjamin Herrenschmidt 11203fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void) 11213fd94c6bSBenjamin Herrenschmidt { 11223fd94c6bSBenjamin Herrenschmidt /* Allocate and assign resources. If we re-assign everything, then 11233fd94c6bSBenjamin Herrenschmidt * we skip the allocate phase 11243fd94c6bSBenjamin Herrenschmidt */ 11253fd94c6bSBenjamin Herrenschmidt pcibios_allocate_bus_resources(&pci_root_buses); 11263fd94c6bSBenjamin Herrenschmidt 11273fd94c6bSBenjamin Herrenschmidt if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) { 11283fd94c6bSBenjamin Herrenschmidt pcibios_allocate_resources(0); 11293fd94c6bSBenjamin Herrenschmidt pcibios_allocate_resources(1); 11303fd94c6bSBenjamin Herrenschmidt } 11313fd94c6bSBenjamin Herrenschmidt 11323fd94c6bSBenjamin Herrenschmidt if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) { 11333fd94c6bSBenjamin Herrenschmidt DBG("PCI: Assigning unassigned resouces...\n"); 11343fd94c6bSBenjamin Herrenschmidt pci_assign_unassigned_resources(); 11353fd94c6bSBenjamin Herrenschmidt } 11363fd94c6bSBenjamin Herrenschmidt 11373fd94c6bSBenjamin Herrenschmidt /* Call machine dependent fixup */ 11383fd94c6bSBenjamin Herrenschmidt if (ppc_md.pcibios_fixup) 11393fd94c6bSBenjamin Herrenschmidt ppc_md.pcibios_fixup(); 11403fd94c6bSBenjamin Herrenschmidt } 11413fd94c6bSBenjamin Herrenschmidt 11423fd94c6bSBenjamin Herrenschmidt #ifdef CONFIG_HOTPLUG 11433fd94c6bSBenjamin Herrenschmidt /* This is used by the pSeries hotplug driver to allocate resource 11443fd94c6bSBenjamin Herrenschmidt * of newly plugged busses. We can try to consolidate with the 11453fd94c6bSBenjamin Herrenschmidt * rest of the code later, for now, keep it as-is 11463fd94c6bSBenjamin Herrenschmidt */ 11473fd94c6bSBenjamin Herrenschmidt void __devinit pcibios_claim_one_bus(struct pci_bus *bus) 11483fd94c6bSBenjamin Herrenschmidt { 11493fd94c6bSBenjamin Herrenschmidt struct pci_dev *dev; 11503fd94c6bSBenjamin Herrenschmidt struct pci_bus *child_bus; 11513fd94c6bSBenjamin Herrenschmidt 11523fd94c6bSBenjamin Herrenschmidt list_for_each_entry(dev, &bus->devices, bus_list) { 11533fd94c6bSBenjamin Herrenschmidt int i; 11543fd94c6bSBenjamin Herrenschmidt 11553fd94c6bSBenjamin Herrenschmidt for (i = 0; i < PCI_NUM_RESOURCES; i++) { 11563fd94c6bSBenjamin Herrenschmidt struct resource *r = &dev->resource[i]; 11573fd94c6bSBenjamin Herrenschmidt 11583fd94c6bSBenjamin Herrenschmidt if (r->parent || !r->start || !r->flags) 11593fd94c6bSBenjamin Herrenschmidt continue; 11603fd94c6bSBenjamin Herrenschmidt pci_claim_resource(dev, i); 11613fd94c6bSBenjamin Herrenschmidt } 11623fd94c6bSBenjamin Herrenschmidt } 11633fd94c6bSBenjamin Herrenschmidt 11643fd94c6bSBenjamin Herrenschmidt list_for_each_entry(child_bus, &bus->children, node) 11653fd94c6bSBenjamin Herrenschmidt pcibios_claim_one_bus(child_bus); 11663fd94c6bSBenjamin Herrenschmidt } 11673fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_claim_one_bus); 11683fd94c6bSBenjamin Herrenschmidt #endif /* CONFIG_HOTPLUG */ 1169549beb9bSBenjamin Herrenschmidt 1170549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask) 1171549beb9bSBenjamin Herrenschmidt { 1172549beb9bSBenjamin Herrenschmidt if (ppc_md.pcibios_enable_device_hook) 1173549beb9bSBenjamin Herrenschmidt if (ppc_md.pcibios_enable_device_hook(dev)) 1174549beb9bSBenjamin Herrenschmidt return -EINVAL; 1175549beb9bSBenjamin Herrenschmidt 11767cfb5f9aSBjorn Helgaas return pci_enable_resources(dev, mask); 1177549beb9bSBenjamin Herrenschmidt } 1178