1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
4 * Copyright (C) 2005-2006, Thomas Gleixner
6 * This file contains the IRQ-resend code
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.
15 #include <linux/irq.h>
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/interrupt.h>
20 #include "internals.h"
22 #ifdef CONFIG_HARDIRQS_SW_RESEND
24 /* Bitmap to handle software resend of interrupts: */
25 static DECLARE_BITMAP(irqs_resend, IRQ_BITMAP_BITS);
28 * Run software resends of IRQ's
30 static void resend_irqs(struct tasklet_struct *unused)
32 struct irq_desc *desc;
35 while (!bitmap_empty(irqs_resend, nr_irqs)) {
36 irq = find_first_bit(irqs_resend, nr_irqs);
37 clear_bit(irq, irqs_resend);
38 desc = irq_to_desc(irq);
42 desc->handle_irq(desc);
47 /* Tasklet to handle resend: */
48 static DECLARE_TASKLET(resend_tasklet, resend_irqs);
50 static int irq_sw_resend(struct irq_desc *desc)
52 unsigned int irq = irq_desc_get_irq(desc);
55 * Validate whether this interrupt can be safely injected from
56 * non interrupt context
58 if (handle_enforce_irqctx(&desc->irq_data))
62 * If the interrupt is running in the thread context of the parent
63 * irq we need to be careful, because we cannot trigger it
66 if (irq_settings_is_nested_thread(desc)) {
68 * If the parent_irq is valid, we retrigger the parent,
69 * otherwise we do nothing.
71 if (!desc->parent_irq)
73 irq = desc->parent_irq;
76 /* Set it pending and activate the softirq: */
77 set_bit(irq, irqs_resend);
78 tasklet_schedule(&resend_tasklet);
83 static int irq_sw_resend(struct irq_desc *desc)
89 static int try_retrigger(struct irq_desc *desc)
91 if (desc->irq_data.chip->irq_retrigger)
92 return desc->irq_data.chip->irq_retrigger(&desc->irq_data);
94 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
95 return irq_chip_retrigger_hierarchy(&desc->irq_data);
104 * Is called with interrupts disabled and desc->lock held.
106 int check_irq_resend(struct irq_desc *desc, bool inject)
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.
115 if (irq_settings_is_level(desc)) {
116 desc->istate &= ~IRQS_PENDING;
120 if (desc->istate & IRQS_REPLAY)
123 if (!(desc->istate & IRQS_PENDING) && !inject)
126 desc->istate &= ~IRQS_PENDING;
128 if (!try_retrigger(desc))
129 err = irq_sw_resend(desc);
131 /* If the retrigger was successful, mark it with the REPLAY bit */
133 desc->istate |= IRQS_REPLAY;
137 #ifdef CONFIG_GENERIC_IRQ_INJECTION
139 * irq_inject_interrupt - Inject an interrupt for testing/error injection
140 * @irq: The interrupt number
142 * This function must only be used for debug and testing purposes!
144 * Especially on x86 this can cause a premature completion of an interrupt
145 * affinity change causing the interrupt line to become stale. Very
146 * unlikely, but possible.
148 * The injection can fail for various reasons:
149 * - Interrupt is not activated
150 * - Interrupt is NMI type or currently replaying
151 * - Interrupt is level type
152 * - Interrupt does not support hardware retrigger and software resend is
153 * either not enabled or not possible for the interrupt.
155 int irq_inject_interrupt(unsigned int irq)
157 struct irq_desc *desc;
161 /* Try the state injection hardware interface first */
162 if (!irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true))
165 /* That failed, try via the resend mechanism */
166 desc = irq_get_desc_buslock(irq, &flags, 0);
171 * Only try to inject when the interrupt is:
175 if ((desc->istate & IRQS_NMI) || !irqd_is_activated(&desc->irq_data))
178 err = check_irq_resend(desc, true);
180 irq_put_desc_busunlock(desc, flags);
183 EXPORT_SYMBOL_GPL(irq_inject_interrupt);