1 /*
2 * Copyright (c) 2015 Intel corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 * @brief Public interface for configuring interrupts
10 */
11 #ifndef ZEPHYR_INCLUDE_IRQ_H_
12 #define ZEPHYR_INCLUDE_IRQ_H_
13
14 /* Pull in the arch-specific implementations */
15 #include <zephyr/arch/cpu.h>
16
17 #ifndef _ASMLANGUAGE
18 #include <zephyr/toolchain.h>
19 #include <zephyr/types.h>
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 /**
26 * @defgroup isr_apis Interrupt Service Routine APIs
27 * @ingroup kernel_apis
28 * @{
29 */
30
31 /**
32 * @brief Initialize an interrupt handler.
33 *
34 * This routine initializes an interrupt handler for an IRQ. The IRQ must be
35 * subsequently enabled before the interrupt handler begins servicing
36 * interrupts.
37 *
38 * @warning
39 * Although this routine is invoked at run-time, all of its arguments must be
40 * computable by the compiler at build time.
41 *
42 * @param irq_p IRQ line number.
43 * @param priority_p Interrupt priority.
44 * @param isr_p Address of interrupt service routine.
45 * @param isr_param_p Parameter passed to interrupt service routine.
46 * @param flags_p Architecture-specific IRQ configuration flags..
47 */
48 #define IRQ_CONNECT(irq_p, priority_p, isr_p, isr_param_p, flags_p) \
49 ARCH_IRQ_CONNECT(irq_p, priority_p, isr_p, isr_param_p, flags_p)
50
51 /**
52 * Configure a dynamic interrupt.
53 *
54 * Use this instead of IRQ_CONNECT() if arguments cannot be known at build time.
55 *
56 * @param irq IRQ line number
57 * @param priority Interrupt priority
58 * @param routine Interrupt service routine
59 * @param parameter ISR parameter
60 * @param flags Arch-specific IRQ configuration flags
61 *
62 * @return The vector assigned to this interrupt
63 */
64 static inline int
irq_connect_dynamic(unsigned int irq,unsigned int priority,void (* routine)(const void * parameter),const void * parameter,uint32_t flags)65 irq_connect_dynamic(unsigned int irq, unsigned int priority,
66 void (*routine)(const void *parameter),
67 const void *parameter, uint32_t flags)
68 {
69 return arch_irq_connect_dynamic(irq, priority, routine, parameter,
70 flags);
71 }
72
73 /**
74 * @brief Initialize a 'direct' interrupt handler.
75 *
76 * This routine initializes an interrupt handler for an IRQ. The IRQ must be
77 * subsequently enabled via irq_enable() before the interrupt handler begins
78 * servicing interrupts.
79 *
80 * These ISRs are designed for performance-critical interrupt handling and do
81 * not go through common interrupt handling code. They must be implemented in
82 * such a way that it is safe to put them directly in the vector table. For
83 * ISRs written in C, The ISR_DIRECT_DECLARE() macro will do this
84 * automatically. For ISRs written in assembly it is entirely up to the
85 * developer to ensure that the right steps are taken.
86 *
87 * This type of interrupt currently has a few limitations compared to normal
88 * Zephyr interrupts:
89 * - No parameters are passed to the ISR.
90 * - No stack switch is done, the ISR will run on the interrupted context's
91 * stack, unless the architecture automatically does the stack switch in HW.
92 * - Interrupt locking state is unchanged from how the HW sets it when the ISR
93 * runs. On arches that enter ISRs with interrupts locked, they will remain
94 * locked.
95 * - Scheduling decisions are now optional, controlled by the return value of
96 * ISRs implemented with the ISR_DIRECT_DECLARE() macro
97 * - The call into the OS to exit power management idle state is now optional.
98 * Normal interrupts always do this before the ISR is run, but when it runs
99 * is now controlled by the placement of a ISR_DIRECT_PM() macro, or omitted
100 * entirely.
101 *
102 * @warning
103 * Although this routine is invoked at run-time, all of its arguments must be
104 * computable by the compiler at build time.
105 *
106 * @param irq_p IRQ line number.
107 * @param priority_p Interrupt priority.
108 * @param isr_p Address of interrupt service routine.
109 * @param flags_p Architecture-specific IRQ configuration flags.
110 */
111 #define IRQ_DIRECT_CONNECT(irq_p, priority_p, isr_p, flags_p) \
112 ARCH_IRQ_DIRECT_CONNECT(irq_p, priority_p, isr_p, flags_p)
113
114 /**
115 * @brief Common tasks before executing the body of an ISR
116 *
117 * This macro must be at the beginning of all direct interrupts and performs
118 * minimal architecture-specific tasks before the ISR itself can run. It takes
119 * no arguments and has no return value.
120 */
121 #define ISR_DIRECT_HEADER() ARCH_ISR_DIRECT_HEADER()
122
123 /**
124 * @brief Common tasks before exiting the body of an ISR
125 *
126 * This macro must be at the end of all direct interrupts and performs
127 * minimal architecture-specific tasks like EOI. It has no return value.
128 *
129 * In a normal interrupt, a check is done at end of interrupt to invoke
130 * z_swap() logic if the current thread is preemptible and there is another
131 * thread ready to run in the kernel's ready queue cache. This is now optional
132 * and controlled by the check_reschedule argument. If unsure, set to nonzero.
133 * On systems that do stack switching and nested interrupt tracking in software,
134 * z_swap() should only be called if this was a non-nested interrupt.
135 *
136 * @param check_reschedule If nonzero, additionally invoke scheduling logic
137 */
138 #define ISR_DIRECT_FOOTER(check_reschedule) \
139 ARCH_ISR_DIRECT_FOOTER(check_reschedule)
140
141 /**
142 * @brief Perform power management idle exit logic
143 *
144 * This macro may optionally be invoked somewhere in between IRQ_DIRECT_HEADER()
145 * and IRQ_DIRECT_FOOTER() invocations. It performs tasks necessary to
146 * exit power management idle state. It takes no parameters and returns no
147 * arguments. It may be omitted, but be careful!
148 */
149 #define ISR_DIRECT_PM() ARCH_ISR_DIRECT_PM()
150
151 /**
152 * @brief Helper macro to declare a direct interrupt service routine.
153 *
154 * This will declare the function in a proper way and automatically include
155 * the ISR_DIRECT_FOOTER() and ISR_DIRECT_HEADER() macros. The function should
156 * return nonzero status if a scheduling decision should potentially be made.
157 * See ISR_DIRECT_FOOTER() for more details on the scheduling decision.
158 *
159 * For architectures that support 'regular' and 'fast' interrupt types, where
160 * these interrupt types require different assembly language handling of
161 * registers by the ISR, this will always generate code for the 'fast'
162 * interrupt type.
163 *
164 * Example usage:
165 *
166 * ISR_DIRECT_DECLARE(my_isr)
167 * {
168 * bool done = do_stuff();
169 * ISR_DIRECT_PM(); // done after do_stuff() due to latency concerns
170 * if (!done) {
171 * return 0; // don't bother checking if we have to z_swap()
172 * }
173 *
174 * k_sem_give(some_sem);
175 * return 1;
176 * }
177 *
178 * @param name symbol name of the ISR
179 */
180 #define ISR_DIRECT_DECLARE(name) ARCH_ISR_DIRECT_DECLARE(name)
181
182 /**
183 * @brief Lock interrupts.
184 * @def irq_lock()
185 *
186 * This routine disables all interrupts on the CPU. It returns an unsigned
187 * integer "lock-out key", which is an architecture-dependent indicator of
188 * whether interrupts were locked prior to the call. The lock-out key must be
189 * passed to irq_unlock() to re-enable interrupts.
190 *
191 * @note
192 * This routine must also serve as a memory barrier to ensure the uniprocessor
193 * implementation of `k_spinlock_t` is correct.
194 *
195 * This routine can be called recursively, as long as the caller keeps track
196 * of each lock-out key that is generated. Interrupts are re-enabled by
197 * passing each of the keys to irq_unlock() in the reverse order they were
198 * acquired. (That is, each call to irq_lock() must be balanced by
199 * a corresponding call to irq_unlock().)
200 *
201 * This routine can only be invoked from supervisor mode. Some architectures
202 * (for example, ARM) will fail silently if invoked from user mode instead
203 * of generating an exception.
204 *
205 * @note
206 * This routine can be called by ISRs or by threads. If it is called by a
207 * thread, the interrupt lock is thread-specific; this means that interrupts
208 * remain disabled only while the thread is running. If the thread performs an
209 * operation that allows another thread to run (for example, giving a semaphore
210 * or sleeping for N milliseconds), the interrupt lock no longer applies and
211 * interrupts may be re-enabled while other processing occurs. When the thread
212 * once again becomes the current thread, the kernel re-establishes its
213 * interrupt lock; this ensures the thread won't be interrupted until it has
214 * explicitly released the interrupt lock it established.
215 *
216 * @warning
217 * The lock-out key should never be used to manually re-enable interrupts
218 * or to inspect or manipulate the contents of the CPU's interrupt bits.
219 *
220 * @return An architecture-dependent lock-out key representing the
221 * "interrupt disable state" prior to the call.
222 */
223 #ifdef CONFIG_SMP
224 unsigned int z_smp_global_lock(void);
225 #define irq_lock() z_smp_global_lock()
226 #else
227 #define irq_lock() arch_irq_lock()
228 #endif
229
230 /**
231 * @brief Unlock interrupts.
232 * @def irq_unlock()
233 *
234 * This routine reverses the effect of a previous call to irq_lock() using
235 * the associated lock-out key. The caller must call the routine once for
236 * each time it called irq_lock(), supplying the keys in the reverse order
237 * they were acquired, before interrupts are enabled.
238 *
239 * @note
240 * This routine must also serve as a memory barrier to ensure the uniprocessor
241 * implementation of `k_spinlock_t` is correct.
242 *
243 * This routine can only be invoked from supervisor mode. Some architectures
244 * (for example, ARM) will fail silently if invoked from user mode instead
245 * of generating an exception.
246 *
247 * @note Can be called by ISRs.
248 *
249 * @param key Lock-out key generated by irq_lock().
250 */
251 #ifdef CONFIG_SMP
252 void z_smp_global_unlock(unsigned int key);
253 #define irq_unlock(key) z_smp_global_unlock(key)
254 #else
255 #define irq_unlock(key) arch_irq_unlock(key)
256 #endif
257
258 /**
259 * @brief Return IRQ level
260 * This routine returns the interrupt level number of the provided interrupt.
261 *
262 * @param irq IRQ number in its zephyr format
263 *
264 * @return 1 if IRQ level 1, 2 if IRQ level 2, 3 if IRQ level 3
265 */
irq_get_level(unsigned int irq)266 static inline unsigned int irq_get_level(unsigned int irq)
267 {
268 #if defined(CONFIG_3RD_LEVEL_INTERRUPTS)
269 return ((irq >> 16) & 0xFF) != 0 ? 3 :
270 (((irq >> 8) & 0xFF) == 0 ? 1 : 2);
271 #elif defined(CONFIG_2ND_LEVEL_INTERRUPTS)
272 return ((irq >> 8) & 0xFF) == 0 ? 1 : 2;
273 #else
274 ARG_UNUSED(irq);
275
276 return 1;
277 #endif
278 }
279
280 #ifdef CONFIG_2ND_LEVEL_INTERRUPTS
281 /**
282 * @brief Return the 2nd level interrupt number
283 *
284 *
285 * This routine returns the second level irq number of the zephyr irq
286 * number passed in
287 *
288 * @param irq IRQ number in its zephyr format
289 *
290 * @return 2nd level IRQ number
291 */
irq_from_level_2(unsigned int irq)292 static inline unsigned int irq_from_level_2(unsigned int irq)
293 {
294 #ifdef CONFIG_3RD_LEVEL_INTERRUPTS
295 return ((irq >> 8) & 0xFF) - 1;
296 #else
297 return (irq >> 8) - 1;
298 #endif
299 }
300
301 /**
302 * @brief Converts irq from level 1 to level 2 format
303 *
304 *
305 * This routine converts the input into the level 2 irq number format
306 *
307 * @note Values >= 0xFF are invalid
308 *
309 * @param irq IRQ number in its zephyr format
310 *
311 * @return 2nd level IRQ number
312 */
irq_to_level_2(unsigned int irq)313 static inline unsigned int irq_to_level_2(unsigned int irq)
314 {
315 return (irq + 1) << 8;
316 }
317
318 /**
319 * @brief Returns the parent IRQ of the level 2 raw IRQ number
320 *
321 *
322 * The parent of a 2nd level interrupt is in the 1st byte
323 *
324 * @param irq IRQ number in its zephyr format
325 *
326 * @return 2nd level IRQ parent
327 */
irq_parent_level_2(unsigned int irq)328 static inline unsigned int irq_parent_level_2(unsigned int irq)
329 {
330 return irq & 0xFF;
331 }
332 #endif
333
334 #ifdef CONFIG_3RD_LEVEL_INTERRUPTS
335 /**
336 * @brief Return the 3rd level interrupt number
337 *
338 *
339 * This routine returns the third level irq number of the zephyr irq
340 * number passed in
341 *
342 * @param irq IRQ number in its zephyr format
343 *
344 * @return 3rd level IRQ number
345 */
irq_from_level_3(unsigned int irq)346 static inline unsigned int irq_from_level_3(unsigned int irq)
347 {
348 return (irq >> 16) - 1;
349 }
350
351 /**
352 * @brief Converts irq from level 1 to level 3 format
353 *
354 *
355 * This routine converts the input into the level 3 irq number format
356 *
357 * @note Values >= 0xFF are invalid
358 *
359 * @param irq IRQ number in its zephyr format
360 *
361 * @return 3rd level IRQ number
362 */
irq_to_level_3(unsigned int irq)363 static inline unsigned int irq_to_level_3(unsigned int irq)
364 {
365 return (irq + 1) << 16;
366 }
367
368 /**
369 * @brief Returns the parent IRQ of the level 3 raw IRQ number
370 *
371 *
372 * The parent of a 3rd level interrupt is in the 2nd byte
373 *
374 * @param irq IRQ number in its zephyr format
375 *
376 * @return 3rd level IRQ parent
377 */
irq_parent_level_3(unsigned int irq)378 static inline unsigned int irq_parent_level_3(unsigned int irq)
379 {
380 return (irq >> 8) & 0xFF;
381 }
382 #endif
383
384 /**
385 * @brief Enable an IRQ.
386 *
387 * This routine enables interrupts from source @a irq.
388 *
389 * @param irq IRQ line.
390 */
391 #define irq_enable(irq) arch_irq_enable(irq)
392
393 /**
394 * @brief Disable an IRQ.
395 *
396 * This routine disables interrupts from source @a irq.
397 *
398 * @param irq IRQ line.
399 */
400 #define irq_disable(irq) arch_irq_disable(irq)
401
402 /**
403 * @brief Get IRQ enable state.
404 *
405 * This routine indicates if interrupts from source @a irq are enabled.
406 *
407 * @param irq IRQ line.
408 *
409 * @return interrupt enable state, true or false
410 */
411 #define irq_is_enabled(irq) arch_irq_is_enabled(irq)
412
413 /**
414 * @}
415 */
416
417 #ifdef __cplusplus
418 }
419 #endif
420
421 #endif /* ASMLANGUAGE */
422 #endif /* ZEPHYR_INCLUDE_IRQ_H_ */
423