• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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(mem_res_sz, SMP_CACHE_BYTES);
154 	if (!mem_res)
155 		panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
156 
157 	/*
158 	 * Start by adding the reserved regions, if they overlap
159 	 * with /memory regions, insert_resource later on will take
160 	 * care of it.
161 	 */
162 	ret = add_kernel_resources();
163 	if (ret < 0)
164 		goto error;
165 
166 #ifdef CONFIG_CRASH_DUMP
167 	if (elfcorehdr_size > 0) {
168 		elfcorehdr_res.start = elfcorehdr_addr;
169 		elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
170 		elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
171 		add_resource(&iomem_resource, &elfcorehdr_res);
172 	}
173 #endif
174 
175 	for_each_reserved_mem_region(region) {
176 		res = &mem_res[res_idx--];
177 
178 		res->name = "Reserved";
179 		res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
180 		res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
181 		res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
182 
183 		/*
184 		 * Ignore any other reserved regions within
185 		 * system memory.
186 		 */
187 		if (memblock_is_memory(res->start)) {
188 			/* Re-use this pre-allocated resource */
189 			res_idx++;
190 			continue;
191 		}
192 
193 		ret = add_resource(&iomem_resource, res);
194 		if (ret < 0)
195 			goto error;
196 	}
197 
198 	/* Add /memory regions to the resource tree */
199 	for_each_mem_region(region) {
200 		res = &mem_res[res_idx--];
201 		non_resv_res++;
202 
203 		if (unlikely(memblock_is_nomap(region))) {
204 			res->name = "Reserved";
205 			res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
206 		} else {
207 			res->name = "System RAM";
208 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
209 		}
210 
211 		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
212 		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
213 
214 		ret = add_resource(&iomem_resource, res);
215 		if (ret < 0)
216 			goto error;
217 	}
218 
219 	num_standard_resources = non_resv_res;
220 	standard_resources = &mem_res[res_idx + 1];
221 
222 	/* Clean-up any unused pre-allocated resources */
223 	if (res_idx >= 0)
224 		memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
225 	return;
226 
227  error:
228 	/* Better an empty resource tree than an inconsistent one */
229 	release_child_resources(&iomem_resource);
230 	memblock_free(mem_res, mem_res_sz);
231 }
232 
reserve_memblock_reserved_regions(void)233 static int __init reserve_memblock_reserved_regions(void)
234 {
235 	u64 i, j;
236 
237 	for (i = 0; i < num_standard_resources; i++) {
238 		struct resource *mem = &standard_resources[i];
239 		phys_addr_t r_start, r_end, mem_size = resource_size(mem);
240 
241 		if (!memblock_is_region_reserved(mem->start, mem_size))
242 			continue;
243 
244 		for_each_reserved_mem_range(j, &r_start, &r_end) {
245 			resource_size_t start, end;
246 
247 			start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
248 			end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
249 
250 			if (start > mem->end || end < mem->start)
251 				continue;
252 
253 			reserve_region_with_split(mem, start, end, "Reserved");
254 		}
255 	}
256 
257 	return 0;
258 }
259 arch_initcall(reserve_memblock_reserved_regions);
260 
parse_dtb(void)261 static void __init parse_dtb(void)
262 {
263 	/* Early scan of device tree from init memory */
264 	if (early_init_dt_scan(dtb_early_va, dtb_early_pa)) {
265 		const char *name = of_flat_dt_get_machine_name();
266 
267 		if (name) {
268 			pr_info("Machine model: %s\n", name);
269 			dump_stack_set_arch_desc("%s (DT)", name);
270 		}
271 	} else {
272 		pr_err("No DTB passed to the kernel\n");
273 	}
274 
275 #ifdef CONFIG_CMDLINE_FORCE
276 	strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
277 	pr_info("Forcing kernel command line to: %s\n", boot_command_line);
278 #endif
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 }
335 
arch_cpu_is_hotpluggable(int cpu)336 bool arch_cpu_is_hotpluggable(int cpu)
337 {
338 	return cpu_has_hotplug(cpu);
339 }
340 
free_initmem(void)341 void free_initmem(void)
342 {
343 	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
344 		set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
345 		if (IS_ENABLED(CONFIG_64BIT))
346 			set_kernel_memory(__init_begin, __init_end, set_memory_nx);
347 	}
348 
349 	free_initmem_default(POISON_FREE_INITMEM);
350 }
351 
dump_kernel_offset(struct notifier_block * self,unsigned long v,void * p)352 static int dump_kernel_offset(struct notifier_block *self,
353 			      unsigned long v, void *p)
354 {
355 	pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
356 		 kernel_map.virt_offset,
357 		 KERNEL_LINK_ADDR);
358 
359 	return 0;
360 }
361 
362 static struct notifier_block kernel_offset_notifier = {
363 	.notifier_call = dump_kernel_offset
364 };
365 
register_kernel_offset_dumper(void)366 static int __init register_kernel_offset_dumper(void)
367 {
368 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
369 		atomic_notifier_chain_register(&panic_notifier_list,
370 					       &kernel_offset_notifier);
371 
372 	return 0;
373 }
374 device_initcall(register_kernel_offset_dumper);
375