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/init.h>
13 #include <linux/memblock.h>
14 #include <linux/sched.h>
15 #include <linux/console.h>
16 #include <linux/screen_info.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_platform.h>
19 #include <linux/sched/task.h>
20 #include <linux/swiotlb.h>
21 #include <linux/smp.h>
22 #include <linux/efi.h>
24 #include <asm/cpu_ops.h>
25 #include <asm/early_ioremap.h>
26 #include <asm/setup.h>
27 #include <asm/set_memory.h>
28 #include <asm/sections.h>
30 #include <asm/tlbflush.h>
31 #include <asm/thread_info.h>
32 #include <asm/kasan.h>
37 #if defined(CONFIG_DUMMY_CONSOLE) || defined(CONFIG_EFI)
38 struct screen_info screen_info
__section(".data") = {
39 .orig_video_lines
= 30,
40 .orig_video_cols
= 80,
42 .orig_video_ega_bx
= 0,
43 .orig_video_isVGA
= 1,
44 .orig_video_points
= 8
49 * The lucky hart to first increment this variable will boot the other cores.
50 * This is used before the kernel initializes the BSS so it can't be in the
53 atomic_t hart_lottery
__section(".sdata");
54 unsigned long boot_cpu_hartid
;
55 static DEFINE_PER_CPU(struct cpu
, cpu_devices
);
58 * Place kernel memory regions on the resource tree so that
59 * kexec-tools can retrieve them from /proc/iomem. While there
60 * also add "System RAM" regions for compatibility with other
61 * archs, and the rest of the known regions for completeness.
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", };
68 static int __init
add_resource(struct resource
*parent
,
73 ret
= insert_resource(parent
, res
);
75 pr_err("Failed to add a %s resource at %llx\n",
76 res
->name
, (unsigned long long) res
->start
);
83 static int __init
add_kernel_resources(struct resource
*res
)
88 * The memory region of the kernel image is continuous and
89 * was reserved on setup_bootmem, find it here and register
90 * it as a resource, then register the various segments of
91 * the image as child nodes
93 if (!(res
->start
<= code_res
.start
&& res
->end
>= data_res
.end
))
96 res
->name
= "Kernel image";
97 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
100 * We removed a part of this region on setup_bootmem so
101 * we need to expand the resource for the bss to fit in.
103 res
->end
= bss_res
.end
;
105 ret
= add_resource(&iomem_resource
, res
);
109 ret
= add_resource(res
, &code_res
);
113 ret
= add_resource(res
, &rodata_res
);
117 ret
= add_resource(res
, &data_res
);
121 ret
= add_resource(res
, &bss_res
);
126 static void __init
init_resources(void)
128 struct memblock_region
*region
= NULL
;
129 struct resource
*res
= NULL
;
132 code_res
.start
= __pa_symbol(_text
);
133 code_res
.end
= __pa_symbol(_etext
) - 1;
134 code_res
.flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
136 rodata_res
.start
= __pa_symbol(__start_rodata
);
137 rodata_res
.end
= __pa_symbol(__end_rodata
) - 1;
138 rodata_res
.flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
140 data_res
.start
= __pa_symbol(_data
);
141 data_res
.end
= __pa_symbol(_edata
) - 1;
142 data_res
.flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
144 bss_res
.start
= __pa_symbol(__bss_start
);
145 bss_res
.end
= __pa_symbol(__bss_stop
) - 1;
146 bss_res
.flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
149 * Start by adding the reserved regions, if they overlap
150 * with /memory regions, insert_resource later on will take
153 for_each_reserved_mem_region(region
) {
154 res
= memblock_alloc(sizeof(struct resource
), SMP_CACHE_BYTES
);
156 panic("%s: Failed to allocate %zu bytes\n", __func__
,
157 sizeof(struct resource
));
159 res
->name
= "Reserved";
160 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
161 res
->start
= __pfn_to_phys(memblock_region_reserved_base_pfn(region
));
162 res
->end
= __pfn_to_phys(memblock_region_reserved_end_pfn(region
)) - 1;
164 ret
= add_kernel_resources(res
);
171 * Ignore any other reserved regions within
174 if (memblock_is_memory(res
->start
))
177 ret
= add_resource(&iomem_resource
, res
);
182 /* Add /memory regions to the resource tree */
183 for_each_mem_region(region
) {
184 res
= memblock_alloc(sizeof(struct resource
), SMP_CACHE_BYTES
);
186 panic("%s: Failed to allocate %zu bytes\n", __func__
,
187 sizeof(struct resource
));
189 if (unlikely(memblock_is_nomap(region
))) {
190 res
->name
= "Reserved";
191 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
193 res
->name
= "System RAM";
194 res
->flags
= IORESOURCE_SYSTEM_RAM
| IORESOURCE_BUSY
;
197 res
->start
= __pfn_to_phys(memblock_region_memory_base_pfn(region
));
198 res
->end
= __pfn_to_phys(memblock_region_memory_end_pfn(region
)) - 1;
200 ret
= add_resource(&iomem_resource
, res
);
208 memblock_free((phys_addr_t
) res
, sizeof(struct resource
));
209 /* Better an empty resource tree than an inconsistent one */
210 release_child_resources(&iomem_resource
);
214 static void __init
parse_dtb(void)
216 /* Early scan of device tree from init memory */
217 if (early_init_dt_scan(dtb_early_va
))
220 pr_err("No DTB passed to the kernel\n");
221 #ifdef CONFIG_CMDLINE_FORCE
222 strlcpy(boot_command_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
223 pr_info("Forcing kernel command line to: %s\n", boot_command_line
);
227 void __init
setup_arch(char **cmdline_p
)
230 init_mm
.start_code
= (unsigned long) _stext
;
231 init_mm
.end_code
= (unsigned long) _etext
;
232 init_mm
.end_data
= (unsigned long) _edata
;
233 init_mm
.brk
= (unsigned long) _end
;
235 *cmdline_p
= boot_command_line
;
237 early_ioremap_setup();
245 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
246 unflatten_and_copy_device_tree();
248 if (early_init_dt_verify(__va(dtb_early_pa
)))
249 unflatten_device_tree();
251 pr_err("No DTB found in kernel mappings\n");
254 if (IS_ENABLED(CONFIG_RISCV_SBI
))
257 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX
))
258 protect_kernel_text_data();
259 #ifdef CONFIG_SWIOTLB
267 #if IS_ENABLED(CONFIG_VT)
268 #if IS_ENABLED(CONFIG_FPC_CONSOLE)
269 conswitchp
= &fpc_con
;
280 static int __init
topology_init(void)
284 for_each_possible_cpu(i
) {
285 struct cpu
*cpu
= &per_cpu(cpu_devices
, i
);
287 cpu
->hotpluggable
= cpu_has_hotplug(i
);
288 register_cpu(cpu
, i
);
293 subsys_initcall(topology_init
);
295 void free_initmem(void)
297 unsigned long init_begin
= (unsigned long)__init_begin
;
298 unsigned long init_end
= (unsigned long)__init_end
;
300 // set_memory_rw_nx(init_begin, (init_end - init_begin) >> PAGE_SHIFT);
301 free_initmem_default(POISON_FREE_INITMEM
);