address.c 16 KB

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