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> 11*3aa551c9SThomas 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> 16*3aa551c9SThomas 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); 55*3aa551c9SThomas Gleixner 56*3aa551c9SThomas Gleixner /* 57*3aa551c9SThomas Gleixner * We made sure that no hardirq handler is running. Now verify 58*3aa551c9SThomas Gleixner * that no threaded handlers are active. 59*3aa551c9SThomas Gleixner */ 60*3aa551c9SThomas Gleixner wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active)); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds EXPORT_SYMBOL(synchronize_irq); 631da177e4SLinus Torvalds 64*3aa551c9SThomas Gleixner #ifdef CONFIG_SMP 65*3aa551c9SThomas Gleixner cpumask_var_t irq_default_affinity; 66*3aa551c9SThomas 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 83*3aa551c9SThomas Gleixner static void 84*3aa551c9SThomas Gleixner irq_set_thread_affinity(struct irq_desc *desc, const struct cpumask *cpumask) 85*3aa551c9SThomas Gleixner { 86*3aa551c9SThomas Gleixner struct irqaction *action = desc->action; 87*3aa551c9SThomas Gleixner 88*3aa551c9SThomas Gleixner while (action) { 89*3aa551c9SThomas Gleixner if (action->thread) 90*3aa551c9SThomas Gleixner set_cpus_allowed_ptr(action->thread, cpumask); 91*3aa551c9SThomas Gleixner action = action->next; 92*3aa551c9SThomas Gleixner } 93*3aa551c9SThomas Gleixner } 94*3aa551c9SThomas 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 123*3aa551c9SThomas 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); 174*3aa551c9SThomas Gleixner if (!ret) 175*3aa551c9SThomas 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 410*3aa551c9SThomas Gleixner static inline int irq_thread_should_run(struct irqaction *action) 411*3aa551c9SThomas Gleixner { 412*3aa551c9SThomas Gleixner return test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags); 413*3aa551c9SThomas Gleixner } 414*3aa551c9SThomas Gleixner 415*3aa551c9SThomas Gleixner static int irq_wait_for_interrupt(struct irqaction *action) 416*3aa551c9SThomas Gleixner { 417*3aa551c9SThomas Gleixner while (!kthread_should_stop()) { 418*3aa551c9SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE); 419*3aa551c9SThomas Gleixner if (irq_thread_should_run(action)) { 420*3aa551c9SThomas Gleixner __set_current_state(TASK_RUNNING); 421*3aa551c9SThomas Gleixner return 0; 422*3aa551c9SThomas Gleixner } else 423*3aa551c9SThomas Gleixner schedule(); 424*3aa551c9SThomas Gleixner } 425*3aa551c9SThomas Gleixner return -1; 426*3aa551c9SThomas Gleixner } 427*3aa551c9SThomas Gleixner 428*3aa551c9SThomas Gleixner /* 429*3aa551c9SThomas Gleixner * Interrupt handler thread 430*3aa551c9SThomas Gleixner */ 431*3aa551c9SThomas Gleixner static int irq_thread(void *data) 432*3aa551c9SThomas Gleixner { 433*3aa551c9SThomas Gleixner struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, }; 434*3aa551c9SThomas Gleixner struct irqaction *action = data; 435*3aa551c9SThomas Gleixner struct irq_desc *desc = irq_to_desc(action->irq); 436*3aa551c9SThomas Gleixner int wake; 437*3aa551c9SThomas Gleixner 438*3aa551c9SThomas Gleixner sched_setscheduler(current, SCHED_FIFO, ¶m); 439*3aa551c9SThomas Gleixner current->irqaction = action; 440*3aa551c9SThomas Gleixner 441*3aa551c9SThomas Gleixner while (!irq_wait_for_interrupt(action)) { 442*3aa551c9SThomas Gleixner 443*3aa551c9SThomas Gleixner atomic_inc(&desc->threads_active); 444*3aa551c9SThomas Gleixner 445*3aa551c9SThomas Gleixner spin_lock_irq(&desc->lock); 446*3aa551c9SThomas Gleixner if (unlikely(desc->status & IRQ_DISABLED)) { 447*3aa551c9SThomas Gleixner /* 448*3aa551c9SThomas Gleixner * CHECKME: We might need a dedicated 449*3aa551c9SThomas Gleixner * IRQ_THREAD_PENDING flag here, which 450*3aa551c9SThomas Gleixner * retriggers the thread in check_irq_resend() 451*3aa551c9SThomas Gleixner * but AFAICT IRQ_PENDING should be fine as it 452*3aa551c9SThomas Gleixner * retriggers the interrupt itself --- tglx 453*3aa551c9SThomas Gleixner */ 454*3aa551c9SThomas Gleixner desc->status |= IRQ_PENDING; 455*3aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 456*3aa551c9SThomas Gleixner } else { 457*3aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 458*3aa551c9SThomas Gleixner 459*3aa551c9SThomas Gleixner action->thread_fn(action->irq, action->dev_id); 460*3aa551c9SThomas Gleixner } 461*3aa551c9SThomas Gleixner 462*3aa551c9SThomas Gleixner wake = atomic_dec_and_test(&desc->threads_active); 463*3aa551c9SThomas Gleixner 464*3aa551c9SThomas Gleixner if (wake && waitqueue_active(&desc->wait_for_threads)) 465*3aa551c9SThomas Gleixner wake_up(&desc->wait_for_threads); 466*3aa551c9SThomas Gleixner } 467*3aa551c9SThomas Gleixner 468*3aa551c9SThomas Gleixner /* 469*3aa551c9SThomas Gleixner * Clear irqaction. Otherwise exit_irq_thread() would make 470*3aa551c9SThomas Gleixner * fuzz about an active irq thread going into nirvana. 471*3aa551c9SThomas Gleixner */ 472*3aa551c9SThomas Gleixner current->irqaction = NULL; 473*3aa551c9SThomas Gleixner return 0; 474*3aa551c9SThomas Gleixner } 475*3aa551c9SThomas Gleixner 476*3aa551c9SThomas Gleixner /* 477*3aa551c9SThomas Gleixner * Called from do_exit() 478*3aa551c9SThomas Gleixner */ 479*3aa551c9SThomas Gleixner void exit_irq_thread(void) 480*3aa551c9SThomas Gleixner { 481*3aa551c9SThomas Gleixner struct task_struct *tsk = current; 482*3aa551c9SThomas Gleixner 483*3aa551c9SThomas Gleixner if (!tsk->irqaction) 484*3aa551c9SThomas Gleixner return; 485*3aa551c9SThomas Gleixner 486*3aa551c9SThomas Gleixner printk(KERN_ERR 487*3aa551c9SThomas Gleixner "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n", 488*3aa551c9SThomas Gleixner tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq); 489*3aa551c9SThomas Gleixner 490*3aa551c9SThomas Gleixner /* 491*3aa551c9SThomas Gleixner * Set the THREAD DIED flag to prevent further wakeups of the 492*3aa551c9SThomas Gleixner * soon to be gone threaded handler. 493*3aa551c9SThomas Gleixner */ 494*3aa551c9SThomas Gleixner set_bit(IRQTF_DIED, &tsk->irqaction->flags); 495*3aa551c9SThomas Gleixner } 496*3aa551c9SThomas Gleixner 4971da177e4SLinus Torvalds /* 4981da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 4991da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 5001da177e4SLinus Torvalds */ 501d3c60047SThomas Gleixner static int 502d3c60047SThomas Gleixner __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new) 5031da177e4SLinus Torvalds { 504f17c7545SIngo Molnar struct irqaction *old, **old_ptr; 5058b126b77SAndrew Morton const char *old_name = NULL; 5061da177e4SLinus Torvalds unsigned long flags; 5071da177e4SLinus Torvalds int shared = 0; 50882736f4dSUwe Kleine-König int ret; 5091da177e4SLinus Torvalds 5107d94f7caSYinghai Lu if (!desc) 511c2b5a251SMatthew Wilcox return -EINVAL; 512c2b5a251SMatthew Wilcox 513f1c2662cSIngo Molnar if (desc->chip == &no_irq_chip) 5141da177e4SLinus Torvalds return -ENOSYS; 5151da177e4SLinus Torvalds /* 5161da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 5171da177e4SLinus Torvalds * so we have to be careful not to interfere with a 5181da177e4SLinus Torvalds * running system. 5191da177e4SLinus Torvalds */ 5203cca53b0SThomas Gleixner if (new->flags & IRQF_SAMPLE_RANDOM) { 5211da177e4SLinus Torvalds /* 5221da177e4SLinus Torvalds * This function might sleep, we want to call it first, 5231da177e4SLinus Torvalds * outside of the atomic block. 5241da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 5251da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 5261da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 5271da177e4SLinus Torvalds * only the sysadmin is able to do this. 5281da177e4SLinus Torvalds */ 5291da177e4SLinus Torvalds rand_initialize_irq(irq); 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds /* 533*3aa551c9SThomas Gleixner * Threaded handler ? 534*3aa551c9SThomas Gleixner */ 535*3aa551c9SThomas Gleixner if (new->thread_fn) { 536*3aa551c9SThomas Gleixner struct task_struct *t; 537*3aa551c9SThomas Gleixner 538*3aa551c9SThomas Gleixner t = kthread_create(irq_thread, new, "irq/%d-%s", irq, 539*3aa551c9SThomas Gleixner new->name); 540*3aa551c9SThomas Gleixner if (IS_ERR(t)) 541*3aa551c9SThomas Gleixner return PTR_ERR(t); 542*3aa551c9SThomas Gleixner /* 543*3aa551c9SThomas Gleixner * We keep the reference to the task struct even if 544*3aa551c9SThomas Gleixner * the thread dies to avoid that the interrupt code 545*3aa551c9SThomas Gleixner * references an already freed task_struct. 546*3aa551c9SThomas Gleixner */ 547*3aa551c9SThomas Gleixner get_task_struct(t); 548*3aa551c9SThomas Gleixner new->thread = t; 549*3aa551c9SThomas Gleixner wake_up_process(t); 550*3aa551c9SThomas Gleixner } 551*3aa551c9SThomas Gleixner 552*3aa551c9SThomas Gleixner /* 5531da177e4SLinus Torvalds * The following block of code has to be executed atomically 5541da177e4SLinus Torvalds */ 5551da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 556f17c7545SIngo Molnar old_ptr = &desc->action; 557f17c7545SIngo Molnar old = *old_ptr; 55806fcb0c6SIngo Molnar if (old) { 559e76de9f8SThomas Gleixner /* 560e76de9f8SThomas Gleixner * Can't share interrupts unless both agree to and are 561e76de9f8SThomas Gleixner * the same type (level, edge, polarity). So both flag 5623cca53b0SThomas Gleixner * fields must have IRQF_SHARED set and the bits which 563e76de9f8SThomas Gleixner * set the trigger type must match. 564e76de9f8SThomas Gleixner */ 5653cca53b0SThomas Gleixner if (!((old->flags & new->flags) & IRQF_SHARED) || 5668b126b77SAndrew Morton ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) { 5678b126b77SAndrew Morton old_name = old->name; 568f5163427SDimitri Sivanich goto mismatch; 5698b126b77SAndrew Morton } 570f5163427SDimitri Sivanich 571284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU) 572f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 5733cca53b0SThomas Gleixner if ((old->flags & IRQF_PERCPU) != 5743cca53b0SThomas Gleixner (new->flags & IRQF_PERCPU)) 575f5163427SDimitri Sivanich goto mismatch; 576f5163427SDimitri Sivanich #endif 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 5791da177e4SLinus Torvalds do { 580f17c7545SIngo Molnar old_ptr = &old->next; 581f17c7545SIngo Molnar old = *old_ptr; 5821da177e4SLinus Torvalds } while (old); 5831da177e4SLinus Torvalds shared = 1; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (!shared) { 5876a6de9efSThomas Gleixner irq_chip_set_defaults(desc->chip); 588e76de9f8SThomas Gleixner 589*3aa551c9SThomas Gleixner init_waitqueue_head(&desc->wait_for_threads); 590*3aa551c9SThomas Gleixner 59182736f4dSUwe Kleine-König /* Setup the type (level, edge polarity) if configured: */ 59282736f4dSUwe Kleine-König if (new->flags & IRQF_TRIGGER_MASK) { 593f2b662daSDavid Brownell ret = __irq_set_trigger(desc, irq, 594f2b662daSDavid Brownell new->flags & IRQF_TRIGGER_MASK); 59582736f4dSUwe Kleine-König 596*3aa551c9SThomas Gleixner if (ret) 597*3aa551c9SThomas Gleixner goto out_thread; 59882736f4dSUwe Kleine-König } else 59982736f4dSUwe Kleine-König compat_irq_chip_set_default_handler(desc); 600f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU) 601f75d222bSAhmed S. Darwish if (new->flags & IRQF_PERCPU) 602f75d222bSAhmed S. Darwish desc->status |= IRQ_PER_CPU; 603f75d222bSAhmed S. Darwish #endif 604f75d222bSAhmed S. Darwish 60594d39e1fSThomas Gleixner desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | 6061adb0850SThomas Gleixner IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED); 60794d39e1fSThomas Gleixner 60894d39e1fSThomas Gleixner if (!(desc->status & IRQ_NOAUTOEN)) { 6091da177e4SLinus Torvalds desc->depth = 0; 61094d39e1fSThomas Gleixner desc->status &= ~IRQ_DISABLED; 611d1bef4edSIngo Molnar desc->chip->startup(irq); 612e76de9f8SThomas Gleixner } else 613e76de9f8SThomas Gleixner /* Undo nested disables: */ 614e76de9f8SThomas Gleixner desc->depth = 1; 61518404756SMax Krasnyansky 616612e3684SThomas Gleixner /* Exclude IRQ from balancing if requested */ 617612e3684SThomas Gleixner if (new->flags & IRQF_NOBALANCING) 618612e3684SThomas Gleixner desc->status |= IRQ_NO_BALANCING; 619612e3684SThomas Gleixner 62018404756SMax Krasnyansky /* Set default affinity mask once everything is setup */ 621548c8933SHannes Eder setup_affinity(irq, desc); 6220c5d1eb7SDavid Brownell 6230c5d1eb7SDavid Brownell } else if ((new->flags & IRQF_TRIGGER_MASK) 6240c5d1eb7SDavid Brownell && (new->flags & IRQF_TRIGGER_MASK) 6250c5d1eb7SDavid Brownell != (desc->status & IRQ_TYPE_SENSE_MASK)) { 6260c5d1eb7SDavid Brownell /* hope the handler works with the actual trigger mode... */ 6270c5d1eb7SDavid Brownell pr_warning("IRQ %d uses trigger mode %d; requested %d\n", 6280c5d1eb7SDavid Brownell irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK), 6290c5d1eb7SDavid Brownell (int)(new->flags & IRQF_TRIGGER_MASK)); 63094d39e1fSThomas Gleixner } 63182736f4dSUwe Kleine-König 632f17c7545SIngo Molnar *old_ptr = new; 63382736f4dSUwe Kleine-König 6348528b0f1SLinus Torvalds /* Reset broken irq detection when installing new handler */ 6358528b0f1SLinus Torvalds desc->irq_count = 0; 6368528b0f1SLinus Torvalds desc->irqs_unhandled = 0; 6371adb0850SThomas Gleixner 6381adb0850SThomas Gleixner /* 6391adb0850SThomas Gleixner * Check whether we disabled the irq via the spurious handler 6401adb0850SThomas Gleixner * before. Reenable it and give it another chance. 6411adb0850SThomas Gleixner */ 6421adb0850SThomas Gleixner if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) { 6431adb0850SThomas Gleixner desc->status &= ~IRQ_SPURIOUS_DISABLED; 6441adb0850SThomas Gleixner __enable_irq(desc, irq); 6451adb0850SThomas Gleixner } 6461adb0850SThomas Gleixner 6471da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds new->irq = irq; 6502c6927a3SYinghai Lu register_irq_proc(irq, desc); 6511da177e4SLinus Torvalds new->dir = NULL; 6521da177e4SLinus Torvalds register_handler_proc(irq, new); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds return 0; 655f5163427SDimitri Sivanich 656f5163427SDimitri Sivanich mismatch: 6573f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ 6583cca53b0SThomas Gleixner if (!(new->flags & IRQF_PROBE_SHARED)) { 659e8c4b9d0SBjorn Helgaas printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq); 6608b126b77SAndrew Morton if (old_name) 6618b126b77SAndrew Morton printk(KERN_ERR "current handler: %s\n", old_name); 662f5163427SDimitri Sivanich dump_stack(); 66313e87ec6SAndrew Morton } 6643f050447SAlan Cox #endif 665*3aa551c9SThomas Gleixner ret = -EBUSY; 666*3aa551c9SThomas Gleixner 667*3aa551c9SThomas Gleixner out_thread: 6688b126b77SAndrew Morton spin_unlock_irqrestore(&desc->lock, flags); 669*3aa551c9SThomas Gleixner if (new->thread) { 670*3aa551c9SThomas Gleixner struct task_struct *t = new->thread; 671*3aa551c9SThomas Gleixner 672*3aa551c9SThomas Gleixner new->thread = NULL; 673*3aa551c9SThomas Gleixner if (likely(!test_bit(IRQTF_DIED, &new->thread_flags))) 674*3aa551c9SThomas Gleixner kthread_stop(t); 675*3aa551c9SThomas Gleixner put_task_struct(t); 676*3aa551c9SThomas Gleixner } 677*3aa551c9SThomas Gleixner return ret; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /** 681d3c60047SThomas Gleixner * setup_irq - setup an interrupt 682d3c60047SThomas Gleixner * @irq: Interrupt line to setup 683d3c60047SThomas Gleixner * @act: irqaction for the interrupt 684d3c60047SThomas Gleixner * 685d3c60047SThomas Gleixner * Used to statically setup interrupts in the early boot process. 686d3c60047SThomas Gleixner */ 687d3c60047SThomas Gleixner int setup_irq(unsigned int irq, struct irqaction *act) 688d3c60047SThomas Gleixner { 689d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 690d3c60047SThomas Gleixner 691d3c60047SThomas Gleixner return __setup_irq(irq, desc, act); 692d3c60047SThomas Gleixner } 693eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(setup_irq); 694d3c60047SThomas Gleixner 695cbf94f06SMagnus Damm /* 696cbf94f06SMagnus Damm * Internal function to unregister an irqaction - used to free 697cbf94f06SMagnus Damm * regular and special interrupts that are part of the architecture. 6981da177e4SLinus Torvalds */ 699cbf94f06SMagnus Damm static struct irqaction *__free_irq(unsigned int irq, void *dev_id) 7001da177e4SLinus Torvalds { 701d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 702f17c7545SIngo Molnar struct irqaction *action, **action_ptr; 703*3aa551c9SThomas Gleixner struct task_struct *irqthread; 7041da177e4SLinus Torvalds unsigned long flags; 7051da177e4SLinus Torvalds 706ae88a23bSIngo Molnar WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 7077d94f7caSYinghai Lu 7087d94f7caSYinghai Lu if (!desc) 709f21cfb25SMagnus Damm return NULL; 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 712ae88a23bSIngo Molnar 713ae88a23bSIngo Molnar /* 714ae88a23bSIngo Molnar * There can be multiple actions per IRQ descriptor, find the right 715ae88a23bSIngo Molnar * one based on the dev_id: 716ae88a23bSIngo Molnar */ 717f17c7545SIngo Molnar action_ptr = &desc->action; 7181da177e4SLinus Torvalds for (;;) { 719f17c7545SIngo Molnar action = *action_ptr; 7201da177e4SLinus Torvalds 721ae88a23bSIngo Molnar if (!action) { 722ae88a23bSIngo Molnar WARN(1, "Trying to free already-free IRQ %d\n", irq); 723ae88a23bSIngo Molnar spin_unlock_irqrestore(&desc->lock, flags); 724ae88a23bSIngo Molnar 725f21cfb25SMagnus Damm return NULL; 726ae88a23bSIngo Molnar } 7271da177e4SLinus Torvalds 7288316e381SIngo Molnar if (action->dev_id == dev_id) 729ae88a23bSIngo Molnar break; 730f17c7545SIngo Molnar action_ptr = &action->next; 731ae88a23bSIngo Molnar } 732ae88a23bSIngo Molnar 733ae88a23bSIngo Molnar /* Found it - now remove it from the list of entries: */ 734f17c7545SIngo Molnar *action_ptr = action->next; 735dbce706eSPaolo 'Blaisorblade' Giarrusso 736ae88a23bSIngo Molnar /* Currently used only by UML, might disappear one day: */ 737b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 738d1bef4edSIngo Molnar if (desc->chip->release) 739d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 740b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 741dbce706eSPaolo 'Blaisorblade' Giarrusso 742ae88a23bSIngo Molnar /* If this was the last handler, shut down the IRQ line: */ 7431da177e4SLinus Torvalds if (!desc->action) { 7441da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 745d1bef4edSIngo Molnar if (desc->chip->shutdown) 746d1bef4edSIngo Molnar desc->chip->shutdown(irq); 7471da177e4SLinus Torvalds else 748d1bef4edSIngo Molnar desc->chip->disable(irq); 7491da177e4SLinus Torvalds } 750*3aa551c9SThomas Gleixner 751*3aa551c9SThomas Gleixner irqthread = action->thread; 752*3aa551c9SThomas Gleixner action->thread = NULL; 753*3aa551c9SThomas Gleixner 7541da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 755ae88a23bSIngo Molnar 7561da177e4SLinus Torvalds unregister_handler_proc(irq, action); 7571da177e4SLinus Torvalds 758ae88a23bSIngo Molnar /* Make sure it's not being used on another CPU: */ 7591da177e4SLinus Torvalds synchronize_irq(irq); 760ae88a23bSIngo Molnar 761*3aa551c9SThomas Gleixner if (irqthread) { 762*3aa551c9SThomas Gleixner if (!test_bit(IRQTF_DIED, &action->thread_flags)) 763*3aa551c9SThomas Gleixner kthread_stop(irqthread); 764*3aa551c9SThomas Gleixner put_task_struct(irqthread); 765*3aa551c9SThomas Gleixner } 766*3aa551c9SThomas Gleixner 7671d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 7681d99493bSDavid Woodhouse /* 769ae88a23bSIngo Molnar * It's a shared IRQ -- the driver ought to be prepared for an IRQ 770ae88a23bSIngo Molnar * event to happen even now it's being freed, so let's make sure that 771ae88a23bSIngo Molnar * is so by doing an extra call to the handler .... 772ae88a23bSIngo Molnar * 773ae88a23bSIngo Molnar * ( We do this after actually deregistering it, to make sure that a 774ae88a23bSIngo Molnar * 'real' IRQ doesn't run in * parallel with our fake. ) 7751d99493bSDavid Woodhouse */ 7761d99493bSDavid Woodhouse if (action->flags & IRQF_SHARED) { 7771d99493bSDavid Woodhouse local_irq_save(flags); 7781d99493bSDavid Woodhouse action->handler(irq, dev_id); 7791d99493bSDavid Woodhouse local_irq_restore(flags); 7801d99493bSDavid Woodhouse } 7811d99493bSDavid Woodhouse #endif 782f21cfb25SMagnus Damm return action; 783f21cfb25SMagnus Damm } 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /** 786cbf94f06SMagnus Damm * remove_irq - free an interrupt 787cbf94f06SMagnus Damm * @irq: Interrupt line to free 788cbf94f06SMagnus Damm * @act: irqaction for the interrupt 789cbf94f06SMagnus Damm * 790cbf94f06SMagnus Damm * Used to remove interrupts statically setup by the early boot process. 791cbf94f06SMagnus Damm */ 792cbf94f06SMagnus Damm void remove_irq(unsigned int irq, struct irqaction *act) 793cbf94f06SMagnus Damm { 794cbf94f06SMagnus Damm __free_irq(irq, act->dev_id); 795cbf94f06SMagnus Damm } 796eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(remove_irq); 797cbf94f06SMagnus Damm 798cbf94f06SMagnus Damm /** 799f21cfb25SMagnus Damm * free_irq - free an interrupt allocated with request_irq 8001da177e4SLinus Torvalds * @irq: Interrupt line to free 8011da177e4SLinus Torvalds * @dev_id: Device identity to free 8021da177e4SLinus Torvalds * 8031da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 8041da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 8051da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 8061da177e4SLinus Torvalds * on the card it drives before calling this function. The function 8071da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 8081da177e4SLinus Torvalds * have completed. 8091da177e4SLinus Torvalds * 8101da177e4SLinus Torvalds * This function must not be called from interrupt context. 8111da177e4SLinus Torvalds */ 8121da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 8131da177e4SLinus Torvalds { 814cbf94f06SMagnus Damm kfree(__free_irq(irq, dev_id)); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /** 819*3aa551c9SThomas Gleixner * request_threaded_irq - allocate an interrupt line 8201da177e4SLinus Torvalds * @irq: Interrupt line to allocate 821*3aa551c9SThomas Gleixner * @handler: Function to be called when the IRQ occurs. 822*3aa551c9SThomas Gleixner * Primary handler for threaded interrupts 823*3aa551c9SThomas Gleixner * @thread_fn: Function called from the irq handler thread 824*3aa551c9SThomas Gleixner * If NULL, no irq thread is created 8251da177e4SLinus Torvalds * @irqflags: Interrupt type flags 8261da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 8271da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 8281da177e4SLinus Torvalds * 8291da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 8301da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 8311da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 8321da177e4SLinus Torvalds * your handler function must clear any interrupt the board 8331da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 8341da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 8351da177e4SLinus Torvalds * 836*3aa551c9SThomas Gleixner * If you want to set up a threaded irq handler for your device 837*3aa551c9SThomas Gleixner * then you need to supply @handler and @thread_fn. @handler ist 838*3aa551c9SThomas Gleixner * still called in hard interrupt context and has to check 839*3aa551c9SThomas Gleixner * whether the interrupt originates from the device. If yes it 840*3aa551c9SThomas Gleixner * needs to disable the interrupt on the device and return 841*3aa551c9SThomas Gleixner * IRQ_THREAD_WAKE which will wake up the handler thread and run 842*3aa551c9SThomas Gleixner * @thread_fn. This split handler design is necessary to support 843*3aa551c9SThomas Gleixner * shared interrupts. 844*3aa551c9SThomas Gleixner * 8451da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 8461da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 8471da177e4SLinus Torvalds * receives this value it makes sense to use it. 8481da177e4SLinus Torvalds * 8491da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 8501da177e4SLinus Torvalds * as this is required when freeing the interrupt. 8511da177e4SLinus Torvalds * 8521da177e4SLinus Torvalds * Flags: 8531da177e4SLinus Torvalds * 8543cca53b0SThomas Gleixner * IRQF_SHARED Interrupt is shared 8553cca53b0SThomas Gleixner * IRQF_DISABLED Disable local interrupts while processing 8563cca53b0SThomas Gleixner * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy 8570c5d1eb7SDavid Brownell * IRQF_TRIGGER_* Specify active edge(s) or level 8581da177e4SLinus Torvalds * 8591da177e4SLinus Torvalds */ 860*3aa551c9SThomas Gleixner int request_threaded_irq(unsigned int irq, irq_handler_t handler, 861*3aa551c9SThomas Gleixner irq_handler_t thread_fn, unsigned long irqflags, 862*3aa551c9SThomas Gleixner const char *devname, void *dev_id) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds struct irqaction *action; 86508678b08SYinghai Lu struct irq_desc *desc; 866d3c60047SThomas Gleixner int retval; 8671da177e4SLinus Torvalds 868470c6623SDavid Brownell /* 869470c6623SDavid Brownell * handle_IRQ_event() always ignores IRQF_DISABLED except for 870470c6623SDavid Brownell * the _first_ irqaction (sigh). That can cause oopsing, but 871470c6623SDavid Brownell * the behavior is classified as "will not fix" so we need to 872470c6623SDavid Brownell * start nudging drivers away from using that idiom. 873470c6623SDavid Brownell */ 874327ec569SIngo Molnar if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) == 875327ec569SIngo Molnar (IRQF_SHARED|IRQF_DISABLED)) { 876327ec569SIngo Molnar pr_warning( 877327ec569SIngo Molnar "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n", 878470c6623SDavid Brownell irq, devname); 879327ec569SIngo Molnar } 880470c6623SDavid Brownell 881fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 882fbb9ce95SIngo Molnar /* 883fbb9ce95SIngo Molnar * Lockdep wants atomic interrupt handlers: 884fbb9ce95SIngo Molnar */ 88538515e90SThomas Gleixner irqflags |= IRQF_DISABLED; 886fbb9ce95SIngo Molnar #endif 8871da177e4SLinus Torvalds /* 8881da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 8891da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 8901da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 8911da177e4SLinus Torvalds * logic etc). 8921da177e4SLinus Torvalds */ 8933cca53b0SThomas Gleixner if ((irqflags & IRQF_SHARED) && !dev_id) 8941da177e4SLinus Torvalds return -EINVAL; 8957d94f7caSYinghai Lu 896cb5bc832SYinghai Lu desc = irq_to_desc(irq); 8977d94f7caSYinghai Lu if (!desc) 8981da177e4SLinus Torvalds return -EINVAL; 8997d94f7caSYinghai Lu 90008678b08SYinghai Lu if (desc->status & IRQ_NOREQUEST) 9016550c775SThomas Gleixner return -EINVAL; 9021da177e4SLinus Torvalds if (!handler) 9031da177e4SLinus Torvalds return -EINVAL; 9041da177e4SLinus Torvalds 90545535732SThomas Gleixner action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 9061da177e4SLinus Torvalds if (!action) 9071da177e4SLinus Torvalds return -ENOMEM; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds action->handler = handler; 910*3aa551c9SThomas Gleixner action->thread_fn = thread_fn; 9111da177e4SLinus Torvalds action->flags = irqflags; 9121da177e4SLinus Torvalds action->name = devname; 9131da177e4SLinus Torvalds action->dev_id = dev_id; 9141da177e4SLinus Torvalds 915d3c60047SThomas Gleixner retval = __setup_irq(irq, desc, action); 916377bf1e4SAnton Vorontsov if (retval) 917377bf1e4SAnton Vorontsov kfree(action); 918377bf1e4SAnton Vorontsov 919a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 920a304e1b8SDavid Woodhouse if (irqflags & IRQF_SHARED) { 921a304e1b8SDavid Woodhouse /* 922a304e1b8SDavid Woodhouse * It's a shared IRQ -- the driver ought to be prepared for it 923a304e1b8SDavid Woodhouse * to happen immediately, so let's make sure.... 924377bf1e4SAnton Vorontsov * We disable the irq to make sure that a 'real' IRQ doesn't 925377bf1e4SAnton Vorontsov * run in parallel with our fake. 926a304e1b8SDavid Woodhouse */ 927a304e1b8SDavid Woodhouse unsigned long flags; 928a304e1b8SDavid Woodhouse 929377bf1e4SAnton Vorontsov disable_irq(irq); 930a304e1b8SDavid Woodhouse local_irq_save(flags); 931377bf1e4SAnton Vorontsov 932a304e1b8SDavid Woodhouse handler(irq, dev_id); 933377bf1e4SAnton Vorontsov 934a304e1b8SDavid Woodhouse local_irq_restore(flags); 935377bf1e4SAnton Vorontsov enable_irq(irq); 936a304e1b8SDavid Woodhouse } 937a304e1b8SDavid Woodhouse #endif 9381da177e4SLinus Torvalds return retval; 9391da177e4SLinus Torvalds } 940*3aa551c9SThomas Gleixner EXPORT_SYMBOL(request_threaded_irq); 941