/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Copyright (c) 2012 Linaro Limited.3*/45#include <linux/init.h>6#include <linux/irqchip/arm-gic-v3.h>7#include <linux/linkage.h>8#include <asm/assembler.h>9#include <asm/virt.h>1011.arch armv7-a1213#ifndef ZIMAGE14/*15* For the kernel proper, we need to find out the CPU boot mode long after16* boot, so we need to store it in a writable variable.17*18* This is not in .bss, because we set it sufficiently early that the boot-time19* zeroing of .bss would clobber it.20*/21.data22.align 223ENTRY(__boot_cpu_mode)24.long 025.text2627/*28* Save the primary CPU boot mode. Requires 2 scratch registers.29*/30.macro store_primary_cpu_mode reg1, reg231mrs \reg1, cpsr32and \reg1, \reg1, #MODE_MASK33str_l \reg1, __boot_cpu_mode, \reg234.endm3536/*37* Compare the current mode with the one saved on the primary CPU.38* If they don't match, record that fact. The Z bit indicates39* if there's a match or not.40* Requires 2 additional scratch registers.41*/42.macro compare_cpu_mode_with_primary mode, reg1, reg243adr_l \reg2, __boot_cpu_mode44ldr \reg1, [\reg2]45cmp \mode, \reg1 @ matches primary CPU boot mode?46orrne \reg1, \reg1, #BOOT_CPU_MODE_MISMATCH47strne \reg1, [\reg2] @ record what happened and give up48.endm4950#else /* ZIMAGE */5152.macro store_primary_cpu_mode reg1:req, reg2:req53.endm5455/*56* The zImage loader only runs on one CPU, so we don't bother with mult-CPU57* consistency checking:58*/59.macro compare_cpu_mode_with_primary mode, reg1, reg260cmp \mode, \mode61.endm6263#endif /* ZIMAGE */6465/*66* Hypervisor stub installation functions.67*68* These must be called with the MMU and D-cache off.69* They are not ABI compliant and are only intended to be called from the kernel70* entry points in head.S.71*/72@ Call this from the primary CPU73ENTRY(__hyp_stub_install)74store_primary_cpu_mode r4, r575ENDPROC(__hyp_stub_install)7677@ fall through...7879@ Secondary CPUs should call here80ENTRY(__hyp_stub_install_secondary)81mrs r4, cpsr82and r4, r4, #MODE_MASK8384/*85* If the secondary has booted with a different mode, give up86* immediately.87*/88compare_cpu_mode_with_primary r4, r5, r689retne lr9091/*92* Once we have given up on one CPU, we do not try to install the93* stub hypervisor on the remaining ones: because the saved boot mode94* is modified, it can't compare equal to the CPSR mode field any95* more.96*97* Otherwise...98*/99100cmp r4, #HYP_MODE101retne lr @ give up if the CPU is not in HYP mode102103/*104* Configure HSCTLR to set correct exception endianness/instruction set105* state etc.106* Turn off all traps107* Eventually, CPU-specific code might be needed -- assume not for now108*109* This code relies on the "eret" instruction to synchronize the110* various coprocessor accesses. This is done when we switch to SVC111* (see safe_svcmode_maskall).112*/113@ Now install the hypervisor stub:114W(adr) r7, __hyp_stub_vectors115mcr p15, 4, r7, c12, c0, 0 @ set hypervisor vector base (HVBAR)116117@ Disable all traps, so we don't get any nasty surprise118mov r7, #0119mcr p15, 4, r7, c1, c1, 0 @ HCR120mcr p15, 4, r7, c1, c1, 2 @ HCPTR121mcr p15, 4, r7, c1, c1, 3 @ HSTR122123THUMB( orr r7, #(1 << 30) ) @ HSCTLR.TE124ARM_BE8(orr r7, r7, #(1 << 25)) @ HSCTLR.EE125mcr p15, 4, r7, c1, c0, 0 @ HSCTLR126127mrc p15, 4, r7, c1, c1, 1 @ HDCR128and r7, #0x1f @ Preserve HPMN129mcr p15, 4, r7, c1, c1, 1 @ HDCR130131@ Make sure NS-SVC is initialised appropriately132mrc p15, 0, r7, c1, c0, 0 @ SCTLR133orr r7, #(1 << 5) @ CP15 barriers enabled134bic r7, #(3 << 7) @ Clear SED/ITD for v8 (RES0 for v7)135bic r7, #(3 << 19) @ WXN and UWXN disabled136mcr p15, 0, r7, c1, c0, 0 @ SCTLR137138mrc p15, 0, r7, c0, c0, 0 @ MIDR139mcr p15, 4, r7, c0, c0, 0 @ VPIDR140141mrc p15, 0, r7, c0, c0, 5 @ MPIDR142mcr p15, 4, r7, c0, c0, 5 @ VMPIDR143144#if !defined(ZIMAGE) && defined(CONFIG_ARM_ARCH_TIMER)145@ make CNTP_* and CNTPCT accessible from PL1146mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1147ubfx r7, r7, #16, #4148teq r7, #0149beq 1f150mrc p15, 4, r7, c14, c1, 0 @ CNTHCTL151orr r7, r7, #3 @ PL1PCEN | PL1PCTEN152mcr p15, 4, r7, c14, c1, 0 @ CNTHCTL153mov r7, #0154mcrr p15, 4, r7, r7, c14 @ CNTVOFF155156@ Disable virtual timer in case it was counting157mrc p15, 0, r7, c14, c3, 1 @ CNTV_CTL158bic r7, #1 @ Clear ENABLE159mcr p15, 0, r7, c14, c3, 1 @ CNTV_CTL1601:161#endif162163#ifdef CONFIG_ARM_GIC_V3164@ Check whether GICv3 system registers are available165mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1166ubfx r7, r7, #28, #4167teq r7, #0168beq 2f169170@ Enable system register accesses171mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE172orr r7, r7, #(ICC_SRE_EL2_ENABLE | ICC_SRE_EL2_SRE)173mcr p15, 4, r7, c12, c9, 5 @ ICC_HSRE174isb175176@ SRE bit could be forced to 0 by firmware.177@ Check whether it sticks before accessing any other sysreg178mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE179tst r7, #ICC_SRE_EL2_SRE180beq 2f181mov r7, #0182mcr p15, 4, r7, c12, c11, 0 @ ICH_HCR1832:184#endif185186bx lr @ The boot CPU mode is left in r4.187ENDPROC(__hyp_stub_install_secondary)188189__hyp_stub_do_trap:190#ifdef ZIMAGE191teq r0, #HVC_SET_VECTORS192bne 1f193/* Only the ZIMAGE stubs can change the HYP vectors */194mcr p15, 4, r1, c12, c0, 0 @ set HVBAR195b __hyp_stub_exit196#endif1971981: teq r0, #HVC_SOFT_RESTART199bne 2f200bx r12012022: ldr r0, =HVC_STUB_ERR203__ERET204205__hyp_stub_exit:206mov r0, #0207__ERET208ENDPROC(__hyp_stub_do_trap)209210/*211* __hyp_set_vectors is only used when ZIMAGE must bounce between HYP212* and SVC. For the kernel itself, the vectors are set once and for213* all by the stubs.214*/215ENTRY(__hyp_set_vectors)216mov r1, r0217mov r0, #HVC_SET_VECTORS218__HVC(0)219ret lr220ENDPROC(__hyp_set_vectors)221222ENTRY(__hyp_soft_restart)223mov r1, r0224mov r0, #HVC_SOFT_RESTART225__HVC(0)226ret lr227ENDPROC(__hyp_soft_restart)228229.align 5230ENTRY(__hyp_stub_vectors)231__hyp_stub_reset: W(b) .232__hyp_stub_und: W(b) .233__hyp_stub_svc: W(b) .234__hyp_stub_pabort: W(b) .235__hyp_stub_dabort: W(b) .236__hyp_stub_trap: W(b) __hyp_stub_do_trap237__hyp_stub_irq: W(b) .238__hyp_stub_fiq: W(b) .239ENDPROC(__hyp_stub_vectors)240241242243