xref: /linux/arch/riscv/kernel/setup.c (revision 2a43434675b2114e8f909a5039cc421d35d35ce9)
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/early_ioremap.h>
30 #include <asm/pgtable.h>
31 #include <asm/setup.h>
32 #include <asm/set_memory.h>
33 #include <asm/sections.h>
34 #include <asm/sbi.h>
35 #include <asm/tlbflush.h>
36 #include <asm/thread_info.h>
37 #include <asm/kasan.h>
38 #include <asm/efi.h>
39 
40 #include "head.h"
41 
42 /*
43  * The lucky hart to first increment this variable will boot the other cores.
44  * This is used before the kernel initializes the BSS so it can't be in the
45  * BSS.
46  */
47 atomic_t hart_lottery __section(".sdata")
48 #ifdef CONFIG_XIP_KERNEL
49 = ATOMIC_INIT(0xC001BEEF)
50 #endif
51 ;
52 unsigned long boot_cpu_hartid;
53 static DEFINE_PER_CPU(struct cpu, cpu_devices);
54 
55 /*
56  * Place kernel memory regions on the resource tree so that
57  * kexec-tools can retrieve them from /proc/iomem. While there
58  * also add "System RAM" regions for compatibility with other
59  * archs, and the rest of the known regions for completeness.
60  */
61 static struct resource kimage_res = { .name = "Kernel image", };
62 static struct resource code_res = { .name = "Kernel code", };
63 static struct resource data_res = { .name = "Kernel data", };
64 static struct resource rodata_res = { .name = "Kernel rodata", };
65 static struct resource bss_res = { .name = "Kernel bss", };
66 #ifdef CONFIG_CRASH_DUMP
67 static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
68 #endif
69 
70 static int __init add_resource(struct resource *parent,
71 				struct resource *res)
72 {
73 	int ret = 0;
74 
75 	ret = insert_resource(parent, res);
76 	if (ret < 0) {
77 		pr_err("Failed to add a %s resource at %llx\n",
78 			res->name, (unsigned long long) res->start);
79 		return ret;
80 	}
81 
82 	return 1;
83 }
84 
85 static int __init add_kernel_resources(void)
86 {
87 	int ret = 0;
88 
89 	/*
90 	 * The memory region of the kernel image is continuous and
91 	 * was reserved on setup_bootmem, register it here as a
92 	 * resource, with the various segments of the image as
93 	 * child nodes.
94 	 */
95 
96 	code_res.start = __pa_symbol(_text);
97 	code_res.end = __pa_symbol(_etext) - 1;
98 	code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
99 
100 	rodata_res.start = __pa_symbol(__start_rodata);
101 	rodata_res.end = __pa_symbol(__end_rodata) - 1;
102 	rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
103 
104 	data_res.start = __pa_symbol(_data);
105 	data_res.end = __pa_symbol(_edata) - 1;
106 	data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
107 
108 	bss_res.start = __pa_symbol(__bss_start);
109 	bss_res.end = __pa_symbol(__bss_stop) - 1;
110 	bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
111 
112 	kimage_res.start = code_res.start;
113 	kimage_res.end = bss_res.end;
114 	kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
115 
116 	ret = add_resource(&iomem_resource, &kimage_res);
117 	if (ret < 0)
118 		return ret;
119 
120 	ret = add_resource(&kimage_res, &code_res);
121 	if (ret < 0)
122 		return ret;
123 
124 	ret = add_resource(&kimage_res, &rodata_res);
125 	if (ret < 0)
126 		return ret;
127 
128 	ret = add_resource(&kimage_res, &data_res);
129 	if (ret < 0)
130 		return ret;
131 
132 	ret = add_resource(&kimage_res, &bss_res);
133 
134 	return ret;
135 }
136 
137 static void __init init_resources(void)
138 {
139 	struct memblock_region *region = NULL;
140 	struct resource *res = NULL;
141 	struct resource *mem_res = NULL;
142 	size_t mem_res_sz = 0;
143 	int num_resources = 0, res_idx = 0;
144 	int ret = 0;
145 
146 	/* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
147 	num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
148 	res_idx = num_resources - 1;
149 
150 	mem_res_sz = num_resources * sizeof(*mem_res);
151 	mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
152 	if (!mem_res)
153 		panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
154 
155 	/*
156 	 * Start by adding the reserved regions, if they overlap
157 	 * with /memory regions, insert_resource later on will take
158 	 * care of it.
159 	 */
160 	ret = add_kernel_resources();
161 	if (ret < 0)
162 		goto error;
163 
164 #ifdef CONFIG_CRASH_DUMP
165 	if (elfcorehdr_size > 0) {
166 		elfcorehdr_res.start = elfcorehdr_addr;
167 		elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
168 		elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
169 		add_resource(&iomem_resource, &elfcorehdr_res);
170 	}
171 #endif
172 
173 	for_each_reserved_mem_region(region) {
174 		res = &mem_res[res_idx--];
175 
176 		res->name = "Reserved";
177 		res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
178 		res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
179 		res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
180 
181 		/*
182 		 * Ignore any other reserved regions within
183 		 * system memory.
184 		 */
185 		if (memblock_is_memory(res->start)) {
186 			/* Re-use this pre-allocated resource */
187 			res_idx++;
188 			continue;
189 		}
190 
191 		ret = add_resource(&iomem_resource, res);
192 		if (ret < 0)
193 			goto error;
194 	}
195 
196 	/* Add /memory regions to the resource tree */
197 	for_each_mem_region(region) {
198 		res = &mem_res[res_idx--];
199 
200 		if (unlikely(memblock_is_nomap(region))) {
201 			res->name = "Reserved";
202 			res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
203 		} else {
204 			res->name = "System RAM";
205 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
206 		}
207 
208 		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
209 		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
210 
211 		ret = add_resource(&iomem_resource, res);
212 		if (ret < 0)
213 			goto error;
214 	}
215 
216 	/* Clean-up any unused pre-allocated resources */
217 	if (res_idx >= 0)
218 		memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
219 	return;
220 
221  error:
222 	/* Better an empty resource tree than an inconsistent one */
223 	release_child_resources(&iomem_resource);
224 	memblock_free(mem_res, mem_res_sz);
225 }
226 
227 
228 static void __init parse_dtb(void)
229 {
230 	/* Early scan of device tree from init memory */
231 	if (early_init_dt_scan(dtb_early_va)) {
232 		const char *name = of_flat_dt_get_machine_name();
233 
234 		if (name) {
235 			pr_info("Machine model: %s\n", name);
236 			dump_stack_set_arch_desc("%s (DT)", name);
237 		}
238 	} else {
239 		pr_err("No DTB passed to the kernel\n");
240 	}
241 
242 #ifdef CONFIG_CMDLINE_FORCE
243 	strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
244 	pr_info("Forcing kernel command line to: %s\n", boot_command_line);
245 #endif
246 }
247 
248 extern void __init init_rt_signal_env(void);
249 
250 void __init setup_arch(char **cmdline_p)
251 {
252 	parse_dtb();
253 	setup_initial_init_mm(_stext, _etext, _edata, _end);
254 
255 	*cmdline_p = boot_command_line;
256 
257 	early_ioremap_setup();
258 	sbi_init();
259 	jump_label_init();
260 	parse_early_param();
261 
262 	efi_init();
263 	paging_init();
264 
265 	/* Parse the ACPI tables for possible boot-time configuration */
266 	acpi_boot_table_init();
267 
268 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
269 	unflatten_and_copy_device_tree();
270 #else
271 	unflatten_device_tree();
272 #endif
273 	misc_mem_init();
274 
275 	init_resources();
276 
277 #ifdef CONFIG_KASAN
278 	kasan_init();
279 #endif
280 
281 #ifdef CONFIG_SMP
282 	setup_smp();
283 #endif
284 
285 	if (!acpi_disabled)
286 		acpi_init_rintc_map();
287 
288 	riscv_init_cbo_blocksizes();
289 	riscv_fill_hwcap();
290 	init_rt_signal_env();
291 	apply_boot_alternatives();
292 
293 	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
294 	    riscv_isa_extension_available(NULL, ZICBOM))
295 		riscv_noncoherent_supported();
296 	riscv_set_dma_cache_alignment();
297 
298 	riscv_user_isa_enable();
299 }
300 
301 static int __init topology_init(void)
302 {
303 	int i, ret;
304 
305 	for_each_possible_cpu(i) {
306 		struct cpu *cpu = &per_cpu(cpu_devices, i);
307 
308 		cpu->hotpluggable = cpu_has_hotplug(i);
309 		ret = register_cpu(cpu, i);
310 		if (unlikely(ret))
311 			pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
312 			       __func__, i, ret);
313 	}
314 
315 	return 0;
316 }
317 subsys_initcall(topology_init);
318 
319 void free_initmem(void)
320 {
321 	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
322 		set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
323 		if (IS_ENABLED(CONFIG_64BIT))
324 			set_kernel_memory(__init_begin, __init_end, set_memory_nx);
325 	}
326 
327 	free_initmem_default(POISON_FREE_INITMEM);
328 }
329 
330 static int dump_kernel_offset(struct notifier_block *self,
331 			      unsigned long v, void *p)
332 {
333 	pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
334 		 kernel_map.virt_offset,
335 		 KERNEL_LINK_ADDR);
336 
337 	return 0;
338 }
339 
340 static struct notifier_block kernel_offset_notifier = {
341 	.notifier_call = dump_kernel_offset
342 };
343 
344 static int __init register_kernel_offset_dumper(void)
345 {
346 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
347 		atomic_notifier_chain_register(&panic_notifier_list,
348 					       &kernel_offset_notifier);
349 
350 	return 0;
351 }
352 device_initcall(register_kernel_offset_dumper);
353