blob: 7991711ffc41c0d9c77a22e02e901a2d0350310f [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
2 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __ARCH_HELPERS_H__
8#define __ARCH_HELPERS_H__
9
10#include <arch.h> /* for additional register definitions */
11#include <misc_utils.h>
12#include <stdint.h>
13#include <types.h>
14
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.
37 */
38
39#define _DEFINE_COPROCR_WRITE_FUNC_64(_name, coproc, opc1, CRm) \
40static inline void write64_## _name(uint64_t v) \
41{ \
42 __asm__ volatile ("mcrr "#coproc","#opc1", %Q0, %R0,"#CRm : : "r" (v));\
43}
44
45#define _DEFINE_COPROCR_READ_FUNC_64(_name, coproc, opc1, CRm) \
46static inline uint64_t read64_## _name(void) \
47{ uint64_t v; \
48 __asm__ volatile ("mrrc "#coproc","#opc1", %Q0, %R0,"#CRm : "=r" (v));\
49 return v; \
50}
51
52#define _DEFINE_SYSREG_READ_FUNC(_name, _reg_name) \
53static inline u_register_t read_ ## _name(void) \
54{ \
55 u_register_t v; \
56 __asm__ volatile ("mrs %0, " #_reg_name : "=r" (v)); \
57 return v; \
58}
59
60#define _DEFINE_SYSREG_WRITE_FUNC(_name, _reg_name) \
61static inline void write_ ## _name(u_register_t v) \
62{ \
63 __asm__ volatile ("msr " #_reg_name ", %0" : : "r" (v)); \
64}
65
66#define _DEFINE_SYSREG_WRITE_CONST_FUNC(_name, _reg_name) \
67static inline void write_ ## _name(const u_register_t v) \
68{ \
69 __asm__ volatile ("msr " #_reg_name ", %0" : : "i" (v)); \
70}
71
72/* Define read function for coproc register */
73#define DEFINE_COPROCR_READ_FUNC(_name, ...) \
74 _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__)
75
76/* Define read & write function for coproc register */
77#define DEFINE_COPROCR_RW_FUNCS(_name, ...) \
78 _DEFINE_COPROCR_READ_FUNC(_name, __VA_ARGS__) \
79 _DEFINE_COPROCR_WRITE_FUNC(_name, __VA_ARGS__)
80
81/* Define 64 bit read function for coproc register */
82#define DEFINE_COPROCR_READ_FUNC_64(_name, ...) \
83 _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__)
84
85/* Define 64 bit write function for coproc register */
86#define DEFINE_COPROCR_WRITE_FUNC_64(_name, ...) \
87 _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__)
88
89/* Define 64 bit read & write function for coproc register */
90#define DEFINE_COPROCR_RW_FUNCS_64(_name, ...) \
91 _DEFINE_COPROCR_READ_FUNC_64(_name, __VA_ARGS__) \
92 _DEFINE_COPROCR_WRITE_FUNC_64(_name, __VA_ARGS__)
93
94/* Define read & write function for system register */
95#define DEFINE_SYSREG_RW_FUNCS(_name) \
96 _DEFINE_SYSREG_READ_FUNC(_name, _name) \
97 _DEFINE_SYSREG_WRITE_FUNC(_name, _name)
98
99/**********************************************************************
100 * Macros to create inline functions for tlbi operations
101 *********************************************************************/
102
103#define _DEFINE_TLBIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
104static inline void tlbi##_op(void) \
105{ \
106 u_register_t v = 0; \
107 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
108}
109
110#define _DEFINE_TLBIOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
111static inline void tlbi##_op(u_register_t v) \
112{ \
113 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
114}
115
116#define _DEFINE_BPIOP_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
117static inline void bpi##_op(void) \
118{ \
119 u_register_t v = 0; \
120 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
121}
122
123/* Define function for simple TLBI operation */
124#define DEFINE_TLBIOP_FUNC(_op, ...) \
125 _DEFINE_TLBIOP_FUNC(_op, __VA_ARGS__)
126
127/* Define function for TLBI operation with register parameter */
128#define DEFINE_TLBIOP_PARAM_FUNC(_op, ...) \
129 _DEFINE_TLBIOP_PARAM_FUNC(_op, __VA_ARGS__)
130
131/* Define function for simple BPI operation */
132#define DEFINE_BPIOP_FUNC(_op, ...) \
133 _DEFINE_BPIOP_FUNC(_op, __VA_ARGS__)
134
135/**********************************************************************
136 * Macros to create inline functions for DC operations
137 *********************************************************************/
138#define _DEFINE_DCOP_PARAM_FUNC(_op, coproc, opc1, CRn, CRm, opc2) \
139static inline void dc##_op(u_register_t v) \
140{ \
141 __asm__ volatile ("mcr "#coproc","#opc1",%0,"#CRn","#CRm","#opc2 : : "r" (v));\
142}
143
144/* Define function for DC operation with register parameter */
145#define DEFINE_DCOP_PARAM_FUNC(_op, ...) \
146 _DEFINE_DCOP_PARAM_FUNC(_op, __VA_ARGS__)
147
148/**********************************************************************
149 * Macros to create inline functions for system instructions
150 *********************************************************************/
151 /* Define function for simple system instruction */
152#define DEFINE_SYSOP_FUNC(_op) \
153static inline void _op(void) \
154{ \
155 __asm__ (#_op); \
156}
157
158
159/* Define function for system instruction with type specifier */
160#define DEFINE_SYSOP_TYPE_FUNC(_op, _type) \
161static inline void _op ## _type(void) \
162{ \
163 __asm__ (#_op " " #_type); \
164}
165
166/* Define function for system instruction with register parameter */
167#define DEFINE_SYSOP_TYPE_PARAM_FUNC(_op, _type) \
168static inline void _op ## _type(u_register_t v) \
169{ \
170 __asm__ (#_op " " #_type ", %0" : : "r" (v)); \
171}
172
173void flush_dcache_range(uintptr_t addr, size_t size);
174void clean_dcache_range(uintptr_t addr, size_t size);
175void inv_dcache_range(uintptr_t addr, size_t size);
176
177void dcsw_op_louis(u_register_t op_type);
178void dcsw_op_all(u_register_t op_type);
179
180DEFINE_SYSOP_FUNC(wfi)
181DEFINE_SYSOP_FUNC(wfe)
182DEFINE_SYSOP_FUNC(sev)
183DEFINE_SYSOP_TYPE_FUNC(dsb, sy)
184DEFINE_SYSOP_TYPE_FUNC(dmb, sy)
185DEFINE_SYSOP_TYPE_FUNC(dsb, ish)
186DEFINE_SYSOP_TYPE_FUNC(dsb, ishst)
187DEFINE_SYSOP_TYPE_FUNC(dmb, ish)
188DEFINE_SYSOP_TYPE_FUNC(dmb, ishst)
189DEFINE_SYSOP_FUNC(isb)
190
191DEFINE_SYSREG_RW_FUNCS(spsr)
192DEFINE_SYSREG_RW_FUNCS(cpsr)
193
194/*******************************************************************************
195 * System register accessor prototypes
196 ******************************************************************************/
197DEFINE_COPROCR_READ_FUNC(mpidr, MPIDR)
198DEFINE_COPROCR_READ_FUNC(midr, MIDR)
199DEFINE_COPROCR_READ_FUNC(id_pfr0, ID_PFR0)
200DEFINE_COPROCR_READ_FUNC(id_pfr1, ID_PFR1)
201DEFINE_COPROCR_READ_FUNC(isr, ISR)
202DEFINE_COPROCR_READ_FUNC(clidr, CLIDR)
203DEFINE_COPROCR_READ_FUNC_64(cntpct, CNTPCT_64)
204
205DEFINE_COPROCR_RW_FUNCS(scr, SCR)
206DEFINE_COPROCR_RW_FUNCS(sctlr, SCTLR)
207DEFINE_COPROCR_RW_FUNCS(hsctlr, HSCTLR)
208DEFINE_COPROCR_RW_FUNCS(hcr, HCR)
209DEFINE_COPROCR_RW_FUNCS(hcptr, HCPTR)
210DEFINE_COPROCR_RW_FUNCS(cntfrq, CNTFRQ)
211DEFINE_COPROCR_RW_FUNCS(cnthctl, CNTHCTL)
212DEFINE_COPROCR_RW_FUNCS(mair0, MAIR0)
213DEFINE_COPROCR_RW_FUNCS(mair1, MAIR1)
214DEFINE_COPROCR_RW_FUNCS(hmair0, HMAIR0)
215DEFINE_COPROCR_RW_FUNCS(ttbcr, TTBCR)
216DEFINE_COPROCR_RW_FUNCS(htcr, HTCR)
217DEFINE_COPROCR_RW_FUNCS(ttbr0, TTBR0)
218DEFINE_COPROCR_RW_FUNCS_64(ttbr0, TTBR0_64)
219DEFINE_COPROCR_RW_FUNCS(ttbr1, TTBR1)
220DEFINE_COPROCR_RW_FUNCS_64(httbr, HTTBR_64)
221DEFINE_COPROCR_RW_FUNCS(vpidr, VPIDR)
222DEFINE_COPROCR_RW_FUNCS(vmpidr, VMPIDR)
223DEFINE_COPROCR_RW_FUNCS_64(vttbr, VTTBR_64)
224DEFINE_COPROCR_RW_FUNCS_64(ttbr1, TTBR1_64)
225DEFINE_COPROCR_RW_FUNCS_64(cntvoff, CNTVOFF_64)
226DEFINE_COPROCR_RW_FUNCS(csselr, CSSELR)
227DEFINE_COPROCR_RW_FUNCS(cnthp_ctl_el2, CNTHP_CTL)
228DEFINE_COPROCR_RW_FUNCS(cnthp_tval_el2, CNTHP_TVAL)
229DEFINE_COPROCR_RW_FUNCS_64(cnthp_cval_el2, CNTHP_CVAL_64)
230
231DEFINE_COPROCR_RW_FUNCS(icc_sre_el1, ICC_SRE)
232DEFINE_COPROCR_RW_FUNCS(icc_sre_el2, ICC_HSRE)
233DEFINE_COPROCR_RW_FUNCS(icc_sre_el3, ICC_MSRE)
234DEFINE_COPROCR_RW_FUNCS(icc_pmr_el1, ICC_PMR)
235DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el3, ICC_MGRPEN1)
236DEFINE_COPROCR_RW_FUNCS(icc_igrpen1_el1, ICC_IGRPEN1)
237DEFINE_COPROCR_RW_FUNCS(icc_igrpen0_el1, ICC_IGRPEN0)
238DEFINE_COPROCR_RW_FUNCS(icc_hppir0_el1, ICC_HPPIR0)
239DEFINE_COPROCR_RW_FUNCS(icc_hppir1_el1, ICC_HPPIR1)
240DEFINE_COPROCR_RW_FUNCS(icc_iar0_el1, ICC_IAR0)
241DEFINE_COPROCR_RW_FUNCS(icc_iar1_el1, ICC_IAR1)
242DEFINE_COPROCR_RW_FUNCS(icc_eoir0_el1, ICC_EOIR0)
243DEFINE_COPROCR_RW_FUNCS(icc_eoir1_el1, ICC_EOIR1)
244DEFINE_COPROCR_WRITE_FUNC_64(icc_sgi1r, ICC_SGI1R_EL1_64)
245
246DEFINE_COPROCR_RW_FUNCS(amcntenset0, AMCNTENSET0)
247DEFINE_COPROCR_RW_FUNCS(amcntenset1, AMCNTENSET1)
248DEFINE_COPROCR_RW_FUNCS(amcntenclr0, AMCNTENCLR0)
249DEFINE_COPROCR_RW_FUNCS(amcntenclr1, AMCNTENCLR1)
250
251DEFINE_COPROCR_RW_FUNCS_64(amevcntr00, AMEVCNTR00)
252DEFINE_COPROCR_RW_FUNCS_64(amevcntr01, AMEVCNTR01)
253DEFINE_COPROCR_RW_FUNCS_64(amevcntr02, AMEVCNTR02)
254DEFINE_COPROCR_RW_FUNCS_64(amevcntr03, AMEVCNTR03)
255
256/*
257 * TLBI operation prototypes
258 */
259DEFINE_TLBIOP_FUNC(all, TLBIALL)
260DEFINE_TLBIOP_FUNC(allis, TLBIALLIS)
261DEFINE_TLBIOP_PARAM_FUNC(mva, TLBIMVA)
262DEFINE_TLBIOP_PARAM_FUNC(mvaa, TLBIMVAA)
263DEFINE_TLBIOP_PARAM_FUNC(mvaais, TLBIMVAAIS)
264DEFINE_TLBIOP_PARAM_FUNC(mvahis, TLBIMVAHIS)
265
266/*
267 * BPI operation prototypes.
268 */
269DEFINE_BPIOP_FUNC(allis, BPIALLIS)
270
271/*
272 * DC operation prototypes
273 */
274DEFINE_DCOP_PARAM_FUNC(civac, DCCIMVAC)
275DEFINE_DCOP_PARAM_FUNC(ivac, DCIMVAC)
276DEFINE_DCOP_PARAM_FUNC(cvac, DCCMVAC)
277
278/* Helper functions to manipulate CPSR */
279static inline void enable_irq(void)
280{
281 /*
282 * The compiler memory barrier will prevent the compiler from
283 * scheduling non-volatile memory access after the write to the
284 * register.
285 *
286 * This could happen if some initialization code issues non-volatile
287 * accesses to an area used by an interrupt handler, in the assumption
288 * that it is safe as the interrupts are disabled at the time it does
289 * that (according to program order). However, non-volatile accesses
290 * are not necessarily in program order relatively with volatile inline
291 * assembly statements (and volatile accesses).
292 */
293 COMPILER_BARRIER();
294 __asm__ volatile ("cpsie i");
295 isb();
296}
297
298static inline void enable_serror(void)
299{
300 COMPILER_BARRIER();
301 __asm__ volatile ("cpsie a");
302 isb();
303}
304
305static inline void enable_fiq(void)
306{
307 COMPILER_BARRIER();
308 __asm__ volatile ("cpsie f");
309 isb();
310}
311
312static inline void disable_irq(void)
313{
314 COMPILER_BARRIER();
315 __asm__ volatile ("cpsid i");
316 isb();
317}
318
319static inline void disable_serror(void)
320{
321 COMPILER_BARRIER();
322 __asm__ volatile ("cpsid a");
323 isb();
324}
325
326static inline void disable_fiq(void)
327{
328 COMPILER_BARRIER();
329 __asm__ volatile ("cpsid f");
330 isb();
331}
332
333/* Previously defined accessor functions with incomplete register names */
334#define dsb() dsbsy()
335
336/*
337 * Helper function to detect the processor mode.
338 */
339#define IS_IN_HYP() (GET_M32(read_cpsr()) == MODE32_hyp)
340#define IS_IN_SVC() (GET_M32(read_cpsr()) == MODE32_svc)
341#define IS_IN_MON() (GET_M32(read_cpsr()) == MODE32_mon)
342#define IS_IN_EL2() IS_IN_HYP()
343
344/* Accessor functions defined for compatibility with AArch32 register names */
345
346#define read_mpidr_el1() read_mpidr()
347#define read_daif() read_cpsr()
348#define write_daif(flags) write_cpsr(flags)
349#define read_cntfrq_el0() read_cntfrq()
350#define read_cntpct_el0() read64_cntpct()
351#define read_cnthp_cval_el2() read64_cnthp_cval_el2()
352#define write_cnthp_cval_el2(v) write64_cnthp_cval_el2(v)
353#define read_amcntenset0_el0() read_amcntenset0()
354#define read_amcntenset1_el0() read_amcntenset1()
355
356void disable_mmu_icache(void);
357
358#endif /* __ARCH_HELPERS_H__ */