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