address.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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/overflow.h>
  11. #include <linux/pci.h>
  12. #include <linux/pci_regs.h>
  13. #include <linux/sizes.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/dma-direct.h> /* for bus_dma_region */
  17. #include "of_private.h"
  18. /* Max address size we deal with */
  19. #define OF_MAX_ADDR_CELLS 4
  20. #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
  21. #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
  22. /* Debug utility */
  23. #ifdef DEBUG
  24. static void of_dump_addr(const char *s, const __be32 *addr, int na)
  25. {
  26. pr_debug("%s", s);
  27. while (na--)
  28. pr_cont(" %08x", be32_to_cpu(*(addr++)));
  29. pr_cont("\n");
  30. }
  31. #else
  32. static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
  33. #endif
  34. /* Callbacks for bus specific translators */
  35. struct of_bus {
  36. const char *name;
  37. const char *addresses;
  38. int (*match)(struct device_node *parent);
  39. void (*count_cells)(struct device_node *child,
  40. int *addrc, int *sizec);
  41. u64 (*map)(__be32 *addr, const __be32 *range,
  42. int na, int ns, int pna, int fna);
  43. int (*translate)(__be32 *addr, u64 offset, int na);
  44. int flag_cells;
  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, int fna)
  60. {
  61. u64 cp, s, da;
  62. cp = of_read_number(range + fna, na - fna);
  63. s = of_read_number(range + na + pna, ns);
  64. da = of_read_number(addr + fna, na - fna);
  65. pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, 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_flags_get_flags(const __be32 *addr)
  81. {
  82. return of_read_number(addr, 1);
  83. }
  84. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  85. {
  86. return IORESOURCE_MEM;
  87. }
  88. static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
  89. int ns, int pna, int fna)
  90. {
  91. /* Check that flags match */
  92. if (*addr != *range)
  93. return OF_BAD_ADDR;
  94. return of_bus_default_map(addr, range, na, ns, pna, fna);
  95. }
  96. static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
  97. {
  98. /* Keep "flags" part (high cell) in translated address */
  99. return of_bus_default_translate(addr + 1, offset, na - 1);
  100. }
  101. #ifdef CONFIG_PCI
  102. static unsigned int of_bus_pci_get_flags(const __be32 *addr)
  103. {
  104. unsigned int flags = 0;
  105. u32 w = be32_to_cpup(addr);
  106. if (!IS_ENABLED(CONFIG_PCI))
  107. return 0;
  108. switch((w >> 24) & 0x03) {
  109. case 0x01:
  110. flags |= IORESOURCE_IO;
  111. break;
  112. case 0x02: /* 32 bits */
  113. flags |= IORESOURCE_MEM;
  114. break;
  115. case 0x03: /* 64 bits */
  116. flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
  117. break;
  118. }
  119. if (w & 0x40000000)
  120. flags |= IORESOURCE_PREFETCH;
  121. return flags;
  122. }
  123. /*
  124. * PCI bus specific translator
  125. */
  126. static bool of_node_is_pcie(struct device_node *np)
  127. {
  128. bool is_pcie = of_node_name_eq(np, "pcie");
  129. if (is_pcie)
  130. pr_warn_once("%pOF: Missing device_type\n", np);
  131. return is_pcie;
  132. }
  133. static int of_bus_pci_match(struct device_node *np)
  134. {
  135. /*
  136. * "pciex" is PCI Express
  137. * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
  138. * "ht" is hypertransport
  139. *
  140. * If none of the device_type match, and that the node name is
  141. * "pcie", accept the device as PCI (with a warning).
  142. */
  143. return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
  144. of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
  145. of_node_is_pcie(np);
  146. }
  147. static void of_bus_pci_count_cells(struct device_node *np,
  148. int *addrc, int *sizec)
  149. {
  150. if (addrc)
  151. *addrc = 3;
  152. if (sizec)
  153. *sizec = 2;
  154. }
  155. static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
  156. int pna, int fna)
  157. {
  158. unsigned int af, rf;
  159. af = of_bus_pci_get_flags(addr);
  160. rf = of_bus_pci_get_flags(range);
  161. /* Check address type match */
  162. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  163. return OF_BAD_ADDR;
  164. return of_bus_default_map(addr, range, na, ns, pna, fna);
  165. }
  166. #endif /* CONFIG_PCI */
  167. static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
  168. {
  169. if (overflows_type(start, r->start))
  170. return -EOVERFLOW;
  171. r->start = start;
  172. if (!size)
  173. r->end = wrapping_sub(typeof(r->end), r->start, 1);
  174. else if (size && check_add_overflow(r->start, size - 1, &r->end))
  175. return -EOVERFLOW;
  176. return 0;
  177. }
  178. /*
  179. * of_pci_range_to_resource - Create a resource from an of_pci_range
  180. * @range: the PCI range that describes the resource
  181. * @np: device node where the range belongs to
  182. * @res: pointer to a valid resource that will be updated to
  183. * reflect the values contained in the range.
  184. *
  185. * Returns -EINVAL if the range cannot be converted to resource.
  186. *
  187. * Note that if the range is an IO range, the resource will be converted
  188. * using pci_address_to_pio() which can fail if it is called too early or
  189. * if the range cannot be matched to any host bridge IO space (our case here).
  190. * To guard against that we try to register the IO range first.
  191. * If that fails we know that pci_address_to_pio() will do too.
  192. */
  193. int of_pci_range_to_resource(struct of_pci_range *range,
  194. struct device_node *np, struct resource *res)
  195. {
  196. u64 start;
  197. int err;
  198. res->flags = range->flags;
  199. res->parent = res->child = res->sibling = NULL;
  200. res->name = np->full_name;
  201. if (res->flags & IORESOURCE_IO) {
  202. unsigned long port;
  203. err = pci_register_io_range(&np->fwnode, range->cpu_addr,
  204. range->size);
  205. if (err)
  206. goto invalid_range;
  207. port = pci_address_to_pio(range->cpu_addr);
  208. if (port == (unsigned long)-1) {
  209. err = -EINVAL;
  210. goto invalid_range;
  211. }
  212. start = port;
  213. } else {
  214. start = range->cpu_addr;
  215. }
  216. return __of_address_resource_bounds(res, start, range->size);
  217. invalid_range:
  218. res->start = (resource_size_t)OF_BAD_ADDR;
  219. res->end = (resource_size_t)OF_BAD_ADDR;
  220. return err;
  221. }
  222. EXPORT_SYMBOL(of_pci_range_to_resource);
  223. /*
  224. * of_range_to_resource - Create a resource from a ranges entry
  225. * @np: device node where the range belongs to
  226. * @index: the 'ranges' index to convert to a resource
  227. * @res: pointer to a valid resource that will be updated to
  228. * reflect the values contained in the range.
  229. *
  230. * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
  231. * cannot be converted to resource.
  232. */
  233. int of_range_to_resource(struct device_node *np, int index, struct resource *res)
  234. {
  235. int ret, i = 0;
  236. struct of_range_parser parser;
  237. struct of_range range;
  238. ret = of_range_parser_init(&parser, np);
  239. if (ret)
  240. return ret;
  241. for_each_of_range(&parser, &range)
  242. if (i++ == index)
  243. return of_pci_range_to_resource(&range, np, res);
  244. return -ENOENT;
  245. }
  246. EXPORT_SYMBOL(of_range_to_resource);
  247. /*
  248. * ISA bus specific translator
  249. */
  250. static int of_bus_isa_match(struct device_node *np)
  251. {
  252. return of_node_name_eq(np, "isa");
  253. }
  254. static void of_bus_isa_count_cells(struct device_node *child,
  255. int *addrc, int *sizec)
  256. {
  257. if (addrc)
  258. *addrc = 2;
  259. if (sizec)
  260. *sizec = 1;
  261. }
  262. static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
  263. int pna, int fna)
  264. {
  265. /* Check address type match */
  266. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  267. return OF_BAD_ADDR;
  268. return of_bus_default_map(addr, range, na, ns, pna, fna);
  269. }
  270. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  271. {
  272. unsigned int flags = 0;
  273. u32 w = be32_to_cpup(addr);
  274. if (w & 1)
  275. flags |= IORESOURCE_IO;
  276. else
  277. flags |= IORESOURCE_MEM;
  278. return flags;
  279. }
  280. static int of_bus_default_flags_match(struct device_node *np)
  281. {
  282. return of_bus_n_addr_cells(np) == 3;
  283. }
  284. /*
  285. * Array of bus specific translators
  286. */
  287. static struct of_bus of_busses[] = {
  288. #ifdef CONFIG_PCI
  289. /* PCI */
  290. {
  291. .name = "pci",
  292. .addresses = "assigned-addresses",
  293. .match = of_bus_pci_match,
  294. .count_cells = of_bus_pci_count_cells,
  295. .map = of_bus_pci_map,
  296. .translate = of_bus_default_flags_translate,
  297. .flag_cells = 1,
  298. .get_flags = of_bus_pci_get_flags,
  299. },
  300. #endif /* CONFIG_PCI */
  301. /* ISA */
  302. {
  303. .name = "isa",
  304. .addresses = "reg",
  305. .match = of_bus_isa_match,
  306. .count_cells = of_bus_isa_count_cells,
  307. .map = of_bus_isa_map,
  308. .translate = of_bus_default_flags_translate,
  309. .flag_cells = 1,
  310. .get_flags = of_bus_isa_get_flags,
  311. },
  312. /* Default with flags cell */
  313. {
  314. .name = "default-flags",
  315. .addresses = "reg",
  316. .match = of_bus_default_flags_match,
  317. .count_cells = of_bus_default_count_cells,
  318. .map = of_bus_default_flags_map,
  319. .translate = of_bus_default_flags_translate,
  320. .flag_cells = 1,
  321. .get_flags = of_bus_default_flags_get_flags,
  322. },
  323. /* Default */
  324. {
  325. .name = "default",
  326. .addresses = "reg",
  327. .match = NULL,
  328. .count_cells = of_bus_default_count_cells,
  329. .map = of_bus_default_map,
  330. .translate = of_bus_default_translate,
  331. .get_flags = of_bus_default_get_flags,
  332. },
  333. };
  334. static struct of_bus *of_match_bus(struct device_node *np)
  335. {
  336. int i;
  337. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  338. if (!of_busses[i].match || of_busses[i].match(np))
  339. return &of_busses[i];
  340. BUG();
  341. return NULL;
  342. }
  343. static int of_empty_ranges_quirk(struct device_node *np)
  344. {
  345. if (IS_ENABLED(CONFIG_PPC)) {
  346. /* To save cycles, we cache the result for global "Mac" setting */
  347. static int quirk_state = -1;
  348. /* PA-SEMI sdc DT bug */
  349. if (of_device_is_compatible(np, "1682m-sdc"))
  350. return true;
  351. /* Make quirk cached */
  352. if (quirk_state < 0)
  353. quirk_state =
  354. of_machine_is_compatible("Power Macintosh") ||
  355. of_machine_is_compatible("MacRISC");
  356. return quirk_state;
  357. }
  358. return false;
  359. }
  360. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  361. struct of_bus *pbus, __be32 *addr,
  362. int na, int ns, int pna, const char *rprop)
  363. {
  364. const __be32 *ranges;
  365. unsigned int rlen;
  366. int rone;
  367. u64 offset = OF_BAD_ADDR;
  368. /*
  369. * Normally, an absence of a "ranges" property means we are
  370. * crossing a non-translatable boundary, and thus the addresses
  371. * below the current cannot be converted to CPU physical ones.
  372. * Unfortunately, while this is very clear in the spec, it's not
  373. * what Apple understood, and they do have things like /uni-n or
  374. * /ht nodes with no "ranges" property and a lot of perfectly
  375. * useable mapped devices below them. Thus we treat the absence of
  376. * "ranges" as equivalent to an empty "ranges" property which means
  377. * a 1:1 translation at that level. It's up to the caller not to try
  378. * to translate addresses that aren't supposed to be translated in
  379. * the first place. --BenH.
  380. *
  381. * As far as we know, this damage only exists on Apple machines, so
  382. * This code is only enabled on powerpc. --gcl
  383. *
  384. * This quirk also applies for 'dma-ranges' which frequently exist in
  385. * child nodes without 'dma-ranges' in the parent nodes. --RobH
  386. */
  387. ranges = of_get_property(parent, rprop, &rlen);
  388. if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
  389. strcmp(rprop, "dma-ranges")) {
  390. pr_debug("no ranges; cannot translate\n");
  391. return 1;
  392. }
  393. if (ranges == NULL || rlen == 0) {
  394. offset = of_read_number(addr, na);
  395. /* set address to zero, pass flags through */
  396. memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
  397. pr_debug("empty ranges; 1:1 translation\n");
  398. goto finish;
  399. }
  400. pr_debug("walking ranges...\n");
  401. /* Now walk through the ranges */
  402. rlen /= 4;
  403. rone = na + pna + ns;
  404. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  405. offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
  406. if (offset != OF_BAD_ADDR)
  407. break;
  408. }
  409. if (offset == OF_BAD_ADDR) {
  410. pr_debug("not found !\n");
  411. return 1;
  412. }
  413. memcpy(addr, ranges + na, 4 * pna);
  414. finish:
  415. of_dump_addr("parent translation for:", addr, pna);
  416. pr_debug("with offset: %llx\n", offset);
  417. /* Translate it into parent bus space */
  418. return pbus->translate(addr, offset, pna);
  419. }
  420. /*
  421. * Translate an address from the device-tree into a CPU physical address,
  422. * this walks up the tree and applies the various bus mappings on the
  423. * way.
  424. *
  425. * Note: We consider that crossing any level with #size-cells == 0 to mean
  426. * that translation is impossible (that is we are not dealing with a value
  427. * that can be mapped to a cpu physical address). This is not really specified
  428. * that way, but this is traditionally the way IBM at least do things
  429. *
  430. * Whenever the translation fails, the *host pointer will be set to the
  431. * device that had registered logical PIO mapping, and the return code is
  432. * relative to that node.
  433. */
  434. static u64 __of_translate_address(struct device_node *node,
  435. struct device_node *(*get_parent)(const struct device_node *),
  436. const __be32 *in_addr, const char *rprop,
  437. struct device_node **host)
  438. {
  439. struct device_node *dev __free(device_node) = of_node_get(node);
  440. struct device_node *parent __free(device_node) = get_parent(dev);
  441. struct of_bus *bus, *pbus;
  442. __be32 addr[OF_MAX_ADDR_CELLS];
  443. int na, ns, pna, pns;
  444. pr_debug("** translation for device %pOF **\n", dev);
  445. *host = NULL;
  446. if (parent == NULL)
  447. return OF_BAD_ADDR;
  448. bus = of_match_bus(parent);
  449. /* Count address cells & copy address locally */
  450. bus->count_cells(dev, &na, &ns);
  451. if (!OF_CHECK_COUNTS(na, ns)) {
  452. pr_debug("Bad cell count for %pOF\n", dev);
  453. return OF_BAD_ADDR;
  454. }
  455. memcpy(addr, in_addr, na * 4);
  456. pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
  457. bus->name, na, ns, parent);
  458. of_dump_addr("translating address:", addr, na);
  459. /* Translate */
  460. for (;;) {
  461. struct logic_pio_hwaddr *iorange;
  462. /* Switch to parent bus */
  463. of_node_put(dev);
  464. dev = parent;
  465. parent = get_parent(dev);
  466. /* If root, we have finished */
  467. if (parent == NULL) {
  468. pr_debug("reached root node\n");
  469. return of_read_number(addr, na);
  470. }
  471. /*
  472. * For indirectIO device which has no ranges property, get
  473. * the address from reg directly.
  474. */
  475. iorange = find_io_range_by_fwnode(&dev->fwnode);
  476. if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
  477. u64 result = of_read_number(addr + 1, na - 1);
  478. pr_debug("indirectIO matched(%pOF) 0x%llx\n",
  479. dev, result);
  480. *host = no_free_ptr(dev);
  481. return result;
  482. }
  483. /* Get new parent bus and counts */
  484. pbus = of_match_bus(parent);
  485. pbus->count_cells(dev, &pna, &pns);
  486. if (!OF_CHECK_COUNTS(pna, pns)) {
  487. pr_err("Bad cell count for %pOF\n", dev);
  488. return OF_BAD_ADDR;
  489. }
  490. pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
  491. pbus->name, pna, pns, parent);
  492. /* Apply bus translation */
  493. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  494. return OF_BAD_ADDR;
  495. /* Complete the move up one level */
  496. na = pna;
  497. ns = pns;
  498. bus = pbus;
  499. of_dump_addr("one level translation:", addr, na);
  500. }
  501. unreachable();
  502. }
  503. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  504. {
  505. struct device_node *host;
  506. u64 ret;
  507. ret = __of_translate_address(dev, of_get_parent,
  508. in_addr, "ranges", &host);
  509. if (host) {
  510. of_node_put(host);
  511. return OF_BAD_ADDR;
  512. }
  513. return ret;
  514. }
  515. EXPORT_SYMBOL(of_translate_address);
  516. #ifdef CONFIG_HAS_DMA
  517. struct device_node *__of_get_dma_parent(const struct device_node *np)
  518. {
  519. struct of_phandle_args args;
  520. int ret, index;
  521. index = of_property_match_string(np, "interconnect-names", "dma-mem");
  522. if (index < 0)
  523. return of_get_parent(np);
  524. ret = of_parse_phandle_with_args(np, "interconnects",
  525. "#interconnect-cells",
  526. index, &args);
  527. if (ret < 0)
  528. return of_get_parent(np);
  529. return args.np;
  530. }
  531. #endif
  532. static struct device_node *of_get_next_dma_parent(struct device_node *np)
  533. {
  534. struct device_node *parent;
  535. parent = __of_get_dma_parent(np);
  536. of_node_put(np);
  537. return parent;
  538. }
  539. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  540. {
  541. struct device_node *host;
  542. u64 ret;
  543. ret = __of_translate_address(dev, __of_get_dma_parent,
  544. in_addr, "dma-ranges", &host);
  545. if (host) {
  546. of_node_put(host);
  547. return OF_BAD_ADDR;
  548. }
  549. return ret;
  550. }
  551. EXPORT_SYMBOL(of_translate_dma_address);
  552. /**
  553. * of_translate_dma_region - Translate device tree address and size tuple
  554. * @dev: device tree node for which to translate
  555. * @prop: pointer into array of cells
  556. * @start: return value for the start of the DMA range
  557. * @length: return value for the length of the DMA range
  558. *
  559. * Returns a pointer to the cell immediately following the translated DMA region.
  560. */
  561. const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
  562. phys_addr_t *start, size_t *length)
  563. {
  564. struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
  565. u64 address, size;
  566. int na, ns;
  567. if (!parent)
  568. return NULL;
  569. na = of_bus_n_addr_cells(parent);
  570. ns = of_bus_n_size_cells(parent);
  571. address = of_translate_dma_address(dev, prop);
  572. if (address == OF_BAD_ADDR)
  573. return NULL;
  574. size = of_read_number(prop + na, ns);
  575. if (start)
  576. *start = address;
  577. if (length)
  578. *length = size;
  579. return prop + na + ns;
  580. }
  581. EXPORT_SYMBOL(of_translate_dma_region);
  582. const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
  583. u64 *size, unsigned int *flags)
  584. {
  585. const __be32 *prop;
  586. unsigned int psize;
  587. struct device_node *parent __free(device_node) = of_get_parent(dev);
  588. struct of_bus *bus;
  589. int onesize, i, na, ns;
  590. if (parent == NULL)
  591. return NULL;
  592. /* match the parent's bus type */
  593. bus = of_match_bus(parent);
  594. if (strcmp(bus->name, "pci") && (bar_no >= 0))
  595. return NULL;
  596. bus->count_cells(dev, &na, &ns);
  597. if (!OF_CHECK_ADDR_COUNT(na))
  598. return NULL;
  599. /* Get "reg" or "assigned-addresses" property */
  600. prop = of_get_property(dev, bus->addresses, &psize);
  601. if (prop == NULL)
  602. return NULL;
  603. psize /= 4;
  604. onesize = na + ns;
  605. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  606. u32 val = be32_to_cpu(prop[0]);
  607. /* PCI bus matches on BAR number instead of index */
  608. if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
  609. ((index >= 0) && (i == index))) {
  610. if (size)
  611. *size = of_read_number(prop + na, ns);
  612. if (flags)
  613. *flags = bus->get_flags(prop);
  614. return prop;
  615. }
  616. }
  617. return NULL;
  618. }
  619. EXPORT_SYMBOL(__of_get_address);
  620. /**
  621. * of_property_read_reg - Retrieve the specified "reg" entry index without translating
  622. * @np: device tree node for which to retrieve "reg" from
  623. * @idx: "reg" entry index to read
  624. * @addr: return value for the untranslated address
  625. * @size: return value for the entry size
  626. *
  627. * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
  628. * size values filled in.
  629. */
  630. int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
  631. {
  632. const __be32 *prop = of_get_address(np, idx, size, NULL);
  633. if (!prop)
  634. return -EINVAL;
  635. *addr = of_read_number(prop, of_n_addr_cells(np));
  636. return 0;
  637. }
  638. EXPORT_SYMBOL(of_property_read_reg);
  639. static int parser_init(struct of_pci_range_parser *parser,
  640. struct device_node *node, const char *name)
  641. {
  642. int rlen;
  643. parser->node = node;
  644. parser->pna = of_n_addr_cells(node);
  645. parser->na = of_bus_n_addr_cells(node);
  646. parser->ns = of_bus_n_size_cells(node);
  647. parser->dma = !strcmp(name, "dma-ranges");
  648. parser->bus = of_match_bus(node);
  649. parser->range = of_get_property(node, name, &rlen);
  650. if (parser->range == NULL)
  651. return -ENOENT;
  652. parser->end = parser->range + rlen / sizeof(__be32);
  653. return 0;
  654. }
  655. int of_pci_range_parser_init(struct of_pci_range_parser *parser,
  656. struct device_node *node)
  657. {
  658. return parser_init(parser, node, "ranges");
  659. }
  660. EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
  661. int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  662. struct device_node *node)
  663. {
  664. return parser_init(parser, node, "dma-ranges");
  665. }
  666. EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
  667. #define of_dma_range_parser_init of_pci_dma_range_parser_init
  668. struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
  669. struct of_pci_range *range)
  670. {
  671. int na = parser->na;
  672. int ns = parser->ns;
  673. int np = parser->pna + na + ns;
  674. int busflag_na = parser->bus->flag_cells;
  675. if (!range)
  676. return NULL;
  677. if (!parser->range || parser->range + np > parser->end)
  678. return NULL;
  679. range->flags = parser->bus->get_flags(parser->range);
  680. range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
  681. if (parser->dma)
  682. range->cpu_addr = of_translate_dma_address(parser->node,
  683. parser->range + na);
  684. else
  685. range->cpu_addr = of_translate_address(parser->node,
  686. parser->range + na);
  687. range->size = of_read_number(parser->range + parser->pna + na, ns);
  688. parser->range += np;
  689. /* Now consume following elements while they are contiguous */
  690. while (parser->range + np <= parser->end) {
  691. u32 flags = 0;
  692. u64 bus_addr, cpu_addr, size;
  693. flags = parser->bus->get_flags(parser->range);
  694. bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
  695. if (parser->dma)
  696. cpu_addr = of_translate_dma_address(parser->node,
  697. parser->range + na);
  698. else
  699. cpu_addr = of_translate_address(parser->node,
  700. parser->range + na);
  701. size = of_read_number(parser->range + parser->pna + na, ns);
  702. if (flags != range->flags)
  703. break;
  704. if (bus_addr != range->bus_addr + range->size ||
  705. cpu_addr != range->cpu_addr + range->size)
  706. break;
  707. range->size += size;
  708. parser->range += np;
  709. }
  710. return range;
  711. }
  712. EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
  713. static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
  714. u64 size)
  715. {
  716. u64 taddr;
  717. unsigned long port;
  718. struct device_node *host;
  719. taddr = __of_translate_address(dev, of_get_parent,
  720. in_addr, "ranges", &host);
  721. if (host) {
  722. /* host-specific port access */
  723. port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
  724. of_node_put(host);
  725. } else {
  726. /* memory-mapped I/O range */
  727. port = pci_address_to_pio(taddr);
  728. }
  729. if (port == (unsigned long)-1)
  730. return OF_BAD_ADDR;
  731. return port;
  732. }
  733. #ifdef CONFIG_HAS_DMA
  734. /**
  735. * of_dma_get_range - Get DMA range info and put it into a map array
  736. * @np: device node to get DMA range info
  737. * @map: dma range structure to return
  738. *
  739. * Look in bottom up direction for the first "dma-ranges" property
  740. * and parse it. Put the information into a DMA offset map array.
  741. *
  742. * dma-ranges format:
  743. * DMA addr (dma_addr) : naddr cells
  744. * CPU addr (phys_addr_t) : pna cells
  745. * size : nsize cells
  746. *
  747. * It returns -ENODEV if "dma-ranges" property was not found for this
  748. * device in the DT.
  749. */
  750. int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
  751. {
  752. struct device_node *node __free(device_node) = of_node_get(np);
  753. const __be32 *ranges = NULL;
  754. bool found_dma_ranges = false;
  755. struct of_range_parser parser;
  756. struct of_range range;
  757. struct bus_dma_region *r;
  758. int len, num_ranges = 0;
  759. while (node) {
  760. ranges = of_get_property(node, "dma-ranges", &len);
  761. /* Ignore empty ranges, they imply no translation required */
  762. if (ranges && len > 0)
  763. break;
  764. /* Once we find 'dma-ranges', then a missing one is an error */
  765. if (found_dma_ranges && !ranges)
  766. return -ENODEV;
  767. found_dma_ranges = true;
  768. node = of_get_next_dma_parent(node);
  769. }
  770. if (!node || !ranges) {
  771. pr_debug("no dma-ranges found for node(%pOF)\n", np);
  772. return -ENODEV;
  773. }
  774. of_dma_range_parser_init(&parser, node);
  775. for_each_of_range(&parser, &range) {
  776. if (range.cpu_addr == OF_BAD_ADDR) {
  777. pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
  778. range.bus_addr, node);
  779. continue;
  780. }
  781. num_ranges++;
  782. }
  783. if (!num_ranges)
  784. return -EINVAL;
  785. r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
  786. if (!r)
  787. return -ENOMEM;
  788. /*
  789. * Record all info in the generic DMA ranges array for struct device,
  790. * returning an error if we don't find any parsable ranges.
  791. */
  792. *map = r;
  793. of_dma_range_parser_init(&parser, node);
  794. for_each_of_range(&parser, &range) {
  795. pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
  796. range.bus_addr, range.cpu_addr, range.size);
  797. if (range.cpu_addr == OF_BAD_ADDR)
  798. continue;
  799. r->cpu_start = range.cpu_addr;
  800. r->dma_start = range.bus_addr;
  801. r->size = range.size;
  802. r++;
  803. }
  804. return 0;
  805. }
  806. #endif /* CONFIG_HAS_DMA */
  807. /**
  808. * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
  809. * @np: The node to start searching from or NULL to start from the root
  810. *
  811. * Gets the highest CPU physical address that is addressable by all DMA masters
  812. * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
  813. * DMA constrained device is found, it returns PHYS_ADDR_MAX.
  814. */
  815. phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
  816. {
  817. phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
  818. struct of_range_parser parser;
  819. phys_addr_t subtree_max_addr;
  820. struct device_node *child;
  821. struct of_range range;
  822. const __be32 *ranges;
  823. u64 cpu_end = 0;
  824. int len;
  825. if (!np)
  826. np = of_root;
  827. ranges = of_get_property(np, "dma-ranges", &len);
  828. if (ranges && len) {
  829. of_dma_range_parser_init(&parser, np);
  830. for_each_of_range(&parser, &range)
  831. if (range.cpu_addr + range.size > cpu_end)
  832. cpu_end = range.cpu_addr + range.size - 1;
  833. if (max_cpu_addr > cpu_end)
  834. max_cpu_addr = cpu_end;
  835. }
  836. for_each_available_child_of_node(np, child) {
  837. subtree_max_addr = of_dma_get_max_cpu_address(child);
  838. if (max_cpu_addr > subtree_max_addr)
  839. max_cpu_addr = subtree_max_addr;
  840. }
  841. return max_cpu_addr;
  842. }
  843. /**
  844. * of_dma_is_coherent - Check if device is coherent
  845. * @np: device node
  846. *
  847. * It returns true if "dma-coherent" property was found
  848. * for this device in the DT, or if DMA is coherent by
  849. * default for OF devices on the current platform and no
  850. * "dma-noncoherent" property was found for this device.
  851. */
  852. bool of_dma_is_coherent(struct device_node *np)
  853. {
  854. struct device_node *node __free(device_node) = of_node_get(np);
  855. while (node) {
  856. if (of_property_read_bool(node, "dma-coherent"))
  857. return true;
  858. if (of_property_read_bool(node, "dma-noncoherent"))
  859. return false;
  860. node = of_get_next_dma_parent(node);
  861. }
  862. return dma_default_coherent;
  863. }
  864. EXPORT_SYMBOL_GPL(of_dma_is_coherent);
  865. /**
  866. * of_mmio_is_nonposted - Check if device uses non-posted MMIO
  867. * @np: device node
  868. *
  869. * Returns true if the "nonposted-mmio" property was found for
  870. * the device's bus.
  871. *
  872. * This is currently only enabled on builds that support Apple ARM devices, as
  873. * an optimization.
  874. */
  875. static bool of_mmio_is_nonposted(struct device_node *np)
  876. {
  877. if (!IS_ENABLED(CONFIG_ARCH_APPLE))
  878. return false;
  879. struct device_node *parent __free(device_node) = of_get_parent(np);
  880. if (!parent)
  881. return false;
  882. return of_property_read_bool(parent, "nonposted-mmio");
  883. }
  884. static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
  885. struct resource *r)
  886. {
  887. u64 taddr;
  888. const __be32 *addrp;
  889. u64 size;
  890. unsigned int flags;
  891. const char *name = NULL;
  892. addrp = __of_get_address(dev, index, bar_no, &size, &flags);
  893. if (addrp == NULL)
  894. return -EINVAL;
  895. /* Get optional "reg-names" property to add a name to a resource */
  896. if (index >= 0)
  897. of_property_read_string_index(dev, "reg-names", index, &name);
  898. if (flags & IORESOURCE_MEM)
  899. taddr = of_translate_address(dev, addrp);
  900. else if (flags & IORESOURCE_IO)
  901. taddr = of_translate_ioport(dev, addrp, size);
  902. else
  903. return -EINVAL;
  904. if (taddr == OF_BAD_ADDR)
  905. return -EINVAL;
  906. memset(r, 0, sizeof(struct resource));
  907. if (of_mmio_is_nonposted(dev))
  908. flags |= IORESOURCE_MEM_NONPOSTED;
  909. r->flags = flags;
  910. r->name = name ? name : dev->full_name;
  911. return __of_address_resource_bounds(r, taddr, size);
  912. }
  913. /**
  914. * of_address_to_resource - Translate device tree address and return as resource
  915. * @dev: Caller's Device Node
  916. * @index: Index into the array
  917. * @r: Pointer to resource array
  918. *
  919. * Returns -EINVAL if the range cannot be converted to resource.
  920. *
  921. * Note that if your address is a PIO address, the conversion will fail if
  922. * the physical address can't be internally converted to an IO token with
  923. * pci_address_to_pio(), that is because it's either called too early or it
  924. * can't be matched to any host bridge IO space
  925. */
  926. int of_address_to_resource(struct device_node *dev, int index,
  927. struct resource *r)
  928. {
  929. return __of_address_to_resource(dev, index, -1, r);
  930. }
  931. EXPORT_SYMBOL_GPL(of_address_to_resource);
  932. int of_pci_address_to_resource(struct device_node *dev, int bar,
  933. struct resource *r)
  934. {
  935. if (!IS_ENABLED(CONFIG_PCI))
  936. return -ENOSYS;
  937. return __of_address_to_resource(dev, -1, bar, r);
  938. }
  939. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  940. /**
  941. * of_iomap - Maps the memory mapped IO for a given device_node
  942. * @np: the device whose io range will be mapped
  943. * @index: index of the io range
  944. *
  945. * Returns a pointer to the mapped memory
  946. */
  947. void __iomem *of_iomap(struct device_node *np, int index)
  948. {
  949. struct resource res;
  950. if (of_address_to_resource(np, index, &res))
  951. return NULL;
  952. if (res.flags & IORESOURCE_MEM_NONPOSTED)
  953. return ioremap_np(res.start, resource_size(&res));
  954. else
  955. return ioremap(res.start, resource_size(&res));
  956. }
  957. EXPORT_SYMBOL(of_iomap);
  958. /*
  959. * of_io_request_and_map - Requests a resource and maps the memory mapped IO
  960. * for a given device_node
  961. * @device: the device whose io range will be mapped
  962. * @index: index of the io range
  963. * @name: name "override" for the memory region request or NULL
  964. *
  965. * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
  966. * error code on failure. Usage example:
  967. *
  968. * base = of_io_request_and_map(node, 0, "foo");
  969. * if (IS_ERR(base))
  970. * return PTR_ERR(base);
  971. */
  972. void __iomem *of_io_request_and_map(struct device_node *np, int index,
  973. const char *name)
  974. {
  975. struct resource res;
  976. void __iomem *mem;
  977. if (of_address_to_resource(np, index, &res))
  978. return IOMEM_ERR_PTR(-EINVAL);
  979. if (!name)
  980. name = res.name;
  981. if (!request_mem_region(res.start, resource_size(&res), name))
  982. return IOMEM_ERR_PTR(-EBUSY);
  983. if (res.flags & IORESOURCE_MEM_NONPOSTED)
  984. mem = ioremap_np(res.start, resource_size(&res));
  985. else
  986. mem = ioremap(res.start, resource_size(&res));
  987. if (!mem) {
  988. release_mem_region(res.start, resource_size(&res));
  989. return IOMEM_ERR_PTR(-ENOMEM);
  990. }
  991. return mem;
  992. }
  993. EXPORT_SYMBOL(of_io_request_and_map);