address.c 28 KB

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