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