hardirq.h 5.9 KB

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