blob: 096c97e03aa655d1e723cc579da9252929e7cad6 [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
Kumar Galaa4c9e322007-06-27 13:09:43 -050058LIST_HEAD(hose_list);
Paul Mackerrase05b3b42006-01-15 22:05:47 +110059
60static int pci_bus_count;
61
62static void
Kumar Gala2052d6d2007-07-25 00:44:11 -050063fixup_hide_host_resource_fsl(struct pci_dev* dev)
64{
65 int i, class = dev->class >> 8;
66
67 if ((class == PCI_CLASS_PROCESSOR_POWERPC) &&
68 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
69 (dev->bus->parent == NULL)) {
70 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
71 dev->resource[i].start = 0;
72 dev->resource[i].end = 0;
73 dev->resource[i].flags = 0;
74 }
75 }
76}
77DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
78DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
79
80static void
Paul Mackerrase05b3b42006-01-15 22:05:47 +110081fixup_broken_pcnet32(struct pci_dev* dev)
82{
83 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
84 dev->vendor = PCI_VENDOR_ID_AMD;
85 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
86 }
87}
88DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
89
90static void
91fixup_cpc710_pci64(struct pci_dev* dev)
92{
93 /* Hide the PCI64 BARs from the kernel as their content doesn't
94 * fit well in the resource management
95 */
96 dev->resource[0].start = dev->resource[0].end = 0;
97 dev->resource[0].flags = 0;
98 dev->resource[1].start = dev->resource[1].end = 0;
99 dev->resource[1].flags = 0;
100}
101DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
102
103static void
104pcibios_fixup_resources(struct pci_dev *dev)
105{
106 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
107 int i;
108 unsigned long offset;
109
110 if (!hose) {
111 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
112 return;
113 }
114 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
115 struct resource *res = dev->resource + i;
116 if (!res->flags)
117 continue;
118 if (res->end == 0xffffffff) {
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700119 DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300120 pci_name(dev), i, (u64)res->start, (u64)res->end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100121 res->end -= res->start;
122 res->start = 0;
123 res->flags |= IORESOURCE_UNSET;
124 continue;
125 }
126 offset = 0;
127 if (res->flags & IORESOURCE_MEM) {
128 offset = hose->pci_mem_offset;
129 } else if (res->flags & IORESOURCE_IO) {
130 offset = (unsigned long) hose->io_base_virt
131 - isa_io_base;
132 }
133 if (offset != 0) {
134 res->start += offset;
135 res->end += offset;
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300136 DBG("Fixup res %d (%lx) of dev %s: %llx -> %llx\n",
137 i, res->flags, pci_name(dev),
138 (u64)res->start - offset, (u64)res->start);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100139 }
140 }
141
142 /* Call machine specific resource fixup */
143 if (ppc_md.pcibios_fixup_resources)
144 ppc_md.pcibios_fixup_resources(dev);
145}
146DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
147
148void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
149 struct resource *res)
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 region->start = res->start - offset;
159 region->end = res->end - offset;
160}
161EXPORT_SYMBOL(pcibios_resource_to_bus);
162
163void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
164 struct pci_bus_region *region)
165{
166 unsigned long offset = 0;
167 struct pci_controller *hose = dev->sysdata;
168
169 if (hose && res->flags & IORESOURCE_IO)
170 offset = (unsigned long)hose->io_base_virt - isa_io_base;
171 else if (hose && res->flags & IORESOURCE_MEM)
172 offset = hose->pci_mem_offset;
173 res->start = region->start + offset;
174 res->end = region->end + offset;
175}
176EXPORT_SYMBOL(pcibios_bus_to_resource);
177
178/*
179 * We need to avoid collisions with `mirrored' VGA ports
180 * and other strange ISA hardware, so we always want the
181 * addresses to be allocated in the 0x000-0x0ff region
182 * modulo 0x400.
183 *
184 * Why? Because some silly external IO cards only decode
185 * the low 10 bits of the IO address. The 0x00-0xff region
186 * is reserved for motherboard devices that decode all 16
187 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
188 * but we want to try to avoid allocating at 0x2900-0x2bff
189 * which might have be mirrored at 0x0100-0x03ff..
190 */
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700191void pcibios_align_resource(void *data, struct resource *res,
192 resource_size_t size, resource_size_t align)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100193{
194 struct pci_dev *dev = data;
195
196 if (res->flags & IORESOURCE_IO) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700197 resource_size_t start = res->start;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100198
199 if (size > 0x100) {
200 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700201 " (%lld bytes)\n", pci_name(dev),
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700202 dev->resource - res, (unsigned long long)size);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100203 }
204
205 if (start & 0x300) {
206 start = (start + 0x3ff) & ~0x3ff;
207 res->start = start;
208 }
209 }
210}
211EXPORT_SYMBOL(pcibios_align_resource);
212
213/*
214 * Handle resources of PCI devices. If the world were perfect, we could
215 * just allocate all the resource regions and do nothing more. It isn't.
216 * On the other hand, we cannot just re-allocate all devices, as it would
217 * require us to know lots of host bridge internals. So we attempt to
218 * keep as much of the original configuration as possible, but tweak it
219 * when it's found to be wrong.
220 *
221 * Known BIOS problems we have to work around:
222 * - I/O or memory regions not configured
223 * - regions configured, but not enabled in the command register
224 * - bogus I/O addresses above 64K used
225 * - expansion ROMs left enabled (this may sound harmless, but given
226 * the fact the PCI specs explicitly allow address decoders to be
227 * shared between expansion ROMs and other resource regions, it's
228 * at least dangerous)
229 *
230 * Our solution:
231 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
232 * This gives us fixed barriers on where we can allocate.
233 * (2) Allocate resources for all enabled devices. If there is
234 * a collision, just mark the resource as unallocated. Also
235 * disable expansion ROMs during this step.
236 * (3) Try to allocate resources for disabled devices. If the
237 * resources were assigned correctly, everything goes well,
238 * if they weren't, they won't disturb allocation of other
239 * resources.
240 * (4) Assign new addresses to resources which were either
241 * not configured at all or misconfigured. If explicitly
242 * requested by the user, configure expansion ROM address
243 * as well.
244 */
245
246static void __init
247pcibios_allocate_bus_resources(struct list_head *bus_list)
248{
249 struct pci_bus *bus;
250 int i;
251 struct resource *res, *pr;
252
253 /* Depth-First Search on bus tree */
254 list_for_each_entry(bus, bus_list, node) {
255 for (i = 0; i < 4; ++i) {
256 if ((res = bus->resource[i]) == NULL || !res->flags
257 || res->start > res->end)
258 continue;
259 if (bus->parent == NULL)
260 pr = (res->flags & IORESOURCE_IO)?
261 &ioport_resource: &iomem_resource;
262 else {
263 pr = pci_find_parent_resource(bus->self, res);
264 if (pr == res) {
265 /* this happens when the generic PCI
266 * code (wrongly) decides that this
267 * bridge is transparent -- paulus
268 */
269 continue;
270 }
271 }
272
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700273 DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300274 (u64)res->start, (u64)res->end, res->flags, pr);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100275 if (pr) {
276 if (request_resource(pr, res) == 0)
277 continue;
278 /*
279 * Must be a conflict with an existing entry.
280 * Move that entry (or entries) under the
281 * bridge resource and try again.
282 */
283 if (reparent_resources(pr, res) == 0)
284 continue;
285 }
286 printk(KERN_ERR "PCI: Cannot allocate resource region "
287 "%d of PCI bridge %d\n", i, bus->number);
288 if (pci_relocate_bridge_resource(bus, i))
289 bus->resource[i] = NULL;
290 }
291 pcibios_allocate_bus_resources(&bus->children);
292 }
293}
294
295/*
296 * Reparent resource children of pr that conflict with res
297 * under res, and make res replace those children.
298 */
299static int __init
300reparent_resources(struct resource *parent, struct resource *res)
301{
302 struct resource *p, **pp;
303 struct resource **firstpp = NULL;
304
305 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
306 if (p->end < res->start)
307 continue;
308 if (res->end < p->start)
309 break;
310 if (p->start < res->start || p->end > res->end)
311 return -1; /* not completely contained */
312 if (firstpp == NULL)
313 firstpp = pp;
314 }
315 if (firstpp == NULL)
316 return -1; /* didn't find any conflicting entries? */
317 res->parent = parent;
318 res->child = *firstpp;
319 res->sibling = *pp;
320 *firstpp = res;
321 *pp = NULL;
322 for (p = res->child; p != NULL; p = p->sibling) {
323 p->parent = res;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700324 DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300325 p->name, (u64)p->start, (u64)p->end, res->name);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100326 }
327 return 0;
328}
329
330/*
331 * A bridge has been allocated a range which is outside the range
332 * of its parent bridge, so it needs to be moved.
333 */
334static int __init
335pci_relocate_bridge_resource(struct pci_bus *bus, int i)
336{
337 struct resource *res, *pr, *conflict;
338 unsigned long try, size;
339 int j;
340 struct pci_bus *parent = bus->parent;
341
342 if (parent == NULL) {
343 /* shouldn't ever happen */
344 printk(KERN_ERR "PCI: can't move host bridge resource\n");
345 return -1;
346 }
347 res = bus->resource[i];
348 if (res == NULL)
349 return -1;
350 pr = NULL;
351 for (j = 0; j < 4; j++) {
352 struct resource *r = parent->resource[j];
353 if (!r)
354 continue;
355 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
356 continue;
357 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
358 pr = r;
359 break;
360 }
361 if (res->flags & IORESOURCE_PREFETCH)
362 pr = r;
363 }
364 if (pr == NULL)
365 return -1;
366 size = res->end - res->start;
367 if (pr->start > pr->end || size > pr->end - pr->start)
368 return -1;
369 try = pr->end;
370 for (;;) {
371 res->start = try - size;
372 res->end = try;
373 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
374 break;
375 if (conflict->start <= pr->start + size)
376 return -1;
377 try = conflict->start - 1;
378 }
379 if (request_resource(pr, res)) {
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700380 DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300381 (u64)res->start, (u64)res->end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100382 return -1; /* "can't happen" */
383 }
384 update_bridge_base(bus, i);
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700385 printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n",
386 bus->number, i, (unsigned long long)res->start,
387 (unsigned long long)res->end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100388 return 0;
389}
390
391static int __init
392probe_resource(struct pci_bus *parent, struct resource *pr,
393 struct resource *res, struct resource **conflict)
394{
395 struct pci_bus *bus;
396 struct pci_dev *dev;
397 struct resource *r;
398 int i;
399
400 for (r = pr->child; r != NULL; r = r->sibling) {
401 if (r->end >= res->start && res->end >= r->start) {
402 *conflict = r;
403 return 1;
404 }
405 }
406 list_for_each_entry(bus, &parent->children, node) {
407 for (i = 0; i < 4; ++i) {
408 if ((r = bus->resource[i]) == NULL)
409 continue;
410 if (!r->flags || r->start > r->end || r == res)
411 continue;
412 if (pci_find_parent_resource(bus->self, r) != pr)
413 continue;
414 if (r->end >= res->start && res->end >= r->start) {
415 *conflict = r;
416 return 1;
417 }
418 }
419 }
420 list_for_each_entry(dev, &parent->devices, bus_list) {
421 for (i = 0; i < 6; ++i) {
422 r = &dev->resource[i];
423 if (!r->flags || (r->flags & IORESOURCE_UNSET))
424 continue;
425 if (pci_find_parent_resource(dev, r) != pr)
426 continue;
427 if (r->end >= res->start && res->end >= r->start) {
428 *conflict = r;
429 return 1;
430 }
431 }
432 }
433 return 0;
434}
435
Kumar Galaf64fddb2007-07-20 13:35:34 -0500436void __init
437update_bridge_resource(struct pci_dev *dev, struct resource *res)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100438{
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100439 u8 io_base_lo, io_limit_lo;
440 u16 mem_base, mem_limit;
441 u16 cmd;
442 unsigned long start, end, off;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100443 struct pci_controller *hose = dev->sysdata;
444
445 if (!hose) {
446 printk("update_bridge_base: no hose?\n");
447 return;
448 }
449 pci_read_config_word(dev, PCI_COMMAND, &cmd);
450 pci_write_config_word(dev, PCI_COMMAND,
451 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
452 if (res->flags & IORESOURCE_IO) {
453 off = (unsigned long) hose->io_base_virt - isa_io_base;
454 start = res->start - off;
455 end = res->end - off;
456 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
457 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
Randy Vinson60b2a462006-10-12 13:36:23 -0700458 if (end > 0xffff)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100459 io_base_lo |= PCI_IO_RANGE_TYPE_32;
Randy Vinson60b2a462006-10-12 13:36:23 -0700460 else
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100461 io_base_lo |= PCI_IO_RANGE_TYPE_16;
Randy Vinson60b2a462006-10-12 13:36:23 -0700462 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
463 start >> 16);
464 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
465 end >> 16);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100466 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
467 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
468
469 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
470 == IORESOURCE_MEM) {
471 off = hose->pci_mem_offset;
472 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
473 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
474 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
475 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
476
477 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
478 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
479 off = hose->pci_mem_offset;
480 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
481 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
482 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
483 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
484
485 } else {
Kumar Galaf64fddb2007-07-20 13:35:34 -0500486 DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
487 pci_name(dev), res->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100488 }
489 pci_write_config_word(dev, PCI_COMMAND, cmd);
490}
491
Kumar Galaf64fddb2007-07-20 13:35:34 -0500492static void __init
493update_bridge_base(struct pci_bus *bus, int i)
494{
495 struct resource *res = bus->resource[i];
496 struct pci_dev *dev = bus->self;
497 update_bridge_resource(dev, res);
498}
499
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100500static inline void alloc_resource(struct pci_dev *dev, int idx)
501{
502 struct resource *pr, *r = &dev->resource[idx];
503
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700504 DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300505 pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100506 pr = pci_find_parent_resource(dev, r);
507 if (!pr || request_resource(pr, r) < 0) {
508 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
509 " of device %s\n", idx, pci_name(dev));
510 if (pr)
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700511 DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300512 pr, (u64)pr->start, (u64)pr->end, pr->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100513 /* We'll assign a new address later */
514 r->flags |= IORESOURCE_UNSET;
515 r->end -= r->start;
516 r->start = 0;
517 }
518}
519
520static void __init
521pcibios_allocate_resources(int pass)
522{
523 struct pci_dev *dev = NULL;
524 int idx, disabled;
525 u16 command;
526 struct resource *r;
527
528 for_each_pci_dev(dev) {
529 pci_read_config_word(dev, PCI_COMMAND, &command);
530 for (idx = 0; idx < 6; idx++) {
531 r = &dev->resource[idx];
532 if (r->parent) /* Already allocated */
533 continue;
534 if (!r->flags || (r->flags & IORESOURCE_UNSET))
535 continue; /* Not assigned at all */
536 if (r->flags & IORESOURCE_IO)
537 disabled = !(command & PCI_COMMAND_IO);
538 else
539 disabled = !(command & PCI_COMMAND_MEMORY);
540 if (pass == disabled)
541 alloc_resource(dev, idx);
542 }
543 if (pass)
544 continue;
545 r = &dev->resource[PCI_ROM_RESOURCE];
546 if (r->flags & IORESOURCE_ROM_ENABLE) {
547 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
548 u32 reg;
549 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
550 r->flags &= ~IORESOURCE_ROM_ENABLE;
551 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
552 pci_write_config_dword(dev, dev->rom_base_reg,
553 reg & ~PCI_ROM_ADDRESS_ENABLE);
554 }
555 }
556}
557
558static void __init
559pcibios_assign_resources(void)
560{
561 struct pci_dev *dev = NULL;
562 int idx;
563 struct resource *r;
564
565 for_each_pci_dev(dev) {
566 int class = dev->class >> 8;
567
568 /* Don't touch classless devices and host bridges */
569 if (!class || class == PCI_CLASS_BRIDGE_HOST)
570 continue;
571
572 for (idx = 0; idx < 6; idx++) {
573 r = &dev->resource[idx];
574
575 /*
576 * We shall assign a new address to this resource,
577 * either because the BIOS (sic) forgot to do so
578 * or because we have decided the old address was
579 * unusable for some reason.
580 */
581 if ((r->flags & IORESOURCE_UNSET) && r->end &&
582 (!ppc_md.pcibios_enable_device_hook ||
583 !ppc_md.pcibios_enable_device_hook(dev, 1))) {
Segher Boessenkool3a77d292007-08-02 01:41:13 +1000584 int rc;
585
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100586 r->flags &= ~IORESOURCE_UNSET;
Segher Boessenkool3a77d292007-08-02 01:41:13 +1000587 rc = pci_assign_resource(dev, idx);
588 BUG_ON(rc);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100589 }
590 }
591
592#if 0 /* don't assign ROMs */
593 r = &dev->resource[PCI_ROM_RESOURCE];
594 r->end -= r->start;
595 r->start = 0;
596 if (r->end)
597 pci_assign_resource(dev, PCI_ROM_RESOURCE);
598#endif
599 }
600}
601
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100602#ifdef CONFIG_PPC_OF
603/*
604 * Functions below are used on OpenFirmware machines.
605 */
606static void
607make_one_node_map(struct device_node* node, u8 pci_bus)
608{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000609 const int *bus_range;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100610 int len;
611
612 if (pci_bus >= pci_bus_count)
613 return;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000614 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100615 if (bus_range == NULL || len < 2 * sizeof(int)) {
616 printk(KERN_WARNING "Can't get bus-range for %s, "
617 "assuming it starts at 0\n", node->full_name);
618 pci_to_OF_bus_map[pci_bus] = 0;
619 } else
620 pci_to_OF_bus_map[pci_bus] = bus_range[0];
621
622 for (node=node->child; node != 0;node = node->sibling) {
623 struct pci_dev* dev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000624 const unsigned int *class_code, *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100625
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000626 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100627 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
628 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
629 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000630 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100631 if (!reg)
632 continue;
Alan Coxab462762007-04-23 14:47:59 +0100633 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
634 if (!dev || !dev->subordinate) {
635 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100636 continue;
Alan Coxab462762007-04-23 14:47:59 +0100637 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100638 make_one_node_map(node, dev->subordinate->number);
Alan Coxab462762007-04-23 14:47:59 +0100639 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100640 }
641}
642
643void
644pcibios_make_OF_bus_map(void)
645{
646 int i;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500647 struct pci_controller *hose, *tmp;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000648 struct property *map_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000649 struct device_node *dn;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100650
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800651 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100652 if (!pci_to_OF_bus_map) {
653 printk(KERN_ERR "Can't allocate OF bus map !\n");
654 return;
655 }
656
657 /* We fill the bus map with invalid values, that helps
658 * debugging.
659 */
660 for (i=0; i<pci_bus_count; i++)
661 pci_to_OF_bus_map[i] = 0xff;
662
663 /* For each hose, we begin searching bridges */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500664 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100665 struct device_node* node = hose->dn;
666
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100667 if (!node)
668 continue;
669 make_one_node_map(node, hose->first_busno);
670 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000671 dn = of_find_node_by_path("/");
672 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000673 if (map_prop) {
674 BUG_ON(pci_bus_count > map_prop->length);
675 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
676 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000677 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100678#ifdef DEBUG
679 printk("PCI->OF bus map:\n");
680 for (i=0; i<pci_bus_count; i++) {
681 if (pci_to_OF_bus_map[i] == 0xff)
682 continue;
683 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
684 }
685#endif
686}
687
688typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
689
690static struct device_node*
691scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
692{
693 struct device_node* sub_node;
694
695 for (; node != 0;node = node->sibling) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000696 const unsigned int *class_code;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100697
698 if (filter(node, data))
699 return node;
700
701 /* For PCI<->PCI bridges or CardBus bridges, we go down
702 * Note: some OFs create a parent node "multifunc-device" as
703 * a fake root for all functions of a multi-function device,
704 * we go down them as well.
705 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000706 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100707 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
708 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
709 strcmp(node->name, "multifunc-device"))
710 continue;
711 sub_node = scan_OF_pci_childs(node->child, filter, data);
712 if (sub_node)
713 return sub_node;
714 }
715 return NULL;
716}
717
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100718static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
719 unsigned int devfn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100720{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100721 struct device_node *np = NULL;
722 const u32 *reg;
723 unsigned int psize;
724
725 while ((np = of_get_next_child(parent, np)) != NULL) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000726 reg = of_get_property(np, "reg", &psize);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100727 if (reg == NULL || psize < 4)
728 continue;
729 if (((reg[0] >> 8) & 0xff) == devfn)
730 return np;
731 }
732 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100733}
734
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100735
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100736static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
737{
738 struct device_node *parent, *np;
739
740 /* Are we a root bus ? */
741 if (bus->self == NULL || bus->parent == NULL) {
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500742 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100743 if (hose == NULL)
744 return NULL;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100745 return of_node_get(hose->dn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100746 }
747
748 /* not a root bus, we need to get our parent */
749 parent = scan_OF_for_pci_bus(bus->parent);
750 if (parent == NULL)
751 return NULL;
752
753 /* now iterate for children for a match */
754 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
755 of_node_put(parent);
756
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100757 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100758}
759
760/*
761 * Scans the OF tree for a device node matching a PCI device
762 */
763struct device_node *
764pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
765{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100766 struct device_node *parent, *np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100767
768 if (!have_of)
769 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100770
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100771 DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
772 parent = scan_OF_for_pci_bus(bus);
773 if (parent == NULL)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100774 return NULL;
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100775 DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
776 np = scan_OF_for_pci_dev(parent, devfn);
777 of_node_put(parent);
778 DBG(" result is %s\n", np ? np->full_name : "<NULL>");
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100779
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100780 /* XXX most callers don't release the returned node
781 * mostly because ppc64 doesn't increase the refcount,
782 * we need to fix that.
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100783 */
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100784 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100785}
786EXPORT_SYMBOL(pci_busdev_to_OF_node);
787
788struct device_node*
789pci_device_to_OF_node(struct pci_dev *dev)
790{
791 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
792}
793EXPORT_SYMBOL(pci_device_to_OF_node);
794
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100795static int
796find_OF_pci_device_filter(struct device_node* node, void* data)
797{
798 return ((void *)node == data);
799}
800
801/*
802 * Returns the PCI device matching a given OF node
803 */
804int
805pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
806{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000807 const unsigned int *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100808 struct pci_controller* hose;
809 struct pci_dev* dev = NULL;
810
811 if (!have_of)
812 return -ENODEV;
813 /* Make sure it's really a PCI device */
814 hose = pci_find_hose_for_OF_device(node);
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100815 if (!hose || !hose->dn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100816 return -ENODEV;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100817 if (!scan_OF_pci_childs(hose->dn->child,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100818 find_OF_pci_device_filter, (void *)node))
819 return -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000820 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100821 if (!reg)
822 return -ENODEV;
823 *bus = (reg[0] >> 16) & 0xff;
824 *devfn = ((reg[0] >> 8) & 0xff);
825
826 /* Ok, here we need some tweak. If we have already renumbered
827 * all busses, we can't rely on the OF bus number any more.
828 * the pci_to_OF_bus_map is not enough as several PCI busses
829 * may match the same OF bus number.
830 */
831 if (!pci_to_OF_bus_map)
832 return 0;
833
834 for_each_pci_dev(dev)
835 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
836 dev->devfn == *devfn) {
837 *bus = dev->bus->number;
838 pci_dev_put(dev);
839 return 0;
840 }
841
842 return -ENODEV;
843}
844EXPORT_SYMBOL(pci_device_from_OF_node);
845
846void __init
847pci_process_bridge_OF_ranges(struct pci_controller *hose,
848 struct device_node *dev, int primary)
849{
850 static unsigned int static_lc_ranges[256] __initdata;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000851 const unsigned int *dt_ranges;
852 unsigned int *lc_ranges, *ranges, *prev, size;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100853 int rlen = 0, orig_rlen;
854 int memno = 0;
855 struct resource *res;
Stephen Rothwella8bda5d2007-04-03 10:56:50 +1000856 int np, na = of_n_addr_cells(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100857 np = na + 5;
858
859 /* First we try to merge ranges to fix a problem with some pmacs
860 * that can have more than 3 ranges, fortunately using contiguous
861 * addresses -- BenH
862 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000863 dt_ranges = of_get_property(dev, "ranges", &rlen);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100864 if (!dt_ranges)
865 return;
866 /* Sanity check, though hopefully that never happens */
867 if (rlen > sizeof(static_lc_ranges)) {
868 printk(KERN_WARNING "OF ranges property too large !\n");
869 rlen = sizeof(static_lc_ranges);
870 }
871 lc_ranges = static_lc_ranges;
872 memcpy(lc_ranges, dt_ranges, rlen);
873 orig_rlen = rlen;
874
875 /* Let's work on a copy of the "ranges" property instead of damaging
876 * the device-tree image in memory
877 */
878 ranges = lc_ranges;
879 prev = NULL;
880 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
881 if (prev) {
882 if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
883 (prev[2] + prev[na+4]) == ranges[2] &&
884 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
885 prev[na+4] += ranges[na+4];
886 ranges[0] = 0;
887 ranges += np;
888 continue;
889 }
890 }
891 prev = ranges;
892 ranges += np;
893 }
894
895 /*
896 * The ranges property is laid out as an array of elements,
897 * each of which comprises:
898 * cells 0 - 2: a PCI address
899 * cells 3 or 3+4: a CPU physical address
900 * (size depending on dev->n_addr_cells)
901 * cells 4+5 or 5+6: the size of the range
902 */
903 ranges = lc_ranges;
904 rlen = orig_rlen;
905 while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
906 res = NULL;
907 size = ranges[na+4];
908 switch ((ranges[0] >> 24) & 0x3) {
909 case 1: /* I/O space */
910 if (ranges[2] != 0)
911 break;
912 hose->io_base_phys = ranges[na+2];
913 /* limit I/O space to 16MB */
914 if (size > 0x01000000)
915 size = 0x01000000;
916 hose->io_base_virt = ioremap(ranges[na+2], size);
917 if (primary)
918 isa_io_base = (unsigned long) hose->io_base_virt;
919 res = &hose->io_resource;
920 res->flags = IORESOURCE_IO;
921 res->start = ranges[2];
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700922 DBG("PCI: IO 0x%llx -> 0x%llx\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300923 (u64)res->start, (u64)res->start + size - 1);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100924 break;
925 case 2: /* memory space */
926 memno = 0;
927 if (ranges[1] == 0 && ranges[2] == 0
928 && ranges[na+4] <= (16 << 20)) {
929 /* 1st 16MB, i.e. ISA memory area */
930 if (primary)
931 isa_mem_base = ranges[na+2];
932 memno = 1;
933 }
934 while (memno < 3 && hose->mem_resources[memno].flags)
935 ++memno;
936 if (memno == 0)
937 hose->pci_mem_offset = ranges[na+2] - ranges[2];
938 if (memno < 3) {
939 res = &hose->mem_resources[memno];
940 res->flags = IORESOURCE_MEM;
941 if(ranges[0] & 0x40000000)
942 res->flags |= IORESOURCE_PREFETCH;
943 res->start = ranges[na+2];
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700944 DBG("PCI: MEM[%d] 0x%llx -> 0x%llx\n", memno,
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300945 (u64)res->start, (u64)res->start + size - 1);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100946 }
947 break;
948 }
949 if (res != NULL) {
950 res->name = dev->full_name;
951 res->end = res->start + size - 1;
952 res->parent = NULL;
953 res->sibling = NULL;
954 res->child = NULL;
955 }
956 ranges += np;
957 }
958}
959
960/* We create the "pci-OF-bus-map" property now so it appears in the
961 * /proc device tree
962 */
963void __init
964pci_create_OF_bus_map(void)
965{
966 struct property* of_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000967 struct device_node *dn;
968
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100969 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000970 if (!of_prop)
971 return;
972 dn = of_find_node_by_path("/");
973 if (dn) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100974 memset(of_prop, -1, sizeof(struct property) + 256);
975 of_prop->name = "pci-OF-bus-map";
976 of_prop->length = 256;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000977 of_prop->value = &of_prop[1];
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000978 prom_add_property(dn, of_prop);
979 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100980 }
981}
982
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100983#else /* CONFIG_PPC_OF */
984void pcibios_make_OF_bus_map(void)
985{
986}
987#endif /* CONFIG_PPC_OF */
988
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100989#ifdef CONFIG_PPC_PMAC
990/*
991 * This set of routines checks for PCI<->PCI bridges that have closed
992 * IO resources and have child devices. It tries to re-open an IO
993 * window on them.
994 *
995 * This is a _temporary_ fix to workaround a problem with Apple's OF
996 * closing IO windows on P2P bridges when the OF drivers of cards
997 * below this bridge don't claim any IO range (typically ATI or
998 * Adaptec).
999 *
1000 * A more complete fix would be to use drivers/pci/setup-bus.c, which
1001 * involves a working pcibios_fixup_pbus_ranges(), some more care about
1002 * ordering when creating the host bus resources, and maybe a few more
1003 * minor tweaks
1004 */
1005
1006/* Initialize bridges with base/limit values we have collected */
1007static void __init
1008do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
1009{
1010 struct pci_dev *bridge = bus->self;
1011 struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
1012 u32 l;
1013 u16 w;
1014 struct resource res;
1015
1016 if (bus->resource[0] == NULL)
1017 return;
1018 res = *(bus->resource[0]);
1019
1020 DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
1021 res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
1022 res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -07001023 DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001024
1025 /* Set up the top and bottom of the PCI I/O segment for this bus. */
1026 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
1027 l &= 0xffff000f;
1028 l |= (res.start >> 8) & 0x00f0;
1029 l |= res.end & 0xf000;
1030 pci_write_config_dword(bridge, PCI_IO_BASE, l);
1031
1032 if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
1033 l = (res.start >> 16) | (res.end & 0xffff0000);
1034 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
1035 }
1036
1037 pci_read_config_word(bridge, PCI_COMMAND, &w);
1038 w |= PCI_COMMAND_IO;
1039 pci_write_config_word(bridge, PCI_COMMAND, w);
1040
1041#if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
1042 if (enable_vga) {
1043 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
1044 w |= PCI_BRIDGE_CTL_VGA;
1045 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
1046 }
1047#endif
1048}
1049
1050/* This function is pretty basic and actually quite broken for the
1051 * general case, it's enough for us right now though. It's supposed
1052 * to tell us if we need to open an IO range at all or not and what
1053 * size.
1054 */
1055static int __init
1056check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
1057{
1058 struct pci_dev *dev;
1059 int i;
1060 int rc = 0;
1061
Paul Mackerras0f582bc2006-06-15 18:03:32 +10001062#define push_end(res, mask) do { \
1063 BUG_ON((mask+1) & mask); \
1064 res->end = (res->end + mask) | mask; \
1065} while (0)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001066
1067 list_for_each_entry(dev, &bus->devices, bus_list) {
1068 u16 class = dev->class >> 8;
1069
1070 if (class == PCI_CLASS_DISPLAY_VGA ||
1071 class == PCI_CLASS_NOT_DEFINED_VGA)
1072 *found_vga = 1;
1073 if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
1074 rc |= check_for_io_childs(dev->subordinate, res, found_vga);
1075 if (class == PCI_CLASS_BRIDGE_CARDBUS)
1076 push_end(res, 0xfff);
1077
1078 for (i=0; i<PCI_NUM_RESOURCES; i++) {
1079 struct resource *r;
1080 unsigned long r_size;
1081
1082 if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
1083 && i >= PCI_BRIDGE_RESOURCES)
1084 continue;
1085 r = &dev->resource[i];
1086 r_size = r->end - r->start;
1087 if (r_size < 0xfff)
1088 r_size = 0xfff;
1089 if (r->flags & IORESOURCE_IO && (r_size) != 0) {
1090 rc = 1;
1091 push_end(res, r_size);
1092 }
1093 }
1094 }
1095
1096 return rc;
1097}
1098
1099/* Here we scan all P2P bridges of a given level that have a closed
1100 * IO window. Note that the test for the presence of a VGA card should
1101 * be improved to take into account already configured P2P bridges,
1102 * currently, we don't see them and might end up configuring 2 bridges
1103 * with VGA pass through enabled
1104 */
1105static void __init
1106do_fixup_p2p_level(struct pci_bus *bus)
1107{
1108 struct pci_bus *b;
1109 int i, parent_io;
1110 int has_vga = 0;
1111
1112 for (parent_io=0; parent_io<4; parent_io++)
1113 if (bus->resource[parent_io]
1114 && bus->resource[parent_io]->flags & IORESOURCE_IO)
1115 break;
1116 if (parent_io >= 4)
1117 return;
1118
1119 list_for_each_entry(b, &bus->children, node) {
1120 struct pci_dev *d = b->self;
1121 struct pci_controller* hose = (struct pci_controller *)d->sysdata;
1122 struct resource *res = b->resource[0];
1123 struct resource tmp_res;
1124 unsigned long max;
1125 int found_vga = 0;
1126
1127 memset(&tmp_res, 0, sizeof(tmp_res));
1128 tmp_res.start = bus->resource[parent_io]->start;
1129
1130 /* We don't let low addresses go through that closed P2P bridge, well,
1131 * that may not be necessary but I feel safer that way
1132 */
1133 if (tmp_res.start == 0)
1134 tmp_res.start = 0x1000;
1135
1136 if (!list_empty(&b->devices) && res && res->flags == 0 &&
1137 res != bus->resource[parent_io] &&
1138 (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
1139 check_for_io_childs(b, &tmp_res, &found_vga)) {
1140 u8 io_base_lo;
1141
1142 printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
1143
1144 if (found_vga) {
1145 if (has_vga) {
1146 printk(KERN_WARNING "Skipping VGA, already active"
1147 " on bus segment\n");
1148 found_vga = 0;
1149 } else
1150 has_vga = 1;
1151 }
1152 pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
1153
1154 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
1155 max = ((unsigned long) hose->io_base_virt
1156 - isa_io_base) + 0xffffffff;
1157 else
1158 max = ((unsigned long) hose->io_base_virt
1159 - isa_io_base) + 0xffff;
1160
1161 *res = tmp_res;
1162 res->flags = IORESOURCE_IO;
1163 res->name = b->name;
1164
1165 /* Find a resource in the parent where we can allocate */
1166 for (i = 0 ; i < 4; i++) {
1167 struct resource *r = bus->resource[i];
1168 if (!r)
1169 continue;
1170 if ((r->flags & IORESOURCE_IO) == 0)
1171 continue;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -07001172 DBG("Trying to allocate from %016llx, size %016llx from parent"
1173 " res %d: %016llx -> %016llx\n",
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001174 res->start, res->end, i, r->start, r->end);
1175
1176 if (allocate_resource(r, res, res->end + 1, res->start, max,
1177 res->end + 1, NULL, NULL) < 0) {
1178 DBG("Failed !\n");
1179 continue;
1180 }
1181 do_update_p2p_io_resource(b, found_vga);
1182 break;
1183 }
1184 }
1185 do_fixup_p2p_level(b);
1186 }
1187}
1188
1189static void
1190pcibios_fixup_p2p_bridges(void)
1191{
1192 struct pci_bus *b;
1193
1194 list_for_each_entry(b, &pci_root_buses, node)
1195 do_fixup_p2p_level(b);
1196}
1197
1198#endif /* CONFIG_PPC_PMAC */
1199
1200static int __init
1201pcibios_init(void)
1202{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001203 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001204 struct pci_bus *bus;
Kumar Galaa4c9e322007-06-27 13:09:43 -05001205 int next_busno = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001206
1207 printk(KERN_INFO "PCI: Probing PCI hardware\n");
1208
1209 /* Scan all of the recorded PCI controllers. */
Kumar Galaa4c9e322007-06-27 13:09:43 -05001210 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001211 if (pci_assign_all_buses)
1212 hose->first_busno = next_busno;
1213 hose->last_busno = 0xff;
Benjamin Herrenschmidt803d4572006-11-11 17:25:07 +11001214 bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
1215 hose->ops, hose);
1216 if (bus)
1217 pci_bus_add_devices(bus);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001218 hose->last_busno = bus->subordinate;
1219 if (pci_assign_all_buses || next_busno <= hose->last_busno)
1220 next_busno = hose->last_busno + pcibios_assign_bus_offset;
1221 }
1222 pci_bus_count = next_busno;
1223
1224 /* OpenFirmware based machines need a map of OF bus
1225 * numbers vs. kernel bus numbers since we may have to
1226 * remap them.
1227 */
1228 if (pci_assign_all_buses && have_of)
1229 pcibios_make_OF_bus_map();
1230
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001231 /* Call machine dependent fixup */
1232 if (ppc_md.pcibios_fixup)
1233 ppc_md.pcibios_fixup();
1234
1235 /* Allocate and assign resources */
1236 pcibios_allocate_bus_resources(&pci_root_buses);
1237 pcibios_allocate_resources(0);
1238 pcibios_allocate_resources(1);
1239#ifdef CONFIG_PPC_PMAC
1240 pcibios_fixup_p2p_bridges();
1241#endif /* CONFIG_PPC_PMAC */
1242 pcibios_assign_resources();
1243
1244 /* Call machine dependent post-init code */
1245 if (ppc_md.pcibios_after_init)
1246 ppc_md.pcibios_after_init();
1247
1248 return 0;
1249}
1250
1251subsys_initcall(pcibios_init);
1252
Kumar Gala282045b2007-07-26 00:16:05 -05001253void pcibios_fixup_bus(struct pci_bus *bus)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001254{
1255 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1256 unsigned long io_offset;
1257 struct resource *res;
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001258 struct pci_dev *dev;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001259 int i;
1260
1261 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1262 if (bus->parent == NULL) {
1263 /* This is a host bridge - fill in its resources */
1264 hose->bus = bus;
1265
1266 bus->resource[0] = res = &hose->io_resource;
1267 if (!res->flags) {
1268 if (io_offset)
1269 printk(KERN_ERR "I/O resource not set for host"
Kumar Gala5516b542007-06-27 01:17:57 -05001270 " bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001271 res->start = 0;
1272 res->end = IO_SPACE_LIMIT;
1273 res->flags = IORESOURCE_IO;
1274 }
1275 res->start += io_offset;
1276 res->end += io_offset;
1277
1278 for (i = 0; i < 3; ++i) {
1279 res = &hose->mem_resources[i];
1280 if (!res->flags) {
1281 if (i > 0)
1282 continue;
1283 printk(KERN_ERR "Memory resource not set for "
Kumar Gala5516b542007-06-27 01:17:57 -05001284 "host bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001285 res->start = hose->pci_mem_offset;
1286 res->end = ~0U;
1287 res->flags = IORESOURCE_MEM;
1288 }
1289 bus->resource[i+1] = res;
1290 }
1291 } else {
1292 /* This is a subordinate bridge */
1293 pci_read_bridge_bases(bus);
1294
1295 for (i = 0; i < 4; ++i) {
1296 if ((res = bus->resource[i]) == NULL)
1297 continue;
York Sun6d8ff102007-06-04 11:56:42 -05001298 if (!res->flags || bus->self->transparent)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001299 continue;
1300 if (io_offset && (res->flags & IORESOURCE_IO)) {
1301 res->start += io_offset;
1302 res->end += io_offset;
1303 } else if (hose->pci_mem_offset
1304 && (res->flags & IORESOURCE_MEM)) {
1305 res->start += hose->pci_mem_offset;
1306 res->end += hose->pci_mem_offset;
1307 }
1308 }
1309 }
1310
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001311 /* Platform specific bus fixups */
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001312 if (ppc_md.pcibios_fixup_bus)
1313 ppc_md.pcibios_fixup_bus(bus);
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001314
1315 /* Read default IRQs and fixup if necessary */
1316 list_for_each_entry(dev, &bus->devices, bus_list) {
1317 pci_read_irq_line(dev);
1318 if (ppc_md.pci_irq_fixup)
1319 ppc_md.pci_irq_fixup(dev);
1320 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001321}
1322
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001323/* the next one is stolen from the alpha port... */
1324void __init
1325pcibios_update_irq(struct pci_dev *dev, int irq)
1326{
1327 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1328 /* XXX FIXME - update OF device tree node interrupt property */
1329}
1330
1331int pcibios_enable_device(struct pci_dev *dev, int mask)
1332{
1333 u16 cmd, old_cmd;
1334 int idx;
1335 struct resource *r;
1336
1337 if (ppc_md.pcibios_enable_device_hook)
1338 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1339 return -EINVAL;
1340
1341 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1342 old_cmd = cmd;
1343 for (idx=0; idx<6; idx++) {
1344 r = &dev->resource[idx];
1345 if (r->flags & IORESOURCE_UNSET) {
1346 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1347 return -EINVAL;
1348 }
1349 if (r->flags & IORESOURCE_IO)
1350 cmd |= PCI_COMMAND_IO;
1351 if (r->flags & IORESOURCE_MEM)
1352 cmd |= PCI_COMMAND_MEMORY;
1353 }
1354 if (cmd != old_cmd) {
1355 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1356 pci_name(dev), old_cmd, cmd);
1357 pci_write_config_word(dev, PCI_COMMAND, cmd);
1358 }
1359 return 0;
1360}
1361
Kumar Gala0b1d40c2007-06-27 10:27:33 -05001362static struct pci_controller*
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001363pci_bus_to_hose(int bus)
1364{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001365 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001366
Kumar Galaa4c9e322007-06-27 13:09:43 -05001367 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001368 if (bus >= hose->first_busno && bus <= hose->last_busno)
1369 return hose;
1370 return NULL;
1371}
1372
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001373/* Provide information on locations of various I/O regions in physical
1374 * memory. Do this on a per-card basis so that we choose the right
1375 * root bridge.
1376 * Note that the returned IO or memory base is a physical address
1377 */
1378
1379long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1380{
1381 struct pci_controller* hose;
1382 long result = -EOPNOTSUPP;
1383
1384 /* Argh ! Please forgive me for that hack, but that's the
1385 * simplest way to get existing XFree to not lockup on some
1386 * G5 machines... So when something asks for bus 0 io base
1387 * (bus 0 is HT root), we return the AGP one instead.
1388 */
1389#ifdef CONFIG_PPC_PMAC
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001390 if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001391 if (bus == 0)
1392 bus = 0xf0;
1393#endif /* CONFIG_PPC_PMAC */
1394
1395 hose = pci_bus_to_hose(bus);
1396 if (!hose)
1397 return -ENODEV;
1398
1399 switch (which) {
1400 case IOBASE_BRIDGE_NUMBER:
1401 return (long)hose->first_busno;
1402 case IOBASE_MEMORY:
1403 return (long)hose->pci_mem_offset;
1404 case IOBASE_IO:
1405 return (long)hose->io_base_phys;
1406 case IOBASE_ISA_IO:
1407 return (long)isa_io_base;
1408 case IOBASE_ISA_MEM:
1409 return (long)isa_mem_base;
1410 }
1411
1412 return result;
1413}
1414
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001415unsigned long pci_address_to_pio(phys_addr_t address)
1416{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001417 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001418
Kumar Galaa4c9e322007-06-27 13:09:43 -05001419 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001420 unsigned int size = hose->io_resource.end -
1421 hose->io_resource.start + 1;
1422 if (address >= hose->io_base_phys &&
1423 address < (hose->io_base_phys + size)) {
1424 unsigned long base =
1425 (unsigned long)hose->io_base_virt - _IO_BASE;
1426 return base + (address - hose->io_base_phys);
1427 }
1428 }
1429 return (unsigned int)-1;
1430}
1431EXPORT_SYMBOL(pci_address_to_pio);
1432
1433/*
1434 * Null PCI config access functions, for the case when we can't
1435 * find a hose.
1436 */
1437#define NULL_PCI_OP(rw, size, type) \
1438static int \
1439null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1440{ \
1441 return PCIBIOS_DEVICE_NOT_FOUND; \
1442}
1443
1444static int
1445null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1446 int len, u32 *val)
1447{
1448 return PCIBIOS_DEVICE_NOT_FOUND;
1449}
1450
1451static int
1452null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1453 int len, u32 val)
1454{
1455 return PCIBIOS_DEVICE_NOT_FOUND;
1456}
1457
1458static struct pci_ops null_pci_ops =
1459{
Nathan Lynch6127d1c2007-08-10 05:18:42 +10001460 .read = null_read_config,
1461 .write = null_write_config,
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001462};
1463
1464/*
1465 * These functions are used early on before PCI scanning is done
1466 * and all of the pci_dev and pci_bus structures have been created.
1467 */
1468static struct pci_bus *
1469fake_pci_bus(struct pci_controller *hose, int busnr)
1470{
1471 static struct pci_bus bus;
1472
1473 if (hose == 0) {
1474 hose = pci_bus_to_hose(busnr);
1475 if (hose == 0)
1476 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1477 }
1478 bus.number = busnr;
1479 bus.sysdata = hose;
1480 bus.ops = hose? hose->ops: &null_pci_ops;
1481 return &bus;
1482}
1483
1484#define EARLY_PCI_OP(rw, size, type) \
1485int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1486 int devfn, int offset, type value) \
1487{ \
1488 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1489 devfn, offset, value); \
1490}
1491
1492EARLY_PCI_OP(read, byte, u8 *)
1493EARLY_PCI_OP(read, word, u16 *)
1494EARLY_PCI_OP(read, dword, u32 *)
1495EARLY_PCI_OP(write, byte, u8)
1496EARLY_PCI_OP(write, word, u16)
1497EARLY_PCI_OP(write, dword, u32)
Kumar Gala38805e52007-07-10 23:37:45 -05001498
1499extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
1500int early_find_capability(struct pci_controller *hose, int bus, int devfn,
1501 int cap)
1502{
1503 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
1504}