hardirq.h 6.4 KB

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