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 irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
777 WARN(1, "Secondary action handler called for irq %d\n", irq);
781 static int irq_wait_for_interrupt(struct irqaction *action)
783 set_current_state(TASK_INTERRUPTIBLE);
785 while (!kthread_should_stop()) {
787 if (test_and_clear_bit(IRQTF_RUNTHREAD,
788 &action->thread_flags)) {
789 __set_current_state(TASK_RUNNING);
793 set_current_state(TASK_INTERRUPTIBLE);
795 __set_current_state(TASK_RUNNING);
800 * Oneshot interrupts keep the irq line masked until the threaded
801 * handler finished. unmask if the interrupt has not been disabled and
804 static void irq_finalize_oneshot(struct irq_desc *desc,
805 struct irqaction *action)
807 if (!(desc->istate & IRQS_ONESHOT) ||
808 action->handler == irq_forced_secondary_handler)
812 raw_spin_lock_irq(&desc->lock);
815 * Implausible though it may be we need to protect us against
816 * the following scenario:
818 * The thread is faster done than the hard interrupt handler
819 * on the other CPU. If we unmask the irq line then the
820 * interrupt can come in again and masks the line, leaves due
821 * to IRQS_INPROGRESS and the irq line is masked forever.
823 * This also serializes the state of shared oneshot handlers
824 * versus "desc->threads_onehsot |= action->thread_mask;" in
825 * irq_wake_thread(). See the comment there which explains the
828 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
829 raw_spin_unlock_irq(&desc->lock);
830 chip_bus_sync_unlock(desc);
836 * Now check again, whether the thread should run. Otherwise
837 * we would clear the threads_oneshot bit of this thread which
840 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
843 desc->threads_oneshot &= ~action->thread_mask;
845 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
846 irqd_irq_masked(&desc->irq_data))
847 unmask_threaded_irq(desc);
850 raw_spin_unlock_irq(&desc->lock);
851 chip_bus_sync_unlock(desc);
856 * Check whether we need to change the affinity of the interrupt thread.
859 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
864 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
868 * In case we are out of memory we set IRQTF_AFFINITY again and
869 * try again next time
871 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
872 set_bit(IRQTF_AFFINITY, &action->thread_flags);
876 raw_spin_lock_irq(&desc->lock);
878 * This code is triggered unconditionally. Check the affinity
879 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
881 if (desc->irq_data.affinity)
882 cpumask_copy(mask, desc->irq_data.affinity);
885 raw_spin_unlock_irq(&desc->lock);
888 set_cpus_allowed_ptr(current, mask);
889 free_cpumask_var(mask);
893 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
897 * Interrupts which are not explicitely requested as threaded
898 * interrupts rely on the implicit bh/preempt disable of the hard irq
899 * context. So we need to disable bh here to avoid deadlocks and other
903 irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
908 ret = action->thread_fn(action->irq, action->dev_id);
909 irq_finalize_oneshot(desc, action);
911 * Interrupts which have real time requirements can be set up
912 * to avoid softirq processing in the thread handler. This is
913 * safe as these interrupts do not raise soft interrupts.
915 if (irq_settings_no_softirq_call(desc))
923 * Interrupts explicitly requested as threaded interrupts want to be
924 * preemtible - many of them need to sleep and wait for slow busses to
927 static irqreturn_t irq_thread_fn(struct irq_desc *desc,
928 struct irqaction *action)
932 ret = action->thread_fn(action->irq, action->dev_id);
933 irq_finalize_oneshot(desc, action);
937 static void wake_threads_waitq(struct irq_desc *desc)
939 if (atomic_dec_and_test(&desc->threads_active))
940 wake_up(&desc->wait_for_threads);
943 static void irq_thread_dtor(struct callback_head *unused)
945 struct task_struct *tsk = current;
946 struct irq_desc *desc;
947 struct irqaction *action;
949 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
952 action = kthread_data(tsk);
954 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
955 tsk->comm, tsk->pid, action->irq);
958 desc = irq_to_desc(action->irq);
960 * If IRQTF_RUNTHREAD is set, we need to decrement
961 * desc->threads_active and wake possible waiters.
963 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
964 wake_threads_waitq(desc);
966 /* Prevent a stale desc->threads_oneshot */
967 irq_finalize_oneshot(desc, action);
970 static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
972 struct irqaction *secondary = action->secondary;
974 if (WARN_ON_ONCE(!secondary))
977 raw_spin_lock_irq(&desc->lock);
978 __irq_wake_thread(desc, secondary);
979 raw_spin_unlock_irq(&desc->lock);
983 * Interrupt handler thread
985 static int irq_thread(void *data)
987 struct callback_head on_exit_work;
988 struct irqaction *action = data;
989 struct irq_desc *desc = irq_to_desc(action->irq);
990 irqreturn_t (*handler_fn)(struct irq_desc *desc,
991 struct irqaction *action);
993 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
994 &action->thread_flags))
995 handler_fn = irq_forced_thread_fn;
997 handler_fn = irq_thread_fn;
999 init_task_work(&on_exit_work, irq_thread_dtor);
1000 task_work_add(current, &on_exit_work, false);
1002 irq_thread_check_affinity(desc, action);
1004 while (!irq_wait_for_interrupt(action)) {
1005 irqreturn_t action_ret;
1007 irq_thread_check_affinity(desc, action);
1009 action_ret = handler_fn(desc, action);
1010 if (action_ret == IRQ_HANDLED)
1011 atomic_inc(&desc->threads_handled);
1012 if (action_ret == IRQ_WAKE_THREAD)
1013 irq_wake_secondary(desc, action);
1015 #ifdef CONFIG_PREEMPT_RT_FULL
1017 add_interrupt_randomness(action->irq, 0,
1018 desc->random_ip ^ (unsigned long) action);
1021 wake_threads_waitq(desc);
1025 * This is the regular exit path. __free_irq() is stopping the
1026 * thread via kthread_stop() after calling
1027 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
1028 * oneshot mask bit can be set. We cannot verify that as we
1029 * cannot touch the oneshot mask at this point anymore as
1030 * __setup_irq() might have given out currents thread_mask
1033 task_work_cancel(current, irq_thread_dtor);
1038 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1039 * @irq: Interrupt line
1040 * @dev_id: Device identity for which the thread should be woken
1043 void irq_wake_thread(unsigned int irq, void *dev_id)
1045 struct irq_desc *desc = irq_to_desc(irq);
1046 struct irqaction *action;
1047 unsigned long flags;
1049 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1052 raw_spin_lock_irqsave(&desc->lock, flags);
1053 for (action = desc->action; action; action = action->next) {
1054 if (action->dev_id == dev_id) {
1056 __irq_wake_thread(desc, action);
1060 raw_spin_unlock_irqrestore(&desc->lock, flags);
1062 EXPORT_SYMBOL_GPL(irq_wake_thread);
1064 static int irq_setup_forced_threading(struct irqaction *new)
1066 if (!force_irqthreads)
1068 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1071 new->flags |= IRQF_ONESHOT;
1074 * Handle the case where we have a real primary handler and a
1075 * thread handler. We force thread them as well by creating a
1078 if (new->handler != irq_default_primary_handler && new->thread_fn) {
1079 /* Allocate the secondary action */
1080 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1081 if (!new->secondary)
1083 new->secondary->handler = irq_forced_secondary_handler;
1084 new->secondary->thread_fn = new->thread_fn;
1085 new->secondary->dev_id = new->dev_id;
1086 new->secondary->irq = new->irq;
1087 new->secondary->name = new->name;
1089 /* Deal with the primary handler */
1090 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1091 new->thread_fn = new->handler;
1092 new->handler = irq_default_primary_handler;
1096 static int irq_request_resources(struct irq_desc *desc)
1098 struct irq_data *d = &desc->irq_data;
1099 struct irq_chip *c = d->chip;
1101 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1104 static void irq_release_resources(struct irq_desc *desc)
1106 struct irq_data *d = &desc->irq_data;
1107 struct irq_chip *c = d->chip;
1109 if (c->irq_release_resources)
1110 c->irq_release_resources(d);
1114 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1116 struct task_struct *t;
1117 struct sched_param param = {
1118 .sched_priority = MAX_USER_RT_PRIO/2,
1122 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1125 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1127 param.sched_priority += 1;
1133 sched_setscheduler_nocheck(t, SCHED_FIFO, ¶m);
1136 * We keep the reference to the task struct even if
1137 * the thread dies to avoid that the interrupt code
1138 * references an already freed task_struct.
1143 * Tell the thread to set its affinity. This is
1144 * important for shared interrupt handlers as we do
1145 * not invoke setup_affinity() for the secondary
1146 * handlers as everything is already set up. Even for
1147 * interrupts marked with IRQF_NO_BALANCE this is
1148 * correct as we want the thread to move to the cpu(s)
1149 * on which the requesting code placed the interrupt.
1151 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1156 * Internal function to register an irqaction - typically used to
1157 * allocate special interrupts that are part of the architecture.
1160 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1162 struct irqaction *old, **old_ptr;
1163 unsigned long flags, thread_mask = 0;
1164 int ret, nested, shared = 0;
1170 if (desc->irq_data.chip == &no_irq_chip)
1172 if (!try_module_get(desc->owner))
1178 * Check whether the interrupt nests into another interrupt
1181 nested = irq_settings_is_nested_thread(desc);
1183 if (!new->thread_fn) {
1188 * Replace the primary handler which was provided from
1189 * the driver for non nested interrupt handling by the
1190 * dummy function which warns when called.
1192 new->handler = irq_nested_primary_handler;
1194 if (irq_settings_can_thread(desc)) {
1195 ret = irq_setup_forced_threading(new);
1202 * Create a handler thread when a thread function is supplied
1203 * and the interrupt does not nest into another interrupt
1206 if (new->thread_fn && !nested) {
1207 ret = setup_irq_thread(new, irq, false);
1210 if (new->secondary) {
1211 ret = setup_irq_thread(new->secondary, irq, true);
1217 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1223 * Drivers are often written to work w/o knowledge about the
1224 * underlying irq chip implementation, so a request for a
1225 * threaded irq without a primary hard irq context handler
1226 * requires the ONESHOT flag to be set. Some irq chips like
1227 * MSI based interrupts are per se one shot safe. Check the
1228 * chip flags, so we can avoid the unmask dance at the end of
1229 * the threaded handler for those.
1231 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1232 new->flags &= ~IRQF_ONESHOT;
1235 * The following block of code has to be executed atomically
1237 raw_spin_lock_irqsave(&desc->lock, flags);
1238 old_ptr = &desc->action;
1242 * Can't share interrupts unless both agree to and are
1243 * the same type (level, edge, polarity). So both flag
1244 * fields must have IRQF_SHARED set and the bits which
1245 * set the trigger type must match. Also all must
1248 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1249 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
1250 ((old->flags ^ new->flags) & IRQF_ONESHOT))
1253 /* All handlers must agree on per-cpuness */
1254 if ((old->flags & IRQF_PERCPU) !=
1255 (new->flags & IRQF_PERCPU))
1258 /* add new interrupt at end of irq queue */
1261 * Or all existing action->thread_mask bits,
1262 * so we can find the next zero bit for this
1265 thread_mask |= old->thread_mask;
1266 old_ptr = &old->next;
1273 * Setup the thread mask for this irqaction for ONESHOT. For
1274 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1275 * conditional in irq_wake_thread().
1277 if (new->flags & IRQF_ONESHOT) {
1279 * Unlikely to have 32 resp 64 irqs sharing one line,
1282 if (thread_mask == ~0UL) {
1287 * The thread_mask for the action is or'ed to
1288 * desc->thread_active to indicate that the
1289 * IRQF_ONESHOT thread handler has been woken, but not
1290 * yet finished. The bit is cleared when a thread
1291 * completes. When all threads of a shared interrupt
1292 * line have completed desc->threads_active becomes
1293 * zero and the interrupt line is unmasked. See
1294 * handle.c:irq_wake_thread() for further information.
1296 * If no thread is woken by primary (hard irq context)
1297 * interrupt handlers, then desc->threads_active is
1298 * also checked for zero to unmask the irq line in the
1299 * affected hard irq flow handlers
1300 * (handle_[fasteoi|level]_irq).
1302 * The new action gets the first zero bit of
1303 * thread_mask assigned. See the loop above which or's
1304 * all existing action->thread_mask bits.
1306 new->thread_mask = 1 << ffz(thread_mask);
1308 } else if (new->handler == irq_default_primary_handler &&
1309 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1311 * The interrupt was requested with handler = NULL, so
1312 * we use the default primary handler for it. But it
1313 * does not have the oneshot flag set. In combination
1314 * with level interrupts this is deadly, because the
1315 * default primary handler just wakes the thread, then
1316 * the irq lines is reenabled, but the device still
1317 * has the level irq asserted. Rinse and repeat....
1319 * While this works for edge type interrupts, we play
1320 * it safe and reject unconditionally because we can't
1321 * say for sure which type this interrupt really
1322 * has. The type flags are unreliable as the
1323 * underlying chip implementation can override them.
1325 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1332 ret = irq_request_resources(desc);
1334 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1335 new->name, irq, desc->irq_data.chip->name);
1339 init_waitqueue_head(&desc->wait_for_threads);
1341 /* Setup the type (level, edge polarity) if configured: */
1342 if (new->flags & IRQF_TRIGGER_MASK) {
1343 ret = __irq_set_trigger(desc, irq,
1344 new->flags & IRQF_TRIGGER_MASK);
1350 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1351 IRQS_ONESHOT | IRQS_WAITING);
1352 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1354 if (new->flags & IRQF_PERCPU) {
1355 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1356 irq_settings_set_per_cpu(desc);
1359 if (new->flags & IRQF_ONESHOT)
1360 desc->istate |= IRQS_ONESHOT;
1362 if (irq_settings_can_autoenable(desc))
1363 irq_startup(desc, true);
1365 /* Undo nested disables: */
1368 /* Exclude IRQ from balancing if requested */
1369 if (new->flags & IRQF_NOBALANCING) {
1370 irq_settings_set_no_balancing(desc);
1371 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1374 if (new->flags & IRQF_NO_SOFTIRQ_CALL)
1375 irq_settings_set_no_softirq_call(desc);
1377 /* Set default affinity mask once everything is setup */
1378 setup_affinity(irq, desc, mask);
1380 } else if (new->flags & IRQF_TRIGGER_MASK) {
1381 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1382 unsigned int omsk = irq_settings_get_trigger_mask(desc);
1385 /* hope the handler works with current trigger mode */
1386 pr_warning("irq %d uses trigger mode %u; requested %u\n",
1392 irq_pm_install_action(desc, new);
1394 /* Reset broken irq detection when installing new handler */
1395 desc->irq_count = 0;
1396 desc->irqs_unhandled = 0;
1399 * Check whether we disabled the irq via the spurious handler
1400 * before. Reenable it and give it another chance.
1402 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1403 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1404 __enable_irq(desc, irq);
1407 raw_spin_unlock_irqrestore(&desc->lock, flags);
1410 * Strictly no need to wake it up, but hung_task complains
1411 * when no hard interrupt wakes the thread up.
1414 wake_up_process(new->thread);
1416 wake_up_process(new->secondary->thread);
1418 register_irq_proc(irq, desc);
1420 register_handler_proc(irq, new);
1421 free_cpumask_var(mask);
1426 if (!(new->flags & IRQF_PROBE_SHARED)) {
1427 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1428 irq, new->flags, new->name, old->flags, old->name);
1429 #ifdef CONFIG_DEBUG_SHIRQ
1436 raw_spin_unlock_irqrestore(&desc->lock, flags);
1437 free_cpumask_var(mask);
1441 struct task_struct *t = new->thread;
1447 if (new->secondary && new->secondary->thread) {
1448 struct task_struct *t = new->secondary->thread;
1450 new->secondary->thread = NULL;
1455 module_put(desc->owner);
1460 * setup_irq - setup an interrupt
1461 * @irq: Interrupt line to setup
1462 * @act: irqaction for the interrupt
1464 * Used to statically setup interrupts in the early boot process.
1466 int setup_irq(unsigned int irq, struct irqaction *act)
1469 struct irq_desc *desc = irq_to_desc(irq);
1471 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1473 chip_bus_lock(desc);
1474 retval = __setup_irq(irq, desc, act);
1475 chip_bus_sync_unlock(desc);
1479 EXPORT_SYMBOL_GPL(setup_irq);
1482 * Internal function to unregister an irqaction - used to free
1483 * regular and special interrupts that are part of the architecture.
1485 static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
1487 struct irq_desc *desc = irq_to_desc(irq);
1488 struct irqaction *action, **action_ptr;
1489 unsigned long flags;
1491 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1496 raw_spin_lock_irqsave(&desc->lock, flags);
1499 * There can be multiple actions per IRQ descriptor, find the right
1500 * one based on the dev_id:
1502 action_ptr = &desc->action;
1504 action = *action_ptr;
1507 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1508 raw_spin_unlock_irqrestore(&desc->lock, flags);
1513 if (action->dev_id == dev_id)
1515 action_ptr = &action->next;
1518 /* Found it - now remove it from the list of entries: */
1519 *action_ptr = action->next;
1521 irq_pm_remove_action(desc, action);
1523 /* If this was the last handler, shut down the IRQ line: */
1524 if (!desc->action) {
1526 irq_release_resources(desc);
1530 /* make sure affinity_hint is cleaned up */
1531 if (WARN_ON_ONCE(desc->affinity_hint))
1532 desc->affinity_hint = NULL;
1535 raw_spin_unlock_irqrestore(&desc->lock, flags);
1537 unregister_handler_proc(irq, action);
1539 /* Make sure it's not being used on another CPU: */
1540 synchronize_irq(irq);
1542 #ifdef CONFIG_DEBUG_SHIRQ
1544 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1545 * event to happen even now it's being freed, so let's make sure that
1546 * is so by doing an extra call to the handler ....
1548 * ( We do this after actually deregistering it, to make sure that a
1549 * 'real' IRQ doesn't run in * parallel with our fake. )
1551 if (action->flags & IRQF_SHARED) {
1552 local_irq_save(flags);
1553 action->handler(irq, dev_id);
1554 local_irq_restore(flags);
1558 if (action->thread) {
1559 kthread_stop(action->thread);
1560 put_task_struct(action->thread);
1561 if (action->secondary && action->secondary->thread) {
1562 kthread_stop(action->secondary->thread);
1563 put_task_struct(action->secondary->thread);
1567 module_put(desc->owner);
1568 kfree(action->secondary);
1573 * remove_irq - free an interrupt
1574 * @irq: Interrupt line to free
1575 * @act: irqaction for the interrupt
1577 * Used to remove interrupts statically setup by the early boot process.
1579 void remove_irq(unsigned int irq, struct irqaction *act)
1581 struct irq_desc *desc = irq_to_desc(irq);
1583 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1584 __free_irq(irq, act->dev_id);
1586 EXPORT_SYMBOL_GPL(remove_irq);
1589 * free_irq - free an interrupt allocated with request_irq
1590 * @irq: Interrupt line to free
1591 * @dev_id: Device identity to free
1593 * Remove an interrupt handler. The handler is removed and if the
1594 * interrupt line is no longer in use by any driver it is disabled.
1595 * On a shared IRQ the caller must ensure the interrupt is disabled
1596 * on the card it drives before calling this function. The function
1597 * does not return until any executing interrupts for this IRQ
1600 * This function must not be called from interrupt context.
1602 void free_irq(unsigned int irq, void *dev_id)
1604 struct irq_desc *desc = irq_to_desc(irq);
1606 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1610 if (WARN_ON(desc->affinity_notify))
1611 desc->affinity_notify = NULL;
1614 chip_bus_lock(desc);
1615 kfree(__free_irq(irq, dev_id));
1616 chip_bus_sync_unlock(desc);
1618 EXPORT_SYMBOL(free_irq);
1621 * request_threaded_irq - allocate an interrupt line
1622 * @irq: Interrupt line to allocate
1623 * @handler: Function to be called when the IRQ occurs.
1624 * Primary handler for threaded interrupts
1625 * If NULL and thread_fn != NULL the default
1626 * primary handler is installed
1627 * @thread_fn: Function called from the irq handler thread
1628 * If NULL, no irq thread is created
1629 * @irqflags: Interrupt type flags
1630 * @devname: An ascii name for the claiming device
1631 * @dev_id: A cookie passed back to the handler function
1633 * This call allocates interrupt resources and enables the
1634 * interrupt line and IRQ handling. From the point this
1635 * call is made your handler function may be invoked. Since
1636 * your handler function must clear any interrupt the board
1637 * raises, you must take care both to initialise your hardware
1638 * and to set up the interrupt handler in the right order.
1640 * If you want to set up a threaded irq handler for your device
1641 * then you need to supply @handler and @thread_fn. @handler is
1642 * still called in hard interrupt context and has to check
1643 * whether the interrupt originates from the device. If yes it
1644 * needs to disable the interrupt on the device and return
1645 * IRQ_WAKE_THREAD which will wake up the handler thread and run
1646 * @thread_fn. This split handler design is necessary to support
1647 * shared interrupts.
1649 * Dev_id must be globally unique. Normally the address of the
1650 * device data structure is used as the cookie. Since the handler
1651 * receives this value it makes sense to use it.
1653 * If your interrupt is shared you must pass a non NULL dev_id
1654 * as this is required when freeing the interrupt.
1658 * IRQF_SHARED Interrupt is shared
1659 * IRQF_TRIGGER_* Specify active edge(s) or level
1662 int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1663 irq_handler_t thread_fn, unsigned long irqflags,
1664 const char *devname, void *dev_id)
1666 struct irqaction *action;
1667 struct irq_desc *desc;
1671 * Sanity-check: shared interrupts must pass in a real dev-ID,
1672 * otherwise we'll have trouble later trying to figure out
1673 * which interrupt is which (messes up the interrupt freeing
1676 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1677 * it cannot be set along with IRQF_NO_SUSPEND.
1679 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1680 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1681 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
1684 desc = irq_to_desc(irq);
1688 if (!irq_settings_can_request(desc) ||
1689 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1695 handler = irq_default_primary_handler;
1698 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1702 action->handler = handler;
1703 action->thread_fn = thread_fn;
1704 action->flags = irqflags;
1705 action->name = devname;
1706 action->dev_id = dev_id;
1708 chip_bus_lock(desc);
1709 retval = __setup_irq(irq, desc, action);
1710 chip_bus_sync_unlock(desc);
1713 kfree(action->secondary);
1717 #ifdef CONFIG_DEBUG_SHIRQ_FIXME
1718 if (!retval && (irqflags & IRQF_SHARED)) {
1720 * It's a shared IRQ -- the driver ought to be prepared for it
1721 * to happen immediately, so let's make sure....
1722 * We disable the irq to make sure that a 'real' IRQ doesn't
1723 * run in parallel with our fake.
1725 unsigned long flags;
1728 local_irq_save(flags);
1730 handler(irq, dev_id);
1732 local_irq_restore(flags);
1738 EXPORT_SYMBOL(request_threaded_irq);
1741 * request_any_context_irq - allocate an interrupt line
1742 * @irq: Interrupt line to allocate
1743 * @handler: Function to be called when the IRQ occurs.
1744 * Threaded handler for threaded interrupts.
1745 * @flags: Interrupt type flags
1746 * @name: An ascii name for the claiming device
1747 * @dev_id: A cookie passed back to the handler function
1749 * This call allocates interrupt resources and enables the
1750 * interrupt line and IRQ handling. It selects either a
1751 * hardirq or threaded handling method depending on the
1754 * On failure, it returns a negative value. On success,
1755 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1757 int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1758 unsigned long flags, const char *name, void *dev_id)
1760 struct irq_desc *desc = irq_to_desc(irq);
1766 if (irq_settings_is_nested_thread(desc)) {
1767 ret = request_threaded_irq(irq, NULL, handler,
1768 flags, name, dev_id);
1769 return !ret ? IRQC_IS_NESTED : ret;
1772 ret = request_irq(irq, handler, flags, name, dev_id);
1773 return !ret ? IRQC_IS_HARDIRQ : ret;
1775 EXPORT_SYMBOL_GPL(request_any_context_irq);
1777 void enable_percpu_irq(unsigned int irq, unsigned int type)
1779 unsigned int cpu = smp_processor_id();
1780 unsigned long flags;
1781 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1786 type &= IRQ_TYPE_SENSE_MASK;
1787 if (type != IRQ_TYPE_NONE) {
1790 ret = __irq_set_trigger(desc, irq, type);
1793 WARN(1, "failed to set type for IRQ%d\n", irq);
1798 irq_percpu_enable(desc, cpu);
1800 irq_put_desc_unlock(desc, flags);
1802 EXPORT_SYMBOL_GPL(enable_percpu_irq);
1804 void disable_percpu_irq(unsigned int irq)
1806 unsigned int cpu = smp_processor_id();
1807 unsigned long flags;
1808 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1813 irq_percpu_disable(desc, cpu);
1814 irq_put_desc_unlock(desc, flags);
1816 EXPORT_SYMBOL_GPL(disable_percpu_irq);
1819 * Internal function to unregister a percpu irqaction.
1821 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1823 struct irq_desc *desc = irq_to_desc(irq);
1824 struct irqaction *action;
1825 unsigned long flags;
1827 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1832 raw_spin_lock_irqsave(&desc->lock, flags);
1834 action = desc->action;
1835 if (!action || action->percpu_dev_id != dev_id) {
1836 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1840 if (!cpumask_empty(desc->percpu_enabled)) {
1841 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1842 irq, cpumask_first(desc->percpu_enabled));
1846 /* Found it - now remove it from the list of entries: */
1847 desc->action = NULL;
1849 raw_spin_unlock_irqrestore(&desc->lock, flags);
1851 unregister_handler_proc(irq, action);
1853 module_put(desc->owner);
1857 raw_spin_unlock_irqrestore(&desc->lock, flags);
1862 * remove_percpu_irq - free a per-cpu interrupt
1863 * @irq: Interrupt line to free
1864 * @act: irqaction for the interrupt
1866 * Used to remove interrupts statically setup by the early boot process.
1868 void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1870 struct irq_desc *desc = irq_to_desc(irq);
1872 if (desc && irq_settings_is_per_cpu_devid(desc))
1873 __free_percpu_irq(irq, act->percpu_dev_id);
1877 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
1878 * @irq: Interrupt line to free
1879 * @dev_id: Device identity to free
1881 * Remove a percpu interrupt handler. The handler is removed, but
1882 * the interrupt line is not disabled. This must be done on each
1883 * CPU before calling this function. The function does not return
1884 * until any executing interrupts for this IRQ have completed.
1886 * This function must not be called from interrupt context.
1888 void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1890 struct irq_desc *desc = irq_to_desc(irq);
1892 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1895 chip_bus_lock(desc);
1896 kfree(__free_percpu_irq(irq, dev_id));
1897 chip_bus_sync_unlock(desc);
1901 * setup_percpu_irq - setup a per-cpu interrupt
1902 * @irq: Interrupt line to setup
1903 * @act: irqaction for the interrupt
1905 * Used to statically setup per-cpu interrupts in the early boot process.
1907 int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1909 struct irq_desc *desc = irq_to_desc(irq);
1912 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1914 chip_bus_lock(desc);
1915 retval = __setup_irq(irq, desc, act);
1916 chip_bus_sync_unlock(desc);
1922 * request_percpu_irq - allocate a percpu interrupt line
1923 * @irq: Interrupt line to allocate
1924 * @handler: Function to be called when the IRQ occurs.
1925 * @devname: An ascii name for the claiming device
1926 * @dev_id: A percpu cookie passed back to the handler function
1928 * This call allocates interrupt resources, but doesn't
1929 * automatically enable the interrupt. It has to be done on each
1930 * CPU using enable_percpu_irq().
1932 * Dev_id must be globally unique. It is a per-cpu variable, and
1933 * the handler gets called with the interrupted CPU's instance of
1936 int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1937 const char *devname, void __percpu *dev_id)
1939 struct irqaction *action;
1940 struct irq_desc *desc;
1946 desc = irq_to_desc(irq);
1947 if (!desc || !irq_settings_can_request(desc) ||
1948 !irq_settings_is_per_cpu_devid(desc))
1951 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1955 action->handler = handler;
1956 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
1957 action->name = devname;
1958 action->percpu_dev_id = dev_id;
1960 chip_bus_lock(desc);
1961 retval = __setup_irq(irq, desc, action);
1962 chip_bus_sync_unlock(desc);
1971 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
1972 * @irq: Interrupt line that is forwarded to a VM
1973 * @which: One of IRQCHIP_STATE_* the caller wants to know about
1974 * @state: a pointer to a boolean where the state is to be storeed
1976 * This call snapshots the internal irqchip state of an
1977 * interrupt, returning into @state the bit corresponding to
1980 * This function should be called with preemption disabled if the
1981 * interrupt controller has per-cpu registers.
1983 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
1986 struct irq_desc *desc;
1987 struct irq_data *data;
1988 struct irq_chip *chip;
1989 unsigned long flags;
1992 desc = irq_get_desc_buslock(irq, &flags, 0);
1996 data = irq_desc_get_irq_data(desc);
1999 chip = irq_data_get_irq_chip(data);
2000 if (chip->irq_get_irqchip_state)
2002 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2003 data = data->parent_data;
2010 err = chip->irq_get_irqchip_state(data, which, state);
2012 irq_put_desc_busunlock(desc, flags);
2017 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2018 * @irq: Interrupt line that is forwarded to a VM
2019 * @which: State to be restored (one of IRQCHIP_STATE_*)
2020 * @val: Value corresponding to @which
2022 * This call sets the internal irqchip state of an interrupt,
2023 * depending on the value of @which.
2025 * This function should be called with preemption disabled if the
2026 * interrupt controller has per-cpu registers.
2028 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2031 struct irq_desc *desc;
2032 struct irq_data *data;
2033 struct irq_chip *chip;
2034 unsigned long flags;
2037 desc = irq_get_desc_buslock(irq, &flags, 0);
2041 data = irq_desc_get_irq_data(desc);
2044 chip = irq_data_get_irq_chip(data);
2045 if (chip->irq_set_irqchip_state)
2047 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2048 data = data->parent_data;
2055 err = chip->irq_set_irqchip_state(data, which, val);
2057 irq_put_desc_busunlock(desc, flags);