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