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
read_id_pfr1(void)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
get_gicd_base_address(void)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
4973169874SIan Campbell /* Define a specific version of this function to enable any available
5073169874SIan Campbell * hardware protections for the reserved region */
protect_secure_section(void)5173169874SIan Campbell void __weak protect_secure_section(void) {}
5273169874SIan Campbell
relocate_secure_section(void)53f510aeaeSMarc Zyngier static void relocate_secure_section(void)
54f510aeaeSMarc Zyngier {
55f510aeaeSMarc Zyngier #ifdef CONFIG_ARMV7_SECURE_BASE
56f510aeaeSMarc Zyngier size_t sz = __secure_end - __secure_start;
57*da91cfedSStefan Agner unsigned long szflush = ALIGN(sz + 1, CONFIG_SYS_CACHELINE_SIZE);
58f510aeaeSMarc Zyngier
59f510aeaeSMarc Zyngier memcpy((void *)CONFIG_ARMV7_SECURE_BASE, __secure_start, sz);
60*da91cfedSStefan Agner
61f510aeaeSMarc Zyngier flush_dcache_range(CONFIG_ARMV7_SECURE_BASE,
62*da91cfedSStefan Agner CONFIG_ARMV7_SECURE_BASE + szflush);
6373169874SIan Campbell protect_secure_section();
64f510aeaeSMarc Zyngier invalidate_icache_all();
65f510aeaeSMarc Zyngier #endif
66f510aeaeSMarc Zyngier }
67f510aeaeSMarc Zyngier
kick_secondary_cpus_gic(unsigned long gicdaddr)68ba6a1698SAndre Przywara static void kick_secondary_cpus_gic(unsigned long gicdaddr)
69ba6a1698SAndre Przywara {
70ba6a1698SAndre Przywara /* kick all CPUs (except this one) by writing to GICD_SGIR */
71ba6a1698SAndre Przywara writel(1U << 24, gicdaddr + GICD_SGIR);
72ba6a1698SAndre Przywara }
73ba6a1698SAndre Przywara
smp_kick_all_cpus(void)74ba6a1698SAndre Przywara void __weak smp_kick_all_cpus(void)
75ba6a1698SAndre Przywara {
7656992743Stang yuantian unsigned long gic_dist_addr;
7756992743Stang yuantian
7856992743Stang yuantian gic_dist_addr = get_gicd_base_address();
7956992743Stang yuantian if (gic_dist_addr == -1)
8056992743Stang yuantian return;
8156992743Stang yuantian
82ba6a1698SAndre Przywara kick_secondary_cpus_gic(gic_dist_addr);
83ba6a1698SAndre Przywara }
84ba6a1698SAndre Przywara
psci_board_init(void)85ce416facSJan Kiszka __weak void psci_board_init(void)
86ce416facSJan Kiszka {
87ce416facSJan Kiszka }
88ce416facSJan Kiszka
armv7_init_nonsec(void)89f510aeaeSMarc Zyngier int armv7_init_nonsec(void)
901ef92385SAndre Przywara {
911ef92385SAndre Przywara unsigned int reg;
921ef92385SAndre Przywara unsigned itlinesnr, i;
9356992743Stang yuantian unsigned long gic_dist_addr;
941ef92385SAndre Przywara
951ef92385SAndre Przywara /* check whether the CPU supports the security extensions */
961ef92385SAndre Przywara reg = read_id_pfr1();
971ef92385SAndre Przywara if ((reg & 0xF0) == 0) {
981ef92385SAndre Przywara printf("nonsec: Security extensions not implemented.\n");
991ef92385SAndre Przywara return -1;
1001ef92385SAndre Przywara }
1011ef92385SAndre Przywara
1021ef92385SAndre Przywara /* the SCR register will be set directly in the monitor mode handler,
1031ef92385SAndre Przywara * according to the spec one should not tinker with it in secure state
1041ef92385SAndre Przywara * in SVC mode. Do not try to read it once in non-secure state,
1051ef92385SAndre Przywara * any access to it will trap.
1061ef92385SAndre Przywara */
1071ef92385SAndre Przywara
1081ef92385SAndre Przywara gic_dist_addr = get_gicd_base_address();
1091ef92385SAndre Przywara if (gic_dist_addr == -1)
1101ef92385SAndre Przywara return -1;
1111ef92385SAndre Przywara
1121ef92385SAndre Przywara /* enable the GIC distributor */
1131ef92385SAndre Przywara writel(readl(gic_dist_addr + GICD_CTLR) | 0x03,
1141ef92385SAndre Przywara gic_dist_addr + GICD_CTLR);
1151ef92385SAndre Przywara
1161ef92385SAndre Przywara /* TYPER[4:0] contains an encoded number of available interrupts */
1171ef92385SAndre Przywara itlinesnr = readl(gic_dist_addr + GICD_TYPER) & 0x1f;
1181ef92385SAndre Przywara
1191ef92385SAndre Przywara /* set all bits in the GIC group registers to one to allow access
1201ef92385SAndre Przywara * from non-secure state. The first 32 interrupts are private per
1211ef92385SAndre Przywara * CPU and will be set later when enabling the GIC for each core
1221ef92385SAndre Przywara */
1231ef92385SAndre Przywara for (i = 1; i <= itlinesnr; i++)
1241ef92385SAndre Przywara writel((unsigned)-1, gic_dist_addr + GICD_IGROUPRn + 4 * i);
1251ef92385SAndre Przywara
126ce416facSJan Kiszka psci_board_init();
127ce416facSJan Kiszka
12802251eefSPeng Fan /*
12902251eefSPeng Fan * Relocate secure section before any cpu runs in secure ram.
13002251eefSPeng Fan * smp_kick_all_cpus may enable other cores and runs into secure
13102251eefSPeng Fan * ram, so need to relocate secure section before enabling other
13202251eefSPeng Fan * cores.
13302251eefSPeng Fan */
13402251eefSPeng Fan relocate_secure_section();
13502251eefSPeng Fan
136f510aeaeSMarc Zyngier #ifndef CONFIG_ARMV7_PSCI
137f510aeaeSMarc Zyngier smp_set_core_boot_addr((unsigned long)secure_ram_addr(_smp_pen), -1);
138ba6a1698SAndre Przywara smp_kick_all_cpus();
139f510aeaeSMarc Zyngier #endif
140ba6a1698SAndre Przywara
141ba6a1698SAndre Przywara /* call the non-sec switching code on this CPU also */
142f510aeaeSMarc Zyngier secure_ram_addr(_nonsec_init)();
1431ef92385SAndre Przywara return 0;
1441ef92385SAndre Przywara }
145