1 /*
2 * Common pmac/prep/chrp pci routines. -- Cort
3 */
4
5 #include <linux/config.h>
6 #include <linux/kernel.h>
7 #include <linux/pci.h>
8 #include <linux/delay.h>
9 #include <linux/string.h>
10 #include <linux/init.h>
11 #include <linux/capability.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/bootmem.h>
15
16 #include <asm/processor.h>
17 #include <asm/io.h>
18 #include <asm/prom.h>
19 #include <asm/sections.h>
20 #include <asm/pci-bridge.h>
21 #include <asm/byteorder.h>
22 #include <asm/irq.h>
23 #include <asm/uaccess.h>
24
25 #undef DEBUG
26
27 #ifdef DEBUG
28 #define DBG(x...) printk(x)
29 #else
30 #define DBG(x...)
31 #endif
32
33 unsigned long isa_io_base = 0;
34 unsigned long isa_mem_base = 0;
35 unsigned long pci_dram_offset = 0;
36
37 void pcibios_make_OF_bus_map(void);
38
39 static int pci_relocate_bridge_resource(struct pci_bus *bus, int i);
40 static int probe_resource(struct pci_bus *parent, struct resource *pr,
41 struct resource *res, struct resource **conflict);
42 static void update_bridge_base(struct pci_bus *bus, int i);
43 static void pcibios_fixup_resources(struct pci_dev* dev);
44 static void fixup_broken_pcnet32(struct pci_dev* dev);
45 static int reparent_resources(struct resource *parent, struct resource *res);
46 static void fixup_rev1_53c810(struct pci_dev* dev);
47 static void fixup_cpc710_pci64(struct pci_dev* dev);
48 #ifdef CONFIG_ALL_PPC
49 static void pcibios_fixup_cardbus(struct pci_dev* dev);
50 static 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 */
56 int pci_assign_all_busses;
57
58 struct pci_controller* hose_head;
59 struct pci_controller** hose_tail = &hose_head;
60
61 static int pci_bus_count;
62
63 struct pci_fixup pcibios_fixups[] = {
64 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32 },
65 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_NCR, PCI_DEVICE_ID_NCR_53C810, fixup_rev1_53c810 },
66 { PCI_FIXUP_HEADER, PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64},
67 { PCI_FIXUP_HEADER, PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources },
68 #ifdef CONFIG_ALL_PPC
69 /* We should add per-machine fixup support in xxx_setup.c or xxx_pci.c */
70 { PCI_FIXUP_FINAL, PCI_VENDOR_ID_TI, PCI_ANY_ID, pcibios_fixup_cardbus },
71 #endif /* CONFIG_ALL_PPC */
72 { 0 }
73 };
74
75 static void
fixup_rev1_53c810(struct pci_dev * dev)76 fixup_rev1_53c810(struct pci_dev* dev)
77 {
78 /* rev 1 ncr53c810 chips don't set the class at all which means
79 * they don't get their resources remapped. Fix that here.
80 */
81
82 if ((dev->class == PCI_CLASS_NOT_DEFINED)) {
83 printk("NCR 53c810 rev 1 detected, setting PCI class.\n");
84 dev->class = PCI_CLASS_STORAGE_SCSI;
85 }
86 }
87
88 static void
fixup_broken_pcnet32(struct pci_dev * dev)89 fixup_broken_pcnet32(struct pci_dev* dev)
90 {
91 if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
92 dev->vendor = PCI_VENDOR_ID_AMD;
93 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
94 pci_name_device(dev);
95 }
96 }
97
98 static void
fixup_cpc710_pci64(struct pci_dev * dev)99 fixup_cpc710_pci64(struct pci_dev* dev)
100 {
101 /* Hide the PCI64 BARs from the kernel as their content doesn't
102 * fit well in the resource management
103 */
104 dev->resource[0].start = dev->resource[0].end = 0;
105 dev->resource[0].flags = 0;
106 dev->resource[1].start = dev->resource[1].end = 0;
107 dev->resource[1].flags = 0;
108 }
109
110 void
pcibios_update_resource(struct pci_dev * dev,struct resource * root,struct resource * res,int resource)111 pcibios_update_resource(struct pci_dev *dev, struct resource *root,
112 struct resource *res, int resource)
113 {
114 u32 new, check;
115 int reg;
116 struct pci_controller* hose = dev->sysdata;
117 unsigned long io_offset;
118
119 new = res->start;
120 res->flags &= ~IORESOURCE_UNSET;
121 if (hose && res->flags & IORESOURCE_IO) {
122 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
123 new -= io_offset;
124 }
125 if (hose && res->flags & IORESOURCE_MEM)
126 new -= hose->pci_mem_offset;
127 new |= (res->flags & PCI_REGION_FLAG_MASK);
128 if (resource < 6) {
129 reg = PCI_BASE_ADDRESS_0 + 4*resource;
130 } else if (resource == PCI_ROM_RESOURCE) {
131 res->flags |= PCI_ROM_ADDRESS_ENABLE;
132 reg = dev->rom_base_reg;
133 } else {
134 /* Somebody might have asked allocation of a non-standard resource */
135 return;
136 }
137
138 pci_write_config_dword(dev, reg, new);
139 pci_read_config_dword(dev, reg, &check);
140 if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
141 printk(KERN_ERR "PCI: Error while updating region "
142 "%s/%d (%08x != %08x)\n", dev->slot_name, resource,
143 new, check);
144 }
145 printk(KERN_INFO "PCI: moved device %s resource %d (%lx) to %x\n",
146 dev->slot_name, resource, res->flags,
147 new & ~PCI_REGION_FLAG_MASK);
148 }
149
150 static void
pcibios_fixup_resources(struct pci_dev * dev)151 pcibios_fixup_resources(struct pci_dev *dev)
152 {
153 struct pci_controller* hose = (struct pci_controller *)dev->sysdata;
154 int i;
155 unsigned long offset;
156
157 if (!hose) {
158 printk(KERN_ERR "No hose for PCI dev %s!\n", dev->slot_name);
159 return;
160 }
161 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
162 struct resource *res = dev->resource + i;
163 if (!res->flags)
164 continue;
165 if (!res->start || res->end == 0xffffffff) {
166 DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
167 dev->slot_name, i, res->start, res->end);
168 res->end -= res->start;
169 res->start = 0;
170 res->flags |= IORESOURCE_UNSET;
171 continue;
172 }
173 offset = 0;
174 if (res->flags & IORESOURCE_MEM) {
175 offset = hose->pci_mem_offset;
176 } else if (res->flags & IORESOURCE_IO) {
177 offset = (unsigned long) hose->io_base_virt
178 - isa_io_base;
179 }
180 if (offset != 0) {
181 res->start += offset;
182 res->end += offset;
183 #ifdef DEBUG
184 printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
185 i, res->flags, dev->slot_name,
186 res->start - offset, res->start);
187 #endif
188 }
189 }
190
191 /* Call machine specific resource fixup */
192 if (ppc_md.pcibios_fixup_resources)
193 ppc_md.pcibios_fixup_resources(dev);
194 }
195
196 #ifdef CONFIG_ALL_PPC
197 static void
pcibios_fixup_cardbus(struct pci_dev * dev)198 pcibios_fixup_cardbus(struct pci_dev* dev)
199 {
200 if (_machine != _MACH_Pmac)
201 return;
202 /*
203 * Fix the interrupt routing on the various cardbus bridges
204 * used on powerbooks
205 */
206 if (dev->vendor != PCI_VENDOR_ID_TI)
207 return;
208 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
209 dev->device == PCI_DEVICE_ID_TI_1131) {
210 u8 val;
211 /* Enable PCI interrupt */
212 if (pci_read_config_byte(dev, 0x91, &val) == 0)
213 pci_write_config_byte(dev, 0x91, val | 0x30);
214 /* Disable ISA interrupt mode */
215 if (pci_read_config_byte(dev, 0x92, &val) == 0)
216 pci_write_config_byte(dev, 0x92, val & ~0x06);
217 }
218 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
219 dev->device == PCI_DEVICE_ID_TI_1211 ||
220 dev->device == PCI_DEVICE_ID_TI_1410) {
221 u8 val;
222 /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
223 signal out the MFUNC0 pin */
224 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
225 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
226 /* Disable ISA interrupt mode */
227 if (pci_read_config_byte(dev, 0x92, &val) == 0)
228 pci_write_config_byte(dev, 0x92, val & ~0x06);
229 }
230 }
231 #endif /* CONFIG_ALL_PPC */
232
233 /*
234 * We need to avoid collisions with `mirrored' VGA ports
235 * and other strange ISA hardware, so we always want the
236 * addresses to be allocated in the 0x000-0x0ff region
237 * modulo 0x400.
238 *
239 * Why? Because some silly external IO cards only decode
240 * the low 10 bits of the IO address. The 0x00-0xff region
241 * is reserved for motherboard devices that decode all 16
242 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
243 * but we want to try to avoid allocating at 0x2900-0x2bff
244 * which might have be mirrored at 0x0100-0x03ff..
245 */
246 void
pcibios_align_resource(void * data,struct resource * res,unsigned long size,unsigned long align)247 pcibios_align_resource(void *data, struct resource *res, unsigned long size,
248 unsigned long align)
249 {
250 struct pci_dev *dev = data;
251
252 if (res->flags & IORESOURCE_IO) {
253 unsigned long start = res->start;
254
255 if (size > 0x100) {
256 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
257 " (%ld bytes)\n", dev->slot_name,
258 dev->resource - res, size);
259 }
260
261 if (start & 0x300) {
262 start = (start + 0x3ff) & ~0x3ff;
263 res->start = start;
264 }
265 }
266 }
267
268
269 /*
270 * Handle resources of PCI devices. If the world were perfect, we could
271 * just allocate all the resource regions and do nothing more. It isn't.
272 * On the other hand, we cannot just re-allocate all devices, as it would
273 * require us to know lots of host bridge internals. So we attempt to
274 * keep as much of the original configuration as possible, but tweak it
275 * when it's found to be wrong.
276 *
277 * Known BIOS problems we have to work around:
278 * - I/O or memory regions not configured
279 * - regions configured, but not enabled in the command register
280 * - bogus I/O addresses above 64K used
281 * - expansion ROMs left enabled (this may sound harmless, but given
282 * the fact the PCI specs explicitly allow address decoders to be
283 * shared between expansion ROMs and other resource regions, it's
284 * at least dangerous)
285 *
286 * Our solution:
287 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
288 * This gives us fixed barriers on where we can allocate.
289 * (2) Allocate resources for all enabled devices. If there is
290 * a collision, just mark the resource as unallocated. Also
291 * disable expansion ROMs during this step.
292 * (3) Try to allocate resources for disabled devices. If the
293 * resources were assigned correctly, everything goes well,
294 * if they weren't, they won't disturb allocation of other
295 * resources.
296 * (4) Assign new addresses to resources which were either
297 * not configured at all or misconfigured. If explicitly
298 * requested by the user, configure expansion ROM address
299 * as well.
300 */
301
302 static void __init
pcibios_allocate_bus_resources(struct list_head * bus_list)303 pcibios_allocate_bus_resources(struct list_head *bus_list)
304 {
305 struct list_head *ln;
306 struct pci_bus *bus;
307 int i;
308 struct resource *res, *pr;
309
310 /* Depth-First Search on bus tree */
311 for (ln = bus_list->next; ln != bus_list; ln=ln->next) {
312 bus = pci_bus_b(ln);
313 for (i = 0; i < 4; ++i) {
314 if ((res = bus->resource[i]) == NULL || !res->flags
315 || res->start > res->end)
316 continue;
317 if (bus->parent == NULL)
318 pr = (res->flags & IORESOURCE_IO)?
319 &ioport_resource: &iomem_resource;
320 else {
321 pr = pci_find_parent_resource(bus->self, res);
322 if (pr == res) {
323 /* this happens when the generic PCI
324 * code (wrongly) decides that this
325 * bridge is transparent -- paulus
326 */
327 continue;
328 }
329 }
330
331 DBG("PCI: bridge rsrc %lx..%lx (%lx), parent %p\n",
332 res->start, res->end, res->flags, pr);
333 if (pr) {
334 if (request_resource(pr, res) == 0)
335 continue;
336 /*
337 * Must be a conflict with an existing entry.
338 * Move that entry (or entries) under the
339 * bridge resource and try again.
340 */
341 if (reparent_resources(pr, res) == 0)
342 continue;
343 }
344 printk(KERN_ERR "PCI: Cannot allocate resource region "
345 "%d of PCI bridge %d\n", i, bus->number);
346 if (pci_relocate_bridge_resource(bus, i))
347 bus->resource[i] = NULL;
348 }
349 pcibios_allocate_bus_resources(&bus->children);
350 }
351 }
352
353 /*
354 * Reparent resource children of pr that conflict with res
355 * under res, and make res replace those children.
356 */
357 static int __init
reparent_resources(struct resource * parent,struct resource * res)358 reparent_resources(struct resource *parent, struct resource *res)
359 {
360 struct resource *p, **pp;
361 struct resource **firstpp = NULL;
362
363 for (pp = &parent->child; (p = *pp) != NULL; pp = &p->sibling) {
364 if (p->end < res->start)
365 continue;
366 if (res->end < p->start)
367 break;
368 if (p->start < res->start || p->end > res->end)
369 return -1; /* not completely contained */
370 if (firstpp == NULL)
371 firstpp = pp;
372 }
373 if (firstpp == NULL)
374 return -1; /* didn't find any conflicting entries? */
375 res->parent = parent;
376 res->child = *firstpp;
377 res->sibling = *pp;
378 *firstpp = res;
379 *pp = NULL;
380 for (p = res->child; p != NULL; p = p->sibling) {
381 p->parent = res;
382 DBG(KERN_INFO "PCI: reparented %s [%lx..%lx] under %s\n",
383 p->name, p->start, p->end, res->name);
384 }
385 return 0;
386 }
387
388 /*
389 * A bridge has been allocated a range which is outside the range
390 * of its parent bridge, so it needs to be moved.
391 */
392 static int __init
pci_relocate_bridge_resource(struct pci_bus * bus,int i)393 pci_relocate_bridge_resource(struct pci_bus *bus, int i)
394 {
395 struct resource *res, *pr, *conflict;
396 unsigned long try, size;
397 int j;
398 struct pci_bus *parent = bus->parent;
399
400 if (parent == NULL) {
401 /* shouldn't ever happen */
402 printk(KERN_ERR "PCI: can't move host bridge resource\n");
403 return -1;
404 }
405 res = bus->resource[i];
406 pr = NULL;
407 for (j = 0; j < 4; j++) {
408 struct resource *r = parent->resource[j];
409 if (!r)
410 continue;
411 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
412 continue;
413 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) {
414 pr = r;
415 break;
416 }
417 if (res->flags & IORESOURCE_PREFETCH)
418 pr = r;
419 }
420 if (pr == NULL)
421 return -1;
422 size = res->end - res->start;
423 if (pr->start > pr->end || size > pr->end - pr->start)
424 return -1;
425 try = pr->end + 1;
426 for (;;) {
427 try &= ~0xfffUL;
428 if (res->flags & IORESOURCE_MEM)
429 try &= ~0xfffffUL;
430 res->start = try - size - 1;
431 res->end = try - 1;
432 if (try <= size || res->start < pr->start)
433 return -1;
434 if (probe_resource(bus->parent, pr, res, &conflict) == 0)
435 break;
436 if (conflict->start <= pr->start + size)
437 return -1;
438 try = conflict->start;
439 }
440 if (request_resource(pr, res)) {
441 DBG(KERN_ERR "PCI: huh? couldn't move to %lx..%lx\n",
442 res->start, res->end);
443 return -1; /* "can't happen" */
444 }
445 update_bridge_base(bus, i);
446 printk(KERN_INFO "PCI: bridge %d resource %d moved to %lx..%lx\n",
447 bus->number, i, res->start, res->end);
448 return 0;
449 }
450
451 static int __init
probe_resource(struct pci_bus * parent,struct resource * pr,struct resource * res,struct resource ** conflict)452 probe_resource(struct pci_bus *parent, struct resource *pr,
453 struct resource *res, struct resource **conflict)
454 {
455 struct pci_bus *bus;
456 struct pci_dev *dev;
457 struct resource *r;
458 struct list_head *ln;
459 int i;
460
461 for (r = pr->child; r != NULL; r = r->sibling) {
462 if (r->end >= res->start && res->end >= r->start) {
463 *conflict = r;
464 return 1;
465 }
466 }
467 for (ln = parent->children.next; ln != &parent->children;
468 ln = ln->next) {
469 bus = pci_bus_b(ln);
470 for (i = 0; i < 4; ++i) {
471 if ((r = bus->resource[i]) == NULL)
472 continue;
473 if (!r->flags || r->start > r->end || r == res)
474 continue;
475 if (pci_find_parent_resource(bus->self, r) != pr)
476 continue;
477 if (r->end >= res->start && res->end >= r->start) {
478 *conflict = r;
479 return 1;
480 }
481 }
482 }
483 for (ln = parent->devices.next; ln != &parent->devices; ln=ln->next) {
484 dev = pci_dev_b(ln);
485 for (i = 0; i < 6; ++i) {
486 r = &dev->resource[i];
487 if (!r->flags || (r->flags & IORESOURCE_UNSET))
488 continue;
489 if (pci_find_parent_resource(bus->self, r) != pr)
490 continue;
491 if (r->end >= res->start && res->end >= r->start) {
492 *conflict = r;
493 return 1;
494 }
495 }
496 }
497 return 0;
498 }
499
500 static void __init
update_bridge_base(struct pci_bus * bus,int i)501 update_bridge_base(struct pci_bus *bus, int i)
502 {
503 struct resource *res = bus->resource[i];
504 u8 io_base_lo, io_limit_lo;
505 u16 mem_base, mem_limit;
506 u16 cmd;
507 unsigned long start, end, off;
508 struct pci_dev *dev = bus->self;
509 struct pci_controller *hose = dev->sysdata;
510
511 if (!hose) {
512 printk("update_bridge_base: no hose?\n");
513 return;
514 }
515 pci_read_config_word(dev, PCI_COMMAND, &cmd);
516 pci_write_config_word(dev, PCI_COMMAND,
517 cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
518 if (res->flags & IORESOURCE_IO) {
519 u16 bu;
520 off = (unsigned long) hose->io_base_virt - isa_io_base;
521 start = res->start - off;
522 end = res->end - off;
523 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
524 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
525 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &bu);
526 io_limit_lo &= PCI_IO_RANGE_TYPE_MASK;
527 if (io_base_lo == PCI_IO_RANGE_TYPE_16 && end > 0xffff) {
528 printk(KERN_ERR "bridge only supports 16-bit I/O!\n");
529 goto out;
530 }
531 io_base_lo |= (start >> 8) & PCI_IO_RANGE_MASK;
532 io_limit_lo |= (end >> 8) & PCI_IO_RANGE_MASK;
533 pci_write_config_word(dev, PCI_IO_BASE_UPPER16, start >> 16);
534 pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
535 pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo);
536 pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo);
537
538 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
539 == IORESOURCE_MEM) {
540 off = hose->pci_mem_offset;
541 mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK;
542 mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK;
543 pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base);
544 pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit);
545
546 } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH))
547 == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
548 off = hose->pci_mem_offset;
549 mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK;
550 mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK;
551 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base);
552 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit);
553
554 } else {
555 DBG(KERN_ERR "PCI: ugh, bridge %s res %d has flags=%lx\n",
556 dev->slot_name, i, res->flags);
557 }
558 out:
559 pci_write_config_word(dev, PCI_COMMAND, cmd);
560 }
561
alloc_resource(struct pci_dev * dev,int idx)562 static inline void alloc_resource(struct pci_dev *dev, int idx)
563 {
564 struct resource *pr, *r = &dev->resource[idx];
565
566 DBG("PCI:%s: Resource %d: %08lx-%08lx (f=%lx)\n",
567 dev->slot_name, idx, r->start, r->end, r->flags);
568 pr = pci_find_parent_resource(dev, r);
569 if (!pr || request_resource(pr, r) < 0) {
570 printk(KERN_ERR "PCI: Cannot allocate resource region %d"
571 " of device %s\n", idx, dev->slot_name);
572 if (pr)
573 DBG("PCI: parent is %p: %08lx-%08lx (f=%lx)\n",
574 pr, pr->start, pr->end, pr->flags);
575 /* We'll assign a new address later */
576 r->flags |= IORESOURCE_UNSET;
577 r->end -= r->start;
578 r->start = 0;
579 }
580 }
581
582 static void __init
pcibios_allocate_resources(int pass)583 pcibios_allocate_resources(int pass)
584 {
585 struct pci_dev *dev;
586 int idx, disabled;
587 u16 command;
588 struct resource *r;
589
590 pci_for_each_dev(dev) {
591 pci_read_config_word(dev, PCI_COMMAND, &command);
592 for (idx = 0; idx < 6; idx++) {
593 r = &dev->resource[idx];
594 if (r->parent) /* Already allocated */
595 continue;
596 if (!r->flags || (r->flags & IORESOURCE_UNSET))
597 continue; /* Not assigned at all */
598 if (r->flags & IORESOURCE_IO)
599 disabled = !(command & PCI_COMMAND_IO);
600 else
601 disabled = !(command & PCI_COMMAND_MEMORY);
602 if (pass == disabled)
603 alloc_resource(dev, idx);
604 }
605 if (pass)
606 continue;
607 r = &dev->resource[PCI_ROM_RESOURCE];
608 if (r->flags & PCI_ROM_ADDRESS_ENABLE) {
609 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
610 u32 reg;
611 DBG("PCI: Switching off ROM of %s\n", dev->slot_name);
612 r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
613 pci_read_config_dword(dev, dev->rom_base_reg, ®);
614 pci_write_config_dword(dev, dev->rom_base_reg,
615 reg & ~PCI_ROM_ADDRESS_ENABLE);
616 }
617 }
618 }
619
620 static void __init
pcibios_assign_resources(void)621 pcibios_assign_resources(void)
622 {
623 struct pci_dev *dev;
624 int idx;
625 struct resource *r;
626
627 pci_for_each_dev(dev) {
628 int class = dev->class >> 8;
629
630 /* Don't touch classless devices and host bridges */
631 if (!class || class == PCI_CLASS_BRIDGE_HOST)
632 continue;
633
634 for (idx = 0; idx < 6; idx++) {
635 r = &dev->resource[idx];
636
637 /*
638 * We shall assign a new address to this resource,
639 * either because the BIOS (sic) forgot to do so
640 * or because we have decided the old address was
641 * unusable for some reason.
642 */
643 if ((r->flags & IORESOURCE_UNSET) && r->end &&
644 (!ppc_md.pcibios_enable_device_hook ||
645 !ppc_md.pcibios_enable_device_hook(dev, 1)))
646 pci_assign_resource(dev, idx);
647 }
648
649 #if 0 /* don't assign ROMs */
650 r = &dev->resource[PCI_ROM_RESOURCE];
651 r->end -= r->start;
652 r->start = 0;
653 if (r->end)
654 pci_assign_resource(dev, PCI_ROM_RESOURCE);
655 #endif
656 }
657 }
658
659
660 int
pcibios_enable_resources(struct pci_dev * dev)661 pcibios_enable_resources(struct pci_dev *dev)
662 {
663 u16 cmd, old_cmd;
664 int idx;
665 struct resource *r;
666
667 pci_read_config_word(dev, PCI_COMMAND, &cmd);
668 old_cmd = cmd;
669 for (idx=0; idx<6; idx++) {
670 r = &dev->resource[idx];
671 if (r->flags & IORESOURCE_UNSET) {
672 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", dev->slot_name);
673 return -EINVAL;
674 }
675 if (r->flags & IORESOURCE_IO)
676 cmd |= PCI_COMMAND_IO;
677 if (r->flags & IORESOURCE_MEM)
678 cmd |= PCI_COMMAND_MEMORY;
679 }
680 if (dev->resource[PCI_ROM_RESOURCE].start)
681 cmd |= PCI_COMMAND_MEMORY;
682 if (cmd != old_cmd) {
683 printk("PCI: Enabling device %s (%04x -> %04x)\n", dev->slot_name, old_cmd, cmd);
684 pci_write_config_word(dev, PCI_COMMAND, cmd);
685 }
686 return 0;
687 }
688
689 static int next_controller_index;
690
691 struct pci_controller * __init
pcibios_alloc_controller(void)692 pcibios_alloc_controller(void)
693 {
694 struct pci_controller *hose;
695
696 hose = (struct pci_controller *)alloc_bootmem(sizeof(*hose));
697 memset(hose, 0, sizeof(struct pci_controller));
698
699 *hose_tail = hose;
700 hose_tail = &hose->next;
701
702 hose->index = next_controller_index++;
703
704 return hose;
705 }
706
707 #ifdef CONFIG_ALL_PPC
708 /*
709 * Functions below are used on OpenFirmware machines.
710 */
711 static void __openfirmware
make_one_node_map(struct device_node * node,u8 pci_bus)712 make_one_node_map(struct device_node* node, u8 pci_bus)
713 {
714 int *bus_range;
715 int len;
716
717 if (pci_bus >= pci_bus_count)
718 return;
719 bus_range = (int *) get_property(node, "bus-range", &len);
720 if (bus_range == NULL || len < 2 * sizeof(int)) {
721 printk(KERN_WARNING "Can't get bus-range for %s\n",
722 node->full_name);
723 return;
724 }
725 pci_to_OF_bus_map[pci_bus] = bus_range[0];
726
727 for (node=node->child; node != 0;node = node->sibling) {
728 struct pci_dev* dev;
729 unsigned int *class_code, *reg;
730
731 class_code = (unsigned int *) get_property(node, "class-code", 0);
732 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
733 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
734 continue;
735 reg = (unsigned int *)get_property(node, "reg", 0);
736 if (!reg)
737 continue;
738 dev = pci_find_slot(pci_bus, ((reg[0] >> 8) & 0xff));
739 if (!dev || !dev->subordinate)
740 continue;
741 make_one_node_map(node, dev->subordinate->number);
742 }
743 }
744
745 void __openfirmware
pcibios_make_OF_bus_map(void)746 pcibios_make_OF_bus_map(void)
747 {
748 int i;
749 struct pci_controller* hose;
750 u8* of_prop_map;
751
752 pci_to_OF_bus_map = (u8*)kmalloc(pci_bus_count, GFP_KERNEL);
753 if (!pci_to_OF_bus_map) {
754 printk(KERN_ERR "Can't allocate OF bus map !\n");
755 return;
756 }
757
758 /* We fill the bus map with invalid values, that helps
759 * debugging.
760 */
761 for (i=0; i<pci_bus_count; i++)
762 pci_to_OF_bus_map[i] = 0xff;
763
764 /* For each hose, we begin searching bridges */
765 for(hose=hose_head; hose; hose=hose->next) {
766 struct device_node* node;
767 node = (struct device_node *)hose->arch_data;
768 if (!node)
769 continue;
770 make_one_node_map(node, hose->first_busno);
771 }
772 of_prop_map = get_property(find_path_device("/"), "pci-OF-bus-map", 0);
773 if (of_prop_map)
774 memcpy(of_prop_map, pci_to_OF_bus_map, pci_bus_count);
775 #ifdef DEBUG
776 printk("PCI->OF bus map:\n");
777 for (i=0; i<pci_bus_count; i++) {
778 if (pci_to_OF_bus_map[i] == 0xff)
779 continue;
780 printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
781 }
782 #endif
783 }
784
785 typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
786
787 static struct device_node* __openfirmware
scan_OF_pci_childs(struct device_node * node,pci_OF_scan_iterator filter,void * data)788 scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data)
789 {
790 struct device_node* sub_node;
791
792 for (; node != 0;node = node->sibling) {
793 unsigned int *class_code;
794
795 if (filter(node, data))
796 return node;
797
798 /* For PCI<->PCI bridges or CardBus bridges, we go down
799 * Note: some OFs create a parent node "multifunc-device" as
800 * a fake root for all functions of a multi-function device,
801 * we go down them as well.
802 */
803 class_code = (unsigned int *) get_property(node, "class-code", 0);
804 if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
805 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
806 strcmp(node->name, "multifunc-device"))
807 continue;
808 sub_node = scan_OF_pci_childs(node->child, filter, data);
809 if (sub_node)
810 return sub_node;
811 }
812 return NULL;
813 }
814
815 static int
scan_OF_pci_childs_iterator(struct device_node * node,void * data)816 scan_OF_pci_childs_iterator(struct device_node* node, void* data)
817 {
818 unsigned int *reg;
819 u8* fdata = (u8*)data;
820
821 reg = (unsigned int *) get_property(node, "reg", 0);
822 if (reg && ((reg[0] >> 8) & 0xff) == fdata[1]
823 && ((reg[0] >> 16) & 0xff) == fdata[0])
824 return 1;
825 return 0;
826 }
827
828 static struct device_node* __openfirmware
scan_OF_childs_for_device(struct device_node * node,u8 bus,u8 dev_fn)829 scan_OF_childs_for_device(struct device_node* node, u8 bus, u8 dev_fn)
830 {
831 u8 filter_data[2] = {bus, dev_fn};
832
833 return scan_OF_pci_childs(node, scan_OF_pci_childs_iterator, filter_data);
834 }
835
836 /*
837 * Scans the OF tree for a device node matching a PCI device
838 */
839 struct device_node*
pci_device_to_OF_node(struct pci_dev * dev)840 pci_device_to_OF_node(struct pci_dev *dev)
841 {
842 struct pci_controller *hose;
843 struct device_node *node;
844 int bus;
845
846 if (!have_of)
847 return NULL;
848
849 /* Lookup the hose */
850 bus = dev->bus->number;
851 hose = pci_bus_to_hose(bus);
852 if (!hose)
853 return NULL;
854
855 /* Check it has an OF node associated */
856 node = (struct device_node *) hose->arch_data;
857 if (!node)
858 return NULL;
859
860 /* Fixup bus number according to what OF think it is. */
861 if (pci_to_OF_bus_map)
862 bus = pci_to_OF_bus_map[bus];
863 if (bus == 0xff)
864 return NULL;
865
866 /* Now, lookup childs of the hose */
867 return scan_OF_childs_for_device(node->child, bus, dev->devfn);
868 }
869
870 /* This routine is meant to be used early during boot, when the
871 * PCI bus numbers have not yet been assigned, and you need to
872 * issue PCI config cycles to an OF device.
873 * It could also be used to "fix" RTAS config cycles if you want
874 * to set pci_assign_all_busses to 1 and still use RTAS for PCI
875 * config cycles.
876 */
877 struct pci_controller*
pci_find_hose_for_OF_device(struct device_node * node)878 pci_find_hose_for_OF_device(struct device_node* node)
879 {
880 if (!have_of)
881 return NULL;
882 while(node) {
883 struct pci_controller* hose;
884 for (hose=hose_head;hose;hose=hose->next)
885 if (hose->arch_data == node)
886 return hose;
887 node=node->parent;
888 }
889 return NULL;
890 }
891
892 static int __openfirmware
find_OF_pci_device_filter(struct device_node * node,void * data)893 find_OF_pci_device_filter(struct device_node* node, void* data)
894 {
895 return ((void *)node == data);
896 }
897
898 /*
899 * Returns the PCI device matching a given OF node
900 */
901 int
pci_device_from_OF_node(struct device_node * node,u8 * bus,u8 * devfn)902 pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
903 {
904 unsigned int *reg;
905 struct pci_controller* hose;
906 struct pci_dev* dev;
907
908 if (!have_of)
909 return -ENODEV;
910 /* Make sure it's really a PCI device */
911 hose = pci_find_hose_for_OF_device(node);
912 if (!hose || !hose->arch_data)
913 return -ENODEV;
914 if (!scan_OF_pci_childs(((struct device_node*)hose->arch_data)->child,
915 find_OF_pci_device_filter, (void *)node))
916 return -ENODEV;
917 reg = (unsigned int *) get_property(node, "reg", 0);
918 if (!reg)
919 return -ENODEV;
920 *bus = (reg[0] >> 16) & 0xff;
921 *devfn = ((reg[0] >> 8) & 0xff);
922
923 /* Ok, here we need some tweak. If we have already renumbered
924 * all busses, we can't rely on the OF bus number any more.
925 * the pci_to_OF_bus_map is not enough as several PCI busses
926 * may match the same OF bus number.
927 */
928 if (!pci_to_OF_bus_map)
929 return 0;
930 pci_for_each_dev(dev) {
931 if (pci_to_OF_bus_map[dev->bus->number] != *bus)
932 continue;
933 if (dev->devfn != *devfn)
934 continue;
935 *bus = dev->bus->number;
936 return 0;
937 }
938 return -ENODEV;
939 }
940
941 void __init
pci_process_bridge_OF_ranges(struct pci_controller * hose,struct device_node * dev,int primary)942 pci_process_bridge_OF_ranges(struct pci_controller *hose,
943 struct device_node *dev, int primary)
944 {
945 unsigned int *ranges, *prev;
946 int rlen = 0;
947 int memno = 0;
948 struct resource *res;
949 int na = prom_n_addr_cells(dev);
950 int np = na + 5;
951 unsigned int size;
952
953 /* First we try to merge ranges to fix a problem with some pmacs
954 * that can have more than 3 ranges, fortunately using contiguous
955 * addresses -- BenH
956 */
957 ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
958 prev = NULL;
959 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
960 if (prev) {
961 if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
962 (prev[2] + prev[na+4]) == ranges[2] &&
963 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
964 prev[na+4] += ranges[na+4];
965 ranges[0] = 0;
966 ranges += np;
967 continue;
968 }
969 }
970 prev = ranges;
971 ranges += np;
972 }
973
974 /*
975 * The ranges property is laid out as an array of elements,
976 * each of which comprises:
977 * cells 0 - 2: a PCI address
978 * cells 3 or 3+4: a CPU physical address
979 * (size depending on dev->n_addr_cells)
980 * cells 4+5 or 5+6: the size of the range
981 */
982 rlen = 0;
983 hose->io_base_phys = 0;
984 ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
985 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
986 res = NULL;
987 size = ranges[na+4];
988 switch (ranges[0] >> 24) {
989 case 1: /* I/O space */
990 if (ranges[2] != 0)
991 break;
992 hose->io_base_phys = ranges[na+2];
993 /* limit I/O to 16MB */
994 if (size > 0x01000000)
995 size = 0x01000000;
996 hose->io_base_virt = ioremap(ranges[na+2], size);
997 if (primary)
998 isa_io_base = (unsigned long) hose->io_base_virt;
999 res = &hose->io_resource;
1000 res->flags = IORESOURCE_IO;
1001 res->start = ranges[2];
1002 break;
1003 case 2: /* memory space */
1004 memno = 0;
1005 if (ranges[1] == 0 && ranges[2] == 0
1006 && ranges[na+4] <= (16 << 20)) {
1007 /* 1st 16MB, i.e. ISA memory area */
1008 if (primary)
1009 isa_mem_base = ranges[na+2];
1010 memno = 1;
1011 }
1012 while (memno < 3 && hose->mem_resources[memno].flags)
1013 ++memno;
1014 if (memno == 0)
1015 hose->pci_mem_offset = ranges[na+2] - ranges[2];
1016 if (memno < 3) {
1017 res = &hose->mem_resources[memno];
1018 res->flags = IORESOURCE_MEM;
1019 res->start = ranges[na+2];
1020 }
1021 break;
1022 }
1023 if (res != NULL) {
1024 res->name = dev->full_name;
1025 res->end = res->start + size - 1;
1026 res->parent = NULL;
1027 res->sibling = NULL;
1028 res->child = NULL;
1029 }
1030 ranges += np;
1031 }
1032 }
1033
1034 /* We create the "pci-OF-bus-map" property now so it appears in the
1035 * /proc device tree
1036 */
1037 void __init
pci_create_OF_bus_map(void)1038 pci_create_OF_bus_map(void)
1039 {
1040 struct property* of_prop;
1041
1042 of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
1043 if (of_prop && find_path_device("/")) {
1044 memset(of_prop, -1, sizeof(struct property) + 256);
1045 of_prop->name = "pci-OF-bus-map";
1046 of_prop->length = 256;
1047 of_prop->value = (unsigned char *)&of_prop[1];
1048 prom_add_property(find_path_device("/"), of_prop);
1049 }
1050 }
1051 #endif /* CONFIG_ALL_PPC */
1052
1053 /*
1054 * This set of routines checks for PCI<->PCI bridges that have closed
1055 * IO resources and have child devices. It tries to re-open an IO
1056 * window on them.
1057 *
1058 * This is a _temporary_ fix to workaround a problem with Apple's OF
1059 * closing IO windows on P2P bridges when the OF drivers of cards
1060 * below this bridge don't claim any IO range (typically ATI or
1061 * Adaptec).
1062 *
1063 * A more complete fix would be to use drivers/pci/setup-bus.c, which
1064 * involves a working pcibios_fixup_pbus_ranges(), some more care about
1065 * ordering when creating the host bus resources, and maybe a few more
1066 * minor tweaks
1067 */
1068
1069 /* Initialize bridges with base/limit values we have collected */
1070 static void __init
do_update_p2p_io_resource(struct pci_bus * bus,int enable_vga)1071 do_update_p2p_io_resource(struct pci_bus *bus, int enable_vga)
1072 {
1073 struct pci_dev *bridge = bus->self;
1074 struct pci_controller* hose = (struct pci_controller *)bridge->sysdata;
1075 u32 l;
1076 u16 w;
1077 struct resource res;
1078
1079 res = *(bus->resource[0]);
1080
1081 DBG("Remapping Bus %d, bridge: %s\n", bus->number, bridge->name);
1082 res.start -= ((unsigned long) hose->io_base_virt - isa_io_base);
1083 res.end -= ((unsigned long) hose->io_base_virt - isa_io_base);
1084 DBG(" IO window: %08lx-%08lx\n", res.start, res.end);
1085
1086 /* Set up the top and bottom of the PCI I/O segment for this bus. */
1087 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
1088 l &= 0xffff000f;
1089 l |= (res.start >> 8) & 0x00f0;
1090 l |= res.end & 0xf000;
1091 pci_write_config_dword(bridge, PCI_IO_BASE, l);
1092
1093 if ((l & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
1094 l = (res.start >> 16) | (res.end & 0xffff0000);
1095 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, l);
1096 }
1097
1098 pci_read_config_word(bridge, PCI_COMMAND, &w);
1099 w |= PCI_COMMAND_IO;
1100 pci_write_config_word(bridge, PCI_COMMAND, w);
1101
1102 #if 0 /* Enabling this causes XFree 4.2.0 to hang during PCI probe */
1103 if (enable_vga) {
1104 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &w);
1105 w |= PCI_BRIDGE_CTL_VGA;
1106 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, w);
1107 }
1108 #endif
1109 }
1110
1111 /* This function is pretty basic and actually quite broken for the
1112 * general case, it's enough for us right now though. It's supposed
1113 * to tell us if we need to open an IO range at all or not and what
1114 * size.
1115 */
1116 static int __init
check_for_io_childs(struct pci_bus * bus,struct resource * res,int * found_vga)1117 check_for_io_childs(struct pci_bus *bus, struct resource* res, int *found_vga)
1118 {
1119 struct list_head *ln;
1120 int i;
1121 int rc = 0;
1122
1123 #define push_end(res, size) do { unsigned long __sz = (size) ; \
1124 res->end = ((res->end + __sz) / (__sz + 1)) * (__sz + 1) + __sz; \
1125 } while (0)
1126
1127 for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
1128 struct pci_dev *dev = pci_dev_b(ln);
1129 u16 class = dev->class >> 8;
1130
1131 if (class == PCI_CLASS_DISPLAY_VGA ||
1132 class == PCI_CLASS_NOT_DEFINED_VGA)
1133 *found_vga = 1;
1134 if (class >> 8 == PCI_BASE_CLASS_BRIDGE && dev->subordinate)
1135 rc |= check_for_io_childs(dev->subordinate, res, found_vga);
1136 if (class == PCI_CLASS_BRIDGE_CARDBUS)
1137 push_end(res, 0xfff);
1138
1139 for (i=0; i<PCI_NUM_RESOURCES; i++) {
1140 struct resource *r;
1141 unsigned long r_size;
1142
1143 if (dev->class >> 8 == PCI_CLASS_BRIDGE_PCI
1144 && i >= PCI_BRIDGE_RESOURCES)
1145 continue;
1146 r = &dev->resource[i];
1147 r_size = r->end - r->start;
1148 if (r_size < 0xfff)
1149 r_size = 0xfff;
1150 if (r->flags & IORESOURCE_IO && (r_size) != 0) {
1151 rc = 1;
1152 push_end(res, r_size);
1153 }
1154 }
1155 }
1156
1157 return rc;
1158 }
1159
1160 /* Here we scan all P2P bridges of a given level that have a closed
1161 * IO window. Note that the test for the presence of a VGA card should
1162 * be improved to take into account already configured P2P bridges,
1163 * currently, we don't see them and might end up configuring 2 bridges
1164 * with VGA pass through enabled
1165 */
1166 static void __init
do_fixup_p2p_level(struct pci_bus * bus)1167 do_fixup_p2p_level(struct pci_bus *bus)
1168 {
1169 struct list_head *ln;
1170 int i, parent_io;
1171 int has_vga = 0;
1172
1173 for (parent_io=0; parent_io<4; parent_io++)
1174 if (bus->resource[parent_io]->flags & IORESOURCE_IO)
1175 break;
1176 if (parent_io >= 4)
1177 return;
1178
1179 for (ln=bus->children.next; ln != &bus->children; ln=ln->next) {
1180 struct pci_bus *b = pci_bus_b(ln);
1181 struct pci_dev *d = b->self;
1182 struct pci_controller* hose = (struct pci_controller *)d->sysdata;
1183 struct resource *res = b->resource[0];
1184 struct resource tmp_res;
1185 unsigned long max;
1186 int found_vga = 0;
1187
1188 memset(&tmp_res, 0, sizeof(tmp_res));
1189 tmp_res.start = bus->resource[parent_io]->start;
1190
1191 /* We don't let low addresses go through that closed P2P bridge, well,
1192 * that may not be necessary but I feel safer that way
1193 */
1194 if (tmp_res.start == 0)
1195 tmp_res.start = 0x1000;
1196
1197 if (!list_empty(&b->devices) && res && res->flags == 0 &&
1198 res != bus->resource[parent_io] &&
1199 (d->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
1200 check_for_io_childs(b, &tmp_res, &found_vga)) {
1201 u8 io_base_lo;
1202
1203 printk(KERN_INFO "Fixing up IO bus %s\n", b->name);
1204
1205 if (found_vga) {
1206 if (has_vga) {
1207 printk(KERN_WARNING "Skipping VGA, already active"
1208 " on bus segment\n");
1209 found_vga = 0;
1210 } else
1211 has_vga = 1;
1212 }
1213 pci_read_config_byte(d, PCI_IO_BASE, &io_base_lo);
1214
1215 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32)
1216 max = ((unsigned long) hose->io_base_virt
1217 - isa_io_base) + 0xffffffff;
1218 else
1219 max = ((unsigned long) hose->io_base_virt
1220 - isa_io_base) + 0xffff;
1221
1222 *res = tmp_res;
1223 res->flags = IORESOURCE_IO;
1224 res->name = b->name;
1225
1226 /* Find a resource in the parent where we can allocate */
1227 for (i = 0 ; i < 4; i++) {
1228 struct resource *r = bus->resource[i];
1229 if (!r)
1230 continue;
1231 if ((r->flags & IORESOURCE_IO) == 0)
1232 continue;
1233 DBG("Trying to allocate from %08lx, size %08lx from parent"
1234 " res %d: %08lx -> %08lx\n",
1235 res->start, res->end, i, r->start, r->end);
1236
1237 if (allocate_resource(r, res, res->end + 1, res->start, max,
1238 res->end + 1, NULL, NULL) < 0) {
1239 DBG("Failed !\n");
1240 continue;
1241 }
1242 do_update_p2p_io_resource(b, found_vga);
1243 break;
1244 }
1245 }
1246 do_fixup_p2p_level(b);
1247 }
1248 }
1249
1250 static void
pcibios_fixup_p2p_bridges(void)1251 pcibios_fixup_p2p_bridges(void)
1252 {
1253 struct list_head *ln;
1254
1255 for(ln=pci_root_buses.next; ln != &pci_root_buses; ln=ln->next) {
1256 struct pci_bus *b = pci_bus_b(ln);
1257 do_fixup_p2p_level(b);
1258 }
1259 }
1260
1261 void __init
pcibios_init(void)1262 pcibios_init(void)
1263 {
1264 struct pci_controller *hose;
1265 struct pci_bus *bus;
1266 int next_busno, bus_offset;
1267
1268 printk(KERN_INFO "PCI: Probing PCI hardware\n");
1269
1270 /* There is a problem with bus renumbering currently. If
1271 * you have 2 sibling pci<->pci bridges, and during PCI
1272 * probe, the first one gets assigned a new number equal
1273 * to the old number of the second one, you'll end up
1274 * probing that branch with 2 bridges racing on the bus
1275 * numbers.
1276 * I work around this on pmac by adding a large offset
1277 * between host bridges, though a better long term solution
1278 * will have to be found in the generic code. --BenH
1279 */
1280 #ifdef CONFIG_ALL_PPC
1281 if (machine_is_compatible("MacRISC"))
1282 bus_offset = 0x10;
1283 else
1284 #endif
1285 bus_offset = 1;
1286 /* Scan all of the recorded PCI controllers. */
1287 for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
1288 if (pci_assign_all_busses)
1289 hose->first_busno = next_busno;
1290 hose->last_busno = 0xff;
1291 bus = pci_scan_bus(hose->first_busno, hose->ops, hose);
1292 hose->last_busno = bus->subordinate;
1293 if (pci_assign_all_busses || next_busno <= hose->last_busno)
1294 next_busno = hose->last_busno + bus_offset;
1295 }
1296 pci_bus_count = next_busno;
1297
1298 /* OpenFirmware based machines need a map of OF bus
1299 * numbers vs. kernel bus numbers since we may have to
1300 * remap them.
1301 */
1302 if (pci_assign_all_busses && have_of)
1303 pcibios_make_OF_bus_map();
1304
1305 /* Do machine dependent PCI interrupt routing */
1306 if (ppc_md.pci_swizzle && ppc_md.pci_map_irq)
1307 pci_fixup_irqs(ppc_md.pci_swizzle, ppc_md.pci_map_irq);
1308
1309 /* Call machine dependant fixup */
1310 if (ppc_md.pcibios_fixup)
1311 ppc_md.pcibios_fixup();
1312
1313 /* Allocate and assign resources */
1314 pcibios_allocate_bus_resources(&pci_root_buses);
1315 pcibios_allocate_resources(0);
1316 pcibios_allocate_resources(1);
1317 pcibios_fixup_p2p_bridges();
1318 pcibios_assign_resources();
1319
1320 /* Call machine dependent post-init code */
1321 if (ppc_md.pcibios_after_init)
1322 ppc_md.pcibios_after_init();
1323 }
1324
1325 unsigned char __init
common_swizzle(struct pci_dev * dev,unsigned char * pinp)1326 common_swizzle(struct pci_dev *dev, unsigned char *pinp)
1327 {
1328 struct pci_controller *hose = dev->sysdata;
1329
1330 if (dev->bus->number != hose->first_busno) {
1331 u8 pin = *pinp;
1332 do {
1333 pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
1334 /* Move up the chain of bridges. */
1335 dev = dev->bus->self;
1336 } while (dev->bus->self);
1337 *pinp = pin;
1338
1339 /* The slot is the idsel of the last bridge. */
1340 }
1341 return PCI_SLOT(dev->devfn);
1342 }
1343
1344 void __init
pcibios_fixup_pbus_ranges(struct pci_bus * bus,struct pbus_set_ranges_data * ranges)1345 pcibios_fixup_pbus_ranges(struct pci_bus * bus, struct pbus_set_ranges_data * ranges)
1346 {
1347 ranges->io_start -= bus->resource[0]->start;
1348 ranges->io_end -= bus->resource[0]->start;
1349 ranges->mem_start -= bus->resource[1]->start;
1350 ranges->mem_end -= bus->resource[1]->start;
1351 }
1352
resource_fixup(struct pci_dev * dev,struct resource * res,unsigned long start,unsigned long size)1353 unsigned long resource_fixup(struct pci_dev * dev, struct resource * res,
1354 unsigned long start, unsigned long size)
1355 {
1356 return start;
1357 }
1358
pcibios_fixup_bus(struct pci_bus * bus)1359 void __init pcibios_fixup_bus(struct pci_bus *bus)
1360 {
1361 struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
1362 unsigned long io_offset;
1363 struct resource *res;
1364 int i;
1365
1366 io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
1367 if (bus->parent == NULL) {
1368 /* This is a host bridge - fill in its resources */
1369 hose->bus = bus;
1370
1371 bus->resource[0] = res = &hose->io_resource;
1372 if (!res->flags) {
1373 if (io_offset)
1374 printk(KERN_ERR "I/O resource not set for host"
1375 " bridge %d\n", hose->index);
1376 res->start = 0;
1377 res->end = IO_SPACE_LIMIT;
1378 res->flags = IORESOURCE_IO;
1379 }
1380 res->start += io_offset;
1381 res->end += io_offset;
1382
1383 for (i = 0; i < 3; ++i) {
1384 res = &hose->mem_resources[i];
1385 if (!res->flags) {
1386 if (i > 0)
1387 continue;
1388 printk(KERN_ERR "Memory resource not set for "
1389 "host bridge %d\n", hose->index);
1390 res->start = hose->pci_mem_offset;
1391 res->end = ~0U;
1392 res->flags = IORESOURCE_MEM;
1393 }
1394 bus->resource[i+1] = res;
1395 }
1396 } else {
1397 /* This is a subordinate bridge */
1398 pci_read_bridge_bases(bus);
1399
1400 for (i = 0; i < 4; ++i) {
1401 if ((res = bus->resource[i]) == NULL)
1402 continue;
1403 if (!res->flags)
1404 continue;
1405 if (io_offset && (res->flags & IORESOURCE_IO)) {
1406 res->start += io_offset;
1407 res->end += io_offset;
1408 } else if (hose->pci_mem_offset
1409 && (res->flags & IORESOURCE_MEM)) {
1410 res->start += hose->pci_mem_offset;
1411 res->end += hose->pci_mem_offset;
1412 }
1413 }
1414 }
1415
1416 if (ppc_md.pcibios_fixup_bus)
1417 ppc_md.pcibios_fixup_bus(bus);
1418 }
1419
pcibios_setup(char * str)1420 char __init *pcibios_setup(char *str)
1421 {
1422 return str;
1423 }
1424
1425 /* the next one is stolen from the alpha port... */
1426 void __init
pcibios_update_irq(struct pci_dev * dev,int irq)1427 pcibios_update_irq(struct pci_dev *dev, int irq)
1428 {
1429 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
1430 /* XXX FIXME - update OF device tree node interrupt property */
1431 }
1432
pcibios_enable_device(struct pci_dev * dev,int mask)1433 int pcibios_enable_device(struct pci_dev *dev, int mask)
1434 {
1435 u16 cmd, old_cmd;
1436 int idx;
1437 struct resource *r;
1438
1439 if (ppc_md.pcibios_enable_device_hook)
1440 if (ppc_md.pcibios_enable_device_hook(dev, 0))
1441 return -EINVAL;
1442
1443 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1444 old_cmd = cmd;
1445 for (idx=0; idx<6; idx++) {
1446 if(!(mask & (1<<idx)))
1447 continue;
1448
1449 r = &dev->resource[idx];
1450 if (r->flags & IORESOURCE_UNSET) {
1451 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", dev->slot_name);
1452 return -EINVAL;
1453 }
1454 if (r->flags & IORESOURCE_IO)
1455 cmd |= PCI_COMMAND_IO;
1456 if (r->flags & IORESOURCE_MEM)
1457 cmd |= PCI_COMMAND_MEMORY;
1458 }
1459 if (cmd != old_cmd) {
1460 printk("PCI: Enabling device %s (%04x -> %04x)\n",
1461 dev->slot_name, old_cmd, cmd);
1462 pci_write_config_word(dev, PCI_COMMAND, cmd);
1463 }
1464 return 0;
1465 }
1466
1467 struct pci_controller*
pci_bus_to_hose(int bus)1468 pci_bus_to_hose(int bus)
1469 {
1470 struct pci_controller* hose = hose_head;
1471
1472 for (; hose; hose = hose->next)
1473 if (bus >= hose->first_busno && bus <= hose->last_busno)
1474 return hose;
1475 return NULL;
1476 }
1477
1478 void*
pci_bus_io_base(unsigned int bus)1479 pci_bus_io_base(unsigned int bus)
1480 {
1481 struct pci_controller *hose;
1482
1483 hose = pci_bus_to_hose(bus);
1484 if (!hose)
1485 return NULL;
1486 return hose->io_base_virt;
1487 }
1488
1489 unsigned long
pci_bus_io_base_phys(unsigned int bus)1490 pci_bus_io_base_phys(unsigned int bus)
1491 {
1492 struct pci_controller *hose;
1493
1494 hose = pci_bus_to_hose(bus);
1495 if (!hose)
1496 return 0;
1497 return hose->io_base_phys;
1498 }
1499
1500 unsigned long
pci_bus_mem_base_phys(unsigned int bus)1501 pci_bus_mem_base_phys(unsigned int bus)
1502 {
1503 struct pci_controller *hose;
1504
1505 hose = pci_bus_to_hose(bus);
1506 if (!hose)
1507 return 0;
1508 return hose->pci_mem_offset;
1509 }
1510
1511 unsigned long
pci_resource_to_bus(struct pci_dev * pdev,struct resource * res)1512 pci_resource_to_bus(struct pci_dev *pdev, struct resource *res)
1513 {
1514 /* Hack alert again ! See comments in chrp_pci.c
1515 */
1516 struct pci_controller* hose =
1517 (struct pci_controller *)pdev->sysdata;
1518 if (hose && res->flags & IORESOURCE_MEM)
1519 return res->start - hose->pci_mem_offset;
1520 /* We may want to do something with IOs here... */
1521 return res->start;
1522 }
1523
1524 /*
1525 * Return the index of the PCI controller for device pdev.
1526 */
pci_controller_num(struct pci_dev * dev)1527 int pci_controller_num(struct pci_dev *dev)
1528 {
1529 struct pci_controller *hose = (struct pci_controller *) dev->sysdata;
1530
1531 return hose->index;
1532 }
1533
1534 /*
1535 * Platform support for /proc/bus/pci/X/Y mmap()s,
1536 * modelled on the sparc64 implementation by Dave Miller.
1537 * -- paulus.
1538 */
1539
1540 /*
1541 * Adjust vm_pgoff of VMA such that it is the physical page offset
1542 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
1543 *
1544 * Basically, the user finds the base address for his device which he wishes
1545 * to mmap. They read the 32-bit value from the config space base register,
1546 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
1547 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
1548 *
1549 * Returns negative error code on failure, zero on success.
1550 */
1551 static __inline__ int
__pci_mmap_make_offset(struct pci_dev * dev,struct vm_area_struct * vma,enum pci_mmap_state mmap_state)1552 __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
1553 enum pci_mmap_state mmap_state)
1554 {
1555 struct pci_controller *hose = (struct pci_controller *) dev->sysdata;
1556 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1557 unsigned long size = vma->vm_end - vma->vm_start;
1558 unsigned long base;
1559 struct resource *res;
1560 int i;
1561 int ret = -EINVAL;
1562
1563 if (hose == 0)
1564 return -EINVAL; /* should never happen */
1565 if (offset + size <= offset)
1566 return -EINVAL;
1567
1568 if (mmap_state == pci_mmap_mem) {
1569 /* PCI memory space */
1570 base = hose->pci_mem_offset;
1571 for (i = 0; i < 3; ++i) {
1572 res = &hose->mem_resources[i];
1573 if (offset >= res->start - base
1574 && offset + size - 1 <= res->end - base) {
1575 ret = 0;
1576 break;
1577 }
1578 }
1579 offset += hose->pci_mem_offset;
1580 } else {
1581 /* PCI I/O space */
1582 base = (unsigned long)hose->io_base_virt - isa_io_base;
1583 res = &hose->io_resource;
1584 if (offset >= res->start - base
1585 && offset + size - 1 <= res->end - base)
1586 ret = 0;
1587 offset += hose->io_base_phys;
1588 }
1589
1590 vma->vm_pgoff = offset >> PAGE_SHIFT;
1591 return ret;
1592 }
1593
1594 /*
1595 * Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1596 * mapping.
1597 */
1598 static __inline__ void
__pci_mmap_set_flags(struct pci_dev * dev,struct vm_area_struct * vma,enum pci_mmap_state mmap_state)1599 __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1600 enum pci_mmap_state mmap_state)
1601 {
1602 vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
1603 }
1604
1605 /*
1606 * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1607 * device mapping.
1608 */
1609 static __inline__ void
__pci_mmap_set_pgprot(struct pci_dev * dev,struct vm_area_struct * vma,enum pci_mmap_state mmap_state,int write_combine)1610 __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1611 enum pci_mmap_state mmap_state, int write_combine)
1612 {
1613 int prot = pgprot_val(vma->vm_page_prot);
1614
1615 /* XXX would be nice to have a way to ask for write-through */
1616 prot |= _PAGE_NO_CACHE;
1617 if (!write_combine)
1618 prot |= _PAGE_GUARDED;
1619 vma->vm_page_prot = __pgprot(prot);
1620 }
1621
1622 /*
1623 * Perform the actual remap of the pages for a PCI device mapping, as
1624 * appropriate for this architecture. The region in the process to map
1625 * is described by vm_start and vm_end members of VMA, the base physical
1626 * address is found in vm_pgoff.
1627 * The pci device structure is provided so that architectures may make mapping
1628 * decisions on a per-device or per-bus basis.
1629 *
1630 * Returns a negative error code on failure, zero on success.
1631 */
pci_mmap_page_range(struct pci_dev * dev,struct vm_area_struct * vma,enum pci_mmap_state mmap_state,int write_combine)1632 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1633 enum pci_mmap_state mmap_state,
1634 int write_combine)
1635 {
1636 int ret;
1637
1638 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1639 if (ret < 0)
1640 return ret;
1641
1642 __pci_mmap_set_flags(dev, vma, mmap_state);
1643 __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
1644
1645 ret = remap_page_range(vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
1646 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1647
1648 return ret;
1649 }
1650
1651 /* Obsolete functions. Should be removed once the symbios driver
1652 * is fixed
1653 */
1654 unsigned long
phys_to_bus(unsigned long pa)1655 phys_to_bus(unsigned long pa)
1656 {
1657 struct pci_controller *hose;
1658 int i;
1659
1660 for (hose = hose_head; hose; hose = hose->next) {
1661 for (i = 0; i < 3; ++i) {
1662 if (pa >= hose->mem_resources[i].start
1663 && pa <= hose->mem_resources[i].end) {
1664 /*
1665 * XXX the hose->pci_mem_offset really
1666 * only applies to mem_resources[0].
1667 * We need a way to store an offset for
1668 * the others. -- paulus
1669 */
1670 if (i == 0)
1671 pa -= hose->pci_mem_offset;
1672 return pa;
1673 }
1674 }
1675 }
1676 /* hmmm, didn't find it */
1677 return 0;
1678 }
1679
1680 unsigned long
pci_phys_to_bus(unsigned long pa,int busnr)1681 pci_phys_to_bus(unsigned long pa, int busnr)
1682 {
1683 struct pci_controller* hose = pci_bus_to_hose(busnr);
1684 if (!hose)
1685 return pa;
1686 return pa - hose->pci_mem_offset;
1687 }
1688
1689 unsigned long
pci_bus_to_phys(unsigned int ba,int busnr)1690 pci_bus_to_phys(unsigned int ba, int busnr)
1691 {
1692 struct pci_controller* hose = pci_bus_to_hose(busnr);
1693 if (!hose)
1694 return ba;
1695 return ba + hose->pci_mem_offset;
1696 }
1697
1698 /* Provide information on locations of various I/O regions in physical
1699 * memory. Do this on a per-card basis so that we choose the right
1700 * root bridge.
1701 * Note that the returned IO or memory base is a physical address
1702 */
1703
1704 long
sys_pciconfig_iobase(long which,unsigned long bus,unsigned long devfn)1705 sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
1706 {
1707 struct pci_controller* hose = pci_bus_to_hose(bus);
1708 long result = -EOPNOTSUPP;
1709
1710 if (!hose)
1711 return -ENODEV;
1712
1713 switch (which) {
1714 case IOBASE_BRIDGE_NUMBER:
1715 return (long)hose->first_busno;
1716 case IOBASE_MEMORY:
1717 return (long)hose->pci_mem_offset;
1718 case IOBASE_IO:
1719 return (long)hose->io_base_phys;
1720 case IOBASE_ISA_IO:
1721 return (long)isa_io_base;
1722 case IOBASE_ISA_MEM:
1723 return (long)isa_mem_base;
1724 }
1725
1726 return result;
1727 }
1728
1729 void __init
pci_init_resource(struct resource * res,unsigned long start,unsigned long end,int flags,char * name)1730 pci_init_resource(struct resource *res, unsigned long start, unsigned long end,
1731 int flags, char *name)
1732 {
1733 res->start = start;
1734 res->end = end;
1735 res->flags = flags;
1736 res->name = name;
1737 res->parent = NULL;
1738 res->sibling = NULL;
1739 res->child = NULL;
1740 }
1741
1742 /*
1743 * Null PCI config access functions, for the case when we can't
1744 * find a hose.
1745 */
1746 #define NULL_PCI_OP(rw, size, type) \
1747 static int \
1748 null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
1749 { \
1750 return PCIBIOS_DEVICE_NOT_FOUND; \
1751 }
1752
1753 NULL_PCI_OP(read, byte, u8 *)
1754 NULL_PCI_OP(read, word, u16 *)
1755 NULL_PCI_OP(read, dword, u32 *)
1756 NULL_PCI_OP(write, byte, u8)
1757 NULL_PCI_OP(write, word, u16)
1758 NULL_PCI_OP(write, dword, u32)
1759
1760 static struct pci_ops null_pci_ops =
1761 {
1762 null_read_config_byte,
1763 null_read_config_word,
1764 null_read_config_dword,
1765 null_write_config_byte,
1766 null_write_config_word,
1767 null_write_config_dword
1768 };
1769
1770 /*
1771 * These functions are used early on before PCI scanning is done
1772 * and all of the pci_dev and pci_bus structures have been created.
1773 */
1774 static struct pci_dev *
fake_pci_dev(struct pci_controller * hose,int busnr,int devfn)1775 fake_pci_dev(struct pci_controller *hose, int busnr, int devfn)
1776 {
1777 static struct pci_dev dev;
1778 static struct pci_bus bus;
1779
1780 if (hose == 0) {
1781 hose = pci_bus_to_hose(busnr);
1782 if (hose == 0)
1783 printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
1784 }
1785 dev.bus = &bus;
1786 dev.sysdata = hose;
1787 dev.devfn = devfn;
1788 bus.number = busnr;
1789 bus.ops = hose? hose->ops: &null_pci_ops;
1790 return &dev;
1791 }
1792
1793 #define EARLY_PCI_OP(rw, size, type) \
1794 int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
1795 int devfn, int offset, type value) \
1796 { \
1797 return pci_##rw##_config_##size(fake_pci_dev(hose, bus, devfn), \
1798 offset, value); \
1799 }
1800
1801 EARLY_PCI_OP(read, byte, u8 *)
1802 EARLY_PCI_OP(read, word, u16 *)
1803 EARLY_PCI_OP(read, dword, u32 *)
1804 EARLY_PCI_OP(write, byte, u8)
1805 EARLY_PCI_OP(write, word, u16)
1806 EARLY_PCI_OP(write, dword, u32)
1807