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