hardirq.h 5.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
#ifndef LINUX_HARDIRQ_H
#define LINUX_HARDIRQ_H

R
Randy Dunlap 已提交
4
#include <linux/preempt.h>
I
Ingo Molnar 已提交
5
#include <linux/lockdep.h>
6
#include <linux/ftrace_irq.h>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15
#include <asm/hardirq.h>

/*
 * We put the hardirq and softirq counter into the preemption
 * counter. The bitmask has the following meaning:
 *
 * - bits 0-7 are the preemption count (max preemption depth: 256)
 * - bits 8-15 are the softirq count (max # of softirqs: 256)
 *
16 17 18 19 20 21
 * The hardirq count can in theory reach the same as NR_IRQS.
 * In reality, the number of nested IRQS is limited to the stack
 * size as well. For archs with over 1000 IRQS it is not practical
 * to expect that they will all nest. We give a max of 10 bits for
 * hardirq nesting. An arch may choose to give less than 10 bits.
 * m68k expects it to be 8.
L
Linus Torvalds 已提交
22
 *
23 24
 * - bits 16-25 are the hardirq count (max # of nested hardirqs: 1024)
 * - bit 26 is the NMI_MASK
25
 * - bit 27 is the PREEMPT_ACTIVE flag
L
Linus Torvalds 已提交
26 27 28
 *
 * PREEMPT_MASK: 0x000000ff
 * SOFTIRQ_MASK: 0x0000ff00
29 30
 * HARDIRQ_MASK: 0x03ff0000
 *     NMI_MASK: 0x04000000
L
Linus Torvalds 已提交
31 32 33
 */
#define PREEMPT_BITS	8
#define SOFTIRQ_BITS	8
34
#define NMI_BITS	1
L
Linus Torvalds 已提交
35

36
#define MAX_HARDIRQ_BITS 10
37

38 39
#ifndef HARDIRQ_BITS
# define HARDIRQ_BITS	MAX_HARDIRQ_BITS
40 41
#endif

42 43
#if HARDIRQ_BITS > MAX_HARDIRQ_BITS
#error HARDIRQ_BITS too high!
L
Linus Torvalds 已提交
44 45 46 47 48
#endif

#define PREEMPT_SHIFT	0
#define SOFTIRQ_SHIFT	(PREEMPT_SHIFT + PREEMPT_BITS)
#define HARDIRQ_SHIFT	(SOFTIRQ_SHIFT + SOFTIRQ_BITS)
49
#define NMI_SHIFT	(HARDIRQ_SHIFT + HARDIRQ_BITS)
L
Linus Torvalds 已提交
50 51 52 53 54

#define __IRQ_MASK(x)	((1UL << (x))-1)

#define PREEMPT_MASK	(__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
#define SOFTIRQ_MASK	(__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
55
#define HARDIRQ_MASK	(__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
56
#define NMI_MASK	(__IRQ_MASK(NMI_BITS)     << NMI_SHIFT)
L
Linus Torvalds 已提交
57 58 59 60

#define PREEMPT_OFFSET	(1UL << PREEMPT_SHIFT)
#define SOFTIRQ_OFFSET	(1UL << SOFTIRQ_SHIFT)
#define HARDIRQ_OFFSET	(1UL << HARDIRQ_SHIFT)
61
#define NMI_OFFSET	(1UL << NMI_SHIFT)
L
Linus Torvalds 已提交
62

63 64
#define SOFTIRQ_DISABLE_OFFSET	(2 * SOFTIRQ_OFFSET)

65 66 67 68 69 70
#ifndef PREEMPT_ACTIVE
#define PREEMPT_ACTIVE_BITS	1
#define PREEMPT_ACTIVE_SHIFT	(NMI_SHIFT + NMI_BITS)
#define PREEMPT_ACTIVE	(__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
#endif

71
#if PREEMPT_ACTIVE < (1 << (NMI_SHIFT + NMI_BITS))
72 73 74
#error PREEMPT_ACTIVE is too low!
#endif

L
Linus Torvalds 已提交
75 76
#define hardirq_count()	(preempt_count() & HARDIRQ_MASK)
#define softirq_count()	(preempt_count() & SOFTIRQ_MASK)
77 78
#define irq_count()	(preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
				 | NMI_MASK))
L
Linus Torvalds 已提交
79 80 81 82

/*
 * Are we doing bottom half or hardware interrupt processing?
 * Are we in a softirq context? Interrupt context?
83 84
 * in_softirq - Are we currently processing softirq or have bh disabled?
 * in_serving_softirq - Are we currently processing softirq?
L
Linus Torvalds 已提交
85 86 87 88
 */
#define in_irq()		(hardirq_count())
#define in_softirq()		(softirq_count())
#define in_interrupt()		(irq_count())
89
#define in_serving_softirq()	(softirq_count() & SOFTIRQ_OFFSET)
L
Linus Torvalds 已提交
90

91 92 93
/*
 * Are we in NMI context?
 */
94
#define in_nmi()	(preempt_count() & NMI_MASK)
95

96
#if defined(CONFIG_PREEMPT_COUNT)
97 98
# define PREEMPT_CHECK_OFFSET 1
#else
99 100 101
# define PREEMPT_CHECK_OFFSET 0
#endif

102 103 104 105 106 107 108
/*
 * Are we running in atomic context?  WARNING: this macro cannot
 * always detect atomic context; in particular, it cannot know about
 * held spinlocks in non-preemptible kernels.  Thus it should not be
 * used in the general case to determine whether sleeping is possible.
 * Do not use in_atomic() in driver code.
 */
A
Arnd Bergmann 已提交
109
#define in_atomic()	((preempt_count() & ~PREEMPT_ACTIVE) != 0)
I
Ingo Molnar 已提交
110 111 112

/*
 * Check whether we were atomic before we did preempt_disable():
113
 * (used by the scheduler, *after* releasing the kernel lock)
I
Ingo Molnar 已提交
114 115 116 117
 */
#define in_atomic_preempt_off() \
		((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)

118
#ifdef CONFIG_PREEMPT_COUNT
L
Linus Torvalds 已提交
119 120 121 122 123 124 125
# define preemptible()	(preempt_count() == 0 && !irqs_disabled())
# define IRQ_EXIT_OFFSET (HARDIRQ_OFFSET-1)
#else
# define preemptible()	0
# define IRQ_EXIT_OFFSET HARDIRQ_OFFSET
#endif

126
#if defined(CONFIG_SMP) || defined(CONFIG_GENERIC_HARDIRQS)
L
Linus Torvalds 已提交
127 128 129 130 131
extern void synchronize_irq(unsigned int irq);
#else
# define synchronize_irq(irq)	barrier()
#endif

A
Al Viro 已提交
132 133
struct task_struct;

134
#if !defined(CONFIG_VIRT_CPU_ACCOUNTING) && !defined(CONFIG_IRQ_TIME_ACCOUNTING)
135
static inline void vtime_account(struct task_struct *tsk)
L
Linus Torvalds 已提交
136 137
{
}
138
#else
139
extern void vtime_account(struct task_struct *tsk);
L
Linus Torvalds 已提交
140 141
#endif

P
Paul E. McKenney 已提交
142
#if defined(CONFIG_TINY_RCU) || defined(CONFIG_TINY_PREEMPT_RCU)
143 144 145 146 147 148 149 150 151 152

static inline void rcu_nmi_enter(void)
{
}

static inline void rcu_nmi_exit(void)
{
}

#else
153 154
extern void rcu_nmi_enter(void);
extern void rcu_nmi_exit(void);
155
#endif
156

157 158 159 160 161 162
/*
 * It is safe to do non-atomic ops on ->hardirq_context,
 * because NMI handlers may not preempt and the ops are
 * always balanced, so the interrupted value of ->hardirq_context
 * will always be restored.
 */
163 164
#define __irq_enter()					\
	do {						\
165
		vtime_account(current);		\
166 167 168 169 170 171 172
		add_preempt_count(HARDIRQ_OFFSET);	\
		trace_hardirq_enter();			\
	} while (0)

/*
 * Enter irq context (on NO_HZ, update jiffies):
 */
I
Ingo Molnar 已提交
173
extern void irq_enter(void);
174 175 176 177 178 179 180

/*
 * Exit irq context without processing softirqs:
 */
#define __irq_exit()					\
	do {						\
		trace_hardirq_exit();			\
181
		vtime_account(current);		\
182
		sub_preempt_count(HARDIRQ_OFFSET);	\
L
Linus Torvalds 已提交
183 184
	} while (0)

185 186 187
/*
 * Exit irq context and process softirqs if needed:
 */
L
Linus Torvalds 已提交
188 189
extern void irq_exit(void);

S
Steven Rostedt 已提交
190 191 192 193 194 195 196 197
#define nmi_enter()						\
	do {							\
		ftrace_nmi_enter();				\
		BUG_ON(in_nmi());				\
		add_preempt_count(NMI_OFFSET + HARDIRQ_OFFSET);	\
		lockdep_off();					\
		rcu_nmi_enter();				\
		trace_hardirq_enter();				\
198
	} while (0)
199

S
Steven Rostedt 已提交
200 201 202 203 204 205 206 207
#define nmi_exit()						\
	do {							\
		trace_hardirq_exit();				\
		rcu_nmi_exit();					\
		lockdep_on();					\
		BUG_ON(!in_nmi());				\
		sub_preempt_count(NMI_OFFSET + HARDIRQ_OFFSET);	\
		ftrace_nmi_exit();				\
208
	} while (0)
209

L
Linus Torvalds 已提交
210
#endif /* LINUX_HARDIRQ_H */