blob: acd519020d7f45dd590be610107b8e5b4a30c928 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Jean-Christophe PLAGNIOL-VILLARD06a819c2009-06-13 20:50:02 +02002/*
3 * include/asm-arm/macro.h
4 *
5 * Copyright (C) 2009 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Jean-Christophe PLAGNIOL-VILLARD06a819c2009-06-13 20:50:02 +02006 */
7
8#ifndef __ASM_ARM_MACRO_H__
9#define __ASM_ARM_MACRO_H__
Alison Wang73818d52016-11-10 10:49:03 +080010
11#ifdef CONFIG_ARM64
12#include <asm/system.h>
13#endif
14
Jean-Christophe PLAGNIOL-VILLARD06a819c2009-06-13 20:50:02 +020015#ifdef __ASSEMBLY__
16
17/*
18 * These macros provide a convenient way to write 8, 16 and 32 bit data
19 * to any address.
20 * Registers r4 and r5 are used, any data in these registers are
21 * overwritten by the macros.
22 * The macros are valid for any ARM architecture, they do not implement
23 * any memory barriers so caution is recommended when using these when the
24 * caches are enabled or on a multi-core system.
25 */
26
27.macro write32, addr, data
28 ldr r4, =\addr
29 ldr r5, =\data
30 str r5, [r4]
31.endm
32
33.macro write16, addr, data
34 ldr r4, =\addr
35 ldrh r5, =\data
36 strh r5, [r4]
37.endm
38
39.macro write8, addr, data
40 ldr r4, =\addr
41 ldrb r5, =\data
42 strb r5, [r4]
43.endm
44
45/*
46 * This macro generates a loop that can be used for delays in the code.
47 * Register r4 is used, any data in this register is overwritten by the
48 * macro.
49 * The macro is valid for any ARM architeture. The actual time spent in the
50 * loop will vary from CPU to CPU though.
51 */
52
53.macro wait_timer, time
54 ldr r4, =\time
551:
56 nop
57 subs r4, r4, #1
58 bcs 1b
59.endm
60
David Feng85fd5f12013-12-14 11:47:35 +080061#ifdef CONFIG_ARM64
62/*
63 * Register aliases.
64 */
65lr .req x30
66
67/*
68 * Branch according to exception level
69 */
70.macro switch_el, xreg, el3_label, el2_label, el1_label
71 mrs \xreg, CurrentEL
Andre Przywarafc215c72022-02-11 11:29:38 +000072 cmp \xreg, #0x8
73 b.gt \el3_label
David Feng85fd5f12013-12-14 11:47:35 +080074 b.eq \el2_label
Andre Przywarafc215c72022-02-11 11:29:38 +000075 b.lt \el1_label
David Feng85fd5f12013-12-14 11:47:35 +080076.endm
77
78/*
Peter Hoyes55262102021-07-12 15:04:21 +010079 * Branch if we are not in the highest exception level
80 */
81.macro branch_if_not_highest_el, xreg, label
82 switch_el \xreg, 3f, 2f, 1f
83
842: mrs \xreg, ID_AA64PFR0_EL1
85 and \xreg, \xreg, #(ID_AA64PFR0_EL1_EL3)
86 cbnz \xreg, \label
87 b 3f
88
891: mrs \xreg, ID_AA64PFR0_EL1
90 and \xreg, \xreg, #(ID_AA64PFR0_EL1_EL3 | ID_AA64PFR0_EL1_EL2)
91 cbnz \xreg, \label
92
933:
94.endm
95
96/*
Bhupesh Sharma80a7e352015-01-23 15:50:04 +053097 * Branch if current processor is a Cortex-A57 core.
98 */
99.macro branch_if_a57_core, xreg, a57_label
100 mrs \xreg, midr_el1
101 lsr \xreg, \xreg, #4
102 and \xreg, \xreg, #0x00000FFF
103 cmp \xreg, #0xD07 /* Cortex-A57 MPCore processor. */
104 b.eq \a57_label
105.endm
106
107/*
108 * Branch if current processor is a Cortex-A53 core.
109 */
110.macro branch_if_a53_core, xreg, a53_label
111 mrs \xreg, midr_el1
112 lsr \xreg, \xreg, #4
113 and \xreg, \xreg, #0x00000FFF
114 cmp \xreg, #0xD03 /* Cortex-A53 MPCore processor. */
115 b.eq \a53_label
116.endm
117
118/*
David Feng85fd5f12013-12-14 11:47:35 +0800119 * Branch if current processor is a slave,
120 * choose processor with all zero affinity value as the master.
121 */
122.macro branch_if_slave, xreg, slave_label
Linus Walleij74771392015-03-09 10:53:21 +0100123#ifdef CONFIG_ARMV8_MULTIENTRY
124 /* NOTE: MPIDR handling will be erroneous on multi-cluster machines */
David Feng85fd5f12013-12-14 11:47:35 +0800125 mrs \xreg, mpidr_el1
126 tst \xreg, #0xff /* Test Affinity 0 */
127 b.ne \slave_label
128 lsr \xreg, \xreg, #8
129 tst \xreg, #0xff /* Test Affinity 1 */
130 b.ne \slave_label
131 lsr \xreg, \xreg, #8
132 tst \xreg, #0xff /* Test Affinity 2 */
133 b.ne \slave_label
134 lsr \xreg, \xreg, #16
135 tst \xreg, #0xff /* Test Affinity 3 */
136 b.ne \slave_label
Linus Walleij74771392015-03-09 10:53:21 +0100137#endif
David Feng85fd5f12013-12-14 11:47:35 +0800138.endm
139
140/*
141 * Branch if current processor is a master,
142 * choose processor with all zero affinity value as the master.
143 */
144.macro branch_if_master, xreg1, xreg2, master_label
Linus Walleij74771392015-03-09 10:53:21 +0100145#ifdef CONFIG_ARMV8_MULTIENTRY
146 /* NOTE: MPIDR handling will be erroneous on multi-cluster machines */
David Feng85fd5f12013-12-14 11:47:35 +0800147 mrs \xreg1, mpidr_el1
148 lsr \xreg2, \xreg1, #32
zijun_huf489a4f2017-09-25 15:28:50 +0800149 lsl \xreg2, \xreg2, #32
David Feng85fd5f12013-12-14 11:47:35 +0800150 lsl \xreg1, \xreg1, #40
151 lsr \xreg1, \xreg1, #40
152 orr \xreg1, \xreg1, \xreg2
153 cbz \xreg1, \master_label
Linus Walleij74771392015-03-09 10:53:21 +0100154#else
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200155 b \master_label
Linus Walleij74771392015-03-09 10:53:21 +0100156#endif
David Feng85fd5f12013-12-14 11:47:35 +0800157.endm
158
Alison Wang73818d52016-11-10 10:49:03 +0800159/*
160 * Switch from EL3 to EL2 for ARMv8
161 * @ep: kernel entry point
162 * @flag: The execution state flag for lower exception
163 * level, ES_TO_AARCH64 or ES_TO_AARCH32
164 * @tmp: temporary register
165 *
166 * For loading 32-bit OS, x1 is machine nr and x2 is ftaddr.
167 * For loading 64-bit OS, x0 is physical address to the FDT blob.
168 * They will be passed to the guest.
169 */
170.macro armv8_switch_to_el2_m, ep, flag, tmp
York Sun56cc3db2014-09-08 12:20:00 -0700171 msr cptr_el3, xzr /* Disable coprocessor traps to EL3 */
Alison Wang73818d52016-11-10 10:49:03 +0800172 mov \tmp, #CPTR_EL2_RES1
173 msr cptr_el2, \tmp /* Disable coprocessor traps to EL2 */
York Sun56cc3db2014-09-08 12:20:00 -0700174
David Feng9e2ea5a2015-03-02 15:29:34 +0800175 /* Initialize Generic Timers */
176 msr cntvoff_el2, xzr
177
York Sun56cc3db2014-09-08 12:20:00 -0700178 /* Initialize SCTLR_EL2
179 *
180 * setting RES1 bits (29,28,23,22,18,16,11,5,4) to 1
181 * and RES0 bits (31,30,27,26,24,21,20,17,15-13,10-6) +
182 * EE,WXN,I,SA,C,A,M to 0
183 */
Alison Wang73818d52016-11-10 10:49:03 +0800184 ldr \tmp, =(SCTLR_EL2_RES1 | SCTLR_EL2_EE_LE |\
185 SCTLR_EL2_WXN_DIS | SCTLR_EL2_ICACHE_DIS |\
186 SCTLR_EL2_SA_DIS | SCTLR_EL2_DCACHE_DIS |\
187 SCTLR_EL2_ALIGN_DIS | SCTLR_EL2_MMU_DIS)
188 msr sctlr_el2, \tmp
189
190 mov \tmp, sp
191 msr sp_el2, \tmp /* Migrate SP */
192 mrs \tmp, vbar_el3
193 msr vbar_el2, \tmp /* Migrate VBAR */
194
195 /* Check switch to AArch64 EL2 or AArch32 Hypervisor mode */
196 cmp \flag, #ES_TO_AARCH32
197 b.eq 1f
198
199 /*
200 * The next lower exception level is AArch64, 64bit EL2 | HCE |
macro.wave.z@gmail.comd8f5af92016-12-08 11:58:23 +0800201 * RES1 (Bits[5:4]) | Non-secure EL0/EL1.
202 * and the SMD depends on requirements.
Alison Wang73818d52016-11-10 10:49:03 +0800203 */
macro.wave.z@gmail.comd8f5af92016-12-08 11:58:23 +0800204#ifdef CONFIG_ARMV8_PSCI
205 ldr \tmp, =(SCR_EL3_RW_AARCH64 | SCR_EL3_HCE_EN |\
206 SCR_EL3_RES1 | SCR_EL3_NS_EN)
207#else
Alison Wang73818d52016-11-10 10:49:03 +0800208 ldr \tmp, =(SCR_EL3_RW_AARCH64 | SCR_EL3_HCE_EN |\
209 SCR_EL3_SMD_DIS | SCR_EL3_RES1 |\
210 SCR_EL3_NS_EN)
macro.wave.z@gmail.comd8f5af92016-12-08 11:58:23 +0800211#endif
Chee Hong Angb07ac0c2018-08-20 10:57:34 -0700212
213#ifdef CONFIG_ARMV8_EA_EL3_FIRST
214 orr \tmp, \tmp, #SCR_EL3_EA_EN
215#endif
Alison Wang73818d52016-11-10 10:49:03 +0800216 msr scr_el3, \tmp
York Sun56cc3db2014-09-08 12:20:00 -0700217
218 /* Return to the EL2_SP2 mode from EL3 */
Alison Wang73818d52016-11-10 10:49:03 +0800219 ldr \tmp, =(SPSR_EL_DEBUG_MASK | SPSR_EL_SERR_MASK |\
220 SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
221 SPSR_EL_M_AARCH64 | SPSR_EL_M_EL2H)
222 msr spsr_el3, \tmp
223 msr elr_el3, \ep
224 eret
225
2261:
227 /*
228 * The next lower exception level is AArch32, 32bit EL2 | HCE |
229 * SMD | RES1 (Bits[5:4]) | Non-secure EL0/EL1.
230 */
231 ldr \tmp, =(SCR_EL3_RW_AARCH32 | SCR_EL3_HCE_EN |\
232 SCR_EL3_SMD_DIS | SCR_EL3_RES1 |\
233 SCR_EL3_NS_EN)
234 msr scr_el3, \tmp
235
236 /* Return to AArch32 Hypervisor mode */
237 ldr \tmp, =(SPSR_EL_END_LE | SPSR_EL_ASYN_MASK |\
238 SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
239 SPSR_EL_T_A32 | SPSR_EL_M_AARCH32 |\
240 SPSR_EL_M_HYP)
241 msr spsr_el3, \tmp
242 msr elr_el3, \ep
York Sun56cc3db2014-09-08 12:20:00 -0700243 eret
244.endm
245
Alison Wang73818d52016-11-10 10:49:03 +0800246/*
247 * Switch from EL2 to EL1 for ARMv8
248 * @ep: kernel entry point
249 * @flag: The execution state flag for lower exception
250 * level, ES_TO_AARCH64 or ES_TO_AARCH32
251 * @tmp: temporary register
252 *
253 * For loading 32-bit OS, x1 is machine nr and x2 is ftaddr.
254 * For loading 64-bit OS, x0 is physical address to the FDT blob.
255 * They will be passed to the guest.
256 */
Peter Hoyes6f4a27d2021-08-19 16:53:09 +0100257.macro armv8_switch_to_el1_m, ep, flag, tmp, tmp2
York Sun56cc3db2014-09-08 12:20:00 -0700258 /* Initialize Generic Timers */
Alison Wang73818d52016-11-10 10:49:03 +0800259 mrs \tmp, cnthctl_el2
260 /* Enable EL1 access to timers */
261 orr \tmp, \tmp, #(CNTHCTL_EL2_EL1PCEN_EN |\
262 CNTHCTL_EL2_EL1PCTEN_EN)
263 msr cnthctl_el2, \tmp
York Sun56cc3db2014-09-08 12:20:00 -0700264 msr cntvoff_el2, xzr
265
266 /* Initilize MPID/MPIDR registers */
Alison Wang73818d52016-11-10 10:49:03 +0800267 mrs \tmp, midr_el1
268 msr vpidr_el2, \tmp
269 mrs \tmp, mpidr_el1
270 msr vmpidr_el2, \tmp
York Sun56cc3db2014-09-08 12:20:00 -0700271
272 /* Disable coprocessor traps */
Alison Wang73818d52016-11-10 10:49:03 +0800273 mov \tmp, #CPTR_EL2_RES1
274 msr cptr_el2, \tmp /* Disable coprocessor traps to EL2 */
York Sun56cc3db2014-09-08 12:20:00 -0700275 msr hstr_el2, xzr /* Disable coprocessor traps to EL2 */
Alison Wang73818d52016-11-10 10:49:03 +0800276 mov \tmp, #CPACR_EL1_FPEN_EN
277 msr cpacr_el1, \tmp /* Enable FP/SIMD at EL1 */
York Sun56cc3db2014-09-08 12:20:00 -0700278
279 /* SCTLR_EL1 initialization
280 *
281 * setting RES1 bits (29,28,23,22,20,11) to 1
282 * and RES0 bits (31,30,27,21,17,13,10,6) +
283 * UCI,EE,EOE,WXN,nTWE,nTWI,UCT,DZE,I,UMA,SED,ITD,
284 * CP15BEN,SA0,SA,C,A,M to 0
285 */
Alison Wang73818d52016-11-10 10:49:03 +0800286 ldr \tmp, =(SCTLR_EL1_RES1 | SCTLR_EL1_UCI_DIS |\
287 SCTLR_EL1_EE_LE | SCTLR_EL1_WXN_DIS |\
288 SCTLR_EL1_NTWE_DIS | SCTLR_EL1_NTWI_DIS |\
289 SCTLR_EL1_UCT_DIS | SCTLR_EL1_DZE_DIS |\
290 SCTLR_EL1_ICACHE_DIS | SCTLR_EL1_UMA_DIS |\
291 SCTLR_EL1_SED_EN | SCTLR_EL1_ITD_EN |\
292 SCTLR_EL1_CP15BEN_DIS | SCTLR_EL1_SA0_DIS |\
293 SCTLR_EL1_SA_DIS | SCTLR_EL1_DCACHE_DIS |\
294 SCTLR_EL1_ALIGN_DIS | SCTLR_EL1_MMU_DIS)
295 msr sctlr_el1, \tmp
296
297 mov \tmp, sp
298 msr sp_el1, \tmp /* Migrate SP */
299 mrs \tmp, vbar_el2
300 msr vbar_el1, \tmp /* Migrate VBAR */
301
302 /* Check switch to AArch64 EL1 or AArch32 Supervisor mode */
303 cmp \flag, #ES_TO_AARCH32
304 b.eq 1f
305
306 /* Initialize HCR_EL2 */
Peter Hoyes6f4a27d2021-08-19 16:53:09 +0100307 /* Only disable PAuth traps if PAuth is supported */
308 mrs \tmp, id_aa64isar1_el1
309 ldr \tmp2, =(ID_AA64ISAR1_EL1_GPI | ID_AA64ISAR1_EL1_GPA | \
310 ID_AA64ISAR1_EL1_API | ID_AA64ISAR1_EL1_APA)
311 tst \tmp, \tmp2
312 mov \tmp2, #(HCR_EL2_RW_AARCH64 | HCR_EL2_HCD_DIS)
313 orr \tmp, \tmp2, #(HCR_EL2_APK | HCR_EL2_API)
314 csel \tmp, \tmp2, \tmp, eq
Alison Wang73818d52016-11-10 10:49:03 +0800315 msr hcr_el2, \tmp
York Sun56cc3db2014-09-08 12:20:00 -0700316
317 /* Return to the EL1_SP1 mode from EL2 */
Alison Wang73818d52016-11-10 10:49:03 +0800318 ldr \tmp, =(SPSR_EL_DEBUG_MASK | SPSR_EL_SERR_MASK |\
319 SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
320 SPSR_EL_M_AARCH64 | SPSR_EL_M_EL1H)
321 msr spsr_el2, \tmp
322 msr elr_el2, \ep
323 eret
324
3251:
326 /* Initialize HCR_EL2 */
327 ldr \tmp, =(HCR_EL2_RW_AARCH32 | HCR_EL2_HCD_DIS)
328 msr hcr_el2, \tmp
329
330 /* Return to AArch32 Supervisor mode from EL2 */
331 ldr \tmp, =(SPSR_EL_END_LE | SPSR_EL_ASYN_MASK |\
332 SPSR_EL_IRQ_MASK | SPSR_EL_FIQ_MASK |\
333 SPSR_EL_T_A32 | SPSR_EL_M_AARCH32 |\
334 SPSR_EL_M_SVC)
335 msr spsr_el2, \tmp
336 msr elr_el2, \ep
York Sun56cc3db2014-09-08 12:20:00 -0700337 eret
338.endm
339
340#if defined(CONFIG_GICV3)
341.macro gic_wait_for_interrupt_m xreg1
3420 : wfi
343 mrs \xreg1, ICC_IAR1_EL1
344 msr ICC_EOIR1_EL1, \xreg1
345 cbnz \xreg1, 0b
346.endm
347#elif defined(CONFIG_GICV2)
348.macro gic_wait_for_interrupt_m xreg1, wreg2
3490 : wfi
350 ldr \wreg2, [\xreg1, GICC_AIAR]
351 str \wreg2, [\xreg1, GICC_AEOIR]
Yehuda Yitschak07e2fd82014-10-27 14:07:16 +0200352 and \wreg2, \wreg2, #0x3ff
York Sun56cc3db2014-09-08 12:20:00 -0700353 cbnz \wreg2, 0b
354.endm
355#endif
356
David Feng85fd5f12013-12-14 11:47:35 +0800357#endif /* CONFIG_ARM64 */
358
Jean-Christophe PLAGNIOL-VILLARD06a819c2009-06-13 20:50:02 +0200359#endif /* __ASSEMBLY__ */
360#endif /* __ASM_ARM_MACRO_H__ */