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