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