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