Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
asm_inline_gcc.h
Go to the documentation of this file.
1/* ARM AArch32 GCC specific public inline assembler functions and macros */
2
3/*
4 * Copyright (c) 2015, Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9/* Either public functions or macros or invoked by public functions */
10
11#ifndef ZEPHYR_INCLUDE_ARCH_ARM_ASM_INLINE_GCC_H_
12#define ZEPHYR_INCLUDE_ARCH_ARM_ASM_INLINE_GCC_H_
13
14/*
15 * The file must not be included directly
16 * Include arch/cpu.h instead
17 */
18
19#ifndef _ASMLANGUAGE
20
21#include <zephyr/toolchain.h>
22#include <zephyr/types.h>
24#include <cmsis_core.h>
25
26#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
28#endif
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/* On ARMv7-M and ARMv8-M Mainline CPUs, this function prevents regular
35 * exceptions (i.e. with interrupt priority lower than or equal to
36 * _EXC_IRQ_DEFAULT_PRIO) from interrupting the CPU. NMI, Faults, SVC,
37 * and Zero Latency IRQs (if supported) may still interrupt the CPU.
38 *
39 * On ARMv6-M and ARMv8-M Baseline CPUs, this function reads the value of
40 * PRIMASK which shows if interrupts are enabled, then disables all interrupts
41 * except NMI.
42 */
43
44static ALWAYS_INLINE unsigned int arch_irq_lock(void)
45{
46 unsigned int key;
47
48#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
49#if CONFIG_MP_MAX_NUM_CPUS == 1 || defined(CONFIG_ARMV8_M_BASELINE)
50 key = __get_PRIMASK();
51 __disable_irq();
52#else
53#error "Cortex-M0 and Cortex-M0+ require SoC specific support for cross core synchronisation."
54#endif
55#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
56 key = __get_BASEPRI();
57 __set_BASEPRI_MAX(_EXC_IRQ_DEFAULT_PRIO);
58#if defined(CONFIG_CORTEX_M_ERRATUM_440977_WORKAROUND)
59 /* Arm erratum 440977 (formerly 837070): the next instruction can
60 * still be preempted. Keep the ISB before the critical region
61 * without masking higher-priority interrupts through PRIMASK.
62 */
63 __ISB();
64#endif
65 /* Priority-raising writes to PRIMASK/FAULTMASK/BASEPRI are
66 * serialized to the instruction stream, so (other than the above
67 * erratum) no ISB is needed here; see Arm DAI0321A, section 4.8.
68 */
69#elif defined(CONFIG_ARMV7_R) || defined(CONFIG_AARCH32_ARMV8_R) || \
70 defined(CONFIG_ARM_A_PROFILE_AARCH32)
71 __asm__ volatile(
72 "mrs %0, cpsr;"
73 "and %0, #" STRINGIFY(I_BIT) ";"
74 "cpsid i;"
75 : "=r" (key)
76 :
77 : "memory", "cc");
78#else
79#error Unknown ARM architecture
80#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
81
82 return key;
83}
84
85
86/* On Cortex-M0/M0+, this enables all interrupts if they were not
87 * previously disabled.
88 */
89
90static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
91{
92 /* No barrier is used when unmasking: on Cortex-M an interrupt
93 * that is (or becomes) pended is taken after at most a couple of
94 * instructions; unlocking makes no promise that it is taken
95 * *before* the next instruction (see Arm DAI0321A, section 4.7).
96 * The one spot that does need synchronous recognition of a pended
97 * exception, the Cortex-M arch_swap(), inserts its own ISB.
98 */
99#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
100 if (key != 0U) {
101 return;
102 }
103 __enable_irq();
104#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
105 __set_BASEPRI(key);
106#elif defined(CONFIG_ARMV7_R) || defined(CONFIG_AARCH32_ARMV8_R) || \
107 defined(CONFIG_ARM_A_PROFILE_AARCH32)
108 if (key != 0U) {
109 return;
110 }
111 __enable_irq();
112#else
113#error Unknown ARM architecture
114#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
115}
116
117static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
118{
119 /* This convention works for both PRIMASK and BASEPRI */
120 return key == 0U;
121}
122
125{
126#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
127 return __get_PRIMASK() == 0U;
128#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
129 return __get_BASEPRI() == 0U;
130#elif defined(CONFIG_ARMV7_R) || defined(CONFIG_AARCH32_ARMV8_R) || \
131 defined(CONFIG_ARM_A_PROFILE_AARCH32)
132 unsigned int cpsr;
133
134 __asm__ volatile("mrs %0, cpsr" : "=r" (cpsr));
135 return (cpsr & I_BIT) == 0U;
136#else
137#error Unknown ARM architecture
138#endif
139}
140
141#ifdef CONFIG_ZERO_LATENCY_IRQS
142
143static ALWAYS_INLINE unsigned int arch_zli_lock(void)
144{
145 unsigned int key;
146
147 key = __get_PRIMASK();
148
149 /*
150 * The cpsid instruction is self synchronizing within the instruction stream, no need for
151 * an explicit __ISB().
152 */
153 __disable_irq();
154
155 return key;
156}
157
158static ALWAYS_INLINE void arch_zli_unlock(unsigned int key)
159{
160 __set_PRIMASK(key);
161 __ISB();
162}
163
164#endif /* CONFIG_ZERO_LATENCY_IRQS */
165
166#ifdef __cplusplus
167}
168#endif
169
170#endif /* _ASMLANGUAGE */
171
172#endif /* ZEPHYR_INCLUDE_ARCH_ARM_ASM_INLINE_GCC_H_ */
#define I_BIT
Definition cpu.h:33
static ALWAYS_INLINE unsigned int arch_irq_lock(void)
Definition asm_inline_gcc.h:44
static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
Definition asm_inline_gcc.h:90
static ALWAYS_INLINE bool arch_cpu_irqs_are_enabled(void)
Implementation of arch_cpu_irqs_are_enabled.
Definition asm_inline_gcc.h:124
static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
Definition asm_inline_gcc.h:117
#define STRINGIFY(s)
Definition common.h:191
#define ALWAYS_INLINE
Definition common.h:186
Macros to abstract toolchain specific capabilities.