• 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/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 
riscv_cpuid_to_hartid_mask(const struct cpumask * in,struct cpumask * out)62 void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out)
63 {
64 	int cpu;
65 
66 	cpumask_clear(out);
67 	for_each_cpu(cpu, in)
68 		cpumask_set_cpu(cpuid_to_hartid_map(cpu), out);
69 }
70 EXPORT_SYMBOL_GPL(riscv_cpuid_to_hartid_mask);
71 
72 /*
73  * Place kernel memory regions on the resource tree so that
74  * kexec-tools can retrieve them from /proc/iomem. While there
75  * also add "System RAM" regions for compatibility with other
76  * archs, and the rest of the known regions for completeness.
77  */
78 static struct resource kimage_res = { .name = "Kernel image", };
79 static struct resource code_res = { .name = "Kernel code", };
80 static struct resource data_res = { .name = "Kernel data", };
81 static struct resource rodata_res = { .name = "Kernel rodata", };
82 static struct resource bss_res = { .name = "Kernel bss", };
83 #ifdef CONFIG_CRASH_DUMP
84 static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
85 #endif
86 
add_resource(struct resource * parent,struct resource * res)87 static int __init add_resource(struct resource *parent,
88 				struct resource *res)
89 {
90 	int ret = 0;
91 
92 	ret = insert_resource(parent, res);
93 	if (ret < 0) {
94 		pr_err("Failed to add a %s resource at %llx\n",
95 			res->name, (unsigned long long) res->start);
96 		return ret;
97 	}
98 
99 	return 1;
100 }
101 
add_kernel_resources(void)102 static int __init add_kernel_resources(void)
103 {
104 	int ret = 0;
105 
106 	/*
107 	 * The memory region of the kernel image is continuous and
108 	 * was reserved on setup_bootmem, register it here as a
109 	 * resource, with the various segments of the image as
110 	 * child nodes.
111 	 */
112 
113 	code_res.start = __pa_symbol(_text);
114 	code_res.end = __pa_symbol(_etext) - 1;
115 	code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
116 
117 	rodata_res.start = __pa_symbol(__start_rodata);
118 	rodata_res.end = __pa_symbol(__end_rodata) - 1;
119 	rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
120 
121 	data_res.start = __pa_symbol(_data);
122 	data_res.end = __pa_symbol(_edata) - 1;
123 	data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
124 
125 	bss_res.start = __pa_symbol(__bss_start);
126 	bss_res.end = __pa_symbol(__bss_stop) - 1;
127 	bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
128 
129 	kimage_res.start = code_res.start;
130 	kimage_res.end = bss_res.end;
131 	kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
132 
133 	ret = add_resource(&iomem_resource, &kimage_res);
134 	if (ret < 0)
135 		return ret;
136 
137 	ret = add_resource(&kimage_res, &code_res);
138 	if (ret < 0)
139 		return ret;
140 
141 	ret = add_resource(&kimage_res, &rodata_res);
142 	if (ret < 0)
143 		return ret;
144 
145 	ret = add_resource(&kimage_res, &data_res);
146 	if (ret < 0)
147 		return ret;
148 
149 	ret = add_resource(&kimage_res, &bss_res);
150 
151 	return ret;
152 }
153 
init_resources(void)154 static void __init init_resources(void)
155 {
156 	struct memblock_region *region = NULL;
157 	struct resource *res = NULL;
158 	struct resource *mem_res = NULL;
159 	size_t mem_res_sz = 0;
160 	int num_resources = 0, res_idx = 0;
161 	int ret = 0;
162 
163 	/* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
164 	num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
165 	res_idx = num_resources - 1;
166 
167 	mem_res_sz = num_resources * sizeof(*mem_res);
168 	mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
169 	if (!mem_res)
170 		panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
171 
172 	/*
173 	 * Start by adding the reserved regions, if they overlap
174 	 * with /memory regions, insert_resource later on will take
175 	 * care of it.
176 	 */
177 	ret = add_kernel_resources();
178 	if (ret < 0)
179 		goto error;
180 
181 #ifdef CONFIG_KEXEC_CORE
182 	if (crashk_res.start != crashk_res.end) {
183 		ret = add_resource(&iomem_resource, &crashk_res);
184 		if (ret < 0)
185 			goto error;
186 	}
187 #endif
188 
189 #ifdef CONFIG_CRASH_DUMP
190 	if (elfcorehdr_size > 0) {
191 		elfcorehdr_res.start = elfcorehdr_addr;
192 		elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
193 		elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
194 		add_resource(&iomem_resource, &elfcorehdr_res);
195 	}
196 #endif
197 
198 	for_each_reserved_mem_region(region) {
199 		res = &mem_res[res_idx--];
200 
201 		res->name = "Reserved";
202 		res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
203 		res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
204 		res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
205 
206 		/*
207 		 * Ignore any other reserved regions within
208 		 * system memory.
209 		 */
210 		if (memblock_is_memory(res->start)) {
211 			/* Re-use this pre-allocated resource */
212 			res_idx++;
213 			continue;
214 		}
215 
216 		ret = add_resource(&iomem_resource, res);
217 		if (ret < 0)
218 			goto error;
219 	}
220 
221 	/* Add /memory regions to the resource tree */
222 	for_each_mem_region(region) {
223 		res = &mem_res[res_idx--];
224 
225 		if (unlikely(memblock_is_nomap(region))) {
226 			res->name = "Reserved";
227 			res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE;
228 		} else {
229 			res->name = "System RAM";
230 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
231 		}
232 
233 		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
234 		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
235 
236 		ret = add_resource(&iomem_resource, res);
237 		if (ret < 0)
238 			goto error;
239 	}
240 
241 	/* Clean-up any unused pre-allocated resources */
242 	if (res_idx >= 0)
243 		memblock_free(__pa(mem_res), (res_idx + 1) * sizeof(*mem_res));
244 	return;
245 
246  error:
247 	/* Better an empty resource tree than an inconsistent one */
248 	release_child_resources(&iomem_resource);
249 	memblock_free(__pa(mem_res), mem_res_sz);
250 }
251 
252 
parse_dtb(void)253 static void __init parse_dtb(void)
254 {
255 	/* Early scan of device tree from init memory */
256 	if (early_init_dt_scan(dtb_early_va)) {
257 		const char *name = of_flat_dt_get_machine_name();
258 
259 		if (name) {
260 			pr_info("Machine model: %s\n", name);
261 			dump_stack_set_arch_desc("%s (DT)", name);
262 		}
263 	} else {
264 		pr_err("No DTB passed to the kernel\n");
265 	}
266 
267 #ifdef CONFIG_CMDLINE_FORCE
268 	strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
269 	pr_info("Forcing kernel command line to: %s\n", boot_command_line);
270 #endif
271 }
272 
setup_arch(char ** cmdline_p)273 void __init setup_arch(char **cmdline_p)
274 {
275 	parse_dtb();
276 	setup_initial_init_mm(_stext, _etext, _edata, _end);
277 
278 	*cmdline_p = boot_command_line;
279 
280 	early_ioremap_setup();
281 	jump_label_init();
282 	parse_early_param();
283 
284 	efi_init();
285 	paging_init();
286 #if IS_ENABLED(CONFIG_BUILTIN_DTB)
287 	unflatten_and_copy_device_tree();
288 #else
289 	unflatten_device_tree();
290 #endif
291 	misc_mem_init();
292 
293 	init_resources();
294 	sbi_init();
295 
296 #ifdef CONFIG_KASAN
297 	kasan_init();
298 #endif
299 
300 #ifdef CONFIG_SMP
301 	setup_smp();
302 #endif
303 
304 	riscv_fill_hwcap();
305 }
306 
topology_init(void)307 static int __init topology_init(void)
308 {
309 	int i, ret;
310 
311 	for_each_online_node(i)
312 		register_one_node(i);
313 
314 	for_each_possible_cpu(i) {
315 		struct cpu *cpu = &per_cpu(cpu_devices, i);
316 
317 		cpu->hotpluggable = cpu_has_hotplug(i);
318 		ret = register_cpu(cpu, i);
319 		if (unlikely(ret))
320 			pr_warn("Warning: %s: register_cpu %d failed (%d)\n",
321 			       __func__, i, ret);
322 	}
323 
324 	return 0;
325 }
326 subsys_initcall(topology_init);
327 
free_initmem(void)328 void free_initmem(void)
329 {
330 	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
331 		set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
332 		if (IS_ENABLED(CONFIG_64BIT))
333 			set_kernel_memory(__init_begin, __init_end, set_memory_nx);
334 	}
335 
336 	free_initmem_default(POISON_FREE_INITMEM);
337 }
338