11ef92385SAndre Przywara /* 21ef92385SAndre Przywara * (C) Copyright 2013 3f833e790SAndre Przywara * Andre Przywara, Linaro <andre.przywara@linaro.org> 41ef92385SAndre Przywara * 51ef92385SAndre Przywara * Routines to transition ARMv7 processors from secure into non-secure state 6d4296887SAndre Przywara * and from non-secure SVC into HYP mode 71ef92385SAndre Przywara * needed to enable ARMv7 virtualization for current hypervisors 81ef92385SAndre Przywara * 9f833e790SAndre Przywara * SPDX-License-Identifier: GPL-2.0+ 101ef92385SAndre Przywara */ 111ef92385SAndre Przywara 121ef92385SAndre Przywara #include <common.h> 131ef92385SAndre Przywara #include <asm/armv7.h> 141ef92385SAndre Przywara #include <asm/gic.h> 151ef92385SAndre Przywara #include <asm/io.h> 16f510aeaeSMarc Zyngier #include <asm/secure.h> 171ef92385SAndre Przywara 181ef92385SAndre Przywara static unsigned int read_id_pfr1(void) 191ef92385SAndre Przywara { 201ef92385SAndre Przywara unsigned int reg; 211ef92385SAndre Przywara 221ef92385SAndre Przywara asm("mrc p15, 0, %0, c0, c1, 1\n" : "=r"(reg)); 231ef92385SAndre Przywara return reg; 241ef92385SAndre Przywara } 251ef92385SAndre Przywara 261ef92385SAndre Przywara static unsigned long get_gicd_base_address(void) 271ef92385SAndre Przywara { 281ef92385SAndre Przywara #ifdef CONFIG_ARM_GIC_BASE_ADDRESS 291ef92385SAndre Przywara return CONFIG_ARM_GIC_BASE_ADDRESS + GIC_DIST_OFFSET; 301ef92385SAndre Przywara #else 311ef92385SAndre Przywara unsigned periphbase; 321ef92385SAndre Przywara 331ef92385SAndre Przywara /* get the GIC base address from the CBAR register */ 341ef92385SAndre Przywara asm("mrc p15, 4, %0, c15, c0, 0\n" : "=r" (periphbase)); 351ef92385SAndre Przywara 361ef92385SAndre Przywara /* the PERIPHBASE can be mapped above 4 GB (lower 8 bits used to 371ef92385SAndre Przywara * encode this). Bail out here since we cannot access this without 381ef92385SAndre Przywara * enabling paging. 391ef92385SAndre Przywara */ 401ef92385SAndre Przywara if ((periphbase & 0xff) != 0) { 411ef92385SAndre Przywara printf("nonsec: PERIPHBASE is above 4 GB, no access.\n"); 421ef92385SAndre Przywara return -1; 431ef92385SAndre Przywara } 441ef92385SAndre Przywara 451ef92385SAndre Przywara return (periphbase & CBAR_MASK) + GIC_DIST_OFFSET; 461ef92385SAndre Przywara #endif 471ef92385SAndre Przywara } 481ef92385SAndre Przywara 49f510aeaeSMarc Zyngier static void relocate_secure_section(void) 50f510aeaeSMarc Zyngier { 51f510aeaeSMarc Zyngier #ifdef CONFIG_ARMV7_SECURE_BASE 52f510aeaeSMarc Zyngier size_t sz = __secure_end - __secure_start; 53f510aeaeSMarc Zyngier 54f510aeaeSMarc Zyngier memcpy((void *)CONFIG_ARMV7_SECURE_BASE, __secure_start, sz); 55f510aeaeSMarc Zyngier flush_dcache_range(CONFIG_ARMV7_SECURE_BASE, 56f510aeaeSMarc Zyngier CONFIG_ARMV7_SECURE_BASE + sz + 1); 57f510aeaeSMarc Zyngier invalidate_icache_all(); 58f510aeaeSMarc Zyngier #endif 59f510aeaeSMarc Zyngier } 60f510aeaeSMarc Zyngier 61ba6a1698SAndre Przywara static void kick_secondary_cpus_gic(unsigned long gicdaddr) 62ba6a1698SAndre Przywara { 63ba6a1698SAndre Przywara /* kick all CPUs (except this one) by writing to GICD_SGIR */ 64ba6a1698SAndre Przywara writel(1U << 24, gicdaddr + GICD_SGIR); 65ba6a1698SAndre Przywara } 66ba6a1698SAndre Przywara 67ba6a1698SAndre Przywara void __weak smp_kick_all_cpus(void) 68ba6a1698SAndre Przywara { 6956992743Stang yuantian unsigned long gic_dist_addr; 7056992743Stang yuantian 7156992743Stang yuantian gic_dist_addr = get_gicd_base_address(); 7256992743Stang yuantian if (gic_dist_addr == -1) 7356992743Stang yuantian return; 7456992743Stang yuantian 75ba6a1698SAndre Przywara kick_secondary_cpus_gic(gic_dist_addr); 76ba6a1698SAndre Przywara } 77ba6a1698SAndre Przywara 78f510aeaeSMarc Zyngier int armv7_init_nonsec(void) 791ef92385SAndre Przywara { 801ef92385SAndre Przywara unsigned int reg; 811ef92385SAndre Przywara unsigned itlinesnr, i; 8256992743Stang yuantian unsigned long gic_dist_addr; 831ef92385SAndre Przywara 841ef92385SAndre Przywara /* check whether the CPU supports the security extensions */ 851ef92385SAndre Przywara reg = read_id_pfr1(); 861ef92385SAndre Przywara if ((reg & 0xF0) == 0) { 871ef92385SAndre Przywara printf("nonsec: Security extensions not implemented.\n"); 881ef92385SAndre Przywara return -1; 891ef92385SAndre Przywara } 901ef92385SAndre Przywara 911ef92385SAndre Przywara /* the SCR register will be set directly in the monitor mode handler, 921ef92385SAndre Przywara * according to the spec one should not tinker with it in secure state 931ef92385SAndre Przywara * in SVC mode. Do not try to read it once in non-secure state, 941ef92385SAndre Przywara * any access to it will trap. 951ef92385SAndre Przywara */ 961ef92385SAndre Przywara 971ef92385SAndre Przywara gic_dist_addr = get_gicd_base_address(); 981ef92385SAndre Przywara if (gic_dist_addr == -1) 991ef92385SAndre Przywara return -1; 1001ef92385SAndre Przywara 1011ef92385SAndre Przywara /* enable the GIC distributor */ 1021ef92385SAndre Przywara writel(readl(gic_dist_addr + GICD_CTLR) | 0x03, 1031ef92385SAndre Przywara gic_dist_addr + GICD_CTLR); 1041ef92385SAndre Przywara 1051ef92385SAndre Przywara /* TYPER[4:0] contains an encoded number of available interrupts */ 1061ef92385SAndre Przywara itlinesnr = readl(gic_dist_addr + GICD_TYPER) & 0x1f; 1071ef92385SAndre Przywara 1081ef92385SAndre Przywara /* set all bits in the GIC group registers to one to allow access 1091ef92385SAndre Przywara * from non-secure state. The first 32 interrupts are private per 1101ef92385SAndre Przywara * CPU and will be set later when enabling the GIC for each core 1111ef92385SAndre Przywara */ 1121ef92385SAndre Przywara for (i = 1; i <= itlinesnr; i++) 1131ef92385SAndre Przywara writel((unsigned)-1, gic_dist_addr + GICD_IGROUPRn + 4 * i); 1141ef92385SAndre Przywara 115*02251eefSPeng Fan /* 116*02251eefSPeng Fan * Relocate secure section before any cpu runs in secure ram. 117*02251eefSPeng Fan * smp_kick_all_cpus may enable other cores and runs into secure 118*02251eefSPeng Fan * ram, so need to relocate secure section before enabling other 119*02251eefSPeng Fan * cores. 120*02251eefSPeng Fan */ 121*02251eefSPeng Fan relocate_secure_section(); 122*02251eefSPeng Fan 123f510aeaeSMarc Zyngier #ifndef CONFIG_ARMV7_PSCI 124f510aeaeSMarc Zyngier smp_set_core_boot_addr((unsigned long)secure_ram_addr(_smp_pen), -1); 125ba6a1698SAndre Przywara smp_kick_all_cpus(); 126f510aeaeSMarc Zyngier #endif 127ba6a1698SAndre Przywara 128ba6a1698SAndre Przywara /* call the non-sec switching code on this CPU also */ 129f510aeaeSMarc Zyngier secure_ram_addr(_nonsec_init)(); 1301ef92385SAndre Przywara return 0; 1311ef92385SAndre Przywara } 132