blob: 03a7dc494b42b5a550fe923cae2f31e0fad1489c [file] [log] [blame]
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001/*
2 * Common pmac/prep/chrp pci routines. -- Cort
3 */
4
Paul Mackerrase05b3b42006-01-15 22:05:47 +11005#include <linux/kernel.h>
6#include <linux/pci.h>
7#include <linux/delay.h>
8#include <linux/string.h>
9#include <linux/init.h>
10#include <linux/capability.h>
11#include <linux/sched.h>
12#include <linux/errno.h>
13#include <linux/bootmem.h>
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -070014#include <linux/irq.h>
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +110015#include <linux/list.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110016
17#include <asm/processor.h>
18#include <asm/io.h>
19#include <asm/prom.h>
20#include <asm/sections.h>
21#include <asm/pci-bridge.h>
22#include <asm/byteorder.h>
Paul Mackerrase05b3b42006-01-15 22:05:47 +110023#include <asm/uaccess.h>
24#include <asm/machdep.h>
25
26#undef DEBUG
27
28#ifdef DEBUG
29#define DBG(x...) printk(x)
30#else
31#define DBG(x...)
32#endif
33
34unsigned long isa_io_base = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +110035unsigned long pci_dram_offset = 0;
36int pcibios_assign_bus_offset = 1;
37
38void pcibios_make_OF_bus_map(void);
39
40static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
41static int probe_resource(struct pci_bus *parent, struct resource *pr,
42 struct resource *res, struct resource **conflict);
43static void update_bridge_base(struct pci_bus *bus, int i);
44static void pcibios_fixup_resources(struct pci_dev* dev);
45static void fixup_broken_pcnet32(struct pci_dev* dev);
46static int reparent_resources(struct resource *parent, struct resource *res);
47static void fixup_cpc710_pci64(struct pci_dev* dev);
48#ifdef CONFIG_PPC_OF
49static u8* pci_to_OF_bus_map;
50#endif
51
52/* By default, we don't re-assign bus numbers. We do this only on
53 * some pmacs
54 */
55int pci_assign_all_buses;
56
Kumar Galaa4c9e322007-06-27 13:09:43 -050057LIST_HEAD(hose_list);
Paul Mackerrase05b3b42006-01-15 22:05:47 +110058
59static int pci_bus_count;
60
61static void
Kumar Gala2052d6d2007-07-25 00:44:11 -050062fixup_hide_host_resource_fsl(struct pci_dev* dev)
63{
64 int i, class = dev->class >> 8;
65
66 if ((class == PCI_CLASS_PROCESSOR_POWERPC) &&
67 (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) &&
68 (dev->bus->parent == NULL)) {
69 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
70 dev->resource[i].start = 0;
71 dev->resource[i].end = 0;
72 dev->resource[i].flags = 0;
73 }
74 }
75}
76DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl);
77DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl);
78
79static void
Paul Mackerrase05b3b42006-01-15 22:05:47 +110080fixup_broken_pcnet32(struct pci_dev* dev)
81{
82 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
83 dev->vendor = PCI_VENDOR_ID_AMD;
84 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
85 }
86}
87DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
88
89static void
90fixup_cpc710_pci64(struct pci_dev* dev)
91{
92 /* Hide the PCI64 BARs from the kernel as their content doesn't
93 * fit well in the resource management
94 */
95 dev->resource[0].start = dev->resource[0].end = 0;
96 dev->resource[0].flags = 0;
97 dev->resource[1].start = dev->resource[1].end = 0;
98 dev->resource[1].flags = 0;
99}
100DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
101
102static void
103pcibios_fixup_resources(struct pci_dev *dev)
104{
105 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
106 int i;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100107 resource_size_t offset, mask;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100108
109 if (!hose) {
110 printk(KERN_ERR "No hose for PCI dev %s!\n", pci_name(dev));
111 return;
112 }
113 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
114 struct resource *res = dev->resource + i;
115 if (!res->flags)
116 continue;
117 if (res->end == 0xffffffff) {
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700118 DBG("PCI:%s Resource %d [%016llx-%016llx] is unassigned\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300119 pci_name(dev), i, (u64)res->start, (u64)res->end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100120 res->end -= res->start;
121 res->start = 0;
122 res->flags |= IORESOURCE_UNSET;
123 continue;
124 }
125 offset = 0;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100126 mask = (resource_size_t)-1;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100127 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;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100132 mask = 0xffffffffu;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100133 }
134 if (offset != 0) {
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100135 res->start = (res->start + offset) & mask;
136 res->end = (res->end + offset) & mask;
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300137 DBG("Fixup res %d (%lx) of dev %s: %llx -> %llx\n",
138 i, res->flags, pci_name(dev),
139 (u64)res->start - offset, (u64)res->start);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100140 }
141 }
142
143 /* Call machine specific resource fixup */
144 if (ppc_md.pcibios_fixup_resources)
145 ppc_md.pcibios_fixup_resources(dev);
146}
147DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources);
148
149void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
150 struct resource *res)
151{
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100152 resource_size_t offset = 0, mask = (resource_size_t)-1;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100153 struct pci_controller *hose = dev->sysdata;
154
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100155 if (hose && res->flags & IORESOURCE_IO) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100156 offset = (unsigned long)hose->io_base_virt - isa_io_base;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100157 mask = 0xffffffffu;
158 } else if (hose && res->flags & IORESOURCE_MEM)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100159 offset = hose->pci_mem_offset;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100160 region->start = (res->start - offset) & mask;
161 region->end = (res->end - offset) & mask;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100162}
163EXPORT_SYMBOL(pcibios_resource_to_bus);
164
165void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
166 struct pci_bus_region *region)
167{
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100168 resource_size_t offset = 0, mask = (resource_size_t)-1;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100169 struct pci_controller *hose = dev->sysdata;
170
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100171 if (hose && res->flags & IORESOURCE_IO) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100172 offset = (unsigned long)hose->io_base_virt - isa_io_base;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100173 mask = 0xffffffffu;
174 } else if (hose && res->flags & IORESOURCE_MEM)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100175 offset = hose->pci_mem_offset;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100176 res->start = (region->start + offset) & mask;
177 res->end = (region->end + offset) & mask;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100178}
179EXPORT_SYMBOL(pcibios_bus_to_resource);
180
181/*
182 * We need to avoid collisions with `mirrored' VGA ports
183 * and other strange ISA hardware, so we always want the
184 * addresses to be allocated in the 0x000-0x0ff region
185 * modulo 0x400.
186 *
187 * Why? Because some silly external IO cards only decode
188 * the low 10 bits of the IO address. The 0x00-0xff region
189 * is reserved for motherboard devices that decode all 16
190 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
191 * but we want to try to avoid allocating at 0x2900-0x2bff
192 * which might have be mirrored at 0x0100-0x03ff..
193 */
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700194void pcibios_align_resource(void *data, struct resource *res,
195 resource_size_t size, resource_size_t align)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100196{
197 struct pci_dev *dev = data;
198
199 if (res->flags & IORESOURCE_IO) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700200 resource_size_t start = res->start;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100201
202 if (size > 0x100) {
203 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700204 " (%lld bytes)\n", pci_name(dev),
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700205 dev->resource - res, (unsigned long long)size);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100206 }
207
208 if (start & 0x300) {
209 start = (start + 0x3ff) & ~0x3ff;
210 res->start = start;
211 }
212 }
213}
214EXPORT_SYMBOL(pcibios_align_resource);
215
216/*
217 * Handle resources of PCI devices. If the world were perfect, we could
218 * just allocate all the resource regions and do nothing more. It isn't.
219 * On the other hand, we cannot just re-allocate all devices, as it would
220 * require us to know lots of host bridge internals. So we attempt to
221 * keep as much of the original configuration as possible, but tweak it
222 * when it's found to be wrong.
223 *
224 * Known BIOS problems we have to work around:
225 * - I/O or memory regions not configured
226 * - regions configured, but not enabled in the command register
227 * - bogus I/O addresses above 64K used
228 * - expansion ROMs left enabled (this may sound harmless, but given
229 * the fact the PCI specs explicitly allow address decoders to be
230 * shared between expansion ROMs and other resource regions, it's
231 * at least dangerous)
232 *
233 * Our solution:
234 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
235 * This gives us fixed barriers on where we can allocate.
236 * (2) Allocate resources for all enabled devices. If there is
237 * a collision, just mark the resource as unallocated. Also
238 * disable expansion ROMs during this step.
239 * (3) Try to allocate resources for disabled devices. If the
240 * resources were assigned correctly, everything goes well,
241 * if they weren't, they won't disturb allocation of other
242 * resources.
243 * (4) Assign new addresses to resources which were either
244 * not configured at all or misconfigured. If explicitly
245 * requested by the user, configure expansion ROM address
246 * as well.
247 */
248
249static void __init
250pcibios_allocate_bus_resources(struct list_head *bus_list)
251{
252 struct pci_bus *bus;
253 int i;
254 struct resource *res, *pr;
255
256 /* Depth-First Search on bus tree */
257 list_for_each_entry(bus, bus_list, node) {
258 for (i = 0; i < 4; ++i) {
259 if ((res = bus->resource[i]) == NULL || !res->flags
260 || res->start > res->end)
261 continue;
262 if (bus->parent == NULL)
263 pr = (res->flags & IORESOURCE_IO)?
264 &ioport_resource: &iomem_resource;
265 else {
266 pr = pci_find_parent_resource(bus->self, res);
267 if (pr == res) {
268 /* this happens when the generic PCI
269 * code (wrongly) decides that this
270 * bridge is transparent -- paulus
271 */
272 continue;
273 }
274 }
275
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700276 DBG("PCI: bridge rsrc %llx..%llx (%lx), parent %p\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300277 (u64)res->start, (u64)res->end, res->flags, pr);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100278 if (pr) {
279 if (request_resource(pr, res) == 0)
280 continue;
281 /*
282 * Must be a conflict with an existing entry.
283 * Move that entry (or entries) under the
284 * bridge resource and try again.
285 */
286 if (reparent_resources(pr, res) == 0)
287 continue;
288 }
289 printk(KERN_ERR "PCI: Cannot allocate resource region "
290 "%d of PCI bridge %d\n", i, bus->number);
291 if (pci_relocate_bridge_resource(bus, i))
292 bus->resource[i] = NULL;
293 }
294 pcibios_allocate_bus_resources(&bus->children);
295 }
296}
297
298/*
299 * Reparent resource children of pr that conflict with res
300 * under res, and make res replace those children.
301 */
302static int __init
303reparent_resources(struct resource *parent, struct resource *res)
304{
305 struct resource *p, **pp;
306 struct resource **firstpp = NULL;
307
308 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
309 if (p->end < res->start)
310 continue;
311 if (res->end < p->start)
312 break;
313 if (p->start < res->start || p->end > res->end)
314 return -1; /* not completely contained */
315 if (firstpp == NULL)
316 firstpp = pp;
317 }
318 if (firstpp == NULL)
319 return -1; /* didn't find any conflicting entries? */
320 res->parent = parent;
321 res->child = *firstpp;
322 res->sibling = *pp;
323 *firstpp = res;
324 *pp = NULL;
325 for (p = res->child; p != NULL; p = p->sibling) {
326 p->parent = res;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700327 DBG(KERN_INFO "PCI: reparented %s [%llx..%llx] under %s\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300328 p->name, (u64)p->start, (u64)p->end, res->name);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100329 }
330 return 0;
331}
332
333/*
334 * A bridge has been allocated a range which is outside the range
335 * of its parent bridge, so it needs to be moved.
336 */
337static int __init
338pci_relocate_bridge_resource(struct pci_bus *bus, int i)
339{
340 struct resource *res, *pr, *conflict;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100341 resource_size_t try, size;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100342 struct pci_bus *parent = bus->parent;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100343 int j;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100344
345 if (parent == NULL) {
346 /* shouldn't ever happen */
347 printk(KERN_ERR "PCI: can't move host bridge resource\n");
348 return -1;
349 }
350 res = bus->resource[i];
351 if (res == NULL)
352 return -1;
353 pr = NULL;
354 for (j = 0; j < 4; j++) {
355 struct resource *r = parent->resource[j];
356 if (!r)
357 continue;
358 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
359 continue;
360 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
361 pr = r;
362 break;
363 }
364 if (res->flags & IORESOURCE_PREFETCH)
365 pr = r;
366 }
367 if (pr == NULL)
368 return -1;
369 size = res->end - res->start;
370 if (pr->start > pr->end || size > pr->end - pr->start)
371 return -1;
372 try = pr->end;
373 for (;;) {
374 res->start = try - size;
375 res->end = try;
376 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
377 break;
378 if (conflict->start <= pr->start + size)
379 return -1;
380 try = conflict->start - 1;
381 }
382 if (request_resource(pr, res)) {
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700383 DBG(KERN_ERR "PCI: huh? couldn't move to %llx..%llx\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300384 (u64)res->start, (u64)res->end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100385 return -1; /* "can't happen" */
386 }
387 update_bridge_base(bus, i);
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700388 printk(KERN_INFO "PCI: bridge %d resource %d moved to %llx..%llx\n",
389 bus->number, i, (unsigned long long)res->start,
390 (unsigned long long)res->end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100391 return 0;
392}
393
394static int __init
395probe_resource(struct pci_bus *parent, struct resource *pr,
396 struct resource *res, struct resource **conflict)
397{
398 struct pci_bus *bus;
399 struct pci_dev *dev;
400 struct resource *r;
401 int i;
402
403 for (r = pr->child; r != NULL; r = r->sibling) {
404 if (r->end >= res->start && res->end >= r->start) {
405 *conflict = r;
406 return 1;
407 }
408 }
409 list_for_each_entry(bus, &parent->children, node) {
410 for (i = 0; i < 4; ++i) {
411 if ((r = bus->resource[i]) == NULL)
412 continue;
413 if (!r->flags || r->start > r->end || r == res)
414 continue;
415 if (pci_find_parent_resource(bus->self, r) != pr)
416 continue;
417 if (r->end >= res->start && res->end >= r->start) {
418 *conflict = r;
419 return 1;
420 }
421 }
422 }
423 list_for_each_entry(dev, &parent->devices, bus_list) {
424 for (i = 0; i < 6; ++i) {
425 r = &dev->resource[i];
426 if (!r->flags || (r->flags & IORESOURCE_UNSET))
427 continue;
428 if (pci_find_parent_resource(dev, r) != pr)
429 continue;
430 if (r->end >= res->start && res->end >= r->start) {
431 *conflict = r;
432 return 1;
433 }
434 }
435 }
436 return 0;
437}
438
Kumar Galaf64fddb2007-07-20 13:35:34 -0500439void __init
440update_bridge_resource(struct pci_dev *dev, struct resource *res)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100441{
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100442 u8 io_base_lo, io_limit_lo;
443 u16 mem_base, mem_limit;
444 u16 cmd;
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +1100445 resource_size_t start, end, off;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100446 struct pci_controller *hose = dev->sysdata;
447
448 if (!hose) {
449 printk("update_bridge_base: no hose?\n");
450 return;
451 }
452 pci_read_config_word(dev, PCI_COMMAND, &cmd);
453 pci_write_config_word(dev, PCI_COMMAND,
454 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
455 if (res->flags & IORESOURCE_IO) {
456 off = (unsigned long) hose->io_base_virt - isa_io_base;
457 start = res->start - off;
458 end = res->end - off;
459 io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK;
460 io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK;
Randy Vinson60b2a462006-10-12 13:36:23 -0700461 if (end > 0xffff)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100462 io_base_lo |= PCI_IO_RANGE_TYPE_32;
Randy Vinson60b2a462006-10-12 13:36:23 -0700463 else
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100464 io_base_lo |= PCI_IO_RANGE_TYPE_16;
Randy Vinson60b2a462006-10-12 13:36:23 -0700465 pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
466 start >> 16);
467 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
468 end >> 16);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100469 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
470 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
471
472 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
473 == IORESOURCE_MEM) {
474 off = hose->pci_mem_offset;
475 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
476 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
477 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
478 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
479
480 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
481 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
482 off = hose->pci_mem_offset;
483 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
484 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
485 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
486 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
487
488 } else {
Kumar Galaf64fddb2007-07-20 13:35:34 -0500489 DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n",
490 pci_name(dev), res->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100491 }
492 pci_write_config_word(dev, PCI_COMMAND, cmd);
493}
494
Kumar Galaf64fddb2007-07-20 13:35:34 -0500495static void __init
496update_bridge_base(struct pci_bus *bus, int i)
497{
498 struct resource *res = bus->resource[i];
499 struct pci_dev *dev = bus->self;
500 update_bridge_resource(dev, res);
501}
502
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100503static inline void alloc_resource(struct pci_dev *dev, int idx)
504{
505 struct resource *pr, *r = &dev->resource[idx];
506
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700507 DBG("PCI:%s: Resource %d: %016llx-%016llx (f=%lx)\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300508 pci_name(dev), idx, (u64)r->start, (u64)r->end, r->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100509 pr = pci_find_parent_resource(dev, r);
510 if (!pr || request_resource(pr, r) < 0) {
511 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
512 " of device %s\n", idx, pci_name(dev));
513 if (pr)
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700514 DBG("PCI: parent is %p: %016llx-%016llx (f=%lx)\n",
Sergei Shtylyov872455e2006-12-03 20:52:27 +0300515 pr, (u64)pr->start, (u64)pr->end, pr->flags);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100516 /* We'll assign a new address later */
517 r->flags |= IORESOURCE_UNSET;
518 r->end -= r->start;
519 r->start = 0;
520 }
521}
522
523static void __init
524pcibios_allocate_resources(int pass)
525{
526 struct pci_dev *dev = NULL;
527 int idx, disabled;
528 u16 command;
529 struct resource *r;
530
531 for_each_pci_dev(dev) {
532 pci_read_config_word(dev, PCI_COMMAND, &command);
533 for (idx = 0; idx < 6; idx++) {
534 r = &dev->resource[idx];
535 if (r->parent) /* Already allocated */
536 continue;
537 if (!r->flags || (r->flags & IORESOURCE_UNSET))
538 continue; /* Not assigned at all */
539 if (r->flags & IORESOURCE_IO)
540 disabled = !(command & PCI_COMMAND_IO);
541 else
542 disabled = !(command & PCI_COMMAND_MEMORY);
543 if (pass == disabled)
544 alloc_resource(dev, idx);
545 }
546 if (pass)
547 continue;
548 r = &dev->resource[PCI_ROM_RESOURCE];
549 if (r->flags & IORESOURCE_ROM_ENABLE) {
550 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
551 u32 reg;
552 DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
553 r->flags &= ~IORESOURCE_ROM_ENABLE;
554 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
555 pci_write_config_dword(dev, dev->rom_base_reg,
556 reg & ~PCI_ROM_ADDRESS_ENABLE);
557 }
558 }
559}
560
561static void __init
562pcibios_assign_resources(void)
563{
564 struct pci_dev *dev = NULL;
565 int idx;
566 struct resource *r;
567
568 for_each_pci_dev(dev) {
569 int class = dev->class >> 8;
570
571 /* Don't touch classless devices and host bridges */
572 if (!class || class == PCI_CLASS_BRIDGE_HOST)
573 continue;
574
575 for (idx = 0; idx < 6; idx++) {
576 r = &dev->resource[idx];
577
578 /*
579 * We shall assign a new address to this resource,
580 * either because the BIOS (sic) forgot to do so
581 * or because we have decided the old address was
582 * unusable for some reason.
583 */
584 if ((r->flags & IORESOURCE_UNSET) && r->end &&
585 (!ppc_md.pcibios_enable_device_hook ||
586 !ppc_md.pcibios_enable_device_hook(dev, 1))) {
Segher Boessenkool3a77d292007-08-02 01:41:13 +1000587 int rc;
588
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100589 r->flags &= ~IORESOURCE_UNSET;
Segher Boessenkool3a77d292007-08-02 01:41:13 +1000590 rc = pci_assign_resource(dev, idx);
591 BUG_ON(rc);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100592 }
593 }
594
595#if 0 /* don't assign ROMs */
596 r = &dev->resource[PCI_ROM_RESOURCE];
597 r->end -= r->start;
598 r->start = 0;
599 if (r->end)
600 pci_assign_resource(dev, PCI_ROM_RESOURCE);
601#endif
602 }
603}
604
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100605#ifdef CONFIG_PPC_OF
606/*
607 * Functions below are used on OpenFirmware machines.
608 */
609static void
610make_one_node_map(struct device_node* node, u8 pci_bus)
611{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000612 const int *bus_range;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100613 int len;
614
615 if (pci_bus >= pci_bus_count)
616 return;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000617 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100618 if (bus_range == NULL || len < 2 * sizeof(int)) {
619 printk(KERN_WARNING "Can't get bus-range for %s, "
620 "assuming it starts at 0\n", node->full_name);
621 pci_to_OF_bus_map[pci_bus] = 0;
622 } else
623 pci_to_OF_bus_map[pci_bus] = bus_range[0];
624
625 for (node=node->child; node != 0;node = node->sibling) {
626 struct pci_dev* dev;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000627 const unsigned int *class_code, *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100628
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000629 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100630 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
631 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
632 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000633 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100634 if (!reg)
635 continue;
Alan Coxab462762007-04-23 14:47:59 +0100636 dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
637 if (!dev || !dev->subordinate) {
638 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100639 continue;
Alan Coxab462762007-04-23 14:47:59 +0100640 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100641 make_one_node_map(node, dev->subordinate->number);
Alan Coxab462762007-04-23 14:47:59 +0100642 pci_dev_put(dev);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100643 }
644}
645
646void
647pcibios_make_OF_bus_map(void)
648{
649 int i;
Kumar Galaa4c9e322007-06-27 13:09:43 -0500650 struct pci_controller *hose, *tmp;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000651 struct property *map_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000652 struct device_node *dn;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100653
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800654 pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100655 if (!pci_to_OF_bus_map) {
656 printk(KERN_ERR "Can't allocate OF bus map !\n");
657 return;
658 }
659
660 /* We fill the bus map with invalid values, that helps
661 * debugging.
662 */
663 for (i=0; i<pci_bus_count; i++)
664 pci_to_OF_bus_map[i] = 0xff;
665
666 /* For each hose, we begin searching bridges */
Kumar Galaa4c9e322007-06-27 13:09:43 -0500667 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100668 struct device_node* node = hose->dn;
669
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100670 if (!node)
671 continue;
672 make_one_node_map(node, hose->first_busno);
673 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000674 dn = of_find_node_by_path("/");
675 map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000676 if (map_prop) {
677 BUG_ON(pci_bus_count > map_prop->length);
678 memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
679 }
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000680 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100681#ifdef DEBUG
682 printk("PCI->OF bus map:\n");
683 for (i=0; i<pci_bus_count; i++) {
684 if (pci_to_OF_bus_map[i] == 0xff)
685 continue;
686 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
687 }
688#endif
689}
690
691typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
692
693static struct device_node*
694scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
695{
696 struct device_node* sub_node;
697
698 for (; node != 0;node = node->sibling) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000699 const unsigned int *class_code;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100700
701 if (filter(node, data))
702 return node;
703
704 /* For PCI<->PCI bridges or CardBus bridges, we go down
705 * Note: some OFs create a parent node "multifunc-device" as
706 * a fake root for all functions of a multi-function device,
707 * we go down them as well.
708 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000709 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100710 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
711 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
712 strcmp(node->name, "multifunc-device"))
713 continue;
714 sub_node = scan_OF_pci_childs(node->child, filter, data);
715 if (sub_node)
716 return sub_node;
717 }
718 return NULL;
719}
720
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100721static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
722 unsigned int devfn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100723{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100724 struct device_node *np = NULL;
725 const u32 *reg;
726 unsigned int psize;
727
728 while ((np = of_get_next_child(parent, np)) != NULL) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000729 reg = of_get_property(np, "reg", &psize);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100730 if (reg == NULL || psize < 4)
731 continue;
732 if (((reg[0] >> 8) & 0xff) == devfn)
733 return np;
734 }
735 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100736}
737
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100738
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100739static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
740{
741 struct device_node *parent, *np;
742
743 /* Are we a root bus ? */
744 if (bus->self == NULL || bus->parent == NULL) {
Kumar Gala0b1d40c2007-06-27 10:27:33 -0500745 struct pci_controller *hose = pci_bus_to_host(bus);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100746 if (hose == NULL)
747 return NULL;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100748 return of_node_get(hose->dn);
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100749 }
750
751 /* not a root bus, we need to get our parent */
752 parent = scan_OF_for_pci_bus(bus->parent);
753 if (parent == NULL)
754 return NULL;
755
756 /* now iterate for children for a match */
757 np = scan_OF_for_pci_dev(parent, bus->self->devfn);
758 of_node_put(parent);
759
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100760 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100761}
762
763/*
764 * Scans the OF tree for a device node matching a PCI device
765 */
766struct device_node *
767pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
768{
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100769 struct device_node *parent, *np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100770
771 if (!have_of)
772 return NULL;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100773
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100774 DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
775 parent = scan_OF_for_pci_bus(bus);
776 if (parent == NULL)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100777 return NULL;
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100778 DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>");
779 np = scan_OF_for_pci_dev(parent, devfn);
780 of_node_put(parent);
781 DBG(" result is %s\n", np ? np->full_name : "<NULL>");
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100782
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100783 /* XXX most callers don't release the returned node
784 * mostly because ppc64 doesn't increase the refcount,
785 * we need to fix that.
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100786 */
Benjamin Herrenschmidtdae48282006-12-11 14:09:07 +1100787 return np;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100788}
789EXPORT_SYMBOL(pci_busdev_to_OF_node);
790
791struct device_node*
792pci_device_to_OF_node(struct pci_dev *dev)
793{
794 return pci_busdev_to_OF_node(dev->bus, dev->devfn);
795}
796EXPORT_SYMBOL(pci_device_to_OF_node);
797
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100798static int
799find_OF_pci_device_filter(struct device_node* node, void* data)
800{
801 return ((void *)node == data);
802}
803
804/*
805 * Returns the PCI device matching a given OF node
806 */
807int
808pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
809{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000810 const unsigned int *reg;
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100811 struct pci_controller* hose;
812 struct pci_dev* dev = NULL;
813
814 if (!have_of)
815 return -ENODEV;
816 /* Make sure it's really a PCI device */
817 hose = pci_find_hose_for_OF_device(node);
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100818 if (!hose || !hose->dn)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100819 return -ENODEV;
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100820 if (!scan_OF_pci_childs(hose->dn->child,
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100821 find_OF_pci_device_filter, (void *)node))
822 return -ENODEV;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000823 reg = of_get_property(node, "reg", NULL);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100824 if (!reg)
825 return -ENODEV;
826 *bus = (reg[0] >> 16) & 0xff;
827 *devfn = ((reg[0] >> 8) & 0xff);
828
829 /* Ok, here we need some tweak. If we have already renumbered
830 * all busses, we can't rely on the OF bus number any more.
831 * the pci_to_OF_bus_map is not enough as several PCI busses
832 * may match the same OF bus number.
833 */
834 if (!pci_to_OF_bus_map)
835 return 0;
836
837 for_each_pci_dev(dev)
838 if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
839 dev->devfn == *devfn) {
840 *bus = dev->bus->number;
841 pci_dev_put(dev);
842 return 0;
843 }
844
845 return -ENODEV;
846}
847EXPORT_SYMBOL(pci_device_from_OF_node);
848
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100849/* We create the "pci-OF-bus-map" property now so it appears in the
850 * /proc device tree
851 */
852void __init
853pci_create_OF_bus_map(void)
854{
855 struct property* of_prop;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000856 struct device_node *dn;
857
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100858 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000859 if (!of_prop)
860 return;
861 dn = of_find_node_by_path("/");
862 if (dn) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100863 memset(of_prop, -1, sizeof(struct property) + 256);
864 of_prop->name = "pci-OF-bus-map";
865 of_prop->length = 256;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000866 of_prop->value = &of_prop[1];
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000867 prom_add_property(dn, of_prop);
868 of_node_put(dn);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100869 }
870}
871
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100872#else /* CONFIG_PPC_OF */
873void pcibios_make_OF_bus_map(void)
874{
875}
876#endif /* CONFIG_PPC_OF */
877
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100878#ifdef CONFIG_PPC_PMAC
879/*
880 * This set of routines checks for PCI<->PCI bridges that have closed
881 * IO resources and have child devices. It tries to re-open an IO
882 * window on them.
883 *
884 * This is a _temporary_ fix to workaround a problem with Apple's OF
885 * closing IO windows on P2P bridges when the OF drivers of cards
886 * below this bridge don't claim any IO range (typically ATI or
887 * Adaptec).
888 *
889 * A more complete fix would be to use drivers/pci/setup-bus.c, which
890 * involves a working pcibios_fixup_pbus_ranges(), some more care about
891 * ordering when creating the host bus resources, and maybe a few more
892 * minor tweaks
893 */
894
895/* Initialize bridges with base/limit values we have collected */
896static void __init
897do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
898{
899 struct pci_dev *bridge = bus->self;
900 struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
901 u32 l;
902 u16 w;
903 struct resource res;
904
905 if (bus->resource[0] == NULL)
906 return;
907 res = *(bus->resource[0]);
908
909 DBG("Remapping Bus %d, bridge: %s\n", bus->number, pci_name(bridge));
910 res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
911 res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700912 DBG(" IO window: %016llx-%016llx\n", res.start, res.end);
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100913
914 /* Set up the top and bottom of the PCI I/O segment for this bus. */
915 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
916 l &= 0xffff000f;
917 l |= (res.start >> 8) & 0x00f0;
918 l |= res.end & 0xf000;
919 pci_write_config_dword(bridge, PCI_IO_BASE, l);
920
921 if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
922 l = (res.start >> 16) | (res.end & 0xffff0000);
923 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
924 }
925
926 pci_read_config_word(bridge, PCI_COMMAND, &w);
927 w |= PCI_COMMAND_IO;
928 pci_write_config_word(bridge, PCI_COMMAND, w);
929
930#if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
931 if (enable_vga) {
932 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
933 w |= PCI_BRIDGE_CTL_VGA;
934 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
935 }
936#endif
937}
938
939/* This function is pretty basic and actually quite broken for the
940 * general case, it's enough for us right now though. It's supposed
941 * to tell us if we need to open an IO range at all or not and what
942 * size.
943 */
944static int __init
945check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
946{
947 struct pci_dev *dev;
948 int i;
949 int rc = 0;
950
Paul Mackerras0f582bc2006-06-15 18:03:32 +1000951#define push_end(res, mask) do { \
952 BUG_ON((mask+1) & mask); \
953 res->end = (res->end + mask) | mask; \
954} while (0)
Paul Mackerrase05b3b42006-01-15 22:05:47 +1100955
956 list_for_each_entry(dev, &bus->devices, bus_list) {
957 u16 class = dev->class >> 8;
958
959 if (class == PCI_CLASS_DISPLAY_VGA ||
960 class == PCI_CLASS_NOT_DEFINED_VGA)
961 *found_vga = 1;
962 if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
963 rc |= check_for_io_childs(dev->subordinate, res, found_vga);
964 if (class == PCI_CLASS_BRIDGE_CARDBUS)
965 push_end(res, 0xfff);
966
967 for (i=0; i<PCI_NUM_RESOURCES; i++) {
968 struct resource *r;
969 unsigned long r_size;
970
971 if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
972 && i >= PCI_BRIDGE_RESOURCES)
973 continue;
974 r = &dev->resource[i];
975 r_size = r->end - r->start;
976 if (r_size < 0xfff)
977 r_size = 0xfff;
978 if (r->flags & IORESOURCE_IO && (r_size) != 0) {
979 rc = 1;
980 push_end(res, r_size);
981 }
982 }
983 }
984
985 return rc;
986}
987
988/* Here we scan all P2P bridges of a given level that have a closed
989 * IO window. Note that the test for the presence of a VGA card should
990 * be improved to take into account already configured P2P bridges,
991 * currently, we don't see them and might end up configuring 2 bridges
992 * with VGA pass through enabled
993 */
994static void __init
995do_fixup_p2p_level(struct pci_bus *bus)
996{
997 struct pci_bus *b;
998 int i, parent_io;
999 int has_vga = 0;
1000
1001 for (parent_io=0; parent_io<4; parent_io++)
1002 if (bus->resource[parent_io]
1003 && bus->resource[parent_io]->flags & IORESOURCE_IO)
1004 break;
1005 if (parent_io >= 4)
1006 return;
1007
1008 list_for_each_entry(b, &bus->children, node) {
1009 struct pci_dev *d = b->self;
1010 struct pci_controller* hose = (struct pci_controller *)d->sysdata;
1011 struct resource *res = b->resource[0];
1012 struct resource tmp_res;
1013 unsigned long max;
1014 int found_vga = 0;
1015
1016 memset(&tmp_res, 0, sizeof(tmp_res));
1017 tmp_res.start = bus->resource[parent_io]->start;
1018
1019 /* We don't let low addresses go through that closed P2P bridge, well,
1020 * that may not be necessary but I feel safer that way
1021 */
1022 if (tmp_res.start == 0)
1023 tmp_res.start = 0x1000;
1024
1025 if (!list_empty(&b->devices) && res && res->flags == 0 &&
1026 res != bus->resource[parent_io] &&
1027 (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
1028 check_for_io_childs(b, &tmp_res, &found_vga)) {
1029 u8 io_base_lo;
1030
1031 printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
1032
1033 if (found_vga) {
1034 if (has_vga) {
1035 printk(KERN_WARNING "Skipping VGA, already active"
1036 " on bus segment\n");
1037 found_vga = 0;
1038 } else
1039 has_vga = 1;
1040 }
1041 pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
1042
1043 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
1044 max = ((unsigned long) hose->io_base_virt
1045 - isa_io_base) + 0xffffffff;
1046 else
1047 max = ((unsigned long) hose->io_base_virt
1048 - isa_io_base) + 0xffff;
1049
1050 *res = tmp_res;
1051 res->flags = IORESOURCE_IO;
1052 res->name = b->name;
1053
1054 /* Find a resource in the parent where we can allocate */
1055 for (i = 0 ; i < 4; i++) {
1056 struct resource *r = bus->resource[i];
1057 if (!r)
1058 continue;
1059 if ((r->flags & IORESOURCE_IO) == 0)
1060 continue;
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -07001061 DBG("Trying to allocate from %016llx, size %016llx from parent"
1062 " res %d: %016llx -> %016llx\n",
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001063 res->start, res->end, i, r->start, r->end);
1064
1065 if (allocate_resource(r, res, res->end + 1, res->start, max,
1066 res->end + 1, NULL, NULL) < 0) {
1067 DBG("Failed !\n");
1068 continue;
1069 }
1070 do_update_p2p_io_resource(b, found_vga);
1071 break;
1072 }
1073 }
1074 do_fixup_p2p_level(b);
1075 }
1076}
1077
1078static void
1079pcibios_fixup_p2p_bridges(void)
1080{
1081 struct pci_bus *b;
1082
1083 list_for_each_entry(b, &pci_root_buses, node)
1084 do_fixup_p2p_level(b);
1085}
1086
1087#endif /* CONFIG_PPC_PMAC */
1088
1089static int __init
1090pcibios_init(void)
1091{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001092 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001093 struct pci_bus *bus;
Kumar Galaa4c9e322007-06-27 13:09:43 -05001094 int next_busno = 0;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001095
1096 printk(KERN_INFO "PCI: Probing PCI hardware\n");
1097
1098 /* Scan all of the recorded PCI controllers. */
Kumar Galaa4c9e322007-06-27 13:09:43 -05001099 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001100 if (pci_assign_all_buses)
1101 hose->first_busno = next_busno;
1102 hose->last_busno = 0xff;
Benjamin Herrenschmidt803d4572006-11-11 17:25:07 +11001103 bus = pci_scan_bus_parented(hose->parent, hose->first_busno,
1104 hose->ops, hose);
1105 if (bus)
1106 pci_bus_add_devices(bus);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001107 hose->last_busno = bus->subordinate;
1108 if (pci_assign_all_buses || next_busno <= hose->last_busno)
1109 next_busno = hose->last_busno + pcibios_assign_bus_offset;
1110 }
1111 pci_bus_count = next_busno;
1112
1113 /* OpenFirmware based machines need a map of OF bus
1114 * numbers vs. kernel bus numbers since we may have to
1115 * remap them.
1116 */
1117 if (pci_assign_all_buses && have_of)
1118 pcibios_make_OF_bus_map();
1119
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001120 /* Call machine dependent fixup */
1121 if (ppc_md.pcibios_fixup)
1122 ppc_md.pcibios_fixup();
1123
1124 /* Allocate and assign resources */
1125 pcibios_allocate_bus_resources(&pci_root_buses);
1126 pcibios_allocate_resources(0);
1127 pcibios_allocate_resources(1);
1128#ifdef CONFIG_PPC_PMAC
1129 pcibios_fixup_p2p_bridges();
1130#endif /* CONFIG_PPC_PMAC */
1131 pcibios_assign_resources();
1132
1133 /* Call machine dependent post-init code */
1134 if (ppc_md.pcibios_after_init)
1135 ppc_md.pcibios_after_init();
1136
1137 return 0;
1138}
1139
1140subsys_initcall(pcibios_init);
1141
Kumar Gala282045b2007-07-26 00:16:05 -05001142void pcibios_fixup_bus(struct pci_bus *bus)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001143{
1144 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1145 unsigned long io_offset;
1146 struct resource *res;
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001147 struct pci_dev *dev;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001148 int i;
1149
1150 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1151 if (bus->parent == NULL) {
1152 /* This is a host bridge - fill in its resources */
1153 hose->bus = bus;
1154
1155 bus->resource[0] = res = &hose->io_resource;
1156 if (!res->flags) {
1157 if (io_offset)
1158 printk(KERN_ERR "I/O resource not set for host"
Kumar Gala5516b542007-06-27 01:17:57 -05001159 " bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001160 res->start = 0;
1161 res->end = IO_SPACE_LIMIT;
1162 res->flags = IORESOURCE_IO;
1163 }
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +11001164 res->start = (res->start + io_offset) & 0xffffffffu;
1165 res->end = (res->end + io_offset) & 0xffffffffu;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001166
1167 for (i = 0; i < 3; ++i) {
1168 res = &hose->mem_resources[i];
1169 if (!res->flags) {
1170 if (i > 0)
1171 continue;
1172 printk(KERN_ERR "Memory resource not set for "
Kumar Gala5516b542007-06-27 01:17:57 -05001173 "host bridge %d\n", hose->global_number);
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001174 res->start = hose->pci_mem_offset;
1175 res->end = ~0U;
1176 res->flags = IORESOURCE_MEM;
1177 }
1178 bus->resource[i+1] = res;
1179 }
1180 } else {
1181 /* This is a subordinate bridge */
1182 pci_read_bridge_bases(bus);
1183
1184 for (i = 0; i < 4; ++i) {
1185 if ((res = bus->resource[i]) == NULL)
1186 continue;
York Sun6d8ff102007-06-04 11:56:42 -05001187 if (!res->flags || bus->self->transparent)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001188 continue;
1189 if (io_offset && (res->flags & IORESOURCE_IO)) {
Benjamin Herrenschmidt05d39572007-12-11 14:48:20 +11001190 res->start = (res->start + io_offset) &
1191 0xffffffffu;
1192 res->end = (res->end + io_offset) &
1193 0xffffffffu;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001194 } else if (hose->pci_mem_offset
1195 && (res->flags & IORESOURCE_MEM)) {
1196 res->start += hose->pci_mem_offset;
1197 res->end += hose->pci_mem_offset;
1198 }
1199 }
1200 }
1201
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001202 /* Platform specific bus fixups */
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001203 if (ppc_md.pcibios_fixup_bus)
1204 ppc_md.pcibios_fixup_bus(bus);
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001205
1206 /* Read default IRQs and fixup if necessary */
1207 list_for_each_entry(dev, &bus->devices, bus_list) {
1208 pci_read_irq_line(dev);
1209 if (ppc_md.pci_irq_fixup)
1210 ppc_md.pci_irq_fixup(dev);
1211 }
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001212}
1213
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001214/* the next one is stolen from the alpha port... */
1215void __init
1216pcibios_update_irq(struct pci_dev *dev, int irq)
1217{
1218 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1219 /* XXX FIXME - update OF device tree node interrupt property */
1220}
1221
1222int pcibios_enable_device(struct pci_dev *dev, int mask)
1223{
1224 u16 cmd, old_cmd;
1225 int idx;
1226 struct resource *r;
1227
1228 if (ppc_md.pcibios_enable_device_hook)
1229 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1230 return -EINVAL;
1231
1232 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1233 old_cmd = cmd;
1234 for (idx=0; idx<6; idx++) {
1235 r = &dev->resource[idx];
1236 if (r->flags & IORESOURCE_UNSET) {
1237 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
1238 return -EINVAL;
1239 }
1240 if (r->flags & IORESOURCE_IO)
1241 cmd |= PCI_COMMAND_IO;
1242 if (r->flags & IORESOURCE_MEM)
1243 cmd |= PCI_COMMAND_MEMORY;
1244 }
1245 if (cmd != old_cmd) {
1246 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1247 pci_name(dev), old_cmd, cmd);
1248 pci_write_config_word(dev, PCI_COMMAND, cmd);
1249 }
1250 return 0;
1251}
1252
Kumar Gala0b1d40c2007-06-27 10:27:33 -05001253static struct pci_controller*
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001254pci_bus_to_hose(int bus)
1255{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001256 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001257
Kumar Galaa4c9e322007-06-27 13:09:43 -05001258 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001259 if (bus >= hose->first_busno && bus <= hose->last_busno)
1260 return hose;
1261 return NULL;
1262}
1263
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001264/* Provide information on locations of various I/O regions in physical
1265 * memory. Do this on a per-card basis so that we choose the right
1266 * root bridge.
1267 * Note that the returned IO or memory base is a physical address
1268 */
1269
1270long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1271{
1272 struct pci_controller* hose;
1273 long result = -EOPNOTSUPP;
1274
1275 /* Argh ! Please forgive me for that hack, but that's the
1276 * simplest way to get existing XFree to not lockup on some
1277 * G5 machines... So when something asks for bus 0 io base
1278 * (bus 0 is HT root), we return the AGP one instead.
1279 */
1280#ifdef CONFIG_PPC_PMAC
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001281 if (machine_is(powermac) && machine_is_compatible("MacRISC4"))
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001282 if (bus == 0)
1283 bus = 0xf0;
1284#endif /* CONFIG_PPC_PMAC */
1285
1286 hose = pci_bus_to_hose(bus);
1287 if (!hose)
1288 return -ENODEV;
1289
1290 switch (which) {
1291 case IOBASE_BRIDGE_NUMBER:
1292 return (long)hose->first_busno;
1293 case IOBASE_MEMORY:
1294 return (long)hose->pci_mem_offset;
1295 case IOBASE_IO:
1296 return (long)hose->io_base_phys;
1297 case IOBASE_ISA_IO:
1298 return (long)isa_io_base;
1299 case IOBASE_ISA_MEM:
1300 return (long)isa_mem_base;
1301 }
1302
1303 return result;
1304}
1305
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001306unsigned long pci_address_to_pio(phys_addr_t address)
1307{
Kumar Galaa4c9e322007-06-27 13:09:43 -05001308 struct pci_controller *hose, *tmp;
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001309
Kumar Galaa4c9e322007-06-27 13:09:43 -05001310 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001311 unsigned int size = hose->io_resource.end -
1312 hose->io_resource.start + 1;
1313 if (address >= hose->io_base_phys &&
1314 address < (hose->io_base_phys + size)) {
1315 unsigned long base =
1316 (unsigned long)hose->io_base_virt - _IO_BASE;
1317 return base + (address - hose->io_base_phys);
1318 }
1319 }
1320 return (unsigned int)-1;
1321}
1322EXPORT_SYMBOL(pci_address_to_pio);
1323
1324/*
1325 * Null PCI config access functions, for the case when we can't
1326 * find a hose.
1327 */
1328#define NULL_PCI_OP(rw, size, type) \
1329static int \
1330null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1331{ \
1332 return PCIBIOS_DEVICE_NOT_FOUND; \
1333}
1334
1335static int
1336null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
1337 int len, u32 *val)
1338{
1339 return PCIBIOS_DEVICE_NOT_FOUND;
1340}
1341
1342static int
1343null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
1344 int len, u32 val)
1345{
1346 return PCIBIOS_DEVICE_NOT_FOUND;
1347}
1348
1349static struct pci_ops null_pci_ops =
1350{
Nathan Lynch6127d1c2007-08-10 05:18:42 +10001351 .read = null_read_config,
1352 .write = null_write_config,
Paul Mackerrase05b3b42006-01-15 22:05:47 +11001353};
1354
1355/*
1356 * These functions are used early on before PCI scanning is done
1357 * and all of the pci_dev and pci_bus structures have been created.
1358 */
1359static struct pci_bus *
1360fake_pci_bus(struct pci_controller *hose, int busnr)
1361{
1362 static struct pci_bus bus;
1363
1364 if (hose == 0) {
1365 hose = pci_bus_to_hose(busnr);
1366 if (hose == 0)
1367 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1368 }
1369 bus.number = busnr;
1370 bus.sysdata = hose;
1371 bus.ops = hose? hose->ops: &null_pci_ops;
1372 return &bus;
1373}
1374
1375#define EARLY_PCI_OP(rw, size, type) \
1376int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1377 int devfn, int offset, type value) \
1378{ \
1379 return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
1380 devfn, offset, value); \
1381}
1382
1383EARLY_PCI_OP(read, byte, u8 *)
1384EARLY_PCI_OP(read, word, u16 *)
1385EARLY_PCI_OP(read, dword, u32 *)
1386EARLY_PCI_OP(write, byte, u8)
1387EARLY_PCI_OP(write, word, u16)
1388EARLY_PCI_OP(write, dword, u32)
Kumar Gala38805e52007-07-10 23:37:45 -05001389
1390extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
1391int early_find_capability(struct pci_controller *hose, int bus, int devfn,
1392 int cap)
1393{
1394 return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
1395}