xref: /openbmc/linux/arch/riscv/kernel/setup.c (revision d6e2d652)
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 
25 #include <asm/acpi.h>
26 #include <asm/alternative.h>
27 #include <asm/cacheflush.h>
28 #include <asm/cpu_ops.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 #if defined(CONFIG_DUMMY_CONSOLE) || defined(CONFIG_EFI)
43 struct screen_info screen_info __section(".data") = {
44 	.orig_video_lines	= 30,
45 	.orig_video_cols	= 80,
46 	.orig_video_mode	= 0,
47 	.orig_video_ega_bx	= 0,
48 	.orig_video_isVGA	= 1,
49 	.orig_video_points	= 8
50 };
51 #endif
52 
53 /*
54  * The lucky hart to first increment this variable will boot the other cores.
55  * This is used before the kernel initializes the BSS so it can't be in the
56  * BSS.
57  */
58 atomic_t hart_lottery __section(".sdata")
59 #ifdef CONFIG_XIP_KERNEL
60 = ATOMIC_INIT(0xC001BEEF)
61 #endif
62 ;
63 unsigned long boot_cpu_hartid;
64 static DEFINE_PER_CPU(struct cpu, cpu_devices);
65 
66 /*
67  * Place kernel memory regions on the resource tree so that
68  * kexec-tools can retrieve them from /proc/iomem. While there
69  * also add "System RAM" regions for compatibility with other
70  * archs, and the rest of the known regions for completeness.
71  */
72 static struct resource kimage_res = { .name = "Kernel image", };
73 static struct resource code_res = { .name = "Kernel code", };
74 static struct resource data_res = { .name = "Kernel data", };
75 static struct resource rodata_res = { .name = "Kernel rodata", };
76 static struct resource bss_res = { .name = "Kernel bss", };
77 #ifdef CONFIG_CRASH_DUMP
78 static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
79 #endif
80 
81 static int __init add_resource(struct resource *parent,
82 				struct resource *res)
83 {
84 	int ret = 0;
85 
86 	ret = insert_resource(parent, res);
87 	if (ret < 0) {
88 		pr_err("Failed to add a %s resource at %llx\n",
89 			res->name, (unsigned long long) res->start);
90 		return ret;
91 	}
92 
93 	return 1;
94 }
95 
96 static int __init add_kernel_resources(void)
97 {
98 	int ret = 0;
99 
100 	/*
101 	 * The memory region of the kernel image is continuous and
102 	 * was reserved on setup_bootmem, register it here as a
103 	 * resource, with the various segments of the image as
104 	 * child nodes.
105 	 */
106 
107 	code_res.start = __pa_symbol(_text);
108 	code_res.end = __pa_symbol(_etext) - 1;
109 	code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
110 
111 	rodata_res.start = __pa_symbol(__start_rodata);
112 	rodata_res.end = __pa_symbol(__end_rodata) - 1;
113 	rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
114 
115 	data_res.start = __pa_symbol(_data);
116 	data_res.end = __pa_symbol(_edata) - 1;
117 	data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
118 
119 	bss_res.start = __pa_symbol(__bss_start);
120 	bss_res.end = __pa_symbol(__bss_stop) - 1;
121 	bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
122 
123 	kimage_res.start = code_res.start;
124 	kimage_res.end = bss_res.end;
125 	kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
126 
127 	ret = add_resource(&iomem_resource, &kimage_res);
128 	if (ret < 0)
129 		return ret;
130 
131 	ret = add_resource(&kimage_res, &code_res);
132 	if (ret < 0)
133 		return ret;
134 
135 	ret = add_resource(&kimage_res, &rodata_res);
136 	if (ret < 0)
137 		return ret;
138 
139 	ret = add_resource(&kimage_res, &data_res);
140 	if (ret < 0)
141 		return ret;
142 
143 	ret = add_resource(&kimage_res, &bss_res);
144 
145 	return ret;
146 }
147 
148 static void __init init_resources(void)
149 {
150 	struct memblock_region *region = NULL;
151 	struct resource *res = NULL;
152 	struct resource *mem_res = NULL;
153 	size_t mem_res_sz = 0;
154 	int num_resources = 0, res_idx = 0;
155 	int ret = 0;
156 
157 	/* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
158 	num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
159 	res_idx = num_resources - 1;
160 
161 	mem_res_sz = num_resources * sizeof(*mem_res);
162 	mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
163 	if (!mem_res)
164 		panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
165 
166 	/*
167 	 * Start by adding the reserved regions, if they overlap
168 	 * with /memory regions, insert_resource later on will take
169 	 * care of it.
170 	 */
171 	ret = add_kernel_resources();
172 	if (ret < 0)
173 		goto error;
174 
175 #ifdef CONFIG_KEXEC_CORE
176 	if (crashk_res.start != crashk_res.end) {
177 		ret = add_resource(&iomem_resource, &crashk_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 
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)) {
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 
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 	init_rt_signal_env();
310 	apply_boot_alternatives();
311 	if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
312 	    riscv_isa_extension_available(NULL, ZICBOM))
313 		riscv_noncoherent_supported();
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