fdt_fixup.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*
  7. * Contains generic routines to fix up the device tree blob passed on to
  8. * payloads like BL32 and BL33 (and further down the boot chain).
  9. * This allows to easily add PSCI nodes, when the original DT does not have
  10. * it or advertises another method.
  11. * Also it supports to add reserved memory nodes to describe memory that
  12. * is used by the secure world, so that non-secure software avoids using
  13. * that.
  14. */
  15. #include <string.h>
  16. #include <libfdt.h>
  17. #include <common/debug.h>
  18. #include <drivers/console.h>
  19. #include <lib/psci/psci.h>
  20. #include <common/fdt_fixup.h>
  21. static int append_psci_compatible(void *fdt, int offs, const char *str)
  22. {
  23. return fdt_appendprop(fdt, offs, "compatible", str, strlen(str) + 1);
  24. }
  25. /*
  26. * Those defines are for PSCI v0.1 legacy clients, which we expect to use
  27. * the same execution state (AArch32/AArch64) as TF-A.
  28. * Kernels running in AArch32 on an AArch64 TF-A should use PSCI v0.2.
  29. */
  30. #ifdef __aarch64__
  31. #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH64
  32. #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH64
  33. #else
  34. #define PSCI_CPU_SUSPEND_FNID PSCI_CPU_SUSPEND_AARCH32
  35. #define PSCI_CPU_ON_FNID PSCI_CPU_ON_AARCH32
  36. #endif
  37. /*******************************************************************************
  38. * dt_add_psci_node() - Add a PSCI node into an existing device tree
  39. * @fdt: pointer to the device tree blob in memory
  40. *
  41. * Add a device tree node describing PSCI into the root level of an existing
  42. * device tree blob in memory.
  43. * This will add v0.1, v0.2 and v1.0 compatible strings and the standard
  44. * function IDs for v0.1 compatibility.
  45. * An existing PSCI node will not be touched, the function will return success
  46. * in this case. This function will not touch the /cpus enable methods, use
  47. * dt_add_psci_cpu_enable_methods() for that.
  48. *
  49. * Return: 0 on success, -1 otherwise.
  50. ******************************************************************************/
  51. int dt_add_psci_node(void *fdt)
  52. {
  53. int offs;
  54. if (fdt_path_offset(fdt, "/psci") >= 0) {
  55. WARN("PSCI Device Tree node already exists!\n");
  56. return 0;
  57. }
  58. offs = fdt_path_offset(fdt, "/");
  59. if (offs < 0)
  60. return -1;
  61. offs = fdt_add_subnode(fdt, offs, "psci");
  62. if (offs < 0)
  63. return -1;
  64. if (append_psci_compatible(fdt, offs, "arm,psci-1.0"))
  65. return -1;
  66. if (append_psci_compatible(fdt, offs, "arm,psci-0.2"))
  67. return -1;
  68. if (append_psci_compatible(fdt, offs, "arm,psci"))
  69. return -1;
  70. if (fdt_setprop_string(fdt, offs, "method", "smc"))
  71. return -1;
  72. if (fdt_setprop_u32(fdt, offs, "cpu_suspend", PSCI_CPU_SUSPEND_FNID))
  73. return -1;
  74. if (fdt_setprop_u32(fdt, offs, "cpu_off", PSCI_CPU_OFF))
  75. return -1;
  76. if (fdt_setprop_u32(fdt, offs, "cpu_on", PSCI_CPU_ON_FNID))
  77. return -1;
  78. return 0;
  79. }
  80. /*
  81. * Find the first subnode that has a "device_type" property with the value
  82. * "cpu" and which's enable-method is not "psci" (yet).
  83. * Returns 0 if no such subnode is found, so all have already been patched
  84. * or none have to be patched in the first place.
  85. * Returns 1 if *one* such subnode has been found and successfully changed
  86. * to "psci".
  87. * Returns -1 on error.
  88. *
  89. * Call in a loop until it returns 0. Recalculate the node offset after
  90. * it has returned 1.
  91. */
  92. static int dt_update_one_cpu_node(void *fdt, int offset)
  93. {
  94. int offs;
  95. /* Iterate over all subnodes to find those with device_type = "cpu". */
  96. for (offs = fdt_first_subnode(fdt, offset); offs >= 0;
  97. offs = fdt_next_subnode(fdt, offs)) {
  98. const char *prop;
  99. int len;
  100. prop = fdt_getprop(fdt, offs, "device_type", &len);
  101. if (!prop)
  102. continue;
  103. if (memcmp(prop, "cpu", 4) != 0 || len != 4)
  104. continue;
  105. /* Ignore any nodes which already use "psci". */
  106. prop = fdt_getprop(fdt, offs, "enable-method", &len);
  107. if (prop && memcmp(prop, "psci", 5) == 0 && len == 5)
  108. continue;
  109. if (fdt_setprop_string(fdt, offs, "enable-method", "psci"))
  110. return -1;
  111. /*
  112. * Subnode found and patched.
  113. * Restart to accommodate potentially changed offsets.
  114. */
  115. return 1;
  116. }
  117. if (offs == -FDT_ERR_NOTFOUND)
  118. return 0;
  119. return offs;
  120. }
  121. /*******************************************************************************
  122. * dt_add_psci_cpu_enable_methods() - switch CPU nodes in DT to use PSCI
  123. * @fdt: pointer to the device tree blob in memory
  124. *
  125. * Iterate over all CPU device tree nodes (/cpus/cpu@x) in memory to change
  126. * the enable-method to PSCI. This will add the enable-method properties, if
  127. * required, or will change existing properties to read "psci".
  128. *
  129. * Return: 0 on success, or a negative error value otherwise.
  130. ******************************************************************************/
  131. int dt_add_psci_cpu_enable_methods(void *fdt)
  132. {
  133. int offs, ret;
  134. do {
  135. offs = fdt_path_offset(fdt, "/cpus");
  136. if (offs < 0)
  137. return offs;
  138. ret = dt_update_one_cpu_node(fdt, offs);
  139. } while (ret > 0);
  140. return ret;
  141. }
  142. #define HIGH_BITS(x) ((sizeof(x) > 4) ? ((x) >> 32) : (typeof(x))0)
  143. /*******************************************************************************
  144. * fdt_add_reserved_memory() - reserve (secure) memory regions in DT
  145. * @dtb: pointer to the device tree blob in memory
  146. * @node_name: name of the subnode to be used
  147. * @base: physical base address of the reserved region
  148. * @size: size of the reserved region
  149. *
  150. * Add a region of memory to the /reserved-memory node in a device tree in
  151. * memory, creating that node if required. Each region goes into a subnode
  152. * of that node and has a @node_name, a @base address and a @size.
  153. * This will prevent any device tree consumer from using that memory. It
  154. * can be used to announce secure memory regions, as it adds the "no-map"
  155. * property to prevent mapping and speculative operations on that region.
  156. *
  157. * See reserved-memory/reserved-memory.txt in the (Linux kernel) DT binding
  158. * documentation for details.
  159. *
  160. * Return: 0 on success, a negative error value otherwise.
  161. ******************************************************************************/
  162. int fdt_add_reserved_memory(void *dtb, const char *node_name,
  163. uintptr_t base, size_t size)
  164. {
  165. int offs = fdt_path_offset(dtb, "/reserved-memory");
  166. uint32_t addresses[3];
  167. if (offs < 0) { /* create if not existing yet */
  168. offs = fdt_add_subnode(dtb, 0, "reserved-memory");
  169. if (offs < 0)
  170. return offs;
  171. fdt_setprop_u32(dtb, offs, "#address-cells", 2);
  172. fdt_setprop_u32(dtb, offs, "#size-cells", 1);
  173. fdt_setprop(dtb, offs, "ranges", NULL, 0);
  174. }
  175. addresses[0] = cpu_to_fdt32(HIGH_BITS(base));
  176. addresses[1] = cpu_to_fdt32(base & 0xffffffff);
  177. addresses[2] = cpu_to_fdt32(size & 0xffffffff);
  178. offs = fdt_add_subnode(dtb, offs, node_name);
  179. fdt_setprop(dtb, offs, "no-map", NULL, 0);
  180. fdt_setprop(dtb, offs, "reg", addresses, 12);
  181. return 0;
  182. }