xref: /openbmc/linux/arch/riscv/kernel/setup.c (revision d699090510c3223641a23834b4710e2d4309a6ad)
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 
76 static int num_standard_resources;
77 static struct resource *standard_resources;
78 
add_resource(struct resource * parent,struct resource * res)79 static int __init add_resource(struct resource *parent,
80 				struct resource *res)
81 {
82 	int ret = 0;
83 
84 	ret = insert_resource(parent, res);
85 	if (ret < 0) {
86 		pr_err("Failed to add a %s resource at %llx\n",
87 			res->name, (unsigned long long) res->start);
88 		return ret;
89 	}
90 
91 	return 1;
92 }
93 
add_kernel_resources(void)94 static int __init add_kernel_resources(void)
95 {
96 	int ret = 0;
97 
98 	/*
99 	 * The memory region of the kernel image is continuous and
100 	 * was reserved on setup_bootmem, register it here as a
101 	 * resource, with the various segments of the image as
102 	 * child nodes.
103 	 */
104 
105 	code_res.start = __pa_symbol(_text);
106 	code_res.end = __pa_symbol(_etext) - 1;
107 	code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
108 
109 	rodata_res.start = __pa_symbol(__start_rodata);
110 	rodata_res.end = __pa_symbol(__end_rodata) - 1;
111 	rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
112 
113 	data_res.start = __pa_symbol(_data);
114 	data_res.end = __pa_symbol(_edata) - 1;
115 	data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
116 
117 	bss_res.start = __pa_symbol(__bss_start);
118 	bss_res.end = __pa_symbol(__bss_stop) - 1;
119 	bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
120 
121 	kimage_res.start = code_res.start;
122 	kimage_res.end = bss_res.end;
123 	kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
124 
125 	ret = add_resource(&iomem_resource, &kimage_res);
126 	if (ret < 0)
127 		return ret;
128 
129 	ret = add_resource(&kimage_res, &code_res);
130 	if (ret < 0)
131 		return ret;
132 
133 	ret = add_resource(&kimage_res, &rodata_res);
134 	if (ret < 0)
135 		return ret;
136 
137 	ret = add_resource(&kimage_res, &data_res);
138 	if (ret < 0)
139 		return ret;
140 
141 	ret = add_resource(&kimage_res, &bss_res);
142 
143 	return ret;
144 }
145 
init_resources(void)146 static void __init init_resources(void)
147 {
148 	struct memblock_region *region = NULL;
149 	struct resource *res = NULL;
150 	struct resource *mem_res = NULL;
151 	size_t mem_res_sz = 0;
152 	int num_resources = 0, res_idx = 0, non_resv_res = 0;
153 	int ret = 0;
154 
155 	/* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
156 	num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
157 	res_idx = num_resources - 1;
158 
159 	mem_res_sz = num_resources * sizeof(*mem_res);
160 	mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
161 	if (!mem_res)
162 		panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
163 
164 	/*
165 	 * Start by adding the reserved regions, if they overlap
166 	 * with /memory regions, insert_resource later on will take
167 	 * care of it.
168 	 */
169 	ret = add_kernel_resources();
170 	if (ret < 0)
171 		goto error;
172 
173 #ifdef CONFIG_KEXEC_CORE
174 	if (crashk_res.start != crashk_res.end) {
175 		ret = add_resource(&iomem_resource, &crashk_res);
176 		if (ret < 0)
177 			goto error;
178 	}
179 	if (crashk_low_res.start != crashk_low_res.end) {
180 		ret = add_resource(&iomem_resource, &crashk_low_res);
181 		if (ret < 0)
182 			goto error;
183 	}
184 #endif
185 
186 #ifdef CONFIG_CRASH_DUMP
187 	if (elfcorehdr_size > 0) {
188 		elfcorehdr_res.start = elfcorehdr_addr;
189 		elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
190 		elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
191 		add_resource(&iomem_resource, &elfcorehdr_res);
192 	}
193 #endif
194 
195 	for_each_reserved_mem_region(region) {
196 		res = &mem_res[res_idx--];
197 
198 		res->name = "Reserved";
199 		res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
200 		res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
201 		res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
202 
203 		/*
204 		 * Ignore any other reserved regions within
205 		 * system memory.
206 		 */
207 		if (memblock_is_memory(res->start)) {
208 			/* Re-use this pre-allocated resource */
209 			res_idx++;
210 			continue;
211 		}
212 
213 		ret = add_resource(&iomem_resource, res);
214 		if (ret < 0)
215 			goto error;
216 	}
217 
218 	/* Add /memory regions to the resource tree */
219 	for_each_mem_region(region) {
220 		res = &mem_res[res_idx--];
221 		non_resv_res++;
222 
223 		if (unlikely(memblock_is_nomap(region))) {
224 			res->name = "Reserved";
225 			res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
226 		} else {
227 			res->name = "System RAM";
228 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
229 		}
230 
231 		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
232 		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
233 
234 		ret = add_resource(&iomem_resource, res);
235 		if (ret < 0)
236 			goto error;
237 	}
238 
239 	num_standard_resources = non_resv_res;
240 	standard_resources = &mem_res[res_idx + 1];
241 
242 	/* Clean-up any unused pre-allocated resources */
243 	if (res_idx >= 0)
244 		memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
245 	return;
246 
247  error:
248 	/* Better an empty resource tree than an inconsistent one */
249 	release_child_resources(&iomem_resource);
250 	memblock_free(mem_res, mem_res_sz);
251 }
252 
reserve_memblock_reserved_regions(void)253 static int __init reserve_memblock_reserved_regions(void)
254 {
255 	u64 i, j;
256 
257 	for (i = 0; i < num_standard_resources; i++) {
258 		struct resource *mem = &standard_resources[i];
259 		phys_addr_t r_start, r_end, mem_size = resource_size(mem);
260 
261 		if (!memblock_is_region_reserved(mem->start, mem_size))
262 			continue;
263 
264 		for_each_reserved_mem_range(j, &r_start, &r_end) {
265 			resource_size_t start, end;
266 
267 			start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
268 			end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
269 
270 			if (start > mem->end || end < mem->start)
271 				continue;
272 
273 			reserve_region_with_split(mem, start, end, "Reserved");
274 		}
275 	}
276 
277 	return 0;
278 }
279 arch_initcall(reserve_memblock_reserved_regions);
280 
parse_dtb(void)281 static void __init parse_dtb(void)
282 {
283 	/* Early scan of device tree from init memory */
284 	if (early_init_dt_scan(dtb_early_va, dtb_early_pa)) {
285 		const char *name = of_flat_dt_get_machine_name();
286 
287 		if (name) {
288 			pr_info("Machine model: %s\n", name);
289 			dump_stack_set_arch_desc("%s (DT)", name);
290 		}
291 	} else {
292 		pr_err("No DTB passed to the kernel\n");
293 	}
294 
295 #ifdef CONFIG_CMDLINE_FORCE
296 	strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
297 	pr_info("Forcing kernel command line to: %s\n", boot_command_line);
298 #endif
299 }
300 
301 extern void __init init_rt_signal_env(void);
302 
setup_arch(char ** cmdline_p)303 void __init setup_arch(char **cmdline_p)
304 {
305 	parse_dtb();
306 	setup_initial_init_mm(_stext, _etext, _edata, _end);
307 
308 	*cmdline_p = boot_command_line;
309 
310 	early_ioremap_setup();
311 	sbi_init();
312 	jump_label_init();
313 	parse_early_param();
314 
315 	efi_init();
316 	paging_init();
317 
318 	/* Parse the ACPI tables for possible boot-time configuration */
319 	acpi_boot_table_init();
320 
321 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
322 	unflatten_and_copy_device_tree();
323 #else
324 	unflatten_device_tree();
325 #endif
326 	misc_mem_init();
327 
328 	init_resources();
329 
330 #ifdef CONFIG_KASAN
331 	kasan_init();
332 #endif
333 
334 #ifdef CONFIG_SMP
335 	setup_smp();
336 #endif
337 
338 	if (!acpi_disabled)
339 		acpi_init_rintc_map();
340 
341 	riscv_init_cbo_blocksizes();
342 	riscv_fill_hwcap();
343 	apply_boot_alternatives();
344 	init_rt_signal_env();
345 
346 	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
347 	    riscv_isa_extension_available(NULL, ZICBOM))
348 		riscv_noncoherent_supported();
349 	riscv_set_dma_cache_alignment();
350 
351 	riscv_user_isa_enable();
352 }
353 
topology_init(void)354 static int __init topology_init(void)
355 {
356 	int i, ret;
357 
358 	for_each_possible_cpu(i) {
359 		struct cpu *cpu = &per_cpu(cpu_devices, i);
360 
361 		cpu->hotpluggable = cpu_has_hotplug(i);
362 		ret = register_cpu(cpu, i);
363 		if (unlikely(ret))
364 			pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
365 			       __func__, i, ret);
366 	}
367 
368 	return 0;
369 }
370 subsys_initcall(topology_init);
371 
free_initmem(void)372 void free_initmem(void)
373 {
374 	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
375 		set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
376 		if (IS_ENABLED(CONFIG_64BIT))
377 			set_kernel_memory(__init_begin, __init_end, set_memory_nx);
378 	}
379 
380 	free_initmem_default(POISON_FREE_INITMEM);
381 }
382 
dump_kernel_offset(struct notifier_block * self,unsigned long v,void * p)383 static int dump_kernel_offset(struct notifier_block *self,
384 			      unsigned long v, void *p)
385 {
386 	pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
387 		 kernel_map.virt_offset,
388 		 KERNEL_LINK_ADDR);
389 
390 	return 0;
391 }
392 
393 static struct notifier_block kernel_offset_notifier = {
394 	.notifier_call = dump_kernel_offset
395 };
396 
register_kernel_offset_dumper(void)397 static int __init register_kernel_offset_dumper(void)
398 {
399 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
400 		atomic_notifier_chain_register(&panic_notifier_list,
401 					       &kernel_offset_notifier);
402 
403 	return 0;
404 }
405 device_initcall(register_kernel_offset_dumper);
406