xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25516b540SKumar Gala /*
35516b540SKumar Gala  * Contains common pci routines for ALL ppc platform
4cf1d8a8aSKumar Gala  * (based on pci_32.c and pci_64.c)
5cf1d8a8aSKumar Gala  *
6cf1d8a8aSKumar Gala  * Port for PPC64 David Engebretsen, IBM Corp.
7cf1d8a8aSKumar Gala  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
8cf1d8a8aSKumar Gala  *
9cf1d8a8aSKumar Gala  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
10cf1d8a8aSKumar Gala  *   Rework, based on alpha PCI code.
11cf1d8a8aSKumar Gala  *
12cf1d8a8aSKumar Gala  * Common pmac/prep/chrp pci routines. -- Cort
135516b540SKumar Gala  */
145516b540SKumar Gala 
155516b540SKumar Gala #include <linux/kernel.h>
165516b540SKumar Gala #include <linux/pci.h>
175516b540SKumar Gala #include <linux/string.h>
185516b540SKumar Gala #include <linux/init.h>
19d92a208dSGavin Shan #include <linux/delay.h>
2066b15db6SPaul Gortmaker #include <linux/export.h>
2122ae782fSGrant Likely #include <linux/of_address.h>
2204bea68bSSebastian Andrzej Siewior #include <linux/of_pci.h>
235516b540SKumar Gala #include <linux/mm.h>
243a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
255516b540SKumar Gala #include <linux/list.h>
265516b540SKumar Gala #include <linux/syscalls.h>
275516b540SKumar Gala #include <linux/irq.h>
285516b540SKumar Gala #include <linux/vmalloc.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30c2e1d845SBrian King #include <linux/vgaarb.h>
3198fa15f3SAnshuman Khandual #include <linux/numa.h>
32a5f3d2c1SCédric Le Goater #include <linux/msi.h>
33e6f6390aSChristophe Leroy #include <linux/irqdomain.h>
345516b540SKumar Gala 
355516b540SKumar Gala #include <asm/processor.h>
365516b540SKumar Gala #include <asm/io.h>
375516b540SKumar Gala #include <asm/pci-bridge.h>
385516b540SKumar Gala #include <asm/byteorder.h>
395516b540SKumar Gala #include <asm/machdep.h>
405516b540SKumar Gala #include <asm/ppc-pci.h>
418b8da358SBenjamin Herrenschmidt #include <asm/eeh.h>
42113fe88eSChristophe Leroy #include <asm/setup.h>
435516b540SKumar Gala 
4444bda4b7SHari Vyas #include "../../../drivers/pci/pci.h"
4544bda4b7SHari Vyas 
4687c78b61SMichael Ellerman /* hose_spinlock protects accesses to the phb_bitmap. */
47a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
48c3bd517dSMilton Miller LIST_HEAD(hose_list);
49a4c9e328SKumar Gala 
5063a72284SGuilherme G. Piccoli /* For dynamic PHB numbering on get_phb_number(): max number of PHBs. */
5163a72284SGuilherme G. Piccoli #define MAX_PHBS 0x10000
5263a72284SGuilherme G. Piccoli 
5363a72284SGuilherme G. Piccoli /*
5463a72284SGuilherme G. Piccoli  * For dynamic PHB numbering: used/free PHBs tracking bitmap.
5563a72284SGuilherme G. Piccoli  * Accesses to this bitmap should be protected by hose_spinlock.
5663a72284SGuilherme G. Piccoli  */
5763a72284SGuilherme G. Piccoli static DECLARE_BITMAP(phb_bitmap, MAX_PHBS);
58a4c9e328SKumar Gala 
5925e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */
6025e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base;
619445aa1aSAl Viro EXPORT_SYMBOL(isa_mem_base);
6225e81f92SBenjamin Herrenschmidt 
63a4c9e328SKumar Gala 
6468005b67SChristoph Hellwig static const struct dma_map_ops *pci_dma_ops;
654fc665b8SBecky Bruce 
set_pci_dma_ops(const struct dma_map_ops * dma_ops)66d276960dSNick Child void __init set_pci_dma_ops(const struct dma_map_ops *dma_ops)
674fc665b8SBecky Bruce {
684fc665b8SBecky Bruce 	pci_dma_ops = dma_ops;
694fc665b8SBecky Bruce }
704fc665b8SBecky Bruce 
get_phb_number(struct device_node * dn)7163a72284SGuilherme G. Piccoli static int get_phb_number(struct device_node *dn)
7263a72284SGuilherme G. Piccoli {
7363a72284SGuilherme G. Piccoli 	int ret, phb_id = -1;
7463a72284SGuilherme G. Piccoli 	u64 prop;
7563a72284SGuilherme G. Piccoli 
7663a72284SGuilherme G. Piccoli 	/*
7763a72284SGuilherme G. Piccoli 	 * Try fixed PHB numbering first, by checking archs and reading
780fe1e96fSPali Rohár 	 * the respective device-tree properties. Firstly, try reading
790fe1e96fSPali Rohár 	 * standard "linux,pci-domain", then try reading "ibm,opal-phbid"
800fe1e96fSPali Rohár 	 * (only present in powernv OPAL environment), then try device-tree
810fe1e96fSPali Rohár 	 * alias and as the last try to use lower bits of "reg" property.
8263a72284SGuilherme G. Piccoli 	 */
830fe1e96fSPali Rohár 	ret = of_get_pci_domain_nr(dn);
840fe1e96fSPali Rohár 	if (ret >= 0) {
850fe1e96fSPali Rohár 		prop = ret;
860fe1e96fSPali Rohár 		ret = 0;
870fe1e96fSPali Rohár 	}
880fe1e96fSPali Rohár 	if (ret)
8963a72284SGuilherme G. Piccoli 		ret = of_property_read_u64(dn, "ibm,opal-phbid", &prop);
90f4b39e88SMichael Ellerman 
91f4b39e88SMichael Ellerman 	if (ret) {
920fe1e96fSPali Rohár 		ret = of_alias_get_id(dn, "pci");
930fe1e96fSPali Rohár 		if (ret >= 0) {
940fe1e96fSPali Rohár 			prop = ret;
950fe1e96fSPali Rohár 			ret = 0;
960fe1e96fSPali Rohár 		}
97f4b39e88SMichael Ellerman 	}
9861e8a0d5SMichael Ellerman 	if (ret) {
990fe1e96fSPali Rohár 		u32 prop_32;
10061e8a0d5SMichael Ellerman 		ret = of_property_read_u32_index(dn, "reg", 1, &prop_32);
10161e8a0d5SMichael Ellerman 		prop = prop_32;
10261e8a0d5SMichael Ellerman 	}
10363a72284SGuilherme G. Piccoli 
10463a72284SGuilherme G. Piccoli 	if (!ret)
10563a72284SGuilherme G. Piccoli 		phb_id = (int)(prop & (MAX_PHBS - 1));
10663a72284SGuilherme G. Piccoli 
1078d48562aSMichael Ellerman 	spin_lock(&hose_spinlock);
1088d48562aSMichael Ellerman 
10963a72284SGuilherme G. Piccoli 	/* We need to be sure to not use the same PHB number twice. */
11063a72284SGuilherme G. Piccoli 	if ((phb_id >= 0) && !test_and_set_bit(phb_id, phb_bitmap))
1118d48562aSMichael Ellerman 		goto out_unlock;
11263a72284SGuilherme G. Piccoli 
1130fe1e96fSPali Rohár 	/* If everything fails then fallback to dynamic PHB numbering. */
11463a72284SGuilherme G. Piccoli 	phb_id = find_first_zero_bit(phb_bitmap, MAX_PHBS);
11563a72284SGuilherme G. Piccoli 	BUG_ON(phb_id >= MAX_PHBS);
11663a72284SGuilherme G. Piccoli 	set_bit(phb_id, phb_bitmap);
11763a72284SGuilherme G. Piccoli 
1188d48562aSMichael Ellerman out_unlock:
1198d48562aSMichael Ellerman 	spin_unlock(&hose_spinlock);
1208d48562aSMichael Ellerman 
12163a72284SGuilherme G. Piccoli 	return phb_id;
12263a72284SGuilherme G. Piccoli }
12363a72284SGuilherme G. Piccoli 
pcibios_alloc_controller(struct device_node * dev)1242d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
125a4c9e328SKumar Gala {
126a4c9e328SKumar Gala 	struct pci_controller *phb;
127a4c9e328SKumar Gala 
128*fabdb27dSMichael Ellerman 	phb = kzalloc(sizeof(struct pci_controller), GFP_KERNEL);
129a4c9e328SKumar Gala 	if (phb == NULL)
130a4c9e328SKumar Gala 		return NULL;
1318d48562aSMichael Ellerman 
13263a72284SGuilherme G. Piccoli 	phb->global_number = get_phb_number(dev);
1338d48562aSMichael Ellerman 
1348d48562aSMichael Ellerman 	spin_lock(&hose_spinlock);
135e60516e3SStephen Rothwell 	list_add_tail(&phb->list_node, &hose_list);
136e60516e3SStephen Rothwell 	spin_unlock(&hose_spinlock);
1378d48562aSMichael Ellerman 
138ce63c44bSLiang He 	phb->dn = of_node_get(dev);
139f691fa10SMichael Ellerman 	phb->is_dynamic = slab_is_available();
140a4c9e328SKumar Gala #ifdef CONFIG_PPC64
141a4c9e328SKumar Gala 	if (dev) {
142a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
143a4c9e328SKumar Gala 
144a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
14598fa15f3SAnshuman Khandual 			nid = NUMA_NO_NODE;
146a4c9e328SKumar Gala 
147a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
148a4c9e328SKumar Gala 	}
149a4c9e328SKumar Gala #endif
150a4c9e328SKumar Gala 	return phb;
151a4c9e328SKumar Gala }
1525b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_alloc_controller);
153a4c9e328SKumar Gala 
pcibios_free_controller(struct pci_controller * phb)154a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
155a4c9e328SKumar Gala {
156a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
15763a72284SGuilherme G. Piccoli 
15863a72284SGuilherme G. Piccoli 	/* Clear bit of phb_bitmap to allow reuse of this PHB number. */
15963a72284SGuilherme G. Piccoli 	if (phb->global_number < MAX_PHBS)
16063a72284SGuilherme G. Piccoli 		clear_bit(phb->global_number, phb_bitmap);
161ce63c44bSLiang He 	of_node_put(phb->dn);
162a4c9e328SKumar Gala 	list_del(&phb->list_node);
163a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
164a4c9e328SKumar Gala 
165a4c9e328SKumar Gala 	if (phb->is_dynamic)
166a4c9e328SKumar Gala 		kfree(phb);
167a4c9e328SKumar Gala }
1686b8b252fSAndrew Donnellan EXPORT_SYMBOL_GPL(pcibios_free_controller);
169a4c9e328SKumar Gala 
1704c2245bbSGavin Shan /*
1712dd9c11bSMauricio Faria de Oliveira  * This function is used to call pcibios_free_controller()
1722dd9c11bSMauricio Faria de Oliveira  * in a deferred manner: a callback from the PCI subsystem.
1732dd9c11bSMauricio Faria de Oliveira  *
1742dd9c11bSMauricio Faria de Oliveira  * _*DO NOT*_ call pcibios_free_controller() explicitly if
1752dd9c11bSMauricio Faria de Oliveira  * this is used (or it may access an invalid *phb pointer).
1762dd9c11bSMauricio Faria de Oliveira  *
1772dd9c11bSMauricio Faria de Oliveira  * The callback occurs when all references to the root bus
1782dd9c11bSMauricio Faria de Oliveira  * are dropped (e.g., child buses/devices and their users).
1792dd9c11bSMauricio Faria de Oliveira  *
1802dd9c11bSMauricio Faria de Oliveira  * It's called as .release_fn() of 'struct pci_host_bridge'
1812dd9c11bSMauricio Faria de Oliveira  * which is associated with the 'struct pci_controller.bus'
1822dd9c11bSMauricio Faria de Oliveira  * (root bus) - it expects .release_data to hold a pointer
1832dd9c11bSMauricio Faria de Oliveira  * to 'struct pci_controller'.
1842dd9c11bSMauricio Faria de Oliveira  *
1852dd9c11bSMauricio Faria de Oliveira  * In order to use it, register .release_fn()/release_data
1862dd9c11bSMauricio Faria de Oliveira  * like this:
1872dd9c11bSMauricio Faria de Oliveira  *
1882dd9c11bSMauricio Faria de Oliveira  * pci_set_host_bridge_release(bridge,
1892dd9c11bSMauricio Faria de Oliveira  *                             pcibios_free_controller_deferred
1902dd9c11bSMauricio Faria de Oliveira  *                             (void *) phb);
1912dd9c11bSMauricio Faria de Oliveira  *
1922dd9c11bSMauricio Faria de Oliveira  * e.g. in the pcibios_root_bridge_prepare() callback from
1932dd9c11bSMauricio Faria de Oliveira  * pci_create_root_bus().
1942dd9c11bSMauricio Faria de Oliveira  */
pcibios_free_controller_deferred(struct pci_host_bridge * bridge)1952dd9c11bSMauricio Faria de Oliveira void pcibios_free_controller_deferred(struct pci_host_bridge *bridge)
1962dd9c11bSMauricio Faria de Oliveira {
1972dd9c11bSMauricio Faria de Oliveira 	struct pci_controller *phb = (struct pci_controller *)
1982dd9c11bSMauricio Faria de Oliveira 					 bridge->release_data;
1992dd9c11bSMauricio Faria de Oliveira 
2002dd9c11bSMauricio Faria de Oliveira 	pr_debug("domain %d, dynamic %d\n", phb->global_number, phb->is_dynamic);
2012dd9c11bSMauricio Faria de Oliveira 
2022dd9c11bSMauricio Faria de Oliveira 	pcibios_free_controller(phb);
2032dd9c11bSMauricio Faria de Oliveira }
2042dd9c11bSMauricio Faria de Oliveira EXPORT_SYMBOL_GPL(pcibios_free_controller_deferred);
2052dd9c11bSMauricio Faria de Oliveira 
2062dd9c11bSMauricio Faria de Oliveira /*
2074c2245bbSGavin Shan  * The function is used to return the minimal alignment
2084c2245bbSGavin Shan  * for memory or I/O windows of the associated P2P bridge.
2094c2245bbSGavin Shan  * By default, 4KiB alignment for I/O windows and 1MiB for
2104c2245bbSGavin Shan  * memory windows.
2114c2245bbSGavin Shan  */
pcibios_window_alignment(struct pci_bus * bus,unsigned long type)2124c2245bbSGavin Shan resource_size_t pcibios_window_alignment(struct pci_bus *bus,
2134c2245bbSGavin Shan 					 unsigned long type)
2144c2245bbSGavin Shan {
215467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(bus);
216467efc2eSDaniel Axtens 
217467efc2eSDaniel Axtens 	if (phb->controller_ops.window_alignment)
218467efc2eSDaniel Axtens 		return phb->controller_ops.window_alignment(bus, type);
219467efc2eSDaniel Axtens 
220467efc2eSDaniel Axtens 	/*
221467efc2eSDaniel Axtens 	 * PCI core will figure out the default
222467efc2eSDaniel Axtens 	 * alignment: 4KiB for I/O and 1MiB for
223467efc2eSDaniel Axtens 	 * memory window.
224467efc2eSDaniel Axtens 	 */
225467efc2eSDaniel Axtens 	return 1;
2264c2245bbSGavin Shan }
2274c2245bbSGavin Shan 
pcibios_setup_bridge(struct pci_bus * bus,unsigned long type)228c5fcb29aSGavin Shan void pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
229c5fcb29aSGavin Shan {
230c5fcb29aSGavin Shan 	struct pci_controller *hose = pci_bus_to_host(bus);
231c5fcb29aSGavin Shan 
232c5fcb29aSGavin Shan 	if (hose->controller_ops.setup_bridge)
233c5fcb29aSGavin Shan 		hose->controller_ops.setup_bridge(bus, type);
234c5fcb29aSGavin Shan }
235c5fcb29aSGavin Shan 
pcibios_reset_secondary_bus(struct pci_dev * dev)236d92a208dSGavin Shan void pcibios_reset_secondary_bus(struct pci_dev *dev)
237d92a208dSGavin Shan {
238467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
239467efc2eSDaniel Axtens 
240467efc2eSDaniel Axtens 	if (phb->controller_ops.reset_secondary_bus) {
241467efc2eSDaniel Axtens 		phb->controller_ops.reset_secondary_bus(dev);
242467efc2eSDaniel Axtens 		return;
243467efc2eSDaniel Axtens 	}
244467efc2eSDaniel Axtens 
245467efc2eSDaniel Axtens 	pci_reset_secondary_bus(dev);
246d92a208dSGavin Shan }
247d92a208dSGavin Shan 
pcibios_default_alignment(void)24838274637SYongji Xie resource_size_t pcibios_default_alignment(void)
24938274637SYongji Xie {
25038274637SYongji Xie 	if (ppc_md.pcibios_default_alignment)
25138274637SYongji Xie 		return ppc_md.pcibios_default_alignment();
25238274637SYongji Xie 
25338274637SYongji Xie 	return 0;
25438274637SYongji Xie }
25538274637SYongji Xie 
2565350ab3fSWei Yang #ifdef CONFIG_PCI_IOV
pcibios_iov_resource_alignment(struct pci_dev * pdev,int resno)2575350ab3fSWei Yang resource_size_t pcibios_iov_resource_alignment(struct pci_dev *pdev, int resno)
2585350ab3fSWei Yang {
2595350ab3fSWei Yang 	if (ppc_md.pcibios_iov_resource_alignment)
2605350ab3fSWei Yang 		return ppc_md.pcibios_iov_resource_alignment(pdev, resno);
2615350ab3fSWei Yang 
2625350ab3fSWei Yang 	return pci_iov_resource_size(pdev, resno);
2635350ab3fSWei Yang }
264988fc3baSBryant G. Ly 
pcibios_sriov_enable(struct pci_dev * pdev,u16 num_vfs)265988fc3baSBryant G. Ly int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
266988fc3baSBryant G. Ly {
267988fc3baSBryant G. Ly 	if (ppc_md.pcibios_sriov_enable)
268988fc3baSBryant G. Ly 		return ppc_md.pcibios_sriov_enable(pdev, num_vfs);
269988fc3baSBryant G. Ly 
270988fc3baSBryant G. Ly 	return 0;
271988fc3baSBryant G. Ly }
272988fc3baSBryant G. Ly 
pcibios_sriov_disable(struct pci_dev * pdev)273988fc3baSBryant G. Ly int pcibios_sriov_disable(struct pci_dev *pdev)
274988fc3baSBryant G. Ly {
275988fc3baSBryant G. Ly 	if (ppc_md.pcibios_sriov_disable)
276988fc3baSBryant G. Ly 		return ppc_md.pcibios_sriov_disable(pdev);
277988fc3baSBryant G. Ly 
278988fc3baSBryant G. Ly 	return 0;
279988fc3baSBryant G. Ly }
280988fc3baSBryant G. Ly 
2815350ab3fSWei Yang #endif /* CONFIG_PCI_IOV */
2825350ab3fSWei Yang 
pcibios_io_size(const struct pci_controller * hose)283c3bd517dSMilton Miller static resource_size_t pcibios_io_size(const struct pci_controller *hose)
284c3bd517dSMilton Miller {
285c3bd517dSMilton Miller #ifdef CONFIG_PPC64
286c3bd517dSMilton Miller 	return hose->pci_io_size;
287c3bd517dSMilton Miller #else
28828f65c11SJoe Perches 	return resource_size(&hose->io_resource);
289c3bd517dSMilton Miller #endif
290c3bd517dSMilton Miller }
291c3bd517dSMilton Miller 
pcibios_vaddr_is_ioport(void __iomem * address)2926dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
2936dfbde20SBenjamin Herrenschmidt {
2946dfbde20SBenjamin Herrenschmidt 	int ret = 0;
2956dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
296c3bd517dSMilton Miller 	resource_size_t size;
2976dfbde20SBenjamin Herrenschmidt 
2986dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
2996dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
300c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
3016dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
3026dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
3036dfbde20SBenjamin Herrenschmidt 			ret = 1;
3046dfbde20SBenjamin Herrenschmidt 			break;
3056dfbde20SBenjamin Herrenschmidt 		}
3066dfbde20SBenjamin Herrenschmidt 	}
3076dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
3086dfbde20SBenjamin Herrenschmidt 	return ret;
3096dfbde20SBenjamin Herrenschmidt }
3106dfbde20SBenjamin Herrenschmidt 
pci_address_to_pio(phys_addr_t address)311c3bd517dSMilton Miller unsigned long pci_address_to_pio(phys_addr_t address)
312c3bd517dSMilton Miller {
313c3bd517dSMilton Miller 	struct pci_controller *hose;
314c3bd517dSMilton Miller 	resource_size_t size;
315c3bd517dSMilton Miller 	unsigned long ret = ~0;
316c3bd517dSMilton Miller 
317c3bd517dSMilton Miller 	spin_lock(&hose_spinlock);
318c3bd517dSMilton Miller 	list_for_each_entry(hose, &hose_list, list_node) {
319c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
320c3bd517dSMilton Miller 		if (address >= hose->io_base_phys &&
321c3bd517dSMilton Miller 		    address < (hose->io_base_phys + size)) {
322c3bd517dSMilton Miller 			unsigned long base =
323c3bd517dSMilton Miller 				(unsigned long)hose->io_base_virt - _IO_BASE;
324c3bd517dSMilton Miller 			ret = base + (address - hose->io_base_phys);
325c3bd517dSMilton Miller 			break;
326c3bd517dSMilton Miller 		}
327c3bd517dSMilton Miller 	}
328c3bd517dSMilton Miller 	spin_unlock(&hose_spinlock);
329c3bd517dSMilton Miller 
330c3bd517dSMilton Miller 	return ret;
331c3bd517dSMilton Miller }
332c3bd517dSMilton Miller EXPORT_SYMBOL_GPL(pci_address_to_pio);
333c3bd517dSMilton Miller 
3345516b540SKumar Gala /*
3355516b540SKumar Gala  * Return the domain number for this bus.
3365516b540SKumar Gala  */
pci_domain_nr(struct pci_bus * bus)3375516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
3385516b540SKumar Gala {
3395516b540SKumar Gala 	struct pci_controller *hose = pci_bus_to_host(bus);
3405516b540SKumar Gala 
3415516b540SKumar Gala 	return hose->global_number;
3425516b540SKumar Gala }
3435516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
34458083dadSKumar Gala 
345a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
346a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
347a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
348a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
349a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
350a4c9e328SKumar Gala  * config cycles.
351a4c9e328SKumar Gala  */
pci_find_hose_for_OF_device(struct device_node * node)352a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
353a4c9e328SKumar Gala {
354a4c9e328SKumar Gala 	while(node) {
355a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
356a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
35744ef3390SStephen Rothwell 			if (hose->dn == node)
358a4c9e328SKumar Gala 				return hose;
359a4c9e328SKumar Gala 		node = node->parent;
360a4c9e328SKumar Gala 	}
361a4c9e328SKumar Gala 	return NULL;
362a4c9e328SKumar Gala }
363a4c9e328SKumar Gala 
pci_find_controller_for_domain(int domain_nr)36467060cb1SOliver O'Halloran struct pci_controller *pci_find_controller_for_domain(int domain_nr)
36567060cb1SOliver O'Halloran {
36667060cb1SOliver O'Halloran 	struct pci_controller *hose;
36767060cb1SOliver O'Halloran 
36867060cb1SOliver O'Halloran 	list_for_each_entry(hose, &hose_list, list_node)
36967060cb1SOliver O'Halloran 		if (hose->global_number == domain_nr)
37067060cb1SOliver O'Halloran 			return hose;
37167060cb1SOliver O'Halloran 
37267060cb1SOliver O'Halloran 	return NULL;
37367060cb1SOliver O'Halloran }
37467060cb1SOliver O'Halloran 
375450be496SOliver O'Halloran struct pci_intx_virq {
376450be496SOliver O'Halloran 	int virq;
377450be496SOliver O'Halloran 	struct kref kref;
378450be496SOliver O'Halloran 	struct list_head list_node;
379450be496SOliver O'Halloran };
380450be496SOliver O'Halloran 
381450be496SOliver O'Halloran static LIST_HEAD(intx_list);
382450be496SOliver O'Halloran static DEFINE_MUTEX(intx_mutex);
383450be496SOliver O'Halloran 
ppc_pci_intx_release(struct kref * kref)384450be496SOliver O'Halloran static void ppc_pci_intx_release(struct kref *kref)
385450be496SOliver O'Halloran {
386450be496SOliver O'Halloran 	struct pci_intx_virq *vi = container_of(kref, struct pci_intx_virq, kref);
387450be496SOliver O'Halloran 
388450be496SOliver O'Halloran 	list_del(&vi->list_node);
389450be496SOliver O'Halloran 	irq_dispose_mapping(vi->virq);
390450be496SOliver O'Halloran 	kfree(vi);
391450be496SOliver O'Halloran }
392450be496SOliver O'Halloran 
ppc_pci_unmap_irq_line(struct notifier_block * nb,unsigned long action,void * data)393450be496SOliver O'Halloran static int ppc_pci_unmap_irq_line(struct notifier_block *nb,
394450be496SOliver O'Halloran 			       unsigned long action, void *data)
395450be496SOliver O'Halloran {
396450be496SOliver O'Halloran 	struct pci_dev *pdev = to_pci_dev(data);
397450be496SOliver O'Halloran 
398450be496SOliver O'Halloran 	if (action == BUS_NOTIFY_DEL_DEVICE) {
399450be496SOliver O'Halloran 		struct pci_intx_virq *vi;
400450be496SOliver O'Halloran 
401450be496SOliver O'Halloran 		mutex_lock(&intx_mutex);
402450be496SOliver O'Halloran 		list_for_each_entry(vi, &intx_list, list_node) {
403450be496SOliver O'Halloran 			if (vi->virq == pdev->irq) {
404450be496SOliver O'Halloran 				kref_put(&vi->kref, ppc_pci_intx_release);
405450be496SOliver O'Halloran 				break;
406450be496SOliver O'Halloran 			}
407450be496SOliver O'Halloran 		}
408450be496SOliver O'Halloran 		mutex_unlock(&intx_mutex);
409450be496SOliver O'Halloran 	}
410450be496SOliver O'Halloran 
411450be496SOliver O'Halloran 	return NOTIFY_DONE;
412450be496SOliver O'Halloran }
413450be496SOliver O'Halloran 
414450be496SOliver O'Halloran static struct notifier_block ppc_pci_unmap_irq_notifier = {
415450be496SOliver O'Halloran 	.notifier_call = ppc_pci_unmap_irq_line,
416450be496SOliver O'Halloran };
417450be496SOliver O'Halloran 
ppc_pci_register_irq_notifier(void)418450be496SOliver O'Halloran static int ppc_pci_register_irq_notifier(void)
419450be496SOliver O'Halloran {
420450be496SOliver O'Halloran 	return bus_register_notifier(&pci_bus_type, &ppc_pci_unmap_irq_notifier);
421450be496SOliver O'Halloran }
422450be496SOliver O'Halloran arch_initcall(ppc_pci_register_irq_notifier);
423450be496SOliver O'Halloran 
42458083dadSKumar Gala /*
42558083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
42658083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
42758083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
42858083dadSKumar Gala  */
pci_read_irq_line(struct pci_dev * pci_dev)4294666ca2aSBenjamin Herrenschmidt static int pci_read_irq_line(struct pci_dev *pci_dev)
43058083dadSKumar Gala {
431c591c2e3SAlexey Kardashevskiy 	int virq;
432450be496SOliver O'Halloran 	struct pci_intx_virq *vi, *vitmp;
433450be496SOliver O'Halloran 
434450be496SOliver O'Halloran 	/* Preallocate vi as rewind is complex if this fails after mapping */
435450be496SOliver O'Halloran 	vi = kzalloc(sizeof(struct pci_intx_virq), GFP_KERNEL);
436450be496SOliver O'Halloran 	if (!vi)
437450be496SOliver O'Halloran 		return -1;
43858083dadSKumar Gala 
439b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
44058083dadSKumar Gala 
44158083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
442c591c2e3SAlexey Kardashevskiy 	virq = of_irq_parse_and_map_pci(pci_dev, 0, 0);
443c591c2e3SAlexey Kardashevskiy 	if (virq <= 0) {
44458083dadSKumar Gala 		u8 line, pin;
44558083dadSKumar Gala 
44658083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
44758083dadSKumar Gala 		 * space and map that through the default controller. We
44858083dadSKumar Gala 		 * also set the type to level low since that's what PCI
44958083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
45058083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
45158083dadSKumar Gala 		 * function.
45258083dadSKumar Gala 		 */
45358083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
454450be496SOliver O'Halloran 			goto error_exit;
45558083dadSKumar Gala 		if (pin == 0)
456450be496SOliver O'Halloran 			goto error_exit;
45758083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
45854a24cbbSBenjamin Herrenschmidt 		    line == 0xff || line == 0) {
459450be496SOliver O'Halloran 			goto error_exit;
46058083dadSKumar Gala 		}
461b0494bc8SBenjamin Herrenschmidt 		pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
46254a24cbbSBenjamin Herrenschmidt 			 line, pin);
46358083dadSKumar Gala 
46458083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
465ef24ba70SMichael Ellerman 		if (virq)
466ec775d0eSThomas Gleixner 			irq_set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
46758083dadSKumar Gala 	}
468ef24ba70SMichael Ellerman 
469ef24ba70SMichael Ellerman 	if (!virq) {
470b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Failed to map !\n");
471450be496SOliver O'Halloran 		goto error_exit;
47258083dadSKumar Gala 	}
47358083dadSKumar Gala 
474b0494bc8SBenjamin Herrenschmidt 	pr_debug(" Mapped to linux irq %d\n", virq);
47558083dadSKumar Gala 
47658083dadSKumar Gala 	pci_dev->irq = virq;
47758083dadSKumar Gala 
478450be496SOliver O'Halloran 	mutex_lock(&intx_mutex);
479450be496SOliver O'Halloran 	list_for_each_entry(vitmp, &intx_list, list_node) {
480450be496SOliver O'Halloran 		if (vitmp->virq == virq) {
481450be496SOliver O'Halloran 			kref_get(&vitmp->kref);
482450be496SOliver O'Halloran 			kfree(vi);
483450be496SOliver O'Halloran 			vi = NULL;
484450be496SOliver O'Halloran 			break;
485450be496SOliver O'Halloran 		}
486450be496SOliver O'Halloran 	}
487450be496SOliver O'Halloran 	if (vi) {
488450be496SOliver O'Halloran 		vi->virq = virq;
489450be496SOliver O'Halloran 		kref_init(&vi->kref);
490450be496SOliver O'Halloran 		list_add_tail(&vi->list_node, &intx_list);
491450be496SOliver O'Halloran 	}
492450be496SOliver O'Halloran 	mutex_unlock(&intx_mutex);
493450be496SOliver O'Halloran 
49458083dadSKumar Gala 	return 0;
495450be496SOliver O'Halloran error_exit:
496450be496SOliver O'Halloran 	kfree(vi);
497450be496SOliver O'Halloran 	return -1;
49858083dadSKumar Gala }
49958083dadSKumar Gala 
50058083dadSKumar Gala /*
50128f8f183SDavid Woodhouse  * Platform support for /proc/bus/pci/X/Y mmap()s.
50258083dadSKumar Gala  *  -- paulus.
50358083dadSKumar Gala  */
pci_iobar_pfn(struct pci_dev * pdev,int bar,struct vm_area_struct * vma)50428f8f183SDavid Woodhouse int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
50558083dadSKumar Gala {
50628f8f183SDavid Woodhouse 	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
50728f8f183SDavid Woodhouse 	resource_size_t ioaddr = pci_resource_start(pdev, bar);
50858083dadSKumar Gala 
50928f8f183SDavid Woodhouse 	if (!hose)
51028f8f183SDavid Woodhouse 		return -EINVAL;
51158083dadSKumar Gala 
51228f8f183SDavid Woodhouse 	/* Convert to an offset within this PCI controller */
51328f8f183SDavid Woodhouse 	ioaddr -= (unsigned long)hose->io_base_virt - _IO_BASE;
51458083dadSKumar Gala 
51528f8f183SDavid Woodhouse 	vma->vm_pgoff += (ioaddr + hose->io_base_phys) >> PAGE_SHIFT;
51628f8f183SDavid Woodhouse 	return 0;
51758083dadSKumar Gala }
51858083dadSKumar Gala 
51958083dadSKumar Gala /*
52058083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
52158083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
52258083dadSKumar Gala  * above routine
52358083dadSKumar Gala  */
pci_phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t prot)52458083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
52558083dadSKumar Gala 				  unsigned long pfn,
52658083dadSKumar Gala 				  unsigned long size,
52764b3d0e8SBenjamin Herrenschmidt 				  pgprot_t prot)
52858083dadSKumar Gala {
52958083dadSKumar Gala 	struct pci_dev *pdev = NULL;
53058083dadSKumar Gala 	struct resource *found = NULL;
5317c12d906SBenjamin Herrenschmidt 	resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
53258083dadSKumar Gala 	int i;
53358083dadSKumar Gala 
53458083dadSKumar Gala 	if (page_is_ram(pfn))
53564b3d0e8SBenjamin Herrenschmidt 		return prot;
53658083dadSKumar Gala 
53764b3d0e8SBenjamin Herrenschmidt 	prot = pgprot_noncached(prot);
53858083dadSKumar Gala 	for_each_pci_dev(pdev) {
53958083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
54058083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
54158083dadSKumar Gala 			int flags = rp->flags;
54258083dadSKumar Gala 
54358083dadSKumar Gala 			/* Active and same type? */
54458083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
54558083dadSKumar Gala 				continue;
54658083dadSKumar Gala 			/* In the range of this resource? */
54758083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
54858083dadSKumar Gala 			    offset > rp->end)
54958083dadSKumar Gala 				continue;
55058083dadSKumar Gala 			found = rp;
55158083dadSKumar Gala 			break;
55258083dadSKumar Gala 		}
55358083dadSKumar Gala 		if (found)
55458083dadSKumar Gala 			break;
55558083dadSKumar Gala 	}
55658083dadSKumar Gala 	if (found) {
55758083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
55864b3d0e8SBenjamin Herrenschmidt 			prot = pgprot_noncached_wc(prot);
55958083dadSKumar Gala 		pci_dev_put(pdev);
56058083dadSKumar Gala 	}
56158083dadSKumar Gala 
562b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
56364b3d0e8SBenjamin Herrenschmidt 		 (unsigned long long)offset, pgprot_val(prot));
56458083dadSKumar Gala 
56564b3d0e8SBenjamin Herrenschmidt 	return prot;
56658083dadSKumar Gala }
56758083dadSKumar Gala 
568e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO read access on a bus */
pci_legacy_read(struct pci_bus * bus,loff_t port,u32 * val,size_t size)569e9f82cb7SBenjamin Herrenschmidt int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
570e9f82cb7SBenjamin Herrenschmidt {
571e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
572e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
573e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
574e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
575e9f82cb7SBenjamin Herrenschmidt 
576e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
577e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
578e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
579e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
580e9f82cb7SBenjamin Herrenschmidt 	 */
581e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
582e9f82cb7SBenjamin Herrenschmidt 	offset += port;
583e9f82cb7SBenjamin Herrenschmidt 
584e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
585e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
586e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
587e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
588e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
589e9f82cb7SBenjamin Herrenschmidt 
590e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
591e9f82cb7SBenjamin Herrenschmidt 	case 1:
592e9f82cb7SBenjamin Herrenschmidt 		*((u8 *)val) = in_8(addr);
593e9f82cb7SBenjamin Herrenschmidt 		return 1;
594e9f82cb7SBenjamin Herrenschmidt 	case 2:
595e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
596e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
597e9f82cb7SBenjamin Herrenschmidt 		*((u16 *)val) = in_le16(addr);
598e9f82cb7SBenjamin Herrenschmidt 		return 2;
599e9f82cb7SBenjamin Herrenschmidt 	case 4:
600e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
601e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
602e9f82cb7SBenjamin Herrenschmidt 		*((u32 *)val) = in_le32(addr);
603e9f82cb7SBenjamin Herrenschmidt 		return 4;
604e9f82cb7SBenjamin Herrenschmidt 	}
605e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
606e9f82cb7SBenjamin Herrenschmidt }
607e9f82cb7SBenjamin Herrenschmidt 
608e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO write access on a bus */
pci_legacy_write(struct pci_bus * bus,loff_t port,u32 val,size_t size)609e9f82cb7SBenjamin Herrenschmidt int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
610e9f82cb7SBenjamin Herrenschmidt {
611e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
612e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
613e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
614e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
615e9f82cb7SBenjamin Herrenschmidt 
616e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
617e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
618e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
619e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
620e9f82cb7SBenjamin Herrenschmidt 	 */
621e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
622e9f82cb7SBenjamin Herrenschmidt 	offset += port;
623e9f82cb7SBenjamin Herrenschmidt 
624e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
625e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
626e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
627e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
628e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
629e9f82cb7SBenjamin Herrenschmidt 
630e9f82cb7SBenjamin Herrenschmidt 	/* WARNING: The generic code is idiotic. It gets passed a pointer
631e9f82cb7SBenjamin Herrenschmidt 	 * to what can be a 1, 2 or 4 byte quantity and always reads that
632e9f82cb7SBenjamin Herrenschmidt 	 * as a u32, which means that we have to correct the location of
633e9f82cb7SBenjamin Herrenschmidt 	 * the data read within those 32 bits for size 1 and 2
634e9f82cb7SBenjamin Herrenschmidt 	 */
635e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
636e9f82cb7SBenjamin Herrenschmidt 	case 1:
637e9f82cb7SBenjamin Herrenschmidt 		out_8(addr, val >> 24);
638e9f82cb7SBenjamin Herrenschmidt 		return 1;
639e9f82cb7SBenjamin Herrenschmidt 	case 2:
640e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
641e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
642e9f82cb7SBenjamin Herrenschmidt 		out_le16(addr, val >> 16);
643e9f82cb7SBenjamin Herrenschmidt 		return 2;
644e9f82cb7SBenjamin Herrenschmidt 	case 4:
645e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
646e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
647e9f82cb7SBenjamin Herrenschmidt 		out_le32(addr, val);
648e9f82cb7SBenjamin Herrenschmidt 		return 4;
649e9f82cb7SBenjamin Herrenschmidt 	}
650e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
651e9f82cb7SBenjamin Herrenschmidt }
652e9f82cb7SBenjamin Herrenschmidt 
653e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO or memory mmap access on a bus */
pci_mmap_legacy_page_range(struct pci_bus * bus,struct vm_area_struct * vma,enum pci_mmap_state mmap_state)654e9f82cb7SBenjamin Herrenschmidt int pci_mmap_legacy_page_range(struct pci_bus *bus,
655e9f82cb7SBenjamin Herrenschmidt 			       struct vm_area_struct *vma,
656e9f82cb7SBenjamin Herrenschmidt 			       enum pci_mmap_state mmap_state)
657e9f82cb7SBenjamin Herrenschmidt {
658e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
659e9f82cb7SBenjamin Herrenschmidt 	resource_size_t offset =
660e9f82cb7SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
661e9f82cb7SBenjamin Herrenschmidt 	resource_size_t size = vma->vm_end - vma->vm_start;
662e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp;
663e9f82cb7SBenjamin Herrenschmidt 
664e9f82cb7SBenjamin Herrenschmidt 	pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
665e9f82cb7SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number,
666e9f82cb7SBenjamin Herrenschmidt 		 mmap_state == pci_mmap_mem ? "MEM" : "IO",
667e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)offset,
668e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)(offset + size - 1));
669e9f82cb7SBenjamin Herrenschmidt 
670e9f82cb7SBenjamin Herrenschmidt 	if (mmap_state == pci_mmap_mem) {
6715b11abfdSBenjamin Herrenschmidt 		/* Hack alert !
6725b11abfdSBenjamin Herrenschmidt 		 *
6735b11abfdSBenjamin Herrenschmidt 		 * Because X is lame and can fail starting if it gets an error trying
6745b11abfdSBenjamin Herrenschmidt 		 * to mmap legacy_mem (instead of just moving on without legacy memory
6755b11abfdSBenjamin Herrenschmidt 		 * access) we fake it here by giving it anonymous memory, effectively
6765b11abfdSBenjamin Herrenschmidt 		 * behaving just like /dev/zero
6775b11abfdSBenjamin Herrenschmidt 		 */
6785b11abfdSBenjamin Herrenschmidt 		if ((offset + size) > hose->isa_mem_size) {
6795b11abfdSBenjamin Herrenschmidt 			printk(KERN_DEBUG
6805b11abfdSBenjamin Herrenschmidt 			       "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
6815b11abfdSBenjamin Herrenschmidt 			       current->comm, current->pid, pci_domain_nr(bus), bus->number);
6825b11abfdSBenjamin Herrenschmidt 			if (vma->vm_flags & VM_SHARED)
6835b11abfdSBenjamin Herrenschmidt 				return shmem_zero_setup(vma);
6845b11abfdSBenjamin Herrenschmidt 			return 0;
6855b11abfdSBenjamin Herrenschmidt 		}
686e9f82cb7SBenjamin Herrenschmidt 		offset += hose->isa_mem_phys;
687e9f82cb7SBenjamin Herrenschmidt 	} else {
688e9f82cb7SBenjamin Herrenschmidt 		unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
689e9f82cb7SBenjamin Herrenschmidt 		unsigned long roffset = offset + io_offset;
690e9f82cb7SBenjamin Herrenschmidt 		rp = &hose->io_resource;
691e9f82cb7SBenjamin Herrenschmidt 		if (!(rp->flags & IORESOURCE_IO))
692e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
693e9f82cb7SBenjamin Herrenschmidt 		if (roffset < rp->start || (roffset + size) > rp->end)
694e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
695e9f82cb7SBenjamin Herrenschmidt 		offset += hose->io_base_phys;
696e9f82cb7SBenjamin Herrenschmidt 	}
697e9f82cb7SBenjamin Herrenschmidt 	pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
698e9f82cb7SBenjamin Herrenschmidt 
699e9f82cb7SBenjamin Herrenschmidt 	vma->vm_pgoff = offset >> PAGE_SHIFT;
70064b3d0e8SBenjamin Herrenschmidt 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
701e9f82cb7SBenjamin Herrenschmidt 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
702e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_end - vma->vm_start,
703e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_page_prot);
704e9f82cb7SBenjamin Herrenschmidt }
705e9f82cb7SBenjamin Herrenschmidt 
pci_resource_to_user(const struct pci_dev * dev,int bar,const struct resource * rsrc,resource_size_t * start,resource_size_t * end)70658083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
70758083dadSKumar Gala 			  const struct resource *rsrc,
70858083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
70958083dadSKumar Gala {
71038301358SBjorn Helgaas 	struct pci_bus_region region;
71158083dadSKumar Gala 
71238301358SBjorn Helgaas 	if (rsrc->flags & IORESOURCE_IO) {
71338301358SBjorn Helgaas 		pcibios_resource_to_bus(dev->bus, &region,
71438301358SBjorn Helgaas 					(struct resource *) rsrc);
71538301358SBjorn Helgaas 		*start = region.start;
71638301358SBjorn Helgaas 		*end = region.end;
71758083dadSKumar Gala 		return;
71838301358SBjorn Helgaas 	}
71958083dadSKumar Gala 
72038301358SBjorn Helgaas 	/* We pass a CPU physical address to userland for MMIO instead of a
72138301358SBjorn Helgaas 	 * BAR value because X is lame and expects to be able to use that
72258083dadSKumar Gala 	 * to pass to /dev/mem!
72358083dadSKumar Gala 	 *
72438301358SBjorn Helgaas 	 * That means we may have 64-bit values where some apps only expect
72538301358SBjorn Helgaas 	 * 32 (like X itself since it thinks only Sparc has 64-bit MMIO).
72658083dadSKumar Gala 	 */
72738301358SBjorn Helgaas 	*start = rsrc->start;
72838301358SBjorn Helgaas 	*end = rsrc->end;
72958083dadSKumar Gala }
73013dccb9eSBenjamin Herrenschmidt 
73113dccb9eSBenjamin Herrenschmidt /**
73213dccb9eSBenjamin Herrenschmidt  * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
73313dccb9eSBenjamin Herrenschmidt  * @hose: newly allocated pci_controller to be setup
73413dccb9eSBenjamin Herrenschmidt  * @dev: device node of the host bridge
73513dccb9eSBenjamin Herrenschmidt  * @primary: set if primary bus (32 bits only, soon to be deprecated)
73613dccb9eSBenjamin Herrenschmidt  *
73713dccb9eSBenjamin Herrenschmidt  * This function will parse the "ranges" property of a PCI host bridge device
73813dccb9eSBenjamin Herrenschmidt  * node and setup the resource mapping of a pci controller based on its
73913dccb9eSBenjamin Herrenschmidt  * content.
74013dccb9eSBenjamin Herrenschmidt  *
74113dccb9eSBenjamin Herrenschmidt  * Life would be boring if it wasn't for a few issues that we have to deal
74213dccb9eSBenjamin Herrenschmidt  * with here:
74313dccb9eSBenjamin Herrenschmidt  *
74413dccb9eSBenjamin Herrenschmidt  *   - We can only cope with one IO space range and up to 3 Memory space
74513dccb9eSBenjamin Herrenschmidt  *     ranges. However, some machines (thanks Apple !) tend to split their
74613dccb9eSBenjamin Herrenschmidt  *     space into lots of small contiguous ranges. So we have to coalesce.
74713dccb9eSBenjamin Herrenschmidt  *
74813dccb9eSBenjamin Herrenschmidt  *   - Some busses have IO space not starting at 0, which causes trouble with
74913dccb9eSBenjamin Herrenschmidt  *     the way we do our IO resource renumbering. The code somewhat deals with
75013dccb9eSBenjamin Herrenschmidt  *     it for 64 bits but I would expect problems on 32 bits.
75113dccb9eSBenjamin Herrenschmidt  *
75213dccb9eSBenjamin Herrenschmidt  *   - Some 32 bits platforms such as 4xx can have physical space larger than
75313dccb9eSBenjamin Herrenschmidt  *     32 bits so we need to use 64 bits values for the parsing
75413dccb9eSBenjamin Herrenschmidt  */
pci_process_bridge_OF_ranges(struct pci_controller * hose,struct device_node * dev,int primary)755cad5cef6SGreg Kroah-Hartman void pci_process_bridge_OF_ranges(struct pci_controller *hose,
756cad5cef6SGreg Kroah-Hartman 				  struct device_node *dev, int primary)
75713dccb9eSBenjamin Herrenschmidt {
758858957abSKevin Hao 	int memno = 0;
75913dccb9eSBenjamin Herrenschmidt 	struct resource *res;
760654837e8SAndrew Murray 	struct of_pci_range range;
761654837e8SAndrew Murray 	struct of_pci_range_parser parser;
76213dccb9eSBenjamin Herrenschmidt 
763b7c670d6SRob Herring 	printk(KERN_INFO "PCI host bridge %pOF %s ranges:\n",
764b7c670d6SRob Herring 	       dev, primary ? "(primary)" : "");
76513dccb9eSBenjamin Herrenschmidt 
766654837e8SAndrew Murray 	/* Check for ranges property */
767654837e8SAndrew Murray 	if (of_pci_range_parser_init(&parser, dev))
76813dccb9eSBenjamin Herrenschmidt 		return;
76913dccb9eSBenjamin Herrenschmidt 
77013dccb9eSBenjamin Herrenschmidt 	/* Parse it */
771654837e8SAndrew Murray 	for_each_of_pci_range(&parser, &range) {
772e9f82cb7SBenjamin Herrenschmidt 		/* If we failed translation or got a zero-sized region
773e9f82cb7SBenjamin Herrenschmidt 		 * (some FW try to feed us with non sensical zero sized regions
774e9f82cb7SBenjamin Herrenschmidt 		 * such as power3 which look like some kind of attempt at exposing
775e9f82cb7SBenjamin Herrenschmidt 		 * the VGA memory hole)
776e9f82cb7SBenjamin Herrenschmidt 		 */
777654837e8SAndrew Murray 		if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
77813dccb9eSBenjamin Herrenschmidt 			continue;
77913dccb9eSBenjamin Herrenschmidt 
78013dccb9eSBenjamin Herrenschmidt 		/* Act based on address space type */
78113dccb9eSBenjamin Herrenschmidt 		res = NULL;
782654837e8SAndrew Murray 		switch (range.flags & IORESOURCE_TYPE_BITS) {
783654837e8SAndrew Murray 		case IORESOURCE_IO:
78413dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
78513dccb9eSBenjamin Herrenschmidt 			       "  IO 0x%016llx..0x%016llx -> 0x%016llx\n",
786654837e8SAndrew Murray 			       range.cpu_addr, range.cpu_addr + range.size - 1,
787654837e8SAndrew Murray 			       range.pci_addr);
78813dccb9eSBenjamin Herrenschmidt 
78913dccb9eSBenjamin Herrenschmidt 			/* We support only one IO range */
79013dccb9eSBenjamin Herrenschmidt 			if (hose->pci_io_size) {
79113dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
79213dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
79313dccb9eSBenjamin Herrenschmidt 				continue;
79413dccb9eSBenjamin Herrenschmidt 			}
79513dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32
79613dccb9eSBenjamin Herrenschmidt 			/* On 32 bits, limit I/O space to 16MB */
797654837e8SAndrew Murray 			if (range.size > 0x01000000)
798654837e8SAndrew Murray 				range.size = 0x01000000;
79913dccb9eSBenjamin Herrenschmidt 
80013dccb9eSBenjamin Herrenschmidt 			/* 32 bits needs to map IOs here */
801654837e8SAndrew Murray 			hose->io_base_virt = ioremap(range.cpu_addr,
802654837e8SAndrew Murray 						range.size);
80313dccb9eSBenjamin Herrenschmidt 
80413dccb9eSBenjamin Herrenschmidt 			/* Expect trouble if pci_addr is not 0 */
80513dccb9eSBenjamin Herrenschmidt 			if (primary)
80613dccb9eSBenjamin Herrenschmidt 				isa_io_base =
80713dccb9eSBenjamin Herrenschmidt 					(unsigned long)hose->io_base_virt;
80813dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
80913dccb9eSBenjamin Herrenschmidt 			/* pci_io_size and io_base_phys always represent IO
81013dccb9eSBenjamin Herrenschmidt 			 * space starting at 0 so we factor in pci_addr
81113dccb9eSBenjamin Herrenschmidt 			 */
812654837e8SAndrew Murray 			hose->pci_io_size = range.pci_addr + range.size;
813654837e8SAndrew Murray 			hose->io_base_phys = range.cpu_addr - range.pci_addr;
81413dccb9eSBenjamin Herrenschmidt 
81513dccb9eSBenjamin Herrenschmidt 			/* Build resource */
81613dccb9eSBenjamin Herrenschmidt 			res = &hose->io_resource;
817654837e8SAndrew Murray 			range.cpu_addr = range.pci_addr;
81813dccb9eSBenjamin Herrenschmidt 			break;
819654837e8SAndrew Murray 		case IORESOURCE_MEM:
82013dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
82113dccb9eSBenjamin Herrenschmidt 			       " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
822654837e8SAndrew Murray 			       range.cpu_addr, range.cpu_addr + range.size - 1,
823654837e8SAndrew Murray 			       range.pci_addr,
8246a9166b5SRob Herring 			       (range.flags & IORESOURCE_PREFETCH) ?
825654837e8SAndrew Murray 			       "Prefetch" : "");
82613dccb9eSBenjamin Herrenschmidt 
82713dccb9eSBenjamin Herrenschmidt 			/* We support only 3 memory ranges */
82813dccb9eSBenjamin Herrenschmidt 			if (memno >= 3) {
82913dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
83013dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
83113dccb9eSBenjamin Herrenschmidt 				continue;
83213dccb9eSBenjamin Herrenschmidt 			}
83313dccb9eSBenjamin Herrenschmidt 			/* Handles ISA memory hole space here */
834654837e8SAndrew Murray 			if (range.pci_addr == 0) {
83513dccb9eSBenjamin Herrenschmidt 				if (primary || isa_mem_base == 0)
836654837e8SAndrew Murray 					isa_mem_base = range.cpu_addr;
837654837e8SAndrew Murray 				hose->isa_mem_phys = range.cpu_addr;
838654837e8SAndrew Murray 				hose->isa_mem_size = range.size;
83913dccb9eSBenjamin Herrenschmidt 			}
84013dccb9eSBenjamin Herrenschmidt 
84113dccb9eSBenjamin Herrenschmidt 			/* Build resource */
842654837e8SAndrew Murray 			hose->mem_offset[memno] = range.cpu_addr -
843654837e8SAndrew Murray 							range.pci_addr;
84413dccb9eSBenjamin Herrenschmidt 			res = &hose->mem_resources[memno++];
84513dccb9eSBenjamin Herrenschmidt 			break;
84613dccb9eSBenjamin Herrenschmidt 		}
84713dccb9eSBenjamin Herrenschmidt 		if (res != NULL) {
848aeba3731SMichael Ellerman 			res->name = dev->full_name;
849aeba3731SMichael Ellerman 			res->flags = range.flags;
850aeba3731SMichael Ellerman 			res->start = range.cpu_addr;
851aeba3731SMichael Ellerman 			res->end = range.cpu_addr + range.size - 1;
852aeba3731SMichael Ellerman 			res->parent = res->child = res->sibling = NULL;
85313dccb9eSBenjamin Herrenschmidt 		}
85413dccb9eSBenjamin Herrenschmidt 	}
85513dccb9eSBenjamin Herrenschmidt }
856fa462f2dSBenjamin Herrenschmidt 
857fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */
pci_proc_domain(struct pci_bus * bus)858fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus)
859fa462f2dSBenjamin Herrenschmidt {
860fa462f2dSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
8611fd0f525SBenjamin Herrenschmidt 
8620e47ff1cSRob Herring 	if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
863fa462f2dSBenjamin Herrenschmidt 		return 0;
8640e47ff1cSRob Herring 	if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
865fa462f2dSBenjamin Herrenschmidt 		return hose->global_number != 0;
866fa462f2dSBenjamin Herrenschmidt 	return 1;
867fa462f2dSBenjamin Herrenschmidt }
868fa462f2dSBenjamin Herrenschmidt 
pcibios_root_bridge_prepare(struct pci_host_bridge * bridge)869d82fb31aSKleber Sacilotto de Souza int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
870d82fb31aSKleber Sacilotto de Souza {
871d82fb31aSKleber Sacilotto de Souza 	if (ppc_md.pcibios_root_bridge_prepare)
872d82fb31aSKleber Sacilotto de Souza 		return ppc_md.pcibios_root_bridge_prepare(bridge);
873d82fb31aSKleber Sacilotto de Souza 
874d82fb31aSKleber Sacilotto de Souza 	return 0;
875d82fb31aSKleber Sacilotto de Souza }
876d82fb31aSKleber Sacilotto de Souza 
877bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are
878bf5e2ba2SBenjamin Herrenschmidt  * probed, but not for bridge ranges
879bf5e2ba2SBenjamin Herrenschmidt  */
pcibios_fixup_resources(struct pci_dev * dev)880cad5cef6SGreg Kroah-Hartman static void pcibios_fixup_resources(struct pci_dev *dev)
881bf5e2ba2SBenjamin Herrenschmidt {
882bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
88309cc9006SMika Westerberg 	struct resource *res;
884bf5e2ba2SBenjamin Herrenschmidt 	int i;
885bf5e2ba2SBenjamin Herrenschmidt 
886bf5e2ba2SBenjamin Herrenschmidt 	if (!hose) {
887bf5e2ba2SBenjamin Herrenschmidt 		printk(KERN_ERR "No host bridge for PCI dev %s !\n",
888bf5e2ba2SBenjamin Herrenschmidt 		       pci_name(dev));
889bf5e2ba2SBenjamin Herrenschmidt 		return;
890bf5e2ba2SBenjamin Herrenschmidt 	}
891c3b80fb0SWei Yang 
892c3b80fb0SWei Yang 	if (dev->is_virtfn)
893c3b80fb0SWei Yang 		return;
894c3b80fb0SWei Yang 
89509cc9006SMika Westerberg 	pci_dev_for_each_resource(dev, res, i) {
896c5df457fSKevin Hao 		struct pci_bus_region reg;
89709cc9006SMika Westerberg 
898bf5e2ba2SBenjamin Herrenschmidt 		if (!res->flags)
899bf5e2ba2SBenjamin Herrenschmidt 			continue;
90048c2ce97SBenjamin Herrenschmidt 
90148c2ce97SBenjamin Herrenschmidt 		/* If we're going to re-assign everything, we mark all resources
90248c2ce97SBenjamin Herrenschmidt 		 * as unset (and 0-base them). In addition, we mark BARs starting
90348c2ce97SBenjamin Herrenschmidt 		 * at 0 as unset as well, except if PCI_PROBE_ONLY is also set
90448c2ce97SBenjamin Herrenschmidt 		 * since in that case, we don't want to re-assign anything
9057f172890SBenjamin Herrenschmidt 		 */
906fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &reg, res);
90748c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC) ||
908c5df457fSKevin Hao 		    (reg.start == 0 && !pci_has_flag(PCI_PROBE_ONLY))) {
90948c2ce97SBenjamin Herrenschmidt 			/* Only print message if not re-assigning */
91048c2ce97SBenjamin Herrenschmidt 			if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC))
911ae2a84b4SKevin Hao 				pr_debug("PCI:%s Resource %d %pR is unassigned\n",
912ae2a84b4SKevin Hao 					 pci_name(dev), i, res);
913bf5e2ba2SBenjamin Herrenschmidt 			res->end -= res->start;
914bf5e2ba2SBenjamin Herrenschmidt 			res->start = 0;
915bf5e2ba2SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
916bf5e2ba2SBenjamin Herrenschmidt 			continue;
917bf5e2ba2SBenjamin Herrenschmidt 		}
918bf5e2ba2SBenjamin Herrenschmidt 
919ae2a84b4SKevin Hao 		pr_debug("PCI:%s Resource %d %pR\n", pci_name(dev), i, res);
920bf5e2ba2SBenjamin Herrenschmidt 	}
921bf5e2ba2SBenjamin Herrenschmidt 
922bf5e2ba2SBenjamin Herrenschmidt 	/* Call machine specific resource fixup */
923bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_resources)
924bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_resources(dev);
925bf5e2ba2SBenjamin Herrenschmidt }
926bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
927bf5e2ba2SBenjamin Herrenschmidt 
928b5561511SBenjamin Herrenschmidt /* This function tries to figure out if a bridge resource has been initialized
929b5561511SBenjamin Herrenschmidt  * by the firmware or not. It doesn't have to be absolutely bullet proof, but
930b5561511SBenjamin Herrenschmidt  * things go more smoothly when it gets it right. It should covers cases such
931b5561511SBenjamin Herrenschmidt  * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
932b5561511SBenjamin Herrenschmidt  */
pcibios_uninitialized_bridge_resource(struct pci_bus * bus,struct resource * res)933cad5cef6SGreg Kroah-Hartman static int pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
934b5561511SBenjamin Herrenschmidt 						 struct resource *res)
935bf5e2ba2SBenjamin Herrenschmidt {
936be8cbcd8SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
937bf5e2ba2SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
938b5561511SBenjamin Herrenschmidt 	resource_size_t offset;
9393fd47f06SBenjamin Herrenschmidt 	struct pci_bus_region region;
940b5561511SBenjamin Herrenschmidt 	u16 command;
941b5561511SBenjamin Herrenschmidt 	int i;
942bf5e2ba2SBenjamin Herrenschmidt 
943b5561511SBenjamin Herrenschmidt 	/* We don't do anything if PCI_PROBE_ONLY is set */
9440e47ff1cSRob Herring 	if (pci_has_flag(PCI_PROBE_ONLY))
945b5561511SBenjamin Herrenschmidt 		return 0;
946bf5e2ba2SBenjamin Herrenschmidt 
947b5561511SBenjamin Herrenschmidt 	/* Job is a bit different between memory and IO */
948b5561511SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_MEM) {
949fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, res);
9503fd47f06SBenjamin Herrenschmidt 
9513fd47f06SBenjamin Herrenschmidt 		/* If the BAR is non-0 then it's probably been initialized */
9523fd47f06SBenjamin Herrenschmidt 		if (region.start != 0)
953b5561511SBenjamin Herrenschmidt 			return 0;
954b5561511SBenjamin Herrenschmidt 
955b5561511SBenjamin Herrenschmidt 		/* The BAR is 0, let's check if memory decoding is enabled on
956b5561511SBenjamin Herrenschmidt 		 * the bridge. If not, we consider it unassigned
957b5561511SBenjamin Herrenschmidt 		 */
958b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
959b5561511SBenjamin Herrenschmidt 		if ((command & PCI_COMMAND_MEMORY) == 0)
960b5561511SBenjamin Herrenschmidt 			return 1;
961b5561511SBenjamin Herrenschmidt 
962b5561511SBenjamin Herrenschmidt 		/* Memory decoding is enabled and the BAR is 0. If any of the bridge
963b5561511SBenjamin Herrenschmidt 		 * resources covers that starting address (0 then it's good enough for
9643fd47f06SBenjamin Herrenschmidt 		 * us for memory space)
965b5561511SBenjamin Herrenschmidt 		 */
966b5561511SBenjamin Herrenschmidt 		for (i = 0; i < 3; i++) {
967b5561511SBenjamin Herrenschmidt 			if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
9683fd47f06SBenjamin Herrenschmidt 			    hose->mem_resources[i].start == hose->mem_offset[i])
969b5561511SBenjamin Herrenschmidt 				return 0;
970b5561511SBenjamin Herrenschmidt 		}
971b5561511SBenjamin Herrenschmidt 
972b5561511SBenjamin Herrenschmidt 		/* Well, it starts at 0 and we know it will collide so we may as
973b5561511SBenjamin Herrenschmidt 		 * well consider it as unassigned. That covers the Apple case.
974b5561511SBenjamin Herrenschmidt 		 */
975b5561511SBenjamin Herrenschmidt 		return 1;
976b5561511SBenjamin Herrenschmidt 	} else {
977b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0, then we consider it assigned */
978b5561511SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
979b5561511SBenjamin Herrenschmidt 		if (((res->start - offset) & 0xfffffffful) != 0)
980b5561511SBenjamin Herrenschmidt 			return 0;
981b5561511SBenjamin Herrenschmidt 
982b5561511SBenjamin Herrenschmidt 		/* Here, we are a bit different than memory as typically IO space
983b5561511SBenjamin Herrenschmidt 		 * starting at low addresses -is- valid. What we do instead if that
984b5561511SBenjamin Herrenschmidt 		 * we consider as unassigned anything that doesn't have IO enabled
985b5561511SBenjamin Herrenschmidt 		 * in the PCI command register, and that's it.
986b5561511SBenjamin Herrenschmidt 		 */
987b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
988b5561511SBenjamin Herrenschmidt 		if (command & PCI_COMMAND_IO)
989b5561511SBenjamin Herrenschmidt 			return 0;
990b5561511SBenjamin Herrenschmidt 
991b5561511SBenjamin Herrenschmidt 		/* It's starting at 0 and IO is disabled in the bridge, consider
992b5561511SBenjamin Herrenschmidt 		 * it unassigned
993b5561511SBenjamin Herrenschmidt 		 */
994b5561511SBenjamin Herrenschmidt 		return 1;
995b5561511SBenjamin Herrenschmidt 	}
996b5561511SBenjamin Herrenschmidt }
997b5561511SBenjamin Herrenschmidt 
998b5561511SBenjamin Herrenschmidt /* Fixup resources of a PCI<->PCI bridge */
pcibios_fixup_bridge(struct pci_bus * bus)999cad5cef6SGreg Kroah-Hartman static void pcibios_fixup_bridge(struct pci_bus *bus)
1000b5561511SBenjamin Herrenschmidt {
1001bf5e2ba2SBenjamin Herrenschmidt 	struct resource *res;
1002bf5e2ba2SBenjamin Herrenschmidt 	int i;
1003bf5e2ba2SBenjamin Herrenschmidt 
1004b5561511SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
1005b5561511SBenjamin Herrenschmidt 
100689a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
100789a74eccSBjorn Helgaas 		if (!res || !res->flags)
1008bf5e2ba2SBenjamin Herrenschmidt 			continue;
1009b188b2aeSKumar Gala 		if (i >= 3 && bus->self->transparent)
1010b188b2aeSKumar Gala 			continue;
1011be8cbcd8SBenjamin Herrenschmidt 
1012cf1a4cf8SGavin Shan 		/* If we're going to reassign everything, we can
1013cf1a4cf8SGavin Shan 		 * shrink the P2P resource to have size as being
1014cf1a4cf8SGavin Shan 		 * of 0 in order to save space.
101548c2ce97SBenjamin Herrenschmidt 		 */
101648c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
101748c2ce97SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
101848c2ce97SBenjamin Herrenschmidt 			res->start = 0;
1019cf1a4cf8SGavin Shan 			res->end = -1;
102048c2ce97SBenjamin Herrenschmidt 			continue;
102148c2ce97SBenjamin Herrenschmidt 		}
102248c2ce97SBenjamin Herrenschmidt 
1023ae2a84b4SKevin Hao 		pr_debug("PCI:%s Bus rsrc %d %pR\n", pci_name(dev), i, res);
1024bf5e2ba2SBenjamin Herrenschmidt 
1025b5561511SBenjamin Herrenschmidt 		/* Try to detect uninitialized P2P bridge resources,
1026b5561511SBenjamin Herrenschmidt 		 * and clear them out so they get re-assigned later
1027b5561511SBenjamin Herrenschmidt 		 */
1028b5561511SBenjamin Herrenschmidt 		if (pcibios_uninitialized_bridge_resource(bus, res)) {
1029b5561511SBenjamin Herrenschmidt 			res->flags = 0;
1030b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            (unassigned)\n", pci_name(dev));
1031bf5e2ba2SBenjamin Herrenschmidt 		}
1032bf5e2ba2SBenjamin Herrenschmidt 	}
1033b5561511SBenjamin Herrenschmidt }
1034b5561511SBenjamin Herrenschmidt 
pcibios_setup_bus_self(struct pci_bus * bus)1035cad5cef6SGreg Kroah-Hartman void pcibios_setup_bus_self(struct pci_bus *bus)
10368b8da358SBenjamin Herrenschmidt {
1037467efc2eSDaniel Axtens 	struct pci_controller *phb;
1038467efc2eSDaniel Axtens 
10397eef440aSBenjamin Herrenschmidt 	/* Fix up the bus resources for P2P bridges */
10408b8da358SBenjamin Herrenschmidt 	if (bus->self != NULL)
10418b8da358SBenjamin Herrenschmidt 		pcibios_fixup_bridge(bus);
10428b8da358SBenjamin Herrenschmidt 
10438b8da358SBenjamin Herrenschmidt 	/* Platform specific bus fixups. This is currently only used
10447eef440aSBenjamin Herrenschmidt 	 * by fsl_pci and I'm hoping to get rid of it at some point
10458b8da358SBenjamin Herrenschmidt 	 */
10468b8da358SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_bus)
10478b8da358SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_bus(bus);
10488b8da358SBenjamin Herrenschmidt 
10498b8da358SBenjamin Herrenschmidt 	/* Setup bus DMA mappings */
1050467efc2eSDaniel Axtens 	phb = pci_bus_to_host(bus);
1051467efc2eSDaniel Axtens 	if (phb->controller_ops.dma_bus_setup)
1052467efc2eSDaniel Axtens 		phb->controller_ops.dma_bus_setup(bus);
10538b8da358SBenjamin Herrenschmidt }
10548b8da358SBenjamin Herrenschmidt 
pcibios_bus_add_device(struct pci_dev * dev)10553ab3f3c9SOliver O'Halloran void pcibios_bus_add_device(struct pci_dev *dev)
10567eef440aSBenjamin Herrenschmidt {
1057467efc2eSDaniel Axtens 	struct pci_controller *phb;
10587eef440aSBenjamin Herrenschmidt 	/* Fixup NUMA node as it may not be setup yet by the generic
10597eef440aSBenjamin Herrenschmidt 	 * code and is needed by the DMA init
10607eef440aSBenjamin Herrenschmidt 	 */
10617eef440aSBenjamin Herrenschmidt 	set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
10627eef440aSBenjamin Herrenschmidt 
10637eef440aSBenjamin Herrenschmidt 	/* Hook up default DMA ops */
1064bc0df9ecSNishanth Aravamudan 	set_dma_ops(&dev->dev, pci_dma_ops);
10650617fc0cSChristoph Hellwig 	dev->dev.archdata.dma_offset = PCI_DRAM_OFFSET;
10667eef440aSBenjamin Herrenschmidt 
10677eef440aSBenjamin Herrenschmidt 	/* Additional platform DMA/iommu setup */
1068467efc2eSDaniel Axtens 	phb = pci_bus_to_host(dev->bus);
1069467efc2eSDaniel Axtens 	if (phb->controller_ops.dma_dev_setup)
1070467efc2eSDaniel Axtens 		phb->controller_ops.dma_dev_setup(dev);
10717eef440aSBenjamin Herrenschmidt 
10727eef440aSBenjamin Herrenschmidt 	/* Read default IRQs and fixup if necessary */
10737eef440aSBenjamin Herrenschmidt 	pci_read_irq_line(dev);
10747eef440aSBenjamin Herrenschmidt 	if (ppc_md.pci_irq_fixup)
10757eef440aSBenjamin Herrenschmidt 		ppc_md.pci_irq_fixup(dev);
107630d87ef8SShawn Anastasio 
107730d87ef8SShawn Anastasio 	if (ppc_md.pcibios_bus_add_device)
10783ab3f3c9SOliver O'Halloran 		ppc_md.pcibios_bus_add_device(dev);
107930d87ef8SShawn Anastasio }
108030d87ef8SShawn Anastasio 
pcibios_device_add(struct pci_dev * dev)108106dc660eSOliver O'Halloran int pcibios_device_add(struct pci_dev *dev)
10827846de40SGuenter Roeck {
1083a5f3d2c1SCédric Le Goater 	struct irq_domain *d;
1084a5f3d2c1SCédric Le Goater 
10856e628c7dSWei Yang #ifdef CONFIG_PCI_IOV
10866e628c7dSWei Yang 	if (ppc_md.pcibios_fixup_sriov)
10876e628c7dSWei Yang 		ppc_md.pcibios_fixup_sriov(dev);
10886e628c7dSWei Yang #endif /* CONFIG_PCI_IOV */
10896e628c7dSWei Yang 
1090a5f3d2c1SCédric Le Goater 	d = dev_get_msi_domain(&dev->bus->dev);
1091a5f3d2c1SCédric Le Goater 	if (d)
1092a5f3d2c1SCédric Le Goater 		dev_set_msi_domain(&dev->dev, d);
10937846de40SGuenter Roeck 	return 0;
10947846de40SGuenter Roeck }
10957846de40SGuenter Roeck 
pcibios_set_master(struct pci_dev * dev)109679c8be83SMyron Stowe void pcibios_set_master(struct pci_dev *dev)
109779c8be83SMyron Stowe {
109879c8be83SMyron Stowe 	/* No special bus mastering setup handling */
109979c8be83SMyron Stowe }
110079c8be83SMyron Stowe 
pcibios_fixup_bus(struct pci_bus * bus)1101cad5cef6SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus)
1102bf5e2ba2SBenjamin Herrenschmidt {
1103237865f1SBjorn Helgaas 	/* When called from the generic PCI probe, read PCI<->PCI bridge
1104237865f1SBjorn Helgaas 	 * bases. This is -not- called when generating the PCI tree from
1105237865f1SBjorn Helgaas 	 * the OF device-tree.
1106237865f1SBjorn Helgaas 	 */
1107237865f1SBjorn Helgaas 	pci_read_bridge_bases(bus);
1108237865f1SBjorn Helgaas 
11092b461880SMichael Ellerman 	/* Now fixup the bus */
11108b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_self(bus);
1111bf5e2ba2SBenjamin Herrenschmidt }
1112bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus);
1113bf5e2ba2SBenjamin Herrenschmidt 
skip_isa_ioresource_align(struct pci_dev * dev)11143fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev)
11153fd94c6bSBenjamin Herrenschmidt {
11160e47ff1cSRob Herring 	if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
11173fd94c6bSBenjamin Herrenschmidt 	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
11183fd94c6bSBenjamin Herrenschmidt 		return 1;
11193fd94c6bSBenjamin Herrenschmidt 	return 0;
11203fd94c6bSBenjamin Herrenschmidt }
11213fd94c6bSBenjamin Herrenschmidt 
11223fd94c6bSBenjamin Herrenschmidt /*
11233fd94c6bSBenjamin Herrenschmidt  * We need to avoid collisions with `mirrored' VGA ports
11243fd94c6bSBenjamin Herrenschmidt  * and other strange ISA hardware, so we always want the
11253fd94c6bSBenjamin Herrenschmidt  * addresses to be allocated in the 0x000-0x0ff region
11263fd94c6bSBenjamin Herrenschmidt  * modulo 0x400.
11273fd94c6bSBenjamin Herrenschmidt  *
11283fd94c6bSBenjamin Herrenschmidt  * Why? Because some silly external IO cards only decode
11293fd94c6bSBenjamin Herrenschmidt  * the low 10 bits of the IO address. The 0x00-0xff region
11303fd94c6bSBenjamin Herrenschmidt  * is reserved for motherboard devices that decode all 16
11313fd94c6bSBenjamin Herrenschmidt  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
11323fd94c6bSBenjamin Herrenschmidt  * but we want to try to avoid allocating at 0x2900-0x2bff
11333fd94c6bSBenjamin Herrenschmidt  * which might have be mirrored at 0x0100-0x03ff..
11343fd94c6bSBenjamin Herrenschmidt  */
pcibios_align_resource(void * data,const struct resource * res,resource_size_t size,resource_size_t align)11353b7a17fcSDominik Brodowski resource_size_t pcibios_align_resource(void *data, const struct resource *res,
11363fd94c6bSBenjamin Herrenschmidt 				resource_size_t size, resource_size_t align)
11373fd94c6bSBenjamin Herrenschmidt {
11383fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = data;
11393fd94c6bSBenjamin Herrenschmidt 	resource_size_t start = res->start;
11403fd94c6bSBenjamin Herrenschmidt 
1141b26b2d49SDominik Brodowski 	if (res->flags & IORESOURCE_IO) {
11423fd94c6bSBenjamin Herrenschmidt 		if (skip_isa_ioresource_align(dev))
1143b26b2d49SDominik Brodowski 			return start;
1144b26b2d49SDominik Brodowski 		if (start & 0x300)
11453fd94c6bSBenjamin Herrenschmidt 			start = (start + 0x3ff) & ~0x3ff;
11463fd94c6bSBenjamin Herrenschmidt 	}
1147b26b2d49SDominik Brodowski 
1148b26b2d49SDominik Brodowski 	return start;
11493fd94c6bSBenjamin Herrenschmidt }
11503fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource);
11513fd94c6bSBenjamin Herrenschmidt 
11523fd94c6bSBenjamin Herrenschmidt /*
11533fd94c6bSBenjamin Herrenschmidt  * Reparent resource children of pr that conflict with res
11543fd94c6bSBenjamin Herrenschmidt  * under res, and make res replace those children.
11553fd94c6bSBenjamin Herrenschmidt  */
reparent_resources(struct resource * parent,struct resource * res)11560f6023d5SHeiko Schocher static int reparent_resources(struct resource *parent,
11573fd94c6bSBenjamin Herrenschmidt 				     struct resource *res)
11583fd94c6bSBenjamin Herrenschmidt {
11593fd94c6bSBenjamin Herrenschmidt 	struct resource *p, **pp;
11603fd94c6bSBenjamin Herrenschmidt 	struct resource **firstpp = NULL;
11613fd94c6bSBenjamin Herrenschmidt 
11623fd94c6bSBenjamin Herrenschmidt 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
11633fd94c6bSBenjamin Herrenschmidt 		if (p->end < res->start)
11643fd94c6bSBenjamin Herrenschmidt 			continue;
11653fd94c6bSBenjamin Herrenschmidt 		if (res->end < p->start)
11663fd94c6bSBenjamin Herrenschmidt 			break;
11673fd94c6bSBenjamin Herrenschmidt 		if (p->start < res->start || p->end > res->end)
11683fd94c6bSBenjamin Herrenschmidt 			return -1;	/* not completely contained */
11693fd94c6bSBenjamin Herrenschmidt 		if (firstpp == NULL)
11703fd94c6bSBenjamin Herrenschmidt 			firstpp = pp;
11713fd94c6bSBenjamin Herrenschmidt 	}
11723fd94c6bSBenjamin Herrenschmidt 	if (firstpp == NULL)
11733fd94c6bSBenjamin Herrenschmidt 		return -1;	/* didn't find any conflicting entries? */
11743fd94c6bSBenjamin Herrenschmidt 	res->parent = parent;
11753fd94c6bSBenjamin Herrenschmidt 	res->child = *firstpp;
11763fd94c6bSBenjamin Herrenschmidt 	res->sibling = *pp;
11773fd94c6bSBenjamin Herrenschmidt 	*firstpp = res;
11783fd94c6bSBenjamin Herrenschmidt 	*pp = NULL;
11793fd94c6bSBenjamin Herrenschmidt 	for (p = res->child; p != NULL; p = p->sibling) {
11803fd94c6bSBenjamin Herrenschmidt 		p->parent = res;
1181ae2a84b4SKevin Hao 		pr_debug("PCI: Reparented %s %pR under %s\n",
1182ae2a84b4SKevin Hao 			 p->name, p, res->name);
11833fd94c6bSBenjamin Herrenschmidt 	}
11843fd94c6bSBenjamin Herrenschmidt 	return 0;
11853fd94c6bSBenjamin Herrenschmidt }
11863fd94c6bSBenjamin Herrenschmidt 
11873fd94c6bSBenjamin Herrenschmidt /*
11883fd94c6bSBenjamin Herrenschmidt  *  Handle resources of PCI devices.  If the world were perfect, we could
11893fd94c6bSBenjamin Herrenschmidt  *  just allocate all the resource regions and do nothing more.  It isn't.
11903fd94c6bSBenjamin Herrenschmidt  *  On the other hand, we cannot just re-allocate all devices, as it would
11913fd94c6bSBenjamin Herrenschmidt  *  require us to know lots of host bridge internals.  So we attempt to
11923fd94c6bSBenjamin Herrenschmidt  *  keep as much of the original configuration as possible, but tweak it
11933fd94c6bSBenjamin Herrenschmidt  *  when it's found to be wrong.
11943fd94c6bSBenjamin Herrenschmidt  *
11953fd94c6bSBenjamin Herrenschmidt  *  Known BIOS problems we have to work around:
11963fd94c6bSBenjamin Herrenschmidt  *	- I/O or memory regions not configured
11973fd94c6bSBenjamin Herrenschmidt  *	- regions configured, but not enabled in the command register
11983fd94c6bSBenjamin Herrenschmidt  *	- bogus I/O addresses above 64K used
11993fd94c6bSBenjamin Herrenschmidt  *	- expansion ROMs left enabled (this may sound harmless, but given
12003fd94c6bSBenjamin Herrenschmidt  *	  the fact the PCI specs explicitly allow address decoders to be
12013fd94c6bSBenjamin Herrenschmidt  *	  shared between expansion ROMs and other resource regions, it's
12023fd94c6bSBenjamin Herrenschmidt  *	  at least dangerous)
12033fd94c6bSBenjamin Herrenschmidt  *
12043fd94c6bSBenjamin Herrenschmidt  *  Our solution:
12053fd94c6bSBenjamin Herrenschmidt  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
12063fd94c6bSBenjamin Herrenschmidt  *	    This gives us fixed barriers on where we can allocate.
12073fd94c6bSBenjamin Herrenschmidt  *	(2) Allocate resources for all enabled devices.  If there is
12083fd94c6bSBenjamin Herrenschmidt  *	    a collision, just mark the resource as unallocated. Also
12093fd94c6bSBenjamin Herrenschmidt  *	    disable expansion ROMs during this step.
12103fd94c6bSBenjamin Herrenschmidt  *	(3) Try to allocate resources for disabled devices.  If the
12113fd94c6bSBenjamin Herrenschmidt  *	    resources were assigned correctly, everything goes well,
12123fd94c6bSBenjamin Herrenschmidt  *	    if they weren't, they won't disturb allocation of other
12133fd94c6bSBenjamin Herrenschmidt  *	    resources.
12143fd94c6bSBenjamin Herrenschmidt  *	(4) Assign new addresses to resources which were either
12153fd94c6bSBenjamin Herrenschmidt  *	    not configured at all or misconfigured.  If explicitly
12163fd94c6bSBenjamin Herrenschmidt  *	    requested by the user, configure expansion ROM address
12173fd94c6bSBenjamin Herrenschmidt  *	    as well.
12183fd94c6bSBenjamin Herrenschmidt  */
12193fd94c6bSBenjamin Herrenschmidt 
pcibios_allocate_bus_resources(struct pci_bus * bus)1220e51df2c1SAnton Blanchard static void pcibios_allocate_bus_resources(struct pci_bus *bus)
12213fd94c6bSBenjamin Herrenschmidt {
1222e90a1318SNathan Fontenot 	struct pci_bus *b;
12233fd94c6bSBenjamin Herrenschmidt 	int i;
12243fd94c6bSBenjamin Herrenschmidt 	struct resource *res, *pr;
12253fd94c6bSBenjamin Herrenschmidt 
1226b5ae5f91SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1227b5ae5f91SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1228b5ae5f91SBenjamin Herrenschmidt 
122989a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
123089a74eccSBjorn Helgaas 		if (!res || !res->flags || res->start > res->end || res->parent)
12313fd94c6bSBenjamin Herrenschmidt 			continue;
123248c2ce97SBenjamin Herrenschmidt 
123348c2ce97SBenjamin Herrenschmidt 		/* If the resource was left unset at this point, we clear it */
123448c2ce97SBenjamin Herrenschmidt 		if (res->flags & IORESOURCE_UNSET)
123548c2ce97SBenjamin Herrenschmidt 			goto clear_resource;
123648c2ce97SBenjamin Herrenschmidt 
12373fd94c6bSBenjamin Herrenschmidt 		if (bus->parent == NULL)
12383fd94c6bSBenjamin Herrenschmidt 			pr = (res->flags & IORESOURCE_IO) ?
12393fd94c6bSBenjamin Herrenschmidt 				&ioport_resource : &iomem_resource;
12403fd94c6bSBenjamin Herrenschmidt 		else {
12413fd94c6bSBenjamin Herrenschmidt 			pr = pci_find_parent_resource(bus->self, res);
12423fd94c6bSBenjamin Herrenschmidt 			if (pr == res) {
12433fd94c6bSBenjamin Herrenschmidt 				/* this happens when the generic PCI
12443fd94c6bSBenjamin Herrenschmidt 				 * code (wrongly) decides that this
12453fd94c6bSBenjamin Herrenschmidt 				 * bridge is transparent  -- paulus
12463fd94c6bSBenjamin Herrenschmidt 				 */
12473fd94c6bSBenjamin Herrenschmidt 				continue;
12483fd94c6bSBenjamin Herrenschmidt 			}
12493fd94c6bSBenjamin Herrenschmidt 		}
12503fd94c6bSBenjamin Herrenschmidt 
1251ae2a84b4SKevin Hao 		pr_debug("PCI: %s (bus %d) bridge rsrc %d: %pR, parent %p (%s)\n",
1252ae2a84b4SKevin Hao 			 bus->self ? pci_name(bus->self) : "PHB", bus->number,
1253ae2a84b4SKevin Hao 			 i, res, pr, (pr && pr->name) ? pr->name : "nil");
12543fd94c6bSBenjamin Herrenschmidt 
12553fd94c6bSBenjamin Herrenschmidt 		if (pr && !(pr->flags & IORESOURCE_UNSET)) {
12563ebfe46aSYinghai Lu 			struct pci_dev *dev = bus->self;
12573ebfe46aSYinghai Lu 
12583fd94c6bSBenjamin Herrenschmidt 			if (request_resource(pr, res) == 0)
12593fd94c6bSBenjamin Herrenschmidt 				continue;
12603fd94c6bSBenjamin Herrenschmidt 			/*
12613fd94c6bSBenjamin Herrenschmidt 			 * Must be a conflict with an existing entry.
12623fd94c6bSBenjamin Herrenschmidt 			 * Move that entry (or entries) under the
12633fd94c6bSBenjamin Herrenschmidt 			 * bridge resource and try again.
12643fd94c6bSBenjamin Herrenschmidt 			 */
12653fd94c6bSBenjamin Herrenschmidt 			if (reparent_resources(pr, res) == 0)
12663fd94c6bSBenjamin Herrenschmidt 				continue;
12673ebfe46aSYinghai Lu 
12683ebfe46aSYinghai Lu 			if (dev && i < PCI_BRIDGE_RESOURCE_NUM &&
12693ebfe46aSYinghai Lu 			    pci_claim_bridge_resource(dev,
12703ebfe46aSYinghai Lu 						i + PCI_BRIDGE_RESOURCES) == 0)
12713ebfe46aSYinghai Lu 				continue;
12723fd94c6bSBenjamin Herrenschmidt 		}
1273f2c2cbccSJoe Perches 		pr_warn("PCI: Cannot allocate resource region %d of PCI bridge %d, will remap\n",
1274f2c2cbccSJoe Perches 			i, bus->number);
12753fd94c6bSBenjamin Herrenschmidt 	clear_resource:
1276cf1a4cf8SGavin Shan 		/* The resource might be figured out when doing
1277cf1a4cf8SGavin Shan 		 * reassignment based on the resources required
1278cf1a4cf8SGavin Shan 		 * by the downstream PCI devices. Here we set
1279cf1a4cf8SGavin Shan 		 * the size of the resource to be 0 in order to
1280cf1a4cf8SGavin Shan 		 * save more space.
1281cf1a4cf8SGavin Shan 		 */
1282cf1a4cf8SGavin Shan 		res->start = 0;
1283cf1a4cf8SGavin Shan 		res->end = -1;
12843fd94c6bSBenjamin Herrenschmidt 		res->flags = 0;
12853fd94c6bSBenjamin Herrenschmidt 	}
1286e90a1318SNathan Fontenot 
1287e90a1318SNathan Fontenot 	list_for_each_entry(b, &bus->children, node)
1288e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
12893fd94c6bSBenjamin Herrenschmidt }
12903fd94c6bSBenjamin Herrenschmidt 
alloc_resource(struct pci_dev * dev,int idx)1291cad5cef6SGreg Kroah-Hartman static inline void alloc_resource(struct pci_dev *dev, int idx)
12923fd94c6bSBenjamin Herrenschmidt {
12933fd94c6bSBenjamin Herrenschmidt 	struct resource *pr, *r = &dev->resource[idx];
12943fd94c6bSBenjamin Herrenschmidt 
1295ae2a84b4SKevin Hao 	pr_debug("PCI: Allocating %s: Resource %d: %pR\n",
1296ae2a84b4SKevin Hao 		 pci_name(dev), idx, r);
12973fd94c6bSBenjamin Herrenschmidt 
12983fd94c6bSBenjamin Herrenschmidt 	pr = pci_find_parent_resource(dev, r);
12993fd94c6bSBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||
13003fd94c6bSBenjamin Herrenschmidt 	    request_resource(pr, r) < 0) {
13013fd94c6bSBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
13023fd94c6bSBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
13033fd94c6bSBenjamin Herrenschmidt 		if (pr)
1304ae2a84b4SKevin Hao 			pr_debug("PCI:  parent is %p: %pR\n", pr, pr);
13053fd94c6bSBenjamin Herrenschmidt 		/* We'll assign a new address later */
13063fd94c6bSBenjamin Herrenschmidt 		r->flags |= IORESOURCE_UNSET;
13073fd94c6bSBenjamin Herrenschmidt 		r->end -= r->start;
13083fd94c6bSBenjamin Herrenschmidt 		r->start = 0;
13093fd94c6bSBenjamin Herrenschmidt 	}
13103fd94c6bSBenjamin Herrenschmidt }
13113fd94c6bSBenjamin Herrenschmidt 
pcibios_allocate_resources(int pass)13123fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass)
13133fd94c6bSBenjamin Herrenschmidt {
13143fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = NULL;
13153fd94c6bSBenjamin Herrenschmidt 	int idx, disabled;
13163fd94c6bSBenjamin Herrenschmidt 	u16 command;
13173fd94c6bSBenjamin Herrenschmidt 	struct resource *r;
13183fd94c6bSBenjamin Herrenschmidt 
13193fd94c6bSBenjamin Herrenschmidt 	for_each_pci_dev(dev) {
13203fd94c6bSBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1321ad892a63SBenjamin Herrenschmidt 		for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
13223fd94c6bSBenjamin Herrenschmidt 			r = &dev->resource[idx];
13233fd94c6bSBenjamin Herrenschmidt 			if (r->parent)		/* Already allocated */
13243fd94c6bSBenjamin Herrenschmidt 				continue;
13253fd94c6bSBenjamin Herrenschmidt 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
13263fd94c6bSBenjamin Herrenschmidt 				continue;	/* Not assigned at all */
1327ad892a63SBenjamin Herrenschmidt 			/* We only allocate ROMs on pass 1 just in case they
1328ad892a63SBenjamin Herrenschmidt 			 * have been screwed up by firmware
1329ad892a63SBenjamin Herrenschmidt 			 */
1330ad892a63SBenjamin Herrenschmidt 			if (idx == PCI_ROM_RESOURCE )
1331ad892a63SBenjamin Herrenschmidt 				disabled = 1;
13323fd94c6bSBenjamin Herrenschmidt 			if (r->flags & IORESOURCE_IO)
13333fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_IO);
13343fd94c6bSBenjamin Herrenschmidt 			else
13353fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_MEMORY);
1336533b1928SPaul Mackerras 			if (pass == disabled)
1337533b1928SPaul Mackerras 				alloc_resource(dev, idx);
13383fd94c6bSBenjamin Herrenschmidt 		}
13393fd94c6bSBenjamin Herrenschmidt 		if (pass)
13403fd94c6bSBenjamin Herrenschmidt 			continue;
13413fd94c6bSBenjamin Herrenschmidt 		r = &dev->resource[PCI_ROM_RESOURCE];
1342ad892a63SBenjamin Herrenschmidt 		if (r->flags) {
13433fd94c6bSBenjamin Herrenschmidt 			/* Turn the ROM off, leave the resource region,
13443fd94c6bSBenjamin Herrenschmidt 			 * but keep it unregistered.
13453fd94c6bSBenjamin Herrenschmidt 			 */
13463fd94c6bSBenjamin Herrenschmidt 			u32 reg;
1347ad892a63SBenjamin Herrenschmidt 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1348ad892a63SBenjamin Herrenschmidt 			if (reg & PCI_ROM_ADDRESS_ENABLE) {
1349b0494bc8SBenjamin Herrenschmidt 				pr_debug("PCI: Switching off ROM of %s\n",
1350b0494bc8SBenjamin Herrenschmidt 					 pci_name(dev));
13513fd94c6bSBenjamin Herrenschmidt 				r->flags &= ~IORESOURCE_ROM_ENABLE;
13523fd94c6bSBenjamin Herrenschmidt 				pci_write_config_dword(dev, dev->rom_base_reg,
13533fd94c6bSBenjamin Herrenschmidt 						       reg & ~PCI_ROM_ADDRESS_ENABLE);
13543fd94c6bSBenjamin Herrenschmidt 			}
13553fd94c6bSBenjamin Herrenschmidt 		}
13563fd94c6bSBenjamin Herrenschmidt 	}
1357ad892a63SBenjamin Herrenschmidt }
13583fd94c6bSBenjamin Herrenschmidt 
pcibios_reserve_legacy_regions(struct pci_bus * bus)1359c1f34302SBenjamin Herrenschmidt static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
1360c1f34302SBenjamin Herrenschmidt {
1361c1f34302SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
1362c1f34302SBenjamin Herrenschmidt 	resource_size_t	offset;
1363c1f34302SBenjamin Herrenschmidt 	struct resource *res, *pres;
1364c1f34302SBenjamin Herrenschmidt 	int i;
1365c1f34302SBenjamin Herrenschmidt 
1366c1f34302SBenjamin Herrenschmidt 	pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
1367c1f34302SBenjamin Herrenschmidt 
1368c1f34302SBenjamin Herrenschmidt 	/* Check for IO */
1369c1f34302SBenjamin Herrenschmidt 	if (!(hose->io_resource.flags & IORESOURCE_IO))
1370c1f34302SBenjamin Herrenschmidt 		goto no_io;
1371c1f34302SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1372c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1373c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1374c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy IO";
1375c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_IO;
1376c1f34302SBenjamin Herrenschmidt 	res->start = offset;
1377c1f34302SBenjamin Herrenschmidt 	res->end = (offset + 0xfff) & 0xfffffffful;
1378c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate legacy IO: %pR\n", res);
1379c1f34302SBenjamin Herrenschmidt 	if (request_resource(&hose->io_resource, res)) {
1380c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1381c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1382c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1383c1f34302SBenjamin Herrenschmidt 		kfree(res);
1384c1f34302SBenjamin Herrenschmidt 	}
1385c1f34302SBenjamin Herrenschmidt 
1386c1f34302SBenjamin Herrenschmidt  no_io:
1387c1f34302SBenjamin Herrenschmidt 	/* Check for memory */
1388c1f34302SBenjamin Herrenschmidt 	for (i = 0; i < 3; i++) {
1389c1f34302SBenjamin Herrenschmidt 		pres = &hose->mem_resources[i];
13903fd47f06SBenjamin Herrenschmidt 		offset = hose->mem_offset[i];
1391c1f34302SBenjamin Herrenschmidt 		if (!(pres->flags & IORESOURCE_MEM))
1392c1f34302SBenjamin Herrenschmidt 			continue;
1393c1f34302SBenjamin Herrenschmidt 		pr_debug("hose mem res: %pR\n", pres);
1394c1f34302SBenjamin Herrenschmidt 		if ((pres->start - offset) <= 0xa0000 &&
1395c1f34302SBenjamin Herrenschmidt 		    (pres->end - offset) >= 0xbffff)
1396c1f34302SBenjamin Herrenschmidt 			break;
1397c1f34302SBenjamin Herrenschmidt 	}
1398c1f34302SBenjamin Herrenschmidt 	if (i >= 3)
1399c1f34302SBenjamin Herrenschmidt 		return;
1400c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1401c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1402c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy VGA memory";
1403c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_MEM;
1404c1f34302SBenjamin Herrenschmidt 	res->start = 0xa0000 + offset;
1405c1f34302SBenjamin Herrenschmidt 	res->end = 0xbffff + offset;
1406c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate VGA memory: %pR\n", res);
1407c1f34302SBenjamin Herrenschmidt 	if (request_resource(pres, res)) {
1408c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1409c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1410c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1411c1f34302SBenjamin Herrenschmidt 		kfree(res);
1412c1f34302SBenjamin Herrenschmidt 	}
1413c1f34302SBenjamin Herrenschmidt }
1414c1f34302SBenjamin Herrenschmidt 
pcibios_resource_survey(void)14153fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void)
14163fd94c6bSBenjamin Herrenschmidt {
1417e90a1318SNathan Fontenot 	struct pci_bus *b;
1418e90a1318SNathan Fontenot 
141948c2ce97SBenjamin Herrenschmidt 	/* Allocate and assign resources */
1420e90a1318SNathan Fontenot 	list_for_each_entry(b, &pci_root_buses, node)
1421e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
14229a1a70aeSBenjamin Herrenschmidt 	if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
14233fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(0);
14243fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(1);
14259a1a70aeSBenjamin Herrenschmidt 	}
14263fd94c6bSBenjamin Herrenschmidt 
1427c1f34302SBenjamin Herrenschmidt 	/* Before we start assigning unassigned resource, we try to reserve
1428c1f34302SBenjamin Herrenschmidt 	 * the low IO area and the VGA memory area if they intersect the
1429c1f34302SBenjamin Herrenschmidt 	 * bus available resources to avoid allocating things on top of them
1430c1f34302SBenjamin Herrenschmidt 	 */
14310e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1432c1f34302SBenjamin Herrenschmidt 		list_for_each_entry(b, &pci_root_buses, node)
1433c1f34302SBenjamin Herrenschmidt 			pcibios_reserve_legacy_regions(b);
1434c1f34302SBenjamin Herrenschmidt 	}
1435c1f34302SBenjamin Herrenschmidt 
1436c1f34302SBenjamin Herrenschmidt 	/* Now, if the platform didn't decide to blindly trust the firmware,
1437c1f34302SBenjamin Herrenschmidt 	 * we proceed to assigning things that were left unassigned
1438c1f34302SBenjamin Herrenschmidt 	 */
14390e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1440a77acda0SWolfram Sang 		pr_debug("PCI: Assigning unassigned resources...\n");
14413fd94c6bSBenjamin Herrenschmidt 		pci_assign_unassigned_resources();
14423fd94c6bSBenjamin Herrenschmidt 	}
14433fd94c6bSBenjamin Herrenschmidt }
14443fd94c6bSBenjamin Herrenschmidt 
1445fd6852c8SBenjamin Herrenschmidt /* This is used by the PCI hotplug driver to allocate resource
14463fd94c6bSBenjamin Herrenschmidt  * of newly plugged busses. We can try to consolidate with the
1447fd6852c8SBenjamin Herrenschmidt  * rest of the code later, for now, keep it as-is as our main
1448fd6852c8SBenjamin Herrenschmidt  * resource allocation function doesn't deal with sub-trees yet.
14493fd94c6bSBenjamin Herrenschmidt  */
pcibios_claim_one_bus(struct pci_bus * bus)1450baf75b0aSStephen Rothwell void pcibios_claim_one_bus(struct pci_bus *bus)
14513fd94c6bSBenjamin Herrenschmidt {
14523fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev;
14533fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *child_bus;
14543fd94c6bSBenjamin Herrenschmidt 
14553fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
145609cc9006SMika Westerberg 		struct resource *r;
14573fd94c6bSBenjamin Herrenschmidt 		int i;
14583fd94c6bSBenjamin Herrenschmidt 
145909cc9006SMika Westerberg 		pci_dev_for_each_resource(dev, r, i) {
14603fd94c6bSBenjamin Herrenschmidt 			if (r->parent || !r->start || !r->flags)
14613fd94c6bSBenjamin Herrenschmidt 				continue;
1462fd6852c8SBenjamin Herrenschmidt 
1463ae2a84b4SKevin Hao 			pr_debug("PCI: Claiming %s: Resource %d: %pR\n",
1464ae2a84b4SKevin Hao 				 pci_name(dev), i, r);
1465fd6852c8SBenjamin Herrenschmidt 
14663ebfe46aSYinghai Lu 			if (pci_claim_resource(dev, i) == 0)
14673ebfe46aSYinghai Lu 				continue;
14683ebfe46aSYinghai Lu 
14693ebfe46aSYinghai Lu 			pci_claim_bridge_resource(dev, i);
14703fd94c6bSBenjamin Herrenschmidt 		}
14713fd94c6bSBenjamin Herrenschmidt 	}
14723fd94c6bSBenjamin Herrenschmidt 
14733fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(child_bus, &bus->children, node)
14743fd94c6bSBenjamin Herrenschmidt 		pcibios_claim_one_bus(child_bus);
14753fd94c6bSBenjamin Herrenschmidt }
14765b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
1477fd6852c8SBenjamin Herrenschmidt 
1478fd6852c8SBenjamin Herrenschmidt 
1479fd6852c8SBenjamin Herrenschmidt /* pcibios_finish_adding_to_bus
1480fd6852c8SBenjamin Herrenschmidt  *
1481fd6852c8SBenjamin Herrenschmidt  * This is to be called by the hotplug code after devices have been
1482fd6852c8SBenjamin Herrenschmidt  * added to a bus, this include calling it for a PHB that is just
1483fd6852c8SBenjamin Herrenschmidt  * being added
1484fd6852c8SBenjamin Herrenschmidt  */
pcibios_finish_adding_to_bus(struct pci_bus * bus)1485fd6852c8SBenjamin Herrenschmidt void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1486fd6852c8SBenjamin Herrenschmidt {
1487fd6852c8SBenjamin Herrenschmidt 	pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1488fd6852c8SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1489fd6852c8SBenjamin Herrenschmidt 
1490fd6852c8SBenjamin Herrenschmidt 	/* Allocate bus and devices resources */
1491fd6852c8SBenjamin Herrenschmidt 	pcibios_allocate_bus_resources(bus);
1492fd6852c8SBenjamin Herrenschmidt 	pcibios_claim_one_bus(bus);
14937415c14cSGavin Shan 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
14947415c14cSGavin Shan 		if (bus->self)
14957415c14cSGavin Shan 			pci_assign_unassigned_bridge_resources(bus->self);
14967415c14cSGavin Shan 		else
1497ab444ec9SGavin Shan 			pci_assign_unassigned_bus_resources(bus);
14987415c14cSGavin Shan 	}
1499fd6852c8SBenjamin Herrenschmidt 
1500fd6852c8SBenjamin Herrenschmidt 	/* Add new devices to global lists.  Register in proc, sysfs. */
1501fd6852c8SBenjamin Herrenschmidt 	pci_bus_add_devices(bus);
1502fd6852c8SBenjamin Herrenschmidt }
1503fd6852c8SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1504fd6852c8SBenjamin Herrenschmidt 
pcibios_enable_device(struct pci_dev * dev,int mask)1505549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask)
1506549beb9bSBenjamin Herrenschmidt {
1507467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
1508467efc2eSDaniel Axtens 
1509467efc2eSDaniel Axtens 	if (phb->controller_ops.enable_device_hook)
1510467efc2eSDaniel Axtens 		if (!phb->controller_ops.enable_device_hook(dev))
1511549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1512549beb9bSBenjamin Herrenschmidt 
15137cfb5f9aSBjorn Helgaas 	return pci_enable_resources(dev, mask);
1514549beb9bSBenjamin Herrenschmidt }
151553280323SBenjamin Herrenschmidt 
pcibios_disable_device(struct pci_dev * dev)1516abeeed6dSMichael Neuling void pcibios_disable_device(struct pci_dev *dev)
1517abeeed6dSMichael Neuling {
1518abeeed6dSMichael Neuling 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
1519abeeed6dSMichael Neuling 
1520abeeed6dSMichael Neuling 	if (phb->controller_ops.disable_device)
1521abeeed6dSMichael Neuling 		phb->controller_ops.disable_device(dev);
1522abeeed6dSMichael Neuling }
1523abeeed6dSMichael Neuling 
pcibios_io_space_offset(struct pci_controller * hose)152438973ba7SBjorn Helgaas resource_size_t pcibios_io_space_offset(struct pci_controller *hose)
152538973ba7SBjorn Helgaas {
152638973ba7SBjorn Helgaas 	return (unsigned long) hose->io_base_virt - _IO_BASE;
152738973ba7SBjorn Helgaas }
152838973ba7SBjorn Helgaas 
pcibios_setup_phb_resources(struct pci_controller * hose,struct list_head * resources)1529cad5cef6SGreg Kroah-Hartman static void pcibios_setup_phb_resources(struct pci_controller *hose,
1530cad5cef6SGreg Kroah-Hartman 					struct list_head *resources)
153153280323SBenjamin Herrenschmidt {
153253280323SBenjamin Herrenschmidt 	struct resource *res;
15333fd47f06SBenjamin Herrenschmidt 	resource_size_t offset;
153453280323SBenjamin Herrenschmidt 	int i;
153553280323SBenjamin Herrenschmidt 
153653280323SBenjamin Herrenschmidt 	/* Hookup PHB IO resource */
153745a709f8SBjorn Helgaas 	res = &hose->io_resource;
153853280323SBenjamin Herrenschmidt 
153953280323SBenjamin Herrenschmidt 	if (!res->flags) {
1540cdb1b342SBenjamin Herrenschmidt 		pr_debug("PCI: I/O resource not set for host"
1541b7c670d6SRob Herring 			 " bridge %pOF (domain %d)\n",
1542b7c670d6SRob Herring 			 hose->dn, hose->global_number);
15433fd47f06SBenjamin Herrenschmidt 	} else {
15443fd47f06SBenjamin Herrenschmidt 		offset = pcibios_io_space_offset(hose);
15453fd47f06SBenjamin Herrenschmidt 
1546ae2a84b4SKevin Hao 		pr_debug("PCI: PHB IO resource    = %pR off 0x%08llx\n",
1547ae2a84b4SKevin Hao 			 res, (unsigned long long)offset);
15483fd47f06SBenjamin Herrenschmidt 		pci_add_resource_offset(resources, res, offset);
1549a0b8e76fSBenjamin Herrenschmidt 	}
1550a0b8e76fSBenjamin Herrenschmidt 
155153280323SBenjamin Herrenschmidt 	/* Hookup PHB Memory resources */
155253280323SBenjamin Herrenschmidt 	for (i = 0; i < 3; ++i) {
155353280323SBenjamin Herrenschmidt 		res = &hose->mem_resources[i];
1554727597d1SGavin Shan 		if (!res->flags)
15553fd47f06SBenjamin Herrenschmidt 			continue;
1556727597d1SGavin Shan 
15573fd47f06SBenjamin Herrenschmidt 		offset = hose->mem_offset[i];
1558ae2a84b4SKevin Hao 		pr_debug("PCI: PHB MEM resource %d = %pR off 0x%08llx\n", i,
1559ae2a84b4SKevin Hao 			 res, (unsigned long long)offset);
156053280323SBenjamin Herrenschmidt 
15613fd47f06SBenjamin Herrenschmidt 		pci_add_resource_offset(resources, res, offset);
15623fd47f06SBenjamin Herrenschmidt 	}
156353280323SBenjamin Herrenschmidt }
156489c2dd62SKumar Gala 
156589c2dd62SKumar Gala /*
156689c2dd62SKumar Gala  * Null PCI config access functions, for the case when we can't
156789c2dd62SKumar Gala  * find a hose.
156889c2dd62SKumar Gala  */
156989c2dd62SKumar Gala #define NULL_PCI_OP(rw, size, type)					\
157089c2dd62SKumar Gala static int								\
157189c2dd62SKumar Gala null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
157289c2dd62SKumar Gala {									\
157389c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
157489c2dd62SKumar Gala }
157589c2dd62SKumar Gala 
157689c2dd62SKumar Gala static int
null_read_config(struct pci_bus * bus,unsigned int devfn,int offset,int len,u32 * val)157789c2dd62SKumar Gala null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
157889c2dd62SKumar Gala 		 int len, u32 *val)
157989c2dd62SKumar Gala {
158089c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
158189c2dd62SKumar Gala }
158289c2dd62SKumar Gala 
158389c2dd62SKumar Gala static int
null_write_config(struct pci_bus * bus,unsigned int devfn,int offset,int len,u32 val)158489c2dd62SKumar Gala null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
158589c2dd62SKumar Gala 		  int len, u32 val)
158689c2dd62SKumar Gala {
158789c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
158889c2dd62SKumar Gala }
158989c2dd62SKumar Gala 
159089c2dd62SKumar Gala static struct pci_ops null_pci_ops =
159189c2dd62SKumar Gala {
159289c2dd62SKumar Gala 	.read = null_read_config,
159389c2dd62SKumar Gala 	.write = null_write_config,
159489c2dd62SKumar Gala };
159589c2dd62SKumar Gala 
159689c2dd62SKumar Gala /*
159789c2dd62SKumar Gala  * These functions are used early on before PCI scanning is done
159889c2dd62SKumar Gala  * and all of the pci_dev and pci_bus structures have been created.
159989c2dd62SKumar Gala  */
160089c2dd62SKumar Gala static struct pci_bus *
fake_pci_bus(struct pci_controller * hose,int busnr)160189c2dd62SKumar Gala fake_pci_bus(struct pci_controller *hose, int busnr)
160289c2dd62SKumar Gala {
160389c2dd62SKumar Gala 	static struct pci_bus bus;
160489c2dd62SKumar Gala 
1605b0d436c7SAnton Blanchard 	if (hose == NULL) {
160689c2dd62SKumar Gala 		printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
160789c2dd62SKumar Gala 	}
160889c2dd62SKumar Gala 	bus.number = busnr;
160989c2dd62SKumar Gala 	bus.sysdata = hose;
161089c2dd62SKumar Gala 	bus.ops = hose? hose->ops: &null_pci_ops;
161189c2dd62SKumar Gala 	return &bus;
161289c2dd62SKumar Gala }
161389c2dd62SKumar Gala 
161489c2dd62SKumar Gala #define EARLY_PCI_OP(rw, size, type)					\
161589c2dd62SKumar Gala int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
161689c2dd62SKumar Gala 			       int devfn, int offset, type value)	\
161789c2dd62SKumar Gala {									\
161889c2dd62SKumar Gala 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
161989c2dd62SKumar Gala 					    devfn, offset, value);	\
162089c2dd62SKumar Gala }
162189c2dd62SKumar Gala 
EARLY_PCI_OP(read,byte,u8 *)162289c2dd62SKumar Gala EARLY_PCI_OP(read, byte, u8 *)
162389c2dd62SKumar Gala EARLY_PCI_OP(read, word, u16 *)
162489c2dd62SKumar Gala EARLY_PCI_OP(read, dword, u32 *)
162589c2dd62SKumar Gala EARLY_PCI_OP(write, byte, u8)
162689c2dd62SKumar Gala EARLY_PCI_OP(write, word, u16)
162789c2dd62SKumar Gala EARLY_PCI_OP(write, dword, u32)
162889c2dd62SKumar Gala 
162989c2dd62SKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
163089c2dd62SKumar Gala 			  int cap)
163189c2dd62SKumar Gala {
163289c2dd62SKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
163389c2dd62SKumar Gala }
16340ed2c722SGrant Likely 
pcibios_get_phb_of_node(struct pci_bus * bus)163598d9f30cSBenjamin Herrenschmidt struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
163698d9f30cSBenjamin Herrenschmidt {
163798d9f30cSBenjamin Herrenschmidt 	struct pci_controller *hose = bus->sysdata;
163898d9f30cSBenjamin Herrenschmidt 
163998d9f30cSBenjamin Herrenschmidt 	return of_node_get(hose->dn);
164098d9f30cSBenjamin Herrenschmidt }
164198d9f30cSBenjamin Herrenschmidt 
16420ed2c722SGrant Likely /**
16430ed2c722SGrant Likely  * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
16440ed2c722SGrant Likely  * @hose: Pointer to the PCI host controller instance structure
16450ed2c722SGrant Likely  */
pcibios_scan_phb(struct pci_controller * hose)1646cad5cef6SGreg Kroah-Hartman void pcibios_scan_phb(struct pci_controller *hose)
16470ed2c722SGrant Likely {
164845a709f8SBjorn Helgaas 	LIST_HEAD(resources);
16490ed2c722SGrant Likely 	struct pci_bus *bus;
16500ed2c722SGrant Likely 	struct device_node *node = hose->dn;
16510ed2c722SGrant Likely 	int mode;
16520ed2c722SGrant Likely 
1653b7c670d6SRob Herring 	pr_debug("PCI: Scanning PHB %pOF\n", node);
16540ed2c722SGrant Likely 
16550ed2c722SGrant Likely 	/* Get some IO space for the new PHB */
16560ed2c722SGrant Likely 	pcibios_setup_phb_io_space(hose);
16570ed2c722SGrant Likely 
16580ed2c722SGrant Likely 	/* Wire up PHB bus resources */
165945a709f8SBjorn Helgaas 	pcibios_setup_phb_resources(hose, &resources);
166045a709f8SBjorn Helgaas 
1661be8e60d8SYinghai Lu 	hose->busn.start = hose->first_busno;
1662be8e60d8SYinghai Lu 	hose->busn.end	 = hose->last_busno;
1663be8e60d8SYinghai Lu 	hose->busn.flags = IORESOURCE_BUS;
1664be8e60d8SYinghai Lu 	pci_add_resource(&resources, &hose->busn);
1665be8e60d8SYinghai Lu 
166645a709f8SBjorn Helgaas 	/* Create an empty bus for the toplevel */
166745a709f8SBjorn Helgaas 	bus = pci_create_root_bus(hose->parent, hose->first_busno,
166845a709f8SBjorn Helgaas 				  hose->ops, hose, &resources);
166945a709f8SBjorn Helgaas 	if (bus == NULL) {
167045a709f8SBjorn Helgaas 		pr_err("Failed to create bus for PCI domain %04x\n",
167145a709f8SBjorn Helgaas 			hose->global_number);
167245a709f8SBjorn Helgaas 		pci_free_resource_list(&resources);
167345a709f8SBjorn Helgaas 		return;
167445a709f8SBjorn Helgaas 	}
167545a709f8SBjorn Helgaas 	hose->bus = bus;
16760ed2c722SGrant Likely 
16770ed2c722SGrant Likely 	/* Get probe mode and perform scan */
16780ed2c722SGrant Likely 	mode = PCI_PROBE_NORMAL;
1679467efc2eSDaniel Axtens 	if (node && hose->controller_ops.probe_mode)
1680467efc2eSDaniel Axtens 		mode = hose->controller_ops.probe_mode(bus);
16810ed2c722SGrant Likely 	pr_debug("    probe mode: %d\n", mode);
1682be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_DEVTREE)
16830ed2c722SGrant Likely 		of_scan_bus(node, bus);
16840ed2c722SGrant Likely 
1685be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_NORMAL) {
1686be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, 255);
1687be8e60d8SYinghai Lu 		hose->last_busno = pci_scan_child_bus(bus);
1688be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, hose->last_busno);
1689be8e60d8SYinghai Lu 	}
1690781fb7a3SBenjamin Herrenschmidt 
1691491b98c3SBenjamin Herrenschmidt 	/* Platform gets a chance to do some global fixups before
1692491b98c3SBenjamin Herrenschmidt 	 * we proceed to resource allocation
1693491b98c3SBenjamin Herrenschmidt 	 */
1694491b98c3SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_phb)
1695491b98c3SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_phb(hose);
1696491b98c3SBenjamin Herrenschmidt 
1697781fb7a3SBenjamin Herrenschmidt 	/* Configure PCI Express settings */
1698bb36c445SBenjamin Herrenschmidt 	if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
1699781fb7a3SBenjamin Herrenschmidt 		struct pci_bus *child;
1700a58674ffSBjorn Helgaas 		list_for_each_entry(child, &bus->children, node)
1701a58674ffSBjorn Helgaas 			pcie_bus_configure_settings(child);
1702781fb7a3SBenjamin Herrenschmidt 	}
17030ed2c722SGrant Likely }
17045b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_scan_phb);
1705c065488fSKumar Gala 
fixup_hide_host_resource_fsl(struct pci_dev * dev)1706c065488fSKumar Gala static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
1707c065488fSKumar Gala {
170809cc9006SMika Westerberg 	int class = dev->class >> 8;
17091fd02f66SJulia Lawall 	/* When configured as agent, programming interface = 1 */
171005737c7cSJason Jin 	int prog_if = dev->class & 0xf;
171109cc9006SMika Westerberg 	struct resource *r;
1712c065488fSKumar Gala 
1713c065488fSKumar Gala 	if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
1714c065488fSKumar Gala 	     class == PCI_CLASS_BRIDGE_OTHER) &&
1715c065488fSKumar Gala 		(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
171605737c7cSJason Jin 		(prog_if == 0) &&
1717c065488fSKumar Gala 		(dev->bus->parent == NULL)) {
171809cc9006SMika Westerberg 		pci_dev_for_each_resource(dev, r) {
171909cc9006SMika Westerberg 			r->start = 0;
172009cc9006SMika Westerberg 			r->end = 0;
172109cc9006SMika Westerberg 			r->flags = 0;
1722c065488fSKumar Gala 		}
1723c065488fSKumar Gala 	}
1724c065488fSKumar Gala }
1725c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1726c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
17275537fcb3SOliver O'Halloran 
17285537fcb3SOliver O'Halloran 
discover_phbs(void)17295537fcb3SOliver O'Halloran static int __init discover_phbs(void)
17305537fcb3SOliver O'Halloran {
17315537fcb3SOliver O'Halloran 	if (ppc_md.discover_phbs)
17325537fcb3SOliver O'Halloran 		ppc_md.discover_phbs();
17335537fcb3SOliver O'Halloran 
17345537fcb3SOliver O'Halloran 	return 0;
17355537fcb3SOliver O'Halloran }
17365537fcb3SOliver O'Halloran core_initcall(discover_phbs);
1737