Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
13#include <stdint.h>
14
15/* All the macros need a memory clobber */
16
17#define read_sysreg(reg) \
18({ \
19 uint64_t reg_val; \
20 __asm__ volatile ("mrs %0, " STRINGIFY(reg) \
21 : "=r" (reg_val) :: "memory"); \
22 reg_val; \
23})
24
25#define write_sysreg(val, reg) \
26({ \
27 uint64_t reg_val = val; \
28 __asm__ volatile ("msr " STRINGIFY(reg) ", %0" \
29 :: "r" (reg_val) : "memory"); \
30})
31
32#define zero_sysreg(reg) \
33({ \
34 __asm__ volatile ("msr " STRINGIFY(reg) ", xzr" \
35 ::: "memory"); \
36})
37
38#define MAKE_REG_HELPER(reg) \
39 static ALWAYS_INLINE uint64_t read_##reg(void) \
40 { \
41 return read_sysreg(reg); \
42 } \
43 static ALWAYS_INLINE void write_##reg(uint64_t val) \
44 { \
45 write_sysreg(val, reg); \
46 } \
47 static ALWAYS_INLINE void zero_##reg(void) \
48 { \
49 zero_sysreg(reg); \
50 }
51
52#define MAKE_REG_HELPER_EL123(reg) \
53 MAKE_REG_HELPER(reg##_el1) \
54 MAKE_REG_HELPER(reg##_el2) \
55 MAKE_REG_HELPER(reg##_el3)
56
57MAKE_REG_HELPER(ccsidr_el1);
58MAKE_REG_HELPER(clidr_el1);
59MAKE_REG_HELPER(cntfrq_el0);
60MAKE_REG_HELPER(cnthctl_el2);
61MAKE_REG_HELPER(cnthp_ctl_el2);
62MAKE_REG_HELPER(cnthps_ctl_el2);
63MAKE_REG_HELPER(cntv_ctl_el0)
64MAKE_REG_HELPER(cntv_cval_el0)
65MAKE_REG_HELPER(cntvct_el0);
66MAKE_REG_HELPER(cntvoff_el2);
67MAKE_REG_HELPER(currentel);
68MAKE_REG_HELPER(csselr_el1);
71MAKE_REG_HELPER(id_aa64pfr0_el1);
72MAKE_REG_HELPER(id_aa64pfr1_el1);
73MAKE_REG_HELPER(id_aa64mmfr0_el1);
74MAKE_REG_HELPER(id_aa64isar0_el1);
75MAKE_REG_HELPER(id_aa64isar1_el1);
76MAKE_REG_HELPER(id_aa64isar2_el1);
77MAKE_REG_HELPER(mpidr_el1);
79#if !defined(CONFIG_ARMV8_R)
81#endif /* CONFIG_ARMV8_R */
82MAKE_REG_HELPER(tpidrro_el0);
83MAKE_REG_HELPER(vmpidr_el2);
85
99
100#if defined(CONFIG_ARM_MPU)
101/* Armv8-R aarch64 mpu registers */
102#define mpuir_el1 S3_0_c0_c0_4
103#define prselr_el1 S3_0_c6_c2_1
104#define prbar_el1 S3_0_c6_c8_0
105#define prlar_el1 S3_0_c6_c8_1
106
107MAKE_REG_HELPER(mpuir_el1);
108MAKE_REG_HELPER(prselr_el1);
109MAKE_REG_HELPER(prbar_el1);
110MAKE_REG_HELPER(prlar_el1);
111#endif
112
114{
115 __asm__ volatile ("msr DAIFClr, %0"
116 :: "i" (DAIFCLR_DBG_BIT) : "memory");
117}
118
120{
121 __asm__ volatile ("msr DAIFSet, %0"
122 :: "i" (DAIFSET_DBG_BIT) : "memory");
123}
124
126{
127 __asm__ volatile ("msr DAIFClr, %0"
128 :: "i" (DAIFCLR_ABT_BIT) : "memory");
129}
130
132{
133 __asm__ volatile ("msr DAIFSet, %0"
134 :: "i" (DAIFSET_ABT_BIT) : "memory");
135}
136
137static ALWAYS_INLINE void enable_irq(void)
138{
139 __asm__ volatile ("msr DAIFClr, %0"
140 :: "i" (DAIFCLR_IRQ_BIT) : "memory");
141}
142
143static ALWAYS_INLINE void disable_irq(void)
144{
145 __asm__ volatile ("msr DAIFSet, %0"
146 :: "i" (DAIFSET_IRQ_BIT) : "memory");
147}
148
149static ALWAYS_INLINE void enable_fiq(void)
150{
151 __asm__ volatile ("msr DAIFClr, %0"
152 :: "i" (DAIFCLR_FIQ_BIT) : "memory");
153}
154
155static ALWAYS_INLINE void disable_fiq(void)
156{
157 __asm__ volatile ("msr DAIFSet, %0"
158 :: "i" (DAIFSET_FIQ_BIT) : "memory");
159}
160
161#define sev() __asm__ volatile("sev" : : : "memory")
162#define wfe() __asm__ volatile("wfe" : : : "memory")
163#define wfi() __asm__ volatile("wfi" : : : "memory")
164
165static inline bool is_el_implemented(unsigned int el)
166{
167 unsigned int shift;
168
169 if (el > 3) {
170 return false;
171 }
172
173 shift = ID_AA64PFR0_EL1_SHIFT * el;
174
175 return (((read_id_aa64pfr0_el1() >> shift) & ID_AA64PFR0_ELX_MASK) != 0U);
176}
177
178static inline bool is_el_highest_implemented(void)
179{
180 uint32_t el_highest;
181 uint32_t curr_el;
182
183 el_highest = read_id_aa64pfr0_el1() & 0xFFFF;
184 el_highest = (31U - __builtin_clz(el_highest)) / 4;
185
186 curr_el = GET_EL(read_currentel());
187
188 if (curr_el < el_highest) {
189 return false;
190 }
191
192 return true;
193}
194
195static inline bool is_el2_sec_supported(void)
196{
198 ID_AA64PFR0_SEL2_MASK) != 0U);
199}
200
201static inline bool is_in_secure_state(void)
202{
203 /* We cannot read SCR_EL3 from EL2 or EL1 */
204 return !IS_ENABLED(CONFIG_ARMV8_A_NS);
205}
206
207static inline bool is_sve_implemented(void)
208{
210}
211
212#endif /* !_ASMLANGUAGE */
213
214#endif /* ZEPHYR_INCLUDE_ARCH_ARM64_LIB_HELPERS_H_ */
static ALWAYS_INLINE void disable_serror_exceptions(void)
Definition lib_helpers.h:131
static ALWAYS_INLINE uint64_t read_id_aa64pfr0_el1(void)
Definition lib_helpers.h:71
static ALWAYS_INLINE void disable_debug_exceptions(void)
Definition lib_helpers.h:119
static bool is_el_implemented(unsigned int el)
Definition lib_helpers.h:165
static bool is_el_highest_implemented(void)
Definition lib_helpers.h:178
static ALWAYS_INLINE void disable_irq(void)
Definition lib_helpers.h:143
static ALWAYS_INLINE void enable_irq(void)
Definition lib_helpers.h:137
static ALWAYS_INLINE void disable_fiq(void)
Definition lib_helpers.h:155
static ALWAYS_INLINE void enable_debug_exceptions(void)
Definition lib_helpers.h:113
#define MAKE_REG_HELPER_EL123(reg)
Definition lib_helpers.h:52
static ALWAYS_INLINE void enable_serror_exceptions(void)
Definition lib_helpers.h:125
static ALWAYS_INLINE uint64_t read_currentel(void)
Definition lib_helpers.h:67
static ALWAYS_INLINE void enable_fiq(void)
Definition lib_helpers.h:149
static bool is_in_secure_state(void)
Definition lib_helpers.h:201
static bool is_sve_implemented(void)
Definition lib_helpers.h:207
static bool is_el2_sec_supported(void)
Definition lib_helpers.h:195
#define GET_EL(_mode)
Definition cpu.h:96
#define DAIFCLR_ABT_BIT
Definition cpu.h:20
#define DAIFSET_IRQ_BIT
Definition cpu.h:14
#define ID_AA64PFR0_SEL2_MASK
Definition cpu.h:124
#define DAIFSET_ABT_BIT
Definition cpu.h:15
#define DAIFSET_FIQ_BIT
Definition cpu.h:13
#define DAIFCLR_IRQ_BIT
Definition cpu.h:19
#define ID_AA64PFR0_SEL2_SHIFT
Definition cpu.h:123
#define ID_AA64PFR0_EL1_SHIFT
Definition cpu.h:113
#define ID_AA64PFR0_SVE_MASK
Definition cpu.h:122
#define DAIFCLR_FIQ_BIT
Definition cpu.h:18
#define ID_AA64PFR0_ELX_MASK
Definition cpu.h:116
#define ID_AA64PFR0_SVE_SHIFT
Definition cpu.h:121
#define DAIFSET_DBG_BIT
Definition cpu.h:16
#define DAIFCLR_DBG_BIT
Definition cpu.h:21
#define MAKE_REG_HELPER(reg, op1, CRn, CRm, op2)
Definition lib_helpers.h:45
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:148
#define ALWAYS_INLINE
Definition common.h:160
__UINT32_TYPE__ uint32_t
Definition stdint.h:90