xref: /openbmc/linux/arch/riscv/kernel/setup.c (revision ba61bb17)
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 #include <linux/swiotlb.h>
33 
34 #include <asm/setup.h>
35 #include <asm/sections.h>
36 #include <asm/pgtable.h>
37 #include <asm/smp.h>
38 #include <asm/sbi.h>
39 #include <asm/tlbflush.h>
40 #include <asm/thread_info.h>
41 
42 #ifdef CONFIG_DUMMY_CONSOLE
43 struct screen_info screen_info = {
44 	.orig_video_lines	= 30,
45 	.orig_video_cols	= 80,
46 	.orig_video_mode	= 0,
47 	.orig_video_ega_bx	= 0,
48 	.orig_video_isVGA	= 1,
49 	.orig_video_points	= 8
50 };
51 #endif
52 
53 unsigned long va_pa_offset;
54 EXPORT_SYMBOL(va_pa_offset);
55 unsigned long pfn_base;
56 EXPORT_SYMBOL(pfn_base);
57 
58 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
59 EXPORT_SYMBOL(empty_zero_page);
60 
61 /* The lucky hart to first increment this variable will boot the other cores */
62 atomic_t hart_lottery;
63 
64 #ifdef CONFIG_BLK_DEV_INITRD
65 static void __init setup_initrd(void)
66 {
67 	extern char __initramfs_start[];
68 	extern unsigned long __initramfs_size;
69 	unsigned long size;
70 
71 	if (__initramfs_size > 0) {
72 		initrd_start = (unsigned long)(&__initramfs_start);
73 		initrd_end = initrd_start + __initramfs_size;
74 	}
75 
76 	if (initrd_start >= initrd_end) {
77 		printk(KERN_INFO "initrd not found or empty");
78 		goto disable;
79 	}
80 	if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
81 		printk(KERN_ERR "initrd extends beyond end of memory");
82 		goto disable;
83 	}
84 
85 	size =  initrd_end - initrd_start;
86 	memblock_reserve(__pa(initrd_start), size);
87 	initrd_below_start_ok = 1;
88 
89 	printk(KERN_INFO "Initial ramdisk at: 0x%p (%lu bytes)\n",
90 		(void *)(initrd_start), size);
91 	return;
92 disable:
93 	pr_cont(" - disabling initrd\n");
94 	initrd_start = 0;
95 	initrd_end = 0;
96 }
97 #endif /* CONFIG_BLK_DEV_INITRD */
98 
99 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
100 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
101 
102 #ifndef __PAGETABLE_PMD_FOLDED
103 #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
104 pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
105 pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
106 #endif
107 
108 asmlinkage void __init setup_vm(void)
109 {
110 	extern char _start;
111 	uintptr_t i;
112 	uintptr_t pa = (uintptr_t) &_start;
113 	pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
114 
115 	va_pa_offset = PAGE_OFFSET - pa;
116 	pfn_base = PFN_DOWN(pa);
117 
118 	/* Sanity check alignment and size */
119 	BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
120 	BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
121 
122 #ifndef __PAGETABLE_PMD_FOLDED
123 	trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
124 		pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
125 			__pgprot(_PAGE_TABLE));
126 	trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
127 
128 	for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
129 		size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
130 		swapper_pg_dir[o] =
131 			pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
132 				__pgprot(_PAGE_TABLE));
133 	}
134 	for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
135 		swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
136 #else
137 	trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
138 		pfn_pgd(PFN_DOWN(pa), prot);
139 
140 	for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
141 		size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
142 		swapper_pg_dir[o] =
143 			pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
144 	}
145 #endif
146 }
147 
148 void __init parse_dtb(unsigned int hartid, void *dtb)
149 {
150 	early_init_dt_scan(__va(dtb));
151 }
152 
153 static void __init setup_bootmem(void)
154 {
155 	struct memblock_region *reg;
156 	phys_addr_t mem_size = 0;
157 
158 	/* Find the memory region containing the kernel */
159 	for_each_memblock(memory, reg) {
160 		phys_addr_t vmlinux_end = __pa(_end);
161 		phys_addr_t end = reg->base + reg->size;
162 
163 		if (reg->base <= vmlinux_end && vmlinux_end <= end) {
164 			/*
165 			 * Reserve from the start of the region to the end of
166 			 * the kernel
167 			 */
168 			memblock_reserve(reg->base, vmlinux_end - reg->base);
169 			mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
170 		}
171 	}
172 	BUG_ON(mem_size == 0);
173 
174 	set_max_mapnr(PFN_DOWN(mem_size));
175 	max_low_pfn = pfn_base + PFN_DOWN(mem_size);
176 
177 #ifdef CONFIG_BLK_DEV_INITRD
178 	setup_initrd();
179 #endif /* CONFIG_BLK_DEV_INITRD */
180 
181 	early_init_fdt_reserve_self();
182 	early_init_fdt_scan_reserved_mem();
183 	memblock_allow_resize();
184 	memblock_dump_all();
185 
186 	for_each_memblock(memory, reg) {
187 		unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
188 		unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
189 
190 		memblock_set_node(PFN_PHYS(start_pfn),
191 		                  PFN_PHYS(end_pfn - start_pfn),
192 		                  &memblock.memory, 0);
193 	}
194 }
195 
196 void __init setup_arch(char **cmdline_p)
197 {
198 	*cmdline_p = boot_command_line;
199 
200 	parse_early_param();
201 
202 	init_mm.start_code = (unsigned long) _stext;
203 	init_mm.end_code   = (unsigned long) _etext;
204 	init_mm.end_data   = (unsigned long) _edata;
205 	init_mm.brk        = (unsigned long) _end;
206 
207 	setup_bootmem();
208 	paging_init();
209 	unflatten_device_tree();
210 	swiotlb_init(1);
211 
212 #ifdef CONFIG_SMP
213 	setup_smp();
214 #endif
215 
216 #ifdef CONFIG_DUMMY_CONSOLE
217 	conswitchp = &dummy_con;
218 #endif
219 
220 	riscv_fill_hwcap();
221 }
222 
223 static int __init riscv_device_init(void)
224 {
225 	return of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
226 }
227 subsys_initcall_sync(riscv_device_init);
228