blob: b06a2955bf5f720b30a1af3db2206de8f9f4d215 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: pci_common.c,v 1.29 2002/02/01 00:56:03 davem Exp $
2 * pci_common.c: PCI controller common support.
3 *
4 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
5 */
6
7#include <linux/string.h>
8#include <linux/slab.h>
9#include <linux/init.h>
10
11#include <asm/pbm.h>
David S. Millerde8d28b2006-06-22 16:18:54 -070012#include <asm/prom.h>
13
14#include "pci_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
David S. Miller20edac8a2006-06-21 22:07:21 -070016/* Pass "pci=irq_verbose" on the kernel command line to enable this. */
17int pci_irq_verbose;
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/* Fix self device of BUS and hook it into BUS->self.
20 * The pci_scan_bus does not do this for the host bridge.
21 */
22void __init pci_fixup_host_bridge_self(struct pci_bus *pbus)
23{
24 struct pci_dev *pdev;
25
26 list_for_each_entry(pdev, &pbus->devices, bus_list) {
27 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_HOST) {
28 pbus->self = pdev;
29 return;
30 }
31 }
32
33 prom_printf("PCI: Critical error, cannot find host bridge PDEV.\n");
34 prom_halt();
35}
36
David S. Millerde8d28b2006-06-22 16:18:54 -070037/* Find the OBP PROM device tree node for a PCI device. */
38static struct device_node * __init
39find_device_prom_node(struct pci_pbm_info *pbm, struct pci_dev *pdev,
40 struct device_node *bus_node,
41 struct linux_prom_pci_registers **pregs,
42 int *nregs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
David S. Millerde8d28b2006-06-22 16:18:54 -070044 struct device_node *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
David S. Miller355db992006-02-14 16:44:39 -080046 *nregs = 0;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 /*
49 * Return the PBM's PROM node in case we are it's PCI device,
50 * as the PBM's reg property is different to standard PCI reg
51 * properties. We would delete this device entry otherwise,
52 * which confuses XFree86's device probing...
53 */
54 if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
55 (pdev->vendor == PCI_VENDOR_ID_SUN) &&
56 (pdev->device == PCI_DEVICE_ID_SUN_PBM ||
57 pdev->device == PCI_DEVICE_ID_SUN_SCHIZO ||
58 pdev->device == PCI_DEVICE_ID_SUN_TOMATILLO ||
59 pdev->device == PCI_DEVICE_ID_SUN_SABRE ||
David S. Miller355db992006-02-14 16:44:39 -080060 pdev->device == PCI_DEVICE_ID_SUN_HUMMINGBIRD))
David S. Millerde8d28b2006-06-22 16:18:54 -070061 return bus_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
David S. Millerde8d28b2006-06-22 16:18:54 -070063 dp = bus_node->child;
64 while (dp) {
65 struct linux_prom_pci_registers *regs;
66 struct property *prop;
67 int len;
68
69 prop = of_find_property(dp, "reg", &len);
70 if (!prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 goto do_next_sibling;
David S. Millerde8d28b2006-06-22 16:18:54 -070072
73 regs = prop->value;
74 if (((regs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
75 *pregs = regs;
76 *nregs = len / sizeof(struct linux_prom_pci_registers);
77 return dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
80 do_next_sibling:
David S. Millerde8d28b2006-06-22 16:18:54 -070081 dp = dp->sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
David S. Millerde8d28b2006-06-22 16:18:54 -070083
84 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/* Older versions of OBP on PCI systems encode 64-bit MEM
88 * space assignments incorrectly, this fixes them up. We also
89 * take the opportunity here to hide other kinds of bogus
90 * assignments.
91 */
92static void __init fixup_obp_assignments(struct pci_dev *pdev,
93 struct pcidev_cookie *pcp)
94{
95 int i;
96
97 if (pdev->vendor == PCI_VENDOR_ID_AL &&
98 (pdev->device == PCI_DEVICE_ID_AL_M7101 ||
99 pdev->device == PCI_DEVICE_ID_AL_M1533)) {
100 int i;
101
102 /* Zap all of the normal resources, they are
103 * meaningless and generate bogus resource collision
104 * messages. This is OpenBoot's ill-fated attempt to
105 * represent the implicit resources that these devices
106 * have.
107 */
108 pcp->num_prom_assignments = 0;
109 for (i = 0; i < 6; i++) {
110 pdev->resource[i].start =
111 pdev->resource[i].end =
112 pdev->resource[i].flags = 0;
113 }
114 pdev->resource[PCI_ROM_RESOURCE].start =
115 pdev->resource[PCI_ROM_RESOURCE].end =
116 pdev->resource[PCI_ROM_RESOURCE].flags = 0;
117 return;
118 }
119
120 for (i = 0; i < pcp->num_prom_assignments; i++) {
121 struct linux_prom_pci_registers *ap;
122 int space;
123
124 ap = &pcp->prom_assignments[i];
125 space = ap->phys_hi >> 24;
126 if ((space & 0x3) == 2 &&
127 (space & 0x4) != 0) {
128 ap->phys_hi &= ~(0x7 << 24);
129 ap->phys_hi |= 0x3 << 24;
130 }
131 }
132}
133
134/* Fill in the PCI device cookie sysdata for the given
135 * PCI device. This cookie is the means by which one
136 * can get to OBP and PCI controller specific information
137 * for a PCI device.
138 */
139static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
140 struct pci_dev *pdev,
David S. Millerde8d28b2006-06-22 16:18:54 -0700141 struct device_node *bus_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
David S. Millerde8d28b2006-06-22 16:18:54 -0700143 struct linux_prom_pci_registers *pregs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct pcidev_cookie *pcp;
David S. Millerde8d28b2006-06-22 16:18:54 -0700145 struct device_node *dp;
146 struct property *prop;
147 int nregs, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David S. Millerde8d28b2006-06-22 16:18:54 -0700149 dp = find_device_prom_node(pbm, pdev, bus_node,
150 &pregs, &nregs);
151 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* If it is not in the OBP device tree then
153 * there must be a damn good reason for it.
154 *
155 * So what we do is delete the device from the
156 * PCI device tree completely. This scenario
157 * is seen, for example, on CP1500 for the
158 * second EBUS/HappyMeal pair if the external
159 * connector for it is not present.
160 */
161 pci_remove_bus_device(pdev);
162 return;
163 }
164
David S. Millerde8d28b2006-06-22 16:18:54 -0700165 pcp = kzalloc(sizeof(*pcp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (pcp == NULL) {
167 prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
168 prom_halt();
169 }
170 pcp->pbm = pbm;
David S. Millerde8d28b2006-06-22 16:18:54 -0700171 pcp->prom_node = dp;
172 memcpy(pcp->prom_regs, pregs,
173 nregs * sizeof(struct linux_prom_pci_registers));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 pcp->num_prom_regs = nregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David S. Millerde8d28b2006-06-22 16:18:54 -0700176 /* We can't have the pcidev_cookie assignments be just
177 * direct pointers into the property value, since they
178 * are potentially modified by the probing process.
179 */
180 prop = of_find_property(dp, "assigned-addresses", &len);
181 if (!prop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 pcp->num_prom_assignments = 0;
David S. Millerde8d28b2006-06-22 16:18:54 -0700183 } else {
184 memcpy(pcp->prom_assignments, prop->value, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 pcp->num_prom_assignments =
David S. Millerde8d28b2006-06-22 16:18:54 -0700186 (len / sizeof(pcp->prom_assignments[0]));
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
David S. Millerde8d28b2006-06-22 16:18:54 -0700189 if (strcmp(dp->name, "ebus") == 0) {
190 struct linux_prom_ebus_ranges *erng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int iter;
192
193 /* EBUS is special... */
David S. Millerde8d28b2006-06-22 16:18:54 -0700194 prop = of_find_property(dp, "ranges", &len);
195 if (!prop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 prom_printf("EBUS: Fatal error, no range property\n");
197 prom_halt();
198 }
David S. Millerde8d28b2006-06-22 16:18:54 -0700199 erng = prop->value;
200 len = (len / sizeof(erng[0]));
201 for (iter = 0; iter < len; iter++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct linux_prom_ebus_ranges *ep = &erng[iter];
203 struct linux_prom_pci_registers *ap;
204
205 ap = &pcp->prom_assignments[iter];
206
207 ap->phys_hi = ep->parent_phys_hi;
208 ap->phys_mid = ep->parent_phys_mid;
209 ap->phys_lo = ep->parent_phys_lo;
210 ap->size_hi = 0;
211 ap->size_lo = ep->size;
212 }
David S. Millerde8d28b2006-06-22 16:18:54 -0700213 pcp->num_prom_assignments = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 fixup_obp_assignments(pdev, pcp);
217
218 pdev->sysdata = pcp;
219}
220
221void __init pci_fill_in_pbm_cookies(struct pci_bus *pbus,
222 struct pci_pbm_info *pbm,
David S. Millerde8d28b2006-06-22 16:18:54 -0700223 struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct pci_dev *pdev, *pdev_next;
226 struct pci_bus *this_pbus, *pbus_next;
227
228 /* This must be _safe because the cookie fillin
229 routine can delete devices from the tree. */
230 list_for_each_entry_safe(pdev, pdev_next, &pbus->devices, bus_list)
David S. Millerde8d28b2006-06-22 16:18:54 -0700231 pdev_cookie_fillin(pbm, pdev, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 list_for_each_entry_safe(this_pbus, pbus_next, &pbus->children, node) {
234 struct pcidev_cookie *pcp = this_pbus->self->sysdata;
235
236 pci_fill_in_pbm_cookies(this_pbus, pbm, pcp->prom_node);
237 }
238}
239
240static void __init bad_assignment(struct pci_dev *pdev,
241 struct linux_prom_pci_registers *ap,
242 struct resource *res,
243 int do_prom_halt)
244{
245 prom_printf("PCI: Bogus PROM assignment. BUS[%02x] DEVFN[%x]\n",
246 pdev->bus->number, pdev->devfn);
247 if (ap)
248 prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n",
249 ap->phys_hi, ap->phys_mid, ap->phys_lo,
250 ap->size_hi, ap->size_lo);
251 if (res)
252 prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n",
253 res->start, res->end, res->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (do_prom_halt)
255 prom_halt();
256}
257
258static struct resource *
259__init get_root_resource(struct linux_prom_pci_registers *ap,
260 struct pci_pbm_info *pbm)
261{
262 int space = (ap->phys_hi >> 24) & 3;
263
264 switch (space) {
265 case 0:
266 /* Configuration space, silently ignore it. */
267 return NULL;
268
269 case 1:
270 /* 16-bit IO space */
271 return &pbm->io_space;
272
273 case 2:
274 /* 32-bit MEM space */
275 return &pbm->mem_space;
276
277 case 3:
278 /* 64-bit MEM space, these are allocated out of
279 * the 32-bit mem_space range for the PBM, ie.
280 * we just zero out the upper 32-bits.
281 */
282 return &pbm->mem_space;
283
284 default:
David S. Millerde8d28b2006-06-22 16:18:54 -0700285 printk("PCI: What is resource space %x?\n", space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return NULL;
287 };
288}
289
290static struct resource *
291__init get_device_resource(struct linux_prom_pci_registers *ap,
292 struct pci_dev *pdev)
293{
294 struct resource *res;
295 int breg = (ap->phys_hi & 0xff);
296
297 switch (breg) {
298 case PCI_ROM_ADDRESS:
299 /* Unfortunately I have seen several cases where
300 * buggy FCODE uses a space value of '1' (I/O space)
301 * in the register property for the ROM address
302 * so disable this sanity check for now.
303 */
304#if 0
305 {
306 int space = (ap->phys_hi >> 24) & 3;
307
308 /* It had better be MEM space. */
309 if (space != 2)
310 bad_assignment(pdev, ap, NULL, 0);
311 }
312#endif
313 res = &pdev->resource[PCI_ROM_RESOURCE];
314 break;
315
316 case PCI_BASE_ADDRESS_0:
317 case PCI_BASE_ADDRESS_1:
318 case PCI_BASE_ADDRESS_2:
319 case PCI_BASE_ADDRESS_3:
320 case PCI_BASE_ADDRESS_4:
321 case PCI_BASE_ADDRESS_5:
322 res = &pdev->resource[(breg - PCI_BASE_ADDRESS_0) / 4];
323 break;
324
325 default:
326 bad_assignment(pdev, ap, NULL, 0);
327 res = NULL;
328 break;
329 };
330
331 return res;
332}
333
334static int __init pdev_resource_collisions_expected(struct pci_dev *pdev)
335{
336 if (pdev->vendor != PCI_VENDOR_ID_SUN)
337 return 0;
338
339 if (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS ||
340 pdev->device == PCI_DEVICE_ID_SUN_RIO_1394 ||
341 pdev->device == PCI_DEVICE_ID_SUN_RIO_USB)
342 return 1;
343
344 return 0;
345}
346
347static void __init pdev_record_assignments(struct pci_pbm_info *pbm,
348 struct pci_dev *pdev)
349{
350 struct pcidev_cookie *pcp = pdev->sysdata;
351 int i;
352
353 for (i = 0; i < pcp->num_prom_assignments; i++) {
354 struct linux_prom_pci_registers *ap;
355 struct resource *root, *res;
356
357 /* The format of this property is specified in
358 * the PCI Bus Binding to IEEE1275-1994.
359 */
360 ap = &pcp->prom_assignments[i];
361 root = get_root_resource(ap, pbm);
362 res = get_device_resource(ap, pdev);
363 if (root == NULL || res == NULL ||
364 res->flags == 0)
365 continue;
366
367 /* Ok we know which resource this PROM assignment is
368 * for, sanity check it.
369 */
370 if ((res->start & 0xffffffffUL) != ap->phys_lo)
371 bad_assignment(pdev, ap, res, 1);
372
373 /* If it is a 64-bit MEM space assignment, verify that
374 * the resource is too and that the upper 32-bits match.
375 */
376 if (((ap->phys_hi >> 24) & 3) == 3) {
377 if (((res->flags & IORESOURCE_MEM) == 0) ||
378 ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
379 != PCI_BASE_ADDRESS_MEM_TYPE_64))
380 bad_assignment(pdev, ap, res, 1);
381 if ((res->start >> 32) != ap->phys_mid)
382 bad_assignment(pdev, ap, res, 1);
383
384 /* PBM cannot generate cpu initiated PIOs
385 * to the full 64-bit space. Therefore the
386 * upper 32-bits better be zero. If it is
387 * not, just skip it and we will assign it
388 * properly ourselves.
389 */
390 if ((res->start >> 32) != 0UL) {
391 printk(KERN_ERR "PCI: OBP assigns out of range MEM address "
392 "%016lx for region %ld on device %s\n",
393 res->start, (res - &pdev->resource[0]), pci_name(pdev));
394 continue;
395 }
396 }
397
398 /* Adjust the resource into the physical address space
399 * of this PBM.
400 */
401 pbm->parent->resource_adjust(pdev, res, root);
402
403 if (request_resource(root, res) < 0) {
404 /* OK, there is some conflict. But this is fine
405 * since we'll reassign it in the fixup pass.
406 *
407 * We notify the user that OBP made an error if it
408 * is a case we don't expect.
409 */
410 if (!pdev_resource_collisions_expected(pdev)) {
411 printk(KERN_ERR "PCI: Address space collision on region %ld "
412 "[%016lx:%016lx] of device %s\n",
413 (res - &pdev->resource[0]),
414 res->start, res->end,
415 pci_name(pdev));
416 }
417 }
418 }
419}
420
421void __init pci_record_assignments(struct pci_pbm_info *pbm,
422 struct pci_bus *pbus)
423{
424 struct pci_dev *dev;
425 struct pci_bus *bus;
426
427 list_for_each_entry(dev, &pbus->devices, bus_list)
428 pdev_record_assignments(pbm, dev);
429
430 list_for_each_entry(bus, &pbus->children, node)
431 pci_record_assignments(pbm, bus);
432}
433
434/* Return non-zero if PDEV has implicit I/O resources even
435 * though it may not have an I/O base address register
436 * active.
437 */
438static int __init has_implicit_io(struct pci_dev *pdev)
439{
440 int class = pdev->class >> 8;
441
442 if (class == PCI_CLASS_NOT_DEFINED ||
443 class == PCI_CLASS_NOT_DEFINED_VGA ||
444 class == PCI_CLASS_STORAGE_IDE ||
445 (pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
446 return 1;
447
448 return 0;
449}
450
451static void __init pdev_assign_unassigned(struct pci_pbm_info *pbm,
452 struct pci_dev *pdev)
453{
454 u32 reg;
455 u16 cmd;
456 int i, io_seen, mem_seen;
457
458 io_seen = mem_seen = 0;
459 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
460 struct resource *root, *res;
461 unsigned long size, min, max, align;
462
463 res = &pdev->resource[i];
464
465 if (res->flags & IORESOURCE_IO)
466 io_seen++;
467 else if (res->flags & IORESOURCE_MEM)
468 mem_seen++;
469
470 /* If it is already assigned or the resource does
471 * not exist, there is nothing to do.
472 */
473 if (res->parent != NULL || res->flags == 0UL)
474 continue;
475
476 /* Determine the root we allocate from. */
477 if (res->flags & IORESOURCE_IO) {
478 root = &pbm->io_space;
479 min = root->start + 0x400UL;
480 max = root->end;
481 } else {
482 root = &pbm->mem_space;
483 min = root->start;
484 max = min + 0x80000000UL;
485 }
486
487 size = res->end - res->start;
488 align = size + 1;
489 if (allocate_resource(root, res, size + 1, min, max, align, NULL, NULL) < 0) {
490 /* uh oh */
491 prom_printf("PCI: Failed to allocate resource %d for %s\n",
492 i, pci_name(pdev));
493 prom_halt();
494 }
495
496 /* Update PCI config space. */
497 pbm->parent->base_address_update(pdev, i);
498 }
499
500 /* Special case, disable the ROM. Several devices
501 * act funny (ie. do not respond to memory space writes)
502 * when it is left enabled. A good example are Qlogic,ISP
503 * adapters.
504 */
505 pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &reg);
506 reg &= ~PCI_ROM_ADDRESS_ENABLE;
507 pci_write_config_dword(pdev, PCI_ROM_ADDRESS, reg);
508
509 /* If we saw I/O or MEM resources, enable appropriate
510 * bits in PCI command register.
511 */
512 if (io_seen || mem_seen) {
513 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
514 if (io_seen || has_implicit_io(pdev))
515 cmd |= PCI_COMMAND_IO;
516 if (mem_seen)
517 cmd |= PCI_COMMAND_MEMORY;
518 pci_write_config_word(pdev, PCI_COMMAND, cmd);
519 }
520
521 /* If this is a PCI bridge or an IDE controller,
522 * enable bus mastering. In the former case also
523 * set the cache line size correctly.
524 */
525 if (((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) ||
526 (((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) &&
527 ((pdev->class & 0x80) != 0))) {
528 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
529 cmd |= PCI_COMMAND_MASTER;
530 pci_write_config_word(pdev, PCI_COMMAND, cmd);
531
532 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
533 pci_write_config_byte(pdev,
534 PCI_CACHE_LINE_SIZE,
535 (64 / sizeof(u32)));
536 }
537}
538
539void __init pci_assign_unassigned(struct pci_pbm_info *pbm,
540 struct pci_bus *pbus)
541{
542 struct pci_dev *dev;
543 struct pci_bus *bus;
544
545 list_for_each_entry(dev, &pbus->devices, bus_list)
546 pdev_assign_unassigned(pbm, dev);
547
548 list_for_each_entry(bus, &pbus->children, node)
549 pci_assign_unassigned(pbm, bus);
550}
551
David S. Miller6154f942006-02-16 23:01:10 -0800552static inline unsigned int pci_slot_swivel(struct pci_pbm_info *pbm,
553 struct pci_dev *toplevel_pdev,
554 struct pci_dev *pdev,
555 unsigned int interrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
David S. Miller6154f942006-02-16 23:01:10 -0800557 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
David S. Miller6154f942006-02-16 23:01:10 -0800559 if (unlikely(interrupt < 1 || interrupt > 4)) {
560 printk("%s: Device %s interrupt value of %u is strange.\n",
561 pbm->name, pci_name(pdev), interrupt);
562 return interrupt;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
David S. Miller6154f942006-02-16 23:01:10 -0800565 ret = ((interrupt - 1 + (PCI_SLOT(pdev->devfn) & 3)) & 3) + 1;
566
David S. Miller20edac8a2006-06-21 22:07:21 -0700567 if (pci_irq_verbose)
568 printk("%s: %s IRQ Swivel %s [%x:%x] -> [%x]\n",
569 pbm->name, pci_name(toplevel_pdev), pci_name(pdev),
570 interrupt, PCI_SLOT(pdev->devfn), ret);
David S. Miller6154f942006-02-16 23:01:10 -0800571
572 return ret;
573}
574
575static inline unsigned int pci_apply_intmap(struct pci_pbm_info *pbm,
576 struct pci_dev *toplevel_pdev,
577 struct pci_dev *pbus,
578 struct pci_dev *pdev,
579 unsigned int interrupt,
David S. Millerde8d28b2006-06-22 16:18:54 -0700580 struct device_node **cnode)
David S. Miller6154f942006-02-16 23:01:10 -0800581{
David S. Millerde8d28b2006-06-22 16:18:54 -0700582 struct linux_prom_pci_intmap *imap;
583 struct linux_prom_pci_intmask *imask;
David S. Miller6154f942006-02-16 23:01:10 -0800584 struct pcidev_cookie *pbus_pcp = pbus->sysdata;
585 struct pcidev_cookie *pdev_pcp = pdev->sysdata;
586 struct linux_prom_pci_registers *pregs = pdev_pcp->prom_regs;
David S. Millerde8d28b2006-06-22 16:18:54 -0700587 struct property *prop;
David S. Miller6154f942006-02-16 23:01:10 -0800588 int plen, num_imap, i;
589 unsigned int hi, mid, lo, irq, orig_interrupt;
590
591 *cnode = pbus_pcp->prom_node;
592
David S. Millerde8d28b2006-06-22 16:18:54 -0700593 prop = of_find_property(pbus_pcp->prom_node, "interrupt-map", &plen);
594 if (!prop ||
David S. Miller6154f942006-02-16 23:01:10 -0800595 (plen % sizeof(struct linux_prom_pci_intmap)) != 0) {
596 printk("%s: Device %s interrupt-map has bad len %d\n",
597 pbm->name, pci_name(pbus), plen);
598 goto no_intmap;
599 }
David S. Millerde8d28b2006-06-22 16:18:54 -0700600 imap = prop->value;
David S. Miller6154f942006-02-16 23:01:10 -0800601 num_imap = plen / sizeof(struct linux_prom_pci_intmap);
602
David S. Millerde8d28b2006-06-22 16:18:54 -0700603 prop = of_find_property(pbus_pcp->prom_node, "interrupt-map-mask", &plen);
604 if (!prop ||
David S. Miller6154f942006-02-16 23:01:10 -0800605 (plen % sizeof(struct linux_prom_pci_intmask)) != 0) {
606 printk("%s: Device %s interrupt-map-mask has bad len %d\n",
607 pbm->name, pci_name(pbus), plen);
608 goto no_intmap;
609 }
David S. Millerde8d28b2006-06-22 16:18:54 -0700610 imask = prop->value;
David S. Miller6154f942006-02-16 23:01:10 -0800611
612 orig_interrupt = interrupt;
613
David S. Millerde8d28b2006-06-22 16:18:54 -0700614 hi = pregs->phys_hi & imask->phys_hi;
615 mid = pregs->phys_mid & imask->phys_mid;
616 lo = pregs->phys_lo & imask->phys_lo;
617 irq = interrupt & imask->interrupt;
David S. Miller6154f942006-02-16 23:01:10 -0800618
619 for (i = 0; i < num_imap; i++) {
620 if (imap[i].phys_hi == hi &&
621 imap[i].phys_mid == mid &&
622 imap[i].phys_lo == lo &&
623 imap[i].interrupt == irq) {
David S. Millerde8d28b2006-06-22 16:18:54 -0700624 *cnode = of_find_node_by_phandle(imap[i].cnode);
David S. Miller6154f942006-02-16 23:01:10 -0800625 interrupt = imap[i].cinterrupt;
626 }
627 }
628
David S. Miller20edac8a2006-06-21 22:07:21 -0700629 if (pci_irq_verbose)
630 printk("%s: %s MAP BUS %s DEV %s [%x] -> [%x]\n",
631 pbm->name, pci_name(toplevel_pdev),
632 pci_name(pbus), pci_name(pdev),
633 orig_interrupt, interrupt);
David S. Miller6154f942006-02-16 23:01:10 -0800634
635no_intmap:
636 return interrupt;
637}
638
639/* For each PCI bus on the way to the root:
640 * 1) If it has an interrupt-map property, apply it.
641 * 2) Else, swivel the interrupt number based upon the PCI device number.
642 *
643 * Return the "IRQ controller" node. If this is the PBM's device node,
644 * all interrupt translations are complete, else we should use that node's
645 * "reg" property to apply the PBM's "interrupt-{map,mask}" to the interrupt.
646 */
David S. Millerde8d28b2006-06-22 16:18:54 -0700647static struct device_node * __init
648pci_intmap_match_to_root(struct pci_pbm_info *pbm,
649 struct pci_dev *pdev,
650 unsigned int *interrupt)
David S. Miller6154f942006-02-16 23:01:10 -0800651{
652 struct pci_dev *toplevel_pdev = pdev;
653 struct pcidev_cookie *toplevel_pcp = toplevel_pdev->sysdata;
David S. Millerde8d28b2006-06-22 16:18:54 -0700654 struct device_node *cnode = toplevel_pcp->prom_node;
David S. Miller6154f942006-02-16 23:01:10 -0800655
656 while (pdev->bus->number != pbm->pci_first_busno) {
657 struct pci_dev *pbus = pdev->bus->self;
658 struct pcidev_cookie *pcp = pbus->sysdata;
David S. Millerde8d28b2006-06-22 16:18:54 -0700659 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David S. Millerde8d28b2006-06-22 16:18:54 -0700661 prop = of_find_property(pcp->prom_node, "interrupt-map", NULL);
662 if (!prop) {
David S. Miller6154f942006-02-16 23:01:10 -0800663 *interrupt = pci_slot_swivel(pbm, toplevel_pdev,
664 pdev, *interrupt);
665 cnode = pcp->prom_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 } else {
David S. Miller6154f942006-02-16 23:01:10 -0800667 *interrupt = pci_apply_intmap(pbm, toplevel_pdev,
668 pbus, pdev,
669 *interrupt, &cnode);
670
671 while (pcp->prom_node != cnode &&
672 pbus->bus->number != pbm->pci_first_busno) {
673 pbus = pbus->bus->self;
674 pcp = pbus->sysdata;
675 }
676 }
677 pdev = pbus;
678
David S. Millerde8d28b2006-06-22 16:18:54 -0700679 if (cnode == pbm->prom_node)
David S. Miller6154f942006-02-16 23:01:10 -0800680 break;
681 }
682
683 return cnode;
684}
685
686static int __init pci_intmap_match(struct pci_dev *pdev, unsigned int *interrupt)
687{
688 struct pcidev_cookie *dev_pcp = pdev->sysdata;
689 struct pci_pbm_info *pbm = dev_pcp->pbm;
David S. Millerde8d28b2006-06-22 16:18:54 -0700690 struct linux_prom_pci_registers *reg;
691 struct device_node *cnode;
692 struct property *prop;
David S. Miller6154f942006-02-16 23:01:10 -0800693 unsigned int hi, mid, lo, irq;
David S. Millerde8d28b2006-06-22 16:18:54 -0700694 int i, plen;
David S. Miller6154f942006-02-16 23:01:10 -0800695
696 cnode = pci_intmap_match_to_root(pbm, pdev, interrupt);
David S. Millerde8d28b2006-06-22 16:18:54 -0700697 if (cnode == pbm->prom_node)
David S. Miller6154f942006-02-16 23:01:10 -0800698 goto success;
699
David S. Millerde8d28b2006-06-22 16:18:54 -0700700 prop = of_find_property(cnode, "reg", &plen);
701 if (!prop ||
David S. Miller6154f942006-02-16 23:01:10 -0800702 (plen % sizeof(struct linux_prom_pci_registers)) != 0) {
David S. Millerde8d28b2006-06-22 16:18:54 -0700703 printk("%s: OBP node %s reg property has bad len %d\n",
704 pbm->name, cnode->full_name, plen);
David S. Miller6154f942006-02-16 23:01:10 -0800705 goto fail;
706 }
David S. Millerde8d28b2006-06-22 16:18:54 -0700707 reg = prop->value;
David S. Miller6154f942006-02-16 23:01:10 -0800708
David S. Millere87dc352006-06-21 18:18:47 -0700709 hi = reg[0].phys_hi & pbm->pbm_intmask->phys_hi;
710 mid = reg[0].phys_mid & pbm->pbm_intmask->phys_mid;
711 lo = reg[0].phys_lo & pbm->pbm_intmask->phys_lo;
712 irq = *interrupt & pbm->pbm_intmask->interrupt;
David S. Miller6154f942006-02-16 23:01:10 -0800713
714 for (i = 0; i < pbm->num_pbm_intmap; i++) {
715 struct linux_prom_pci_intmap *intmap;
716
717 intmap = &pbm->pbm_intmap[i];
718
719 if (intmap->phys_hi == hi &&
720 intmap->phys_mid == mid &&
721 intmap->phys_lo == lo &&
722 intmap->interrupt == irq) {
723 *interrupt = intmap->cinterrupt;
724 goto success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 }
727
David S. Miller6154f942006-02-16 23:01:10 -0800728fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return 0;
David S. Miller6154f942006-02-16 23:01:10 -0800730
731success:
David S. Miller20edac8a2006-06-21 22:07:21 -0700732 if (pci_irq_verbose)
733 printk("%s: Routing bus[%2x] slot[%2x] to INO[%02x]\n",
734 pbm->name,
735 pdev->bus->number, PCI_SLOT(pdev->devfn),
736 *interrupt);
David S. Miller6154f942006-02-16 23:01:10 -0800737 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740static void __init pdev_fixup_irq(struct pci_dev *pdev)
741{
742 struct pcidev_cookie *pcp = pdev->sysdata;
743 struct pci_pbm_info *pbm = pcp->pbm;
744 struct pci_controller_info *p = pbm->parent;
745 unsigned int portid = pbm->portid;
746 unsigned int prom_irq;
David S. Millerde8d28b2006-06-22 16:18:54 -0700747 struct device_node *dp = pcp->prom_node;
748 struct property *prop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /* If this is an empty EBUS device, sometimes OBP fails to
751 * give it a valid fully specified interrupts property.
752 * The EBUS hooked up to SunHME on PCI I/O boards of
753 * Ex000 systems is one such case.
754 *
755 * The interrupt is not important so just ignore it.
756 */
757 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
758 pdev->device == PCI_DEVICE_ID_SUN_EBUS &&
David S. Millerde8d28b2006-06-22 16:18:54 -0700759 !dp->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 pdev->irq = 0;
761 return;
762 }
763
David S. Millerde8d28b2006-06-22 16:18:54 -0700764 prop = of_find_property(dp, "interrupts", NULL);
765 if (!prop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 pdev->irq = 0;
767 return;
768 }
David S. Millerde8d28b2006-06-22 16:18:54 -0700769 prom_irq = *(unsigned int *) prop->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David S. Millere7093702006-02-14 14:12:44 -0800771 if (tlb_type != hypervisor) {
772 /* Fully specified already? */
773 if (((prom_irq & PCI_IRQ_IGN) >> 6) == portid) {
774 pdev->irq = p->irq_build(pbm, pdev, prom_irq);
775 goto have_irq;
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
David S. Millere7093702006-02-14 14:12:44 -0800778 /* An onboard device? (bit 5 set) */
779 if ((prom_irq & PCI_IRQ_INO) & 0x20) {
780 pdev->irq = p->irq_build(pbm, pdev, (portid << 6 | prom_irq));
781 goto have_irq;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 /* Can we find a matching entry in the interrupt-map? */
786 if (pci_intmap_match(pdev, &prom_irq)) {
787 pdev->irq = p->irq_build(pbm, pdev, (portid << 6) | prom_irq);
788 goto have_irq;
789 }
790
791 /* Ok, we have to do it the hard way. */
792 {
793 unsigned int bus, slot, line;
794
795 bus = (pbm == &pbm->parent->pbm_B) ? (1 << 4) : 0;
796
797 /* If we have a legal interrupt property, use it as
798 * the IRQ line.
799 */
800 if (prom_irq > 0 && prom_irq < 5) {
801 line = ((prom_irq - 1) & 3);
802 } else {
803 u8 pci_irq_line;
804
805 /* Else just directly consult PCI config space. */
806 pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pci_irq_line);
807 line = ((pci_irq_line - 1) & 3);
808 }
809
810 /* Now figure out the slot.
811 *
812 * Basically, device number zero on the top-level bus is
813 * always the PCI host controller. Slot 0 is then device 1.
814 * PBM A supports two external slots (0 and 1), and PBM B
815 * supports 4 external slots (0, 1, 2, and 3). On-board PCI
816 * devices are wired to device numbers outside of these
817 * ranges. -DaveM
818 */
819 if (pdev->bus->number == pbm->pci_first_busno) {
820 slot = PCI_SLOT(pdev->devfn) - pbm->pci_first_slot;
821 } else {
822 struct pci_dev *bus_dev;
823
824 /* Underneath a bridge, use slot number of parent
825 * bridge which is closest to the PBM.
826 */
827 bus_dev = pdev->bus->self;
828 while (bus_dev->bus &&
829 bus_dev->bus->number != pbm->pci_first_busno)
830 bus_dev = bus_dev->bus->self;
831
832 slot = PCI_SLOT(bus_dev->devfn) - pbm->pci_first_slot;
833 }
834 slot = slot << 2;
835
836 pdev->irq = p->irq_build(pbm, pdev,
837 ((portid << 6) & PCI_IRQ_IGN) |
838 (bus | slot | line));
839 }
840
841have_irq:
842 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
843 pdev->irq & PCI_IRQ_INO);
844}
845
846void __init pci_fixup_irq(struct pci_pbm_info *pbm,
847 struct pci_bus *pbus)
848{
849 struct pci_dev *dev;
850 struct pci_bus *bus;
851
852 list_for_each_entry(dev, &pbus->devices, bus_list)
853 pdev_fixup_irq(dev);
854
855 list_for_each_entry(bus, &pbus->children, node)
856 pci_fixup_irq(pbm, bus);
857}
858
859static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
860{
861 u16 cmd;
862 u8 hdr_type, min_gnt, ltimer;
863
864 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
865 cmd |= PCI_COMMAND_MASTER;
866 pci_write_config_word(pdev, PCI_COMMAND, cmd);
867
868 /* Read it back, if the mastering bit did not
869 * get set, the device does not support bus
870 * mastering so we have nothing to do here.
871 */
872 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
873 if ((cmd & PCI_COMMAND_MASTER) == 0)
874 return;
875
876 /* Set correct cache line size, 64-byte on all
877 * Sparc64 PCI systems. Note that the value is
878 * measured in 32-bit words.
879 */
880 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
881 64 / sizeof(u32));
882
883 pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
884 hdr_type &= ~0x80;
885 if (hdr_type != PCI_HEADER_TYPE_NORMAL)
886 return;
887
888 /* If the latency timer is already programmed with a non-zero
889 * value, assume whoever set it (OBP or whoever) knows what
890 * they are doing.
891 */
892 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ltimer);
893 if (ltimer != 0)
894 return;
895
896 /* XXX Since I'm tipping off the min grant value to
897 * XXX choose a suitable latency timer value, I also
898 * XXX considered making use of the max latency value
899 * XXX as well. Unfortunately I've seen too many bogusly
900 * XXX low settings for it to the point where it lacks
901 * XXX any usefulness. In one case, an ethernet card
902 * XXX claimed a min grant of 10 and a max latency of 5.
903 * XXX Now, if I had two such cards on the same bus I
904 * XXX could not set the desired burst period (calculated
905 * XXX from min grant) without violating the max latency
906 * XXX bound. Duh...
907 * XXX
908 * XXX I blame dumb PC bios implementors for stuff like
909 * XXX this, most of them don't even try to do something
910 * XXX sensible with latency timer values and just set some
911 * XXX default value (usually 32) into every device.
912 */
913
914 pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
915
916 if (min_gnt == 0) {
917 /* If no min_gnt setting then use a default
918 * value.
919 */
920 if (is_66mhz)
921 ltimer = 16;
922 else
923 ltimer = 32;
924 } else {
925 int shift_factor;
926
927 if (is_66mhz)
928 shift_factor = 2;
929 else
930 shift_factor = 3;
931
932 /* Use a default value when the min_gnt value
933 * is erroneously high.
934 */
935 if (((unsigned int) min_gnt << shift_factor) > 512 ||
936 ((min_gnt << shift_factor) & 0xff) == 0) {
937 ltimer = 8 << shift_factor;
938 } else {
939 ltimer = min_gnt << shift_factor;
940 }
941 }
942
943 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
944}
945
946void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
947 struct pci_bus *pbus)
948{
949 struct pci_dev *pdev;
950 int all_are_66mhz;
951 u16 status;
952
953 if (pbm->is_66mhz_capable == 0) {
954 all_are_66mhz = 0;
955 goto out;
956 }
957
958 all_are_66mhz = 1;
959 list_for_each_entry(pdev, &pbus->devices, bus_list) {
960 pci_read_config_word(pdev, PCI_STATUS, &status);
961 if (!(status & PCI_STATUS_66MHZ)) {
962 all_are_66mhz = 0;
963 break;
964 }
965 }
966out:
967 pbm->all_devs_66mhz = all_are_66mhz;
968
969 printk("PCI%d(PBM%c): Bus running at %dMHz\n",
970 pbm->parent->index,
971 (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
972 (all_are_66mhz ? 66 : 33));
973}
974
975void pci_setup_busmastering(struct pci_pbm_info *pbm,
976 struct pci_bus *pbus)
977{
978 struct pci_dev *dev;
979 struct pci_bus *bus;
980 int is_66mhz;
981
982 is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
983
984 list_for_each_entry(dev, &pbus->devices, bus_list)
985 pdev_setup_busmastering(dev, is_66mhz);
986
987 list_for_each_entry(bus, &pbus->children, node)
988 pci_setup_busmastering(pbm, bus);
989}
990
991void pci_register_legacy_regions(struct resource *io_res,
992 struct resource *mem_res)
993{
994 struct resource *p;
995
996 /* VGA Video RAM. */
Eric Sesterhenn91329832006-03-06 13:48:40 -0800997 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!p)
999 return;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 p->name = "Video RAM area";
1002 p->start = mem_res->start + 0xa0000UL;
1003 p->end = p->start + 0x1ffffUL;
1004 p->flags = IORESOURCE_BUSY;
1005 request_resource(mem_res, p);
1006
Eric Sesterhenn91329832006-03-06 13:48:40 -08001007 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!p)
1009 return;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 p->name = "System ROM";
1012 p->start = mem_res->start + 0xf0000UL;
1013 p->end = p->start + 0xffffUL;
1014 p->flags = IORESOURCE_BUSY;
1015 request_resource(mem_res, p);
1016
Eric Sesterhenn91329832006-03-06 13:48:40 -08001017 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!p)
1019 return;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 p->name = "Video ROM";
1022 p->start = mem_res->start + 0xc0000UL;
1023 p->end = p->start + 0x7fffUL;
1024 p->flags = IORESOURCE_BUSY;
1025 request_resource(mem_res, p);
1026}
1027
1028/* Generic helper routines for PCI error reporting. */
1029void pci_scan_for_target_abort(struct pci_controller_info *p,
1030 struct pci_pbm_info *pbm,
1031 struct pci_bus *pbus)
1032{
1033 struct pci_dev *pdev;
1034 struct pci_bus *bus;
1035
1036 list_for_each_entry(pdev, &pbus->devices, bus_list) {
1037 u16 status, error_bits;
1038
1039 pci_read_config_word(pdev, PCI_STATUS, &status);
1040 error_bits =
1041 (status & (PCI_STATUS_SIG_TARGET_ABORT |
1042 PCI_STATUS_REC_TARGET_ABORT));
1043 if (error_bits) {
1044 pci_write_config_word(pdev, PCI_STATUS, error_bits);
1045 printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
1046 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1047 pci_name(pdev), status);
1048 }
1049 }
1050
1051 list_for_each_entry(bus, &pbus->children, node)
1052 pci_scan_for_target_abort(p, pbm, bus);
1053}
1054
1055void pci_scan_for_master_abort(struct pci_controller_info *p,
1056 struct pci_pbm_info *pbm,
1057 struct pci_bus *pbus)
1058{
1059 struct pci_dev *pdev;
1060 struct pci_bus *bus;
1061
1062 list_for_each_entry(pdev, &pbus->devices, bus_list) {
1063 u16 status, error_bits;
1064
1065 pci_read_config_word(pdev, PCI_STATUS, &status);
1066 error_bits =
1067 (status & (PCI_STATUS_REC_MASTER_ABORT));
1068 if (error_bits) {
1069 pci_write_config_word(pdev, PCI_STATUS, error_bits);
1070 printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
1071 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1072 pci_name(pdev), status);
1073 }
1074 }
1075
1076 list_for_each_entry(bus, &pbus->children, node)
1077 pci_scan_for_master_abort(p, pbm, bus);
1078}
1079
1080void pci_scan_for_parity_error(struct pci_controller_info *p,
1081 struct pci_pbm_info *pbm,
1082 struct pci_bus *pbus)
1083{
1084 struct pci_dev *pdev;
1085 struct pci_bus *bus;
1086
1087 list_for_each_entry(pdev, &pbus->devices, bus_list) {
1088 u16 status, error_bits;
1089
1090 pci_read_config_word(pdev, PCI_STATUS, &status);
1091 error_bits =
1092 (status & (PCI_STATUS_PARITY |
1093 PCI_STATUS_DETECTED_PARITY));
1094 if (error_bits) {
1095 pci_write_config_word(pdev, PCI_STATUS, error_bits);
1096 printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
1097 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
1098 pci_name(pdev), status);
1099 }
1100 }
1101
1102 list_for_each_entry(bus, &pbus->children, node)
1103 pci_scan_for_parity_error(p, pbm, bus);
1104}