fdt_fixup.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. * fdt_fixup.c - Flat Device Tree parsing helper routines
  4. * Implement helper routines to parse FDT nodes on top of
  5. * libfdt for OpenSBI usage
  6. *
  7. * Copyright (C) 2020 Bin Meng <bmeng.cn@gmail.com>
  8. */
  9. #include <libfdt.h>
  10. #include <sbi/sbi_console.h>
  11. #include <sbi/sbi_domain.h>
  12. #include <sbi/sbi_math.h>
  13. #include <sbi/sbi_hart.h>
  14. #include <sbi/sbi_scratch.h>
  15. #include <sbi/sbi_string.h>
  16. #include <sbi_utils/fdt/fdt_fixup.h>
  17. #include <sbi_utils/fdt/fdt_pmu.h>
  18. #include <sbi_utils/fdt/fdt_helper.h>
  19. void fdt_cpu_fixup(void *fdt)
  20. {
  21. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  22. int err, cpu_offset, cpus_offset, len;
  23. const char *mmu_type;
  24. u32 hartid;
  25. err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
  26. if (err < 0)
  27. return;
  28. cpus_offset = fdt_path_offset(fdt, "/cpus");
  29. if (cpus_offset < 0)
  30. return;
  31. fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
  32. err = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
  33. if (err)
  34. continue;
  35. if (!fdt_node_is_enabled(fdt, cpu_offset))
  36. continue;
  37. /*
  38. * Disable a HART DT node if one of the following is true:
  39. * 1. The HART is not assigned to the current domain
  40. * 2. MMU is not available for the HART
  41. */
  42. mmu_type = fdt_getprop(fdt, cpu_offset, "mmu-type", &len);
  43. if (!sbi_domain_is_assigned_hart(dom, hartid) ||
  44. !mmu_type || !len)
  45. fdt_setprop_string(fdt, cpu_offset, "status",
  46. "disabled");
  47. }
  48. }
  49. static void fdt_domain_based_fixup_one(void *fdt, int nodeoff)
  50. {
  51. int rc;
  52. uint64_t reg_addr, reg_size;
  53. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  54. rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &reg_addr, &reg_size);
  55. if (rc < 0 || !reg_addr || !reg_size)
  56. return;
  57. if (!sbi_domain_check_addr(dom, reg_addr, dom->next_mode,
  58. SBI_DOMAIN_READ | SBI_DOMAIN_WRITE)) {
  59. rc = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
  60. if (rc < 0)
  61. return;
  62. fdt_setprop_string(fdt, nodeoff, "status", "disabled");
  63. }
  64. }
  65. static void fdt_fixup_node(void *fdt, const char *compatible)
  66. {
  67. int noff = 0;
  68. while ((noff = fdt_node_offset_by_compatible(fdt, noff,
  69. compatible)) >= 0)
  70. fdt_domain_based_fixup_one(fdt, noff);
  71. }
  72. void fdt_aplic_fixup(void *fdt)
  73. {
  74. fdt_fixup_node(fdt, "riscv,aplic");
  75. }
  76. void fdt_imsic_fixup(void *fdt)
  77. {
  78. fdt_fixup_node(fdt, "riscv,imsics");
  79. }
  80. void fdt_plic_fixup(void *fdt)
  81. {
  82. u32 *cells;
  83. int i, cells_count;
  84. int plic_off;
  85. plic_off = fdt_node_offset_by_compatible(fdt, 0, "sifive,plic-1.0.0");
  86. if (plic_off < 0) {
  87. plic_off = fdt_node_offset_by_compatible(fdt, 0, "riscv,plic0");
  88. if (plic_off < 0)
  89. return;
  90. }
  91. cells = (u32 *)fdt_getprop(fdt, plic_off,
  92. "interrupts-extended", &cells_count);
  93. if (!cells)
  94. return;
  95. cells_count = cells_count / sizeof(u32);
  96. if (!cells_count)
  97. return;
  98. for (i = 0; i < (cells_count / 2); i++) {
  99. if (fdt32_to_cpu(cells[2 * i + 1]) == IRQ_M_EXT)
  100. cells[2 * i + 1] = cpu_to_fdt32(0xffffffff);
  101. }
  102. }
  103. static int fdt_resv_memory_update_node(void *fdt, unsigned long addr,
  104. unsigned long size, int index,
  105. int parent, bool no_map)
  106. {
  107. int na = fdt_address_cells(fdt, 0);
  108. int ns = fdt_size_cells(fdt, 0);
  109. fdt32_t addr_high, addr_low;
  110. fdt32_t size_high, size_low;
  111. int subnode, err;
  112. fdt32_t reg[4];
  113. fdt32_t *val;
  114. char name[32];
  115. addr_high = (u64)addr >> 32;
  116. addr_low = addr;
  117. size_high = (u64)size >> 32;
  118. size_low = size;
  119. if (na > 1 && addr_high)
  120. sbi_snprintf(name, sizeof(name),
  121. "mmode_resv%d@%x,%x", index,
  122. addr_high, addr_low);
  123. else
  124. sbi_snprintf(name, sizeof(name),
  125. "mmode_resv%d@%x", index,
  126. addr_low);
  127. subnode = fdt_add_subnode(fdt, parent, name);
  128. if (subnode < 0)
  129. return subnode;
  130. if (no_map) {
  131. /*
  132. * Tell operating system not to create a virtual
  133. * mapping of the region as part of its standard
  134. * mapping of system memory.
  135. */
  136. err = fdt_setprop_empty(fdt, subnode, "no-map");
  137. if (err < 0)
  138. return err;
  139. }
  140. /* encode the <reg> property value */
  141. val = reg;
  142. if (na > 1)
  143. *val++ = cpu_to_fdt32(addr_high);
  144. *val++ = cpu_to_fdt32(addr_low);
  145. if (ns > 1)
  146. *val++ = cpu_to_fdt32(size_high);
  147. *val++ = cpu_to_fdt32(size_low);
  148. err = fdt_setprop(fdt, subnode, "reg", reg,
  149. (na + ns) * sizeof(fdt32_t));
  150. if (err < 0)
  151. return err;
  152. return 0;
  153. }
  154. /**
  155. * We use PMP to protect OpenSBI firmware to safe-guard it from buggy S-mode
  156. * software, see pmp_init() in lib/sbi/sbi_hart.c. The protected memory region
  157. * information needs to be conveyed to S-mode software (e.g.: operating system)
  158. * via some well-known method.
  159. *
  160. * With device tree, this can be done by inserting a child node of the reserved
  161. * memory node which is used to specify one or more regions of reserved memory.
  162. *
  163. * For the reserved memory node bindings, see Linux kernel documentation at
  164. * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
  165. *
  166. * Some additional memory spaces may be protected by platform codes via PMP as
  167. * well, and corresponding child nodes will be inserted.
  168. */
  169. int fdt_reserved_memory_fixup(void *fdt)
  170. {
  171. struct sbi_domain_memregion *reg;
  172. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  173. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  174. unsigned long addr, size;
  175. int err, parent, i;
  176. int na = fdt_address_cells(fdt, 0);
  177. int ns = fdt_size_cells(fdt, 0);
  178. /*
  179. * Expand the device tree to accommodate new node
  180. * by the following estimated size:
  181. *
  182. * Each PMP memory region entry occupies 64 bytes.
  183. * With 16 PMP memory regions we need 64 * 16 = 1024 bytes.
  184. */
  185. err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 1024);
  186. if (err < 0)
  187. return err;
  188. /* try to locate the reserved memory node */
  189. parent = fdt_path_offset(fdt, "/reserved-memory");
  190. if (parent < 0) {
  191. /* if such node does not exist, create one */
  192. parent = fdt_add_subnode(fdt, 0, "reserved-memory");
  193. if (parent < 0)
  194. return parent;
  195. /*
  196. * reserved-memory node has 3 required properties:
  197. * - #address-cells: the same value as the root node
  198. * - #size-cells: the same value as the root node
  199. * - ranges: should be empty
  200. */
  201. err = fdt_setprop_empty(fdt, parent, "ranges");
  202. if (err < 0)
  203. return err;
  204. err = fdt_setprop_u32(fdt, parent, "#size-cells", ns);
  205. if (err < 0)
  206. return err;
  207. err = fdt_setprop_u32(fdt, parent, "#address-cells", na);
  208. if (err < 0)
  209. return err;
  210. }
  211. /*
  212. * We assume the given device tree does not contain any memory region
  213. * child node protected by PMP. Normally PMP programming happens at
  214. * M-mode firmware. The memory space used by OpenSBI is protected.
  215. * Some additional memory spaces may be protected by domain memory
  216. * regions.
  217. *
  218. * With above assumption, we create child nodes directly.
  219. */
  220. i = 0;
  221. sbi_domain_for_each_memregion(dom, reg) {
  222. /* Ignore MMIO or READABLE or WRITABLE or EXECUTABLE regions */
  223. if (reg->flags & SBI_DOMAIN_MEMREGION_MMIO)
  224. continue;
  225. if (reg->flags & SBI_DOMAIN_MEMREGION_READABLE)
  226. continue;
  227. if (reg->flags & SBI_DOMAIN_MEMREGION_WRITEABLE)
  228. continue;
  229. if (reg->flags & SBI_DOMAIN_MEMREGION_EXECUTABLE)
  230. continue;
  231. addr = reg->base;
  232. size = 1UL << reg->order;
  233. fdt_resv_memory_update_node(fdt, addr, size, i, parent,
  234. (sbi_hart_pmp_count(scratch)) ? false : true);
  235. i++;
  236. }
  237. return 0;
  238. }
  239. int fdt_reserved_memory_nomap_fixup(void *fdt)
  240. {
  241. int parent, subnode;
  242. int err;
  243. /* Locate the reserved memory node */
  244. parent = fdt_path_offset(fdt, "/reserved-memory");
  245. if (parent < 0)
  246. return parent;
  247. fdt_for_each_subnode(subnode, fdt, parent) {
  248. /*
  249. * Tell operating system not to create a virtual
  250. * mapping of the region as part of its standard
  251. * mapping of system memory.
  252. */
  253. err = fdt_setprop_empty(fdt, subnode, "no-map");
  254. if (err < 0)
  255. return err;
  256. }
  257. return 0;
  258. }
  259. void fdt_fixups(void *fdt)
  260. {
  261. fdt_aplic_fixup(fdt);
  262. fdt_imsic_fixup(fdt);
  263. fdt_plic_fixup(fdt);
  264. fdt_reserved_memory_fixup(fdt);
  265. fdt_pmu_fixup(fdt);
  266. }