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