address.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. #define pr_fmt(fmt) "OF: " fmt
  2. #include <linux/device.h>
  3. #include <linux/io.h>
  4. #include <linux/ioport.h>
  5. #include <linux/module.h>
  6. #include <linux/of_address.h>
  7. #include <linux/pci.h>
  8. #include <linux/pci_regs.h>
  9. #include <linux/sizes.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. /* Max address size we deal with */
  13. #define OF_MAX_ADDR_CELLS 4
  14. #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
  15. #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
  16. static struct of_bus *of_match_bus(struct device_node *np);
  17. static int __of_address_to_resource(struct device_node *dev,
  18. const __be32 *addrp, u64 size, unsigned int flags,
  19. const char *name, struct resource *r);
  20. /* Debug utility */
  21. #ifdef DEBUG
  22. static void of_dump_addr(const char *s, const __be32 *addr, int na)
  23. {
  24. pr_debug("%s", s);
  25. while (na--)
  26. pr_cont(" %08x", be32_to_cpu(*(addr++)));
  27. pr_cont("\n");
  28. }
  29. #else
  30. static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
  31. #endif
  32. /* Callbacks for bus specific translators */
  33. struct of_bus {
  34. const char *name;
  35. const char *addresses;
  36. int (*match)(struct device_node *parent);
  37. void (*count_cells)(struct device_node *child,
  38. int *addrc, int *sizec);
  39. u64 (*map)(__be32 *addr, const __be32 *range,
  40. int na, int ns, int pna);
  41. int (*translate)(__be32 *addr, u64 offset, int na);
  42. unsigned int (*get_flags)(const __be32 *addr);
  43. };
  44. /*
  45. * Default translator (generic bus)
  46. */
  47. static void of_bus_default_count_cells(struct device_node *dev,
  48. int *addrc, int *sizec)
  49. {
  50. if (addrc)
  51. *addrc = of_n_addr_cells(dev);
  52. if (sizec)
  53. *sizec = of_n_size_cells(dev);
  54. }
  55. static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
  56. int na, int ns, int pna)
  57. {
  58. u64 cp, s, da;
  59. cp = of_read_number(range, na);
  60. s = of_read_number(range + na + pna, ns);
  61. da = of_read_number(addr, na);
  62. pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
  63. (unsigned long long)cp, (unsigned long long)s,
  64. (unsigned long long)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(__be32 *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] = cpu_to_be32(a >> 32);
  76. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  77. return 0;
  78. }
  79. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  80. {
  81. return IORESOURCE_MEM;
  82. }
  83. #ifdef CONFIG_OF_ADDRESS_PCI
  84. /*
  85. * PCI bus specific translator
  86. */
  87. static int of_bus_pci_match(struct device_node *np)
  88. {
  89. /*
  90. * "pciex" is PCI Express
  91. * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
  92. * "ht" is hypertransport
  93. */
  94. return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") ||
  95. !strcmp(np->type, "vci") || !strcmp(np->type, "ht");
  96. }
  97. static void of_bus_pci_count_cells(struct device_node *np,
  98. int *addrc, int *sizec)
  99. {
  100. if (addrc)
  101. *addrc = 3;
  102. if (sizec)
  103. *sizec = 2;
  104. }
  105. static unsigned int of_bus_pci_get_flags(const __be32 *addr)
  106. {
  107. unsigned int flags = 0;
  108. u32 w = be32_to_cpup(addr);
  109. switch((w >> 24) & 0x03) {
  110. case 0x01:
  111. flags |= IORESOURCE_IO;
  112. break;
  113. case 0x02: /* 32 bits */
  114. case 0x03: /* 64 bits */
  115. flags |= IORESOURCE_MEM;
  116. break;
  117. }
  118. if (w & 0x40000000)
  119. flags |= IORESOURCE_PREFETCH;
  120. return flags;
  121. }
  122. static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
  123. int pna)
  124. {
  125. u64 cp, s, da;
  126. unsigned int af, rf;
  127. af = of_bus_pci_get_flags(addr);
  128. rf = of_bus_pci_get_flags(range);
  129. /* Check address type match */
  130. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  131. return OF_BAD_ADDR;
  132. /* Read address values, skipping high cell */
  133. cp = of_read_number(range + 1, na - 1);
  134. s = of_read_number(range + na + pna, ns);
  135. da = of_read_number(addr + 1, na - 1);
  136. pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
  137. (unsigned long long)cp, (unsigned long long)s,
  138. (unsigned long long)da);
  139. if (da < cp || da >= (cp + s))
  140. return OF_BAD_ADDR;
  141. return da - cp;
  142. }
  143. static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
  144. {
  145. return of_bus_default_translate(addr + 1, offset, na - 1);
  146. }
  147. #endif /* CONFIG_OF_ADDRESS_PCI */
  148. #ifdef CONFIG_PCI
  149. const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  150. unsigned int *flags)
  151. {
  152. const __be32 *prop;
  153. unsigned int psize;
  154. struct device_node *parent;
  155. struct of_bus *bus;
  156. int onesize, i, na, ns;
  157. /* Get parent & match bus type */
  158. parent = of_get_parent(dev);
  159. if (parent == NULL)
  160. return NULL;
  161. bus = of_match_bus(parent);
  162. if (strcmp(bus->name, "pci")) {
  163. of_node_put(parent);
  164. return NULL;
  165. }
  166. bus->count_cells(dev, &na, &ns);
  167. of_node_put(parent);
  168. if (!OF_CHECK_ADDR_COUNT(na))
  169. return NULL;
  170. /* Get "reg" or "assigned-addresses" property */
  171. prop = of_get_property(dev, bus->addresses, &psize);
  172. if (prop == NULL)
  173. return NULL;
  174. psize /= 4;
  175. onesize = na + ns;
  176. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  177. u32 val = be32_to_cpu(prop[0]);
  178. if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  179. if (size)
  180. *size = of_read_number(prop + na, ns);
  181. if (flags)
  182. *flags = bus->get_flags(prop);
  183. return prop;
  184. }
  185. }
  186. return NULL;
  187. }
  188. EXPORT_SYMBOL(of_get_pci_address);
  189. int of_pci_address_to_resource(struct device_node *dev, int bar,
  190. struct resource *r)
  191. {
  192. const __be32 *addrp;
  193. u64 size;
  194. unsigned int flags;
  195. addrp = of_get_pci_address(dev, bar, &size, &flags);
  196. if (addrp == NULL)
  197. return -EINVAL;
  198. return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
  199. }
  200. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  201. int of_pci_range_parser_init(struct of_pci_range_parser *parser,
  202. struct device_node *node)
  203. {
  204. const int na = 3, ns = 2;
  205. int rlen;
  206. parser->node = node;
  207. parser->pna = of_n_addr_cells(node);
  208. parser->np = parser->pna + na + ns;
  209. parser->range = of_get_property(node, "ranges", &rlen);
  210. if (parser->range == NULL)
  211. return -ENOENT;
  212. parser->end = parser->range + rlen / sizeof(__be32);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
  216. struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
  217. struct of_pci_range *range)
  218. {
  219. const int na = 3, ns = 2;
  220. if (!range)
  221. return NULL;
  222. if (!parser->range || parser->range + parser->np > parser->end)
  223. return NULL;
  224. range->pci_space = be32_to_cpup(parser->range);
  225. range->flags = of_bus_pci_get_flags(parser->range);
  226. range->pci_addr = of_read_number(parser->range + 1, ns);
  227. range->cpu_addr = of_translate_address(parser->node,
  228. parser->range + na);
  229. range->size = of_read_number(parser->range + parser->pna + na, ns);
  230. parser->range += parser->np;
  231. /* Now consume following elements while they are contiguous */
  232. while (parser->range + parser->np <= parser->end) {
  233. u32 flags;
  234. u64 pci_addr, cpu_addr, size;
  235. flags = of_bus_pci_get_flags(parser->range);
  236. pci_addr = of_read_number(parser->range + 1, ns);
  237. cpu_addr = of_translate_address(parser->node,
  238. parser->range + na);
  239. size = of_read_number(parser->range + parser->pna + na, ns);
  240. if (flags != range->flags)
  241. break;
  242. if (pci_addr != range->pci_addr + range->size ||
  243. cpu_addr != range->cpu_addr + range->size)
  244. break;
  245. range->size += size;
  246. parser->range += parser->np;
  247. }
  248. return range;
  249. }
  250. EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
  251. /*
  252. * of_pci_range_to_resource - Create a resource from an of_pci_range
  253. * @range: the PCI range that describes the resource
  254. * @np: device node where the range belongs to
  255. * @res: pointer to a valid resource that will be updated to
  256. * reflect the values contained in the range.
  257. *
  258. * Returns EINVAL if the range cannot be converted to resource.
  259. *
  260. * Note that if the range is an IO range, the resource will be converted
  261. * using pci_address_to_pio() which can fail if it is called too early or
  262. * if the range cannot be matched to any host bridge IO space (our case here).
  263. * To guard against that we try to register the IO range first.
  264. * If that fails we know that pci_address_to_pio() will do too.
  265. */
  266. int of_pci_range_to_resource(struct of_pci_range *range,
  267. struct device_node *np, struct resource *res)
  268. {
  269. int err;
  270. res->flags = range->flags;
  271. res->parent = res->child = res->sibling = NULL;
  272. res->name = np->full_name;
  273. if (res->flags & IORESOURCE_IO) {
  274. unsigned long port;
  275. err = pci_register_io_range(range->cpu_addr, range->size);
  276. if (err)
  277. goto invalid_range;
  278. port = pci_address_to_pio(range->cpu_addr);
  279. if (port == (unsigned long)-1) {
  280. err = -EINVAL;
  281. goto invalid_range;
  282. }
  283. res->start = port;
  284. } else {
  285. if ((sizeof(resource_size_t) < 8) &&
  286. upper_32_bits(range->cpu_addr)) {
  287. err = -EINVAL;
  288. goto invalid_range;
  289. }
  290. res->start = range->cpu_addr;
  291. }
  292. res->end = res->start + range->size - 1;
  293. return 0;
  294. invalid_range:
  295. res->start = (resource_size_t)OF_BAD_ADDR;
  296. res->end = (resource_size_t)OF_BAD_ADDR;
  297. return err;
  298. }
  299. #endif /* CONFIG_PCI */
  300. /*
  301. * ISA bus specific translator
  302. */
  303. static int of_bus_isa_match(struct device_node *np)
  304. {
  305. return !strcmp(np->name, "isa");
  306. }
  307. static void of_bus_isa_count_cells(struct device_node *child,
  308. int *addrc, int *sizec)
  309. {
  310. if (addrc)
  311. *addrc = 2;
  312. if (sizec)
  313. *sizec = 1;
  314. }
  315. static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
  316. int pna)
  317. {
  318. u64 cp, s, da;
  319. /* Check address type match */
  320. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  321. return OF_BAD_ADDR;
  322. /* Read address values, skipping high cell */
  323. cp = of_read_number(range + 1, na - 1);
  324. s = of_read_number(range + na + pna, ns);
  325. da = of_read_number(addr + 1, na - 1);
  326. pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
  327. (unsigned long long)cp, (unsigned long long)s,
  328. (unsigned long long)da);
  329. if (da < cp || da >= (cp + s))
  330. return OF_BAD_ADDR;
  331. return da - cp;
  332. }
  333. static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
  334. {
  335. return of_bus_default_translate(addr + 1, offset, na - 1);
  336. }
  337. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  338. {
  339. unsigned int flags = 0;
  340. u32 w = be32_to_cpup(addr);
  341. if (w & 1)
  342. flags |= IORESOURCE_IO;
  343. else
  344. flags |= IORESOURCE_MEM;
  345. return flags;
  346. }
  347. /*
  348. * Array of bus specific translators
  349. */
  350. static struct of_bus of_busses[] = {
  351. #ifdef CONFIG_OF_ADDRESS_PCI
  352. /* PCI */
  353. {
  354. .name = "pci",
  355. .addresses = "assigned-addresses",
  356. .match = of_bus_pci_match,
  357. .count_cells = of_bus_pci_count_cells,
  358. .map = of_bus_pci_map,
  359. .translate = of_bus_pci_translate,
  360. .get_flags = of_bus_pci_get_flags,
  361. },
  362. #endif /* CONFIG_OF_ADDRESS_PCI */
  363. /* ISA */
  364. {
  365. .name = "isa",
  366. .addresses = "reg",
  367. .match = of_bus_isa_match,
  368. .count_cells = of_bus_isa_count_cells,
  369. .map = of_bus_isa_map,
  370. .translate = of_bus_isa_translate,
  371. .get_flags = of_bus_isa_get_flags,
  372. },
  373. /* Default */
  374. {
  375. .name = "default",
  376. .addresses = "reg",
  377. .match = NULL,
  378. .count_cells = of_bus_default_count_cells,
  379. .map = of_bus_default_map,
  380. .translate = of_bus_default_translate,
  381. .get_flags = of_bus_default_get_flags,
  382. },
  383. };
  384. static struct of_bus *of_match_bus(struct device_node *np)
  385. {
  386. int i;
  387. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  388. if (!of_busses[i].match || of_busses[i].match(np))
  389. return &of_busses[i];
  390. BUG();
  391. return NULL;
  392. }
  393. static int of_empty_ranges_quirk(struct device_node *np)
  394. {
  395. if (IS_ENABLED(CONFIG_PPC)) {
  396. /* To save cycles, we cache the result for global "Mac" setting */
  397. static int quirk_state = -1;
  398. /* PA-SEMI sdc DT bug */
  399. if (of_device_is_compatible(np, "1682m-sdc"))
  400. return true;
  401. /* Make quirk cached */
  402. if (quirk_state < 0)
  403. quirk_state =
  404. of_machine_is_compatible("Power Macintosh") ||
  405. of_machine_is_compatible("MacRISC");
  406. return quirk_state;
  407. }
  408. return false;
  409. }
  410. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  411. struct of_bus *pbus, __be32 *addr,
  412. int na, int ns, int pna, const char *rprop)
  413. {
  414. const __be32 *ranges;
  415. unsigned int rlen;
  416. int rone;
  417. u64 offset = OF_BAD_ADDR;
  418. /*
  419. * Normally, an absence of a "ranges" property means we are
  420. * crossing a non-translatable boundary, and thus the addresses
  421. * below the current cannot be converted to CPU physical ones.
  422. * Unfortunately, while this is very clear in the spec, it's not
  423. * what Apple understood, and they do have things like /uni-n or
  424. * /ht nodes with no "ranges" property and a lot of perfectly
  425. * useable mapped devices below them. Thus we treat the absence of
  426. * "ranges" as equivalent to an empty "ranges" property which means
  427. * a 1:1 translation at that level. It's up to the caller not to try
  428. * to translate addresses that aren't supposed to be translated in
  429. * the first place. --BenH.
  430. *
  431. * As far as we know, this damage only exists on Apple machines, so
  432. * This code is only enabled on powerpc. --gcl
  433. */
  434. ranges = of_get_property(parent, rprop, &rlen);
  435. if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
  436. pr_debug("no ranges; cannot translate\n");
  437. return 1;
  438. }
  439. if (ranges == NULL || rlen == 0) {
  440. offset = of_read_number(addr, na);
  441. memset(addr, 0, pna * 4);
  442. pr_debug("empty ranges; 1:1 translation\n");
  443. goto finish;
  444. }
  445. pr_debug("walking ranges...\n");
  446. /* Now walk through the ranges */
  447. rlen /= 4;
  448. rone = na + pna + ns;
  449. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  450. offset = bus->map(addr, ranges, na, ns, pna);
  451. if (offset != OF_BAD_ADDR)
  452. break;
  453. }
  454. if (offset == OF_BAD_ADDR) {
  455. pr_debug("not found !\n");
  456. return 1;
  457. }
  458. memcpy(addr, ranges + na, 4 * pna);
  459. finish:
  460. of_dump_addr("parent translation for:", addr, pna);
  461. pr_debug("with offset: %llx\n", (unsigned long long)offset);
  462. /* Translate it into parent bus space */
  463. return pbus->translate(addr, offset, pna);
  464. }
  465. /*
  466. * Translate an address from the device-tree into a CPU physical address,
  467. * this walks up the tree and applies the various bus mappings on the
  468. * way.
  469. *
  470. * Note: We consider that crossing any level with #size-cells == 0 to mean
  471. * that translation is impossible (that is we are not dealing with a value
  472. * that can be mapped to a cpu physical address). This is not really specified
  473. * that way, but this is traditionally the way IBM at least do things
  474. */
  475. static u64 __of_translate_address(struct device_node *dev,
  476. const __be32 *in_addr, const char *rprop)
  477. {
  478. struct device_node *parent = NULL;
  479. struct of_bus *bus, *pbus;
  480. __be32 addr[OF_MAX_ADDR_CELLS];
  481. int na, ns, pna, pns;
  482. u64 result = OF_BAD_ADDR;
  483. pr_debug("** translation for device %pOF **\n", dev);
  484. /* Increase refcount at current level */
  485. of_node_get(dev);
  486. /* Get parent & match bus type */
  487. parent = of_get_parent(dev);
  488. if (parent == NULL)
  489. goto bail;
  490. bus = of_match_bus(parent);
  491. /* Count address cells & copy address locally */
  492. bus->count_cells(dev, &na, &ns);
  493. if (!OF_CHECK_COUNTS(na, ns)) {
  494. pr_debug("Bad cell count for %pOF\n", dev);
  495. goto bail;
  496. }
  497. memcpy(addr, in_addr, na * 4);
  498. pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
  499. bus->name, na, ns, parent);
  500. of_dump_addr("translating address:", addr, na);
  501. /* Translate */
  502. for (;;) {
  503. /* Switch to parent bus */
  504. of_node_put(dev);
  505. dev = parent;
  506. parent = of_get_parent(dev);
  507. /* If root, we have finished */
  508. if (parent == NULL) {
  509. pr_debug("reached root node\n");
  510. result = of_read_number(addr, na);
  511. break;
  512. }
  513. /* Get new parent bus and counts */
  514. pbus = of_match_bus(parent);
  515. pbus->count_cells(dev, &pna, &pns);
  516. if (!OF_CHECK_COUNTS(pna, pns)) {
  517. pr_err("Bad cell count for %pOF\n", dev);
  518. break;
  519. }
  520. pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
  521. pbus->name, pna, pns, parent);
  522. /* Apply bus translation */
  523. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  524. break;
  525. /* Complete the move up one level */
  526. na = pna;
  527. ns = pns;
  528. bus = pbus;
  529. of_dump_addr("one level translation:", addr, na);
  530. }
  531. bail:
  532. of_node_put(parent);
  533. of_node_put(dev);
  534. return result;
  535. }
  536. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  537. {
  538. return __of_translate_address(dev, in_addr, "ranges");
  539. }
  540. EXPORT_SYMBOL(of_translate_address);
  541. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  542. {
  543. return __of_translate_address(dev, in_addr, "dma-ranges");
  544. }
  545. EXPORT_SYMBOL(of_translate_dma_address);
  546. const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
  547. unsigned int *flags)
  548. {
  549. const __be32 *prop;
  550. unsigned int psize;
  551. struct device_node *parent;
  552. struct of_bus *bus;
  553. int onesize, i, na, ns;
  554. /* Get parent & match bus type */
  555. parent = of_get_parent(dev);
  556. if (parent == NULL)
  557. return NULL;
  558. bus = of_match_bus(parent);
  559. bus->count_cells(dev, &na, &ns);
  560. of_node_put(parent);
  561. if (!OF_CHECK_ADDR_COUNT(na))
  562. return NULL;
  563. /* Get "reg" or "assigned-addresses" property */
  564. prop = of_get_property(dev, bus->addresses, &psize);
  565. if (prop == NULL)
  566. return NULL;
  567. psize /= 4;
  568. onesize = na + ns;
  569. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  570. if (i == index) {
  571. if (size)
  572. *size = of_read_number(prop + na, ns);
  573. if (flags)
  574. *flags = bus->get_flags(prop);
  575. return prop;
  576. }
  577. return NULL;
  578. }
  579. EXPORT_SYMBOL(of_get_address);
  580. static int __of_address_to_resource(struct device_node *dev,
  581. const __be32 *addrp, u64 size, unsigned int flags,
  582. const char *name, struct resource *r)
  583. {
  584. u64 taddr;
  585. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  586. return -EINVAL;
  587. taddr = of_translate_address(dev, addrp);
  588. if (taddr == OF_BAD_ADDR)
  589. return -EINVAL;
  590. memset(r, 0, sizeof(struct resource));
  591. if (flags & IORESOURCE_IO) {
  592. unsigned long port;
  593. port = pci_address_to_pio(taddr);
  594. if (port == (unsigned long)-1)
  595. return -EINVAL;
  596. r->start = port;
  597. r->end = port + size - 1;
  598. } else {
  599. r->start = taddr;
  600. r->end = taddr + size - 1;
  601. }
  602. r->flags = flags;
  603. r->name = name ? name : dev->full_name;
  604. return 0;
  605. }
  606. /**
  607. * of_address_to_resource - Translate device tree address and return as resource
  608. *
  609. * Note that if your address is a PIO address, the conversion will fail if
  610. * the physical address can't be internally converted to an IO token with
  611. * pci_address_to_pio(), that is because it's either called too early or it
  612. * can't be matched to any host bridge IO space
  613. */
  614. int of_address_to_resource(struct device_node *dev, int index,
  615. struct resource *r)
  616. {
  617. const __be32 *addrp;
  618. u64 size;
  619. unsigned int flags;
  620. const char *name = NULL;
  621. addrp = of_get_address(dev, index, &size, &flags);
  622. if (addrp == NULL)
  623. return -EINVAL;
  624. /* Get optional "reg-names" property to add a name to a resource */
  625. of_property_read_string_index(dev, "reg-names", index, &name);
  626. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  627. }
  628. EXPORT_SYMBOL_GPL(of_address_to_resource);
  629. struct device_node *of_find_matching_node_by_address(struct device_node *from,
  630. const struct of_device_id *matches,
  631. u64 base_address)
  632. {
  633. struct device_node *dn = of_find_matching_node(from, matches);
  634. struct resource res;
  635. while (dn) {
  636. if (!of_address_to_resource(dn, 0, &res) &&
  637. res.start == base_address)
  638. return dn;
  639. dn = of_find_matching_node(dn, matches);
  640. }
  641. return NULL;
  642. }
  643. /**
  644. * of_iomap - Maps the memory mapped IO for a given device_node
  645. * @device: the device whose io range will be mapped
  646. * @index: index of the io range
  647. *
  648. * Returns a pointer to the mapped memory
  649. */
  650. void __iomem *of_iomap(struct device_node *np, int index)
  651. {
  652. struct resource res;
  653. if (of_address_to_resource(np, index, &res))
  654. return NULL;
  655. return ioremap(res.start, resource_size(&res));
  656. }
  657. EXPORT_SYMBOL(of_iomap);
  658. /*
  659. * of_io_request_and_map - Requests a resource and maps the memory mapped IO
  660. * for a given device_node
  661. * @device: the device whose io range will be mapped
  662. * @index: index of the io range
  663. * @name: name of the resource
  664. *
  665. * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
  666. * error code on failure. Usage example:
  667. *
  668. * base = of_io_request_and_map(node, 0, "foo");
  669. * if (IS_ERR(base))
  670. * return PTR_ERR(base);
  671. */
  672. void __iomem *of_io_request_and_map(struct device_node *np, int index,
  673. const char *name)
  674. {
  675. struct resource res;
  676. void __iomem *mem;
  677. if (of_address_to_resource(np, index, &res))
  678. return IOMEM_ERR_PTR(-EINVAL);
  679. if (!request_mem_region(res.start, resource_size(&res), name))
  680. return IOMEM_ERR_PTR(-EBUSY);
  681. mem = ioremap(res.start, resource_size(&res));
  682. if (!mem) {
  683. release_mem_region(res.start, resource_size(&res));
  684. return IOMEM_ERR_PTR(-ENOMEM);
  685. }
  686. return mem;
  687. }
  688. EXPORT_SYMBOL(of_io_request_and_map);
  689. /**
  690. * of_dma_get_range - Get DMA range info
  691. * @np: device node to get DMA range info
  692. * @dma_addr: pointer to store initial DMA address of DMA range
  693. * @paddr: pointer to store initial CPU address of DMA range
  694. * @size: pointer to store size of DMA range
  695. *
  696. * Look in bottom up direction for the first "dma-ranges" property
  697. * and parse it.
  698. * dma-ranges format:
  699. * DMA addr (dma_addr) : naddr cells
  700. * CPU addr (phys_addr_t) : pna cells
  701. * size : nsize cells
  702. *
  703. * It returns -ENODEV if "dma-ranges" property was not found
  704. * for this device in DT.
  705. */
  706. int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
  707. {
  708. struct device_node *node = of_node_get(np);
  709. const __be32 *ranges = NULL;
  710. int len, naddr, nsize, pna;
  711. int ret = 0;
  712. u64 dmaaddr;
  713. if (!node)
  714. return -EINVAL;
  715. while (1) {
  716. naddr = of_n_addr_cells(node);
  717. nsize = of_n_size_cells(node);
  718. node = of_get_next_parent(node);
  719. if (!node)
  720. break;
  721. ranges = of_get_property(node, "dma-ranges", &len);
  722. /* Ignore empty ranges, they imply no translation required */
  723. if (ranges && len > 0)
  724. break;
  725. /*
  726. * At least empty ranges has to be defined for parent node if
  727. * DMA is supported
  728. */
  729. if (!ranges)
  730. break;
  731. }
  732. if (!ranges) {
  733. pr_debug("no dma-ranges found for node(%pOF)\n", np);
  734. ret = -ENODEV;
  735. goto out;
  736. }
  737. len /= sizeof(u32);
  738. pna = of_n_addr_cells(node);
  739. /* dma-ranges format:
  740. * DMA addr : naddr cells
  741. * CPU addr : pna cells
  742. * size : nsize cells
  743. */
  744. dmaaddr = of_read_number(ranges, naddr);
  745. *paddr = of_translate_dma_address(np, ranges);
  746. if (*paddr == OF_BAD_ADDR) {
  747. pr_err("translation of DMA address(%pad) to CPU address failed node(%pOF)\n",
  748. dma_addr, np);
  749. ret = -EINVAL;
  750. goto out;
  751. }
  752. *dma_addr = dmaaddr;
  753. *size = of_read_number(ranges + naddr + pna, nsize);
  754. pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
  755. *dma_addr, *paddr, *size);
  756. out:
  757. of_node_put(node);
  758. return ret;
  759. }
  760. EXPORT_SYMBOL_GPL(of_dma_get_range);
  761. /**
  762. * of_dma_is_coherent - Check if device is coherent
  763. * @np: device node
  764. *
  765. * It returns true if "dma-coherent" property was found
  766. * for this device in the DT, or if DMA is coherent by
  767. * default for OF devices on the current platform.
  768. */
  769. bool of_dma_is_coherent(struct device_node *np)
  770. {
  771. struct device_node *node;
  772. if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
  773. return true;
  774. node = of_node_get(np);
  775. while (node) {
  776. if (of_property_read_bool(node, "dma-coherent")) {
  777. of_node_put(node);
  778. return true;
  779. }
  780. node = of_get_next_parent(node);
  781. }
  782. of_node_put(node);
  783. return false;
  784. }
  785. EXPORT_SYMBOL_GPL(of_dma_is_coherent);