address.c 23 KB

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