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/of_fdt.h>
19 #include <linux/sched/task.h>
20 #include <linux/smp.h>
21 #include <linux/efi.h>
22 #include <linux/crash_dump.h>
23 #include <linux/panic_notifier.h>
26 #include <asm/alternative.h>
27 #include <asm/cacheflush.h>
28 #include <asm/cpufeature.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>
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
48 atomic_t hart_lottery __section(".sdata")
49 #ifdef CONFIG_XIP_KERNEL
50 = ATOMIC_INIT(0xC001BEEF)
53 unsigned long boot_cpu_hartid;
54 static DEFINE_PER_CPU(struct cpu, cpu_devices);
57 * Place kernel memory regions on the resource tree so that
58 * kexec-tools can retrieve them from /proc/iomem. While there
59 * also add "System RAM" regions for compatibility with other
60 * archs, and the rest of the known regions for completeness.
62 static struct resource kimage_res = { .name = "Kernel image", };
63 static struct resource code_res = { .name = "Kernel code", };
64 static struct resource data_res = { .name = "Kernel data", };
65 static struct resource rodata_res = { .name = "Kernel rodata", };
66 static struct resource bss_res = { .name = "Kernel bss", };
67 #ifdef CONFIG_CRASH_DUMP
68 static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
71 static int __init add_resource(struct resource *parent,
76 ret = insert_resource(parent, res);
78 pr_err("Failed to add a %s resource at %llx\n",
79 res->name, (unsigned long long) res->start);
86 static int __init add_kernel_resources(void)
91 * The memory region of the kernel image is continuous and
92 * was reserved on setup_bootmem, register it here as a
93 * resource, with the various segments of the image as
97 code_res.start = __pa_symbol(_text);
98 code_res.end = __pa_symbol(_etext) - 1;
99 code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
101 rodata_res.start = __pa_symbol(__start_rodata);
102 rodata_res.end = __pa_symbol(__end_rodata) - 1;
103 rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
105 data_res.start = __pa_symbol(_data);
106 data_res.end = __pa_symbol(_edata) - 1;
107 data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
109 bss_res.start = __pa_symbol(__bss_start);
110 bss_res.end = __pa_symbol(__bss_stop) - 1;
111 bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
113 kimage_res.start = code_res.start;
114 kimage_res.end = bss_res.end;
115 kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
117 ret = add_resource(&iomem_resource, &kimage_res);
121 ret = add_resource(&kimage_res, &code_res);
125 ret = add_resource(&kimage_res, &rodata_res);
129 ret = add_resource(&kimage_res, &data_res);
133 ret = add_resource(&kimage_res, &bss_res);
138 static void __init init_resources(void)
140 struct memblock_region *region = NULL;
141 struct resource *res = NULL;
142 struct resource *mem_res = NULL;
143 size_t mem_res_sz = 0;
144 int num_resources = 0, res_idx = 0;
147 /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
148 num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
149 res_idx = num_resources - 1;
151 mem_res_sz = num_resources * sizeof(*mem_res);
152 mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
154 panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
157 * Start by adding the reserved regions, if they overlap
158 * with /memory regions, insert_resource later on will take
161 ret = add_kernel_resources();
165 #ifdef CONFIG_CRASH_DUMP
166 if (elfcorehdr_size > 0) {
167 elfcorehdr_res.start = elfcorehdr_addr;
168 elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
169 elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
170 add_resource(&iomem_resource, &elfcorehdr_res);
174 for_each_reserved_mem_region(region) {
175 res = &mem_res[res_idx--];
177 res->name = "Reserved";
178 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
179 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
180 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
183 * Ignore any other reserved regions within
186 if (memblock_is_memory(res->start)) {
187 /* Re-use this pre-allocated resource */
192 ret = add_resource(&iomem_resource, res);
197 /* Add /memory regions to the resource tree */
198 for_each_mem_region(region) {
199 res = &mem_res[res_idx--];
201 if (unlikely(memblock_is_nomap(region))) {
202 res->name = "Reserved";
203 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
205 res->name = "System RAM";
206 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
209 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
210 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
212 ret = add_resource(&iomem_resource, res);
217 /* Clean-up any unused pre-allocated resources */
219 memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
223 /* Better an empty resource tree than an inconsistent one */
224 release_child_resources(&iomem_resource);
225 memblock_free(mem_res, mem_res_sz);
229 static void __init parse_dtb(void)
231 /* Early scan of device tree from init memory */
232 if (early_init_dt_scan(dtb_early_va)) {
233 const char *name = of_flat_dt_get_machine_name();
236 pr_info("Machine model: %s\n", name);
237 dump_stack_set_arch_desc("%s (DT)", name);
240 pr_err("No DTB passed to the kernel\n");
243 #ifdef CONFIG_CMDLINE_FORCE
244 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
245 pr_info("Forcing kernel command line to: %s\n", boot_command_line);
249 extern void __init init_rt_signal_env(void);
251 void __init setup_arch(char **cmdline_p)
254 setup_initial_init_mm(_stext, _etext, _edata, _end);
256 *cmdline_p = boot_command_line;
258 early_ioremap_setup();
266 /* Parse the ACPI tables for possible boot-time configuration */
267 acpi_boot_table_init();
269 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
270 unflatten_and_copy_device_tree();
272 unflatten_device_tree();
287 acpi_init_rintc_map();
289 riscv_init_cbo_blocksizes();
291 init_rt_signal_env();
292 apply_boot_alternatives();
294 if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
295 riscv_isa_extension_available(NULL, ZICBOM))
296 riscv_noncoherent_supported();
297 riscv_set_dma_cache_alignment();
299 riscv_user_isa_enable();
302 static int __init topology_init(void)
306 for_each_possible_cpu(i) {
307 struct cpu *cpu = &per_cpu(cpu_devices, i);
309 cpu->hotpluggable = cpu_has_hotplug(i);
310 ret = register_cpu(cpu, i);
312 pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
318 subsys_initcall(topology_init);
320 void free_initmem(void)
322 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
323 set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
324 if (IS_ENABLED(CONFIG_64BIT))
325 set_kernel_memory(__init_begin, __init_end, set_memory_nx);
328 free_initmem_default(POISON_FREE_INITMEM);
331 static int dump_kernel_offset(struct notifier_block *self,
332 unsigned long v, void *p)
334 pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
335 kernel_map.virt_offset,
341 static struct notifier_block kernel_offset_notifier = {
342 .notifier_call = dump_kernel_offset
345 static int __init register_kernel_offset_dumper(void)
347 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
348 atomic_notifier_chain_register(&panic_notifier_list,
349 &kernel_offset_notifier);
353 device_initcall(register_kernel_offset_dumper);