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/sched/task.h>
21 #include <uapi/linux/sched/types.h>
22 #include <linux/task_work.h>
24 #include "internals.h"
26 #ifdef CONFIG_IRQ_FORCED_THREADING
27 __read_mostly bool force_irqthreads;
29 static int __init setup_forced_irqthreads(char *arg)
31 force_irqthreads = true;
34 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;
120 static bool __irq_can_set_affinity(struct irq_desc *desc)
122 if (!desc || !irqd_can_balance(&desc->irq_data) ||
123 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
129 * irq_can_set_affinity - Check if the affinity of a given irq can be set
130 * @irq: Interrupt to check
133 int irq_can_set_affinity(unsigned int irq)
135 return __irq_can_set_affinity(irq_to_desc(irq));
139 * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space
140 * @irq: Interrupt to check
142 * Like irq_can_set_affinity() above, but additionally checks for the
143 * AFFINITY_MANAGED flag.
145 bool irq_can_set_affinity_usr(unsigned int irq)
147 struct irq_desc *desc = irq_to_desc(irq);
149 return __irq_can_set_affinity(desc) &&
150 !irqd_affinity_is_managed(&desc->irq_data);
154 * irq_set_thread_affinity - Notify irq threads to adjust affinity
155 * @desc: irq descriptor which has affitnity changed
157 * We just set IRQTF_AFFINITY and delegate the affinity setting
158 * to the interrupt thread itself. We can not call
159 * set_cpus_allowed_ptr() here as we hold desc->lock and this
160 * code can be called from hard interrupt context.
162 void irq_set_thread_affinity(struct irq_desc *desc)
164 struct irqaction *action;
166 for_each_action_of_desc(desc, action)
168 set_bit(IRQTF_AFFINITY, &action->thread_flags);
171 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
172 static void irq_validate_effective_affinity(struct irq_data *data)
174 const struct cpumask *m = irq_data_get_effective_affinity_mask(data);
175 struct irq_chip *chip = irq_data_get_irq_chip(data);
177 if (!cpumask_empty(m))
179 pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n",
180 chip->name, data->irq);
183 static inline void irq_init_effective_affinity(struct irq_data *data,
184 const struct cpumask *mask)
186 cpumask_copy(irq_data_get_effective_affinity_mask(data), mask);
189 static inline void irq_validate_effective_affinity(struct irq_data *data) { }
190 static inline void irq_init_effective_affinity(struct irq_data *data,
191 const struct cpumask *mask) { }
194 int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
197 struct irq_desc *desc = irq_data_to_desc(data);
198 struct irq_chip *chip = irq_data_get_irq_chip(data);
201 if (!chip || !chip->irq_set_affinity)
204 ret = chip->irq_set_affinity(data, mask, force);
206 case IRQ_SET_MASK_OK:
207 case IRQ_SET_MASK_OK_DONE:
208 cpumask_copy(desc->irq_common_data.affinity, mask);
209 case IRQ_SET_MASK_OK_NOCOPY:
210 irq_validate_effective_affinity(data);
211 irq_set_thread_affinity(desc);
218 static bool irq_set_affinity_deactivated(struct irq_data *data,
219 const struct cpumask *mask, bool force)
221 struct irq_desc *desc = irq_data_to_desc(data);
224 * Handle irq chips which can handle affinity only in activated
227 * If the interrupt is not yet activated, just store the affinity
228 * mask and do not call the chip driver at all. On activation the
229 * driver has to make sure anyway that the interrupt is in a
230 * useable state so startup works.
232 if (!IS_ENABLED(CONFIG_IRQ_DOMAIN_HIERARCHY) ||
233 irqd_is_activated(data) || !irqd_affinity_on_activate(data))
236 cpumask_copy(desc->irq_common_data.affinity, mask);
237 irq_init_effective_affinity(data, mask);
238 irqd_set(data, IRQD_AFFINITY_SET);
242 int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
245 struct irq_chip *chip = irq_data_get_irq_chip(data);
246 struct irq_desc *desc = irq_data_to_desc(data);
249 if (!chip || !chip->irq_set_affinity)
252 if (irq_set_affinity_deactivated(data, mask, force))
255 if (irq_can_move_pcntxt(data)) {
256 ret = irq_do_set_affinity(data, mask, force);
258 irqd_set_move_pending(data);
259 irq_copy_pending(desc, mask);
262 if (desc->affinity_notify) {
263 kref_get(&desc->affinity_notify->kref);
264 if (!schedule_work(&desc->affinity_notify->work)) {
265 /* Work was already scheduled, drop our extra ref */
266 kref_put(&desc->affinity_notify->kref,
267 desc->affinity_notify->release);
270 irqd_set(data, IRQD_AFFINITY_SET);
275 int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
277 struct irq_desc *desc = irq_to_desc(irq);
284 raw_spin_lock_irqsave(&desc->lock, flags);
285 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
286 raw_spin_unlock_irqrestore(&desc->lock, flags);
290 int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
293 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
297 desc->affinity_hint = m;
298 irq_put_desc_unlock(desc, flags);
299 /* set the initial affinity to prevent every interrupt being on CPU0 */
301 __irq_set_affinity(irq, m, false);
304 EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
306 static void irq_affinity_notify(struct work_struct *work)
308 struct irq_affinity_notify *notify =
309 container_of(work, struct irq_affinity_notify, work);
310 struct irq_desc *desc = irq_to_desc(notify->irq);
311 cpumask_var_t cpumask;
314 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
317 raw_spin_lock_irqsave(&desc->lock, flags);
318 if (irq_move_pending(&desc->irq_data))
319 irq_get_pending(cpumask, desc);
321 cpumask_copy(cpumask, desc->irq_common_data.affinity);
322 raw_spin_unlock_irqrestore(&desc->lock, flags);
324 notify->notify(notify, cpumask);
326 free_cpumask_var(cpumask);
328 kref_put(¬ify->kref, notify->release);
332 * irq_set_affinity_notifier - control notification of IRQ affinity changes
333 * @irq: Interrupt for which to enable/disable notification
334 * @notify: Context for notification, or %NULL to disable
335 * notification. Function pointers must be initialised;
336 * the other fields will be initialised by this function.
338 * Must be called in process context. Notification may only be enabled
339 * after the IRQ is allocated and must be disabled before the IRQ is
340 * freed using free_irq().
343 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
345 struct irq_desc *desc = irq_to_desc(irq);
346 struct irq_affinity_notify *old_notify;
349 /* The release function is promised process context */
355 /* Complete initialisation of *notify */
358 kref_init(¬ify->kref);
359 INIT_WORK(¬ify->work, irq_affinity_notify);
362 raw_spin_lock_irqsave(&desc->lock, flags);
363 old_notify = desc->affinity_notify;
364 desc->affinity_notify = notify;
365 raw_spin_unlock_irqrestore(&desc->lock, flags);
368 if (cancel_work_sync(&old_notify->work)) {
369 /* Pending work had a ref, put that one too */
370 kref_put(&old_notify->kref, old_notify->release);
372 kref_put(&old_notify->kref, old_notify->release);
377 EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
379 #ifndef CONFIG_AUTO_IRQ_AFFINITY
381 * Generic version of the affinity autoselector.
383 int irq_setup_affinity(struct irq_desc *desc)
385 struct cpumask *set = irq_default_affinity;
386 int ret, node = irq_desc_get_node(desc);
387 static DEFINE_RAW_SPINLOCK(mask_lock);
388 static struct cpumask mask;
390 /* Excludes PER_CPU and NO_BALANCE interrupts */
391 if (!__irq_can_set_affinity(desc))
394 raw_spin_lock(&mask_lock);
396 * Preserve the managed affinity setting and a userspace affinity
397 * setup, but make sure that one of the targets is online.
399 if (irqd_affinity_is_managed(&desc->irq_data) ||
400 irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
401 if (cpumask_intersects(desc->irq_common_data.affinity,
403 set = desc->irq_common_data.affinity;
405 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
408 cpumask_and(&mask, cpu_online_mask, set);
409 if (cpumask_empty(&mask))
410 cpumask_copy(&mask, cpu_online_mask);
412 if (node != NUMA_NO_NODE) {
413 const struct cpumask *nodemask = cpumask_of_node(node);
415 /* make sure at least one of the cpus in nodemask is online */
416 if (cpumask_intersects(&mask, nodemask))
417 cpumask_and(&mask, &mask, nodemask);
419 ret = irq_do_set_affinity(&desc->irq_data, &mask, false);
420 raw_spin_unlock(&mask_lock);
424 /* Wrapper for ALPHA specific affinity selector magic */
425 int irq_setup_affinity(struct irq_desc *desc)
427 return irq_select_affinity(irq_desc_get_irq(desc));
429 #endif /* CONFIG_AUTO_IRQ_AFFINITY */
430 #endif /* CONFIG_SMP */
434 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
435 * @irq: interrupt number to set affinity
436 * @vcpu_info: vCPU specific data
438 * This function uses the vCPU specific data to set the vCPU
439 * affinity for an irq. The vCPU specific data is passed from
440 * outside, such as KVM. One example code path is as below:
441 * KVM -> IOMMU -> irq_set_vcpu_affinity().
443 int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
446 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
447 struct irq_data *data;
448 struct irq_chip *chip;
454 data = irq_desc_get_irq_data(desc);
456 chip = irq_data_get_irq_chip(data);
457 if (chip && chip->irq_set_vcpu_affinity)
459 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
460 data = data->parent_data;
467 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
468 irq_put_desc_unlock(desc, flags);
472 EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
474 void __disable_irq(struct irq_desc *desc)
480 static int __disable_irq_nosync(unsigned int irq)
483 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
488 irq_put_desc_busunlock(desc, flags);
493 * disable_irq_nosync - disable an irq without waiting
494 * @irq: Interrupt to disable
496 * Disable the selected interrupt line. Disables and Enables are
498 * Unlike disable_irq(), this function does not ensure existing
499 * instances of the IRQ handler have completed before returning.
501 * This function may be called from IRQ context.
503 void disable_irq_nosync(unsigned int irq)
505 __disable_irq_nosync(irq);
507 EXPORT_SYMBOL(disable_irq_nosync);
510 * disable_irq - disable an irq and wait for completion
511 * @irq: Interrupt to disable
513 * Disable the selected interrupt line. Enables and Disables are
515 * This function waits for any pending IRQ handlers for this interrupt
516 * to complete before returning. If you use this function while
517 * holding a resource the IRQ handler may need you will deadlock.
519 * This function may be called - with care - from IRQ context.
521 void disable_irq(unsigned int irq)
523 if (!__disable_irq_nosync(irq))
524 synchronize_irq(irq);
526 EXPORT_SYMBOL(disable_irq);
529 * disable_hardirq - disables an irq and waits for hardirq completion
530 * @irq: Interrupt to disable
532 * Disable the selected interrupt line. Enables and Disables are
534 * This function waits for any pending hard IRQ handlers for this
535 * interrupt to complete before returning. If you use this function while
536 * holding a resource the hard IRQ handler may need you will deadlock.
538 * When used to optimistically disable an interrupt from atomic context
539 * the return value must be checked.
541 * Returns: false if a threaded handler is active.
543 * This function may be called - with care - from IRQ context.
545 bool disable_hardirq(unsigned int irq)
547 if (!__disable_irq_nosync(irq))
548 return synchronize_hardirq(irq);
552 EXPORT_SYMBOL_GPL(disable_hardirq);
554 void __enable_irq(struct irq_desc *desc)
556 switch (desc->depth) {
559 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
560 irq_desc_get_irq(desc));
563 if (desc->istate & IRQS_SUSPENDED)
565 /* Prevent probing on this irq: */
566 irq_settings_set_noprobe(desc);
568 * Call irq_startup() not irq_enable() here because the
569 * interrupt might be marked NOAUTOEN. So irq_startup()
570 * needs to be invoked when it gets enabled the first
571 * time. If it was already started up, then irq_startup()
572 * will invoke irq_enable() under the hood.
574 irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
583 * enable_irq - enable handling of an irq
584 * @irq: Interrupt to enable
586 * Undoes the effect of one call to disable_irq(). If this
587 * matches the last disable, processing of interrupts on this
588 * IRQ line is re-enabled.
590 * This function may be called from IRQ context only when
591 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
593 void enable_irq(unsigned int irq)
596 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
600 if (WARN(!desc->irq_data.chip,
601 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
606 irq_put_desc_busunlock(desc, flags);
608 EXPORT_SYMBOL(enable_irq);
610 static int set_irq_wake_real(unsigned int irq, unsigned int on)
612 struct irq_desc *desc = irq_to_desc(irq);
615 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
618 if (desc->irq_data.chip->irq_set_wake)
619 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
625 * irq_set_irq_wake - control irq power management wakeup
626 * @irq: interrupt to control
627 * @on: enable/disable power management wakeup
629 * Enable/disable power management wakeup mode, which is
630 * disabled by default. Enables and disables must match,
631 * just as they match for non-wakeup mode support.
633 * Wakeup mode lets this IRQ wake the system from sleep
634 * states like "suspend to RAM".
636 int irq_set_irq_wake(unsigned int irq, unsigned int on)
639 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
645 /* wakeup-capable irqs can be shared between drivers that
646 * don't need to have the same sleep mode behaviors.
649 if (desc->wake_depth++ == 0) {
650 ret = set_irq_wake_real(irq, on);
652 desc->wake_depth = 0;
654 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
657 if (desc->wake_depth == 0) {
658 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
659 } else if (--desc->wake_depth == 0) {
660 ret = set_irq_wake_real(irq, on);
662 desc->wake_depth = 1;
664 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
667 irq_put_desc_busunlock(desc, flags);
670 EXPORT_SYMBOL(irq_set_irq_wake);
673 * Internal function that tells the architecture code whether a
674 * particular irq has been exclusively allocated or is available
677 int can_request_irq(unsigned int irq, unsigned long irqflags)
680 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
686 if (irq_settings_can_request(desc)) {
688 irqflags & desc->action->flags & IRQF_SHARED)
691 irq_put_desc_unlock(desc, flags);
695 int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
697 struct irq_chip *chip = desc->irq_data.chip;
700 if (!chip || !chip->irq_set_type) {
702 * IRQF_TRIGGER_* but the PIC does not support multiple
705 pr_debug("No set_type function for IRQ %d (%s)\n",
706 irq_desc_get_irq(desc),
707 chip ? (chip->name ? : "unknown") : "unknown");
711 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
712 if (!irqd_irq_masked(&desc->irq_data))
714 if (!irqd_irq_disabled(&desc->irq_data))
718 /* Mask all flags except trigger mode */
719 flags &= IRQ_TYPE_SENSE_MASK;
720 ret = chip->irq_set_type(&desc->irq_data, flags);
723 case IRQ_SET_MASK_OK:
724 case IRQ_SET_MASK_OK_DONE:
725 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
726 irqd_set(&desc->irq_data, flags);
728 case IRQ_SET_MASK_OK_NOCOPY:
729 flags = irqd_get_trigger_type(&desc->irq_data);
730 irq_settings_set_trigger_mask(desc, flags);
731 irqd_clear(&desc->irq_data, IRQD_LEVEL);
732 irq_settings_clr_level(desc);
733 if (flags & IRQ_TYPE_LEVEL_MASK) {
734 irq_settings_set_level(desc);
735 irqd_set(&desc->irq_data, IRQD_LEVEL);
741 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
742 flags, irq_desc_get_irq(desc), chip->irq_set_type);
749 #ifdef CONFIG_HARDIRQS_SW_RESEND
750 int irq_set_parent(int irq, int parent_irq)
753 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
758 desc->parent_irq = parent_irq;
760 irq_put_desc_unlock(desc, flags);
763 EXPORT_SYMBOL_GPL(irq_set_parent);
767 * Default primary interrupt handler for threaded interrupts. Is
768 * assigned as primary handler when request_threaded_irq is called
769 * with handler == NULL. Useful for oneshot interrupts.
771 static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
773 return IRQ_WAKE_THREAD;
777 * Primary handler for nested threaded interrupts. Should never be
780 static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
782 WARN(1, "Primary handler called for nested irq %d\n", irq);
786 static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
788 WARN(1, "Secondary action handler called for irq %d\n", irq);
792 static int irq_wait_for_interrupt(struct irqaction *action)
794 set_current_state(TASK_INTERRUPTIBLE);
796 while (!kthread_should_stop()) {
798 if (test_and_clear_bit(IRQTF_RUNTHREAD,
799 &action->thread_flags)) {
800 __set_current_state(TASK_RUNNING);
804 set_current_state(TASK_INTERRUPTIBLE);
806 __set_current_state(TASK_RUNNING);
811 * Oneshot interrupts keep the irq line masked until the threaded
812 * handler finished. unmask if the interrupt has not been disabled and
815 static void irq_finalize_oneshot(struct irq_desc *desc,
816 struct irqaction *action)
818 if (!(desc->istate & IRQS_ONESHOT) ||
819 action->handler == irq_forced_secondary_handler)
823 raw_spin_lock_irq(&desc->lock);
826 * Implausible though it may be we need to protect us against
827 * the following scenario:
829 * The thread is faster done than the hard interrupt handler
830 * on the other CPU. If we unmask the irq line then the
831 * interrupt can come in again and masks the line, leaves due
832 * to IRQS_INPROGRESS and the irq line is masked forever.
834 * This also serializes the state of shared oneshot handlers
835 * versus "desc->threads_onehsot |= action->thread_mask;" in
836 * irq_wake_thread(). See the comment there which explains the
839 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
840 raw_spin_unlock_irq(&desc->lock);
841 chip_bus_sync_unlock(desc);
847 * Now check again, whether the thread should run. Otherwise
848 * we would clear the threads_oneshot bit of this thread which
851 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
854 desc->threads_oneshot &= ~action->thread_mask;
856 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
857 irqd_irq_masked(&desc->irq_data))
858 unmask_threaded_irq(desc);
861 raw_spin_unlock_irq(&desc->lock);
862 chip_bus_sync_unlock(desc);
867 * Check whether we need to change the affinity of the interrupt thread.
870 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
875 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
879 * In case we are out of memory we set IRQTF_AFFINITY again and
880 * try again next time
882 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
883 set_bit(IRQTF_AFFINITY, &action->thread_flags);
887 raw_spin_lock_irq(&desc->lock);
889 * This code is triggered unconditionally. Check the affinity
890 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
892 if (cpumask_available(desc->irq_common_data.affinity))
893 cpumask_copy(mask, desc->irq_common_data.affinity);
896 raw_spin_unlock_irq(&desc->lock);
899 set_cpus_allowed_ptr(current, mask);
900 free_cpumask_var(mask);
904 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
908 * Interrupts which are not explicitely requested as threaded
909 * interrupts rely on the implicit bh/preempt disable of the hard irq
910 * context. So we need to disable bh here to avoid deadlocks and other
914 irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
919 if (!IS_ENABLED(CONFIG_PREEMPT_RT_BASE))
921 ret = action->thread_fn(action->irq, action->dev_id);
922 if (ret == IRQ_HANDLED)
923 atomic_inc(&desc->threads_handled);
925 irq_finalize_oneshot(desc, action);
926 if (!IS_ENABLED(CONFIG_PREEMPT_RT_BASE))
933 * Interrupts explicitly requested as threaded interrupts want to be
934 * preemtible - many of them need to sleep and wait for slow busses to
937 static irqreturn_t irq_thread_fn(struct irq_desc *desc,
938 struct irqaction *action)
942 ret = action->thread_fn(action->irq, action->dev_id);
943 if (ret == IRQ_HANDLED)
944 atomic_inc(&desc->threads_handled);
946 irq_finalize_oneshot(desc, action);
950 static void wake_threads_waitq(struct irq_desc *desc)
952 if (atomic_dec_and_test(&desc->threads_active))
953 wake_up(&desc->wait_for_threads);
956 static void irq_thread_dtor(struct callback_head *unused)
958 struct task_struct *tsk = current;
959 struct irq_desc *desc;
960 struct irqaction *action;
962 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
965 action = kthread_data(tsk);
967 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
968 tsk->comm, tsk->pid, action->irq);
971 desc = irq_to_desc(action->irq);
973 * If IRQTF_RUNTHREAD is set, we need to decrement
974 * desc->threads_active and wake possible waiters.
976 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
977 wake_threads_waitq(desc);
979 /* Prevent a stale desc->threads_oneshot */
980 irq_finalize_oneshot(desc, action);
983 static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
985 struct irqaction *secondary = action->secondary;
987 if (WARN_ON_ONCE(!secondary))
990 raw_spin_lock_irq(&desc->lock);
991 __irq_wake_thread(desc, secondary);
992 raw_spin_unlock_irq(&desc->lock);
996 * Interrupt handler thread
998 static int irq_thread(void *data)
1000 struct callback_head on_exit_work;
1001 struct irqaction *action = data;
1002 struct irq_desc *desc = irq_to_desc(action->irq);
1003 irqreturn_t (*handler_fn)(struct irq_desc *desc,
1004 struct irqaction *action);
1006 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
1007 &action->thread_flags))
1008 handler_fn = irq_forced_thread_fn;
1010 handler_fn = irq_thread_fn;
1012 init_task_work(&on_exit_work, irq_thread_dtor);
1013 task_work_add(current, &on_exit_work, false);
1015 irq_thread_check_affinity(desc, action);
1017 while (!irq_wait_for_interrupt(action)) {
1018 irqreturn_t action_ret;
1020 irq_thread_check_affinity(desc, action);
1022 action_ret = handler_fn(desc, action);
1023 if (action_ret == IRQ_WAKE_THREAD)
1024 irq_wake_secondary(desc, action);
1026 wake_threads_waitq(desc);
1030 * This is the regular exit path. __free_irq() is stopping the
1031 * thread via kthread_stop() after calling
1032 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
1033 * oneshot mask bit can be set. We cannot verify that as we
1034 * cannot touch the oneshot mask at this point anymore as
1035 * __setup_irq() might have given out currents thread_mask
1038 task_work_cancel(current, irq_thread_dtor);
1043 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1044 * @irq: Interrupt line
1045 * @dev_id: Device identity for which the thread should be woken
1048 void irq_wake_thread(unsigned int irq, void *dev_id)
1050 struct irq_desc *desc = irq_to_desc(irq);
1051 struct irqaction *action;
1052 unsigned long flags;
1054 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1057 raw_spin_lock_irqsave(&desc->lock, flags);
1058 for_each_action_of_desc(desc, action) {
1059 if (action->dev_id == dev_id) {
1061 __irq_wake_thread(desc, action);
1065 raw_spin_unlock_irqrestore(&desc->lock, flags);
1067 EXPORT_SYMBOL_GPL(irq_wake_thread);
1069 static int irq_setup_forced_threading(struct irqaction *new)
1071 if (!force_irqthreads)
1073 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1077 * No further action required for interrupts which are requested as
1078 * threaded interrupts already
1080 if (new->handler == irq_default_primary_handler)
1083 new->flags |= IRQF_ONESHOT;
1086 * Handle the case where we have a real primary handler and a
1087 * thread handler. We force thread them as well by creating a
1090 if (new->handler && new->thread_fn) {
1091 /* Allocate the secondary action */
1092 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1093 if (!new->secondary)
1095 new->secondary->handler = irq_forced_secondary_handler;
1096 new->secondary->thread_fn = new->thread_fn;
1097 new->secondary->dev_id = new->dev_id;
1098 new->secondary->irq = new->irq;
1099 new->secondary->name = new->name;
1101 /* Deal with the primary handler */
1102 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1103 new->thread_fn = new->handler;
1104 new->handler = irq_default_primary_handler;
1108 static int irq_request_resources(struct irq_desc *desc)
1110 struct irq_data *d = &desc->irq_data;
1111 struct irq_chip *c = d->chip;
1113 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1116 static void irq_release_resources(struct irq_desc *desc)
1118 struct irq_data *d = &desc->irq_data;
1119 struct irq_chip *c = d->chip;
1121 if (c->irq_release_resources)
1122 c->irq_release_resources(d);
1126 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1128 struct task_struct *t;
1129 struct sched_param param = {
1130 .sched_priority = MAX_USER_RT_PRIO/2,
1134 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1137 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1139 param.sched_priority -= 1;
1145 sched_setscheduler_nocheck(t, SCHED_FIFO, ¶m);
1148 * We keep the reference to the task struct even if
1149 * the thread dies to avoid that the interrupt code
1150 * references an already freed task_struct.
1155 * Tell the thread to set its affinity. This is
1156 * important for shared interrupt handlers as we do
1157 * not invoke setup_affinity() for the secondary
1158 * handlers as everything is already set up. Even for
1159 * interrupts marked with IRQF_NO_BALANCE this is
1160 * correct as we want the thread to move to the cpu(s)
1161 * on which the requesting code placed the interrupt.
1163 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1168 * Internal function to register an irqaction - typically used to
1169 * allocate special interrupts that are part of the architecture.
1173 * desc->request_mutex Provides serialization against a concurrent free_irq()
1174 * chip_bus_lock Provides serialization for slow bus operations
1175 * desc->lock Provides serialization against hard interrupts
1177 * chip_bus_lock and desc->lock are sufficient for all other management and
1178 * interrupt related functions. desc->request_mutex solely serializes
1179 * request/free_irq().
1182 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1184 struct irqaction *old, **old_ptr;
1185 unsigned long flags, thread_mask = 0;
1186 int ret, nested, shared = 0;
1191 if (desc->irq_data.chip == &no_irq_chip)
1193 if (!try_module_get(desc->owner))
1199 * If the trigger type is not specified by the caller,
1200 * then use the default for this interrupt.
1202 if (!(new->flags & IRQF_TRIGGER_MASK))
1203 new->flags |= irqd_get_trigger_type(&desc->irq_data);
1206 * Check whether the interrupt nests into another interrupt
1209 nested = irq_settings_is_nested_thread(desc);
1211 if (!new->thread_fn) {
1216 * Replace the primary handler which was provided from
1217 * the driver for non nested interrupt handling by the
1218 * dummy function which warns when called.
1220 new->handler = irq_nested_primary_handler;
1222 if (irq_settings_can_thread(desc)) {
1223 ret = irq_setup_forced_threading(new);
1230 * Create a handler thread when a thread function is supplied
1231 * and the interrupt does not nest into another interrupt
1234 if (new->thread_fn && !nested) {
1235 ret = setup_irq_thread(new, irq, false);
1238 if (new->secondary) {
1239 ret = setup_irq_thread(new->secondary, irq, true);
1246 * Drivers are often written to work w/o knowledge about the
1247 * underlying irq chip implementation, so a request for a
1248 * threaded irq without a primary hard irq context handler
1249 * requires the ONESHOT flag to be set. Some irq chips like
1250 * MSI based interrupts are per se one shot safe. Check the
1251 * chip flags, so we can avoid the unmask dance at the end of
1252 * the threaded handler for those.
1254 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1255 new->flags &= ~IRQF_ONESHOT;
1258 * Protects against a concurrent __free_irq() call which might wait
1259 * for synchronize_irq() to complete without holding the optional
1260 * chip bus lock and desc->lock.
1262 mutex_lock(&desc->request_mutex);
1265 * Acquire bus lock as the irq_request_resources() callback below
1266 * might rely on the serialization or the magic power management
1267 * functions which are abusing the irq_bus_lock() callback,
1269 chip_bus_lock(desc);
1271 /* First installed action requests resources. */
1272 if (!desc->action) {
1273 ret = irq_request_resources(desc);
1275 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1276 new->name, irq, desc->irq_data.chip->name);
1277 goto out_bus_unlock;
1282 * The following block of code has to be executed atomically
1283 * protected against a concurrent interrupt and any of the other
1284 * management calls which are not serialized via
1285 * desc->request_mutex or the optional bus lock.
1287 raw_spin_lock_irqsave(&desc->lock, flags);
1288 old_ptr = &desc->action;
1292 * Can't share interrupts unless both agree to and are
1293 * the same type (level, edge, polarity). So both flag
1294 * fields must have IRQF_SHARED set and the bits which
1295 * set the trigger type must match. Also all must
1298 unsigned int oldtype;
1301 * If nobody did set the configuration before, inherit
1302 * the one provided by the requester.
1304 if (irqd_trigger_type_was_set(&desc->irq_data)) {
1305 oldtype = irqd_get_trigger_type(&desc->irq_data);
1307 oldtype = new->flags & IRQF_TRIGGER_MASK;
1308 irqd_set_trigger_type(&desc->irq_data, oldtype);
1311 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1312 (oldtype != (new->flags & IRQF_TRIGGER_MASK)) ||
1313 ((old->flags ^ new->flags) & IRQF_ONESHOT))
1316 /* All handlers must agree on per-cpuness */
1317 if ((old->flags & IRQF_PERCPU) !=
1318 (new->flags & IRQF_PERCPU))
1321 /* add new interrupt at end of irq queue */
1324 * Or all existing action->thread_mask bits,
1325 * so we can find the next zero bit for this
1328 thread_mask |= old->thread_mask;
1329 old_ptr = &old->next;
1336 * Setup the thread mask for this irqaction for ONESHOT. For
1337 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1338 * conditional in irq_wake_thread().
1340 if (new->flags & IRQF_ONESHOT) {
1342 * Unlikely to have 32 resp 64 irqs sharing one line,
1345 if (thread_mask == ~0UL) {
1350 * The thread_mask for the action is or'ed to
1351 * desc->thread_active to indicate that the
1352 * IRQF_ONESHOT thread handler has been woken, but not
1353 * yet finished. The bit is cleared when a thread
1354 * completes. When all threads of a shared interrupt
1355 * line have completed desc->threads_active becomes
1356 * zero and the interrupt line is unmasked. See
1357 * handle.c:irq_wake_thread() for further information.
1359 * If no thread is woken by primary (hard irq context)
1360 * interrupt handlers, then desc->threads_active is
1361 * also checked for zero to unmask the irq line in the
1362 * affected hard irq flow handlers
1363 * (handle_[fasteoi|level]_irq).
1365 * The new action gets the first zero bit of
1366 * thread_mask assigned. See the loop above which or's
1367 * all existing action->thread_mask bits.
1369 new->thread_mask = 1 << ffz(thread_mask);
1371 } else if (new->handler == irq_default_primary_handler &&
1372 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1374 * The interrupt was requested with handler = NULL, so
1375 * we use the default primary handler for it. But it
1376 * does not have the oneshot flag set. In combination
1377 * with level interrupts this is deadly, because the
1378 * default primary handler just wakes the thread, then
1379 * the irq lines is reenabled, but the device still
1380 * has the level irq asserted. Rinse and repeat....
1382 * While this works for edge type interrupts, we play
1383 * it safe and reject unconditionally because we can't
1384 * say for sure which type this interrupt really
1385 * has. The type flags are unreliable as the
1386 * underlying chip implementation can override them.
1388 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1395 init_waitqueue_head(&desc->wait_for_threads);
1397 /* Setup the type (level, edge polarity) if configured: */
1398 if (new->flags & IRQF_TRIGGER_MASK) {
1399 ret = __irq_set_trigger(desc,
1400 new->flags & IRQF_TRIGGER_MASK);
1406 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1407 IRQS_ONESHOT | IRQS_WAITING);
1408 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1410 if (new->flags & IRQF_PERCPU) {
1411 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1412 irq_settings_set_per_cpu(desc);
1415 if (new->flags & IRQF_ONESHOT)
1416 desc->istate |= IRQS_ONESHOT;
1418 /* Exclude IRQ from balancing if requested */
1419 if (new->flags & IRQF_NOBALANCING) {
1420 irq_settings_set_no_balancing(desc);
1421 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1424 if (irq_settings_can_autoenable(desc)) {
1425 irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
1428 * Shared interrupts do not go well with disabling
1429 * auto enable. The sharing interrupt might request
1430 * it while it's still disabled and then wait for
1431 * interrupts forever.
1433 WARN_ON_ONCE(new->flags & IRQF_SHARED);
1434 /* Undo nested disables: */
1438 } else if (new->flags & IRQF_TRIGGER_MASK) {
1439 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1440 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
1443 /* hope the handler works with current trigger mode */
1444 pr_warn("irq %d uses trigger mode %u; requested %u\n",
1450 irq_pm_install_action(desc, new);
1452 /* Reset broken irq detection when installing new handler */
1453 desc->irq_count = 0;
1454 desc->irqs_unhandled = 0;
1457 * Check whether we disabled the irq via the spurious handler
1458 * before. Reenable it and give it another chance.
1460 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1461 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1465 raw_spin_unlock_irqrestore(&desc->lock, flags);
1466 chip_bus_sync_unlock(desc);
1467 mutex_unlock(&desc->request_mutex);
1469 irq_setup_timings(desc, new);
1472 * Strictly no need to wake it up, but hung_task complains
1473 * when no hard interrupt wakes the thread up.
1476 wake_up_process(new->thread);
1478 wake_up_process(new->secondary->thread);
1480 register_irq_proc(irq, desc);
1481 irq_add_debugfs_entry(irq, desc);
1483 register_handler_proc(irq, new);
1487 if (!(new->flags & IRQF_PROBE_SHARED)) {
1488 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1489 irq, new->flags, new->name, old->flags, old->name);
1490 #ifdef CONFIG_DEBUG_SHIRQ
1497 raw_spin_unlock_irqrestore(&desc->lock, flags);
1500 irq_release_resources(desc);
1502 chip_bus_sync_unlock(desc);
1503 mutex_unlock(&desc->request_mutex);
1507 struct task_struct *t = new->thread;
1513 if (new->secondary && new->secondary->thread) {
1514 struct task_struct *t = new->secondary->thread;
1516 new->secondary->thread = NULL;
1521 module_put(desc->owner);
1526 * setup_irq - setup an interrupt
1527 * @irq: Interrupt line to setup
1528 * @act: irqaction for the interrupt
1530 * Used to statically setup interrupts in the early boot process.
1532 int setup_irq(unsigned int irq, struct irqaction *act)
1535 struct irq_desc *desc = irq_to_desc(irq);
1537 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1540 retval = irq_chip_pm_get(&desc->irq_data);
1544 retval = __setup_irq(irq, desc, act);
1547 irq_chip_pm_put(&desc->irq_data);
1551 EXPORT_SYMBOL_GPL(setup_irq);
1554 * Internal function to unregister an irqaction - used to free
1555 * regular and special interrupts that are part of the architecture.
1557 static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
1559 struct irq_desc *desc = irq_to_desc(irq);
1560 struct irqaction *action, **action_ptr;
1561 unsigned long flags;
1563 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1568 mutex_lock(&desc->request_mutex);
1569 chip_bus_lock(desc);
1570 raw_spin_lock_irqsave(&desc->lock, flags);
1573 * There can be multiple actions per IRQ descriptor, find the right
1574 * one based on the dev_id:
1576 action_ptr = &desc->action;
1578 action = *action_ptr;
1581 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1582 raw_spin_unlock_irqrestore(&desc->lock, flags);
1583 chip_bus_sync_unlock(desc);
1584 mutex_unlock(&desc->request_mutex);
1588 if (action->dev_id == dev_id)
1590 action_ptr = &action->next;
1593 /* Found it - now remove it from the list of entries: */
1594 *action_ptr = action->next;
1596 irq_pm_remove_action(desc, action);
1598 /* If this was the last handler, shut down the IRQ line: */
1599 if (!desc->action) {
1600 irq_settings_clr_disable_unlazy(desc);
1605 /* make sure affinity_hint is cleaned up */
1606 if (WARN_ON_ONCE(desc->affinity_hint))
1607 desc->affinity_hint = NULL;
1610 raw_spin_unlock_irqrestore(&desc->lock, flags);
1612 * Drop bus_lock here so the changes which were done in the chip
1613 * callbacks above are synced out to the irq chips which hang
1614 * behind a slow bus (I2C, SPI) before calling synchronize_irq().
1616 * Aside of that the bus_lock can also be taken from the threaded
1617 * handler in irq_finalize_oneshot() which results in a deadlock
1618 * because synchronize_irq() would wait forever for the thread to
1619 * complete, which is blocked on the bus lock.
1621 * The still held desc->request_mutex() protects against a
1622 * concurrent request_irq() of this irq so the release of resources
1623 * and timing data is properly serialized.
1625 chip_bus_sync_unlock(desc);
1627 unregister_handler_proc(irq, action);
1629 /* Make sure it's not being used on another CPU: */
1630 synchronize_irq(irq);
1632 #ifdef CONFIG_DEBUG_SHIRQ
1634 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1635 * event to happen even now it's being freed, so let's make sure that
1636 * is so by doing an extra call to the handler ....
1638 * ( We do this after actually deregistering it, to make sure that a
1639 * 'real' IRQ doesn't run in * parallel with our fake. )
1641 if (action->flags & IRQF_SHARED) {
1642 local_irq_save(flags);
1643 action->handler(irq, dev_id);
1644 local_irq_restore(flags);
1648 if (action->thread) {
1649 kthread_stop(action->thread);
1650 put_task_struct(action->thread);
1651 if (action->secondary && action->secondary->thread) {
1652 kthread_stop(action->secondary->thread);
1653 put_task_struct(action->secondary->thread);
1657 /* Last action releases resources */
1658 if (!desc->action) {
1660 * Reaquire bus lock as irq_release_resources() might
1661 * require it to deallocate resources over the slow bus.
1663 chip_bus_lock(desc);
1664 irq_release_resources(desc);
1665 chip_bus_sync_unlock(desc);
1666 irq_remove_timings(desc);
1669 mutex_unlock(&desc->request_mutex);
1671 irq_chip_pm_put(&desc->irq_data);
1672 module_put(desc->owner);
1673 kfree(action->secondary);
1678 * remove_irq - free an interrupt
1679 * @irq: Interrupt line to free
1680 * @act: irqaction for the interrupt
1682 * Used to remove interrupts statically setup by the early boot process.
1684 void remove_irq(unsigned int irq, struct irqaction *act)
1686 struct irq_desc *desc = irq_to_desc(irq);
1688 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1689 __free_irq(irq, act->dev_id);
1691 EXPORT_SYMBOL_GPL(remove_irq);
1694 * free_irq - free an interrupt allocated with request_irq
1695 * @irq: Interrupt line to free
1696 * @dev_id: Device identity to free
1698 * Remove an interrupt handler. The handler is removed and if the
1699 * interrupt line is no longer in use by any driver it is disabled.
1700 * On a shared IRQ the caller must ensure the interrupt is disabled
1701 * on the card it drives before calling this function. The function
1702 * does not return until any executing interrupts for this IRQ
1705 * This function must not be called from interrupt context.
1707 * Returns the devname argument passed to request_irq.
1709 const void *free_irq(unsigned int irq, void *dev_id)
1711 struct irq_desc *desc = irq_to_desc(irq);
1712 struct irqaction *action;
1713 const char *devname;
1715 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1719 if (WARN_ON(desc->affinity_notify))
1720 desc->affinity_notify = NULL;
1723 action = __free_irq(irq, dev_id);
1728 devname = action->name;
1732 EXPORT_SYMBOL(free_irq);
1735 * request_threaded_irq - allocate an interrupt line
1736 * @irq: Interrupt line to allocate
1737 * @handler: Function to be called when the IRQ occurs.
1738 * Primary handler for threaded interrupts
1739 * If NULL and thread_fn != NULL the default
1740 * primary handler is installed
1741 * @thread_fn: Function called from the irq handler thread
1742 * If NULL, no irq thread is created
1743 * @irqflags: Interrupt type flags
1744 * @devname: An ascii name for the claiming device
1745 * @dev_id: A cookie passed back to the handler function
1747 * This call allocates interrupt resources and enables the
1748 * interrupt line and IRQ handling. From the point this
1749 * call is made your handler function may be invoked. Since
1750 * your handler function must clear any interrupt the board
1751 * raises, you must take care both to initialise your hardware
1752 * and to set up the interrupt handler in the right order.
1754 * If you want to set up a threaded irq handler for your device
1755 * then you need to supply @handler and @thread_fn. @handler is
1756 * still called in hard interrupt context and has to check
1757 * whether the interrupt originates from the device. If yes it
1758 * needs to disable the interrupt on the device and return
1759 * IRQ_WAKE_THREAD which will wake up the handler thread and run
1760 * @thread_fn. This split handler design is necessary to support
1761 * shared interrupts.
1763 * Dev_id must be globally unique. Normally the address of the
1764 * device data structure is used as the cookie. Since the handler
1765 * receives this value it makes sense to use it.
1767 * If your interrupt is shared you must pass a non NULL dev_id
1768 * as this is required when freeing the interrupt.
1772 * IRQF_SHARED Interrupt is shared
1773 * IRQF_TRIGGER_* Specify active edge(s) or level
1776 int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1777 irq_handler_t thread_fn, unsigned long irqflags,
1778 const char *devname, void *dev_id)
1780 struct irqaction *action;
1781 struct irq_desc *desc;
1784 if (irq == IRQ_NOTCONNECTED)
1788 * Sanity-check: shared interrupts must pass in a real dev-ID,
1789 * otherwise we'll have trouble later trying to figure out
1790 * which interrupt is which (messes up the interrupt freeing
1793 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1794 * it cannot be set along with IRQF_NO_SUSPEND.
1796 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1797 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1798 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
1801 desc = irq_to_desc(irq);
1805 if (!irq_settings_can_request(desc) ||
1806 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1812 handler = irq_default_primary_handler;
1815 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1819 action->handler = handler;
1820 action->thread_fn = thread_fn;
1821 action->flags = irqflags;
1822 action->name = devname;
1823 action->dev_id = dev_id;
1825 retval = irq_chip_pm_get(&desc->irq_data);
1831 retval = __setup_irq(irq, desc, action);
1834 irq_chip_pm_put(&desc->irq_data);
1835 kfree(action->secondary);
1839 #ifdef CONFIG_DEBUG_SHIRQ_FIXME
1840 if (!retval && (irqflags & IRQF_SHARED)) {
1842 * It's a shared IRQ -- the driver ought to be prepared for it
1843 * to happen immediately, so let's make sure....
1844 * We disable the irq to make sure that a 'real' IRQ doesn't
1845 * run in parallel with our fake.
1847 unsigned long flags;
1850 local_irq_save(flags);
1852 handler(irq, dev_id);
1854 local_irq_restore(flags);
1860 EXPORT_SYMBOL(request_threaded_irq);
1863 * request_any_context_irq - allocate an interrupt line
1864 * @irq: Interrupt line to allocate
1865 * @handler: Function to be called when the IRQ occurs.
1866 * Threaded handler for threaded interrupts.
1867 * @flags: Interrupt type flags
1868 * @name: An ascii name for the claiming device
1869 * @dev_id: A cookie passed back to the handler function
1871 * This call allocates interrupt resources and enables the
1872 * interrupt line and IRQ handling. It selects either a
1873 * hardirq or threaded handling method depending on the
1876 * On failure, it returns a negative value. On success,
1877 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1879 int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1880 unsigned long flags, const char *name, void *dev_id)
1882 struct irq_desc *desc;
1885 if (irq == IRQ_NOTCONNECTED)
1888 desc = irq_to_desc(irq);
1892 if (irq_settings_is_nested_thread(desc)) {
1893 ret = request_threaded_irq(irq, NULL, handler,
1894 flags, name, dev_id);
1895 return !ret ? IRQC_IS_NESTED : ret;
1898 ret = request_irq(irq, handler, flags, name, dev_id);
1899 return !ret ? IRQC_IS_HARDIRQ : ret;
1901 EXPORT_SYMBOL_GPL(request_any_context_irq);
1903 void enable_percpu_irq(unsigned int irq, unsigned int type)
1905 unsigned int cpu = smp_processor_id();
1906 unsigned long flags;
1907 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1913 * If the trigger type is not specified by the caller, then
1914 * use the default for this interrupt.
1916 type &= IRQ_TYPE_SENSE_MASK;
1917 if (type == IRQ_TYPE_NONE)
1918 type = irqd_get_trigger_type(&desc->irq_data);
1920 if (type != IRQ_TYPE_NONE) {
1923 ret = __irq_set_trigger(desc, type);
1926 WARN(1, "failed to set type for IRQ%d\n", irq);
1931 irq_percpu_enable(desc, cpu);
1933 irq_put_desc_unlock(desc, flags);
1935 EXPORT_SYMBOL_GPL(enable_percpu_irq);
1938 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
1939 * @irq: Linux irq number to check for
1941 * Must be called from a non migratable context. Returns the enable
1942 * state of a per cpu interrupt on the current cpu.
1944 bool irq_percpu_is_enabled(unsigned int irq)
1946 unsigned int cpu = smp_processor_id();
1947 struct irq_desc *desc;
1948 unsigned long flags;
1951 desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1955 is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
1956 irq_put_desc_unlock(desc, flags);
1960 EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
1962 void disable_percpu_irq(unsigned int irq)
1964 unsigned int cpu = smp_processor_id();
1965 unsigned long flags;
1966 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1971 irq_percpu_disable(desc, cpu);
1972 irq_put_desc_unlock(desc, flags);
1974 EXPORT_SYMBOL_GPL(disable_percpu_irq);
1977 * Internal function to unregister a percpu irqaction.
1979 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1981 struct irq_desc *desc = irq_to_desc(irq);
1982 struct irqaction *action;
1983 unsigned long flags;
1985 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1990 raw_spin_lock_irqsave(&desc->lock, flags);
1992 action = desc->action;
1993 if (!action || action->percpu_dev_id != dev_id) {
1994 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1998 if (!cpumask_empty(desc->percpu_enabled)) {
1999 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
2000 irq, cpumask_first(desc->percpu_enabled));
2004 /* Found it - now remove it from the list of entries: */
2005 desc->action = NULL;
2007 raw_spin_unlock_irqrestore(&desc->lock, flags);
2009 unregister_handler_proc(irq, action);
2011 irq_chip_pm_put(&desc->irq_data);
2012 module_put(desc->owner);
2016 raw_spin_unlock_irqrestore(&desc->lock, flags);
2021 * remove_percpu_irq - free a per-cpu interrupt
2022 * @irq: Interrupt line to free
2023 * @act: irqaction for the interrupt
2025 * Used to remove interrupts statically setup by the early boot process.
2027 void remove_percpu_irq(unsigned int irq, struct irqaction *act)
2029 struct irq_desc *desc = irq_to_desc(irq);
2031 if (desc && irq_settings_is_per_cpu_devid(desc))
2032 __free_percpu_irq(irq, act->percpu_dev_id);
2036 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
2037 * @irq: Interrupt line to free
2038 * @dev_id: Device identity to free
2040 * Remove a percpu interrupt handler. The handler is removed, but
2041 * the interrupt line is not disabled. This must be done on each
2042 * CPU before calling this function. The function does not return
2043 * until any executing interrupts for this IRQ have completed.
2045 * This function must not be called from interrupt context.
2047 void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2049 struct irq_desc *desc = irq_to_desc(irq);
2051 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2054 chip_bus_lock(desc);
2055 kfree(__free_percpu_irq(irq, dev_id));
2056 chip_bus_sync_unlock(desc);
2058 EXPORT_SYMBOL_GPL(free_percpu_irq);
2061 * setup_percpu_irq - setup a per-cpu interrupt
2062 * @irq: Interrupt line to setup
2063 * @act: irqaction for the interrupt
2065 * Used to statically setup per-cpu interrupts in the early boot process.
2067 int setup_percpu_irq(unsigned int irq, struct irqaction *act)
2069 struct irq_desc *desc = irq_to_desc(irq);
2072 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2075 retval = irq_chip_pm_get(&desc->irq_data);
2079 retval = __setup_irq(irq, desc, act);
2082 irq_chip_pm_put(&desc->irq_data);
2088 * __request_percpu_irq - allocate a percpu interrupt line
2089 * @irq: Interrupt line to allocate
2090 * @handler: Function to be called when the IRQ occurs.
2091 * @flags: Interrupt type flags (IRQF_TIMER only)
2092 * @devname: An ascii name for the claiming device
2093 * @dev_id: A percpu cookie passed back to the handler function
2095 * This call allocates interrupt resources and enables the
2096 * interrupt on the local CPU. If the interrupt is supposed to be
2097 * enabled on other CPUs, it has to be done on each CPU using
2098 * enable_percpu_irq().
2100 * Dev_id must be globally unique. It is a per-cpu variable, and
2101 * the handler gets called with the interrupted CPU's instance of
2104 int __request_percpu_irq(unsigned int irq, irq_handler_t handler,
2105 unsigned long flags, const char *devname,
2106 void __percpu *dev_id)
2108 struct irqaction *action;
2109 struct irq_desc *desc;
2115 desc = irq_to_desc(irq);
2116 if (!desc || !irq_settings_can_request(desc) ||
2117 !irq_settings_is_per_cpu_devid(desc))
2120 if (flags && flags != IRQF_TIMER)
2123 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2127 action->handler = handler;
2128 action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND;
2129 action->name = devname;
2130 action->percpu_dev_id = dev_id;
2132 retval = irq_chip_pm_get(&desc->irq_data);
2138 retval = __setup_irq(irq, desc, action);
2141 irq_chip_pm_put(&desc->irq_data);
2147 EXPORT_SYMBOL_GPL(__request_percpu_irq);
2150 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2151 * @irq: Interrupt line that is forwarded to a VM
2152 * @which: One of IRQCHIP_STATE_* the caller wants to know about
2153 * @state: a pointer to a boolean where the state is to be storeed
2155 * This call snapshots the internal irqchip state of an
2156 * interrupt, returning into @state the bit corresponding to
2159 * This function should be called with preemption disabled if the
2160 * interrupt controller has per-cpu registers.
2162 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2165 struct irq_desc *desc;
2166 struct irq_data *data;
2167 struct irq_chip *chip;
2168 unsigned long flags;
2171 desc = irq_get_desc_buslock(irq, &flags, 0);
2175 data = irq_desc_get_irq_data(desc);
2178 chip = irq_data_get_irq_chip(data);
2179 if (chip->irq_get_irqchip_state)
2181 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2182 data = data->parent_data;
2189 err = chip->irq_get_irqchip_state(data, which, state);
2191 irq_put_desc_busunlock(desc, flags);
2194 EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
2197 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2198 * @irq: Interrupt line that is forwarded to a VM
2199 * @which: State to be restored (one of IRQCHIP_STATE_*)
2200 * @val: Value corresponding to @which
2202 * This call sets the internal irqchip state of an interrupt,
2203 * depending on the value of @which.
2205 * This function should be called with preemption disabled if the
2206 * interrupt controller has per-cpu registers.
2208 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2211 struct irq_desc *desc;
2212 struct irq_data *data;
2213 struct irq_chip *chip;
2214 unsigned long flags;
2217 desc = irq_get_desc_buslock(irq, &flags, 0);
2221 data = irq_desc_get_irq_data(desc);
2224 chip = irq_data_get_irq_chip(data);
2225 if (chip->irq_set_irqchip_state)
2227 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2228 data = data->parent_data;
2235 err = chip->irq_set_irqchip_state(data, which, val);
2237 irq_put_desc_busunlock(desc, flags);
2240 EXPORT_SYMBOL_GPL(irq_set_irqchip_state);