prom_parse.c 24 KB

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