fdt_fixup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/sbi_error.h>
  17. #include <sbi_utils/fdt/fdt_fixup.h>
  18. #include <sbi_utils/fdt/fdt_pmu.h>
  19. #include <sbi_utils/fdt/fdt_helper.h>
  20. int fdt_add_cpu_idle_states(void *fdt, const struct sbi_cpu_idle_state *state)
  21. {
  22. int cpu_node, cpus_node, err, idle_states_node;
  23. uint32_t count, phandle;
  24. err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 1024);
  25. if (err < 0)
  26. return err;
  27. err = fdt_find_max_phandle(fdt, &phandle);
  28. phandle++;
  29. if (err < 0)
  30. return err;
  31. cpus_node = fdt_path_offset(fdt, "/cpus");
  32. if (cpus_node < 0)
  33. return cpus_node;
  34. /* Do nothing if the idle-states node already exists. */
  35. idle_states_node = fdt_subnode_offset(fdt, cpus_node, "idle-states");
  36. if (idle_states_node >= 0)
  37. return 0;
  38. /* Create the idle-states node and its child nodes. */
  39. idle_states_node = fdt_add_subnode(fdt, cpus_node, "idle-states");
  40. if (idle_states_node < 0)
  41. return idle_states_node;
  42. for (count = 0; state->name; count++, phandle++, state++) {
  43. int idle_state_node;
  44. idle_state_node = fdt_add_subnode(fdt, idle_states_node,
  45. state->name);
  46. if (idle_state_node < 0)
  47. return idle_state_node;
  48. fdt_setprop_string(fdt, idle_state_node, "compatible",
  49. "riscv,idle-state");
  50. fdt_setprop_u32(fdt, idle_state_node,
  51. "riscv,sbi-suspend-param",
  52. state->suspend_param);
  53. if (state->local_timer_stop)
  54. fdt_setprop_empty(fdt, idle_state_node,
  55. "local-timer-stop");
  56. fdt_setprop_u32(fdt, idle_state_node, "entry-latency-us",
  57. state->entry_latency_us);
  58. fdt_setprop_u32(fdt, idle_state_node, "exit-latency-us",
  59. state->exit_latency_us);
  60. fdt_setprop_u32(fdt, idle_state_node, "min-residency-us",
  61. state->min_residency_us);
  62. if (state->wakeup_latency_us)
  63. fdt_setprop_u32(fdt, idle_state_node,
  64. "wakeup-latency-us",
  65. state->wakeup_latency_us);
  66. fdt_setprop_u32(fdt, idle_state_node, "phandle", phandle);
  67. }
  68. if (count == 0)
  69. return 0;
  70. /* Link each cpu node to the idle state nodes. */
  71. fdt_for_each_subnode(cpu_node, fdt, cpus_node) {
  72. const char *device_type;
  73. fdt32_t *value;
  74. /* Only process child nodes with device_type = "cpu". */
  75. device_type = fdt_getprop(fdt, cpu_node, "device_type", NULL);
  76. if (!device_type || strcmp(device_type, "cpu"))
  77. continue;
  78. /* Allocate space for the list of phandles. */
  79. err = fdt_setprop_placeholder(fdt, cpu_node, "cpu-idle-states",
  80. count * sizeof(phandle),
  81. (void **)&value);
  82. if (err < 0)
  83. return err;
  84. /* Fill in the phandles of the idle state nodes. */
  85. for (uint32_t i = 0; i < count; ++i)
  86. value[i] = cpu_to_fdt32(phandle - count + i);
  87. }
  88. return 0;
  89. }
  90. void fdt_cpu_fixup(void *fdt)
  91. {
  92. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  93. int err, cpu_offset, cpus_offset, len;
  94. const char *mmu_type;
  95. u32 hartid;
  96. err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
  97. if (err < 0)
  98. return;
  99. cpus_offset = fdt_path_offset(fdt, "/cpus");
  100. if (cpus_offset < 0)
  101. return;
  102. fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
  103. err = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
  104. if (err)
  105. continue;
  106. if (!fdt_node_is_enabled(fdt, cpu_offset))
  107. continue;
  108. /*
  109. * Disable a HART DT node if one of the following is true:
  110. * 1. The HART is not assigned to the current domain
  111. * 2. MMU is not available for the HART
  112. */
  113. mmu_type = fdt_getprop(fdt, cpu_offset, "mmu-type", &len);
  114. if (!sbi_domain_is_assigned_hart(dom, hartid) ||
  115. !mmu_type || !len)
  116. fdt_setprop_string(fdt, cpu_offset, "status",
  117. "disabled");
  118. }
  119. }
  120. static void fdt_domain_based_fixup_one(void *fdt, int nodeoff)
  121. {
  122. int rc;
  123. uint64_t reg_addr, reg_size;
  124. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  125. rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &reg_addr, &reg_size);
  126. if (rc < 0 || !reg_addr || !reg_size)
  127. return;
  128. if (!sbi_domain_check_addr(dom, reg_addr, dom->next_mode,
  129. SBI_DOMAIN_READ | SBI_DOMAIN_WRITE)) {
  130. rc = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
  131. if (rc < 0)
  132. return;
  133. fdt_setprop_string(fdt, nodeoff, "status", "disabled");
  134. }
  135. }
  136. static void fdt_fixup_node(void *fdt, const char *compatible)
  137. {
  138. int noff = 0;
  139. while ((noff = fdt_node_offset_by_compatible(fdt, noff,
  140. compatible)) >= 0)
  141. fdt_domain_based_fixup_one(fdt, noff);
  142. }
  143. void fdt_aplic_fixup(void *fdt)
  144. {
  145. fdt_fixup_node(fdt, "riscv,aplic");
  146. }
  147. void fdt_imsic_fixup(void *fdt)
  148. {
  149. fdt_fixup_node(fdt, "riscv,imsics");
  150. }
  151. void fdt_plic_fixup(void *fdt)
  152. {
  153. u32 *cells;
  154. int i, cells_count;
  155. int plic_off;
  156. plic_off = fdt_node_offset_by_compatible(fdt, 0, "sifive,plic-1.0.0");
  157. if (plic_off < 0) {
  158. plic_off = fdt_node_offset_by_compatible(fdt, 0, "riscv,plic0");
  159. if (plic_off < 0)
  160. return;
  161. }
  162. cells = (u32 *)fdt_getprop(fdt, plic_off,
  163. "interrupts-extended", &cells_count);
  164. if (!cells)
  165. return;
  166. cells_count = cells_count / sizeof(u32);
  167. if (!cells_count)
  168. return;
  169. for (i = 0; i < (cells_count / 2); i++) {
  170. if (fdt32_to_cpu(cells[2 * i + 1]) == IRQ_M_EXT)
  171. cells[2 * i + 1] = cpu_to_fdt32(0xffffffff);
  172. }
  173. }
  174. static int fdt_resv_memory_update_node(void *fdt, unsigned long addr,
  175. unsigned long size, int index,
  176. int parent, bool no_map)
  177. {
  178. int na = fdt_address_cells(fdt, 0);
  179. int ns = fdt_size_cells(fdt, 0);
  180. fdt32_t addr_high, addr_low;
  181. fdt32_t size_high, size_low;
  182. int subnode, err;
  183. fdt32_t reg[4];
  184. fdt32_t *val;
  185. char name[32];
  186. addr_high = (u64)addr >> 32;
  187. addr_low = addr;
  188. size_high = (u64)size >> 32;
  189. size_low = size;
  190. if (na > 1 && addr_high)
  191. sbi_snprintf(name, sizeof(name),
  192. "mmode_resv%d@%x,%x", index,
  193. addr_high, addr_low);
  194. else
  195. sbi_snprintf(name, sizeof(name),
  196. "mmode_resv%d@%x", index,
  197. addr_low);
  198. subnode = fdt_add_subnode(fdt, parent, name);
  199. if (subnode < 0)
  200. return subnode;
  201. if (no_map) {
  202. /*
  203. * Tell operating system not to create a virtual
  204. * mapping of the region as part of its standard
  205. * mapping of system memory.
  206. */
  207. err = fdt_setprop_empty(fdt, subnode, "no-map");
  208. if (err < 0)
  209. return err;
  210. }
  211. /* encode the <reg> property value */
  212. val = reg;
  213. if (na > 1)
  214. *val++ = cpu_to_fdt32(addr_high);
  215. *val++ = cpu_to_fdt32(addr_low);
  216. if (ns > 1)
  217. *val++ = cpu_to_fdt32(size_high);
  218. *val++ = cpu_to_fdt32(size_low);
  219. err = fdt_setprop(fdt, subnode, "reg", reg,
  220. (na + ns) * sizeof(fdt32_t));
  221. if (err < 0)
  222. return err;
  223. return 0;
  224. }
  225. /**
  226. * We use PMP to protect OpenSBI firmware to safe-guard it from buggy S-mode
  227. * software, see pmp_init() in lib/sbi/sbi_hart.c. The protected memory region
  228. * information needs to be conveyed to S-mode software (e.g.: operating system)
  229. * via some well-known method.
  230. *
  231. * With device tree, this can be done by inserting a child node of the reserved
  232. * memory node which is used to specify one or more regions of reserved memory.
  233. *
  234. * For the reserved memory node bindings, see Linux kernel documentation at
  235. * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
  236. *
  237. * Some additional memory spaces may be protected by platform codes via PMP as
  238. * well, and corresponding child nodes will be inserted.
  239. */
  240. int fdt_reserved_memory_fixup(void *fdt)
  241. {
  242. struct sbi_domain_memregion *reg;
  243. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  244. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  245. unsigned long filtered_base[PMP_COUNT] = { 0 };
  246. unsigned char filtered_order[PMP_COUNT] = { 0 };
  247. unsigned long addr, size;
  248. int err, parent, i, j;
  249. int na = fdt_address_cells(fdt, 0);
  250. int ns = fdt_size_cells(fdt, 0);
  251. /*
  252. * Expand the device tree to accommodate new node
  253. * by the following estimated size:
  254. *
  255. * Each PMP memory region entry occupies 64 bytes.
  256. * With 16 PMP memory regions we need 64 * 16 = 1024 bytes.
  257. */
  258. err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 1024);
  259. if (err < 0)
  260. return err;
  261. /* try to locate the reserved memory node */
  262. parent = fdt_path_offset(fdt, "/reserved-memory");
  263. if (parent < 0) {
  264. /* if such node does not exist, create one */
  265. parent = fdt_add_subnode(fdt, 0, "reserved-memory");
  266. if (parent < 0)
  267. return parent;
  268. /*
  269. * reserved-memory node has 3 required properties:
  270. * - #address-cells: the same value as the root node
  271. * - #size-cells: the same value as the root node
  272. * - ranges: should be empty
  273. */
  274. err = fdt_setprop_empty(fdt, parent, "ranges");
  275. if (err < 0)
  276. return err;
  277. err = fdt_setprop_u32(fdt, parent, "#size-cells", ns);
  278. if (err < 0)
  279. return err;
  280. err = fdt_setprop_u32(fdt, parent, "#address-cells", na);
  281. if (err < 0)
  282. return err;
  283. }
  284. /*
  285. * We assume the given device tree does not contain any memory region
  286. * child node protected by PMP. Normally PMP programming happens at
  287. * M-mode firmware. The memory space used by OpenSBI is protected.
  288. * Some additional memory spaces may be protected by domain memory
  289. * regions.
  290. *
  291. * With above assumption, we create child nodes directly.
  292. */
  293. i = 0;
  294. sbi_domain_for_each_memregion(dom, reg) {
  295. /* Ignore MMIO or READABLE or WRITABLE or EXECUTABLE regions */
  296. if (reg->flags & SBI_DOMAIN_MEMREGION_MMIO)
  297. continue;
  298. if (reg->flags & SBI_DOMAIN_MEMREGION_SU_READABLE)
  299. continue;
  300. if (reg->flags & SBI_DOMAIN_MEMREGION_SU_WRITABLE)
  301. continue;
  302. if (reg->flags & SBI_DOMAIN_MEMREGION_SU_EXECUTABLE)
  303. continue;
  304. if (i > PMP_COUNT) {
  305. sbi_printf("%s: Too many memory regions to fixup.\n",
  306. __func__);
  307. return SBI_ENOSPC;
  308. }
  309. bool overlap = false;
  310. addr = reg->base;
  311. for (j = 0; j < i; j++) {
  312. if (addr == filtered_base[j]
  313. && filtered_order[j] < reg->order) {
  314. overlap = true;
  315. filtered_order[j] = reg->order;
  316. break;
  317. }
  318. }
  319. if (!overlap) {
  320. filtered_base[i] = reg->base;
  321. filtered_order[i] = reg->order;
  322. i++;
  323. }
  324. }
  325. for (j = 0; j < i; j++) {
  326. addr = filtered_base[j];
  327. size = 1UL << filtered_order[j];
  328. fdt_resv_memory_update_node(fdt, addr, size, j, parent,
  329. (sbi_hart_pmp_count(scratch))
  330. ? false : true);
  331. }
  332. return 0;
  333. }
  334. int fdt_reserved_memory_nomap_fixup(void *fdt)
  335. {
  336. int parent, subnode;
  337. int err;
  338. /* Locate the reserved memory node */
  339. parent = fdt_path_offset(fdt, "/reserved-memory");
  340. if (parent < 0)
  341. return parent;
  342. fdt_for_each_subnode(subnode, fdt, parent) {
  343. /*
  344. * Tell operating system not to create a virtual
  345. * mapping of the region as part of its standard
  346. * mapping of system memory.
  347. */
  348. err = fdt_setprop_empty(fdt, subnode, "no-map");
  349. if (err < 0)
  350. return err;
  351. }
  352. return 0;
  353. }
  354. void fdt_fixups(void *fdt)
  355. {
  356. fdt_aplic_fixup(fdt);
  357. fdt_imsic_fixup(fdt);
  358. fdt_plic_fixup(fdt);
  359. fdt_reserved_memory_fixup(fdt);
  360. fdt_pmu_fixup(fdt);
  361. }