address.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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/string.h>
  8. /* Max address size we deal with */
  9. #define OF_MAX_ADDR_CELLS 4
  10. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  11. (ns) > 0)
  12. static struct of_bus *of_match_bus(struct device_node *np);
  13. static int __of_address_to_resource(struct device_node *dev,
  14. const __be32 *addrp, u64 size, unsigned int flags,
  15. const char *name, struct resource *r);
  16. /* Debug utility */
  17. #ifdef DEBUG
  18. static void of_dump_addr(const char *s, const __be32 *addr, int na)
  19. {
  20. printk(KERN_DEBUG "%s", s);
  21. while (na--)
  22. printk(" %08x", be32_to_cpu(*(addr++)));
  23. printk("\n");
  24. }
  25. #else
  26. static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
  27. #endif
  28. /* Callbacks for bus specific translators */
  29. struct of_bus {
  30. const char *name;
  31. const char *addresses;
  32. int (*match)(struct device_node *parent);
  33. void (*count_cells)(struct device_node *child,
  34. int *addrc, int *sizec);
  35. u64 (*map)(u32 *addr, const __be32 *range,
  36. int na, int ns, int pna);
  37. int (*translate)(u32 *addr, u64 offset, int na);
  38. unsigned int (*get_flags)(const __be32 *addr);
  39. };
  40. /*
  41. * Default translator (generic bus)
  42. */
  43. static void of_bus_default_count_cells(struct device_node *dev,
  44. int *addrc, int *sizec)
  45. {
  46. if (addrc)
  47. *addrc = of_n_addr_cells(dev);
  48. if (sizec)
  49. *sizec = of_n_size_cells(dev);
  50. }
  51. static u64 of_bus_default_map(u32 *addr, const __be32 *range,
  52. int na, int ns, int pna)
  53. {
  54. u64 cp, s, da;
  55. cp = of_read_number(range, na);
  56. s = of_read_number(range + na + pna, ns);
  57. da = of_read_number(addr, na);
  58. pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
  59. (unsigned long long)cp, (unsigned long long)s,
  60. (unsigned long long)da);
  61. if (da < cp || da >= (cp + s))
  62. return OF_BAD_ADDR;
  63. return da - cp;
  64. }
  65. static int of_bus_default_translate(u32 *addr, u64 offset, int na)
  66. {
  67. u64 a = of_read_number(addr, na);
  68. memset(addr, 0, na * 4);
  69. a += offset;
  70. if (na > 1)
  71. addr[na - 2] = cpu_to_be32(a >> 32);
  72. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  73. return 0;
  74. }
  75. static unsigned int of_bus_default_get_flags(const __be32 *addr)
  76. {
  77. return IORESOURCE_MEM;
  78. }
  79. #ifdef CONFIG_PCI
  80. /*
  81. * PCI bus specific translator
  82. */
  83. static int of_bus_pci_match(struct device_node *np)
  84. {
  85. /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
  86. return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
  87. }
  88. static void of_bus_pci_count_cells(struct device_node *np,
  89. int *addrc, int *sizec)
  90. {
  91. if (addrc)
  92. *addrc = 3;
  93. if (sizec)
  94. *sizec = 2;
  95. }
  96. static unsigned int of_bus_pci_get_flags(const __be32 *addr)
  97. {
  98. unsigned int flags = 0;
  99. u32 w = be32_to_cpup(addr);
  100. switch((w >> 24) & 0x03) {
  101. case 0x01:
  102. flags |= IORESOURCE_IO;
  103. break;
  104. case 0x02: /* 32 bits */
  105. case 0x03: /* 64 bits */
  106. flags |= IORESOURCE_MEM;
  107. break;
  108. }
  109. if (w & 0x40000000)
  110. flags |= IORESOURCE_PREFETCH;
  111. return flags;
  112. }
  113. static u64 of_bus_pci_map(u32 *addr, const __be32 *range, int na, int ns,
  114. int pna)
  115. {
  116. u64 cp, s, da;
  117. unsigned int af, rf;
  118. af = of_bus_pci_get_flags(addr);
  119. rf = of_bus_pci_get_flags(range);
  120. /* Check address type match */
  121. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  122. return OF_BAD_ADDR;
  123. /* Read address values, skipping high cell */
  124. cp = of_read_number(range + 1, na - 1);
  125. s = of_read_number(range + na + pna, ns);
  126. da = of_read_number(addr + 1, na - 1);
  127. pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
  128. (unsigned long long)cp, (unsigned long long)s,
  129. (unsigned long long)da);
  130. if (da < cp || da >= (cp + s))
  131. return OF_BAD_ADDR;
  132. return da - cp;
  133. }
  134. static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
  135. {
  136. return of_bus_default_translate(addr + 1, offset, na - 1);
  137. }
  138. const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  139. unsigned int *flags)
  140. {
  141. const __be32 *prop;
  142. unsigned int psize;
  143. struct device_node *parent;
  144. struct of_bus *bus;
  145. int onesize, i, na, ns;
  146. /* Get parent & match bus type */
  147. parent = of_get_parent(dev);
  148. if (parent == NULL)
  149. return NULL;
  150. bus = of_match_bus(parent);
  151. if (strcmp(bus->name, "pci")) {
  152. of_node_put(parent);
  153. return NULL;
  154. }
  155. bus->count_cells(dev, &na, &ns);
  156. of_node_put(parent);
  157. if (!OF_CHECK_COUNTS(na, ns))
  158. return NULL;
  159. /* Get "reg" or "assigned-addresses" property */
  160. prop = of_get_property(dev, bus->addresses, &psize);
  161. if (prop == NULL)
  162. return NULL;
  163. psize /= 4;
  164. onesize = na + ns;
  165. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  166. u32 val = be32_to_cpu(prop[0]);
  167. if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  168. if (size)
  169. *size = of_read_number(prop + na, ns);
  170. if (flags)
  171. *flags = bus->get_flags(prop);
  172. return prop;
  173. }
  174. }
  175. return NULL;
  176. }
  177. EXPORT_SYMBOL(of_get_pci_address);
  178. int of_pci_address_to_resource(struct device_node *dev, int bar,
  179. struct resource *r)
  180. {
  181. const __be32 *addrp;
  182. u64 size;
  183. unsigned int flags;
  184. addrp = of_get_pci_address(dev, bar, &size, &flags);
  185. if (addrp == NULL)
  186. return -EINVAL;
  187. return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
  188. }
  189. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  190. #endif /* CONFIG_PCI */
  191. /*
  192. * ISA bus specific translator
  193. */
  194. static int of_bus_isa_match(struct device_node *np)
  195. {
  196. return !strcmp(np->name, "isa");
  197. }
  198. static void of_bus_isa_count_cells(struct device_node *child,
  199. int *addrc, int *sizec)
  200. {
  201. if (addrc)
  202. *addrc = 2;
  203. if (sizec)
  204. *sizec = 1;
  205. }
  206. static u64 of_bus_isa_map(u32 *addr, const __be32 *range, int na, int ns,
  207. int pna)
  208. {
  209. u64 cp, s, da;
  210. /* Check address type match */
  211. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  212. return OF_BAD_ADDR;
  213. /* Read address values, skipping high cell */
  214. cp = of_read_number(range + 1, na - 1);
  215. s = of_read_number(range + na + pna, ns);
  216. da = of_read_number(addr + 1, na - 1);
  217. pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
  218. (unsigned long long)cp, (unsigned long long)s,
  219. (unsigned long long)da);
  220. if (da < cp || da >= (cp + s))
  221. return OF_BAD_ADDR;
  222. return da - cp;
  223. }
  224. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  225. {
  226. return of_bus_default_translate(addr + 1, offset, na - 1);
  227. }
  228. static unsigned int of_bus_isa_get_flags(const __be32 *addr)
  229. {
  230. unsigned int flags = 0;
  231. u32 w = be32_to_cpup(addr);
  232. if (w & 1)
  233. flags |= IORESOURCE_IO;
  234. else
  235. flags |= IORESOURCE_MEM;
  236. return flags;
  237. }
  238. /*
  239. * Array of bus specific translators
  240. */
  241. static struct of_bus of_busses[] = {
  242. #ifdef CONFIG_PCI
  243. /* PCI */
  244. {
  245. .name = "pci",
  246. .addresses = "assigned-addresses",
  247. .match = of_bus_pci_match,
  248. .count_cells = of_bus_pci_count_cells,
  249. .map = of_bus_pci_map,
  250. .translate = of_bus_pci_translate,
  251. .get_flags = of_bus_pci_get_flags,
  252. },
  253. #endif /* CONFIG_PCI */
  254. /* ISA */
  255. {
  256. .name = "isa",
  257. .addresses = "reg",
  258. .match = of_bus_isa_match,
  259. .count_cells = of_bus_isa_count_cells,
  260. .map = of_bus_isa_map,
  261. .translate = of_bus_isa_translate,
  262. .get_flags = of_bus_isa_get_flags,
  263. },
  264. /* Default */
  265. {
  266. .name = "default",
  267. .addresses = "reg",
  268. .match = NULL,
  269. .count_cells = of_bus_default_count_cells,
  270. .map = of_bus_default_map,
  271. .translate = of_bus_default_translate,
  272. .get_flags = of_bus_default_get_flags,
  273. },
  274. };
  275. static struct of_bus *of_match_bus(struct device_node *np)
  276. {
  277. int i;
  278. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  279. if (!of_busses[i].match || of_busses[i].match(np))
  280. return &of_busses[i];
  281. BUG();
  282. return NULL;
  283. }
  284. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  285. struct of_bus *pbus, u32 *addr,
  286. int na, int ns, int pna, const char *rprop)
  287. {
  288. const __be32 *ranges;
  289. unsigned int rlen;
  290. int rone;
  291. u64 offset = OF_BAD_ADDR;
  292. /* Normally, an absence of a "ranges" property means we are
  293. * crossing a non-translatable boundary, and thus the addresses
  294. * below the current not cannot be converted to CPU physical ones.
  295. * Unfortunately, while this is very clear in the spec, it's not
  296. * what Apple understood, and they do have things like /uni-n or
  297. * /ht nodes with no "ranges" property and a lot of perfectly
  298. * useable mapped devices below them. Thus we treat the absence of
  299. * "ranges" as equivalent to an empty "ranges" property which means
  300. * a 1:1 translation at that level. It's up to the caller not to try
  301. * to translate addresses that aren't supposed to be translated in
  302. * the first place. --BenH.
  303. *
  304. * As far as we know, this damage only exists on Apple machines, so
  305. * This code is only enabled on powerpc. --gcl
  306. */
  307. ranges = of_get_property(parent, rprop, &rlen);
  308. #if !defined(CONFIG_PPC)
  309. if (ranges == NULL) {
  310. pr_err("OF: no ranges; cannot translate\n");
  311. return 1;
  312. }
  313. #endif /* !defined(CONFIG_PPC) */
  314. if (ranges == NULL || rlen == 0) {
  315. offset = of_read_number(addr, na);
  316. memset(addr, 0, pna * 4);
  317. pr_debug("OF: empty ranges; 1:1 translation\n");
  318. goto finish;
  319. }
  320. pr_debug("OF: walking ranges...\n");
  321. /* Now walk through the ranges */
  322. rlen /= 4;
  323. rone = na + pna + ns;
  324. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  325. offset = bus->map(addr, ranges, na, ns, pna);
  326. if (offset != OF_BAD_ADDR)
  327. break;
  328. }
  329. if (offset == OF_BAD_ADDR) {
  330. pr_debug("OF: not found !\n");
  331. return 1;
  332. }
  333. memcpy(addr, ranges + na, 4 * pna);
  334. finish:
  335. of_dump_addr("OF: parent translation for:", addr, pna);
  336. pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
  337. /* Translate it into parent bus space */
  338. return pbus->translate(addr, offset, pna);
  339. }
  340. /*
  341. * Translate an address from the device-tree into a CPU physical address,
  342. * this walks up the tree and applies the various bus mappings on the
  343. * way.
  344. *
  345. * Note: We consider that crossing any level with #size-cells == 0 to mean
  346. * that translation is impossible (that is we are not dealing with a value
  347. * that can be mapped to a cpu physical address). This is not really specified
  348. * that way, but this is traditionally the way IBM at least do things
  349. */
  350. u64 __of_translate_address(struct device_node *dev, const __be32 *in_addr,
  351. const char *rprop)
  352. {
  353. struct device_node *parent = NULL;
  354. struct of_bus *bus, *pbus;
  355. u32 addr[OF_MAX_ADDR_CELLS];
  356. int na, ns, pna, pns;
  357. u64 result = OF_BAD_ADDR;
  358. pr_debug("OF: ** translation for device %s **\n", dev->full_name);
  359. /* Increase refcount at current level */
  360. of_node_get(dev);
  361. /* Get parent & match bus type */
  362. parent = of_get_parent(dev);
  363. if (parent == NULL)
  364. goto bail;
  365. bus = of_match_bus(parent);
  366. /* Cound address cells & copy address locally */
  367. bus->count_cells(dev, &na, &ns);
  368. if (!OF_CHECK_COUNTS(na, ns)) {
  369. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  370. dev->full_name);
  371. goto bail;
  372. }
  373. memcpy(addr, in_addr, na * 4);
  374. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  375. bus->name, na, ns, parent->full_name);
  376. of_dump_addr("OF: translating address:", addr, na);
  377. /* Translate */
  378. for (;;) {
  379. /* Switch to parent bus */
  380. of_node_put(dev);
  381. dev = parent;
  382. parent = of_get_parent(dev);
  383. /* If root, we have finished */
  384. if (parent == NULL) {
  385. pr_debug("OF: reached root node\n");
  386. result = of_read_number(addr, na);
  387. break;
  388. }
  389. /* Get new parent bus and counts */
  390. pbus = of_match_bus(parent);
  391. pbus->count_cells(dev, &pna, &pns);
  392. if (!OF_CHECK_COUNTS(pna, pns)) {
  393. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  394. dev->full_name);
  395. break;
  396. }
  397. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  398. pbus->name, pna, pns, parent->full_name);
  399. /* Apply bus translation */
  400. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  401. break;
  402. /* Complete the move up one level */
  403. na = pna;
  404. ns = pns;
  405. bus = pbus;
  406. of_dump_addr("OF: one level translation:", addr, na);
  407. }
  408. bail:
  409. of_node_put(parent);
  410. of_node_put(dev);
  411. return result;
  412. }
  413. u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
  414. {
  415. return __of_translate_address(dev, in_addr, "ranges");
  416. }
  417. EXPORT_SYMBOL(of_translate_address);
  418. u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
  419. {
  420. return __of_translate_address(dev, in_addr, "dma-ranges");
  421. }
  422. EXPORT_SYMBOL(of_translate_dma_address);
  423. const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
  424. unsigned int *flags)
  425. {
  426. const __be32 *prop;
  427. unsigned int psize;
  428. struct device_node *parent;
  429. struct of_bus *bus;
  430. int onesize, i, na, ns;
  431. /* Get parent & match bus type */
  432. parent = of_get_parent(dev);
  433. if (parent == NULL)
  434. return NULL;
  435. bus = of_match_bus(parent);
  436. bus->count_cells(dev, &na, &ns);
  437. of_node_put(parent);
  438. if (!OF_CHECK_COUNTS(na, ns))
  439. return NULL;
  440. /* Get "reg" or "assigned-addresses" property */
  441. prop = of_get_property(dev, bus->addresses, &psize);
  442. if (prop == NULL)
  443. return NULL;
  444. psize /= 4;
  445. onesize = na + ns;
  446. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  447. if (i == index) {
  448. if (size)
  449. *size = of_read_number(prop + na, ns);
  450. if (flags)
  451. *flags = bus->get_flags(prop);
  452. return prop;
  453. }
  454. return NULL;
  455. }
  456. EXPORT_SYMBOL(of_get_address);
  457. static int __of_address_to_resource(struct device_node *dev,
  458. const __be32 *addrp, u64 size, unsigned int flags,
  459. const char *name, struct resource *r)
  460. {
  461. u64 taddr;
  462. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  463. return -EINVAL;
  464. taddr = of_translate_address(dev, addrp);
  465. if (taddr == OF_BAD_ADDR)
  466. return -EINVAL;
  467. memset(r, 0, sizeof(struct resource));
  468. if (flags & IORESOURCE_IO) {
  469. unsigned long port;
  470. port = pci_address_to_pio(taddr);
  471. if (port == (unsigned long)-1)
  472. return -EINVAL;
  473. r->start = port;
  474. r->end = port + size - 1;
  475. } else {
  476. r->start = taddr;
  477. r->end = taddr + size - 1;
  478. }
  479. r->flags = flags;
  480. r->name = name ? name : dev->full_name;
  481. return 0;
  482. }
  483. /**
  484. * of_address_to_resource - Translate device tree address and return as resource
  485. *
  486. * Note that if your address is a PIO address, the conversion will fail if
  487. * the physical address can't be internally converted to an IO token with
  488. * pci_address_to_pio(), that is because it's either called to early or it
  489. * can't be matched to any host bridge IO space
  490. */
  491. int of_address_to_resource(struct device_node *dev, int index,
  492. struct resource *r)
  493. {
  494. const __be32 *addrp;
  495. u64 size;
  496. unsigned int flags;
  497. const char *name = NULL;
  498. addrp = of_get_address(dev, index, &size, &flags);
  499. if (addrp == NULL)
  500. return -EINVAL;
  501. /* Get optional "reg-names" property to add a name to a resource */
  502. of_property_read_string_index(dev, "reg-names", index, &name);
  503. return __of_address_to_resource(dev, addrp, size, flags, name, r);
  504. }
  505. EXPORT_SYMBOL_GPL(of_address_to_resource);
  506. struct device_node *of_find_matching_node_by_address(struct device_node *from,
  507. const struct of_device_id *matches,
  508. u64 base_address)
  509. {
  510. struct device_node *dn = of_find_matching_node(from, matches);
  511. struct resource res;
  512. while (dn) {
  513. if (of_address_to_resource(dn, 0, &res))
  514. continue;
  515. if (res.start == base_address)
  516. return dn;
  517. dn = of_find_matching_node(dn, matches);
  518. }
  519. return NULL;
  520. }
  521. /**
  522. * of_iomap - Maps the memory mapped IO for a given device_node
  523. * @device: the device whose io range will be mapped
  524. * @index: index of the io range
  525. *
  526. * Returns a pointer to the mapped memory
  527. */
  528. void __iomem *of_iomap(struct device_node *np, int index)
  529. {
  530. struct resource res;
  531. if (of_address_to_resource(np, index, &res))
  532. return NULL;
  533. return ioremap(res.start, resource_size(&res));
  534. }
  535. EXPORT_SYMBOL(of_iomap);