1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
11 #include <linux/acpi.h>
12 #include <linux/cpu.h>
13 #include <linux/init.h>
15 #include <linux/memblock.h>
16 #include <linux/sched.h>
17 #include <linux/console.h>
18 #include <linux/screen_info.h>
19 #include <linux/of_fdt.h>
20 #include <linux/sched/task.h>
21 #include <linux/smp.h>
22 #include <linux/efi.h>
23 #include <linux/crash_dump.h>
24 #include <linux/panic_notifier.h>
27 #include <asm/alternative.h>
28 #include <asm/cacheflush.h>
29 #include <asm/cpu_ops.h>
30 #include <asm/early_ioremap.h>
31 #include <asm/pgtable.h>
32 #include <asm/setup.h>
33 #include <asm/set_memory.h>
34 #include <asm/sections.h>
36 #include <asm/tlbflush.h>
37 #include <asm/thread_info.h>
38 #include <asm/kasan.h>
43 #if defined(CONFIG_DUMMY_CONSOLE) || defined(CONFIG_EFI)
44 struct screen_info screen_info __section(".data") = {
45 .orig_video_lines = 30,
46 .orig_video_cols = 80,
48 .orig_video_ega_bx = 0,
49 .orig_video_isVGA = 1,
50 .orig_video_points = 8
55 * The lucky hart to first increment this variable will boot the other cores.
56 * This is used before the kernel initializes the BSS so it can't be in the
59 atomic_t hart_lottery __section(".sdata")
60 #ifdef CONFIG_XIP_KERNEL
61 = ATOMIC_INIT(0xC001BEEF)
64 unsigned long boot_cpu_hartid;
65 static DEFINE_PER_CPU(struct cpu, cpu_devices);
68 * Place kernel memory regions on the resource tree so that
69 * kexec-tools can retrieve them from /proc/iomem. While there
70 * also add "System RAM" regions for compatibility with other
71 * archs, and the rest of the known regions for completeness.
73 static struct resource kimage_res = { .name = "Kernel image", };
74 static struct resource code_res = { .name = "Kernel code", };
75 static struct resource data_res = { .name = "Kernel data", };
76 static struct resource rodata_res = { .name = "Kernel rodata", };
77 static struct resource bss_res = { .name = "Kernel bss", };
78 #ifdef CONFIG_CRASH_DUMP
79 static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
82 static int __init add_resource(struct resource *parent,
87 ret = insert_resource(parent, res);
89 pr_err("Failed to add a %s resource at %llx\n",
90 res->name, (unsigned long long) res->start);
97 static int __init add_kernel_resources(void)
102 * The memory region of the kernel image is continuous and
103 * was reserved on setup_bootmem, register it here as a
104 * resource, with the various segments of the image as
108 code_res.start = __pa_symbol(_text);
109 code_res.end = __pa_symbol(_etext) - 1;
110 code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
112 rodata_res.start = __pa_symbol(__start_rodata);
113 rodata_res.end = __pa_symbol(__end_rodata) - 1;
114 rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
116 data_res.start = __pa_symbol(_data);
117 data_res.end = __pa_symbol(_edata) - 1;
118 data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
120 bss_res.start = __pa_symbol(__bss_start);
121 bss_res.end = __pa_symbol(__bss_stop) - 1;
122 bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
124 kimage_res.start = code_res.start;
125 kimage_res.end = bss_res.end;
126 kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
128 ret = add_resource(&iomem_resource, &kimage_res);
132 ret = add_resource(&kimage_res, &code_res);
136 ret = add_resource(&kimage_res, &rodata_res);
140 ret = add_resource(&kimage_res, &data_res);
144 ret = add_resource(&kimage_res, &bss_res);
149 static void __init init_resources(void)
151 struct memblock_region *region = NULL;
152 struct resource *res = NULL;
153 struct resource *mem_res = NULL;
154 size_t mem_res_sz = 0;
155 int num_resources = 0, res_idx = 0;
158 /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
159 num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
160 res_idx = num_resources - 1;
162 mem_res_sz = num_resources * sizeof(*mem_res);
163 mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
165 panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
168 * Start by adding the reserved regions, if they overlap
169 * with /memory regions, insert_resource later on will take
172 ret = add_kernel_resources();
176 #ifdef CONFIG_KEXEC_CORE
177 if (crashk_res.start != crashk_res.end) {
178 ret = add_resource(&iomem_resource, &crashk_res);
182 if (crashk_low_res.start != crashk_low_res.end) {
183 ret = add_resource(&iomem_resource, &crashk_low_res);
189 #ifdef CONFIG_CRASH_DUMP
190 if (elfcorehdr_size > 0) {
191 elfcorehdr_res.start = elfcorehdr_addr;
192 elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
193 elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
194 add_resource(&iomem_resource, &elfcorehdr_res);
198 for_each_reserved_mem_region(region) {
199 res = &mem_res[res_idx--];
201 res->name = "Reserved";
202 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
203 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
204 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
207 * Ignore any other reserved regions within
210 if (memblock_is_memory(res->start)) {
211 /* Re-use this pre-allocated resource */
216 ret = add_resource(&iomem_resource, res);
221 /* Add /memory regions to the resource tree */
222 for_each_mem_region(region) {
223 res = &mem_res[res_idx--];
225 if (unlikely(memblock_is_nomap(region))) {
226 res->name = "Reserved";
227 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
229 res->name = "System RAM";
230 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
233 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
234 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
236 ret = add_resource(&iomem_resource, res);
241 /* Clean-up any unused pre-allocated resources */
243 memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
247 /* Better an empty resource tree than an inconsistent one */
248 release_child_resources(&iomem_resource);
249 memblock_free(mem_res, mem_res_sz);
253 static void __init parse_dtb(void)
255 /* Early scan of device tree from init memory */
256 if (early_init_dt_scan(dtb_early_va)) {
257 const char *name = of_flat_dt_get_machine_name();
260 pr_info("Machine model: %s\n", name);
261 dump_stack_set_arch_desc("%s (DT)", name);
264 pr_err("No DTB passed to the kernel\n");
267 #ifdef CONFIG_CMDLINE_FORCE
268 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
269 pr_info("Forcing kernel command line to: %s\n", boot_command_line);
273 extern void __init init_rt_signal_env(void);
275 void __init setup_arch(char **cmdline_p)
278 setup_initial_init_mm(_stext, _etext, _edata, _end);
280 *cmdline_p = boot_command_line;
282 early_ioremap_setup();
290 /* Parse the ACPI tables for possible boot-time configuration */
291 acpi_boot_table_init();
293 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
294 unflatten_and_copy_device_tree();
296 unflatten_device_tree();
311 acpi_init_rintc_map();
313 riscv_init_cbo_blocksizes();
315 init_rt_signal_env();
316 apply_boot_alternatives();
317 if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
318 riscv_isa_extension_available(NULL, ZICBOM))
319 riscv_noncoherent_supported();
320 riscv_set_dma_cache_alignment();
323 static int __init topology_init(void)
327 for_each_possible_cpu(i) {
328 struct cpu *cpu = &per_cpu(cpu_devices, i);
330 cpu->hotpluggable = cpu_has_hotplug(i);
331 ret = register_cpu(cpu, i);
333 pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
339 subsys_initcall(topology_init);
341 void free_initmem(void)
343 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
344 set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
345 if (IS_ENABLED(CONFIG_64BIT))
346 set_kernel_memory(__init_begin, __init_end, set_memory_nx);
349 free_initmem_default(POISON_FREE_INITMEM);
352 static int dump_kernel_offset(struct notifier_block *self,
353 unsigned long v, void *p)
355 pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
356 kernel_map.virt_offset,
362 static struct notifier_block kernel_offset_notifier = {
363 .notifier_call = dump_kernel_offset
366 static int __init register_kernel_offset_dumper(void)
368 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
369 atomic_notifier_chain_register(&panic_notifier_list,
370 &kernel_offset_notifier);
374 device_initcall(register_kernel_offset_dumper);