address.c 23 KB

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