prom_parse.c 24 KB

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