blob: 0c46e9fe3a89de94dd32366ae93215f755d426b9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Gleixnera4633adc2006-06-29 02:24:48 -07002/*
Thomas Gleixnera4633adc2006-06-29 02:24:48 -07003 * 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
15#include <linux/irq.h>
16#include <linux/module.h>
17#include <linux/random.h>
18#include <linux/interrupt.h>
19
20#include "internals.h"
21
22#ifdef CONFIG_HARDIRQS_SW_RESEND
23
24/* Bitmap to handle software resend of interrupts: */
Thomas Gleixnerc1ee6262011-02-17 17:45:15 +010025static DECLARE_BITMAP(irqs_resend, IRQ_BITMAP_BITS);
Thomas Gleixnera4633adc2006-06-29 02:24:48 -070026
27/*
28 * Run software resends of IRQ's
29 */
Emil Renner Berthingc2609542021-01-23 19:24:56 +010030static void resend_irqs(struct tasklet_struct *unused)
Thomas Gleixnera4633adc2006-06-29 02:24:48 -070031{
32 struct irq_desc *desc;
33 int irq;
34
Yinghai Lu85c0f902008-08-19 20:49:47 -070035 while (!bitmap_empty(irqs_resend, nr_irqs)) {
36 irq = find_first_bit(irqs_resend, nr_irqs);
Thomas Gleixnera4633adc2006-06-29 02:24:48 -070037 clear_bit(irq, irqs_resend);
Yinghai Lu08678b02008-08-19 20:50:05 -070038 desc = irq_to_desc(irq);
Yunfeng Yeeddf3e92019-09-04 20:46:25 +080039 if (!desc)
40 continue;
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070041 local_irq_disable();
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +020042 desc->handle_irq(desc);
Thomas Gleixner6a6de9e2006-06-29 02:24:51 -070043 local_irq_enable();
Thomas Gleixnera4633adc2006-06-29 02:24:48 -070044 }
45}
46
47/* Tasklet to handle resend: */
Emil Renner Berthingc2609542021-01-23 19:24:56 +010048static DECLARE_TASKLET(resend_tasklet, resend_irqs);
Thomas Gleixnera4633adc2006-06-29 02:24:48 -070049
Thomas Gleixner1f85b1f2020-03-06 14:03:45 +010050static int irq_sw_resend(struct irq_desc *desc)
51{
52 unsigned int irq = irq_desc_get_irq(desc);
53
54 /*
55 * Validate whether this interrupt can be safely injected from
56 * non interrupt context
57 */
58 if (handle_enforce_irqctx(&desc->irq_data))
59 return -EINVAL;
60
61 /*
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
64 * directly.
65 */
66 if (irq_settings_is_nested_thread(desc)) {
67 /*
68 * If the parent_irq is valid, we retrigger the parent,
69 * otherwise we do nothing.
70 */
71 if (!desc->parent_irq)
72 return -EINVAL;
73 irq = desc->parent_irq;
74 }
75
76 /* Set it pending and activate the softirq: */
77 set_bit(irq, irqs_resend);
78 tasklet_schedule(&resend_tasklet);
79 return 0;
80}
81
82#else
83static int irq_sw_resend(struct irq_desc *desc)
84{
85 return -EINVAL;
86}
Thomas Gleixnera4633adc2006-06-29 02:24:48 -070087#endif
88
Marc Zyngiercd1752d2020-08-26 18:37:50 +010089static int try_retrigger(struct irq_desc *desc)
90{
91 if (desc->irq_data.chip->irq_retrigger)
92 return desc->irq_data.chip->irq_retrigger(&desc->irq_data);
93
94#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
95 return irq_chip_retrigger_hierarchy(&desc->irq_data);
96#else
97 return 0;
98#endif
99}
100
Thomas Gleixnera4633adc2006-06-29 02:24:48 -0700101/*
102 * IRQ resend
103 *
104 * Is called with interrupts disabled and desc->lock held.
105 */
Thomas Gleixneracd26bc2020-03-06 14:03:47 +0100106int check_irq_resend(struct irq_desc *desc, bool inject)
Thomas Gleixnera4633adc2006-06-29 02:24:48 -0700107{
Thomas Gleixnerda909212020-03-06 14:03:46 +0100108 int err = 0;
109
Thomas Gleixnera4633adc2006-06-29 02:24:48 -0700110 /*
Thomas Gleixner1f85b1f2020-03-06 14:03:45 +0100111 * 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.
Thomas Gleixner24642862007-08-12 15:46:35 +0000114 */
Thomas Gleixnerd4dc0f92012-04-25 12:54:54 +0200115 if (irq_settings_is_level(desc)) {
116 desc->istate &= ~IRQS_PENDING;
Thomas Gleixner1f85b1f2020-03-06 14:03:45 +0100117 return -EINVAL;
Thomas Gleixnerd4dc0f92012-04-25 12:54:54 +0200118 }
Thomas Gleixner1f85b1f2020-03-06 14:03:45 +0100119
Thomas Gleixner163ef302011-02-08 11:39:15 +0100120 if (desc->istate & IRQS_REPLAY)
Thomas Gleixner1f85b1f2020-03-06 14:03:45 +0100121 return -EBUSY;
122
Thomas Gleixneracd26bc2020-03-06 14:03:47 +0100123 if (!(desc->istate & IRQS_PENDING) && !inject)
Thomas Gleixnerda909212020-03-06 14:03:46 +0100124 return 0;
Thomas Gleixnera4633adc2006-06-29 02:24:48 -0700125
Thomas Gleixnerda909212020-03-06 14:03:46 +0100126 desc->istate &= ~IRQS_PENDING;
127
Marc Zyngiercd1752d2020-08-26 18:37:50 +0100128 if (!try_retrigger(desc))
Thomas Gleixnerda909212020-03-06 14:03:46 +0100129 err = irq_sw_resend(desc);
130
Krzysztof Kozlowski5c982c52021-03-16 11:02:05 +0100131 /* If the retrigger was successful, mark it with the REPLAY bit */
Thomas Gleixnerda909212020-03-06 14:03:46 +0100132 if (!err)
133 desc->istate |= IRQS_REPLAY;
134 return err;
Thomas Gleixnera4633adc2006-06-29 02:24:48 -0700135}
Thomas Gleixneracd26bc2020-03-06 14:03:47 +0100136
137#ifdef CONFIG_GENERIC_IRQ_INJECTION
138/**
139 * irq_inject_interrupt - Inject an interrupt for testing/error injection
140 * @irq: The interrupt number
141 *
142 * This function must only be used for debug and testing purposes!
143 *
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.
147 *
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.
154 */
155int irq_inject_interrupt(unsigned int irq)
156{
157 struct irq_desc *desc;
158 unsigned long flags;
159 int err;
160
161 /* Try the state injection hardware interface first */
162 if (!irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true))
163 return 0;
164
165 /* That failed, try via the resend mechanism */
166 desc = irq_get_desc_buslock(irq, &flags, 0);
167 if (!desc)
168 return -EINVAL;
169
170 /*
171 * Only try to inject when the interrupt is:
172 * - not NMI type
173 * - activated
174 */
175 if ((desc->istate & IRQS_NMI) || !irqd_is_activated(&desc->irq_data))
176 err = -EINVAL;
177 else
178 err = check_irq_resend(desc, true);
179
180 irq_put_desc_busunlock(desc, flags);
181 return err;
182}
183EXPORT_SYMBOL_GPL(irq_inject_interrupt);
184#endif