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