11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/irq/manage.c 31da177e4SLinus Torvalds * 4a34db9b2SIngo Molnar * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar 5a34db9b2SIngo Molnar * Copyright (C) 2005-2006 Thomas Gleixner 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This file contains driver APIs to the irq subsystem. 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/irq.h> 113aa551c9SThomas Gleixner #include <linux/kthread.h> 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds #include <linux/random.h> 141da177e4SLinus Torvalds #include <linux/interrupt.h> 151aeb272cSRobert P. J. Day #include <linux/slab.h> 163aa551c9SThomas Gleixner #include <linux/sched.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include "internals.h" 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds /** 211da177e4SLinus Torvalds * synchronize_irq - wait for pending IRQ handlers (on other CPUs) 221e5d5331SRandy Dunlap * @irq: interrupt number to wait for 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 251da177e4SLinus Torvalds * to complete before returning. If you use this function while 261da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 271da177e4SLinus Torvalds * 281da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds void synchronize_irq(unsigned int irq) 311da177e4SLinus Torvalds { 32cb5bc832SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 33a98ce5c6SHerbert Xu unsigned int status; 341da177e4SLinus Torvalds 357d94f7caSYinghai Lu if (!desc) 36c2b5a251SMatthew Wilcox return; 37c2b5a251SMatthew Wilcox 38a98ce5c6SHerbert Xu do { 39a98ce5c6SHerbert Xu unsigned long flags; 40a98ce5c6SHerbert Xu 41a98ce5c6SHerbert Xu /* 42a98ce5c6SHerbert Xu * Wait until we're out of the critical section. This might 43a98ce5c6SHerbert Xu * give the wrong answer due to the lack of memory barriers. 44a98ce5c6SHerbert Xu */ 451da177e4SLinus Torvalds while (desc->status & IRQ_INPROGRESS) 461da177e4SLinus Torvalds cpu_relax(); 47a98ce5c6SHerbert Xu 48a98ce5c6SHerbert Xu /* Ok, that indicated we're done: double-check carefully. */ 49a98ce5c6SHerbert Xu spin_lock_irqsave(&desc->lock, flags); 50a98ce5c6SHerbert Xu status = desc->status; 51a98ce5c6SHerbert Xu spin_unlock_irqrestore(&desc->lock, flags); 52a98ce5c6SHerbert Xu 53a98ce5c6SHerbert Xu /* Oops, that failed? */ 54a98ce5c6SHerbert Xu } while (status & IRQ_INPROGRESS); 553aa551c9SThomas Gleixner 563aa551c9SThomas Gleixner /* 573aa551c9SThomas Gleixner * We made sure that no hardirq handler is running. Now verify 583aa551c9SThomas Gleixner * that no threaded handlers are active. 593aa551c9SThomas Gleixner */ 603aa551c9SThomas Gleixner wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active)); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds EXPORT_SYMBOL(synchronize_irq); 631da177e4SLinus Torvalds 643aa551c9SThomas Gleixner #ifdef CONFIG_SMP 653aa551c9SThomas Gleixner cpumask_var_t irq_default_affinity; 663aa551c9SThomas Gleixner 67771ee3b0SThomas Gleixner /** 68771ee3b0SThomas Gleixner * irq_can_set_affinity - Check if the affinity of a given irq can be set 69771ee3b0SThomas Gleixner * @irq: Interrupt to check 70771ee3b0SThomas Gleixner * 71771ee3b0SThomas Gleixner */ 72771ee3b0SThomas Gleixner int irq_can_set_affinity(unsigned int irq) 73771ee3b0SThomas Gleixner { 7408678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 75771ee3b0SThomas Gleixner 76771ee3b0SThomas Gleixner if (CHECK_IRQ_PER_CPU(desc->status) || !desc->chip || 77771ee3b0SThomas Gleixner !desc->chip->set_affinity) 78771ee3b0SThomas Gleixner return 0; 79771ee3b0SThomas Gleixner 80771ee3b0SThomas Gleixner return 1; 81771ee3b0SThomas Gleixner } 82771ee3b0SThomas Gleixner 833aa551c9SThomas Gleixner static void 843aa551c9SThomas Gleixner irq_set_thread_affinity(struct irq_desc *desc, const struct cpumask *cpumask) 853aa551c9SThomas Gleixner { 863aa551c9SThomas Gleixner struct irqaction *action = desc->action; 873aa551c9SThomas Gleixner 883aa551c9SThomas Gleixner while (action) { 893aa551c9SThomas Gleixner if (action->thread) 903aa551c9SThomas Gleixner set_cpus_allowed_ptr(action->thread, cpumask); 913aa551c9SThomas Gleixner action = action->next; 923aa551c9SThomas Gleixner } 933aa551c9SThomas Gleixner } 943aa551c9SThomas Gleixner 95771ee3b0SThomas Gleixner /** 96771ee3b0SThomas Gleixner * irq_set_affinity - Set the irq affinity of a given irq 97771ee3b0SThomas Gleixner * @irq: Interrupt to set affinity 98771ee3b0SThomas Gleixner * @cpumask: cpumask 99771ee3b0SThomas Gleixner * 100771ee3b0SThomas Gleixner */ 1010de26520SRusty Russell int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask) 102771ee3b0SThomas Gleixner { 10308678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 104f6d87f4bSThomas Gleixner unsigned long flags; 105771ee3b0SThomas Gleixner 106771ee3b0SThomas Gleixner if (!desc->chip->set_affinity) 107771ee3b0SThomas Gleixner return -EINVAL; 108771ee3b0SThomas Gleixner 109f6d87f4bSThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 110f6d87f4bSThomas Gleixner 111771ee3b0SThomas Gleixner #ifdef CONFIG_GENERIC_PENDING_IRQ 112932775a4Svenkatesh.pallipadi@intel.com if (desc->status & IRQ_MOVE_PCNTXT || desc->status & IRQ_DISABLED) { 1137f7ace0cSMike Travis cpumask_copy(desc->affinity, cpumask); 11472b1e22dSSuresh Siddha desc->chip->set_affinity(irq, cpumask); 115f6d87f4bSThomas Gleixner } else { 116f6d87f4bSThomas Gleixner desc->status |= IRQ_MOVE_PENDING; 1177f7ace0cSMike Travis cpumask_copy(desc->pending_mask, cpumask); 118f6d87f4bSThomas Gleixner } 119771ee3b0SThomas Gleixner #else 1207f7ace0cSMike Travis cpumask_copy(desc->affinity, cpumask); 121771ee3b0SThomas Gleixner desc->chip->set_affinity(irq, cpumask); 122771ee3b0SThomas Gleixner #endif 1233aa551c9SThomas Gleixner irq_set_thread_affinity(desc, cpumask); 124f6d87f4bSThomas Gleixner desc->status |= IRQ_AFFINITY_SET; 125f6d87f4bSThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 126771ee3b0SThomas Gleixner return 0; 127771ee3b0SThomas Gleixner } 128771ee3b0SThomas Gleixner 12918404756SMax Krasnyansky #ifndef CONFIG_AUTO_IRQ_AFFINITY 13018404756SMax Krasnyansky /* 13118404756SMax Krasnyansky * Generic version of the affinity autoselector. 13218404756SMax Krasnyansky */ 133548c8933SHannes Eder static int setup_affinity(unsigned int irq, struct irq_desc *desc) 13418404756SMax Krasnyansky { 13518404756SMax Krasnyansky if (!irq_can_set_affinity(irq)) 13618404756SMax Krasnyansky return 0; 13718404756SMax Krasnyansky 138f6d87f4bSThomas Gleixner /* 139f6d87f4bSThomas Gleixner * Preserve an userspace affinity setup, but make sure that 140f6d87f4bSThomas Gleixner * one of the targets is online. 141f6d87f4bSThomas Gleixner */ 142612e3684SThomas Gleixner if (desc->status & (IRQ_AFFINITY_SET | IRQ_NO_BALANCING)) { 1437f7ace0cSMike Travis if (cpumask_any_and(desc->affinity, cpu_online_mask) 1440de26520SRusty Russell < nr_cpu_ids) 1450de26520SRusty Russell goto set_affinity; 146f6d87f4bSThomas Gleixner else 147f6d87f4bSThomas Gleixner desc->status &= ~IRQ_AFFINITY_SET; 148f6d87f4bSThomas Gleixner } 149f6d87f4bSThomas Gleixner 1507f7ace0cSMike Travis cpumask_and(desc->affinity, cpu_online_mask, irq_default_affinity); 1510de26520SRusty Russell set_affinity: 1527f7ace0cSMike Travis desc->chip->set_affinity(irq, desc->affinity); 15318404756SMax Krasnyansky 15418404756SMax Krasnyansky return 0; 15518404756SMax Krasnyansky } 156f6d87f4bSThomas Gleixner #else 157548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *d) 158f6d87f4bSThomas Gleixner { 159f6d87f4bSThomas Gleixner return irq_select_affinity(irq); 160f6d87f4bSThomas Gleixner } 16118404756SMax Krasnyansky #endif 16218404756SMax Krasnyansky 163f6d87f4bSThomas Gleixner /* 164f6d87f4bSThomas Gleixner * Called when affinity is set via /proc/irq 165f6d87f4bSThomas Gleixner */ 166f6d87f4bSThomas Gleixner int irq_select_affinity_usr(unsigned int irq) 167f6d87f4bSThomas Gleixner { 168f6d87f4bSThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 169f6d87f4bSThomas Gleixner unsigned long flags; 170f6d87f4bSThomas Gleixner int ret; 171f6d87f4bSThomas Gleixner 172f6d87f4bSThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 173548c8933SHannes Eder ret = setup_affinity(irq, desc); 1743aa551c9SThomas Gleixner if (!ret) 1753aa551c9SThomas Gleixner irq_set_thread_affinity(desc, desc->affinity); 176f6d87f4bSThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 177f6d87f4bSThomas Gleixner 178f6d87f4bSThomas Gleixner return ret; 179f6d87f4bSThomas Gleixner } 180f6d87f4bSThomas Gleixner 181f6d87f4bSThomas Gleixner #else 182548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *desc) 183f6d87f4bSThomas Gleixner { 184f6d87f4bSThomas Gleixner return 0; 185f6d87f4bSThomas Gleixner } 1861da177e4SLinus Torvalds #endif 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /** 1891da177e4SLinus Torvalds * disable_irq_nosync - disable an irq without waiting 1901da177e4SLinus Torvalds * @irq: Interrupt to disable 1911da177e4SLinus Torvalds * 1921da177e4SLinus Torvalds * Disable the selected interrupt line. Disables and Enables are 1931da177e4SLinus Torvalds * nested. 1941da177e4SLinus Torvalds * Unlike disable_irq(), this function does not ensure existing 1951da177e4SLinus Torvalds * instances of the IRQ handler have completed before returning. 1961da177e4SLinus Torvalds * 1971da177e4SLinus Torvalds * This function may be called from IRQ context. 1981da177e4SLinus Torvalds */ 1991da177e4SLinus Torvalds void disable_irq_nosync(unsigned int irq) 2001da177e4SLinus Torvalds { 201d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2021da177e4SLinus Torvalds unsigned long flags; 2031da177e4SLinus Torvalds 2047d94f7caSYinghai Lu if (!desc) 205c2b5a251SMatthew Wilcox return; 206c2b5a251SMatthew Wilcox 2071da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 2081da177e4SLinus Torvalds if (!desc->depth++) { 2091da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 210d1bef4edSIngo Molnar desc->chip->disable(irq); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync); 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /** 2171da177e4SLinus Torvalds * disable_irq - disable an irq and wait for completion 2181da177e4SLinus Torvalds * @irq: Interrupt to disable 2191da177e4SLinus Torvalds * 2201da177e4SLinus Torvalds * Disable the selected interrupt line. Enables and Disables are 2211da177e4SLinus Torvalds * nested. 2221da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 2231da177e4SLinus Torvalds * to complete before returning. If you use this function while 2241da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 2251da177e4SLinus Torvalds * 2261da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 2271da177e4SLinus Torvalds */ 2281da177e4SLinus Torvalds void disable_irq(unsigned int irq) 2291da177e4SLinus Torvalds { 230d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2311da177e4SLinus Torvalds 2327d94f7caSYinghai Lu if (!desc) 233c2b5a251SMatthew Wilcox return; 234c2b5a251SMatthew Wilcox 2351da177e4SLinus Torvalds disable_irq_nosync(irq); 2361da177e4SLinus Torvalds if (desc->action) 2371da177e4SLinus Torvalds synchronize_irq(irq); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq); 2401da177e4SLinus Torvalds 2411adb0850SThomas Gleixner static void __enable_irq(struct irq_desc *desc, unsigned int irq) 2421adb0850SThomas Gleixner { 2431adb0850SThomas Gleixner switch (desc->depth) { 2441adb0850SThomas Gleixner case 0: 245b8c512f6SArjan van de Ven WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq); 2461adb0850SThomas Gleixner break; 2471adb0850SThomas Gleixner case 1: { 2481adb0850SThomas Gleixner unsigned int status = desc->status & ~IRQ_DISABLED; 2491adb0850SThomas Gleixner 2501adb0850SThomas Gleixner /* Prevent probing on this irq: */ 2511adb0850SThomas Gleixner desc->status = status | IRQ_NOPROBE; 2521adb0850SThomas Gleixner check_irq_resend(desc, irq); 2531adb0850SThomas Gleixner /* fall-through */ 2541adb0850SThomas Gleixner } 2551adb0850SThomas Gleixner default: 2561adb0850SThomas Gleixner desc->depth--; 2571adb0850SThomas Gleixner } 2581adb0850SThomas Gleixner } 2591adb0850SThomas Gleixner 2601da177e4SLinus Torvalds /** 2611da177e4SLinus Torvalds * enable_irq - enable handling of an irq 2621da177e4SLinus Torvalds * @irq: Interrupt to enable 2631da177e4SLinus Torvalds * 2641da177e4SLinus Torvalds * Undoes the effect of one call to disable_irq(). If this 2651da177e4SLinus Torvalds * matches the last disable, processing of interrupts on this 2661da177e4SLinus Torvalds * IRQ line is re-enabled. 2671da177e4SLinus Torvalds * 2681da177e4SLinus Torvalds * This function may be called from IRQ context. 2691da177e4SLinus Torvalds */ 2701da177e4SLinus Torvalds void enable_irq(unsigned int irq) 2711da177e4SLinus Torvalds { 272d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2731da177e4SLinus Torvalds unsigned long flags; 2741da177e4SLinus Torvalds 2757d94f7caSYinghai Lu if (!desc) 276c2b5a251SMatthew Wilcox return; 277c2b5a251SMatthew Wilcox 2781da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 2791adb0850SThomas Gleixner __enable_irq(desc, irq); 2801da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq); 2831da177e4SLinus Torvalds 2840c5d1eb7SDavid Brownell static int set_irq_wake_real(unsigned int irq, unsigned int on) 2852db87321SUwe Kleine-König { 28608678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 2872db87321SUwe Kleine-König int ret = -ENXIO; 2882db87321SUwe Kleine-König 2892db87321SUwe Kleine-König if (desc->chip->set_wake) 2902db87321SUwe Kleine-König ret = desc->chip->set_wake(irq, on); 2912db87321SUwe Kleine-König 2922db87321SUwe Kleine-König return ret; 2932db87321SUwe Kleine-König } 2942db87321SUwe Kleine-König 295ba9a2331SThomas Gleixner /** 296ba9a2331SThomas Gleixner * set_irq_wake - control irq power management wakeup 297ba9a2331SThomas Gleixner * @irq: interrupt to control 298ba9a2331SThomas Gleixner * @on: enable/disable power management wakeup 299ba9a2331SThomas Gleixner * 30015a647ebSDavid Brownell * Enable/disable power management wakeup mode, which is 30115a647ebSDavid Brownell * disabled by default. Enables and disables must match, 30215a647ebSDavid Brownell * just as they match for non-wakeup mode support. 30315a647ebSDavid Brownell * 30415a647ebSDavid Brownell * Wakeup mode lets this IRQ wake the system from sleep 30515a647ebSDavid Brownell * states like "suspend to RAM". 306ba9a2331SThomas Gleixner */ 307ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on) 308ba9a2331SThomas Gleixner { 30908678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 310ba9a2331SThomas Gleixner unsigned long flags; 3112db87321SUwe Kleine-König int ret = 0; 312ba9a2331SThomas Gleixner 31315a647ebSDavid Brownell /* wakeup-capable irqs can be shared between drivers that 31415a647ebSDavid Brownell * don't need to have the same sleep mode behaviors. 31515a647ebSDavid Brownell */ 316ba9a2331SThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 31715a647ebSDavid Brownell if (on) { 3182db87321SUwe Kleine-König if (desc->wake_depth++ == 0) { 3192db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3202db87321SUwe Kleine-König if (ret) 3212db87321SUwe Kleine-König desc->wake_depth = 0; 32215a647ebSDavid Brownell else 3232db87321SUwe Kleine-König desc->status |= IRQ_WAKEUP; 3242db87321SUwe Kleine-König } 32515a647ebSDavid Brownell } else { 32615a647ebSDavid Brownell if (desc->wake_depth == 0) { 3277a2c4770SArjan van de Ven WARN(1, "Unbalanced IRQ %d wake disable\n", irq); 3282db87321SUwe Kleine-König } else if (--desc->wake_depth == 0) { 3292db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3302db87321SUwe Kleine-König if (ret) 3312db87321SUwe Kleine-König desc->wake_depth = 1; 33215a647ebSDavid Brownell else 3332db87321SUwe Kleine-König desc->status &= ~IRQ_WAKEUP; 33415a647ebSDavid Brownell } 3352db87321SUwe Kleine-König } 3362db87321SUwe Kleine-König 337ba9a2331SThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 338ba9a2331SThomas Gleixner return ret; 339ba9a2331SThomas Gleixner } 340ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake); 341ba9a2331SThomas Gleixner 3421da177e4SLinus Torvalds /* 3431da177e4SLinus Torvalds * Internal function that tells the architecture code whether a 3441da177e4SLinus Torvalds * particular irq has been exclusively allocated or is available 3451da177e4SLinus Torvalds * for driver use. 3461da177e4SLinus Torvalds */ 3471da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags) 3481da177e4SLinus Torvalds { 349d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 3501da177e4SLinus Torvalds struct irqaction *action; 3511da177e4SLinus Torvalds 3527d94f7caSYinghai Lu if (!desc) 3537d94f7caSYinghai Lu return 0; 3547d94f7caSYinghai Lu 3557d94f7caSYinghai Lu if (desc->status & IRQ_NOREQUEST) 3561da177e4SLinus Torvalds return 0; 3571da177e4SLinus Torvalds 35808678b08SYinghai Lu action = desc->action; 3591da177e4SLinus Torvalds if (action) 3603cca53b0SThomas Gleixner if (irqflags & action->flags & IRQF_SHARED) 3611da177e4SLinus Torvalds action = NULL; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds return !action; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3666a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc) 3676a6de9efSThomas Gleixner { 3686a6de9efSThomas Gleixner /* 3696a6de9efSThomas Gleixner * If the architecture still has not overriden 3706a6de9efSThomas Gleixner * the flow handler then zap the default. This 3716a6de9efSThomas Gleixner * should catch incorrect flow-type setting. 3726a6de9efSThomas Gleixner */ 3736a6de9efSThomas Gleixner if (desc->handle_irq == &handle_bad_irq) 3746a6de9efSThomas Gleixner desc->handle_irq = NULL; 3756a6de9efSThomas Gleixner } 3766a6de9efSThomas Gleixner 3770c5d1eb7SDavid Brownell int __irq_set_trigger(struct irq_desc *desc, unsigned int irq, 37882736f4dSUwe Kleine-König unsigned long flags) 37982736f4dSUwe Kleine-König { 38082736f4dSUwe Kleine-König int ret; 3810c5d1eb7SDavid Brownell struct irq_chip *chip = desc->chip; 38282736f4dSUwe Kleine-König 38382736f4dSUwe Kleine-König if (!chip || !chip->set_type) { 38482736f4dSUwe Kleine-König /* 38582736f4dSUwe Kleine-König * IRQF_TRIGGER_* but the PIC does not support multiple 38682736f4dSUwe Kleine-König * flow-types? 38782736f4dSUwe Kleine-König */ 3883ff68a6aSMark Nelson pr_debug("No set_type function for IRQ %d (%s)\n", irq, 38982736f4dSUwe Kleine-König chip ? (chip->name ? : "unknown") : "unknown"); 39082736f4dSUwe Kleine-König return 0; 39182736f4dSUwe Kleine-König } 39282736f4dSUwe Kleine-König 393f2b662daSDavid Brownell /* caller masked out all except trigger mode flags */ 394f2b662daSDavid Brownell ret = chip->set_type(irq, flags); 39582736f4dSUwe Kleine-König 39682736f4dSUwe Kleine-König if (ret) 397c69ad71bSDavid Brownell pr_err("setting trigger mode %d for irq %u failed (%pF)\n", 398f2b662daSDavid Brownell (int)flags, irq, chip->set_type); 3990c5d1eb7SDavid Brownell else { 400f2b662daSDavid Brownell if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) 401f2b662daSDavid Brownell flags |= IRQ_LEVEL; 4020c5d1eb7SDavid Brownell /* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */ 403f2b662daSDavid Brownell desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK); 404f2b662daSDavid Brownell desc->status |= flags; 4050c5d1eb7SDavid Brownell } 40682736f4dSUwe Kleine-König 40782736f4dSUwe Kleine-König return ret; 40882736f4dSUwe Kleine-König } 40982736f4dSUwe Kleine-König 4103aa551c9SThomas Gleixner static int irq_wait_for_interrupt(struct irqaction *action) 4113aa551c9SThomas Gleixner { 4123aa551c9SThomas Gleixner while (!kthread_should_stop()) { 4133aa551c9SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE); 414*f48fe81eSThomas Gleixner 415*f48fe81eSThomas Gleixner if (test_and_clear_bit(IRQTF_RUNTHREAD, 416*f48fe81eSThomas Gleixner &action->thread_flags)) { 4173aa551c9SThomas Gleixner __set_current_state(TASK_RUNNING); 4183aa551c9SThomas Gleixner return 0; 419*f48fe81eSThomas Gleixner } 4203aa551c9SThomas Gleixner schedule(); 4213aa551c9SThomas Gleixner } 4223aa551c9SThomas Gleixner return -1; 4233aa551c9SThomas Gleixner } 4243aa551c9SThomas Gleixner 4253aa551c9SThomas Gleixner /* 4263aa551c9SThomas Gleixner * Interrupt handler thread 4273aa551c9SThomas Gleixner */ 4283aa551c9SThomas Gleixner static int irq_thread(void *data) 4293aa551c9SThomas Gleixner { 4303aa551c9SThomas Gleixner struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, }; 4313aa551c9SThomas Gleixner struct irqaction *action = data; 4323aa551c9SThomas Gleixner struct irq_desc *desc = irq_to_desc(action->irq); 4333aa551c9SThomas Gleixner int wake; 4343aa551c9SThomas Gleixner 4353aa551c9SThomas Gleixner sched_setscheduler(current, SCHED_FIFO, ¶m); 4363aa551c9SThomas Gleixner current->irqaction = action; 4373aa551c9SThomas Gleixner 4383aa551c9SThomas Gleixner while (!irq_wait_for_interrupt(action)) { 4393aa551c9SThomas Gleixner 4403aa551c9SThomas Gleixner atomic_inc(&desc->threads_active); 4413aa551c9SThomas Gleixner 4423aa551c9SThomas Gleixner spin_lock_irq(&desc->lock); 4433aa551c9SThomas Gleixner if (unlikely(desc->status & IRQ_DISABLED)) { 4443aa551c9SThomas Gleixner /* 4453aa551c9SThomas Gleixner * CHECKME: We might need a dedicated 4463aa551c9SThomas Gleixner * IRQ_THREAD_PENDING flag here, which 4473aa551c9SThomas Gleixner * retriggers the thread in check_irq_resend() 4483aa551c9SThomas Gleixner * but AFAICT IRQ_PENDING should be fine as it 4493aa551c9SThomas Gleixner * retriggers the interrupt itself --- tglx 4503aa551c9SThomas Gleixner */ 4513aa551c9SThomas Gleixner desc->status |= IRQ_PENDING; 4523aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 4533aa551c9SThomas Gleixner } else { 4543aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 4553aa551c9SThomas Gleixner 4563aa551c9SThomas Gleixner action->thread_fn(action->irq, action->dev_id); 4573aa551c9SThomas Gleixner } 4583aa551c9SThomas Gleixner 4593aa551c9SThomas Gleixner wake = atomic_dec_and_test(&desc->threads_active); 4603aa551c9SThomas Gleixner 4613aa551c9SThomas Gleixner if (wake && waitqueue_active(&desc->wait_for_threads)) 4623aa551c9SThomas Gleixner wake_up(&desc->wait_for_threads); 4633aa551c9SThomas Gleixner } 4643aa551c9SThomas Gleixner 4653aa551c9SThomas Gleixner /* 4663aa551c9SThomas Gleixner * Clear irqaction. Otherwise exit_irq_thread() would make 4673aa551c9SThomas Gleixner * fuzz about an active irq thread going into nirvana. 4683aa551c9SThomas Gleixner */ 4693aa551c9SThomas Gleixner current->irqaction = NULL; 4703aa551c9SThomas Gleixner return 0; 4713aa551c9SThomas Gleixner } 4723aa551c9SThomas Gleixner 4733aa551c9SThomas Gleixner /* 4743aa551c9SThomas Gleixner * Called from do_exit() 4753aa551c9SThomas Gleixner */ 4763aa551c9SThomas Gleixner void exit_irq_thread(void) 4773aa551c9SThomas Gleixner { 4783aa551c9SThomas Gleixner struct task_struct *tsk = current; 4793aa551c9SThomas Gleixner 4803aa551c9SThomas Gleixner if (!tsk->irqaction) 4813aa551c9SThomas Gleixner return; 4823aa551c9SThomas Gleixner 4833aa551c9SThomas Gleixner printk(KERN_ERR 4843aa551c9SThomas Gleixner "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n", 4853aa551c9SThomas Gleixner tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq); 4863aa551c9SThomas Gleixner 4873aa551c9SThomas Gleixner /* 4883aa551c9SThomas Gleixner * Set the THREAD DIED flag to prevent further wakeups of the 4893aa551c9SThomas Gleixner * soon to be gone threaded handler. 4903aa551c9SThomas Gleixner */ 4913aa551c9SThomas Gleixner set_bit(IRQTF_DIED, &tsk->irqaction->flags); 4923aa551c9SThomas Gleixner } 4933aa551c9SThomas Gleixner 4941da177e4SLinus Torvalds /* 4951da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 4961da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 4971da177e4SLinus Torvalds */ 498d3c60047SThomas Gleixner static int 499d3c60047SThomas Gleixner __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new) 5001da177e4SLinus Torvalds { 501f17c7545SIngo Molnar struct irqaction *old, **old_ptr; 5028b126b77SAndrew Morton const char *old_name = NULL; 5031da177e4SLinus Torvalds unsigned long flags; 5041da177e4SLinus Torvalds int shared = 0; 50582736f4dSUwe Kleine-König int ret; 5061da177e4SLinus Torvalds 5077d94f7caSYinghai Lu if (!desc) 508c2b5a251SMatthew Wilcox return -EINVAL; 509c2b5a251SMatthew Wilcox 510f1c2662cSIngo Molnar if (desc->chip == &no_irq_chip) 5111da177e4SLinus Torvalds return -ENOSYS; 5121da177e4SLinus Torvalds /* 5131da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 5141da177e4SLinus Torvalds * so we have to be careful not to interfere with a 5151da177e4SLinus Torvalds * running system. 5161da177e4SLinus Torvalds */ 5173cca53b0SThomas Gleixner if (new->flags & IRQF_SAMPLE_RANDOM) { 5181da177e4SLinus Torvalds /* 5191da177e4SLinus Torvalds * This function might sleep, we want to call it first, 5201da177e4SLinus Torvalds * outside of the atomic block. 5211da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 5221da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 5231da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 5241da177e4SLinus Torvalds * only the sysadmin is able to do this. 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds rand_initialize_irq(irq); 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds /* 5303aa551c9SThomas Gleixner * Threaded handler ? 5313aa551c9SThomas Gleixner */ 5323aa551c9SThomas Gleixner if (new->thread_fn) { 5333aa551c9SThomas Gleixner struct task_struct *t; 5343aa551c9SThomas Gleixner 5353aa551c9SThomas Gleixner t = kthread_create(irq_thread, new, "irq/%d-%s", irq, 5363aa551c9SThomas Gleixner new->name); 5373aa551c9SThomas Gleixner if (IS_ERR(t)) 5383aa551c9SThomas Gleixner return PTR_ERR(t); 5393aa551c9SThomas Gleixner /* 5403aa551c9SThomas Gleixner * We keep the reference to the task struct even if 5413aa551c9SThomas Gleixner * the thread dies to avoid that the interrupt code 5423aa551c9SThomas Gleixner * references an already freed task_struct. 5433aa551c9SThomas Gleixner */ 5443aa551c9SThomas Gleixner get_task_struct(t); 5453aa551c9SThomas Gleixner new->thread = t; 5463aa551c9SThomas Gleixner wake_up_process(t); 5473aa551c9SThomas Gleixner } 5483aa551c9SThomas Gleixner 5493aa551c9SThomas Gleixner /* 5501da177e4SLinus Torvalds * The following block of code has to be executed atomically 5511da177e4SLinus Torvalds */ 5521da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 553f17c7545SIngo Molnar old_ptr = &desc->action; 554f17c7545SIngo Molnar old = *old_ptr; 55506fcb0c6SIngo Molnar if (old) { 556e76de9f8SThomas Gleixner /* 557e76de9f8SThomas Gleixner * Can't share interrupts unless both agree to and are 558e76de9f8SThomas Gleixner * the same type (level, edge, polarity). So both flag 5593cca53b0SThomas Gleixner * fields must have IRQF_SHARED set and the bits which 560e76de9f8SThomas Gleixner * set the trigger type must match. 561e76de9f8SThomas Gleixner */ 5623cca53b0SThomas Gleixner if (!((old->flags & new->flags) & IRQF_SHARED) || 5638b126b77SAndrew Morton ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) { 5648b126b77SAndrew Morton old_name = old->name; 565f5163427SDimitri Sivanich goto mismatch; 5668b126b77SAndrew Morton } 567f5163427SDimitri Sivanich 568284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU) 569f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 5703cca53b0SThomas Gleixner if ((old->flags & IRQF_PERCPU) != 5713cca53b0SThomas Gleixner (new->flags & IRQF_PERCPU)) 572f5163427SDimitri Sivanich goto mismatch; 573f5163427SDimitri Sivanich #endif 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 5761da177e4SLinus Torvalds do { 577f17c7545SIngo Molnar old_ptr = &old->next; 578f17c7545SIngo Molnar old = *old_ptr; 5791da177e4SLinus Torvalds } while (old); 5801da177e4SLinus Torvalds shared = 1; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds if (!shared) { 5846a6de9efSThomas Gleixner irq_chip_set_defaults(desc->chip); 585e76de9f8SThomas Gleixner 5863aa551c9SThomas Gleixner init_waitqueue_head(&desc->wait_for_threads); 5873aa551c9SThomas Gleixner 58882736f4dSUwe Kleine-König /* Setup the type (level, edge polarity) if configured: */ 58982736f4dSUwe Kleine-König if (new->flags & IRQF_TRIGGER_MASK) { 590f2b662daSDavid Brownell ret = __irq_set_trigger(desc, irq, 591f2b662daSDavid Brownell new->flags & IRQF_TRIGGER_MASK); 59282736f4dSUwe Kleine-König 5933aa551c9SThomas Gleixner if (ret) 5943aa551c9SThomas Gleixner goto out_thread; 59582736f4dSUwe Kleine-König } else 59682736f4dSUwe Kleine-König compat_irq_chip_set_default_handler(desc); 597f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU) 598f75d222bSAhmed S. Darwish if (new->flags & IRQF_PERCPU) 599f75d222bSAhmed S. Darwish desc->status |= IRQ_PER_CPU; 600f75d222bSAhmed S. Darwish #endif 601f75d222bSAhmed S. Darwish 60294d39e1fSThomas Gleixner desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | 6031adb0850SThomas Gleixner IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED); 60494d39e1fSThomas Gleixner 60594d39e1fSThomas Gleixner if (!(desc->status & IRQ_NOAUTOEN)) { 6061da177e4SLinus Torvalds desc->depth = 0; 60794d39e1fSThomas Gleixner desc->status &= ~IRQ_DISABLED; 608d1bef4edSIngo Molnar desc->chip->startup(irq); 609e76de9f8SThomas Gleixner } else 610e76de9f8SThomas Gleixner /* Undo nested disables: */ 611e76de9f8SThomas Gleixner desc->depth = 1; 61218404756SMax Krasnyansky 613612e3684SThomas Gleixner /* Exclude IRQ from balancing if requested */ 614612e3684SThomas Gleixner if (new->flags & IRQF_NOBALANCING) 615612e3684SThomas Gleixner desc->status |= IRQ_NO_BALANCING; 616612e3684SThomas Gleixner 61718404756SMax Krasnyansky /* Set default affinity mask once everything is setup */ 618548c8933SHannes Eder setup_affinity(irq, desc); 6190c5d1eb7SDavid Brownell 6200c5d1eb7SDavid Brownell } else if ((new->flags & IRQF_TRIGGER_MASK) 6210c5d1eb7SDavid Brownell && (new->flags & IRQF_TRIGGER_MASK) 6220c5d1eb7SDavid Brownell != (desc->status & IRQ_TYPE_SENSE_MASK)) { 6230c5d1eb7SDavid Brownell /* hope the handler works with the actual trigger mode... */ 6240c5d1eb7SDavid Brownell pr_warning("IRQ %d uses trigger mode %d; requested %d\n", 6250c5d1eb7SDavid Brownell irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK), 6260c5d1eb7SDavid Brownell (int)(new->flags & IRQF_TRIGGER_MASK)); 62794d39e1fSThomas Gleixner } 62882736f4dSUwe Kleine-König 629f17c7545SIngo Molnar *old_ptr = new; 63082736f4dSUwe Kleine-König 6318528b0f1SLinus Torvalds /* Reset broken irq detection when installing new handler */ 6328528b0f1SLinus Torvalds desc->irq_count = 0; 6338528b0f1SLinus Torvalds desc->irqs_unhandled = 0; 6341adb0850SThomas Gleixner 6351adb0850SThomas Gleixner /* 6361adb0850SThomas Gleixner * Check whether we disabled the irq via the spurious handler 6371adb0850SThomas Gleixner * before. Reenable it and give it another chance. 6381adb0850SThomas Gleixner */ 6391adb0850SThomas Gleixner if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) { 6401adb0850SThomas Gleixner desc->status &= ~IRQ_SPURIOUS_DISABLED; 6411adb0850SThomas Gleixner __enable_irq(desc, irq); 6421adb0850SThomas Gleixner } 6431adb0850SThomas Gleixner 6441da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds new->irq = irq; 6472c6927a3SYinghai Lu register_irq_proc(irq, desc); 6481da177e4SLinus Torvalds new->dir = NULL; 6491da177e4SLinus Torvalds register_handler_proc(irq, new); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds return 0; 652f5163427SDimitri Sivanich 653f5163427SDimitri Sivanich mismatch: 6543f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ 6553cca53b0SThomas Gleixner if (!(new->flags & IRQF_PROBE_SHARED)) { 656e8c4b9d0SBjorn Helgaas printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq); 6578b126b77SAndrew Morton if (old_name) 6588b126b77SAndrew Morton printk(KERN_ERR "current handler: %s\n", old_name); 659f5163427SDimitri Sivanich dump_stack(); 66013e87ec6SAndrew Morton } 6613f050447SAlan Cox #endif 6623aa551c9SThomas Gleixner ret = -EBUSY; 6633aa551c9SThomas Gleixner 6643aa551c9SThomas Gleixner out_thread: 6658b126b77SAndrew Morton spin_unlock_irqrestore(&desc->lock, flags); 6663aa551c9SThomas Gleixner if (new->thread) { 6673aa551c9SThomas Gleixner struct task_struct *t = new->thread; 6683aa551c9SThomas Gleixner 6693aa551c9SThomas Gleixner new->thread = NULL; 6703aa551c9SThomas Gleixner if (likely(!test_bit(IRQTF_DIED, &new->thread_flags))) 6713aa551c9SThomas Gleixner kthread_stop(t); 6723aa551c9SThomas Gleixner put_task_struct(t); 6733aa551c9SThomas Gleixner } 6743aa551c9SThomas Gleixner return ret; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds /** 678d3c60047SThomas Gleixner * setup_irq - setup an interrupt 679d3c60047SThomas Gleixner * @irq: Interrupt line to setup 680d3c60047SThomas Gleixner * @act: irqaction for the interrupt 681d3c60047SThomas Gleixner * 682d3c60047SThomas Gleixner * Used to statically setup interrupts in the early boot process. 683d3c60047SThomas Gleixner */ 684d3c60047SThomas Gleixner int setup_irq(unsigned int irq, struct irqaction *act) 685d3c60047SThomas Gleixner { 686d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 687d3c60047SThomas Gleixner 688d3c60047SThomas Gleixner return __setup_irq(irq, desc, act); 689d3c60047SThomas Gleixner } 690eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(setup_irq); 691d3c60047SThomas Gleixner 692cbf94f06SMagnus Damm /* 693cbf94f06SMagnus Damm * Internal function to unregister an irqaction - used to free 694cbf94f06SMagnus Damm * regular and special interrupts that are part of the architecture. 6951da177e4SLinus Torvalds */ 696cbf94f06SMagnus Damm static struct irqaction *__free_irq(unsigned int irq, void *dev_id) 6971da177e4SLinus Torvalds { 698d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 699f17c7545SIngo Molnar struct irqaction *action, **action_ptr; 7003aa551c9SThomas Gleixner struct task_struct *irqthread; 7011da177e4SLinus Torvalds unsigned long flags; 7021da177e4SLinus Torvalds 703ae88a23bSIngo Molnar WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 7047d94f7caSYinghai Lu 7057d94f7caSYinghai Lu if (!desc) 706f21cfb25SMagnus Damm return NULL; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 709ae88a23bSIngo Molnar 710ae88a23bSIngo Molnar /* 711ae88a23bSIngo Molnar * There can be multiple actions per IRQ descriptor, find the right 712ae88a23bSIngo Molnar * one based on the dev_id: 713ae88a23bSIngo Molnar */ 714f17c7545SIngo Molnar action_ptr = &desc->action; 7151da177e4SLinus Torvalds for (;;) { 716f17c7545SIngo Molnar action = *action_ptr; 7171da177e4SLinus Torvalds 718ae88a23bSIngo Molnar if (!action) { 719ae88a23bSIngo Molnar WARN(1, "Trying to free already-free IRQ %d\n", irq); 720ae88a23bSIngo Molnar spin_unlock_irqrestore(&desc->lock, flags); 721ae88a23bSIngo Molnar 722f21cfb25SMagnus Damm return NULL; 723ae88a23bSIngo Molnar } 7241da177e4SLinus Torvalds 7258316e381SIngo Molnar if (action->dev_id == dev_id) 726ae88a23bSIngo Molnar break; 727f17c7545SIngo Molnar action_ptr = &action->next; 728ae88a23bSIngo Molnar } 729ae88a23bSIngo Molnar 730ae88a23bSIngo Molnar /* Found it - now remove it from the list of entries: */ 731f17c7545SIngo Molnar *action_ptr = action->next; 732dbce706eSPaolo 'Blaisorblade' Giarrusso 733ae88a23bSIngo Molnar /* Currently used only by UML, might disappear one day: */ 734b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 735d1bef4edSIngo Molnar if (desc->chip->release) 736d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 737b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 738dbce706eSPaolo 'Blaisorblade' Giarrusso 739ae88a23bSIngo Molnar /* If this was the last handler, shut down the IRQ line: */ 7401da177e4SLinus Torvalds if (!desc->action) { 7411da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 742d1bef4edSIngo Molnar if (desc->chip->shutdown) 743d1bef4edSIngo Molnar desc->chip->shutdown(irq); 7441da177e4SLinus Torvalds else 745d1bef4edSIngo Molnar desc->chip->disable(irq); 7461da177e4SLinus Torvalds } 7473aa551c9SThomas Gleixner 7483aa551c9SThomas Gleixner irqthread = action->thread; 7493aa551c9SThomas Gleixner action->thread = NULL; 7503aa551c9SThomas Gleixner 7511da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 752ae88a23bSIngo Molnar 7531da177e4SLinus Torvalds unregister_handler_proc(irq, action); 7541da177e4SLinus Torvalds 755ae88a23bSIngo Molnar /* Make sure it's not being used on another CPU: */ 7561da177e4SLinus Torvalds synchronize_irq(irq); 757ae88a23bSIngo Molnar 7583aa551c9SThomas Gleixner if (irqthread) { 7593aa551c9SThomas Gleixner if (!test_bit(IRQTF_DIED, &action->thread_flags)) 7603aa551c9SThomas Gleixner kthread_stop(irqthread); 7613aa551c9SThomas Gleixner put_task_struct(irqthread); 7623aa551c9SThomas Gleixner } 7633aa551c9SThomas Gleixner 7641d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 7651d99493bSDavid Woodhouse /* 766ae88a23bSIngo Molnar * It's a shared IRQ -- the driver ought to be prepared for an IRQ 767ae88a23bSIngo Molnar * event to happen even now it's being freed, so let's make sure that 768ae88a23bSIngo Molnar * is so by doing an extra call to the handler .... 769ae88a23bSIngo Molnar * 770ae88a23bSIngo Molnar * ( We do this after actually deregistering it, to make sure that a 771ae88a23bSIngo Molnar * 'real' IRQ doesn't run in * parallel with our fake. ) 7721d99493bSDavid Woodhouse */ 7731d99493bSDavid Woodhouse if (action->flags & IRQF_SHARED) { 7741d99493bSDavid Woodhouse local_irq_save(flags); 7751d99493bSDavid Woodhouse action->handler(irq, dev_id); 7761d99493bSDavid Woodhouse local_irq_restore(flags); 7771d99493bSDavid Woodhouse } 7781d99493bSDavid Woodhouse #endif 779f21cfb25SMagnus Damm return action; 780f21cfb25SMagnus Damm } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /** 783cbf94f06SMagnus Damm * remove_irq - free an interrupt 784cbf94f06SMagnus Damm * @irq: Interrupt line to free 785cbf94f06SMagnus Damm * @act: irqaction for the interrupt 786cbf94f06SMagnus Damm * 787cbf94f06SMagnus Damm * Used to remove interrupts statically setup by the early boot process. 788cbf94f06SMagnus Damm */ 789cbf94f06SMagnus Damm void remove_irq(unsigned int irq, struct irqaction *act) 790cbf94f06SMagnus Damm { 791cbf94f06SMagnus Damm __free_irq(irq, act->dev_id); 792cbf94f06SMagnus Damm } 793eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(remove_irq); 794cbf94f06SMagnus Damm 795cbf94f06SMagnus Damm /** 796f21cfb25SMagnus Damm * free_irq - free an interrupt allocated with request_irq 7971da177e4SLinus Torvalds * @irq: Interrupt line to free 7981da177e4SLinus Torvalds * @dev_id: Device identity to free 7991da177e4SLinus Torvalds * 8001da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 8011da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 8021da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 8031da177e4SLinus Torvalds * on the card it drives before calling this function. The function 8041da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 8051da177e4SLinus Torvalds * have completed. 8061da177e4SLinus Torvalds * 8071da177e4SLinus Torvalds * This function must not be called from interrupt context. 8081da177e4SLinus Torvalds */ 8091da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 8101da177e4SLinus Torvalds { 811cbf94f06SMagnus Damm kfree(__free_irq(irq, dev_id)); 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds /** 8163aa551c9SThomas Gleixner * request_threaded_irq - allocate an interrupt line 8171da177e4SLinus Torvalds * @irq: Interrupt line to allocate 8183aa551c9SThomas Gleixner * @handler: Function to be called when the IRQ occurs. 8193aa551c9SThomas Gleixner * Primary handler for threaded interrupts 8203aa551c9SThomas Gleixner * @thread_fn: Function called from the irq handler thread 8213aa551c9SThomas Gleixner * If NULL, no irq thread is created 8221da177e4SLinus Torvalds * @irqflags: Interrupt type flags 8231da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 8241da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 8251da177e4SLinus Torvalds * 8261da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 8271da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 8281da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 8291da177e4SLinus Torvalds * your handler function must clear any interrupt the board 8301da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 8311da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 8321da177e4SLinus Torvalds * 8333aa551c9SThomas Gleixner * If you want to set up a threaded irq handler for your device 8343aa551c9SThomas Gleixner * then you need to supply @handler and @thread_fn. @handler ist 8353aa551c9SThomas Gleixner * still called in hard interrupt context and has to check 8363aa551c9SThomas Gleixner * whether the interrupt originates from the device. If yes it 8373aa551c9SThomas Gleixner * needs to disable the interrupt on the device and return 8383aa551c9SThomas Gleixner * IRQ_THREAD_WAKE which will wake up the handler thread and run 8393aa551c9SThomas Gleixner * @thread_fn. This split handler design is necessary to support 8403aa551c9SThomas Gleixner * shared interrupts. 8413aa551c9SThomas Gleixner * 8421da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 8431da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 8441da177e4SLinus Torvalds * receives this value it makes sense to use it. 8451da177e4SLinus Torvalds * 8461da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 8471da177e4SLinus Torvalds * as this is required when freeing the interrupt. 8481da177e4SLinus Torvalds * 8491da177e4SLinus Torvalds * Flags: 8501da177e4SLinus Torvalds * 8513cca53b0SThomas Gleixner * IRQF_SHARED Interrupt is shared 8523cca53b0SThomas Gleixner * IRQF_DISABLED Disable local interrupts while processing 8533cca53b0SThomas Gleixner * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy 8540c5d1eb7SDavid Brownell * IRQF_TRIGGER_* Specify active edge(s) or level 8551da177e4SLinus Torvalds * 8561da177e4SLinus Torvalds */ 8573aa551c9SThomas Gleixner int request_threaded_irq(unsigned int irq, irq_handler_t handler, 8583aa551c9SThomas Gleixner irq_handler_t thread_fn, unsigned long irqflags, 8593aa551c9SThomas Gleixner const char *devname, void *dev_id) 8601da177e4SLinus Torvalds { 8611da177e4SLinus Torvalds struct irqaction *action; 86208678b08SYinghai Lu struct irq_desc *desc; 863d3c60047SThomas Gleixner int retval; 8641da177e4SLinus Torvalds 865470c6623SDavid Brownell /* 866470c6623SDavid Brownell * handle_IRQ_event() always ignores IRQF_DISABLED except for 867470c6623SDavid Brownell * the _first_ irqaction (sigh). That can cause oopsing, but 868470c6623SDavid Brownell * the behavior is classified as "will not fix" so we need to 869470c6623SDavid Brownell * start nudging drivers away from using that idiom. 870470c6623SDavid Brownell */ 871327ec569SIngo Molnar if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) == 872327ec569SIngo Molnar (IRQF_SHARED|IRQF_DISABLED)) { 873327ec569SIngo Molnar pr_warning( 874327ec569SIngo Molnar "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n", 875470c6623SDavid Brownell irq, devname); 876327ec569SIngo Molnar } 877470c6623SDavid Brownell 878fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 879fbb9ce95SIngo Molnar /* 880fbb9ce95SIngo Molnar * Lockdep wants atomic interrupt handlers: 881fbb9ce95SIngo Molnar */ 88238515e90SThomas Gleixner irqflags |= IRQF_DISABLED; 883fbb9ce95SIngo Molnar #endif 8841da177e4SLinus Torvalds /* 8851da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 8861da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 8871da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 8881da177e4SLinus Torvalds * logic etc). 8891da177e4SLinus Torvalds */ 8903cca53b0SThomas Gleixner if ((irqflags & IRQF_SHARED) && !dev_id) 8911da177e4SLinus Torvalds return -EINVAL; 8927d94f7caSYinghai Lu 893cb5bc832SYinghai Lu desc = irq_to_desc(irq); 8947d94f7caSYinghai Lu if (!desc) 8951da177e4SLinus Torvalds return -EINVAL; 8967d94f7caSYinghai Lu 89708678b08SYinghai Lu if (desc->status & IRQ_NOREQUEST) 8986550c775SThomas Gleixner return -EINVAL; 8991da177e4SLinus Torvalds if (!handler) 9001da177e4SLinus Torvalds return -EINVAL; 9011da177e4SLinus Torvalds 90245535732SThomas Gleixner action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 9031da177e4SLinus Torvalds if (!action) 9041da177e4SLinus Torvalds return -ENOMEM; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds action->handler = handler; 9073aa551c9SThomas Gleixner action->thread_fn = thread_fn; 9081da177e4SLinus Torvalds action->flags = irqflags; 9091da177e4SLinus Torvalds action->name = devname; 9101da177e4SLinus Torvalds action->dev_id = dev_id; 9111da177e4SLinus Torvalds 912d3c60047SThomas Gleixner retval = __setup_irq(irq, desc, action); 913377bf1e4SAnton Vorontsov if (retval) 914377bf1e4SAnton Vorontsov kfree(action); 915377bf1e4SAnton Vorontsov 916a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 917a304e1b8SDavid Woodhouse if (irqflags & IRQF_SHARED) { 918a304e1b8SDavid Woodhouse /* 919a304e1b8SDavid Woodhouse * It's a shared IRQ -- the driver ought to be prepared for it 920a304e1b8SDavid Woodhouse * to happen immediately, so let's make sure.... 921377bf1e4SAnton Vorontsov * We disable the irq to make sure that a 'real' IRQ doesn't 922377bf1e4SAnton Vorontsov * run in parallel with our fake. 923a304e1b8SDavid Woodhouse */ 924a304e1b8SDavid Woodhouse unsigned long flags; 925a304e1b8SDavid Woodhouse 926377bf1e4SAnton Vorontsov disable_irq(irq); 927a304e1b8SDavid Woodhouse local_irq_save(flags); 928377bf1e4SAnton Vorontsov 929a304e1b8SDavid Woodhouse handler(irq, dev_id); 930377bf1e4SAnton Vorontsov 931a304e1b8SDavid Woodhouse local_irq_restore(flags); 932377bf1e4SAnton Vorontsov enable_irq(irq); 933a304e1b8SDavid Woodhouse } 934a304e1b8SDavid Woodhouse #endif 9351da177e4SLinus Torvalds return retval; 9361da177e4SLinus Torvalds } 9373aa551c9SThomas Gleixner EXPORT_SYMBOL(request_threaded_irq); 938