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 112*6ec3cfecSPallipadi, Venkatesh if (desc->status & IRQ_MOVE_PCNTXT) 11372b1e22dSSuresh Siddha desc->chip->set_affinity(irq, cpumask); 114*6ec3cfecSPallipadi, Venkatesh else { 115f6d87f4bSThomas Gleixner desc->status |= IRQ_MOVE_PENDING; 1167f7ace0cSMike Travis cpumask_copy(desc->pending_mask, cpumask); 117f6d87f4bSThomas Gleixner } 118771ee3b0SThomas Gleixner #else 1197f7ace0cSMike Travis cpumask_copy(desc->affinity, cpumask); 120771ee3b0SThomas Gleixner desc->chip->set_affinity(irq, cpumask); 121771ee3b0SThomas Gleixner #endif 1223aa551c9SThomas Gleixner irq_set_thread_affinity(desc, cpumask); 123f6d87f4bSThomas Gleixner desc->status |= IRQ_AFFINITY_SET; 124f6d87f4bSThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 125771ee3b0SThomas Gleixner return 0; 126771ee3b0SThomas Gleixner } 127771ee3b0SThomas Gleixner 12818404756SMax Krasnyansky #ifndef CONFIG_AUTO_IRQ_AFFINITY 12918404756SMax Krasnyansky /* 13018404756SMax Krasnyansky * Generic version of the affinity autoselector. 13118404756SMax Krasnyansky */ 132548c8933SHannes Eder static int setup_affinity(unsigned int irq, struct irq_desc *desc) 13318404756SMax Krasnyansky { 13418404756SMax Krasnyansky if (!irq_can_set_affinity(irq)) 13518404756SMax Krasnyansky return 0; 13618404756SMax Krasnyansky 137f6d87f4bSThomas Gleixner /* 138f6d87f4bSThomas Gleixner * Preserve an userspace affinity setup, but make sure that 139f6d87f4bSThomas Gleixner * one of the targets is online. 140f6d87f4bSThomas Gleixner */ 141612e3684SThomas Gleixner if (desc->status & (IRQ_AFFINITY_SET | IRQ_NO_BALANCING)) { 1427f7ace0cSMike Travis if (cpumask_any_and(desc->affinity, cpu_online_mask) 1430de26520SRusty Russell < nr_cpu_ids) 1440de26520SRusty Russell goto set_affinity; 145f6d87f4bSThomas Gleixner else 146f6d87f4bSThomas Gleixner desc->status &= ~IRQ_AFFINITY_SET; 147f6d87f4bSThomas Gleixner } 148f6d87f4bSThomas Gleixner 1497f7ace0cSMike Travis cpumask_and(desc->affinity, cpu_online_mask, irq_default_affinity); 1500de26520SRusty Russell set_affinity: 1517f7ace0cSMike Travis desc->chip->set_affinity(irq, desc->affinity); 15218404756SMax Krasnyansky 15318404756SMax Krasnyansky return 0; 15418404756SMax Krasnyansky } 155f6d87f4bSThomas Gleixner #else 156548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *d) 157f6d87f4bSThomas Gleixner { 158f6d87f4bSThomas Gleixner return irq_select_affinity(irq); 159f6d87f4bSThomas Gleixner } 16018404756SMax Krasnyansky #endif 16118404756SMax Krasnyansky 162f6d87f4bSThomas Gleixner /* 163f6d87f4bSThomas Gleixner * Called when affinity is set via /proc/irq 164f6d87f4bSThomas Gleixner */ 165f6d87f4bSThomas Gleixner int irq_select_affinity_usr(unsigned int irq) 166f6d87f4bSThomas Gleixner { 167f6d87f4bSThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 168f6d87f4bSThomas Gleixner unsigned long flags; 169f6d87f4bSThomas Gleixner int ret; 170f6d87f4bSThomas Gleixner 171f6d87f4bSThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 172548c8933SHannes Eder ret = setup_affinity(irq, desc); 1733aa551c9SThomas Gleixner if (!ret) 1743aa551c9SThomas Gleixner irq_set_thread_affinity(desc, desc->affinity); 175f6d87f4bSThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 176f6d87f4bSThomas Gleixner 177f6d87f4bSThomas Gleixner return ret; 178f6d87f4bSThomas Gleixner } 179f6d87f4bSThomas Gleixner 180f6d87f4bSThomas Gleixner #else 181548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *desc) 182f6d87f4bSThomas Gleixner { 183f6d87f4bSThomas Gleixner return 0; 184f6d87f4bSThomas Gleixner } 1851da177e4SLinus Torvalds #endif 1861da177e4SLinus Torvalds 1870a0c5168SRafael J. Wysocki void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend) 1880a0c5168SRafael J. Wysocki { 1890a0c5168SRafael J. Wysocki if (suspend) { 1900a0c5168SRafael J. Wysocki if (!desc->action || (desc->action->flags & IRQF_TIMER)) 1910a0c5168SRafael J. Wysocki return; 1920a0c5168SRafael J. Wysocki desc->status |= IRQ_SUSPENDED; 1930a0c5168SRafael J. Wysocki } 1940a0c5168SRafael J. Wysocki 1950a0c5168SRafael J. Wysocki if (!desc->depth++) { 1960a0c5168SRafael J. Wysocki desc->status |= IRQ_DISABLED; 1970a0c5168SRafael J. Wysocki desc->chip->disable(irq); 1980a0c5168SRafael J. Wysocki } 1990a0c5168SRafael J. Wysocki } 2000a0c5168SRafael J. Wysocki 2011da177e4SLinus Torvalds /** 2021da177e4SLinus Torvalds * disable_irq_nosync - disable an irq without waiting 2031da177e4SLinus Torvalds * @irq: Interrupt to disable 2041da177e4SLinus Torvalds * 2051da177e4SLinus Torvalds * Disable the selected interrupt line. Disables and Enables are 2061da177e4SLinus Torvalds * nested. 2071da177e4SLinus Torvalds * Unlike disable_irq(), this function does not ensure existing 2081da177e4SLinus Torvalds * instances of the IRQ handler have completed before returning. 2091da177e4SLinus Torvalds * 2101da177e4SLinus Torvalds * This function may be called from IRQ context. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds void disable_irq_nosync(unsigned int irq) 2131da177e4SLinus Torvalds { 214d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2151da177e4SLinus Torvalds unsigned long flags; 2161da177e4SLinus Torvalds 2177d94f7caSYinghai Lu if (!desc) 218c2b5a251SMatthew Wilcox return; 219c2b5a251SMatthew Wilcox 2201da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 2210a0c5168SRafael J. Wysocki __disable_irq(desc, irq, false); 2221da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync); 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /** 2271da177e4SLinus Torvalds * disable_irq - disable an irq and wait for completion 2281da177e4SLinus Torvalds * @irq: Interrupt to disable 2291da177e4SLinus Torvalds * 2301da177e4SLinus Torvalds * Disable the selected interrupt line. Enables and Disables are 2311da177e4SLinus Torvalds * nested. 2321da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 2331da177e4SLinus Torvalds * to complete before returning. If you use this function while 2341da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 2351da177e4SLinus Torvalds * 2361da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 2371da177e4SLinus Torvalds */ 2381da177e4SLinus Torvalds void disable_irq(unsigned int irq) 2391da177e4SLinus Torvalds { 240d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2411da177e4SLinus Torvalds 2427d94f7caSYinghai Lu if (!desc) 243c2b5a251SMatthew Wilcox return; 244c2b5a251SMatthew Wilcox 2451da177e4SLinus Torvalds disable_irq_nosync(irq); 2461da177e4SLinus Torvalds if (desc->action) 2471da177e4SLinus Torvalds synchronize_irq(irq); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq); 2501da177e4SLinus Torvalds 2510a0c5168SRafael J. Wysocki void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume) 2521adb0850SThomas Gleixner { 2530a0c5168SRafael J. Wysocki if (resume) 2540a0c5168SRafael J. Wysocki desc->status &= ~IRQ_SUSPENDED; 2550a0c5168SRafael J. Wysocki 2561adb0850SThomas Gleixner switch (desc->depth) { 2571adb0850SThomas Gleixner case 0: 2580a0c5168SRafael J. Wysocki err_out: 259b8c512f6SArjan van de Ven WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq); 2601adb0850SThomas Gleixner break; 2611adb0850SThomas Gleixner case 1: { 2621adb0850SThomas Gleixner unsigned int status = desc->status & ~IRQ_DISABLED; 2631adb0850SThomas Gleixner 2640a0c5168SRafael J. Wysocki if (desc->status & IRQ_SUSPENDED) 2650a0c5168SRafael J. Wysocki goto err_out; 2661adb0850SThomas Gleixner /* Prevent probing on this irq: */ 2671adb0850SThomas Gleixner desc->status = status | IRQ_NOPROBE; 2681adb0850SThomas Gleixner check_irq_resend(desc, irq); 2691adb0850SThomas Gleixner /* fall-through */ 2701adb0850SThomas Gleixner } 2711adb0850SThomas Gleixner default: 2721adb0850SThomas Gleixner desc->depth--; 2731adb0850SThomas Gleixner } 2741adb0850SThomas Gleixner } 2751adb0850SThomas Gleixner 2761da177e4SLinus Torvalds /** 2771da177e4SLinus Torvalds * enable_irq - enable handling of an irq 2781da177e4SLinus Torvalds * @irq: Interrupt to enable 2791da177e4SLinus Torvalds * 2801da177e4SLinus Torvalds * Undoes the effect of one call to disable_irq(). If this 2811da177e4SLinus Torvalds * matches the last disable, processing of interrupts on this 2821da177e4SLinus Torvalds * IRQ line is re-enabled. 2831da177e4SLinus Torvalds * 2841da177e4SLinus Torvalds * This function may be called from IRQ context. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds void enable_irq(unsigned int irq) 2871da177e4SLinus Torvalds { 288d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2891da177e4SLinus Torvalds unsigned long flags; 2901da177e4SLinus Torvalds 2917d94f7caSYinghai Lu if (!desc) 292c2b5a251SMatthew Wilcox return; 293c2b5a251SMatthew Wilcox 2941da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 2950a0c5168SRafael J. Wysocki __enable_irq(desc, irq, false); 2961da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq); 2991da177e4SLinus Torvalds 3000c5d1eb7SDavid Brownell static int set_irq_wake_real(unsigned int irq, unsigned int on) 3012db87321SUwe Kleine-König { 30208678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 3032db87321SUwe Kleine-König int ret = -ENXIO; 3042db87321SUwe Kleine-König 3052db87321SUwe Kleine-König if (desc->chip->set_wake) 3062db87321SUwe Kleine-König ret = desc->chip->set_wake(irq, on); 3072db87321SUwe Kleine-König 3082db87321SUwe Kleine-König return ret; 3092db87321SUwe Kleine-König } 3102db87321SUwe Kleine-König 311ba9a2331SThomas Gleixner /** 312ba9a2331SThomas Gleixner * set_irq_wake - control irq power management wakeup 313ba9a2331SThomas Gleixner * @irq: interrupt to control 314ba9a2331SThomas Gleixner * @on: enable/disable power management wakeup 315ba9a2331SThomas Gleixner * 31615a647ebSDavid Brownell * Enable/disable power management wakeup mode, which is 31715a647ebSDavid Brownell * disabled by default. Enables and disables must match, 31815a647ebSDavid Brownell * just as they match for non-wakeup mode support. 31915a647ebSDavid Brownell * 32015a647ebSDavid Brownell * Wakeup mode lets this IRQ wake the system from sleep 32115a647ebSDavid Brownell * states like "suspend to RAM". 322ba9a2331SThomas Gleixner */ 323ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on) 324ba9a2331SThomas Gleixner { 32508678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 326ba9a2331SThomas Gleixner unsigned long flags; 3272db87321SUwe Kleine-König int ret = 0; 328ba9a2331SThomas Gleixner 32915a647ebSDavid Brownell /* wakeup-capable irqs can be shared between drivers that 33015a647ebSDavid Brownell * don't need to have the same sleep mode behaviors. 33115a647ebSDavid Brownell */ 332ba9a2331SThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 33315a647ebSDavid Brownell if (on) { 3342db87321SUwe Kleine-König if (desc->wake_depth++ == 0) { 3352db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3362db87321SUwe Kleine-König if (ret) 3372db87321SUwe Kleine-König desc->wake_depth = 0; 33815a647ebSDavid Brownell else 3392db87321SUwe Kleine-König desc->status |= IRQ_WAKEUP; 3402db87321SUwe Kleine-König } 34115a647ebSDavid Brownell } else { 34215a647ebSDavid Brownell if (desc->wake_depth == 0) { 3437a2c4770SArjan van de Ven WARN(1, "Unbalanced IRQ %d wake disable\n", irq); 3442db87321SUwe Kleine-König } else if (--desc->wake_depth == 0) { 3452db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3462db87321SUwe Kleine-König if (ret) 3472db87321SUwe Kleine-König desc->wake_depth = 1; 34815a647ebSDavid Brownell else 3492db87321SUwe Kleine-König desc->status &= ~IRQ_WAKEUP; 35015a647ebSDavid Brownell } 3512db87321SUwe Kleine-König } 3522db87321SUwe Kleine-König 353ba9a2331SThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 354ba9a2331SThomas Gleixner return ret; 355ba9a2331SThomas Gleixner } 356ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake); 357ba9a2331SThomas Gleixner 3581da177e4SLinus Torvalds /* 3591da177e4SLinus Torvalds * Internal function that tells the architecture code whether a 3601da177e4SLinus Torvalds * particular irq has been exclusively allocated or is available 3611da177e4SLinus Torvalds * for driver use. 3621da177e4SLinus Torvalds */ 3631da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags) 3641da177e4SLinus Torvalds { 365d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 3661da177e4SLinus Torvalds struct irqaction *action; 3671da177e4SLinus Torvalds 3687d94f7caSYinghai Lu if (!desc) 3697d94f7caSYinghai Lu return 0; 3707d94f7caSYinghai Lu 3717d94f7caSYinghai Lu if (desc->status & IRQ_NOREQUEST) 3721da177e4SLinus Torvalds return 0; 3731da177e4SLinus Torvalds 37408678b08SYinghai Lu action = desc->action; 3751da177e4SLinus Torvalds if (action) 3763cca53b0SThomas Gleixner if (irqflags & action->flags & IRQF_SHARED) 3771da177e4SLinus Torvalds action = NULL; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds return !action; 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3826a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc) 3836a6de9efSThomas Gleixner { 3846a6de9efSThomas Gleixner /* 3856a6de9efSThomas Gleixner * If the architecture still has not overriden 3866a6de9efSThomas Gleixner * the flow handler then zap the default. This 3876a6de9efSThomas Gleixner * should catch incorrect flow-type setting. 3886a6de9efSThomas Gleixner */ 3896a6de9efSThomas Gleixner if (desc->handle_irq == &handle_bad_irq) 3906a6de9efSThomas Gleixner desc->handle_irq = NULL; 3916a6de9efSThomas Gleixner } 3926a6de9efSThomas Gleixner 3930c5d1eb7SDavid Brownell int __irq_set_trigger(struct irq_desc *desc, unsigned int irq, 39482736f4dSUwe Kleine-König unsigned long flags) 39582736f4dSUwe Kleine-König { 39682736f4dSUwe Kleine-König int ret; 3970c5d1eb7SDavid Brownell struct irq_chip *chip = desc->chip; 39882736f4dSUwe Kleine-König 39982736f4dSUwe Kleine-König if (!chip || !chip->set_type) { 40082736f4dSUwe Kleine-König /* 40182736f4dSUwe Kleine-König * IRQF_TRIGGER_* but the PIC does not support multiple 40282736f4dSUwe Kleine-König * flow-types? 40382736f4dSUwe Kleine-König */ 4043ff68a6aSMark Nelson pr_debug("No set_type function for IRQ %d (%s)\n", irq, 40582736f4dSUwe Kleine-König chip ? (chip->name ? : "unknown") : "unknown"); 40682736f4dSUwe Kleine-König return 0; 40782736f4dSUwe Kleine-König } 40882736f4dSUwe Kleine-König 409f2b662daSDavid Brownell /* caller masked out all except trigger mode flags */ 410f2b662daSDavid Brownell ret = chip->set_type(irq, flags); 41182736f4dSUwe Kleine-König 41282736f4dSUwe Kleine-König if (ret) 413c69ad71bSDavid Brownell pr_err("setting trigger mode %d for irq %u failed (%pF)\n", 414f2b662daSDavid Brownell (int)flags, irq, chip->set_type); 4150c5d1eb7SDavid Brownell else { 416f2b662daSDavid Brownell if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) 417f2b662daSDavid Brownell flags |= IRQ_LEVEL; 4180c5d1eb7SDavid Brownell /* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */ 419f2b662daSDavid Brownell desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK); 420f2b662daSDavid Brownell desc->status |= flags; 4210c5d1eb7SDavid Brownell } 42282736f4dSUwe Kleine-König 42382736f4dSUwe Kleine-König return ret; 42482736f4dSUwe Kleine-König } 42582736f4dSUwe Kleine-König 4263aa551c9SThomas Gleixner static int irq_wait_for_interrupt(struct irqaction *action) 4273aa551c9SThomas Gleixner { 4283aa551c9SThomas Gleixner while (!kthread_should_stop()) { 4293aa551c9SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE); 430f48fe81eSThomas Gleixner 431f48fe81eSThomas Gleixner if (test_and_clear_bit(IRQTF_RUNTHREAD, 432f48fe81eSThomas Gleixner &action->thread_flags)) { 4333aa551c9SThomas Gleixner __set_current_state(TASK_RUNNING); 4343aa551c9SThomas Gleixner return 0; 435f48fe81eSThomas Gleixner } 4363aa551c9SThomas Gleixner schedule(); 4373aa551c9SThomas Gleixner } 4383aa551c9SThomas Gleixner return -1; 4393aa551c9SThomas Gleixner } 4403aa551c9SThomas Gleixner 4413aa551c9SThomas Gleixner /* 4423aa551c9SThomas Gleixner * Interrupt handler thread 4433aa551c9SThomas Gleixner */ 4443aa551c9SThomas Gleixner static int irq_thread(void *data) 4453aa551c9SThomas Gleixner { 4463aa551c9SThomas Gleixner struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, }; 4473aa551c9SThomas Gleixner struct irqaction *action = data; 4483aa551c9SThomas Gleixner struct irq_desc *desc = irq_to_desc(action->irq); 4493aa551c9SThomas Gleixner int wake; 4503aa551c9SThomas Gleixner 4513aa551c9SThomas Gleixner sched_setscheduler(current, SCHED_FIFO, ¶m); 4523aa551c9SThomas Gleixner current->irqaction = action; 4533aa551c9SThomas Gleixner 4543aa551c9SThomas Gleixner while (!irq_wait_for_interrupt(action)) { 4553aa551c9SThomas Gleixner 4563aa551c9SThomas Gleixner atomic_inc(&desc->threads_active); 4573aa551c9SThomas Gleixner 4583aa551c9SThomas Gleixner spin_lock_irq(&desc->lock); 4593aa551c9SThomas Gleixner if (unlikely(desc->status & IRQ_DISABLED)) { 4603aa551c9SThomas Gleixner /* 4613aa551c9SThomas Gleixner * CHECKME: We might need a dedicated 4623aa551c9SThomas Gleixner * IRQ_THREAD_PENDING flag here, which 4633aa551c9SThomas Gleixner * retriggers the thread in check_irq_resend() 4643aa551c9SThomas Gleixner * but AFAICT IRQ_PENDING should be fine as it 4653aa551c9SThomas Gleixner * retriggers the interrupt itself --- tglx 4663aa551c9SThomas Gleixner */ 4673aa551c9SThomas Gleixner desc->status |= IRQ_PENDING; 4683aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 4693aa551c9SThomas Gleixner } else { 4703aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 4713aa551c9SThomas Gleixner 4723aa551c9SThomas Gleixner action->thread_fn(action->irq, action->dev_id); 4733aa551c9SThomas Gleixner } 4743aa551c9SThomas Gleixner 4753aa551c9SThomas Gleixner wake = atomic_dec_and_test(&desc->threads_active); 4763aa551c9SThomas Gleixner 4773aa551c9SThomas Gleixner if (wake && waitqueue_active(&desc->wait_for_threads)) 4783aa551c9SThomas Gleixner wake_up(&desc->wait_for_threads); 4793aa551c9SThomas Gleixner } 4803aa551c9SThomas Gleixner 4813aa551c9SThomas Gleixner /* 4823aa551c9SThomas Gleixner * Clear irqaction. Otherwise exit_irq_thread() would make 4833aa551c9SThomas Gleixner * fuzz about an active irq thread going into nirvana. 4843aa551c9SThomas Gleixner */ 4853aa551c9SThomas Gleixner current->irqaction = NULL; 4863aa551c9SThomas Gleixner return 0; 4873aa551c9SThomas Gleixner } 4883aa551c9SThomas Gleixner 4893aa551c9SThomas Gleixner /* 4903aa551c9SThomas Gleixner * Called from do_exit() 4913aa551c9SThomas Gleixner */ 4923aa551c9SThomas Gleixner void exit_irq_thread(void) 4933aa551c9SThomas Gleixner { 4943aa551c9SThomas Gleixner struct task_struct *tsk = current; 4953aa551c9SThomas Gleixner 4963aa551c9SThomas Gleixner if (!tsk->irqaction) 4973aa551c9SThomas Gleixner return; 4983aa551c9SThomas Gleixner 4993aa551c9SThomas Gleixner printk(KERN_ERR 5003aa551c9SThomas Gleixner "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n", 5013aa551c9SThomas Gleixner tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq); 5023aa551c9SThomas Gleixner 5033aa551c9SThomas Gleixner /* 5043aa551c9SThomas Gleixner * Set the THREAD DIED flag to prevent further wakeups of the 5053aa551c9SThomas Gleixner * soon to be gone threaded handler. 5063aa551c9SThomas Gleixner */ 5073aa551c9SThomas Gleixner set_bit(IRQTF_DIED, &tsk->irqaction->flags); 5083aa551c9SThomas Gleixner } 5093aa551c9SThomas Gleixner 5101da177e4SLinus Torvalds /* 5111da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 5121da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 5131da177e4SLinus Torvalds */ 514d3c60047SThomas Gleixner static int 515d3c60047SThomas Gleixner __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new) 5161da177e4SLinus Torvalds { 517f17c7545SIngo Molnar struct irqaction *old, **old_ptr; 5188b126b77SAndrew Morton const char *old_name = NULL; 5191da177e4SLinus Torvalds unsigned long flags; 5201da177e4SLinus Torvalds int shared = 0; 52182736f4dSUwe Kleine-König int ret; 5221da177e4SLinus Torvalds 5237d94f7caSYinghai Lu if (!desc) 524c2b5a251SMatthew Wilcox return -EINVAL; 525c2b5a251SMatthew Wilcox 526f1c2662cSIngo Molnar if (desc->chip == &no_irq_chip) 5271da177e4SLinus Torvalds return -ENOSYS; 5281da177e4SLinus Torvalds /* 5291da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 5301da177e4SLinus Torvalds * so we have to be careful not to interfere with a 5311da177e4SLinus Torvalds * running system. 5321da177e4SLinus Torvalds */ 5333cca53b0SThomas Gleixner if (new->flags & IRQF_SAMPLE_RANDOM) { 5341da177e4SLinus Torvalds /* 5351da177e4SLinus Torvalds * This function might sleep, we want to call it first, 5361da177e4SLinus Torvalds * outside of the atomic block. 5371da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 5381da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 5391da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 5401da177e4SLinus Torvalds * only the sysadmin is able to do this. 5411da177e4SLinus Torvalds */ 5421da177e4SLinus Torvalds rand_initialize_irq(irq); 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* 5463aa551c9SThomas Gleixner * Threaded handler ? 5473aa551c9SThomas Gleixner */ 5483aa551c9SThomas Gleixner if (new->thread_fn) { 5493aa551c9SThomas Gleixner struct task_struct *t; 5503aa551c9SThomas Gleixner 5513aa551c9SThomas Gleixner t = kthread_create(irq_thread, new, "irq/%d-%s", irq, 5523aa551c9SThomas Gleixner new->name); 5533aa551c9SThomas Gleixner if (IS_ERR(t)) 5543aa551c9SThomas Gleixner return PTR_ERR(t); 5553aa551c9SThomas Gleixner /* 5563aa551c9SThomas Gleixner * We keep the reference to the task struct even if 5573aa551c9SThomas Gleixner * the thread dies to avoid that the interrupt code 5583aa551c9SThomas Gleixner * references an already freed task_struct. 5593aa551c9SThomas Gleixner */ 5603aa551c9SThomas Gleixner get_task_struct(t); 5613aa551c9SThomas Gleixner new->thread = t; 5623aa551c9SThomas Gleixner wake_up_process(t); 5633aa551c9SThomas Gleixner } 5643aa551c9SThomas Gleixner 5653aa551c9SThomas Gleixner /* 5661da177e4SLinus Torvalds * The following block of code has to be executed atomically 5671da177e4SLinus Torvalds */ 5681da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 569f17c7545SIngo Molnar old_ptr = &desc->action; 570f17c7545SIngo Molnar old = *old_ptr; 57106fcb0c6SIngo Molnar if (old) { 572e76de9f8SThomas Gleixner /* 573e76de9f8SThomas Gleixner * Can't share interrupts unless both agree to and are 574e76de9f8SThomas Gleixner * the same type (level, edge, polarity). So both flag 5753cca53b0SThomas Gleixner * fields must have IRQF_SHARED set and the bits which 576e76de9f8SThomas Gleixner * set the trigger type must match. 577e76de9f8SThomas Gleixner */ 5783cca53b0SThomas Gleixner if (!((old->flags & new->flags) & IRQF_SHARED) || 5798b126b77SAndrew Morton ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) { 5808b126b77SAndrew Morton old_name = old->name; 581f5163427SDimitri Sivanich goto mismatch; 5828b126b77SAndrew Morton } 583f5163427SDimitri Sivanich 584284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU) 585f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 5863cca53b0SThomas Gleixner if ((old->flags & IRQF_PERCPU) != 5873cca53b0SThomas Gleixner (new->flags & IRQF_PERCPU)) 588f5163427SDimitri Sivanich goto mismatch; 589f5163427SDimitri Sivanich #endif 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 5921da177e4SLinus Torvalds do { 593f17c7545SIngo Molnar old_ptr = &old->next; 594f17c7545SIngo Molnar old = *old_ptr; 5951da177e4SLinus Torvalds } while (old); 5961da177e4SLinus Torvalds shared = 1; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds if (!shared) { 6006a6de9efSThomas Gleixner irq_chip_set_defaults(desc->chip); 601e76de9f8SThomas Gleixner 6023aa551c9SThomas Gleixner init_waitqueue_head(&desc->wait_for_threads); 6033aa551c9SThomas Gleixner 60482736f4dSUwe Kleine-König /* Setup the type (level, edge polarity) if configured: */ 60582736f4dSUwe Kleine-König if (new->flags & IRQF_TRIGGER_MASK) { 606f2b662daSDavid Brownell ret = __irq_set_trigger(desc, irq, 607f2b662daSDavid Brownell new->flags & IRQF_TRIGGER_MASK); 60882736f4dSUwe Kleine-König 6093aa551c9SThomas Gleixner if (ret) 6103aa551c9SThomas Gleixner goto out_thread; 61182736f4dSUwe Kleine-König } else 61282736f4dSUwe Kleine-König compat_irq_chip_set_default_handler(desc); 613f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU) 614f75d222bSAhmed S. Darwish if (new->flags & IRQF_PERCPU) 615f75d222bSAhmed S. Darwish desc->status |= IRQ_PER_CPU; 616f75d222bSAhmed S. Darwish #endif 617f75d222bSAhmed S. Darwish 61894d39e1fSThomas Gleixner desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | 6191adb0850SThomas Gleixner IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED); 62094d39e1fSThomas Gleixner 62194d39e1fSThomas Gleixner if (!(desc->status & IRQ_NOAUTOEN)) { 6221da177e4SLinus Torvalds desc->depth = 0; 62394d39e1fSThomas Gleixner desc->status &= ~IRQ_DISABLED; 624d1bef4edSIngo Molnar desc->chip->startup(irq); 625e76de9f8SThomas Gleixner } else 626e76de9f8SThomas Gleixner /* Undo nested disables: */ 627e76de9f8SThomas Gleixner desc->depth = 1; 62818404756SMax Krasnyansky 629612e3684SThomas Gleixner /* Exclude IRQ from balancing if requested */ 630612e3684SThomas Gleixner if (new->flags & IRQF_NOBALANCING) 631612e3684SThomas Gleixner desc->status |= IRQ_NO_BALANCING; 632612e3684SThomas Gleixner 63318404756SMax Krasnyansky /* Set default affinity mask once everything is setup */ 634548c8933SHannes Eder setup_affinity(irq, desc); 6350c5d1eb7SDavid Brownell 6360c5d1eb7SDavid Brownell } else if ((new->flags & IRQF_TRIGGER_MASK) 6370c5d1eb7SDavid Brownell && (new->flags & IRQF_TRIGGER_MASK) 6380c5d1eb7SDavid Brownell != (desc->status & IRQ_TYPE_SENSE_MASK)) { 6390c5d1eb7SDavid Brownell /* hope the handler works with the actual trigger mode... */ 6400c5d1eb7SDavid Brownell pr_warning("IRQ %d uses trigger mode %d; requested %d\n", 6410c5d1eb7SDavid Brownell irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK), 6420c5d1eb7SDavid Brownell (int)(new->flags & IRQF_TRIGGER_MASK)); 64394d39e1fSThomas Gleixner } 64482736f4dSUwe Kleine-König 645f17c7545SIngo Molnar *old_ptr = new; 64682736f4dSUwe Kleine-König 6478528b0f1SLinus Torvalds /* Reset broken irq detection when installing new handler */ 6488528b0f1SLinus Torvalds desc->irq_count = 0; 6498528b0f1SLinus Torvalds desc->irqs_unhandled = 0; 6501adb0850SThomas Gleixner 6511adb0850SThomas Gleixner /* 6521adb0850SThomas Gleixner * Check whether we disabled the irq via the spurious handler 6531adb0850SThomas Gleixner * before. Reenable it and give it another chance. 6541adb0850SThomas Gleixner */ 6551adb0850SThomas Gleixner if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) { 6561adb0850SThomas Gleixner desc->status &= ~IRQ_SPURIOUS_DISABLED; 6570a0c5168SRafael J. Wysocki __enable_irq(desc, irq, false); 6581adb0850SThomas Gleixner } 6591adb0850SThomas Gleixner 6601da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds new->irq = irq; 6632c6927a3SYinghai Lu register_irq_proc(irq, desc); 6641da177e4SLinus Torvalds new->dir = NULL; 6651da177e4SLinus Torvalds register_handler_proc(irq, new); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds return 0; 668f5163427SDimitri Sivanich 669f5163427SDimitri Sivanich mismatch: 6703f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ 6713cca53b0SThomas Gleixner if (!(new->flags & IRQF_PROBE_SHARED)) { 672e8c4b9d0SBjorn Helgaas printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq); 6738b126b77SAndrew Morton if (old_name) 6748b126b77SAndrew Morton printk(KERN_ERR "current handler: %s\n", old_name); 675f5163427SDimitri Sivanich dump_stack(); 67613e87ec6SAndrew Morton } 6773f050447SAlan Cox #endif 6783aa551c9SThomas Gleixner ret = -EBUSY; 6793aa551c9SThomas Gleixner 6803aa551c9SThomas Gleixner out_thread: 6818b126b77SAndrew Morton spin_unlock_irqrestore(&desc->lock, flags); 6823aa551c9SThomas Gleixner if (new->thread) { 6833aa551c9SThomas Gleixner struct task_struct *t = new->thread; 6843aa551c9SThomas Gleixner 6853aa551c9SThomas Gleixner new->thread = NULL; 6863aa551c9SThomas Gleixner if (likely(!test_bit(IRQTF_DIED, &new->thread_flags))) 6873aa551c9SThomas Gleixner kthread_stop(t); 6883aa551c9SThomas Gleixner put_task_struct(t); 6893aa551c9SThomas Gleixner } 6903aa551c9SThomas Gleixner return ret; 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds /** 694d3c60047SThomas Gleixner * setup_irq - setup an interrupt 695d3c60047SThomas Gleixner * @irq: Interrupt line to setup 696d3c60047SThomas Gleixner * @act: irqaction for the interrupt 697d3c60047SThomas Gleixner * 698d3c60047SThomas Gleixner * Used to statically setup interrupts in the early boot process. 699d3c60047SThomas Gleixner */ 700d3c60047SThomas Gleixner int setup_irq(unsigned int irq, struct irqaction *act) 701d3c60047SThomas Gleixner { 702d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 703d3c60047SThomas Gleixner 704d3c60047SThomas Gleixner return __setup_irq(irq, desc, act); 705d3c60047SThomas Gleixner } 706eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(setup_irq); 707d3c60047SThomas Gleixner 708cbf94f06SMagnus Damm /* 709cbf94f06SMagnus Damm * Internal function to unregister an irqaction - used to free 710cbf94f06SMagnus Damm * regular and special interrupts that are part of the architecture. 7111da177e4SLinus Torvalds */ 712cbf94f06SMagnus Damm static struct irqaction *__free_irq(unsigned int irq, void *dev_id) 7131da177e4SLinus Torvalds { 714d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 715f17c7545SIngo Molnar struct irqaction *action, **action_ptr; 7163aa551c9SThomas Gleixner struct task_struct *irqthread; 7171da177e4SLinus Torvalds unsigned long flags; 7181da177e4SLinus Torvalds 719ae88a23bSIngo Molnar WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 7207d94f7caSYinghai Lu 7217d94f7caSYinghai Lu if (!desc) 722f21cfb25SMagnus Damm return NULL; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 725ae88a23bSIngo Molnar 726ae88a23bSIngo Molnar /* 727ae88a23bSIngo Molnar * There can be multiple actions per IRQ descriptor, find the right 728ae88a23bSIngo Molnar * one based on the dev_id: 729ae88a23bSIngo Molnar */ 730f17c7545SIngo Molnar action_ptr = &desc->action; 7311da177e4SLinus Torvalds for (;;) { 732f17c7545SIngo Molnar action = *action_ptr; 7331da177e4SLinus Torvalds 734ae88a23bSIngo Molnar if (!action) { 735ae88a23bSIngo Molnar WARN(1, "Trying to free already-free IRQ %d\n", irq); 736ae88a23bSIngo Molnar spin_unlock_irqrestore(&desc->lock, flags); 737ae88a23bSIngo Molnar 738f21cfb25SMagnus Damm return NULL; 739ae88a23bSIngo Molnar } 7401da177e4SLinus Torvalds 7418316e381SIngo Molnar if (action->dev_id == dev_id) 742ae88a23bSIngo Molnar break; 743f17c7545SIngo Molnar action_ptr = &action->next; 744ae88a23bSIngo Molnar } 745ae88a23bSIngo Molnar 746ae88a23bSIngo Molnar /* Found it - now remove it from the list of entries: */ 747f17c7545SIngo Molnar *action_ptr = action->next; 748dbce706eSPaolo 'Blaisorblade' Giarrusso 749ae88a23bSIngo Molnar /* Currently used only by UML, might disappear one day: */ 750b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 751d1bef4edSIngo Molnar if (desc->chip->release) 752d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 753b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 754dbce706eSPaolo 'Blaisorblade' Giarrusso 755ae88a23bSIngo Molnar /* If this was the last handler, shut down the IRQ line: */ 7561da177e4SLinus Torvalds if (!desc->action) { 7571da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 758d1bef4edSIngo Molnar if (desc->chip->shutdown) 759d1bef4edSIngo Molnar desc->chip->shutdown(irq); 7601da177e4SLinus Torvalds else 761d1bef4edSIngo Molnar desc->chip->disable(irq); 7621da177e4SLinus Torvalds } 7633aa551c9SThomas Gleixner 7643aa551c9SThomas Gleixner irqthread = action->thread; 7653aa551c9SThomas Gleixner action->thread = NULL; 7663aa551c9SThomas Gleixner 7671da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 768ae88a23bSIngo Molnar 7691da177e4SLinus Torvalds unregister_handler_proc(irq, action); 7701da177e4SLinus Torvalds 771ae88a23bSIngo Molnar /* Make sure it's not being used on another CPU: */ 7721da177e4SLinus Torvalds synchronize_irq(irq); 773ae88a23bSIngo Molnar 7743aa551c9SThomas Gleixner if (irqthread) { 7753aa551c9SThomas Gleixner if (!test_bit(IRQTF_DIED, &action->thread_flags)) 7763aa551c9SThomas Gleixner kthread_stop(irqthread); 7773aa551c9SThomas Gleixner put_task_struct(irqthread); 7783aa551c9SThomas Gleixner } 7793aa551c9SThomas Gleixner 7801d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 7811d99493bSDavid Woodhouse /* 782ae88a23bSIngo Molnar * It's a shared IRQ -- the driver ought to be prepared for an IRQ 783ae88a23bSIngo Molnar * event to happen even now it's being freed, so let's make sure that 784ae88a23bSIngo Molnar * is so by doing an extra call to the handler .... 785ae88a23bSIngo Molnar * 786ae88a23bSIngo Molnar * ( We do this after actually deregistering it, to make sure that a 787ae88a23bSIngo Molnar * 'real' IRQ doesn't run in * parallel with our fake. ) 7881d99493bSDavid Woodhouse */ 7891d99493bSDavid Woodhouse if (action->flags & IRQF_SHARED) { 7901d99493bSDavid Woodhouse local_irq_save(flags); 7911d99493bSDavid Woodhouse action->handler(irq, dev_id); 7921d99493bSDavid Woodhouse local_irq_restore(flags); 7931d99493bSDavid Woodhouse } 7941d99493bSDavid Woodhouse #endif 795f21cfb25SMagnus Damm return action; 796f21cfb25SMagnus Damm } 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds /** 799cbf94f06SMagnus Damm * remove_irq - free an interrupt 800cbf94f06SMagnus Damm * @irq: Interrupt line to free 801cbf94f06SMagnus Damm * @act: irqaction for the interrupt 802cbf94f06SMagnus Damm * 803cbf94f06SMagnus Damm * Used to remove interrupts statically setup by the early boot process. 804cbf94f06SMagnus Damm */ 805cbf94f06SMagnus Damm void remove_irq(unsigned int irq, struct irqaction *act) 806cbf94f06SMagnus Damm { 807cbf94f06SMagnus Damm __free_irq(irq, act->dev_id); 808cbf94f06SMagnus Damm } 809eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(remove_irq); 810cbf94f06SMagnus Damm 811cbf94f06SMagnus Damm /** 812f21cfb25SMagnus Damm * free_irq - free an interrupt allocated with request_irq 8131da177e4SLinus Torvalds * @irq: Interrupt line to free 8141da177e4SLinus Torvalds * @dev_id: Device identity to free 8151da177e4SLinus Torvalds * 8161da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 8171da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 8181da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 8191da177e4SLinus Torvalds * on the card it drives before calling this function. The function 8201da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 8211da177e4SLinus Torvalds * have completed. 8221da177e4SLinus Torvalds * 8231da177e4SLinus Torvalds * This function must not be called from interrupt context. 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 8261da177e4SLinus Torvalds { 827cbf94f06SMagnus Damm kfree(__free_irq(irq, dev_id)); 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds /** 8323aa551c9SThomas Gleixner * request_threaded_irq - allocate an interrupt line 8331da177e4SLinus Torvalds * @irq: Interrupt line to allocate 8343aa551c9SThomas Gleixner * @handler: Function to be called when the IRQ occurs. 8353aa551c9SThomas Gleixner * Primary handler for threaded interrupts 8363aa551c9SThomas Gleixner * @thread_fn: Function called from the irq handler thread 8373aa551c9SThomas Gleixner * If NULL, no irq thread is created 8381da177e4SLinus Torvalds * @irqflags: Interrupt type flags 8391da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 8401da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 8411da177e4SLinus Torvalds * 8421da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 8431da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 8441da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 8451da177e4SLinus Torvalds * your handler function must clear any interrupt the board 8461da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 8471da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 8481da177e4SLinus Torvalds * 8493aa551c9SThomas Gleixner * If you want to set up a threaded irq handler for your device 8503aa551c9SThomas Gleixner * then you need to supply @handler and @thread_fn. @handler ist 8513aa551c9SThomas Gleixner * still called in hard interrupt context and has to check 8523aa551c9SThomas Gleixner * whether the interrupt originates from the device. If yes it 8533aa551c9SThomas Gleixner * needs to disable the interrupt on the device and return 8543aa551c9SThomas Gleixner * IRQ_THREAD_WAKE which will wake up the handler thread and run 8553aa551c9SThomas Gleixner * @thread_fn. This split handler design is necessary to support 8563aa551c9SThomas Gleixner * shared interrupts. 8573aa551c9SThomas Gleixner * 8581da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 8591da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 8601da177e4SLinus Torvalds * receives this value it makes sense to use it. 8611da177e4SLinus Torvalds * 8621da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 8631da177e4SLinus Torvalds * as this is required when freeing the interrupt. 8641da177e4SLinus Torvalds * 8651da177e4SLinus Torvalds * Flags: 8661da177e4SLinus Torvalds * 8673cca53b0SThomas Gleixner * IRQF_SHARED Interrupt is shared 8683cca53b0SThomas Gleixner * IRQF_DISABLED Disable local interrupts while processing 8693cca53b0SThomas Gleixner * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy 8700c5d1eb7SDavid Brownell * IRQF_TRIGGER_* Specify active edge(s) or level 8711da177e4SLinus Torvalds * 8721da177e4SLinus Torvalds */ 8733aa551c9SThomas Gleixner int request_threaded_irq(unsigned int irq, irq_handler_t handler, 8743aa551c9SThomas Gleixner irq_handler_t thread_fn, unsigned long irqflags, 8753aa551c9SThomas Gleixner const char *devname, void *dev_id) 8761da177e4SLinus Torvalds { 8771da177e4SLinus Torvalds struct irqaction *action; 87808678b08SYinghai Lu struct irq_desc *desc; 879d3c60047SThomas Gleixner int retval; 8801da177e4SLinus Torvalds 881470c6623SDavid Brownell /* 882470c6623SDavid Brownell * handle_IRQ_event() always ignores IRQF_DISABLED except for 883470c6623SDavid Brownell * the _first_ irqaction (sigh). That can cause oopsing, but 884470c6623SDavid Brownell * the behavior is classified as "will not fix" so we need to 885470c6623SDavid Brownell * start nudging drivers away from using that idiom. 886470c6623SDavid Brownell */ 887327ec569SIngo Molnar if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) == 888327ec569SIngo Molnar (IRQF_SHARED|IRQF_DISABLED)) { 889327ec569SIngo Molnar pr_warning( 890327ec569SIngo Molnar "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n", 891470c6623SDavid Brownell irq, devname); 892327ec569SIngo Molnar } 893470c6623SDavid Brownell 894fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 895fbb9ce95SIngo Molnar /* 896fbb9ce95SIngo Molnar * Lockdep wants atomic interrupt handlers: 897fbb9ce95SIngo Molnar */ 89838515e90SThomas Gleixner irqflags |= IRQF_DISABLED; 899fbb9ce95SIngo Molnar #endif 9001da177e4SLinus Torvalds /* 9011da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 9021da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 9031da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 9041da177e4SLinus Torvalds * logic etc). 9051da177e4SLinus Torvalds */ 9063cca53b0SThomas Gleixner if ((irqflags & IRQF_SHARED) && !dev_id) 9071da177e4SLinus Torvalds return -EINVAL; 9087d94f7caSYinghai Lu 909cb5bc832SYinghai Lu desc = irq_to_desc(irq); 9107d94f7caSYinghai Lu if (!desc) 9111da177e4SLinus Torvalds return -EINVAL; 9127d94f7caSYinghai Lu 91308678b08SYinghai Lu if (desc->status & IRQ_NOREQUEST) 9146550c775SThomas Gleixner return -EINVAL; 9151da177e4SLinus Torvalds if (!handler) 9161da177e4SLinus Torvalds return -EINVAL; 9171da177e4SLinus Torvalds 91845535732SThomas Gleixner action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 9191da177e4SLinus Torvalds if (!action) 9201da177e4SLinus Torvalds return -ENOMEM; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds action->handler = handler; 9233aa551c9SThomas Gleixner action->thread_fn = thread_fn; 9241da177e4SLinus Torvalds action->flags = irqflags; 9251da177e4SLinus Torvalds action->name = devname; 9261da177e4SLinus Torvalds action->dev_id = dev_id; 9271da177e4SLinus Torvalds 928d3c60047SThomas Gleixner retval = __setup_irq(irq, desc, action); 929377bf1e4SAnton Vorontsov if (retval) 930377bf1e4SAnton Vorontsov kfree(action); 931377bf1e4SAnton Vorontsov 932a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 933a304e1b8SDavid Woodhouse if (irqflags & IRQF_SHARED) { 934a304e1b8SDavid Woodhouse /* 935a304e1b8SDavid Woodhouse * It's a shared IRQ -- the driver ought to be prepared for it 936a304e1b8SDavid Woodhouse * to happen immediately, so let's make sure.... 937377bf1e4SAnton Vorontsov * We disable the irq to make sure that a 'real' IRQ doesn't 938377bf1e4SAnton Vorontsov * run in parallel with our fake. 939a304e1b8SDavid Woodhouse */ 940a304e1b8SDavid Woodhouse unsigned long flags; 941a304e1b8SDavid Woodhouse 942377bf1e4SAnton Vorontsov disable_irq(irq); 943a304e1b8SDavid Woodhouse local_irq_save(flags); 944377bf1e4SAnton Vorontsov 945a304e1b8SDavid Woodhouse handler(irq, dev_id); 946377bf1e4SAnton Vorontsov 947a304e1b8SDavid Woodhouse local_irq_restore(flags); 948377bf1e4SAnton Vorontsov enable_irq(irq); 949a304e1b8SDavid Woodhouse } 950a304e1b8SDavid Woodhouse #endif 9511da177e4SLinus Torvalds return retval; 9521da177e4SLinus Torvalds } 9533aa551c9SThomas Gleixner EXPORT_SYMBOL(request_threaded_irq); 954