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. */ 49239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags); 50a98ce5c6SHerbert Xu status = desc->status; 51239007b8SThomas Gleixner raw_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 117239007b8SThomas Gleixner raw_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; 137239007b8SThomas Gleixner raw_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 184239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags); 185548c8933SHannes Eder ret = setup_affinity(irq, desc); 1863aa551c9SThomas Gleixner if (!ret) 187591d2fb0SThomas Gleixner irq_set_thread_affinity(desc); 188239007b8SThomas Gleixner raw_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 23370aedd24SThomas Gleixner chip_bus_lock(irq, desc); 234239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags); 2350a0c5168SRafael J. Wysocki __disable_irq(desc, irq, false); 236239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags); 23770aedd24SThomas Gleixner chip_bus_sync_unlock(irq, desc); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync); 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /** 2421da177e4SLinus Torvalds * disable_irq - disable an irq and wait for completion 2431da177e4SLinus Torvalds * @irq: Interrupt to disable 2441da177e4SLinus Torvalds * 2451da177e4SLinus Torvalds * Disable the selected interrupt line. Enables and Disables are 2461da177e4SLinus Torvalds * nested. 2471da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 2481da177e4SLinus Torvalds * to complete before returning. If you use this function while 2491da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 2501da177e4SLinus Torvalds * 2511da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 2521da177e4SLinus Torvalds */ 2531da177e4SLinus Torvalds void disable_irq(unsigned int irq) 2541da177e4SLinus Torvalds { 255d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 2561da177e4SLinus Torvalds 2577d94f7caSYinghai Lu if (!desc) 258c2b5a251SMatthew Wilcox return; 259c2b5a251SMatthew Wilcox 2601da177e4SLinus Torvalds disable_irq_nosync(irq); 2611da177e4SLinus Torvalds if (desc->action) 2621da177e4SLinus Torvalds synchronize_irq(irq); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq); 2651da177e4SLinus Torvalds 2660a0c5168SRafael J. Wysocki void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume) 2671adb0850SThomas Gleixner { 2680a0c5168SRafael J. Wysocki if (resume) 2690a0c5168SRafael J. Wysocki desc->status &= ~IRQ_SUSPENDED; 2700a0c5168SRafael J. Wysocki 2711adb0850SThomas Gleixner switch (desc->depth) { 2721adb0850SThomas Gleixner case 0: 2730a0c5168SRafael J. Wysocki err_out: 274b8c512f6SArjan van de Ven WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq); 2751adb0850SThomas Gleixner break; 2761adb0850SThomas Gleixner case 1: { 2771adb0850SThomas Gleixner unsigned int status = desc->status & ~IRQ_DISABLED; 2781adb0850SThomas Gleixner 2790a0c5168SRafael J. Wysocki if (desc->status & IRQ_SUSPENDED) 2800a0c5168SRafael J. Wysocki goto err_out; 2811adb0850SThomas Gleixner /* Prevent probing on this irq: */ 2821adb0850SThomas Gleixner desc->status = status | IRQ_NOPROBE; 2831adb0850SThomas Gleixner check_irq_resend(desc, irq); 2841adb0850SThomas Gleixner /* fall-through */ 2851adb0850SThomas Gleixner } 2861adb0850SThomas Gleixner default: 2871adb0850SThomas Gleixner desc->depth--; 2881adb0850SThomas Gleixner } 2891adb0850SThomas Gleixner } 2901adb0850SThomas Gleixner 2911da177e4SLinus Torvalds /** 2921da177e4SLinus Torvalds * enable_irq - enable handling of an irq 2931da177e4SLinus Torvalds * @irq: Interrupt to enable 2941da177e4SLinus Torvalds * 2951da177e4SLinus Torvalds * Undoes the effect of one call to disable_irq(). If this 2961da177e4SLinus Torvalds * matches the last disable, processing of interrupts on this 2971da177e4SLinus Torvalds * IRQ line is re-enabled. 2981da177e4SLinus Torvalds * 29970aedd24SThomas Gleixner * This function may be called from IRQ context only when 30070aedd24SThomas Gleixner * desc->chip->bus_lock and desc->chip->bus_sync_unlock are NULL ! 3011da177e4SLinus Torvalds */ 3021da177e4SLinus Torvalds void enable_irq(unsigned int irq) 3031da177e4SLinus Torvalds { 304d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 3051da177e4SLinus Torvalds unsigned long flags; 3061da177e4SLinus Torvalds 3077d94f7caSYinghai Lu if (!desc) 308c2b5a251SMatthew Wilcox return; 309c2b5a251SMatthew Wilcox 31070aedd24SThomas Gleixner chip_bus_lock(irq, desc); 311239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags); 3120a0c5168SRafael J. Wysocki __enable_irq(desc, irq, false); 313239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags); 31470aedd24SThomas Gleixner chip_bus_sync_unlock(irq, desc); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq); 3171da177e4SLinus Torvalds 3180c5d1eb7SDavid Brownell static int set_irq_wake_real(unsigned int irq, unsigned int on) 3192db87321SUwe Kleine-König { 32008678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 3212db87321SUwe Kleine-König int ret = -ENXIO; 3222db87321SUwe Kleine-König 3232db87321SUwe Kleine-König if (desc->chip->set_wake) 3242db87321SUwe Kleine-König ret = desc->chip->set_wake(irq, on); 3252db87321SUwe Kleine-König 3262db87321SUwe Kleine-König return ret; 3272db87321SUwe Kleine-König } 3282db87321SUwe Kleine-König 329ba9a2331SThomas Gleixner /** 330ba9a2331SThomas Gleixner * set_irq_wake - control irq power management wakeup 331ba9a2331SThomas Gleixner * @irq: interrupt to control 332ba9a2331SThomas Gleixner * @on: enable/disable power management wakeup 333ba9a2331SThomas Gleixner * 33415a647ebSDavid Brownell * Enable/disable power management wakeup mode, which is 33515a647ebSDavid Brownell * disabled by default. Enables and disables must match, 33615a647ebSDavid Brownell * just as they match for non-wakeup mode support. 33715a647ebSDavid Brownell * 33815a647ebSDavid Brownell * Wakeup mode lets this IRQ wake the system from sleep 33915a647ebSDavid Brownell * states like "suspend to RAM". 340ba9a2331SThomas Gleixner */ 341ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on) 342ba9a2331SThomas Gleixner { 34308678b08SYinghai Lu struct irq_desc *desc = irq_to_desc(irq); 344ba9a2331SThomas Gleixner unsigned long flags; 3452db87321SUwe Kleine-König int ret = 0; 346ba9a2331SThomas Gleixner 34715a647ebSDavid Brownell /* wakeup-capable irqs can be shared between drivers that 34815a647ebSDavid Brownell * don't need to have the same sleep mode behaviors. 34915a647ebSDavid Brownell */ 350239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags); 35115a647ebSDavid Brownell if (on) { 3522db87321SUwe Kleine-König if (desc->wake_depth++ == 0) { 3532db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3542db87321SUwe Kleine-König if (ret) 3552db87321SUwe Kleine-König desc->wake_depth = 0; 35615a647ebSDavid Brownell else 3572db87321SUwe Kleine-König desc->status |= IRQ_WAKEUP; 3582db87321SUwe Kleine-König } 35915a647ebSDavid Brownell } else { 36015a647ebSDavid Brownell if (desc->wake_depth == 0) { 3617a2c4770SArjan van de Ven WARN(1, "Unbalanced IRQ %d wake disable\n", irq); 3622db87321SUwe Kleine-König } else if (--desc->wake_depth == 0) { 3632db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 3642db87321SUwe Kleine-König if (ret) 3652db87321SUwe Kleine-König desc->wake_depth = 1; 36615a647ebSDavid Brownell else 3672db87321SUwe Kleine-König desc->status &= ~IRQ_WAKEUP; 36815a647ebSDavid Brownell } 3692db87321SUwe Kleine-König } 3702db87321SUwe Kleine-König 371239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags); 372ba9a2331SThomas Gleixner return ret; 373ba9a2331SThomas Gleixner } 374ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake); 375ba9a2331SThomas Gleixner 3761da177e4SLinus Torvalds /* 3771da177e4SLinus Torvalds * Internal function that tells the architecture code whether a 3781da177e4SLinus Torvalds * particular irq has been exclusively allocated or is available 3791da177e4SLinus Torvalds * for driver use. 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags) 3821da177e4SLinus Torvalds { 383d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 3841da177e4SLinus Torvalds struct irqaction *action; 3851da177e4SLinus Torvalds 3867d94f7caSYinghai Lu if (!desc) 3877d94f7caSYinghai Lu return 0; 3887d94f7caSYinghai Lu 3897d94f7caSYinghai Lu if (desc->status & IRQ_NOREQUEST) 3901da177e4SLinus Torvalds return 0; 3911da177e4SLinus Torvalds 39208678b08SYinghai Lu action = desc->action; 3931da177e4SLinus Torvalds if (action) 3943cca53b0SThomas Gleixner if (irqflags & action->flags & IRQF_SHARED) 3951da177e4SLinus Torvalds action = NULL; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds return !action; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4006a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc) 4016a6de9efSThomas Gleixner { 4026a6de9efSThomas Gleixner /* 4036a6de9efSThomas Gleixner * If the architecture still has not overriden 4046a6de9efSThomas Gleixner * the flow handler then zap the default. This 4056a6de9efSThomas Gleixner * should catch incorrect flow-type setting. 4066a6de9efSThomas Gleixner */ 4076a6de9efSThomas Gleixner if (desc->handle_irq == &handle_bad_irq) 4086a6de9efSThomas Gleixner desc->handle_irq = NULL; 4096a6de9efSThomas Gleixner } 4106a6de9efSThomas Gleixner 4110c5d1eb7SDavid Brownell int __irq_set_trigger(struct irq_desc *desc, unsigned int irq, 41282736f4dSUwe Kleine-König unsigned long flags) 41382736f4dSUwe Kleine-König { 41482736f4dSUwe Kleine-König int ret; 4150c5d1eb7SDavid Brownell struct irq_chip *chip = desc->chip; 41682736f4dSUwe Kleine-König 41782736f4dSUwe Kleine-König if (!chip || !chip->set_type) { 41882736f4dSUwe Kleine-König /* 41982736f4dSUwe Kleine-König * IRQF_TRIGGER_* but the PIC does not support multiple 42082736f4dSUwe Kleine-König * flow-types? 42182736f4dSUwe Kleine-König */ 4223ff68a6aSMark Nelson pr_debug("No set_type function for IRQ %d (%s)\n", irq, 42382736f4dSUwe Kleine-König chip ? (chip->name ? : "unknown") : "unknown"); 42482736f4dSUwe Kleine-König return 0; 42582736f4dSUwe Kleine-König } 42682736f4dSUwe Kleine-König 427f2b662daSDavid Brownell /* caller masked out all except trigger mode flags */ 428f2b662daSDavid Brownell ret = chip->set_type(irq, flags); 42982736f4dSUwe Kleine-König 43082736f4dSUwe Kleine-König if (ret) 431c69ad71bSDavid Brownell pr_err("setting trigger mode %d for irq %u failed (%pF)\n", 432f2b662daSDavid Brownell (int)flags, irq, chip->set_type); 4330c5d1eb7SDavid Brownell else { 434f2b662daSDavid Brownell if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) 435f2b662daSDavid Brownell flags |= IRQ_LEVEL; 4360c5d1eb7SDavid Brownell /* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */ 437f2b662daSDavid Brownell desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK); 438f2b662daSDavid Brownell desc->status |= flags; 4390c5d1eb7SDavid Brownell } 44082736f4dSUwe Kleine-König 44182736f4dSUwe Kleine-König return ret; 44282736f4dSUwe Kleine-König } 44382736f4dSUwe Kleine-König 444b25c340cSThomas Gleixner /* 445b25c340cSThomas Gleixner * Default primary interrupt handler for threaded interrupts. Is 446b25c340cSThomas Gleixner * assigned as primary handler when request_threaded_irq is called 447b25c340cSThomas Gleixner * with handler == NULL. Useful for oneshot interrupts. 448b25c340cSThomas Gleixner */ 449b25c340cSThomas Gleixner static irqreturn_t irq_default_primary_handler(int irq, void *dev_id) 450b25c340cSThomas Gleixner { 451b25c340cSThomas Gleixner return IRQ_WAKE_THREAD; 452b25c340cSThomas Gleixner } 453b25c340cSThomas Gleixner 454399b5da2SThomas Gleixner /* 455399b5da2SThomas Gleixner * Primary handler for nested threaded interrupts. Should never be 456399b5da2SThomas Gleixner * called. 457399b5da2SThomas Gleixner */ 458399b5da2SThomas Gleixner static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id) 459399b5da2SThomas Gleixner { 460399b5da2SThomas Gleixner WARN(1, "Primary handler called for nested irq %d\n", irq); 461399b5da2SThomas Gleixner return IRQ_NONE; 462399b5da2SThomas Gleixner } 463399b5da2SThomas Gleixner 4643aa551c9SThomas Gleixner static int irq_wait_for_interrupt(struct irqaction *action) 4653aa551c9SThomas Gleixner { 4663aa551c9SThomas Gleixner while (!kthread_should_stop()) { 4673aa551c9SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE); 468f48fe81eSThomas Gleixner 469f48fe81eSThomas Gleixner if (test_and_clear_bit(IRQTF_RUNTHREAD, 470f48fe81eSThomas Gleixner &action->thread_flags)) { 4713aa551c9SThomas Gleixner __set_current_state(TASK_RUNNING); 4723aa551c9SThomas Gleixner return 0; 473f48fe81eSThomas Gleixner } 4743aa551c9SThomas Gleixner schedule(); 4753aa551c9SThomas Gleixner } 4763aa551c9SThomas Gleixner return -1; 4773aa551c9SThomas Gleixner } 4783aa551c9SThomas Gleixner 479b25c340cSThomas Gleixner /* 480b25c340cSThomas Gleixner * Oneshot interrupts keep the irq line masked until the threaded 481b25c340cSThomas Gleixner * handler finished. unmask if the interrupt has not been disabled and 482b25c340cSThomas Gleixner * is marked MASKED. 483b25c340cSThomas Gleixner */ 484b25c340cSThomas Gleixner static void irq_finalize_oneshot(unsigned int irq, struct irq_desc *desc) 485b25c340cSThomas Gleixner { 486*0b1adaa0SThomas Gleixner again: 48770aedd24SThomas Gleixner chip_bus_lock(irq, desc); 488239007b8SThomas Gleixner raw_spin_lock_irq(&desc->lock); 489*0b1adaa0SThomas Gleixner 490*0b1adaa0SThomas Gleixner /* 491*0b1adaa0SThomas Gleixner * Implausible though it may be we need to protect us against 492*0b1adaa0SThomas Gleixner * the following scenario: 493*0b1adaa0SThomas Gleixner * 494*0b1adaa0SThomas Gleixner * The thread is faster done than the hard interrupt handler 495*0b1adaa0SThomas Gleixner * on the other CPU. If we unmask the irq line then the 496*0b1adaa0SThomas Gleixner * interrupt can come in again and masks the line, leaves due 497*0b1adaa0SThomas Gleixner * to IRQ_INPROGRESS and the irq line is masked forever. 498*0b1adaa0SThomas Gleixner */ 499*0b1adaa0SThomas Gleixner if (unlikely(desc->status & IRQ_INPROGRESS)) { 500*0b1adaa0SThomas Gleixner raw_spin_unlock_irq(&desc->lock); 501*0b1adaa0SThomas Gleixner chip_bus_sync_unlock(irq, desc); 502*0b1adaa0SThomas Gleixner cpu_relax(); 503*0b1adaa0SThomas Gleixner goto again; 504*0b1adaa0SThomas Gleixner } 505*0b1adaa0SThomas Gleixner 506b25c340cSThomas Gleixner if (!(desc->status & IRQ_DISABLED) && (desc->status & IRQ_MASKED)) { 507b25c340cSThomas Gleixner desc->status &= ~IRQ_MASKED; 508b25c340cSThomas Gleixner desc->chip->unmask(irq); 509b25c340cSThomas Gleixner } 510239007b8SThomas Gleixner raw_spin_unlock_irq(&desc->lock); 51170aedd24SThomas Gleixner chip_bus_sync_unlock(irq, desc); 512b25c340cSThomas Gleixner } 513b25c340cSThomas Gleixner 51461f38261SBruno Premont #ifdef CONFIG_SMP 5153aa551c9SThomas Gleixner /* 516591d2fb0SThomas Gleixner * Check whether we need to change the affinity of the interrupt thread. 517591d2fb0SThomas Gleixner */ 518591d2fb0SThomas Gleixner static void 519591d2fb0SThomas Gleixner irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) 520591d2fb0SThomas Gleixner { 521591d2fb0SThomas Gleixner cpumask_var_t mask; 522591d2fb0SThomas Gleixner 523591d2fb0SThomas Gleixner if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags)) 524591d2fb0SThomas Gleixner return; 525591d2fb0SThomas Gleixner 526591d2fb0SThomas Gleixner /* 527591d2fb0SThomas Gleixner * In case we are out of memory we set IRQTF_AFFINITY again and 528591d2fb0SThomas Gleixner * try again next time 529591d2fb0SThomas Gleixner */ 530591d2fb0SThomas Gleixner if (!alloc_cpumask_var(&mask, GFP_KERNEL)) { 531591d2fb0SThomas Gleixner set_bit(IRQTF_AFFINITY, &action->thread_flags); 532591d2fb0SThomas Gleixner return; 533591d2fb0SThomas Gleixner } 534591d2fb0SThomas Gleixner 535239007b8SThomas Gleixner raw_spin_lock_irq(&desc->lock); 536591d2fb0SThomas Gleixner cpumask_copy(mask, desc->affinity); 537239007b8SThomas Gleixner raw_spin_unlock_irq(&desc->lock); 538591d2fb0SThomas Gleixner 539591d2fb0SThomas Gleixner set_cpus_allowed_ptr(current, mask); 540591d2fb0SThomas Gleixner free_cpumask_var(mask); 541591d2fb0SThomas Gleixner } 54261f38261SBruno Premont #else 54361f38261SBruno Premont static inline void 54461f38261SBruno Premont irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { } 54561f38261SBruno Premont #endif 546591d2fb0SThomas Gleixner 547591d2fb0SThomas Gleixner /* 5483aa551c9SThomas Gleixner * Interrupt handler thread 5493aa551c9SThomas Gleixner */ 5503aa551c9SThomas Gleixner static int irq_thread(void *data) 5513aa551c9SThomas Gleixner { 5523aa551c9SThomas Gleixner struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, }; 5533aa551c9SThomas Gleixner struct irqaction *action = data; 5543aa551c9SThomas Gleixner struct irq_desc *desc = irq_to_desc(action->irq); 555b25c340cSThomas Gleixner int wake, oneshot = desc->status & IRQ_ONESHOT; 5563aa551c9SThomas Gleixner 5573aa551c9SThomas Gleixner sched_setscheduler(current, SCHED_FIFO, ¶m); 5583aa551c9SThomas Gleixner current->irqaction = action; 5593aa551c9SThomas Gleixner 5603aa551c9SThomas Gleixner while (!irq_wait_for_interrupt(action)) { 5613aa551c9SThomas Gleixner 562591d2fb0SThomas Gleixner irq_thread_check_affinity(desc, action); 563591d2fb0SThomas Gleixner 5643aa551c9SThomas Gleixner atomic_inc(&desc->threads_active); 5653aa551c9SThomas Gleixner 566239007b8SThomas Gleixner raw_spin_lock_irq(&desc->lock); 5673aa551c9SThomas Gleixner if (unlikely(desc->status & IRQ_DISABLED)) { 5683aa551c9SThomas Gleixner /* 5693aa551c9SThomas Gleixner * CHECKME: We might need a dedicated 5703aa551c9SThomas Gleixner * IRQ_THREAD_PENDING flag here, which 5713aa551c9SThomas Gleixner * retriggers the thread in check_irq_resend() 5723aa551c9SThomas Gleixner * but AFAICT IRQ_PENDING should be fine as it 5733aa551c9SThomas Gleixner * retriggers the interrupt itself --- tglx 5743aa551c9SThomas Gleixner */ 5753aa551c9SThomas Gleixner desc->status |= IRQ_PENDING; 576239007b8SThomas Gleixner raw_spin_unlock_irq(&desc->lock); 5773aa551c9SThomas Gleixner } else { 578239007b8SThomas Gleixner raw_spin_unlock_irq(&desc->lock); 5793aa551c9SThomas Gleixner 5803aa551c9SThomas Gleixner action->thread_fn(action->irq, action->dev_id); 581b25c340cSThomas Gleixner 582b25c340cSThomas Gleixner if (oneshot) 583b25c340cSThomas Gleixner irq_finalize_oneshot(action->irq, desc); 5843aa551c9SThomas Gleixner } 5853aa551c9SThomas Gleixner 5863aa551c9SThomas Gleixner wake = atomic_dec_and_test(&desc->threads_active); 5873aa551c9SThomas Gleixner 5883aa551c9SThomas Gleixner if (wake && waitqueue_active(&desc->wait_for_threads)) 5893aa551c9SThomas Gleixner wake_up(&desc->wait_for_threads); 5903aa551c9SThomas Gleixner } 5913aa551c9SThomas Gleixner 5923aa551c9SThomas Gleixner /* 5933aa551c9SThomas Gleixner * Clear irqaction. Otherwise exit_irq_thread() would make 5943aa551c9SThomas Gleixner * fuzz about an active irq thread going into nirvana. 5953aa551c9SThomas Gleixner */ 5963aa551c9SThomas Gleixner current->irqaction = NULL; 5973aa551c9SThomas Gleixner return 0; 5983aa551c9SThomas Gleixner } 5993aa551c9SThomas Gleixner 6003aa551c9SThomas Gleixner /* 6013aa551c9SThomas Gleixner * Called from do_exit() 6023aa551c9SThomas Gleixner */ 6033aa551c9SThomas Gleixner void exit_irq_thread(void) 6043aa551c9SThomas Gleixner { 6053aa551c9SThomas Gleixner struct task_struct *tsk = current; 6063aa551c9SThomas Gleixner 6073aa551c9SThomas Gleixner if (!tsk->irqaction) 6083aa551c9SThomas Gleixner return; 6093aa551c9SThomas Gleixner 6103aa551c9SThomas Gleixner printk(KERN_ERR 6113aa551c9SThomas Gleixner "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n", 6123aa551c9SThomas Gleixner tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq); 6133aa551c9SThomas Gleixner 6143aa551c9SThomas Gleixner /* 6153aa551c9SThomas Gleixner * Set the THREAD DIED flag to prevent further wakeups of the 6163aa551c9SThomas Gleixner * soon to be gone threaded handler. 6173aa551c9SThomas Gleixner */ 6183aa551c9SThomas Gleixner set_bit(IRQTF_DIED, &tsk->irqaction->flags); 6193aa551c9SThomas Gleixner } 6203aa551c9SThomas Gleixner 6211da177e4SLinus Torvalds /* 6221da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 6231da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 6241da177e4SLinus Torvalds */ 625d3c60047SThomas Gleixner static int 626d3c60047SThomas Gleixner __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new) 6271da177e4SLinus Torvalds { 628f17c7545SIngo Molnar struct irqaction *old, **old_ptr; 6298b126b77SAndrew Morton const char *old_name = NULL; 6301da177e4SLinus Torvalds unsigned long flags; 631399b5da2SThomas Gleixner int nested, shared = 0; 63282736f4dSUwe Kleine-König int ret; 6331da177e4SLinus Torvalds 6347d94f7caSYinghai Lu if (!desc) 635c2b5a251SMatthew Wilcox return -EINVAL; 636c2b5a251SMatthew Wilcox 637f1c2662cSIngo Molnar if (desc->chip == &no_irq_chip) 6381da177e4SLinus Torvalds return -ENOSYS; 6391da177e4SLinus Torvalds /* 6401da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 6411da177e4SLinus Torvalds * so we have to be careful not to interfere with a 6421da177e4SLinus Torvalds * running system. 6431da177e4SLinus Torvalds */ 6443cca53b0SThomas Gleixner if (new->flags & IRQF_SAMPLE_RANDOM) { 6451da177e4SLinus Torvalds /* 6461da177e4SLinus Torvalds * This function might sleep, we want to call it first, 6471da177e4SLinus Torvalds * outside of the atomic block. 6481da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 6491da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 6501da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 6511da177e4SLinus Torvalds * only the sysadmin is able to do this. 6521da177e4SLinus Torvalds */ 6531da177e4SLinus Torvalds rand_initialize_irq(irq); 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 656b25c340cSThomas Gleixner /* Oneshot interrupts are not allowed with shared */ 657b25c340cSThomas Gleixner if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED)) 658b25c340cSThomas Gleixner return -EINVAL; 659b25c340cSThomas Gleixner 6601da177e4SLinus Torvalds /* 661399b5da2SThomas Gleixner * Check whether the interrupt nests into another interrupt 662399b5da2SThomas Gleixner * thread. 6633aa551c9SThomas Gleixner */ 664399b5da2SThomas Gleixner nested = desc->status & IRQ_NESTED_THREAD; 665399b5da2SThomas Gleixner if (nested) { 666399b5da2SThomas Gleixner if (!new->thread_fn) 667399b5da2SThomas Gleixner return -EINVAL; 668399b5da2SThomas Gleixner /* 669399b5da2SThomas Gleixner * Replace the primary handler which was provided from 670399b5da2SThomas Gleixner * the driver for non nested interrupt handling by the 671399b5da2SThomas Gleixner * dummy function which warns when called. 672399b5da2SThomas Gleixner */ 673399b5da2SThomas Gleixner new->handler = irq_nested_primary_handler; 674399b5da2SThomas Gleixner } 675399b5da2SThomas Gleixner 676399b5da2SThomas Gleixner /* 677399b5da2SThomas Gleixner * Create a handler thread when a thread function is supplied 678399b5da2SThomas Gleixner * and the interrupt does not nest into another interrupt 679399b5da2SThomas Gleixner * thread. 680399b5da2SThomas Gleixner */ 681399b5da2SThomas Gleixner if (new->thread_fn && !nested) { 6823aa551c9SThomas Gleixner struct task_struct *t; 6833aa551c9SThomas Gleixner 6843aa551c9SThomas Gleixner t = kthread_create(irq_thread, new, "irq/%d-%s", irq, 6853aa551c9SThomas Gleixner new->name); 6863aa551c9SThomas Gleixner if (IS_ERR(t)) 6873aa551c9SThomas Gleixner return PTR_ERR(t); 6883aa551c9SThomas Gleixner /* 6893aa551c9SThomas Gleixner * We keep the reference to the task struct even if 6903aa551c9SThomas Gleixner * the thread dies to avoid that the interrupt code 6913aa551c9SThomas Gleixner * references an already freed task_struct. 6923aa551c9SThomas Gleixner */ 6933aa551c9SThomas Gleixner get_task_struct(t); 6943aa551c9SThomas Gleixner new->thread = t; 6953aa551c9SThomas Gleixner } 6963aa551c9SThomas Gleixner 6973aa551c9SThomas Gleixner /* 6981da177e4SLinus Torvalds * The following block of code has to be executed atomically 6991da177e4SLinus Torvalds */ 700239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags); 701f17c7545SIngo Molnar old_ptr = &desc->action; 702f17c7545SIngo Molnar old = *old_ptr; 70306fcb0c6SIngo Molnar if (old) { 704e76de9f8SThomas Gleixner /* 705e76de9f8SThomas Gleixner * Can't share interrupts unless both agree to and are 706e76de9f8SThomas Gleixner * the same type (level, edge, polarity). So both flag 7073cca53b0SThomas Gleixner * fields must have IRQF_SHARED set and the bits which 708e76de9f8SThomas Gleixner * set the trigger type must match. 709e76de9f8SThomas Gleixner */ 7103cca53b0SThomas Gleixner if (!((old->flags & new->flags) & IRQF_SHARED) || 7118b126b77SAndrew Morton ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) { 7128b126b77SAndrew Morton old_name = old->name; 713f5163427SDimitri Sivanich goto mismatch; 7148b126b77SAndrew Morton } 715f5163427SDimitri Sivanich 716284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU) 717f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 7183cca53b0SThomas Gleixner if ((old->flags & IRQF_PERCPU) != 7193cca53b0SThomas Gleixner (new->flags & IRQF_PERCPU)) 720f5163427SDimitri Sivanich goto mismatch; 721f5163427SDimitri Sivanich #endif 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 7241da177e4SLinus Torvalds do { 725f17c7545SIngo Molnar old_ptr = &old->next; 726f17c7545SIngo Molnar old = *old_ptr; 7271da177e4SLinus Torvalds } while (old); 7281da177e4SLinus Torvalds shared = 1; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds if (!shared) { 7326a6de9efSThomas Gleixner irq_chip_set_defaults(desc->chip); 733e76de9f8SThomas Gleixner 7343aa551c9SThomas Gleixner init_waitqueue_head(&desc->wait_for_threads); 7353aa551c9SThomas Gleixner 73682736f4dSUwe Kleine-König /* Setup the type (level, edge polarity) if configured: */ 73782736f4dSUwe Kleine-König if (new->flags & IRQF_TRIGGER_MASK) { 738f2b662daSDavid Brownell ret = __irq_set_trigger(desc, irq, 739f2b662daSDavid Brownell new->flags & IRQF_TRIGGER_MASK); 74082736f4dSUwe Kleine-König 7413aa551c9SThomas Gleixner if (ret) 7423aa551c9SThomas Gleixner goto out_thread; 74382736f4dSUwe Kleine-König } else 74482736f4dSUwe Kleine-König compat_irq_chip_set_default_handler(desc); 745f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU) 746f75d222bSAhmed S. Darwish if (new->flags & IRQF_PERCPU) 747f75d222bSAhmed S. Darwish desc->status |= IRQ_PER_CPU; 748f75d222bSAhmed S. Darwish #endif 749f75d222bSAhmed S. Darwish 750b25c340cSThomas Gleixner desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | IRQ_ONESHOT | 7511adb0850SThomas Gleixner IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED); 75294d39e1fSThomas Gleixner 753b25c340cSThomas Gleixner if (new->flags & IRQF_ONESHOT) 754b25c340cSThomas Gleixner desc->status |= IRQ_ONESHOT; 755b25c340cSThomas Gleixner 75694d39e1fSThomas Gleixner if (!(desc->status & IRQ_NOAUTOEN)) { 7571da177e4SLinus Torvalds desc->depth = 0; 75894d39e1fSThomas Gleixner desc->status &= ~IRQ_DISABLED; 759d1bef4edSIngo Molnar desc->chip->startup(irq); 760e76de9f8SThomas Gleixner } else 761e76de9f8SThomas Gleixner /* Undo nested disables: */ 762e76de9f8SThomas Gleixner desc->depth = 1; 76318404756SMax Krasnyansky 764612e3684SThomas Gleixner /* Exclude IRQ from balancing if requested */ 765612e3684SThomas Gleixner if (new->flags & IRQF_NOBALANCING) 766612e3684SThomas Gleixner desc->status |= IRQ_NO_BALANCING; 767612e3684SThomas Gleixner 76818404756SMax Krasnyansky /* Set default affinity mask once everything is setup */ 769548c8933SHannes Eder setup_affinity(irq, desc); 7700c5d1eb7SDavid Brownell 7710c5d1eb7SDavid Brownell } else if ((new->flags & IRQF_TRIGGER_MASK) 7720c5d1eb7SDavid Brownell && (new->flags & IRQF_TRIGGER_MASK) 7730c5d1eb7SDavid Brownell != (desc->status & IRQ_TYPE_SENSE_MASK)) { 7740c5d1eb7SDavid Brownell /* hope the handler works with the actual trigger mode... */ 7750c5d1eb7SDavid Brownell pr_warning("IRQ %d uses trigger mode %d; requested %d\n", 7760c5d1eb7SDavid Brownell irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK), 7770c5d1eb7SDavid Brownell (int)(new->flags & IRQF_TRIGGER_MASK)); 77894d39e1fSThomas Gleixner } 77982736f4dSUwe Kleine-König 78069ab8494SThomas Gleixner new->irq = irq; 781f17c7545SIngo Molnar *old_ptr = new; 78282736f4dSUwe Kleine-König 7838528b0f1SLinus Torvalds /* Reset broken irq detection when installing new handler */ 7848528b0f1SLinus Torvalds desc->irq_count = 0; 7858528b0f1SLinus Torvalds desc->irqs_unhandled = 0; 7861adb0850SThomas Gleixner 7871adb0850SThomas Gleixner /* 7881adb0850SThomas Gleixner * Check whether we disabled the irq via the spurious handler 7891adb0850SThomas Gleixner * before. Reenable it and give it another chance. 7901adb0850SThomas Gleixner */ 7911adb0850SThomas Gleixner if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) { 7921adb0850SThomas Gleixner desc->status &= ~IRQ_SPURIOUS_DISABLED; 7930a0c5168SRafael J. Wysocki __enable_irq(desc, irq, false); 7941adb0850SThomas Gleixner } 7951adb0850SThomas Gleixner 796239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags); 7971da177e4SLinus Torvalds 79869ab8494SThomas Gleixner /* 79969ab8494SThomas Gleixner * Strictly no need to wake it up, but hung_task complains 80069ab8494SThomas Gleixner * when no hard interrupt wakes the thread up. 80169ab8494SThomas Gleixner */ 80269ab8494SThomas Gleixner if (new->thread) 80369ab8494SThomas Gleixner wake_up_process(new->thread); 80469ab8494SThomas Gleixner 8052c6927a3SYinghai Lu register_irq_proc(irq, desc); 8061da177e4SLinus Torvalds new->dir = NULL; 8071da177e4SLinus Torvalds register_handler_proc(irq, new); 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds return 0; 810f5163427SDimitri Sivanich 811f5163427SDimitri Sivanich mismatch: 8123f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ 8133cca53b0SThomas Gleixner if (!(new->flags & IRQF_PROBE_SHARED)) { 814e8c4b9d0SBjorn Helgaas printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq); 8158b126b77SAndrew Morton if (old_name) 8168b126b77SAndrew Morton printk(KERN_ERR "current handler: %s\n", old_name); 817f5163427SDimitri Sivanich dump_stack(); 81813e87ec6SAndrew Morton } 8193f050447SAlan Cox #endif 8203aa551c9SThomas Gleixner ret = -EBUSY; 8213aa551c9SThomas Gleixner 8223aa551c9SThomas Gleixner out_thread: 823239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags); 8243aa551c9SThomas Gleixner if (new->thread) { 8253aa551c9SThomas Gleixner struct task_struct *t = new->thread; 8263aa551c9SThomas Gleixner 8273aa551c9SThomas Gleixner new->thread = NULL; 8283aa551c9SThomas Gleixner if (likely(!test_bit(IRQTF_DIED, &new->thread_flags))) 8293aa551c9SThomas Gleixner kthread_stop(t); 8303aa551c9SThomas Gleixner put_task_struct(t); 8313aa551c9SThomas Gleixner } 8323aa551c9SThomas Gleixner return ret; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /** 836d3c60047SThomas Gleixner * setup_irq - setup an interrupt 837d3c60047SThomas Gleixner * @irq: Interrupt line to setup 838d3c60047SThomas Gleixner * @act: irqaction for the interrupt 839d3c60047SThomas Gleixner * 840d3c60047SThomas Gleixner * Used to statically setup interrupts in the early boot process. 841d3c60047SThomas Gleixner */ 842d3c60047SThomas Gleixner int setup_irq(unsigned int irq, struct irqaction *act) 843d3c60047SThomas Gleixner { 844d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 845d3c60047SThomas Gleixner 846d3c60047SThomas Gleixner return __setup_irq(irq, desc, act); 847d3c60047SThomas Gleixner } 848eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(setup_irq); 849d3c60047SThomas Gleixner 850cbf94f06SMagnus Damm /* 851cbf94f06SMagnus Damm * Internal function to unregister an irqaction - used to free 852cbf94f06SMagnus Damm * regular and special interrupts that are part of the architecture. 8531da177e4SLinus Torvalds */ 854cbf94f06SMagnus Damm static struct irqaction *__free_irq(unsigned int irq, void *dev_id) 8551da177e4SLinus Torvalds { 856d3c60047SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 857f17c7545SIngo Molnar struct irqaction *action, **action_ptr; 8581da177e4SLinus Torvalds unsigned long flags; 8591da177e4SLinus Torvalds 860ae88a23bSIngo Molnar WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 8617d94f7caSYinghai Lu 8627d94f7caSYinghai Lu if (!desc) 863f21cfb25SMagnus Damm return NULL; 8641da177e4SLinus Torvalds 865239007b8SThomas Gleixner raw_spin_lock_irqsave(&desc->lock, flags); 866ae88a23bSIngo Molnar 867ae88a23bSIngo Molnar /* 868ae88a23bSIngo Molnar * There can be multiple actions per IRQ descriptor, find the right 869ae88a23bSIngo Molnar * one based on the dev_id: 870ae88a23bSIngo Molnar */ 871f17c7545SIngo Molnar action_ptr = &desc->action; 8721da177e4SLinus Torvalds for (;;) { 873f17c7545SIngo Molnar action = *action_ptr; 8741da177e4SLinus Torvalds 875ae88a23bSIngo Molnar if (!action) { 876ae88a23bSIngo Molnar WARN(1, "Trying to free already-free IRQ %d\n", irq); 877239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags); 878ae88a23bSIngo Molnar 879f21cfb25SMagnus Damm return NULL; 880ae88a23bSIngo Molnar } 8811da177e4SLinus Torvalds 8828316e381SIngo Molnar if (action->dev_id == dev_id) 883ae88a23bSIngo Molnar break; 884f17c7545SIngo Molnar action_ptr = &action->next; 885ae88a23bSIngo Molnar } 886ae88a23bSIngo Molnar 887ae88a23bSIngo Molnar /* Found it - now remove it from the list of entries: */ 888f17c7545SIngo Molnar *action_ptr = action->next; 889dbce706eSPaolo 'Blaisorblade' Giarrusso 890ae88a23bSIngo Molnar /* Currently used only by UML, might disappear one day: */ 891b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 892d1bef4edSIngo Molnar if (desc->chip->release) 893d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 894b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 895dbce706eSPaolo 'Blaisorblade' Giarrusso 896ae88a23bSIngo Molnar /* If this was the last handler, shut down the IRQ line: */ 8971da177e4SLinus Torvalds if (!desc->action) { 8981da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 899d1bef4edSIngo Molnar if (desc->chip->shutdown) 900d1bef4edSIngo Molnar desc->chip->shutdown(irq); 9011da177e4SLinus Torvalds else 902d1bef4edSIngo Molnar desc->chip->disable(irq); 9031da177e4SLinus Torvalds } 9043aa551c9SThomas Gleixner 905239007b8SThomas Gleixner raw_spin_unlock_irqrestore(&desc->lock, flags); 906ae88a23bSIngo Molnar 9071da177e4SLinus Torvalds unregister_handler_proc(irq, action); 9081da177e4SLinus Torvalds 909ae88a23bSIngo Molnar /* Make sure it's not being used on another CPU: */ 9101da177e4SLinus Torvalds synchronize_irq(irq); 911ae88a23bSIngo Molnar 9121d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 9131d99493bSDavid Woodhouse /* 914ae88a23bSIngo Molnar * It's a shared IRQ -- the driver ought to be prepared for an IRQ 915ae88a23bSIngo Molnar * event to happen even now it's being freed, so let's make sure that 916ae88a23bSIngo Molnar * is so by doing an extra call to the handler .... 917ae88a23bSIngo Molnar * 918ae88a23bSIngo Molnar * ( We do this after actually deregistering it, to make sure that a 919ae88a23bSIngo Molnar * 'real' IRQ doesn't run in * parallel with our fake. ) 9201d99493bSDavid Woodhouse */ 9211d99493bSDavid Woodhouse if (action->flags & IRQF_SHARED) { 9221d99493bSDavid Woodhouse local_irq_save(flags); 9231d99493bSDavid Woodhouse action->handler(irq, dev_id); 9241d99493bSDavid Woodhouse local_irq_restore(flags); 9251d99493bSDavid Woodhouse } 9261d99493bSDavid Woodhouse #endif 9272d860ad7SLinus Torvalds 9282d860ad7SLinus Torvalds if (action->thread) { 9292d860ad7SLinus Torvalds if (!test_bit(IRQTF_DIED, &action->thread_flags)) 9302d860ad7SLinus Torvalds kthread_stop(action->thread); 9312d860ad7SLinus Torvalds put_task_struct(action->thread); 9322d860ad7SLinus Torvalds } 9332d860ad7SLinus Torvalds 934f21cfb25SMagnus Damm return action; 935f21cfb25SMagnus Damm } 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /** 938cbf94f06SMagnus Damm * remove_irq - free an interrupt 939cbf94f06SMagnus Damm * @irq: Interrupt line to free 940cbf94f06SMagnus Damm * @act: irqaction for the interrupt 941cbf94f06SMagnus Damm * 942cbf94f06SMagnus Damm * Used to remove interrupts statically setup by the early boot process. 943cbf94f06SMagnus Damm */ 944cbf94f06SMagnus Damm void remove_irq(unsigned int irq, struct irqaction *act) 945cbf94f06SMagnus Damm { 946cbf94f06SMagnus Damm __free_irq(irq, act->dev_id); 947cbf94f06SMagnus Damm } 948eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(remove_irq); 949cbf94f06SMagnus Damm 950cbf94f06SMagnus Damm /** 951f21cfb25SMagnus Damm * free_irq - free an interrupt allocated with request_irq 9521da177e4SLinus Torvalds * @irq: Interrupt line to free 9531da177e4SLinus Torvalds * @dev_id: Device identity to free 9541da177e4SLinus Torvalds * 9551da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 9561da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 9571da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 9581da177e4SLinus Torvalds * on the card it drives before calling this function. The function 9591da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 9601da177e4SLinus Torvalds * have completed. 9611da177e4SLinus Torvalds * 9621da177e4SLinus Torvalds * This function must not be called from interrupt context. 9631da177e4SLinus Torvalds */ 9641da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 9651da177e4SLinus Torvalds { 96670aedd24SThomas Gleixner struct irq_desc *desc = irq_to_desc(irq); 96770aedd24SThomas Gleixner 96870aedd24SThomas Gleixner if (!desc) 96970aedd24SThomas Gleixner return; 97070aedd24SThomas Gleixner 97170aedd24SThomas Gleixner chip_bus_lock(irq, desc); 972cbf94f06SMagnus Damm kfree(__free_irq(irq, dev_id)); 97370aedd24SThomas Gleixner chip_bus_sync_unlock(irq, desc); 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds /** 9783aa551c9SThomas Gleixner * request_threaded_irq - allocate an interrupt line 9791da177e4SLinus Torvalds * @irq: Interrupt line to allocate 9803aa551c9SThomas Gleixner * @handler: Function to be called when the IRQ occurs. 9813aa551c9SThomas Gleixner * Primary handler for threaded interrupts 982b25c340cSThomas Gleixner * If NULL and thread_fn != NULL the default 983b25c340cSThomas Gleixner * primary handler is installed 9843aa551c9SThomas Gleixner * @thread_fn: Function called from the irq handler thread 9853aa551c9SThomas Gleixner * If NULL, no irq thread is created 9861da177e4SLinus Torvalds * @irqflags: Interrupt type flags 9871da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 9881da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 9891da177e4SLinus Torvalds * 9901da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 9911da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 9921da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 9931da177e4SLinus Torvalds * your handler function must clear any interrupt the board 9941da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 9951da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 9961da177e4SLinus Torvalds * 9973aa551c9SThomas Gleixner * If you want to set up a threaded irq handler for your device 9983aa551c9SThomas Gleixner * then you need to supply @handler and @thread_fn. @handler ist 9993aa551c9SThomas Gleixner * still called in hard interrupt context and has to check 10003aa551c9SThomas Gleixner * whether the interrupt originates from the device. If yes it 10013aa551c9SThomas Gleixner * needs to disable the interrupt on the device and return 100239a2eddbSSteven Rostedt * IRQ_WAKE_THREAD which will wake up the handler thread and run 10033aa551c9SThomas Gleixner * @thread_fn. This split handler design is necessary to support 10043aa551c9SThomas Gleixner * shared interrupts. 10053aa551c9SThomas Gleixner * 10061da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 10071da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 10081da177e4SLinus Torvalds * receives this value it makes sense to use it. 10091da177e4SLinus Torvalds * 10101da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 10111da177e4SLinus Torvalds * as this is required when freeing the interrupt. 10121da177e4SLinus Torvalds * 10131da177e4SLinus Torvalds * Flags: 10141da177e4SLinus Torvalds * 10153cca53b0SThomas Gleixner * IRQF_SHARED Interrupt is shared 10163cca53b0SThomas Gleixner * IRQF_DISABLED Disable local interrupts while processing 10173cca53b0SThomas Gleixner * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy 10180c5d1eb7SDavid Brownell * IRQF_TRIGGER_* Specify active edge(s) or level 10191da177e4SLinus Torvalds * 10201da177e4SLinus Torvalds */ 10213aa551c9SThomas Gleixner int request_threaded_irq(unsigned int irq, irq_handler_t handler, 10223aa551c9SThomas Gleixner irq_handler_t thread_fn, unsigned long irqflags, 10233aa551c9SThomas Gleixner const char *devname, void *dev_id) 10241da177e4SLinus Torvalds { 10251da177e4SLinus Torvalds struct irqaction *action; 102608678b08SYinghai Lu struct irq_desc *desc; 1027d3c60047SThomas Gleixner int retval; 10281da177e4SLinus Torvalds 1029470c6623SDavid Brownell /* 1030470c6623SDavid Brownell * handle_IRQ_event() always ignores IRQF_DISABLED except for 1031470c6623SDavid Brownell * the _first_ irqaction (sigh). That can cause oopsing, but 1032470c6623SDavid Brownell * the behavior is classified as "will not fix" so we need to 1033470c6623SDavid Brownell * start nudging drivers away from using that idiom. 1034470c6623SDavid Brownell */ 1035327ec569SIngo Molnar if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) == 1036327ec569SIngo Molnar (IRQF_SHARED|IRQF_DISABLED)) { 1037327ec569SIngo Molnar pr_warning( 1038327ec569SIngo Molnar "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n", 1039470c6623SDavid Brownell irq, devname); 1040327ec569SIngo Molnar } 1041470c6623SDavid Brownell 1042fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 1043fbb9ce95SIngo Molnar /* 1044fbb9ce95SIngo Molnar * Lockdep wants atomic interrupt handlers: 1045fbb9ce95SIngo Molnar */ 104638515e90SThomas Gleixner irqflags |= IRQF_DISABLED; 1047fbb9ce95SIngo Molnar #endif 10481da177e4SLinus Torvalds /* 10491da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 10501da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 10511da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 10521da177e4SLinus Torvalds * logic etc). 10531da177e4SLinus Torvalds */ 10543cca53b0SThomas Gleixner if ((irqflags & IRQF_SHARED) && !dev_id) 10551da177e4SLinus Torvalds return -EINVAL; 10567d94f7caSYinghai Lu 1057cb5bc832SYinghai Lu desc = irq_to_desc(irq); 10587d94f7caSYinghai Lu if (!desc) 10591da177e4SLinus Torvalds return -EINVAL; 10607d94f7caSYinghai Lu 106108678b08SYinghai Lu if (desc->status & IRQ_NOREQUEST) 10626550c775SThomas Gleixner return -EINVAL; 1063b25c340cSThomas Gleixner 1064b25c340cSThomas Gleixner if (!handler) { 1065b25c340cSThomas Gleixner if (!thread_fn) 10661da177e4SLinus Torvalds return -EINVAL; 1067b25c340cSThomas Gleixner handler = irq_default_primary_handler; 1068b25c340cSThomas Gleixner } 10691da177e4SLinus Torvalds 107045535732SThomas Gleixner action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 10711da177e4SLinus Torvalds if (!action) 10721da177e4SLinus Torvalds return -ENOMEM; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds action->handler = handler; 10753aa551c9SThomas Gleixner action->thread_fn = thread_fn; 10761da177e4SLinus Torvalds action->flags = irqflags; 10771da177e4SLinus Torvalds action->name = devname; 10781da177e4SLinus Torvalds action->dev_id = dev_id; 10791da177e4SLinus Torvalds 108070aedd24SThomas Gleixner chip_bus_lock(irq, desc); 1081d3c60047SThomas Gleixner retval = __setup_irq(irq, desc, action); 108270aedd24SThomas Gleixner chip_bus_sync_unlock(irq, desc); 108370aedd24SThomas Gleixner 1084377bf1e4SAnton Vorontsov if (retval) 1085377bf1e4SAnton Vorontsov kfree(action); 1086377bf1e4SAnton Vorontsov 1087a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 10886ce51c43SLuis Henriques if (!retval && (irqflags & IRQF_SHARED)) { 1089a304e1b8SDavid Woodhouse /* 1090a304e1b8SDavid Woodhouse * It's a shared IRQ -- the driver ought to be prepared for it 1091a304e1b8SDavid Woodhouse * to happen immediately, so let's make sure.... 1092377bf1e4SAnton Vorontsov * We disable the irq to make sure that a 'real' IRQ doesn't 1093377bf1e4SAnton Vorontsov * run in parallel with our fake. 1094a304e1b8SDavid Woodhouse */ 1095a304e1b8SDavid Woodhouse unsigned long flags; 1096a304e1b8SDavid Woodhouse 1097377bf1e4SAnton Vorontsov disable_irq(irq); 1098a304e1b8SDavid Woodhouse local_irq_save(flags); 1099377bf1e4SAnton Vorontsov 1100a304e1b8SDavid Woodhouse handler(irq, dev_id); 1101377bf1e4SAnton Vorontsov 1102a304e1b8SDavid Woodhouse local_irq_restore(flags); 1103377bf1e4SAnton Vorontsov enable_irq(irq); 1104a304e1b8SDavid Woodhouse } 1105a304e1b8SDavid Woodhouse #endif 11061da177e4SLinus Torvalds return retval; 11071da177e4SLinus Torvalds } 11083aa551c9SThomas Gleixner EXPORT_SYMBOL(request_threaded_irq); 1109