resend.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /* Bitmap to handle software resend of interrupts: */
  21. static DECLARE_BITMAP(irqs_resend, IRQ_BITMAP_BITS);
  22. /*
  23. * Run software resends of IRQ's
  24. */
  25. static void resend_irqs(struct tasklet_struct *unused)
  26. {
  27. struct irq_desc *desc;
  28. int irq;
  29. while (!bitmap_empty(irqs_resend, nr_irqs)) {
  30. irq = find_first_bit(irqs_resend, nr_irqs);
  31. clear_bit(irq, irqs_resend);
  32. desc = irq_to_desc(irq);
  33. if (!desc)
  34. continue;
  35. local_irq_disable();
  36. desc->handle_irq(desc);
  37. local_irq_enable();
  38. }
  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. unsigned int irq = irq_desc_get_irq(desc);
  45. /*
  46. * Validate whether this interrupt can be safely injected from
  47. * non interrupt context
  48. */
  49. if (handle_enforce_irqctx(&desc->irq_data))
  50. return -EINVAL;
  51. /*
  52. * If the interrupt is running in the thread context of the parent
  53. * irq we need to be careful, because we cannot trigger it
  54. * directly.
  55. */
  56. if (irq_settings_is_nested_thread(desc)) {
  57. /*
  58. * If the parent_irq is valid, we retrigger the parent,
  59. * otherwise we do nothing.
  60. */
  61. if (!desc->parent_irq)
  62. return -EINVAL;
  63. irq = desc->parent_irq;
  64. }
  65. /* Set it pending and activate the softirq: */
  66. set_bit(irq, irqs_resend);
  67. tasklet_schedule(&resend_tasklet);
  68. return 0;
  69. }
  70. #else
  71. static int irq_sw_resend(struct irq_desc *desc)
  72. {
  73. return -EINVAL;
  74. }
  75. #endif
  76. static int try_retrigger(struct irq_desc *desc)
  77. {
  78. if (desc->irq_data.chip->irq_retrigger)
  79. return desc->irq_data.chip->irq_retrigger(&desc->irq_data);
  80. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  81. return irq_chip_retrigger_hierarchy(&desc->irq_data);
  82. #else
  83. return 0;
  84. #endif
  85. }
  86. /*
  87. * IRQ resend
  88. *
  89. * Is called with interrupts disabled and desc->lock held.
  90. */
  91. int check_irq_resend(struct irq_desc *desc, bool inject)
  92. {
  93. int err = 0;
  94. /*
  95. * We do not resend level type interrupts. Level type interrupts
  96. * are resent by hardware when they are still active. Clear the
  97. * pending bit so suspend/resume does not get confused.
  98. */
  99. if (irq_settings_is_level(desc)) {
  100. desc->istate &= ~IRQS_PENDING;
  101. return -EINVAL;
  102. }
  103. if (desc->istate & IRQS_REPLAY)
  104. return -EBUSY;
  105. if (!(desc->istate & IRQS_PENDING) && !inject)
  106. return 0;
  107. desc->istate &= ~IRQS_PENDING;
  108. if (!try_retrigger(desc))
  109. err = irq_sw_resend(desc);
  110. /* If the retrigger was successful, mark it with the REPLAY bit */
  111. if (!err)
  112. desc->istate |= IRQS_REPLAY;
  113. return err;
  114. }
  115. #ifdef CONFIG_GENERIC_IRQ_INJECTION
  116. /**
  117. * irq_inject_interrupt - Inject an interrupt for testing/error injection
  118. * @irq: The interrupt number
  119. *
  120. * This function must only be used for debug and testing purposes!
  121. *
  122. * Especially on x86 this can cause a premature completion of an interrupt
  123. * affinity change causing the interrupt line to become stale. Very
  124. * unlikely, but possible.
  125. *
  126. * The injection can fail for various reasons:
  127. * - Interrupt is not activated
  128. * - Interrupt is NMI type or currently replaying
  129. * - Interrupt is level type
  130. * - Interrupt does not support hardware retrigger and software resend is
  131. * either not enabled or not possible for the interrupt.
  132. */
  133. int irq_inject_interrupt(unsigned int irq)
  134. {
  135. struct irq_desc *desc;
  136. unsigned long flags;
  137. int err;
  138. /* Try the state injection hardware interface first */
  139. if (!irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true))
  140. return 0;
  141. /* That failed, try via the resend mechanism */
  142. desc = irq_get_desc_buslock(irq, &flags, 0);
  143. if (!desc)
  144. return -EINVAL;
  145. /*
  146. * Only try to inject when the interrupt is:
  147. * - not NMI type
  148. * - activated
  149. */
  150. if ((desc->istate & IRQS_NMI) || !irqd_is_activated(&desc->irq_data))
  151. err = -EINVAL;
  152. else
  153. err = check_irq_resend(desc, true);
  154. irq_put_desc_busunlock(desc, flags);
  155. return err;
  156. }
  157. EXPORT_SYMBOL_GPL(irq_inject_interrupt);
  158. #endif