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