address.c 23 KB

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