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> 111da177e4SLinus Torvalds #include <linux/module.h> 121da177e4SLinus Torvalds #include <linux/random.h> 131da177e4SLinus Torvalds #include <linux/interrupt.h> 141aeb272cSRobert P. J. Day #include <linux/slab.h> 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include "internals.h" 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #ifdef CONFIG_SMP 191da177e4SLinus Torvalds 2018404756SMax Krasnyansky cpumask_t irq_default_affinity = CPU_MASK_ALL; 2118404756SMax Krasnyansky 221da177e4SLinus Torvalds /** 231da177e4SLinus Torvalds * synchronize_irq - wait for pending IRQ handlers (on other CPUs) 241e5d5331SRandy Dunlap * @irq: interrupt number to wait for 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 271da177e4SLinus Torvalds * to complete before returning. If you use this function while 281da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 311da177e4SLinus Torvalds */ 321da177e4SLinus Torvalds void synchronize_irq(unsigned int irq) 331da177e4SLinus Torvalds { 341da177e4SLinus Torvalds struct irq_desc *desc = irq_desc + irq; 35a98ce5c6SHerbert Xu unsigned int status; 361da177e4SLinus Torvalds 37c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 38c2b5a251SMatthew Wilcox return; 39c2b5a251SMatthew Wilcox 40a98ce5c6SHerbert Xu do { 41a98ce5c6SHerbert Xu unsigned long flags; 42a98ce5c6SHerbert Xu 43a98ce5c6SHerbert Xu /* 44a98ce5c6SHerbert Xu * Wait until we're out of the critical section. This might 45a98ce5c6SHerbert Xu * give the wrong answer due to the lack of memory barriers. 46a98ce5c6SHerbert Xu */ 471da177e4SLinus Torvalds while (desc->status & IRQ_INPROGRESS) 481da177e4SLinus Torvalds cpu_relax(); 49a98ce5c6SHerbert Xu 50a98ce5c6SHerbert Xu /* Ok, that indicated we're done: double-check carefully. */ 51a98ce5c6SHerbert Xu spin_lock_irqsave(&desc->lock, flags); 52a98ce5c6SHerbert Xu status = desc->status; 53a98ce5c6SHerbert Xu spin_unlock_irqrestore(&desc->lock, flags); 54a98ce5c6SHerbert Xu 55a98ce5c6SHerbert Xu /* Oops, that failed? */ 56a98ce5c6SHerbert Xu } while (status & IRQ_INPROGRESS); 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds EXPORT_SYMBOL(synchronize_irq); 591da177e4SLinus Torvalds 60771ee3b0SThomas Gleixner /** 61771ee3b0SThomas Gleixner * irq_can_set_affinity - Check if the affinity of a given irq can be set 62771ee3b0SThomas Gleixner * @irq: Interrupt to check 63771ee3b0SThomas Gleixner * 64771ee3b0SThomas Gleixner */ 65771ee3b0SThomas Gleixner int irq_can_set_affinity(unsigned int irq) 66771ee3b0SThomas Gleixner { 67771ee3b0SThomas Gleixner struct irq_desc *desc = irq_desc + irq; 68771ee3b0SThomas Gleixner 69771ee3b0SThomas Gleixner if (CHECK_IRQ_PER_CPU(desc->status) || !desc->chip || 70771ee3b0SThomas Gleixner !desc->chip->set_affinity) 71771ee3b0SThomas Gleixner return 0; 72771ee3b0SThomas Gleixner 73771ee3b0SThomas Gleixner return 1; 74771ee3b0SThomas Gleixner } 75771ee3b0SThomas Gleixner 76771ee3b0SThomas Gleixner /** 77771ee3b0SThomas Gleixner * irq_set_affinity - Set the irq affinity of a given irq 78771ee3b0SThomas Gleixner * @irq: Interrupt to set affinity 79771ee3b0SThomas Gleixner * @cpumask: cpumask 80771ee3b0SThomas Gleixner * 81771ee3b0SThomas Gleixner */ 82771ee3b0SThomas Gleixner int irq_set_affinity(unsigned int irq, cpumask_t cpumask) 83771ee3b0SThomas Gleixner { 84771ee3b0SThomas Gleixner struct irq_desc *desc = irq_desc + irq; 85771ee3b0SThomas Gleixner 86771ee3b0SThomas Gleixner if (!desc->chip->set_affinity) 87771ee3b0SThomas Gleixner return -EINVAL; 88771ee3b0SThomas Gleixner 89771ee3b0SThomas Gleixner set_balance_irq_affinity(irq, cpumask); 90771ee3b0SThomas Gleixner 91771ee3b0SThomas Gleixner #ifdef CONFIG_GENERIC_PENDING_IRQ 92771ee3b0SThomas Gleixner set_pending_irq(irq, cpumask); 93771ee3b0SThomas Gleixner #else 94771ee3b0SThomas Gleixner desc->affinity = cpumask; 95771ee3b0SThomas Gleixner desc->chip->set_affinity(irq, cpumask); 96771ee3b0SThomas Gleixner #endif 97771ee3b0SThomas Gleixner return 0; 98771ee3b0SThomas Gleixner } 99771ee3b0SThomas Gleixner 10018404756SMax Krasnyansky #ifndef CONFIG_AUTO_IRQ_AFFINITY 10118404756SMax Krasnyansky /* 10218404756SMax Krasnyansky * Generic version of the affinity autoselector. 10318404756SMax Krasnyansky */ 10418404756SMax Krasnyansky int irq_select_affinity(unsigned int irq) 10518404756SMax Krasnyansky { 10618404756SMax Krasnyansky cpumask_t mask; 10718404756SMax Krasnyansky 10818404756SMax Krasnyansky if (!irq_can_set_affinity(irq)) 10918404756SMax Krasnyansky return 0; 11018404756SMax Krasnyansky 11118404756SMax Krasnyansky cpus_and(mask, cpu_online_map, irq_default_affinity); 11218404756SMax Krasnyansky 11318404756SMax Krasnyansky irq_desc[irq].affinity = mask; 11418404756SMax Krasnyansky irq_desc[irq].chip->set_affinity(irq, mask); 11518404756SMax Krasnyansky 11618404756SMax Krasnyansky set_balance_irq_affinity(irq, mask); 11718404756SMax Krasnyansky return 0; 11818404756SMax Krasnyansky } 11918404756SMax Krasnyansky #endif 12018404756SMax Krasnyansky 1211da177e4SLinus Torvalds #endif 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /** 1241da177e4SLinus Torvalds * disable_irq_nosync - disable an irq without waiting 1251da177e4SLinus Torvalds * @irq: Interrupt to disable 1261da177e4SLinus Torvalds * 1271da177e4SLinus Torvalds * Disable the selected interrupt line. Disables and Enables are 1281da177e4SLinus Torvalds * nested. 1291da177e4SLinus Torvalds * Unlike disable_irq(), this function does not ensure existing 1301da177e4SLinus Torvalds * instances of the IRQ handler have completed before returning. 1311da177e4SLinus Torvalds * 1321da177e4SLinus Torvalds * This function may be called from IRQ context. 1331da177e4SLinus Torvalds */ 1341da177e4SLinus Torvalds void disable_irq_nosync(unsigned int irq) 1351da177e4SLinus Torvalds { 13634ffdb72SIngo Molnar struct irq_desc *desc = irq_desc + irq; 1371da177e4SLinus Torvalds unsigned long flags; 1381da177e4SLinus Torvalds 139c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 140c2b5a251SMatthew Wilcox return; 141c2b5a251SMatthew Wilcox 1421da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 1431da177e4SLinus Torvalds if (!desc->depth++) { 1441da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 145d1bef4edSIngo Molnar desc->chip->disable(irq); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync); 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /** 1521da177e4SLinus Torvalds * disable_irq - disable an irq and wait for completion 1531da177e4SLinus Torvalds * @irq: Interrupt to disable 1541da177e4SLinus Torvalds * 1551da177e4SLinus Torvalds * Disable the selected interrupt line. Enables and Disables are 1561da177e4SLinus Torvalds * nested. 1571da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 1581da177e4SLinus Torvalds * to complete before returning. If you use this function while 1591da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 1601da177e4SLinus Torvalds * 1611da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds void disable_irq(unsigned int irq) 1641da177e4SLinus Torvalds { 16534ffdb72SIngo Molnar struct irq_desc *desc = irq_desc + irq; 1661da177e4SLinus Torvalds 167c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 168c2b5a251SMatthew Wilcox return; 169c2b5a251SMatthew Wilcox 1701da177e4SLinus Torvalds disable_irq_nosync(irq); 1711da177e4SLinus Torvalds if (desc->action) 1721da177e4SLinus Torvalds synchronize_irq(irq); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq); 1751da177e4SLinus Torvalds 1761adb0850SThomas Gleixner static void __enable_irq(struct irq_desc *desc, unsigned int irq) 1771adb0850SThomas Gleixner { 1781adb0850SThomas Gleixner switch (desc->depth) { 1791adb0850SThomas Gleixner case 0: 1801adb0850SThomas Gleixner printk(KERN_WARNING "Unbalanced enable for IRQ %d\n", irq); 1811adb0850SThomas Gleixner WARN_ON(1); 1821adb0850SThomas Gleixner break; 1831adb0850SThomas Gleixner case 1: { 1841adb0850SThomas Gleixner unsigned int status = desc->status & ~IRQ_DISABLED; 1851adb0850SThomas Gleixner 1861adb0850SThomas Gleixner /* Prevent probing on this irq: */ 1871adb0850SThomas Gleixner desc->status = status | IRQ_NOPROBE; 1881adb0850SThomas Gleixner check_irq_resend(desc, irq); 1891adb0850SThomas Gleixner /* fall-through */ 1901adb0850SThomas Gleixner } 1911adb0850SThomas Gleixner default: 1921adb0850SThomas Gleixner desc->depth--; 1931adb0850SThomas Gleixner } 1941adb0850SThomas Gleixner } 1951adb0850SThomas Gleixner 1961da177e4SLinus Torvalds /** 1971da177e4SLinus Torvalds * enable_irq - enable handling of an irq 1981da177e4SLinus Torvalds * @irq: Interrupt to enable 1991da177e4SLinus Torvalds * 2001da177e4SLinus Torvalds * Undoes the effect of one call to disable_irq(). If this 2011da177e4SLinus Torvalds * matches the last disable, processing of interrupts on this 2021da177e4SLinus Torvalds * IRQ line is re-enabled. 2031da177e4SLinus Torvalds * 2041da177e4SLinus Torvalds * This function may be called from IRQ context. 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds void enable_irq(unsigned int irq) 2071da177e4SLinus Torvalds { 20834ffdb72SIngo Molnar struct irq_desc *desc = irq_desc + irq; 2091da177e4SLinus Torvalds unsigned long flags; 2101da177e4SLinus Torvalds 211c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 212c2b5a251SMatthew Wilcox return; 213c2b5a251SMatthew Wilcox 2141da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 2151adb0850SThomas Gleixner __enable_irq(desc, irq); 2161da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq); 2191da177e4SLinus Torvalds 2202db87321SUwe Kleine-König int set_irq_wake_real(unsigned int irq, unsigned int on) 2212db87321SUwe Kleine-König { 2222db87321SUwe Kleine-König struct irq_desc *desc = irq_desc + irq; 2232db87321SUwe Kleine-König int ret = -ENXIO; 2242db87321SUwe Kleine-König 2252db87321SUwe Kleine-König if (desc->chip->set_wake) 2262db87321SUwe Kleine-König ret = desc->chip->set_wake(irq, on); 2272db87321SUwe Kleine-König 2282db87321SUwe Kleine-König return ret; 2292db87321SUwe Kleine-König } 2302db87321SUwe Kleine-König 231ba9a2331SThomas Gleixner /** 232ba9a2331SThomas Gleixner * set_irq_wake - control irq power management wakeup 233ba9a2331SThomas Gleixner * @irq: interrupt to control 234ba9a2331SThomas Gleixner * @on: enable/disable power management wakeup 235ba9a2331SThomas Gleixner * 23615a647ebSDavid Brownell * Enable/disable power management wakeup mode, which is 23715a647ebSDavid Brownell * disabled by default. Enables and disables must match, 23815a647ebSDavid Brownell * just as they match for non-wakeup mode support. 23915a647ebSDavid Brownell * 24015a647ebSDavid Brownell * Wakeup mode lets this IRQ wake the system from sleep 24115a647ebSDavid Brownell * states like "suspend to RAM". 242ba9a2331SThomas Gleixner */ 243ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on) 244ba9a2331SThomas Gleixner { 245ba9a2331SThomas Gleixner struct irq_desc *desc = irq_desc + irq; 246ba9a2331SThomas Gleixner unsigned long flags; 2472db87321SUwe Kleine-König int ret = 0; 248ba9a2331SThomas Gleixner 24915a647ebSDavid Brownell /* wakeup-capable irqs can be shared between drivers that 25015a647ebSDavid Brownell * don't need to have the same sleep mode behaviors. 25115a647ebSDavid Brownell */ 252ba9a2331SThomas Gleixner spin_lock_irqsave(&desc->lock, flags); 25315a647ebSDavid Brownell if (on) { 2542db87321SUwe Kleine-König if (desc->wake_depth++ == 0) { 2552db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 2562db87321SUwe Kleine-König if (ret) 2572db87321SUwe Kleine-König desc->wake_depth = 0; 25815a647ebSDavid Brownell else 2592db87321SUwe Kleine-König desc->status |= IRQ_WAKEUP; 2602db87321SUwe Kleine-König } 26115a647ebSDavid Brownell } else { 26215a647ebSDavid Brownell if (desc->wake_depth == 0) { 26315a647ebSDavid Brownell printk(KERN_WARNING "Unbalanced IRQ %d " 26415a647ebSDavid Brownell "wake disable\n", irq); 26515a647ebSDavid Brownell WARN_ON(1); 2662db87321SUwe Kleine-König } else if (--desc->wake_depth == 0) { 2672db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 2682db87321SUwe Kleine-König if (ret) 2692db87321SUwe Kleine-König desc->wake_depth = 1; 27015a647ebSDavid Brownell else 2712db87321SUwe Kleine-König desc->status &= ~IRQ_WAKEUP; 27215a647ebSDavid Brownell } 2732db87321SUwe Kleine-König } 2742db87321SUwe Kleine-König 275ba9a2331SThomas Gleixner spin_unlock_irqrestore(&desc->lock, flags); 276ba9a2331SThomas Gleixner return ret; 277ba9a2331SThomas Gleixner } 278ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake); 279ba9a2331SThomas Gleixner 2801da177e4SLinus Torvalds /* 2811da177e4SLinus Torvalds * Internal function that tells the architecture code whether a 2821da177e4SLinus Torvalds * particular irq has been exclusively allocated or is available 2831da177e4SLinus Torvalds * for driver use. 2841da177e4SLinus Torvalds */ 2851da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds struct irqaction *action; 2881da177e4SLinus Torvalds 2896550c775SThomas Gleixner if (irq >= NR_IRQS || irq_desc[irq].status & IRQ_NOREQUEST) 2901da177e4SLinus Torvalds return 0; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds action = irq_desc[irq].action; 2931da177e4SLinus Torvalds if (action) 2943cca53b0SThomas Gleixner if (irqflags & action->flags & IRQF_SHARED) 2951da177e4SLinus Torvalds action = NULL; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds return !action; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3006a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc) 3016a6de9efSThomas Gleixner { 3026a6de9efSThomas Gleixner /* 3036a6de9efSThomas Gleixner * If the architecture still has not overriden 3046a6de9efSThomas Gleixner * the flow handler then zap the default. This 3056a6de9efSThomas Gleixner * should catch incorrect flow-type setting. 3066a6de9efSThomas Gleixner */ 3076a6de9efSThomas Gleixner if (desc->handle_irq == &handle_bad_irq) 3086a6de9efSThomas Gleixner desc->handle_irq = NULL; 3096a6de9efSThomas Gleixner } 3106a6de9efSThomas Gleixner 311*82736f4dSUwe Kleine-König static int __irq_set_trigger(struct irq_chip *chip, unsigned int irq, 312*82736f4dSUwe Kleine-König unsigned long flags) 313*82736f4dSUwe Kleine-König { 314*82736f4dSUwe Kleine-König int ret; 315*82736f4dSUwe Kleine-König 316*82736f4dSUwe Kleine-König if (!chip || !chip->set_type) { 317*82736f4dSUwe Kleine-König /* 318*82736f4dSUwe Kleine-König * IRQF_TRIGGER_* but the PIC does not support multiple 319*82736f4dSUwe Kleine-König * flow-types? 320*82736f4dSUwe Kleine-König */ 321*82736f4dSUwe Kleine-König pr_warning("No set_type function for IRQ %d (%s)\n", irq, 322*82736f4dSUwe Kleine-König chip ? (chip->name ? : "unknown") : "unknown"); 323*82736f4dSUwe Kleine-König return 0; 324*82736f4dSUwe Kleine-König } 325*82736f4dSUwe Kleine-König 326*82736f4dSUwe Kleine-König ret = chip->set_type(irq, flags & IRQF_TRIGGER_MASK); 327*82736f4dSUwe Kleine-König 328*82736f4dSUwe Kleine-König if (ret) 329*82736f4dSUwe Kleine-König pr_err("setting flow type for irq %u failed (%pF)\n", 330*82736f4dSUwe Kleine-König irq, chip->set_type); 331*82736f4dSUwe Kleine-König 332*82736f4dSUwe Kleine-König return ret; 333*82736f4dSUwe Kleine-König } 334*82736f4dSUwe Kleine-König 3351da177e4SLinus Torvalds /* 3361da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 3371da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds int setup_irq(unsigned int irq, struct irqaction *new) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds struct irq_desc *desc = irq_desc + irq; 3421da177e4SLinus Torvalds struct irqaction *old, **p; 3438b126b77SAndrew Morton const char *old_name = NULL; 3441da177e4SLinus Torvalds unsigned long flags; 3451da177e4SLinus Torvalds int shared = 0; 346*82736f4dSUwe Kleine-König int ret; 3471da177e4SLinus Torvalds 348c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 349c2b5a251SMatthew Wilcox return -EINVAL; 350c2b5a251SMatthew Wilcox 351f1c2662cSIngo Molnar if (desc->chip == &no_irq_chip) 3521da177e4SLinus Torvalds return -ENOSYS; 3531da177e4SLinus Torvalds /* 3541da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 3551da177e4SLinus Torvalds * so we have to be careful not to interfere with a 3561da177e4SLinus Torvalds * running system. 3571da177e4SLinus Torvalds */ 3583cca53b0SThomas Gleixner if (new->flags & IRQF_SAMPLE_RANDOM) { 3591da177e4SLinus Torvalds /* 3601da177e4SLinus Torvalds * This function might sleep, we want to call it first, 3611da177e4SLinus Torvalds * outside of the atomic block. 3621da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 3631da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 3641da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 3651da177e4SLinus Torvalds * only the sysadmin is able to do this. 3661da177e4SLinus Torvalds */ 3671da177e4SLinus Torvalds rand_initialize_irq(irq); 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /* 3711da177e4SLinus Torvalds * The following block of code has to be executed atomically 3721da177e4SLinus Torvalds */ 3731da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 3741da177e4SLinus Torvalds p = &desc->action; 37506fcb0c6SIngo Molnar old = *p; 37606fcb0c6SIngo Molnar if (old) { 377e76de9f8SThomas Gleixner /* 378e76de9f8SThomas Gleixner * Can't share interrupts unless both agree to and are 379e76de9f8SThomas Gleixner * the same type (level, edge, polarity). So both flag 3803cca53b0SThomas Gleixner * fields must have IRQF_SHARED set and the bits which 381e76de9f8SThomas Gleixner * set the trigger type must match. 382e76de9f8SThomas Gleixner */ 3833cca53b0SThomas Gleixner if (!((old->flags & new->flags) & IRQF_SHARED) || 3848b126b77SAndrew Morton ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) { 3858b126b77SAndrew Morton old_name = old->name; 386f5163427SDimitri Sivanich goto mismatch; 3878b126b77SAndrew Morton } 388f5163427SDimitri Sivanich 389284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU) 390f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 3913cca53b0SThomas Gleixner if ((old->flags & IRQF_PERCPU) != 3923cca53b0SThomas Gleixner (new->flags & IRQF_PERCPU)) 393f5163427SDimitri Sivanich goto mismatch; 394f5163427SDimitri Sivanich #endif 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 3971da177e4SLinus Torvalds do { 3981da177e4SLinus Torvalds p = &old->next; 3991da177e4SLinus Torvalds old = *p; 4001da177e4SLinus Torvalds } while (old); 4011da177e4SLinus Torvalds shared = 1; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds if (!shared) { 4056a6de9efSThomas Gleixner irq_chip_set_defaults(desc->chip); 406e76de9f8SThomas Gleixner 407*82736f4dSUwe Kleine-König /* Setup the type (level, edge polarity) if configured: */ 408*82736f4dSUwe Kleine-König if (new->flags & IRQF_TRIGGER_MASK) { 409*82736f4dSUwe Kleine-König ret = __irq_set_trigger(desc->chip, irq, new->flags); 410*82736f4dSUwe Kleine-König 411*82736f4dSUwe Kleine-König if (ret) { 412*82736f4dSUwe Kleine-König spin_unlock_irqrestore(&desc->lock, flags); 413*82736f4dSUwe Kleine-König return ret; 414*82736f4dSUwe Kleine-König } 415*82736f4dSUwe Kleine-König } else 416*82736f4dSUwe Kleine-König compat_irq_chip_set_default_handler(desc); 417f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU) 418f75d222bSAhmed S. Darwish if (new->flags & IRQF_PERCPU) 419f75d222bSAhmed S. Darwish desc->status |= IRQ_PER_CPU; 420f75d222bSAhmed S. Darwish #endif 421f75d222bSAhmed S. Darwish 42294d39e1fSThomas Gleixner desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | 4231adb0850SThomas Gleixner IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED); 42494d39e1fSThomas Gleixner 42594d39e1fSThomas Gleixner if (!(desc->status & IRQ_NOAUTOEN)) { 4261da177e4SLinus Torvalds desc->depth = 0; 42794d39e1fSThomas Gleixner desc->status &= ~IRQ_DISABLED; 428d1bef4edSIngo Molnar if (desc->chip->startup) 429d1bef4edSIngo Molnar desc->chip->startup(irq); 4301da177e4SLinus Torvalds else 431d1bef4edSIngo Molnar desc->chip->enable(irq); 432e76de9f8SThomas Gleixner } else 433e76de9f8SThomas Gleixner /* Undo nested disables: */ 434e76de9f8SThomas Gleixner desc->depth = 1; 43518404756SMax Krasnyansky 43618404756SMax Krasnyansky /* Set default affinity mask once everything is setup */ 43718404756SMax Krasnyansky irq_select_affinity(irq); 43894d39e1fSThomas Gleixner } 439*82736f4dSUwe Kleine-König 440*82736f4dSUwe Kleine-König *p = new; 441*82736f4dSUwe Kleine-König 442*82736f4dSUwe Kleine-König /* Exclude IRQ from balancing */ 443*82736f4dSUwe Kleine-König if (new->flags & IRQF_NOBALANCING) 444*82736f4dSUwe Kleine-König desc->status |= IRQ_NO_BALANCING; 445*82736f4dSUwe Kleine-König 4468528b0f1SLinus Torvalds /* Reset broken irq detection when installing new handler */ 4478528b0f1SLinus Torvalds desc->irq_count = 0; 4488528b0f1SLinus Torvalds desc->irqs_unhandled = 0; 4491adb0850SThomas Gleixner 4501adb0850SThomas Gleixner /* 4511adb0850SThomas Gleixner * Check whether we disabled the irq via the spurious handler 4521adb0850SThomas Gleixner * before. Reenable it and give it another chance. 4531adb0850SThomas Gleixner */ 4541adb0850SThomas Gleixner if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) { 4551adb0850SThomas Gleixner desc->status &= ~IRQ_SPURIOUS_DISABLED; 4561adb0850SThomas Gleixner __enable_irq(desc, irq); 4571adb0850SThomas Gleixner } 4581adb0850SThomas Gleixner 4591da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds new->irq = irq; 4621da177e4SLinus Torvalds register_irq_proc(irq); 4631da177e4SLinus Torvalds new->dir = NULL; 4641da177e4SLinus Torvalds register_handler_proc(irq, new); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds return 0; 467f5163427SDimitri Sivanich 468f5163427SDimitri Sivanich mismatch: 4693f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ 4703cca53b0SThomas Gleixner if (!(new->flags & IRQF_PROBE_SHARED)) { 471e8c4b9d0SBjorn Helgaas printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq); 4728b126b77SAndrew Morton if (old_name) 4738b126b77SAndrew Morton printk(KERN_ERR "current handler: %s\n", old_name); 474f5163427SDimitri Sivanich dump_stack(); 47513e87ec6SAndrew Morton } 4763f050447SAlan Cox #endif 4778b126b77SAndrew Morton spin_unlock_irqrestore(&desc->lock, flags); 478f5163427SDimitri Sivanich return -EBUSY; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds /** 4821da177e4SLinus Torvalds * free_irq - free an interrupt 4831da177e4SLinus Torvalds * @irq: Interrupt line to free 4841da177e4SLinus Torvalds * @dev_id: Device identity to free 4851da177e4SLinus Torvalds * 4861da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 4871da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 4881da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 4891da177e4SLinus Torvalds * on the card it drives before calling this function. The function 4901da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 4911da177e4SLinus Torvalds * have completed. 4921da177e4SLinus Torvalds * 4931da177e4SLinus Torvalds * This function must not be called from interrupt context. 4941da177e4SLinus Torvalds */ 4951da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 4961da177e4SLinus Torvalds { 4971da177e4SLinus Torvalds struct irq_desc *desc; 4981da177e4SLinus Torvalds struct irqaction **p; 4991da177e4SLinus Torvalds unsigned long flags; 5001da177e4SLinus Torvalds 501cd7b24bbSIngo Molnar WARN_ON(in_interrupt()); 5021da177e4SLinus Torvalds if (irq >= NR_IRQS) 5031da177e4SLinus Torvalds return; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds desc = irq_desc + irq; 5061da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 5071da177e4SLinus Torvalds p = &desc->action; 5081da177e4SLinus Torvalds for (;;) { 5091da177e4SLinus Torvalds struct irqaction *action = *p; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (action) { 5121da177e4SLinus Torvalds struct irqaction **pp = p; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds p = &action->next; 5151da177e4SLinus Torvalds if (action->dev_id != dev_id) 5161da177e4SLinus Torvalds continue; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds /* Found it - now remove it from the list of entries */ 5191da177e4SLinus Torvalds *pp = action->next; 520dbce706eSPaolo 'Blaisorblade' Giarrusso 521b77d6adcSPaolo 'Blaisorblade' Giarrusso /* Currently used only by UML, might disappear one day.*/ 522b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 523d1bef4edSIngo Molnar if (desc->chip->release) 524d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 525b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 526dbce706eSPaolo 'Blaisorblade' Giarrusso 5271da177e4SLinus Torvalds if (!desc->action) { 5281da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 529d1bef4edSIngo Molnar if (desc->chip->shutdown) 530d1bef4edSIngo Molnar desc->chip->shutdown(irq); 5311da177e4SLinus Torvalds else 532d1bef4edSIngo Molnar desc->chip->disable(irq); 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 5351da177e4SLinus Torvalds unregister_handler_proc(irq, action); 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* Make sure it's not being used on another CPU */ 5381da177e4SLinus Torvalds synchronize_irq(irq); 5391d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 5401d99493bSDavid Woodhouse /* 5411d99493bSDavid Woodhouse * It's a shared IRQ -- the driver ought to be 5421d99493bSDavid Woodhouse * prepared for it to happen even now it's 5431d99493bSDavid Woodhouse * being freed, so let's make sure.... We do 5441d99493bSDavid Woodhouse * this after actually deregistering it, to 5451d99493bSDavid Woodhouse * make sure that a 'real' IRQ doesn't run in 5461d99493bSDavid Woodhouse * parallel with our fake 5471d99493bSDavid Woodhouse */ 5481d99493bSDavid Woodhouse if (action->flags & IRQF_SHARED) { 5491d99493bSDavid Woodhouse local_irq_save(flags); 5501d99493bSDavid Woodhouse action->handler(irq, dev_id); 5511d99493bSDavid Woodhouse local_irq_restore(flags); 5521d99493bSDavid Woodhouse } 5531d99493bSDavid Woodhouse #endif 5541da177e4SLinus Torvalds kfree(action); 5551da177e4SLinus Torvalds return; 5561da177e4SLinus Torvalds } 557e8c4b9d0SBjorn Helgaas printk(KERN_ERR "Trying to free already-free IRQ %d\n", irq); 55870edcd77SIngo Molnar #ifdef CONFIG_DEBUG_SHIRQ 55970edcd77SIngo Molnar dump_stack(); 56070edcd77SIngo Molnar #endif 5611da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 5621da177e4SLinus Torvalds return; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /** 5681da177e4SLinus Torvalds * request_irq - allocate an interrupt line 5691da177e4SLinus Torvalds * @irq: Interrupt line to allocate 5701da177e4SLinus Torvalds * @handler: Function to be called when the IRQ occurs 5711da177e4SLinus Torvalds * @irqflags: Interrupt type flags 5721da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 5731da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 5741da177e4SLinus Torvalds * 5751da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 5761da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 5771da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 5781da177e4SLinus Torvalds * your handler function must clear any interrupt the board 5791da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 5801da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 5811da177e4SLinus Torvalds * 5821da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 5831da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 5841da177e4SLinus Torvalds * receives this value it makes sense to use it. 5851da177e4SLinus Torvalds * 5861da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 5871da177e4SLinus Torvalds * as this is required when freeing the interrupt. 5881da177e4SLinus Torvalds * 5891da177e4SLinus Torvalds * Flags: 5901da177e4SLinus Torvalds * 5913cca53b0SThomas Gleixner * IRQF_SHARED Interrupt is shared 5923cca53b0SThomas Gleixner * IRQF_DISABLED Disable local interrupts while processing 5933cca53b0SThomas Gleixner * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy 5941da177e4SLinus Torvalds * 5951da177e4SLinus Torvalds */ 596da482792SDavid Howells int request_irq(unsigned int irq, irq_handler_t handler, 5971da177e4SLinus Torvalds unsigned long irqflags, const char *devname, void *dev_id) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds struct irqaction *action; 6001da177e4SLinus Torvalds int retval; 6011da177e4SLinus Torvalds 602fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 603fbb9ce95SIngo Molnar /* 604fbb9ce95SIngo Molnar * Lockdep wants atomic interrupt handlers: 605fbb9ce95SIngo Molnar */ 60638515e90SThomas Gleixner irqflags |= IRQF_DISABLED; 607fbb9ce95SIngo Molnar #endif 6081da177e4SLinus Torvalds /* 6091da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 6101da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 6111da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 6121da177e4SLinus Torvalds * logic etc). 6131da177e4SLinus Torvalds */ 6143cca53b0SThomas Gleixner if ((irqflags & IRQF_SHARED) && !dev_id) 6151da177e4SLinus Torvalds return -EINVAL; 6161da177e4SLinus Torvalds if (irq >= NR_IRQS) 6171da177e4SLinus Torvalds return -EINVAL; 6186550c775SThomas Gleixner if (irq_desc[irq].status & IRQ_NOREQUEST) 6196550c775SThomas Gleixner return -EINVAL; 6201da177e4SLinus Torvalds if (!handler) 6211da177e4SLinus Torvalds return -EINVAL; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC); 6241da177e4SLinus Torvalds if (!action) 6251da177e4SLinus Torvalds return -ENOMEM; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds action->handler = handler; 6281da177e4SLinus Torvalds action->flags = irqflags; 6291da177e4SLinus Torvalds cpus_clear(action->mask); 6301da177e4SLinus Torvalds action->name = devname; 6311da177e4SLinus Torvalds action->next = NULL; 6321da177e4SLinus Torvalds action->dev_id = dev_id; 6331da177e4SLinus Torvalds 634a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 635a304e1b8SDavid Woodhouse if (irqflags & IRQF_SHARED) { 636a304e1b8SDavid Woodhouse /* 637a304e1b8SDavid Woodhouse * It's a shared IRQ -- the driver ought to be prepared for it 638a304e1b8SDavid Woodhouse * to happen immediately, so let's make sure.... 639a304e1b8SDavid Woodhouse * We do this before actually registering it, to make sure that 640a304e1b8SDavid Woodhouse * a 'real' IRQ doesn't run in parallel with our fake 641a304e1b8SDavid Woodhouse */ 642a304e1b8SDavid Woodhouse unsigned long flags; 643a304e1b8SDavid Woodhouse 644a304e1b8SDavid Woodhouse local_irq_save(flags); 645a304e1b8SDavid Woodhouse handler(irq, dev_id); 646a304e1b8SDavid Woodhouse local_irq_restore(flags); 647a304e1b8SDavid Woodhouse } 648a304e1b8SDavid Woodhouse #endif 649a304e1b8SDavid Woodhouse 6501da177e4SLinus Torvalds retval = setup_irq(irq, action); 6511da177e4SLinus Torvalds if (retval) 6521da177e4SLinus Torvalds kfree(action); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds return retval; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds EXPORT_SYMBOL(request_irq); 657