xref: /openbmc/linux/arch/riscv/kernel/setup.c (revision 65417d9f)
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_HVC_RISCV_SBI
42 #include <asm/hvc_riscv_sbi.h>
43 #endif
44 
45 #ifdef CONFIG_DUMMY_CONSOLE
46 struct screen_info screen_info = {
47 	.orig_video_lines	= 30,
48 	.orig_video_cols	= 80,
49 	.orig_video_mode	= 0,
50 	.orig_video_ega_bx	= 0,
51 	.orig_video_isVGA	= 1,
52 	.orig_video_points	= 8
53 };
54 #endif
55 
56 #ifdef CONFIG_CMDLINE_BOOL
57 static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
58 #endif /* CONFIG_CMDLINE_BOOL */
59 
60 unsigned long va_pa_offset;
61 EXPORT_SYMBOL(va_pa_offset);
62 unsigned long pfn_base;
63 EXPORT_SYMBOL(pfn_base);
64 
65 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
66 EXPORT_SYMBOL(empty_zero_page);
67 
68 /* The lucky hart to first increment this variable will boot the other cores */
69 atomic_t hart_lottery;
70 
71 #ifdef CONFIG_BLK_DEV_INITRD
72 static void __init setup_initrd(void)
73 {
74 	extern char __initramfs_start[];
75 	extern unsigned long __initramfs_size;
76 	unsigned long size;
77 
78 	if (__initramfs_size > 0) {
79 		initrd_start = (unsigned long)(&__initramfs_start);
80 		initrd_end = initrd_start + __initramfs_size;
81 	}
82 
83 	if (initrd_start >= initrd_end) {
84 		printk(KERN_INFO "initrd not found or empty");
85 		goto disable;
86 	}
87 	if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
88 		printk(KERN_ERR "initrd extends beyond end of memory");
89 		goto disable;
90 	}
91 
92 	size =  initrd_end - initrd_start;
93 	memblock_reserve(__pa(initrd_start), size);
94 	initrd_below_start_ok = 1;
95 
96 	printk(KERN_INFO "Initial ramdisk at: 0x%p (%lu bytes)\n",
97 		(void *)(initrd_start), size);
98 	return;
99 disable:
100 	pr_cont(" - disabling initrd\n");
101 	initrd_start = 0;
102 	initrd_end = 0;
103 }
104 #endif /* CONFIG_BLK_DEV_INITRD */
105 
106 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
107 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
108 
109 #ifndef __PAGETABLE_PMD_FOLDED
110 #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
111 pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
112 pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
113 #endif
114 
115 asmlinkage void __init setup_vm(void)
116 {
117 	extern char _start;
118 	uintptr_t i;
119 	uintptr_t pa = (uintptr_t) &_start;
120 	pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
121 
122 	va_pa_offset = PAGE_OFFSET - pa;
123 	pfn_base = PFN_DOWN(pa);
124 
125 	/* Sanity check alignment and size */
126 	BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
127 	BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
128 
129 #ifndef __PAGETABLE_PMD_FOLDED
130 	trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
131 		pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
132 			__pgprot(_PAGE_TABLE));
133 	trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
134 
135 	for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
136 		size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
137 		swapper_pg_dir[o] =
138 			pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
139 				__pgprot(_PAGE_TABLE));
140 	}
141 	for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
142 		swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
143 #else
144 	trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
145 		pfn_pgd(PFN_DOWN(pa), prot);
146 
147 	for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
148 		size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
149 		swapper_pg_dir[o] =
150 			pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
151 	}
152 #endif
153 }
154 
155 void __init sbi_save(unsigned int hartid, void *dtb)
156 {
157 	early_init_dt_scan(__va(dtb));
158 }
159 
160 /*
161  * Allow the user to manually add a memory region (in case DTS is broken);
162  * "mem_end=nn[KkMmGg]"
163  */
164 static int __init mem_end_override(char *p)
165 {
166 	resource_size_t base, end;
167 
168 	if (!p)
169 		return -EINVAL;
170 	base = (uintptr_t) __pa(PAGE_OFFSET);
171 	end = memparse(p, &p) & PMD_MASK;
172 	if (end == 0)
173 		return -EINVAL;
174 	memblock_add(base, end - base);
175 	return 0;
176 }
177 early_param("mem_end", mem_end_override);
178 
179 static void __init setup_bootmem(void)
180 {
181 	struct memblock_region *reg;
182 	phys_addr_t mem_size = 0;
183 
184 	/* Find the memory region containing the kernel */
185 	for_each_memblock(memory, reg) {
186 		phys_addr_t vmlinux_end = __pa(_end);
187 		phys_addr_t end = reg->base + reg->size;
188 
189 		if (reg->base <= vmlinux_end && vmlinux_end <= end) {
190 			/*
191 			 * Reserve from the start of the region to the end of
192 			 * the kernel
193 			 */
194 			memblock_reserve(reg->base, vmlinux_end - reg->base);
195 			mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
196 		}
197 	}
198 	BUG_ON(mem_size == 0);
199 
200 	set_max_mapnr(PFN_DOWN(mem_size));
201 	max_low_pfn = pfn_base + PFN_DOWN(mem_size);
202 
203 #ifdef CONFIG_BLK_DEV_INITRD
204 	setup_initrd();
205 #endif /* CONFIG_BLK_DEV_INITRD */
206 
207 	early_init_fdt_reserve_self();
208 	early_init_fdt_scan_reserved_mem();
209 	memblock_allow_resize();
210 	memblock_dump_all();
211 }
212 
213 void __init setup_arch(char **cmdline_p)
214 {
215 #if defined(CONFIG_HVC_RISCV_SBI)
216 	if (likely(early_console == NULL)) {
217 		early_console = &riscv_sbi_early_console_dev;
218 		register_console(early_console);
219 	}
220 #endif
221 
222 #ifdef CONFIG_CMDLINE_BOOL
223 #ifdef CONFIG_CMDLINE_OVERRIDE
224 	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
225 #else
226 	if (builtin_cmdline[0] != '\0') {
227 		/* Append bootloader command line to built-in */
228 		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
229 		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
230 		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
231 	}
232 #endif /* CONFIG_CMDLINE_OVERRIDE */
233 #endif /* CONFIG_CMDLINE_BOOL */
234 	*cmdline_p = boot_command_line;
235 
236 	parse_early_param();
237 
238 	init_mm.start_code = (unsigned long) _stext;
239 	init_mm.end_code   = (unsigned long) _etext;
240 	init_mm.end_data   = (unsigned long) _edata;
241 	init_mm.brk        = (unsigned long) _end;
242 
243 	setup_bootmem();
244 	paging_init();
245 	unflatten_device_tree();
246 
247 #ifdef CONFIG_SMP
248 	setup_smp();
249 #endif
250 
251 #ifdef CONFIG_DUMMY_CONSOLE
252 	conswitchp = &dummy_con;
253 #endif
254 
255 	riscv_fill_hwcap();
256 }
257 
258 static int __init riscv_device_init(void)
259 {
260 	return of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
261 }
262 subsys_initcall_sync(riscv_device_init);
263