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 220*2db87321SUwe Kleine-König int set_irq_wake_real(unsigned int irq, unsigned int on) 221*2db87321SUwe Kleine-König { 222*2db87321SUwe Kleine-König struct irq_desc *desc = irq_desc + irq; 223*2db87321SUwe Kleine-König int ret = -ENXIO; 224*2db87321SUwe Kleine-König 225*2db87321SUwe Kleine-König if (desc->chip->set_wake) 226*2db87321SUwe Kleine-König ret = desc->chip->set_wake(irq, on); 227*2db87321SUwe Kleine-König 228*2db87321SUwe Kleine-König return ret; 229*2db87321SUwe Kleine-König } 230*2db87321SUwe 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; 247*2db87321SUwe 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) { 254*2db87321SUwe Kleine-König if (desc->wake_depth++ == 0) { 255*2db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 256*2db87321SUwe Kleine-König if (ret) 257*2db87321SUwe Kleine-König desc->wake_depth = 0; 25815a647ebSDavid Brownell else 259*2db87321SUwe Kleine-König desc->status |= IRQ_WAKEUP; 260*2db87321SUwe 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); 266*2db87321SUwe Kleine-König } else if (--desc->wake_depth == 0) { 267*2db87321SUwe Kleine-König ret = set_irq_wake_real(irq, on); 268*2db87321SUwe Kleine-König if (ret) 269*2db87321SUwe Kleine-König desc->wake_depth = 1; 27015a647ebSDavid Brownell else 271*2db87321SUwe Kleine-König desc->status &= ~IRQ_WAKEUP; 27215a647ebSDavid Brownell } 273*2db87321SUwe Kleine-König } 274*2db87321SUwe 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 3111da177e4SLinus Torvalds /* 3121da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 3131da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 3141da177e4SLinus Torvalds */ 3151da177e4SLinus Torvalds int setup_irq(unsigned int irq, struct irqaction *new) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds struct irq_desc *desc = irq_desc + irq; 3181da177e4SLinus Torvalds struct irqaction *old, **p; 3198b126b77SAndrew Morton const char *old_name = NULL; 3201da177e4SLinus Torvalds unsigned long flags; 3211da177e4SLinus Torvalds int shared = 0; 3221da177e4SLinus Torvalds 323c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 324c2b5a251SMatthew Wilcox return -EINVAL; 325c2b5a251SMatthew Wilcox 326f1c2662cSIngo Molnar if (desc->chip == &no_irq_chip) 3271da177e4SLinus Torvalds return -ENOSYS; 3281da177e4SLinus Torvalds /* 3291da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 3301da177e4SLinus Torvalds * so we have to be careful not to interfere with a 3311da177e4SLinus Torvalds * running system. 3321da177e4SLinus Torvalds */ 3333cca53b0SThomas Gleixner if (new->flags & IRQF_SAMPLE_RANDOM) { 3341da177e4SLinus Torvalds /* 3351da177e4SLinus Torvalds * This function might sleep, we want to call it first, 3361da177e4SLinus Torvalds * outside of the atomic block. 3371da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 3381da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 3391da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 3401da177e4SLinus Torvalds * only the sysadmin is able to do this. 3411da177e4SLinus Torvalds */ 3421da177e4SLinus Torvalds rand_initialize_irq(irq); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* 3461da177e4SLinus Torvalds * The following block of code has to be executed atomically 3471da177e4SLinus Torvalds */ 3481da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 3491da177e4SLinus Torvalds p = &desc->action; 35006fcb0c6SIngo Molnar old = *p; 35106fcb0c6SIngo Molnar if (old) { 352e76de9f8SThomas Gleixner /* 353e76de9f8SThomas Gleixner * Can't share interrupts unless both agree to and are 354e76de9f8SThomas Gleixner * the same type (level, edge, polarity). So both flag 3553cca53b0SThomas Gleixner * fields must have IRQF_SHARED set and the bits which 356e76de9f8SThomas Gleixner * set the trigger type must match. 357e76de9f8SThomas Gleixner */ 3583cca53b0SThomas Gleixner if (!((old->flags & new->flags) & IRQF_SHARED) || 3598b126b77SAndrew Morton ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) { 3608b126b77SAndrew Morton old_name = old->name; 361f5163427SDimitri Sivanich goto mismatch; 3628b126b77SAndrew Morton } 363f5163427SDimitri Sivanich 364284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU) 365f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 3663cca53b0SThomas Gleixner if ((old->flags & IRQF_PERCPU) != 3673cca53b0SThomas Gleixner (new->flags & IRQF_PERCPU)) 368f5163427SDimitri Sivanich goto mismatch; 369f5163427SDimitri Sivanich #endif 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 3721da177e4SLinus Torvalds do { 3731da177e4SLinus Torvalds p = &old->next; 3741da177e4SLinus Torvalds old = *p; 3751da177e4SLinus Torvalds } while (old); 3761da177e4SLinus Torvalds shared = 1; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds *p = new; 380f75d222bSAhmed S. Darwish 381950f4427SThomas Gleixner /* Exclude IRQ from balancing */ 382950f4427SThomas Gleixner if (new->flags & IRQF_NOBALANCING) 383950f4427SThomas Gleixner desc->status |= IRQ_NO_BALANCING; 384950f4427SThomas Gleixner 3851da177e4SLinus Torvalds if (!shared) { 3866a6de9efSThomas Gleixner irq_chip_set_defaults(desc->chip); 387e76de9f8SThomas Gleixner 388f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU) 389f75d222bSAhmed S. Darwish if (new->flags & IRQF_PERCPU) 390f75d222bSAhmed S. Darwish desc->status |= IRQ_PER_CPU; 391f75d222bSAhmed S. Darwish #endif 392f75d222bSAhmed S. Darwish 393e76de9f8SThomas Gleixner /* Setup the type (level, edge polarity) if configured: */ 3943cca53b0SThomas Gleixner if (new->flags & IRQF_TRIGGER_MASK) { 39548627d8dSThomas Gleixner if (desc->chip->set_type) 396e76de9f8SThomas Gleixner desc->chip->set_type(irq, 3973cca53b0SThomas Gleixner new->flags & IRQF_TRIGGER_MASK); 398e76de9f8SThomas Gleixner else 399e76de9f8SThomas Gleixner /* 4003cca53b0SThomas Gleixner * IRQF_TRIGGER_* but the PIC does not support 401e76de9f8SThomas Gleixner * multiple flow-types? 402e76de9f8SThomas Gleixner */ 4033cca53b0SThomas Gleixner printk(KERN_WARNING "No IRQF_TRIGGER set_type " 404e8c4b9d0SBjorn Helgaas "function for IRQ %d (%s)\n", irq, 40548627d8dSThomas Gleixner desc->chip->name); 406e76de9f8SThomas Gleixner } else 4076a6de9efSThomas Gleixner compat_irq_chip_set_default_handler(desc); 4086a6de9efSThomas Gleixner 40994d39e1fSThomas Gleixner desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | 4101adb0850SThomas Gleixner IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED); 41194d39e1fSThomas Gleixner 41294d39e1fSThomas Gleixner if (!(desc->status & IRQ_NOAUTOEN)) { 4131da177e4SLinus Torvalds desc->depth = 0; 41494d39e1fSThomas Gleixner desc->status &= ~IRQ_DISABLED; 415d1bef4edSIngo Molnar if (desc->chip->startup) 416d1bef4edSIngo Molnar desc->chip->startup(irq); 4171da177e4SLinus Torvalds else 418d1bef4edSIngo Molnar desc->chip->enable(irq); 419e76de9f8SThomas Gleixner } else 420e76de9f8SThomas Gleixner /* Undo nested disables: */ 421e76de9f8SThomas Gleixner desc->depth = 1; 42218404756SMax Krasnyansky 42318404756SMax Krasnyansky /* Set default affinity mask once everything is setup */ 42418404756SMax Krasnyansky irq_select_affinity(irq); 42594d39e1fSThomas Gleixner } 4268528b0f1SLinus Torvalds /* Reset broken irq detection when installing new handler */ 4278528b0f1SLinus Torvalds desc->irq_count = 0; 4288528b0f1SLinus Torvalds desc->irqs_unhandled = 0; 4291adb0850SThomas Gleixner 4301adb0850SThomas Gleixner /* 4311adb0850SThomas Gleixner * Check whether we disabled the irq via the spurious handler 4321adb0850SThomas Gleixner * before. Reenable it and give it another chance. 4331adb0850SThomas Gleixner */ 4341adb0850SThomas Gleixner if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) { 4351adb0850SThomas Gleixner desc->status &= ~IRQ_SPURIOUS_DISABLED; 4361adb0850SThomas Gleixner __enable_irq(desc, irq); 4371adb0850SThomas Gleixner } 4381adb0850SThomas Gleixner 4391da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds new->irq = irq; 4421da177e4SLinus Torvalds register_irq_proc(irq); 4431da177e4SLinus Torvalds new->dir = NULL; 4441da177e4SLinus Torvalds register_handler_proc(irq, new); 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds return 0; 447f5163427SDimitri Sivanich 448f5163427SDimitri Sivanich mismatch: 4493f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ 4503cca53b0SThomas Gleixner if (!(new->flags & IRQF_PROBE_SHARED)) { 451e8c4b9d0SBjorn Helgaas printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq); 4528b126b77SAndrew Morton if (old_name) 4538b126b77SAndrew Morton printk(KERN_ERR "current handler: %s\n", old_name); 454f5163427SDimitri Sivanich dump_stack(); 45513e87ec6SAndrew Morton } 4563f050447SAlan Cox #endif 4578b126b77SAndrew Morton spin_unlock_irqrestore(&desc->lock, flags); 458f5163427SDimitri Sivanich return -EBUSY; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /** 4621da177e4SLinus Torvalds * free_irq - free an interrupt 4631da177e4SLinus Torvalds * @irq: Interrupt line to free 4641da177e4SLinus Torvalds * @dev_id: Device identity to free 4651da177e4SLinus Torvalds * 4661da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 4671da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 4681da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 4691da177e4SLinus Torvalds * on the card it drives before calling this function. The function 4701da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 4711da177e4SLinus Torvalds * have completed. 4721da177e4SLinus Torvalds * 4731da177e4SLinus Torvalds * This function must not be called from interrupt context. 4741da177e4SLinus Torvalds */ 4751da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds struct irq_desc *desc; 4781da177e4SLinus Torvalds struct irqaction **p; 4791da177e4SLinus Torvalds unsigned long flags; 4801da177e4SLinus Torvalds 481cd7b24bbSIngo Molnar WARN_ON(in_interrupt()); 4821da177e4SLinus Torvalds if (irq >= NR_IRQS) 4831da177e4SLinus Torvalds return; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds desc = irq_desc + irq; 4861da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 4871da177e4SLinus Torvalds p = &desc->action; 4881da177e4SLinus Torvalds for (;;) { 4891da177e4SLinus Torvalds struct irqaction *action = *p; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds if (action) { 4921da177e4SLinus Torvalds struct irqaction **pp = p; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds p = &action->next; 4951da177e4SLinus Torvalds if (action->dev_id != dev_id) 4961da177e4SLinus Torvalds continue; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /* Found it - now remove it from the list of entries */ 4991da177e4SLinus Torvalds *pp = action->next; 500dbce706eSPaolo 'Blaisorblade' Giarrusso 501b77d6adcSPaolo 'Blaisorblade' Giarrusso /* Currently used only by UML, might disappear one day.*/ 502b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 503d1bef4edSIngo Molnar if (desc->chip->release) 504d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 505b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 506dbce706eSPaolo 'Blaisorblade' Giarrusso 5071da177e4SLinus Torvalds if (!desc->action) { 5081da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 509d1bef4edSIngo Molnar if (desc->chip->shutdown) 510d1bef4edSIngo Molnar desc->chip->shutdown(irq); 5111da177e4SLinus Torvalds else 512d1bef4edSIngo Molnar desc->chip->disable(irq); 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 5151da177e4SLinus Torvalds unregister_handler_proc(irq, action); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* Make sure it's not being used on another CPU */ 5181da177e4SLinus Torvalds synchronize_irq(irq); 5191d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 5201d99493bSDavid Woodhouse /* 5211d99493bSDavid Woodhouse * It's a shared IRQ -- the driver ought to be 5221d99493bSDavid Woodhouse * prepared for it to happen even now it's 5231d99493bSDavid Woodhouse * being freed, so let's make sure.... We do 5241d99493bSDavid Woodhouse * this after actually deregistering it, to 5251d99493bSDavid Woodhouse * make sure that a 'real' IRQ doesn't run in 5261d99493bSDavid Woodhouse * parallel with our fake 5271d99493bSDavid Woodhouse */ 5281d99493bSDavid Woodhouse if (action->flags & IRQF_SHARED) { 5291d99493bSDavid Woodhouse local_irq_save(flags); 5301d99493bSDavid Woodhouse action->handler(irq, dev_id); 5311d99493bSDavid Woodhouse local_irq_restore(flags); 5321d99493bSDavid Woodhouse } 5331d99493bSDavid Woodhouse #endif 5341da177e4SLinus Torvalds kfree(action); 5351da177e4SLinus Torvalds return; 5361da177e4SLinus Torvalds } 537e8c4b9d0SBjorn Helgaas printk(KERN_ERR "Trying to free already-free IRQ %d\n", irq); 53870edcd77SIngo Molnar #ifdef CONFIG_DEBUG_SHIRQ 53970edcd77SIngo Molnar dump_stack(); 54070edcd77SIngo Molnar #endif 5411da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 5421da177e4SLinus Torvalds return; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /** 5481da177e4SLinus Torvalds * request_irq - allocate an interrupt line 5491da177e4SLinus Torvalds * @irq: Interrupt line to allocate 5501da177e4SLinus Torvalds * @handler: Function to be called when the IRQ occurs 5511da177e4SLinus Torvalds * @irqflags: Interrupt type flags 5521da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 5531da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 5541da177e4SLinus Torvalds * 5551da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 5561da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 5571da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 5581da177e4SLinus Torvalds * your handler function must clear any interrupt the board 5591da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 5601da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 5611da177e4SLinus Torvalds * 5621da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 5631da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 5641da177e4SLinus Torvalds * receives this value it makes sense to use it. 5651da177e4SLinus Torvalds * 5661da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 5671da177e4SLinus Torvalds * as this is required when freeing the interrupt. 5681da177e4SLinus Torvalds * 5691da177e4SLinus Torvalds * Flags: 5701da177e4SLinus Torvalds * 5713cca53b0SThomas Gleixner * IRQF_SHARED Interrupt is shared 5723cca53b0SThomas Gleixner * IRQF_DISABLED Disable local interrupts while processing 5733cca53b0SThomas Gleixner * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy 5741da177e4SLinus Torvalds * 5751da177e4SLinus Torvalds */ 576da482792SDavid Howells int request_irq(unsigned int irq, irq_handler_t handler, 5771da177e4SLinus Torvalds unsigned long irqflags, const char *devname, void *dev_id) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds struct irqaction *action; 5801da177e4SLinus Torvalds int retval; 5811da177e4SLinus Torvalds 582fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP 583fbb9ce95SIngo Molnar /* 584fbb9ce95SIngo Molnar * Lockdep wants atomic interrupt handlers: 585fbb9ce95SIngo Molnar */ 58638515e90SThomas Gleixner irqflags |= IRQF_DISABLED; 587fbb9ce95SIngo Molnar #endif 5881da177e4SLinus Torvalds /* 5891da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 5901da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 5911da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 5921da177e4SLinus Torvalds * logic etc). 5931da177e4SLinus Torvalds */ 5943cca53b0SThomas Gleixner if ((irqflags & IRQF_SHARED) && !dev_id) 5951da177e4SLinus Torvalds return -EINVAL; 5961da177e4SLinus Torvalds if (irq >= NR_IRQS) 5971da177e4SLinus Torvalds return -EINVAL; 5986550c775SThomas Gleixner if (irq_desc[irq].status & IRQ_NOREQUEST) 5996550c775SThomas Gleixner return -EINVAL; 6001da177e4SLinus Torvalds if (!handler) 6011da177e4SLinus Torvalds return -EINVAL; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC); 6041da177e4SLinus Torvalds if (!action) 6051da177e4SLinus Torvalds return -ENOMEM; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds action->handler = handler; 6081da177e4SLinus Torvalds action->flags = irqflags; 6091da177e4SLinus Torvalds cpus_clear(action->mask); 6101da177e4SLinus Torvalds action->name = devname; 6111da177e4SLinus Torvalds action->next = NULL; 6121da177e4SLinus Torvalds action->dev_id = dev_id; 6131da177e4SLinus Torvalds 614a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ 615a304e1b8SDavid Woodhouse if (irqflags & IRQF_SHARED) { 616a304e1b8SDavid Woodhouse /* 617a304e1b8SDavid Woodhouse * It's a shared IRQ -- the driver ought to be prepared for it 618a304e1b8SDavid Woodhouse * to happen immediately, so let's make sure.... 619a304e1b8SDavid Woodhouse * We do this before actually registering it, to make sure that 620a304e1b8SDavid Woodhouse * a 'real' IRQ doesn't run in parallel with our fake 621a304e1b8SDavid Woodhouse */ 622a304e1b8SDavid Woodhouse unsigned long flags; 623a304e1b8SDavid Woodhouse 624a304e1b8SDavid Woodhouse local_irq_save(flags); 625a304e1b8SDavid Woodhouse handler(irq, dev_id); 626a304e1b8SDavid Woodhouse local_irq_restore(flags); 627a304e1b8SDavid Woodhouse } 628a304e1b8SDavid Woodhouse #endif 629a304e1b8SDavid Woodhouse 6301da177e4SLinus Torvalds retval = setup_irq(irq, action); 6311da177e4SLinus Torvalds if (retval) 6321da177e4SLinus Torvalds kfree(action); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds return retval; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds EXPORT_SYMBOL(request_irq); 637