Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / kernel / irq / manage.c
blob78f3ddeb7fe44a297229ebd65f28e591f4e3a56f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
4 * Copyright (C) 2005-2006 Thomas Gleixner
6 * This file contains driver APIs to the irq subsystem.
7 */
9 #define pr_fmt(fmt) "genirq: " fmt
11 #include <linux/irq.h>
12 #include <linux/kthread.h>
13 #include <linux/module.h>
14 #include <linux/random.h>
15 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/sched.h>
18 #include <linux/sched/rt.h>
19 #include <linux/sched/task.h>
20 #include <uapi/linux/sched/types.h>
21 #include <linux/task_work.h>
23 #include "internals.h"
25 #ifdef CONFIG_IRQ_FORCED_THREADING
26 __read_mostly bool force_irqthreads;
27 EXPORT_SYMBOL_GPL(force_irqthreads);
29 static int __init setup_forced_irqthreads(char *arg)
31 force_irqthreads = true;
32 return 0;
34 early_param("threadirqs", setup_forced_irqthreads);
35 #endif
37 static void __synchronize_hardirq(struct irq_desc *desc)
39 bool inprogress;
41 do {
42 unsigned long flags;
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))
49 cpu_relax();
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? */
57 } while (inprogress);
60 /**
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
68 * into account.
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);
81 if (desc) {
82 __synchronize_hardirq(desc);
83 return !atomic_read(&desc->threads_active);
86 return true;
88 EXPORT_SYMBOL(synchronize_hardirq);
90 /**
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);
104 if (desc) {
105 __synchronize_hardirq(desc);
107 * We made sure that no hardirq handler is
108 * running. Now verify that no threaded handlers are
109 * active.
111 wait_event(desc->wait_for_threads,
112 !atomic_read(&desc->threads_active));
115 EXPORT_SYMBOL(synchronize_irq);
117 #ifdef CONFIG_SMP
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)
124 return false;
125 return true;
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)
167 if (action->thread)
168 set_bit(IRQTF_AFFINITY, &action->thread_flags);
171 static void irq_validate_effective_affinity(struct irq_data *data)
173 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
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))
178 return;
179 pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n",
180 chip->name, data->irq);
181 #endif
184 int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
185 bool force)
187 struct irq_desc *desc = irq_data_to_desc(data);
188 struct irq_chip *chip = irq_data_get_irq_chip(data);
189 int ret;
191 if (!chip || !chip->irq_set_affinity)
192 return -EINVAL;
194 ret = chip->irq_set_affinity(data, mask, force);
195 switch (ret) {
196 case IRQ_SET_MASK_OK:
197 case IRQ_SET_MASK_OK_DONE:
198 cpumask_copy(desc->irq_common_data.affinity, mask);
199 /* fall through */
200 case IRQ_SET_MASK_OK_NOCOPY:
201 irq_validate_effective_affinity(data);
202 irq_set_thread_affinity(desc);
203 ret = 0;
206 return ret;
209 #ifdef CONFIG_GENERIC_PENDING_IRQ
210 static inline int irq_set_affinity_pending(struct irq_data *data,
211 const struct cpumask *dest)
213 struct irq_desc *desc = irq_data_to_desc(data);
215 irqd_set_move_pending(data);
216 irq_copy_pending(desc, dest);
217 return 0;
219 #else
220 static inline int irq_set_affinity_pending(struct irq_data *data,
221 const struct cpumask *dest)
223 return -EBUSY;
225 #endif
227 static int irq_try_set_affinity(struct irq_data *data,
228 const struct cpumask *dest, bool force)
230 int ret = irq_do_set_affinity(data, dest, force);
233 * In case that the underlying vector management is busy and the
234 * architecture supports the generic pending mechanism then utilize
235 * this to avoid returning an error to user space.
237 if (ret == -EBUSY && !force)
238 ret = irq_set_affinity_pending(data, dest);
239 return ret;
242 int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
243 bool force)
245 struct irq_chip *chip = irq_data_get_irq_chip(data);
246 struct irq_desc *desc = irq_data_to_desc(data);
247 int ret = 0;
249 if (!chip || !chip->irq_set_affinity)
250 return -EINVAL;
252 if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) {
253 ret = irq_try_set_affinity(data, mask, force);
254 } else {
255 irqd_set_move_pending(data);
256 irq_copy_pending(desc, mask);
259 if (desc->affinity_notify) {
260 kref_get(&desc->affinity_notify->kref);
261 schedule_work(&desc->affinity_notify->work);
263 irqd_set(data, IRQD_AFFINITY_SET);
265 return ret;
268 int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
270 struct irq_desc *desc = irq_to_desc(irq);
271 unsigned long flags;
272 int ret;
274 if (!desc)
275 return -EINVAL;
277 raw_spin_lock_irqsave(&desc->lock, flags);
278 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
279 raw_spin_unlock_irqrestore(&desc->lock, flags);
280 return ret;
283 int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
285 unsigned long flags;
286 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
288 if (!desc)
289 return -EINVAL;
290 desc->affinity_hint = m;
291 irq_put_desc_unlock(desc, flags);
292 /* set the initial affinity to prevent every interrupt being on CPU0 */
293 if (m)
294 __irq_set_affinity(irq, m, false);
295 return 0;
297 EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
299 static void irq_affinity_notify(struct work_struct *work)
301 struct irq_affinity_notify *notify =
302 container_of(work, struct irq_affinity_notify, work);
303 struct irq_desc *desc = irq_to_desc(notify->irq);
304 cpumask_var_t cpumask;
305 unsigned long flags;
307 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
308 goto out;
310 raw_spin_lock_irqsave(&desc->lock, flags);
311 if (irq_move_pending(&desc->irq_data))
312 irq_get_pending(cpumask, desc);
313 else
314 cpumask_copy(cpumask, desc->irq_common_data.affinity);
315 raw_spin_unlock_irqrestore(&desc->lock, flags);
317 notify->notify(notify, cpumask);
319 free_cpumask_var(cpumask);
320 out:
321 kref_put(&notify->kref, notify->release);
325 * irq_set_affinity_notifier - control notification of IRQ affinity changes
326 * @irq: Interrupt for which to enable/disable notification
327 * @notify: Context for notification, or %NULL to disable
328 * notification. Function pointers must be initialised;
329 * the other fields will be initialised by this function.
331 * Must be called in process context. Notification may only be enabled
332 * after the IRQ is allocated and must be disabled before the IRQ is
333 * freed using free_irq().
336 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
338 struct irq_desc *desc = irq_to_desc(irq);
339 struct irq_affinity_notify *old_notify;
340 unsigned long flags;
342 /* The release function is promised process context */
343 might_sleep();
345 if (!desc || desc->istate & IRQS_NMI)
346 return -EINVAL;
348 /* Complete initialisation of *notify */
349 if (notify) {
350 notify->irq = irq;
351 kref_init(&notify->kref);
352 INIT_WORK(&notify->work, irq_affinity_notify);
355 raw_spin_lock_irqsave(&desc->lock, flags);
356 old_notify = desc->affinity_notify;
357 desc->affinity_notify = notify;
358 raw_spin_unlock_irqrestore(&desc->lock, flags);
360 if (old_notify) {
361 cancel_work_sync(&old_notify->work);
362 kref_put(&old_notify->kref, old_notify->release);
365 return 0;
367 EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
369 #ifndef CONFIG_AUTO_IRQ_AFFINITY
371 * Generic version of the affinity autoselector.
373 int irq_setup_affinity(struct irq_desc *desc)
375 struct cpumask *set = irq_default_affinity;
376 int ret, node = irq_desc_get_node(desc);
377 static DEFINE_RAW_SPINLOCK(mask_lock);
378 static struct cpumask mask;
380 /* Excludes PER_CPU and NO_BALANCE interrupts */
381 if (!__irq_can_set_affinity(desc))
382 return 0;
384 raw_spin_lock(&mask_lock);
386 * Preserve the managed affinity setting and a userspace affinity
387 * setup, but make sure that one of the targets is online.
389 if (irqd_affinity_is_managed(&desc->irq_data) ||
390 irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
391 if (cpumask_intersects(desc->irq_common_data.affinity,
392 cpu_online_mask))
393 set = desc->irq_common_data.affinity;
394 else
395 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
398 cpumask_and(&mask, cpu_online_mask, set);
399 if (cpumask_empty(&mask))
400 cpumask_copy(&mask, cpu_online_mask);
402 if (node != NUMA_NO_NODE) {
403 const struct cpumask *nodemask = cpumask_of_node(node);
405 /* make sure at least one of the cpus in nodemask is online */
406 if (cpumask_intersects(&mask, nodemask))
407 cpumask_and(&mask, &mask, nodemask);
409 ret = irq_do_set_affinity(&desc->irq_data, &mask, false);
410 raw_spin_unlock(&mask_lock);
411 return ret;
413 #else
414 /* Wrapper for ALPHA specific affinity selector magic */
415 int irq_setup_affinity(struct irq_desc *desc)
417 return irq_select_affinity(irq_desc_get_irq(desc));
419 #endif
422 * Called when a bogus affinity is set via /proc/irq
424 int irq_select_affinity_usr(unsigned int irq)
426 struct irq_desc *desc = irq_to_desc(irq);
427 unsigned long flags;
428 int ret;
430 raw_spin_lock_irqsave(&desc->lock, flags);
431 ret = irq_setup_affinity(desc);
432 raw_spin_unlock_irqrestore(&desc->lock, flags);
433 return ret;
435 #endif
438 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
439 * @irq: interrupt number to set affinity
440 * @vcpu_info: vCPU specific data or pointer to a percpu array of vCPU
441 * specific data for percpu_devid interrupts
443 * This function uses the vCPU specific data to set the vCPU
444 * affinity for an irq. The vCPU specific data is passed from
445 * outside, such as KVM. One example code path is as below:
446 * KVM -> IOMMU -> irq_set_vcpu_affinity().
448 int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
450 unsigned long flags;
451 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
452 struct irq_data *data;
453 struct irq_chip *chip;
454 int ret = -ENOSYS;
456 if (!desc)
457 return -EINVAL;
459 data = irq_desc_get_irq_data(desc);
460 do {
461 chip = irq_data_get_irq_chip(data);
462 if (chip && chip->irq_set_vcpu_affinity)
463 break;
464 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
465 data = data->parent_data;
466 #else
467 data = NULL;
468 #endif
469 } while (data);
471 if (data)
472 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
473 irq_put_desc_unlock(desc, flags);
475 return ret;
477 EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
479 void __disable_irq(struct irq_desc *desc)
481 if (!desc->depth++)
482 irq_disable(desc);
485 static int __disable_irq_nosync(unsigned int irq)
487 unsigned long flags;
488 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
490 if (!desc)
491 return -EINVAL;
492 __disable_irq(desc);
493 irq_put_desc_busunlock(desc, flags);
494 return 0;
498 * disable_irq_nosync - disable an irq without waiting
499 * @irq: Interrupt to disable
501 * Disable the selected interrupt line. Disables and Enables are
502 * nested.
503 * Unlike disable_irq(), this function does not ensure existing
504 * instances of the IRQ handler have completed before returning.
506 * This function may be called from IRQ context.
508 void disable_irq_nosync(unsigned int irq)
510 __disable_irq_nosync(irq);
512 EXPORT_SYMBOL(disable_irq_nosync);
515 * disable_irq - disable an irq and wait for completion
516 * @irq: Interrupt to disable
518 * Disable the selected interrupt line. Enables and Disables are
519 * nested.
520 * This function waits for any pending IRQ handlers for this interrupt
521 * to complete before returning. If you use this function while
522 * holding a resource the IRQ handler may need you will deadlock.
524 * This function may be called - with care - from IRQ context.
526 void disable_irq(unsigned int irq)
528 if (!__disable_irq_nosync(irq))
529 synchronize_irq(irq);
531 EXPORT_SYMBOL(disable_irq);
534 * disable_hardirq - disables an irq and waits for hardirq completion
535 * @irq: Interrupt to disable
537 * Disable the selected interrupt line. Enables and Disables are
538 * nested.
539 * This function waits for any pending hard IRQ handlers for this
540 * interrupt to complete before returning. If you use this function while
541 * holding a resource the hard IRQ handler may need you will deadlock.
543 * When used to optimistically disable an interrupt from atomic context
544 * the return value must be checked.
546 * Returns: false if a threaded handler is active.
548 * This function may be called - with care - from IRQ context.
550 bool disable_hardirq(unsigned int irq)
552 if (!__disable_irq_nosync(irq))
553 return synchronize_hardirq(irq);
555 return false;
557 EXPORT_SYMBOL_GPL(disable_hardirq);
560 * disable_nmi_nosync - disable an nmi without waiting
561 * @irq: Interrupt to disable
563 * Disable the selected interrupt line. Disables and enables are
564 * nested.
565 * The interrupt to disable must have been requested through request_nmi.
566 * Unlike disable_nmi(), this function does not ensure existing
567 * instances of the IRQ handler have completed before returning.
569 void disable_nmi_nosync(unsigned int irq)
571 disable_irq_nosync(irq);
574 void __enable_irq(struct irq_desc *desc)
576 switch (desc->depth) {
577 case 0:
578 err_out:
579 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
580 irq_desc_get_irq(desc));
581 break;
582 case 1: {
583 if (desc->istate & IRQS_SUSPENDED)
584 goto err_out;
585 /* Prevent probing on this irq: */
586 irq_settings_set_noprobe(desc);
588 * Call irq_startup() not irq_enable() here because the
589 * interrupt might be marked NOAUTOEN. So irq_startup()
590 * needs to be invoked when it gets enabled the first
591 * time. If it was already started up, then irq_startup()
592 * will invoke irq_enable() under the hood.
594 irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE);
595 break;
597 default:
598 desc->depth--;
603 * enable_irq - enable handling of an irq
604 * @irq: Interrupt to enable
606 * Undoes the effect of one call to disable_irq(). If this
607 * matches the last disable, processing of interrupts on this
608 * IRQ line is re-enabled.
610 * This function may be called from IRQ context only when
611 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
613 void enable_irq(unsigned int irq)
615 unsigned long flags;
616 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
618 if (!desc)
619 return;
620 if (WARN(!desc->irq_data.chip,
621 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
622 goto out;
624 __enable_irq(desc);
625 out:
626 irq_put_desc_busunlock(desc, flags);
628 EXPORT_SYMBOL(enable_irq);
631 * enable_nmi - enable handling of an nmi
632 * @irq: Interrupt to enable
634 * The interrupt to enable must have been requested through request_nmi.
635 * Undoes the effect of one call to disable_nmi(). If this
636 * matches the last disable, processing of interrupts on this
637 * IRQ line is re-enabled.
639 void enable_nmi(unsigned int irq)
641 enable_irq(irq);
644 static int set_irq_wake_real(unsigned int irq, unsigned int on)
646 struct irq_desc *desc = irq_to_desc(irq);
647 int ret = -ENXIO;
649 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
650 return 0;
652 if (desc->irq_data.chip->irq_set_wake)
653 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
655 return ret;
659 * irq_set_irq_wake - control irq power management wakeup
660 * @irq: interrupt to control
661 * @on: enable/disable power management wakeup
663 * Enable/disable power management wakeup mode, which is
664 * disabled by default. Enables and disables must match,
665 * just as they match for non-wakeup mode support.
667 * Wakeup mode lets this IRQ wake the system from sleep
668 * states like "suspend to RAM".
670 int irq_set_irq_wake(unsigned int irq, unsigned int on)
672 unsigned long flags;
673 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
674 int ret = 0;
676 if (!desc)
677 return -EINVAL;
679 /* Don't use NMIs as wake up interrupts please */
680 if (desc->istate & IRQS_NMI) {
681 ret = -EINVAL;
682 goto out_unlock;
685 /* wakeup-capable irqs can be shared between drivers that
686 * don't need to have the same sleep mode behaviors.
688 if (on) {
689 if (desc->wake_depth++ == 0) {
690 ret = set_irq_wake_real(irq, on);
691 if (ret)
692 desc->wake_depth = 0;
693 else
694 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
696 } else {
697 if (desc->wake_depth == 0) {
698 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
699 } else if (--desc->wake_depth == 0) {
700 ret = set_irq_wake_real(irq, on);
701 if (ret)
702 desc->wake_depth = 1;
703 else
704 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
708 out_unlock:
709 irq_put_desc_busunlock(desc, flags);
710 return ret;
712 EXPORT_SYMBOL(irq_set_irq_wake);
715 * Internal function that tells the architecture code whether a
716 * particular irq has been exclusively allocated or is available
717 * for driver use.
719 int can_request_irq(unsigned int irq, unsigned long irqflags)
721 unsigned long flags;
722 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
723 int canrequest = 0;
725 if (!desc)
726 return 0;
728 if (irq_settings_can_request(desc)) {
729 if (!desc->action ||
730 irqflags & desc->action->flags & IRQF_SHARED)
731 canrequest = 1;
733 irq_put_desc_unlock(desc, flags);
734 return canrequest;
737 int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
739 struct irq_chip *chip = desc->irq_data.chip;
740 int ret, unmask = 0;
742 if (!chip || !chip->irq_set_type) {
744 * IRQF_TRIGGER_* but the PIC does not support multiple
745 * flow-types?
747 pr_debug("No set_type function for IRQ %d (%s)\n",
748 irq_desc_get_irq(desc),
749 chip ? (chip->name ? : "unknown") : "unknown");
750 return 0;
753 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
754 if (!irqd_irq_masked(&desc->irq_data))
755 mask_irq(desc);
756 if (!irqd_irq_disabled(&desc->irq_data))
757 unmask = 1;
760 /* Mask all flags except trigger mode */
761 flags &= IRQ_TYPE_SENSE_MASK;
762 ret = chip->irq_set_type(&desc->irq_data, flags);
764 switch (ret) {
765 case IRQ_SET_MASK_OK:
766 case IRQ_SET_MASK_OK_DONE:
767 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
768 irqd_set(&desc->irq_data, flags);
769 /* fall through */
771 case IRQ_SET_MASK_OK_NOCOPY:
772 flags = irqd_get_trigger_type(&desc->irq_data);
773 irq_settings_set_trigger_mask(desc, flags);
774 irqd_clear(&desc->irq_data, IRQD_LEVEL);
775 irq_settings_clr_level(desc);
776 if (flags & IRQ_TYPE_LEVEL_MASK) {
777 irq_settings_set_level(desc);
778 irqd_set(&desc->irq_data, IRQD_LEVEL);
781 ret = 0;
782 break;
783 default:
784 pr_err("Setting trigger mode %lu for irq %u failed (%pS)\n",
785 flags, irq_desc_get_irq(desc), chip->irq_set_type);
787 if (unmask)
788 unmask_irq(desc);
789 return ret;
792 #ifdef CONFIG_HARDIRQS_SW_RESEND
793 int irq_set_parent(int irq, int parent_irq)
795 unsigned long flags;
796 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
798 if (!desc)
799 return -EINVAL;
801 desc->parent_irq = parent_irq;
803 irq_put_desc_unlock(desc, flags);
804 return 0;
806 EXPORT_SYMBOL_GPL(irq_set_parent);
807 #endif
810 * Default primary interrupt handler for threaded interrupts. Is
811 * assigned as primary handler when request_threaded_irq is called
812 * with handler == NULL. Useful for oneshot interrupts.
814 static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
816 return IRQ_WAKE_THREAD;
820 * Primary handler for nested threaded interrupts. Should never be
821 * called.
823 static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
825 WARN(1, "Primary handler called for nested irq %d\n", irq);
826 return IRQ_NONE;
829 static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
831 WARN(1, "Secondary action handler called for irq %d\n", irq);
832 return IRQ_NONE;
835 static int irq_wait_for_interrupt(struct irqaction *action)
837 for (;;) {
838 set_current_state(TASK_INTERRUPTIBLE);
840 if (kthread_should_stop()) {
841 /* may need to run one last time */
842 if (test_and_clear_bit(IRQTF_RUNTHREAD,
843 &action->thread_flags)) {
844 __set_current_state(TASK_RUNNING);
845 return 0;
847 __set_current_state(TASK_RUNNING);
848 return -1;
851 if (test_and_clear_bit(IRQTF_RUNTHREAD,
852 &action->thread_flags)) {
853 __set_current_state(TASK_RUNNING);
854 return 0;
856 schedule();
861 * Oneshot interrupts keep the irq line masked until the threaded
862 * handler finished. unmask if the interrupt has not been disabled and
863 * is marked MASKED.
865 static void irq_finalize_oneshot(struct irq_desc *desc,
866 struct irqaction *action)
868 if (!(desc->istate & IRQS_ONESHOT) ||
869 action->handler == irq_forced_secondary_handler)
870 return;
871 again:
872 chip_bus_lock(desc);
873 raw_spin_lock_irq(&desc->lock);
876 * Implausible though it may be we need to protect us against
877 * the following scenario:
879 * The thread is faster done than the hard interrupt handler
880 * on the other CPU. If we unmask the irq line then the
881 * interrupt can come in again and masks the line, leaves due
882 * to IRQS_INPROGRESS and the irq line is masked forever.
884 * This also serializes the state of shared oneshot handlers
885 * versus "desc->threads_onehsot |= action->thread_mask;" in
886 * irq_wake_thread(). See the comment there which explains the
887 * serialization.
889 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
890 raw_spin_unlock_irq(&desc->lock);
891 chip_bus_sync_unlock(desc);
892 cpu_relax();
893 goto again;
897 * Now check again, whether the thread should run. Otherwise
898 * we would clear the threads_oneshot bit of this thread which
899 * was just set.
901 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
902 goto out_unlock;
904 desc->threads_oneshot &= ~action->thread_mask;
906 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
907 irqd_irq_masked(&desc->irq_data))
908 unmask_threaded_irq(desc);
910 out_unlock:
911 raw_spin_unlock_irq(&desc->lock);
912 chip_bus_sync_unlock(desc);
915 #ifdef CONFIG_SMP
917 * Check whether we need to change the affinity of the interrupt thread.
919 static void
920 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
922 cpumask_var_t mask;
923 bool valid = true;
925 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
926 return;
929 * In case we are out of memory we set IRQTF_AFFINITY again and
930 * try again next time
932 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
933 set_bit(IRQTF_AFFINITY, &action->thread_flags);
934 return;
937 raw_spin_lock_irq(&desc->lock);
939 * This code is triggered unconditionally. Check the affinity
940 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
942 if (cpumask_available(desc->irq_common_data.affinity)) {
943 const struct cpumask *m;
945 m = irq_data_get_effective_affinity_mask(&desc->irq_data);
946 cpumask_copy(mask, m);
947 } else {
948 valid = false;
950 raw_spin_unlock_irq(&desc->lock);
952 if (valid)
953 set_cpus_allowed_ptr(current, mask);
954 free_cpumask_var(mask);
956 #else
957 static inline void
958 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
959 #endif
962 * Interrupts which are not explicitly requested as threaded
963 * interrupts rely on the implicit bh/preempt disable of the hard irq
964 * context. So we need to disable bh here to avoid deadlocks and other
965 * side effects.
967 static irqreturn_t
968 irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
970 irqreturn_t ret;
972 local_bh_disable();
973 ret = action->thread_fn(action->irq, action->dev_id);
974 if (ret == IRQ_HANDLED)
975 atomic_inc(&desc->threads_handled);
977 irq_finalize_oneshot(desc, action);
978 local_bh_enable();
979 return ret;
983 * Interrupts explicitly requested as threaded interrupts want to be
984 * preemtible - many of them need to sleep and wait for slow busses to
985 * complete.
987 static irqreturn_t irq_thread_fn(struct irq_desc *desc,
988 struct irqaction *action)
990 irqreturn_t ret;
992 ret = action->thread_fn(action->irq, action->dev_id);
993 if (ret == IRQ_HANDLED)
994 atomic_inc(&desc->threads_handled);
996 irq_finalize_oneshot(desc, action);
997 return ret;
1000 static void wake_threads_waitq(struct irq_desc *desc)
1002 if (atomic_dec_and_test(&desc->threads_active))
1003 wake_up(&desc->wait_for_threads);
1006 static void irq_thread_dtor(struct callback_head *unused)
1008 struct task_struct *tsk = current;
1009 struct irq_desc *desc;
1010 struct irqaction *action;
1012 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
1013 return;
1015 action = kthread_data(tsk);
1017 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
1018 tsk->comm, tsk->pid, action->irq);
1021 desc = irq_to_desc(action->irq);
1023 * If IRQTF_RUNTHREAD is set, we need to decrement
1024 * desc->threads_active and wake possible waiters.
1026 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
1027 wake_threads_waitq(desc);
1029 /* Prevent a stale desc->threads_oneshot */
1030 irq_finalize_oneshot(desc, action);
1033 static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
1035 struct irqaction *secondary = action->secondary;
1037 if (WARN_ON_ONCE(!secondary))
1038 return;
1040 raw_spin_lock_irq(&desc->lock);
1041 __irq_wake_thread(desc, secondary);
1042 raw_spin_unlock_irq(&desc->lock);
1046 * Interrupt handler thread
1048 static int irq_thread(void *data)
1050 struct callback_head on_exit_work;
1051 struct irqaction *action = data;
1052 struct irq_desc *desc = irq_to_desc(action->irq);
1053 irqreturn_t (*handler_fn)(struct irq_desc *desc,
1054 struct irqaction *action);
1056 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
1057 &action->thread_flags))
1058 handler_fn = irq_forced_thread_fn;
1059 else
1060 handler_fn = irq_thread_fn;
1062 init_task_work(&on_exit_work, irq_thread_dtor);
1063 task_work_add(current, &on_exit_work, false);
1065 irq_thread_check_affinity(desc, action);
1067 while (!irq_wait_for_interrupt(action)) {
1068 irqreturn_t action_ret;
1070 irq_thread_check_affinity(desc, action);
1072 action_ret = handler_fn(desc, action);
1073 if (action_ret == IRQ_WAKE_THREAD)
1074 irq_wake_secondary(desc, action);
1076 wake_threads_waitq(desc);
1080 * This is the regular exit path. __free_irq() is stopping the
1081 * thread via kthread_stop() after calling
1082 * synchronize_hardirq(). So neither IRQTF_RUNTHREAD nor the
1083 * oneshot mask bit can be set.
1085 task_work_cancel(current, irq_thread_dtor);
1086 return 0;
1090 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1091 * @irq: Interrupt line
1092 * @dev_id: Device identity for which the thread should be woken
1095 void irq_wake_thread(unsigned int irq, void *dev_id)
1097 struct irq_desc *desc = irq_to_desc(irq);
1098 struct irqaction *action;
1099 unsigned long flags;
1101 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1102 return;
1104 raw_spin_lock_irqsave(&desc->lock, flags);
1105 for_each_action_of_desc(desc, action) {
1106 if (action->dev_id == dev_id) {
1107 if (action->thread)
1108 __irq_wake_thread(desc, action);
1109 break;
1112 raw_spin_unlock_irqrestore(&desc->lock, flags);
1114 EXPORT_SYMBOL_GPL(irq_wake_thread);
1116 static int irq_setup_forced_threading(struct irqaction *new)
1118 if (!force_irqthreads)
1119 return 0;
1120 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1121 return 0;
1124 * No further action required for interrupts which are requested as
1125 * threaded interrupts already
1127 if (new->handler == irq_default_primary_handler)
1128 return 0;
1130 new->flags |= IRQF_ONESHOT;
1133 * Handle the case where we have a real primary handler and a
1134 * thread handler. We force thread them as well by creating a
1135 * secondary action.
1137 if (new->handler && new->thread_fn) {
1138 /* Allocate the secondary action */
1139 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1140 if (!new->secondary)
1141 return -ENOMEM;
1142 new->secondary->handler = irq_forced_secondary_handler;
1143 new->secondary->thread_fn = new->thread_fn;
1144 new->secondary->dev_id = new->dev_id;
1145 new->secondary->irq = new->irq;
1146 new->secondary->name = new->name;
1148 /* Deal with the primary handler */
1149 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1150 new->thread_fn = new->handler;
1151 new->handler = irq_default_primary_handler;
1152 return 0;
1155 static int irq_request_resources(struct irq_desc *desc)
1157 struct irq_data *d = &desc->irq_data;
1158 struct irq_chip *c = d->chip;
1160 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1163 static void irq_release_resources(struct irq_desc *desc)
1165 struct irq_data *d = &desc->irq_data;
1166 struct irq_chip *c = d->chip;
1168 if (c->irq_release_resources)
1169 c->irq_release_resources(d);
1172 static bool irq_supports_nmi(struct irq_desc *desc)
1174 struct irq_data *d = irq_desc_get_irq_data(desc);
1176 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1177 /* Only IRQs directly managed by the root irqchip can be set as NMI */
1178 if (d->parent_data)
1179 return false;
1180 #endif
1181 /* Don't support NMIs for chips behind a slow bus */
1182 if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock)
1183 return false;
1185 return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
1188 static int irq_nmi_setup(struct irq_desc *desc)
1190 struct irq_data *d = irq_desc_get_irq_data(desc);
1191 struct irq_chip *c = d->chip;
1193 return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL;
1196 static void irq_nmi_teardown(struct irq_desc *desc)
1198 struct irq_data *d = irq_desc_get_irq_data(desc);
1199 struct irq_chip *c = d->chip;
1201 if (c->irq_nmi_teardown)
1202 c->irq_nmi_teardown(d);
1205 static int
1206 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1208 struct task_struct *t;
1209 struct sched_param param = {
1210 .sched_priority = MAX_USER_RT_PRIO/2,
1213 if (!secondary) {
1214 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1215 new->name);
1216 } else {
1217 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1218 new->name);
1219 param.sched_priority -= 1;
1222 if (IS_ERR(t))
1223 return PTR_ERR(t);
1225 sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1228 * We keep the reference to the task struct even if
1229 * the thread dies to avoid that the interrupt code
1230 * references an already freed task_struct.
1232 get_task_struct(t);
1233 new->thread = t;
1235 * Tell the thread to set its affinity. This is
1236 * important for shared interrupt handlers as we do
1237 * not invoke setup_affinity() for the secondary
1238 * handlers as everything is already set up. Even for
1239 * interrupts marked with IRQF_NO_BALANCE this is
1240 * correct as we want the thread to move to the cpu(s)
1241 * on which the requesting code placed the interrupt.
1243 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1244 return 0;
1248 * Internal function to register an irqaction - typically used to
1249 * allocate special interrupts that are part of the architecture.
1251 * Locking rules:
1253 * desc->request_mutex Provides serialization against a concurrent free_irq()
1254 * chip_bus_lock Provides serialization for slow bus operations
1255 * desc->lock Provides serialization against hard interrupts
1257 * chip_bus_lock and desc->lock are sufficient for all other management and
1258 * interrupt related functions. desc->request_mutex solely serializes
1259 * request/free_irq().
1261 static int
1262 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1264 struct irqaction *old, **old_ptr;
1265 unsigned long flags, thread_mask = 0;
1266 int ret, nested, shared = 0;
1268 if (!desc)
1269 return -EINVAL;
1271 if (desc->irq_data.chip == &no_irq_chip)
1272 return -ENOSYS;
1273 if (!try_module_get(desc->owner))
1274 return -ENODEV;
1276 new->irq = irq;
1279 * If the trigger type is not specified by the caller,
1280 * then use the default for this interrupt.
1282 if (!(new->flags & IRQF_TRIGGER_MASK))
1283 new->flags |= irqd_get_trigger_type(&desc->irq_data);
1286 * Check whether the interrupt nests into another interrupt
1287 * thread.
1289 nested = irq_settings_is_nested_thread(desc);
1290 if (nested) {
1291 if (!new->thread_fn) {
1292 ret = -EINVAL;
1293 goto out_mput;
1296 * Replace the primary handler which was provided from
1297 * the driver for non nested interrupt handling by the
1298 * dummy function which warns when called.
1300 new->handler = irq_nested_primary_handler;
1301 } else {
1302 if (irq_settings_can_thread(desc)) {
1303 ret = irq_setup_forced_threading(new);
1304 if (ret)
1305 goto out_mput;
1310 * Create a handler thread when a thread function is supplied
1311 * and the interrupt does not nest into another interrupt
1312 * thread.
1314 if (new->thread_fn && !nested) {
1315 ret = setup_irq_thread(new, irq, false);
1316 if (ret)
1317 goto out_mput;
1318 if (new->secondary) {
1319 ret = setup_irq_thread(new->secondary, irq, true);
1320 if (ret)
1321 goto out_thread;
1326 * Drivers are often written to work w/o knowledge about the
1327 * underlying irq chip implementation, so a request for a
1328 * threaded irq without a primary hard irq context handler
1329 * requires the ONESHOT flag to be set. Some irq chips like
1330 * MSI based interrupts are per se one shot safe. Check the
1331 * chip flags, so we can avoid the unmask dance at the end of
1332 * the threaded handler for those.
1334 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1335 new->flags &= ~IRQF_ONESHOT;
1338 * Protects against a concurrent __free_irq() call which might wait
1339 * for synchronize_hardirq() to complete without holding the optional
1340 * chip bus lock and desc->lock. Also protects against handing out
1341 * a recycled oneshot thread_mask bit while it's still in use by
1342 * its previous owner.
1344 mutex_lock(&desc->request_mutex);
1347 * Acquire bus lock as the irq_request_resources() callback below
1348 * might rely on the serialization or the magic power management
1349 * functions which are abusing the irq_bus_lock() callback,
1351 chip_bus_lock(desc);
1353 /* First installed action requests resources. */
1354 if (!desc->action) {
1355 ret = irq_request_resources(desc);
1356 if (ret) {
1357 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1358 new->name, irq, desc->irq_data.chip->name);
1359 goto out_bus_unlock;
1364 * The following block of code has to be executed atomically
1365 * protected against a concurrent interrupt and any of the other
1366 * management calls which are not serialized via
1367 * desc->request_mutex or the optional bus lock.
1369 raw_spin_lock_irqsave(&desc->lock, flags);
1370 old_ptr = &desc->action;
1371 old = *old_ptr;
1372 if (old) {
1374 * Can't share interrupts unless both agree to and are
1375 * the same type (level, edge, polarity). So both flag
1376 * fields must have IRQF_SHARED set and the bits which
1377 * set the trigger type must match. Also all must
1378 * agree on ONESHOT.
1379 * Interrupt lines used for NMIs cannot be shared.
1381 unsigned int oldtype;
1383 if (desc->istate & IRQS_NMI) {
1384 pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n",
1385 new->name, irq, desc->irq_data.chip->name);
1386 ret = -EINVAL;
1387 goto out_unlock;
1391 * If nobody did set the configuration before, inherit
1392 * the one provided by the requester.
1394 if (irqd_trigger_type_was_set(&desc->irq_data)) {
1395 oldtype = irqd_get_trigger_type(&desc->irq_data);
1396 } else {
1397 oldtype = new->flags & IRQF_TRIGGER_MASK;
1398 irqd_set_trigger_type(&desc->irq_data, oldtype);
1401 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1402 (oldtype != (new->flags & IRQF_TRIGGER_MASK)) ||
1403 ((old->flags ^ new->flags) & IRQF_ONESHOT))
1404 goto mismatch;
1406 /* All handlers must agree on per-cpuness */
1407 if ((old->flags & IRQF_PERCPU) !=
1408 (new->flags & IRQF_PERCPU))
1409 goto mismatch;
1411 /* add new interrupt at end of irq queue */
1412 do {
1414 * Or all existing action->thread_mask bits,
1415 * so we can find the next zero bit for this
1416 * new action.
1418 thread_mask |= old->thread_mask;
1419 old_ptr = &old->next;
1420 old = *old_ptr;
1421 } while (old);
1422 shared = 1;
1426 * Setup the thread mask for this irqaction for ONESHOT. For
1427 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1428 * conditional in irq_wake_thread().
1430 if (new->flags & IRQF_ONESHOT) {
1432 * Unlikely to have 32 resp 64 irqs sharing one line,
1433 * but who knows.
1435 if (thread_mask == ~0UL) {
1436 ret = -EBUSY;
1437 goto out_unlock;
1440 * The thread_mask for the action is or'ed to
1441 * desc->thread_active to indicate that the
1442 * IRQF_ONESHOT thread handler has been woken, but not
1443 * yet finished. The bit is cleared when a thread
1444 * completes. When all threads of a shared interrupt
1445 * line have completed desc->threads_active becomes
1446 * zero and the interrupt line is unmasked. See
1447 * handle.c:irq_wake_thread() for further information.
1449 * If no thread is woken by primary (hard irq context)
1450 * interrupt handlers, then desc->threads_active is
1451 * also checked for zero to unmask the irq line in the
1452 * affected hard irq flow handlers
1453 * (handle_[fasteoi|level]_irq).
1455 * The new action gets the first zero bit of
1456 * thread_mask assigned. See the loop above which or's
1457 * all existing action->thread_mask bits.
1459 new->thread_mask = 1UL << ffz(thread_mask);
1461 } else if (new->handler == irq_default_primary_handler &&
1462 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1464 * The interrupt was requested with handler = NULL, so
1465 * we use the default primary handler for it. But it
1466 * does not have the oneshot flag set. In combination
1467 * with level interrupts this is deadly, because the
1468 * default primary handler just wakes the thread, then
1469 * the irq lines is reenabled, but the device still
1470 * has the level irq asserted. Rinse and repeat....
1472 * While this works for edge type interrupts, we play
1473 * it safe and reject unconditionally because we can't
1474 * say for sure which type this interrupt really
1475 * has. The type flags are unreliable as the
1476 * underlying chip implementation can override them.
1478 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1479 irq);
1480 ret = -EINVAL;
1481 goto out_unlock;
1484 if (!shared) {
1485 init_waitqueue_head(&desc->wait_for_threads);
1487 /* Setup the type (level, edge polarity) if configured: */
1488 if (new->flags & IRQF_TRIGGER_MASK) {
1489 ret = __irq_set_trigger(desc,
1490 new->flags & IRQF_TRIGGER_MASK);
1492 if (ret)
1493 goto out_unlock;
1497 * Activate the interrupt. That activation must happen
1498 * independently of IRQ_NOAUTOEN. request_irq() can fail
1499 * and the callers are supposed to handle
1500 * that. enable_irq() of an interrupt requested with
1501 * IRQ_NOAUTOEN is not supposed to fail. The activation
1502 * keeps it in shutdown mode, it merily associates
1503 * resources if necessary and if that's not possible it
1504 * fails. Interrupts which are in managed shutdown mode
1505 * will simply ignore that activation request.
1507 ret = irq_activate(desc);
1508 if (ret)
1509 goto out_unlock;
1511 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1512 IRQS_ONESHOT | IRQS_WAITING);
1513 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1515 if (new->flags & IRQF_PERCPU) {
1516 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1517 irq_settings_set_per_cpu(desc);
1520 if (new->flags & IRQF_ONESHOT)
1521 desc->istate |= IRQS_ONESHOT;
1523 /* Exclude IRQ from balancing if requested */
1524 if (new->flags & IRQF_NOBALANCING) {
1525 irq_settings_set_no_balancing(desc);
1526 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1529 if (irq_settings_can_autoenable(desc)) {
1530 irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
1531 } else {
1533 * Shared interrupts do not go well with disabling
1534 * auto enable. The sharing interrupt might request
1535 * it while it's still disabled and then wait for
1536 * interrupts forever.
1538 WARN_ON_ONCE(new->flags & IRQF_SHARED);
1539 /* Undo nested disables: */
1540 desc->depth = 1;
1543 } else if (new->flags & IRQF_TRIGGER_MASK) {
1544 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1545 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
1547 if (nmsk != omsk)
1548 /* hope the handler works with current trigger mode */
1549 pr_warn("irq %d uses trigger mode %u; requested %u\n",
1550 irq, omsk, nmsk);
1553 *old_ptr = new;
1555 irq_pm_install_action(desc, new);
1557 /* Reset broken irq detection when installing new handler */
1558 desc->irq_count = 0;
1559 desc->irqs_unhandled = 0;
1562 * Check whether we disabled the irq via the spurious handler
1563 * before. Reenable it and give it another chance.
1565 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1566 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1567 __enable_irq(desc);
1570 raw_spin_unlock_irqrestore(&desc->lock, flags);
1571 chip_bus_sync_unlock(desc);
1572 mutex_unlock(&desc->request_mutex);
1574 irq_setup_timings(desc, new);
1577 * Strictly no need to wake it up, but hung_task complains
1578 * when no hard interrupt wakes the thread up.
1580 if (new->thread)
1581 wake_up_process(new->thread);
1582 if (new->secondary)
1583 wake_up_process(new->secondary->thread);
1585 register_irq_proc(irq, desc);
1586 new->dir = NULL;
1587 register_handler_proc(irq, new);
1588 return 0;
1590 mismatch:
1591 if (!(new->flags & IRQF_PROBE_SHARED)) {
1592 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1593 irq, new->flags, new->name, old->flags, old->name);
1594 #ifdef CONFIG_DEBUG_SHIRQ
1595 dump_stack();
1596 #endif
1598 ret = -EBUSY;
1600 out_unlock:
1601 raw_spin_unlock_irqrestore(&desc->lock, flags);
1603 if (!desc->action)
1604 irq_release_resources(desc);
1605 out_bus_unlock:
1606 chip_bus_sync_unlock(desc);
1607 mutex_unlock(&desc->request_mutex);
1609 out_thread:
1610 if (new->thread) {
1611 struct task_struct *t = new->thread;
1613 new->thread = NULL;
1614 kthread_stop(t);
1615 put_task_struct(t);
1617 if (new->secondary && new->secondary->thread) {
1618 struct task_struct *t = new->secondary->thread;
1620 new->secondary->thread = NULL;
1621 kthread_stop(t);
1622 put_task_struct(t);
1624 out_mput:
1625 module_put(desc->owner);
1626 return ret;
1630 * setup_irq - setup an interrupt
1631 * @irq: Interrupt line to setup
1632 * @act: irqaction for the interrupt
1634 * Used to statically setup interrupts in the early boot process.
1636 int setup_irq(unsigned int irq, struct irqaction *act)
1638 int retval;
1639 struct irq_desc *desc = irq_to_desc(irq);
1641 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1642 return -EINVAL;
1644 retval = irq_chip_pm_get(&desc->irq_data);
1645 if (retval < 0)
1646 return retval;
1648 retval = __setup_irq(irq, desc, act);
1650 if (retval)
1651 irq_chip_pm_put(&desc->irq_data);
1653 return retval;
1655 EXPORT_SYMBOL_GPL(setup_irq);
1658 * Internal function to unregister an irqaction - used to free
1659 * regular and special interrupts that are part of the architecture.
1661 static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
1663 unsigned irq = desc->irq_data.irq;
1664 struct irqaction *action, **action_ptr;
1665 unsigned long flags;
1667 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1669 mutex_lock(&desc->request_mutex);
1670 chip_bus_lock(desc);
1671 raw_spin_lock_irqsave(&desc->lock, flags);
1674 * There can be multiple actions per IRQ descriptor, find the right
1675 * one based on the dev_id:
1677 action_ptr = &desc->action;
1678 for (;;) {
1679 action = *action_ptr;
1681 if (!action) {
1682 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1683 raw_spin_unlock_irqrestore(&desc->lock, flags);
1684 chip_bus_sync_unlock(desc);
1685 mutex_unlock(&desc->request_mutex);
1686 return NULL;
1689 if (action->dev_id == dev_id)
1690 break;
1691 action_ptr = &action->next;
1694 /* Found it - now remove it from the list of entries: */
1695 *action_ptr = action->next;
1697 irq_pm_remove_action(desc, action);
1699 /* If this was the last handler, shut down the IRQ line: */
1700 if (!desc->action) {
1701 irq_settings_clr_disable_unlazy(desc);
1702 irq_shutdown(desc);
1705 #ifdef CONFIG_SMP
1706 /* make sure affinity_hint is cleaned up */
1707 if (WARN_ON_ONCE(desc->affinity_hint))
1708 desc->affinity_hint = NULL;
1709 #endif
1711 raw_spin_unlock_irqrestore(&desc->lock, flags);
1713 * Drop bus_lock here so the changes which were done in the chip
1714 * callbacks above are synced out to the irq chips which hang
1715 * behind a slow bus (I2C, SPI) before calling synchronize_hardirq().
1717 * Aside of that the bus_lock can also be taken from the threaded
1718 * handler in irq_finalize_oneshot() which results in a deadlock
1719 * because kthread_stop() would wait forever for the thread to
1720 * complete, which is blocked on the bus lock.
1722 * The still held desc->request_mutex() protects against a
1723 * concurrent request_irq() of this irq so the release of resources
1724 * and timing data is properly serialized.
1726 chip_bus_sync_unlock(desc);
1728 unregister_handler_proc(irq, action);
1730 /* Make sure it's not being used on another CPU: */
1731 synchronize_hardirq(irq);
1733 #ifdef CONFIG_DEBUG_SHIRQ
1735 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1736 * event to happen even now it's being freed, so let's make sure that
1737 * is so by doing an extra call to the handler ....
1739 * ( We do this after actually deregistering it, to make sure that a
1740 * 'real' IRQ doesn't run in parallel with our fake. )
1742 if (action->flags & IRQF_SHARED) {
1743 local_irq_save(flags);
1744 action->handler(irq, dev_id);
1745 local_irq_restore(flags);
1747 #endif
1750 * The action has already been removed above, but the thread writes
1751 * its oneshot mask bit when it completes. Though request_mutex is
1752 * held across this which prevents __setup_irq() from handing out
1753 * the same bit to a newly requested action.
1755 if (action->thread) {
1756 kthread_stop(action->thread);
1757 put_task_struct(action->thread);
1758 if (action->secondary && action->secondary->thread) {
1759 kthread_stop(action->secondary->thread);
1760 put_task_struct(action->secondary->thread);
1764 /* Last action releases resources */
1765 if (!desc->action) {
1767 * Reaquire bus lock as irq_release_resources() might
1768 * require it to deallocate resources over the slow bus.
1770 chip_bus_lock(desc);
1771 irq_release_resources(desc);
1772 chip_bus_sync_unlock(desc);
1773 irq_remove_timings(desc);
1776 mutex_unlock(&desc->request_mutex);
1778 irq_chip_pm_put(&desc->irq_data);
1779 module_put(desc->owner);
1780 kfree(action->secondary);
1781 return action;
1785 * remove_irq - free an interrupt
1786 * @irq: Interrupt line to free
1787 * @act: irqaction for the interrupt
1789 * Used to remove interrupts statically setup by the early boot process.
1791 void remove_irq(unsigned int irq, struct irqaction *act)
1793 struct irq_desc *desc = irq_to_desc(irq);
1795 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1796 __free_irq(desc, act->dev_id);
1798 EXPORT_SYMBOL_GPL(remove_irq);
1801 * free_irq - free an interrupt allocated with request_irq
1802 * @irq: Interrupt line to free
1803 * @dev_id: Device identity to free
1805 * Remove an interrupt handler. The handler is removed and if the
1806 * interrupt line is no longer in use by any driver it is disabled.
1807 * On a shared IRQ the caller must ensure the interrupt is disabled
1808 * on the card it drives before calling this function. The function
1809 * does not return until any executing interrupts for this IRQ
1810 * have completed.
1812 * This function must not be called from interrupt context.
1814 * Returns the devname argument passed to request_irq.
1816 const void *free_irq(unsigned int irq, void *dev_id)
1818 struct irq_desc *desc = irq_to_desc(irq);
1819 struct irqaction *action;
1820 const char *devname;
1822 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1823 return NULL;
1825 #ifdef CONFIG_SMP
1826 if (WARN_ON(desc->affinity_notify))
1827 desc->affinity_notify = NULL;
1828 #endif
1830 action = __free_irq(desc, dev_id);
1832 if (!action)
1833 return NULL;
1835 devname = action->name;
1836 kfree(action);
1837 return devname;
1839 EXPORT_SYMBOL(free_irq);
1841 /* This function must be called with desc->lock held */
1842 static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc)
1844 const char *devname = NULL;
1846 desc->istate &= ~IRQS_NMI;
1848 if (!WARN_ON(desc->action == NULL)) {
1849 irq_pm_remove_action(desc, desc->action);
1850 devname = desc->action->name;
1851 unregister_handler_proc(irq, desc->action);
1853 kfree(desc->action);
1854 desc->action = NULL;
1857 irq_settings_clr_disable_unlazy(desc);
1858 irq_shutdown(desc);
1860 irq_release_resources(desc);
1862 irq_chip_pm_put(&desc->irq_data);
1863 module_put(desc->owner);
1865 return devname;
1868 const void *free_nmi(unsigned int irq, void *dev_id)
1870 struct irq_desc *desc = irq_to_desc(irq);
1871 unsigned long flags;
1872 const void *devname;
1874 if (!desc || WARN_ON(!(desc->istate & IRQS_NMI)))
1875 return NULL;
1877 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1878 return NULL;
1880 /* NMI still enabled */
1881 if (WARN_ON(desc->depth == 0))
1882 disable_nmi_nosync(irq);
1884 raw_spin_lock_irqsave(&desc->lock, flags);
1886 irq_nmi_teardown(desc);
1887 devname = __cleanup_nmi(irq, desc);
1889 raw_spin_unlock_irqrestore(&desc->lock, flags);
1891 return devname;
1895 * request_threaded_irq - allocate an interrupt line
1896 * @irq: Interrupt line to allocate
1897 * @handler: Function to be called when the IRQ occurs.
1898 * Primary handler for threaded interrupts
1899 * If NULL and thread_fn != NULL the default
1900 * primary handler is installed
1901 * @thread_fn: Function called from the irq handler thread
1902 * If NULL, no irq thread is created
1903 * @irqflags: Interrupt type flags
1904 * @devname: An ascii name for the claiming device
1905 * @dev_id: A cookie passed back to the handler function
1907 * This call allocates interrupt resources and enables the
1908 * interrupt line and IRQ handling. From the point this
1909 * call is made your handler function may be invoked. Since
1910 * your handler function must clear any interrupt the board
1911 * raises, you must take care both to initialise your hardware
1912 * and to set up the interrupt handler in the right order.
1914 * If you want to set up a threaded irq handler for your device
1915 * then you need to supply @handler and @thread_fn. @handler is
1916 * still called in hard interrupt context and has to check
1917 * whether the interrupt originates from the device. If yes it
1918 * needs to disable the interrupt on the device and return
1919 * IRQ_WAKE_THREAD which will wake up the handler thread and run
1920 * @thread_fn. This split handler design is necessary to support
1921 * shared interrupts.
1923 * Dev_id must be globally unique. Normally the address of the
1924 * device data structure is used as the cookie. Since the handler
1925 * receives this value it makes sense to use it.
1927 * If your interrupt is shared you must pass a non NULL dev_id
1928 * as this is required when freeing the interrupt.
1930 * Flags:
1932 * IRQF_SHARED Interrupt is shared
1933 * IRQF_TRIGGER_* Specify active edge(s) or level
1936 int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1937 irq_handler_t thread_fn, unsigned long irqflags,
1938 const char *devname, void *dev_id)
1940 struct irqaction *action;
1941 struct irq_desc *desc;
1942 int retval;
1944 if (irq == IRQ_NOTCONNECTED)
1945 return -ENOTCONN;
1948 * Sanity-check: shared interrupts must pass in a real dev-ID,
1949 * otherwise we'll have trouble later trying to figure out
1950 * which interrupt is which (messes up the interrupt freeing
1951 * logic etc).
1953 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1954 * it cannot be set along with IRQF_NO_SUSPEND.
1956 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1957 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1958 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
1959 return -EINVAL;
1961 desc = irq_to_desc(irq);
1962 if (!desc)
1963 return -EINVAL;
1965 if (!irq_settings_can_request(desc) ||
1966 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1967 return -EINVAL;
1969 if (!handler) {
1970 if (!thread_fn)
1971 return -EINVAL;
1972 handler = irq_default_primary_handler;
1975 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1976 if (!action)
1977 return -ENOMEM;
1979 action->handler = handler;
1980 action->thread_fn = thread_fn;
1981 action->flags = irqflags;
1982 action->name = devname;
1983 action->dev_id = dev_id;
1985 retval = irq_chip_pm_get(&desc->irq_data);
1986 if (retval < 0) {
1987 kfree(action);
1988 return retval;
1991 retval = __setup_irq(irq, desc, action);
1993 if (retval) {
1994 irq_chip_pm_put(&desc->irq_data);
1995 kfree(action->secondary);
1996 kfree(action);
1999 #ifdef CONFIG_DEBUG_SHIRQ_FIXME
2000 if (!retval && (irqflags & IRQF_SHARED)) {
2002 * It's a shared IRQ -- the driver ought to be prepared for it
2003 * to happen immediately, so let's make sure....
2004 * We disable the irq to make sure that a 'real' IRQ doesn't
2005 * run in parallel with our fake.
2007 unsigned long flags;
2009 disable_irq(irq);
2010 local_irq_save(flags);
2012 handler(irq, dev_id);
2014 local_irq_restore(flags);
2015 enable_irq(irq);
2017 #endif
2018 return retval;
2020 EXPORT_SYMBOL(request_threaded_irq);
2023 * request_any_context_irq - allocate an interrupt line
2024 * @irq: Interrupt line to allocate
2025 * @handler: Function to be called when the IRQ occurs.
2026 * Threaded handler for threaded interrupts.
2027 * @flags: Interrupt type flags
2028 * @name: An ascii name for the claiming device
2029 * @dev_id: A cookie passed back to the handler function
2031 * This call allocates interrupt resources and enables the
2032 * interrupt line and IRQ handling. It selects either a
2033 * hardirq or threaded handling method depending on the
2034 * context.
2036 * On failure, it returns a negative value. On success,
2037 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
2039 int request_any_context_irq(unsigned int irq, irq_handler_t handler,
2040 unsigned long flags, const char *name, void *dev_id)
2042 struct irq_desc *desc;
2043 int ret;
2045 if (irq == IRQ_NOTCONNECTED)
2046 return -ENOTCONN;
2048 desc = irq_to_desc(irq);
2049 if (!desc)
2050 return -EINVAL;
2052 if (irq_settings_is_nested_thread(desc)) {
2053 ret = request_threaded_irq(irq, NULL, handler,
2054 flags, name, dev_id);
2055 return !ret ? IRQC_IS_NESTED : ret;
2058 ret = request_irq(irq, handler, flags, name, dev_id);
2059 return !ret ? IRQC_IS_HARDIRQ : ret;
2061 EXPORT_SYMBOL_GPL(request_any_context_irq);
2064 * request_nmi - allocate an interrupt line for NMI delivery
2065 * @irq: Interrupt line to allocate
2066 * @handler: Function to be called when the IRQ occurs.
2067 * Threaded handler for threaded interrupts.
2068 * @irqflags: Interrupt type flags
2069 * @name: An ascii name for the claiming device
2070 * @dev_id: A cookie passed back to the handler function
2072 * This call allocates interrupt resources and enables the
2073 * interrupt line and IRQ handling. It sets up the IRQ line
2074 * to be handled as an NMI.
2076 * An interrupt line delivering NMIs cannot be shared and IRQ handling
2077 * cannot be threaded.
2079 * Interrupt lines requested for NMI delivering must produce per cpu
2080 * interrupts and have auto enabling setting disabled.
2082 * Dev_id must be globally unique. Normally the address of the
2083 * device data structure is used as the cookie. Since the handler
2084 * receives this value it makes sense to use it.
2086 * If the interrupt line cannot be used to deliver NMIs, function
2087 * will fail and return a negative value.
2089 int request_nmi(unsigned int irq, irq_handler_t handler,
2090 unsigned long irqflags, const char *name, void *dev_id)
2092 struct irqaction *action;
2093 struct irq_desc *desc;
2094 unsigned long flags;
2095 int retval;
2097 if (irq == IRQ_NOTCONNECTED)
2098 return -ENOTCONN;
2100 /* NMI cannot be shared, used for Polling */
2101 if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
2102 return -EINVAL;
2104 if (!(irqflags & IRQF_PERCPU))
2105 return -EINVAL;
2107 if (!handler)
2108 return -EINVAL;
2110 desc = irq_to_desc(irq);
2112 if (!desc || irq_settings_can_autoenable(desc) ||
2113 !irq_settings_can_request(desc) ||
2114 WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
2115 !irq_supports_nmi(desc))
2116 return -EINVAL;
2118 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2119 if (!action)
2120 return -ENOMEM;
2122 action->handler = handler;
2123 action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING;
2124 action->name = name;
2125 action->dev_id = dev_id;
2127 retval = irq_chip_pm_get(&desc->irq_data);
2128 if (retval < 0)
2129 goto err_out;
2131 retval = __setup_irq(irq, desc, action);
2132 if (retval)
2133 goto err_irq_setup;
2135 raw_spin_lock_irqsave(&desc->lock, flags);
2137 /* Setup NMI state */
2138 desc->istate |= IRQS_NMI;
2139 retval = irq_nmi_setup(desc);
2140 if (retval) {
2141 __cleanup_nmi(irq, desc);
2142 raw_spin_unlock_irqrestore(&desc->lock, flags);
2143 return -EINVAL;
2146 raw_spin_unlock_irqrestore(&desc->lock, flags);
2148 return 0;
2150 err_irq_setup:
2151 irq_chip_pm_put(&desc->irq_data);
2152 err_out:
2153 kfree(action);
2155 return retval;
2158 void enable_percpu_irq(unsigned int irq, unsigned int type)
2160 unsigned int cpu = smp_processor_id();
2161 unsigned long flags;
2162 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2164 if (!desc)
2165 return;
2168 * If the trigger type is not specified by the caller, then
2169 * use the default for this interrupt.
2171 type &= IRQ_TYPE_SENSE_MASK;
2172 if (type == IRQ_TYPE_NONE)
2173 type = irqd_get_trigger_type(&desc->irq_data);
2175 if (type != IRQ_TYPE_NONE) {
2176 int ret;
2178 ret = __irq_set_trigger(desc, type);
2180 if (ret) {
2181 WARN(1, "failed to set type for IRQ%d\n", irq);
2182 goto out;
2186 irq_percpu_enable(desc, cpu);
2187 out:
2188 irq_put_desc_unlock(desc, flags);
2190 EXPORT_SYMBOL_GPL(enable_percpu_irq);
2192 void enable_percpu_nmi(unsigned int irq, unsigned int type)
2194 enable_percpu_irq(irq, type);
2198 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
2199 * @irq: Linux irq number to check for
2201 * Must be called from a non migratable context. Returns the enable
2202 * state of a per cpu interrupt on the current cpu.
2204 bool irq_percpu_is_enabled(unsigned int irq)
2206 unsigned int cpu = smp_processor_id();
2207 struct irq_desc *desc;
2208 unsigned long flags;
2209 bool is_enabled;
2211 desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2212 if (!desc)
2213 return false;
2215 is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
2216 irq_put_desc_unlock(desc, flags);
2218 return is_enabled;
2220 EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
2222 void disable_percpu_irq(unsigned int irq)
2224 unsigned int cpu = smp_processor_id();
2225 unsigned long flags;
2226 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2228 if (!desc)
2229 return;
2231 irq_percpu_disable(desc, cpu);
2232 irq_put_desc_unlock(desc, flags);
2234 EXPORT_SYMBOL_GPL(disable_percpu_irq);
2236 void disable_percpu_nmi(unsigned int irq)
2238 disable_percpu_irq(irq);
2242 * Internal function to unregister a percpu irqaction.
2244 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2246 struct irq_desc *desc = irq_to_desc(irq);
2247 struct irqaction *action;
2248 unsigned long flags;
2250 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
2252 if (!desc)
2253 return NULL;
2255 raw_spin_lock_irqsave(&desc->lock, flags);
2257 action = desc->action;
2258 if (!action || action->percpu_dev_id != dev_id) {
2259 WARN(1, "Trying to free already-free IRQ %d\n", irq);
2260 goto bad;
2263 if (!cpumask_empty(desc->percpu_enabled)) {
2264 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
2265 irq, cpumask_first(desc->percpu_enabled));
2266 goto bad;
2269 /* Found it - now remove it from the list of entries: */
2270 desc->action = NULL;
2272 desc->istate &= ~IRQS_NMI;
2274 raw_spin_unlock_irqrestore(&desc->lock, flags);
2276 unregister_handler_proc(irq, action);
2278 irq_chip_pm_put(&desc->irq_data);
2279 module_put(desc->owner);
2280 return action;
2282 bad:
2283 raw_spin_unlock_irqrestore(&desc->lock, flags);
2284 return NULL;
2288 * remove_percpu_irq - free a per-cpu interrupt
2289 * @irq: Interrupt line to free
2290 * @act: irqaction for the interrupt
2292 * Used to remove interrupts statically setup by the early boot process.
2294 void remove_percpu_irq(unsigned int irq, struct irqaction *act)
2296 struct irq_desc *desc = irq_to_desc(irq);
2298 if (desc && irq_settings_is_per_cpu_devid(desc))
2299 __free_percpu_irq(irq, act->percpu_dev_id);
2303 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
2304 * @irq: Interrupt line to free
2305 * @dev_id: Device identity to free
2307 * Remove a percpu interrupt handler. The handler is removed, but
2308 * the interrupt line is not disabled. This must be done on each
2309 * CPU before calling this function. The function does not return
2310 * until any executing interrupts for this IRQ have completed.
2312 * This function must not be called from interrupt context.
2314 void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2316 struct irq_desc *desc = irq_to_desc(irq);
2318 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2319 return;
2321 chip_bus_lock(desc);
2322 kfree(__free_percpu_irq(irq, dev_id));
2323 chip_bus_sync_unlock(desc);
2325 EXPORT_SYMBOL_GPL(free_percpu_irq);
2327 void free_percpu_nmi(unsigned int irq, void __percpu *dev_id)
2329 struct irq_desc *desc = irq_to_desc(irq);
2331 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2332 return;
2334 if (WARN_ON(!(desc->istate & IRQS_NMI)))
2335 return;
2337 kfree(__free_percpu_irq(irq, dev_id));
2341 * setup_percpu_irq - setup a per-cpu interrupt
2342 * @irq: Interrupt line to setup
2343 * @act: irqaction for the interrupt
2345 * Used to statically setup per-cpu interrupts in the early boot process.
2347 int setup_percpu_irq(unsigned int irq, struct irqaction *act)
2349 struct irq_desc *desc = irq_to_desc(irq);
2350 int retval;
2352 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2353 return -EINVAL;
2355 retval = irq_chip_pm_get(&desc->irq_data);
2356 if (retval < 0)
2357 return retval;
2359 retval = __setup_irq(irq, desc, act);
2361 if (retval)
2362 irq_chip_pm_put(&desc->irq_data);
2364 return retval;
2368 * __request_percpu_irq - allocate a percpu interrupt line
2369 * @irq: Interrupt line to allocate
2370 * @handler: Function to be called when the IRQ occurs.
2371 * @flags: Interrupt type flags (IRQF_TIMER only)
2372 * @devname: An ascii name for the claiming device
2373 * @dev_id: A percpu cookie passed back to the handler function
2375 * This call allocates interrupt resources and enables the
2376 * interrupt on the local CPU. If the interrupt is supposed to be
2377 * enabled on other CPUs, it has to be done on each CPU using
2378 * enable_percpu_irq().
2380 * Dev_id must be globally unique. It is a per-cpu variable, and
2381 * the handler gets called with the interrupted CPU's instance of
2382 * that variable.
2384 int __request_percpu_irq(unsigned int irq, irq_handler_t handler,
2385 unsigned long flags, const char *devname,
2386 void __percpu *dev_id)
2388 struct irqaction *action;
2389 struct irq_desc *desc;
2390 int retval;
2392 if (!dev_id)
2393 return -EINVAL;
2395 desc = irq_to_desc(irq);
2396 if (!desc || !irq_settings_can_request(desc) ||
2397 !irq_settings_is_per_cpu_devid(desc))
2398 return -EINVAL;
2400 if (flags && flags != IRQF_TIMER)
2401 return -EINVAL;
2403 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2404 if (!action)
2405 return -ENOMEM;
2407 action->handler = handler;
2408 action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND;
2409 action->name = devname;
2410 action->percpu_dev_id = dev_id;
2412 retval = irq_chip_pm_get(&desc->irq_data);
2413 if (retval < 0) {
2414 kfree(action);
2415 return retval;
2418 retval = __setup_irq(irq, desc, action);
2420 if (retval) {
2421 irq_chip_pm_put(&desc->irq_data);
2422 kfree(action);
2425 return retval;
2427 EXPORT_SYMBOL_GPL(__request_percpu_irq);
2430 * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery
2431 * @irq: Interrupt line to allocate
2432 * @handler: Function to be called when the IRQ occurs.
2433 * @name: An ascii name for the claiming device
2434 * @dev_id: A percpu cookie passed back to the handler function
2436 * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
2437 * have to be setup on each CPU by calling prepare_percpu_nmi() before
2438 * being enabled on the same CPU by using enable_percpu_nmi().
2440 * Dev_id must be globally unique. It is a per-cpu variable, and
2441 * the handler gets called with the interrupted CPU's instance of
2442 * that variable.
2444 * Interrupt lines requested for NMI delivering should have auto enabling
2445 * setting disabled.
2447 * If the interrupt line cannot be used to deliver NMIs, function
2448 * will fail returning a negative value.
2450 int request_percpu_nmi(unsigned int irq, irq_handler_t handler,
2451 const char *name, void __percpu *dev_id)
2453 struct irqaction *action;
2454 struct irq_desc *desc;
2455 unsigned long flags;
2456 int retval;
2458 if (!handler)
2459 return -EINVAL;
2461 desc = irq_to_desc(irq);
2463 if (!desc || !irq_settings_can_request(desc) ||
2464 !irq_settings_is_per_cpu_devid(desc) ||
2465 irq_settings_can_autoenable(desc) ||
2466 !irq_supports_nmi(desc))
2467 return -EINVAL;
2469 /* The line cannot already be NMI */
2470 if (desc->istate & IRQS_NMI)
2471 return -EINVAL;
2473 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2474 if (!action)
2475 return -ENOMEM;
2477 action->handler = handler;
2478 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND | IRQF_NO_THREAD
2479 | IRQF_NOBALANCING;
2480 action->name = name;
2481 action->percpu_dev_id = dev_id;
2483 retval = irq_chip_pm_get(&desc->irq_data);
2484 if (retval < 0)
2485 goto err_out;
2487 retval = __setup_irq(irq, desc, action);
2488 if (retval)
2489 goto err_irq_setup;
2491 raw_spin_lock_irqsave(&desc->lock, flags);
2492 desc->istate |= IRQS_NMI;
2493 raw_spin_unlock_irqrestore(&desc->lock, flags);
2495 return 0;
2497 err_irq_setup:
2498 irq_chip_pm_put(&desc->irq_data);
2499 err_out:
2500 kfree(action);
2502 return retval;
2506 * prepare_percpu_nmi - performs CPU local setup for NMI delivery
2507 * @irq: Interrupt line to prepare for NMI delivery
2509 * This call prepares an interrupt line to deliver NMI on the current CPU,
2510 * before that interrupt line gets enabled with enable_percpu_nmi().
2512 * As a CPU local operation, this should be called from non-preemptible
2513 * context.
2515 * If the interrupt line cannot be used to deliver NMIs, function
2516 * will fail returning a negative value.
2518 int prepare_percpu_nmi(unsigned int irq)
2520 unsigned long flags;
2521 struct irq_desc *desc;
2522 int ret = 0;
2524 WARN_ON(preemptible());
2526 desc = irq_get_desc_lock(irq, &flags,
2527 IRQ_GET_DESC_CHECK_PERCPU);
2528 if (!desc)
2529 return -EINVAL;
2531 if (WARN(!(desc->istate & IRQS_NMI),
2532 KERN_ERR "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n",
2533 irq)) {
2534 ret = -EINVAL;
2535 goto out;
2538 ret = irq_nmi_setup(desc);
2539 if (ret) {
2540 pr_err("Failed to setup NMI delivery: irq %u\n", irq);
2541 goto out;
2544 out:
2545 irq_put_desc_unlock(desc, flags);
2546 return ret;
2550 * teardown_percpu_nmi - undoes NMI setup of IRQ line
2551 * @irq: Interrupt line from which CPU local NMI configuration should be
2552 * removed
2554 * This call undoes the setup done by prepare_percpu_nmi().
2556 * IRQ line should not be enabled for the current CPU.
2558 * As a CPU local operation, this should be called from non-preemptible
2559 * context.
2561 void teardown_percpu_nmi(unsigned int irq)
2563 unsigned long flags;
2564 struct irq_desc *desc;
2566 WARN_ON(preemptible());
2568 desc = irq_get_desc_lock(irq, &flags,
2569 IRQ_GET_DESC_CHECK_PERCPU);
2570 if (!desc)
2571 return;
2573 if (WARN_ON(!(desc->istate & IRQS_NMI)))
2574 goto out;
2576 irq_nmi_teardown(desc);
2577 out:
2578 irq_put_desc_unlock(desc, flags);
2582 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2583 * @irq: Interrupt line that is forwarded to a VM
2584 * @which: One of IRQCHIP_STATE_* the caller wants to know about
2585 * @state: a pointer to a boolean where the state is to be storeed
2587 * This call snapshots the internal irqchip state of an
2588 * interrupt, returning into @state the bit corresponding to
2589 * stage @which
2591 * This function should be called with preemption disabled if the
2592 * interrupt controller has per-cpu registers.
2594 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2595 bool *state)
2597 struct irq_desc *desc;
2598 struct irq_data *data;
2599 struct irq_chip *chip;
2600 unsigned long flags;
2601 int err = -EINVAL;
2603 desc = irq_get_desc_buslock(irq, &flags, 0);
2604 if (!desc)
2605 return err;
2607 data = irq_desc_get_irq_data(desc);
2609 do {
2610 chip = irq_data_get_irq_chip(data);
2611 if (chip->irq_get_irqchip_state)
2612 break;
2613 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2614 data = data->parent_data;
2615 #else
2616 data = NULL;
2617 #endif
2618 } while (data);
2620 if (data)
2621 err = chip->irq_get_irqchip_state(data, which, state);
2623 irq_put_desc_busunlock(desc, flags);
2624 return err;
2626 EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
2629 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2630 * @irq: Interrupt line that is forwarded to a VM
2631 * @which: State to be restored (one of IRQCHIP_STATE_*)
2632 * @val: Value corresponding to @which
2634 * This call sets the internal irqchip state of an interrupt,
2635 * depending on the value of @which.
2637 * This function should be called with preemption disabled if the
2638 * interrupt controller has per-cpu registers.
2640 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2641 bool val)
2643 struct irq_desc *desc;
2644 struct irq_data *data;
2645 struct irq_chip *chip;
2646 unsigned long flags;
2647 int err = -EINVAL;
2649 desc = irq_get_desc_buslock(irq, &flags, 0);
2650 if (!desc)
2651 return err;
2653 data = irq_desc_get_irq_data(desc);
2655 do {
2656 chip = irq_data_get_irq_chip(data);
2657 if (chip->irq_set_irqchip_state)
2658 break;
2659 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2660 data = data->parent_data;
2661 #else
2662 data = NULL;
2663 #endif
2664 } while (data);
2666 if (data)
2667 err = chip->irq_set_irqchip_state(data, which, val);
2669 irq_put_desc_busunlock(desc, flags);
2670 return err;
2672 EXPORT_SYMBOL_GPL(irq_set_irqchip_state);