address.c 16 KB

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