xref: /linux/arch/powerpc/kernel/pci_64.c (revision b711531641038f3ff3723914f3d5ba79848d347e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27568cb4eSPaul Mackerras /*
37568cb4eSPaul Mackerras  * Port for PPC64 David Engebretsen, IBM Corp.
47568cb4eSPaul Mackerras  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
57568cb4eSPaul Mackerras  *
67568cb4eSPaul Mackerras  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
77568cb4eSPaul Mackerras  *   Rework, based on alpha PCI code.
87568cb4eSPaul Mackerras  */
97568cb4eSPaul Mackerras 
107568cb4eSPaul Mackerras #undef DEBUG
117568cb4eSPaul Mackerras 
127568cb4eSPaul Mackerras #include <linux/kernel.h>
137568cb4eSPaul Mackerras #include <linux/pci.h>
147568cb4eSPaul Mackerras #include <linux/string.h>
157568cb4eSPaul Mackerras #include <linux/init.h>
1666b15db6SPaul Gortmaker #include <linux/export.h>
177568cb4eSPaul Mackerras #include <linux/mm.h>
187568cb4eSPaul Mackerras #include <linux/list.h>
197568cb4eSPaul Mackerras #include <linux/syscalls.h>
206e99e458SBenjamin Herrenschmidt #include <linux/irq.h>
213d5134eeSBenjamin Herrenschmidt #include <linux/vmalloc.h>
227568cb4eSPaul Mackerras 
237568cb4eSPaul Mackerras #include <asm/processor.h>
247568cb4eSPaul Mackerras #include <asm/io.h>
257568cb4eSPaul Mackerras #include <asm/prom.h>
267568cb4eSPaul Mackerras #include <asm/pci-bridge.h>
277568cb4eSPaul Mackerras #include <asm/byteorder.h>
287568cb4eSPaul Mackerras #include <asm/machdep.h>
297568cb4eSPaul Mackerras #include <asm/ppc-pci.h>
307568cb4eSPaul Mackerras 
317568cb4eSPaul Mackerras /* pci_io_base -- the base address from which io bars are offsets.
327568cb4eSPaul Mackerras  * This is the lowest I/O base address (so bar values are always positive),
337568cb4eSPaul Mackerras  * and it *must* be the start of ISA space if an ISA bus exists because
343d5134eeSBenjamin Herrenschmidt  * ISA drivers use hard coded offsets.  If no ISA bus exists nothing
353d5134eeSBenjamin Herrenschmidt  * is mapped on the first 64K of IO space
367568cb4eSPaul Mackerras  */
37d6a9996eSAneesh Kumar K.V unsigned long pci_io_base;
387568cb4eSPaul Mackerras EXPORT_SYMBOL(pci_io_base);
397568cb4eSPaul Mackerras 
407568cb4eSPaul Mackerras static int __init pcibios_init(void)
417568cb4eSPaul Mackerras {
427568cb4eSPaul Mackerras 	struct pci_controller *hose, *tmp;
437568cb4eSPaul Mackerras 
443fd94c6bSBenjamin Herrenschmidt 	printk(KERN_INFO "PCI: Probing PCI hardware\n");
453fd94c6bSBenjamin Herrenschmidt 
4653280323SBenjamin Herrenschmidt 	/* For now, override phys_mem_access_prot. If we need it,g
477568cb4eSPaul Mackerras 	 * later, we may move that initialization to each ppc_md
487568cb4eSPaul Mackerras 	 */
497568cb4eSPaul Mackerras 	ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
507568cb4eSPaul Mackerras 
511fd0f525SBenjamin Herrenschmidt 	/* On ppc64, we always enable PCI domains and we keep domain 0
521fd0f525SBenjamin Herrenschmidt 	 * backward compatible in /proc for video cards
531fd0f525SBenjamin Herrenschmidt 	 */
540e47ff1cSRob Herring 	pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0);
551fd0f525SBenjamin Herrenschmidt 
567568cb4eSPaul Mackerras 	/* Scan all of the recorded PCI controllers.  */
573f068aaeSSam Bobroff 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
58b5d937deSGrant Likely 		pcibios_scan_phb(hose);
597568cb4eSPaul Mackerras 
603fd94c6bSBenjamin Herrenschmidt 	/* Call common code to handle resource allocation */
613fd94c6bSBenjamin Herrenschmidt 	pcibios_resource_survey();
627568cb4eSPaul Mackerras 
633f068aaeSSam Bobroff 	/* Add devices. */
643f068aaeSSam Bobroff 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
653f068aaeSSam Bobroff 		pci_bus_add_devices(hose->bus);
663f068aaeSSam Bobroff 
673f068aaeSSam Bobroff 	/* Call machine dependent fixup */
683f068aaeSSam Bobroff 	if (ppc_md.pcibios_fixup)
693f068aaeSSam Bobroff 		ppc_md.pcibios_fixup();
703f068aaeSSam Bobroff 
71e884e9c5SOlof Johansson 	printk(KERN_DEBUG "PCI: Probing PCI hardware done\n");
727568cb4eSPaul Mackerras 
737568cb4eSPaul Mackerras 	return 0;
747568cb4eSPaul Mackerras }
757568cb4eSPaul Mackerras 
767568cb4eSPaul Mackerras subsys_initcall(pcibios_init);
777568cb4eSPaul Mackerras 
783d5134eeSBenjamin Herrenschmidt int pcibios_unmap_io_space(struct pci_bus *bus)
797568cb4eSPaul Mackerras {
803d5134eeSBenjamin Herrenschmidt 	struct pci_controller *hose;
817568cb4eSPaul Mackerras 
823d5134eeSBenjamin Herrenschmidt 	WARN_ON(bus == NULL);
83de821204SBenjamin Herrenschmidt 
843d5134eeSBenjamin Herrenschmidt 	/* If this is not a PHB, we only flush the hash table over
853d5134eeSBenjamin Herrenschmidt 	 * the area mapped by this bridge. We don't play with the PTE
86027dfac6SMichael Ellerman 	 * mappings since we might have to deal with sub-page alignments
873d5134eeSBenjamin Herrenschmidt 	 * so flushing the hash table is the only sane way to make sure
883d5134eeSBenjamin Herrenschmidt 	 * that no hash entries are covering that removed bridge area
893d5134eeSBenjamin Herrenschmidt 	 * while still allowing other busses overlapping those pages
9094491685SBenjamin Herrenschmidt 	 *
9194491685SBenjamin Herrenschmidt 	 * Note: If we ever support P2P hotplug on Book3E, we'll have
9294491685SBenjamin Herrenschmidt 	 * to do an appropriate TLB flush here too
93de821204SBenjamin Herrenschmidt 	 */
943d5134eeSBenjamin Herrenschmidt 	if (bus->self) {
954e003747SMichael Ellerman #ifdef CONFIG_PPC_BOOK3S_64
963d5134eeSBenjamin Herrenschmidt 		struct resource *res = bus->resource[0];
97ce7a35c7SKumar Gala #endif
983d5134eeSBenjamin Herrenschmidt 
99b0494bc8SBenjamin Herrenschmidt 		pr_debug("IO unmapping for PCI-PCI bridge %s\n",
1003d5134eeSBenjamin Herrenschmidt 			 pci_name(bus->self));
1013d5134eeSBenjamin Herrenschmidt 
1024e003747SMichael Ellerman #ifdef CONFIG_PPC_BOOK3S_64
1037900757cSAneesh Kumar K.V 		__flush_hash_table_range(res->start + _IO_BASE,
104b30115eaSBenjamin Herrenschmidt 					 res->end + _IO_BASE + 1);
10594491685SBenjamin Herrenschmidt #endif
1063d5134eeSBenjamin Herrenschmidt 		return 0;
1077568cb4eSPaul Mackerras 	}
1087568cb4eSPaul Mackerras 
1093d5134eeSBenjamin Herrenschmidt 	/* Get the host bridge */
1103d5134eeSBenjamin Herrenschmidt 	hose = pci_bus_to_host(bus);
1113d5134eeSBenjamin Herrenschmidt 
1123d5134eeSBenjamin Herrenschmidt 	/* Check if we have IOs allocated */
113b0d436c7SAnton Blanchard 	if (hose->io_base_alloc == NULL)
1143d5134eeSBenjamin Herrenschmidt 		return 0;
1153d5134eeSBenjamin Herrenschmidt 
116b7c670d6SRob Herring 	pr_debug("IO unmapping for PHB %pOF\n", hose->dn);
117b0494bc8SBenjamin Herrenschmidt 	pr_debug("  alloc=0x%p\n", hose->io_base_alloc);
1183d5134eeSBenjamin Herrenschmidt 
1193d5134eeSBenjamin Herrenschmidt 	/* This is a PHB, we fully unmap the IO area */
1203d5134eeSBenjamin Herrenschmidt 	vunmap(hose->io_base_alloc);
1213d5134eeSBenjamin Herrenschmidt 
1223d5134eeSBenjamin Herrenschmidt 	return 0;
1233d5134eeSBenjamin Herrenschmidt }
1243d5134eeSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_unmap_io_space);
1253d5134eeSBenjamin Herrenschmidt 
126cad5cef6SGreg Kroah-Hartman static int pcibios_map_phb_io_space(struct pci_controller *hose)
1277568cb4eSPaul Mackerras {
1283d5134eeSBenjamin Herrenschmidt 	struct vm_struct *area;
1293d5134eeSBenjamin Herrenschmidt 	unsigned long phys_page;
1303d5134eeSBenjamin Herrenschmidt 	unsigned long size_page;
1317568cb4eSPaul Mackerras 	unsigned long io_virt_offset;
1327568cb4eSPaul Mackerras 
133e96d904eSChristophe Leroy 	phys_page = ALIGN_DOWN(hose->io_base_phys, PAGE_SIZE);
134*b7115316SChristophe Leroy 	size_page = ALIGN(hose->pci_io_size, PAGE_SIZE);
1357568cb4eSPaul Mackerras 
1363d5134eeSBenjamin Herrenschmidt 	/* Make sure IO area address is clear */
1373d5134eeSBenjamin Herrenschmidt 	hose->io_base_alloc = NULL;
1387568cb4eSPaul Mackerras 
1393d5134eeSBenjamin Herrenschmidt 	/* If there's no IO to map on that bus, get away too */
1403d5134eeSBenjamin Herrenschmidt 	if (hose->pci_io_size == 0 || hose->io_base_phys == 0)
1413d5134eeSBenjamin Herrenschmidt 		return 0;
1423d5134eeSBenjamin Herrenschmidt 
1433d5134eeSBenjamin Herrenschmidt 	/* Let's allocate some IO space for that guy. We don't pass
1443d5134eeSBenjamin Herrenschmidt 	 * VM_IOREMAP because we don't care about alignment tricks that
1453d5134eeSBenjamin Herrenschmidt 	 * the core does in that case. Maybe we should due to stupid card
1463d5134eeSBenjamin Herrenschmidt 	 * with incomplete address decoding but I'd rather not deal with
1473d5134eeSBenjamin Herrenschmidt 	 * those outside of the reserved 64K legacy region.
1483d5134eeSBenjamin Herrenschmidt 	 */
1493d5134eeSBenjamin Herrenschmidt 	area = __get_vm_area(size_page, 0, PHB_IO_BASE, PHB_IO_END);
1503d5134eeSBenjamin Herrenschmidt 	if (area == NULL)
1513d5134eeSBenjamin Herrenschmidt 		return -ENOMEM;
1523d5134eeSBenjamin Herrenschmidt 	hose->io_base_alloc = area->addr;
1533d5134eeSBenjamin Herrenschmidt 	hose->io_base_virt = (void __iomem *)(area->addr +
1543d5134eeSBenjamin Herrenschmidt 					      hose->io_base_phys - phys_page);
1553d5134eeSBenjamin Herrenschmidt 
156b7c670d6SRob Herring 	pr_debug("IO mapping for PHB %pOF\n", hose->dn);
1579477e455SStephen Rothwell 	pr_debug("  phys=0x%016llx, virt=0x%p (alloc=0x%p)\n",
1583d5134eeSBenjamin Herrenschmidt 		 hose->io_base_phys, hose->io_base_virt, hose->io_base_alloc);
159bcba0778SStephen Rothwell 	pr_debug("  size=0x%016llx (alloc=0x%016lx)\n",
1603d5134eeSBenjamin Herrenschmidt 		 hose->pci_io_size, size_page);
1613d5134eeSBenjamin Herrenschmidt 
1623d5134eeSBenjamin Herrenschmidt 	/* Establish the mapping */
1633d5134eeSBenjamin Herrenschmidt 	if (__ioremap_at(phys_page, area->addr, size_page,
164c766ee72SChristophe Leroy 			 pgprot_noncached(PAGE_KERNEL)) == NULL)
1653d5134eeSBenjamin Herrenschmidt 		return -ENOMEM;
1663d5134eeSBenjamin Herrenschmidt 
1673d5134eeSBenjamin Herrenschmidt 	/* Fixup hose IO resource */
16838973ba7SBjorn Helgaas 	io_virt_offset = pcibios_io_space_offset(hose);
1693d5134eeSBenjamin Herrenschmidt 	hose->io_resource.start += io_virt_offset;
1703d5134eeSBenjamin Herrenschmidt 	hose->io_resource.end += io_virt_offset;
1713d5134eeSBenjamin Herrenschmidt 
172518fdae2SJoe Perches 	pr_debug("  hose->io_resource=%pR\n", &hose->io_resource);
1737568cb4eSPaul Mackerras 
1747568cb4eSPaul Mackerras 	return 0;
1757568cb4eSPaul Mackerras }
17649a6cba4SBjorn Helgaas 
177cad5cef6SGreg Kroah-Hartman int pcibios_map_io_space(struct pci_bus *bus)
17849a6cba4SBjorn Helgaas {
17949a6cba4SBjorn Helgaas 	WARN_ON(bus == NULL);
18049a6cba4SBjorn Helgaas 
18149a6cba4SBjorn Helgaas 	/* If this not a PHB, nothing to do, page tables still exist and
18249a6cba4SBjorn Helgaas 	 * thus HPTEs will be faulted in when needed
18349a6cba4SBjorn Helgaas 	 */
18449a6cba4SBjorn Helgaas 	if (bus->self) {
18549a6cba4SBjorn Helgaas 		pr_debug("IO mapping for PCI-PCI bridge %s\n",
18649a6cba4SBjorn Helgaas 			 pci_name(bus->self));
18749a6cba4SBjorn Helgaas 		pr_debug("  virt=0x%016llx...0x%016llx\n",
18849a6cba4SBjorn Helgaas 			 bus->resource[0]->start + _IO_BASE,
18949a6cba4SBjorn Helgaas 			 bus->resource[0]->end + _IO_BASE);
19049a6cba4SBjorn Helgaas 		return 0;
19149a6cba4SBjorn Helgaas 	}
19249a6cba4SBjorn Helgaas 
19349a6cba4SBjorn Helgaas 	return pcibios_map_phb_io_space(pci_bus_to_host(bus));
19449a6cba4SBjorn Helgaas }
1953d5134eeSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_map_io_space);
1967568cb4eSPaul Mackerras 
197cad5cef6SGreg Kroah-Hartman void pcibios_setup_phb_io_space(struct pci_controller *hose)
1980ed2c722SGrant Likely {
19949a6cba4SBjorn Helgaas 	pcibios_map_phb_io_space(hose);
2000ed2c722SGrant Likely }
2010ed2c722SGrant Likely 
2027568cb4eSPaul Mackerras #define IOBASE_BRIDGE_NUMBER	0
2037568cb4eSPaul Mackerras #define IOBASE_MEMORY		1
2047568cb4eSPaul Mackerras #define IOBASE_IO		2
2057568cb4eSPaul Mackerras #define IOBASE_ISA_IO		3
2067568cb4eSPaul Mackerras #define IOBASE_ISA_MEM		4
2077568cb4eSPaul Mackerras 
2083691d614SAl Viro SYSCALL_DEFINE3(pciconfig_iobase, long, which, unsigned long, in_bus,
2093691d614SAl Viro 			  unsigned long, in_devfn)
2107568cb4eSPaul Mackerras {
2117568cb4eSPaul Mackerras 	struct pci_controller* hose;
212140ab645SMike Qiu 	struct pci_bus *tmp_bus, *bus = NULL;
2137568cb4eSPaul Mackerras 	struct device_node *hose_node;
2147568cb4eSPaul Mackerras 
2157568cb4eSPaul Mackerras 	/* Argh ! Please forgive me for that hack, but that's the
2167568cb4eSPaul Mackerras 	 * simplest way to get existing XFree to not lockup on some
2177568cb4eSPaul Mackerras 	 * G5 machines... So when something asks for bus 0 io base
2187568cb4eSPaul Mackerras 	 * (bus 0 is HT root), we return the AGP one instead.
2197568cb4eSPaul Mackerras 	 */
22071a157e8SGrant Likely 	if (in_bus == 0 && of_machine_is_compatible("MacRISC4")) {
22116124f10SPaul Mackerras 		struct device_node *agp;
22216124f10SPaul Mackerras 
22316124f10SPaul Mackerras 		agp = of_find_compatible_node(NULL, NULL, "u3-agp");
22416124f10SPaul Mackerras 		if (agp)
2257568cb4eSPaul Mackerras 			in_bus = 0xf0;
22616124f10SPaul Mackerras 		of_node_put(agp);
22716124f10SPaul Mackerras 	}
2287568cb4eSPaul Mackerras 
2297568cb4eSPaul Mackerras 	/* That syscall isn't quite compatible with PCI domains, but it's
2307568cb4eSPaul Mackerras 	 * used on pre-domains setup. We return the first match
2317568cb4eSPaul Mackerras 	 */
2327568cb4eSPaul Mackerras 
233140ab645SMike Qiu 	list_for_each_entry(tmp_bus, &pci_root_buses, node) {
234140ab645SMike Qiu 		if (in_bus >= tmp_bus->number &&
235140ab645SMike Qiu 		    in_bus <= tmp_bus->busn_res.end) {
236140ab645SMike Qiu 			bus = tmp_bus;
2377568cb4eSPaul Mackerras 			break;
238140ab645SMike Qiu 		}
2397568cb4eSPaul Mackerras 	}
240b5d937deSGrant Likely 	if (bus == NULL || bus->dev.of_node == NULL)
2417568cb4eSPaul Mackerras 		return -ENODEV;
2427568cb4eSPaul Mackerras 
243b5d937deSGrant Likely 	hose_node = bus->dev.of_node;
2447568cb4eSPaul Mackerras 	hose = PCI_DN(hose_node)->phb;
2457568cb4eSPaul Mackerras 
2467568cb4eSPaul Mackerras 	switch (which) {
2477568cb4eSPaul Mackerras 	case IOBASE_BRIDGE_NUMBER:
2487568cb4eSPaul Mackerras 		return (long)hose->first_busno;
2497568cb4eSPaul Mackerras 	case IOBASE_MEMORY:
2503fd47f06SBenjamin Herrenschmidt 		return (long)hose->mem_offset[0];
2517568cb4eSPaul Mackerras 	case IOBASE_IO:
2527568cb4eSPaul Mackerras 		return (long)hose->io_base_phys;
2537568cb4eSPaul Mackerras 	case IOBASE_ISA_IO:
2547568cb4eSPaul Mackerras 		return (long)isa_io_base;
2557568cb4eSPaul Mackerras 	case IOBASE_ISA_MEM:
2567568cb4eSPaul Mackerras 		return -EINVAL;
2577568cb4eSPaul Mackerras 	}
2587568cb4eSPaul Mackerras 
2597568cb4eSPaul Mackerras 	return -EOPNOTSUPP;
2607568cb4eSPaul Mackerras }
261357518faSAnton Blanchard 
262357518faSAnton Blanchard #ifdef CONFIG_NUMA
263357518faSAnton Blanchard int pcibus_to_node(struct pci_bus *bus)
264357518faSAnton Blanchard {
265357518faSAnton Blanchard 	struct pci_controller *phb = pci_bus_to_host(bus);
266357518faSAnton Blanchard 	return phb->node;
267357518faSAnton Blanchard }
268357518faSAnton Blanchard EXPORT_SYMBOL(pcibus_to_node);
269357518faSAnton Blanchard #endif
270