2 * linux/kernel/irq/manage.c
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006 Thomas Gleixner
7 * This file contains driver APIs to the irq subsystem.
10 #define pr_fmt(fmt) "genirq: " fmt
12 #include <linux/irq.h>
13 #include <linux/kthread.h>
14 #include <linux/module.h>
15 #include <linux/random.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/sched/rt.h>
20 #include <linux/task_work.h>
22 #include "internals.h"
24 #ifdef CONFIG_IRQ_FORCED_THREADING
25 # ifndef CONFIG_PREEMPT_RT_BASE
26 __read_mostly bool force_irqthreads;
28 static int __init setup_forced_irqthreads(char *arg)
30 force_irqthreads = true;
33 early_param("threadirqs", setup_forced_irqthreads);
37 static void __synchronize_hardirq(struct irq_desc *desc)
45 * Wait until we're out of the critical section. This might
46 * give the wrong answer due to the lack of memory barriers.
48 while (irqd_irq_inprogress(&desc->irq_data))
51 /* Ok, that indicated we're done: double-check carefully. */
52 raw_spin_lock_irqsave(&desc->lock, flags);
53 inprogress = irqd_irq_inprogress(&desc->irq_data);
54 raw_spin_unlock_irqrestore(&desc->lock, flags);
56 /* Oops, that failed? */
61 * synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs)
62 * @irq: interrupt number to wait for
64 * This function waits for any pending hard IRQ handlers for this
65 * interrupt to complete before returning. If you use this
66 * function while holding a resource the IRQ handler may need you
67 * will deadlock. It does not take associated threaded handlers
70 * Do not use this for shutdown scenarios where you must be sure
71 * that all parts (hardirq and threaded handler) have completed.
73 * Returns: false if a threaded handler is active.
75 * This function may be called - with care - from IRQ context.
77 bool synchronize_hardirq(unsigned int irq)
79 struct irq_desc *desc = irq_to_desc(irq);
82 __synchronize_hardirq(desc);
83 return !atomic_read(&desc->threads_active);
88 EXPORT_SYMBOL(synchronize_hardirq);
91 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
92 * @irq: interrupt number to wait for
94 * This function waits for any pending IRQ handlers for this interrupt
95 * to complete before returning. If you use this function while
96 * holding a resource the IRQ handler may need you will deadlock.
98 * This function may be called - with care - from IRQ context.
100 void synchronize_irq(unsigned int irq)
102 struct irq_desc *desc = irq_to_desc(irq);
105 __synchronize_hardirq(desc);
107 * We made sure that no hardirq handler is
108 * running. Now verify that no threaded handlers are
111 wait_event(desc->wait_for_threads,
112 !atomic_read(&desc->threads_active));
115 EXPORT_SYMBOL(synchronize_irq);
118 cpumask_var_t irq_default_affinity;
121 * irq_can_set_affinity - Check if the affinity of a given irq can be set
122 * @irq: Interrupt to check
125 int irq_can_set_affinity(unsigned int irq)
127 struct irq_desc *desc = irq_to_desc(irq);
129 if (!desc || !irqd_can_balance(&desc->irq_data) ||
130 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
137 * irq_set_thread_affinity - Notify irq threads to adjust affinity
138 * @desc: irq descriptor which has affitnity changed
140 * We just set IRQTF_AFFINITY and delegate the affinity setting
141 * to the interrupt thread itself. We can not call
142 * set_cpus_allowed_ptr() here as we hold desc->lock and this
143 * code can be called from hard interrupt context.
145 void irq_set_thread_affinity(struct irq_desc *desc)
147 struct irqaction *action = desc->action;
151 set_bit(IRQTF_AFFINITY, &action->thread_flags);
152 action = action->next;
156 #ifdef CONFIG_GENERIC_PENDING_IRQ
157 static inline bool irq_can_move_pcntxt(struct irq_data *data)
159 return irqd_can_move_in_process_context(data);
161 static inline bool irq_move_pending(struct irq_data *data)
163 return irqd_is_setaffinity_pending(data);
166 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
168 cpumask_copy(desc->pending_mask, mask);
171 irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
173 cpumask_copy(mask, desc->pending_mask);
176 static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
177 static inline bool irq_move_pending(struct irq_data *data) { return false; }
179 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
181 irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
184 #ifdef CONFIG_PREEMPT_RT_FULL
185 static void _irq_affinity_notify(struct irq_affinity_notify *notify);
186 static struct task_struct *set_affinity_helper;
187 static LIST_HEAD(affinity_list);
188 static DEFINE_RAW_SPINLOCK(affinity_list_lock);
190 static int set_affinity_thread(void *unused)
193 struct irq_affinity_notify *notify;
196 set_current_state(TASK_INTERRUPTIBLE);
198 raw_spin_lock_irq(&affinity_list_lock);
199 empty = list_empty(&affinity_list);
200 raw_spin_unlock_irq(&affinity_list_lock);
204 if (kthread_should_stop())
206 set_current_state(TASK_RUNNING);
210 raw_spin_lock_irq(&affinity_list_lock);
211 if (!list_empty(&affinity_list)) {
212 notify = list_first_entry(&affinity_list,
213 struct irq_affinity_notify, list);
214 list_del_init(¬ify->list);
216 raw_spin_unlock_irq(&affinity_list_lock);
220 _irq_affinity_notify(notify);
226 static void init_helper_thread(void)
228 if (set_affinity_helper)
230 set_affinity_helper = kthread_run(set_affinity_thread, NULL,
232 WARN_ON(IS_ERR(set_affinity_helper));
236 static inline void init_helper_thread(void) { }
240 int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
243 struct irq_desc *desc = irq_data_to_desc(data);
244 struct irq_chip *chip = irq_data_get_irq_chip(data);
247 ret = chip->irq_set_affinity(data, mask, force);
249 case IRQ_SET_MASK_OK:
250 case IRQ_SET_MASK_OK_DONE:
251 cpumask_copy(data->affinity, mask);
252 case IRQ_SET_MASK_OK_NOCOPY:
253 irq_set_thread_affinity(desc);
260 int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
263 struct irq_chip *chip = irq_data_get_irq_chip(data);
264 struct irq_desc *desc = irq_data_to_desc(data);
267 if (!chip || !chip->irq_set_affinity)
270 if (irq_can_move_pcntxt(data)) {
271 ret = irq_do_set_affinity(data, mask, force);
273 irqd_set_move_pending(data);
274 irq_copy_pending(desc, mask);
277 if (desc->affinity_notify) {
278 kref_get(&desc->affinity_notify->kref);
280 #ifdef CONFIG_PREEMPT_RT_FULL
281 raw_spin_lock(&affinity_list_lock);
282 if (list_empty(&desc->affinity_notify->list))
283 list_add_tail(&affinity_list,
284 &desc->affinity_notify->list);
285 raw_spin_unlock(&affinity_list_lock);
286 wake_up_process(set_affinity_helper);
288 schedule_work(&desc->affinity_notify->work);
291 irqd_set(data, IRQD_AFFINITY_SET);
296 int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
298 struct irq_desc *desc = irq_to_desc(irq);
305 raw_spin_lock_irqsave(&desc->lock, flags);
306 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
307 raw_spin_unlock_irqrestore(&desc->lock, flags);
311 int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
314 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
318 desc->affinity_hint = m;
319 irq_put_desc_unlock(desc, flags);
320 /* set the initial affinity to prevent every interrupt being on CPU0 */
322 __irq_set_affinity(irq, m, false);
325 EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
327 static void _irq_affinity_notify(struct irq_affinity_notify *notify)
329 struct irq_desc *desc = irq_to_desc(notify->irq);
330 cpumask_var_t cpumask;
333 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
336 raw_spin_lock_irqsave(&desc->lock, flags);
337 if (irq_move_pending(&desc->irq_data))
338 irq_get_pending(cpumask, desc);
340 cpumask_copy(cpumask, desc->irq_data.affinity);
341 raw_spin_unlock_irqrestore(&desc->lock, flags);
343 notify->notify(notify, cpumask);
345 free_cpumask_var(cpumask);
347 kref_put(¬ify->kref, notify->release);
350 static void irq_affinity_notify(struct work_struct *work)
352 struct irq_affinity_notify *notify =
353 container_of(work, struct irq_affinity_notify, work);
354 _irq_affinity_notify(notify);
358 * irq_set_affinity_notifier - control notification of IRQ affinity changes
359 * @irq: Interrupt for which to enable/disable notification
360 * @notify: Context for notification, or %NULL to disable
361 * notification. Function pointers must be initialised;
362 * the other fields will be initialised by this function.
364 * Must be called in process context. Notification may only be enabled
365 * after the IRQ is allocated and must be disabled before the IRQ is
366 * freed using free_irq().
369 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
371 struct irq_desc *desc = irq_to_desc(irq);
372 struct irq_affinity_notify *old_notify;
375 /* The release function is promised process context */
381 /* Complete initialisation of *notify */
384 kref_init(¬ify->kref);
385 INIT_WORK(¬ify->work, irq_affinity_notify);
386 INIT_LIST_HEAD(¬ify->list);
387 init_helper_thread();
390 raw_spin_lock_irqsave(&desc->lock, flags);
391 old_notify = desc->affinity_notify;
392 desc->affinity_notify = notify;
393 raw_spin_unlock_irqrestore(&desc->lock, flags);
396 kref_put(&old_notify->kref, old_notify->release);
400 EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
402 #ifndef CONFIG_AUTO_IRQ_AFFINITY
404 * Generic version of the affinity autoselector.
407 setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
409 struct cpumask *set = irq_default_affinity;
410 int node = desc->irq_data.node;
412 /* Excludes PER_CPU and NO_BALANCE interrupts */
413 if (!irq_can_set_affinity(irq))
417 * Preserve an userspace affinity setup, but make sure that
418 * one of the targets is online.
420 if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
421 if (cpumask_intersects(desc->irq_data.affinity,
423 set = desc->irq_data.affinity;
425 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
428 cpumask_and(mask, cpu_online_mask, set);
429 if (node != NUMA_NO_NODE) {
430 const struct cpumask *nodemask = cpumask_of_node(node);
432 /* make sure at least one of the cpus in nodemask is online */
433 if (cpumask_intersects(mask, nodemask))
434 cpumask_and(mask, mask, nodemask);
436 irq_do_set_affinity(&desc->irq_data, mask, false);
441 setup_affinity(unsigned int irq, struct irq_desc *d, struct cpumask *mask)
443 return irq_select_affinity(irq);
448 * Called when affinity is set via /proc/irq
450 int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
452 struct irq_desc *desc = irq_to_desc(irq);
456 raw_spin_lock_irqsave(&desc->lock, flags);
457 ret = setup_affinity(irq, desc, mask);
458 raw_spin_unlock_irqrestore(&desc->lock, flags);
464 setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
470 void __disable_irq(struct irq_desc *desc, unsigned int irq)
476 static int __disable_irq_nosync(unsigned int irq)
479 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
483 __disable_irq(desc, irq);
484 irq_put_desc_busunlock(desc, flags);
489 * disable_irq_nosync - disable an irq without waiting
490 * @irq: Interrupt to disable
492 * Disable the selected interrupt line. Disables and Enables are
494 * Unlike disable_irq(), this function does not ensure existing
495 * instances of the IRQ handler have completed before returning.
497 * This function may be called from IRQ context.
499 void disable_irq_nosync(unsigned int irq)
501 __disable_irq_nosync(irq);
503 EXPORT_SYMBOL(disable_irq_nosync);
506 * disable_irq - disable an irq and wait for completion
507 * @irq: Interrupt to disable
509 * Disable the selected interrupt line. Enables and Disables are
511 * This function waits for any pending IRQ handlers for this interrupt
512 * to complete before returning. If you use this function while
513 * holding a resource the IRQ handler may need you will deadlock.
515 * This function may be called - with care - from IRQ context.
517 void disable_irq(unsigned int irq)
519 if (!__disable_irq_nosync(irq))
520 synchronize_irq(irq);
522 EXPORT_SYMBOL(disable_irq);
525 * disable_hardirq - disables an irq and waits for hardirq completion
526 * @irq: Interrupt to disable
528 * Disable the selected interrupt line. Enables and Disables are
530 * This function waits for any pending hard IRQ handlers for this
531 * interrupt to complete before returning. If you use this function while
532 * holding a resource the hard IRQ handler may need you will deadlock.
534 * When used to optimistically disable an interrupt from atomic context
535 * the return value must be checked.
537 * Returns: false if a threaded handler is active.
539 * This function may be called - with care - from IRQ context.
541 bool disable_hardirq(unsigned int irq)
543 if (!__disable_irq_nosync(irq))
544 return synchronize_hardirq(irq);
548 EXPORT_SYMBOL_GPL(disable_hardirq);
550 void __enable_irq(struct irq_desc *desc, unsigned int irq)
552 switch (desc->depth) {
555 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
558 if (desc->istate & IRQS_SUSPENDED)
560 /* Prevent probing on this irq: */
561 irq_settings_set_noprobe(desc);
563 check_irq_resend(desc, irq);
572 * enable_irq - enable handling of an irq
573 * @irq: Interrupt to enable
575 * Undoes the effect of one call to disable_irq(). If this
576 * matches the last disable, processing of interrupts on this
577 * IRQ line is re-enabled.
579 * This function may be called from IRQ context only when
580 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
582 void enable_irq(unsigned int irq)
585 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
589 if (WARN(!desc->irq_data.chip,
590 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
593 __enable_irq(desc, irq);
595 irq_put_desc_busunlock(desc, flags);
597 EXPORT_SYMBOL(enable_irq);
599 static int set_irq_wake_real(unsigned int irq, unsigned int on)
601 struct irq_desc *desc = irq_to_desc(irq);
604 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
607 if (desc->irq_data.chip->irq_set_wake)
608 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
614 * irq_set_irq_wake - control irq power management wakeup
615 * @irq: interrupt to control
616 * @on: enable/disable power management wakeup
618 * Enable/disable power management wakeup mode, which is
619 * disabled by default. Enables and disables must match,
620 * just as they match for non-wakeup mode support.
622 * Wakeup mode lets this IRQ wake the system from sleep
623 * states like "suspend to RAM".
625 int irq_set_irq_wake(unsigned int irq, unsigned int on)
628 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
634 /* wakeup-capable irqs can be shared between drivers that
635 * don't need to have the same sleep mode behaviors.
638 if (desc->wake_depth++ == 0) {
639 ret = set_irq_wake_real(irq, on);
641 desc->wake_depth = 0;
643 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
646 if (desc->wake_depth == 0) {
647 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
648 } else if (--desc->wake_depth == 0) {
649 ret = set_irq_wake_real(irq, on);
651 desc->wake_depth = 1;
653 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
656 irq_put_desc_busunlock(desc, flags);
659 EXPORT_SYMBOL(irq_set_irq_wake);
662 * Internal function that tells the architecture code whether a
663 * particular irq has been exclusively allocated or is available
666 int can_request_irq(unsigned int irq, unsigned long irqflags)
669 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
675 if (irq_settings_can_request(desc)) {
677 irqflags & desc->action->flags & IRQF_SHARED)
680 irq_put_desc_unlock(desc, flags);
684 int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
687 struct irq_chip *chip = desc->irq_data.chip;
690 if (!chip || !chip->irq_set_type) {
692 * IRQF_TRIGGER_* but the PIC does not support multiple
695 pr_debug("No set_type function for IRQ %d (%s)\n", irq,
696 chip ? (chip->name ? : "unknown") : "unknown");
700 flags &= IRQ_TYPE_SENSE_MASK;
702 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
703 if (!irqd_irq_masked(&desc->irq_data))
705 if (!irqd_irq_disabled(&desc->irq_data))
709 /* caller masked out all except trigger mode flags */
710 ret = chip->irq_set_type(&desc->irq_data, flags);
713 case IRQ_SET_MASK_OK:
714 case IRQ_SET_MASK_OK_DONE:
715 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
716 irqd_set(&desc->irq_data, flags);
718 case IRQ_SET_MASK_OK_NOCOPY:
719 flags = irqd_get_trigger_type(&desc->irq_data);
720 irq_settings_set_trigger_mask(desc, flags);
721 irqd_clear(&desc->irq_data, IRQD_LEVEL);
722 irq_settings_clr_level(desc);
723 if (flags & IRQ_TYPE_LEVEL_MASK) {
724 irq_settings_set_level(desc);
725 irqd_set(&desc->irq_data, IRQD_LEVEL);
731 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
732 flags, irq, chip->irq_set_type);
739 #ifdef CONFIG_HARDIRQS_SW_RESEND
740 int irq_set_parent(int irq, int parent_irq)
743 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
748 desc->parent_irq = parent_irq;
750 irq_put_desc_unlock(desc, flags);
756 * Default primary interrupt handler for threaded interrupts. Is
757 * assigned as primary handler when request_threaded_irq is called
758 * with handler == NULL. Useful for oneshot interrupts.
760 static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
762 return IRQ_WAKE_THREAD;
766 * Primary handler for nested threaded interrupts. Should never be
769 static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
771 WARN(1, "Primary handler called for nested irq %d\n", irq);
775 static int irq_wait_for_interrupt(struct irqaction *action)
777 set_current_state(TASK_INTERRUPTIBLE);
779 while (!kthread_should_stop()) {
781 if (test_and_clear_bit(IRQTF_RUNTHREAD,
782 &action->thread_flags)) {
783 __set_current_state(TASK_RUNNING);
787 set_current_state(TASK_INTERRUPTIBLE);
789 __set_current_state(TASK_RUNNING);
794 * Oneshot interrupts keep the irq line masked until the threaded
795 * handler finished. unmask if the interrupt has not been disabled and
798 static void irq_finalize_oneshot(struct irq_desc *desc,
799 struct irqaction *action)
801 if (!(desc->istate & IRQS_ONESHOT))
805 raw_spin_lock_irq(&desc->lock);
808 * Implausible though it may be we need to protect us against
809 * the following scenario:
811 * The thread is faster done than the hard interrupt handler
812 * on the other CPU. If we unmask the irq line then the
813 * interrupt can come in again and masks the line, leaves due
814 * to IRQS_INPROGRESS and the irq line is masked forever.
816 * This also serializes the state of shared oneshot handlers
817 * versus "desc->threads_onehsot |= action->thread_mask;" in
818 * irq_wake_thread(). See the comment there which explains the
821 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
822 raw_spin_unlock_irq(&desc->lock);
823 chip_bus_sync_unlock(desc);
829 * Now check again, whether the thread should run. Otherwise
830 * we would clear the threads_oneshot bit of this thread which
833 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
836 desc->threads_oneshot &= ~action->thread_mask;
838 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
839 irqd_irq_masked(&desc->irq_data))
840 unmask_threaded_irq(desc);
843 raw_spin_unlock_irq(&desc->lock);
844 chip_bus_sync_unlock(desc);
849 * Check whether we need to change the affinity of the interrupt thread.
852 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
857 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
861 * In case we are out of memory we set IRQTF_AFFINITY again and
862 * try again next time
864 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
865 set_bit(IRQTF_AFFINITY, &action->thread_flags);
869 raw_spin_lock_irq(&desc->lock);
871 * This code is triggered unconditionally. Check the affinity
872 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
874 if (desc->irq_data.affinity)
875 cpumask_copy(mask, desc->irq_data.affinity);
878 raw_spin_unlock_irq(&desc->lock);
881 set_cpus_allowed_ptr(current, mask);
882 free_cpumask_var(mask);
886 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
890 * Interrupts which are not explicitely requested as threaded
891 * interrupts rely on the implicit bh/preempt disable of the hard irq
892 * context. So we need to disable bh here to avoid deadlocks and other
896 irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
901 ret = action->thread_fn(action->irq, action->dev_id);
902 irq_finalize_oneshot(desc, action);
904 * Interrupts which have real time requirements can be set up
905 * to avoid softirq processing in the thread handler. This is
906 * safe as these interrupts do not raise soft interrupts.
908 if (irq_settings_no_softirq_call(desc))
916 * Interrupts explicitly requested as threaded interrupts want to be
917 * preemtible - many of them need to sleep and wait for slow busses to
920 static irqreturn_t irq_thread_fn(struct irq_desc *desc,
921 struct irqaction *action)
925 ret = action->thread_fn(action->irq, action->dev_id);
926 irq_finalize_oneshot(desc, action);
930 static void wake_threads_waitq(struct irq_desc *desc)
932 if (atomic_dec_and_test(&desc->threads_active))
933 wake_up(&desc->wait_for_threads);
936 static void irq_thread_dtor(struct callback_head *unused)
938 struct task_struct *tsk = current;
939 struct irq_desc *desc;
940 struct irqaction *action;
942 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
945 action = kthread_data(tsk);
947 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
948 tsk->comm, tsk->pid, action->irq);
951 desc = irq_to_desc(action->irq);
953 * If IRQTF_RUNTHREAD is set, we need to decrement
954 * desc->threads_active and wake possible waiters.
956 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
957 wake_threads_waitq(desc);
959 /* Prevent a stale desc->threads_oneshot */
960 irq_finalize_oneshot(desc, action);
964 * Interrupt handler thread
966 static int irq_thread(void *data)
968 struct callback_head on_exit_work;
969 struct irqaction *action = data;
970 struct irq_desc *desc = irq_to_desc(action->irq);
971 irqreturn_t (*handler_fn)(struct irq_desc *desc,
972 struct irqaction *action);
974 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
975 &action->thread_flags))
976 handler_fn = irq_forced_thread_fn;
978 handler_fn = irq_thread_fn;
980 init_task_work(&on_exit_work, irq_thread_dtor);
981 task_work_add(current, &on_exit_work, false);
983 irq_thread_check_affinity(desc, action);
985 while (!irq_wait_for_interrupt(action)) {
986 irqreturn_t action_ret;
988 irq_thread_check_affinity(desc, action);
990 action_ret = handler_fn(desc, action);
991 if (action_ret == IRQ_HANDLED)
992 atomic_inc(&desc->threads_handled);
994 #ifdef CONFIG_PREEMPT_RT_FULL
996 add_interrupt_randomness(action->irq, 0,
997 desc->random_ip ^ (unsigned long) action);
1000 wake_threads_waitq(desc);
1004 * This is the regular exit path. __free_irq() is stopping the
1005 * thread via kthread_stop() after calling
1006 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
1007 * oneshot mask bit can be set. We cannot verify that as we
1008 * cannot touch the oneshot mask at this point anymore as
1009 * __setup_irq() might have given out currents thread_mask
1012 task_work_cancel(current, irq_thread_dtor);
1017 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1018 * @irq: Interrupt line
1019 * @dev_id: Device identity for which the thread should be woken
1022 void irq_wake_thread(unsigned int irq, void *dev_id)
1024 struct irq_desc *desc = irq_to_desc(irq);
1025 struct irqaction *action;
1026 unsigned long flags;
1028 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1031 raw_spin_lock_irqsave(&desc->lock, flags);
1032 for (action = desc->action; action; action = action->next) {
1033 if (action->dev_id == dev_id) {
1035 __irq_wake_thread(desc, action);
1039 raw_spin_unlock_irqrestore(&desc->lock, flags);
1041 EXPORT_SYMBOL_GPL(irq_wake_thread);
1043 static void irq_setup_forced_threading(struct irqaction *new)
1045 if (!force_irqthreads)
1047 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1050 new->flags |= IRQF_ONESHOT;
1052 if (!new->thread_fn) {
1053 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1054 new->thread_fn = new->handler;
1055 new->handler = irq_default_primary_handler;
1059 static int irq_request_resources(struct irq_desc *desc)
1061 struct irq_data *d = &desc->irq_data;
1062 struct irq_chip *c = d->chip;
1064 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1067 static void irq_release_resources(struct irq_desc *desc)
1069 struct irq_data *d = &desc->irq_data;
1070 struct irq_chip *c = d->chip;
1072 if (c->irq_release_resources)
1073 c->irq_release_resources(d);
1077 * Internal function to register an irqaction - typically used to
1078 * allocate special interrupts that are part of the architecture.
1081 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1083 struct irqaction *old, **old_ptr;
1084 unsigned long flags, thread_mask = 0;
1085 int ret, nested, shared = 0;
1091 if (desc->irq_data.chip == &no_irq_chip)
1093 if (!try_module_get(desc->owner))
1097 * Check whether the interrupt nests into another interrupt
1100 nested = irq_settings_is_nested_thread(desc);
1102 if (!new->thread_fn) {
1107 * Replace the primary handler which was provided from
1108 * the driver for non nested interrupt handling by the
1109 * dummy function which warns when called.
1111 new->handler = irq_nested_primary_handler;
1113 if (irq_settings_can_thread(desc))
1114 irq_setup_forced_threading(new);
1118 * Create a handler thread when a thread function is supplied
1119 * and the interrupt does not nest into another interrupt
1122 if (new->thread_fn && !nested) {
1123 struct task_struct *t;
1124 static const struct sched_param param = {
1125 .sched_priority = MAX_USER_RT_PRIO/2,
1128 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1135 sched_setscheduler_nocheck(t, SCHED_FIFO, ¶m);
1138 * We keep the reference to the task struct even if
1139 * the thread dies to avoid that the interrupt code
1140 * references an already freed task_struct.
1145 * Tell the thread to set its affinity. This is
1146 * important for shared interrupt handlers as we do
1147 * not invoke setup_affinity() for the secondary
1148 * handlers as everything is already set up. Even for
1149 * interrupts marked with IRQF_NO_BALANCE this is
1150 * correct as we want the thread to move to the cpu(s)
1151 * on which the requesting code placed the interrupt.
1153 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1156 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1162 * Drivers are often written to work w/o knowledge about the
1163 * underlying irq chip implementation, so a request for a
1164 * threaded irq without a primary hard irq context handler
1165 * requires the ONESHOT flag to be set. Some irq chips like
1166 * MSI based interrupts are per se one shot safe. Check the
1167 * chip flags, so we can avoid the unmask dance at the end of
1168 * the threaded handler for those.
1170 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1171 new->flags &= ~IRQF_ONESHOT;
1174 * The following block of code has to be executed atomically
1176 raw_spin_lock_irqsave(&desc->lock, flags);
1177 old_ptr = &desc->action;
1181 * Can't share interrupts unless both agree to and are
1182 * the same type (level, edge, polarity). So both flag
1183 * fields must have IRQF_SHARED set and the bits which
1184 * set the trigger type must match. Also all must
1187 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1188 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
1189 ((old->flags ^ new->flags) & IRQF_ONESHOT))
1192 /* All handlers must agree on per-cpuness */
1193 if ((old->flags & IRQF_PERCPU) !=
1194 (new->flags & IRQF_PERCPU))
1197 /* add new interrupt at end of irq queue */
1200 * Or all existing action->thread_mask bits,
1201 * so we can find the next zero bit for this
1204 thread_mask |= old->thread_mask;
1205 old_ptr = &old->next;
1212 * Setup the thread mask for this irqaction for ONESHOT. For
1213 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1214 * conditional in irq_wake_thread().
1216 if (new->flags & IRQF_ONESHOT) {
1218 * Unlikely to have 32 resp 64 irqs sharing one line,
1221 if (thread_mask == ~0UL) {
1226 * The thread_mask for the action is or'ed to
1227 * desc->thread_active to indicate that the
1228 * IRQF_ONESHOT thread handler has been woken, but not
1229 * yet finished. The bit is cleared when a thread
1230 * completes. When all threads of a shared interrupt
1231 * line have completed desc->threads_active becomes
1232 * zero and the interrupt line is unmasked. See
1233 * handle.c:irq_wake_thread() for further information.
1235 * If no thread is woken by primary (hard irq context)
1236 * interrupt handlers, then desc->threads_active is
1237 * also checked for zero to unmask the irq line in the
1238 * affected hard irq flow handlers
1239 * (handle_[fasteoi|level]_irq).
1241 * The new action gets the first zero bit of
1242 * thread_mask assigned. See the loop above which or's
1243 * all existing action->thread_mask bits.
1245 new->thread_mask = 1 << ffz(thread_mask);
1247 } else if (new->handler == irq_default_primary_handler &&
1248 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1250 * The interrupt was requested with handler = NULL, so
1251 * we use the default primary handler for it. But it
1252 * does not have the oneshot flag set. In combination
1253 * with level interrupts this is deadly, because the
1254 * default primary handler just wakes the thread, then
1255 * the irq lines is reenabled, but the device still
1256 * has the level irq asserted. Rinse and repeat....
1258 * While this works for edge type interrupts, we play
1259 * it safe and reject unconditionally because we can't
1260 * say for sure which type this interrupt really
1261 * has. The type flags are unreliable as the
1262 * underlying chip implementation can override them.
1264 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1271 ret = irq_request_resources(desc);
1273 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1274 new->name, irq, desc->irq_data.chip->name);
1278 init_waitqueue_head(&desc->wait_for_threads);
1280 /* Setup the type (level, edge polarity) if configured: */
1281 if (new->flags & IRQF_TRIGGER_MASK) {
1282 ret = __irq_set_trigger(desc, irq,
1283 new->flags & IRQF_TRIGGER_MASK);
1289 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1290 IRQS_ONESHOT | IRQS_WAITING);
1291 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1293 if (new->flags & IRQF_PERCPU) {
1294 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1295 irq_settings_set_per_cpu(desc);
1298 if (new->flags & IRQF_ONESHOT)
1299 desc->istate |= IRQS_ONESHOT;
1301 if (irq_settings_can_autoenable(desc))
1302 irq_startup(desc, true);
1304 /* Undo nested disables: */
1307 /* Exclude IRQ from balancing if requested */
1308 if (new->flags & IRQF_NOBALANCING) {
1309 irq_settings_set_no_balancing(desc);
1310 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1313 if (new->flags & IRQF_NO_SOFTIRQ_CALL)
1314 irq_settings_set_no_softirq_call(desc);
1316 /* Set default affinity mask once everything is setup */
1317 setup_affinity(irq, desc, mask);
1319 } else if (new->flags & IRQF_TRIGGER_MASK) {
1320 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1321 unsigned int omsk = irq_settings_get_trigger_mask(desc);
1324 /* hope the handler works with current trigger mode */
1325 pr_warning("irq %d uses trigger mode %u; requested %u\n",
1332 irq_pm_install_action(desc, new);
1334 /* Reset broken irq detection when installing new handler */
1335 desc->irq_count = 0;
1336 desc->irqs_unhandled = 0;
1339 * Check whether we disabled the irq via the spurious handler
1340 * before. Reenable it and give it another chance.
1342 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1343 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1344 __enable_irq(desc, irq);
1347 raw_spin_unlock_irqrestore(&desc->lock, flags);
1350 * Strictly no need to wake it up, but hung_task complains
1351 * when no hard interrupt wakes the thread up.
1354 wake_up_process(new->thread);
1356 register_irq_proc(irq, desc);
1358 register_handler_proc(irq, new);
1359 free_cpumask_var(mask);
1364 if (!(new->flags & IRQF_PROBE_SHARED)) {
1365 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1366 irq, new->flags, new->name, old->flags, old->name);
1367 #ifdef CONFIG_DEBUG_SHIRQ
1374 raw_spin_unlock_irqrestore(&desc->lock, flags);
1375 free_cpumask_var(mask);
1379 struct task_struct *t = new->thread;
1386 module_put(desc->owner);
1391 * setup_irq - setup an interrupt
1392 * @irq: Interrupt line to setup
1393 * @act: irqaction for the interrupt
1395 * Used to statically setup interrupts in the early boot process.
1397 int setup_irq(unsigned int irq, struct irqaction *act)
1400 struct irq_desc *desc = irq_to_desc(irq);
1402 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1404 chip_bus_lock(desc);
1405 retval = __setup_irq(irq, desc, act);
1406 chip_bus_sync_unlock(desc);
1410 EXPORT_SYMBOL_GPL(setup_irq);
1413 * Internal function to unregister an irqaction - used to free
1414 * regular and special interrupts that are part of the architecture.
1416 static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
1418 struct irq_desc *desc = irq_to_desc(irq);
1419 struct irqaction *action, **action_ptr;
1420 unsigned long flags;
1422 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1427 raw_spin_lock_irqsave(&desc->lock, flags);
1430 * There can be multiple actions per IRQ descriptor, find the right
1431 * one based on the dev_id:
1433 action_ptr = &desc->action;
1435 action = *action_ptr;
1438 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1439 raw_spin_unlock_irqrestore(&desc->lock, flags);
1444 if (action->dev_id == dev_id)
1446 action_ptr = &action->next;
1449 /* Found it - now remove it from the list of entries: */
1450 *action_ptr = action->next;
1452 irq_pm_remove_action(desc, action);
1454 /* If this was the last handler, shut down the IRQ line: */
1455 if (!desc->action) {
1457 irq_release_resources(desc);
1461 /* make sure affinity_hint is cleaned up */
1462 if (WARN_ON_ONCE(desc->affinity_hint))
1463 desc->affinity_hint = NULL;
1466 raw_spin_unlock_irqrestore(&desc->lock, flags);
1468 unregister_handler_proc(irq, action);
1470 /* Make sure it's not being used on another CPU: */
1471 synchronize_irq(irq);
1473 #ifdef CONFIG_DEBUG_SHIRQ
1475 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1476 * event to happen even now it's being freed, so let's make sure that
1477 * is so by doing an extra call to the handler ....
1479 * ( We do this after actually deregistering it, to make sure that a
1480 * 'real' IRQ doesn't run in * parallel with our fake. )
1482 if (action->flags & IRQF_SHARED) {
1483 local_irq_save(flags);
1484 action->handler(irq, dev_id);
1485 local_irq_restore(flags);
1489 if (action->thread) {
1490 kthread_stop(action->thread);
1491 put_task_struct(action->thread);
1494 module_put(desc->owner);
1499 * remove_irq - free an interrupt
1500 * @irq: Interrupt line to free
1501 * @act: irqaction for the interrupt
1503 * Used to remove interrupts statically setup by the early boot process.
1505 void remove_irq(unsigned int irq, struct irqaction *act)
1507 struct irq_desc *desc = irq_to_desc(irq);
1509 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1510 __free_irq(irq, act->dev_id);
1512 EXPORT_SYMBOL_GPL(remove_irq);
1515 * free_irq - free an interrupt allocated with request_irq
1516 * @irq: Interrupt line to free
1517 * @dev_id: Device identity to free
1519 * Remove an interrupt handler. The handler is removed and if the
1520 * interrupt line is no longer in use by any driver it is disabled.
1521 * On a shared IRQ the caller must ensure the interrupt is disabled
1522 * on the card it drives before calling this function. The function
1523 * does not return until any executing interrupts for this IRQ
1526 * This function must not be called from interrupt context.
1528 void free_irq(unsigned int irq, void *dev_id)
1530 struct irq_desc *desc = irq_to_desc(irq);
1532 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1536 if (WARN_ON(desc->affinity_notify))
1537 desc->affinity_notify = NULL;
1540 chip_bus_lock(desc);
1541 kfree(__free_irq(irq, dev_id));
1542 chip_bus_sync_unlock(desc);
1544 EXPORT_SYMBOL(free_irq);
1547 * request_threaded_irq - allocate an interrupt line
1548 * @irq: Interrupt line to allocate
1549 * @handler: Function to be called when the IRQ occurs.
1550 * Primary handler for threaded interrupts
1551 * If NULL and thread_fn != NULL the default
1552 * primary handler is installed
1553 * @thread_fn: Function called from the irq handler thread
1554 * If NULL, no irq thread is created
1555 * @irqflags: Interrupt type flags
1556 * @devname: An ascii name for the claiming device
1557 * @dev_id: A cookie passed back to the handler function
1559 * This call allocates interrupt resources and enables the
1560 * interrupt line and IRQ handling. From the point this
1561 * call is made your handler function may be invoked. Since
1562 * your handler function must clear any interrupt the board
1563 * raises, you must take care both to initialise your hardware
1564 * and to set up the interrupt handler in the right order.
1566 * If you want to set up a threaded irq handler for your device
1567 * then you need to supply @handler and @thread_fn. @handler is
1568 * still called in hard interrupt context and has to check
1569 * whether the interrupt originates from the device. If yes it
1570 * needs to disable the interrupt on the device and return
1571 * IRQ_WAKE_THREAD which will wake up the handler thread and run
1572 * @thread_fn. This split handler design is necessary to support
1573 * shared interrupts.
1575 * Dev_id must be globally unique. Normally the address of the
1576 * device data structure is used as the cookie. Since the handler
1577 * receives this value it makes sense to use it.
1579 * If your interrupt is shared you must pass a non NULL dev_id
1580 * as this is required when freeing the interrupt.
1584 * IRQF_SHARED Interrupt is shared
1585 * IRQF_TRIGGER_* Specify active edge(s) or level
1588 int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1589 irq_handler_t thread_fn, unsigned long irqflags,
1590 const char *devname, void *dev_id)
1592 struct irqaction *action;
1593 struct irq_desc *desc;
1597 * Sanity-check: shared interrupts must pass in a real dev-ID,
1598 * otherwise we'll have trouble later trying to figure out
1599 * which interrupt is which (messes up the interrupt freeing
1602 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1603 * it cannot be set along with IRQF_NO_SUSPEND.
1605 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1606 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1607 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
1610 desc = irq_to_desc(irq);
1614 if (!irq_settings_can_request(desc) ||
1615 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1621 handler = irq_default_primary_handler;
1624 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1628 action->handler = handler;
1629 action->thread_fn = thread_fn;
1630 action->flags = irqflags;
1631 action->name = devname;
1632 action->dev_id = dev_id;
1634 chip_bus_lock(desc);
1635 retval = __setup_irq(irq, desc, action);
1636 chip_bus_sync_unlock(desc);
1641 #ifdef CONFIG_DEBUG_SHIRQ_FIXME
1642 if (!retval && (irqflags & IRQF_SHARED)) {
1644 * It's a shared IRQ -- the driver ought to be prepared for it
1645 * to happen immediately, so let's make sure....
1646 * We disable the irq to make sure that a 'real' IRQ doesn't
1647 * run in parallel with our fake.
1649 unsigned long flags;
1652 local_irq_save(flags);
1654 handler(irq, dev_id);
1656 local_irq_restore(flags);
1662 EXPORT_SYMBOL(request_threaded_irq);
1665 * request_any_context_irq - allocate an interrupt line
1666 * @irq: Interrupt line to allocate
1667 * @handler: Function to be called when the IRQ occurs.
1668 * Threaded handler for threaded interrupts.
1669 * @flags: Interrupt type flags
1670 * @name: An ascii name for the claiming device
1671 * @dev_id: A cookie passed back to the handler function
1673 * This call allocates interrupt resources and enables the
1674 * interrupt line and IRQ handling. It selects either a
1675 * hardirq or threaded handling method depending on the
1678 * On failure, it returns a negative value. On success,
1679 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1681 int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1682 unsigned long flags, const char *name, void *dev_id)
1684 struct irq_desc *desc = irq_to_desc(irq);
1690 if (irq_settings_is_nested_thread(desc)) {
1691 ret = request_threaded_irq(irq, NULL, handler,
1692 flags, name, dev_id);
1693 return !ret ? IRQC_IS_NESTED : ret;
1696 ret = request_irq(irq, handler, flags, name, dev_id);
1697 return !ret ? IRQC_IS_HARDIRQ : ret;
1699 EXPORT_SYMBOL_GPL(request_any_context_irq);
1701 void enable_percpu_irq(unsigned int irq, unsigned int type)
1703 unsigned int cpu = smp_processor_id();
1704 unsigned long flags;
1705 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1710 type &= IRQ_TYPE_SENSE_MASK;
1711 if (type != IRQ_TYPE_NONE) {
1714 ret = __irq_set_trigger(desc, irq, type);
1717 WARN(1, "failed to set type for IRQ%d\n", irq);
1722 irq_percpu_enable(desc, cpu);
1724 irq_put_desc_unlock(desc, flags);
1726 EXPORT_SYMBOL_GPL(enable_percpu_irq);
1728 void disable_percpu_irq(unsigned int irq)
1730 unsigned int cpu = smp_processor_id();
1731 unsigned long flags;
1732 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1737 irq_percpu_disable(desc, cpu);
1738 irq_put_desc_unlock(desc, flags);
1740 EXPORT_SYMBOL_GPL(disable_percpu_irq);
1743 * Internal function to unregister a percpu irqaction.
1745 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1747 struct irq_desc *desc = irq_to_desc(irq);
1748 struct irqaction *action;
1749 unsigned long flags;
1751 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1756 raw_spin_lock_irqsave(&desc->lock, flags);
1758 action = desc->action;
1759 if (!action || action->percpu_dev_id != dev_id) {
1760 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1764 if (!cpumask_empty(desc->percpu_enabled)) {
1765 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1766 irq, cpumask_first(desc->percpu_enabled));
1770 /* Found it - now remove it from the list of entries: */
1771 desc->action = NULL;
1773 raw_spin_unlock_irqrestore(&desc->lock, flags);
1775 unregister_handler_proc(irq, action);
1777 module_put(desc->owner);
1781 raw_spin_unlock_irqrestore(&desc->lock, flags);
1786 * remove_percpu_irq - free a per-cpu interrupt
1787 * @irq: Interrupt line to free
1788 * @act: irqaction for the interrupt
1790 * Used to remove interrupts statically setup by the early boot process.
1792 void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1794 struct irq_desc *desc = irq_to_desc(irq);
1796 if (desc && irq_settings_is_per_cpu_devid(desc))
1797 __free_percpu_irq(irq, act->percpu_dev_id);
1801 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
1802 * @irq: Interrupt line to free
1803 * @dev_id: Device identity to free
1805 * Remove a percpu interrupt handler. The handler is removed, but
1806 * the interrupt line is not disabled. This must be done on each
1807 * CPU before calling this function. The function does not return
1808 * until any executing interrupts for this IRQ have completed.
1810 * This function must not be called from interrupt context.
1812 void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1814 struct irq_desc *desc = irq_to_desc(irq);
1816 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1819 chip_bus_lock(desc);
1820 kfree(__free_percpu_irq(irq, dev_id));
1821 chip_bus_sync_unlock(desc);
1825 * setup_percpu_irq - setup a per-cpu interrupt
1826 * @irq: Interrupt line to setup
1827 * @act: irqaction for the interrupt
1829 * Used to statically setup per-cpu interrupts in the early boot process.
1831 int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1833 struct irq_desc *desc = irq_to_desc(irq);
1836 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1838 chip_bus_lock(desc);
1839 retval = __setup_irq(irq, desc, act);
1840 chip_bus_sync_unlock(desc);
1846 * request_percpu_irq - allocate a percpu interrupt line
1847 * @irq: Interrupt line to allocate
1848 * @handler: Function to be called when the IRQ occurs.
1849 * @devname: An ascii name for the claiming device
1850 * @dev_id: A percpu cookie passed back to the handler function
1852 * This call allocates interrupt resources, but doesn't
1853 * automatically enable the interrupt. It has to be done on each
1854 * CPU using enable_percpu_irq().
1856 * Dev_id must be globally unique. It is a per-cpu variable, and
1857 * the handler gets called with the interrupted CPU's instance of
1860 int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1861 const char *devname, void __percpu *dev_id)
1863 struct irqaction *action;
1864 struct irq_desc *desc;
1870 desc = irq_to_desc(irq);
1871 if (!desc || !irq_settings_can_request(desc) ||
1872 !irq_settings_is_per_cpu_devid(desc))
1875 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1879 action->handler = handler;
1880 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
1881 action->name = devname;
1882 action->percpu_dev_id = dev_id;
1884 chip_bus_lock(desc);
1885 retval = __setup_irq(irq, desc, action);
1886 chip_bus_sync_unlock(desc);
1895 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
1896 * @irq: Interrupt line that is forwarded to a VM
1897 * @which: One of IRQCHIP_STATE_* the caller wants to know about
1898 * @state: a pointer to a boolean where the state is to be storeed
1900 * This call snapshots the internal irqchip state of an
1901 * interrupt, returning into @state the bit corresponding to
1904 * This function should be called with preemption disabled if the
1905 * interrupt controller has per-cpu registers.
1907 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
1910 struct irq_desc *desc;
1911 struct irq_data *data;
1912 struct irq_chip *chip;
1913 unsigned long flags;
1916 desc = irq_get_desc_buslock(irq, &flags, 0);
1920 data = irq_desc_get_irq_data(desc);
1923 chip = irq_data_get_irq_chip(data);
1924 if (chip->irq_get_irqchip_state)
1926 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1927 data = data->parent_data;
1934 err = chip->irq_get_irqchip_state(data, which, state);
1936 irq_put_desc_busunlock(desc, flags);
1941 * irq_set_irqchip_state - set the state of a forwarded interrupt.
1942 * @irq: Interrupt line that is forwarded to a VM
1943 * @which: State to be restored (one of IRQCHIP_STATE_*)
1944 * @val: Value corresponding to @which
1946 * This call sets the internal irqchip state of an interrupt,
1947 * depending on the value of @which.
1949 * This function should be called with preemption disabled if the
1950 * interrupt controller has per-cpu registers.
1952 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
1955 struct irq_desc *desc;
1956 struct irq_data *data;
1957 struct irq_chip *chip;
1958 unsigned long flags;
1961 desc = irq_get_desc_buslock(irq, &flags, 0);
1965 data = irq_desc_get_irq_data(desc);
1968 chip = irq_data_get_irq_chip(data);
1969 if (chip->irq_set_irqchip_state)
1971 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1972 data = data->parent_data;
1979 err = chip->irq_set_irqchip_state(data, which, val);
1981 irq_put_desc_busunlock(desc, flags);