]> git.sur5r.net Git - u-boot/blob - arch/arm/cpu/armv7/nonsec_virt.S
ARM: HYP/non-sec: allow relocation to secure RAM
[u-boot] / arch / arm / cpu / armv7 / nonsec_virt.S
1 /*
2  * code for switching cores into non-secure state and into HYP mode
3  *
4  * Copyright (c) 2013   Andre Przywara <andre.przywara@linaro.org>
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <config.h>
10 #include <linux/linkage.h>
11 #include <asm/gic.h>
12 #include <asm/armv7.h>
13 #include <asm/proc-armv/ptrace.h>
14
15 .arch_extension sec
16 .arch_extension virt
17
18         .pushsection ._secure.text, "ax"
19
20         .align  5
21 /* the vector table for secure state and HYP mode */
22 _monitor_vectors:
23         .word 0 /* reset */
24         .word 0 /* undef */
25         adr pc, _secure_monitor
26         .word 0
27         .word 0
28         .word 0
29         .word 0
30         .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
37
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.
43  * r0, r1, r2: passed to the callee
44  * ip: target PC
45  */
46 _secure_monitor:
47         mrc     p15, 0, r5, c1, c1, 0           @ read SCR
48         bic     r5, r5, #0x4e                   @ clear IRQ, FIQ, EA, nET bits
49         orr     r5, r5, #0x31                   @ enable NS, AW, FW bits
50
51         mov     r6, #SVC_MODE                   @ default mode is SVC
52         is_cpu_virt_capable r4
53 #ifdef CONFIG_ARMV7_VIRT
54         orreq   r5, r5, #0x100                  @ allow HVC instruction
55         moveq   r6, #HYP_MODE                   @ Enter the kernel as HYP
56 #endif
57
58         mcr     p15, 0, r5, c1, c1, 0           @ write SCR (with NS bit set)
59         isb
60
61         bne     1f
62
63         @ Reset CNTVOFF to 0 before leaving monitor mode
64         mrc     p15, 0, r4, c0, c1, 1           @ read ID_PFR1
65         ands    r4, r4, #CPUID_ARM_GENTIMER_MASK        @ test arch timer bits
66         movne   r4, #0
67         mcrrne  p15, 4, r4, r4, c14             @ Reset CNTVOFF to zero
68 1:
69         mov     lr, ip
70         mov     ip, #(F_BIT | I_BIT | A_BIT)    @ Set A, I and F
71         tst     lr, #1                          @ Check for Thumb PC
72         orrne   ip, ip, #T_BIT                  @ Set T if Thumb
73         orr     ip, ip, r6                      @ Slot target mode in
74         msr     spsr_cxfs, ip                   @ Set full SPSR
75         movs    pc, lr                          @ ERET to non-secure
76
77 ENTRY(_do_nonsec_entry)
78         mov     ip, r0
79         mov     r0, r1
80         mov     r1, r2
81         mov     r2, r3
82         smc     #0
83 ENDPROC(_do_nonsec_entry)
84
85 .macro get_cbar_addr    addr
86 #ifdef CONFIG_ARM_GIC_BASE_ADDRESS
87         ldr     \addr, =CONFIG_ARM_GIC_BASE_ADDRESS
88 #else
89         mrc     p15, 4, \addr, c15, c0, 0       @ read CBAR
90         bfc     \addr, #0, #15                  @ clear reserved bits
91 #endif
92 .endm
93
94 .macro get_gicd_addr    addr
95         get_cbar_addr   \addr
96         add     \addr, \addr, #GIC_DIST_OFFSET  @ GIC dist i/f offset
97 .endm
98
99 .macro get_gicc_addr    addr, tmp
100         get_cbar_addr   \addr
101         is_cpu_virt_capable \tmp
102         movne   \tmp, #GIC_CPU_OFFSET_A9        @ GIC CPU offset for A9
103         moveq   \tmp, #GIC_CPU_OFFSET_A15       @ GIC CPU offset for A15/A7
104         add     \addr, \addr, \tmp
105 .endm
106
107 #ifndef CONFIG_ARMV7_PSCI
108 /*
109  * Secondary CPUs start here and call the code for the core specific parts
110  * of the non-secure and HYP mode transition. The GIC distributor specific
111  * code has already been executed by a C function before.
112  * Then they go back to wfi and wait to be woken up by the kernel again.
113  */
114 ENTRY(_smp_pen)
115         cpsid   i
116         cpsid   f
117
118         bl      _nonsec_init
119
120         adr     r0, _smp_pen                    @ do not use this address again
121         b       smp_waitloop                    @ wait for IPIs, board specific
122 ENDPROC(_smp_pen)
123 #endif
124
125 /*
126  * Switch a core to non-secure state.
127  *
128  *  1. initialize the GIC per-core interface
129  *  2. allow coprocessor access in non-secure modes
130  *
131  * Called from smp_pen by secondary cores and directly by the BSP.
132  * Do not assume that the stack is available and only use registers
133  * r0-r3 and r12.
134  *
135  * PERIPHBASE is used to get the GIC address. This could be 40 bits long,
136  * though, but we check this in C before calling this function.
137  */
138 ENTRY(_nonsec_init)
139         get_gicd_addr   r3
140
141         mvn     r1, #0                          @ all bits to 1
142         str     r1, [r3, #GICD_IGROUPRn]        @ allow private interrupts
143
144         get_gicc_addr   r3, r1
145
146         mov     r1, #3                          @ Enable both groups
147         str     r1, [r3, #GICC_CTLR]            @ and clear all other bits
148         mov     r1, #0xff
149         str     r1, [r3, #GICC_PMR]             @ set priority mask register
150
151         mrc     p15, 0, r0, c1, c1, 2
152         movw    r1, #0x3fff
153         movt    r1, #0x0004
154         orr     r0, r0, r1
155         mcr     p15, 0, r0, c1, c1, 2           @ NSACR = all copros to non-sec
156
157 /* The CNTFRQ register of the generic timer needs to be
158  * programmed in secure state. Some primary bootloaders / firmware
159  * omit this, so if the frequency is provided in the configuration,
160  * we do this here instead.
161  * But first check if we have the generic timer.
162  */
163 #ifdef CONFIG_SYS_CLK_FREQ
164         mrc     p15, 0, r0, c0, c1, 1           @ read ID_PFR1
165         and     r0, r0, #CPUID_ARM_GENTIMER_MASK        @ mask arch timer bits
166         cmp     r0, #(1 << CPUID_ARM_GENTIMER_SHIFT)
167         ldreq   r1, =CONFIG_SYS_CLK_FREQ
168         mcreq   p15, 0, r1, c14, c0, 0          @ write CNTFRQ
169 #endif
170
171         adr     r1, _monitor_vectors
172         mcr     p15, 0, r1, c12, c0, 1          @ set MVBAR to secure vectors
173         isb
174
175         mov     r0, r3                          @ return GICC address
176         bx      lr
177 ENDPROC(_nonsec_init)
178
179 #ifdef CONFIG_SMP_PEN_ADDR
180 /* void __weak smp_waitloop(unsigned previous_address); */
181 ENTRY(smp_waitloop)
182         wfi
183         ldr     r1, =CONFIG_SMP_PEN_ADDR        @ load start address
184         ldr     r1, [r1]
185         cmp     r0, r1                  @ make sure we dont execute this code
186         beq     smp_waitloop            @ again (due to a spurious wakeup)
187         mov     r0, r1
188         b       _do_nonsec_entry
189 ENDPROC(smp_waitloop)
190 .weak smp_waitloop
191 #endif
192
193         .popsection