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
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.
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.
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.,
22 #include <linux/init.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>
33 #include <asm/setup.h>
34 #include <asm/sections.h>
35 #include <asm/pgtable.h>
38 #include <asm/tlbflush.h>
39 #include <asm/thread_info.h>
41 #ifdef CONFIG_DUMMY_CONSOLE
42 struct screen_info screen_info
= {
43 .orig_video_lines
= 30,
44 .orig_video_cols
= 80,
46 .orig_video_ega_bx
= 0,
47 .orig_video_isVGA
= 1,
48 .orig_video_points
= 8
52 #ifdef CONFIG_CMDLINE_BOOL
53 static char __initdata builtin_cmdline
[COMMAND_LINE_SIZE
] = CONFIG_CMDLINE
;
54 #endif /* CONFIG_CMDLINE_BOOL */
56 unsigned long va_pa_offset
;
57 EXPORT_SYMBOL(va_pa_offset
);
58 unsigned long pfn_base
;
59 EXPORT_SYMBOL(pfn_base
);
61 unsigned long empty_zero_page
[PAGE_SIZE
/ sizeof(unsigned long)] __page_aligned_bss
;
62 EXPORT_SYMBOL(empty_zero_page
);
64 /* The lucky hart to first increment this variable will boot the other cores */
65 atomic_t hart_lottery
;
67 #ifdef CONFIG_BLK_DEV_INITRD
68 static void __init
setup_initrd(void)
70 extern char __initramfs_start
[];
71 extern unsigned long __initramfs_size
;
74 if (__initramfs_size
> 0) {
75 initrd_start
= (unsigned long)(&__initramfs_start
);
76 initrd_end
= initrd_start
+ __initramfs_size
;
79 if (initrd_start
>= initrd_end
) {
80 printk(KERN_INFO
"initrd not found or empty");
83 if (__pa(initrd_end
) > PFN_PHYS(max_low_pfn
)) {
84 printk(KERN_ERR
"initrd extends beyond end of memory");
88 size
= initrd_end
- initrd_start
;
89 memblock_reserve(__pa(initrd_start
), size
);
90 initrd_below_start_ok
= 1;
92 printk(KERN_INFO
"Initial ramdisk at: 0x%p (%lu bytes)\n",
93 (void *)(initrd_start
), size
);
96 pr_cont(" - disabling initrd\n");
100 #endif /* CONFIG_BLK_DEV_INITRD */
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
);
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
);
111 asmlinkage
void __init
setup_vm(void)
115 uintptr_t pa
= (uintptr_t) &_start
;
116 pgprot_t prot
= __pgprot(pgprot_val(PAGE_KERNEL
) | _PAGE_EXEC
);
118 va_pa_offset
= PAGE_OFFSET
- pa
;
119 pfn_base
= PFN_DOWN(pa
);
121 /* Sanity check alignment and size */
122 BUG_ON((PAGE_OFFSET
% PGDIR_SIZE
) != 0);
123 BUG_ON((pa
% (PAGE_SIZE
* PTRS_PER_PTE
)) != 0);
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
);
131 for (i
= 0; i
< (-PAGE_OFFSET
)/PGDIR_SIZE
; ++i
) {
132 size_t o
= (PAGE_OFFSET
>> PGDIR_SHIFT
) % PTRS_PER_PGD
+ i
;
134 pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd
) + i
,
135 __pgprot(_PAGE_TABLE
));
137 for (i
= 0; i
< ARRAY_SIZE(swapper_pmd
); i
++)
138 swapper_pmd
[i
] = pfn_pmd(PFN_DOWN(pa
+ i
* PMD_SIZE
), prot
);
140 trampoline_pg_dir
[(PAGE_OFFSET
>> PGDIR_SHIFT
) % PTRS_PER_PGD
] =
141 pfn_pgd(PFN_DOWN(pa
), prot
);
143 for (i
= 0; i
< (-PAGE_OFFSET
)/PGDIR_SIZE
; ++i
) {
144 size_t o
= (PAGE_OFFSET
>> PGDIR_SHIFT
) % PTRS_PER_PGD
+ i
;
146 pfn_pgd(PFN_DOWN(pa
+ i
* PGDIR_SIZE
), prot
);
151 void __init
sbi_save(unsigned int hartid
, void *dtb
)
153 early_init_dt_scan(__va(dtb
));
157 * Allow the user to manually add a memory region (in case DTS is broken);
158 * "mem_end=nn[KkMmGg]"
160 static int __init
mem_end_override(char *p
)
162 resource_size_t base
, end
;
166 base
= (uintptr_t) __pa(PAGE_OFFSET
);
167 end
= memparse(p
, &p
) & PMD_MASK
;
170 memblock_add(base
, end
- base
);
173 early_param("mem_end", mem_end_override
);
175 static void __init
setup_bootmem(void)
177 struct memblock_region
*reg
;
178 phys_addr_t mem_size
= 0;
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
;
185 if (reg
->base
<= vmlinux_end
&& vmlinux_end
<= end
) {
187 * Reserve from the start of the region to the end of
190 memblock_reserve(reg
->base
, vmlinux_end
- reg
->base
);
191 mem_size
= min(reg
->size
, (phys_addr_t
)-PAGE_OFFSET
);
194 BUG_ON(mem_size
== 0);
196 set_max_mapnr(PFN_DOWN(mem_size
));
197 max_low_pfn
= pfn_base
+ PFN_DOWN(mem_size
);
199 #ifdef CONFIG_BLK_DEV_INITRD
201 #endif /* CONFIG_BLK_DEV_INITRD */
203 early_init_fdt_reserve_self();
204 early_init_fdt_scan_reserved_mem();
205 memblock_allow_resize();
209 void __init
setup_arch(char **cmdline_p
)
211 #ifdef CONFIG_CMDLINE_BOOL
212 #ifdef CONFIG_CMDLINE_OVERRIDE
213 strlcpy(boot_command_line
, builtin_cmdline
, COMMAND_LINE_SIZE
);
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
);
221 #endif /* CONFIG_CMDLINE_OVERRIDE */
222 #endif /* CONFIG_CMDLINE_BOOL */
223 *cmdline_p
= boot_command_line
;
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
;
234 unflatten_device_tree();
240 #ifdef CONFIG_DUMMY_CONSOLE
241 conswitchp
= &dummy_con
;
247 static int __init
riscv_device_init(void)
249 return of_platform_populate(NULL
, of_default_bus_match_table
, NULL
, NULL
);
251 subsys_initcall_sync(riscv_device_init
);