blob: 1020d04b0a5afc283bdcf8e6ebc72f550bdc71b8 [file] [log] [blame]
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001/*
2 * Common pmac/prep/chrp pci routines. -- Cort
3 */
4
Paul Mackerrase05b3b42006-01-15 22:05:47 +11005#include <linux/kernel.h>
6#include <linux/pci.h>
7#include <linux/delay.h>
8#include <linux/string.h>
9#include <linux/init.h>
10#include <linux/capability.h>
11#include <linux/sched.h>
12#include <linux/errno.h>
13#include <linux/bootmem.h>
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -070014#include <linux/irq.h>
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +110015#include <linux/list.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110016
17#include <asm/processor.h>
18#include <asm/io.h>
19#include <asm/prom.h>
20#include <asm/sections.h>
21#include <asm/pci-bridge.h>
22#include <asm/byteorder.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110023#include <asm/uaccess.h>
24#include <asm/machdep.h>
25
26#undef DEBUG
27
28#ifdef DEBUG
29#define DBG(x...) printk(x)
30#else
31#define DBG(x...)
32#endif
33
34unsigned long isa_io_base = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110035unsigned long pci_dram_offset = 0;
36int pcibios_assign_bus_offset = 1;
37
38void pcibios_make_OF_bus_map(void);
39
Paul Mackerrase05b3b42006-01-15 22:05:47 +110040static void pcibios_fixup_resources(struct pci_dev* dev);
41static void fixup_broken_pcnet32(struct pci_dev* dev);
42static int reparent_resources(struct resource *parent, struct resource *res);
43static void fixup_cpc710_pci64(struct pci_dev* dev);
44#ifdef CONFIG_PPC_OF
45static u8* pci_to_OF_bus_map;
46#endif
47
48/* By default, we don't re-assign bus numbers. We do this only on
49 * some pmacs
50 */
51int pci_assign_all_buses;
52
Kumar Galaa4c9e322007-06-27 13:09:43 -050053LIST_HEAD(hose_list);
Paul Mackerrase05b3b42006-01-15 22:05:47 +110054
55static int pci_bus_count;
56
57static void
Kumar Gala2052d6d2007-07-25 00:44:11 -050058fixup_hide_host_resource_fsl(struct pci_dev* dev)
59{
60 int i, class = dev->class >> 8;
61
62 if ((class == PCI_CLASS_PROCESSOR_POWERPC) &&
63 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
64 (dev->bus->parent == NULL)) {
65 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
66 dev->resource[i].start = 0;
67 dev->resource[i].end = 0;
68 dev->resource[i].flags = 0;
69 }
70 }
71}
72DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
73DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
74
75static void
Paul Mackerrase05b3b42006-01-15 22:05:47 +110076fixup_broken_pcnet32(struct pci_dev* dev)
77{
78 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
79 dev->vendor = PCI_VENDOR_ID_AMD;
80 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
81 }
82}
83DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
84
85static void
86fixup_cpc710_pci64(struct pci_dev* dev)
87{
88 /* Hide the PCI64 BARs from the kernel as their content doesn't
89 * fit well in the resource management
90 */
91 dev->resource[0].start = dev->resource[0].end = 0;
92 dev->resource[0].flags = 0;
93 dev->resource[1].start = dev->resource[1].end = 0;
94 dev->resource[1].flags = 0;
95}
96DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
97
98static void
99pcibios_fixup_resources(struct pci_dev *dev)
100{
101 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
102 int i;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100103 resource_size_t offset, mask;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100104
105 if (!hose) {
106 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
107 return;
108 }
109 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
110 struct resource *res = dev->resource + i;
111 if (!res->flags)
112 continue;
113 if (res->end == 0xffffffff) {
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700114 DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300115 pci_name(dev), i, (u64)res->start, (u64)res->end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100116 res->end -= res->start;
117 res->start = 0;
118 res->flags |= IORESOURCE_UNSET;
119 continue;
120 }
121 offset = 0;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100122 mask = (resource_size_t)-1;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100123 if (res->flags & IORESOURCE_MEM) {
124 offset = hose->pci_mem_offset;
125 } else if (res->flags & IORESOURCE_IO) {
126 offset = (unsigned long) hose->io_base_virt
127 - isa_io_base;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100128 mask = 0xffffffffu;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100129 }
130 if (offset != 0) {
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100131 res->start = (res->start + offset) & mask;
132 res->end = (res->end + offset) & mask;
Benjamin Herrenschmidt0ec6b5c2007-12-20 14:54:43 +1100133 DBG("PCI: Fixup res %d (0x%lx) of dev %s: %llx -> %llx\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300134 i, res->flags, pci_name(dev),
135 (u64)res->start - offset, (u64)res->start);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100136 }
137 }
138
139 /* Call machine specific resource fixup */
140 if (ppc_md.pcibios_fixup_resources)
141 ppc_md.pcibios_fixup_resources(dev);
142}
143DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
144
145void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
146 struct resource *res)
147{
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100148 resource_size_t offset = 0, mask = (resource_size_t)-1;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100149 struct pci_controller *hose = dev->sysdata;
150
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100151 if (hose && res->flags & IORESOURCE_IO) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100152 offset = (unsigned long)hose->io_base_virt - isa_io_base;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100153 mask = 0xffffffffu;
154 } else if (hose && res->flags & IORESOURCE_MEM)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100155 offset = hose->pci_mem_offset;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100156 region->start = (res->start - offset) & mask;
157 region->end = (res->end - offset) & mask;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100158}
159EXPORT_SYMBOL(pcibios_resource_to_bus);
160
161void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
162 struct pci_bus_region *region)
163{
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100164 resource_size_t offset = 0, mask = (resource_size_t)-1;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100165 struct pci_controller *hose = dev->sysdata;
166
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100167 if (hose && res->flags & IORESOURCE_IO) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100168 offset = (unsigned long)hose->io_base_virt - isa_io_base;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100169 mask = 0xffffffffu;
170 } else if (hose && res->flags & IORESOURCE_MEM)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100171 offset = hose->pci_mem_offset;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100172 res->start = (region->start + offset) & mask;
173 res->end = (region->end + offset) & mask;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100174}
175EXPORT_SYMBOL(pcibios_bus_to_resource);
176
177/*
178 * We need to avoid collisions with `mirrored' VGA ports
179 * and other strange ISA hardware, so we always want the
180 * addresses to be allocated in the 0x000-0x0ff region
181 * modulo 0x400.
182 *
183 * Why? Because some silly external IO cards only decode
184 * the low 10 bits of the IO address. The 0x00-0xff region
185 * is reserved for motherboard devices that decode all 16
186 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
187 * but we want to try to avoid allocating at 0x2900-0x2bff
188 * which might have be mirrored at 0x0100-0x03ff..
189 */
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700190void pcibios_align_resource(void *data, struct resource *res,
191 resource_size_t size, resource_size_t align)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100192{
193 struct pci_dev *dev = data;
194
195 if (res->flags & IORESOURCE_IO) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700196 resource_size_t start = res->start;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100197
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100198 if (start & 0x300) {
199 start = (start + 0x3ff) & ~0x3ff;
200 res->start = start;
201 }
202 }
203}
204EXPORT_SYMBOL(pcibios_align_resource);
205
206/*
207 * Handle resources of PCI devices. If the world were perfect, we could
208 * just allocate all the resource regions and do nothing more. It isn't.
209 * On the other hand, we cannot just re-allocate all devices, as it would
210 * require us to know lots of host bridge internals. So we attempt to
211 * keep as much of the original configuration as possible, but tweak it
212 * when it's found to be wrong.
213 *
214 * Known BIOS problems we have to work around:
215 * - I/O or memory regions not configured
216 * - regions configured, but not enabled in the command register
217 * - bogus I/O addresses above 64K used
218 * - expansion ROMs left enabled (this may sound harmless, but given
219 * the fact the PCI specs explicitly allow address decoders to be
220 * shared between expansion ROMs and other resource regions, it's
221 * at least dangerous)
222 *
223 * Our solution:
224 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
225 * This gives us fixed barriers on where we can allocate.
226 * (2) Allocate resources for all enabled devices. If there is
227 * a collision, just mark the resource as unallocated. Also
228 * disable expansion ROMs during this step.
229 * (3) Try to allocate resources for disabled devices. If the
230 * resources were assigned correctly, everything goes well,
231 * if they weren't, they won't disturb allocation of other
232 * resources.
233 * (4) Assign new addresses to resources which were either
234 * not configured at all or misconfigured. If explicitly
235 * requested by the user, configure expansion ROM address
236 * as well.
237 */
238
239static void __init
240pcibios_allocate_bus_resources(struct list_head *bus_list)
241{
242 struct pci_bus *bus;
243 int i;
244 struct resource *res, *pr;
245
246 /* Depth-First Search on bus tree */
247 list_for_each_entry(bus, bus_list, node) {
248 for (i = 0; i < 4; ++i) {
249 if ((res = bus->resource[i]) == NULL || !res->flags
250 || res->start > res->end)
251 continue;
252 if (bus->parent == NULL)
253 pr = (res->flags & IORESOURCE_IO)?
254 &ioport_resource: &iomem_resource;
255 else {
256 pr = pci_find_parent_resource(bus->self, res);
257 if (pr == res) {
258 /* this happens when the generic PCI
259 * code (wrongly) decides that this
260 * bridge is transparent -- paulus
261 */
262 continue;
263 }
264 }
265
Benjamin Herrenschmidt0ec6b5c2007-12-20 14:54:43 +1100266 DBG("PCI: dev %s (bus 0x%02x) bridge rsrc %d: %016llx..%016llx "
267 "(f:0x%08lx), parent %p\n",
268 bus->self ? pci_name(bus->self) : "PHB", bus->number, i,
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300269 (u64)res->start, (u64)res->end, res->flags, pr);
Benjamin Herrenschmidt0ec6b5c2007-12-20 14:54:43 +1100270
271 if (pr && !(pr->flags & IORESOURCE_UNSET)) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100272 if (request_resource(pr, res) == 0)
273 continue;
274 /*
275 * Must be a conflict with an existing entry.
276 * Move that entry (or entries) under the
277 * bridge resource and try again.
278 */
279 if (reparent_resources(pr, res) == 0)
280 continue;
281 }
Benjamin Herrenschmidt0ec6b5c2007-12-20 14:54:43 +1100282 printk(KERN_WARNING
283 "PCI: Cannot allocate resource region "
284 "%d of PCI bridge %d, will remap\n",
285 i, bus->number);
286 res->flags |= IORESOURCE_UNSET;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100287 }
288 pcibios_allocate_bus_resources(&bus->children);
289 }
290}
291
292/*
293 * Reparent resource children of pr that conflict with res
294 * under res, and make res replace those children.
295 */
296static int __init
297reparent_resources(struct resource *parent, struct resource *res)
298{
299 struct resource *p, **pp;
300 struct resource **firstpp = NULL;
301
302 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
303 if (p->end < res->start)
304 continue;
305 if (res->end < p->start)
306 break;
307 if (p->start < res->start || p->end > res->end)
308 return -1; /* not completely contained */
309 if (firstpp == NULL)
310 firstpp = pp;
311 }
312 if (firstpp == NULL)
313 return -1; /* didn't find any conflicting entries? */
314 res->parent = parent;
315 res->child = *firstpp;
316 res->sibling = *pp;
317 *firstpp = res;
318 *pp = NULL;
319 for (p = res->child; p != NULL; p = p->sibling) {
320 p->parent = res;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700321 DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300322 p->name, (u64)p->start, (u64)p->end, res->name);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100323 }
324 return 0;
325}
326
Kumar Galaf64fddb2007-07-20 13:35:34 -0500327void __init
328update_bridge_resource(struct pci_dev *dev, struct resource *res)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100329{
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100330 u8 io_base_lo, io_limit_lo;
331 u16 mem_base, mem_limit;
332 u16 cmd;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100333 resource_size_t start, end, off;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100334 struct pci_controller *hose = dev->sysdata;
335
336 if (!hose) {
337 printk("update_bridge_base: no hose?\n");
338 return;
339 }
340 pci_read_config_word(dev, PCI_COMMAND, &cmd);
341 pci_write_config_word(dev, PCI_COMMAND,
342 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
343 if (res->flags & IORESOURCE_IO) {
344 off = (unsigned long) hose->io_base_virt - isa_io_base;
345 start = res->start - off;
346 end = res->end - off;
347 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
348 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
Randy Vinson60b2a462006-10-12 13:36:23 -0700349 if (end > 0xffff)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100350 io_base_lo |= PCI_IO_RANGE_TYPE_32;
Randy Vinson60b2a462006-10-12 13:36:23 -0700351 else
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100352 io_base_lo |= PCI_IO_RANGE_TYPE_16;
Randy Vinson60b2a462006-10-12 13:36:23 -0700353 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
354 start >> 16);
355 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
356 end >> 16);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100357 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
358 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
359
360 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
361 == IORESOURCE_MEM) {
362 off = hose->pci_mem_offset;
363 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
364 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
365 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
366 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
367
368 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
369 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
370 off = hose->pci_mem_offset;
371 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
372 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
373 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
374 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
375
376 } else {
Kumar Galaf64fddb2007-07-20 13:35:34 -0500377 DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
378 pci_name(dev), res->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100379 }
380 pci_write_config_word(dev, PCI_COMMAND, cmd);
381}
382
383static inline void alloc_resource(struct pci_dev *dev, int idx)
384{
385 struct resource *pr, *r = &dev->resource[idx];
386
Benjamin Herrenschmidt0ec6b5c2007-12-20 14:54:43 +1100387 DBG("PCI: Allocating %s: Resource %d: %016llx..%016llx (f=%lx)\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300388 pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100389 pr = pci_find_parent_resource(dev, r);
Benjamin Herrenschmidt0ec6b5c2007-12-20 14:54:43 +1100390 if (!pr || (pr->flags & IORESOURCE_UNSET) || request_resource(pr, r) < 0) {
391 printk(KERN_WARNING "PCI: Cannot allocate resource region %d"
392 " of device %s, will remap\n", idx, pci_name(dev));
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100393 if (pr)
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700394 DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300395 pr, (u64)pr->start, (u64)pr->end, pr->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100396 /* We'll assign a new address later */
397 r->flags |= IORESOURCE_UNSET;
398 r->end -= r->start;
399 r->start = 0;
400 }
401}
402
403static void __init
404pcibios_allocate_resources(int pass)
405{
406 struct pci_dev *dev = NULL;
407 int idx, disabled;
408 u16 command;
409 struct resource *r;
410
411 for_each_pci_dev(dev) {
412 pci_read_config_word(dev, PCI_COMMAND, &command);
413 for (idx = 0; idx < 6; idx++) {
414 r = &dev->resource[idx];
415 if (r->parent) /* Already allocated */
416 continue;
417 if (!r->flags || (r->flags & IORESOURCE_UNSET))
418 continue; /* Not assigned at all */
419 if (r->flags & IORESOURCE_IO)
420 disabled = !(command & PCI_COMMAND_IO);
421 else
422 disabled = !(command & PCI_COMMAND_MEMORY);
423 if (pass == disabled)
424 alloc_resource(dev, idx);
425 }
426 if (pass)
427 continue;
428 r = &dev->resource[PCI_ROM_RESOURCE];
429 if (r->flags & IORESOURCE_ROM_ENABLE) {
430 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
431 u32 reg;
432 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
433 r->flags &= ~IORESOURCE_ROM_ENABLE;
434 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
435 pci_write_config_dword(dev, dev->rom_base_reg,
436 reg & ~PCI_ROM_ADDRESS_ENABLE);
437 }
438 }
439}
440
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100441#ifdef CONFIG_PPC_OF
442/*
443 * Functions below are used on OpenFirmware machines.
444 */
445static void
446make_one_node_map(struct device_node* node, u8 pci_bus)
447{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000448 const int *bus_range;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100449 int len;
450
451 if (pci_bus >= pci_bus_count)
452 return;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000453 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100454 if (bus_range == NULL || len < 2 * sizeof(int)) {
455 printk(KERN_WARNING "Can't get bus-range for %s, "
456 "assuming it starts at 0\n", node->full_name);
457 pci_to_OF_bus_map[pci_bus] = 0;
458 } else
459 pci_to_OF_bus_map[pci_bus] = bus_range[0];
460
461 for (node=node->child; node != 0;node = node->sibling) {
462 struct pci_dev* dev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000463 const unsigned int *class_code, *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100464
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000465 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100466 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
467 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
468 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000469 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100470 if (!reg)
471 continue;
Alan Coxab462762007-04-23 14:47:59 +0100472 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
473 if (!dev || !dev->subordinate) {
474 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100475 continue;
Alan Coxab462762007-04-23 14:47:59 +0100476 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100477 make_one_node_map(node, dev->subordinate->number);
Alan Coxab462762007-04-23 14:47:59 +0100478 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100479 }
480}
481
482void
483pcibios_make_OF_bus_map(void)
484{
485 int i;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500486 struct pci_controller *hose, *tmp;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000487 struct property *map_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000488 struct device_node *dn;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100489
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800490 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100491 if (!pci_to_OF_bus_map) {
492 printk(KERN_ERR "Can't allocate OF bus map !\n");
493 return;
494 }
495
496 /* We fill the bus map with invalid values, that helps
497 * debugging.
498 */
499 for (i=0; i<pci_bus_count; i++)
500 pci_to_OF_bus_map[i] = 0xff;
501
502 /* For each hose, we begin searching bridges */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500503 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100504 struct device_node* node = hose->dn;
505
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100506 if (!node)
507 continue;
508 make_one_node_map(node, hose->first_busno);
509 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000510 dn = of_find_node_by_path("/");
511 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000512 if (map_prop) {
513 BUG_ON(pci_bus_count > map_prop->length);
514 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
515 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000516 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100517#ifdef DEBUG
518 printk("PCI->OF bus map:\n");
519 for (i=0; i<pci_bus_count; i++) {
520 if (pci_to_OF_bus_map[i] == 0xff)
521 continue;
522 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
523 }
524#endif
525}
526
527typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
528
529static struct device_node*
530scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
531{
532 struct device_node* sub_node;
533
534 for (; node != 0;node = node->sibling) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000535 const unsigned int *class_code;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100536
537 if (filter(node, data))
538 return node;
539
540 /* For PCI<->PCI bridges or CardBus bridges, we go down
541 * Note: some OFs create a parent node "multifunc-device" as
542 * a fake root for all functions of a multi-function device,
543 * we go down them as well.
544 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000545 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100546 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
547 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
548 strcmp(node->name, "multifunc-device"))
549 continue;
550 sub_node = scan_OF_pci_childs(node->child, filter, data);
551 if (sub_node)
552 return sub_node;
553 }
554 return NULL;
555}
556
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100557static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
558 unsigned int devfn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100559{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100560 struct device_node *np = NULL;
561 const u32 *reg;
562 unsigned int psize;
563
564 while ((np = of_get_next_child(parent, np)) != NULL) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000565 reg = of_get_property(np, "reg", &psize);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100566 if (reg == NULL || psize < 4)
567 continue;
568 if (((reg[0] >> 8) & 0xff) == devfn)
569 return np;
570 }
571 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100572}
573
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100574
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100575static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
576{
577 struct device_node *parent, *np;
578
579 /* Are we a root bus ? */
580 if (bus->self == NULL || bus->parent == NULL) {
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500581 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100582 if (hose == NULL)
583 return NULL;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100584 return of_node_get(hose->dn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100585 }
586
587 /* not a root bus, we need to get our parent */
588 parent = scan_OF_for_pci_bus(bus->parent);
589 if (parent == NULL)
590 return NULL;
591
592 /* now iterate for children for a match */
593 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
594 of_node_put(parent);
595
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100596 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100597}
598
599/*
600 * Scans the OF tree for a device node matching a PCI device
601 */
602struct device_node *
603pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
604{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100605 struct device_node *parent, *np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100606
607 if (!have_of)
608 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100609
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100610 DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
611 parent = scan_OF_for_pci_bus(bus);
612 if (parent == NULL)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100613 return NULL;
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100614 DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
615 np = scan_OF_for_pci_dev(parent, devfn);
616 of_node_put(parent);
617 DBG(" result is %s\n", np ? np->full_name : "<NULL>");
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100618
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100619 /* XXX most callers don't release the returned node
620 * mostly because ppc64 doesn't increase the refcount,
621 * we need to fix that.
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100622 */
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100623 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100624}
625EXPORT_SYMBOL(pci_busdev_to_OF_node);
626
627struct device_node*
628pci_device_to_OF_node(struct pci_dev *dev)
629{
630 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
631}
632EXPORT_SYMBOL(pci_device_to_OF_node);
633
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100634static int
635find_OF_pci_device_filter(struct device_node* node, void* data)
636{
637 return ((void *)node == data);
638}
639
640/*
641 * Returns the PCI device matching a given OF node
642 */
643int
644pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
645{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000646 const unsigned int *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100647 struct pci_controller* hose;
648 struct pci_dev* dev = NULL;
649
650 if (!have_of)
651 return -ENODEV;
652 /* Make sure it's really a PCI device */
653 hose = pci_find_hose_for_OF_device(node);
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100654 if (!hose || !hose->dn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100655 return -ENODEV;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100656 if (!scan_OF_pci_childs(hose->dn->child,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100657 find_OF_pci_device_filter, (void *)node))
658 return -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000659 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100660 if (!reg)
661 return -ENODEV;
662 *bus = (reg[0] >> 16) & 0xff;
663 *devfn = ((reg[0] >> 8) & 0xff);
664
665 /* Ok, here we need some tweak. If we have already renumbered
666 * all busses, we can't rely on the OF bus number any more.
667 * the pci_to_OF_bus_map is not enough as several PCI busses
668 * may match the same OF bus number.
669 */
670 if (!pci_to_OF_bus_map)
671 return 0;
672
673 for_each_pci_dev(dev)
674 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
675 dev->devfn == *devfn) {
676 *bus = dev->bus->number;
677 pci_dev_put(dev);
678 return 0;
679 }
680
681 return -ENODEV;
682}
683EXPORT_SYMBOL(pci_device_from_OF_node);
684
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100685/* We create the "pci-OF-bus-map" property now so it appears in the
686 * /proc device tree
687 */
688void __init
689pci_create_OF_bus_map(void)
690{
691 struct property* of_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000692 struct device_node *dn;
693
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100694 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000695 if (!of_prop)
696 return;
697 dn = of_find_node_by_path("/");
698 if (dn) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100699 memset(of_prop, -1, sizeof(struct property) + 256);
700 of_prop->name = "pci-OF-bus-map";
701 of_prop->length = 256;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000702 of_prop->value = &of_prop[1];
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000703 prom_add_property(dn, of_prop);
704 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100705 }
706}
707
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100708#else /* CONFIG_PPC_OF */
709void pcibios_make_OF_bus_map(void)
710{
711}
712#endif /* CONFIG_PPC_OF */
713
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100714#ifdef CONFIG_PPC_PMAC
715/*
716 * This set of routines checks for PCI<->PCI bridges that have closed
717 * IO resources and have child devices. It tries to re-open an IO
718 * window on them.
719 *
720 * This is a _temporary_ fix to workaround a problem with Apple's OF
721 * closing IO windows on P2P bridges when the OF drivers of cards
722 * below this bridge don't claim any IO range (typically ATI or
723 * Adaptec).
724 *
725 * A more complete fix would be to use drivers/pci/setup-bus.c, which
726 * involves a working pcibios_fixup_pbus_ranges(), some more care about
727 * ordering when creating the host bus resources, and maybe a few more
728 * minor tweaks
729 */
730
731/* Initialize bridges with base/limit values we have collected */
732static void __init
733do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
734{
735 struct pci_dev *bridge = bus->self;
736 struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
737 u32 l;
738 u16 w;
739 struct resource res;
740
741 if (bus->resource[0] == NULL)
742 return;
743 res = *(bus->resource[0]);
744
745 DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
746 res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
747 res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700748 DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100749
750 /* Set up the top and bottom of the PCI I/O segment for this bus. */
751 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
752 l &= 0xffff000f;
753 l |= (res.start >> 8) & 0x00f0;
754 l |= res.end & 0xf000;
755 pci_write_config_dword(bridge, PCI_IO_BASE, l);
756
757 if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
758 l = (res.start >> 16) | (res.end & 0xffff0000);
759 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
760 }
761
762 pci_read_config_word(bridge, PCI_COMMAND, &w);
763 w |= PCI_COMMAND_IO;
764 pci_write_config_word(bridge, PCI_COMMAND, w);
765
766#if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
767 if (enable_vga) {
768 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
769 w |= PCI_BRIDGE_CTL_VGA;
770 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
771 }
772#endif
773}
774
775/* This function is pretty basic and actually quite broken for the
776 * general case, it's enough for us right now though. It's supposed
777 * to tell us if we need to open an IO range at all or not and what
778 * size.
779 */
780static int __init
781check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
782{
783 struct pci_dev *dev;
784 int i;
785 int rc = 0;
786
Paul Mackerras0f582bc2006-06-15 18:03:32 +1000787#define push_end(res, mask) do { \
788 BUG_ON((mask+1) & mask); \
789 res->end = (res->end + mask) | mask; \
790} while (0)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100791
792 list_for_each_entry(dev, &bus->devices, bus_list) {
793 u16 class = dev->class >> 8;
794
795 if (class == PCI_CLASS_DISPLAY_VGA ||
796 class == PCI_CLASS_NOT_DEFINED_VGA)
797 *found_vga = 1;
798 if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
799 rc |= check_for_io_childs(dev->subordinate, res, found_vga);
800 if (class == PCI_CLASS_BRIDGE_CARDBUS)
801 push_end(res, 0xfff);
802
803 for (i=0; i<PCI_NUM_RESOURCES; i++) {
804 struct resource *r;
805 unsigned long r_size;
806
807 if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
808 && i >= PCI_BRIDGE_RESOURCES)
809 continue;
810 r = &dev->resource[i];
811 r_size = r->end - r->start;
812 if (r_size < 0xfff)
813 r_size = 0xfff;
814 if (r->flags & IORESOURCE_IO && (r_size) != 0) {
815 rc = 1;
816 push_end(res, r_size);
817 }
818 }
819 }
820
821 return rc;
822}
823
824/* Here we scan all P2P bridges of a given level that have a closed
825 * IO window. Note that the test for the presence of a VGA card should
826 * be improved to take into account already configured P2P bridges,
827 * currently, we don't see them and might end up configuring 2 bridges
828 * with VGA pass through enabled
829 */
830static void __init
831do_fixup_p2p_level(struct pci_bus *bus)
832{
833 struct pci_bus *b;
834 int i, parent_io;
835 int has_vga = 0;
836
837 for (parent_io=0; parent_io<4; parent_io++)
838 if (bus->resource[parent_io]
839 && bus->resource[parent_io]->flags & IORESOURCE_IO)
840 break;
841 if (parent_io >= 4)
842 return;
843
844 list_for_each_entry(b, &bus->children, node) {
845 struct pci_dev *d = b->self;
846 struct pci_controller* hose = (struct pci_controller *)d->sysdata;
847 struct resource *res = b->resource[0];
848 struct resource tmp_res;
849 unsigned long max;
850 int found_vga = 0;
851
852 memset(&tmp_res, 0, sizeof(tmp_res));
853 tmp_res.start = bus->resource[parent_io]->start;
854
855 /* We don't let low addresses go through that closed P2P bridge, well,
856 * that may not be necessary but I feel safer that way
857 */
858 if (tmp_res.start == 0)
859 tmp_res.start = 0x1000;
860
861 if (!list_empty(&b->devices) && res && res->flags == 0 &&
862 res != bus->resource[parent_io] &&
863 (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
864 check_for_io_childs(b, &tmp_res, &found_vga)) {
865 u8 io_base_lo;
866
867 printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
868
869 if (found_vga) {
870 if (has_vga) {
871 printk(KERN_WARNING "Skipping VGA, already active"
872 " on bus segment\n");
873 found_vga = 0;
874 } else
875 has_vga = 1;
876 }
877 pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
878
879 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
880 max = ((unsigned long) hose->io_base_virt
881 - isa_io_base) + 0xffffffff;
882 else
883 max = ((unsigned long) hose->io_base_virt
884 - isa_io_base) + 0xffff;
885
886 *res = tmp_res;
887 res->flags = IORESOURCE_IO;
888 res->name = b->name;
889
890 /* Find a resource in the parent where we can allocate */
891 for (i = 0 ; i < 4; i++) {
892 struct resource *r = bus->resource[i];
893 if (!r)
894 continue;
895 if ((r->flags & IORESOURCE_IO) == 0)
896 continue;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700897 DBG("Trying to allocate from %016llx, size %016llx from parent"
898 " res %d: %016llx -> %016llx\n",
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100899 res->start, res->end, i, r->start, r->end);
900
901 if (allocate_resource(r, res, res->end + 1, res->start, max,
902 res->end + 1, NULL, NULL) < 0) {
903 DBG("Failed !\n");
904 continue;
905 }
906 do_update_p2p_io_resource(b, found_vga);
907 break;
908 }
909 }
910 do_fixup_p2p_level(b);
911 }
912}
913
914static void
915pcibios_fixup_p2p_bridges(void)
916{
917 struct pci_bus *b;
918
919 list_for_each_entry(b, &pci_root_buses, node)
920 do_fixup_p2p_level(b);
921}
922
923#endif /* CONFIG_PPC_PMAC */
924
925static int __init
926pcibios_init(void)
927{
Kumar Galaa4c9e322007-06-27 13:09:43 -0500928 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100929 struct pci_bus *bus;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500930 int next_busno = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100931
932 printk(KERN_INFO "PCI: Probing PCI hardware\n");
933
934 /* Scan all of the recorded PCI controllers. */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500935 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100936 if (pci_assign_all_buses)
937 hose->first_busno = next_busno;
938 hose->last_busno = 0xff;
Benjamin Herrenschmidt803d4572006-11-11 17:25:07 +1100939 bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
940 hose->ops, hose);
941 if (bus)
942 pci_bus_add_devices(bus);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100943 hose->last_busno = bus->subordinate;
944 if (pci_assign_all_buses || next_busno <= hose->last_busno)
945 next_busno = hose->last_busno + pcibios_assign_bus_offset;
946 }
947 pci_bus_count = next_busno;
948
949 /* OpenFirmware based machines need a map of OF bus
950 * numbers vs. kernel bus numbers since we may have to
951 * remap them.
952 */
953 if (pci_assign_all_buses && have_of)
954 pcibios_make_OF_bus_map();
955
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100956 /* Call machine dependent fixup */
957 if (ppc_md.pcibios_fixup)
958 ppc_md.pcibios_fixup();
959
960 /* Allocate and assign resources */
961 pcibios_allocate_bus_resources(&pci_root_buses);
962 pcibios_allocate_resources(0);
963 pcibios_allocate_resources(1);
964#ifdef CONFIG_PPC_PMAC
965 pcibios_fixup_p2p_bridges();
966#endif /* CONFIG_PPC_PMAC */
Benjamin Herrenschmidt0ec6b5c2007-12-20 14:54:43 +1100967 DBG("PCI: Assigning unassigned resouces...\n");
968 pci_assign_unassigned_resources();
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100969
970 /* Call machine dependent post-init code */
971 if (ppc_md.pcibios_after_init)
972 ppc_md.pcibios_after_init();
973
974 return 0;
975}
976
977subsys_initcall(pcibios_init);
978
Kumar Gala282045b2007-07-26 00:16:05 -0500979void pcibios_fixup_bus(struct pci_bus *bus)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100980{
981 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
982 unsigned long io_offset;
983 struct resource *res;
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +1100984 struct pci_dev *dev;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100985 int i;
986
987 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
988 if (bus->parent == NULL) {
989 /* This is a host bridge - fill in its resources */
990 hose->bus = bus;
991
992 bus->resource[0] = res = &hose->io_resource;
993 if (!res->flags) {
994 if (io_offset)
995 printk(KERN_ERR "I/O resource not set for host"
Kumar Gala5516b542007-06-27 01:17:57 -0500996 " bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100997 res->start = 0;
998 res->end = IO_SPACE_LIMIT;
999 res->flags = IORESOURCE_IO;
1000 }
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +11001001 res->start = (res->start + io_offset) & 0xffffffffu;
1002 res->end = (res->end + io_offset) & 0xffffffffu;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001003
1004 for (i = 0; i < 3; ++i) {
1005 res = &hose->mem_resources[i];
1006 if (!res->flags) {
1007 if (i > 0)
1008 continue;
1009 printk(KERN_ERR "Memory resource not set for "
Kumar Gala5516b542007-06-27 01:17:57 -05001010 "host bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001011 res->start = hose->pci_mem_offset;
1012 res->end = ~0U;
1013 res->flags = IORESOURCE_MEM;
1014 }
1015 bus->resource[i+1] = res;
1016 }
1017 } else {
1018 /* This is a subordinate bridge */
1019 pci_read_bridge_bases(bus);
1020
1021 for (i = 0; i < 4; ++i) {
1022 if ((res = bus->resource[i]) == NULL)
1023 continue;
York Sun6d8ff102007-06-04 11:56:42 -05001024 if (!res->flags || bus->self->transparent)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001025 continue;
1026 if (io_offset && (res->flags & IORESOURCE_IO)) {
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +11001027 res->start = (res->start + io_offset) &
1028 0xffffffffu;
1029 res->end = (res->end + io_offset) &
1030 0xffffffffu;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001031 } else if (hose->pci_mem_offset
1032 && (res->flags & IORESOURCE_MEM)) {
1033 res->start += hose->pci_mem_offset;
1034 res->end += hose->pci_mem_offset;
1035 }
1036 }
1037 }
1038
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001039 /* Platform specific bus fixups */
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001040 if (ppc_md.pcibios_fixup_bus)
1041 ppc_md.pcibios_fixup_bus(bus);
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001042
1043 /* Read default IRQs and fixup if necessary */
1044 list_for_each_entry(dev, &bus->devices, bus_list) {
1045 pci_read_irq_line(dev);
1046 if (ppc_md.pci_irq_fixup)
1047 ppc_md.pci_irq_fixup(dev);
1048 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001049}
1050
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001051/* the next one is stolen from the alpha port... */
1052void __init
1053pcibios_update_irq(struct pci_dev *dev, int irq)
1054{
1055 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1056 /* XXX FIXME - update OF device tree node interrupt property */
1057}
1058
1059int pcibios_enable_device(struct pci_dev *dev, int mask)
1060{
1061 u16 cmd, old_cmd;
1062 int idx;
1063 struct resource *r;
1064
1065 if (ppc_md.pcibios_enable_device_hook)
1066 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1067 return -EINVAL;
1068
1069 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1070 old_cmd = cmd;
1071 for (idx=0; idx<6; idx++) {
1072 r = &dev->resource[idx];
1073 if (r->flags & IORESOURCE_UNSET) {
1074 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1075 return -EINVAL;
1076 }
1077 if (r->flags & IORESOURCE_IO)
1078 cmd |= PCI_COMMAND_IO;
1079 if (r->flags & IORESOURCE_MEM)
1080 cmd |= PCI_COMMAND_MEMORY;
1081 }
1082 if (cmd != old_cmd) {
1083 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1084 pci_name(dev), old_cmd, cmd);
1085 pci_write_config_word(dev, PCI_COMMAND, cmd);
1086 }
1087 return 0;
1088}
1089
Kumar Gala0b1d40c2007-06-27 10:27:33 -05001090static struct pci_controller*
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001091pci_bus_to_hose(int bus)
1092{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001093 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001094
Kumar Galaa4c9e322007-06-27 13:09:43 -05001095 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001096 if (bus >= hose->first_busno && bus <= hose->last_busno)
1097 return hose;
1098 return NULL;
1099}
1100
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001101/* Provide information on locations of various I/O regions in physical
1102 * memory. Do this on a per-card basis so that we choose the right
1103 * root bridge.
1104 * Note that the returned IO or memory base is a physical address
1105 */
1106
1107long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1108{
1109 struct pci_controller* hose;
1110 long result = -EOPNOTSUPP;
1111
1112 /* Argh ! Please forgive me for that hack, but that's the
1113 * simplest way to get existing XFree to not lockup on some
1114 * G5 machines... So when something asks for bus 0 io base
1115 * (bus 0 is HT root), we return the AGP one instead.
1116 */
1117#ifdef CONFIG_PPC_PMAC
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001118 if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001119 if (bus == 0)
1120 bus = 0xf0;
1121#endif /* CONFIG_PPC_PMAC */
1122
1123 hose = pci_bus_to_hose(bus);
1124 if (!hose)
1125 return -ENODEV;
1126
1127 switch (which) {
1128 case IOBASE_BRIDGE_NUMBER:
1129 return (long)hose->first_busno;
1130 case IOBASE_MEMORY:
1131 return (long)hose->pci_mem_offset;
1132 case IOBASE_IO:
1133 return (long)hose->io_base_phys;
1134 case IOBASE_ISA_IO:
1135 return (long)isa_io_base;
1136 case IOBASE_ISA_MEM:
1137 return (long)isa_mem_base;
1138 }
1139
1140 return result;
1141}
1142
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001143unsigned long pci_address_to_pio(phys_addr_t address)
1144{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001145 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001146
Kumar Galaa4c9e322007-06-27 13:09:43 -05001147 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001148 unsigned int size = hose->io_resource.end -
1149 hose->io_resource.start + 1;
1150 if (address >= hose->io_base_phys &&
1151 address < (hose->io_base_phys + size)) {
1152 unsigned long base =
1153 (unsigned long)hose->io_base_virt - _IO_BASE;
1154 return base + (address - hose->io_base_phys);
1155 }
1156 }
1157 return (unsigned int)-1;
1158}
1159EXPORT_SYMBOL(pci_address_to_pio);
1160
1161/*
1162 * Null PCI config access functions, for the case when we can't
1163 * find a hose.
1164 */
1165#define NULL_PCI_OP(rw, size, type) \
1166static int \
1167null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1168{ \
1169 return PCIBIOS_DEVICE_NOT_FOUND; \
1170}
1171
1172static int
1173null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1174 int len, u32 *val)
1175{
1176 return PCIBIOS_DEVICE_NOT_FOUND;
1177}
1178
1179static int
1180null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1181 int len, u32 val)
1182{
1183 return PCIBIOS_DEVICE_NOT_FOUND;
1184}
1185
1186static struct pci_ops null_pci_ops =
1187{
Nathan Lynch6127d1c2007-08-10 05:18:42 +10001188 .read = null_read_config,
1189 .write = null_write_config,
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001190};
1191
1192/*
1193 * These functions are used early on before PCI scanning is done
1194 * and all of the pci_dev and pci_bus structures have been created.
1195 */
1196static struct pci_bus *
1197fake_pci_bus(struct pci_controller *hose, int busnr)
1198{
1199 static struct pci_bus bus;
1200
1201 if (hose == 0) {
1202 hose = pci_bus_to_hose(busnr);
1203 if (hose == 0)
1204 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1205 }
1206 bus.number = busnr;
1207 bus.sysdata = hose;
1208 bus.ops = hose? hose->ops: &null_pci_ops;
1209 return &bus;
1210}
1211
1212#define EARLY_PCI_OP(rw, size, type) \
1213int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1214 int devfn, int offset, type value) \
1215{ \
1216 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1217 devfn, offset, value); \
1218}
1219
1220EARLY_PCI_OP(read, byte, u8 *)
1221EARLY_PCI_OP(read, word, u16 *)
1222EARLY_PCI_OP(read, dword, u32 *)
1223EARLY_PCI_OP(write, byte, u8)
1224EARLY_PCI_OP(write, word, u16)
1225EARLY_PCI_OP(write, dword, u32)
Kumar Gala38805e52007-07-10 23:37:45 -05001226
1227extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
1228int early_find_capability(struct pci_controller *hose, int bus, int devfn,
1229 int cap)
1230{
1231 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
1232}