prom_parse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. /* Read a big address */
  36. static inline u64 of_read_addr(u32 *cell, int size)
  37. {
  38. u64 r = 0;
  39. while (size--)
  40. r = (r << 32) | *(cell++);
  41. return r;
  42. }
  43. /* Callbacks for bus specific translators */
  44. struct of_bus {
  45. const char *name;
  46. const char *addresses;
  47. int (*match)(struct device_node *parent);
  48. void (*count_cells)(struct device_node *child,
  49. int *addrc, int *sizec);
  50. u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
  51. int (*translate)(u32 *addr, u64 offset, int na);
  52. unsigned int (*get_flags)(u32 *addr);
  53. };
  54. /*
  55. * Default translator (generic bus)
  56. */
  57. static void of_bus_default_count_cells(struct device_node *dev,
  58. int *addrc, int *sizec)
  59. {
  60. if (addrc)
  61. *addrc = prom_n_addr_cells(dev);
  62. if (sizec)
  63. *sizec = prom_n_size_cells(dev);
  64. }
  65. static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
  66. {
  67. u64 cp, s, da;
  68. cp = of_read_addr(range, na);
  69. s = of_read_addr(range + na + pna, ns);
  70. da = of_read_addr(addr, na);
  71. DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
  72. cp, s, da);
  73. if (da < cp || da >= (cp + s))
  74. return OF_BAD_ADDR;
  75. return da - cp;
  76. }
  77. static int of_bus_default_translate(u32 *addr, u64 offset, int na)
  78. {
  79. u64 a = of_read_addr(addr, na);
  80. memset(addr, 0, na * 4);
  81. a += offset;
  82. if (na > 1)
  83. addr[na - 2] = a >> 32;
  84. addr[na - 1] = a & 0xffffffffu;
  85. return 0;
  86. }
  87. static unsigned int of_bus_default_get_flags(u32 *addr)
  88. {
  89. return IORESOURCE_MEM;
  90. }
  91. /*
  92. * PCI bus specific translator
  93. */
  94. static int of_bus_pci_match(struct device_node *np)
  95. {
  96. return !strcmp(np->type, "pci");
  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, 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_addr(range + 1, na - 1);
  114. s = of_read_addr(range + na + pna, ns);
  115. da = of_read_addr(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(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. /*
  141. * ISA bus specific translator
  142. */
  143. static int of_bus_isa_match(struct device_node *np)
  144. {
  145. return !strcmp(np->name, "isa");
  146. }
  147. static void of_bus_isa_count_cells(struct device_node *child,
  148. int *addrc, int *sizec)
  149. {
  150. if (addrc)
  151. *addrc = 2;
  152. if (sizec)
  153. *sizec = 1;
  154. }
  155. static u64 of_bus_isa_map(u32 *addr, u32 *range, int na, int ns, int pna)
  156. {
  157. u64 cp, s, da;
  158. /* Check address type match */
  159. if ((addr[0] ^ range[0]) & 0x00000001)
  160. return OF_BAD_ADDR;
  161. /* Read address values, skipping high cell */
  162. cp = of_read_addr(range + 1, na - 1);
  163. s = of_read_addr(range + na + pna, ns);
  164. da = of_read_addr(addr + 1, na - 1);
  165. DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  166. if (da < cp || da >= (cp + s))
  167. return OF_BAD_ADDR;
  168. return da - cp;
  169. }
  170. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  171. {
  172. return of_bus_default_translate(addr + 1, offset, na - 1);
  173. }
  174. static unsigned int of_bus_isa_get_flags(u32 *addr)
  175. {
  176. unsigned int flags = 0;
  177. u32 w = addr[0];
  178. if (w & 1)
  179. flags |= IORESOURCE_IO;
  180. else
  181. flags |= IORESOURCE_MEM;
  182. return flags;
  183. }
  184. /*
  185. * Array of bus specific translators
  186. */
  187. static struct of_bus of_busses[] = {
  188. /* PCI */
  189. {
  190. .name = "pci",
  191. .addresses = "assigned-addresses",
  192. .match = of_bus_pci_match,
  193. .count_cells = of_bus_pci_count_cells,
  194. .map = of_bus_pci_map,
  195. .translate = of_bus_pci_translate,
  196. .get_flags = of_bus_pci_get_flags,
  197. },
  198. /* ISA */
  199. {
  200. .name = "isa",
  201. .addresses = "reg",
  202. .match = of_bus_isa_match,
  203. .count_cells = of_bus_isa_count_cells,
  204. .map = of_bus_isa_map,
  205. .translate = of_bus_isa_translate,
  206. .get_flags = of_bus_isa_get_flags,
  207. },
  208. /* Default */
  209. {
  210. .name = "default",
  211. .addresses = "reg",
  212. .match = NULL,
  213. .count_cells = of_bus_default_count_cells,
  214. .map = of_bus_default_map,
  215. .translate = of_bus_default_translate,
  216. .get_flags = of_bus_default_get_flags,
  217. },
  218. };
  219. static struct of_bus *of_match_bus(struct device_node *np)
  220. {
  221. int i;
  222. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  223. if (!of_busses[i].match || of_busses[i].match(np))
  224. return &of_busses[i];
  225. BUG();
  226. return NULL;
  227. }
  228. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  229. struct of_bus *pbus, u32 *addr,
  230. int na, int ns, int pna)
  231. {
  232. u32 *ranges;
  233. unsigned int rlen;
  234. int rone;
  235. u64 offset = OF_BAD_ADDR;
  236. /* Normally, an absence of a "ranges" property means we are
  237. * crossing a non-translatable boundary, and thus the addresses
  238. * below the current not cannot be converted to CPU physical ones.
  239. * Unfortunately, while this is very clear in the spec, it's not
  240. * what Apple understood, and they do have things like /uni-n or
  241. * /ht nodes with no "ranges" property and a lot of perfectly
  242. * useable mapped devices below them. Thus we treat the absence of
  243. * "ranges" as equivalent to an empty "ranges" property which means
  244. * a 1:1 translation at that level. It's up to the caller not to try
  245. * to translate addresses that aren't supposed to be translated in
  246. * the first place. --BenH.
  247. */
  248. ranges = (u32 *)get_property(parent, "ranges", &rlen);
  249. if (ranges == NULL || rlen == 0) {
  250. offset = of_read_addr(addr, na);
  251. memset(addr, 0, pna * 4);
  252. DBG("OF: no ranges, 1:1 translation\n");
  253. goto finish;
  254. }
  255. DBG("OF: walking ranges...\n");
  256. /* Now walk through the ranges */
  257. rlen /= 4;
  258. rone = na + pna + ns;
  259. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  260. offset = bus->map(addr, ranges, na, ns, pna);
  261. if (offset != OF_BAD_ADDR)
  262. break;
  263. }
  264. if (offset == OF_BAD_ADDR) {
  265. DBG("OF: not found !\n");
  266. return 1;
  267. }
  268. memcpy(addr, ranges + na, 4 * pna);
  269. finish:
  270. of_dump_addr("OF: parent translation for:", addr, pna);
  271. DBG("OF: with offset: "PRu64"\n", offset);
  272. /* Translate it into parent bus space */
  273. return pbus->translate(addr, offset, pna);
  274. }
  275. /*
  276. * Translate an address from the device-tree into a CPU physical address,
  277. * this walks up the tree and applies the various bus mappings on the
  278. * way.
  279. *
  280. * Note: We consider that crossing any level with #size-cells == 0 to mean
  281. * that translation is impossible (that is we are not dealing with a value
  282. * that can be mapped to a cpu physical address). This is not really specified
  283. * that way, but this is traditionally the way IBM at least do things
  284. */
  285. u64 of_translate_address(struct device_node *dev, u32 *in_addr)
  286. {
  287. struct device_node *parent = NULL;
  288. struct of_bus *bus, *pbus;
  289. u32 addr[OF_MAX_ADDR_CELLS];
  290. int na, ns, pna, pns;
  291. u64 result = OF_BAD_ADDR;
  292. DBG("OF: ** translation for device %s **\n", dev->full_name);
  293. /* Increase refcount at current level */
  294. of_node_get(dev);
  295. /* Get parent & match bus type */
  296. parent = of_get_parent(dev);
  297. if (parent == NULL)
  298. goto bail;
  299. bus = of_match_bus(parent);
  300. /* Cound address cells & copy address locally */
  301. bus->count_cells(dev, &na, &ns);
  302. if (!OF_CHECK_COUNTS(na, ns)) {
  303. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  304. dev->full_name);
  305. goto bail;
  306. }
  307. memcpy(addr, in_addr, na * 4);
  308. DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
  309. bus->name, na, ns, parent->full_name);
  310. of_dump_addr("OF: translating address:", addr, na);
  311. /* Translate */
  312. for (;;) {
  313. /* Switch to parent bus */
  314. of_node_put(dev);
  315. dev = parent;
  316. parent = of_get_parent(dev);
  317. /* If root, we have finished */
  318. if (parent == NULL) {
  319. DBG("OF: reached root node\n");
  320. result = of_read_addr(addr, na);
  321. break;
  322. }
  323. /* Get new parent bus and counts */
  324. pbus = of_match_bus(parent);
  325. pbus->count_cells(dev, &pna, &pns);
  326. if (!OF_CHECK_COUNTS(pna, pns)) {
  327. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  328. dev->full_name);
  329. break;
  330. }
  331. DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  332. pbus->name, pna, pns, parent->full_name);
  333. /* Apply bus translation */
  334. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
  335. break;
  336. /* Complete the move up one level */
  337. na = pna;
  338. ns = pns;
  339. bus = pbus;
  340. of_dump_addr("OF: one level translation:", addr, na);
  341. }
  342. bail:
  343. of_node_put(parent);
  344. of_node_put(dev);
  345. return result;
  346. }
  347. EXPORT_SYMBOL(of_translate_address);
  348. u32 *of_get_address(struct device_node *dev, int index, u64 *size,
  349. unsigned int *flags)
  350. {
  351. u32 *prop;
  352. unsigned int psize;
  353. struct device_node *parent;
  354. struct of_bus *bus;
  355. int onesize, i, na, ns;
  356. /* Get parent & match bus type */
  357. parent = of_get_parent(dev);
  358. if (parent == NULL)
  359. return NULL;
  360. bus = of_match_bus(parent);
  361. bus->count_cells(dev, &na, &ns);
  362. of_node_put(parent);
  363. if (!OF_CHECK_COUNTS(na, ns))
  364. return NULL;
  365. /* Get "reg" or "assigned-addresses" property */
  366. prop = (u32 *)get_property(dev, bus->addresses, &psize);
  367. if (prop == NULL)
  368. return NULL;
  369. psize /= 4;
  370. onesize = na + ns;
  371. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  372. if (i == index) {
  373. if (size)
  374. *size = of_read_addr(prop + na, ns);
  375. if (flags)
  376. *flags = bus->get_flags(prop);
  377. return prop;
  378. }
  379. return NULL;
  380. }
  381. EXPORT_SYMBOL(of_get_address);
  382. u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  383. unsigned int *flags)
  384. {
  385. u32 *prop;
  386. unsigned int psize;
  387. struct device_node *parent;
  388. struct of_bus *bus;
  389. int onesize, i, na, ns;
  390. /* Get parent & match bus type */
  391. parent = of_get_parent(dev);
  392. if (parent == NULL)
  393. return NULL;
  394. bus = of_match_bus(parent);
  395. if (strcmp(bus->name, "pci"))
  396. return NULL;
  397. bus->count_cells(dev, &na, &ns);
  398. of_node_put(parent);
  399. if (!OF_CHECK_COUNTS(na, ns))
  400. return NULL;
  401. /* Get "reg" or "assigned-addresses" property */
  402. prop = (u32 *)get_property(dev, bus->addresses, &psize);
  403. if (prop == NULL)
  404. return NULL;
  405. psize /= 4;
  406. onesize = na + ns;
  407. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  408. if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  409. if (size)
  410. *size = of_read_addr(prop + na, ns);
  411. if (flags)
  412. *flags = bus->get_flags(prop);
  413. return prop;
  414. }
  415. return NULL;
  416. }
  417. EXPORT_SYMBOL(of_get_pci_address);
  418. static int __of_address_to_resource(struct device_node *dev, u32 *addrp,
  419. u64 size, unsigned int flags,
  420. struct resource *r)
  421. {
  422. u64 taddr;
  423. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  424. return -EINVAL;
  425. taddr = of_translate_address(dev, addrp);
  426. if (taddr == OF_BAD_ADDR)
  427. return -EINVAL;
  428. memset(r, 0, sizeof(struct resource));
  429. if (flags & IORESOURCE_IO) {
  430. unsigned long port;
  431. port = pci_address_to_pio(taddr);
  432. if (port == (unsigned long)-1)
  433. return -EINVAL;
  434. r->start = port;
  435. r->end = port + size - 1;
  436. } else {
  437. r->start = taddr;
  438. r->end = taddr + size - 1;
  439. }
  440. r->flags = flags;
  441. r->name = dev->name;
  442. return 0;
  443. }
  444. int of_address_to_resource(struct device_node *dev, int index,
  445. struct resource *r)
  446. {
  447. u32 *addrp;
  448. u64 size;
  449. unsigned int flags;
  450. addrp = of_get_address(dev, index, &size, &flags);
  451. if (addrp == NULL)
  452. return -EINVAL;
  453. return __of_address_to_resource(dev, addrp, size, flags, r);
  454. }
  455. EXPORT_SYMBOL_GPL(of_address_to_resource);
  456. int of_pci_address_to_resource(struct device_node *dev, int bar,
  457. struct resource *r)
  458. {
  459. u32 *addrp;
  460. u64 size;
  461. unsigned int flags;
  462. addrp = of_get_pci_address(dev, bar, &size, &flags);
  463. if (addrp == NULL)
  464. return -EINVAL;
  465. return __of_address_to_resource(dev, addrp, size, flags, r);
  466. }
  467. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);