LCOV - code coverage report
Current view: top level - kernel/irq - manage.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 79 706 11.2 %
Date: 2022-12-09 01:23:36 Functions: 3 57 5.3 %

          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 driver APIs to the irq subsystem.
       7             :  */
       8             : 
       9             : #define pr_fmt(fmt) "genirq: " fmt
      10             : 
      11             : #include <linux/irq.h>
      12             : #include <linux/kthread.h>
      13             : #include <linux/module.h>
      14             : #include <linux/random.h>
      15             : #include <linux/interrupt.h>
      16             : #include <linux/irqdomain.h>
      17             : #include <linux/slab.h>
      18             : #include <linux/sched.h>
      19             : #include <linux/sched/rt.h>
      20             : #include <linux/sched/task.h>
      21             : #include <linux/sched/isolation.h>
      22             : #include <uapi/linux/sched/types.h>
      23             : #include <linux/task_work.h>
      24             : 
      25             : #include "internals.h"
      26             : 
      27             : #if defined(CONFIG_IRQ_FORCED_THREADING) && !defined(CONFIG_PREEMPT_RT)
      28             : DEFINE_STATIC_KEY_FALSE(force_irqthreads_key);
      29             : 
      30             : static int __init setup_forced_irqthreads(char *arg)
      31             : {
      32             :         static_branch_enable(&force_irqthreads_key);
      33             :         return 0;
      34             : }
      35             : early_param("threadirqs", setup_forced_irqthreads);
      36             : #endif
      37             : 
      38           0 : static void __synchronize_hardirq(struct irq_desc *desc, bool sync_chip)
      39             : {
      40           0 :         struct irq_data *irqd = irq_desc_get_irq_data(desc);
      41             :         bool inprogress;
      42             : 
      43             :         do {
      44             :                 unsigned long flags;
      45             : 
      46             :                 /*
      47             :                  * Wait until we're out of the critical section.  This might
      48             :                  * give the wrong answer due to the lack of memory barriers.
      49             :                  */
      50           0 :                 while (irqd_irq_inprogress(&desc->irq_data))
      51             :                         cpu_relax();
      52             : 
      53             :                 /* Ok, that indicated we're done: double-check carefully. */
      54           0 :                 raw_spin_lock_irqsave(&desc->lock, flags);
      55           0 :                 inprogress = irqd_irq_inprogress(&desc->irq_data);
      56             : 
      57             :                 /*
      58             :                  * If requested and supported, check at the chip whether it
      59             :                  * is in flight at the hardware level, i.e. already pending
      60             :                  * in a CPU and waiting for service and acknowledge.
      61             :                  */
      62           0 :                 if (!inprogress && sync_chip) {
      63             :                         /*
      64             :                          * Ignore the return code. inprogress is only updated
      65             :                          * when the chip supports it.
      66             :                          */
      67           0 :                         __irq_get_irqchip_state(irqd, IRQCHIP_STATE_ACTIVE,
      68             :                                                 &inprogress);
      69             :                 }
      70           0 :                 raw_spin_unlock_irqrestore(&desc->lock, flags);
      71             : 
      72             :                 /* Oops, that failed? */
      73           0 :         } while (inprogress);
      74           0 : }
      75             : 
      76             : /**
      77             :  *      synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs)
      78             :  *      @irq: interrupt number to wait for
      79             :  *
      80             :  *      This function waits for any pending hard IRQ handlers for this
      81             :  *      interrupt to complete before returning. If you use this
      82             :  *      function while holding a resource the IRQ handler may need you
      83             :  *      will deadlock. It does not take associated threaded handlers
      84             :  *      into account.
      85             :  *
      86             :  *      Do not use this for shutdown scenarios where you must be sure
      87             :  *      that all parts (hardirq and threaded handler) have completed.
      88             :  *
      89             :  *      Returns: false if a threaded handler is active.
      90             :  *
      91             :  *      This function may be called - with care - from IRQ context.
      92             :  *
      93             :  *      It does not check whether there is an interrupt in flight at the
      94             :  *      hardware level, but not serviced yet, as this might deadlock when
      95             :  *      called with interrupts disabled and the target CPU of the interrupt
      96             :  *      is the current CPU.
      97             :  */
      98           0 : bool synchronize_hardirq(unsigned int irq)
      99             : {
     100           0 :         struct irq_desc *desc = irq_to_desc(irq);
     101             : 
     102           0 :         if (desc) {
     103           0 :                 __synchronize_hardirq(desc, false);
     104           0 :                 return !atomic_read(&desc->threads_active);
     105             :         }
     106             : 
     107             :         return true;
     108             : }
     109             : EXPORT_SYMBOL(synchronize_hardirq);
     110             : 
     111             : /**
     112             :  *      synchronize_irq - wait for pending IRQ handlers (on other CPUs)
     113             :  *      @irq: interrupt number to wait for
     114             :  *
     115             :  *      This function waits for any pending IRQ handlers for this interrupt
     116             :  *      to complete before returning. If you use this function while
     117             :  *      holding a resource the IRQ handler may need you will deadlock.
     118             :  *
     119             :  *      Can only be called from preemptible code as it might sleep when
     120             :  *      an interrupt thread is associated to @irq.
     121             :  *
     122             :  *      It optionally makes sure (when the irq chip supports that method)
     123             :  *      that the interrupt is not pending in any CPU and waiting for
     124             :  *      service.
     125             :  */
     126           0 : void synchronize_irq(unsigned int irq)
     127             : {
     128           0 :         struct irq_desc *desc = irq_to_desc(irq);
     129             : 
     130           0 :         if (desc) {
     131           0 :                 __synchronize_hardirq(desc, true);
     132             :                 /*
     133             :                  * We made sure that no hardirq handler is
     134             :                  * running. Now verify that no threaded handlers are
     135             :                  * active.
     136             :                  */
     137           0 :                 wait_event(desc->wait_for_threads,
     138             :                            !atomic_read(&desc->threads_active));
     139             :         }
     140           0 : }
     141             : EXPORT_SYMBOL(synchronize_irq);
     142             : 
     143             : #ifdef CONFIG_SMP
     144             : cpumask_var_t irq_default_affinity;
     145             : 
     146             : static bool __irq_can_set_affinity(struct irq_desc *desc)
     147             : {
     148             :         if (!desc || !irqd_can_balance(&desc->irq_data) ||
     149             :             !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
     150             :                 return false;
     151             :         return true;
     152             : }
     153             : 
     154             : /**
     155             :  *      irq_can_set_affinity - Check if the affinity of a given irq can be set
     156             :  *      @irq:           Interrupt to check
     157             :  *
     158             :  */
     159             : int irq_can_set_affinity(unsigned int irq)
     160             : {
     161             :         return __irq_can_set_affinity(irq_to_desc(irq));
     162             : }
     163             : 
     164             : /**
     165             :  * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space
     166             :  * @irq:        Interrupt to check
     167             :  *
     168             :  * Like irq_can_set_affinity() above, but additionally checks for the
     169             :  * AFFINITY_MANAGED flag.
     170             :  */
     171             : bool irq_can_set_affinity_usr(unsigned int irq)
     172             : {
     173             :         struct irq_desc *desc = irq_to_desc(irq);
     174             : 
     175             :         return __irq_can_set_affinity(desc) &&
     176             :                 !irqd_affinity_is_managed(&desc->irq_data);
     177             : }
     178             : 
     179             : /**
     180             :  *      irq_set_thread_affinity - Notify irq threads to adjust affinity
     181             :  *      @desc:          irq descriptor which has affinity changed
     182             :  *
     183             :  *      We just set IRQTF_AFFINITY and delegate the affinity setting
     184             :  *      to the interrupt thread itself. We can not call
     185             :  *      set_cpus_allowed_ptr() here as we hold desc->lock and this
     186             :  *      code can be called from hard interrupt context.
     187             :  */
     188             : void irq_set_thread_affinity(struct irq_desc *desc)
     189             : {
     190             :         struct irqaction *action;
     191             : 
     192             :         for_each_action_of_desc(desc, action)
     193             :                 if (action->thread)
     194             :                         set_bit(IRQTF_AFFINITY, &action->thread_flags);
     195             : }
     196             : 
     197             : #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
     198             : static void irq_validate_effective_affinity(struct irq_data *data)
     199             : {
     200             :         const struct cpumask *m = irq_data_get_effective_affinity_mask(data);
     201             :         struct irq_chip *chip = irq_data_get_irq_chip(data);
     202             : 
     203             :         if (!cpumask_empty(m))
     204             :                 return;
     205             :         pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n",
     206             :                      chip->name, data->irq);
     207             : }
     208             : 
     209             : static inline void irq_init_effective_affinity(struct irq_data *data,
     210             :                                                const struct cpumask *mask)
     211             : {
     212             :         cpumask_copy(irq_data_get_effective_affinity_mask(data), mask);
     213             : }
     214             : #else
     215             : static inline void irq_validate_effective_affinity(struct irq_data *data) { }
     216             : static inline void irq_init_effective_affinity(struct irq_data *data,
     217             :                                                const struct cpumask *mask) { }
     218             : #endif
     219             : 
     220             : int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
     221             :                         bool force)
     222             : {
     223             :         struct irq_desc *desc = irq_data_to_desc(data);
     224             :         struct irq_chip *chip = irq_data_get_irq_chip(data);
     225             :         int ret;
     226             : 
     227             :         if (!chip || !chip->irq_set_affinity)
     228             :                 return -EINVAL;
     229             : 
     230             :         /*
     231             :          * If this is a managed interrupt and housekeeping is enabled on
     232             :          * it check whether the requested affinity mask intersects with
     233             :          * a housekeeping CPU. If so, then remove the isolated CPUs from
     234             :          * the mask and just keep the housekeeping CPU(s). This prevents
     235             :          * the affinity setter from routing the interrupt to an isolated
     236             :          * CPU to avoid that I/O submitted from a housekeeping CPU causes
     237             :          * interrupts on an isolated one.
     238             :          *
     239             :          * If the masks do not intersect or include online CPU(s) then
     240             :          * keep the requested mask. The isolated target CPUs are only
     241             :          * receiving interrupts when the I/O operation was submitted
     242             :          * directly from them.
     243             :          *
     244             :          * If all housekeeping CPUs in the affinity mask are offline, the
     245             :          * interrupt will be migrated by the CPU hotplug code once a
     246             :          * housekeeping CPU which belongs to the affinity mask comes
     247             :          * online.
     248             :          */
     249             :         if (irqd_affinity_is_managed(data) &&
     250             :             housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) {
     251             :                 const struct cpumask *hk_mask, *prog_mask;
     252             : 
     253             :                 static DEFINE_RAW_SPINLOCK(tmp_mask_lock);
     254             :                 static struct cpumask tmp_mask;
     255             : 
     256             :                 hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
     257             : 
     258             :                 raw_spin_lock(&tmp_mask_lock);
     259             :                 cpumask_and(&tmp_mask, mask, hk_mask);
     260             :                 if (!cpumask_intersects(&tmp_mask, cpu_online_mask))
     261             :                         prog_mask = mask;
     262             :                 else
     263             :                         prog_mask = &tmp_mask;
     264             :                 ret = chip->irq_set_affinity(data, prog_mask, force);
     265             :                 raw_spin_unlock(&tmp_mask_lock);
     266             :         } else {
     267             :                 ret = chip->irq_set_affinity(data, mask, force);
     268             :         }
     269             :         switch (ret) {
     270             :         case IRQ_SET_MASK_OK:
     271             :         case IRQ_SET_MASK_OK_DONE:
     272             :                 cpumask_copy(desc->irq_common_data.affinity, mask);
     273             :                 fallthrough;
     274             :         case IRQ_SET_MASK_OK_NOCOPY:
     275             :                 irq_validate_effective_affinity(data);
     276             :                 irq_set_thread_affinity(desc);
     277             :                 ret = 0;
     278             :         }
     279             : 
     280             :         return ret;
     281             : }
     282             : 
     283             : #ifdef CONFIG_GENERIC_PENDING_IRQ
     284             : static inline int irq_set_affinity_pending(struct irq_data *data,
     285             :                                            const struct cpumask *dest)
     286             : {
     287             :         struct irq_desc *desc = irq_data_to_desc(data);
     288             : 
     289             :         irqd_set_move_pending(data);
     290             :         irq_copy_pending(desc, dest);
     291             :         return 0;
     292             : }
     293             : #else
     294             : static inline int irq_set_affinity_pending(struct irq_data *data,
     295             :                                            const struct cpumask *dest)
     296             : {
     297             :         return -EBUSY;
     298             : }
     299             : #endif
     300             : 
     301             : static int irq_try_set_affinity(struct irq_data *data,
     302             :                                 const struct cpumask *dest, bool force)
     303             : {
     304             :         int ret = irq_do_set_affinity(data, dest, force);
     305             : 
     306             :         /*
     307             :          * In case that the underlying vector management is busy and the
     308             :          * architecture supports the generic pending mechanism then utilize
     309             :          * this to avoid returning an error to user space.
     310             :          */
     311             :         if (ret == -EBUSY && !force)
     312             :                 ret = irq_set_affinity_pending(data, dest);
     313             :         return ret;
     314             : }
     315             : 
     316             : static bool irq_set_affinity_deactivated(struct irq_data *data,
     317             :                                          const struct cpumask *mask, bool force)
     318             : {
     319             :         struct irq_desc *desc = irq_data_to_desc(data);
     320             : 
     321             :         /*
     322             :          * Handle irq chips which can handle affinity only in activated
     323             :          * state correctly
     324             :          *
     325             :          * If the interrupt is not yet activated, just store the affinity
     326             :          * mask and do not call the chip driver at all. On activation the
     327             :          * driver has to make sure anyway that the interrupt is in a
     328             :          * usable state so startup works.
     329             :          */
     330             :         if (!IS_ENABLED(CONFIG_IRQ_DOMAIN_HIERARCHY) ||
     331             :             irqd_is_activated(data) || !irqd_affinity_on_activate(data))
     332             :                 return false;
     333             : 
     334             :         cpumask_copy(desc->irq_common_data.affinity, mask);
     335             :         irq_init_effective_affinity(data, mask);
     336             :         irqd_set(data, IRQD_AFFINITY_SET);
     337             :         return true;
     338             : }
     339             : 
     340             : int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
     341             :                             bool force)
     342             : {
     343             :         struct irq_chip *chip = irq_data_get_irq_chip(data);
     344             :         struct irq_desc *desc = irq_data_to_desc(data);
     345             :         int ret = 0;
     346             : 
     347             :         if (!chip || !chip->irq_set_affinity)
     348             :                 return -EINVAL;
     349             : 
     350             :         if (irq_set_affinity_deactivated(data, mask, force))
     351             :                 return 0;
     352             : 
     353             :         if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) {
     354             :                 ret = irq_try_set_affinity(data, mask, force);
     355             :         } else {
     356             :                 irqd_set_move_pending(data);
     357             :                 irq_copy_pending(desc, mask);
     358             :         }
     359             : 
     360             :         if (desc->affinity_notify) {
     361             :                 kref_get(&desc->affinity_notify->kref);
     362             :                 if (!schedule_work(&desc->affinity_notify->work)) {
     363             :                         /* Work was already scheduled, drop our extra ref */
     364             :                         kref_put(&desc->affinity_notify->kref,
     365             :                                  desc->affinity_notify->release);
     366             :                 }
     367             :         }
     368             :         irqd_set(data, IRQD_AFFINITY_SET);
     369             : 
     370             :         return ret;
     371             : }
     372             : 
     373             : /**
     374             :  * irq_update_affinity_desc - Update affinity management for an interrupt
     375             :  * @irq:        The interrupt number to update
     376             :  * @affinity:   Pointer to the affinity descriptor
     377             :  *
     378             :  * This interface can be used to configure the affinity management of
     379             :  * interrupts which have been allocated already.
     380             :  *
     381             :  * There are certain limitations on when it may be used - attempts to use it
     382             :  * for when the kernel is configured for generic IRQ reservation mode (in
     383             :  * config GENERIC_IRQ_RESERVATION_MODE) will fail, as it may conflict with
     384             :  * managed/non-managed interrupt accounting. In addition, attempts to use it on
     385             :  * an interrupt which is already started or which has already been configured
     386             :  * as managed will also fail, as these mean invalid init state or double init.
     387             :  */
     388             : int irq_update_affinity_desc(unsigned int irq,
     389             :                              struct irq_affinity_desc *affinity)
     390             : {
     391             :         struct irq_desc *desc;
     392             :         unsigned long flags;
     393             :         bool activated;
     394             :         int ret = 0;
     395             : 
     396             :         /*
     397             :          * Supporting this with the reservation scheme used by x86 needs
     398             :          * some more thought. Fail it for now.
     399             :          */
     400             :         if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
     401             :                 return -EOPNOTSUPP;
     402             : 
     403             :         desc = irq_get_desc_buslock(irq, &flags, 0);
     404             :         if (!desc)
     405             :                 return -EINVAL;
     406             : 
     407             :         /* Requires the interrupt to be shut down */
     408             :         if (irqd_is_started(&desc->irq_data)) {
     409             :                 ret = -EBUSY;
     410             :                 goto out_unlock;
     411             :         }
     412             : 
     413             :         /* Interrupts which are already managed cannot be modified */
     414             :         if (irqd_affinity_is_managed(&desc->irq_data)) {
     415             :                 ret = -EBUSY;
     416             :                 goto out_unlock;
     417             :         }
     418             : 
     419             :         /*
     420             :          * Deactivate the interrupt. That's required to undo
     421             :          * anything an earlier activation has established.
     422             :          */
     423             :         activated = irqd_is_activated(&desc->irq_data);
     424             :         if (activated)
     425             :                 irq_domain_deactivate_irq(&desc->irq_data);
     426             : 
     427             :         if (affinity->is_managed) {
     428             :                 irqd_set(&desc->irq_data, IRQD_AFFINITY_MANAGED);
     429             :                 irqd_set(&desc->irq_data, IRQD_MANAGED_SHUTDOWN);
     430             :         }
     431             : 
     432             :         cpumask_copy(desc->irq_common_data.affinity, &affinity->mask);
     433             : 
     434             :         /* Restore the activation state */
     435             :         if (activated)
     436             :                 irq_domain_activate_irq(&desc->irq_data, false);
     437             : 
     438             : out_unlock:
     439             :         irq_put_desc_busunlock(desc, flags);
     440             :         return ret;
     441             : }
     442             : 
     443             : static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask,
     444             :                               bool force)
     445             : {
     446             :         struct irq_desc *desc = irq_to_desc(irq);
     447             :         unsigned long flags;
     448             :         int ret;
     449             : 
     450             :         if (!desc)
     451             :                 return -EINVAL;
     452             : 
     453             :         raw_spin_lock_irqsave(&desc->lock, flags);
     454             :         ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
     455             :         raw_spin_unlock_irqrestore(&desc->lock, flags);
     456             :         return ret;
     457             : }
     458             : 
     459             : /**
     460             :  * irq_set_affinity - Set the irq affinity of a given irq
     461             :  * @irq:        Interrupt to set affinity
     462             :  * @cpumask:    cpumask
     463             :  *
     464             :  * Fails if cpumask does not contain an online CPU
     465             :  */
     466             : int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
     467             : {
     468             :         return __irq_set_affinity(irq, cpumask, false);
     469             : }
     470             : EXPORT_SYMBOL_GPL(irq_set_affinity);
     471             : 
     472             : /**
     473             :  * irq_force_affinity - Force the irq affinity of a given irq
     474             :  * @irq:        Interrupt to set affinity
     475             :  * @cpumask:    cpumask
     476             :  *
     477             :  * Same as irq_set_affinity, but without checking the mask against
     478             :  * online cpus.
     479             :  *
     480             :  * Solely for low level cpu hotplug code, where we need to make per
     481             :  * cpu interrupts affine before the cpu becomes online.
     482             :  */
     483             : int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
     484             : {
     485             :         return __irq_set_affinity(irq, cpumask, true);
     486             : }
     487             : EXPORT_SYMBOL_GPL(irq_force_affinity);
     488             : 
     489             : int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m,
     490             :                               bool setaffinity)
     491             : {
     492             :         unsigned long flags;
     493             :         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
     494             : 
     495             :         if (!desc)
     496             :                 return -EINVAL;
     497             :         desc->affinity_hint = m;
     498             :         irq_put_desc_unlock(desc, flags);
     499             :         if (m && setaffinity)
     500             :                 __irq_set_affinity(irq, m, false);
     501             :         return 0;
     502             : }
     503             : EXPORT_SYMBOL_GPL(__irq_apply_affinity_hint);
     504             : 
     505             : static void irq_affinity_notify(struct work_struct *work)
     506             : {
     507             :         struct irq_affinity_notify *notify =
     508             :                 container_of(work, struct irq_affinity_notify, work);
     509             :         struct irq_desc *desc = irq_to_desc(notify->irq);
     510             :         cpumask_var_t cpumask;
     511             :         unsigned long flags;
     512             : 
     513             :         if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
     514             :                 goto out;
     515             : 
     516             :         raw_spin_lock_irqsave(&desc->lock, flags);
     517             :         if (irq_move_pending(&desc->irq_data))
     518             :                 irq_get_pending(cpumask, desc);
     519             :         else
     520             :                 cpumask_copy(cpumask, desc->irq_common_data.affinity);
     521             :         raw_spin_unlock_irqrestore(&desc->lock, flags);
     522             : 
     523             :         notify->notify(notify, cpumask);
     524             : 
     525             :         free_cpumask_var(cpumask);
     526             : out:
     527             :         kref_put(&notify->kref, notify->release);
     528             : }
     529             : 
     530             : /**
     531             :  *      irq_set_affinity_notifier - control notification of IRQ affinity changes
     532             :  *      @irq:           Interrupt for which to enable/disable notification
     533             :  *      @notify:        Context for notification, or %NULL to disable
     534             :  *                      notification.  Function pointers must be initialised;
     535             :  *                      the other fields will be initialised by this function.
     536             :  *
     537             :  *      Must be called in process context.  Notification may only be enabled
     538             :  *      after the IRQ is allocated and must be disabled before the IRQ is
     539             :  *      freed using free_irq().
     540             :  */
     541             : int
     542             : irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
     543             : {
     544             :         struct irq_desc *desc = irq_to_desc(irq);
     545             :         struct irq_affinity_notify *old_notify;
     546             :         unsigned long flags;
     547             : 
     548             :         /* The release function is promised process context */
     549             :         might_sleep();
     550             : 
     551             :         if (!desc || desc->istate & IRQS_NMI)
     552             :                 return -EINVAL;
     553             : 
     554             :         /* Complete initialisation of *notify */
     555             :         if (notify) {
     556             :                 notify->irq = irq;
     557             :                 kref_init(&notify->kref);
     558             :                 INIT_WORK(&notify->work, irq_affinity_notify);
     559             :         }
     560             : 
     561             :         raw_spin_lock_irqsave(&desc->lock, flags);
     562             :         old_notify = desc->affinity_notify;
     563             :         desc->affinity_notify = notify;
     564             :         raw_spin_unlock_irqrestore(&desc->lock, flags);
     565             : 
     566             :         if (old_notify) {
     567             :                 if (cancel_work_sync(&old_notify->work)) {
     568             :                         /* Pending work had a ref, put that one too */
     569             :                         kref_put(&old_notify->kref, old_notify->release);
     570             :                 }
     571             :                 kref_put(&old_notify->kref, old_notify->release);
     572             :         }
     573             : 
     574             :         return 0;
     575             : }
     576             : EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
     577             : 
     578             : #ifndef CONFIG_AUTO_IRQ_AFFINITY
     579             : /*
     580             :  * Generic version of the affinity autoselector.
     581             :  */
     582             : int irq_setup_affinity(struct irq_desc *desc)
     583             : {
     584             :         struct cpumask *set = irq_default_affinity;
     585             :         int ret, node = irq_desc_get_node(desc);
     586             :         static DEFINE_RAW_SPINLOCK(mask_lock);
     587             :         static struct cpumask mask;
     588             : 
     589             :         /* Excludes PER_CPU and NO_BALANCE interrupts */
     590             :         if (!__irq_can_set_affinity(desc))
     591             :                 return 0;
     592             : 
     593             :         raw_spin_lock(&mask_lock);
     594             :         /*
     595             :          * Preserve the managed affinity setting and a userspace affinity
     596             :          * setup, but make sure that one of the targets is online.
     597             :          */
     598             :         if (irqd_affinity_is_managed(&desc->irq_data) ||
     599             :             irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
     600             :                 if (cpumask_intersects(desc->irq_common_data.affinity,
     601             :                                        cpu_online_mask))
     602             :                         set = desc->irq_common_data.affinity;
     603             :                 else
     604             :                         irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
     605             :         }
     606             : 
     607             :         cpumask_and(&mask, cpu_online_mask, set);
     608             :         if (cpumask_empty(&mask))
     609             :                 cpumask_copy(&mask, cpu_online_mask);
     610             : 
     611             :         if (node != NUMA_NO_NODE) {
     612             :                 const struct cpumask *nodemask = cpumask_of_node(node);
     613             : 
     614             :                 /* make sure at least one of the cpus in nodemask is online */
     615             :                 if (cpumask_intersects(&mask, nodemask))
     616             :                         cpumask_and(&mask, &mask, nodemask);
     617             :         }
     618             :         ret = irq_do_set_affinity(&desc->irq_data, &mask, false);
     619             :         raw_spin_unlock(&mask_lock);
     620             :         return ret;
     621             : }
     622             : #else
     623             : /* Wrapper for ALPHA specific affinity selector magic */
     624             : int irq_setup_affinity(struct irq_desc *desc)
     625             : {
     626             :         return irq_select_affinity(irq_desc_get_irq(desc));
     627             : }
     628             : #endif /* CONFIG_AUTO_IRQ_AFFINITY */
     629             : #endif /* CONFIG_SMP */
     630             : 
     631             : 
     632             : /**
     633             :  *      irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
     634             :  *      @irq: interrupt number to set affinity
     635             :  *      @vcpu_info: vCPU specific data or pointer to a percpu array of vCPU
     636             :  *                  specific data for percpu_devid interrupts
     637             :  *
     638             :  *      This function uses the vCPU specific data to set the vCPU
     639             :  *      affinity for an irq. The vCPU specific data is passed from
     640             :  *      outside, such as KVM. One example code path is as below:
     641             :  *      KVM -> IOMMU -> irq_set_vcpu_affinity().
     642             :  */
     643           0 : int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
     644             : {
     645             :         unsigned long flags;
     646           0 :         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
     647             :         struct irq_data *data;
     648             :         struct irq_chip *chip;
     649           0 :         int ret = -ENOSYS;
     650             : 
     651           0 :         if (!desc)
     652             :                 return -EINVAL;
     653             : 
     654           0 :         data = irq_desc_get_irq_data(desc);
     655             :         do {
     656           0 :                 chip = irq_data_get_irq_chip(data);
     657           0 :                 if (chip && chip->irq_set_vcpu_affinity)
     658             :                         break;
     659             : #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
     660           0 :                 data = data->parent_data;
     661             : #else
     662             :                 data = NULL;
     663             : #endif
     664           0 :         } while (data);
     665             : 
     666           0 :         if (data)
     667           0 :                 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
     668           0 :         irq_put_desc_unlock(desc, flags);
     669             : 
     670           0 :         return ret;
     671             : }
     672             : EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
     673             : 
     674           0 : void __disable_irq(struct irq_desc *desc)
     675             : {
     676           0 :         if (!desc->depth++)
     677           0 :                 irq_disable(desc);
     678           0 : }
     679             : 
     680           0 : static int __disable_irq_nosync(unsigned int irq)
     681             : {
     682             :         unsigned long flags;
     683           0 :         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
     684             : 
     685           0 :         if (!desc)
     686             :                 return -EINVAL;
     687           0 :         __disable_irq(desc);
     688           0 :         irq_put_desc_busunlock(desc, flags);
     689           0 :         return 0;
     690             : }
     691             : 
     692             : /**
     693             :  *      disable_irq_nosync - disable an irq without waiting
     694             :  *      @irq: Interrupt to disable
     695             :  *
     696             :  *      Disable the selected interrupt line.  Disables and Enables are
     697             :  *      nested.
     698             :  *      Unlike disable_irq(), this function does not ensure existing
     699             :  *      instances of the IRQ handler have completed before returning.
     700             :  *
     701             :  *      This function may be called from IRQ context.
     702             :  */
     703           0 : void disable_irq_nosync(unsigned int irq)
     704             : {
     705           0 :         __disable_irq_nosync(irq);
     706           0 : }
     707             : EXPORT_SYMBOL(disable_irq_nosync);
     708             : 
     709             : /**
     710             :  *      disable_irq - disable an irq and wait for completion
     711             :  *      @irq: Interrupt to disable
     712             :  *
     713             :  *      Disable the selected interrupt line.  Enables and Disables are
     714             :  *      nested.
     715             :  *      This function waits for any pending IRQ handlers for this interrupt
     716             :  *      to complete before returning. If you use this function while
     717             :  *      holding a resource the IRQ handler may need you will deadlock.
     718             :  *
     719             :  *      This function may be called - with care - from IRQ context.
     720             :  */
     721           0 : void disable_irq(unsigned int irq)
     722             : {
     723           0 :         if (!__disable_irq_nosync(irq))
     724           0 :                 synchronize_irq(irq);
     725           0 : }
     726             : EXPORT_SYMBOL(disable_irq);
     727             : 
     728             : /**
     729             :  *      disable_hardirq - disables an irq and waits for hardirq completion
     730             :  *      @irq: Interrupt to disable
     731             :  *
     732             :  *      Disable the selected interrupt line.  Enables and Disables are
     733             :  *      nested.
     734             :  *      This function waits for any pending hard IRQ handlers for this
     735             :  *      interrupt to complete before returning. If you use this function while
     736             :  *      holding a resource the hard IRQ handler may need you will deadlock.
     737             :  *
     738             :  *      When used to optimistically disable an interrupt from atomic context
     739             :  *      the return value must be checked.
     740             :  *
     741             :  *      Returns: false if a threaded handler is active.
     742             :  *
     743             :  *      This function may be called - with care - from IRQ context.
     744             :  */
     745           0 : bool disable_hardirq(unsigned int irq)
     746             : {
     747           0 :         if (!__disable_irq_nosync(irq))
     748           0 :                 return synchronize_hardirq(irq);
     749             : 
     750             :         return false;
     751             : }
     752             : EXPORT_SYMBOL_GPL(disable_hardirq);
     753             : 
     754             : /**
     755             :  *      disable_nmi_nosync - disable an nmi without waiting
     756             :  *      @irq: Interrupt to disable
     757             :  *
     758             :  *      Disable the selected interrupt line. Disables and enables are
     759             :  *      nested.
     760             :  *      The interrupt to disable must have been requested through request_nmi.
     761             :  *      Unlike disable_nmi(), this function does not ensure existing
     762             :  *      instances of the IRQ handler have completed before returning.
     763             :  */
     764           0 : void disable_nmi_nosync(unsigned int irq)
     765             : {
     766           0 :         disable_irq_nosync(irq);
     767           0 : }
     768             : 
     769           0 : void __enable_irq(struct irq_desc *desc)
     770             : {
     771           0 :         switch (desc->depth) {
     772             :         case 0:
     773             :  err_out:
     774           0 :                 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
     775             :                      irq_desc_get_irq(desc));
     776           0 :                 break;
     777             :         case 1: {
     778           0 :                 if (desc->istate & IRQS_SUSPENDED)
     779             :                         goto err_out;
     780             :                 /* Prevent probing on this irq: */
     781           0 :                 irq_settings_set_noprobe(desc);
     782             :                 /*
     783             :                  * Call irq_startup() not irq_enable() here because the
     784             :                  * interrupt might be marked NOAUTOEN. So irq_startup()
     785             :                  * needs to be invoked when it gets enabled the first
     786             :                  * time. If it was already started up, then irq_startup()
     787             :                  * will invoke irq_enable() under the hood.
     788             :                  */
     789           0 :                 irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE);
     790           0 :                 break;
     791             :         }
     792             :         default:
     793           0 :                 desc->depth--;
     794             :         }
     795           0 : }
     796             : 
     797             : /**
     798             :  *      enable_irq - enable handling of an irq
     799             :  *      @irq: Interrupt to enable
     800             :  *
     801             :  *      Undoes the effect of one call to disable_irq().  If this
     802             :  *      matches the last disable, processing of interrupts on this
     803             :  *      IRQ line is re-enabled.
     804             :  *
     805             :  *      This function may be called from IRQ context only when
     806             :  *      desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
     807             :  */
     808           0 : void enable_irq(unsigned int irq)
     809             : {
     810             :         unsigned long flags;
     811           0 :         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
     812             : 
     813           0 :         if (!desc)
     814           0 :                 return;
     815           0 :         if (WARN(!desc->irq_data.chip,
     816             :                  KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
     817             :                 goto out;
     818             : 
     819           0 :         __enable_irq(desc);
     820             : out:
     821           0 :         irq_put_desc_busunlock(desc, flags);
     822             : }
     823             : EXPORT_SYMBOL(enable_irq);
     824             : 
     825             : /**
     826             :  *      enable_nmi - enable handling of an nmi
     827             :  *      @irq: Interrupt to enable
     828             :  *
     829             :  *      The interrupt to enable must have been requested through request_nmi.
     830             :  *      Undoes the effect of one call to disable_nmi(). If this
     831             :  *      matches the last disable, processing of interrupts on this
     832             :  *      IRQ line is re-enabled.
     833             :  */
     834           0 : void enable_nmi(unsigned int irq)
     835             : {
     836           0 :         enable_irq(irq);
     837           0 : }
     838             : 
     839           0 : static int set_irq_wake_real(unsigned int irq, unsigned int on)
     840             : {
     841           0 :         struct irq_desc *desc = irq_to_desc(irq);
     842           0 :         int ret = -ENXIO;
     843             : 
     844           0 :         if (irq_desc_get_chip(desc)->flags &  IRQCHIP_SKIP_SET_WAKE)
     845             :                 return 0;
     846             : 
     847           0 :         if (desc->irq_data.chip->irq_set_wake)
     848           0 :                 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
     849             : 
     850             :         return ret;
     851             : }
     852             : 
     853             : /**
     854             :  *      irq_set_irq_wake - control irq power management wakeup
     855             :  *      @irq:   interrupt to control
     856             :  *      @on:    enable/disable power management wakeup
     857             :  *
     858             :  *      Enable/disable power management wakeup mode, which is
     859             :  *      disabled by default.  Enables and disables must match,
     860             :  *      just as they match for non-wakeup mode support.
     861             :  *
     862             :  *      Wakeup mode lets this IRQ wake the system from sleep
     863             :  *      states like "suspend to RAM".
     864             :  *
     865             :  *      Note: irq enable/disable state is completely orthogonal
     866             :  *      to the enable/disable state of irq wake. An irq can be
     867             :  *      disabled with disable_irq() and still wake the system as
     868             :  *      long as the irq has wake enabled. If this does not hold,
     869             :  *      then the underlying irq chip and the related driver need
     870             :  *      to be investigated.
     871             :  */
     872           0 : int irq_set_irq_wake(unsigned int irq, unsigned int on)
     873             : {
     874             :         unsigned long flags;
     875           0 :         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
     876           0 :         int ret = 0;
     877             : 
     878           0 :         if (!desc)
     879             :                 return -EINVAL;
     880             : 
     881             :         /* Don't use NMIs as wake up interrupts please */
     882           0 :         if (desc->istate & IRQS_NMI) {
     883             :                 ret = -EINVAL;
     884             :                 goto out_unlock;
     885             :         }
     886             : 
     887             :         /* wakeup-capable irqs can be shared between drivers that
     888             :          * don't need to have the same sleep mode behaviors.
     889             :          */
     890           0 :         if (on) {
     891           0 :                 if (desc->wake_depth++ == 0) {
     892           0 :                         ret = set_irq_wake_real(irq, on);
     893           0 :                         if (ret)
     894           0 :                                 desc->wake_depth = 0;
     895             :                         else
     896           0 :                                 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
     897             :                 }
     898             :         } else {
     899           0 :                 if (desc->wake_depth == 0) {
     900           0 :                         WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
     901           0 :                 } else if (--desc->wake_depth == 0) {
     902           0 :                         ret = set_irq_wake_real(irq, on);
     903           0 :                         if (ret)
     904           0 :                                 desc->wake_depth = 1;
     905             :                         else
     906           0 :                                 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
     907             :                 }
     908             :         }
     909             : 
     910             : out_unlock:
     911           0 :         irq_put_desc_busunlock(desc, flags);
     912           0 :         return ret;
     913             : }
     914             : EXPORT_SYMBOL(irq_set_irq_wake);
     915             : 
     916             : /*
     917             :  * Internal function that tells the architecture code whether a
     918             :  * particular irq has been exclusively allocated or is available
     919             :  * for driver use.
     920             :  */
     921           0 : int can_request_irq(unsigned int irq, unsigned long irqflags)
     922             : {
     923             :         unsigned long flags;
     924           0 :         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
     925           0 :         int canrequest = 0;
     926             : 
     927           0 :         if (!desc)
     928             :                 return 0;
     929             : 
     930           0 :         if (irq_settings_can_request(desc)) {
     931           0 :                 if (!desc->action ||
     932           0 :                     irqflags & desc->action->flags & IRQF_SHARED)
     933           0 :                         canrequest = 1;
     934             :         }
     935           0 :         irq_put_desc_unlock(desc, flags);
     936           0 :         return canrequest;
     937             : }
     938             : 
     939           0 : int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
     940             : {
     941           0 :         struct irq_chip *chip = desc->irq_data.chip;
     942           0 :         int ret, unmask = 0;
     943             : 
     944           0 :         if (!chip || !chip->irq_set_type) {
     945             :                 /*
     946             :                  * IRQF_TRIGGER_* but the PIC does not support multiple
     947             :                  * flow-types?
     948             :                  */
     949             :                 pr_debug("No set_type function for IRQ %d (%s)\n",
     950             :                          irq_desc_get_irq(desc),
     951             :                          chip ? (chip->name ? : "unknown") : "unknown");
     952             :                 return 0;
     953             :         }
     954             : 
     955           0 :         if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
     956           0 :                 if (!irqd_irq_masked(&desc->irq_data))
     957           0 :                         mask_irq(desc);
     958           0 :                 if (!irqd_irq_disabled(&desc->irq_data))
     959           0 :                         unmask = 1;
     960             :         }
     961             : 
     962             :         /* Mask all flags except trigger mode */
     963           0 :         flags &= IRQ_TYPE_SENSE_MASK;
     964           0 :         ret = chip->irq_set_type(&desc->irq_data, flags);
     965             : 
     966           0 :         switch (ret) {
     967             :         case IRQ_SET_MASK_OK:
     968             :         case IRQ_SET_MASK_OK_DONE:
     969           0 :                 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
     970           0 :                 irqd_set(&desc->irq_data, flags);
     971             :                 fallthrough;
     972             : 
     973             :         case IRQ_SET_MASK_OK_NOCOPY:
     974           0 :                 flags = irqd_get_trigger_type(&desc->irq_data);
     975           0 :                 irq_settings_set_trigger_mask(desc, flags);
     976           0 :                 irqd_clear(&desc->irq_data, IRQD_LEVEL);
     977           0 :                 irq_settings_clr_level(desc);
     978           0 :                 if (flags & IRQ_TYPE_LEVEL_MASK) {
     979           0 :                         irq_settings_set_level(desc);
     980           0 :                         irqd_set(&desc->irq_data, IRQD_LEVEL);
     981             :                 }
     982             : 
     983             :                 ret = 0;
     984             :                 break;
     985             :         default:
     986           0 :                 pr_err("Setting trigger mode %lu for irq %u failed (%pS)\n",
     987             :                        flags, irq_desc_get_irq(desc), chip->irq_set_type);
     988             :         }
     989           0 :         if (unmask)
     990           0 :                 unmask_irq(desc);
     991             :         return ret;
     992             : }
     993             : 
     994             : #ifdef CONFIG_HARDIRQS_SW_RESEND
     995             : int irq_set_parent(int irq, int parent_irq)
     996             : {
     997             :         unsigned long flags;
     998             :         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
     999             : 
    1000             :         if (!desc)
    1001             :                 return -EINVAL;
    1002             : 
    1003             :         desc->parent_irq = parent_irq;
    1004             : 
    1005             :         irq_put_desc_unlock(desc, flags);
    1006             :         return 0;
    1007             : }
    1008             : EXPORT_SYMBOL_GPL(irq_set_parent);
    1009             : #endif
    1010             : 
    1011             : /*
    1012             :  * Default primary interrupt handler for threaded interrupts. Is
    1013             :  * assigned as primary handler when request_threaded_irq is called
    1014             :  * with handler == NULL. Useful for oneshot interrupts.
    1015             :  */
    1016           0 : static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
    1017             : {
    1018           0 :         return IRQ_WAKE_THREAD;
    1019             : }
    1020             : 
    1021             : /*
    1022             :  * Primary handler for nested threaded interrupts. Should never be
    1023             :  * called.
    1024             :  */
    1025           0 : static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
    1026             : {
    1027           0 :         WARN(1, "Primary handler called for nested irq %d\n", irq);
    1028           0 :         return IRQ_NONE;
    1029             : }
    1030             : 
    1031           0 : static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
    1032             : {
    1033           0 :         WARN(1, "Secondary action handler called for irq %d\n", irq);
    1034           0 :         return IRQ_NONE;
    1035             : }
    1036             : 
    1037           0 : static int irq_wait_for_interrupt(struct irqaction *action)
    1038             : {
    1039             :         for (;;) {
    1040           0 :                 set_current_state(TASK_INTERRUPTIBLE);
    1041             : 
    1042           0 :                 if (kthread_should_stop()) {
    1043             :                         /* may need to run one last time */
    1044           0 :                         if (test_and_clear_bit(IRQTF_RUNTHREAD,
    1045           0 :                                                &action->thread_flags)) {
    1046           0 :                                 __set_current_state(TASK_RUNNING);
    1047           0 :                                 return 0;
    1048             :                         }
    1049           0 :                         __set_current_state(TASK_RUNNING);
    1050           0 :                         return -1;
    1051             :                 }
    1052             : 
    1053           0 :                 if (test_and_clear_bit(IRQTF_RUNTHREAD,
    1054           0 :                                        &action->thread_flags)) {
    1055           0 :                         __set_current_state(TASK_RUNNING);
    1056           0 :                         return 0;
    1057             :                 }
    1058           0 :                 schedule();
    1059             :         }
    1060             : }
    1061             : 
    1062             : /*
    1063             :  * Oneshot interrupts keep the irq line masked until the threaded
    1064             :  * handler finished. unmask if the interrupt has not been disabled and
    1065             :  * is marked MASKED.
    1066             :  */
    1067           0 : static void irq_finalize_oneshot(struct irq_desc *desc,
    1068             :                                  struct irqaction *action)
    1069             : {
    1070           0 :         if (!(desc->istate & IRQS_ONESHOT) ||
    1071           0 :             action->handler == irq_forced_secondary_handler)
    1072             :                 return;
    1073             : again:
    1074           0 :         chip_bus_lock(desc);
    1075           0 :         raw_spin_lock_irq(&desc->lock);
    1076             : 
    1077             :         /*
    1078             :          * Implausible though it may be we need to protect us against
    1079             :          * the following scenario:
    1080             :          *
    1081             :          * The thread is faster done than the hard interrupt handler
    1082             :          * on the other CPU. If we unmask the irq line then the
    1083             :          * interrupt can come in again and masks the line, leaves due
    1084             :          * to IRQS_INPROGRESS and the irq line is masked forever.
    1085             :          *
    1086             :          * This also serializes the state of shared oneshot handlers
    1087             :          * versus "desc->threads_oneshot |= action->thread_mask;" in
    1088             :          * irq_wake_thread(). See the comment there which explains the
    1089             :          * serialization.
    1090             :          */
    1091           0 :         if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
    1092           0 :                 raw_spin_unlock_irq(&desc->lock);
    1093           0 :                 chip_bus_sync_unlock(desc);
    1094             :                 cpu_relax();
    1095             :                 goto again;
    1096             :         }
    1097             : 
    1098             :         /*
    1099             :          * Now check again, whether the thread should run. Otherwise
    1100             :          * we would clear the threads_oneshot bit of this thread which
    1101             :          * was just set.
    1102             :          */
    1103           0 :         if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
    1104             :                 goto out_unlock;
    1105             : 
    1106           0 :         desc->threads_oneshot &= ~action->thread_mask;
    1107             : 
    1108           0 :         if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
    1109           0 :             irqd_irq_masked(&desc->irq_data))
    1110           0 :                 unmask_threaded_irq(desc);
    1111             : 
    1112             : out_unlock:
    1113           0 :         raw_spin_unlock_irq(&desc->lock);
    1114             :         chip_bus_sync_unlock(desc);
    1115             : }
    1116             : 
    1117             : #ifdef CONFIG_SMP
    1118             : /*
    1119             :  * Check whether we need to change the affinity of the interrupt thread.
    1120             :  */
    1121             : static void
    1122             : irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
    1123             : {
    1124             :         cpumask_var_t mask;
    1125             :         bool valid = true;
    1126             : 
    1127             :         if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
    1128             :                 return;
    1129             : 
    1130             :         /*
    1131             :          * In case we are out of memory we set IRQTF_AFFINITY again and
    1132             :          * try again next time
    1133             :          */
    1134             :         if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
    1135             :                 set_bit(IRQTF_AFFINITY, &action->thread_flags);
    1136             :                 return;
    1137             :         }
    1138             : 
    1139             :         raw_spin_lock_irq(&desc->lock);
    1140             :         /*
    1141             :          * This code is triggered unconditionally. Check the affinity
    1142             :          * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
    1143             :          */
    1144             :         if (cpumask_available(desc->irq_common_data.affinity)) {
    1145             :                 const struct cpumask *m;
    1146             : 
    1147             :                 m = irq_data_get_effective_affinity_mask(&desc->irq_data);
    1148             :                 cpumask_copy(mask, m);
    1149             :         } else {
    1150             :                 valid = false;
    1151             :         }
    1152             :         raw_spin_unlock_irq(&desc->lock);
    1153             : 
    1154             :         if (valid)
    1155             :                 set_cpus_allowed_ptr(current, mask);
    1156             :         free_cpumask_var(mask);
    1157             : }
    1158             : #else
    1159             : static inline void
    1160             : irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
    1161             : #endif
    1162             : 
    1163             : /*
    1164             :  * Interrupts which are not explicitly requested as threaded
    1165             :  * interrupts rely on the implicit bh/preempt disable of the hard irq
    1166             :  * context. So we need to disable bh here to avoid deadlocks and other
    1167             :  * side effects.
    1168             :  */
    1169             : static irqreturn_t
    1170             : irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
    1171             : {
    1172             :         irqreturn_t ret;
    1173             : 
    1174             :         local_bh_disable();
    1175             :         if (!IS_ENABLED(CONFIG_PREEMPT_RT))
    1176             :                 local_irq_disable();
    1177             :         ret = action->thread_fn(action->irq, action->dev_id);
    1178             :         if (ret == IRQ_HANDLED)
    1179             :                 atomic_inc(&desc->threads_handled);
    1180             : 
    1181             :         irq_finalize_oneshot(desc, action);
    1182             :         if (!IS_ENABLED(CONFIG_PREEMPT_RT))
    1183             :                 local_irq_enable();
    1184             :         local_bh_enable();
    1185             :         return ret;
    1186             : }
    1187             : 
    1188             : /*
    1189             :  * Interrupts explicitly requested as threaded interrupts want to be
    1190             :  * preemptible - many of them need to sleep and wait for slow busses to
    1191             :  * complete.
    1192             :  */
    1193           0 : static irqreturn_t irq_thread_fn(struct irq_desc *desc,
    1194             :                 struct irqaction *action)
    1195             : {
    1196             :         irqreturn_t ret;
    1197             : 
    1198           0 :         ret = action->thread_fn(action->irq, action->dev_id);
    1199           0 :         if (ret == IRQ_HANDLED)
    1200           0 :                 atomic_inc(&desc->threads_handled);
    1201             : 
    1202           0 :         irq_finalize_oneshot(desc, action);
    1203           0 :         return ret;
    1204             : }
    1205             : 
    1206           0 : static void wake_threads_waitq(struct irq_desc *desc)
    1207             : {
    1208           0 :         if (atomic_dec_and_test(&desc->threads_active))
    1209           0 :                 wake_up(&desc->wait_for_threads);
    1210           0 : }
    1211             : 
    1212           0 : static void irq_thread_dtor(struct callback_head *unused)
    1213             : {
    1214           0 :         struct task_struct *tsk = current;
    1215             :         struct irq_desc *desc;
    1216             :         struct irqaction *action;
    1217             : 
    1218           0 :         if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
    1219             :                 return;
    1220             : 
    1221           0 :         action = kthread_data(tsk);
    1222             : 
    1223           0 :         pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
    1224             :                tsk->comm, tsk->pid, action->irq);
    1225             : 
    1226             : 
    1227           0 :         desc = irq_to_desc(action->irq);
    1228             :         /*
    1229             :          * If IRQTF_RUNTHREAD is set, we need to decrement
    1230             :          * desc->threads_active and wake possible waiters.
    1231             :          */
    1232           0 :         if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
    1233           0 :                 wake_threads_waitq(desc);
    1234             : 
    1235             :         /* Prevent a stale desc->threads_oneshot */
    1236           0 :         irq_finalize_oneshot(desc, action);
    1237             : }
    1238             : 
    1239           0 : static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
    1240             : {
    1241           0 :         struct irqaction *secondary = action->secondary;
    1242             : 
    1243           0 :         if (WARN_ON_ONCE(!secondary))
    1244             :                 return;
    1245             : 
    1246           0 :         raw_spin_lock_irq(&desc->lock);
    1247           0 :         __irq_wake_thread(desc, secondary);
    1248           0 :         raw_spin_unlock_irq(&desc->lock);
    1249             : }
    1250             : 
    1251             : /*
    1252             :  * Internal function to notify that a interrupt thread is ready.
    1253             :  */
    1254             : static void irq_thread_set_ready(struct irq_desc *desc,
    1255             :                                  struct irqaction *action)
    1256             : {
    1257           0 :         set_bit(IRQTF_READY, &action->thread_flags);
    1258           0 :         wake_up(&desc->wait_for_threads);
    1259             : }
    1260             : 
    1261             : /*
    1262             :  * Internal function to wake up a interrupt thread and wait until it is
    1263             :  * ready.
    1264             :  */
    1265           4 : static void wake_up_and_wait_for_irq_thread_ready(struct irq_desc *desc,
    1266             :                                                   struct irqaction *action)
    1267             : {
    1268           4 :         if (!action || !action->thread)
    1269             :                 return;
    1270             : 
    1271           0 :         wake_up_process(action->thread);
    1272           0 :         wait_event(desc->wait_for_threads,
    1273             :                    test_bit(IRQTF_READY, &action->thread_flags));
    1274             : }
    1275             : 
    1276             : /*
    1277             :  * Interrupt handler thread
    1278             :  */
    1279           0 : static int irq_thread(void *data)
    1280             : {
    1281             :         struct callback_head on_exit_work;
    1282           0 :         struct irqaction *action = data;
    1283           0 :         struct irq_desc *desc = irq_to_desc(action->irq);
    1284             :         irqreturn_t (*handler_fn)(struct irq_desc *desc,
    1285             :                         struct irqaction *action);
    1286             : 
    1287           0 :         irq_thread_set_ready(desc, action);
    1288             : 
    1289           0 :         sched_set_fifo(current);
    1290             : 
    1291             :         if (force_irqthreads() && test_bit(IRQTF_FORCED_THREAD,
    1292             :                                            &action->thread_flags))
    1293             :                 handler_fn = irq_forced_thread_fn;
    1294             :         else
    1295           0 :                 handler_fn = irq_thread_fn;
    1296             : 
    1297           0 :         init_task_work(&on_exit_work, irq_thread_dtor);
    1298           0 :         task_work_add(current, &on_exit_work, TWA_NONE);
    1299             : 
    1300           0 :         irq_thread_check_affinity(desc, action);
    1301             : 
    1302           0 :         while (!irq_wait_for_interrupt(action)) {
    1303             :                 irqreturn_t action_ret;
    1304             : 
    1305           0 :                 irq_thread_check_affinity(desc, action);
    1306             : 
    1307           0 :                 action_ret = handler_fn(desc, action);
    1308           0 :                 if (action_ret == IRQ_WAKE_THREAD)
    1309           0 :                         irq_wake_secondary(desc, action);
    1310             : 
    1311           0 :                 wake_threads_waitq(desc);
    1312             :         }
    1313             : 
    1314             :         /*
    1315             :          * This is the regular exit path. __free_irq() is stopping the
    1316             :          * thread via kthread_stop() after calling
    1317             :          * synchronize_hardirq(). So neither IRQTF_RUNTHREAD nor the
    1318             :          * oneshot mask bit can be set.
    1319             :          */
    1320           0 :         task_work_cancel(current, irq_thread_dtor);
    1321           0 :         return 0;
    1322             : }
    1323             : 
    1324             : /**
    1325             :  *      irq_wake_thread - wake the irq thread for the action identified by dev_id
    1326             :  *      @irq:           Interrupt line
    1327             :  *      @dev_id:        Device identity for which the thread should be woken
    1328             :  *
    1329             :  */
    1330           0 : void irq_wake_thread(unsigned int irq, void *dev_id)
    1331             : {
    1332           0 :         struct irq_desc *desc = irq_to_desc(irq);
    1333             :         struct irqaction *action;
    1334             :         unsigned long flags;
    1335             : 
    1336           0 :         if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
    1337             :                 return;
    1338             : 
    1339           0 :         raw_spin_lock_irqsave(&desc->lock, flags);
    1340           0 :         for_each_action_of_desc(desc, action) {
    1341           0 :                 if (action->dev_id == dev_id) {
    1342           0 :                         if (action->thread)
    1343           0 :                                 __irq_wake_thread(desc, action);
    1344             :                         break;
    1345             :                 }
    1346             :         }
    1347           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    1348             : }
    1349             : EXPORT_SYMBOL_GPL(irq_wake_thread);
    1350             : 
    1351             : static int irq_setup_forced_threading(struct irqaction *new)
    1352             : {
    1353             :         if (!force_irqthreads())
    1354             :                 return 0;
    1355             :         if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
    1356             :                 return 0;
    1357             : 
    1358             :         /*
    1359             :          * No further action required for interrupts which are requested as
    1360             :          * threaded interrupts already
    1361             :          */
    1362             :         if (new->handler == irq_default_primary_handler)
    1363             :                 return 0;
    1364             : 
    1365             :         new->flags |= IRQF_ONESHOT;
    1366             : 
    1367             :         /*
    1368             :          * Handle the case where we have a real primary handler and a
    1369             :          * thread handler. We force thread them as well by creating a
    1370             :          * secondary action.
    1371             :          */
    1372             :         if (new->handler && new->thread_fn) {
    1373             :                 /* Allocate the secondary action */
    1374             :                 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
    1375             :                 if (!new->secondary)
    1376             :                         return -ENOMEM;
    1377             :                 new->secondary->handler = irq_forced_secondary_handler;
    1378             :                 new->secondary->thread_fn = new->thread_fn;
    1379             :                 new->secondary->dev_id = new->dev_id;
    1380             :                 new->secondary->irq = new->irq;
    1381             :                 new->secondary->name = new->name;
    1382             :         }
    1383             :         /* Deal with the primary handler */
    1384             :         set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
    1385             :         new->thread_fn = new->handler;
    1386             :         new->handler = irq_default_primary_handler;
    1387             :         return 0;
    1388             : }
    1389             : 
    1390             : static int irq_request_resources(struct irq_desc *desc)
    1391             : {
    1392           2 :         struct irq_data *d = &desc->irq_data;
    1393           2 :         struct irq_chip *c = d->chip;
    1394             : 
    1395           2 :         return c->irq_request_resources ? c->irq_request_resources(d) : 0;
    1396             : }
    1397             : 
    1398             : static void irq_release_resources(struct irq_desc *desc)
    1399             : {
    1400           0 :         struct irq_data *d = &desc->irq_data;
    1401           0 :         struct irq_chip *c = d->chip;
    1402             : 
    1403           0 :         if (c->irq_release_resources)
    1404           0 :                 c->irq_release_resources(d);
    1405             : }
    1406             : 
    1407             : static bool irq_supports_nmi(struct irq_desc *desc)
    1408             : {
    1409           0 :         struct irq_data *d = irq_desc_get_irq_data(desc);
    1410             : 
    1411             : #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
    1412             :         /* Only IRQs directly managed by the root irqchip can be set as NMI */
    1413           0 :         if (d->parent_data)
    1414             :                 return false;
    1415             : #endif
    1416             :         /* Don't support NMIs for chips behind a slow bus */
    1417           0 :         if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock)
    1418             :                 return false;
    1419             : 
    1420           0 :         return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
    1421             : }
    1422             : 
    1423             : static int irq_nmi_setup(struct irq_desc *desc)
    1424             : {
    1425           0 :         struct irq_data *d = irq_desc_get_irq_data(desc);
    1426           0 :         struct irq_chip *c = d->chip;
    1427             : 
    1428           0 :         return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL;
    1429             : }
    1430             : 
    1431             : static void irq_nmi_teardown(struct irq_desc *desc)
    1432             : {
    1433           0 :         struct irq_data *d = irq_desc_get_irq_data(desc);
    1434           0 :         struct irq_chip *c = d->chip;
    1435             : 
    1436           0 :         if (c->irq_nmi_teardown)
    1437           0 :                 c->irq_nmi_teardown(d);
    1438             : }
    1439             : 
    1440             : static int
    1441           0 : setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
    1442             : {
    1443             :         struct task_struct *t;
    1444             : 
    1445           0 :         if (!secondary) {
    1446           0 :                 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
    1447             :                                    new->name);
    1448             :         } else {
    1449           0 :                 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
    1450             :                                    new->name);
    1451             :         }
    1452             : 
    1453           0 :         if (IS_ERR(t))
    1454           0 :                 return PTR_ERR(t);
    1455             : 
    1456             :         /*
    1457             :          * We keep the reference to the task struct even if
    1458             :          * the thread dies to avoid that the interrupt code
    1459             :          * references an already freed task_struct.
    1460             :          */
    1461           0 :         new->thread = get_task_struct(t);
    1462             :         /*
    1463             :          * Tell the thread to set its affinity. This is
    1464             :          * important for shared interrupt handlers as we do
    1465             :          * not invoke setup_affinity() for the secondary
    1466             :          * handlers as everything is already set up. Even for
    1467             :          * interrupts marked with IRQF_NO_BALANCE this is
    1468             :          * correct as we want the thread to move to the cpu(s)
    1469             :          * on which the requesting code placed the interrupt.
    1470             :          */
    1471           0 :         set_bit(IRQTF_AFFINITY, &new->thread_flags);
    1472           0 :         return 0;
    1473             : }
    1474             : 
    1475             : /*
    1476             :  * Internal function to register an irqaction - typically used to
    1477             :  * allocate special interrupts that are part of the architecture.
    1478             :  *
    1479             :  * Locking rules:
    1480             :  *
    1481             :  * desc->request_mutex       Provides serialization against a concurrent free_irq()
    1482             :  *   chip_bus_lock      Provides serialization for slow bus operations
    1483             :  *     desc->lock    Provides serialization against hard interrupts
    1484             :  *
    1485             :  * chip_bus_lock and desc->lock are sufficient for all other management and
    1486             :  * interrupt related functions. desc->request_mutex solely serializes
    1487             :  * request/free_irq().
    1488             :  */
    1489             : static int
    1490           2 : __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
    1491             : {
    1492             :         struct irqaction *old, **old_ptr;
    1493           2 :         unsigned long flags, thread_mask = 0;
    1494           2 :         int ret, nested, shared = 0;
    1495             : 
    1496           2 :         if (!desc)
    1497             :                 return -EINVAL;
    1498             : 
    1499           2 :         if (desc->irq_data.chip == &no_irq_chip)
    1500             :                 return -ENOSYS;
    1501           2 :         if (!try_module_get(desc->owner))
    1502             :                 return -ENODEV;
    1503             : 
    1504           2 :         new->irq = irq;
    1505             : 
    1506             :         /*
    1507             :          * If the trigger type is not specified by the caller,
    1508             :          * then use the default for this interrupt.
    1509             :          */
    1510           2 :         if (!(new->flags & IRQF_TRIGGER_MASK))
    1511           4 :                 new->flags |= irqd_get_trigger_type(&desc->irq_data);
    1512             : 
    1513             :         /*
    1514             :          * Check whether the interrupt nests into another interrupt
    1515             :          * thread.
    1516             :          */
    1517           4 :         nested = irq_settings_is_nested_thread(desc);
    1518           2 :         if (nested) {
    1519           0 :                 if (!new->thread_fn) {
    1520             :                         ret = -EINVAL;
    1521             :                         goto out_mput;
    1522             :                 }
    1523             :                 /*
    1524             :                  * Replace the primary handler which was provided from
    1525             :                  * the driver for non nested interrupt handling by the
    1526             :                  * dummy function which warns when called.
    1527             :                  */
    1528           0 :                 new->handler = irq_nested_primary_handler;
    1529             :         } else {
    1530             :                 if (irq_settings_can_thread(desc)) {
    1531             :                         ret = irq_setup_forced_threading(new);
    1532             :                         if (ret)
    1533             :                                 goto out_mput;
    1534             :                 }
    1535             :         }
    1536             : 
    1537             :         /*
    1538             :          * Create a handler thread when a thread function is supplied
    1539             :          * and the interrupt does not nest into another interrupt
    1540             :          * thread.
    1541             :          */
    1542           2 :         if (new->thread_fn && !nested) {
    1543           0 :                 ret = setup_irq_thread(new, irq, false);
    1544           0 :                 if (ret)
    1545             :                         goto out_mput;
    1546           0 :                 if (new->secondary) {
    1547           0 :                         ret = setup_irq_thread(new->secondary, irq, true);
    1548           0 :                         if (ret)
    1549             :                                 goto out_thread;
    1550             :                 }
    1551             :         }
    1552             : 
    1553             :         /*
    1554             :          * Drivers are often written to work w/o knowledge about the
    1555             :          * underlying irq chip implementation, so a request for a
    1556             :          * threaded irq without a primary hard irq context handler
    1557             :          * requires the ONESHOT flag to be set. Some irq chips like
    1558             :          * MSI based interrupts are per se one shot safe. Check the
    1559             :          * chip flags, so we can avoid the unmask dance at the end of
    1560             :          * the threaded handler for those.
    1561             :          */
    1562           2 :         if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
    1563           0 :                 new->flags &= ~IRQF_ONESHOT;
    1564             : 
    1565             :         /*
    1566             :          * Protects against a concurrent __free_irq() call which might wait
    1567             :          * for synchronize_hardirq() to complete without holding the optional
    1568             :          * chip bus lock and desc->lock. Also protects against handing out
    1569             :          * a recycled oneshot thread_mask bit while it's still in use by
    1570             :          * its previous owner.
    1571             :          */
    1572           2 :         mutex_lock(&desc->request_mutex);
    1573             : 
    1574             :         /*
    1575             :          * Acquire bus lock as the irq_request_resources() callback below
    1576             :          * might rely on the serialization or the magic power management
    1577             :          * functions which are abusing the irq_bus_lock() callback,
    1578             :          */
    1579           2 :         chip_bus_lock(desc);
    1580             : 
    1581             :         /* First installed action requests resources. */
    1582           2 :         if (!desc->action) {
    1583           2 :                 ret = irq_request_resources(desc);
    1584           2 :                 if (ret) {
    1585           0 :                         pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
    1586             :                                new->name, irq, desc->irq_data.chip->name);
    1587           0 :                         goto out_bus_unlock;
    1588             :                 }
    1589             :         }
    1590             : 
    1591             :         /*
    1592             :          * The following block of code has to be executed atomically
    1593             :          * protected against a concurrent interrupt and any of the other
    1594             :          * management calls which are not serialized via
    1595             :          * desc->request_mutex or the optional bus lock.
    1596             :          */
    1597           2 :         raw_spin_lock_irqsave(&desc->lock, flags);
    1598           2 :         old_ptr = &desc->action;
    1599           2 :         old = *old_ptr;
    1600           2 :         if (old) {
    1601             :                 /*
    1602             :                  * Can't share interrupts unless both agree to and are
    1603             :                  * the same type (level, edge, polarity). So both flag
    1604             :                  * fields must have IRQF_SHARED set and the bits which
    1605             :                  * set the trigger type must match. Also all must
    1606             :                  * agree on ONESHOT.
    1607             :                  * Interrupt lines used for NMIs cannot be shared.
    1608             :                  */
    1609             :                 unsigned int oldtype;
    1610             : 
    1611           0 :                 if (desc->istate & IRQS_NMI) {
    1612           0 :                         pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n",
    1613             :                                 new->name, irq, desc->irq_data.chip->name);
    1614           0 :                         ret = -EINVAL;
    1615           0 :                         goto out_unlock;
    1616             :                 }
    1617             : 
    1618             :                 /*
    1619             :                  * If nobody did set the configuration before, inherit
    1620             :                  * the one provided by the requester.
    1621             :                  */
    1622           0 :                 if (irqd_trigger_type_was_set(&desc->irq_data)) {
    1623           0 :                         oldtype = irqd_get_trigger_type(&desc->irq_data);
    1624             :                 } else {
    1625           0 :                         oldtype = new->flags & IRQF_TRIGGER_MASK;
    1626           0 :                         irqd_set_trigger_type(&desc->irq_data, oldtype);
    1627             :                 }
    1628             : 
    1629           0 :                 if (!((old->flags & new->flags) & IRQF_SHARED) ||
    1630           0 :                     (oldtype != (new->flags & IRQF_TRIGGER_MASK)) ||
    1631           0 :                     ((old->flags ^ new->flags) & IRQF_ONESHOT))
    1632             :                         goto mismatch;
    1633             : 
    1634             :                 /* All handlers must agree on per-cpuness */
    1635           0 :                 if ((old->flags & IRQF_PERCPU) !=
    1636             :                     (new->flags & IRQF_PERCPU))
    1637             :                         goto mismatch;
    1638             : 
    1639             :                 /* add new interrupt at end of irq queue */
    1640             :                 do {
    1641             :                         /*
    1642             :                          * Or all existing action->thread_mask bits,
    1643             :                          * so we can find the next zero bit for this
    1644             :                          * new action.
    1645             :                          */
    1646           0 :                         thread_mask |= old->thread_mask;
    1647           0 :                         old_ptr = &old->next;
    1648           0 :                         old = *old_ptr;
    1649           0 :                 } while (old);
    1650             :                 shared = 1;
    1651             :         }
    1652             : 
    1653             :         /*
    1654             :          * Setup the thread mask for this irqaction for ONESHOT. For
    1655             :          * !ONESHOT irqs the thread mask is 0 so we can avoid a
    1656             :          * conditional in irq_wake_thread().
    1657             :          */
    1658           2 :         if (new->flags & IRQF_ONESHOT) {
    1659             :                 /*
    1660             :                  * Unlikely to have 32 resp 64 irqs sharing one line,
    1661             :                  * but who knows.
    1662             :                  */
    1663           0 :                 if (thread_mask == ~0UL) {
    1664             :                         ret = -EBUSY;
    1665             :                         goto out_unlock;
    1666             :                 }
    1667             :                 /*
    1668             :                  * The thread_mask for the action is or'ed to
    1669             :                  * desc->thread_active to indicate that the
    1670             :                  * IRQF_ONESHOT thread handler has been woken, but not
    1671             :                  * yet finished. The bit is cleared when a thread
    1672             :                  * completes. When all threads of a shared interrupt
    1673             :                  * line have completed desc->threads_active becomes
    1674             :                  * zero and the interrupt line is unmasked. See
    1675             :                  * handle.c:irq_wake_thread() for further information.
    1676             :                  *
    1677             :                  * If no thread is woken by primary (hard irq context)
    1678             :                  * interrupt handlers, then desc->threads_active is
    1679             :                  * also checked for zero to unmask the irq line in the
    1680             :                  * affected hard irq flow handlers
    1681             :                  * (handle_[fasteoi|level]_irq).
    1682             :                  *
    1683             :                  * The new action gets the first zero bit of
    1684             :                  * thread_mask assigned. See the loop above which or's
    1685             :                  * all existing action->thread_mask bits.
    1686             :                  */
    1687           0 :                 new->thread_mask = 1UL << ffz(thread_mask);
    1688             : 
    1689           2 :         } else if (new->handler == irq_default_primary_handler &&
    1690           0 :                    !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
    1691             :                 /*
    1692             :                  * The interrupt was requested with handler = NULL, so
    1693             :                  * we use the default primary handler for it. But it
    1694             :                  * does not have the oneshot flag set. In combination
    1695             :                  * with level interrupts this is deadly, because the
    1696             :                  * default primary handler just wakes the thread, then
    1697             :                  * the irq lines is reenabled, but the device still
    1698             :                  * has the level irq asserted. Rinse and repeat....
    1699             :                  *
    1700             :                  * While this works for edge type interrupts, we play
    1701             :                  * it safe and reject unconditionally because we can't
    1702             :                  * say for sure which type this interrupt really
    1703             :                  * has. The type flags are unreliable as the
    1704             :                  * underlying chip implementation can override them.
    1705             :                  */
    1706           0 :                 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for %s (irq %d)\n",
    1707             :                        new->name, irq);
    1708           0 :                 ret = -EINVAL;
    1709           0 :                 goto out_unlock;
    1710             :         }
    1711             : 
    1712           2 :         if (!shared) {
    1713             :                 /* Setup the type (level, edge polarity) if configured: */
    1714           2 :                 if (new->flags & IRQF_TRIGGER_MASK) {
    1715           0 :                         ret = __irq_set_trigger(desc,
    1716             :                                                 new->flags & IRQF_TRIGGER_MASK);
    1717             : 
    1718           0 :                         if (ret)
    1719             :                                 goto out_unlock;
    1720             :                 }
    1721             : 
    1722             :                 /*
    1723             :                  * Activate the interrupt. That activation must happen
    1724             :                  * independently of IRQ_NOAUTOEN. request_irq() can fail
    1725             :                  * and the callers are supposed to handle
    1726             :                  * that. enable_irq() of an interrupt requested with
    1727             :                  * IRQ_NOAUTOEN is not supposed to fail. The activation
    1728             :                  * keeps it in shutdown mode, it merily associates
    1729             :                  * resources if necessary and if that's not possible it
    1730             :                  * fails. Interrupts which are in managed shutdown mode
    1731             :                  * will simply ignore that activation request.
    1732             :                  */
    1733           2 :                 ret = irq_activate(desc);
    1734           2 :                 if (ret)
    1735             :                         goto out_unlock;
    1736             : 
    1737           2 :                 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
    1738             :                                   IRQS_ONESHOT | IRQS_WAITING);
    1739           4 :                 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
    1740             : 
    1741           2 :                 if (new->flags & IRQF_PERCPU) {
    1742           0 :                         irqd_set(&desc->irq_data, IRQD_PER_CPU);
    1743           0 :                         irq_settings_set_per_cpu(desc);
    1744           0 :                         if (new->flags & IRQF_NO_DEBUG)
    1745           0 :                                 irq_settings_set_no_debug(desc);
    1746             :                 }
    1747             : 
    1748           2 :                 if (noirqdebug)
    1749           0 :                         irq_settings_set_no_debug(desc);
    1750             : 
    1751           2 :                 if (new->flags & IRQF_ONESHOT)
    1752           0 :                         desc->istate |= IRQS_ONESHOT;
    1753             : 
    1754             :                 /* Exclude IRQ from balancing if requested */
    1755           2 :                 if (new->flags & IRQF_NOBALANCING) {
    1756           0 :                         irq_settings_set_no_balancing(desc);
    1757           0 :                         irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
    1758             :                 }
    1759             : 
    1760           4 :                 if (!(new->flags & IRQF_NO_AUTOEN) &&
    1761           2 :                     irq_settings_can_autoenable(desc)) {
    1762           2 :                         irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
    1763             :                 } else {
    1764             :                         /*
    1765             :                          * Shared interrupts do not go well with disabling
    1766             :                          * auto enable. The sharing interrupt might request
    1767             :                          * it while it's still disabled and then wait for
    1768             :                          * interrupts forever.
    1769             :                          */
    1770           0 :                         WARN_ON_ONCE(new->flags & IRQF_SHARED);
    1771             :                         /* Undo nested disables: */
    1772           0 :                         desc->depth = 1;
    1773             :                 }
    1774             : 
    1775           0 :         } else if (new->flags & IRQF_TRIGGER_MASK) {
    1776           0 :                 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
    1777           0 :                 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
    1778             : 
    1779           0 :                 if (nmsk != omsk)
    1780             :                         /* hope the handler works with current  trigger mode */
    1781           0 :                         pr_warn("irq %d uses trigger mode %u; requested %u\n",
    1782             :                                 irq, omsk, nmsk);
    1783             :         }
    1784             : 
    1785           2 :         *old_ptr = new;
    1786             : 
    1787           2 :         irq_pm_install_action(desc, new);
    1788             : 
    1789             :         /* Reset broken irq detection when installing new handler */
    1790           2 :         desc->irq_count = 0;
    1791           2 :         desc->irqs_unhandled = 0;
    1792             : 
    1793             :         /*
    1794             :          * Check whether we disabled the irq via the spurious handler
    1795             :          * before. Reenable it and give it another chance.
    1796             :          */
    1797           2 :         if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
    1798           0 :                 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
    1799           0 :                 __enable_irq(desc);
    1800             :         }
    1801             : 
    1802           4 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    1803           2 :         chip_bus_sync_unlock(desc);
    1804           2 :         mutex_unlock(&desc->request_mutex);
    1805             : 
    1806           2 :         irq_setup_timings(desc, new);
    1807             : 
    1808           2 :         wake_up_and_wait_for_irq_thread_ready(desc, new);
    1809           2 :         wake_up_and_wait_for_irq_thread_ready(desc, new->secondary);
    1810             : 
    1811           2 :         register_irq_proc(irq, desc);
    1812           2 :         new->dir = NULL;
    1813           2 :         register_handler_proc(irq, new);
    1814           2 :         return 0;
    1815             : 
    1816             : mismatch:
    1817           0 :         if (!(new->flags & IRQF_PROBE_SHARED)) {
    1818           0 :                 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
    1819             :                        irq, new->flags, new->name, old->flags, old->name);
    1820             : #ifdef CONFIG_DEBUG_SHIRQ
    1821             :                 dump_stack();
    1822             : #endif
    1823             :         }
    1824             :         ret = -EBUSY;
    1825             : 
    1826             : out_unlock:
    1827           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    1828             : 
    1829           0 :         if (!desc->action)
    1830             :                 irq_release_resources(desc);
    1831             : out_bus_unlock:
    1832           0 :         chip_bus_sync_unlock(desc);
    1833           0 :         mutex_unlock(&desc->request_mutex);
    1834             : 
    1835             : out_thread:
    1836           0 :         if (new->thread) {
    1837           0 :                 struct task_struct *t = new->thread;
    1838             : 
    1839           0 :                 new->thread = NULL;
    1840           0 :                 kthread_stop(t);
    1841           0 :                 put_task_struct(t);
    1842             :         }
    1843           0 :         if (new->secondary && new->secondary->thread) {
    1844           0 :                 struct task_struct *t = new->secondary->thread;
    1845             : 
    1846           0 :                 new->secondary->thread = NULL;
    1847           0 :                 kthread_stop(t);
    1848           0 :                 put_task_struct(t);
    1849             :         }
    1850             : out_mput:
    1851           0 :         module_put(desc->owner);
    1852           0 :         return ret;
    1853             : }
    1854             : 
    1855             : /*
    1856             :  * Internal function to unregister an irqaction - used to free
    1857             :  * regular and special interrupts that are part of the architecture.
    1858             :  */
    1859           0 : static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
    1860             : {
    1861           0 :         unsigned irq = desc->irq_data.irq;
    1862             :         struct irqaction *action, **action_ptr;
    1863             :         unsigned long flags;
    1864             : 
    1865           0 :         WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
    1866             : 
    1867           0 :         mutex_lock(&desc->request_mutex);
    1868           0 :         chip_bus_lock(desc);
    1869           0 :         raw_spin_lock_irqsave(&desc->lock, flags);
    1870             : 
    1871             :         /*
    1872             :          * There can be multiple actions per IRQ descriptor, find the right
    1873             :          * one based on the dev_id:
    1874             :          */
    1875           0 :         action_ptr = &desc->action;
    1876             :         for (;;) {
    1877           0 :                 action = *action_ptr;
    1878             : 
    1879           0 :                 if (!action) {
    1880           0 :                         WARN(1, "Trying to free already-free IRQ %d\n", irq);
    1881           0 :                         raw_spin_unlock_irqrestore(&desc->lock, flags);
    1882           0 :                         chip_bus_sync_unlock(desc);
    1883           0 :                         mutex_unlock(&desc->request_mutex);
    1884           0 :                         return NULL;
    1885             :                 }
    1886             : 
    1887           0 :                 if (action->dev_id == dev_id)
    1888             :                         break;
    1889           0 :                 action_ptr = &action->next;
    1890             :         }
    1891             : 
    1892             :         /* Found it - now remove it from the list of entries: */
    1893           0 :         *action_ptr = action->next;
    1894             : 
    1895           0 :         irq_pm_remove_action(desc, action);
    1896             : 
    1897             :         /* If this was the last handler, shut down the IRQ line: */
    1898           0 :         if (!desc->action) {
    1899           0 :                 irq_settings_clr_disable_unlazy(desc);
    1900             :                 /* Only shutdown. Deactivate after synchronize_hardirq() */
    1901           0 :                 irq_shutdown(desc);
    1902             :         }
    1903             : 
    1904             : #ifdef CONFIG_SMP
    1905             :         /* make sure affinity_hint is cleaned up */
    1906             :         if (WARN_ON_ONCE(desc->affinity_hint))
    1907             :                 desc->affinity_hint = NULL;
    1908             : #endif
    1909             : 
    1910           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    1911             :         /*
    1912             :          * Drop bus_lock here so the changes which were done in the chip
    1913             :          * callbacks above are synced out to the irq chips which hang
    1914             :          * behind a slow bus (I2C, SPI) before calling synchronize_hardirq().
    1915             :          *
    1916             :          * Aside of that the bus_lock can also be taken from the threaded
    1917             :          * handler in irq_finalize_oneshot() which results in a deadlock
    1918             :          * because kthread_stop() would wait forever for the thread to
    1919             :          * complete, which is blocked on the bus lock.
    1920             :          *
    1921             :          * The still held desc->request_mutex() protects against a
    1922             :          * concurrent request_irq() of this irq so the release of resources
    1923             :          * and timing data is properly serialized.
    1924             :          */
    1925           0 :         chip_bus_sync_unlock(desc);
    1926             : 
    1927           0 :         unregister_handler_proc(irq, action);
    1928             : 
    1929             :         /*
    1930             :          * Make sure it's not being used on another CPU and if the chip
    1931             :          * supports it also make sure that there is no (not yet serviced)
    1932             :          * interrupt in flight at the hardware level.
    1933             :          */
    1934           0 :         __synchronize_hardirq(desc, true);
    1935             : 
    1936             : #ifdef CONFIG_DEBUG_SHIRQ
    1937             :         /*
    1938             :          * It's a shared IRQ -- the driver ought to be prepared for an IRQ
    1939             :          * event to happen even now it's being freed, so let's make sure that
    1940             :          * is so by doing an extra call to the handler ....
    1941             :          *
    1942             :          * ( We do this after actually deregistering it, to make sure that a
    1943             :          *   'real' IRQ doesn't run in parallel with our fake. )
    1944             :          */
    1945             :         if (action->flags & IRQF_SHARED) {
    1946             :                 local_irq_save(flags);
    1947             :                 action->handler(irq, dev_id);
    1948             :                 local_irq_restore(flags);
    1949             :         }
    1950             : #endif
    1951             : 
    1952             :         /*
    1953             :          * The action has already been removed above, but the thread writes
    1954             :          * its oneshot mask bit when it completes. Though request_mutex is
    1955             :          * held across this which prevents __setup_irq() from handing out
    1956             :          * the same bit to a newly requested action.
    1957             :          */
    1958           0 :         if (action->thread) {
    1959           0 :                 kthread_stop(action->thread);
    1960           0 :                 put_task_struct(action->thread);
    1961           0 :                 if (action->secondary && action->secondary->thread) {
    1962           0 :                         kthread_stop(action->secondary->thread);
    1963           0 :                         put_task_struct(action->secondary->thread);
    1964             :                 }
    1965             :         }
    1966             : 
    1967             :         /* Last action releases resources */
    1968           0 :         if (!desc->action) {
    1969             :                 /*
    1970             :                  * Reacquire bus lock as irq_release_resources() might
    1971             :                  * require it to deallocate resources over the slow bus.
    1972             :                  */
    1973           0 :                 chip_bus_lock(desc);
    1974             :                 /*
    1975             :                  * There is no interrupt on the fly anymore. Deactivate it
    1976             :                  * completely.
    1977             :                  */
    1978           0 :                 raw_spin_lock_irqsave(&desc->lock, flags);
    1979           0 :                 irq_domain_deactivate_irq(&desc->irq_data);
    1980           0 :                 raw_spin_unlock_irqrestore(&desc->lock, flags);
    1981             : 
    1982           0 :                 irq_release_resources(desc);
    1983             :                 chip_bus_sync_unlock(desc);
    1984             :                 irq_remove_timings(desc);
    1985             :         }
    1986             : 
    1987           0 :         mutex_unlock(&desc->request_mutex);
    1988             : 
    1989           0 :         irq_chip_pm_put(&desc->irq_data);
    1990           0 :         module_put(desc->owner);
    1991           0 :         kfree(action->secondary);
    1992           0 :         return action;
    1993             : }
    1994             : 
    1995             : /**
    1996             :  *      free_irq - free an interrupt allocated with request_irq
    1997             :  *      @irq: Interrupt line to free
    1998             :  *      @dev_id: Device identity to free
    1999             :  *
    2000             :  *      Remove an interrupt handler. The handler is removed and if the
    2001             :  *      interrupt line is no longer in use by any driver it is disabled.
    2002             :  *      On a shared IRQ the caller must ensure the interrupt is disabled
    2003             :  *      on the card it drives before calling this function. The function
    2004             :  *      does not return until any executing interrupts for this IRQ
    2005             :  *      have completed.
    2006             :  *
    2007             :  *      This function must not be called from interrupt context.
    2008             :  *
    2009             :  *      Returns the devname argument passed to request_irq.
    2010             :  */
    2011           0 : const void *free_irq(unsigned int irq, void *dev_id)
    2012             : {
    2013           0 :         struct irq_desc *desc = irq_to_desc(irq);
    2014             :         struct irqaction *action;
    2015             :         const char *devname;
    2016             : 
    2017           0 :         if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
    2018             :                 return NULL;
    2019             : 
    2020             : #ifdef CONFIG_SMP
    2021             :         if (WARN_ON(desc->affinity_notify))
    2022             :                 desc->affinity_notify = NULL;
    2023             : #endif
    2024             : 
    2025           0 :         action = __free_irq(desc, dev_id);
    2026             : 
    2027           0 :         if (!action)
    2028             :                 return NULL;
    2029             : 
    2030           0 :         devname = action->name;
    2031           0 :         kfree(action);
    2032           0 :         return devname;
    2033             : }
    2034             : EXPORT_SYMBOL(free_irq);
    2035             : 
    2036             : /* This function must be called with desc->lock held */
    2037           0 : static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc)
    2038             : {
    2039           0 :         const char *devname = NULL;
    2040             : 
    2041           0 :         desc->istate &= ~IRQS_NMI;
    2042             : 
    2043           0 :         if (!WARN_ON(desc->action == NULL)) {
    2044           0 :                 irq_pm_remove_action(desc, desc->action);
    2045           0 :                 devname = desc->action->name;
    2046           0 :                 unregister_handler_proc(irq, desc->action);
    2047             : 
    2048           0 :                 kfree(desc->action);
    2049           0 :                 desc->action = NULL;
    2050             :         }
    2051             : 
    2052           0 :         irq_settings_clr_disable_unlazy(desc);
    2053           0 :         irq_shutdown_and_deactivate(desc);
    2054             : 
    2055           0 :         irq_release_resources(desc);
    2056             : 
    2057           0 :         irq_chip_pm_put(&desc->irq_data);
    2058           0 :         module_put(desc->owner);
    2059             : 
    2060           0 :         return devname;
    2061             : }
    2062             : 
    2063           0 : const void *free_nmi(unsigned int irq, void *dev_id)
    2064             : {
    2065           0 :         struct irq_desc *desc = irq_to_desc(irq);
    2066             :         unsigned long flags;
    2067             :         const void *devname;
    2068             : 
    2069           0 :         if (!desc || WARN_ON(!(desc->istate & IRQS_NMI)))
    2070             :                 return NULL;
    2071             : 
    2072           0 :         if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
    2073             :                 return NULL;
    2074             : 
    2075             :         /* NMI still enabled */
    2076           0 :         if (WARN_ON(desc->depth == 0))
    2077             :                 disable_nmi_nosync(irq);
    2078             : 
    2079           0 :         raw_spin_lock_irqsave(&desc->lock, flags);
    2080             : 
    2081           0 :         irq_nmi_teardown(desc);
    2082           0 :         devname = __cleanup_nmi(irq, desc);
    2083             : 
    2084           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    2085             : 
    2086           0 :         return devname;
    2087             : }
    2088             : 
    2089             : /**
    2090             :  *      request_threaded_irq - allocate an interrupt line
    2091             :  *      @irq: Interrupt line to allocate
    2092             :  *      @handler: Function to be called when the IRQ occurs.
    2093             :  *                Primary handler for threaded interrupts.
    2094             :  *                If handler is NULL and thread_fn != NULL
    2095             :  *                the default primary handler is installed.
    2096             :  *      @thread_fn: Function called from the irq handler thread
    2097             :  *                  If NULL, no irq thread is created
    2098             :  *      @irqflags: Interrupt type flags
    2099             :  *      @devname: An ascii name for the claiming device
    2100             :  *      @dev_id: A cookie passed back to the handler function
    2101             :  *
    2102             :  *      This call allocates interrupt resources and enables the
    2103             :  *      interrupt line and IRQ handling. From the point this
    2104             :  *      call is made your handler function may be invoked. Since
    2105             :  *      your handler function must clear any interrupt the board
    2106             :  *      raises, you must take care both to initialise your hardware
    2107             :  *      and to set up the interrupt handler in the right order.
    2108             :  *
    2109             :  *      If you want to set up a threaded irq handler for your device
    2110             :  *      then you need to supply @handler and @thread_fn. @handler is
    2111             :  *      still called in hard interrupt context and has to check
    2112             :  *      whether the interrupt originates from the device. If yes it
    2113             :  *      needs to disable the interrupt on the device and return
    2114             :  *      IRQ_WAKE_THREAD which will wake up the handler thread and run
    2115             :  *      @thread_fn. This split handler design is necessary to support
    2116             :  *      shared interrupts.
    2117             :  *
    2118             :  *      Dev_id must be globally unique. Normally the address of the
    2119             :  *      device data structure is used as the cookie. Since the handler
    2120             :  *      receives this value it makes sense to use it.
    2121             :  *
    2122             :  *      If your interrupt is shared you must pass a non NULL dev_id
    2123             :  *      as this is required when freeing the interrupt.
    2124             :  *
    2125             :  *      Flags:
    2126             :  *
    2127             :  *      IRQF_SHARED             Interrupt is shared
    2128             :  *      IRQF_TRIGGER_*          Specify active edge(s) or level
    2129             :  *      IRQF_ONESHOT            Run thread_fn with interrupt line masked
    2130             :  */
    2131           2 : int request_threaded_irq(unsigned int irq, irq_handler_t handler,
    2132             :                          irq_handler_t thread_fn, unsigned long irqflags,
    2133             :                          const char *devname, void *dev_id)
    2134             : {
    2135             :         struct irqaction *action;
    2136             :         struct irq_desc *desc;
    2137             :         int retval;
    2138             : 
    2139           2 :         if (irq == IRQ_NOTCONNECTED)
    2140             :                 return -ENOTCONN;
    2141             : 
    2142             :         /*
    2143             :          * Sanity-check: shared interrupts must pass in a real dev-ID,
    2144             :          * otherwise we'll have trouble later trying to figure out
    2145             :          * which interrupt is which (messes up the interrupt freeing
    2146             :          * logic etc).
    2147             :          *
    2148             :          * Also shared interrupts do not go well with disabling auto enable.
    2149             :          * The sharing interrupt might request it while it's still disabled
    2150             :          * and then wait for interrupts forever.
    2151             :          *
    2152             :          * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
    2153             :          * it cannot be set along with IRQF_NO_SUSPEND.
    2154             :          */
    2155           4 :         if (((irqflags & IRQF_SHARED) && !dev_id) ||
    2156           4 :             ((irqflags & IRQF_SHARED) && (irqflags & IRQF_NO_AUTOEN)) ||
    2157           4 :             (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
    2158           2 :             ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
    2159             :                 return -EINVAL;
    2160             : 
    2161           2 :         desc = irq_to_desc(irq);
    2162           2 :         if (!desc)
    2163             :                 return -EINVAL;
    2164             : 
    2165           6 :         if (!irq_settings_can_request(desc) ||
    2166           4 :             WARN_ON(irq_settings_is_per_cpu_devid(desc)))
    2167             :                 return -EINVAL;
    2168             : 
    2169           2 :         if (!handler) {
    2170           0 :                 if (!thread_fn)
    2171             :                         return -EINVAL;
    2172             :                 handler = irq_default_primary_handler;
    2173             :         }
    2174             : 
    2175           2 :         action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
    2176           2 :         if (!action)
    2177             :                 return -ENOMEM;
    2178             : 
    2179           2 :         action->handler = handler;
    2180           2 :         action->thread_fn = thread_fn;
    2181           2 :         action->flags = irqflags;
    2182           2 :         action->name = devname;
    2183           2 :         action->dev_id = dev_id;
    2184             : 
    2185           2 :         retval = irq_chip_pm_get(&desc->irq_data);
    2186           2 :         if (retval < 0) {
    2187           0 :                 kfree(action);
    2188           0 :                 return retval;
    2189             :         }
    2190             : 
    2191           2 :         retval = __setup_irq(irq, desc, action);
    2192             : 
    2193           2 :         if (retval) {
    2194           0 :                 irq_chip_pm_put(&desc->irq_data);
    2195           0 :                 kfree(action->secondary);
    2196           0 :                 kfree(action);
    2197             :         }
    2198             : 
    2199             : #ifdef CONFIG_DEBUG_SHIRQ_FIXME
    2200             :         if (!retval && (irqflags & IRQF_SHARED)) {
    2201             :                 /*
    2202             :                  * It's a shared IRQ -- the driver ought to be prepared for it
    2203             :                  * to happen immediately, so let's make sure....
    2204             :                  * We disable the irq to make sure that a 'real' IRQ doesn't
    2205             :                  * run in parallel with our fake.
    2206             :                  */
    2207             :                 unsigned long flags;
    2208             : 
    2209             :                 disable_irq(irq);
    2210             :                 local_irq_save(flags);
    2211             : 
    2212             :                 handler(irq, dev_id);
    2213             : 
    2214             :                 local_irq_restore(flags);
    2215             :                 enable_irq(irq);
    2216             :         }
    2217             : #endif
    2218             :         return retval;
    2219             : }
    2220             : EXPORT_SYMBOL(request_threaded_irq);
    2221             : 
    2222             : /**
    2223             :  *      request_any_context_irq - allocate an interrupt line
    2224             :  *      @irq: Interrupt line to allocate
    2225             :  *      @handler: Function to be called when the IRQ occurs.
    2226             :  *                Threaded handler for threaded interrupts.
    2227             :  *      @flags: Interrupt type flags
    2228             :  *      @name: An ascii name for the claiming device
    2229             :  *      @dev_id: A cookie passed back to the handler function
    2230             :  *
    2231             :  *      This call allocates interrupt resources and enables the
    2232             :  *      interrupt line and IRQ handling. It selects either a
    2233             :  *      hardirq or threaded handling method depending on the
    2234             :  *      context.
    2235             :  *
    2236             :  *      On failure, it returns a negative value. On success,
    2237             :  *      it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
    2238             :  */
    2239           0 : int request_any_context_irq(unsigned int irq, irq_handler_t handler,
    2240             :                             unsigned long flags, const char *name, void *dev_id)
    2241             : {
    2242             :         struct irq_desc *desc;
    2243             :         int ret;
    2244             : 
    2245           0 :         if (irq == IRQ_NOTCONNECTED)
    2246             :                 return -ENOTCONN;
    2247             : 
    2248           0 :         desc = irq_to_desc(irq);
    2249           0 :         if (!desc)
    2250             :                 return -EINVAL;
    2251             : 
    2252           0 :         if (irq_settings_is_nested_thread(desc)) {
    2253           0 :                 ret = request_threaded_irq(irq, NULL, handler,
    2254             :                                            flags, name, dev_id);
    2255           0 :                 return !ret ? IRQC_IS_NESTED : ret;
    2256             :         }
    2257             : 
    2258           0 :         ret = request_irq(irq, handler, flags, name, dev_id);
    2259           0 :         return !ret ? IRQC_IS_HARDIRQ : ret;
    2260             : }
    2261             : EXPORT_SYMBOL_GPL(request_any_context_irq);
    2262             : 
    2263             : /**
    2264             :  *      request_nmi - allocate an interrupt line for NMI delivery
    2265             :  *      @irq: Interrupt line to allocate
    2266             :  *      @handler: Function to be called when the IRQ occurs.
    2267             :  *                Threaded handler for threaded interrupts.
    2268             :  *      @irqflags: Interrupt type flags
    2269             :  *      @name: An ascii name for the claiming device
    2270             :  *      @dev_id: A cookie passed back to the handler function
    2271             :  *
    2272             :  *      This call allocates interrupt resources and enables the
    2273             :  *      interrupt line and IRQ handling. It sets up the IRQ line
    2274             :  *      to be handled as an NMI.
    2275             :  *
    2276             :  *      An interrupt line delivering NMIs cannot be shared and IRQ handling
    2277             :  *      cannot be threaded.
    2278             :  *
    2279             :  *      Interrupt lines requested for NMI delivering must produce per cpu
    2280             :  *      interrupts and have auto enabling setting disabled.
    2281             :  *
    2282             :  *      Dev_id must be globally unique. Normally the address of the
    2283             :  *      device data structure is used as the cookie. Since the handler
    2284             :  *      receives this value it makes sense to use it.
    2285             :  *
    2286             :  *      If the interrupt line cannot be used to deliver NMIs, function
    2287             :  *      will fail and return a negative value.
    2288             :  */
    2289           0 : int request_nmi(unsigned int irq, irq_handler_t handler,
    2290             :                 unsigned long irqflags, const char *name, void *dev_id)
    2291             : {
    2292             :         struct irqaction *action;
    2293             :         struct irq_desc *desc;
    2294             :         unsigned long flags;
    2295             :         int retval;
    2296             : 
    2297           0 :         if (irq == IRQ_NOTCONNECTED)
    2298             :                 return -ENOTCONN;
    2299             : 
    2300             :         /* NMI cannot be shared, used for Polling */
    2301           0 :         if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
    2302             :                 return -EINVAL;
    2303             : 
    2304           0 :         if (!(irqflags & IRQF_PERCPU))
    2305             :                 return -EINVAL;
    2306             : 
    2307           0 :         if (!handler)
    2308             :                 return -EINVAL;
    2309             : 
    2310           0 :         desc = irq_to_desc(irq);
    2311             : 
    2312           0 :         if (!desc || (irq_settings_can_autoenable(desc) &&
    2313           0 :             !(irqflags & IRQF_NO_AUTOEN)) ||
    2314           0 :             !irq_settings_can_request(desc) ||
    2315           0 :             WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
    2316           0 :             !irq_supports_nmi(desc))
    2317             :                 return -EINVAL;
    2318             : 
    2319           0 :         action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
    2320           0 :         if (!action)
    2321             :                 return -ENOMEM;
    2322             : 
    2323           0 :         action->handler = handler;
    2324           0 :         action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING;
    2325           0 :         action->name = name;
    2326           0 :         action->dev_id = dev_id;
    2327             : 
    2328           0 :         retval = irq_chip_pm_get(&desc->irq_data);
    2329           0 :         if (retval < 0)
    2330             :                 goto err_out;
    2331             : 
    2332           0 :         retval = __setup_irq(irq, desc, action);
    2333           0 :         if (retval)
    2334             :                 goto err_irq_setup;
    2335             : 
    2336           0 :         raw_spin_lock_irqsave(&desc->lock, flags);
    2337             : 
    2338             :         /* Setup NMI state */
    2339           0 :         desc->istate |= IRQS_NMI;
    2340           0 :         retval = irq_nmi_setup(desc);
    2341           0 :         if (retval) {
    2342           0 :                 __cleanup_nmi(irq, desc);
    2343           0 :                 raw_spin_unlock_irqrestore(&desc->lock, flags);
    2344           0 :                 return -EINVAL;
    2345             :         }
    2346             : 
    2347           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    2348             : 
    2349           0 :         return 0;
    2350             : 
    2351             : err_irq_setup:
    2352           0 :         irq_chip_pm_put(&desc->irq_data);
    2353             : err_out:
    2354           0 :         kfree(action);
    2355             : 
    2356           0 :         return retval;
    2357             : }
    2358             : 
    2359           0 : void enable_percpu_irq(unsigned int irq, unsigned int type)
    2360             : {
    2361           0 :         unsigned int cpu = smp_processor_id();
    2362             :         unsigned long flags;
    2363           0 :         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
    2364             : 
    2365           0 :         if (!desc)
    2366           0 :                 return;
    2367             : 
    2368             :         /*
    2369             :          * If the trigger type is not specified by the caller, then
    2370             :          * use the default for this interrupt.
    2371             :          */
    2372           0 :         type &= IRQ_TYPE_SENSE_MASK;
    2373           0 :         if (type == IRQ_TYPE_NONE)
    2374           0 :                 type = irqd_get_trigger_type(&desc->irq_data);
    2375             : 
    2376           0 :         if (type != IRQ_TYPE_NONE) {
    2377             :                 int ret;
    2378             : 
    2379           0 :                 ret = __irq_set_trigger(desc, type);
    2380             : 
    2381           0 :                 if (ret) {
    2382           0 :                         WARN(1, "failed to set type for IRQ%d\n", irq);
    2383           0 :                         goto out;
    2384             :                 }
    2385             :         }
    2386             : 
    2387           0 :         irq_percpu_enable(desc, cpu);
    2388             : out:
    2389           0 :         irq_put_desc_unlock(desc, flags);
    2390             : }
    2391             : EXPORT_SYMBOL_GPL(enable_percpu_irq);
    2392             : 
    2393           0 : void enable_percpu_nmi(unsigned int irq, unsigned int type)
    2394             : {
    2395           0 :         enable_percpu_irq(irq, type);
    2396           0 : }
    2397             : 
    2398             : /**
    2399             :  * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
    2400             :  * @irq:        Linux irq number to check for
    2401             :  *
    2402             :  * Must be called from a non migratable context. Returns the enable
    2403             :  * state of a per cpu interrupt on the current cpu.
    2404             :  */
    2405           0 : bool irq_percpu_is_enabled(unsigned int irq)
    2406             : {
    2407           0 :         unsigned int cpu = smp_processor_id();
    2408             :         struct irq_desc *desc;
    2409             :         unsigned long flags;
    2410             :         bool is_enabled;
    2411             : 
    2412           0 :         desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
    2413           0 :         if (!desc)
    2414             :                 return false;
    2415             : 
    2416           0 :         is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
    2417           0 :         irq_put_desc_unlock(desc, flags);
    2418             : 
    2419           0 :         return is_enabled;
    2420             : }
    2421             : EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
    2422             : 
    2423           0 : void disable_percpu_irq(unsigned int irq)
    2424             : {
    2425           0 :         unsigned int cpu = smp_processor_id();
    2426             :         unsigned long flags;
    2427           0 :         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
    2428             : 
    2429           0 :         if (!desc)
    2430           0 :                 return;
    2431             : 
    2432           0 :         irq_percpu_disable(desc, cpu);
    2433           0 :         irq_put_desc_unlock(desc, flags);
    2434             : }
    2435             : EXPORT_SYMBOL_GPL(disable_percpu_irq);
    2436             : 
    2437           0 : void disable_percpu_nmi(unsigned int irq)
    2438             : {
    2439           0 :         disable_percpu_irq(irq);
    2440           0 : }
    2441             : 
    2442             : /*
    2443             :  * Internal function to unregister a percpu irqaction.
    2444             :  */
    2445           0 : static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
    2446             : {
    2447           0 :         struct irq_desc *desc = irq_to_desc(irq);
    2448             :         struct irqaction *action;
    2449             :         unsigned long flags;
    2450             : 
    2451           0 :         WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
    2452             : 
    2453           0 :         if (!desc)
    2454             :                 return NULL;
    2455             : 
    2456           0 :         raw_spin_lock_irqsave(&desc->lock, flags);
    2457             : 
    2458           0 :         action = desc->action;
    2459           0 :         if (!action || action->percpu_dev_id != dev_id) {
    2460           0 :                 WARN(1, "Trying to free already-free IRQ %d\n", irq);
    2461           0 :                 goto bad;
    2462             :         }
    2463             : 
    2464           0 :         if (!cpumask_empty(desc->percpu_enabled)) {
    2465           0 :                 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
    2466             :                      irq, cpumask_first(desc->percpu_enabled));
    2467           0 :                 goto bad;
    2468             :         }
    2469             : 
    2470             :         /* Found it - now remove it from the list of entries: */
    2471           0 :         desc->action = NULL;
    2472             : 
    2473           0 :         desc->istate &= ~IRQS_NMI;
    2474             : 
    2475           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    2476             : 
    2477           0 :         unregister_handler_proc(irq, action);
    2478             : 
    2479           0 :         irq_chip_pm_put(&desc->irq_data);
    2480           0 :         module_put(desc->owner);
    2481           0 :         return action;
    2482             : 
    2483             : bad:
    2484           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    2485           0 :         return NULL;
    2486             : }
    2487             : 
    2488             : /**
    2489             :  *      remove_percpu_irq - free a per-cpu interrupt
    2490             :  *      @irq: Interrupt line to free
    2491             :  *      @act: irqaction for the interrupt
    2492             :  *
    2493             :  * Used to remove interrupts statically setup by the early boot process.
    2494             :  */
    2495           0 : void remove_percpu_irq(unsigned int irq, struct irqaction *act)
    2496             : {
    2497           0 :         struct irq_desc *desc = irq_to_desc(irq);
    2498             : 
    2499           0 :         if (desc && irq_settings_is_per_cpu_devid(desc))
    2500           0 :             __free_percpu_irq(irq, act->percpu_dev_id);
    2501           0 : }
    2502             : 
    2503             : /**
    2504             :  *      free_percpu_irq - free an interrupt allocated with request_percpu_irq
    2505             :  *      @irq: Interrupt line to free
    2506             :  *      @dev_id: Device identity to free
    2507             :  *
    2508             :  *      Remove a percpu interrupt handler. The handler is removed, but
    2509             :  *      the interrupt line is not disabled. This must be done on each
    2510             :  *      CPU before calling this function. The function does not return
    2511             :  *      until any executing interrupts for this IRQ have completed.
    2512             :  *
    2513             :  *      This function must not be called from interrupt context.
    2514             :  */
    2515           0 : void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
    2516             : {
    2517           0 :         struct irq_desc *desc = irq_to_desc(irq);
    2518             : 
    2519           0 :         if (!desc || !irq_settings_is_per_cpu_devid(desc))
    2520             :                 return;
    2521             : 
    2522           0 :         chip_bus_lock(desc);
    2523           0 :         kfree(__free_percpu_irq(irq, dev_id));
    2524             :         chip_bus_sync_unlock(desc);
    2525             : }
    2526             : EXPORT_SYMBOL_GPL(free_percpu_irq);
    2527             : 
    2528           0 : void free_percpu_nmi(unsigned int irq, void __percpu *dev_id)
    2529             : {
    2530           0 :         struct irq_desc *desc = irq_to_desc(irq);
    2531             : 
    2532           0 :         if (!desc || !irq_settings_is_per_cpu_devid(desc))
    2533             :                 return;
    2534             : 
    2535           0 :         if (WARN_ON(!(desc->istate & IRQS_NMI)))
    2536             :                 return;
    2537             : 
    2538           0 :         kfree(__free_percpu_irq(irq, dev_id));
    2539             : }
    2540             : 
    2541             : /**
    2542             :  *      setup_percpu_irq - setup a per-cpu interrupt
    2543             :  *      @irq: Interrupt line to setup
    2544             :  *      @act: irqaction for the interrupt
    2545             :  *
    2546             :  * Used to statically setup per-cpu interrupts in the early boot process.
    2547             :  */
    2548           0 : int setup_percpu_irq(unsigned int irq, struct irqaction *act)
    2549             : {
    2550           0 :         struct irq_desc *desc = irq_to_desc(irq);
    2551             :         int retval;
    2552             : 
    2553           0 :         if (!desc || !irq_settings_is_per_cpu_devid(desc))
    2554             :                 return -EINVAL;
    2555             : 
    2556           0 :         retval = irq_chip_pm_get(&desc->irq_data);
    2557           0 :         if (retval < 0)
    2558             :                 return retval;
    2559             : 
    2560           0 :         retval = __setup_irq(irq, desc, act);
    2561             : 
    2562           0 :         if (retval)
    2563           0 :                 irq_chip_pm_put(&desc->irq_data);
    2564             : 
    2565             :         return retval;
    2566             : }
    2567             : 
    2568             : /**
    2569             :  *      __request_percpu_irq - allocate a percpu interrupt line
    2570             :  *      @irq: Interrupt line to allocate
    2571             :  *      @handler: Function to be called when the IRQ occurs.
    2572             :  *      @flags: Interrupt type flags (IRQF_TIMER only)
    2573             :  *      @devname: An ascii name for the claiming device
    2574             :  *      @dev_id: A percpu cookie passed back to the handler function
    2575             :  *
    2576             :  *      This call allocates interrupt resources and enables the
    2577             :  *      interrupt on the local CPU. If the interrupt is supposed to be
    2578             :  *      enabled on other CPUs, it has to be done on each CPU using
    2579             :  *      enable_percpu_irq().
    2580             :  *
    2581             :  *      Dev_id must be globally unique. It is a per-cpu variable, and
    2582             :  *      the handler gets called with the interrupted CPU's instance of
    2583             :  *      that variable.
    2584             :  */
    2585           0 : int __request_percpu_irq(unsigned int irq, irq_handler_t handler,
    2586             :                          unsigned long flags, const char *devname,
    2587             :                          void __percpu *dev_id)
    2588             : {
    2589             :         struct irqaction *action;
    2590             :         struct irq_desc *desc;
    2591             :         int retval;
    2592             : 
    2593           0 :         if (!dev_id)
    2594             :                 return -EINVAL;
    2595             : 
    2596           0 :         desc = irq_to_desc(irq);
    2597           0 :         if (!desc || !irq_settings_can_request(desc) ||
    2598           0 :             !irq_settings_is_per_cpu_devid(desc))
    2599             :                 return -EINVAL;
    2600             : 
    2601           0 :         if (flags && flags != IRQF_TIMER)
    2602             :                 return -EINVAL;
    2603             : 
    2604           0 :         action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
    2605           0 :         if (!action)
    2606             :                 return -ENOMEM;
    2607             : 
    2608           0 :         action->handler = handler;
    2609           0 :         action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND;
    2610           0 :         action->name = devname;
    2611           0 :         action->percpu_dev_id = dev_id;
    2612             : 
    2613           0 :         retval = irq_chip_pm_get(&desc->irq_data);
    2614           0 :         if (retval < 0) {
    2615           0 :                 kfree(action);
    2616           0 :                 return retval;
    2617             :         }
    2618             : 
    2619           0 :         retval = __setup_irq(irq, desc, action);
    2620             : 
    2621           0 :         if (retval) {
    2622           0 :                 irq_chip_pm_put(&desc->irq_data);
    2623           0 :                 kfree(action);
    2624             :         }
    2625             : 
    2626             :         return retval;
    2627             : }
    2628             : EXPORT_SYMBOL_GPL(__request_percpu_irq);
    2629             : 
    2630             : /**
    2631             :  *      request_percpu_nmi - allocate a percpu interrupt line for NMI delivery
    2632             :  *      @irq: Interrupt line to allocate
    2633             :  *      @handler: Function to be called when the IRQ occurs.
    2634             :  *      @name: An ascii name for the claiming device
    2635             :  *      @dev_id: A percpu cookie passed back to the handler function
    2636             :  *
    2637             :  *      This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
    2638             :  *      have to be setup on each CPU by calling prepare_percpu_nmi() before
    2639             :  *      being enabled on the same CPU by using enable_percpu_nmi().
    2640             :  *
    2641             :  *      Dev_id must be globally unique. It is a per-cpu variable, and
    2642             :  *      the handler gets called with the interrupted CPU's instance of
    2643             :  *      that variable.
    2644             :  *
    2645             :  *      Interrupt lines requested for NMI delivering should have auto enabling
    2646             :  *      setting disabled.
    2647             :  *
    2648             :  *      If the interrupt line cannot be used to deliver NMIs, function
    2649             :  *      will fail returning a negative value.
    2650             :  */
    2651           0 : int request_percpu_nmi(unsigned int irq, irq_handler_t handler,
    2652             :                        const char *name, void __percpu *dev_id)
    2653             : {
    2654             :         struct irqaction *action;
    2655             :         struct irq_desc *desc;
    2656             :         unsigned long flags;
    2657             :         int retval;
    2658             : 
    2659           0 :         if (!handler)
    2660             :                 return -EINVAL;
    2661             : 
    2662           0 :         desc = irq_to_desc(irq);
    2663             : 
    2664           0 :         if (!desc || !irq_settings_can_request(desc) ||
    2665           0 :             !irq_settings_is_per_cpu_devid(desc) ||
    2666           0 :             irq_settings_can_autoenable(desc) ||
    2667           0 :             !irq_supports_nmi(desc))
    2668             :                 return -EINVAL;
    2669             : 
    2670             :         /* The line cannot already be NMI */
    2671           0 :         if (desc->istate & IRQS_NMI)
    2672             :                 return -EINVAL;
    2673             : 
    2674           0 :         action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
    2675           0 :         if (!action)
    2676             :                 return -ENOMEM;
    2677             : 
    2678           0 :         action->handler = handler;
    2679           0 :         action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND | IRQF_NO_THREAD
    2680             :                 | IRQF_NOBALANCING;
    2681           0 :         action->name = name;
    2682           0 :         action->percpu_dev_id = dev_id;
    2683             : 
    2684           0 :         retval = irq_chip_pm_get(&desc->irq_data);
    2685           0 :         if (retval < 0)
    2686             :                 goto err_out;
    2687             : 
    2688           0 :         retval = __setup_irq(irq, desc, action);
    2689           0 :         if (retval)
    2690             :                 goto err_irq_setup;
    2691             : 
    2692           0 :         raw_spin_lock_irqsave(&desc->lock, flags);
    2693           0 :         desc->istate |= IRQS_NMI;
    2694           0 :         raw_spin_unlock_irqrestore(&desc->lock, flags);
    2695             : 
    2696           0 :         return 0;
    2697             : 
    2698             : err_irq_setup:
    2699           0 :         irq_chip_pm_put(&desc->irq_data);
    2700             : err_out:
    2701           0 :         kfree(action);
    2702             : 
    2703           0 :         return retval;
    2704             : }
    2705             : 
    2706             : /**
    2707             :  *      prepare_percpu_nmi - performs CPU local setup for NMI delivery
    2708             :  *      @irq: Interrupt line to prepare for NMI delivery
    2709             :  *
    2710             :  *      This call prepares an interrupt line to deliver NMI on the current CPU,
    2711             :  *      before that interrupt line gets enabled with enable_percpu_nmi().
    2712             :  *
    2713             :  *      As a CPU local operation, this should be called from non-preemptible
    2714             :  *      context.
    2715             :  *
    2716             :  *      If the interrupt line cannot be used to deliver NMIs, function
    2717             :  *      will fail returning a negative value.
    2718             :  */
    2719           0 : int prepare_percpu_nmi(unsigned int irq)
    2720             : {
    2721             :         unsigned long flags;
    2722             :         struct irq_desc *desc;
    2723           0 :         int ret = 0;
    2724             : 
    2725           0 :         WARN_ON(preemptible());
    2726             : 
    2727           0 :         desc = irq_get_desc_lock(irq, &flags,
    2728             :                                  IRQ_GET_DESC_CHECK_PERCPU);
    2729           0 :         if (!desc)
    2730             :                 return -EINVAL;
    2731             : 
    2732           0 :         if (WARN(!(desc->istate & IRQS_NMI),
    2733             :                  KERN_ERR "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n",
    2734             :                  irq)) {
    2735             :                 ret = -EINVAL;
    2736             :                 goto out;
    2737             :         }
    2738             : 
    2739           0 :         ret = irq_nmi_setup(desc);
    2740           0 :         if (ret) {
    2741           0 :                 pr_err("Failed to setup NMI delivery: irq %u\n", irq);
    2742           0 :                 goto out;
    2743             :         }
    2744             : 
    2745             : out:
    2746           0 :         irq_put_desc_unlock(desc, flags);
    2747           0 :         return ret;
    2748             : }
    2749             : 
    2750             : /**
    2751             :  *      teardown_percpu_nmi - undoes NMI setup of IRQ line
    2752             :  *      @irq: Interrupt line from which CPU local NMI configuration should be
    2753             :  *            removed
    2754             :  *
    2755             :  *      This call undoes the setup done by prepare_percpu_nmi().
    2756             :  *
    2757             :  *      IRQ line should not be enabled for the current CPU.
    2758             :  *
    2759             :  *      As a CPU local operation, this should be called from non-preemptible
    2760             :  *      context.
    2761             :  */
    2762           0 : void teardown_percpu_nmi(unsigned int irq)
    2763             : {
    2764             :         unsigned long flags;
    2765             :         struct irq_desc *desc;
    2766             : 
    2767           0 :         WARN_ON(preemptible());
    2768             : 
    2769           0 :         desc = irq_get_desc_lock(irq, &flags,
    2770             :                                  IRQ_GET_DESC_CHECK_PERCPU);
    2771           0 :         if (!desc)
    2772           0 :                 return;
    2773             : 
    2774           0 :         if (WARN_ON(!(desc->istate & IRQS_NMI)))
    2775             :                 goto out;
    2776             : 
    2777             :         irq_nmi_teardown(desc);
    2778             : out:
    2779           0 :         irq_put_desc_unlock(desc, flags);
    2780             : }
    2781             : 
    2782           0 : int __irq_get_irqchip_state(struct irq_data *data, enum irqchip_irq_state which,
    2783             :                             bool *state)
    2784             : {
    2785             :         struct irq_chip *chip;
    2786           0 :         int err = -EINVAL;
    2787             : 
    2788             :         do {
    2789           0 :                 chip = irq_data_get_irq_chip(data);
    2790           0 :                 if (WARN_ON_ONCE(!chip))
    2791             :                         return -ENODEV;
    2792           0 :                 if (chip->irq_get_irqchip_state)
    2793             :                         break;
    2794             : #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
    2795           0 :                 data = data->parent_data;
    2796             : #else
    2797             :                 data = NULL;
    2798             : #endif
    2799           0 :         } while (data);
    2800             : 
    2801           0 :         if (data)
    2802           0 :                 err = chip->irq_get_irqchip_state(data, which, state);
    2803             :         return err;
    2804             : }
    2805             : 
    2806             : /**
    2807             :  *      irq_get_irqchip_state - returns the irqchip state of a interrupt.
    2808             :  *      @irq: Interrupt line that is forwarded to a VM
    2809             :  *      @which: One of IRQCHIP_STATE_* the caller wants to know about
    2810             :  *      @state: a pointer to a boolean where the state is to be stored
    2811             :  *
    2812             :  *      This call snapshots the internal irqchip state of an
    2813             :  *      interrupt, returning into @state the bit corresponding to
    2814             :  *      stage @which
    2815             :  *
    2816             :  *      This function should be called with preemption disabled if the
    2817             :  *      interrupt controller has per-cpu registers.
    2818             :  */
    2819           0 : int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
    2820             :                           bool *state)
    2821             : {
    2822             :         struct irq_desc *desc;
    2823             :         struct irq_data *data;
    2824             :         unsigned long flags;
    2825           0 :         int err = -EINVAL;
    2826             : 
    2827           0 :         desc = irq_get_desc_buslock(irq, &flags, 0);
    2828           0 :         if (!desc)
    2829             :                 return err;
    2830             : 
    2831           0 :         data = irq_desc_get_irq_data(desc);
    2832             : 
    2833           0 :         err = __irq_get_irqchip_state(data, which, state);
    2834             : 
    2835           0 :         irq_put_desc_busunlock(desc, flags);
    2836           0 :         return err;
    2837             : }
    2838             : EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
    2839             : 
    2840             : /**
    2841             :  *      irq_set_irqchip_state - set the state of a forwarded interrupt.
    2842             :  *      @irq: Interrupt line that is forwarded to a VM
    2843             :  *      @which: State to be restored (one of IRQCHIP_STATE_*)
    2844             :  *      @val: Value corresponding to @which
    2845             :  *
    2846             :  *      This call sets the internal irqchip state of an interrupt,
    2847             :  *      depending on the value of @which.
    2848             :  *
    2849             :  *      This function should be called with migration disabled if the
    2850             :  *      interrupt controller has per-cpu registers.
    2851             :  */
    2852           0 : int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
    2853             :                           bool val)
    2854             : {
    2855             :         struct irq_desc *desc;
    2856             :         struct irq_data *data;
    2857             :         struct irq_chip *chip;
    2858             :         unsigned long flags;
    2859           0 :         int err = -EINVAL;
    2860             : 
    2861           0 :         desc = irq_get_desc_buslock(irq, &flags, 0);
    2862           0 :         if (!desc)
    2863             :                 return err;
    2864             : 
    2865           0 :         data = irq_desc_get_irq_data(desc);
    2866             : 
    2867             :         do {
    2868           0 :                 chip = irq_data_get_irq_chip(data);
    2869           0 :                 if (WARN_ON_ONCE(!chip)) {
    2870             :                         err = -ENODEV;
    2871             :                         goto out_unlock;
    2872             :                 }
    2873           0 :                 if (chip->irq_set_irqchip_state)
    2874             :                         break;
    2875             : #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
    2876           0 :                 data = data->parent_data;
    2877             : #else
    2878             :                 data = NULL;
    2879             : #endif
    2880           0 :         } while (data);
    2881             : 
    2882           0 :         if (data)
    2883           0 :                 err = chip->irq_set_irqchip_state(data, which, val);
    2884             : 
    2885             : out_unlock:
    2886           0 :         irq_put_desc_busunlock(desc, flags);
    2887           0 :         return err;
    2888             : }
    2889             : EXPORT_SYMBOL_GPL(irq_set_irqchip_state);
    2890             : 
    2891             : /**
    2892             :  * irq_has_action - Check whether an interrupt is requested
    2893             :  * @irq:        The linux irq number
    2894             :  *
    2895             :  * Returns: A snapshot of the current state
    2896             :  */
    2897           0 : bool irq_has_action(unsigned int irq)
    2898             : {
    2899             :         bool res;
    2900             : 
    2901             :         rcu_read_lock();
    2902           0 :         res = irq_desc_has_action(irq_to_desc(irq));
    2903             :         rcu_read_unlock();
    2904           0 :         return res;
    2905             : }
    2906             : EXPORT_SYMBOL_GPL(irq_has_action);
    2907             : 
    2908             : /**
    2909             :  * irq_check_status_bit - Check whether bits in the irq descriptor status are set
    2910             :  * @irq:        The linux irq number
    2911             :  * @bitmask:    The bitmask to evaluate
    2912             :  *
    2913             :  * Returns: True if one of the bits in @bitmask is set
    2914             :  */
    2915           0 : bool irq_check_status_bit(unsigned int irq, unsigned int bitmask)
    2916             : {
    2917             :         struct irq_desc *desc;
    2918           0 :         bool res = false;
    2919             : 
    2920             :         rcu_read_lock();
    2921           0 :         desc = irq_to_desc(irq);
    2922           0 :         if (desc)
    2923           0 :                 res = !!(desc->status_use_accessors & bitmask);
    2924             :         rcu_read_unlock();
    2925           0 :         return res;
    2926             : }
    2927             : EXPORT_SYMBOL_GPL(irq_check_status_bit);

Generated by: LCOV version 1.14