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