xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision 05737c7c5bca9a4f3e0f8bb9476445971b64fafd)
15516b540SKumar Gala /*
25516b540SKumar Gala  * Contains common pci routines for ALL ppc platform
3cf1d8a8aSKumar Gala  * (based on pci_32.c and pci_64.c)
4cf1d8a8aSKumar Gala  *
5cf1d8a8aSKumar Gala  * Port for PPC64 David Engebretsen, IBM Corp.
6cf1d8a8aSKumar Gala  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
7cf1d8a8aSKumar Gala  *
8cf1d8a8aSKumar Gala  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
9cf1d8a8aSKumar Gala  *   Rework, based on alpha PCI code.
10cf1d8a8aSKumar Gala  *
11cf1d8a8aSKumar Gala  * Common pmac/prep/chrp pci routines. -- Cort
125516b540SKumar Gala  *
135516b540SKumar Gala  * This program is free software; you can redistribute it and/or
145516b540SKumar Gala  * modify it under the terms of the GNU General Public License
155516b540SKumar Gala  * as published by the Free Software Foundation; either version
165516b540SKumar Gala  * 2 of the License, or (at your option) any later version.
175516b540SKumar Gala  */
185516b540SKumar Gala 
195516b540SKumar Gala #include <linux/kernel.h>
205516b540SKumar Gala #include <linux/pci.h>
215516b540SKumar Gala #include <linux/string.h>
225516b540SKumar Gala #include <linux/init.h>
235516b540SKumar Gala #include <linux/bootmem.h>
2466b15db6SPaul Gortmaker #include <linux/export.h>
2522ae782fSGrant Likely #include <linux/of_address.h>
2604bea68bSSebastian Andrzej Siewior #include <linux/of_pci.h>
275516b540SKumar Gala #include <linux/mm.h>
285516b540SKumar Gala #include <linux/list.h>
295516b540SKumar Gala #include <linux/syscalls.h>
305516b540SKumar Gala #include <linux/irq.h>
315516b540SKumar Gala #include <linux/vmalloc.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
335516b540SKumar Gala 
345516b540SKumar Gala #include <asm/processor.h>
355516b540SKumar Gala #include <asm/io.h>
365516b540SKumar Gala #include <asm/prom.h>
375516b540SKumar Gala #include <asm/pci-bridge.h>
385516b540SKumar Gala #include <asm/byteorder.h>
395516b540SKumar Gala #include <asm/machdep.h>
405516b540SKumar Gala #include <asm/ppc-pci.h>
415516b540SKumar Gala #include <asm/firmware.h>
428b8da358SBenjamin Herrenschmidt #include <asm/eeh.h>
435516b540SKumar Gala 
44a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
45c3bd517dSMilton Miller LIST_HEAD(hose_list);
46a4c9e328SKumar Gala 
47a4c9e328SKumar Gala /* XXX kill that some day ... */
48ebfc00f7SStephen Rothwell static int global_phb_number;		/* Global phb counter */
49a4c9e328SKumar Gala 
5025e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */
5125e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base;
5225e81f92SBenjamin Herrenschmidt 
531fd0f525SBenjamin Herrenschmidt /* Default PCI flags is 0 on ppc32, modified at boot on ppc64 */
540e47ff1cSRob Herring unsigned int pci_flags = 0;
551fd0f525SBenjamin Herrenschmidt 
56a4c9e328SKumar Gala 
5745223c54SFUJITA Tomonori static struct dma_map_ops *pci_dma_ops = &dma_direct_ops;
584fc665b8SBecky Bruce 
5945223c54SFUJITA Tomonori void set_pci_dma_ops(struct dma_map_ops *dma_ops)
604fc665b8SBecky Bruce {
614fc665b8SBecky Bruce 	pci_dma_ops = dma_ops;
624fc665b8SBecky Bruce }
634fc665b8SBecky Bruce 
6445223c54SFUJITA Tomonori struct dma_map_ops *get_pci_dma_ops(void)
654fc665b8SBecky Bruce {
664fc665b8SBecky Bruce 	return pci_dma_ops;
674fc665b8SBecky Bruce }
684fc665b8SBecky Bruce EXPORT_SYMBOL(get_pci_dma_ops);
694fc665b8SBecky Bruce 
702d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
71a4c9e328SKumar Gala {
72a4c9e328SKumar Gala 	struct pci_controller *phb;
73a4c9e328SKumar Gala 
74e60516e3SStephen Rothwell 	phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
75a4c9e328SKumar Gala 	if (phb == NULL)
76a4c9e328SKumar Gala 		return NULL;
77e60516e3SStephen Rothwell 	spin_lock(&hose_spinlock);
78e60516e3SStephen Rothwell 	phb->global_number = global_phb_number++;
79e60516e3SStephen Rothwell 	list_add_tail(&phb->list_node, &hose_list);
80e60516e3SStephen Rothwell 	spin_unlock(&hose_spinlock);
8144ef3390SStephen Rothwell 	phb->dn = dev;
82a4c9e328SKumar Gala 	phb->is_dynamic = mem_init_done;
83a4c9e328SKumar Gala #ifdef CONFIG_PPC64
84a4c9e328SKumar Gala 	if (dev) {
85a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
86a4c9e328SKumar Gala 
87a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
88a4c9e328SKumar Gala 			nid = -1;
89a4c9e328SKumar Gala 
90a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
91a4c9e328SKumar Gala 	}
92a4c9e328SKumar Gala #endif
93a4c9e328SKumar Gala 	return phb;
94a4c9e328SKumar Gala }
95a4c9e328SKumar Gala 
96a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
97a4c9e328SKumar Gala {
98a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
99a4c9e328SKumar Gala 	list_del(&phb->list_node);
100a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
101a4c9e328SKumar Gala 
102a4c9e328SKumar Gala 	if (phb->is_dynamic)
103a4c9e328SKumar Gala 		kfree(phb);
104a4c9e328SKumar Gala }
105a4c9e328SKumar Gala 
106c3bd517dSMilton Miller static resource_size_t pcibios_io_size(const struct pci_controller *hose)
107c3bd517dSMilton Miller {
108c3bd517dSMilton Miller #ifdef CONFIG_PPC64
109c3bd517dSMilton Miller 	return hose->pci_io_size;
110c3bd517dSMilton Miller #else
11128f65c11SJoe Perches 	return resource_size(&hose->io_resource);
112c3bd517dSMilton Miller #endif
113c3bd517dSMilton Miller }
114c3bd517dSMilton Miller 
1156dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
1166dfbde20SBenjamin Herrenschmidt {
1176dfbde20SBenjamin Herrenschmidt 	int ret = 0;
1186dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
119c3bd517dSMilton Miller 	resource_size_t size;
1206dfbde20SBenjamin Herrenschmidt 
1216dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
1226dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
123c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
1246dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
1256dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
1266dfbde20SBenjamin Herrenschmidt 			ret = 1;
1276dfbde20SBenjamin Herrenschmidt 			break;
1286dfbde20SBenjamin Herrenschmidt 		}
1296dfbde20SBenjamin Herrenschmidt 	}
1306dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
1316dfbde20SBenjamin Herrenschmidt 	return ret;
1326dfbde20SBenjamin Herrenschmidt }
1336dfbde20SBenjamin Herrenschmidt 
134c3bd517dSMilton Miller unsigned long pci_address_to_pio(phys_addr_t address)
135c3bd517dSMilton Miller {
136c3bd517dSMilton Miller 	struct pci_controller *hose;
137c3bd517dSMilton Miller 	resource_size_t size;
138c3bd517dSMilton Miller 	unsigned long ret = ~0;
139c3bd517dSMilton Miller 
140c3bd517dSMilton Miller 	spin_lock(&hose_spinlock);
141c3bd517dSMilton Miller 	list_for_each_entry(hose, &hose_list, list_node) {
142c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
143c3bd517dSMilton Miller 		if (address >= hose->io_base_phys &&
144c3bd517dSMilton Miller 		    address < (hose->io_base_phys + size)) {
145c3bd517dSMilton Miller 			unsigned long base =
146c3bd517dSMilton Miller 				(unsigned long)hose->io_base_virt - _IO_BASE;
147c3bd517dSMilton Miller 			ret = base + (address - hose->io_base_phys);
148c3bd517dSMilton Miller 			break;
149c3bd517dSMilton Miller 		}
150c3bd517dSMilton Miller 	}
151c3bd517dSMilton Miller 	spin_unlock(&hose_spinlock);
152c3bd517dSMilton Miller 
153c3bd517dSMilton Miller 	return ret;
154c3bd517dSMilton Miller }
155c3bd517dSMilton Miller EXPORT_SYMBOL_GPL(pci_address_to_pio);
156c3bd517dSMilton Miller 
1575516b540SKumar Gala /*
1585516b540SKumar Gala  * Return the domain number for this bus.
1595516b540SKumar Gala  */
1605516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
1615516b540SKumar Gala {
1625516b540SKumar Gala 	struct pci_controller *hose = pci_bus_to_host(bus);
1635516b540SKumar Gala 
1645516b540SKumar Gala 	return hose->global_number;
1655516b540SKumar Gala }
1665516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
16758083dadSKumar Gala 
168a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
169a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
170a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
171a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
172a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
173a4c9e328SKumar Gala  * config cycles.
174a4c9e328SKumar Gala  */
175a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
176a4c9e328SKumar Gala {
177a4c9e328SKumar Gala 	while(node) {
178a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
179a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
18044ef3390SStephen Rothwell 			if (hose->dn == node)
181a4c9e328SKumar Gala 				return hose;
182a4c9e328SKumar Gala 		node = node->parent;
183a4c9e328SKumar Gala 	}
184a4c9e328SKumar Gala 	return NULL;
185a4c9e328SKumar Gala }
186a4c9e328SKumar Gala 
18758083dadSKumar Gala static ssize_t pci_show_devspec(struct device *dev,
18858083dadSKumar Gala 		struct device_attribute *attr, char *buf)
18958083dadSKumar Gala {
19058083dadSKumar Gala 	struct pci_dev *pdev;
19158083dadSKumar Gala 	struct device_node *np;
19258083dadSKumar Gala 
19358083dadSKumar Gala 	pdev = to_pci_dev (dev);
19458083dadSKumar Gala 	np = pci_device_to_OF_node(pdev);
19558083dadSKumar Gala 	if (np == NULL || np->full_name == NULL)
19658083dadSKumar Gala 		return 0;
19758083dadSKumar Gala 	return sprintf(buf, "%s", np->full_name);
19858083dadSKumar Gala }
19958083dadSKumar Gala static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
20058083dadSKumar Gala 
20158083dadSKumar Gala /* Add sysfs properties */
2024f3731daSTony Breeds int pcibios_add_platform_entries(struct pci_dev *pdev)
20358083dadSKumar Gala {
2044f3731daSTony Breeds 	return device_create_file(&pdev->dev, &dev_attr_devspec);
20558083dadSKumar Gala }
20658083dadSKumar Gala 
207a2b7390aSStephen Rothwell char __devinit *pcibios_setup(char *str)
20858083dadSKumar Gala {
20958083dadSKumar Gala 	return str;
21058083dadSKumar Gala }
21158083dadSKumar Gala 
21258083dadSKumar Gala /*
21358083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
21458083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
21558083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
21658083dadSKumar Gala  */
21758083dadSKumar Gala int pci_read_irq_line(struct pci_dev *pci_dev)
21858083dadSKumar Gala {
21958083dadSKumar Gala 	struct of_irq oirq;
22058083dadSKumar Gala 	unsigned int virq;
22158083dadSKumar Gala 
22250c9bc2fSBenjamin Herrenschmidt 	/* The current device-tree that iSeries generates from the HV
22350c9bc2fSBenjamin Herrenschmidt 	 * PCI informations doesn't contain proper interrupt routing,
22450c9bc2fSBenjamin Herrenschmidt 	 * and all the fallback would do is print out crap, so we
22550c9bc2fSBenjamin Herrenschmidt 	 * don't attempt to resolve the interrupts here at all, some
22650c9bc2fSBenjamin Herrenschmidt 	 * iSeries specific fixup does it.
22750c9bc2fSBenjamin Herrenschmidt 	 *
22850c9bc2fSBenjamin Herrenschmidt 	 * In the long run, we will hopefully fix the generated device-tree
22950c9bc2fSBenjamin Herrenschmidt 	 * instead.
23050c9bc2fSBenjamin Herrenschmidt 	 */
23150c9bc2fSBenjamin Herrenschmidt #ifdef CONFIG_PPC_ISERIES
23250c9bc2fSBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_ISERIES))
23350c9bc2fSBenjamin Herrenschmidt 		return -1;
23450c9bc2fSBenjamin Herrenschmidt #endif
23550c9bc2fSBenjamin Herrenschmidt 
236b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
23758083dadSKumar Gala 
23858083dadSKumar Gala #ifdef DEBUG
23958083dadSKumar Gala 	memset(&oirq, 0xff, sizeof(oirq));
24058083dadSKumar Gala #endif
24158083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
24258083dadSKumar Gala 	if (of_irq_map_pci(pci_dev, &oirq)) {
24358083dadSKumar Gala 		u8 line, pin;
24458083dadSKumar Gala 
24558083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
24658083dadSKumar Gala 		 * space and map that through the default controller. We
24758083dadSKumar Gala 		 * also set the type to level low since that's what PCI
24858083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
24958083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
25058083dadSKumar Gala 		 * function.
25158083dadSKumar Gala 		 */
25258083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
25358083dadSKumar Gala 			return -1;
25458083dadSKumar Gala 		if (pin == 0)
25558083dadSKumar Gala 			return -1;
25658083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
25754a24cbbSBenjamin Herrenschmidt 		    line == 0xff || line == 0) {
25858083dadSKumar Gala 			return -1;
25958083dadSKumar Gala 		}
260b0494bc8SBenjamin Herrenschmidt 		pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
26154a24cbbSBenjamin Herrenschmidt 			 line, pin);
26258083dadSKumar Gala 
26358083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
26458083dadSKumar Gala 		if (virq != NO_IRQ)
265ec775d0eSThomas Gleixner 			irq_set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
26658083dadSKumar Gala 	} else {
267b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
26858083dadSKumar Gala 			 oirq.size, oirq.specifier[0], oirq.specifier[1],
26959b608c2SBenjamin Herrenschmidt 			 oirq.controller ? oirq.controller->full_name :
27059b608c2SBenjamin Herrenschmidt 			 "<default>");
27158083dadSKumar Gala 
27258083dadSKumar Gala 		virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
27358083dadSKumar Gala 					     oirq.size);
27458083dadSKumar Gala 	}
27558083dadSKumar Gala 	if(virq == NO_IRQ) {
276b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Failed to map !\n");
27758083dadSKumar Gala 		return -1;
27858083dadSKumar Gala 	}
27958083dadSKumar Gala 
280b0494bc8SBenjamin Herrenschmidt 	pr_debug(" Mapped to linux irq %d\n", virq);
28158083dadSKumar Gala 
28258083dadSKumar Gala 	pci_dev->irq = virq;
28358083dadSKumar Gala 
28458083dadSKumar Gala 	return 0;
28558083dadSKumar Gala }
28658083dadSKumar Gala EXPORT_SYMBOL(pci_read_irq_line);
28758083dadSKumar Gala 
28858083dadSKumar Gala /*
28958083dadSKumar Gala  * Platform support for /proc/bus/pci/X/Y mmap()s,
29058083dadSKumar Gala  * modelled on the sparc64 implementation by Dave Miller.
29158083dadSKumar Gala  *  -- paulus.
29258083dadSKumar Gala  */
29358083dadSKumar Gala 
29458083dadSKumar Gala /*
29558083dadSKumar Gala  * Adjust vm_pgoff of VMA such that it is the physical page offset
29658083dadSKumar Gala  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
29758083dadSKumar Gala  *
29858083dadSKumar Gala  * Basically, the user finds the base address for his device which he wishes
29958083dadSKumar Gala  * to mmap.  They read the 32-bit value from the config space base register,
30058083dadSKumar Gala  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
30158083dadSKumar Gala  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
30258083dadSKumar Gala  *
30358083dadSKumar Gala  * Returns negative error code on failure, zero on success.
30458083dadSKumar Gala  */
30558083dadSKumar Gala static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
30658083dadSKumar Gala 					       resource_size_t *offset,
30758083dadSKumar Gala 					       enum pci_mmap_state mmap_state)
30858083dadSKumar Gala {
30958083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
31058083dadSKumar Gala 	unsigned long io_offset = 0;
31158083dadSKumar Gala 	int i, res_bit;
31258083dadSKumar Gala 
31358083dadSKumar Gala 	if (hose == 0)
31458083dadSKumar Gala 		return NULL;		/* should never happen */
31558083dadSKumar Gala 
31658083dadSKumar Gala 	/* If memory, add on the PCI bridge address offset */
31758083dadSKumar Gala 	if (mmap_state == pci_mmap_mem) {
31858083dadSKumar Gala #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
31958083dadSKumar Gala 		*offset += hose->pci_mem_offset;
32058083dadSKumar Gala #endif
32158083dadSKumar Gala 		res_bit = IORESOURCE_MEM;
32258083dadSKumar Gala 	} else {
32358083dadSKumar Gala 		io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
32458083dadSKumar Gala 		*offset += io_offset;
32558083dadSKumar Gala 		res_bit = IORESOURCE_IO;
32658083dadSKumar Gala 	}
32758083dadSKumar Gala 
32858083dadSKumar Gala 	/*
32958083dadSKumar Gala 	 * Check that the offset requested corresponds to one of the
33058083dadSKumar Gala 	 * resources of the device.
33158083dadSKumar Gala 	 */
33258083dadSKumar Gala 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
33358083dadSKumar Gala 		struct resource *rp = &dev->resource[i];
33458083dadSKumar Gala 		int flags = rp->flags;
33558083dadSKumar Gala 
33658083dadSKumar Gala 		/* treat ROM as memory (should be already) */
33758083dadSKumar Gala 		if (i == PCI_ROM_RESOURCE)
33858083dadSKumar Gala 			flags |= IORESOURCE_MEM;
33958083dadSKumar Gala 
34058083dadSKumar Gala 		/* Active and same type? */
34158083dadSKumar Gala 		if ((flags & res_bit) == 0)
34258083dadSKumar Gala 			continue;
34358083dadSKumar Gala 
34458083dadSKumar Gala 		/* In the range of this resource? */
34558083dadSKumar Gala 		if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
34658083dadSKumar Gala 			continue;
34758083dadSKumar Gala 
34858083dadSKumar Gala 		/* found it! construct the final physical address */
34958083dadSKumar Gala 		if (mmap_state == pci_mmap_io)
35058083dadSKumar Gala 			*offset += hose->io_base_phys - io_offset;
35158083dadSKumar Gala 		return rp;
35258083dadSKumar Gala 	}
35358083dadSKumar Gala 
35458083dadSKumar Gala 	return NULL;
35558083dadSKumar Gala }
35658083dadSKumar Gala 
35758083dadSKumar Gala /*
35858083dadSKumar Gala  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
35958083dadSKumar Gala  * device mapping.
36058083dadSKumar Gala  */
36158083dadSKumar Gala static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
36258083dadSKumar Gala 				      pgprot_t protection,
36358083dadSKumar Gala 				      enum pci_mmap_state mmap_state,
36458083dadSKumar Gala 				      int write_combine)
36558083dadSKumar Gala {
36658083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
36758083dadSKumar Gala 
36858083dadSKumar Gala 	/* Write combine is always 0 on non-memory space mappings. On
36958083dadSKumar Gala 	 * memory space, if the user didn't pass 1, we check for a
37058083dadSKumar Gala 	 * "prefetchable" resource. This is a bit hackish, but we use
37158083dadSKumar Gala 	 * this to workaround the inability of /sysfs to provide a write
37258083dadSKumar Gala 	 * combine bit
37358083dadSKumar Gala 	 */
37458083dadSKumar Gala 	if (mmap_state != pci_mmap_mem)
37558083dadSKumar Gala 		write_combine = 0;
37658083dadSKumar Gala 	else if (write_combine == 0) {
37758083dadSKumar Gala 		if (rp->flags & IORESOURCE_PREFETCH)
37858083dadSKumar Gala 			write_combine = 1;
37958083dadSKumar Gala 	}
38058083dadSKumar Gala 
38158083dadSKumar Gala 	/* XXX would be nice to have a way to ask for write-through */
38258083dadSKumar Gala 	if (write_combine)
38364b3d0e8SBenjamin Herrenschmidt 		return pgprot_noncached_wc(prot);
38458083dadSKumar Gala 	else
38564b3d0e8SBenjamin Herrenschmidt 		return pgprot_noncached(prot);
38658083dadSKumar Gala }
38758083dadSKumar Gala 
38858083dadSKumar Gala /*
38958083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
39058083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
39158083dadSKumar Gala  * above routine
39258083dadSKumar Gala  */
39358083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
39458083dadSKumar Gala 				  unsigned long pfn,
39558083dadSKumar Gala 				  unsigned long size,
39664b3d0e8SBenjamin Herrenschmidt 				  pgprot_t prot)
39758083dadSKumar Gala {
39858083dadSKumar Gala 	struct pci_dev *pdev = NULL;
39958083dadSKumar Gala 	struct resource *found = NULL;
4007c12d906SBenjamin Herrenschmidt 	resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
40158083dadSKumar Gala 	int i;
40258083dadSKumar Gala 
40358083dadSKumar Gala 	if (page_is_ram(pfn))
40464b3d0e8SBenjamin Herrenschmidt 		return prot;
40558083dadSKumar Gala 
40664b3d0e8SBenjamin Herrenschmidt 	prot = pgprot_noncached(prot);
40758083dadSKumar Gala 	for_each_pci_dev(pdev) {
40858083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
40958083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
41058083dadSKumar Gala 			int flags = rp->flags;
41158083dadSKumar Gala 
41258083dadSKumar Gala 			/* Active and same type? */
41358083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
41458083dadSKumar Gala 				continue;
41558083dadSKumar Gala 			/* In the range of this resource? */
41658083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
41758083dadSKumar Gala 			    offset > rp->end)
41858083dadSKumar Gala 				continue;
41958083dadSKumar Gala 			found = rp;
42058083dadSKumar Gala 			break;
42158083dadSKumar Gala 		}
42258083dadSKumar Gala 		if (found)
42358083dadSKumar Gala 			break;
42458083dadSKumar Gala 	}
42558083dadSKumar Gala 	if (found) {
42658083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
42764b3d0e8SBenjamin Herrenschmidt 			prot = pgprot_noncached_wc(prot);
42858083dadSKumar Gala 		pci_dev_put(pdev);
42958083dadSKumar Gala 	}
43058083dadSKumar Gala 
431b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
43264b3d0e8SBenjamin Herrenschmidt 		 (unsigned long long)offset, pgprot_val(prot));
43358083dadSKumar Gala 
43464b3d0e8SBenjamin Herrenschmidt 	return prot;
43558083dadSKumar Gala }
43658083dadSKumar Gala 
43758083dadSKumar Gala 
43858083dadSKumar Gala /*
43958083dadSKumar Gala  * Perform the actual remap of the pages for a PCI device mapping, as
44058083dadSKumar Gala  * appropriate for this architecture.  The region in the process to map
44158083dadSKumar Gala  * is described by vm_start and vm_end members of VMA, the base physical
44258083dadSKumar Gala  * address is found in vm_pgoff.
44358083dadSKumar Gala  * The pci device structure is provided so that architectures may make mapping
44458083dadSKumar Gala  * decisions on a per-device or per-bus basis.
44558083dadSKumar Gala  *
44658083dadSKumar Gala  * Returns a negative error code on failure, zero on success.
44758083dadSKumar Gala  */
44858083dadSKumar Gala int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
44958083dadSKumar Gala 			enum pci_mmap_state mmap_state, int write_combine)
45058083dadSKumar Gala {
4517c12d906SBenjamin Herrenschmidt 	resource_size_t offset =
4527c12d906SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
45358083dadSKumar Gala 	struct resource *rp;
45458083dadSKumar Gala 	int ret;
45558083dadSKumar Gala 
45658083dadSKumar Gala 	rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
45758083dadSKumar Gala 	if (rp == NULL)
45858083dadSKumar Gala 		return -EINVAL;
45958083dadSKumar Gala 
46058083dadSKumar Gala 	vma->vm_pgoff = offset >> PAGE_SHIFT;
46158083dadSKumar Gala 	vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
46258083dadSKumar Gala 						  vma->vm_page_prot,
46358083dadSKumar Gala 						  mmap_state, write_combine);
46458083dadSKumar Gala 
46558083dadSKumar Gala 	ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
46658083dadSKumar Gala 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
46758083dadSKumar Gala 
46858083dadSKumar Gala 	return ret;
46958083dadSKumar Gala }
47058083dadSKumar Gala 
471e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO read access on a bus */
472e9f82cb7SBenjamin Herrenschmidt int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
473e9f82cb7SBenjamin Herrenschmidt {
474e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
475e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
476e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
477e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
478e9f82cb7SBenjamin Herrenschmidt 
479e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
480e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
481e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
482e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
483e9f82cb7SBenjamin Herrenschmidt 	 */
484e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
485e9f82cb7SBenjamin Herrenschmidt 	offset += port;
486e9f82cb7SBenjamin Herrenschmidt 
487e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
488e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
489e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
490e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
491e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
492e9f82cb7SBenjamin Herrenschmidt 
493e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
494e9f82cb7SBenjamin Herrenschmidt 	case 1:
495e9f82cb7SBenjamin Herrenschmidt 		*((u8 *)val) = in_8(addr);
496e9f82cb7SBenjamin Herrenschmidt 		return 1;
497e9f82cb7SBenjamin Herrenschmidt 	case 2:
498e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
499e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
500e9f82cb7SBenjamin Herrenschmidt 		*((u16 *)val) = in_le16(addr);
501e9f82cb7SBenjamin Herrenschmidt 		return 2;
502e9f82cb7SBenjamin Herrenschmidt 	case 4:
503e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
504e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
505e9f82cb7SBenjamin Herrenschmidt 		*((u32 *)val) = in_le32(addr);
506e9f82cb7SBenjamin Herrenschmidt 		return 4;
507e9f82cb7SBenjamin Herrenschmidt 	}
508e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
509e9f82cb7SBenjamin Herrenschmidt }
510e9f82cb7SBenjamin Herrenschmidt 
511e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO write access on a bus */
512e9f82cb7SBenjamin Herrenschmidt int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
513e9f82cb7SBenjamin Herrenschmidt {
514e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
515e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
516e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
517e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
518e9f82cb7SBenjamin Herrenschmidt 
519e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
520e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
521e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
522e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
523e9f82cb7SBenjamin Herrenschmidt 	 */
524e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
525e9f82cb7SBenjamin Herrenschmidt 	offset += port;
526e9f82cb7SBenjamin Herrenschmidt 
527e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
528e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
529e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
530e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
531e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
532e9f82cb7SBenjamin Herrenschmidt 
533e9f82cb7SBenjamin Herrenschmidt 	/* WARNING: The generic code is idiotic. It gets passed a pointer
534e9f82cb7SBenjamin Herrenschmidt 	 * to what can be a 1, 2 or 4 byte quantity and always reads that
535e9f82cb7SBenjamin Herrenschmidt 	 * as a u32, which means that we have to correct the location of
536e9f82cb7SBenjamin Herrenschmidt 	 * the data read within those 32 bits for size 1 and 2
537e9f82cb7SBenjamin Herrenschmidt 	 */
538e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
539e9f82cb7SBenjamin Herrenschmidt 	case 1:
540e9f82cb7SBenjamin Herrenschmidt 		out_8(addr, val >> 24);
541e9f82cb7SBenjamin Herrenschmidt 		return 1;
542e9f82cb7SBenjamin Herrenschmidt 	case 2:
543e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
544e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
545e9f82cb7SBenjamin Herrenschmidt 		out_le16(addr, val >> 16);
546e9f82cb7SBenjamin Herrenschmidt 		return 2;
547e9f82cb7SBenjamin Herrenschmidt 	case 4:
548e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
549e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
550e9f82cb7SBenjamin Herrenschmidt 		out_le32(addr, val);
551e9f82cb7SBenjamin Herrenschmidt 		return 4;
552e9f82cb7SBenjamin Herrenschmidt 	}
553e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
554e9f82cb7SBenjamin Herrenschmidt }
555e9f82cb7SBenjamin Herrenschmidt 
556e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO or memory mmap access on a bus */
557e9f82cb7SBenjamin Herrenschmidt int pci_mmap_legacy_page_range(struct pci_bus *bus,
558e9f82cb7SBenjamin Herrenschmidt 			       struct vm_area_struct *vma,
559e9f82cb7SBenjamin Herrenschmidt 			       enum pci_mmap_state mmap_state)
560e9f82cb7SBenjamin Herrenschmidt {
561e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
562e9f82cb7SBenjamin Herrenschmidt 	resource_size_t offset =
563e9f82cb7SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
564e9f82cb7SBenjamin Herrenschmidt 	resource_size_t size = vma->vm_end - vma->vm_start;
565e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp;
566e9f82cb7SBenjamin Herrenschmidt 
567e9f82cb7SBenjamin Herrenschmidt 	pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
568e9f82cb7SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number,
569e9f82cb7SBenjamin Herrenschmidt 		 mmap_state == pci_mmap_mem ? "MEM" : "IO",
570e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)offset,
571e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)(offset + size - 1));
572e9f82cb7SBenjamin Herrenschmidt 
573e9f82cb7SBenjamin Herrenschmidt 	if (mmap_state == pci_mmap_mem) {
5745b11abfdSBenjamin Herrenschmidt 		/* Hack alert !
5755b11abfdSBenjamin Herrenschmidt 		 *
5765b11abfdSBenjamin Herrenschmidt 		 * Because X is lame and can fail starting if it gets an error trying
5775b11abfdSBenjamin Herrenschmidt 		 * to mmap legacy_mem (instead of just moving on without legacy memory
5785b11abfdSBenjamin Herrenschmidt 		 * access) we fake it here by giving it anonymous memory, effectively
5795b11abfdSBenjamin Herrenschmidt 		 * behaving just like /dev/zero
5805b11abfdSBenjamin Herrenschmidt 		 */
5815b11abfdSBenjamin Herrenschmidt 		if ((offset + size) > hose->isa_mem_size) {
5825b11abfdSBenjamin Herrenschmidt 			printk(KERN_DEBUG
5835b11abfdSBenjamin Herrenschmidt 			       "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
5845b11abfdSBenjamin Herrenschmidt 			       current->comm, current->pid, pci_domain_nr(bus), bus->number);
5855b11abfdSBenjamin Herrenschmidt 			if (vma->vm_flags & VM_SHARED)
5865b11abfdSBenjamin Herrenschmidt 				return shmem_zero_setup(vma);
5875b11abfdSBenjamin Herrenschmidt 			return 0;
5885b11abfdSBenjamin Herrenschmidt 		}
589e9f82cb7SBenjamin Herrenschmidt 		offset += hose->isa_mem_phys;
590e9f82cb7SBenjamin Herrenschmidt 	} else {
591e9f82cb7SBenjamin Herrenschmidt 		unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
592e9f82cb7SBenjamin Herrenschmidt 		unsigned long roffset = offset + io_offset;
593e9f82cb7SBenjamin Herrenschmidt 		rp = &hose->io_resource;
594e9f82cb7SBenjamin Herrenschmidt 		if (!(rp->flags & IORESOURCE_IO))
595e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
596e9f82cb7SBenjamin Herrenschmidt 		if (roffset < rp->start || (roffset + size) > rp->end)
597e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
598e9f82cb7SBenjamin Herrenschmidt 		offset += hose->io_base_phys;
599e9f82cb7SBenjamin Herrenschmidt 	}
600e9f82cb7SBenjamin Herrenschmidt 	pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
601e9f82cb7SBenjamin Herrenschmidt 
602e9f82cb7SBenjamin Herrenschmidt 	vma->vm_pgoff = offset >> PAGE_SHIFT;
60364b3d0e8SBenjamin Herrenschmidt 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
604e9f82cb7SBenjamin Herrenschmidt 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
605e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_end - vma->vm_start,
606e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_page_prot);
607e9f82cb7SBenjamin Herrenschmidt }
608e9f82cb7SBenjamin Herrenschmidt 
60958083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
61058083dadSKumar Gala 			  const struct resource *rsrc,
61158083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
61258083dadSKumar Gala {
61358083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
61458083dadSKumar Gala 	resource_size_t offset = 0;
61558083dadSKumar Gala 
61658083dadSKumar Gala 	if (hose == NULL)
61758083dadSKumar Gala 		return;
61858083dadSKumar Gala 
61958083dadSKumar Gala 	if (rsrc->flags & IORESOURCE_IO)
62058083dadSKumar Gala 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
62158083dadSKumar Gala 
62258083dadSKumar Gala 	/* We pass a fully fixed up address to userland for MMIO instead of
62358083dadSKumar Gala 	 * a BAR value because X is lame and expects to be able to use that
62458083dadSKumar Gala 	 * to pass to /dev/mem !
62558083dadSKumar Gala 	 *
62658083dadSKumar Gala 	 * That means that we'll have potentially 64 bits values where some
62758083dadSKumar Gala 	 * userland apps only expect 32 (like X itself since it thinks only
62858083dadSKumar Gala 	 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
62958083dadSKumar Gala 	 * 32 bits CHRPs :-(
63058083dadSKumar Gala 	 *
63158083dadSKumar Gala 	 * Hopefully, the sysfs insterface is immune to that gunk. Once X
63258083dadSKumar Gala 	 * has been fixed (and the fix spread enough), we can re-enable the
63358083dadSKumar Gala 	 * 2 lines below and pass down a BAR value to userland. In that case
63458083dadSKumar Gala 	 * we'll also have to re-enable the matching code in
63558083dadSKumar Gala 	 * __pci_mmap_make_offset().
63658083dadSKumar Gala 	 *
63758083dadSKumar Gala 	 * BenH.
63858083dadSKumar Gala 	 */
63958083dadSKumar Gala #if 0
64058083dadSKumar Gala 	else if (rsrc->flags & IORESOURCE_MEM)
64158083dadSKumar Gala 		offset = hose->pci_mem_offset;
64258083dadSKumar Gala #endif
64358083dadSKumar Gala 
64458083dadSKumar Gala 	*start = rsrc->start - offset;
64558083dadSKumar Gala 	*end = rsrc->end - offset;
64658083dadSKumar Gala }
64713dccb9eSBenjamin Herrenschmidt 
64813dccb9eSBenjamin Herrenschmidt /**
64913dccb9eSBenjamin Herrenschmidt  * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
65013dccb9eSBenjamin Herrenschmidt  * @hose: newly allocated pci_controller to be setup
65113dccb9eSBenjamin Herrenschmidt  * @dev: device node of the host bridge
65213dccb9eSBenjamin Herrenschmidt  * @primary: set if primary bus (32 bits only, soon to be deprecated)
65313dccb9eSBenjamin Herrenschmidt  *
65413dccb9eSBenjamin Herrenschmidt  * This function will parse the "ranges" property of a PCI host bridge device
65513dccb9eSBenjamin Herrenschmidt  * node and setup the resource mapping of a pci controller based on its
65613dccb9eSBenjamin Herrenschmidt  * content.
65713dccb9eSBenjamin Herrenschmidt  *
65813dccb9eSBenjamin Herrenschmidt  * Life would be boring if it wasn't for a few issues that we have to deal
65913dccb9eSBenjamin Herrenschmidt  * with here:
66013dccb9eSBenjamin Herrenschmidt  *
66113dccb9eSBenjamin Herrenschmidt  *   - We can only cope with one IO space range and up to 3 Memory space
66213dccb9eSBenjamin Herrenschmidt  *     ranges. However, some machines (thanks Apple !) tend to split their
66313dccb9eSBenjamin Herrenschmidt  *     space into lots of small contiguous ranges. So we have to coalesce.
66413dccb9eSBenjamin Herrenschmidt  *
66513dccb9eSBenjamin Herrenschmidt  *   - We can only cope with all memory ranges having the same offset
66613dccb9eSBenjamin Herrenschmidt  *     between CPU addresses and PCI addresses. Unfortunately, some bridges
66713dccb9eSBenjamin Herrenschmidt  *     are setup for a large 1:1 mapping along with a small "window" which
66813dccb9eSBenjamin Herrenschmidt  *     maps PCI address 0 to some arbitrary high address of the CPU space in
66913dccb9eSBenjamin Herrenschmidt  *     order to give access to the ISA memory hole.
67013dccb9eSBenjamin Herrenschmidt  *     The way out of here that I've chosen for now is to always set the
67113dccb9eSBenjamin Herrenschmidt  *     offset based on the first resource found, then override it if we
67213dccb9eSBenjamin Herrenschmidt  *     have a different offset and the previous was set by an ISA hole.
67313dccb9eSBenjamin Herrenschmidt  *
67413dccb9eSBenjamin Herrenschmidt  *   - Some busses have IO space not starting at 0, which causes trouble with
67513dccb9eSBenjamin Herrenschmidt  *     the way we do our IO resource renumbering. The code somewhat deals with
67613dccb9eSBenjamin Herrenschmidt  *     it for 64 bits but I would expect problems on 32 bits.
67713dccb9eSBenjamin Herrenschmidt  *
67813dccb9eSBenjamin Herrenschmidt  *   - Some 32 bits platforms such as 4xx can have physical space larger than
67913dccb9eSBenjamin Herrenschmidt  *     32 bits so we need to use 64 bits values for the parsing
68013dccb9eSBenjamin Herrenschmidt  */
68113dccb9eSBenjamin Herrenschmidt void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
68213dccb9eSBenjamin Herrenschmidt 					    struct device_node *dev,
68313dccb9eSBenjamin Herrenschmidt 					    int primary)
68413dccb9eSBenjamin Herrenschmidt {
68513dccb9eSBenjamin Herrenschmidt 	const u32 *ranges;
68613dccb9eSBenjamin Herrenschmidt 	int rlen;
68713dccb9eSBenjamin Herrenschmidt 	int pna = of_n_addr_cells(dev);
68813dccb9eSBenjamin Herrenschmidt 	int np = pna + 5;
68913dccb9eSBenjamin Herrenschmidt 	int memno = 0, isa_hole = -1;
69013dccb9eSBenjamin Herrenschmidt 	u32 pci_space;
69113dccb9eSBenjamin Herrenschmidt 	unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
69213dccb9eSBenjamin Herrenschmidt 	unsigned long long isa_mb = 0;
69313dccb9eSBenjamin Herrenschmidt 	struct resource *res;
69413dccb9eSBenjamin Herrenschmidt 
69513dccb9eSBenjamin Herrenschmidt 	printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
69613dccb9eSBenjamin Herrenschmidt 	       dev->full_name, primary ? "(primary)" : "");
69713dccb9eSBenjamin Herrenschmidt 
69813dccb9eSBenjamin Herrenschmidt 	/* Get ranges property */
69913dccb9eSBenjamin Herrenschmidt 	ranges = of_get_property(dev, "ranges", &rlen);
70013dccb9eSBenjamin Herrenschmidt 	if (ranges == NULL)
70113dccb9eSBenjamin Herrenschmidt 		return;
70213dccb9eSBenjamin Herrenschmidt 
70313dccb9eSBenjamin Herrenschmidt 	/* Parse it */
70413dccb9eSBenjamin Herrenschmidt 	while ((rlen -= np * 4) >= 0) {
70513dccb9eSBenjamin Herrenschmidt 		/* Read next ranges element */
70613dccb9eSBenjamin Herrenschmidt 		pci_space = ranges[0];
70713dccb9eSBenjamin Herrenschmidt 		pci_addr = of_read_number(ranges + 1, 2);
70813dccb9eSBenjamin Herrenschmidt 		cpu_addr = of_translate_address(dev, ranges + 3);
70913dccb9eSBenjamin Herrenschmidt 		size = of_read_number(ranges + pna + 3, 2);
71013dccb9eSBenjamin Herrenschmidt 		ranges += np;
711e9f82cb7SBenjamin Herrenschmidt 
712e9f82cb7SBenjamin Herrenschmidt 		/* If we failed translation or got a zero-sized region
713e9f82cb7SBenjamin Herrenschmidt 		 * (some FW try to feed us with non sensical zero sized regions
714e9f82cb7SBenjamin Herrenschmidt 		 * such as power3 which look like some kind of attempt at exposing
715e9f82cb7SBenjamin Herrenschmidt 		 * the VGA memory hole)
716e9f82cb7SBenjamin Herrenschmidt 		 */
71713dccb9eSBenjamin Herrenschmidt 		if (cpu_addr == OF_BAD_ADDR || size == 0)
71813dccb9eSBenjamin Herrenschmidt 			continue;
71913dccb9eSBenjamin Herrenschmidt 
72013dccb9eSBenjamin Herrenschmidt 		/* Now consume following elements while they are contiguous */
72113dccb9eSBenjamin Herrenschmidt 		for (; rlen >= np * sizeof(u32);
72213dccb9eSBenjamin Herrenschmidt 		     ranges += np, rlen -= np * 4) {
72313dccb9eSBenjamin Herrenschmidt 			if (ranges[0] != pci_space)
72413dccb9eSBenjamin Herrenschmidt 				break;
72513dccb9eSBenjamin Herrenschmidt 			pci_next = of_read_number(ranges + 1, 2);
72613dccb9eSBenjamin Herrenschmidt 			cpu_next = of_translate_address(dev, ranges + 3);
72713dccb9eSBenjamin Herrenschmidt 			if (pci_next != pci_addr + size ||
72813dccb9eSBenjamin Herrenschmidt 			    cpu_next != cpu_addr + size)
72913dccb9eSBenjamin Herrenschmidt 				break;
73013dccb9eSBenjamin Herrenschmidt 			size += of_read_number(ranges + pna + 3, 2);
73113dccb9eSBenjamin Herrenschmidt 		}
73213dccb9eSBenjamin Herrenschmidt 
73313dccb9eSBenjamin Herrenschmidt 		/* Act based on address space type */
73413dccb9eSBenjamin Herrenschmidt 		res = NULL;
73513dccb9eSBenjamin Herrenschmidt 		switch ((pci_space >> 24) & 0x3) {
73613dccb9eSBenjamin Herrenschmidt 		case 1:		/* PCI IO space */
73713dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
73813dccb9eSBenjamin Herrenschmidt 			       "  IO 0x%016llx..0x%016llx -> 0x%016llx\n",
73913dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr);
74013dccb9eSBenjamin Herrenschmidt 
74113dccb9eSBenjamin Herrenschmidt 			/* We support only one IO range */
74213dccb9eSBenjamin Herrenschmidt 			if (hose->pci_io_size) {
74313dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
74413dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
74513dccb9eSBenjamin Herrenschmidt 				continue;
74613dccb9eSBenjamin Herrenschmidt 			}
74713dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32
74813dccb9eSBenjamin Herrenschmidt 			/* On 32 bits, limit I/O space to 16MB */
74913dccb9eSBenjamin Herrenschmidt 			if (size > 0x01000000)
75013dccb9eSBenjamin Herrenschmidt 				size = 0x01000000;
75113dccb9eSBenjamin Herrenschmidt 
75213dccb9eSBenjamin Herrenschmidt 			/* 32 bits needs to map IOs here */
75313dccb9eSBenjamin Herrenschmidt 			hose->io_base_virt = ioremap(cpu_addr, size);
75413dccb9eSBenjamin Herrenschmidt 
75513dccb9eSBenjamin Herrenschmidt 			/* Expect trouble if pci_addr is not 0 */
75613dccb9eSBenjamin Herrenschmidt 			if (primary)
75713dccb9eSBenjamin Herrenschmidt 				isa_io_base =
75813dccb9eSBenjamin Herrenschmidt 					(unsigned long)hose->io_base_virt;
75913dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
76013dccb9eSBenjamin Herrenschmidt 			/* pci_io_size and io_base_phys always represent IO
76113dccb9eSBenjamin Herrenschmidt 			 * space starting at 0 so we factor in pci_addr
76213dccb9eSBenjamin Herrenschmidt 			 */
76313dccb9eSBenjamin Herrenschmidt 			hose->pci_io_size = pci_addr + size;
76413dccb9eSBenjamin Herrenschmidt 			hose->io_base_phys = cpu_addr - pci_addr;
76513dccb9eSBenjamin Herrenschmidt 
76613dccb9eSBenjamin Herrenschmidt 			/* Build resource */
76713dccb9eSBenjamin Herrenschmidt 			res = &hose->io_resource;
76813dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_IO;
76913dccb9eSBenjamin Herrenschmidt 			res->start = pci_addr;
77013dccb9eSBenjamin Herrenschmidt 			break;
77113dccb9eSBenjamin Herrenschmidt 		case 2:		/* PCI Memory space */
77267260ac9SBenjamin Herrenschmidt 		case 3:		/* PCI 64 bits Memory space */
77313dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
77413dccb9eSBenjamin Herrenschmidt 			       " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
77513dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr,
77613dccb9eSBenjamin Herrenschmidt 			       (pci_space & 0x40000000) ? "Prefetch" : "");
77713dccb9eSBenjamin Herrenschmidt 
77813dccb9eSBenjamin Herrenschmidt 			/* We support only 3 memory ranges */
77913dccb9eSBenjamin Herrenschmidt 			if (memno >= 3) {
78013dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
78113dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
78213dccb9eSBenjamin Herrenschmidt 				continue;
78313dccb9eSBenjamin Herrenschmidt 			}
78413dccb9eSBenjamin Herrenschmidt 			/* Handles ISA memory hole space here */
78513dccb9eSBenjamin Herrenschmidt 			if (pci_addr == 0) {
78613dccb9eSBenjamin Herrenschmidt 				isa_mb = cpu_addr;
78713dccb9eSBenjamin Herrenschmidt 				isa_hole = memno;
78813dccb9eSBenjamin Herrenschmidt 				if (primary || isa_mem_base == 0)
78913dccb9eSBenjamin Herrenschmidt 					isa_mem_base = cpu_addr;
790e9f82cb7SBenjamin Herrenschmidt 				hose->isa_mem_phys = cpu_addr;
791e9f82cb7SBenjamin Herrenschmidt 				hose->isa_mem_size = size;
79213dccb9eSBenjamin Herrenschmidt 			}
79313dccb9eSBenjamin Herrenschmidt 
79413dccb9eSBenjamin Herrenschmidt 			/* We get the PCI/Mem offset from the first range or
79513dccb9eSBenjamin Herrenschmidt 			 * the, current one if the offset came from an ISA
79613dccb9eSBenjamin Herrenschmidt 			 * hole. If they don't match, bugger.
79713dccb9eSBenjamin Herrenschmidt 			 */
79813dccb9eSBenjamin Herrenschmidt 			if (memno == 0 ||
79913dccb9eSBenjamin Herrenschmidt 			    (isa_hole >= 0 && pci_addr != 0 &&
80013dccb9eSBenjamin Herrenschmidt 			     hose->pci_mem_offset == isa_mb))
80113dccb9eSBenjamin Herrenschmidt 				hose->pci_mem_offset = cpu_addr - pci_addr;
80213dccb9eSBenjamin Herrenschmidt 			else if (pci_addr != 0 &&
80313dccb9eSBenjamin Herrenschmidt 				 hose->pci_mem_offset != cpu_addr - pci_addr) {
80413dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
80513dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (offset mismatch) !\n");
80613dccb9eSBenjamin Herrenschmidt 				continue;
80713dccb9eSBenjamin Herrenschmidt 			}
80813dccb9eSBenjamin Herrenschmidt 
80913dccb9eSBenjamin Herrenschmidt 			/* Build resource */
81013dccb9eSBenjamin Herrenschmidt 			res = &hose->mem_resources[memno++];
81113dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_MEM;
81213dccb9eSBenjamin Herrenschmidt 			if (pci_space & 0x40000000)
81313dccb9eSBenjamin Herrenschmidt 				res->flags |= IORESOURCE_PREFETCH;
81413dccb9eSBenjamin Herrenschmidt 			res->start = cpu_addr;
81513dccb9eSBenjamin Herrenschmidt 			break;
81613dccb9eSBenjamin Herrenschmidt 		}
81713dccb9eSBenjamin Herrenschmidt 		if (res != NULL) {
81813dccb9eSBenjamin Herrenschmidt 			res->name = dev->full_name;
81913dccb9eSBenjamin Herrenschmidt 			res->end = res->start + size - 1;
82013dccb9eSBenjamin Herrenschmidt 			res->parent = NULL;
82113dccb9eSBenjamin Herrenschmidt 			res->sibling = NULL;
82213dccb9eSBenjamin Herrenschmidt 			res->child = NULL;
82313dccb9eSBenjamin Herrenschmidt 		}
82413dccb9eSBenjamin Herrenschmidt 	}
82513dccb9eSBenjamin Herrenschmidt 
8268db13a0eSBenjamin Herrenschmidt 	/* If there's an ISA hole and the pci_mem_offset is -not- matching
8278db13a0eSBenjamin Herrenschmidt 	 * the ISA hole offset, then we need to remove the ISA hole from
8288db13a0eSBenjamin Herrenschmidt 	 * the resource list for that brige
8298db13a0eSBenjamin Herrenschmidt 	 */
8308db13a0eSBenjamin Herrenschmidt 	if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
8318db13a0eSBenjamin Herrenschmidt 		unsigned int next = isa_hole + 1;
8328db13a0eSBenjamin Herrenschmidt 		printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb);
8338db13a0eSBenjamin Herrenschmidt 		if (next < memno)
8348db13a0eSBenjamin Herrenschmidt 			memmove(&hose->mem_resources[isa_hole],
8358db13a0eSBenjamin Herrenschmidt 				&hose->mem_resources[next],
8368db13a0eSBenjamin Herrenschmidt 				sizeof(struct resource) * (memno - next));
8378db13a0eSBenjamin Herrenschmidt 		hose->mem_resources[--memno].flags = 0;
83813dccb9eSBenjamin Herrenschmidt 	}
83913dccb9eSBenjamin Herrenschmidt }
840fa462f2dSBenjamin Herrenschmidt 
841fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */
842fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus)
843fa462f2dSBenjamin Herrenschmidt {
844fa462f2dSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
8451fd0f525SBenjamin Herrenschmidt 
8460e47ff1cSRob Herring 	if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
847fa462f2dSBenjamin Herrenschmidt 		return 0;
8480e47ff1cSRob Herring 	if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
849fa462f2dSBenjamin Herrenschmidt 		return hose->global_number != 0;
850fa462f2dSBenjamin Herrenschmidt 	return 1;
851fa462f2dSBenjamin Herrenschmidt }
852fa462f2dSBenjamin Herrenschmidt 
853fe2d338cSBenjamin Herrenschmidt void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
854fe2d338cSBenjamin Herrenschmidt 			     struct resource *res)
855fe2d338cSBenjamin Herrenschmidt {
856fe2d338cSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
857fe2d338cSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
858fe2d338cSBenjamin Herrenschmidt 
859fe2d338cSBenjamin Herrenschmidt 	if (!hose)
860fe2d338cSBenjamin Herrenschmidt 		return;
861fe2d338cSBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
862fe2d338cSBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
863fe2d338cSBenjamin Herrenschmidt 		mask = 0xffffffffu;
864fe2d338cSBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
865fe2d338cSBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
866fe2d338cSBenjamin Herrenschmidt 
867fe2d338cSBenjamin Herrenschmidt 	region->start = (res->start - offset) & mask;
868fe2d338cSBenjamin Herrenschmidt 	region->end = (res->end - offset) & mask;
869fe2d338cSBenjamin Herrenschmidt }
870fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_resource_to_bus);
871fe2d338cSBenjamin Herrenschmidt 
872fe2d338cSBenjamin Herrenschmidt void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
873fe2d338cSBenjamin Herrenschmidt 			     struct pci_bus_region *region)
874fe2d338cSBenjamin Herrenschmidt {
875fe2d338cSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
876fe2d338cSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
877fe2d338cSBenjamin Herrenschmidt 
878fe2d338cSBenjamin Herrenschmidt 	if (!hose)
879fe2d338cSBenjamin Herrenschmidt 		return;
880fe2d338cSBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
881fe2d338cSBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
882fe2d338cSBenjamin Herrenschmidt 		mask = 0xffffffffu;
883fe2d338cSBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
884fe2d338cSBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
885fe2d338cSBenjamin Herrenschmidt 	res->start = (region->start + offset) & mask;
886fe2d338cSBenjamin Herrenschmidt 	res->end = (region->end + offset) & mask;
887fe2d338cSBenjamin Herrenschmidt }
888fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_bus_to_resource);
889bf5e2ba2SBenjamin Herrenschmidt 
890bf5e2ba2SBenjamin Herrenschmidt /* Fixup a bus resource into a linux resource */
891bf5e2ba2SBenjamin Herrenschmidt static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
892bf5e2ba2SBenjamin Herrenschmidt {
893bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
894bf5e2ba2SBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
895bf5e2ba2SBenjamin Herrenschmidt 
896bf5e2ba2SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
897bf5e2ba2SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
898bf5e2ba2SBenjamin Herrenschmidt 		mask = 0xffffffffu;
899bf5e2ba2SBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
900bf5e2ba2SBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
901bf5e2ba2SBenjamin Herrenschmidt 
902bf5e2ba2SBenjamin Herrenschmidt 	res->start = (res->start + offset) & mask;
903bf5e2ba2SBenjamin Herrenschmidt 	res->end = (res->end + offset) & mask;
904bf5e2ba2SBenjamin Herrenschmidt }
905bf5e2ba2SBenjamin Herrenschmidt 
906bf5e2ba2SBenjamin Herrenschmidt 
907bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are
908bf5e2ba2SBenjamin Herrenschmidt  * probed, but not for bridge ranges
909bf5e2ba2SBenjamin Herrenschmidt  */
910bf5e2ba2SBenjamin Herrenschmidt static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
911bf5e2ba2SBenjamin Herrenschmidt {
912bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
913bf5e2ba2SBenjamin Herrenschmidt 	int i;
914bf5e2ba2SBenjamin Herrenschmidt 
915bf5e2ba2SBenjamin Herrenschmidt 	if (!hose) {
916bf5e2ba2SBenjamin Herrenschmidt 		printk(KERN_ERR "No host bridge for PCI dev %s !\n",
917bf5e2ba2SBenjamin Herrenschmidt 		       pci_name(dev));
918bf5e2ba2SBenjamin Herrenschmidt 		return;
919bf5e2ba2SBenjamin Herrenschmidt 	}
920bf5e2ba2SBenjamin Herrenschmidt 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
921bf5e2ba2SBenjamin Herrenschmidt 		struct resource *res = dev->resource + i;
922bf5e2ba2SBenjamin Herrenschmidt 		if (!res->flags)
923bf5e2ba2SBenjamin Herrenschmidt 			continue;
9240e47ff1cSRob Herring 		/* On platforms that have PCI_PROBE_ONLY set, we don't
9257f172890SBenjamin Herrenschmidt 		 * consider 0 as an unassigned BAR value. It's technically
9267f172890SBenjamin Herrenschmidt 		 * a valid value, but linux doesn't like it... so when we can
9277f172890SBenjamin Herrenschmidt 		 * re-assign things, we do so, but if we can't, we keep it
9287f172890SBenjamin Herrenschmidt 		 * around and hope for the best...
9297f172890SBenjamin Herrenschmidt 		 */
9300e47ff1cSRob Herring 		if (res->start == 0 && !pci_has_flag(PCI_PROBE_ONLY)) {
931bf5e2ba2SBenjamin Herrenschmidt 			pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
932bf5e2ba2SBenjamin Herrenschmidt 				 pci_name(dev), i,
933bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->start,
934bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->end,
935bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned int)res->flags);
936bf5e2ba2SBenjamin Herrenschmidt 			res->end -= res->start;
937bf5e2ba2SBenjamin Herrenschmidt 			res->start = 0;
938bf5e2ba2SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
939bf5e2ba2SBenjamin Herrenschmidt 			continue;
940bf5e2ba2SBenjamin Herrenschmidt 		}
941bf5e2ba2SBenjamin Herrenschmidt 
942bf5e2ba2SBenjamin Herrenschmidt 		pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
943bf5e2ba2SBenjamin Herrenschmidt 			 pci_name(dev), i,
944bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->start,\
945bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
946bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned int)res->flags);
947bf5e2ba2SBenjamin Herrenschmidt 
948bf5e2ba2SBenjamin Herrenschmidt 		fixup_resource(res, dev);
949b5561511SBenjamin Herrenschmidt 
950b5561511SBenjamin Herrenschmidt 		pr_debug("PCI:%s            %016llx-%016llx\n",
951b5561511SBenjamin Herrenschmidt 			 pci_name(dev),
952b5561511SBenjamin Herrenschmidt 			 (unsigned long long)res->start,
953b5561511SBenjamin Herrenschmidt 			 (unsigned long long)res->end);
954bf5e2ba2SBenjamin Herrenschmidt 	}
955bf5e2ba2SBenjamin Herrenschmidt 
956bf5e2ba2SBenjamin Herrenschmidt 	/* Call machine specific resource fixup */
957bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_resources)
958bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_resources(dev);
959bf5e2ba2SBenjamin Herrenschmidt }
960bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
961bf5e2ba2SBenjamin Herrenschmidt 
962b5561511SBenjamin Herrenschmidt /* This function tries to figure out if a bridge resource has been initialized
963b5561511SBenjamin Herrenschmidt  * by the firmware or not. It doesn't have to be absolutely bullet proof, but
964b5561511SBenjamin Herrenschmidt  * things go more smoothly when it gets it right. It should covers cases such
965b5561511SBenjamin Herrenschmidt  * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
966b5561511SBenjamin Herrenschmidt  */
967b5561511SBenjamin Herrenschmidt static int __devinit pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
968b5561511SBenjamin Herrenschmidt 							   struct resource *res)
969bf5e2ba2SBenjamin Herrenschmidt {
970be8cbcd8SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
971bf5e2ba2SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
972b5561511SBenjamin Herrenschmidt 	resource_size_t offset;
973b5561511SBenjamin Herrenschmidt 	u16 command;
974b5561511SBenjamin Herrenschmidt 	int i;
975bf5e2ba2SBenjamin Herrenschmidt 
976b5561511SBenjamin Herrenschmidt 	/* We don't do anything if PCI_PROBE_ONLY is set */
9770e47ff1cSRob Herring 	if (pci_has_flag(PCI_PROBE_ONLY))
978b5561511SBenjamin Herrenschmidt 		return 0;
979bf5e2ba2SBenjamin Herrenschmidt 
980b5561511SBenjamin Herrenschmidt 	/* Job is a bit different between memory and IO */
981b5561511SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_MEM) {
982b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0 (res != pci_mem_offset) then it's probably been
983b5561511SBenjamin Herrenschmidt 		 * initialized by somebody
984bf5e2ba2SBenjamin Herrenschmidt 		 */
985b5561511SBenjamin Herrenschmidt 		if (res->start != hose->pci_mem_offset)
986b5561511SBenjamin Herrenschmidt 			return 0;
987b5561511SBenjamin Herrenschmidt 
988b5561511SBenjamin Herrenschmidt 		/* The BAR is 0, let's check if memory decoding is enabled on
989b5561511SBenjamin Herrenschmidt 		 * the bridge. If not, we consider it unassigned
990b5561511SBenjamin Herrenschmidt 		 */
991b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
992b5561511SBenjamin Herrenschmidt 		if ((command & PCI_COMMAND_MEMORY) == 0)
993b5561511SBenjamin Herrenschmidt 			return 1;
994b5561511SBenjamin Herrenschmidt 
995b5561511SBenjamin Herrenschmidt 		/* Memory decoding is enabled and the BAR is 0. If any of the bridge
996b5561511SBenjamin Herrenschmidt 		 * resources covers that starting address (0 then it's good enough for
997b5561511SBenjamin Herrenschmidt 		 * us for memory
998b5561511SBenjamin Herrenschmidt 		 */
999b5561511SBenjamin Herrenschmidt 		for (i = 0; i < 3; i++) {
1000b5561511SBenjamin Herrenschmidt 			if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
1001b5561511SBenjamin Herrenschmidt 			    hose->mem_resources[i].start == hose->pci_mem_offset)
1002b5561511SBenjamin Herrenschmidt 				return 0;
1003b5561511SBenjamin Herrenschmidt 		}
1004b5561511SBenjamin Herrenschmidt 
1005b5561511SBenjamin Herrenschmidt 		/* Well, it starts at 0 and we know it will collide so we may as
1006b5561511SBenjamin Herrenschmidt 		 * well consider it as unassigned. That covers the Apple case.
1007b5561511SBenjamin Herrenschmidt 		 */
1008b5561511SBenjamin Herrenschmidt 		return 1;
1009b5561511SBenjamin Herrenschmidt 	} else {
1010b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0, then we consider it assigned */
1011b5561511SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1012b5561511SBenjamin Herrenschmidt 		if (((res->start - offset) & 0xfffffffful) != 0)
1013b5561511SBenjamin Herrenschmidt 			return 0;
1014b5561511SBenjamin Herrenschmidt 
1015b5561511SBenjamin Herrenschmidt 		/* Here, we are a bit different than memory as typically IO space
1016b5561511SBenjamin Herrenschmidt 		 * starting at low addresses -is- valid. What we do instead if that
1017b5561511SBenjamin Herrenschmidt 		 * we consider as unassigned anything that doesn't have IO enabled
1018b5561511SBenjamin Herrenschmidt 		 * in the PCI command register, and that's it.
1019b5561511SBenjamin Herrenschmidt 		 */
1020b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1021b5561511SBenjamin Herrenschmidt 		if (command & PCI_COMMAND_IO)
1022b5561511SBenjamin Herrenschmidt 			return 0;
1023b5561511SBenjamin Herrenschmidt 
1024b5561511SBenjamin Herrenschmidt 		/* It's starting at 0 and IO is disabled in the bridge, consider
1025b5561511SBenjamin Herrenschmidt 		 * it unassigned
1026b5561511SBenjamin Herrenschmidt 		 */
1027b5561511SBenjamin Herrenschmidt 		return 1;
1028b5561511SBenjamin Herrenschmidt 	}
1029b5561511SBenjamin Herrenschmidt }
1030b5561511SBenjamin Herrenschmidt 
1031b5561511SBenjamin Herrenschmidt /* Fixup resources of a PCI<->PCI bridge */
1032b5561511SBenjamin Herrenschmidt static void __devinit pcibios_fixup_bridge(struct pci_bus *bus)
1033b5561511SBenjamin Herrenschmidt {
1034bf5e2ba2SBenjamin Herrenschmidt 	struct resource *res;
1035bf5e2ba2SBenjamin Herrenschmidt 	int i;
1036bf5e2ba2SBenjamin Herrenschmidt 
1037b5561511SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
1038b5561511SBenjamin Herrenschmidt 
103989a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
104089a74eccSBjorn Helgaas 		if (!res || !res->flags)
1041bf5e2ba2SBenjamin Herrenschmidt 			continue;
1042b188b2aeSKumar Gala 		if (i >= 3 && bus->self->transparent)
1043b188b2aeSKumar Gala 			continue;
1044be8cbcd8SBenjamin Herrenschmidt 
1045bf5e2ba2SBenjamin Herrenschmidt 		pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
1046bf5e2ba2SBenjamin Herrenschmidt 			 pci_name(dev), i,
1047bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->start,\
1048bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
1049bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned int)res->flags);
1050bf5e2ba2SBenjamin Herrenschmidt 
1051b5561511SBenjamin Herrenschmidt 		/* Perform fixup */
1052bf5e2ba2SBenjamin Herrenschmidt 		fixup_resource(res, dev);
1053b5561511SBenjamin Herrenschmidt 
1054b5561511SBenjamin Herrenschmidt 		/* Try to detect uninitialized P2P bridge resources,
1055b5561511SBenjamin Herrenschmidt 		 * and clear them out so they get re-assigned later
1056b5561511SBenjamin Herrenschmidt 		 */
1057b5561511SBenjamin Herrenschmidt 		if (pcibios_uninitialized_bridge_resource(bus, res)) {
1058b5561511SBenjamin Herrenschmidt 			res->flags = 0;
1059b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            (unassigned)\n", pci_name(dev));
1060b5561511SBenjamin Herrenschmidt 		} else {
1061b5561511SBenjamin Herrenschmidt 
1062b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            %016llx-%016llx\n",
1063b5561511SBenjamin Herrenschmidt 				 pci_name(dev),
1064b5561511SBenjamin Herrenschmidt 				 (unsigned long long)res->start,
1065b5561511SBenjamin Herrenschmidt 				 (unsigned long long)res->end);
1066bf5e2ba2SBenjamin Herrenschmidt 		}
1067bf5e2ba2SBenjamin Herrenschmidt 	}
1068b5561511SBenjamin Herrenschmidt }
1069b5561511SBenjamin Herrenschmidt 
10708b8da358SBenjamin Herrenschmidt void __devinit pcibios_setup_bus_self(struct pci_bus *bus)
10718b8da358SBenjamin Herrenschmidt {
10727eef440aSBenjamin Herrenschmidt 	/* Fix up the bus resources for P2P bridges */
10738b8da358SBenjamin Herrenschmidt 	if (bus->self != NULL)
10748b8da358SBenjamin Herrenschmidt 		pcibios_fixup_bridge(bus);
10758b8da358SBenjamin Herrenschmidt 
10768b8da358SBenjamin Herrenschmidt 	/* Platform specific bus fixups. This is currently only used
10777eef440aSBenjamin Herrenschmidt 	 * by fsl_pci and I'm hoping to get rid of it at some point
10788b8da358SBenjamin Herrenschmidt 	 */
10798b8da358SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_bus)
10808b8da358SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_bus(bus);
10818b8da358SBenjamin Herrenschmidt 
10828b8da358SBenjamin Herrenschmidt 	/* Setup bus DMA mappings */
10838b8da358SBenjamin Herrenschmidt 	if (ppc_md.pci_dma_bus_setup)
10848b8da358SBenjamin Herrenschmidt 		ppc_md.pci_dma_bus_setup(bus);
10858b8da358SBenjamin Herrenschmidt }
10868b8da358SBenjamin Herrenschmidt 
10877eef440aSBenjamin Herrenschmidt void __devinit pcibios_setup_bus_devices(struct pci_bus *bus)
10887eef440aSBenjamin Herrenschmidt {
10897eef440aSBenjamin Herrenschmidt 	struct pci_dev *dev;
10907eef440aSBenjamin Herrenschmidt 
10917eef440aSBenjamin Herrenschmidt 	pr_debug("PCI: Fixup bus devices %d (%s)\n",
10927eef440aSBenjamin Herrenschmidt 		 bus->number, bus->self ? pci_name(bus->self) : "PHB");
10937eef440aSBenjamin Herrenschmidt 
10947eef440aSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
10952d1c8618SBenjamin Herrenschmidt 		/* Cardbus can call us to add new devices to a bus, so ignore
10962d1c8618SBenjamin Herrenschmidt 		 * those who are already fully discovered
10972d1c8618SBenjamin Herrenschmidt 		 */
10982d1c8618SBenjamin Herrenschmidt 		if (dev->is_added)
10992d1c8618SBenjamin Herrenschmidt 			continue;
11002d1c8618SBenjamin Herrenschmidt 
11017eef440aSBenjamin Herrenschmidt 		/* Fixup NUMA node as it may not be setup yet by the generic
11027eef440aSBenjamin Herrenschmidt 		 * code and is needed by the DMA init
11037eef440aSBenjamin Herrenschmidt 		 */
11047eef440aSBenjamin Herrenschmidt 		set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
11057eef440aSBenjamin Herrenschmidt 
11067eef440aSBenjamin Herrenschmidt 		/* Hook up default DMA ops */
1107bc0df9ecSNishanth Aravamudan 		set_dma_ops(&dev->dev, pci_dma_ops);
1108738ef42eSBecky Bruce 		set_dma_offset(&dev->dev, PCI_DRAM_OFFSET);
11097eef440aSBenjamin Herrenschmidt 
11107eef440aSBenjamin Herrenschmidt 		/* Additional platform DMA/iommu setup */
11117eef440aSBenjamin Herrenschmidt 		if (ppc_md.pci_dma_dev_setup)
11127eef440aSBenjamin Herrenschmidt 			ppc_md.pci_dma_dev_setup(dev);
11137eef440aSBenjamin Herrenschmidt 
11147eef440aSBenjamin Herrenschmidt 		/* Read default IRQs and fixup if necessary */
11157eef440aSBenjamin Herrenschmidt 		pci_read_irq_line(dev);
11167eef440aSBenjamin Herrenschmidt 		if (ppc_md.pci_irq_fixup)
11177eef440aSBenjamin Herrenschmidt 			ppc_md.pci_irq_fixup(dev);
11187eef440aSBenjamin Herrenschmidt 	}
11197eef440aSBenjamin Herrenschmidt }
11207eef440aSBenjamin Herrenschmidt 
1121bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_fixup_bus(struct pci_bus *bus)
1122bf5e2ba2SBenjamin Herrenschmidt {
1123bf5e2ba2SBenjamin Herrenschmidt 	/* When called from the generic PCI probe, read PCI<->PCI bridge
11247eef440aSBenjamin Herrenschmidt 	 * bases. This is -not- called when generating the PCI tree from
11258b8da358SBenjamin Herrenschmidt 	 * the OF device-tree.
1126bf5e2ba2SBenjamin Herrenschmidt 	 */
1127bf5e2ba2SBenjamin Herrenschmidt 	if (bus->self != NULL)
1128bf5e2ba2SBenjamin Herrenschmidt 		pci_read_bridge_bases(bus);
11298b8da358SBenjamin Herrenschmidt 
11308b8da358SBenjamin Herrenschmidt 	/* Now fixup the bus bus */
11318b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_self(bus);
11328b8da358SBenjamin Herrenschmidt 
11338b8da358SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
11348b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
1135bf5e2ba2SBenjamin Herrenschmidt }
1136bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus);
1137bf5e2ba2SBenjamin Herrenschmidt 
11382d1c8618SBenjamin Herrenschmidt void __devinit pci_fixup_cardbus(struct pci_bus *bus)
11392d1c8618SBenjamin Herrenschmidt {
11402d1c8618SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
11412d1c8618SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
11422d1c8618SBenjamin Herrenschmidt }
11432d1c8618SBenjamin Herrenschmidt 
11442d1c8618SBenjamin Herrenschmidt 
11453fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev)
11463fd94c6bSBenjamin Herrenschmidt {
11470e47ff1cSRob Herring 	if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
11483fd94c6bSBenjamin Herrenschmidt 	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
11493fd94c6bSBenjamin Herrenschmidt 		return 1;
11503fd94c6bSBenjamin Herrenschmidt 	return 0;
11513fd94c6bSBenjamin Herrenschmidt }
11523fd94c6bSBenjamin Herrenschmidt 
11533fd94c6bSBenjamin Herrenschmidt /*
11543fd94c6bSBenjamin Herrenschmidt  * We need to avoid collisions with `mirrored' VGA ports
11553fd94c6bSBenjamin Herrenschmidt  * and other strange ISA hardware, so we always want the
11563fd94c6bSBenjamin Herrenschmidt  * addresses to be allocated in the 0x000-0x0ff region
11573fd94c6bSBenjamin Herrenschmidt  * modulo 0x400.
11583fd94c6bSBenjamin Herrenschmidt  *
11593fd94c6bSBenjamin Herrenschmidt  * Why? Because some silly external IO cards only decode
11603fd94c6bSBenjamin Herrenschmidt  * the low 10 bits of the IO address. The 0x00-0xff region
11613fd94c6bSBenjamin Herrenschmidt  * is reserved for motherboard devices that decode all 16
11623fd94c6bSBenjamin Herrenschmidt  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
11633fd94c6bSBenjamin Herrenschmidt  * but we want to try to avoid allocating at 0x2900-0x2bff
11643fd94c6bSBenjamin Herrenschmidt  * which might have be mirrored at 0x0100-0x03ff..
11653fd94c6bSBenjamin Herrenschmidt  */
11663b7a17fcSDominik Brodowski resource_size_t pcibios_align_resource(void *data, const struct resource *res,
11673fd94c6bSBenjamin Herrenschmidt 				resource_size_t size, resource_size_t align)
11683fd94c6bSBenjamin Herrenschmidt {
11693fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = data;
11703fd94c6bSBenjamin Herrenschmidt 	resource_size_t start = res->start;
11713fd94c6bSBenjamin Herrenschmidt 
1172b26b2d49SDominik Brodowski 	if (res->flags & IORESOURCE_IO) {
11733fd94c6bSBenjamin Herrenschmidt 		if (skip_isa_ioresource_align(dev))
1174b26b2d49SDominik Brodowski 			return start;
1175b26b2d49SDominik Brodowski 		if (start & 0x300)
11763fd94c6bSBenjamin Herrenschmidt 			start = (start + 0x3ff) & ~0x3ff;
11773fd94c6bSBenjamin Herrenschmidt 	}
1178b26b2d49SDominik Brodowski 
1179b26b2d49SDominik Brodowski 	return start;
11803fd94c6bSBenjamin Herrenschmidt }
11813fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource);
11823fd94c6bSBenjamin Herrenschmidt 
11833fd94c6bSBenjamin Herrenschmidt /*
11843fd94c6bSBenjamin Herrenschmidt  * Reparent resource children of pr that conflict with res
11853fd94c6bSBenjamin Herrenschmidt  * under res, and make res replace those children.
11863fd94c6bSBenjamin Herrenschmidt  */
11870f6023d5SHeiko Schocher static int reparent_resources(struct resource *parent,
11883fd94c6bSBenjamin Herrenschmidt 				     struct resource *res)
11893fd94c6bSBenjamin Herrenschmidt {
11903fd94c6bSBenjamin Herrenschmidt 	struct resource *p, **pp;
11913fd94c6bSBenjamin Herrenschmidt 	struct resource **firstpp = NULL;
11923fd94c6bSBenjamin Herrenschmidt 
11933fd94c6bSBenjamin Herrenschmidt 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
11943fd94c6bSBenjamin Herrenschmidt 		if (p->end < res->start)
11953fd94c6bSBenjamin Herrenschmidt 			continue;
11963fd94c6bSBenjamin Herrenschmidt 		if (res->end < p->start)
11973fd94c6bSBenjamin Herrenschmidt 			break;
11983fd94c6bSBenjamin Herrenschmidt 		if (p->start < res->start || p->end > res->end)
11993fd94c6bSBenjamin Herrenschmidt 			return -1;	/* not completely contained */
12003fd94c6bSBenjamin Herrenschmidt 		if (firstpp == NULL)
12013fd94c6bSBenjamin Herrenschmidt 			firstpp = pp;
12023fd94c6bSBenjamin Herrenschmidt 	}
12033fd94c6bSBenjamin Herrenschmidt 	if (firstpp == NULL)
12043fd94c6bSBenjamin Herrenschmidt 		return -1;	/* didn't find any conflicting entries? */
12053fd94c6bSBenjamin Herrenschmidt 	res->parent = parent;
12063fd94c6bSBenjamin Herrenschmidt 	res->child = *firstpp;
12073fd94c6bSBenjamin Herrenschmidt 	res->sibling = *pp;
12083fd94c6bSBenjamin Herrenschmidt 	*firstpp = res;
12093fd94c6bSBenjamin Herrenschmidt 	*pp = NULL;
12103fd94c6bSBenjamin Herrenschmidt 	for (p = res->child; p != NULL; p = p->sibling) {
12113fd94c6bSBenjamin Herrenschmidt 		p->parent = res;
1212b0494bc8SBenjamin Herrenschmidt 		pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n",
12133fd94c6bSBenjamin Herrenschmidt 			 p->name,
12143fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)p->start,
12153fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)p->end, res->name);
12163fd94c6bSBenjamin Herrenschmidt 	}
12173fd94c6bSBenjamin Herrenschmidt 	return 0;
12183fd94c6bSBenjamin Herrenschmidt }
12193fd94c6bSBenjamin Herrenschmidt 
12203fd94c6bSBenjamin Herrenschmidt /*
12213fd94c6bSBenjamin Herrenschmidt  *  Handle resources of PCI devices.  If the world were perfect, we could
12223fd94c6bSBenjamin Herrenschmidt  *  just allocate all the resource regions and do nothing more.  It isn't.
12233fd94c6bSBenjamin Herrenschmidt  *  On the other hand, we cannot just re-allocate all devices, as it would
12243fd94c6bSBenjamin Herrenschmidt  *  require us to know lots of host bridge internals.  So we attempt to
12253fd94c6bSBenjamin Herrenschmidt  *  keep as much of the original configuration as possible, but tweak it
12263fd94c6bSBenjamin Herrenschmidt  *  when it's found to be wrong.
12273fd94c6bSBenjamin Herrenschmidt  *
12283fd94c6bSBenjamin Herrenschmidt  *  Known BIOS problems we have to work around:
12293fd94c6bSBenjamin Herrenschmidt  *	- I/O or memory regions not configured
12303fd94c6bSBenjamin Herrenschmidt  *	- regions configured, but not enabled in the command register
12313fd94c6bSBenjamin Herrenschmidt  *	- bogus I/O addresses above 64K used
12323fd94c6bSBenjamin Herrenschmidt  *	- expansion ROMs left enabled (this may sound harmless, but given
12333fd94c6bSBenjamin Herrenschmidt  *	  the fact the PCI specs explicitly allow address decoders to be
12343fd94c6bSBenjamin Herrenschmidt  *	  shared between expansion ROMs and other resource regions, it's
12353fd94c6bSBenjamin Herrenschmidt  *	  at least dangerous)
12363fd94c6bSBenjamin Herrenschmidt  *
12373fd94c6bSBenjamin Herrenschmidt  *  Our solution:
12383fd94c6bSBenjamin Herrenschmidt  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
12393fd94c6bSBenjamin Herrenschmidt  *	    This gives us fixed barriers on where we can allocate.
12403fd94c6bSBenjamin Herrenschmidt  *	(2) Allocate resources for all enabled devices.  If there is
12413fd94c6bSBenjamin Herrenschmidt  *	    a collision, just mark the resource as unallocated. Also
12423fd94c6bSBenjamin Herrenschmidt  *	    disable expansion ROMs during this step.
12433fd94c6bSBenjamin Herrenschmidt  *	(3) Try to allocate resources for disabled devices.  If the
12443fd94c6bSBenjamin Herrenschmidt  *	    resources were assigned correctly, everything goes well,
12453fd94c6bSBenjamin Herrenschmidt  *	    if they weren't, they won't disturb allocation of other
12463fd94c6bSBenjamin Herrenschmidt  *	    resources.
12473fd94c6bSBenjamin Herrenschmidt  *	(4) Assign new addresses to resources which were either
12483fd94c6bSBenjamin Herrenschmidt  *	    not configured at all or misconfigured.  If explicitly
12493fd94c6bSBenjamin Herrenschmidt  *	    requested by the user, configure expansion ROM address
12503fd94c6bSBenjamin Herrenschmidt  *	    as well.
12513fd94c6bSBenjamin Herrenschmidt  */
12523fd94c6bSBenjamin Herrenschmidt 
1253e90a1318SNathan Fontenot void pcibios_allocate_bus_resources(struct pci_bus *bus)
12543fd94c6bSBenjamin Herrenschmidt {
1255e90a1318SNathan Fontenot 	struct pci_bus *b;
12563fd94c6bSBenjamin Herrenschmidt 	int i;
12573fd94c6bSBenjamin Herrenschmidt 	struct resource *res, *pr;
12583fd94c6bSBenjamin Herrenschmidt 
1259b5ae5f91SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1260b5ae5f91SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1261b5ae5f91SBenjamin Herrenschmidt 
126289a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
126389a74eccSBjorn Helgaas 		if (!res || !res->flags || res->start > res->end || res->parent)
12643fd94c6bSBenjamin Herrenschmidt 			continue;
12653fd94c6bSBenjamin Herrenschmidt 		if (bus->parent == NULL)
12663fd94c6bSBenjamin Herrenschmidt 			pr = (res->flags & IORESOURCE_IO) ?
12673fd94c6bSBenjamin Herrenschmidt 				&ioport_resource : &iomem_resource;
12683fd94c6bSBenjamin Herrenschmidt 		else {
12693fd94c6bSBenjamin Herrenschmidt 			/* Don't bother with non-root busses when
12703fd94c6bSBenjamin Herrenschmidt 			 * re-assigning all resources. We clear the
12713fd94c6bSBenjamin Herrenschmidt 			 * resource flags as if they were colliding
12723fd94c6bSBenjamin Herrenschmidt 			 * and as such ensure proper re-allocation
12733fd94c6bSBenjamin Herrenschmidt 			 * later.
12743fd94c6bSBenjamin Herrenschmidt 			 */
12750e47ff1cSRob Herring 			if (pci_has_flag(PCI_REASSIGN_ALL_RSRC))
12763fd94c6bSBenjamin Herrenschmidt 				goto clear_resource;
12773fd94c6bSBenjamin Herrenschmidt 			pr = pci_find_parent_resource(bus->self, res);
12783fd94c6bSBenjamin Herrenschmidt 			if (pr == res) {
12793fd94c6bSBenjamin Herrenschmidt 				/* this happens when the generic PCI
12803fd94c6bSBenjamin Herrenschmidt 				 * code (wrongly) decides that this
12813fd94c6bSBenjamin Herrenschmidt 				 * bridge is transparent  -- paulus
12823fd94c6bSBenjamin Herrenschmidt 				 */
12833fd94c6bSBenjamin Herrenschmidt 				continue;
12843fd94c6bSBenjamin Herrenschmidt 			}
12853fd94c6bSBenjamin Herrenschmidt 		}
12863fd94c6bSBenjamin Herrenschmidt 
1287b0494bc8SBenjamin Herrenschmidt 		pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
12883fd94c6bSBenjamin Herrenschmidt 			 "[0x%x], parent %p (%s)\n",
12893fd94c6bSBenjamin Herrenschmidt 			 bus->self ? pci_name(bus->self) : "PHB",
12903fd94c6bSBenjamin Herrenschmidt 			 bus->number, i,
12913fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)res->start,
12923fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)res->end,
12933fd94c6bSBenjamin Herrenschmidt 			 (unsigned int)res->flags,
12943fd94c6bSBenjamin Herrenschmidt 			 pr, (pr && pr->name) ? pr->name : "nil");
12953fd94c6bSBenjamin Herrenschmidt 
12963fd94c6bSBenjamin Herrenschmidt 		if (pr && !(pr->flags & IORESOURCE_UNSET)) {
12973fd94c6bSBenjamin Herrenschmidt 			if (request_resource(pr, res) == 0)
12983fd94c6bSBenjamin Herrenschmidt 				continue;
12993fd94c6bSBenjamin Herrenschmidt 			/*
13003fd94c6bSBenjamin Herrenschmidt 			 * Must be a conflict with an existing entry.
13013fd94c6bSBenjamin Herrenschmidt 			 * Move that entry (or entries) under the
13023fd94c6bSBenjamin Herrenschmidt 			 * bridge resource and try again.
13033fd94c6bSBenjamin Herrenschmidt 			 */
13043fd94c6bSBenjamin Herrenschmidt 			if (reparent_resources(pr, res) == 0)
13053fd94c6bSBenjamin Herrenschmidt 				continue;
13063fd94c6bSBenjamin Herrenschmidt 		}
1307e90a1318SNathan Fontenot 		printk(KERN_WARNING "PCI: Cannot allocate resource region "
1308e90a1318SNathan Fontenot 		       "%d of PCI bridge %d, will remap\n", i, bus->number);
13093fd94c6bSBenjamin Herrenschmidt clear_resource:
1310837c4ef1SYinghai Lu 		res->start = res->end = 0;
13113fd94c6bSBenjamin Herrenschmidt 		res->flags = 0;
13123fd94c6bSBenjamin Herrenschmidt 	}
1313e90a1318SNathan Fontenot 
1314e90a1318SNathan Fontenot 	list_for_each_entry(b, &bus->children, node)
1315e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
13163fd94c6bSBenjamin Herrenschmidt }
13173fd94c6bSBenjamin Herrenschmidt 
1318533b1928SPaul Mackerras static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
13193fd94c6bSBenjamin Herrenschmidt {
13203fd94c6bSBenjamin Herrenschmidt 	struct resource *pr, *r = &dev->resource[idx];
13213fd94c6bSBenjamin Herrenschmidt 
1322b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
13233fd94c6bSBenjamin Herrenschmidt 		 pci_name(dev), idx,
13243fd94c6bSBenjamin Herrenschmidt 		 (unsigned long long)r->start,
13253fd94c6bSBenjamin Herrenschmidt 		 (unsigned long long)r->end,
13263fd94c6bSBenjamin Herrenschmidt 		 (unsigned int)r->flags);
13273fd94c6bSBenjamin Herrenschmidt 
13283fd94c6bSBenjamin Herrenschmidt 	pr = pci_find_parent_resource(dev, r);
13293fd94c6bSBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||
13303fd94c6bSBenjamin Herrenschmidt 	    request_resource(pr, r) < 0) {
13313fd94c6bSBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
13323fd94c6bSBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
13333fd94c6bSBenjamin Herrenschmidt 		if (pr)
1334b0494bc8SBenjamin Herrenschmidt 			pr_debug("PCI:  parent is %p: %016llx-%016llx [%x]\n",
1335b0494bc8SBenjamin Herrenschmidt 				 pr,
13363fd94c6bSBenjamin Herrenschmidt 				 (unsigned long long)pr->start,
13373fd94c6bSBenjamin Herrenschmidt 				 (unsigned long long)pr->end,
13383fd94c6bSBenjamin Herrenschmidt 				 (unsigned int)pr->flags);
13393fd94c6bSBenjamin Herrenschmidt 		/* We'll assign a new address later */
13403fd94c6bSBenjamin Herrenschmidt 		r->flags |= IORESOURCE_UNSET;
13413fd94c6bSBenjamin Herrenschmidt 		r->end -= r->start;
13423fd94c6bSBenjamin Herrenschmidt 		r->start = 0;
13433fd94c6bSBenjamin Herrenschmidt 	}
13443fd94c6bSBenjamin Herrenschmidt }
13453fd94c6bSBenjamin Herrenschmidt 
13463fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass)
13473fd94c6bSBenjamin Herrenschmidt {
13483fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = NULL;
13493fd94c6bSBenjamin Herrenschmidt 	int idx, disabled;
13503fd94c6bSBenjamin Herrenschmidt 	u16 command;
13513fd94c6bSBenjamin Herrenschmidt 	struct resource *r;
13523fd94c6bSBenjamin Herrenschmidt 
13533fd94c6bSBenjamin Herrenschmidt 	for_each_pci_dev(dev) {
13543fd94c6bSBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1355ad892a63SBenjamin Herrenschmidt 		for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
13563fd94c6bSBenjamin Herrenschmidt 			r = &dev->resource[idx];
13573fd94c6bSBenjamin Herrenschmidt 			if (r->parent)		/* Already allocated */
13583fd94c6bSBenjamin Herrenschmidt 				continue;
13593fd94c6bSBenjamin Herrenschmidt 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
13603fd94c6bSBenjamin Herrenschmidt 				continue;	/* Not assigned at all */
1361ad892a63SBenjamin Herrenschmidt 			/* We only allocate ROMs on pass 1 just in case they
1362ad892a63SBenjamin Herrenschmidt 			 * have been screwed up by firmware
1363ad892a63SBenjamin Herrenschmidt 			 */
1364ad892a63SBenjamin Herrenschmidt 			if (idx == PCI_ROM_RESOURCE )
1365ad892a63SBenjamin Herrenschmidt 				disabled = 1;
13663fd94c6bSBenjamin Herrenschmidt 			if (r->flags & IORESOURCE_IO)
13673fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_IO);
13683fd94c6bSBenjamin Herrenschmidt 			else
13693fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_MEMORY);
1370533b1928SPaul Mackerras 			if (pass == disabled)
1371533b1928SPaul Mackerras 				alloc_resource(dev, idx);
13723fd94c6bSBenjamin Herrenschmidt 		}
13733fd94c6bSBenjamin Herrenschmidt 		if (pass)
13743fd94c6bSBenjamin Herrenschmidt 			continue;
13753fd94c6bSBenjamin Herrenschmidt 		r = &dev->resource[PCI_ROM_RESOURCE];
1376ad892a63SBenjamin Herrenschmidt 		if (r->flags) {
13773fd94c6bSBenjamin Herrenschmidt 			/* Turn the ROM off, leave the resource region,
13783fd94c6bSBenjamin Herrenschmidt 			 * but keep it unregistered.
13793fd94c6bSBenjamin Herrenschmidt 			 */
13803fd94c6bSBenjamin Herrenschmidt 			u32 reg;
1381ad892a63SBenjamin Herrenschmidt 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1382ad892a63SBenjamin Herrenschmidt 			if (reg & PCI_ROM_ADDRESS_ENABLE) {
1383b0494bc8SBenjamin Herrenschmidt 				pr_debug("PCI: Switching off ROM of %s\n",
1384b0494bc8SBenjamin Herrenschmidt 					 pci_name(dev));
13853fd94c6bSBenjamin Herrenschmidt 				r->flags &= ~IORESOURCE_ROM_ENABLE;
13863fd94c6bSBenjamin Herrenschmidt 				pci_write_config_dword(dev, dev->rom_base_reg,
13873fd94c6bSBenjamin Herrenschmidt 						       reg & ~PCI_ROM_ADDRESS_ENABLE);
13883fd94c6bSBenjamin Herrenschmidt 			}
13893fd94c6bSBenjamin Herrenschmidt 		}
13903fd94c6bSBenjamin Herrenschmidt 	}
1391ad892a63SBenjamin Herrenschmidt }
13923fd94c6bSBenjamin Herrenschmidt 
1393c1f34302SBenjamin Herrenschmidt static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
1394c1f34302SBenjamin Herrenschmidt {
1395c1f34302SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
1396c1f34302SBenjamin Herrenschmidt 	resource_size_t	offset;
1397c1f34302SBenjamin Herrenschmidt 	struct resource *res, *pres;
1398c1f34302SBenjamin Herrenschmidt 	int i;
1399c1f34302SBenjamin Herrenschmidt 
1400c1f34302SBenjamin Herrenschmidt 	pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
1401c1f34302SBenjamin Herrenschmidt 
1402c1f34302SBenjamin Herrenschmidt 	/* Check for IO */
1403c1f34302SBenjamin Herrenschmidt 	if (!(hose->io_resource.flags & IORESOURCE_IO))
1404c1f34302SBenjamin Herrenschmidt 		goto no_io;
1405c1f34302SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1406c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1407c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1408c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy IO";
1409c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_IO;
1410c1f34302SBenjamin Herrenschmidt 	res->start = offset;
1411c1f34302SBenjamin Herrenschmidt 	res->end = (offset + 0xfff) & 0xfffffffful;
1412c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate legacy IO: %pR\n", res);
1413c1f34302SBenjamin Herrenschmidt 	if (request_resource(&hose->io_resource, res)) {
1414c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1415c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1416c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1417c1f34302SBenjamin Herrenschmidt 		kfree(res);
1418c1f34302SBenjamin Herrenschmidt 	}
1419c1f34302SBenjamin Herrenschmidt 
1420c1f34302SBenjamin Herrenschmidt  no_io:
1421c1f34302SBenjamin Herrenschmidt 	/* Check for memory */
1422c1f34302SBenjamin Herrenschmidt 	offset = hose->pci_mem_offset;
1423c1f34302SBenjamin Herrenschmidt 	pr_debug("hose mem offset: %016llx\n", (unsigned long long)offset);
1424c1f34302SBenjamin Herrenschmidt 	for (i = 0; i < 3; i++) {
1425c1f34302SBenjamin Herrenschmidt 		pres = &hose->mem_resources[i];
1426c1f34302SBenjamin Herrenschmidt 		if (!(pres->flags & IORESOURCE_MEM))
1427c1f34302SBenjamin Herrenschmidt 			continue;
1428c1f34302SBenjamin Herrenschmidt 		pr_debug("hose mem res: %pR\n", pres);
1429c1f34302SBenjamin Herrenschmidt 		if ((pres->start - offset) <= 0xa0000 &&
1430c1f34302SBenjamin Herrenschmidt 		    (pres->end - offset) >= 0xbffff)
1431c1f34302SBenjamin Herrenschmidt 			break;
1432c1f34302SBenjamin Herrenschmidt 	}
1433c1f34302SBenjamin Herrenschmidt 	if (i >= 3)
1434c1f34302SBenjamin Herrenschmidt 		return;
1435c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1436c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1437c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy VGA memory";
1438c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_MEM;
1439c1f34302SBenjamin Herrenschmidt 	res->start = 0xa0000 + offset;
1440c1f34302SBenjamin Herrenschmidt 	res->end = 0xbffff + offset;
1441c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate VGA memory: %pR\n", res);
1442c1f34302SBenjamin Herrenschmidt 	if (request_resource(pres, res)) {
1443c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1444c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1445c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1446c1f34302SBenjamin Herrenschmidt 		kfree(res);
1447c1f34302SBenjamin Herrenschmidt 	}
1448c1f34302SBenjamin Herrenschmidt }
1449c1f34302SBenjamin Herrenschmidt 
14503fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void)
14513fd94c6bSBenjamin Herrenschmidt {
1452e90a1318SNathan Fontenot 	struct pci_bus *b;
1453e90a1318SNathan Fontenot 
14543fd94c6bSBenjamin Herrenschmidt 	/* Allocate and assign resources. If we re-assign everything, then
14553fd94c6bSBenjamin Herrenschmidt 	 * we skip the allocate phase
14563fd94c6bSBenjamin Herrenschmidt 	 */
1457e90a1318SNathan Fontenot 	list_for_each_entry(b, &pci_root_buses, node)
1458e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
14593fd94c6bSBenjamin Herrenschmidt 
14600e47ff1cSRob Herring 	if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
14613fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(0);
14623fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(1);
14633fd94c6bSBenjamin Herrenschmidt 	}
14643fd94c6bSBenjamin Herrenschmidt 
1465c1f34302SBenjamin Herrenschmidt 	/* Before we start assigning unassigned resource, we try to reserve
1466c1f34302SBenjamin Herrenschmidt 	 * the low IO area and the VGA memory area if they intersect the
1467c1f34302SBenjamin Herrenschmidt 	 * bus available resources to avoid allocating things on top of them
1468c1f34302SBenjamin Herrenschmidt 	 */
14690e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1470c1f34302SBenjamin Herrenschmidt 		list_for_each_entry(b, &pci_root_buses, node)
1471c1f34302SBenjamin Herrenschmidt 			pcibios_reserve_legacy_regions(b);
1472c1f34302SBenjamin Herrenschmidt 	}
1473c1f34302SBenjamin Herrenschmidt 
1474c1f34302SBenjamin Herrenschmidt 	/* Now, if the platform didn't decide to blindly trust the firmware,
1475c1f34302SBenjamin Herrenschmidt 	 * we proceed to assigning things that were left unassigned
1476c1f34302SBenjamin Herrenschmidt 	 */
14770e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1478a77acda0SWolfram Sang 		pr_debug("PCI: Assigning unassigned resources...\n");
14793fd94c6bSBenjamin Herrenschmidt 		pci_assign_unassigned_resources();
14803fd94c6bSBenjamin Herrenschmidt 	}
14813fd94c6bSBenjamin Herrenschmidt 
14823fd94c6bSBenjamin Herrenschmidt 	/* Call machine dependent fixup */
14833fd94c6bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup)
14843fd94c6bSBenjamin Herrenschmidt 		ppc_md.pcibios_fixup();
14853fd94c6bSBenjamin Herrenschmidt }
14863fd94c6bSBenjamin Herrenschmidt 
14873fd94c6bSBenjamin Herrenschmidt #ifdef CONFIG_HOTPLUG
14888b8da358SBenjamin Herrenschmidt 
1489fd6852c8SBenjamin Herrenschmidt /* This is used by the PCI hotplug driver to allocate resource
14903fd94c6bSBenjamin Herrenschmidt  * of newly plugged busses. We can try to consolidate with the
1491fd6852c8SBenjamin Herrenschmidt  * rest of the code later, for now, keep it as-is as our main
1492fd6852c8SBenjamin Herrenschmidt  * resource allocation function doesn't deal with sub-trees yet.
14933fd94c6bSBenjamin Herrenschmidt  */
1494baf75b0aSStephen Rothwell void pcibios_claim_one_bus(struct pci_bus *bus)
14953fd94c6bSBenjamin Herrenschmidt {
14963fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev;
14973fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *child_bus;
14983fd94c6bSBenjamin Herrenschmidt 
14993fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
15003fd94c6bSBenjamin Herrenschmidt 		int i;
15013fd94c6bSBenjamin Herrenschmidt 
15023fd94c6bSBenjamin Herrenschmidt 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
15033fd94c6bSBenjamin Herrenschmidt 			struct resource *r = &dev->resource[i];
15043fd94c6bSBenjamin Herrenschmidt 
15053fd94c6bSBenjamin Herrenschmidt 			if (r->parent || !r->start || !r->flags)
15063fd94c6bSBenjamin Herrenschmidt 				continue;
1507fd6852c8SBenjamin Herrenschmidt 
1508fd6852c8SBenjamin Herrenschmidt 			pr_debug("PCI: Claiming %s: "
1509fd6852c8SBenjamin Herrenschmidt 				 "Resource %d: %016llx..%016llx [%x]\n",
1510fd6852c8SBenjamin Herrenschmidt 				 pci_name(dev), i,
1511fd6852c8SBenjamin Herrenschmidt 				 (unsigned long long)r->start,
1512fd6852c8SBenjamin Herrenschmidt 				 (unsigned long long)r->end,
1513fd6852c8SBenjamin Herrenschmidt 				 (unsigned int)r->flags);
1514fd6852c8SBenjamin Herrenschmidt 
15153fd94c6bSBenjamin Herrenschmidt 			pci_claim_resource(dev, i);
15163fd94c6bSBenjamin Herrenschmidt 		}
15173fd94c6bSBenjamin Herrenschmidt 	}
15183fd94c6bSBenjamin Herrenschmidt 
15193fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(child_bus, &bus->children, node)
15203fd94c6bSBenjamin Herrenschmidt 		pcibios_claim_one_bus(child_bus);
15213fd94c6bSBenjamin Herrenschmidt }
1522fd6852c8SBenjamin Herrenschmidt 
1523fd6852c8SBenjamin Herrenschmidt 
1524fd6852c8SBenjamin Herrenschmidt /* pcibios_finish_adding_to_bus
1525fd6852c8SBenjamin Herrenschmidt  *
1526fd6852c8SBenjamin Herrenschmidt  * This is to be called by the hotplug code after devices have been
1527fd6852c8SBenjamin Herrenschmidt  * added to a bus, this include calling it for a PHB that is just
1528fd6852c8SBenjamin Herrenschmidt  * being added
1529fd6852c8SBenjamin Herrenschmidt  */
1530fd6852c8SBenjamin Herrenschmidt void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1531fd6852c8SBenjamin Herrenschmidt {
1532fd6852c8SBenjamin Herrenschmidt 	pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1533fd6852c8SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1534fd6852c8SBenjamin Herrenschmidt 
1535fd6852c8SBenjamin Herrenschmidt 	/* Allocate bus and devices resources */
1536fd6852c8SBenjamin Herrenschmidt 	pcibios_allocate_bus_resources(bus);
1537fd6852c8SBenjamin Herrenschmidt 	pcibios_claim_one_bus(bus);
1538fd6852c8SBenjamin Herrenschmidt 
1539fd6852c8SBenjamin Herrenschmidt 	/* Add new devices to global lists.  Register in proc, sysfs. */
1540fd6852c8SBenjamin Herrenschmidt 	pci_bus_add_devices(bus);
1541fd6852c8SBenjamin Herrenschmidt 
1542fd6852c8SBenjamin Herrenschmidt 	/* Fixup EEH */
1543fd6852c8SBenjamin Herrenschmidt 	eeh_add_device_tree_late(bus);
1544fd6852c8SBenjamin Herrenschmidt }
1545fd6852c8SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1546fd6852c8SBenjamin Herrenschmidt 
15473fd94c6bSBenjamin Herrenschmidt #endif /* CONFIG_HOTPLUG */
1548549beb9bSBenjamin Herrenschmidt 
1549549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask)
1550549beb9bSBenjamin Herrenschmidt {
1551549beb9bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_enable_device_hook)
1552549beb9bSBenjamin Herrenschmidt 		if (ppc_md.pcibios_enable_device_hook(dev))
1553549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1554549beb9bSBenjamin Herrenschmidt 
15557cfb5f9aSBjorn Helgaas 	return pci_enable_resources(dev, mask);
1556549beb9bSBenjamin Herrenschmidt }
155753280323SBenjamin Herrenschmidt 
155853280323SBenjamin Herrenschmidt void __devinit pcibios_setup_phb_resources(struct pci_controller *hose)
155953280323SBenjamin Herrenschmidt {
156053280323SBenjamin Herrenschmidt 	struct pci_bus *bus = hose->bus;
156153280323SBenjamin Herrenschmidt 	struct resource *res;
156253280323SBenjamin Herrenschmidt 	int i;
156353280323SBenjamin Herrenschmidt 
156453280323SBenjamin Herrenschmidt 	/* Hookup PHB IO resource */
156553280323SBenjamin Herrenschmidt 	bus->resource[0] = res = &hose->io_resource;
156653280323SBenjamin Herrenschmidt 
156753280323SBenjamin Herrenschmidt 	if (!res->flags) {
156853280323SBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: I/O resource not set for host"
156953280323SBenjamin Herrenschmidt 		       " bridge %s (domain %d)\n",
157053280323SBenjamin Herrenschmidt 		       hose->dn->full_name, hose->global_number);
157153280323SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
157253280323SBenjamin Herrenschmidt 		/* Workaround for lack of IO resource only on 32-bit */
157353280323SBenjamin Herrenschmidt 		res->start = (unsigned long)hose->io_base_virt - isa_io_base;
157453280323SBenjamin Herrenschmidt 		res->end = res->start + IO_SPACE_LIMIT;
157553280323SBenjamin Herrenschmidt 		res->flags = IORESOURCE_IO;
157653280323SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
157753280323SBenjamin Herrenschmidt 	}
157853280323SBenjamin Herrenschmidt 
157953280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB IO resource    = %016llx-%016llx [%lx]\n",
158053280323SBenjamin Herrenschmidt 		 (unsigned long long)res->start,
158153280323SBenjamin Herrenschmidt 		 (unsigned long long)res->end,
158253280323SBenjamin Herrenschmidt 		 (unsigned long)res->flags);
158353280323SBenjamin Herrenschmidt 
158453280323SBenjamin Herrenschmidt 	/* Hookup PHB Memory resources */
158553280323SBenjamin Herrenschmidt 	for (i = 0; i < 3; ++i) {
158653280323SBenjamin Herrenschmidt 		res = &hose->mem_resources[i];
158753280323SBenjamin Herrenschmidt 		if (!res->flags) {
158853280323SBenjamin Herrenschmidt 			if (i > 0)
158953280323SBenjamin Herrenschmidt 				continue;
159053280323SBenjamin Herrenschmidt 			printk(KERN_ERR "PCI: Memory resource 0 not set for "
159153280323SBenjamin Herrenschmidt 			       "host bridge %s (domain %d)\n",
159253280323SBenjamin Herrenschmidt 			       hose->dn->full_name, hose->global_number);
159353280323SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
159453280323SBenjamin Herrenschmidt 			/* Workaround for lack of MEM resource only on 32-bit */
159553280323SBenjamin Herrenschmidt 			res->start = hose->pci_mem_offset;
159653280323SBenjamin Herrenschmidt 			res->end = (resource_size_t)-1LL;
159753280323SBenjamin Herrenschmidt 			res->flags = IORESOURCE_MEM;
159853280323SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
159953280323SBenjamin Herrenschmidt 		}
160053280323SBenjamin Herrenschmidt 		bus->resource[i+1] = res;
160153280323SBenjamin Herrenschmidt 
160253280323SBenjamin Herrenschmidt 		pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n", i,
160353280323SBenjamin Herrenschmidt 			 (unsigned long long)res->start,
160453280323SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
160553280323SBenjamin Herrenschmidt 			 (unsigned long)res->flags);
160653280323SBenjamin Herrenschmidt 	}
160753280323SBenjamin Herrenschmidt 
160853280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB MEM offset     = %016llx\n",
160953280323SBenjamin Herrenschmidt 		 (unsigned long long)hose->pci_mem_offset);
161053280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB IO  offset     = %08lx\n",
161153280323SBenjamin Herrenschmidt 		 (unsigned long)hose->io_base_virt - _IO_BASE);
161253280323SBenjamin Herrenschmidt 
161353280323SBenjamin Herrenschmidt }
161489c2dd62SKumar Gala 
161589c2dd62SKumar Gala /*
161689c2dd62SKumar Gala  * Null PCI config access functions, for the case when we can't
161789c2dd62SKumar Gala  * find a hose.
161889c2dd62SKumar Gala  */
161989c2dd62SKumar Gala #define NULL_PCI_OP(rw, size, type)					\
162089c2dd62SKumar Gala static int								\
162189c2dd62SKumar Gala null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
162289c2dd62SKumar Gala {									\
162389c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
162489c2dd62SKumar Gala }
162589c2dd62SKumar Gala 
162689c2dd62SKumar Gala static int
162789c2dd62SKumar Gala null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
162889c2dd62SKumar Gala 		 int len, u32 *val)
162989c2dd62SKumar Gala {
163089c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
163189c2dd62SKumar Gala }
163289c2dd62SKumar Gala 
163389c2dd62SKumar Gala static int
163489c2dd62SKumar Gala null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
163589c2dd62SKumar Gala 		  int len, u32 val)
163689c2dd62SKumar Gala {
163789c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
163889c2dd62SKumar Gala }
163989c2dd62SKumar Gala 
164089c2dd62SKumar Gala static struct pci_ops null_pci_ops =
164189c2dd62SKumar Gala {
164289c2dd62SKumar Gala 	.read = null_read_config,
164389c2dd62SKumar Gala 	.write = null_write_config,
164489c2dd62SKumar Gala };
164589c2dd62SKumar Gala 
164689c2dd62SKumar Gala /*
164789c2dd62SKumar Gala  * These functions are used early on before PCI scanning is done
164889c2dd62SKumar Gala  * and all of the pci_dev and pci_bus structures have been created.
164989c2dd62SKumar Gala  */
165089c2dd62SKumar Gala static struct pci_bus *
165189c2dd62SKumar Gala fake_pci_bus(struct pci_controller *hose, int busnr)
165289c2dd62SKumar Gala {
165389c2dd62SKumar Gala 	static struct pci_bus bus;
165489c2dd62SKumar Gala 
165589c2dd62SKumar Gala 	if (hose == 0) {
165689c2dd62SKumar Gala 		printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
165789c2dd62SKumar Gala 	}
165889c2dd62SKumar Gala 	bus.number = busnr;
165989c2dd62SKumar Gala 	bus.sysdata = hose;
166089c2dd62SKumar Gala 	bus.ops = hose? hose->ops: &null_pci_ops;
166189c2dd62SKumar Gala 	return &bus;
166289c2dd62SKumar Gala }
166389c2dd62SKumar Gala 
166489c2dd62SKumar Gala #define EARLY_PCI_OP(rw, size, type)					\
166589c2dd62SKumar Gala int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
166689c2dd62SKumar Gala 			       int devfn, int offset, type value)	\
166789c2dd62SKumar Gala {									\
166889c2dd62SKumar Gala 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
166989c2dd62SKumar Gala 					    devfn, offset, value);	\
167089c2dd62SKumar Gala }
167189c2dd62SKumar Gala 
167289c2dd62SKumar Gala EARLY_PCI_OP(read, byte, u8 *)
167389c2dd62SKumar Gala EARLY_PCI_OP(read, word, u16 *)
167489c2dd62SKumar Gala EARLY_PCI_OP(read, dword, u32 *)
167589c2dd62SKumar Gala EARLY_PCI_OP(write, byte, u8)
167689c2dd62SKumar Gala EARLY_PCI_OP(write, word, u16)
167789c2dd62SKumar Gala EARLY_PCI_OP(write, dword, u32)
167889c2dd62SKumar Gala 
167989c2dd62SKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
168089c2dd62SKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
168189c2dd62SKumar Gala 			  int cap)
168289c2dd62SKumar Gala {
168389c2dd62SKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
168489c2dd62SKumar Gala }
16850ed2c722SGrant Likely 
168698d9f30cSBenjamin Herrenschmidt struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
168798d9f30cSBenjamin Herrenschmidt {
168898d9f30cSBenjamin Herrenschmidt 	struct pci_controller *hose = bus->sysdata;
168998d9f30cSBenjamin Herrenschmidt 
169098d9f30cSBenjamin Herrenschmidt 	return of_node_get(hose->dn);
169198d9f30cSBenjamin Herrenschmidt }
169298d9f30cSBenjamin Herrenschmidt 
16930ed2c722SGrant Likely /**
16940ed2c722SGrant Likely  * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
16950ed2c722SGrant Likely  * @hose: Pointer to the PCI host controller instance structure
16960ed2c722SGrant Likely  */
1697b5d937deSGrant Likely void __devinit pcibios_scan_phb(struct pci_controller *hose)
16980ed2c722SGrant Likely {
16990ed2c722SGrant Likely 	struct pci_bus *bus;
17000ed2c722SGrant Likely 	struct device_node *node = hose->dn;
17010ed2c722SGrant Likely 	int mode;
17020ed2c722SGrant Likely 
17030ed2c722SGrant Likely 	pr_debug("PCI: Scanning PHB %s\n",
17040ed2c722SGrant Likely 		 node ? node->full_name : "<NO NAME>");
17050ed2c722SGrant Likely 
17060ed2c722SGrant Likely 	/* Create an empty bus for the toplevel */
1707b5d937deSGrant Likely 	bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
17080ed2c722SGrant Likely 	if (bus == NULL) {
17090ed2c722SGrant Likely 		pr_err("Failed to create bus for PCI domain %04x\n",
17100ed2c722SGrant Likely 			hose->global_number);
17110ed2c722SGrant Likely 		return;
17120ed2c722SGrant Likely 	}
17130ed2c722SGrant Likely 	bus->secondary = hose->first_busno;
17140ed2c722SGrant Likely 	hose->bus = bus;
17150ed2c722SGrant Likely 
17160ed2c722SGrant Likely 	/* Get some IO space for the new PHB */
17170ed2c722SGrant Likely 	pcibios_setup_phb_io_space(hose);
17180ed2c722SGrant Likely 
17190ed2c722SGrant Likely 	/* Wire up PHB bus resources */
17200ed2c722SGrant Likely 	pcibios_setup_phb_resources(hose);
17210ed2c722SGrant Likely 
17220ed2c722SGrant Likely 	/* Get probe mode and perform scan */
17230ed2c722SGrant Likely 	mode = PCI_PROBE_NORMAL;
17240ed2c722SGrant Likely 	if (node && ppc_md.pci_probe_mode)
17250ed2c722SGrant Likely 		mode = ppc_md.pci_probe_mode(bus);
17260ed2c722SGrant Likely 	pr_debug("    probe mode: %d\n", mode);
17270ed2c722SGrant Likely 	if (mode == PCI_PROBE_DEVTREE) {
17280ed2c722SGrant Likely 		bus->subordinate = hose->last_busno;
17290ed2c722SGrant Likely 		of_scan_bus(node, bus);
17300ed2c722SGrant Likely 	}
17310ed2c722SGrant Likely 
17320ed2c722SGrant Likely 	if (mode == PCI_PROBE_NORMAL)
17330ed2c722SGrant Likely 		hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
1734781fb7a3SBenjamin Herrenschmidt 
1735781fb7a3SBenjamin Herrenschmidt 	/* Configure PCI Express settings */
1736bb36c445SBenjamin Herrenschmidt 	if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
1737781fb7a3SBenjamin Herrenschmidt 		struct pci_bus *child;
1738781fb7a3SBenjamin Herrenschmidt 		list_for_each_entry(child, &bus->children, node) {
1739781fb7a3SBenjamin Herrenschmidt 			struct pci_dev *self = child->self;
1740781fb7a3SBenjamin Herrenschmidt 			if (!self)
1741781fb7a3SBenjamin Herrenschmidt 				continue;
1742781fb7a3SBenjamin Herrenschmidt 			pcie_bus_configure_settings(child, self->pcie_mpss);
1743781fb7a3SBenjamin Herrenschmidt 		}
1744781fb7a3SBenjamin Herrenschmidt 	}
17450ed2c722SGrant Likely }
1746c065488fSKumar Gala 
1747c065488fSKumar Gala static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
1748c065488fSKumar Gala {
1749c065488fSKumar Gala 	int i, class = dev->class >> 8;
1750*05737c7cSJason Jin 	/* When configured as agent, programing interface = 1 */
1751*05737c7cSJason Jin 	int prog_if = dev->class & 0xf;
1752c065488fSKumar Gala 
1753c065488fSKumar Gala 	if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
1754c065488fSKumar Gala 	     class == PCI_CLASS_BRIDGE_OTHER) &&
1755c065488fSKumar Gala 		(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
1756*05737c7cSJason Jin 		(prog_if == 0) &&
1757c065488fSKumar Gala 		(dev->bus->parent == NULL)) {
1758c065488fSKumar Gala 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1759c065488fSKumar Gala 			dev->resource[i].start = 0;
1760c065488fSKumar Gala 			dev->resource[i].end = 0;
1761c065488fSKumar Gala 			dev->resource[i].flags = 0;
1762c065488fSKumar Gala 		}
1763c065488fSKumar Gala 	}
1764c065488fSKumar Gala }
1765c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1766c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1767