address.c 24 KB

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