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_CRASH_DUMP
177 if (elfcorehdr_size > 0) {
178 elfcorehdr_res.start = elfcorehdr_addr;
179 elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
180 elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
181 add_resource(&iomem_resource, &elfcorehdr_res);
185 for_each_reserved_mem_region(region) {
186 res = &mem_res[res_idx--];
188 res->name = "Reserved";
189 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
190 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
191 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
194 * Ignore any other reserved regions within
197 if (memblock_is_memory(res->start)) {
198 /* Re-use this pre-allocated resource */
203 ret = add_resource(&iomem_resource, res);
208 /* Add /memory regions to the resource tree */
209 for_each_mem_region(region) {
210 res = &mem_res[res_idx--];
212 if (unlikely(memblock_is_nomap(region))) {
213 res->name = "Reserved";
214 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
216 res->name = "System RAM";
217 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
220 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
221 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
223 ret = add_resource(&iomem_resource, res);
228 /* Clean-up any unused pre-allocated resources */
230 memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
234 /* Better an empty resource tree than an inconsistent one */
235 release_child_resources(&iomem_resource);
236 memblock_free(mem_res, mem_res_sz);
240 static void __init parse_dtb(void)
242 /* Early scan of device tree from init memory */
243 if (early_init_dt_scan(dtb_early_va)) {
244 const char *name = of_flat_dt_get_machine_name();
247 pr_info("Machine model: %s\n", name);
248 dump_stack_set_arch_desc("%s (DT)", name);
251 pr_err("No DTB passed to the kernel\n");
254 #ifdef CONFIG_CMDLINE_FORCE
255 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
256 pr_info("Forcing kernel command line to: %s\n", boot_command_line);
260 extern void __init init_rt_signal_env(void);
262 void __init setup_arch(char **cmdline_p)
265 setup_initial_init_mm(_stext, _etext, _edata, _end);
267 *cmdline_p = boot_command_line;
269 early_ioremap_setup();
277 /* Parse the ACPI tables for possible boot-time configuration */
278 acpi_boot_table_init();
280 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
281 unflatten_and_copy_device_tree();
283 unflatten_device_tree();
298 acpi_init_rintc_map();
300 riscv_init_cbo_blocksizes();
302 init_rt_signal_env();
303 apply_boot_alternatives();
304 if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
305 riscv_isa_extension_available(NULL, ZICBOM))
306 riscv_noncoherent_supported();
307 riscv_set_dma_cache_alignment();
310 static int __init topology_init(void)
314 for_each_possible_cpu(i) {
315 struct cpu *cpu = &per_cpu(cpu_devices, i);
317 cpu->hotpluggable = cpu_has_hotplug(i);
318 ret = register_cpu(cpu, i);
320 pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
326 subsys_initcall(topology_init);
328 void free_initmem(void)
330 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
331 set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
332 if (IS_ENABLED(CONFIG_64BIT))
333 set_kernel_memory(__init_begin, __init_end, set_memory_nx);
336 free_initmem_default(POISON_FREE_INITMEM);
339 static int dump_kernel_offset(struct notifier_block *self,
340 unsigned long v, void *p)
342 pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
343 kernel_map.virt_offset,
349 static struct notifier_block kernel_offset_notifier = {
350 .notifier_call = dump_kernel_offset
353 static int __init register_kernel_offset_dumper(void)
355 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
356 atomic_notifier_chain_register(&panic_notifier_list,
357 &kernel_offset_notifier);
361 device_initcall(register_kernel_offset_dumper);