xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
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>
245516b540SKumar Gala #include <linux/mm.h>
255516b540SKumar Gala #include <linux/list.h>
265516b540SKumar Gala #include <linux/syscalls.h>
275516b540SKumar Gala #include <linux/irq.h>
285516b540SKumar Gala #include <linux/vmalloc.h>
29*5a0e3ad6STejun Heo #include <linux/slab.h>
305516b540SKumar Gala 
315516b540SKumar Gala #include <asm/processor.h>
325516b540SKumar Gala #include <asm/io.h>
335516b540SKumar Gala #include <asm/prom.h>
345516b540SKumar Gala #include <asm/pci-bridge.h>
355516b540SKumar Gala #include <asm/byteorder.h>
365516b540SKumar Gala #include <asm/machdep.h>
375516b540SKumar Gala #include <asm/ppc-pci.h>
385516b540SKumar Gala #include <asm/firmware.h>
398b8da358SBenjamin Herrenschmidt #include <asm/eeh.h>
405516b540SKumar Gala 
41a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
42c3bd517dSMilton Miller LIST_HEAD(hose_list);
43a4c9e328SKumar Gala 
44a4c9e328SKumar Gala /* XXX kill that some day ... */
45ebfc00f7SStephen Rothwell static int global_phb_number;		/* Global phb counter */
46a4c9e328SKumar Gala 
4725e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */
4825e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base;
4925e81f92SBenjamin Herrenschmidt 
501fd0f525SBenjamin Herrenschmidt /* Default PCI flags is 0 on ppc32, modified at boot on ppc64 */
511fd0f525SBenjamin Herrenschmidt unsigned int ppc_pci_flags = 0;
521fd0f525SBenjamin Herrenschmidt 
53a4c9e328SKumar Gala 
5445223c54SFUJITA Tomonori static struct dma_map_ops *pci_dma_ops = &dma_direct_ops;
554fc665b8SBecky Bruce 
5645223c54SFUJITA Tomonori void set_pci_dma_ops(struct dma_map_ops *dma_ops)
574fc665b8SBecky Bruce {
584fc665b8SBecky Bruce 	pci_dma_ops = dma_ops;
594fc665b8SBecky Bruce }
604fc665b8SBecky Bruce 
6145223c54SFUJITA Tomonori struct dma_map_ops *get_pci_dma_ops(void)
624fc665b8SBecky Bruce {
634fc665b8SBecky Bruce 	return pci_dma_ops;
644fc665b8SBecky Bruce }
654fc665b8SBecky Bruce EXPORT_SYMBOL(get_pci_dma_ops);
664fc665b8SBecky Bruce 
672d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
68a4c9e328SKumar Gala {
69a4c9e328SKumar Gala 	struct pci_controller *phb;
70a4c9e328SKumar Gala 
71e60516e3SStephen Rothwell 	phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
72a4c9e328SKumar Gala 	if (phb == NULL)
73a4c9e328SKumar Gala 		return NULL;
74e60516e3SStephen Rothwell 	spin_lock(&hose_spinlock);
75e60516e3SStephen Rothwell 	phb->global_number = global_phb_number++;
76e60516e3SStephen Rothwell 	list_add_tail(&phb->list_node, &hose_list);
77e60516e3SStephen Rothwell 	spin_unlock(&hose_spinlock);
7844ef3390SStephen Rothwell 	phb->dn = dev;
79a4c9e328SKumar Gala 	phb->is_dynamic = mem_init_done;
80a4c9e328SKumar Gala #ifdef CONFIG_PPC64
81a4c9e328SKumar Gala 	if (dev) {
82a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
83a4c9e328SKumar Gala 
84a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
85a4c9e328SKumar Gala 			nid = -1;
86a4c9e328SKumar Gala 
87a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
88a4c9e328SKumar Gala 	}
89a4c9e328SKumar Gala #endif
90a4c9e328SKumar Gala 	return phb;
91a4c9e328SKumar Gala }
92a4c9e328SKumar Gala 
93a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
94a4c9e328SKumar Gala {
95a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
96a4c9e328SKumar Gala 	list_del(&phb->list_node);
97a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
98a4c9e328SKumar Gala 
99a4c9e328SKumar Gala 	if (phb->is_dynamic)
100a4c9e328SKumar Gala 		kfree(phb);
101a4c9e328SKumar Gala }
102a4c9e328SKumar Gala 
103c3bd517dSMilton Miller static resource_size_t pcibios_io_size(const struct pci_controller *hose)
104c3bd517dSMilton Miller {
105c3bd517dSMilton Miller #ifdef CONFIG_PPC64
106c3bd517dSMilton Miller 	return hose->pci_io_size;
107c3bd517dSMilton Miller #else
108c3bd517dSMilton Miller 	return hose->io_resource.end - hose->io_resource.start + 1;
109c3bd517dSMilton Miller #endif
110c3bd517dSMilton Miller }
111c3bd517dSMilton Miller 
1126dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
1136dfbde20SBenjamin Herrenschmidt {
1146dfbde20SBenjamin Herrenschmidt 	int ret = 0;
1156dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
116c3bd517dSMilton Miller 	resource_size_t size;
1176dfbde20SBenjamin Herrenschmidt 
1186dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
1196dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
120c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
1216dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
1226dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
1236dfbde20SBenjamin Herrenschmidt 			ret = 1;
1246dfbde20SBenjamin Herrenschmidt 			break;
1256dfbde20SBenjamin Herrenschmidt 		}
1266dfbde20SBenjamin Herrenschmidt 	}
1276dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
1286dfbde20SBenjamin Herrenschmidt 	return ret;
1296dfbde20SBenjamin Herrenschmidt }
1306dfbde20SBenjamin Herrenschmidt 
131c3bd517dSMilton Miller unsigned long pci_address_to_pio(phys_addr_t address)
132c3bd517dSMilton Miller {
133c3bd517dSMilton Miller 	struct pci_controller *hose;
134c3bd517dSMilton Miller 	resource_size_t size;
135c3bd517dSMilton Miller 	unsigned long ret = ~0;
136c3bd517dSMilton Miller 
137c3bd517dSMilton Miller 	spin_lock(&hose_spinlock);
138c3bd517dSMilton Miller 	list_for_each_entry(hose, &hose_list, list_node) {
139c3bd517dSMilton Miller 		size = pcibios_io_size(hose);
140c3bd517dSMilton Miller 		if (address >= hose->io_base_phys &&
141c3bd517dSMilton Miller 		    address < (hose->io_base_phys + size)) {
142c3bd517dSMilton Miller 			unsigned long base =
143c3bd517dSMilton Miller 				(unsigned long)hose->io_base_virt - _IO_BASE;
144c3bd517dSMilton Miller 			ret = base + (address - hose->io_base_phys);
145c3bd517dSMilton Miller 			break;
146c3bd517dSMilton Miller 		}
147c3bd517dSMilton Miller 	}
148c3bd517dSMilton Miller 	spin_unlock(&hose_spinlock);
149c3bd517dSMilton Miller 
150c3bd517dSMilton Miller 	return ret;
151c3bd517dSMilton Miller }
152c3bd517dSMilton Miller EXPORT_SYMBOL_GPL(pci_address_to_pio);
153c3bd517dSMilton Miller 
1545516b540SKumar Gala /*
1555516b540SKumar Gala  * Return the domain number for this bus.
1565516b540SKumar Gala  */
1575516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
1585516b540SKumar Gala {
1595516b540SKumar Gala 	struct pci_controller *hose = pci_bus_to_host(bus);
1605516b540SKumar Gala 
1615516b540SKumar Gala 	return hose->global_number;
1625516b540SKumar Gala }
1635516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
16458083dadSKumar Gala 
165a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
166a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
167a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
168a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
169a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
170a4c9e328SKumar Gala  * config cycles.
171a4c9e328SKumar Gala  */
172a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
173a4c9e328SKumar Gala {
174a4c9e328SKumar Gala 	while(node) {
175a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
176a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
17744ef3390SStephen Rothwell 			if (hose->dn == node)
178a4c9e328SKumar Gala 				return hose;
179a4c9e328SKumar Gala 		node = node->parent;
180a4c9e328SKumar Gala 	}
181a4c9e328SKumar Gala 	return NULL;
182a4c9e328SKumar Gala }
183a4c9e328SKumar Gala 
18458083dadSKumar Gala static ssize_t pci_show_devspec(struct device *dev,
18558083dadSKumar Gala 		struct device_attribute *attr, char *buf)
18658083dadSKumar Gala {
18758083dadSKumar Gala 	struct pci_dev *pdev;
18858083dadSKumar Gala 	struct device_node *np;
18958083dadSKumar Gala 
19058083dadSKumar Gala 	pdev = to_pci_dev (dev);
19158083dadSKumar Gala 	np = pci_device_to_OF_node(pdev);
19258083dadSKumar Gala 	if (np == NULL || np->full_name == NULL)
19358083dadSKumar Gala 		return 0;
19458083dadSKumar Gala 	return sprintf(buf, "%s", np->full_name);
19558083dadSKumar Gala }
19658083dadSKumar Gala static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
19758083dadSKumar Gala 
19858083dadSKumar Gala /* Add sysfs properties */
1994f3731daSTony Breeds int pcibios_add_platform_entries(struct pci_dev *pdev)
20058083dadSKumar Gala {
2014f3731daSTony Breeds 	return device_create_file(&pdev->dev, &dev_attr_devspec);
20258083dadSKumar Gala }
20358083dadSKumar Gala 
204a2b7390aSStephen Rothwell char __devinit *pcibios_setup(char *str)
20558083dadSKumar Gala {
20658083dadSKumar Gala 	return str;
20758083dadSKumar Gala }
20858083dadSKumar Gala 
20958083dadSKumar Gala /*
21058083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
21158083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
21258083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
21358083dadSKumar Gala  */
21458083dadSKumar Gala int pci_read_irq_line(struct pci_dev *pci_dev)
21558083dadSKumar Gala {
21658083dadSKumar Gala 	struct of_irq oirq;
21758083dadSKumar Gala 	unsigned int virq;
21858083dadSKumar Gala 
21950c9bc2fSBenjamin Herrenschmidt 	/* The current device-tree that iSeries generates from the HV
22050c9bc2fSBenjamin Herrenschmidt 	 * PCI informations doesn't contain proper interrupt routing,
22150c9bc2fSBenjamin Herrenschmidt 	 * and all the fallback would do is print out crap, so we
22250c9bc2fSBenjamin Herrenschmidt 	 * don't attempt to resolve the interrupts here at all, some
22350c9bc2fSBenjamin Herrenschmidt 	 * iSeries specific fixup does it.
22450c9bc2fSBenjamin Herrenschmidt 	 *
22550c9bc2fSBenjamin Herrenschmidt 	 * In the long run, we will hopefully fix the generated device-tree
22650c9bc2fSBenjamin Herrenschmidt 	 * instead.
22750c9bc2fSBenjamin Herrenschmidt 	 */
22850c9bc2fSBenjamin Herrenschmidt #ifdef CONFIG_PPC_ISERIES
22950c9bc2fSBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_ISERIES))
23050c9bc2fSBenjamin Herrenschmidt 		return -1;
23150c9bc2fSBenjamin Herrenschmidt #endif
23250c9bc2fSBenjamin Herrenschmidt 
233b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Try to map irq for %s...\n", pci_name(pci_dev));
23458083dadSKumar Gala 
23558083dadSKumar Gala #ifdef DEBUG
23658083dadSKumar Gala 	memset(&oirq, 0xff, sizeof(oirq));
23758083dadSKumar Gala #endif
23858083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
23958083dadSKumar Gala 	if (of_irq_map_pci(pci_dev, &oirq)) {
24058083dadSKumar Gala 		u8 line, pin;
24158083dadSKumar Gala 
24258083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
24358083dadSKumar Gala 		 * space and map that through the default controller. We
24458083dadSKumar Gala 		 * also set the type to level low since that's what PCI
24558083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
24658083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
24758083dadSKumar Gala 		 * function.
24858083dadSKumar Gala 		 */
24958083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
25058083dadSKumar Gala 			return -1;
25158083dadSKumar Gala 		if (pin == 0)
25258083dadSKumar Gala 			return -1;
25358083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
25454a24cbbSBenjamin Herrenschmidt 		    line == 0xff || line == 0) {
25558083dadSKumar Gala 			return -1;
25658083dadSKumar Gala 		}
257b0494bc8SBenjamin Herrenschmidt 		pr_debug(" No map ! Using line %d (pin %d) from PCI config\n",
25854a24cbbSBenjamin Herrenschmidt 			 line, pin);
25958083dadSKumar Gala 
26058083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
26158083dadSKumar Gala 		if (virq != NO_IRQ)
26258083dadSKumar Gala 			set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
26358083dadSKumar Gala 	} else {
264b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
26558083dadSKumar Gala 			 oirq.size, oirq.specifier[0], oirq.specifier[1],
26659b608c2SBenjamin Herrenschmidt 			 oirq.controller ? oirq.controller->full_name :
26759b608c2SBenjamin Herrenschmidt 			 "<default>");
26858083dadSKumar Gala 
26958083dadSKumar Gala 		virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
27058083dadSKumar Gala 					     oirq.size);
27158083dadSKumar Gala 	}
27258083dadSKumar Gala 	if(virq == NO_IRQ) {
273b0494bc8SBenjamin Herrenschmidt 		pr_debug(" Failed to map !\n");
27458083dadSKumar Gala 		return -1;
27558083dadSKumar Gala 	}
27658083dadSKumar Gala 
277b0494bc8SBenjamin Herrenschmidt 	pr_debug(" Mapped to linux irq %d\n", virq);
27858083dadSKumar Gala 
27958083dadSKumar Gala 	pci_dev->irq = virq;
28058083dadSKumar Gala 
28158083dadSKumar Gala 	return 0;
28258083dadSKumar Gala }
28358083dadSKumar Gala EXPORT_SYMBOL(pci_read_irq_line);
28458083dadSKumar Gala 
28558083dadSKumar Gala /*
28658083dadSKumar Gala  * Platform support for /proc/bus/pci/X/Y mmap()s,
28758083dadSKumar Gala  * modelled on the sparc64 implementation by Dave Miller.
28858083dadSKumar Gala  *  -- paulus.
28958083dadSKumar Gala  */
29058083dadSKumar Gala 
29158083dadSKumar Gala /*
29258083dadSKumar Gala  * Adjust vm_pgoff of VMA such that it is the physical page offset
29358083dadSKumar Gala  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
29458083dadSKumar Gala  *
29558083dadSKumar Gala  * Basically, the user finds the base address for his device which he wishes
29658083dadSKumar Gala  * to mmap.  They read the 32-bit value from the config space base register,
29758083dadSKumar Gala  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
29858083dadSKumar Gala  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
29958083dadSKumar Gala  *
30058083dadSKumar Gala  * Returns negative error code on failure, zero on success.
30158083dadSKumar Gala  */
30258083dadSKumar Gala static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
30358083dadSKumar Gala 					       resource_size_t *offset,
30458083dadSKumar Gala 					       enum pci_mmap_state mmap_state)
30558083dadSKumar Gala {
30658083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
30758083dadSKumar Gala 	unsigned long io_offset = 0;
30858083dadSKumar Gala 	int i, res_bit;
30958083dadSKumar Gala 
31058083dadSKumar Gala 	if (hose == 0)
31158083dadSKumar Gala 		return NULL;		/* should never happen */
31258083dadSKumar Gala 
31358083dadSKumar Gala 	/* If memory, add on the PCI bridge address offset */
31458083dadSKumar Gala 	if (mmap_state == pci_mmap_mem) {
31558083dadSKumar Gala #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
31658083dadSKumar Gala 		*offset += hose->pci_mem_offset;
31758083dadSKumar Gala #endif
31858083dadSKumar Gala 		res_bit = IORESOURCE_MEM;
31958083dadSKumar Gala 	} else {
32058083dadSKumar Gala 		io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
32158083dadSKumar Gala 		*offset += io_offset;
32258083dadSKumar Gala 		res_bit = IORESOURCE_IO;
32358083dadSKumar Gala 	}
32458083dadSKumar Gala 
32558083dadSKumar Gala 	/*
32658083dadSKumar Gala 	 * Check that the offset requested corresponds to one of the
32758083dadSKumar Gala 	 * resources of the device.
32858083dadSKumar Gala 	 */
32958083dadSKumar Gala 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
33058083dadSKumar Gala 		struct resource *rp = &dev->resource[i];
33158083dadSKumar Gala 		int flags = rp->flags;
33258083dadSKumar Gala 
33358083dadSKumar Gala 		/* treat ROM as memory (should be already) */
33458083dadSKumar Gala 		if (i == PCI_ROM_RESOURCE)
33558083dadSKumar Gala 			flags |= IORESOURCE_MEM;
33658083dadSKumar Gala 
33758083dadSKumar Gala 		/* Active and same type? */
33858083dadSKumar Gala 		if ((flags & res_bit) == 0)
33958083dadSKumar Gala 			continue;
34058083dadSKumar Gala 
34158083dadSKumar Gala 		/* In the range of this resource? */
34258083dadSKumar Gala 		if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
34358083dadSKumar Gala 			continue;
34458083dadSKumar Gala 
34558083dadSKumar Gala 		/* found it! construct the final physical address */
34658083dadSKumar Gala 		if (mmap_state == pci_mmap_io)
34758083dadSKumar Gala 			*offset += hose->io_base_phys - io_offset;
34858083dadSKumar Gala 		return rp;
34958083dadSKumar Gala 	}
35058083dadSKumar Gala 
35158083dadSKumar Gala 	return NULL;
35258083dadSKumar Gala }
35358083dadSKumar Gala 
35458083dadSKumar Gala /*
35558083dadSKumar Gala  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
35658083dadSKumar Gala  * device mapping.
35758083dadSKumar Gala  */
35858083dadSKumar Gala static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
35958083dadSKumar Gala 				      pgprot_t protection,
36058083dadSKumar Gala 				      enum pci_mmap_state mmap_state,
36158083dadSKumar Gala 				      int write_combine)
36258083dadSKumar Gala {
36358083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
36458083dadSKumar Gala 
36558083dadSKumar Gala 	/* Write combine is always 0 on non-memory space mappings. On
36658083dadSKumar Gala 	 * memory space, if the user didn't pass 1, we check for a
36758083dadSKumar Gala 	 * "prefetchable" resource. This is a bit hackish, but we use
36858083dadSKumar Gala 	 * this to workaround the inability of /sysfs to provide a write
36958083dadSKumar Gala 	 * combine bit
37058083dadSKumar Gala 	 */
37158083dadSKumar Gala 	if (mmap_state != pci_mmap_mem)
37258083dadSKumar Gala 		write_combine = 0;
37358083dadSKumar Gala 	else if (write_combine == 0) {
37458083dadSKumar Gala 		if (rp->flags & IORESOURCE_PREFETCH)
37558083dadSKumar Gala 			write_combine = 1;
37658083dadSKumar Gala 	}
37758083dadSKumar Gala 
37858083dadSKumar Gala 	/* XXX would be nice to have a way to ask for write-through */
37958083dadSKumar Gala 	if (write_combine)
38064b3d0e8SBenjamin Herrenschmidt 		return pgprot_noncached_wc(prot);
38158083dadSKumar Gala 	else
38264b3d0e8SBenjamin Herrenschmidt 		return pgprot_noncached(prot);
38358083dadSKumar Gala }
38458083dadSKumar Gala 
38558083dadSKumar Gala /*
38658083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
38758083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
38858083dadSKumar Gala  * above routine
38958083dadSKumar Gala  */
39058083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
39158083dadSKumar Gala 				  unsigned long pfn,
39258083dadSKumar Gala 				  unsigned long size,
39364b3d0e8SBenjamin Herrenschmidt 				  pgprot_t prot)
39458083dadSKumar Gala {
39558083dadSKumar Gala 	struct pci_dev *pdev = NULL;
39658083dadSKumar Gala 	struct resource *found = NULL;
3977c12d906SBenjamin Herrenschmidt 	resource_size_t offset = ((resource_size_t)pfn) << PAGE_SHIFT;
39858083dadSKumar Gala 	int i;
39958083dadSKumar Gala 
40058083dadSKumar Gala 	if (page_is_ram(pfn))
40164b3d0e8SBenjamin Herrenschmidt 		return prot;
40258083dadSKumar Gala 
40364b3d0e8SBenjamin Herrenschmidt 	prot = pgprot_noncached(prot);
40458083dadSKumar Gala 	for_each_pci_dev(pdev) {
40558083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
40658083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
40758083dadSKumar Gala 			int flags = rp->flags;
40858083dadSKumar Gala 
40958083dadSKumar Gala 			/* Active and same type? */
41058083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
41158083dadSKumar Gala 				continue;
41258083dadSKumar Gala 			/* In the range of this resource? */
41358083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
41458083dadSKumar Gala 			    offset > rp->end)
41558083dadSKumar Gala 				continue;
41658083dadSKumar Gala 			found = rp;
41758083dadSKumar Gala 			break;
41858083dadSKumar Gala 		}
41958083dadSKumar Gala 		if (found)
42058083dadSKumar Gala 			break;
42158083dadSKumar Gala 	}
42258083dadSKumar Gala 	if (found) {
42358083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
42464b3d0e8SBenjamin Herrenschmidt 			prot = pgprot_noncached_wc(prot);
42558083dadSKumar Gala 		pci_dev_put(pdev);
42658083dadSKumar Gala 	}
42758083dadSKumar Gala 
428b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Non-PCI map for %llx, prot: %lx\n",
42964b3d0e8SBenjamin Herrenschmidt 		 (unsigned long long)offset, pgprot_val(prot));
43058083dadSKumar Gala 
43164b3d0e8SBenjamin Herrenschmidt 	return prot;
43258083dadSKumar Gala }
43358083dadSKumar Gala 
43458083dadSKumar Gala 
43558083dadSKumar Gala /*
43658083dadSKumar Gala  * Perform the actual remap of the pages for a PCI device mapping, as
43758083dadSKumar Gala  * appropriate for this architecture.  The region in the process to map
43858083dadSKumar Gala  * is described by vm_start and vm_end members of VMA, the base physical
43958083dadSKumar Gala  * address is found in vm_pgoff.
44058083dadSKumar Gala  * The pci device structure is provided so that architectures may make mapping
44158083dadSKumar Gala  * decisions on a per-device or per-bus basis.
44258083dadSKumar Gala  *
44358083dadSKumar Gala  * Returns a negative error code on failure, zero on success.
44458083dadSKumar Gala  */
44558083dadSKumar Gala int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
44658083dadSKumar Gala 			enum pci_mmap_state mmap_state, int write_combine)
44758083dadSKumar Gala {
4487c12d906SBenjamin Herrenschmidt 	resource_size_t offset =
4497c12d906SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
45058083dadSKumar Gala 	struct resource *rp;
45158083dadSKumar Gala 	int ret;
45258083dadSKumar Gala 
45358083dadSKumar Gala 	rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
45458083dadSKumar Gala 	if (rp == NULL)
45558083dadSKumar Gala 		return -EINVAL;
45658083dadSKumar Gala 
45758083dadSKumar Gala 	vma->vm_pgoff = offset >> PAGE_SHIFT;
45858083dadSKumar Gala 	vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
45958083dadSKumar Gala 						  vma->vm_page_prot,
46058083dadSKumar Gala 						  mmap_state, write_combine);
46158083dadSKumar Gala 
46258083dadSKumar Gala 	ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
46358083dadSKumar Gala 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
46458083dadSKumar Gala 
46558083dadSKumar Gala 	return ret;
46658083dadSKumar Gala }
46758083dadSKumar Gala 
468e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO read access on a bus */
469e9f82cb7SBenjamin Herrenschmidt int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
470e9f82cb7SBenjamin Herrenschmidt {
471e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
472e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
473e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
474e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
475e9f82cb7SBenjamin Herrenschmidt 
476e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
477e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
478e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
479e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
480e9f82cb7SBenjamin Herrenschmidt 	 */
481e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
482e9f82cb7SBenjamin Herrenschmidt 	offset += port;
483e9f82cb7SBenjamin Herrenschmidt 
484e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
485e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
486e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
487e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
488e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
489e9f82cb7SBenjamin Herrenschmidt 
490e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
491e9f82cb7SBenjamin Herrenschmidt 	case 1:
492e9f82cb7SBenjamin Herrenschmidt 		*((u8 *)val) = in_8(addr);
493e9f82cb7SBenjamin Herrenschmidt 		return 1;
494e9f82cb7SBenjamin Herrenschmidt 	case 2:
495e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
496e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
497e9f82cb7SBenjamin Herrenschmidt 		*((u16 *)val) = in_le16(addr);
498e9f82cb7SBenjamin Herrenschmidt 		return 2;
499e9f82cb7SBenjamin Herrenschmidt 	case 4:
500e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
501e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
502e9f82cb7SBenjamin Herrenschmidt 		*((u32 *)val) = in_le32(addr);
503e9f82cb7SBenjamin Herrenschmidt 		return 4;
504e9f82cb7SBenjamin Herrenschmidt 	}
505e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
506e9f82cb7SBenjamin Herrenschmidt }
507e9f82cb7SBenjamin Herrenschmidt 
508e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO write access on a bus */
509e9f82cb7SBenjamin Herrenschmidt int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val, size_t size)
510e9f82cb7SBenjamin Herrenschmidt {
511e9f82cb7SBenjamin Herrenschmidt 	unsigned long offset;
512e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
513e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp = &hose->io_resource;
514e9f82cb7SBenjamin Herrenschmidt 	void __iomem *addr;
515e9f82cb7SBenjamin Herrenschmidt 
516e9f82cb7SBenjamin Herrenschmidt 	/* Check if port can be supported by that bus. We only check
517e9f82cb7SBenjamin Herrenschmidt 	 * the ranges of the PHB though, not the bus itself as the rules
518e9f82cb7SBenjamin Herrenschmidt 	 * for forwarding legacy cycles down bridges are not our problem
519e9f82cb7SBenjamin Herrenschmidt 	 * here. So if the host bridge supports it, we do it.
520e9f82cb7SBenjamin Herrenschmidt 	 */
521e9f82cb7SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
522e9f82cb7SBenjamin Herrenschmidt 	offset += port;
523e9f82cb7SBenjamin Herrenschmidt 
524e9f82cb7SBenjamin Herrenschmidt 	if (!(rp->flags & IORESOURCE_IO))
525e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
526e9f82cb7SBenjamin Herrenschmidt 	if (offset < rp->start || (offset + size) > rp->end)
527e9f82cb7SBenjamin Herrenschmidt 		return -ENXIO;
528e9f82cb7SBenjamin Herrenschmidt 	addr = hose->io_base_virt + port;
529e9f82cb7SBenjamin Herrenschmidt 
530e9f82cb7SBenjamin Herrenschmidt 	/* WARNING: The generic code is idiotic. It gets passed a pointer
531e9f82cb7SBenjamin Herrenschmidt 	 * to what can be a 1, 2 or 4 byte quantity and always reads that
532e9f82cb7SBenjamin Herrenschmidt 	 * as a u32, which means that we have to correct the location of
533e9f82cb7SBenjamin Herrenschmidt 	 * the data read within those 32 bits for size 1 and 2
534e9f82cb7SBenjamin Herrenschmidt 	 */
535e9f82cb7SBenjamin Herrenschmidt 	switch(size) {
536e9f82cb7SBenjamin Herrenschmidt 	case 1:
537e9f82cb7SBenjamin Herrenschmidt 		out_8(addr, val >> 24);
538e9f82cb7SBenjamin Herrenschmidt 		return 1;
539e9f82cb7SBenjamin Herrenschmidt 	case 2:
540e9f82cb7SBenjamin Herrenschmidt 		if (port & 1)
541e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
542e9f82cb7SBenjamin Herrenschmidt 		out_le16(addr, val >> 16);
543e9f82cb7SBenjamin Herrenschmidt 		return 2;
544e9f82cb7SBenjamin Herrenschmidt 	case 4:
545e9f82cb7SBenjamin Herrenschmidt 		if (port & 3)
546e9f82cb7SBenjamin Herrenschmidt 			return -EINVAL;
547e9f82cb7SBenjamin Herrenschmidt 		out_le32(addr, val);
548e9f82cb7SBenjamin Herrenschmidt 		return 4;
549e9f82cb7SBenjamin Herrenschmidt 	}
550e9f82cb7SBenjamin Herrenschmidt 	return -EINVAL;
551e9f82cb7SBenjamin Herrenschmidt }
552e9f82cb7SBenjamin Herrenschmidt 
553e9f82cb7SBenjamin Herrenschmidt /* This provides legacy IO or memory mmap access on a bus */
554e9f82cb7SBenjamin Herrenschmidt int pci_mmap_legacy_page_range(struct pci_bus *bus,
555e9f82cb7SBenjamin Herrenschmidt 			       struct vm_area_struct *vma,
556e9f82cb7SBenjamin Herrenschmidt 			       enum pci_mmap_state mmap_state)
557e9f82cb7SBenjamin Herrenschmidt {
558e9f82cb7SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
559e9f82cb7SBenjamin Herrenschmidt 	resource_size_t offset =
560e9f82cb7SBenjamin Herrenschmidt 		((resource_size_t)vma->vm_pgoff) << PAGE_SHIFT;
561e9f82cb7SBenjamin Herrenschmidt 	resource_size_t size = vma->vm_end - vma->vm_start;
562e9f82cb7SBenjamin Herrenschmidt 	struct resource *rp;
563e9f82cb7SBenjamin Herrenschmidt 
564e9f82cb7SBenjamin Herrenschmidt 	pr_debug("pci_mmap_legacy_page_range(%04x:%02x, %s @%llx..%llx)\n",
565e9f82cb7SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number,
566e9f82cb7SBenjamin Herrenschmidt 		 mmap_state == pci_mmap_mem ? "MEM" : "IO",
567e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)offset,
568e9f82cb7SBenjamin Herrenschmidt 		 (unsigned long long)(offset + size - 1));
569e9f82cb7SBenjamin Herrenschmidt 
570e9f82cb7SBenjamin Herrenschmidt 	if (mmap_state == pci_mmap_mem) {
5715b11abfdSBenjamin Herrenschmidt 		/* Hack alert !
5725b11abfdSBenjamin Herrenschmidt 		 *
5735b11abfdSBenjamin Herrenschmidt 		 * Because X is lame and can fail starting if it gets an error trying
5745b11abfdSBenjamin Herrenschmidt 		 * to mmap legacy_mem (instead of just moving on without legacy memory
5755b11abfdSBenjamin Herrenschmidt 		 * access) we fake it here by giving it anonymous memory, effectively
5765b11abfdSBenjamin Herrenschmidt 		 * behaving just like /dev/zero
5775b11abfdSBenjamin Herrenschmidt 		 */
5785b11abfdSBenjamin Herrenschmidt 		if ((offset + size) > hose->isa_mem_size) {
5795b11abfdSBenjamin Herrenschmidt 			printk(KERN_DEBUG
5805b11abfdSBenjamin Herrenschmidt 			       "Process %s (pid:%d) mapped non-existing PCI legacy memory for 0%04x:%02x\n",
5815b11abfdSBenjamin Herrenschmidt 			       current->comm, current->pid, pci_domain_nr(bus), bus->number);
5825b11abfdSBenjamin Herrenschmidt 			if (vma->vm_flags & VM_SHARED)
5835b11abfdSBenjamin Herrenschmidt 				return shmem_zero_setup(vma);
5845b11abfdSBenjamin Herrenschmidt 			return 0;
5855b11abfdSBenjamin Herrenschmidt 		}
586e9f82cb7SBenjamin Herrenschmidt 		offset += hose->isa_mem_phys;
587e9f82cb7SBenjamin Herrenschmidt 	} else {
588e9f82cb7SBenjamin Herrenschmidt 		unsigned long io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
589e9f82cb7SBenjamin Herrenschmidt 		unsigned long roffset = offset + io_offset;
590e9f82cb7SBenjamin Herrenschmidt 		rp = &hose->io_resource;
591e9f82cb7SBenjamin Herrenschmidt 		if (!(rp->flags & IORESOURCE_IO))
592e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
593e9f82cb7SBenjamin Herrenschmidt 		if (roffset < rp->start || (roffset + size) > rp->end)
594e9f82cb7SBenjamin Herrenschmidt 			return -ENXIO;
595e9f82cb7SBenjamin Herrenschmidt 		offset += hose->io_base_phys;
596e9f82cb7SBenjamin Herrenschmidt 	}
597e9f82cb7SBenjamin Herrenschmidt 	pr_debug(" -> mapping phys %llx\n", (unsigned long long)offset);
598e9f82cb7SBenjamin Herrenschmidt 
599e9f82cb7SBenjamin Herrenschmidt 	vma->vm_pgoff = offset >> PAGE_SHIFT;
60064b3d0e8SBenjamin Herrenschmidt 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
601e9f82cb7SBenjamin Herrenschmidt 	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
602e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_end - vma->vm_start,
603e9f82cb7SBenjamin Herrenschmidt 			       vma->vm_page_prot);
604e9f82cb7SBenjamin Herrenschmidt }
605e9f82cb7SBenjamin Herrenschmidt 
60658083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
60758083dadSKumar Gala 			  const struct resource *rsrc,
60858083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
60958083dadSKumar Gala {
61058083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
61158083dadSKumar Gala 	resource_size_t offset = 0;
61258083dadSKumar Gala 
61358083dadSKumar Gala 	if (hose == NULL)
61458083dadSKumar Gala 		return;
61558083dadSKumar Gala 
61658083dadSKumar Gala 	if (rsrc->flags & IORESOURCE_IO)
61758083dadSKumar Gala 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
61858083dadSKumar Gala 
61958083dadSKumar Gala 	/* We pass a fully fixed up address to userland for MMIO instead of
62058083dadSKumar Gala 	 * a BAR value because X is lame and expects to be able to use that
62158083dadSKumar Gala 	 * to pass to /dev/mem !
62258083dadSKumar Gala 	 *
62358083dadSKumar Gala 	 * That means that we'll have potentially 64 bits values where some
62458083dadSKumar Gala 	 * userland apps only expect 32 (like X itself since it thinks only
62558083dadSKumar Gala 	 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
62658083dadSKumar Gala 	 * 32 bits CHRPs :-(
62758083dadSKumar Gala 	 *
62858083dadSKumar Gala 	 * Hopefully, the sysfs insterface is immune to that gunk. Once X
62958083dadSKumar Gala 	 * has been fixed (and the fix spread enough), we can re-enable the
63058083dadSKumar Gala 	 * 2 lines below and pass down a BAR value to userland. In that case
63158083dadSKumar Gala 	 * we'll also have to re-enable the matching code in
63258083dadSKumar Gala 	 * __pci_mmap_make_offset().
63358083dadSKumar Gala 	 *
63458083dadSKumar Gala 	 * BenH.
63558083dadSKumar Gala 	 */
63658083dadSKumar Gala #if 0
63758083dadSKumar Gala 	else if (rsrc->flags & IORESOURCE_MEM)
63858083dadSKumar Gala 		offset = hose->pci_mem_offset;
63958083dadSKumar Gala #endif
64058083dadSKumar Gala 
64158083dadSKumar Gala 	*start = rsrc->start - offset;
64258083dadSKumar Gala 	*end = rsrc->end - offset;
64358083dadSKumar Gala }
64413dccb9eSBenjamin Herrenschmidt 
64513dccb9eSBenjamin Herrenschmidt /**
64613dccb9eSBenjamin Herrenschmidt  * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
64713dccb9eSBenjamin Herrenschmidt  * @hose: newly allocated pci_controller to be setup
64813dccb9eSBenjamin Herrenschmidt  * @dev: device node of the host bridge
64913dccb9eSBenjamin Herrenschmidt  * @primary: set if primary bus (32 bits only, soon to be deprecated)
65013dccb9eSBenjamin Herrenschmidt  *
65113dccb9eSBenjamin Herrenschmidt  * This function will parse the "ranges" property of a PCI host bridge device
65213dccb9eSBenjamin Herrenschmidt  * node and setup the resource mapping of a pci controller based on its
65313dccb9eSBenjamin Herrenschmidt  * content.
65413dccb9eSBenjamin Herrenschmidt  *
65513dccb9eSBenjamin Herrenschmidt  * Life would be boring if it wasn't for a few issues that we have to deal
65613dccb9eSBenjamin Herrenschmidt  * with here:
65713dccb9eSBenjamin Herrenschmidt  *
65813dccb9eSBenjamin Herrenschmidt  *   - We can only cope with one IO space range and up to 3 Memory space
65913dccb9eSBenjamin Herrenschmidt  *     ranges. However, some machines (thanks Apple !) tend to split their
66013dccb9eSBenjamin Herrenschmidt  *     space into lots of small contiguous ranges. So we have to coalesce.
66113dccb9eSBenjamin Herrenschmidt  *
66213dccb9eSBenjamin Herrenschmidt  *   - We can only cope with all memory ranges having the same offset
66313dccb9eSBenjamin Herrenschmidt  *     between CPU addresses and PCI addresses. Unfortunately, some bridges
66413dccb9eSBenjamin Herrenschmidt  *     are setup for a large 1:1 mapping along with a small "window" which
66513dccb9eSBenjamin Herrenschmidt  *     maps PCI address 0 to some arbitrary high address of the CPU space in
66613dccb9eSBenjamin Herrenschmidt  *     order to give access to the ISA memory hole.
66713dccb9eSBenjamin Herrenschmidt  *     The way out of here that I've chosen for now is to always set the
66813dccb9eSBenjamin Herrenschmidt  *     offset based on the first resource found, then override it if we
66913dccb9eSBenjamin Herrenschmidt  *     have a different offset and the previous was set by an ISA hole.
67013dccb9eSBenjamin Herrenschmidt  *
67113dccb9eSBenjamin Herrenschmidt  *   - Some busses have IO space not starting at 0, which causes trouble with
67213dccb9eSBenjamin Herrenschmidt  *     the way we do our IO resource renumbering. The code somewhat deals with
67313dccb9eSBenjamin Herrenschmidt  *     it for 64 bits but I would expect problems on 32 bits.
67413dccb9eSBenjamin Herrenschmidt  *
67513dccb9eSBenjamin Herrenschmidt  *   - Some 32 bits platforms such as 4xx can have physical space larger than
67613dccb9eSBenjamin Herrenschmidt  *     32 bits so we need to use 64 bits values for the parsing
67713dccb9eSBenjamin Herrenschmidt  */
67813dccb9eSBenjamin Herrenschmidt void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
67913dccb9eSBenjamin Herrenschmidt 					    struct device_node *dev,
68013dccb9eSBenjamin Herrenschmidt 					    int primary)
68113dccb9eSBenjamin Herrenschmidt {
68213dccb9eSBenjamin Herrenschmidt 	const u32 *ranges;
68313dccb9eSBenjamin Herrenschmidt 	int rlen;
68413dccb9eSBenjamin Herrenschmidt 	int pna = of_n_addr_cells(dev);
68513dccb9eSBenjamin Herrenschmidt 	int np = pna + 5;
68613dccb9eSBenjamin Herrenschmidt 	int memno = 0, isa_hole = -1;
68713dccb9eSBenjamin Herrenschmidt 	u32 pci_space;
68813dccb9eSBenjamin Herrenschmidt 	unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
68913dccb9eSBenjamin Herrenschmidt 	unsigned long long isa_mb = 0;
69013dccb9eSBenjamin Herrenschmidt 	struct resource *res;
69113dccb9eSBenjamin Herrenschmidt 
69213dccb9eSBenjamin Herrenschmidt 	printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
69313dccb9eSBenjamin Herrenschmidt 	       dev->full_name, primary ? "(primary)" : "");
69413dccb9eSBenjamin Herrenschmidt 
69513dccb9eSBenjamin Herrenschmidt 	/* Get ranges property */
69613dccb9eSBenjamin Herrenschmidt 	ranges = of_get_property(dev, "ranges", &rlen);
69713dccb9eSBenjamin Herrenschmidt 	if (ranges == NULL)
69813dccb9eSBenjamin Herrenschmidt 		return;
69913dccb9eSBenjamin Herrenschmidt 
70013dccb9eSBenjamin Herrenschmidt 	/* Parse it */
70113dccb9eSBenjamin Herrenschmidt 	while ((rlen -= np * 4) >= 0) {
70213dccb9eSBenjamin Herrenschmidt 		/* Read next ranges element */
70313dccb9eSBenjamin Herrenschmidt 		pci_space = ranges[0];
70413dccb9eSBenjamin Herrenschmidt 		pci_addr = of_read_number(ranges + 1, 2);
70513dccb9eSBenjamin Herrenschmidt 		cpu_addr = of_translate_address(dev, ranges + 3);
70613dccb9eSBenjamin Herrenschmidt 		size = of_read_number(ranges + pna + 3, 2);
70713dccb9eSBenjamin Herrenschmidt 		ranges += np;
708e9f82cb7SBenjamin Herrenschmidt 
709e9f82cb7SBenjamin Herrenschmidt 		/* If we failed translation or got a zero-sized region
710e9f82cb7SBenjamin Herrenschmidt 		 * (some FW try to feed us with non sensical zero sized regions
711e9f82cb7SBenjamin Herrenschmidt 		 * such as power3 which look like some kind of attempt at exposing
712e9f82cb7SBenjamin Herrenschmidt 		 * the VGA memory hole)
713e9f82cb7SBenjamin Herrenschmidt 		 */
71413dccb9eSBenjamin Herrenschmidt 		if (cpu_addr == OF_BAD_ADDR || size == 0)
71513dccb9eSBenjamin Herrenschmidt 			continue;
71613dccb9eSBenjamin Herrenschmidt 
71713dccb9eSBenjamin Herrenschmidt 		/* Now consume following elements while they are contiguous */
71813dccb9eSBenjamin Herrenschmidt 		for (; rlen >= np * sizeof(u32);
71913dccb9eSBenjamin Herrenschmidt 		     ranges += np, rlen -= np * 4) {
72013dccb9eSBenjamin Herrenschmidt 			if (ranges[0] != pci_space)
72113dccb9eSBenjamin Herrenschmidt 				break;
72213dccb9eSBenjamin Herrenschmidt 			pci_next = of_read_number(ranges + 1, 2);
72313dccb9eSBenjamin Herrenschmidt 			cpu_next = of_translate_address(dev, ranges + 3);
72413dccb9eSBenjamin Herrenschmidt 			if (pci_next != pci_addr + size ||
72513dccb9eSBenjamin Herrenschmidt 			    cpu_next != cpu_addr + size)
72613dccb9eSBenjamin Herrenschmidt 				break;
72713dccb9eSBenjamin Herrenschmidt 			size += of_read_number(ranges + pna + 3, 2);
72813dccb9eSBenjamin Herrenschmidt 		}
72913dccb9eSBenjamin Herrenschmidt 
73013dccb9eSBenjamin Herrenschmidt 		/* Act based on address space type */
73113dccb9eSBenjamin Herrenschmidt 		res = NULL;
73213dccb9eSBenjamin Herrenschmidt 		switch ((pci_space >> 24) & 0x3) {
73313dccb9eSBenjamin Herrenschmidt 		case 1:		/* PCI IO space */
73413dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
73513dccb9eSBenjamin Herrenschmidt 			       "  IO 0x%016llx..0x%016llx -> 0x%016llx\n",
73613dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr);
73713dccb9eSBenjamin Herrenschmidt 
73813dccb9eSBenjamin Herrenschmidt 			/* We support only one IO range */
73913dccb9eSBenjamin Herrenschmidt 			if (hose->pci_io_size) {
74013dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
74113dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
74213dccb9eSBenjamin Herrenschmidt 				continue;
74313dccb9eSBenjamin Herrenschmidt 			}
74413dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32
74513dccb9eSBenjamin Herrenschmidt 			/* On 32 bits, limit I/O space to 16MB */
74613dccb9eSBenjamin Herrenschmidt 			if (size > 0x01000000)
74713dccb9eSBenjamin Herrenschmidt 				size = 0x01000000;
74813dccb9eSBenjamin Herrenschmidt 
74913dccb9eSBenjamin Herrenschmidt 			/* 32 bits needs to map IOs here */
75013dccb9eSBenjamin Herrenschmidt 			hose->io_base_virt = ioremap(cpu_addr, size);
75113dccb9eSBenjamin Herrenschmidt 
75213dccb9eSBenjamin Herrenschmidt 			/* Expect trouble if pci_addr is not 0 */
75313dccb9eSBenjamin Herrenschmidt 			if (primary)
75413dccb9eSBenjamin Herrenschmidt 				isa_io_base =
75513dccb9eSBenjamin Herrenschmidt 					(unsigned long)hose->io_base_virt;
75613dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
75713dccb9eSBenjamin Herrenschmidt 			/* pci_io_size and io_base_phys always represent IO
75813dccb9eSBenjamin Herrenschmidt 			 * space starting at 0 so we factor in pci_addr
75913dccb9eSBenjamin Herrenschmidt 			 */
76013dccb9eSBenjamin Herrenschmidt 			hose->pci_io_size = pci_addr + size;
76113dccb9eSBenjamin Herrenschmidt 			hose->io_base_phys = cpu_addr - pci_addr;
76213dccb9eSBenjamin Herrenschmidt 
76313dccb9eSBenjamin Herrenschmidt 			/* Build resource */
76413dccb9eSBenjamin Herrenschmidt 			res = &hose->io_resource;
76513dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_IO;
76613dccb9eSBenjamin Herrenschmidt 			res->start = pci_addr;
76713dccb9eSBenjamin Herrenschmidt 			break;
76813dccb9eSBenjamin Herrenschmidt 		case 2:		/* PCI Memory space */
76967260ac9SBenjamin Herrenschmidt 		case 3:		/* PCI 64 bits Memory space */
77013dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
77113dccb9eSBenjamin Herrenschmidt 			       " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
77213dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr,
77313dccb9eSBenjamin Herrenschmidt 			       (pci_space & 0x40000000) ? "Prefetch" : "");
77413dccb9eSBenjamin Herrenschmidt 
77513dccb9eSBenjamin Herrenschmidt 			/* We support only 3 memory ranges */
77613dccb9eSBenjamin Herrenschmidt 			if (memno >= 3) {
77713dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
77813dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
77913dccb9eSBenjamin Herrenschmidt 				continue;
78013dccb9eSBenjamin Herrenschmidt 			}
78113dccb9eSBenjamin Herrenschmidt 			/* Handles ISA memory hole space here */
78213dccb9eSBenjamin Herrenschmidt 			if (pci_addr == 0) {
78313dccb9eSBenjamin Herrenschmidt 				isa_mb = cpu_addr;
78413dccb9eSBenjamin Herrenschmidt 				isa_hole = memno;
78513dccb9eSBenjamin Herrenschmidt 				if (primary || isa_mem_base == 0)
78613dccb9eSBenjamin Herrenschmidt 					isa_mem_base = cpu_addr;
787e9f82cb7SBenjamin Herrenschmidt 				hose->isa_mem_phys = cpu_addr;
788e9f82cb7SBenjamin Herrenschmidt 				hose->isa_mem_size = size;
78913dccb9eSBenjamin Herrenschmidt 			}
79013dccb9eSBenjamin Herrenschmidt 
79113dccb9eSBenjamin Herrenschmidt 			/* We get the PCI/Mem offset from the first range or
79213dccb9eSBenjamin Herrenschmidt 			 * the, current one if the offset came from an ISA
79313dccb9eSBenjamin Herrenschmidt 			 * hole. If they don't match, bugger.
79413dccb9eSBenjamin Herrenschmidt 			 */
79513dccb9eSBenjamin Herrenschmidt 			if (memno == 0 ||
79613dccb9eSBenjamin Herrenschmidt 			    (isa_hole >= 0 && pci_addr != 0 &&
79713dccb9eSBenjamin Herrenschmidt 			     hose->pci_mem_offset == isa_mb))
79813dccb9eSBenjamin Herrenschmidt 				hose->pci_mem_offset = cpu_addr - pci_addr;
79913dccb9eSBenjamin Herrenschmidt 			else if (pci_addr != 0 &&
80013dccb9eSBenjamin Herrenschmidt 				 hose->pci_mem_offset != cpu_addr - pci_addr) {
80113dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
80213dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (offset mismatch) !\n");
80313dccb9eSBenjamin Herrenschmidt 				continue;
80413dccb9eSBenjamin Herrenschmidt 			}
80513dccb9eSBenjamin Herrenschmidt 
80613dccb9eSBenjamin Herrenschmidt 			/* Build resource */
80713dccb9eSBenjamin Herrenschmidt 			res = &hose->mem_resources[memno++];
80813dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_MEM;
80913dccb9eSBenjamin Herrenschmidt 			if (pci_space & 0x40000000)
81013dccb9eSBenjamin Herrenschmidt 				res->flags |= IORESOURCE_PREFETCH;
81113dccb9eSBenjamin Herrenschmidt 			res->start = cpu_addr;
81213dccb9eSBenjamin Herrenschmidt 			break;
81313dccb9eSBenjamin Herrenschmidt 		}
81413dccb9eSBenjamin Herrenschmidt 		if (res != NULL) {
81513dccb9eSBenjamin Herrenschmidt 			res->name = dev->full_name;
81613dccb9eSBenjamin Herrenschmidt 			res->end = res->start + size - 1;
81713dccb9eSBenjamin Herrenschmidt 			res->parent = NULL;
81813dccb9eSBenjamin Herrenschmidt 			res->sibling = NULL;
81913dccb9eSBenjamin Herrenschmidt 			res->child = NULL;
82013dccb9eSBenjamin Herrenschmidt 		}
82113dccb9eSBenjamin Herrenschmidt 	}
82213dccb9eSBenjamin Herrenschmidt 
8238db13a0eSBenjamin Herrenschmidt 	/* If there's an ISA hole and the pci_mem_offset is -not- matching
8248db13a0eSBenjamin Herrenschmidt 	 * the ISA hole offset, then we need to remove the ISA hole from
8258db13a0eSBenjamin Herrenschmidt 	 * the resource list for that brige
8268db13a0eSBenjamin Herrenschmidt 	 */
8278db13a0eSBenjamin Herrenschmidt 	if (isa_hole >= 0 && hose->pci_mem_offset != isa_mb) {
8288db13a0eSBenjamin Herrenschmidt 		unsigned int next = isa_hole + 1;
8298db13a0eSBenjamin Herrenschmidt 		printk(KERN_INFO " Removing ISA hole at 0x%016llx\n", isa_mb);
8308db13a0eSBenjamin Herrenschmidt 		if (next < memno)
8318db13a0eSBenjamin Herrenschmidt 			memmove(&hose->mem_resources[isa_hole],
8328db13a0eSBenjamin Herrenschmidt 				&hose->mem_resources[next],
8338db13a0eSBenjamin Herrenschmidt 				sizeof(struct resource) * (memno - next));
8348db13a0eSBenjamin Herrenschmidt 		hose->mem_resources[--memno].flags = 0;
83513dccb9eSBenjamin Herrenschmidt 	}
83613dccb9eSBenjamin Herrenschmidt }
837fa462f2dSBenjamin Herrenschmidt 
838fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */
839fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus)
840fa462f2dSBenjamin Herrenschmidt {
841fa462f2dSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
8421fd0f525SBenjamin Herrenschmidt 
843fa462f2dSBenjamin Herrenschmidt 	if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS))
844fa462f2dSBenjamin Herrenschmidt 		return 0;
845fa462f2dSBenjamin Herrenschmidt 	if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0)
846fa462f2dSBenjamin Herrenschmidt 		return hose->global_number != 0;
847fa462f2dSBenjamin Herrenschmidt 	return 1;
848fa462f2dSBenjamin Herrenschmidt }
849fa462f2dSBenjamin Herrenschmidt 
850fe2d338cSBenjamin Herrenschmidt void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
851fe2d338cSBenjamin Herrenschmidt 			     struct resource *res)
852fe2d338cSBenjamin Herrenschmidt {
853fe2d338cSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
854fe2d338cSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
855fe2d338cSBenjamin Herrenschmidt 
856fe2d338cSBenjamin Herrenschmidt 	if (!hose)
857fe2d338cSBenjamin Herrenschmidt 		return;
858fe2d338cSBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
859fe2d338cSBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
860fe2d338cSBenjamin Herrenschmidt 		mask = 0xffffffffu;
861fe2d338cSBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
862fe2d338cSBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
863fe2d338cSBenjamin Herrenschmidt 
864fe2d338cSBenjamin Herrenschmidt 	region->start = (res->start - offset) & mask;
865fe2d338cSBenjamin Herrenschmidt 	region->end = (res->end - offset) & mask;
866fe2d338cSBenjamin Herrenschmidt }
867fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_resource_to_bus);
868fe2d338cSBenjamin Herrenschmidt 
869fe2d338cSBenjamin Herrenschmidt void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
870fe2d338cSBenjamin Herrenschmidt 			     struct pci_bus_region *region)
871fe2d338cSBenjamin Herrenschmidt {
872fe2d338cSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
873fe2d338cSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
874fe2d338cSBenjamin Herrenschmidt 
875fe2d338cSBenjamin Herrenschmidt 	if (!hose)
876fe2d338cSBenjamin Herrenschmidt 		return;
877fe2d338cSBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
878fe2d338cSBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
879fe2d338cSBenjamin Herrenschmidt 		mask = 0xffffffffu;
880fe2d338cSBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
881fe2d338cSBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
882fe2d338cSBenjamin Herrenschmidt 	res->start = (region->start + offset) & mask;
883fe2d338cSBenjamin Herrenschmidt 	res->end = (region->end + offset) & mask;
884fe2d338cSBenjamin Herrenschmidt }
885fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_bus_to_resource);
886bf5e2ba2SBenjamin Herrenschmidt 
887bf5e2ba2SBenjamin Herrenschmidt /* Fixup a bus resource into a linux resource */
888bf5e2ba2SBenjamin Herrenschmidt static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
889bf5e2ba2SBenjamin Herrenschmidt {
890bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
891bf5e2ba2SBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
892bf5e2ba2SBenjamin Herrenschmidt 
893bf5e2ba2SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
894bf5e2ba2SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
895bf5e2ba2SBenjamin Herrenschmidt 		mask = 0xffffffffu;
896bf5e2ba2SBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
897bf5e2ba2SBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
898bf5e2ba2SBenjamin Herrenschmidt 
899bf5e2ba2SBenjamin Herrenschmidt 	res->start = (res->start + offset) & mask;
900bf5e2ba2SBenjamin Herrenschmidt 	res->end = (res->end + offset) & mask;
901bf5e2ba2SBenjamin Herrenschmidt }
902bf5e2ba2SBenjamin Herrenschmidt 
903bf5e2ba2SBenjamin Herrenschmidt 
904bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are
905bf5e2ba2SBenjamin Herrenschmidt  * probed, but not for bridge ranges
906bf5e2ba2SBenjamin Herrenschmidt  */
907bf5e2ba2SBenjamin Herrenschmidt static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
908bf5e2ba2SBenjamin Herrenschmidt {
909bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
910bf5e2ba2SBenjamin Herrenschmidt 	int i;
911bf5e2ba2SBenjamin Herrenschmidt 
912bf5e2ba2SBenjamin Herrenschmidt 	if (!hose) {
913bf5e2ba2SBenjamin Herrenschmidt 		printk(KERN_ERR "No host bridge for PCI dev %s !\n",
914bf5e2ba2SBenjamin Herrenschmidt 		       pci_name(dev));
915bf5e2ba2SBenjamin Herrenschmidt 		return;
916bf5e2ba2SBenjamin Herrenschmidt 	}
917bf5e2ba2SBenjamin Herrenschmidt 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
918bf5e2ba2SBenjamin Herrenschmidt 		struct resource *res = dev->resource + i;
919bf5e2ba2SBenjamin Herrenschmidt 		if (!res->flags)
920bf5e2ba2SBenjamin Herrenschmidt 			continue;
9217f172890SBenjamin Herrenschmidt 		/* On platforms that have PPC_PCI_PROBE_ONLY set, we don't
9227f172890SBenjamin Herrenschmidt 		 * consider 0 as an unassigned BAR value. It's technically
9237f172890SBenjamin Herrenschmidt 		 * a valid value, but linux doesn't like it... so when we can
9247f172890SBenjamin Herrenschmidt 		 * re-assign things, we do so, but if we can't, we keep it
9257f172890SBenjamin Herrenschmidt 		 * around and hope for the best...
9267f172890SBenjamin Herrenschmidt 		 */
9277f172890SBenjamin Herrenschmidt 		if (res->start == 0 && !(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
928bf5e2ba2SBenjamin Herrenschmidt 			pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
929bf5e2ba2SBenjamin Herrenschmidt 				 pci_name(dev), i,
930bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->start,
931bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->end,
932bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned int)res->flags);
933bf5e2ba2SBenjamin Herrenschmidt 			res->end -= res->start;
934bf5e2ba2SBenjamin Herrenschmidt 			res->start = 0;
935bf5e2ba2SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
936bf5e2ba2SBenjamin Herrenschmidt 			continue;
937bf5e2ba2SBenjamin Herrenschmidt 		}
938bf5e2ba2SBenjamin Herrenschmidt 
939bf5e2ba2SBenjamin Herrenschmidt 		pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
940bf5e2ba2SBenjamin Herrenschmidt 			 pci_name(dev), i,
941bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->start,\
942bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
943bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned int)res->flags);
944bf5e2ba2SBenjamin Herrenschmidt 
945bf5e2ba2SBenjamin Herrenschmidt 		fixup_resource(res, dev);
946b5561511SBenjamin Herrenschmidt 
947b5561511SBenjamin Herrenschmidt 		pr_debug("PCI:%s            %016llx-%016llx\n",
948b5561511SBenjamin Herrenschmidt 			 pci_name(dev),
949b5561511SBenjamin Herrenschmidt 			 (unsigned long long)res->start,
950b5561511SBenjamin Herrenschmidt 			 (unsigned long long)res->end);
951bf5e2ba2SBenjamin Herrenschmidt 	}
952bf5e2ba2SBenjamin Herrenschmidt 
953bf5e2ba2SBenjamin Herrenschmidt 	/* Call machine specific resource fixup */
954bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_resources)
955bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_resources(dev);
956bf5e2ba2SBenjamin Herrenschmidt }
957bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
958bf5e2ba2SBenjamin Herrenschmidt 
959b5561511SBenjamin Herrenschmidt /* This function tries to figure out if a bridge resource has been initialized
960b5561511SBenjamin Herrenschmidt  * by the firmware or not. It doesn't have to be absolutely bullet proof, but
961b5561511SBenjamin Herrenschmidt  * things go more smoothly when it gets it right. It should covers cases such
962b5561511SBenjamin Herrenschmidt  * as Apple "closed" bridge resources and bare-metal pSeries unassigned bridges
963b5561511SBenjamin Herrenschmidt  */
964b5561511SBenjamin Herrenschmidt static int __devinit pcibios_uninitialized_bridge_resource(struct pci_bus *bus,
965b5561511SBenjamin Herrenschmidt 							   struct resource *res)
966bf5e2ba2SBenjamin Herrenschmidt {
967be8cbcd8SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
968bf5e2ba2SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
969b5561511SBenjamin Herrenschmidt 	resource_size_t offset;
970b5561511SBenjamin Herrenschmidt 	u16 command;
971b5561511SBenjamin Herrenschmidt 	int i;
972bf5e2ba2SBenjamin Herrenschmidt 
973b5561511SBenjamin Herrenschmidt 	/* We don't do anything if PCI_PROBE_ONLY is set */
974b5561511SBenjamin Herrenschmidt 	if (ppc_pci_flags & PPC_PCI_PROBE_ONLY)
975b5561511SBenjamin Herrenschmidt 		return 0;
976bf5e2ba2SBenjamin Herrenschmidt 
977b5561511SBenjamin Herrenschmidt 	/* Job is a bit different between memory and IO */
978b5561511SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_MEM) {
979b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0 (res != pci_mem_offset) then it's probably been
980b5561511SBenjamin Herrenschmidt 		 * initialized by somebody
981bf5e2ba2SBenjamin Herrenschmidt 		 */
982b5561511SBenjamin Herrenschmidt 		if (res->start != hose->pci_mem_offset)
983b5561511SBenjamin Herrenschmidt 			return 0;
984b5561511SBenjamin Herrenschmidt 
985b5561511SBenjamin Herrenschmidt 		/* The BAR is 0, let's check if memory decoding is enabled on
986b5561511SBenjamin Herrenschmidt 		 * the bridge. If not, we consider it unassigned
987b5561511SBenjamin Herrenschmidt 		 */
988b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
989b5561511SBenjamin Herrenschmidt 		if ((command & PCI_COMMAND_MEMORY) == 0)
990b5561511SBenjamin Herrenschmidt 			return 1;
991b5561511SBenjamin Herrenschmidt 
992b5561511SBenjamin Herrenschmidt 		/* Memory decoding is enabled and the BAR is 0. If any of the bridge
993b5561511SBenjamin Herrenschmidt 		 * resources covers that starting address (0 then it's good enough for
994b5561511SBenjamin Herrenschmidt 		 * us for memory
995b5561511SBenjamin Herrenschmidt 		 */
996b5561511SBenjamin Herrenschmidt 		for (i = 0; i < 3; i++) {
997b5561511SBenjamin Herrenschmidt 			if ((hose->mem_resources[i].flags & IORESOURCE_MEM) &&
998b5561511SBenjamin Herrenschmidt 			    hose->mem_resources[i].start == hose->pci_mem_offset)
999b5561511SBenjamin Herrenschmidt 				return 0;
1000b5561511SBenjamin Herrenschmidt 		}
1001b5561511SBenjamin Herrenschmidt 
1002b5561511SBenjamin Herrenschmidt 		/* Well, it starts at 0 and we know it will collide so we may as
1003b5561511SBenjamin Herrenschmidt 		 * well consider it as unassigned. That covers the Apple case.
1004b5561511SBenjamin Herrenschmidt 		 */
1005b5561511SBenjamin Herrenschmidt 		return 1;
1006b5561511SBenjamin Herrenschmidt 	} else {
1007b5561511SBenjamin Herrenschmidt 		/* If the BAR is non-0, then we consider it assigned */
1008b5561511SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1009b5561511SBenjamin Herrenschmidt 		if (((res->start - offset) & 0xfffffffful) != 0)
1010b5561511SBenjamin Herrenschmidt 			return 0;
1011b5561511SBenjamin Herrenschmidt 
1012b5561511SBenjamin Herrenschmidt 		/* Here, we are a bit different than memory as typically IO space
1013b5561511SBenjamin Herrenschmidt 		 * starting at low addresses -is- valid. What we do instead if that
1014b5561511SBenjamin Herrenschmidt 		 * we consider as unassigned anything that doesn't have IO enabled
1015b5561511SBenjamin Herrenschmidt 		 * in the PCI command register, and that's it.
1016b5561511SBenjamin Herrenschmidt 		 */
1017b5561511SBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1018b5561511SBenjamin Herrenschmidt 		if (command & PCI_COMMAND_IO)
1019b5561511SBenjamin Herrenschmidt 			return 0;
1020b5561511SBenjamin Herrenschmidt 
1021b5561511SBenjamin Herrenschmidt 		/* It's starting at 0 and IO is disabled in the bridge, consider
1022b5561511SBenjamin Herrenschmidt 		 * it unassigned
1023b5561511SBenjamin Herrenschmidt 		 */
1024b5561511SBenjamin Herrenschmidt 		return 1;
1025b5561511SBenjamin Herrenschmidt 	}
1026b5561511SBenjamin Herrenschmidt }
1027b5561511SBenjamin Herrenschmidt 
1028b5561511SBenjamin Herrenschmidt /* Fixup resources of a PCI<->PCI bridge */
1029b5561511SBenjamin Herrenschmidt static void __devinit pcibios_fixup_bridge(struct pci_bus *bus)
1030b5561511SBenjamin Herrenschmidt {
1031bf5e2ba2SBenjamin Herrenschmidt 	struct resource *res;
1032bf5e2ba2SBenjamin Herrenschmidt 	int i;
1033bf5e2ba2SBenjamin Herrenschmidt 
1034b5561511SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
1035b5561511SBenjamin Herrenschmidt 
103689a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
103789a74eccSBjorn Helgaas 		if (!res || !res->flags)
1038bf5e2ba2SBenjamin Herrenschmidt 			continue;
1039b188b2aeSKumar Gala 		if (i >= 3 && bus->self->transparent)
1040b188b2aeSKumar Gala 			continue;
1041be8cbcd8SBenjamin Herrenschmidt 
1042bf5e2ba2SBenjamin Herrenschmidt 		pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
1043bf5e2ba2SBenjamin Herrenschmidt 			 pci_name(dev), i,
1044bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->start,\
1045bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
1046bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned int)res->flags);
1047bf5e2ba2SBenjamin Herrenschmidt 
1048b5561511SBenjamin Herrenschmidt 		/* Perform fixup */
1049bf5e2ba2SBenjamin Herrenschmidt 		fixup_resource(res, dev);
1050b5561511SBenjamin Herrenschmidt 
1051b5561511SBenjamin Herrenschmidt 		/* Try to detect uninitialized P2P bridge resources,
1052b5561511SBenjamin Herrenschmidt 		 * and clear them out so they get re-assigned later
1053b5561511SBenjamin Herrenschmidt 		 */
1054b5561511SBenjamin Herrenschmidt 		if (pcibios_uninitialized_bridge_resource(bus, res)) {
1055b5561511SBenjamin Herrenschmidt 			res->flags = 0;
1056b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            (unassigned)\n", pci_name(dev));
1057b5561511SBenjamin Herrenschmidt 		} else {
1058b5561511SBenjamin Herrenschmidt 
1059b5561511SBenjamin Herrenschmidt 			pr_debug("PCI:%s            %016llx-%016llx\n",
1060b5561511SBenjamin Herrenschmidt 				 pci_name(dev),
1061b5561511SBenjamin Herrenschmidt 				 (unsigned long long)res->start,
1062b5561511SBenjamin Herrenschmidt 				 (unsigned long long)res->end);
1063bf5e2ba2SBenjamin Herrenschmidt 		}
1064bf5e2ba2SBenjamin Herrenschmidt 	}
1065b5561511SBenjamin Herrenschmidt }
1066b5561511SBenjamin Herrenschmidt 
10678b8da358SBenjamin Herrenschmidt void __devinit pcibios_setup_bus_self(struct pci_bus *bus)
10688b8da358SBenjamin Herrenschmidt {
10697eef440aSBenjamin Herrenschmidt 	/* Fix up the bus resources for P2P bridges */
10708b8da358SBenjamin Herrenschmidt 	if (bus->self != NULL)
10718b8da358SBenjamin Herrenschmidt 		pcibios_fixup_bridge(bus);
10728b8da358SBenjamin Herrenschmidt 
10738b8da358SBenjamin Herrenschmidt 	/* Platform specific bus fixups. This is currently only used
10747eef440aSBenjamin Herrenschmidt 	 * by fsl_pci and I'm hoping to get rid of it at some point
10758b8da358SBenjamin Herrenschmidt 	 */
10768b8da358SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_bus)
10778b8da358SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_bus(bus);
10788b8da358SBenjamin Herrenschmidt 
10798b8da358SBenjamin Herrenschmidt 	/* Setup bus DMA mappings */
10808b8da358SBenjamin Herrenschmidt 	if (ppc_md.pci_dma_bus_setup)
10818b8da358SBenjamin Herrenschmidt 		ppc_md.pci_dma_bus_setup(bus);
10828b8da358SBenjamin Herrenschmidt }
10838b8da358SBenjamin Herrenschmidt 
10847eef440aSBenjamin Herrenschmidt void __devinit pcibios_setup_bus_devices(struct pci_bus *bus)
10857eef440aSBenjamin Herrenschmidt {
10867eef440aSBenjamin Herrenschmidt 	struct pci_dev *dev;
10877eef440aSBenjamin Herrenschmidt 
10887eef440aSBenjamin Herrenschmidt 	pr_debug("PCI: Fixup bus devices %d (%s)\n",
10897eef440aSBenjamin Herrenschmidt 		 bus->number, bus->self ? pci_name(bus->self) : "PHB");
10907eef440aSBenjamin Herrenschmidt 
10917eef440aSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
10927eef440aSBenjamin Herrenschmidt 		struct dev_archdata *sd = &dev->dev.archdata;
10937eef440aSBenjamin Herrenschmidt 
10942d1c8618SBenjamin Herrenschmidt 		/* Cardbus can call us to add new devices to a bus, so ignore
10952d1c8618SBenjamin Herrenschmidt 		 * those who are already fully discovered
10962d1c8618SBenjamin Herrenschmidt 		 */
10972d1c8618SBenjamin Herrenschmidt 		if (dev->is_added)
10982d1c8618SBenjamin Herrenschmidt 			continue;
10992d1c8618SBenjamin Herrenschmidt 
11007eef440aSBenjamin Herrenschmidt 		/* Setup OF node pointer in archdata */
11017eef440aSBenjamin Herrenschmidt 		sd->of_node = pci_device_to_OF_node(dev);
11027eef440aSBenjamin Herrenschmidt 
11037eef440aSBenjamin Herrenschmidt 		/* Fixup NUMA node as it may not be setup yet by the generic
11047eef440aSBenjamin Herrenschmidt 		 * code and is needed by the DMA init
11057eef440aSBenjamin Herrenschmidt 		 */
11067eef440aSBenjamin Herrenschmidt 		set_dev_node(&dev->dev, pcibus_to_node(dev->bus));
11077eef440aSBenjamin Herrenschmidt 
11087eef440aSBenjamin Herrenschmidt 		/* Hook up default DMA ops */
11097eef440aSBenjamin Herrenschmidt 		sd->dma_ops = pci_dma_ops;
1110738ef42eSBecky Bruce 		set_dma_offset(&dev->dev, PCI_DRAM_OFFSET);
11117eef440aSBenjamin Herrenschmidt 
11127eef440aSBenjamin Herrenschmidt 		/* Additional platform DMA/iommu setup */
11137eef440aSBenjamin Herrenschmidt 		if (ppc_md.pci_dma_dev_setup)
11147eef440aSBenjamin Herrenschmidt 			ppc_md.pci_dma_dev_setup(dev);
11157eef440aSBenjamin Herrenschmidt 
11167eef440aSBenjamin Herrenschmidt 		/* Read default IRQs and fixup if necessary */
11177eef440aSBenjamin Herrenschmidt 		pci_read_irq_line(dev);
11187eef440aSBenjamin Herrenschmidt 		if (ppc_md.pci_irq_fixup)
11197eef440aSBenjamin Herrenschmidt 			ppc_md.pci_irq_fixup(dev);
11207eef440aSBenjamin Herrenschmidt 	}
11217eef440aSBenjamin Herrenschmidt }
11227eef440aSBenjamin Herrenschmidt 
1123bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_fixup_bus(struct pci_bus *bus)
1124bf5e2ba2SBenjamin Herrenschmidt {
1125bf5e2ba2SBenjamin Herrenschmidt 	/* When called from the generic PCI probe, read PCI<->PCI bridge
11267eef440aSBenjamin Herrenschmidt 	 * bases. This is -not- called when generating the PCI tree from
11278b8da358SBenjamin Herrenschmidt 	 * the OF device-tree.
1128bf5e2ba2SBenjamin Herrenschmidt 	 */
1129bf5e2ba2SBenjamin Herrenschmidt 	if (bus->self != NULL)
1130bf5e2ba2SBenjamin Herrenschmidt 		pci_read_bridge_bases(bus);
11318b8da358SBenjamin Herrenschmidt 
11328b8da358SBenjamin Herrenschmidt 	/* Now fixup the bus bus */
11338b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_self(bus);
11348b8da358SBenjamin Herrenschmidt 
11358b8da358SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
11368b8da358SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
1137bf5e2ba2SBenjamin Herrenschmidt }
1138bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus);
1139bf5e2ba2SBenjamin Herrenschmidt 
11402d1c8618SBenjamin Herrenschmidt void __devinit pci_fixup_cardbus(struct pci_bus *bus)
11412d1c8618SBenjamin Herrenschmidt {
11422d1c8618SBenjamin Herrenschmidt 	/* Now fixup devices on that bus */
11432d1c8618SBenjamin Herrenschmidt 	pcibios_setup_bus_devices(bus);
11442d1c8618SBenjamin Herrenschmidt }
11452d1c8618SBenjamin Herrenschmidt 
11462d1c8618SBenjamin Herrenschmidt 
11473fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev)
11483fd94c6bSBenjamin Herrenschmidt {
11493fd94c6bSBenjamin Herrenschmidt 	if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
11503fd94c6bSBenjamin Herrenschmidt 	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
11513fd94c6bSBenjamin Herrenschmidt 		return 1;
11523fd94c6bSBenjamin Herrenschmidt 	return 0;
11533fd94c6bSBenjamin Herrenschmidt }
11543fd94c6bSBenjamin Herrenschmidt 
11553fd94c6bSBenjamin Herrenschmidt /*
11563fd94c6bSBenjamin Herrenschmidt  * We need to avoid collisions with `mirrored' VGA ports
11573fd94c6bSBenjamin Herrenschmidt  * and other strange ISA hardware, so we always want the
11583fd94c6bSBenjamin Herrenschmidt  * addresses to be allocated in the 0x000-0x0ff region
11593fd94c6bSBenjamin Herrenschmidt  * modulo 0x400.
11603fd94c6bSBenjamin Herrenschmidt  *
11613fd94c6bSBenjamin Herrenschmidt  * Why? Because some silly external IO cards only decode
11623fd94c6bSBenjamin Herrenschmidt  * the low 10 bits of the IO address. The 0x00-0xff region
11633fd94c6bSBenjamin Herrenschmidt  * is reserved for motherboard devices that decode all 16
11643fd94c6bSBenjamin Herrenschmidt  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
11653fd94c6bSBenjamin Herrenschmidt  * but we want to try to avoid allocating at 0x2900-0x2bff
11663fd94c6bSBenjamin Herrenschmidt  * which might have be mirrored at 0x0100-0x03ff..
11673fd94c6bSBenjamin Herrenschmidt  */
11683b7a17fcSDominik Brodowski resource_size_t pcibios_align_resource(void *data, const struct resource *res,
11693fd94c6bSBenjamin Herrenschmidt 				resource_size_t size, resource_size_t align)
11703fd94c6bSBenjamin Herrenschmidt {
11713fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = data;
11723fd94c6bSBenjamin Herrenschmidt 	resource_size_t start = res->start;
11733fd94c6bSBenjamin Herrenschmidt 
1174b26b2d49SDominik Brodowski 	if (res->flags & IORESOURCE_IO) {
11753fd94c6bSBenjamin Herrenschmidt 		if (skip_isa_ioresource_align(dev))
1176b26b2d49SDominik Brodowski 			return start;
1177b26b2d49SDominik Brodowski 		if (start & 0x300)
11783fd94c6bSBenjamin Herrenschmidt 			start = (start + 0x3ff) & ~0x3ff;
11793fd94c6bSBenjamin Herrenschmidt 	}
1180b26b2d49SDominik Brodowski 
1181b26b2d49SDominik Brodowski 	return start;
11823fd94c6bSBenjamin Herrenschmidt }
11833fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource);
11843fd94c6bSBenjamin Herrenschmidt 
11853fd94c6bSBenjamin Herrenschmidt /*
11863fd94c6bSBenjamin Herrenschmidt  * Reparent resource children of pr that conflict with res
11873fd94c6bSBenjamin Herrenschmidt  * under res, and make res replace those children.
11883fd94c6bSBenjamin Herrenschmidt  */
11890f6023d5SHeiko Schocher static int reparent_resources(struct resource *parent,
11903fd94c6bSBenjamin Herrenschmidt 				     struct resource *res)
11913fd94c6bSBenjamin Herrenschmidt {
11923fd94c6bSBenjamin Herrenschmidt 	struct resource *p, **pp;
11933fd94c6bSBenjamin Herrenschmidt 	struct resource **firstpp = NULL;
11943fd94c6bSBenjamin Herrenschmidt 
11953fd94c6bSBenjamin Herrenschmidt 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
11963fd94c6bSBenjamin Herrenschmidt 		if (p->end < res->start)
11973fd94c6bSBenjamin Herrenschmidt 			continue;
11983fd94c6bSBenjamin Herrenschmidt 		if (res->end < p->start)
11993fd94c6bSBenjamin Herrenschmidt 			break;
12003fd94c6bSBenjamin Herrenschmidt 		if (p->start < res->start || p->end > res->end)
12013fd94c6bSBenjamin Herrenschmidt 			return -1;	/* not completely contained */
12023fd94c6bSBenjamin Herrenschmidt 		if (firstpp == NULL)
12033fd94c6bSBenjamin Herrenschmidt 			firstpp = pp;
12043fd94c6bSBenjamin Herrenschmidt 	}
12053fd94c6bSBenjamin Herrenschmidt 	if (firstpp == NULL)
12063fd94c6bSBenjamin Herrenschmidt 		return -1;	/* didn't find any conflicting entries? */
12073fd94c6bSBenjamin Herrenschmidt 	res->parent = parent;
12083fd94c6bSBenjamin Herrenschmidt 	res->child = *firstpp;
12093fd94c6bSBenjamin Herrenschmidt 	res->sibling = *pp;
12103fd94c6bSBenjamin Herrenschmidt 	*firstpp = res;
12113fd94c6bSBenjamin Herrenschmidt 	*pp = NULL;
12123fd94c6bSBenjamin Herrenschmidt 	for (p = res->child; p != NULL; p = p->sibling) {
12133fd94c6bSBenjamin Herrenschmidt 		p->parent = res;
1214b0494bc8SBenjamin Herrenschmidt 		pr_debug("PCI: Reparented %s [%llx..%llx] under %s\n",
12153fd94c6bSBenjamin Herrenschmidt 			 p->name,
12163fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)p->start,
12173fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)p->end, res->name);
12183fd94c6bSBenjamin Herrenschmidt 	}
12193fd94c6bSBenjamin Herrenschmidt 	return 0;
12203fd94c6bSBenjamin Herrenschmidt }
12213fd94c6bSBenjamin Herrenschmidt 
12223fd94c6bSBenjamin Herrenschmidt /*
12233fd94c6bSBenjamin Herrenschmidt  *  Handle resources of PCI devices.  If the world were perfect, we could
12243fd94c6bSBenjamin Herrenschmidt  *  just allocate all the resource regions and do nothing more.  It isn't.
12253fd94c6bSBenjamin Herrenschmidt  *  On the other hand, we cannot just re-allocate all devices, as it would
12263fd94c6bSBenjamin Herrenschmidt  *  require us to know lots of host bridge internals.  So we attempt to
12273fd94c6bSBenjamin Herrenschmidt  *  keep as much of the original configuration as possible, but tweak it
12283fd94c6bSBenjamin Herrenschmidt  *  when it's found to be wrong.
12293fd94c6bSBenjamin Herrenschmidt  *
12303fd94c6bSBenjamin Herrenschmidt  *  Known BIOS problems we have to work around:
12313fd94c6bSBenjamin Herrenschmidt  *	- I/O or memory regions not configured
12323fd94c6bSBenjamin Herrenschmidt  *	- regions configured, but not enabled in the command register
12333fd94c6bSBenjamin Herrenschmidt  *	- bogus I/O addresses above 64K used
12343fd94c6bSBenjamin Herrenschmidt  *	- expansion ROMs left enabled (this may sound harmless, but given
12353fd94c6bSBenjamin Herrenschmidt  *	  the fact the PCI specs explicitly allow address decoders to be
12363fd94c6bSBenjamin Herrenschmidt  *	  shared between expansion ROMs and other resource regions, it's
12373fd94c6bSBenjamin Herrenschmidt  *	  at least dangerous)
12383fd94c6bSBenjamin Herrenschmidt  *
12393fd94c6bSBenjamin Herrenschmidt  *  Our solution:
12403fd94c6bSBenjamin Herrenschmidt  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
12413fd94c6bSBenjamin Herrenschmidt  *	    This gives us fixed barriers on where we can allocate.
12423fd94c6bSBenjamin Herrenschmidt  *	(2) Allocate resources for all enabled devices.  If there is
12433fd94c6bSBenjamin Herrenschmidt  *	    a collision, just mark the resource as unallocated. Also
12443fd94c6bSBenjamin Herrenschmidt  *	    disable expansion ROMs during this step.
12453fd94c6bSBenjamin Herrenschmidt  *	(3) Try to allocate resources for disabled devices.  If the
12463fd94c6bSBenjamin Herrenschmidt  *	    resources were assigned correctly, everything goes well,
12473fd94c6bSBenjamin Herrenschmidt  *	    if they weren't, they won't disturb allocation of other
12483fd94c6bSBenjamin Herrenschmidt  *	    resources.
12493fd94c6bSBenjamin Herrenschmidt  *	(4) Assign new addresses to resources which were either
12503fd94c6bSBenjamin Herrenschmidt  *	    not configured at all or misconfigured.  If explicitly
12513fd94c6bSBenjamin Herrenschmidt  *	    requested by the user, configure expansion ROM address
12523fd94c6bSBenjamin Herrenschmidt  *	    as well.
12533fd94c6bSBenjamin Herrenschmidt  */
12543fd94c6bSBenjamin Herrenschmidt 
1255e90a1318SNathan Fontenot void pcibios_allocate_bus_resources(struct pci_bus *bus)
12563fd94c6bSBenjamin Herrenschmidt {
1257e90a1318SNathan Fontenot 	struct pci_bus *b;
12583fd94c6bSBenjamin Herrenschmidt 	int i;
12593fd94c6bSBenjamin Herrenschmidt 	struct resource *res, *pr;
12603fd94c6bSBenjamin Herrenschmidt 
1261b5ae5f91SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating bus resources for %04x:%02x...\n",
1262b5ae5f91SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1263b5ae5f91SBenjamin Herrenschmidt 
126489a74eccSBjorn Helgaas 	pci_bus_for_each_resource(bus, res, i) {
126589a74eccSBjorn Helgaas 		if (!res || !res->flags || res->start > res->end || res->parent)
12663fd94c6bSBenjamin Herrenschmidt 			continue;
12673fd94c6bSBenjamin Herrenschmidt 		if (bus->parent == NULL)
12683fd94c6bSBenjamin Herrenschmidt 			pr = (res->flags & IORESOURCE_IO) ?
12693fd94c6bSBenjamin Herrenschmidt 				&ioport_resource : &iomem_resource;
12703fd94c6bSBenjamin Herrenschmidt 		else {
12713fd94c6bSBenjamin Herrenschmidt 			/* Don't bother with non-root busses when
12723fd94c6bSBenjamin Herrenschmidt 			 * re-assigning all resources. We clear the
12733fd94c6bSBenjamin Herrenschmidt 			 * resource flags as if they were colliding
12743fd94c6bSBenjamin Herrenschmidt 			 * and as such ensure proper re-allocation
12753fd94c6bSBenjamin Herrenschmidt 			 * later.
12763fd94c6bSBenjamin Herrenschmidt 			 */
12773fd94c6bSBenjamin Herrenschmidt 			if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
12783fd94c6bSBenjamin Herrenschmidt 				goto clear_resource;
12793fd94c6bSBenjamin Herrenschmidt 			pr = pci_find_parent_resource(bus->self, res);
12803fd94c6bSBenjamin Herrenschmidt 			if (pr == res) {
12813fd94c6bSBenjamin Herrenschmidt 				/* this happens when the generic PCI
12823fd94c6bSBenjamin Herrenschmidt 				 * code (wrongly) decides that this
12833fd94c6bSBenjamin Herrenschmidt 				 * bridge is transparent  -- paulus
12843fd94c6bSBenjamin Herrenschmidt 				 */
12853fd94c6bSBenjamin Herrenschmidt 				continue;
12863fd94c6bSBenjamin Herrenschmidt 			}
12873fd94c6bSBenjamin Herrenschmidt 		}
12883fd94c6bSBenjamin Herrenschmidt 
1289b0494bc8SBenjamin Herrenschmidt 		pr_debug("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
12903fd94c6bSBenjamin Herrenschmidt 			 "[0x%x], parent %p (%s)\n",
12913fd94c6bSBenjamin Herrenschmidt 			 bus->self ? pci_name(bus->self) : "PHB",
12923fd94c6bSBenjamin Herrenschmidt 			 bus->number, i,
12933fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)res->start,
12943fd94c6bSBenjamin Herrenschmidt 			 (unsigned long long)res->end,
12953fd94c6bSBenjamin Herrenschmidt 			 (unsigned int)res->flags,
12963fd94c6bSBenjamin Herrenschmidt 			 pr, (pr && pr->name) ? pr->name : "nil");
12973fd94c6bSBenjamin Herrenschmidt 
12983fd94c6bSBenjamin Herrenschmidt 		if (pr && !(pr->flags & IORESOURCE_UNSET)) {
12993fd94c6bSBenjamin Herrenschmidt 			if (request_resource(pr, res) == 0)
13003fd94c6bSBenjamin Herrenschmidt 				continue;
13013fd94c6bSBenjamin Herrenschmidt 			/*
13023fd94c6bSBenjamin Herrenschmidt 			 * Must be a conflict with an existing entry.
13033fd94c6bSBenjamin Herrenschmidt 			 * Move that entry (or entries) under the
13043fd94c6bSBenjamin Herrenschmidt 			 * bridge resource and try again.
13053fd94c6bSBenjamin Herrenschmidt 			 */
13063fd94c6bSBenjamin Herrenschmidt 			if (reparent_resources(pr, res) == 0)
13073fd94c6bSBenjamin Herrenschmidt 				continue;
13083fd94c6bSBenjamin Herrenschmidt 		}
1309e90a1318SNathan Fontenot 		printk(KERN_WARNING "PCI: Cannot allocate resource region "
1310e90a1318SNathan Fontenot 		       "%d of PCI bridge %d, will remap\n", i, bus->number);
13113fd94c6bSBenjamin Herrenschmidt clear_resource:
13123fd94c6bSBenjamin Herrenschmidt 		res->flags = 0;
13133fd94c6bSBenjamin Herrenschmidt 	}
1314e90a1318SNathan Fontenot 
1315e90a1318SNathan Fontenot 	list_for_each_entry(b, &bus->children, node)
1316e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
13173fd94c6bSBenjamin Herrenschmidt }
13183fd94c6bSBenjamin Herrenschmidt 
1319533b1928SPaul Mackerras static inline void __devinit alloc_resource(struct pci_dev *dev, int idx)
13203fd94c6bSBenjamin Herrenschmidt {
13213fd94c6bSBenjamin Herrenschmidt 	struct resource *pr, *r = &dev->resource[idx];
13223fd94c6bSBenjamin Herrenschmidt 
1323b0494bc8SBenjamin Herrenschmidt 	pr_debug("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
13243fd94c6bSBenjamin Herrenschmidt 		 pci_name(dev), idx,
13253fd94c6bSBenjamin Herrenschmidt 		 (unsigned long long)r->start,
13263fd94c6bSBenjamin Herrenschmidt 		 (unsigned long long)r->end,
13273fd94c6bSBenjamin Herrenschmidt 		 (unsigned int)r->flags);
13283fd94c6bSBenjamin Herrenschmidt 
13293fd94c6bSBenjamin Herrenschmidt 	pr = pci_find_parent_resource(dev, r);
13303fd94c6bSBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||
13313fd94c6bSBenjamin Herrenschmidt 	    request_resource(pr, r) < 0) {
13323fd94c6bSBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
13333fd94c6bSBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
13343fd94c6bSBenjamin Herrenschmidt 		if (pr)
1335b0494bc8SBenjamin Herrenschmidt 			pr_debug("PCI:  parent is %p: %016llx-%016llx [%x]\n",
1336b0494bc8SBenjamin Herrenschmidt 				 pr,
13373fd94c6bSBenjamin Herrenschmidt 				 (unsigned long long)pr->start,
13383fd94c6bSBenjamin Herrenschmidt 				 (unsigned long long)pr->end,
13393fd94c6bSBenjamin Herrenschmidt 				 (unsigned int)pr->flags);
13403fd94c6bSBenjamin Herrenschmidt 		/* We'll assign a new address later */
13413fd94c6bSBenjamin Herrenschmidt 		r->flags |= IORESOURCE_UNSET;
13423fd94c6bSBenjamin Herrenschmidt 		r->end -= r->start;
13433fd94c6bSBenjamin Herrenschmidt 		r->start = 0;
13443fd94c6bSBenjamin Herrenschmidt 	}
13453fd94c6bSBenjamin Herrenschmidt }
13463fd94c6bSBenjamin Herrenschmidt 
13473fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass)
13483fd94c6bSBenjamin Herrenschmidt {
13493fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = NULL;
13503fd94c6bSBenjamin Herrenschmidt 	int idx, disabled;
13513fd94c6bSBenjamin Herrenschmidt 	u16 command;
13523fd94c6bSBenjamin Herrenschmidt 	struct resource *r;
13533fd94c6bSBenjamin Herrenschmidt 
13543fd94c6bSBenjamin Herrenschmidt 	for_each_pci_dev(dev) {
13553fd94c6bSBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
1356ad892a63SBenjamin Herrenschmidt 		for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
13573fd94c6bSBenjamin Herrenschmidt 			r = &dev->resource[idx];
13583fd94c6bSBenjamin Herrenschmidt 			if (r->parent)		/* Already allocated */
13593fd94c6bSBenjamin Herrenschmidt 				continue;
13603fd94c6bSBenjamin Herrenschmidt 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
13613fd94c6bSBenjamin Herrenschmidt 				continue;	/* Not assigned at all */
1362ad892a63SBenjamin Herrenschmidt 			/* We only allocate ROMs on pass 1 just in case they
1363ad892a63SBenjamin Herrenschmidt 			 * have been screwed up by firmware
1364ad892a63SBenjamin Herrenschmidt 			 */
1365ad892a63SBenjamin Herrenschmidt 			if (idx == PCI_ROM_RESOURCE )
1366ad892a63SBenjamin Herrenschmidt 				disabled = 1;
13673fd94c6bSBenjamin Herrenschmidt 			if (r->flags & IORESOURCE_IO)
13683fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_IO);
13693fd94c6bSBenjamin Herrenschmidt 			else
13703fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_MEMORY);
1371533b1928SPaul Mackerras 			if (pass == disabled)
1372533b1928SPaul Mackerras 				alloc_resource(dev, idx);
13733fd94c6bSBenjamin Herrenschmidt 		}
13743fd94c6bSBenjamin Herrenschmidt 		if (pass)
13753fd94c6bSBenjamin Herrenschmidt 			continue;
13763fd94c6bSBenjamin Herrenschmidt 		r = &dev->resource[PCI_ROM_RESOURCE];
1377ad892a63SBenjamin Herrenschmidt 		if (r->flags) {
13783fd94c6bSBenjamin Herrenschmidt 			/* Turn the ROM off, leave the resource region,
13793fd94c6bSBenjamin Herrenschmidt 			 * but keep it unregistered.
13803fd94c6bSBenjamin Herrenschmidt 			 */
13813fd94c6bSBenjamin Herrenschmidt 			u32 reg;
1382ad892a63SBenjamin Herrenschmidt 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
1383ad892a63SBenjamin Herrenschmidt 			if (reg & PCI_ROM_ADDRESS_ENABLE) {
1384b0494bc8SBenjamin Herrenschmidt 				pr_debug("PCI: Switching off ROM of %s\n",
1385b0494bc8SBenjamin Herrenschmidt 					 pci_name(dev));
13863fd94c6bSBenjamin Herrenschmidt 				r->flags &= ~IORESOURCE_ROM_ENABLE;
13873fd94c6bSBenjamin Herrenschmidt 				pci_write_config_dword(dev, dev->rom_base_reg,
13883fd94c6bSBenjamin Herrenschmidt 						       reg & ~PCI_ROM_ADDRESS_ENABLE);
13893fd94c6bSBenjamin Herrenschmidt 			}
13903fd94c6bSBenjamin Herrenschmidt 		}
13913fd94c6bSBenjamin Herrenschmidt 	}
1392ad892a63SBenjamin Herrenschmidt }
13933fd94c6bSBenjamin Herrenschmidt 
1394c1f34302SBenjamin Herrenschmidt static void __init pcibios_reserve_legacy_regions(struct pci_bus *bus)
1395c1f34302SBenjamin Herrenschmidt {
1396c1f34302SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
1397c1f34302SBenjamin Herrenschmidt 	resource_size_t	offset;
1398c1f34302SBenjamin Herrenschmidt 	struct resource *res, *pres;
1399c1f34302SBenjamin Herrenschmidt 	int i;
1400c1f34302SBenjamin Herrenschmidt 
1401c1f34302SBenjamin Herrenschmidt 	pr_debug("Reserving legacy ranges for domain %04x\n", pci_domain_nr(bus));
1402c1f34302SBenjamin Herrenschmidt 
1403c1f34302SBenjamin Herrenschmidt 	/* Check for IO */
1404c1f34302SBenjamin Herrenschmidt 	if (!(hose->io_resource.flags & IORESOURCE_IO))
1405c1f34302SBenjamin Herrenschmidt 		goto no_io;
1406c1f34302SBenjamin Herrenschmidt 	offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1407c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1408c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1409c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy IO";
1410c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_IO;
1411c1f34302SBenjamin Herrenschmidt 	res->start = offset;
1412c1f34302SBenjamin Herrenschmidt 	res->end = (offset + 0xfff) & 0xfffffffful;
1413c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate legacy IO: %pR\n", res);
1414c1f34302SBenjamin Herrenschmidt 	if (request_resource(&hose->io_resource, res)) {
1415c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1416c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve Legacy IO %pR\n",
1417c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1418c1f34302SBenjamin Herrenschmidt 		kfree(res);
1419c1f34302SBenjamin Herrenschmidt 	}
1420c1f34302SBenjamin Herrenschmidt 
1421c1f34302SBenjamin Herrenschmidt  no_io:
1422c1f34302SBenjamin Herrenschmidt 	/* Check for memory */
1423c1f34302SBenjamin Herrenschmidt 	offset = hose->pci_mem_offset;
1424c1f34302SBenjamin Herrenschmidt 	pr_debug("hose mem offset: %016llx\n", (unsigned long long)offset);
1425c1f34302SBenjamin Herrenschmidt 	for (i = 0; i < 3; i++) {
1426c1f34302SBenjamin Herrenschmidt 		pres = &hose->mem_resources[i];
1427c1f34302SBenjamin Herrenschmidt 		if (!(pres->flags & IORESOURCE_MEM))
1428c1f34302SBenjamin Herrenschmidt 			continue;
1429c1f34302SBenjamin Herrenschmidt 		pr_debug("hose mem res: %pR\n", pres);
1430c1f34302SBenjamin Herrenschmidt 		if ((pres->start - offset) <= 0xa0000 &&
1431c1f34302SBenjamin Herrenschmidt 		    (pres->end - offset) >= 0xbffff)
1432c1f34302SBenjamin Herrenschmidt 			break;
1433c1f34302SBenjamin Herrenschmidt 	}
1434c1f34302SBenjamin Herrenschmidt 	if (i >= 3)
1435c1f34302SBenjamin Herrenschmidt 		return;
1436c1f34302SBenjamin Herrenschmidt 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1437c1f34302SBenjamin Herrenschmidt 	BUG_ON(res == NULL);
1438c1f34302SBenjamin Herrenschmidt 	res->name = "Legacy VGA memory";
1439c1f34302SBenjamin Herrenschmidt 	res->flags = IORESOURCE_MEM;
1440c1f34302SBenjamin Herrenschmidt 	res->start = 0xa0000 + offset;
1441c1f34302SBenjamin Herrenschmidt 	res->end = 0xbffff + offset;
1442c1f34302SBenjamin Herrenschmidt 	pr_debug("Candidate VGA memory: %pR\n", res);
1443c1f34302SBenjamin Herrenschmidt 	if (request_resource(pres, res)) {
1444c1f34302SBenjamin Herrenschmidt 		printk(KERN_DEBUG
1445c1f34302SBenjamin Herrenschmidt 		       "PCI %04x:%02x Cannot reserve VGA memory %pR\n",
1446c1f34302SBenjamin Herrenschmidt 		       pci_domain_nr(bus), bus->number, res);
1447c1f34302SBenjamin Herrenschmidt 		kfree(res);
1448c1f34302SBenjamin Herrenschmidt 	}
1449c1f34302SBenjamin Herrenschmidt }
1450c1f34302SBenjamin Herrenschmidt 
14513fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void)
14523fd94c6bSBenjamin Herrenschmidt {
1453e90a1318SNathan Fontenot 	struct pci_bus *b;
1454e90a1318SNathan Fontenot 
14553fd94c6bSBenjamin Herrenschmidt 	/* Allocate and assign resources. If we re-assign everything, then
14563fd94c6bSBenjamin Herrenschmidt 	 * we skip the allocate phase
14573fd94c6bSBenjamin Herrenschmidt 	 */
1458e90a1318SNathan Fontenot 	list_for_each_entry(b, &pci_root_buses, node)
1459e90a1318SNathan Fontenot 		pcibios_allocate_bus_resources(b);
14603fd94c6bSBenjamin Herrenschmidt 
14613fd94c6bSBenjamin Herrenschmidt 	if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
14623fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(0);
14633fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(1);
14643fd94c6bSBenjamin Herrenschmidt 	}
14653fd94c6bSBenjamin Herrenschmidt 
1466c1f34302SBenjamin Herrenschmidt 	/* Before we start assigning unassigned resource, we try to reserve
1467c1f34302SBenjamin Herrenschmidt 	 * the low IO area and the VGA memory area if they intersect the
1468c1f34302SBenjamin Herrenschmidt 	 * bus available resources to avoid allocating things on top of them
1469c1f34302SBenjamin Herrenschmidt 	 */
1470c1f34302SBenjamin Herrenschmidt 	if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
1471c1f34302SBenjamin Herrenschmidt 		list_for_each_entry(b, &pci_root_buses, node)
1472c1f34302SBenjamin Herrenschmidt 			pcibios_reserve_legacy_regions(b);
1473c1f34302SBenjamin Herrenschmidt 	}
1474c1f34302SBenjamin Herrenschmidt 
1475c1f34302SBenjamin Herrenschmidt 	/* Now, if the platform didn't decide to blindly trust the firmware,
1476c1f34302SBenjamin Herrenschmidt 	 * we proceed to assigning things that were left unassigned
1477c1f34302SBenjamin Herrenschmidt 	 */
14783fd94c6bSBenjamin Herrenschmidt 	if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
1479a77acda0SWolfram Sang 		pr_debug("PCI: Assigning unassigned resources...\n");
14803fd94c6bSBenjamin Herrenschmidt 		pci_assign_unassigned_resources();
14813fd94c6bSBenjamin Herrenschmidt 	}
14823fd94c6bSBenjamin Herrenschmidt 
14833fd94c6bSBenjamin Herrenschmidt 	/* Call machine dependent fixup */
14843fd94c6bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup)
14853fd94c6bSBenjamin Herrenschmidt 		ppc_md.pcibios_fixup();
14863fd94c6bSBenjamin Herrenschmidt }
14873fd94c6bSBenjamin Herrenschmidt 
14883fd94c6bSBenjamin Herrenschmidt #ifdef CONFIG_HOTPLUG
14898b8da358SBenjamin Herrenschmidt 
1490fd6852c8SBenjamin Herrenschmidt /* This is used by the PCI hotplug driver to allocate resource
14913fd94c6bSBenjamin Herrenschmidt  * of newly plugged busses. We can try to consolidate with the
1492fd6852c8SBenjamin Herrenschmidt  * rest of the code later, for now, keep it as-is as our main
1493fd6852c8SBenjamin Herrenschmidt  * resource allocation function doesn't deal with sub-trees yet.
14943fd94c6bSBenjamin Herrenschmidt  */
1495baf75b0aSStephen Rothwell void pcibios_claim_one_bus(struct pci_bus *bus)
14963fd94c6bSBenjamin Herrenschmidt {
14973fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev;
14983fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *child_bus;
14993fd94c6bSBenjamin Herrenschmidt 
15003fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
15013fd94c6bSBenjamin Herrenschmidt 		int i;
15023fd94c6bSBenjamin Herrenschmidt 
15033fd94c6bSBenjamin Herrenschmidt 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
15043fd94c6bSBenjamin Herrenschmidt 			struct resource *r = &dev->resource[i];
15053fd94c6bSBenjamin Herrenschmidt 
15063fd94c6bSBenjamin Herrenschmidt 			if (r->parent || !r->start || !r->flags)
15073fd94c6bSBenjamin Herrenschmidt 				continue;
1508fd6852c8SBenjamin Herrenschmidt 
1509fd6852c8SBenjamin Herrenschmidt 			pr_debug("PCI: Claiming %s: "
1510fd6852c8SBenjamin Herrenschmidt 				 "Resource %d: %016llx..%016llx [%x]\n",
1511fd6852c8SBenjamin Herrenschmidt 				 pci_name(dev), i,
1512fd6852c8SBenjamin Herrenschmidt 				 (unsigned long long)r->start,
1513fd6852c8SBenjamin Herrenschmidt 				 (unsigned long long)r->end,
1514fd6852c8SBenjamin Herrenschmidt 				 (unsigned int)r->flags);
1515fd6852c8SBenjamin Herrenschmidt 
15163fd94c6bSBenjamin Herrenschmidt 			pci_claim_resource(dev, i);
15173fd94c6bSBenjamin Herrenschmidt 		}
15183fd94c6bSBenjamin Herrenschmidt 	}
15193fd94c6bSBenjamin Herrenschmidt 
15203fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(child_bus, &bus->children, node)
15213fd94c6bSBenjamin Herrenschmidt 		pcibios_claim_one_bus(child_bus);
15223fd94c6bSBenjamin Herrenschmidt }
1523fd6852c8SBenjamin Herrenschmidt 
1524fd6852c8SBenjamin Herrenschmidt 
1525fd6852c8SBenjamin Herrenschmidt /* pcibios_finish_adding_to_bus
1526fd6852c8SBenjamin Herrenschmidt  *
1527fd6852c8SBenjamin Herrenschmidt  * This is to be called by the hotplug code after devices have been
1528fd6852c8SBenjamin Herrenschmidt  * added to a bus, this include calling it for a PHB that is just
1529fd6852c8SBenjamin Herrenschmidt  * being added
1530fd6852c8SBenjamin Herrenschmidt  */
1531fd6852c8SBenjamin Herrenschmidt void pcibios_finish_adding_to_bus(struct pci_bus *bus)
1532fd6852c8SBenjamin Herrenschmidt {
1533fd6852c8SBenjamin Herrenschmidt 	pr_debug("PCI: Finishing adding to hotplug bus %04x:%02x\n",
1534fd6852c8SBenjamin Herrenschmidt 		 pci_domain_nr(bus), bus->number);
1535fd6852c8SBenjamin Herrenschmidt 
1536fd6852c8SBenjamin Herrenschmidt 	/* Allocate bus and devices resources */
1537fd6852c8SBenjamin Herrenschmidt 	pcibios_allocate_bus_resources(bus);
1538fd6852c8SBenjamin Herrenschmidt 	pcibios_claim_one_bus(bus);
1539fd6852c8SBenjamin Herrenschmidt 
1540fd6852c8SBenjamin Herrenschmidt 	/* Add new devices to global lists.  Register in proc, sysfs. */
1541fd6852c8SBenjamin Herrenschmidt 	pci_bus_add_devices(bus);
1542fd6852c8SBenjamin Herrenschmidt 
1543fd6852c8SBenjamin Herrenschmidt 	/* Fixup EEH */
1544fd6852c8SBenjamin Herrenschmidt 	eeh_add_device_tree_late(bus);
1545fd6852c8SBenjamin Herrenschmidt }
1546fd6852c8SBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_finish_adding_to_bus);
1547fd6852c8SBenjamin Herrenschmidt 
15483fd94c6bSBenjamin Herrenschmidt #endif /* CONFIG_HOTPLUG */
1549549beb9bSBenjamin Herrenschmidt 
1550549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask)
1551549beb9bSBenjamin Herrenschmidt {
1552549beb9bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_enable_device_hook)
1553549beb9bSBenjamin Herrenschmidt 		if (ppc_md.pcibios_enable_device_hook(dev))
1554549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1555549beb9bSBenjamin Herrenschmidt 
15567cfb5f9aSBjorn Helgaas 	return pci_enable_resources(dev, mask);
1557549beb9bSBenjamin Herrenschmidt }
155853280323SBenjamin Herrenschmidt 
155953280323SBenjamin Herrenschmidt void __devinit pcibios_setup_phb_resources(struct pci_controller *hose)
156053280323SBenjamin Herrenschmidt {
156153280323SBenjamin Herrenschmidt 	struct pci_bus *bus = hose->bus;
156253280323SBenjamin Herrenschmidt 	struct resource *res;
156353280323SBenjamin Herrenschmidt 	int i;
156453280323SBenjamin Herrenschmidt 
156553280323SBenjamin Herrenschmidt 	/* Hookup PHB IO resource */
156653280323SBenjamin Herrenschmidt 	bus->resource[0] = res = &hose->io_resource;
156753280323SBenjamin Herrenschmidt 
156853280323SBenjamin Herrenschmidt 	if (!res->flags) {
156953280323SBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: I/O resource not set for host"
157053280323SBenjamin Herrenschmidt 		       " bridge %s (domain %d)\n",
157153280323SBenjamin Herrenschmidt 		       hose->dn->full_name, hose->global_number);
157253280323SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
157353280323SBenjamin Herrenschmidt 		/* Workaround for lack of IO resource only on 32-bit */
157453280323SBenjamin Herrenschmidt 		res->start = (unsigned long)hose->io_base_virt - isa_io_base;
157553280323SBenjamin Herrenschmidt 		res->end = res->start + IO_SPACE_LIMIT;
157653280323SBenjamin Herrenschmidt 		res->flags = IORESOURCE_IO;
157753280323SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
157853280323SBenjamin Herrenschmidt 	}
157953280323SBenjamin Herrenschmidt 
158053280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB IO resource    = %016llx-%016llx [%lx]\n",
158153280323SBenjamin Herrenschmidt 		 (unsigned long long)res->start,
158253280323SBenjamin Herrenschmidt 		 (unsigned long long)res->end,
158353280323SBenjamin Herrenschmidt 		 (unsigned long)res->flags);
158453280323SBenjamin Herrenschmidt 
158553280323SBenjamin Herrenschmidt 	/* Hookup PHB Memory resources */
158653280323SBenjamin Herrenschmidt 	for (i = 0; i < 3; ++i) {
158753280323SBenjamin Herrenschmidt 		res = &hose->mem_resources[i];
158853280323SBenjamin Herrenschmidt 		if (!res->flags) {
158953280323SBenjamin Herrenschmidt 			if (i > 0)
159053280323SBenjamin Herrenschmidt 				continue;
159153280323SBenjamin Herrenschmidt 			printk(KERN_ERR "PCI: Memory resource 0 not set for "
159253280323SBenjamin Herrenschmidt 			       "host bridge %s (domain %d)\n",
159353280323SBenjamin Herrenschmidt 			       hose->dn->full_name, hose->global_number);
159453280323SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
159553280323SBenjamin Herrenschmidt 			/* Workaround for lack of MEM resource only on 32-bit */
159653280323SBenjamin Herrenschmidt 			res->start = hose->pci_mem_offset;
159753280323SBenjamin Herrenschmidt 			res->end = (resource_size_t)-1LL;
159853280323SBenjamin Herrenschmidt 			res->flags = IORESOURCE_MEM;
159953280323SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
160053280323SBenjamin Herrenschmidt 		}
160153280323SBenjamin Herrenschmidt 		bus->resource[i+1] = res;
160253280323SBenjamin Herrenschmidt 
160353280323SBenjamin Herrenschmidt 		pr_debug("PCI: PHB MEM resource %d = %016llx-%016llx [%lx]\n", i,
160453280323SBenjamin Herrenschmidt 			 (unsigned long long)res->start,
160553280323SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
160653280323SBenjamin Herrenschmidt 			 (unsigned long)res->flags);
160753280323SBenjamin Herrenschmidt 	}
160853280323SBenjamin Herrenschmidt 
160953280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB MEM offset     = %016llx\n",
161053280323SBenjamin Herrenschmidt 		 (unsigned long long)hose->pci_mem_offset);
161153280323SBenjamin Herrenschmidt 	pr_debug("PCI: PHB IO  offset     = %08lx\n",
161253280323SBenjamin Herrenschmidt 		 (unsigned long)hose->io_base_virt - _IO_BASE);
161353280323SBenjamin Herrenschmidt 
161453280323SBenjamin Herrenschmidt }
161589c2dd62SKumar Gala 
161689c2dd62SKumar Gala /*
161789c2dd62SKumar Gala  * Null PCI config access functions, for the case when we can't
161889c2dd62SKumar Gala  * find a hose.
161989c2dd62SKumar Gala  */
162089c2dd62SKumar Gala #define NULL_PCI_OP(rw, size, type)					\
162189c2dd62SKumar Gala static int								\
162289c2dd62SKumar Gala null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
162389c2dd62SKumar Gala {									\
162489c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
162589c2dd62SKumar Gala }
162689c2dd62SKumar Gala 
162789c2dd62SKumar Gala static int
162889c2dd62SKumar Gala null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
162989c2dd62SKumar Gala 		 int len, u32 *val)
163089c2dd62SKumar Gala {
163189c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
163289c2dd62SKumar Gala }
163389c2dd62SKumar Gala 
163489c2dd62SKumar Gala static int
163589c2dd62SKumar Gala null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
163689c2dd62SKumar Gala 		  int len, u32 val)
163789c2dd62SKumar Gala {
163889c2dd62SKumar Gala 	return PCIBIOS_DEVICE_NOT_FOUND;
163989c2dd62SKumar Gala }
164089c2dd62SKumar Gala 
164189c2dd62SKumar Gala static struct pci_ops null_pci_ops =
164289c2dd62SKumar Gala {
164389c2dd62SKumar Gala 	.read = null_read_config,
164489c2dd62SKumar Gala 	.write = null_write_config,
164589c2dd62SKumar Gala };
164689c2dd62SKumar Gala 
164789c2dd62SKumar Gala /*
164889c2dd62SKumar Gala  * These functions are used early on before PCI scanning is done
164989c2dd62SKumar Gala  * and all of the pci_dev and pci_bus structures have been created.
165089c2dd62SKumar Gala  */
165189c2dd62SKumar Gala static struct pci_bus *
165289c2dd62SKumar Gala fake_pci_bus(struct pci_controller *hose, int busnr)
165389c2dd62SKumar Gala {
165489c2dd62SKumar Gala 	static struct pci_bus bus;
165589c2dd62SKumar Gala 
165689c2dd62SKumar Gala 	if (hose == 0) {
165789c2dd62SKumar Gala 		printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
165889c2dd62SKumar Gala 	}
165989c2dd62SKumar Gala 	bus.number = busnr;
166089c2dd62SKumar Gala 	bus.sysdata = hose;
166189c2dd62SKumar Gala 	bus.ops = hose? hose->ops: &null_pci_ops;
166289c2dd62SKumar Gala 	return &bus;
166389c2dd62SKumar Gala }
166489c2dd62SKumar Gala 
166589c2dd62SKumar Gala #define EARLY_PCI_OP(rw, size, type)					\
166689c2dd62SKumar Gala int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
166789c2dd62SKumar Gala 			       int devfn, int offset, type value)	\
166889c2dd62SKumar Gala {									\
166989c2dd62SKumar Gala 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
167089c2dd62SKumar Gala 					    devfn, offset, value);	\
167189c2dd62SKumar Gala }
167289c2dd62SKumar Gala 
167389c2dd62SKumar Gala EARLY_PCI_OP(read, byte, u8 *)
167489c2dd62SKumar Gala EARLY_PCI_OP(read, word, u16 *)
167589c2dd62SKumar Gala EARLY_PCI_OP(read, dword, u32 *)
167689c2dd62SKumar Gala EARLY_PCI_OP(write, byte, u8)
167789c2dd62SKumar Gala EARLY_PCI_OP(write, word, u16)
167889c2dd62SKumar Gala EARLY_PCI_OP(write, dword, u32)
167989c2dd62SKumar Gala 
168089c2dd62SKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
168189c2dd62SKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
168289c2dd62SKumar Gala 			  int cap)
168389c2dd62SKumar Gala {
168489c2dd62SKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
168589c2dd62SKumar Gala }
16860ed2c722SGrant Likely 
16870ed2c722SGrant Likely /**
16880ed2c722SGrant Likely  * pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
16890ed2c722SGrant Likely  * @hose: Pointer to the PCI host controller instance structure
16900ed2c722SGrant Likely  * @sysdata: value to use for sysdata pointer.  ppc32 and ppc64 differ here
16910ed2c722SGrant Likely  *
16920ed2c722SGrant Likely  * Note: the 'data' pointer is a temporary measure.  As 32 and 64 bit
16930ed2c722SGrant Likely  * pci code gets merged, this parameter should become unnecessary because
16940ed2c722SGrant Likely  * both will use the same value.
16950ed2c722SGrant Likely  */
16960ed2c722SGrant Likely void __devinit pcibios_scan_phb(struct pci_controller *hose, void *sysdata)
16970ed2c722SGrant Likely {
16980ed2c722SGrant Likely 	struct pci_bus *bus;
16990ed2c722SGrant Likely 	struct device_node *node = hose->dn;
17000ed2c722SGrant Likely 	int mode;
17010ed2c722SGrant Likely 
17020ed2c722SGrant Likely 	pr_debug("PCI: Scanning PHB %s\n",
17030ed2c722SGrant Likely 		 node ? node->full_name : "<NO NAME>");
17040ed2c722SGrant Likely 
17050ed2c722SGrant Likely 	/* Create an empty bus for the toplevel */
17060ed2c722SGrant Likely 	bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops,
17070ed2c722SGrant Likely 			     sysdata);
17080ed2c722SGrant Likely 	if (bus == NULL) {
17090ed2c722SGrant Likely 		pr_err("Failed to create bus for PCI domain %04x\n",
17100ed2c722SGrant Likely 			hose->global_number);
17110ed2c722SGrant Likely 		return;
17120ed2c722SGrant Likely 	}
17130ed2c722SGrant Likely 	bus->secondary = hose->first_busno;
17140ed2c722SGrant Likely 	hose->bus = bus;
17150ed2c722SGrant Likely 
17160ed2c722SGrant Likely 	/* Get some IO space for the new PHB */
17170ed2c722SGrant Likely 	pcibios_setup_phb_io_space(hose);
17180ed2c722SGrant Likely 
17190ed2c722SGrant Likely 	/* Wire up PHB bus resources */
17200ed2c722SGrant Likely 	pcibios_setup_phb_resources(hose);
17210ed2c722SGrant Likely 
17220ed2c722SGrant Likely 	/* Get probe mode and perform scan */
17230ed2c722SGrant Likely 	mode = PCI_PROBE_NORMAL;
17240ed2c722SGrant Likely 	if (node && ppc_md.pci_probe_mode)
17250ed2c722SGrant Likely 		mode = ppc_md.pci_probe_mode(bus);
17260ed2c722SGrant Likely 	pr_debug("    probe mode: %d\n", mode);
17270ed2c722SGrant Likely 	if (mode == PCI_PROBE_DEVTREE) {
17280ed2c722SGrant Likely 		bus->subordinate = hose->last_busno;
17290ed2c722SGrant Likely 		of_scan_bus(node, bus);
17300ed2c722SGrant Likely 	}
17310ed2c722SGrant Likely 
17320ed2c722SGrant Likely 	if (mode == PCI_PROBE_NORMAL)
17330ed2c722SGrant Likely 		hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
17340ed2c722SGrant Likely }
1735