xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision 44bda4b7d26e9fffed6d7152d98a2e9edaeb2a76)
15516b540SKumar Gala /*
25516b540SKumar Gala  * Contains common pci routines for ALL ppc platform
3cf1d8a8aSKumar Gala  * (based on pci_32.c and pci_64.c)
4cf1d8a8aSKumar Gala  *
5cf1d8a8aSKumar Gala  * Port for PPC64 David Engebretsen, IBM Corp.
6cf1d8a8aSKumar Gala  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
7cf1d8a8aSKumar Gala  *
8cf1d8a8aSKumar Gala  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
9cf1d8a8aSKumar Gala  *   Rework, based on alpha PCI code.
10cf1d8a8aSKumar Gala  *
11cf1d8a8aSKumar Gala  * Common pmac/prep/chrp pci routines. -- Cort
125516b540SKumar Gala  *
135516b540SKumar Gala  * This program is free software; you can redistribute it and/or
145516b540SKumar Gala  * modify it under the terms of the GNU General Public License
155516b540SKumar Gala  * as published by the Free Software Foundation; either version
165516b540SKumar Gala  * 2 of the License, or (at your option) any later version.
175516b540SKumar Gala  */
185516b540SKumar Gala 
195516b540SKumar Gala #include <linux/kernel.h>
205516b540SKumar Gala #include <linux/pci.h>
215516b540SKumar Gala #include <linux/string.h>
225516b540SKumar Gala #include <linux/init.h>
23d92a208dSGavin Shan #include <linux/delay.h>
2466b15db6SPaul Gortmaker #include <linux/export.h>
2522ae782fSGrant Likely #include <linux/of_address.h>
2604bea68bSSebastian Andrzej Siewior #include <linux/of_pci.h>
275516b540SKumar Gala #include <linux/mm.h>
283a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
295516b540SKumar Gala #include <linux/list.h>
305516b540SKumar Gala #include <linux/syscalls.h>
315516b540SKumar Gala #include <linux/irq.h>
325516b540SKumar Gala #include <linux/vmalloc.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
34c2e1d845SBrian King #include <linux/vgaarb.h>
355516b540SKumar Gala 
365516b540SKumar Gala #include <asm/processor.h>
375516b540SKumar Gala #include <asm/io.h>
385516b540SKumar Gala #include <asm/prom.h>
395516b540SKumar Gala #include <asm/pci-bridge.h>
405516b540SKumar Gala #include <asm/byteorder.h>
415516b540SKumar Gala #include <asm/machdep.h>
425516b540SKumar Gala #include <asm/ppc-pci.h>
438b8da358SBenjamin Herrenschmidt #include <asm/eeh.h>
445516b540SKumar Gala 
45*44bda4b7SHari Vyas #include "../../../drivers/pci/pci.h"
46*44bda4b7SHari Vyas 
4763a72284SGuilherme G. Piccoli /* hose_spinlock protects accesses to the the phb_bitmap. */
48a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
49c3bd517dSMilton Miller LIST_HEAD(hose_list);
50a4c9e328SKumar Gala 
5163a72284SGuilherme G. Piccoli /* For dynamic PHB numbering on get_phb_number(): max number of PHBs. */
5263a72284SGuilherme G. Piccoli #define MAX_PHBS 0x10000
5363a72284SGuilherme G. Piccoli 
5463a72284SGuilherme G. Piccoli /*
5563a72284SGuilherme G. Piccoli  * For dynamic PHB numbering: used/free PHBs tracking bitmap.
5663a72284SGuilherme G. Piccoli  * Accesses to this bitmap should be protected by hose_spinlock.
5763a72284SGuilherme G. Piccoli  */
5863a72284SGuilherme G. Piccoli static DECLARE_BITMAP(phb_bitmap, MAX_PHBS);
59a4c9e328SKumar Gala 
6025e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */
6125e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base;
629445aa1aSAl Viro EXPORT_SYMBOL(isa_mem_base);
6325e81f92SBenjamin Herrenschmidt 
64a4c9e328SKumar Gala 
652d9d6f6cSChristoph Hellwig static const struct dma_map_ops *pci_dma_ops = &dma_nommu_ops;
664fc665b8SBecky Bruce 
675299709dSBart Van Assche void set_pci_dma_ops(const struct dma_map_ops *dma_ops)
684fc665b8SBecky Bruce {
694fc665b8SBecky Bruce 	pci_dma_ops = dma_ops;
704fc665b8SBecky Bruce }
714fc665b8SBecky Bruce 
725299709dSBart Van Assche const struct dma_map_ops *get_pci_dma_ops(void)
734fc665b8SBecky Bruce {
744fc665b8SBecky Bruce 	return pci_dma_ops;
754fc665b8SBecky Bruce }
764fc665b8SBecky Bruce EXPORT_SYMBOL(get_pci_dma_ops);
774fc665b8SBecky Bruce 
7863a72284SGuilherme G. Piccoli /*
7963a72284SGuilherme G. Piccoli  * This function should run under locking protection, specifically
8063a72284SGuilherme G. Piccoli  * hose_spinlock.
8163a72284SGuilherme G. Piccoli  */
8263a72284SGuilherme G. Piccoli static int get_phb_number(struct device_node *dn)
8363a72284SGuilherme G. Piccoli {
8463a72284SGuilherme G. Piccoli 	int ret, phb_id = -1;
8561e8a0d5SMichael Ellerman 	u32 prop_32;
8663a72284SGuilherme G. Piccoli 	u64 prop;
8763a72284SGuilherme G. Piccoli 
8863a72284SGuilherme G. Piccoli 	/*
8963a72284SGuilherme G. Piccoli 	 * Try fixed PHB numbering first, by checking archs and reading
9063a72284SGuilherme G. Piccoli 	 * the respective device-tree properties. Firstly, try powernv by
9163a72284SGuilherme G. Piccoli 	 * reading "ibm,opal-phbid", only present in OPAL environment.
9263a72284SGuilherme G. Piccoli 	 */
9363a72284SGuilherme G. Piccoli 	ret = of_property_read_u64(dn, "ibm,opal-phbid", &prop);
9461e8a0d5SMichael Ellerman 	if (ret) {
9561e8a0d5SMichael Ellerman 		ret = of_property_read_u32_index(dn, "reg", 1, &prop_32);
9661e8a0d5SMichael Ellerman 		prop = prop_32;
9761e8a0d5SMichael Ellerman 	}
9863a72284SGuilherme G. Piccoli 
9963a72284SGuilherme G. Piccoli 	if (!ret)
10063a72284SGuilherme G. Piccoli 		phb_id = (int)(prop & (MAX_PHBS - 1));
10163a72284SGuilherme G. Piccoli 
10263a72284SGuilherme G. Piccoli 	/* We need to be sure to not use the same PHB number twice. */
10363a72284SGuilherme G. Piccoli 	if ((phb_id >= 0) && !test_and_set_bit(phb_id, phb_bitmap))
10463a72284SGuilherme G. Piccoli 		return phb_id;
10563a72284SGuilherme G. Piccoli 
10663a72284SGuilherme G. Piccoli 	/*
10763a72284SGuilherme G. Piccoli 	 * If not pseries nor powernv, or if fixed PHB numbering tried to add
10863a72284SGuilherme G. Piccoli 	 * the same PHB number twice, then fallback to dynamic PHB numbering.
10963a72284SGuilherme G. Piccoli 	 */
11063a72284SGuilherme G. Piccoli 	phb_id = find_first_zero_bit(phb_bitmap, MAX_PHBS);
11163a72284SGuilherme G. Piccoli 	BUG_ON(phb_id >= MAX_PHBS);
11263a72284SGuilherme G. Piccoli 	set_bit(phb_id, phb_bitmap);
11363a72284SGuilherme G. Piccoli 
11463a72284SGuilherme G. Piccoli 	return phb_id;
11563a72284SGuilherme G. Piccoli }
11663a72284SGuilherme G. Piccoli 
1172d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
118a4c9e328SKumar Gala {
119a4c9e328SKumar Gala 	struct pci_controller *phb;
120a4c9e328SKumar Gala 
121e60516e3SStephen Rothwell 	phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
122a4c9e328SKumar Gala 	if (phb == NULL)
123a4c9e328SKumar Gala 		return NULL;
124e60516e3SStephen Rothwell 	spin_lock(&hose_spinlock);
12563a72284SGuilherme G. Piccoli 	phb->global_number = get_phb_number(dev);
126e60516e3SStephen Rothwell 	list_add_tail(&phb->list_node, &hose_list);
127e60516e3SStephen Rothwell 	spin_unlock(&hose_spinlock);
12844ef3390SStephen Rothwell 	phb->dn = dev;
129f691fa10SMichael Ellerman 	phb->is_dynamic = slab_is_available();
130a4c9e328SKumar Gala #ifdef CONFIG_PPC64
131a4c9e328SKumar Gala 	if (dev) {
132a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
133a4c9e328SKumar Gala 
134a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
135a4c9e328SKumar Gala 			nid = -1;
136a4c9e328SKumar Gala 
137a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
138a4c9e328SKumar Gala 	}
139a4c9e328SKumar Gala #endif
140a4c9e328SKumar Gala 	return phb;
141a4c9e328SKumar Gala }
1425b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_alloc_controller);
143a4c9e328SKumar Gala 
144a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
145a4c9e328SKumar Gala {
146a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
14763a72284SGuilherme G. Piccoli 
14863a72284SGuilherme G. Piccoli 	/* Clear bit of phb_bitmap to allow reuse of this PHB number. */
14963a72284SGuilherme G. Piccoli 	if (phb->global_number < MAX_PHBS)
15063a72284SGuilherme G. Piccoli 		clear_bit(phb->global_number, phb_bitmap);
15163a72284SGuilherme G. Piccoli 
152a4c9e328SKumar Gala 	list_del(&phb->list_node);
153a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
154a4c9e328SKumar Gala 
155a4c9e328SKumar Gala 	if (phb->is_dynamic)
156a4c9e328SKumar Gala 		kfree(phb);
157a4c9e328SKumar Gala }
1586b8b252fSAndrew Donnellan EXPORT_SYMBOL_GPL(pcibios_free_controller);
159a4c9e328SKumar Gala 
1604c2245bbSGavin Shan /*
1612dd9c11bSMauricio Faria de Oliveira  * This function is used to call pcibios_free_controller()
1622dd9c11bSMauricio Faria de Oliveira  * in a deferred manner: a callback from the PCI subsystem.
1632dd9c11bSMauricio Faria de Oliveira  *
1642dd9c11bSMauricio Faria de Oliveira  * _*DO NOT*_ call pcibios_free_controller() explicitly if
1652dd9c11bSMauricio Faria de Oliveira  * this is used (or it may access an invalid *phb pointer).
1662dd9c11bSMauricio Faria de Oliveira  *
1672dd9c11bSMauricio Faria de Oliveira  * The callback occurs when all references to the root bus
1682dd9c11bSMauricio Faria de Oliveira  * are dropped (e.g., child buses/devices and their users).
1692dd9c11bSMauricio Faria de Oliveira  *
1702dd9c11bSMauricio Faria de Oliveira  * It's called as .release_fn() of 'struct pci_host_bridge'
1712dd9c11bSMauricio Faria de Oliveira  * which is associated with the 'struct pci_controller.bus'
1722dd9c11bSMauricio Faria de Oliveira  * (root bus) - it expects .release_data to hold a pointer
1732dd9c11bSMauricio Faria de Oliveira  * to 'struct pci_controller'.
1742dd9c11bSMauricio Faria de Oliveira  *
1752dd9c11bSMauricio Faria de Oliveira  * In order to use it, register .release_fn()/release_data
1762dd9c11bSMauricio Faria de Oliveira  * like this:
1772dd9c11bSMauricio Faria de Oliveira  *
1782dd9c11bSMauricio Faria de Oliveira  * pci_set_host_bridge_release(bridge,
1792dd9c11bSMauricio Faria de Oliveira  *                             pcibios_free_controller_deferred
1802dd9c11bSMauricio Faria de Oliveira  *                             (void *) phb);
1812dd9c11bSMauricio Faria de Oliveira  *
1822dd9c11bSMauricio Faria de Oliveira  * e.g. in the pcibios_root_bridge_prepare() callback from
1832dd9c11bSMauricio Faria de Oliveira  * pci_create_root_bus().
1842dd9c11bSMauricio Faria de Oliveira  */
1852dd9c11bSMauricio Faria de Oliveira void pcibios_free_controller_deferred(struct pci_host_bridge *bridge)
1862dd9c11bSMauricio Faria de Oliveira {
1872dd9c11bSMauricio Faria de Oliveira 	struct pci_controller *phb = (struct pci_controller *)
1882dd9c11bSMauricio Faria de Oliveira 					 bridge->release_data;
1892dd9c11bSMauricio Faria de Oliveira 
1902dd9c11bSMauricio Faria de Oliveira 	pr_debug("domain %d, dynamic %d\n", phb->global_number, phb->is_dynamic);
1912dd9c11bSMauricio Faria de Oliveira 
1922dd9c11bSMauricio Faria de Oliveira 	pcibios_free_controller(phb);
1932dd9c11bSMauricio Faria de Oliveira }
1942dd9c11bSMauricio Faria de Oliveira EXPORT_SYMBOL_GPL(pcibios_free_controller_deferred);
1952dd9c11bSMauricio Faria de Oliveira 
1962dd9c11bSMauricio Faria de Oliveira /*
1974c2245bbSGavin Shan  * The function is used to return the minimal alignment
1984c2245bbSGavin Shan  * for memory or I/O windows of the associated P2P bridge.
1994c2245bbSGavin Shan  * By default, 4KiB alignment for I/O windows and 1MiB for
2004c2245bbSGavin Shan  * memory windows.
2014c2245bbSGavin Shan  */
2024c2245bbSGavin Shan resource_size_t pcibios_window_alignment(struct pci_bus *bus,
2034c2245bbSGavin Shan 					 unsigned long type)
2044c2245bbSGavin Shan {
205467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(bus);
206467efc2eSDaniel Axtens 
207467efc2eSDaniel Axtens 	if (phb->controller_ops.window_alignment)
208467efc2eSDaniel Axtens 		return phb->controller_ops.window_alignment(bus, type);
209467efc2eSDaniel Axtens 
210467efc2eSDaniel Axtens 	/*
211467efc2eSDaniel Axtens 	 * PCI core will figure out the default
212467efc2eSDaniel Axtens 	 * alignment: 4KiB for I/O and 1MiB for
213467efc2eSDaniel Axtens 	 * memory window.
214467efc2eSDaniel Axtens 	 */
215467efc2eSDaniel Axtens 	return 1;
2164c2245bbSGavin Shan }
2174c2245bbSGavin Shan 
218c5fcb29aSGavin Shan void pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
219c5fcb29aSGavin Shan {
220c5fcb29aSGavin Shan 	struct pci_controller *hose = pci_bus_to_host(bus);
221c5fcb29aSGavin Shan 
222c5fcb29aSGavin Shan 	if (hose->controller_ops.setup_bridge)
223c5fcb29aSGavin Shan 		hose->controller_ops.setup_bridge(bus, type);
224c5fcb29aSGavin Shan }
225c5fcb29aSGavin Shan 
226d92a208dSGavin Shan void pcibios_reset_secondary_bus(struct pci_dev *dev)
227d92a208dSGavin Shan {
228467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
229467efc2eSDaniel Axtens 
230467efc2eSDaniel Axtens 	if (phb->controller_ops.reset_secondary_bus) {
231467efc2eSDaniel Axtens 		phb->controller_ops.reset_secondary_bus(dev);
232467efc2eSDaniel Axtens 		return;
233467efc2eSDaniel Axtens 	}
234467efc2eSDaniel Axtens 
235467efc2eSDaniel Axtens 	pci_reset_secondary_bus(dev);
236d92a208dSGavin Shan }
237d92a208dSGavin Shan 
23838274637SYongji Xie resource_size_t pcibios_default_alignment(void)
23938274637SYongji Xie {
24038274637SYongji Xie 	if (ppc_md.pcibios_default_alignment)
24138274637SYongji Xie 		return ppc_md.pcibios_default_alignment();
24238274637SYongji Xie 
24338274637SYongji Xie 	return 0;
24438274637SYongji Xie }
24538274637SYongji Xie 
2465350ab3fSWei Yang #ifdef CONFIG_PCI_IOV
2475350ab3fSWei Yang resource_size_t pcibios_iov_resource_alignment(struct pci_dev *pdev, int resno)
2485350ab3fSWei Yang {
2495350ab3fSWei Yang 	if (ppc_md.pcibios_iov_resource_alignment)
2505350ab3fSWei Yang 		return ppc_md.pcibios_iov_resource_alignment(pdev, resno);
2515350ab3fSWei Yang 
2525350ab3fSWei Yang 	return pci_iov_resource_size(pdev, resno);
2535350ab3fSWei Yang }
254988fc3baSBryant G. Ly 
255988fc3baSBryant G. Ly int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
256988fc3baSBryant G. Ly {
257988fc3baSBryant G. Ly 	if (ppc_md.pcibios_sriov_enable)
258988fc3baSBryant G. Ly 		return ppc_md.pcibios_sriov_enable(pdev, num_vfs);
259988fc3baSBryant G. Ly 
260988fc3baSBryant G. Ly 	return 0;
261988fc3baSBryant G. Ly }
262988fc3baSBryant G. Ly 
263988fc3baSBryant G. Ly int pcibios_sriov_disable(struct pci_dev *pdev)
264988fc3baSBryant G. Ly {
265988fc3baSBryant G. Ly 	if (ppc_md.pcibios_sriov_disable)
266988fc3baSBryant G. Ly 		return ppc_md.pcibios_sriov_disable(pdev);
267988fc3baSBryant G. Ly 
268988fc3baSBryant G. Ly 	return 0;
269988fc3baSBryant G. Ly }
270988fc3baSBryant G. Ly 
2715350ab3fSWei Yang #endif /* CONFIG_PCI_IOV */
2725350ab3fSWei Yang 
273988fc3baSBryant G. Ly void pcibios_bus_add_device(struct pci_dev *pdev)
274988fc3baSBryant G. Ly {
275988fc3baSBryant G. Ly 	if (ppc_md.pcibios_bus_add_device)
276988fc3baSBryant G. Ly 		ppc_md.pcibios_bus_add_device(pdev);
277988fc3baSBryant G. Ly }
278988fc3baSBryant G. Ly 
279c3bd517dSMilton Miller static resource_size_t pcibios_io_size(const struct pci_controller *hose)
280c3bd517dSMilton Miller {
281c3bd517dSMilton Miller #ifdef CONFIG_PPC64
282c3bd517dSMilton Miller 	return hose->pci_io_size;
283c3bd517dSMilton Miller #else
28428f65c11SJoe Perches 	return resource_size(&hose->io_resource);
285c3bd517dSMilton Miller #endif
286c3bd517dSMilton Miller }
287c3bd517dSMilton Miller 
2886dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
2896dfbde20SBenjamin Herrenschmidt {
2906dfbde20SBenjamin Herrenschmidt 	int ret = 0;
2916dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
292c3bd517dSMilton Miller 	resource_size_t size;
2936dfbde20SBenjamin Herrenschmidt 
2946dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
2956dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
296c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
2976dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
2986dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
2996dfbde20SBenjamin Herrenschmidt 			ret = 1;
3006dfbde20SBenjamin Herrenschmidt 			break;
3016dfbde20SBenjamin Herrenschmidt 		}
3026dfbde20SBenjamin Herrenschmidt 	}
3036dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
3046dfbde20SBenjamin Herrenschmidt 	return ret;
3056dfbde20SBenjamin Herrenschmidt }
3066dfbde20SBenjamin Herrenschmidt 
307c3bd517dSMilton Miller unsigned long pci_address_to_pio(phys_addr_t address)
308c3bd517dSMilton Miller {
309c3bd517dSMilton Miller 	struct pci_controller *hose;
310c3bd517dSMilton Miller 	resource_size_t size;
311c3bd517dSMilton Miller 	unsigned long ret = ~0;
312c3bd517dSMilton Miller 
313c3bd517dSMilton Miller 	spin_lock(&hose_spinlock);
314c3bd517dSMilton Miller 	list_for_each_entry(hose, &hose_list, list_node) {
315c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
316c3bd517dSMilton Miller 		if (address >= hose->io_base_phys &&
317c3bd517dSMilton Miller 		    address < (hose->io_base_phys + size)) {
318c3bd517dSMilton Miller 			unsigned long base =
319c3bd517dSMilton Miller 				(unsigned long)hose->io_base_virt - _IO_BASE;
320c3bd517dSMilton Miller 			ret = base + (address - hose->io_base_phys);
321c3bd517dSMilton Miller 			break;
322c3bd517dSMilton Miller 		}
323c3bd517dSMilton Miller 	}
324c3bd517dSMilton Miller 	spin_unlock(&hose_spinlock);
325c3bd517dSMilton Miller 
326c3bd517dSMilton Miller 	return ret;
327c3bd517dSMilton Miller }
328c3bd517dSMilton Miller EXPORT_SYMBOL_GPL(pci_address_to_pio);
329c3bd517dSMilton Miller 
3305516b540SKumar Gala /*
3315516b540SKumar Gala  * Return the domain number for this bus.
3325516b540SKumar Gala  */
3335516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
3345516b540SKumar Gala {
3355516b540SKumar Gala 	struct pci_controller *hose = pci_bus_to_host(bus);
3365516b540SKumar Gala 
3375516b540SKumar Gala 	return hose->global_number;
3385516b540SKumar Gala }
3395516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
34058083dadSKumar Gala 
341a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
342a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
343a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
344a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
345a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
346a4c9e328SKumar Gala  * config cycles.
347a4c9e328SKumar Gala  */
348a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
349a4c9e328SKumar Gala {
350a4c9e328SKumar Gala 	while(node) {
351a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
352a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
35344ef3390SStephen Rothwell 			if (hose->dn == node)
354a4c9e328SKumar Gala 				return hose;
355a4c9e328SKumar Gala 		node = node->parent;
356a4c9e328SKumar Gala 	}
357a4c9e328SKumar Gala 	return NULL;
358a4c9e328SKumar Gala }
359a4c9e328SKumar Gala 
36058083dadSKumar Gala /*
36158083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
36258083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
36358083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
36458083dadSKumar Gala  */
3654666ca2aSBenjamin Herrenschmidt static int pci_read_irq_line(struct pci_dev *pci_dev)
36658083dadSKumar Gala {
367c591c2e3SAlexey Kardashevskiy 	int virq;
36858083dadSKumar Gala 
369b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
37058083dadSKumar Gala 
37158083dadSKumar Gala #ifdef DEBUG
37258083dadSKumar Gala 	memset(&oirq, 0xff, sizeof(oirq));
37358083dadSKumar Gala #endif
37458083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
375c591c2e3SAlexey Kardashevskiy 	virq = of_irq_parse_and_map_pci(pci_dev, 0, 0);
376c591c2e3SAlexey Kardashevskiy 	if (virq <= 0) {
37758083dadSKumar Gala 		u8 line, pin;
37858083dadSKumar Gala 
37958083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
38058083dadSKumar Gala 		 * space and map that through the default controller. We
38158083dadSKumar Gala 		 * also set the type to level low since that's what PCI
38258083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
38358083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
38458083dadSKumar Gala 		 * function.
38558083dadSKumar Gala 		 */
38658083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
38758083dadSKumar Gala 			return -1;
38858083dadSKumar Gala 		if (pin == 0)
38958083dadSKumar Gala 			return -1;
39058083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
39154a24cbbSBenjamin Herrenschmidt 		    line == 0xff || line == 0) {
39258083dadSKumar Gala 			return -1;
39358083dadSKumar Gala 		}
394b0494bc8SBenjamin Herrenschmidt 		pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
39554a24cbbSBenjamin Herrenschmidt 			 line, pin);
39658083dadSKumar Gala 
39758083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
398ef24ba70SMichael Ellerman 		if (virq)
399ec775d0eSThomas Gleixner 			irq_set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
40058083dadSKumar Gala 	}
401ef24ba70SMichael Ellerman 
402ef24ba70SMichael Ellerman 	if (!virq) {
403b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Failed to map !\n");
40458083dadSKumar Gala 		return -1;
40558083dadSKumar Gala 	}
40658083dadSKumar Gala 
407b0494bc8SBenjamin Herrenschmidt 	pr_debug(" Mapped to linux irq %d\n", virq);
40858083dadSKumar Gala 
40958083dadSKumar Gala 	pci_dev->irq = virq;
41058083dadSKumar Gala 
41158083dadSKumar Gala 	return 0;
41258083dadSKumar Gala }
41358083dadSKumar Gala 
41458083dadSKumar Gala /*
41528f8f183SDavid Woodhouse  * Platform support for /proc/bus/pci/X/Y mmap()s.
41658083dadSKumar Gala  *  -- paulus.
41758083dadSKumar Gala  */
41828f8f183SDavid Woodhouse int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
41958083dadSKumar Gala {
42028f8f183SDavid Woodhouse 	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
42128f8f183SDavid Woodhouse 	resource_size_t ioaddr = pci_resource_start(pdev, bar);
42258083dadSKumar Gala 
42328f8f183SDavid Woodhouse 	if (!hose)
42428f8f183SDavid Woodhouse 		return -EINVAL;
42558083dadSKumar Gala 
42628f8f183SDavid Woodhouse 	/* Convert to an offset within this PCI controller */
42728f8f183SDavid Woodhouse 	ioaddr -= (unsigned long)hose->io_base_virt - _IO_BASE;
42858083dadSKumar Gala 
42928f8f183SDavid Woodhouse 	vma->vm_pgoff += (ioaddr + hose->io_base_phys) >> PAGE_SHIFT;
43028f8f183SDavid Woodhouse 	return 0;
43158083dadSKumar Gala }
43258083dadSKumar Gala 
43358083dadSKumar Gala /*
43458083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
43558083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
43658083dadSKumar Gala  * above routine
43758083dadSKumar Gala  */
43858083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
43958083dadSKumar Gala 				  unsigned long pfn,
44058083dadSKumar Gala 				  unsigned long size,
44164b3d0e8SBenjamin Herrenschmidt 				  pgprot_t prot)
44258083dadSKumar Gala {
44358083dadSKumar Gala 	struct pci_dev *pdev = NULL;
44458083dadSKumar Gala 	struct resource *found = NULL;
4457c12d906SBenjamin Herrenschmidt 	resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
44658083dadSKumar Gala 	int i;
44758083dadSKumar Gala 
44858083dadSKumar Gala 	if (page_is_ram(pfn))
44964b3d0e8SBenjamin Herrenschmidt 		return prot;
45058083dadSKumar Gala 
45164b3d0e8SBenjamin Herrenschmidt 	prot = pgprot_noncached(prot);
45258083dadSKumar Gala 	for_each_pci_dev(pdev) {
45358083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
45458083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
45558083dadSKumar Gala 			int flags = rp->flags;
45658083dadSKumar Gala 
45758083dadSKumar Gala 			/* Active and same type? */
45858083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
45958083dadSKumar Gala 				continue;
46058083dadSKumar Gala 			/* In the range of this resource? */
46158083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
46258083dadSKumar Gala 			    offset > rp->end)
46358083dadSKumar Gala 				continue;
46458083dadSKumar Gala 			found = rp;
46558083dadSKumar Gala 			break;
46658083dadSKumar Gala 		}
46758083dadSKumar Gala 		if (found)
46858083dadSKumar Gala 			break;
46958083dadSKumar Gala 	}
47058083dadSKumar Gala 	if (found) {
47158083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
47264b3d0e8SBenjamin Herrenschmidt 			prot = pgprot_noncached_wc(prot);
47358083dadSKumar Gala 		pci_dev_put(pdev);
47458083dadSKumar Gala 	}
47558083dadSKumar Gala 
476b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
47764b3d0e8SBenjamin Herrenschmidt 		 (unsigned long long)offset, pgprot_val(prot));
47858083dadSKumar Gala 
47964b3d0e8SBenjamin Herrenschmidt 	return prot;
48058083dadSKumar Gala }
48158083dadSKumar Gala 
482e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO read access on a bus */
483e9f82cb7SBenjamin Herrenschmidt int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
484e9f82cb7SBenjamin Herrenschmidt {
485e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
486e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
487e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
488e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
489e9f82cb7SBenjamin Herrenschmidt 
490e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
491e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
492e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
493e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
494e9f82cb7SBenjamin Herrenschmidt 	 */
495e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
496e9f82cb7SBenjamin Herrenschmidt 	offset += port;
497e9f82cb7SBenjamin Herrenschmidt 
498e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
499e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
500e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
501e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
502e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
503e9f82cb7SBenjamin Herrenschmidt 
504e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
505e9f82cb7SBenjamin Herrenschmidt 	case 1:
506e9f82cb7SBenjamin Herrenschmidt 		*((u8 *)val) = in_8(addr);
507e9f82cb7SBenjamin Herrenschmidt 		return 1;
508e9f82cb7SBenjamin Herrenschmidt 	case 2:
509e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
510e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
511e9f82cb7SBenjamin Herrenschmidt 		*((u16 *)val) = in_le16(addr);
512e9f82cb7SBenjamin Herrenschmidt 		return 2;
513e9f82cb7SBenjamin Herrenschmidt 	case 4:
514e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
515e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
516e9f82cb7SBenjamin Herrenschmidt 		*((u32 *)val) = in_le32(addr);
517e9f82cb7SBenjamin Herrenschmidt 		return 4;
518e9f82cb7SBenjamin Herrenschmidt 	}
519e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
520e9f82cb7SBenjamin Herrenschmidt }
521e9f82cb7SBenjamin Herrenschmidt 
522e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO write access on a bus */
523e9f82cb7SBenjamin Herrenschmidt int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
524e9f82cb7SBenjamin Herrenschmidt {
525e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
526e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
527e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
528e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
529e9f82cb7SBenjamin Herrenschmidt 
530e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
531e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
532e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
533e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
534e9f82cb7SBenjamin Herrenschmidt 	 */
535e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
536e9f82cb7SBenjamin Herrenschmidt 	offset += port;
537e9f82cb7SBenjamin Herrenschmidt 
538e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
539e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
540e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
541e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
542e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
543e9f82cb7SBenjamin Herrenschmidt 
544e9f82cb7SBenjamin Herrenschmidt 	/* WARNING: The generic code is idiotic. It gets passed a pointer
545e9f82cb7SBenjamin Herrenschmidt 	 * to what can be a 1, 2 or 4 byte quantity and always reads that
546e9f82cb7SBenjamin Herrenschmidt 	 * as a u32, which means that we have to correct the location of
547e9f82cb7SBenjamin Herrenschmidt 	 * the data read within those 32 bits for size 1 and 2
548e9f82cb7SBenjamin Herrenschmidt 	 */
549e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
550e9f82cb7SBenjamin Herrenschmidt 	case 1:
551e9f82cb7SBenjamin Herrenschmidt 		out_8(addr, val >> 24);
552e9f82cb7SBenjamin Herrenschmidt 		return 1;
553e9f82cb7SBenjamin Herrenschmidt 	case 2:
554e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
555e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
556e9f82cb7SBenjamin Herrenschmidt 		out_le16(addr, val >> 16);
557e9f82cb7SBenjamin Herrenschmidt 		return 2;
558e9f82cb7SBenjamin Herrenschmidt 	case 4:
559e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
560e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
561e9f82cb7SBenjamin Herrenschmidt 		out_le32(addr, val);
562e9f82cb7SBenjamin Herrenschmidt 		return 4;
563e9f82cb7SBenjamin Herrenschmidt 	}
564e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
565e9f82cb7SBenjamin Herrenschmidt }
566e9f82cb7SBenjamin Herrenschmidt 
567e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO or memory mmap access on a bus */
568e9f82cb7SBenjamin Herrenschmidt int pci_mmap_legacy_page_range(struct pci_bus *bus,
569e9f82cb7SBenjamin Herrenschmidt 			       struct vm_area_struct *vma,
570e9f82cb7SBenjamin Herrenschmidt 			       enum pci_mmap_state mmap_state)
571e9f82cb7SBenjamin Herrenschmidt {
572e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
573e9f82cb7SBenjamin Herrenschmidt 	resource_size_t offset =
574e9f82cb7SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
575e9f82cb7SBenjamin Herrenschmidt 	resource_size_t size = vma->vm_end - vma->vm_start;
576e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp;
577e9f82cb7SBenjamin Herrenschmidt 
578e9f82cb7SBenjamin Herrenschmidt 	pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
579e9f82cb7SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number,
580e9f82cb7SBenjamin Herrenschmidt 		 mmap_state == pci_mmap_mem ? "MEM" : "IO",
581e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)offset,
582e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)(offset + size - 1));
583e9f82cb7SBenjamin Herrenschmidt 
584e9f82cb7SBenjamin Herrenschmidt 	if (mmap_state == pci_mmap_mem) {
5855b11abfdSBenjamin Herrenschmidt 		/* Hack alert !
5865b11abfdSBenjamin Herrenschmidt 		 *
5875b11abfdSBenjamin Herrenschmidt 		 * Because X is lame and can fail starting if it gets an error trying
5885b11abfdSBenjamin Herrenschmidt 		 * to mmap legacy_mem (instead of just moving on without legacy memory
5895b11abfdSBenjamin Herrenschmidt 		 * access) we fake it here by giving it anonymous memory, effectively
5905b11abfdSBenjamin Herrenschmidt 		 * behaving just like /dev/zero
5915b11abfdSBenjamin Herrenschmidt 		 */
5925b11abfdSBenjamin Herrenschmidt 		if ((offset + size) > hose->isa_mem_size) {
5935b11abfdSBenjamin Herrenschmidt 			printk(KERN_DEBUG
5945b11abfdSBenjamin Herrenschmidt 			       "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
5955b11abfdSBenjamin Herrenschmidt 			       current->comm, current->pid, pci_domain_nr(bus), bus->number);
5965b11abfdSBenjamin Herrenschmidt 			if (vma->vm_flags & VM_SHARED)
5975b11abfdSBenjamin Herrenschmidt 				return shmem_zero_setup(vma);
5985b11abfdSBenjamin Herrenschmidt 			return 0;
5995b11abfdSBenjamin Herrenschmidt 		}
600e9f82cb7SBenjamin Herrenschmidt 		offset += hose->isa_mem_phys;
601e9f82cb7SBenjamin Herrenschmidt 	} else {
602e9f82cb7SBenjamin Herrenschmidt 		unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
603e9f82cb7SBenjamin Herrenschmidt 		unsigned long roffset = offset + io_offset;
604e9f82cb7SBenjamin Herrenschmidt 		rp = &hose->io_resource;
605e9f82cb7SBenjamin Herrenschmidt 		if (!(rp->flags & IORESOURCE_IO))
606e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
607e9f82cb7SBenjamin Herrenschmidt 		if (roffset < rp->start || (roffset + size) > rp->end)
608e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
609e9f82cb7SBenjamin Herrenschmidt 		offset += hose->io_base_phys;
610e9f82cb7SBenjamin Herrenschmidt 	}
611e9f82cb7SBenjamin Herrenschmidt 	pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
612e9f82cb7SBenjamin Herrenschmidt 
613e9f82cb7SBenjamin Herrenschmidt 	vma->vm_pgoff = offset >> PAGE_SHIFT;
61464b3d0e8SBenjamin Herrenschmidt 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
615e9f82cb7SBenjamin Herrenschmidt 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
616e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_end - vma->vm_start,
617e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_page_prot);
618e9f82cb7SBenjamin Herrenschmidt }
619e9f82cb7SBenjamin Herrenschmidt 
62058083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
62158083dadSKumar Gala 			  const struct resource *rsrc,
62258083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
62358083dadSKumar Gala {
62438301358SBjorn Helgaas 	struct pci_bus_region region;
62558083dadSKumar Gala 
62638301358SBjorn Helgaas 	if (rsrc->flags & IORESOURCE_IO) {
62738301358SBjorn Helgaas 		pcibios_resource_to_bus(dev->bus, &region,
62838301358SBjorn Helgaas 					(struct resource *) rsrc);
62938301358SBjorn Helgaas 		*start = region.start;
63038301358SBjorn Helgaas 		*end = region.end;
63158083dadSKumar Gala 		return;
63238301358SBjorn Helgaas 	}
63358083dadSKumar Gala 
63438301358SBjorn Helgaas 	/* We pass a CPU physical address to userland for MMIO instead of a
63538301358SBjorn Helgaas 	 * BAR value because X is lame and expects to be able to use that
63658083dadSKumar Gala 	 * to pass to /dev/mem!
63758083dadSKumar Gala 	 *
63838301358SBjorn Helgaas 	 * That means we may have 64-bit values where some apps only expect
63938301358SBjorn Helgaas 	 * 32 (like X itself since it thinks only Sparc has 64-bit MMIO).
64058083dadSKumar Gala 	 */
64138301358SBjorn Helgaas 	*start = rsrc->start;
64238301358SBjorn Helgaas 	*end = rsrc->end;
64358083dadSKumar Gala }
64413dccb9eSBenjamin Herrenschmidt 
64513dccb9eSBenjamin Herrenschmidt /**
64613dccb9eSBenjamin Herrenschmidt  * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
64713dccb9eSBenjamin Herrenschmidt  * @hose: newly allocated pci_controller to be setup
64813dccb9eSBenjamin Herrenschmidt  * @dev: device node of the host bridge
64913dccb9eSBenjamin Herrenschmidt  * @primary: set if primary bus (32 bits only, soon to be deprecated)
65013dccb9eSBenjamin Herrenschmidt  *
65113dccb9eSBenjamin Herrenschmidt  * This function will parse the "ranges" property of a PCI host bridge device
65213dccb9eSBenjamin Herrenschmidt  * node and setup the resource mapping of a pci controller based on its
65313dccb9eSBenjamin Herrenschmidt  * content.
65413dccb9eSBenjamin Herrenschmidt  *
65513dccb9eSBenjamin Herrenschmidt  * Life would be boring if it wasn't for a few issues that we have to deal
65613dccb9eSBenjamin Herrenschmidt  * with here:
65713dccb9eSBenjamin Herrenschmidt  *
65813dccb9eSBenjamin Herrenschmidt  *   - We can only cope with one IO space range and up to 3 Memory space
65913dccb9eSBenjamin Herrenschmidt  *     ranges. However, some machines (thanks Apple !) tend to split their
66013dccb9eSBenjamin Herrenschmidt  *     space into lots of small contiguous ranges. So we have to coalesce.
66113dccb9eSBenjamin Herrenschmidt  *
66213dccb9eSBenjamin Herrenschmidt  *   - Some busses have IO space not starting at 0, which causes trouble with
66313dccb9eSBenjamin Herrenschmidt  *     the way we do our IO resource renumbering. The code somewhat deals with
66413dccb9eSBenjamin Herrenschmidt  *     it for 64 bits but I would expect problems on 32 bits.
66513dccb9eSBenjamin Herrenschmidt  *
66613dccb9eSBenjamin Herrenschmidt  *   - Some 32 bits platforms such as 4xx can have physical space larger than
66713dccb9eSBenjamin Herrenschmidt  *     32 bits so we need to use 64 bits values for the parsing
66813dccb9eSBenjamin Herrenschmidt  */
669cad5cef6SGreg Kroah-Hartman void pci_process_bridge_OF_ranges(struct pci_controller *hose,
670cad5cef6SGreg Kroah-Hartman 				  struct device_node *dev, int primary)
67113dccb9eSBenjamin Herrenschmidt {
672858957abSKevin Hao 	int memno = 0;
67313dccb9eSBenjamin Herrenschmidt 	struct resource *res;
674654837e8SAndrew Murray 	struct of_pci_range range;
675654837e8SAndrew Murray 	struct of_pci_range_parser parser;
67613dccb9eSBenjamin Herrenschmidt 
677b7c670d6SRob Herring 	printk(KERN_INFO "PCI host bridge %pOF %s ranges:\n",
678b7c670d6SRob Herring 	       dev, primary ? "(primary)" : "");
67913dccb9eSBenjamin Herrenschmidt 
680654837e8SAndrew Murray 	/* Check for ranges property */
681654837e8SAndrew Murray 	if (of_pci_range_parser_init(&parser, dev))
68213dccb9eSBenjamin Herrenschmidt 		return;
68313dccb9eSBenjamin Herrenschmidt 
68413dccb9eSBenjamin Herrenschmidt 	/* Parse it */
685654837e8SAndrew Murray 	for_each_of_pci_range(&parser, &range) {
686e9f82cb7SBenjamin Herrenschmidt 		/* If we failed translation or got a zero-sized region
687e9f82cb7SBenjamin Herrenschmidt 		 * (some FW try to feed us with non sensical zero sized regions
688e9f82cb7SBenjamin Herrenschmidt 		 * such as power3 which look like some kind of attempt at exposing
689e9f82cb7SBenjamin Herrenschmidt 		 * the VGA memory hole)
690e9f82cb7SBenjamin Herrenschmidt 		 */
691654837e8SAndrew Murray 		if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
69213dccb9eSBenjamin Herrenschmidt 			continue;
69313dccb9eSBenjamin Herrenschmidt 
69413dccb9eSBenjamin Herrenschmidt 		/* Act based on address space type */
69513dccb9eSBenjamin Herrenschmidt 		res = NULL;
696654837e8SAndrew Murray 		switch (range.flags & IORESOURCE_TYPE_BITS) {
697654837e8SAndrew Murray 		case IORESOURCE_IO:
69813dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
69913dccb9eSBenjamin Herrenschmidt 			       "  IO 0x%016llx..0x%016llx -> 0x%016llx\n",
700654837e8SAndrew Murray 			       range.cpu_addr, range.cpu_addr + range.size - 1,
701654837e8SAndrew Murray 			       range.pci_addr);
70213dccb9eSBenjamin Herrenschmidt 
70313dccb9eSBenjamin Herrenschmidt 			/* We support only one IO range */
70413dccb9eSBenjamin Herrenschmidt 			if (hose->pci_io_size) {
70513dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
70613dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
70713dccb9eSBenjamin Herrenschmidt 				continue;
70813dccb9eSBenjamin Herrenschmidt 			}
70913dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32
71013dccb9eSBenjamin Herrenschmidt 			/* On 32 bits, limit I/O space to 16MB */
711654837e8SAndrew Murray 			if (range.size > 0x01000000)
712654837e8SAndrew Murray 				range.size = 0x01000000;
71313dccb9eSBenjamin Herrenschmidt 
71413dccb9eSBenjamin Herrenschmidt 			/* 32 bits needs to map IOs here */
715654837e8SAndrew Murray 			hose->io_base_virt = ioremap(range.cpu_addr,
716654837e8SAndrew Murray 						range.size);
71713dccb9eSBenjamin Herrenschmidt 
71813dccb9eSBenjamin Herrenschmidt 			/* Expect trouble if pci_addr is not 0 */
71913dccb9eSBenjamin Herrenschmidt 			if (primary)
72013dccb9eSBenjamin Herrenschmidt 				isa_io_base =
72113dccb9eSBenjamin Herrenschmidt 					(unsigned long)hose->io_base_virt;
72213dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
72313dccb9eSBenjamin Herrenschmidt 			/* pci_io_size and io_base_phys always represent IO
72413dccb9eSBenjamin Herrenschmidt 			 * space starting at 0 so we factor in pci_addr
72513dccb9eSBenjamin Herrenschmidt 			 */
726654837e8SAndrew Murray 			hose->pci_io_size = range.pci_addr + range.size;
727654837e8SAndrew Murray 			hose->io_base_phys = range.cpu_addr - range.pci_addr;
72813dccb9eSBenjamin Herrenschmidt 
72913dccb9eSBenjamin Herrenschmidt 			/* Build resource */
73013dccb9eSBenjamin Herrenschmidt 			res = &hose->io_resource;
731654837e8SAndrew Murray 			range.cpu_addr = range.pci_addr;
73213dccb9eSBenjamin Herrenschmidt 			break;
733654837e8SAndrew Murray 		case IORESOURCE_MEM:
73413dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
73513dccb9eSBenjamin Herrenschmidt 			       " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
736654837e8SAndrew Murray 			       range.cpu_addr, range.cpu_addr + range.size - 1,
737654837e8SAndrew Murray 			       range.pci_addr,
738654837e8SAndrew Murray 			       (range.pci_space & 0x40000000) ?
739654837e8SAndrew Murray 			       "Prefetch" : "");
74013dccb9eSBenjamin Herrenschmidt 
74113dccb9eSBenjamin Herrenschmidt 			/* We support only 3 memory ranges */
74213dccb9eSBenjamin Herrenschmidt 			if (memno >= 3) {
74313dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
74413dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
74513dccb9eSBenjamin Herrenschmidt 				continue;
74613dccb9eSBenjamin Herrenschmidt 			}
74713dccb9eSBenjamin Herrenschmidt 			/* Handles ISA memory hole space here */
748654837e8SAndrew Murray 			if (range.pci_addr == 0) {
74913dccb9eSBenjamin Herrenschmidt 				if (primary || isa_mem_base == 0)
750654837e8SAndrew Murray 					isa_mem_base = range.cpu_addr;
751654837e8SAndrew Murray 				hose->isa_mem_phys = range.cpu_addr;
752654837e8SAndrew Murray 				hose->isa_mem_size = range.size;
75313dccb9eSBenjamin Herrenschmidt 			}
75413dccb9eSBenjamin Herrenschmidt 
75513dccb9eSBenjamin Herrenschmidt 			/* Build resource */
756654837e8SAndrew Murray 			hose->mem_offset[memno] = range.cpu_addr -
757654837e8SAndrew Murray 							range.pci_addr;
75813dccb9eSBenjamin Herrenschmidt 			res = &hose->mem_resources[memno++];
75913dccb9eSBenjamin Herrenschmidt 			break;
76013dccb9eSBenjamin Herrenschmidt 		}
76113dccb9eSBenjamin Herrenschmidt 		if (res != NULL) {
762aeba3731SMichael Ellerman 			res->name = dev->full_name;
763aeba3731SMichael Ellerman 			res->flags = range.flags;
764aeba3731SMichael Ellerman 			res->start = range.cpu_addr;
765aeba3731SMichael Ellerman 			res->end = range.cpu_addr + range.size - 1;
766aeba3731SMichael Ellerman 			res->parent = res->child = res->sibling = NULL;
76713dccb9eSBenjamin Herrenschmidt 		}
76813dccb9eSBenjamin Herrenschmidt 	}
76913dccb9eSBenjamin Herrenschmidt }
770fa462f2dSBenjamin Herrenschmidt 
771fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */
772fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus)
773fa462f2dSBenjamin Herrenschmidt {
774fa462f2dSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
7751fd0f525SBenjamin Herrenschmidt 
7760e47ff1cSRob Herring 	if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
777fa462f2dSBenjamin Herrenschmidt 		return 0;
7780e47ff1cSRob Herring 	if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
779fa462f2dSBenjamin Herrenschmidt 		return hose->global_number != 0;
780fa462f2dSBenjamin Herrenschmidt 	return 1;
781fa462f2dSBenjamin Herrenschmidt }
782fa462f2dSBenjamin Herrenschmidt 
783d82fb31aSKleber Sacilotto de Souza int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
784d82fb31aSKleber Sacilotto de Souza {
785d82fb31aSKleber Sacilotto de Souza 	if (ppc_md.pcibios_root_bridge_prepare)
786d82fb31aSKleber Sacilotto de Souza 		return ppc_md.pcibios_root_bridge_prepare(bridge);
787d82fb31aSKleber Sacilotto de Souza 
788d82fb31aSKleber Sacilotto de Souza 	return 0;
789d82fb31aSKleber Sacilotto de Souza }
790d82fb31aSKleber Sacilotto de Souza 
791bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are
792bf5e2ba2SBenjamin Herrenschmidt  * probed, but not for bridge ranges
793bf5e2ba2SBenjamin Herrenschmidt  */
794cad5cef6SGreg Kroah-Hartman static void pcibios_fixup_resources(struct pci_dev *dev)
795bf5e2ba2SBenjamin Herrenschmidt {
796bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
797bf5e2ba2SBenjamin Herrenschmidt 	int i;
798bf5e2ba2SBenjamin Herrenschmidt 
799bf5e2ba2SBenjamin Herrenschmidt 	if (!hose) {
800bf5e2ba2SBenjamin Herrenschmidt 		printk(KERN_ERR "No host bridge for PCI dev %s !\n",
801bf5e2ba2SBenjamin Herrenschmidt 		       pci_name(dev));
802bf5e2ba2SBenjamin Herrenschmidt 		return;
803bf5e2ba2SBenjamin Herrenschmidt 	}
804c3b80fb0SWei Yang 
805c3b80fb0SWei Yang 	if (dev->is_virtfn)
806c3b80fb0SWei Yang 		return;
807c3b80fb0SWei Yang 
808bf5e2ba2SBenjamin Herrenschmidt 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
809bf5e2ba2SBenjamin Herrenschmidt 		struct resource *res = dev->resource + i;
810c5df457fSKevin Hao 		struct pci_bus_region reg;
811bf5e2ba2SBenjamin Herrenschmidt 		if (!res->flags)
812bf5e2ba2SBenjamin Herrenschmidt 			continue;
81348c2ce97SBenjamin Herrenschmidt 
81448c2ce97SBenjamin Herrenschmidt 		/* If we're going to re-assign everything, we mark all resources
81548c2ce97SBenjamin Herrenschmidt 		 * as unset (and 0-base them). In addition, we mark BARs starting
81648c2ce97SBenjamin Herrenschmidt 		 * at 0 as unset as well, except if PCI_PROBE_ONLY is also set
81748c2ce97SBenjamin Herrenschmidt 		 * since in that case, we don't want to re-assign anything
8187f172890SBenjamin Herrenschmidt 		 */
819fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &reg, res);
82048c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC) ||
821c5df457fSKevin Hao 		    (reg.start == 0 && !pci_has_flag(PCI_PROBE_ONLY))) {
82248c2ce97SBenjamin Herrenschmidt 			/* Only print message if not re-assigning */
82348c2ce97SBenjamin Herrenschmidt 			if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC))
824ae2a84b4SKevin Hao 				pr_debug("PCI:%s Resource %d %pR is unassigned\n",
825ae2a84b4SKevin Hao 					 pci_name(dev), i, res);
826bf5e2ba2SBenjamin Herrenschmidt 			res->end -= res->start;
827bf5e2ba2SBenjamin Herrenschmidt 			res->start = 0;
828bf5e2ba2SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
829bf5e2ba2SBenjamin Herrenschmidt 			continue;
830bf5e2ba2SBenjamin Herrenschmidt 		}
831bf5e2ba2SBenjamin Herrenschmidt 
832ae2a84b4SKevin Hao 		pr_debug("PCI:%s Resource %d %pR\n", pci_name(dev), i, res);
833bf5e2ba2SBenjamin Herrenschmidt 	}
834bf5e2ba2SBenjamin Herrenschmidt 
835bf5e2ba2SBenjamin Herrenschmidt 	/* Call machine specific resource fixup */
836bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_resources)
837bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_resources(dev);
838bf5e2ba2SBenjamin Herrenschmidt }
839bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
840bf5e2ba2SBenjamin Herrenschmidt 
841b5561511SBenjamin Herrenschmidt /* This function tries to figure out if a bridge resource has been initialized
842b5561511SBenjamin Herrenschmidt  * by the firmware or not. It doesn't have to be absolutely bullet proof, but
843b5561511SBenjamin Herrenschmidt  * things go more smoothly when it gets it right. It should covers cases such
844b5561511SBenjamin Herrenschmidt  * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
845b5561511SBenjamin Herrenschmidt  */
846cad5cef6SGreg Kroah-Hartman static int pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
847b5561511SBenjamin Herrenschmidt 						 struct resource *res)
848bf5e2ba2SBenjamin Herrenschmidt {
849be8cbcd8SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
850bf5e2ba2SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
851b5561511SBenjamin Herrenschmidt 	resource_size_t offset;
8523fd47f06SBenjamin Herrenschmidt 	struct pci_bus_region region;
853b5561511SBenjamin Herrenschmidt 	u16 command;
854b5561511SBenjamin Herrenschmidt 	int i;
855bf5e2ba2SBenjamin Herrenschmidt 
856b5561511SBenjamin Herrenschmidt 	/* We don't do anything if PCI_PROBE_ONLY is set */
8570e47ff1cSRob Herring 	if (pci_has_flag(PCI_PROBE_ONLY))
858b5561511SBenjamin Herrenschmidt 		return 0;
859bf5e2ba2SBenjamin Herrenschmidt 
860b5561511SBenjamin Herrenschmidt 	/* Job is a bit different between memory and IO */
861b5561511SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_MEM) {
862fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, res);
8633fd47f06SBenjamin Herrenschmidt 
8643fd47f06SBenjamin Herrenschmidt 		/* If the BAR is non-0 then it's probably been initialized */
8653fd47f06SBenjamin Herrenschmidt 		if (region.start != 0)
866b5561511SBenjamin Herrenschmidt 			return 0;
867b5561511SBenjamin Herrenschmidt 
868b5561511SBenjamin Herrenschmidt 		/* The BAR is 0, let's check if memory decoding is enabled on
869b5561511SBenjamin Herrenschmidt 		 * the bridge. If not, we consider it unassigned
870b5561511SBenjamin Herrenschmidt 		 */
871b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
872b5561511SBenjamin Herrenschmidt 		if ((command & PCI_COMMAND_MEMORY) == 0)
873b5561511SBenjamin Herrenschmidt 			return 1;
874b5561511SBenjamin Herrenschmidt 
875b5561511SBenjamin Herrenschmidt 		/* Memory decoding is enabled and the BAR is 0. If any of the bridge
876b5561511SBenjamin Herrenschmidt 		 * resources covers that starting address (0 then it's good enough for
8773fd47f06SBenjamin Herrenschmidt 		 * us for memory space)
878b5561511SBenjamin Herrenschmidt 		 */
879b5561511SBenjamin Herrenschmidt 		for (i = 0; i < 3; i++) {
880b5561511SBenjamin Herrenschmidt 			if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
8813fd47f06SBenjamin Herrenschmidt 			    hose->mem_resources[i].start == hose->mem_offset[i])
882b5561511SBenjamin Herrenschmidt 				return 0;
883b5561511SBenjamin Herrenschmidt 		}
884b5561511SBenjamin Herrenschmidt 
885b5561511SBenjamin Herrenschmidt 		/* Well, it starts at 0 and we know it will collide so we may as
886b5561511SBenjamin Herrenschmidt 		 * well consider it as unassigned. That covers the Apple case.
887b5561511SBenjamin Herrenschmidt 		 */
888b5561511SBenjamin Herrenschmidt 		return 1;
889b5561511SBenjamin Herrenschmidt 	} else {
890b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0, then we consider it assigned */
891b5561511SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
892b5561511SBenjamin Herrenschmidt 		if (((res->start - offset) & 0xfffffffful) != 0)
893b5561511SBenjamin Herrenschmidt 			return 0;
894b5561511SBenjamin Herrenschmidt 
895b5561511SBenjamin Herrenschmidt 		/* Here, we are a bit different than memory as typically IO space
896b5561511SBenjamin Herrenschmidt 		 * starting at low addresses -is- valid. What we do instead if that
897b5561511SBenjamin Herrenschmidt 		 * we consider as unassigned anything that doesn't have IO enabled
898b5561511SBenjamin Herrenschmidt 		 * in the PCI command register, and that's it.
899b5561511SBenjamin Herrenschmidt 		 */
900b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
901b5561511SBenjamin Herrenschmidt 		if (command & PCI_COMMAND_IO)
902b5561511SBenjamin Herrenschmidt 			return 0;
903b5561511SBenjamin Herrenschmidt 
904b5561511SBenjamin Herrenschmidt 		/* It's starting at 0 and IO is disabled in the bridge, consider
905b5561511SBenjamin Herrenschmidt 		 * it unassigned
906b5561511SBenjamin Herrenschmidt 		 */
907b5561511SBenjamin Herrenschmidt 		return 1;
908b5561511SBenjamin Herrenschmidt 	}
909b5561511SBenjamin Herrenschmidt }
910b5561511SBenjamin Herrenschmidt 
911b5561511SBenjamin Herrenschmidt /* Fixup resources of a PCI<->PCI bridge */
912cad5cef6SGreg Kroah-Hartman static void pcibios_fixup_bridge(struct pci_bus *bus)
913b5561511SBenjamin Herrenschmidt {
914bf5e2ba2SBenjamin Herrenschmidt 	struct resource *res;
915bf5e2ba2SBenjamin Herrenschmidt 	int i;
916bf5e2ba2SBenjamin Herrenschmidt 
917b5561511SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
918b5561511SBenjamin Herrenschmidt 
91989a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
92089a74eccSBjorn Helgaas 		if (!res || !res->flags)
921bf5e2ba2SBenjamin Herrenschmidt 			continue;
922b188b2aeSKumar Gala 		if (i >= 3 && bus->self->transparent)
923b188b2aeSKumar Gala 			continue;
924be8cbcd8SBenjamin Herrenschmidt 
925cf1a4cf8SGavin Shan 		/* If we're going to reassign everything, we can
926cf1a4cf8SGavin Shan 		 * shrink the P2P resource to have size as being
927cf1a4cf8SGavin Shan 		 * of 0 in order to save space.
92848c2ce97SBenjamin Herrenschmidt 		 */
92948c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
93048c2ce97SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
93148c2ce97SBenjamin Herrenschmidt 			res->start = 0;
932cf1a4cf8SGavin Shan 			res->end = -1;
93348c2ce97SBenjamin Herrenschmidt 			continue;
93448c2ce97SBenjamin Herrenschmidt 		}
93548c2ce97SBenjamin Herrenschmidt 
936ae2a84b4SKevin Hao 		pr_debug("PCI:%s Bus rsrc %d %pR\n", pci_name(dev), i, res);
937bf5e2ba2SBenjamin Herrenschmidt 
938b5561511SBenjamin Herrenschmidt 		/* Try to detect uninitialized P2P bridge resources,
939b5561511SBenjamin Herrenschmidt 		 * and clear them out so they get re-assigned later
940b5561511SBenjamin Herrenschmidt 		 */
941b5561511SBenjamin Herrenschmidt 		if (pcibios_uninitialized_bridge_resource(bus, res)) {
942b5561511SBenjamin Herrenschmidt 			res->flags = 0;
943b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            (unassigned)\n", pci_name(dev));
944bf5e2ba2SBenjamin Herrenschmidt 		}
945bf5e2ba2SBenjamin Herrenschmidt 	}
946b5561511SBenjamin Herrenschmidt }
947b5561511SBenjamin Herrenschmidt 
948cad5cef6SGreg Kroah-Hartman void pcibios_setup_bus_self(struct pci_bus *bus)
9498b8da358SBenjamin Herrenschmidt {
950467efc2eSDaniel Axtens 	struct pci_controller *phb;
951467efc2eSDaniel Axtens 
9527eef440aSBenjamin Herrenschmidt 	/* Fix up the bus resources for P2P bridges */
9538b8da358SBenjamin Herrenschmidt 	if (bus->self != NULL)
9548b8da358SBenjamin Herrenschmidt 		pcibios_fixup_bridge(bus);
9558b8da358SBenjamin Herrenschmidt 
9568b8da358SBenjamin Herrenschmidt 	/* Platform specific bus fixups. This is currently only used
9577eef440aSBenjamin Herrenschmidt 	 * by fsl_pci and I'm hoping to get rid of it at some point
9588b8da358SBenjamin Herrenschmidt 	 */
9598b8da358SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_bus)
9608b8da358SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_bus(bus);
9618b8da358SBenjamin Herrenschmidt 
9628b8da358SBenjamin Herrenschmidt 	/* Setup bus DMA mappings */
963467efc2eSDaniel Axtens 	phb = pci_bus_to_host(bus);
964467efc2eSDaniel Axtens 	if (phb->controller_ops.dma_bus_setup)
965467efc2eSDaniel Axtens 		phb->controller_ops.dma_bus_setup(bus);
9668b8da358SBenjamin Herrenschmidt }
9678b8da358SBenjamin Herrenschmidt 
9687846de40SGuenter Roeck static void pcibios_setup_device(struct pci_dev *dev)
9697eef440aSBenjamin Herrenschmidt {
970467efc2eSDaniel Axtens 	struct pci_controller *phb;
9717eef440aSBenjamin Herrenschmidt 	/* Fixup NUMA node as it may not be setup yet by the generic
9727eef440aSBenjamin Herrenschmidt 	 * code and is needed by the DMA init
9737eef440aSBenjamin Herrenschmidt 	 */
9747eef440aSBenjamin Herrenschmidt 	set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
9757eef440aSBenjamin Herrenschmidt 
9767eef440aSBenjamin Herrenschmidt 	/* Hook up default DMA ops */
977bc0df9ecSNishanth Aravamudan 	set_dma_ops(&dev->dev, pci_dma_ops);
978738ef42eSBecky Bruce 	set_dma_offset(&dev->dev, PCI_DRAM_OFFSET);
9797eef440aSBenjamin Herrenschmidt 
9807eef440aSBenjamin Herrenschmidt 	/* Additional platform DMA/iommu setup */
981467efc2eSDaniel Axtens 	phb = pci_bus_to_host(dev->bus);
982467efc2eSDaniel Axtens 	if (phb->controller_ops.dma_dev_setup)
983467efc2eSDaniel Axtens 		phb->controller_ops.dma_dev_setup(dev);
9847eef440aSBenjamin Herrenschmidt 
9857eef440aSBenjamin Herrenschmidt 	/* Read default IRQs and fixup if necessary */
9867eef440aSBenjamin Herrenschmidt 	pci_read_irq_line(dev);
9877eef440aSBenjamin Herrenschmidt 	if (ppc_md.pci_irq_fixup)
9887eef440aSBenjamin Herrenschmidt 		ppc_md.pci_irq_fixup(dev);
9897eef440aSBenjamin Herrenschmidt }
99037f02195SYuanquan Chen 
9917846de40SGuenter Roeck int pcibios_add_device(struct pci_dev *dev)
9927846de40SGuenter Roeck {
9937846de40SGuenter Roeck 	/*
9947846de40SGuenter Roeck 	 * We can only call pcibios_setup_device() after bus setup is complete,
9957846de40SGuenter Roeck 	 * since some of the platform specific DMA setup code depends on it.
9967846de40SGuenter Roeck 	 */
9977846de40SGuenter Roeck 	if (dev->bus->is_added)
9987846de40SGuenter Roeck 		pcibios_setup_device(dev);
9996e628c7dSWei Yang 
10006e628c7dSWei Yang #ifdef CONFIG_PCI_IOV
10016e628c7dSWei Yang 	if (ppc_md.pcibios_fixup_sriov)
10026e628c7dSWei Yang 		ppc_md.pcibios_fixup_sriov(dev);
10036e628c7dSWei Yang #endif /* CONFIG_PCI_IOV */
10046e628c7dSWei Yang 
10057846de40SGuenter Roeck 	return 0;
10067846de40SGuenter Roeck }
10077846de40SGuenter Roeck 
100837f02195SYuanquan Chen void pcibios_setup_bus_devices(struct pci_bus *bus)
100937f02195SYuanquan Chen {
101037f02195SYuanquan Chen 	struct pci_dev *dev;
101137f02195SYuanquan Chen 
101237f02195SYuanquan Chen 	pr_debug("PCI: Fixup bus devices %d (%s)\n",
101337f02195SYuanquan Chen 		 bus->number, bus->self ? pci_name(bus->self) : "PHB");
101437f02195SYuanquan Chen 
101537f02195SYuanquan Chen 	list_for_each_entry(dev, &bus->devices, bus_list) {
101637f02195SYuanquan Chen 		/* Cardbus can call us to add new devices to a bus, so ignore
101737f02195SYuanquan Chen 		 * those who are already fully discovered
101837f02195SYuanquan Chen 		 */
1019*44bda4b7SHari Vyas 		if (pci_dev_is_added(dev))
102037f02195SYuanquan Chen 			continue;
102137f02195SYuanquan Chen 
102237f02195SYuanquan Chen 		pcibios_setup_device(dev);
102337f02195SYuanquan Chen 	}
10247eef440aSBenjamin Herrenschmidt }
10257eef440aSBenjamin Herrenschmidt 
102679c8be83SMyron Stowe void pcibios_set_master(struct pci_dev *dev)
102779c8be83SMyron Stowe {
102879c8be83SMyron Stowe 	/* No special bus mastering setup handling */
102979c8be83SMyron Stowe }
103079c8be83SMyron Stowe 
1031cad5cef6SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus)
1032bf5e2ba2SBenjamin Herrenschmidt {
1033237865f1SBjorn Helgaas 	/* When called from the generic PCI probe, read PCI<->PCI bridge
1034237865f1SBjorn Helgaas 	 * bases. This is -not- called when generating the PCI tree from
1035237865f1SBjorn Helgaas 	 * the OF device-tree.
1036237865f1SBjorn Helgaas 	 */
1037237865f1SBjorn Helgaas 	pci_read_bridge_bases(bus);
1038237865f1SBjorn Helgaas 
1039237865f1SBjorn Helgaas 	/* Now fixup the bus bus */
10408b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_self(bus);
10418b8da358SBenjamin Herrenschmidt 
10428b8da358SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
10438b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
1044bf5e2ba2SBenjamin Herrenschmidt }
1045bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus);
1046bf5e2ba2SBenjamin Herrenschmidt 
1047cad5cef6SGreg Kroah-Hartman void pci_fixup_cardbus(struct pci_bus *bus)
10482d1c8618SBenjamin Herrenschmidt {
10492d1c8618SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
10502d1c8618SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
10512d1c8618SBenjamin Herrenschmidt }
10522d1c8618SBenjamin Herrenschmidt 
10532d1c8618SBenjamin Herrenschmidt 
10543fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev)
10553fd94c6bSBenjamin Herrenschmidt {
10560e47ff1cSRob Herring 	if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
10573fd94c6bSBenjamin Herrenschmidt 	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
10583fd94c6bSBenjamin Herrenschmidt 		return 1;
10593fd94c6bSBenjamin Herrenschmidt 	return 0;
10603fd94c6bSBenjamin Herrenschmidt }
10613fd94c6bSBenjamin Herrenschmidt 
10623fd94c6bSBenjamin Herrenschmidt /*
10633fd94c6bSBenjamin Herrenschmidt  * We need to avoid collisions with `mirrored' VGA ports
10643fd94c6bSBenjamin Herrenschmidt  * and other strange ISA hardware, so we always want the
10653fd94c6bSBenjamin Herrenschmidt  * addresses to be allocated in the 0x000-0x0ff region
10663fd94c6bSBenjamin Herrenschmidt  * modulo 0x400.
10673fd94c6bSBenjamin Herrenschmidt  *
10683fd94c6bSBenjamin Herrenschmidt  * Why? Because some silly external IO cards only decode
10693fd94c6bSBenjamin Herrenschmidt  * the low 10 bits of the IO address. The 0x00-0xff region
10703fd94c6bSBenjamin Herrenschmidt  * is reserved for motherboard devices that decode all 16
10713fd94c6bSBenjamin Herrenschmidt  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
10723fd94c6bSBenjamin Herrenschmidt  * but we want to try to avoid allocating at 0x2900-0x2bff
10733fd94c6bSBenjamin Herrenschmidt  * which might have be mirrored at 0x0100-0x03ff..
10743fd94c6bSBenjamin Herrenschmidt  */
10753b7a17fcSDominik Brodowski resource_size_t pcibios_align_resource(void *data, const struct resource *res,
10763fd94c6bSBenjamin Herrenschmidt 				resource_size_t size, resource_size_t align)
10773fd94c6bSBenjamin Herrenschmidt {
10783fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = data;
10793fd94c6bSBenjamin Herrenschmidt 	resource_size_t start = res->start;
10803fd94c6bSBenjamin Herrenschmidt 
1081b26b2d49SDominik Brodowski 	if (res->flags & IORESOURCE_IO) {
10823fd94c6bSBenjamin Herrenschmidt 		if (skip_isa_ioresource_align(dev))
1083b26b2d49SDominik Brodowski 			return start;
1084b26b2d49SDominik Brodowski 		if (start & 0x300)
10853fd94c6bSBenjamin Herrenschmidt 			start = (start + 0x3ff) & ~0x3ff;
10863fd94c6bSBenjamin Herrenschmidt 	}
1087b26b2d49SDominik Brodowski 
1088b26b2d49SDominik Brodowski 	return start;
10893fd94c6bSBenjamin Herrenschmidt }
10903fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource);
10913fd94c6bSBenjamin Herrenschmidt 
10923fd94c6bSBenjamin Herrenschmidt /*
10933fd94c6bSBenjamin Herrenschmidt  * Reparent resource children of pr that conflict with res
10943fd94c6bSBenjamin Herrenschmidt  * under res, and make res replace those children.
10953fd94c6bSBenjamin Herrenschmidt  */
10960f6023d5SHeiko Schocher static int reparent_resources(struct resource *parent,
10973fd94c6bSBenjamin Herrenschmidt 				     struct resource *res)
10983fd94c6bSBenjamin Herrenschmidt {
10993fd94c6bSBenjamin Herrenschmidt 	struct resource *p, **pp;
11003fd94c6bSBenjamin Herrenschmidt 	struct resource **firstpp = NULL;
11013fd94c6bSBenjamin Herrenschmidt 
11023fd94c6bSBenjamin Herrenschmidt 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
11033fd94c6bSBenjamin Herrenschmidt 		if (p->end < res->start)
11043fd94c6bSBenjamin Herrenschmidt 			continue;
11053fd94c6bSBenjamin Herrenschmidt 		if (res->end < p->start)
11063fd94c6bSBenjamin Herrenschmidt 			break;
11073fd94c6bSBenjamin Herrenschmidt 		if (p->start < res->start || p->end > res->end)
11083fd94c6bSBenjamin Herrenschmidt 			return -1;	/* not completely contained */
11093fd94c6bSBenjamin Herrenschmidt 		if (firstpp == NULL)
11103fd94c6bSBenjamin Herrenschmidt 			firstpp = pp;
11113fd94c6bSBenjamin Herrenschmidt 	}
11123fd94c6bSBenjamin Herrenschmidt 	if (firstpp == NULL)
11133fd94c6bSBenjamin Herrenschmidt 		return -1;	/* didn't find any conflicting entries? */
11143fd94c6bSBenjamin Herrenschmidt 	res->parent = parent;
11153fd94c6bSBenjamin Herrenschmidt 	res->child = *firstpp;
11163fd94c6bSBenjamin Herrenschmidt 	res->sibling = *pp;
11173fd94c6bSBenjamin Herrenschmidt 	*firstpp = res;
11183fd94c6bSBenjamin Herrenschmidt 	*pp = NULL;
11193fd94c6bSBenjamin Herrenschmidt 	for (p = res->child; p != NULL; p = p->sibling) {
11203fd94c6bSBenjamin Herrenschmidt 		p->parent = res;
1121ae2a84b4SKevin Hao 		pr_debug("PCI: Reparented %s %pR under %s\n",
1122ae2a84b4SKevin Hao 			 p->name, p, res->name);
11233fd94c6bSBenjamin Herrenschmidt 	}
11243fd94c6bSBenjamin Herrenschmidt 	return 0;
11253fd94c6bSBenjamin Herrenschmidt }
11263fd94c6bSBenjamin Herrenschmidt 
11273fd94c6bSBenjamin Herrenschmidt /*
11283fd94c6bSBenjamin Herrenschmidt  *  Handle resources of PCI devices.  If the world were perfect, we could
11293fd94c6bSBenjamin Herrenschmidt  *  just allocate all the resource regions and do nothing more.  It isn't.
11303fd94c6bSBenjamin Herrenschmidt  *  On the other hand, we cannot just re-allocate all devices, as it would
11313fd94c6bSBenjamin Herrenschmidt  *  require us to know lots of host bridge internals.  So we attempt to
11323fd94c6bSBenjamin Herrenschmidt  *  keep as much of the original configuration as possible, but tweak it
11333fd94c6bSBenjamin Herrenschmidt  *  when it's found to be wrong.
11343fd94c6bSBenjamin Herrenschmidt  *
11353fd94c6bSBenjamin Herrenschmidt  *  Known BIOS problems we have to work around:
11363fd94c6bSBenjamin Herrenschmidt  *	- I/O or memory regions not configured
11373fd94c6bSBenjamin Herrenschmidt  *	- regions configured, but not enabled in the command register
11383fd94c6bSBenjamin Herrenschmidt  *	- bogus I/O addresses above 64K used
11393fd94c6bSBenjamin Herrenschmidt  *	- expansion ROMs left enabled (this may sound harmless, but given
11403fd94c6bSBenjamin Herrenschmidt  *	  the fact the PCI specs explicitly allow address decoders to be
11413fd94c6bSBenjamin Herrenschmidt  *	  shared between expansion ROMs and other resource regions, it's
11423fd94c6bSBenjamin Herrenschmidt  *	  at least dangerous)
11433fd94c6bSBenjamin Herrenschmidt  *
11443fd94c6bSBenjamin Herrenschmidt  *  Our solution:
11453fd94c6bSBenjamin Herrenschmidt  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
11463fd94c6bSBenjamin Herrenschmidt  *	    This gives us fixed barriers on where we can allocate.
11473fd94c6bSBenjamin Herrenschmidt  *	(2) Allocate resources for all enabled devices.  If there is
11483fd94c6bSBenjamin Herrenschmidt  *	    a collision, just mark the resource as unallocated. Also
11493fd94c6bSBenjamin Herrenschmidt  *	    disable expansion ROMs during this step.
11503fd94c6bSBenjamin Herrenschmidt  *	(3) Try to allocate resources for disabled devices.  If the
11513fd94c6bSBenjamin Herrenschmidt  *	    resources were assigned correctly, everything goes well,
11523fd94c6bSBenjamin Herrenschmidt  *	    if they weren't, they won't disturb allocation of other
11533fd94c6bSBenjamin Herrenschmidt  *	    resources.
11543fd94c6bSBenjamin Herrenschmidt  *	(4) Assign new addresses to resources which were either
11553fd94c6bSBenjamin Herrenschmidt  *	    not configured at all or misconfigured.  If explicitly
11563fd94c6bSBenjamin Herrenschmidt  *	    requested by the user, configure expansion ROM address
11573fd94c6bSBenjamin Herrenschmidt  *	    as well.
11583fd94c6bSBenjamin Herrenschmidt  */
11593fd94c6bSBenjamin Herrenschmidt 
1160e51df2c1SAnton Blanchard static void pcibios_allocate_bus_resources(struct pci_bus *bus)
11613fd94c6bSBenjamin Herrenschmidt {
1162e90a1318SNathan Fontenot 	struct pci_bus *b;
11633fd94c6bSBenjamin Herrenschmidt 	int i;
11643fd94c6bSBenjamin Herrenschmidt 	struct resource *res, *pr;
11653fd94c6bSBenjamin Herrenschmidt 
1166b5ae5f91SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1167b5ae5f91SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1168b5ae5f91SBenjamin Herrenschmidt 
116989a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
117089a74eccSBjorn Helgaas 		if (!res || !res->flags || res->start > res->end || res->parent)
11713fd94c6bSBenjamin Herrenschmidt 			continue;
117248c2ce97SBenjamin Herrenschmidt 
117348c2ce97SBenjamin Herrenschmidt 		/* If the resource was left unset at this point, we clear it */
117448c2ce97SBenjamin Herrenschmidt 		if (res->flags & IORESOURCE_UNSET)
117548c2ce97SBenjamin Herrenschmidt 			goto clear_resource;
117648c2ce97SBenjamin Herrenschmidt 
11773fd94c6bSBenjamin Herrenschmidt 		if (bus->parent == NULL)
11783fd94c6bSBenjamin Herrenschmidt 			pr = (res->flags & IORESOURCE_IO) ?
11793fd94c6bSBenjamin Herrenschmidt 				&ioport_resource : &iomem_resource;
11803fd94c6bSBenjamin Herrenschmidt 		else {
11813fd94c6bSBenjamin Herrenschmidt 			pr = pci_find_parent_resource(bus->self, res);
11823fd94c6bSBenjamin Herrenschmidt 			if (pr == res) {
11833fd94c6bSBenjamin Herrenschmidt 				/* this happens when the generic PCI
11843fd94c6bSBenjamin Herrenschmidt 				 * code (wrongly) decides that this
11853fd94c6bSBenjamin Herrenschmidt 				 * bridge is transparent  -- paulus
11863fd94c6bSBenjamin Herrenschmidt 				 */
11873fd94c6bSBenjamin Herrenschmidt 				continue;
11883fd94c6bSBenjamin Herrenschmidt 			}
11893fd94c6bSBenjamin Herrenschmidt 		}
11903fd94c6bSBenjamin Herrenschmidt 
1191ae2a84b4SKevin Hao 		pr_debug("PCI: %s (bus %d) bridge rsrc %d: %pR, parent %p (%s)\n",
1192ae2a84b4SKevin Hao 			 bus->self ? pci_name(bus->self) : "PHB", bus->number,
1193ae2a84b4SKevin Hao 			 i, res, pr, (pr && pr->name) ? pr->name : "nil");
11943fd94c6bSBenjamin Herrenschmidt 
11953fd94c6bSBenjamin Herrenschmidt 		if (pr && !(pr->flags & IORESOURCE_UNSET)) {
11963ebfe46aSYinghai Lu 			struct pci_dev *dev = bus->self;
11973ebfe46aSYinghai Lu 
11983fd94c6bSBenjamin Herrenschmidt 			if (request_resource(pr, res) == 0)
11993fd94c6bSBenjamin Herrenschmidt 				continue;
12003fd94c6bSBenjamin Herrenschmidt 			/*
12013fd94c6bSBenjamin Herrenschmidt 			 * Must be a conflict with an existing entry.
12023fd94c6bSBenjamin Herrenschmidt 			 * Move that entry (or entries) under the
12033fd94c6bSBenjamin Herrenschmidt 			 * bridge resource and try again.
12043fd94c6bSBenjamin Herrenschmidt 			 */
12053fd94c6bSBenjamin Herrenschmidt 			if (reparent_resources(pr, res) == 0)
12063fd94c6bSBenjamin Herrenschmidt 				continue;
12073ebfe46aSYinghai Lu 
12083ebfe46aSYinghai Lu 			if (dev && i < PCI_BRIDGE_RESOURCE_NUM &&
12093ebfe46aSYinghai Lu 			    pci_claim_bridge_resource(dev,
12103ebfe46aSYinghai Lu 						i + PCI_BRIDGE_RESOURCES) == 0)
12113ebfe46aSYinghai Lu 				continue;
12123fd94c6bSBenjamin Herrenschmidt 		}
1213f2c2cbccSJoe Perches 		pr_warn("PCI: Cannot allocate resource region %d of PCI bridge %d, will remap\n",
1214f2c2cbccSJoe Perches 			i, bus->number);
12153fd94c6bSBenjamin Herrenschmidt 	clear_resource:
1216cf1a4cf8SGavin Shan 		/* The resource might be figured out when doing
1217cf1a4cf8SGavin Shan 		 * reassignment based on the resources required
1218cf1a4cf8SGavin Shan 		 * by the downstream PCI devices. Here we set
1219cf1a4cf8SGavin Shan 		 * the size of the resource to be 0 in order to
1220cf1a4cf8SGavin Shan 		 * save more space.
1221cf1a4cf8SGavin Shan 		 */
1222cf1a4cf8SGavin Shan 		res->start = 0;
1223cf1a4cf8SGavin Shan 		res->end = -1;
12243fd94c6bSBenjamin Herrenschmidt 		res->flags = 0;
12253fd94c6bSBenjamin Herrenschmidt 	}
1226e90a1318SNathan Fontenot 
1227e90a1318SNathan Fontenot 	list_for_each_entry(b, &bus->children, node)
1228e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
12293fd94c6bSBenjamin Herrenschmidt }
12303fd94c6bSBenjamin Herrenschmidt 
1231cad5cef6SGreg Kroah-Hartman static inline void alloc_resource(struct pci_dev *dev, int idx)
12323fd94c6bSBenjamin Herrenschmidt {
12333fd94c6bSBenjamin Herrenschmidt 	struct resource *pr, *r = &dev->resource[idx];
12343fd94c6bSBenjamin Herrenschmidt 
1235ae2a84b4SKevin Hao 	pr_debug("PCI: Allocating %s: Resource %d: %pR\n",
1236ae2a84b4SKevin Hao 		 pci_name(dev), idx, r);
12373fd94c6bSBenjamin Herrenschmidt 
12383fd94c6bSBenjamin Herrenschmidt 	pr = pci_find_parent_resource(dev, r);
12393fd94c6bSBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||
12403fd94c6bSBenjamin Herrenschmidt 	    request_resource(pr, r) < 0) {
12413fd94c6bSBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
12423fd94c6bSBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
12433fd94c6bSBenjamin Herrenschmidt 		if (pr)
1244ae2a84b4SKevin Hao 			pr_debug("PCI:  parent is %p: %pR\n", pr, pr);
12453fd94c6bSBenjamin Herrenschmidt 		/* We'll assign a new address later */
12463fd94c6bSBenjamin Herrenschmidt 		r->flags |= IORESOURCE_UNSET;
12473fd94c6bSBenjamin Herrenschmidt 		r->end -= r->start;
12483fd94c6bSBenjamin Herrenschmidt 		r->start = 0;
12493fd94c6bSBenjamin Herrenschmidt 	}
12503fd94c6bSBenjamin Herrenschmidt }
12513fd94c6bSBenjamin Herrenschmidt 
12523fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass)
12533fd94c6bSBenjamin Herrenschmidt {
12543fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = NULL;
12553fd94c6bSBenjamin Herrenschmidt 	int idx, disabled;
12563fd94c6bSBenjamin Herrenschmidt 	u16 command;
12573fd94c6bSBenjamin Herrenschmidt 	struct resource *r;
12583fd94c6bSBenjamin Herrenschmidt 
12593fd94c6bSBenjamin Herrenschmidt 	for_each_pci_dev(dev) {
12603fd94c6bSBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1261ad892a63SBenjamin Herrenschmidt 		for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
12623fd94c6bSBenjamin Herrenschmidt 			r = &dev->resource[idx];
12633fd94c6bSBenjamin Herrenschmidt 			if (r->parent)		/* Already allocated */
12643fd94c6bSBenjamin Herrenschmidt 				continue;
12653fd94c6bSBenjamin Herrenschmidt 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
12663fd94c6bSBenjamin Herrenschmidt 				continue;	/* Not assigned at all */
1267ad892a63SBenjamin Herrenschmidt 			/* We only allocate ROMs on pass 1 just in case they
1268ad892a63SBenjamin Herrenschmidt 			 * have been screwed up by firmware
1269ad892a63SBenjamin Herrenschmidt 			 */
1270ad892a63SBenjamin Herrenschmidt 			if (idx == PCI_ROM_RESOURCE )
1271ad892a63SBenjamin Herrenschmidt 				disabled = 1;
12723fd94c6bSBenjamin Herrenschmidt 			if (r->flags & IORESOURCE_IO)
12733fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_IO);
12743fd94c6bSBenjamin Herrenschmidt 			else
12753fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_MEMORY);
1276533b1928SPaul Mackerras 			if (pass == disabled)
1277533b1928SPaul Mackerras 				alloc_resource(dev, idx);
12783fd94c6bSBenjamin Herrenschmidt 		}
12793fd94c6bSBenjamin Herrenschmidt 		if (pass)
12803fd94c6bSBenjamin Herrenschmidt 			continue;
12813fd94c6bSBenjamin Herrenschmidt 		r = &dev->resource[PCI_ROM_RESOURCE];
1282ad892a63SBenjamin Herrenschmidt 		if (r->flags) {
12833fd94c6bSBenjamin Herrenschmidt 			/* Turn the ROM off, leave the resource region,
12843fd94c6bSBenjamin Herrenschmidt 			 * but keep it unregistered.
12853fd94c6bSBenjamin Herrenschmidt 			 */
12863fd94c6bSBenjamin Herrenschmidt 			u32 reg;
1287ad892a63SBenjamin Herrenschmidt 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1288ad892a63SBenjamin Herrenschmidt 			if (reg & PCI_ROM_ADDRESS_ENABLE) {
1289b0494bc8SBenjamin Herrenschmidt 				pr_debug("PCI: Switching off ROM of %s\n",
1290b0494bc8SBenjamin Herrenschmidt 					 pci_name(dev));
12913fd94c6bSBenjamin Herrenschmidt 				r->flags &= ~IORESOURCE_ROM_ENABLE;
12923fd94c6bSBenjamin Herrenschmidt 				pci_write_config_dword(dev, dev->rom_base_reg,
12933fd94c6bSBenjamin Herrenschmidt 						       reg & ~PCI_ROM_ADDRESS_ENABLE);
12943fd94c6bSBenjamin Herrenschmidt 			}
12953fd94c6bSBenjamin Herrenschmidt 		}
12963fd94c6bSBenjamin Herrenschmidt 	}
1297ad892a63SBenjamin Herrenschmidt }
12983fd94c6bSBenjamin Herrenschmidt 
1299c1f34302SBenjamin Herrenschmidt static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
1300c1f34302SBenjamin Herrenschmidt {
1301c1f34302SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
1302c1f34302SBenjamin Herrenschmidt 	resource_size_t	offset;
1303c1f34302SBenjamin Herrenschmidt 	struct resource *res, *pres;
1304c1f34302SBenjamin Herrenschmidt 	int i;
1305c1f34302SBenjamin Herrenschmidt 
1306c1f34302SBenjamin Herrenschmidt 	pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
1307c1f34302SBenjamin Herrenschmidt 
1308c1f34302SBenjamin Herrenschmidt 	/* Check for IO */
1309c1f34302SBenjamin Herrenschmidt 	if (!(hose->io_resource.flags & IORESOURCE_IO))
1310c1f34302SBenjamin Herrenschmidt 		goto no_io;
1311c1f34302SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1312c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1313c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1314c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy IO";
1315c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_IO;
1316c1f34302SBenjamin Herrenschmidt 	res->start = offset;
1317c1f34302SBenjamin Herrenschmidt 	res->end = (offset + 0xfff) & 0xfffffffful;
1318c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate legacy IO: %pR\n", res);
1319c1f34302SBenjamin Herrenschmidt 	if (request_resource(&hose->io_resource, res)) {
1320c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1321c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1322c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1323c1f34302SBenjamin Herrenschmidt 		kfree(res);
1324c1f34302SBenjamin Herrenschmidt 	}
1325c1f34302SBenjamin Herrenschmidt 
1326c1f34302SBenjamin Herrenschmidt  no_io:
1327c1f34302SBenjamin Herrenschmidt 	/* Check for memory */
1328c1f34302SBenjamin Herrenschmidt 	for (i = 0; i < 3; i++) {
1329c1f34302SBenjamin Herrenschmidt 		pres = &hose->mem_resources[i];
13303fd47f06SBenjamin Herrenschmidt 		offset = hose->mem_offset[i];
1331c1f34302SBenjamin Herrenschmidt 		if (!(pres->flags & IORESOURCE_MEM))
1332c1f34302SBenjamin Herrenschmidt 			continue;
1333c1f34302SBenjamin Herrenschmidt 		pr_debug("hose mem res: %pR\n", pres);
1334c1f34302SBenjamin Herrenschmidt 		if ((pres->start - offset) <= 0xa0000 &&
1335c1f34302SBenjamin Herrenschmidt 		    (pres->end - offset) >= 0xbffff)
1336c1f34302SBenjamin Herrenschmidt 			break;
1337c1f34302SBenjamin Herrenschmidt 	}
1338c1f34302SBenjamin Herrenschmidt 	if (i >= 3)
1339c1f34302SBenjamin Herrenschmidt 		return;
1340c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1341c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1342c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy VGA memory";
1343c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_MEM;
1344c1f34302SBenjamin Herrenschmidt 	res->start = 0xa0000 + offset;
1345c1f34302SBenjamin Herrenschmidt 	res->end = 0xbffff + offset;
1346c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate VGA memory: %pR\n", res);
1347c1f34302SBenjamin Herrenschmidt 	if (request_resource(pres, res)) {
1348c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1349c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1350c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1351c1f34302SBenjamin Herrenschmidt 		kfree(res);
1352c1f34302SBenjamin Herrenschmidt 	}
1353c1f34302SBenjamin Herrenschmidt }
1354c1f34302SBenjamin Herrenschmidt 
13553fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void)
13563fd94c6bSBenjamin Herrenschmidt {
1357e90a1318SNathan Fontenot 	struct pci_bus *b;
1358e90a1318SNathan Fontenot 
135948c2ce97SBenjamin Herrenschmidt 	/* Allocate and assign resources */
1360e90a1318SNathan Fontenot 	list_for_each_entry(b, &pci_root_buses, node)
1361e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
13629a1a70aeSBenjamin Herrenschmidt 	if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
13633fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(0);
13643fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(1);
13659a1a70aeSBenjamin Herrenschmidt 	}
13663fd94c6bSBenjamin Herrenschmidt 
1367c1f34302SBenjamin Herrenschmidt 	/* Before we start assigning unassigned resource, we try to reserve
1368c1f34302SBenjamin Herrenschmidt 	 * the low IO area and the VGA memory area if they intersect the
1369c1f34302SBenjamin Herrenschmidt 	 * bus available resources to avoid allocating things on top of them
1370c1f34302SBenjamin Herrenschmidt 	 */
13710e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1372c1f34302SBenjamin Herrenschmidt 		list_for_each_entry(b, &pci_root_buses, node)
1373c1f34302SBenjamin Herrenschmidt 			pcibios_reserve_legacy_regions(b);
1374c1f34302SBenjamin Herrenschmidt 	}
1375c1f34302SBenjamin Herrenschmidt 
1376c1f34302SBenjamin Herrenschmidt 	/* Now, if the platform didn't decide to blindly trust the firmware,
1377c1f34302SBenjamin Herrenschmidt 	 * we proceed to assigning things that were left unassigned
1378c1f34302SBenjamin Herrenschmidt 	 */
13790e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1380a77acda0SWolfram Sang 		pr_debug("PCI: Assigning unassigned resources...\n");
13813fd94c6bSBenjamin Herrenschmidt 		pci_assign_unassigned_resources();
13823fd94c6bSBenjamin Herrenschmidt 	}
13833fd94c6bSBenjamin Herrenschmidt 
13843fd94c6bSBenjamin Herrenschmidt 	/* Call machine dependent fixup */
13853fd94c6bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup)
13863fd94c6bSBenjamin Herrenschmidt 		ppc_md.pcibios_fixup();
13873fd94c6bSBenjamin Herrenschmidt }
13883fd94c6bSBenjamin Herrenschmidt 
1389fd6852c8SBenjamin Herrenschmidt /* This is used by the PCI hotplug driver to allocate resource
13903fd94c6bSBenjamin Herrenschmidt  * of newly plugged busses. We can try to consolidate with the
1391fd6852c8SBenjamin Herrenschmidt  * rest of the code later, for now, keep it as-is as our main
1392fd6852c8SBenjamin Herrenschmidt  * resource allocation function doesn't deal with sub-trees yet.
13933fd94c6bSBenjamin Herrenschmidt  */
1394baf75b0aSStephen Rothwell void pcibios_claim_one_bus(struct pci_bus *bus)
13953fd94c6bSBenjamin Herrenschmidt {
13963fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev;
13973fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *child_bus;
13983fd94c6bSBenjamin Herrenschmidt 
13993fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
14003fd94c6bSBenjamin Herrenschmidt 		int i;
14013fd94c6bSBenjamin Herrenschmidt 
14023fd94c6bSBenjamin Herrenschmidt 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
14033fd94c6bSBenjamin Herrenschmidt 			struct resource *r = &dev->resource[i];
14043fd94c6bSBenjamin Herrenschmidt 
14053fd94c6bSBenjamin Herrenschmidt 			if (r->parent || !r->start || !r->flags)
14063fd94c6bSBenjamin Herrenschmidt 				continue;
1407fd6852c8SBenjamin Herrenschmidt 
1408ae2a84b4SKevin Hao 			pr_debug("PCI: Claiming %s: Resource %d: %pR\n",
1409ae2a84b4SKevin Hao 				 pci_name(dev), i, r);
1410fd6852c8SBenjamin Herrenschmidt 
14113ebfe46aSYinghai Lu 			if (pci_claim_resource(dev, i) == 0)
14123ebfe46aSYinghai Lu 				continue;
14133ebfe46aSYinghai Lu 
14143ebfe46aSYinghai Lu 			pci_claim_bridge_resource(dev, i);
14153fd94c6bSBenjamin Herrenschmidt 		}
14163fd94c6bSBenjamin Herrenschmidt 	}
14173fd94c6bSBenjamin Herrenschmidt 
14183fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(child_bus, &bus->children, node)
14193fd94c6bSBenjamin Herrenschmidt 		pcibios_claim_one_bus(child_bus);
14203fd94c6bSBenjamin Herrenschmidt }
14215b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
1422fd6852c8SBenjamin Herrenschmidt 
1423fd6852c8SBenjamin Herrenschmidt 
1424fd6852c8SBenjamin Herrenschmidt /* pcibios_finish_adding_to_bus
1425fd6852c8SBenjamin Herrenschmidt  *
1426fd6852c8SBenjamin Herrenschmidt  * This is to be called by the hotplug code after devices have been
1427fd6852c8SBenjamin Herrenschmidt  * added to a bus, this include calling it for a PHB that is just
1428fd6852c8SBenjamin Herrenschmidt  * being added
1429fd6852c8SBenjamin Herrenschmidt  */
1430fd6852c8SBenjamin Herrenschmidt void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1431fd6852c8SBenjamin Herrenschmidt {
1432fd6852c8SBenjamin Herrenschmidt 	pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1433fd6852c8SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1434fd6852c8SBenjamin Herrenschmidt 
1435fd6852c8SBenjamin Herrenschmidt 	/* Allocate bus and devices resources */
1436fd6852c8SBenjamin Herrenschmidt 	pcibios_allocate_bus_resources(bus);
1437fd6852c8SBenjamin Herrenschmidt 	pcibios_claim_one_bus(bus);
14387415c14cSGavin Shan 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
14397415c14cSGavin Shan 		if (bus->self)
14407415c14cSGavin Shan 			pci_assign_unassigned_bridge_resources(bus->self);
14417415c14cSGavin Shan 		else
1442ab444ec9SGavin Shan 			pci_assign_unassigned_bus_resources(bus);
14437415c14cSGavin Shan 	}
1444fd6852c8SBenjamin Herrenschmidt 
14456a040ce7SThadeu Lima de Souza Cascardo 	/* Fixup EEH */
14466a040ce7SThadeu Lima de Souza Cascardo 	eeh_add_device_tree_late(bus);
14476a040ce7SThadeu Lima de Souza Cascardo 
1448fd6852c8SBenjamin Herrenschmidt 	/* Add new devices to global lists.  Register in proc, sysfs. */
1449fd6852c8SBenjamin Herrenschmidt 	pci_bus_add_devices(bus);
1450fd6852c8SBenjamin Herrenschmidt 
14516a040ce7SThadeu Lima de Souza Cascardo 	/* sysfs files should only be added after devices are added */
14526a040ce7SThadeu Lima de Souza Cascardo 	eeh_add_sysfs_files(bus);
1453fd6852c8SBenjamin Herrenschmidt }
1454fd6852c8SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1455fd6852c8SBenjamin Herrenschmidt 
1456549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask)
1457549beb9bSBenjamin Herrenschmidt {
1458467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
1459467efc2eSDaniel Axtens 
1460467efc2eSDaniel Axtens 	if (phb->controller_ops.enable_device_hook)
1461467efc2eSDaniel Axtens 		if (!phb->controller_ops.enable_device_hook(dev))
1462549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1463549beb9bSBenjamin Herrenschmidt 
14647cfb5f9aSBjorn Helgaas 	return pci_enable_resources(dev, mask);
1465549beb9bSBenjamin Herrenschmidt }
146653280323SBenjamin Herrenschmidt 
1467abeeed6dSMichael Neuling void pcibios_disable_device(struct pci_dev *dev)
1468abeeed6dSMichael Neuling {
1469abeeed6dSMichael Neuling 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
1470abeeed6dSMichael Neuling 
1471abeeed6dSMichael Neuling 	if (phb->controller_ops.disable_device)
1472abeeed6dSMichael Neuling 		phb->controller_ops.disable_device(dev);
1473abeeed6dSMichael Neuling }
1474abeeed6dSMichael Neuling 
147538973ba7SBjorn Helgaas resource_size_t pcibios_io_space_offset(struct pci_controller *hose)
147638973ba7SBjorn Helgaas {
147738973ba7SBjorn Helgaas 	return (unsigned long) hose->io_base_virt - _IO_BASE;
147838973ba7SBjorn Helgaas }
147938973ba7SBjorn Helgaas 
1480cad5cef6SGreg Kroah-Hartman static void pcibios_setup_phb_resources(struct pci_controller *hose,
1481cad5cef6SGreg Kroah-Hartman 					struct list_head *resources)
148253280323SBenjamin Herrenschmidt {
148353280323SBenjamin Herrenschmidt 	struct resource *res;
14843fd47f06SBenjamin Herrenschmidt 	resource_size_t offset;
148553280323SBenjamin Herrenschmidt 	int i;
148653280323SBenjamin Herrenschmidt 
148753280323SBenjamin Herrenschmidt 	/* Hookup PHB IO resource */
148845a709f8SBjorn Helgaas 	res = &hose->io_resource;
148953280323SBenjamin Herrenschmidt 
149053280323SBenjamin Herrenschmidt 	if (!res->flags) {
1491cdb1b342SBenjamin Herrenschmidt 		pr_debug("PCI: I/O resource not set for host"
1492b7c670d6SRob Herring 			 " bridge %pOF (domain %d)\n",
1493b7c670d6SRob Herring 			 hose->dn, hose->global_number);
14943fd47f06SBenjamin Herrenschmidt 	} else {
14953fd47f06SBenjamin Herrenschmidt 		offset = pcibios_io_space_offset(hose);
14963fd47f06SBenjamin Herrenschmidt 
1497ae2a84b4SKevin Hao 		pr_debug("PCI: PHB IO resource    = %pR off 0x%08llx\n",
1498ae2a84b4SKevin Hao 			 res, (unsigned long long)offset);
14993fd47f06SBenjamin Herrenschmidt 		pci_add_resource_offset(resources, res, offset);
1500a0b8e76fSBenjamin Herrenschmidt 	}
1501a0b8e76fSBenjamin Herrenschmidt 
150253280323SBenjamin Herrenschmidt 	/* Hookup PHB Memory resources */
150353280323SBenjamin Herrenschmidt 	for (i = 0; i < 3; ++i) {
150453280323SBenjamin Herrenschmidt 		res = &hose->mem_resources[i];
1505727597d1SGavin Shan 		if (!res->flags)
15063fd47f06SBenjamin Herrenschmidt 			continue;
1507727597d1SGavin Shan 
15083fd47f06SBenjamin Herrenschmidt 		offset = hose->mem_offset[i];
1509ae2a84b4SKevin Hao 		pr_debug("PCI: PHB MEM resource %d = %pR off 0x%08llx\n", i,
1510ae2a84b4SKevin Hao 			 res, (unsigned long long)offset);
151153280323SBenjamin Herrenschmidt 
15123fd47f06SBenjamin Herrenschmidt 		pci_add_resource_offset(resources, res, offset);
15133fd47f06SBenjamin Herrenschmidt 	}
151453280323SBenjamin Herrenschmidt }
151589c2dd62SKumar Gala 
151689c2dd62SKumar Gala /*
151789c2dd62SKumar Gala  * Null PCI config access functions, for the case when we can't
151889c2dd62SKumar Gala  * find a hose.
151989c2dd62SKumar Gala  */
152089c2dd62SKumar Gala #define NULL_PCI_OP(rw, size, type)					\
152189c2dd62SKumar Gala static int								\
152289c2dd62SKumar Gala null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
152389c2dd62SKumar Gala {									\
152489c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
152589c2dd62SKumar Gala }
152689c2dd62SKumar Gala 
152789c2dd62SKumar Gala static int
152889c2dd62SKumar Gala null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
152989c2dd62SKumar Gala 		 int len, u32 *val)
153089c2dd62SKumar Gala {
153189c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
153289c2dd62SKumar Gala }
153389c2dd62SKumar Gala 
153489c2dd62SKumar Gala static int
153589c2dd62SKumar Gala null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
153689c2dd62SKumar Gala 		  int len, u32 val)
153789c2dd62SKumar Gala {
153889c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
153989c2dd62SKumar Gala }
154089c2dd62SKumar Gala 
154189c2dd62SKumar Gala static struct pci_ops null_pci_ops =
154289c2dd62SKumar Gala {
154389c2dd62SKumar Gala 	.read = null_read_config,
154489c2dd62SKumar Gala 	.write = null_write_config,
154589c2dd62SKumar Gala };
154689c2dd62SKumar Gala 
154789c2dd62SKumar Gala /*
154889c2dd62SKumar Gala  * These functions are used early on before PCI scanning is done
154989c2dd62SKumar Gala  * and all of the pci_dev and pci_bus structures have been created.
155089c2dd62SKumar Gala  */
155189c2dd62SKumar Gala static struct pci_bus *
155289c2dd62SKumar Gala fake_pci_bus(struct pci_controller *hose, int busnr)
155389c2dd62SKumar Gala {
155489c2dd62SKumar Gala 	static struct pci_bus bus;
155589c2dd62SKumar Gala 
1556b0d436c7SAnton Blanchard 	if (hose == NULL) {
155789c2dd62SKumar Gala 		printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
155889c2dd62SKumar Gala 	}
155989c2dd62SKumar Gala 	bus.number = busnr;
156089c2dd62SKumar Gala 	bus.sysdata = hose;
156189c2dd62SKumar Gala 	bus.ops = hose? hose->ops: &null_pci_ops;
156289c2dd62SKumar Gala 	return &bus;
156389c2dd62SKumar Gala }
156489c2dd62SKumar Gala 
156589c2dd62SKumar Gala #define EARLY_PCI_OP(rw, size, type)					\
156689c2dd62SKumar Gala int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
156789c2dd62SKumar Gala 			       int devfn, int offset, type value)	\
156889c2dd62SKumar Gala {									\
156989c2dd62SKumar Gala 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
157089c2dd62SKumar Gala 					    devfn, offset, value);	\
157189c2dd62SKumar Gala }
157289c2dd62SKumar Gala 
157389c2dd62SKumar Gala EARLY_PCI_OP(read, byte, u8 *)
157489c2dd62SKumar Gala EARLY_PCI_OP(read, word, u16 *)
157589c2dd62SKumar Gala EARLY_PCI_OP(read, dword, u32 *)
157689c2dd62SKumar Gala EARLY_PCI_OP(write, byte, u8)
157789c2dd62SKumar Gala EARLY_PCI_OP(write, word, u16)
157889c2dd62SKumar Gala EARLY_PCI_OP(write, dword, u32)
157989c2dd62SKumar Gala 
158089c2dd62SKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
158189c2dd62SKumar Gala 			  int cap)
158289c2dd62SKumar Gala {
158389c2dd62SKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
158489c2dd62SKumar Gala }
15850ed2c722SGrant Likely 
158698d9f30cSBenjamin Herrenschmidt struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
158798d9f30cSBenjamin Herrenschmidt {
158898d9f30cSBenjamin Herrenschmidt 	struct pci_controller *hose = bus->sysdata;
158998d9f30cSBenjamin Herrenschmidt 
159098d9f30cSBenjamin Herrenschmidt 	return of_node_get(hose->dn);
159198d9f30cSBenjamin Herrenschmidt }
159298d9f30cSBenjamin Herrenschmidt 
15930ed2c722SGrant Likely /**
15940ed2c722SGrant Likely  * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
15950ed2c722SGrant Likely  * @hose: Pointer to the PCI host controller instance structure
15960ed2c722SGrant Likely  */
1597cad5cef6SGreg Kroah-Hartman void pcibios_scan_phb(struct pci_controller *hose)
15980ed2c722SGrant Likely {
159945a709f8SBjorn Helgaas 	LIST_HEAD(resources);
16000ed2c722SGrant Likely 	struct pci_bus *bus;
16010ed2c722SGrant Likely 	struct device_node *node = hose->dn;
16020ed2c722SGrant Likely 	int mode;
16030ed2c722SGrant Likely 
1604b7c670d6SRob Herring 	pr_debug("PCI: Scanning PHB %pOF\n", node);
16050ed2c722SGrant Likely 
16060ed2c722SGrant Likely 	/* Get some IO space for the new PHB */
16070ed2c722SGrant Likely 	pcibios_setup_phb_io_space(hose);
16080ed2c722SGrant Likely 
16090ed2c722SGrant Likely 	/* Wire up PHB bus resources */
161045a709f8SBjorn Helgaas 	pcibios_setup_phb_resources(hose, &resources);
161145a709f8SBjorn Helgaas 
1612be8e60d8SYinghai Lu 	hose->busn.start = hose->first_busno;
1613be8e60d8SYinghai Lu 	hose->busn.end	 = hose->last_busno;
1614be8e60d8SYinghai Lu 	hose->busn.flags = IORESOURCE_BUS;
1615be8e60d8SYinghai Lu 	pci_add_resource(&resources, &hose->busn);
1616be8e60d8SYinghai Lu 
161745a709f8SBjorn Helgaas 	/* Create an empty bus for the toplevel */
161845a709f8SBjorn Helgaas 	bus = pci_create_root_bus(hose->parent, hose->first_busno,
161945a709f8SBjorn Helgaas 				  hose->ops, hose, &resources);
162045a709f8SBjorn Helgaas 	if (bus == NULL) {
162145a709f8SBjorn Helgaas 		pr_err("Failed to create bus for PCI domain %04x\n",
162245a709f8SBjorn Helgaas 			hose->global_number);
162345a709f8SBjorn Helgaas 		pci_free_resource_list(&resources);
162445a709f8SBjorn Helgaas 		return;
162545a709f8SBjorn Helgaas 	}
162645a709f8SBjorn Helgaas 	hose->bus = bus;
16270ed2c722SGrant Likely 
16280ed2c722SGrant Likely 	/* Get probe mode and perform scan */
16290ed2c722SGrant Likely 	mode = PCI_PROBE_NORMAL;
1630467efc2eSDaniel Axtens 	if (node && hose->controller_ops.probe_mode)
1631467efc2eSDaniel Axtens 		mode = hose->controller_ops.probe_mode(bus);
16320ed2c722SGrant Likely 	pr_debug("    probe mode: %d\n", mode);
1633be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_DEVTREE)
16340ed2c722SGrant Likely 		of_scan_bus(node, bus);
16350ed2c722SGrant Likely 
1636be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_NORMAL) {
1637be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, 255);
1638be8e60d8SYinghai Lu 		hose->last_busno = pci_scan_child_bus(bus);
1639be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, hose->last_busno);
1640be8e60d8SYinghai Lu 	}
1641781fb7a3SBenjamin Herrenschmidt 
1642491b98c3SBenjamin Herrenschmidt 	/* Platform gets a chance to do some global fixups before
1643491b98c3SBenjamin Herrenschmidt 	 * we proceed to resource allocation
1644491b98c3SBenjamin Herrenschmidt 	 */
1645491b98c3SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_phb)
1646491b98c3SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_phb(hose);
1647491b98c3SBenjamin Herrenschmidt 
1648781fb7a3SBenjamin Herrenschmidt 	/* Configure PCI Express settings */
1649bb36c445SBenjamin Herrenschmidt 	if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
1650781fb7a3SBenjamin Herrenschmidt 		struct pci_bus *child;
1651a58674ffSBjorn Helgaas 		list_for_each_entry(child, &bus->children, node)
1652a58674ffSBjorn Helgaas 			pcie_bus_configure_settings(child);
1653781fb7a3SBenjamin Herrenschmidt 	}
16540ed2c722SGrant Likely }
16555b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_scan_phb);
1656c065488fSKumar Gala 
1657c065488fSKumar Gala static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
1658c065488fSKumar Gala {
1659c065488fSKumar Gala 	int i, class = dev->class >> 8;
166005737c7cSJason Jin 	/* When configured as agent, programing interface = 1 */
166105737c7cSJason Jin 	int prog_if = dev->class & 0xf;
1662c065488fSKumar Gala 
1663c065488fSKumar Gala 	if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
1664c065488fSKumar Gala 	     class == PCI_CLASS_BRIDGE_OTHER) &&
1665c065488fSKumar Gala 		(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
166605737c7cSJason Jin 		(prog_if == 0) &&
1667c065488fSKumar Gala 		(dev->bus->parent == NULL)) {
1668c065488fSKumar Gala 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1669c065488fSKumar Gala 			dev->resource[i].start = 0;
1670c065488fSKumar Gala 			dev->resource[i].end = 0;
1671c065488fSKumar Gala 			dev->resource[i].flags = 0;
1672c065488fSKumar Gala 		}
1673c065488fSKumar Gala 	}
1674c065488fSKumar Gala }
1675c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1676c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1677