xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision 6a9166b5beb82bbfcf86b9215785c6b74a4608ff)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25516b540SKumar Gala /*
35516b540SKumar Gala  * Contains common pci routines for ALL ppc platform
4cf1d8a8aSKumar Gala  * (based on pci_32.c and pci_64.c)
5cf1d8a8aSKumar Gala  *
6cf1d8a8aSKumar Gala  * Port for PPC64 David Engebretsen, IBM Corp.
7cf1d8a8aSKumar Gala  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
8cf1d8a8aSKumar Gala  *
9cf1d8a8aSKumar Gala  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
10cf1d8a8aSKumar Gala  *   Rework, based on alpha PCI code.
11cf1d8a8aSKumar Gala  *
12cf1d8a8aSKumar Gala  * Common pmac/prep/chrp pci routines. -- Cort
135516b540SKumar Gala  */
145516b540SKumar Gala 
155516b540SKumar Gala #include <linux/kernel.h>
165516b540SKumar Gala #include <linux/pci.h>
175516b540SKumar Gala #include <linux/string.h>
185516b540SKumar Gala #include <linux/init.h>
19d92a208dSGavin Shan #include <linux/delay.h>
2066b15db6SPaul Gortmaker #include <linux/export.h>
2122ae782fSGrant Likely #include <linux/of_address.h>
2204bea68bSSebastian Andrzej Siewior #include <linux/of_pci.h>
235516b540SKumar Gala #include <linux/mm.h>
243a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
255516b540SKumar Gala #include <linux/list.h>
265516b540SKumar Gala #include <linux/syscalls.h>
275516b540SKumar Gala #include <linux/irq.h>
285516b540SKumar Gala #include <linux/vmalloc.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
30c2e1d845SBrian King #include <linux/vgaarb.h>
3198fa15f3SAnshuman Khandual #include <linux/numa.h>
325516b540SKumar Gala 
335516b540SKumar Gala #include <asm/processor.h>
345516b540SKumar Gala #include <asm/io.h>
355516b540SKumar Gala #include <asm/prom.h>
365516b540SKumar Gala #include <asm/pci-bridge.h>
375516b540SKumar Gala #include <asm/byteorder.h>
385516b540SKumar Gala #include <asm/machdep.h>
395516b540SKumar Gala #include <asm/ppc-pci.h>
408b8da358SBenjamin Herrenschmidt #include <asm/eeh.h>
415516b540SKumar Gala 
4244bda4b7SHari Vyas #include "../../../drivers/pci/pci.h"
4344bda4b7SHari Vyas 
4463a72284SGuilherme G. Piccoli /* hose_spinlock protects accesses to the the phb_bitmap. */
45a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
46c3bd517dSMilton Miller LIST_HEAD(hose_list);
47a4c9e328SKumar Gala 
4863a72284SGuilherme G. Piccoli /* For dynamic PHB numbering on get_phb_number(): max number of PHBs. */
4963a72284SGuilherme G. Piccoli #define MAX_PHBS 0x10000
5063a72284SGuilherme G. Piccoli 
5163a72284SGuilherme G. Piccoli /*
5263a72284SGuilherme G. Piccoli  * For dynamic PHB numbering: used/free PHBs tracking bitmap.
5363a72284SGuilherme G. Piccoli  * Accesses to this bitmap should be protected by hose_spinlock.
5463a72284SGuilherme G. Piccoli  */
5563a72284SGuilherme G. Piccoli static DECLARE_BITMAP(phb_bitmap, MAX_PHBS);
56a4c9e328SKumar Gala 
5725e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */
5825e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base;
599445aa1aSAl Viro EXPORT_SYMBOL(isa_mem_base);
6025e81f92SBenjamin Herrenschmidt 
61a4c9e328SKumar Gala 
6268005b67SChristoph Hellwig static const struct dma_map_ops *pci_dma_ops;
634fc665b8SBecky Bruce 
645299709dSBart Van Assche void set_pci_dma_ops(const struct dma_map_ops *dma_ops)
654fc665b8SBecky Bruce {
664fc665b8SBecky Bruce 	pci_dma_ops = dma_ops;
674fc665b8SBecky Bruce }
684fc665b8SBecky Bruce 
6963a72284SGuilherme G. Piccoli /*
7063a72284SGuilherme G. Piccoli  * This function should run under locking protection, specifically
7163a72284SGuilherme G. Piccoli  * hose_spinlock.
7263a72284SGuilherme G. Piccoli  */
7363a72284SGuilherme G. Piccoli static int get_phb_number(struct device_node *dn)
7463a72284SGuilherme G. Piccoli {
7563a72284SGuilherme G. Piccoli 	int ret, phb_id = -1;
7661e8a0d5SMichael Ellerman 	u32 prop_32;
7763a72284SGuilherme G. Piccoli 	u64 prop;
7863a72284SGuilherme G. Piccoli 
7963a72284SGuilherme G. Piccoli 	/*
8063a72284SGuilherme G. Piccoli 	 * Try fixed PHB numbering first, by checking archs and reading
8163a72284SGuilherme G. Piccoli 	 * the respective device-tree properties. Firstly, try powernv by
8263a72284SGuilherme G. Piccoli 	 * reading "ibm,opal-phbid", only present in OPAL environment.
8363a72284SGuilherme G. Piccoli 	 */
8463a72284SGuilherme G. Piccoli 	ret = of_property_read_u64(dn, "ibm,opal-phbid", &prop);
8561e8a0d5SMichael Ellerman 	if (ret) {
8661e8a0d5SMichael Ellerman 		ret = of_property_read_u32_index(dn, "reg", 1, &prop_32);
8761e8a0d5SMichael Ellerman 		prop = prop_32;
8861e8a0d5SMichael Ellerman 	}
8963a72284SGuilherme G. Piccoli 
9063a72284SGuilherme G. Piccoli 	if (!ret)
9163a72284SGuilherme G. Piccoli 		phb_id = (int)(prop & (MAX_PHBS - 1));
9263a72284SGuilherme G. Piccoli 
9363a72284SGuilherme G. Piccoli 	/* We need to be sure to not use the same PHB number twice. */
9463a72284SGuilherme G. Piccoli 	if ((phb_id >= 0) && !test_and_set_bit(phb_id, phb_bitmap))
9563a72284SGuilherme G. Piccoli 		return phb_id;
9663a72284SGuilherme G. Piccoli 
9763a72284SGuilherme G. Piccoli 	/*
9863a72284SGuilherme G. Piccoli 	 * If not pseries nor powernv, or if fixed PHB numbering tried to add
9963a72284SGuilherme G. Piccoli 	 * the same PHB number twice, then fallback to dynamic PHB numbering.
10063a72284SGuilherme G. Piccoli 	 */
10163a72284SGuilherme G. Piccoli 	phb_id = find_first_zero_bit(phb_bitmap, MAX_PHBS);
10263a72284SGuilherme G. Piccoli 	BUG_ON(phb_id >= MAX_PHBS);
10363a72284SGuilherme G. Piccoli 	set_bit(phb_id, phb_bitmap);
10463a72284SGuilherme G. Piccoli 
10563a72284SGuilherme G. Piccoli 	return phb_id;
10663a72284SGuilherme G. Piccoli }
10763a72284SGuilherme G. Piccoli 
1082d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
109a4c9e328SKumar Gala {
110a4c9e328SKumar Gala 	struct pci_controller *phb;
111a4c9e328SKumar Gala 
112e60516e3SStephen Rothwell 	phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
113a4c9e328SKumar Gala 	if (phb == NULL)
114a4c9e328SKumar Gala 		return NULL;
115e60516e3SStephen Rothwell 	spin_lock(&hose_spinlock);
11663a72284SGuilherme G. Piccoli 	phb->global_number = get_phb_number(dev);
117e60516e3SStephen Rothwell 	list_add_tail(&phb->list_node, &hose_list);
118e60516e3SStephen Rothwell 	spin_unlock(&hose_spinlock);
11944ef3390SStephen Rothwell 	phb->dn = dev;
120f691fa10SMichael Ellerman 	phb->is_dynamic = slab_is_available();
121a4c9e328SKumar Gala #ifdef CONFIG_PPC64
122a4c9e328SKumar Gala 	if (dev) {
123a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
124a4c9e328SKumar Gala 
125a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
12698fa15f3SAnshuman Khandual 			nid = NUMA_NO_NODE;
127a4c9e328SKumar Gala 
128a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
129a4c9e328SKumar Gala 	}
130a4c9e328SKumar Gala #endif
131a4c9e328SKumar Gala 	return phb;
132a4c9e328SKumar Gala }
1335b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_alloc_controller);
134a4c9e328SKumar Gala 
135a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
136a4c9e328SKumar Gala {
137a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
13863a72284SGuilherme G. Piccoli 
13963a72284SGuilherme G. Piccoli 	/* Clear bit of phb_bitmap to allow reuse of this PHB number. */
14063a72284SGuilherme G. Piccoli 	if (phb->global_number < MAX_PHBS)
14163a72284SGuilherme G. Piccoli 		clear_bit(phb->global_number, phb_bitmap);
14263a72284SGuilherme G. Piccoli 
143a4c9e328SKumar Gala 	list_del(&phb->list_node);
144a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
145a4c9e328SKumar Gala 
146a4c9e328SKumar Gala 	if (phb->is_dynamic)
147a4c9e328SKumar Gala 		kfree(phb);
148a4c9e328SKumar Gala }
1496b8b252fSAndrew Donnellan EXPORT_SYMBOL_GPL(pcibios_free_controller);
150a4c9e328SKumar Gala 
1514c2245bbSGavin Shan /*
1522dd9c11bSMauricio Faria de Oliveira  * This function is used to call pcibios_free_controller()
1532dd9c11bSMauricio Faria de Oliveira  * in a deferred manner: a callback from the PCI subsystem.
1542dd9c11bSMauricio Faria de Oliveira  *
1552dd9c11bSMauricio Faria de Oliveira  * _*DO NOT*_ call pcibios_free_controller() explicitly if
1562dd9c11bSMauricio Faria de Oliveira  * this is used (or it may access an invalid *phb pointer).
1572dd9c11bSMauricio Faria de Oliveira  *
1582dd9c11bSMauricio Faria de Oliveira  * The callback occurs when all references to the root bus
1592dd9c11bSMauricio Faria de Oliveira  * are dropped (e.g., child buses/devices and their users).
1602dd9c11bSMauricio Faria de Oliveira  *
1612dd9c11bSMauricio Faria de Oliveira  * It's called as .release_fn() of 'struct pci_host_bridge'
1622dd9c11bSMauricio Faria de Oliveira  * which is associated with the 'struct pci_controller.bus'
1632dd9c11bSMauricio Faria de Oliveira  * (root bus) - it expects .release_data to hold a pointer
1642dd9c11bSMauricio Faria de Oliveira  * to 'struct pci_controller'.
1652dd9c11bSMauricio Faria de Oliveira  *
1662dd9c11bSMauricio Faria de Oliveira  * In order to use it, register .release_fn()/release_data
1672dd9c11bSMauricio Faria de Oliveira  * like this:
1682dd9c11bSMauricio Faria de Oliveira  *
1692dd9c11bSMauricio Faria de Oliveira  * pci_set_host_bridge_release(bridge,
1702dd9c11bSMauricio Faria de Oliveira  *                             pcibios_free_controller_deferred
1712dd9c11bSMauricio Faria de Oliveira  *                             (void *) phb);
1722dd9c11bSMauricio Faria de Oliveira  *
1732dd9c11bSMauricio Faria de Oliveira  * e.g. in the pcibios_root_bridge_prepare() callback from
1742dd9c11bSMauricio Faria de Oliveira  * pci_create_root_bus().
1752dd9c11bSMauricio Faria de Oliveira  */
1762dd9c11bSMauricio Faria de Oliveira void pcibios_free_controller_deferred(struct pci_host_bridge *bridge)
1772dd9c11bSMauricio Faria de Oliveira {
1782dd9c11bSMauricio Faria de Oliveira 	struct pci_controller *phb = (struct pci_controller *)
1792dd9c11bSMauricio Faria de Oliveira 					 bridge->release_data;
1802dd9c11bSMauricio Faria de Oliveira 
1812dd9c11bSMauricio Faria de Oliveira 	pr_debug("domain %d, dynamic %d\n", phb->global_number, phb->is_dynamic);
1822dd9c11bSMauricio Faria de Oliveira 
1832dd9c11bSMauricio Faria de Oliveira 	pcibios_free_controller(phb);
1842dd9c11bSMauricio Faria de Oliveira }
1852dd9c11bSMauricio Faria de Oliveira EXPORT_SYMBOL_GPL(pcibios_free_controller_deferred);
1862dd9c11bSMauricio Faria de Oliveira 
1872dd9c11bSMauricio Faria de Oliveira /*
1884c2245bbSGavin Shan  * The function is used to return the minimal alignment
1894c2245bbSGavin Shan  * for memory or I/O windows of the associated P2P bridge.
1904c2245bbSGavin Shan  * By default, 4KiB alignment for I/O windows and 1MiB for
1914c2245bbSGavin Shan  * memory windows.
1924c2245bbSGavin Shan  */
1934c2245bbSGavin Shan resource_size_t pcibios_window_alignment(struct pci_bus *bus,
1944c2245bbSGavin Shan 					 unsigned long type)
1954c2245bbSGavin Shan {
196467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(bus);
197467efc2eSDaniel Axtens 
198467efc2eSDaniel Axtens 	if (phb->controller_ops.window_alignment)
199467efc2eSDaniel Axtens 		return phb->controller_ops.window_alignment(bus, type);
200467efc2eSDaniel Axtens 
201467efc2eSDaniel Axtens 	/*
202467efc2eSDaniel Axtens 	 * PCI core will figure out the default
203467efc2eSDaniel Axtens 	 * alignment: 4KiB for I/O and 1MiB for
204467efc2eSDaniel Axtens 	 * memory window.
205467efc2eSDaniel Axtens 	 */
206467efc2eSDaniel Axtens 	return 1;
2074c2245bbSGavin Shan }
2084c2245bbSGavin Shan 
209c5fcb29aSGavin Shan void pcibios_setup_bridge(struct pci_bus *bus, unsigned long type)
210c5fcb29aSGavin Shan {
211c5fcb29aSGavin Shan 	struct pci_controller *hose = pci_bus_to_host(bus);
212c5fcb29aSGavin Shan 
213c5fcb29aSGavin Shan 	if (hose->controller_ops.setup_bridge)
214c5fcb29aSGavin Shan 		hose->controller_ops.setup_bridge(bus, type);
215c5fcb29aSGavin Shan }
216c5fcb29aSGavin Shan 
217d92a208dSGavin Shan void pcibios_reset_secondary_bus(struct pci_dev *dev)
218d92a208dSGavin Shan {
219467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
220467efc2eSDaniel Axtens 
221467efc2eSDaniel Axtens 	if (phb->controller_ops.reset_secondary_bus) {
222467efc2eSDaniel Axtens 		phb->controller_ops.reset_secondary_bus(dev);
223467efc2eSDaniel Axtens 		return;
224467efc2eSDaniel Axtens 	}
225467efc2eSDaniel Axtens 
226467efc2eSDaniel Axtens 	pci_reset_secondary_bus(dev);
227d92a208dSGavin Shan }
228d92a208dSGavin Shan 
22938274637SYongji Xie resource_size_t pcibios_default_alignment(void)
23038274637SYongji Xie {
23138274637SYongji Xie 	if (ppc_md.pcibios_default_alignment)
23238274637SYongji Xie 		return ppc_md.pcibios_default_alignment();
23338274637SYongji Xie 
23438274637SYongji Xie 	return 0;
23538274637SYongji Xie }
23638274637SYongji Xie 
2375350ab3fSWei Yang #ifdef CONFIG_PCI_IOV
2385350ab3fSWei Yang resource_size_t pcibios_iov_resource_alignment(struct pci_dev *pdev, int resno)
2395350ab3fSWei Yang {
2405350ab3fSWei Yang 	if (ppc_md.pcibios_iov_resource_alignment)
2415350ab3fSWei Yang 		return ppc_md.pcibios_iov_resource_alignment(pdev, resno);
2425350ab3fSWei Yang 
2435350ab3fSWei Yang 	return pci_iov_resource_size(pdev, resno);
2445350ab3fSWei Yang }
245988fc3baSBryant G. Ly 
246988fc3baSBryant G. Ly int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
247988fc3baSBryant G. Ly {
248988fc3baSBryant G. Ly 	if (ppc_md.pcibios_sriov_enable)
249988fc3baSBryant G. Ly 		return ppc_md.pcibios_sriov_enable(pdev, num_vfs);
250988fc3baSBryant G. Ly 
251988fc3baSBryant G. Ly 	return 0;
252988fc3baSBryant G. Ly }
253988fc3baSBryant G. Ly 
254988fc3baSBryant G. Ly int pcibios_sriov_disable(struct pci_dev *pdev)
255988fc3baSBryant G. Ly {
256988fc3baSBryant G. Ly 	if (ppc_md.pcibios_sriov_disable)
257988fc3baSBryant G. Ly 		return ppc_md.pcibios_sriov_disable(pdev);
258988fc3baSBryant G. Ly 
259988fc3baSBryant G. Ly 	return 0;
260988fc3baSBryant G. Ly }
261988fc3baSBryant G. Ly 
2625350ab3fSWei Yang #endif /* CONFIG_PCI_IOV */
2635350ab3fSWei Yang 
264c3bd517dSMilton Miller static resource_size_t pcibios_io_size(const struct pci_controller *hose)
265c3bd517dSMilton Miller {
266c3bd517dSMilton Miller #ifdef CONFIG_PPC64
267c3bd517dSMilton Miller 	return hose->pci_io_size;
268c3bd517dSMilton Miller #else
26928f65c11SJoe Perches 	return resource_size(&hose->io_resource);
270c3bd517dSMilton Miller #endif
271c3bd517dSMilton Miller }
272c3bd517dSMilton Miller 
2736dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
2746dfbde20SBenjamin Herrenschmidt {
2756dfbde20SBenjamin Herrenschmidt 	int ret = 0;
2766dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
277c3bd517dSMilton Miller 	resource_size_t size;
2786dfbde20SBenjamin Herrenschmidt 
2796dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
2806dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
281c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
2826dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
2836dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
2846dfbde20SBenjamin Herrenschmidt 			ret = 1;
2856dfbde20SBenjamin Herrenschmidt 			break;
2866dfbde20SBenjamin Herrenschmidt 		}
2876dfbde20SBenjamin Herrenschmidt 	}
2886dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
2896dfbde20SBenjamin Herrenschmidt 	return ret;
2906dfbde20SBenjamin Herrenschmidt }
2916dfbde20SBenjamin Herrenschmidt 
292c3bd517dSMilton Miller unsigned long pci_address_to_pio(phys_addr_t address)
293c3bd517dSMilton Miller {
294c3bd517dSMilton Miller 	struct pci_controller *hose;
295c3bd517dSMilton Miller 	resource_size_t size;
296c3bd517dSMilton Miller 	unsigned long ret = ~0;
297c3bd517dSMilton Miller 
298c3bd517dSMilton Miller 	spin_lock(&hose_spinlock);
299c3bd517dSMilton Miller 	list_for_each_entry(hose, &hose_list, list_node) {
300c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
301c3bd517dSMilton Miller 		if (address >= hose->io_base_phys &&
302c3bd517dSMilton Miller 		    address < (hose->io_base_phys + size)) {
303c3bd517dSMilton Miller 			unsigned long base =
304c3bd517dSMilton Miller 				(unsigned long)hose->io_base_virt - _IO_BASE;
305c3bd517dSMilton Miller 			ret = base + (address - hose->io_base_phys);
306c3bd517dSMilton Miller 			break;
307c3bd517dSMilton Miller 		}
308c3bd517dSMilton Miller 	}
309c3bd517dSMilton Miller 	spin_unlock(&hose_spinlock);
310c3bd517dSMilton Miller 
311c3bd517dSMilton Miller 	return ret;
312c3bd517dSMilton Miller }
313c3bd517dSMilton Miller EXPORT_SYMBOL_GPL(pci_address_to_pio);
314c3bd517dSMilton Miller 
3155516b540SKumar Gala /*
3165516b540SKumar Gala  * Return the domain number for this bus.
3175516b540SKumar Gala  */
3185516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
3195516b540SKumar Gala {
3205516b540SKumar Gala 	struct pci_controller *hose = pci_bus_to_host(bus);
3215516b540SKumar Gala 
3225516b540SKumar Gala 	return hose->global_number;
3235516b540SKumar Gala }
3245516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
32558083dadSKumar Gala 
326a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
327a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
328a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
329a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
330a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
331a4c9e328SKumar Gala  * config cycles.
332a4c9e328SKumar Gala  */
333a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
334a4c9e328SKumar Gala {
335a4c9e328SKumar Gala 	while(node) {
336a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
337a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
33844ef3390SStephen Rothwell 			if (hose->dn == node)
339a4c9e328SKumar Gala 				return hose;
340a4c9e328SKumar Gala 		node = node->parent;
341a4c9e328SKumar Gala 	}
342a4c9e328SKumar Gala 	return NULL;
343a4c9e328SKumar Gala }
344a4c9e328SKumar Gala 
34567060cb1SOliver O'Halloran struct pci_controller *pci_find_controller_for_domain(int domain_nr)
34667060cb1SOliver O'Halloran {
34767060cb1SOliver O'Halloran 	struct pci_controller *hose;
34867060cb1SOliver O'Halloran 
34967060cb1SOliver O'Halloran 	list_for_each_entry(hose, &hose_list, list_node)
35067060cb1SOliver O'Halloran 		if (hose->global_number == domain_nr)
35167060cb1SOliver O'Halloran 			return hose;
35267060cb1SOliver O'Halloran 
35367060cb1SOliver O'Halloran 	return NULL;
35467060cb1SOliver O'Halloran }
35567060cb1SOliver O'Halloran 
35658083dadSKumar Gala /*
35758083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
35858083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
35958083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
36058083dadSKumar Gala  */
3614666ca2aSBenjamin Herrenschmidt static int pci_read_irq_line(struct pci_dev *pci_dev)
36258083dadSKumar Gala {
363c591c2e3SAlexey Kardashevskiy 	int virq;
36458083dadSKumar Gala 
365b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
36658083dadSKumar Gala 
36758083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
368c591c2e3SAlexey Kardashevskiy 	virq = of_irq_parse_and_map_pci(pci_dev, 0, 0);
369c591c2e3SAlexey Kardashevskiy 	if (virq <= 0) {
37058083dadSKumar Gala 		u8 line, pin;
37158083dadSKumar Gala 
37258083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
37358083dadSKumar Gala 		 * space and map that through the default controller. We
37458083dadSKumar Gala 		 * also set the type to level low since that's what PCI
37558083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
37658083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
37758083dadSKumar Gala 		 * function.
37858083dadSKumar Gala 		 */
37958083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
38058083dadSKumar Gala 			return -1;
38158083dadSKumar Gala 		if (pin == 0)
38258083dadSKumar Gala 			return -1;
38358083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
38454a24cbbSBenjamin Herrenschmidt 		    line == 0xff || line == 0) {
38558083dadSKumar Gala 			return -1;
38658083dadSKumar Gala 		}
387b0494bc8SBenjamin Herrenschmidt 		pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
38854a24cbbSBenjamin Herrenschmidt 			 line, pin);
38958083dadSKumar Gala 
39058083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
391ef24ba70SMichael Ellerman 		if (virq)
392ec775d0eSThomas Gleixner 			irq_set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
39358083dadSKumar Gala 	}
394ef24ba70SMichael Ellerman 
395ef24ba70SMichael Ellerman 	if (!virq) {
396b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Failed to map !\n");
39758083dadSKumar Gala 		return -1;
39858083dadSKumar Gala 	}
39958083dadSKumar Gala 
400b0494bc8SBenjamin Herrenschmidt 	pr_debug(" Mapped to linux irq %d\n", virq);
40158083dadSKumar Gala 
40258083dadSKumar Gala 	pci_dev->irq = virq;
40358083dadSKumar Gala 
40458083dadSKumar Gala 	return 0;
40558083dadSKumar Gala }
40658083dadSKumar Gala 
40758083dadSKumar Gala /*
40828f8f183SDavid Woodhouse  * Platform support for /proc/bus/pci/X/Y mmap()s.
40958083dadSKumar Gala  *  -- paulus.
41058083dadSKumar Gala  */
41128f8f183SDavid Woodhouse int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
41258083dadSKumar Gala {
41328f8f183SDavid Woodhouse 	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
41428f8f183SDavid Woodhouse 	resource_size_t ioaddr = pci_resource_start(pdev, bar);
41558083dadSKumar Gala 
41628f8f183SDavid Woodhouse 	if (!hose)
41728f8f183SDavid Woodhouse 		return -EINVAL;
41858083dadSKumar Gala 
41928f8f183SDavid Woodhouse 	/* Convert to an offset within this PCI controller */
42028f8f183SDavid Woodhouse 	ioaddr -= (unsigned long)hose->io_base_virt - _IO_BASE;
42158083dadSKumar Gala 
42228f8f183SDavid Woodhouse 	vma->vm_pgoff += (ioaddr + hose->io_base_phys) >> PAGE_SHIFT;
42328f8f183SDavid Woodhouse 	return 0;
42458083dadSKumar Gala }
42558083dadSKumar Gala 
42658083dadSKumar Gala /*
42758083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
42858083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
42958083dadSKumar Gala  * above routine
43058083dadSKumar Gala  */
43158083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
43258083dadSKumar Gala 				  unsigned long pfn,
43358083dadSKumar Gala 				  unsigned long size,
43464b3d0e8SBenjamin Herrenschmidt 				  pgprot_t prot)
43558083dadSKumar Gala {
43658083dadSKumar Gala 	struct pci_dev *pdev = NULL;
43758083dadSKumar Gala 	struct resource *found = NULL;
4387c12d906SBenjamin Herrenschmidt 	resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
43958083dadSKumar Gala 	int i;
44058083dadSKumar Gala 
44158083dadSKumar Gala 	if (page_is_ram(pfn))
44264b3d0e8SBenjamin Herrenschmidt 		return prot;
44358083dadSKumar Gala 
44464b3d0e8SBenjamin Herrenschmidt 	prot = pgprot_noncached(prot);
44558083dadSKumar Gala 	for_each_pci_dev(pdev) {
44658083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
44758083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
44858083dadSKumar Gala 			int flags = rp->flags;
44958083dadSKumar Gala 
45058083dadSKumar Gala 			/* Active and same type? */
45158083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
45258083dadSKumar Gala 				continue;
45358083dadSKumar Gala 			/* In the range of this resource? */
45458083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
45558083dadSKumar Gala 			    offset > rp->end)
45658083dadSKumar Gala 				continue;
45758083dadSKumar Gala 			found = rp;
45858083dadSKumar Gala 			break;
45958083dadSKumar Gala 		}
46058083dadSKumar Gala 		if (found)
46158083dadSKumar Gala 			break;
46258083dadSKumar Gala 	}
46358083dadSKumar Gala 	if (found) {
46458083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
46564b3d0e8SBenjamin Herrenschmidt 			prot = pgprot_noncached_wc(prot);
46658083dadSKumar Gala 		pci_dev_put(pdev);
46758083dadSKumar Gala 	}
46858083dadSKumar Gala 
469b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
47064b3d0e8SBenjamin Herrenschmidt 		 (unsigned long long)offset, pgprot_val(prot));
47158083dadSKumar Gala 
47264b3d0e8SBenjamin Herrenschmidt 	return prot;
47358083dadSKumar Gala }
47458083dadSKumar Gala 
475e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO read access on a bus */
476e9f82cb7SBenjamin Herrenschmidt int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
477e9f82cb7SBenjamin Herrenschmidt {
478e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
479e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
480e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
481e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
482e9f82cb7SBenjamin Herrenschmidt 
483e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
484e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
485e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
486e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
487e9f82cb7SBenjamin Herrenschmidt 	 */
488e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
489e9f82cb7SBenjamin Herrenschmidt 	offset += port;
490e9f82cb7SBenjamin Herrenschmidt 
491e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
492e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
493e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
494e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
495e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
496e9f82cb7SBenjamin Herrenschmidt 
497e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
498e9f82cb7SBenjamin Herrenschmidt 	case 1:
499e9f82cb7SBenjamin Herrenschmidt 		*((u8 *)val) = in_8(addr);
500e9f82cb7SBenjamin Herrenschmidt 		return 1;
501e9f82cb7SBenjamin Herrenschmidt 	case 2:
502e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
503e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
504e9f82cb7SBenjamin Herrenschmidt 		*((u16 *)val) = in_le16(addr);
505e9f82cb7SBenjamin Herrenschmidt 		return 2;
506e9f82cb7SBenjamin Herrenschmidt 	case 4:
507e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
508e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
509e9f82cb7SBenjamin Herrenschmidt 		*((u32 *)val) = in_le32(addr);
510e9f82cb7SBenjamin Herrenschmidt 		return 4;
511e9f82cb7SBenjamin Herrenschmidt 	}
512e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
513e9f82cb7SBenjamin Herrenschmidt }
514e9f82cb7SBenjamin Herrenschmidt 
515e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO write access on a bus */
516e9f82cb7SBenjamin Herrenschmidt int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
517e9f82cb7SBenjamin Herrenschmidt {
518e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
519e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
520e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
521e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
522e9f82cb7SBenjamin Herrenschmidt 
523e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
524e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
525e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
526e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
527e9f82cb7SBenjamin Herrenschmidt 	 */
528e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
529e9f82cb7SBenjamin Herrenschmidt 	offset += port;
530e9f82cb7SBenjamin Herrenschmidt 
531e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
532e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
533e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
534e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
535e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
536e9f82cb7SBenjamin Herrenschmidt 
537e9f82cb7SBenjamin Herrenschmidt 	/* WARNING: The generic code is idiotic. It gets passed a pointer
538e9f82cb7SBenjamin Herrenschmidt 	 * to what can be a 1, 2 or 4 byte quantity and always reads that
539e9f82cb7SBenjamin Herrenschmidt 	 * as a u32, which means that we have to correct the location of
540e9f82cb7SBenjamin Herrenschmidt 	 * the data read within those 32 bits for size 1 and 2
541e9f82cb7SBenjamin Herrenschmidt 	 */
542e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
543e9f82cb7SBenjamin Herrenschmidt 	case 1:
544e9f82cb7SBenjamin Herrenschmidt 		out_8(addr, val >> 24);
545e9f82cb7SBenjamin Herrenschmidt 		return 1;
546e9f82cb7SBenjamin Herrenschmidt 	case 2:
547e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
548e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
549e9f82cb7SBenjamin Herrenschmidt 		out_le16(addr, val >> 16);
550e9f82cb7SBenjamin Herrenschmidt 		return 2;
551e9f82cb7SBenjamin Herrenschmidt 	case 4:
552e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
553e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
554e9f82cb7SBenjamin Herrenschmidt 		out_le32(addr, val);
555e9f82cb7SBenjamin Herrenschmidt 		return 4;
556e9f82cb7SBenjamin Herrenschmidt 	}
557e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
558e9f82cb7SBenjamin Herrenschmidt }
559e9f82cb7SBenjamin Herrenschmidt 
560e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO or memory mmap access on a bus */
561e9f82cb7SBenjamin Herrenschmidt int pci_mmap_legacy_page_range(struct pci_bus *bus,
562e9f82cb7SBenjamin Herrenschmidt 			       struct vm_area_struct *vma,
563e9f82cb7SBenjamin Herrenschmidt 			       enum pci_mmap_state mmap_state)
564e9f82cb7SBenjamin Herrenschmidt {
565e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
566e9f82cb7SBenjamin Herrenschmidt 	resource_size_t offset =
567e9f82cb7SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
568e9f82cb7SBenjamin Herrenschmidt 	resource_size_t size = vma->vm_end - vma->vm_start;
569e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp;
570e9f82cb7SBenjamin Herrenschmidt 
571e9f82cb7SBenjamin Herrenschmidt 	pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
572e9f82cb7SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number,
573e9f82cb7SBenjamin Herrenschmidt 		 mmap_state == pci_mmap_mem ? "MEM" : "IO",
574e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)offset,
575e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)(offset + size - 1));
576e9f82cb7SBenjamin Herrenschmidt 
577e9f82cb7SBenjamin Herrenschmidt 	if (mmap_state == pci_mmap_mem) {
5785b11abfdSBenjamin Herrenschmidt 		/* Hack alert !
5795b11abfdSBenjamin Herrenschmidt 		 *
5805b11abfdSBenjamin Herrenschmidt 		 * Because X is lame and can fail starting if it gets an error trying
5815b11abfdSBenjamin Herrenschmidt 		 * to mmap legacy_mem (instead of just moving on without legacy memory
5825b11abfdSBenjamin Herrenschmidt 		 * access) we fake it here by giving it anonymous memory, effectively
5835b11abfdSBenjamin Herrenschmidt 		 * behaving just like /dev/zero
5845b11abfdSBenjamin Herrenschmidt 		 */
5855b11abfdSBenjamin Herrenschmidt 		if ((offset + size) > hose->isa_mem_size) {
5865b11abfdSBenjamin Herrenschmidt 			printk(KERN_DEBUG
5875b11abfdSBenjamin Herrenschmidt 			       "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
5885b11abfdSBenjamin Herrenschmidt 			       current->comm, current->pid, pci_domain_nr(bus), bus->number);
5895b11abfdSBenjamin Herrenschmidt 			if (vma->vm_flags & VM_SHARED)
5905b11abfdSBenjamin Herrenschmidt 				return shmem_zero_setup(vma);
5915b11abfdSBenjamin Herrenschmidt 			return 0;
5925b11abfdSBenjamin Herrenschmidt 		}
593e9f82cb7SBenjamin Herrenschmidt 		offset += hose->isa_mem_phys;
594e9f82cb7SBenjamin Herrenschmidt 	} else {
595e9f82cb7SBenjamin Herrenschmidt 		unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
596e9f82cb7SBenjamin Herrenschmidt 		unsigned long roffset = offset + io_offset;
597e9f82cb7SBenjamin Herrenschmidt 		rp = &hose->io_resource;
598e9f82cb7SBenjamin Herrenschmidt 		if (!(rp->flags & IORESOURCE_IO))
599e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
600e9f82cb7SBenjamin Herrenschmidt 		if (roffset < rp->start || (roffset + size) > rp->end)
601e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
602e9f82cb7SBenjamin Herrenschmidt 		offset += hose->io_base_phys;
603e9f82cb7SBenjamin Herrenschmidt 	}
604e9f82cb7SBenjamin Herrenschmidt 	pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
605e9f82cb7SBenjamin Herrenschmidt 
606e9f82cb7SBenjamin Herrenschmidt 	vma->vm_pgoff = offset >> PAGE_SHIFT;
60764b3d0e8SBenjamin Herrenschmidt 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
608e9f82cb7SBenjamin Herrenschmidt 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
609e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_end - vma->vm_start,
610e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_page_prot);
611e9f82cb7SBenjamin Herrenschmidt }
612e9f82cb7SBenjamin Herrenschmidt 
61358083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
61458083dadSKumar Gala 			  const struct resource *rsrc,
61558083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
61658083dadSKumar Gala {
61738301358SBjorn Helgaas 	struct pci_bus_region region;
61858083dadSKumar Gala 
61938301358SBjorn Helgaas 	if (rsrc->flags & IORESOURCE_IO) {
62038301358SBjorn Helgaas 		pcibios_resource_to_bus(dev->bus, &region,
62138301358SBjorn Helgaas 					(struct resource *) rsrc);
62238301358SBjorn Helgaas 		*start = region.start;
62338301358SBjorn Helgaas 		*end = region.end;
62458083dadSKumar Gala 		return;
62538301358SBjorn Helgaas 	}
62658083dadSKumar Gala 
62738301358SBjorn Helgaas 	/* We pass a CPU physical address to userland for MMIO instead of a
62838301358SBjorn Helgaas 	 * BAR value because X is lame and expects to be able to use that
62958083dadSKumar Gala 	 * to pass to /dev/mem!
63058083dadSKumar Gala 	 *
63138301358SBjorn Helgaas 	 * That means we may have 64-bit values where some apps only expect
63238301358SBjorn Helgaas 	 * 32 (like X itself since it thinks only Sparc has 64-bit MMIO).
63358083dadSKumar Gala 	 */
63438301358SBjorn Helgaas 	*start = rsrc->start;
63538301358SBjorn Helgaas 	*end = rsrc->end;
63658083dadSKumar Gala }
63713dccb9eSBenjamin Herrenschmidt 
63813dccb9eSBenjamin Herrenschmidt /**
63913dccb9eSBenjamin Herrenschmidt  * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
64013dccb9eSBenjamin Herrenschmidt  * @hose: newly allocated pci_controller to be setup
64113dccb9eSBenjamin Herrenschmidt  * @dev: device node of the host bridge
64213dccb9eSBenjamin Herrenschmidt  * @primary: set if primary bus (32 bits only, soon to be deprecated)
64313dccb9eSBenjamin Herrenschmidt  *
64413dccb9eSBenjamin Herrenschmidt  * This function will parse the "ranges" property of a PCI host bridge device
64513dccb9eSBenjamin Herrenschmidt  * node and setup the resource mapping of a pci controller based on its
64613dccb9eSBenjamin Herrenschmidt  * content.
64713dccb9eSBenjamin Herrenschmidt  *
64813dccb9eSBenjamin Herrenschmidt  * Life would be boring if it wasn't for a few issues that we have to deal
64913dccb9eSBenjamin Herrenschmidt  * with here:
65013dccb9eSBenjamin Herrenschmidt  *
65113dccb9eSBenjamin Herrenschmidt  *   - We can only cope with one IO space range and up to 3 Memory space
65213dccb9eSBenjamin Herrenschmidt  *     ranges. However, some machines (thanks Apple !) tend to split their
65313dccb9eSBenjamin Herrenschmidt  *     space into lots of small contiguous ranges. So we have to coalesce.
65413dccb9eSBenjamin Herrenschmidt  *
65513dccb9eSBenjamin Herrenschmidt  *   - Some busses have IO space not starting at 0, which causes trouble with
65613dccb9eSBenjamin Herrenschmidt  *     the way we do our IO resource renumbering. The code somewhat deals with
65713dccb9eSBenjamin Herrenschmidt  *     it for 64 bits but I would expect problems on 32 bits.
65813dccb9eSBenjamin Herrenschmidt  *
65913dccb9eSBenjamin Herrenschmidt  *   - Some 32 bits platforms such as 4xx can have physical space larger than
66013dccb9eSBenjamin Herrenschmidt  *     32 bits so we need to use 64 bits values for the parsing
66113dccb9eSBenjamin Herrenschmidt  */
662cad5cef6SGreg Kroah-Hartman void pci_process_bridge_OF_ranges(struct pci_controller *hose,
663cad5cef6SGreg Kroah-Hartman 				  struct device_node *dev, int primary)
66413dccb9eSBenjamin Herrenschmidt {
665858957abSKevin Hao 	int memno = 0;
66613dccb9eSBenjamin Herrenschmidt 	struct resource *res;
667654837e8SAndrew Murray 	struct of_pci_range range;
668654837e8SAndrew Murray 	struct of_pci_range_parser parser;
66913dccb9eSBenjamin Herrenschmidt 
670b7c670d6SRob Herring 	printk(KERN_INFO "PCI host bridge %pOF %s ranges:\n",
671b7c670d6SRob Herring 	       dev, primary ? "(primary)" : "");
67213dccb9eSBenjamin Herrenschmidt 
673654837e8SAndrew Murray 	/* Check for ranges property */
674654837e8SAndrew Murray 	if (of_pci_range_parser_init(&parser, dev))
67513dccb9eSBenjamin Herrenschmidt 		return;
67613dccb9eSBenjamin Herrenschmidt 
67713dccb9eSBenjamin Herrenschmidt 	/* Parse it */
678654837e8SAndrew Murray 	for_each_of_pci_range(&parser, &range) {
679e9f82cb7SBenjamin Herrenschmidt 		/* If we failed translation or got a zero-sized region
680e9f82cb7SBenjamin Herrenschmidt 		 * (some FW try to feed us with non sensical zero sized regions
681e9f82cb7SBenjamin Herrenschmidt 		 * such as power3 which look like some kind of attempt at exposing
682e9f82cb7SBenjamin Herrenschmidt 		 * the VGA memory hole)
683e9f82cb7SBenjamin Herrenschmidt 		 */
684654837e8SAndrew Murray 		if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
68513dccb9eSBenjamin Herrenschmidt 			continue;
68613dccb9eSBenjamin Herrenschmidt 
68713dccb9eSBenjamin Herrenschmidt 		/* Act based on address space type */
68813dccb9eSBenjamin Herrenschmidt 		res = NULL;
689654837e8SAndrew Murray 		switch (range.flags & IORESOURCE_TYPE_BITS) {
690654837e8SAndrew Murray 		case IORESOURCE_IO:
69113dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
69213dccb9eSBenjamin Herrenschmidt 			       "  IO 0x%016llx..0x%016llx -> 0x%016llx\n",
693654837e8SAndrew Murray 			       range.cpu_addr, range.cpu_addr + range.size - 1,
694654837e8SAndrew Murray 			       range.pci_addr);
69513dccb9eSBenjamin Herrenschmidt 
69613dccb9eSBenjamin Herrenschmidt 			/* We support only one IO range */
69713dccb9eSBenjamin Herrenschmidt 			if (hose->pci_io_size) {
69813dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
69913dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
70013dccb9eSBenjamin Herrenschmidt 				continue;
70113dccb9eSBenjamin Herrenschmidt 			}
70213dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32
70313dccb9eSBenjamin Herrenschmidt 			/* On 32 bits, limit I/O space to 16MB */
704654837e8SAndrew Murray 			if (range.size > 0x01000000)
705654837e8SAndrew Murray 				range.size = 0x01000000;
70613dccb9eSBenjamin Herrenschmidt 
70713dccb9eSBenjamin Herrenschmidt 			/* 32 bits needs to map IOs here */
708654837e8SAndrew Murray 			hose->io_base_virt = ioremap(range.cpu_addr,
709654837e8SAndrew Murray 						range.size);
71013dccb9eSBenjamin Herrenschmidt 
71113dccb9eSBenjamin Herrenschmidt 			/* Expect trouble if pci_addr is not 0 */
71213dccb9eSBenjamin Herrenschmidt 			if (primary)
71313dccb9eSBenjamin Herrenschmidt 				isa_io_base =
71413dccb9eSBenjamin Herrenschmidt 					(unsigned long)hose->io_base_virt;
71513dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
71613dccb9eSBenjamin Herrenschmidt 			/* pci_io_size and io_base_phys always represent IO
71713dccb9eSBenjamin Herrenschmidt 			 * space starting at 0 so we factor in pci_addr
71813dccb9eSBenjamin Herrenschmidt 			 */
719654837e8SAndrew Murray 			hose->pci_io_size = range.pci_addr + range.size;
720654837e8SAndrew Murray 			hose->io_base_phys = range.cpu_addr - range.pci_addr;
72113dccb9eSBenjamin Herrenschmidt 
72213dccb9eSBenjamin Herrenschmidt 			/* Build resource */
72313dccb9eSBenjamin Herrenschmidt 			res = &hose->io_resource;
724654837e8SAndrew Murray 			range.cpu_addr = range.pci_addr;
72513dccb9eSBenjamin Herrenschmidt 			break;
726654837e8SAndrew Murray 		case IORESOURCE_MEM:
72713dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
72813dccb9eSBenjamin Herrenschmidt 			       " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
729654837e8SAndrew Murray 			       range.cpu_addr, range.cpu_addr + range.size - 1,
730654837e8SAndrew Murray 			       range.pci_addr,
731*6a9166b5SRob Herring 			       (range.flags & IORESOURCE_PREFETCH) ?
732654837e8SAndrew Murray 			       "Prefetch" : "");
73313dccb9eSBenjamin Herrenschmidt 
73413dccb9eSBenjamin Herrenschmidt 			/* We support only 3 memory ranges */
73513dccb9eSBenjamin Herrenschmidt 			if (memno >= 3) {
73613dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
73713dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
73813dccb9eSBenjamin Herrenschmidt 				continue;
73913dccb9eSBenjamin Herrenschmidt 			}
74013dccb9eSBenjamin Herrenschmidt 			/* Handles ISA memory hole space here */
741654837e8SAndrew Murray 			if (range.pci_addr == 0) {
74213dccb9eSBenjamin Herrenschmidt 				if (primary || isa_mem_base == 0)
743654837e8SAndrew Murray 					isa_mem_base = range.cpu_addr;
744654837e8SAndrew Murray 				hose->isa_mem_phys = range.cpu_addr;
745654837e8SAndrew Murray 				hose->isa_mem_size = range.size;
74613dccb9eSBenjamin Herrenschmidt 			}
74713dccb9eSBenjamin Herrenschmidt 
74813dccb9eSBenjamin Herrenschmidt 			/* Build resource */
749654837e8SAndrew Murray 			hose->mem_offset[memno] = range.cpu_addr -
750654837e8SAndrew Murray 							range.pci_addr;
75113dccb9eSBenjamin Herrenschmidt 			res = &hose->mem_resources[memno++];
75213dccb9eSBenjamin Herrenschmidt 			break;
75313dccb9eSBenjamin Herrenschmidt 		}
75413dccb9eSBenjamin Herrenschmidt 		if (res != NULL) {
755aeba3731SMichael Ellerman 			res->name = dev->full_name;
756aeba3731SMichael Ellerman 			res->flags = range.flags;
757aeba3731SMichael Ellerman 			res->start = range.cpu_addr;
758aeba3731SMichael Ellerman 			res->end = range.cpu_addr + range.size - 1;
759aeba3731SMichael Ellerman 			res->parent = res->child = res->sibling = NULL;
76013dccb9eSBenjamin Herrenschmidt 		}
76113dccb9eSBenjamin Herrenschmidt 	}
76213dccb9eSBenjamin Herrenschmidt }
763fa462f2dSBenjamin Herrenschmidt 
764fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */
765fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus)
766fa462f2dSBenjamin Herrenschmidt {
767fa462f2dSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
7681fd0f525SBenjamin Herrenschmidt 
7690e47ff1cSRob Herring 	if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
770fa462f2dSBenjamin Herrenschmidt 		return 0;
7710e47ff1cSRob Herring 	if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
772fa462f2dSBenjamin Herrenschmidt 		return hose->global_number != 0;
773fa462f2dSBenjamin Herrenschmidt 	return 1;
774fa462f2dSBenjamin Herrenschmidt }
775fa462f2dSBenjamin Herrenschmidt 
776d82fb31aSKleber Sacilotto de Souza int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
777d82fb31aSKleber Sacilotto de Souza {
778d82fb31aSKleber Sacilotto de Souza 	if (ppc_md.pcibios_root_bridge_prepare)
779d82fb31aSKleber Sacilotto de Souza 		return ppc_md.pcibios_root_bridge_prepare(bridge);
780d82fb31aSKleber Sacilotto de Souza 
781d82fb31aSKleber Sacilotto de Souza 	return 0;
782d82fb31aSKleber Sacilotto de Souza }
783d82fb31aSKleber Sacilotto de Souza 
784bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are
785bf5e2ba2SBenjamin Herrenschmidt  * probed, but not for bridge ranges
786bf5e2ba2SBenjamin Herrenschmidt  */
787cad5cef6SGreg Kroah-Hartman static void pcibios_fixup_resources(struct pci_dev *dev)
788bf5e2ba2SBenjamin Herrenschmidt {
789bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
790bf5e2ba2SBenjamin Herrenschmidt 	int i;
791bf5e2ba2SBenjamin Herrenschmidt 
792bf5e2ba2SBenjamin Herrenschmidt 	if (!hose) {
793bf5e2ba2SBenjamin Herrenschmidt 		printk(KERN_ERR "No host bridge for PCI dev %s !\n",
794bf5e2ba2SBenjamin Herrenschmidt 		       pci_name(dev));
795bf5e2ba2SBenjamin Herrenschmidt 		return;
796bf5e2ba2SBenjamin Herrenschmidt 	}
797c3b80fb0SWei Yang 
798c3b80fb0SWei Yang 	if (dev->is_virtfn)
799c3b80fb0SWei Yang 		return;
800c3b80fb0SWei Yang 
801bf5e2ba2SBenjamin Herrenschmidt 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
802bf5e2ba2SBenjamin Herrenschmidt 		struct resource *res = dev->resource + i;
803c5df457fSKevin Hao 		struct pci_bus_region reg;
804bf5e2ba2SBenjamin Herrenschmidt 		if (!res->flags)
805bf5e2ba2SBenjamin Herrenschmidt 			continue;
80648c2ce97SBenjamin Herrenschmidt 
80748c2ce97SBenjamin Herrenschmidt 		/* If we're going to re-assign everything, we mark all resources
80848c2ce97SBenjamin Herrenschmidt 		 * as unset (and 0-base them). In addition, we mark BARs starting
80948c2ce97SBenjamin Herrenschmidt 		 * at 0 as unset as well, except if PCI_PROBE_ONLY is also set
81048c2ce97SBenjamin Herrenschmidt 		 * since in that case, we don't want to re-assign anything
8117f172890SBenjamin Herrenschmidt 		 */
812fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &reg, res);
81348c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC) ||
814c5df457fSKevin Hao 		    (reg.start == 0 && !pci_has_flag(PCI_PROBE_ONLY))) {
81548c2ce97SBenjamin Herrenschmidt 			/* Only print message if not re-assigning */
81648c2ce97SBenjamin Herrenschmidt 			if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC))
817ae2a84b4SKevin Hao 				pr_debug("PCI:%s Resource %d %pR is unassigned\n",
818ae2a84b4SKevin Hao 					 pci_name(dev), i, res);
819bf5e2ba2SBenjamin Herrenschmidt 			res->end -= res->start;
820bf5e2ba2SBenjamin Herrenschmidt 			res->start = 0;
821bf5e2ba2SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
822bf5e2ba2SBenjamin Herrenschmidt 			continue;
823bf5e2ba2SBenjamin Herrenschmidt 		}
824bf5e2ba2SBenjamin Herrenschmidt 
825ae2a84b4SKevin Hao 		pr_debug("PCI:%s Resource %d %pR\n", pci_name(dev), i, res);
826bf5e2ba2SBenjamin Herrenschmidt 	}
827bf5e2ba2SBenjamin Herrenschmidt 
828bf5e2ba2SBenjamin Herrenschmidt 	/* Call machine specific resource fixup */
829bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_resources)
830bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_resources(dev);
831bf5e2ba2SBenjamin Herrenschmidt }
832bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
833bf5e2ba2SBenjamin Herrenschmidt 
834b5561511SBenjamin Herrenschmidt /* This function tries to figure out if a bridge resource has been initialized
835b5561511SBenjamin Herrenschmidt  * by the firmware or not. It doesn't have to be absolutely bullet proof, but
836b5561511SBenjamin Herrenschmidt  * things go more smoothly when it gets it right. It should covers cases such
837b5561511SBenjamin Herrenschmidt  * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
838b5561511SBenjamin Herrenschmidt  */
839cad5cef6SGreg Kroah-Hartman static int pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
840b5561511SBenjamin Herrenschmidt 						 struct resource *res)
841bf5e2ba2SBenjamin Herrenschmidt {
842be8cbcd8SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
843bf5e2ba2SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
844b5561511SBenjamin Herrenschmidt 	resource_size_t offset;
8453fd47f06SBenjamin Herrenschmidt 	struct pci_bus_region region;
846b5561511SBenjamin Herrenschmidt 	u16 command;
847b5561511SBenjamin Herrenschmidt 	int i;
848bf5e2ba2SBenjamin Herrenschmidt 
849b5561511SBenjamin Herrenschmidt 	/* We don't do anything if PCI_PROBE_ONLY is set */
8500e47ff1cSRob Herring 	if (pci_has_flag(PCI_PROBE_ONLY))
851b5561511SBenjamin Herrenschmidt 		return 0;
852bf5e2ba2SBenjamin Herrenschmidt 
853b5561511SBenjamin Herrenschmidt 	/* Job is a bit different between memory and IO */
854b5561511SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_MEM) {
855fc279850SYinghai Lu 		pcibios_resource_to_bus(dev->bus, &region, res);
8563fd47f06SBenjamin Herrenschmidt 
8573fd47f06SBenjamin Herrenschmidt 		/* If the BAR is non-0 then it's probably been initialized */
8583fd47f06SBenjamin Herrenschmidt 		if (region.start != 0)
859b5561511SBenjamin Herrenschmidt 			return 0;
860b5561511SBenjamin Herrenschmidt 
861b5561511SBenjamin Herrenschmidt 		/* The BAR is 0, let's check if memory decoding is enabled on
862b5561511SBenjamin Herrenschmidt 		 * the bridge. If not, we consider it unassigned
863b5561511SBenjamin Herrenschmidt 		 */
864b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
865b5561511SBenjamin Herrenschmidt 		if ((command & PCI_COMMAND_MEMORY) == 0)
866b5561511SBenjamin Herrenschmidt 			return 1;
867b5561511SBenjamin Herrenschmidt 
868b5561511SBenjamin Herrenschmidt 		/* Memory decoding is enabled and the BAR is 0. If any of the bridge
869b5561511SBenjamin Herrenschmidt 		 * resources covers that starting address (0 then it's good enough for
8703fd47f06SBenjamin Herrenschmidt 		 * us for memory space)
871b5561511SBenjamin Herrenschmidt 		 */
872b5561511SBenjamin Herrenschmidt 		for (i = 0; i < 3; i++) {
873b5561511SBenjamin Herrenschmidt 			if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
8743fd47f06SBenjamin Herrenschmidt 			    hose->mem_resources[i].start == hose->mem_offset[i])
875b5561511SBenjamin Herrenschmidt 				return 0;
876b5561511SBenjamin Herrenschmidt 		}
877b5561511SBenjamin Herrenschmidt 
878b5561511SBenjamin Herrenschmidt 		/* Well, it starts at 0 and we know it will collide so we may as
879b5561511SBenjamin Herrenschmidt 		 * well consider it as unassigned. That covers the Apple case.
880b5561511SBenjamin Herrenschmidt 		 */
881b5561511SBenjamin Herrenschmidt 		return 1;
882b5561511SBenjamin Herrenschmidt 	} else {
883b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0, then we consider it assigned */
884b5561511SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
885b5561511SBenjamin Herrenschmidt 		if (((res->start - offset) & 0xfffffffful) != 0)
886b5561511SBenjamin Herrenschmidt 			return 0;
887b5561511SBenjamin Herrenschmidt 
888b5561511SBenjamin Herrenschmidt 		/* Here, we are a bit different than memory as typically IO space
889b5561511SBenjamin Herrenschmidt 		 * starting at low addresses -is- valid. What we do instead if that
890b5561511SBenjamin Herrenschmidt 		 * we consider as unassigned anything that doesn't have IO enabled
891b5561511SBenjamin Herrenschmidt 		 * in the PCI command register, and that's it.
892b5561511SBenjamin Herrenschmidt 		 */
893b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
894b5561511SBenjamin Herrenschmidt 		if (command & PCI_COMMAND_IO)
895b5561511SBenjamin Herrenschmidt 			return 0;
896b5561511SBenjamin Herrenschmidt 
897b5561511SBenjamin Herrenschmidt 		/* It's starting at 0 and IO is disabled in the bridge, consider
898b5561511SBenjamin Herrenschmidt 		 * it unassigned
899b5561511SBenjamin Herrenschmidt 		 */
900b5561511SBenjamin Herrenschmidt 		return 1;
901b5561511SBenjamin Herrenschmidt 	}
902b5561511SBenjamin Herrenschmidt }
903b5561511SBenjamin Herrenschmidt 
904b5561511SBenjamin Herrenschmidt /* Fixup resources of a PCI<->PCI bridge */
905cad5cef6SGreg Kroah-Hartman static void pcibios_fixup_bridge(struct pci_bus *bus)
906b5561511SBenjamin Herrenschmidt {
907bf5e2ba2SBenjamin Herrenschmidt 	struct resource *res;
908bf5e2ba2SBenjamin Herrenschmidt 	int i;
909bf5e2ba2SBenjamin Herrenschmidt 
910b5561511SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
911b5561511SBenjamin Herrenschmidt 
91289a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
91389a74eccSBjorn Helgaas 		if (!res || !res->flags)
914bf5e2ba2SBenjamin Herrenschmidt 			continue;
915b188b2aeSKumar Gala 		if (i >= 3 && bus->self->transparent)
916b188b2aeSKumar Gala 			continue;
917be8cbcd8SBenjamin Herrenschmidt 
918cf1a4cf8SGavin Shan 		/* If we're going to reassign everything, we can
919cf1a4cf8SGavin Shan 		 * shrink the P2P resource to have size as being
920cf1a4cf8SGavin Shan 		 * of 0 in order to save space.
92148c2ce97SBenjamin Herrenschmidt 		 */
92248c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
92348c2ce97SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
92448c2ce97SBenjamin Herrenschmidt 			res->start = 0;
925cf1a4cf8SGavin Shan 			res->end = -1;
92648c2ce97SBenjamin Herrenschmidt 			continue;
92748c2ce97SBenjamin Herrenschmidt 		}
92848c2ce97SBenjamin Herrenschmidt 
929ae2a84b4SKevin Hao 		pr_debug("PCI:%s Bus rsrc %d %pR\n", pci_name(dev), i, res);
930bf5e2ba2SBenjamin Herrenschmidt 
931b5561511SBenjamin Herrenschmidt 		/* Try to detect uninitialized P2P bridge resources,
932b5561511SBenjamin Herrenschmidt 		 * and clear them out so they get re-assigned later
933b5561511SBenjamin Herrenschmidt 		 */
934b5561511SBenjamin Herrenschmidt 		if (pcibios_uninitialized_bridge_resource(bus, res)) {
935b5561511SBenjamin Herrenschmidt 			res->flags = 0;
936b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            (unassigned)\n", pci_name(dev));
937bf5e2ba2SBenjamin Herrenschmidt 		}
938bf5e2ba2SBenjamin Herrenschmidt 	}
939b5561511SBenjamin Herrenschmidt }
940b5561511SBenjamin Herrenschmidt 
941cad5cef6SGreg Kroah-Hartman void pcibios_setup_bus_self(struct pci_bus *bus)
9428b8da358SBenjamin Herrenschmidt {
943467efc2eSDaniel Axtens 	struct pci_controller *phb;
944467efc2eSDaniel Axtens 
9457eef440aSBenjamin Herrenschmidt 	/* Fix up the bus resources for P2P bridges */
9468b8da358SBenjamin Herrenschmidt 	if (bus->self != NULL)
9478b8da358SBenjamin Herrenschmidt 		pcibios_fixup_bridge(bus);
9488b8da358SBenjamin Herrenschmidt 
9498b8da358SBenjamin Herrenschmidt 	/* Platform specific bus fixups. This is currently only used
9507eef440aSBenjamin Herrenschmidt 	 * by fsl_pci and I'm hoping to get rid of it at some point
9518b8da358SBenjamin Herrenschmidt 	 */
9528b8da358SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_bus)
9538b8da358SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_bus(bus);
9548b8da358SBenjamin Herrenschmidt 
9558b8da358SBenjamin Herrenschmidt 	/* Setup bus DMA mappings */
956467efc2eSDaniel Axtens 	phb = pci_bus_to_host(bus);
957467efc2eSDaniel Axtens 	if (phb->controller_ops.dma_bus_setup)
958467efc2eSDaniel Axtens 		phb->controller_ops.dma_bus_setup(bus);
9598b8da358SBenjamin Herrenschmidt }
9608b8da358SBenjamin Herrenschmidt 
9613ab3f3c9SOliver O'Halloran void pcibios_bus_add_device(struct pci_dev *dev)
9627eef440aSBenjamin Herrenschmidt {
963467efc2eSDaniel Axtens 	struct pci_controller *phb;
9647eef440aSBenjamin Herrenschmidt 	/* Fixup NUMA node as it may not be setup yet by the generic
9657eef440aSBenjamin Herrenschmidt 	 * code and is needed by the DMA init
9667eef440aSBenjamin Herrenschmidt 	 */
9677eef440aSBenjamin Herrenschmidt 	set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
9687eef440aSBenjamin Herrenschmidt 
9697eef440aSBenjamin Herrenschmidt 	/* Hook up default DMA ops */
970bc0df9ecSNishanth Aravamudan 	set_dma_ops(&dev->dev, pci_dma_ops);
9710617fc0cSChristoph Hellwig 	dev->dev.archdata.dma_offset = PCI_DRAM_OFFSET;
9727eef440aSBenjamin Herrenschmidt 
9737eef440aSBenjamin Herrenschmidt 	/* Additional platform DMA/iommu setup */
974467efc2eSDaniel Axtens 	phb = pci_bus_to_host(dev->bus);
975467efc2eSDaniel Axtens 	if (phb->controller_ops.dma_dev_setup)
976467efc2eSDaniel Axtens 		phb->controller_ops.dma_dev_setup(dev);
9777eef440aSBenjamin Herrenschmidt 
9787eef440aSBenjamin Herrenschmidt 	/* Read default IRQs and fixup if necessary */
9797eef440aSBenjamin Herrenschmidt 	pci_read_irq_line(dev);
9807eef440aSBenjamin Herrenschmidt 	if (ppc_md.pci_irq_fixup)
9817eef440aSBenjamin Herrenschmidt 		ppc_md.pci_irq_fixup(dev);
98230d87ef8SShawn Anastasio 
98330d87ef8SShawn Anastasio 	if (ppc_md.pcibios_bus_add_device)
9843ab3f3c9SOliver O'Halloran 		ppc_md.pcibios_bus_add_device(dev);
98530d87ef8SShawn Anastasio }
98630d87ef8SShawn Anastasio 
9877846de40SGuenter Roeck int pcibios_add_device(struct pci_dev *dev)
9887846de40SGuenter Roeck {
9896e628c7dSWei Yang #ifdef CONFIG_PCI_IOV
9906e628c7dSWei Yang 	if (ppc_md.pcibios_fixup_sriov)
9916e628c7dSWei Yang 		ppc_md.pcibios_fixup_sriov(dev);
9926e628c7dSWei Yang #endif /* CONFIG_PCI_IOV */
9936e628c7dSWei Yang 
9947846de40SGuenter Roeck 	return 0;
9957846de40SGuenter Roeck }
9967846de40SGuenter Roeck 
99779c8be83SMyron Stowe void pcibios_set_master(struct pci_dev *dev)
99879c8be83SMyron Stowe {
99979c8be83SMyron Stowe 	/* No special bus mastering setup handling */
100079c8be83SMyron Stowe }
100179c8be83SMyron Stowe 
1002cad5cef6SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus)
1003bf5e2ba2SBenjamin Herrenschmidt {
1004237865f1SBjorn Helgaas 	/* When called from the generic PCI probe, read PCI<->PCI bridge
1005237865f1SBjorn Helgaas 	 * bases. This is -not- called when generating the PCI tree from
1006237865f1SBjorn Helgaas 	 * the OF device-tree.
1007237865f1SBjorn Helgaas 	 */
1008237865f1SBjorn Helgaas 	pci_read_bridge_bases(bus);
1009237865f1SBjorn Helgaas 
1010237865f1SBjorn Helgaas 	/* Now fixup the bus bus */
10118b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_self(bus);
1012bf5e2ba2SBenjamin Herrenschmidt }
1013bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus);
1014bf5e2ba2SBenjamin Herrenschmidt 
10153fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev)
10163fd94c6bSBenjamin Herrenschmidt {
10170e47ff1cSRob Herring 	if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
10183fd94c6bSBenjamin Herrenschmidt 	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
10193fd94c6bSBenjamin Herrenschmidt 		return 1;
10203fd94c6bSBenjamin Herrenschmidt 	return 0;
10213fd94c6bSBenjamin Herrenschmidt }
10223fd94c6bSBenjamin Herrenschmidt 
10233fd94c6bSBenjamin Herrenschmidt /*
10243fd94c6bSBenjamin Herrenschmidt  * We need to avoid collisions with `mirrored' VGA ports
10253fd94c6bSBenjamin Herrenschmidt  * and other strange ISA hardware, so we always want the
10263fd94c6bSBenjamin Herrenschmidt  * addresses to be allocated in the 0x000-0x0ff region
10273fd94c6bSBenjamin Herrenschmidt  * modulo 0x400.
10283fd94c6bSBenjamin Herrenschmidt  *
10293fd94c6bSBenjamin Herrenschmidt  * Why? Because some silly external IO cards only decode
10303fd94c6bSBenjamin Herrenschmidt  * the low 10 bits of the IO address. The 0x00-0xff region
10313fd94c6bSBenjamin Herrenschmidt  * is reserved for motherboard devices that decode all 16
10323fd94c6bSBenjamin Herrenschmidt  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
10333fd94c6bSBenjamin Herrenschmidt  * but we want to try to avoid allocating at 0x2900-0x2bff
10343fd94c6bSBenjamin Herrenschmidt  * which might have be mirrored at 0x0100-0x03ff..
10353fd94c6bSBenjamin Herrenschmidt  */
10363b7a17fcSDominik Brodowski resource_size_t pcibios_align_resource(void *data, const struct resource *res,
10373fd94c6bSBenjamin Herrenschmidt 				resource_size_t size, resource_size_t align)
10383fd94c6bSBenjamin Herrenschmidt {
10393fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = data;
10403fd94c6bSBenjamin Herrenschmidt 	resource_size_t start = res->start;
10413fd94c6bSBenjamin Herrenschmidt 
1042b26b2d49SDominik Brodowski 	if (res->flags & IORESOURCE_IO) {
10433fd94c6bSBenjamin Herrenschmidt 		if (skip_isa_ioresource_align(dev))
1044b26b2d49SDominik Brodowski 			return start;
1045b26b2d49SDominik Brodowski 		if (start & 0x300)
10463fd94c6bSBenjamin Herrenschmidt 			start = (start + 0x3ff) & ~0x3ff;
10473fd94c6bSBenjamin Herrenschmidt 	}
1048b26b2d49SDominik Brodowski 
1049b26b2d49SDominik Brodowski 	return start;
10503fd94c6bSBenjamin Herrenschmidt }
10513fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource);
10523fd94c6bSBenjamin Herrenschmidt 
10533fd94c6bSBenjamin Herrenschmidt /*
10543fd94c6bSBenjamin Herrenschmidt  * Reparent resource children of pr that conflict with res
10553fd94c6bSBenjamin Herrenschmidt  * under res, and make res replace those children.
10563fd94c6bSBenjamin Herrenschmidt  */
10570f6023d5SHeiko Schocher static int reparent_resources(struct resource *parent,
10583fd94c6bSBenjamin Herrenschmidt 				     struct resource *res)
10593fd94c6bSBenjamin Herrenschmidt {
10603fd94c6bSBenjamin Herrenschmidt 	struct resource *p, **pp;
10613fd94c6bSBenjamin Herrenschmidt 	struct resource **firstpp = NULL;
10623fd94c6bSBenjamin Herrenschmidt 
10633fd94c6bSBenjamin Herrenschmidt 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
10643fd94c6bSBenjamin Herrenschmidt 		if (p->end < res->start)
10653fd94c6bSBenjamin Herrenschmidt 			continue;
10663fd94c6bSBenjamin Herrenschmidt 		if (res->end < p->start)
10673fd94c6bSBenjamin Herrenschmidt 			break;
10683fd94c6bSBenjamin Herrenschmidt 		if (p->start < res->start || p->end > res->end)
10693fd94c6bSBenjamin Herrenschmidt 			return -1;	/* not completely contained */
10703fd94c6bSBenjamin Herrenschmidt 		if (firstpp == NULL)
10713fd94c6bSBenjamin Herrenschmidt 			firstpp = pp;
10723fd94c6bSBenjamin Herrenschmidt 	}
10733fd94c6bSBenjamin Herrenschmidt 	if (firstpp == NULL)
10743fd94c6bSBenjamin Herrenschmidt 		return -1;	/* didn't find any conflicting entries? */
10753fd94c6bSBenjamin Herrenschmidt 	res->parent = parent;
10763fd94c6bSBenjamin Herrenschmidt 	res->child = *firstpp;
10773fd94c6bSBenjamin Herrenschmidt 	res->sibling = *pp;
10783fd94c6bSBenjamin Herrenschmidt 	*firstpp = res;
10793fd94c6bSBenjamin Herrenschmidt 	*pp = NULL;
10803fd94c6bSBenjamin Herrenschmidt 	for (p = res->child; p != NULL; p = p->sibling) {
10813fd94c6bSBenjamin Herrenschmidt 		p->parent = res;
1082ae2a84b4SKevin Hao 		pr_debug("PCI: Reparented %s %pR under %s\n",
1083ae2a84b4SKevin Hao 			 p->name, p, res->name);
10843fd94c6bSBenjamin Herrenschmidt 	}
10853fd94c6bSBenjamin Herrenschmidt 	return 0;
10863fd94c6bSBenjamin Herrenschmidt }
10873fd94c6bSBenjamin Herrenschmidt 
10883fd94c6bSBenjamin Herrenschmidt /*
10893fd94c6bSBenjamin Herrenschmidt  *  Handle resources of PCI devices.  If the world were perfect, we could
10903fd94c6bSBenjamin Herrenschmidt  *  just allocate all the resource regions and do nothing more.  It isn't.
10913fd94c6bSBenjamin Herrenschmidt  *  On the other hand, we cannot just re-allocate all devices, as it would
10923fd94c6bSBenjamin Herrenschmidt  *  require us to know lots of host bridge internals.  So we attempt to
10933fd94c6bSBenjamin Herrenschmidt  *  keep as much of the original configuration as possible, but tweak it
10943fd94c6bSBenjamin Herrenschmidt  *  when it's found to be wrong.
10953fd94c6bSBenjamin Herrenschmidt  *
10963fd94c6bSBenjamin Herrenschmidt  *  Known BIOS problems we have to work around:
10973fd94c6bSBenjamin Herrenschmidt  *	- I/O or memory regions not configured
10983fd94c6bSBenjamin Herrenschmidt  *	- regions configured, but not enabled in the command register
10993fd94c6bSBenjamin Herrenschmidt  *	- bogus I/O addresses above 64K used
11003fd94c6bSBenjamin Herrenschmidt  *	- expansion ROMs left enabled (this may sound harmless, but given
11013fd94c6bSBenjamin Herrenschmidt  *	  the fact the PCI specs explicitly allow address decoders to be
11023fd94c6bSBenjamin Herrenschmidt  *	  shared between expansion ROMs and other resource regions, it's
11033fd94c6bSBenjamin Herrenschmidt  *	  at least dangerous)
11043fd94c6bSBenjamin Herrenschmidt  *
11053fd94c6bSBenjamin Herrenschmidt  *  Our solution:
11063fd94c6bSBenjamin Herrenschmidt  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
11073fd94c6bSBenjamin Herrenschmidt  *	    This gives us fixed barriers on where we can allocate.
11083fd94c6bSBenjamin Herrenschmidt  *	(2) Allocate resources for all enabled devices.  If there is
11093fd94c6bSBenjamin Herrenschmidt  *	    a collision, just mark the resource as unallocated. Also
11103fd94c6bSBenjamin Herrenschmidt  *	    disable expansion ROMs during this step.
11113fd94c6bSBenjamin Herrenschmidt  *	(3) Try to allocate resources for disabled devices.  If the
11123fd94c6bSBenjamin Herrenschmidt  *	    resources were assigned correctly, everything goes well,
11133fd94c6bSBenjamin Herrenschmidt  *	    if they weren't, they won't disturb allocation of other
11143fd94c6bSBenjamin Herrenschmidt  *	    resources.
11153fd94c6bSBenjamin Herrenschmidt  *	(4) Assign new addresses to resources which were either
11163fd94c6bSBenjamin Herrenschmidt  *	    not configured at all or misconfigured.  If explicitly
11173fd94c6bSBenjamin Herrenschmidt  *	    requested by the user, configure expansion ROM address
11183fd94c6bSBenjamin Herrenschmidt  *	    as well.
11193fd94c6bSBenjamin Herrenschmidt  */
11203fd94c6bSBenjamin Herrenschmidt 
1121e51df2c1SAnton Blanchard static void pcibios_allocate_bus_resources(struct pci_bus *bus)
11223fd94c6bSBenjamin Herrenschmidt {
1123e90a1318SNathan Fontenot 	struct pci_bus *b;
11243fd94c6bSBenjamin Herrenschmidt 	int i;
11253fd94c6bSBenjamin Herrenschmidt 	struct resource *res, *pr;
11263fd94c6bSBenjamin Herrenschmidt 
1127b5ae5f91SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1128b5ae5f91SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1129b5ae5f91SBenjamin Herrenschmidt 
113089a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
113189a74eccSBjorn Helgaas 		if (!res || !res->flags || res->start > res->end || res->parent)
11323fd94c6bSBenjamin Herrenschmidt 			continue;
113348c2ce97SBenjamin Herrenschmidt 
113448c2ce97SBenjamin Herrenschmidt 		/* If the resource was left unset at this point, we clear it */
113548c2ce97SBenjamin Herrenschmidt 		if (res->flags & IORESOURCE_UNSET)
113648c2ce97SBenjamin Herrenschmidt 			goto clear_resource;
113748c2ce97SBenjamin Herrenschmidt 
11383fd94c6bSBenjamin Herrenschmidt 		if (bus->parent == NULL)
11393fd94c6bSBenjamin Herrenschmidt 			pr = (res->flags & IORESOURCE_IO) ?
11403fd94c6bSBenjamin Herrenschmidt 				&ioport_resource : &iomem_resource;
11413fd94c6bSBenjamin Herrenschmidt 		else {
11423fd94c6bSBenjamin Herrenschmidt 			pr = pci_find_parent_resource(bus->self, res);
11433fd94c6bSBenjamin Herrenschmidt 			if (pr == res) {
11443fd94c6bSBenjamin Herrenschmidt 				/* this happens when the generic PCI
11453fd94c6bSBenjamin Herrenschmidt 				 * code (wrongly) decides that this
11463fd94c6bSBenjamin Herrenschmidt 				 * bridge is transparent  -- paulus
11473fd94c6bSBenjamin Herrenschmidt 				 */
11483fd94c6bSBenjamin Herrenschmidt 				continue;
11493fd94c6bSBenjamin Herrenschmidt 			}
11503fd94c6bSBenjamin Herrenschmidt 		}
11513fd94c6bSBenjamin Herrenschmidt 
1152ae2a84b4SKevin Hao 		pr_debug("PCI: %s (bus %d) bridge rsrc %d: %pR, parent %p (%s)\n",
1153ae2a84b4SKevin Hao 			 bus->self ? pci_name(bus->self) : "PHB", bus->number,
1154ae2a84b4SKevin Hao 			 i, res, pr, (pr && pr->name) ? pr->name : "nil");
11553fd94c6bSBenjamin Herrenschmidt 
11563fd94c6bSBenjamin Herrenschmidt 		if (pr && !(pr->flags & IORESOURCE_UNSET)) {
11573ebfe46aSYinghai Lu 			struct pci_dev *dev = bus->self;
11583ebfe46aSYinghai Lu 
11593fd94c6bSBenjamin Herrenschmidt 			if (request_resource(pr, res) == 0)
11603fd94c6bSBenjamin Herrenschmidt 				continue;
11613fd94c6bSBenjamin Herrenschmidt 			/*
11623fd94c6bSBenjamin Herrenschmidt 			 * Must be a conflict with an existing entry.
11633fd94c6bSBenjamin Herrenschmidt 			 * Move that entry (or entries) under the
11643fd94c6bSBenjamin Herrenschmidt 			 * bridge resource and try again.
11653fd94c6bSBenjamin Herrenschmidt 			 */
11663fd94c6bSBenjamin Herrenschmidt 			if (reparent_resources(pr, res) == 0)
11673fd94c6bSBenjamin Herrenschmidt 				continue;
11683ebfe46aSYinghai Lu 
11693ebfe46aSYinghai Lu 			if (dev && i < PCI_BRIDGE_RESOURCE_NUM &&
11703ebfe46aSYinghai Lu 			    pci_claim_bridge_resource(dev,
11713ebfe46aSYinghai Lu 						i + PCI_BRIDGE_RESOURCES) == 0)
11723ebfe46aSYinghai Lu 				continue;
11733fd94c6bSBenjamin Herrenschmidt 		}
1174f2c2cbccSJoe Perches 		pr_warn("PCI: Cannot allocate resource region %d of PCI bridge %d, will remap\n",
1175f2c2cbccSJoe Perches 			i, bus->number);
11763fd94c6bSBenjamin Herrenschmidt 	clear_resource:
1177cf1a4cf8SGavin Shan 		/* The resource might be figured out when doing
1178cf1a4cf8SGavin Shan 		 * reassignment based on the resources required
1179cf1a4cf8SGavin Shan 		 * by the downstream PCI devices. Here we set
1180cf1a4cf8SGavin Shan 		 * the size of the resource to be 0 in order to
1181cf1a4cf8SGavin Shan 		 * save more space.
1182cf1a4cf8SGavin Shan 		 */
1183cf1a4cf8SGavin Shan 		res->start = 0;
1184cf1a4cf8SGavin Shan 		res->end = -1;
11853fd94c6bSBenjamin Herrenschmidt 		res->flags = 0;
11863fd94c6bSBenjamin Herrenschmidt 	}
1187e90a1318SNathan Fontenot 
1188e90a1318SNathan Fontenot 	list_for_each_entry(b, &bus->children, node)
1189e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
11903fd94c6bSBenjamin Herrenschmidt }
11913fd94c6bSBenjamin Herrenschmidt 
1192cad5cef6SGreg Kroah-Hartman static inline void alloc_resource(struct pci_dev *dev, int idx)
11933fd94c6bSBenjamin Herrenschmidt {
11943fd94c6bSBenjamin Herrenschmidt 	struct resource *pr, *r = &dev->resource[idx];
11953fd94c6bSBenjamin Herrenschmidt 
1196ae2a84b4SKevin Hao 	pr_debug("PCI: Allocating %s: Resource %d: %pR\n",
1197ae2a84b4SKevin Hao 		 pci_name(dev), idx, r);
11983fd94c6bSBenjamin Herrenschmidt 
11993fd94c6bSBenjamin Herrenschmidt 	pr = pci_find_parent_resource(dev, r);
12003fd94c6bSBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||
12013fd94c6bSBenjamin Herrenschmidt 	    request_resource(pr, r) < 0) {
12023fd94c6bSBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
12033fd94c6bSBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
12043fd94c6bSBenjamin Herrenschmidt 		if (pr)
1205ae2a84b4SKevin Hao 			pr_debug("PCI:  parent is %p: %pR\n", pr, pr);
12063fd94c6bSBenjamin Herrenschmidt 		/* We'll assign a new address later */
12073fd94c6bSBenjamin Herrenschmidt 		r->flags |= IORESOURCE_UNSET;
12083fd94c6bSBenjamin Herrenschmidt 		r->end -= r->start;
12093fd94c6bSBenjamin Herrenschmidt 		r->start = 0;
12103fd94c6bSBenjamin Herrenschmidt 	}
12113fd94c6bSBenjamin Herrenschmidt }
12123fd94c6bSBenjamin Herrenschmidt 
12133fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass)
12143fd94c6bSBenjamin Herrenschmidt {
12153fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = NULL;
12163fd94c6bSBenjamin Herrenschmidt 	int idx, disabled;
12173fd94c6bSBenjamin Herrenschmidt 	u16 command;
12183fd94c6bSBenjamin Herrenschmidt 	struct resource *r;
12193fd94c6bSBenjamin Herrenschmidt 
12203fd94c6bSBenjamin Herrenschmidt 	for_each_pci_dev(dev) {
12213fd94c6bSBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1222ad892a63SBenjamin Herrenschmidt 		for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
12233fd94c6bSBenjamin Herrenschmidt 			r = &dev->resource[idx];
12243fd94c6bSBenjamin Herrenschmidt 			if (r->parent)		/* Already allocated */
12253fd94c6bSBenjamin Herrenschmidt 				continue;
12263fd94c6bSBenjamin Herrenschmidt 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
12273fd94c6bSBenjamin Herrenschmidt 				continue;	/* Not assigned at all */
1228ad892a63SBenjamin Herrenschmidt 			/* We only allocate ROMs on pass 1 just in case they
1229ad892a63SBenjamin Herrenschmidt 			 * have been screwed up by firmware
1230ad892a63SBenjamin Herrenschmidt 			 */
1231ad892a63SBenjamin Herrenschmidt 			if (idx == PCI_ROM_RESOURCE )
1232ad892a63SBenjamin Herrenschmidt 				disabled = 1;
12333fd94c6bSBenjamin Herrenschmidt 			if (r->flags & IORESOURCE_IO)
12343fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_IO);
12353fd94c6bSBenjamin Herrenschmidt 			else
12363fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_MEMORY);
1237533b1928SPaul Mackerras 			if (pass == disabled)
1238533b1928SPaul Mackerras 				alloc_resource(dev, idx);
12393fd94c6bSBenjamin Herrenschmidt 		}
12403fd94c6bSBenjamin Herrenschmidt 		if (pass)
12413fd94c6bSBenjamin Herrenschmidt 			continue;
12423fd94c6bSBenjamin Herrenschmidt 		r = &dev->resource[PCI_ROM_RESOURCE];
1243ad892a63SBenjamin Herrenschmidt 		if (r->flags) {
12443fd94c6bSBenjamin Herrenschmidt 			/* Turn the ROM off, leave the resource region,
12453fd94c6bSBenjamin Herrenschmidt 			 * but keep it unregistered.
12463fd94c6bSBenjamin Herrenschmidt 			 */
12473fd94c6bSBenjamin Herrenschmidt 			u32 reg;
1248ad892a63SBenjamin Herrenschmidt 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1249ad892a63SBenjamin Herrenschmidt 			if (reg & PCI_ROM_ADDRESS_ENABLE) {
1250b0494bc8SBenjamin Herrenschmidt 				pr_debug("PCI: Switching off ROM of %s\n",
1251b0494bc8SBenjamin Herrenschmidt 					 pci_name(dev));
12523fd94c6bSBenjamin Herrenschmidt 				r->flags &= ~IORESOURCE_ROM_ENABLE;
12533fd94c6bSBenjamin Herrenschmidt 				pci_write_config_dword(dev, dev->rom_base_reg,
12543fd94c6bSBenjamin Herrenschmidt 						       reg & ~PCI_ROM_ADDRESS_ENABLE);
12553fd94c6bSBenjamin Herrenschmidt 			}
12563fd94c6bSBenjamin Herrenschmidt 		}
12573fd94c6bSBenjamin Herrenschmidt 	}
1258ad892a63SBenjamin Herrenschmidt }
12593fd94c6bSBenjamin Herrenschmidt 
1260c1f34302SBenjamin Herrenschmidt static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
1261c1f34302SBenjamin Herrenschmidt {
1262c1f34302SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
1263c1f34302SBenjamin Herrenschmidt 	resource_size_t	offset;
1264c1f34302SBenjamin Herrenschmidt 	struct resource *res, *pres;
1265c1f34302SBenjamin Herrenschmidt 	int i;
1266c1f34302SBenjamin Herrenschmidt 
1267c1f34302SBenjamin Herrenschmidt 	pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
1268c1f34302SBenjamin Herrenschmidt 
1269c1f34302SBenjamin Herrenschmidt 	/* Check for IO */
1270c1f34302SBenjamin Herrenschmidt 	if (!(hose->io_resource.flags & IORESOURCE_IO))
1271c1f34302SBenjamin Herrenschmidt 		goto no_io;
1272c1f34302SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1273c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1274c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1275c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy IO";
1276c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_IO;
1277c1f34302SBenjamin Herrenschmidt 	res->start = offset;
1278c1f34302SBenjamin Herrenschmidt 	res->end = (offset + 0xfff) & 0xfffffffful;
1279c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate legacy IO: %pR\n", res);
1280c1f34302SBenjamin Herrenschmidt 	if (request_resource(&hose->io_resource, res)) {
1281c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1282c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1283c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1284c1f34302SBenjamin Herrenschmidt 		kfree(res);
1285c1f34302SBenjamin Herrenschmidt 	}
1286c1f34302SBenjamin Herrenschmidt 
1287c1f34302SBenjamin Herrenschmidt  no_io:
1288c1f34302SBenjamin Herrenschmidt 	/* Check for memory */
1289c1f34302SBenjamin Herrenschmidt 	for (i = 0; i < 3; i++) {
1290c1f34302SBenjamin Herrenschmidt 		pres = &hose->mem_resources[i];
12913fd47f06SBenjamin Herrenschmidt 		offset = hose->mem_offset[i];
1292c1f34302SBenjamin Herrenschmidt 		if (!(pres->flags & IORESOURCE_MEM))
1293c1f34302SBenjamin Herrenschmidt 			continue;
1294c1f34302SBenjamin Herrenschmidt 		pr_debug("hose mem res: %pR\n", pres);
1295c1f34302SBenjamin Herrenschmidt 		if ((pres->start - offset) <= 0xa0000 &&
1296c1f34302SBenjamin Herrenschmidt 		    (pres->end - offset) >= 0xbffff)
1297c1f34302SBenjamin Herrenschmidt 			break;
1298c1f34302SBenjamin Herrenschmidt 	}
1299c1f34302SBenjamin Herrenschmidt 	if (i >= 3)
1300c1f34302SBenjamin Herrenschmidt 		return;
1301c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1302c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1303c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy VGA memory";
1304c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_MEM;
1305c1f34302SBenjamin Herrenschmidt 	res->start = 0xa0000 + offset;
1306c1f34302SBenjamin Herrenschmidt 	res->end = 0xbffff + offset;
1307c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate VGA memory: %pR\n", res);
1308c1f34302SBenjamin Herrenschmidt 	if (request_resource(pres, res)) {
1309c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1310c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1311c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1312c1f34302SBenjamin Herrenschmidt 		kfree(res);
1313c1f34302SBenjamin Herrenschmidt 	}
1314c1f34302SBenjamin Herrenschmidt }
1315c1f34302SBenjamin Herrenschmidt 
13163fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void)
13173fd94c6bSBenjamin Herrenschmidt {
1318e90a1318SNathan Fontenot 	struct pci_bus *b;
1319e90a1318SNathan Fontenot 
132048c2ce97SBenjamin Herrenschmidt 	/* Allocate and assign resources */
1321e90a1318SNathan Fontenot 	list_for_each_entry(b, &pci_root_buses, node)
1322e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
13239a1a70aeSBenjamin Herrenschmidt 	if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
13243fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(0);
13253fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(1);
13269a1a70aeSBenjamin Herrenschmidt 	}
13273fd94c6bSBenjamin Herrenschmidt 
1328c1f34302SBenjamin Herrenschmidt 	/* Before we start assigning unassigned resource, we try to reserve
1329c1f34302SBenjamin Herrenschmidt 	 * the low IO area and the VGA memory area if they intersect the
1330c1f34302SBenjamin Herrenschmidt 	 * bus available resources to avoid allocating things on top of them
1331c1f34302SBenjamin Herrenschmidt 	 */
13320e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1333c1f34302SBenjamin Herrenschmidt 		list_for_each_entry(b, &pci_root_buses, node)
1334c1f34302SBenjamin Herrenschmidt 			pcibios_reserve_legacy_regions(b);
1335c1f34302SBenjamin Herrenschmidt 	}
1336c1f34302SBenjamin Herrenschmidt 
1337c1f34302SBenjamin Herrenschmidt 	/* Now, if the platform didn't decide to blindly trust the firmware,
1338c1f34302SBenjamin Herrenschmidt 	 * we proceed to assigning things that were left unassigned
1339c1f34302SBenjamin Herrenschmidt 	 */
13400e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1341a77acda0SWolfram Sang 		pr_debug("PCI: Assigning unassigned resources...\n");
13423fd94c6bSBenjamin Herrenschmidt 		pci_assign_unassigned_resources();
13433fd94c6bSBenjamin Herrenschmidt 	}
13443fd94c6bSBenjamin Herrenschmidt }
13453fd94c6bSBenjamin Herrenschmidt 
1346fd6852c8SBenjamin Herrenschmidt /* This is used by the PCI hotplug driver to allocate resource
13473fd94c6bSBenjamin Herrenschmidt  * of newly plugged busses. We can try to consolidate with the
1348fd6852c8SBenjamin Herrenschmidt  * rest of the code later, for now, keep it as-is as our main
1349fd6852c8SBenjamin Herrenschmidt  * resource allocation function doesn't deal with sub-trees yet.
13503fd94c6bSBenjamin Herrenschmidt  */
1351baf75b0aSStephen Rothwell void pcibios_claim_one_bus(struct pci_bus *bus)
13523fd94c6bSBenjamin Herrenschmidt {
13533fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev;
13543fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *child_bus;
13553fd94c6bSBenjamin Herrenschmidt 
13563fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
13573fd94c6bSBenjamin Herrenschmidt 		int i;
13583fd94c6bSBenjamin Herrenschmidt 
13593fd94c6bSBenjamin Herrenschmidt 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
13603fd94c6bSBenjamin Herrenschmidt 			struct resource *r = &dev->resource[i];
13613fd94c6bSBenjamin Herrenschmidt 
13623fd94c6bSBenjamin Herrenschmidt 			if (r->parent || !r->start || !r->flags)
13633fd94c6bSBenjamin Herrenschmidt 				continue;
1364fd6852c8SBenjamin Herrenschmidt 
1365ae2a84b4SKevin Hao 			pr_debug("PCI: Claiming %s: Resource %d: %pR\n",
1366ae2a84b4SKevin Hao 				 pci_name(dev), i, r);
1367fd6852c8SBenjamin Herrenschmidt 
13683ebfe46aSYinghai Lu 			if (pci_claim_resource(dev, i) == 0)
13693ebfe46aSYinghai Lu 				continue;
13703ebfe46aSYinghai Lu 
13713ebfe46aSYinghai Lu 			pci_claim_bridge_resource(dev, i);
13723fd94c6bSBenjamin Herrenschmidt 		}
13733fd94c6bSBenjamin Herrenschmidt 	}
13743fd94c6bSBenjamin Herrenschmidt 
13753fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(child_bus, &bus->children, node)
13763fd94c6bSBenjamin Herrenschmidt 		pcibios_claim_one_bus(child_bus);
13773fd94c6bSBenjamin Herrenschmidt }
13785b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
1379fd6852c8SBenjamin Herrenschmidt 
1380fd6852c8SBenjamin Herrenschmidt 
1381fd6852c8SBenjamin Herrenschmidt /* pcibios_finish_adding_to_bus
1382fd6852c8SBenjamin Herrenschmidt  *
1383fd6852c8SBenjamin Herrenschmidt  * This is to be called by the hotplug code after devices have been
1384fd6852c8SBenjamin Herrenschmidt  * added to a bus, this include calling it for a PHB that is just
1385fd6852c8SBenjamin Herrenschmidt  * being added
1386fd6852c8SBenjamin Herrenschmidt  */
1387fd6852c8SBenjamin Herrenschmidt void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1388fd6852c8SBenjamin Herrenschmidt {
1389fd6852c8SBenjamin Herrenschmidt 	pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1390fd6852c8SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1391fd6852c8SBenjamin Herrenschmidt 
1392fd6852c8SBenjamin Herrenschmidt 	/* Allocate bus and devices resources */
1393fd6852c8SBenjamin Herrenschmidt 	pcibios_allocate_bus_resources(bus);
1394fd6852c8SBenjamin Herrenschmidt 	pcibios_claim_one_bus(bus);
13957415c14cSGavin Shan 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
13967415c14cSGavin Shan 		if (bus->self)
13977415c14cSGavin Shan 			pci_assign_unassigned_bridge_resources(bus->self);
13987415c14cSGavin Shan 		else
1399ab444ec9SGavin Shan 			pci_assign_unassigned_bus_resources(bus);
14007415c14cSGavin Shan 	}
1401fd6852c8SBenjamin Herrenschmidt 
14026a040ce7SThadeu Lima de Souza Cascardo 	/* Fixup EEH */
14036a040ce7SThadeu Lima de Souza Cascardo 	eeh_add_device_tree_late(bus);
14046a040ce7SThadeu Lima de Souza Cascardo 
1405fd6852c8SBenjamin Herrenschmidt 	/* Add new devices to global lists.  Register in proc, sysfs. */
1406fd6852c8SBenjamin Herrenschmidt 	pci_bus_add_devices(bus);
1407fd6852c8SBenjamin Herrenschmidt 
14086a040ce7SThadeu Lima de Souza Cascardo 	/* sysfs files should only be added after devices are added */
14096a040ce7SThadeu Lima de Souza Cascardo 	eeh_add_sysfs_files(bus);
1410fd6852c8SBenjamin Herrenschmidt }
1411fd6852c8SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1412fd6852c8SBenjamin Herrenschmidt 
1413549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask)
1414549beb9bSBenjamin Herrenschmidt {
1415467efc2eSDaniel Axtens 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
1416467efc2eSDaniel Axtens 
1417467efc2eSDaniel Axtens 	if (phb->controller_ops.enable_device_hook)
1418467efc2eSDaniel Axtens 		if (!phb->controller_ops.enable_device_hook(dev))
1419549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1420549beb9bSBenjamin Herrenschmidt 
14217cfb5f9aSBjorn Helgaas 	return pci_enable_resources(dev, mask);
1422549beb9bSBenjamin Herrenschmidt }
142353280323SBenjamin Herrenschmidt 
1424abeeed6dSMichael Neuling void pcibios_disable_device(struct pci_dev *dev)
1425abeeed6dSMichael Neuling {
1426abeeed6dSMichael Neuling 	struct pci_controller *phb = pci_bus_to_host(dev->bus);
1427abeeed6dSMichael Neuling 
1428abeeed6dSMichael Neuling 	if (phb->controller_ops.disable_device)
1429abeeed6dSMichael Neuling 		phb->controller_ops.disable_device(dev);
1430abeeed6dSMichael Neuling }
1431abeeed6dSMichael Neuling 
143238973ba7SBjorn Helgaas resource_size_t pcibios_io_space_offset(struct pci_controller *hose)
143338973ba7SBjorn Helgaas {
143438973ba7SBjorn Helgaas 	return (unsigned long) hose->io_base_virt - _IO_BASE;
143538973ba7SBjorn Helgaas }
143638973ba7SBjorn Helgaas 
1437cad5cef6SGreg Kroah-Hartman static void pcibios_setup_phb_resources(struct pci_controller *hose,
1438cad5cef6SGreg Kroah-Hartman 					struct list_head *resources)
143953280323SBenjamin Herrenschmidt {
144053280323SBenjamin Herrenschmidt 	struct resource *res;
14413fd47f06SBenjamin Herrenschmidt 	resource_size_t offset;
144253280323SBenjamin Herrenschmidt 	int i;
144353280323SBenjamin Herrenschmidt 
144453280323SBenjamin Herrenschmidt 	/* Hookup PHB IO resource */
144545a709f8SBjorn Helgaas 	res = &hose->io_resource;
144653280323SBenjamin Herrenschmidt 
144753280323SBenjamin Herrenschmidt 	if (!res->flags) {
1448cdb1b342SBenjamin Herrenschmidt 		pr_debug("PCI: I/O resource not set for host"
1449b7c670d6SRob Herring 			 " bridge %pOF (domain %d)\n",
1450b7c670d6SRob Herring 			 hose->dn, hose->global_number);
14513fd47f06SBenjamin Herrenschmidt 	} else {
14523fd47f06SBenjamin Herrenschmidt 		offset = pcibios_io_space_offset(hose);
14533fd47f06SBenjamin Herrenschmidt 
1454ae2a84b4SKevin Hao 		pr_debug("PCI: PHB IO resource    = %pR off 0x%08llx\n",
1455ae2a84b4SKevin Hao 			 res, (unsigned long long)offset);
14563fd47f06SBenjamin Herrenschmidt 		pci_add_resource_offset(resources, res, offset);
1457a0b8e76fSBenjamin Herrenschmidt 	}
1458a0b8e76fSBenjamin Herrenschmidt 
145953280323SBenjamin Herrenschmidt 	/* Hookup PHB Memory resources */
146053280323SBenjamin Herrenschmidt 	for (i = 0; i < 3; ++i) {
146153280323SBenjamin Herrenschmidt 		res = &hose->mem_resources[i];
1462727597d1SGavin Shan 		if (!res->flags)
14633fd47f06SBenjamin Herrenschmidt 			continue;
1464727597d1SGavin Shan 
14653fd47f06SBenjamin Herrenschmidt 		offset = hose->mem_offset[i];
1466ae2a84b4SKevin Hao 		pr_debug("PCI: PHB MEM resource %d = %pR off 0x%08llx\n", i,
1467ae2a84b4SKevin Hao 			 res, (unsigned long long)offset);
146853280323SBenjamin Herrenschmidt 
14693fd47f06SBenjamin Herrenschmidt 		pci_add_resource_offset(resources, res, offset);
14703fd47f06SBenjamin Herrenschmidt 	}
147153280323SBenjamin Herrenschmidt }
147289c2dd62SKumar Gala 
147389c2dd62SKumar Gala /*
147489c2dd62SKumar Gala  * Null PCI config access functions, for the case when we can't
147589c2dd62SKumar Gala  * find a hose.
147689c2dd62SKumar Gala  */
147789c2dd62SKumar Gala #define NULL_PCI_OP(rw, size, type)					\
147889c2dd62SKumar Gala static int								\
147989c2dd62SKumar Gala null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
148089c2dd62SKumar Gala {									\
148189c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
148289c2dd62SKumar Gala }
148389c2dd62SKumar Gala 
148489c2dd62SKumar Gala static int
148589c2dd62SKumar Gala null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
148689c2dd62SKumar Gala 		 int len, u32 *val)
148789c2dd62SKumar Gala {
148889c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
148989c2dd62SKumar Gala }
149089c2dd62SKumar Gala 
149189c2dd62SKumar Gala static int
149289c2dd62SKumar Gala null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
149389c2dd62SKumar Gala 		  int len, u32 val)
149489c2dd62SKumar Gala {
149589c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
149689c2dd62SKumar Gala }
149789c2dd62SKumar Gala 
149889c2dd62SKumar Gala static struct pci_ops null_pci_ops =
149989c2dd62SKumar Gala {
150089c2dd62SKumar Gala 	.read = null_read_config,
150189c2dd62SKumar Gala 	.write = null_write_config,
150289c2dd62SKumar Gala };
150389c2dd62SKumar Gala 
150489c2dd62SKumar Gala /*
150589c2dd62SKumar Gala  * These functions are used early on before PCI scanning is done
150689c2dd62SKumar Gala  * and all of the pci_dev and pci_bus structures have been created.
150789c2dd62SKumar Gala  */
150889c2dd62SKumar Gala static struct pci_bus *
150989c2dd62SKumar Gala fake_pci_bus(struct pci_controller *hose, int busnr)
151089c2dd62SKumar Gala {
151189c2dd62SKumar Gala 	static struct pci_bus bus;
151289c2dd62SKumar Gala 
1513b0d436c7SAnton Blanchard 	if (hose == NULL) {
151489c2dd62SKumar Gala 		printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
151589c2dd62SKumar Gala 	}
151689c2dd62SKumar Gala 	bus.number = busnr;
151789c2dd62SKumar Gala 	bus.sysdata = hose;
151889c2dd62SKumar Gala 	bus.ops = hose? hose->ops: &null_pci_ops;
151989c2dd62SKumar Gala 	return &bus;
152089c2dd62SKumar Gala }
152189c2dd62SKumar Gala 
152289c2dd62SKumar Gala #define EARLY_PCI_OP(rw, size, type)					\
152389c2dd62SKumar Gala int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
152489c2dd62SKumar Gala 			       int devfn, int offset, type value)	\
152589c2dd62SKumar Gala {									\
152689c2dd62SKumar Gala 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
152789c2dd62SKumar Gala 					    devfn, offset, value);	\
152889c2dd62SKumar Gala }
152989c2dd62SKumar Gala 
153089c2dd62SKumar Gala EARLY_PCI_OP(read, byte, u8 *)
153189c2dd62SKumar Gala EARLY_PCI_OP(read, word, u16 *)
153289c2dd62SKumar Gala EARLY_PCI_OP(read, dword, u32 *)
153389c2dd62SKumar Gala EARLY_PCI_OP(write, byte, u8)
153489c2dd62SKumar Gala EARLY_PCI_OP(write, word, u16)
153589c2dd62SKumar Gala EARLY_PCI_OP(write, dword, u32)
153689c2dd62SKumar Gala 
153789c2dd62SKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
153889c2dd62SKumar Gala 			  int cap)
153989c2dd62SKumar Gala {
154089c2dd62SKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
154189c2dd62SKumar Gala }
15420ed2c722SGrant Likely 
154398d9f30cSBenjamin Herrenschmidt struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
154498d9f30cSBenjamin Herrenschmidt {
154598d9f30cSBenjamin Herrenschmidt 	struct pci_controller *hose = bus->sysdata;
154698d9f30cSBenjamin Herrenschmidt 
154798d9f30cSBenjamin Herrenschmidt 	return of_node_get(hose->dn);
154898d9f30cSBenjamin Herrenschmidt }
154998d9f30cSBenjamin Herrenschmidt 
15500ed2c722SGrant Likely /**
15510ed2c722SGrant Likely  * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
15520ed2c722SGrant Likely  * @hose: Pointer to the PCI host controller instance structure
15530ed2c722SGrant Likely  */
1554cad5cef6SGreg Kroah-Hartman void pcibios_scan_phb(struct pci_controller *hose)
15550ed2c722SGrant Likely {
155645a709f8SBjorn Helgaas 	LIST_HEAD(resources);
15570ed2c722SGrant Likely 	struct pci_bus *bus;
15580ed2c722SGrant Likely 	struct device_node *node = hose->dn;
15590ed2c722SGrant Likely 	int mode;
15600ed2c722SGrant Likely 
1561b7c670d6SRob Herring 	pr_debug("PCI: Scanning PHB %pOF\n", node);
15620ed2c722SGrant Likely 
15630ed2c722SGrant Likely 	/* Get some IO space for the new PHB */
15640ed2c722SGrant Likely 	pcibios_setup_phb_io_space(hose);
15650ed2c722SGrant Likely 
15660ed2c722SGrant Likely 	/* Wire up PHB bus resources */
156745a709f8SBjorn Helgaas 	pcibios_setup_phb_resources(hose, &resources);
156845a709f8SBjorn Helgaas 
1569be8e60d8SYinghai Lu 	hose->busn.start = hose->first_busno;
1570be8e60d8SYinghai Lu 	hose->busn.end	 = hose->last_busno;
1571be8e60d8SYinghai Lu 	hose->busn.flags = IORESOURCE_BUS;
1572be8e60d8SYinghai Lu 	pci_add_resource(&resources, &hose->busn);
1573be8e60d8SYinghai Lu 
157445a709f8SBjorn Helgaas 	/* Create an empty bus for the toplevel */
157545a709f8SBjorn Helgaas 	bus = pci_create_root_bus(hose->parent, hose->first_busno,
157645a709f8SBjorn Helgaas 				  hose->ops, hose, &resources);
157745a709f8SBjorn Helgaas 	if (bus == NULL) {
157845a709f8SBjorn Helgaas 		pr_err("Failed to create bus for PCI domain %04x\n",
157945a709f8SBjorn Helgaas 			hose->global_number);
158045a709f8SBjorn Helgaas 		pci_free_resource_list(&resources);
158145a709f8SBjorn Helgaas 		return;
158245a709f8SBjorn Helgaas 	}
158345a709f8SBjorn Helgaas 	hose->bus = bus;
15840ed2c722SGrant Likely 
15850ed2c722SGrant Likely 	/* Get probe mode and perform scan */
15860ed2c722SGrant Likely 	mode = PCI_PROBE_NORMAL;
1587467efc2eSDaniel Axtens 	if (node && hose->controller_ops.probe_mode)
1588467efc2eSDaniel Axtens 		mode = hose->controller_ops.probe_mode(bus);
15890ed2c722SGrant Likely 	pr_debug("    probe mode: %d\n", mode);
1590be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_DEVTREE)
15910ed2c722SGrant Likely 		of_scan_bus(node, bus);
15920ed2c722SGrant Likely 
1593be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_NORMAL) {
1594be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, 255);
1595be8e60d8SYinghai Lu 		hose->last_busno = pci_scan_child_bus(bus);
1596be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, hose->last_busno);
1597be8e60d8SYinghai Lu 	}
1598781fb7a3SBenjamin Herrenschmidt 
1599491b98c3SBenjamin Herrenschmidt 	/* Platform gets a chance to do some global fixups before
1600491b98c3SBenjamin Herrenschmidt 	 * we proceed to resource allocation
1601491b98c3SBenjamin Herrenschmidt 	 */
1602491b98c3SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_phb)
1603491b98c3SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_phb(hose);
1604491b98c3SBenjamin Herrenschmidt 
1605781fb7a3SBenjamin Herrenschmidt 	/* Configure PCI Express settings */
1606bb36c445SBenjamin Herrenschmidt 	if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
1607781fb7a3SBenjamin Herrenschmidt 		struct pci_bus *child;
1608a58674ffSBjorn Helgaas 		list_for_each_entry(child, &bus->children, node)
1609a58674ffSBjorn Helgaas 			pcie_bus_configure_settings(child);
1610781fb7a3SBenjamin Herrenschmidt 	}
16110ed2c722SGrant Likely }
16125b64d2ccSDaniel Axtens EXPORT_SYMBOL_GPL(pcibios_scan_phb);
1613c065488fSKumar Gala 
1614c065488fSKumar Gala static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
1615c065488fSKumar Gala {
1616c065488fSKumar Gala 	int i, class = dev->class >> 8;
161705737c7cSJason Jin 	/* When configured as agent, programing interface = 1 */
161805737c7cSJason Jin 	int prog_if = dev->class & 0xf;
1619c065488fSKumar Gala 
1620c065488fSKumar Gala 	if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
1621c065488fSKumar Gala 	     class == PCI_CLASS_BRIDGE_OTHER) &&
1622c065488fSKumar Gala 		(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
162305737c7cSJason Jin 		(prog_if == 0) &&
1624c065488fSKumar Gala 		(dev->bus->parent == NULL)) {
1625c065488fSKumar Gala 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1626c065488fSKumar Gala 			dev->resource[i].start = 0;
1627c065488fSKumar Gala 			dev->resource[i].end = 0;
1628c065488fSKumar Gala 			dev->resource[i].flags = 0;
1629c065488fSKumar Gala 		}
1630c065488fSKumar Gala 	}
1631c065488fSKumar Gala }
1632c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1633c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1634