1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Low-level CPU initialisation
4 * Based on arch/arm/kernel/head.S
6 * Copyright (C) 1994-2002 Russell King
7 * Copyright (C) 2003-2012 ARM Ltd.
8 * Authors: Catalin Marinas <catalin.marinas@arm.com>
9 * Will Deacon <will.deacon@arm.com>
12 #include <linux/linkage.h>
13 #include <linux/init.h>
14 #include <linux/pgtable.h>
16 #include <asm/asm_pointer_auth.h>
17 #include <asm/assembler.h>
20 #include <asm/ptrace.h>
21 #include <asm/asm-offsets.h>
22 #include <asm/cache.h>
23 #include <asm/cputype.h>
24 #include <asm/el2_setup.h>
26 #include <asm/image.h>
27 #include <asm/kernel-pgtable.h>
28 #include <asm/kvm_arm.h>
29 #include <asm/memory.h>
30 #include <asm/pgtable-hwdef.h>
34 #include <asm/sysreg.h>
35 #include <asm/stacktrace/frame.h>
36 #include <asm/thread_info.h>
39 #include "efi-header.S"
41 #if (PAGE_OFFSET & 0x1fffff) != 0
42 #error PAGE_OFFSET must be at least 2MB aligned
46 * Kernel startup entry point.
47 * ---------------------------
49 * The requirements are:
50 * MMU = off, D-cache = off, I-cache = on or off,
51 * x0 = physical address to the FDT blob.
53 * Note that the callee-saved registers are used for storing variables
54 * that are useful before the MMU is enabled. The allocations are described
55 * in the entry routines.
59 * DO NOT MODIFY. Image header expected by Linux boot-loaders.
61 efi_signature_nop // special NOP to identity as PE/COFF executable
62 b primary_entry // branch to kernel start, magic
63 .quad 0 // Image load offset from start of RAM, little-endian
64 le64sym _kernel_size_le // Effective size of kernel image, little-endian
65 le64sym _kernel_flags_le // Informative flags, little-endian
69 .ascii ARM64_IMAGE_MAGIC // Magic number
70 .long .Lpe_header_offset // Offset to the PE header.
74 .section ".idmap.text","a"
77 * The following callee saved general purpose registers are used on the
78 * primary lowlevel boot path:
80 * Register Scope Purpose
81 * x19 primary_entry() .. start_kernel() whether we entered with the MMU on
82 * x20 primary_entry() .. __primary_switch() CPU boot mode
83 * x21 primary_entry() .. start_kernel() FDT pointer passed at boot in x0
85 SYM_CODE_START(primary_entry)
89 adrp x1, early_init_stack
92 adrp x0, init_idmap_pg_dir
94 bl __pi_create_init_idmap
97 * If the page tables have been populated with non-cacheable
98 * accesses (MMU disabled), invalidate those tables again to
99 * remove any speculatively loaded cache lines.
103 mov x1, x0 // end of used region
104 adrp x0, init_idmap_pg_dir
105 adr_l x2, dcache_inval_poc
110 * If we entered with the MMU and caches on, clean the ID mapped part
111 * of the primary boot code to the PoC so we can safely execute it with
114 0: adrp x0, __idmap_text_start
115 adr_l x1, __idmap_text_end
116 adr_l x2, dcache_clean_poc
120 bl init_kernel_el // w0=cpu_boot_mode
124 * The following calls CPU setup code, see arch/arm64/mm/proc.S for
126 * On return, the CPU will be ready for the MMU to be turned on and
127 * the TCR will have been set.
129 bl __cpu_setup // initialise processor
131 SYM_CODE_END(primary_entry)
134 SYM_CODE_START_LOCAL(record_mmu_state)
136 cmp x19, #CurrentEL_EL2
141 CPU_LE( tbnz x19, #SCTLR_ELx_EE_SHIFT, 1f )
142 CPU_BE( tbz x19, #SCTLR_ELx_EE_SHIFT, 1f )
143 tst x19, #SCTLR_ELx_C // Z := (C == 0)
144 and x19, x19, #SCTLR_ELx_M // isolate M bit
145 csel x19, xzr, x19, eq // clear x19 if Z
149 * Set the correct endianness early so all memory accesses issued
150 * before init_kernel_el() occur in the correct byte order. Note that
151 * this means the MMU must be disabled, or the active ID map will end
152 * up getting interpreted with the wrong byte order.
154 1: eor x19, x19, #SCTLR_ELx_EE
155 bic x19, x19, #SCTLR_ELx_M
157 pre_disable_mmu_workaround
160 2: pre_disable_mmu_workaround
165 SYM_CODE_END(record_mmu_state)
168 * Preserve the arguments passed by the bootloader in x0 .. x3
170 SYM_CODE_START_LOCAL(preserve_boot_args)
171 mov x21, x0 // x21=FDT
173 adr_l x0, boot_args // record the contents of
174 stp x21, x1, [x0] // x0 .. x3 at kernel entry
175 stp x2, x3, [x0, #16]
177 cbnz x19, 0f // skip cache invalidation if MMU is on
178 dmb sy // needed before dc ivac with
181 add x1, x0, #0x20 // 4 x 8 bytes
182 b dcache_inval_poc // tail call
183 0: str_l x19, mmu_enabled_at_boot, x0
185 SYM_CODE_END(preserve_boot_args)
188 * Initialize CPU registers with task-specific and cpu-specific context.
190 * Create a final frame record at task_pt_regs(current)->stackframe, so
191 * that the unwinder can identify the final frame record of any task by
192 * its location in the task stack. We reserve the entire pt_regs space
193 * for consistency with user tasks and kthreads.
195 .macro init_cpu_task tsk, tmp1, tmp2
198 ldr \tmp1, [\tsk, #TSK_STACK]
199 add sp, \tmp1, #THREAD_SIZE
200 sub sp, sp, #PT_REGS_SIZE
202 stp xzr, xzr, [sp, #S_STACKFRAME]
203 mov \tmp1, #FRAME_META_TYPE_FINAL
204 str \tmp1, [sp, #S_STACKFRAME_TYPE]
205 add x29, sp, #S_STACKFRAME
209 adr_l \tmp1, __per_cpu_offset
210 ldr w\tmp2, [\tsk, #TSK_TI_CPU]
211 ldr \tmp1, [\tmp1, \tmp2, lsl #3]
212 set_this_cpu_offset \tmp1
216 * The following fragment of code is executed with the MMU enabled.
218 * x0 = __pa(KERNEL_START)
220 SYM_FUNC_START_LOCAL(__primary_switched)
222 init_cpu_task x4, x5, x6
224 adr_l x8, vectors // load VBAR_EL1 with virtual
225 msr vbar_el1, x8 // vector table address
228 stp x29, x30, [sp, #-16]!
231 str_l x21, __fdt_pointer, x5 // Save FDT pointer
233 adrp x4, _text // Save the offset between
234 sub x4, x4, x0 // the kernel virtual and
235 str_l x4, kimage_voffset, x5 // physical mappings
238 bl set_cpu_boot_mode_flag
240 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
244 bl finalise_el2 // Prefer VHE if possible
245 ldp x29, x30, [sp], #16
248 SYM_FUNC_END(__primary_switched)
251 * end early head section, begin head code that is also used for
252 * hotplug and needs to have the same protections as the text region
254 .section ".idmap.text","a"
257 * Starting from EL2 or EL1, configure the CPU to execute at the highest
258 * reachable EL supported by the kernel in a chosen default state. If dropping
259 * from EL2 to EL1, configure EL2 before configuring EL1.
261 * Since we cannot always rely on ERET synchronizing writes to sysregs (e.g. if
262 * SCTLR_ELx.EOS is clear), we place an ISB prior to ERET.
264 * Returns either BOOT_CPU_MODE_EL1 or BOOT_CPU_MODE_EL2 in x0 if
265 * booted in EL1 or EL2 respectively, with the top 32 bits containing
266 * potential context flags. These flags are *not* stored in __boot_cpu_mode.
268 * x0: whether we are being called from the primary boot path with the MMU on
270 SYM_FUNC_START(init_kernel_el)
272 cmp x1, #CurrentEL_EL2
275 SYM_INNER_LABEL(init_el1, SYM_L_LOCAL)
276 mov_q x0, INIT_SCTLR_EL1_MMU_OFF
277 pre_disable_mmu_workaround
280 mov_q x0, INIT_PSTATE_EL1
283 mov w0, #BOOT_CPU_MODE_EL1
286 SYM_INNER_LABEL(init_el2, SYM_L_LOCAL)
289 // clean all HYP code to the PoC if we booted at EL2 with the MMU on
291 adrp x0, __hyp_idmap_text_start
292 adr_l x1, __hyp_text_end
293 adr_l x2, dcache_clean_poc
296 mov_q x0, INIT_SCTLR_EL2_MMU_OFF
297 pre_disable_mmu_workaround
301 mov_q x0, HCR_HOST_NVHE_FLAGS
304 * Compliant CPUs advertise their VHE-onlyness with
305 * ID_AA64MMFR4_EL1.E2H0 < 0. HCR_EL2.E2H can be
306 * RES1 in that case. Publish the E2H bit early so that
307 * it can be picked up by the init_el2_state macro.
309 * Fruity CPUs seem to have HCR_EL2.E2H set to RAO/WI, but
310 * don't advertise it (they predate this relaxation).
312 mrs_s x1, SYS_ID_AA64MMFR4_EL1
313 tbz x1, #(ID_AA64MMFR4_EL1_E2H0_SHIFT + ID_AA64MMFR4_EL1_E2H0_WIDTH - 1), 1f
322 /* Hypervisor stub */
323 adr_l x0, __hyp_stub_vectors
327 mov_q x1, INIT_SCTLR_EL1_MMU_OFF
333 /* Set a sane SCTLR_EL1, the VHE way */
334 msr_s SYS_SCTLR_EL12, x1
335 mov x2, #BOOT_CPU_FLAG_E2H
342 __init_el2_nvhe_prepare_eret
344 mov w0, #BOOT_CPU_MODE_EL2
347 SYM_FUNC_END(init_kernel_el)
350 * This provides a "holding pen" for platforms to hold all secondary
351 * cores are held until we're ready for them to initialise.
353 SYM_FUNC_START(secondary_holding_pen)
355 bl init_kernel_el // w0=cpu_boot_mode
357 mov_q x1, MPIDR_HWID_BITMASK
359 adr_l x3, secondary_holding_pen_release
362 b.eq secondary_startup
365 SYM_FUNC_END(secondary_holding_pen)
368 * Secondary entry point that jumps straight into the kernel. Only to
369 * be used where CPUs are brought online dynamically by the kernel.
371 SYM_FUNC_START(secondary_entry)
373 bl init_kernel_el // w0=cpu_boot_mode
375 SYM_FUNC_END(secondary_entry)
377 SYM_FUNC_START_LOCAL(secondary_startup)
379 * Common entry point for secondary CPUs.
381 mov x20, x0 // preserve boot mode
383 #ifdef CONFIG_ARM64_VA_BITS_52
384 alternative_if ARM64_HAS_VA52
385 bl __cpu_secondary_check52bitva
386 alternative_else_nop_endif
389 bl __cpu_setup // initialise processor
390 adrp x1, swapper_pg_dir
391 adrp x2, idmap_pg_dir
393 ldr x8, =__secondary_switched
395 SYM_FUNC_END(secondary_startup)
398 SYM_FUNC_START_LOCAL(__secondary_switched)
400 bl set_cpu_boot_mode_flag
405 str_l xzr, __early_cpu_boot_status, x3
410 adr_l x0, secondary_data
411 ldr x2, [x0, #CPU_BOOT_TASK]
412 cbz x2, __secondary_too_slow
414 init_cpu_task x2, x1, x3
416 #ifdef CONFIG_ARM64_PTR_AUTH
417 ptrauth_keys_init_cpu x2, x3, x4, x5
420 bl secondary_start_kernel
422 SYM_FUNC_END(__secondary_switched)
424 SYM_FUNC_START_LOCAL(__secondary_too_slow)
427 b __secondary_too_slow
428 SYM_FUNC_END(__secondary_too_slow)
431 * Sets the __boot_cpu_mode flag depending on the CPU boot mode passed
432 * in w0. See arch/arm64/include/asm/virt.h for more info.
434 SYM_FUNC_START_LOCAL(set_cpu_boot_mode_flag)
435 adr_l x1, __boot_cpu_mode
436 cmp w0, #BOOT_CPU_MODE_EL2
439 1: str w0, [x1] // Save CPU boot mode
441 SYM_FUNC_END(set_cpu_boot_mode_flag)
444 * The booting CPU updates the failed status @__early_cpu_boot_status,
445 * with MMU turned off.
447 * update_early_cpu_boot_status tmp, status
448 * - Corrupts tmp1, tmp2
449 * - Writes 'status' to __early_cpu_boot_status and makes sure
450 * it is committed to memory.
453 .macro update_early_cpu_boot_status status, tmp1, tmp2
455 adr_l \tmp1, __early_cpu_boot_status
458 dc ivac, \tmp1 // Invalidate potentially stale cache line
464 * x0 = SCTLR_EL1 value for turning on the MMU.
465 * x1 = TTBR1_EL1 value
466 * x2 = ID map root table address
468 * Returns to the caller via x30/lr. This requires the caller to be covered
469 * by the .idmap.text section.
471 * Checks if the selected granule size is supported by the CPU.
472 * If it isn't, park the CPU
474 .section ".idmap.text","a"
475 SYM_FUNC_START(__enable_mmu)
476 mrs x3, ID_AA64MMFR0_EL1
477 ubfx x3, x3, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4
478 cmp x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN
479 b.lt __no_granule_support
480 cmp x3, #ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX
481 b.gt __no_granule_support
483 msr ttbr0_el1, x2 // load TTBR0
484 load_ttbr1 x1, x1, x3
489 SYM_FUNC_END(__enable_mmu)
491 #ifdef CONFIG_ARM64_VA_BITS_52
492 SYM_FUNC_START(__cpu_secondary_check52bitva)
493 #ifndef CONFIG_ARM64_LPA2
494 mrs_s x0, SYS_ID_AA64MMFR2_EL1
495 and x0, x0, ID_AA64MMFR2_EL1_VARange_MASK
498 mrs x0, id_aa64mmfr0_el1
499 sbfx x0, x0, #ID_AA64MMFR0_EL1_TGRAN_SHIFT, 4
500 cmp x0, #ID_AA64MMFR0_EL1_TGRAN_LPA2
504 update_early_cpu_boot_status \
505 CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_52_BIT_VA, x0, x1
511 SYM_FUNC_END(__cpu_secondary_check52bitva)
514 SYM_FUNC_START_LOCAL(__no_granule_support)
515 /* Indicate that this CPU can't boot and is stuck in the kernel */
516 update_early_cpu_boot_status \
517 CPU_STUCK_IN_KERNEL | CPU_STUCK_REASON_NO_GRAN, x1, x2
522 SYM_FUNC_END(__no_granule_support)
524 SYM_FUNC_START_LOCAL(__primary_switch)
525 adrp x1, reserved_pg_dir
526 adrp x2, init_idmap_pg_dir
529 adrp x1, early_init_stack
532 mov x0, x20 // pass the full boot status
533 mov x1, x21 // pass the FDT
534 bl __pi_early_map_kernel // Map and relocate the kernel
536 ldr x8, =__primary_switched
537 adrp x0, KERNEL_START // __pa(KERNEL_START)
539 SYM_FUNC_END(__primary_switch)