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