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 #define SOFTIRQ_DISABLE_OFFSET	(2 * SOFTIRQ_OFFSET)
48 
49 #define PREEMPT_ACTIVE_BITS	1
50 #define PREEMPT_ACTIVE_SHIFT	(NMI_SHIFT + NMI_BITS)
51 #define PREEMPT_ACTIVE	(__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
52 
53 #define hardirq_count()	(preempt_count() & HARDIRQ_MASK)
54 #define softirq_count()	(preempt_count() & SOFTIRQ_MASK)
55 #define irq_count()	(preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
56 				 | NMI_MASK))
57 
58 /*
59  * Are we doing bottom half or hardware interrupt processing?
60  * Are we in a softirq context? Interrupt context?
61  * in_softirq - Are we currently processing softirq or have bh disabled?
62  * in_serving_softirq - Are we currently processing softirq?
63  */
64 #define in_irq()		(hardirq_count())
65 #define in_softirq()		(softirq_count())
66 #define in_interrupt()		(irq_count())
67 #define in_serving_softirq()	(softirq_count() & SOFTIRQ_OFFSET)
68 
69 /*
70  * Are we in NMI context?
71  */
72 #define in_nmi()	(preempt_count() & NMI_MASK)
73 
74 /*
75  * The preempt_count offset after preempt_disable();
76  */
77 #if defined(CONFIG_PREEMPT_COUNT)
78 # define PREEMPT_DISABLE_OFFSET	PREEMPT_OFFSET
79 #else
80 # define PREEMPT_DISABLE_OFFSET	0
81 #endif
82 
83 /*
84  * The preempt_count offset after spin_lock()
85  */
86 #define PREEMPT_LOCK_OFFSET	PREEMPT_DISABLE_OFFSET
87 
88 /*
89  * The preempt_count offset needed for things like:
90  *
91  *  spin_lock_bh()
92  *
93  * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
94  * softirqs, such that unlock sequences of:
95  *
96  *  spin_unlock();
97  *  local_bh_enable();
98  *
99  * Work as expected.
100  */
101 #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
102 
103 /*
104  * Are we running in atomic context?  WARNING: this macro cannot
105  * always detect atomic context; in particular, it cannot know about
106  * held spinlocks in non-preemptible kernels.  Thus it should not be
107  * used in the general case to determine whether sleeping is possible.
108  * Do not use in_atomic() in driver code.
109  */
110 #define in_atomic()	((preempt_count() & ~PREEMPT_ACTIVE) != 0)
111 
112 /*
113  * Check whether we were atomic before we did preempt_disable():
114  * (used by the scheduler, *after* releasing the kernel lock)
115  */
116 #define in_atomic_preempt_off() \
117 		((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_DISABLE_OFFSET)
118 
119 #ifdef CONFIG_PREEMPT_COUNT
120 # define preemptible()	(preempt_count() == 0 && !irqs_disabled())
121 #else
122 # define preemptible()	0
123 #endif
124 
125 #endif /* LINUX_PREEMPT_MASK_H */
126