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