prom_parse.c 24 KB

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