setup.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/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/swiotlb.h>
  20. #include <linux/smp.h>
  21. #include <linux/efi.h>
  22. #include <linux/crash_dump.h>
  23. #include <asm/cpu_ops.h>
  24. #include <asm/early_ioremap.h>
  25. #include <asm/setup.h>
  26. #include <asm/sections.h>
  27. #include <asm/sbi.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/thread_info.h>
  30. #include <asm/kasan.h>
  31. #include <asm/efi.h>
  32. #include "head.h"
  33. #if defined(CONFIG_DUMMY_CONSOLE) || defined(CONFIG_EFI)
  34. struct screen_info screen_info __section(".data") = {
  35. .orig_video_lines = 30,
  36. .orig_video_cols = 80,
  37. .orig_video_mode = 0,
  38. .orig_video_ega_bx = 0,
  39. .orig_video_isVGA = 1,
  40. .orig_video_points = 8
  41. };
  42. #endif
  43. /*
  44. * The lucky hart to first increment this variable will boot the other cores.
  45. * This is used before the kernel initializes the BSS so it can't be in the
  46. * BSS.
  47. */
  48. atomic_t hart_lottery __section(".sdata");
  49. unsigned long boot_cpu_hartid;
  50. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  51. /*
  52. * Place kernel memory regions on the resource tree so that
  53. * kexec-tools can retrieve them from /proc/iomem. While there
  54. * also add "System RAM" regions for compatibility with other
  55. * archs, and the rest of the known regions for completeness.
  56. */
  57. static struct resource code_res = { .name = "Kernel code", };
  58. static struct resource data_res = { .name = "Kernel data", };
  59. static struct resource rodata_res = { .name = "Kernel rodata", };
  60. static struct resource bss_res = { .name = "Kernel bss", };
  61. #ifdef CONFIG_CRASH_DUMP
  62. static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
  63. #endif
  64. static int __init add_resource(struct resource *parent,
  65. struct resource *res)
  66. {
  67. int ret = 0;
  68. ret = insert_resource(parent, res);
  69. if (ret < 0) {
  70. pr_err("Failed to add a %s resource at %llx\n",
  71. res->name, (unsigned long long) res->start);
  72. return ret;
  73. }
  74. return 1;
  75. }
  76. static int __init add_kernel_resources(struct resource *res)
  77. {
  78. int ret = 0;
  79. /*
  80. * The memory region of the kernel image is continuous and
  81. * was reserved on setup_bootmem, find it here and register
  82. * it as a resource, then register the various segments of
  83. * the image as child nodes
  84. */
  85. if (!(res->start <= code_res.start && res->end >= data_res.end))
  86. return 0;
  87. res->name = "Kernel image";
  88. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  89. /*
  90. * We removed a part of this region on setup_bootmem so
  91. * we need to expand the resource for the bss to fit in.
  92. */
  93. res->end = bss_res.end;
  94. ret = add_resource(&iomem_resource, res);
  95. if (ret < 0)
  96. return ret;
  97. ret = add_resource(res, &code_res);
  98. if (ret < 0)
  99. return ret;
  100. ret = add_resource(res, &rodata_res);
  101. if (ret < 0)
  102. return ret;
  103. ret = add_resource(res, &data_res);
  104. if (ret < 0)
  105. return ret;
  106. ret = add_resource(res, &bss_res);
  107. return ret;
  108. }
  109. static void __init init_resources(void)
  110. {
  111. struct memblock_region *region = NULL;
  112. struct resource *res = NULL;
  113. int ret = 0;
  114. code_res.start = __pa_symbol(_text);
  115. code_res.end = __pa_symbol(_etext) - 1;
  116. code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  117. rodata_res.start = __pa_symbol(__start_rodata);
  118. rodata_res.end = __pa_symbol(__end_rodata) - 1;
  119. rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  120. data_res.start = __pa_symbol(_data);
  121. data_res.end = __pa_symbol(_edata) - 1;
  122. data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  123. bss_res.start = __pa_symbol(__bss_start);
  124. bss_res.end = __pa_symbol(__bss_stop) - 1;
  125. bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  126. /*
  127. * Start by adding the reserved regions, if they overlap
  128. * with /memory regions, insert_resource later on will take
  129. * care of it.
  130. */
  131. #ifdef CONFIG_KEXEC_CORE
  132. if (crashk_res.start != crashk_res.end) {
  133. ret = add_resource(&iomem_resource, &crashk_res);
  134. if (ret < 0)
  135. goto error;
  136. }
  137. #endif
  138. #ifdef CONFIG_CRASH_DUMP
  139. if (elfcorehdr_size > 0) {
  140. elfcorehdr_res.start = elfcorehdr_addr;
  141. elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
  142. elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  143. add_resource(&iomem_resource, &elfcorehdr_res);
  144. }
  145. #endif
  146. for_each_reserved_mem_region(region) {
  147. res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
  148. if (!res)
  149. panic("%s: Failed to allocate %zu bytes\n", __func__,
  150. sizeof(struct resource));
  151. res->name = "Reserved";
  152. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  153. res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
  154. res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
  155. ret = add_kernel_resources(res);
  156. if (ret < 0)
  157. goto error;
  158. else if (ret)
  159. continue;
  160. /*
  161. * Ignore any other reserved regions within
  162. * system memory.
  163. */
  164. if (memblock_is_memory(res->start))
  165. continue;
  166. ret = add_resource(&iomem_resource, res);
  167. if (ret < 0)
  168. goto error;
  169. }
  170. /* Add /memory regions to the resource tree */
  171. for_each_mem_region(region) {
  172. res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
  173. if (!res)
  174. panic("%s: Failed to allocate %zu bytes\n", __func__,
  175. sizeof(struct resource));
  176. if (unlikely(memblock_is_nomap(region))) {
  177. res->name = "Reserved";
  178. res->flags = IORESOURCE_MEM;
  179. } else {
  180. res->name = "System RAM";
  181. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  182. }
  183. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  184. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  185. ret = add_resource(&iomem_resource, res);
  186. if (ret < 0)
  187. goto error;
  188. }
  189. return;
  190. error:
  191. memblock_free((phys_addr_t) res, sizeof(struct resource));
  192. /* Better an empty resource tree than an inconsistent one */
  193. release_child_resources(&iomem_resource);
  194. }
  195. static void __init parse_dtb(void)
  196. {
  197. /* Early scan of device tree from init memory */
  198. if (early_init_dt_scan(dtb_early_va))
  199. return;
  200. pr_err("No DTB passed to the kernel\n");
  201. #ifdef CONFIG_CMDLINE_FORCE
  202. strlcpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  203. pr_info("Forcing kernel command line to: %s\n", boot_command_line);
  204. #endif
  205. }
  206. void __init setup_arch(char **cmdline_p)
  207. {
  208. parse_dtb();
  209. init_mm.start_code = (unsigned long) _stext;
  210. init_mm.end_code = (unsigned long) _etext;
  211. init_mm.end_data = (unsigned long) _edata;
  212. init_mm.brk = (unsigned long) _end;
  213. *cmdline_p = boot_command_line;
  214. early_ioremap_setup();
  215. jump_label_init();
  216. parse_early_param();
  217. efi_init();
  218. setup_bootmem();
  219. paging_init();
  220. #if IS_ENABLED(CONFIG_BUILTIN_DTB)
  221. unflatten_and_copy_device_tree();
  222. #else
  223. if (early_init_dt_verify(__va(dtb_early_pa)))
  224. unflatten_device_tree();
  225. else
  226. pr_err("No DTB found in kernel mappings\n");
  227. #endif
  228. #ifdef CONFIG_SWIOTLB
  229. swiotlb_init(1);
  230. #endif
  231. #ifdef CONFIG_KASAN
  232. kasan_init();
  233. #endif
  234. #if IS_ENABLED(CONFIG_RISCV_SBI)
  235. init_resources();
  236. sbi_init();
  237. #endif
  238. #ifdef CONFIG_SMP
  239. setup_smp();
  240. #endif
  241. riscv_fill_hwcap();
  242. }
  243. static int __init topology_init(void)
  244. {
  245. int i;
  246. for_each_possible_cpu(i) {
  247. struct cpu *cpu = &per_cpu(cpu_devices, i);
  248. cpu->hotpluggable = cpu_has_hotplug(i);
  249. register_cpu(cpu, i);
  250. }
  251. return 0;
  252. }
  253. subsys_initcall(topology_init);