address.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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 = be32_to_cpup(parser->range);
  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. if ((sizeof(resource_size_t) < 8) &&
  285. upper_32_bits(range->cpu_addr)) {
  286. err = -EINVAL;
  287. goto invalid_range;
  288. }
  289. res->start = range->cpu_addr;
  290. }
  291. res->end = res->start + range->size - 1;
  292. return 0;
  293. invalid_range:
  294. res->start = (resource_size_t)OF_BAD_ADDR;
  295. res->end = (resource_size_t)OF_BAD_ADDR;
  296. return err;
  297. }
  298. #endif /* CONFIG_PCI */
  299. /*
  300. * ISA bus specific translator
  301. */
  302. static int of_bus_isa_match(struct device_node *np)
  303. {
  304. return !strcmp(np->name, "isa");
  305. }
  306. static void of_bus_isa_count_cells(struct device_node *child,
  307. int *addrc, int *sizec)
  308. {
  309. if (addrc)
  310. *addrc = 2;
  311. if (sizec)
  312. *sizec = 1;
  313. }
  314. static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
  315. int pna)
  316. {
  317. u64 cp, s, da;
  318. /* Check address type match */
  319. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  320. return OF_BAD_ADDR;
  321. /* Read address values, skipping high cell */
  322. cp = of_read_number(range + 1, na - 1);
  323. s = of_read_number(range + na + pna, ns);
  324. da = of_read_number(addr + 1, na - 1);
  325. pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
  326. (unsigned long long)cp, (unsigned long long)s,
  327. (unsigned long long)da);
  328. if (da < cp || da >= (cp + s))
  329. return OF_BAD_ADDR;
  330. return da - cp;
  331. }
  332. static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
  333. {
  334. return of_bus_default_translate(addr + 1, offset, na - 1);
  335. }
  336. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  337. {
  338. unsigned int flags = 0;
  339. u32 w = be32_to_cpup(addr);
  340. if (w & 1)
  341. flags |= IORESOURCE_IO;
  342. else
  343. flags |= IORESOURCE_MEM;
  344. return flags;
  345. }
  346. /*
  347. * Array of bus specific translators
  348. */
  349. static struct of_bus of_busses[] = {
  350. #ifdef CONFIG_OF_ADDRESS_PCI
  351. /* PCI */
  352. {
  353. .name = "pci",
  354. .addresses = "assigned-addresses",
  355. .match = of_bus_pci_match,
  356. .count_cells = of_bus_pci_count_cells,
  357. .map = of_bus_pci_map,
  358. .translate = of_bus_pci_translate,
  359. .get_flags = of_bus_pci_get_flags,
  360. },
  361. #endif /* CONFIG_OF_ADDRESS_PCI */
  362. /* ISA */
  363. {
  364. .name = "isa",
  365. .addresses = "reg",
  366. .match = of_bus_isa_match,
  367. .count_cells = of_bus_isa_count_cells,
  368. .map = of_bus_isa_map,
  369. .translate = of_bus_isa_translate,
  370. .get_flags = of_bus_isa_get_flags,
  371. },
  372. /* Default */
  373. {
  374. .name = "default",
  375. .addresses = "reg",
  376. .match = NULL,
  377. .count_cells = of_bus_default_count_cells,
  378. .map = of_bus_default_map,
  379. .translate = of_bus_default_translate,
  380. .get_flags = of_bus_default_get_flags,
  381. },
  382. };
  383. static struct of_bus *of_match_bus(struct device_node *np)
  384. {
  385. int i;
  386. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  387. if (!of_busses[i].match || of_busses[i].match(np))
  388. return &of_busses[i];
  389. BUG();
  390. return NULL;
  391. }
  392. static int of_empty_ranges_quirk(struct device_node *np)
  393. {
  394. if (IS_ENABLED(CONFIG_PPC)) {
  395. /* To save cycles, we cache the result for global "Mac" setting */
  396. static int quirk_state = -1;
  397. /* PA-SEMI sdc DT bug */
  398. if (of_device_is_compatible(np, "1682m-sdc"))
  399. return true;
  400. /* Make quirk cached */
  401. if (quirk_state < 0)
  402. quirk_state =
  403. of_machine_is_compatible("Power Macintosh") ||
  404. of_machine_is_compatible("MacRISC");
  405. return quirk_state;
  406. }
  407. return false;
  408. }
  409. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  410. struct of_bus *pbus, __be32 *addr,
  411. int na, int ns, int pna, const char *rprop)
  412. {
  413. const __be32 *ranges;
  414. unsigned int rlen;
  415. int rone;
  416. u64 offset = OF_BAD_ADDR;
  417. /*
  418. * Normally, an absence of a "ranges" property means we are
  419. * crossing a non-translatable boundary, and thus the addresses
  420. * below the current cannot be converted to CPU physical ones.
  421. * Unfortunately, while this is very clear in the spec, it's not
  422. * what Apple understood, and they do have things like /uni-n or
  423. * /ht nodes with no "ranges" property and a lot of perfectly
  424. * useable mapped devices below them. Thus we treat the absence of
  425. * "ranges" as equivalent to an empty "ranges" property which means
  426. * a 1:1 translation at that level. It's up to the caller not to try
  427. * to translate addresses that aren't supposed to be translated in
  428. * the first place. --BenH.
  429. *
  430. * As far as we know, this damage only exists on Apple machines, so
  431. * This code is only enabled on powerpc. --gcl
  432. */
  433. ranges = of_get_property(parent, rprop, &rlen);
  434. if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
  435. pr_debug("OF: no ranges; cannot translate\n");
  436. return 1;
  437. }
  438. if (ranges == NULL || rlen == 0) {
  439. offset = of_read_number(addr, na);
  440. memset(addr, 0, pna * 4);
  441. pr_debug("OF: empty ranges; 1:1 translation\n");
  442. goto finish;
  443. }
  444. pr_debug("OF: walking ranges...\n");
  445. /* Now walk through the ranges */
  446. rlen /= 4;
  447. rone = na + pna + ns;
  448. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  449. offset = bus->map(addr, ranges, na, ns, pna);
  450. if (offset != OF_BAD_ADDR)
  451. break;
  452. }
  453. if (offset == OF_BAD_ADDR) {
  454. pr_debug("OF: not found !\n");
  455. return 1;
  456. }
  457. memcpy(addr, ranges + na, 4 * pna);
  458. finish:
  459. of_dump_addr("OF: parent translation for:", addr, pna);
  460. pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
  461. /* Translate it into parent bus space */
  462. return pbus->translate(addr, offset, pna);
  463. }
  464. /*
  465. * Translate an address from the device-tree into a CPU physical address,
  466. * this walks up the tree and applies the various bus mappings on the
  467. * way.
  468. *
  469. * Note: We consider that crossing any level with #size-cells == 0 to mean
  470. * that translation is impossible (that is we are not dealing with a value
  471. * that can be mapped to a cpu physical address). This is not really specified
  472. * that way, but this is traditionally the way IBM at least do things
  473. */
  474. static u64 __of_translate_address(struct device_node *dev,
  475. const __be32 *in_addr, const char *rprop)
  476. {
  477. struct device_node *parent = NULL;
  478. struct of_bus *bus, *pbus;
  479. __be32 addr[OF_MAX_ADDR_CELLS];
  480. int na, ns, pna, pns;
  481. u64 result = OF_BAD_ADDR;
  482. pr_debug("OF: ** translation for device %s **\n", of_node_full_name(dev));
  483. /* Increase refcount at current level */
  484. of_node_get(dev);
  485. /* Get parent & match bus type */
  486. parent = of_get_parent(dev);
  487. if (parent == NULL)
  488. goto bail;
  489. bus = of_match_bus(parent);
  490. /* Count address cells & copy address locally */
  491. bus->count_cells(dev, &na, &ns);
  492. if (!OF_CHECK_COUNTS(na, ns)) {
  493. pr_debug("OF: Bad cell count for %s\n", of_node_full_name(dev));
  494. goto bail;
  495. }
  496. memcpy(addr, in_addr, na * 4);
  497. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  498. bus->name, na, ns, of_node_full_name(parent));
  499. of_dump_addr("OF: translating address:", addr, na);
  500. /* Translate */
  501. for (;;) {
  502. /* Switch to parent bus */
  503. of_node_put(dev);
  504. dev = parent;
  505. parent = of_get_parent(dev);
  506. /* If root, we have finished */
  507. if (parent == NULL) {
  508. pr_debug("OF: reached root node\n");
  509. result = of_read_number(addr, na);
  510. break;
  511. }
  512. /* Get new parent bus and counts */
  513. pbus = of_match_bus(parent);
  514. pbus->count_cells(dev, &pna, &pns);
  515. if (!OF_CHECK_COUNTS(pna, pns)) {
  516. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  517. of_node_full_name(dev));
  518. break;
  519. }
  520. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  521. pbus->name, pna, pns, of_node_full_name(parent));
  522. /* Apply bus translation */
  523. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  524. break;
  525. /* Complete the move up one level */
  526. na = pna;
  527. ns = pns;
  528. bus = pbus;
  529. of_dump_addr("OF: one level translation:", addr, na);
  530. }
  531. bail:
  532. of_node_put(parent);
  533. of_node_put(dev);
  534. return result;
  535. }
  536. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  537. {
  538. return __of_translate_address(dev, in_addr, "ranges");
  539. }
  540. EXPORT_SYMBOL(of_translate_address);
  541. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  542. {
  543. return __of_translate_address(dev, in_addr, "dma-ranges");
  544. }
  545. EXPORT_SYMBOL(of_translate_dma_address);
  546. const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
  547. unsigned int *flags)
  548. {
  549. const __be32 *prop;
  550. unsigned int psize;
  551. struct device_node *parent;
  552. struct of_bus *bus;
  553. int onesize, i, na, ns;
  554. /* Get parent & match bus type */
  555. parent = of_get_parent(dev);
  556. if (parent == NULL)
  557. return NULL;
  558. bus = of_match_bus(parent);
  559. bus->count_cells(dev, &na, &ns);
  560. of_node_put(parent);
  561. if (!OF_CHECK_ADDR_COUNT(na))
  562. return NULL;
  563. /* Get "reg" or "assigned-addresses" property */
  564. prop = of_get_property(dev, bus->addresses, &psize);
  565. if (prop == NULL)
  566. return NULL;
  567. psize /= 4;
  568. onesize = na + ns;
  569. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  570. if (i == index) {
  571. if (size)
  572. *size = of_read_number(prop + na, ns);
  573. if (flags)
  574. *flags = bus->get_flags(prop);
  575. return prop;
  576. }
  577. return NULL;
  578. }
  579. EXPORT_SYMBOL(of_get_address);
  580. #ifdef PCI_IOBASE
  581. struct io_range {
  582. struct list_head list;
  583. phys_addr_t start;
  584. resource_size_t size;
  585. };
  586. static LIST_HEAD(io_range_list);
  587. static DEFINE_SPINLOCK(io_range_lock);
  588. #endif
  589. /*
  590. * Record the PCI IO range (expressed as CPU physical address + size).
  591. * Return a negative value if an error has occured, zero otherwise
  592. */
  593. int __weak pci_register_io_range(phys_addr_t addr, resource_size_t size)
  594. {
  595. int err = 0;
  596. #ifdef PCI_IOBASE
  597. struct io_range *range;
  598. resource_size_t allocated_size = 0;
  599. /* check if the range hasn't been previously recorded */
  600. spin_lock(&io_range_lock);
  601. list_for_each_entry(range, &io_range_list, list) {
  602. if (addr >= range->start && addr + size <= range->start + size) {
  603. /* range already registered, bail out */
  604. goto end_register;
  605. }
  606. allocated_size += range->size;
  607. }
  608. /* range not registed yet, check for available space */
  609. if (allocated_size + size - 1 > IO_SPACE_LIMIT) {
  610. /* if it's too big check if 64K space can be reserved */
  611. if (allocated_size + SZ_64K - 1 > IO_SPACE_LIMIT) {
  612. err = -E2BIG;
  613. goto end_register;
  614. }
  615. size = SZ_64K;
  616. pr_warn("Requested IO range too big, new size set to 64K\n");
  617. }
  618. /* add the range to the list */
  619. range = kzalloc(sizeof(*range), GFP_ATOMIC);
  620. if (!range) {
  621. err = -ENOMEM;
  622. goto end_register;
  623. }
  624. range->start = addr;
  625. range->size = size;
  626. list_add_tail(&range->list, &io_range_list);
  627. end_register:
  628. spin_unlock(&io_range_lock);
  629. #endif
  630. return err;
  631. }
  632. phys_addr_t pci_pio_to_address(unsigned long pio)
  633. {
  634. phys_addr_t address = (phys_addr_t)OF_BAD_ADDR;
  635. #ifdef PCI_IOBASE
  636. struct io_range *range;
  637. resource_size_t allocated_size = 0;
  638. if (pio > IO_SPACE_LIMIT)
  639. return address;
  640. spin_lock(&io_range_lock);
  641. list_for_each_entry(range, &io_range_list, list) {
  642. if (pio >= allocated_size && pio < allocated_size + range->size) {
  643. address = range->start + pio - allocated_size;
  644. break;
  645. }
  646. allocated_size += range->size;
  647. }
  648. spin_unlock(&io_range_lock);
  649. #endif
  650. return address;
  651. }
  652. unsigned long __weak pci_address_to_pio(phys_addr_t address)
  653. {
  654. #ifdef PCI_IOBASE
  655. struct io_range *res;
  656. resource_size_t offset = 0;
  657. unsigned long addr = -1;
  658. spin_lock(&io_range_lock);
  659. list_for_each_entry(res, &io_range_list, list) {
  660. if (address >= res->start && address < res->start + res->size) {
  661. addr = address - res->start + offset;
  662. break;
  663. }
  664. offset += res->size;
  665. }
  666. spin_unlock(&io_range_lock);
  667. return addr;
  668. #else
  669. if (address > IO_SPACE_LIMIT)
  670. return (unsigned long)-1;
  671. return (unsigned long) address;
  672. #endif
  673. }
  674. static int __of_address_to_resource(struct device_node *dev,
  675. const __be32 *addrp, u64 size, unsigned int flags,
  676. const char *name, struct resource *r)
  677. {
  678. u64 taddr;
  679. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  680. return -EINVAL;
  681. taddr = of_translate_address(dev, addrp);
  682. if (taddr == OF_BAD_ADDR)
  683. return -EINVAL;
  684. memset(r, 0, sizeof(struct resource));
  685. if (flags & IORESOURCE_IO) {
  686. unsigned long port;
  687. port = pci_address_to_pio(taddr);
  688. if (port == (unsigned long)-1)
  689. return -EINVAL;
  690. r->start = port;
  691. r->end = port + size - 1;
  692. } else {
  693. r->start = taddr;
  694. r->end = taddr + size - 1;
  695. }
  696. r->flags = flags;
  697. r->name = name ? name : dev->full_name;
  698. return 0;
  699. }
  700. /**
  701. * of_address_to_resource - Translate device tree address and return as resource
  702. *
  703. * Note that if your address is a PIO address, the conversion will fail if
  704. * the physical address can't be internally converted to an IO token with
  705. * pci_address_to_pio(), that is because it's either called to early or it
  706. * can't be matched to any host bridge IO space
  707. */
  708. int of_address_to_resource(struct device_node *dev, int index,
  709. struct resource *r)
  710. {
  711. const __be32 *addrp;
  712. u64 size;
  713. unsigned int flags;
  714. const char *name = NULL;
  715. addrp = of_get_address(dev, index, &size, &flags);
  716. if (addrp == NULL)
  717. return -EINVAL;
  718. /* Get optional "reg-names" property to add a name to a resource */
  719. of_property_read_string_index(dev, "reg-names", index, &name);
  720. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  721. }
  722. EXPORT_SYMBOL_GPL(of_address_to_resource);
  723. struct device_node *of_find_matching_node_by_address(struct device_node *from,
  724. const struct of_device_id *matches,
  725. u64 base_address)
  726. {
  727. struct device_node *dn = of_find_matching_node(from, matches);
  728. struct resource res;
  729. while (dn) {
  730. if (!of_address_to_resource(dn, 0, &res) &&
  731. res.start == base_address)
  732. return dn;
  733. dn = of_find_matching_node(dn, matches);
  734. }
  735. return NULL;
  736. }
  737. /**
  738. * of_iomap - Maps the memory mapped IO for a given device_node
  739. * @device: the device whose io range will be mapped
  740. * @index: index of the io range
  741. *
  742. * Returns a pointer to the mapped memory
  743. */
  744. void __iomem *of_iomap(struct device_node *np, int index)
  745. {
  746. struct resource res;
  747. if (of_address_to_resource(np, index, &res))
  748. return NULL;
  749. return ioremap(res.start, resource_size(&res));
  750. }
  751. EXPORT_SYMBOL(of_iomap);
  752. /*
  753. * of_io_request_and_map - Requests a resource and maps the memory mapped IO
  754. * for a given device_node
  755. * @device: the device whose io range will be mapped
  756. * @index: index of the io range
  757. * @name: name of the resource
  758. *
  759. * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
  760. * error code on failure. Usage example:
  761. *
  762. * base = of_io_request_and_map(node, 0, "foo");
  763. * if (IS_ERR(base))
  764. * return PTR_ERR(base);
  765. */
  766. void __iomem *of_io_request_and_map(struct device_node *np, int index,
  767. const char *name)
  768. {
  769. struct resource res;
  770. void __iomem *mem;
  771. if (of_address_to_resource(np, index, &res))
  772. return IOMEM_ERR_PTR(-EINVAL);
  773. if (!request_mem_region(res.start, resource_size(&res), name))
  774. return IOMEM_ERR_PTR(-EBUSY);
  775. mem = ioremap(res.start, resource_size(&res));
  776. if (!mem) {
  777. release_mem_region(res.start, resource_size(&res));
  778. return IOMEM_ERR_PTR(-ENOMEM);
  779. }
  780. return mem;
  781. }
  782. EXPORT_SYMBOL(of_io_request_and_map);
  783. /**
  784. * of_dma_get_range - Get DMA range info
  785. * @np: device node to get DMA range info
  786. * @dma_addr: pointer to store initial DMA address of DMA range
  787. * @paddr: pointer to store initial CPU address of DMA range
  788. * @size: pointer to store size of DMA range
  789. *
  790. * Look in bottom up direction for the first "dma-ranges" property
  791. * and parse it.
  792. * dma-ranges format:
  793. * DMA addr (dma_addr) : naddr cells
  794. * CPU addr (phys_addr_t) : pna cells
  795. * size : nsize cells
  796. *
  797. * It returns -ENODEV if "dma-ranges" property was not found
  798. * for this device in DT.
  799. */
  800. int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
  801. {
  802. struct device_node *node = of_node_get(np);
  803. const __be32 *ranges = NULL;
  804. int len, naddr, nsize, pna;
  805. int ret = 0;
  806. u64 dmaaddr;
  807. if (!node)
  808. return -EINVAL;
  809. while (1) {
  810. naddr = of_n_addr_cells(node);
  811. nsize = of_n_size_cells(node);
  812. node = of_get_next_parent(node);
  813. if (!node)
  814. break;
  815. ranges = of_get_property(node, "dma-ranges", &len);
  816. /* Ignore empty ranges, they imply no translation required */
  817. if (ranges && len > 0)
  818. break;
  819. /*
  820. * At least empty ranges has to be defined for parent node if
  821. * DMA is supported
  822. */
  823. if (!ranges)
  824. break;
  825. }
  826. if (!ranges) {
  827. pr_debug("%s: no dma-ranges found for node(%s)\n",
  828. __func__, np->full_name);
  829. ret = -ENODEV;
  830. goto out;
  831. }
  832. len /= sizeof(u32);
  833. pna = of_n_addr_cells(node);
  834. /* dma-ranges format:
  835. * DMA addr : naddr cells
  836. * CPU addr : pna cells
  837. * size : nsize cells
  838. */
  839. dmaaddr = of_read_number(ranges, naddr);
  840. *paddr = of_translate_dma_address(np, ranges);
  841. if (*paddr == OF_BAD_ADDR) {
  842. pr_err("%s: translation of DMA address(%pad) to CPU address failed node(%s)\n",
  843. __func__, dma_addr, np->full_name);
  844. ret = -EINVAL;
  845. goto out;
  846. }
  847. *dma_addr = dmaaddr;
  848. *size = of_read_number(ranges + naddr + pna, nsize);
  849. pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
  850. *dma_addr, *paddr, *size);
  851. out:
  852. of_node_put(node);
  853. return ret;
  854. }
  855. EXPORT_SYMBOL_GPL(of_dma_get_range);
  856. /**
  857. * of_dma_is_coherent - Check if device is coherent
  858. * @np: device node
  859. *
  860. * It returns true if "dma-coherent" property was found
  861. * for this device in DT.
  862. */
  863. bool of_dma_is_coherent(struct device_node *np)
  864. {
  865. struct device_node *node = of_node_get(np);
  866. while (node) {
  867. if (of_property_read_bool(node, "dma-coherent")) {
  868. of_node_put(node);
  869. return true;
  870. }
  871. node = of_get_next_parent(node);
  872. }
  873. of_node_put(node);
  874. return false;
  875. }
  876. EXPORT_SYMBOL_GPL(of_dma_is_coherent);