[POWERPC] Constify & voidify get_property()
[linux-block.git] / arch / powerpc / kernel / prom_parse.c
CommitLineData
d1405b86
BH
1#undef DEBUG
2
3#include <linux/kernel.h>
4#include <linux/string.h>
5#include <linux/pci_regs.h>
6#include <linux/module.h>
d2dd482b 7#include <linux/ioport.h>
d1405b86 8#include <asm/prom.h>
d2dd482b 9#include <asm/pci-bridge.h>
d1405b86
BH
10
11#ifdef DEBUG
12#define DBG(fmt...) do { printk(fmt); } while(0)
13#else
14#define DBG(fmt...) do { } while(0)
15#endif
16
17#ifdef CONFIG_PPC64
18#define PRu64 "%lx"
19#else
20#define PRu64 "%llx"
21#endif
22
23/* Max address size we deal with */
24#define OF_MAX_ADDR_CELLS 4
25#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
26 (ns) > 0)
27
28/* Debug utility */
29#ifdef DEBUG
b5a1a9ab 30static void of_dump_addr(const char *s, const u32 *addr, int na)
d1405b86
BH
31{
32 printk("%s", s);
33 while(na--)
34 printk(" %08x", *(addr++));
35 printk("\n");
36}
37#else
b5a1a9ab 38static void of_dump_addr(const char *s, const u32 *addr, int na) { }
d1405b86
BH
39#endif
40
d1405b86
BH
41
42/* Callbacks for bus specific translators */
43struct of_bus {
44 const char *name;
45 const char *addresses;
46 int (*match)(struct device_node *parent);
47 void (*count_cells)(struct device_node *child,
48 int *addrc, int *sizec);
b5a1a9ab
JK
49 u64 (*map)(u32 *addr, const u32 *range,
50 int na, int ns, int pna);
d1405b86 51 int (*translate)(u32 *addr, u64 offset, int na);
b5a1a9ab 52 unsigned int (*get_flags)(const u32 *addr);
d1405b86
BH
53};
54
55
56/*
57 * Default translator (generic bus)
58 */
59
d2dd482b
BH
60static void of_bus_default_count_cells(struct device_node *dev,
61 int *addrc, int *sizec)
d1405b86
BH
62{
63 if (addrc)
64 *addrc = prom_n_addr_cells(dev);
65 if (sizec)
66 *sizec = prom_n_size_cells(dev);
67}
68
b5a1a9ab
JK
69static u64 of_bus_default_map(u32 *addr, const u32 *range,
70 int na, int ns, int pna)
d1405b86
BH
71{
72 u64 cp, s, da;
73
cc9fd71c
BH
74 cp = of_read_number(range, na);
75 s = of_read_number(range + na + pna, ns);
76 da = of_read_number(addr, na);
d1405b86
BH
77
78 DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
79 cp, s, da);
80
81 if (da < cp || da >= (cp + s))
82 return OF_BAD_ADDR;
83 return da - cp;
84}
85
d2dd482b 86static int of_bus_default_translate(u32 *addr, u64 offset, int na)
d1405b86 87{
cc9fd71c 88 u64 a = of_read_number(addr, na);
d1405b86
BH
89 memset(addr, 0, na * 4);
90 a += offset;
91 if (na > 1)
92 addr[na - 2] = a >> 32;
93 addr[na - 1] = a & 0xffffffffu;
94
95 return 0;
96}
97
b5a1a9ab 98static unsigned int of_bus_default_get_flags(const u32 *addr)
d2dd482b
BH
99{
100 return IORESOURCE_MEM;
101}
102
d1405b86
BH
103
104/*
105 * PCI bus specific translator
106 */
107
108static int of_bus_pci_match(struct device_node *np)
109{
d5f07900
PM
110 /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
111 return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
d1405b86
BH
112}
113
114static void of_bus_pci_count_cells(struct device_node *np,
115 int *addrc, int *sizec)
116{
117 if (addrc)
118 *addrc = 3;
119 if (sizec)
120 *sizec = 2;
121}
122
b5a1a9ab 123static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
d1405b86
BH
124{
125 u64 cp, s, da;
126
127 /* Check address type match */
128 if ((addr[0] ^ range[0]) & 0x03000000)
129 return OF_BAD_ADDR;
130
131 /* Read address values, skipping high cell */
cc9fd71c
BH
132 cp = of_read_number(range + 1, na - 1);
133 s = of_read_number(range + na + pna, ns);
134 da = of_read_number(addr + 1, na - 1);
d1405b86
BH
135
136 DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
137
138 if (da < cp || da >= (cp + s))
139 return OF_BAD_ADDR;
140 return da - cp;
141}
142
143static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
144{
d2dd482b
BH
145 return of_bus_default_translate(addr + 1, offset, na - 1);
146}
147
b5a1a9ab 148static unsigned int of_bus_pci_get_flags(const u32 *addr)
d2dd482b
BH
149{
150 unsigned int flags = 0;
151 u32 w = addr[0];
152
153 switch((w >> 24) & 0x03) {
154 case 0x01:
155 flags |= IORESOURCE_IO;
156 case 0x02: /* 32 bits */
157 case 0x03: /* 64 bits */
158 flags |= IORESOURCE_MEM;
159 }
160 if (w & 0x40000000)
161 flags |= IORESOURCE_PREFETCH;
162 return flags;
d1405b86
BH
163}
164
165/*
166 * ISA bus specific translator
167 */
168
169static int of_bus_isa_match(struct device_node *np)
170{
171 return !strcmp(np->name, "isa");
172}
173
174static void of_bus_isa_count_cells(struct device_node *child,
175 int *addrc, int *sizec)
176{
177 if (addrc)
178 *addrc = 2;
179 if (sizec)
180 *sizec = 1;
181}
182
b5a1a9ab 183static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
d1405b86
BH
184{
185 u64 cp, s, da;
186
187 /* Check address type match */
188 if ((addr[0] ^ range[0]) & 0x00000001)
189 return OF_BAD_ADDR;
190
191 /* Read address values, skipping high cell */
cc9fd71c
BH
192 cp = of_read_number(range + 1, na - 1);
193 s = of_read_number(range + na + pna, ns);
194 da = of_read_number(addr + 1, na - 1);
d1405b86
BH
195
196 DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
197
198 if (da < cp || da >= (cp + s))
199 return OF_BAD_ADDR;
200 return da - cp;
201}
202
203static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
204{
d2dd482b
BH
205 return of_bus_default_translate(addr + 1, offset, na - 1);
206}
207
b5a1a9ab 208static unsigned int of_bus_isa_get_flags(const u32 *addr)
d2dd482b
BH
209{
210 unsigned int flags = 0;
211 u32 w = addr[0];
212
213 if (w & 1)
214 flags |= IORESOURCE_IO;
215 else
216 flags |= IORESOURCE_MEM;
217 return flags;
d1405b86
BH
218}
219
d2dd482b 220
d1405b86
BH
221/*
222 * Array of bus specific translators
223 */
224
225static struct of_bus of_busses[] = {
226 /* PCI */
227 {
228 .name = "pci",
229 .addresses = "assigned-addresses",
230 .match = of_bus_pci_match,
231 .count_cells = of_bus_pci_count_cells,
232 .map = of_bus_pci_map,
233 .translate = of_bus_pci_translate,
d2dd482b 234 .get_flags = of_bus_pci_get_flags,
d1405b86
BH
235 },
236 /* ISA */
237 {
238 .name = "isa",
239 .addresses = "reg",
240 .match = of_bus_isa_match,
241 .count_cells = of_bus_isa_count_cells,
242 .map = of_bus_isa_map,
243 .translate = of_bus_isa_translate,
d2dd482b 244 .get_flags = of_bus_isa_get_flags,
d1405b86
BH
245 },
246 /* Default */
247 {
248 .name = "default",
249 .addresses = "reg",
250 .match = NULL,
d2dd482b
BH
251 .count_cells = of_bus_default_count_cells,
252 .map = of_bus_default_map,
253 .translate = of_bus_default_translate,
254 .get_flags = of_bus_default_get_flags,
d1405b86
BH
255 },
256};
257
258static struct of_bus *of_match_bus(struct device_node *np)
259{
260 int i;
261
262 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
263 if (!of_busses[i].match || of_busses[i].match(np))
264 return &of_busses[i];
265 BUG();
266 return NULL;
267}
268
269static int of_translate_one(struct device_node *parent, struct of_bus *bus,
270 struct of_bus *pbus, u32 *addr,
271 int na, int ns, int pna)
272{
a7f67bdf 273 const u32 *ranges;
d1405b86
BH
274 unsigned int rlen;
275 int rone;
276 u64 offset = OF_BAD_ADDR;
277
278 /* Normally, an absence of a "ranges" property means we are
279 * crossing a non-translatable boundary, and thus the addresses
280 * below the current not cannot be converted to CPU physical ones.
281 * Unfortunately, while this is very clear in the spec, it's not
282 * what Apple understood, and they do have things like /uni-n or
283 * /ht nodes with no "ranges" property and a lot of perfectly
284 * useable mapped devices below them. Thus we treat the absence of
285 * "ranges" as equivalent to an empty "ranges" property which means
286 * a 1:1 translation at that level. It's up to the caller not to try
287 * to translate addresses that aren't supposed to be translated in
288 * the first place. --BenH.
289 */
a7f67bdf 290 ranges = get_property(parent, "ranges", &rlen);
d1405b86 291 if (ranges == NULL || rlen == 0) {
cc9fd71c 292 offset = of_read_number(addr, na);
d2dd482b
BH
293 memset(addr, 0, pna * 4);
294 DBG("OF: no ranges, 1:1 translation\n");
d1405b86
BH
295 goto finish;
296 }
297
298 DBG("OF: walking ranges...\n");
299
300 /* Now walk through the ranges */
301 rlen /= 4;
302 rone = na + pna + ns;
303 for (; rlen >= rone; rlen -= rone, ranges += rone) {
304 offset = bus->map(addr, ranges, na, ns, pna);
305 if (offset != OF_BAD_ADDR)
306 break;
307 }
308 if (offset == OF_BAD_ADDR) {
309 DBG("OF: not found !\n");
310 return 1;
311 }
312 memcpy(addr, ranges + na, 4 * pna);
313
314 finish:
315 of_dump_addr("OF: parent translation for:", addr, pna);
bb6b9b28 316 DBG("OF: with offset: "PRu64"\n", offset);
d1405b86
BH
317
318 /* Translate it into parent bus space */
319 return pbus->translate(addr, offset, pna);
320}
321
322
323/*
324 * Translate an address from the device-tree into a CPU physical address,
325 * this walks up the tree and applies the various bus mappings on the
326 * way.
327 *
328 * Note: We consider that crossing any level with #size-cells == 0 to mean
329 * that translation is impossible (that is we are not dealing with a value
330 * that can be mapped to a cpu physical address). This is not really specified
331 * that way, but this is traditionally the way IBM at least do things
332 */
a7f67bdf 333u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
d1405b86
BH
334{
335 struct device_node *parent = NULL;
336 struct of_bus *bus, *pbus;
337 u32 addr[OF_MAX_ADDR_CELLS];
338 int na, ns, pna, pns;
339 u64 result = OF_BAD_ADDR;
340
341 DBG("OF: ** translation for device %s **\n", dev->full_name);
342
343 /* Increase refcount at current level */
344 of_node_get(dev);
345
346 /* Get parent & match bus type */
347 parent = of_get_parent(dev);
348 if (parent == NULL)
349 goto bail;
350 bus = of_match_bus(parent);
351
352 /* Cound address cells & copy address locally */
353 bus->count_cells(dev, &na, &ns);
354 if (!OF_CHECK_COUNTS(na, ns)) {
355 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
356 dev->full_name);
357 goto bail;
358 }
359 memcpy(addr, in_addr, na * 4);
360
361 DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
362 bus->name, na, ns, parent->full_name);
363 of_dump_addr("OF: translating address:", addr, na);
364
365 /* Translate */
366 for (;;) {
367 /* Switch to parent bus */
368 of_node_put(dev);
369 dev = parent;
370 parent = of_get_parent(dev);
371
372 /* If root, we have finished */
373 if (parent == NULL) {
374 DBG("OF: reached root node\n");
cc9fd71c 375 result = of_read_number(addr, na);
d1405b86
BH
376 break;
377 }
378
379 /* Get new parent bus and counts */
380 pbus = of_match_bus(parent);
381 pbus->count_cells(dev, &pna, &pns);
382 if (!OF_CHECK_COUNTS(pna, pns)) {
383 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
384 dev->full_name);
385 break;
386 }
387
388 DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
389 pbus->name, pna, pns, parent->full_name);
390
391 /* Apply bus translation */
392 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
393 break;
394
395 /* Complete the move up one level */
396 na = pna;
397 ns = pns;
398 bus = pbus;
399
400 of_dump_addr("OF: one level translation:", addr, na);
401 }
402 bail:
403 of_node_put(parent);
404 of_node_put(dev);
405
406 return result;
407}
408EXPORT_SYMBOL(of_translate_address);
409
a7f67bdf 410const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
d2dd482b 411 unsigned int *flags)
d1405b86 412{
a7f67bdf 413 const u32 *prop;
d1405b86
BH
414 unsigned int psize;
415 struct device_node *parent;
416 struct of_bus *bus;
417 int onesize, i, na, ns;
418
419 /* Get parent & match bus type */
420 parent = of_get_parent(dev);
421 if (parent == NULL)
422 return NULL;
423 bus = of_match_bus(parent);
424 bus->count_cells(dev, &na, &ns);
425 of_node_put(parent);
426 if (!OF_CHECK_COUNTS(na, ns))
427 return NULL;
428
429 /* Get "reg" or "assigned-addresses" property */
a7f67bdf 430 prop = get_property(dev, bus->addresses, &psize);
d1405b86
BH
431 if (prop == NULL)
432 return NULL;
433 psize /= 4;
434
435 onesize = na + ns;
436 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
437 if (i == index) {
438 if (size)
cc9fd71c 439 *size = of_read_number(prop + na, ns);
d2dd482b
BH
440 if (flags)
441 *flags = bus->get_flags(prop);
d1405b86
BH
442 return prop;
443 }
444 return NULL;
445}
446EXPORT_SYMBOL(of_get_address);
447
a7f67bdf 448const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
d2dd482b 449 unsigned int *flags)
d1405b86 450{
a7f67bdf 451 const u32 *prop;
d2dd482b
BH
452 unsigned int psize;
453 struct device_node *parent;
454 struct of_bus *bus;
455 int onesize, i, na, ns;
d1405b86 456
d2dd482b
BH
457 /* Get parent & match bus type */
458 parent = of_get_parent(dev);
459 if (parent == NULL)
460 return NULL;
461 bus = of_match_bus(parent);
d60dcd94
OH
462 if (strcmp(bus->name, "pci")) {
463 of_node_put(parent);
d2dd482b 464 return NULL;
d60dcd94 465 }
d2dd482b
BH
466 bus->count_cells(dev, &na, &ns);
467 of_node_put(parent);
468 if (!OF_CHECK_COUNTS(na, ns))
469 return NULL;
470
471 /* Get "reg" or "assigned-addresses" property */
a7f67bdf 472 prop = get_property(dev, bus->addresses, &psize);
d2dd482b
BH
473 if (prop == NULL)
474 return NULL;
475 psize /= 4;
476
477 onesize = na + ns;
478 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
479 if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
480 if (size)
cc9fd71c 481 *size = of_read_number(prop + na, ns);
d2dd482b
BH
482 if (flags)
483 *flags = bus->get_flags(prop);
484 return prop;
485 }
d1405b86
BH
486 return NULL;
487}
488EXPORT_SYMBOL(of_get_pci_address);
d2dd482b 489
a7f67bdf 490static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
d2dd482b
BH
491 u64 size, unsigned int flags,
492 struct resource *r)
493{
494 u64 taddr;
495
496 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
497 return -EINVAL;
498 taddr = of_translate_address(dev, addrp);
499 if (taddr == OF_BAD_ADDR)
500 return -EINVAL;
501 memset(r, 0, sizeof(struct resource));
502 if (flags & IORESOURCE_IO) {
f2c4583a 503 unsigned long port;
d2dd482b 504 port = pci_address_to_pio(taddr);
f2c4583a 505 if (port == (unsigned long)-1)
d2dd482b
BH
506 return -EINVAL;
507 r->start = port;
508 r->end = port + size - 1;
509 } else {
510 r->start = taddr;
511 r->end = taddr + size - 1;
512 }
513 r->flags = flags;
514 r->name = dev->name;
515 return 0;
516}
517
518int of_address_to_resource(struct device_node *dev, int index,
519 struct resource *r)
520{
a7f67bdf 521 const u32 *addrp;
d2dd482b
BH
522 u64 size;
523 unsigned int flags;
524
525 addrp = of_get_address(dev, index, &size, &flags);
526 if (addrp == NULL)
527 return -EINVAL;
528 return __of_address_to_resource(dev, addrp, size, flags, r);
529}
530EXPORT_SYMBOL_GPL(of_address_to_resource);
531
532int of_pci_address_to_resource(struct device_node *dev, int bar,
533 struct resource *r)
534{
a7f67bdf 535 const u32 *addrp;
d2dd482b
BH
536 u64 size;
537 unsigned int flags;
538
539 addrp = of_get_pci_address(dev, bar, &size, &flags);
540 if (addrp == NULL)
541 return -EINVAL;
542 return __of_address_to_resource(dev, addrp, size, flags, r);
543}
544EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
d4ad66fa 545
a7f67bdf 546void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
d4ad66fa
JK
547 unsigned long *busno, unsigned long *phys, unsigned long *size)
548{
a7f67bdf
JK
549 const u32 *dma_window;
550 u32 cells;
551 const unsigned char *prop;
d4ad66fa 552
a7f67bdf 553 dma_window = dma_window_prop;
d4ad66fa
JK
554
555 /* busno is always one cell */
556 *busno = *(dma_window++);
557
558 prop = get_property(dn, "ibm,#dma-address-cells", NULL);
03ac829b
WS
559 if (!prop)
560 prop = get_property(dn, "#address-cells", NULL);
561
d4ad66fa 562 cells = prop ? *(u32 *)prop : prom_n_addr_cells(dn);
cc9fd71c 563 *phys = of_read_number(dma_window, cells);
d4ad66fa
JK
564
565 dma_window += cells;
566
567 prop = get_property(dn, "ibm,#dma-size-cells", NULL);
568 cells = prop ? *(u32 *)prop : prom_n_size_cells(dn);
cc9fd71c
BH
569 *size = of_read_number(dma_window, cells);
570}
571
572/*
573 * Interrupt remapper
574 */
575
576static unsigned int of_irq_workarounds;
577static struct device_node *of_irq_dflt_pic;
578
579static struct device_node *of_irq_find_parent(struct device_node *child)
580{
581 struct device_node *p;
a7f67bdf 582 const phandle *parp;
cc9fd71c
BH
583
584 if (!of_node_get(child))
585 return NULL;
586
587 do {
a7f67bdf 588 parp = get_property(child, "interrupt-parent", NULL);
cc9fd71c
BH
589 if (parp == NULL)
590 p = of_get_parent(child);
591 else {
592 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
593 p = of_node_get(of_irq_dflt_pic);
594 else
595 p = of_find_node_by_phandle(*parp);
596 }
597 of_node_put(child);
598 child = p;
599 } while (p && get_property(p, "#interrupt-cells", NULL) == NULL);
600
601 return p;
602}
603
604static u8 of_irq_pci_swizzle(u8 slot, u8 pin)
605{
606 return (((pin - 1) + slot) % 4) + 1;
d4ad66fa 607}
cc9fd71c
BH
608
609/* This doesn't need to be called if you don't have any special workaround
610 * flags to pass
611 */
612void of_irq_map_init(unsigned int flags)
613{
614 of_irq_workarounds = flags;
615
616 /* OldWorld, don't bother looking at other things */
617 if (flags & OF_IMAP_OLDWORLD_MAC)
618 return;
619
620 /* If we don't have phandles, let's try to locate a default interrupt
621 * controller (happens when booting with BootX). We do a first match
622 * here, hopefully, that only ever happens on machines with one
623 * controller.
624 */
625 if (flags & OF_IMAP_NO_PHANDLE) {
626 struct device_node *np;
627
628 for(np = NULL; (np = of_find_all_nodes(np)) != NULL;) {
629 if (get_property(np, "interrupt-controller", NULL)
630 == NULL)
631 continue;
632 /* Skip /chosen/interrupt-controller */
633 if (strcmp(np->name, "chosen") == 0)
634 continue;
635 /* It seems like at least one person on this planet wants
636 * to use BootX on a machine with an AppleKiwi controller
637 * which happens to pretend to be an interrupt
638 * controller too.
639 */
640 if (strcmp(np->name, "AppleKiwi") == 0)
641 continue;
642 /* I think we found one ! */
643 of_irq_dflt_pic = np;
644 break;
645 }
646 }
647
648}
649
a7f67bdf
JK
650int of_irq_map_raw(struct device_node *parent, const u32 *intspec,
651 const u32 *addr, struct of_irq *out_irq)
cc9fd71c
BH
652{
653 struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
a7f67bdf 654 const u32 *tmp, *imap, *imask;
cc9fd71c
BH
655 u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
656 int imaplen, match, i;
657
658 ipar = of_node_get(parent);
659
660 /* First get the #interrupt-cells property of the current cursor
661 * that tells us how to interpret the passed-in intspec. If there
662 * is none, we are nice and just walk up the tree
663 */
664 do {
a7f67bdf 665 tmp = get_property(ipar, "#interrupt-cells", NULL);
cc9fd71c
BH
666 if (tmp != NULL) {
667 intsize = *tmp;
668 break;
669 }
670 tnode = ipar;
671 ipar = of_irq_find_parent(ipar);
672 of_node_put(tnode);
673 } while (ipar);
674 if (ipar == NULL) {
675 DBG(" -> no parent found !\n");
676 goto fail;
677 }
678
679 DBG("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
680
681 /* Look for this #address-cells. We have to implement the old linux
682 * trick of looking for the parent here as some device-trees rely on it
683 */
684 old = of_node_get(ipar);
685 do {
a7f67bdf 686 tmp = get_property(old, "#address-cells", NULL);
cc9fd71c
BH
687 tnode = of_get_parent(old);
688 of_node_put(old);
689 old = tnode;
690 } while(old && tmp == NULL);
691 of_node_put(old);
692 old = NULL;
693 addrsize = (tmp == NULL) ? 2 : *tmp;
694
695 DBG(" -> addrsize=%d\n", addrsize);
696
697 /* Now start the actual "proper" walk of the interrupt tree */
698 while (ipar != NULL) {
699 /* Now check if cursor is an interrupt-controller and if it is
700 * then we are done
701 */
702 if (get_property(ipar, "interrupt-controller", NULL) != NULL) {
703 DBG(" -> got it !\n");
704 memcpy(out_irq->specifier, intspec,
705 intsize * sizeof(u32));
706 out_irq->size = intsize;
707 out_irq->controller = ipar;
708 of_node_put(old);
709 return 0;
710 }
711
712 /* Now look for an interrupt-map */
a7f67bdf 713 imap = get_property(ipar, "interrupt-map", &imaplen);
cc9fd71c
BH
714 /* No interrupt map, check for an interrupt parent */
715 if (imap == NULL) {
716 DBG(" -> no map, getting parent\n");
717 newpar = of_irq_find_parent(ipar);
718 goto skiplevel;
719 }
720 imaplen /= sizeof(u32);
721
722 /* Look for a mask */
a7f67bdf 723 imask = get_property(ipar, "interrupt-map-mask", NULL);
cc9fd71c
BH
724
725 /* If we were passed no "reg" property and we attempt to parse
726 * an interrupt-map, then #address-cells must be 0.
727 * Fail if it's not.
728 */
729 if (addr == NULL && addrsize != 0) {
730 DBG(" -> no reg passed in when needed !\n");
731 goto fail;
732 }
733
734 /* Parse interrupt-map */
735 match = 0;
736 while (imaplen > (addrsize + intsize + 1) && !match) {
737 /* Compare specifiers */
738 match = 1;
739 for (i = 0; i < addrsize && match; ++i) {
740 u32 mask = imask ? imask[i] : 0xffffffffu;
741 match = ((addr[i] ^ imap[i]) & mask) == 0;
742 }
743 for (; i < (addrsize + intsize) && match; ++i) {
744 u32 mask = imask ? imask[i] : 0xffffffffu;
745 match =
746 ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
747 }
748 imap += addrsize + intsize;
749 imaplen -= addrsize + intsize;
750
751 DBG(" -> match=%d (imaplen=%d)\n", match, imaplen);
752
753 /* Get the interrupt parent */
754 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
755 newpar = of_node_get(of_irq_dflt_pic);
756 else
757 newpar = of_find_node_by_phandle((phandle)*imap);
758 imap++;
759 --imaplen;
760
761 /* Check if not found */
762 if (newpar == NULL) {
763 DBG(" -> imap parent not found !\n");
764 goto fail;
765 }
766
767 /* Get #interrupt-cells and #address-cells of new
768 * parent
769 */
a7f67bdf 770 tmp = get_property(newpar, "#interrupt-cells",
cc9fd71c
BH
771 NULL);
772 if (tmp == NULL) {
773 DBG(" -> parent lacks #interrupt-cells !\n");
774 goto fail;
775 }
776 newintsize = *tmp;
a7f67bdf 777 tmp = get_property(newpar, "#address-cells",
cc9fd71c
BH
778 NULL);
779 newaddrsize = (tmp == NULL) ? 0 : *tmp;
780
781 DBG(" -> newintsize=%d, newaddrsize=%d\n",
782 newintsize, newaddrsize);
783
784 /* Check for malformed properties */
785 if (imaplen < (newaddrsize + newintsize))
786 goto fail;
787
788 imap += newaddrsize + newintsize;
789 imaplen -= newaddrsize + newintsize;
790
791 DBG(" -> imaplen=%d\n", imaplen);
792 }
793 if (!match)
794 goto fail;
795
796 of_node_put(old);
797 old = of_node_get(newpar);
798 addrsize = newaddrsize;
799 intsize = newintsize;
800 intspec = imap - intsize;
801 addr = intspec - addrsize;
802
803 skiplevel:
804 /* Iterate again with new parent */
805 DBG(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
806 of_node_put(ipar);
807 ipar = newpar;
808 newpar = NULL;
809 }
810 fail:
811 of_node_put(ipar);
812 of_node_put(old);
813 of_node_put(newpar);
814
815 return -EINVAL;
816}
817EXPORT_SYMBOL_GPL(of_irq_map_raw);
818
819#if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
820static int of_irq_map_oldworld(struct device_node *device, int index,
821 struct of_irq *out_irq)
822{
a7f67bdf 823 const u32 *ints;
cc9fd71c
BH
824 int intlen;
825
826 /*
827 * Old machines just have a list of interrupt numbers
828 * and no interrupt-controller nodes.
829 */
a7f67bdf 830 ints = get_property(device, "AAPL,interrupts", &intlen);
cc9fd71c
BH
831 if (ints == NULL)
832 return -EINVAL;
833 intlen /= sizeof(u32);
834
835 if (index >= intlen)
836 return -EINVAL;
837
838 out_irq->controller = NULL;
839 out_irq->specifier[0] = ints[index];
840 out_irq->size = 1;
841
842 return 0;
843}
844#else /* defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) */
845static int of_irq_map_oldworld(struct device_node *device, int index,
846 struct of_irq *out_irq)
847{
848 return -EINVAL;
849}
850#endif /* !(defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)) */
851
852int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
853{
854 struct device_node *p;
a7f67bdf
JK
855 const u32 *intspec, *tmp, *addr;
856 u32 intsize, intlen;
cc9fd71c
BH
857 int res;
858
859 DBG("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
860
861 /* OldWorld mac stuff is "special", handle out of line */
862 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
863 return of_irq_map_oldworld(device, index, out_irq);
864
865 /* Get the interrupts property */
a7f67bdf 866 intspec = get_property(device, "interrupts", &intlen);
cc9fd71c
BH
867 if (intspec == NULL)
868 return -EINVAL;
869 intlen /= sizeof(u32);
870
871 /* Get the reg property (if any) */
a7f67bdf 872 addr = get_property(device, "reg", NULL);
cc9fd71c
BH
873
874 /* Look for the interrupt parent. */
875 p = of_irq_find_parent(device);
876 if (p == NULL)
877 return -EINVAL;
878
879 /* Get size of interrupt specifier */
a7f67bdf 880 tmp = get_property(p, "#interrupt-cells", NULL);
cc9fd71c
BH
881 if (tmp == NULL) {
882 of_node_put(p);
883 return -EINVAL;
884 }
885 intsize = *tmp;
886
887 /* Check index */
888 if (index * intsize >= intlen)
889 return -EINVAL;
890
891 /* Get new specifier and map it */
892 res = of_irq_map_raw(p, intspec + index * intsize, addr, out_irq);
893 of_node_put(p);
894 return res;
895}
896EXPORT_SYMBOL_GPL(of_irq_map_one);
897
898int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
899{
900 struct device_node *dn, *ppnode;
901 struct pci_dev *ppdev;
902 u32 lspec;
903 u32 laddr[3];
904 u8 pin;
905 int rc;
906
907 /* Check if we have a device node, if yes, fallback to standard OF
908 * parsing
909 */
910 dn = pci_device_to_OF_node(pdev);
911 if (dn)
912 return of_irq_map_one(dn, 0, out_irq);
913
914 /* Ok, we don't, time to have fun. Let's start by building up an
915 * interrupt spec. we assume #interrupt-cells is 1, which is standard
916 * for PCI. If you do different, then don't use that routine.
917 */
918 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
919 if (rc != 0)
920 return rc;
921 /* No pin, exit */
922 if (pin == 0)
923 return -ENODEV;
924
925 /* Now we walk up the PCI tree */
926 lspec = pin;
927 for (;;) {
928 /* Get the pci_dev of our parent */
929 ppdev = pdev->bus->self;
930
931 /* Ouch, it's a host bridge... */
932 if (ppdev == NULL) {
933#ifdef CONFIG_PPC64
934 ppnode = pci_bus_to_OF_node(pdev->bus);
935#else
936 struct pci_controller *host;
937 host = pci_bus_to_host(pdev->bus);
938 ppnode = host ? host->arch_data : NULL;
939#endif
940 /* No node for host bridge ? give up */
941 if (ppnode == NULL)
942 return -EINVAL;
943 } else
944 /* We found a P2P bridge, check if it has a node */
945 ppnode = pci_device_to_OF_node(ppdev);
946
947 /* Ok, we have found a parent with a device-node, hand over to
948 * the OF parsing code.
949 * We build a unit address from the linux device to be used for
950 * resolution. Note that we use the linux bus number which may
951 * not match your firmware bus numbering.
952 * Fortunately, in most cases, interrupt-map-mask doesn't include
953 * the bus number as part of the matching.
954 * You should still be careful about that though if you intend
955 * to rely on this function (you ship a firmware that doesn't
956 * create device nodes for all PCI devices).
957 */
958 if (ppnode)
959 break;
960
961 /* We can only get here if we hit a P2P bridge with no node,
962 * let's do standard swizzling and try again
963 */
964 lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec);
965 pdev = ppdev;
966 }
967
968 laddr[0] = (pdev->bus->number << 16)
969 | (pdev->devfn << 8);
970 laddr[1] = laddr[2] = 0;
971 return of_irq_map_raw(ppnode, &lspec, laddr, out_irq);
972}
973EXPORT_SYMBOL_GPL(of_irq_map_pci);
974