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