blob: a7954cc16769aa007e469c1c46ad7c055531eaed [file] [log] [blame]
Soby Mathewc6820d12016-05-09 17:49:55 +01001/*
johpow01fa59c6f2020-10-02 13:41:11 -05002 * Copyright (c) 2016-2021, ARM Limited and Contributors. All rights reserved.
Florian Lugoud4e25032021-09-08 12:40:24 +02003 * Portions copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved.
Soby Mathewc6820d12016-05-09 17:49:55 +01004 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewc6820d12016-05-09 17:49:55 +01006 */
7
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00008#ifndef ARCH_HELPERS_H
9#define ARCH_HELPERS_H
Soby Mathewc6820d12016-05-09 17:49:55 +010010
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010011#include <cdefs.h>
Masahiro Yamada019b4f82020-04-02 15:35:19 +090012#include <stdbool.h>
Soby Mathewc6820d12016-05-09 17:49:55 +010013#include <stdint.h>
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010014#include <string.h>
Soby Mathewc6820d12016-05-09 17:49:55 +010015
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <arch.h>
17
Soby Mathewc6820d12016-05-09 17:49:55 +010018/**********************************************************************
19 * Macros which create inline functions to read or write CPU system
20 * registers
21 *********************************************************************/
22
23#define _DEFINE_COPROCR_WRITE_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \
24static inline void write_## _name(u_register_t v) \
25{ \
26 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
27}
28
29#define _DEFINE_COPROCR_READ_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \
30static inline u_register_t read_ ## _name(void) \
31{ \
32 u_register_t v; \
33 __asm__ volatile ("mrc "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : "=r" (v));\
34 return v; \
35}
36
37/*
38 * The undocumented %Q and %R extended asm are used to implemented the below
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +000039 * 64 bit `mrrc` and `mcrr` instructions.
Soby Mathewc6820d12016-05-09 17:49:55 +010040 */
Soby Mathewc6820d12016-05-09 17:49:55 +010041
42#define _DEFINE_COPROCR_WRITE_FUNC_64(_name, coproc, opc1, CRm) \
43static inline void write64_## _name(uint64_t v) \
44{ \
45 __asm__ volatile ("mcrr "#coproc","#opc1", %Q0, %R0,"#CRm : : "r" (v));\
46}
47
48#define _DEFINE_COPROCR_READ_FUNC_64(_name, coproc, opc1, CRm) \
49static inline uint64_t read64_## _name(void) \
50{ uint64_t v; \
51 __asm__ volatile ("mrrc "#coproc","#opc1", %Q0, %R0,"#CRm : "=r" (v));\
52 return v; \
53}
54
55#define _DEFINE_SYSREG_READ_FUNC(_name, _reg_name) \
56static inline u_register_t read_ ## _name(void) \
57{ \
58 u_register_t v; \
59 __asm__ volatile ("mrs %0, " #_reg_name : "=r" (v)); \
60 return v; \
61}
62
63#define _DEFINE_SYSREG_WRITE_FUNC(_name, _reg_name) \
64static inline void write_ ## _name(u_register_t v) \
65{ \
66 __asm__ volatile ("msr " #_reg_name ", %0" : : "r" (v)); \
67}
68
69#define _DEFINE_SYSREG_WRITE_CONST_FUNC(_name, _reg_name) \
70static inline void write_ ## _name(const u_register_t v) \
71{ \
72 __asm__ volatile ("msr " #_reg_name ", %0" : : "i" (v)); \
73}
74
75/* Define read function for coproc register */
76#define DEFINE_COPROCR_READ_FUNC(_name, ...) \
77 _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__)
78
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +000079/* Define write function for coproc register */
80#define DEFINE_COPROCR_WRITE_FUNC(_name, ...) \
81 _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__)
82
Soby Mathewc6820d12016-05-09 17:49:55 +010083/* Define read & write function for coproc register */
84#define DEFINE_COPROCR_RW_FUNCS(_name, ...) \
85 _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) \
86 _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__)
87
88/* Define 64 bit read function for coproc register */
89#define DEFINE_COPROCR_READ_FUNC_64(_name, ...) \
90 _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__)
91
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +000092/* Define 64 bit write function for coproc register */
93#define DEFINE_COPROCR_WRITE_FUNC_64(_name, ...) \
94 _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__)
95
Soby Mathewc6820d12016-05-09 17:49:55 +010096/* Define 64 bit read & write function for coproc register */
97#define DEFINE_COPROCR_RW_FUNCS_64(_name, ...) \
98 _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) \
99 _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__)
100
101/* Define read & write function for system register */
102#define DEFINE_SYSREG_RW_FUNCS(_name) \
103 _DEFINE_SYSREG_READ_FUNC(_name, _name) \
104 _DEFINE_SYSREG_WRITE_FUNC(_name, _name)
105
106/**********************************************************************
107 * Macros to create inline functions for tlbi operations
108 *********************************************************************/
109
110#define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
111static inline void tlbi##_op(void) \
112{ \
113 u_register_t v = 0; \
114 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
115}
116
Antonio Nino Diazab37d152018-11-22 15:38:05 +0000117#define _DEFINE_BPIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
118static inline void bpi##_op(void) \
Antonio Nino Diazac998032017-02-27 17:23:54 +0000119{ \
120 u_register_t v = 0; \
121 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
122}
123
Soby Mathewc6820d12016-05-09 17:49:55 +0100124#define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
125static inline void tlbi##_op(u_register_t v) \
126{ \
127 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
128}
129
130/* Define function for simple TLBI operation */
131#define DEFINE_TLBIOP_FUNC(_op, ...) \
132 _DEFINE_TLBIOP_FUNC(_op, __VA_ARGS__)
133
134/* Define function for TLBI operation with register parameter */
135#define DEFINE_TLBIOP_PARAM_FUNC(_op, ...) \
136 _DEFINE_TLBIOP_PARAM_FUNC(_op, __VA_ARGS__)
137
Antonio Nino Diazac998032017-02-27 17:23:54 +0000138/* Define function for simple BPI operation */
139#define DEFINE_BPIOP_FUNC(_op, ...) \
140 _DEFINE_BPIOP_FUNC(_op, __VA_ARGS__)
141
Soby Mathewc6820d12016-05-09 17:49:55 +0100142/**********************************************************************
143 * Macros to create inline functions for DC operations
144 *********************************************************************/
145#define _DEFINE_DCOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
146static inline void dc##_op(u_register_t v) \
147{ \
148 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
149}
150
151/* Define function for DC operation with register parameter */
152#define DEFINE_DCOP_PARAM_FUNC(_op, ...) \
153 _DEFINE_DCOP_PARAM_FUNC(_op, __VA_ARGS__)
154
155/**********************************************************************
156 * Macros to create inline functions for system instructions
157 *********************************************************************/
158 /* Define function for simple system instruction */
159#define DEFINE_SYSOP_FUNC(_op) \
160static inline void _op(void) \
161{ \
162 __asm__ (#_op); \
163}
164
165
166/* Define function for system instruction with type specifier */
167#define DEFINE_SYSOP_TYPE_FUNC(_op, _type) \
168static inline void _op ## _type(void) \
169{ \
Andre Przywara5c29cba2020-10-16 18:19:03 +0100170 __asm__ (#_op " " #_type : : : "memory"); \
Soby Mathewc6820d12016-05-09 17:49:55 +0100171}
172
173/* Define function for system instruction with register parameter */
174#define DEFINE_SYSOP_TYPE_PARAM_FUNC(_op, _type) \
175static inline void _op ## _type(u_register_t v) \
176{ \
177 __asm__ (#_op " " #_type ", %0" : : "r" (v)); \
178}
179
180void flush_dcache_range(uintptr_t addr, size_t size);
181void clean_dcache_range(uintptr_t addr, size_t size);
182void inv_dcache_range(uintptr_t addr, size_t size);
Masahiro Yamada019b4f82020-04-02 15:35:19 +0900183bool is_dcache_enabled(void);
Soby Mathewc6820d12016-05-09 17:49:55 +0100184
Antonio Nino Diaze40306b2017-01-13 15:03:07 +0000185void dcsw_op_louis(u_register_t op_type);
186void dcsw_op_all(u_register_t op_type);
187
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100188void disable_mmu_secure(void);
189void disable_mmu_icache_secure(void);
190
Soby Mathewc6820d12016-05-09 17:49:55 +0100191DEFINE_SYSOP_FUNC(wfi)
192DEFINE_SYSOP_FUNC(wfe)
193DEFINE_SYSOP_FUNC(sev)
194DEFINE_SYSOP_TYPE_FUNC(dsb, sy)
195DEFINE_SYSOP_TYPE_FUNC(dmb, sy)
Yatharth Kochar2694cba2016-11-14 12:00:41 +0000196DEFINE_SYSOP_TYPE_FUNC(dmb, st)
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100197
198/* dmb ld is not valid for armv7/thumb machines */
199#if ARM_ARCH_MAJOR != 7
Yatharth Kochar2694cba2016-11-14 12:00:41 +0000200DEFINE_SYSOP_TYPE_FUNC(dmb, ld)
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100201#endif
202
Soby Mathewc6820d12016-05-09 17:49:55 +0100203DEFINE_SYSOP_TYPE_FUNC(dsb, ish)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000204DEFINE_SYSOP_TYPE_FUNC(dsb, ishst)
Soby Mathewc6820d12016-05-09 17:49:55 +0100205DEFINE_SYSOP_TYPE_FUNC(dmb, ish)
Jeenu Viswambharan62505072017-09-22 08:32:09 +0100206DEFINE_SYSOP_TYPE_FUNC(dmb, ishst)
Soby Mathewc6820d12016-05-09 17:49:55 +0100207DEFINE_SYSOP_FUNC(isb)
208
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100209void __dead2 smc(uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3,
210 uint32_t r4, uint32_t r5, uint32_t r6, uint32_t r7);
211
Soby Mathewc6820d12016-05-09 17:49:55 +0100212DEFINE_SYSREG_RW_FUNCS(spsr)
213DEFINE_SYSREG_RW_FUNCS(cpsr)
214
215/*******************************************************************************
216 * System register accessor prototypes
217 ******************************************************************************/
218DEFINE_COPROCR_READ_FUNC(mpidr, MPIDR)
219DEFINE_COPROCR_READ_FUNC(midr, MIDR)
Andre Przywara54d57912023-05-23 13:56:55 +0100220DEFINE_COPROCR_READ_FUNC(id_mmfr3, ID_MMFR3)
Antonio Nino Diazc326c342019-01-11 11:20:10 +0000221DEFINE_COPROCR_READ_FUNC(id_mmfr4, ID_MMFR4)
Manish V Badarkhef356f7e2021-06-29 11:44:20 +0100222DEFINE_COPROCR_READ_FUNC(id_dfr0, ID_DFR0)
Dimitris Papastamosdda48b02017-10-17 14:03:14 +0100223DEFINE_COPROCR_READ_FUNC(id_pfr0, ID_PFR0)
Soby Mathewc6820d12016-05-09 17:49:55 +0100224DEFINE_COPROCR_READ_FUNC(id_pfr1, ID_PFR1)
225DEFINE_COPROCR_READ_FUNC(isr, ISR)
226DEFINE_COPROCR_READ_FUNC(clidr, CLIDR)
227DEFINE_COPROCR_READ_FUNC_64(cntpct, CNTPCT_64)
228
229DEFINE_COPROCR_RW_FUNCS(scr, SCR)
230DEFINE_COPROCR_RW_FUNCS(ctr, CTR)
231DEFINE_COPROCR_RW_FUNCS(sctlr, SCTLR)
Etienne Carriere70a004b2017-11-05 22:56:03 +0100232DEFINE_COPROCR_RW_FUNCS(actlr, ACTLR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100233DEFINE_COPROCR_RW_FUNCS(hsctlr, HSCTLR)
234DEFINE_COPROCR_RW_FUNCS(hcr, HCR)
235DEFINE_COPROCR_RW_FUNCS(hcptr, HCPTR)
236DEFINE_COPROCR_RW_FUNCS(cntfrq, CNTFRQ)
237DEFINE_COPROCR_RW_FUNCS(cnthctl, CNTHCTL)
238DEFINE_COPROCR_RW_FUNCS(mair0, MAIR0)
239DEFINE_COPROCR_RW_FUNCS(mair1, MAIR1)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000240DEFINE_COPROCR_RW_FUNCS(hmair0, HMAIR0)
Soby Mathewc6820d12016-05-09 17:49:55 +0100241DEFINE_COPROCR_RW_FUNCS(ttbcr, TTBCR)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000242DEFINE_COPROCR_RW_FUNCS(htcr, HTCR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100243DEFINE_COPROCR_RW_FUNCS(ttbr0, TTBR0)
244DEFINE_COPROCR_RW_FUNCS_64(ttbr0, TTBR0_64)
245DEFINE_COPROCR_RW_FUNCS(ttbr1, TTBR1)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000246DEFINE_COPROCR_RW_FUNCS_64(httbr, HTTBR_64)
Soby Mathewc6820d12016-05-09 17:49:55 +0100247DEFINE_COPROCR_RW_FUNCS(vpidr, VPIDR)
248DEFINE_COPROCR_RW_FUNCS(vmpidr, VMPIDR)
249DEFINE_COPROCR_RW_FUNCS_64(vttbr, VTTBR_64)
250DEFINE_COPROCR_RW_FUNCS_64(ttbr1, TTBR1_64)
251DEFINE_COPROCR_RW_FUNCS_64(cntvoff, CNTVOFF_64)
252DEFINE_COPROCR_RW_FUNCS(csselr, CSSELR)
David Cunadofee86532017-04-13 22:38:29 +0100253DEFINE_COPROCR_RW_FUNCS(hstr, HSTR)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000254DEFINE_COPROCR_RW_FUNCS(cnthp_ctl_el2, CNTHP_CTL)
255DEFINE_COPROCR_RW_FUNCS(cnthp_tval_el2, CNTHP_TVAL)
256DEFINE_COPROCR_RW_FUNCS_64(cnthp_cval_el2, CNTHP_CVAL_64)
Soby Mathewc6820d12016-05-09 17:49:55 +0100257
Antonio Nino Diazdc4ed3d2018-11-23 13:54:00 +0000258#define get_cntp_ctl_enable(x) (((x) >> CNTP_CTL_ENABLE_SHIFT) & \
259 CNTP_CTL_ENABLE_MASK)
260#define get_cntp_ctl_imask(x) (((x) >> CNTP_CTL_IMASK_SHIFT) & \
261 CNTP_CTL_IMASK_MASK)
262#define get_cntp_ctl_istatus(x) (((x) >> CNTP_CTL_ISTATUS_SHIFT) & \
263 CNTP_CTL_ISTATUS_MASK)
264
265#define set_cntp_ctl_enable(x) ((x) |= U(1) << CNTP_CTL_ENABLE_SHIFT)
266#define set_cntp_ctl_imask(x) ((x) |= U(1) << CNTP_CTL_IMASK_SHIFT)
267
268#define clr_cntp_ctl_enable(x) ((x) &= ~(U(1) << CNTP_CTL_ENABLE_SHIFT))
269#define clr_cntp_ctl_imask(x) ((x) &= ~(U(1) << CNTP_CTL_IMASK_SHIFT))
270
Soby Mathewc6820d12016-05-09 17:49:55 +0100271DEFINE_COPROCR_RW_FUNCS(icc_sre_el1, ICC_SRE)
272DEFINE_COPROCR_RW_FUNCS(icc_sre_el2, ICC_HSRE)
273DEFINE_COPROCR_RW_FUNCS(icc_sre_el3, ICC_MSRE)
274DEFINE_COPROCR_RW_FUNCS(icc_pmr_el1, ICC_PMR)
Jeenu Viswambharanb1e957e2017-09-22 08:32:09 +0100275DEFINE_COPROCR_RW_FUNCS(icc_rpr_el1, ICC_RPR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100276DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el3, ICC_MGRPEN1)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000277DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el1, ICC_IGRPEN1)
Soby Mathewc6820d12016-05-09 17:49:55 +0100278DEFINE_COPROCR_RW_FUNCS(icc_igrpen0_el1, ICC_IGRPEN0)
279DEFINE_COPROCR_RW_FUNCS(icc_hppir0_el1, ICC_HPPIR0)
280DEFINE_COPROCR_RW_FUNCS(icc_hppir1_el1, ICC_HPPIR1)
281DEFINE_COPROCR_RW_FUNCS(icc_iar0_el1, ICC_IAR0)
282DEFINE_COPROCR_RW_FUNCS(icc_iar1_el1, ICC_IAR1)
283DEFINE_COPROCR_RW_FUNCS(icc_eoir0_el1, ICC_EOIR0)
284DEFINE_COPROCR_RW_FUNCS(icc_eoir1_el1, ICC_EOIR1)
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100285DEFINE_COPROCR_RW_FUNCS_64(icc_sgi0r_el1, ICC_SGI0R_EL1_64)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000286DEFINE_COPROCR_WRITE_FUNC_64(icc_sgi1r, ICC_SGI1R_EL1_64)
Florian Lugoud4e25032021-09-08 12:40:24 +0200287DEFINE_COPROCR_WRITE_FUNC_64(icc_asgi1r, ICC_ASGI1R_EL1_64)
Soby Mathewc6820d12016-05-09 17:49:55 +0100288
Manish V Badarkhe51a97112021-07-08 09:33:18 +0100289DEFINE_COPROCR_RW_FUNCS(sdcr, SDCR)
David Cunado5f55e282016-10-31 17:37:34 +0000290DEFINE_COPROCR_RW_FUNCS(hdcr, HDCR)
David Cunadoc14b08e2016-11-25 00:21:59 +0000291DEFINE_COPROCR_RW_FUNCS(cnthp_ctl, CNTHP_CTL)
David Cunado5f55e282016-10-31 17:37:34 +0000292DEFINE_COPROCR_READ_FUNC(pmcr, PMCR)
293
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000294/*
295 * Address translation
296 */
297DEFINE_COPROCR_WRITE_FUNC(ats1cpr, ATS1CPR)
298DEFINE_COPROCR_WRITE_FUNC(ats1hr, ATS1HR)
Douglas Raillard77414632018-08-21 12:54:45 +0100299DEFINE_COPROCR_RW_FUNCS_64(par, PAR_64)
300
Etienne Carriere70a004b2017-11-05 22:56:03 +0100301DEFINE_COPROCR_RW_FUNCS(nsacr, NSACR)
302
303/* AArch32 coproc registers for 32bit MMU descriptor support */
304DEFINE_COPROCR_RW_FUNCS(prrr, PRRR)
305DEFINE_COPROCR_RW_FUNCS(nmrr, NMRR)
306DEFINE_COPROCR_RW_FUNCS(dacr, DACR)
307
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100308/* Coproc registers for 32bit AMU support */
309DEFINE_COPROCR_READ_FUNC(amcfgr, AMCFGR)
310DEFINE_COPROCR_READ_FUNC(amcgcr, AMCGCR)
johpow01fa59c6f2020-10-02 13:41:11 -0500311DEFINE_COPROCR_RW_FUNCS(amcr, AMCR)
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100312
Dimitris Papastamosdda48b02017-10-17 14:03:14 +0100313DEFINE_COPROCR_RW_FUNCS(amcntenset0, AMCNTENSET0)
314DEFINE_COPROCR_RW_FUNCS(amcntenset1, AMCNTENSET1)
315DEFINE_COPROCR_RW_FUNCS(amcntenclr0, AMCNTENCLR0)
316DEFINE_COPROCR_RW_FUNCS(amcntenclr1, AMCNTENCLR1)
317
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100318/* Coproc registers for 64bit AMU support */
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000319DEFINE_COPROCR_RW_FUNCS_64(amevcntr00, AMEVCNTR00)
320DEFINE_COPROCR_RW_FUNCS_64(amevcntr01, AMEVCNTR01)
321DEFINE_COPROCR_RW_FUNCS_64(amevcntr02, AMEVCNTR02)
322DEFINE_COPROCR_RW_FUNCS_64(amevcntr03, AMEVCNTR03)
323
Soby Mathewc6820d12016-05-09 17:49:55 +0100324/*
325 * TLBI operation prototypes
326 */
327DEFINE_TLBIOP_FUNC(all, TLBIALL)
328DEFINE_TLBIOP_FUNC(allis, TLBIALLIS)
329DEFINE_TLBIOP_PARAM_FUNC(mva, TLBIMVA)
330DEFINE_TLBIOP_PARAM_FUNC(mvaa, TLBIMVAA)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000331DEFINE_TLBIOP_PARAM_FUNC(mvaais, TLBIMVAAIS)
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100332DEFINE_TLBIOP_PARAM_FUNC(mvahis, TLBIMVAHIS)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000333
334/*
335 * BPI operation prototypes.
336 */
337DEFINE_BPIOP_FUNC(allis, BPIALLIS)
Soby Mathewc6820d12016-05-09 17:49:55 +0100338
339/*
340 * DC operation prototypes
341 */
342DEFINE_DCOP_PARAM_FUNC(civac, DCCIMVAC)
343DEFINE_DCOP_PARAM_FUNC(ivac, DCIMVAC)
Ambroise Vincentf5fdfbc2019-02-21 14:16:24 +0000344#if ERRATA_A53_819472 || ERRATA_A53_824069 || ERRATA_A53_827319
345DEFINE_DCOP_PARAM_FUNC(cvac, DCCIMVAC)
346#else
Soby Mathewc6820d12016-05-09 17:49:55 +0100347DEFINE_DCOP_PARAM_FUNC(cvac, DCCMVAC)
Ambroise Vincentf5fdfbc2019-02-21 14:16:24 +0000348#endif
Soby Mathewc6820d12016-05-09 17:49:55 +0100349
Madhukar Pappireddy90d65322019-10-30 14:24:39 -0500350/*
351 * DynamIQ Shared Unit power management
352 */
353DEFINE_COPROCR_RW_FUNCS(clusterpwrdn, CLUSTERPWRDN)
354
Soby Mathewc6820d12016-05-09 17:49:55 +0100355/* Previously defined accessor functions with incomplete register names */
356#define dsb() dsbsy()
Etienne Carrierea2579862017-11-05 22:57:29 +0100357#define dmb() dmbsy()
Soby Mathewc6820d12016-05-09 17:49:55 +0100358
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100359/* dmb ld is not valid for armv7/thumb machines, so alias it to dmb */
360#if ARM_ARCH_MAJOR == 7
361#define dmbld() dmb()
362#endif
363
Soby Mathewc6820d12016-05-09 17:49:55 +0100364#define IS_IN_SECURE() \
365 (GET_NS_BIT(read_scr()) == 0)
366
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100367#define IS_IN_HYP() (GET_M32(read_cpsr()) == MODE32_hyp)
368#define IS_IN_SVC() (GET_M32(read_cpsr()) == MODE32_svc)
369#define IS_IN_MON() (GET_M32(read_cpsr()) == MODE32_mon)
370#define IS_IN_EL2() IS_IN_HYP()
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000371/* If EL3 is AArch32, then secure PL1 and monitor mode correspond to EL3 */
Soby Mathewc6820d12016-05-09 17:49:55 +0100372#define IS_IN_EL3() \
373 ((GET_M32(read_cpsr()) == MODE32_mon) || \
374 (IS_IN_SECURE() && (GET_M32(read_cpsr()) != MODE32_usr)))
375
Douglas Raillard77414632018-08-21 12:54:45 +0100376static inline unsigned int get_current_el(void)
377{
378 if (IS_IN_EL3()) {
379 return 3U;
380 } else if (IS_IN_EL2()) {
381 return 2U;
382 } else {
383 return 1U;
384 }
385}
386
Soby Mathewc6820d12016-05-09 17:49:55 +0100387/* Macros for compatibility with AArch64 system registers */
388#define read_mpidr_el1() read_mpidr()
389
390#define read_scr_el3() read_scr()
391#define write_scr_el3(_v) write_scr(_v)
392
393#define read_hcr_el2() read_hcr()
394#define write_hcr_el2(_v) write_hcr(_v)
395
396#define read_cpacr_el1() read_cpacr()
397#define write_cpacr_el1(_v) write_cpacr(_v)
398
399#define read_cntfrq_el0() read_cntfrq()
400#define write_cntfrq_el0(_v) write_cntfrq(_v)
401#define read_isr_el1() read_isr()
402
403#define read_cntpct_el0() read64_cntpct()
404
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100405#define read_ctr_el0() read_ctr()
406
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000407#define write_icc_sgi0r_el1(_v) write64_icc_sgi0r_el1(_v)
Florian Lugoud4e25032021-09-08 12:40:24 +0200408#define write_icc_sgi1r(_v) write64_icc_sgi1r(_v)
409#define write_icc_asgi1r(_v) write64_icc_asgi1r(_v)
Antonio Nino Diaz8257f5b2018-11-22 15:53:17 +0000410
411#define read_daif() read_cpsr()
412#define write_daif(flags) write_cpsr(flags)
413
414#define read_cnthp_cval_el2() read64_cnthp_cval_el2()
415#define write_cnthp_cval_el2(v) write64_cnthp_cval_el2(v)
416
417#define read_amcntenset0_el0() read_amcntenset0()
418#define read_amcntenset1_el0() read_amcntenset1()
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100419
Antonio Nino Diazb4e3e4b2018-11-23 15:04:01 +0000420/* Helper functions to manipulate CPSR */
421static inline void enable_irq(void)
422{
423 /*
424 * The compiler memory barrier will prevent the compiler from
425 * scheduling non-volatile memory access after the write to the
426 * register.
427 *
428 * This could happen if some initialization code issues non-volatile
429 * accesses to an area used by an interrupt handler, in the assumption
430 * that it is safe as the interrupts are disabled at the time it does
431 * that (according to program order). However, non-volatile accesses
432 * are not necessarily in program order relatively with volatile inline
433 * assembly statements (and volatile accesses).
434 */
435 COMPILER_BARRIER();
436 __asm__ volatile ("cpsie i");
437 isb();
438}
439
440static inline void enable_serror(void)
441{
442 COMPILER_BARRIER();
443 __asm__ volatile ("cpsie a");
444 isb();
445}
446
447static inline void enable_fiq(void)
448{
449 COMPILER_BARRIER();
450 __asm__ volatile ("cpsie f");
451 isb();
452}
453
454static inline void disable_irq(void)
455{
456 COMPILER_BARRIER();
457 __asm__ volatile ("cpsid i");
458 isb();
459}
460
461static inline void disable_serror(void)
462{
463 COMPILER_BARRIER();
464 __asm__ volatile ("cpsid a");
465 isb();
466}
467
468static inline void disable_fiq(void)
469{
470 COMPILER_BARRIER();
471 __asm__ volatile ("cpsid f");
472 isb();
473}
474
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000475#endif /* ARCH_HELPERS_H */