prom_parse.c 24 KB

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