xref: /openbmc/linux/kernel/irq/manage.c (revision da482792a6d1a3fbaaa25fae867b343fb4db3246)
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>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include "internals.h"
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #ifdef CONFIG_SMP
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds /**
201da177e4SLinus Torvalds  *	synchronize_irq - wait for pending IRQ handlers (on other CPUs)
211e5d5331SRandy Dunlap  *	@irq: interrupt number to wait for
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *	This function waits for any pending IRQ handlers for this interrupt
241da177e4SLinus Torvalds  *	to complete before returning. If you use this function while
251da177e4SLinus Torvalds  *	holding a resource the IRQ handler may need you will deadlock.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *	This function may be called - with care - from IRQ context.
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds void synchronize_irq(unsigned int irq)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	struct irq_desc *desc = irq_desc + irq;
321da177e4SLinus Torvalds 
33c2b5a251SMatthew Wilcox 	if (irq >= NR_IRQS)
34c2b5a251SMatthew Wilcox 		return;
35c2b5a251SMatthew Wilcox 
361da177e4SLinus Torvalds 	while (desc->status & IRQ_INPROGRESS)
371da177e4SLinus Torvalds 		cpu_relax();
381da177e4SLinus Torvalds }
391da177e4SLinus Torvalds EXPORT_SYMBOL(synchronize_irq);
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #endif
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /**
441da177e4SLinus Torvalds  *	disable_irq_nosync - disable an irq without waiting
451da177e4SLinus Torvalds  *	@irq: Interrupt to disable
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  *	Disable the selected interrupt line.  Disables and Enables are
481da177e4SLinus Torvalds  *	nested.
491da177e4SLinus Torvalds  *	Unlike disable_irq(), this function does not ensure existing
501da177e4SLinus Torvalds  *	instances of the IRQ handler have completed before returning.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  *	This function may be called from IRQ context.
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds void disable_irq_nosync(unsigned int irq)
551da177e4SLinus Torvalds {
5634ffdb72SIngo Molnar 	struct irq_desc *desc = irq_desc + irq;
571da177e4SLinus Torvalds 	unsigned long flags;
581da177e4SLinus Torvalds 
59c2b5a251SMatthew Wilcox 	if (irq >= NR_IRQS)
60c2b5a251SMatthew Wilcox 		return;
61c2b5a251SMatthew Wilcox 
621da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
631da177e4SLinus Torvalds 	if (!desc->depth++) {
641da177e4SLinus Torvalds 		desc->status |= IRQ_DISABLED;
65d1bef4edSIngo Molnar 		desc->chip->disable(irq);
661da177e4SLinus Torvalds 	}
671da177e4SLinus Torvalds 	spin_unlock_irqrestore(&desc->lock, flags);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync);
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /**
721da177e4SLinus Torvalds  *	disable_irq - disable an irq and wait for completion
731da177e4SLinus Torvalds  *	@irq: Interrupt to disable
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  *	Disable the selected interrupt line.  Enables and Disables are
761da177e4SLinus Torvalds  *	nested.
771da177e4SLinus Torvalds  *	This function waits for any pending IRQ handlers for this interrupt
781da177e4SLinus Torvalds  *	to complete before returning. If you use this function while
791da177e4SLinus Torvalds  *	holding a resource the IRQ handler may need you will deadlock.
801da177e4SLinus Torvalds  *
811da177e4SLinus Torvalds  *	This function may be called - with care - from IRQ context.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds void disable_irq(unsigned int irq)
841da177e4SLinus Torvalds {
8534ffdb72SIngo Molnar 	struct irq_desc *desc = irq_desc + irq;
861da177e4SLinus Torvalds 
87c2b5a251SMatthew Wilcox 	if (irq >= NR_IRQS)
88c2b5a251SMatthew Wilcox 		return;
89c2b5a251SMatthew Wilcox 
901da177e4SLinus Torvalds 	disable_irq_nosync(irq);
911da177e4SLinus Torvalds 	if (desc->action)
921da177e4SLinus Torvalds 		synchronize_irq(irq);
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /**
971da177e4SLinus Torvalds  *	enable_irq - enable handling of an irq
981da177e4SLinus Torvalds  *	@irq: Interrupt to enable
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  *	Undoes the effect of one call to disable_irq().  If this
1011da177e4SLinus Torvalds  *	matches the last disable, processing of interrupts on this
1021da177e4SLinus Torvalds  *	IRQ line is re-enabled.
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  *	This function may be called from IRQ context.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds void enable_irq(unsigned int irq)
1071da177e4SLinus Torvalds {
10834ffdb72SIngo Molnar 	struct irq_desc *desc = irq_desc + irq;
1091da177e4SLinus Torvalds 	unsigned long flags;
1101da177e4SLinus Torvalds 
111c2b5a251SMatthew Wilcox 	if (irq >= NR_IRQS)
112c2b5a251SMatthew Wilcox 		return;
113c2b5a251SMatthew Wilcox 
1141da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
1151da177e4SLinus Torvalds 	switch (desc->depth) {
1161da177e4SLinus Torvalds 	case 0:
117e8c4b9d0SBjorn Helgaas 		printk(KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
1181da177e4SLinus Torvalds 		WARN_ON(1);
1191da177e4SLinus Torvalds 		break;
1201da177e4SLinus Torvalds 	case 1: {
1213418d724SThomas Gleixner 		unsigned int status = desc->status & ~IRQ_DISABLED;
1223418d724SThomas Gleixner 
1233418d724SThomas Gleixner 		/* Prevent probing on this irq: */
1243418d724SThomas Gleixner 		desc->status = status | IRQ_NOPROBE;
125a4633adcSThomas Gleixner 		check_irq_resend(desc, irq);
1261da177e4SLinus Torvalds 		/* fall-through */
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 	default:
1291da177e4SLinus Torvalds 		desc->depth--;
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 	spin_unlock_irqrestore(&desc->lock, flags);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq);
1341da177e4SLinus Torvalds 
135ba9a2331SThomas Gleixner /**
136ba9a2331SThomas Gleixner  *	set_irq_wake - control irq power management wakeup
137ba9a2331SThomas Gleixner  *	@irq:	interrupt to control
138ba9a2331SThomas Gleixner  *	@on:	enable/disable power management wakeup
139ba9a2331SThomas Gleixner  *
14015a647ebSDavid Brownell  *	Enable/disable power management wakeup mode, which is
14115a647ebSDavid Brownell  *	disabled by default.  Enables and disables must match,
14215a647ebSDavid Brownell  *	just as they match for non-wakeup mode support.
14315a647ebSDavid Brownell  *
14415a647ebSDavid Brownell  *	Wakeup mode lets this IRQ wake the system from sleep
14515a647ebSDavid Brownell  *	states like "suspend to RAM".
146ba9a2331SThomas Gleixner  */
147ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on)
148ba9a2331SThomas Gleixner {
149ba9a2331SThomas Gleixner 	struct irq_desc *desc = irq_desc + irq;
150ba9a2331SThomas Gleixner 	unsigned long flags;
151ba9a2331SThomas Gleixner 	int ret = -ENXIO;
15215a647ebSDavid Brownell 	int (*set_wake)(unsigned, unsigned) = desc->chip->set_wake;
153ba9a2331SThomas Gleixner 
15415a647ebSDavid Brownell 	/* wakeup-capable irqs can be shared between drivers that
15515a647ebSDavid Brownell 	 * don't need to have the same sleep mode behaviors.
15615a647ebSDavid Brownell 	 */
157ba9a2331SThomas Gleixner 	spin_lock_irqsave(&desc->lock, flags);
15815a647ebSDavid Brownell 	if (on) {
15915a647ebSDavid Brownell 		if (desc->wake_depth++ == 0)
16015a647ebSDavid Brownell 			desc->status |= IRQ_WAKEUP;
16115a647ebSDavid Brownell 		else
16215a647ebSDavid Brownell 			set_wake = NULL;
16315a647ebSDavid Brownell 	} else {
16415a647ebSDavid Brownell 		if (desc->wake_depth == 0) {
16515a647ebSDavid Brownell 			printk(KERN_WARNING "Unbalanced IRQ %d "
16615a647ebSDavid Brownell 					"wake disable\n", irq);
16715a647ebSDavid Brownell 			WARN_ON(1);
16815a647ebSDavid Brownell 		} else if (--desc->wake_depth == 0)
16915a647ebSDavid Brownell 			desc->status &= ~IRQ_WAKEUP;
17015a647ebSDavid Brownell 		else
17115a647ebSDavid Brownell 			set_wake = NULL;
17215a647ebSDavid Brownell 	}
17315a647ebSDavid Brownell 	if (set_wake)
174ba9a2331SThomas Gleixner 		ret = desc->chip->set_wake(irq, on);
175ba9a2331SThomas Gleixner 	spin_unlock_irqrestore(&desc->lock, flags);
176ba9a2331SThomas Gleixner 	return ret;
177ba9a2331SThomas Gleixner }
178ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake);
179ba9a2331SThomas Gleixner 
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds  * Internal function that tells the architecture code whether a
1821da177e4SLinus Torvalds  * particular irq has been exclusively allocated or is available
1831da177e4SLinus Torvalds  * for driver use.
1841da177e4SLinus Torvalds  */
1851da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	struct irqaction *action;
1881da177e4SLinus Torvalds 
1896550c775SThomas Gleixner 	if (irq >= NR_IRQS || irq_desc[irq].status & IRQ_NOREQUEST)
1901da177e4SLinus Torvalds 		return 0;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	action = irq_desc[irq].action;
1931da177e4SLinus Torvalds 	if (action)
1943cca53b0SThomas Gleixner 		if (irqflags & action->flags & IRQF_SHARED)
1951da177e4SLinus Torvalds 			action = NULL;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	return !action;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2006a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc)
2016a6de9efSThomas Gleixner {
2026a6de9efSThomas Gleixner 	/*
2036a6de9efSThomas Gleixner 	 * If the architecture still has not overriden
2046a6de9efSThomas Gleixner 	 * the flow handler then zap the default. This
2056a6de9efSThomas Gleixner 	 * should catch incorrect flow-type setting.
2066a6de9efSThomas Gleixner 	 */
2076a6de9efSThomas Gleixner 	if (desc->handle_irq == &handle_bad_irq)
2086a6de9efSThomas Gleixner 		desc->handle_irq = NULL;
2096a6de9efSThomas Gleixner }
2106a6de9efSThomas Gleixner 
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds  * Internal function to register an irqaction - typically used to
2131da177e4SLinus Torvalds  * allocate special interrupts that are part of the architecture.
2141da177e4SLinus Torvalds  */
2151da177e4SLinus Torvalds int setup_irq(unsigned int irq, struct irqaction *new)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct irq_desc *desc = irq_desc + irq;
2181da177e4SLinus Torvalds 	struct irqaction *old, **p;
2191da177e4SLinus Torvalds 	unsigned long flags;
2201da177e4SLinus Torvalds 	int shared = 0;
2211da177e4SLinus Torvalds 
222c2b5a251SMatthew Wilcox 	if (irq >= NR_IRQS)
223c2b5a251SMatthew Wilcox 		return -EINVAL;
224c2b5a251SMatthew Wilcox 
225f1c2662cSIngo Molnar 	if (desc->chip == &no_irq_chip)
2261da177e4SLinus Torvalds 		return -ENOSYS;
2271da177e4SLinus Torvalds 	/*
2281da177e4SLinus Torvalds 	 * Some drivers like serial.c use request_irq() heavily,
2291da177e4SLinus Torvalds 	 * so we have to be careful not to interfere with a
2301da177e4SLinus Torvalds 	 * running system.
2311da177e4SLinus Torvalds 	 */
2323cca53b0SThomas Gleixner 	if (new->flags & IRQF_SAMPLE_RANDOM) {
2331da177e4SLinus Torvalds 		/*
2341da177e4SLinus Torvalds 		 * This function might sleep, we want to call it first,
2351da177e4SLinus Torvalds 		 * outside of the atomic block.
2361da177e4SLinus Torvalds 		 * Yes, this might clear the entropy pool if the wrong
2371da177e4SLinus Torvalds 		 * driver is attempted to be loaded, without actually
2381da177e4SLinus Torvalds 		 * installing a new handler, but is this really a problem,
2391da177e4SLinus Torvalds 		 * only the sysadmin is able to do this.
2401da177e4SLinus Torvalds 		 */
2411da177e4SLinus Torvalds 		rand_initialize_irq(irq);
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/*
2451da177e4SLinus Torvalds 	 * The following block of code has to be executed atomically
2461da177e4SLinus Torvalds 	 */
2471da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
2481da177e4SLinus Torvalds 	p = &desc->action;
24906fcb0c6SIngo Molnar 	old = *p;
25006fcb0c6SIngo Molnar 	if (old) {
251e76de9f8SThomas Gleixner 		/*
252e76de9f8SThomas Gleixner 		 * Can't share interrupts unless both agree to and are
253e76de9f8SThomas Gleixner 		 * the same type (level, edge, polarity). So both flag
2543cca53b0SThomas Gleixner 		 * fields must have IRQF_SHARED set and the bits which
255e76de9f8SThomas Gleixner 		 * set the trigger type must match.
256e76de9f8SThomas Gleixner 		 */
2573cca53b0SThomas Gleixner 		if (!((old->flags & new->flags) & IRQF_SHARED) ||
2583cca53b0SThomas Gleixner 		    ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK))
259f5163427SDimitri Sivanich 			goto mismatch;
260f5163427SDimitri Sivanich 
261284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU)
262f5163427SDimitri Sivanich 		/* All handlers must agree on per-cpuness */
2633cca53b0SThomas Gleixner 		if ((old->flags & IRQF_PERCPU) !=
2643cca53b0SThomas Gleixner 		    (new->flags & IRQF_PERCPU))
265f5163427SDimitri Sivanich 			goto mismatch;
266f5163427SDimitri Sivanich #endif
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 		/* add new interrupt at end of irq queue */
2691da177e4SLinus Torvalds 		do {
2701da177e4SLinus Torvalds 			p = &old->next;
2711da177e4SLinus Torvalds 			old = *p;
2721da177e4SLinus Torvalds 		} while (old);
2731da177e4SLinus Torvalds 		shared = 1;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	*p = new;
277284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU)
2783cca53b0SThomas Gleixner 	if (new->flags & IRQF_PERCPU)
279f5163427SDimitri Sivanich 		desc->status |= IRQ_PER_CPU;
280f5163427SDimitri Sivanich #endif
2811da177e4SLinus Torvalds 	if (!shared) {
2826a6de9efSThomas Gleixner 		irq_chip_set_defaults(desc->chip);
283e76de9f8SThomas Gleixner 
284e76de9f8SThomas Gleixner 		/* Setup the type (level, edge polarity) if configured: */
2853cca53b0SThomas Gleixner 		if (new->flags & IRQF_TRIGGER_MASK) {
286e76de9f8SThomas Gleixner 			if (desc->chip && desc->chip->set_type)
287e76de9f8SThomas Gleixner 				desc->chip->set_type(irq,
2883cca53b0SThomas Gleixner 						new->flags & IRQF_TRIGGER_MASK);
289e76de9f8SThomas Gleixner 			else
290e76de9f8SThomas Gleixner 				/*
2913cca53b0SThomas Gleixner 				 * IRQF_TRIGGER_* but the PIC does not support
292e76de9f8SThomas Gleixner 				 * multiple flow-types?
293e76de9f8SThomas Gleixner 				 */
2943cca53b0SThomas Gleixner 				printk(KERN_WARNING "No IRQF_TRIGGER set_type "
295e8c4b9d0SBjorn Helgaas 				       "function for IRQ %d (%s)\n", irq,
296e8c4b9d0SBjorn Helgaas 				       desc->chip ? desc->chip->name :
297e8c4b9d0SBjorn Helgaas 				       "unknown");
298e76de9f8SThomas Gleixner 		} else
2996a6de9efSThomas Gleixner 			compat_irq_chip_set_default_handler(desc);
3006a6de9efSThomas Gleixner 
30194d39e1fSThomas Gleixner 		desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
30294d39e1fSThomas Gleixner 				  IRQ_INPROGRESS);
30394d39e1fSThomas Gleixner 
30494d39e1fSThomas Gleixner 		if (!(desc->status & IRQ_NOAUTOEN)) {
3051da177e4SLinus Torvalds 			desc->depth = 0;
30694d39e1fSThomas Gleixner 			desc->status &= ~IRQ_DISABLED;
307d1bef4edSIngo Molnar 			if (desc->chip->startup)
308d1bef4edSIngo Molnar 				desc->chip->startup(irq);
3091da177e4SLinus Torvalds 			else
310d1bef4edSIngo Molnar 				desc->chip->enable(irq);
311e76de9f8SThomas Gleixner 		} else
312e76de9f8SThomas Gleixner 			/* Undo nested disables: */
313e76de9f8SThomas Gleixner 			desc->depth = 1;
31494d39e1fSThomas Gleixner 	}
3151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&desc->lock, flags);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	new->irq = irq;
3181da177e4SLinus Torvalds 	register_irq_proc(irq);
3191da177e4SLinus Torvalds 	new->dir = NULL;
3201da177e4SLinus Torvalds 	register_handler_proc(irq, new);
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	return 0;
323f5163427SDimitri Sivanich 
324f5163427SDimitri Sivanich mismatch:
325f5163427SDimitri Sivanich 	spin_unlock_irqrestore(&desc->lock, flags);
3263cca53b0SThomas Gleixner 	if (!(new->flags & IRQF_PROBE_SHARED)) {
327e8c4b9d0SBjorn Helgaas 		printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
328f5163427SDimitri Sivanich 		dump_stack();
32913e87ec6SAndrew Morton 	}
330f5163427SDimitri Sivanich 	return -EBUSY;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /**
3341da177e4SLinus Torvalds  *	free_irq - free an interrupt
3351da177e4SLinus Torvalds  *	@irq: Interrupt line to free
3361da177e4SLinus Torvalds  *	@dev_id: Device identity to free
3371da177e4SLinus Torvalds  *
3381da177e4SLinus Torvalds  *	Remove an interrupt handler. The handler is removed and if the
3391da177e4SLinus Torvalds  *	interrupt line is no longer in use by any driver it is disabled.
3401da177e4SLinus Torvalds  *	On a shared IRQ the caller must ensure the interrupt is disabled
3411da177e4SLinus Torvalds  *	on the card it drives before calling this function. The function
3421da177e4SLinus Torvalds  *	does not return until any executing interrupts for this IRQ
3431da177e4SLinus Torvalds  *	have completed.
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  *	This function must not be called from interrupt context.
3461da177e4SLinus Torvalds  */
3471da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct irq_desc *desc;
3501da177e4SLinus Torvalds 	struct irqaction **p;
3511da177e4SLinus Torvalds 	unsigned long flags;
3521da177e4SLinus Torvalds 
353cd7b24bbSIngo Molnar 	WARN_ON(in_interrupt());
3541da177e4SLinus Torvalds 	if (irq >= NR_IRQS)
3551da177e4SLinus Torvalds 		return;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	desc = irq_desc + irq;
3581da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
3591da177e4SLinus Torvalds 	p = &desc->action;
3601da177e4SLinus Torvalds 	for (;;) {
3611da177e4SLinus Torvalds 		struct irqaction *action = *p;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 		if (action) {
3641da177e4SLinus Torvalds 			struct irqaction **pp = p;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 			p = &action->next;
3671da177e4SLinus Torvalds 			if (action->dev_id != dev_id)
3681da177e4SLinus Torvalds 				continue;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 			/* Found it - now remove it from the list of entries */
3711da177e4SLinus Torvalds 			*pp = action->next;
372dbce706eSPaolo 'Blaisorblade' Giarrusso 
373b77d6adcSPaolo 'Blaisorblade' Giarrusso 			/* Currently used only by UML, might disappear one day.*/
374b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD
375d1bef4edSIngo Molnar 			if (desc->chip->release)
376d1bef4edSIngo Molnar 				desc->chip->release(irq, dev_id);
377b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif
378dbce706eSPaolo 'Blaisorblade' Giarrusso 
3791da177e4SLinus Torvalds 			if (!desc->action) {
3801da177e4SLinus Torvalds 				desc->status |= IRQ_DISABLED;
381d1bef4edSIngo Molnar 				if (desc->chip->shutdown)
382d1bef4edSIngo Molnar 					desc->chip->shutdown(irq);
3831da177e4SLinus Torvalds 				else
384d1bef4edSIngo Molnar 					desc->chip->disable(irq);
3851da177e4SLinus Torvalds 			}
3861da177e4SLinus Torvalds 			spin_unlock_irqrestore(&desc->lock, flags);
3871da177e4SLinus Torvalds 			unregister_handler_proc(irq, action);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 			/* Make sure it's not being used on another CPU */
3901da177e4SLinus Torvalds 			synchronize_irq(irq);
3911da177e4SLinus Torvalds 			kfree(action);
3921da177e4SLinus Torvalds 			return;
3931da177e4SLinus Torvalds 		}
394e8c4b9d0SBjorn Helgaas 		printk(KERN_ERR "Trying to free already-free IRQ %d\n", irq);
3951da177e4SLinus Torvalds 		spin_unlock_irqrestore(&desc->lock, flags);
3961da177e4SLinus Torvalds 		return;
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /**
4021da177e4SLinus Torvalds  *	request_irq - allocate an interrupt line
4031da177e4SLinus Torvalds  *	@irq: Interrupt line to allocate
4041da177e4SLinus Torvalds  *	@handler: Function to be called when the IRQ occurs
4051da177e4SLinus Torvalds  *	@irqflags: Interrupt type flags
4061da177e4SLinus Torvalds  *	@devname: An ascii name for the claiming device
4071da177e4SLinus Torvalds  *	@dev_id: A cookie passed back to the handler function
4081da177e4SLinus Torvalds  *
4091da177e4SLinus Torvalds  *	This call allocates interrupt resources and enables the
4101da177e4SLinus Torvalds  *	interrupt line and IRQ handling. From the point this
4111da177e4SLinus Torvalds  *	call is made your handler function may be invoked. Since
4121da177e4SLinus Torvalds  *	your handler function must clear any interrupt the board
4131da177e4SLinus Torvalds  *	raises, you must take care both to initialise your hardware
4141da177e4SLinus Torvalds  *	and to set up the interrupt handler in the right order.
4151da177e4SLinus Torvalds  *
4161da177e4SLinus Torvalds  *	Dev_id must be globally unique. Normally the address of the
4171da177e4SLinus Torvalds  *	device data structure is used as the cookie. Since the handler
4181da177e4SLinus Torvalds  *	receives this value it makes sense to use it.
4191da177e4SLinus Torvalds  *
4201da177e4SLinus Torvalds  *	If your interrupt is shared you must pass a non NULL dev_id
4211da177e4SLinus Torvalds  *	as this is required when freeing the interrupt.
4221da177e4SLinus Torvalds  *
4231da177e4SLinus Torvalds  *	Flags:
4241da177e4SLinus Torvalds  *
4253cca53b0SThomas Gleixner  *	IRQF_SHARED		Interrupt is shared
4263cca53b0SThomas Gleixner  *	IRQF_DISABLED	Disable local interrupts while processing
4273cca53b0SThomas Gleixner  *	IRQF_SAMPLE_RANDOM	The interrupt can be used for entropy
4281da177e4SLinus Torvalds  *
4291da177e4SLinus Torvalds  */
430*da482792SDavid Howells int request_irq(unsigned int irq, irq_handler_t handler,
4311da177e4SLinus Torvalds 		unsigned long irqflags, const char *devname, void *dev_id)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct irqaction *action;
4341da177e4SLinus Torvalds 	int retval;
4351da177e4SLinus Torvalds 
436fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
437fbb9ce95SIngo Molnar 	/*
438fbb9ce95SIngo Molnar 	 * Lockdep wants atomic interrupt handlers:
439fbb9ce95SIngo Molnar 	 */
440fbb9ce95SIngo Molnar 	irqflags |= SA_INTERRUPT;
441fbb9ce95SIngo Molnar #endif
4421da177e4SLinus Torvalds 	/*
4431da177e4SLinus Torvalds 	 * Sanity-check: shared interrupts must pass in a real dev-ID,
4441da177e4SLinus Torvalds 	 * otherwise we'll have trouble later trying to figure out
4451da177e4SLinus Torvalds 	 * which interrupt is which (messes up the interrupt freeing
4461da177e4SLinus Torvalds 	 * logic etc).
4471da177e4SLinus Torvalds 	 */
4483cca53b0SThomas Gleixner 	if ((irqflags & IRQF_SHARED) && !dev_id)
4491da177e4SLinus Torvalds 		return -EINVAL;
4501da177e4SLinus Torvalds 	if (irq >= NR_IRQS)
4511da177e4SLinus Torvalds 		return -EINVAL;
4526550c775SThomas Gleixner 	if (irq_desc[irq].status & IRQ_NOREQUEST)
4536550c775SThomas Gleixner 		return -EINVAL;
4541da177e4SLinus Torvalds 	if (!handler)
4551da177e4SLinus Torvalds 		return -EINVAL;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
4581da177e4SLinus Torvalds 	if (!action)
4591da177e4SLinus Torvalds 		return -ENOMEM;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	action->handler = handler;
4621da177e4SLinus Torvalds 	action->flags = irqflags;
4631da177e4SLinus Torvalds 	cpus_clear(action->mask);
4641da177e4SLinus Torvalds 	action->name = devname;
4651da177e4SLinus Torvalds 	action->next = NULL;
4661da177e4SLinus Torvalds 	action->dev_id = dev_id;
4671da177e4SLinus Torvalds 
468eee45269SIvan Kokshaysky 	select_smp_affinity(irq);
469eee45269SIvan Kokshaysky 
4701da177e4SLinus Torvalds 	retval = setup_irq(irq, action);
4711da177e4SLinus Torvalds 	if (retval)
4721da177e4SLinus Torvalds 		kfree(action);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	return retval;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds EXPORT_SYMBOL(request_irq);
477