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