address.c 25 KB

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