xref: /openbmc/linux/arch/powerpc/kernel/pci_32.c (revision 0ec6b5c1)
1e05b3b4aSPaul Mackerras /*
2e05b3b4aSPaul Mackerras  * Common pmac/prep/chrp pci routines. -- Cort
3e05b3b4aSPaul Mackerras  */
4e05b3b4aSPaul Mackerras 
5e05b3b4aSPaul Mackerras #include <linux/kernel.h>
6e05b3b4aSPaul Mackerras #include <linux/pci.h>
7e05b3b4aSPaul Mackerras #include <linux/delay.h>
8e05b3b4aSPaul Mackerras #include <linux/string.h>
9e05b3b4aSPaul Mackerras #include <linux/init.h>
10e05b3b4aSPaul Mackerras #include <linux/capability.h>
11e05b3b4aSPaul Mackerras #include <linux/sched.h>
12e05b3b4aSPaul Mackerras #include <linux/errno.h>
13e05b3b4aSPaul Mackerras #include <linux/bootmem.h>
146e99e458SBenjamin Herrenschmidt #include <linux/irq.h>
15f90bb153SBenjamin Herrenschmidt #include <linux/list.h>
16e05b3b4aSPaul Mackerras 
17e05b3b4aSPaul Mackerras #include <asm/processor.h>
18e05b3b4aSPaul Mackerras #include <asm/io.h>
19e05b3b4aSPaul Mackerras #include <asm/prom.h>
20e05b3b4aSPaul Mackerras #include <asm/sections.h>
21e05b3b4aSPaul Mackerras #include <asm/pci-bridge.h>
22e05b3b4aSPaul Mackerras #include <asm/byteorder.h>
23e05b3b4aSPaul Mackerras #include <asm/uaccess.h>
24e05b3b4aSPaul Mackerras #include <asm/machdep.h>
25e05b3b4aSPaul Mackerras 
26e05b3b4aSPaul Mackerras #undef DEBUG
27e05b3b4aSPaul Mackerras 
28e05b3b4aSPaul Mackerras #ifdef DEBUG
29e05b3b4aSPaul Mackerras #define DBG(x...) printk(x)
30e05b3b4aSPaul Mackerras #else
31e05b3b4aSPaul Mackerras #define DBG(x...)
32e05b3b4aSPaul Mackerras #endif
33e05b3b4aSPaul Mackerras 
34e05b3b4aSPaul Mackerras unsigned long isa_io_base     = 0;
35e05b3b4aSPaul Mackerras unsigned long pci_dram_offset = 0;
36e05b3b4aSPaul Mackerras int pcibios_assign_bus_offset = 1;
37e05b3b4aSPaul Mackerras 
38e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void);
39e05b3b4aSPaul Mackerras 
40e05b3b4aSPaul Mackerras static void pcibios_fixup_resources(struct pci_dev* dev);
41e05b3b4aSPaul Mackerras static void fixup_broken_pcnet32(struct pci_dev* dev);
42e05b3b4aSPaul Mackerras static int reparent_resources(struct resource *parent, struct resource *res);
43e05b3b4aSPaul Mackerras static void fixup_cpc710_pci64(struct pci_dev* dev);
44e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF
45e05b3b4aSPaul Mackerras static u8* pci_to_OF_bus_map;
46e05b3b4aSPaul Mackerras #endif
47e05b3b4aSPaul Mackerras 
48e05b3b4aSPaul Mackerras /* By default, we don't re-assign bus numbers. We do this only on
49e05b3b4aSPaul Mackerras  * some pmacs
50e05b3b4aSPaul Mackerras  */
51e05b3b4aSPaul Mackerras int pci_assign_all_buses;
52e05b3b4aSPaul Mackerras 
53a4c9e328SKumar Gala LIST_HEAD(hose_list);
54e05b3b4aSPaul Mackerras 
55e05b3b4aSPaul Mackerras static int pci_bus_count;
56e05b3b4aSPaul Mackerras 
57e05b3b4aSPaul Mackerras static void
582052d6d2SKumar Gala fixup_hide_host_resource_fsl(struct pci_dev* dev)
592052d6d2SKumar Gala {
602052d6d2SKumar Gala 	int i, class = dev->class >> 8;
612052d6d2SKumar Gala 
622052d6d2SKumar Gala 	if ((class == PCI_CLASS_PROCESSOR_POWERPC) &&
632052d6d2SKumar Gala 		(dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
642052d6d2SKumar Gala 		(dev->bus->parent == NULL)) {
652052d6d2SKumar Gala 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
662052d6d2SKumar Gala 			dev->resource[i].start = 0;
672052d6d2SKumar Gala 			dev->resource[i].end = 0;
682052d6d2SKumar Gala 			dev->resource[i].flags = 0;
692052d6d2SKumar Gala 		}
702052d6d2SKumar Gala 	}
712052d6d2SKumar Gala }
722052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
732052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
742052d6d2SKumar Gala 
752052d6d2SKumar Gala static void
76e05b3b4aSPaul Mackerras fixup_broken_pcnet32(struct pci_dev* dev)
77e05b3b4aSPaul Mackerras {
78e05b3b4aSPaul Mackerras 	if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
79e05b3b4aSPaul Mackerras 		dev->vendor = PCI_VENDOR_ID_AMD;
80e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
81e05b3b4aSPaul Mackerras 	}
82e05b3b4aSPaul Mackerras }
83e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT,	PCI_ANY_ID,			fixup_broken_pcnet32);
84e05b3b4aSPaul Mackerras 
85e05b3b4aSPaul Mackerras static void
86e05b3b4aSPaul Mackerras fixup_cpc710_pci64(struct pci_dev* dev)
87e05b3b4aSPaul Mackerras {
88e05b3b4aSPaul Mackerras 	/* Hide the PCI64 BARs from the kernel as their content doesn't
89e05b3b4aSPaul Mackerras 	 * fit well in the resource management
90e05b3b4aSPaul Mackerras 	 */
91e05b3b4aSPaul Mackerras 	dev->resource[0].start = dev->resource[0].end = 0;
92e05b3b4aSPaul Mackerras 	dev->resource[0].flags = 0;
93e05b3b4aSPaul Mackerras 	dev->resource[1].start = dev->resource[1].end = 0;
94e05b3b4aSPaul Mackerras 	dev->resource[1].flags = 0;
95e05b3b4aSPaul Mackerras }
96e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,	PCI_DEVICE_ID_IBM_CPC710_PCI64,	fixup_cpc710_pci64);
97e05b3b4aSPaul Mackerras 
98e05b3b4aSPaul Mackerras static void
99e05b3b4aSPaul Mackerras pcibios_fixup_resources(struct pci_dev *dev)
100e05b3b4aSPaul Mackerras {
101e05b3b4aSPaul Mackerras 	struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
102e05b3b4aSPaul Mackerras 	int i;
10305d3957eSBenjamin Herrenschmidt 	resource_size_t offset, mask;
104e05b3b4aSPaul Mackerras 
105e05b3b4aSPaul Mackerras 	if (!hose) {
106e05b3b4aSPaul Mackerras 		printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
107e05b3b4aSPaul Mackerras 		return;
108e05b3b4aSPaul Mackerras 	}
109e05b3b4aSPaul Mackerras 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
110e05b3b4aSPaul Mackerras 		struct resource *res = dev->resource + i;
111e05b3b4aSPaul Mackerras 		if (!res->flags)
112e05b3b4aSPaul Mackerras 			continue;
113e05b3b4aSPaul Mackerras 		if (res->end == 0xffffffff) {
114685143acSGreg Kroah-Hartman 			DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
115872455e2SSergei Shtylyov 			    pci_name(dev), i, (u64)res->start, (u64)res->end);
116e05b3b4aSPaul Mackerras 			res->end -= res->start;
117e05b3b4aSPaul Mackerras 			res->start = 0;
118e05b3b4aSPaul Mackerras 			res->flags |= IORESOURCE_UNSET;
119e05b3b4aSPaul Mackerras 			continue;
120e05b3b4aSPaul Mackerras 		}
121e05b3b4aSPaul Mackerras 		offset = 0;
12205d3957eSBenjamin Herrenschmidt 		mask = (resource_size_t)-1;
123e05b3b4aSPaul Mackerras 		if (res->flags & IORESOURCE_MEM) {
124e05b3b4aSPaul Mackerras 			offset = hose->pci_mem_offset;
125e05b3b4aSPaul Mackerras 		} else if (res->flags & IORESOURCE_IO) {
126e05b3b4aSPaul Mackerras 			offset = (unsigned long) hose->io_base_virt
127e05b3b4aSPaul Mackerras 				- isa_io_base;
12805d3957eSBenjamin Herrenschmidt 			mask = 0xffffffffu;
129e05b3b4aSPaul Mackerras 		}
130e05b3b4aSPaul Mackerras 		if (offset != 0) {
13105d3957eSBenjamin Herrenschmidt 			res->start = (res->start + offset) & mask;
13205d3957eSBenjamin Herrenschmidt 			res->end = (res->end + offset) & mask;
1330ec6b5c1SBenjamin Herrenschmidt 			DBG("PCI: Fixup res %d (0x%lx) of dev %s: %llx -> %llx\n",
134e05b3b4aSPaul Mackerras 			    i, res->flags, pci_name(dev),
135872455e2SSergei Shtylyov 			    (u64)res->start - offset, (u64)res->start);
136e05b3b4aSPaul Mackerras 		}
137e05b3b4aSPaul Mackerras 	}
138e05b3b4aSPaul Mackerras 
139e05b3b4aSPaul Mackerras 	/* Call machine specific resource fixup */
140e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_fixup_resources)
141e05b3b4aSPaul Mackerras 		ppc_md.pcibios_fixup_resources(dev);
142e05b3b4aSPaul Mackerras }
143e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID,		PCI_ANY_ID,			pcibios_fixup_resources);
144e05b3b4aSPaul Mackerras 
145e05b3b4aSPaul Mackerras void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
146e05b3b4aSPaul Mackerras 			struct resource *res)
147e05b3b4aSPaul Mackerras {
14805d3957eSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
149e05b3b4aSPaul Mackerras 	struct pci_controller *hose = dev->sysdata;
150e05b3b4aSPaul Mackerras 
15105d3957eSBenjamin Herrenschmidt 	if (hose && res->flags & IORESOURCE_IO) {
152e05b3b4aSPaul Mackerras 		offset = (unsigned long)hose->io_base_virt - isa_io_base;
15305d3957eSBenjamin Herrenschmidt 		mask = 0xffffffffu;
15405d3957eSBenjamin Herrenschmidt 	} else if (hose && res->flags & IORESOURCE_MEM)
155e05b3b4aSPaul Mackerras 		offset = hose->pci_mem_offset;
15605d3957eSBenjamin Herrenschmidt 	region->start = (res->start - offset) & mask;
15705d3957eSBenjamin Herrenschmidt 	region->end = (res->end - offset) & mask;
158e05b3b4aSPaul Mackerras }
159e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pcibios_resource_to_bus);
160e05b3b4aSPaul Mackerras 
161e05b3b4aSPaul Mackerras void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
162e05b3b4aSPaul Mackerras 			     struct pci_bus_region *region)
163e05b3b4aSPaul Mackerras {
16405d3957eSBenjamin Herrenschmidt 	resource_size_t offset = 0, mask = (resource_size_t)-1;
165e05b3b4aSPaul Mackerras 	struct pci_controller *hose = dev->sysdata;
166e05b3b4aSPaul Mackerras 
16705d3957eSBenjamin Herrenschmidt 	if (hose && res->flags & IORESOURCE_IO) {
168e05b3b4aSPaul Mackerras 		offset = (unsigned long)hose->io_base_virt - isa_io_base;
16905d3957eSBenjamin Herrenschmidt 		mask = 0xffffffffu;
17005d3957eSBenjamin Herrenschmidt 	} else if (hose && res->flags & IORESOURCE_MEM)
171e05b3b4aSPaul Mackerras 		offset = hose->pci_mem_offset;
17205d3957eSBenjamin Herrenschmidt 	res->start = (region->start + offset) & mask;
17305d3957eSBenjamin Herrenschmidt 	res->end = (region->end + offset) & mask;
174e05b3b4aSPaul Mackerras }
175e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pcibios_bus_to_resource);
176e05b3b4aSPaul Mackerras 
177e05b3b4aSPaul Mackerras /*
178e05b3b4aSPaul Mackerras  * We need to avoid collisions with `mirrored' VGA ports
179e05b3b4aSPaul Mackerras  * and other strange ISA hardware, so we always want the
180e05b3b4aSPaul Mackerras  * addresses to be allocated in the 0x000-0x0ff region
181e05b3b4aSPaul Mackerras  * modulo 0x400.
182e05b3b4aSPaul Mackerras  *
183e05b3b4aSPaul Mackerras  * Why? Because some silly external IO cards only decode
184e05b3b4aSPaul Mackerras  * the low 10 bits of the IO address. The 0x00-0xff region
185e05b3b4aSPaul Mackerras  * is reserved for motherboard devices that decode all 16
186e05b3b4aSPaul Mackerras  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
187e05b3b4aSPaul Mackerras  * but we want to try to avoid allocating at 0x2900-0x2bff
188e05b3b4aSPaul Mackerras  * which might have be mirrored at 0x0100-0x03ff..
189e05b3b4aSPaul Mackerras  */
190e31dd6e4SGreg Kroah-Hartman void pcibios_align_resource(void *data, struct resource *res,
191e31dd6e4SGreg Kroah-Hartman 				resource_size_t size, resource_size_t align)
192e05b3b4aSPaul Mackerras {
193e05b3b4aSPaul Mackerras 	struct pci_dev *dev = data;
194e05b3b4aSPaul Mackerras 
195e05b3b4aSPaul Mackerras 	if (res->flags & IORESOURCE_IO) {
196e31dd6e4SGreg Kroah-Hartman 		resource_size_t start = res->start;
197e05b3b4aSPaul Mackerras 
198e05b3b4aSPaul Mackerras 		if (start & 0x300) {
199e05b3b4aSPaul Mackerras 			start = (start + 0x3ff) & ~0x3ff;
200e05b3b4aSPaul Mackerras 			res->start = start;
201e05b3b4aSPaul Mackerras 		}
202e05b3b4aSPaul Mackerras 	}
203e05b3b4aSPaul Mackerras }
204e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pcibios_align_resource);
205e05b3b4aSPaul Mackerras 
206e05b3b4aSPaul Mackerras /*
207e05b3b4aSPaul Mackerras  *  Handle resources of PCI devices.  If the world were perfect, we could
208e05b3b4aSPaul Mackerras  *  just allocate all the resource regions and do nothing more.  It isn't.
209e05b3b4aSPaul Mackerras  *  On the other hand, we cannot just re-allocate all devices, as it would
210e05b3b4aSPaul Mackerras  *  require us to know lots of host bridge internals.  So we attempt to
211e05b3b4aSPaul Mackerras  *  keep as much of the original configuration as possible, but tweak it
212e05b3b4aSPaul Mackerras  *  when it's found to be wrong.
213e05b3b4aSPaul Mackerras  *
214e05b3b4aSPaul Mackerras  *  Known BIOS problems we have to work around:
215e05b3b4aSPaul Mackerras  *	- I/O or memory regions not configured
216e05b3b4aSPaul Mackerras  *	- regions configured, but not enabled in the command register
217e05b3b4aSPaul Mackerras  *	- bogus I/O addresses above 64K used
218e05b3b4aSPaul Mackerras  *	- expansion ROMs left enabled (this may sound harmless, but given
219e05b3b4aSPaul Mackerras  *	  the fact the PCI specs explicitly allow address decoders to be
220e05b3b4aSPaul Mackerras  *	  shared between expansion ROMs and other resource regions, it's
221e05b3b4aSPaul Mackerras  *	  at least dangerous)
222e05b3b4aSPaul Mackerras  *
223e05b3b4aSPaul Mackerras  *  Our solution:
224e05b3b4aSPaul Mackerras  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
225e05b3b4aSPaul Mackerras  *	    This gives us fixed barriers on where we can allocate.
226e05b3b4aSPaul Mackerras  *	(2) Allocate resources for all enabled devices.  If there is
227e05b3b4aSPaul Mackerras  *	    a collision, just mark the resource as unallocated. Also
228e05b3b4aSPaul Mackerras  *	    disable expansion ROMs during this step.
229e05b3b4aSPaul Mackerras  *	(3) Try to allocate resources for disabled devices.  If the
230e05b3b4aSPaul Mackerras  *	    resources were assigned correctly, everything goes well,
231e05b3b4aSPaul Mackerras  *	    if they weren't, they won't disturb allocation of other
232e05b3b4aSPaul Mackerras  *	    resources.
233e05b3b4aSPaul Mackerras  *	(4) Assign new addresses to resources which were either
234e05b3b4aSPaul Mackerras  *	    not configured at all or misconfigured.  If explicitly
235e05b3b4aSPaul Mackerras  *	    requested by the user, configure expansion ROM address
236e05b3b4aSPaul Mackerras  *	    as well.
237e05b3b4aSPaul Mackerras  */
238e05b3b4aSPaul Mackerras 
239e05b3b4aSPaul Mackerras static void __init
240e05b3b4aSPaul Mackerras pcibios_allocate_bus_resources(struct list_head *bus_list)
241e05b3b4aSPaul Mackerras {
242e05b3b4aSPaul Mackerras 	struct pci_bus *bus;
243e05b3b4aSPaul Mackerras 	int i;
244e05b3b4aSPaul Mackerras 	struct resource *res, *pr;
245e05b3b4aSPaul Mackerras 
246e05b3b4aSPaul Mackerras 	/* Depth-First Search on bus tree */
247e05b3b4aSPaul Mackerras 	list_for_each_entry(bus, bus_list, node) {
248e05b3b4aSPaul Mackerras 		for (i = 0; i < 4; ++i) {
249e05b3b4aSPaul Mackerras 			if ((res = bus->resource[i]) == NULL || !res->flags
250e05b3b4aSPaul Mackerras 			    || res->start > res->end)
251e05b3b4aSPaul Mackerras 				continue;
252e05b3b4aSPaul Mackerras 			if (bus->parent == NULL)
253e05b3b4aSPaul Mackerras 				pr = (res->flags & IORESOURCE_IO)?
254e05b3b4aSPaul Mackerras 					&ioport_resource: &iomem_resource;
255e05b3b4aSPaul Mackerras 			else {
256e05b3b4aSPaul Mackerras 				pr = pci_find_parent_resource(bus->self, res);
257e05b3b4aSPaul Mackerras 				if (pr == res) {
258e05b3b4aSPaul Mackerras 					/* this happens when the generic PCI
259e05b3b4aSPaul Mackerras 					 * code (wrongly) decides that this
260e05b3b4aSPaul Mackerras 					 * bridge is transparent  -- paulus
261e05b3b4aSPaul Mackerras 					 */
262e05b3b4aSPaul Mackerras 					continue;
263e05b3b4aSPaul Mackerras 				}
264e05b3b4aSPaul Mackerras 			}
265e05b3b4aSPaul Mackerras 
2660ec6b5c1SBenjamin Herrenschmidt 			DBG("PCI: dev %s (bus 0x%02x) bridge rsrc %d: %016llx..%016llx "
2670ec6b5c1SBenjamin Herrenschmidt 			    "(f:0x%08lx), parent %p\n",
2680ec6b5c1SBenjamin Herrenschmidt 			    bus->self ? pci_name(bus->self) : "PHB", bus->number, i,
269872455e2SSergei Shtylyov 			    (u64)res->start, (u64)res->end, res->flags, pr);
2700ec6b5c1SBenjamin Herrenschmidt 
2710ec6b5c1SBenjamin Herrenschmidt 			if (pr && !(pr->flags & IORESOURCE_UNSET)) {
272e05b3b4aSPaul Mackerras 				if (request_resource(pr, res) == 0)
273e05b3b4aSPaul Mackerras 					continue;
274e05b3b4aSPaul Mackerras 				/*
275e05b3b4aSPaul Mackerras 				 * Must be a conflict with an existing entry.
276e05b3b4aSPaul Mackerras 				 * Move that entry (or entries) under the
277e05b3b4aSPaul Mackerras 				 * bridge resource and try again.
278e05b3b4aSPaul Mackerras 				 */
279e05b3b4aSPaul Mackerras 				if (reparent_resources(pr, res) == 0)
280e05b3b4aSPaul Mackerras 					continue;
281e05b3b4aSPaul Mackerras 			}
2820ec6b5c1SBenjamin Herrenschmidt 			printk(KERN_WARNING
2830ec6b5c1SBenjamin Herrenschmidt 			       "PCI: Cannot allocate resource region "
2840ec6b5c1SBenjamin Herrenschmidt 			       "%d of PCI bridge %d, will remap\n",
2850ec6b5c1SBenjamin Herrenschmidt 			       i, bus->number);
2860ec6b5c1SBenjamin Herrenschmidt 			res->flags |= IORESOURCE_UNSET;
287e05b3b4aSPaul Mackerras 		}
288e05b3b4aSPaul Mackerras 		pcibios_allocate_bus_resources(&bus->children);
289e05b3b4aSPaul Mackerras 	}
290e05b3b4aSPaul Mackerras }
291e05b3b4aSPaul Mackerras 
292e05b3b4aSPaul Mackerras /*
293e05b3b4aSPaul Mackerras  * Reparent resource children of pr that conflict with res
294e05b3b4aSPaul Mackerras  * under res, and make res replace those children.
295e05b3b4aSPaul Mackerras  */
296e05b3b4aSPaul Mackerras static int __init
297e05b3b4aSPaul Mackerras reparent_resources(struct resource *parent, struct resource *res)
298e05b3b4aSPaul Mackerras {
299e05b3b4aSPaul Mackerras 	struct resource *p, **pp;
300e05b3b4aSPaul Mackerras 	struct resource **firstpp = NULL;
301e05b3b4aSPaul Mackerras 
302e05b3b4aSPaul Mackerras 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
303e05b3b4aSPaul Mackerras 		if (p->end < res->start)
304e05b3b4aSPaul Mackerras 			continue;
305e05b3b4aSPaul Mackerras 		if (res->end < p->start)
306e05b3b4aSPaul Mackerras 			break;
307e05b3b4aSPaul Mackerras 		if (p->start < res->start || p->end > res->end)
308e05b3b4aSPaul Mackerras 			return -1;	/* not completely contained */
309e05b3b4aSPaul Mackerras 		if (firstpp == NULL)
310e05b3b4aSPaul Mackerras 			firstpp = pp;
311e05b3b4aSPaul Mackerras 	}
312e05b3b4aSPaul Mackerras 	if (firstpp == NULL)
313e05b3b4aSPaul Mackerras 		return -1;	/* didn't find any conflicting entries? */
314e05b3b4aSPaul Mackerras 	res->parent = parent;
315e05b3b4aSPaul Mackerras 	res->child = *firstpp;
316e05b3b4aSPaul Mackerras 	res->sibling = *pp;
317e05b3b4aSPaul Mackerras 	*firstpp = res;
318e05b3b4aSPaul Mackerras 	*pp = NULL;
319e05b3b4aSPaul Mackerras 	for (p = res->child; p != NULL; p = p->sibling) {
320e05b3b4aSPaul Mackerras 		p->parent = res;
321685143acSGreg Kroah-Hartman 		DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
322872455e2SSergei Shtylyov 		    p->name, (u64)p->start, (u64)p->end, res->name);
323e05b3b4aSPaul Mackerras 	}
324e05b3b4aSPaul Mackerras 	return 0;
325e05b3b4aSPaul Mackerras }
326e05b3b4aSPaul Mackerras 
327f64fddbeSKumar Gala void __init
328f64fddbeSKumar Gala update_bridge_resource(struct pci_dev *dev, struct resource *res)
329e05b3b4aSPaul Mackerras {
330e05b3b4aSPaul Mackerras 	u8 io_base_lo, io_limit_lo;
331e05b3b4aSPaul Mackerras 	u16 mem_base, mem_limit;
332e05b3b4aSPaul Mackerras 	u16 cmd;
33305d3957eSBenjamin Herrenschmidt 	resource_size_t start, end, off;
334e05b3b4aSPaul Mackerras 	struct pci_controller *hose = dev->sysdata;
335e05b3b4aSPaul Mackerras 
336e05b3b4aSPaul Mackerras 	if (!hose) {
337e05b3b4aSPaul Mackerras 		printk("update_bridge_base: no hose?\n");
338e05b3b4aSPaul Mackerras 		return;
339e05b3b4aSPaul Mackerras 	}
340e05b3b4aSPaul Mackerras 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
341e05b3b4aSPaul Mackerras 	pci_write_config_word(dev, PCI_COMMAND,
342e05b3b4aSPaul Mackerras 			      cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
343e05b3b4aSPaul Mackerras 	if (res->flags & IORESOURCE_IO) {
344e05b3b4aSPaul Mackerras 		off = (unsigned long) hose->io_base_virt - isa_io_base;
345e05b3b4aSPaul Mackerras 		start = res->start - off;
346e05b3b4aSPaul Mackerras 		end = res->end - off;
347e05b3b4aSPaul Mackerras 		io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
348e05b3b4aSPaul Mackerras 		io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
34960b2a46cSRandy Vinson 		if (end > 0xffff)
35060b2a46cSRandy Vinson 			io_base_lo |= PCI_IO_RANGE_TYPE_32;
35160b2a46cSRandy Vinson 		else
35260b2a46cSRandy Vinson 			io_base_lo |= PCI_IO_RANGE_TYPE_16;
353e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
354e05b3b4aSPaul Mackerras 				start >> 16);
355e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
356e05b3b4aSPaul Mackerras 				end >> 16);
357e05b3b4aSPaul Mackerras 		pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
358e05b3b4aSPaul Mackerras 		pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
359e05b3b4aSPaul Mackerras 
360e05b3b4aSPaul Mackerras 	} else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
361e05b3b4aSPaul Mackerras 		   == IORESOURCE_MEM) {
362e05b3b4aSPaul Mackerras 		off = hose->pci_mem_offset;
363e05b3b4aSPaul Mackerras 		mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
364e05b3b4aSPaul Mackerras 		mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
365e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
366e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
367e05b3b4aSPaul Mackerras 
368e05b3b4aSPaul Mackerras 	} else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
369e05b3b4aSPaul Mackerras 		   == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
370e05b3b4aSPaul Mackerras 		off = hose->pci_mem_offset;
371e05b3b4aSPaul Mackerras 		mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
372e05b3b4aSPaul Mackerras 		mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
373e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
374e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
375e05b3b4aSPaul Mackerras 
376e05b3b4aSPaul Mackerras 	} else {
377f64fddbeSKumar Gala 		DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
378f64fddbeSKumar Gala 		    pci_name(dev), res->flags);
379e05b3b4aSPaul Mackerras 	}
380e05b3b4aSPaul Mackerras 	pci_write_config_word(dev, PCI_COMMAND, cmd);
381e05b3b4aSPaul Mackerras }
382e05b3b4aSPaul Mackerras 
383e05b3b4aSPaul Mackerras static inline void alloc_resource(struct pci_dev *dev, int idx)
384e05b3b4aSPaul Mackerras {
385e05b3b4aSPaul Mackerras 	struct resource *pr, *r = &dev->resource[idx];
386e05b3b4aSPaul Mackerras 
3870ec6b5c1SBenjamin Herrenschmidt 	DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx (f=%lx)\n",
388872455e2SSergei Shtylyov 	    pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
389e05b3b4aSPaul Mackerras 	pr = pci_find_parent_resource(dev, r);
3900ec6b5c1SBenjamin Herrenschmidt 	if (!pr || (pr->flags & IORESOURCE_UNSET) ||  request_resource(pr, r) < 0) {
3910ec6b5c1SBenjamin Herrenschmidt 		printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
3920ec6b5c1SBenjamin Herrenschmidt 		       " of device %s, will remap\n", idx, pci_name(dev));
393e05b3b4aSPaul Mackerras 		if (pr)
394685143acSGreg Kroah-Hartman 			DBG("PCI:  parent is %p: %016llx-%016llx (f=%lx)\n",
395872455e2SSergei Shtylyov 			    pr, (u64)pr->start, (u64)pr->end, pr->flags);
396e05b3b4aSPaul Mackerras 		/* We'll assign a new address later */
397e05b3b4aSPaul Mackerras 		r->flags |= IORESOURCE_UNSET;
398e05b3b4aSPaul Mackerras 		r->end -= r->start;
399e05b3b4aSPaul Mackerras 		r->start = 0;
400e05b3b4aSPaul Mackerras 	}
401e05b3b4aSPaul Mackerras }
402e05b3b4aSPaul Mackerras 
403e05b3b4aSPaul Mackerras static void __init
404e05b3b4aSPaul Mackerras pcibios_allocate_resources(int pass)
405e05b3b4aSPaul Mackerras {
406e05b3b4aSPaul Mackerras 	struct pci_dev *dev = NULL;
407e05b3b4aSPaul Mackerras 	int idx, disabled;
408e05b3b4aSPaul Mackerras 	u16 command;
409e05b3b4aSPaul Mackerras 	struct resource *r;
410e05b3b4aSPaul Mackerras 
411e05b3b4aSPaul Mackerras 	for_each_pci_dev(dev) {
412e05b3b4aSPaul Mackerras 		pci_read_config_word(dev, PCI_COMMAND, &command);
413e05b3b4aSPaul Mackerras 		for (idx = 0; idx < 6; idx++) {
414e05b3b4aSPaul Mackerras 			r = &dev->resource[idx];
415e05b3b4aSPaul Mackerras 			if (r->parent)		/* Already allocated */
416e05b3b4aSPaul Mackerras 				continue;
417e05b3b4aSPaul Mackerras 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
418e05b3b4aSPaul Mackerras 				continue;	/* Not assigned at all */
419e05b3b4aSPaul Mackerras 			if (r->flags & IORESOURCE_IO)
420e05b3b4aSPaul Mackerras 				disabled = !(command & PCI_COMMAND_IO);
421e05b3b4aSPaul Mackerras 			else
422e05b3b4aSPaul Mackerras 				disabled = !(command & PCI_COMMAND_MEMORY);
423e05b3b4aSPaul Mackerras 			if (pass == disabled)
424e05b3b4aSPaul Mackerras 				alloc_resource(dev, idx);
425e05b3b4aSPaul Mackerras 		}
426e05b3b4aSPaul Mackerras 		if (pass)
427e05b3b4aSPaul Mackerras 			continue;
428e05b3b4aSPaul Mackerras 		r = &dev->resource[PCI_ROM_RESOURCE];
429e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_ROM_ENABLE) {
430e05b3b4aSPaul Mackerras 			/* Turn the ROM off, leave the resource region, but keep it unregistered. */
431e05b3b4aSPaul Mackerras 			u32 reg;
432e05b3b4aSPaul Mackerras 			DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
433e05b3b4aSPaul Mackerras 			r->flags &= ~IORESOURCE_ROM_ENABLE;
434e05b3b4aSPaul Mackerras 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
435e05b3b4aSPaul Mackerras 			pci_write_config_dword(dev, dev->rom_base_reg,
436e05b3b4aSPaul Mackerras 					       reg & ~PCI_ROM_ADDRESS_ENABLE);
437e05b3b4aSPaul Mackerras 		}
438e05b3b4aSPaul Mackerras 	}
439e05b3b4aSPaul Mackerras }
440e05b3b4aSPaul Mackerras 
441e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF
442e05b3b4aSPaul Mackerras /*
443e05b3b4aSPaul Mackerras  * Functions below are used on OpenFirmware machines.
444e05b3b4aSPaul Mackerras  */
445e05b3b4aSPaul Mackerras static void
446e05b3b4aSPaul Mackerras make_one_node_map(struct device_node* node, u8 pci_bus)
447e05b3b4aSPaul Mackerras {
448a7f67bdfSJeremy Kerr 	const int *bus_range;
449e05b3b4aSPaul Mackerras 	int len;
450e05b3b4aSPaul Mackerras 
451e05b3b4aSPaul Mackerras 	if (pci_bus >= pci_bus_count)
452e05b3b4aSPaul Mackerras 		return;
453e2eb6392SStephen Rothwell 	bus_range = of_get_property(node, "bus-range", &len);
454e05b3b4aSPaul Mackerras 	if (bus_range == NULL || len < 2 * sizeof(int)) {
455e05b3b4aSPaul Mackerras 		printk(KERN_WARNING "Can't get bus-range for %s, "
456e05b3b4aSPaul Mackerras 		       "assuming it starts at 0\n", node->full_name);
457e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[pci_bus] = 0;
458e05b3b4aSPaul Mackerras 	} else
459e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[pci_bus] = bus_range[0];
460e05b3b4aSPaul Mackerras 
461e05b3b4aSPaul Mackerras 	for (node=node->child; node != 0;node = node->sibling) {
462e05b3b4aSPaul Mackerras 		struct pci_dev* dev;
463a7f67bdfSJeremy Kerr 		const unsigned int *class_code, *reg;
464e05b3b4aSPaul Mackerras 
465e2eb6392SStephen Rothwell 		class_code = of_get_property(node, "class-code", NULL);
466e05b3b4aSPaul Mackerras 		if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
467e05b3b4aSPaul Mackerras 			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
468e05b3b4aSPaul Mackerras 			continue;
469e2eb6392SStephen Rothwell 		reg = of_get_property(node, "reg", NULL);
470e05b3b4aSPaul Mackerras 		if (!reg)
471e05b3b4aSPaul Mackerras 			continue;
472ab462768SAlan Cox 		dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
473ab462768SAlan Cox 		if (!dev || !dev->subordinate) {
474ab462768SAlan Cox 			pci_dev_put(dev);
475e05b3b4aSPaul Mackerras 			continue;
476ab462768SAlan Cox 		}
477e05b3b4aSPaul Mackerras 		make_one_node_map(node, dev->subordinate->number);
478ab462768SAlan Cox 		pci_dev_put(dev);
479e05b3b4aSPaul Mackerras 	}
480e05b3b4aSPaul Mackerras }
481e05b3b4aSPaul Mackerras 
482e05b3b4aSPaul Mackerras void
483e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(void)
484e05b3b4aSPaul Mackerras {
485e05b3b4aSPaul Mackerras 	int i;
486a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
487a7f67bdfSJeremy Kerr 	struct property *map_prop;
4888c8dc322SStephen Rothwell 	struct device_node *dn;
489e05b3b4aSPaul Mackerras 
4905cbded58SRobert P. J. Day 	pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
491e05b3b4aSPaul Mackerras 	if (!pci_to_OF_bus_map) {
492e05b3b4aSPaul Mackerras 		printk(KERN_ERR "Can't allocate OF bus map !\n");
493e05b3b4aSPaul Mackerras 		return;
494e05b3b4aSPaul Mackerras 	}
495e05b3b4aSPaul Mackerras 
496e05b3b4aSPaul Mackerras 	/* We fill the bus map with invalid values, that helps
497e05b3b4aSPaul Mackerras 	 * debugging.
498e05b3b4aSPaul Mackerras 	 */
499e05b3b4aSPaul Mackerras 	for (i=0; i<pci_bus_count; i++)
500e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[i] = 0xff;
501e05b3b4aSPaul Mackerras 
502e05b3b4aSPaul Mackerras 	/* For each hose, we begin searching bridges */
503a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
50444ef3390SStephen Rothwell 		struct device_node* node = hose->dn;
50544ef3390SStephen Rothwell 
506e05b3b4aSPaul Mackerras 		if (!node)
507e05b3b4aSPaul Mackerras 			continue;
508e05b3b4aSPaul Mackerras 		make_one_node_map(node, hose->first_busno);
509e05b3b4aSPaul Mackerras 	}
5108c8dc322SStephen Rothwell 	dn = of_find_node_by_path("/");
5118c8dc322SStephen Rothwell 	map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
512a7f67bdfSJeremy Kerr 	if (map_prop) {
513a7f67bdfSJeremy Kerr 		BUG_ON(pci_bus_count > map_prop->length);
514a7f67bdfSJeremy Kerr 		memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
515a7f67bdfSJeremy Kerr 	}
5168c8dc322SStephen Rothwell 	of_node_put(dn);
517e05b3b4aSPaul Mackerras #ifdef DEBUG
518e05b3b4aSPaul Mackerras 	printk("PCI->OF bus map:\n");
519e05b3b4aSPaul Mackerras 	for (i=0; i<pci_bus_count; i++) {
520e05b3b4aSPaul Mackerras 		if (pci_to_OF_bus_map[i] == 0xff)
521e05b3b4aSPaul Mackerras 			continue;
522e05b3b4aSPaul Mackerras 		printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
523e05b3b4aSPaul Mackerras 	}
524e05b3b4aSPaul Mackerras #endif
525e05b3b4aSPaul Mackerras }
526e05b3b4aSPaul Mackerras 
527e05b3b4aSPaul Mackerras typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
528e05b3b4aSPaul Mackerras 
529e05b3b4aSPaul Mackerras static struct device_node*
530e05b3b4aSPaul Mackerras scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
531e05b3b4aSPaul Mackerras {
532e05b3b4aSPaul Mackerras 	struct device_node* sub_node;
533e05b3b4aSPaul Mackerras 
534e05b3b4aSPaul Mackerras 	for (; node != 0;node = node->sibling) {
535a7f67bdfSJeremy Kerr 		const unsigned int *class_code;
536e05b3b4aSPaul Mackerras 
537e05b3b4aSPaul Mackerras 		if (filter(node, data))
538e05b3b4aSPaul Mackerras 			return node;
539e05b3b4aSPaul Mackerras 
540e05b3b4aSPaul Mackerras 		/* For PCI<->PCI bridges or CardBus bridges, we go down
541e05b3b4aSPaul Mackerras 		 * Note: some OFs create a parent node "multifunc-device" as
542e05b3b4aSPaul Mackerras 		 * a fake root for all functions of a multi-function device,
543e05b3b4aSPaul Mackerras 		 * we go down them as well.
544e05b3b4aSPaul Mackerras 		 */
545e2eb6392SStephen Rothwell 		class_code = of_get_property(node, "class-code", NULL);
546e05b3b4aSPaul Mackerras 		if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
547e05b3b4aSPaul Mackerras 			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
548e05b3b4aSPaul Mackerras 			strcmp(node->name, "multifunc-device"))
549e05b3b4aSPaul Mackerras 			continue;
550e05b3b4aSPaul Mackerras 		sub_node = scan_OF_pci_childs(node->child, filter, data);
551e05b3b4aSPaul Mackerras 		if (sub_node)
552e05b3b4aSPaul Mackerras 			return sub_node;
553e05b3b4aSPaul Mackerras 	}
554e05b3b4aSPaul Mackerras 	return NULL;
555e05b3b4aSPaul Mackerras }
556e05b3b4aSPaul Mackerras 
557dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
558dae4828dSBenjamin Herrenschmidt 					       unsigned int devfn)
559e05b3b4aSPaul Mackerras {
560dae4828dSBenjamin Herrenschmidt 	struct device_node *np = NULL;
561dae4828dSBenjamin Herrenschmidt 	const u32 *reg;
562dae4828dSBenjamin Herrenschmidt 	unsigned int psize;
563e05b3b4aSPaul Mackerras 
564dae4828dSBenjamin Herrenschmidt 	while ((np = of_get_next_child(parent, np)) != NULL) {
565e2eb6392SStephen Rothwell 		reg = of_get_property(np, "reg", &psize);
566dae4828dSBenjamin Herrenschmidt 		if (reg == NULL || psize < 4)
567dae4828dSBenjamin Herrenschmidt 			continue;
568dae4828dSBenjamin Herrenschmidt 		if (((reg[0] >> 8) & 0xff) == devfn)
569dae4828dSBenjamin Herrenschmidt 			return np;
570dae4828dSBenjamin Herrenschmidt 	}
571dae4828dSBenjamin Herrenschmidt 	return NULL;
572e05b3b4aSPaul Mackerras }
573e05b3b4aSPaul Mackerras 
574e05b3b4aSPaul Mackerras 
575dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
576dae4828dSBenjamin Herrenschmidt {
577dae4828dSBenjamin Herrenschmidt 	struct device_node *parent, *np;
578dae4828dSBenjamin Herrenschmidt 
579dae4828dSBenjamin Herrenschmidt 	/* Are we a root bus ? */
580dae4828dSBenjamin Herrenschmidt 	if (bus->self == NULL || bus->parent == NULL) {
5810b1d40c4SKumar Gala 		struct pci_controller *hose = pci_bus_to_host(bus);
582dae4828dSBenjamin Herrenschmidt 		if (hose == NULL)
583dae4828dSBenjamin Herrenschmidt 			return NULL;
58444ef3390SStephen Rothwell 		return of_node_get(hose->dn);
585dae4828dSBenjamin Herrenschmidt 	}
586dae4828dSBenjamin Herrenschmidt 
587dae4828dSBenjamin Herrenschmidt 	/* not a root bus, we need to get our parent */
588dae4828dSBenjamin Herrenschmidt 	parent = scan_OF_for_pci_bus(bus->parent);
589dae4828dSBenjamin Herrenschmidt 	if (parent == NULL)
590dae4828dSBenjamin Herrenschmidt 		return NULL;
591dae4828dSBenjamin Herrenschmidt 
592dae4828dSBenjamin Herrenschmidt 	/* now iterate for children for a match */
593dae4828dSBenjamin Herrenschmidt 	np = scan_OF_for_pci_dev(parent, bus->self->devfn);
594dae4828dSBenjamin Herrenschmidt 	of_node_put(parent);
595dae4828dSBenjamin Herrenschmidt 
596dae4828dSBenjamin Herrenschmidt 	return np;
597e05b3b4aSPaul Mackerras }
598e05b3b4aSPaul Mackerras 
599e05b3b4aSPaul Mackerras /*
600e05b3b4aSPaul Mackerras  * Scans the OF tree for a device node matching a PCI device
601e05b3b4aSPaul Mackerras  */
602e05b3b4aSPaul Mackerras struct device_node *
603e05b3b4aSPaul Mackerras pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
604e05b3b4aSPaul Mackerras {
605dae4828dSBenjamin Herrenschmidt 	struct device_node *parent, *np;
606e05b3b4aSPaul Mackerras 
607e05b3b4aSPaul Mackerras 	if (!have_of)
608e05b3b4aSPaul Mackerras 		return NULL;
609e05b3b4aSPaul Mackerras 
610dae4828dSBenjamin Herrenschmidt 	DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
611dae4828dSBenjamin Herrenschmidt 	parent = scan_OF_for_pci_bus(bus);
612dae4828dSBenjamin Herrenschmidt 	if (parent == NULL)
613e05b3b4aSPaul Mackerras 		return NULL;
614dae4828dSBenjamin Herrenschmidt 	DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
615dae4828dSBenjamin Herrenschmidt 	np = scan_OF_for_pci_dev(parent, devfn);
616dae4828dSBenjamin Herrenschmidt 	of_node_put(parent);
617dae4828dSBenjamin Herrenschmidt 	DBG(" result is %s\n", np ? np->full_name : "<NULL>");
618e05b3b4aSPaul Mackerras 
619dae4828dSBenjamin Herrenschmidt 	/* XXX most callers don't release the returned node
620dae4828dSBenjamin Herrenschmidt 	 * mostly because ppc64 doesn't increase the refcount,
621dae4828dSBenjamin Herrenschmidt 	 * we need to fix that.
622e05b3b4aSPaul Mackerras 	 */
623dae4828dSBenjamin Herrenschmidt 	return np;
624e05b3b4aSPaul Mackerras }
625e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_busdev_to_OF_node);
626e05b3b4aSPaul Mackerras 
627e05b3b4aSPaul Mackerras struct device_node*
628e05b3b4aSPaul Mackerras pci_device_to_OF_node(struct pci_dev *dev)
629e05b3b4aSPaul Mackerras {
630e05b3b4aSPaul Mackerras 	return pci_busdev_to_OF_node(dev->bus, dev->devfn);
631e05b3b4aSPaul Mackerras }
632e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_to_OF_node);
633e05b3b4aSPaul Mackerras 
634e05b3b4aSPaul Mackerras static int
635e05b3b4aSPaul Mackerras find_OF_pci_device_filter(struct device_node* node, void* data)
636e05b3b4aSPaul Mackerras {
637e05b3b4aSPaul Mackerras 	return ((void *)node == data);
638e05b3b4aSPaul Mackerras }
639e05b3b4aSPaul Mackerras 
640e05b3b4aSPaul Mackerras /*
641e05b3b4aSPaul Mackerras  * Returns the PCI device matching a given OF node
642e05b3b4aSPaul Mackerras  */
643e05b3b4aSPaul Mackerras int
644e05b3b4aSPaul Mackerras pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
645e05b3b4aSPaul Mackerras {
646a7f67bdfSJeremy Kerr 	const unsigned int *reg;
647e05b3b4aSPaul Mackerras 	struct pci_controller* hose;
648e05b3b4aSPaul Mackerras 	struct pci_dev* dev = NULL;
649e05b3b4aSPaul Mackerras 
650e05b3b4aSPaul Mackerras 	if (!have_of)
651e05b3b4aSPaul Mackerras 		return -ENODEV;
652e05b3b4aSPaul Mackerras 	/* Make sure it's really a PCI device */
653e05b3b4aSPaul Mackerras 	hose = pci_find_hose_for_OF_device(node);
65444ef3390SStephen Rothwell 	if (!hose || !hose->dn)
655e05b3b4aSPaul Mackerras 		return -ENODEV;
65644ef3390SStephen Rothwell 	if (!scan_OF_pci_childs(hose->dn->child,
657e05b3b4aSPaul Mackerras 			find_OF_pci_device_filter, (void *)node))
658e05b3b4aSPaul Mackerras 		return -ENODEV;
659e2eb6392SStephen Rothwell 	reg = of_get_property(node, "reg", NULL);
660e05b3b4aSPaul Mackerras 	if (!reg)
661e05b3b4aSPaul Mackerras 		return -ENODEV;
662e05b3b4aSPaul Mackerras 	*bus = (reg[0] >> 16) & 0xff;
663e05b3b4aSPaul Mackerras 	*devfn = ((reg[0] >> 8) & 0xff);
664e05b3b4aSPaul Mackerras 
665e05b3b4aSPaul Mackerras 	/* Ok, here we need some tweak. If we have already renumbered
666e05b3b4aSPaul Mackerras 	 * all busses, we can't rely on the OF bus number any more.
667e05b3b4aSPaul Mackerras 	 * the pci_to_OF_bus_map is not enough as several PCI busses
668e05b3b4aSPaul Mackerras 	 * may match the same OF bus number.
669e05b3b4aSPaul Mackerras 	 */
670e05b3b4aSPaul Mackerras 	if (!pci_to_OF_bus_map)
671e05b3b4aSPaul Mackerras 		return 0;
672e05b3b4aSPaul Mackerras 
673e05b3b4aSPaul Mackerras 	for_each_pci_dev(dev)
674e05b3b4aSPaul Mackerras 		if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
675e05b3b4aSPaul Mackerras 				dev->devfn == *devfn) {
676e05b3b4aSPaul Mackerras 			*bus = dev->bus->number;
677e05b3b4aSPaul Mackerras 			pci_dev_put(dev);
678e05b3b4aSPaul Mackerras 			return 0;
679e05b3b4aSPaul Mackerras 		}
680e05b3b4aSPaul Mackerras 
681e05b3b4aSPaul Mackerras 	return -ENODEV;
682e05b3b4aSPaul Mackerras }
683e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node);
684e05b3b4aSPaul Mackerras 
685e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the
686e05b3b4aSPaul Mackerras  * /proc device tree
687e05b3b4aSPaul Mackerras  */
688e05b3b4aSPaul Mackerras void __init
689e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void)
690e05b3b4aSPaul Mackerras {
691e05b3b4aSPaul Mackerras 	struct property* of_prop;
6928c8dc322SStephen Rothwell 	struct device_node *dn;
693e05b3b4aSPaul Mackerras 
694e05b3b4aSPaul Mackerras 	of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
6958c8dc322SStephen Rothwell 	if (!of_prop)
6968c8dc322SStephen Rothwell 		return;
6978c8dc322SStephen Rothwell 	dn = of_find_node_by_path("/");
6988c8dc322SStephen Rothwell 	if (dn) {
699e05b3b4aSPaul Mackerras 		memset(of_prop, -1, sizeof(struct property) + 256);
700e05b3b4aSPaul Mackerras 		of_prop->name = "pci-OF-bus-map";
701e05b3b4aSPaul Mackerras 		of_prop->length = 256;
7021a38147eSStephen Rothwell 		of_prop->value = &of_prop[1];
7038c8dc322SStephen Rothwell 		prom_add_property(dn, of_prop);
7048c8dc322SStephen Rothwell 		of_node_put(dn);
705e05b3b4aSPaul Mackerras 	}
706e05b3b4aSPaul Mackerras }
707e05b3b4aSPaul Mackerras 
708e05b3b4aSPaul Mackerras #else /* CONFIG_PPC_OF */
709e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void)
710e05b3b4aSPaul Mackerras {
711e05b3b4aSPaul Mackerras }
712e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */
713e05b3b4aSPaul Mackerras 
714e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_PMAC
715e05b3b4aSPaul Mackerras /*
716e05b3b4aSPaul Mackerras  * This set of routines checks for PCI<->PCI bridges that have closed
717e05b3b4aSPaul Mackerras  * IO resources and have child devices. It tries to re-open an IO
718e05b3b4aSPaul Mackerras  * window on them.
719e05b3b4aSPaul Mackerras  *
720e05b3b4aSPaul Mackerras  * This is a _temporary_ fix to workaround a problem with Apple's OF
721e05b3b4aSPaul Mackerras  * closing IO windows on P2P bridges when the OF drivers of cards
722e05b3b4aSPaul Mackerras  * below this bridge don't claim any IO range (typically ATI or
723e05b3b4aSPaul Mackerras  * Adaptec).
724e05b3b4aSPaul Mackerras  *
725e05b3b4aSPaul Mackerras  * A more complete fix would be to use drivers/pci/setup-bus.c, which
726e05b3b4aSPaul Mackerras  * involves a working pcibios_fixup_pbus_ranges(), some more care about
727e05b3b4aSPaul Mackerras  * ordering when creating the host bus resources, and maybe a few more
728e05b3b4aSPaul Mackerras  * minor tweaks
729e05b3b4aSPaul Mackerras  */
730e05b3b4aSPaul Mackerras 
731e05b3b4aSPaul Mackerras /* Initialize bridges with base/limit values we have collected */
732e05b3b4aSPaul Mackerras static void __init
733e05b3b4aSPaul Mackerras do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
734e05b3b4aSPaul Mackerras {
735e05b3b4aSPaul Mackerras 	struct pci_dev *bridge = bus->self;
736e05b3b4aSPaul Mackerras 	struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
737e05b3b4aSPaul Mackerras 	u32 l;
738e05b3b4aSPaul Mackerras 	u16 w;
739e05b3b4aSPaul Mackerras 	struct resource res;
740e05b3b4aSPaul Mackerras 
741e05b3b4aSPaul Mackerras 	if (bus->resource[0] == NULL)
742e05b3b4aSPaul Mackerras 		return;
743e05b3b4aSPaul Mackerras  	res = *(bus->resource[0]);
744e05b3b4aSPaul Mackerras 
745e05b3b4aSPaul Mackerras 	DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
746e05b3b4aSPaul Mackerras 	res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
747e05b3b4aSPaul Mackerras 	res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
748685143acSGreg Kroah-Hartman 	DBG("  IO window: %016llx-%016llx\n", res.start, res.end);
749e05b3b4aSPaul Mackerras 
750e05b3b4aSPaul Mackerras 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
751e05b3b4aSPaul Mackerras 	pci_read_config_dword(bridge, PCI_IO_BASE, &l);
752e05b3b4aSPaul Mackerras 	l &= 0xffff000f;
753e05b3b4aSPaul Mackerras 	l |= (res.start >> 8) & 0x00f0;
754e05b3b4aSPaul Mackerras 	l |= res.end & 0xf000;
755e05b3b4aSPaul Mackerras 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
756e05b3b4aSPaul Mackerras 
757e05b3b4aSPaul Mackerras 	if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
758e05b3b4aSPaul Mackerras 		l = (res.start >> 16) | (res.end & 0xffff0000);
759e05b3b4aSPaul Mackerras 		pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
760e05b3b4aSPaul Mackerras 	}
761e05b3b4aSPaul Mackerras 
762e05b3b4aSPaul Mackerras 	pci_read_config_word(bridge, PCI_COMMAND, &w);
763e05b3b4aSPaul Mackerras 	w |= PCI_COMMAND_IO;
764e05b3b4aSPaul Mackerras 	pci_write_config_word(bridge, PCI_COMMAND, w);
765e05b3b4aSPaul Mackerras 
766e05b3b4aSPaul Mackerras #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
767e05b3b4aSPaul Mackerras 	if (enable_vga) {
768e05b3b4aSPaul Mackerras 		pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
769e05b3b4aSPaul Mackerras 		w |= PCI_BRIDGE_CTL_VGA;
770e05b3b4aSPaul Mackerras 		pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
771e05b3b4aSPaul Mackerras 	}
772e05b3b4aSPaul Mackerras #endif
773e05b3b4aSPaul Mackerras }
774e05b3b4aSPaul Mackerras 
775e05b3b4aSPaul Mackerras /* This function is pretty basic and actually quite broken for the
776e05b3b4aSPaul Mackerras  * general case, it's enough for us right now though. It's supposed
777e05b3b4aSPaul Mackerras  * to tell us if we need to open an IO range at all or not and what
778e05b3b4aSPaul Mackerras  * size.
779e05b3b4aSPaul Mackerras  */
780e05b3b4aSPaul Mackerras static int __init
781e05b3b4aSPaul Mackerras check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
782e05b3b4aSPaul Mackerras {
783e05b3b4aSPaul Mackerras 	struct pci_dev *dev;
784e05b3b4aSPaul Mackerras 	int	i;
785e05b3b4aSPaul Mackerras 	int	rc = 0;
786e05b3b4aSPaul Mackerras 
7870f582bc1SPaul Mackerras #define push_end(res, mask) do {		\
7880f582bc1SPaul Mackerras 	BUG_ON((mask+1) & mask);		\
7890f582bc1SPaul Mackerras 	res->end = (res->end + mask) | mask;	\
790e05b3b4aSPaul Mackerras } while (0)
791e05b3b4aSPaul Mackerras 
792e05b3b4aSPaul Mackerras 	list_for_each_entry(dev, &bus->devices, bus_list) {
793e05b3b4aSPaul Mackerras 		u16 class = dev->class >> 8;
794e05b3b4aSPaul Mackerras 
795e05b3b4aSPaul Mackerras 		if (class == PCI_CLASS_DISPLAY_VGA ||
796e05b3b4aSPaul Mackerras 		    class == PCI_CLASS_NOT_DEFINED_VGA)
797e05b3b4aSPaul Mackerras 			*found_vga = 1;
798e05b3b4aSPaul Mackerras 		if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
799e05b3b4aSPaul Mackerras 			rc |= check_for_io_childs(dev->subordinate, res, found_vga);
800e05b3b4aSPaul Mackerras 		if (class == PCI_CLASS_BRIDGE_CARDBUS)
801e05b3b4aSPaul Mackerras 			push_end(res, 0xfff);
802e05b3b4aSPaul Mackerras 
803e05b3b4aSPaul Mackerras 		for (i=0; i<PCI_NUM_RESOURCES; i++) {
804e05b3b4aSPaul Mackerras 			struct resource *r;
805e05b3b4aSPaul Mackerras 			unsigned long r_size;
806e05b3b4aSPaul Mackerras 
807e05b3b4aSPaul Mackerras 			if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
808e05b3b4aSPaul Mackerras 			    && i >= PCI_BRIDGE_RESOURCES)
809e05b3b4aSPaul Mackerras 				continue;
810e05b3b4aSPaul Mackerras 			r = &dev->resource[i];
811e05b3b4aSPaul Mackerras 			r_size = r->end - r->start;
812e05b3b4aSPaul Mackerras 			if (r_size < 0xfff)
813e05b3b4aSPaul Mackerras 				r_size = 0xfff;
814e05b3b4aSPaul Mackerras 			if (r->flags & IORESOURCE_IO && (r_size) != 0) {
815e05b3b4aSPaul Mackerras 				rc = 1;
816e05b3b4aSPaul Mackerras 				push_end(res, r_size);
817e05b3b4aSPaul Mackerras 			}
818e05b3b4aSPaul Mackerras 		}
819e05b3b4aSPaul Mackerras 	}
820e05b3b4aSPaul Mackerras 
821e05b3b4aSPaul Mackerras 	return rc;
822e05b3b4aSPaul Mackerras }
823e05b3b4aSPaul Mackerras 
824e05b3b4aSPaul Mackerras /* Here we scan all P2P bridges of a given level that have a closed
825e05b3b4aSPaul Mackerras  * IO window. Note that the test for the presence of a VGA card should
826e05b3b4aSPaul Mackerras  * be improved to take into account already configured P2P bridges,
827e05b3b4aSPaul Mackerras  * currently, we don't see them and might end up configuring 2 bridges
828e05b3b4aSPaul Mackerras  * with VGA pass through enabled
829e05b3b4aSPaul Mackerras  */
830e05b3b4aSPaul Mackerras static void __init
831e05b3b4aSPaul Mackerras do_fixup_p2p_level(struct pci_bus *bus)
832e05b3b4aSPaul Mackerras {
833e05b3b4aSPaul Mackerras 	struct pci_bus *b;
834e05b3b4aSPaul Mackerras 	int i, parent_io;
835e05b3b4aSPaul Mackerras 	int has_vga = 0;
836e05b3b4aSPaul Mackerras 
837e05b3b4aSPaul Mackerras 	for (parent_io=0; parent_io<4; parent_io++)
838e05b3b4aSPaul Mackerras 		if (bus->resource[parent_io]
839e05b3b4aSPaul Mackerras 		    && bus->resource[parent_io]->flags & IORESOURCE_IO)
840e05b3b4aSPaul Mackerras 			break;
841e05b3b4aSPaul Mackerras 	if (parent_io >= 4)
842e05b3b4aSPaul Mackerras 		return;
843e05b3b4aSPaul Mackerras 
844e05b3b4aSPaul Mackerras 	list_for_each_entry(b, &bus->children, node) {
845e05b3b4aSPaul Mackerras 		struct pci_dev *d = b->self;
846e05b3b4aSPaul Mackerras 		struct pci_controller* hose = (struct pci_controller *)d->sysdata;
847e05b3b4aSPaul Mackerras 		struct resource *res = b->resource[0];
848e05b3b4aSPaul Mackerras 		struct resource tmp_res;
849e05b3b4aSPaul Mackerras 		unsigned long max;
850e05b3b4aSPaul Mackerras 		int found_vga = 0;
851e05b3b4aSPaul Mackerras 
852e05b3b4aSPaul Mackerras 		memset(&tmp_res, 0, sizeof(tmp_res));
853e05b3b4aSPaul Mackerras 		tmp_res.start = bus->resource[parent_io]->start;
854e05b3b4aSPaul Mackerras 
855e05b3b4aSPaul Mackerras 		/* We don't let low addresses go through that closed P2P bridge, well,
856e05b3b4aSPaul Mackerras 		 * that may not be necessary but I feel safer that way
857e05b3b4aSPaul Mackerras 		 */
858e05b3b4aSPaul Mackerras 		if (tmp_res.start == 0)
859e05b3b4aSPaul Mackerras 			tmp_res.start = 0x1000;
860e05b3b4aSPaul Mackerras 
861e05b3b4aSPaul Mackerras 		if (!list_empty(&b->devices) && res && res->flags == 0 &&
862e05b3b4aSPaul Mackerras 		    res != bus->resource[parent_io] &&
863e05b3b4aSPaul Mackerras 		    (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
864e05b3b4aSPaul Mackerras 		    check_for_io_childs(b, &tmp_res, &found_vga)) {
865e05b3b4aSPaul Mackerras 			u8 io_base_lo;
866e05b3b4aSPaul Mackerras 
867e05b3b4aSPaul Mackerras 			printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
868e05b3b4aSPaul Mackerras 
869e05b3b4aSPaul Mackerras 			if (found_vga) {
870e05b3b4aSPaul Mackerras 				if (has_vga) {
871e05b3b4aSPaul Mackerras 					printk(KERN_WARNING "Skipping VGA, already active"
872e05b3b4aSPaul Mackerras 					    " on bus segment\n");
873e05b3b4aSPaul Mackerras 					found_vga = 0;
874e05b3b4aSPaul Mackerras 				} else
875e05b3b4aSPaul Mackerras 					has_vga = 1;
876e05b3b4aSPaul Mackerras 			}
877e05b3b4aSPaul Mackerras 			pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
878e05b3b4aSPaul Mackerras 
879e05b3b4aSPaul Mackerras 			if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
880e05b3b4aSPaul Mackerras 				max = ((unsigned long) hose->io_base_virt
881e05b3b4aSPaul Mackerras 					- isa_io_base) + 0xffffffff;
882e05b3b4aSPaul Mackerras 			else
883e05b3b4aSPaul Mackerras 				max = ((unsigned long) hose->io_base_virt
884e05b3b4aSPaul Mackerras 					- isa_io_base) + 0xffff;
885e05b3b4aSPaul Mackerras 
886e05b3b4aSPaul Mackerras 			*res = tmp_res;
887e05b3b4aSPaul Mackerras 			res->flags = IORESOURCE_IO;
888e05b3b4aSPaul Mackerras 			res->name = b->name;
889e05b3b4aSPaul Mackerras 
890e05b3b4aSPaul Mackerras 			/* Find a resource in the parent where we can allocate */
891e05b3b4aSPaul Mackerras 			for (i = 0 ; i < 4; i++) {
892e05b3b4aSPaul Mackerras 				struct resource *r = bus->resource[i];
893e05b3b4aSPaul Mackerras 				if (!r)
894e05b3b4aSPaul Mackerras 					continue;
895e05b3b4aSPaul Mackerras 				if ((r->flags & IORESOURCE_IO) == 0)
896e05b3b4aSPaul Mackerras 					continue;
897685143acSGreg Kroah-Hartman 				DBG("Trying to allocate from %016llx, size %016llx from parent"
898685143acSGreg Kroah-Hartman 				    " res %d: %016llx -> %016llx\n",
899e05b3b4aSPaul Mackerras 					res->start, res->end, i, r->start, r->end);
900e05b3b4aSPaul Mackerras 
901e05b3b4aSPaul Mackerras 				if (allocate_resource(r, res, res->end + 1, res->start, max,
902e05b3b4aSPaul Mackerras 				    res->end + 1, NULL, NULL) < 0) {
903e05b3b4aSPaul Mackerras 					DBG("Failed !\n");
904e05b3b4aSPaul Mackerras 					continue;
905e05b3b4aSPaul Mackerras 				}
906e05b3b4aSPaul Mackerras 				do_update_p2p_io_resource(b, found_vga);
907e05b3b4aSPaul Mackerras 				break;
908e05b3b4aSPaul Mackerras 			}
909e05b3b4aSPaul Mackerras 		}
910e05b3b4aSPaul Mackerras 		do_fixup_p2p_level(b);
911e05b3b4aSPaul Mackerras 	}
912e05b3b4aSPaul Mackerras }
913e05b3b4aSPaul Mackerras 
914e05b3b4aSPaul Mackerras static void
915e05b3b4aSPaul Mackerras pcibios_fixup_p2p_bridges(void)
916e05b3b4aSPaul Mackerras {
917e05b3b4aSPaul Mackerras 	struct pci_bus *b;
918e05b3b4aSPaul Mackerras 
919e05b3b4aSPaul Mackerras 	list_for_each_entry(b, &pci_root_buses, node)
920e05b3b4aSPaul Mackerras 		do_fixup_p2p_level(b);
921e05b3b4aSPaul Mackerras }
922e05b3b4aSPaul Mackerras 
923e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_PMAC */
924e05b3b4aSPaul Mackerras 
925e05b3b4aSPaul Mackerras static int __init
926e05b3b4aSPaul Mackerras pcibios_init(void)
927e05b3b4aSPaul Mackerras {
928a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
929e05b3b4aSPaul Mackerras 	struct pci_bus *bus;
930a4c9e328SKumar Gala 	int next_busno = 0;
931e05b3b4aSPaul Mackerras 
932e05b3b4aSPaul Mackerras 	printk(KERN_INFO "PCI: Probing PCI hardware\n");
933e05b3b4aSPaul Mackerras 
934e05b3b4aSPaul Mackerras 	/* Scan all of the recorded PCI controllers.  */
935a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
936e05b3b4aSPaul Mackerras 		if (pci_assign_all_buses)
937e05b3b4aSPaul Mackerras 			hose->first_busno = next_busno;
938e05b3b4aSPaul Mackerras 		hose->last_busno = 0xff;
939803d4573SBenjamin Herrenschmidt 		bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
940803d4573SBenjamin Herrenschmidt 					    hose->ops, hose);
941803d4573SBenjamin Herrenschmidt 		if (bus)
942803d4573SBenjamin Herrenschmidt 			pci_bus_add_devices(bus);
943e05b3b4aSPaul Mackerras 		hose->last_busno = bus->subordinate;
944e05b3b4aSPaul Mackerras 		if (pci_assign_all_buses || next_busno <= hose->last_busno)
945e05b3b4aSPaul Mackerras 			next_busno = hose->last_busno + pcibios_assign_bus_offset;
946e05b3b4aSPaul Mackerras 	}
947e05b3b4aSPaul Mackerras 	pci_bus_count = next_busno;
948e05b3b4aSPaul Mackerras 
949e05b3b4aSPaul Mackerras 	/* OpenFirmware based machines need a map of OF bus
950e05b3b4aSPaul Mackerras 	 * numbers vs. kernel bus numbers since we may have to
951e05b3b4aSPaul Mackerras 	 * remap them.
952e05b3b4aSPaul Mackerras 	 */
953e05b3b4aSPaul Mackerras 	if (pci_assign_all_buses && have_of)
954e05b3b4aSPaul Mackerras 		pcibios_make_OF_bus_map();
955e05b3b4aSPaul Mackerras 
956e05b3b4aSPaul Mackerras 	/* Call machine dependent fixup */
957e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_fixup)
958e05b3b4aSPaul Mackerras 		ppc_md.pcibios_fixup();
959e05b3b4aSPaul Mackerras 
960e05b3b4aSPaul Mackerras 	/* Allocate and assign resources */
961e05b3b4aSPaul Mackerras 	pcibios_allocate_bus_resources(&pci_root_buses);
962e05b3b4aSPaul Mackerras 	pcibios_allocate_resources(0);
963e05b3b4aSPaul Mackerras 	pcibios_allocate_resources(1);
964e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_PMAC
965e05b3b4aSPaul Mackerras 	pcibios_fixup_p2p_bridges();
966e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_PMAC */
9670ec6b5c1SBenjamin Herrenschmidt 	DBG("PCI: Assigning unassigned resouces...\n");
9680ec6b5c1SBenjamin Herrenschmidt 	pci_assign_unassigned_resources();
969e05b3b4aSPaul Mackerras 
970e05b3b4aSPaul Mackerras 	/* Call machine dependent post-init code */
971e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_after_init)
972e05b3b4aSPaul Mackerras 		ppc_md.pcibios_after_init();
973e05b3b4aSPaul Mackerras 
974e05b3b4aSPaul Mackerras 	return 0;
975e05b3b4aSPaul Mackerras }
976e05b3b4aSPaul Mackerras 
977e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init);
978e05b3b4aSPaul Mackerras 
979282045b4SKumar Gala void pcibios_fixup_bus(struct pci_bus *bus)
980e05b3b4aSPaul Mackerras {
981e05b3b4aSPaul Mackerras 	struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
982e05b3b4aSPaul Mackerras 	unsigned long io_offset;
983e05b3b4aSPaul Mackerras 	struct resource *res;
984f90bb153SBenjamin Herrenschmidt 	struct pci_dev *dev;
985e05b3b4aSPaul Mackerras 	int i;
986e05b3b4aSPaul Mackerras 
987e05b3b4aSPaul Mackerras 	io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
988e05b3b4aSPaul Mackerras 	if (bus->parent == NULL) {
989e05b3b4aSPaul Mackerras 		/* This is a host bridge - fill in its resources */
990e05b3b4aSPaul Mackerras 		hose->bus = bus;
991e05b3b4aSPaul Mackerras 
992e05b3b4aSPaul Mackerras 		bus->resource[0] = res = &hose->io_resource;
993e05b3b4aSPaul Mackerras 		if (!res->flags) {
994e05b3b4aSPaul Mackerras 			if (io_offset)
995e05b3b4aSPaul Mackerras 				printk(KERN_ERR "I/O resource not set for host"
9965516b540SKumar Gala 				       " bridge %d\n", hose->global_number);
997e05b3b4aSPaul Mackerras 			res->start = 0;
998e05b3b4aSPaul Mackerras 			res->end = IO_SPACE_LIMIT;
999e05b3b4aSPaul Mackerras 			res->flags = IORESOURCE_IO;
1000e05b3b4aSPaul Mackerras 		}
100105d3957eSBenjamin Herrenschmidt 		res->start = (res->start + io_offset) & 0xffffffffu;
100205d3957eSBenjamin Herrenschmidt 		res->end = (res->end + io_offset) & 0xffffffffu;
1003e05b3b4aSPaul Mackerras 
1004e05b3b4aSPaul Mackerras 		for (i = 0; i < 3; ++i) {
1005e05b3b4aSPaul Mackerras 			res = &hose->mem_resources[i];
1006e05b3b4aSPaul Mackerras 			if (!res->flags) {
1007e05b3b4aSPaul Mackerras 				if (i > 0)
1008e05b3b4aSPaul Mackerras 					continue;
1009e05b3b4aSPaul Mackerras 				printk(KERN_ERR "Memory resource not set for "
10105516b540SKumar Gala 				       "host bridge %d\n", hose->global_number);
1011e05b3b4aSPaul Mackerras 				res->start = hose->pci_mem_offset;
1012e05b3b4aSPaul Mackerras 				res->end = ~0U;
1013e05b3b4aSPaul Mackerras 				res->flags = IORESOURCE_MEM;
1014e05b3b4aSPaul Mackerras 			}
1015e05b3b4aSPaul Mackerras 			bus->resource[i+1] = res;
1016e05b3b4aSPaul Mackerras 		}
1017e05b3b4aSPaul Mackerras 	} else {
1018e05b3b4aSPaul Mackerras 		/* This is a subordinate bridge */
1019e05b3b4aSPaul Mackerras 		pci_read_bridge_bases(bus);
1020e05b3b4aSPaul Mackerras 
1021e05b3b4aSPaul Mackerras 		for (i = 0; i < 4; ++i) {
1022e05b3b4aSPaul Mackerras 			if ((res = bus->resource[i]) == NULL)
1023e05b3b4aSPaul Mackerras 				continue;
10246d8ff10cSYork Sun 			if (!res->flags || bus->self->transparent)
1025e05b3b4aSPaul Mackerras 				continue;
1026e05b3b4aSPaul Mackerras 			if (io_offset && (res->flags & IORESOURCE_IO)) {
102705d3957eSBenjamin Herrenschmidt 				res->start = (res->start + io_offset) &
102805d3957eSBenjamin Herrenschmidt 					0xffffffffu;
102905d3957eSBenjamin Herrenschmidt 				res->end = (res->end + io_offset) &
103005d3957eSBenjamin Herrenschmidt 					0xffffffffu;
1031e05b3b4aSPaul Mackerras 			} else if (hose->pci_mem_offset
1032e05b3b4aSPaul Mackerras 				   && (res->flags & IORESOURCE_MEM)) {
1033e05b3b4aSPaul Mackerras 				res->start += hose->pci_mem_offset;
1034e05b3b4aSPaul Mackerras 				res->end += hose->pci_mem_offset;
1035e05b3b4aSPaul Mackerras 			}
1036e05b3b4aSPaul Mackerras 		}
1037e05b3b4aSPaul Mackerras 	}
1038e05b3b4aSPaul Mackerras 
1039f90bb153SBenjamin Herrenschmidt 	/* Platform specific bus fixups */
1040e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_fixup_bus)
1041e05b3b4aSPaul Mackerras 		ppc_md.pcibios_fixup_bus(bus);
1042f90bb153SBenjamin Herrenschmidt 
1043f90bb153SBenjamin Herrenschmidt 	/* Read default IRQs and fixup if necessary */
1044f90bb153SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
1045f90bb153SBenjamin Herrenschmidt 		pci_read_irq_line(dev);
1046f90bb153SBenjamin Herrenschmidt 		if (ppc_md.pci_irq_fixup)
1047f90bb153SBenjamin Herrenschmidt 			ppc_md.pci_irq_fixup(dev);
1048f90bb153SBenjamin Herrenschmidt 	}
1049e05b3b4aSPaul Mackerras }
1050e05b3b4aSPaul Mackerras 
1051e05b3b4aSPaul Mackerras /* the next one is stolen from the alpha port... */
1052e05b3b4aSPaul Mackerras void __init
1053e05b3b4aSPaul Mackerras pcibios_update_irq(struct pci_dev *dev, int irq)
1054e05b3b4aSPaul Mackerras {
1055e05b3b4aSPaul Mackerras 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1056e05b3b4aSPaul Mackerras 	/* XXX FIXME - update OF device tree node interrupt property */
1057e05b3b4aSPaul Mackerras }
1058e05b3b4aSPaul Mackerras 
1059e05b3b4aSPaul Mackerras int pcibios_enable_device(struct pci_dev *dev, int mask)
1060e05b3b4aSPaul Mackerras {
1061e05b3b4aSPaul Mackerras 	u16 cmd, old_cmd;
1062e05b3b4aSPaul Mackerras 	int idx;
1063e05b3b4aSPaul Mackerras 	struct resource *r;
1064e05b3b4aSPaul Mackerras 
1065e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_enable_device_hook)
1066e05b3b4aSPaul Mackerras 		if (ppc_md.pcibios_enable_device_hook(dev, 0))
1067e05b3b4aSPaul Mackerras 			return -EINVAL;
1068e05b3b4aSPaul Mackerras 
1069e05b3b4aSPaul Mackerras 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
1070e05b3b4aSPaul Mackerras 	old_cmd = cmd;
1071e05b3b4aSPaul Mackerras 	for (idx=0; idx<6; idx++) {
1072e05b3b4aSPaul Mackerras 		r = &dev->resource[idx];
1073e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_UNSET) {
1074e05b3b4aSPaul Mackerras 			printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1075e05b3b4aSPaul Mackerras 			return -EINVAL;
1076e05b3b4aSPaul Mackerras 		}
1077e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_IO)
1078e05b3b4aSPaul Mackerras 			cmd |= PCI_COMMAND_IO;
1079e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_MEM)
1080e05b3b4aSPaul Mackerras 			cmd |= PCI_COMMAND_MEMORY;
1081e05b3b4aSPaul Mackerras 	}
1082e05b3b4aSPaul Mackerras 	if (cmd != old_cmd) {
1083e05b3b4aSPaul Mackerras 		printk("PCI: Enabling device %s (%04x -> %04x)\n",
1084e05b3b4aSPaul Mackerras 		       pci_name(dev), old_cmd, cmd);
1085e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_COMMAND, cmd);
1086e05b3b4aSPaul Mackerras 	}
1087e05b3b4aSPaul Mackerras 	return 0;
1088e05b3b4aSPaul Mackerras }
1089e05b3b4aSPaul Mackerras 
10900b1d40c4SKumar Gala static struct pci_controller*
1091e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus)
1092e05b3b4aSPaul Mackerras {
1093a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
1094e05b3b4aSPaul Mackerras 
1095a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
1096e05b3b4aSPaul Mackerras 		if (bus >= hose->first_busno && bus <= hose->last_busno)
1097e05b3b4aSPaul Mackerras 			return hose;
1098e05b3b4aSPaul Mackerras 	return NULL;
1099e05b3b4aSPaul Mackerras }
1100e05b3b4aSPaul Mackerras 
1101e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical
1102e05b3b4aSPaul Mackerras  * memory.  Do this on a per-card basis so that we choose the right
1103e05b3b4aSPaul Mackerras  * root bridge.
1104e05b3b4aSPaul Mackerras  * Note that the returned IO or memory base is a physical address
1105e05b3b4aSPaul Mackerras  */
1106e05b3b4aSPaul Mackerras 
1107e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1108e05b3b4aSPaul Mackerras {
1109e05b3b4aSPaul Mackerras 	struct pci_controller* hose;
1110e05b3b4aSPaul Mackerras 	long result = -EOPNOTSUPP;
1111e05b3b4aSPaul Mackerras 
1112e05b3b4aSPaul Mackerras 	/* Argh ! Please forgive me for that hack, but that's the
1113e05b3b4aSPaul Mackerras 	 * simplest way to get existing XFree to not lockup on some
1114e05b3b4aSPaul Mackerras 	 * G5 machines... So when something asks for bus 0 io base
1115e05b3b4aSPaul Mackerras 	 * (bus 0 is HT root), we return the AGP one instead.
1116e05b3b4aSPaul Mackerras 	 */
1117e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_PMAC
1118e8222502SBenjamin Herrenschmidt 	if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
1119e05b3b4aSPaul Mackerras 		if (bus == 0)
1120e05b3b4aSPaul Mackerras 			bus = 0xf0;
1121e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_PMAC */
1122e05b3b4aSPaul Mackerras 
1123e05b3b4aSPaul Mackerras 	hose = pci_bus_to_hose(bus);
1124e05b3b4aSPaul Mackerras 	if (!hose)
1125e05b3b4aSPaul Mackerras 		return -ENODEV;
1126e05b3b4aSPaul Mackerras 
1127e05b3b4aSPaul Mackerras 	switch (which) {
1128e05b3b4aSPaul Mackerras 	case IOBASE_BRIDGE_NUMBER:
1129e05b3b4aSPaul Mackerras 		return (long)hose->first_busno;
1130e05b3b4aSPaul Mackerras 	case IOBASE_MEMORY:
1131e05b3b4aSPaul Mackerras 		return (long)hose->pci_mem_offset;
1132e05b3b4aSPaul Mackerras 	case IOBASE_IO:
1133e05b3b4aSPaul Mackerras 		return (long)hose->io_base_phys;
1134e05b3b4aSPaul Mackerras 	case IOBASE_ISA_IO:
1135e05b3b4aSPaul Mackerras 		return (long)isa_io_base;
1136e05b3b4aSPaul Mackerras 	case IOBASE_ISA_MEM:
1137e05b3b4aSPaul Mackerras 		return (long)isa_mem_base;
1138e05b3b4aSPaul Mackerras 	}
1139e05b3b4aSPaul Mackerras 
1140e05b3b4aSPaul Mackerras 	return result;
1141e05b3b4aSPaul Mackerras }
1142e05b3b4aSPaul Mackerras 
1143e05b3b4aSPaul Mackerras unsigned long pci_address_to_pio(phys_addr_t address)
1144e05b3b4aSPaul Mackerras {
1145a4c9e328SKumar Gala 	struct pci_controller *hose, *tmp;
1146e05b3b4aSPaul Mackerras 
1147a4c9e328SKumar Gala 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1148e05b3b4aSPaul Mackerras 		unsigned int size = hose->io_resource.end -
1149e05b3b4aSPaul Mackerras 			hose->io_resource.start + 1;
1150e05b3b4aSPaul Mackerras 		if (address >= hose->io_base_phys &&
1151e05b3b4aSPaul Mackerras 		    address < (hose->io_base_phys + size)) {
1152e05b3b4aSPaul Mackerras 			unsigned long base =
1153e05b3b4aSPaul Mackerras 				(unsigned long)hose->io_base_virt - _IO_BASE;
1154e05b3b4aSPaul Mackerras 			return base + (address - hose->io_base_phys);
1155e05b3b4aSPaul Mackerras 		}
1156e05b3b4aSPaul Mackerras 	}
1157e05b3b4aSPaul Mackerras 	return (unsigned int)-1;
1158e05b3b4aSPaul Mackerras }
1159e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_address_to_pio);
1160e05b3b4aSPaul Mackerras 
1161e05b3b4aSPaul Mackerras /*
1162e05b3b4aSPaul Mackerras  * Null PCI config access functions, for the case when we can't
1163e05b3b4aSPaul Mackerras  * find a hose.
1164e05b3b4aSPaul Mackerras  */
1165e05b3b4aSPaul Mackerras #define NULL_PCI_OP(rw, size, type)					\
1166e05b3b4aSPaul Mackerras static int								\
1167e05b3b4aSPaul Mackerras null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
1168e05b3b4aSPaul Mackerras {									\
1169e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
1170e05b3b4aSPaul Mackerras }
1171e05b3b4aSPaul Mackerras 
1172e05b3b4aSPaul Mackerras static int
1173e05b3b4aSPaul Mackerras null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1174e05b3b4aSPaul Mackerras 		 int len, u32 *val)
1175e05b3b4aSPaul Mackerras {
1176e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;
1177e05b3b4aSPaul Mackerras }
1178e05b3b4aSPaul Mackerras 
1179e05b3b4aSPaul Mackerras static int
1180e05b3b4aSPaul Mackerras null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1181e05b3b4aSPaul Mackerras 		  int len, u32 val)
1182e05b3b4aSPaul Mackerras {
1183e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;
1184e05b3b4aSPaul Mackerras }
1185e05b3b4aSPaul Mackerras 
1186e05b3b4aSPaul Mackerras static struct pci_ops null_pci_ops =
1187e05b3b4aSPaul Mackerras {
11886127d1c0SNathan Lynch 	.read = null_read_config,
11896127d1c0SNathan Lynch 	.write = null_write_config,
1190e05b3b4aSPaul Mackerras };
1191e05b3b4aSPaul Mackerras 
1192e05b3b4aSPaul Mackerras /*
1193e05b3b4aSPaul Mackerras  * These functions are used early on before PCI scanning is done
1194e05b3b4aSPaul Mackerras  * and all of the pci_dev and pci_bus structures have been created.
1195e05b3b4aSPaul Mackerras  */
1196e05b3b4aSPaul Mackerras static struct pci_bus *
1197e05b3b4aSPaul Mackerras fake_pci_bus(struct pci_controller *hose, int busnr)
1198e05b3b4aSPaul Mackerras {
1199e05b3b4aSPaul Mackerras 	static struct pci_bus bus;
1200e05b3b4aSPaul Mackerras 
1201e05b3b4aSPaul Mackerras 	if (hose == 0) {
1202e05b3b4aSPaul Mackerras 		hose = pci_bus_to_hose(busnr);
1203e05b3b4aSPaul Mackerras 		if (hose == 0)
1204e05b3b4aSPaul Mackerras 			printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1205e05b3b4aSPaul Mackerras 	}
1206e05b3b4aSPaul Mackerras 	bus.number = busnr;
1207e05b3b4aSPaul Mackerras 	bus.sysdata = hose;
1208e05b3b4aSPaul Mackerras 	bus.ops = hose? hose->ops: &null_pci_ops;
1209e05b3b4aSPaul Mackerras 	return &bus;
1210e05b3b4aSPaul Mackerras }
1211e05b3b4aSPaul Mackerras 
1212e05b3b4aSPaul Mackerras #define EARLY_PCI_OP(rw, size, type)					\
1213e05b3b4aSPaul Mackerras int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
1214e05b3b4aSPaul Mackerras 			       int devfn, int offset, type value)	\
1215e05b3b4aSPaul Mackerras {									\
1216e05b3b4aSPaul Mackerras 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
1217e05b3b4aSPaul Mackerras 					    devfn, offset, value);	\
1218e05b3b4aSPaul Mackerras }
1219e05b3b4aSPaul Mackerras 
1220e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, byte, u8 *)
1221e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, word, u16 *)
1222e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, dword, u32 *)
1223e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, byte, u8)
1224e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, word, u16)
1225e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, dword, u32)
122638805e5fSKumar Gala 
122738805e5fSKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
122838805e5fSKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn,
122938805e5fSKumar Gala 			  int cap)
123038805e5fSKumar Gala {
123138805e5fSKumar Gala 	return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
123238805e5fSKumar Gala }
1233