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