1/*
2 * linux/kernel/irq/manage.c
3 *
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006 Thomas Gleixner
6 *
7 * This file contains driver APIs to the irq subsystem.
8 */
9
10#include <linux/irq.h>
11#include <linux/module.h>
12#include <linux/random.h>
13#include <linux/interrupt.h>
14
15#include "internals.h"
16
17#ifdef CONFIG_SMP
18
19/**
20 *	synchronize_irq - wait for pending IRQ handlers (on other CPUs)
21 *	@irq: interrupt number to wait for
22 *
23 *	This function waits for any pending IRQ handlers for this interrupt
24 *	to complete before returning. If you use this function while
25 *	holding a resource the IRQ handler may need you will deadlock.
26 *
27 *	This function may be called - with care - from IRQ context.
28 */
29void synchronize_irq(unsigned int irq)
30{
31	struct irq_desc *desc = irq_desc + irq;
32
33	if (irq >= NR_IRQS)
34		return;
35
36	while (desc->status & IRQ_INPROGRESS)
37		cpu_relax();
38}
39EXPORT_SYMBOL(synchronize_irq);
40
41/**
42 *	irq_can_set_affinity - Check if the affinity of a given irq can be set
43 *	@irq:		Interrupt to check
44 *
45 */
46int irq_can_set_affinity(unsigned int irq)
47{
48	struct irq_desc *desc = irq_desc + irq;
49
50	if (CHECK_IRQ_PER_CPU(desc->status) || !desc->chip ||
51	    !desc->chip->set_affinity)
52		return 0;
53
54	return 1;
55}
56
57/**
58 *	irq_set_affinity - Set the irq affinity of a given irq
59 *	@irq:		Interrupt to set affinity
60 *	@cpumask:	cpumask
61 *
62 */
63int irq_set_affinity(unsigned int irq, cpumask_t cpumask)
64{
65	struct irq_desc *desc = irq_desc + irq;
66
67	if (!desc->chip->set_affinity)
68		return -EINVAL;
69
70	set_balance_irq_affinity(irq, cpumask);
71
72#ifdef CONFIG_GENERIC_PENDING_IRQ
73	set_pending_irq(irq, cpumask);
74#else
75	desc->affinity = cpumask;
76	desc->chip->set_affinity(irq, cpumask);
77#endif
78	return 0;
79}
80
81#endif
82
83/**
84 *	disable_irq_nosync - disable an irq without waiting
85 *	@irq: Interrupt to disable
86 *
87 *	Disable the selected interrupt line.  Disables and Enables are
88 *	nested.
89 *	Unlike disable_irq(), this function does not ensure existing
90 *	instances of the IRQ handler have completed before returning.
91 *
92 *	This function may be called from IRQ context.
93 */
94void disable_irq_nosync(unsigned int irq)
95{
96	struct irq_desc *desc = irq_desc + irq;
97	unsigned long flags;
98
99	if (irq >= NR_IRQS)
100		return;
101
102	spin_lock_irqsave(&desc->lock, flags);
103	if (!desc->depth++) {
104		desc->status |= IRQ_DISABLED;
105		desc->chip->disable(irq);
106	}
107	spin_unlock_irqrestore(&desc->lock, flags);
108}
109EXPORT_SYMBOL(disable_irq_nosync);
110
111/**
112 *	disable_irq - disable an irq and wait for completion
113 *	@irq: Interrupt to disable
114 *
115 *	Disable the selected interrupt line.  Enables and Disables are
116 *	nested.
117 *	This function waits for any pending IRQ handlers for this interrupt
118 *	to complete before returning. If you use this function while
119 *	holding a resource the IRQ handler may need you will deadlock.
120 *
121 *	This function may be called - with care - from IRQ context.
122 */
123void disable_irq(unsigned int irq)
124{
125	struct irq_desc *desc = irq_desc + irq;
126
127	if (irq >= NR_IRQS)
128		return;
129
130	disable_irq_nosync(irq);
131	if (desc->action)
132		synchronize_irq(irq);
133}
134EXPORT_SYMBOL(disable_irq);
135
136/**
137 *	enable_irq - enable handling of an irq
138 *	@irq: Interrupt to enable
139 *
140 *	Undoes the effect of one call to disable_irq().  If this
141 *	matches the last disable, processing of interrupts on this
142 *	IRQ line is re-enabled.
143 *
144 *	This function may be called from IRQ context.
145 */
146void enable_irq(unsigned int irq)
147{
148	struct irq_desc *desc = irq_desc + irq;
149	unsigned long flags;
150
151	if (irq >= NR_IRQS)
152		return;
153
154	spin_lock_irqsave(&desc->lock, flags);
155	switch (desc->depth) {
156	case 0:
157		printk(KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
158		WARN_ON(1);
159		break;
160	case 1: {
161		unsigned int status = desc->status & ~IRQ_DISABLED;
162
163		/* Prevent probing on this irq: */
164		desc->status = status | IRQ_NOPROBE;
165		check_irq_resend(desc, irq);
166		/* fall-through */
167	}
168	default:
169		desc->depth--;
170	}
171	spin_unlock_irqrestore(&desc->lock, flags);
172}
173EXPORT_SYMBOL(enable_irq);
174
175/**
176 *	set_irq_wake - control irq power management wakeup
177 *	@irq:	interrupt to control
178 *	@on:	enable/disable power management wakeup
179 *
180 *	Enable/disable power management wakeup mode, which is
181 *	disabled by default.  Enables and disables must match,
182 *	just as they match for non-wakeup mode support.
183 *
184 *	Wakeup mode lets this IRQ wake the system from sleep
185 *	states like "suspend to RAM".
186 */
187int set_irq_wake(unsigned int irq, unsigned int on)
188{
189	struct irq_desc *desc = irq_desc + irq;
190	unsigned long flags;
191	int ret = -ENXIO;
192	int (*set_wake)(unsigned, unsigned) = desc->chip->set_wake;
193
194	/* wakeup-capable irqs can be shared between drivers that
195	 * don't need to have the same sleep mode behaviors.
196	 */
197	spin_lock_irqsave(&desc->lock, flags);
198	if (on) {
199		if (desc->wake_depth++ == 0)
200			desc->status |= IRQ_WAKEUP;
201		else
202			set_wake = NULL;
203	} else {
204		if (desc->wake_depth == 0) {
205			printk(KERN_WARNING "Unbalanced IRQ %d "
206					"wake disable\n", irq);
207			WARN_ON(1);
208		} else if (--desc->wake_depth == 0)
209			desc->status &= ~IRQ_WAKEUP;
210		else
211			set_wake = NULL;
212	}
213	if (set_wake)
214		ret = desc->chip->set_wake(irq, on);
215	spin_unlock_irqrestore(&desc->lock, flags);
216	return ret;
217}
218EXPORT_SYMBOL(set_irq_wake);
219
220/*
221 * Internal function that tells the architecture code whether a
222 * particular irq has been exclusively allocated or is available
223 * for driver use.
224 */
225int can_request_irq(unsigned int irq, unsigned long irqflags)
226{
227	struct irqaction *action;
228
229	if (irq >= NR_IRQS || irq_desc[irq].status & IRQ_NOREQUEST)
230		return 0;
231
232	action = irq_desc[irq].action;
233	if (action)
234		if (irqflags & action->flags & IRQF_SHARED)
235			action = NULL;
236
237	return !action;
238}
239
240void compat_irq_chip_set_default_handler(struct irq_desc *desc)
241{
242	/*
243	 * If the architecture still has not overriden
244	 * the flow handler then zap the default. This
245	 * should catch incorrect flow-type setting.
246	 */
247	if (desc->handle_irq == &handle_bad_irq)
248		desc->handle_irq = NULL;
249}
250
251/*
252 * Internal function to register an irqaction - typically used to
253 * allocate special interrupts that are part of the architecture.
254 */
255int setup_irq(unsigned int irq, struct irqaction *new)
256{
257	struct irq_desc *desc = irq_desc + irq;
258	struct irqaction *old, **p;
259	const char *old_name = NULL;
260	unsigned long flags;
261	int shared = 0;
262
263	if (irq >= NR_IRQS)
264		return -EINVAL;
265
266	if (desc->chip == &no_irq_chip)
267		return -ENOSYS;
268	/*
269	 * Some drivers like serial.c use request_irq() heavily,
270	 * so we have to be careful not to interfere with a
271	 * running system.
272	 */
273	if (new->flags & IRQF_SAMPLE_RANDOM) {
274		/*
275		 * This function might sleep, we want to call it first,
276		 * outside of the atomic block.
277		 * Yes, this might clear the entropy pool if the wrong
278		 * driver is attempted to be loaded, without actually
279		 * installing a new handler, but is this really a problem,
280		 * only the sysadmin is able to do this.
281		 */
282		rand_initialize_irq(irq);
283	}
284
285	/*
286	 * The following block of code has to be executed atomically
287	 */
288	spin_lock_irqsave(&desc->lock, flags);
289	p = &desc->action;
290	old = *p;
291	if (old) {
292		/*
293		 * Can't share interrupts unless both agree to and are
294		 * the same type (level, edge, polarity). So both flag
295		 * fields must have IRQF_SHARED set and the bits which
296		 * set the trigger type must match.
297		 */
298		if (!((old->flags & new->flags) & IRQF_SHARED) ||
299		    ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
300			old_name = old->name;
301			goto mismatch;
302		}
303
304#if defined(CONFIG_IRQ_PER_CPU)
305		/* All handlers must agree on per-cpuness */
306		if ((old->flags & IRQF_PERCPU) !=
307		    (new->flags & IRQF_PERCPU))
308			goto mismatch;
309#endif
310
311		/* add new interrupt at end of irq queue */
312		do {
313			p = &old->next;
314			old = *p;
315		} while (old);
316		shared = 1;
317	}
318
319	*p = new;
320
321	/* Exclude IRQ from balancing */
322	if (new->flags & IRQF_NOBALANCING)
323		desc->status |= IRQ_NO_BALANCING;
324
325	if (!shared) {
326		irq_chip_set_defaults(desc->chip);
327
328#if defined(CONFIG_IRQ_PER_CPU)
329		if (new->flags & IRQF_PERCPU)
330			desc->status |= IRQ_PER_CPU;
331#endif
332
333		/* Setup the type (level, edge polarity) if configured: */
334		if (new->flags & IRQF_TRIGGER_MASK) {
335			if (desc->chip && desc->chip->set_type)
336				desc->chip->set_type(irq,
337						new->flags & IRQF_TRIGGER_MASK);
338			else
339				/*
340				 * IRQF_TRIGGER_* but the PIC does not support
341				 * multiple flow-types?
342				 */
343				printk(KERN_WARNING "No IRQF_TRIGGER set_type "
344				       "function for IRQ %d (%s)\n", irq,
345				       desc->chip ? desc->chip->name :
346				       "unknown");
347		} else
348			compat_irq_chip_set_default_handler(desc);
349
350		desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
351				  IRQ_INPROGRESS);
352
353		if (!(desc->status & IRQ_NOAUTOEN)) {
354			desc->depth = 0;
355			desc->status &= ~IRQ_DISABLED;
356			if (desc->chip->startup)
357				desc->chip->startup(irq);
358			else
359				desc->chip->enable(irq);
360		} else
361			/* Undo nested disables: */
362			desc->depth = 1;
363	}
364	/* Reset broken irq detection when installing new handler */
365	desc->irq_count = 0;
366	desc->irqs_unhandled = 0;
367	spin_unlock_irqrestore(&desc->lock, flags);
368
369	new->irq = irq;
370	register_irq_proc(irq);
371	new->dir = NULL;
372	register_handler_proc(irq, new);
373
374	return 0;
375
376mismatch:
377#ifdef CONFIG_DEBUG_SHIRQ
378	if (!(new->flags & IRQF_PROBE_SHARED)) {
379		printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
380		if (old_name)
381			printk(KERN_ERR "current handler: %s\n", old_name);
382		dump_stack();
383	}
384#endif
385	spin_unlock_irqrestore(&desc->lock, flags);
386	return -EBUSY;
387}
388
389/**
390 *	free_irq - free an interrupt
391 *	@irq: Interrupt line to free
392 *	@dev_id: Device identity to free
393 *
394 *	Remove an interrupt handler. The handler is removed and if the
395 *	interrupt line is no longer in use by any driver it is disabled.
396 *	On a shared IRQ the caller must ensure the interrupt is disabled
397 *	on the card it drives before calling this function. The function
398 *	does not return until any executing interrupts for this IRQ
399 *	have completed.
400 *
401 *	This function must not be called from interrupt context.
402 */
403void free_irq(unsigned int irq, void *dev_id)
404{
405	struct irq_desc *desc;
406	struct irqaction **p;
407	unsigned long flags;
408	irqreturn_t (*handler)(int, void *) = NULL;
409
410	WARN_ON(in_interrupt());
411	if (irq >= NR_IRQS)
412		return;
413
414	desc = irq_desc + irq;
415	spin_lock_irqsave(&desc->lock, flags);
416	p = &desc->action;
417	for (;;) {
418		struct irqaction *action = *p;
419
420		if (action) {
421			struct irqaction **pp = p;
422
423			p = &action->next;
424			if (action->dev_id != dev_id)
425				continue;
426
427			/* Found it - now remove it from the list of entries */
428			*pp = action->next;
429
430			/* Currently used only by UML, might disappear one day.*/
431#ifdef CONFIG_IRQ_RELEASE_METHOD
432			if (desc->chip->release)
433				desc->chip->release(irq, dev_id);
434#endif
435
436			if (!desc->action) {
437				desc->status |= IRQ_DISABLED;
438				if (desc->chip->shutdown)
439					desc->chip->shutdown(irq);
440				else
441					desc->chip->disable(irq);
442			}
443			spin_unlock_irqrestore(&desc->lock, flags);
444			unregister_handler_proc(irq, action);
445
446			/* Make sure it's not being used on another CPU */
447			synchronize_irq(irq);
448			if (action->flags & IRQF_SHARED)
449				handler = action->handler;
450			kfree(action);
451			return;
452		}
453		printk(KERN_ERR "Trying to free already-free IRQ %d\n", irq);
454		spin_unlock_irqrestore(&desc->lock, flags);
455		return;
456	}
457#ifdef CONFIG_DEBUG_SHIRQ
458	if (handler) {
459		/*
460		 * It's a shared IRQ -- the driver ought to be prepared for it
461		 * to happen even now it's being freed, so let's make sure....
462		 * We do this after actually deregistering it, to make sure that
463		 * a 'real' IRQ doesn't run in parallel with our fake
464		 */
465		handler(irq, dev_id);
466	}
467#endif
468}
469EXPORT_SYMBOL(free_irq);
470
471/**
472 *	request_irq - allocate an interrupt line
473 *	@irq: Interrupt line to allocate
474 *	@handler: Function to be called when the IRQ occurs
475 *	@irqflags: Interrupt type flags
476 *	@devname: An ascii name for the claiming device
477 *	@dev_id: A cookie passed back to the handler function
478 *
479 *	This call allocates interrupt resources and enables the
480 *	interrupt line and IRQ handling. From the point this
481 *	call is made your handler function may be invoked. Since
482 *	your handler function must clear any interrupt the board
483 *	raises, you must take care both to initialise your hardware
484 *	and to set up the interrupt handler in the right order.
485 *
486 *	Dev_id must be globally unique. Normally the address of the
487 *	device data structure is used as the cookie. Since the handler
488 *	receives this value it makes sense to use it.
489 *
490 *	If your interrupt is shared you must pass a non NULL dev_id
491 *	as this is required when freeing the interrupt.
492 *
493 *	Flags:
494 *
495 *	IRQF_SHARED		Interrupt is shared
496 *	IRQF_DISABLED	Disable local interrupts while processing
497 *	IRQF_SAMPLE_RANDOM	The interrupt can be used for entropy
498 *
499 */
500int request_irq(unsigned int irq, irq_handler_t handler,
501		unsigned long irqflags, const char *devname, void *dev_id)
502{
503	struct irqaction *action;
504	int retval;
505
506#ifdef CONFIG_LOCKDEP
507	/*
508	 * Lockdep wants atomic interrupt handlers:
509	 */
510	irqflags |= IRQF_DISABLED;
511#endif
512	/*
513	 * Sanity-check: shared interrupts must pass in a real dev-ID,
514	 * otherwise we'll have trouble later trying to figure out
515	 * which interrupt is which (messes up the interrupt freeing
516	 * logic etc).
517	 */
518	if ((irqflags & IRQF_SHARED) && !dev_id)
519		return -EINVAL;
520	if (irq >= NR_IRQS)
521		return -EINVAL;
522	if (irq_desc[irq].status & IRQ_NOREQUEST)
523		return -EINVAL;
524	if (!handler)
525		return -EINVAL;
526
527	action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
528	if (!action)
529		return -ENOMEM;
530
531	action->handler = handler;
532	action->flags = irqflags;
533	cpus_clear(action->mask);
534	action->name = devname;
535	action->next = NULL;
536	action->dev_id = dev_id;
537
538	select_smp_affinity(irq);
539
540#ifdef CONFIG_DEBUG_SHIRQ
541	if (irqflags & IRQF_SHARED) {
542		/*
543		 * It's a shared IRQ -- the driver ought to be prepared for it
544		 * to happen immediately, so let's make sure....
545		 * We do this before actually registering it, to make sure that
546		 * a 'real' IRQ doesn't run in parallel with our fake
547		 */
548		if (irqflags & IRQF_DISABLED) {
549			unsigned long flags;
550
551			local_irq_save(flags);
552			handler(irq, dev_id);
553			local_irq_restore(flags);
554		} else
555			handler(irq, dev_id);
556	}
557#endif
558
559	retval = setup_irq(irq, action);
560	if (retval)
561		kfree(action);
562
563	return retval;
564}
565EXPORT_SYMBOL(request_irq);
566