ARM7TDMI: optimize IRQ macros.
[bertos.git] / bertos / cpu / irq.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief CPU-specific IRQ definitions.
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_IRQ_H
42 #define CPU_IRQ_H
43
44 #include "detect.h"
45 #include "types.h"
46
47 #include <kern/proc.h> /* proc_needPreempt() / proc_preempt() */
48
49 #include <cfg/compiler.h> /* for uintXX_t */
50 #include "cfg/cfg_proc.h" /* CONFIG_KERN_PREEMPT */
51
52 #if CPU_I196
53         #define IRQ_DISABLE             disable_interrupt()
54         #define IRQ_ENABLE              enable_interrupt()
55 #elif CPU_X86
56
57         /* Get IRQ_* definitions from the hosting environment. */
58         #include <cfg/os.h>
59         #if OS_EMBEDDED
60                 #define IRQ_DISABLE             FIXME
61                 #define IRQ_ENABLE              FIXME
62                 #define IRQ_SAVE_DISABLE(x)     FIXME
63                 #define IRQ_RESTORE(x)          FIXME
64         #endif /* OS_EMBEDDED */
65
66 #elif CPU_CM3
67         /* Cortex-M3 */
68
69         /*
70          * Interrupt priority.
71          *
72          * NOTE: 0 means that an interrupt is not affected by the global IRQ
73          * priority settings.
74          */
75         #define IRQ_PRIO                0x80
76         #define IRQ_PRIO_MIN            0xf0
77         #define IRQ_PRIO_MAX            0
78         /*
79          * To disable interrupts we just raise the system base priority to a
80          * number lower than the default IRQ priority. In this way, all the
81          * "normal" interrupt can't be triggered. High-priority interrupt can
82          * still happen (at the moment only the soft-interrupt svcall uses a
83          * priority greater than the default IRQ priority).
84          *
85          * To enable interrupts we set the system base priority to 0, that
86          * means IRQ priority mechanism is disabled, and any interrupt can
87          * happen.
88          */
89         #define IRQ_PRIO_DISABLED       0x40
90         #define IRQ_PRIO_ENABLED        0
91
92         #define IRQ_DISABLE                                             \
93         ({                                                              \
94                 register cpu_flags_t reg = IRQ_PRIO_DISABLED;           \
95                 asm volatile (                                          \
96                         "msr basepri, %0"                               \
97                         : : "r"(reg) : "memory", "cc");                 \
98         })
99
100         #define IRQ_ENABLE                                              \
101         ({                                                              \
102                 register cpu_flags_t reg = IRQ_PRIO_ENABLED;            \
103                 asm volatile (                                          \
104                         "msr basepri, %0"                               \
105                         : : "r"(reg) : "memory", "cc");                 \
106         })
107
108         #define CPU_READ_FLAGS()                                        \
109         ({                                                              \
110                 register cpu_flags_t reg;                               \
111                 asm volatile (                                          \
112                         "mrs %0, basepri"                               \
113                          : "=r"(reg) : : "memory", "cc");               \
114                 reg;                                                    \
115         })
116
117         #define IRQ_SAVE_DISABLE(x)                                     \
118         ({                                                              \
119                 x = CPU_READ_FLAGS();                                   \
120                 IRQ_DISABLE;                                            \
121         })
122
123         #define IRQ_RESTORE(x)                                          \
124         ({                                                              \
125                 asm volatile (                                          \
126                         "msr basepri, %0"                               \
127                         : : "r"(x) : "memory", "cc");                   \
128         })
129
130         #define IRQ_ENABLED() (CPU_READ_FLAGS() == IRQ_PRIO_ENABLED)
131
132         INLINE bool irq_running(void)
133         {
134                 register uint32_t ret;
135
136                 /*
137                  * Check if the current stack pointer is the main stack or
138                  * process stack: we use the main stack only in Handler mode,
139                  * so this means we're running inside an ISR.
140                  */
141                 asm volatile (
142                         "mrs %0, msp\n\t"
143                         "cmp sp, %0\n\t"
144                         "ite ne\n\t"
145                         "movne %0, #0\n\t"
146                         "moveq %0, #1\n\t" : "=r"(ret) : : "cc");
147                 return ret;
148         }
149         #define IRQ_RUNNING() irq_running()
150
151         #if CONFIG_KERN_PREEMPT
152
153                 #define DECLARE_ISR_CONTEXT_SWITCH(func)                \
154                 void func(void);                                        \
155                 INLINE void __isr_##func(void);                         \
156                 void func(void)                                         \
157                 {                                                       \
158                         __isr_##func();                                 \
159                         if (!proc_needPreempt())                        \
160                                 return;                                 \
161                         /*
162                          * Set a PendSV request.
163                          *
164                          * The preemption handler will be called immediately
165                          * after this ISR in tail-chaining mode (without the
166                          * overhead of hardware state saving and restoration
167                          * between interrupts).
168                          */                                             \
169                         HWREG(NVIC_INT_CTRL) = NVIC_INT_CTRL_PEND_SV;   \
170                 }                                                       \
171                 INLINE void __isr_##func(void)
172
173                 /**
174                  * With task priorities enabled each ISR is used a point to
175                  * check if we need to perform a context switch.
176                  *
177                  * Instead, without priorities a context switch can occur only
178                  * when the running task expires its time quantum. In this last
179                  * case, the context switch can only occur in the timer ISR,
180                  * that must be always declared with the
181                  * DECLARE_ISR_CONTEXT_SWITCH() macro.
182                  */
183                 #if CONFIG_KERN_PRI
184                         #define DECLARE_ISR(func) \
185                                 DECLARE_ISR_CONTEXT_SWITCH(func)
186                         /**
187                          * Interrupt service routine prototype: can be used for
188                          * forward declarations.
189                          */
190                         #define ISR_PROTO(func) \
191                                 ISR_PROTO_CONTEXT_SWITCH(func)
192                 #endif /* !CONFIG_KERN_PRI */
193         #endif
194
195         #ifndef ISR_PROTO
196                 #define ISR_PROTO(func) void func(void)
197         #endif
198         #ifndef DECLARE_ISR
199                 #define DECLARE_ISR(func) void func(void)
200         #endif
201         #ifndef DECLARE_ISR_CONTEXT_SWITCH
202                 #define DECLARE_ISR_CONTEXT_SWITCH(func) void func(void)
203         #endif
204         #ifndef ISR_PROTO_CONTEXT_SWITCH
205                 #define ISR_PROTO_CONTEXT_SWITCH(func) void func(void)
206         #endif
207
208 #elif CPU_ARM
209
210         #ifdef __IAR_SYSTEMS_ICC__
211
212                 #include <inarm.h>
213
214                 #if __CPU_MODE__ == 1 /* Thumb */
215                         /* Use stubs */
216                         extern cpu_flags_t get_CPSR(void);
217                         extern void set_CPSR(cpu_flags_t flags);
218                 #else
219                         #define get_CPSR __get_CPSR
220                         #define set_CPSR __set_CPSR
221                 #endif
222
223                 #define IRQ_DISABLE __disable_interrupt()
224                 #define IRQ_ENABLE  __enable_interrupt()
225
226                 #define IRQ_SAVE_DISABLE(x) \
227                 do { \
228                         (x) = get_CPSR(); \
229                         __disable_interrupt(); \
230                 } while (0)
231
232                 #define IRQ_RESTORE(x) \
233                 do { \
234                         set_CPSR(x); \
235                 } while (0)
236
237                 #define IRQ_ENABLED() \
238                         ((bool)(get_CPSR() & 0xb0))
239
240         #else /* !__IAR_SYSTEMS_ICC__ */
241
242                 #define IRQ_DISABLE                                     \
243                 do {                                                    \
244                         cpu_flags_t sreg;                               \
245                         asm volatile (                                  \
246                                 "mrs %0, cpsr\n\t"                      \
247                                 "orr %0, %0, #0xc0\n\t"                 \
248                                 "msr cpsr_c, %0\n\t"                    \
249                                 : "=r" (sreg) : : "memory", "cc");      \
250                 } while (0)
251
252                 #define IRQ_ENABLE                                      \
253                 do {                                                    \
254                         cpu_flags_t sreg;                               \
255                         asm volatile (                                  \
256                                 "mrs %0, cpsr\n\t"                      \
257                                 "bic %0, %0, #0xc0\n\t"                 \
258                                 "msr cpsr_c, %0\n\t"                    \
259                                 : "=r" (sreg) : : "memory", "cc");      \
260                 } while (0)
261
262                 #define IRQ_SAVE_DISABLE(x)                             \
263                 do {                                                    \
264                         cpu_flags_t sreg;                               \
265                         (void) (&sreg == &x);                           \
266                         asm volatile (                                  \
267                                 "mrs %0, cpsr\n\t"                      \
268                                 "orr %1, %0, #0xc0\n\t"                 \
269                                 "msr cpsr_c, %1\n\t"                    \
270                                 : "=r" (x), "=r" (sreg)                 \
271                                 : : "memory", "cc");                    \
272                 } while (0)
273
274                 #define IRQ_RESTORE(x)                                  \
275                 do {                                                    \
276                         asm volatile (                                  \
277                                 "msr cpsr_c, %0\n\t"                    \
278                                 : : "r" (x) : "memory", "cc");          \
279                 } while (0)
280
281                 #define CPU_READ_FLAGS()                                \
282                 ({                                                      \
283                         cpu_flags_t sreg;                               \
284                         asm volatile (                                  \
285                                 "mrs %0, cpsr\n\t"                      \
286                                 : "=r" (sreg) : : "memory", "cc");      \
287                         sreg;                                           \
288                 })
289
290                 #define IRQ_ENABLED() (!(CPU_READ_FLAGS() & 0x80))
291
292                 #if CONFIG_KERN_PREEMPT
293                         EXTERN_C void asm_irq_switch_context(void);
294
295                         /**
296                          * At the beginning of any ISR immediately ajust the
297                          * return address and store all the caller-save
298                          * registers (the ISR may change these registers that
299                          * are shared with the user-context).
300                          */
301                         #define IRQ_ENTRY() asm volatile ( \
302                                                 "sub    lr, lr, #4\n\t" \
303                                                 "stmfd  sp!, {r0-r3, ip, lr}\n\t")
304                         #define IRQ_EXIT()  asm volatile ( \
305                                                 "b      asm_irq_switch_context\n\t")
306                         /**
307                          * Function attribute to declare an interrupt service
308                          * routine.
309                          *
310                          * An ISR function must be declared as naked because we
311                          * want to add our IRQ_ENTRY() prologue and IRQ_EXIT()
312                          * epilogue code to handle the context switch and save
313                          * all the registers (not only the callee-save).
314                          *
315                          */
316                         #define ISR_FUNC __attribute__((naked))
317
318                         /**
319                          * The compiler cannot establish which
320                          * registers actually need to be saved, because
321                          * the interrupt can happen at any time, so the
322                          * "normal" prologue and epilogue used for a
323                          * generic function call are not suitable for
324                          * the ISR.
325                          *
326                          * Using a naked function has the drawback that
327                          * the stack is not automatically adjusted at
328                          * this point, like a "normal" function call.
329                          *
330                          * So, an ISR can _only_ contain other function
331                          * calls and they can't use the stack in any
332                          * other way.
333                          *
334                          * NOTE: we need to explicitly disable IRQs after
335                          * IRQ_ENTRY(), because the IRQ status flag is not
336                          * masked by the hardware and an IRQ ack inside the ISR
337                          * may cause the triggering of another IRQ before
338                          * exiting from the current ISR.
339                          *
340                          * The respective IRQ_ENABLE is not necessary, because
341                          * IRQs will be automatically re-enabled when restoring
342                          * the context of the user task.
343                          */
344                         #define DECLARE_ISR_CONTEXT_SWITCH(func)        \
345                                 void ISR_FUNC func(void);               \
346                                 static void __isr_##func(void);         \
347                                 void ISR_FUNC func(void)                \
348                                 {                                       \
349                                         IRQ_ENTRY();                    \
350                                         IRQ_DISABLE;                    \
351                                         __isr_##func();                 \
352                                         IRQ_EXIT();                     \
353                                 }                                       \
354                                 static void __isr_##func(void)
355                         /**
356                          * Interrupt service routine prototype: can be used for
357                          * forward declarations.
358                          */
359                         #define ISR_PROTO_CONTEXT_SWITCH(func)  \
360                                 void ISR_FUNC func(void)
361                         /**
362                          * With task priorities enabled each ISR is used a point to
363                          * check if we need to perform a context switch.
364                          *
365                          * Instead, without priorities a context switch can occur only
366                          * when the running task expires its time quantum. In this last
367                          * case, the context switch can only occur in the timer
368                          * ISR, that must be always declared with the
369                          * DECLARE_ISR_CONTEXT_SWITCH() macro.
370                          */
371                         #if CONFIG_KERN_PRI
372                                 #define DECLARE_ISR(func) \
373                                         DECLARE_ISR_CONTEXT_SWITCH(func)
374
375                                 #define ISR_PROTO(func) \
376                                         ISR_PROTO_CONTEXT_SWITCH(func)
377                         #endif /* !CONFIG_KERN_PRI */
378                 #endif /* CONFIG_KERN_PREEMPT */
379
380                 #ifndef DECLARE_ISR
381                         #define DECLARE_ISR(func) \
382                                 void __attribute__((interrupt)) func(void)
383                 #endif
384                 #ifndef DECLARE_ISR_CONTEXT_SWITCH
385                         #define DECLARE_ISR_CONTEXT_SWITCH(func) \
386                                 void __attribute__((interrupt)) func(void)
387                 #endif
388                 #ifndef ISR_PROTO
389                         #define ISR_PROTO(func) \
390                                 void __attribute__((interrupt)) func(void)
391                 #endif
392                 #ifndef ISR_PROTO_CONTEXT_SWITCH
393                         #define ISR_PROTO_CONTEXT_SWITCH(func)  \
394                                 void __attribute__((interrupt)) func(void)
395                 #endif
396
397         #endif /* !__IAR_SYSTEMS_ICC_ */
398
399 #elif CPU_PPC
400
401         /* Get IRQ_* definitions from the hosting environment. */
402         #include <cfg/os.h>
403         #if OS_EMBEDDED
404                 #define IRQ_DISABLE         FIXME
405                 #define IRQ_ENABLE          FIXME
406                 #define IRQ_SAVE_DISABLE(x) FIXME
407                 #define IRQ_RESTORE(x)      FIXME
408                 #define IRQ_ENABLED()       FIXME
409         #endif /* OS_EMBEDDED */
410
411 #elif CPU_DSP56K
412
413         #define IRQ_DISABLE             do { asm(bfset #0x0200,SR); asm(nop); } while (0)
414         #define IRQ_ENABLE              do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
415
416         #define IRQ_SAVE_DISABLE(x)  \
417                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
418         #define IRQ_RESTORE(x)  \
419                 do { (void)x; asm(move x,SR); } while (0)
420
421         static inline bool irq_running(void)
422         {
423                 extern void *user_sp;
424                 return !!user_sp;
425         }
426         #define IRQ_RUNNING() irq_running()
427
428         static inline bool irq_enabled(void)
429         {
430                 uint16_t x;
431                 asm(move SR,x);
432                 return !(x & 0x0200);
433         }
434         #define IRQ_ENABLED() irq_enabled()
435
436 #elif CPU_AVR
437
438         #define IRQ_DISABLE   asm volatile ("cli" ::)
439         #define IRQ_ENABLE    asm volatile ("sei" ::)
440
441         #define IRQ_SAVE_DISABLE(x) \
442         do { \
443                 __asm__ __volatile__( \
444                         "in %0,__SREG__\n\t" \
445                         "cli" \
446                         : "=r" (x) : /* no inputs */ : "cc" \
447                 ); \
448         } while (0)
449
450         #define IRQ_RESTORE(x) \
451         do { \
452                 __asm__ __volatile__( \
453                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
454                 ); \
455         } while (0)
456
457         #define IRQ_ENABLED() \
458         ({ \
459                 uint8_t sreg; \
460                 __asm__ __volatile__( \
461                         "in %0,__SREG__\n\t" \
462                         : "=r" (sreg)  /* no inputs & no clobbers */ \
463                 ); \
464                 (bool)(sreg & 0x80); \
465         })
466         #if CONFIG_KERN_PREEMPT
467                 #define DECLARE_ISR_CONTEXT_SWITCH(vect)                \
468                         INLINE void __isr_##vect(void);                 \
469                         ISR(vect)                                       \
470                         {                                               \
471                                 __isr_##vect();                         \
472                                 IRQ_PREEMPT_HANDLER();                  \
473                         }                                               \
474                         INLINE void __isr_##vect(void)
475
476                 /**
477                  * With task priorities enabled each ISR is used a point to
478                  * check if we need to perform a context switch.
479                  *
480                  * Instead, without priorities a context switch can occur only
481                  * when the running task expires its time quantum. In this last
482                  * case, the context switch can only occur in the timer ISR,
483                  * that must be always declared with the
484                  * DECLARE_ISR_CONTEXT_SWITCH() macro.
485                  */
486                 #if CONFIG_KERN_PRI
487                         #define DECLARE_ISR(func) \
488                                 DECLARE_ISR_CONTEXT_SWITCH(func)
489                         /**
490                          * Interrupt service routine prototype: can be used for
491                          * forward declarations.
492                          */
493                         #define ISR_PROTO(func) \
494                                 ISR_PROTO_CONTEXT_SWITCH(func)
495                 #endif /* !CONFIG_KERN_PRI */
496         #endif
497
498         #ifndef ISR_PROTO
499                 #define ISR_PROTO(vect) ISR(vect)
500         #endif
501         #ifndef DECLARE_ISR
502                 #define DECLARE_ISR(vect) ISR(vect)
503         #endif
504         #ifndef DECLARE_ISR_CONTEXT_SWITCH
505                 #define DECLARE_ISR_CONTEXT_SWITCH(vect) ISR(vect)
506         #endif
507         #ifndef ISR_PROTO_CONTEXT_SWITCH
508                 #define ISR_PROTO_CONTEXT_SWITCH(vect) ISR(vect)
509         #endif
510
511 #else
512         #error No CPU_... defined.
513 #endif
514
515 #ifdef IRQ_RUNNING
516         /// Ensure callee is running within an interrupt
517         #define ASSERT_IRQ_CONTEXT()  ASSERT(IRQ_RUNNING())
518
519         /// Ensure callee is not running within an interrupt
520         #define ASSERT_USER_CONTEXT() ASSERT(!IRQ_RUNNING())
521 #else
522         #define IRQ_RUNNING()   false
523         #define ASSERT_USER_CONTEXT()  do {} while(0)
524         #define ASSERT_IRQ_CONTEXT()   do {} while(0)
525 #endif
526
527 #ifdef IRQ_ENABLED
528         /// Ensure interrupts are enabled
529         #define IRQ_ASSERT_ENABLED()  ASSERT(IRQ_ENABLED())
530
531         /// Ensure interrupts are not enabled
532         #define IRQ_ASSERT_DISABLED() ASSERT(!IRQ_ENABLED())
533 #else
534         #define IRQ_ASSERT_ENABLED() do {} while(0)
535         #define IRQ_ASSERT_DISABLED() do {} while(0)
536 #endif
537
538
539 #ifndef IRQ_PREEMPT_HANDLER
540         #if CONFIG_KERN_PREEMPT
541                 /**
542                  * Handle preemptive context switch inside timer IRQ.
543                  */
544                 INLINE void IRQ_PREEMPT_HANDLER(void)
545                 {
546                         if (proc_needPreempt())
547                                 proc_preempt();
548                 }
549         #else
550                 #define IRQ_PREEMPT_HANDLER() /* Nothing */
551         #endif
552 #endif
553
554 /**
555  * Execute \a CODE atomically with respect to interrupts.
556  *
557  * \see IRQ_SAVE_DISABLE IRQ_RESTORE
558  */
559 #define ATOMIC(CODE) \
560         do { \
561                 cpu_flags_t __flags; \
562                 IRQ_SAVE_DISABLE(__flags); \
563                 CODE; \
564                 IRQ_RESTORE(__flags); \
565         } while (0)
566
567 #endif /* CPU_IRQ_H */