blob: 1f69bb2b7dbefd9f18ac200331d7b39453e56ec4 [file] [log] [blame]
Soby Mathewc6820d12016-05-09 17:49:55 +01001/*
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +01002 * Copyright (c) 2016-2018, 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
10#include <arch.h> /* for additional register definitions */
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010011#include <cdefs.h>
Soby Mathewc6820d12016-05-09 17:49:55 +010012#include <stdint.h>
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010013#include <string.h>
Soby Mathewc6820d12016-05-09 17:49:55 +010014
15/**********************************************************************
16 * Macros which create inline functions to read or write CPU system
17 * registers
18 *********************************************************************/
19
20#define _DEFINE_COPROCR_WRITE_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \
21static inline void write_## _name(u_register_t v) \
22{ \
23 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
24}
25
26#define _DEFINE_COPROCR_READ_FUNC(_name, coproc, opc1, CRn, CRm, opc2) \
27static inline u_register_t read_ ## _name(void) \
28{ \
29 u_register_t v; \
30 __asm__ volatile ("mrc "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : "=r" (v));\
31 return v; \
32}
33
34/*
35 * The undocumented %Q and %R extended asm are used to implemented the below
36 * 64 bit `mrrc` and `mcrr` instructions. It works only on Little Endian
37 * systems for GCC versions < 4.6. Above GCC 4.6, both Little Endian and
38 * Big Endian systems generate the right instruction encoding.
39 */
dp-arm320e8442017-05-02 12:00:08 +010040#if !(__clang__ || __GNUC__ > (4) || __GNUC__ == (4) && __GNUC_MINOR__ >= (6))
41#error "clang or GCC 4.6 or above is required to build AArch32 Trusted Firmware"
Soby Mathewc6820d12016-05-09 17:49:55 +010042#endif
43
44#define _DEFINE_COPROCR_WRITE_FUNC_64(_name, coproc, opc1, CRm) \
45static inline void write64_## _name(uint64_t v) \
46{ \
47 __asm__ volatile ("mcrr "#coproc","#opc1", %Q0, %R0,"#CRm : : "r" (v));\
48}
49
50#define _DEFINE_COPROCR_READ_FUNC_64(_name, coproc, opc1, CRm) \
51static inline uint64_t read64_## _name(void) \
52{ uint64_t v; \
53 __asm__ volatile ("mrrc "#coproc","#opc1", %Q0, %R0,"#CRm : "=r" (v));\
54 return v; \
55}
56
57#define _DEFINE_SYSREG_READ_FUNC(_name, _reg_name) \
58static inline u_register_t read_ ## _name(void) \
59{ \
60 u_register_t v; \
61 __asm__ volatile ("mrs %0, " #_reg_name : "=r" (v)); \
62 return v; \
63}
64
65#define _DEFINE_SYSREG_WRITE_FUNC(_name, _reg_name) \
66static inline void write_ ## _name(u_register_t v) \
67{ \
68 __asm__ volatile ("msr " #_reg_name ", %0" : : "r" (v)); \
69}
70
71#define _DEFINE_SYSREG_WRITE_CONST_FUNC(_name, _reg_name) \
72static inline void write_ ## _name(const u_register_t v) \
73{ \
74 __asm__ volatile ("msr " #_reg_name ", %0" : : "i" (v)); \
75}
76
77/* Define read function for coproc register */
78#define DEFINE_COPROCR_READ_FUNC(_name, ...) \
79 _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__)
80
81/* 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
90/* Define 64 bit read & write function for coproc register */
91#define DEFINE_COPROCR_RW_FUNCS_64(_name, ...) \
92 _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) \
93 _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__)
94
95/* Define read & write function for system register */
96#define DEFINE_SYSREG_RW_FUNCS(_name) \
97 _DEFINE_SYSREG_READ_FUNC(_name, _name) \
98 _DEFINE_SYSREG_WRITE_FUNC(_name, _name)
99
100/**********************************************************************
101 * Macros to create inline functions for tlbi operations
102 *********************************************************************/
103
104#define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
105static inline void tlbi##_op(void) \
106{ \
107 u_register_t v = 0; \
108 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
109}
110
Antonio Nino Diazab37d152018-11-22 15:38:05 +0000111#define _DEFINE_BPIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
112static inline void bpi##_op(void) \
Antonio Nino Diazac998032017-02-27 17:23:54 +0000113{ \
114 u_register_t v = 0; \
115 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
116}
117
Soby Mathewc6820d12016-05-09 17:49:55 +0100118#define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
119static inline void tlbi##_op(u_register_t v) \
120{ \
121 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
122}
123
124/* Define function for simple TLBI operation */
125#define DEFINE_TLBIOP_FUNC(_op, ...) \
126 _DEFINE_TLBIOP_FUNC(_op, __VA_ARGS__)
127
128/* Define function for TLBI operation with register parameter */
129#define DEFINE_TLBIOP_PARAM_FUNC(_op, ...) \
130 _DEFINE_TLBIOP_PARAM_FUNC(_op, __VA_ARGS__)
131
Antonio Nino Diazac998032017-02-27 17:23:54 +0000132/* Define function for simple BPI operation */
133#define DEFINE_BPIOP_FUNC(_op, ...) \
134 _DEFINE_BPIOP_FUNC(_op, __VA_ARGS__)
135
Soby Mathewc6820d12016-05-09 17:49:55 +0100136/**********************************************************************
137 * Macros to create inline functions for DC operations
138 *********************************************************************/
139#define _DEFINE_DCOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
140static inline void dc##_op(u_register_t v) \
141{ \
142 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
143}
144
145/* Define function for DC operation with register parameter */
146#define DEFINE_DCOP_PARAM_FUNC(_op, ...) \
147 _DEFINE_DCOP_PARAM_FUNC(_op, __VA_ARGS__)
148
149/**********************************************************************
150 * Macros to create inline functions for system instructions
151 *********************************************************************/
152 /* Define function for simple system instruction */
153#define DEFINE_SYSOP_FUNC(_op) \
154static inline void _op(void) \
155{ \
156 __asm__ (#_op); \
157}
158
159
160/* Define function for system instruction with type specifier */
161#define DEFINE_SYSOP_TYPE_FUNC(_op, _type) \
162static inline void _op ## _type(void) \
163{ \
164 __asm__ (#_op " " #_type); \
165}
166
167/* Define function for system instruction with register parameter */
168#define DEFINE_SYSOP_TYPE_PARAM_FUNC(_op, _type) \
169static inline void _op ## _type(u_register_t v) \
170{ \
171 __asm__ (#_op " " #_type ", %0" : : "r" (v)); \
172}
173
174void flush_dcache_range(uintptr_t addr, size_t size);
175void clean_dcache_range(uintptr_t addr, size_t size);
176void inv_dcache_range(uintptr_t addr, size_t size);
177
Antonio Nino Diaze40306b2017-01-13 15:03:07 +0000178void dcsw_op_louis(u_register_t op_type);
179void dcsw_op_all(u_register_t op_type);
180
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100181void disable_mmu_secure(void);
182void disable_mmu_icache_secure(void);
183
Soby Mathewc6820d12016-05-09 17:49:55 +0100184DEFINE_SYSOP_FUNC(wfi)
185DEFINE_SYSOP_FUNC(wfe)
186DEFINE_SYSOP_FUNC(sev)
187DEFINE_SYSOP_TYPE_FUNC(dsb, sy)
188DEFINE_SYSOP_TYPE_FUNC(dmb, sy)
Yatharth Kochar2694cba2016-11-14 12:00:41 +0000189DEFINE_SYSOP_TYPE_FUNC(dmb, st)
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100190
191/* dmb ld is not valid for armv7/thumb machines */
192#if ARM_ARCH_MAJOR != 7
Yatharth Kochar2694cba2016-11-14 12:00:41 +0000193DEFINE_SYSOP_TYPE_FUNC(dmb, ld)
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100194#endif
195
Soby Mathewc6820d12016-05-09 17:49:55 +0100196DEFINE_SYSOP_TYPE_FUNC(dsb, ish)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000197DEFINE_SYSOP_TYPE_FUNC(dsb, ishst)
Soby Mathewc6820d12016-05-09 17:49:55 +0100198DEFINE_SYSOP_TYPE_FUNC(dmb, ish)
Jeenu Viswambharan62505072017-09-22 08:32:09 +0100199DEFINE_SYSOP_TYPE_FUNC(dmb, ishst)
Soby Mathewc6820d12016-05-09 17:49:55 +0100200DEFINE_SYSOP_FUNC(isb)
201
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100202void __dead2 smc(uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3,
203 uint32_t r4, uint32_t r5, uint32_t r6, uint32_t r7);
204
Soby Mathewc6820d12016-05-09 17:49:55 +0100205DEFINE_SYSREG_RW_FUNCS(spsr)
206DEFINE_SYSREG_RW_FUNCS(cpsr)
207
208/*******************************************************************************
209 * System register accessor prototypes
210 ******************************************************************************/
211DEFINE_COPROCR_READ_FUNC(mpidr, MPIDR)
212DEFINE_COPROCR_READ_FUNC(midr, MIDR)
Dimitris Papastamosdda48b02017-10-17 14:03:14 +0100213DEFINE_COPROCR_READ_FUNC(id_pfr0, ID_PFR0)
Soby Mathewc6820d12016-05-09 17:49:55 +0100214DEFINE_COPROCR_READ_FUNC(id_pfr1, ID_PFR1)
215DEFINE_COPROCR_READ_FUNC(isr, ISR)
216DEFINE_COPROCR_READ_FUNC(clidr, CLIDR)
217DEFINE_COPROCR_READ_FUNC_64(cntpct, CNTPCT_64)
218
219DEFINE_COPROCR_RW_FUNCS(scr, SCR)
220DEFINE_COPROCR_RW_FUNCS(ctr, CTR)
221DEFINE_COPROCR_RW_FUNCS(sctlr, SCTLR)
Etienne Carriere70a004b2017-11-05 22:56:03 +0100222DEFINE_COPROCR_RW_FUNCS(actlr, ACTLR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100223DEFINE_COPROCR_RW_FUNCS(hsctlr, HSCTLR)
224DEFINE_COPROCR_RW_FUNCS(hcr, HCR)
225DEFINE_COPROCR_RW_FUNCS(hcptr, HCPTR)
226DEFINE_COPROCR_RW_FUNCS(cntfrq, CNTFRQ)
227DEFINE_COPROCR_RW_FUNCS(cnthctl, CNTHCTL)
228DEFINE_COPROCR_RW_FUNCS(mair0, MAIR0)
229DEFINE_COPROCR_RW_FUNCS(mair1, MAIR1)
230DEFINE_COPROCR_RW_FUNCS(ttbcr, TTBCR)
231DEFINE_COPROCR_RW_FUNCS(ttbr0, TTBR0)
232DEFINE_COPROCR_RW_FUNCS_64(ttbr0, TTBR0_64)
233DEFINE_COPROCR_RW_FUNCS(ttbr1, TTBR1)
234DEFINE_COPROCR_RW_FUNCS(vpidr, VPIDR)
235DEFINE_COPROCR_RW_FUNCS(vmpidr, VMPIDR)
236DEFINE_COPROCR_RW_FUNCS_64(vttbr, VTTBR_64)
237DEFINE_COPROCR_RW_FUNCS_64(ttbr1, TTBR1_64)
238DEFINE_COPROCR_RW_FUNCS_64(cntvoff, CNTVOFF_64)
239DEFINE_COPROCR_RW_FUNCS(csselr, CSSELR)
David Cunadofee86532017-04-13 22:38:29 +0100240DEFINE_COPROCR_RW_FUNCS(hstr, HSTR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100241
242DEFINE_COPROCR_RW_FUNCS(icc_sre_el1, ICC_SRE)
243DEFINE_COPROCR_RW_FUNCS(icc_sre_el2, ICC_HSRE)
244DEFINE_COPROCR_RW_FUNCS(icc_sre_el3, ICC_MSRE)
245DEFINE_COPROCR_RW_FUNCS(icc_pmr_el1, ICC_PMR)
Jeenu Viswambharanb1e957e2017-09-22 08:32:09 +0100246DEFINE_COPROCR_RW_FUNCS(icc_rpr_el1, ICC_RPR)
Soby Mathewc6820d12016-05-09 17:49:55 +0100247DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el3, ICC_MGRPEN1)
248DEFINE_COPROCR_RW_FUNCS(icc_igrpen0_el1, ICC_IGRPEN0)
249DEFINE_COPROCR_RW_FUNCS(icc_hppir0_el1, ICC_HPPIR0)
250DEFINE_COPROCR_RW_FUNCS(icc_hppir1_el1, ICC_HPPIR1)
251DEFINE_COPROCR_RW_FUNCS(icc_iar0_el1, ICC_IAR0)
252DEFINE_COPROCR_RW_FUNCS(icc_iar1_el1, ICC_IAR1)
253DEFINE_COPROCR_RW_FUNCS(icc_eoir0_el1, ICC_EOIR0)
254DEFINE_COPROCR_RW_FUNCS(icc_eoir1_el1, ICC_EOIR1)
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100255DEFINE_COPROCR_RW_FUNCS_64(icc_sgi0r_el1, ICC_SGI0R_EL1_64)
Soby Mathewc6820d12016-05-09 17:49:55 +0100256
David Cunado5f55e282016-10-31 17:37:34 +0000257DEFINE_COPROCR_RW_FUNCS(hdcr, HDCR)
David Cunadoc14b08e2016-11-25 00:21:59 +0000258DEFINE_COPROCR_RW_FUNCS(cnthp_ctl, CNTHP_CTL)
David Cunado5f55e282016-10-31 17:37:34 +0000259DEFINE_COPROCR_READ_FUNC(pmcr, PMCR)
260
Douglas Raillard77414632018-08-21 12:54:45 +0100261DEFINE_COPROCR_RW_FUNCS(ats1cpr, ATS1CPR)
262DEFINE_COPROCR_RW_FUNCS(ats1hr, ATS1HR)
263DEFINE_COPROCR_RW_FUNCS_64(par, PAR_64)
264
Etienne Carriere70a004b2017-11-05 22:56:03 +0100265DEFINE_COPROCR_RW_FUNCS(nsacr, NSACR)
266
267/* AArch32 coproc registers for 32bit MMU descriptor support */
268DEFINE_COPROCR_RW_FUNCS(prrr, PRRR)
269DEFINE_COPROCR_RW_FUNCS(nmrr, NMRR)
270DEFINE_COPROCR_RW_FUNCS(dacr, DACR)
271
Dimitris Papastamosdda48b02017-10-17 14:03:14 +0100272DEFINE_COPROCR_RW_FUNCS(amcntenset0, AMCNTENSET0)
273DEFINE_COPROCR_RW_FUNCS(amcntenset1, AMCNTENSET1)
274DEFINE_COPROCR_RW_FUNCS(amcntenclr0, AMCNTENCLR0)
275DEFINE_COPROCR_RW_FUNCS(amcntenclr1, AMCNTENCLR1)
276
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000277DEFINE_COPROCR_RW_FUNCS_64(amevcntr00, AMEVCNTR00)
278DEFINE_COPROCR_RW_FUNCS_64(amevcntr01, AMEVCNTR01)
279DEFINE_COPROCR_RW_FUNCS_64(amevcntr02, AMEVCNTR02)
280DEFINE_COPROCR_RW_FUNCS_64(amevcntr03, AMEVCNTR03)
281
Soby Mathewc6820d12016-05-09 17:49:55 +0100282/*
283 * TLBI operation prototypes
284 */
285DEFINE_TLBIOP_FUNC(all, TLBIALL)
286DEFINE_TLBIOP_FUNC(allis, TLBIALLIS)
287DEFINE_TLBIOP_PARAM_FUNC(mva, TLBIMVA)
288DEFINE_TLBIOP_PARAM_FUNC(mvaa, TLBIMVAA)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000289DEFINE_TLBIOP_PARAM_FUNC(mvaais, TLBIMVAAIS)
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100290DEFINE_TLBIOP_PARAM_FUNC(mvahis, TLBIMVAHIS)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000291
292/*
293 * BPI operation prototypes.
294 */
295DEFINE_BPIOP_FUNC(allis, BPIALLIS)
Soby Mathewc6820d12016-05-09 17:49:55 +0100296
297/*
298 * DC operation prototypes
299 */
300DEFINE_DCOP_PARAM_FUNC(civac, DCCIMVAC)
301DEFINE_DCOP_PARAM_FUNC(ivac, DCIMVAC)
302DEFINE_DCOP_PARAM_FUNC(cvac, DCCMVAC)
303
304/* Previously defined accessor functions with incomplete register names */
305#define dsb() dsbsy()
Etienne Carrierea2579862017-11-05 22:57:29 +0100306#define dmb() dmbsy()
Soby Mathewc6820d12016-05-09 17:49:55 +0100307
Jeenu Viswambharan1aa2db32018-09-05 14:23:27 +0100308/* dmb ld is not valid for armv7/thumb machines, so alias it to dmb */
309#if ARM_ARCH_MAJOR == 7
310#define dmbld() dmb()
311#endif
312
Soby Mathewc6820d12016-05-09 17:49:55 +0100313#define IS_IN_SECURE() \
314 (GET_NS_BIT(read_scr()) == 0)
315
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100316#define IS_IN_HYP() (GET_M32(read_cpsr()) == MODE32_hyp)
317#define IS_IN_SVC() (GET_M32(read_cpsr()) == MODE32_svc)
318#define IS_IN_MON() (GET_M32(read_cpsr()) == MODE32_mon)
319#define IS_IN_EL2() IS_IN_HYP()
Soby Mathewc6820d12016-05-09 17:49:55 +0100320 /*
321 * If EL3 is AArch32, then secure PL1 and monitor mode correspond to EL3
322 */
323#define IS_IN_EL3() \
324 ((GET_M32(read_cpsr()) == MODE32_mon) || \
325 (IS_IN_SECURE() && (GET_M32(read_cpsr()) != MODE32_usr)))
326
Douglas Raillard77414632018-08-21 12:54:45 +0100327static inline unsigned int get_current_el(void)
328{
329 if (IS_IN_EL3()) {
330 return 3U;
331 } else if (IS_IN_EL2()) {
332 return 2U;
333 } else {
334 return 1U;
335 }
336}
337
Soby Mathewc6820d12016-05-09 17:49:55 +0100338/* Macros for compatibility with AArch64 system registers */
339#define read_mpidr_el1() read_mpidr()
340
341#define read_scr_el3() read_scr()
342#define write_scr_el3(_v) write_scr(_v)
343
344#define read_hcr_el2() read_hcr()
345#define write_hcr_el2(_v) write_hcr(_v)
346
347#define read_cpacr_el1() read_cpacr()
348#define write_cpacr_el1(_v) write_cpacr(_v)
349
350#define read_cntfrq_el0() read_cntfrq()
351#define write_cntfrq_el0(_v) write_cntfrq(_v)
352#define read_isr_el1() read_isr()
353
354#define read_cntpct_el0() read64_cntpct()
355
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100356#define read_ctr_el0() read_ctr()
357
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100358#define write_icc_sgi0r_el1(_v) \
359 write64_icc_sgi0r_el1(_v)
360
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000361#endif /* ARCH_HELPERS_H */