setup.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4. * Chen Liqin <[email protected]>
  5. * Lennox Wu <[email protected]>
  6. * Copyright (C) 2012 Regents of the University of California
  7. * Copyright (C) 2020 FORTH-ICS/CARV
  8. * Nick Kossifidis <[email protected]>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/mm.h>
  12. #include <linux/memblock.h>
  13. #include <linux/sched.h>
  14. #include <linux/console.h>
  15. #include <linux/screen_info.h>
  16. #include <linux/of_fdt.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/sched/task.h>
  19. #include <linux/smp.h>
  20. #include <linux/efi.h>
  21. #include <linux/crash_dump.h>
  22. #include <asm/alternative.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/cpu_ops.h>
  25. #include <asm/early_ioremap.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/setup.h>
  28. #include <asm/set_memory.h>
  29. #include <asm/sections.h>
  30. #include <asm/sbi.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/thread_info.h>
  33. #include <asm/kasan.h>
  34. #include <asm/efi.h>
  35. #include "head.h"
  36. #if defined(CONFIG_DUMMY_CONSOLE) || defined(CONFIG_EFI)
  37. struct screen_info screen_info __section(".data") = {
  38. .orig_video_lines = 30,
  39. .orig_video_cols = 80,
  40. .orig_video_mode = 0,
  41. .orig_video_ega_bx = 0,
  42. .orig_video_isVGA = 1,
  43. .orig_video_points = 8
  44. };
  45. #endif
  46. /*
  47. * The lucky hart to first increment this variable will boot the other cores.
  48. * This is used before the kernel initializes the BSS so it can't be in the
  49. * BSS.
  50. */
  51. atomic_t hart_lottery __section(".sdata")
  52. #ifdef CONFIG_XIP_KERNEL
  53. = ATOMIC_INIT(0xC001BEEF)
  54. #endif
  55. ;
  56. unsigned long boot_cpu_hartid;
  57. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  58. /*
  59. * Place kernel memory regions on the resource tree so that
  60. * kexec-tools can retrieve them from /proc/iomem. While there
  61. * also add "System RAM" regions for compatibility with other
  62. * archs, and the rest of the known regions for completeness.
  63. */
  64. static struct resource kimage_res = { .name = "Kernel image", };
  65. static struct resource code_res = { .name = "Kernel code", };
  66. static struct resource data_res = { .name = "Kernel data", };
  67. static struct resource rodata_res = { .name = "Kernel rodata", };
  68. static struct resource bss_res = { .name = "Kernel bss", };
  69. #ifdef CONFIG_CRASH_DUMP
  70. static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
  71. #endif
  72. static int __init add_resource(struct resource *parent,
  73. struct resource *res)
  74. {
  75. int ret = 0;
  76. ret = insert_resource(parent, res);
  77. if (ret < 0) {
  78. pr_err("Failed to add a %s resource at %llx\n",
  79. res->name, (unsigned long long) res->start);
  80. return ret;
  81. }
  82. return 1;
  83. }
  84. static int __init add_kernel_resources(void)
  85. {
  86. int ret = 0;
  87. /*
  88. * The memory region of the kernel image is continuous and
  89. * was reserved on setup_bootmem, register it here as a
  90. * resource, with the various segments of the image as
  91. * child nodes.
  92. */
  93. code_res.start = __pa_symbol(_text);
  94. code_res.end = __pa_symbol(_etext) - 1;
  95. code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  96. rodata_res.start = __pa_symbol(__start_rodata);
  97. rodata_res.end = __pa_symbol(__end_rodata) - 1;
  98. rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  99. data_res.start = __pa_symbol(_data);
  100. data_res.end = __pa_symbol(_edata) - 1;
  101. data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  102. bss_res.start = __pa_symbol(__bss_start);
  103. bss_res.end = __pa_symbol(__bss_stop) - 1;
  104. bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  105. kimage_res.start = code_res.start;
  106. kimage_res.end = bss_res.end;
  107. kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  108. ret = add_resource(&iomem_resource, &kimage_res);
  109. if (ret < 0)
  110. return ret;
  111. ret = add_resource(&kimage_res, &code_res);
  112. if (ret < 0)
  113. return ret;
  114. ret = add_resource(&kimage_res, &rodata_res);
  115. if (ret < 0)
  116. return ret;
  117. ret = add_resource(&kimage_res, &data_res);
  118. if (ret < 0)
  119. return ret;
  120. ret = add_resource(&kimage_res, &bss_res);
  121. return ret;
  122. }
  123. static void __init init_resources(void)
  124. {
  125. struct memblock_region *region = NULL;
  126. struct resource *res = NULL;
  127. struct resource *mem_res = NULL;
  128. size_t mem_res_sz = 0;
  129. int num_resources = 0, res_idx = 0;
  130. int ret = 0;
  131. /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
  132. num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
  133. res_idx = num_resources - 1;
  134. mem_res_sz = num_resources * sizeof(*mem_res);
  135. mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
  136. if (!mem_res)
  137. panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
  138. /*
  139. * Start by adding the reserved regions, if they overlap
  140. * with /memory regions, insert_resource later on will take
  141. * care of it.
  142. */
  143. ret = add_kernel_resources();
  144. if (ret < 0)
  145. goto error;
  146. #ifdef CONFIG_KEXEC_CORE
  147. if (crashk_res.start != crashk_res.end) {
  148. ret = add_resource(&iomem_resource, &crashk_res);
  149. if (ret < 0)
  150. goto error;
  151. }
  152. #endif
  153. #ifdef CONFIG_CRASH_DUMP
  154. if (elfcorehdr_size > 0) {
  155. elfcorehdr_res.start = elfcorehdr_addr;
  156. elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
  157. elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  158. add_resource(&iomem_resource, &elfcorehdr_res);
  159. }
  160. #endif
  161. for_each_reserved_mem_region(region) {
  162. res = &mem_res[res_idx--];
  163. res->name = "Reserved";
  164. res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
  165. res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
  166. res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
  167. /*
  168. * Ignore any other reserved regions within
  169. * system memory.
  170. */
  171. if (memblock_is_memory(res->start)) {
  172. /* Re-use this pre-allocated resource */
  173. res_idx++;
  174. continue;
  175. }
  176. ret = add_resource(&iomem_resource, res);
  177. if (ret < 0)
  178. goto error;
  179. }
  180. /* Add /memory regions to the resource tree */
  181. for_each_mem_region(region) {
  182. res = &mem_res[res_idx--];
  183. if (unlikely(memblock_is_nomap(region))) {
  184. res->name = "Reserved";
  185. res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
  186. } else {
  187. res->name = "System RAM";
  188. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  189. }
  190. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  191. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  192. ret = add_resource(&iomem_resource, res);
  193. if (ret < 0)
  194. goto error;
  195. }
  196. /* Clean-up any unused pre-allocated resources */
  197. if (res_idx >= 0)
  198. memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
  199. return;
  200. error:
  201. /* Better an empty resource tree than an inconsistent one */
  202. release_child_resources(&iomem_resource);
  203. memblock_free(mem_res, mem_res_sz);
  204. }
  205. static void __init parse_dtb(void)
  206. {
  207. /* Early scan of device tree from init memory */
  208. if (early_init_dt_scan(dtb_early_va)) {
  209. const char *name = of_flat_dt_get_machine_name();
  210. if (name) {
  211. pr_info("Machine model: %s\n", name);
  212. dump_stack_set_arch_desc("%s (DT)", name);
  213. }
  214. } else {
  215. pr_err("No DTB passed to the kernel\n");
  216. }
  217. #ifdef CONFIG_CMDLINE_FORCE
  218. strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  219. pr_info("Forcing kernel command line to: %s\n", boot_command_line);
  220. #endif
  221. }
  222. void __init setup_arch(char **cmdline_p)
  223. {
  224. parse_dtb();
  225. setup_initial_init_mm(_stext, _etext, _edata, _end);
  226. *cmdline_p = boot_command_line;
  227. early_ioremap_setup();
  228. jump_label_init();
  229. parse_early_param();
  230. efi_init();
  231. paging_init();
  232. #if IS_ENABLED(CONFIG_BUILTIN_DTB)
  233. unflatten_and_copy_device_tree();
  234. #else
  235. unflatten_device_tree();
  236. #endif
  237. misc_mem_init();
  238. init_resources();
  239. sbi_init();
  240. #ifdef CONFIG_KASAN
  241. kasan_init();
  242. #endif
  243. #ifdef CONFIG_SMP
  244. setup_smp();
  245. #endif
  246. riscv_init_cbom_blocksize();
  247. riscv_fill_hwcap();
  248. apply_boot_alternatives();
  249. }
  250. static int __init topology_init(void)
  251. {
  252. int i, ret;
  253. for_each_possible_cpu(i) {
  254. struct cpu *cpu = &per_cpu(cpu_devices, i);
  255. cpu->hotpluggable = cpu_has_hotplug(i);
  256. ret = register_cpu(cpu, i);
  257. if (unlikely(ret))
  258. pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
  259. __func__, i, ret);
  260. }
  261. return 0;
  262. }
  263. subsys_initcall(topology_init);
  264. void free_initmem(void)
  265. {
  266. if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
  267. set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
  268. if (IS_ENABLED(CONFIG_64BIT))
  269. set_kernel_memory(__init_begin, __init_end, set_memory_nx);
  270. }
  271. free_initmem_default(POISON_FREE_INITMEM);
  272. }