17568cb4eSPaul Mackerras /* 27568cb4eSPaul Mackerras * Port for PPC64 David Engebretsen, IBM Corp. 37568cb4eSPaul Mackerras * Contains common pci routines for ppc64 platform, pSeries and iSeries brands. 47568cb4eSPaul Mackerras * 57568cb4eSPaul Mackerras * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM 67568cb4eSPaul Mackerras * Rework, based on alpha PCI code. 77568cb4eSPaul Mackerras * 87568cb4eSPaul Mackerras * This program is free software; you can redistribute it and/or 97568cb4eSPaul Mackerras * modify it under the terms of the GNU General Public License 107568cb4eSPaul Mackerras * as published by the Free Software Foundation; either version 117568cb4eSPaul Mackerras * 2 of the License, or (at your option) any later version. 127568cb4eSPaul Mackerras */ 137568cb4eSPaul Mackerras 147568cb4eSPaul Mackerras #undef DEBUG 157568cb4eSPaul Mackerras 167568cb4eSPaul Mackerras #include <linux/kernel.h> 177568cb4eSPaul Mackerras #include <linux/pci.h> 187568cb4eSPaul Mackerras #include <linux/string.h> 197568cb4eSPaul Mackerras #include <linux/init.h> 2066b15db6SPaul Gortmaker #include <linux/export.h> 217568cb4eSPaul Mackerras #include <linux/mm.h> 227568cb4eSPaul Mackerras #include <linux/list.h> 237568cb4eSPaul Mackerras #include <linux/syscalls.h> 246e99e458SBenjamin Herrenschmidt #include <linux/irq.h> 253d5134eeSBenjamin Herrenschmidt #include <linux/vmalloc.h> 267568cb4eSPaul Mackerras 277568cb4eSPaul Mackerras #include <asm/processor.h> 287568cb4eSPaul Mackerras #include <asm/io.h> 297568cb4eSPaul Mackerras #include <asm/prom.h> 307568cb4eSPaul Mackerras #include <asm/pci-bridge.h> 317568cb4eSPaul Mackerras #include <asm/byteorder.h> 327568cb4eSPaul Mackerras #include <asm/machdep.h> 337568cb4eSPaul Mackerras #include <asm/ppc-pci.h> 347568cb4eSPaul Mackerras 357568cb4eSPaul Mackerras /* pci_io_base -- the base address from which io bars are offsets. 367568cb4eSPaul Mackerras * This is the lowest I/O base address (so bar values are always positive), 377568cb4eSPaul Mackerras * and it *must* be the start of ISA space if an ISA bus exists because 383d5134eeSBenjamin Herrenschmidt * ISA drivers use hard coded offsets. If no ISA bus exists nothing 393d5134eeSBenjamin Herrenschmidt * is mapped on the first 64K of IO space 407568cb4eSPaul Mackerras */ 41d6a9996eSAneesh Kumar K.V unsigned long pci_io_base; 427568cb4eSPaul Mackerras EXPORT_SYMBOL(pci_io_base); 437568cb4eSPaul Mackerras 447568cb4eSPaul Mackerras static int __init pcibios_init(void) 457568cb4eSPaul Mackerras { 467568cb4eSPaul Mackerras struct pci_controller *hose, *tmp; 477568cb4eSPaul Mackerras 483fd94c6bSBenjamin Herrenschmidt printk(KERN_INFO "PCI: Probing PCI hardware\n"); 493fd94c6bSBenjamin Herrenschmidt 5053280323SBenjamin Herrenschmidt /* For now, override phys_mem_access_prot. If we need it,g 517568cb4eSPaul Mackerras * later, we may move that initialization to each ppc_md 527568cb4eSPaul Mackerras */ 537568cb4eSPaul Mackerras ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot; 547568cb4eSPaul Mackerras 551fd0f525SBenjamin Herrenschmidt /* On ppc64, we always enable PCI domains and we keep domain 0 561fd0f525SBenjamin Herrenschmidt * backward compatible in /proc for video cards 571fd0f525SBenjamin Herrenschmidt */ 580e47ff1cSRob Herring pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0); 591fd0f525SBenjamin Herrenschmidt 607568cb4eSPaul Mackerras /* Scan all of the recorded PCI controllers. */ 6192eb4602SJohn Rose list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 62b5d937deSGrant Likely pcibios_scan_phb(hose); 6392eb4602SJohn Rose pci_bus_add_devices(hose->bus); 6492eb4602SJohn Rose } 657568cb4eSPaul Mackerras 663fd94c6bSBenjamin Herrenschmidt /* Call common code to handle resource allocation */ 673fd94c6bSBenjamin Herrenschmidt pcibios_resource_survey(); 687568cb4eSPaul Mackerras 69e884e9c5SOlof Johansson printk(KERN_DEBUG "PCI: Probing PCI hardware done\n"); 707568cb4eSPaul Mackerras 717568cb4eSPaul Mackerras return 0; 727568cb4eSPaul Mackerras } 737568cb4eSPaul Mackerras 747568cb4eSPaul Mackerras subsys_initcall(pcibios_init); 757568cb4eSPaul Mackerras 763d5134eeSBenjamin Herrenschmidt int pcibios_unmap_io_space(struct pci_bus *bus) 777568cb4eSPaul Mackerras { 783d5134eeSBenjamin Herrenschmidt struct pci_controller *hose; 797568cb4eSPaul Mackerras 803d5134eeSBenjamin Herrenschmidt WARN_ON(bus == NULL); 81de821204SBenjamin Herrenschmidt 823d5134eeSBenjamin Herrenschmidt /* If this is not a PHB, we only flush the hash table over 833d5134eeSBenjamin Herrenschmidt * the area mapped by this bridge. We don't play with the PTE 84027dfac6SMichael Ellerman * mappings since we might have to deal with sub-page alignments 853d5134eeSBenjamin Herrenschmidt * so flushing the hash table is the only sane way to make sure 863d5134eeSBenjamin Herrenschmidt * that no hash entries are covering that removed bridge area 873d5134eeSBenjamin Herrenschmidt * while still allowing other busses overlapping those pages 8894491685SBenjamin Herrenschmidt * 8994491685SBenjamin Herrenschmidt * Note: If we ever support P2P hotplug on Book3E, we'll have 9094491685SBenjamin Herrenschmidt * to do an appropriate TLB flush here too 91de821204SBenjamin Herrenschmidt */ 923d5134eeSBenjamin Herrenschmidt if (bus->self) { 934e003747SMichael Ellerman #ifdef CONFIG_PPC_BOOK3S_64 943d5134eeSBenjamin Herrenschmidt struct resource *res = bus->resource[0]; 95ce7a35c7SKumar Gala #endif 963d5134eeSBenjamin Herrenschmidt 97b0494bc8SBenjamin Herrenschmidt pr_debug("IO unmapping for PCI-PCI bridge %s\n", 983d5134eeSBenjamin Herrenschmidt pci_name(bus->self)); 993d5134eeSBenjamin Herrenschmidt 1004e003747SMichael Ellerman #ifdef CONFIG_PPC_BOOK3S_64 1013d5134eeSBenjamin Herrenschmidt __flush_hash_table_range(&init_mm, res->start + _IO_BASE, 102b30115eaSBenjamin Herrenschmidt res->end + _IO_BASE + 1); 10394491685SBenjamin Herrenschmidt #endif 1043d5134eeSBenjamin Herrenschmidt return 0; 1057568cb4eSPaul Mackerras } 1067568cb4eSPaul Mackerras 1073d5134eeSBenjamin Herrenschmidt /* Get the host bridge */ 1083d5134eeSBenjamin Herrenschmidt hose = pci_bus_to_host(bus); 1093d5134eeSBenjamin Herrenschmidt 1103d5134eeSBenjamin Herrenschmidt /* Check if we have IOs allocated */ 111b0d436c7SAnton Blanchard if (hose->io_base_alloc == NULL) 1123d5134eeSBenjamin Herrenschmidt return 0; 1133d5134eeSBenjamin Herrenschmidt 114b7c670d6SRob Herring pr_debug("IO unmapping for PHB %pOF\n", hose->dn); 115b0494bc8SBenjamin Herrenschmidt pr_debug(" alloc=0x%p\n", hose->io_base_alloc); 1163d5134eeSBenjamin Herrenschmidt 1173d5134eeSBenjamin Herrenschmidt /* This is a PHB, we fully unmap the IO area */ 1183d5134eeSBenjamin Herrenschmidt vunmap(hose->io_base_alloc); 1193d5134eeSBenjamin Herrenschmidt 1203d5134eeSBenjamin Herrenschmidt return 0; 1213d5134eeSBenjamin Herrenschmidt } 1223d5134eeSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_unmap_io_space); 1233d5134eeSBenjamin Herrenschmidt 124cad5cef6SGreg Kroah-Hartman static int pcibios_map_phb_io_space(struct pci_controller *hose) 1257568cb4eSPaul Mackerras { 1263d5134eeSBenjamin Herrenschmidt struct vm_struct *area; 1273d5134eeSBenjamin Herrenschmidt unsigned long phys_page; 1283d5134eeSBenjamin Herrenschmidt unsigned long size_page; 1297568cb4eSPaul Mackerras unsigned long io_virt_offset; 1307568cb4eSPaul Mackerras 1313d5134eeSBenjamin Herrenschmidt phys_page = _ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE); 1323d5134eeSBenjamin Herrenschmidt size_page = _ALIGN_UP(hose->pci_io_size, PAGE_SIZE); 1337568cb4eSPaul Mackerras 1343d5134eeSBenjamin Herrenschmidt /* Make sure IO area address is clear */ 1353d5134eeSBenjamin Herrenschmidt hose->io_base_alloc = NULL; 1367568cb4eSPaul Mackerras 1373d5134eeSBenjamin Herrenschmidt /* If there's no IO to map on that bus, get away too */ 1383d5134eeSBenjamin Herrenschmidt if (hose->pci_io_size == 0 || hose->io_base_phys == 0) 1393d5134eeSBenjamin Herrenschmidt return 0; 1403d5134eeSBenjamin Herrenschmidt 1413d5134eeSBenjamin Herrenschmidt /* Let's allocate some IO space for that guy. We don't pass 1423d5134eeSBenjamin Herrenschmidt * VM_IOREMAP because we don't care about alignment tricks that 1433d5134eeSBenjamin Herrenschmidt * the core does in that case. Maybe we should due to stupid card 1443d5134eeSBenjamin Herrenschmidt * with incomplete address decoding but I'd rather not deal with 1453d5134eeSBenjamin Herrenschmidt * those outside of the reserved 64K legacy region. 1463d5134eeSBenjamin Herrenschmidt */ 1473d5134eeSBenjamin Herrenschmidt area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END); 1483d5134eeSBenjamin Herrenschmidt if (area == NULL) 1493d5134eeSBenjamin Herrenschmidt return -ENOMEM; 1503d5134eeSBenjamin Herrenschmidt hose->io_base_alloc = area->addr; 1513d5134eeSBenjamin Herrenschmidt hose->io_base_virt = (void __iomem *)(area->addr + 1523d5134eeSBenjamin Herrenschmidt hose->io_base_phys - phys_page); 1533d5134eeSBenjamin Herrenschmidt 154b7c670d6SRob Herring pr_debug("IO mapping for PHB %pOF\n", hose->dn); 1559477e455SStephen Rothwell pr_debug(" phys=0x%016llx, virt=0x%p (alloc=0x%p)\n", 1563d5134eeSBenjamin Herrenschmidt hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc); 157bcba0778SStephen Rothwell pr_debug(" size=0x%016llx (alloc=0x%016lx)\n", 1583d5134eeSBenjamin Herrenschmidt hose->pci_io_size, size_page); 1593d5134eeSBenjamin Herrenschmidt 1603d5134eeSBenjamin Herrenschmidt /* Establish the mapping */ 1613d5134eeSBenjamin Herrenschmidt if (__ioremap_at(phys_page, area->addr, size_page, 16272176dd0SAneesh Kumar K.V pgprot_val(pgprot_noncached(__pgprot(0)))) == NULL) 1633d5134eeSBenjamin Herrenschmidt return -ENOMEM; 1643d5134eeSBenjamin Herrenschmidt 1653d5134eeSBenjamin Herrenschmidt /* Fixup hose IO resource */ 16638973ba7SBjorn Helgaas io_virt_offset = pcibios_io_space_offset(hose); 1673d5134eeSBenjamin Herrenschmidt hose->io_resource.start += io_virt_offset; 1683d5134eeSBenjamin Herrenschmidt hose->io_resource.end += io_virt_offset; 1693d5134eeSBenjamin Herrenschmidt 170518fdae2SJoe Perches pr_debug(" hose->io_resource=%pR\n", &hose->io_resource); 1717568cb4eSPaul Mackerras 1727568cb4eSPaul Mackerras return 0; 1737568cb4eSPaul Mackerras } 17449a6cba4SBjorn Helgaas 175cad5cef6SGreg Kroah-Hartman int pcibios_map_io_space(struct pci_bus *bus) 17649a6cba4SBjorn Helgaas { 17749a6cba4SBjorn Helgaas WARN_ON(bus == NULL); 17849a6cba4SBjorn Helgaas 17949a6cba4SBjorn Helgaas /* If this not a PHB, nothing to do, page tables still exist and 18049a6cba4SBjorn Helgaas * thus HPTEs will be faulted in when needed 18149a6cba4SBjorn Helgaas */ 18249a6cba4SBjorn Helgaas if (bus->self) { 18349a6cba4SBjorn Helgaas pr_debug("IO mapping for PCI-PCI bridge %s\n", 18449a6cba4SBjorn Helgaas pci_name(bus->self)); 18549a6cba4SBjorn Helgaas pr_debug(" virt=0x%016llx...0x%016llx\n", 18649a6cba4SBjorn Helgaas bus->resource[0]->start + _IO_BASE, 18749a6cba4SBjorn Helgaas bus->resource[0]->end + _IO_BASE); 18849a6cba4SBjorn Helgaas return 0; 18949a6cba4SBjorn Helgaas } 19049a6cba4SBjorn Helgaas 19149a6cba4SBjorn Helgaas return pcibios_map_phb_io_space(pci_bus_to_host(bus)); 19249a6cba4SBjorn Helgaas } 1933d5134eeSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_map_io_space); 1947568cb4eSPaul Mackerras 195cad5cef6SGreg Kroah-Hartman void pcibios_setup_phb_io_space(struct pci_controller *hose) 1960ed2c722SGrant Likely { 19749a6cba4SBjorn Helgaas pcibios_map_phb_io_space(hose); 1980ed2c722SGrant Likely } 1990ed2c722SGrant Likely 2007568cb4eSPaul Mackerras #define IOBASE_BRIDGE_NUMBER 0 2017568cb4eSPaul Mackerras #define IOBASE_MEMORY 1 2027568cb4eSPaul Mackerras #define IOBASE_IO 2 2037568cb4eSPaul Mackerras #define IOBASE_ISA_IO 3 2047568cb4eSPaul Mackerras #define IOBASE_ISA_MEM 4 2057568cb4eSPaul Mackerras 206*3691d614SAl Viro SYSCALL_DEFINE3(pciconfig_iobase, long, which, unsigned long, in_bus, 207*3691d614SAl Viro unsigned long, in_devfn) 2087568cb4eSPaul Mackerras { 2097568cb4eSPaul Mackerras struct pci_controller* hose; 210140ab645SMike Qiu struct pci_bus *tmp_bus, *bus = NULL; 2117568cb4eSPaul Mackerras struct device_node *hose_node; 2127568cb4eSPaul Mackerras 2137568cb4eSPaul Mackerras /* Argh ! Please forgive me for that hack, but that's the 2147568cb4eSPaul Mackerras * simplest way to get existing XFree to not lockup on some 2157568cb4eSPaul Mackerras * G5 machines... So when something asks for bus 0 io base 2167568cb4eSPaul Mackerras * (bus 0 is HT root), we return the AGP one instead. 2177568cb4eSPaul Mackerras */ 21871a157e8SGrant Likely if (in_bus == 0 && of_machine_is_compatible("MacRISC4")) { 21916124f10SPaul Mackerras struct device_node *agp; 22016124f10SPaul Mackerras 22116124f10SPaul Mackerras agp = of_find_compatible_node(NULL, NULL, "u3-agp"); 22216124f10SPaul Mackerras if (agp) 2237568cb4eSPaul Mackerras in_bus = 0xf0; 22416124f10SPaul Mackerras of_node_put(agp); 22516124f10SPaul Mackerras } 2267568cb4eSPaul Mackerras 2277568cb4eSPaul Mackerras /* That syscall isn't quite compatible with PCI domains, but it's 2287568cb4eSPaul Mackerras * used on pre-domains setup. We return the first match 2297568cb4eSPaul Mackerras */ 2307568cb4eSPaul Mackerras 231140ab645SMike Qiu list_for_each_entry(tmp_bus, &pci_root_buses, node) { 232140ab645SMike Qiu if (in_bus >= tmp_bus->number && 233140ab645SMike Qiu in_bus <= tmp_bus->busn_res.end) { 234140ab645SMike Qiu bus = tmp_bus; 2357568cb4eSPaul Mackerras break; 236140ab645SMike Qiu } 2377568cb4eSPaul Mackerras } 238b5d937deSGrant Likely if (bus == NULL || bus->dev.of_node == NULL) 2397568cb4eSPaul Mackerras return -ENODEV; 2407568cb4eSPaul Mackerras 241b5d937deSGrant Likely hose_node = bus->dev.of_node; 2427568cb4eSPaul Mackerras hose = PCI_DN(hose_node)->phb; 2437568cb4eSPaul Mackerras 2447568cb4eSPaul Mackerras switch (which) { 2457568cb4eSPaul Mackerras case IOBASE_BRIDGE_NUMBER: 2467568cb4eSPaul Mackerras return (long)hose->first_busno; 2477568cb4eSPaul Mackerras case IOBASE_MEMORY: 2483fd47f06SBenjamin Herrenschmidt return (long)hose->mem_offset[0]; 2497568cb4eSPaul Mackerras case IOBASE_IO: 2507568cb4eSPaul Mackerras return (long)hose->io_base_phys; 2517568cb4eSPaul Mackerras case IOBASE_ISA_IO: 2527568cb4eSPaul Mackerras return (long)isa_io_base; 2537568cb4eSPaul Mackerras case IOBASE_ISA_MEM: 2547568cb4eSPaul Mackerras return -EINVAL; 2557568cb4eSPaul Mackerras } 2567568cb4eSPaul Mackerras 2577568cb4eSPaul Mackerras return -EOPNOTSUPP; 2587568cb4eSPaul Mackerras } 259357518faSAnton Blanchard 260357518faSAnton Blanchard #ifdef CONFIG_NUMA 261357518faSAnton Blanchard int pcibus_to_node(struct pci_bus *bus) 262357518faSAnton Blanchard { 263357518faSAnton Blanchard struct pci_controller *phb = pci_bus_to_host(bus); 264357518faSAnton Blanchard return phb->node; 265357518faSAnton Blanchard } 266357518faSAnton Blanchard EXPORT_SYMBOL(pcibus_to_node); 267357518faSAnton Blanchard #endif 268