address.c 16 KB

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