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