Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / include / linux / preempt_mask.h
1 #ifndef LINUX_PREEMPT_MASK_H
2 #define LINUX_PREEMPT_MASK_H
3
4 #include <linux/preempt.h>
5
6 /*
7  * We put the hardirq and softirq counter into the preemption
8  * counter. The bitmask has the following meaning:
9  *
10  * - bits 0-7 are the preemption count (max preemption depth: 256)
11  * - bits 8-15 are the softirq count (max # of softirqs: 256)
12  *
13  * The hardirq count could in theory be the same as the number of
14  * interrupts in the system, but we run all interrupt handlers with
15  * interrupts disabled, so we cannot have nesting interrupts. Though
16  * there are a few palaeontologic drivers which reenable interrupts in
17  * the handler, so we need more than one bit here.
18  *
19  * PREEMPT_MASK:        0x000000ff
20  * SOFTIRQ_MASK:        0x0000ff00
21  * HARDIRQ_MASK:        0x000f0000
22  *     NMI_MASK:        0x00100000
23  * PREEMPT_ACTIVE:      0x00200000
24  */
25 #define PREEMPT_BITS    8
26 #define SOFTIRQ_BITS    8
27 #define HARDIRQ_BITS    4
28 #define NMI_BITS        1
29
30 #define PREEMPT_SHIFT   0
31 #define SOFTIRQ_SHIFT   (PREEMPT_SHIFT + PREEMPT_BITS)
32 #define HARDIRQ_SHIFT   (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
33 #define NMI_SHIFT       (HARDIRQ_SHIFT + HARDIRQ_BITS)
34
35 #define __IRQ_MASK(x)   ((1UL << (x))-1)
36
37 #define PREEMPT_MASK    (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
38 #define SOFTIRQ_MASK    (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
39 #define HARDIRQ_MASK    (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
40 #define NMI_MASK        (__IRQ_MASK(NMI_BITS)     << NMI_SHIFT)
41
42 #define PREEMPT_OFFSET  (1UL << PREEMPT_SHIFT)
43 #define SOFTIRQ_OFFSET  (1UL << SOFTIRQ_SHIFT)
44 #define HARDIRQ_OFFSET  (1UL << HARDIRQ_SHIFT)
45 #define NMI_OFFSET      (1UL << NMI_SHIFT)
46
47 #ifndef CONFIG_PREEMPT_RT_FULL
48 # define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
49 #else
50 # define SOFTIRQ_DISABLE_OFFSET (0)
51 #endif
52
53 #define PREEMPT_ACTIVE_BITS     1
54 #define PREEMPT_ACTIVE_SHIFT    (NMI_SHIFT + NMI_BITS)
55 #define PREEMPT_ACTIVE  (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
56
57 #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
58 #define irq_count()     (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
59                                  | NMI_MASK))
60 #ifndef CONFIG_PREEMPT_RT_FULL
61 # define softirq_count()        (preempt_count() & SOFTIRQ_MASK)
62 # define in_serving_softirq()   (softirq_count() & SOFTIRQ_OFFSET)
63 #else
64 # define softirq_count()        (0UL)
65 extern int in_serving_softirq(void);
66 #endif
67
68 /*
69  * Are we doing bottom half or hardware interrupt processing?
70  * Are we in a softirq context? Interrupt context?
71  * in_softirq - Are we currently processing softirq or have bh disabled?
72  * in_serving_softirq - Are we currently processing softirq?
73  */
74 #define in_irq()                (hardirq_count())
75 #define in_softirq()            (softirq_count())
76 #define in_interrupt()          (irq_count())
77
78 /*
79  * Are we in NMI context?
80  */
81 #define in_nmi()        (preempt_count() & NMI_MASK)
82
83 #if defined(CONFIG_PREEMPT_COUNT)
84 # define PREEMPT_CHECK_OFFSET 1
85 #else
86 # define PREEMPT_CHECK_OFFSET 0
87 #endif
88
89 /*
90  * The preempt_count offset needed for things like:
91  *
92  *  spin_lock_bh()
93  *
94  * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
95  * softirqs, such that unlock sequences of:
96  *
97  *  spin_unlock();
98  *  local_bh_enable();
99  *
100  * Work as expected.
101  */
102 #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_CHECK_OFFSET)
103
104 /*
105  * Are we running in atomic context?  WARNING: this macro cannot
106  * always detect atomic context; in particular, it cannot know about
107  * held spinlocks in non-preemptible kernels.  Thus it should not be
108  * used in the general case to determine whether sleeping is possible.
109  * Do not use in_atomic() in driver code.
110  */
111 #define in_atomic()     ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
112
113 /*
114  * Check whether we were atomic before we did preempt_disable():
115  * (used by the scheduler, *after* releasing the kernel lock)
116  */
117 #define in_atomic_preempt_off() \
118                 ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
119
120 #ifdef CONFIG_PREEMPT_COUNT
121 # define preemptible()  (preempt_count() == 0 && !irqs_disabled())
122 #else
123 # define preemptible()  0
124 #endif
125
126 #endif /* LINUX_PREEMPT_MASK_H */