hardirq.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifndef LINUX_HARDIRQ_H
  2. #define LINUX_HARDIRQ_H
  3. #include <linux/preempt.h>
  4. #include <linux/lockdep.h>
  5. #include <linux/ftrace_irq.h>
  6. #include <asm/hardirq.h>
  7. /*
  8. * We put the hardirq and softirq counter into the preemption
  9. * counter. The bitmask has the following meaning:
  10. *
  11. * - bits 0-7 are the preemption count (max preemption depth: 256)
  12. * - bits 8-15 are the softirq count (max # of softirqs: 256)
  13. *
  14. * The hardirq count can in theory reach the same as NR_IRQS.
  15. * In reality, the number of nested IRQS is limited to the stack
  16. * size as well. For archs with over 1000 IRQS it is not practical
  17. * to expect that they will all nest. We give a max of 10 bits for
  18. * hardirq nesting. An arch may choose to give less than 10 bits.
  19. * m68k expects it to be 8.
  20. *
  21. * - bits 16-25 are the hardirq count (max # of nested hardirqs: 1024)
  22. * - bit 26 is the NMI_MASK
  23. * - bit 28 is the PREEMPT_ACTIVE flag
  24. *
  25. * PREEMPT_MASK: 0x000000ff
  26. * SOFTIRQ_MASK: 0x0000ff00
  27. * HARDIRQ_MASK: 0x03ff0000
  28. * NMI_MASK: 0x04000000
  29. */
  30. #define PREEMPT_BITS 8
  31. #define SOFTIRQ_BITS 8
  32. #define NMI_BITS 1
  33. #define MAX_HARDIRQ_BITS 10
  34. #ifndef HARDIRQ_BITS
  35. # define HARDIRQ_BITS MAX_HARDIRQ_BITS
  36. #endif
  37. #if HARDIRQ_BITS > MAX_HARDIRQ_BITS
  38. #error HARDIRQ_BITS too high!
  39. #endif
  40. #define PREEMPT_SHIFT 0
  41. #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
  42. #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
  43. #define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
  44. #define __IRQ_MASK(x) ((1UL << (x))-1)
  45. #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
  46. #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
  47. #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
  48. #define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
  49. #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
  50. #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
  51. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  52. #define NMI_OFFSET (1UL << NMI_SHIFT)
  53. #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  54. #ifndef PREEMPT_ACTIVE
  55. #define PREEMPT_ACTIVE_BITS 1
  56. #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
  57. #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
  58. #endif
  59. #if PREEMPT_ACTIVE < (1 << (NMI_SHIFT + NMI_BITS))
  60. #error PREEMPT_ACTIVE is too low!
  61. #endif
  62. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  63. #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  64. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  65. | NMI_MASK))
  66. /*
  67. * Are we doing bottom half or hardware interrupt processing?
  68. * Are we in a softirq context? Interrupt context?
  69. * in_softirq - Are we currently processing softirq or have bh disabled?
  70. * in_serving_softirq - Are we currently processing softirq?
  71. */
  72. #define in_irq() (hardirq_count())
  73. #define in_softirq() (softirq_count())
  74. #define in_interrupt() (irq_count())
  75. #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  76. /*
  77. * Are we in NMI context?
  78. */
  79. #define in_nmi() (preempt_count() & NMI_MASK)
  80. #if defined(CONFIG_PREEMPT) && defined(CONFIG_BKL)
  81. # include <linux/sched.h>
  82. # define PREEMPT_INATOMIC_BASE (current->lock_depth >= 0)
  83. #else
  84. # define PREEMPT_INATOMIC_BASE 0
  85. #endif
  86. #if defined(CONFIG_PREEMPT)
  87. # define PREEMPT_CHECK_OFFSET 1
  88. #else
  89. # define PREEMPT_CHECK_OFFSET 0
  90. #endif
  91. /*
  92. * Are we running in atomic context? WARNING: this macro cannot
  93. * always detect atomic context; in particular, it cannot know about
  94. * held spinlocks in non-preemptible kernels. Thus it should not be
  95. * used in the general case to determine whether sleeping is possible.
  96. * Do not use in_atomic() in driver code.
  97. */
  98. #define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_INATOMIC_BASE)
  99. /*
  100. * Check whether we were atomic before we did preempt_disable():
  101. * (used by the scheduler, *after* releasing the kernel lock)
  102. */
  103. #define in_atomic_preempt_off() \
  104. ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
  105. #ifdef CONFIG_PREEMPT
  106. # define preemptible() (preempt_count() == 0 && !irqs_disabled())
  107. # define IRQ_EXIT_OFFSET (HARDIRQ_OFFSET-1)
  108. #else
  109. # define preemptible() 0
  110. # define IRQ_EXIT_OFFSET HARDIRQ_OFFSET
  111. #endif
  112. #if defined(CONFIG_SMP) || defined(CONFIG_GENERIC_HARDIRQS)
  113. extern void synchronize_irq(unsigned int irq);
  114. #else
  115. # define synchronize_irq(irq) barrier()
  116. #endif
  117. struct task_struct;
  118. #if !defined(CONFIG_VIRT_CPU_ACCOUNTING) && !defined(CONFIG_IRQ_TIME_ACCOUNTING)
  119. static inline void account_system_vtime(struct task_struct *tsk)
  120. {
  121. }
  122. #else
  123. extern void account_system_vtime(struct task_struct *tsk);
  124. #endif
  125. #if defined(CONFIG_NO_HZ)
  126. #if defined(CONFIG_TINY_RCU) || defined(CONFIG_TINY_PREEMPT_RCU)
  127. extern void rcu_enter_nohz(void);
  128. extern void rcu_exit_nohz(void);
  129. static inline void rcu_irq_enter(void)
  130. {
  131. rcu_exit_nohz();
  132. }
  133. static inline void rcu_irq_exit(void)
  134. {
  135. rcu_enter_nohz();
  136. }
  137. static inline void rcu_nmi_enter(void)
  138. {
  139. }
  140. static inline void rcu_nmi_exit(void)
  141. {
  142. }
  143. #else
  144. extern void rcu_irq_enter(void);
  145. extern void rcu_irq_exit(void);
  146. extern void rcu_nmi_enter(void);
  147. extern void rcu_nmi_exit(void);
  148. #endif
  149. #else
  150. # define rcu_irq_enter() do { } while (0)
  151. # define rcu_irq_exit() do { } while (0)
  152. # define rcu_nmi_enter() do { } while (0)
  153. # define rcu_nmi_exit() do { } while (0)
  154. #endif /* #if defined(CONFIG_NO_HZ) */
  155. /*
  156. * It is safe to do non-atomic ops on ->hardirq_context,
  157. * because NMI handlers may not preempt and the ops are
  158. * always balanced, so the interrupted value of ->hardirq_context
  159. * will always be restored.
  160. */
  161. #define __irq_enter() \
  162. do { \
  163. account_system_vtime(current); \
  164. add_preempt_count(HARDIRQ_OFFSET); \
  165. trace_hardirq_enter(); \
  166. } while (0)
  167. /*
  168. * Enter irq context (on NO_HZ, update jiffies):
  169. */
  170. extern void irq_enter(void);
  171. /*
  172. * Exit irq context without processing softirqs:
  173. */
  174. #define __irq_exit() \
  175. do { \
  176. trace_hardirq_exit(); \
  177. account_system_vtime(current); \
  178. sub_preempt_count(HARDIRQ_OFFSET); \
  179. } while (0)
  180. /*
  181. * Exit irq context and process softirqs if needed:
  182. */
  183. extern void irq_exit(void);
  184. #define nmi_enter() \
  185. do { \
  186. ftrace_nmi_enter(); \
  187. BUG_ON(in_nmi()); \
  188. add_preempt_count(NMI_OFFSET + HARDIRQ_OFFSET); \
  189. lockdep_off(); \
  190. rcu_nmi_enter(); \
  191. trace_hardirq_enter(); \
  192. } while (0)
  193. #define nmi_exit() \
  194. do { \
  195. trace_hardirq_exit(); \
  196. rcu_nmi_exit(); \
  197. lockdep_on(); \
  198. BUG_ON(!in_nmi()); \
  199. sub_preempt_count(NMI_OFFSET + HARDIRQ_OFFSET); \
  200. ftrace_nmi_exit(); \
  201. } while (0)
  202. #endif /* LINUX_HARDIRQ_H */