blob: 04d04c5bfdd0ed79b16b45cf4ee3edec8edf5cd8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mackerrasa7fdd902006-01-15 17:30:44 +11002 * Common prep/chrp pci routines. -- Cort
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 */
4
5#include <linux/config.h>
6#include <linux/kernel.h>
7#include <linux/pci.h>
8#include <linux/delay.h>
9#include <linux/string.h>
10#include <linux/init.h>
11#include <linux/capability.h>
12#include <linux/sched.h>
13#include <linux/errno.h>
14#include <linux/bootmem.h>
15
16#include <asm/processor.h>
17#include <asm/io.h>
18#include <asm/prom.h>
19#include <asm/sections.h>
20#include <asm/pci-bridge.h>
21#include <asm/byteorder.h>
22#include <asm/irq.h>
23#include <asm/uaccess.h>
Paul Mackerrasb60fc8bb2005-10-10 14:14:55 +100024#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static void fixup_cpc710_pci64(struct pci_dev* dev);
49#ifdef CONFIG_PPC_OF
50static u8* pci_to_OF_bus_map;
51#endif
52
Paul Mackerrasa7fdd902006-01-15 17:30:44 +110053/* By default, we don't re-assign bus numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
Paul Mackerras399fe2b2005-10-20 20:57:05 +100055int pci_assign_all_buses;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57struct pci_controller* hose_head;
58struct pci_controller** hose_tail = &hose_head;
59
60static int pci_bus_count;
61
62static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070063fixup_broken_pcnet32(struct pci_dev* dev)
64{
65 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
66 dev->vendor = PCI_VENDOR_ID_AMD;
67 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69}
70DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
71
72static void
73fixup_cpc710_pci64(struct pci_dev* dev)
74{
75 /* Hide the PCI64 BARs from the kernel as their content doesn't
76 * fit well in the resource management
77 */
78 dev->resource[0].start = dev->resource[0].end = 0;
79 dev->resource[0].flags = 0;
80 dev->resource[1].start = dev->resource[1].end = 0;
81 dev->resource[1].flags = 0;
82}
83DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
84
85static void
86pcibios_fixup_resources(struct pci_dev *dev)
87{
88 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
89 int i;
90 unsigned long offset;
91
92 if (!hose) {
93 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
94 return;
95 }
96 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
97 struct resource *res = dev->resource + i;
98 if (!res->flags)
99 continue;
100 if (res->end == 0xffffffff) {
101 DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
102 pci_name(dev), i, res->start, res->end);
103 res->end -= res->start;
104 res->start = 0;
105 res->flags |= IORESOURCE_UNSET;
106 continue;
107 }
108 offset = 0;
109 if (res->flags & IORESOURCE_MEM) {
110 offset = hose->pci_mem_offset;
111 } else if (res->flags & IORESOURCE_IO) {
112 offset = (unsigned long) hose->io_base_virt
113 - isa_io_base;
114 }
115 if (offset != 0) {
116 res->start += offset;
117 res->end += offset;
118#ifdef DEBUG
119 printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
120 i, res->flags, pci_name(dev),
121 res->start - offset, res->start);
122#endif
123 }
124 }
125
126 /* Call machine specific resource fixup */
127 if (ppc_md.pcibios_fixup_resources)
128 ppc_md.pcibios_fixup_resources(dev);
129}
130DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
131
132void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
133 struct resource *res)
134{
135 unsigned long offset = 0;
136 struct pci_controller *hose = dev->sysdata;
137
138 if (hose && res->flags & IORESOURCE_IO)
139 offset = (unsigned long)hose->io_base_virt - isa_io_base;
140 else if (hose && res->flags & IORESOURCE_MEM)
141 offset = hose->pci_mem_offset;
142 region->start = res->start - offset;
143 region->end = res->end - offset;
144}
145EXPORT_SYMBOL(pcibios_resource_to_bus);
146
Dominik Brodowski43c34732005-08-04 18:06:21 -0700147void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
148 struct pci_bus_region *region)
149{
150 unsigned long offset = 0;
151 struct pci_controller *hose = dev->sysdata;
152
153 if (hose && res->flags & IORESOURCE_IO)
154 offset = (unsigned long)hose->io_base_virt - isa_io_base;
155 else if (hose && res->flags & IORESOURCE_MEM)
156 offset = hose->pci_mem_offset;
157 res->start = region->start + offset;
158 res->end = region->end + offset;
159}
160EXPORT_SYMBOL(pcibios_bus_to_resource);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/*
163 * We need to avoid collisions with `mirrored' VGA ports
164 * and other strange ISA hardware, so we always want the
165 * addresses to be allocated in the 0x000-0x0ff region
166 * modulo 0x400.
167 *
168 * Why? Because some silly external IO cards only decode
169 * the low 10 bits of the IO address. The 0x00-0xff region
170 * is reserved for motherboard devices that decode all 16
171 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
172 * but we want to try to avoid allocating at 0x2900-0x2bff
173 * which might have be mirrored at 0x0100-0x03ff..
174 */
175void pcibios_align_resource(void *data, struct resource *res, unsigned long size,
176 unsigned long align)
177{
178 struct pci_dev *dev = data;
179
180 if (res->flags & IORESOURCE_IO) {
181 unsigned long start = res->start;
182
183 if (size > 0x100) {
184 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
185 " (%ld bytes)\n", pci_name(dev),
186 dev->resource - res, size);
187 }
188
189 if (start & 0x300) {
190 start = (start + 0x3ff) & ~0x3ff;
191 res->start = start;
192 }
193 }
194}
195EXPORT_SYMBOL(pcibios_align_resource);
196
197/*
198 * Handle resources of PCI devices. If the world were perfect, we could
199 * just allocate all the resource regions and do nothing more. It isn't.
200 * On the other hand, we cannot just re-allocate all devices, as it would
201 * require us to know lots of host bridge internals. So we attempt to
202 * keep as much of the original configuration as possible, but tweak it
203 * when it's found to be wrong.
204 *
205 * Known BIOS problems we have to work around:
206 * - I/O or memory regions not configured
207 * - regions configured, but not enabled in the command register
208 * - bogus I/O addresses above 64K used
209 * - expansion ROMs left enabled (this may sound harmless, but given
210 * the fact the PCI specs explicitly allow address decoders to be
211 * shared between expansion ROMs and other resource regions, it's
212 * at least dangerous)
213 *
214 * Our solution:
215 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
216 * This gives us fixed barriers on where we can allocate.
217 * (2) Allocate resources for all enabled devices. If there is
218 * a collision, just mark the resource as unallocated. Also
219 * disable expansion ROMs during this step.
220 * (3) Try to allocate resources for disabled devices. If the
221 * resources were assigned correctly, everything goes well,
222 * if they weren't, they won't disturb allocation of other
223 * resources.
224 * (4) Assign new addresses to resources which were either
225 * not configured at all or misconfigured. If explicitly
226 * requested by the user, configure expansion ROM address
227 * as well.
228 */
229
230static void __init
231pcibios_allocate_bus_resources(struct list_head *bus_list)
232{
233 struct pci_bus *bus;
234 int i;
235 struct resource *res, *pr;
236
237 /* Depth-First Search on bus tree */
238 list_for_each_entry(bus, bus_list, node) {
239 for (i = 0; i < 4; ++i) {
240 if ((res = bus->resource[i]) == NULL || !res->flags
241 || res->start > res->end)
242 continue;
243 if (bus->parent == NULL)
244 pr = (res->flags & IORESOURCE_IO)?
245 &ioport_resource: &iomem_resource;
246 else {
247 pr = pci_find_parent_resource(bus->self, res);
248 if (pr == res) {
249 /* this happens when the generic PCI
250 * code (wrongly) decides that this
251 * bridge is transparent -- paulus
252 */
253 continue;
254 }
255 }
256
257 DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
258 res->start, res->end, res->flags, pr);
259 if (pr) {
260 if (request_resource(pr, res) == 0)
261 continue;
262 /*
263 * Must be a conflict with an existing entry.
264 * Move that entry (or entries) under the
265 * bridge resource and try again.
266 */
267 if (reparent_resources(pr, res) == 0)
268 continue;
269 }
270 printk(KERN_ERR "PCI: Cannot allocate resource region "
271 "%d of PCI bridge %d\n", i, bus->number);
272 if (pci_relocate_bridge_resource(bus, i))
273 bus->resource[i] = NULL;
274 }
275 pcibios_allocate_bus_resources(&bus->children);
276 }
277}
278
279/*
280 * Reparent resource children of pr that conflict with res
281 * under res, and make res replace those children.
282 */
283static int __init
284reparent_resources(struct resource *parent, struct resource *res)
285{
286 struct resource *p, **pp;
287 struct resource **firstpp = NULL;
288
289 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
290 if (p->end < res->start)
291 continue;
292 if (res->end < p->start)
293 break;
294 if (p->start < res->start || p->end > res->end)
295 return -1; /* not completely contained */
296 if (firstpp == NULL)
297 firstpp = pp;
298 }
299 if (firstpp == NULL)
300 return -1; /* didn't find any conflicting entries? */
301 res->parent = parent;
302 res->child = *firstpp;
303 res->sibling = *pp;
304 *firstpp = res;
305 *pp = NULL;
306 for (p = res->child; p != NULL; p = p->sibling) {
307 p->parent = res;
308 DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
309 p->name, p->start, p->end, res->name);
310 }
311 return 0;
312}
313
314/*
315 * A bridge has been allocated a range which is outside the range
316 * of its parent bridge, so it needs to be moved.
317 */
318static int __init
319pci_relocate_bridge_resource(struct pci_bus *bus, int i)
320{
321 struct resource *res, *pr, *conflict;
322 unsigned long try, size;
323 int j;
324 struct pci_bus *parent = bus->parent;
325
326 if (parent == NULL) {
327 /* shouldn't ever happen */
328 printk(KERN_ERR "PCI: can't move host bridge resource\n");
329 return -1;
330 }
331 res = bus->resource[i];
332 if (res == NULL)
333 return -1;
334 pr = NULL;
335 for (j = 0; j < 4; j++) {
336 struct resource *r = parent->resource[j];
337 if (!r)
338 continue;
339 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
340 continue;
341 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
342 pr = r;
343 break;
344 }
345 if (res->flags & IORESOURCE_PREFETCH)
346 pr = r;
347 }
348 if (pr == NULL)
349 return -1;
350 size = res->end - res->start;
351 if (pr->start > pr->end || size > pr->end - pr->start)
352 return -1;
353 try = pr->end;
354 for (;;) {
355 res->start = try - size;
356 res->end = try;
357 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
358 break;
359 if (conflict->start <= pr->start + size)
360 return -1;
361 try = conflict->start - 1;
362 }
363 if (request_resource(pr, res)) {
364 DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
365 res->start, res->end);
366 return -1; /* "can't happen" */
367 }
368 update_bridge_base(bus, i);
369 printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
370 bus->number, i, res->start, res->end);
371 return 0;
372}
373
374static int __init
375probe_resource(struct pci_bus *parent, struct resource *pr,
376 struct resource *res, struct resource **conflict)
377{
378 struct pci_bus *bus;
379 struct pci_dev *dev;
380 struct resource *r;
381 int i;
382
383 for (r = pr->child; r != NULL; r = r->sibling) {
384 if (r->end >= res->start && res->end >= r->start) {
385 *conflict = r;
386 return 1;
387 }
388 }
389 list_for_each_entry(bus, &parent->children, node) {
390 for (i = 0; i < 4; ++i) {
391 if ((r = bus->resource[i]) == NULL)
392 continue;
393 if (!r->flags || r->start > r->end || r == res)
394 continue;
395 if (pci_find_parent_resource(bus->self, r) != pr)
396 continue;
397 if (r->end >= res->start && res->end >= r->start) {
398 *conflict = r;
399 return 1;
400 }
401 }
402 }
403 list_for_each_entry(dev, &parent->devices, bus_list) {
404 for (i = 0; i < 6; ++i) {
405 r = &dev->resource[i];
406 if (!r->flags || (r->flags & IORESOURCE_UNSET))
407 continue;
408 if (pci_find_parent_resource(dev, r) != pr)
409 continue;
410 if (r->end >= res->start && res->end >= r->start) {
411 *conflict = r;
412 return 1;
413 }
414 }
415 }
416 return 0;
417}
418
419static void __init
420update_bridge_base(struct pci_bus *bus, int i)
421{
422 struct resource *res = bus->resource[i];
423 u8 io_base_lo, io_limit_lo;
424 u16 mem_base, mem_limit;
425 u16 cmd;
426 unsigned long start, end, off;
427 struct pci_dev *dev = bus->self;
428 struct pci_controller *hose = dev->sysdata;
429
430 if (!hose) {
431 printk("update_bridge_base: no hose?\n");
432 return;
433 }
434 pci_read_config_word(dev, PCI_COMMAND, &cmd);
435 pci_write_config_word(dev, PCI_COMMAND,
436 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
437 if (res->flags & IORESOURCE_IO) {
438 off = (unsigned long) hose->io_base_virt - isa_io_base;
439 start = res->start - off;
440 end = res->end - off;
441 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
442 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
443 if (end > 0xffff) {
444 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
445 start >> 16);
446 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
447 end >> 16);
448 io_base_lo |= PCI_IO_RANGE_TYPE_32;
449 } else
450 io_base_lo |= PCI_IO_RANGE_TYPE_16;
451 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
452 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
453
454 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
455 == IORESOURCE_MEM) {
456 off = hose->pci_mem_offset;
457 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
458 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
459 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
460 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
461
462 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
463 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
464 off = hose->pci_mem_offset;
465 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
466 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
467 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
468 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
469
470 } else {
471 DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
472 pci_name(dev), i, res->flags);
473 }
474 pci_write_config_word(dev, PCI_COMMAND, cmd);
475}
476
477static inline void alloc_resource(struct pci_dev *dev, int idx)
478{
479 struct resource *pr, *r = &dev->resource[idx];
480
481 DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
482 pci_name(dev), idx, r->start, r->end, r->flags);
483 pr = pci_find_parent_resource(dev, r);
484 if (!pr || request_resource(pr, r) < 0) {
485 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
486 " of device %s\n", idx, pci_name(dev));
487 if (pr)
488 DBG("PCI: parent is %p: %08lx-%08lx (f=%lx)\n",
489 pr, pr->start, pr->end, pr->flags);
490 /* We'll assign a new address later */
491 r->flags |= IORESOURCE_UNSET;
492 r->end -= r->start;
493 r->start = 0;
494 }
495}
496
497static void __init
498pcibios_allocate_resources(int pass)
499{
500 struct pci_dev *dev = NULL;
501 int idx, disabled;
502 u16 command;
503 struct resource *r;
504
Jiri Slabycee02952005-11-06 23:39:34 -0800505 for_each_pci_dev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 pci_read_config_word(dev, PCI_COMMAND, &command);
507 for (idx = 0; idx < 6; idx++) {
508 r = &dev->resource[idx];
509 if (r->parent) /* Already allocated */
510 continue;
511 if (!r->flags || (r->flags & IORESOURCE_UNSET))
512 continue; /* Not assigned at all */
513 if (r->flags & IORESOURCE_IO)
514 disabled = !(command & PCI_COMMAND_IO);
515 else
516 disabled = !(command & PCI_COMMAND_MEMORY);
517 if (pass == disabled)
518 alloc_resource(dev, idx);
519 }
520 if (pass)
521 continue;
522 r = &dev->resource[PCI_ROM_RESOURCE];
523 if (r->flags & IORESOURCE_ROM_ENABLE) {
524 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
525 u32 reg;
526 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
527 r->flags &= ~IORESOURCE_ROM_ENABLE;
528 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
529 pci_write_config_dword(dev, dev->rom_base_reg,
530 reg & ~PCI_ROM_ADDRESS_ENABLE);
531 }
532 }
533}
534
535static void __init
536pcibios_assign_resources(void)
537{
538 struct pci_dev *dev = NULL;
539 int idx;
540 struct resource *r;
541
Jiri Slabycee02952005-11-06 23:39:34 -0800542 for_each_pci_dev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int class = dev->class >> 8;
544
545 /* Don't touch classless devices and host bridges */
546 if (!class || class == PCI_CLASS_BRIDGE_HOST)
547 continue;
548
549 for (idx = 0; idx < 6; idx++) {
550 r = &dev->resource[idx];
551
552 /*
553 * We shall assign a new address to this resource,
554 * either because the BIOS (sic) forgot to do so
555 * or because we have decided the old address was
556 * unusable for some reason.
557 */
558 if ((r->flags & IORESOURCE_UNSET) && r->end &&
559 (!ppc_md.pcibios_enable_device_hook ||
560 !ppc_md.pcibios_enable_device_hook(dev, 1))) {
561 r->flags &= ~IORESOURCE_UNSET;
562 pci_assign_resource(dev, idx);
563 }
564 }
565
566#if 0 /* don't assign ROMs */
567 r = &dev->resource[PCI_ROM_RESOURCE];
568 r->end -= r->start;
569 r->start = 0;
570 if (r->end)
571 pci_assign_resource(dev, PCI_ROM_RESOURCE);
572#endif
573 }
574}
575
576
577int
578pcibios_enable_resources(struct pci_dev *dev, int mask)
579{
580 u16 cmd, old_cmd;
581 int idx;
582 struct resource *r;
583
584 pci_read_config_word(dev, PCI_COMMAND, &cmd);
585 old_cmd = cmd;
586 for (idx=0; idx<6; idx++) {
587 /* Only set up the requested stuff */
588 if (!(mask & (1<<idx)))
589 continue;
590
591 r = &dev->resource[idx];
592 if (r->flags & IORESOURCE_UNSET) {
593 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
594 return -EINVAL;
595 }
596 if (r->flags & IORESOURCE_IO)
597 cmd |= PCI_COMMAND_IO;
598 if (r->flags & IORESOURCE_MEM)
599 cmd |= PCI_COMMAND_MEMORY;
600 }
601 if (dev->resource[PCI_ROM_RESOURCE].start)
602 cmd |= PCI_COMMAND_MEMORY;
603 if (cmd != old_cmd) {
604 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
605 pci_write_config_word(dev, PCI_COMMAND, cmd);
606 }
607 return 0;
608}
609
610static int next_controller_index;
611
612struct pci_controller * __init
613pcibios_alloc_controller(void)
614{
615 struct pci_controller *hose;
616
617 hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
618 memset(hose, 0, sizeof(struct pci_controller));
619
620 *hose_tail = hose;
621 hose_tail = &hose->next;
622
623 hose->index = next_controller_index++;
624
625 return hose;
626}
627
628#ifdef CONFIG_PPC_OF
629/*
630 * Functions below are used on OpenFirmware machines.
631 */
Jon Loeligerf495a8b2005-09-17 10:35:08 -0500632static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633make_one_node_map(struct device_node* node, u8 pci_bus)
634{
635 int *bus_range;
636 int len;
637
638 if (pci_bus >= pci_bus_count)
639 return;
640 bus_range = (int *) get_property(node, "bus-range", &len);
641 if (bus_range == NULL || len < 2 * sizeof(int)) {
642 printk(KERN_WARNING "Can't get bus-range for %s, "
643 "assuming it starts at 0\n", node->full_name);
644 pci_to_OF_bus_map[pci_bus] = 0;
645 } else
646 pci_to_OF_bus_map[pci_bus] = bus_range[0];
647
648 for (node=node->child; node != 0;node = node->sibling) {
649 struct pci_dev* dev;
650 unsigned int *class_code, *reg;
651
652 class_code = (unsigned int *) get_property(node, "class-code", NULL);
653 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
654 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
655 continue;
656 reg = (unsigned int *)get_property(node, "reg", NULL);
657 if (!reg)
658 continue;
659 dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
660 if (!dev || !dev->subordinate)
661 continue;
662 make_one_node_map(node, dev->subordinate->number);
663 }
664}
665
Jon Loeligerf495a8b2005-09-17 10:35:08 -0500666void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667pcibios_make_OF_bus_map(void)
668{
669 int i;
670 struct pci_controller* hose;
671 u8* of_prop_map;
672
673 pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
674 if (!pci_to_OF_bus_map) {
675 printk(KERN_ERR "Can't allocate OF bus map !\n");
676 return;
677 }
678
679 /* We fill the bus map with invalid values, that helps
680 * debugging.
681 */
682 for (i=0; i<pci_bus_count; i++)
683 pci_to_OF_bus_map[i] = 0xff;
684
685 /* For each hose, we begin searching bridges */
686 for(hose=hose_head; hose; hose=hose->next) {
687 struct device_node* node;
688 node = (struct device_node *)hose->arch_data;
689 if (!node)
690 continue;
691 make_one_node_map(node, hose->first_busno);
692 }
693 of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", NULL);
694 if (of_prop_map)
695 memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
696#ifdef DEBUG
697 printk("PCI->OF bus map:\n");
698 for (i=0; i<pci_bus_count; i++) {
699 if (pci_to_OF_bus_map[i] == 0xff)
700 continue;
701 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
702 }
703#endif
704}
705
706typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
707
Jon Loeligerf495a8b2005-09-17 10:35:08 -0500708static struct device_node*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
710{
711 struct device_node* sub_node;
712
713 for (; node != 0;node = node->sibling) {
714 unsigned int *class_code;
715
716 if (filter(node, data))
717 return node;
718
719 /* For PCI<->PCI bridges or CardBus bridges, we go down
720 * Note: some OFs create a parent node "multifunc-device" as
721 * a fake root for all functions of a multi-function device,
722 * we go down them as well.
723 */
724 class_code = (unsigned int *) get_property(node, "class-code", NULL);
725 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
726 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
727 strcmp(node->name, "multifunc-device"))
728 continue;
729 sub_node = scan_OF_pci_childs(node->child, filter, data);
730 if (sub_node)
731 return sub_node;
732 }
733 return NULL;
734}
735
736static int
737scan_OF_pci_childs_iterator(struct device_node* node, void* data)
738{
739 unsigned int *reg;
740 u8* fdata = (u8*)data;
741
742 reg = (unsigned int *) get_property(node, "reg", NULL);
743 if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
744 && ((reg[0] >> 16) & 0xff) == fdata[0])
745 return 1;
746 return 0;
747}
748
Jon Loeligerf495a8b2005-09-17 10:35:08 -0500749static struct device_node*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
751{
752 u8 filter_data[2] = {bus, dev_fn};
753
754 return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
755}
756
757/*
758 * Scans the OF tree for a device node matching a PCI device
759 */
760struct device_node *
761pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
762{
763 struct pci_controller *hose;
764 struct device_node *node;
765 int busnr;
766
767 if (!have_of)
768 return NULL;
769
770 /* Lookup the hose */
771 busnr = bus->number;
772 hose = pci_bus_to_hose(busnr);
773 if (!hose)
774 return NULL;
775
776 /* Check it has an OF node associated */
777 node = (struct device_node *) hose->arch_data;
778 if (!node)
779 return NULL;
780
781 /* Fixup bus number according to what OF think it is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (pci_to_OF_bus_map)
783 busnr = pci_to_OF_bus_map[busnr];
784 if (busnr == 0xff)
785 return NULL;
786
787 /* Now, lookup childs of the hose */
788 return scan_OF_childs_for_device(node->child, busnr, devfn);
789}
Paul Mackerrasdd8cad62005-10-04 14:28:29 +1000790EXPORT_SYMBOL(pci_busdev_to_OF_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792struct device_node*
793pci_device_to_OF_node(struct pci_dev *dev)
794{
795 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
796}
Paul Mackerrasdd8cad62005-10-04 14:28:29 +1000797EXPORT_SYMBOL(pci_device_to_OF_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799/* This routine is meant to be used early during boot, when the
800 * PCI bus numbers have not yet been assigned, and you need to
801 * issue PCI config cycles to an OF device.
802 * It could also be used to "fix" RTAS config cycles if you want
Paul Mackerras399fe2b2005-10-20 20:57:05 +1000803 * to set pci_assign_all_buses to 1 and still use RTAS for PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * config cycles.
805 */
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100806struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 if (!have_of)
809 return NULL;
810 while(node) {
811 struct pci_controller* hose;
812 for (hose=hose_head;hose;hose=hose->next)
813 if (hose->arch_data == node)
814 return hose;
815 node=node->parent;
816 }
817 return NULL;
818}
819
Jon Loeligerf495a8b2005-09-17 10:35:08 -0500820static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821find_OF_pci_device_filter(struct device_node* node, void* data)
822{
823 return ((void *)node == data);
824}
825
826/*
827 * Returns the PCI device matching a given OF node
828 */
829int
830pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
831{
832 unsigned int *reg;
833 struct pci_controller* hose;
834 struct pci_dev* dev = NULL;
835
836 if (!have_of)
837 return -ENODEV;
838 /* Make sure it's really a PCI device */
839 hose = pci_find_hose_for_OF_device(node);
840 if (!hose || !hose->arch_data)
841 return -ENODEV;
842 if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
843 find_OF_pci_device_filter, (void *)node))
844 return -ENODEV;
845 reg = (unsigned int *) get_property(node, "reg", NULL);
846 if (!reg)
847 return -ENODEV;
848 *bus = (reg[0] >> 16) & 0xff;
849 *devfn = ((reg[0] >> 8) & 0xff);
850
851 /* Ok, here we need some tweak. If we have already renumbered
852 * all busses, we can't rely on the OF bus number any more.
853 * the pci_to_OF_bus_map is not enough as several PCI busses
854 * may match the same OF bus number.
855 */
856 if (!pci_to_OF_bus_map)
857 return 0;
Jiri Slabycee02952005-11-06 23:39:34 -0800858
859 for_each_pci_dev(dev)
860 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
861 dev->devfn == *devfn) {
862 *bus = dev->bus->number;
863 pci_dev_put(dev);
864 return 0;
865 }
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -ENODEV;
868}
Paul Mackerrasdd8cad62005-10-04 14:28:29 +1000869EXPORT_SYMBOL(pci_device_from_OF_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871void __init
872pci_process_bridge_OF_ranges(struct pci_controller *hose,
873 struct device_node *dev, int primary)
874{
875 static unsigned int static_lc_ranges[256] __initdata;
876 unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
877 unsigned int size;
878 int rlen = 0, orig_rlen;
879 int memno = 0;
880 struct resource *res;
881 int np, na = prom_n_addr_cells(dev);
882 np = na + 5;
883
884 /* First we try to merge ranges to fix a problem with some pmacs
885 * that can have more than 3 ranges, fortunately using contiguous
886 * addresses -- BenH
887 */
888 dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
889 if (!dt_ranges)
890 return;
891 /* Sanity check, though hopefully that never happens */
892 if (rlen > sizeof(static_lc_ranges)) {
893 printk(KERN_WARNING "OF ranges property too large !\n");
894 rlen = sizeof(static_lc_ranges);
895 }
896 lc_ranges = static_lc_ranges;
897 memcpy(lc_ranges, dt_ranges, rlen);
898 orig_rlen = rlen;
899
900 /* Let's work on a copy of the "ranges" property instead of damaging
901 * the device-tree image in memory
902 */
903 ranges = lc_ranges;
904 prev = NULL;
905 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
906 if (prev) {
907 if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
908 (prev[2] + prev[na+4]) == ranges[2] &&
909 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
910 prev[na+4] += ranges[na+4];
911 ranges[0] = 0;
912 ranges += np;
913 continue;
914 }
915 }
916 prev = ranges;
917 ranges += np;
918 }
919
920 /*
921 * The ranges property is laid out as an array of elements,
922 * each of which comprises:
923 * cells 0 - 2: a PCI address
924 * cells 3 or 3+4: a CPU physical address
925 * (size depending on dev->n_addr_cells)
926 * cells 4+5 or 5+6: the size of the range
927 */
928 ranges = lc_ranges;
929 rlen = orig_rlen;
930 while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
931 res = NULL;
932 size = ranges[na+4];
Kumar Galacd0c7f02005-12-13 14:46:29 -0600933 switch ((ranges[0] >> 24) & 0x3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case 1: /* I/O space */
935 if (ranges[2] != 0)
936 break;
937 hose->io_base_phys = ranges[na+2];
938 /* limit I/O space to 16MB */
939 if (size > 0x01000000)
940 size = 0x01000000;
941 hose->io_base_virt = ioremap(ranges[na+2], size);
942 if (primary)
943 isa_io_base = (unsigned long) hose->io_base_virt;
944 res = &hose->io_resource;
945 res->flags = IORESOURCE_IO;
946 res->start = ranges[2];
Kumar Galacd0c7f02005-12-13 14:46:29 -0600947 DBG("PCI: IO 0x%lx -> 0x%lx\n",
948 res->start, res->start + size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950 case 2: /* memory space */
951 memno = 0;
952 if (ranges[1] == 0 && ranges[2] == 0
953 && ranges[na+4] <= (16 << 20)) {
954 /* 1st 16MB, i.e. ISA memory area */
955 if (primary)
956 isa_mem_base = ranges[na+2];
957 memno = 1;
958 }
959 while (memno < 3 && hose->mem_resources[memno].flags)
960 ++memno;
961 if (memno == 0)
962 hose->pci_mem_offset = ranges[na+2] - ranges[2];
963 if (memno < 3) {
964 res = &hose->mem_resources[memno];
965 res->flags = IORESOURCE_MEM;
Kumar Galacd0c7f02005-12-13 14:46:29 -0600966 if(ranges[0] & 0x40000000)
967 res->flags |= IORESOURCE_PREFETCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 res->start = ranges[na+2];
Kumar Galacd0c7f02005-12-13 14:46:29 -0600969 DBG("PCI: MEM[%d] 0x%lx -> 0x%lx\n", memno,
970 res->start, res->start + size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 break;
973 }
974 if (res != NULL) {
975 res->name = dev->full_name;
976 res->end = res->start + size - 1;
977 res->parent = NULL;
978 res->sibling = NULL;
979 res->child = NULL;
980 }
981 ranges += np;
982 }
983}
984
985/* We create the "pci-OF-bus-map" property now so it appears in the
986 * /proc device tree
987 */
988void __init
989pci_create_OF_bus_map(void)
990{
991 struct property* of_prop;
992
993 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
994 if (of_prop && find_path_device("/")) {
995 memset(of_prop, -1, sizeof(struct property) + 256);
996 of_prop->name = "pci-OF-bus-map";
997 of_prop->length = 256;
998 of_prop->value = (unsigned char *)&of_prop[1];
999 prom_add_property(find_path_device("/"), of_prop);
1000 }
1001}
1002
Yani Ioannouff381d22005-05-17 06:40:51 -04001003static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct pci_dev *pdev;
1006 struct device_node *np;
1007
1008 pdev = to_pci_dev (dev);
1009 np = pci_device_to_OF_node(pdev);
1010 if (np == NULL || np->full_name == NULL)
1011 return 0;
1012 return sprintf(buf, "%s", np->full_name);
1013}
1014static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
1015
Paul Mackerrasfd582ec2005-10-11 22:08:12 +10001016#else /* CONFIG_PPC_OF */
1017void pcibios_make_OF_bus_map(void)
1018{
1019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020#endif /* CONFIG_PPC_OF */
1021
1022/* Add sysfs properties */
1023void pcibios_add_platform_entries(struct pci_dev *pdev)
1024{
1025#ifdef CONFIG_PPC_OF
1026 device_create_file(&pdev->dev, &dev_attr_devspec);
1027#endif /* CONFIG_PPC_OF */
1028}
1029
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static int __init
1032pcibios_init(void)
1033{
1034 struct pci_controller *hose;
1035 struct pci_bus *bus;
1036 int next_busno;
1037
1038 printk(KERN_INFO "PCI: Probing PCI hardware\n");
1039
1040 /* Scan all of the recorded PCI controllers. */
1041 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
Paul Mackerras399fe2b2005-10-20 20:57:05 +10001042 if (pci_assign_all_buses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 hose->first_busno = next_busno;
1044 hose->last_busno = 0xff;
1045 bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
1046 hose->last_busno = bus->subordinate;
Paul Mackerras399fe2b2005-10-20 20:57:05 +10001047 if (pci_assign_all_buses || next_busno <= hose->last_busno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 next_busno = hose->last_busno + pcibios_assign_bus_offset;
1049 }
1050 pci_bus_count = next_busno;
1051
1052 /* OpenFirmware based machines need a map of OF bus
1053 * numbers vs. kernel bus numbers since we may have to
1054 * remap them.
1055 */
Paul Mackerras399fe2b2005-10-20 20:57:05 +10001056 if (pci_assign_all_buses && have_of)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 pcibios_make_OF_bus_map();
1058
1059 /* Do machine dependent PCI interrupt routing */
1060 if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
1061 pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
1062
1063 /* Call machine dependent fixup */
1064 if (ppc_md.pcibios_fixup)
1065 ppc_md.pcibios_fixup();
1066
1067 /* Allocate and assign resources */
1068 pcibios_allocate_bus_resources(&pci_root_buses);
1069 pcibios_allocate_resources(0);
1070 pcibios_allocate_resources(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 pcibios_assign_resources();
1072
1073 /* Call machine dependent post-init code */
1074 if (ppc_md.pcibios_after_init)
1075 ppc_md.pcibios_after_init();
1076
1077 return 0;
1078}
1079
1080subsys_initcall(pcibios_init);
1081
1082unsigned char __init
1083common_swizzle(struct pci_dev *dev, unsigned char *pinp)
1084{
1085 struct pci_controller *hose = dev->sysdata;
1086
1087 if (dev->bus->number != hose->first_busno) {
1088 u8 pin = *pinp;
1089 do {
1090 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
1091 /* Move up the chain of bridges. */
1092 dev = dev->bus->self;
1093 } while (dev->bus->self);
1094 *pinp = pin;
1095
1096 /* The slot is the idsel of the last bridge. */
1097 }
1098 return PCI_SLOT(dev->devfn);
1099}
1100
1101unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
1102 unsigned long start, unsigned long size)
1103{
1104 return start;
1105}
1106
1107void __init pcibios_fixup_bus(struct pci_bus *bus)
1108{
1109 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1110 unsigned long io_offset;
1111 struct resource *res;
1112 int i;
1113
1114 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1115 if (bus->parent == NULL) {
1116 /* This is a host bridge - fill in its resources */
1117 hose->bus = bus;
1118
1119 bus->resource[0] = res = &hose->io_resource;
1120 if (!res->flags) {
1121 if (io_offset)
1122 printk(KERN_ERR "I/O resource not set for host"
1123 " bridge %d\n", hose->index);
1124 res->start = 0;
1125 res->end = IO_SPACE_LIMIT;
1126 res->flags = IORESOURCE_IO;
1127 }
1128 res->start += io_offset;
1129 res->end += io_offset;
1130
1131 for (i = 0; i < 3; ++i) {
1132 res = &hose->mem_resources[i];
1133 if (!res->flags) {
1134 if (i > 0)
1135 continue;
1136 printk(KERN_ERR "Memory resource not set for "
1137 "host bridge %d\n", hose->index);
1138 res->start = hose->pci_mem_offset;
1139 res->end = ~0U;
1140 res->flags = IORESOURCE_MEM;
1141 }
1142 bus->resource[i+1] = res;
1143 }
1144 } else {
1145 /* This is a subordinate bridge */
1146 pci_read_bridge_bases(bus);
1147
1148 for (i = 0; i < 4; ++i) {
1149 if ((res = bus->resource[i]) == NULL)
1150 continue;
1151 if (!res->flags)
1152 continue;
1153 if (io_offset && (res->flags & IORESOURCE_IO)) {
1154 res->start += io_offset;
1155 res->end += io_offset;
1156 } else if (hose->pci_mem_offset
1157 && (res->flags & IORESOURCE_MEM)) {
1158 res->start += hose->pci_mem_offset;
1159 res->end += hose->pci_mem_offset;
1160 }
1161 }
1162 }
1163
1164 if (ppc_md.pcibios_fixup_bus)
1165 ppc_md.pcibios_fixup_bus(bus);
1166}
1167
1168char __init *pcibios_setup(char *str)
1169{
1170 return str;
1171}
1172
1173/* the next one is stolen from the alpha port... */
1174void __init
1175pcibios_update_irq(struct pci_dev *dev, int irq)
1176{
1177 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1178 /* XXX FIXME - update OF device tree node interrupt property */
1179}
1180
1181int pcibios_enable_device(struct pci_dev *dev, int mask)
1182{
1183 u16 cmd, old_cmd;
1184 int idx;
1185 struct resource *r;
1186
1187 if (ppc_md.pcibios_enable_device_hook)
1188 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1189 return -EINVAL;
1190
1191 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1192 old_cmd = cmd;
1193 for (idx=0; idx<6; idx++) {
1194 r = &dev->resource[idx];
1195 if (r->flags & IORESOURCE_UNSET) {
1196 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1197 return -EINVAL;
1198 }
1199 if (r->flags & IORESOURCE_IO)
1200 cmd |= PCI_COMMAND_IO;
1201 if (r->flags & IORESOURCE_MEM)
1202 cmd |= PCI_COMMAND_MEMORY;
1203 }
1204 if (cmd != old_cmd) {
1205 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1206 pci_name(dev), old_cmd, cmd);
1207 pci_write_config_word(dev, PCI_COMMAND, cmd);
1208 }
1209 return 0;
1210}
1211
1212struct pci_controller*
1213pci_bus_to_hose(int bus)
1214{
1215 struct pci_controller* hose = hose_head;
1216
1217 for (; hose; hose = hose->next)
1218 if (bus >= hose->first_busno && bus <= hose->last_busno)
1219 return hose;
1220 return NULL;
1221}
1222
Al Viro92a11f92005-04-25 07:55:57 -07001223void __iomem *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224pci_bus_io_base(unsigned int bus)
1225{
1226 struct pci_controller *hose;
1227
1228 hose = pci_bus_to_hose(bus);
1229 if (!hose)
1230 return NULL;
1231 return hose->io_base_virt;
1232}
1233
1234unsigned long
1235pci_bus_io_base_phys(unsigned int bus)
1236{
1237 struct pci_controller *hose;
1238
1239 hose = pci_bus_to_hose(bus);
1240 if (!hose)
1241 return 0;
1242 return hose->io_base_phys;
1243}
1244
1245unsigned long
1246pci_bus_mem_base_phys(unsigned int bus)
1247{
1248 struct pci_controller *hose;
1249
1250 hose = pci_bus_to_hose(bus);
1251 if (!hose)
1252 return 0;
1253 return hose->pci_mem_offset;
1254}
1255
1256unsigned long
1257pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
1258{
1259 /* Hack alert again ! See comments in chrp_pci.c
1260 */
1261 struct pci_controller* hose =
1262 (struct pci_controller *)pdev->sysdata;
1263 if (hose && res->flags & IORESOURCE_MEM)
1264 return res->start - hose->pci_mem_offset;
1265 /* We may want to do something with IOs here... */
1266 return res->start;
1267}
1268
1269
1270static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
1271 unsigned long *offset,
1272 enum pci_mmap_state mmap_state)
1273{
1274 struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
1275 unsigned long io_offset = 0;
1276 int i, res_bit;
1277
1278 if (hose == 0)
1279 return NULL; /* should never happen */
1280
1281 /* If memory, add on the PCI bridge address offset */
1282 if (mmap_state == pci_mmap_mem) {
1283 *offset += hose->pci_mem_offset;
1284 res_bit = IORESOURCE_MEM;
1285 } else {
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001286 io_offset = hose->io_base_virt - ___IO_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 *offset += io_offset;
1288 res_bit = IORESOURCE_IO;
1289 }
1290
1291 /*
1292 * Check that the offset requested corresponds to one of the
1293 * resources of the device.
1294 */
1295 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
1296 struct resource *rp = &dev->resource[i];
1297 int flags = rp->flags;
1298
1299 /* treat ROM as memory (should be already) */
1300 if (i == PCI_ROM_RESOURCE)
1301 flags |= IORESOURCE_MEM;
1302
1303 /* Active and same type? */
1304 if ((flags & res_bit) == 0)
1305 continue;
1306
1307 /* In the range of this resource? */
1308 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
1309 continue;
1310
1311 /* found it! construct the final physical address */
1312 if (mmap_state == pci_mmap_io)
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001313 *offset += hose->io_base_phys - io_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return rp;
1315 }
1316
1317 return NULL;
1318}
1319
1320/*
1321 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1322 * device mapping.
1323 */
1324static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
1325 pgprot_t protection,
1326 enum pci_mmap_state mmap_state,
1327 int write_combine)
1328{
1329 unsigned long prot = pgprot_val(protection);
1330
1331 /* Write combine is always 0 on non-memory space mappings. On
1332 * memory space, if the user didn't pass 1, we check for a
1333 * "prefetchable" resource. This is a bit hackish, but we use
1334 * this to workaround the inability of /sysfs to provide a write
1335 * combine bit
1336 */
1337 if (mmap_state != pci_mmap_mem)
1338 write_combine = 0;
1339 else if (write_combine == 0) {
1340 if (rp->flags & IORESOURCE_PREFETCH)
1341 write_combine = 1;
1342 }
1343
1344 /* XXX would be nice to have a way to ask for write-through */
1345 prot |= _PAGE_NO_CACHE;
1346 if (write_combine)
1347 prot &= ~_PAGE_GUARDED;
1348 else
1349 prot |= _PAGE_GUARDED;
1350
1351 printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
1352 prot);
1353
1354 return __pgprot(prot);
1355}
1356
1357/*
1358 * This one is used by /dev/mem and fbdev who have no clue about the
1359 * PCI device, it tries to find the PCI device first and calls the
1360 * above routine
1361 */
1362pgprot_t pci_phys_mem_access_prot(struct file *file,
Roland Dreier8b150472005-10-28 17:46:18 -07001363 unsigned long pfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 unsigned long size,
1365 pgprot_t protection)
1366{
1367 struct pci_dev *pdev = NULL;
1368 struct resource *found = NULL;
1369 unsigned long prot = pgprot_val(protection);
Roland Dreier8b150472005-10-28 17:46:18 -07001370 unsigned long offset = pfn << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 int i;
1372
Roland Dreier8b150472005-10-28 17:46:18 -07001373 if (page_is_ram(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return prot;
1375
1376 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
1377
1378 for_each_pci_dev(pdev) {
1379 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
1380 struct resource *rp = &pdev->resource[i];
1381 int flags = rp->flags;
1382
1383 /* Active and same type? */
1384 if ((flags & IORESOURCE_MEM) == 0)
1385 continue;
1386 /* In the range of this resource? */
1387 if (offset < (rp->start & PAGE_MASK) ||
1388 offset > rp->end)
1389 continue;
1390 found = rp;
1391 break;
1392 }
1393 if (found)
1394 break;
1395 }
1396 if (found) {
1397 if (found->flags & IORESOURCE_PREFETCH)
1398 prot &= ~_PAGE_GUARDED;
1399 pci_dev_put(pdev);
1400 }
1401
1402 DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
1403
1404 return __pgprot(prot);
1405}
1406
1407
1408/*
1409 * Perform the actual remap of the pages for a PCI device mapping, as
1410 * appropriate for this architecture. The region in the process to map
1411 * is described by vm_start and vm_end members of VMA, the base physical
1412 * address is found in vm_pgoff.
1413 * The pci device structure is provided so that architectures may make mapping
1414 * decisions on a per-device or per-bus basis.
1415 *
1416 * Returns a negative error code on failure, zero on success.
1417 */
1418int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1419 enum pci_mmap_state mmap_state,
1420 int write_combine)
1421{
1422 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1423 struct resource *rp;
1424 int ret;
1425
1426 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
1427 if (rp == NULL)
1428 return -EINVAL;
1429
1430 vma->vm_pgoff = offset >> PAGE_SHIFT;
1431 vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
1432 vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
1433 vma->vm_page_prot,
1434 mmap_state, write_combine);
1435
1436 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1437 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1438
1439 return ret;
1440}
1441
1442/* Obsolete functions. Should be removed once the symbios driver
1443 * is fixed
1444 */
1445unsigned long
1446phys_to_bus(unsigned long pa)
1447{
1448 struct pci_controller *hose;
1449 int i;
1450
1451 for (hose = hose_head; hose; hose = hose->next) {
1452 for (i = 0; i < 3; ++i) {
1453 if (pa >= hose->mem_resources[i].start
1454 && pa <= hose->mem_resources[i].end) {
1455 /*
1456 * XXX the hose->pci_mem_offset really
1457 * only applies to mem_resources[0].
1458 * We need a way to store an offset for
1459 * the others. -- paulus
1460 */
1461 if (i == 0)
1462 pa -= hose->pci_mem_offset;
1463 return pa;
1464 }
1465 }
1466 }
1467 /* hmmm, didn't find it */
1468 return 0;
1469}
1470
1471unsigned long
1472pci_phys_to_bus(unsigned long pa, int busnr)
1473{
1474 struct pci_controller* hose = pci_bus_to_hose(busnr);
1475 if (!hose)
1476 return pa;
1477 return pa - hose->pci_mem_offset;
1478}
1479
1480unsigned long
1481pci_bus_to_phys(unsigned int ba, int busnr)
1482{
1483 struct pci_controller* hose = pci_bus_to_hose(busnr);
1484 if (!hose)
1485 return ba;
1486 return ba + hose->pci_mem_offset;
1487}
1488
1489/* Provide information on locations of various I/O regions in physical
1490 * memory. Do this on a per-card basis so that we choose the right
1491 * root bridge.
1492 * Note that the returned IO or memory base is a physical address
1493 */
1494
1495long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1496{
1497 struct pci_controller* hose;
1498 long result = -EOPNOTSUPP;
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 hose = pci_bus_to_hose(bus);
1501 if (!hose)
1502 return -ENODEV;
1503
1504 switch (which) {
1505 case IOBASE_BRIDGE_NUMBER:
1506 return (long)hose->first_busno;
1507 case IOBASE_MEMORY:
1508 return (long)hose->pci_mem_offset;
1509 case IOBASE_IO:
1510 return (long)hose->io_base_phys;
1511 case IOBASE_ISA_IO:
1512 return (long)isa_io_base;
1513 case IOBASE_ISA_MEM:
1514 return (long)isa_mem_base;
1515 }
1516
1517 return result;
1518}
1519
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001520void pci_resource_to_user(const struct pci_dev *dev, int bar,
1521 const struct resource *rsrc,
1522 u64 *start, u64 *end)
1523{
1524 struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
1525 unsigned long offset = 0;
1526
1527 if (hose == NULL)
1528 return;
1529
1530 if (rsrc->flags & IORESOURCE_IO)
1531 offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
1532
1533 *start = rsrc->start + offset;
1534 *end = rsrc->end + offset;
1535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537void __init
1538pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
1539 int flags, char *name)
1540{
1541 res->start = start;
1542 res->end = end;
1543 res->flags = flags;
1544 res->name = name;
1545 res->parent = NULL;
1546 res->sibling = NULL;
1547 res->child = NULL;
1548}
1549
1550void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
1551{
1552 unsigned long start = pci_resource_start(dev, bar);
1553 unsigned long len = pci_resource_len(dev, bar);
1554 unsigned long flags = pci_resource_flags(dev, bar);
1555
1556 if (!len)
1557 return NULL;
1558 if (max && len > max)
1559 len = max;
1560 if (flags & IORESOURCE_IO)
1561 return ioport_map(start, len);
1562 if (flags & IORESOURCE_MEM)
1563 /* Not checking IORESOURCE_CACHEABLE because PPC does
1564 * not currently distinguish between ioremap and
1565 * ioremap_nocache.
1566 */
1567 return ioremap(start, len);
1568 /* What? */
1569 return NULL;
1570}
1571
1572void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
1573{
1574 /* Nothing to do */
1575}
1576EXPORT_SYMBOL(pci_iomap);
1577EXPORT_SYMBOL(pci_iounmap);
1578
Benjamin Herrenschmidtf2c45832005-12-15 15:00:57 +11001579unsigned long pci_address_to_pio(phys_addr_t address)
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +11001580{
1581 struct pci_controller* hose = hose_head;
1582
1583 for (; hose; hose = hose->next) {
1584 unsigned int size = hose->io_resource.end -
1585 hose->io_resource.start + 1;
1586 if (address >= hose->io_base_phys &&
Benjamin Herrenschmidtf2c45832005-12-15 15:00:57 +11001587 address < (hose->io_base_phys + size)) {
1588 unsigned long base =
1589 (unsigned long)hose->io_base_virt - _IO_BASE;
1590 return base + (address - hose->io_base_phys);
Kumar Galae5cd0402005-12-19 15:49:07 -06001591 }
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +11001592 }
1593 return (unsigned int)-1;
1594}
1595EXPORT_SYMBOL(pci_address_to_pio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597/*
1598 * Null PCI config access functions, for the case when we can't
1599 * find a hose.
1600 */
1601#define NULL_PCI_OP(rw, size, type) \
1602static int \
1603null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1604{ \
1605 return PCIBIOS_DEVICE_NOT_FOUND; \
1606}
1607
1608static int
1609null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1610 int len, u32 *val)
1611{
1612 return PCIBIOS_DEVICE_NOT_FOUND;
1613}
1614
1615static int
1616null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1617 int len, u32 val)
1618{
1619 return PCIBIOS_DEVICE_NOT_FOUND;
1620}
1621
1622static struct pci_ops null_pci_ops =
1623{
1624 null_read_config,
1625 null_write_config
1626};
1627
1628/*
1629 * These functions are used early on before PCI scanning is done
1630 * and all of the pci_dev and pci_bus structures have been created.
1631 */
1632static struct pci_bus *
1633fake_pci_bus(struct pci_controller *hose, int busnr)
1634{
1635 static struct pci_bus bus;
1636
1637 if (hose == 0) {
1638 hose = pci_bus_to_hose(busnr);
1639 if (hose == 0)
1640 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1641 }
1642 bus.number = busnr;
1643 bus.sysdata = hose;
1644 bus.ops = hose? hose->ops: &null_pci_ops;
1645 return &bus;
1646}
1647
1648#define EARLY_PCI_OP(rw, size, type) \
1649int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1650 int devfn, int offset, type value) \
1651{ \
1652 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1653 devfn, offset, value); \
1654}
1655
1656EARLY_PCI_OP(read, byte, u8 *)
1657EARLY_PCI_OP(read, word, u16 *)
1658EARLY_PCI_OP(read, dword, u32 *)
1659EARLY_PCI_OP(write, byte, u8)
1660EARLY_PCI_OP(write, word, u16)
1661EARLY_PCI_OP(write, dword, u32)