address.c 25 KB

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