2 * Copyright (c) 2012 Linaro Limited.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/init.h>
20 #include <linux/linkage.h>
21 #include <asm/assembler.h>
26 * For the kernel proper, we need to find out the CPU boot mode long after
27 * boot, so we need to store it in a writable variable.
29 * This is not in .bss, because we set it sufficiently early that the boot-time
30 * zeroing of .bss would clobber it.
33 ENTRY(__boot_cpu_mode)
38 * Save the primary CPU boot mode. Requires 3 scratch registers.
40 .macro store_primary_cpu_mode reg1, reg2, reg3
42 and \reg1, \reg1, #MODE_MASK
43 adr \reg2, .L__boot_cpu_mode_offset
45 str \reg1, [\reg2, \reg3]
49 * Compare the current mode with the one saved on the primary CPU.
50 * If they don't match, record that fact. The Z bit indicates
51 * if there's a match or not.
52 * Requires 3 additionnal scratch registers.
54 .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
55 adr \reg2, .L__boot_cpu_mode_offset
57 ldr \reg1, [\reg2, \reg3]
58 cmp \mode, \reg1 @ matches primary CPU boot mode?
59 orrne r7, r7, #BOOT_CPU_MODE_MISMATCH
60 strne r7, [r5, r6] @ record what happened and give up
65 .macro store_primary_cpu_mode reg1:req, reg2:req, reg3:req
69 * The zImage loader only runs on one CPU, so we don't bother with mult-CPU
70 * consistency checking:
72 .macro compare_cpu_mode_with_primary mode, reg1, reg2, reg3
79 * Hypervisor stub installation functions.
81 * These must be called with the MMU and D-cache off.
82 * They are not ABI compliant and are only intended to be called from the kernel
83 * entry points in head.S.
85 @ Call this from the primary CPU
86 ENTRY(__hyp_stub_install)
87 store_primary_cpu_mode r4, r5, r6
88 ENDPROC(__hyp_stub_install)
92 @ Secondary CPUs should call here
93 ENTRY(__hyp_stub_install_secondary)
95 and r4, r4, #MODE_MASK
98 * If the secondary has booted with a different mode, give up
101 compare_cpu_mode_with_primary r4, r5, r6, r7
105 * Once we have given up on one CPU, we do not try to install the
106 * stub hypervisor on the remaining ones: because the saved boot mode
107 * is modified, it can't compare equal to the CPSR mode field any
114 bxne lr @ give up if the CPU is not in HYP mode
117 * Configure HSCTLR to set correct exception endianness/instruction set
120 * Eventually, CPU-specific code might be needed -- assume not for now
122 * This code relies on the "eret" instruction to synchronize the
123 * various coprocessor accesses.
125 @ Now install the hypervisor stub:
126 adr r7, __hyp_stub_vectors
127 mcr p15, 4, r7, c12, c0, 0 @ set hypervisor vector base (HVBAR)
129 @ Disable all traps, so we don't get any nasty surprise
131 mcr p15, 4, r7, c1, c1, 0 @ HCR
132 mcr p15, 4, r7, c1, c1, 2 @ HCPTR
133 mcr p15, 4, r7, c1, c1, 3 @ HSTR
135 THUMB( orr r7, #(1 << 30) ) @ HSCTLR.TE
136 #ifdef CONFIG_CPU_BIG_ENDIAN
137 orr r7, #(1 << 9) @ HSCTLR.EE
139 mcr p15, 4, r7, c1, c0, 0 @ HSCTLR
141 mrc p15, 4, r7, c1, c1, 1 @ HDCR
142 and r7, #0x1f @ Preserve HPMN
143 mcr p15, 4, r7, c1, c1, 1 @ HDCR
145 #if !defined(ZIMAGE) && defined(CONFIG_ARM_ARCH_TIMER)
146 @ make CNTP_* and CNTPCT accessible from PL1
147 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
158 bic r7, r4, #MODE_MASK
159 orr r7, r7, #SVC_MODE
160 THUMB( orr r7, r7, #PSR_T_BIT )
161 msr spsr_cxsf, r7 @ This is SPSR_hyp.
163 __MSR_ELR_HYP(14) @ msr elr_hyp, lr
164 __ERET @ return, switching to SVC mode
165 @ The boot CPU mode is left in r4.
166 ENDPROC(__hyp_stub_install_secondary)
170 mrceq p15, 4, r0, c12, c0, 0 @ get HVBAR
171 mcrne p15, 4, r0, c12, c0, 0 @ set HVBAR
173 ENDPROC(__hyp_stub_do_trap)
176 * __hyp_set_vectors: Call this after boot to set the initial hypervisor
177 * vectors as part of hypervisor installation. On an SMP system, this should
178 * be called on each CPU.
180 * r0 must be the physical address of the new vector table (which must lie in
181 * the bottom 4GB of physical address space.
183 * r0 must be 32-byte aligned.
185 * Before calling this, you must check that the stub hypervisor is installed
186 * everywhere, by waiting for any secondary CPUs to be brought up and then
187 * checking that BOOT_CPU_MODE_HAVE_HYP(__boot_cpu_mode) is true.
189 * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
190 * something else went wrong... in such cases, trying to install a new
191 * hypervisor is unlikely to work as desired.
193 * When you call into your shiny new hypervisor, sp_hyp will contain junk,
194 * so you will need to set that to something sensible at the new hypervisor's
195 * initialisation entry point.
197 ENTRY(__hyp_get_vectors)
199 ENDPROC(__hyp_get_vectors)
201 ENTRY(__hyp_set_vectors)
204 ENDPROC(__hyp_set_vectors)
208 .L__boot_cpu_mode_offset:
209 .long __boot_cpu_mode - .
214 __hyp_stub_reset: W(b) .
215 __hyp_stub_und: W(b) .
216 __hyp_stub_svc: W(b) .
217 __hyp_stub_pabort: W(b) .
218 __hyp_stub_dabort: W(b) .
219 __hyp_stub_trap: W(b) __hyp_stub_do_trap
220 __hyp_stub_irq: W(b) .
221 __hyp_stub_fiq: W(b) .
222 ENDPROC(__hyp_stub_vectors)