xref: /linux/arch/riscv/kernel/setup.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
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 
11 #include <linux/acpi.h>
12 #include <linux/cpu.h>
13 #include <linux/init.h>
14 #include <linux/mm.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>
24 
25 #include <asm/acpi.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>
35 #include <asm/sbi.h>
36 #include <asm/tlbflush.h>
37 #include <asm/thread_info.h>
38 #include <asm/kasan.h>
39 #include <asm/efi.h>
40 
41 #include "head.h"
42 
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 #ifdef CONFIG_XIP_KERNEL
50 = ATOMIC_INIT(0xC001BEEF)
51 #endif
52 ;
53 unsigned long boot_cpu_hartid;
54 static DEFINE_PER_CPU(struct cpu, cpu_devices);
55 
56 /*
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.
61  */
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", };
69 #endif
70 
71 static int __init add_resource(struct resource *parent,
72 				struct resource *res)
73 {
74 	int ret = 0;
75 
76 	ret = insert_resource(parent, res);
77 	if (ret < 0) {
78 		pr_err("Failed to add a %s resource at %llx\n",
79 			res->name, (unsigned long long) res->start);
80 		return ret;
81 	}
82 
83 	return 1;
84 }
85 
86 static int __init add_kernel_resources(void)
87 {
88 	int ret = 0;
89 
90 	/*
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
94 	 * child nodes.
95 	 */
96 
97 	code_res.start = __pa_symbol(_text);
98 	code_res.end = __pa_symbol(_etext) - 1;
99 	code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
100 
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;
104 
105 	data_res.start = __pa_symbol(_data);
106 	data_res.end = __pa_symbol(_edata) - 1;
107 	data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
108 
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;
112 
113 	kimage_res.start = code_res.start;
114 	kimage_res.end = bss_res.end;
115 	kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
116 
117 	ret = add_resource(&iomem_resource, &kimage_res);
118 	if (ret < 0)
119 		return ret;
120 
121 	ret = add_resource(&kimage_res, &code_res);
122 	if (ret < 0)
123 		return ret;
124 
125 	ret = add_resource(&kimage_res, &rodata_res);
126 	if (ret < 0)
127 		return ret;
128 
129 	ret = add_resource(&kimage_res, &data_res);
130 	if (ret < 0)
131 		return ret;
132 
133 	ret = add_resource(&kimage_res, &bss_res);
134 
135 	return ret;
136 }
137 
138 static void __init init_resources(void)
139 {
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;
145 	int ret = 0;
146 
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;
150 
151 	mem_res_sz = num_resources * sizeof(*mem_res);
152 	mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
153 	if (!mem_res)
154 		panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
155 
156 	/*
157 	 * Start by adding the reserved regions, if they overlap
158 	 * with /memory regions, insert_resource later on will take
159 	 * care of it.
160 	 */
161 	ret = add_kernel_resources();
162 	if (ret < 0)
163 		goto error;
164 
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);
171 	}
172 #endif
173 
174 	for_each_reserved_mem_region(region) {
175 		res = &mem_res[res_idx--];
176 
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;
181 
182 		/*
183 		 * Ignore any other reserved regions within
184 		 * system memory.
185 		 */
186 		if (memblock_is_memory(res->start)) {
187 			/* Re-use this pre-allocated resource */
188 			res_idx++;
189 			continue;
190 		}
191 
192 		ret = add_resource(&iomem_resource, res);
193 		if (ret < 0)
194 			goto error;
195 	}
196 
197 	/* Add /memory regions to the resource tree */
198 	for_each_mem_region(region) {
199 		res = &mem_res[res_idx--];
200 
201 		if (unlikely(memblock_is_nomap(region))) {
202 			res->name = "Reserved";
203 			res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
204 		} else {
205 			res->name = "System RAM";
206 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
207 		}
208 
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;
211 
212 		ret = add_resource(&iomem_resource, res);
213 		if (ret < 0)
214 			goto error;
215 	}
216 
217 	/* Clean-up any unused pre-allocated resources */
218 	if (res_idx >= 0)
219 		memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
220 	return;
221 
222  error:
223 	/* Better an empty resource tree than an inconsistent one */
224 	release_child_resources(&iomem_resource);
225 	memblock_free(mem_res, mem_res_sz);
226 }
227 
228 
229 static void __init parse_dtb(void)
230 {
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();
234 
235 		if (name) {
236 			pr_info("Machine model: %s\n", name);
237 			dump_stack_set_arch_desc("%s (DT)", name);
238 		}
239 	} else {
240 		pr_err("No DTB passed to the kernel\n");
241 	}
242 
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);
246 #endif
247 }
248 
249 extern void __init init_rt_signal_env(void);
250 
251 void __init setup_arch(char **cmdline_p)
252 {
253 	parse_dtb();
254 	setup_initial_init_mm(_stext, _etext, _edata, _end);
255 
256 	*cmdline_p = boot_command_line;
257 
258 	early_ioremap_setup();
259 	sbi_init();
260 	jump_label_init();
261 	parse_early_param();
262 
263 	efi_init();
264 	paging_init();
265 
266 	/* Parse the ACPI tables for possible boot-time configuration */
267 	acpi_boot_table_init();
268 
269 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
270 	unflatten_and_copy_device_tree();
271 #else
272 	unflatten_device_tree();
273 #endif
274 	misc_mem_init();
275 
276 	init_resources();
277 
278 #ifdef CONFIG_KASAN
279 	kasan_init();
280 #endif
281 
282 #ifdef CONFIG_SMP
283 	setup_smp();
284 #endif
285 
286 	if (!acpi_disabled)
287 		acpi_init_rintc_map();
288 
289 	riscv_init_cbo_blocksizes();
290 	riscv_fill_hwcap();
291 	init_rt_signal_env();
292 	apply_boot_alternatives();
293 
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();
298 
299 	riscv_user_isa_enable();
300 }
301 
302 static int __init topology_init(void)
303 {
304 	int i, ret;
305 
306 	for_each_possible_cpu(i) {
307 		struct cpu *cpu = &per_cpu(cpu_devices, i);
308 
309 		cpu->hotpluggable = cpu_has_hotplug(i);
310 		ret = register_cpu(cpu, i);
311 		if (unlikely(ret))
312 			pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
313 			       __func__, i, ret);
314 	}
315 
316 	return 0;
317 }
318 subsys_initcall(topology_init);
319 
320 void free_initmem(void)
321 {
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);
326 	}
327 
328 	free_initmem_default(POISON_FREE_INITMEM);
329 }
330 
331 static int dump_kernel_offset(struct notifier_block *self,
332 			      unsigned long v, void *p)
333 {
334 	pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
335 		 kernel_map.virt_offset,
336 		 KERNEL_LINK_ADDR);
337 
338 	return 0;
339 }
340 
341 static struct notifier_block kernel_offset_notifier = {
342 	.notifier_call = dump_kernel_offset
343 };
344 
345 static int __init register_kernel_offset_dumper(void)
346 {
347 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
348 		atomic_notifier_chain_register(&panic_notifier_list,
349 					       &kernel_offset_notifier);
350 
351 	return 0;
352 }
353 device_initcall(register_kernel_offset_dumper);
354