Loading...
1/*
2 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
3 * Chen Liqin <liqin.chen@sunplusct.com>
4 * Lennox Wu <lennox.wu@sunplusct.com>
5 * Copyright (C) 2012 Regents of the University of California
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see the file COPYING, or write
19 * to the Free Software Foundation, Inc.,
20 */
21
22#include <linux/init.h>
23#include <linux/mm.h>
24#include <linux/memblock.h>
25#include <linux/sched.h>
26#include <linux/initrd.h>
27#include <linux/console.h>
28#include <linux/screen_info.h>
29#include <linux/of_fdt.h>
30#include <linux/of_platform.h>
31#include <linux/sched/task.h>
32
33#include <asm/setup.h>
34#include <asm/sections.h>
35#include <asm/pgtable.h>
36#include <asm/smp.h>
37#include <asm/sbi.h>
38#include <asm/tlbflush.h>
39#include <asm/thread_info.h>
40
41#ifdef CONFIG_DUMMY_CONSOLE
42struct screen_info screen_info = {
43 .orig_video_lines = 30,
44 .orig_video_cols = 80,
45 .orig_video_mode = 0,
46 .orig_video_ega_bx = 0,
47 .orig_video_isVGA = 1,
48 .orig_video_points = 8
49};
50#endif
51
52unsigned long va_pa_offset;
53EXPORT_SYMBOL(va_pa_offset);
54unsigned long pfn_base;
55EXPORT_SYMBOL(pfn_base);
56
57unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
58EXPORT_SYMBOL(empty_zero_page);
59
60/* The lucky hart to first increment this variable will boot the other cores */
61atomic_t hart_lottery;
62
63#ifdef CONFIG_BLK_DEV_INITRD
64static void __init setup_initrd(void)
65{
66 extern char __initramfs_start[];
67 extern unsigned long __initramfs_size;
68 unsigned long size;
69
70 if (__initramfs_size > 0) {
71 initrd_start = (unsigned long)(&__initramfs_start);
72 initrd_end = initrd_start + __initramfs_size;
73 }
74
75 if (initrd_start >= initrd_end) {
76 printk(KERN_INFO "initrd not found or empty");
77 goto disable;
78 }
79 if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
80 printk(KERN_ERR "initrd extends beyond end of memory");
81 goto disable;
82 }
83
84 size = initrd_end - initrd_start;
85 memblock_reserve(__pa(initrd_start), size);
86 initrd_below_start_ok = 1;
87
88 printk(KERN_INFO "Initial ramdisk at: 0x%p (%lu bytes)\n",
89 (void *)(initrd_start), size);
90 return;
91disable:
92 pr_cont(" - disabling initrd\n");
93 initrd_start = 0;
94 initrd_end = 0;
95}
96#endif /* CONFIG_BLK_DEV_INITRD */
97
98pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
99pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
100
101#ifndef __PAGETABLE_PMD_FOLDED
102#define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
103pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
104pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
105#endif
106
107asmlinkage void __init setup_vm(void)
108{
109 extern char _start;
110 uintptr_t i;
111 uintptr_t pa = (uintptr_t) &_start;
112 pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
113
114 va_pa_offset = PAGE_OFFSET - pa;
115 pfn_base = PFN_DOWN(pa);
116
117 /* Sanity check alignment and size */
118 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
119 BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
120
121#ifndef __PAGETABLE_PMD_FOLDED
122 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
123 pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
124 __pgprot(_PAGE_TABLE));
125 trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
126
127 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
128 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
129 swapper_pg_dir[o] =
130 pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
131 __pgprot(_PAGE_TABLE));
132 }
133 for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
134 swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
135#else
136 trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
137 pfn_pgd(PFN_DOWN(pa), prot);
138
139 for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
140 size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
141 swapper_pg_dir[o] =
142 pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
143 }
144#endif
145}
146
147void __init parse_dtb(unsigned int hartid, void *dtb)
148{
149 early_init_dt_scan(__va(dtb));
150}
151
152static void __init setup_bootmem(void)
153{
154 struct memblock_region *reg;
155 phys_addr_t mem_size = 0;
156
157 /* Find the memory region containing the kernel */
158 for_each_memblock(memory, reg) {
159 phys_addr_t vmlinux_end = __pa(_end);
160 phys_addr_t end = reg->base + reg->size;
161
162 if (reg->base <= vmlinux_end && vmlinux_end <= end) {
163 /*
164 * Reserve from the start of the region to the end of
165 * the kernel
166 */
167 memblock_reserve(reg->base, vmlinux_end - reg->base);
168 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
169 }
170 }
171 BUG_ON(mem_size == 0);
172
173 set_max_mapnr(PFN_DOWN(mem_size));
174 max_low_pfn = pfn_base + PFN_DOWN(mem_size);
175
176#ifdef CONFIG_BLK_DEV_INITRD
177 setup_initrd();
178#endif /* CONFIG_BLK_DEV_INITRD */
179
180 early_init_fdt_reserve_self();
181 early_init_fdt_scan_reserved_mem();
182 memblock_allow_resize();
183 memblock_dump_all();
184
185 for_each_memblock(memory, reg) {
186 unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
187 unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
188
189 memblock_set_node(PFN_PHYS(start_pfn),
190 PFN_PHYS(end_pfn - start_pfn),
191 &memblock.memory, 0);
192 }
193}
194
195void __init setup_arch(char **cmdline_p)
196{
197 *cmdline_p = boot_command_line;
198
199 parse_early_param();
200
201 init_mm.start_code = (unsigned long) _stext;
202 init_mm.end_code = (unsigned long) _etext;
203 init_mm.end_data = (unsigned long) _edata;
204 init_mm.brk = (unsigned long) _end;
205
206 setup_bootmem();
207 paging_init();
208 unflatten_device_tree();
209
210#ifdef CONFIG_SMP
211 setup_smp();
212#endif
213
214#ifdef CONFIG_DUMMY_CONSOLE
215 conswitchp = &dummy_con;
216#endif
217
218 riscv_fill_hwcap();
219}
220
221static int __init riscv_device_init(void)
222{
223 return of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
224}
225subsys_initcall_sync(riscv_device_init);
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 */
47atomic_t hart_lottery __section(".sdata")
48#ifdef CONFIG_XIP_KERNEL
49= ATOMIC_INIT(0xC001BEEF)
50#endif
51;
52unsigned 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 */
60static struct resource kimage_res = { .name = "Kernel image", };
61static struct resource code_res = { .name = "Kernel code", };
62static struct resource data_res = { .name = "Kernel data", };
63static struct resource rodata_res = { .name = "Kernel rodata", };
64static struct resource bss_res = { .name = "Kernel bss", };
65#ifdef CONFIG_CRASH_DUMP
66static struct resource elfcorehdr_res = { .name = "ELF Core hdr", };
67#endif
68
69static int __init add_resource(struct resource *parent,
70 struct resource *res)
71{
72 int ret = 0;
73
74 ret = insert_resource(parent, res);
75 if (ret < 0) {
76 pr_err("Failed to add a %s resource at %llx\n",
77 res->name, (unsigned long long) res->start);
78 return ret;
79 }
80
81 return 1;
82}
83
84static int __init add_kernel_resources(void)
85{
86 int ret = 0;
87
88 /*
89 * The memory region of the kernel image is continuous and
90 * was reserved on setup_bootmem, register it here as a
91 * resource, with the various segments of the image as
92 * child nodes.
93 */
94
95 code_res.start = __pa_symbol(_text);
96 code_res.end = __pa_symbol(_etext) - 1;
97 code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
98
99 rodata_res.start = __pa_symbol(__start_rodata);
100 rodata_res.end = __pa_symbol(__end_rodata) - 1;
101 rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
102
103 data_res.start = __pa_symbol(_data);
104 data_res.end = __pa_symbol(_edata) - 1;
105 data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
106
107 bss_res.start = __pa_symbol(__bss_start);
108 bss_res.end = __pa_symbol(__bss_stop) - 1;
109 bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
110
111 kimage_res.start = code_res.start;
112 kimage_res.end = bss_res.end;
113 kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
114
115 ret = add_resource(&iomem_resource, &kimage_res);
116 if (ret < 0)
117 return ret;
118
119 ret = add_resource(&kimage_res, &code_res);
120 if (ret < 0)
121 return ret;
122
123 ret = add_resource(&kimage_res, &rodata_res);
124 if (ret < 0)
125 return ret;
126
127 ret = add_resource(&kimage_res, &data_res);
128 if (ret < 0)
129 return ret;
130
131 ret = add_resource(&kimage_res, &bss_res);
132
133 return ret;
134}
135
136static void __init init_resources(void)
137{
138 struct memblock_region *region = NULL;
139 struct resource *res = NULL;
140 struct resource *mem_res = NULL;
141 size_t mem_res_sz = 0;
142 int num_resources = 0, res_idx = 0;
143 int ret = 0;
144
145 /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */
146 num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1;
147 res_idx = num_resources - 1;
148
149 mem_res_sz = num_resources * sizeof(*mem_res);
150 mem_res = memblock_alloc(mem_res_sz, SMP_CACHE_BYTES);
151 if (!mem_res)
152 panic("%s: Failed to allocate %zu bytes\n", __func__, mem_res_sz);
153
154 /*
155 * Start by adding the reserved regions, if they overlap
156 * with /memory regions, insert_resource later on will take
157 * care of it.
158 */
159 ret = add_kernel_resources();
160 if (ret < 0)
161 goto error;
162
163#ifdef CONFIG_CRASH_DUMP
164 if (elfcorehdr_size > 0) {
165 elfcorehdr_res.start = elfcorehdr_addr;
166 elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1;
167 elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
168 add_resource(&iomem_resource, &elfcorehdr_res);
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_EXCLUSIVE;
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_EXCLUSIVE;
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 if (res_idx >= 0)
217 memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res));
218 return;
219
220 error:
221 /* Better an empty resource tree than an inconsistent one */
222 release_child_resources(&iomem_resource);
223 memblock_free(mem_res, mem_res_sz);
224}
225
226
227static void __init parse_dtb(void)
228{
229 /* Early scan of device tree from init memory */
230 if (early_init_dt_scan(dtb_early_va, dtb_early_pa)) {
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 } else {
238 pr_err("No DTB passed to the kernel\n");
239 }
240
241#ifdef CONFIG_CMDLINE_FORCE
242 strscpy(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#if defined(CONFIG_RISCV_COMBO_SPINLOCKS)
248DEFINE_STATIC_KEY_TRUE(qspinlock_key);
249EXPORT_SYMBOL(qspinlock_key);
250#endif
251
252static void __init riscv_spinlock_init(void)
253{
254 char *using_ext = NULL;
255
256 if (IS_ENABLED(CONFIG_RISCV_TICKET_SPINLOCKS)) {
257 pr_info("Ticket spinlock: enabled\n");
258 return;
259 }
260
261 if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) &&
262 IS_ENABLED(CONFIG_RISCV_ISA_ZACAS) &&
263 riscv_isa_extension_available(NULL, ZABHA) &&
264 riscv_isa_extension_available(NULL, ZACAS)) {
265 using_ext = "using Zabha";
266 } else if (riscv_isa_extension_available(NULL, ZICCRSE)) {
267 using_ext = "using Ziccrse";
268 }
269#if defined(CONFIG_RISCV_COMBO_SPINLOCKS)
270 else {
271 static_branch_disable(&qspinlock_key);
272 pr_info("Ticket spinlock: enabled\n");
273 return;
274 }
275#endif
276
277 if (!using_ext)
278 pr_err("Queued spinlock without Zabha or Ziccrse");
279 else
280 pr_info("Queued spinlock %s: enabled\n", using_ext);
281}
282
283extern void __init init_rt_signal_env(void);
284
285void __init setup_arch(char **cmdline_p)
286{
287 parse_dtb();
288 setup_initial_init_mm(_stext, _etext, _edata, _end);
289
290 *cmdline_p = boot_command_line;
291
292 early_ioremap_setup();
293 sbi_init();
294 jump_label_init();
295 parse_early_param();
296
297 efi_init();
298 paging_init();
299
300 /* Parse the ACPI tables for possible boot-time configuration */
301 acpi_boot_table_init();
302
303#if IS_ENABLED(CONFIG_BUILTIN_DTB)
304 unflatten_and_copy_device_tree();
305#else
306 unflatten_device_tree();
307#endif
308 misc_mem_init();
309
310 init_resources();
311
312#ifdef CONFIG_KASAN
313 kasan_init();
314#endif
315
316#ifdef CONFIG_SMP
317 setup_smp();
318#endif
319
320 if (!acpi_disabled) {
321 acpi_init_rintc_map();
322 acpi_map_cpus_to_nodes();
323 }
324
325 riscv_init_cbo_blocksizes();
326 riscv_fill_hwcap();
327 apply_boot_alternatives();
328 init_rt_signal_env();
329
330 if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) &&
331 riscv_isa_extension_available(NULL, ZICBOM))
332 riscv_noncoherent_supported();
333 riscv_set_dma_cache_alignment();
334
335 riscv_user_isa_enable();
336 riscv_spinlock_init();
337}
338
339bool arch_cpu_is_hotpluggable(int cpu)
340{
341 return cpu_has_hotplug(cpu);
342}
343
344void free_initmem(void)
345{
346 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
347 set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx);
348 if (IS_ENABLED(CONFIG_64BIT))
349 set_kernel_memory(__init_begin, __init_end, set_memory_nx);
350 }
351
352 free_initmem_default(POISON_FREE_INITMEM);
353}
354
355static int dump_kernel_offset(struct notifier_block *self,
356 unsigned long v, void *p)
357{
358 pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
359 kernel_map.virt_offset,
360 KERNEL_LINK_ADDR);
361
362 return 0;
363}
364
365static struct notifier_block kernel_offset_notifier = {
366 .notifier_call = dump_kernel_offset
367};
368
369static int __init register_kernel_offset_dumper(void)
370{
371 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
372 atomic_notifier_chain_register(&panic_notifier_list,
373 &kernel_offset_notifier);
374
375 return 0;
376}
377device_initcall(register_kernel_offset_dumper);