hardirq.h 5.3 KB

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