3 * Andre Przywara, Linaro
5 * Routines to transition ARMv7 processors from secure into non-secure state
6 * and from non-secure SVC into HYP mode
7 * needed to enable ARMv7 virtualization for current hypervisors
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/armv7.h>
33 unsigned long gic_dist_addr;
35 static unsigned int read_cpsr(void)
39 asm volatile ("mrs %0, cpsr\n" : "=r" (reg));
43 static unsigned int read_id_pfr1(void)
47 asm("mrc p15, 0, %0, c0, c1, 1\n" : "=r"(reg));
51 static unsigned long get_gicd_base_address(void)
53 #ifdef CONFIG_ARM_GIC_BASE_ADDRESS
54 return CONFIG_ARM_GIC_BASE_ADDRESS + GIC_DIST_OFFSET;
59 /* check whether we are an Cortex-A15 or A7.
60 * The actual HYP switch should work with all CPUs supporting
61 * the virtualization extension, but we need the GIC address,
62 * which we know only for sure for those two CPUs.
64 asm("mrc p15, 0, %0, c0, c0, 0\n" : "=r"(midr));
65 switch (midr & MIDR_PRIMARY_PART_MASK) {
66 case MIDR_CORTEX_A9_R0P1:
67 case MIDR_CORTEX_A15_R0P0:
68 case MIDR_CORTEX_A7_R0P0:
71 printf("nonsec: could not determine GIC address.\n");
75 /* get the GIC base address from the CBAR register */
76 asm("mrc p15, 4, %0, c15, c0, 0\n" : "=r" (periphbase));
78 /* the PERIPHBASE can be mapped above 4 GB (lower 8 bits used to
79 * encode this). Bail out here since we cannot access this without
82 if ((periphbase & 0xff) != 0) {
83 printf("nonsec: PERIPHBASE is above 4 GB, no access.\n");
87 return (periphbase & CBAR_MASK) + GIC_DIST_OFFSET;
91 static void kick_secondary_cpus_gic(unsigned long gicdaddr)
93 /* kick all CPUs (except this one) by writing to GICD_SGIR */
94 writel(1U << 24, gicdaddr + GICD_SGIR);
97 void __weak smp_kick_all_cpus(void)
99 kick_secondary_cpus_gic(gic_dist_addr);
102 int armv7_switch_hyp(void)
106 /* check whether we are in HYP mode already */
107 if ((read_cpsr() & 0x1f) == 0x1a) {
108 debug("CPU already in HYP mode\n");
112 /* check whether the CPU supports the virtualization extensions */
113 reg = read_id_pfr1();
114 if ((reg & CPUID_ARM_VIRT_MASK) != 1 << CPUID_ARM_VIRT_SHIFT) {
115 printf("HYP mode: Virtualization extensions not implemented.\n");
119 /* call the HYP switching code on this CPU also */
122 if ((read_cpsr() & 0x1F) != 0x1a) {
123 printf("HYP mode: switch not successful.\n");
130 int armv7_switch_nonsec(void)
133 unsigned itlinesnr, i;
135 /* check whether the CPU supports the security extensions */
136 reg = read_id_pfr1();
137 if ((reg & 0xF0) == 0) {
138 printf("nonsec: Security extensions not implemented.\n");
142 /* the SCR register will be set directly in the monitor mode handler,
143 * according to the spec one should not tinker with it in secure state
144 * in SVC mode. Do not try to read it once in non-secure state,
145 * any access to it will trap.
148 gic_dist_addr = get_gicd_base_address();
149 if (gic_dist_addr == -1)
152 /* enable the GIC distributor */
153 writel(readl(gic_dist_addr + GICD_CTLR) | 0x03,
154 gic_dist_addr + GICD_CTLR);
156 /* TYPER[4:0] contains an encoded number of available interrupts */
157 itlinesnr = readl(gic_dist_addr + GICD_TYPER) & 0x1f;
159 /* set all bits in the GIC group registers to one to allow access
160 * from non-secure state. The first 32 interrupts are private per
161 * CPU and will be set later when enabling the GIC for each core
163 for (i = 1; i <= itlinesnr; i++)
164 writel((unsigned)-1, gic_dist_addr + GICD_IGROUPRn + 4 * i);
166 smp_set_core_boot_addr((unsigned long)_smp_pen, -1);
169 /* call the non-sec switching code on this CPU also */