address.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 <kunit/visibility.h>
  18. /* Uncomment me to enable of_dump_addr() debugging output */
  19. // #define DEBUG
  20. #include "of_private.h"
  21. /* Callbacks for bus specific translators */
  22. struct of_bus {
  23. const char *name;
  24. const char *addresses;
  25. int (*match)(struct device_node *parent);
  26. void (*count_cells)(struct device_node *child,
  27. int *addrc, int *sizec);
  28. u64 (*map)(__be32 *addr, const __be32 *range,
  29. int na, int ns, int pna, int fna);
  30. int (*translate)(__be32 *addr, u64 offset, int na);
  31. int flag_cells;
  32. unsigned int (*get_flags)(const __be32 *addr);
  33. };
  34. /*
  35. * Default translator (generic bus)
  36. */
  37. static void of_bus_default_count_cells(struct device_node *dev,
  38. int *addrc, int *sizec)
  39. {
  40. if (addrc)
  41. *addrc = of_n_addr_cells(dev);
  42. if (sizec)
  43. *sizec = of_n_size_cells(dev);
  44. }
  45. static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
  46. int na, int ns, int pna, int fna)
  47. {
  48. u64 cp, s, da;
  49. cp = of_read_number(range + fna, na - fna);
  50. s = of_read_number(range + na + pna, ns);
  51. da = of_read_number(addr + fna, na - fna);
  52. pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
  53. if (da < cp || da >= (cp + s))
  54. return OF_BAD_ADDR;
  55. return da - cp;
  56. }
  57. static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
  58. {
  59. u64 a = of_read_number(addr, na);
  60. memset(addr, 0, na * 4);
  61. a += offset;
  62. if (na > 1)
  63. addr[na - 2] = cpu_to_be32(a >> 32);
  64. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  65. return 0;
  66. }
  67. static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
  68. {
  69. return of_read_number(addr, 1);
  70. }
  71. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  72. {
  73. return IORESOURCE_MEM;
  74. }
  75. static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
  76. int ns, int pna, int fna)
  77. {
  78. /* Check that flags match */
  79. if (*addr != *range)
  80. return OF_BAD_ADDR;
  81. return of_bus_default_map(addr, range, na, ns, pna, fna);
  82. }
  83. static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
  84. {
  85. /* Keep "flags" part (high cell) in translated address */
  86. return of_bus_default_translate(addr + 1, offset, na - 1);
  87. }
  88. #ifdef CONFIG_PCI
  89. static unsigned int of_bus_pci_get_flags(const __be32 *addr)
  90. {
  91. unsigned int flags = 0;
  92. u32 w = be32_to_cpup(addr);
  93. if (!IS_ENABLED(CONFIG_PCI))
  94. return 0;
  95. switch((w >> 24) & 0x03) {
  96. case 0x01:
  97. flags |= IORESOURCE_IO;
  98. break;
  99. case 0x02: /* 32 bits */
  100. flags |= IORESOURCE_MEM;
  101. break;
  102. case 0x03: /* 64 bits */
  103. flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
  104. break;
  105. }
  106. if (w & 0x40000000)
  107. flags |= IORESOURCE_PREFETCH;
  108. return flags;
  109. }
  110. /*
  111. * PCI bus specific translator
  112. */
  113. static bool of_node_is_pcie(const struct device_node *np)
  114. {
  115. bool is_pcie = of_node_name_eq(np, "pcie");
  116. if (is_pcie)
  117. pr_warn_once("%pOF: Missing device_type\n", np);
  118. return is_pcie;
  119. }
  120. static int of_bus_pci_match(struct device_node *np)
  121. {
  122. /*
  123. * "pciex" is PCI Express
  124. * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
  125. * "ht" is hypertransport
  126. *
  127. * If none of the device_type match, and that the node name is
  128. * "pcie", accept the device as PCI (with a warning).
  129. */
  130. return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
  131. of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
  132. of_node_is_pcie(np);
  133. }
  134. static void of_bus_pci_count_cells(struct device_node *np,
  135. int *addrc, int *sizec)
  136. {
  137. if (addrc)
  138. *addrc = 3;
  139. if (sizec)
  140. *sizec = 2;
  141. }
  142. static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
  143. int pna, int fna)
  144. {
  145. unsigned int af, rf;
  146. af = of_bus_pci_get_flags(addr);
  147. rf = of_bus_pci_get_flags(range);
  148. /* Check address type match */
  149. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  150. return OF_BAD_ADDR;
  151. return of_bus_default_map(addr, range, na, ns, pna, fna);
  152. }
  153. #endif /* CONFIG_PCI */
  154. VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
  155. {
  156. if (overflows_type(start, r->start))
  157. return -EOVERFLOW;
  158. r->start = start;
  159. if (!size)
  160. r->end = wrapping_sub(typeof(r->end), r->start, 1);
  161. else if (size && check_add_overflow(r->start, size - 1, &r->end))
  162. return -EOVERFLOW;
  163. return 0;
  164. }
  165. EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds);
  166. /*
  167. * of_pci_range_to_resource - Create a resource from an of_pci_range
  168. * @range: the PCI range that describes the resource
  169. * @np: device node where the range belongs to
  170. * @res: pointer to a valid resource that will be updated to
  171. * reflect the values contained in the range.
  172. *
  173. * Returns -EINVAL if the range cannot be converted to resource.
  174. *
  175. * Note that if the range is an IO range, the resource will be converted
  176. * using pci_address_to_pio() which can fail if it is called too early or
  177. * if the range cannot be matched to any host bridge IO space (our case here).
  178. * To guard against that we try to register the IO range first.
  179. * If that fails we know that pci_address_to_pio() will do too.
  180. */
  181. int of_pci_range_to_resource(const struct of_pci_range *range,
  182. const struct device_node *np, struct resource *res)
  183. {
  184. u64 start;
  185. int err;
  186. res->flags = range->flags;
  187. res->parent = res->child = res->sibling = NULL;
  188. res->name = np->full_name;
  189. if (res->flags & IORESOURCE_IO) {
  190. unsigned long port;
  191. err = pci_register_io_range(&np->fwnode, range->cpu_addr,
  192. range->size);
  193. if (err)
  194. goto invalid_range;
  195. port = pci_address_to_pio(range->cpu_addr);
  196. if (port == (unsigned long)-1) {
  197. err = -EINVAL;
  198. goto invalid_range;
  199. }
  200. start = port;
  201. } else {
  202. start = range->cpu_addr;
  203. }
  204. return __of_address_resource_bounds(res, start, range->size);
  205. invalid_range:
  206. res->start = (resource_size_t)OF_BAD_ADDR;
  207. res->end = (resource_size_t)OF_BAD_ADDR;
  208. return err;
  209. }
  210. EXPORT_SYMBOL(of_pci_range_to_resource);
  211. /*
  212. * of_range_to_resource - Create a resource from a ranges entry
  213. * @np: device node where the range belongs to
  214. * @index: the 'ranges' index to convert to a resource
  215. * @res: pointer to a valid resource that will be updated to
  216. * reflect the values contained in the range.
  217. *
  218. * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
  219. * cannot be converted to resource.
  220. */
  221. int of_range_to_resource(struct device_node *np, int index, struct resource *res)
  222. {
  223. int ret, i = 0;
  224. struct of_range_parser parser;
  225. struct of_range range;
  226. ret = of_range_parser_init(&parser, np);
  227. if (ret)
  228. return ret;
  229. for_each_of_range(&parser, &range)
  230. if (i++ == index)
  231. return of_pci_range_to_resource(&range, np, res);
  232. return -ENOENT;
  233. }
  234. EXPORT_SYMBOL(of_range_to_resource);
  235. /*
  236. * ISA bus specific translator
  237. */
  238. static int of_bus_isa_match(struct device_node *np)
  239. {
  240. return of_node_name_eq(np, "isa");
  241. }
  242. static void of_bus_isa_count_cells(struct device_node *child,
  243. int *addrc, int *sizec)
  244. {
  245. if (addrc)
  246. *addrc = 2;
  247. if (sizec)
  248. *sizec = 1;
  249. }
  250. static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
  251. int pna, int fna)
  252. {
  253. /* Check address type match */
  254. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  255. return OF_BAD_ADDR;
  256. return of_bus_default_map(addr, range, na, ns, pna, fna);
  257. }
  258. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  259. {
  260. unsigned int flags = 0;
  261. u32 w = be32_to_cpup(addr);
  262. if (w & 1)
  263. flags |= IORESOURCE_IO;
  264. else
  265. flags |= IORESOURCE_MEM;
  266. return flags;
  267. }
  268. static int of_bus_default_flags_match(struct device_node *np)
  269. {
  270. /*
  271. * Check for presence first since of_bus_n_addr_cells() will warn when
  272. * walking parent nodes.
  273. */
  274. return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);
  275. }
  276. static int of_bus_default_match(struct device_node *np)
  277. {
  278. return of_property_present(np, "#address-cells");
  279. }
  280. /*
  281. * Array of bus specific translators
  282. */
  283. static const struct of_bus of_busses[] = {
  284. #ifdef CONFIG_PCI
  285. /* PCI */
  286. {
  287. .name = "pci",
  288. .addresses = "assigned-addresses",
  289. .match = of_bus_pci_match,
  290. .count_cells = of_bus_pci_count_cells,
  291. .map = of_bus_pci_map,
  292. .translate = of_bus_default_flags_translate,
  293. .flag_cells = 1,
  294. .get_flags = of_bus_pci_get_flags,
  295. },
  296. #endif /* CONFIG_PCI */
  297. /* ISA */
  298. {
  299. .name = "isa",
  300. .addresses = "reg",
  301. .match = of_bus_isa_match,
  302. .count_cells = of_bus_isa_count_cells,
  303. .map = of_bus_isa_map,
  304. .translate = of_bus_default_flags_translate,
  305. .flag_cells = 1,
  306. .get_flags = of_bus_isa_get_flags,
  307. },
  308. /* Default with flags cell */
  309. {
  310. .name = "default-flags",
  311. .addresses = "reg",
  312. .match = of_bus_default_flags_match,
  313. .count_cells = of_bus_default_count_cells,
  314. .map = of_bus_default_flags_map,
  315. .translate = of_bus_default_flags_translate,
  316. .flag_cells = 1,
  317. .get_flags = of_bus_default_flags_get_flags,
  318. },
  319. /* Default */
  320. {
  321. .name = "default",
  322. .addresses = "reg",
  323. .match = of_bus_default_match,
  324. .count_cells = of_bus_default_count_cells,
  325. .map = of_bus_default_map,
  326. .translate = of_bus_default_translate,
  327. .get_flags = of_bus_default_get_flags,
  328. },
  329. };
  330. static const struct of_bus *of_match_bus(struct device_node *np)
  331. {
  332. int i;
  333. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  334. if (!of_busses[i].match || of_busses[i].match(np))
  335. return &of_busses[i];
  336. return NULL;
  337. }
  338. static int of_empty_ranges_quirk(const struct device_node *np)
  339. {
  340. if (IS_ENABLED(CONFIG_PPC)) {
  341. /* To save cycles, we cache the result for global "Mac" setting */
  342. static int quirk_state = -1;
  343. /* PA-SEMI sdc DT bug */
  344. if (of_device_is_compatible(np, "1682m-sdc"))
  345. return true;
  346. /* Make quirk cached */
  347. if (quirk_state < 0)
  348. quirk_state =
  349. of_machine_is_compatible("Power Macintosh") ||
  350. of_machine_is_compatible("MacRISC");
  351. return quirk_state;
  352. }
  353. return false;
  354. }
  355. static int of_translate_one(const struct device_node *parent, const struct of_bus *bus,
  356. const struct of_bus *pbus, __be32 *addr,
  357. int na, int ns, int pna, const char *rprop)
  358. {
  359. const __be32 *ranges;
  360. unsigned int rlen;
  361. int rone;
  362. u64 offset = OF_BAD_ADDR;
  363. /*
  364. * Normally, an absence of a "ranges" property means we are
  365. * crossing a non-translatable boundary, and thus the addresses
  366. * below the current cannot be converted to CPU physical ones.
  367. * Unfortunately, while this is very clear in the spec, it's not
  368. * what Apple understood, and they do have things like /uni-n or
  369. * /ht nodes with no "ranges" property and a lot of perfectly
  370. * useable mapped devices below them. Thus we treat the absence of
  371. * "ranges" as equivalent to an empty "ranges" property which means
  372. * a 1:1 translation at that level. It's up to the caller not to try
  373. * to translate addresses that aren't supposed to be translated in
  374. * the first place. --BenH.
  375. *
  376. * As far as we know, this damage only exists on Apple machines, so
  377. * This code is only enabled on powerpc. --gcl
  378. *
  379. * This quirk also applies for 'dma-ranges' which frequently exist in
  380. * child nodes without 'dma-ranges' in the parent nodes. --RobH
  381. */
  382. ranges = of_get_property(parent, rprop, &rlen);
  383. if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
  384. strcmp(rprop, "dma-ranges")) {
  385. pr_debug("no ranges; cannot translate\n");
  386. return 1;
  387. }
  388. if (ranges == NULL || rlen == 0) {
  389. offset = of_read_number(addr, na);
  390. /* set address to zero, pass flags through */
  391. memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
  392. pr_debug("empty ranges; 1:1 translation\n");
  393. goto finish;
  394. }
  395. pr_debug("walking ranges...\n");
  396. /* Now walk through the ranges */
  397. rlen /= 4;
  398. rone = na + pna + ns;
  399. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  400. offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
  401. if (offset != OF_BAD_ADDR)
  402. break;
  403. }
  404. if (offset == OF_BAD_ADDR) {
  405. pr_debug("not found !\n");
  406. return 1;
  407. }
  408. memcpy(addr, ranges + na, 4 * pna);
  409. finish:
  410. of_dump_addr("parent translation for:", addr, pna);
  411. pr_debug("with offset: %llx\n", offset);
  412. /* Translate it into parent bus space */
  413. return pbus->translate(addr, offset, pna);
  414. }
  415. /*
  416. * Translate an address from the device-tree into a CPU physical address,
  417. * this walks up the tree and applies the various bus mappings on the
  418. * way.
  419. *
  420. * Note: We consider that crossing any level with #size-cells == 0 to mean
  421. * that translation is impossible (that is we are not dealing with a value
  422. * that can be mapped to a cpu physical address). This is not really specified
  423. * that way, but this is traditionally the way IBM at least do things
  424. *
  425. * Whenever the translation fails, the *host pointer will be set to the
  426. * device that had registered logical PIO mapping, and the return code is
  427. * relative to that node.
  428. */
  429. static u64 __of_translate_address(struct device_node *node,
  430. struct device_node *(*get_parent)(const struct device_node *),
  431. const __be32 *in_addr, const char *rprop,
  432. struct device_node **host)
  433. {
  434. struct device_node *dev __free(device_node) = of_node_get(node);
  435. struct device_node *parent __free(device_node) = get_parent(dev);
  436. const struct of_bus *bus, *pbus;
  437. __be32 addr[OF_MAX_ADDR_CELLS];
  438. int na, ns, pna, pns;
  439. pr_debug("** translation for device %pOF **\n", dev);
  440. *host = NULL;
  441. if (parent == NULL)
  442. return OF_BAD_ADDR;
  443. bus = of_match_bus(parent);
  444. if (!bus)
  445. return OF_BAD_ADDR;
  446. /* Count address cells & copy address locally */
  447. bus->count_cells(dev, &na, &ns);
  448. if (!OF_CHECK_COUNTS(na, ns)) {
  449. pr_debug("Bad cell count for %pOF\n", dev);
  450. return OF_BAD_ADDR;
  451. }
  452. memcpy(addr, in_addr, na * 4);
  453. pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
  454. bus->name, na, ns, parent);
  455. of_dump_addr("translating address:", addr, na);
  456. /* Translate */
  457. for (;;) {
  458. struct logic_pio_hwaddr *iorange;
  459. /* Switch to parent bus */
  460. of_node_put(dev);
  461. dev = parent;
  462. parent = get_parent(dev);
  463. /* If root, we have finished */
  464. if (parent == NULL) {
  465. pr_debug("reached root node\n");
  466. return of_read_number(addr, na);
  467. }
  468. /*
  469. * For indirectIO device which has no ranges property, get
  470. * the address from reg directly.
  471. */
  472. iorange = find_io_range_by_fwnode(&dev->fwnode);
  473. if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
  474. u64 result = of_read_number(addr + 1, na - 1);
  475. pr_debug("indirectIO matched(%pOF) 0x%llx\n",
  476. dev, result);
  477. *host = no_free_ptr(dev);
  478. return result;
  479. }
  480. /* Get new parent bus and counts */
  481. pbus = of_match_bus(parent);
  482. if (!pbus)
  483. return OF_BAD_ADDR;
  484. pbus->count_cells(dev, &pna, &pns);
  485. if (!OF_CHECK_COUNTS(pna, pns)) {
  486. pr_err("Bad cell count for %pOF\n", dev);
  487. return OF_BAD_ADDR;
  488. }
  489. pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
  490. pbus->name, pna, pns, parent);
  491. /* Apply bus translation */
  492. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  493. return OF_BAD_ADDR;
  494. /* Complete the move up one level */
  495. na = pna;
  496. ns = pns;
  497. bus = pbus;
  498. of_dump_addr("one level translation:", addr, na);
  499. }
  500. unreachable();
  501. }
  502. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  503. {
  504. struct device_node *host;
  505. u64 ret;
  506. ret = __of_translate_address(dev, of_get_parent,
  507. in_addr, "ranges", &host);
  508. if (host) {
  509. of_node_put(host);
  510. return OF_BAD_ADDR;
  511. }
  512. return ret;
  513. }
  514. EXPORT_SYMBOL(of_translate_address);
  515. #ifdef CONFIG_HAS_DMA
  516. struct device_node *__of_get_dma_parent(const struct device_node *np)
  517. {
  518. struct of_phandle_args args;
  519. int ret, index;
  520. index = of_property_match_string(np, "interconnect-names", "dma-mem");
  521. if (index < 0)
  522. return of_get_parent(np);
  523. ret = of_parse_phandle_with_args(np, "interconnects",
  524. "#interconnect-cells",
  525. index, &args);
  526. if (ret < 0)
  527. return of_get_parent(np);
  528. return args.np;
  529. }
  530. #endif
  531. static struct device_node *of_get_next_dma_parent(struct device_node *np)
  532. {
  533. struct device_node *parent;
  534. parent = __of_get_dma_parent(np);
  535. of_node_put(np);
  536. return parent;
  537. }
  538. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  539. {
  540. struct device_node *host;
  541. u64 ret;
  542. ret = __of_translate_address(dev, __of_get_dma_parent,
  543. in_addr, "dma-ranges", &host);
  544. if (host) {
  545. of_node_put(host);
  546. return OF_BAD_ADDR;
  547. }
  548. return ret;
  549. }
  550. EXPORT_SYMBOL(of_translate_dma_address);
  551. /**
  552. * of_translate_dma_region - Translate device tree address and size tuple
  553. * @dev: device tree node for which to translate
  554. * @prop: pointer into array of cells
  555. * @start: return value for the start of the DMA range
  556. * @length: return value for the length of the DMA range
  557. *
  558. * Returns a pointer to the cell immediately following the translated DMA region.
  559. */
  560. const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
  561. phys_addr_t *start, size_t *length)
  562. {
  563. struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
  564. u64 address, size;
  565. int na, ns;
  566. if (!parent)
  567. return NULL;
  568. na = of_bus_n_addr_cells(parent);
  569. ns = of_bus_n_size_cells(parent);
  570. address = of_translate_dma_address(dev, prop);
  571. if (address == OF_BAD_ADDR)
  572. return NULL;
  573. size = of_read_number(prop + na, ns);
  574. if (start)
  575. *start = address;
  576. if (length)
  577. *length = size;
  578. return prop + na + ns;
  579. }
  580. EXPORT_SYMBOL(of_translate_dma_region);
  581. const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
  582. u64 *size, unsigned int *flags)
  583. {
  584. const __be32 *prop;
  585. unsigned int psize;
  586. struct device_node *parent __free(device_node) = of_get_parent(dev);
  587. const struct of_bus *bus;
  588. int onesize, i, na, ns;
  589. if (parent == NULL)
  590. return NULL;
  591. /* match the parent's bus type */
  592. bus = of_match_bus(parent);
  593. if (!bus || (strcmp(bus->name, "pci") && (bar_no >= 0)))
  594. return NULL;
  595. /* Get "reg" or "assigned-addresses" property */
  596. prop = of_get_property(dev, bus->addresses, &psize);
  597. if (prop == NULL)
  598. return NULL;
  599. psize /= 4;
  600. bus->count_cells(dev, &na, &ns);
  601. if (!OF_CHECK_ADDR_COUNT(na))
  602. return NULL;
  603. onesize = na + ns;
  604. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  605. u32 val = be32_to_cpu(prop[0]);
  606. /* PCI bus matches on BAR number instead of index */
  607. if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
  608. ((index >= 0) && (i == index))) {
  609. if (size)
  610. *size = of_read_number(prop + na, ns);
  611. if (flags)
  612. *flags = bus->get_flags(prop);
  613. return prop;
  614. }
  615. }
  616. return NULL;
  617. }
  618. EXPORT_SYMBOL(__of_get_address);
  619. /**
  620. * of_property_read_reg - Retrieve the specified "reg" entry index without translating
  621. * @np: device tree node for which to retrieve "reg" from
  622. * @idx: "reg" entry index to read
  623. * @addr: return value for the untranslated address
  624. * @size: return value for the entry size
  625. *
  626. * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
  627. * size values filled in.
  628. */
  629. int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
  630. {
  631. const __be32 *prop = of_get_address(np, idx, size, NULL);
  632. if (!prop)
  633. return -EINVAL;
  634. *addr = of_read_number(prop, of_n_addr_cells(np));
  635. return 0;
  636. }
  637. EXPORT_SYMBOL(of_property_read_reg);
  638. static int parser_init(struct of_pci_range_parser *parser,
  639. struct device_node *node, const char *name)
  640. {
  641. int rlen;
  642. parser->node = node;
  643. parser->pna = of_n_addr_cells(node);
  644. parser->na = of_bus_n_addr_cells(node);
  645. parser->ns = of_bus_n_size_cells(node);
  646. parser->dma = !strcmp(name, "dma-ranges");
  647. parser->bus = of_match_bus(node);
  648. parser->range = of_get_property(node, name, &rlen);
  649. if (parser->range == NULL)
  650. return -ENOENT;
  651. parser->end = parser->range + rlen / sizeof(__be32);
  652. return 0;
  653. }
  654. int of_pci_range_parser_init(struct of_pci_range_parser *parser,
  655. struct device_node *node)
  656. {
  657. return parser_init(parser, node, "ranges");
  658. }
  659. EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
  660. int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  661. struct device_node *node)
  662. {
  663. return parser_init(parser, node, "dma-ranges");
  664. }
  665. EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
  666. #define of_dma_range_parser_init of_pci_dma_range_parser_init
  667. struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
  668. struct of_pci_range *range)
  669. {
  670. int na = parser->na;
  671. int ns = parser->ns;
  672. int np = parser->pna + na + ns;
  673. int busflag_na = parser->bus->flag_cells;
  674. if (!range)
  675. return NULL;
  676. if (!parser->range || parser->range + np > parser->end)
  677. return NULL;
  678. range->flags = parser->bus->get_flags(parser->range);
  679. range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
  680. if (parser->dma)
  681. range->cpu_addr = of_translate_dma_address(parser->node,
  682. parser->range + na);
  683. else
  684. range->cpu_addr = of_translate_address(parser->node,
  685. parser->range + na);
  686. range->parent_bus_addr = of_read_number(parser->range + na, parser->pna);
  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 = kzalloc_objs(*r, num_ranges + 1);
  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. static bool of_mmio_is_nonposted(const struct device_node *np)
  873. {
  874. struct device_node *parent __free(device_node) = of_get_parent(np);
  875. if (of_property_read_bool(np, "nonposted-mmio"))
  876. return true;
  877. return parent && of_property_read_bool(parent, "nonposted-mmio");
  878. }
  879. static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
  880. struct resource *r)
  881. {
  882. u64 taddr;
  883. const __be32 *addrp;
  884. u64 size;
  885. unsigned int flags;
  886. const char *name = NULL;
  887. addrp = __of_get_address(dev, index, bar_no, &size, &flags);
  888. if (addrp == NULL)
  889. return -EINVAL;
  890. /* Get optional "reg-names" property to add a name to a resource */
  891. if (index >= 0)
  892. of_property_read_string_index(dev, "reg-names", index, &name);
  893. if (flags & IORESOURCE_MEM)
  894. taddr = of_translate_address(dev, addrp);
  895. else if (flags & IORESOURCE_IO)
  896. taddr = of_translate_ioport(dev, addrp, size);
  897. else
  898. return -EINVAL;
  899. if (taddr == OF_BAD_ADDR)
  900. return -EINVAL;
  901. memset(r, 0, sizeof(struct resource));
  902. if (of_mmio_is_nonposted(dev))
  903. flags |= IORESOURCE_MEM_NONPOSTED;
  904. r->flags = flags;
  905. r->name = name ? name : dev->full_name;
  906. return __of_address_resource_bounds(r, taddr, size);
  907. }
  908. /**
  909. * of_address_to_resource - Translate device tree address and return as resource
  910. * @dev: Caller's Device Node
  911. * @index: Index into the array
  912. * @r: Pointer to resource array
  913. *
  914. * Returns -EINVAL if the range cannot be converted to resource.
  915. *
  916. * Note that if your address is a PIO address, the conversion will fail if
  917. * the physical address can't be internally converted to an IO token with
  918. * pci_address_to_pio(), that is because it's either called too early or it
  919. * can't be matched to any host bridge IO space
  920. */
  921. int of_address_to_resource(struct device_node *dev, int index,
  922. struct resource *r)
  923. {
  924. return __of_address_to_resource(dev, index, -1, r);
  925. }
  926. EXPORT_SYMBOL_GPL(of_address_to_resource);
  927. int of_pci_address_to_resource(struct device_node *dev, int bar,
  928. struct resource *r)
  929. {
  930. if (!IS_ENABLED(CONFIG_PCI))
  931. return -ENOSYS;
  932. return __of_address_to_resource(dev, -1, bar, r);
  933. }
  934. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  935. /**
  936. * of_iomap - Maps the memory mapped IO for a given device_node
  937. * @np: the device whose io range will be mapped
  938. * @index: index of the io range
  939. *
  940. * Returns a pointer to the mapped memory
  941. */
  942. void __iomem *of_iomap(struct device_node *np, int index)
  943. {
  944. struct resource res;
  945. if (of_address_to_resource(np, index, &res))
  946. return NULL;
  947. if (res.flags & IORESOURCE_MEM_NONPOSTED)
  948. return ioremap_np(res.start, resource_size(&res));
  949. else
  950. return ioremap(res.start, resource_size(&res));
  951. }
  952. EXPORT_SYMBOL(of_iomap);
  953. /*
  954. * of_io_request_and_map - Requests a resource and maps the memory mapped IO
  955. * for a given device_node
  956. * @device: the device whose io range will be mapped
  957. * @index: index of the io range
  958. * @name: name "override" for the memory region request or NULL
  959. *
  960. * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
  961. * error code on failure. Usage example:
  962. *
  963. * base = of_io_request_and_map(node, 0, "foo");
  964. * if (IS_ERR(base))
  965. * return PTR_ERR(base);
  966. */
  967. void __iomem *of_io_request_and_map(struct device_node *np, int index,
  968. const char *name)
  969. {
  970. struct resource res;
  971. void __iomem *mem;
  972. if (of_address_to_resource(np, index, &res))
  973. return IOMEM_ERR_PTR(-EINVAL);
  974. if (!name)
  975. name = res.name;
  976. if (!request_mem_region(res.start, resource_size(&res), name))
  977. return IOMEM_ERR_PTR(-EBUSY);
  978. if (res.flags & IORESOURCE_MEM_NONPOSTED)
  979. mem = ioremap_np(res.start, resource_size(&res));
  980. else
  981. mem = ioremap(res.start, resource_size(&res));
  982. if (!mem) {
  983. release_mem_region(res.start, resource_size(&res));
  984. return IOMEM_ERR_PTR(-ENOMEM);
  985. }
  986. return mem;
  987. }
  988. EXPORT_SYMBOL(of_io_request_and_map);