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