blob: cbac84b93af659a150da326decf5aebbd49227af [file] [log] [blame]
Soby Mathewc6820d12016-05-09 17:49:55 +01001/*
Antonio Nino Diazc326c342019-01-11 11:20:10 +00002 * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
Soby Mathewc6820d12016-05-09 17:49:55 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewc6820d12016-05-09 17:49:55 +01005 */
6
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00007#ifndef ARCH_HELPERS_H
8#define ARCH_HELPERS_H
Soby Mathewc6820d12016-05-09 17:49:55 +01009
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010010#include <cdefs.h>
Soby Mathewc6820d12016-05-09 17:49:55 +010011#include <stdint.h>
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010012#include <string.h>
Soby Mathewc6820d12016-05-09 17:49:55 +010013
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <arch.h>
15
Soby Mathewc6820d12016-05-09 17:49:55 +010016/**********************************************************************
17 * Macros which create inline functions to read or write CPU system
18 * registers
19 *********************************************************************/
20
21#define _DEFINE_COPROCR_WRITE_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \
22static inline void write_## _name(u_register_t v) \
23{ \
24 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
25}
26
27#define _DEFINE_COPROCR_READ_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \
28static inline u_register_t read_ ## _name(void) \
29{ \
30 u_register_t v; \
31 __asm__ volatile ("mrc "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : "=r" (v));\
32 return v; \
33}
34
35/*
36 * The undocumented %Q and %R extended asm are used to implemented the below
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +000037 * 64 bit `mrrc` and `mcrr` instructions.
Soby Mathewc6820d12016-05-09 17:49:55 +010038 */
Soby Mathewc6820d12016-05-09 17:49:55 +010039
40#define _DEFINE_COPROCR_WRITE_FUNC_64(_name, coproc, opc1, CRm) \
41static inline void write64_## _name(uint64_t v) \
42{ \
43 __asm__ volatile ("mcrr "#coproc","#opc1", %Q0, %R0,"#CRm : : "r" (v));\
44}
45
46#define _DEFINE_COPROCR_READ_FUNC_64(_name, coproc, opc1, CRm) \
47static inline uint64_t read64_## _name(void) \
48{ uint64_t v; \
49 __asm__ volatile ("mrrc "#coproc","#opc1", %Q0, %R0,"#CRm : "=r" (v));\
50 return v; \
51}
52
53#define _DEFINE_SYSREG_READ_FUNC(_name, _reg_name) \
54static inline u_register_t read_ ## _name(void) \
55{ \
56 u_register_t v; \
57 __asm__ volatile ("mrs %0, " #_reg_name : "=r" (v)); \
58 return v; \
59}
60
61#define _DEFINE_SYSREG_WRITE_FUNC(_name, _reg_name) \
62static inline void write_ ## _name(u_register_t v) \
63{ \
64 __asm__ volatile ("msr " #_reg_name ", %0" : : "r" (v)); \
65}
66
67#define _DEFINE_SYSREG_WRITE_CONST_FUNC(_name, _reg_name) \
68static inline void write_ ## _name(const u_register_t v) \
69{ \
70 __asm__ volatile ("msr " #_reg_name ", %0" : : "i" (v)); \
71}
72
73/* Define read function for coproc register */
74#define DEFINE_COPROCR_READ_FUNC(_name, ...) \
75 _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__)
76
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +000077/* Define write function for coproc register */
78#define DEFINE_COPROCR_WRITE_FUNC(_name, ...) \
79 _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__)
80
Soby Mathewc6820d12016-05-09 17:49:55 +010081/* Define read & write function for coproc register */
82#define DEFINE_COPROCR_RW_FUNCS(_name, ...) \
83 _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) \
84 _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__)
85
86/* Define 64 bit read function for coproc register */
87#define DEFINE_COPROCR_READ_FUNC_64(_name, ...) \
88 _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__)
89
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +000090/* Define 64 bit write function for coproc register */
91#define DEFINE_COPROCR_WRITE_FUNC_64(_name, ...) \
92 _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__)
93
Soby Mathewc6820d12016-05-09 17:49:55 +010094/* Define 64 bit read & write function for coproc register */
95#define DEFINE_COPROCR_RW_FUNCS_64(_name, ...) \
96 _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) \
97 _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__)
98
99/* Define read & write function for system register */
100#define DEFINE_SYSREG_RW_FUNCS(_name) \
101 _DEFINE_SYSREG_READ_FUNC(_name, _name) \
102 _DEFINE_SYSREG_WRITE_FUNC(_name, _name)
103
104/**********************************************************************
105 * Macros to create inline functions for tlbi operations
106 *********************************************************************/
107
108#define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
109static inline void tlbi##_op(void) \
110{ \
111 u_register_t v = 0; \
112 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
113}
114
Antonio Nino Diazab37d152018-11-22 15:38:05 +0000115#define _DEFINE_BPIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
116static inline void bpi##_op(void) \
Antonio Nino Diazac998032017-02-27 17:23:54 +0000117{ \
118 u_register_t v = 0; \
119 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
120}
121
Soby Mathewc6820d12016-05-09 17:49:55 +0100122#define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
123static inline void tlbi##_op(u_register_t v) \
124{ \
125 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
126}
127
128/* Define function for simple TLBI operation */
129#define DEFINE_TLBIOP_FUNC(_op, ...) \
130 _DEFINE_TLBIOP_FUNC(_op, __VA_ARGS__)
131
132/* Define function for TLBI operation with register parameter */
133#define DEFINE_TLBIOP_PARAM_FUNC(_op, ...) \
134 _DEFINE_TLBIOP_PARAM_FUNC(_op, __VA_ARGS__)
135
Antonio Nino Diazac998032017-02-27 17:23:54 +0000136/* Define function for simple BPI operation */
137#define DEFINE_BPIOP_FUNC(_op, ...) \
138 _DEFINE_BPIOP_FUNC(_op, __VA_ARGS__)
139
Soby Mathewc6820d12016-05-09 17:49:55 +0100140/**********************************************************************
141 * Macros to create inline functions for DC operations
142 *********************************************************************/
143#define _DEFINE_DCOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
144static inline void dc##_op(u_register_t v) \
145{ \
146 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
147}
148
149/* Define function for DC operation with register parameter */
150#define DEFINE_DCOP_PARAM_FUNC(_op, ...) \
151 _DEFINE_DCOP_PARAM_FUNC(_op, __VA_ARGS__)
152
153/**********************************************************************
154 * Macros to create inline functions for system instructions
155 *********************************************************************/
156 /* Define function for simple system instruction */
157#define DEFINE_SYSOP_FUNC(_op) \
158static inline void _op(void) \
159{ \
160 __asm__ (#_op); \
161}
162
163
164/* Define function for system instruction with type specifier */
165#define DEFINE_SYSOP_TYPE_FUNC(_op, _type) \
166static inline void _op ## _type(void) \
167{ \
168 __asm__ (#_op " " #_type); \
169}
170
171/* Define function for system instruction with register parameter */
172#define DEFINE_SYSOP_TYPE_PARAM_FUNC(_op, _type) \
173static inline void _op ## _type(u_register_t v) \
174{ \
175 __asm__ (#_op " " #_type ", %0" : : "r" (v)); \
176}
177
178void flush_dcache_range(uintptr_t addr, size_t size);
179void clean_dcache_range(uintptr_t addr, size_t size);
180void inv_dcache_range(uintptr_t addr, size_t size);
181
Antonio Nino Diaze40306b2017-01-13 15:03:07 +0000182void dcsw_op_louis(u_register_t op_type);
183void dcsw_op_all(u_register_t op_type);
184
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100185void disable_mmu_secure(void);
186void disable_mmu_icache_secure(void);
187
Soby Mathewc6820d12016-05-09 17:49:55 +0100188DEFINE_SYSOP_FUNC(wfi)
189DEFINE_SYSOP_FUNC(wfe)
190DEFINE_SYSOP_FUNC(sev)
191DEFINE_SYSOP_TYPE_FUNC(dsb, sy)
192DEFINE_SYSOP_TYPE_FUNC(dmb, sy)
Yatharth Kochar2694cba2016-11-14 12:00:41 +0000193DEFINE_SYSOP_TYPE_FUNC(dmb, st)
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100194
195/* dmb ld is not valid for armv7/thumb machines */
196#if ARM_ARCH_MAJOR != 7
Yatharth Kochar2694cba2016-11-14 12:00:41 +0000197DEFINE_SYSOP_TYPE_FUNC(dmb, ld)
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100198#endif
199
Soby Mathewc6820d12016-05-09 17:49:55 +0100200DEFINE_SYSOP_TYPE_FUNC(dsb, ish)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000201DEFINE_SYSOP_TYPE_FUNC(dsb, ishst)
Soby Mathewc6820d12016-05-09 17:49:55 +0100202DEFINE_SYSOP_TYPE_FUNC(dmb, ish)
Jeenu Viswambharan62505072017-09-22 08:32:09 +0100203DEFINE_SYSOP_TYPE_FUNC(dmb, ishst)
Soby Mathewc6820d12016-05-09 17:49:55 +0100204DEFINE_SYSOP_FUNC(isb)
205
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100206void __dead2 smc(uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3,
207 uint32_t r4, uint32_t r5, uint32_t r6, uint32_t r7);
208
Soby Mathewc6820d12016-05-09 17:49:55 +0100209DEFINE_SYSREG_RW_FUNCS(spsr)
210DEFINE_SYSREG_RW_FUNCS(cpsr)
211
212/*******************************************************************************
213 * System register accessor prototypes
214 ******************************************************************************/
215DEFINE_COPROCR_READ_FUNC(mpidr, MPIDR)
216DEFINE_COPROCR_READ_FUNC(midr, MIDR)
Antonio Nino Diazc326c342019-01-11 11:20:10 +0000217DEFINE_COPROCR_READ_FUNC(id_mmfr4, ID_MMFR4)
Dimitris Papastamosdda48b02017-10-17 14:03:14 +0100218DEFINE_COPROCR_READ_FUNC(id_pfr0, ID_PFR0)
Soby Mathewc6820d12016-05-09 17:49:55 +0100219DEFINE_COPROCR_READ_FUNC(id_pfr1, ID_PFR1)
220DEFINE_COPROCR_READ_FUNC(isr, ISR)
221DEFINE_COPROCR_READ_FUNC(clidr, CLIDR)
222DEFINE_COPROCR_READ_FUNC_64(cntpct, CNTPCT_64)
223
224DEFINE_COPROCR_RW_FUNCS(scr, SCR)
225DEFINE_COPROCR_RW_FUNCS(ctr, CTR)
226DEFINE_COPROCR_RW_FUNCS(sctlr, SCTLR)
Etienne Carriere70a004b2017-11-05 22:56:03 +0100227DEFINE_COPROCR_RW_FUNCS(actlr, ACTLR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100228DEFINE_COPROCR_RW_FUNCS(hsctlr, HSCTLR)
229DEFINE_COPROCR_RW_FUNCS(hcr, HCR)
230DEFINE_COPROCR_RW_FUNCS(hcptr, HCPTR)
231DEFINE_COPROCR_RW_FUNCS(cntfrq, CNTFRQ)
232DEFINE_COPROCR_RW_FUNCS(cnthctl, CNTHCTL)
233DEFINE_COPROCR_RW_FUNCS(mair0, MAIR0)
234DEFINE_COPROCR_RW_FUNCS(mair1, MAIR1)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000235DEFINE_COPROCR_RW_FUNCS(hmair0, HMAIR0)
Soby Mathewc6820d12016-05-09 17:49:55 +0100236DEFINE_COPROCR_RW_FUNCS(ttbcr, TTBCR)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000237DEFINE_COPROCR_RW_FUNCS(htcr, HTCR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100238DEFINE_COPROCR_RW_FUNCS(ttbr0, TTBR0)
239DEFINE_COPROCR_RW_FUNCS_64(ttbr0, TTBR0_64)
240DEFINE_COPROCR_RW_FUNCS(ttbr1, TTBR1)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000241DEFINE_COPROCR_RW_FUNCS_64(httbr, HTTBR_64)
Soby Mathewc6820d12016-05-09 17:49:55 +0100242DEFINE_COPROCR_RW_FUNCS(vpidr, VPIDR)
243DEFINE_COPROCR_RW_FUNCS(vmpidr, VMPIDR)
244DEFINE_COPROCR_RW_FUNCS_64(vttbr, VTTBR_64)
245DEFINE_COPROCR_RW_FUNCS_64(ttbr1, TTBR1_64)
246DEFINE_COPROCR_RW_FUNCS_64(cntvoff, CNTVOFF_64)
247DEFINE_COPROCR_RW_FUNCS(csselr, CSSELR)
David Cunadofee86532017-04-13 22:38:29 +0100248DEFINE_COPROCR_RW_FUNCS(hstr, HSTR)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000249DEFINE_COPROCR_RW_FUNCS(cnthp_ctl_el2, CNTHP_CTL)
250DEFINE_COPROCR_RW_FUNCS(cnthp_tval_el2, CNTHP_TVAL)
251DEFINE_COPROCR_RW_FUNCS_64(cnthp_cval_el2, CNTHP_CVAL_64)
Soby Mathewc6820d12016-05-09 17:49:55 +0100252
Antonio Nino Diazdc4ed3d2018-11-23 13:54:00 +0000253#define get_cntp_ctl_enable(x) (((x) >> CNTP_CTL_ENABLE_SHIFT) & \
254 CNTP_CTL_ENABLE_MASK)
255#define get_cntp_ctl_imask(x) (((x) >> CNTP_CTL_IMASK_SHIFT) & \
256 CNTP_CTL_IMASK_MASK)
257#define get_cntp_ctl_istatus(x) (((x) >> CNTP_CTL_ISTATUS_SHIFT) & \
258 CNTP_CTL_ISTATUS_MASK)
259
260#define set_cntp_ctl_enable(x) ((x) |= U(1) << CNTP_CTL_ENABLE_SHIFT)
261#define set_cntp_ctl_imask(x) ((x) |= U(1) << CNTP_CTL_IMASK_SHIFT)
262
263#define clr_cntp_ctl_enable(x) ((x) &= ~(U(1) << CNTP_CTL_ENABLE_SHIFT))
264#define clr_cntp_ctl_imask(x) ((x) &= ~(U(1) << CNTP_CTL_IMASK_SHIFT))
265
Soby Mathewc6820d12016-05-09 17:49:55 +0100266DEFINE_COPROCR_RW_FUNCS(icc_sre_el1, ICC_SRE)
267DEFINE_COPROCR_RW_FUNCS(icc_sre_el2, ICC_HSRE)
268DEFINE_COPROCR_RW_FUNCS(icc_sre_el3, ICC_MSRE)
269DEFINE_COPROCR_RW_FUNCS(icc_pmr_el1, ICC_PMR)
Jeenu Viswambharanb1e957e2017-09-22 08:32:09 +0100270DEFINE_COPROCR_RW_FUNCS(icc_rpr_el1, ICC_RPR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100271DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el3, ICC_MGRPEN1)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000272DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el1, ICC_IGRPEN1)
Soby Mathewc6820d12016-05-09 17:49:55 +0100273DEFINE_COPROCR_RW_FUNCS(icc_igrpen0_el1, ICC_IGRPEN0)
274DEFINE_COPROCR_RW_FUNCS(icc_hppir0_el1, ICC_HPPIR0)
275DEFINE_COPROCR_RW_FUNCS(icc_hppir1_el1, ICC_HPPIR1)
276DEFINE_COPROCR_RW_FUNCS(icc_iar0_el1, ICC_IAR0)
277DEFINE_COPROCR_RW_FUNCS(icc_iar1_el1, ICC_IAR1)
278DEFINE_COPROCR_RW_FUNCS(icc_eoir0_el1, ICC_EOIR0)
279DEFINE_COPROCR_RW_FUNCS(icc_eoir1_el1, ICC_EOIR1)
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100280DEFINE_COPROCR_RW_FUNCS_64(icc_sgi0r_el1, ICC_SGI0R_EL1_64)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000281DEFINE_COPROCR_WRITE_FUNC_64(icc_sgi1r, ICC_SGI1R_EL1_64)
Soby Mathewc6820d12016-05-09 17:49:55 +0100282
David Cunado5f55e282016-10-31 17:37:34 +0000283DEFINE_COPROCR_RW_FUNCS(hdcr, HDCR)
David Cunadoc14b08e2016-11-25 00:21:59 +0000284DEFINE_COPROCR_RW_FUNCS(cnthp_ctl, CNTHP_CTL)
David Cunado5f55e282016-10-31 17:37:34 +0000285DEFINE_COPROCR_READ_FUNC(pmcr, PMCR)
286
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000287/*
288 * Address translation
289 */
290DEFINE_COPROCR_WRITE_FUNC(ats1cpr, ATS1CPR)
291DEFINE_COPROCR_WRITE_FUNC(ats1hr, ATS1HR)
Douglas Raillard77414632018-08-21 12:54:45 +0100292DEFINE_COPROCR_RW_FUNCS_64(par, PAR_64)
293
Etienne Carriere70a004b2017-11-05 22:56:03 +0100294DEFINE_COPROCR_RW_FUNCS(nsacr, NSACR)
295
296/* AArch32 coproc registers for 32bit MMU descriptor support */
297DEFINE_COPROCR_RW_FUNCS(prrr, PRRR)
298DEFINE_COPROCR_RW_FUNCS(nmrr, NMRR)
299DEFINE_COPROCR_RW_FUNCS(dacr, DACR)
300
Dimitris Papastamosdda48b02017-10-17 14:03:14 +0100301DEFINE_COPROCR_RW_FUNCS(amcntenset0, AMCNTENSET0)
302DEFINE_COPROCR_RW_FUNCS(amcntenset1, AMCNTENSET1)
303DEFINE_COPROCR_RW_FUNCS(amcntenclr0, AMCNTENCLR0)
304DEFINE_COPROCR_RW_FUNCS(amcntenclr1, AMCNTENCLR1)
305
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000306DEFINE_COPROCR_RW_FUNCS_64(amevcntr00, AMEVCNTR00)
307DEFINE_COPROCR_RW_FUNCS_64(amevcntr01, AMEVCNTR01)
308DEFINE_COPROCR_RW_FUNCS_64(amevcntr02, AMEVCNTR02)
309DEFINE_COPROCR_RW_FUNCS_64(amevcntr03, AMEVCNTR03)
310
Soby Mathewc6820d12016-05-09 17:49:55 +0100311/*
312 * TLBI operation prototypes
313 */
314DEFINE_TLBIOP_FUNC(all, TLBIALL)
315DEFINE_TLBIOP_FUNC(allis, TLBIALLIS)
316DEFINE_TLBIOP_PARAM_FUNC(mva, TLBIMVA)
317DEFINE_TLBIOP_PARAM_FUNC(mvaa, TLBIMVAA)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000318DEFINE_TLBIOP_PARAM_FUNC(mvaais, TLBIMVAAIS)
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100319DEFINE_TLBIOP_PARAM_FUNC(mvahis, TLBIMVAHIS)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000320
321/*
322 * BPI operation prototypes.
323 */
324DEFINE_BPIOP_FUNC(allis, BPIALLIS)
Soby Mathewc6820d12016-05-09 17:49:55 +0100325
326/*
327 * DC operation prototypes
328 */
329DEFINE_DCOP_PARAM_FUNC(civac, DCCIMVAC)
330DEFINE_DCOP_PARAM_FUNC(ivac, DCIMVAC)
Ambroise Vincentf5fdfbc2019-02-21 14:16:24 +0000331#if ERRATA_A53_819472 || ERRATA_A53_824069 || ERRATA_A53_827319
332DEFINE_DCOP_PARAM_FUNC(cvac, DCCIMVAC)
333#else
Soby Mathewc6820d12016-05-09 17:49:55 +0100334DEFINE_DCOP_PARAM_FUNC(cvac, DCCMVAC)
Ambroise Vincentf5fdfbc2019-02-21 14:16:24 +0000335#endif
Soby Mathewc6820d12016-05-09 17:49:55 +0100336
337/* Previously defined accessor functions with incomplete register names */
338#define dsb() dsbsy()
Etienne Carrierea2579862017-11-05 22:57:29 +0100339#define dmb() dmbsy()
Soby Mathewc6820d12016-05-09 17:49:55 +0100340
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100341/* dmb ld is not valid for armv7/thumb machines, so alias it to dmb */
342#if ARM_ARCH_MAJOR == 7
343#define dmbld() dmb()
344#endif
345
Soby Mathewc6820d12016-05-09 17:49:55 +0100346#define IS_IN_SECURE() \
347 (GET_NS_BIT(read_scr()) == 0)
348
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100349#define IS_IN_HYP() (GET_M32(read_cpsr()) == MODE32_hyp)
350#define IS_IN_SVC() (GET_M32(read_cpsr()) == MODE32_svc)
351#define IS_IN_MON() (GET_M32(read_cpsr()) == MODE32_mon)
352#define IS_IN_EL2() IS_IN_HYP()
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000353/* If EL3 is AArch32, then secure PL1 and monitor mode correspond to EL3 */
Soby Mathewc6820d12016-05-09 17:49:55 +0100354#define IS_IN_EL3() \
355 ((GET_M32(read_cpsr()) == MODE32_mon) || \
356 (IS_IN_SECURE() && (GET_M32(read_cpsr()) != MODE32_usr)))
357
Douglas Raillard77414632018-08-21 12:54:45 +0100358static inline unsigned int get_current_el(void)
359{
360 if (IS_IN_EL3()) {
361 return 3U;
362 } else if (IS_IN_EL2()) {
363 return 2U;
364 } else {
365 return 1U;
366 }
367}
368
Soby Mathewc6820d12016-05-09 17:49:55 +0100369/* Macros for compatibility with AArch64 system registers */
370#define read_mpidr_el1() read_mpidr()
371
372#define read_scr_el3() read_scr()
373#define write_scr_el3(_v) write_scr(_v)
374
375#define read_hcr_el2() read_hcr()
376#define write_hcr_el2(_v) write_hcr(_v)
377
378#define read_cpacr_el1() read_cpacr()
379#define write_cpacr_el1(_v) write_cpacr(_v)
380
381#define read_cntfrq_el0() read_cntfrq()
382#define write_cntfrq_el0(_v) write_cntfrq(_v)
383#define read_isr_el1() read_isr()
384
385#define read_cntpct_el0() read64_cntpct()
386
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100387#define read_ctr_el0() read_ctr()
388
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000389#define write_icc_sgi0r_el1(_v) write64_icc_sgi0r_el1(_v)
390
391#define read_daif() read_cpsr()
392#define write_daif(flags) write_cpsr(flags)
393
394#define read_cnthp_cval_el2() read64_cnthp_cval_el2()
395#define write_cnthp_cval_el2(v) write64_cnthp_cval_el2(v)
396
397#define read_amcntenset0_el0() read_amcntenset0()
398#define read_amcntenset1_el0() read_amcntenset1()
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100399
Antonio Nino Diazb4e3e4b2018-11-23 15:04:01 +0000400/* Helper functions to manipulate CPSR */
401static inline void enable_irq(void)
402{
403 /*
404 * The compiler memory barrier will prevent the compiler from
405 * scheduling non-volatile memory access after the write to the
406 * register.
407 *
408 * This could happen if some initialization code issues non-volatile
409 * accesses to an area used by an interrupt handler, in the assumption
410 * that it is safe as the interrupts are disabled at the time it does
411 * that (according to program order). However, non-volatile accesses
412 * are not necessarily in program order relatively with volatile inline
413 * assembly statements (and volatile accesses).
414 */
415 COMPILER_BARRIER();
416 __asm__ volatile ("cpsie i");
417 isb();
418}
419
420static inline void enable_serror(void)
421{
422 COMPILER_BARRIER();
423 __asm__ volatile ("cpsie a");
424 isb();
425}
426
427static inline void enable_fiq(void)
428{
429 COMPILER_BARRIER();
430 __asm__ volatile ("cpsie f");
431 isb();
432}
433
434static inline void disable_irq(void)
435{
436 COMPILER_BARRIER();
437 __asm__ volatile ("cpsid i");
438 isb();
439}
440
441static inline void disable_serror(void)
442{
443 COMPILER_BARRIER();
444 __asm__ volatile ("cpsid a");
445 isb();
446}
447
448static inline void disable_fiq(void)
449{
450 COMPILER_BARRIER();
451 __asm__ volatile ("cpsid f");
452 isb();
453}
454
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000455#endif /* ARCH_HELPERS_H */