xref: /rk3399_rockchip-uboot/arch/arm/cpu/armv7/nonsec_virt.S (revision d53ecad92f06d2e38a5cbc13af7473867c7fa277)
145b940d6SAndre Przywara/*
2d4296887SAndre Przywara * code for switching cores into non-secure state and into HYP mode
345b940d6SAndre Przywara *
445b940d6SAndre Przywara * Copyright (c) 2013	Andre Przywara <andre.przywara@linaro.org>
545b940d6SAndre Przywara *
6f833e790SAndre Przywara * SPDX-License-Identifier:	GPL-2.0+
745b940d6SAndre Przywara */
845b940d6SAndre Przywara
945b940d6SAndre Przywara#include <config.h>
1016212b59SAndre Przywara#include <linux/linkage.h>
1116212b59SAndre Przywara#include <asm/gic.h>
1216212b59SAndre Przywara#include <asm/armv7.h>
13f510aeaeSMarc Zyngier#include <asm/proc-armv/ptrace.h>
1416212b59SAndre Przywara
1516212b59SAndre Przywara.arch_extension sec
16d4296887SAndre Przywara.arch_extension virt
1745b940d6SAndre Przywara
18f510aeaeSMarc Zyngier	.pushsection ._secure.text, "ax"
19f510aeaeSMarc Zyngier
203064d599SMasahiro Yamada	.align	5
21d4296887SAndre Przywara/* the vector table for secure state and HYP mode */
2245b940d6SAndre Przywara_monitor_vectors:
2345b940d6SAndre Przywara	.word 0	/* reset */
2445b940d6SAndre Przywara	.word 0 /* undef */
2545b940d6SAndre Przywara	adr pc, _secure_monitor
2645b940d6SAndre Przywara	.word 0
2745b940d6SAndre Przywara	.word 0
2845b940d6SAndre Przywara	.word 0
2945b940d6SAndre Przywara	.word 0
30f510aeaeSMarc Zyngier	.word 0
31f510aeaeSMarc Zyngier
32f510aeaeSMarc Zyngier.macro is_cpu_virt_capable	tmp
33f510aeaeSMarc Zyngier	mrc	p15, 0, \tmp, c0, c1, 1		@ read ID_PFR1
34f510aeaeSMarc Zyngier	and	\tmp, \tmp, #CPUID_ARM_VIRT_MASK	@ mask virtualization bits
35f510aeaeSMarc Zyngier	cmp	\tmp, #(1 << CPUID_ARM_VIRT_SHIFT)
36f510aeaeSMarc Zyngier.endm
3745b940d6SAndre Przywara
3845b940d6SAndre Przywara/*
3945b940d6SAndre Przywara * secure monitor handler
40a187559eSBin Meng * U-Boot calls this "software interrupt" in start.S
4145b940d6SAndre Przywara * This is executed on a "smc" instruction, we use a "smc #0" to switch
4245b940d6SAndre Przywara * to non-secure state.
43f510aeaeSMarc Zyngier * r0, r1, r2: passed to the callee
44f510aeaeSMarc Zyngier * ip: target PC
4545b940d6SAndre Przywara */
4645b940d6SAndre Przywara_secure_monitor:
4738510a4bSMarc Zyngier#ifdef CONFIG_ARMV7_PSCI
4838510a4bSMarc Zyngier	ldr	r5, =_psci_vectors		@ Switch to the next monitor
4938510a4bSMarc Zyngier	mcr	p15, 0, r5, c12, c0, 1
5038510a4bSMarc Zyngier	isb
5145b940d6SAndre Przywara
52b5281323SChen-Yu Tsai	@ Obtain a secure stack
53b5281323SChen-Yu Tsai	bl	psci_stack_setup
54b5281323SChen-Yu Tsai
55b5281323SChen-Yu Tsai	@ Configure the PSCI backend
56b5281323SChen-Yu Tsai	push	{r0, r1, r2, ip}
5738510a4bSMarc Zyngier	bl	psci_arch_init
58b5281323SChen-Yu Tsai	pop	{r0, r1, r2, ip}
5938510a4bSMarc Zyngier#endif
6038510a4bSMarc Zyngier
61e392b923SIan Campbell#ifdef CONFIG_ARM_ERRATA_773022
62e392b923SIan Campbell	mrc	p15, 0, r5, c1, c0, 1
63e392b923SIan Campbell	orr	r5, r5, #(1 << 1)
64e392b923SIan Campbell	mcr	p15, 0, r5, c1, c0, 1
65e392b923SIan Campbell	isb
66e392b923SIan Campbell#endif
67e392b923SIan Campbell
68e392b923SIan Campbell#ifdef CONFIG_ARM_ERRATA_774769
69e392b923SIan Campbell	mrc	p15, 0, r5, c1, c0, 1
70e392b923SIan Campbell	orr	r5, r5, #(1 << 25)
71e392b923SIan Campbell	mcr	p15, 0, r5, c1, c0, 1
72e392b923SIan Campbell	isb
73e392b923SIan Campbell#endif
74e392b923SIan Campbell
7538510a4bSMarc Zyngier	mrc	p15, 0, r5, c1, c1, 0		@ read SCR
7638510a4bSMarc Zyngier	bic	r5, r5, #0x4a			@ clear IRQ, EA, nET bits
7738510a4bSMarc Zyngier	orr	r5, r5, #0x31			@ enable NS, AW, FW bits
7838510a4bSMarc Zyngier						@ FIQ preserved for secure mode
79f510aeaeSMarc Zyngier	mov	r6, #SVC_MODE			@ default mode is SVC
80f510aeaeSMarc Zyngier	is_cpu_virt_capable r4
8164fd44dcSMarc Zyngier#ifdef CONFIG_ARMV7_VIRT
82f510aeaeSMarc Zyngier	orreq	r5, r5, #0x100			@ allow HVC instruction
83f510aeaeSMarc Zyngier	moveq	r6, #HYP_MODE			@ Enter the kernel as HYP
84d4296887SAndre Przywara#endif
85d4296887SAndre Przywara
86f510aeaeSMarc Zyngier	mcr	p15, 0, r5, c1, c1, 0		@ write SCR (with NS bit set)
87800c8352SMarc Zyngier	isb
8845b940d6SAndre Przywara
8964fd44dcSMarc Zyngier	bne	1f
90d4296887SAndre Przywara
9164fd44dcSMarc Zyngier	@ Reset CNTVOFF to 0 before leaving monitor mode
92f510aeaeSMarc Zyngier	mrc	p15, 0, r4, c0, c1, 1		@ read ID_PFR1
93f510aeaeSMarc Zyngier	ands	r4, r4, #CPUID_ARM_GENTIMER_MASK	@ test arch timer bits
94f510aeaeSMarc Zyngier	movne	r4, #0
95f510aeaeSMarc Zyngier	mcrrne	p15, 4, r4, r4, c14		@ Reset CNTVOFF to zero
9664fd44dcSMarc Zyngier1:
97f510aeaeSMarc Zyngier	mov	lr, ip
98f510aeaeSMarc Zyngier	mov	ip, #(F_BIT | I_BIT | A_BIT)	@ Set A, I and F
99f510aeaeSMarc Zyngier	tst	lr, #1				@ Check for Thumb PC
100f510aeaeSMarc Zyngier	orrne	ip, ip, #T_BIT			@ Set T if Thumb
101f510aeaeSMarc Zyngier	orr	ip, ip, r6			@ Slot target mode in
102f510aeaeSMarc Zyngier	msr	spsr_cxfs, ip			@ Set full SPSR
103f510aeaeSMarc Zyngier	movs	pc, lr				@ ERET to non-secure
10416212b59SAndre Przywara
105f510aeaeSMarc ZyngierENTRY(_do_nonsec_entry)
106f510aeaeSMarc Zyngier	mov	ip, r0
107f510aeaeSMarc Zyngier	mov	r0, r1
108f510aeaeSMarc Zyngier	mov	r1, r2
109f510aeaeSMarc Zyngier	mov	r2, r3
110f510aeaeSMarc Zyngier	smc	#0
111f510aeaeSMarc ZyngierENDPROC(_do_nonsec_entry)
112d4296887SAndre Przywara
113f510aeaeSMarc Zyngier.macro get_cbar_addr	addr
114f510aeaeSMarc Zyngier#ifdef CONFIG_ARM_GIC_BASE_ADDRESS
115f510aeaeSMarc Zyngier	ldr	\addr, =CONFIG_ARM_GIC_BASE_ADDRESS
116f510aeaeSMarc Zyngier#else
117f510aeaeSMarc Zyngier	mrc	p15, 4, \addr, c15, c0, 0	@ read CBAR
118f510aeaeSMarc Zyngier	bfc	\addr, #0, #15			@ clear reserved bits
119f510aeaeSMarc Zyngier#endif
120f510aeaeSMarc Zyngier.endm
121f510aeaeSMarc Zyngier
122f510aeaeSMarc Zyngier.macro get_gicd_addr	addr
123f510aeaeSMarc Zyngier	get_cbar_addr	\addr
124f510aeaeSMarc Zyngier	add	\addr, \addr, #GIC_DIST_OFFSET	@ GIC dist i/f offset
125f510aeaeSMarc Zyngier.endm
126f510aeaeSMarc Zyngier
127f510aeaeSMarc Zyngier.macro get_gicc_addr	addr, tmp
128f510aeaeSMarc Zyngier	get_cbar_addr	\addr
129f510aeaeSMarc Zyngier	is_cpu_virt_capable \tmp
130f510aeaeSMarc Zyngier	movne	\tmp, #GIC_CPU_OFFSET_A9	@ GIC CPU offset for A9
131f510aeaeSMarc Zyngier	moveq	\tmp, #GIC_CPU_OFFSET_A15	@ GIC CPU offset for A15/A7
132f510aeaeSMarc Zyngier	add	\addr, \addr, \tmp
133f510aeaeSMarc Zyngier.endm
134f510aeaeSMarc Zyngier
135f510aeaeSMarc Zyngier#ifndef CONFIG_ARMV7_PSCI
13616212b59SAndre Przywara/*
137ba6a1698SAndre Przywara * Secondary CPUs start here and call the code for the core specific parts
138ba6a1698SAndre Przywara * of the non-secure and HYP mode transition. The GIC distributor specific
139ba6a1698SAndre Przywara * code has already been executed by a C function before.
140ba6a1698SAndre Przywara * Then they go back to wfi and wait to be woken up by the kernel again.
141ba6a1698SAndre Przywara */
142ba6a1698SAndre PrzywaraENTRY(_smp_pen)
143f510aeaeSMarc Zyngier	cpsid	i
144f510aeaeSMarc Zyngier	cpsid	f
145ba6a1698SAndre Przywara
146ba6a1698SAndre Przywara	bl	_nonsec_init
147ba6a1698SAndre Przywara
148ba6a1698SAndre Przywara	adr	r0, _smp_pen			@ do not use this address again
149ba6a1698SAndre Przywara	b	smp_waitloop			@ wait for IPIs, board specific
150ba6a1698SAndre PrzywaraENDPROC(_smp_pen)
151f510aeaeSMarc Zyngier#endif
152ba6a1698SAndre Przywara
153ba6a1698SAndre Przywara/*
15416212b59SAndre Przywara * Switch a core to non-secure state.
15516212b59SAndre Przywara *
15616212b59SAndre Przywara *  1. initialize the GIC per-core interface
15716212b59SAndre Przywara *  2. allow coprocessor access in non-secure modes
15816212b59SAndre Przywara *
15916212b59SAndre Przywara * Called from smp_pen by secondary cores and directly by the BSP.
16016212b59SAndre Przywara * Do not assume that the stack is available and only use registers
16116212b59SAndre Przywara * r0-r3 and r12.
16216212b59SAndre Przywara *
16316212b59SAndre Przywara * PERIPHBASE is used to get the GIC address. This could be 40 bits long,
16416212b59SAndre Przywara * though, but we check this in C before calling this function.
16516212b59SAndre Przywara */
16616212b59SAndre PrzywaraENTRY(_nonsec_init)
167f510aeaeSMarc Zyngier	get_gicd_addr	r3
168f510aeaeSMarc Zyngier
16916212b59SAndre Przywara	mvn	r1, #0				@ all bits to 1
17016212b59SAndre Przywara	str	r1, [r3, #GICD_IGROUPRn]	@ allow private interrupts
17116212b59SAndre Przywara
172f510aeaeSMarc Zyngier	get_gicc_addr	r3, r1
17316212b59SAndre Przywara
174f510aeaeSMarc Zyngier	mov	r1, #3				@ Enable both groups
17516212b59SAndre Przywara	str	r1, [r3, #GICC_CTLR]		@ and clear all other bits
17616212b59SAndre Przywara	mov	r1, #0xff
17716212b59SAndre Przywara	str	r1, [r3, #GICC_PMR]		@ set priority mask register
17816212b59SAndre Przywara
179f510aeaeSMarc Zyngier	mrc	p15, 0, r0, c1, c1, 2
18016212b59SAndre Przywara	movw	r1, #0x3fff
181f510aeaeSMarc Zyngier	movt	r1, #0x0004
182f510aeaeSMarc Zyngier	orr	r0, r0, r1
183f510aeaeSMarc Zyngier	mcr	p15, 0, r0, c1, c1, 2		@ NSACR = all copros to non-sec
18416212b59SAndre Przywara
18516212b59SAndre Przywara/* The CNTFRQ register of the generic timer needs to be
18616212b59SAndre Przywara * programmed in secure state. Some primary bootloaders / firmware
18716212b59SAndre Przywara * omit this, so if the frequency is provided in the configuration,
18816212b59SAndre Przywara * we do this here instead.
18916212b59SAndre Przywara * But first check if we have the generic timer.
19016212b59SAndre Przywara */
191*e4916e85SAndre Przywara#ifdef COUNTER_FREQUENCY
19216212b59SAndre Przywara	mrc	p15, 0, r0, c0, c1, 1		@ read ID_PFR1
19316212b59SAndre Przywara	and	r0, r0, #CPUID_ARM_GENTIMER_MASK	@ mask arch timer bits
19416212b59SAndre Przywara	cmp	r0, #(1 << CPUID_ARM_GENTIMER_SHIFT)
195*e4916e85SAndre Przywara	ldreq	r1, =COUNTER_FREQUENCY
19616212b59SAndre Przywara	mcreq	p15, 0, r1, c14, c0, 0		@ write CNTFRQ
19716212b59SAndre Przywara#endif
19816212b59SAndre Przywara
19916212b59SAndre Przywara	adr	r1, _monitor_vectors
20016212b59SAndre Przywara	mcr	p15, 0, r1, c12, c0, 1		@ set MVBAR to secure vectors
20116212b59SAndre Przywara	isb
20216212b59SAndre Przywara
20316212b59SAndre Przywara	mov	r0, r3				@ return GICC address
20416212b59SAndre Przywara	bx	lr
20516212b59SAndre PrzywaraENDPROC(_nonsec_init)
206ba6a1698SAndre Przywara
207ba6a1698SAndre Przywara#ifdef CONFIG_SMP_PEN_ADDR
208ba6a1698SAndre Przywara/* void __weak smp_waitloop(unsigned previous_address); */
209ba6a1698SAndre PrzywaraENTRY(smp_waitloop)
210ba6a1698SAndre Przywara	wfi
211ba6a1698SAndre Przywara	ldr	r1, =CONFIG_SMP_PEN_ADDR	@ load start address
212ba6a1698SAndre Przywara	ldr	r1, [r1]
213b8e5c7f9SXiubo Li#ifdef CONFIG_PEN_ADDR_BIG_ENDIAN
214b8e5c7f9SXiubo Li	rev	r1, r1
215b8e5c7f9SXiubo Li#endif
216ba6a1698SAndre Przywara	cmp	r0, r1			@ make sure we dont execute this code
217ba6a1698SAndre Przywara	beq	smp_waitloop		@ again (due to a spurious wakeup)
218f510aeaeSMarc Zyngier	mov	r0, r1
219f510aeaeSMarc Zyngier	b	_do_nonsec_entry
220ba6a1698SAndre PrzywaraENDPROC(smp_waitloop)
221ba6a1698SAndre Przywara.weak smp_waitloop
222ba6a1698SAndre Przywara#endif
223d4296887SAndre Przywara
224f510aeaeSMarc Zyngier	.popsection
225