xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision cf1a4cf8754afb248e498815c7957aeb4faca79f)
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>
418b8da358SBenjamin Herrenschmidt #include <asm/eeh.h>
425516b540SKumar Gala 
43a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
44c3bd517dSMilton Miller LIST_HEAD(hose_list);
45a4c9e328SKumar Gala 
46a4c9e328SKumar Gala /* XXX kill that some day ... */
47ebfc00f7SStephen Rothwell static int global_phb_number;		/* Global phb counter */
48a4c9e328SKumar Gala 
4925e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */
5025e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base;
5125e81f92SBenjamin Herrenschmidt 
52a4c9e328SKumar Gala 
5345223c54SFUJITA Tomonori static struct dma_map_ops *pci_dma_ops = &dma_direct_ops;
544fc665b8SBecky Bruce 
5545223c54SFUJITA Tomonori void set_pci_dma_ops(struct dma_map_ops *dma_ops)
564fc665b8SBecky Bruce {
574fc665b8SBecky Bruce 	pci_dma_ops = dma_ops;
584fc665b8SBecky Bruce }
594fc665b8SBecky Bruce 
6045223c54SFUJITA Tomonori struct dma_map_ops *get_pci_dma_ops(void)
614fc665b8SBecky Bruce {
624fc665b8SBecky Bruce 	return pci_dma_ops;
634fc665b8SBecky Bruce }
644fc665b8SBecky Bruce EXPORT_SYMBOL(get_pci_dma_ops);
654fc665b8SBecky Bruce 
662d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
67a4c9e328SKumar Gala {
68a4c9e328SKumar Gala 	struct pci_controller *phb;
69a4c9e328SKumar Gala 
70e60516e3SStephen Rothwell 	phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
71a4c9e328SKumar Gala 	if (phb == NULL)
72a4c9e328SKumar Gala 		return NULL;
73e60516e3SStephen Rothwell 	spin_lock(&hose_spinlock);
74e60516e3SStephen Rothwell 	phb->global_number = global_phb_number++;
75e60516e3SStephen Rothwell 	list_add_tail(&phb->list_node, &hose_list);
76e60516e3SStephen Rothwell 	spin_unlock(&hose_spinlock);
7744ef3390SStephen Rothwell 	phb->dn = dev;
78a4c9e328SKumar Gala 	phb->is_dynamic = mem_init_done;
79a4c9e328SKumar Gala #ifdef CONFIG_PPC64
80a4c9e328SKumar Gala 	if (dev) {
81a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
82a4c9e328SKumar Gala 
83a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
84a4c9e328SKumar Gala 			nid = -1;
85a4c9e328SKumar Gala 
86a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
87a4c9e328SKumar Gala 	}
88a4c9e328SKumar Gala #endif
89a4c9e328SKumar Gala 	return phb;
90a4c9e328SKumar Gala }
91a4c9e328SKumar Gala 
92a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
93a4c9e328SKumar Gala {
94a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
95a4c9e328SKumar Gala 	list_del(&phb->list_node);
96a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
97a4c9e328SKumar Gala 
98a4c9e328SKumar Gala 	if (phb->is_dynamic)
99a4c9e328SKumar Gala 		kfree(phb);
100a4c9e328SKumar Gala }
101a4c9e328SKumar Gala 
102c3bd517dSMilton Miller static resource_size_t pcibios_io_size(const struct pci_controller *hose)
103c3bd517dSMilton Miller {
104c3bd517dSMilton Miller #ifdef CONFIG_PPC64
105c3bd517dSMilton Miller 	return hose->pci_io_size;
106c3bd517dSMilton Miller #else
10728f65c11SJoe Perches 	return resource_size(&hose->io_resource);
108c3bd517dSMilton Miller #endif
109c3bd517dSMilton Miller }
110c3bd517dSMilton Miller 
1116dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
1126dfbde20SBenjamin Herrenschmidt {
1136dfbde20SBenjamin Herrenschmidt 	int ret = 0;
1146dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
115c3bd517dSMilton Miller 	resource_size_t size;
1166dfbde20SBenjamin Herrenschmidt 
1176dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
1186dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
119c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
1206dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
1216dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
1226dfbde20SBenjamin Herrenschmidt 			ret = 1;
1236dfbde20SBenjamin Herrenschmidt 			break;
1246dfbde20SBenjamin Herrenschmidt 		}
1256dfbde20SBenjamin Herrenschmidt 	}
1266dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
1276dfbde20SBenjamin Herrenschmidt 	return ret;
1286dfbde20SBenjamin Herrenschmidt }
1296dfbde20SBenjamin Herrenschmidt 
130c3bd517dSMilton Miller unsigned long pci_address_to_pio(phys_addr_t address)
131c3bd517dSMilton Miller {
132c3bd517dSMilton Miller 	struct pci_controller *hose;
133c3bd517dSMilton Miller 	resource_size_t size;
134c3bd517dSMilton Miller 	unsigned long ret = ~0;
135c3bd517dSMilton Miller 
136c3bd517dSMilton Miller 	spin_lock(&hose_spinlock);
137c3bd517dSMilton Miller 	list_for_each_entry(hose, &hose_list, list_node) {
138c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
139c3bd517dSMilton Miller 		if (address >= hose->io_base_phys &&
140c3bd517dSMilton Miller 		    address < (hose->io_base_phys + size)) {
141c3bd517dSMilton Miller 			unsigned long base =
142c3bd517dSMilton Miller 				(unsigned long)hose->io_base_virt - _IO_BASE;
143c3bd517dSMilton Miller 			ret = base + (address - hose->io_base_phys);
144c3bd517dSMilton Miller 			break;
145c3bd517dSMilton Miller 		}
146c3bd517dSMilton Miller 	}
147c3bd517dSMilton Miller 	spin_unlock(&hose_spinlock);
148c3bd517dSMilton Miller 
149c3bd517dSMilton Miller 	return ret;
150c3bd517dSMilton Miller }
151c3bd517dSMilton Miller EXPORT_SYMBOL_GPL(pci_address_to_pio);
152c3bd517dSMilton Miller 
1535516b540SKumar Gala /*
1545516b540SKumar Gala  * Return the domain number for this bus.
1555516b540SKumar Gala  */
1565516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
1575516b540SKumar Gala {
1585516b540SKumar Gala 	struct pci_controller *hose = pci_bus_to_host(bus);
1595516b540SKumar Gala 
1605516b540SKumar Gala 	return hose->global_number;
1615516b540SKumar Gala }
1625516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
16358083dadSKumar Gala 
164a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
165a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
166a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
167a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
168a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
169a4c9e328SKumar Gala  * config cycles.
170a4c9e328SKumar Gala  */
171a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
172a4c9e328SKumar Gala {
173a4c9e328SKumar Gala 	while(node) {
174a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
175a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
17644ef3390SStephen Rothwell 			if (hose->dn == node)
177a4c9e328SKumar Gala 				return hose;
178a4c9e328SKumar Gala 		node = node->parent;
179a4c9e328SKumar Gala 	}
180a4c9e328SKumar Gala 	return NULL;
181a4c9e328SKumar Gala }
182a4c9e328SKumar Gala 
18358083dadSKumar Gala static ssize_t pci_show_devspec(struct device *dev,
18458083dadSKumar Gala 		struct device_attribute *attr, char *buf)
18558083dadSKumar Gala {
18658083dadSKumar Gala 	struct pci_dev *pdev;
18758083dadSKumar Gala 	struct device_node *np;
18858083dadSKumar Gala 
18958083dadSKumar Gala 	pdev = to_pci_dev (dev);
19058083dadSKumar Gala 	np = pci_device_to_OF_node(pdev);
19158083dadSKumar Gala 	if (np == NULL || np->full_name == NULL)
19258083dadSKumar Gala 		return 0;
19358083dadSKumar Gala 	return sprintf(buf, "%s", np->full_name);
19458083dadSKumar Gala }
19558083dadSKumar Gala static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
19658083dadSKumar Gala 
19758083dadSKumar Gala /* Add sysfs properties */
1984f3731daSTony Breeds int pcibios_add_platform_entries(struct pci_dev *pdev)
19958083dadSKumar Gala {
2004f3731daSTony Breeds 	return device_create_file(&pdev->dev, &dev_attr_devspec);
20158083dadSKumar Gala }
20258083dadSKumar Gala 
20358083dadSKumar Gala /*
20458083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
20558083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
20658083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
20758083dadSKumar Gala  */
2084666ca2aSBenjamin Herrenschmidt static int pci_read_irq_line(struct pci_dev *pci_dev)
20958083dadSKumar Gala {
21058083dadSKumar Gala 	struct of_irq oirq;
21158083dadSKumar Gala 	unsigned int virq;
21258083dadSKumar Gala 
213b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
21458083dadSKumar Gala 
21558083dadSKumar Gala #ifdef DEBUG
21658083dadSKumar Gala 	memset(&oirq, 0xff, sizeof(oirq));
21758083dadSKumar Gala #endif
21858083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
21958083dadSKumar Gala 	if (of_irq_map_pci(pci_dev, &oirq)) {
22058083dadSKumar Gala 		u8 line, pin;
22158083dadSKumar Gala 
22258083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
22358083dadSKumar Gala 		 * space and map that through the default controller. We
22458083dadSKumar Gala 		 * also set the type to level low since that's what PCI
22558083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
22658083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
22758083dadSKumar Gala 		 * function.
22858083dadSKumar Gala 		 */
22958083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
23058083dadSKumar Gala 			return -1;
23158083dadSKumar Gala 		if (pin == 0)
23258083dadSKumar Gala 			return -1;
23358083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
23454a24cbbSBenjamin Herrenschmidt 		    line == 0xff || line == 0) {
23558083dadSKumar Gala 			return -1;
23658083dadSKumar Gala 		}
237b0494bc8SBenjamin Herrenschmidt 		pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
23854a24cbbSBenjamin Herrenschmidt 			 line, pin);
23958083dadSKumar Gala 
24058083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
24158083dadSKumar Gala 		if (virq != NO_IRQ)
242ec775d0eSThomas Gleixner 			irq_set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
24358083dadSKumar Gala 	} else {
244b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
24558083dadSKumar Gala 			 oirq.size, oirq.specifier[0], oirq.specifier[1],
24674a7f084SGrant Likely 			 of_node_full_name(oirq.controller));
24758083dadSKumar Gala 
24858083dadSKumar Gala 		virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
24958083dadSKumar Gala 					     oirq.size);
25058083dadSKumar Gala 	}
25158083dadSKumar Gala 	if(virq == NO_IRQ) {
252b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Failed to map !\n");
25358083dadSKumar Gala 		return -1;
25458083dadSKumar Gala 	}
25558083dadSKumar Gala 
256b0494bc8SBenjamin Herrenschmidt 	pr_debug(" Mapped to linux irq %d\n", virq);
25758083dadSKumar Gala 
25858083dadSKumar Gala 	pci_dev->irq = virq;
25958083dadSKumar Gala 
26058083dadSKumar Gala 	return 0;
26158083dadSKumar Gala }
26258083dadSKumar Gala 
26358083dadSKumar Gala /*
26458083dadSKumar Gala  * Platform support for /proc/bus/pci/X/Y mmap()s,
26558083dadSKumar Gala  * modelled on the sparc64 implementation by Dave Miller.
26658083dadSKumar Gala  *  -- paulus.
26758083dadSKumar Gala  */
26858083dadSKumar Gala 
26958083dadSKumar Gala /*
27058083dadSKumar Gala  * Adjust vm_pgoff of VMA such that it is the physical page offset
27158083dadSKumar Gala  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
27258083dadSKumar Gala  *
27358083dadSKumar Gala  * Basically, the user finds the base address for his device which he wishes
27458083dadSKumar Gala  * to mmap.  They read the 32-bit value from the config space base register,
27558083dadSKumar Gala  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
27658083dadSKumar Gala  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
27758083dadSKumar Gala  *
27858083dadSKumar Gala  * Returns negative error code on failure, zero on success.
27958083dadSKumar Gala  */
28058083dadSKumar Gala static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
28158083dadSKumar Gala 					       resource_size_t *offset,
28258083dadSKumar Gala 					       enum pci_mmap_state mmap_state)
28358083dadSKumar Gala {
28458083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
28558083dadSKumar Gala 	unsigned long io_offset = 0;
28658083dadSKumar Gala 	int i, res_bit;
28758083dadSKumar Gala 
28858083dadSKumar Gala 	if (hose == 0)
28958083dadSKumar Gala 		return NULL;		/* should never happen */
29058083dadSKumar Gala 
29158083dadSKumar Gala 	/* If memory, add on the PCI bridge address offset */
29258083dadSKumar Gala 	if (mmap_state == pci_mmap_mem) {
29358083dadSKumar Gala #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
29458083dadSKumar Gala 		*offset += hose->pci_mem_offset;
29558083dadSKumar Gala #endif
29658083dadSKumar Gala 		res_bit = IORESOURCE_MEM;
29758083dadSKumar Gala 	} else {
29858083dadSKumar Gala 		io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
29958083dadSKumar Gala 		*offset += io_offset;
30058083dadSKumar Gala 		res_bit = IORESOURCE_IO;
30158083dadSKumar Gala 	}
30258083dadSKumar Gala 
30358083dadSKumar Gala 	/*
30458083dadSKumar Gala 	 * Check that the offset requested corresponds to one of the
30558083dadSKumar Gala 	 * resources of the device.
30658083dadSKumar Gala 	 */
30758083dadSKumar Gala 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
30858083dadSKumar Gala 		struct resource *rp = &dev->resource[i];
30958083dadSKumar Gala 		int flags = rp->flags;
31058083dadSKumar Gala 
31158083dadSKumar Gala 		/* treat ROM as memory (should be already) */
31258083dadSKumar Gala 		if (i == PCI_ROM_RESOURCE)
31358083dadSKumar Gala 			flags |= IORESOURCE_MEM;
31458083dadSKumar Gala 
31558083dadSKumar Gala 		/* Active and same type? */
31658083dadSKumar Gala 		if ((flags & res_bit) == 0)
31758083dadSKumar Gala 			continue;
31858083dadSKumar Gala 
31958083dadSKumar Gala 		/* In the range of this resource? */
32058083dadSKumar Gala 		if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
32158083dadSKumar Gala 			continue;
32258083dadSKumar Gala 
32358083dadSKumar Gala 		/* found it! construct the final physical address */
32458083dadSKumar Gala 		if (mmap_state == pci_mmap_io)
32558083dadSKumar Gala 			*offset += hose->io_base_phys - io_offset;
32658083dadSKumar Gala 		return rp;
32758083dadSKumar Gala 	}
32858083dadSKumar Gala 
32958083dadSKumar Gala 	return NULL;
33058083dadSKumar Gala }
33158083dadSKumar Gala 
33258083dadSKumar Gala /*
33358083dadSKumar Gala  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
33458083dadSKumar Gala  * device mapping.
33558083dadSKumar Gala  */
33658083dadSKumar Gala static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
33758083dadSKumar Gala 				      pgprot_t protection,
33858083dadSKumar Gala 				      enum pci_mmap_state mmap_state,
33958083dadSKumar Gala 				      int write_combine)
34058083dadSKumar Gala {
34158083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
34258083dadSKumar Gala 
34358083dadSKumar Gala 	/* Write combine is always 0 on non-memory space mappings. On
34458083dadSKumar Gala 	 * memory space, if the user didn't pass 1, we check for a
34558083dadSKumar Gala 	 * "prefetchable" resource. This is a bit hackish, but we use
34658083dadSKumar Gala 	 * this to workaround the inability of /sysfs to provide a write
34758083dadSKumar Gala 	 * combine bit
34858083dadSKumar Gala 	 */
34958083dadSKumar Gala 	if (mmap_state != pci_mmap_mem)
35058083dadSKumar Gala 		write_combine = 0;
35158083dadSKumar Gala 	else if (write_combine == 0) {
35258083dadSKumar Gala 		if (rp->flags & IORESOURCE_PREFETCH)
35358083dadSKumar Gala 			write_combine = 1;
35458083dadSKumar Gala 	}
35558083dadSKumar Gala 
35658083dadSKumar Gala 	/* XXX would be nice to have a way to ask for write-through */
35758083dadSKumar Gala 	if (write_combine)
35864b3d0e8SBenjamin Herrenschmidt 		return pgprot_noncached_wc(prot);
35958083dadSKumar Gala 	else
36064b3d0e8SBenjamin Herrenschmidt 		return pgprot_noncached(prot);
36158083dadSKumar Gala }
36258083dadSKumar Gala 
36358083dadSKumar Gala /*
36458083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
36558083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
36658083dadSKumar Gala  * above routine
36758083dadSKumar Gala  */
36858083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
36958083dadSKumar Gala 				  unsigned long pfn,
37058083dadSKumar Gala 				  unsigned long size,
37164b3d0e8SBenjamin Herrenschmidt 				  pgprot_t prot)
37258083dadSKumar Gala {
37358083dadSKumar Gala 	struct pci_dev *pdev = NULL;
37458083dadSKumar Gala 	struct resource *found = NULL;
3757c12d906SBenjamin Herrenschmidt 	resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
37658083dadSKumar Gala 	int i;
37758083dadSKumar Gala 
37858083dadSKumar Gala 	if (page_is_ram(pfn))
37964b3d0e8SBenjamin Herrenschmidt 		return prot;
38058083dadSKumar Gala 
38164b3d0e8SBenjamin Herrenschmidt 	prot = pgprot_noncached(prot);
38258083dadSKumar Gala 	for_each_pci_dev(pdev) {
38358083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
38458083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
38558083dadSKumar Gala 			int flags = rp->flags;
38658083dadSKumar Gala 
38758083dadSKumar Gala 			/* Active and same type? */
38858083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
38958083dadSKumar Gala 				continue;
39058083dadSKumar Gala 			/* In the range of this resource? */
39158083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
39258083dadSKumar Gala 			    offset > rp->end)
39358083dadSKumar Gala 				continue;
39458083dadSKumar Gala 			found = rp;
39558083dadSKumar Gala 			break;
39658083dadSKumar Gala 		}
39758083dadSKumar Gala 		if (found)
39858083dadSKumar Gala 			break;
39958083dadSKumar Gala 	}
40058083dadSKumar Gala 	if (found) {
40158083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
40264b3d0e8SBenjamin Herrenschmidt 			prot = pgprot_noncached_wc(prot);
40358083dadSKumar Gala 		pci_dev_put(pdev);
40458083dadSKumar Gala 	}
40558083dadSKumar Gala 
406b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
40764b3d0e8SBenjamin Herrenschmidt 		 (unsigned long long)offset, pgprot_val(prot));
40858083dadSKumar Gala 
40964b3d0e8SBenjamin Herrenschmidt 	return prot;
41058083dadSKumar Gala }
41158083dadSKumar Gala 
41258083dadSKumar Gala 
41358083dadSKumar Gala /*
41458083dadSKumar Gala  * Perform the actual remap of the pages for a PCI device mapping, as
41558083dadSKumar Gala  * appropriate for this architecture.  The region in the process to map
41658083dadSKumar Gala  * is described by vm_start and vm_end members of VMA, the base physical
41758083dadSKumar Gala  * address is found in vm_pgoff.
41858083dadSKumar Gala  * The pci device structure is provided so that architectures may make mapping
41958083dadSKumar Gala  * decisions on a per-device or per-bus basis.
42058083dadSKumar Gala  *
42158083dadSKumar Gala  * Returns a negative error code on failure, zero on success.
42258083dadSKumar Gala  */
42358083dadSKumar Gala int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
42458083dadSKumar Gala 			enum pci_mmap_state mmap_state, int write_combine)
42558083dadSKumar Gala {
4267c12d906SBenjamin Herrenschmidt 	resource_size_t offset =
4277c12d906SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
42858083dadSKumar Gala 	struct resource *rp;
42958083dadSKumar Gala 	int ret;
43058083dadSKumar Gala 
43158083dadSKumar Gala 	rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
43258083dadSKumar Gala 	if (rp == NULL)
43358083dadSKumar Gala 		return -EINVAL;
43458083dadSKumar Gala 
43558083dadSKumar Gala 	vma->vm_pgoff = offset >> PAGE_SHIFT;
43658083dadSKumar Gala 	vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
43758083dadSKumar Gala 						  vma->vm_page_prot,
43858083dadSKumar Gala 						  mmap_state, write_combine);
43958083dadSKumar Gala 
44058083dadSKumar Gala 	ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
44158083dadSKumar Gala 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
44258083dadSKumar Gala 
44358083dadSKumar Gala 	return ret;
44458083dadSKumar Gala }
44558083dadSKumar Gala 
446e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO read access on a bus */
447e9f82cb7SBenjamin Herrenschmidt int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
448e9f82cb7SBenjamin Herrenschmidt {
449e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
450e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
451e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
452e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
453e9f82cb7SBenjamin Herrenschmidt 
454e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
455e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
456e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
457e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
458e9f82cb7SBenjamin Herrenschmidt 	 */
459e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
460e9f82cb7SBenjamin Herrenschmidt 	offset += port;
461e9f82cb7SBenjamin Herrenschmidt 
462e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
463e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
464e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
465e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
466e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
467e9f82cb7SBenjamin Herrenschmidt 
468e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
469e9f82cb7SBenjamin Herrenschmidt 	case 1:
470e9f82cb7SBenjamin Herrenschmidt 		*((u8 *)val) = in_8(addr);
471e9f82cb7SBenjamin Herrenschmidt 		return 1;
472e9f82cb7SBenjamin Herrenschmidt 	case 2:
473e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
474e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
475e9f82cb7SBenjamin Herrenschmidt 		*((u16 *)val) = in_le16(addr);
476e9f82cb7SBenjamin Herrenschmidt 		return 2;
477e9f82cb7SBenjamin Herrenschmidt 	case 4:
478e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
479e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
480e9f82cb7SBenjamin Herrenschmidt 		*((u32 *)val) = in_le32(addr);
481e9f82cb7SBenjamin Herrenschmidt 		return 4;
482e9f82cb7SBenjamin Herrenschmidt 	}
483e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
484e9f82cb7SBenjamin Herrenschmidt }
485e9f82cb7SBenjamin Herrenschmidt 
486e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO write access on a bus */
487e9f82cb7SBenjamin Herrenschmidt int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
488e9f82cb7SBenjamin Herrenschmidt {
489e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
490e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
491e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
492e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
493e9f82cb7SBenjamin Herrenschmidt 
494e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
495e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
496e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
497e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
498e9f82cb7SBenjamin Herrenschmidt 	 */
499e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
500e9f82cb7SBenjamin Herrenschmidt 	offset += port;
501e9f82cb7SBenjamin Herrenschmidt 
502e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
503e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
504e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
505e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
506e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
507e9f82cb7SBenjamin Herrenschmidt 
508e9f82cb7SBenjamin Herrenschmidt 	/* WARNING: The generic code is idiotic. It gets passed a pointer
509e9f82cb7SBenjamin Herrenschmidt 	 * to what can be a 1, 2 or 4 byte quantity and always reads that
510e9f82cb7SBenjamin Herrenschmidt 	 * as a u32, which means that we have to correct the location of
511e9f82cb7SBenjamin Herrenschmidt 	 * the data read within those 32 bits for size 1 and 2
512e9f82cb7SBenjamin Herrenschmidt 	 */
513e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
514e9f82cb7SBenjamin Herrenschmidt 	case 1:
515e9f82cb7SBenjamin Herrenschmidt 		out_8(addr, val >> 24);
516e9f82cb7SBenjamin Herrenschmidt 		return 1;
517e9f82cb7SBenjamin Herrenschmidt 	case 2:
518e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
519e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
520e9f82cb7SBenjamin Herrenschmidt 		out_le16(addr, val >> 16);
521e9f82cb7SBenjamin Herrenschmidt 		return 2;
522e9f82cb7SBenjamin Herrenschmidt 	case 4:
523e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
524e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
525e9f82cb7SBenjamin Herrenschmidt 		out_le32(addr, val);
526e9f82cb7SBenjamin Herrenschmidt 		return 4;
527e9f82cb7SBenjamin Herrenschmidt 	}
528e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
529e9f82cb7SBenjamin Herrenschmidt }
530e9f82cb7SBenjamin Herrenschmidt 
531e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO or memory mmap access on a bus */
532e9f82cb7SBenjamin Herrenschmidt int pci_mmap_legacy_page_range(struct pci_bus *bus,
533e9f82cb7SBenjamin Herrenschmidt 			       struct vm_area_struct *vma,
534e9f82cb7SBenjamin Herrenschmidt 			       enum pci_mmap_state mmap_state)
535e9f82cb7SBenjamin Herrenschmidt {
536e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
537e9f82cb7SBenjamin Herrenschmidt 	resource_size_t offset =
538e9f82cb7SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
539e9f82cb7SBenjamin Herrenschmidt 	resource_size_t size = vma->vm_end - vma->vm_start;
540e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp;
541e9f82cb7SBenjamin Herrenschmidt 
542e9f82cb7SBenjamin Herrenschmidt 	pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
543e9f82cb7SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number,
544e9f82cb7SBenjamin Herrenschmidt 		 mmap_state == pci_mmap_mem ? "MEM" : "IO",
545e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)offset,
546e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)(offset + size - 1));
547e9f82cb7SBenjamin Herrenschmidt 
548e9f82cb7SBenjamin Herrenschmidt 	if (mmap_state == pci_mmap_mem) {
5495b11abfdSBenjamin Herrenschmidt 		/* Hack alert !
5505b11abfdSBenjamin Herrenschmidt 		 *
5515b11abfdSBenjamin Herrenschmidt 		 * Because X is lame and can fail starting if it gets an error trying
5525b11abfdSBenjamin Herrenschmidt 		 * to mmap legacy_mem (instead of just moving on without legacy memory
5535b11abfdSBenjamin Herrenschmidt 		 * access) we fake it here by giving it anonymous memory, effectively
5545b11abfdSBenjamin Herrenschmidt 		 * behaving just like /dev/zero
5555b11abfdSBenjamin Herrenschmidt 		 */
5565b11abfdSBenjamin Herrenschmidt 		if ((offset + size) > hose->isa_mem_size) {
5575b11abfdSBenjamin Herrenschmidt 			printk(KERN_DEBUG
5585b11abfdSBenjamin Herrenschmidt 			       "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
5595b11abfdSBenjamin Herrenschmidt 			       current->comm, current->pid, pci_domain_nr(bus), bus->number);
5605b11abfdSBenjamin Herrenschmidt 			if (vma->vm_flags & VM_SHARED)
5615b11abfdSBenjamin Herrenschmidt 				return shmem_zero_setup(vma);
5625b11abfdSBenjamin Herrenschmidt 			return 0;
5635b11abfdSBenjamin Herrenschmidt 		}
564e9f82cb7SBenjamin Herrenschmidt 		offset += hose->isa_mem_phys;
565e9f82cb7SBenjamin Herrenschmidt 	} else {
566e9f82cb7SBenjamin Herrenschmidt 		unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
567e9f82cb7SBenjamin Herrenschmidt 		unsigned long roffset = offset + io_offset;
568e9f82cb7SBenjamin Herrenschmidt 		rp = &hose->io_resource;
569e9f82cb7SBenjamin Herrenschmidt 		if (!(rp->flags & IORESOURCE_IO))
570e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
571e9f82cb7SBenjamin Herrenschmidt 		if (roffset < rp->start || (roffset + size) > rp->end)
572e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
573e9f82cb7SBenjamin Herrenschmidt 		offset += hose->io_base_phys;
574e9f82cb7SBenjamin Herrenschmidt 	}
575e9f82cb7SBenjamin Herrenschmidt 	pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
576e9f82cb7SBenjamin Herrenschmidt 
577e9f82cb7SBenjamin Herrenschmidt 	vma->vm_pgoff = offset >> PAGE_SHIFT;
57864b3d0e8SBenjamin Herrenschmidt 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
579e9f82cb7SBenjamin Herrenschmidt 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
580e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_end - vma->vm_start,
581e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_page_prot);
582e9f82cb7SBenjamin Herrenschmidt }
583e9f82cb7SBenjamin Herrenschmidt 
58458083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
58558083dadSKumar Gala 			  const struct resource *rsrc,
58658083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
58758083dadSKumar Gala {
58858083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
58958083dadSKumar Gala 	resource_size_t offset = 0;
59058083dadSKumar Gala 
59158083dadSKumar Gala 	if (hose == NULL)
59258083dadSKumar Gala 		return;
59358083dadSKumar Gala 
59458083dadSKumar Gala 	if (rsrc->flags & IORESOURCE_IO)
59558083dadSKumar Gala 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
59658083dadSKumar Gala 
59758083dadSKumar Gala 	/* We pass a fully fixed up address to userland for MMIO instead of
59858083dadSKumar Gala 	 * a BAR value because X is lame and expects to be able to use that
59958083dadSKumar Gala 	 * to pass to /dev/mem !
60058083dadSKumar Gala 	 *
60158083dadSKumar Gala 	 * That means that we'll have potentially 64 bits values where some
60258083dadSKumar Gala 	 * userland apps only expect 32 (like X itself since it thinks only
60358083dadSKumar Gala 	 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
60458083dadSKumar Gala 	 * 32 bits CHRPs :-(
60558083dadSKumar Gala 	 *
60658083dadSKumar Gala 	 * Hopefully, the sysfs insterface is immune to that gunk. Once X
60758083dadSKumar Gala 	 * has been fixed (and the fix spread enough), we can re-enable the
60858083dadSKumar Gala 	 * 2 lines below and pass down a BAR value to userland. In that case
60958083dadSKumar Gala 	 * we'll also have to re-enable the matching code in
61058083dadSKumar Gala 	 * __pci_mmap_make_offset().
61158083dadSKumar Gala 	 *
61258083dadSKumar Gala 	 * BenH.
61358083dadSKumar Gala 	 */
61458083dadSKumar Gala #if 0
61558083dadSKumar Gala 	else if (rsrc->flags & IORESOURCE_MEM)
61658083dadSKumar Gala 		offset = hose->pci_mem_offset;
61758083dadSKumar Gala #endif
61858083dadSKumar Gala 
61958083dadSKumar Gala 	*start = rsrc->start - offset;
62058083dadSKumar Gala 	*end = rsrc->end - offset;
62158083dadSKumar Gala }
62213dccb9eSBenjamin Herrenschmidt 
62313dccb9eSBenjamin Herrenschmidt /**
62413dccb9eSBenjamin Herrenschmidt  * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
62513dccb9eSBenjamin Herrenschmidt  * @hose: newly allocated pci_controller to be setup
62613dccb9eSBenjamin Herrenschmidt  * @dev: device node of the host bridge
62713dccb9eSBenjamin Herrenschmidt  * @primary: set if primary bus (32 bits only, soon to be deprecated)
62813dccb9eSBenjamin Herrenschmidt  *
62913dccb9eSBenjamin Herrenschmidt  * This function will parse the "ranges" property of a PCI host bridge device
63013dccb9eSBenjamin Herrenschmidt  * node and setup the resource mapping of a pci controller based on its
63113dccb9eSBenjamin Herrenschmidt  * content.
63213dccb9eSBenjamin Herrenschmidt  *
63313dccb9eSBenjamin Herrenschmidt  * Life would be boring if it wasn't for a few issues that we have to deal
63413dccb9eSBenjamin Herrenschmidt  * with here:
63513dccb9eSBenjamin Herrenschmidt  *
63613dccb9eSBenjamin Herrenschmidt  *   - We can only cope with one IO space range and up to 3 Memory space
63713dccb9eSBenjamin Herrenschmidt  *     ranges. However, some machines (thanks Apple !) tend to split their
63813dccb9eSBenjamin Herrenschmidt  *     space into lots of small contiguous ranges. So we have to coalesce.
63913dccb9eSBenjamin Herrenschmidt  *
64013dccb9eSBenjamin Herrenschmidt  *   - We can only cope with all memory ranges having the same offset
64113dccb9eSBenjamin Herrenschmidt  *     between CPU addresses and PCI addresses. Unfortunately, some bridges
64213dccb9eSBenjamin Herrenschmidt  *     are setup for a large 1:1 mapping along with a small "window" which
64313dccb9eSBenjamin Herrenschmidt  *     maps PCI address 0 to some arbitrary high address of the CPU space in
64413dccb9eSBenjamin Herrenschmidt  *     order to give access to the ISA memory hole.
64513dccb9eSBenjamin Herrenschmidt  *     The way out of here that I've chosen for now is to always set the
64613dccb9eSBenjamin Herrenschmidt  *     offset based on the first resource found, then override it if we
64713dccb9eSBenjamin Herrenschmidt  *     have a different offset and the previous was set by an ISA hole.
64813dccb9eSBenjamin Herrenschmidt  *
64913dccb9eSBenjamin Herrenschmidt  *   - Some busses have IO space not starting at 0, which causes trouble with
65013dccb9eSBenjamin Herrenschmidt  *     the way we do our IO resource renumbering. The code somewhat deals with
65113dccb9eSBenjamin Herrenschmidt  *     it for 64 bits but I would expect problems on 32 bits.
65213dccb9eSBenjamin Herrenschmidt  *
65313dccb9eSBenjamin Herrenschmidt  *   - Some 32 bits platforms such as 4xx can have physical space larger than
65413dccb9eSBenjamin Herrenschmidt  *     32 bits so we need to use 64 bits values for the parsing
65513dccb9eSBenjamin Herrenschmidt  */
65613dccb9eSBenjamin Herrenschmidt void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
65713dccb9eSBenjamin Herrenschmidt 					    struct device_node *dev,
65813dccb9eSBenjamin Herrenschmidt 					    int primary)
65913dccb9eSBenjamin Herrenschmidt {
66013dccb9eSBenjamin Herrenschmidt 	const u32 *ranges;
66113dccb9eSBenjamin Herrenschmidt 	int rlen;
66213dccb9eSBenjamin Herrenschmidt 	int pna = of_n_addr_cells(dev);
66313dccb9eSBenjamin Herrenschmidt 	int np = pna + 5;
66413dccb9eSBenjamin Herrenschmidt 	int memno = 0, isa_hole = -1;
66513dccb9eSBenjamin Herrenschmidt 	u32 pci_space;
66613dccb9eSBenjamin Herrenschmidt 	unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
66713dccb9eSBenjamin Herrenschmidt 	unsigned long long isa_mb = 0;
66813dccb9eSBenjamin Herrenschmidt 	struct resource *res;
66913dccb9eSBenjamin Herrenschmidt 
67013dccb9eSBenjamin Herrenschmidt 	printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
67113dccb9eSBenjamin Herrenschmidt 	       dev->full_name, primary ? "(primary)" : "");
67213dccb9eSBenjamin Herrenschmidt 
67313dccb9eSBenjamin Herrenschmidt 	/* Get ranges property */
67413dccb9eSBenjamin Herrenschmidt 	ranges = of_get_property(dev, "ranges", &rlen);
67513dccb9eSBenjamin Herrenschmidt 	if (ranges == NULL)
67613dccb9eSBenjamin Herrenschmidt 		return;
67713dccb9eSBenjamin Herrenschmidt 
67813dccb9eSBenjamin Herrenschmidt 	/* Parse it */
67913dccb9eSBenjamin Herrenschmidt 	while ((rlen -= np * 4) >= 0) {
68013dccb9eSBenjamin Herrenschmidt 		/* Read next ranges element */
68113dccb9eSBenjamin Herrenschmidt 		pci_space = ranges[0];
68213dccb9eSBenjamin Herrenschmidt 		pci_addr = of_read_number(ranges + 1, 2);
68313dccb9eSBenjamin Herrenschmidt 		cpu_addr = of_translate_address(dev, ranges + 3);
68413dccb9eSBenjamin Herrenschmidt 		size = of_read_number(ranges + pna + 3, 2);
68513dccb9eSBenjamin Herrenschmidt 		ranges += np;
686e9f82cb7SBenjamin Herrenschmidt 
687e9f82cb7SBenjamin Herrenschmidt 		/* If we failed translation or got a zero-sized region
688e9f82cb7SBenjamin Herrenschmidt 		 * (some FW try to feed us with non sensical zero sized regions
689e9f82cb7SBenjamin Herrenschmidt 		 * such as power3 which look like some kind of attempt at exposing
690e9f82cb7SBenjamin Herrenschmidt 		 * the VGA memory hole)
691e9f82cb7SBenjamin Herrenschmidt 		 */
69213dccb9eSBenjamin Herrenschmidt 		if (cpu_addr == OF_BAD_ADDR || size == 0)
69313dccb9eSBenjamin Herrenschmidt 			continue;
69413dccb9eSBenjamin Herrenschmidt 
69513dccb9eSBenjamin Herrenschmidt 		/* Now consume following elements while they are contiguous */
69613dccb9eSBenjamin Herrenschmidt 		for (; rlen >= np * sizeof(u32);
69713dccb9eSBenjamin Herrenschmidt 		     ranges += np, rlen -= np * 4) {
69813dccb9eSBenjamin Herrenschmidt 			if (ranges[0] != pci_space)
69913dccb9eSBenjamin Herrenschmidt 				break;
70013dccb9eSBenjamin Herrenschmidt 			pci_next = of_read_number(ranges + 1, 2);
70113dccb9eSBenjamin Herrenschmidt 			cpu_next = of_translate_address(dev, ranges + 3);
70213dccb9eSBenjamin Herrenschmidt 			if (pci_next != pci_addr + size ||
70313dccb9eSBenjamin Herrenschmidt 			    cpu_next != cpu_addr + size)
70413dccb9eSBenjamin Herrenschmidt 				break;
70513dccb9eSBenjamin Herrenschmidt 			size += of_read_number(ranges + pna + 3, 2);
70613dccb9eSBenjamin Herrenschmidt 		}
70713dccb9eSBenjamin Herrenschmidt 
70813dccb9eSBenjamin Herrenschmidt 		/* Act based on address space type */
70913dccb9eSBenjamin Herrenschmidt 		res = NULL;
71013dccb9eSBenjamin Herrenschmidt 		switch ((pci_space >> 24) & 0x3) {
71113dccb9eSBenjamin Herrenschmidt 		case 1:		/* PCI IO space */
71213dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
71313dccb9eSBenjamin Herrenschmidt 			       "  IO 0x%016llx..0x%016llx -> 0x%016llx\n",
71413dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr);
71513dccb9eSBenjamin Herrenschmidt 
71613dccb9eSBenjamin Herrenschmidt 			/* We support only one IO range */
71713dccb9eSBenjamin Herrenschmidt 			if (hose->pci_io_size) {
71813dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
71913dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
72013dccb9eSBenjamin Herrenschmidt 				continue;
72113dccb9eSBenjamin Herrenschmidt 			}
72213dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32
72313dccb9eSBenjamin Herrenschmidt 			/* On 32 bits, limit I/O space to 16MB */
72413dccb9eSBenjamin Herrenschmidt 			if (size > 0x01000000)
72513dccb9eSBenjamin Herrenschmidt 				size = 0x01000000;
72613dccb9eSBenjamin Herrenschmidt 
72713dccb9eSBenjamin Herrenschmidt 			/* 32 bits needs to map IOs here */
72813dccb9eSBenjamin Herrenschmidt 			hose->io_base_virt = ioremap(cpu_addr, size);
72913dccb9eSBenjamin Herrenschmidt 
73013dccb9eSBenjamin Herrenschmidt 			/* Expect trouble if pci_addr is not 0 */
73113dccb9eSBenjamin Herrenschmidt 			if (primary)
73213dccb9eSBenjamin Herrenschmidt 				isa_io_base =
73313dccb9eSBenjamin Herrenschmidt 					(unsigned long)hose->io_base_virt;
73413dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
73513dccb9eSBenjamin Herrenschmidt 			/* pci_io_size and io_base_phys always represent IO
73613dccb9eSBenjamin Herrenschmidt 			 * space starting at 0 so we factor in pci_addr
73713dccb9eSBenjamin Herrenschmidt 			 */
73813dccb9eSBenjamin Herrenschmidt 			hose->pci_io_size = pci_addr + size;
73913dccb9eSBenjamin Herrenschmidt 			hose->io_base_phys = cpu_addr - pci_addr;
74013dccb9eSBenjamin Herrenschmidt 
74113dccb9eSBenjamin Herrenschmidt 			/* Build resource */
74213dccb9eSBenjamin Herrenschmidt 			res = &hose->io_resource;
74313dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_IO;
74413dccb9eSBenjamin Herrenschmidt 			res->start = pci_addr;
74513dccb9eSBenjamin Herrenschmidt 			break;
74613dccb9eSBenjamin Herrenschmidt 		case 2:		/* PCI Memory space */
74767260ac9SBenjamin Herrenschmidt 		case 3:		/* PCI 64 bits Memory space */
74813dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
74913dccb9eSBenjamin Herrenschmidt 			       " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
75013dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr,
75113dccb9eSBenjamin Herrenschmidt 			       (pci_space & 0x40000000) ? "Prefetch" : "");
75213dccb9eSBenjamin Herrenschmidt 
75313dccb9eSBenjamin Herrenschmidt 			/* We support only 3 memory ranges */
75413dccb9eSBenjamin Herrenschmidt 			if (memno >= 3) {
75513dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
75613dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
75713dccb9eSBenjamin Herrenschmidt 				continue;
75813dccb9eSBenjamin Herrenschmidt 			}
75913dccb9eSBenjamin Herrenschmidt 			/* Handles ISA memory hole space here */
76013dccb9eSBenjamin Herrenschmidt 			if (pci_addr == 0) {
76113dccb9eSBenjamin Herrenschmidt 				isa_mb = cpu_addr;
76213dccb9eSBenjamin Herrenschmidt 				isa_hole = memno;
76313dccb9eSBenjamin Herrenschmidt 				if (primary || isa_mem_base == 0)
76413dccb9eSBenjamin Herrenschmidt 					isa_mem_base = cpu_addr;
765e9f82cb7SBenjamin Herrenschmidt 				hose->isa_mem_phys = cpu_addr;
766e9f82cb7SBenjamin Herrenschmidt 				hose->isa_mem_size = size;
76713dccb9eSBenjamin Herrenschmidt 			}
76813dccb9eSBenjamin Herrenschmidt 
76913dccb9eSBenjamin Herrenschmidt 			/* We get the PCI/Mem offset from the first range or
77013dccb9eSBenjamin Herrenschmidt 			 * the, current one if the offset came from an ISA
77113dccb9eSBenjamin Herrenschmidt 			 * hole. If they don't match, bugger.
77213dccb9eSBenjamin Herrenschmidt 			 */
77313dccb9eSBenjamin Herrenschmidt 			if (memno == 0 ||
77413dccb9eSBenjamin Herrenschmidt 			    (isa_hole >= 0 && pci_addr != 0 &&
77513dccb9eSBenjamin Herrenschmidt 			     hose->pci_mem_offset == isa_mb))
77613dccb9eSBenjamin Herrenschmidt 				hose->pci_mem_offset = cpu_addr - pci_addr;
77713dccb9eSBenjamin Herrenschmidt 			else if (pci_addr != 0 &&
77813dccb9eSBenjamin Herrenschmidt 				 hose->pci_mem_offset != cpu_addr - pci_addr) {
77913dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
78013dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (offset mismatch) !\n");
78113dccb9eSBenjamin Herrenschmidt 				continue;
78213dccb9eSBenjamin Herrenschmidt 			}
78313dccb9eSBenjamin Herrenschmidt 
78413dccb9eSBenjamin Herrenschmidt 			/* Build resource */
78513dccb9eSBenjamin Herrenschmidt 			res = &hose->mem_resources[memno++];
78613dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_MEM;
78713dccb9eSBenjamin Herrenschmidt 			if (pci_space & 0x40000000)
78813dccb9eSBenjamin Herrenschmidt 				res->flags |= IORESOURCE_PREFETCH;
78913dccb9eSBenjamin Herrenschmidt 			res->start = cpu_addr;
79013dccb9eSBenjamin Herrenschmidt 			break;
79113dccb9eSBenjamin Herrenschmidt 		}
79213dccb9eSBenjamin Herrenschmidt 		if (res != NULL) {
79313dccb9eSBenjamin Herrenschmidt 			res->name = dev->full_name;
79413dccb9eSBenjamin Herrenschmidt 			res->end = res->start + size - 1;
79513dccb9eSBenjamin Herrenschmidt 			res->parent = NULL;
79613dccb9eSBenjamin Herrenschmidt 			res->sibling = NULL;
79713dccb9eSBenjamin Herrenschmidt 			res->child = NULL;
79813dccb9eSBenjamin Herrenschmidt 		}
79913dccb9eSBenjamin Herrenschmidt 	}
80013dccb9eSBenjamin Herrenschmidt 
8018db13a0eSBenjamin Herrenschmidt 	/* If there's an ISA hole and the pci_mem_offset is -not- matching
8028db13a0eSBenjamin Herrenschmidt 	 * the ISA hole offset, then we need to remove the ISA hole from
8038db13a0eSBenjamin Herrenschmidt 	 * the resource list for that brige
8048db13a0eSBenjamin Herrenschmidt 	 */
8058db13a0eSBenjamin Herrenschmidt 	if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
8068db13a0eSBenjamin Herrenschmidt 		unsigned int next = isa_hole + 1;
8078db13a0eSBenjamin Herrenschmidt 		printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb);
8088db13a0eSBenjamin Herrenschmidt 		if (next < memno)
8098db13a0eSBenjamin Herrenschmidt 			memmove(&hose->mem_resources[isa_hole],
8108db13a0eSBenjamin Herrenschmidt 				&hose->mem_resources[next],
8118db13a0eSBenjamin Herrenschmidt 				sizeof(struct resource) * (memno - next));
8128db13a0eSBenjamin Herrenschmidt 		hose->mem_resources[--memno].flags = 0;
81313dccb9eSBenjamin Herrenschmidt 	}
81413dccb9eSBenjamin Herrenschmidt }
815fa462f2dSBenjamin Herrenschmidt 
816fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */
817fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus)
818fa462f2dSBenjamin Herrenschmidt {
819fa462f2dSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
8201fd0f525SBenjamin Herrenschmidt 
8210e47ff1cSRob Herring 	if (!pci_has_flag(PCI_ENABLE_PROC_DOMAINS))
822fa462f2dSBenjamin Herrenschmidt 		return 0;
8230e47ff1cSRob Herring 	if (pci_has_flag(PCI_COMPAT_DOMAIN_0))
824fa462f2dSBenjamin Herrenschmidt 		return hose->global_number != 0;
825fa462f2dSBenjamin Herrenschmidt 	return 1;
826fa462f2dSBenjamin Herrenschmidt }
827fa462f2dSBenjamin Herrenschmidt 
828bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are
829bf5e2ba2SBenjamin Herrenschmidt  * probed, but not for bridge ranges
830bf5e2ba2SBenjamin Herrenschmidt  */
831bf5e2ba2SBenjamin Herrenschmidt static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
832bf5e2ba2SBenjamin Herrenschmidt {
833bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
834bf5e2ba2SBenjamin Herrenschmidt 	int i;
835bf5e2ba2SBenjamin Herrenschmidt 
836bf5e2ba2SBenjamin Herrenschmidt 	if (!hose) {
837bf5e2ba2SBenjamin Herrenschmidt 		printk(KERN_ERR "No host bridge for PCI dev %s !\n",
838bf5e2ba2SBenjamin Herrenschmidt 		       pci_name(dev));
839bf5e2ba2SBenjamin Herrenschmidt 		return;
840bf5e2ba2SBenjamin Herrenschmidt 	}
841bf5e2ba2SBenjamin Herrenschmidt 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
842bf5e2ba2SBenjamin Herrenschmidt 		struct resource *res = dev->resource + i;
843bf5e2ba2SBenjamin Herrenschmidt 		if (!res->flags)
844bf5e2ba2SBenjamin Herrenschmidt 			continue;
84548c2ce97SBenjamin Herrenschmidt 
84648c2ce97SBenjamin Herrenschmidt 		/* If we're going to re-assign everything, we mark all resources
84748c2ce97SBenjamin Herrenschmidt 		 * as unset (and 0-base them). In addition, we mark BARs starting
84848c2ce97SBenjamin Herrenschmidt 		 * at 0 as unset as well, except if PCI_PROBE_ONLY is also set
84948c2ce97SBenjamin Herrenschmidt 		 * since in that case, we don't want to re-assign anything
8507f172890SBenjamin Herrenschmidt 		 */
85148c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC) ||
85248c2ce97SBenjamin Herrenschmidt 		    (res->start == 0 && !pci_has_flag(PCI_PROBE_ONLY))) {
85348c2ce97SBenjamin Herrenschmidt 			/* Only print message if not re-assigning */
85448c2ce97SBenjamin Herrenschmidt 			if (!pci_has_flag(PCI_REASSIGN_ALL_RSRC))
85548c2ce97SBenjamin Herrenschmidt 				pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] "
85648c2ce97SBenjamin Herrenschmidt 					 "is unassigned\n",
857bf5e2ba2SBenjamin Herrenschmidt 					 pci_name(dev), i,
858bf5e2ba2SBenjamin Herrenschmidt 					 (unsigned long long)res->start,
859bf5e2ba2SBenjamin Herrenschmidt 					 (unsigned long long)res->end,
860bf5e2ba2SBenjamin Herrenschmidt 					 (unsigned int)res->flags);
861bf5e2ba2SBenjamin Herrenschmidt 			res->end -= res->start;
862bf5e2ba2SBenjamin Herrenschmidt 			res->start = 0;
863bf5e2ba2SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
864bf5e2ba2SBenjamin Herrenschmidt 			continue;
865bf5e2ba2SBenjamin Herrenschmidt 		}
866bf5e2ba2SBenjamin Herrenschmidt 
8676c5705feSBjorn Helgaas 		pr_debug("PCI:%s Resource %d %016llx-%016llx [%x]\n",
868bf5e2ba2SBenjamin Herrenschmidt 			 pci_name(dev), i,
869bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->start,\
870bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
871bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned int)res->flags);
872bf5e2ba2SBenjamin Herrenschmidt 	}
873bf5e2ba2SBenjamin Herrenschmidt 
874bf5e2ba2SBenjamin Herrenschmidt 	/* Call machine specific resource fixup */
875bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_resources)
876bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_resources(dev);
877bf5e2ba2SBenjamin Herrenschmidt }
878bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
879bf5e2ba2SBenjamin Herrenschmidt 
880b5561511SBenjamin Herrenschmidt /* This function tries to figure out if a bridge resource has been initialized
881b5561511SBenjamin Herrenschmidt  * by the firmware or not. It doesn't have to be absolutely bullet proof, but
882b5561511SBenjamin Herrenschmidt  * things go more smoothly when it gets it right. It should covers cases such
883b5561511SBenjamin Herrenschmidt  * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
884b5561511SBenjamin Herrenschmidt  */
885b5561511SBenjamin Herrenschmidt static int __devinit pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
886b5561511SBenjamin Herrenschmidt 							   struct resource *res)
887bf5e2ba2SBenjamin Herrenschmidt {
888be8cbcd8SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
889bf5e2ba2SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
890b5561511SBenjamin Herrenschmidt 	resource_size_t offset;
891b5561511SBenjamin Herrenschmidt 	u16 command;
892b5561511SBenjamin Herrenschmidt 	int i;
893bf5e2ba2SBenjamin Herrenschmidt 
894b5561511SBenjamin Herrenschmidt 	/* We don't do anything if PCI_PROBE_ONLY is set */
8950e47ff1cSRob Herring 	if (pci_has_flag(PCI_PROBE_ONLY))
896b5561511SBenjamin Herrenschmidt 		return 0;
897bf5e2ba2SBenjamin Herrenschmidt 
898b5561511SBenjamin Herrenschmidt 	/* Job is a bit different between memory and IO */
899b5561511SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_MEM) {
900b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0 (res != pci_mem_offset) then it's probably been
901b5561511SBenjamin Herrenschmidt 		 * initialized by somebody
902bf5e2ba2SBenjamin Herrenschmidt 		 */
903b5561511SBenjamin Herrenschmidt 		if (res->start != hose->pci_mem_offset)
904b5561511SBenjamin Herrenschmidt 			return 0;
905b5561511SBenjamin Herrenschmidt 
906b5561511SBenjamin Herrenschmidt 		/* The BAR is 0, let's check if memory decoding is enabled on
907b5561511SBenjamin Herrenschmidt 		 * the bridge. If not, we consider it unassigned
908b5561511SBenjamin Herrenschmidt 		 */
909b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
910b5561511SBenjamin Herrenschmidt 		if ((command & PCI_COMMAND_MEMORY) == 0)
911b5561511SBenjamin Herrenschmidt 			return 1;
912b5561511SBenjamin Herrenschmidt 
913b5561511SBenjamin Herrenschmidt 		/* Memory decoding is enabled and the BAR is 0. If any of the bridge
914b5561511SBenjamin Herrenschmidt 		 * resources covers that starting address (0 then it's good enough for
915b5561511SBenjamin Herrenschmidt 		 * us for memory
916b5561511SBenjamin Herrenschmidt 		 */
917b5561511SBenjamin Herrenschmidt 		for (i = 0; i < 3; i++) {
918b5561511SBenjamin Herrenschmidt 			if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
919b5561511SBenjamin Herrenschmidt 			    hose->mem_resources[i].start == hose->pci_mem_offset)
920b5561511SBenjamin Herrenschmidt 				return 0;
921b5561511SBenjamin Herrenschmidt 		}
922b5561511SBenjamin Herrenschmidt 
923b5561511SBenjamin Herrenschmidt 		/* Well, it starts at 0 and we know it will collide so we may as
924b5561511SBenjamin Herrenschmidt 		 * well consider it as unassigned. That covers the Apple case.
925b5561511SBenjamin Herrenschmidt 		 */
926b5561511SBenjamin Herrenschmidt 		return 1;
927b5561511SBenjamin Herrenschmidt 	} else {
928b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0, then we consider it assigned */
929b5561511SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
930b5561511SBenjamin Herrenschmidt 		if (((res->start - offset) & 0xfffffffful) != 0)
931b5561511SBenjamin Herrenschmidt 			return 0;
932b5561511SBenjamin Herrenschmidt 
933b5561511SBenjamin Herrenschmidt 		/* Here, we are a bit different than memory as typically IO space
934b5561511SBenjamin Herrenschmidt 		 * starting at low addresses -is- valid. What we do instead if that
935b5561511SBenjamin Herrenschmidt 		 * we consider as unassigned anything that doesn't have IO enabled
936b5561511SBenjamin Herrenschmidt 		 * in the PCI command register, and that's it.
937b5561511SBenjamin Herrenschmidt 		 */
938b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
939b5561511SBenjamin Herrenschmidt 		if (command & PCI_COMMAND_IO)
940b5561511SBenjamin Herrenschmidt 			return 0;
941b5561511SBenjamin Herrenschmidt 
942b5561511SBenjamin Herrenschmidt 		/* It's starting at 0 and IO is disabled in the bridge, consider
943b5561511SBenjamin Herrenschmidt 		 * it unassigned
944b5561511SBenjamin Herrenschmidt 		 */
945b5561511SBenjamin Herrenschmidt 		return 1;
946b5561511SBenjamin Herrenschmidt 	}
947b5561511SBenjamin Herrenschmidt }
948b5561511SBenjamin Herrenschmidt 
949b5561511SBenjamin Herrenschmidt /* Fixup resources of a PCI<->PCI bridge */
950b5561511SBenjamin Herrenschmidt static void __devinit pcibios_fixup_bridge(struct pci_bus *bus)
951b5561511SBenjamin Herrenschmidt {
952bf5e2ba2SBenjamin Herrenschmidt 	struct resource *res;
953bf5e2ba2SBenjamin Herrenschmidt 	int i;
954bf5e2ba2SBenjamin Herrenschmidt 
955b5561511SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
956b5561511SBenjamin Herrenschmidt 
95789a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
95889a74eccSBjorn Helgaas 		if (!res || !res->flags)
959bf5e2ba2SBenjamin Herrenschmidt 			continue;
960b188b2aeSKumar Gala 		if (i >= 3 && bus->self->transparent)
961b188b2aeSKumar Gala 			continue;
962be8cbcd8SBenjamin Herrenschmidt 
963*cf1a4cf8SGavin Shan 		/* If we're going to reassign everything, we can
964*cf1a4cf8SGavin Shan 		 * shrink the P2P resource to have size as being
965*cf1a4cf8SGavin Shan 		 * of 0 in order to save space.
96648c2ce97SBenjamin Herrenschmidt 		 */
96748c2ce97SBenjamin Herrenschmidt 		if (pci_has_flag(PCI_REASSIGN_ALL_RSRC)) {
96848c2ce97SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
96948c2ce97SBenjamin Herrenschmidt 			res->start = 0;
970*cf1a4cf8SGavin Shan 			res->end = -1;
97148c2ce97SBenjamin Herrenschmidt 			continue;
97248c2ce97SBenjamin Herrenschmidt 		}
97348c2ce97SBenjamin Herrenschmidt 
9746c5705feSBjorn Helgaas 		pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x]\n",
975bf5e2ba2SBenjamin Herrenschmidt 			 pci_name(dev), i,
976bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->start,\
977bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
978bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned int)res->flags);
979bf5e2ba2SBenjamin Herrenschmidt 
980b5561511SBenjamin Herrenschmidt 		/* Try to detect uninitialized P2P bridge resources,
981b5561511SBenjamin Herrenschmidt 		 * and clear them out so they get re-assigned later
982b5561511SBenjamin Herrenschmidt 		 */
983b5561511SBenjamin Herrenschmidt 		if (pcibios_uninitialized_bridge_resource(bus, res)) {
984b5561511SBenjamin Herrenschmidt 			res->flags = 0;
985b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            (unassigned)\n", pci_name(dev));
986bf5e2ba2SBenjamin Herrenschmidt 		}
987bf5e2ba2SBenjamin Herrenschmidt 	}
988b5561511SBenjamin Herrenschmidt }
989b5561511SBenjamin Herrenschmidt 
9908b8da358SBenjamin Herrenschmidt void __devinit pcibios_setup_bus_self(struct pci_bus *bus)
9918b8da358SBenjamin Herrenschmidt {
9927eef440aSBenjamin Herrenschmidt 	/* Fix up the bus resources for P2P bridges */
9938b8da358SBenjamin Herrenschmidt 	if (bus->self != NULL)
9948b8da358SBenjamin Herrenschmidt 		pcibios_fixup_bridge(bus);
9958b8da358SBenjamin Herrenschmidt 
9968b8da358SBenjamin Herrenschmidt 	/* Platform specific bus fixups. This is currently only used
9977eef440aSBenjamin Herrenschmidt 	 * by fsl_pci and I'm hoping to get rid of it at some point
9988b8da358SBenjamin Herrenschmidt 	 */
9998b8da358SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_bus)
10008b8da358SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_bus(bus);
10018b8da358SBenjamin Herrenschmidt 
10028b8da358SBenjamin Herrenschmidt 	/* Setup bus DMA mappings */
10038b8da358SBenjamin Herrenschmidt 	if (ppc_md.pci_dma_bus_setup)
10048b8da358SBenjamin Herrenschmidt 		ppc_md.pci_dma_bus_setup(bus);
10058b8da358SBenjamin Herrenschmidt }
10068b8da358SBenjamin Herrenschmidt 
10077eef440aSBenjamin Herrenschmidt void __devinit pcibios_setup_bus_devices(struct pci_bus *bus)
10087eef440aSBenjamin Herrenschmidt {
10097eef440aSBenjamin Herrenschmidt 	struct pci_dev *dev;
10107eef440aSBenjamin Herrenschmidt 
10117eef440aSBenjamin Herrenschmidt 	pr_debug("PCI: Fixup bus devices %d (%s)\n",
10127eef440aSBenjamin Herrenschmidt 		 bus->number, bus->self ? pci_name(bus->self) : "PHB");
10137eef440aSBenjamin Herrenschmidt 
10147eef440aSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
10152d1c8618SBenjamin Herrenschmidt 		/* Cardbus can call us to add new devices to a bus, so ignore
10162d1c8618SBenjamin Herrenschmidt 		 * those who are already fully discovered
10172d1c8618SBenjamin Herrenschmidt 		 */
10182d1c8618SBenjamin Herrenschmidt 		if (dev->is_added)
10192d1c8618SBenjamin Herrenschmidt 			continue;
10202d1c8618SBenjamin Herrenschmidt 
10217eef440aSBenjamin Herrenschmidt 		/* Fixup NUMA node as it may not be setup yet by the generic
10227eef440aSBenjamin Herrenschmidt 		 * code and is needed by the DMA init
10237eef440aSBenjamin Herrenschmidt 		 */
10247eef440aSBenjamin Herrenschmidt 		set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
10257eef440aSBenjamin Herrenschmidt 
10267eef440aSBenjamin Herrenschmidt 		/* Hook up default DMA ops */
1027bc0df9ecSNishanth Aravamudan 		set_dma_ops(&dev->dev, pci_dma_ops);
1028738ef42eSBecky Bruce 		set_dma_offset(&dev->dev, PCI_DRAM_OFFSET);
10297eef440aSBenjamin Herrenschmidt 
10307eef440aSBenjamin Herrenschmidt 		/* Additional platform DMA/iommu setup */
10317eef440aSBenjamin Herrenschmidt 		if (ppc_md.pci_dma_dev_setup)
10327eef440aSBenjamin Herrenschmidt 			ppc_md.pci_dma_dev_setup(dev);
10337eef440aSBenjamin Herrenschmidt 
10347eef440aSBenjamin Herrenschmidt 		/* Read default IRQs and fixup if necessary */
10357eef440aSBenjamin Herrenschmidt 		pci_read_irq_line(dev);
10367eef440aSBenjamin Herrenschmidt 		if (ppc_md.pci_irq_fixup)
10377eef440aSBenjamin Herrenschmidt 			ppc_md.pci_irq_fixup(dev);
10387eef440aSBenjamin Herrenschmidt 	}
10397eef440aSBenjamin Herrenschmidt }
10407eef440aSBenjamin Herrenschmidt 
104179c8be83SMyron Stowe void pcibios_set_master(struct pci_dev *dev)
104279c8be83SMyron Stowe {
104379c8be83SMyron Stowe 	/* No special bus mastering setup handling */
104479c8be83SMyron Stowe }
104579c8be83SMyron Stowe 
1046bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_fixup_bus(struct pci_bus *bus)
1047bf5e2ba2SBenjamin Herrenschmidt {
1048bf5e2ba2SBenjamin Herrenschmidt 	/* When called from the generic PCI probe, read PCI<->PCI bridge
10497eef440aSBenjamin Herrenschmidt 	 * bases. This is -not- called when generating the PCI tree from
10508b8da358SBenjamin Herrenschmidt 	 * the OF device-tree.
1051bf5e2ba2SBenjamin Herrenschmidt 	 */
1052bf5e2ba2SBenjamin Herrenschmidt 	if (bus->self != NULL)
1053bf5e2ba2SBenjamin Herrenschmidt 		pci_read_bridge_bases(bus);
10548b8da358SBenjamin Herrenschmidt 
10558b8da358SBenjamin Herrenschmidt 	/* Now fixup the bus bus */
10568b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_self(bus);
10578b8da358SBenjamin Herrenschmidt 
10588b8da358SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
10598b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
1060bf5e2ba2SBenjamin Herrenschmidt }
1061bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus);
1062bf5e2ba2SBenjamin Herrenschmidt 
10632d1c8618SBenjamin Herrenschmidt void __devinit pci_fixup_cardbus(struct pci_bus *bus)
10642d1c8618SBenjamin Herrenschmidt {
10652d1c8618SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
10662d1c8618SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
10672d1c8618SBenjamin Herrenschmidt }
10682d1c8618SBenjamin Herrenschmidt 
10692d1c8618SBenjamin Herrenschmidt 
10703fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev)
10713fd94c6bSBenjamin Herrenschmidt {
10720e47ff1cSRob Herring 	if (pci_has_flag(PCI_CAN_SKIP_ISA_ALIGN) &&
10733fd94c6bSBenjamin Herrenschmidt 	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
10743fd94c6bSBenjamin Herrenschmidt 		return 1;
10753fd94c6bSBenjamin Herrenschmidt 	return 0;
10763fd94c6bSBenjamin Herrenschmidt }
10773fd94c6bSBenjamin Herrenschmidt 
10783fd94c6bSBenjamin Herrenschmidt /*
10793fd94c6bSBenjamin Herrenschmidt  * We need to avoid collisions with `mirrored' VGA ports
10803fd94c6bSBenjamin Herrenschmidt  * and other strange ISA hardware, so we always want the
10813fd94c6bSBenjamin Herrenschmidt  * addresses to be allocated in the 0x000-0x0ff region
10823fd94c6bSBenjamin Herrenschmidt  * modulo 0x400.
10833fd94c6bSBenjamin Herrenschmidt  *
10843fd94c6bSBenjamin Herrenschmidt  * Why? Because some silly external IO cards only decode
10853fd94c6bSBenjamin Herrenschmidt  * the low 10 bits of the IO address. The 0x00-0xff region
10863fd94c6bSBenjamin Herrenschmidt  * is reserved for motherboard devices that decode all 16
10873fd94c6bSBenjamin Herrenschmidt  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
10883fd94c6bSBenjamin Herrenschmidt  * but we want to try to avoid allocating at 0x2900-0x2bff
10893fd94c6bSBenjamin Herrenschmidt  * which might have be mirrored at 0x0100-0x03ff..
10903fd94c6bSBenjamin Herrenschmidt  */
10913b7a17fcSDominik Brodowski resource_size_t pcibios_align_resource(void *data, const struct resource *res,
10923fd94c6bSBenjamin Herrenschmidt 				resource_size_t size, resource_size_t align)
10933fd94c6bSBenjamin Herrenschmidt {
10943fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = data;
10953fd94c6bSBenjamin Herrenschmidt 	resource_size_t start = res->start;
10963fd94c6bSBenjamin Herrenschmidt 
1097b26b2d49SDominik Brodowski 	if (res->flags & IORESOURCE_IO) {
10983fd94c6bSBenjamin Herrenschmidt 		if (skip_isa_ioresource_align(dev))
1099b26b2d49SDominik Brodowski 			return start;
1100b26b2d49SDominik Brodowski 		if (start & 0x300)
11013fd94c6bSBenjamin Herrenschmidt 			start = (start + 0x3ff) & ~0x3ff;
11023fd94c6bSBenjamin Herrenschmidt 	}
1103b26b2d49SDominik Brodowski 
1104b26b2d49SDominik Brodowski 	return start;
11053fd94c6bSBenjamin Herrenschmidt }
11063fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource);
11073fd94c6bSBenjamin Herrenschmidt 
11083fd94c6bSBenjamin Herrenschmidt /*
11093fd94c6bSBenjamin Herrenschmidt  * Reparent resource children of pr that conflict with res
11103fd94c6bSBenjamin Herrenschmidt  * under res, and make res replace those children.
11113fd94c6bSBenjamin Herrenschmidt  */
11120f6023d5SHeiko Schocher static int reparent_resources(struct resource *parent,
11133fd94c6bSBenjamin Herrenschmidt 				     struct resource *res)
11143fd94c6bSBenjamin Herrenschmidt {
11153fd94c6bSBenjamin Herrenschmidt 	struct resource *p, **pp;
11163fd94c6bSBenjamin Herrenschmidt 	struct resource **firstpp = NULL;
11173fd94c6bSBenjamin Herrenschmidt 
11183fd94c6bSBenjamin Herrenschmidt 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
11193fd94c6bSBenjamin Herrenschmidt 		if (p->end < res->start)
11203fd94c6bSBenjamin Herrenschmidt 			continue;
11213fd94c6bSBenjamin Herrenschmidt 		if (res->end < p->start)
11223fd94c6bSBenjamin Herrenschmidt 			break;
11233fd94c6bSBenjamin Herrenschmidt 		if (p->start < res->start || p->end > res->end)
11243fd94c6bSBenjamin Herrenschmidt 			return -1;	/* not completely contained */
11253fd94c6bSBenjamin Herrenschmidt 		if (firstpp == NULL)
11263fd94c6bSBenjamin Herrenschmidt 			firstpp = pp;
11273fd94c6bSBenjamin Herrenschmidt 	}
11283fd94c6bSBenjamin Herrenschmidt 	if (firstpp == NULL)
11293fd94c6bSBenjamin Herrenschmidt 		return -1;	/* didn't find any conflicting entries? */
11303fd94c6bSBenjamin Herrenschmidt 	res->parent = parent;
11313fd94c6bSBenjamin Herrenschmidt 	res->child = *firstpp;
11323fd94c6bSBenjamin Herrenschmidt 	res->sibling = *pp;
11333fd94c6bSBenjamin Herrenschmidt 	*firstpp = res;
11343fd94c6bSBenjamin Herrenschmidt 	*pp = NULL;
11353fd94c6bSBenjamin Herrenschmidt 	for (p = res->child; p != NULL; p = p->sibling) {
11363fd94c6bSBenjamin Herrenschmidt 		p->parent = res;
1137b0494bc8SBenjamin Herrenschmidt 		pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n",
11383fd94c6bSBenjamin Herrenschmidt 			 p->name,
11393fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)p->start,
11403fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)p->end, res->name);
11413fd94c6bSBenjamin Herrenschmidt 	}
11423fd94c6bSBenjamin Herrenschmidt 	return 0;
11433fd94c6bSBenjamin Herrenschmidt }
11443fd94c6bSBenjamin Herrenschmidt 
11453fd94c6bSBenjamin Herrenschmidt /*
11463fd94c6bSBenjamin Herrenschmidt  *  Handle resources of PCI devices.  If the world were perfect, we could
11473fd94c6bSBenjamin Herrenschmidt  *  just allocate all the resource regions and do nothing more.  It isn't.
11483fd94c6bSBenjamin Herrenschmidt  *  On the other hand, we cannot just re-allocate all devices, as it would
11493fd94c6bSBenjamin Herrenschmidt  *  require us to know lots of host bridge internals.  So we attempt to
11503fd94c6bSBenjamin Herrenschmidt  *  keep as much of the original configuration as possible, but tweak it
11513fd94c6bSBenjamin Herrenschmidt  *  when it's found to be wrong.
11523fd94c6bSBenjamin Herrenschmidt  *
11533fd94c6bSBenjamin Herrenschmidt  *  Known BIOS problems we have to work around:
11543fd94c6bSBenjamin Herrenschmidt  *	- I/O or memory regions not configured
11553fd94c6bSBenjamin Herrenschmidt  *	- regions configured, but not enabled in the command register
11563fd94c6bSBenjamin Herrenschmidt  *	- bogus I/O addresses above 64K used
11573fd94c6bSBenjamin Herrenschmidt  *	- expansion ROMs left enabled (this may sound harmless, but given
11583fd94c6bSBenjamin Herrenschmidt  *	  the fact the PCI specs explicitly allow address decoders to be
11593fd94c6bSBenjamin Herrenschmidt  *	  shared between expansion ROMs and other resource regions, it's
11603fd94c6bSBenjamin Herrenschmidt  *	  at least dangerous)
11613fd94c6bSBenjamin Herrenschmidt  *
11623fd94c6bSBenjamin Herrenschmidt  *  Our solution:
11633fd94c6bSBenjamin Herrenschmidt  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
11643fd94c6bSBenjamin Herrenschmidt  *	    This gives us fixed barriers on where we can allocate.
11653fd94c6bSBenjamin Herrenschmidt  *	(2) Allocate resources for all enabled devices.  If there is
11663fd94c6bSBenjamin Herrenschmidt  *	    a collision, just mark the resource as unallocated. Also
11673fd94c6bSBenjamin Herrenschmidt  *	    disable expansion ROMs during this step.
11683fd94c6bSBenjamin Herrenschmidt  *	(3) Try to allocate resources for disabled devices.  If the
11693fd94c6bSBenjamin Herrenschmidt  *	    resources were assigned correctly, everything goes well,
11703fd94c6bSBenjamin Herrenschmidt  *	    if they weren't, they won't disturb allocation of other
11713fd94c6bSBenjamin Herrenschmidt  *	    resources.
11723fd94c6bSBenjamin Herrenschmidt  *	(4) Assign new addresses to resources which were either
11733fd94c6bSBenjamin Herrenschmidt  *	    not configured at all or misconfigured.  If explicitly
11743fd94c6bSBenjamin Herrenschmidt  *	    requested by the user, configure expansion ROM address
11753fd94c6bSBenjamin Herrenschmidt  *	    as well.
11763fd94c6bSBenjamin Herrenschmidt  */
11773fd94c6bSBenjamin Herrenschmidt 
1178e90a1318SNathan Fontenot void pcibios_allocate_bus_resources(struct pci_bus *bus)
11793fd94c6bSBenjamin Herrenschmidt {
1180e90a1318SNathan Fontenot 	struct pci_bus *b;
11813fd94c6bSBenjamin Herrenschmidt 	int i;
11823fd94c6bSBenjamin Herrenschmidt 	struct resource *res, *pr;
11833fd94c6bSBenjamin Herrenschmidt 
1184b5ae5f91SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1185b5ae5f91SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1186b5ae5f91SBenjamin Herrenschmidt 
118789a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
118889a74eccSBjorn Helgaas 		if (!res || !res->flags || res->start > res->end || res->parent)
11893fd94c6bSBenjamin Herrenschmidt 			continue;
119048c2ce97SBenjamin Herrenschmidt 
119148c2ce97SBenjamin Herrenschmidt 		/* If the resource was left unset at this point, we clear it */
119248c2ce97SBenjamin Herrenschmidt 		if (res->flags & IORESOURCE_UNSET)
119348c2ce97SBenjamin Herrenschmidt 			goto clear_resource;
119448c2ce97SBenjamin Herrenschmidt 
11953fd94c6bSBenjamin Herrenschmidt 		if (bus->parent == NULL)
11963fd94c6bSBenjamin Herrenschmidt 			pr = (res->flags & IORESOURCE_IO) ?
11973fd94c6bSBenjamin Herrenschmidt 				&ioport_resource : &iomem_resource;
11983fd94c6bSBenjamin Herrenschmidt 		else {
11993fd94c6bSBenjamin Herrenschmidt 			pr = pci_find_parent_resource(bus->self, res);
12003fd94c6bSBenjamin Herrenschmidt 			if (pr == res) {
12013fd94c6bSBenjamin Herrenschmidt 				/* this happens when the generic PCI
12023fd94c6bSBenjamin Herrenschmidt 				 * code (wrongly) decides that this
12033fd94c6bSBenjamin Herrenschmidt 				 * bridge is transparent  -- paulus
12043fd94c6bSBenjamin Herrenschmidt 				 */
12053fd94c6bSBenjamin Herrenschmidt 				continue;
12063fd94c6bSBenjamin Herrenschmidt 			}
12073fd94c6bSBenjamin Herrenschmidt 		}
12083fd94c6bSBenjamin Herrenschmidt 
1209b0494bc8SBenjamin Herrenschmidt 		pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
12103fd94c6bSBenjamin Herrenschmidt 			 "[0x%x], parent %p (%s)\n",
12113fd94c6bSBenjamin Herrenschmidt 			 bus->self ? pci_name(bus->self) : "PHB",
12123fd94c6bSBenjamin Herrenschmidt 			 bus->number, i,
12133fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)res->start,
12143fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)res->end,
12153fd94c6bSBenjamin Herrenschmidt 			 (unsigned int)res->flags,
12163fd94c6bSBenjamin Herrenschmidt 			 pr, (pr && pr->name) ? pr->name : "nil");
12173fd94c6bSBenjamin Herrenschmidt 
12183fd94c6bSBenjamin Herrenschmidt 		if (pr && !(pr->flags & IORESOURCE_UNSET)) {
12193fd94c6bSBenjamin Herrenschmidt 			if (request_resource(pr, res) == 0)
12203fd94c6bSBenjamin Herrenschmidt 				continue;
12213fd94c6bSBenjamin Herrenschmidt 			/*
12223fd94c6bSBenjamin Herrenschmidt 			 * Must be a conflict with an existing entry.
12233fd94c6bSBenjamin Herrenschmidt 			 * Move that entry (or entries) under the
12243fd94c6bSBenjamin Herrenschmidt 			 * bridge resource and try again.
12253fd94c6bSBenjamin Herrenschmidt 			 */
12263fd94c6bSBenjamin Herrenschmidt 			if (reparent_resources(pr, res) == 0)
12273fd94c6bSBenjamin Herrenschmidt 				continue;
12283fd94c6bSBenjamin Herrenschmidt 		}
122948c2ce97SBenjamin Herrenschmidt 		pr_warning("PCI: Cannot allocate resource region "
1230e90a1318SNathan Fontenot 			   "%d of PCI bridge %d, will remap\n", i, bus->number);
12313fd94c6bSBenjamin Herrenschmidt 	clear_resource:
1232*cf1a4cf8SGavin Shan 		/* The resource might be figured out when doing
1233*cf1a4cf8SGavin Shan 		 * reassignment based on the resources required
1234*cf1a4cf8SGavin Shan 		 * by the downstream PCI devices. Here we set
1235*cf1a4cf8SGavin Shan 		 * the size of the resource to be 0 in order to
1236*cf1a4cf8SGavin Shan 		 * save more space.
1237*cf1a4cf8SGavin Shan 		 */
1238*cf1a4cf8SGavin Shan 		res->start = 0;
1239*cf1a4cf8SGavin Shan 		res->end = -1;
12403fd94c6bSBenjamin Herrenschmidt 		res->flags = 0;
12413fd94c6bSBenjamin Herrenschmidt 	}
1242e90a1318SNathan Fontenot 
1243e90a1318SNathan Fontenot 	list_for_each_entry(b, &bus->children, node)
1244e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
12453fd94c6bSBenjamin Herrenschmidt }
12463fd94c6bSBenjamin Herrenschmidt 
1247533b1928SPaul Mackerras static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
12483fd94c6bSBenjamin Herrenschmidt {
12493fd94c6bSBenjamin Herrenschmidt 	struct resource *pr, *r = &dev->resource[idx];
12503fd94c6bSBenjamin Herrenschmidt 
1251b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
12523fd94c6bSBenjamin Herrenschmidt 		 pci_name(dev), idx,
12533fd94c6bSBenjamin Herrenschmidt 		 (unsigned long long)r->start,
12543fd94c6bSBenjamin Herrenschmidt 		 (unsigned long long)r->end,
12553fd94c6bSBenjamin Herrenschmidt 		 (unsigned int)r->flags);
12563fd94c6bSBenjamin Herrenschmidt 
12573fd94c6bSBenjamin Herrenschmidt 	pr = pci_find_parent_resource(dev, r);
12583fd94c6bSBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||
12593fd94c6bSBenjamin Herrenschmidt 	    request_resource(pr, r) < 0) {
12603fd94c6bSBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
12613fd94c6bSBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
12623fd94c6bSBenjamin Herrenschmidt 		if (pr)
1263b0494bc8SBenjamin Herrenschmidt 			pr_debug("PCI:  parent is %p: %016llx-%016llx [%x]\n",
1264b0494bc8SBenjamin Herrenschmidt 				 pr,
12653fd94c6bSBenjamin Herrenschmidt 				 (unsigned long long)pr->start,
12663fd94c6bSBenjamin Herrenschmidt 				 (unsigned long long)pr->end,
12673fd94c6bSBenjamin Herrenschmidt 				 (unsigned int)pr->flags);
12683fd94c6bSBenjamin Herrenschmidt 		/* We'll assign a new address later */
12693fd94c6bSBenjamin Herrenschmidt 		r->flags |= IORESOURCE_UNSET;
12703fd94c6bSBenjamin Herrenschmidt 		r->end -= r->start;
12713fd94c6bSBenjamin Herrenschmidt 		r->start = 0;
12723fd94c6bSBenjamin Herrenschmidt 	}
12733fd94c6bSBenjamin Herrenschmidt }
12743fd94c6bSBenjamin Herrenschmidt 
12753fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass)
12763fd94c6bSBenjamin Herrenschmidt {
12773fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = NULL;
12783fd94c6bSBenjamin Herrenschmidt 	int idx, disabled;
12793fd94c6bSBenjamin Herrenschmidt 	u16 command;
12803fd94c6bSBenjamin Herrenschmidt 	struct resource *r;
12813fd94c6bSBenjamin Herrenschmidt 
12823fd94c6bSBenjamin Herrenschmidt 	for_each_pci_dev(dev) {
12833fd94c6bSBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1284ad892a63SBenjamin Herrenschmidt 		for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
12853fd94c6bSBenjamin Herrenschmidt 			r = &dev->resource[idx];
12863fd94c6bSBenjamin Herrenschmidt 			if (r->parent)		/* Already allocated */
12873fd94c6bSBenjamin Herrenschmidt 				continue;
12883fd94c6bSBenjamin Herrenschmidt 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
12893fd94c6bSBenjamin Herrenschmidt 				continue;	/* Not assigned at all */
1290ad892a63SBenjamin Herrenschmidt 			/* We only allocate ROMs on pass 1 just in case they
1291ad892a63SBenjamin Herrenschmidt 			 * have been screwed up by firmware
1292ad892a63SBenjamin Herrenschmidt 			 */
1293ad892a63SBenjamin Herrenschmidt 			if (idx == PCI_ROM_RESOURCE )
1294ad892a63SBenjamin Herrenschmidt 				disabled = 1;
12953fd94c6bSBenjamin Herrenschmidt 			if (r->flags & IORESOURCE_IO)
12963fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_IO);
12973fd94c6bSBenjamin Herrenschmidt 			else
12983fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_MEMORY);
1299533b1928SPaul Mackerras 			if (pass == disabled)
1300533b1928SPaul Mackerras 				alloc_resource(dev, idx);
13013fd94c6bSBenjamin Herrenschmidt 		}
13023fd94c6bSBenjamin Herrenschmidt 		if (pass)
13033fd94c6bSBenjamin Herrenschmidt 			continue;
13043fd94c6bSBenjamin Herrenschmidt 		r = &dev->resource[PCI_ROM_RESOURCE];
1305ad892a63SBenjamin Herrenschmidt 		if (r->flags) {
13063fd94c6bSBenjamin Herrenschmidt 			/* Turn the ROM off, leave the resource region,
13073fd94c6bSBenjamin Herrenschmidt 			 * but keep it unregistered.
13083fd94c6bSBenjamin Herrenschmidt 			 */
13093fd94c6bSBenjamin Herrenschmidt 			u32 reg;
1310ad892a63SBenjamin Herrenschmidt 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1311ad892a63SBenjamin Herrenschmidt 			if (reg & PCI_ROM_ADDRESS_ENABLE) {
1312b0494bc8SBenjamin Herrenschmidt 				pr_debug("PCI: Switching off ROM of %s\n",
1313b0494bc8SBenjamin Herrenschmidt 					 pci_name(dev));
13143fd94c6bSBenjamin Herrenschmidt 				r->flags &= ~IORESOURCE_ROM_ENABLE;
13153fd94c6bSBenjamin Herrenschmidt 				pci_write_config_dword(dev, dev->rom_base_reg,
13163fd94c6bSBenjamin Herrenschmidt 						       reg & ~PCI_ROM_ADDRESS_ENABLE);
13173fd94c6bSBenjamin Herrenschmidt 			}
13183fd94c6bSBenjamin Herrenschmidt 		}
13193fd94c6bSBenjamin Herrenschmidt 	}
1320ad892a63SBenjamin Herrenschmidt }
13213fd94c6bSBenjamin Herrenschmidt 
1322c1f34302SBenjamin Herrenschmidt static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
1323c1f34302SBenjamin Herrenschmidt {
1324c1f34302SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
1325c1f34302SBenjamin Herrenschmidt 	resource_size_t	offset;
1326c1f34302SBenjamin Herrenschmidt 	struct resource *res, *pres;
1327c1f34302SBenjamin Herrenschmidt 	int i;
1328c1f34302SBenjamin Herrenschmidt 
1329c1f34302SBenjamin Herrenschmidt 	pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
1330c1f34302SBenjamin Herrenschmidt 
1331c1f34302SBenjamin Herrenschmidt 	/* Check for IO */
1332c1f34302SBenjamin Herrenschmidt 	if (!(hose->io_resource.flags & IORESOURCE_IO))
1333c1f34302SBenjamin Herrenschmidt 		goto no_io;
1334c1f34302SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1335c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1336c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1337c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy IO";
1338c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_IO;
1339c1f34302SBenjamin Herrenschmidt 	res->start = offset;
1340c1f34302SBenjamin Herrenschmidt 	res->end = (offset + 0xfff) & 0xfffffffful;
1341c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate legacy IO: %pR\n", res);
1342c1f34302SBenjamin Herrenschmidt 	if (request_resource(&hose->io_resource, res)) {
1343c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1344c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1345c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1346c1f34302SBenjamin Herrenschmidt 		kfree(res);
1347c1f34302SBenjamin Herrenschmidt 	}
1348c1f34302SBenjamin Herrenschmidt 
1349c1f34302SBenjamin Herrenschmidt  no_io:
1350c1f34302SBenjamin Herrenschmidt 	/* Check for memory */
1351c1f34302SBenjamin Herrenschmidt 	offset = hose->pci_mem_offset;
1352c1f34302SBenjamin Herrenschmidt 	pr_debug("hose mem offset: %016llx\n", (unsigned long long)offset);
1353c1f34302SBenjamin Herrenschmidt 	for (i = 0; i < 3; i++) {
1354c1f34302SBenjamin Herrenschmidt 		pres = &hose->mem_resources[i];
1355c1f34302SBenjamin Herrenschmidt 		if (!(pres->flags & IORESOURCE_MEM))
1356c1f34302SBenjamin Herrenschmidt 			continue;
1357c1f34302SBenjamin Herrenschmidt 		pr_debug("hose mem res: %pR\n", pres);
1358c1f34302SBenjamin Herrenschmidt 		if ((pres->start - offset) <= 0xa0000 &&
1359c1f34302SBenjamin Herrenschmidt 		    (pres->end - offset) >= 0xbffff)
1360c1f34302SBenjamin Herrenschmidt 			break;
1361c1f34302SBenjamin Herrenschmidt 	}
1362c1f34302SBenjamin Herrenschmidt 	if (i >= 3)
1363c1f34302SBenjamin Herrenschmidt 		return;
1364c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1365c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1366c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy VGA memory";
1367c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_MEM;
1368c1f34302SBenjamin Herrenschmidt 	res->start = 0xa0000 + offset;
1369c1f34302SBenjamin Herrenschmidt 	res->end = 0xbffff + offset;
1370c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate VGA memory: %pR\n", res);
1371c1f34302SBenjamin Herrenschmidt 	if (request_resource(pres, res)) {
1372c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1373c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1374c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1375c1f34302SBenjamin Herrenschmidt 		kfree(res);
1376c1f34302SBenjamin Herrenschmidt 	}
1377c1f34302SBenjamin Herrenschmidt }
1378c1f34302SBenjamin Herrenschmidt 
13793fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void)
13803fd94c6bSBenjamin Herrenschmidt {
1381e90a1318SNathan Fontenot 	struct pci_bus *b;
1382e90a1318SNathan Fontenot 
138348c2ce97SBenjamin Herrenschmidt 	/* Allocate and assign resources */
1384e90a1318SNathan Fontenot 	list_for_each_entry(b, &pci_root_buses, node)
1385e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
13863fd94c6bSBenjamin Herrenschmidt 	pcibios_allocate_resources(0);
13873fd94c6bSBenjamin Herrenschmidt 	pcibios_allocate_resources(1);
13883fd94c6bSBenjamin Herrenschmidt 
1389c1f34302SBenjamin Herrenschmidt 	/* Before we start assigning unassigned resource, we try to reserve
1390c1f34302SBenjamin Herrenschmidt 	 * the low IO area and the VGA memory area if they intersect the
1391c1f34302SBenjamin Herrenschmidt 	 * bus available resources to avoid allocating things on top of them
1392c1f34302SBenjamin Herrenschmidt 	 */
13930e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1394c1f34302SBenjamin Herrenschmidt 		list_for_each_entry(b, &pci_root_buses, node)
1395c1f34302SBenjamin Herrenschmidt 			pcibios_reserve_legacy_regions(b);
1396c1f34302SBenjamin Herrenschmidt 	}
1397c1f34302SBenjamin Herrenschmidt 
1398c1f34302SBenjamin Herrenschmidt 	/* Now, if the platform didn't decide to blindly trust the firmware,
1399c1f34302SBenjamin Herrenschmidt 	 * we proceed to assigning things that were left unassigned
1400c1f34302SBenjamin Herrenschmidt 	 */
14010e47ff1cSRob Herring 	if (!pci_has_flag(PCI_PROBE_ONLY)) {
1402a77acda0SWolfram Sang 		pr_debug("PCI: Assigning unassigned resources...\n");
14033fd94c6bSBenjamin Herrenschmidt 		pci_assign_unassigned_resources();
14043fd94c6bSBenjamin Herrenschmidt 	}
14053fd94c6bSBenjamin Herrenschmidt 
14063fd94c6bSBenjamin Herrenschmidt 	/* Call machine dependent fixup */
14073fd94c6bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup)
14083fd94c6bSBenjamin Herrenschmidt 		ppc_md.pcibios_fixup();
14093fd94c6bSBenjamin Herrenschmidt }
14103fd94c6bSBenjamin Herrenschmidt 
14113fd94c6bSBenjamin Herrenschmidt #ifdef CONFIG_HOTPLUG
14128b8da358SBenjamin Herrenschmidt 
1413fd6852c8SBenjamin Herrenschmidt /* This is used by the PCI hotplug driver to allocate resource
14143fd94c6bSBenjamin Herrenschmidt  * of newly plugged busses. We can try to consolidate with the
1415fd6852c8SBenjamin Herrenschmidt  * rest of the code later, for now, keep it as-is as our main
1416fd6852c8SBenjamin Herrenschmidt  * resource allocation function doesn't deal with sub-trees yet.
14173fd94c6bSBenjamin Herrenschmidt  */
1418baf75b0aSStephen Rothwell void pcibios_claim_one_bus(struct pci_bus *bus)
14193fd94c6bSBenjamin Herrenschmidt {
14203fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev;
14213fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *child_bus;
14223fd94c6bSBenjamin Herrenschmidt 
14233fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
14243fd94c6bSBenjamin Herrenschmidt 		int i;
14253fd94c6bSBenjamin Herrenschmidt 
14263fd94c6bSBenjamin Herrenschmidt 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
14273fd94c6bSBenjamin Herrenschmidt 			struct resource *r = &dev->resource[i];
14283fd94c6bSBenjamin Herrenschmidt 
14293fd94c6bSBenjamin Herrenschmidt 			if (r->parent || !r->start || !r->flags)
14303fd94c6bSBenjamin Herrenschmidt 				continue;
1431fd6852c8SBenjamin Herrenschmidt 
1432fd6852c8SBenjamin Herrenschmidt 			pr_debug("PCI: Claiming %s: "
1433fd6852c8SBenjamin Herrenschmidt 				 "Resource %d: %016llx..%016llx [%x]\n",
1434fd6852c8SBenjamin Herrenschmidt 				 pci_name(dev), i,
1435fd6852c8SBenjamin Herrenschmidt 				 (unsigned long long)r->start,
1436fd6852c8SBenjamin Herrenschmidt 				 (unsigned long long)r->end,
1437fd6852c8SBenjamin Herrenschmidt 				 (unsigned int)r->flags);
1438fd6852c8SBenjamin Herrenschmidt 
14393fd94c6bSBenjamin Herrenschmidt 			pci_claim_resource(dev, i);
14403fd94c6bSBenjamin Herrenschmidt 		}
14413fd94c6bSBenjamin Herrenschmidt 	}
14423fd94c6bSBenjamin Herrenschmidt 
14433fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(child_bus, &bus->children, node)
14443fd94c6bSBenjamin Herrenschmidt 		pcibios_claim_one_bus(child_bus);
14453fd94c6bSBenjamin Herrenschmidt }
1446fd6852c8SBenjamin Herrenschmidt 
1447fd6852c8SBenjamin Herrenschmidt 
1448fd6852c8SBenjamin Herrenschmidt /* pcibios_finish_adding_to_bus
1449fd6852c8SBenjamin Herrenschmidt  *
1450fd6852c8SBenjamin Herrenschmidt  * This is to be called by the hotplug code after devices have been
1451fd6852c8SBenjamin Herrenschmidt  * added to a bus, this include calling it for a PHB that is just
1452fd6852c8SBenjamin Herrenschmidt  * being added
1453fd6852c8SBenjamin Herrenschmidt  */
1454fd6852c8SBenjamin Herrenschmidt void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1455fd6852c8SBenjamin Herrenschmidt {
1456fd6852c8SBenjamin Herrenschmidt 	pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1457fd6852c8SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1458fd6852c8SBenjamin Herrenschmidt 
1459fd6852c8SBenjamin Herrenschmidt 	/* Allocate bus and devices resources */
1460fd6852c8SBenjamin Herrenschmidt 	pcibios_allocate_bus_resources(bus);
1461fd6852c8SBenjamin Herrenschmidt 	pcibios_claim_one_bus(bus);
1462fd6852c8SBenjamin Herrenschmidt 
1463fd6852c8SBenjamin Herrenschmidt 	/* Add new devices to global lists.  Register in proc, sysfs. */
1464fd6852c8SBenjamin Herrenschmidt 	pci_bus_add_devices(bus);
1465fd6852c8SBenjamin Herrenschmidt 
1466fd6852c8SBenjamin Herrenschmidt 	/* Fixup EEH */
1467fd6852c8SBenjamin Herrenschmidt 	eeh_add_device_tree_late(bus);
1468fd6852c8SBenjamin Herrenschmidt }
1469fd6852c8SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1470fd6852c8SBenjamin Herrenschmidt 
14713fd94c6bSBenjamin Herrenschmidt #endif /* CONFIG_HOTPLUG */
1472549beb9bSBenjamin Herrenschmidt 
1473549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask)
1474549beb9bSBenjamin Herrenschmidt {
1475549beb9bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_enable_device_hook)
1476549beb9bSBenjamin Herrenschmidt 		if (ppc_md.pcibios_enable_device_hook(dev))
1477549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1478549beb9bSBenjamin Herrenschmidt 
14797cfb5f9aSBjorn Helgaas 	return pci_enable_resources(dev, mask);
1480549beb9bSBenjamin Herrenschmidt }
148153280323SBenjamin Herrenschmidt 
148238973ba7SBjorn Helgaas resource_size_t pcibios_io_space_offset(struct pci_controller *hose)
148338973ba7SBjorn Helgaas {
148438973ba7SBjorn Helgaas 	return (unsigned long) hose->io_base_virt - _IO_BASE;
148538973ba7SBjorn Helgaas }
148638973ba7SBjorn Helgaas 
148745a709f8SBjorn Helgaas static void __devinit pcibios_setup_phb_resources(struct pci_controller *hose, struct list_head *resources)
148853280323SBenjamin Herrenschmidt {
148953280323SBenjamin Herrenschmidt 	struct resource *res;
149053280323SBenjamin Herrenschmidt 	int i;
149153280323SBenjamin Herrenschmidt 
149253280323SBenjamin Herrenschmidt 	/* Hookup PHB IO resource */
149345a709f8SBjorn Helgaas 	res = &hose->io_resource;
149453280323SBenjamin Herrenschmidt 
149553280323SBenjamin Herrenschmidt 	if (!res->flags) {
149653280323SBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: I/O resource not set for host"
149753280323SBenjamin Herrenschmidt 		       " bridge %s (domain %d)\n",
149853280323SBenjamin Herrenschmidt 		       hose->dn->full_name, hose->global_number);
149953280323SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
150053280323SBenjamin Herrenschmidt 		/* Workaround for lack of IO resource only on 32-bit */
150153280323SBenjamin Herrenschmidt 		res->start = (unsigned long)hose->io_base_virt - isa_io_base;
150253280323SBenjamin Herrenschmidt 		res->end = res->start + IO_SPACE_LIMIT;
150353280323SBenjamin Herrenschmidt 		res->flags = IORESOURCE_IO;
150453280323SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
150553280323SBenjamin Herrenschmidt 	}
150653280323SBenjamin Herrenschmidt 
150753280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB IO resource    = %016llx-%016llx [%lx]\n",
150853280323SBenjamin Herrenschmidt 		 (unsigned long long)res->start,
150953280323SBenjamin Herrenschmidt 		 (unsigned long long)res->end,
151053280323SBenjamin Herrenschmidt 		 (unsigned long)res->flags);
151138973ba7SBjorn Helgaas 	pci_add_resource_offset(resources, res, pcibios_io_space_offset(hose));
151253280323SBenjamin Herrenschmidt 
151353280323SBenjamin Herrenschmidt 	/* Hookup PHB Memory resources */
151453280323SBenjamin Herrenschmidt 	for (i = 0; i < 3; ++i) {
151553280323SBenjamin Herrenschmidt 		res = &hose->mem_resources[i];
151653280323SBenjamin Herrenschmidt 		if (!res->flags) {
151753280323SBenjamin Herrenschmidt 			if (i > 0)
151853280323SBenjamin Herrenschmidt 				continue;
151953280323SBenjamin Herrenschmidt 			printk(KERN_ERR "PCI: Memory resource 0 not set for "
152053280323SBenjamin Herrenschmidt 			       "host bridge %s (domain %d)\n",
152153280323SBenjamin Herrenschmidt 			       hose->dn->full_name, hose->global_number);
152253280323SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
152353280323SBenjamin Herrenschmidt 			/* Workaround for lack of MEM resource only on 32-bit */
152453280323SBenjamin Herrenschmidt 			res->start = hose->pci_mem_offset;
152553280323SBenjamin Herrenschmidt 			res->end = (resource_size_t)-1LL;
152653280323SBenjamin Herrenschmidt 			res->flags = IORESOURCE_MEM;
152753280323SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
152853280323SBenjamin Herrenschmidt 		}
152953280323SBenjamin Herrenschmidt 
153053280323SBenjamin Herrenschmidt 		pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n", i,
153153280323SBenjamin Herrenschmidt 			 (unsigned long long)res->start,
153253280323SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
153353280323SBenjamin Herrenschmidt 			 (unsigned long)res->flags);
15346c5705feSBjorn Helgaas 		pci_add_resource_offset(resources, res, hose->pci_mem_offset);
153553280323SBenjamin Herrenschmidt 	}
153653280323SBenjamin Herrenschmidt 
153753280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB MEM offset     = %016llx\n",
153853280323SBenjamin Herrenschmidt 		 (unsigned long long)hose->pci_mem_offset);
153953280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB IO  offset     = %08lx\n",
154053280323SBenjamin Herrenschmidt 		 (unsigned long)hose->io_base_virt - _IO_BASE);
154153280323SBenjamin Herrenschmidt 
154253280323SBenjamin Herrenschmidt }
154389c2dd62SKumar Gala 
154489c2dd62SKumar Gala /*
154589c2dd62SKumar Gala  * Null PCI config access functions, for the case when we can't
154689c2dd62SKumar Gala  * find a hose.
154789c2dd62SKumar Gala  */
154889c2dd62SKumar Gala #define NULL_PCI_OP(rw, size, type)					\
154989c2dd62SKumar Gala static int								\
155089c2dd62SKumar Gala null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
155189c2dd62SKumar Gala {									\
155289c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
155389c2dd62SKumar Gala }
155489c2dd62SKumar Gala 
155589c2dd62SKumar Gala static int
155689c2dd62SKumar Gala null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
155789c2dd62SKumar Gala 		 int len, u32 *val)
155889c2dd62SKumar Gala {
155989c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
156089c2dd62SKumar Gala }
156189c2dd62SKumar Gala 
156289c2dd62SKumar Gala static int
156389c2dd62SKumar Gala null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
156489c2dd62SKumar Gala 		  int len, u32 val)
156589c2dd62SKumar Gala {
156689c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
156789c2dd62SKumar Gala }
156889c2dd62SKumar Gala 
156989c2dd62SKumar Gala static struct pci_ops null_pci_ops =
157089c2dd62SKumar Gala {
157189c2dd62SKumar Gala 	.read = null_read_config,
157289c2dd62SKumar Gala 	.write = null_write_config,
157389c2dd62SKumar Gala };
157489c2dd62SKumar Gala 
157589c2dd62SKumar Gala /*
157689c2dd62SKumar Gala  * These functions are used early on before PCI scanning is done
157789c2dd62SKumar Gala  * and all of the pci_dev and pci_bus structures have been created.
157889c2dd62SKumar Gala  */
157989c2dd62SKumar Gala static struct pci_bus *
158089c2dd62SKumar Gala fake_pci_bus(struct pci_controller *hose, int busnr)
158189c2dd62SKumar Gala {
158289c2dd62SKumar Gala 	static struct pci_bus bus;
158389c2dd62SKumar Gala 
158489c2dd62SKumar Gala 	if (hose == 0) {
158589c2dd62SKumar Gala 		printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
158689c2dd62SKumar Gala 	}
158789c2dd62SKumar Gala 	bus.number = busnr;
158889c2dd62SKumar Gala 	bus.sysdata = hose;
158989c2dd62SKumar Gala 	bus.ops = hose? hose->ops: &null_pci_ops;
159089c2dd62SKumar Gala 	return &bus;
159189c2dd62SKumar Gala }
159289c2dd62SKumar Gala 
159389c2dd62SKumar Gala #define EARLY_PCI_OP(rw, size, type)					\
159489c2dd62SKumar Gala int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
159589c2dd62SKumar Gala 			       int devfn, int offset, type value)	\
159689c2dd62SKumar Gala {									\
159789c2dd62SKumar Gala 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
159889c2dd62SKumar Gala 					    devfn, offset, value);	\
159989c2dd62SKumar Gala }
160089c2dd62SKumar Gala 
160189c2dd62SKumar Gala EARLY_PCI_OP(read, byte, u8 *)
160289c2dd62SKumar Gala EARLY_PCI_OP(read, word, u16 *)
160389c2dd62SKumar Gala EARLY_PCI_OP(read, dword, u32 *)
160489c2dd62SKumar Gala EARLY_PCI_OP(write, byte, u8)
160589c2dd62SKumar Gala EARLY_PCI_OP(write, word, u16)
160689c2dd62SKumar Gala EARLY_PCI_OP(write, dword, u32)
160789c2dd62SKumar Gala 
160889c2dd62SKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
160989c2dd62SKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
161089c2dd62SKumar Gala 			  int cap)
161189c2dd62SKumar Gala {
161289c2dd62SKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
161389c2dd62SKumar Gala }
16140ed2c722SGrant Likely 
161598d9f30cSBenjamin Herrenschmidt struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
161698d9f30cSBenjamin Herrenschmidt {
161798d9f30cSBenjamin Herrenschmidt 	struct pci_controller *hose = bus->sysdata;
161898d9f30cSBenjamin Herrenschmidt 
161998d9f30cSBenjamin Herrenschmidt 	return of_node_get(hose->dn);
162098d9f30cSBenjamin Herrenschmidt }
162198d9f30cSBenjamin Herrenschmidt 
16220ed2c722SGrant Likely /**
16230ed2c722SGrant Likely  * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
16240ed2c722SGrant Likely  * @hose: Pointer to the PCI host controller instance structure
16250ed2c722SGrant Likely  */
1626b5d937deSGrant Likely void __devinit pcibios_scan_phb(struct pci_controller *hose)
16270ed2c722SGrant Likely {
162845a709f8SBjorn Helgaas 	LIST_HEAD(resources);
16290ed2c722SGrant Likely 	struct pci_bus *bus;
16300ed2c722SGrant Likely 	struct device_node *node = hose->dn;
16310ed2c722SGrant Likely 	int mode;
16320ed2c722SGrant Likely 
163374a7f084SGrant Likely 	pr_debug("PCI: Scanning PHB %s\n", of_node_full_name(node));
16340ed2c722SGrant Likely 
16350ed2c722SGrant Likely 	/* Get some IO space for the new PHB */
16360ed2c722SGrant Likely 	pcibios_setup_phb_io_space(hose);
16370ed2c722SGrant Likely 
16380ed2c722SGrant Likely 	/* Wire up PHB bus resources */
163945a709f8SBjorn Helgaas 	pcibios_setup_phb_resources(hose, &resources);
164045a709f8SBjorn Helgaas 
1641be8e60d8SYinghai Lu 	hose->busn.start = hose->first_busno;
1642be8e60d8SYinghai Lu 	hose->busn.end	 = hose->last_busno;
1643be8e60d8SYinghai Lu 	hose->busn.flags = IORESOURCE_BUS;
1644be8e60d8SYinghai Lu 	pci_add_resource(&resources, &hose->busn);
1645be8e60d8SYinghai Lu 
164645a709f8SBjorn Helgaas 	/* Create an empty bus for the toplevel */
164745a709f8SBjorn Helgaas 	bus = pci_create_root_bus(hose->parent, hose->first_busno,
164845a709f8SBjorn Helgaas 				  hose->ops, hose, &resources);
164945a709f8SBjorn Helgaas 	if (bus == NULL) {
165045a709f8SBjorn Helgaas 		pr_err("Failed to create bus for PCI domain %04x\n",
165145a709f8SBjorn Helgaas 			hose->global_number);
165245a709f8SBjorn Helgaas 		pci_free_resource_list(&resources);
165345a709f8SBjorn Helgaas 		return;
165445a709f8SBjorn Helgaas 	}
165545a709f8SBjorn Helgaas 	hose->bus = bus;
16560ed2c722SGrant Likely 
16570ed2c722SGrant Likely 	/* Get probe mode and perform scan */
16580ed2c722SGrant Likely 	mode = PCI_PROBE_NORMAL;
16590ed2c722SGrant Likely 	if (node && ppc_md.pci_probe_mode)
16600ed2c722SGrant Likely 		mode = ppc_md.pci_probe_mode(bus);
16610ed2c722SGrant Likely 	pr_debug("    probe mode: %d\n", mode);
1662be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_DEVTREE)
16630ed2c722SGrant Likely 		of_scan_bus(node, bus);
16640ed2c722SGrant Likely 
1665be8e60d8SYinghai Lu 	if (mode == PCI_PROBE_NORMAL) {
1666be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, 255);
1667be8e60d8SYinghai Lu 		hose->last_busno = pci_scan_child_bus(bus);
1668be8e60d8SYinghai Lu 		pci_bus_update_busn_res_end(bus, hose->last_busno);
1669be8e60d8SYinghai Lu 	}
1670781fb7a3SBenjamin Herrenschmidt 
1671491b98c3SBenjamin Herrenschmidt 	/* Platform gets a chance to do some global fixups before
1672491b98c3SBenjamin Herrenschmidt 	 * we proceed to resource allocation
1673491b98c3SBenjamin Herrenschmidt 	 */
1674491b98c3SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_phb)
1675491b98c3SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_phb(hose);
1676491b98c3SBenjamin Herrenschmidt 
1677781fb7a3SBenjamin Herrenschmidt 	/* Configure PCI Express settings */
1678bb36c445SBenjamin Herrenschmidt 	if (bus && !pci_has_flag(PCI_PROBE_ONLY)) {
1679781fb7a3SBenjamin Herrenschmidt 		struct pci_bus *child;
1680781fb7a3SBenjamin Herrenschmidt 		list_for_each_entry(child, &bus->children, node) {
1681781fb7a3SBenjamin Herrenschmidt 			struct pci_dev *self = child->self;
1682781fb7a3SBenjamin Herrenschmidt 			if (!self)
1683781fb7a3SBenjamin Herrenschmidt 				continue;
1684781fb7a3SBenjamin Herrenschmidt 			pcie_bus_configure_settings(child, self->pcie_mpss);
1685781fb7a3SBenjamin Herrenschmidt 		}
1686781fb7a3SBenjamin Herrenschmidt 	}
16870ed2c722SGrant Likely }
1688c065488fSKumar Gala 
1689c065488fSKumar Gala static void fixup_hide_host_resource_fsl(struct pci_dev *dev)
1690c065488fSKumar Gala {
1691c065488fSKumar Gala 	int i, class = dev->class >> 8;
169205737c7cSJason Jin 	/* When configured as agent, programing interface = 1 */
169305737c7cSJason Jin 	int prog_if = dev->class & 0xf;
1694c065488fSKumar Gala 
1695c065488fSKumar Gala 	if ((class == PCI_CLASS_PROCESSOR_POWERPC ||
1696c065488fSKumar Gala 	     class == PCI_CLASS_BRIDGE_OTHER) &&
1697c065488fSKumar Gala 		(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
169805737c7cSJason Jin 		(prog_if == 0) &&
1699c065488fSKumar Gala 		(dev->bus->parent == NULL)) {
1700c065488fSKumar Gala 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1701c065488fSKumar Gala 			dev->resource[i].start = 0;
1702c065488fSKumar Gala 			dev->resource[i].end = 0;
1703c065488fSKumar Gala 			dev->resource[i].flags = 0;
1704c065488fSKumar Gala 		}
1705c065488fSKumar Gala 	}
1706c065488fSKumar Gala }
1707c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1708c065488fSKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
1709