xref: /openbmc/linux/arch/powerpc/kernel/pci-common.c (revision 54a24cbbd0184faffc37c39cd3a896f4ddac3e03)
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 #undef DEBUG
205516b540SKumar Gala 
215516b540SKumar Gala #include <linux/kernel.h>
225516b540SKumar Gala #include <linux/pci.h>
235516b540SKumar Gala #include <linux/string.h>
245516b540SKumar Gala #include <linux/init.h>
255516b540SKumar Gala #include <linux/bootmem.h>
265516b540SKumar Gala #include <linux/mm.h>
275516b540SKumar Gala #include <linux/list.h>
285516b540SKumar Gala #include <linux/syscalls.h>
295516b540SKumar Gala #include <linux/irq.h>
305516b540SKumar Gala #include <linux/vmalloc.h>
315516b540SKumar Gala 
325516b540SKumar Gala #include <asm/processor.h>
335516b540SKumar Gala #include <asm/io.h>
345516b540SKumar Gala #include <asm/prom.h>
355516b540SKumar Gala #include <asm/pci-bridge.h>
365516b540SKumar Gala #include <asm/byteorder.h>
375516b540SKumar Gala #include <asm/machdep.h>
385516b540SKumar Gala #include <asm/ppc-pci.h>
395516b540SKumar Gala #include <asm/firmware.h>
405516b540SKumar Gala 
415516b540SKumar Gala #ifdef DEBUG
425516b540SKumar Gala #include <asm/udbg.h>
435516b540SKumar Gala #define DBG(fmt...) printk(fmt)
445516b540SKumar Gala #else
455516b540SKumar Gala #define DBG(fmt...)
465516b540SKumar Gala #endif
475516b540SKumar Gala 
48a4c9e328SKumar Gala static DEFINE_SPINLOCK(hose_spinlock);
49a4c9e328SKumar Gala 
50a4c9e328SKumar Gala /* XXX kill that some day ... */
51ebfc00f7SStephen Rothwell static int global_phb_number;		/* Global phb counter */
52a4c9e328SKumar Gala 
5325e81f92SBenjamin Herrenschmidt /* ISA Memory physical address */
5425e81f92SBenjamin Herrenschmidt resource_size_t isa_mem_base;
5525e81f92SBenjamin Herrenschmidt 
563fd94c6bSBenjamin Herrenschmidt /* Default PCI flags is 0 */
573fd94c6bSBenjamin Herrenschmidt unsigned int ppc_pci_flags;
58a4c9e328SKumar Gala 
592d5f5659SLinas Vepstas struct pci_controller *pcibios_alloc_controller(struct device_node *dev)
60a4c9e328SKumar Gala {
61a4c9e328SKumar Gala 	struct pci_controller *phb;
62a4c9e328SKumar Gala 
63e60516e3SStephen Rothwell 	phb = zalloc_maybe_bootmem(sizeof(struct pci_controller), GFP_KERNEL);
64a4c9e328SKumar Gala 	if (phb == NULL)
65a4c9e328SKumar Gala 		return NULL;
66e60516e3SStephen Rothwell 	spin_lock(&hose_spinlock);
67e60516e3SStephen Rothwell 	phb->global_number = global_phb_number++;
68e60516e3SStephen Rothwell 	list_add_tail(&phb->list_node, &hose_list);
69e60516e3SStephen Rothwell 	spin_unlock(&hose_spinlock);
7044ef3390SStephen Rothwell 	phb->dn = dev;
71a4c9e328SKumar Gala 	phb->is_dynamic = mem_init_done;
72a4c9e328SKumar Gala #ifdef CONFIG_PPC64
73a4c9e328SKumar Gala 	if (dev) {
74a4c9e328SKumar Gala 		int nid = of_node_to_nid(dev);
75a4c9e328SKumar Gala 
76a4c9e328SKumar Gala 		if (nid < 0 || !node_online(nid))
77a4c9e328SKumar Gala 			nid = -1;
78a4c9e328SKumar Gala 
79a4c9e328SKumar Gala 		PHB_SET_NODE(phb, nid);
80a4c9e328SKumar Gala 	}
81a4c9e328SKumar Gala #endif
82a4c9e328SKumar Gala 	return phb;
83a4c9e328SKumar Gala }
84a4c9e328SKumar Gala 
85a4c9e328SKumar Gala void pcibios_free_controller(struct pci_controller *phb)
86a4c9e328SKumar Gala {
87a4c9e328SKumar Gala 	spin_lock(&hose_spinlock);
88a4c9e328SKumar Gala 	list_del(&phb->list_node);
89a4c9e328SKumar Gala 	spin_unlock(&hose_spinlock);
90a4c9e328SKumar Gala 
91a4c9e328SKumar Gala 	if (phb->is_dynamic)
92a4c9e328SKumar Gala 		kfree(phb);
93a4c9e328SKumar Gala }
94a4c9e328SKumar Gala 
956dfbde20SBenjamin Herrenschmidt int pcibios_vaddr_is_ioport(void __iomem *address)
966dfbde20SBenjamin Herrenschmidt {
976dfbde20SBenjamin Herrenschmidt 	int ret = 0;
986dfbde20SBenjamin Herrenschmidt 	struct pci_controller *hose;
996dfbde20SBenjamin Herrenschmidt 	unsigned long size;
1006dfbde20SBenjamin Herrenschmidt 
1016dfbde20SBenjamin Herrenschmidt 	spin_lock(&hose_spinlock);
1026dfbde20SBenjamin Herrenschmidt 	list_for_each_entry(hose, &hose_list, list_node) {
1036dfbde20SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
1046dfbde20SBenjamin Herrenschmidt 		size = hose->pci_io_size;
1056dfbde20SBenjamin Herrenschmidt #else
1066dfbde20SBenjamin Herrenschmidt 		size = hose->io_resource.end - hose->io_resource.start + 1;
1076dfbde20SBenjamin Herrenschmidt #endif
1086dfbde20SBenjamin Herrenschmidt 		if (address >= hose->io_base_virt &&
1096dfbde20SBenjamin Herrenschmidt 		    address < (hose->io_base_virt + size)) {
1106dfbde20SBenjamin Herrenschmidt 			ret = 1;
1116dfbde20SBenjamin Herrenschmidt 			break;
1126dfbde20SBenjamin Herrenschmidt 		}
1136dfbde20SBenjamin Herrenschmidt 	}
1146dfbde20SBenjamin Herrenschmidt 	spin_unlock(&hose_spinlock);
1156dfbde20SBenjamin Herrenschmidt 	return ret;
1166dfbde20SBenjamin Herrenschmidt }
1176dfbde20SBenjamin Herrenschmidt 
1185516b540SKumar Gala /*
1195516b540SKumar Gala  * Return the domain number for this bus.
1205516b540SKumar Gala  */
1215516b540SKumar Gala int pci_domain_nr(struct pci_bus *bus)
1225516b540SKumar Gala {
1235516b540SKumar Gala 	struct pci_controller *hose = pci_bus_to_host(bus);
1245516b540SKumar Gala 
1255516b540SKumar Gala 	return hose->global_number;
1265516b540SKumar Gala }
1275516b540SKumar Gala EXPORT_SYMBOL(pci_domain_nr);
12858083dadSKumar Gala 
12958083dadSKumar Gala #ifdef CONFIG_PPC_OF
130a4c9e328SKumar Gala 
131a4c9e328SKumar Gala /* This routine is meant to be used early during boot, when the
132a4c9e328SKumar Gala  * PCI bus numbers have not yet been assigned, and you need to
133a4c9e328SKumar Gala  * issue PCI config cycles to an OF device.
134a4c9e328SKumar Gala  * It could also be used to "fix" RTAS config cycles if you want
135a4c9e328SKumar Gala  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
136a4c9e328SKumar Gala  * config cycles.
137a4c9e328SKumar Gala  */
138a4c9e328SKumar Gala struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
139a4c9e328SKumar Gala {
140a4c9e328SKumar Gala 	if (!have_of)
141a4c9e328SKumar Gala 		return NULL;
142a4c9e328SKumar Gala 	while(node) {
143a4c9e328SKumar Gala 		struct pci_controller *hose, *tmp;
144a4c9e328SKumar Gala 		list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
14544ef3390SStephen Rothwell 			if (hose->dn == node)
146a4c9e328SKumar Gala 				return hose;
147a4c9e328SKumar Gala 		node = node->parent;
148a4c9e328SKumar Gala 	}
149a4c9e328SKumar Gala 	return NULL;
150a4c9e328SKumar Gala }
151a4c9e328SKumar Gala 
15258083dadSKumar Gala static ssize_t pci_show_devspec(struct device *dev,
15358083dadSKumar Gala 		struct device_attribute *attr, char *buf)
15458083dadSKumar Gala {
15558083dadSKumar Gala 	struct pci_dev *pdev;
15658083dadSKumar Gala 	struct device_node *np;
15758083dadSKumar Gala 
15858083dadSKumar Gala 	pdev = to_pci_dev (dev);
15958083dadSKumar Gala 	np = pci_device_to_OF_node(pdev);
16058083dadSKumar Gala 	if (np == NULL || np->full_name == NULL)
16158083dadSKumar Gala 		return 0;
16258083dadSKumar Gala 	return sprintf(buf, "%s", np->full_name);
16358083dadSKumar Gala }
16458083dadSKumar Gala static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
16558083dadSKumar Gala #endif /* CONFIG_PPC_OF */
16658083dadSKumar Gala 
16758083dadSKumar Gala /* Add sysfs properties */
1684f3731daSTony Breeds int pcibios_add_platform_entries(struct pci_dev *pdev)
16958083dadSKumar Gala {
17058083dadSKumar Gala #ifdef CONFIG_PPC_OF
1714f3731daSTony Breeds 	return device_create_file(&pdev->dev, &dev_attr_devspec);
1724f3731daSTony Breeds #else
1734f3731daSTony Breeds 	return 0;
17458083dadSKumar Gala #endif /* CONFIG_PPC_OF */
1754f3731daSTony Breeds 
17658083dadSKumar Gala }
17758083dadSKumar Gala 
178a2b7390aSStephen Rothwell char __devinit *pcibios_setup(char *str)
17958083dadSKumar Gala {
18058083dadSKumar Gala 	return str;
18158083dadSKumar Gala }
18258083dadSKumar Gala 
18358083dadSKumar Gala /*
18458083dadSKumar Gala  * Reads the interrupt pin to determine if interrupt is use by card.
18558083dadSKumar Gala  * If the interrupt is used, then gets the interrupt line from the
18658083dadSKumar Gala  * openfirmware and sets it in the pci_dev and pci_config line.
18758083dadSKumar Gala  */
18858083dadSKumar Gala int pci_read_irq_line(struct pci_dev *pci_dev)
18958083dadSKumar Gala {
19058083dadSKumar Gala 	struct of_irq oirq;
19158083dadSKumar Gala 	unsigned int virq;
19258083dadSKumar Gala 
19350c9bc2fSBenjamin Herrenschmidt 	/* The current device-tree that iSeries generates from the HV
19450c9bc2fSBenjamin Herrenschmidt 	 * PCI informations doesn't contain proper interrupt routing,
19550c9bc2fSBenjamin Herrenschmidt 	 * and all the fallback would do is print out crap, so we
19650c9bc2fSBenjamin Herrenschmidt 	 * don't attempt to resolve the interrupts here at all, some
19750c9bc2fSBenjamin Herrenschmidt 	 * iSeries specific fixup does it.
19850c9bc2fSBenjamin Herrenschmidt 	 *
19950c9bc2fSBenjamin Herrenschmidt 	 * In the long run, we will hopefully fix the generated device-tree
20050c9bc2fSBenjamin Herrenschmidt 	 * instead.
20150c9bc2fSBenjamin Herrenschmidt 	 */
20250c9bc2fSBenjamin Herrenschmidt #ifdef CONFIG_PPC_ISERIES
20350c9bc2fSBenjamin Herrenschmidt 	if (firmware_has_feature(FW_FEATURE_ISERIES))
20450c9bc2fSBenjamin Herrenschmidt 		return -1;
20550c9bc2fSBenjamin Herrenschmidt #endif
20650c9bc2fSBenjamin Herrenschmidt 
20758083dadSKumar Gala 	DBG("Try to map irq for %s...\n", pci_name(pci_dev));
20858083dadSKumar Gala 
20958083dadSKumar Gala #ifdef DEBUG
21058083dadSKumar Gala 	memset(&oirq, 0xff, sizeof(oirq));
21158083dadSKumar Gala #endif
21258083dadSKumar Gala 	/* Try to get a mapping from the device-tree */
21358083dadSKumar Gala 	if (of_irq_map_pci(pci_dev, &oirq)) {
21458083dadSKumar Gala 		u8 line, pin;
21558083dadSKumar Gala 
21658083dadSKumar Gala 		/* If that fails, lets fallback to what is in the config
21758083dadSKumar Gala 		 * space and map that through the default controller. We
21858083dadSKumar Gala 		 * also set the type to level low since that's what PCI
21958083dadSKumar Gala 		 * interrupts are. If your platform does differently, then
22058083dadSKumar Gala 		 * either provide a proper interrupt tree or don't use this
22158083dadSKumar Gala 		 * function.
22258083dadSKumar Gala 		 */
22358083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
22458083dadSKumar Gala 			return -1;
22558083dadSKumar Gala 		if (pin == 0)
22658083dadSKumar Gala 			return -1;
22758083dadSKumar Gala 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
228*54a24cbbSBenjamin Herrenschmidt 		    line == 0xff || line == 0) {
22958083dadSKumar Gala 			return -1;
23058083dadSKumar Gala 		}
231*54a24cbbSBenjamin Herrenschmidt 		DBG(" -> no map ! Using line %d (pin %d) from PCI config\n",
232*54a24cbbSBenjamin Herrenschmidt 		    line, pin);
23358083dadSKumar Gala 
23458083dadSKumar Gala 		virq = irq_create_mapping(NULL, line);
23558083dadSKumar Gala 		if (virq != NO_IRQ)
23658083dadSKumar Gala 			set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
23758083dadSKumar Gala 	} else {
23858083dadSKumar Gala 		DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
23958083dadSKumar Gala 		    oirq.size, oirq.specifier[0], oirq.specifier[1],
24058083dadSKumar Gala 		    oirq.controller->full_name);
24158083dadSKumar Gala 
24258083dadSKumar Gala 		virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
24358083dadSKumar Gala 					     oirq.size);
24458083dadSKumar Gala 	}
24558083dadSKumar Gala 	if(virq == NO_IRQ) {
24658083dadSKumar Gala 		DBG(" -> failed to map !\n");
24758083dadSKumar Gala 		return -1;
24858083dadSKumar Gala 	}
24958083dadSKumar Gala 
25058083dadSKumar Gala 	DBG(" -> mapped to linux irq %d\n", virq);
25158083dadSKumar Gala 
25258083dadSKumar Gala 	pci_dev->irq = virq;
25358083dadSKumar Gala 
25458083dadSKumar Gala 	return 0;
25558083dadSKumar Gala }
25658083dadSKumar Gala EXPORT_SYMBOL(pci_read_irq_line);
25758083dadSKumar Gala 
25858083dadSKumar Gala /*
25958083dadSKumar Gala  * Platform support for /proc/bus/pci/X/Y mmap()s,
26058083dadSKumar Gala  * modelled on the sparc64 implementation by Dave Miller.
26158083dadSKumar Gala  *  -- paulus.
26258083dadSKumar Gala  */
26358083dadSKumar Gala 
26458083dadSKumar Gala /*
26558083dadSKumar Gala  * Adjust vm_pgoff of VMA such that it is the physical page offset
26658083dadSKumar Gala  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
26758083dadSKumar Gala  *
26858083dadSKumar Gala  * Basically, the user finds the base address for his device which he wishes
26958083dadSKumar Gala  * to mmap.  They read the 32-bit value from the config space base register,
27058083dadSKumar Gala  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
27158083dadSKumar Gala  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
27258083dadSKumar Gala  *
27358083dadSKumar Gala  * Returns negative error code on failure, zero on success.
27458083dadSKumar Gala  */
27558083dadSKumar Gala static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
27658083dadSKumar Gala 					       resource_size_t *offset,
27758083dadSKumar Gala 					       enum pci_mmap_state mmap_state)
27858083dadSKumar Gala {
27958083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
28058083dadSKumar Gala 	unsigned long io_offset = 0;
28158083dadSKumar Gala 	int i, res_bit;
28258083dadSKumar Gala 
28358083dadSKumar Gala 	if (hose == 0)
28458083dadSKumar Gala 		return NULL;		/* should never happen */
28558083dadSKumar Gala 
28658083dadSKumar Gala 	/* If memory, add on the PCI bridge address offset */
28758083dadSKumar Gala 	if (mmap_state == pci_mmap_mem) {
28858083dadSKumar Gala #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
28958083dadSKumar Gala 		*offset += hose->pci_mem_offset;
29058083dadSKumar Gala #endif
29158083dadSKumar Gala 		res_bit = IORESOURCE_MEM;
29258083dadSKumar Gala 	} else {
29358083dadSKumar Gala 		io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
29458083dadSKumar Gala 		*offset += io_offset;
29558083dadSKumar Gala 		res_bit = IORESOURCE_IO;
29658083dadSKumar Gala 	}
29758083dadSKumar Gala 
29858083dadSKumar Gala 	/*
29958083dadSKumar Gala 	 * Check that the offset requested corresponds to one of the
30058083dadSKumar Gala 	 * resources of the device.
30158083dadSKumar Gala 	 */
30258083dadSKumar Gala 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
30358083dadSKumar Gala 		struct resource *rp = &dev->resource[i];
30458083dadSKumar Gala 		int flags = rp->flags;
30558083dadSKumar Gala 
30658083dadSKumar Gala 		/* treat ROM as memory (should be already) */
30758083dadSKumar Gala 		if (i == PCI_ROM_RESOURCE)
30858083dadSKumar Gala 			flags |= IORESOURCE_MEM;
30958083dadSKumar Gala 
31058083dadSKumar Gala 		/* Active and same type? */
31158083dadSKumar Gala 		if ((flags & res_bit) == 0)
31258083dadSKumar Gala 			continue;
31358083dadSKumar Gala 
31458083dadSKumar Gala 		/* In the range of this resource? */
31558083dadSKumar Gala 		if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
31658083dadSKumar Gala 			continue;
31758083dadSKumar Gala 
31858083dadSKumar Gala 		/* found it! construct the final physical address */
31958083dadSKumar Gala 		if (mmap_state == pci_mmap_io)
32058083dadSKumar Gala 			*offset += hose->io_base_phys - io_offset;
32158083dadSKumar Gala 		return rp;
32258083dadSKumar Gala 	}
32358083dadSKumar Gala 
32458083dadSKumar Gala 	return NULL;
32558083dadSKumar Gala }
32658083dadSKumar Gala 
32758083dadSKumar Gala /*
32858083dadSKumar Gala  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
32958083dadSKumar Gala  * device mapping.
33058083dadSKumar Gala  */
33158083dadSKumar Gala static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
33258083dadSKumar Gala 				      pgprot_t protection,
33358083dadSKumar Gala 				      enum pci_mmap_state mmap_state,
33458083dadSKumar Gala 				      int write_combine)
33558083dadSKumar Gala {
33658083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
33758083dadSKumar Gala 
33858083dadSKumar Gala 	/* Write combine is always 0 on non-memory space mappings. On
33958083dadSKumar Gala 	 * memory space, if the user didn't pass 1, we check for a
34058083dadSKumar Gala 	 * "prefetchable" resource. This is a bit hackish, but we use
34158083dadSKumar Gala 	 * this to workaround the inability of /sysfs to provide a write
34258083dadSKumar Gala 	 * combine bit
34358083dadSKumar Gala 	 */
34458083dadSKumar Gala 	if (mmap_state != pci_mmap_mem)
34558083dadSKumar Gala 		write_combine = 0;
34658083dadSKumar Gala 	else if (write_combine == 0) {
34758083dadSKumar Gala 		if (rp->flags & IORESOURCE_PREFETCH)
34858083dadSKumar Gala 			write_combine = 1;
34958083dadSKumar Gala 	}
35058083dadSKumar Gala 
35158083dadSKumar Gala 	/* XXX would be nice to have a way to ask for write-through */
35258083dadSKumar Gala 	prot |= _PAGE_NO_CACHE;
35358083dadSKumar Gala 	if (write_combine)
35458083dadSKumar Gala 		prot &= ~_PAGE_GUARDED;
35558083dadSKumar Gala 	else
35658083dadSKumar Gala 		prot |= _PAGE_GUARDED;
35758083dadSKumar Gala 
35858083dadSKumar Gala 	return __pgprot(prot);
35958083dadSKumar Gala }
36058083dadSKumar Gala 
36158083dadSKumar Gala /*
36258083dadSKumar Gala  * This one is used by /dev/mem and fbdev who have no clue about the
36358083dadSKumar Gala  * PCI device, it tries to find the PCI device first and calls the
36458083dadSKumar Gala  * above routine
36558083dadSKumar Gala  */
36658083dadSKumar Gala pgprot_t pci_phys_mem_access_prot(struct file *file,
36758083dadSKumar Gala 				  unsigned long pfn,
36858083dadSKumar Gala 				  unsigned long size,
36958083dadSKumar Gala 				  pgprot_t protection)
37058083dadSKumar Gala {
37158083dadSKumar Gala 	struct pci_dev *pdev = NULL;
37258083dadSKumar Gala 	struct resource *found = NULL;
37358083dadSKumar Gala 	unsigned long prot = pgprot_val(protection);
37458083dadSKumar Gala 	unsigned long offset = pfn << PAGE_SHIFT;
37558083dadSKumar Gala 	int i;
37658083dadSKumar Gala 
37758083dadSKumar Gala 	if (page_is_ram(pfn))
37858083dadSKumar Gala 		return __pgprot(prot);
37958083dadSKumar Gala 
38058083dadSKumar Gala 	prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
38158083dadSKumar Gala 
38258083dadSKumar Gala 	for_each_pci_dev(pdev) {
38358083dadSKumar Gala 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
38458083dadSKumar Gala 			struct resource *rp = &pdev->resource[i];
38558083dadSKumar Gala 			int flags = rp->flags;
38658083dadSKumar Gala 
38758083dadSKumar Gala 			/* Active and same type? */
38858083dadSKumar Gala 			if ((flags & IORESOURCE_MEM) == 0)
38958083dadSKumar Gala 				continue;
39058083dadSKumar Gala 			/* In the range of this resource? */
39158083dadSKumar Gala 			if (offset < (rp->start & PAGE_MASK) ||
39258083dadSKumar Gala 			    offset > rp->end)
39358083dadSKumar Gala 				continue;
39458083dadSKumar Gala 			found = rp;
39558083dadSKumar Gala 			break;
39658083dadSKumar Gala 		}
39758083dadSKumar Gala 		if (found)
39858083dadSKumar Gala 			break;
39958083dadSKumar Gala 	}
40058083dadSKumar Gala 	if (found) {
40158083dadSKumar Gala 		if (found->flags & IORESOURCE_PREFETCH)
40258083dadSKumar Gala 			prot &= ~_PAGE_GUARDED;
40358083dadSKumar Gala 		pci_dev_put(pdev);
40458083dadSKumar Gala 	}
40558083dadSKumar Gala 
40658083dadSKumar Gala 	DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
40758083dadSKumar Gala 
40858083dadSKumar Gala 	return __pgprot(prot);
40958083dadSKumar Gala }
41058083dadSKumar Gala 
41158083dadSKumar Gala 
41258083dadSKumar Gala /*
41358083dadSKumar Gala  * Perform the actual remap of the pages for a PCI device mapping, as
41458083dadSKumar Gala  * appropriate for this architecture.  The region in the process to map
41558083dadSKumar Gala  * is described by vm_start and vm_end members of VMA, the base physical
41658083dadSKumar Gala  * address is found in vm_pgoff.
41758083dadSKumar Gala  * The pci device structure is provided so that architectures may make mapping
41858083dadSKumar Gala  * decisions on a per-device or per-bus basis.
41958083dadSKumar Gala  *
42058083dadSKumar Gala  * Returns a negative error code on failure, zero on success.
42158083dadSKumar Gala  */
42258083dadSKumar Gala int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
42358083dadSKumar Gala 			enum pci_mmap_state mmap_state, int write_combine)
42458083dadSKumar Gala {
42558083dadSKumar Gala 	resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
42658083dadSKumar Gala 	struct resource *rp;
42758083dadSKumar Gala 	int ret;
42858083dadSKumar Gala 
42958083dadSKumar Gala 	rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
43058083dadSKumar Gala 	if (rp == NULL)
43158083dadSKumar Gala 		return -EINVAL;
43258083dadSKumar Gala 
43358083dadSKumar Gala 	vma->vm_pgoff = offset >> PAGE_SHIFT;
43458083dadSKumar Gala 	vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
43558083dadSKumar Gala 						  vma->vm_page_prot,
43658083dadSKumar Gala 						  mmap_state, write_combine);
43758083dadSKumar Gala 
43858083dadSKumar Gala 	ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
43958083dadSKumar Gala 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
44058083dadSKumar Gala 
44158083dadSKumar Gala 	return ret;
44258083dadSKumar Gala }
44358083dadSKumar Gala 
44458083dadSKumar Gala void pci_resource_to_user(const struct pci_dev *dev, int bar,
44558083dadSKumar Gala 			  const struct resource *rsrc,
44658083dadSKumar Gala 			  resource_size_t *start, resource_size_t *end)
44758083dadSKumar Gala {
44858083dadSKumar Gala 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
44958083dadSKumar Gala 	resource_size_t offset = 0;
45058083dadSKumar Gala 
45158083dadSKumar Gala 	if (hose == NULL)
45258083dadSKumar Gala 		return;
45358083dadSKumar Gala 
45458083dadSKumar Gala 	if (rsrc->flags & IORESOURCE_IO)
45558083dadSKumar Gala 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
45658083dadSKumar Gala 
45758083dadSKumar Gala 	/* We pass a fully fixed up address to userland for MMIO instead of
45858083dadSKumar Gala 	 * a BAR value because X is lame and expects to be able to use that
45958083dadSKumar Gala 	 * to pass to /dev/mem !
46058083dadSKumar Gala 	 *
46158083dadSKumar Gala 	 * That means that we'll have potentially 64 bits values where some
46258083dadSKumar Gala 	 * userland apps only expect 32 (like X itself since it thinks only
46358083dadSKumar Gala 	 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
46458083dadSKumar Gala 	 * 32 bits CHRPs :-(
46558083dadSKumar Gala 	 *
46658083dadSKumar Gala 	 * Hopefully, the sysfs insterface is immune to that gunk. Once X
46758083dadSKumar Gala 	 * has been fixed (and the fix spread enough), we can re-enable the
46858083dadSKumar Gala 	 * 2 lines below and pass down a BAR value to userland. In that case
46958083dadSKumar Gala 	 * we'll also have to re-enable the matching code in
47058083dadSKumar Gala 	 * __pci_mmap_make_offset().
47158083dadSKumar Gala 	 *
47258083dadSKumar Gala 	 * BenH.
47358083dadSKumar Gala 	 */
47458083dadSKumar Gala #if 0
47558083dadSKumar Gala 	else if (rsrc->flags & IORESOURCE_MEM)
47658083dadSKumar Gala 		offset = hose->pci_mem_offset;
47758083dadSKumar Gala #endif
47858083dadSKumar Gala 
47958083dadSKumar Gala 	*start = rsrc->start - offset;
48058083dadSKumar Gala 	*end = rsrc->end - offset;
48158083dadSKumar Gala }
48213dccb9eSBenjamin Herrenschmidt 
48313dccb9eSBenjamin Herrenschmidt /**
48413dccb9eSBenjamin Herrenschmidt  * pci_process_bridge_OF_ranges - Parse PCI bridge resources from device tree
48513dccb9eSBenjamin Herrenschmidt  * @hose: newly allocated pci_controller to be setup
48613dccb9eSBenjamin Herrenschmidt  * @dev: device node of the host bridge
48713dccb9eSBenjamin Herrenschmidt  * @primary: set if primary bus (32 bits only, soon to be deprecated)
48813dccb9eSBenjamin Herrenschmidt  *
48913dccb9eSBenjamin Herrenschmidt  * This function will parse the "ranges" property of a PCI host bridge device
49013dccb9eSBenjamin Herrenschmidt  * node and setup the resource mapping of a pci controller based on its
49113dccb9eSBenjamin Herrenschmidt  * content.
49213dccb9eSBenjamin Herrenschmidt  *
49313dccb9eSBenjamin Herrenschmidt  * Life would be boring if it wasn't for a few issues that we have to deal
49413dccb9eSBenjamin Herrenschmidt  * with here:
49513dccb9eSBenjamin Herrenschmidt  *
49613dccb9eSBenjamin Herrenschmidt  *   - We can only cope with one IO space range and up to 3 Memory space
49713dccb9eSBenjamin Herrenschmidt  *     ranges. However, some machines (thanks Apple !) tend to split their
49813dccb9eSBenjamin Herrenschmidt  *     space into lots of small contiguous ranges. So we have to coalesce.
49913dccb9eSBenjamin Herrenschmidt  *
50013dccb9eSBenjamin Herrenschmidt  *   - We can only cope with all memory ranges having the same offset
50113dccb9eSBenjamin Herrenschmidt  *     between CPU addresses and PCI addresses. Unfortunately, some bridges
50213dccb9eSBenjamin Herrenschmidt  *     are setup for a large 1:1 mapping along with a small "window" which
50313dccb9eSBenjamin Herrenschmidt  *     maps PCI address 0 to some arbitrary high address of the CPU space in
50413dccb9eSBenjamin Herrenschmidt  *     order to give access to the ISA memory hole.
50513dccb9eSBenjamin Herrenschmidt  *     The way out of here that I've chosen for now is to always set the
50613dccb9eSBenjamin Herrenschmidt  *     offset based on the first resource found, then override it if we
50713dccb9eSBenjamin Herrenschmidt  *     have a different offset and the previous was set by an ISA hole.
50813dccb9eSBenjamin Herrenschmidt  *
50913dccb9eSBenjamin Herrenschmidt  *   - Some busses have IO space not starting at 0, which causes trouble with
51013dccb9eSBenjamin Herrenschmidt  *     the way we do our IO resource renumbering. The code somewhat deals with
51113dccb9eSBenjamin Herrenschmidt  *     it for 64 bits but I would expect problems on 32 bits.
51213dccb9eSBenjamin Herrenschmidt  *
51313dccb9eSBenjamin Herrenschmidt  *   - Some 32 bits platforms such as 4xx can have physical space larger than
51413dccb9eSBenjamin Herrenschmidt  *     32 bits so we need to use 64 bits values for the parsing
51513dccb9eSBenjamin Herrenschmidt  */
51613dccb9eSBenjamin Herrenschmidt void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
51713dccb9eSBenjamin Herrenschmidt 					    struct device_node *dev,
51813dccb9eSBenjamin Herrenschmidt 					    int primary)
51913dccb9eSBenjamin Herrenschmidt {
52013dccb9eSBenjamin Herrenschmidt 	const u32 *ranges;
52113dccb9eSBenjamin Herrenschmidt 	int rlen;
52213dccb9eSBenjamin Herrenschmidt 	int pna = of_n_addr_cells(dev);
52313dccb9eSBenjamin Herrenschmidt 	int np = pna + 5;
52413dccb9eSBenjamin Herrenschmidt 	int memno = 0, isa_hole = -1;
52513dccb9eSBenjamin Herrenschmidt 	u32 pci_space;
52613dccb9eSBenjamin Herrenschmidt 	unsigned long long pci_addr, cpu_addr, pci_next, cpu_next, size;
52713dccb9eSBenjamin Herrenschmidt 	unsigned long long isa_mb = 0;
52813dccb9eSBenjamin Herrenschmidt 	struct resource *res;
52913dccb9eSBenjamin Herrenschmidt 
53013dccb9eSBenjamin Herrenschmidt 	printk(KERN_INFO "PCI host bridge %s %s ranges:\n",
53113dccb9eSBenjamin Herrenschmidt 	       dev->full_name, primary ? "(primary)" : "");
53213dccb9eSBenjamin Herrenschmidt 
53313dccb9eSBenjamin Herrenschmidt 	/* Get ranges property */
53413dccb9eSBenjamin Herrenschmidt 	ranges = of_get_property(dev, "ranges", &rlen);
53513dccb9eSBenjamin Herrenschmidt 	if (ranges == NULL)
53613dccb9eSBenjamin Herrenschmidt 		return;
53713dccb9eSBenjamin Herrenschmidt 
53813dccb9eSBenjamin Herrenschmidt 	/* Parse it */
53913dccb9eSBenjamin Herrenschmidt 	while ((rlen -= np * 4) >= 0) {
54013dccb9eSBenjamin Herrenschmidt 		/* Read next ranges element */
54113dccb9eSBenjamin Herrenschmidt 		pci_space = ranges[0];
54213dccb9eSBenjamin Herrenschmidt 		pci_addr = of_read_number(ranges + 1, 2);
54313dccb9eSBenjamin Herrenschmidt 		cpu_addr = of_translate_address(dev, ranges + 3);
54413dccb9eSBenjamin Herrenschmidt 		size = of_read_number(ranges + pna + 3, 2);
54513dccb9eSBenjamin Herrenschmidt 		ranges += np;
54613dccb9eSBenjamin Herrenschmidt 		if (cpu_addr == OF_BAD_ADDR || size == 0)
54713dccb9eSBenjamin Herrenschmidt 			continue;
54813dccb9eSBenjamin Herrenschmidt 
54913dccb9eSBenjamin Herrenschmidt 		/* Now consume following elements while they are contiguous */
55013dccb9eSBenjamin Herrenschmidt 		for (; rlen >= np * sizeof(u32);
55113dccb9eSBenjamin Herrenschmidt 		     ranges += np, rlen -= np * 4) {
55213dccb9eSBenjamin Herrenschmidt 			if (ranges[0] != pci_space)
55313dccb9eSBenjamin Herrenschmidt 				break;
55413dccb9eSBenjamin Herrenschmidt 			pci_next = of_read_number(ranges + 1, 2);
55513dccb9eSBenjamin Herrenschmidt 			cpu_next = of_translate_address(dev, ranges + 3);
55613dccb9eSBenjamin Herrenschmidt 			if (pci_next != pci_addr + size ||
55713dccb9eSBenjamin Herrenschmidt 			    cpu_next != cpu_addr + size)
55813dccb9eSBenjamin Herrenschmidt 				break;
55913dccb9eSBenjamin Herrenschmidt 			size += of_read_number(ranges + pna + 3, 2);
56013dccb9eSBenjamin Herrenschmidt 		}
56113dccb9eSBenjamin Herrenschmidt 
56213dccb9eSBenjamin Herrenschmidt 		/* Act based on address space type */
56313dccb9eSBenjamin Herrenschmidt 		res = NULL;
56413dccb9eSBenjamin Herrenschmidt 		switch ((pci_space >> 24) & 0x3) {
56513dccb9eSBenjamin Herrenschmidt 		case 1:		/* PCI IO space */
56613dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
56713dccb9eSBenjamin Herrenschmidt 			       "  IO 0x%016llx..0x%016llx -> 0x%016llx\n",
56813dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr);
56913dccb9eSBenjamin Herrenschmidt 
57013dccb9eSBenjamin Herrenschmidt 			/* We support only one IO range */
57113dccb9eSBenjamin Herrenschmidt 			if (hose->pci_io_size) {
57213dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
57313dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
57413dccb9eSBenjamin Herrenschmidt 				continue;
57513dccb9eSBenjamin Herrenschmidt 			}
57613dccb9eSBenjamin Herrenschmidt #ifdef CONFIG_PPC32
57713dccb9eSBenjamin Herrenschmidt 			/* On 32 bits, limit I/O space to 16MB */
57813dccb9eSBenjamin Herrenschmidt 			if (size > 0x01000000)
57913dccb9eSBenjamin Herrenschmidt 				size = 0x01000000;
58013dccb9eSBenjamin Herrenschmidt 
58113dccb9eSBenjamin Herrenschmidt 			/* 32 bits needs to map IOs here */
58213dccb9eSBenjamin Herrenschmidt 			hose->io_base_virt = ioremap(cpu_addr, size);
58313dccb9eSBenjamin Herrenschmidt 
58413dccb9eSBenjamin Herrenschmidt 			/* Expect trouble if pci_addr is not 0 */
58513dccb9eSBenjamin Herrenschmidt 			if (primary)
58613dccb9eSBenjamin Herrenschmidt 				isa_io_base =
58713dccb9eSBenjamin Herrenschmidt 					(unsigned long)hose->io_base_virt;
58813dccb9eSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
58913dccb9eSBenjamin Herrenschmidt 			/* pci_io_size and io_base_phys always represent IO
59013dccb9eSBenjamin Herrenschmidt 			 * space starting at 0 so we factor in pci_addr
59113dccb9eSBenjamin Herrenschmidt 			 */
59213dccb9eSBenjamin Herrenschmidt 			hose->pci_io_size = pci_addr + size;
59313dccb9eSBenjamin Herrenschmidt 			hose->io_base_phys = cpu_addr - pci_addr;
59413dccb9eSBenjamin Herrenschmidt 
59513dccb9eSBenjamin Herrenschmidt 			/* Build resource */
59613dccb9eSBenjamin Herrenschmidt 			res = &hose->io_resource;
59713dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_IO;
59813dccb9eSBenjamin Herrenschmidt 			res->start = pci_addr;
59913dccb9eSBenjamin Herrenschmidt 			break;
60013dccb9eSBenjamin Herrenschmidt 		case 2:		/* PCI Memory space */
60113dccb9eSBenjamin Herrenschmidt 			printk(KERN_INFO
60213dccb9eSBenjamin Herrenschmidt 			       " MEM 0x%016llx..0x%016llx -> 0x%016llx %s\n",
60313dccb9eSBenjamin Herrenschmidt 			       cpu_addr, cpu_addr + size - 1, pci_addr,
60413dccb9eSBenjamin Herrenschmidt 			       (pci_space & 0x40000000) ? "Prefetch" : "");
60513dccb9eSBenjamin Herrenschmidt 
60613dccb9eSBenjamin Herrenschmidt 			/* We support only 3 memory ranges */
60713dccb9eSBenjamin Herrenschmidt 			if (memno >= 3) {
60813dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
60913dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (too many) !\n");
61013dccb9eSBenjamin Herrenschmidt 				continue;
61113dccb9eSBenjamin Herrenschmidt 			}
61213dccb9eSBenjamin Herrenschmidt 			/* Handles ISA memory hole space here */
61313dccb9eSBenjamin Herrenschmidt 			if (pci_addr == 0) {
61413dccb9eSBenjamin Herrenschmidt 				isa_mb = cpu_addr;
61513dccb9eSBenjamin Herrenschmidt 				isa_hole = memno;
61613dccb9eSBenjamin Herrenschmidt 				if (primary || isa_mem_base == 0)
61713dccb9eSBenjamin Herrenschmidt 					isa_mem_base = cpu_addr;
61813dccb9eSBenjamin Herrenschmidt 			}
61913dccb9eSBenjamin Herrenschmidt 
62013dccb9eSBenjamin Herrenschmidt 			/* We get the PCI/Mem offset from the first range or
62113dccb9eSBenjamin Herrenschmidt 			 * the, current one if the offset came from an ISA
62213dccb9eSBenjamin Herrenschmidt 			 * hole. If they don't match, bugger.
62313dccb9eSBenjamin Herrenschmidt 			 */
62413dccb9eSBenjamin Herrenschmidt 			if (memno == 0 ||
62513dccb9eSBenjamin Herrenschmidt 			    (isa_hole >= 0 && pci_addr != 0 &&
62613dccb9eSBenjamin Herrenschmidt 			     hose->pci_mem_offset == isa_mb))
62713dccb9eSBenjamin Herrenschmidt 				hose->pci_mem_offset = cpu_addr - pci_addr;
62813dccb9eSBenjamin Herrenschmidt 			else if (pci_addr != 0 &&
62913dccb9eSBenjamin Herrenschmidt 				 hose->pci_mem_offset != cpu_addr - pci_addr) {
63013dccb9eSBenjamin Herrenschmidt 				printk(KERN_INFO
63113dccb9eSBenjamin Herrenschmidt 				       " \\--> Skipped (offset mismatch) !\n");
63213dccb9eSBenjamin Herrenschmidt 				continue;
63313dccb9eSBenjamin Herrenschmidt 			}
63413dccb9eSBenjamin Herrenschmidt 
63513dccb9eSBenjamin Herrenschmidt 			/* Build resource */
63613dccb9eSBenjamin Herrenschmidt 			res = &hose->mem_resources[memno++];
63713dccb9eSBenjamin Herrenschmidt 			res->flags = IORESOURCE_MEM;
63813dccb9eSBenjamin Herrenschmidt 			if (pci_space & 0x40000000)
63913dccb9eSBenjamin Herrenschmidt 				res->flags |= IORESOURCE_PREFETCH;
64013dccb9eSBenjamin Herrenschmidt 			res->start = cpu_addr;
64113dccb9eSBenjamin Herrenschmidt 			break;
64213dccb9eSBenjamin Herrenschmidt 		}
64313dccb9eSBenjamin Herrenschmidt 		if (res != NULL) {
64413dccb9eSBenjamin Herrenschmidt 			res->name = dev->full_name;
64513dccb9eSBenjamin Herrenschmidt 			res->end = res->start + size - 1;
64613dccb9eSBenjamin Herrenschmidt 			res->parent = NULL;
64713dccb9eSBenjamin Herrenschmidt 			res->sibling = NULL;
64813dccb9eSBenjamin Herrenschmidt 			res->child = NULL;
64913dccb9eSBenjamin Herrenschmidt 		}
65013dccb9eSBenjamin Herrenschmidt 	}
65113dccb9eSBenjamin Herrenschmidt 
65213dccb9eSBenjamin Herrenschmidt 	/* Out of paranoia, let's put the ISA hole last if any */
65313dccb9eSBenjamin Herrenschmidt 	if (isa_hole >= 0 && memno > 0 && isa_hole != (memno-1)) {
65413dccb9eSBenjamin Herrenschmidt 		struct resource tmp = hose->mem_resources[isa_hole];
65513dccb9eSBenjamin Herrenschmidt 		hose->mem_resources[isa_hole] = hose->mem_resources[memno-1];
65613dccb9eSBenjamin Herrenschmidt 		hose->mem_resources[memno-1] = tmp;
65713dccb9eSBenjamin Herrenschmidt 	}
65813dccb9eSBenjamin Herrenschmidt }
659fa462f2dSBenjamin Herrenschmidt 
660fa462f2dSBenjamin Herrenschmidt /* Decide whether to display the domain number in /proc */
661fa462f2dSBenjamin Herrenschmidt int pci_proc_domain(struct pci_bus *bus)
662fa462f2dSBenjamin Herrenschmidt {
663fa462f2dSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
664fa462f2dSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
665fa462f2dSBenjamin Herrenschmidt 	return hose->buid != 0;
666fa462f2dSBenjamin Herrenschmidt #else
667fa462f2dSBenjamin Herrenschmidt 	if (!(ppc_pci_flags & PPC_PCI_ENABLE_PROC_DOMAINS))
668fa462f2dSBenjamin Herrenschmidt 		return 0;
669fa462f2dSBenjamin Herrenschmidt 	if (ppc_pci_flags & PPC_PCI_COMPAT_DOMAIN_0)
670fa462f2dSBenjamin Herrenschmidt 		return hose->global_number != 0;
671fa462f2dSBenjamin Herrenschmidt 	return 1;
672fa462f2dSBenjamin Herrenschmidt #endif
673fa462f2dSBenjamin Herrenschmidt }
674fa462f2dSBenjamin Herrenschmidt 
675fe2d338cSBenjamin Herrenschmidt void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
676fe2d338cSBenjamin Herrenschmidt 			     struct resource *res)
677fe2d338cSBenjamin Herrenschmidt {
678fe2d338cSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
679fe2d338cSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
680fe2d338cSBenjamin Herrenschmidt 
681fe2d338cSBenjamin Herrenschmidt 	if (!hose)
682fe2d338cSBenjamin Herrenschmidt 		return;
683fe2d338cSBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
684fe2d338cSBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
685fe2d338cSBenjamin Herrenschmidt 		mask = 0xffffffffu;
686fe2d338cSBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
687fe2d338cSBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
688fe2d338cSBenjamin Herrenschmidt 
689fe2d338cSBenjamin Herrenschmidt 	region->start = (res->start - offset) & mask;
690fe2d338cSBenjamin Herrenschmidt 	region->end = (res->end - offset) & mask;
691fe2d338cSBenjamin Herrenschmidt }
692fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_resource_to_bus);
693fe2d338cSBenjamin Herrenschmidt 
694fe2d338cSBenjamin Herrenschmidt void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
695fe2d338cSBenjamin Herrenschmidt 			     struct pci_bus_region *region)
696fe2d338cSBenjamin Herrenschmidt {
697fe2d338cSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
698fe2d338cSBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
699fe2d338cSBenjamin Herrenschmidt 
700fe2d338cSBenjamin Herrenschmidt 	if (!hose)
701fe2d338cSBenjamin Herrenschmidt 		return;
702fe2d338cSBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
703fe2d338cSBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
704fe2d338cSBenjamin Herrenschmidt 		mask = 0xffffffffu;
705fe2d338cSBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
706fe2d338cSBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
707fe2d338cSBenjamin Herrenschmidt 	res->start = (region->start + offset) & mask;
708fe2d338cSBenjamin Herrenschmidt 	res->end = (region->end + offset) & mask;
709fe2d338cSBenjamin Herrenschmidt }
710fe2d338cSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_bus_to_resource);
711bf5e2ba2SBenjamin Herrenschmidt 
712bf5e2ba2SBenjamin Herrenschmidt /* Fixup a bus resource into a linux resource */
713bf5e2ba2SBenjamin Herrenschmidt static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
714bf5e2ba2SBenjamin Herrenschmidt {
715bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
716bf5e2ba2SBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
717bf5e2ba2SBenjamin Herrenschmidt 
718bf5e2ba2SBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
719bf5e2ba2SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
720bf5e2ba2SBenjamin Herrenschmidt 		mask = 0xffffffffu;
721bf5e2ba2SBenjamin Herrenschmidt 	} else if (res->flags & IORESOURCE_MEM)
722bf5e2ba2SBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
723bf5e2ba2SBenjamin Herrenschmidt 
724bf5e2ba2SBenjamin Herrenschmidt 	res->start = (res->start + offset) & mask;
725bf5e2ba2SBenjamin Herrenschmidt 	res->end = (res->end + offset) & mask;
726bf5e2ba2SBenjamin Herrenschmidt 
727bf5e2ba2SBenjamin Herrenschmidt 	pr_debug("PCI:%s            %016llx-%016llx\n",
728bf5e2ba2SBenjamin Herrenschmidt 		 pci_name(dev),
729bf5e2ba2SBenjamin Herrenschmidt 		 (unsigned long long)res->start,
730bf5e2ba2SBenjamin Herrenschmidt 		 (unsigned long long)res->end);
731bf5e2ba2SBenjamin Herrenschmidt }
732bf5e2ba2SBenjamin Herrenschmidt 
733bf5e2ba2SBenjamin Herrenschmidt 
734bf5e2ba2SBenjamin Herrenschmidt /* This header fixup will do the resource fixup for all devices as they are
735bf5e2ba2SBenjamin Herrenschmidt  * probed, but not for bridge ranges
736bf5e2ba2SBenjamin Herrenschmidt  */
737bf5e2ba2SBenjamin Herrenschmidt static void __devinit pcibios_fixup_resources(struct pci_dev *dev)
738bf5e2ba2SBenjamin Herrenschmidt {
739bf5e2ba2SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(dev->bus);
740bf5e2ba2SBenjamin Herrenschmidt 	int i;
741bf5e2ba2SBenjamin Herrenschmidt 
742bf5e2ba2SBenjamin Herrenschmidt 	if (!hose) {
743bf5e2ba2SBenjamin Herrenschmidt 		printk(KERN_ERR "No host bridge for PCI dev %s !\n",
744bf5e2ba2SBenjamin Herrenschmidt 		       pci_name(dev));
745bf5e2ba2SBenjamin Herrenschmidt 		return;
746bf5e2ba2SBenjamin Herrenschmidt 	}
747bf5e2ba2SBenjamin Herrenschmidt 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
748bf5e2ba2SBenjamin Herrenschmidt 		struct resource *res = dev->resource + i;
749bf5e2ba2SBenjamin Herrenschmidt 		if (!res->flags)
750bf5e2ba2SBenjamin Herrenschmidt 			continue;
751bf5e2ba2SBenjamin Herrenschmidt 		if (res->end == 0xffffffff) {
752bf5e2ba2SBenjamin Herrenschmidt 			pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] is unassigned\n",
753bf5e2ba2SBenjamin Herrenschmidt 				 pci_name(dev), i,
754bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->start,
755bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->end,
756bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned int)res->flags);
757bf5e2ba2SBenjamin Herrenschmidt 			res->end -= res->start;
758bf5e2ba2SBenjamin Herrenschmidt 			res->start = 0;
759bf5e2ba2SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
760bf5e2ba2SBenjamin Herrenschmidt 			continue;
761bf5e2ba2SBenjamin Herrenschmidt 		}
762bf5e2ba2SBenjamin Herrenschmidt 
763bf5e2ba2SBenjamin Herrenschmidt 		pr_debug("PCI:%s Resource %d %016llx-%016llx [%x] fixup...\n",
764bf5e2ba2SBenjamin Herrenschmidt 			 pci_name(dev), i,
765bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->start,\
766bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned long long)res->end,
767bf5e2ba2SBenjamin Herrenschmidt 			 (unsigned int)res->flags);
768bf5e2ba2SBenjamin Herrenschmidt 
769bf5e2ba2SBenjamin Herrenschmidt 		fixup_resource(res, dev);
770bf5e2ba2SBenjamin Herrenschmidt 	}
771bf5e2ba2SBenjamin Herrenschmidt 
772bf5e2ba2SBenjamin Herrenschmidt 	/* Call machine specific resource fixup */
773bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_resources)
774bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_resources(dev);
775bf5e2ba2SBenjamin Herrenschmidt }
776bf5e2ba2SBenjamin Herrenschmidt DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
777bf5e2ba2SBenjamin Herrenschmidt 
778bf5e2ba2SBenjamin Herrenschmidt static void __devinit __pcibios_fixup_bus(struct pci_bus *bus)
779bf5e2ba2SBenjamin Herrenschmidt {
780be8cbcd8SBenjamin Herrenschmidt 	struct pci_controller *hose = pci_bus_to_host(bus);
781bf5e2ba2SBenjamin Herrenschmidt 	struct pci_dev *dev = bus->self;
782bf5e2ba2SBenjamin Herrenschmidt 
783bf5e2ba2SBenjamin Herrenschmidt 	pr_debug("PCI: Fixup bus %d (%s)\n", bus->number, dev ? pci_name(dev) : "PHB");
784bf5e2ba2SBenjamin Herrenschmidt 
785bf5e2ba2SBenjamin Herrenschmidt 	/* Fixup PCI<->PCI bridges. Host bridges are handled separately, for
786bf5e2ba2SBenjamin Herrenschmidt 	 * now differently between 32 and 64 bits.
787bf5e2ba2SBenjamin Herrenschmidt 	 */
788bf5e2ba2SBenjamin Herrenschmidt 	if (dev != NULL) {
789bf5e2ba2SBenjamin Herrenschmidt 		struct resource *res;
790bf5e2ba2SBenjamin Herrenschmidt 		int i;
791bf5e2ba2SBenjamin Herrenschmidt 
792bf5e2ba2SBenjamin Herrenschmidt 		for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
793bf5e2ba2SBenjamin Herrenschmidt 			if ((res = bus->resource[i]) == NULL)
794bf5e2ba2SBenjamin Herrenschmidt 				continue;
795bf5e2ba2SBenjamin Herrenschmidt 			if (!res->flags || bus->self->transparent)
796bf5e2ba2SBenjamin Herrenschmidt 				continue;
797bf5e2ba2SBenjamin Herrenschmidt 
798be8cbcd8SBenjamin Herrenschmidt 			/* On PowerMac, Apple leaves bridge windows open over
799be8cbcd8SBenjamin Herrenschmidt 			 * an inaccessible region of memory space (0...fffff)
800be8cbcd8SBenjamin Herrenschmidt 			 * which is somewhat bogus, but that's what they think
801be8cbcd8SBenjamin Herrenschmidt 			 * means disabled...
802be8cbcd8SBenjamin Herrenschmidt 			 *
803be8cbcd8SBenjamin Herrenschmidt 			 * We clear those to force them to be reallocated later
804be8cbcd8SBenjamin Herrenschmidt 			 *
805be8cbcd8SBenjamin Herrenschmidt 			 * We detect such regions by the fact that the base is
806be8cbcd8SBenjamin Herrenschmidt 			 * equal to the pci_mem_offset of the host bridge and
807be8cbcd8SBenjamin Herrenschmidt 			 * their size is smaller than 1M.
808be8cbcd8SBenjamin Herrenschmidt 			 */
809be8cbcd8SBenjamin Herrenschmidt 			if (res->start == hose->pci_mem_offset &&
810be8cbcd8SBenjamin Herrenschmidt 			    res->end < 0x100000) {
811be8cbcd8SBenjamin Herrenschmidt 				printk(KERN_INFO
812be8cbcd8SBenjamin Herrenschmidt 				       "PCI: Closing bogus Apple Firmware"
813be8cbcd8SBenjamin Herrenschmidt 				       " region %d on bus 0x%02x\n",
814be8cbcd8SBenjamin Herrenschmidt 				       i, bus->number);
815be8cbcd8SBenjamin Herrenschmidt 				res->flags = 0;
816be8cbcd8SBenjamin Herrenschmidt 				continue;
817be8cbcd8SBenjamin Herrenschmidt 			}
818be8cbcd8SBenjamin Herrenschmidt 
819bf5e2ba2SBenjamin Herrenschmidt 			pr_debug("PCI:%s Bus rsrc %d %016llx-%016llx [%x] fixup...\n",
820bf5e2ba2SBenjamin Herrenschmidt 				 pci_name(dev), i,
821bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->start,\
822bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned long long)res->end,
823bf5e2ba2SBenjamin Herrenschmidt 				 (unsigned int)res->flags);
824bf5e2ba2SBenjamin Herrenschmidt 
825bf5e2ba2SBenjamin Herrenschmidt 			fixup_resource(res, dev);
826bf5e2ba2SBenjamin Herrenschmidt 		}
827bf5e2ba2SBenjamin Herrenschmidt 	}
828bf5e2ba2SBenjamin Herrenschmidt 
829bf5e2ba2SBenjamin Herrenschmidt 	/* Additional setup that is different between 32 and 64 bits for now */
830bf5e2ba2SBenjamin Herrenschmidt 	pcibios_do_bus_setup(bus);
831bf5e2ba2SBenjamin Herrenschmidt 
832bf5e2ba2SBenjamin Herrenschmidt 	/* Platform specific bus fixups */
833bf5e2ba2SBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup_bus)
834bf5e2ba2SBenjamin Herrenschmidt 		ppc_md.pcibios_fixup_bus(bus);
835bf5e2ba2SBenjamin Herrenschmidt 
836bf5e2ba2SBenjamin Herrenschmidt 	/* Read default IRQs and fixup if necessary */
837bf5e2ba2SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
838bf5e2ba2SBenjamin Herrenschmidt 		pci_read_irq_line(dev);
839bf5e2ba2SBenjamin Herrenschmidt 		if (ppc_md.pci_irq_fixup)
840bf5e2ba2SBenjamin Herrenschmidt 			ppc_md.pci_irq_fixup(dev);
841bf5e2ba2SBenjamin Herrenschmidt 	}
842bf5e2ba2SBenjamin Herrenschmidt }
843bf5e2ba2SBenjamin Herrenschmidt 
844bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_fixup_bus(struct pci_bus *bus)
845bf5e2ba2SBenjamin Herrenschmidt {
846bf5e2ba2SBenjamin Herrenschmidt 	/* When called from the generic PCI probe, read PCI<->PCI bridge
847bf5e2ba2SBenjamin Herrenschmidt 	 * bases before proceeding
848bf5e2ba2SBenjamin Herrenschmidt 	 */
849bf5e2ba2SBenjamin Herrenschmidt 	if (bus->self != NULL)
850bf5e2ba2SBenjamin Herrenschmidt 		pci_read_bridge_bases(bus);
851bf5e2ba2SBenjamin Herrenschmidt 	__pcibios_fixup_bus(bus);
852bf5e2ba2SBenjamin Herrenschmidt }
853bf5e2ba2SBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_fixup_bus);
854bf5e2ba2SBenjamin Herrenschmidt 
855bf5e2ba2SBenjamin Herrenschmidt /* When building a bus from the OF tree rather than probing, we need a
856bf5e2ba2SBenjamin Herrenschmidt  * slightly different version of the fixup which doesn't read the
857bf5e2ba2SBenjamin Herrenschmidt  * bridge bases using config space accesses
858bf5e2ba2SBenjamin Herrenschmidt  */
859bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_fixup_of_probed_bus(struct pci_bus *bus)
860bf5e2ba2SBenjamin Herrenschmidt {
861bf5e2ba2SBenjamin Herrenschmidt 	__pcibios_fixup_bus(bus);
862bf5e2ba2SBenjamin Herrenschmidt }
8633fd94c6bSBenjamin Herrenschmidt 
8643fd94c6bSBenjamin Herrenschmidt static int skip_isa_ioresource_align(struct pci_dev *dev)
8653fd94c6bSBenjamin Herrenschmidt {
8663fd94c6bSBenjamin Herrenschmidt 	if ((ppc_pci_flags & PPC_PCI_CAN_SKIP_ISA_ALIGN) &&
8673fd94c6bSBenjamin Herrenschmidt 	    !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
8683fd94c6bSBenjamin Herrenschmidt 		return 1;
8693fd94c6bSBenjamin Herrenschmidt 	return 0;
8703fd94c6bSBenjamin Herrenschmidt }
8713fd94c6bSBenjamin Herrenschmidt 
8723fd94c6bSBenjamin Herrenschmidt /*
8733fd94c6bSBenjamin Herrenschmidt  * We need to avoid collisions with `mirrored' VGA ports
8743fd94c6bSBenjamin Herrenschmidt  * and other strange ISA hardware, so we always want the
8753fd94c6bSBenjamin Herrenschmidt  * addresses to be allocated in the 0x000-0x0ff region
8763fd94c6bSBenjamin Herrenschmidt  * modulo 0x400.
8773fd94c6bSBenjamin Herrenschmidt  *
8783fd94c6bSBenjamin Herrenschmidt  * Why? Because some silly external IO cards only decode
8793fd94c6bSBenjamin Herrenschmidt  * the low 10 bits of the IO address. The 0x00-0xff region
8803fd94c6bSBenjamin Herrenschmidt  * is reserved for motherboard devices that decode all 16
8813fd94c6bSBenjamin Herrenschmidt  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
8823fd94c6bSBenjamin Herrenschmidt  * but we want to try to avoid allocating at 0x2900-0x2bff
8833fd94c6bSBenjamin Herrenschmidt  * which might have be mirrored at 0x0100-0x03ff..
8843fd94c6bSBenjamin Herrenschmidt  */
8853fd94c6bSBenjamin Herrenschmidt void pcibios_align_resource(void *data, struct resource *res,
8863fd94c6bSBenjamin Herrenschmidt 				resource_size_t size, resource_size_t align)
8873fd94c6bSBenjamin Herrenschmidt {
8883fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = data;
8893fd94c6bSBenjamin Herrenschmidt 
8903fd94c6bSBenjamin Herrenschmidt 	if (res->flags & IORESOURCE_IO) {
8913fd94c6bSBenjamin Herrenschmidt 		resource_size_t start = res->start;
8923fd94c6bSBenjamin Herrenschmidt 
8933fd94c6bSBenjamin Herrenschmidt 		if (skip_isa_ioresource_align(dev))
8943fd94c6bSBenjamin Herrenschmidt 			return;
8953fd94c6bSBenjamin Herrenschmidt 		if (start & 0x300) {
8963fd94c6bSBenjamin Herrenschmidt 			start = (start + 0x3ff) & ~0x3ff;
8973fd94c6bSBenjamin Herrenschmidt 			res->start = start;
8983fd94c6bSBenjamin Herrenschmidt 		}
8993fd94c6bSBenjamin Herrenschmidt 	}
9003fd94c6bSBenjamin Herrenschmidt }
9013fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL(pcibios_align_resource);
9023fd94c6bSBenjamin Herrenschmidt 
9033fd94c6bSBenjamin Herrenschmidt /*
9043fd94c6bSBenjamin Herrenschmidt  * Reparent resource children of pr that conflict with res
9053fd94c6bSBenjamin Herrenschmidt  * under res, and make res replace those children.
9063fd94c6bSBenjamin Herrenschmidt  */
9073fd94c6bSBenjamin Herrenschmidt static int __init reparent_resources(struct resource *parent,
9083fd94c6bSBenjamin Herrenschmidt 				     struct resource *res)
9093fd94c6bSBenjamin Herrenschmidt {
9103fd94c6bSBenjamin Herrenschmidt 	struct resource *p, **pp;
9113fd94c6bSBenjamin Herrenschmidt 	struct resource **firstpp = NULL;
9123fd94c6bSBenjamin Herrenschmidt 
9133fd94c6bSBenjamin Herrenschmidt 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
9143fd94c6bSBenjamin Herrenschmidt 		if (p->end < res->start)
9153fd94c6bSBenjamin Herrenschmidt 			continue;
9163fd94c6bSBenjamin Herrenschmidt 		if (res->end < p->start)
9173fd94c6bSBenjamin Herrenschmidt 			break;
9183fd94c6bSBenjamin Herrenschmidt 		if (p->start < res->start || p->end > res->end)
9193fd94c6bSBenjamin Herrenschmidt 			return -1;	/* not completely contained */
9203fd94c6bSBenjamin Herrenschmidt 		if (firstpp == NULL)
9213fd94c6bSBenjamin Herrenschmidt 			firstpp = pp;
9223fd94c6bSBenjamin Herrenschmidt 	}
9233fd94c6bSBenjamin Herrenschmidt 	if (firstpp == NULL)
9243fd94c6bSBenjamin Herrenschmidt 		return -1;	/* didn't find any conflicting entries? */
9253fd94c6bSBenjamin Herrenschmidt 	res->parent = parent;
9263fd94c6bSBenjamin Herrenschmidt 	res->child = *firstpp;
9273fd94c6bSBenjamin Herrenschmidt 	res->sibling = *pp;
9283fd94c6bSBenjamin Herrenschmidt 	*firstpp = res;
9293fd94c6bSBenjamin Herrenschmidt 	*pp = NULL;
9303fd94c6bSBenjamin Herrenschmidt 	for (p = res->child; p != NULL; p = p->sibling) {
9313fd94c6bSBenjamin Herrenschmidt 		p->parent = res;
9323fd94c6bSBenjamin Herrenschmidt 		DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
9333fd94c6bSBenjamin Herrenschmidt 		    p->name,
9343fd94c6bSBenjamin Herrenschmidt 		    (unsigned long long)p->start,
9353fd94c6bSBenjamin Herrenschmidt 		    (unsigned long long)p->end, res->name);
9363fd94c6bSBenjamin Herrenschmidt 	}
9373fd94c6bSBenjamin Herrenschmidt 	return 0;
9383fd94c6bSBenjamin Herrenschmidt }
9393fd94c6bSBenjamin Herrenschmidt 
9403fd94c6bSBenjamin Herrenschmidt /*
9413fd94c6bSBenjamin Herrenschmidt  *  Handle resources of PCI devices.  If the world were perfect, we could
9423fd94c6bSBenjamin Herrenschmidt  *  just allocate all the resource regions and do nothing more.  It isn't.
9433fd94c6bSBenjamin Herrenschmidt  *  On the other hand, we cannot just re-allocate all devices, as it would
9443fd94c6bSBenjamin Herrenschmidt  *  require us to know lots of host bridge internals.  So we attempt to
9453fd94c6bSBenjamin Herrenschmidt  *  keep as much of the original configuration as possible, but tweak it
9463fd94c6bSBenjamin Herrenschmidt  *  when it's found to be wrong.
9473fd94c6bSBenjamin Herrenschmidt  *
9483fd94c6bSBenjamin Herrenschmidt  *  Known BIOS problems we have to work around:
9493fd94c6bSBenjamin Herrenschmidt  *	- I/O or memory regions not configured
9503fd94c6bSBenjamin Herrenschmidt  *	- regions configured, but not enabled in the command register
9513fd94c6bSBenjamin Herrenschmidt  *	- bogus I/O addresses above 64K used
9523fd94c6bSBenjamin Herrenschmidt  *	- expansion ROMs left enabled (this may sound harmless, but given
9533fd94c6bSBenjamin Herrenschmidt  *	  the fact the PCI specs explicitly allow address decoders to be
9543fd94c6bSBenjamin Herrenschmidt  *	  shared between expansion ROMs and other resource regions, it's
9553fd94c6bSBenjamin Herrenschmidt  *	  at least dangerous)
9563fd94c6bSBenjamin Herrenschmidt  *
9573fd94c6bSBenjamin Herrenschmidt  *  Our solution:
9583fd94c6bSBenjamin Herrenschmidt  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
9593fd94c6bSBenjamin Herrenschmidt  *	    This gives us fixed barriers on where we can allocate.
9603fd94c6bSBenjamin Herrenschmidt  *	(2) Allocate resources for all enabled devices.  If there is
9613fd94c6bSBenjamin Herrenschmidt  *	    a collision, just mark the resource as unallocated. Also
9623fd94c6bSBenjamin Herrenschmidt  *	    disable expansion ROMs during this step.
9633fd94c6bSBenjamin Herrenschmidt  *	(3) Try to allocate resources for disabled devices.  If the
9643fd94c6bSBenjamin Herrenschmidt  *	    resources were assigned correctly, everything goes well,
9653fd94c6bSBenjamin Herrenschmidt  *	    if they weren't, they won't disturb allocation of other
9663fd94c6bSBenjamin Herrenschmidt  *	    resources.
9673fd94c6bSBenjamin Herrenschmidt  *	(4) Assign new addresses to resources which were either
9683fd94c6bSBenjamin Herrenschmidt  *	    not configured at all or misconfigured.  If explicitly
9693fd94c6bSBenjamin Herrenschmidt  *	    requested by the user, configure expansion ROM address
9703fd94c6bSBenjamin Herrenschmidt  *	    as well.
9713fd94c6bSBenjamin Herrenschmidt  */
9723fd94c6bSBenjamin Herrenschmidt 
9733fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
9743fd94c6bSBenjamin Herrenschmidt {
9753fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *bus;
9763fd94c6bSBenjamin Herrenschmidt 	int i;
9773fd94c6bSBenjamin Herrenschmidt 	struct resource *res, *pr;
9783fd94c6bSBenjamin Herrenschmidt 
9793fd94c6bSBenjamin Herrenschmidt 	/* Depth-First Search on bus tree */
9803fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(bus, bus_list, node) {
9813fd94c6bSBenjamin Herrenschmidt 		for (i = 0; i < PCI_BUS_NUM_RESOURCES; ++i) {
9823fd94c6bSBenjamin Herrenschmidt 			if ((res = bus->resource[i]) == NULL || !res->flags
9833fd94c6bSBenjamin Herrenschmidt 			    || res->start > res->end)
9843fd94c6bSBenjamin Herrenschmidt 				continue;
9853fd94c6bSBenjamin Herrenschmidt 			if (bus->parent == NULL)
9863fd94c6bSBenjamin Herrenschmidt 				pr = (res->flags & IORESOURCE_IO) ?
9873fd94c6bSBenjamin Herrenschmidt 					&ioport_resource : &iomem_resource;
9883fd94c6bSBenjamin Herrenschmidt 			else {
9893fd94c6bSBenjamin Herrenschmidt 				/* Don't bother with non-root busses when
9903fd94c6bSBenjamin Herrenschmidt 				 * re-assigning all resources. We clear the
9913fd94c6bSBenjamin Herrenschmidt 				 * resource flags as if they were colliding
9923fd94c6bSBenjamin Herrenschmidt 				 * and as such ensure proper re-allocation
9933fd94c6bSBenjamin Herrenschmidt 				 * later.
9943fd94c6bSBenjamin Herrenschmidt 				 */
9953fd94c6bSBenjamin Herrenschmidt 				if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)
9963fd94c6bSBenjamin Herrenschmidt 					goto clear_resource;
9973fd94c6bSBenjamin Herrenschmidt 				pr = pci_find_parent_resource(bus->self, res);
9983fd94c6bSBenjamin Herrenschmidt 				if (pr == res) {
9993fd94c6bSBenjamin Herrenschmidt 					/* this happens when the generic PCI
10003fd94c6bSBenjamin Herrenschmidt 					 * code (wrongly) decides that this
10013fd94c6bSBenjamin Herrenschmidt 					 * bridge is transparent  -- paulus
10023fd94c6bSBenjamin Herrenschmidt 					 */
10033fd94c6bSBenjamin Herrenschmidt 					continue;
10043fd94c6bSBenjamin Herrenschmidt 				}
10053fd94c6bSBenjamin Herrenschmidt 			}
10063fd94c6bSBenjamin Herrenschmidt 
10073fd94c6bSBenjamin Herrenschmidt 			DBG("PCI: %s (bus %d) bridge rsrc %d: %016llx-%016llx "
10083fd94c6bSBenjamin Herrenschmidt 			    "[0x%x], parent %p (%s)\n",
10093fd94c6bSBenjamin Herrenschmidt 			    bus->self ? pci_name(bus->self) : "PHB",
10103fd94c6bSBenjamin Herrenschmidt 			    bus->number, i,
10113fd94c6bSBenjamin Herrenschmidt 			    (unsigned long long)res->start,
10123fd94c6bSBenjamin Herrenschmidt 			    (unsigned long long)res->end,
10133fd94c6bSBenjamin Herrenschmidt 			    (unsigned int)res->flags,
10143fd94c6bSBenjamin Herrenschmidt 			    pr, (pr && pr->name) ? pr->name : "nil");
10153fd94c6bSBenjamin Herrenschmidt 
10163fd94c6bSBenjamin Herrenschmidt 			if (pr && !(pr->flags & IORESOURCE_UNSET)) {
10173fd94c6bSBenjamin Herrenschmidt 				if (request_resource(pr, res) == 0)
10183fd94c6bSBenjamin Herrenschmidt 					continue;
10193fd94c6bSBenjamin Herrenschmidt 				/*
10203fd94c6bSBenjamin Herrenschmidt 				 * Must be a conflict with an existing entry.
10213fd94c6bSBenjamin Herrenschmidt 				 * Move that entry (or entries) under the
10223fd94c6bSBenjamin Herrenschmidt 				 * bridge resource and try again.
10233fd94c6bSBenjamin Herrenschmidt 				 */
10243fd94c6bSBenjamin Herrenschmidt 				if (reparent_resources(pr, res) == 0)
10253fd94c6bSBenjamin Herrenschmidt 					continue;
10263fd94c6bSBenjamin Herrenschmidt 			}
10273fd94c6bSBenjamin Herrenschmidt 			printk(KERN_WARNING
10283fd94c6bSBenjamin Herrenschmidt 			       "PCI: Cannot allocate resource region "
10293fd94c6bSBenjamin Herrenschmidt 			       "%d of PCI bridge %d, will remap\n",
10303fd94c6bSBenjamin Herrenschmidt 			       i, bus->number);
10313fd94c6bSBenjamin Herrenschmidt clear_resource:
10323fd94c6bSBenjamin Herrenschmidt 			res->flags = 0;
10333fd94c6bSBenjamin Herrenschmidt 		}
10343fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_bus_resources(&bus->children);
10353fd94c6bSBenjamin Herrenschmidt 	}
10363fd94c6bSBenjamin Herrenschmidt }
10373fd94c6bSBenjamin Herrenschmidt 
1038553aa765SBenjamin Herrenschmidt static inline int __devinit alloc_resource(struct pci_dev *dev, int idx)
10393fd94c6bSBenjamin Herrenschmidt {
10403fd94c6bSBenjamin Herrenschmidt 	struct resource *pr, *r = &dev->resource[idx];
10413fd94c6bSBenjamin Herrenschmidt 
10423fd94c6bSBenjamin Herrenschmidt 	DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx [%x]\n",
10433fd94c6bSBenjamin Herrenschmidt 	    pci_name(dev), idx,
10443fd94c6bSBenjamin Herrenschmidt 	    (unsigned long long)r->start,
10453fd94c6bSBenjamin Herrenschmidt 	    (unsigned long long)r->end,
10463fd94c6bSBenjamin Herrenschmidt 	    (unsigned int)r->flags);
10473fd94c6bSBenjamin Herrenschmidt 
10483fd94c6bSBenjamin Herrenschmidt 	pr = pci_find_parent_resource(dev, r);
10493fd94c6bSBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||
10503fd94c6bSBenjamin Herrenschmidt 	    request_resource(pr, r) < 0) {
10513fd94c6bSBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
10523fd94c6bSBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
10533fd94c6bSBenjamin Herrenschmidt 		if (pr)
10543fd94c6bSBenjamin Herrenschmidt 			DBG("PCI:  parent is %p: %016llx-%016llx [%x]\n", pr,
10553fd94c6bSBenjamin Herrenschmidt 			    (unsigned long long)pr->start,
10563fd94c6bSBenjamin Herrenschmidt 			    (unsigned long long)pr->end,
10573fd94c6bSBenjamin Herrenschmidt 			    (unsigned int)pr->flags);
10583fd94c6bSBenjamin Herrenschmidt 		/* We'll assign a new address later */
10593fd94c6bSBenjamin Herrenschmidt 		r->flags |= IORESOURCE_UNSET;
10603fd94c6bSBenjamin Herrenschmidt 		r->end -= r->start;
10613fd94c6bSBenjamin Herrenschmidt 		r->start = 0;
1062553aa765SBenjamin Herrenschmidt 
1063553aa765SBenjamin Herrenschmidt 		return -EBUSY;
10643fd94c6bSBenjamin Herrenschmidt 	}
1065553aa765SBenjamin Herrenschmidt 	return 0;
10663fd94c6bSBenjamin Herrenschmidt }
10673fd94c6bSBenjamin Herrenschmidt 
10683fd94c6bSBenjamin Herrenschmidt static void __init pcibios_allocate_resources(int pass)
10693fd94c6bSBenjamin Herrenschmidt {
10703fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev = NULL;
10713fd94c6bSBenjamin Herrenschmidt 	int idx, disabled;
10723fd94c6bSBenjamin Herrenschmidt 	u16 command;
10733fd94c6bSBenjamin Herrenschmidt 	struct resource *r;
10743fd94c6bSBenjamin Herrenschmidt 
10753fd94c6bSBenjamin Herrenschmidt 	for_each_pci_dev(dev) {
10763fd94c6bSBenjamin Herrenschmidt 		pci_read_config_word(dev, PCI_COMMAND, &command);
10773fd94c6bSBenjamin Herrenschmidt 		for (idx = 0; idx < 6; idx++) {
10783fd94c6bSBenjamin Herrenschmidt 			r = &dev->resource[idx];
10793fd94c6bSBenjamin Herrenschmidt 			if (r->parent)		/* Already allocated */
10803fd94c6bSBenjamin Herrenschmidt 				continue;
10813fd94c6bSBenjamin Herrenschmidt 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
10823fd94c6bSBenjamin Herrenschmidt 				continue;	/* Not assigned at all */
10833fd94c6bSBenjamin Herrenschmidt 			if (r->flags & IORESOURCE_IO)
10843fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_IO);
10853fd94c6bSBenjamin Herrenschmidt 			else
10863fd94c6bSBenjamin Herrenschmidt 				disabled = !(command & PCI_COMMAND_MEMORY);
1087553aa765SBenjamin Herrenschmidt 			if (pass == disabled && alloc_resource(dev, idx)) {
1088553aa765SBenjamin Herrenschmidt 				command &= ~(r->flags & (IORESOURCE_IO |
1089553aa765SBenjamin Herrenschmidt 							 IORESOURCE_MEM));
1090553aa765SBenjamin Herrenschmidt 				pci_write_config_word(dev,
1091553aa765SBenjamin Herrenschmidt 						      PCI_COMMAND, command);
1092553aa765SBenjamin Herrenschmidt 			}
10933fd94c6bSBenjamin Herrenschmidt 		}
10943fd94c6bSBenjamin Herrenschmidt 		if (pass)
10953fd94c6bSBenjamin Herrenschmidt 			continue;
10963fd94c6bSBenjamin Herrenschmidt 		r = &dev->resource[PCI_ROM_RESOURCE];
10973fd94c6bSBenjamin Herrenschmidt 		if (r->flags & IORESOURCE_ROM_ENABLE) {
10983fd94c6bSBenjamin Herrenschmidt 			/* Turn the ROM off, leave the resource region,
10993fd94c6bSBenjamin Herrenschmidt 			 * but keep it unregistered.
11003fd94c6bSBenjamin Herrenschmidt 			 */
11013fd94c6bSBenjamin Herrenschmidt 			u32 reg;
11023fd94c6bSBenjamin Herrenschmidt 			DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
11033fd94c6bSBenjamin Herrenschmidt 			r->flags &= ~IORESOURCE_ROM_ENABLE;
11043fd94c6bSBenjamin Herrenschmidt 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
11053fd94c6bSBenjamin Herrenschmidt 			pci_write_config_dword(dev, dev->rom_base_reg,
11063fd94c6bSBenjamin Herrenschmidt 					       reg & ~PCI_ROM_ADDRESS_ENABLE);
11073fd94c6bSBenjamin Herrenschmidt 		}
11083fd94c6bSBenjamin Herrenschmidt 	}
11093fd94c6bSBenjamin Herrenschmidt }
11103fd94c6bSBenjamin Herrenschmidt 
11113fd94c6bSBenjamin Herrenschmidt void __init pcibios_resource_survey(void)
11123fd94c6bSBenjamin Herrenschmidt {
11133fd94c6bSBenjamin Herrenschmidt 	/* Allocate and assign resources. If we re-assign everything, then
11143fd94c6bSBenjamin Herrenschmidt 	 * we skip the allocate phase
11153fd94c6bSBenjamin Herrenschmidt 	 */
11163fd94c6bSBenjamin Herrenschmidt 	pcibios_allocate_bus_resources(&pci_root_buses);
11173fd94c6bSBenjamin Herrenschmidt 
11183fd94c6bSBenjamin Herrenschmidt 	if (!(ppc_pci_flags & PPC_PCI_REASSIGN_ALL_RSRC)) {
11193fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(0);
11203fd94c6bSBenjamin Herrenschmidt 		pcibios_allocate_resources(1);
11213fd94c6bSBenjamin Herrenschmidt 	}
11223fd94c6bSBenjamin Herrenschmidt 
11233fd94c6bSBenjamin Herrenschmidt 	if (!(ppc_pci_flags & PPC_PCI_PROBE_ONLY)) {
11243fd94c6bSBenjamin Herrenschmidt 		DBG("PCI: Assigning unassigned resouces...\n");
11253fd94c6bSBenjamin Herrenschmidt 		pci_assign_unassigned_resources();
11263fd94c6bSBenjamin Herrenschmidt 	}
11273fd94c6bSBenjamin Herrenschmidt 
11283fd94c6bSBenjamin Herrenschmidt 	/* Call machine dependent fixup */
11293fd94c6bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_fixup)
11303fd94c6bSBenjamin Herrenschmidt 		ppc_md.pcibios_fixup();
11313fd94c6bSBenjamin Herrenschmidt }
11323fd94c6bSBenjamin Herrenschmidt 
11333fd94c6bSBenjamin Herrenschmidt #ifdef CONFIG_HOTPLUG
11343fd94c6bSBenjamin Herrenschmidt /* This is used by the pSeries hotplug driver to allocate resource
11353fd94c6bSBenjamin Herrenschmidt  * of newly plugged busses. We can try to consolidate with the
11363fd94c6bSBenjamin Herrenschmidt  * rest of the code later, for now, keep it as-is
11373fd94c6bSBenjamin Herrenschmidt  */
11383fd94c6bSBenjamin Herrenschmidt void __devinit pcibios_claim_one_bus(struct pci_bus *bus)
11393fd94c6bSBenjamin Herrenschmidt {
11403fd94c6bSBenjamin Herrenschmidt 	struct pci_dev *dev;
11413fd94c6bSBenjamin Herrenschmidt 	struct pci_bus *child_bus;
11423fd94c6bSBenjamin Herrenschmidt 
11433fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
11443fd94c6bSBenjamin Herrenschmidt 		int i;
11453fd94c6bSBenjamin Herrenschmidt 
11463fd94c6bSBenjamin Herrenschmidt 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
11473fd94c6bSBenjamin Herrenschmidt 			struct resource *r = &dev->resource[i];
11483fd94c6bSBenjamin Herrenschmidt 
11493fd94c6bSBenjamin Herrenschmidt 			if (r->parent || !r->start || !r->flags)
11503fd94c6bSBenjamin Herrenschmidt 				continue;
11513fd94c6bSBenjamin Herrenschmidt 			pci_claim_resource(dev, i);
11523fd94c6bSBenjamin Herrenschmidt 		}
11533fd94c6bSBenjamin Herrenschmidt 	}
11543fd94c6bSBenjamin Herrenschmidt 
11553fd94c6bSBenjamin Herrenschmidt 	list_for_each_entry(child_bus, &bus->children, node)
11563fd94c6bSBenjamin Herrenschmidt 		pcibios_claim_one_bus(child_bus);
11573fd94c6bSBenjamin Herrenschmidt }
11583fd94c6bSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(pcibios_claim_one_bus);
11593fd94c6bSBenjamin Herrenschmidt #endif /* CONFIG_HOTPLUG */
1160549beb9bSBenjamin Herrenschmidt 
1161549beb9bSBenjamin Herrenschmidt int pcibios_enable_device(struct pci_dev *dev, int mask)
1162549beb9bSBenjamin Herrenschmidt {
1163549beb9bSBenjamin Herrenschmidt 	u16 cmd, old_cmd;
1164549beb9bSBenjamin Herrenschmidt 	int idx;
1165549beb9bSBenjamin Herrenschmidt 	struct resource *r;
1166549beb9bSBenjamin Herrenschmidt 
1167549beb9bSBenjamin Herrenschmidt 	if (ppc_md.pcibios_enable_device_hook)
1168549beb9bSBenjamin Herrenschmidt 		if (ppc_md.pcibios_enable_device_hook(dev))
1169549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1170549beb9bSBenjamin Herrenschmidt 
1171549beb9bSBenjamin Herrenschmidt 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
1172549beb9bSBenjamin Herrenschmidt 	old_cmd = cmd;
1173549beb9bSBenjamin Herrenschmidt 	for (idx = 0; idx < PCI_NUM_RESOURCES; idx++) {
1174549beb9bSBenjamin Herrenschmidt 		/* Only set up the requested stuff */
1175549beb9bSBenjamin Herrenschmidt 		if (!(mask & (1 << idx)))
1176549beb9bSBenjamin Herrenschmidt 			continue;
1177549beb9bSBenjamin Herrenschmidt 		r = &dev->resource[idx];
1178549beb9bSBenjamin Herrenschmidt 		if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
1179549beb9bSBenjamin Herrenschmidt 			continue;
1180bcf988a1SBenjamin Herrenschmidt 		if ((idx == PCI_ROM_RESOURCE) &&
1181bcf988a1SBenjamin Herrenschmidt 				(!(r->flags & IORESOURCE_ROM_ENABLE)))
1182bcf988a1SBenjamin Herrenschmidt 			continue;
1183bcf988a1SBenjamin Herrenschmidt 		if (r->parent == NULL) {
1184549beb9bSBenjamin Herrenschmidt 			printk(KERN_ERR "PCI: Device %s not available because"
1185549beb9bSBenjamin Herrenschmidt 			       " of resource collisions\n", pci_name(dev));
1186549beb9bSBenjamin Herrenschmidt 			return -EINVAL;
1187549beb9bSBenjamin Herrenschmidt 		}
1188549beb9bSBenjamin Herrenschmidt 		if (r->flags & IORESOURCE_IO)
1189549beb9bSBenjamin Herrenschmidt 			cmd |= PCI_COMMAND_IO;
1190549beb9bSBenjamin Herrenschmidt 		if (r->flags & IORESOURCE_MEM)
1191549beb9bSBenjamin Herrenschmidt 			cmd |= PCI_COMMAND_MEMORY;
1192549beb9bSBenjamin Herrenschmidt 	}
1193549beb9bSBenjamin Herrenschmidt 	if (cmd != old_cmd) {
1194549beb9bSBenjamin Herrenschmidt 		printk("PCI: Enabling device %s (%04x -> %04x)\n",
1195549beb9bSBenjamin Herrenschmidt 		       pci_name(dev), old_cmd, cmd);
1196549beb9bSBenjamin Herrenschmidt 		pci_write_config_word(dev, PCI_COMMAND, cmd);
1197549beb9bSBenjamin Herrenschmidt 	}
1198549beb9bSBenjamin Herrenschmidt 	return 0;
1199549beb9bSBenjamin Herrenschmidt }
1200549beb9bSBenjamin Herrenschmidt 
1201