address.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #include <linux/device.h>
  2. #include <linux/io.h>
  3. #include <linux/ioport.h>
  4. #include <linux/module.h>
  5. #include <linux/of_address.h>
  6. #include <linux/pci_regs.h>
  7. #include <linux/string.h>
  8. /* Max address size we deal with */
  9. #define OF_MAX_ADDR_CELLS 4
  10. #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
  11. #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
  12. static struct of_bus *of_match_bus(struct device_node *np);
  13. static int __of_address_to_resource(struct device_node *dev,
  14. const __be32 *addrp, u64 size, unsigned int flags,
  15. const char *name, struct resource *r);
  16. /* Debug utility */
  17. #ifdef DEBUG
  18. static void of_dump_addr(const char *s, const __be32 *addr, int na)
  19. {
  20. printk(KERN_DEBUG "%s", s);
  21. while (na--)
  22. printk(" %08x", be32_to_cpu(*(addr++)));
  23. printk("\n");
  24. }
  25. #else
  26. static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
  27. #endif
  28. /* Callbacks for bus specific translators */
  29. struct of_bus {
  30. const char *name;
  31. const char *addresses;
  32. int (*match)(struct device_node *parent);
  33. void (*count_cells)(struct device_node *child,
  34. int *addrc, int *sizec);
  35. u64 (*map)(__be32 *addr, const __be32 *range,
  36. int na, int ns, int pna);
  37. int (*translate)(__be32 *addr, u64 offset, int na);
  38. unsigned int (*get_flags)(const __be32 *addr);
  39. };
  40. /*
  41. * Default translator (generic bus)
  42. */
  43. static void of_bus_default_count_cells(struct device_node *dev,
  44. int *addrc, int *sizec)
  45. {
  46. if (addrc)
  47. *addrc = of_n_addr_cells(dev);
  48. if (sizec)
  49. *sizec = of_n_size_cells(dev);
  50. }
  51. static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
  52. int na, int ns, int pna)
  53. {
  54. u64 cp, s, da;
  55. cp = of_read_number(range, na);
  56. s = of_read_number(range + na + pna, ns);
  57. da = of_read_number(addr, na);
  58. pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
  59. (unsigned long long)cp, (unsigned long long)s,
  60. (unsigned long long)da);
  61. /*
  62. * If the number of address cells is larger than 2 we assume the
  63. * mapping doesn't specify a physical address. Rather, the address
  64. * specifies an identifier that must match exactly.
  65. */
  66. if (na > 2 && memcmp(range, addr, na * 4) != 0)
  67. return OF_BAD_ADDR;
  68. if (da < cp || da >= (cp + s))
  69. return OF_BAD_ADDR;
  70. return da - cp;
  71. }
  72. static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
  73. {
  74. u64 a = of_read_number(addr, na);
  75. memset(addr, 0, na * 4);
  76. a += offset;
  77. if (na > 1)
  78. addr[na - 2] = cpu_to_be32(a >> 32);
  79. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  80. return 0;
  81. }
  82. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  83. {
  84. return IORESOURCE_MEM;
  85. }
  86. #ifdef CONFIG_PCI
  87. /*
  88. * PCI bus specific translator
  89. */
  90. static int of_bus_pci_match(struct device_node *np)
  91. {
  92. /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
  93. return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
  94. }
  95. static void of_bus_pci_count_cells(struct device_node *np,
  96. int *addrc, int *sizec)
  97. {
  98. if (addrc)
  99. *addrc = 3;
  100. if (sizec)
  101. *sizec = 2;
  102. }
  103. static unsigned int of_bus_pci_get_flags(const __be32 *addr)
  104. {
  105. unsigned int flags = 0;
  106. u32 w = be32_to_cpup(addr);
  107. switch((w >> 24) & 0x03) {
  108. case 0x01:
  109. flags |= IORESOURCE_IO;
  110. break;
  111. case 0x02: /* 32 bits */
  112. case 0x03: /* 64 bits */
  113. flags |= IORESOURCE_MEM;
  114. break;
  115. }
  116. if (w & 0x40000000)
  117. flags |= IORESOURCE_PREFETCH;
  118. return flags;
  119. }
  120. static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
  121. int pna)
  122. {
  123. u64 cp, s, da;
  124. unsigned int af, rf;
  125. af = of_bus_pci_get_flags(addr);
  126. rf = of_bus_pci_get_flags(range);
  127. /* Check address type match */
  128. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  129. return OF_BAD_ADDR;
  130. /* Read address values, skipping high cell */
  131. cp = of_read_number(range + 1, na - 1);
  132. s = of_read_number(range + na + pna, ns);
  133. da = of_read_number(addr + 1, na - 1);
  134. pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
  135. (unsigned long long)cp, (unsigned long long)s,
  136. (unsigned long long)da);
  137. if (da < cp || da >= (cp + s))
  138. return OF_BAD_ADDR;
  139. return da - cp;
  140. }
  141. static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
  142. {
  143. return of_bus_default_translate(addr + 1, offset, na - 1);
  144. }
  145. const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  146. unsigned int *flags)
  147. {
  148. const __be32 *prop;
  149. unsigned int psize;
  150. struct device_node *parent;
  151. struct of_bus *bus;
  152. int onesize, i, na, ns;
  153. /* Get parent & match bus type */
  154. parent = of_get_parent(dev);
  155. if (parent == NULL)
  156. return NULL;
  157. bus = of_match_bus(parent);
  158. if (strcmp(bus->name, "pci")) {
  159. of_node_put(parent);
  160. return NULL;
  161. }
  162. bus->count_cells(dev, &na, &ns);
  163. of_node_put(parent);
  164. if (!OF_CHECK_ADDR_COUNT(na))
  165. return NULL;
  166. /* Get "reg" or "assigned-addresses" property */
  167. prop = of_get_property(dev, bus->addresses, &psize);
  168. if (prop == NULL)
  169. return NULL;
  170. psize /= 4;
  171. onesize = na + ns;
  172. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  173. u32 val = be32_to_cpu(prop[0]);
  174. if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  175. if (size)
  176. *size = of_read_number(prop + na, ns);
  177. if (flags)
  178. *flags = bus->get_flags(prop);
  179. return prop;
  180. }
  181. }
  182. return NULL;
  183. }
  184. EXPORT_SYMBOL(of_get_pci_address);
  185. int of_pci_address_to_resource(struct device_node *dev, int bar,
  186. struct resource *r)
  187. {
  188. const __be32 *addrp;
  189. u64 size;
  190. unsigned int flags;
  191. addrp = of_get_pci_address(dev, bar, &size, &flags);
  192. if (addrp == NULL)
  193. return -EINVAL;
  194. return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
  195. }
  196. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  197. #endif /* CONFIG_PCI */
  198. /*
  199. * ISA bus specific translator
  200. */
  201. static int of_bus_isa_match(struct device_node *np)
  202. {
  203. return !strcmp(np->name, "isa");
  204. }
  205. static void of_bus_isa_count_cells(struct device_node *child,
  206. int *addrc, int *sizec)
  207. {
  208. if (addrc)
  209. *addrc = 2;
  210. if (sizec)
  211. *sizec = 1;
  212. }
  213. static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
  214. int pna)
  215. {
  216. u64 cp, s, da;
  217. /* Check address type match */
  218. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  219. return OF_BAD_ADDR;
  220. /* Read address values, skipping high cell */
  221. cp = of_read_number(range + 1, na - 1);
  222. s = of_read_number(range + na + pna, ns);
  223. da = of_read_number(addr + 1, na - 1);
  224. pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
  225. (unsigned long long)cp, (unsigned long long)s,
  226. (unsigned long long)da);
  227. if (da < cp || da >= (cp + s))
  228. return OF_BAD_ADDR;
  229. return da - cp;
  230. }
  231. static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
  232. {
  233. return of_bus_default_translate(addr + 1, offset, na - 1);
  234. }
  235. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  236. {
  237. unsigned int flags = 0;
  238. u32 w = be32_to_cpup(addr);
  239. if (w & 1)
  240. flags |= IORESOURCE_IO;
  241. else
  242. flags |= IORESOURCE_MEM;
  243. return flags;
  244. }
  245. /*
  246. * Array of bus specific translators
  247. */
  248. static struct of_bus of_busses[] = {
  249. #ifdef CONFIG_PCI
  250. /* PCI */
  251. {
  252. .name = "pci",
  253. .addresses = "assigned-addresses",
  254. .match = of_bus_pci_match,
  255. .count_cells = of_bus_pci_count_cells,
  256. .map = of_bus_pci_map,
  257. .translate = of_bus_pci_translate,
  258. .get_flags = of_bus_pci_get_flags,
  259. },
  260. #endif /* CONFIG_PCI */
  261. /* ISA */
  262. {
  263. .name = "isa",
  264. .addresses = "reg",
  265. .match = of_bus_isa_match,
  266. .count_cells = of_bus_isa_count_cells,
  267. .map = of_bus_isa_map,
  268. .translate = of_bus_isa_translate,
  269. .get_flags = of_bus_isa_get_flags,
  270. },
  271. /* Default */
  272. {
  273. .name = "default",
  274. .addresses = "reg",
  275. .match = NULL,
  276. .count_cells = of_bus_default_count_cells,
  277. .map = of_bus_default_map,
  278. .translate = of_bus_default_translate,
  279. .get_flags = of_bus_default_get_flags,
  280. },
  281. };
  282. static struct of_bus *of_match_bus(struct device_node *np)
  283. {
  284. int i;
  285. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  286. if (!of_busses[i].match || of_busses[i].match(np))
  287. return &of_busses[i];
  288. BUG();
  289. return NULL;
  290. }
  291. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  292. struct of_bus *pbus, __be32 *addr,
  293. int na, int ns, int pna, const char *rprop)
  294. {
  295. const __be32 *ranges;
  296. unsigned int rlen;
  297. int rone;
  298. u64 offset = OF_BAD_ADDR;
  299. /* Normally, an absence of a "ranges" property means we are
  300. * crossing a non-translatable boundary, and thus the addresses
  301. * below the current not cannot be converted to CPU physical ones.
  302. * Unfortunately, while this is very clear in the spec, it's not
  303. * what Apple understood, and they do have things like /uni-n or
  304. * /ht nodes with no "ranges" property and a lot of perfectly
  305. * useable mapped devices below them. Thus we treat the absence of
  306. * "ranges" as equivalent to an empty "ranges" property which means
  307. * a 1:1 translation at that level. It's up to the caller not to try
  308. * to translate addresses that aren't supposed to be translated in
  309. * the first place. --BenH.
  310. *
  311. * As far as we know, this damage only exists on Apple machines, so
  312. * This code is only enabled on powerpc. --gcl
  313. */
  314. ranges = of_get_property(parent, rprop, &rlen);
  315. #if !defined(CONFIG_PPC)
  316. if (ranges == NULL) {
  317. pr_err("OF: no ranges; cannot translate\n");
  318. return 1;
  319. }
  320. #endif /* !defined(CONFIG_PPC) */
  321. if (ranges == NULL || rlen == 0) {
  322. offset = of_read_number(addr, na);
  323. memset(addr, 0, pna * 4);
  324. pr_debug("OF: empty ranges; 1:1 translation\n");
  325. goto finish;
  326. }
  327. pr_debug("OF: walking ranges...\n");
  328. /* Now walk through the ranges */
  329. rlen /= 4;
  330. rone = na + pna + ns;
  331. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  332. offset = bus->map(addr, ranges, na, ns, pna);
  333. if (offset != OF_BAD_ADDR)
  334. break;
  335. }
  336. if (offset == OF_BAD_ADDR) {
  337. pr_debug("OF: not found !\n");
  338. return 1;
  339. }
  340. memcpy(addr, ranges + na, 4 * pna);
  341. finish:
  342. of_dump_addr("OF: parent translation for:", addr, pna);
  343. pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
  344. /* Translate it into parent bus space */
  345. return pbus->translate(addr, offset, pna);
  346. }
  347. /*
  348. * Translate an address from the device-tree into a CPU physical address,
  349. * this walks up the tree and applies the various bus mappings on the
  350. * way.
  351. *
  352. * Note: We consider that crossing any level with #size-cells == 0 to mean
  353. * that translation is impossible (that is we are not dealing with a value
  354. * that can be mapped to a cpu physical address). This is not really specified
  355. * that way, but this is traditionally the way IBM at least do things
  356. */
  357. static u64 __of_translate_address(struct device_node *dev,
  358. const __be32 *in_addr, const char *rprop)
  359. {
  360. struct device_node *parent = NULL;
  361. struct of_bus *bus, *pbus;
  362. __be32 addr[OF_MAX_ADDR_CELLS];
  363. int na, ns, pna, pns;
  364. u64 result = OF_BAD_ADDR;
  365. pr_debug("OF: ** translation for device %s **\n", dev->full_name);
  366. /* Increase refcount at current level */
  367. of_node_get(dev);
  368. /* Get parent & match bus type */
  369. parent = of_get_parent(dev);
  370. if (parent == NULL)
  371. goto bail;
  372. bus = of_match_bus(parent);
  373. /* Count address cells & copy address locally */
  374. bus->count_cells(dev, &na, &ns);
  375. if (!OF_CHECK_COUNTS(na, ns)) {
  376. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  377. dev->full_name);
  378. goto bail;
  379. }
  380. memcpy(addr, in_addr, na * 4);
  381. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  382. bus->name, na, ns, parent->full_name);
  383. of_dump_addr("OF: translating address:", addr, na);
  384. /* Translate */
  385. for (;;) {
  386. /* Switch to parent bus */
  387. of_node_put(dev);
  388. dev = parent;
  389. parent = of_get_parent(dev);
  390. /* If root, we have finished */
  391. if (parent == NULL) {
  392. pr_debug("OF: reached root node\n");
  393. result = of_read_number(addr, na);
  394. break;
  395. }
  396. /* Get new parent bus and counts */
  397. pbus = of_match_bus(parent);
  398. pbus->count_cells(dev, &pna, &pns);
  399. if (!OF_CHECK_COUNTS(pna, pns)) {
  400. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  401. dev->full_name);
  402. break;
  403. }
  404. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  405. pbus->name, pna, pns, parent->full_name);
  406. /* Apply bus translation */
  407. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  408. break;
  409. /* Complete the move up one level */
  410. na = pna;
  411. ns = pns;
  412. bus = pbus;
  413. of_dump_addr("OF: one level translation:", addr, na);
  414. }
  415. bail:
  416. of_node_put(parent);
  417. of_node_put(dev);
  418. return result;
  419. }
  420. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  421. {
  422. return __of_translate_address(dev, in_addr, "ranges");
  423. }
  424. EXPORT_SYMBOL(of_translate_address);
  425. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  426. {
  427. return __of_translate_address(dev, in_addr, "dma-ranges");
  428. }
  429. EXPORT_SYMBOL(of_translate_dma_address);
  430. bool of_can_translate_address(struct device_node *dev)
  431. {
  432. struct device_node *parent;
  433. struct of_bus *bus;
  434. int na, ns;
  435. parent = of_get_parent(dev);
  436. if (parent == NULL)
  437. return false;
  438. bus = of_match_bus(parent);
  439. bus->count_cells(dev, &na, &ns);
  440. of_node_put(parent);
  441. return OF_CHECK_COUNTS(na, ns);
  442. }
  443. EXPORT_SYMBOL(of_can_translate_address);
  444. const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
  445. unsigned int *flags)
  446. {
  447. const __be32 *prop;
  448. unsigned int psize;
  449. struct device_node *parent;
  450. struct of_bus *bus;
  451. int onesize, i, na, ns;
  452. /* Get parent & match bus type */
  453. parent = of_get_parent(dev);
  454. if (parent == NULL)
  455. return NULL;
  456. bus = of_match_bus(parent);
  457. bus->count_cells(dev, &na, &ns);
  458. of_node_put(parent);
  459. if (!OF_CHECK_ADDR_COUNT(na))
  460. return NULL;
  461. /* Get "reg" or "assigned-addresses" property */
  462. prop = of_get_property(dev, bus->addresses, &psize);
  463. if (prop == NULL)
  464. return NULL;
  465. psize /= 4;
  466. onesize = na + ns;
  467. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  468. if (i == index) {
  469. if (size)
  470. *size = of_read_number(prop + na, ns);
  471. if (flags)
  472. *flags = bus->get_flags(prop);
  473. return prop;
  474. }
  475. return NULL;
  476. }
  477. EXPORT_SYMBOL(of_get_address);
  478. static int __of_address_to_resource(struct device_node *dev,
  479. const __be32 *addrp, u64 size, unsigned int flags,
  480. const char *name, struct resource *r)
  481. {
  482. u64 taddr;
  483. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  484. return -EINVAL;
  485. taddr = of_translate_address(dev, addrp);
  486. if (taddr == OF_BAD_ADDR)
  487. return -EINVAL;
  488. memset(r, 0, sizeof(struct resource));
  489. if (flags & IORESOURCE_IO) {
  490. unsigned long port;
  491. port = pci_address_to_pio(taddr);
  492. if (port == (unsigned long)-1)
  493. return -EINVAL;
  494. r->start = port;
  495. r->end = port + size - 1;
  496. } else {
  497. r->start = taddr;
  498. r->end = taddr + size - 1;
  499. }
  500. r->flags = flags;
  501. r->name = name ? name : dev->full_name;
  502. return 0;
  503. }
  504. /**
  505. * of_address_to_resource - Translate device tree address and return as resource
  506. *
  507. * Note that if your address is a PIO address, the conversion will fail if
  508. * the physical address can't be internally converted to an IO token with
  509. * pci_address_to_pio(), that is because it's either called to early or it
  510. * can't be matched to any host bridge IO space
  511. */
  512. int of_address_to_resource(struct device_node *dev, int index,
  513. struct resource *r)
  514. {
  515. const __be32 *addrp;
  516. u64 size;
  517. unsigned int flags;
  518. const char *name = NULL;
  519. addrp = of_get_address(dev, index, &size, &flags);
  520. if (addrp == NULL)
  521. return -EINVAL;
  522. /* Get optional "reg-names" property to add a name to a resource */
  523. of_property_read_string_index(dev, "reg-names", index, &name);
  524. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  525. }
  526. EXPORT_SYMBOL_GPL(of_address_to_resource);
  527. struct device_node *of_find_matching_node_by_address(struct device_node *from,
  528. const struct of_device_id *matches,
  529. u64 base_address)
  530. {
  531. struct device_node *dn = of_find_matching_node(from, matches);
  532. struct resource res;
  533. while (dn) {
  534. if (of_address_to_resource(dn, 0, &res))
  535. continue;
  536. if (res.start == base_address)
  537. return dn;
  538. dn = of_find_matching_node(dn, matches);
  539. }
  540. return NULL;
  541. }
  542. /**
  543. * of_iomap - Maps the memory mapped IO for a given device_node
  544. * @device: the device whose io range will be mapped
  545. * @index: index of the io range
  546. *
  547. * Returns a pointer to the mapped memory
  548. */
  549. void __iomem *of_iomap(struct device_node *np, int index)
  550. {
  551. struct resource res;
  552. if (of_address_to_resource(np, index, &res))
  553. return NULL;
  554. return ioremap(res.start, resource_size(&res));
  555. }
  556. EXPORT_SYMBOL(of_iomap);