1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
21ef92385SAndre Przywara /*
31ef92385SAndre Przywara * (C) Copyright 2013
4f833e790SAndre Przywara * Andre Przywara, Linaro <andre.przywara@linaro.org>
51ef92385SAndre Przywara *
61ef92385SAndre Przywara * Routines to transition ARMv7 processors from secure into non-secure state
7d4296887SAndre Przywara * and from non-secure SVC into HYP mode
81ef92385SAndre Przywara * needed to enable ARMv7 virtualization for current hypervisors
91ef92385SAndre Przywara */
101ef92385SAndre Przywara
111ef92385SAndre Przywara #include <common.h>
121ef92385SAndre Przywara #include <asm/armv7.h>
131ef92385SAndre Przywara #include <asm/gic.h>
141ef92385SAndre Przywara #include <asm/io.h>
15f510aeaeSMarc Zyngier #include <asm/secure.h>
161ef92385SAndre Przywara
read_id_pfr1(void)171ef92385SAndre Przywara static unsigned int read_id_pfr1(void)
181ef92385SAndre Przywara {
191ef92385SAndre Przywara unsigned int reg;
201ef92385SAndre Przywara
211ef92385SAndre Przywara asm("mrc p15, 0, %0, c0, c1, 1\n" : "=r"(reg));
221ef92385SAndre Przywara return reg;
231ef92385SAndre Przywara }
241ef92385SAndre Przywara
get_gicd_base_address(void)251ef92385SAndre Przywara static unsigned long get_gicd_base_address(void)
261ef92385SAndre Przywara {
271ef92385SAndre Przywara #ifdef CONFIG_ARM_GIC_BASE_ADDRESS
281ef92385SAndre Przywara return CONFIG_ARM_GIC_BASE_ADDRESS + GIC_DIST_OFFSET;
291ef92385SAndre Przywara #else
301ef92385SAndre Przywara unsigned periphbase;
311ef92385SAndre Przywara
321ef92385SAndre Przywara /* get the GIC base address from the CBAR register */
331ef92385SAndre Przywara asm("mrc p15, 4, %0, c15, c0, 0\n" : "=r" (periphbase));
341ef92385SAndre Przywara
351ef92385SAndre Przywara /* the PERIPHBASE can be mapped above 4 GB (lower 8 bits used to
361ef92385SAndre Przywara * encode this). Bail out here since we cannot access this without
371ef92385SAndre Przywara * enabling paging.
381ef92385SAndre Przywara */
391ef92385SAndre Przywara if ((periphbase & 0xff) != 0) {
401ef92385SAndre Przywara printf("nonsec: PERIPHBASE is above 4 GB, no access.\n");
411ef92385SAndre Przywara return -1;
421ef92385SAndre Przywara }
431ef92385SAndre Przywara
441ef92385SAndre Przywara return (periphbase & CBAR_MASK) + GIC_DIST_OFFSET;
451ef92385SAndre Przywara #endif
461ef92385SAndre Przywara }
471ef92385SAndre Przywara
4873169874SIan Campbell /* Define a specific version of this function to enable any available
4973169874SIan Campbell * hardware protections for the reserved region */
protect_secure_section(void)5073169874SIan Campbell void __weak protect_secure_section(void) {}
5173169874SIan Campbell
relocate_secure_section(void)52f510aeaeSMarc Zyngier static void relocate_secure_section(void)
53f510aeaeSMarc Zyngier {
54f510aeaeSMarc Zyngier #ifdef CONFIG_ARMV7_SECURE_BASE
55f510aeaeSMarc Zyngier size_t sz = __secure_end - __secure_start;
56da91cfedSStefan Agner unsigned long szflush = ALIGN(sz + 1, CONFIG_SYS_CACHELINE_SIZE);
57f510aeaeSMarc Zyngier
58f510aeaeSMarc Zyngier memcpy((void *)CONFIG_ARMV7_SECURE_BASE, __secure_start, sz);
59da91cfedSStefan Agner
60f510aeaeSMarc Zyngier flush_dcache_range(CONFIG_ARMV7_SECURE_BASE,
61da91cfedSStefan Agner CONFIG_ARMV7_SECURE_BASE + szflush);
6273169874SIan Campbell protect_secure_section();
63f510aeaeSMarc Zyngier invalidate_icache_all();
64f510aeaeSMarc Zyngier #endif
65f510aeaeSMarc Zyngier }
66f510aeaeSMarc Zyngier
kick_secondary_cpus_gic(unsigned long gicdaddr)67ba6a1698SAndre Przywara static void kick_secondary_cpus_gic(unsigned long gicdaddr)
68ba6a1698SAndre Przywara {
69ba6a1698SAndre Przywara /* kick all CPUs (except this one) by writing to GICD_SGIR */
70ba6a1698SAndre Przywara writel(1U << 24, gicdaddr + GICD_SGIR);
71ba6a1698SAndre Przywara }
72ba6a1698SAndre Przywara
smp_kick_all_cpus(void)73ba6a1698SAndre Przywara void __weak smp_kick_all_cpus(void)
74ba6a1698SAndre Przywara {
7556992743Stang yuantian unsigned long gic_dist_addr;
7656992743Stang yuantian
7756992743Stang yuantian gic_dist_addr = get_gicd_base_address();
7856992743Stang yuantian if (gic_dist_addr == -1)
7956992743Stang yuantian return;
8056992743Stang yuantian
81ba6a1698SAndre Przywara kick_secondary_cpus_gic(gic_dist_addr);
82ba6a1698SAndre Przywara }
83ba6a1698SAndre Przywara
psci_board_init(void)84ce416facSJan Kiszka __weak void psci_board_init(void)
85ce416facSJan Kiszka {
86ce416facSJan Kiszka }
87ce416facSJan Kiszka
armv7_init_nonsec(void)88f510aeaeSMarc Zyngier int armv7_init_nonsec(void)
891ef92385SAndre Przywara {
901ef92385SAndre Przywara unsigned int reg;
911ef92385SAndre Przywara unsigned itlinesnr, i;
9256992743Stang yuantian unsigned long gic_dist_addr;
931ef92385SAndre Przywara
941ef92385SAndre Przywara /* check whether the CPU supports the security extensions */
951ef92385SAndre Przywara reg = read_id_pfr1();
961ef92385SAndre Przywara if ((reg & 0xF0) == 0) {
971ef92385SAndre Przywara printf("nonsec: Security extensions not implemented.\n");
981ef92385SAndre Przywara return -1;
991ef92385SAndre Przywara }
1001ef92385SAndre Przywara
1011ef92385SAndre Przywara /* the SCR register will be set directly in the monitor mode handler,
1021ef92385SAndre Przywara * according to the spec one should not tinker with it in secure state
1031ef92385SAndre Przywara * in SVC mode. Do not try to read it once in non-secure state,
1041ef92385SAndre Przywara * any access to it will trap.
1051ef92385SAndre Przywara */
1061ef92385SAndre Przywara
1071ef92385SAndre Przywara gic_dist_addr = get_gicd_base_address();
1081ef92385SAndre Przywara if (gic_dist_addr == -1)
1091ef92385SAndre Przywara return -1;
1101ef92385SAndre Przywara
1111ef92385SAndre Przywara /* enable the GIC distributor */
1121ef92385SAndre Przywara writel(readl(gic_dist_addr + GICD_CTLR) | 0x03,
1131ef92385SAndre Przywara gic_dist_addr + GICD_CTLR);
1141ef92385SAndre Przywara
1151ef92385SAndre Przywara /* TYPER[4:0] contains an encoded number of available interrupts */
1161ef92385SAndre Przywara itlinesnr = readl(gic_dist_addr + GICD_TYPER) & 0x1f;
1171ef92385SAndre Przywara
1181ef92385SAndre Przywara /* set all bits in the GIC group registers to one to allow access
1191ef92385SAndre Przywara * from non-secure state. The first 32 interrupts are private per
1201ef92385SAndre Przywara * CPU and will be set later when enabling the GIC for each core
1211ef92385SAndre Przywara */
1221ef92385SAndre Przywara for (i = 1; i <= itlinesnr; i++)
1231ef92385SAndre Przywara writel((unsigned)-1, gic_dist_addr + GICD_IGROUPRn + 4 * i);
1241ef92385SAndre Przywara
125ce416facSJan Kiszka psci_board_init();
126ce416facSJan Kiszka
12702251eefSPeng Fan /*
12802251eefSPeng Fan * Relocate secure section before any cpu runs in secure ram.
12902251eefSPeng Fan * smp_kick_all_cpus may enable other cores and runs into secure
13002251eefSPeng Fan * ram, so need to relocate secure section before enabling other
13102251eefSPeng Fan * cores.
13202251eefSPeng Fan */
13302251eefSPeng Fan relocate_secure_section();
13402251eefSPeng Fan
135f510aeaeSMarc Zyngier #ifndef CONFIG_ARMV7_PSCI
136f510aeaeSMarc Zyngier smp_set_core_boot_addr((unsigned long)secure_ram_addr(_smp_pen), -1);
137ba6a1698SAndre Przywara smp_kick_all_cpus();
138f510aeaeSMarc Zyngier #endif
139ba6a1698SAndre Przywara
140ba6a1698SAndre Przywara /* call the non-sec switching code on this CPU also */
141f510aeaeSMarc Zyngier secure_ram_addr(_nonsec_init)();
1421ef92385SAndre Przywara return 0;
1431ef92385SAndre Przywara }
144