Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
0004  * Copyright (C) 2005-2006, Thomas Gleixner
0005  *
0006  * This file contains the IRQ-resend code
0007  *
0008  * If the interrupt is waiting to be processed, we try to re-run it.
0009  * We can't directly run it from here since the caller might be in an
0010  * interrupt-protected region. Not all irq controller chips can
0011  * retrigger interrupts at the hardware level, so in those cases
0012  * we allow the resending of IRQs via a tasklet.
0013  */
0014 
0015 #include <linux/irq.h>
0016 #include <linux/module.h>
0017 #include <linux/random.h>
0018 #include <linux/interrupt.h>
0019 
0020 #include "internals.h"
0021 
0022 #ifdef CONFIG_HARDIRQS_SW_RESEND
0023 
0024 /* Bitmap to handle software resend of interrupts: */
0025 static DECLARE_BITMAP(irqs_resend, IRQ_BITMAP_BITS);
0026 
0027 /*
0028  * Run software resends of IRQ's
0029  */
0030 static void resend_irqs(struct tasklet_struct *unused)
0031 {
0032     struct irq_desc *desc;
0033     int irq;
0034 
0035     while (!bitmap_empty(irqs_resend, nr_irqs)) {
0036         irq = find_first_bit(irqs_resend, nr_irqs);
0037         clear_bit(irq, irqs_resend);
0038         desc = irq_to_desc(irq);
0039         if (!desc)
0040             continue;
0041         local_irq_disable();
0042         desc->handle_irq(desc);
0043         local_irq_enable();
0044     }
0045 }
0046 
0047 /* Tasklet to handle resend: */
0048 static DECLARE_TASKLET(resend_tasklet, resend_irqs);
0049 
0050 static int irq_sw_resend(struct irq_desc *desc)
0051 {
0052     unsigned int irq = irq_desc_get_irq(desc);
0053 
0054     /*
0055      * Validate whether this interrupt can be safely injected from
0056      * non interrupt context
0057      */
0058     if (handle_enforce_irqctx(&desc->irq_data))
0059         return -EINVAL;
0060 
0061     /*
0062      * If the interrupt is running in the thread context of the parent
0063      * irq we need to be careful, because we cannot trigger it
0064      * directly.
0065      */
0066     if (irq_settings_is_nested_thread(desc)) {
0067         /*
0068          * If the parent_irq is valid, we retrigger the parent,
0069          * otherwise we do nothing.
0070          */
0071         if (!desc->parent_irq)
0072             return -EINVAL;
0073         irq = desc->parent_irq;
0074     }
0075 
0076     /* Set it pending and activate the softirq: */
0077     set_bit(irq, irqs_resend);
0078     tasklet_schedule(&resend_tasklet);
0079     return 0;
0080 }
0081 
0082 #else
0083 static int irq_sw_resend(struct irq_desc *desc)
0084 {
0085     return -EINVAL;
0086 }
0087 #endif
0088 
0089 static int try_retrigger(struct irq_desc *desc)
0090 {
0091     if (desc->irq_data.chip->irq_retrigger)
0092         return desc->irq_data.chip->irq_retrigger(&desc->irq_data);
0093 
0094 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
0095     return irq_chip_retrigger_hierarchy(&desc->irq_data);
0096 #else
0097     return 0;
0098 #endif
0099 }
0100 
0101 /*
0102  * IRQ resend
0103  *
0104  * Is called with interrupts disabled and desc->lock held.
0105  */
0106 int check_irq_resend(struct irq_desc *desc, bool inject)
0107 {
0108     int err = 0;
0109 
0110     /*
0111      * We do not resend level type interrupts. Level type interrupts
0112      * are resent by hardware when they are still active. Clear the
0113      * pending bit so suspend/resume does not get confused.
0114      */
0115     if (irq_settings_is_level(desc)) {
0116         desc->istate &= ~IRQS_PENDING;
0117         return -EINVAL;
0118     }
0119 
0120     if (desc->istate & IRQS_REPLAY)
0121         return -EBUSY;
0122 
0123     if (!(desc->istate & IRQS_PENDING) && !inject)
0124         return 0;
0125 
0126     desc->istate &= ~IRQS_PENDING;
0127 
0128     if (!try_retrigger(desc))
0129         err = irq_sw_resend(desc);
0130 
0131     /* If the retrigger was successful, mark it with the REPLAY bit */
0132     if (!err)
0133         desc->istate |= IRQS_REPLAY;
0134     return err;
0135 }
0136 
0137 #ifdef CONFIG_GENERIC_IRQ_INJECTION
0138 /**
0139  * irq_inject_interrupt - Inject an interrupt for testing/error injection
0140  * @irq:    The interrupt number
0141  *
0142  * This function must only be used for debug and testing purposes!
0143  *
0144  * Especially on x86 this can cause a premature completion of an interrupt
0145  * affinity change causing the interrupt line to become stale. Very
0146  * unlikely, but possible.
0147  *
0148  * The injection can fail for various reasons:
0149  * - Interrupt is not activated
0150  * - Interrupt is NMI type or currently replaying
0151  * - Interrupt is level type
0152  * - Interrupt does not support hardware retrigger and software resend is
0153  *   either not enabled or not possible for the interrupt.
0154  */
0155 int irq_inject_interrupt(unsigned int irq)
0156 {
0157     struct irq_desc *desc;
0158     unsigned long flags;
0159     int err;
0160 
0161     /* Try the state injection hardware interface first */
0162     if (!irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true))
0163         return 0;
0164 
0165     /* That failed, try via the resend mechanism */
0166     desc = irq_get_desc_buslock(irq, &flags, 0);
0167     if (!desc)
0168         return -EINVAL;
0169 
0170     /*
0171      * Only try to inject when the interrupt is:
0172      *  - not NMI type
0173      *  - activated
0174      */
0175     if ((desc->istate & IRQS_NMI) || !irqd_is_activated(&desc->irq_data))
0176         err = -EINVAL;
0177     else
0178         err = check_irq_resend(desc, true);
0179 
0180     irq_put_desc_busunlock(desc, flags);
0181     return err;
0182 }
0183 EXPORT_SYMBOL_GPL(irq_inject_interrupt);
0184 #endif