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 83591d2fb0SThomas Gleixner /** 84591d2fb0SThomas Gleixner * irq_set_thread_affinity - Notify irq threads to adjust affinity 85591d2fb0SThomas Gleixner * @desc: irq descriptor which has affitnity changed 86591d2fb0SThomas Gleixner * 87591d2fb0SThomas Gleixner * We just set IRQTF_AFFINITY and delegate the affinity setting 88591d2fb0SThomas Gleixner * to the interrupt thread itself. We can not call 89591d2fb0SThomas Gleixner * set_cpus_allowed_ptr() here as we hold desc->lock and this 90591d2fb0SThomas Gleixner * code can be called from hard interrupt context. 91591d2fb0SThomas Gleixner */ 92591d2fb0SThomas Gleixner void irq_set_thread_affinity(struct irq_desc *desc) 933aa551c9SThomas Gleixner { 943aa551c9SThomas Gleixner struct irqaction *action = desc->action; 953aa551c9SThomas Gleixner 963aa551c9SThomas Gleixner while (action) { 973aa551c9SThomas Gleixner if (action->thread) 98591d2fb0SThomas Gleixner set_bit(IRQTF_AFFINITY, &action->thread_flags); 993aa551c9SThomas Gleixner action = action->next; 1003aa551c9SThomas Gleixner } 1013aa551c9SThomas Gleixner } 1023aa551c9SThomas Gleixner 103771ee3b0SThomas Gleixner /** 104771ee3b0SThomas Gleixner * irq_set_affinity - Set the irq affinity of a given irq 105771ee3b0SThomas Gleixner * @irq: Interrupt to set affinity 106771ee3b0SThomas Gleixner * @cpumask: cpumask 107771ee3b0SThomas Gleixner * 108771ee3b0SThomas Gleixner */ 1090de26520SRusty Russell int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask) 110771ee3b0SThomas Gleixner { 11108678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 112f6d87f4bSThomas Gleixner unsigned long flags; 113771ee3b0SThomas Gleixner 114771ee3b0SThomas Gleixner if (!desc->chip->set_affinity) 115771ee3b0SThomas Gleixner return -EINVAL; 116771ee3b0SThomas Gleixner 117f6d87f4bSThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 118f6d87f4bSThomas Gleixner 119771ee3b0SThomas Gleixner #ifdef CONFIG_GENERIC_PENDING_IRQ 12057b150ccSYinghai Lu if (desc->status & IRQ_MOVE_PCNTXT) { 12157b150ccSYinghai Lu if (!desc->chip->set_affinity(irq, cpumask)) { 12257b150ccSYinghai Lu cpumask_copy(desc->affinity, cpumask); 123591d2fb0SThomas Gleixner irq_set_thread_affinity(desc); 12457b150ccSYinghai Lu } 12557b150ccSYinghai Lu } 1266ec3cfecSPallipadi, Venkatesh else { 127f6d87f4bSThomas Gleixner desc->status |= IRQ_MOVE_PENDING; 1287f7ace0cSMike Travis cpumask_copy(desc->pending_mask, cpumask); 129f6d87f4bSThomas Gleixner } 130771ee3b0SThomas Gleixner #else 13157b150ccSYinghai Lu if (!desc->chip->set_affinity(irq, cpumask)) { 1327f7ace0cSMike Travis cpumask_copy(desc->affinity, cpumask); 133591d2fb0SThomas Gleixner irq_set_thread_affinity(desc); 13457b150ccSYinghai Lu } 13557b150ccSYinghai Lu #endif 136f6d87f4bSThomas Gleixner desc->status |= IRQ_AFFINITY_SET; 137f6d87f4bSThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 138771ee3b0SThomas Gleixner return 0; 139771ee3b0SThomas Gleixner } 140771ee3b0SThomas Gleixner 14118404756SMax Krasnyansky #ifndef CONFIG_AUTO_IRQ_AFFINITY 14218404756SMax Krasnyansky /* 14318404756SMax Krasnyansky * Generic version of the affinity autoselector. 14418404756SMax Krasnyansky */ 145548c8933SHannes Eder static int setup_affinity(unsigned int irq, struct irq_desc *desc) 14618404756SMax Krasnyansky { 14718404756SMax Krasnyansky if (!irq_can_set_affinity(irq)) 14818404756SMax Krasnyansky return 0; 14918404756SMax Krasnyansky 150f6d87f4bSThomas Gleixner /* 151f6d87f4bSThomas Gleixner * Preserve an userspace affinity setup, but make sure that 152f6d87f4bSThomas Gleixner * one of the targets is online. 153f6d87f4bSThomas Gleixner */ 154612e3684SThomas Gleixner if (desc->status & (IRQ_AFFINITY_SET | IRQ_NO_BALANCING)) { 1557f7ace0cSMike Travis if (cpumask_any_and(desc->affinity, cpu_online_mask) 1560de26520SRusty Russell < nr_cpu_ids) 1570de26520SRusty Russell goto set_affinity; 158f6d87f4bSThomas Gleixner else 159f6d87f4bSThomas Gleixner desc->status &= ~IRQ_AFFINITY_SET; 160f6d87f4bSThomas Gleixner } 161f6d87f4bSThomas Gleixner 1627f7ace0cSMike Travis cpumask_and(desc->affinity, cpu_online_mask, irq_default_affinity); 1630de26520SRusty Russell set_affinity: 1647f7ace0cSMike Travis desc->chip->set_affinity(irq, desc->affinity); 16518404756SMax Krasnyansky 16618404756SMax Krasnyansky return 0; 16718404756SMax Krasnyansky } 168f6d87f4bSThomas Gleixner #else 169548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *d) 170f6d87f4bSThomas Gleixner { 171f6d87f4bSThomas Gleixner return irq_select_affinity(irq); 172f6d87f4bSThomas Gleixner } 17318404756SMax Krasnyansky #endif 17418404756SMax Krasnyansky 175f6d87f4bSThomas Gleixner /* 176f6d87f4bSThomas Gleixner * Called when affinity is set via /proc/irq 177f6d87f4bSThomas Gleixner */ 178f6d87f4bSThomas Gleixner int irq_select_affinity_usr(unsigned int irq) 179f6d87f4bSThomas Gleixner { 180f6d87f4bSThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 181f6d87f4bSThomas Gleixner unsigned long flags; 182f6d87f4bSThomas Gleixner int ret; 183f6d87f4bSThomas Gleixner 184f6d87f4bSThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 185548c8933SHannes Eder ret = setup_affinity(irq, desc); 1863aa551c9SThomas Gleixner if (!ret) 187591d2fb0SThomas Gleixner irq_set_thread_affinity(desc); 188f6d87f4bSThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 189f6d87f4bSThomas Gleixner 190f6d87f4bSThomas Gleixner return ret; 191f6d87f4bSThomas Gleixner } 192f6d87f4bSThomas Gleixner 193f6d87f4bSThomas Gleixner #else 194548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *desc) 195f6d87f4bSThomas Gleixner { 196f6d87f4bSThomas Gleixner return 0; 197f6d87f4bSThomas Gleixner } 1981da177e4SLinus Torvalds #endif 1991da177e4SLinus Torvalds 2000a0c5168SRafael J. Wysocki void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend) 2010a0c5168SRafael J. Wysocki { 2020a0c5168SRafael J. Wysocki if (suspend) { 2030a0c5168SRafael J. Wysocki if (!desc->action || (desc->action->flags & IRQF_TIMER)) 2040a0c5168SRafael J. Wysocki return; 2050a0c5168SRafael J. Wysocki desc->status |= IRQ_SUSPENDED; 2060a0c5168SRafael J. Wysocki } 2070a0c5168SRafael J. Wysocki 2080a0c5168SRafael J. Wysocki if (!desc->depth++) { 2090a0c5168SRafael J. Wysocki desc->status |= IRQ_DISABLED; 2100a0c5168SRafael J. Wysocki desc->chip->disable(irq); 2110a0c5168SRafael J. Wysocki } 2120a0c5168SRafael J. Wysocki } 2130a0c5168SRafael J. Wysocki 2141da177e4SLinus Torvalds /** 2151da177e4SLinus Torvalds * disable_irq_nosync - disable an irq without waiting 2161da177e4SLinus Torvalds * @irq: Interrupt to disable 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * Disable the selected interrupt line. Disables and Enables are 2191da177e4SLinus Torvalds * nested. 2201da177e4SLinus Torvalds * Unlike disable_irq(), this function does not ensure existing 2211da177e4SLinus Torvalds * instances of the IRQ handler have completed before returning. 2221da177e4SLinus Torvalds * 2231da177e4SLinus Torvalds * This function may be called from IRQ context. 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds void disable_irq_nosync(unsigned int irq) 2261da177e4SLinus Torvalds { 227d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2281da177e4SLinus Torvalds unsigned long flags; 2291da177e4SLinus Torvalds 2307d94f7caSYinghai Lu if (!desc) 231c2b5a251SMatthew Wilcox return; 232c2b5a251SMatthew Wilcox 2331da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 2340a0c5168SRafael J. Wysocki __disable_irq(desc, irq, false); 2351da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync); 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /** 2401da177e4SLinus Torvalds * disable_irq - disable an irq and wait for completion 2411da177e4SLinus Torvalds * @irq: Interrupt to disable 2421da177e4SLinus Torvalds * 2431da177e4SLinus Torvalds * Disable the selected interrupt line. Enables and Disables are 2441da177e4SLinus Torvalds * nested. 2451da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 2461da177e4SLinus Torvalds * to complete before returning. If you use this function while 2471da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 2481da177e4SLinus Torvalds * 2491da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds void disable_irq(unsigned int irq) 2521da177e4SLinus Torvalds { 253d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2541da177e4SLinus Torvalds 2557d94f7caSYinghai Lu if (!desc) 256c2b5a251SMatthew Wilcox return; 257c2b5a251SMatthew Wilcox 2581da177e4SLinus Torvalds disable_irq_nosync(irq); 2591da177e4SLinus Torvalds if (desc->action) 2601da177e4SLinus Torvalds synchronize_irq(irq); 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq); 2631da177e4SLinus Torvalds 2640a0c5168SRafael J. Wysocki void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume) 2651adb0850SThomas Gleixner { 2660a0c5168SRafael J. Wysocki if (resume) 2670a0c5168SRafael J. Wysocki desc->status &= ~IRQ_SUSPENDED; 2680a0c5168SRafael J. Wysocki 2691adb0850SThomas Gleixner switch (desc->depth) { 2701adb0850SThomas Gleixner case 0: 2710a0c5168SRafael J. Wysocki err_out: 272b8c512f6SArjan van de Ven WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq); 2731adb0850SThomas Gleixner break; 2741adb0850SThomas Gleixner case 1: { 2751adb0850SThomas Gleixner unsigned int status = desc->status & ~IRQ_DISABLED; 2761adb0850SThomas Gleixner 2770a0c5168SRafael J. Wysocki if (desc->status & IRQ_SUSPENDED) 2780a0c5168SRafael J. Wysocki goto err_out; 2791adb0850SThomas Gleixner /* Prevent probing on this irq: */ 2801adb0850SThomas Gleixner desc->status = status | IRQ_NOPROBE; 2811adb0850SThomas Gleixner check_irq_resend(desc, irq); 2821adb0850SThomas Gleixner /* fall-through */ 2831adb0850SThomas Gleixner } 2841adb0850SThomas Gleixner default: 2851adb0850SThomas Gleixner desc->depth--; 2861adb0850SThomas Gleixner } 2871adb0850SThomas Gleixner } 2881adb0850SThomas Gleixner 2891da177e4SLinus Torvalds /** 2901da177e4SLinus Torvalds * enable_irq - enable handling of an irq 2911da177e4SLinus Torvalds * @irq: Interrupt to enable 2921da177e4SLinus Torvalds * 2931da177e4SLinus Torvalds * Undoes the effect of one call to disable_irq(). If this 2941da177e4SLinus Torvalds * matches the last disable, processing of interrupts on this 2951da177e4SLinus Torvalds * IRQ line is re-enabled. 2961da177e4SLinus Torvalds * 2971da177e4SLinus Torvalds * This function may be called from IRQ context. 2981da177e4SLinus Torvalds */ 2991da177e4SLinus Torvalds void enable_irq(unsigned int irq) 3001da177e4SLinus Torvalds { 301d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 3021da177e4SLinus Torvalds unsigned long flags; 3031da177e4SLinus Torvalds 3047d94f7caSYinghai Lu if (!desc) 305c2b5a251SMatthew Wilcox return; 306c2b5a251SMatthew Wilcox 3071da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 3080a0c5168SRafael J. Wysocki __enable_irq(desc, irq, false); 3091da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq); 3121da177e4SLinus Torvalds 3130c5d1eb7SDavid Brownell static int set_irq_wake_real(unsigned int irq, unsigned int on) 3142db87321SUwe Kleine-König { 31508678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 3162db87321SUwe Kleine-König int ret = -ENXIO; 3172db87321SUwe Kleine-König 3182db87321SUwe Kleine-König if (desc->chip->set_wake) 3192db87321SUwe Kleine-König ret = desc->chip->set_wake(irq, on); 3202db87321SUwe Kleine-König 3212db87321SUwe Kleine-König return ret; 3222db87321SUwe Kleine-König } 3232db87321SUwe Kleine-König 324ba9a2331SThomas Gleixner /** 325ba9a2331SThomas Gleixner * set_irq_wake - control irq power management wakeup 326ba9a2331SThomas Gleixner * @irq: interrupt to control 327ba9a2331SThomas Gleixner * @on: enable/disable power management wakeup 328ba9a2331SThomas Gleixner * 32915a647ebSDavid Brownell * Enable/disable power management wakeup mode, which is 33015a647ebSDavid Brownell * disabled by default. Enables and disables must match, 33115a647ebSDavid Brownell * just as they match for non-wakeup mode support. 33215a647ebSDavid Brownell * 33315a647ebSDavid Brownell * Wakeup mode lets this IRQ wake the system from sleep 33415a647ebSDavid Brownell * states like "suspend to RAM". 335ba9a2331SThomas Gleixner */ 336ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on) 337ba9a2331SThomas Gleixner { 33808678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 339ba9a2331SThomas Gleixner unsigned long flags; 3402db87321SUwe Kleine-König int ret = 0; 341ba9a2331SThomas Gleixner 34215a647ebSDavid Brownell /* wakeup-capable irqs can be shared between drivers that 34315a647ebSDavid Brownell * don't need to have the same sleep mode behaviors. 34415a647ebSDavid Brownell */ 345ba9a2331SThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 34615a647ebSDavid Brownell if (on) { 3472db87321SUwe Kleine-König if (desc->wake_depth++ == 0) { 3482db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3492db87321SUwe Kleine-König if (ret) 3502db87321SUwe Kleine-König desc->wake_depth = 0; 35115a647ebSDavid Brownell else 3522db87321SUwe Kleine-König desc->status |= IRQ_WAKEUP; 3532db87321SUwe Kleine-König } 35415a647ebSDavid Brownell } else { 35515a647ebSDavid Brownell if (desc->wake_depth == 0) { 3567a2c4770SArjan van de Ven WARN(1, "Unbalanced IRQ %d wake disable\n", irq); 3572db87321SUwe Kleine-König } else if (--desc->wake_depth == 0) { 3582db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3592db87321SUwe Kleine-König if (ret) 3602db87321SUwe Kleine-König desc->wake_depth = 1; 36115a647ebSDavid Brownell else 3622db87321SUwe Kleine-König desc->status &= ~IRQ_WAKEUP; 36315a647ebSDavid Brownell } 3642db87321SUwe Kleine-König } 3652db87321SUwe Kleine-König 366ba9a2331SThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 367ba9a2331SThomas Gleixner return ret; 368ba9a2331SThomas Gleixner } 369ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake); 370ba9a2331SThomas Gleixner 3711da177e4SLinus Torvalds /* 3721da177e4SLinus Torvalds * Internal function that tells the architecture code whether a 3731da177e4SLinus Torvalds * particular irq has been exclusively allocated or is available 3741da177e4SLinus Torvalds * for driver use. 3751da177e4SLinus Torvalds */ 3761da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags) 3771da177e4SLinus Torvalds { 378d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 3791da177e4SLinus Torvalds struct irqaction *action; 3801da177e4SLinus Torvalds 3817d94f7caSYinghai Lu if (!desc) 3827d94f7caSYinghai Lu return 0; 3837d94f7caSYinghai Lu 3847d94f7caSYinghai Lu if (desc->status & IRQ_NOREQUEST) 3851da177e4SLinus Torvalds return 0; 3861da177e4SLinus Torvalds 38708678b08SYinghai Lu action = desc->action; 3881da177e4SLinus Torvalds if (action) 3893cca53b0SThomas Gleixner if (irqflags & action->flags & IRQF_SHARED) 3901da177e4SLinus Torvalds action = NULL; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds return !action; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3956a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc) 3966a6de9efSThomas Gleixner { 3976a6de9efSThomas Gleixner /* 3986a6de9efSThomas Gleixner * If the architecture still has not overriden 3996a6de9efSThomas Gleixner * the flow handler then zap the default. This 4006a6de9efSThomas Gleixner * should catch incorrect flow-type setting. 4016a6de9efSThomas Gleixner */ 4026a6de9efSThomas Gleixner if (desc->handle_irq == &handle_bad_irq) 4036a6de9efSThomas Gleixner desc->handle_irq = NULL; 4046a6de9efSThomas Gleixner } 4056a6de9efSThomas Gleixner 4060c5d1eb7SDavid Brownell int __irq_set_trigger(struct irq_desc *desc, unsigned int irq, 40782736f4dSUwe Kleine-König unsigned long flags) 40882736f4dSUwe Kleine-König { 40982736f4dSUwe Kleine-König int ret; 4100c5d1eb7SDavid Brownell struct irq_chip *chip = desc->chip; 41182736f4dSUwe Kleine-König 41282736f4dSUwe Kleine-König if (!chip || !chip->set_type) { 41382736f4dSUwe Kleine-König /* 41482736f4dSUwe Kleine-König * IRQF_TRIGGER_* but the PIC does not support multiple 41582736f4dSUwe Kleine-König * flow-types? 41682736f4dSUwe Kleine-König */ 4173ff68a6aSMark Nelson pr_debug("No set_type function for IRQ %d (%s)\n", irq, 41882736f4dSUwe Kleine-König chip ? (chip->name ? : "unknown") : "unknown"); 41982736f4dSUwe Kleine-König return 0; 42082736f4dSUwe Kleine-König } 42182736f4dSUwe Kleine-König 422f2b662daSDavid Brownell /* caller masked out all except trigger mode flags */ 423f2b662daSDavid Brownell ret = chip->set_type(irq, flags); 42482736f4dSUwe Kleine-König 42582736f4dSUwe Kleine-König if (ret) 426c69ad71bSDavid Brownell pr_err("setting trigger mode %d for irq %u failed (%pF)\n", 427f2b662daSDavid Brownell (int)flags, irq, chip->set_type); 4280c5d1eb7SDavid Brownell else { 429f2b662daSDavid Brownell if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) 430f2b662daSDavid Brownell flags |= IRQ_LEVEL; 4310c5d1eb7SDavid Brownell /* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */ 432f2b662daSDavid Brownell desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK); 433f2b662daSDavid Brownell desc->status |= flags; 4340c5d1eb7SDavid Brownell } 43582736f4dSUwe Kleine-König 43682736f4dSUwe Kleine-König return ret; 43782736f4dSUwe Kleine-König } 43882736f4dSUwe Kleine-König 4393aa551c9SThomas Gleixner static int irq_wait_for_interrupt(struct irqaction *action) 4403aa551c9SThomas Gleixner { 4413aa551c9SThomas Gleixner while (!kthread_should_stop()) { 4423aa551c9SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE); 443f48fe81eSThomas Gleixner 444f48fe81eSThomas Gleixner if (test_and_clear_bit(IRQTF_RUNTHREAD, 445f48fe81eSThomas Gleixner &action->thread_flags)) { 4463aa551c9SThomas Gleixner __set_current_state(TASK_RUNNING); 4473aa551c9SThomas Gleixner return 0; 448f48fe81eSThomas Gleixner } 4493aa551c9SThomas Gleixner schedule(); 4503aa551c9SThomas Gleixner } 4513aa551c9SThomas Gleixner return -1; 4523aa551c9SThomas Gleixner } 4533aa551c9SThomas Gleixner 454*61f38261SBruno Premont #ifdef CONFIG_SMP 4553aa551c9SThomas Gleixner /* 456591d2fb0SThomas Gleixner * Check whether we need to change the affinity of the interrupt thread. 457591d2fb0SThomas Gleixner */ 458591d2fb0SThomas Gleixner static void 459591d2fb0SThomas Gleixner irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) 460591d2fb0SThomas Gleixner { 461591d2fb0SThomas Gleixner cpumask_var_t mask; 462591d2fb0SThomas Gleixner 463591d2fb0SThomas Gleixner if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags)) 464591d2fb0SThomas Gleixner return; 465591d2fb0SThomas Gleixner 466591d2fb0SThomas Gleixner /* 467591d2fb0SThomas Gleixner * In case we are out of memory we set IRQTF_AFFINITY again and 468591d2fb0SThomas Gleixner * try again next time 469591d2fb0SThomas Gleixner */ 470591d2fb0SThomas Gleixner if (!alloc_cpumask_var(&mask, GFP_KERNEL)) { 471591d2fb0SThomas Gleixner set_bit(IRQTF_AFFINITY, &action->thread_flags); 472591d2fb0SThomas Gleixner return; 473591d2fb0SThomas Gleixner } 474591d2fb0SThomas Gleixner 475591d2fb0SThomas Gleixner spin_lock_irq(&desc->lock); 476591d2fb0SThomas Gleixner cpumask_copy(mask, desc->affinity); 477591d2fb0SThomas Gleixner spin_unlock_irq(&desc->lock); 478591d2fb0SThomas Gleixner 479591d2fb0SThomas Gleixner set_cpus_allowed_ptr(current, mask); 480591d2fb0SThomas Gleixner free_cpumask_var(mask); 481591d2fb0SThomas Gleixner } 482*61f38261SBruno Premont #else 483*61f38261SBruno Premont static inline void 484*61f38261SBruno Premont irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { } 485*61f38261SBruno Premont #endif 486591d2fb0SThomas Gleixner 487591d2fb0SThomas Gleixner /* 4883aa551c9SThomas Gleixner * Interrupt handler thread 4893aa551c9SThomas Gleixner */ 4903aa551c9SThomas Gleixner static int irq_thread(void *data) 4913aa551c9SThomas Gleixner { 4923aa551c9SThomas Gleixner struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, }; 4933aa551c9SThomas Gleixner struct irqaction *action = data; 4943aa551c9SThomas Gleixner struct irq_desc *desc = irq_to_desc(action->irq); 4953aa551c9SThomas Gleixner int wake; 4963aa551c9SThomas Gleixner 4973aa551c9SThomas Gleixner sched_setscheduler(current, SCHED_FIFO, ¶m); 4983aa551c9SThomas Gleixner current->irqaction = action; 4993aa551c9SThomas Gleixner 5003aa551c9SThomas Gleixner while (!irq_wait_for_interrupt(action)) { 5013aa551c9SThomas Gleixner 502591d2fb0SThomas Gleixner irq_thread_check_affinity(desc, action); 503591d2fb0SThomas Gleixner 5043aa551c9SThomas Gleixner atomic_inc(&desc->threads_active); 5053aa551c9SThomas Gleixner 5063aa551c9SThomas Gleixner spin_lock_irq(&desc->lock); 5073aa551c9SThomas Gleixner if (unlikely(desc->status & IRQ_DISABLED)) { 5083aa551c9SThomas Gleixner /* 5093aa551c9SThomas Gleixner * CHECKME: We might need a dedicated 5103aa551c9SThomas Gleixner * IRQ_THREAD_PENDING flag here, which 5113aa551c9SThomas Gleixner * retriggers the thread in check_irq_resend() 5123aa551c9SThomas Gleixner * but AFAICT IRQ_PENDING should be fine as it 5133aa551c9SThomas Gleixner * retriggers the interrupt itself --- tglx 5143aa551c9SThomas Gleixner */ 5153aa551c9SThomas Gleixner desc->status |= IRQ_PENDING; 5163aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 5173aa551c9SThomas Gleixner } else { 5183aa551c9SThomas Gleixner spin_unlock_irq(&desc->lock); 5193aa551c9SThomas Gleixner 5203aa551c9SThomas Gleixner action->thread_fn(action->irq, action->dev_id); 5213aa551c9SThomas Gleixner } 5223aa551c9SThomas Gleixner 5233aa551c9SThomas Gleixner wake = atomic_dec_and_test(&desc->threads_active); 5243aa551c9SThomas Gleixner 5253aa551c9SThomas Gleixner if (wake && waitqueue_active(&desc->wait_for_threads)) 5263aa551c9SThomas Gleixner wake_up(&desc->wait_for_threads); 5273aa551c9SThomas Gleixner } 5283aa551c9SThomas Gleixner 5293aa551c9SThomas Gleixner /* 5303aa551c9SThomas Gleixner * Clear irqaction. Otherwise exit_irq_thread() would make 5313aa551c9SThomas Gleixner * fuzz about an active irq thread going into nirvana. 5323aa551c9SThomas Gleixner */ 5333aa551c9SThomas Gleixner current->irqaction = NULL; 5343aa551c9SThomas Gleixner return 0; 5353aa551c9SThomas Gleixner } 5363aa551c9SThomas Gleixner 5373aa551c9SThomas Gleixner /* 5383aa551c9SThomas Gleixner * Called from do_exit() 5393aa551c9SThomas Gleixner */ 5403aa551c9SThomas Gleixner void exit_irq_thread(void) 5413aa551c9SThomas Gleixner { 5423aa551c9SThomas Gleixner struct task_struct *tsk = current; 5433aa551c9SThomas Gleixner 5443aa551c9SThomas Gleixner if (!tsk->irqaction) 5453aa551c9SThomas Gleixner return; 5463aa551c9SThomas Gleixner 5473aa551c9SThomas Gleixner printk(KERN_ERR 5483aa551c9SThomas Gleixner "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n", 5493aa551c9SThomas Gleixner tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq); 5503aa551c9SThomas Gleixner 5513aa551c9SThomas Gleixner /* 5523aa551c9SThomas Gleixner * Set the THREAD DIED flag to prevent further wakeups of the 5533aa551c9SThomas Gleixner * soon to be gone threaded handler. 5543aa551c9SThomas Gleixner */ 5553aa551c9SThomas Gleixner set_bit(IRQTF_DIED, &tsk->irqaction->flags); 5563aa551c9SThomas Gleixner } 5573aa551c9SThomas Gleixner 5581da177e4SLinus Torvalds /* 5591da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 5601da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 5611da177e4SLinus Torvalds */ 562d3c60047SThomas Gleixner static int 563d3c60047SThomas Gleixner __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new) 5641da177e4SLinus Torvalds { 565f17c7545SIngo Molnar struct irqaction *old, **old_ptr; 5668b126b77SAndrew Morton const char *old_name = NULL; 5671da177e4SLinus Torvalds unsigned long flags; 5681da177e4SLinus Torvalds int shared = 0; 56982736f4dSUwe Kleine-König int ret; 5701da177e4SLinus Torvalds 5717d94f7caSYinghai Lu if (!desc) 572c2b5a251SMatthew Wilcox return -EINVAL; 573c2b5a251SMatthew Wilcox 574f1c2662cSIngo Molnar if (desc->chip == &no_irq_chip) 5751da177e4SLinus Torvalds return -ENOSYS; 5761da177e4SLinus Torvalds /* 5771da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 5781da177e4SLinus Torvalds * so we have to be careful not to interfere with a 5791da177e4SLinus Torvalds * running system. 5801da177e4SLinus Torvalds */ 5813cca53b0SThomas Gleixner if (new->flags & IRQF_SAMPLE_RANDOM) { 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * This function might sleep, we want to call it first, 5841da177e4SLinus Torvalds * outside of the atomic block. 5851da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 5861da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 5871da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 5881da177e4SLinus Torvalds * only the sysadmin is able to do this. 5891da177e4SLinus Torvalds */ 5901da177e4SLinus Torvalds rand_initialize_irq(irq); 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /* 5943aa551c9SThomas Gleixner * Threaded handler ? 5953aa551c9SThomas Gleixner */ 5963aa551c9SThomas Gleixner if (new->thread_fn) { 5973aa551c9SThomas Gleixner struct task_struct *t; 5983aa551c9SThomas Gleixner 5993aa551c9SThomas Gleixner t = kthread_create(irq_thread, new, "irq/%d-%s", irq, 6003aa551c9SThomas Gleixner new->name); 6013aa551c9SThomas Gleixner if (IS_ERR(t)) 6023aa551c9SThomas Gleixner return PTR_ERR(t); 6033aa551c9SThomas Gleixner /* 6043aa551c9SThomas Gleixner * We keep the reference to the task struct even if 6053aa551c9SThomas Gleixner * the thread dies to avoid that the interrupt code 6063aa551c9SThomas Gleixner * references an already freed task_struct. 6073aa551c9SThomas Gleixner */ 6083aa551c9SThomas Gleixner get_task_struct(t); 6093aa551c9SThomas Gleixner new->thread = t; 6103aa551c9SThomas Gleixner wake_up_process(t); 6113aa551c9SThomas Gleixner } 6123aa551c9SThomas Gleixner 6133aa551c9SThomas Gleixner /* 6141da177e4SLinus Torvalds * The following block of code has to be executed atomically 6151da177e4SLinus Torvalds */ 6161da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 617f17c7545SIngo Molnar old_ptr = &desc->action; 618f17c7545SIngo Molnar old = *old_ptr; 61906fcb0c6SIngo Molnar if (old) { 620e76de9f8SThomas Gleixner /* 621e76de9f8SThomas Gleixner * Can't share interrupts unless both agree to and are 622e76de9f8SThomas Gleixner * the same type (level, edge, polarity). So both flag 6233cca53b0SThomas Gleixner * fields must have IRQF_SHARED set and the bits which 624e76de9f8SThomas Gleixner * set the trigger type must match. 625e76de9f8SThomas Gleixner */ 6263cca53b0SThomas Gleixner if (!((old->flags & new->flags) & IRQF_SHARED) || 6278b126b77SAndrew Morton ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) { 6288b126b77SAndrew Morton old_name = old->name; 629f5163427SDimitri Sivanich goto mismatch; 6308b126b77SAndrew Morton } 631f5163427SDimitri Sivanich 632284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU) 633f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 6343cca53b0SThomas Gleixner if ((old->flags & IRQF_PERCPU) != 6353cca53b0SThomas Gleixner (new->flags & IRQF_PERCPU)) 636f5163427SDimitri Sivanich goto mismatch; 637f5163427SDimitri Sivanich #endif 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 6401da177e4SLinus Torvalds do { 641f17c7545SIngo Molnar old_ptr = &old->next; 642f17c7545SIngo Molnar old = *old_ptr; 6431da177e4SLinus Torvalds } while (old); 6441da177e4SLinus Torvalds shared = 1; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds if (!shared) { 6486a6de9efSThomas Gleixner irq_chip_set_defaults(desc->chip); 649e76de9f8SThomas Gleixner 6503aa551c9SThomas Gleixner init_waitqueue_head(&desc->wait_for_threads); 6513aa551c9SThomas Gleixner 65282736f4dSUwe Kleine-König /* Setup the type (level, edge polarity) if configured: */ 65382736f4dSUwe Kleine-König if (new->flags & IRQF_TRIGGER_MASK) { 654f2b662daSDavid Brownell ret = __irq_set_trigger(desc, irq, 655f2b662daSDavid Brownell new->flags & IRQF_TRIGGER_MASK); 65682736f4dSUwe Kleine-König 6573aa551c9SThomas Gleixner if (ret) 6583aa551c9SThomas Gleixner goto out_thread; 65982736f4dSUwe Kleine-König } else 66082736f4dSUwe Kleine-König compat_irq_chip_set_default_handler(desc); 661f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU) 662f75d222bSAhmed S. Darwish if (new->flags & IRQF_PERCPU) 663f75d222bSAhmed S. Darwish desc->status |= IRQ_PER_CPU; 664f75d222bSAhmed S. Darwish #endif 665f75d222bSAhmed S. Darwish 66694d39e1fSThomas Gleixner desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | 6671adb0850SThomas Gleixner IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED); 66894d39e1fSThomas Gleixner 66994d39e1fSThomas Gleixner if (!(desc->status & IRQ_NOAUTOEN)) { 6701da177e4SLinus Torvalds desc->depth = 0; 67194d39e1fSThomas Gleixner desc->status &= ~IRQ_DISABLED; 672d1bef4edSIngo Molnar desc->chip->startup(irq); 673e76de9f8SThomas Gleixner } else 674e76de9f8SThomas Gleixner /* Undo nested disables: */ 675e76de9f8SThomas Gleixner desc->depth = 1; 67618404756SMax Krasnyansky 677612e3684SThomas Gleixner /* Exclude IRQ from balancing if requested */ 678612e3684SThomas Gleixner if (new->flags & IRQF_NOBALANCING) 679612e3684SThomas Gleixner desc->status |= IRQ_NO_BALANCING; 680612e3684SThomas Gleixner 68118404756SMax Krasnyansky /* Set default affinity mask once everything is setup */ 682548c8933SHannes Eder setup_affinity(irq, desc); 6830c5d1eb7SDavid Brownell 6840c5d1eb7SDavid Brownell } else if ((new->flags & IRQF_TRIGGER_MASK) 6850c5d1eb7SDavid Brownell && (new->flags & IRQF_TRIGGER_MASK) 6860c5d1eb7SDavid Brownell != (desc->status & IRQ_TYPE_SENSE_MASK)) { 6870c5d1eb7SDavid Brownell /* hope the handler works with the actual trigger mode... */ 6880c5d1eb7SDavid Brownell pr_warning("IRQ %d uses trigger mode %d; requested %d\n", 6890c5d1eb7SDavid Brownell irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK), 6900c5d1eb7SDavid Brownell (int)(new->flags & IRQF_TRIGGER_MASK)); 69194d39e1fSThomas Gleixner } 69282736f4dSUwe Kleine-König 693f17c7545SIngo Molnar *old_ptr = new; 69482736f4dSUwe Kleine-König 6958528b0f1SLinus Torvalds /* Reset broken irq detection when installing new handler */ 6968528b0f1SLinus Torvalds desc->irq_count = 0; 6978528b0f1SLinus Torvalds desc->irqs_unhandled = 0; 6981adb0850SThomas Gleixner 6991adb0850SThomas Gleixner /* 7001adb0850SThomas Gleixner * Check whether we disabled the irq via the spurious handler 7011adb0850SThomas Gleixner * before. Reenable it and give it another chance. 7021adb0850SThomas Gleixner */ 7031adb0850SThomas Gleixner if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) { 7041adb0850SThomas Gleixner desc->status &= ~IRQ_SPURIOUS_DISABLED; 7050a0c5168SRafael J. Wysocki __enable_irq(desc, irq, false); 7061adb0850SThomas Gleixner } 7071adb0850SThomas Gleixner 7081da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds new->irq = irq; 7112c6927a3SYinghai Lu register_irq_proc(irq, desc); 7121da177e4SLinus Torvalds new->dir = NULL; 7131da177e4SLinus Torvalds register_handler_proc(irq, new); 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds return 0; 716f5163427SDimitri Sivanich 717f5163427SDimitri Sivanich mismatch: 7183f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ 7193cca53b0SThomas Gleixner if (!(new->flags & IRQF_PROBE_SHARED)) { 720e8c4b9d0SBjorn Helgaas printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq); 7218b126b77SAndrew Morton if (old_name) 7228b126b77SAndrew Morton printk(KERN_ERR "current handler: %s\n", old_name); 723f5163427SDimitri Sivanich dump_stack(); 72413e87ec6SAndrew Morton } 7253f050447SAlan Cox #endif 7263aa551c9SThomas Gleixner ret = -EBUSY; 7273aa551c9SThomas Gleixner 7283aa551c9SThomas Gleixner out_thread: 7298b126b77SAndrew Morton spin_unlock_irqrestore(&desc->lock, flags); 7303aa551c9SThomas Gleixner if (new->thread) { 7313aa551c9SThomas Gleixner struct task_struct *t = new->thread; 7323aa551c9SThomas Gleixner 7333aa551c9SThomas Gleixner new->thread = NULL; 7343aa551c9SThomas Gleixner if (likely(!test_bit(IRQTF_DIED, &new->thread_flags))) 7353aa551c9SThomas Gleixner kthread_stop(t); 7363aa551c9SThomas Gleixner put_task_struct(t); 7373aa551c9SThomas Gleixner } 7383aa551c9SThomas Gleixner return ret; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds /** 742d3c60047SThomas Gleixner * setup_irq - setup an interrupt 743d3c60047SThomas Gleixner * @irq: Interrupt line to setup 744d3c60047SThomas Gleixner * @act: irqaction for the interrupt 745d3c60047SThomas Gleixner * 746d3c60047SThomas Gleixner * Used to statically setup interrupts in the early boot process. 747d3c60047SThomas Gleixner */ 748d3c60047SThomas Gleixner int setup_irq(unsigned int irq, struct irqaction *act) 749d3c60047SThomas Gleixner { 750d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 751d3c60047SThomas Gleixner 752d3c60047SThomas Gleixner return __setup_irq(irq, desc, act); 753d3c60047SThomas Gleixner } 754eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(setup_irq); 755d3c60047SThomas Gleixner 756cbf94f06SMagnus Damm /* 757cbf94f06SMagnus Damm * Internal function to unregister an irqaction - used to free 758cbf94f06SMagnus Damm * regular and special interrupts that are part of the architecture. 7591da177e4SLinus Torvalds */ 760cbf94f06SMagnus Damm static struct irqaction *__free_irq(unsigned int irq, void *dev_id) 7611da177e4SLinus Torvalds { 762d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 763f17c7545SIngo Molnar struct irqaction *action, **action_ptr; 7643aa551c9SThomas Gleixner struct task_struct *irqthread; 7651da177e4SLinus Torvalds unsigned long flags; 7661da177e4SLinus Torvalds 767ae88a23bSIngo Molnar WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 7687d94f7caSYinghai Lu 7697d94f7caSYinghai Lu if (!desc) 770f21cfb25SMagnus Damm return NULL; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 773ae88a23bSIngo Molnar 774ae88a23bSIngo Molnar /* 775ae88a23bSIngo Molnar * There can be multiple actions per IRQ descriptor, find the right 776ae88a23bSIngo Molnar * one based on the dev_id: 777ae88a23bSIngo Molnar */ 778f17c7545SIngo Molnar action_ptr = &desc->action; 7791da177e4SLinus Torvalds for (;;) { 780f17c7545SIngo Molnar action = *action_ptr; 7811da177e4SLinus Torvalds 782ae88a23bSIngo Molnar if (!action) { 783ae88a23bSIngo Molnar WARN(1, "Trying to free already-free IRQ %d\n", irq); 784ae88a23bSIngo Molnar spin_unlock_irqrestore(&desc->lock, flags); 785ae88a23bSIngo Molnar 786f21cfb25SMagnus Damm return NULL; 787ae88a23bSIngo Molnar } 7881da177e4SLinus Torvalds 7898316e381SIngo Molnar if (action->dev_id == dev_id) 790ae88a23bSIngo Molnar break; 791f17c7545SIngo Molnar action_ptr = &action->next; 792ae88a23bSIngo Molnar } 793ae88a23bSIngo Molnar 794ae88a23bSIngo Molnar /* Found it - now remove it from the list of entries: */ 795f17c7545SIngo Molnar *action_ptr = action->next; 796dbce706eSPaolo 'Blaisorblade' Giarrusso 797ae88a23bSIngo Molnar /* Currently used only by UML, might disappear one day: */ 798b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 799d1bef4edSIngo Molnar if (desc->chip->release) 800d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 801b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 802dbce706eSPaolo 'Blaisorblade' Giarrusso 803ae88a23bSIngo Molnar /* If this was the last handler, shut down the IRQ line: */ 8041da177e4SLinus Torvalds if (!desc->action) { 8051da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 806d1bef4edSIngo Molnar if (desc->chip->shutdown) 807d1bef4edSIngo Molnar desc->chip->shutdown(irq); 8081da177e4SLinus Torvalds else 809d1bef4edSIngo Molnar desc->chip->disable(irq); 8101da177e4SLinus Torvalds } 8113aa551c9SThomas Gleixner 8123aa551c9SThomas Gleixner irqthread = action->thread; 8133aa551c9SThomas Gleixner action->thread = NULL; 8143aa551c9SThomas Gleixner 8151da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 816ae88a23bSIngo Molnar 8171da177e4SLinus Torvalds unregister_handler_proc(irq, action); 8181da177e4SLinus Torvalds 819ae88a23bSIngo Molnar /* Make sure it's not being used on another CPU: */ 8201da177e4SLinus Torvalds synchronize_irq(irq); 821ae88a23bSIngo Molnar 8223aa551c9SThomas Gleixner if (irqthread) { 8233aa551c9SThomas Gleixner if (!test_bit(IRQTF_DIED, &action->thread_flags)) 8243aa551c9SThomas Gleixner kthread_stop(irqthread); 8253aa551c9SThomas Gleixner put_task_struct(irqthread); 8263aa551c9SThomas Gleixner } 8273aa551c9SThomas Gleixner 8281d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 8291d99493bSDavid Woodhouse /* 830ae88a23bSIngo Molnar * It's a shared IRQ -- the driver ought to be prepared for an IRQ 831ae88a23bSIngo Molnar * event to happen even now it's being freed, so let's make sure that 832ae88a23bSIngo Molnar * is so by doing an extra call to the handler .... 833ae88a23bSIngo Molnar * 834ae88a23bSIngo Molnar * ( We do this after actually deregistering it, to make sure that a 835ae88a23bSIngo Molnar * 'real' IRQ doesn't run in * parallel with our fake. ) 8361d99493bSDavid Woodhouse */ 8371d99493bSDavid Woodhouse if (action->flags & IRQF_SHARED) { 8381d99493bSDavid Woodhouse local_irq_save(flags); 8391d99493bSDavid Woodhouse action->handler(irq, dev_id); 8401d99493bSDavid Woodhouse local_irq_restore(flags); 8411d99493bSDavid Woodhouse } 8421d99493bSDavid Woodhouse #endif 843f21cfb25SMagnus Damm return action; 844f21cfb25SMagnus Damm } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds /** 847cbf94f06SMagnus Damm * remove_irq - free an interrupt 848cbf94f06SMagnus Damm * @irq: Interrupt line to free 849cbf94f06SMagnus Damm * @act: irqaction for the interrupt 850cbf94f06SMagnus Damm * 851cbf94f06SMagnus Damm * Used to remove interrupts statically setup by the early boot process. 852cbf94f06SMagnus Damm */ 853cbf94f06SMagnus Damm void remove_irq(unsigned int irq, struct irqaction *act) 854cbf94f06SMagnus Damm { 855cbf94f06SMagnus Damm __free_irq(irq, act->dev_id); 856cbf94f06SMagnus Damm } 857eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(remove_irq); 858cbf94f06SMagnus Damm 859cbf94f06SMagnus Damm /** 860f21cfb25SMagnus Damm * free_irq - free an interrupt allocated with request_irq 8611da177e4SLinus Torvalds * @irq: Interrupt line to free 8621da177e4SLinus Torvalds * @dev_id: Device identity to free 8631da177e4SLinus Torvalds * 8641da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 8651da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 8661da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 8671da177e4SLinus Torvalds * on the card it drives before calling this function. The function 8681da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 8691da177e4SLinus Torvalds * have completed. 8701da177e4SLinus Torvalds * 8711da177e4SLinus Torvalds * This function must not be called from interrupt context. 8721da177e4SLinus Torvalds */ 8731da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 8741da177e4SLinus Torvalds { 875cbf94f06SMagnus Damm kfree(__free_irq(irq, dev_id)); 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /** 8803aa551c9SThomas Gleixner * request_threaded_irq - allocate an interrupt line 8811da177e4SLinus Torvalds * @irq: Interrupt line to allocate 8823aa551c9SThomas Gleixner * @handler: Function to be called when the IRQ occurs. 8833aa551c9SThomas Gleixner * Primary handler for threaded interrupts 8843aa551c9SThomas Gleixner * @thread_fn: Function called from the irq handler thread 8853aa551c9SThomas Gleixner * If NULL, no irq thread is created 8861da177e4SLinus Torvalds * @irqflags: Interrupt type flags 8871da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 8881da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 8891da177e4SLinus Torvalds * 8901da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 8911da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 8921da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 8931da177e4SLinus Torvalds * your handler function must clear any interrupt the board 8941da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 8951da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 8961da177e4SLinus Torvalds * 8973aa551c9SThomas Gleixner * If you want to set up a threaded irq handler for your device 8983aa551c9SThomas Gleixner * then you need to supply @handler and @thread_fn. @handler ist 8993aa551c9SThomas Gleixner * still called in hard interrupt context and has to check 9003aa551c9SThomas Gleixner * whether the interrupt originates from the device. If yes it 9013aa551c9SThomas Gleixner * needs to disable the interrupt on the device and return 90239a2eddbSSteven Rostedt * IRQ_WAKE_THREAD which will wake up the handler thread and run 9033aa551c9SThomas Gleixner * @thread_fn. This split handler design is necessary to support 9043aa551c9SThomas Gleixner * shared interrupts. 9053aa551c9SThomas Gleixner * 9061da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 9071da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 9081da177e4SLinus Torvalds * receives this value it makes sense to use it. 9091da177e4SLinus Torvalds * 9101da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 9111da177e4SLinus Torvalds * as this is required when freeing the interrupt. 9121da177e4SLinus Torvalds * 9131da177e4SLinus Torvalds * Flags: 9141da177e4SLinus Torvalds * 9153cca53b0SThomas Gleixner * IRQF_SHARED Interrupt is shared 9163cca53b0SThomas Gleixner * IRQF_DISABLED Disable local interrupts while processing 9173cca53b0SThomas Gleixner * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy 9180c5d1eb7SDavid Brownell * IRQF_TRIGGER_* Specify active edge(s) or level 9191da177e4SLinus Torvalds * 9201da177e4SLinus Torvalds */ 9213aa551c9SThomas Gleixner int request_threaded_irq(unsigned int irq, irq_handler_t handler, 9223aa551c9SThomas Gleixner irq_handler_t thread_fn, unsigned long irqflags, 9233aa551c9SThomas Gleixner const char *devname, void *dev_id) 9241da177e4SLinus Torvalds { 9251da177e4SLinus Torvalds struct irqaction *action; 92608678b08SYinghai Lu struct irq_desc *desc; 927d3c60047SThomas Gleixner int retval; 9281da177e4SLinus Torvalds 929470c6623SDavid Brownell /* 930470c6623SDavid Brownell * handle_IRQ_event() always ignores IRQF_DISABLED except for 931470c6623SDavid Brownell * the _first_ irqaction (sigh). That can cause oopsing, but 932470c6623SDavid Brownell * the behavior is classified as "will not fix" so we need to 933470c6623SDavid Brownell * start nudging drivers away from using that idiom. 934470c6623SDavid Brownell */ 935327ec569SIngo Molnar if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) == 936327ec569SIngo Molnar (IRQF_SHARED|IRQF_DISABLED)) { 937327ec569SIngo Molnar pr_warning( 938327ec569SIngo Molnar "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n", 939470c6623SDavid Brownell irq, devname); 940327ec569SIngo Molnar } 941470c6623SDavid Brownell 942fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 943fbb9ce95SIngo Molnar /* 944fbb9ce95SIngo Molnar * Lockdep wants atomic interrupt handlers: 945fbb9ce95SIngo Molnar */ 94638515e90SThomas Gleixner irqflags |= IRQF_DISABLED; 947fbb9ce95SIngo Molnar #endif 9481da177e4SLinus Torvalds /* 9491da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 9501da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 9511da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 9521da177e4SLinus Torvalds * logic etc). 9531da177e4SLinus Torvalds */ 9543cca53b0SThomas Gleixner if ((irqflags & IRQF_SHARED) && !dev_id) 9551da177e4SLinus Torvalds return -EINVAL; 9567d94f7caSYinghai Lu 957cb5bc832SYinghai Lu desc = irq_to_desc(irq); 9587d94f7caSYinghai Lu if (!desc) 9591da177e4SLinus Torvalds return -EINVAL; 9607d94f7caSYinghai Lu 96108678b08SYinghai Lu if (desc->status & IRQ_NOREQUEST) 9626550c775SThomas Gleixner return -EINVAL; 9631da177e4SLinus Torvalds if (!handler) 9641da177e4SLinus Torvalds return -EINVAL; 9651da177e4SLinus Torvalds 96645535732SThomas Gleixner action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 9671da177e4SLinus Torvalds if (!action) 9681da177e4SLinus Torvalds return -ENOMEM; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds action->handler = handler; 9713aa551c9SThomas Gleixner action->thread_fn = thread_fn; 9721da177e4SLinus Torvalds action->flags = irqflags; 9731da177e4SLinus Torvalds action->name = devname; 9741da177e4SLinus Torvalds action->dev_id = dev_id; 9751da177e4SLinus Torvalds 976d3c60047SThomas Gleixner retval = __setup_irq(irq, desc, action); 977377bf1e4SAnton Vorontsov if (retval) 978377bf1e4SAnton Vorontsov kfree(action); 979377bf1e4SAnton Vorontsov 980a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 981a304e1b8SDavid Woodhouse if (irqflags & IRQF_SHARED) { 982a304e1b8SDavid Woodhouse /* 983a304e1b8SDavid Woodhouse * It's a shared IRQ -- the driver ought to be prepared for it 984a304e1b8SDavid Woodhouse * to happen immediately, so let's make sure.... 985377bf1e4SAnton Vorontsov * We disable the irq to make sure that a 'real' IRQ doesn't 986377bf1e4SAnton Vorontsov * run in parallel with our fake. 987a304e1b8SDavid Woodhouse */ 988a304e1b8SDavid Woodhouse unsigned long flags; 989a304e1b8SDavid Woodhouse 990377bf1e4SAnton Vorontsov disable_irq(irq); 991a304e1b8SDavid Woodhouse local_irq_save(flags); 992377bf1e4SAnton Vorontsov 993a304e1b8SDavid Woodhouse handler(irq, dev_id); 994377bf1e4SAnton Vorontsov 995a304e1b8SDavid Woodhouse local_irq_restore(flags); 996377bf1e4SAnton Vorontsov enable_irq(irq); 997a304e1b8SDavid Woodhouse } 998a304e1b8SDavid Woodhouse #endif 9991da177e4SLinus Torvalds return retval; 10001da177e4SLinus Torvalds } 10013aa551c9SThomas Gleixner EXPORT_SYMBOL(request_threaded_irq); 1002