LCOV - code coverage report
Current view: top level - kernel/irq - resend.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 6 18 33.3 %
Date: 2023-08-24 13:40:31 Functions: 2 4 50.0 %

          Line data    Source code
       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             : 
      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             : /* hlist_head to handle software resend of interrupts: */
      25             : static HLIST_HEAD(irq_resend_list);
      26             : static DEFINE_RAW_SPINLOCK(irq_resend_lock);
      27             : 
      28             : /*
      29             :  * Run software resends of IRQ's
      30             :  */
      31             : static void resend_irqs(struct tasklet_struct *unused)
      32             : {
      33             :         struct irq_desc *desc;
      34             : 
      35             :         raw_spin_lock_irq(&irq_resend_lock);
      36             :         while (!hlist_empty(&irq_resend_list)) {
      37             :                 desc = hlist_entry(irq_resend_list.first, struct irq_desc,
      38             :                                    resend_node);
      39             :                 hlist_del_init(&desc->resend_node);
      40             :                 raw_spin_unlock(&irq_resend_lock);
      41             :                 desc->handle_irq(desc);
      42             :                 raw_spin_lock(&irq_resend_lock);
      43             :         }
      44             :         raw_spin_unlock_irq(&irq_resend_lock);
      45             : }
      46             : 
      47             : /* Tasklet to handle resend: */
      48             : static DECLARE_TASKLET(resend_tasklet, resend_irqs);
      49             : 
      50             : static int irq_sw_resend(struct irq_desc *desc)
      51             : {
      52             :         /*
      53             :          * Validate whether this interrupt can be safely injected from
      54             :          * non interrupt context
      55             :          */
      56             :         if (handle_enforce_irqctx(&desc->irq_data))
      57             :                 return -EINVAL;
      58             : 
      59             :         /*
      60             :          * If the interrupt is running in the thread context of the parent
      61             :          * irq we need to be careful, because we cannot trigger it
      62             :          * directly.
      63             :          */
      64             :         if (irq_settings_is_nested_thread(desc)) {
      65             :                 /*
      66             :                  * If the parent_irq is valid, we retrigger the parent,
      67             :                  * otherwise we do nothing.
      68             :                  */
      69             :                 if (!desc->parent_irq)
      70             :                         return -EINVAL;
      71             :         }
      72             : 
      73             :         /* Add to resend_list and activate the softirq: */
      74             :         raw_spin_lock(&irq_resend_lock);
      75             :         hlist_add_head(&desc->resend_node, &irq_resend_list);
      76             :         raw_spin_unlock(&irq_resend_lock);
      77             :         tasklet_schedule(&resend_tasklet);
      78             :         return 0;
      79             : }
      80             : 
      81             : void clear_irq_resend(struct irq_desc *desc)
      82             : {
      83             :         raw_spin_lock(&irq_resend_lock);
      84             :         hlist_del_init(&desc->resend_node);
      85             :         raw_spin_unlock(&irq_resend_lock);
      86             : }
      87             : 
      88             : void irq_resend_init(struct irq_desc *desc)
      89             : {
      90             :         INIT_HLIST_NODE(&desc->resend_node);
      91             : }
      92             : #else
      93           0 : void clear_irq_resend(struct irq_desc *desc) {}
      94         128 : void irq_resend_init(struct irq_desc *desc) {}
      95             : 
      96             : static int irq_sw_resend(struct irq_desc *desc)
      97             : {
      98             :         return -EINVAL;
      99             : }
     100             : #endif
     101             : 
     102           0 : static int try_retrigger(struct irq_desc *desc)
     103             : {
     104           0 :         if (desc->irq_data.chip->irq_retrigger)
     105           0 :                 return desc->irq_data.chip->irq_retrigger(&desc->irq_data);
     106             : 
     107             : #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
     108           0 :         return irq_chip_retrigger_hierarchy(&desc->irq_data);
     109             : #else
     110             :         return 0;
     111             : #endif
     112             : }
     113             : 
     114             : /*
     115             :  * IRQ resend
     116             :  *
     117             :  * Is called with interrupts disabled and desc->lock held.
     118             :  */
     119           2 : int check_irq_resend(struct irq_desc *desc, bool inject)
     120             : {
     121           2 :         int err = 0;
     122             : 
     123             :         /*
     124             :          * We do not resend level type interrupts. Level type interrupts
     125             :          * are resent by hardware when they are still active. Clear the
     126             :          * pending bit so suspend/resume does not get confused.
     127             :          */
     128           4 :         if (irq_settings_is_level(desc)) {
     129           0 :                 desc->istate &= ~IRQS_PENDING;
     130           0 :                 return -EINVAL;
     131             :         }
     132             : 
     133           2 :         if (desc->istate & IRQS_REPLAY)
     134             :                 return -EBUSY;
     135             : 
     136           2 :         if (!(desc->istate & IRQS_PENDING) && !inject)
     137             :                 return 0;
     138             : 
     139           0 :         desc->istate &= ~IRQS_PENDING;
     140             : 
     141           0 :         if (!try_retrigger(desc))
     142           0 :                 err = irq_sw_resend(desc);
     143             : 
     144             :         /* If the retrigger was successful, mark it with the REPLAY bit */
     145           0 :         if (!err)
     146           0 :                 desc->istate |= IRQS_REPLAY;
     147             :         return err;
     148             : }
     149             : 
     150             : #ifdef CONFIG_GENERIC_IRQ_INJECTION
     151             : /**
     152             :  * irq_inject_interrupt - Inject an interrupt for testing/error injection
     153             :  * @irq:        The interrupt number
     154             :  *
     155             :  * This function must only be used for debug and testing purposes!
     156             :  *
     157             :  * Especially on x86 this can cause a premature completion of an interrupt
     158             :  * affinity change causing the interrupt line to become stale. Very
     159             :  * unlikely, but possible.
     160             :  *
     161             :  * The injection can fail for various reasons:
     162             :  * - Interrupt is not activated
     163             :  * - Interrupt is NMI type or currently replaying
     164             :  * - Interrupt is level type
     165             :  * - Interrupt does not support hardware retrigger and software resend is
     166             :  *   either not enabled or not possible for the interrupt.
     167             :  */
     168             : int irq_inject_interrupt(unsigned int irq)
     169             : {
     170             :         struct irq_desc *desc;
     171             :         unsigned long flags;
     172             :         int err;
     173             : 
     174             :         /* Try the state injection hardware interface first */
     175             :         if (!irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true))
     176             :                 return 0;
     177             : 
     178             :         /* That failed, try via the resend mechanism */
     179             :         desc = irq_get_desc_buslock(irq, &flags, 0);
     180             :         if (!desc)
     181             :                 return -EINVAL;
     182             : 
     183             :         /*
     184             :          * Only try to inject when the interrupt is:
     185             :          *  - not NMI type
     186             :          *  - activated
     187             :          */
     188             :         if ((desc->istate & IRQS_NMI) || !irqd_is_activated(&desc->irq_data))
     189             :                 err = -EINVAL;
     190             :         else
     191             :                 err = check_irq_resend(desc, true);
     192             : 
     193             :         irq_put_desc_busunlock(desc, flags);
     194             :         return err;
     195             : }
     196             : EXPORT_SYMBOL_GPL(irq_inject_interrupt);
     197             : #endif

Generated by: LCOV version 1.14