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;
120 static int __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_set_thread_affinity - Notify irq threads to adjust affinity
140 * @desc: irq descriptor which has affitnity changed
142 * We just set IRQTF_AFFINITY and delegate the affinity setting
143 * to the interrupt thread itself. We can not call
144 * set_cpus_allowed_ptr() here as we hold desc->lock and this
145 * code can be called from hard interrupt context.
147 void irq_set_thread_affinity(struct irq_desc *desc)
149 struct irqaction *action = desc->action;
153 set_bit(IRQTF_AFFINITY, &action->thread_flags);
154 action = action->next;
158 #ifdef CONFIG_GENERIC_PENDING_IRQ
159 static inline bool irq_can_move_pcntxt(struct irq_data *data)
161 return irqd_can_move_in_process_context(data);
163 static inline bool irq_move_pending(struct irq_data *data)
165 return irqd_is_setaffinity_pending(data);
168 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
170 cpumask_copy(desc->pending_mask, mask);
173 irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
175 cpumask_copy(mask, desc->pending_mask);
178 static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
179 static inline bool irq_move_pending(struct irq_data *data) { return false; }
181 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
183 irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
186 #ifdef CONFIG_PREEMPT_RT_FULL
187 static void _irq_affinity_notify(struct irq_affinity_notify *notify);
188 static struct task_struct *set_affinity_helper;
189 static LIST_HEAD(affinity_list);
190 static DEFINE_RAW_SPINLOCK(affinity_list_lock);
192 static int set_affinity_thread(void *unused)
195 struct irq_affinity_notify *notify;
198 set_current_state(TASK_INTERRUPTIBLE);
200 raw_spin_lock_irq(&affinity_list_lock);
201 empty = list_empty(&affinity_list);
202 raw_spin_unlock_irq(&affinity_list_lock);
206 if (kthread_should_stop())
208 set_current_state(TASK_RUNNING);
212 raw_spin_lock_irq(&affinity_list_lock);
213 if (!list_empty(&affinity_list)) {
214 notify = list_first_entry(&affinity_list,
215 struct irq_affinity_notify, list);
216 list_del_init(¬ify->list);
218 raw_spin_unlock_irq(&affinity_list_lock);
222 _irq_affinity_notify(notify);
228 static void init_helper_thread(void)
230 if (set_affinity_helper)
232 set_affinity_helper = kthread_run(set_affinity_thread, NULL,
234 WARN_ON(IS_ERR(set_affinity_helper));
238 static inline void init_helper_thread(void) { }
242 int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
245 struct irq_desc *desc = irq_data_to_desc(data);
246 struct irq_chip *chip = irq_data_get_irq_chip(data);
249 ret = chip->irq_set_affinity(data, mask, force);
251 case IRQ_SET_MASK_OK:
252 case IRQ_SET_MASK_OK_DONE:
253 cpumask_copy(desc->irq_common_data.affinity, mask);
254 case IRQ_SET_MASK_OK_NOCOPY:
255 irq_set_thread_affinity(desc);
262 int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
265 struct irq_chip *chip = irq_data_get_irq_chip(data);
266 struct irq_desc *desc = irq_data_to_desc(data);
269 if (!chip || !chip->irq_set_affinity)
272 if (irq_can_move_pcntxt(data)) {
273 ret = irq_do_set_affinity(data, mask, force);
275 irqd_set_move_pending(data);
276 irq_copy_pending(desc, mask);
279 if (desc->affinity_notify) {
280 kref_get(&desc->affinity_notify->kref);
282 #ifdef CONFIG_PREEMPT_RT_FULL
283 raw_spin_lock(&affinity_list_lock);
284 if (list_empty(&desc->affinity_notify->list))
285 list_add_tail(&affinity_list,
286 &desc->affinity_notify->list);
287 raw_spin_unlock(&affinity_list_lock);
288 wake_up_process(set_affinity_helper);
290 schedule_work(&desc->affinity_notify->work);
293 irqd_set(data, IRQD_AFFINITY_SET);
298 int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
300 struct irq_desc *desc = irq_to_desc(irq);
307 raw_spin_lock_irqsave(&desc->lock, flags);
308 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
309 raw_spin_unlock_irqrestore(&desc->lock, flags);
313 int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
316 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
320 desc->affinity_hint = m;
321 irq_put_desc_unlock(desc, flags);
322 /* set the initial affinity to prevent every interrupt being on CPU0 */
324 __irq_set_affinity(irq, m, false);
327 EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
329 static void _irq_affinity_notify(struct irq_affinity_notify *notify)
331 struct irq_desc *desc = irq_to_desc(notify->irq);
332 cpumask_var_t cpumask;
335 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
338 raw_spin_lock_irqsave(&desc->lock, flags);
339 if (irq_move_pending(&desc->irq_data))
340 irq_get_pending(cpumask, desc);
342 cpumask_copy(cpumask, desc->irq_common_data.affinity);
343 raw_spin_unlock_irqrestore(&desc->lock, flags);
345 notify->notify(notify, cpumask);
347 free_cpumask_var(cpumask);
349 kref_put(¬ify->kref, notify->release);
352 static void irq_affinity_notify(struct work_struct *work)
354 struct irq_affinity_notify *notify =
355 container_of(work, struct irq_affinity_notify, work);
356 _irq_affinity_notify(notify);
360 * irq_set_affinity_notifier - control notification of IRQ affinity changes
361 * @irq: Interrupt for which to enable/disable notification
362 * @notify: Context for notification, or %NULL to disable
363 * notification. Function pointers must be initialised;
364 * the other fields will be initialised by this function.
366 * Must be called in process context. Notification may only be enabled
367 * after the IRQ is allocated and must be disabled before the IRQ is
368 * freed using free_irq().
371 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
373 struct irq_desc *desc = irq_to_desc(irq);
374 struct irq_affinity_notify *old_notify;
377 /* The release function is promised process context */
383 /* Complete initialisation of *notify */
386 kref_init(¬ify->kref);
387 INIT_WORK(¬ify->work, irq_affinity_notify);
388 INIT_LIST_HEAD(¬ify->list);
389 init_helper_thread();
392 raw_spin_lock_irqsave(&desc->lock, flags);
393 old_notify = desc->affinity_notify;
394 desc->affinity_notify = notify;
395 raw_spin_unlock_irqrestore(&desc->lock, flags);
398 kref_put(&old_notify->kref, old_notify->release);
402 EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
404 #ifndef CONFIG_AUTO_IRQ_AFFINITY
406 * Generic version of the affinity autoselector.
408 static int setup_affinity(struct irq_desc *desc, struct cpumask *mask)
410 struct cpumask *set = irq_default_affinity;
411 int node = irq_desc_get_node(desc);
413 /* Excludes PER_CPU and NO_BALANCE interrupts */
414 if (!__irq_can_set_affinity(desc))
418 * Preserve an userspace affinity setup, but make sure that
419 * one of the targets is online.
421 if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
422 if (cpumask_intersects(desc->irq_common_data.affinity,
424 set = desc->irq_common_data.affinity;
426 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
429 cpumask_and(mask, cpu_online_mask, set);
430 if (node != NUMA_NO_NODE) {
431 const struct cpumask *nodemask = cpumask_of_node(node);
433 /* make sure at least one of the cpus in nodemask is online */
434 if (cpumask_intersects(mask, nodemask))
435 cpumask_and(mask, mask, nodemask);
437 irq_do_set_affinity(&desc->irq_data, mask, false);
441 /* Wrapper for ALPHA specific affinity selector magic */
442 static inline int setup_affinity(struct irq_desc *d, struct cpumask *mask)
444 return irq_select_affinity(irq_desc_get_irq(d));
449 * Called when affinity is set via /proc/irq
451 int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
453 struct irq_desc *desc = irq_to_desc(irq);
457 raw_spin_lock_irqsave(&desc->lock, flags);
458 ret = setup_affinity(desc, mask);
459 raw_spin_unlock_irqrestore(&desc->lock, flags);
465 setup_affinity(struct irq_desc *desc, struct cpumask *mask)
472 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
473 * @irq: interrupt number to set affinity
474 * @vcpu_info: vCPU specific data
476 * This function uses the vCPU specific data to set the vCPU
477 * affinity for an irq. The vCPU specific data is passed from
478 * outside, such as KVM. One example code path is as below:
479 * KVM -> IOMMU -> irq_set_vcpu_affinity().
481 int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
484 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
485 struct irq_data *data;
486 struct irq_chip *chip;
492 data = irq_desc_get_irq_data(desc);
493 chip = irq_data_get_irq_chip(data);
494 if (chip && chip->irq_set_vcpu_affinity)
495 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
496 irq_put_desc_unlock(desc, flags);
500 EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
502 void __disable_irq(struct irq_desc *desc)
508 static int __disable_irq_nosync(unsigned int irq)
511 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
516 irq_put_desc_busunlock(desc, flags);
521 * disable_irq_nosync - disable an irq without waiting
522 * @irq: Interrupt to disable
524 * Disable the selected interrupt line. Disables and Enables are
526 * Unlike disable_irq(), this function does not ensure existing
527 * instances of the IRQ handler have completed before returning.
529 * This function may be called from IRQ context.
531 void disable_irq_nosync(unsigned int irq)
533 __disable_irq_nosync(irq);
535 EXPORT_SYMBOL(disable_irq_nosync);
538 * disable_irq - disable an irq and wait for completion
539 * @irq: Interrupt to disable
541 * Disable the selected interrupt line. Enables and Disables are
543 * This function waits for any pending IRQ handlers for this interrupt
544 * to complete before returning. If you use this function while
545 * holding a resource the IRQ handler may need you will deadlock.
547 * This function may be called - with care - from IRQ context.
549 void disable_irq(unsigned int irq)
551 if (!__disable_irq_nosync(irq))
552 synchronize_irq(irq);
554 EXPORT_SYMBOL(disable_irq);
557 * disable_hardirq - disables an irq and waits for hardirq completion
558 * @irq: Interrupt to disable
560 * Disable the selected interrupt line. Enables and Disables are
562 * This function waits for any pending hard IRQ handlers for this
563 * interrupt to complete before returning. If you use this function while
564 * holding a resource the hard IRQ handler may need you will deadlock.
566 * When used to optimistically disable an interrupt from atomic context
567 * the return value must be checked.
569 * Returns: false if a threaded handler is active.
571 * This function may be called - with care - from IRQ context.
573 bool disable_hardirq(unsigned int irq)
575 if (!__disable_irq_nosync(irq))
576 return synchronize_hardirq(irq);
580 EXPORT_SYMBOL_GPL(disable_hardirq);
582 void __enable_irq(struct irq_desc *desc)
584 switch (desc->depth) {
587 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
588 irq_desc_get_irq(desc));
591 if (desc->istate & IRQS_SUSPENDED)
593 /* Prevent probing on this irq: */
594 irq_settings_set_noprobe(desc);
596 check_irq_resend(desc);
605 * enable_irq - enable handling of an irq
606 * @irq: Interrupt to enable
608 * Undoes the effect of one call to disable_irq(). If this
609 * matches the last disable, processing of interrupts on this
610 * IRQ line is re-enabled.
612 * This function may be called from IRQ context only when
613 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
615 void enable_irq(unsigned int irq)
618 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
622 if (WARN(!desc->irq_data.chip,
623 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
628 irq_put_desc_busunlock(desc, flags);
630 EXPORT_SYMBOL(enable_irq);
632 static int set_irq_wake_real(unsigned int irq, unsigned int on)
634 struct irq_desc *desc = irq_to_desc(irq);
637 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
640 if (desc->irq_data.chip->irq_set_wake)
641 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
647 * irq_set_irq_wake - control irq power management wakeup
648 * @irq: interrupt to control
649 * @on: enable/disable power management wakeup
651 * Enable/disable power management wakeup mode, which is
652 * disabled by default. Enables and disables must match,
653 * just as they match for non-wakeup mode support.
655 * Wakeup mode lets this IRQ wake the system from sleep
656 * states like "suspend to RAM".
658 int irq_set_irq_wake(unsigned int irq, unsigned int on)
661 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
667 /* wakeup-capable irqs can be shared between drivers that
668 * don't need to have the same sleep mode behaviors.
671 if (desc->wake_depth++ == 0) {
672 ret = set_irq_wake_real(irq, on);
674 desc->wake_depth = 0;
676 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
679 if (desc->wake_depth == 0) {
680 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
681 } else if (--desc->wake_depth == 0) {
682 ret = set_irq_wake_real(irq, on);
684 desc->wake_depth = 1;
686 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
689 irq_put_desc_busunlock(desc, flags);
692 EXPORT_SYMBOL(irq_set_irq_wake);
695 * Internal function that tells the architecture code whether a
696 * particular irq has been exclusively allocated or is available
699 int can_request_irq(unsigned int irq, unsigned long irqflags)
702 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
708 if (irq_settings_can_request(desc)) {
710 irqflags & desc->action->flags & IRQF_SHARED)
713 irq_put_desc_unlock(desc, flags);
717 int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
719 struct irq_chip *chip = desc->irq_data.chip;
722 if (!chip || !chip->irq_set_type) {
724 * IRQF_TRIGGER_* but the PIC does not support multiple
727 pr_debug("No set_type function for IRQ %d (%s)\n",
728 irq_desc_get_irq(desc),
729 chip ? (chip->name ? : "unknown") : "unknown");
733 flags &= IRQ_TYPE_SENSE_MASK;
735 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
736 if (!irqd_irq_masked(&desc->irq_data))
738 if (!irqd_irq_disabled(&desc->irq_data))
742 /* caller masked out all except trigger mode flags */
743 ret = chip->irq_set_type(&desc->irq_data, flags);
746 case IRQ_SET_MASK_OK:
747 case IRQ_SET_MASK_OK_DONE:
748 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
749 irqd_set(&desc->irq_data, flags);
751 case IRQ_SET_MASK_OK_NOCOPY:
752 flags = irqd_get_trigger_type(&desc->irq_data);
753 irq_settings_set_trigger_mask(desc, flags);
754 irqd_clear(&desc->irq_data, IRQD_LEVEL);
755 irq_settings_clr_level(desc);
756 if (flags & IRQ_TYPE_LEVEL_MASK) {
757 irq_settings_set_level(desc);
758 irqd_set(&desc->irq_data, IRQD_LEVEL);
764 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
765 flags, irq_desc_get_irq(desc), chip->irq_set_type);
772 #ifdef CONFIG_HARDIRQS_SW_RESEND
773 int irq_set_parent(int irq, int parent_irq)
776 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
781 desc->parent_irq = parent_irq;
783 irq_put_desc_unlock(desc, flags);
789 * Default primary interrupt handler for threaded interrupts. Is
790 * assigned as primary handler when request_threaded_irq is called
791 * with handler == NULL. Useful for oneshot interrupts.
793 static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
795 return IRQ_WAKE_THREAD;
799 * Primary handler for nested threaded interrupts. Should never be
802 static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
804 WARN(1, "Primary handler called for nested irq %d\n", irq);
808 static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
810 WARN(1, "Secondary action handler called for irq %d\n", irq);
814 static int irq_wait_for_interrupt(struct irqaction *action)
816 set_current_state(TASK_INTERRUPTIBLE);
818 while (!kthread_should_stop()) {
820 if (test_and_clear_bit(IRQTF_RUNTHREAD,
821 &action->thread_flags)) {
822 __set_current_state(TASK_RUNNING);
826 set_current_state(TASK_INTERRUPTIBLE);
828 __set_current_state(TASK_RUNNING);
833 * Oneshot interrupts keep the irq line masked until the threaded
834 * handler finished. unmask if the interrupt has not been disabled and
837 static void irq_finalize_oneshot(struct irq_desc *desc,
838 struct irqaction *action)
840 if (!(desc->istate & IRQS_ONESHOT) ||
841 action->handler == irq_forced_secondary_handler)
845 raw_spin_lock_irq(&desc->lock);
848 * Implausible though it may be we need to protect us against
849 * the following scenario:
851 * The thread is faster done than the hard interrupt handler
852 * on the other CPU. If we unmask the irq line then the
853 * interrupt can come in again and masks the line, leaves due
854 * to IRQS_INPROGRESS and the irq line is masked forever.
856 * This also serializes the state of shared oneshot handlers
857 * versus "desc->threads_onehsot |= action->thread_mask;" in
858 * irq_wake_thread(). See the comment there which explains the
861 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
862 raw_spin_unlock_irq(&desc->lock);
863 chip_bus_sync_unlock(desc);
869 * Now check again, whether the thread should run. Otherwise
870 * we would clear the threads_oneshot bit of this thread which
873 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
876 desc->threads_oneshot &= ~action->thread_mask;
878 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
879 irqd_irq_masked(&desc->irq_data))
880 unmask_threaded_irq(desc);
883 raw_spin_unlock_irq(&desc->lock);
884 chip_bus_sync_unlock(desc);
889 * Check whether we need to change the affinity of the interrupt thread.
892 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
897 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
901 * In case we are out of memory we set IRQTF_AFFINITY again and
902 * try again next time
904 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
905 set_bit(IRQTF_AFFINITY, &action->thread_flags);
909 raw_spin_lock_irq(&desc->lock);
911 * This code is triggered unconditionally. Check the affinity
912 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
914 if (desc->irq_common_data.affinity)
915 cpumask_copy(mask, desc->irq_common_data.affinity);
918 raw_spin_unlock_irq(&desc->lock);
921 set_cpus_allowed_ptr(current, mask);
922 free_cpumask_var(mask);
926 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
930 * Interrupts which are not explicitely requested as threaded
931 * interrupts rely on the implicit bh/preempt disable of the hard irq
932 * context. So we need to disable bh here to avoid deadlocks and other
936 irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
941 ret = action->thread_fn(action->irq, action->dev_id);
942 irq_finalize_oneshot(desc, action);
944 * Interrupts which have real time requirements can be set up
945 * to avoid softirq processing in the thread handler. This is
946 * safe as these interrupts do not raise soft interrupts.
948 if (irq_settings_no_softirq_call(desc))
956 * Interrupts explicitly requested as threaded interrupts want to be
957 * preemtible - many of them need to sleep and wait for slow busses to
960 static irqreturn_t irq_thread_fn(struct irq_desc *desc,
961 struct irqaction *action)
965 ret = action->thread_fn(action->irq, action->dev_id);
966 irq_finalize_oneshot(desc, action);
970 static void wake_threads_waitq(struct irq_desc *desc)
972 if (atomic_dec_and_test(&desc->threads_active))
973 wake_up(&desc->wait_for_threads);
976 static void irq_thread_dtor(struct callback_head *unused)
978 struct task_struct *tsk = current;
979 struct irq_desc *desc;
980 struct irqaction *action;
982 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
985 action = kthread_data(tsk);
987 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
988 tsk->comm, tsk->pid, action->irq);
991 desc = irq_to_desc(action->irq);
993 * If IRQTF_RUNTHREAD is set, we need to decrement
994 * desc->threads_active and wake possible waiters.
996 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
997 wake_threads_waitq(desc);
999 /* Prevent a stale desc->threads_oneshot */
1000 irq_finalize_oneshot(desc, action);
1003 static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
1005 struct irqaction *secondary = action->secondary;
1007 if (WARN_ON_ONCE(!secondary))
1010 raw_spin_lock_irq(&desc->lock);
1011 __irq_wake_thread(desc, secondary);
1012 raw_spin_unlock_irq(&desc->lock);
1016 * Interrupt handler thread
1018 static int irq_thread(void *data)
1020 struct callback_head on_exit_work;
1021 struct irqaction *action = data;
1022 struct irq_desc *desc = irq_to_desc(action->irq);
1023 irqreturn_t (*handler_fn)(struct irq_desc *desc,
1024 struct irqaction *action);
1026 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
1027 &action->thread_flags))
1028 handler_fn = irq_forced_thread_fn;
1030 handler_fn = irq_thread_fn;
1032 init_task_work(&on_exit_work, irq_thread_dtor);
1033 task_work_add(current, &on_exit_work, false);
1035 irq_thread_check_affinity(desc, action);
1037 while (!irq_wait_for_interrupt(action)) {
1038 irqreturn_t action_ret;
1040 irq_thread_check_affinity(desc, action);
1042 action_ret = handler_fn(desc, action);
1043 if (action_ret == IRQ_HANDLED)
1044 atomic_inc(&desc->threads_handled);
1045 if (action_ret == IRQ_WAKE_THREAD)
1046 irq_wake_secondary(desc, action);
1048 #ifdef CONFIG_PREEMPT_RT_FULL
1050 add_interrupt_randomness(action->irq, 0,
1051 desc->random_ip ^ (unsigned long) action);
1054 wake_threads_waitq(desc);
1058 * This is the regular exit path. __free_irq() is stopping the
1059 * thread via kthread_stop() after calling
1060 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
1061 * oneshot mask bit can be set. We cannot verify that as we
1062 * cannot touch the oneshot mask at this point anymore as
1063 * __setup_irq() might have given out currents thread_mask
1066 task_work_cancel(current, irq_thread_dtor);
1071 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1072 * @irq: Interrupt line
1073 * @dev_id: Device identity for which the thread should be woken
1076 void irq_wake_thread(unsigned int irq, void *dev_id)
1078 struct irq_desc *desc = irq_to_desc(irq);
1079 struct irqaction *action;
1080 unsigned long flags;
1082 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1085 raw_spin_lock_irqsave(&desc->lock, flags);
1086 for (action = desc->action; action; action = action->next) {
1087 if (action->dev_id == dev_id) {
1089 __irq_wake_thread(desc, action);
1093 raw_spin_unlock_irqrestore(&desc->lock, flags);
1095 EXPORT_SYMBOL_GPL(irq_wake_thread);
1097 static int irq_setup_forced_threading(struct irqaction *new)
1099 if (!force_irqthreads)
1101 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1104 new->flags |= IRQF_ONESHOT;
1107 * Handle the case where we have a real primary handler and a
1108 * thread handler. We force thread them as well by creating a
1111 if (new->handler != irq_default_primary_handler && new->thread_fn) {
1112 /* Allocate the secondary action */
1113 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1114 if (!new->secondary)
1116 new->secondary->handler = irq_forced_secondary_handler;
1117 new->secondary->thread_fn = new->thread_fn;
1118 new->secondary->dev_id = new->dev_id;
1119 new->secondary->irq = new->irq;
1120 new->secondary->name = new->name;
1122 /* Deal with the primary handler */
1123 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1124 new->thread_fn = new->handler;
1125 new->handler = irq_default_primary_handler;
1129 static int irq_request_resources(struct irq_desc *desc)
1131 struct irq_data *d = &desc->irq_data;
1132 struct irq_chip *c = d->chip;
1134 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1137 static void irq_release_resources(struct irq_desc *desc)
1139 struct irq_data *d = &desc->irq_data;
1140 struct irq_chip *c = d->chip;
1142 if (c->irq_release_resources)
1143 c->irq_release_resources(d);
1147 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1149 struct task_struct *t;
1150 struct sched_param param = {
1151 .sched_priority = MAX_USER_RT_PRIO/2,
1155 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1158 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1160 param.sched_priority -= 1;
1166 sched_setscheduler_nocheck(t, SCHED_FIFO, ¶m);
1169 * We keep the reference to the task struct even if
1170 * the thread dies to avoid that the interrupt code
1171 * references an already freed task_struct.
1176 * Tell the thread to set its affinity. This is
1177 * important for shared interrupt handlers as we do
1178 * not invoke setup_affinity() for the secondary
1179 * handlers as everything is already set up. Even for
1180 * interrupts marked with IRQF_NO_BALANCE this is
1181 * correct as we want the thread to move to the cpu(s)
1182 * on which the requesting code placed the interrupt.
1184 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1189 * Internal function to register an irqaction - typically used to
1190 * allocate special interrupts that are part of the architecture.
1193 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1195 struct irqaction *old, **old_ptr;
1196 unsigned long flags, thread_mask = 0;
1197 int ret, nested, shared = 0;
1203 if (desc->irq_data.chip == &no_irq_chip)
1205 if (!try_module_get(desc->owner))
1211 * Check whether the interrupt nests into another interrupt
1214 nested = irq_settings_is_nested_thread(desc);
1216 if (!new->thread_fn) {
1221 * Replace the primary handler which was provided from
1222 * the driver for non nested interrupt handling by the
1223 * dummy function which warns when called.
1225 new->handler = irq_nested_primary_handler;
1227 if (irq_settings_can_thread(desc)) {
1228 ret = irq_setup_forced_threading(new);
1235 * Create a handler thread when a thread function is supplied
1236 * and the interrupt does not nest into another interrupt
1239 if (new->thread_fn && !nested) {
1240 ret = setup_irq_thread(new, irq, false);
1243 if (new->secondary) {
1244 ret = setup_irq_thread(new->secondary, irq, true);
1250 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1256 * Drivers are often written to work w/o knowledge about the
1257 * underlying irq chip implementation, so a request for a
1258 * threaded irq without a primary hard irq context handler
1259 * requires the ONESHOT flag to be set. Some irq chips like
1260 * MSI based interrupts are per se one shot safe. Check the
1261 * chip flags, so we can avoid the unmask dance at the end of
1262 * the threaded handler for those.
1264 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1265 new->flags &= ~IRQF_ONESHOT;
1268 * The following block of code has to be executed atomically
1270 raw_spin_lock_irqsave(&desc->lock, flags);
1271 old_ptr = &desc->action;
1275 * Can't share interrupts unless both agree to and are
1276 * the same type (level, edge, polarity). So both flag
1277 * fields must have IRQF_SHARED set and the bits which
1278 * set the trigger type must match. Also all must
1281 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1282 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
1283 ((old->flags ^ new->flags) & IRQF_ONESHOT))
1286 /* All handlers must agree on per-cpuness */
1287 if ((old->flags & IRQF_PERCPU) !=
1288 (new->flags & IRQF_PERCPU))
1291 /* add new interrupt at end of irq queue */
1294 * Or all existing action->thread_mask bits,
1295 * so we can find the next zero bit for this
1298 thread_mask |= old->thread_mask;
1299 old_ptr = &old->next;
1306 * Setup the thread mask for this irqaction for ONESHOT. For
1307 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1308 * conditional in irq_wake_thread().
1310 if (new->flags & IRQF_ONESHOT) {
1312 * Unlikely to have 32 resp 64 irqs sharing one line,
1315 if (thread_mask == ~0UL) {
1320 * The thread_mask for the action is or'ed to
1321 * desc->thread_active to indicate that the
1322 * IRQF_ONESHOT thread handler has been woken, but not
1323 * yet finished. The bit is cleared when a thread
1324 * completes. When all threads of a shared interrupt
1325 * line have completed desc->threads_active becomes
1326 * zero and the interrupt line is unmasked. See
1327 * handle.c:irq_wake_thread() for further information.
1329 * If no thread is woken by primary (hard irq context)
1330 * interrupt handlers, then desc->threads_active is
1331 * also checked for zero to unmask the irq line in the
1332 * affected hard irq flow handlers
1333 * (handle_[fasteoi|level]_irq).
1335 * The new action gets the first zero bit of
1336 * thread_mask assigned. See the loop above which or's
1337 * all existing action->thread_mask bits.
1339 new->thread_mask = 1 << ffz(thread_mask);
1341 } else if (new->handler == irq_default_primary_handler &&
1342 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1344 * The interrupt was requested with handler = NULL, so
1345 * we use the default primary handler for it. But it
1346 * does not have the oneshot flag set. In combination
1347 * with level interrupts this is deadly, because the
1348 * default primary handler just wakes the thread, then
1349 * the irq lines is reenabled, but the device still
1350 * has the level irq asserted. Rinse and repeat....
1352 * While this works for edge type interrupts, we play
1353 * it safe and reject unconditionally because we can't
1354 * say for sure which type this interrupt really
1355 * has. The type flags are unreliable as the
1356 * underlying chip implementation can override them.
1358 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1365 ret = irq_request_resources(desc);
1367 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1368 new->name, irq, desc->irq_data.chip->name);
1372 init_waitqueue_head(&desc->wait_for_threads);
1374 /* Setup the type (level, edge polarity) if configured: */
1375 if (new->flags & IRQF_TRIGGER_MASK) {
1376 ret = __irq_set_trigger(desc,
1377 new->flags & IRQF_TRIGGER_MASK);
1383 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1384 IRQS_ONESHOT | IRQS_WAITING);
1385 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1387 if (new->flags & IRQF_PERCPU) {
1388 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1389 irq_settings_set_per_cpu(desc);
1392 if (new->flags & IRQF_ONESHOT)
1393 desc->istate |= IRQS_ONESHOT;
1395 if (irq_settings_can_autoenable(desc))
1396 irq_startup(desc, true);
1398 /* Undo nested disables: */
1401 /* Exclude IRQ from balancing if requested */
1402 if (new->flags & IRQF_NOBALANCING) {
1403 irq_settings_set_no_balancing(desc);
1404 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1407 if (new->flags & IRQF_NO_SOFTIRQ_CALL)
1408 irq_settings_set_no_softirq_call(desc);
1410 /* Set default affinity mask once everything is setup */
1411 setup_affinity(desc, mask);
1413 } else if (new->flags & IRQF_TRIGGER_MASK) {
1414 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1415 unsigned int omsk = irq_settings_get_trigger_mask(desc);
1418 /* hope the handler works with current trigger mode */
1419 pr_warning("irq %d uses trigger mode %u; requested %u\n",
1425 irq_pm_install_action(desc, new);
1427 /* Reset broken irq detection when installing new handler */
1428 desc->irq_count = 0;
1429 desc->irqs_unhandled = 0;
1432 * Check whether we disabled the irq via the spurious handler
1433 * before. Reenable it and give it another chance.
1435 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1436 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1440 raw_spin_unlock_irqrestore(&desc->lock, flags);
1443 * Strictly no need to wake it up, but hung_task complains
1444 * when no hard interrupt wakes the thread up.
1447 wake_up_process(new->thread);
1449 wake_up_process(new->secondary->thread);
1451 register_irq_proc(irq, desc);
1453 register_handler_proc(irq, new);
1454 free_cpumask_var(mask);
1459 if (!(new->flags & IRQF_PROBE_SHARED)) {
1460 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1461 irq, new->flags, new->name, old->flags, old->name);
1462 #ifdef CONFIG_DEBUG_SHIRQ
1469 raw_spin_unlock_irqrestore(&desc->lock, flags);
1470 free_cpumask_var(mask);
1474 struct task_struct *t = new->thread;
1480 if (new->secondary && new->secondary->thread) {
1481 struct task_struct *t = new->secondary->thread;
1483 new->secondary->thread = NULL;
1488 module_put(desc->owner);
1493 * setup_irq - setup an interrupt
1494 * @irq: Interrupt line to setup
1495 * @act: irqaction for the interrupt
1497 * Used to statically setup interrupts in the early boot process.
1499 int setup_irq(unsigned int irq, struct irqaction *act)
1502 struct irq_desc *desc = irq_to_desc(irq);
1504 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1506 chip_bus_lock(desc);
1507 retval = __setup_irq(irq, desc, act);
1508 chip_bus_sync_unlock(desc);
1512 EXPORT_SYMBOL_GPL(setup_irq);
1515 * Internal function to unregister an irqaction - used to free
1516 * regular and special interrupts that are part of the architecture.
1518 static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
1520 struct irq_desc *desc = irq_to_desc(irq);
1521 struct irqaction *action, **action_ptr;
1522 unsigned long flags;
1524 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1529 chip_bus_lock(desc);
1530 raw_spin_lock_irqsave(&desc->lock, flags);
1533 * There can be multiple actions per IRQ descriptor, find the right
1534 * one based on the dev_id:
1536 action_ptr = &desc->action;
1538 action = *action_ptr;
1541 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1542 raw_spin_unlock_irqrestore(&desc->lock, flags);
1543 chip_bus_sync_unlock(desc);
1547 if (action->dev_id == dev_id)
1549 action_ptr = &action->next;
1552 /* Found it - now remove it from the list of entries: */
1553 *action_ptr = action->next;
1555 irq_pm_remove_action(desc, action);
1557 /* If this was the last handler, shut down the IRQ line: */
1558 if (!desc->action) {
1559 irq_settings_clr_disable_unlazy(desc);
1561 irq_release_resources(desc);
1565 /* make sure affinity_hint is cleaned up */
1566 if (WARN_ON_ONCE(desc->affinity_hint))
1567 desc->affinity_hint = NULL;
1570 raw_spin_unlock_irqrestore(&desc->lock, flags);
1571 chip_bus_sync_unlock(desc);
1573 unregister_handler_proc(irq, action);
1575 /* Make sure it's not being used on another CPU: */
1576 synchronize_irq(irq);
1578 #ifdef CONFIG_DEBUG_SHIRQ
1580 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1581 * event to happen even now it's being freed, so let's make sure that
1582 * is so by doing an extra call to the handler ....
1584 * ( We do this after actually deregistering it, to make sure that a
1585 * 'real' IRQ doesn't run in * parallel with our fake. )
1587 if (action->flags & IRQF_SHARED) {
1588 local_irq_save(flags);
1589 action->handler(irq, dev_id);
1590 local_irq_restore(flags);
1594 if (action->thread) {
1595 kthread_stop(action->thread);
1596 put_task_struct(action->thread);
1597 if (action->secondary && action->secondary->thread) {
1598 kthread_stop(action->secondary->thread);
1599 put_task_struct(action->secondary->thread);
1603 module_put(desc->owner);
1604 kfree(action->secondary);
1609 * remove_irq - free an interrupt
1610 * @irq: Interrupt line to free
1611 * @act: irqaction for the interrupt
1613 * Used to remove interrupts statically setup by the early boot process.
1615 void remove_irq(unsigned int irq, struct irqaction *act)
1617 struct irq_desc *desc = irq_to_desc(irq);
1619 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1620 __free_irq(irq, act->dev_id);
1622 EXPORT_SYMBOL_GPL(remove_irq);
1625 * free_irq - free an interrupt allocated with request_irq
1626 * @irq: Interrupt line to free
1627 * @dev_id: Device identity to free
1629 * Remove an interrupt handler. The handler is removed and if the
1630 * interrupt line is no longer in use by any driver it is disabled.
1631 * On a shared IRQ the caller must ensure the interrupt is disabled
1632 * on the card it drives before calling this function. The function
1633 * does not return until any executing interrupts for this IRQ
1636 * This function must not be called from interrupt context.
1638 void free_irq(unsigned int irq, void *dev_id)
1640 struct irq_desc *desc = irq_to_desc(irq);
1642 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1646 if (WARN_ON(desc->affinity_notify))
1647 desc->affinity_notify = NULL;
1650 kfree(__free_irq(irq, dev_id));
1652 EXPORT_SYMBOL(free_irq);
1655 * request_threaded_irq - allocate an interrupt line
1656 * @irq: Interrupt line to allocate
1657 * @handler: Function to be called when the IRQ occurs.
1658 * Primary handler for threaded interrupts
1659 * If NULL and thread_fn != NULL the default
1660 * primary handler is installed
1661 * @thread_fn: Function called from the irq handler thread
1662 * If NULL, no irq thread is created
1663 * @irqflags: Interrupt type flags
1664 * @devname: An ascii name for the claiming device
1665 * @dev_id: A cookie passed back to the handler function
1667 * This call allocates interrupt resources and enables the
1668 * interrupt line and IRQ handling. From the point this
1669 * call is made your handler function may be invoked. Since
1670 * your handler function must clear any interrupt the board
1671 * raises, you must take care both to initialise your hardware
1672 * and to set up the interrupt handler in the right order.
1674 * If you want to set up a threaded irq handler for your device
1675 * then you need to supply @handler and @thread_fn. @handler is
1676 * still called in hard interrupt context and has to check
1677 * whether the interrupt originates from the device. If yes it
1678 * needs to disable the interrupt on the device and return
1679 * IRQ_WAKE_THREAD which will wake up the handler thread and run
1680 * @thread_fn. This split handler design is necessary to support
1681 * shared interrupts.
1683 * Dev_id must be globally unique. Normally the address of the
1684 * device data structure is used as the cookie. Since the handler
1685 * receives this value it makes sense to use it.
1687 * If your interrupt is shared you must pass a non NULL dev_id
1688 * as this is required when freeing the interrupt.
1692 * IRQF_SHARED Interrupt is shared
1693 * IRQF_TRIGGER_* Specify active edge(s) or level
1696 int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1697 irq_handler_t thread_fn, unsigned long irqflags,
1698 const char *devname, void *dev_id)
1700 struct irqaction *action;
1701 struct irq_desc *desc;
1705 * Sanity-check: shared interrupts must pass in a real dev-ID,
1706 * otherwise we'll have trouble later trying to figure out
1707 * which interrupt is which (messes up the interrupt freeing
1710 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1711 * it cannot be set along with IRQF_NO_SUSPEND.
1713 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1714 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1715 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
1718 desc = irq_to_desc(irq);
1722 if (!irq_settings_can_request(desc) ||
1723 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1729 handler = irq_default_primary_handler;
1732 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1736 action->handler = handler;
1737 action->thread_fn = thread_fn;
1738 action->flags = irqflags;
1739 action->name = devname;
1740 action->dev_id = dev_id;
1742 chip_bus_lock(desc);
1743 retval = __setup_irq(irq, desc, action);
1744 chip_bus_sync_unlock(desc);
1747 kfree(action->secondary);
1751 #ifdef CONFIG_DEBUG_SHIRQ_FIXME
1752 if (!retval && (irqflags & IRQF_SHARED)) {
1754 * It's a shared IRQ -- the driver ought to be prepared for it
1755 * to happen immediately, so let's make sure....
1756 * We disable the irq to make sure that a 'real' IRQ doesn't
1757 * run in parallel with our fake.
1759 unsigned long flags;
1762 local_irq_save(flags);
1764 handler(irq, dev_id);
1766 local_irq_restore(flags);
1772 EXPORT_SYMBOL(request_threaded_irq);
1775 * request_any_context_irq - allocate an interrupt line
1776 * @irq: Interrupt line to allocate
1777 * @handler: Function to be called when the IRQ occurs.
1778 * Threaded handler for threaded interrupts.
1779 * @flags: Interrupt type flags
1780 * @name: An ascii name for the claiming device
1781 * @dev_id: A cookie passed back to the handler function
1783 * This call allocates interrupt resources and enables the
1784 * interrupt line and IRQ handling. It selects either a
1785 * hardirq or threaded handling method depending on the
1788 * On failure, it returns a negative value. On success,
1789 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1791 int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1792 unsigned long flags, const char *name, void *dev_id)
1794 struct irq_desc *desc = irq_to_desc(irq);
1800 if (irq_settings_is_nested_thread(desc)) {
1801 ret = request_threaded_irq(irq, NULL, handler,
1802 flags, name, dev_id);
1803 return !ret ? IRQC_IS_NESTED : ret;
1806 ret = request_irq(irq, handler, flags, name, dev_id);
1807 return !ret ? IRQC_IS_HARDIRQ : ret;
1809 EXPORT_SYMBOL_GPL(request_any_context_irq);
1811 void enable_percpu_irq(unsigned int irq, unsigned int type)
1813 unsigned int cpu = smp_processor_id();
1814 unsigned long flags;
1815 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1820 type &= IRQ_TYPE_SENSE_MASK;
1821 if (type != IRQ_TYPE_NONE) {
1824 ret = __irq_set_trigger(desc, type);
1827 WARN(1, "failed to set type for IRQ%d\n", irq);
1832 irq_percpu_enable(desc, cpu);
1834 irq_put_desc_unlock(desc, flags);
1836 EXPORT_SYMBOL_GPL(enable_percpu_irq);
1838 void disable_percpu_irq(unsigned int irq)
1840 unsigned int cpu = smp_processor_id();
1841 unsigned long flags;
1842 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1847 irq_percpu_disable(desc, cpu);
1848 irq_put_desc_unlock(desc, flags);
1850 EXPORT_SYMBOL_GPL(disable_percpu_irq);
1853 * Internal function to unregister a percpu irqaction.
1855 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1857 struct irq_desc *desc = irq_to_desc(irq);
1858 struct irqaction *action;
1859 unsigned long flags;
1861 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1866 raw_spin_lock_irqsave(&desc->lock, flags);
1868 action = desc->action;
1869 if (!action || action->percpu_dev_id != dev_id) {
1870 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1874 if (!cpumask_empty(desc->percpu_enabled)) {
1875 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1876 irq, cpumask_first(desc->percpu_enabled));
1880 /* Found it - now remove it from the list of entries: */
1881 desc->action = NULL;
1883 raw_spin_unlock_irqrestore(&desc->lock, flags);
1885 unregister_handler_proc(irq, action);
1887 module_put(desc->owner);
1891 raw_spin_unlock_irqrestore(&desc->lock, flags);
1896 * remove_percpu_irq - free a per-cpu interrupt
1897 * @irq: Interrupt line to free
1898 * @act: irqaction for the interrupt
1900 * Used to remove interrupts statically setup by the early boot process.
1902 void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1904 struct irq_desc *desc = irq_to_desc(irq);
1906 if (desc && irq_settings_is_per_cpu_devid(desc))
1907 __free_percpu_irq(irq, act->percpu_dev_id);
1911 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
1912 * @irq: Interrupt line to free
1913 * @dev_id: Device identity to free
1915 * Remove a percpu interrupt handler. The handler is removed, but
1916 * the interrupt line is not disabled. This must be done on each
1917 * CPU before calling this function. The function does not return
1918 * until any executing interrupts for this IRQ have completed.
1920 * This function must not be called from interrupt context.
1922 void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1924 struct irq_desc *desc = irq_to_desc(irq);
1926 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1929 chip_bus_lock(desc);
1930 kfree(__free_percpu_irq(irq, dev_id));
1931 chip_bus_sync_unlock(desc);
1933 EXPORT_SYMBOL_GPL(free_percpu_irq);
1936 * setup_percpu_irq - setup a per-cpu interrupt
1937 * @irq: Interrupt line to setup
1938 * @act: irqaction for the interrupt
1940 * Used to statically setup per-cpu interrupts in the early boot process.
1942 int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1944 struct irq_desc *desc = irq_to_desc(irq);
1947 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1949 chip_bus_lock(desc);
1950 retval = __setup_irq(irq, desc, act);
1951 chip_bus_sync_unlock(desc);
1957 * request_percpu_irq - allocate a percpu interrupt line
1958 * @irq: Interrupt line to allocate
1959 * @handler: Function to be called when the IRQ occurs.
1960 * @devname: An ascii name for the claiming device
1961 * @dev_id: A percpu cookie passed back to the handler function
1963 * This call allocates interrupt resources and enables the
1964 * interrupt on the local CPU. If the interrupt is supposed to be
1965 * enabled on other CPUs, it has to be done on each CPU using
1966 * enable_percpu_irq().
1968 * Dev_id must be globally unique. It is a per-cpu variable, and
1969 * the handler gets called with the interrupted CPU's instance of
1972 int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1973 const char *devname, void __percpu *dev_id)
1975 struct irqaction *action;
1976 struct irq_desc *desc;
1982 desc = irq_to_desc(irq);
1983 if (!desc || !irq_settings_can_request(desc) ||
1984 !irq_settings_is_per_cpu_devid(desc))
1987 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1991 action->handler = handler;
1992 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
1993 action->name = devname;
1994 action->percpu_dev_id = dev_id;
1996 chip_bus_lock(desc);
1997 retval = __setup_irq(irq, desc, action);
1998 chip_bus_sync_unlock(desc);
2005 EXPORT_SYMBOL_GPL(request_percpu_irq);
2008 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2009 * @irq: Interrupt line that is forwarded to a VM
2010 * @which: One of IRQCHIP_STATE_* the caller wants to know about
2011 * @state: a pointer to a boolean where the state is to be storeed
2013 * This call snapshots the internal irqchip state of an
2014 * interrupt, returning into @state the bit corresponding to
2017 * This function should be called with preemption disabled if the
2018 * interrupt controller has per-cpu registers.
2020 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2023 struct irq_desc *desc;
2024 struct irq_data *data;
2025 struct irq_chip *chip;
2026 unsigned long flags;
2029 desc = irq_get_desc_buslock(irq, &flags, 0);
2033 data = irq_desc_get_irq_data(desc);
2036 chip = irq_data_get_irq_chip(data);
2037 if (chip->irq_get_irqchip_state)
2039 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2040 data = data->parent_data;
2047 err = chip->irq_get_irqchip_state(data, which, state);
2049 irq_put_desc_busunlock(desc, flags);
2052 EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
2055 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2056 * @irq: Interrupt line that is forwarded to a VM
2057 * @which: State to be restored (one of IRQCHIP_STATE_*)
2058 * @val: Value corresponding to @which
2060 * This call sets the internal irqchip state of an interrupt,
2061 * depending on the value of @which.
2063 * This function should be called with migration disabled if the
2064 * interrupt controller has per-cpu registers.
2066 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2069 struct irq_desc *desc;
2070 struct irq_data *data;
2071 struct irq_chip *chip;
2072 unsigned long flags;
2075 desc = irq_get_desc_buslock(irq, &flags, 0);
2079 data = irq_desc_get_irq_data(desc);
2082 chip = irq_data_get_irq_chip(data);
2083 if (chip->irq_set_irqchip_state)
2085 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2086 data = data->parent_data;
2093 err = chip->irq_set_irqchip_state(data, which, val);
2095 irq_put_desc_busunlock(desc, flags);
2098 EXPORT_SYMBOL_GPL(irq_set_irqchip_state);