setup.c 9.3 KB

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