xref: /openbmc/linux/kernel/irq/manage.c (revision ae731f8d0785ccd3380f511bae888933b6562e45)
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. */
49239007b8SThomas Gleixner 		raw_spin_lock_irqsave(&desc->lock, flags);
50a98ce5c6SHerbert Xu 		status = desc->status;
51239007b8SThomas Gleixner 		raw_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 
117239007b8SThomas Gleixner 	raw_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;
137239007b8SThomas Gleixner 	raw_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 
184239007b8SThomas Gleixner 	raw_spin_lock_irqsave(&desc->lock, flags);
185548c8933SHannes Eder 	ret = setup_affinity(irq, desc);
1863aa551c9SThomas Gleixner 	if (!ret)
187591d2fb0SThomas Gleixner 		irq_set_thread_affinity(desc);
188239007b8SThomas Gleixner 	raw_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 
23370aedd24SThomas Gleixner 	chip_bus_lock(irq, desc);
234239007b8SThomas Gleixner 	raw_spin_lock_irqsave(&desc->lock, flags);
2350a0c5168SRafael J. Wysocki 	__disable_irq(desc, irq, false);
236239007b8SThomas Gleixner 	raw_spin_unlock_irqrestore(&desc->lock, flags);
23770aedd24SThomas Gleixner 	chip_bus_sync_unlock(irq, desc);
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq_nosync);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /**
2421da177e4SLinus Torvalds  *	disable_irq - disable an irq and wait for completion
2431da177e4SLinus Torvalds  *	@irq: Interrupt to disable
2441da177e4SLinus Torvalds  *
2451da177e4SLinus Torvalds  *	Disable the selected interrupt line.  Enables and Disables are
2461da177e4SLinus Torvalds  *	nested.
2471da177e4SLinus Torvalds  *	This function waits for any pending IRQ handlers for this interrupt
2481da177e4SLinus Torvalds  *	to complete before returning. If you use this function while
2491da177e4SLinus Torvalds  *	holding a resource the IRQ handler may need you will deadlock.
2501da177e4SLinus Torvalds  *
2511da177e4SLinus Torvalds  *	This function may be called - with care - from IRQ context.
2521da177e4SLinus Torvalds  */
2531da177e4SLinus Torvalds void disable_irq(unsigned int irq)
2541da177e4SLinus Torvalds {
255d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
2561da177e4SLinus Torvalds 
2577d94f7caSYinghai Lu 	if (!desc)
258c2b5a251SMatthew Wilcox 		return;
259c2b5a251SMatthew Wilcox 
2601da177e4SLinus Torvalds 	disable_irq_nosync(irq);
2611da177e4SLinus Torvalds 	if (desc->action)
2621da177e4SLinus Torvalds 		synchronize_irq(irq);
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds EXPORT_SYMBOL(disable_irq);
2651da177e4SLinus Torvalds 
2660a0c5168SRafael J. Wysocki void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
2671adb0850SThomas Gleixner {
2680a0c5168SRafael J. Wysocki 	if (resume)
2690a0c5168SRafael J. Wysocki 		desc->status &= ~IRQ_SUSPENDED;
2700a0c5168SRafael J. Wysocki 
2711adb0850SThomas Gleixner 	switch (desc->depth) {
2721adb0850SThomas Gleixner 	case 0:
2730a0c5168SRafael J. Wysocki  err_out:
274b8c512f6SArjan van de Ven 		WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
2751adb0850SThomas Gleixner 		break;
2761adb0850SThomas Gleixner 	case 1: {
2771adb0850SThomas Gleixner 		unsigned int status = desc->status & ~IRQ_DISABLED;
2781adb0850SThomas Gleixner 
2790a0c5168SRafael J. Wysocki 		if (desc->status & IRQ_SUSPENDED)
2800a0c5168SRafael J. Wysocki 			goto err_out;
2811adb0850SThomas Gleixner 		/* Prevent probing on this irq: */
2821adb0850SThomas Gleixner 		desc->status = status | IRQ_NOPROBE;
2831adb0850SThomas Gleixner 		check_irq_resend(desc, irq);
2841adb0850SThomas Gleixner 		/* fall-through */
2851adb0850SThomas Gleixner 	}
2861adb0850SThomas Gleixner 	default:
2871adb0850SThomas Gleixner 		desc->depth--;
2881adb0850SThomas Gleixner 	}
2891adb0850SThomas Gleixner }
2901adb0850SThomas Gleixner 
2911da177e4SLinus Torvalds /**
2921da177e4SLinus Torvalds  *	enable_irq - enable handling of an irq
2931da177e4SLinus Torvalds  *	@irq: Interrupt to enable
2941da177e4SLinus Torvalds  *
2951da177e4SLinus Torvalds  *	Undoes the effect of one call to disable_irq().  If this
2961da177e4SLinus Torvalds  *	matches the last disable, processing of interrupts on this
2971da177e4SLinus Torvalds  *	IRQ line is re-enabled.
2981da177e4SLinus Torvalds  *
29970aedd24SThomas Gleixner  *	This function may be called from IRQ context only when
30070aedd24SThomas Gleixner  *	desc->chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds void enable_irq(unsigned int irq)
3031da177e4SLinus Torvalds {
304d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
3051da177e4SLinus Torvalds 	unsigned long flags;
3061da177e4SLinus Torvalds 
3077d94f7caSYinghai Lu 	if (!desc)
308c2b5a251SMatthew Wilcox 		return;
309c2b5a251SMatthew Wilcox 
31070aedd24SThomas Gleixner 	chip_bus_lock(irq, desc);
311239007b8SThomas Gleixner 	raw_spin_lock_irqsave(&desc->lock, flags);
3120a0c5168SRafael J. Wysocki 	__enable_irq(desc, irq, false);
313239007b8SThomas Gleixner 	raw_spin_unlock_irqrestore(&desc->lock, flags);
31470aedd24SThomas Gleixner 	chip_bus_sync_unlock(irq, desc);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds EXPORT_SYMBOL(enable_irq);
3171da177e4SLinus Torvalds 
3180c5d1eb7SDavid Brownell static int set_irq_wake_real(unsigned int irq, unsigned int on)
3192db87321SUwe Kleine-König {
32008678b08SYinghai Lu 	struct irq_desc *desc = irq_to_desc(irq);
3212db87321SUwe Kleine-König 	int ret = -ENXIO;
3222db87321SUwe Kleine-König 
3232db87321SUwe Kleine-König 	if (desc->chip->set_wake)
3242db87321SUwe Kleine-König 		ret = desc->chip->set_wake(irq, on);
3252db87321SUwe Kleine-König 
3262db87321SUwe Kleine-König 	return ret;
3272db87321SUwe Kleine-König }
3282db87321SUwe Kleine-König 
329ba9a2331SThomas Gleixner /**
330ba9a2331SThomas Gleixner  *	set_irq_wake - control irq power management wakeup
331ba9a2331SThomas Gleixner  *	@irq:	interrupt to control
332ba9a2331SThomas Gleixner  *	@on:	enable/disable power management wakeup
333ba9a2331SThomas Gleixner  *
33415a647ebSDavid Brownell  *	Enable/disable power management wakeup mode, which is
33515a647ebSDavid Brownell  *	disabled by default.  Enables and disables must match,
33615a647ebSDavid Brownell  *	just as they match for non-wakeup mode support.
33715a647ebSDavid Brownell  *
33815a647ebSDavid Brownell  *	Wakeup mode lets this IRQ wake the system from sleep
33915a647ebSDavid Brownell  *	states like "suspend to RAM".
340ba9a2331SThomas Gleixner  */
341ba9a2331SThomas Gleixner int set_irq_wake(unsigned int irq, unsigned int on)
342ba9a2331SThomas Gleixner {
34308678b08SYinghai Lu 	struct irq_desc *desc = irq_to_desc(irq);
344ba9a2331SThomas Gleixner 	unsigned long flags;
3452db87321SUwe Kleine-König 	int ret = 0;
346ba9a2331SThomas Gleixner 
34715a647ebSDavid Brownell 	/* wakeup-capable irqs can be shared between drivers that
34815a647ebSDavid Brownell 	 * don't need to have the same sleep mode behaviors.
34915a647ebSDavid Brownell 	 */
350239007b8SThomas Gleixner 	raw_spin_lock_irqsave(&desc->lock, flags);
35115a647ebSDavid Brownell 	if (on) {
3522db87321SUwe Kleine-König 		if (desc->wake_depth++ == 0) {
3532db87321SUwe Kleine-König 			ret = set_irq_wake_real(irq, on);
3542db87321SUwe Kleine-König 			if (ret)
3552db87321SUwe Kleine-König 				desc->wake_depth = 0;
35615a647ebSDavid Brownell 			else
3572db87321SUwe Kleine-König 				desc->status |= IRQ_WAKEUP;
3582db87321SUwe Kleine-König 		}
35915a647ebSDavid Brownell 	} else {
36015a647ebSDavid Brownell 		if (desc->wake_depth == 0) {
3617a2c4770SArjan van de Ven 			WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
3622db87321SUwe Kleine-König 		} else if (--desc->wake_depth == 0) {
3632db87321SUwe Kleine-König 			ret = set_irq_wake_real(irq, on);
3642db87321SUwe Kleine-König 			if (ret)
3652db87321SUwe Kleine-König 				desc->wake_depth = 1;
36615a647ebSDavid Brownell 			else
3672db87321SUwe Kleine-König 				desc->status &= ~IRQ_WAKEUP;
36815a647ebSDavid Brownell 		}
3692db87321SUwe Kleine-König 	}
3702db87321SUwe Kleine-König 
371239007b8SThomas Gleixner 	raw_spin_unlock_irqrestore(&desc->lock, flags);
372ba9a2331SThomas Gleixner 	return ret;
373ba9a2331SThomas Gleixner }
374ba9a2331SThomas Gleixner EXPORT_SYMBOL(set_irq_wake);
375ba9a2331SThomas Gleixner 
3761da177e4SLinus Torvalds /*
3771da177e4SLinus Torvalds  * Internal function that tells the architecture code whether a
3781da177e4SLinus Torvalds  * particular irq has been exclusively allocated or is available
3791da177e4SLinus Torvalds  * for driver use.
3801da177e4SLinus Torvalds  */
3811da177e4SLinus Torvalds int can_request_irq(unsigned int irq, unsigned long irqflags)
3821da177e4SLinus Torvalds {
383d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
3841da177e4SLinus Torvalds 	struct irqaction *action;
385cc8c3b78SThomas Gleixner 	unsigned long flags;
3861da177e4SLinus Torvalds 
3877d94f7caSYinghai Lu 	if (!desc)
3887d94f7caSYinghai Lu 		return 0;
3897d94f7caSYinghai Lu 
3907d94f7caSYinghai Lu 	if (desc->status & IRQ_NOREQUEST)
3911da177e4SLinus Torvalds 		return 0;
3921da177e4SLinus Torvalds 
393cc8c3b78SThomas Gleixner 	raw_spin_lock_irqsave(&desc->lock, flags);
39408678b08SYinghai Lu 	action = desc->action;
3951da177e4SLinus Torvalds 	if (action)
3963cca53b0SThomas Gleixner 		if (irqflags & action->flags & IRQF_SHARED)
3971da177e4SLinus Torvalds 			action = NULL;
3981da177e4SLinus Torvalds 
399cc8c3b78SThomas Gleixner 	raw_spin_unlock_irqrestore(&desc->lock, flags);
400cc8c3b78SThomas Gleixner 
4011da177e4SLinus Torvalds 	return !action;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4046a6de9efSThomas Gleixner void compat_irq_chip_set_default_handler(struct irq_desc *desc)
4056a6de9efSThomas Gleixner {
4066a6de9efSThomas Gleixner 	/*
4076a6de9efSThomas Gleixner 	 * If the architecture still has not overriden
4086a6de9efSThomas Gleixner 	 * the flow handler then zap the default. This
4096a6de9efSThomas Gleixner 	 * should catch incorrect flow-type setting.
4106a6de9efSThomas Gleixner 	 */
4116a6de9efSThomas Gleixner 	if (desc->handle_irq == &handle_bad_irq)
4126a6de9efSThomas Gleixner 		desc->handle_irq = NULL;
4136a6de9efSThomas Gleixner }
4146a6de9efSThomas Gleixner 
4150c5d1eb7SDavid Brownell int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
41682736f4dSUwe Kleine-König 		unsigned long flags)
41782736f4dSUwe Kleine-König {
41882736f4dSUwe Kleine-König 	int ret;
4190c5d1eb7SDavid Brownell 	struct irq_chip *chip = desc->chip;
42082736f4dSUwe Kleine-König 
42182736f4dSUwe Kleine-König 	if (!chip || !chip->set_type) {
42282736f4dSUwe Kleine-König 		/*
42382736f4dSUwe Kleine-König 		 * IRQF_TRIGGER_* but the PIC does not support multiple
42482736f4dSUwe Kleine-König 		 * flow-types?
42582736f4dSUwe Kleine-König 		 */
4263ff68a6aSMark Nelson 		pr_debug("No set_type function for IRQ %d (%s)\n", irq,
42782736f4dSUwe Kleine-König 				chip ? (chip->name ? : "unknown") : "unknown");
42882736f4dSUwe Kleine-König 		return 0;
42982736f4dSUwe Kleine-König 	}
43082736f4dSUwe Kleine-König 
431f2b662daSDavid Brownell 	/* caller masked out all except trigger mode flags */
432f2b662daSDavid Brownell 	ret = chip->set_type(irq, flags);
43382736f4dSUwe Kleine-König 
43482736f4dSUwe Kleine-König 	if (ret)
435c69ad71bSDavid Brownell 		pr_err("setting trigger mode %d for irq %u failed (%pF)\n",
436f2b662daSDavid Brownell 				(int)flags, irq, chip->set_type);
4370c5d1eb7SDavid Brownell 	else {
438f2b662daSDavid Brownell 		if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
439f2b662daSDavid Brownell 			flags |= IRQ_LEVEL;
4400c5d1eb7SDavid Brownell 		/* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */
441f2b662daSDavid Brownell 		desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK);
442f2b662daSDavid Brownell 		desc->status |= flags;
4430c5d1eb7SDavid Brownell 	}
44482736f4dSUwe Kleine-König 
44582736f4dSUwe Kleine-König 	return ret;
44682736f4dSUwe Kleine-König }
44782736f4dSUwe Kleine-König 
448b25c340cSThomas Gleixner /*
449b25c340cSThomas Gleixner  * Default primary interrupt handler for threaded interrupts. Is
450b25c340cSThomas Gleixner  * assigned as primary handler when request_threaded_irq is called
451b25c340cSThomas Gleixner  * with handler == NULL. Useful for oneshot interrupts.
452b25c340cSThomas Gleixner  */
453b25c340cSThomas Gleixner static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
454b25c340cSThomas Gleixner {
455b25c340cSThomas Gleixner 	return IRQ_WAKE_THREAD;
456b25c340cSThomas Gleixner }
457b25c340cSThomas Gleixner 
458399b5da2SThomas Gleixner /*
459399b5da2SThomas Gleixner  * Primary handler for nested threaded interrupts. Should never be
460399b5da2SThomas Gleixner  * called.
461399b5da2SThomas Gleixner  */
462399b5da2SThomas Gleixner static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
463399b5da2SThomas Gleixner {
464399b5da2SThomas Gleixner 	WARN(1, "Primary handler called for nested irq %d\n", irq);
465399b5da2SThomas Gleixner 	return IRQ_NONE;
466399b5da2SThomas Gleixner }
467399b5da2SThomas Gleixner 
4683aa551c9SThomas Gleixner static int irq_wait_for_interrupt(struct irqaction *action)
4693aa551c9SThomas Gleixner {
4703aa551c9SThomas Gleixner 	while (!kthread_should_stop()) {
4713aa551c9SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
472f48fe81eSThomas Gleixner 
473f48fe81eSThomas Gleixner 		if (test_and_clear_bit(IRQTF_RUNTHREAD,
474f48fe81eSThomas Gleixner 				       &action->thread_flags)) {
4753aa551c9SThomas Gleixner 			__set_current_state(TASK_RUNNING);
4763aa551c9SThomas Gleixner 			return 0;
477f48fe81eSThomas Gleixner 		}
4783aa551c9SThomas Gleixner 		schedule();
4793aa551c9SThomas Gleixner 	}
4803aa551c9SThomas Gleixner 	return -1;
4813aa551c9SThomas Gleixner }
4823aa551c9SThomas Gleixner 
483b25c340cSThomas Gleixner /*
484b25c340cSThomas Gleixner  * Oneshot interrupts keep the irq line masked until the threaded
485b25c340cSThomas Gleixner  * handler finished. unmask if the interrupt has not been disabled and
486b25c340cSThomas Gleixner  * is marked MASKED.
487b25c340cSThomas Gleixner  */
488b25c340cSThomas Gleixner static void irq_finalize_oneshot(unsigned int irq, struct irq_desc *desc)
489b25c340cSThomas Gleixner {
4900b1adaa0SThomas Gleixner again:
49170aedd24SThomas Gleixner 	chip_bus_lock(irq, desc);
492239007b8SThomas Gleixner 	raw_spin_lock_irq(&desc->lock);
4930b1adaa0SThomas Gleixner 
4940b1adaa0SThomas Gleixner 	/*
4950b1adaa0SThomas Gleixner 	 * Implausible though it may be we need to protect us against
4960b1adaa0SThomas Gleixner 	 * the following scenario:
4970b1adaa0SThomas Gleixner 	 *
4980b1adaa0SThomas Gleixner 	 * The thread is faster done than the hard interrupt handler
4990b1adaa0SThomas Gleixner 	 * on the other CPU. If we unmask the irq line then the
5000b1adaa0SThomas Gleixner 	 * interrupt can come in again and masks the line, leaves due
5010b1adaa0SThomas Gleixner 	 * to IRQ_INPROGRESS and the irq line is masked forever.
5020b1adaa0SThomas Gleixner 	 */
5030b1adaa0SThomas Gleixner 	if (unlikely(desc->status & IRQ_INPROGRESS)) {
5040b1adaa0SThomas Gleixner 		raw_spin_unlock_irq(&desc->lock);
5050b1adaa0SThomas Gleixner 		chip_bus_sync_unlock(irq, desc);
5060b1adaa0SThomas Gleixner 		cpu_relax();
5070b1adaa0SThomas Gleixner 		goto again;
5080b1adaa0SThomas Gleixner 	}
5090b1adaa0SThomas Gleixner 
510b25c340cSThomas Gleixner 	if (!(desc->status & IRQ_DISABLED) && (desc->status & IRQ_MASKED)) {
511b25c340cSThomas Gleixner 		desc->status &= ~IRQ_MASKED;
512b25c340cSThomas Gleixner 		desc->chip->unmask(irq);
513b25c340cSThomas Gleixner 	}
514239007b8SThomas Gleixner 	raw_spin_unlock_irq(&desc->lock);
51570aedd24SThomas Gleixner 	chip_bus_sync_unlock(irq, desc);
516b25c340cSThomas Gleixner }
517b25c340cSThomas Gleixner 
51861f38261SBruno Premont #ifdef CONFIG_SMP
5193aa551c9SThomas Gleixner /*
520591d2fb0SThomas Gleixner  * Check whether we need to change the affinity of the interrupt thread.
521591d2fb0SThomas Gleixner  */
522591d2fb0SThomas Gleixner static void
523591d2fb0SThomas Gleixner irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
524591d2fb0SThomas Gleixner {
525591d2fb0SThomas Gleixner 	cpumask_var_t mask;
526591d2fb0SThomas Gleixner 
527591d2fb0SThomas Gleixner 	if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
528591d2fb0SThomas Gleixner 		return;
529591d2fb0SThomas Gleixner 
530591d2fb0SThomas Gleixner 	/*
531591d2fb0SThomas Gleixner 	 * In case we are out of memory we set IRQTF_AFFINITY again and
532591d2fb0SThomas Gleixner 	 * try again next time
533591d2fb0SThomas Gleixner 	 */
534591d2fb0SThomas Gleixner 	if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
535591d2fb0SThomas Gleixner 		set_bit(IRQTF_AFFINITY, &action->thread_flags);
536591d2fb0SThomas Gleixner 		return;
537591d2fb0SThomas Gleixner 	}
538591d2fb0SThomas Gleixner 
539239007b8SThomas Gleixner 	raw_spin_lock_irq(&desc->lock);
540591d2fb0SThomas Gleixner 	cpumask_copy(mask, desc->affinity);
541239007b8SThomas Gleixner 	raw_spin_unlock_irq(&desc->lock);
542591d2fb0SThomas Gleixner 
543591d2fb0SThomas Gleixner 	set_cpus_allowed_ptr(current, mask);
544591d2fb0SThomas Gleixner 	free_cpumask_var(mask);
545591d2fb0SThomas Gleixner }
54661f38261SBruno Premont #else
54761f38261SBruno Premont static inline void
54861f38261SBruno Premont irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
54961f38261SBruno Premont #endif
550591d2fb0SThomas Gleixner 
551591d2fb0SThomas Gleixner /*
5523aa551c9SThomas Gleixner  * Interrupt handler thread
5533aa551c9SThomas Gleixner  */
5543aa551c9SThomas Gleixner static int irq_thread(void *data)
5553aa551c9SThomas Gleixner {
5563aa551c9SThomas Gleixner 	struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, };
5573aa551c9SThomas Gleixner 	struct irqaction *action = data;
5583aa551c9SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(action->irq);
559b25c340cSThomas Gleixner 	int wake, oneshot = desc->status & IRQ_ONESHOT;
5603aa551c9SThomas Gleixner 
5613aa551c9SThomas Gleixner 	sched_setscheduler(current, SCHED_FIFO, &param);
5623aa551c9SThomas Gleixner 	current->irqaction = action;
5633aa551c9SThomas Gleixner 
5643aa551c9SThomas Gleixner 	while (!irq_wait_for_interrupt(action)) {
5653aa551c9SThomas Gleixner 
566591d2fb0SThomas Gleixner 		irq_thread_check_affinity(desc, action);
567591d2fb0SThomas Gleixner 
5683aa551c9SThomas Gleixner 		atomic_inc(&desc->threads_active);
5693aa551c9SThomas Gleixner 
570239007b8SThomas Gleixner 		raw_spin_lock_irq(&desc->lock);
5713aa551c9SThomas Gleixner 		if (unlikely(desc->status & IRQ_DISABLED)) {
5723aa551c9SThomas Gleixner 			/*
5733aa551c9SThomas Gleixner 			 * CHECKME: We might need a dedicated
5743aa551c9SThomas Gleixner 			 * IRQ_THREAD_PENDING flag here, which
5753aa551c9SThomas Gleixner 			 * retriggers the thread in check_irq_resend()
5763aa551c9SThomas Gleixner 			 * but AFAICT IRQ_PENDING should be fine as it
5773aa551c9SThomas Gleixner 			 * retriggers the interrupt itself --- tglx
5783aa551c9SThomas Gleixner 			 */
5793aa551c9SThomas Gleixner 			desc->status |= IRQ_PENDING;
580239007b8SThomas Gleixner 			raw_spin_unlock_irq(&desc->lock);
5813aa551c9SThomas Gleixner 		} else {
582239007b8SThomas Gleixner 			raw_spin_unlock_irq(&desc->lock);
5833aa551c9SThomas Gleixner 
5843aa551c9SThomas Gleixner 			action->thread_fn(action->irq, action->dev_id);
585b25c340cSThomas Gleixner 
586b25c340cSThomas Gleixner 			if (oneshot)
587b25c340cSThomas Gleixner 				irq_finalize_oneshot(action->irq, desc);
5883aa551c9SThomas Gleixner 		}
5893aa551c9SThomas Gleixner 
5903aa551c9SThomas Gleixner 		wake = atomic_dec_and_test(&desc->threads_active);
5913aa551c9SThomas Gleixner 
5923aa551c9SThomas Gleixner 		if (wake && waitqueue_active(&desc->wait_for_threads))
5933aa551c9SThomas Gleixner 			wake_up(&desc->wait_for_threads);
5943aa551c9SThomas Gleixner 	}
5953aa551c9SThomas Gleixner 
5963aa551c9SThomas Gleixner 	/*
5973aa551c9SThomas Gleixner 	 * Clear irqaction. Otherwise exit_irq_thread() would make
5983aa551c9SThomas Gleixner 	 * fuzz about an active irq thread going into nirvana.
5993aa551c9SThomas Gleixner 	 */
6003aa551c9SThomas Gleixner 	current->irqaction = NULL;
6013aa551c9SThomas Gleixner 	return 0;
6023aa551c9SThomas Gleixner }
6033aa551c9SThomas Gleixner 
6043aa551c9SThomas Gleixner /*
6053aa551c9SThomas Gleixner  * Called from do_exit()
6063aa551c9SThomas Gleixner  */
6073aa551c9SThomas Gleixner void exit_irq_thread(void)
6083aa551c9SThomas Gleixner {
6093aa551c9SThomas Gleixner 	struct task_struct *tsk = current;
6103aa551c9SThomas Gleixner 
6113aa551c9SThomas Gleixner 	if (!tsk->irqaction)
6123aa551c9SThomas Gleixner 		return;
6133aa551c9SThomas Gleixner 
6143aa551c9SThomas Gleixner 	printk(KERN_ERR
6153aa551c9SThomas Gleixner 	       "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
6163aa551c9SThomas Gleixner 	       tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq);
6173aa551c9SThomas Gleixner 
6183aa551c9SThomas Gleixner 	/*
6193aa551c9SThomas Gleixner 	 * Set the THREAD DIED flag to prevent further wakeups of the
6203aa551c9SThomas Gleixner 	 * soon to be gone threaded handler.
6213aa551c9SThomas Gleixner 	 */
6223aa551c9SThomas Gleixner 	set_bit(IRQTF_DIED, &tsk->irqaction->flags);
6233aa551c9SThomas Gleixner }
6243aa551c9SThomas Gleixner 
6251da177e4SLinus Torvalds /*
6261da177e4SLinus Torvalds  * Internal function to register an irqaction - typically used to
6271da177e4SLinus Torvalds  * allocate special interrupts that are part of the architecture.
6281da177e4SLinus Torvalds  */
629d3c60047SThomas Gleixner static int
630d3c60047SThomas Gleixner __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
6311da177e4SLinus Torvalds {
632f17c7545SIngo Molnar 	struct irqaction *old, **old_ptr;
6338b126b77SAndrew Morton 	const char *old_name = NULL;
6341da177e4SLinus Torvalds 	unsigned long flags;
635399b5da2SThomas Gleixner 	int nested, shared = 0;
63682736f4dSUwe Kleine-König 	int ret;
6371da177e4SLinus Torvalds 
6387d94f7caSYinghai Lu 	if (!desc)
639c2b5a251SMatthew Wilcox 		return -EINVAL;
640c2b5a251SMatthew Wilcox 
641f1c2662cSIngo Molnar 	if (desc->chip == &no_irq_chip)
6421da177e4SLinus Torvalds 		return -ENOSYS;
6431da177e4SLinus Torvalds 	/*
6441da177e4SLinus Torvalds 	 * Some drivers like serial.c use request_irq() heavily,
6451da177e4SLinus Torvalds 	 * so we have to be careful not to interfere with a
6461da177e4SLinus Torvalds 	 * running system.
6471da177e4SLinus Torvalds 	 */
6483cca53b0SThomas Gleixner 	if (new->flags & IRQF_SAMPLE_RANDOM) {
6491da177e4SLinus Torvalds 		/*
6501da177e4SLinus Torvalds 		 * This function might sleep, we want to call it first,
6511da177e4SLinus Torvalds 		 * outside of the atomic block.
6521da177e4SLinus Torvalds 		 * Yes, this might clear the entropy pool if the wrong
6531da177e4SLinus Torvalds 		 * driver is attempted to be loaded, without actually
6541da177e4SLinus Torvalds 		 * installing a new handler, but is this really a problem,
6551da177e4SLinus Torvalds 		 * only the sysadmin is able to do this.
6561da177e4SLinus Torvalds 		 */
6571da177e4SLinus Torvalds 		rand_initialize_irq(irq);
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds 
660b25c340cSThomas Gleixner 	/* Oneshot interrupts are not allowed with shared */
661b25c340cSThomas Gleixner 	if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED))
662b25c340cSThomas Gleixner 		return -EINVAL;
663b25c340cSThomas Gleixner 
6641da177e4SLinus Torvalds 	/*
665399b5da2SThomas Gleixner 	 * Check whether the interrupt nests into another interrupt
666399b5da2SThomas Gleixner 	 * thread.
6673aa551c9SThomas Gleixner 	 */
668399b5da2SThomas Gleixner 	nested = desc->status & IRQ_NESTED_THREAD;
669399b5da2SThomas Gleixner 	if (nested) {
670399b5da2SThomas Gleixner 		if (!new->thread_fn)
671399b5da2SThomas Gleixner 			return -EINVAL;
672399b5da2SThomas Gleixner 		/*
673399b5da2SThomas Gleixner 		 * Replace the primary handler which was provided from
674399b5da2SThomas Gleixner 		 * the driver for non nested interrupt handling by the
675399b5da2SThomas Gleixner 		 * dummy function which warns when called.
676399b5da2SThomas Gleixner 		 */
677399b5da2SThomas Gleixner 		new->handler = irq_nested_primary_handler;
678399b5da2SThomas Gleixner 	}
679399b5da2SThomas Gleixner 
680399b5da2SThomas Gleixner 	/*
681399b5da2SThomas Gleixner 	 * Create a handler thread when a thread function is supplied
682399b5da2SThomas Gleixner 	 * and the interrupt does not nest into another interrupt
683399b5da2SThomas Gleixner 	 * thread.
684399b5da2SThomas Gleixner 	 */
685399b5da2SThomas Gleixner 	if (new->thread_fn && !nested) {
6863aa551c9SThomas Gleixner 		struct task_struct *t;
6873aa551c9SThomas Gleixner 
6883aa551c9SThomas Gleixner 		t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
6893aa551c9SThomas Gleixner 				   new->name);
6903aa551c9SThomas Gleixner 		if (IS_ERR(t))
6913aa551c9SThomas Gleixner 			return PTR_ERR(t);
6923aa551c9SThomas Gleixner 		/*
6933aa551c9SThomas Gleixner 		 * We keep the reference to the task struct even if
6943aa551c9SThomas Gleixner 		 * the thread dies to avoid that the interrupt code
6953aa551c9SThomas Gleixner 		 * references an already freed task_struct.
6963aa551c9SThomas Gleixner 		 */
6973aa551c9SThomas Gleixner 		get_task_struct(t);
6983aa551c9SThomas Gleixner 		new->thread = t;
6993aa551c9SThomas Gleixner 	}
7003aa551c9SThomas Gleixner 
7013aa551c9SThomas Gleixner 	/*
7021da177e4SLinus Torvalds 	 * The following block of code has to be executed atomically
7031da177e4SLinus Torvalds 	 */
704239007b8SThomas Gleixner 	raw_spin_lock_irqsave(&desc->lock, flags);
705f17c7545SIngo Molnar 	old_ptr = &desc->action;
706f17c7545SIngo Molnar 	old = *old_ptr;
70706fcb0c6SIngo Molnar 	if (old) {
708e76de9f8SThomas Gleixner 		/*
709e76de9f8SThomas Gleixner 		 * Can't share interrupts unless both agree to and are
710e76de9f8SThomas Gleixner 		 * the same type (level, edge, polarity). So both flag
7113cca53b0SThomas Gleixner 		 * fields must have IRQF_SHARED set and the bits which
712e76de9f8SThomas Gleixner 		 * set the trigger type must match.
713e76de9f8SThomas Gleixner 		 */
7143cca53b0SThomas Gleixner 		if (!((old->flags & new->flags) & IRQF_SHARED) ||
7158b126b77SAndrew Morton 		    ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
7168b126b77SAndrew Morton 			old_name = old->name;
717f5163427SDimitri Sivanich 			goto mismatch;
7188b126b77SAndrew Morton 		}
719f5163427SDimitri Sivanich 
720284c6680SThomas Gleixner #if defined(CONFIG_IRQ_PER_CPU)
721f5163427SDimitri Sivanich 		/* All handlers must agree on per-cpuness */
7223cca53b0SThomas Gleixner 		if ((old->flags & IRQF_PERCPU) !=
7233cca53b0SThomas Gleixner 		    (new->flags & IRQF_PERCPU))
724f5163427SDimitri Sivanich 			goto mismatch;
725f5163427SDimitri Sivanich #endif
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 		/* add new interrupt at end of irq queue */
7281da177e4SLinus Torvalds 		do {
729f17c7545SIngo Molnar 			old_ptr = &old->next;
730f17c7545SIngo Molnar 			old = *old_ptr;
7311da177e4SLinus Torvalds 		} while (old);
7321da177e4SLinus Torvalds 		shared = 1;
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if (!shared) {
7366a6de9efSThomas Gleixner 		irq_chip_set_defaults(desc->chip);
737e76de9f8SThomas Gleixner 
7383aa551c9SThomas Gleixner 		init_waitqueue_head(&desc->wait_for_threads);
7393aa551c9SThomas Gleixner 
74082736f4dSUwe Kleine-König 		/* Setup the type (level, edge polarity) if configured: */
74182736f4dSUwe Kleine-König 		if (new->flags & IRQF_TRIGGER_MASK) {
742f2b662daSDavid Brownell 			ret = __irq_set_trigger(desc, irq,
743f2b662daSDavid Brownell 					new->flags & IRQF_TRIGGER_MASK);
74482736f4dSUwe Kleine-König 
7453aa551c9SThomas Gleixner 			if (ret)
7463aa551c9SThomas Gleixner 				goto out_thread;
74782736f4dSUwe Kleine-König 		} else
74882736f4dSUwe Kleine-König 			compat_irq_chip_set_default_handler(desc);
749f75d222bSAhmed S. Darwish #if defined(CONFIG_IRQ_PER_CPU)
750f75d222bSAhmed S. Darwish 		if (new->flags & IRQF_PERCPU)
751f75d222bSAhmed S. Darwish 			desc->status |= IRQ_PER_CPU;
752f75d222bSAhmed S. Darwish #endif
753f75d222bSAhmed S. Darwish 
754b25c340cSThomas Gleixner 		desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | IRQ_ONESHOT |
7551adb0850SThomas Gleixner 				  IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);
75694d39e1fSThomas Gleixner 
757b25c340cSThomas Gleixner 		if (new->flags & IRQF_ONESHOT)
758b25c340cSThomas Gleixner 			desc->status |= IRQ_ONESHOT;
759b25c340cSThomas Gleixner 
760753649dbSThomas Gleixner 		/*
761753649dbSThomas Gleixner 		 * Force MSI interrupts to run with interrupts
762753649dbSThomas Gleixner 		 * disabled. The multi vector cards can cause stack
763753649dbSThomas Gleixner 		 * overflows due to nested interrupts when enough of
764753649dbSThomas Gleixner 		 * them are directed to a core and fire at the same
765753649dbSThomas Gleixner 		 * time.
766753649dbSThomas Gleixner 		 */
767753649dbSThomas Gleixner 		if (desc->msi_desc)
768753649dbSThomas Gleixner 			new->flags |= IRQF_DISABLED;
769753649dbSThomas Gleixner 
77094d39e1fSThomas Gleixner 		if (!(desc->status & IRQ_NOAUTOEN)) {
7711da177e4SLinus Torvalds 			desc->depth = 0;
77294d39e1fSThomas Gleixner 			desc->status &= ~IRQ_DISABLED;
773d1bef4edSIngo Molnar 			desc->chip->startup(irq);
774e76de9f8SThomas Gleixner 		} else
775e76de9f8SThomas Gleixner 			/* Undo nested disables: */
776e76de9f8SThomas Gleixner 			desc->depth = 1;
77718404756SMax Krasnyansky 
778612e3684SThomas Gleixner 		/* Exclude IRQ from balancing if requested */
779612e3684SThomas Gleixner 		if (new->flags & IRQF_NOBALANCING)
780612e3684SThomas Gleixner 			desc->status |= IRQ_NO_BALANCING;
781612e3684SThomas Gleixner 
78218404756SMax Krasnyansky 		/* Set default affinity mask once everything is setup */
783548c8933SHannes Eder 		setup_affinity(irq, desc);
7840c5d1eb7SDavid Brownell 
7850c5d1eb7SDavid Brownell 	} else if ((new->flags & IRQF_TRIGGER_MASK)
7860c5d1eb7SDavid Brownell 			&& (new->flags & IRQF_TRIGGER_MASK)
7870c5d1eb7SDavid Brownell 				!= (desc->status & IRQ_TYPE_SENSE_MASK)) {
7880c5d1eb7SDavid Brownell 		/* hope the handler works with the actual trigger mode... */
7890c5d1eb7SDavid Brownell 		pr_warning("IRQ %d uses trigger mode %d; requested %d\n",
7900c5d1eb7SDavid Brownell 				irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK),
7910c5d1eb7SDavid Brownell 				(int)(new->flags & IRQF_TRIGGER_MASK));
79294d39e1fSThomas Gleixner 	}
79382736f4dSUwe Kleine-König 
79469ab8494SThomas Gleixner 	new->irq = irq;
795f17c7545SIngo Molnar 	*old_ptr = new;
79682736f4dSUwe Kleine-König 
7978528b0f1SLinus Torvalds 	/* Reset broken irq detection when installing new handler */
7988528b0f1SLinus Torvalds 	desc->irq_count = 0;
7998528b0f1SLinus Torvalds 	desc->irqs_unhandled = 0;
8001adb0850SThomas Gleixner 
8011adb0850SThomas Gleixner 	/*
8021adb0850SThomas Gleixner 	 * Check whether we disabled the irq via the spurious handler
8031adb0850SThomas Gleixner 	 * before. Reenable it and give it another chance.
8041adb0850SThomas Gleixner 	 */
8051adb0850SThomas Gleixner 	if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {
8061adb0850SThomas Gleixner 		desc->status &= ~IRQ_SPURIOUS_DISABLED;
8070a0c5168SRafael J. Wysocki 		__enable_irq(desc, irq, false);
8081adb0850SThomas Gleixner 	}
8091adb0850SThomas Gleixner 
810239007b8SThomas Gleixner 	raw_spin_unlock_irqrestore(&desc->lock, flags);
8111da177e4SLinus Torvalds 
81269ab8494SThomas Gleixner 	/*
81369ab8494SThomas Gleixner 	 * Strictly no need to wake it up, but hung_task complains
81469ab8494SThomas Gleixner 	 * when no hard interrupt wakes the thread up.
81569ab8494SThomas Gleixner 	 */
81669ab8494SThomas Gleixner 	if (new->thread)
81769ab8494SThomas Gleixner 		wake_up_process(new->thread);
81869ab8494SThomas Gleixner 
8192c6927a3SYinghai Lu 	register_irq_proc(irq, desc);
8201da177e4SLinus Torvalds 	new->dir = NULL;
8211da177e4SLinus Torvalds 	register_handler_proc(irq, new);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	return 0;
824f5163427SDimitri Sivanich 
825f5163427SDimitri Sivanich mismatch:
8263f050447SAlan Cox #ifdef CONFIG_DEBUG_SHIRQ
8273cca53b0SThomas Gleixner 	if (!(new->flags & IRQF_PROBE_SHARED)) {
828e8c4b9d0SBjorn Helgaas 		printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
8298b126b77SAndrew Morton 		if (old_name)
8308b126b77SAndrew Morton 			printk(KERN_ERR "current handler: %s\n", old_name);
831f5163427SDimitri Sivanich 		dump_stack();
83213e87ec6SAndrew Morton 	}
8333f050447SAlan Cox #endif
8343aa551c9SThomas Gleixner 	ret = -EBUSY;
8353aa551c9SThomas Gleixner 
8363aa551c9SThomas Gleixner out_thread:
837239007b8SThomas Gleixner 	raw_spin_unlock_irqrestore(&desc->lock, flags);
8383aa551c9SThomas Gleixner 	if (new->thread) {
8393aa551c9SThomas Gleixner 		struct task_struct *t = new->thread;
8403aa551c9SThomas Gleixner 
8413aa551c9SThomas Gleixner 		new->thread = NULL;
8423aa551c9SThomas Gleixner 		if (likely(!test_bit(IRQTF_DIED, &new->thread_flags)))
8433aa551c9SThomas Gleixner 			kthread_stop(t);
8443aa551c9SThomas Gleixner 		put_task_struct(t);
8453aa551c9SThomas Gleixner 	}
8463aa551c9SThomas Gleixner 	return ret;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /**
850d3c60047SThomas Gleixner  *	setup_irq - setup an interrupt
851d3c60047SThomas Gleixner  *	@irq: Interrupt line to setup
852d3c60047SThomas Gleixner  *	@act: irqaction for the interrupt
853d3c60047SThomas Gleixner  *
854d3c60047SThomas Gleixner  * Used to statically setup interrupts in the early boot process.
855d3c60047SThomas Gleixner  */
856d3c60047SThomas Gleixner int setup_irq(unsigned int irq, struct irqaction *act)
857d3c60047SThomas Gleixner {
858d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
859d3c60047SThomas Gleixner 
860d3c60047SThomas Gleixner 	return __setup_irq(irq, desc, act);
861d3c60047SThomas Gleixner }
862eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(setup_irq);
863d3c60047SThomas Gleixner 
864cbf94f06SMagnus Damm  /*
865cbf94f06SMagnus Damm  * Internal function to unregister an irqaction - used to free
866cbf94f06SMagnus Damm  * regular and special interrupts that are part of the architecture.
8671da177e4SLinus Torvalds  */
868cbf94f06SMagnus Damm static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
8691da177e4SLinus Torvalds {
870d3c60047SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
871f17c7545SIngo Molnar 	struct irqaction *action, **action_ptr;
8721da177e4SLinus Torvalds 	unsigned long flags;
8731da177e4SLinus Torvalds 
874ae88a23bSIngo Molnar 	WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
8757d94f7caSYinghai Lu 
8767d94f7caSYinghai Lu 	if (!desc)
877f21cfb25SMagnus Damm 		return NULL;
8781da177e4SLinus Torvalds 
879239007b8SThomas Gleixner 	raw_spin_lock_irqsave(&desc->lock, flags);
880ae88a23bSIngo Molnar 
881ae88a23bSIngo Molnar 	/*
882ae88a23bSIngo Molnar 	 * There can be multiple actions per IRQ descriptor, find the right
883ae88a23bSIngo Molnar 	 * one based on the dev_id:
884ae88a23bSIngo Molnar 	 */
885f17c7545SIngo Molnar 	action_ptr = &desc->action;
8861da177e4SLinus Torvalds 	for (;;) {
887f17c7545SIngo Molnar 		action = *action_ptr;
8881da177e4SLinus Torvalds 
889ae88a23bSIngo Molnar 		if (!action) {
890ae88a23bSIngo Molnar 			WARN(1, "Trying to free already-free IRQ %d\n", irq);
891239007b8SThomas Gleixner 			raw_spin_unlock_irqrestore(&desc->lock, flags);
892ae88a23bSIngo Molnar 
893f21cfb25SMagnus Damm 			return NULL;
894ae88a23bSIngo Molnar 		}
8951da177e4SLinus Torvalds 
8968316e381SIngo Molnar 		if (action->dev_id == dev_id)
897ae88a23bSIngo Molnar 			break;
898f17c7545SIngo Molnar 		action_ptr = &action->next;
899ae88a23bSIngo Molnar 	}
900ae88a23bSIngo Molnar 
901ae88a23bSIngo Molnar 	/* Found it - now remove it from the list of entries: */
902f17c7545SIngo Molnar 	*action_ptr = action->next;
903dbce706eSPaolo 'Blaisorblade' Giarrusso 
904ae88a23bSIngo Molnar 	/* Currently used only by UML, might disappear one day: */
905b77d6adcSPaolo 'Blaisorblade' Giarrusso #ifdef CONFIG_IRQ_RELEASE_METHOD
906d1bef4edSIngo Molnar 	if (desc->chip->release)
907d1bef4edSIngo Molnar 		desc->chip->release(irq, dev_id);
908b77d6adcSPaolo 'Blaisorblade' Giarrusso #endif
909dbce706eSPaolo 'Blaisorblade' Giarrusso 
910ae88a23bSIngo Molnar 	/* If this was the last handler, shut down the IRQ line: */
9111da177e4SLinus Torvalds 	if (!desc->action) {
9121da177e4SLinus Torvalds 		desc->status |= IRQ_DISABLED;
913d1bef4edSIngo Molnar 		if (desc->chip->shutdown)
914d1bef4edSIngo Molnar 			desc->chip->shutdown(irq);
9151da177e4SLinus Torvalds 		else
916d1bef4edSIngo Molnar 			desc->chip->disable(irq);
9171da177e4SLinus Torvalds 	}
9183aa551c9SThomas Gleixner 
919239007b8SThomas Gleixner 	raw_spin_unlock_irqrestore(&desc->lock, flags);
920ae88a23bSIngo Molnar 
9211da177e4SLinus Torvalds 	unregister_handler_proc(irq, action);
9221da177e4SLinus Torvalds 
923ae88a23bSIngo Molnar 	/* Make sure it's not being used on another CPU: */
9241da177e4SLinus Torvalds 	synchronize_irq(irq);
925ae88a23bSIngo Molnar 
9261d99493bSDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ
9271d99493bSDavid Woodhouse 	/*
928ae88a23bSIngo Molnar 	 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
929ae88a23bSIngo Molnar 	 * event to happen even now it's being freed, so let's make sure that
930ae88a23bSIngo Molnar 	 * is so by doing an extra call to the handler ....
931ae88a23bSIngo Molnar 	 *
932ae88a23bSIngo Molnar 	 * ( We do this after actually deregistering it, to make sure that a
933ae88a23bSIngo Molnar 	 *   'real' IRQ doesn't run in * parallel with our fake. )
9341d99493bSDavid Woodhouse 	 */
9351d99493bSDavid Woodhouse 	if (action->flags & IRQF_SHARED) {
9361d99493bSDavid Woodhouse 		local_irq_save(flags);
9371d99493bSDavid Woodhouse 		action->handler(irq, dev_id);
9381d99493bSDavid Woodhouse 		local_irq_restore(flags);
9391d99493bSDavid Woodhouse 	}
9401d99493bSDavid Woodhouse #endif
9412d860ad7SLinus Torvalds 
9422d860ad7SLinus Torvalds 	if (action->thread) {
9432d860ad7SLinus Torvalds 		if (!test_bit(IRQTF_DIED, &action->thread_flags))
9442d860ad7SLinus Torvalds 			kthread_stop(action->thread);
9452d860ad7SLinus Torvalds 		put_task_struct(action->thread);
9462d860ad7SLinus Torvalds 	}
9472d860ad7SLinus Torvalds 
948f21cfb25SMagnus Damm 	return action;
949f21cfb25SMagnus Damm }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds /**
952cbf94f06SMagnus Damm  *	remove_irq - free an interrupt
953cbf94f06SMagnus Damm  *	@irq: Interrupt line to free
954cbf94f06SMagnus Damm  *	@act: irqaction for the interrupt
955cbf94f06SMagnus Damm  *
956cbf94f06SMagnus Damm  * Used to remove interrupts statically setup by the early boot process.
957cbf94f06SMagnus Damm  */
958cbf94f06SMagnus Damm void remove_irq(unsigned int irq, struct irqaction *act)
959cbf94f06SMagnus Damm {
960cbf94f06SMagnus Damm 	__free_irq(irq, act->dev_id);
961cbf94f06SMagnus Damm }
962eb53b4e8SMagnus Damm EXPORT_SYMBOL_GPL(remove_irq);
963cbf94f06SMagnus Damm 
964cbf94f06SMagnus Damm /**
965f21cfb25SMagnus Damm  *	free_irq - free an interrupt allocated with request_irq
9661da177e4SLinus Torvalds  *	@irq: Interrupt line to free
9671da177e4SLinus Torvalds  *	@dev_id: Device identity to free
9681da177e4SLinus Torvalds  *
9691da177e4SLinus Torvalds  *	Remove an interrupt handler. The handler is removed and if the
9701da177e4SLinus Torvalds  *	interrupt line is no longer in use by any driver it is disabled.
9711da177e4SLinus Torvalds  *	On a shared IRQ the caller must ensure the interrupt is disabled
9721da177e4SLinus Torvalds  *	on the card it drives before calling this function. The function
9731da177e4SLinus Torvalds  *	does not return until any executing interrupts for this IRQ
9741da177e4SLinus Torvalds  *	have completed.
9751da177e4SLinus Torvalds  *
9761da177e4SLinus Torvalds  *	This function must not be called from interrupt context.
9771da177e4SLinus Torvalds  */
9781da177e4SLinus Torvalds void free_irq(unsigned int irq, void *dev_id)
9791da177e4SLinus Torvalds {
98070aedd24SThomas Gleixner 	struct irq_desc *desc = irq_to_desc(irq);
98170aedd24SThomas Gleixner 
98270aedd24SThomas Gleixner 	if (!desc)
98370aedd24SThomas Gleixner 		return;
98470aedd24SThomas Gleixner 
98570aedd24SThomas Gleixner 	chip_bus_lock(irq, desc);
986cbf94f06SMagnus Damm 	kfree(__free_irq(irq, dev_id));
98770aedd24SThomas Gleixner 	chip_bus_sync_unlock(irq, desc);
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds EXPORT_SYMBOL(free_irq);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds /**
9923aa551c9SThomas Gleixner  *	request_threaded_irq - allocate an interrupt line
9931da177e4SLinus Torvalds  *	@irq: Interrupt line to allocate
9943aa551c9SThomas Gleixner  *	@handler: Function to be called when the IRQ occurs.
9953aa551c9SThomas Gleixner  *		  Primary handler for threaded interrupts
996b25c340cSThomas Gleixner  *		  If NULL and thread_fn != NULL the default
997b25c340cSThomas Gleixner  *		  primary handler is installed
9983aa551c9SThomas Gleixner  *	@thread_fn: Function called from the irq handler thread
9993aa551c9SThomas Gleixner  *		    If NULL, no irq thread is created
10001da177e4SLinus Torvalds  *	@irqflags: Interrupt type flags
10011da177e4SLinus Torvalds  *	@devname: An ascii name for the claiming device
10021da177e4SLinus Torvalds  *	@dev_id: A cookie passed back to the handler function
10031da177e4SLinus Torvalds  *
10041da177e4SLinus Torvalds  *	This call allocates interrupt resources and enables the
10051da177e4SLinus Torvalds  *	interrupt line and IRQ handling. From the point this
10061da177e4SLinus Torvalds  *	call is made your handler function may be invoked. Since
10071da177e4SLinus Torvalds  *	your handler function must clear any interrupt the board
10081da177e4SLinus Torvalds  *	raises, you must take care both to initialise your hardware
10091da177e4SLinus Torvalds  *	and to set up the interrupt handler in the right order.
10101da177e4SLinus Torvalds  *
10113aa551c9SThomas Gleixner  *	If you want to set up a threaded irq handler for your device
10123aa551c9SThomas Gleixner  *	then you need to supply @handler and @thread_fn. @handler ist
10133aa551c9SThomas Gleixner  *	still called in hard interrupt context and has to check
10143aa551c9SThomas Gleixner  *	whether the interrupt originates from the device. If yes it
10153aa551c9SThomas Gleixner  *	needs to disable the interrupt on the device and return
101639a2eddbSSteven Rostedt  *	IRQ_WAKE_THREAD which will wake up the handler thread and run
10173aa551c9SThomas Gleixner  *	@thread_fn. This split handler design is necessary to support
10183aa551c9SThomas Gleixner  *	shared interrupts.
10193aa551c9SThomas Gleixner  *
10201da177e4SLinus Torvalds  *	Dev_id must be globally unique. Normally the address of the
10211da177e4SLinus Torvalds  *	device data structure is used as the cookie. Since the handler
10221da177e4SLinus Torvalds  *	receives this value it makes sense to use it.
10231da177e4SLinus Torvalds  *
10241da177e4SLinus Torvalds  *	If your interrupt is shared you must pass a non NULL dev_id
10251da177e4SLinus Torvalds  *	as this is required when freeing the interrupt.
10261da177e4SLinus Torvalds  *
10271da177e4SLinus Torvalds  *	Flags:
10281da177e4SLinus Torvalds  *
10293cca53b0SThomas Gleixner  *	IRQF_SHARED		Interrupt is shared
10303cca53b0SThomas Gleixner  *	IRQF_DISABLED	Disable local interrupts while processing
10313cca53b0SThomas Gleixner  *	IRQF_SAMPLE_RANDOM	The interrupt can be used for entropy
10320c5d1eb7SDavid Brownell  *	IRQF_TRIGGER_*		Specify active edge(s) or level
10331da177e4SLinus Torvalds  *
10341da177e4SLinus Torvalds  */
10353aa551c9SThomas Gleixner int request_threaded_irq(unsigned int irq, irq_handler_t handler,
10363aa551c9SThomas Gleixner 			 irq_handler_t thread_fn, unsigned long irqflags,
10373aa551c9SThomas Gleixner 			 const char *devname, void *dev_id)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	struct irqaction *action;
104008678b08SYinghai Lu 	struct irq_desc *desc;
1041d3c60047SThomas Gleixner 	int retval;
10421da177e4SLinus Torvalds 
1043470c6623SDavid Brownell 	/*
1044470c6623SDavid Brownell 	 * handle_IRQ_event() always ignores IRQF_DISABLED except for
1045470c6623SDavid Brownell 	 * the _first_ irqaction (sigh).  That can cause oopsing, but
1046470c6623SDavid Brownell 	 * the behavior is classified as "will not fix" so we need to
1047470c6623SDavid Brownell 	 * start nudging drivers away from using that idiom.
1048470c6623SDavid Brownell 	 */
1049327ec569SIngo Molnar 	if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) ==
1050327ec569SIngo Molnar 					(IRQF_SHARED|IRQF_DISABLED)) {
1051327ec569SIngo Molnar 		pr_warning(
1052327ec569SIngo Molnar 		  "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n",
1053470c6623SDavid Brownell 			irq, devname);
1054327ec569SIngo Molnar 	}
1055470c6623SDavid Brownell 
1056fbb9ce95SIngo Molnar #ifdef CONFIG_LOCKDEP
1057fbb9ce95SIngo Molnar 	/*
1058fbb9ce95SIngo Molnar 	 * Lockdep wants atomic interrupt handlers:
1059fbb9ce95SIngo Molnar 	 */
106038515e90SThomas Gleixner 	irqflags |= IRQF_DISABLED;
1061fbb9ce95SIngo Molnar #endif
10621da177e4SLinus Torvalds 	/*
10631da177e4SLinus Torvalds 	 * Sanity-check: shared interrupts must pass in a real dev-ID,
10641da177e4SLinus Torvalds 	 * otherwise we'll have trouble later trying to figure out
10651da177e4SLinus Torvalds 	 * which interrupt is which (messes up the interrupt freeing
10661da177e4SLinus Torvalds 	 * logic etc).
10671da177e4SLinus Torvalds 	 */
10683cca53b0SThomas Gleixner 	if ((irqflags & IRQF_SHARED) && !dev_id)
10691da177e4SLinus Torvalds 		return -EINVAL;
10707d94f7caSYinghai Lu 
1071cb5bc832SYinghai Lu 	desc = irq_to_desc(irq);
10727d94f7caSYinghai Lu 	if (!desc)
10731da177e4SLinus Torvalds 		return -EINVAL;
10747d94f7caSYinghai Lu 
107508678b08SYinghai Lu 	if (desc->status & IRQ_NOREQUEST)
10766550c775SThomas Gleixner 		return -EINVAL;
1077b25c340cSThomas Gleixner 
1078b25c340cSThomas Gleixner 	if (!handler) {
1079b25c340cSThomas Gleixner 		if (!thread_fn)
10801da177e4SLinus Torvalds 			return -EINVAL;
1081b25c340cSThomas Gleixner 		handler = irq_default_primary_handler;
1082b25c340cSThomas Gleixner 	}
10831da177e4SLinus Torvalds 
108445535732SThomas Gleixner 	action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
10851da177e4SLinus Torvalds 	if (!action)
10861da177e4SLinus Torvalds 		return -ENOMEM;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	action->handler = handler;
10893aa551c9SThomas Gleixner 	action->thread_fn = thread_fn;
10901da177e4SLinus Torvalds 	action->flags = irqflags;
10911da177e4SLinus Torvalds 	action->name = devname;
10921da177e4SLinus Torvalds 	action->dev_id = dev_id;
10931da177e4SLinus Torvalds 
109470aedd24SThomas Gleixner 	chip_bus_lock(irq, desc);
1095d3c60047SThomas Gleixner 	retval = __setup_irq(irq, desc, action);
109670aedd24SThomas Gleixner 	chip_bus_sync_unlock(irq, desc);
109770aedd24SThomas Gleixner 
1098377bf1e4SAnton Vorontsov 	if (retval)
1099377bf1e4SAnton Vorontsov 		kfree(action);
1100377bf1e4SAnton Vorontsov 
1101a304e1b8SDavid Woodhouse #ifdef CONFIG_DEBUG_SHIRQ
11026ce51c43SLuis Henriques 	if (!retval && (irqflags & IRQF_SHARED)) {
1103a304e1b8SDavid Woodhouse 		/*
1104a304e1b8SDavid Woodhouse 		 * It's a shared IRQ -- the driver ought to be prepared for it
1105a304e1b8SDavid Woodhouse 		 * to happen immediately, so let's make sure....
1106377bf1e4SAnton Vorontsov 		 * We disable the irq to make sure that a 'real' IRQ doesn't
1107377bf1e4SAnton Vorontsov 		 * run in parallel with our fake.
1108a304e1b8SDavid Woodhouse 		 */
1109a304e1b8SDavid Woodhouse 		unsigned long flags;
1110a304e1b8SDavid Woodhouse 
1111377bf1e4SAnton Vorontsov 		disable_irq(irq);
1112a304e1b8SDavid Woodhouse 		local_irq_save(flags);
1113377bf1e4SAnton Vorontsov 
1114a304e1b8SDavid Woodhouse 		handler(irq, dev_id);
1115377bf1e4SAnton Vorontsov 
1116a304e1b8SDavid Woodhouse 		local_irq_restore(flags);
1117377bf1e4SAnton Vorontsov 		enable_irq(irq);
1118a304e1b8SDavid Woodhouse 	}
1119a304e1b8SDavid Woodhouse #endif
11201da177e4SLinus Torvalds 	return retval;
11211da177e4SLinus Torvalds }
11223aa551c9SThomas Gleixner EXPORT_SYMBOL(request_threaded_irq);
1123*ae731f8dSMarc Zyngier 
1124*ae731f8dSMarc Zyngier /**
1125*ae731f8dSMarc Zyngier  *	request_any_context_irq - allocate an interrupt line
1126*ae731f8dSMarc Zyngier  *	@irq: Interrupt line to allocate
1127*ae731f8dSMarc Zyngier  *	@handler: Function to be called when the IRQ occurs.
1128*ae731f8dSMarc Zyngier  *		  Threaded handler for threaded interrupts.
1129*ae731f8dSMarc Zyngier  *	@flags: Interrupt type flags
1130*ae731f8dSMarc Zyngier  *	@name: An ascii name for the claiming device
1131*ae731f8dSMarc Zyngier  *	@dev_id: A cookie passed back to the handler function
1132*ae731f8dSMarc Zyngier  *
1133*ae731f8dSMarc Zyngier  *	This call allocates interrupt resources and enables the
1134*ae731f8dSMarc Zyngier  *	interrupt line and IRQ handling. It selects either a
1135*ae731f8dSMarc Zyngier  *	hardirq or threaded handling method depending on the
1136*ae731f8dSMarc Zyngier  *	context.
1137*ae731f8dSMarc Zyngier  *
1138*ae731f8dSMarc Zyngier  *	On failure, it returns a negative value. On success,
1139*ae731f8dSMarc Zyngier  *	it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1140*ae731f8dSMarc Zyngier  */
1141*ae731f8dSMarc Zyngier int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1142*ae731f8dSMarc Zyngier 			    unsigned long flags, const char *name, void *dev_id)
1143*ae731f8dSMarc Zyngier {
1144*ae731f8dSMarc Zyngier 	struct irq_desc *desc = irq_to_desc(irq);
1145*ae731f8dSMarc Zyngier 	int ret;
1146*ae731f8dSMarc Zyngier 
1147*ae731f8dSMarc Zyngier 	if (!desc)
1148*ae731f8dSMarc Zyngier 		return -EINVAL;
1149*ae731f8dSMarc Zyngier 
1150*ae731f8dSMarc Zyngier 	if (desc->status & IRQ_NESTED_THREAD) {
1151*ae731f8dSMarc Zyngier 		ret = request_threaded_irq(irq, NULL, handler,
1152*ae731f8dSMarc Zyngier 					   flags, name, dev_id);
1153*ae731f8dSMarc Zyngier 		return !ret ? IRQC_IS_NESTED : ret;
1154*ae731f8dSMarc Zyngier 	}
1155*ae731f8dSMarc Zyngier 
1156*ae731f8dSMarc Zyngier 	ret = request_irq(irq, handler, flags, name, dev_id);
1157*ae731f8dSMarc Zyngier 	return !ret ? IRQC_IS_HARDIRQ : ret;
1158*ae731f8dSMarc Zyngier }
1159*ae731f8dSMarc Zyngier EXPORT_SYMBOL_GPL(request_any_context_irq);
1160