blob: 8544e100d713d281a717061792547d9aae436dd9 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Paul Mackerrasa7fdd902006-01-15 17:30:44 +110050/* By default, we don't re-assign bus numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
Paul Mackerras399fe2b2005-10-20 20:57:05 +100052int pci_assign_all_buses;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54struct pci_controller* hose_head;
55struct pci_controller** hose_tail = &hose_head;
56
57static int pci_bus_count;
58
59static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070060fixup_broken_pcnet32(struct pci_dev* dev)
61{
62 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
63 dev->vendor = PCI_VENDOR_ID_AMD;
64 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66}
67DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
68
69static void
70fixup_cpc710_pci64(struct pci_dev* dev)
71{
72 /* Hide the PCI64 BARs from the kernel as their content doesn't
73 * fit well in the resource management
74 */
75 dev->resource[0].start = dev->resource[0].end = 0;
76 dev->resource[0].flags = 0;
77 dev->resource[1].start = dev->resource[1].end = 0;
78 dev->resource[1].flags = 0;
79}
80DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
81
82static void
83pcibios_fixup_resources(struct pci_dev *dev)
84{
85 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
86 int i;
87 unsigned long offset;
88
89 if (!hose) {
90 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
91 return;
92 }
93 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
94 struct resource *res = dev->resource + i;
95 if (!res->flags)
96 continue;
97 if (res->end == 0xffffffff) {
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -070098 DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
99 pci_name(dev), i,
100 (unsigned long long)res->start,
101 (unsigned long long)res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 res->end -= res->start;
103 res->start = 0;
104 res->flags |= IORESOURCE_UNSET;
105 continue;
106 }
107 offset = 0;
108 if (res->flags & IORESOURCE_MEM) {
109 offset = hose->pci_mem_offset;
110 } else if (res->flags & IORESOURCE_IO) {
111 offset = (unsigned long) hose->io_base_virt
112 - isa_io_base;
113 }
114 if (offset != 0) {
115 res->start += offset;
116 res->end += offset;
117#ifdef DEBUG
118 printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
119 i, res->flags, pci_name(dev),
120 res->start - offset, res->start);
121#endif
122 }
123 }
124
125 /* Call machine specific resource fixup */
126 if (ppc_md.pcibios_fixup_resources)
127 ppc_md.pcibios_fixup_resources(dev);
128}
129DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
130
131void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
132 struct resource *res)
133{
134 unsigned long offset = 0;
135 struct pci_controller *hose = dev->sysdata;
136
137 if (hose && res->flags & IORESOURCE_IO)
138 offset = (unsigned long)hose->io_base_virt - isa_io_base;
139 else if (hose && res->flags & IORESOURCE_MEM)
140 offset = hose->pci_mem_offset;
141 region->start = res->start - offset;
142 region->end = res->end - offset;
143}
144EXPORT_SYMBOL(pcibios_resource_to_bus);
145
Dominik Brodowski43c34732005-08-04 18:06:21 -0700146void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
147 struct pci_bus_region *region)
148{
149 unsigned long offset = 0;
150 struct pci_controller *hose = dev->sysdata;
151
152 if (hose && res->flags & IORESOURCE_IO)
153 offset = (unsigned long)hose->io_base_virt - isa_io_base;
154 else if (hose && res->flags & IORESOURCE_MEM)
155 offset = hose->pci_mem_offset;
156 res->start = region->start + offset;
157 res->end = region->end + offset;
158}
159EXPORT_SYMBOL(pcibios_bus_to_resource);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
162 * We need to avoid collisions with `mirrored' VGA ports
163 * and other strange ISA hardware, so we always want the
164 * addresses to be allocated in the 0x000-0x0ff region
165 * modulo 0x400.
166 *
167 * Why? Because some silly external IO cards only decode
168 * the low 10 bits of the IO address. The 0x00-0xff region
169 * is reserved for motherboard devices that decode all 16
170 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
171 * but we want to try to avoid allocating at 0x2900-0x2bff
172 * which might have be mirrored at 0x0100-0x03ff..
173 */
174void pcibios_align_resource(void *data, struct resource *res, unsigned long size,
175 unsigned long align)
176{
177 struct pci_dev *dev = data;
178
179 if (res->flags & IORESOURCE_IO) {
180 unsigned long start = res->start;
181
182 if (size > 0x100) {
183 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700184 " (%lld bytes)\n", pci_name(dev),
185 dev->resource - res, (unsigned long long)size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
188 if (start & 0x300) {
189 start = (start + 0x3ff) & ~0x3ff;
190 res->start = start;
191 }
192 }
193}
194EXPORT_SYMBOL(pcibios_align_resource);
195
196/*
197 * Handle resources of PCI devices. If the world were perfect, we could
198 * just allocate all the resource regions and do nothing more. It isn't.
199 * On the other hand, we cannot just re-allocate all devices, as it would
200 * require us to know lots of host bridge internals. So we attempt to
201 * keep as much of the original configuration as possible, but tweak it
202 * when it's found to be wrong.
203 *
204 * Known BIOS problems we have to work around:
205 * - I/O or memory regions not configured
206 * - regions configured, but not enabled in the command register
207 * - bogus I/O addresses above 64K used
208 * - expansion ROMs left enabled (this may sound harmless, but given
209 * the fact the PCI specs explicitly allow address decoders to be
210 * shared between expansion ROMs and other resource regions, it's
211 * at least dangerous)
212 *
213 * Our solution:
214 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
215 * This gives us fixed barriers on where we can allocate.
216 * (2) Allocate resources for all enabled devices. If there is
217 * a collision, just mark the resource as unallocated. Also
218 * disable expansion ROMs during this step.
219 * (3) Try to allocate resources for disabled devices. If the
220 * resources were assigned correctly, everything goes well,
221 * if they weren't, they won't disturb allocation of other
222 * resources.
223 * (4) Assign new addresses to resources which were either
224 * not configured at all or misconfigured. If explicitly
225 * requested by the user, configure expansion ROM address
226 * as well.
227 */
228
229static void __init
230pcibios_allocate_bus_resources(struct list_head *bus_list)
231{
232 struct pci_bus *bus;
233 int i;
234 struct resource *res, *pr;
235
236 /* Depth-First Search on bus tree */
237 list_for_each_entry(bus, bus_list, node) {
238 for (i = 0; i < 4; ++i) {
239 if ((res = bus->resource[i]) == NULL || !res->flags
240 || res->start > res->end)
241 continue;
242 if (bus->parent == NULL)
243 pr = (res->flags & IORESOURCE_IO)?
244 &ioport_resource: &iomem_resource;
245 else {
246 pr = pci_find_parent_resource(bus->self, res);
247 if (pr == res) {
248 /* this happens when the generic PCI
249 * code (wrongly) decides that this
250 * bridge is transparent -- paulus
251 */
252 continue;
253 }
254 }
255
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700256 DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
257 (unsigned long long)res->start,
258 (unsigned long long)res->end, res->flags, pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 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;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700308 DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
309 p->name, (unsigned long long)p->start,
310 (unsigned long long)p->end, res->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
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",
366 (unsigned long long)res->start,
367 (unsigned long long)res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -1; /* "can't happen" */
369 }
370 update_bridge_base(bus, i);
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700371 printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n",
372 bus->number, i, (unsigned long long)res->start,
373 (unsigned long long)res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
377static int __init
378probe_resource(struct pci_bus *parent, struct resource *pr,
379 struct resource *res, struct resource **conflict)
380{
381 struct pci_bus *bus;
382 struct pci_dev *dev;
383 struct resource *r;
384 int i;
385
386 for (r = pr->child; r != NULL; r = r->sibling) {
387 if (r->end >= res->start && res->end >= r->start) {
388 *conflict = r;
389 return 1;
390 }
391 }
392 list_for_each_entry(bus, &parent->children, node) {
393 for (i = 0; i < 4; ++i) {
394 if ((r = bus->resource[i]) == NULL)
395 continue;
396 if (!r->flags || r->start > r->end || r == res)
397 continue;
398 if (pci_find_parent_resource(bus->self, r) != pr)
399 continue;
400 if (r->end >= res->start && res->end >= r->start) {
401 *conflict = r;
402 return 1;
403 }
404 }
405 }
406 list_for_each_entry(dev, &parent->devices, bus_list) {
407 for (i = 0; i < 6; ++i) {
408 r = &dev->resource[i];
409 if (!r->flags || (r->flags & IORESOURCE_UNSET))
410 continue;
411 if (pci_find_parent_resource(dev, r) != pr)
412 continue;
413 if (r->end >= res->start && res->end >= r->start) {
414 *conflict = r;
415 return 1;
416 }
417 }
418 }
419 return 0;
420}
421
422static void __init
423update_bridge_base(struct pci_bus *bus, int i)
424{
425 struct resource *res = bus->resource[i];
426 u8 io_base_lo, io_limit_lo;
427 u16 mem_base, mem_limit;
428 u16 cmd;
429 unsigned long start, end, off;
430 struct pci_dev *dev = bus->self;
431 struct pci_controller *hose = dev->sysdata;
432
433 if (!hose) {
434 printk("update_bridge_base: no hose?\n");
435 return;
436 }
437 pci_read_config_word(dev, PCI_COMMAND, &cmd);
438 pci_write_config_word(dev, PCI_COMMAND,
439 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
440 if (res->flags & IORESOURCE_IO) {
441 off = (unsigned long) hose->io_base_virt - isa_io_base;
442 start = res->start - off;
443 end = res->end - off;
444 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
445 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
446 if (end > 0xffff) {
447 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
448 start >> 16);
449 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
450 end >> 16);
451 io_base_lo |= PCI_IO_RANGE_TYPE_32;
452 } else
453 io_base_lo |= PCI_IO_RANGE_TYPE_16;
454 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
455 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
456
457 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
458 == IORESOURCE_MEM) {
459 off = hose->pci_mem_offset;
460 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
461 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
462 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
463 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
464
465 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
466 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
467 off = hose->pci_mem_offset;
468 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
469 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
470 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
471 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
472
473 } else {
474 DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
475 pci_name(dev), i, res->flags);
476 }
477 pci_write_config_word(dev, PCI_COMMAND, cmd);
478}
479
480static inline void alloc_resource(struct pci_dev *dev, int idx)
481{
482 struct resource *pr, *r = &dev->resource[idx];
483
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700484 DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
485 pci_name(dev), idx, (unsigned long long)r->start,
486 (unsigned long long)r->end, r->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 pr = pci_find_parent_resource(dev, r);
488 if (!pr || request_resource(pr, r) < 0) {
489 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
490 " of device %s\n", idx, pci_name(dev));
491 if (pr)
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700492 DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
493 pr, (unsigned long long)pr->start,
494 (unsigned long long)pr->end, pr->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* We'll assign a new address later */
496 r->flags |= IORESOURCE_UNSET;
497 r->end -= r->start;
498 r->start = 0;
499 }
500}
501
502static void __init
503pcibios_allocate_resources(int pass)
504{
505 struct pci_dev *dev = NULL;
506 int idx, disabled;
507 u16 command;
508 struct resource *r;
509
Jiri Slabycee02952005-11-06 23:39:34 -0800510 for_each_pci_dev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 pci_read_config_word(dev, PCI_COMMAND, &command);
512 for (idx = 0; idx < 6; idx++) {
513 r = &dev->resource[idx];
514 if (r->parent) /* Already allocated */
515 continue;
516 if (!r->flags || (r->flags & IORESOURCE_UNSET))
517 continue; /* Not assigned at all */
518 if (r->flags & IORESOURCE_IO)
519 disabled = !(command & PCI_COMMAND_IO);
520 else
521 disabled = !(command & PCI_COMMAND_MEMORY);
522 if (pass == disabled)
523 alloc_resource(dev, idx);
524 }
525 if (pass)
526 continue;
527 r = &dev->resource[PCI_ROM_RESOURCE];
528 if (r->flags & IORESOURCE_ROM_ENABLE) {
529 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
530 u32 reg;
531 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
532 r->flags &= ~IORESOURCE_ROM_ENABLE;
533 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
534 pci_write_config_dword(dev, dev->rom_base_reg,
535 reg & ~PCI_ROM_ADDRESS_ENABLE);
536 }
537 }
538}
539
540static void __init
541pcibios_assign_resources(void)
542{
543 struct pci_dev *dev = NULL;
544 int idx;
545 struct resource *r;
546
Jiri Slabycee02952005-11-06 23:39:34 -0800547 for_each_pci_dev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int class = dev->class >> 8;
549
550 /* Don't touch classless devices and host bridges */
551 if (!class || class == PCI_CLASS_BRIDGE_HOST)
552 continue;
553
554 for (idx = 0; idx < 6; idx++) {
555 r = &dev->resource[idx];
556
557 /*
558 * We shall assign a new address to this resource,
559 * either because the BIOS (sic) forgot to do so
560 * or because we have decided the old address was
561 * unusable for some reason.
562 */
563 if ((r->flags & IORESOURCE_UNSET) && r->end &&
564 (!ppc_md.pcibios_enable_device_hook ||
565 !ppc_md.pcibios_enable_device_hook(dev, 1))) {
566 r->flags &= ~IORESOURCE_UNSET;
567 pci_assign_resource(dev, idx);
568 }
569 }
570
571#if 0 /* don't assign ROMs */
572 r = &dev->resource[PCI_ROM_RESOURCE];
573 r->end -= r->start;
574 r->start = 0;
575 if (r->end)
576 pci_assign_resource(dev, PCI_ROM_RESOURCE);
577#endif
578 }
579}
580
581
582int
583pcibios_enable_resources(struct pci_dev *dev, int mask)
584{
585 u16 cmd, old_cmd;
586 int idx;
587 struct resource *r;
588
589 pci_read_config_word(dev, PCI_COMMAND, &cmd);
590 old_cmd = cmd;
591 for (idx=0; idx<6; idx++) {
592 /* Only set up the requested stuff */
593 if (!(mask & (1<<idx)))
594 continue;
595
596 r = &dev->resource[idx];
597 if (r->flags & IORESOURCE_UNSET) {
598 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
599 return -EINVAL;
600 }
601 if (r->flags & IORESOURCE_IO)
602 cmd |= PCI_COMMAND_IO;
603 if (r->flags & IORESOURCE_MEM)
604 cmd |= PCI_COMMAND_MEMORY;
605 }
606 if (dev->resource[PCI_ROM_RESOURCE].start)
607 cmd |= PCI_COMMAND_MEMORY;
608 if (cmd != old_cmd) {
609 printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
610 pci_write_config_word(dev, PCI_COMMAND, cmd);
611 }
612 return 0;
613}
614
615static int next_controller_index;
616
617struct pci_controller * __init
618pcibios_alloc_controller(void)
619{
620 struct pci_controller *hose;
621
622 hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
623 memset(hose, 0, sizeof(struct pci_controller));
624
625 *hose_tail = hose;
626 hose_tail = &hose->next;
627
628 hose->index = next_controller_index++;
629
630 return hose;
631}
632
Paul Mackerrasfd582ec2005-10-11 22:08:12 +1000633void pcibios_make_OF_bus_map(void)
634{
635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637/* Add sysfs properties */
638void pcibios_add_platform_entries(struct pci_dev *pdev)
639{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643static int __init
644pcibios_init(void)
645{
646 struct pci_controller *hose;
647 struct pci_bus *bus;
648 int next_busno;
649
650 printk(KERN_INFO "PCI: Probing PCI hardware\n");
651
652 /* Scan all of the recorded PCI controllers. */
653 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
Paul Mackerras399fe2b2005-10-20 20:57:05 +1000654 if (pci_assign_all_buses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 hose->first_busno = next_busno;
656 hose->last_busno = 0xff;
657 bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
658 hose->last_busno = bus->subordinate;
Paul Mackerras399fe2b2005-10-20 20:57:05 +1000659 if (pci_assign_all_buses || next_busno <= hose->last_busno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 next_busno = hose->last_busno + pcibios_assign_bus_offset;
661 }
662 pci_bus_count = next_busno;
663
664 /* OpenFirmware based machines need a map of OF bus
665 * numbers vs. kernel bus numbers since we may have to
666 * remap them.
667 */
Paul Mackerras399fe2b2005-10-20 20:57:05 +1000668 if (pci_assign_all_buses && have_of)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 pcibios_make_OF_bus_map();
670
671 /* Do machine dependent PCI interrupt routing */
672 if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
673 pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
674
675 /* Call machine dependent fixup */
676 if (ppc_md.pcibios_fixup)
677 ppc_md.pcibios_fixup();
678
679 /* Allocate and assign resources */
680 pcibios_allocate_bus_resources(&pci_root_buses);
681 pcibios_allocate_resources(0);
682 pcibios_allocate_resources(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 pcibios_assign_resources();
684
685 /* Call machine dependent post-init code */
686 if (ppc_md.pcibios_after_init)
687 ppc_md.pcibios_after_init();
688
689 return 0;
690}
691
692subsys_initcall(pcibios_init);
693
694unsigned char __init
695common_swizzle(struct pci_dev *dev, unsigned char *pinp)
696{
697 struct pci_controller *hose = dev->sysdata;
698
699 if (dev->bus->number != hose->first_busno) {
700 u8 pin = *pinp;
701 do {
702 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
703 /* Move up the chain of bridges. */
704 dev = dev->bus->self;
705 } while (dev->bus->self);
706 *pinp = pin;
707
708 /* The slot is the idsel of the last bridge. */
709 }
710 return PCI_SLOT(dev->devfn);
711}
712
713unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
714 unsigned long start, unsigned long size)
715{
716 return start;
717}
718
719void __init pcibios_fixup_bus(struct pci_bus *bus)
720{
721 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
722 unsigned long io_offset;
723 struct resource *res;
724 int i;
725
726 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
727 if (bus->parent == NULL) {
728 /* This is a host bridge - fill in its resources */
729 hose->bus = bus;
730
731 bus->resource[0] = res = &hose->io_resource;
732 if (!res->flags) {
733 if (io_offset)
734 printk(KERN_ERR "I/O resource not set for host"
735 " bridge %d\n", hose->index);
736 res->start = 0;
737 res->end = IO_SPACE_LIMIT;
738 res->flags = IORESOURCE_IO;
739 }
740 res->start += io_offset;
741 res->end += io_offset;
742
743 for (i = 0; i < 3; ++i) {
744 res = &hose->mem_resources[i];
745 if (!res->flags) {
746 if (i > 0)
747 continue;
748 printk(KERN_ERR "Memory resource not set for "
749 "host bridge %d\n", hose->index);
750 res->start = hose->pci_mem_offset;
751 res->end = ~0U;
752 res->flags = IORESOURCE_MEM;
753 }
754 bus->resource[i+1] = res;
755 }
756 } else {
757 /* This is a subordinate bridge */
758 pci_read_bridge_bases(bus);
759
760 for (i = 0; i < 4; ++i) {
761 if ((res = bus->resource[i]) == NULL)
762 continue;
763 if (!res->flags)
764 continue;
765 if (io_offset && (res->flags & IORESOURCE_IO)) {
766 res->start += io_offset;
767 res->end += io_offset;
768 } else if (hose->pci_mem_offset
769 && (res->flags & IORESOURCE_MEM)) {
770 res->start += hose->pci_mem_offset;
771 res->end += hose->pci_mem_offset;
772 }
773 }
774 }
775
776 if (ppc_md.pcibios_fixup_bus)
777 ppc_md.pcibios_fixup_bus(bus);
778}
779
780char __init *pcibios_setup(char *str)
781{
782 return str;
783}
784
785/* the next one is stolen from the alpha port... */
786void __init
787pcibios_update_irq(struct pci_dev *dev, int irq)
788{
789 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
790 /* XXX FIXME - update OF device tree node interrupt property */
791}
792
793int pcibios_enable_device(struct pci_dev *dev, int mask)
794{
795 u16 cmd, old_cmd;
796 int idx;
797 struct resource *r;
798
799 if (ppc_md.pcibios_enable_device_hook)
800 if (ppc_md.pcibios_enable_device_hook(dev, 0))
801 return -EINVAL;
802
803 pci_read_config_word(dev, PCI_COMMAND, &cmd);
804 old_cmd = cmd;
805 for (idx=0; idx<6; idx++) {
806 r = &dev->resource[idx];
807 if (r->flags & IORESOURCE_UNSET) {
808 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
809 return -EINVAL;
810 }
811 if (r->flags & IORESOURCE_IO)
812 cmd |= PCI_COMMAND_IO;
813 if (r->flags & IORESOURCE_MEM)
814 cmd |= PCI_COMMAND_MEMORY;
815 }
816 if (cmd != old_cmd) {
817 printk("PCI: Enabling device %s (%04x -> %04x)\n",
818 pci_name(dev), old_cmd, cmd);
819 pci_write_config_word(dev, PCI_COMMAND, cmd);
820 }
821 return 0;
822}
823
824struct pci_controller*
825pci_bus_to_hose(int bus)
826{
827 struct pci_controller* hose = hose_head;
828
829 for (; hose; hose = hose->next)
830 if (bus >= hose->first_busno && bus <= hose->last_busno)
831 return hose;
832 return NULL;
833}
834
Al Viro92a11f92005-04-25 07:55:57 -0700835void __iomem *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836pci_bus_io_base(unsigned int bus)
837{
838 struct pci_controller *hose;
839
840 hose = pci_bus_to_hose(bus);
841 if (!hose)
842 return NULL;
843 return hose->io_base_virt;
844}
845
846unsigned long
847pci_bus_io_base_phys(unsigned int bus)
848{
849 struct pci_controller *hose;
850
851 hose = pci_bus_to_hose(bus);
852 if (!hose)
853 return 0;
854 return hose->io_base_phys;
855}
856
857unsigned long
858pci_bus_mem_base_phys(unsigned int bus)
859{
860 struct pci_controller *hose;
861
862 hose = pci_bus_to_hose(bus);
863 if (!hose)
864 return 0;
865 return hose->pci_mem_offset;
866}
867
868unsigned long
869pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
870{
871 /* Hack alert again ! See comments in chrp_pci.c
872 */
873 struct pci_controller* hose =
874 (struct pci_controller *)pdev->sysdata;
875 if (hose && res->flags & IORESOURCE_MEM)
876 return res->start - hose->pci_mem_offset;
877 /* We may want to do something with IOs here... */
878 return res->start;
879}
880
881
882static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
883 unsigned long *offset,
884 enum pci_mmap_state mmap_state)
885{
886 struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
887 unsigned long io_offset = 0;
888 int i, res_bit;
889
890 if (hose == 0)
891 return NULL; /* should never happen */
892
893 /* If memory, add on the PCI bridge address offset */
894 if (mmap_state == pci_mmap_mem) {
895 *offset += hose->pci_mem_offset;
896 res_bit = IORESOURCE_MEM;
897 } else {
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000898 io_offset = hose->io_base_virt - ___IO_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *offset += io_offset;
900 res_bit = IORESOURCE_IO;
901 }
902
903 /*
904 * Check that the offset requested corresponds to one of the
905 * resources of the device.
906 */
907 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
908 struct resource *rp = &dev->resource[i];
909 int flags = rp->flags;
910
911 /* treat ROM as memory (should be already) */
912 if (i == PCI_ROM_RESOURCE)
913 flags |= IORESOURCE_MEM;
914
915 /* Active and same type? */
916 if ((flags & res_bit) == 0)
917 continue;
918
919 /* In the range of this resource? */
920 if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
921 continue;
922
923 /* found it! construct the final physical address */
924 if (mmap_state == pci_mmap_io)
Michael Ellerman2311b1f2005-05-13 17:44:10 +1000925 *offset += hose->io_base_phys - io_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return rp;
927 }
928
929 return NULL;
930}
931
932/*
933 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
934 * device mapping.
935 */
936static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
937 pgprot_t protection,
938 enum pci_mmap_state mmap_state,
939 int write_combine)
940{
941 unsigned long prot = pgprot_val(protection);
942
943 /* Write combine is always 0 on non-memory space mappings. On
944 * memory space, if the user didn't pass 1, we check for a
945 * "prefetchable" resource. This is a bit hackish, but we use
946 * this to workaround the inability of /sysfs to provide a write
947 * combine bit
948 */
949 if (mmap_state != pci_mmap_mem)
950 write_combine = 0;
951 else if (write_combine == 0) {
952 if (rp->flags & IORESOURCE_PREFETCH)
953 write_combine = 1;
954 }
955
956 /* XXX would be nice to have a way to ask for write-through */
957 prot |= _PAGE_NO_CACHE;
958 if (write_combine)
959 prot &= ~_PAGE_GUARDED;
960 else
961 prot |= _PAGE_GUARDED;
962
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700963 printk("PCI map for %s:%llx, prot: %llx\n", pci_name(dev), rp->start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 prot);
965
966 return __pgprot(prot);
967}
968
969/*
970 * This one is used by /dev/mem and fbdev who have no clue about the
971 * PCI device, it tries to find the PCI device first and calls the
972 * above routine
973 */
974pgprot_t pci_phys_mem_access_prot(struct file *file,
Roland Dreier8b150472005-10-28 17:46:18 -0700975 unsigned long pfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 unsigned long size,
977 pgprot_t protection)
978{
979 struct pci_dev *pdev = NULL;
980 struct resource *found = NULL;
981 unsigned long prot = pgprot_val(protection);
Roland Dreier8b150472005-10-28 17:46:18 -0700982 unsigned long offset = pfn << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int i;
984
Roland Dreier8b150472005-10-28 17:46:18 -0700985 if (page_is_ram(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return prot;
987
988 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
989
990 for_each_pci_dev(pdev) {
991 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
992 struct resource *rp = &pdev->resource[i];
993 int flags = rp->flags;
994
995 /* Active and same type? */
996 if ((flags & IORESOURCE_MEM) == 0)
997 continue;
998 /* In the range of this resource? */
999 if (offset < (rp->start & PAGE_MASK) ||
1000 offset > rp->end)
1001 continue;
1002 found = rp;
1003 break;
1004 }
1005 if (found)
1006 break;
1007 }
1008 if (found) {
1009 if (found->flags & IORESOURCE_PREFETCH)
1010 prot &= ~_PAGE_GUARDED;
1011 pci_dev_put(pdev);
1012 }
1013
1014 DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
1015
1016 return __pgprot(prot);
1017}
1018
1019
1020/*
1021 * Perform the actual remap of the pages for a PCI device mapping, as
1022 * appropriate for this architecture. The region in the process to map
1023 * is described by vm_start and vm_end members of VMA, the base physical
1024 * address is found in vm_pgoff.
1025 * The pci device structure is provided so that architectures may make mapping
1026 * decisions on a per-device or per-bus basis.
1027 *
1028 * Returns a negative error code on failure, zero on success.
1029 */
1030int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1031 enum pci_mmap_state mmap_state,
1032 int write_combine)
1033{
1034 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1035 struct resource *rp;
1036 int ret;
1037
1038 rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
1039 if (rp == NULL)
1040 return -EINVAL;
1041
1042 vma->vm_pgoff = offset >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
1044 vma->vm_page_prot,
1045 mmap_state, write_combine);
1046
1047 ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1048 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1049
1050 return ret;
1051}
1052
1053/* Obsolete functions. Should be removed once the symbios driver
1054 * is fixed
1055 */
1056unsigned long
1057phys_to_bus(unsigned long pa)
1058{
1059 struct pci_controller *hose;
1060 int i;
1061
1062 for (hose = hose_head; hose; hose = hose->next) {
1063 for (i = 0; i < 3; ++i) {
1064 if (pa >= hose->mem_resources[i].start
1065 && pa <= hose->mem_resources[i].end) {
1066 /*
1067 * XXX the hose->pci_mem_offset really
1068 * only applies to mem_resources[0].
1069 * We need a way to store an offset for
1070 * the others. -- paulus
1071 */
1072 if (i == 0)
1073 pa -= hose->pci_mem_offset;
1074 return pa;
1075 }
1076 }
1077 }
1078 /* hmmm, didn't find it */
1079 return 0;
1080}
1081
1082unsigned long
1083pci_phys_to_bus(unsigned long pa, int busnr)
1084{
1085 struct pci_controller* hose = pci_bus_to_hose(busnr);
1086 if (!hose)
1087 return pa;
1088 return pa - hose->pci_mem_offset;
1089}
1090
1091unsigned long
1092pci_bus_to_phys(unsigned int ba, int busnr)
1093{
1094 struct pci_controller* hose = pci_bus_to_hose(busnr);
1095 if (!hose)
1096 return ba;
1097 return ba + hose->pci_mem_offset;
1098}
1099
1100/* Provide information on locations of various I/O regions in physical
1101 * memory. Do this on a per-card basis so that we choose the right
1102 * root bridge.
1103 * Note that the returned IO or memory base is a physical address
1104 */
1105
1106long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1107{
1108 struct pci_controller* hose;
1109 long result = -EOPNOTSUPP;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 hose = pci_bus_to_hose(bus);
1112 if (!hose)
1113 return -ENODEV;
1114
1115 switch (which) {
1116 case IOBASE_BRIDGE_NUMBER:
1117 return (long)hose->first_busno;
1118 case IOBASE_MEMORY:
1119 return (long)hose->pci_mem_offset;
1120 case IOBASE_IO:
1121 return (long)hose->io_base_phys;
1122 case IOBASE_ISA_IO:
1123 return (long)isa_io_base;
1124 case IOBASE_ISA_MEM:
1125 return (long)isa_mem_base;
1126 }
1127
1128 return result;
1129}
1130
Michael Ellerman2311b1f2005-05-13 17:44:10 +10001131void pci_resource_to_user(const struct pci_dev *dev, int bar,
1132 const struct resource *rsrc,
1133 u64 *start, u64 *end)
1134{
1135 struct pci_controller *hose = pci_bus_to_hose(dev->bus->number);
1136 unsigned long offset = 0;
1137
1138 if (hose == NULL)
1139 return;
1140
1141 if (rsrc->flags & IORESOURCE_IO)
1142 offset = ___IO_BASE - hose->io_base_virt + hose->io_base_phys;
1143
1144 *start = rsrc->start + offset;
1145 *end = rsrc->end + offset;
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148void __init
1149pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
1150 int flags, char *name)
1151{
1152 res->start = start;
1153 res->end = end;
1154 res->flags = flags;
1155 res->name = name;
1156 res->parent = NULL;
1157 res->sibling = NULL;
1158 res->child = NULL;
1159}
1160
1161void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max)
1162{
1163 unsigned long start = pci_resource_start(dev, bar);
1164 unsigned long len = pci_resource_len(dev, bar);
1165 unsigned long flags = pci_resource_flags(dev, bar);
1166
1167 if (!len)
1168 return NULL;
1169 if (max && len > max)
1170 len = max;
1171 if (flags & IORESOURCE_IO)
1172 return ioport_map(start, len);
1173 if (flags & IORESOURCE_MEM)
1174 /* Not checking IORESOURCE_CACHEABLE because PPC does
1175 * not currently distinguish between ioremap and
1176 * ioremap_nocache.
1177 */
1178 return ioremap(start, len);
1179 /* What? */
1180 return NULL;
1181}
1182
1183void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
1184{
1185 /* Nothing to do */
1186}
1187EXPORT_SYMBOL(pci_iomap);
1188EXPORT_SYMBOL(pci_iounmap);
1189
Benjamin Herrenschmidtf2c45832005-12-15 15:00:57 +11001190unsigned long pci_address_to_pio(phys_addr_t address)
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +11001191{
1192 struct pci_controller* hose = hose_head;
1193
1194 for (; hose; hose = hose->next) {
1195 unsigned int size = hose->io_resource.end -
1196 hose->io_resource.start + 1;
1197 if (address >= hose->io_base_phys &&
Benjamin Herrenschmidtf2c45832005-12-15 15:00:57 +11001198 address < (hose->io_base_phys + size)) {
1199 unsigned long base =
1200 (unsigned long)hose->io_base_virt - _IO_BASE;
1201 return base + (address - hose->io_base_phys);
Kumar Galae5cd0402005-12-19 15:49:07 -06001202 }
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +11001203 }
1204 return (unsigned int)-1;
1205}
1206EXPORT_SYMBOL(pci_address_to_pio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208/*
1209 * Null PCI config access functions, for the case when we can't
1210 * find a hose.
1211 */
1212#define NULL_PCI_OP(rw, size, type) \
1213static int \
1214null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1215{ \
1216 return PCIBIOS_DEVICE_NOT_FOUND; \
1217}
1218
1219static int
1220null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1221 int len, u32 *val)
1222{
1223 return PCIBIOS_DEVICE_NOT_FOUND;
1224}
1225
1226static int
1227null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1228 int len, u32 val)
1229{
1230 return PCIBIOS_DEVICE_NOT_FOUND;
1231}
1232
1233static struct pci_ops null_pci_ops =
1234{
1235 null_read_config,
1236 null_write_config
1237};
1238
1239/*
1240 * These functions are used early on before PCI scanning is done
1241 * and all of the pci_dev and pci_bus structures have been created.
1242 */
1243static struct pci_bus *
1244fake_pci_bus(struct pci_controller *hose, int busnr)
1245{
1246 static struct pci_bus bus;
1247
1248 if (hose == 0) {
1249 hose = pci_bus_to_hose(busnr);
1250 if (hose == 0)
1251 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1252 }
1253 bus.number = busnr;
1254 bus.sysdata = hose;
1255 bus.ops = hose? hose->ops: &null_pci_ops;
1256 return &bus;
1257}
1258
1259#define EARLY_PCI_OP(rw, size, type) \
1260int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1261 int devfn, int offset, type value) \
1262{ \
1263 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1264 devfn, offset, value); \
1265}
1266
1267EARLY_PCI_OP(read, byte, u8 *)
1268EARLY_PCI_OP(read, word, u16 *)
1269EARLY_PCI_OP(read, dword, u32 *)
1270EARLY_PCI_OP(write, byte, u8)
1271EARLY_PCI_OP(write, word, u16)
1272EARLY_PCI_OP(write, dword, u32)