address.c 25 KB

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