1 /*
2 * Copyright (c) 2013-2014 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 * @brief ARM Cortex-M and Cortex-R interrupt management
10 *
11 *
12 * Interrupt management: enabling/disabling and dynamic ISR
13 * connecting/replacing. SW_ISR_TABLE_DYNAMIC has to be enabled for
14 * connecting ISRs at runtime.
15 */
16
17 #include <kernel.h>
18 #include <arch/cpu.h>
19 #if defined(CONFIG_CPU_CORTEX_M)
20 #include <arch/arm/aarch32/cortex_m/cmsis.h>
21 #elif defined(CONFIG_CPU_CORTEX_A) || defined(CONFIG_CPU_CORTEX_R)
22 #include <drivers/interrupt_controller/gic.h>
23 #endif
24 #include <sys/__assert.h>
25 #include <toolchain.h>
26 #include <linker/sections.h>
27 #include <sw_isr_table.h>
28 #include <irq.h>
29 #include <tracing/tracing.h>
30 #include <pm/pm.h>
31
32 extern void z_arm_reserved(void);
33
34 #if defined(CONFIG_CPU_CORTEX_M)
35 #define NUM_IRQS_PER_REG 32
36 #define REG_FROM_IRQ(irq) (irq / NUM_IRQS_PER_REG)
37 #define BIT_FROM_IRQ(irq) (irq % NUM_IRQS_PER_REG)
38
arch_irq_enable(unsigned int irq)39 void arch_irq_enable(unsigned int irq)
40 {
41 NVIC_EnableIRQ((IRQn_Type)irq);
42 }
43
arch_irq_disable(unsigned int irq)44 void arch_irq_disable(unsigned int irq)
45 {
46 NVIC_DisableIRQ((IRQn_Type)irq);
47 }
48
arch_irq_is_enabled(unsigned int irq)49 int arch_irq_is_enabled(unsigned int irq)
50 {
51 return NVIC->ISER[REG_FROM_IRQ(irq)] & BIT(BIT_FROM_IRQ(irq));
52 }
53
54 /**
55 * @internal
56 *
57 * @brief Set an interrupt's priority
58 *
59 * The priority is verified if ASSERT_ON is enabled. The maximum number
60 * of priority levels is a little complex, as there are some hardware
61 * priority levels which are reserved.
62 *
63 * @return N/A
64 */
z_arm_irq_priority_set(unsigned int irq,unsigned int prio,uint32_t flags)65 void z_arm_irq_priority_set(unsigned int irq, unsigned int prio, uint32_t flags)
66 {
67 /* The kernel may reserve some of the highest priority levels.
68 * So we offset the requested priority level with the number
69 * of priority levels reserved by the kernel.
70 */
71
72 /* If we have zero latency interrupts, those interrupts will
73 * run at a priority level which is not masked by irq_lock().
74 * Our policy is to express priority levels with special properties
75 * via flags
76 */
77 if (IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) && (flags & IRQ_ZERO_LATENCY)) {
78 prio = _EXC_ZERO_LATENCY_IRQS_PRIO;
79 } else {
80 prio += _IRQ_PRIO_OFFSET;
81 }
82
83 /* The last priority level is also used by PendSV exception, but
84 * allow other interrupts to use the same level, even if it ends up
85 * affecting performance (can still be useful on systems with a
86 * reduced set of priorities, like Cortex-M0/M0+).
87 */
88 __ASSERT(prio <= (BIT(NUM_IRQ_PRIO_BITS) - 1),
89 "invalid priority %d for %d irq! values must be less than %lu\n",
90 prio - _IRQ_PRIO_OFFSET, irq,
91 BIT(NUM_IRQ_PRIO_BITS) - (_IRQ_PRIO_OFFSET));
92 NVIC_SetPriority((IRQn_Type)irq, prio);
93 }
94
95 #elif defined(CONFIG_CPU_CORTEX_A) || defined(CONFIG_CPU_CORTEX_R)
96 /*
97 * For Cortex-A and Cortex-R cores, the default interrupt controller is the ARM
98 * Generic Interrupt Controller (GIC) and therefore the architecture interrupt
99 * control functions are mapped to the GIC driver interface.
100 *
101 * When a custom interrupt controller is used (i.e.
102 * CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER is enabled), the architecture
103 * interrupt control functions are mapped to the SoC layer in
104 * `include/arch/arm/aarch32/irq.h`.
105 */
106
107 #if !defined(CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER)
arch_irq_enable(unsigned int irq)108 void arch_irq_enable(unsigned int irq)
109 {
110 arm_gic_irq_enable(irq);
111 }
112
arch_irq_disable(unsigned int irq)113 void arch_irq_disable(unsigned int irq)
114 {
115 arm_gic_irq_disable(irq);
116 }
117
arch_irq_is_enabled(unsigned int irq)118 int arch_irq_is_enabled(unsigned int irq)
119 {
120 return arm_gic_irq_is_enabled(irq);
121 }
122
123 /**
124 * @internal
125 *
126 * @brief Set an interrupt's priority
127 *
128 * The priority is verified if ASSERT_ON is enabled. The maximum number
129 * of priority levels is a little complex, as there are some hardware
130 * priority levels which are reserved: three for various types of exceptions,
131 * and possibly one additional to support zero latency interrupts.
132 *
133 * @return N/A
134 */
z_arm_irq_priority_set(unsigned int irq,unsigned int prio,uint32_t flags)135 void z_arm_irq_priority_set(unsigned int irq, unsigned int prio, uint32_t flags)
136 {
137 arm_gic_irq_set_priority(irq, prio, flags);
138 }
139 #endif /* !CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER */
140
141 #endif /* CONFIG_CPU_CORTEX_M */
142
143 void z_arm_fatal_error(unsigned int reason, const z_arch_esf_t *esf);
144
145 /**
146 *
147 * @brief Spurious interrupt handler
148 *
149 * Installed in all _sw_isr_table slots at boot time. Throws an error if
150 * called.
151 *
152 * @return N/A
153 */
z_irq_spurious(const void * unused)154 void z_irq_spurious(const void *unused)
155 {
156 ARG_UNUSED(unused);
157
158 z_arm_fatal_error(K_ERR_SPURIOUS_IRQ, NULL);
159 }
160
161 #ifdef CONFIG_PM
_arch_isr_direct_pm(void)162 void _arch_isr_direct_pm(void)
163 {
164 #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) \
165 || defined(CONFIG_ARMV7_R)
166 unsigned int key;
167
168 /* irq_lock() does what we wan for this CPU */
169 key = irq_lock();
170 #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
171 /* Lock all interrupts. irq_lock() will on this CPU only disable those
172 * lower than BASEPRI, which is not what we want. See comments in
173 * arch/arm/core/aarch32/isr_wrapper.S
174 */
175 __asm__ volatile("cpsid i" : : : "memory");
176 #else
177 #error Unknown ARM architecture
178 #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
179
180 if (_kernel.idle) {
181 int32_t idle_val = _kernel.idle;
182
183 _kernel.idle = 0;
184 z_pm_save_idle_exit(idle_val);
185 }
186
187 #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) \
188 || defined(CONFIG_ARMV7_R)
189 irq_unlock(key);
190 #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
191 __asm__ volatile("cpsie i" : : : "memory");
192 #else
193 #error Unknown ARM architecture
194 #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
195
196 }
197 #endif
198
199 #if defined(CONFIG_ARM_SECURE_FIRMWARE)
200 /**
201 *
202 * @brief Set the target security state for the given IRQ
203 *
204 * Function sets the security state (Secure or Non-Secure) targeted
205 * by the given irq. It requires ARMv8-M MCU.
206 * It is only compiled if ARM_SECURE_FIRMWARE is defined.
207 * It should only be called while in Secure state, otherwise, a write attempt
208 * to NVIC.ITNS register is write-ignored(WI), as the ITNS register is not
209 * banked between security states and, therefore, has no Non-Secure instance.
210 *
211 * It shall return the resulting target state of the given IRQ, indicating
212 * whether the operation has been performed successfully.
213 *
214 * @param irq IRQ line
215 * @param irq_target_state the desired IRQ target state
216 *
217 * @return The resulting target state of the given IRQ
218 */
irq_target_state_set(unsigned int irq,irq_target_state_t irq_target_state)219 irq_target_state_t irq_target_state_set(unsigned int irq,
220 irq_target_state_t irq_target_state)
221 {
222 uint32_t result;
223
224 if (irq_target_state == IRQ_TARGET_STATE_SECURE) {
225 /* Set target to Secure */
226 result = NVIC_ClearTargetState(irq);
227 } else {
228 /* Set target to Non-Secure */
229 result = NVIC_SetTargetState(irq);
230 }
231
232 if (result) {
233 return IRQ_TARGET_STATE_NON_SECURE;
234 } else {
235 return IRQ_TARGET_STATE_SECURE;
236 }
237 }
238
239 /**
240 *
241 * @brief Determine whether the given IRQ targets the Secure state
242 *
243 * Function determines whether the given irq targets the Secure state
244 * or not (i.e. targets the Non-Secure state). It requires ARMv8-M MCU.
245 * It is only compiled if ARM_SECURE_FIRMWARE is defined.
246 * It should only be called while in Secure state, otherwise, a read attempt
247 * to NVIC.ITNS register is read-as-zero(RAZ), as the ITNS register is not
248 * banked between security states and, therefore, has no Non-Secure instance.
249 *
250 * @param irq IRQ line
251 *
252 * @return 1 if target state is Secure, 0 otherwise.
253 */
irq_target_state_is_secure(unsigned int irq)254 int irq_target_state_is_secure(unsigned int irq)
255 {
256 return NVIC_GetTargetState(irq) == 0;
257 }
258
259 /**
260 *
261 * @brief Disable and set all interrupt lines to target Non-Secure state.
262 *
263 * The function is used to set all HW NVIC interrupt lines to target the
264 * Non-Secure state. The function shall only be called fron Secure state.
265 *
266 * Notes:
267 * - All NVIC interrupts are disabled before being routed to Non-Secure.
268 * - Bits corresponding to un-implemented interrupts are RES0, so writes
269 * will be ignored.
270 *
271 * @return N/A
272 */
irq_target_state_set_all_non_secure(void)273 void irq_target_state_set_all_non_secure(void)
274 {
275 int i;
276
277 /* Disable (Clear) all NVIC interrupt lines. */
278 for (i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
279 NVIC->ICER[i] = 0xFFFFFFFF;
280 }
281
282 __DSB();
283 __ISB();
284
285 /* Set all NVIC interrupt lines to target Non-Secure */
286 for (i = 0; i < sizeof(NVIC->ITNS) / sizeof(NVIC->ITNS[0]); i++) {
287 NVIC->ITNS[i] = 0xFFFFFFFF;
288 }
289 }
290
291 #endif /* CONFIG_ARM_SECURE_FIRMWARE */
292
293 #ifdef CONFIG_DYNAMIC_INTERRUPTS
294 #ifdef CONFIG_GEN_ISR_TABLES
arch_irq_connect_dynamic(unsigned int irq,unsigned int priority,void (* routine)(const void * parameter),const void * parameter,uint32_t flags)295 int arch_irq_connect_dynamic(unsigned int irq, unsigned int priority,
296 void (*routine)(const void *parameter),
297 const void *parameter, uint32_t flags)
298 {
299 z_isr_install(irq, routine, parameter);
300 z_arm_irq_priority_set(irq, priority, flags);
301 return irq;
302 }
303 #endif /* CONFIG_GEN_ISR_TABLES */
304
305 #ifdef CONFIG_DYNAMIC_DIRECT_INTERRUPTS
z_arm_irq_dynamic_direct_isr_dispatch(void)306 static inline void z_arm_irq_dynamic_direct_isr_dispatch(void)
307 {
308 uint32_t irq = __get_IPSR() - 16;
309
310 if (irq < IRQ_TABLE_SIZE) {
311 struct _isr_table_entry *isr_entry = &_sw_isr_table[irq];
312
313 isr_entry->isr(isr_entry->arg);
314 }
315 }
316
ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_reschedule)317 ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_reschedule)
318 {
319 z_arm_irq_dynamic_direct_isr_dispatch();
320
321 return 1;
322 }
323
ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_no_reschedule)324 ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_no_reschedule)
325 {
326 z_arm_irq_dynamic_direct_isr_dispatch();
327
328 return 0;
329 }
330
331 #endif /* CONFIG_DYNAMIC_DIRECT_INTERRUPTS */
332
333 #endif /* CONFIG_DYNAMIC_INTERRUPTS */
334