11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/irq/manage.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This file contains driver APIs to the irq subsystem. 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 9b77d6adcSPaolo 'Blaisorblade' Giarrusso #include <linux/config.h> 101da177e4SLinus Torvalds #include <linux/irq.h> 111da177e4SLinus Torvalds #include <linux/module.h> 121da177e4SLinus Torvalds #include <linux/random.h> 131da177e4SLinus Torvalds #include <linux/interrupt.h> 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include "internals.h" 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #ifdef CONFIG_SMP 181da177e4SLinus Torvalds 1954d5d424SAshok Raj #if defined (CONFIG_GENERIC_PENDING_IRQ) || defined (CONFIG_IRQBALANCE) 2054d5d424SAshok Raj cpumask_t __cacheline_aligned pending_irq_cpumask[NR_IRQS]; 2154d5d424SAshok Raj #endif 2254d5d424SAshok Raj 231da177e4SLinus Torvalds /** 241da177e4SLinus Torvalds * synchronize_irq - wait for pending IRQ handlers (on other CPUs) 251e5d5331SRandy Dunlap * @irq: interrupt number to wait for 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 281da177e4SLinus Torvalds * to complete before returning. If you use this function while 291da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds void synchronize_irq(unsigned int irq) 341da177e4SLinus Torvalds { 351da177e4SLinus Torvalds struct irq_desc *desc = irq_desc + irq; 361da177e4SLinus Torvalds 37c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 38c2b5a251SMatthew Wilcox return; 39c2b5a251SMatthew Wilcox 401da177e4SLinus Torvalds while (desc->status & IRQ_INPROGRESS) 411da177e4SLinus Torvalds cpu_relax(); 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds EXPORT_SYMBOL(synchronize_irq); 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #endif 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds /** 481da177e4SLinus Torvalds * disable_irq_nosync - disable an irq without waiting 491da177e4SLinus Torvalds * @irq: Interrupt to disable 501da177e4SLinus Torvalds * 511da177e4SLinus Torvalds * Disable the selected interrupt line. Disables and Enables are 521da177e4SLinus Torvalds * nested. 531da177e4SLinus Torvalds * Unlike disable_irq(), this function does not ensure existing 541da177e4SLinus Torvalds * instances of the IRQ handler have completed before returning. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * This function may be called from IRQ context. 571da177e4SLinus Torvalds */ 581da177e4SLinus Torvalds void disable_irq_nosync(unsigned int irq) 591da177e4SLinus Torvalds { 60*34ffdb72SIngo Molnar struct irq_desc *desc = irq_desc + irq; 611da177e4SLinus Torvalds unsigned long flags; 621da177e4SLinus Torvalds 63c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 64c2b5a251SMatthew Wilcox return; 65c2b5a251SMatthew Wilcox 661da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 671da177e4SLinus Torvalds if (!desc->depth++) { 681da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 69d1bef4edSIngo Molnar desc->chip->disable(irq); 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync); 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /** 761da177e4SLinus Torvalds * disable_irq - disable an irq and wait for completion 771da177e4SLinus Torvalds * @irq: Interrupt to disable 781da177e4SLinus Torvalds * 791da177e4SLinus Torvalds * Disable the selected interrupt line. Enables and Disables are 801da177e4SLinus Torvalds * nested. 811da177e4SLinus Torvalds * This function waits for any pending IRQ handlers for this interrupt 821da177e4SLinus Torvalds * to complete before returning. If you use this function while 831da177e4SLinus Torvalds * holding a resource the IRQ handler may need you will deadlock. 841da177e4SLinus Torvalds * 851da177e4SLinus Torvalds * This function may be called - with care - from IRQ context. 861da177e4SLinus Torvalds */ 871da177e4SLinus Torvalds void disable_irq(unsigned int irq) 881da177e4SLinus Torvalds { 89*34ffdb72SIngo Molnar struct irq_desc *desc = irq_desc + irq; 901da177e4SLinus Torvalds 91c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 92c2b5a251SMatthew Wilcox return; 93c2b5a251SMatthew Wilcox 941da177e4SLinus Torvalds disable_irq_nosync(irq); 951da177e4SLinus Torvalds if (desc->action) 961da177e4SLinus Torvalds synchronize_irq(irq); 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /** 1011da177e4SLinus Torvalds * enable_irq - enable handling of an irq 1021da177e4SLinus Torvalds * @irq: Interrupt to enable 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Undoes the effect of one call to disable_irq(). If this 1051da177e4SLinus Torvalds * matches the last disable, processing of interrupts on this 1061da177e4SLinus Torvalds * IRQ line is re-enabled. 1071da177e4SLinus Torvalds * 1081da177e4SLinus Torvalds * This function may be called from IRQ context. 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds void enable_irq(unsigned int irq) 1111da177e4SLinus Torvalds { 112*34ffdb72SIngo Molnar struct irq_desc *desc = irq_desc + irq; 1131da177e4SLinus Torvalds unsigned long flags; 1141da177e4SLinus Torvalds 115c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 116c2b5a251SMatthew Wilcox return; 117c2b5a251SMatthew Wilcox 1181da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 1191da177e4SLinus Torvalds switch (desc->depth) { 1201da177e4SLinus Torvalds case 0: 1211da177e4SLinus Torvalds WARN_ON(1); 1221da177e4SLinus Torvalds break; 1231da177e4SLinus Torvalds case 1: { 1241da177e4SLinus Torvalds unsigned int status = desc->status & ~IRQ_DISABLED; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds desc->status = status; 1271da177e4SLinus Torvalds if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) { 1281da177e4SLinus Torvalds desc->status = status | IRQ_REPLAY; 129d1bef4edSIngo Molnar hw_resend_irq(desc->chip,irq); 1301da177e4SLinus Torvalds } 131d1bef4edSIngo Molnar desc->chip->enable(irq); 1321da177e4SLinus Torvalds /* fall-through */ 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds default: 1351da177e4SLinus Torvalds desc->depth--; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq); 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * Internal function that tells the architecture code whether a 1431da177e4SLinus Torvalds * particular irq has been exclusively allocated or is available 1441da177e4SLinus Torvalds * for driver use. 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds struct irqaction *action; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds if (irq >= NR_IRQS) 1511da177e4SLinus Torvalds return 0; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds action = irq_desc[irq].action; 1541da177e4SLinus Torvalds if (action) 1551da177e4SLinus Torvalds if (irqflags & action->flags & SA_SHIRQ) 1561da177e4SLinus Torvalds action = NULL; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds return !action; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* 1621da177e4SLinus Torvalds * Internal function to register an irqaction - typically used to 1631da177e4SLinus Torvalds * allocate special interrupts that are part of the architecture. 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds int setup_irq(unsigned int irq, struct irqaction *new) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds struct irq_desc *desc = irq_desc + irq; 1681da177e4SLinus Torvalds struct irqaction *old, **p; 1691da177e4SLinus Torvalds unsigned long flags; 1701da177e4SLinus Torvalds int shared = 0; 1711da177e4SLinus Torvalds 172c2b5a251SMatthew Wilcox if (irq >= NR_IRQS) 173c2b5a251SMatthew Wilcox return -EINVAL; 174c2b5a251SMatthew Wilcox 175d1bef4edSIngo Molnar if (desc->chip == &no_irq_type) 1761da177e4SLinus Torvalds return -ENOSYS; 1771da177e4SLinus Torvalds /* 1781da177e4SLinus Torvalds * Some drivers like serial.c use request_irq() heavily, 1791da177e4SLinus Torvalds * so we have to be careful not to interfere with a 1801da177e4SLinus Torvalds * running system. 1811da177e4SLinus Torvalds */ 1821da177e4SLinus Torvalds if (new->flags & SA_SAMPLE_RANDOM) { 1831da177e4SLinus Torvalds /* 1841da177e4SLinus Torvalds * This function might sleep, we want to call it first, 1851da177e4SLinus Torvalds * outside of the atomic block. 1861da177e4SLinus Torvalds * Yes, this might clear the entropy pool if the wrong 1871da177e4SLinus Torvalds * driver is attempted to be loaded, without actually 1881da177e4SLinus Torvalds * installing a new handler, but is this really a problem, 1891da177e4SLinus Torvalds * only the sysadmin is able to do this. 1901da177e4SLinus Torvalds */ 1911da177e4SLinus Torvalds rand_initialize_irq(irq); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* 1951da177e4SLinus Torvalds * The following block of code has to be executed atomically 1961da177e4SLinus Torvalds */ 1971da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 1981da177e4SLinus Torvalds p = &desc->action; 19906fcb0c6SIngo Molnar old = *p; 20006fcb0c6SIngo Molnar if (old) { 2011da177e4SLinus Torvalds /* Can't share interrupts unless both agree to */ 202f5163427SDimitri Sivanich if (!(old->flags & new->flags & SA_SHIRQ)) 203f5163427SDimitri Sivanich goto mismatch; 204f5163427SDimitri Sivanich 205f5163427SDimitri Sivanich #if defined(ARCH_HAS_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ) 206f5163427SDimitri Sivanich /* All handlers must agree on per-cpuness */ 207f5163427SDimitri Sivanich if ((old->flags & IRQ_PER_CPU) != (new->flags & IRQ_PER_CPU)) 208f5163427SDimitri Sivanich goto mismatch; 209f5163427SDimitri Sivanich #endif 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* add new interrupt at end of irq queue */ 2121da177e4SLinus Torvalds do { 2131da177e4SLinus Torvalds p = &old->next; 2141da177e4SLinus Torvalds old = *p; 2151da177e4SLinus Torvalds } while (old); 2161da177e4SLinus Torvalds shared = 1; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds *p = new; 220f5163427SDimitri Sivanich #if defined(ARCH_HAS_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ) 221f5163427SDimitri Sivanich if (new->flags & SA_PERCPU_IRQ) 222f5163427SDimitri Sivanich desc->status |= IRQ_PER_CPU; 223f5163427SDimitri Sivanich #endif 2241da177e4SLinus Torvalds if (!shared) { 2251da177e4SLinus Torvalds desc->depth = 0; 2261da177e4SLinus Torvalds desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | 2271da177e4SLinus Torvalds IRQ_WAITING | IRQ_INPROGRESS); 228d1bef4edSIngo Molnar if (desc->chip->startup) 229d1bef4edSIngo Molnar desc->chip->startup(irq); 2301da177e4SLinus Torvalds else 231d1bef4edSIngo Molnar desc->chip->enable(irq); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds new->irq = irq; 2361da177e4SLinus Torvalds register_irq_proc(irq); 2371da177e4SLinus Torvalds new->dir = NULL; 2381da177e4SLinus Torvalds register_handler_proc(irq, new); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds return 0; 241f5163427SDimitri Sivanich 242f5163427SDimitri Sivanich mismatch: 243f5163427SDimitri Sivanich spin_unlock_irqrestore(&desc->lock, flags); 24413e87ec6SAndrew Morton if (!(new->flags & SA_PROBEIRQ)) { 245f5163427SDimitri Sivanich printk(KERN_ERR "%s: irq handler mismatch\n", __FUNCTION__); 246f5163427SDimitri Sivanich dump_stack(); 24713e87ec6SAndrew Morton } 248f5163427SDimitri Sivanich return -EBUSY; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /** 2521da177e4SLinus Torvalds * free_irq - free an interrupt 2531da177e4SLinus Torvalds * @irq: Interrupt line to free 2541da177e4SLinus Torvalds * @dev_id: Device identity to free 2551da177e4SLinus Torvalds * 2561da177e4SLinus Torvalds * Remove an interrupt handler. The handler is removed and if the 2571da177e4SLinus Torvalds * interrupt line is no longer in use by any driver it is disabled. 2581da177e4SLinus Torvalds * On a shared IRQ the caller must ensure the interrupt is disabled 2591da177e4SLinus Torvalds * on the card it drives before calling this function. The function 2601da177e4SLinus Torvalds * does not return until any executing interrupts for this IRQ 2611da177e4SLinus Torvalds * have completed. 2621da177e4SLinus Torvalds * 2631da177e4SLinus Torvalds * This function must not be called from interrupt context. 2641da177e4SLinus Torvalds */ 2651da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds struct irq_desc *desc; 2681da177e4SLinus Torvalds struct irqaction **p; 2691da177e4SLinus Torvalds unsigned long flags; 2701da177e4SLinus Torvalds 271cd7b24bbSIngo Molnar WARN_ON(in_interrupt()); 2721da177e4SLinus Torvalds if (irq >= NR_IRQS) 2731da177e4SLinus Torvalds return; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds desc = irq_desc + irq; 2761da177e4SLinus Torvalds spin_lock_irqsave(&desc->lock, flags); 2771da177e4SLinus Torvalds p = &desc->action; 2781da177e4SLinus Torvalds for (;;) { 2791da177e4SLinus Torvalds struct irqaction *action = *p; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds if (action) { 2821da177e4SLinus Torvalds struct irqaction **pp = p; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds p = &action->next; 2851da177e4SLinus Torvalds if (action->dev_id != dev_id) 2861da177e4SLinus Torvalds continue; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* Found it - now remove it from the list of entries */ 2891da177e4SLinus Torvalds *pp = action->next; 290dbce706eSPaolo 'Blaisorblade' Giarrusso 291b77d6adcSPaolo 'Blaisorblade' Giarrusso /* Currently used only by UML, might disappear one day.*/ 292b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD 293d1bef4edSIngo Molnar if (desc->chip->release) 294d1bef4edSIngo Molnar desc->chip->release(irq, dev_id); 295b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif 296dbce706eSPaolo 'Blaisorblade' Giarrusso 2971da177e4SLinus Torvalds if (!desc->action) { 2981da177e4SLinus Torvalds desc->status |= IRQ_DISABLED; 299d1bef4edSIngo Molnar if (desc->chip->shutdown) 300d1bef4edSIngo Molnar desc->chip->shutdown(irq); 3011da177e4SLinus Torvalds else 302d1bef4edSIngo Molnar desc->chip->disable(irq); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 3051da177e4SLinus Torvalds unregister_handler_proc(irq, action); 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* Make sure it's not being used on another CPU */ 3081da177e4SLinus Torvalds synchronize_irq(irq); 3091da177e4SLinus Torvalds kfree(action); 3101da177e4SLinus Torvalds return; 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds printk(KERN_ERR "Trying to free free IRQ%d\n", irq); 3131da177e4SLinus Torvalds spin_unlock_irqrestore(&desc->lock, flags); 3141da177e4SLinus Torvalds return; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq); 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds /** 3201da177e4SLinus Torvalds * request_irq - allocate an interrupt line 3211da177e4SLinus Torvalds * @irq: Interrupt line to allocate 3221da177e4SLinus Torvalds * @handler: Function to be called when the IRQ occurs 3231da177e4SLinus Torvalds * @irqflags: Interrupt type flags 3241da177e4SLinus Torvalds * @devname: An ascii name for the claiming device 3251da177e4SLinus Torvalds * @dev_id: A cookie passed back to the handler function 3261da177e4SLinus Torvalds * 3271da177e4SLinus Torvalds * This call allocates interrupt resources and enables the 3281da177e4SLinus Torvalds * interrupt line and IRQ handling. From the point this 3291da177e4SLinus Torvalds * call is made your handler function may be invoked. Since 3301da177e4SLinus Torvalds * your handler function must clear any interrupt the board 3311da177e4SLinus Torvalds * raises, you must take care both to initialise your hardware 3321da177e4SLinus Torvalds * and to set up the interrupt handler in the right order. 3331da177e4SLinus Torvalds * 3341da177e4SLinus Torvalds * Dev_id must be globally unique. Normally the address of the 3351da177e4SLinus Torvalds * device data structure is used as the cookie. Since the handler 3361da177e4SLinus Torvalds * receives this value it makes sense to use it. 3371da177e4SLinus Torvalds * 3381da177e4SLinus Torvalds * If your interrupt is shared you must pass a non NULL dev_id 3391da177e4SLinus Torvalds * as this is required when freeing the interrupt. 3401da177e4SLinus Torvalds * 3411da177e4SLinus Torvalds * Flags: 3421da177e4SLinus Torvalds * 3431da177e4SLinus Torvalds * SA_SHIRQ Interrupt is shared 3441da177e4SLinus Torvalds * SA_INTERRUPT Disable local interrupts while processing 3451da177e4SLinus Torvalds * SA_SAMPLE_RANDOM The interrupt can be used for entropy 3461da177e4SLinus Torvalds * 3471da177e4SLinus Torvalds */ 3481da177e4SLinus Torvalds int request_irq(unsigned int irq, 3491da177e4SLinus Torvalds irqreturn_t (*handler)(int, void *, struct pt_regs *), 3501da177e4SLinus Torvalds unsigned long irqflags, const char *devname, void *dev_id) 3511da177e4SLinus Torvalds { 3521da177e4SLinus Torvalds struct irqaction *action; 3531da177e4SLinus Torvalds int retval; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /* 3561da177e4SLinus Torvalds * Sanity-check: shared interrupts must pass in a real dev-ID, 3571da177e4SLinus Torvalds * otherwise we'll have trouble later trying to figure out 3581da177e4SLinus Torvalds * which interrupt is which (messes up the interrupt freeing 3591da177e4SLinus Torvalds * logic etc). 3601da177e4SLinus Torvalds */ 3611da177e4SLinus Torvalds if ((irqflags & SA_SHIRQ) && !dev_id) 3621da177e4SLinus Torvalds return -EINVAL; 3631da177e4SLinus Torvalds if (irq >= NR_IRQS) 3641da177e4SLinus Torvalds return -EINVAL; 3651da177e4SLinus Torvalds if (!handler) 3661da177e4SLinus Torvalds return -EINVAL; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC); 3691da177e4SLinus Torvalds if (!action) 3701da177e4SLinus Torvalds return -ENOMEM; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds action->handler = handler; 3731da177e4SLinus Torvalds action->flags = irqflags; 3741da177e4SLinus Torvalds cpus_clear(action->mask); 3751da177e4SLinus Torvalds action->name = devname; 3761da177e4SLinus Torvalds action->next = NULL; 3771da177e4SLinus Torvalds action->dev_id = dev_id; 3781da177e4SLinus Torvalds 379eee45269SIvan Kokshaysky select_smp_affinity(irq); 380eee45269SIvan Kokshaysky 3811da177e4SLinus Torvalds retval = setup_irq(irq, action); 3821da177e4SLinus Torvalds if (retval) 3831da177e4SLinus Torvalds kfree(action); 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds return retval; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds EXPORT_SYMBOL(request_irq); 3881da177e4SLinus Torvalds 389