hardirq.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 be overridden per architecture, the default is:
  17. *
  18. * - bits 16-27 are the hardirq count (max # of hardirqs: 4096)
  19. * - ( bit 28 is the PREEMPT_ACTIVE flag. )
  20. *
  21. * PREEMPT_MASK: 0x000000ff
  22. * SOFTIRQ_MASK: 0x0000ff00
  23. * HARDIRQ_MASK: 0x0fff0000
  24. */
  25. #define PREEMPT_BITS 8
  26. #define SOFTIRQ_BITS 8
  27. #ifndef HARDIRQ_BITS
  28. #define HARDIRQ_BITS 12
  29. #ifndef MAX_HARDIRQS_PER_CPU
  30. #define MAX_HARDIRQS_PER_CPU NR_IRQS
  31. #endif
  32. /*
  33. * The hardirq mask has to be large enough to have space for potentially
  34. * all IRQ sources in the system nesting on a single CPU.
  35. */
  36. #if (1 << HARDIRQ_BITS) < MAX_HARDIRQS_PER_CPU
  37. # error HARDIRQ_BITS is too low!
  38. #endif
  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 __IRQ_MASK(x) ((1UL << (x))-1)
  44. #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
  45. #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
  46. #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
  47. #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
  48. #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
  49. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  50. #if PREEMPT_ACTIVE < (1 << (HARDIRQ_SHIFT + HARDIRQ_BITS))
  51. #error PREEMPT_ACTIVE is too low!
  52. #endif
  53. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  54. #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  55. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK))
  56. /*
  57. * Are we doing bottom half or hardware interrupt processing?
  58. * Are we in a softirq context? Interrupt context?
  59. */
  60. #define in_irq() (hardirq_count())
  61. #define in_softirq() (softirq_count())
  62. #define in_interrupt() (irq_count())
  63. #if defined(CONFIG_PREEMPT)
  64. # define PREEMPT_INATOMIC_BASE kernel_locked()
  65. # define PREEMPT_CHECK_OFFSET 1
  66. #else
  67. # define PREEMPT_INATOMIC_BASE 0
  68. # define PREEMPT_CHECK_OFFSET 0
  69. #endif
  70. /*
  71. * Are we running in atomic context? WARNING: this macro cannot
  72. * always detect atomic context; in particular, it cannot know about
  73. * held spinlocks in non-preemptible kernels. Thus it should not be
  74. * used in the general case to determine whether sleeping is possible.
  75. * Do not use in_atomic() in driver code.
  76. */
  77. #define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_INATOMIC_BASE)
  78. /*
  79. * Check whether we were atomic before we did preempt_disable():
  80. * (used by the scheduler, *after* releasing the kernel lock)
  81. */
  82. #define in_atomic_preempt_off() \
  83. ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
  84. #ifdef CONFIG_PREEMPT
  85. # define preemptible() (preempt_count() == 0 && !irqs_disabled())
  86. # define IRQ_EXIT_OFFSET (HARDIRQ_OFFSET-1)
  87. #else
  88. # define preemptible() 0
  89. # define IRQ_EXIT_OFFSET HARDIRQ_OFFSET
  90. #endif
  91. #ifdef CONFIG_SMP
  92. extern void synchronize_irq(unsigned int irq);
  93. #else
  94. # define synchronize_irq(irq) barrier()
  95. #endif
  96. struct task_struct;
  97. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  98. static inline void account_system_vtime(struct task_struct *tsk)
  99. {
  100. }
  101. #endif
  102. #if defined(CONFIG_PREEMPT_RCU) && defined(CONFIG_NO_HZ)
  103. extern void rcu_irq_enter(void);
  104. extern void rcu_irq_exit(void);
  105. #else
  106. # define rcu_irq_enter() do { } while (0)
  107. # define rcu_irq_exit() do { } while (0)
  108. #endif /* CONFIG_PREEMPT_RCU */
  109. /*
  110. * It is safe to do non-atomic ops on ->hardirq_context,
  111. * because NMI handlers may not preempt and the ops are
  112. * always balanced, so the interrupted value of ->hardirq_context
  113. * will always be restored.
  114. */
  115. #define __irq_enter() \
  116. do { \
  117. rcu_irq_enter(); \
  118. account_system_vtime(current); \
  119. add_preempt_count(HARDIRQ_OFFSET); \
  120. trace_hardirq_enter(); \
  121. } while (0)
  122. /*
  123. * Enter irq context (on NO_HZ, update jiffies):
  124. */
  125. extern void irq_enter(void);
  126. /*
  127. * Exit irq context without processing softirqs:
  128. */
  129. #define __irq_exit() \
  130. do { \
  131. trace_hardirq_exit(); \
  132. account_system_vtime(current); \
  133. sub_preempt_count(HARDIRQ_OFFSET); \
  134. rcu_irq_exit(); \
  135. } while (0)
  136. /*
  137. * Exit irq context and process softirqs if needed:
  138. */
  139. extern void irq_exit(void);
  140. #define nmi_enter() \
  141. do { \
  142. ftrace_nmi_enter(); \
  143. lockdep_off(); \
  144. __irq_enter(); \
  145. } while (0)
  146. #define nmi_exit() \
  147. do { \
  148. __irq_exit(); \
  149. lockdep_on(); \
  150. ftrace_nmi_exit(); \
  151. } while (0)
  152. #endif /* LINUX_HARDIRQ_H */