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