fdt_address.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * FDT Address translation based on u-boot fdt_support.c which in turn was
  4. * based on the kernel unflattened DT address translation code.
  5. *
  6. * (C) Copyright 2007
  7. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  8. *
  9. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  10. */
  11. #define pr_fmt(fmt) "OF: fdt: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/libfdt.h>
  14. #include <linux/of.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/sizes.h>
  17. /* Uncomment me to enable of_dump_addr() debugging output */
  18. // #define DEBUG
  19. #include "of_private.h"
  20. /* Callbacks for bus specific translators */
  21. struct of_bus {
  22. void (*count_cells)(const void *blob, int parentoffset,
  23. int *addrc, int *sizec);
  24. u64 (*map)(__be32 *addr, const __be32 *range,
  25. int na, int ns, int pna);
  26. int (*translate)(__be32 *addr, u64 offset, int na);
  27. };
  28. /* Default translator (generic bus) */
  29. static void __init fdt_bus_default_count_cells(const void *blob, int parentoffset,
  30. int *addrc, int *sizec)
  31. {
  32. const __be32 *prop;
  33. if (addrc) {
  34. prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
  35. if (prop)
  36. *addrc = be32_to_cpup(prop);
  37. else
  38. *addrc = -1;
  39. }
  40. if (sizec) {
  41. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  42. if (prop)
  43. *sizec = be32_to_cpup(prop);
  44. else
  45. *sizec = -1;
  46. }
  47. }
  48. static u64 __init fdt_bus_default_map(__be32 *addr, const __be32 *range,
  49. int na, int ns, int pna)
  50. {
  51. u64 cp, s, da;
  52. cp = of_read_number(range, na);
  53. s = of_read_number(range + na + pna, ns);
  54. da = of_read_number(addr, na);
  55. pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
  56. cp, s, da);
  57. if (da < cp || da >= (cp + s))
  58. return OF_BAD_ADDR;
  59. return da - cp;
  60. }
  61. static int __init fdt_bus_default_translate(__be32 *addr, u64 offset, int na)
  62. {
  63. u64 a = of_read_number(addr, na);
  64. memset(addr, 0, na * 4);
  65. a += offset;
  66. if (na > 1)
  67. addr[na - 2] = cpu_to_fdt32(a >> 32);
  68. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  69. return 0;
  70. }
  71. /* Array of bus specific translators */
  72. static const struct of_bus of_busses[] __initconst = {
  73. /* Default */
  74. {
  75. .count_cells = fdt_bus_default_count_cells,
  76. .map = fdt_bus_default_map,
  77. .translate = fdt_bus_default_translate,
  78. },
  79. };
  80. static int __init fdt_translate_one(const void *blob, int parent,
  81. const struct of_bus *bus,
  82. const struct of_bus *pbus, __be32 *addr,
  83. int na, int ns, int pna, const char *rprop)
  84. {
  85. const __be32 *ranges;
  86. int rlen;
  87. int rone;
  88. u64 offset = OF_BAD_ADDR;
  89. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  90. if (!ranges)
  91. return 1;
  92. if (rlen == 0) {
  93. offset = of_read_number(addr, na);
  94. memset(addr, 0, pna * 4);
  95. pr_debug("empty ranges, 1:1 translation\n");
  96. goto finish;
  97. }
  98. pr_debug("walking ranges...\n");
  99. /* Now walk through the ranges */
  100. rlen /= 4;
  101. rone = na + pna + ns;
  102. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  103. offset = bus->map(addr, ranges, na, ns, pna);
  104. if (offset != OF_BAD_ADDR)
  105. break;
  106. }
  107. if (offset == OF_BAD_ADDR) {
  108. pr_debug("not found !\n");
  109. return 1;
  110. }
  111. memcpy(addr, ranges + na, 4 * pna);
  112. finish:
  113. of_dump_addr("parent translation for:", addr, pna);
  114. pr_debug("with offset: %llx\n", offset);
  115. /* Translate it into parent bus space */
  116. return pbus->translate(addr, offset, pna);
  117. }
  118. /*
  119. * Translate an address from the device-tree into a CPU physical address,
  120. * this walks up the tree and applies the various bus mappings on the
  121. * way.
  122. *
  123. * Note: We consider that crossing any level with #size-cells == 0 to mean
  124. * that translation is impossible (that is we are not dealing with a value
  125. * that can be mapped to a cpu physical address). This is not really specified
  126. * that way, but this is traditionally the way IBM at least do things
  127. */
  128. static u64 __init fdt_translate_address(const void *blob, int node_offset)
  129. {
  130. int parent, len;
  131. const struct of_bus *bus, *pbus;
  132. const __be32 *reg;
  133. __be32 addr[OF_MAX_ADDR_CELLS];
  134. int na, ns, pna, pns;
  135. u64 result = OF_BAD_ADDR;
  136. pr_debug("** translation for device %s **\n",
  137. fdt_get_name(blob, node_offset, NULL));
  138. reg = fdt_getprop(blob, node_offset, "reg", &len);
  139. if (!reg) {
  140. pr_err("warning: device tree node '%s' has no address.\n",
  141. fdt_get_name(blob, node_offset, NULL));
  142. goto bail;
  143. }
  144. /* Get parent & match bus type */
  145. parent = fdt_parent_offset(blob, node_offset);
  146. if (parent < 0)
  147. goto bail;
  148. bus = &of_busses[0];
  149. /* Cound address cells & copy address locally */
  150. bus->count_cells(blob, parent, &na, &ns);
  151. if (!OF_CHECK_COUNTS(na, ns)) {
  152. pr_err("Bad cell count for %s\n",
  153. fdt_get_name(blob, node_offset, NULL));
  154. goto bail;
  155. }
  156. memcpy(addr, reg, na * 4);
  157. pr_debug("bus (na=%d, ns=%d) on %s\n",
  158. na, ns, fdt_get_name(blob, parent, NULL));
  159. of_dump_addr("translating address:", addr, na);
  160. /* Translate */
  161. for (;;) {
  162. /* Switch to parent bus */
  163. node_offset = parent;
  164. parent = fdt_parent_offset(blob, node_offset);
  165. /* If root, we have finished */
  166. if (parent < 0) {
  167. pr_debug("reached root node\n");
  168. result = of_read_number(addr, na);
  169. break;
  170. }
  171. /* Get new parent bus and counts */
  172. pbus = &of_busses[0];
  173. pbus->count_cells(blob, parent, &pna, &pns);
  174. if (!OF_CHECK_COUNTS(pna, pns)) {
  175. pr_err("Bad cell count for %s\n",
  176. fdt_get_name(blob, node_offset, NULL));
  177. break;
  178. }
  179. pr_debug("parent bus (na=%d, ns=%d) on %s\n",
  180. pna, pns, fdt_get_name(blob, parent, NULL));
  181. /* Apply bus translation */
  182. if (fdt_translate_one(blob, node_offset, bus, pbus,
  183. addr, na, ns, pna, "ranges"))
  184. break;
  185. /* Complete the move up one level */
  186. na = pna;
  187. ns = pns;
  188. bus = pbus;
  189. of_dump_addr("one level translation:", addr, na);
  190. }
  191. bail:
  192. return result;
  193. }
  194. /**
  195. * of_flat_dt_translate_address - translate DT addr into CPU phys addr
  196. * @node: node in the flat blob
  197. */
  198. u64 __init of_flat_dt_translate_address(unsigned long node)
  199. {
  200. return fdt_translate_address(initial_boot_params, node);
  201. }