resend.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  4. * Copyright (C) 2005-2006, Thomas Gleixner
  5. *
  6. * This file contains the IRQ-resend code
  7. *
  8. * If the interrupt is waiting to be processed, we try to re-run it.
  9. * We can't directly run it from here since the caller might be in an
  10. * interrupt-protected region. Not all irq controller chips can
  11. * retrigger interrupts at the hardware level, so in those cases
  12. * we allow the resending of IRQs via a tasklet.
  13. */
  14. #include <linux/irq.h>
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/interrupt.h>
  18. #include "internals.h"
  19. #ifdef CONFIG_HARDIRQS_SW_RESEND
  20. /* hlist_head to handle software resend of interrupts: */
  21. static HLIST_HEAD(irq_resend_list);
  22. static DEFINE_RAW_SPINLOCK(irq_resend_lock);
  23. /*
  24. * Run software resends of IRQ's
  25. */
  26. static void resend_irqs(struct tasklet_struct *unused)
  27. {
  28. struct irq_desc *desc;
  29. raw_spin_lock_irq(&irq_resend_lock);
  30. while (!hlist_empty(&irq_resend_list)) {
  31. desc = hlist_entry(irq_resend_list.first, struct irq_desc,
  32. resend_node);
  33. hlist_del_init(&desc->resend_node);
  34. raw_spin_unlock(&irq_resend_lock);
  35. desc->handle_irq(desc);
  36. raw_spin_lock(&irq_resend_lock);
  37. }
  38. raw_spin_unlock_irq(&irq_resend_lock);
  39. }
  40. /* Tasklet to handle resend: */
  41. static DECLARE_TASKLET(resend_tasklet, resend_irqs);
  42. static int irq_sw_resend(struct irq_desc *desc)
  43. {
  44. /*
  45. * Validate whether this interrupt can be safely injected from
  46. * non interrupt context
  47. */
  48. if (handle_enforce_irqctx(&desc->irq_data))
  49. return -EINVAL;
  50. /*
  51. * If the interrupt is running in the thread context of the parent
  52. * irq we need to be careful, because we cannot trigger it
  53. * directly.
  54. */
  55. if (irq_settings_is_nested_thread(desc)) {
  56. /*
  57. * If the parent_irq is valid, we retrigger the parent,
  58. * otherwise we do nothing.
  59. */
  60. if (!desc->parent_irq)
  61. return -EINVAL;
  62. desc = irq_to_desc(desc->parent_irq);
  63. if (!desc)
  64. return -EINVAL;
  65. }
  66. /* Add to resend_list and activate the softirq: */
  67. raw_spin_lock(&irq_resend_lock);
  68. if (hlist_unhashed(&desc->resend_node))
  69. hlist_add_head(&desc->resend_node, &irq_resend_list);
  70. raw_spin_unlock(&irq_resend_lock);
  71. tasklet_schedule(&resend_tasklet);
  72. return 0;
  73. }
  74. void clear_irq_resend(struct irq_desc *desc)
  75. {
  76. raw_spin_lock(&irq_resend_lock);
  77. hlist_del_init(&desc->resend_node);
  78. raw_spin_unlock(&irq_resend_lock);
  79. }
  80. void irq_resend_init(struct irq_desc *desc)
  81. {
  82. INIT_HLIST_NODE(&desc->resend_node);
  83. }
  84. #else
  85. void clear_irq_resend(struct irq_desc *desc) {}
  86. void irq_resend_init(struct irq_desc *desc) {}
  87. static int irq_sw_resend(struct irq_desc *desc)
  88. {
  89. return -EINVAL;
  90. }
  91. #endif
  92. static int try_retrigger(struct irq_desc *desc)
  93. {
  94. if (desc->irq_data.chip->irq_retrigger)
  95. return desc->irq_data.chip->irq_retrigger(&desc->irq_data);
  96. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  97. return irq_chip_retrigger_hierarchy(&desc->irq_data);
  98. #else
  99. return 0;
  100. #endif
  101. }
  102. /*
  103. * IRQ resend
  104. *
  105. * Is called with interrupts disabled and desc->lock held.
  106. */
  107. int check_irq_resend(struct irq_desc *desc, bool inject)
  108. {
  109. int err = 0;
  110. /*
  111. * We do not resend level type interrupts. Level type interrupts
  112. * are resent by hardware when they are still active. Clear the
  113. * pending bit so suspend/resume does not get confused.
  114. */
  115. if (irq_settings_is_level(desc)) {
  116. desc->istate &= ~IRQS_PENDING;
  117. return -EINVAL;
  118. }
  119. if (desc->istate & IRQS_REPLAY)
  120. return -EBUSY;
  121. if (!(desc->istate & IRQS_PENDING) && !inject)
  122. return 0;
  123. desc->istate &= ~IRQS_PENDING;
  124. if (!try_retrigger(desc))
  125. err = irq_sw_resend(desc);
  126. /* If the retrigger was successful, mark it with the REPLAY bit */
  127. if (!err)
  128. desc->istate |= IRQS_REPLAY;
  129. return err;
  130. }
  131. #ifdef CONFIG_GENERIC_IRQ_INJECTION
  132. /**
  133. * irq_inject_interrupt - Inject an interrupt for testing/error injection
  134. * @irq: The interrupt number
  135. *
  136. * This function must only be used for debug and testing purposes!
  137. *
  138. * Especially on x86 this can cause a premature completion of an interrupt
  139. * affinity change causing the interrupt line to become stale. Very
  140. * unlikely, but possible.
  141. *
  142. * The injection can fail for various reasons:
  143. * - Interrupt is not activated
  144. * - Interrupt is NMI type or currently replaying
  145. * - Interrupt is level type
  146. * - Interrupt does not support hardware retrigger and software resend is
  147. * either not enabled or not possible for the interrupt.
  148. */
  149. int irq_inject_interrupt(unsigned int irq)
  150. {
  151. struct irq_desc *desc;
  152. unsigned long flags;
  153. int err;
  154. /* Try the state injection hardware interface first */
  155. if (!irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true))
  156. return 0;
  157. /* That failed, try via the resend mechanism */
  158. desc = irq_get_desc_buslock(irq, &flags, 0);
  159. if (!desc)
  160. return -EINVAL;
  161. /*
  162. * Only try to inject when the interrupt is:
  163. * - not NMI type
  164. * - activated
  165. */
  166. if (irq_is_nmi(desc) || !irqd_is_activated(&desc->irq_data))
  167. err = -EINVAL;
  168. else
  169. err = check_irq_resend(desc, true);
  170. irq_put_desc_busunlock(desc, flags);
  171. return err;
  172. }
  173. EXPORT_SYMBOL_GPL(irq_inject_interrupt);
  174. #endif