1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd. 4 * Chen Liqin <liqin.chen@sunplusct.com> 5 * Lennox Wu <lennox.wu@sunplusct.com> 6 * Copyright (C) 2012 Regents of the University of California 7 * Copyright (C) 2020 FORTH-ICS/CARV 8 * Nick Kossifidis <mick@ics.forth.gr> 9 */ 10 11 #include <linux/acpi.h> 12 #include <linux/cpu.h> 13 #include <linux/init.h> 14 #include <linux/mm.h> 15 #include <linux/memblock.h> 16 #include <linux/sched.h> 17 #include <linux/console.h> 18 #include <linux/of_fdt.h> 19 #include <linux/sched/task.h> 20 #include <linux/smp.h> 21 #include <linux/efi.h> 22 #include <linux/crash_dump.h> 23 #include <linux/panic_notifier.h> 24 25 #include <asm/acpi.h> 26 #include <asm/alternative.h> 27 #include <asm/cacheflush.h> 28 #include <asm/cpufeature.h> 29 #include <asm/early_ioremap.h> 30 #include <asm/pgtable.h> 31 #include <asm/setup.h> 32 #include <asm/set_memory.h> 33 #include <asm/sections.h> 34 #include <asm/sbi.h> 35 #include <asm/tlbflush.h> 36 #include <asm/thread_info.h> 37 #include <asm/kasan.h> 38 #include <asm/efi.h> 39 40 #include "head.h" 41 42 /* 43 * The lucky hart to first increment this variable will boot the other cores. 44 * This is used before the kernel initializes the BSS so it can't be in the 45 * BSS. 46 */ 47 atomic_t hart_lottery __section(".sdata") 48 #ifdef CONFIG_XIP_KERNEL 49 = ATOMIC_INIT(0xC001BEEF) 50 #endif 51 ; 52 unsigned long boot_cpu_hartid; 53 54 /* 55 * Place kernel memory regions on the resource tree so that 56 * kexec-tools can retrieve them from /proc/iomem. While there 57 * also add "System RAM" regions for compatibility with other 58 * archs, and the rest of the known regions for completeness. 59 */ 60 static struct resource kimage_res = { .name = "Kernel image", }; 61 static struct resource code_res = { .name = "Kernel code", }; 62 static struct resource data_res = { .name = "Kernel data", }; 63 static struct resource rodata_res = { .name = "Kernel rodata", }; 64 static struct resource bss_res = { .name = "Kernel bss", }; 65 #ifdef CONFIG_CRASH_DUMP 66 static struct resource elfcorehdr_res = { .name = "ELF Core hdr", }; 67 #endif 68 69 static int num_standard_resources; 70 static struct resource *standard_resources; 71 72 static int __init add_resource(struct resource *parent, 73 struct resource *res) 74 { 75 int ret = 0; 76 77 ret = insert_resource(parent, res); 78 if (ret < 0) { 79 pr_err("Failed to add a %s resource at %llx\n", 80 res->name, (unsigned long long) res->start); 81 return ret; 82 } 83 84 return 1; 85 } 86 87 static int __init add_kernel_resources(void) 88 { 89 int ret = 0; 90 91 /* 92 * The memory region of the kernel image is continuous and 93 * was reserved on setup_bootmem, register it here as a 94 * resource, with the various segments of the image as 95 * child nodes. 96 */ 97 98 code_res.start = __pa_symbol(_text); 99 code_res.end = __pa_symbol(_etext) - 1; 100 code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 101 102 rodata_res.start = __pa_symbol(__start_rodata); 103 rodata_res.end = __pa_symbol(__end_rodata) - 1; 104 rodata_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 105 106 data_res.start = __pa_symbol(_data); 107 data_res.end = __pa_symbol(_edata) - 1; 108 data_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 109 110 bss_res.start = __pa_symbol(__bss_start); 111 bss_res.end = __pa_symbol(__bss_stop) - 1; 112 bss_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 113 114 kimage_res.start = code_res.start; 115 kimage_res.end = bss_res.end; 116 kimage_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 117 118 ret = add_resource(&iomem_resource, &kimage_res); 119 if (ret < 0) 120 return ret; 121 122 ret = add_resource(&kimage_res, &code_res); 123 if (ret < 0) 124 return ret; 125 126 ret = add_resource(&kimage_res, &rodata_res); 127 if (ret < 0) 128 return ret; 129 130 ret = add_resource(&kimage_res, &data_res); 131 if (ret < 0) 132 return ret; 133 134 ret = add_resource(&kimage_res, &bss_res); 135 136 return ret; 137 } 138 139 static void __init init_resources(void) 140 { 141 struct memblock_region *region = NULL; 142 struct resource *res = NULL; 143 struct resource *mem_res = NULL; 144 size_t mem_res_sz = 0; 145 int num_resources = 0, res_idx = 0, non_resv_res = 0; 146 int ret = 0; 147 148 /* + 1 as memblock_alloc() might increase memblock.reserved.cnt */ 149 num_resources = memblock.memory.cnt + memblock.reserved.cnt + 1; 150 res_idx = num_resources - 1; 151 152 mem_res_sz = num_resources * sizeof(*mem_res); 153 mem_res = memblock_alloc_or_panic(mem_res_sz, SMP_CACHE_BYTES); 154 155 /* 156 * Start by adding the reserved regions, if they overlap 157 * with /memory regions, insert_resource later on will take 158 * care of it. 159 */ 160 ret = add_kernel_resources(); 161 if (ret < 0) 162 goto error; 163 164 #ifdef CONFIG_CRASH_DUMP 165 if (elfcorehdr_size > 0) { 166 elfcorehdr_res.start = elfcorehdr_addr; 167 elfcorehdr_res.end = elfcorehdr_addr + elfcorehdr_size - 1; 168 elfcorehdr_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 169 add_resource(&iomem_resource, &elfcorehdr_res); 170 } 171 #endif 172 173 for_each_reserved_mem_region(region) { 174 res = &mem_res[res_idx--]; 175 176 res->name = "Reserved"; 177 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE; 178 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region)); 179 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1; 180 181 /* 182 * Ignore any other reserved regions within 183 * system memory. 184 */ 185 if (memblock_is_memory(res->start)) { 186 /* Re-use this pre-allocated resource */ 187 res_idx++; 188 continue; 189 } 190 191 ret = add_resource(&iomem_resource, res); 192 if (ret < 0) 193 goto error; 194 } 195 196 /* Add /memory regions to the resource tree */ 197 for_each_mem_region(region) { 198 res = &mem_res[res_idx--]; 199 non_resv_res++; 200 201 if (unlikely(memblock_is_nomap(region))) { 202 res->name = "Reserved"; 203 res->flags = IORESOURCE_MEM | IORESOURCE_EXCLUSIVE; 204 } else { 205 res->name = "System RAM"; 206 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 207 } 208 209 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region)); 210 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1; 211 212 ret = add_resource(&iomem_resource, res); 213 if (ret < 0) 214 goto error; 215 } 216 217 num_standard_resources = non_resv_res; 218 standard_resources = &mem_res[res_idx + 1]; 219 220 /* Clean-up any unused pre-allocated resources */ 221 if (res_idx >= 0) 222 memblock_free(mem_res, (res_idx + 1) * sizeof(*mem_res)); 223 return; 224 225 error: 226 /* Better an empty resource tree than an inconsistent one */ 227 release_child_resources(&iomem_resource); 228 memblock_free(mem_res, mem_res_sz); 229 } 230 231 static int __init reserve_memblock_reserved_regions(void) 232 { 233 u64 i, j; 234 235 for (i = 0; i < num_standard_resources; i++) { 236 struct resource *mem = &standard_resources[i]; 237 phys_addr_t r_start, r_end, mem_size = resource_size(mem); 238 239 if (!memblock_is_region_reserved(mem->start, mem_size)) 240 continue; 241 242 for_each_reserved_mem_range(j, &r_start, &r_end) { 243 resource_size_t start, end; 244 245 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start); 246 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end); 247 248 if (start > mem->end || end < mem->start) 249 continue; 250 251 reserve_region_with_split(mem, start, end, "Reserved"); 252 } 253 } 254 255 return 0; 256 } 257 arch_initcall(reserve_memblock_reserved_regions); 258 259 static void __init parse_dtb(void) 260 { 261 /* Early scan of device tree from init memory */ 262 if (early_init_dt_scan(dtb_early_va, dtb_early_pa)) { 263 const char *name = of_flat_dt_get_machine_name(); 264 265 if (name) { 266 pr_info("Machine model: %s\n", name); 267 dump_stack_set_arch_desc("%s (DT)", name); 268 } 269 } else { 270 pr_err("No DTB passed to the kernel\n"); 271 } 272 } 273 274 #if defined(CONFIG_RISCV_COMBO_SPINLOCKS) 275 DEFINE_STATIC_KEY_TRUE(qspinlock_key); 276 EXPORT_SYMBOL(qspinlock_key); 277 #endif 278 279 static void __init riscv_spinlock_init(void) 280 { 281 char *using_ext = NULL; 282 283 if (IS_ENABLED(CONFIG_RISCV_TICKET_SPINLOCKS)) { 284 pr_info("Ticket spinlock: enabled\n"); 285 return; 286 } 287 288 if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) && 289 IS_ENABLED(CONFIG_RISCV_ISA_ZACAS) && 290 riscv_isa_extension_available(NULL, ZABHA) && 291 riscv_isa_extension_available(NULL, ZACAS)) { 292 using_ext = "using Zabha"; 293 } else if (riscv_isa_extension_available(NULL, ZICCRSE)) { 294 using_ext = "using Ziccrse"; 295 } 296 #if defined(CONFIG_RISCV_COMBO_SPINLOCKS) 297 else { 298 static_branch_disable(&qspinlock_key); 299 pr_info("Ticket spinlock: enabled\n"); 300 return; 301 } 302 #endif 303 304 if (!using_ext) 305 pr_err("Queued spinlock without Zabha or Ziccrse"); 306 else 307 pr_info("Queued spinlock %s: enabled\n", using_ext); 308 } 309 310 extern void __init init_rt_signal_env(void); 311 312 void __init setup_arch(char **cmdline_p) 313 { 314 parse_dtb(); 315 setup_initial_init_mm(_stext, _etext, _edata, _end); 316 317 *cmdline_p = boot_command_line; 318 319 early_ioremap_setup(); 320 sbi_init(); 321 jump_label_init(); 322 parse_early_param(); 323 324 efi_init(); 325 paging_init(); 326 327 /* Parse the ACPI tables for possible boot-time configuration */ 328 acpi_boot_table_init(); 329 330 #if IS_ENABLED(CONFIG_BUILTIN_DTB) 331 unflatten_and_copy_device_tree(); 332 #else 333 unflatten_device_tree(); 334 #endif 335 misc_mem_init(); 336 337 init_resources(); 338 339 #ifdef CONFIG_KASAN 340 kasan_init(); 341 #endif 342 343 #ifdef CONFIG_SMP 344 setup_smp(); 345 #endif 346 347 if (!acpi_disabled) { 348 acpi_init_rintc_map(); 349 acpi_map_cpus_to_nodes(); 350 } 351 352 riscv_init_cbo_blocksizes(); 353 riscv_fill_hwcap(); 354 apply_boot_alternatives(); 355 init_rt_signal_env(); 356 357 if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) && 358 riscv_isa_extension_available(NULL, ZICBOM)) 359 riscv_noncoherent_supported(); 360 riscv_set_dma_cache_alignment(); 361 362 riscv_user_isa_enable(); 363 riscv_spinlock_init(); 364 } 365 366 bool arch_cpu_is_hotpluggable(int cpu) 367 { 368 return cpu_has_hotplug(cpu); 369 } 370 371 void free_initmem(void) 372 { 373 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) { 374 set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end), set_memory_rw_nx); 375 if (IS_ENABLED(CONFIG_64BIT)) 376 set_kernel_memory(__init_begin, __init_end, set_memory_nx); 377 } 378 379 free_initmem_default(POISON_FREE_INITMEM); 380 } 381 382 static int dump_kernel_offset(struct notifier_block *self, 383 unsigned long v, void *p) 384 { 385 pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n", 386 kernel_map.virt_offset, 387 KERNEL_LINK_ADDR); 388 389 return 0; 390 } 391 392 static struct notifier_block kernel_offset_notifier = { 393 .notifier_call = dump_kernel_offset 394 }; 395 396 static int __init register_kernel_offset_dumper(void) 397 { 398 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) 399 atomic_notifier_chain_register(&panic_notifier_list, 400 &kernel_offset_notifier); 401 402 return 0; 403 } 404 device_initcall(register_kernel_offset_dumper); 405