blob: 745670e549d90c0870fbcf05f30ad62ca175a53b [file] [log] [blame]
Andre Przywara45b940d2013-09-19 18:06:40 +02001/*
Andre Przywarad4296882013-09-19 18:06:45 +02002 * code for switching cores into non-secure state and into HYP mode
Andre Przywara45b940d2013-09-19 18:06:40 +02003 *
4 * Copyright (c) 2013 Andre Przywara <andre.przywara@linaro.org>
5 *
Andre Przywaraf833e792013-10-07 10:56:51 +02006 * SPDX-License-Identifier: GPL-2.0+
Andre Przywara45b940d2013-09-19 18:06:40 +02007 */
8
9#include <config.h>
Andre Przywara16212b52013-09-19 18:06:41 +020010#include <linux/linkage.h>
11#include <asm/gic.h>
12#include <asm/armv7.h>
Marc Zyngierf510aea2014-07-12 14:24:03 +010013#include <asm/proc-armv/ptrace.h>
Andre Przywara16212b52013-09-19 18:06:41 +020014
15.arch_extension sec
Andre Przywarad4296882013-09-19 18:06:45 +020016.arch_extension virt
Andre Przywara45b940d2013-09-19 18:06:40 +020017
Marc Zyngierf510aea2014-07-12 14:24:03 +010018 .pushsection ._secure.text, "ax"
19
Masahiro Yamada3064d592013-10-07 11:46:56 +090020 .align 5
Andre Przywarad4296882013-09-19 18:06:45 +020021/* the vector table for secure state and HYP mode */
Andre Przywara45b940d2013-09-19 18:06:40 +020022_monitor_vectors:
23 .word 0 /* reset */
24 .word 0 /* undef */
25 adr pc, _secure_monitor
26 .word 0
27 .word 0
Andre Przywara45b940d2013-09-19 18:06:40 +020028 .word 0
29 .word 0
Marc Zyngierf510aea2014-07-12 14:24:03 +010030 .word 0
31
32.macro is_cpu_virt_capable tmp
33 mrc p15, 0, \tmp, c0, c1, 1 @ read ID_PFR1
34 and \tmp, \tmp, #CPUID_ARM_VIRT_MASK @ mask virtualization bits
35 cmp \tmp, #(1 << CPUID_ARM_VIRT_SHIFT)
36.endm
Andre Przywara45b940d2013-09-19 18:06:40 +020037
38/*
39 * secure monitor handler
40 * U-boot calls this "software interrupt" in start.S
41 * This is executed on a "smc" instruction, we use a "smc #0" to switch
42 * to non-secure state.
Marc Zyngierf510aea2014-07-12 14:24:03 +010043 * r0, r1, r2: passed to the callee
44 * ip: target PC
Andre Przywara45b940d2013-09-19 18:06:40 +020045 */
Andre Przywara45b940d2013-09-19 18:06:40 +020046_secure_monitor:
Marc Zyngier38510a42014-07-12 14:24:05 +010047#ifdef CONFIG_ARMV7_PSCI
48 ldr r5, =_psci_vectors @ Switch to the next monitor
49 mcr p15, 0, r5, c12, c0, 1
50 isb
Andre Przywara45b940d2013-09-19 18:06:40 +020051
Marc Zyngier38510a42014-07-12 14:24:05 +010052 @ Obtain a secure stack, and configure the PSCI backend
53 bl psci_arch_init
54#endif
55
56 mrc p15, 0, r5, c1, c1, 0 @ read SCR
57 bic r5, r5, #0x4a @ clear IRQ, EA, nET bits
58 orr r5, r5, #0x31 @ enable NS, AW, FW bits
59 @ FIQ preserved for secure mode
Marc Zyngierf510aea2014-07-12 14:24:03 +010060 mov r6, #SVC_MODE @ default mode is SVC
61 is_cpu_virt_capable r4
Marc Zyngier64fd44d2014-07-12 14:24:00 +010062#ifdef CONFIG_ARMV7_VIRT
Marc Zyngierf510aea2014-07-12 14:24:03 +010063 orreq r5, r5, #0x100 @ allow HVC instruction
64 moveq r6, #HYP_MODE @ Enter the kernel as HYP
Andre Przywarad4296882013-09-19 18:06:45 +020065#endif
66
Marc Zyngierf510aea2014-07-12 14:24:03 +010067 mcr p15, 0, r5, c1, c1, 0 @ write SCR (with NS bit set)
Marc Zyngier800c8352014-07-12 14:23:59 +010068 isb
Andre Przywara45b940d2013-09-19 18:06:40 +020069
Marc Zyngier64fd44d2014-07-12 14:24:00 +010070 bne 1f
Andre Przywarad4296882013-09-19 18:06:45 +020071
Marc Zyngier64fd44d2014-07-12 14:24:00 +010072 @ Reset CNTVOFF to 0 before leaving monitor mode
Marc Zyngierf510aea2014-07-12 14:24:03 +010073 mrc p15, 0, r4, c0, c1, 1 @ read ID_PFR1
74 ands r4, r4, #CPUID_ARM_GENTIMER_MASK @ test arch timer bits
75 movne r4, #0
76 mcrrne p15, 4, r4, r4, c14 @ Reset CNTVOFF to zero
Marc Zyngier64fd44d2014-07-12 14:24:00 +0100771:
Marc Zyngierf510aea2014-07-12 14:24:03 +010078 mov lr, ip
79 mov ip, #(F_BIT | I_BIT | A_BIT) @ Set A, I and F
80 tst lr, #1 @ Check for Thumb PC
81 orrne ip, ip, #T_BIT @ Set T if Thumb
82 orr ip, ip, r6 @ Slot target mode in
83 msr spsr_cxfs, ip @ Set full SPSR
84 movs pc, lr @ ERET to non-secure
Andre Przywara16212b52013-09-19 18:06:41 +020085
Marc Zyngierf510aea2014-07-12 14:24:03 +010086ENTRY(_do_nonsec_entry)
87 mov ip, r0
88 mov r0, r1
89 mov r1, r2
90 mov r2, r3
91 smc #0
92ENDPROC(_do_nonsec_entry)
Andre Przywarad4296882013-09-19 18:06:45 +020093
Marc Zyngierf510aea2014-07-12 14:24:03 +010094.macro get_cbar_addr addr
95#ifdef CONFIG_ARM_GIC_BASE_ADDRESS
96 ldr \addr, =CONFIG_ARM_GIC_BASE_ADDRESS
97#else
98 mrc p15, 4, \addr, c15, c0, 0 @ read CBAR
99 bfc \addr, #0, #15 @ clear reserved bits
100#endif
101.endm
102
103.macro get_gicd_addr addr
104 get_cbar_addr \addr
105 add \addr, \addr, #GIC_DIST_OFFSET @ GIC dist i/f offset
106.endm
107
108.macro get_gicc_addr addr, tmp
109 get_cbar_addr \addr
110 is_cpu_virt_capable \tmp
111 movne \tmp, #GIC_CPU_OFFSET_A9 @ GIC CPU offset for A9
112 moveq \tmp, #GIC_CPU_OFFSET_A15 @ GIC CPU offset for A15/A7
113 add \addr, \addr, \tmp
114.endm
115
116#ifndef CONFIG_ARMV7_PSCI
Andre Przywara16212b52013-09-19 18:06:41 +0200117/*
Andre Przywaraba6a1692013-09-19 18:06:44 +0200118 * Secondary CPUs start here and call the code for the core specific parts
119 * of the non-secure and HYP mode transition. The GIC distributor specific
120 * code has already been executed by a C function before.
121 * Then they go back to wfi and wait to be woken up by the kernel again.
122 */
123ENTRY(_smp_pen)
Marc Zyngierf510aea2014-07-12 14:24:03 +0100124 cpsid i
125 cpsid f
Andre Przywaraba6a1692013-09-19 18:06:44 +0200126
127 bl _nonsec_init
Andre Przywaraba6a1692013-09-19 18:06:44 +0200128
129 adr r0, _smp_pen @ do not use this address again
130 b smp_waitloop @ wait for IPIs, board specific
131ENDPROC(_smp_pen)
Marc Zyngierf510aea2014-07-12 14:24:03 +0100132#endif
Andre Przywaraba6a1692013-09-19 18:06:44 +0200133
134/*
Andre Przywara16212b52013-09-19 18:06:41 +0200135 * Switch a core to non-secure state.
136 *
137 * 1. initialize the GIC per-core interface
138 * 2. allow coprocessor access in non-secure modes
Andre Przywara16212b52013-09-19 18:06:41 +0200139 *
140 * Called from smp_pen by secondary cores and directly by the BSP.
141 * Do not assume that the stack is available and only use registers
142 * r0-r3 and r12.
143 *
144 * PERIPHBASE is used to get the GIC address. This could be 40 bits long,
145 * though, but we check this in C before calling this function.
146 */
147ENTRY(_nonsec_init)
Marc Zyngierf510aea2014-07-12 14:24:03 +0100148 get_gicd_addr r3
149
Andre Przywara16212b52013-09-19 18:06:41 +0200150 mvn r1, #0 @ all bits to 1
151 str r1, [r3, #GICD_IGROUPRn] @ allow private interrupts
152
Marc Zyngierf510aea2014-07-12 14:24:03 +0100153 get_gicc_addr r3, r1
Andre Przywara16212b52013-09-19 18:06:41 +0200154
Marc Zyngierf510aea2014-07-12 14:24:03 +0100155 mov r1, #3 @ Enable both groups
Andre Przywara16212b52013-09-19 18:06:41 +0200156 str r1, [r3, #GICC_CTLR] @ and clear all other bits
157 mov r1, #0xff
158 str r1, [r3, #GICC_PMR] @ set priority mask register
159
Marc Zyngierf510aea2014-07-12 14:24:03 +0100160 mrc p15, 0, r0, c1, c1, 2
Andre Przywara16212b52013-09-19 18:06:41 +0200161 movw r1, #0x3fff
Marc Zyngierf510aea2014-07-12 14:24:03 +0100162 movt r1, #0x0004
163 orr r0, r0, r1
164 mcr p15, 0, r0, c1, c1, 2 @ NSACR = all copros to non-sec
Andre Przywara16212b52013-09-19 18:06:41 +0200165
166/* The CNTFRQ register of the generic timer needs to be
167 * programmed in secure state. Some primary bootloaders / firmware
168 * omit this, so if the frequency is provided in the configuration,
169 * we do this here instead.
170 * But first check if we have the generic timer.
171 */
172#ifdef CONFIG_SYS_CLK_FREQ
173 mrc p15, 0, r0, c0, c1, 1 @ read ID_PFR1
174 and r0, r0, #CPUID_ARM_GENTIMER_MASK @ mask arch timer bits
175 cmp r0, #(1 << CPUID_ARM_GENTIMER_SHIFT)
176 ldreq r1, =CONFIG_SYS_CLK_FREQ
177 mcreq p15, 0, r1, c14, c0, 0 @ write CNTFRQ
178#endif
179
180 adr r1, _monitor_vectors
181 mcr p15, 0, r1, c12, c0, 1 @ set MVBAR to secure vectors
Andre Przywara16212b52013-09-19 18:06:41 +0200182 isb
Andre Przywara16212b52013-09-19 18:06:41 +0200183
184 mov r0, r3 @ return GICC address
Andre Przywara16212b52013-09-19 18:06:41 +0200185 bx lr
186ENDPROC(_nonsec_init)
Andre Przywaraba6a1692013-09-19 18:06:44 +0200187
188#ifdef CONFIG_SMP_PEN_ADDR
189/* void __weak smp_waitloop(unsigned previous_address); */
190ENTRY(smp_waitloop)
191 wfi
192 ldr r1, =CONFIG_SMP_PEN_ADDR @ load start address
193 ldr r1, [r1]
194 cmp r0, r1 @ make sure we dont execute this code
195 beq smp_waitloop @ again (due to a spurious wakeup)
Marc Zyngierf510aea2014-07-12 14:24:03 +0100196 mov r0, r1
197 b _do_nonsec_entry
Andre Przywaraba6a1692013-09-19 18:06:44 +0200198ENDPROC(smp_waitloop)
199.weak smp_waitloop
200#endif
Andre Przywarad4296882013-09-19 18:06:45 +0200201
Marc Zyngierf510aea2014-07-12 14:24:03 +0100202 .popsection