xref: /openbmc/linux/kernel/irq/manage.c (revision b25c340c195447afb1860da580fe2a85a6b652c5)
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>
113aa551c9SThomas Gleixner #include <linux/kthread.h>
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/random.h>
141da177e4SLinus Torvalds #include <linux/interrupt.h>
151aeb272cSRobert P. J. Day #include <linux/slab.h>
163aa551c9SThomas Gleixner #include <linux/sched.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include "internals.h"
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /**
211da177e4SLinus Torvalds  *	synchronize_irq - wait for pending IRQ handlers (on other CPUs)
221e5d5331SRandy Dunlap  *	@irq: interrupt number to wait for
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *	This function waits for any pending IRQ handlers for this interrupt
251da177e4SLinus Torvalds  *	to complete before returning. If you use this function while
261da177e4SLinus Torvalds  *	holding a resource the IRQ handler may need you will deadlock.
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  *	This function may be called - with care - from IRQ context.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds void synchronize_irq(unsigned int irq)
311da177e4SLinus Torvalds {
32cb5bc832SYinghai Lu 	struct irq_desc *desc = irq_to_desc(irq);
33a98ce5c6SHerbert Xu 	unsigned int status;
341da177e4SLinus Torvalds 
357d94f7caSYinghai Lu 	if (!desc)
36c2b5a251SMatthew Wilcox 		return;
37c2b5a251SMatthew Wilcox 
38a98ce5c6SHerbert Xu 	do {
39a98ce5c6SHerbert Xu 		unsigned long flags;
40a98ce5c6SHerbert Xu 
41a98ce5c6SHerbert Xu 		/*
42a98ce5c6SHerbert Xu 		 * Wait until we're out of the critical section.  This might
43a98ce5c6SHerbert Xu 		 * give the wrong answer due to the lack of memory barriers.
44a98ce5c6SHerbert Xu 		 */
451da177e4SLinus Torvalds 		while (desc->status & IRQ_INPROGRESS)
461da177e4SLinus Torvalds 			cpu_relax();
47a98ce5c6SHerbert Xu 
48a98ce5c6SHerbert Xu 		/* Ok, that indicated we're done: double-check carefully. */
49a98ce5c6SHerbert Xu 		spin_lock_irqsave(&desc->lock, flags);
50a98ce5c6SHerbert Xu 		status = desc->status;
51a98ce5c6SHerbert Xu 		spin_unlock_irqrestore(&desc->lock, flags);
52a98ce5c6SHerbert Xu 
53a98ce5c6SHerbert Xu 		/* Oops, that failed? */
54a98ce5c6SHerbert Xu 	} while (status & IRQ_INPROGRESS);
553aa551c9SThomas Gleixner 
563aa551c9SThomas Gleixner 	/*
573aa551c9SThomas Gleixner 	 * We made sure that no hardirq handler is running. Now verify
583aa551c9SThomas Gleixner 	 * that no threaded handlers are active.
593aa551c9SThomas Gleixner 	 */
603aa551c9SThomas Gleixner 	wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds EXPORT_SYMBOL(synchronize_irq);
631da177e4SLinus Torvalds 
643aa551c9SThomas Gleixner #ifdef CONFIG_SMP
653aa551c9SThomas Gleixner cpumask_var_t irq_default_affinity;
663aa551c9SThomas Gleixner 
67771ee3b0SThomas Gleixner /**
68771ee3b0SThomas Gleixner  *	irq_can_set_affinity - Check if the affinity of a given irq can be set
69771ee3b0SThomas Gleixner  *	@irq:		Interrupt to check
70771ee3b0SThomas Gleixner  *
71771ee3b0SThomas Gleixner  */
72771ee3b0SThomas Gleixner int irq_can_set_affinity(unsigned int irq)
73771ee3b0SThomas Gleixner {
7408678b08SYinghai Lu 	struct irq_desc *desc = irq_to_desc(irq);
75771ee3b0SThomas Gleixner 
76771ee3b0SThomas Gleixner 	if (CHECK_IRQ_PER_CPU(desc->status) || !desc->chip ||
77771ee3b0SThomas Gleixner 	    !desc->chip->set_affinity)
78771ee3b0SThomas Gleixner 		return 0;
79771ee3b0SThomas Gleixner 
80771ee3b0SThomas Gleixner 	return 1;
81771ee3b0SThomas Gleixner }
82771ee3b0SThomas Gleixner 
83591d2fb0SThomas Gleixner /**
84591d2fb0SThomas Gleixner  *	irq_set_thread_affinity - Notify irq threads to adjust affinity
85591d2fb0SThomas Gleixner  *	@desc:		irq descriptor which has affitnity changed
86591d2fb0SThomas Gleixner  *
87591d2fb0SThomas Gleixner  *	We just set IRQTF_AFFINITY and delegate the affinity setting
88591d2fb0SThomas Gleixner  *	to the interrupt thread itself. We can not call
89591d2fb0SThomas Gleixner  *	set_cpus_allowed_ptr() here as we hold desc->lock and this
90591d2fb0SThomas Gleixner  *	code can be called from hard interrupt context.
91591d2fb0SThomas Gleixner  */
92591d2fb0SThomas Gleixner void irq_set_thread_affinity(struct irq_desc *desc)
933aa551c9SThomas Gleixner {
943aa551c9SThomas Gleixner 	struct irqaction *action = desc->action;
953aa551c9SThomas Gleixner 
963aa551c9SThomas Gleixner 	while (action) {
973aa551c9SThomas Gleixner 		if (action->thread)
98591d2fb0SThomas Gleixner 			set_bit(IRQTF_AFFINITY, &action->thread_flags);
993aa551c9SThomas Gleixner 		action = action->next;
1003aa551c9SThomas Gleixner 	}
1013aa551c9SThomas Gleixner }
1023aa551c9SThomas Gleixner 
103771ee3b0SThomas Gleixner /**
104771ee3b0SThomas Gleixner  *	irq_set_affinity - Set the irq affinity of a given irq
105771ee3b0SThomas Gleixner  *	@irq:		Interrupt to set affinity
106771ee3b0SThomas Gleixner  *	@cpumask:	cpumask
107771ee3b0SThomas Gleixner  *
108771ee3b0SThomas Gleixner  */
1090de26520SRusty Russell int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
110771ee3b0SThomas Gleixner {
11108678b08SYinghai Lu 	struct irq_desc *desc = irq_to_desc(irq);
112f6d87f4bSThomas Gleixner 	unsigned long flags;
113771ee3b0SThomas Gleixner 
114771ee3b0SThomas Gleixner 	if (!desc->chip->set_affinity)
115771ee3b0SThomas Gleixner 		return -EINVAL;
116771ee3b0SThomas Gleixner 
117f6d87f4bSThomas Gleixner 	spin_lock_irqsave(&desc->lock, flags);
118f6d87f4bSThomas Gleixner 
119771ee3b0SThomas Gleixner #ifdef CONFIG_GENERIC_PENDING_IRQ
12057b150ccSYinghai Lu 	if (desc->status & IRQ_MOVE_PCNTXT) {
12157b150ccSYinghai Lu 		if (!desc->chip->set_affinity(irq, cpumask)) {
12257b150ccSYinghai Lu 			cpumask_copy(desc->affinity, cpumask);
123591d2fb0SThomas Gleixner 			irq_set_thread_affinity(desc);
12457b150ccSYinghai Lu 		}
12557b150ccSYinghai Lu 	}
1266ec3cfecSPallipadi, Venkatesh 	else {
127f6d87f4bSThomas Gleixner 		desc->status |= IRQ_MOVE_PENDING;
1287f7ace0cSMike Travis 		cpumask_copy(desc->pending_mask, cpumask);
129f6d87f4bSThomas Gleixner 	}
130771ee3b0SThomas Gleixner #else
13157b150ccSYinghai Lu 	if (!desc->chip->set_affinity(irq, cpumask)) {
1327f7ace0cSMike Travis 		cpumask_copy(desc->affinity, cpumask);
133591d2fb0SThomas Gleixner 		irq_set_thread_affinity(desc);
13457b150ccSYinghai Lu 	}
13557b150ccSYinghai Lu #endif
136f6d87f4bSThomas Gleixner 	desc->status |= IRQ_AFFINITY_SET;
137f6d87f4bSThomas Gleixner 	spin_unlock_irqrestore(&desc->lock, flags);
138771ee3b0SThomas Gleixner 	return 0;
139771ee3b0SThomas Gleixner }
140771ee3b0SThomas Gleixner 
14118404756SMax Krasnyansky #ifndef CONFIG_AUTO_IRQ_AFFINITY
14218404756SMax Krasnyansky /*
14318404756SMax Krasnyansky  * Generic version of the affinity autoselector.
14418404756SMax Krasnyansky  */
145548c8933SHannes Eder static int setup_affinity(unsigned int irq, struct irq_desc *desc)
14618404756SMax Krasnyansky {
14718404756SMax Krasnyansky 	if (!irq_can_set_affinity(irq))
14818404756SMax Krasnyansky 		return 0;
14918404756SMax Krasnyansky 
150f6d87f4bSThomas Gleixner 	/*
151f6d87f4bSThomas Gleixner 	 * Preserve an userspace affinity setup, but make sure that
152f6d87f4bSThomas Gleixner 	 * one of the targets is online.
153f6d87f4bSThomas Gleixner 	 */
154612e3684SThomas Gleixner 	if (desc->status & (IRQ_AFFINITY_SET | IRQ_NO_BALANCING)) {
1557f7ace0cSMike Travis 		if (cpumask_any_and(desc->affinity, cpu_online_mask)
1560de26520SRusty Russell 		    < nr_cpu_ids)
1570de26520SRusty Russell 			goto set_affinity;
158f6d87f4bSThomas Gleixner 		else
159f6d87f4bSThomas Gleixner 			desc->status &= ~IRQ_AFFINITY_SET;
160f6d87f4bSThomas Gleixner 	}
161f6d87f4bSThomas Gleixner 
1627f7ace0cSMike Travis 	cpumask_and(desc->affinity, cpu_online_mask, irq_default_affinity);
1630de26520SRusty Russell set_affinity:
1647f7ace0cSMike Travis 	desc->chip->set_affinity(irq, desc->affinity);
16518404756SMax Krasnyansky 
16618404756SMax Krasnyansky 	return 0;
16718404756SMax Krasnyansky }
168f6d87f4bSThomas Gleixner #else
169548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *d)
170f6d87f4bSThomas Gleixner {
171f6d87f4bSThomas Gleixner 	return irq_select_affinity(irq);
172f6d87f4bSThomas Gleixner }
17318404756SMax Krasnyansky #endif
17418404756SMax Krasnyansky 
175f6d87f4bSThomas Gleixner /*
176f6d87f4bSThomas Gleixner  * Called when affinity is set via /proc/irq
177f6d87f4bSThomas Gleixner  */
178f6d87f4bSThomas Gleixner int irq_select_affinity_usr(unsigned int irq)
179f6d87f4bSThomas Gleixner {
180f6d87f4bSThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
181f6d87f4bSThomas Gleixner 	unsigned long flags;
182f6d87f4bSThomas Gleixner 	int ret;
183f6d87f4bSThomas Gleixner 
184f6d87f4bSThomas Gleixner 	spin_lock_irqsave(&desc->lock, flags);
185548c8933SHannes Eder 	ret = setup_affinity(irq, desc);
1863aa551c9SThomas Gleixner 	if (!ret)
187591d2fb0SThomas Gleixner 		irq_set_thread_affinity(desc);
188f6d87f4bSThomas Gleixner 	spin_unlock_irqrestore(&desc->lock, flags);
189f6d87f4bSThomas Gleixner 
190f6d87f4bSThomas Gleixner 	return ret;
191f6d87f4bSThomas Gleixner }
192f6d87f4bSThomas Gleixner 
193f6d87f4bSThomas Gleixner #else
194548c8933SHannes Eder static inline int setup_affinity(unsigned int irq, struct irq_desc *desc)
195f6d87f4bSThomas Gleixner {
196f6d87f4bSThomas Gleixner 	return 0;
197f6d87f4bSThomas Gleixner }
1981da177e4SLinus Torvalds #endif
1991da177e4SLinus Torvalds 
2000a0c5168SRafael J. Wysocki void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
2010a0c5168SRafael J. Wysocki {
2020a0c5168SRafael J. Wysocki 	if (suspend) {
2030a0c5168SRafael J. Wysocki 		if (!desc->action || (desc->action->flags & IRQF_TIMER))
2040a0c5168SRafael J. Wysocki 			return;
2050a0c5168SRafael J. Wysocki 		desc->status |= IRQ_SUSPENDED;
2060a0c5168SRafael J. Wysocki 	}
2070a0c5168SRafael J. Wysocki 
2080a0c5168SRafael J. Wysocki 	if (!desc->depth++) {
2090a0c5168SRafael J. Wysocki 		desc->status |= IRQ_DISABLED;
2100a0c5168SRafael J. Wysocki 		desc->chip->disable(irq);
2110a0c5168SRafael J. Wysocki 	}
2120a0c5168SRafael J. Wysocki }
2130a0c5168SRafael J. Wysocki 
2141da177e4SLinus Torvalds /**
2151da177e4SLinus Torvalds  *	disable_irq_nosync - disable an irq without waiting
2161da177e4SLinus Torvalds  *	@irq: Interrupt to disable
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  *	Disable the selected interrupt line.  Disables and Enables are
2191da177e4SLinus Torvalds  *	nested.
2201da177e4SLinus Torvalds  *	Unlike disable_irq(), this function does not ensure existing
2211da177e4SLinus Torvalds  *	instances of the IRQ handler have completed before returning.
2221da177e4SLinus Torvalds  *
2231da177e4SLinus Torvalds  *	This function may be called from IRQ context.
2241da177e4SLinus Torvalds  */
2251da177e4SLinus Torvalds void disable_irq_nosync(unsigned int irq)
2261da177e4SLinus Torvalds {
227d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
2281da177e4SLinus Torvalds 	unsigned long flags;
2291da177e4SLinus Torvalds 
2307d94f7caSYinghai Lu 	if (!desc)
231c2b5a251SMatthew Wilcox 		return;
232c2b5a251SMatthew Wilcox 
2331da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
2340a0c5168SRafael J. Wysocki 	__disable_irq(desc, irq, false);
2351da177e4SLinus Torvalds 	spin_unlock_irqrestore(&desc->lock, flags);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**
2401da177e4SLinus Torvalds  *	disable_irq - disable an irq and wait for completion
2411da177e4SLinus Torvalds  *	@irq: Interrupt to disable
2421da177e4SLinus Torvalds  *
2431da177e4SLinus Torvalds  *	Disable the selected interrupt line.  Enables and Disables are
2441da177e4SLinus Torvalds  *	nested.
2451da177e4SLinus Torvalds  *	This function waits for any pending IRQ handlers for this interrupt
2461da177e4SLinus Torvalds  *	to complete before returning. If you use this function while
2471da177e4SLinus Torvalds  *	holding a resource the IRQ handler may need you will deadlock.
2481da177e4SLinus Torvalds  *
2491da177e4SLinus Torvalds  *	This function may be called - with care - from IRQ context.
2501da177e4SLinus Torvalds  */
2511da177e4SLinus Torvalds void disable_irq(unsigned int irq)
2521da177e4SLinus Torvalds {
253d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
2541da177e4SLinus Torvalds 
2557d94f7caSYinghai Lu 	if (!desc)
256c2b5a251SMatthew Wilcox 		return;
257c2b5a251SMatthew Wilcox 
2581da177e4SLinus Torvalds 	disable_irq_nosync(irq);
2591da177e4SLinus Torvalds 	if (desc->action)
2601da177e4SLinus Torvalds 		synchronize_irq(irq);
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq);
2631da177e4SLinus Torvalds 
2640a0c5168SRafael J. Wysocki void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
2651adb0850SThomas Gleixner {
2660a0c5168SRafael J. Wysocki 	if (resume)
2670a0c5168SRafael J. Wysocki 		desc->status &= ~IRQ_SUSPENDED;
2680a0c5168SRafael J. Wysocki 
2691adb0850SThomas Gleixner 	switch (desc->depth) {
2701adb0850SThomas Gleixner 	case 0:
2710a0c5168SRafael J. Wysocki  err_out:
272b8c512f6SArjan van de Ven 		WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
2731adb0850SThomas Gleixner 		break;
2741adb0850SThomas Gleixner 	case 1: {
2751adb0850SThomas Gleixner 		unsigned int status = desc->status & ~IRQ_DISABLED;
2761adb0850SThomas Gleixner 
2770a0c5168SRafael J. Wysocki 		if (desc->status & IRQ_SUSPENDED)
2780a0c5168SRafael J. Wysocki 			goto err_out;
2791adb0850SThomas Gleixner 		/* Prevent probing on this irq: */
2801adb0850SThomas Gleixner 		desc->status = status | IRQ_NOPROBE;
2811adb0850SThomas Gleixner 		check_irq_resend(desc, irq);
2821adb0850SThomas Gleixner 		/* fall-through */
2831adb0850SThomas Gleixner 	}
2841adb0850SThomas Gleixner 	default:
2851adb0850SThomas Gleixner 		desc->depth--;
2861adb0850SThomas Gleixner 	}
2871adb0850SThomas Gleixner }
2881adb0850SThomas Gleixner 
2891da177e4SLinus Torvalds /**
2901da177e4SLinus Torvalds  *	enable_irq - enable handling of an irq
2911da177e4SLinus Torvalds  *	@irq: Interrupt to enable
2921da177e4SLinus Torvalds  *
2931da177e4SLinus Torvalds  *	Undoes the effect of one call to disable_irq().  If this
2941da177e4SLinus Torvalds  *	matches the last disable, processing of interrupts on this
2951da177e4SLinus Torvalds  *	IRQ line is re-enabled.
2961da177e4SLinus Torvalds  *
2971da177e4SLinus Torvalds  *	This function may be called from IRQ context.
2981da177e4SLinus Torvalds  */
2991da177e4SLinus Torvalds void enable_irq(unsigned int irq)
3001da177e4SLinus Torvalds {
301d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
3021da177e4SLinus Torvalds 	unsigned long flags;
3031da177e4SLinus Torvalds 
3047d94f7caSYinghai Lu 	if (!desc)
305c2b5a251SMatthew Wilcox 		return;
306c2b5a251SMatthew Wilcox 
3071da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
3080a0c5168SRafael J. Wysocki 	__enable_irq(desc, irq, false);
3091da177e4SLinus Torvalds 	spin_unlock_irqrestore(&desc->lock, flags);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq);
3121da177e4SLinus Torvalds 
3130c5d1eb7SDavid Brownell static int set_irq_wake_real(unsigned int irq, unsigned int on)
3142db87321SUwe Kleine-König {
31508678b08SYinghai Lu 	struct irq_desc *desc = irq_to_desc(irq);
3162db87321SUwe Kleine-König 	int ret = -ENXIO;
3172db87321SUwe Kleine-König 
3182db87321SUwe Kleine-König 	if (desc->chip->set_wake)
3192db87321SUwe Kleine-König 		ret = desc->chip->set_wake(irq, on);
3202db87321SUwe Kleine-König 
3212db87321SUwe Kleine-König 	return ret;
3222db87321SUwe Kleine-König }
3232db87321SUwe Kleine-König 
324ba9a2331SThomas Gleixner /**
325ba9a2331SThomas Gleixner  *	set_irq_wake - control irq power management wakeup
326ba9a2331SThomas Gleixner  *	@irq:	interrupt to control
327ba9a2331SThomas Gleixner  *	@on:	enable/disable power management wakeup
328ba9a2331SThomas Gleixner  *
32915a647ebSDavid Brownell  *	Enable/disable power management wakeup mode, which is
33015a647ebSDavid Brownell  *	disabled by default.  Enables and disables must match,
33115a647ebSDavid Brownell  *	just as they match for non-wakeup mode support.
33215a647ebSDavid Brownell  *
33315a647ebSDavid Brownell  *	Wakeup mode lets this IRQ wake the system from sleep
33415a647ebSDavid Brownell  *	states like "suspend to RAM".
335ba9a2331SThomas Gleixner  */
336ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on)
337ba9a2331SThomas Gleixner {
33808678b08SYinghai Lu 	struct irq_desc *desc = irq_to_desc(irq);
339ba9a2331SThomas Gleixner 	unsigned long flags;
3402db87321SUwe Kleine-König 	int ret = 0;
341ba9a2331SThomas Gleixner 
34215a647ebSDavid Brownell 	/* wakeup-capable irqs can be shared between drivers that
34315a647ebSDavid Brownell 	 * don't need to have the same sleep mode behaviors.
34415a647ebSDavid Brownell 	 */
345ba9a2331SThomas Gleixner 	spin_lock_irqsave(&desc->lock, flags);
34615a647ebSDavid Brownell 	if (on) {
3472db87321SUwe Kleine-König 		if (desc->wake_depth++ == 0) {
3482db87321SUwe Kleine-König 			ret = set_irq_wake_real(irq, on);
3492db87321SUwe Kleine-König 			if (ret)
3502db87321SUwe Kleine-König 				desc->wake_depth = 0;
35115a647ebSDavid Brownell 			else
3522db87321SUwe Kleine-König 				desc->status |= IRQ_WAKEUP;
3532db87321SUwe Kleine-König 		}
35415a647ebSDavid Brownell 	} else {
35515a647ebSDavid Brownell 		if (desc->wake_depth == 0) {
3567a2c4770SArjan van de Ven 			WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
3572db87321SUwe Kleine-König 		} else if (--desc->wake_depth == 0) {
3582db87321SUwe Kleine-König 			ret = set_irq_wake_real(irq, on);
3592db87321SUwe Kleine-König 			if (ret)
3602db87321SUwe Kleine-König 				desc->wake_depth = 1;
36115a647ebSDavid Brownell 			else
3622db87321SUwe Kleine-König 				desc->status &= ~IRQ_WAKEUP;
36315a647ebSDavid Brownell 		}
3642db87321SUwe Kleine-König 	}
3652db87321SUwe Kleine-König 
366ba9a2331SThomas Gleixner 	spin_unlock_irqrestore(&desc->lock, flags);
367ba9a2331SThomas Gleixner 	return ret;
368ba9a2331SThomas Gleixner }
369ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake);
370ba9a2331SThomas Gleixner 
3711da177e4SLinus Torvalds /*
3721da177e4SLinus Torvalds  * Internal function that tells the architecture code whether a
3731da177e4SLinus Torvalds  * particular irq has been exclusively allocated or is available
3741da177e4SLinus Torvalds  * for driver use.
3751da177e4SLinus Torvalds  */
3761da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags)
3771da177e4SLinus Torvalds {
378d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
3791da177e4SLinus Torvalds 	struct irqaction *action;
3801da177e4SLinus Torvalds 
3817d94f7caSYinghai Lu 	if (!desc)
3827d94f7caSYinghai Lu 		return 0;
3837d94f7caSYinghai Lu 
3847d94f7caSYinghai Lu 	if (desc->status & IRQ_NOREQUEST)
3851da177e4SLinus Torvalds 		return 0;
3861da177e4SLinus Torvalds 
38708678b08SYinghai Lu 	action = desc->action;
3881da177e4SLinus Torvalds 	if (action)
3893cca53b0SThomas Gleixner 		if (irqflags & action->flags & IRQF_SHARED)
3901da177e4SLinus Torvalds 			action = NULL;
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	return !action;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
3956a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc)
3966a6de9efSThomas Gleixner {
3976a6de9efSThomas Gleixner 	/*
3986a6de9efSThomas Gleixner 	 * If the architecture still has not overriden
3996a6de9efSThomas Gleixner 	 * the flow handler then zap the default. This
4006a6de9efSThomas Gleixner 	 * should catch incorrect flow-type setting.
4016a6de9efSThomas Gleixner 	 */
4026a6de9efSThomas Gleixner 	if (desc->handle_irq == &handle_bad_irq)
4036a6de9efSThomas Gleixner 		desc->handle_irq = NULL;
4046a6de9efSThomas Gleixner }
4056a6de9efSThomas Gleixner 
4060c5d1eb7SDavid Brownell int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
40782736f4dSUwe Kleine-König 		unsigned long flags)
40882736f4dSUwe Kleine-König {
40982736f4dSUwe Kleine-König 	int ret;
4100c5d1eb7SDavid Brownell 	struct irq_chip *chip = desc->chip;
41182736f4dSUwe Kleine-König 
41282736f4dSUwe Kleine-König 	if (!chip || !chip->set_type) {
41382736f4dSUwe Kleine-König 		/*
41482736f4dSUwe Kleine-König 		 * IRQF_TRIGGER_* but the PIC does not support multiple
41582736f4dSUwe Kleine-König 		 * flow-types?
41682736f4dSUwe Kleine-König 		 */
4173ff68a6aSMark Nelson 		pr_debug("No set_type function for IRQ %d (%s)\n", irq,
41882736f4dSUwe Kleine-König 				chip ? (chip->name ? : "unknown") : "unknown");
41982736f4dSUwe Kleine-König 		return 0;
42082736f4dSUwe Kleine-König 	}
42182736f4dSUwe Kleine-König 
422f2b662daSDavid Brownell 	/* caller masked out all except trigger mode flags */
423f2b662daSDavid Brownell 	ret = chip->set_type(irq, flags);
42482736f4dSUwe Kleine-König 
42582736f4dSUwe Kleine-König 	if (ret)
426c69ad71bSDavid Brownell 		pr_err("setting trigger mode %d for irq %u failed (%pF)\n",
427f2b662daSDavid Brownell 				(int)flags, irq, chip->set_type);
4280c5d1eb7SDavid Brownell 	else {
429f2b662daSDavid Brownell 		if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
430f2b662daSDavid Brownell 			flags |= IRQ_LEVEL;
4310c5d1eb7SDavid Brownell 		/* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */
432f2b662daSDavid Brownell 		desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK);
433f2b662daSDavid Brownell 		desc->status |= flags;
4340c5d1eb7SDavid Brownell 	}
43582736f4dSUwe Kleine-König 
43682736f4dSUwe Kleine-König 	return ret;
43782736f4dSUwe Kleine-König }
43882736f4dSUwe Kleine-König 
439*b25c340cSThomas Gleixner /*
440*b25c340cSThomas Gleixner  * Default primary interrupt handler for threaded interrupts. Is
441*b25c340cSThomas Gleixner  * assigned as primary handler when request_threaded_irq is called
442*b25c340cSThomas Gleixner  * with handler == NULL. Useful for oneshot interrupts.
443*b25c340cSThomas Gleixner  */
444*b25c340cSThomas Gleixner static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
445*b25c340cSThomas Gleixner {
446*b25c340cSThomas Gleixner 	return IRQ_WAKE_THREAD;
447*b25c340cSThomas Gleixner }
448*b25c340cSThomas Gleixner 
4493aa551c9SThomas Gleixner static int irq_wait_for_interrupt(struct irqaction *action)
4503aa551c9SThomas Gleixner {
4513aa551c9SThomas Gleixner 	while (!kthread_should_stop()) {
4523aa551c9SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
453f48fe81eSThomas Gleixner 
454f48fe81eSThomas Gleixner 		if (test_and_clear_bit(IRQTF_RUNTHREAD,
455f48fe81eSThomas Gleixner 				       &action->thread_flags)) {
4563aa551c9SThomas Gleixner 			__set_current_state(TASK_RUNNING);
4573aa551c9SThomas Gleixner 			return 0;
458f48fe81eSThomas Gleixner 		}
4593aa551c9SThomas Gleixner 		schedule();
4603aa551c9SThomas Gleixner 	}
4613aa551c9SThomas Gleixner 	return -1;
4623aa551c9SThomas Gleixner }
4633aa551c9SThomas Gleixner 
464*b25c340cSThomas Gleixner /*
465*b25c340cSThomas Gleixner  * Oneshot interrupts keep the irq line masked until the threaded
466*b25c340cSThomas Gleixner  * handler finished. unmask if the interrupt has not been disabled and
467*b25c340cSThomas Gleixner  * is marked MASKED.
468*b25c340cSThomas Gleixner  */
469*b25c340cSThomas Gleixner static void irq_finalize_oneshot(unsigned int irq, struct irq_desc *desc)
470*b25c340cSThomas Gleixner {
471*b25c340cSThomas Gleixner 	spin_lock_irq(&desc->lock);
472*b25c340cSThomas Gleixner 	if (!(desc->status & IRQ_DISABLED) && (desc->status & IRQ_MASKED)) {
473*b25c340cSThomas Gleixner 		desc->status &= ~IRQ_MASKED;
474*b25c340cSThomas Gleixner 		desc->chip->unmask(irq);
475*b25c340cSThomas Gleixner 	}
476*b25c340cSThomas Gleixner 	spin_unlock_irq(&desc->lock);
477*b25c340cSThomas Gleixner }
478*b25c340cSThomas Gleixner 
47961f38261SBruno Premont #ifdef CONFIG_SMP
4803aa551c9SThomas Gleixner /*
481591d2fb0SThomas Gleixner  * Check whether we need to change the affinity of the interrupt thread.
482591d2fb0SThomas Gleixner  */
483591d2fb0SThomas Gleixner static void
484591d2fb0SThomas Gleixner irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
485591d2fb0SThomas Gleixner {
486591d2fb0SThomas Gleixner 	cpumask_var_t mask;
487591d2fb0SThomas Gleixner 
488591d2fb0SThomas Gleixner 	if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
489591d2fb0SThomas Gleixner 		return;
490591d2fb0SThomas Gleixner 
491591d2fb0SThomas Gleixner 	/*
492591d2fb0SThomas Gleixner 	 * In case we are out of memory we set IRQTF_AFFINITY again and
493591d2fb0SThomas Gleixner 	 * try again next time
494591d2fb0SThomas Gleixner 	 */
495591d2fb0SThomas Gleixner 	if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
496591d2fb0SThomas Gleixner 		set_bit(IRQTF_AFFINITY, &action->thread_flags);
497591d2fb0SThomas Gleixner 		return;
498591d2fb0SThomas Gleixner 	}
499591d2fb0SThomas Gleixner 
500591d2fb0SThomas Gleixner 	spin_lock_irq(&desc->lock);
501591d2fb0SThomas Gleixner 	cpumask_copy(mask, desc->affinity);
502591d2fb0SThomas Gleixner 	spin_unlock_irq(&desc->lock);
503591d2fb0SThomas Gleixner 
504591d2fb0SThomas Gleixner 	set_cpus_allowed_ptr(current, mask);
505591d2fb0SThomas Gleixner 	free_cpumask_var(mask);
506591d2fb0SThomas Gleixner }
50761f38261SBruno Premont #else
50861f38261SBruno Premont static inline void
50961f38261SBruno Premont irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
51061f38261SBruno Premont #endif
511591d2fb0SThomas Gleixner 
512591d2fb0SThomas Gleixner /*
5133aa551c9SThomas Gleixner  * Interrupt handler thread
5143aa551c9SThomas Gleixner  */
5153aa551c9SThomas Gleixner static int irq_thread(void *data)
5163aa551c9SThomas Gleixner {
5173aa551c9SThomas Gleixner 	struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, };
5183aa551c9SThomas Gleixner 	struct irqaction *action = data;
5193aa551c9SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(action->irq);
520*b25c340cSThomas Gleixner 	int wake, oneshot = desc->status & IRQ_ONESHOT;
5213aa551c9SThomas Gleixner 
5223aa551c9SThomas Gleixner 	sched_setscheduler(current, SCHED_FIFO, &param);
5233aa551c9SThomas Gleixner 	current->irqaction = action;
5243aa551c9SThomas Gleixner 
5253aa551c9SThomas Gleixner 	while (!irq_wait_for_interrupt(action)) {
5263aa551c9SThomas Gleixner 
527591d2fb0SThomas Gleixner 		irq_thread_check_affinity(desc, action);
528591d2fb0SThomas Gleixner 
5293aa551c9SThomas Gleixner 		atomic_inc(&desc->threads_active);
5303aa551c9SThomas Gleixner 
5313aa551c9SThomas Gleixner 		spin_lock_irq(&desc->lock);
5323aa551c9SThomas Gleixner 		if (unlikely(desc->status & IRQ_DISABLED)) {
5333aa551c9SThomas Gleixner 			/*
5343aa551c9SThomas Gleixner 			 * CHECKME: We might need a dedicated
5353aa551c9SThomas Gleixner 			 * IRQ_THREAD_PENDING flag here, which
5363aa551c9SThomas Gleixner 			 * retriggers the thread in check_irq_resend()
5373aa551c9SThomas Gleixner 			 * but AFAICT IRQ_PENDING should be fine as it
5383aa551c9SThomas Gleixner 			 * retriggers the interrupt itself --- tglx
5393aa551c9SThomas Gleixner 			 */
5403aa551c9SThomas Gleixner 			desc->status |= IRQ_PENDING;
5413aa551c9SThomas Gleixner 			spin_unlock_irq(&desc->lock);
5423aa551c9SThomas Gleixner 		} else {
5433aa551c9SThomas Gleixner 			spin_unlock_irq(&desc->lock);
5443aa551c9SThomas Gleixner 
5453aa551c9SThomas Gleixner 			action->thread_fn(action->irq, action->dev_id);
546*b25c340cSThomas Gleixner 
547*b25c340cSThomas Gleixner 			if (oneshot)
548*b25c340cSThomas Gleixner 				irq_finalize_oneshot(action->irq, desc);
5493aa551c9SThomas Gleixner 		}
5503aa551c9SThomas Gleixner 
5513aa551c9SThomas Gleixner 		wake = atomic_dec_and_test(&desc->threads_active);
5523aa551c9SThomas Gleixner 
5533aa551c9SThomas Gleixner 		if (wake && waitqueue_active(&desc->wait_for_threads))
5543aa551c9SThomas Gleixner 			wake_up(&desc->wait_for_threads);
5553aa551c9SThomas Gleixner 	}
5563aa551c9SThomas Gleixner 
5573aa551c9SThomas Gleixner 	/*
5583aa551c9SThomas Gleixner 	 * Clear irqaction. Otherwise exit_irq_thread() would make
5593aa551c9SThomas Gleixner 	 * fuzz about an active irq thread going into nirvana.
5603aa551c9SThomas Gleixner 	 */
5613aa551c9SThomas Gleixner 	current->irqaction = NULL;
5623aa551c9SThomas Gleixner 	return 0;
5633aa551c9SThomas Gleixner }
5643aa551c9SThomas Gleixner 
5653aa551c9SThomas Gleixner /*
5663aa551c9SThomas Gleixner  * Called from do_exit()
5673aa551c9SThomas Gleixner  */
5683aa551c9SThomas Gleixner void exit_irq_thread(void)
5693aa551c9SThomas Gleixner {
5703aa551c9SThomas Gleixner 	struct task_struct *tsk = current;
5713aa551c9SThomas Gleixner 
5723aa551c9SThomas Gleixner 	if (!tsk->irqaction)
5733aa551c9SThomas Gleixner 		return;
5743aa551c9SThomas Gleixner 
5753aa551c9SThomas Gleixner 	printk(KERN_ERR
5763aa551c9SThomas Gleixner 	       "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
5773aa551c9SThomas Gleixner 	       tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq);
5783aa551c9SThomas Gleixner 
5793aa551c9SThomas Gleixner 	/*
5803aa551c9SThomas Gleixner 	 * Set the THREAD DIED flag to prevent further wakeups of the
5813aa551c9SThomas Gleixner 	 * soon to be gone threaded handler.
5823aa551c9SThomas Gleixner 	 */
5833aa551c9SThomas Gleixner 	set_bit(IRQTF_DIED, &tsk->irqaction->flags);
5843aa551c9SThomas Gleixner }
5853aa551c9SThomas Gleixner 
5861da177e4SLinus Torvalds /*
5871da177e4SLinus Torvalds  * Internal function to register an irqaction - typically used to
5881da177e4SLinus Torvalds  * allocate special interrupts that are part of the architecture.
5891da177e4SLinus Torvalds  */
590d3c60047SThomas Gleixner static int
591d3c60047SThomas Gleixner __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
5921da177e4SLinus Torvalds {
593f17c7545SIngo Molnar 	struct irqaction *old, **old_ptr;
5948b126b77SAndrew Morton 	const char *old_name = NULL;
5951da177e4SLinus Torvalds 	unsigned long flags;
5961da177e4SLinus Torvalds 	int shared = 0;
59782736f4dSUwe Kleine-König 	int ret;
5981da177e4SLinus Torvalds 
5997d94f7caSYinghai Lu 	if (!desc)
600c2b5a251SMatthew Wilcox 		return -EINVAL;
601c2b5a251SMatthew Wilcox 
602f1c2662cSIngo Molnar 	if (desc->chip == &no_irq_chip)
6031da177e4SLinus Torvalds 		return -ENOSYS;
6041da177e4SLinus Torvalds 	/*
6051da177e4SLinus Torvalds 	 * Some drivers like serial.c use request_irq() heavily,
6061da177e4SLinus Torvalds 	 * so we have to be careful not to interfere with a
6071da177e4SLinus Torvalds 	 * running system.
6081da177e4SLinus Torvalds 	 */
6093cca53b0SThomas Gleixner 	if (new->flags & IRQF_SAMPLE_RANDOM) {
6101da177e4SLinus Torvalds 		/*
6111da177e4SLinus Torvalds 		 * This function might sleep, we want to call it first,
6121da177e4SLinus Torvalds 		 * outside of the atomic block.
6131da177e4SLinus Torvalds 		 * Yes, this might clear the entropy pool if the wrong
6141da177e4SLinus Torvalds 		 * driver is attempted to be loaded, without actually
6151da177e4SLinus Torvalds 		 * installing a new handler, but is this really a problem,
6161da177e4SLinus Torvalds 		 * only the sysadmin is able to do this.
6171da177e4SLinus Torvalds 		 */
6181da177e4SLinus Torvalds 		rand_initialize_irq(irq);
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
621*b25c340cSThomas Gleixner 	/* Oneshot interrupts are not allowed with shared */
622*b25c340cSThomas Gleixner 	if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED))
623*b25c340cSThomas Gleixner 		return -EINVAL;
624*b25c340cSThomas Gleixner 
6251da177e4SLinus Torvalds 	/*
6263aa551c9SThomas Gleixner 	 * Threaded handler ?
6273aa551c9SThomas Gleixner 	 */
6283aa551c9SThomas Gleixner 	if (new->thread_fn) {
6293aa551c9SThomas Gleixner 		struct task_struct *t;
6303aa551c9SThomas Gleixner 
6313aa551c9SThomas Gleixner 		t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
6323aa551c9SThomas Gleixner 				   new->name);
6333aa551c9SThomas Gleixner 		if (IS_ERR(t))
6343aa551c9SThomas Gleixner 			return PTR_ERR(t);
6353aa551c9SThomas Gleixner 		/*
6363aa551c9SThomas Gleixner 		 * We keep the reference to the task struct even if
6373aa551c9SThomas Gleixner 		 * the thread dies to avoid that the interrupt code
6383aa551c9SThomas Gleixner 		 * references an already freed task_struct.
6393aa551c9SThomas Gleixner 		 */
6403aa551c9SThomas Gleixner 		get_task_struct(t);
6413aa551c9SThomas Gleixner 		new->thread = t;
6423aa551c9SThomas Gleixner 		wake_up_process(t);
6433aa551c9SThomas Gleixner 	}
6443aa551c9SThomas Gleixner 
6453aa551c9SThomas Gleixner 	/*
6461da177e4SLinus Torvalds 	 * The following block of code has to be executed atomically
6471da177e4SLinus Torvalds 	 */
6481da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
649f17c7545SIngo Molnar 	old_ptr = &desc->action;
650f17c7545SIngo Molnar 	old = *old_ptr;
65106fcb0c6SIngo Molnar 	if (old) {
652e76de9f8SThomas Gleixner 		/*
653e76de9f8SThomas Gleixner 		 * Can't share interrupts unless both agree to and are
654e76de9f8SThomas Gleixner 		 * the same type (level, edge, polarity). So both flag
6553cca53b0SThomas Gleixner 		 * fields must have IRQF_SHARED set and the bits which
656e76de9f8SThomas Gleixner 		 * set the trigger type must match.
657e76de9f8SThomas Gleixner 		 */
6583cca53b0SThomas Gleixner 		if (!((old->flags & new->flags) & IRQF_SHARED) ||
6598b126b77SAndrew Morton 		    ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
6608b126b77SAndrew Morton 			old_name = old->name;
661f5163427SDimitri Sivanich 			goto mismatch;
6628b126b77SAndrew Morton 		}
663f5163427SDimitri Sivanich 
664284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU)
665f5163427SDimitri Sivanich 		/* All handlers must agree on per-cpuness */
6663cca53b0SThomas Gleixner 		if ((old->flags & IRQF_PERCPU) !=
6673cca53b0SThomas Gleixner 		    (new->flags & IRQF_PERCPU))
668f5163427SDimitri Sivanich 			goto mismatch;
669f5163427SDimitri Sivanich #endif
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		/* add new interrupt at end of irq queue */
6721da177e4SLinus Torvalds 		do {
673f17c7545SIngo Molnar 			old_ptr = &old->next;
674f17c7545SIngo Molnar 			old = *old_ptr;
6751da177e4SLinus Torvalds 		} while (old);
6761da177e4SLinus Torvalds 		shared = 1;
6771da177e4SLinus Torvalds 	}
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	if (!shared) {
6806a6de9efSThomas Gleixner 		irq_chip_set_defaults(desc->chip);
681e76de9f8SThomas Gleixner 
6823aa551c9SThomas Gleixner 		init_waitqueue_head(&desc->wait_for_threads);
6833aa551c9SThomas Gleixner 
68482736f4dSUwe Kleine-König 		/* Setup the type (level, edge polarity) if configured: */
68582736f4dSUwe Kleine-König 		if (new->flags & IRQF_TRIGGER_MASK) {
686f2b662daSDavid Brownell 			ret = __irq_set_trigger(desc, irq,
687f2b662daSDavid Brownell 					new->flags & IRQF_TRIGGER_MASK);
68882736f4dSUwe Kleine-König 
6893aa551c9SThomas Gleixner 			if (ret)
6903aa551c9SThomas Gleixner 				goto out_thread;
69182736f4dSUwe Kleine-König 		} else
69282736f4dSUwe Kleine-König 			compat_irq_chip_set_default_handler(desc);
693f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU)
694f75d222bSAhmed S. Darwish 		if (new->flags & IRQF_PERCPU)
695f75d222bSAhmed S. Darwish 			desc->status |= IRQ_PER_CPU;
696f75d222bSAhmed S. Darwish #endif
697f75d222bSAhmed S. Darwish 
698*b25c340cSThomas Gleixner 		desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | IRQ_ONESHOT |
6991adb0850SThomas Gleixner 				  IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);
70094d39e1fSThomas Gleixner 
701*b25c340cSThomas Gleixner 		if (new->flags & IRQF_ONESHOT)
702*b25c340cSThomas Gleixner 			desc->status |= IRQ_ONESHOT;
703*b25c340cSThomas Gleixner 
70494d39e1fSThomas Gleixner 		if (!(desc->status & IRQ_NOAUTOEN)) {
7051da177e4SLinus Torvalds 			desc->depth = 0;
70694d39e1fSThomas Gleixner 			desc->status &= ~IRQ_DISABLED;
707d1bef4edSIngo Molnar 			desc->chip->startup(irq);
708e76de9f8SThomas Gleixner 		} else
709e76de9f8SThomas Gleixner 			/* Undo nested disables: */
710e76de9f8SThomas Gleixner 			desc->depth = 1;
71118404756SMax Krasnyansky 
712612e3684SThomas Gleixner 		/* Exclude IRQ from balancing if requested */
713612e3684SThomas Gleixner 		if (new->flags & IRQF_NOBALANCING)
714612e3684SThomas Gleixner 			desc->status |= IRQ_NO_BALANCING;
715612e3684SThomas Gleixner 
71618404756SMax Krasnyansky 		/* Set default affinity mask once everything is setup */
717548c8933SHannes Eder 		setup_affinity(irq, desc);
7180c5d1eb7SDavid Brownell 
7190c5d1eb7SDavid Brownell 	} else if ((new->flags & IRQF_TRIGGER_MASK)
7200c5d1eb7SDavid Brownell 			&& (new->flags & IRQF_TRIGGER_MASK)
7210c5d1eb7SDavid Brownell 				!= (desc->status & IRQ_TYPE_SENSE_MASK)) {
7220c5d1eb7SDavid Brownell 		/* hope the handler works with the actual trigger mode... */
7230c5d1eb7SDavid Brownell 		pr_warning("IRQ %d uses trigger mode %d; requested %d\n",
7240c5d1eb7SDavid Brownell 				irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK),
7250c5d1eb7SDavid Brownell 				(int)(new->flags & IRQF_TRIGGER_MASK));
72694d39e1fSThomas Gleixner 	}
72782736f4dSUwe Kleine-König 
728f17c7545SIngo Molnar 	*old_ptr = new;
72982736f4dSUwe Kleine-König 
7308528b0f1SLinus Torvalds 	/* Reset broken irq detection when installing new handler */
7318528b0f1SLinus Torvalds 	desc->irq_count = 0;
7328528b0f1SLinus Torvalds 	desc->irqs_unhandled = 0;
7331adb0850SThomas Gleixner 
7341adb0850SThomas Gleixner 	/*
7351adb0850SThomas Gleixner 	 * Check whether we disabled the irq via the spurious handler
7361adb0850SThomas Gleixner 	 * before. Reenable it and give it another chance.
7371adb0850SThomas Gleixner 	 */
7381adb0850SThomas Gleixner 	if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {
7391adb0850SThomas Gleixner 		desc->status &= ~IRQ_SPURIOUS_DISABLED;
7400a0c5168SRafael J. Wysocki 		__enable_irq(desc, irq, false);
7411adb0850SThomas Gleixner 	}
7421adb0850SThomas Gleixner 
7431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&desc->lock, flags);
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	new->irq = irq;
7462c6927a3SYinghai Lu 	register_irq_proc(irq, desc);
7471da177e4SLinus Torvalds 	new->dir = NULL;
7481da177e4SLinus Torvalds 	register_handler_proc(irq, new);
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	return 0;
751f5163427SDimitri Sivanich 
752f5163427SDimitri Sivanich mismatch:
7533f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ
7543cca53b0SThomas Gleixner 	if (!(new->flags & IRQF_PROBE_SHARED)) {
755e8c4b9d0SBjorn Helgaas 		printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
7568b126b77SAndrew Morton 		if (old_name)
7578b126b77SAndrew Morton 			printk(KERN_ERR "current handler: %s\n", old_name);
758f5163427SDimitri Sivanich 		dump_stack();
75913e87ec6SAndrew Morton 	}
7603f050447SAlan Cox #endif
7613aa551c9SThomas Gleixner 	ret = -EBUSY;
7623aa551c9SThomas Gleixner 
7633aa551c9SThomas Gleixner out_thread:
7648b126b77SAndrew Morton 	spin_unlock_irqrestore(&desc->lock, flags);
7653aa551c9SThomas Gleixner 	if (new->thread) {
7663aa551c9SThomas Gleixner 		struct task_struct *t = new->thread;
7673aa551c9SThomas Gleixner 
7683aa551c9SThomas Gleixner 		new->thread = NULL;
7693aa551c9SThomas Gleixner 		if (likely(!test_bit(IRQTF_DIED, &new->thread_flags)))
7703aa551c9SThomas Gleixner 			kthread_stop(t);
7713aa551c9SThomas Gleixner 		put_task_struct(t);
7723aa551c9SThomas Gleixner 	}
7733aa551c9SThomas Gleixner 	return ret;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds /**
777d3c60047SThomas Gleixner  *	setup_irq - setup an interrupt
778d3c60047SThomas Gleixner  *	@irq: Interrupt line to setup
779d3c60047SThomas Gleixner  *	@act: irqaction for the interrupt
780d3c60047SThomas Gleixner  *
781d3c60047SThomas Gleixner  * Used to statically setup interrupts in the early boot process.
782d3c60047SThomas Gleixner  */
783d3c60047SThomas Gleixner int setup_irq(unsigned int irq, struct irqaction *act)
784d3c60047SThomas Gleixner {
785d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
786d3c60047SThomas Gleixner 
787d3c60047SThomas Gleixner 	return __setup_irq(irq, desc, act);
788d3c60047SThomas Gleixner }
789eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(setup_irq);
790d3c60047SThomas Gleixner 
791cbf94f06SMagnus Damm  /*
792cbf94f06SMagnus Damm  * Internal function to unregister an irqaction - used to free
793cbf94f06SMagnus Damm  * regular and special interrupts that are part of the architecture.
7941da177e4SLinus Torvalds  */
795cbf94f06SMagnus Damm static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
7961da177e4SLinus Torvalds {
797d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
798f17c7545SIngo Molnar 	struct irqaction *action, **action_ptr;
7991da177e4SLinus Torvalds 	unsigned long flags;
8001da177e4SLinus Torvalds 
801ae88a23bSIngo Molnar 	WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
8027d94f7caSYinghai Lu 
8037d94f7caSYinghai Lu 	if (!desc)
804f21cfb25SMagnus Damm 		return NULL;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	spin_lock_irqsave(&desc->lock, flags);
807ae88a23bSIngo Molnar 
808ae88a23bSIngo Molnar 	/*
809ae88a23bSIngo Molnar 	 * There can be multiple actions per IRQ descriptor, find the right
810ae88a23bSIngo Molnar 	 * one based on the dev_id:
811ae88a23bSIngo Molnar 	 */
812f17c7545SIngo Molnar 	action_ptr = &desc->action;
8131da177e4SLinus Torvalds 	for (;;) {
814f17c7545SIngo Molnar 		action = *action_ptr;
8151da177e4SLinus Torvalds 
816ae88a23bSIngo Molnar 		if (!action) {
817ae88a23bSIngo Molnar 			WARN(1, "Trying to free already-free IRQ %d\n", irq);
818ae88a23bSIngo Molnar 			spin_unlock_irqrestore(&desc->lock, flags);
819ae88a23bSIngo Molnar 
820f21cfb25SMagnus Damm 			return NULL;
821ae88a23bSIngo Molnar 		}
8221da177e4SLinus Torvalds 
8238316e381SIngo Molnar 		if (action->dev_id == dev_id)
824ae88a23bSIngo Molnar 			break;
825f17c7545SIngo Molnar 		action_ptr = &action->next;
826ae88a23bSIngo Molnar 	}
827ae88a23bSIngo Molnar 
828ae88a23bSIngo Molnar 	/* Found it - now remove it from the list of entries: */
829f17c7545SIngo Molnar 	*action_ptr = action->next;
830dbce706eSPaolo 'Blaisorblade' Giarrusso 
831ae88a23bSIngo Molnar 	/* Currently used only by UML, might disappear one day: */
832b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD
833d1bef4edSIngo Molnar 	if (desc->chip->release)
834d1bef4edSIngo Molnar 		desc->chip->release(irq, dev_id);
835b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif
836dbce706eSPaolo 'Blaisorblade' Giarrusso 
837ae88a23bSIngo Molnar 	/* If this was the last handler, shut down the IRQ line: */
8381da177e4SLinus Torvalds 	if (!desc->action) {
8391da177e4SLinus Torvalds 		desc->status |= IRQ_DISABLED;
840d1bef4edSIngo Molnar 		if (desc->chip->shutdown)
841d1bef4edSIngo Molnar 			desc->chip->shutdown(irq);
8421da177e4SLinus Torvalds 		else
843d1bef4edSIngo Molnar 			desc->chip->disable(irq);
8441da177e4SLinus Torvalds 	}
8453aa551c9SThomas Gleixner 
8461da177e4SLinus Torvalds 	spin_unlock_irqrestore(&desc->lock, flags);
847ae88a23bSIngo Molnar 
8481da177e4SLinus Torvalds 	unregister_handler_proc(irq, action);
8491da177e4SLinus Torvalds 
850ae88a23bSIngo Molnar 	/* Make sure it's not being used on another CPU: */
8511da177e4SLinus Torvalds 	synchronize_irq(irq);
852ae88a23bSIngo Molnar 
8531d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ
8541d99493bSDavid Woodhouse 	/*
855ae88a23bSIngo Molnar 	 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
856ae88a23bSIngo Molnar 	 * event to happen even now it's being freed, so let's make sure that
857ae88a23bSIngo Molnar 	 * is so by doing an extra call to the handler ....
858ae88a23bSIngo Molnar 	 *
859ae88a23bSIngo Molnar 	 * ( We do this after actually deregistering it, to make sure that a
860ae88a23bSIngo Molnar 	 *   'real' IRQ doesn't run in * parallel with our fake. )
8611d99493bSDavid Woodhouse 	 */
8621d99493bSDavid Woodhouse 	if (action->flags & IRQF_SHARED) {
8631d99493bSDavid Woodhouse 		local_irq_save(flags);
8641d99493bSDavid Woodhouse 		action->handler(irq, dev_id);
8651d99493bSDavid Woodhouse 		local_irq_restore(flags);
8661d99493bSDavid Woodhouse 	}
8671d99493bSDavid Woodhouse #endif
8682d860ad7SLinus Torvalds 
8692d860ad7SLinus Torvalds 	if (action->thread) {
8702d860ad7SLinus Torvalds 		if (!test_bit(IRQTF_DIED, &action->thread_flags))
8712d860ad7SLinus Torvalds 			kthread_stop(action->thread);
8722d860ad7SLinus Torvalds 		put_task_struct(action->thread);
8732d860ad7SLinus Torvalds 	}
8742d860ad7SLinus Torvalds 
875f21cfb25SMagnus Damm 	return action;
876f21cfb25SMagnus Damm }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds /**
879cbf94f06SMagnus Damm  *	remove_irq - free an interrupt
880cbf94f06SMagnus Damm  *	@irq: Interrupt line to free
881cbf94f06SMagnus Damm  *	@act: irqaction for the interrupt
882cbf94f06SMagnus Damm  *
883cbf94f06SMagnus Damm  * Used to remove interrupts statically setup by the early boot process.
884cbf94f06SMagnus Damm  */
885cbf94f06SMagnus Damm void remove_irq(unsigned int irq, struct irqaction *act)
886cbf94f06SMagnus Damm {
887cbf94f06SMagnus Damm 	__free_irq(irq, act->dev_id);
888cbf94f06SMagnus Damm }
889eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(remove_irq);
890cbf94f06SMagnus Damm 
891cbf94f06SMagnus Damm /**
892f21cfb25SMagnus Damm  *	free_irq - free an interrupt allocated with request_irq
8931da177e4SLinus Torvalds  *	@irq: Interrupt line to free
8941da177e4SLinus Torvalds  *	@dev_id: Device identity to free
8951da177e4SLinus Torvalds  *
8961da177e4SLinus Torvalds  *	Remove an interrupt handler. The handler is removed and if the
8971da177e4SLinus Torvalds  *	interrupt line is no longer in use by any driver it is disabled.
8981da177e4SLinus Torvalds  *	On a shared IRQ the caller must ensure the interrupt is disabled
8991da177e4SLinus Torvalds  *	on the card it drives before calling this function. The function
9001da177e4SLinus Torvalds  *	does not return until any executing interrupts for this IRQ
9011da177e4SLinus Torvalds  *	have completed.
9021da177e4SLinus Torvalds  *
9031da177e4SLinus Torvalds  *	This function must not be called from interrupt context.
9041da177e4SLinus Torvalds  */
9051da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id)
9061da177e4SLinus Torvalds {
907cbf94f06SMagnus Damm 	kfree(__free_irq(irq, dev_id));
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq);
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds /**
9123aa551c9SThomas Gleixner  *	request_threaded_irq - allocate an interrupt line
9131da177e4SLinus Torvalds  *	@irq: Interrupt line to allocate
9143aa551c9SThomas Gleixner  *	@handler: Function to be called when the IRQ occurs.
9153aa551c9SThomas Gleixner  *		  Primary handler for threaded interrupts
916*b25c340cSThomas Gleixner  *		  If NULL and thread_fn != NULL the default
917*b25c340cSThomas Gleixner  *		  primary handler is installed
9183aa551c9SThomas Gleixner  *	@thread_fn: Function called from the irq handler thread
9193aa551c9SThomas Gleixner  *		    If NULL, no irq thread is created
9201da177e4SLinus Torvalds  *	@irqflags: Interrupt type flags
9211da177e4SLinus Torvalds  *	@devname: An ascii name for the claiming device
9221da177e4SLinus Torvalds  *	@dev_id: A cookie passed back to the handler function
9231da177e4SLinus Torvalds  *
9241da177e4SLinus Torvalds  *	This call allocates interrupt resources and enables the
9251da177e4SLinus Torvalds  *	interrupt line and IRQ handling. From the point this
9261da177e4SLinus Torvalds  *	call is made your handler function may be invoked. Since
9271da177e4SLinus Torvalds  *	your handler function must clear any interrupt the board
9281da177e4SLinus Torvalds  *	raises, you must take care both to initialise your hardware
9291da177e4SLinus Torvalds  *	and to set up the interrupt handler in the right order.
9301da177e4SLinus Torvalds  *
9313aa551c9SThomas Gleixner  *	If you want to set up a threaded irq handler for your device
9323aa551c9SThomas Gleixner  *	then you need to supply @handler and @thread_fn. @handler ist
9333aa551c9SThomas Gleixner  *	still called in hard interrupt context and has to check
9343aa551c9SThomas Gleixner  *	whether the interrupt originates from the device. If yes it
9353aa551c9SThomas Gleixner  *	needs to disable the interrupt on the device and return
93639a2eddbSSteven Rostedt  *	IRQ_WAKE_THREAD which will wake up the handler thread and run
9373aa551c9SThomas Gleixner  *	@thread_fn. This split handler design is necessary to support
9383aa551c9SThomas Gleixner  *	shared interrupts.
9393aa551c9SThomas Gleixner  *
9401da177e4SLinus Torvalds  *	Dev_id must be globally unique. Normally the address of the
9411da177e4SLinus Torvalds  *	device data structure is used as the cookie. Since the handler
9421da177e4SLinus Torvalds  *	receives this value it makes sense to use it.
9431da177e4SLinus Torvalds  *
9441da177e4SLinus Torvalds  *	If your interrupt is shared you must pass a non NULL dev_id
9451da177e4SLinus Torvalds  *	as this is required when freeing the interrupt.
9461da177e4SLinus Torvalds  *
9471da177e4SLinus Torvalds  *	Flags:
9481da177e4SLinus Torvalds  *
9493cca53b0SThomas Gleixner  *	IRQF_SHARED		Interrupt is shared
9503cca53b0SThomas Gleixner  *	IRQF_DISABLED	Disable local interrupts while processing
9513cca53b0SThomas Gleixner  *	IRQF_SAMPLE_RANDOM	The interrupt can be used for entropy
9520c5d1eb7SDavid Brownell  *	IRQF_TRIGGER_*		Specify active edge(s) or level
9531da177e4SLinus Torvalds  *
9541da177e4SLinus Torvalds  */
9553aa551c9SThomas Gleixner int request_threaded_irq(unsigned int irq, irq_handler_t handler,
9563aa551c9SThomas Gleixner 			 irq_handler_t thread_fn, unsigned long irqflags,
9573aa551c9SThomas Gleixner 			 const char *devname, void *dev_id)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	struct irqaction *action;
96008678b08SYinghai Lu 	struct irq_desc *desc;
961d3c60047SThomas Gleixner 	int retval;
9621da177e4SLinus Torvalds 
963470c6623SDavid Brownell 	/*
964470c6623SDavid Brownell 	 * handle_IRQ_event() always ignores IRQF_DISABLED except for
965470c6623SDavid Brownell 	 * the _first_ irqaction (sigh).  That can cause oopsing, but
966470c6623SDavid Brownell 	 * the behavior is classified as "will not fix" so we need to
967470c6623SDavid Brownell 	 * start nudging drivers away from using that idiom.
968470c6623SDavid Brownell 	 */
969327ec569SIngo Molnar 	if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) ==
970327ec569SIngo Molnar 					(IRQF_SHARED|IRQF_DISABLED)) {
971327ec569SIngo Molnar 		pr_warning(
972327ec569SIngo Molnar 		  "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n",
973470c6623SDavid Brownell 			irq, devname);
974327ec569SIngo Molnar 	}
975470c6623SDavid Brownell 
976fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
977fbb9ce95SIngo Molnar 	/*
978fbb9ce95SIngo Molnar 	 * Lockdep wants atomic interrupt handlers:
979fbb9ce95SIngo Molnar 	 */
98038515e90SThomas Gleixner 	irqflags |= IRQF_DISABLED;
981fbb9ce95SIngo Molnar #endif
9821da177e4SLinus Torvalds 	/*
9831da177e4SLinus Torvalds 	 * Sanity-check: shared interrupts must pass in a real dev-ID,
9841da177e4SLinus Torvalds 	 * otherwise we'll have trouble later trying to figure out
9851da177e4SLinus Torvalds 	 * which interrupt is which (messes up the interrupt freeing
9861da177e4SLinus Torvalds 	 * logic etc).
9871da177e4SLinus Torvalds 	 */
9883cca53b0SThomas Gleixner 	if ((irqflags & IRQF_SHARED) && !dev_id)
9891da177e4SLinus Torvalds 		return -EINVAL;
9907d94f7caSYinghai Lu 
991cb5bc832SYinghai Lu 	desc = irq_to_desc(irq);
9927d94f7caSYinghai Lu 	if (!desc)
9931da177e4SLinus Torvalds 		return -EINVAL;
9947d94f7caSYinghai Lu 
99508678b08SYinghai Lu 	if (desc->status & IRQ_NOREQUEST)
9966550c775SThomas Gleixner 		return -EINVAL;
997*b25c340cSThomas Gleixner 
998*b25c340cSThomas Gleixner 	if (!handler) {
999*b25c340cSThomas Gleixner 		if (!thread_fn)
10001da177e4SLinus Torvalds 			return -EINVAL;
1001*b25c340cSThomas Gleixner 		handler = irq_default_primary_handler;
1002*b25c340cSThomas Gleixner 	}
10031da177e4SLinus Torvalds 
100445535732SThomas Gleixner 	action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
10051da177e4SLinus Torvalds 	if (!action)
10061da177e4SLinus Torvalds 		return -ENOMEM;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	action->handler = handler;
10093aa551c9SThomas Gleixner 	action->thread_fn = thread_fn;
10101da177e4SLinus Torvalds 	action->flags = irqflags;
10111da177e4SLinus Torvalds 	action->name = devname;
10121da177e4SLinus Torvalds 	action->dev_id = dev_id;
10131da177e4SLinus Torvalds 
1014d3c60047SThomas Gleixner 	retval = __setup_irq(irq, desc, action);
1015377bf1e4SAnton Vorontsov 	if (retval)
1016377bf1e4SAnton Vorontsov 		kfree(action);
1017377bf1e4SAnton Vorontsov 
1018a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ
1019a304e1b8SDavid Woodhouse 	if (irqflags & IRQF_SHARED) {
1020a304e1b8SDavid Woodhouse 		/*
1021a304e1b8SDavid Woodhouse 		 * It's a shared IRQ -- the driver ought to be prepared for it
1022a304e1b8SDavid Woodhouse 		 * to happen immediately, so let's make sure....
1023377bf1e4SAnton Vorontsov 		 * We disable the irq to make sure that a 'real' IRQ doesn't
1024377bf1e4SAnton Vorontsov 		 * run in parallel with our fake.
1025a304e1b8SDavid Woodhouse 		 */
1026a304e1b8SDavid Woodhouse 		unsigned long flags;
1027a304e1b8SDavid Woodhouse 
1028377bf1e4SAnton Vorontsov 		disable_irq(irq);
1029a304e1b8SDavid Woodhouse 		local_irq_save(flags);
1030377bf1e4SAnton Vorontsov 
1031a304e1b8SDavid Woodhouse 		handler(irq, dev_id);
1032377bf1e4SAnton Vorontsov 
1033a304e1b8SDavid Woodhouse 		local_irq_restore(flags);
1034377bf1e4SAnton Vorontsov 		enable_irq(irq);
1035a304e1b8SDavid Woodhouse 	}
1036a304e1b8SDavid Woodhouse #endif
10371da177e4SLinus Torvalds 	return retval;
10381da177e4SLinus Torvalds }
10393aa551c9SThomas Gleixner EXPORT_SYMBOL(request_threaded_irq);
1040