xref: /openbmc/linux/arch/powerpc/kernel/pci_32.c (revision 8c8dc322)
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 isa_mem_base    = 0;
36e05b3b4aSPaul Mackerras unsigned long pci_dram_offset = 0;
37e05b3b4aSPaul Mackerras int pcibios_assign_bus_offset = 1;
38e05b3b4aSPaul Mackerras 
39e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void);
40e05b3b4aSPaul Mackerras 
41e05b3b4aSPaul Mackerras static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
42e05b3b4aSPaul Mackerras static int probe_resource(struct pci_bus *parent, struct resource *pr,
43e05b3b4aSPaul Mackerras 			  struct resource *res, struct resource **conflict);
44e05b3b4aSPaul Mackerras static void update_bridge_base(struct pci_bus *bus, int i);
45e05b3b4aSPaul Mackerras static void pcibios_fixup_resources(struct pci_dev* dev);
46e05b3b4aSPaul Mackerras static void fixup_broken_pcnet32(struct pci_dev* dev);
47e05b3b4aSPaul Mackerras static int reparent_resources(struct resource *parent, struct resource *res);
48e05b3b4aSPaul Mackerras static void fixup_cpc710_pci64(struct pci_dev* dev);
49e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF
50e05b3b4aSPaul Mackerras static u8* pci_to_OF_bus_map;
51e05b3b4aSPaul Mackerras #endif
52e05b3b4aSPaul Mackerras 
53e05b3b4aSPaul Mackerras /* By default, we don't re-assign bus numbers. We do this only on
54e05b3b4aSPaul Mackerras  * some pmacs
55e05b3b4aSPaul Mackerras  */
56e05b3b4aSPaul Mackerras int pci_assign_all_buses;
57e05b3b4aSPaul Mackerras 
58e05b3b4aSPaul Mackerras struct pci_controller* hose_head;
59e05b3b4aSPaul Mackerras struct pci_controller** hose_tail = &hose_head;
60e05b3b4aSPaul Mackerras 
61e05b3b4aSPaul Mackerras static int pci_bus_count;
62e05b3b4aSPaul Mackerras 
63e05b3b4aSPaul Mackerras static void
64e05b3b4aSPaul Mackerras fixup_broken_pcnet32(struct pci_dev* dev)
65e05b3b4aSPaul Mackerras {
66e05b3b4aSPaul Mackerras 	if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
67e05b3b4aSPaul Mackerras 		dev->vendor = PCI_VENDOR_ID_AMD;
68e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
69e05b3b4aSPaul Mackerras 	}
70e05b3b4aSPaul Mackerras }
71e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT,	PCI_ANY_ID,			fixup_broken_pcnet32);
72e05b3b4aSPaul Mackerras 
73e05b3b4aSPaul Mackerras static void
74e05b3b4aSPaul Mackerras fixup_cpc710_pci64(struct pci_dev* dev)
75e05b3b4aSPaul Mackerras {
76e05b3b4aSPaul Mackerras 	/* Hide the PCI64 BARs from the kernel as their content doesn't
77e05b3b4aSPaul Mackerras 	 * fit well in the resource management
78e05b3b4aSPaul Mackerras 	 */
79e05b3b4aSPaul Mackerras 	dev->resource[0].start = dev->resource[0].end = 0;
80e05b3b4aSPaul Mackerras 	dev->resource[0].flags = 0;
81e05b3b4aSPaul Mackerras 	dev->resource[1].start = dev->resource[1].end = 0;
82e05b3b4aSPaul Mackerras 	dev->resource[1].flags = 0;
83e05b3b4aSPaul Mackerras }
84e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,	PCI_DEVICE_ID_IBM_CPC710_PCI64,	fixup_cpc710_pci64);
85e05b3b4aSPaul Mackerras 
86e05b3b4aSPaul Mackerras static void
87e05b3b4aSPaul Mackerras pcibios_fixup_resources(struct pci_dev *dev)
88e05b3b4aSPaul Mackerras {
89e05b3b4aSPaul Mackerras 	struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
90e05b3b4aSPaul Mackerras 	int i;
91e05b3b4aSPaul Mackerras 	unsigned long offset;
92e05b3b4aSPaul Mackerras 
93e05b3b4aSPaul Mackerras 	if (!hose) {
94e05b3b4aSPaul Mackerras 		printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
95e05b3b4aSPaul Mackerras 		return;
96e05b3b4aSPaul Mackerras 	}
97e05b3b4aSPaul Mackerras 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
98e05b3b4aSPaul Mackerras 		struct resource *res = dev->resource + i;
99e05b3b4aSPaul Mackerras 		if (!res->flags)
100e05b3b4aSPaul Mackerras 			continue;
101e05b3b4aSPaul Mackerras 		if (res->end == 0xffffffff) {
102685143acSGreg Kroah-Hartman 			DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
103872455e2SSergei Shtylyov 			    pci_name(dev), i, (u64)res->start, (u64)res->end);
104e05b3b4aSPaul Mackerras 			res->end -= res->start;
105e05b3b4aSPaul Mackerras 			res->start = 0;
106e05b3b4aSPaul Mackerras 			res->flags |= IORESOURCE_UNSET;
107e05b3b4aSPaul Mackerras 			continue;
108e05b3b4aSPaul Mackerras 		}
109e05b3b4aSPaul Mackerras 		offset = 0;
110e05b3b4aSPaul Mackerras 		if (res->flags & IORESOURCE_MEM) {
111e05b3b4aSPaul Mackerras 			offset = hose->pci_mem_offset;
112e05b3b4aSPaul Mackerras 		} else if (res->flags & IORESOURCE_IO) {
113e05b3b4aSPaul Mackerras 			offset = (unsigned long) hose->io_base_virt
114e05b3b4aSPaul Mackerras 				- isa_io_base;
115e05b3b4aSPaul Mackerras 		}
116e05b3b4aSPaul Mackerras 		if (offset != 0) {
117e05b3b4aSPaul Mackerras 			res->start += offset;
118e05b3b4aSPaul Mackerras 			res->end += offset;
119872455e2SSergei Shtylyov 			DBG("Fixup res %d (%lx) of dev %s: %llx -> %llx\n",
120e05b3b4aSPaul Mackerras 			    i, res->flags, pci_name(dev),
121872455e2SSergei Shtylyov 			    (u64)res->start - offset, (u64)res->start);
122e05b3b4aSPaul Mackerras 		}
123e05b3b4aSPaul Mackerras 	}
124e05b3b4aSPaul Mackerras 
125e05b3b4aSPaul Mackerras 	/* Call machine specific resource fixup */
126e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_fixup_resources)
127e05b3b4aSPaul Mackerras 		ppc_md.pcibios_fixup_resources(dev);
128e05b3b4aSPaul Mackerras }
129e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID,		PCI_ANY_ID,			pcibios_fixup_resources);
130e05b3b4aSPaul Mackerras 
131e05b3b4aSPaul Mackerras void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
132e05b3b4aSPaul Mackerras 			struct resource *res)
133e05b3b4aSPaul Mackerras {
134e05b3b4aSPaul Mackerras 	unsigned long offset = 0;
135e05b3b4aSPaul Mackerras 	struct pci_controller *hose = dev->sysdata;
136e05b3b4aSPaul Mackerras 
137e05b3b4aSPaul Mackerras 	if (hose && res->flags & IORESOURCE_IO)
138e05b3b4aSPaul Mackerras 		offset = (unsigned long)hose->io_base_virt - isa_io_base;
139e05b3b4aSPaul Mackerras 	else if (hose && res->flags & IORESOURCE_MEM)
140e05b3b4aSPaul Mackerras 		offset = hose->pci_mem_offset;
141e05b3b4aSPaul Mackerras 	region->start = res->start - offset;
142e05b3b4aSPaul Mackerras 	region->end = res->end - offset;
143e05b3b4aSPaul Mackerras }
144e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pcibios_resource_to_bus);
145e05b3b4aSPaul Mackerras 
146e05b3b4aSPaul Mackerras void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
147e05b3b4aSPaul Mackerras 			     struct pci_bus_region *region)
148e05b3b4aSPaul Mackerras {
149e05b3b4aSPaul Mackerras 	unsigned long offset = 0;
150e05b3b4aSPaul Mackerras 	struct pci_controller *hose = dev->sysdata;
151e05b3b4aSPaul Mackerras 
152e05b3b4aSPaul Mackerras 	if (hose && res->flags & IORESOURCE_IO)
153e05b3b4aSPaul Mackerras 		offset = (unsigned long)hose->io_base_virt - isa_io_base;
154e05b3b4aSPaul Mackerras 	else if (hose && res->flags & IORESOURCE_MEM)
155e05b3b4aSPaul Mackerras 		offset = hose->pci_mem_offset;
156e05b3b4aSPaul Mackerras 	res->start = region->start + offset;
157e05b3b4aSPaul Mackerras 	res->end = region->end + offset;
158e05b3b4aSPaul Mackerras }
159e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pcibios_bus_to_resource);
160e05b3b4aSPaul Mackerras 
161e05b3b4aSPaul Mackerras /*
162e05b3b4aSPaul Mackerras  * We need to avoid collisions with `mirrored' VGA ports
163e05b3b4aSPaul Mackerras  * and other strange ISA hardware, so we always want the
164e05b3b4aSPaul Mackerras  * addresses to be allocated in the 0x000-0x0ff region
165e05b3b4aSPaul Mackerras  * modulo 0x400.
166e05b3b4aSPaul Mackerras  *
167e05b3b4aSPaul Mackerras  * Why? Because some silly external IO cards only decode
168e05b3b4aSPaul Mackerras  * the low 10 bits of the IO address. The 0x00-0xff region
169e05b3b4aSPaul Mackerras  * is reserved for motherboard devices that decode all 16
170e05b3b4aSPaul Mackerras  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
171e05b3b4aSPaul Mackerras  * but we want to try to avoid allocating at 0x2900-0x2bff
172e05b3b4aSPaul Mackerras  * which might have be mirrored at 0x0100-0x03ff..
173e05b3b4aSPaul Mackerras  */
174e31dd6e4SGreg Kroah-Hartman void pcibios_align_resource(void *data, struct resource *res,
175e31dd6e4SGreg Kroah-Hartman 				resource_size_t size, resource_size_t align)
176e05b3b4aSPaul Mackerras {
177e05b3b4aSPaul Mackerras 	struct pci_dev *dev = data;
178e05b3b4aSPaul Mackerras 
179e05b3b4aSPaul Mackerras 	if (res->flags & IORESOURCE_IO) {
180e31dd6e4SGreg Kroah-Hartman 		resource_size_t start = res->start;
181e05b3b4aSPaul Mackerras 
182e05b3b4aSPaul Mackerras 		if (size > 0x100) {
183e05b3b4aSPaul Mackerras 			printk(KERN_ERR "PCI: I/O Region %s/%d too large"
184685143acSGreg Kroah-Hartman 			       " (%lld bytes)\n", pci_name(dev),
185e31dd6e4SGreg Kroah-Hartman 			       dev->resource - res, (unsigned long long)size);
186e05b3b4aSPaul Mackerras 		}
187e05b3b4aSPaul Mackerras 
188e05b3b4aSPaul Mackerras 		if (start & 0x300) {
189e05b3b4aSPaul Mackerras 			start = (start + 0x3ff) & ~0x3ff;
190e05b3b4aSPaul Mackerras 			res->start = start;
191e05b3b4aSPaul Mackerras 		}
192e05b3b4aSPaul Mackerras 	}
193e05b3b4aSPaul Mackerras }
194e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pcibios_align_resource);
195e05b3b4aSPaul Mackerras 
196e05b3b4aSPaul Mackerras /*
197e05b3b4aSPaul Mackerras  *  Handle resources of PCI devices.  If the world were perfect, we could
198e05b3b4aSPaul Mackerras  *  just allocate all the resource regions and do nothing more.  It isn't.
199e05b3b4aSPaul Mackerras  *  On the other hand, we cannot just re-allocate all devices, as it would
200e05b3b4aSPaul Mackerras  *  require us to know lots of host bridge internals.  So we attempt to
201e05b3b4aSPaul Mackerras  *  keep as much of the original configuration as possible, but tweak it
202e05b3b4aSPaul Mackerras  *  when it's found to be wrong.
203e05b3b4aSPaul Mackerras  *
204e05b3b4aSPaul Mackerras  *  Known BIOS problems we have to work around:
205e05b3b4aSPaul Mackerras  *	- I/O or memory regions not configured
206e05b3b4aSPaul Mackerras  *	- regions configured, but not enabled in the command register
207e05b3b4aSPaul Mackerras  *	- bogus I/O addresses above 64K used
208e05b3b4aSPaul Mackerras  *	- expansion ROMs left enabled (this may sound harmless, but given
209e05b3b4aSPaul Mackerras  *	  the fact the PCI specs explicitly allow address decoders to be
210e05b3b4aSPaul Mackerras  *	  shared between expansion ROMs and other resource regions, it's
211e05b3b4aSPaul Mackerras  *	  at least dangerous)
212e05b3b4aSPaul Mackerras  *
213e05b3b4aSPaul Mackerras  *  Our solution:
214e05b3b4aSPaul Mackerras  *	(1) Allocate resources for all buses behind PCI-to-PCI bridges.
215e05b3b4aSPaul Mackerras  *	    This gives us fixed barriers on where we can allocate.
216e05b3b4aSPaul Mackerras  *	(2) Allocate resources for all enabled devices.  If there is
217e05b3b4aSPaul Mackerras  *	    a collision, just mark the resource as unallocated. Also
218e05b3b4aSPaul Mackerras  *	    disable expansion ROMs during this step.
219e05b3b4aSPaul Mackerras  *	(3) Try to allocate resources for disabled devices.  If the
220e05b3b4aSPaul Mackerras  *	    resources were assigned correctly, everything goes well,
221e05b3b4aSPaul Mackerras  *	    if they weren't, they won't disturb allocation of other
222e05b3b4aSPaul Mackerras  *	    resources.
223e05b3b4aSPaul Mackerras  *	(4) Assign new addresses to resources which were either
224e05b3b4aSPaul Mackerras  *	    not configured at all or misconfigured.  If explicitly
225e05b3b4aSPaul Mackerras  *	    requested by the user, configure expansion ROM address
226e05b3b4aSPaul Mackerras  *	    as well.
227e05b3b4aSPaul Mackerras  */
228e05b3b4aSPaul Mackerras 
229e05b3b4aSPaul Mackerras static void __init
230e05b3b4aSPaul Mackerras pcibios_allocate_bus_resources(struct list_head *bus_list)
231e05b3b4aSPaul Mackerras {
232e05b3b4aSPaul Mackerras 	struct pci_bus *bus;
233e05b3b4aSPaul Mackerras 	int i;
234e05b3b4aSPaul Mackerras 	struct resource *res, *pr;
235e05b3b4aSPaul Mackerras 
236e05b3b4aSPaul Mackerras 	/* Depth-First Search on bus tree */
237e05b3b4aSPaul Mackerras 	list_for_each_entry(bus, bus_list, node) {
238e05b3b4aSPaul Mackerras 		for (i = 0; i < 4; ++i) {
239e05b3b4aSPaul Mackerras 			if ((res = bus->resource[i]) == NULL || !res->flags
240e05b3b4aSPaul Mackerras 			    || res->start > res->end)
241e05b3b4aSPaul Mackerras 				continue;
242e05b3b4aSPaul Mackerras 			if (bus->parent == NULL)
243e05b3b4aSPaul Mackerras 				pr = (res->flags & IORESOURCE_IO)?
244e05b3b4aSPaul Mackerras 					&ioport_resource: &iomem_resource;
245e05b3b4aSPaul Mackerras 			else {
246e05b3b4aSPaul Mackerras 				pr = pci_find_parent_resource(bus->self, res);
247e05b3b4aSPaul Mackerras 				if (pr == res) {
248e05b3b4aSPaul Mackerras 					/* this happens when the generic PCI
249e05b3b4aSPaul Mackerras 					 * code (wrongly) decides that this
250e05b3b4aSPaul Mackerras 					 * bridge is transparent  -- paulus
251e05b3b4aSPaul Mackerras 					 */
252e05b3b4aSPaul Mackerras 					continue;
253e05b3b4aSPaul Mackerras 				}
254e05b3b4aSPaul Mackerras 			}
255e05b3b4aSPaul Mackerras 
256685143acSGreg Kroah-Hartman 			DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
257872455e2SSergei Shtylyov 			    (u64)res->start, (u64)res->end, res->flags, pr);
258e05b3b4aSPaul Mackerras 			if (pr) {
259e05b3b4aSPaul Mackerras 				if (request_resource(pr, res) == 0)
260e05b3b4aSPaul Mackerras 					continue;
261e05b3b4aSPaul Mackerras 				/*
262e05b3b4aSPaul Mackerras 				 * Must be a conflict with an existing entry.
263e05b3b4aSPaul Mackerras 				 * Move that entry (or entries) under the
264e05b3b4aSPaul Mackerras 				 * bridge resource and try again.
265e05b3b4aSPaul Mackerras 				 */
266e05b3b4aSPaul Mackerras 				if (reparent_resources(pr, res) == 0)
267e05b3b4aSPaul Mackerras 					continue;
268e05b3b4aSPaul Mackerras 			}
269e05b3b4aSPaul Mackerras 			printk(KERN_ERR "PCI: Cannot allocate resource region "
270e05b3b4aSPaul Mackerras 			       "%d of PCI bridge %d\n", i, bus->number);
271e05b3b4aSPaul Mackerras 			if (pci_relocate_bridge_resource(bus, i))
272e05b3b4aSPaul Mackerras 				bus->resource[i] = NULL;
273e05b3b4aSPaul Mackerras 		}
274e05b3b4aSPaul Mackerras 		pcibios_allocate_bus_resources(&bus->children);
275e05b3b4aSPaul Mackerras 	}
276e05b3b4aSPaul Mackerras }
277e05b3b4aSPaul Mackerras 
278e05b3b4aSPaul Mackerras /*
279e05b3b4aSPaul Mackerras  * Reparent resource children of pr that conflict with res
280e05b3b4aSPaul Mackerras  * under res, and make res replace those children.
281e05b3b4aSPaul Mackerras  */
282e05b3b4aSPaul Mackerras static int __init
283e05b3b4aSPaul Mackerras reparent_resources(struct resource *parent, struct resource *res)
284e05b3b4aSPaul Mackerras {
285e05b3b4aSPaul Mackerras 	struct resource *p, **pp;
286e05b3b4aSPaul Mackerras 	struct resource **firstpp = NULL;
287e05b3b4aSPaul Mackerras 
288e05b3b4aSPaul Mackerras 	for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
289e05b3b4aSPaul Mackerras 		if (p->end < res->start)
290e05b3b4aSPaul Mackerras 			continue;
291e05b3b4aSPaul Mackerras 		if (res->end < p->start)
292e05b3b4aSPaul Mackerras 			break;
293e05b3b4aSPaul Mackerras 		if (p->start < res->start || p->end > res->end)
294e05b3b4aSPaul Mackerras 			return -1;	/* not completely contained */
295e05b3b4aSPaul Mackerras 		if (firstpp == NULL)
296e05b3b4aSPaul Mackerras 			firstpp = pp;
297e05b3b4aSPaul Mackerras 	}
298e05b3b4aSPaul Mackerras 	if (firstpp == NULL)
299e05b3b4aSPaul Mackerras 		return -1;	/* didn't find any conflicting entries? */
300e05b3b4aSPaul Mackerras 	res->parent = parent;
301e05b3b4aSPaul Mackerras 	res->child = *firstpp;
302e05b3b4aSPaul Mackerras 	res->sibling = *pp;
303e05b3b4aSPaul Mackerras 	*firstpp = res;
304e05b3b4aSPaul Mackerras 	*pp = NULL;
305e05b3b4aSPaul Mackerras 	for (p = res->child; p != NULL; p = p->sibling) {
306e05b3b4aSPaul Mackerras 		p->parent = res;
307685143acSGreg Kroah-Hartman 		DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
308872455e2SSergei Shtylyov 		    p->name, (u64)p->start, (u64)p->end, res->name);
309e05b3b4aSPaul Mackerras 	}
310e05b3b4aSPaul Mackerras 	return 0;
311e05b3b4aSPaul Mackerras }
312e05b3b4aSPaul Mackerras 
313e05b3b4aSPaul Mackerras /*
314e05b3b4aSPaul Mackerras  * A bridge has been allocated a range which is outside the range
315e05b3b4aSPaul Mackerras  * of its parent bridge, so it needs to be moved.
316e05b3b4aSPaul Mackerras  */
317e05b3b4aSPaul Mackerras static int __init
318e05b3b4aSPaul Mackerras pci_relocate_bridge_resource(struct pci_bus *bus, int i)
319e05b3b4aSPaul Mackerras {
320e05b3b4aSPaul Mackerras 	struct resource *res, *pr, *conflict;
321e05b3b4aSPaul Mackerras 	unsigned long try, size;
322e05b3b4aSPaul Mackerras 	int j;
323e05b3b4aSPaul Mackerras 	struct pci_bus *parent = bus->parent;
324e05b3b4aSPaul Mackerras 
325e05b3b4aSPaul Mackerras 	if (parent == NULL) {
326e05b3b4aSPaul Mackerras 		/* shouldn't ever happen */
327e05b3b4aSPaul Mackerras 		printk(KERN_ERR "PCI: can't move host bridge resource\n");
328e05b3b4aSPaul Mackerras 		return -1;
329e05b3b4aSPaul Mackerras 	}
330e05b3b4aSPaul Mackerras 	res = bus->resource[i];
331e05b3b4aSPaul Mackerras 	if (res == NULL)
332e05b3b4aSPaul Mackerras 		return -1;
333e05b3b4aSPaul Mackerras 	pr = NULL;
334e05b3b4aSPaul Mackerras 	for (j = 0; j < 4; j++) {
335e05b3b4aSPaul Mackerras 		struct resource *r = parent->resource[j];
336e05b3b4aSPaul Mackerras 		if (!r)
337e05b3b4aSPaul Mackerras 			continue;
338e05b3b4aSPaul Mackerras 		if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
339e05b3b4aSPaul Mackerras 			continue;
340e05b3b4aSPaul Mackerras 		if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
341e05b3b4aSPaul Mackerras 			pr = r;
342e05b3b4aSPaul Mackerras 			break;
343e05b3b4aSPaul Mackerras 		}
344e05b3b4aSPaul Mackerras 		if (res->flags & IORESOURCE_PREFETCH)
345e05b3b4aSPaul Mackerras 			pr = r;
346e05b3b4aSPaul Mackerras 	}
347e05b3b4aSPaul Mackerras 	if (pr == NULL)
348e05b3b4aSPaul Mackerras 		return -1;
349e05b3b4aSPaul Mackerras 	size = res->end - res->start;
350e05b3b4aSPaul Mackerras 	if (pr->start > pr->end || size > pr->end - pr->start)
351e05b3b4aSPaul Mackerras 		return -1;
352e05b3b4aSPaul Mackerras 	try = pr->end;
353e05b3b4aSPaul Mackerras 	for (;;) {
354e05b3b4aSPaul Mackerras 		res->start = try - size;
355e05b3b4aSPaul Mackerras 		res->end = try;
356e05b3b4aSPaul Mackerras 		if (probe_resource(bus->parent, pr, res, &conflict) == 0)
357e05b3b4aSPaul Mackerras 			break;
358e05b3b4aSPaul Mackerras 		if (conflict->start <= pr->start + size)
359e05b3b4aSPaul Mackerras 			return -1;
360e05b3b4aSPaul Mackerras 		try = conflict->start - 1;
361e05b3b4aSPaul Mackerras 	}
362e05b3b4aSPaul Mackerras 	if (request_resource(pr, res)) {
363685143acSGreg Kroah-Hartman 		DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n",
364872455e2SSergei Shtylyov 		    (u64)res->start, (u64)res->end);
365e05b3b4aSPaul Mackerras 		return -1;		/* "can't happen" */
366e05b3b4aSPaul Mackerras 	}
367e05b3b4aSPaul Mackerras 	update_bridge_base(bus, i);
368685143acSGreg Kroah-Hartman 	printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n",
369685143acSGreg Kroah-Hartman 	       bus->number, i, (unsigned long long)res->start,
370685143acSGreg Kroah-Hartman 	       (unsigned long long)res->end);
371e05b3b4aSPaul Mackerras 	return 0;
372e05b3b4aSPaul Mackerras }
373e05b3b4aSPaul Mackerras 
374e05b3b4aSPaul Mackerras static int __init
375e05b3b4aSPaul Mackerras probe_resource(struct pci_bus *parent, struct resource *pr,
376e05b3b4aSPaul Mackerras 	       struct resource *res, struct resource **conflict)
377e05b3b4aSPaul Mackerras {
378e05b3b4aSPaul Mackerras 	struct pci_bus *bus;
379e05b3b4aSPaul Mackerras 	struct pci_dev *dev;
380e05b3b4aSPaul Mackerras 	struct resource *r;
381e05b3b4aSPaul Mackerras 	int i;
382e05b3b4aSPaul Mackerras 
383e05b3b4aSPaul Mackerras 	for (r = pr->child; r != NULL; r = r->sibling) {
384e05b3b4aSPaul Mackerras 		if (r->end >= res->start && res->end >= r->start) {
385e05b3b4aSPaul Mackerras 			*conflict = r;
386e05b3b4aSPaul Mackerras 			return 1;
387e05b3b4aSPaul Mackerras 		}
388e05b3b4aSPaul Mackerras 	}
389e05b3b4aSPaul Mackerras 	list_for_each_entry(bus, &parent->children, node) {
390e05b3b4aSPaul Mackerras 		for (i = 0; i < 4; ++i) {
391e05b3b4aSPaul Mackerras 			if ((r = bus->resource[i]) == NULL)
392e05b3b4aSPaul Mackerras 				continue;
393e05b3b4aSPaul Mackerras 			if (!r->flags || r->start > r->end || r == res)
394e05b3b4aSPaul Mackerras 				continue;
395e05b3b4aSPaul Mackerras 			if (pci_find_parent_resource(bus->self, r) != pr)
396e05b3b4aSPaul Mackerras 				continue;
397e05b3b4aSPaul Mackerras 			if (r->end >= res->start && res->end >= r->start) {
398e05b3b4aSPaul Mackerras 				*conflict = r;
399e05b3b4aSPaul Mackerras 				return 1;
400e05b3b4aSPaul Mackerras 			}
401e05b3b4aSPaul Mackerras 		}
402e05b3b4aSPaul Mackerras 	}
403e05b3b4aSPaul Mackerras 	list_for_each_entry(dev, &parent->devices, bus_list) {
404e05b3b4aSPaul Mackerras 		for (i = 0; i < 6; ++i) {
405e05b3b4aSPaul Mackerras 			r = &dev->resource[i];
406e05b3b4aSPaul Mackerras 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
407e05b3b4aSPaul Mackerras 				continue;
408e05b3b4aSPaul Mackerras 			if (pci_find_parent_resource(dev, r) != pr)
409e05b3b4aSPaul Mackerras 				continue;
410e05b3b4aSPaul Mackerras 			if (r->end >= res->start && res->end >= r->start) {
411e05b3b4aSPaul Mackerras 				*conflict = r;
412e05b3b4aSPaul Mackerras 				return 1;
413e05b3b4aSPaul Mackerras 			}
414e05b3b4aSPaul Mackerras 		}
415e05b3b4aSPaul Mackerras 	}
416e05b3b4aSPaul Mackerras 	return 0;
417e05b3b4aSPaul Mackerras }
418e05b3b4aSPaul Mackerras 
419e05b3b4aSPaul Mackerras static void __init
420e05b3b4aSPaul Mackerras update_bridge_base(struct pci_bus *bus, int i)
421e05b3b4aSPaul Mackerras {
422e05b3b4aSPaul Mackerras 	struct resource *res = bus->resource[i];
423e05b3b4aSPaul Mackerras 	u8 io_base_lo, io_limit_lo;
424e05b3b4aSPaul Mackerras 	u16 mem_base, mem_limit;
425e05b3b4aSPaul Mackerras 	u16 cmd;
426e05b3b4aSPaul Mackerras 	unsigned long start, end, off;
427e05b3b4aSPaul Mackerras 	struct pci_dev *dev = bus->self;
428e05b3b4aSPaul Mackerras 	struct pci_controller *hose = dev->sysdata;
429e05b3b4aSPaul Mackerras 
430e05b3b4aSPaul Mackerras 	if (!hose) {
431e05b3b4aSPaul Mackerras 		printk("update_bridge_base: no hose?\n");
432e05b3b4aSPaul Mackerras 		return;
433e05b3b4aSPaul Mackerras 	}
434e05b3b4aSPaul Mackerras 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
435e05b3b4aSPaul Mackerras 	pci_write_config_word(dev, PCI_COMMAND,
436e05b3b4aSPaul Mackerras 			      cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
437e05b3b4aSPaul Mackerras 	if (res->flags & IORESOURCE_IO) {
438e05b3b4aSPaul Mackerras 		off = (unsigned long) hose->io_base_virt - isa_io_base;
439e05b3b4aSPaul Mackerras 		start = res->start - off;
440e05b3b4aSPaul Mackerras 		end = res->end - off;
441e05b3b4aSPaul Mackerras 		io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
442e05b3b4aSPaul Mackerras 		io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
44360b2a46cSRandy Vinson 		if (end > 0xffff)
44460b2a46cSRandy Vinson 			io_base_lo |= PCI_IO_RANGE_TYPE_32;
44560b2a46cSRandy Vinson 		else
44660b2a46cSRandy Vinson 			io_base_lo |= PCI_IO_RANGE_TYPE_16;
447e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
448e05b3b4aSPaul Mackerras 				start >> 16);
449e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
450e05b3b4aSPaul Mackerras 				end >> 16);
451e05b3b4aSPaul Mackerras 		pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
452e05b3b4aSPaul Mackerras 		pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
453e05b3b4aSPaul Mackerras 
454e05b3b4aSPaul Mackerras 	} else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
455e05b3b4aSPaul Mackerras 		   == IORESOURCE_MEM) {
456e05b3b4aSPaul Mackerras 		off = hose->pci_mem_offset;
457e05b3b4aSPaul Mackerras 		mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
458e05b3b4aSPaul Mackerras 		mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
459e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
460e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
461e05b3b4aSPaul Mackerras 
462e05b3b4aSPaul Mackerras 	} else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
463e05b3b4aSPaul Mackerras 		   == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
464e05b3b4aSPaul Mackerras 		off = hose->pci_mem_offset;
465e05b3b4aSPaul Mackerras 		mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
466e05b3b4aSPaul Mackerras 		mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
467e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
468e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
469e05b3b4aSPaul Mackerras 
470e05b3b4aSPaul Mackerras 	} else {
471e05b3b4aSPaul Mackerras 		DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
472e05b3b4aSPaul Mackerras 		    pci_name(dev), i, res->flags);
473e05b3b4aSPaul Mackerras 	}
474e05b3b4aSPaul Mackerras 	pci_write_config_word(dev, PCI_COMMAND, cmd);
475e05b3b4aSPaul Mackerras }
476e05b3b4aSPaul Mackerras 
477e05b3b4aSPaul Mackerras static inline void alloc_resource(struct pci_dev *dev, int idx)
478e05b3b4aSPaul Mackerras {
479e05b3b4aSPaul Mackerras 	struct resource *pr, *r = &dev->resource[idx];
480e05b3b4aSPaul Mackerras 
481685143acSGreg Kroah-Hartman 	DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
482872455e2SSergei Shtylyov 	    pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
483e05b3b4aSPaul Mackerras 	pr = pci_find_parent_resource(dev, r);
484e05b3b4aSPaul Mackerras 	if (!pr || request_resource(pr, r) < 0) {
485e05b3b4aSPaul Mackerras 		printk(KERN_ERR "PCI: Cannot allocate resource region %d"
486e05b3b4aSPaul Mackerras 		       " of device %s\n", idx, pci_name(dev));
487e05b3b4aSPaul Mackerras 		if (pr)
488685143acSGreg Kroah-Hartman 			DBG("PCI:  parent is %p: %016llx-%016llx (f=%lx)\n",
489872455e2SSergei Shtylyov 			    pr, (u64)pr->start, (u64)pr->end, pr->flags);
490e05b3b4aSPaul Mackerras 		/* We'll assign a new address later */
491e05b3b4aSPaul Mackerras 		r->flags |= IORESOURCE_UNSET;
492e05b3b4aSPaul Mackerras 		r->end -= r->start;
493e05b3b4aSPaul Mackerras 		r->start = 0;
494e05b3b4aSPaul Mackerras 	}
495e05b3b4aSPaul Mackerras }
496e05b3b4aSPaul Mackerras 
497e05b3b4aSPaul Mackerras static void __init
498e05b3b4aSPaul Mackerras pcibios_allocate_resources(int pass)
499e05b3b4aSPaul Mackerras {
500e05b3b4aSPaul Mackerras 	struct pci_dev *dev = NULL;
501e05b3b4aSPaul Mackerras 	int idx, disabled;
502e05b3b4aSPaul Mackerras 	u16 command;
503e05b3b4aSPaul Mackerras 	struct resource *r;
504e05b3b4aSPaul Mackerras 
505e05b3b4aSPaul Mackerras 	for_each_pci_dev(dev) {
506e05b3b4aSPaul Mackerras 		pci_read_config_word(dev, PCI_COMMAND, &command);
507e05b3b4aSPaul Mackerras 		for (idx = 0; idx < 6; idx++) {
508e05b3b4aSPaul Mackerras 			r = &dev->resource[idx];
509e05b3b4aSPaul Mackerras 			if (r->parent)		/* Already allocated */
510e05b3b4aSPaul Mackerras 				continue;
511e05b3b4aSPaul Mackerras 			if (!r->flags || (r->flags & IORESOURCE_UNSET))
512e05b3b4aSPaul Mackerras 				continue;	/* Not assigned at all */
513e05b3b4aSPaul Mackerras 			if (r->flags & IORESOURCE_IO)
514e05b3b4aSPaul Mackerras 				disabled = !(command & PCI_COMMAND_IO);
515e05b3b4aSPaul Mackerras 			else
516e05b3b4aSPaul Mackerras 				disabled = !(command & PCI_COMMAND_MEMORY);
517e05b3b4aSPaul Mackerras 			if (pass == disabled)
518e05b3b4aSPaul Mackerras 				alloc_resource(dev, idx);
519e05b3b4aSPaul Mackerras 		}
520e05b3b4aSPaul Mackerras 		if (pass)
521e05b3b4aSPaul Mackerras 			continue;
522e05b3b4aSPaul Mackerras 		r = &dev->resource[PCI_ROM_RESOURCE];
523e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_ROM_ENABLE) {
524e05b3b4aSPaul Mackerras 			/* Turn the ROM off, leave the resource region, but keep it unregistered. */
525e05b3b4aSPaul Mackerras 			u32 reg;
526e05b3b4aSPaul Mackerras 			DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
527e05b3b4aSPaul Mackerras 			r->flags &= ~IORESOURCE_ROM_ENABLE;
528e05b3b4aSPaul Mackerras 			pci_read_config_dword(dev, dev->rom_base_reg, &reg);
529e05b3b4aSPaul Mackerras 			pci_write_config_dword(dev, dev->rom_base_reg,
530e05b3b4aSPaul Mackerras 					       reg & ~PCI_ROM_ADDRESS_ENABLE);
531e05b3b4aSPaul Mackerras 		}
532e05b3b4aSPaul Mackerras 	}
533e05b3b4aSPaul Mackerras }
534e05b3b4aSPaul Mackerras 
535e05b3b4aSPaul Mackerras static void __init
536e05b3b4aSPaul Mackerras pcibios_assign_resources(void)
537e05b3b4aSPaul Mackerras {
538e05b3b4aSPaul Mackerras 	struct pci_dev *dev = NULL;
539e05b3b4aSPaul Mackerras 	int idx;
540e05b3b4aSPaul Mackerras 	struct resource *r;
541e05b3b4aSPaul Mackerras 
542e05b3b4aSPaul Mackerras 	for_each_pci_dev(dev) {
543e05b3b4aSPaul Mackerras 		int class = dev->class >> 8;
544e05b3b4aSPaul Mackerras 
545e05b3b4aSPaul Mackerras 		/* Don't touch classless devices and host bridges */
546e05b3b4aSPaul Mackerras 		if (!class || class == PCI_CLASS_BRIDGE_HOST)
547e05b3b4aSPaul Mackerras 			continue;
548e05b3b4aSPaul Mackerras 
549e05b3b4aSPaul Mackerras 		for (idx = 0; idx < 6; idx++) {
550e05b3b4aSPaul Mackerras 			r = &dev->resource[idx];
551e05b3b4aSPaul Mackerras 
552e05b3b4aSPaul Mackerras 			/*
553e05b3b4aSPaul Mackerras 			 * We shall assign a new address to this resource,
554e05b3b4aSPaul Mackerras 			 * either because the BIOS (sic) forgot to do so
555e05b3b4aSPaul Mackerras 			 * or because we have decided the old address was
556e05b3b4aSPaul Mackerras 			 * unusable for some reason.
557e05b3b4aSPaul Mackerras 			 */
558e05b3b4aSPaul Mackerras 			if ((r->flags & IORESOURCE_UNSET) && r->end &&
559e05b3b4aSPaul Mackerras 			    (!ppc_md.pcibios_enable_device_hook ||
560e05b3b4aSPaul Mackerras 			     !ppc_md.pcibios_enable_device_hook(dev, 1))) {
561e05b3b4aSPaul Mackerras 				r->flags &= ~IORESOURCE_UNSET;
562e05b3b4aSPaul Mackerras 				pci_assign_resource(dev, idx);
563e05b3b4aSPaul Mackerras 			}
564e05b3b4aSPaul Mackerras 		}
565e05b3b4aSPaul Mackerras 
566e05b3b4aSPaul Mackerras #if 0 /* don't assign ROMs */
567e05b3b4aSPaul Mackerras 		r = &dev->resource[PCI_ROM_RESOURCE];
568e05b3b4aSPaul Mackerras 		r->end -= r->start;
569e05b3b4aSPaul Mackerras 		r->start = 0;
570e05b3b4aSPaul Mackerras 		if (r->end)
571e05b3b4aSPaul Mackerras 			pci_assign_resource(dev, PCI_ROM_RESOURCE);
572e05b3b4aSPaul Mackerras #endif
573e05b3b4aSPaul Mackerras 	}
574e05b3b4aSPaul Mackerras }
575e05b3b4aSPaul Mackerras 
576e05b3b4aSPaul Mackerras 
577e05b3b4aSPaul Mackerras int
578e05b3b4aSPaul Mackerras pcibios_enable_resources(struct pci_dev *dev, int mask)
579e05b3b4aSPaul Mackerras {
580e05b3b4aSPaul Mackerras 	u16 cmd, old_cmd;
581e05b3b4aSPaul Mackerras 	int idx;
582e05b3b4aSPaul Mackerras 	struct resource *r;
583e05b3b4aSPaul Mackerras 
584e05b3b4aSPaul Mackerras 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
585e05b3b4aSPaul Mackerras 	old_cmd = cmd;
586e05b3b4aSPaul Mackerras 	for (idx=0; idx<6; idx++) {
587e05b3b4aSPaul Mackerras 		/* Only set up the requested stuff */
588e05b3b4aSPaul Mackerras 		if (!(mask & (1<<idx)))
589e05b3b4aSPaul Mackerras 			continue;
590e05b3b4aSPaul Mackerras 
591e05b3b4aSPaul Mackerras 		r = &dev->resource[idx];
592e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_UNSET) {
593e05b3b4aSPaul Mackerras 			printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
594e05b3b4aSPaul Mackerras 			return -EINVAL;
595e05b3b4aSPaul Mackerras 		}
596e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_IO)
597e05b3b4aSPaul Mackerras 			cmd |= PCI_COMMAND_IO;
598e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_MEM)
599e05b3b4aSPaul Mackerras 			cmd |= PCI_COMMAND_MEMORY;
600e05b3b4aSPaul Mackerras 	}
601e05b3b4aSPaul Mackerras 	if (dev->resource[PCI_ROM_RESOURCE].start)
602e05b3b4aSPaul Mackerras 		cmd |= PCI_COMMAND_MEMORY;
603e05b3b4aSPaul Mackerras 	if (cmd != old_cmd) {
604e05b3b4aSPaul Mackerras 		printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
605e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_COMMAND, cmd);
606e05b3b4aSPaul Mackerras 	}
607e05b3b4aSPaul Mackerras 	return 0;
608e05b3b4aSPaul Mackerras }
609e05b3b4aSPaul Mackerras 
610e05b3b4aSPaul Mackerras static int next_controller_index;
611e05b3b4aSPaul Mackerras 
612e05b3b4aSPaul Mackerras struct pci_controller * __init
613e05b3b4aSPaul Mackerras pcibios_alloc_controller(void)
614e05b3b4aSPaul Mackerras {
615e05b3b4aSPaul Mackerras 	struct pci_controller *hose;
616e05b3b4aSPaul Mackerras 
617e05b3b4aSPaul Mackerras 	hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
618e05b3b4aSPaul Mackerras 	memset(hose, 0, sizeof(struct pci_controller));
619e05b3b4aSPaul Mackerras 
620e05b3b4aSPaul Mackerras 	*hose_tail = hose;
621e05b3b4aSPaul Mackerras 	hose_tail = &hose->next;
622e05b3b4aSPaul Mackerras 
623e05b3b4aSPaul Mackerras 	hose->index = next_controller_index++;
624e05b3b4aSPaul Mackerras 
625e05b3b4aSPaul Mackerras 	return hose;
626e05b3b4aSPaul Mackerras }
627e05b3b4aSPaul Mackerras 
628e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF
629e05b3b4aSPaul Mackerras /*
630e05b3b4aSPaul Mackerras  * Functions below are used on OpenFirmware machines.
631e05b3b4aSPaul Mackerras  */
632e05b3b4aSPaul Mackerras static void
633e05b3b4aSPaul Mackerras make_one_node_map(struct device_node* node, u8 pci_bus)
634e05b3b4aSPaul Mackerras {
635a7f67bdfSJeremy Kerr 	const int *bus_range;
636e05b3b4aSPaul Mackerras 	int len;
637e05b3b4aSPaul Mackerras 
638e05b3b4aSPaul Mackerras 	if (pci_bus >= pci_bus_count)
639e05b3b4aSPaul Mackerras 		return;
640e2eb6392SStephen Rothwell 	bus_range = of_get_property(node, "bus-range", &len);
641e05b3b4aSPaul Mackerras 	if (bus_range == NULL || len < 2 * sizeof(int)) {
642e05b3b4aSPaul Mackerras 		printk(KERN_WARNING "Can't get bus-range for %s, "
643e05b3b4aSPaul Mackerras 		       "assuming it starts at 0\n", node->full_name);
644e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[pci_bus] = 0;
645e05b3b4aSPaul Mackerras 	} else
646e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[pci_bus] = bus_range[0];
647e05b3b4aSPaul Mackerras 
648e05b3b4aSPaul Mackerras 	for (node=node->child; node != 0;node = node->sibling) {
649e05b3b4aSPaul Mackerras 		struct pci_dev* dev;
650a7f67bdfSJeremy Kerr 		const unsigned int *class_code, *reg;
651e05b3b4aSPaul Mackerras 
652e2eb6392SStephen Rothwell 		class_code = of_get_property(node, "class-code", NULL);
653e05b3b4aSPaul Mackerras 		if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
654e05b3b4aSPaul Mackerras 			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
655e05b3b4aSPaul Mackerras 			continue;
656e2eb6392SStephen Rothwell 		reg = of_get_property(node, "reg", NULL);
657e05b3b4aSPaul Mackerras 		if (!reg)
658e05b3b4aSPaul Mackerras 			continue;
659e05b3b4aSPaul Mackerras 		dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
660e05b3b4aSPaul Mackerras 		if (!dev || !dev->subordinate)
661e05b3b4aSPaul Mackerras 			continue;
662e05b3b4aSPaul Mackerras 		make_one_node_map(node, dev->subordinate->number);
663e05b3b4aSPaul Mackerras 	}
664e05b3b4aSPaul Mackerras }
665e05b3b4aSPaul Mackerras 
666e05b3b4aSPaul Mackerras void
667e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(void)
668e05b3b4aSPaul Mackerras {
669e05b3b4aSPaul Mackerras 	int i;
670e05b3b4aSPaul Mackerras 	struct pci_controller* hose;
671a7f67bdfSJeremy Kerr 	struct property *map_prop;
6728c8dc322SStephen Rothwell 	struct device_node *dn;
673e05b3b4aSPaul Mackerras 
6745cbded58SRobert P. J. Day 	pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
675e05b3b4aSPaul Mackerras 	if (!pci_to_OF_bus_map) {
676e05b3b4aSPaul Mackerras 		printk(KERN_ERR "Can't allocate OF bus map !\n");
677e05b3b4aSPaul Mackerras 		return;
678e05b3b4aSPaul Mackerras 	}
679e05b3b4aSPaul Mackerras 
680e05b3b4aSPaul Mackerras 	/* We fill the bus map with invalid values, that helps
681e05b3b4aSPaul Mackerras 	 * debugging.
682e05b3b4aSPaul Mackerras 	 */
683e05b3b4aSPaul Mackerras 	for (i=0; i<pci_bus_count; i++)
684e05b3b4aSPaul Mackerras 		pci_to_OF_bus_map[i] = 0xff;
685e05b3b4aSPaul Mackerras 
686e05b3b4aSPaul Mackerras 	/* For each hose, we begin searching bridges */
687e05b3b4aSPaul Mackerras 	for(hose=hose_head; hose; hose=hose->next) {
688e05b3b4aSPaul Mackerras 		struct device_node* node;
689e05b3b4aSPaul Mackerras 		node = (struct device_node *)hose->arch_data;
690e05b3b4aSPaul Mackerras 		if (!node)
691e05b3b4aSPaul Mackerras 			continue;
692e05b3b4aSPaul Mackerras 		make_one_node_map(node, hose->first_busno);
693e05b3b4aSPaul Mackerras 	}
6948c8dc322SStephen Rothwell 	dn = of_find_node_by_path("/");
6958c8dc322SStephen Rothwell 	map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
696a7f67bdfSJeremy Kerr 	if (map_prop) {
697a7f67bdfSJeremy Kerr 		BUG_ON(pci_bus_count > map_prop->length);
698a7f67bdfSJeremy Kerr 		memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
699a7f67bdfSJeremy Kerr 	}
7008c8dc322SStephen Rothwell 	of_node_put(dn);
701e05b3b4aSPaul Mackerras #ifdef DEBUG
702e05b3b4aSPaul Mackerras 	printk("PCI->OF bus map:\n");
703e05b3b4aSPaul Mackerras 	for (i=0; i<pci_bus_count; i++) {
704e05b3b4aSPaul Mackerras 		if (pci_to_OF_bus_map[i] == 0xff)
705e05b3b4aSPaul Mackerras 			continue;
706e05b3b4aSPaul Mackerras 		printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
707e05b3b4aSPaul Mackerras 	}
708e05b3b4aSPaul Mackerras #endif
709e05b3b4aSPaul Mackerras }
710e05b3b4aSPaul Mackerras 
711e05b3b4aSPaul Mackerras typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
712e05b3b4aSPaul Mackerras 
713e05b3b4aSPaul Mackerras static struct device_node*
714e05b3b4aSPaul Mackerras scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
715e05b3b4aSPaul Mackerras {
716e05b3b4aSPaul Mackerras 	struct device_node* sub_node;
717e05b3b4aSPaul Mackerras 
718e05b3b4aSPaul Mackerras 	for (; node != 0;node = node->sibling) {
719a7f67bdfSJeremy Kerr 		const unsigned int *class_code;
720e05b3b4aSPaul Mackerras 
721e05b3b4aSPaul Mackerras 		if (filter(node, data))
722e05b3b4aSPaul Mackerras 			return node;
723e05b3b4aSPaul Mackerras 
724e05b3b4aSPaul Mackerras 		/* For PCI<->PCI bridges or CardBus bridges, we go down
725e05b3b4aSPaul Mackerras 		 * Note: some OFs create a parent node "multifunc-device" as
726e05b3b4aSPaul Mackerras 		 * a fake root for all functions of a multi-function device,
727e05b3b4aSPaul Mackerras 		 * we go down them as well.
728e05b3b4aSPaul Mackerras 		 */
729e2eb6392SStephen Rothwell 		class_code = of_get_property(node, "class-code", NULL);
730e05b3b4aSPaul Mackerras 		if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
731e05b3b4aSPaul Mackerras 			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
732e05b3b4aSPaul Mackerras 			strcmp(node->name, "multifunc-device"))
733e05b3b4aSPaul Mackerras 			continue;
734e05b3b4aSPaul Mackerras 		sub_node = scan_OF_pci_childs(node->child, filter, data);
735e05b3b4aSPaul Mackerras 		if (sub_node)
736e05b3b4aSPaul Mackerras 			return sub_node;
737e05b3b4aSPaul Mackerras 	}
738e05b3b4aSPaul Mackerras 	return NULL;
739e05b3b4aSPaul Mackerras }
740e05b3b4aSPaul Mackerras 
741dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
742dae4828dSBenjamin Herrenschmidt 					       unsigned int devfn)
743e05b3b4aSPaul Mackerras {
744dae4828dSBenjamin Herrenschmidt 	struct device_node *np = NULL;
745dae4828dSBenjamin Herrenschmidt 	const u32 *reg;
746dae4828dSBenjamin Herrenschmidt 	unsigned int psize;
747e05b3b4aSPaul Mackerras 
748dae4828dSBenjamin Herrenschmidt 	while ((np = of_get_next_child(parent, np)) != NULL) {
749e2eb6392SStephen Rothwell 		reg = of_get_property(np, "reg", &psize);
750dae4828dSBenjamin Herrenschmidt 		if (reg == NULL || psize < 4)
751dae4828dSBenjamin Herrenschmidt 			continue;
752dae4828dSBenjamin Herrenschmidt 		if (((reg[0] >> 8) & 0xff) == devfn)
753dae4828dSBenjamin Herrenschmidt 			return np;
754dae4828dSBenjamin Herrenschmidt 	}
755dae4828dSBenjamin Herrenschmidt 	return NULL;
756e05b3b4aSPaul Mackerras }
757e05b3b4aSPaul Mackerras 
758e05b3b4aSPaul Mackerras 
759dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
760dae4828dSBenjamin Herrenschmidt {
761dae4828dSBenjamin Herrenschmidt 	struct device_node *parent, *np;
762dae4828dSBenjamin Herrenschmidt 
763dae4828dSBenjamin Herrenschmidt 	/* Are we a root bus ? */
764dae4828dSBenjamin Herrenschmidt 	if (bus->self == NULL || bus->parent == NULL) {
765dae4828dSBenjamin Herrenschmidt 		struct pci_controller *hose = pci_bus_to_hose(bus->number);
766dae4828dSBenjamin Herrenschmidt 		if (hose == NULL)
767dae4828dSBenjamin Herrenschmidt 			return NULL;
768dae4828dSBenjamin Herrenschmidt 		return of_node_get(hose->arch_data);
769dae4828dSBenjamin Herrenschmidt 	}
770dae4828dSBenjamin Herrenschmidt 
771dae4828dSBenjamin Herrenschmidt 	/* not a root bus, we need to get our parent */
772dae4828dSBenjamin Herrenschmidt 	parent = scan_OF_for_pci_bus(bus->parent);
773dae4828dSBenjamin Herrenschmidt 	if (parent == NULL)
774dae4828dSBenjamin Herrenschmidt 		return NULL;
775dae4828dSBenjamin Herrenschmidt 
776dae4828dSBenjamin Herrenschmidt 	/* now iterate for children for a match */
777dae4828dSBenjamin Herrenschmidt 	np = scan_OF_for_pci_dev(parent, bus->self->devfn);
778dae4828dSBenjamin Herrenschmidt 	of_node_put(parent);
779dae4828dSBenjamin Herrenschmidt 
780dae4828dSBenjamin Herrenschmidt 	return np;
781e05b3b4aSPaul Mackerras }
782e05b3b4aSPaul Mackerras 
783e05b3b4aSPaul Mackerras /*
784e05b3b4aSPaul Mackerras  * Scans the OF tree for a device node matching a PCI device
785e05b3b4aSPaul Mackerras  */
786e05b3b4aSPaul Mackerras struct device_node *
787e05b3b4aSPaul Mackerras pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
788e05b3b4aSPaul Mackerras {
789dae4828dSBenjamin Herrenschmidt 	struct device_node *parent, *np;
790e05b3b4aSPaul Mackerras 
791e05b3b4aSPaul Mackerras 	if (!have_of)
792e05b3b4aSPaul Mackerras 		return NULL;
793e05b3b4aSPaul Mackerras 
794dae4828dSBenjamin Herrenschmidt 	DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
795dae4828dSBenjamin Herrenschmidt 	parent = scan_OF_for_pci_bus(bus);
796dae4828dSBenjamin Herrenschmidt 	if (parent == NULL)
797e05b3b4aSPaul Mackerras 		return NULL;
798dae4828dSBenjamin Herrenschmidt 	DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
799dae4828dSBenjamin Herrenschmidt 	np = scan_OF_for_pci_dev(parent, devfn);
800dae4828dSBenjamin Herrenschmidt 	of_node_put(parent);
801dae4828dSBenjamin Herrenschmidt 	DBG(" result is %s\n", np ? np->full_name : "<NULL>");
802e05b3b4aSPaul Mackerras 
803dae4828dSBenjamin Herrenschmidt 	/* XXX most callers don't release the returned node
804dae4828dSBenjamin Herrenschmidt 	 * mostly because ppc64 doesn't increase the refcount,
805dae4828dSBenjamin Herrenschmidt 	 * we need to fix that.
806e05b3b4aSPaul Mackerras 	 */
807dae4828dSBenjamin Herrenschmidt 	return np;
808e05b3b4aSPaul Mackerras }
809e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_busdev_to_OF_node);
810e05b3b4aSPaul Mackerras 
811e05b3b4aSPaul Mackerras struct device_node*
812e05b3b4aSPaul Mackerras pci_device_to_OF_node(struct pci_dev *dev)
813e05b3b4aSPaul Mackerras {
814e05b3b4aSPaul Mackerras 	return pci_busdev_to_OF_node(dev->bus, dev->devfn);
815e05b3b4aSPaul Mackerras }
816e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_to_OF_node);
817e05b3b4aSPaul Mackerras 
818e05b3b4aSPaul Mackerras /* This routine is meant to be used early during boot, when the
819e05b3b4aSPaul Mackerras  * PCI bus numbers have not yet been assigned, and you need to
820e05b3b4aSPaul Mackerras  * issue PCI config cycles to an OF device.
821e05b3b4aSPaul Mackerras  * It could also be used to "fix" RTAS config cycles if you want
822e05b3b4aSPaul Mackerras  * to set pci_assign_all_buses to 1 and still use RTAS for PCI
823e05b3b4aSPaul Mackerras  * config cycles.
824e05b3b4aSPaul Mackerras  */
825e05b3b4aSPaul Mackerras struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
826e05b3b4aSPaul Mackerras {
827e05b3b4aSPaul Mackerras 	if (!have_of)
828e05b3b4aSPaul Mackerras 		return NULL;
829e05b3b4aSPaul Mackerras 	while(node) {
830e05b3b4aSPaul Mackerras 		struct pci_controller* hose;
831e05b3b4aSPaul Mackerras 		for (hose=hose_head;hose;hose=hose->next)
832e05b3b4aSPaul Mackerras 			if (hose->arch_data == node)
833e05b3b4aSPaul Mackerras 				return hose;
834e05b3b4aSPaul Mackerras 		node=node->parent;
835e05b3b4aSPaul Mackerras 	}
836e05b3b4aSPaul Mackerras 	return NULL;
837e05b3b4aSPaul Mackerras }
838e05b3b4aSPaul Mackerras 
839e05b3b4aSPaul Mackerras static int
840e05b3b4aSPaul Mackerras find_OF_pci_device_filter(struct device_node* node, void* data)
841e05b3b4aSPaul Mackerras {
842e05b3b4aSPaul Mackerras 	return ((void *)node == data);
843e05b3b4aSPaul Mackerras }
844e05b3b4aSPaul Mackerras 
845e05b3b4aSPaul Mackerras /*
846e05b3b4aSPaul Mackerras  * Returns the PCI device matching a given OF node
847e05b3b4aSPaul Mackerras  */
848e05b3b4aSPaul Mackerras int
849e05b3b4aSPaul Mackerras pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
850e05b3b4aSPaul Mackerras {
851a7f67bdfSJeremy Kerr 	const unsigned int *reg;
852e05b3b4aSPaul Mackerras 	struct pci_controller* hose;
853e05b3b4aSPaul Mackerras 	struct pci_dev* dev = NULL;
854e05b3b4aSPaul Mackerras 
855e05b3b4aSPaul Mackerras 	if (!have_of)
856e05b3b4aSPaul Mackerras 		return -ENODEV;
857e05b3b4aSPaul Mackerras 	/* Make sure it's really a PCI device */
858e05b3b4aSPaul Mackerras 	hose = pci_find_hose_for_OF_device(node);
859e05b3b4aSPaul Mackerras 	if (!hose || !hose->arch_data)
860e05b3b4aSPaul Mackerras 		return -ENODEV;
861e05b3b4aSPaul Mackerras 	if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
862e05b3b4aSPaul Mackerras 			find_OF_pci_device_filter, (void *)node))
863e05b3b4aSPaul Mackerras 		return -ENODEV;
864e2eb6392SStephen Rothwell 	reg = of_get_property(node, "reg", NULL);
865e05b3b4aSPaul Mackerras 	if (!reg)
866e05b3b4aSPaul Mackerras 		return -ENODEV;
867e05b3b4aSPaul Mackerras 	*bus = (reg[0] >> 16) & 0xff;
868e05b3b4aSPaul Mackerras 	*devfn = ((reg[0] >> 8) & 0xff);
869e05b3b4aSPaul Mackerras 
870e05b3b4aSPaul Mackerras 	/* Ok, here we need some tweak. If we have already renumbered
871e05b3b4aSPaul Mackerras 	 * all busses, we can't rely on the OF bus number any more.
872e05b3b4aSPaul Mackerras 	 * the pci_to_OF_bus_map is not enough as several PCI busses
873e05b3b4aSPaul Mackerras 	 * may match the same OF bus number.
874e05b3b4aSPaul Mackerras 	 */
875e05b3b4aSPaul Mackerras 	if (!pci_to_OF_bus_map)
876e05b3b4aSPaul Mackerras 		return 0;
877e05b3b4aSPaul Mackerras 
878e05b3b4aSPaul Mackerras 	for_each_pci_dev(dev)
879e05b3b4aSPaul Mackerras 		if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
880e05b3b4aSPaul Mackerras 				dev->devfn == *devfn) {
881e05b3b4aSPaul Mackerras 			*bus = dev->bus->number;
882e05b3b4aSPaul Mackerras 			pci_dev_put(dev);
883e05b3b4aSPaul Mackerras 			return 0;
884e05b3b4aSPaul Mackerras 		}
885e05b3b4aSPaul Mackerras 
886e05b3b4aSPaul Mackerras 	return -ENODEV;
887e05b3b4aSPaul Mackerras }
888e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node);
889e05b3b4aSPaul Mackerras 
890e05b3b4aSPaul Mackerras void __init
891e05b3b4aSPaul Mackerras pci_process_bridge_OF_ranges(struct pci_controller *hose,
892e05b3b4aSPaul Mackerras 			   struct device_node *dev, int primary)
893e05b3b4aSPaul Mackerras {
894e05b3b4aSPaul Mackerras 	static unsigned int static_lc_ranges[256] __initdata;
895a7f67bdfSJeremy Kerr 	const unsigned int *dt_ranges;
896a7f67bdfSJeremy Kerr 	unsigned int *lc_ranges, *ranges, *prev, size;
897e05b3b4aSPaul Mackerras 	int rlen = 0, orig_rlen;
898e05b3b4aSPaul Mackerras 	int memno = 0;
899e05b3b4aSPaul Mackerras 	struct resource *res;
900a8bda5ddSStephen Rothwell 	int np, na = of_n_addr_cells(dev);
901e05b3b4aSPaul Mackerras 	np = na + 5;
902e05b3b4aSPaul Mackerras 
903e05b3b4aSPaul Mackerras 	/* First we try to merge ranges to fix a problem with some pmacs
904e05b3b4aSPaul Mackerras 	 * that can have more than 3 ranges, fortunately using contiguous
905e05b3b4aSPaul Mackerras 	 * addresses -- BenH
906e05b3b4aSPaul Mackerras 	 */
907e2eb6392SStephen Rothwell 	dt_ranges = of_get_property(dev, "ranges", &rlen);
908e05b3b4aSPaul Mackerras 	if (!dt_ranges)
909e05b3b4aSPaul Mackerras 		return;
910e05b3b4aSPaul Mackerras 	/* Sanity check, though hopefully that never happens */
911e05b3b4aSPaul Mackerras 	if (rlen > sizeof(static_lc_ranges)) {
912e05b3b4aSPaul Mackerras 		printk(KERN_WARNING "OF ranges property too large !\n");
913e05b3b4aSPaul Mackerras 		rlen = sizeof(static_lc_ranges);
914e05b3b4aSPaul Mackerras 	}
915e05b3b4aSPaul Mackerras 	lc_ranges = static_lc_ranges;
916e05b3b4aSPaul Mackerras 	memcpy(lc_ranges, dt_ranges, rlen);
917e05b3b4aSPaul Mackerras 	orig_rlen = rlen;
918e05b3b4aSPaul Mackerras 
919e05b3b4aSPaul Mackerras 	/* Let's work on a copy of the "ranges" property instead of damaging
920e05b3b4aSPaul Mackerras 	 * the device-tree image in memory
921e05b3b4aSPaul Mackerras 	 */
922e05b3b4aSPaul Mackerras 	ranges = lc_ranges;
923e05b3b4aSPaul Mackerras 	prev = NULL;
924e05b3b4aSPaul Mackerras 	while ((rlen -= np * sizeof(unsigned int)) >= 0) {
925e05b3b4aSPaul Mackerras 		if (prev) {
926e05b3b4aSPaul Mackerras 			if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
927e05b3b4aSPaul Mackerras 				(prev[2] + prev[na+4]) == ranges[2] &&
928e05b3b4aSPaul Mackerras 				(prev[na+2] + prev[na+4]) == ranges[na+2]) {
929e05b3b4aSPaul Mackerras 				prev[na+4] += ranges[na+4];
930e05b3b4aSPaul Mackerras 				ranges[0] = 0;
931e05b3b4aSPaul Mackerras 				ranges += np;
932e05b3b4aSPaul Mackerras 				continue;
933e05b3b4aSPaul Mackerras 			}
934e05b3b4aSPaul Mackerras 		}
935e05b3b4aSPaul Mackerras 		prev = ranges;
936e05b3b4aSPaul Mackerras 		ranges += np;
937e05b3b4aSPaul Mackerras 	}
938e05b3b4aSPaul Mackerras 
939e05b3b4aSPaul Mackerras 	/*
940e05b3b4aSPaul Mackerras 	 * The ranges property is laid out as an array of elements,
941e05b3b4aSPaul Mackerras 	 * each of which comprises:
942e05b3b4aSPaul Mackerras 	 *   cells 0 - 2:	a PCI address
943e05b3b4aSPaul Mackerras 	 *   cells 3 or 3+4:	a CPU physical address
944e05b3b4aSPaul Mackerras 	 *			(size depending on dev->n_addr_cells)
945e05b3b4aSPaul Mackerras 	 *   cells 4+5 or 5+6:	the size of the range
946e05b3b4aSPaul Mackerras 	 */
947e05b3b4aSPaul Mackerras 	ranges = lc_ranges;
948e05b3b4aSPaul Mackerras 	rlen = orig_rlen;
949e05b3b4aSPaul Mackerras 	while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
950e05b3b4aSPaul Mackerras 		res = NULL;
951e05b3b4aSPaul Mackerras 		size = ranges[na+4];
952e05b3b4aSPaul Mackerras 		switch ((ranges[0] >> 24) & 0x3) {
953e05b3b4aSPaul Mackerras 		case 1:		/* I/O space */
954e05b3b4aSPaul Mackerras 			if (ranges[2] != 0)
955e05b3b4aSPaul Mackerras 				break;
956e05b3b4aSPaul Mackerras 			hose->io_base_phys = ranges[na+2];
957e05b3b4aSPaul Mackerras 			/* limit I/O space to 16MB */
958e05b3b4aSPaul Mackerras 			if (size > 0x01000000)
959e05b3b4aSPaul Mackerras 				size = 0x01000000;
960e05b3b4aSPaul Mackerras 			hose->io_base_virt = ioremap(ranges[na+2], size);
961e05b3b4aSPaul Mackerras 			if (primary)
962e05b3b4aSPaul Mackerras 				isa_io_base = (unsigned long) hose->io_base_virt;
963e05b3b4aSPaul Mackerras 			res = &hose->io_resource;
964e05b3b4aSPaul Mackerras 			res->flags = IORESOURCE_IO;
965e05b3b4aSPaul Mackerras 			res->start = ranges[2];
966685143acSGreg Kroah-Hartman 			DBG("PCI: IO 0x%llx -> 0x%llx\n",
967872455e2SSergei Shtylyov 			    (u64)res->start, (u64)res->start + size - 1);
968e05b3b4aSPaul Mackerras 			break;
969e05b3b4aSPaul Mackerras 		case 2:		/* memory space */
970e05b3b4aSPaul Mackerras 			memno = 0;
971e05b3b4aSPaul Mackerras 			if (ranges[1] == 0 && ranges[2] == 0
972e05b3b4aSPaul Mackerras 			    && ranges[na+4] <= (16 << 20)) {
973e05b3b4aSPaul Mackerras 				/* 1st 16MB, i.e. ISA memory area */
974e05b3b4aSPaul Mackerras 				if (primary)
975e05b3b4aSPaul Mackerras 					isa_mem_base = ranges[na+2];
976e05b3b4aSPaul Mackerras 				memno = 1;
977e05b3b4aSPaul Mackerras 			}
978e05b3b4aSPaul Mackerras 			while (memno < 3 && hose->mem_resources[memno].flags)
979e05b3b4aSPaul Mackerras 				++memno;
980e05b3b4aSPaul Mackerras 			if (memno == 0)
981e05b3b4aSPaul Mackerras 				hose->pci_mem_offset = ranges[na+2] - ranges[2];
982e05b3b4aSPaul Mackerras 			if (memno < 3) {
983e05b3b4aSPaul Mackerras 				res = &hose->mem_resources[memno];
984e05b3b4aSPaul Mackerras 				res->flags = IORESOURCE_MEM;
985e05b3b4aSPaul Mackerras 				if(ranges[0] & 0x40000000)
986e05b3b4aSPaul Mackerras 					res->flags |= IORESOURCE_PREFETCH;
987e05b3b4aSPaul Mackerras 				res->start = ranges[na+2];
988685143acSGreg Kroah-Hartman 				DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
989872455e2SSergei Shtylyov 				    (u64)res->start, (u64)res->start + size - 1);
990e05b3b4aSPaul Mackerras 			}
991e05b3b4aSPaul Mackerras 			break;
992e05b3b4aSPaul Mackerras 		}
993e05b3b4aSPaul Mackerras 		if (res != NULL) {
994e05b3b4aSPaul Mackerras 			res->name = dev->full_name;
995e05b3b4aSPaul Mackerras 			res->end = res->start + size - 1;
996e05b3b4aSPaul Mackerras 			res->parent = NULL;
997e05b3b4aSPaul Mackerras 			res->sibling = NULL;
998e05b3b4aSPaul Mackerras 			res->child = NULL;
999e05b3b4aSPaul Mackerras 		}
1000e05b3b4aSPaul Mackerras 		ranges += np;
1001e05b3b4aSPaul Mackerras 	}
1002e05b3b4aSPaul Mackerras }
1003e05b3b4aSPaul Mackerras 
1004e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the
1005e05b3b4aSPaul Mackerras  * /proc device tree
1006e05b3b4aSPaul Mackerras  */
1007e05b3b4aSPaul Mackerras void __init
1008e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void)
1009e05b3b4aSPaul Mackerras {
1010e05b3b4aSPaul Mackerras 	struct property* of_prop;
10118c8dc322SStephen Rothwell 	struct device_node *dn;
1012e05b3b4aSPaul Mackerras 
1013e05b3b4aSPaul Mackerras 	of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
10148c8dc322SStephen Rothwell 	if (!of_prop)
10158c8dc322SStephen Rothwell 		return;
10168c8dc322SStephen Rothwell 	dn = of_find_node_by_path("/");
10178c8dc322SStephen Rothwell 	if (dn) {
1018e05b3b4aSPaul Mackerras 		memset(of_prop, -1, sizeof(struct property) + 256);
1019e05b3b4aSPaul Mackerras 		of_prop->name = "pci-OF-bus-map";
1020e05b3b4aSPaul Mackerras 		of_prop->length = 256;
10211a38147eSStephen Rothwell 		of_prop->value = &of_prop[1];
10228c8dc322SStephen Rothwell 		prom_add_property(dn, of_prop);
10238c8dc322SStephen Rothwell 		of_node_put(dn);
1024e05b3b4aSPaul Mackerras 	}
1025e05b3b4aSPaul Mackerras }
1026e05b3b4aSPaul Mackerras 
1027e05b3b4aSPaul Mackerras static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
1028e05b3b4aSPaul Mackerras {
1029e05b3b4aSPaul Mackerras 	struct pci_dev *pdev;
1030e05b3b4aSPaul Mackerras 	struct device_node *np;
1031e05b3b4aSPaul Mackerras 
1032e05b3b4aSPaul Mackerras 	pdev = to_pci_dev (dev);
1033e05b3b4aSPaul Mackerras 	np = pci_device_to_OF_node(pdev);
1034e05b3b4aSPaul Mackerras 	if (np == NULL || np->full_name == NULL)
1035e05b3b4aSPaul Mackerras 		return 0;
1036e05b3b4aSPaul Mackerras 	return sprintf(buf, "%s", np->full_name);
1037e05b3b4aSPaul Mackerras }
1038e05b3b4aSPaul Mackerras static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
1039e05b3b4aSPaul Mackerras 
1040e05b3b4aSPaul Mackerras #else /* CONFIG_PPC_OF */
1041e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void)
1042e05b3b4aSPaul Mackerras {
1043e05b3b4aSPaul Mackerras }
1044e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */
1045e05b3b4aSPaul Mackerras 
1046e05b3b4aSPaul Mackerras /* Add sysfs properties */
1047e05b3b4aSPaul Mackerras void pcibios_add_platform_entries(struct pci_dev *pdev)
1048e05b3b4aSPaul Mackerras {
1049e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF
1050e05b3b4aSPaul Mackerras 	device_create_file(&pdev->dev, &dev_attr_devspec);
1051e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */
1052e05b3b4aSPaul Mackerras }
1053e05b3b4aSPaul Mackerras 
1054e05b3b4aSPaul Mackerras 
1055e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_PMAC
1056e05b3b4aSPaul Mackerras /*
1057e05b3b4aSPaul Mackerras  * This set of routines checks for PCI<->PCI bridges that have closed
1058e05b3b4aSPaul Mackerras  * IO resources and have child devices. It tries to re-open an IO
1059e05b3b4aSPaul Mackerras  * window on them.
1060e05b3b4aSPaul Mackerras  *
1061e05b3b4aSPaul Mackerras  * This is a _temporary_ fix to workaround a problem with Apple's OF
1062e05b3b4aSPaul Mackerras  * closing IO windows on P2P bridges when the OF drivers of cards
1063e05b3b4aSPaul Mackerras  * below this bridge don't claim any IO range (typically ATI or
1064e05b3b4aSPaul Mackerras  * Adaptec).
1065e05b3b4aSPaul Mackerras  *
1066e05b3b4aSPaul Mackerras  * A more complete fix would be to use drivers/pci/setup-bus.c, which
1067e05b3b4aSPaul Mackerras  * involves a working pcibios_fixup_pbus_ranges(), some more care about
1068e05b3b4aSPaul Mackerras  * ordering when creating the host bus resources, and maybe a few more
1069e05b3b4aSPaul Mackerras  * minor tweaks
1070e05b3b4aSPaul Mackerras  */
1071e05b3b4aSPaul Mackerras 
1072e05b3b4aSPaul Mackerras /* Initialize bridges with base/limit values we have collected */
1073e05b3b4aSPaul Mackerras static void __init
1074e05b3b4aSPaul Mackerras do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
1075e05b3b4aSPaul Mackerras {
1076e05b3b4aSPaul Mackerras 	struct pci_dev *bridge = bus->self;
1077e05b3b4aSPaul Mackerras 	struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
1078e05b3b4aSPaul Mackerras 	u32 l;
1079e05b3b4aSPaul Mackerras 	u16 w;
1080e05b3b4aSPaul Mackerras 	struct resource res;
1081e05b3b4aSPaul Mackerras 
1082e05b3b4aSPaul Mackerras 	if (bus->resource[0] == NULL)
1083e05b3b4aSPaul Mackerras 		return;
1084e05b3b4aSPaul Mackerras  	res = *(bus->resource[0]);
1085e05b3b4aSPaul Mackerras 
1086e05b3b4aSPaul Mackerras 	DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
1087e05b3b4aSPaul Mackerras 	res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
1088e05b3b4aSPaul Mackerras 	res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
1089685143acSGreg Kroah-Hartman 	DBG("  IO window: %016llx-%016llx\n", res.start, res.end);
1090e05b3b4aSPaul Mackerras 
1091e05b3b4aSPaul Mackerras 	/* Set up the top and bottom of the PCI I/O segment for this bus. */
1092e05b3b4aSPaul Mackerras 	pci_read_config_dword(bridge, PCI_IO_BASE, &l);
1093e05b3b4aSPaul Mackerras 	l &= 0xffff000f;
1094e05b3b4aSPaul Mackerras 	l |= (res.start >> 8) & 0x00f0;
1095e05b3b4aSPaul Mackerras 	l |= res.end & 0xf000;
1096e05b3b4aSPaul Mackerras 	pci_write_config_dword(bridge, PCI_IO_BASE, l);
1097e05b3b4aSPaul Mackerras 
1098e05b3b4aSPaul Mackerras 	if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
1099e05b3b4aSPaul Mackerras 		l = (res.start >> 16) | (res.end & 0xffff0000);
1100e05b3b4aSPaul Mackerras 		pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
1101e05b3b4aSPaul Mackerras 	}
1102e05b3b4aSPaul Mackerras 
1103e05b3b4aSPaul Mackerras 	pci_read_config_word(bridge, PCI_COMMAND, &w);
1104e05b3b4aSPaul Mackerras 	w |= PCI_COMMAND_IO;
1105e05b3b4aSPaul Mackerras 	pci_write_config_word(bridge, PCI_COMMAND, w);
1106e05b3b4aSPaul Mackerras 
1107e05b3b4aSPaul Mackerras #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
1108e05b3b4aSPaul Mackerras 	if (enable_vga) {
1109e05b3b4aSPaul Mackerras 		pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
1110e05b3b4aSPaul Mackerras 		w |= PCI_BRIDGE_CTL_VGA;
1111e05b3b4aSPaul Mackerras 		pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
1112e05b3b4aSPaul Mackerras 	}
1113e05b3b4aSPaul Mackerras #endif
1114e05b3b4aSPaul Mackerras }
1115e05b3b4aSPaul Mackerras 
1116e05b3b4aSPaul Mackerras /* This function is pretty basic and actually quite broken for the
1117e05b3b4aSPaul Mackerras  * general case, it's enough for us right now though. It's supposed
1118e05b3b4aSPaul Mackerras  * to tell us if we need to open an IO range at all or not and what
1119e05b3b4aSPaul Mackerras  * size.
1120e05b3b4aSPaul Mackerras  */
1121e05b3b4aSPaul Mackerras static int __init
1122e05b3b4aSPaul Mackerras check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
1123e05b3b4aSPaul Mackerras {
1124e05b3b4aSPaul Mackerras 	struct pci_dev *dev;
1125e05b3b4aSPaul Mackerras 	int	i;
1126e05b3b4aSPaul Mackerras 	int	rc = 0;
1127e05b3b4aSPaul Mackerras 
11280f582bc1SPaul Mackerras #define push_end(res, mask) do {		\
11290f582bc1SPaul Mackerras 	BUG_ON((mask+1) & mask);		\
11300f582bc1SPaul Mackerras 	res->end = (res->end + mask) | mask;	\
1131e05b3b4aSPaul Mackerras } while (0)
1132e05b3b4aSPaul Mackerras 
1133e05b3b4aSPaul Mackerras 	list_for_each_entry(dev, &bus->devices, bus_list) {
1134e05b3b4aSPaul Mackerras 		u16 class = dev->class >> 8;
1135e05b3b4aSPaul Mackerras 
1136e05b3b4aSPaul Mackerras 		if (class == PCI_CLASS_DISPLAY_VGA ||
1137e05b3b4aSPaul Mackerras 		    class == PCI_CLASS_NOT_DEFINED_VGA)
1138e05b3b4aSPaul Mackerras 			*found_vga = 1;
1139e05b3b4aSPaul Mackerras 		if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
1140e05b3b4aSPaul Mackerras 			rc |= check_for_io_childs(dev->subordinate, res, found_vga);
1141e05b3b4aSPaul Mackerras 		if (class == PCI_CLASS_BRIDGE_CARDBUS)
1142e05b3b4aSPaul Mackerras 			push_end(res, 0xfff);
1143e05b3b4aSPaul Mackerras 
1144e05b3b4aSPaul Mackerras 		for (i=0; i<PCI_NUM_RESOURCES; i++) {
1145e05b3b4aSPaul Mackerras 			struct resource *r;
1146e05b3b4aSPaul Mackerras 			unsigned long r_size;
1147e05b3b4aSPaul Mackerras 
1148e05b3b4aSPaul Mackerras 			if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
1149e05b3b4aSPaul Mackerras 			    && i >= PCI_BRIDGE_RESOURCES)
1150e05b3b4aSPaul Mackerras 				continue;
1151e05b3b4aSPaul Mackerras 			r = &dev->resource[i];
1152e05b3b4aSPaul Mackerras 			r_size = r->end - r->start;
1153e05b3b4aSPaul Mackerras 			if (r_size < 0xfff)
1154e05b3b4aSPaul Mackerras 				r_size = 0xfff;
1155e05b3b4aSPaul Mackerras 			if (r->flags & IORESOURCE_IO && (r_size) != 0) {
1156e05b3b4aSPaul Mackerras 				rc = 1;
1157e05b3b4aSPaul Mackerras 				push_end(res, r_size);
1158e05b3b4aSPaul Mackerras 			}
1159e05b3b4aSPaul Mackerras 		}
1160e05b3b4aSPaul Mackerras 	}
1161e05b3b4aSPaul Mackerras 
1162e05b3b4aSPaul Mackerras 	return rc;
1163e05b3b4aSPaul Mackerras }
1164e05b3b4aSPaul Mackerras 
1165e05b3b4aSPaul Mackerras /* Here we scan all P2P bridges of a given level that have a closed
1166e05b3b4aSPaul Mackerras  * IO window. Note that the test for the presence of a VGA card should
1167e05b3b4aSPaul Mackerras  * be improved to take into account already configured P2P bridges,
1168e05b3b4aSPaul Mackerras  * currently, we don't see them and might end up configuring 2 bridges
1169e05b3b4aSPaul Mackerras  * with VGA pass through enabled
1170e05b3b4aSPaul Mackerras  */
1171e05b3b4aSPaul Mackerras static void __init
1172e05b3b4aSPaul Mackerras do_fixup_p2p_level(struct pci_bus *bus)
1173e05b3b4aSPaul Mackerras {
1174e05b3b4aSPaul Mackerras 	struct pci_bus *b;
1175e05b3b4aSPaul Mackerras 	int i, parent_io;
1176e05b3b4aSPaul Mackerras 	int has_vga = 0;
1177e05b3b4aSPaul Mackerras 
1178e05b3b4aSPaul Mackerras 	for (parent_io=0; parent_io<4; parent_io++)
1179e05b3b4aSPaul Mackerras 		if (bus->resource[parent_io]
1180e05b3b4aSPaul Mackerras 		    && bus->resource[parent_io]->flags & IORESOURCE_IO)
1181e05b3b4aSPaul Mackerras 			break;
1182e05b3b4aSPaul Mackerras 	if (parent_io >= 4)
1183e05b3b4aSPaul Mackerras 		return;
1184e05b3b4aSPaul Mackerras 
1185e05b3b4aSPaul Mackerras 	list_for_each_entry(b, &bus->children, node) {
1186e05b3b4aSPaul Mackerras 		struct pci_dev *d = b->self;
1187e05b3b4aSPaul Mackerras 		struct pci_controller* hose = (struct pci_controller *)d->sysdata;
1188e05b3b4aSPaul Mackerras 		struct resource *res = b->resource[0];
1189e05b3b4aSPaul Mackerras 		struct resource tmp_res;
1190e05b3b4aSPaul Mackerras 		unsigned long max;
1191e05b3b4aSPaul Mackerras 		int found_vga = 0;
1192e05b3b4aSPaul Mackerras 
1193e05b3b4aSPaul Mackerras 		memset(&tmp_res, 0, sizeof(tmp_res));
1194e05b3b4aSPaul Mackerras 		tmp_res.start = bus->resource[parent_io]->start;
1195e05b3b4aSPaul Mackerras 
1196e05b3b4aSPaul Mackerras 		/* We don't let low addresses go through that closed P2P bridge, well,
1197e05b3b4aSPaul Mackerras 		 * that may not be necessary but I feel safer that way
1198e05b3b4aSPaul Mackerras 		 */
1199e05b3b4aSPaul Mackerras 		if (tmp_res.start == 0)
1200e05b3b4aSPaul Mackerras 			tmp_res.start = 0x1000;
1201e05b3b4aSPaul Mackerras 
1202e05b3b4aSPaul Mackerras 		if (!list_empty(&b->devices) && res && res->flags == 0 &&
1203e05b3b4aSPaul Mackerras 		    res != bus->resource[parent_io] &&
1204e05b3b4aSPaul Mackerras 		    (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
1205e05b3b4aSPaul Mackerras 		    check_for_io_childs(b, &tmp_res, &found_vga)) {
1206e05b3b4aSPaul Mackerras 			u8 io_base_lo;
1207e05b3b4aSPaul Mackerras 
1208e05b3b4aSPaul Mackerras 			printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
1209e05b3b4aSPaul Mackerras 
1210e05b3b4aSPaul Mackerras 			if (found_vga) {
1211e05b3b4aSPaul Mackerras 				if (has_vga) {
1212e05b3b4aSPaul Mackerras 					printk(KERN_WARNING "Skipping VGA, already active"
1213e05b3b4aSPaul Mackerras 					    " on bus segment\n");
1214e05b3b4aSPaul Mackerras 					found_vga = 0;
1215e05b3b4aSPaul Mackerras 				} else
1216e05b3b4aSPaul Mackerras 					has_vga = 1;
1217e05b3b4aSPaul Mackerras 			}
1218e05b3b4aSPaul Mackerras 			pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
1219e05b3b4aSPaul Mackerras 
1220e05b3b4aSPaul Mackerras 			if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
1221e05b3b4aSPaul Mackerras 				max = ((unsigned long) hose->io_base_virt
1222e05b3b4aSPaul Mackerras 					- isa_io_base) + 0xffffffff;
1223e05b3b4aSPaul Mackerras 			else
1224e05b3b4aSPaul Mackerras 				max = ((unsigned long) hose->io_base_virt
1225e05b3b4aSPaul Mackerras 					- isa_io_base) + 0xffff;
1226e05b3b4aSPaul Mackerras 
1227e05b3b4aSPaul Mackerras 			*res = tmp_res;
1228e05b3b4aSPaul Mackerras 			res->flags = IORESOURCE_IO;
1229e05b3b4aSPaul Mackerras 			res->name = b->name;
1230e05b3b4aSPaul Mackerras 
1231e05b3b4aSPaul Mackerras 			/* Find a resource in the parent where we can allocate */
1232e05b3b4aSPaul Mackerras 			for (i = 0 ; i < 4; i++) {
1233e05b3b4aSPaul Mackerras 				struct resource *r = bus->resource[i];
1234e05b3b4aSPaul Mackerras 				if (!r)
1235e05b3b4aSPaul Mackerras 					continue;
1236e05b3b4aSPaul Mackerras 				if ((r->flags & IORESOURCE_IO) == 0)
1237e05b3b4aSPaul Mackerras 					continue;
1238685143acSGreg Kroah-Hartman 				DBG("Trying to allocate from %016llx, size %016llx from parent"
1239685143acSGreg Kroah-Hartman 				    " res %d: %016llx -> %016llx\n",
1240e05b3b4aSPaul Mackerras 					res->start, res->end, i, r->start, r->end);
1241e05b3b4aSPaul Mackerras 
1242e05b3b4aSPaul Mackerras 				if (allocate_resource(r, res, res->end + 1, res->start, max,
1243e05b3b4aSPaul Mackerras 				    res->end + 1, NULL, NULL) < 0) {
1244e05b3b4aSPaul Mackerras 					DBG("Failed !\n");
1245e05b3b4aSPaul Mackerras 					continue;
1246e05b3b4aSPaul Mackerras 				}
1247e05b3b4aSPaul Mackerras 				do_update_p2p_io_resource(b, found_vga);
1248e05b3b4aSPaul Mackerras 				break;
1249e05b3b4aSPaul Mackerras 			}
1250e05b3b4aSPaul Mackerras 		}
1251e05b3b4aSPaul Mackerras 		do_fixup_p2p_level(b);
1252e05b3b4aSPaul Mackerras 	}
1253e05b3b4aSPaul Mackerras }
1254e05b3b4aSPaul Mackerras 
1255e05b3b4aSPaul Mackerras static void
1256e05b3b4aSPaul Mackerras pcibios_fixup_p2p_bridges(void)
1257e05b3b4aSPaul Mackerras {
1258e05b3b4aSPaul Mackerras 	struct pci_bus *b;
1259e05b3b4aSPaul Mackerras 
1260e05b3b4aSPaul Mackerras 	list_for_each_entry(b, &pci_root_buses, node)
1261e05b3b4aSPaul Mackerras 		do_fixup_p2p_level(b);
1262e05b3b4aSPaul Mackerras }
1263e05b3b4aSPaul Mackerras 
1264e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_PMAC */
1265e05b3b4aSPaul Mackerras 
1266e05b3b4aSPaul Mackerras static int __init
1267e05b3b4aSPaul Mackerras pcibios_init(void)
1268e05b3b4aSPaul Mackerras {
1269e05b3b4aSPaul Mackerras 	struct pci_controller *hose;
1270e05b3b4aSPaul Mackerras 	struct pci_bus *bus;
1271e05b3b4aSPaul Mackerras 	int next_busno;
1272e05b3b4aSPaul Mackerras 
1273e05b3b4aSPaul Mackerras 	printk(KERN_INFO "PCI: Probing PCI hardware\n");
1274e05b3b4aSPaul Mackerras 
1275e05b3b4aSPaul Mackerras 	/* Scan all of the recorded PCI controllers.  */
1276e05b3b4aSPaul Mackerras 	for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
1277e05b3b4aSPaul Mackerras 		if (pci_assign_all_buses)
1278e05b3b4aSPaul Mackerras 			hose->first_busno = next_busno;
1279e05b3b4aSPaul Mackerras 		hose->last_busno = 0xff;
1280803d4573SBenjamin Herrenschmidt 		bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
1281803d4573SBenjamin Herrenschmidt 					    hose->ops, hose);
1282803d4573SBenjamin Herrenschmidt 		if (bus)
1283803d4573SBenjamin Herrenschmidt 			pci_bus_add_devices(bus);
1284e05b3b4aSPaul Mackerras 		hose->last_busno = bus->subordinate;
1285e05b3b4aSPaul Mackerras 		if (pci_assign_all_buses || next_busno <= hose->last_busno)
1286e05b3b4aSPaul Mackerras 			next_busno = hose->last_busno + pcibios_assign_bus_offset;
1287e05b3b4aSPaul Mackerras 	}
1288e05b3b4aSPaul Mackerras 	pci_bus_count = next_busno;
1289e05b3b4aSPaul Mackerras 
1290e05b3b4aSPaul Mackerras 	/* OpenFirmware based machines need a map of OF bus
1291e05b3b4aSPaul Mackerras 	 * numbers vs. kernel bus numbers since we may have to
1292e05b3b4aSPaul Mackerras 	 * remap them.
1293e05b3b4aSPaul Mackerras 	 */
1294e05b3b4aSPaul Mackerras 	if (pci_assign_all_buses && have_of)
1295e05b3b4aSPaul Mackerras 		pcibios_make_OF_bus_map();
1296e05b3b4aSPaul Mackerras 
1297e05b3b4aSPaul Mackerras 	/* Call machine dependent fixup */
1298e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_fixup)
1299e05b3b4aSPaul Mackerras 		ppc_md.pcibios_fixup();
1300e05b3b4aSPaul Mackerras 
1301e05b3b4aSPaul Mackerras 	/* Allocate and assign resources */
1302e05b3b4aSPaul Mackerras 	pcibios_allocate_bus_resources(&pci_root_buses);
1303e05b3b4aSPaul Mackerras 	pcibios_allocate_resources(0);
1304e05b3b4aSPaul Mackerras 	pcibios_allocate_resources(1);
1305e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_PMAC
1306e05b3b4aSPaul Mackerras 	pcibios_fixup_p2p_bridges();
1307e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_PMAC */
1308e05b3b4aSPaul Mackerras 	pcibios_assign_resources();
1309e05b3b4aSPaul Mackerras 
1310e05b3b4aSPaul Mackerras 	/* Call machine dependent post-init code */
1311e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_after_init)
1312e05b3b4aSPaul Mackerras 		ppc_md.pcibios_after_init();
1313e05b3b4aSPaul Mackerras 
1314e05b3b4aSPaul Mackerras 	return 0;
1315e05b3b4aSPaul Mackerras }
1316e05b3b4aSPaul Mackerras 
1317e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init);
1318e05b3b4aSPaul Mackerras 
1319e05b3b4aSPaul Mackerras unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
1320e05b3b4aSPaul Mackerras 			     unsigned long start, unsigned long size)
1321e05b3b4aSPaul Mackerras {
1322e05b3b4aSPaul Mackerras 	return start;
1323e05b3b4aSPaul Mackerras }
1324e05b3b4aSPaul Mackerras 
1325e05b3b4aSPaul Mackerras void __init pcibios_fixup_bus(struct pci_bus *bus)
1326e05b3b4aSPaul Mackerras {
1327e05b3b4aSPaul Mackerras 	struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1328e05b3b4aSPaul Mackerras 	unsigned long io_offset;
1329e05b3b4aSPaul Mackerras 	struct resource *res;
1330f90bb153SBenjamin Herrenschmidt 	struct pci_dev *dev;
1331e05b3b4aSPaul Mackerras 	int i;
1332e05b3b4aSPaul Mackerras 
1333e05b3b4aSPaul Mackerras 	io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1334e05b3b4aSPaul Mackerras 	if (bus->parent == NULL) {
1335e05b3b4aSPaul Mackerras 		/* This is a host bridge - fill in its resources */
1336e05b3b4aSPaul Mackerras 		hose->bus = bus;
1337e05b3b4aSPaul Mackerras 
1338e05b3b4aSPaul Mackerras 		bus->resource[0] = res = &hose->io_resource;
1339e05b3b4aSPaul Mackerras 		if (!res->flags) {
1340e05b3b4aSPaul Mackerras 			if (io_offset)
1341e05b3b4aSPaul Mackerras 				printk(KERN_ERR "I/O resource not set for host"
1342e05b3b4aSPaul Mackerras 				       " bridge %d\n", hose->index);
1343e05b3b4aSPaul Mackerras 			res->start = 0;
1344e05b3b4aSPaul Mackerras 			res->end = IO_SPACE_LIMIT;
1345e05b3b4aSPaul Mackerras 			res->flags = IORESOURCE_IO;
1346e05b3b4aSPaul Mackerras 		}
1347e05b3b4aSPaul Mackerras 		res->start += io_offset;
1348e05b3b4aSPaul Mackerras 		res->end += io_offset;
1349e05b3b4aSPaul Mackerras 
1350e05b3b4aSPaul Mackerras 		for (i = 0; i < 3; ++i) {
1351e05b3b4aSPaul Mackerras 			res = &hose->mem_resources[i];
1352e05b3b4aSPaul Mackerras 			if (!res->flags) {
1353e05b3b4aSPaul Mackerras 				if (i > 0)
1354e05b3b4aSPaul Mackerras 					continue;
1355e05b3b4aSPaul Mackerras 				printk(KERN_ERR "Memory resource not set for "
1356e05b3b4aSPaul Mackerras 				       "host bridge %d\n", hose->index);
1357e05b3b4aSPaul Mackerras 				res->start = hose->pci_mem_offset;
1358e05b3b4aSPaul Mackerras 				res->end = ~0U;
1359e05b3b4aSPaul Mackerras 				res->flags = IORESOURCE_MEM;
1360e05b3b4aSPaul Mackerras 			}
1361e05b3b4aSPaul Mackerras 			bus->resource[i+1] = res;
1362e05b3b4aSPaul Mackerras 		}
1363e05b3b4aSPaul Mackerras 	} else {
1364e05b3b4aSPaul Mackerras 		/* This is a subordinate bridge */
1365e05b3b4aSPaul Mackerras 		pci_read_bridge_bases(bus);
1366e05b3b4aSPaul Mackerras 
1367e05b3b4aSPaul Mackerras 		for (i = 0; i < 4; ++i) {
1368e05b3b4aSPaul Mackerras 			if ((res = bus->resource[i]) == NULL)
1369e05b3b4aSPaul Mackerras 				continue;
1370e05b3b4aSPaul Mackerras 			if (!res->flags)
1371e05b3b4aSPaul Mackerras 				continue;
1372e05b3b4aSPaul Mackerras 			if (io_offset && (res->flags & IORESOURCE_IO)) {
1373e05b3b4aSPaul Mackerras 				res->start += io_offset;
1374e05b3b4aSPaul Mackerras 				res->end += io_offset;
1375e05b3b4aSPaul Mackerras 			} else if (hose->pci_mem_offset
1376e05b3b4aSPaul Mackerras 				   && (res->flags & IORESOURCE_MEM)) {
1377e05b3b4aSPaul Mackerras 				res->start += hose->pci_mem_offset;
1378e05b3b4aSPaul Mackerras 				res->end += hose->pci_mem_offset;
1379e05b3b4aSPaul Mackerras 			}
1380e05b3b4aSPaul Mackerras 		}
1381e05b3b4aSPaul Mackerras 	}
1382e05b3b4aSPaul Mackerras 
1383f90bb153SBenjamin Herrenschmidt 	/* Platform specific bus fixups */
1384e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_fixup_bus)
1385e05b3b4aSPaul Mackerras 		ppc_md.pcibios_fixup_bus(bus);
1386f90bb153SBenjamin Herrenschmidt 
1387f90bb153SBenjamin Herrenschmidt 	/* Read default IRQs and fixup if necessary */
1388f90bb153SBenjamin Herrenschmidt 	list_for_each_entry(dev, &bus->devices, bus_list) {
1389f90bb153SBenjamin Herrenschmidt 		pci_read_irq_line(dev);
1390f90bb153SBenjamin Herrenschmidt 		if (ppc_md.pci_irq_fixup)
1391f90bb153SBenjamin Herrenschmidt 			ppc_md.pci_irq_fixup(dev);
1392f90bb153SBenjamin Herrenschmidt 	}
1393e05b3b4aSPaul Mackerras }
1394e05b3b4aSPaul Mackerras 
1395e05b3b4aSPaul Mackerras char __init *pcibios_setup(char *str)
1396e05b3b4aSPaul Mackerras {
1397e05b3b4aSPaul Mackerras 	return str;
1398e05b3b4aSPaul Mackerras }
1399e05b3b4aSPaul Mackerras 
1400e05b3b4aSPaul Mackerras /* the next one is stolen from the alpha port... */
1401e05b3b4aSPaul Mackerras void __init
1402e05b3b4aSPaul Mackerras pcibios_update_irq(struct pci_dev *dev, int irq)
1403e05b3b4aSPaul Mackerras {
1404e05b3b4aSPaul Mackerras 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1405e05b3b4aSPaul Mackerras 	/* XXX FIXME - update OF device tree node interrupt property */
1406e05b3b4aSPaul Mackerras }
1407e05b3b4aSPaul Mackerras 
14080ebfff14SBenjamin Herrenschmidt #ifdef CONFIG_PPC_MERGE
14090ebfff14SBenjamin Herrenschmidt /* XXX This is a copy of the ppc64 version. This is temporary until we start
14100ebfff14SBenjamin Herrenschmidt  * merging the 2 PCI layers
14110ebfff14SBenjamin Herrenschmidt  */
14120ebfff14SBenjamin Herrenschmidt /*
14130ebfff14SBenjamin Herrenschmidt  * Reads the interrupt pin to determine if interrupt is use by card.
14140ebfff14SBenjamin Herrenschmidt  * If the interrupt is used, then gets the interrupt line from the
14150ebfff14SBenjamin Herrenschmidt  * openfirmware and sets it in the pci_dev and pci_config line.
14160ebfff14SBenjamin Herrenschmidt  */
14170ebfff14SBenjamin Herrenschmidt int pci_read_irq_line(struct pci_dev *pci_dev)
14180ebfff14SBenjamin Herrenschmidt {
14190ebfff14SBenjamin Herrenschmidt 	struct of_irq oirq;
14200ebfff14SBenjamin Herrenschmidt 	unsigned int virq;
14210ebfff14SBenjamin Herrenschmidt 
14220ebfff14SBenjamin Herrenschmidt 	DBG("Try to map irq for %s...\n", pci_name(pci_dev));
14230ebfff14SBenjamin Herrenschmidt 
14246e99e458SBenjamin Herrenschmidt 	/* Try to get a mapping from the device-tree */
14250ebfff14SBenjamin Herrenschmidt 	if (of_irq_map_pci(pci_dev, &oirq)) {
14266e99e458SBenjamin Herrenschmidt 		u8 line, pin;
14276e99e458SBenjamin Herrenschmidt 
14286e99e458SBenjamin Herrenschmidt 		/* If that fails, lets fallback to what is in the config
14296e99e458SBenjamin Herrenschmidt 		 * space and map that through the default controller. We
14306e99e458SBenjamin Herrenschmidt 		 * also set the type to level low since that's what PCI
14316e99e458SBenjamin Herrenschmidt 		 * interrupts are. If your platform does differently, then
14326e99e458SBenjamin Herrenschmidt 		 * either provide a proper interrupt tree or don't use this
14336e99e458SBenjamin Herrenschmidt 		 * function.
14346e99e458SBenjamin Herrenschmidt 		 */
14356e99e458SBenjamin Herrenschmidt 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
14366e99e458SBenjamin Herrenschmidt 			return -1;
14376e99e458SBenjamin Herrenschmidt 		if (pin == 0)
14386e99e458SBenjamin Herrenschmidt 			return -1;
14396e99e458SBenjamin Herrenschmidt 		if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
14406e99e458SBenjamin Herrenschmidt 		    line == 0xff) {
14410ebfff14SBenjamin Herrenschmidt 			return -1;
14420ebfff14SBenjamin Herrenschmidt 		}
14436e99e458SBenjamin Herrenschmidt 		DBG(" -> no map ! Using irq line %d from PCI config\n", line);
14440ebfff14SBenjamin Herrenschmidt 
14456e99e458SBenjamin Herrenschmidt 		virq = irq_create_mapping(NULL, line);
14466e99e458SBenjamin Herrenschmidt 		if (virq != NO_IRQ)
14476e99e458SBenjamin Herrenschmidt 			set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
14486e99e458SBenjamin Herrenschmidt 	} else {
14490ebfff14SBenjamin Herrenschmidt 		DBG(" -> got one, spec %d cells (0x%08x...) on %s\n",
14500ebfff14SBenjamin Herrenschmidt 		    oirq.size, oirq.specifier[0], oirq.controller->full_name);
14510ebfff14SBenjamin Herrenschmidt 
14526e99e458SBenjamin Herrenschmidt 		virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
14536e99e458SBenjamin Herrenschmidt 					     oirq.size);
14546e99e458SBenjamin Herrenschmidt 	}
14550ebfff14SBenjamin Herrenschmidt 	if(virq == NO_IRQ) {
14560ebfff14SBenjamin Herrenschmidt 		DBG(" -> failed to map !\n");
14570ebfff14SBenjamin Herrenschmidt 		return -1;
14580ebfff14SBenjamin Herrenschmidt 	}
14590ebfff14SBenjamin Herrenschmidt 	pci_dev->irq = virq;
14600ebfff14SBenjamin Herrenschmidt 
14610ebfff14SBenjamin Herrenschmidt 	return 0;
14620ebfff14SBenjamin Herrenschmidt }
14630ebfff14SBenjamin Herrenschmidt EXPORT_SYMBOL(pci_read_irq_line);
14640ebfff14SBenjamin Herrenschmidt #endif /* CONFIG_PPC_MERGE */
14650ebfff14SBenjamin Herrenschmidt 
1466e05b3b4aSPaul Mackerras int pcibios_enable_device(struct pci_dev *dev, int mask)
1467e05b3b4aSPaul Mackerras {
1468e05b3b4aSPaul Mackerras 	u16 cmd, old_cmd;
1469e05b3b4aSPaul Mackerras 	int idx;
1470e05b3b4aSPaul Mackerras 	struct resource *r;
1471e05b3b4aSPaul Mackerras 
1472e05b3b4aSPaul Mackerras 	if (ppc_md.pcibios_enable_device_hook)
1473e05b3b4aSPaul Mackerras 		if (ppc_md.pcibios_enable_device_hook(dev, 0))
1474e05b3b4aSPaul Mackerras 			return -EINVAL;
1475e05b3b4aSPaul Mackerras 
1476e05b3b4aSPaul Mackerras 	pci_read_config_word(dev, PCI_COMMAND, &cmd);
1477e05b3b4aSPaul Mackerras 	old_cmd = cmd;
1478e05b3b4aSPaul Mackerras 	for (idx=0; idx<6; idx++) {
1479e05b3b4aSPaul Mackerras 		r = &dev->resource[idx];
1480e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_UNSET) {
1481e05b3b4aSPaul Mackerras 			printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1482e05b3b4aSPaul Mackerras 			return -EINVAL;
1483e05b3b4aSPaul Mackerras 		}
1484e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_IO)
1485e05b3b4aSPaul Mackerras 			cmd |= PCI_COMMAND_IO;
1486e05b3b4aSPaul Mackerras 		if (r->flags & IORESOURCE_MEM)
1487e05b3b4aSPaul Mackerras 			cmd |= PCI_COMMAND_MEMORY;
1488e05b3b4aSPaul Mackerras 	}
1489e05b3b4aSPaul Mackerras 	if (cmd != old_cmd) {
1490e05b3b4aSPaul Mackerras 		printk("PCI: Enabling device %s (%04x -> %04x)\n",
1491e05b3b4aSPaul Mackerras 		       pci_name(dev), old_cmd, cmd);
1492e05b3b4aSPaul Mackerras 		pci_write_config_word(dev, PCI_COMMAND, cmd);
1493e05b3b4aSPaul Mackerras 	}
1494e05b3b4aSPaul Mackerras 	return 0;
1495e05b3b4aSPaul Mackerras }
1496e05b3b4aSPaul Mackerras 
1497e05b3b4aSPaul Mackerras struct pci_controller*
1498e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus)
1499e05b3b4aSPaul Mackerras {
1500e05b3b4aSPaul Mackerras 	struct pci_controller* hose = hose_head;
1501e05b3b4aSPaul Mackerras 
1502e05b3b4aSPaul Mackerras 	for (; hose; hose = hose->next)
1503e05b3b4aSPaul Mackerras 		if (bus >= hose->first_busno && bus <= hose->last_busno)
1504e05b3b4aSPaul Mackerras 			return hose;
1505e05b3b4aSPaul Mackerras 	return NULL;
1506e05b3b4aSPaul Mackerras }
1507e05b3b4aSPaul Mackerras 
1508e05b3b4aSPaul Mackerras void __iomem *
1509e05b3b4aSPaul Mackerras pci_bus_io_base(unsigned int bus)
1510e05b3b4aSPaul Mackerras {
1511e05b3b4aSPaul Mackerras 	struct pci_controller *hose;
1512e05b3b4aSPaul Mackerras 
1513e05b3b4aSPaul Mackerras 	hose = pci_bus_to_hose(bus);
1514e05b3b4aSPaul Mackerras 	if (!hose)
1515e05b3b4aSPaul Mackerras 		return NULL;
1516e05b3b4aSPaul Mackerras 	return hose->io_base_virt;
1517e05b3b4aSPaul Mackerras }
1518e05b3b4aSPaul Mackerras 
1519e05b3b4aSPaul Mackerras unsigned long
1520e05b3b4aSPaul Mackerras pci_bus_io_base_phys(unsigned int bus)
1521e05b3b4aSPaul Mackerras {
1522e05b3b4aSPaul Mackerras 	struct pci_controller *hose;
1523e05b3b4aSPaul Mackerras 
1524e05b3b4aSPaul Mackerras 	hose = pci_bus_to_hose(bus);
1525e05b3b4aSPaul Mackerras 	if (!hose)
1526e05b3b4aSPaul Mackerras 		return 0;
1527e05b3b4aSPaul Mackerras 	return hose->io_base_phys;
1528e05b3b4aSPaul Mackerras }
1529e05b3b4aSPaul Mackerras 
1530e05b3b4aSPaul Mackerras unsigned long
1531e05b3b4aSPaul Mackerras pci_bus_mem_base_phys(unsigned int bus)
1532e05b3b4aSPaul Mackerras {
1533e05b3b4aSPaul Mackerras 	struct pci_controller *hose;
1534e05b3b4aSPaul Mackerras 
1535e05b3b4aSPaul Mackerras 	hose = pci_bus_to_hose(bus);
1536e05b3b4aSPaul Mackerras 	if (!hose)
1537e05b3b4aSPaul Mackerras 		return 0;
1538e05b3b4aSPaul Mackerras 	return hose->pci_mem_offset;
1539e05b3b4aSPaul Mackerras }
1540e05b3b4aSPaul Mackerras 
1541e05b3b4aSPaul Mackerras unsigned long
1542e05b3b4aSPaul Mackerras pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
1543e05b3b4aSPaul Mackerras {
1544e05b3b4aSPaul Mackerras 	/* Hack alert again ! See comments in chrp_pci.c
1545e05b3b4aSPaul Mackerras 	 */
1546e05b3b4aSPaul Mackerras 	struct pci_controller* hose =
1547e05b3b4aSPaul Mackerras 		(struct pci_controller *)pdev->sysdata;
1548e05b3b4aSPaul Mackerras 	if (hose && res->flags & IORESOURCE_MEM)
1549e05b3b4aSPaul Mackerras 		return res->start - hose->pci_mem_offset;
1550e05b3b4aSPaul Mackerras 	/* We may want to do something with IOs here... */
1551e05b3b4aSPaul Mackerras 	return res->start;
1552e05b3b4aSPaul Mackerras }
1553e05b3b4aSPaul Mackerras 
1554e05b3b4aSPaul Mackerras 
1555e05b3b4aSPaul Mackerras static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
1556396a1a58SBenjamin Herrenschmidt 					       resource_size_t *offset,
1557e05b3b4aSPaul Mackerras 					       enum pci_mmap_state mmap_state)
1558e05b3b4aSPaul Mackerras {
1559e05b3b4aSPaul Mackerras 	struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
1560e05b3b4aSPaul Mackerras 	unsigned long io_offset = 0;
1561e05b3b4aSPaul Mackerras 	int i, res_bit;
1562e05b3b4aSPaul Mackerras 
1563e05b3b4aSPaul Mackerras 	if (hose == 0)
1564e05b3b4aSPaul Mackerras 		return NULL;		/* should never happen */
1565e05b3b4aSPaul Mackerras 
1566e05b3b4aSPaul Mackerras 	/* If memory, add on the PCI bridge address offset */
1567e05b3b4aSPaul Mackerras 	if (mmap_state == pci_mmap_mem) {
1568396a1a58SBenjamin Herrenschmidt #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
1569e05b3b4aSPaul Mackerras 		*offset += hose->pci_mem_offset;
1570396a1a58SBenjamin Herrenschmidt #endif
1571e05b3b4aSPaul Mackerras 		res_bit = IORESOURCE_MEM;
1572e05b3b4aSPaul Mackerras 	} else {
157368a64357SBenjamin Herrenschmidt 		io_offset = hose->io_base_virt - (void __iomem *)_IO_BASE;
1574e05b3b4aSPaul Mackerras 		*offset += io_offset;
1575e05b3b4aSPaul Mackerras 		res_bit = IORESOURCE_IO;
1576e05b3b4aSPaul Mackerras 	}
1577e05b3b4aSPaul Mackerras 
1578e05b3b4aSPaul Mackerras 	/*
1579e05b3b4aSPaul Mackerras 	 * Check that the offset requested corresponds to one of the
1580e05b3b4aSPaul Mackerras 	 * resources of the device.
1581e05b3b4aSPaul Mackerras 	 */
1582e05b3b4aSPaul Mackerras 	for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
1583e05b3b4aSPaul Mackerras 		struct resource *rp = &dev->resource[i];
1584e05b3b4aSPaul Mackerras 		int flags = rp->flags;
1585e05b3b4aSPaul Mackerras 
1586e05b3b4aSPaul Mackerras 		/* treat ROM as memory (should be already) */
1587e05b3b4aSPaul Mackerras 		if (i == PCI_ROM_RESOURCE)
1588e05b3b4aSPaul Mackerras 			flags |= IORESOURCE_MEM;
1589e05b3b4aSPaul Mackerras 
1590e05b3b4aSPaul Mackerras 		/* Active and same type? */
1591e05b3b4aSPaul Mackerras 		if ((flags & res_bit) == 0)
1592e05b3b4aSPaul Mackerras 			continue;
1593e05b3b4aSPaul Mackerras 
1594e05b3b4aSPaul Mackerras 		/* In the range of this resource? */
1595e05b3b4aSPaul Mackerras 		if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
1596e05b3b4aSPaul Mackerras 			continue;
1597e05b3b4aSPaul Mackerras 
1598e05b3b4aSPaul Mackerras 		/* found it! construct the final physical address */
1599e05b3b4aSPaul Mackerras 		if (mmap_state == pci_mmap_io)
1600e05b3b4aSPaul Mackerras 			*offset += hose->io_base_phys - io_offset;
1601e05b3b4aSPaul Mackerras 		return rp;
1602e05b3b4aSPaul Mackerras 	}
1603e05b3b4aSPaul Mackerras 
1604e05b3b4aSPaul Mackerras 	return NULL;
1605e05b3b4aSPaul Mackerras }
1606e05b3b4aSPaul Mackerras 
1607e05b3b4aSPaul Mackerras /*
1608e05b3b4aSPaul Mackerras  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1609e05b3b4aSPaul Mackerras  * device mapping.
1610e05b3b4aSPaul Mackerras  */
1611e05b3b4aSPaul Mackerras static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
1612e05b3b4aSPaul Mackerras 				      pgprot_t protection,
1613e05b3b4aSPaul Mackerras 				      enum pci_mmap_state mmap_state,
1614e05b3b4aSPaul Mackerras 				      int write_combine)
1615e05b3b4aSPaul Mackerras {
1616e05b3b4aSPaul Mackerras 	unsigned long prot = pgprot_val(protection);
1617e05b3b4aSPaul Mackerras 
1618e05b3b4aSPaul Mackerras 	/* Write combine is always 0 on non-memory space mappings. On
1619e05b3b4aSPaul Mackerras 	 * memory space, if the user didn't pass 1, we check for a
1620e05b3b4aSPaul Mackerras 	 * "prefetchable" resource. This is a bit hackish, but we use
1621e05b3b4aSPaul Mackerras 	 * this to workaround the inability of /sysfs to provide a write
1622e05b3b4aSPaul Mackerras 	 * combine bit
1623e05b3b4aSPaul Mackerras 	 */
1624e05b3b4aSPaul Mackerras 	if (mmap_state != pci_mmap_mem)
1625e05b3b4aSPaul Mackerras 		write_combine = 0;
1626e05b3b4aSPaul Mackerras 	else if (write_combine == 0) {
1627e05b3b4aSPaul Mackerras 		if (rp->flags & IORESOURCE_PREFETCH)
1628e05b3b4aSPaul Mackerras 			write_combine = 1;
1629e05b3b4aSPaul Mackerras 	}
1630e05b3b4aSPaul Mackerras 
1631e05b3b4aSPaul Mackerras 	/* XXX would be nice to have a way to ask for write-through */
1632e05b3b4aSPaul Mackerras 	prot |= _PAGE_NO_CACHE;
1633e05b3b4aSPaul Mackerras 	if (write_combine)
1634e05b3b4aSPaul Mackerras 		prot &= ~_PAGE_GUARDED;
1635e05b3b4aSPaul Mackerras 	else
1636e05b3b4aSPaul Mackerras 		prot |= _PAGE_GUARDED;
1637e05b3b4aSPaul Mackerras 
1638e05b3b4aSPaul Mackerras 	return __pgprot(prot);
1639e05b3b4aSPaul Mackerras }
1640e05b3b4aSPaul Mackerras 
1641e05b3b4aSPaul Mackerras /*
1642e05b3b4aSPaul Mackerras  * This one is used by /dev/mem and fbdev who have no clue about the
1643e05b3b4aSPaul Mackerras  * PCI device, it tries to find the PCI device first and calls the
1644e05b3b4aSPaul Mackerras  * above routine
1645e05b3b4aSPaul Mackerras  */
1646e05b3b4aSPaul Mackerras pgprot_t pci_phys_mem_access_prot(struct file *file,
1647e05b3b4aSPaul Mackerras 				  unsigned long pfn,
1648e05b3b4aSPaul Mackerras 				  unsigned long size,
1649e05b3b4aSPaul Mackerras 				  pgprot_t protection)
1650e05b3b4aSPaul Mackerras {
1651e05b3b4aSPaul Mackerras 	struct pci_dev *pdev = NULL;
1652e05b3b4aSPaul Mackerras 	struct resource *found = NULL;
1653e05b3b4aSPaul Mackerras 	unsigned long prot = pgprot_val(protection);
1654e05b3b4aSPaul Mackerras 	unsigned long offset = pfn << PAGE_SHIFT;
1655e05b3b4aSPaul Mackerras 	int i;
1656e05b3b4aSPaul Mackerras 
1657e05b3b4aSPaul Mackerras 	if (page_is_ram(pfn))
1658e05b3b4aSPaul Mackerras 		return prot;
1659e05b3b4aSPaul Mackerras 
1660e05b3b4aSPaul Mackerras 	prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
1661e05b3b4aSPaul Mackerras 
1662e05b3b4aSPaul Mackerras 	for_each_pci_dev(pdev) {
1663e05b3b4aSPaul Mackerras 		for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
1664e05b3b4aSPaul Mackerras 			struct resource *rp = &pdev->resource[i];
1665e05b3b4aSPaul Mackerras 			int flags = rp->flags;
1666e05b3b4aSPaul Mackerras 
1667e05b3b4aSPaul Mackerras 			/* Active and same type? */
1668e05b3b4aSPaul Mackerras 			if ((flags & IORESOURCE_MEM) == 0)
1669e05b3b4aSPaul Mackerras 				continue;
1670e05b3b4aSPaul Mackerras 			/* In the range of this resource? */
1671e05b3b4aSPaul Mackerras 			if (offset < (rp->start & PAGE_MASK) ||
1672e05b3b4aSPaul Mackerras 			    offset > rp->end)
1673e05b3b4aSPaul Mackerras 				continue;
1674e05b3b4aSPaul Mackerras 			found = rp;
1675e05b3b4aSPaul Mackerras 			break;
1676e05b3b4aSPaul Mackerras 		}
1677e05b3b4aSPaul Mackerras 		if (found)
1678e05b3b4aSPaul Mackerras 			break;
1679e05b3b4aSPaul Mackerras 	}
1680e05b3b4aSPaul Mackerras 	if (found) {
1681e05b3b4aSPaul Mackerras 		if (found->flags & IORESOURCE_PREFETCH)
1682e05b3b4aSPaul Mackerras 			prot &= ~_PAGE_GUARDED;
1683e05b3b4aSPaul Mackerras 		pci_dev_put(pdev);
1684e05b3b4aSPaul Mackerras 	}
1685e05b3b4aSPaul Mackerras 
1686e05b3b4aSPaul Mackerras 	DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
1687e05b3b4aSPaul Mackerras 
1688e05b3b4aSPaul Mackerras 	return __pgprot(prot);
1689e05b3b4aSPaul Mackerras }
1690e05b3b4aSPaul Mackerras 
1691e05b3b4aSPaul Mackerras 
1692e05b3b4aSPaul Mackerras /*
1693e05b3b4aSPaul Mackerras  * Perform the actual remap of the pages for a PCI device mapping, as
1694e05b3b4aSPaul Mackerras  * appropriate for this architecture.  The region in the process to map
1695e05b3b4aSPaul Mackerras  * is described by vm_start and vm_end members of VMA, the base physical
1696e05b3b4aSPaul Mackerras  * address is found in vm_pgoff.
1697e05b3b4aSPaul Mackerras  * The pci device structure is provided so that architectures may make mapping
1698e05b3b4aSPaul Mackerras  * decisions on a per-device or per-bus basis.
1699e05b3b4aSPaul Mackerras  *
1700e05b3b4aSPaul Mackerras  * Returns a negative error code on failure, zero on success.
1701e05b3b4aSPaul Mackerras  */
1702e05b3b4aSPaul Mackerras int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1703e05b3b4aSPaul Mackerras 			enum pci_mmap_state mmap_state,
1704e05b3b4aSPaul Mackerras 			int write_combine)
1705e05b3b4aSPaul Mackerras {
1706396a1a58SBenjamin Herrenschmidt 	resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
1707e05b3b4aSPaul Mackerras 	struct resource *rp;
1708e05b3b4aSPaul Mackerras 	int ret;
1709e05b3b4aSPaul Mackerras 
1710e05b3b4aSPaul Mackerras 	rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
1711e05b3b4aSPaul Mackerras 	if (rp == NULL)
1712e05b3b4aSPaul Mackerras 		return -EINVAL;
1713e05b3b4aSPaul Mackerras 
1714e05b3b4aSPaul Mackerras 	vma->vm_pgoff = offset >> PAGE_SHIFT;
1715e05b3b4aSPaul Mackerras 	vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
1716e05b3b4aSPaul Mackerras 						  vma->vm_page_prot,
1717e05b3b4aSPaul Mackerras 						  mmap_state, write_combine);
1718e05b3b4aSPaul Mackerras 
1719e05b3b4aSPaul Mackerras 	ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1720e05b3b4aSPaul Mackerras 			       vma->vm_end - vma->vm_start, vma->vm_page_prot);
1721e05b3b4aSPaul Mackerras 
1722e05b3b4aSPaul Mackerras 	return ret;
1723e05b3b4aSPaul Mackerras }
1724e05b3b4aSPaul Mackerras 
1725e05b3b4aSPaul Mackerras /* Obsolete functions. Should be removed once the symbios driver
1726e05b3b4aSPaul Mackerras  * is fixed
1727e05b3b4aSPaul Mackerras  */
1728e05b3b4aSPaul Mackerras unsigned long
1729e05b3b4aSPaul Mackerras phys_to_bus(unsigned long pa)
1730e05b3b4aSPaul Mackerras {
1731e05b3b4aSPaul Mackerras 	struct pci_controller *hose;
1732e05b3b4aSPaul Mackerras 	int i;
1733e05b3b4aSPaul Mackerras 
1734e05b3b4aSPaul Mackerras 	for (hose = hose_head; hose; hose = hose->next) {
1735e05b3b4aSPaul Mackerras 		for (i = 0; i < 3; ++i) {
1736e05b3b4aSPaul Mackerras 			if (pa >= hose->mem_resources[i].start
1737e05b3b4aSPaul Mackerras 			    && pa <= hose->mem_resources[i].end) {
1738e05b3b4aSPaul Mackerras 				/*
1739e05b3b4aSPaul Mackerras 				 * XXX the hose->pci_mem_offset really
1740e05b3b4aSPaul Mackerras 				 * only applies to mem_resources[0].
1741e05b3b4aSPaul Mackerras 				 * We need a way to store an offset for
1742e05b3b4aSPaul Mackerras 				 * the others.  -- paulus
1743e05b3b4aSPaul Mackerras 				 */
1744e05b3b4aSPaul Mackerras 				if (i == 0)
1745e05b3b4aSPaul Mackerras 					pa -= hose->pci_mem_offset;
1746e05b3b4aSPaul Mackerras 				return pa;
1747e05b3b4aSPaul Mackerras 			}
1748e05b3b4aSPaul Mackerras 		}
1749e05b3b4aSPaul Mackerras 	}
1750e05b3b4aSPaul Mackerras 	/* hmmm, didn't find it */
1751e05b3b4aSPaul Mackerras 	return 0;
1752e05b3b4aSPaul Mackerras }
1753e05b3b4aSPaul Mackerras 
1754e05b3b4aSPaul Mackerras unsigned long
1755e05b3b4aSPaul Mackerras pci_phys_to_bus(unsigned long pa, int busnr)
1756e05b3b4aSPaul Mackerras {
1757e05b3b4aSPaul Mackerras 	struct pci_controller* hose = pci_bus_to_hose(busnr);
1758e05b3b4aSPaul Mackerras 	if (!hose)
1759e05b3b4aSPaul Mackerras 		return pa;
1760e05b3b4aSPaul Mackerras 	return pa - hose->pci_mem_offset;
1761e05b3b4aSPaul Mackerras }
1762e05b3b4aSPaul Mackerras 
1763e05b3b4aSPaul Mackerras unsigned long
1764e05b3b4aSPaul Mackerras pci_bus_to_phys(unsigned int ba, int busnr)
1765e05b3b4aSPaul Mackerras {
1766e05b3b4aSPaul Mackerras 	struct pci_controller* hose = pci_bus_to_hose(busnr);
1767e05b3b4aSPaul Mackerras 	if (!hose)
1768e05b3b4aSPaul Mackerras 		return ba;
1769e05b3b4aSPaul Mackerras 	return ba + hose->pci_mem_offset;
1770e05b3b4aSPaul Mackerras }
1771e05b3b4aSPaul Mackerras 
1772e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical
1773e05b3b4aSPaul Mackerras  * memory.  Do this on a per-card basis so that we choose the right
1774e05b3b4aSPaul Mackerras  * root bridge.
1775e05b3b4aSPaul Mackerras  * Note that the returned IO or memory base is a physical address
1776e05b3b4aSPaul Mackerras  */
1777e05b3b4aSPaul Mackerras 
1778e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1779e05b3b4aSPaul Mackerras {
1780e05b3b4aSPaul Mackerras 	struct pci_controller* hose;
1781e05b3b4aSPaul Mackerras 	long result = -EOPNOTSUPP;
1782e05b3b4aSPaul Mackerras 
1783e05b3b4aSPaul Mackerras 	/* Argh ! Please forgive me for that hack, but that's the
1784e05b3b4aSPaul Mackerras 	 * simplest way to get existing XFree to not lockup on some
1785e05b3b4aSPaul Mackerras 	 * G5 machines... So when something asks for bus 0 io base
1786e05b3b4aSPaul Mackerras 	 * (bus 0 is HT root), we return the AGP one instead.
1787e05b3b4aSPaul Mackerras 	 */
1788e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_PMAC
1789e8222502SBenjamin Herrenschmidt 	if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
1790e05b3b4aSPaul Mackerras 		if (bus == 0)
1791e05b3b4aSPaul Mackerras 			bus = 0xf0;
1792e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_PMAC */
1793e05b3b4aSPaul Mackerras 
1794e05b3b4aSPaul Mackerras 	hose = pci_bus_to_hose(bus);
1795e05b3b4aSPaul Mackerras 	if (!hose)
1796e05b3b4aSPaul Mackerras 		return -ENODEV;
1797e05b3b4aSPaul Mackerras 
1798e05b3b4aSPaul Mackerras 	switch (which) {
1799e05b3b4aSPaul Mackerras 	case IOBASE_BRIDGE_NUMBER:
1800e05b3b4aSPaul Mackerras 		return (long)hose->first_busno;
1801e05b3b4aSPaul Mackerras 	case IOBASE_MEMORY:
1802e05b3b4aSPaul Mackerras 		return (long)hose->pci_mem_offset;
1803e05b3b4aSPaul Mackerras 	case IOBASE_IO:
1804e05b3b4aSPaul Mackerras 		return (long)hose->io_base_phys;
1805e05b3b4aSPaul Mackerras 	case IOBASE_ISA_IO:
1806e05b3b4aSPaul Mackerras 		return (long)isa_io_base;
1807e05b3b4aSPaul Mackerras 	case IOBASE_ISA_MEM:
1808e05b3b4aSPaul Mackerras 		return (long)isa_mem_base;
1809e05b3b4aSPaul Mackerras 	}
1810e05b3b4aSPaul Mackerras 
1811e05b3b4aSPaul Mackerras 	return result;
1812e05b3b4aSPaul Mackerras }
1813e05b3b4aSPaul Mackerras 
1814e05b3b4aSPaul Mackerras void pci_resource_to_user(const struct pci_dev *dev, int bar,
1815e05b3b4aSPaul Mackerras 			  const struct resource *rsrc,
1816e31dd6e4SGreg Kroah-Hartman 			  resource_size_t *start, resource_size_t *end)
1817e05b3b4aSPaul Mackerras {
1818e05b3b4aSPaul Mackerras 	struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
1819396a1a58SBenjamin Herrenschmidt 	resource_size_t offset = 0;
1820e05b3b4aSPaul Mackerras 
1821e05b3b4aSPaul Mackerras 	if (hose == NULL)
1822e05b3b4aSPaul Mackerras 		return;
1823e05b3b4aSPaul Mackerras 
1824e05b3b4aSPaul Mackerras 	if (rsrc->flags & IORESOURCE_IO)
1825396a1a58SBenjamin Herrenschmidt 		offset = (unsigned long)hose->io_base_virt - _IO_BASE;
1826e05b3b4aSPaul Mackerras 
1827396a1a58SBenjamin Herrenschmidt 	/* We pass a fully fixed up address to userland for MMIO instead of
1828396a1a58SBenjamin Herrenschmidt 	 * a BAR value because X is lame and expects to be able to use that
1829396a1a58SBenjamin Herrenschmidt 	 * to pass to /dev/mem !
1830396a1a58SBenjamin Herrenschmidt 	 *
1831396a1a58SBenjamin Herrenschmidt 	 * That means that we'll have potentially 64 bits values where some
1832396a1a58SBenjamin Herrenschmidt 	 * userland apps only expect 32 (like X itself since it thinks only
1833396a1a58SBenjamin Herrenschmidt 	 * Sparc has 64 bits MMIO) but if we don't do that, we break it on
1834396a1a58SBenjamin Herrenschmidt 	 * 32 bits CHRPs :-(
1835396a1a58SBenjamin Herrenschmidt 	 *
1836396a1a58SBenjamin Herrenschmidt 	 * Hopefully, the sysfs insterface is immune to that gunk. Once X
1837396a1a58SBenjamin Herrenschmidt 	 * has been fixed (and the fix spread enough), we can re-enable the
1838396a1a58SBenjamin Herrenschmidt 	 * 2 lines below and pass down a BAR value to userland. In that case
1839396a1a58SBenjamin Herrenschmidt 	 * we'll also have to re-enable the matching code in
1840396a1a58SBenjamin Herrenschmidt 	 * __pci_mmap_make_offset().
1841396a1a58SBenjamin Herrenschmidt 	 *
1842396a1a58SBenjamin Herrenschmidt 	 * BenH.
1843396a1a58SBenjamin Herrenschmidt 	 */
1844396a1a58SBenjamin Herrenschmidt #if 0
1845396a1a58SBenjamin Herrenschmidt 	else if (rsrc->flags & IORESOURCE_MEM)
1846396a1a58SBenjamin Herrenschmidt 		offset = hose->pci_mem_offset;
1847396a1a58SBenjamin Herrenschmidt #endif
1848396a1a58SBenjamin Herrenschmidt 
1849396a1a58SBenjamin Herrenschmidt 	*start = rsrc->start - offset;
1850396a1a58SBenjamin Herrenschmidt 	*end = rsrc->end - offset;
1851e05b3b4aSPaul Mackerras }
1852e05b3b4aSPaul Mackerras 
1853396a1a58SBenjamin Herrenschmidt void __init pci_init_resource(struct resource *res, resource_size_t start,
1854396a1a58SBenjamin Herrenschmidt 			      resource_size_t end, int flags, char *name)
1855e05b3b4aSPaul Mackerras {
1856e05b3b4aSPaul Mackerras 	res->start = start;
1857e05b3b4aSPaul Mackerras 	res->end = end;
1858e05b3b4aSPaul Mackerras 	res->flags = flags;
1859e05b3b4aSPaul Mackerras 	res->name = name;
1860e05b3b4aSPaul Mackerras 	res->parent = NULL;
1861e05b3b4aSPaul Mackerras 	res->sibling = NULL;
1862e05b3b4aSPaul Mackerras 	res->child = NULL;
1863e05b3b4aSPaul Mackerras }
1864e05b3b4aSPaul Mackerras 
1865e05b3b4aSPaul Mackerras unsigned long pci_address_to_pio(phys_addr_t address)
1866e05b3b4aSPaul Mackerras {
1867e05b3b4aSPaul Mackerras 	struct pci_controller* hose = hose_head;
1868e05b3b4aSPaul Mackerras 
1869e05b3b4aSPaul Mackerras 	for (; hose; hose = hose->next) {
1870e05b3b4aSPaul Mackerras 		unsigned int size = hose->io_resource.end -
1871e05b3b4aSPaul Mackerras 			hose->io_resource.start + 1;
1872e05b3b4aSPaul Mackerras 		if (address >= hose->io_base_phys &&
1873e05b3b4aSPaul Mackerras 		    address < (hose->io_base_phys + size)) {
1874e05b3b4aSPaul Mackerras 			unsigned long base =
1875e05b3b4aSPaul Mackerras 				(unsigned long)hose->io_base_virt - _IO_BASE;
1876e05b3b4aSPaul Mackerras 			return base + (address - hose->io_base_phys);
1877e05b3b4aSPaul Mackerras 		}
1878e05b3b4aSPaul Mackerras 	}
1879e05b3b4aSPaul Mackerras 	return (unsigned int)-1;
1880e05b3b4aSPaul Mackerras }
1881e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_address_to_pio);
1882e05b3b4aSPaul Mackerras 
1883e05b3b4aSPaul Mackerras /*
1884e05b3b4aSPaul Mackerras  * Null PCI config access functions, for the case when we can't
1885e05b3b4aSPaul Mackerras  * find a hose.
1886e05b3b4aSPaul Mackerras  */
1887e05b3b4aSPaul Mackerras #define NULL_PCI_OP(rw, size, type)					\
1888e05b3b4aSPaul Mackerras static int								\
1889e05b3b4aSPaul Mackerras null_##rw##_config_##size(struct pci_dev *dev, int offset, type val)	\
1890e05b3b4aSPaul Mackerras {									\
1891e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;    				\
1892e05b3b4aSPaul Mackerras }
1893e05b3b4aSPaul Mackerras 
1894e05b3b4aSPaul Mackerras static int
1895e05b3b4aSPaul Mackerras null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1896e05b3b4aSPaul Mackerras 		 int len, u32 *val)
1897e05b3b4aSPaul Mackerras {
1898e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;
1899e05b3b4aSPaul Mackerras }
1900e05b3b4aSPaul Mackerras 
1901e05b3b4aSPaul Mackerras static int
1902e05b3b4aSPaul Mackerras null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1903e05b3b4aSPaul Mackerras 		  int len, u32 val)
1904e05b3b4aSPaul Mackerras {
1905e05b3b4aSPaul Mackerras 	return PCIBIOS_DEVICE_NOT_FOUND;
1906e05b3b4aSPaul Mackerras }
1907e05b3b4aSPaul Mackerras 
1908e05b3b4aSPaul Mackerras static struct pci_ops null_pci_ops =
1909e05b3b4aSPaul Mackerras {
1910e05b3b4aSPaul Mackerras 	null_read_config,
1911e05b3b4aSPaul Mackerras 	null_write_config
1912e05b3b4aSPaul Mackerras };
1913e05b3b4aSPaul Mackerras 
1914e05b3b4aSPaul Mackerras /*
1915e05b3b4aSPaul Mackerras  * These functions are used early on before PCI scanning is done
1916e05b3b4aSPaul Mackerras  * and all of the pci_dev and pci_bus structures have been created.
1917e05b3b4aSPaul Mackerras  */
1918e05b3b4aSPaul Mackerras static struct pci_bus *
1919e05b3b4aSPaul Mackerras fake_pci_bus(struct pci_controller *hose, int busnr)
1920e05b3b4aSPaul Mackerras {
1921e05b3b4aSPaul Mackerras 	static struct pci_bus bus;
1922e05b3b4aSPaul Mackerras 
1923e05b3b4aSPaul Mackerras 	if (hose == 0) {
1924e05b3b4aSPaul Mackerras 		hose = pci_bus_to_hose(busnr);
1925e05b3b4aSPaul Mackerras 		if (hose == 0)
1926e05b3b4aSPaul Mackerras 			printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1927e05b3b4aSPaul Mackerras 	}
1928e05b3b4aSPaul Mackerras 	bus.number = busnr;
1929e05b3b4aSPaul Mackerras 	bus.sysdata = hose;
1930e05b3b4aSPaul Mackerras 	bus.ops = hose? hose->ops: &null_pci_ops;
1931e05b3b4aSPaul Mackerras 	return &bus;
1932e05b3b4aSPaul Mackerras }
1933e05b3b4aSPaul Mackerras 
1934e05b3b4aSPaul Mackerras #define EARLY_PCI_OP(rw, size, type)					\
1935e05b3b4aSPaul Mackerras int early_##rw##_config_##size(struct pci_controller *hose, int bus,	\
1936e05b3b4aSPaul Mackerras 			       int devfn, int offset, type value)	\
1937e05b3b4aSPaul Mackerras {									\
1938e05b3b4aSPaul Mackerras 	return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus),	\
1939e05b3b4aSPaul Mackerras 					    devfn, offset, value);	\
1940e05b3b4aSPaul Mackerras }
1941e05b3b4aSPaul Mackerras 
1942e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, byte, u8 *)
1943e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, word, u16 *)
1944e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, dword, u32 *)
1945e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, byte, u8)
1946e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, word, u16)
1947e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, dword, u32)
1948