1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2012 Linaro Limited.
6 #include <linux/init.h>
7 #include <linux/irqchip/arm-gic-v3.h>
8 #include <linux/linkage.h>
9 #include <asm/assembler.h>
14 * For the kernel proper, we need to find out the CPU boot mode long after
15 * boot, so we need to store it in a writable variable.
17 * This is not in .bss, because we set it sufficiently early that the boot-time
18 * zeroing of .bss would clobber it.
22 ENTRY(__boot_cpu_mode)
27 * Save the primary CPU boot mode. Requires 3 scratch registers.
29 .macro store_primary_cpu_mode reg1, reg2, reg3
31 and \reg1, \reg1, #MODE_MASK
32 adr \reg2, .L__boot_cpu_mode_offset
34 str \reg1, [\reg2, \reg3]
38 * Compare the current mode with the one saved on the primary CPU.
39 * If they don't match, record that fact. The Z bit indicates
40 * if there's a match or not.
41 * Requires 3 additionnal scratch registers.
43 .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
44 adr \reg2, .L__boot_cpu_mode_offset
46 ldr \reg1, [\reg2, \reg3]
47 cmp \mode, \reg1 @ matches primary CPU boot mode?
48 orrne \reg1, \reg1, #BOOT_CPU_MODE_MISMATCH
49 strne \reg1, [\reg2, \reg3] @ record what happened and give up
54 .macro store_primary_cpu_mode reg1:req, reg2:req, reg3:req
58 * The zImage loader only runs on one CPU, so we don't bother with mult-CPU
59 * consistency checking:
61 .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
68 * Hypervisor stub installation functions.
70 * These must be called with the MMU and D-cache off.
71 * They are not ABI compliant and are only intended to be called from the kernel
72 * entry points in head.S.
74 @ Call this from the primary CPU
75 ENTRY(__hyp_stub_install)
76 store_primary_cpu_mode r4, r5, r6
77 ENDPROC(__hyp_stub_install)
81 @ Secondary CPUs should call here
82 ENTRY(__hyp_stub_install_secondary)
84 and r4, r4, #MODE_MASK
87 * If the secondary has booted with a different mode, give up
90 compare_cpu_mode_with_primary r4, r5, r6, r7
94 * Once we have given up on one CPU, we do not try to install the
95 * stub hypervisor on the remaining ones: because the saved boot mode
96 * is modified, it can't compare equal to the CPSR mode field any
103 retne lr @ give up if the CPU is not in HYP mode
106 * Configure HSCTLR to set correct exception endianness/instruction set
109 * Eventually, CPU-specific code might be needed -- assume not for now
111 * This code relies on the "eret" instruction to synchronize the
112 * various coprocessor accesses. This is done when we switch to SVC
113 * (see safe_svcmode_maskall).
115 @ Now install the hypervisor stub:
116 W(adr) r7, __hyp_stub_vectors
117 mcr p15, 4, r7, c12, c0, 0 @ set hypervisor vector base (HVBAR)
119 @ Disable all traps, so we don't get any nasty surprise
121 mcr p15, 4, r7, c1, c1, 0 @ HCR
122 mcr p15, 4, r7, c1, c1, 2 @ HCPTR
123 mcr p15, 4, r7, c1, c1, 3 @ HSTR
125 THUMB( orr r7, #(1 << 30) ) @ HSCTLR.TE
126 ARM_BE8(orr r7, r7, #(1 << 25)) @ HSCTLR.EE
127 mcr p15, 4, r7, c1, c0, 0 @ HSCTLR
129 mrc p15, 4, r7, c1, c1, 1 @ HDCR
130 and r7, #0x1f @ Preserve HPMN
131 mcr p15, 4, r7, c1, c1, 1 @ HDCR
133 @ Make sure NS-SVC is initialised appropriately
134 mrc p15, 0, r7, c1, c0, 0 @ SCTLR
135 orr r7, #(1 << 5) @ CP15 barriers enabled
136 bic r7, #(3 << 7) @ Clear SED/ITD for v8 (RES0 for v7)
137 bic r7, #(3 << 19) @ WXN and UWXN disabled
138 mcr p15, 0, r7, c1, c0, 0 @ SCTLR
140 mrc p15, 0, r7, c0, c0, 0 @ MIDR
141 mcr p15, 4, r7, c0, c0, 0 @ VPIDR
143 mrc p15, 0, r7, c0, c0, 5 @ MPIDR
144 mcr p15, 4, r7, c0, c0, 5 @ VMPIDR
146 #if !defined(ZIMAGE) && defined(CONFIG_ARM_ARCH_TIMER)
147 @ make CNTP_* and CNTPCT accessible from PL1
148 mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1
152 mrc p15, 4, r7, c14, c1, 0 @ CNTHCTL
153 orr r7, r7, #3 @ PL1PCEN | PL1PCTEN
154 mcr p15, 4, r7, c14, c1, 0 @ CNTHCTL
156 mcrr p15, 4, r7, r7, c14 @ CNTVOFF
158 @ Disable virtual timer in case it was counting
159 mrc p15, 0, r7, c14, c3, 1 @ CNTV_CTL
160 bic r7, #1 @ Clear ENABLE
161 mcr p15, 0, r7, c14, c3, 1 @ CNTV_CTL
165 #ifdef CONFIG_ARM_GIC_V3
166 @ Check whether GICv3 system registers are available
167 mrc p15, 0, r7, c0, c1, 1 @ ID_PFR1
172 @ Enable system register accesses
173 mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE
174 orr r7, r7, #(ICC_SRE_EL2_ENABLE | ICC_SRE_EL2_SRE)
175 mcr p15, 4, r7, c12, c9, 5 @ ICC_HSRE
178 @ SRE bit could be forced to 0 by firmware.
179 @ Check whether it sticks before accessing any other sysreg
180 mrc p15, 4, r7, c12, c9, 5 @ ICC_HSRE
181 tst r7, #ICC_SRE_EL2_SRE
184 mcr p15, 4, r7, c12, c11, 0 @ ICH_HCR
188 bx lr @ The boot CPU mode is left in r4.
189 ENDPROC(__hyp_stub_install_secondary)
192 teq r0, #HVC_SET_VECTORS
194 mcr p15, 4, r1, c12, c0, 0 @ set HVBAR
197 1: teq r0, #HVC_SOFT_RESTART
201 1: teq r0, #HVC_RESET_VECTORS
204 ldr r0, =HVC_STUB_ERR
210 ENDPROC(__hyp_stub_do_trap)
213 * __hyp_set_vectors: Call this after boot to set the initial hypervisor
214 * vectors as part of hypervisor installation. On an SMP system, this should
215 * be called on each CPU.
217 * r0 must be the physical address of the new vector table (which must lie in
218 * the bottom 4GB of physical address space.
220 * r0 must be 32-byte aligned.
222 * Before calling this, you must check that the stub hypervisor is installed
223 * everywhere, by waiting for any secondary CPUs to be brought up and then
224 * checking that BOOT_CPU_MODE_HAVE_HYP(__boot_cpu_mode) is true.
226 * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
227 * something else went wrong... in such cases, trying to install a new
228 * hypervisor is unlikely to work as desired.
230 * When you call into your shiny new hypervisor, sp_hyp will contain junk,
231 * so you will need to set that to something sensible at the new hypervisor's
232 * initialisation entry point.
234 ENTRY(__hyp_set_vectors)
236 mov r0, #HVC_SET_VECTORS
239 ENDPROC(__hyp_set_vectors)
241 ENTRY(__hyp_soft_restart)
243 mov r0, #HVC_SOFT_RESTART
246 ENDPROC(__hyp_soft_restart)
248 ENTRY(__hyp_reset_vectors)
249 mov r0, #HVC_RESET_VECTORS
252 ENDPROC(__hyp_reset_vectors)
256 .L__boot_cpu_mode_offset:
257 .long __boot_cpu_mode - .
261 ENTRY(__hyp_stub_vectors)
262 __hyp_stub_reset: W(b) .
263 __hyp_stub_und: W(b) .
264 __hyp_stub_svc: W(b) .
265 __hyp_stub_pabort: W(b) .
266 __hyp_stub_dabort: W(b) .
267 __hyp_stub_trap: W(b) __hyp_stub_do_trap
268 __hyp_stub_irq: W(b) .
269 __hyp_stub_fiq: W(b) .
270 ENDPROC(__hyp_stub_vectors)