Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
lib_helpers.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_ARCH_ARM64_LIB_HELPERS_H_
8#define ZEPHYR_INCLUDE_ARCH_ARM64_LIB_HELPERS_H_
9
10#ifndef _ASMLANGUAGE
11
12#include <arch/arm64/cpu.h>
13#include <stdint.h>
14
15/* All the macros need a memory clobber */
16
17#define read_sysreg(reg) \
18({ \
19 uint64_t val; \
20 __asm__ volatile ("mrs %0, " STRINGIFY(reg) \
21 : "=r" (val) :: "memory"); \
22 val; \
23})
24
25#define write_sysreg(val, reg) \
26({ \
27 __asm__ volatile ("msr " STRINGIFY(reg) ", %0" \
28 :: "r" (val) : "memory"); \
29})
30
31#define zero_sysreg(reg) \
32({ \
33 __asm__ volatile ("msr " STRINGIFY(reg) ", xzr" \
34 ::: "memory"); \
35})
36
37#define MAKE_REG_HELPER(reg) \
38 static ALWAYS_INLINE uint64_t read_##reg(void) \
39 { \
40 return read_sysreg(reg); \
41 } \
42 static ALWAYS_INLINE void write_##reg(uint64_t val) \
43 { \
44 write_sysreg(val, reg); \
45 } \
46 static ALWAYS_INLINE void zero_##reg(void) \
47 { \
48 zero_sysreg(reg); \
49 }
50
51#define MAKE_REG_HELPER_EL123(reg) \
52 MAKE_REG_HELPER(reg##_el1) \
53 MAKE_REG_HELPER(reg##_el2) \
54 MAKE_REG_HELPER(reg##_el3)
55
56MAKE_REG_HELPER(cntfrq_el0);
57MAKE_REG_HELPER(cnthctl_el2);
58MAKE_REG_HELPER(cnthp_ctl_el2);
59MAKE_REG_HELPER(cntv_ctl_el0)
60MAKE_REG_HELPER(cntv_cval_el0)
61MAKE_REG_HELPER(cntvct_el0);
62MAKE_REG_HELPER(cntvoff_el2);
63MAKE_REG_HELPER(currentel);
66MAKE_REG_HELPER(id_aa64pfr0_el1);
67MAKE_REG_HELPER(id_aa64mmfr0_el1);
69MAKE_REG_HELPER(tpidrro_el0);
70MAKE_REG_HELPER(clidr_el1);
71MAKE_REG_HELPER(csselr_el1);
72MAKE_REG_HELPER(ccsidr_el1);
73MAKE_REG_HELPER(vmpidr_el2);
74MAKE_REG_HELPER(mpidr_el1);
75
88
89#if defined(CONFIG_ARM_MPU)
90/* Armv8-R aarch64 mpu registers */
91#define mpuir_el1 S3_0_c0_c0_4
92#define prselr_el1 S3_0_c6_c2_1
93#define prbar_el1 S3_0_c6_c8_0
94#define prlar_el1 S3_0_c6_c8_1
95
96MAKE_REG_HELPER(mpuir_el1);
97MAKE_REG_HELPER(prselr_el1);
98MAKE_REG_HELPER(prbar_el1);
99MAKE_REG_HELPER(prlar_el1);
100#endif
101
103{
104 __asm__ volatile ("msr DAIFClr, %0"
105 :: "i" (DAIFCLR_DBG_BIT) : "memory");
106}
107
109{
110 __asm__ volatile ("msr DAIFSet, %0"
111 :: "i" (DAIFSET_DBG_BIT) : "memory");
112}
113
115{
116 __asm__ volatile ("msr DAIFClr, %0"
117 :: "i" (DAIFCLR_ABT_BIT) : "memory");
118}
119
121{
122 __asm__ volatile ("msr DAIFSet, %0"
123 :: "i" (DAIFSET_ABT_BIT) : "memory");
124}
125
126static ALWAYS_INLINE void enable_irq(void)
127{
128 __asm__ volatile ("msr DAIFClr, %0"
129 :: "i" (DAIFCLR_IRQ_BIT) : "memory");
130}
131
132static ALWAYS_INLINE void disable_irq(void)
133{
134 __asm__ volatile ("msr DAIFSet, %0"
135 :: "i" (DAIFSET_IRQ_BIT) : "memory");
136}
137
138static ALWAYS_INLINE void enable_fiq(void)
139{
140 __asm__ volatile ("msr DAIFClr, %0"
141 :: "i" (DAIFCLR_FIQ_BIT) : "memory");
142}
143
144static ALWAYS_INLINE void disable_fiq(void)
145{
146 __asm__ volatile ("msr DAIFSet, %0"
147 :: "i" (DAIFSET_FIQ_BIT) : "memory");
148}
149
150#define sev() __asm__ volatile("sev" : : : "memory")
151#define wfe() __asm__ volatile("wfe" : : : "memory")
152#define wfi() __asm__ volatile("wfi" : : : "memory")
153
154#define dsb() __asm__ volatile ("dsb sy" ::: "memory")
155#define dmb() __asm__ volatile ("dmb sy" ::: "memory")
156#define isb() __asm__ volatile ("isb" ::: "memory")
157
158/* Zephyr needs these as well */
159#define __ISB() isb()
160#define __DMB() dmb()
161#define __DSB() dsb()
162
163static inline bool is_el_implemented(unsigned int el)
164{
165 unsigned int shift;
166
167 if (el > 3) {
168 return false;
169 }
170
171 shift = ID_AA64PFR0_EL1_SHIFT * el;
172
173 return (((read_id_aa64pfr0_el1() >> shift) & ID_AA64PFR0_ELX_MASK) != 0U);
174}
175
176static inline bool is_el_highest_implemented(void)
177{
178 uint32_t el_highest;
179 uint32_t curr_el;
180
181 el_highest = read_id_aa64pfr0_el1() & 0xFFFF;
182 el_highest = (31U - __builtin_clz(el_highest)) / 4;
183
184 curr_el = GET_EL(read_currentel());
185
186 if (curr_el < el_highest)
187 return false;
188
189 return true;
190}
191
192static inline bool is_el2_sec_supported(void)
193{
195 ID_AA64PFR0_SEL2_MASK) != 0U);
196}
197
198static inline bool is_in_secure_state(void)
199{
200 /* We cannot read SCR_EL3 from EL2 or EL1 */
201 return !IS_ENABLED(CONFIG_ARMV8_A_NS);
202}
203
204#endif /* !_ASMLANGUAGE */
205
206#endif /* ZEPHYR_INCLUDE_ARCH_ARM64_LIB_HELPERS_H_ */
#define ALWAYS_INLINE
Definition: common.h:116
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:101
#define GET_EL(_mode)
Definition: cpu.h:91
#define DAIFCLR_ABT_BIT
Definition: cpu.h:19
#define DAIFSET_IRQ_BIT
Definition: cpu.h:13
#define ID_AA64PFR0_SEL2_MASK
Definition: cpu.h:113
#define DAIFSET_ABT_BIT
Definition: cpu.h:14
#define DAIFSET_FIQ_BIT
Definition: cpu.h:12
#define DAIFCLR_IRQ_BIT
Definition: cpu.h:18
#define ID_AA64PFR0_SEL2_SHIFT
Definition: cpu.h:112
#define ID_AA64PFR0_EL1_SHIFT
Definition: cpu.h:108
#define DAIFCLR_FIQ_BIT
Definition: cpu.h:17
#define ID_AA64PFR0_ELX_MASK
Definition: cpu.h:111
#define DAIFSET_DBG_BIT
Definition: cpu.h:15
#define DAIFCLR_DBG_BIT
Definition: cpu.h:20
static ALWAYS_INLINE void disable_serror_exceptions(void)
Definition: lib_helpers.h:120
static ALWAYS_INLINE uint64_t read_id_aa64pfr0_el1(void)
Definition: lib_helpers.h:66
#define MAKE_REG_HELPER(reg)
Definition: lib_helpers.h:37
static ALWAYS_INLINE void disable_debug_exceptions(void)
Definition: lib_helpers.h:108
static bool is_el_implemented(unsigned int el)
Definition: lib_helpers.h:163
static bool is_el_highest_implemented(void)
Definition: lib_helpers.h:176
static ALWAYS_INLINE void disable_irq(void)
Definition: lib_helpers.h:132
static ALWAYS_INLINE void enable_irq(void)
Definition: lib_helpers.h:126
static ALWAYS_INLINE void disable_fiq(void)
Definition: lib_helpers.h:144
static ALWAYS_INLINE void enable_debug_exceptions(void)
Definition: lib_helpers.h:102
#define MAKE_REG_HELPER_EL123(reg)
Definition: lib_helpers.h:51
static ALWAYS_INLINE void enable_serror_exceptions(void)
Definition: lib_helpers.h:114
static ALWAYS_INLINE uint64_t read_currentel(void)
Definition: lib_helpers.h:63
static ALWAYS_INLINE void enable_fiq(void)
Definition: lib_helpers.h:138
static bool is_in_secure_state(void)
Definition: lib_helpers.h:198
static bool is_el2_sec_supported(void)
Definition: lib_helpers.h:192
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60