| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) 2009 Sunplus Core Technology Co., Ltd. |
|---|
| 3 | 4 | * Chen Liqin <liqin.chen@sunplusct.com> |
|---|
| 4 | 5 | * Lennox Wu <lennox.wu@sunplusct.com> |
|---|
| 5 | 6 | * 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 | 7 | */ |
|---|
| 21 | 8 | |
|---|
| 22 | | -#include <linux/bootmem.h> |
|---|
| 23 | 9 | #include <linux/init.h> |
|---|
| 24 | 10 | #include <linux/mm.h> |
|---|
| 25 | 11 | #include <linux/memblock.h> |
|---|
| 26 | 12 | #include <linux/sched.h> |
|---|
| 27 | | -#include <linux/initrd.h> |
|---|
| 28 | 13 | #include <linux/console.h> |
|---|
| 29 | 14 | #include <linux/screen_info.h> |
|---|
| 30 | 15 | #include <linux/of_fdt.h> |
|---|
| 31 | 16 | #include <linux/of_platform.h> |
|---|
| 32 | 17 | #include <linux/sched/task.h> |
|---|
| 33 | 18 | #include <linux/swiotlb.h> |
|---|
| 19 | +#include <linux/smp.h> |
|---|
| 20 | +#include <linux/efi.h> |
|---|
| 34 | 21 | |
|---|
| 22 | +#include <asm/cpu_ops.h> |
|---|
| 23 | +#include <asm/early_ioremap.h> |
|---|
| 35 | 24 | #include <asm/setup.h> |
|---|
| 36 | 25 | #include <asm/sections.h> |
|---|
| 37 | | -#include <asm/pgtable.h> |
|---|
| 38 | | -#include <asm/smp.h> |
|---|
| 39 | 26 | #include <asm/sbi.h> |
|---|
| 40 | 27 | #include <asm/tlbflush.h> |
|---|
| 41 | 28 | #include <asm/thread_info.h> |
|---|
| 29 | +#include <asm/kasan.h> |
|---|
| 30 | +#include <asm/efi.h> |
|---|
| 42 | 31 | |
|---|
| 43 | | -#ifdef CONFIG_EARLY_PRINTK |
|---|
| 44 | | -static void sbi_console_write(struct console *co, const char *buf, |
|---|
| 45 | | - unsigned int n) |
|---|
| 46 | | -{ |
|---|
| 47 | | - int i; |
|---|
| 32 | +#include "head.h" |
|---|
| 48 | 33 | |
|---|
| 49 | | - for (i = 0; i < n; ++i) { |
|---|
| 50 | | - if (buf[i] == '\n') |
|---|
| 51 | | - sbi_console_putchar('\r'); |
|---|
| 52 | | - sbi_console_putchar(buf[i]); |
|---|
| 53 | | - } |
|---|
| 54 | | -} |
|---|
| 55 | | - |
|---|
| 56 | | -struct console riscv_sbi_early_console_dev __initdata = { |
|---|
| 57 | | - .name = "early", |
|---|
| 58 | | - .write = sbi_console_write, |
|---|
| 59 | | - .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME, |
|---|
| 60 | | - .index = -1 |
|---|
| 61 | | -}; |
|---|
| 62 | | -#endif |
|---|
| 63 | | - |
|---|
| 64 | | -#ifdef CONFIG_DUMMY_CONSOLE |
|---|
| 65 | | -struct screen_info screen_info = { |
|---|
| 34 | +#if defined(CONFIG_DUMMY_CONSOLE) || defined(CONFIG_EFI) |
|---|
| 35 | +struct screen_info screen_info __section(".data") = { |
|---|
| 66 | 36 | .orig_video_lines = 30, |
|---|
| 67 | 37 | .orig_video_cols = 80, |
|---|
| 68 | 38 | .orig_video_mode = 0, |
|---|
| .. | .. |
|---|
| 72 | 42 | }; |
|---|
| 73 | 43 | #endif |
|---|
| 74 | 44 | |
|---|
| 75 | | -unsigned long va_pa_offset; |
|---|
| 76 | | -EXPORT_SYMBOL(va_pa_offset); |
|---|
| 77 | | -unsigned long pfn_base; |
|---|
| 78 | | -EXPORT_SYMBOL(pfn_base); |
|---|
| 45 | +/* |
|---|
| 46 | + * The lucky hart to first increment this variable will boot the other cores. |
|---|
| 47 | + * This is used before the kernel initializes the BSS so it can't be in the |
|---|
| 48 | + * BSS. |
|---|
| 49 | + */ |
|---|
| 50 | +atomic_t hart_lottery __section(".sdata"); |
|---|
| 51 | +unsigned long boot_cpu_hartid; |
|---|
| 52 | +static DEFINE_PER_CPU(struct cpu, cpu_devices); |
|---|
| 79 | 53 | |
|---|
| 80 | | -unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; |
|---|
| 81 | | -EXPORT_SYMBOL(empty_zero_page); |
|---|
| 82 | | - |
|---|
| 83 | | -/* The lucky hart to first increment this variable will boot the other cores */ |
|---|
| 84 | | -atomic_t hart_lottery; |
|---|
| 85 | | - |
|---|
| 86 | | -#ifdef CONFIG_BLK_DEV_INITRD |
|---|
| 87 | | -static void __init setup_initrd(void) |
|---|
| 54 | +static void __init parse_dtb(void) |
|---|
| 88 | 55 | { |
|---|
| 89 | | - unsigned long size; |
|---|
| 56 | + /* Early scan of device tree from init memory */ |
|---|
| 57 | + if (early_init_dt_scan(dtb_early_va)) { |
|---|
| 58 | + const char *name = of_flat_dt_get_machine_name(); |
|---|
| 90 | 59 | |
|---|
| 91 | | - if (initrd_start >= initrd_end) { |
|---|
| 92 | | - printk(KERN_INFO "initrd not found or empty"); |
|---|
| 93 | | - goto disable; |
|---|
| 94 | | - } |
|---|
| 95 | | - if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) { |
|---|
| 96 | | - printk(KERN_ERR "initrd extends beyond end of memory"); |
|---|
| 97 | | - goto disable; |
|---|
| 98 | | - } |
|---|
| 99 | | - |
|---|
| 100 | | - size = initrd_end - initrd_start; |
|---|
| 101 | | - memblock_reserve(__pa(initrd_start), size); |
|---|
| 102 | | - initrd_below_start_ok = 1; |
|---|
| 103 | | - |
|---|
| 104 | | - printk(KERN_INFO "Initial ramdisk at: 0x%p (%lu bytes)\n", |
|---|
| 105 | | - (void *)(initrd_start), size); |
|---|
| 106 | | - return; |
|---|
| 107 | | -disable: |
|---|
| 108 | | - pr_cont(" - disabling initrd\n"); |
|---|
| 109 | | - initrd_start = 0; |
|---|
| 110 | | - initrd_end = 0; |
|---|
| 111 | | -} |
|---|
| 112 | | -#endif /* CONFIG_BLK_DEV_INITRD */ |
|---|
| 113 | | - |
|---|
| 114 | | -pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; |
|---|
| 115 | | -pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); |
|---|
| 116 | | - |
|---|
| 117 | | -#ifndef __PAGETABLE_PMD_FOLDED |
|---|
| 118 | | -#define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT) |
|---|
| 119 | | -pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss; |
|---|
| 120 | | -pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); |
|---|
| 121 | | -#endif |
|---|
| 122 | | - |
|---|
| 123 | | -asmlinkage void __init setup_vm(void) |
|---|
| 124 | | -{ |
|---|
| 125 | | - extern char _start; |
|---|
| 126 | | - uintptr_t i; |
|---|
| 127 | | - uintptr_t pa = (uintptr_t) &_start; |
|---|
| 128 | | - pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC); |
|---|
| 129 | | - |
|---|
| 130 | | - va_pa_offset = PAGE_OFFSET - pa; |
|---|
| 131 | | - pfn_base = PFN_DOWN(pa); |
|---|
| 132 | | - |
|---|
| 133 | | - /* Sanity check alignment and size */ |
|---|
| 134 | | - BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); |
|---|
| 135 | | - BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0); |
|---|
| 136 | | - |
|---|
| 137 | | -#ifndef __PAGETABLE_PMD_FOLDED |
|---|
| 138 | | - trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] = |
|---|
| 139 | | - pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd), |
|---|
| 140 | | - __pgprot(_PAGE_TABLE)); |
|---|
| 141 | | - trampoline_pmd[0] = pfn_pmd(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((uintptr_t)swapper_pmd) + i, |
|---|
| 147 | | - __pgprot(_PAGE_TABLE)); |
|---|
| 148 | | - } |
|---|
| 149 | | - for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++) |
|---|
| 150 | | - swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot); |
|---|
| 151 | | -#else |
|---|
| 152 | | - trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] = |
|---|
| 153 | | - pfn_pgd(PFN_DOWN(pa), prot); |
|---|
| 154 | | - |
|---|
| 155 | | - for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) { |
|---|
| 156 | | - size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i; |
|---|
| 157 | | - swapper_pg_dir[o] = |
|---|
| 158 | | - pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot); |
|---|
| 159 | | - } |
|---|
| 160 | | -#endif |
|---|
| 161 | | -} |
|---|
| 162 | | - |
|---|
| 163 | | -void __init parse_dtb(unsigned int hartid, void *dtb) |
|---|
| 164 | | -{ |
|---|
| 165 | | - early_init_dt_scan(__va(dtb)); |
|---|
| 166 | | -} |
|---|
| 167 | | - |
|---|
| 168 | | -static void __init setup_bootmem(void) |
|---|
| 169 | | -{ |
|---|
| 170 | | - struct memblock_region *reg; |
|---|
| 171 | | - phys_addr_t mem_size = 0; |
|---|
| 172 | | - |
|---|
| 173 | | - /* Find the memory region containing the kernel */ |
|---|
| 174 | | - for_each_memblock(memory, reg) { |
|---|
| 175 | | - phys_addr_t vmlinux_end = __pa(_end); |
|---|
| 176 | | - phys_addr_t end = reg->base + reg->size; |
|---|
| 177 | | - |
|---|
| 178 | | - if (reg->base <= vmlinux_end && vmlinux_end <= end) { |
|---|
| 179 | | - /* |
|---|
| 180 | | - * Reserve from the start of the region to the end of |
|---|
| 181 | | - * the kernel |
|---|
| 182 | | - */ |
|---|
| 183 | | - memblock_reserve(reg->base, vmlinux_end - reg->base); |
|---|
| 184 | | - mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET); |
|---|
| 60 | + if (name) { |
|---|
| 61 | + pr_info("Machine model: %s\n", name); |
|---|
| 62 | + dump_stack_set_arch_desc("%s (DT)", name); |
|---|
| 185 | 63 | } |
|---|
| 64 | + } else { |
|---|
| 65 | + pr_err("No DTB passed to the kernel\n"); |
|---|
| 186 | 66 | } |
|---|
| 187 | | - BUG_ON(mem_size == 0); |
|---|
| 188 | 67 | |
|---|
| 189 | | - set_max_mapnr(PFN_DOWN(mem_size)); |
|---|
| 190 | | - max_low_pfn = PFN_DOWN(memblock_end_of_DRAM()); |
|---|
| 191 | | - max_pfn = max_low_pfn; |
|---|
| 192 | | - |
|---|
| 193 | | -#ifdef CONFIG_BLK_DEV_INITRD |
|---|
| 194 | | - setup_initrd(); |
|---|
| 195 | | -#endif /* CONFIG_BLK_DEV_INITRD */ |
|---|
| 196 | | - |
|---|
| 197 | | - early_init_fdt_reserve_self(); |
|---|
| 198 | | - early_init_fdt_scan_reserved_mem(); |
|---|
| 199 | | - memblock_allow_resize(); |
|---|
| 200 | | - memblock_dump_all(); |
|---|
| 201 | | - |
|---|
| 202 | | - for_each_memblock(memory, reg) { |
|---|
| 203 | | - unsigned long start_pfn = memblock_region_memory_base_pfn(reg); |
|---|
| 204 | | - unsigned long end_pfn = memblock_region_memory_end_pfn(reg); |
|---|
| 205 | | - |
|---|
| 206 | | - memblock_set_node(PFN_PHYS(start_pfn), |
|---|
| 207 | | - PFN_PHYS(end_pfn - start_pfn), |
|---|
| 208 | | - &memblock.memory, 0); |
|---|
| 209 | | - } |
|---|
| 68 | +#ifdef CONFIG_CMDLINE_FORCE |
|---|
| 69 | + strlcpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE); |
|---|
| 70 | + pr_info("Forcing kernel command line to: %s\n", boot_command_line); |
|---|
| 71 | +#endif |
|---|
| 210 | 72 | } |
|---|
| 211 | 73 | |
|---|
| 212 | 74 | void __init setup_arch(char **cmdline_p) |
|---|
| 213 | 75 | { |
|---|
| 214 | | -#if defined(CONFIG_EARLY_PRINTK) |
|---|
| 215 | | - if (likely(early_console == NULL)) { |
|---|
| 216 | | - early_console = &riscv_sbi_early_console_dev; |
|---|
| 217 | | - register_console(early_console); |
|---|
| 218 | | - } |
|---|
| 219 | | -#endif |
|---|
| 220 | | - *cmdline_p = boot_command_line; |
|---|
| 221 | | - |
|---|
| 222 | | - parse_early_param(); |
|---|
| 223 | | - |
|---|
| 76 | + parse_dtb(); |
|---|
| 224 | 77 | init_mm.start_code = (unsigned long) _stext; |
|---|
| 225 | 78 | init_mm.end_code = (unsigned long) _etext; |
|---|
| 226 | 79 | init_mm.end_data = (unsigned long) _edata; |
|---|
| 227 | 80 | init_mm.brk = (unsigned long) _end; |
|---|
| 228 | 81 | |
|---|
| 82 | + *cmdline_p = boot_command_line; |
|---|
| 83 | + |
|---|
| 84 | + early_ioremap_setup(); |
|---|
| 85 | + jump_label_init(); |
|---|
| 86 | + parse_early_param(); |
|---|
| 87 | + |
|---|
| 88 | + efi_init(); |
|---|
| 229 | 89 | setup_bootmem(); |
|---|
| 230 | 90 | paging_init(); |
|---|
| 231 | | - unflatten_device_tree(); |
|---|
| 91 | +#if IS_ENABLED(CONFIG_BUILTIN_DTB) |
|---|
| 92 | + unflatten_and_copy_device_tree(); |
|---|
| 93 | +#else |
|---|
| 94 | + if (early_init_dt_verify(__va(dtb_early_pa))) |
|---|
| 95 | + unflatten_device_tree(); |
|---|
| 96 | + else |
|---|
| 97 | + pr_err("No DTB found in kernel mappings\n"); |
|---|
| 98 | +#endif |
|---|
| 99 | + early_init_fdt_scan_reserved_mem(); |
|---|
| 100 | + misc_mem_init(); |
|---|
| 101 | + |
|---|
| 102 | +#ifdef CONFIG_SWIOTLB |
|---|
| 232 | 103 | swiotlb_init(1); |
|---|
| 104 | +#endif |
|---|
| 105 | + |
|---|
| 106 | +#ifdef CONFIG_KASAN |
|---|
| 107 | + kasan_init(); |
|---|
| 108 | +#endif |
|---|
| 109 | + |
|---|
| 110 | +#if IS_ENABLED(CONFIG_RISCV_SBI) |
|---|
| 111 | + sbi_init(); |
|---|
| 112 | +#endif |
|---|
| 233 | 113 | |
|---|
| 234 | 114 | #ifdef CONFIG_SMP |
|---|
| 235 | 115 | setup_smp(); |
|---|
| 236 | 116 | #endif |
|---|
| 237 | 117 | |
|---|
| 238 | | -#ifdef CONFIG_DUMMY_CONSOLE |
|---|
| 239 | | - conswitchp = &dummy_con; |
|---|
| 240 | | -#endif |
|---|
| 241 | | - |
|---|
| 242 | 118 | riscv_fill_hwcap(); |
|---|
| 243 | 119 | } |
|---|
| 244 | 120 | |
|---|
| 121 | +static int __init topology_init(void) |
|---|
| 122 | +{ |
|---|
| 123 | + int i; |
|---|
| 124 | + |
|---|
| 125 | + for_each_possible_cpu(i) { |
|---|
| 126 | + struct cpu *cpu = &per_cpu(cpu_devices, i); |
|---|
| 127 | + |
|---|
| 128 | + cpu->hotpluggable = cpu_has_hotplug(i); |
|---|
| 129 | + register_cpu(cpu, i); |
|---|
| 130 | + } |
|---|
| 131 | + |
|---|
| 132 | + return 0; |
|---|
| 133 | +} |
|---|
| 134 | +subsys_initcall(topology_init); |
|---|