blob: ea0c6c2ae6f747d0bd8dff198e5e1f6b6934f050 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/irq/manage.c
3 *
Ingo Molnara34db9b2006-06-29 02:24:50 -07004 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006 Thomas Gleixner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file contains driver APIs to the irq subsystem.
8 */
9
Andrew Morton97fd75b2012-05-31 16:26:07 -070010#define pr_fmt(fmt) "genirq: " fmt
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/irq.h>
Thomas Gleixner3aa551c2009-03-23 18:28:15 +010013#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/random.h>
16#include <linux/interrupt.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070017#include <linux/slab.h>
Thomas Gleixner3aa551c2009-03-23 18:28:15 +010018#include <linux/sched.h>
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +100019#include <linux/task_work.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "internals.h"
22
Thomas Gleixner8d32a302011-02-23 23:52:23 +000023#ifdef CONFIG_IRQ_FORCED_THREADING
24__read_mostly bool force_irqthreads;
25
26static int __init setup_forced_irqthreads(char *arg)
27{
28 force_irqthreads = true;
29 return 0;
30}
31early_param("threadirqs", setup_forced_irqthreads);
32#endif
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/**
35 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
Randy Dunlap1e5d5332005-11-07 01:01:06 -080036 * @irq: interrupt number to wait for
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
38 * This function waits for any pending IRQ handlers for this interrupt
39 * to complete before returning. If you use this function while
40 * holding a resource the IRQ handler may need you will deadlock.
41 *
42 * This function may be called - with care - from IRQ context.
43 */
44void synchronize_irq(unsigned int irq)
45{
Yinghai Lucb5bc832008-08-19 20:50:17 -070046 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixner32f41252011-03-28 14:10:52 +020047 bool inprogress;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Yinghai Lu7d94f7c2008-08-19 20:50:14 -070049 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -070050 return;
51
Herbert Xua98ce5c2007-10-23 11:26:25 +080052 do {
53 unsigned long flags;
54
55 /*
56 * Wait until we're out of the critical section. This might
57 * give the wrong answer due to the lack of memory barriers.
58 */
Thomas Gleixner32f41252011-03-28 14:10:52 +020059 while (irqd_irq_inprogress(&desc->irq_data))
Herbert Xua98ce5c2007-10-23 11:26:25 +080060 cpu_relax();
61
62 /* Ok, that indicated we're done: double-check carefully. */
Thomas Gleixner239007b2009-11-17 16:46:45 +010063 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner32f41252011-03-28 14:10:52 +020064 inprogress = irqd_irq_inprogress(&desc->irq_data);
Thomas Gleixner239007b2009-11-17 16:46:45 +010065 raw_spin_unlock_irqrestore(&desc->lock, flags);
Herbert Xua98ce5c2007-10-23 11:26:25 +080066
67 /* Oops, that failed? */
Thomas Gleixner32f41252011-03-28 14:10:52 +020068 } while (inprogress);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +010069
70 /*
71 * We made sure that no hardirq handler is running. Now verify
72 * that no threaded handlers are active.
73 */
74 wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076EXPORT_SYMBOL(synchronize_irq);
77
Thomas Gleixner3aa551c2009-03-23 18:28:15 +010078#ifdef CONFIG_SMP
79cpumask_var_t irq_default_affinity;
80
Thomas Gleixner771ee3b2007-02-16 01:27:25 -080081/**
82 * irq_can_set_affinity - Check if the affinity of a given irq can be set
83 * @irq: Interrupt to check
84 *
85 */
86int irq_can_set_affinity(unsigned int irq)
87{
Yinghai Lu08678b02008-08-19 20:50:05 -070088 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixner771ee3b2007-02-16 01:27:25 -080089
Thomas Gleixnerbce43032011-02-10 22:37:41 +010090 if (!desc || !irqd_can_balance(&desc->irq_data) ||
91 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -080092 return 0;
93
94 return 1;
95}
96
Thomas Gleixner591d2fb2009-07-21 11:09:39 +020097/**
98 * irq_set_thread_affinity - Notify irq threads to adjust affinity
99 * @desc: irq descriptor which has affitnity changed
100 *
101 * We just set IRQTF_AFFINITY and delegate the affinity setting
102 * to the interrupt thread itself. We can not call
103 * set_cpus_allowed_ptr() here as we hold desc->lock and this
104 * code can be called from hard interrupt context.
105 */
106void irq_set_thread_affinity(struct irq_desc *desc)
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100107{
108 struct irqaction *action = desc->action;
109
110 while (action) {
111 if (action->thread)
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200112 set_bit(IRQTF_AFFINITY, &action->thread_flags);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100113 action = action->next;
114 }
115}
116
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100117#ifdef CONFIG_GENERIC_PENDING_IRQ
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200118static inline bool irq_can_move_pcntxt(struct irq_data *data)
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100119{
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200120 return irqd_can_move_in_process_context(data);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100121}
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200122static inline bool irq_move_pending(struct irq_data *data)
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100123{
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200124 return irqd_is_setaffinity_pending(data);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100125}
126static inline void
127irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
128{
129 cpumask_copy(desc->pending_mask, mask);
130}
131static inline void
132irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
133{
134 cpumask_copy(mask, desc->pending_mask);
135}
136#else
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200137static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
Thomas Gleixnercd22c0e2011-03-29 11:36:05 +0200138static inline bool irq_move_pending(struct irq_data *data) { return false; }
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100139static inline void
140irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
141static inline void
142irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
143#endif
144
David Daneyc2d0c552011-03-25 12:38:50 -0700145int __irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask)
146{
147 struct irq_chip *chip = irq_data_get_irq_chip(data);
148 struct irq_desc *desc = irq_data_to_desc(data);
149 int ret = 0;
150
151 if (!chip || !chip->irq_set_affinity)
152 return -EINVAL;
153
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200154 if (irq_can_move_pcntxt(data)) {
David Daneyc2d0c552011-03-25 12:38:50 -0700155 ret = chip->irq_set_affinity(data, mask, false);
156 switch (ret) {
157 case IRQ_SET_MASK_OK:
158 cpumask_copy(data->affinity, mask);
159 case IRQ_SET_MASK_OK_NOCOPY:
160 irq_set_thread_affinity(desc);
161 ret = 0;
162 }
163 } else {
164 irqd_set_move_pending(data);
165 irq_copy_pending(desc, mask);
166 }
167
168 if (desc->affinity_notify) {
169 kref_get(&desc->affinity_notify->kref);
170 schedule_work(&desc->affinity_notify->work);
171 }
David Daneyc2d0c552011-03-25 12:38:50 -0700172 irqd_set(data, IRQD_AFFINITY_SET);
173
174 return ret;
175}
176
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800177/**
178 * irq_set_affinity - Set the irq affinity of a given irq
179 * @irq: Interrupt to set affinity
Randy Dunlap30398bf2011-03-18 09:33:56 -0700180 * @mask: cpumask
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800181 *
182 */
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100183int irq_set_affinity(unsigned int irq, const struct cpumask *mask)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800184{
Yinghai Lu08678b02008-08-19 20:50:05 -0700185 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100186 unsigned long flags;
David Daneyc2d0c552011-03-25 12:38:50 -0700187 int ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800188
David Daneyc2d0c552011-03-25 12:38:50 -0700189 if (!desc)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800190 return -EINVAL;
191
Thomas Gleixner239007b2009-11-17 16:46:45 +0100192 raw_spin_lock_irqsave(&desc->lock, flags);
David Daneyc2d0c552011-03-25 12:38:50 -0700193 ret = __irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100194 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100195 return ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800196}
197
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700198int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
199{
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700200 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100201 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700202
203 if (!desc)
204 return -EINVAL;
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700205 desc->affinity_hint = m;
Thomas Gleixner02725e72011-02-12 10:37:36 +0100206 irq_put_desc_unlock(desc, flags);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700207 return 0;
208}
209EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
210
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000211static void irq_affinity_notify(struct work_struct *work)
212{
213 struct irq_affinity_notify *notify =
214 container_of(work, struct irq_affinity_notify, work);
215 struct irq_desc *desc = irq_to_desc(notify->irq);
216 cpumask_var_t cpumask;
217 unsigned long flags;
218
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100219 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000220 goto out;
221
222 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200223 if (irq_move_pending(&desc->irq_data))
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100224 irq_get_pending(cpumask, desc);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000225 else
Thomas Gleixner1fb0ef32011-01-31 08:57:41 +0100226 cpumask_copy(cpumask, desc->irq_data.affinity);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000227 raw_spin_unlock_irqrestore(&desc->lock, flags);
228
229 notify->notify(notify, cpumask);
230
231 free_cpumask_var(cpumask);
232out:
233 kref_put(&notify->kref, notify->release);
234}
235
236/**
237 * irq_set_affinity_notifier - control notification of IRQ affinity changes
238 * @irq: Interrupt for which to enable/disable notification
239 * @notify: Context for notification, or %NULL to disable
240 * notification. Function pointers must be initialised;
241 * the other fields will be initialised by this function.
242 *
243 * Must be called in process context. Notification may only be enabled
244 * after the IRQ is allocated and must be disabled before the IRQ is
245 * freed using free_irq().
246 */
247int
248irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
249{
250 struct irq_desc *desc = irq_to_desc(irq);
251 struct irq_affinity_notify *old_notify;
252 unsigned long flags;
253
254 /* The release function is promised process context */
255 might_sleep();
256
257 if (!desc)
258 return -EINVAL;
259
260 /* Complete initialisation of *notify */
261 if (notify) {
262 notify->irq = irq;
263 kref_init(&notify->kref);
264 INIT_WORK(&notify->work, irq_affinity_notify);
265 }
266
267 raw_spin_lock_irqsave(&desc->lock, flags);
268 old_notify = desc->affinity_notify;
269 desc->affinity_notify = notify;
270 raw_spin_unlock_irqrestore(&desc->lock, flags);
271
272 if (old_notify)
273 kref_put(&old_notify->kref, old_notify->release);
274
275 return 0;
276}
277EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
278
Max Krasnyansky18404752008-05-29 11:02:52 -0700279#ifndef CONFIG_AUTO_IRQ_AFFINITY
280/*
281 * Generic version of the affinity autoselector.
282 */
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100283static int
284setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
Max Krasnyansky18404752008-05-29 11:02:52 -0700285{
Thomas Gleixner35e857c2011-02-10 12:20:23 +0100286 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Gleixner569bda82011-02-07 17:05:08 +0100287 struct cpumask *set = irq_default_affinity;
Prarit Bhargava241fc642012-03-26 15:02:18 -0400288 int ret, node = desc->irq_data.node;
Thomas Gleixner569bda82011-02-07 17:05:08 +0100289
Thomas Gleixnerb0082072011-02-07 17:30:50 +0100290 /* Excludes PER_CPU and NO_BALANCE interrupts */
Max Krasnyansky18404752008-05-29 11:02:52 -0700291 if (!irq_can_set_affinity(irq))
292 return 0;
293
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100294 /*
295 * Preserve an userspace affinity setup, but make sure that
296 * one of the targets is online.
297 */
Thomas Gleixner2bdd1052011-02-08 17:22:00 +0100298 if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
Thomas Gleixner569bda82011-02-07 17:05:08 +0100299 if (cpumask_intersects(desc->irq_data.affinity,
300 cpu_online_mask))
301 set = desc->irq_data.affinity;
Thomas Gleixner0c6f8a82011-03-28 13:32:20 +0200302 else
Thomas Gleixner2bdd1052011-02-08 17:22:00 +0100303 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100304 }
Max Krasnyansky18404752008-05-29 11:02:52 -0700305
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100306 cpumask_and(mask, cpu_online_mask, set);
Prarit Bhargava241fc642012-03-26 15:02:18 -0400307 if (node != NUMA_NO_NODE) {
308 const struct cpumask *nodemask = cpumask_of_node(node);
309
310 /* make sure at least one of the cpus in nodemask is online */
311 if (cpumask_intersects(mask, nodemask))
312 cpumask_and(mask, mask, nodemask);
313 }
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100314 ret = chip->irq_set_affinity(&desc->irq_data, mask, false);
315 switch (ret) {
316 case IRQ_SET_MASK_OK:
317 cpumask_copy(desc->irq_data.affinity, mask);
318 case IRQ_SET_MASK_OK_NOCOPY:
319 irq_set_thread_affinity(desc);
320 }
Max Krasnyansky18404752008-05-29 11:02:52 -0700321 return 0;
322}
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100323#else
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100324static inline int
325setup_affinity(unsigned int irq, struct irq_desc *d, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100326{
327 return irq_select_affinity(irq);
328}
Max Krasnyansky18404752008-05-29 11:02:52 -0700329#endif
330
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100331/*
332 * Called when affinity is set via /proc/irq
333 */
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100334int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100335{
336 struct irq_desc *desc = irq_to_desc(irq);
337 unsigned long flags;
338 int ret;
339
Thomas Gleixner239007b2009-11-17 16:46:45 +0100340 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100341 ret = setup_affinity(irq, desc, mask);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100342 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100343 return ret;
344}
345
346#else
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100347static inline int
348setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100349{
350 return 0;
351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#endif
353
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100354void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
355{
356 if (suspend) {
Ian Campbell685fd0b42010-07-29 11:16:32 +0100357 if (!desc->action || (desc->action->flags & IRQF_NO_SUSPEND))
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100358 return;
Thomas Gleixnerc531e832011-02-08 12:44:58 +0100359 desc->istate |= IRQS_SUSPENDED;
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100360 }
361
Thomas Gleixner3aae9942011-02-04 10:17:52 +0100362 if (!desc->depth++)
Thomas Gleixner87923472011-02-03 12:27:44 +0100363 irq_disable(desc);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100364}
365
Thomas Gleixner02725e72011-02-12 10:37:36 +0100366static int __disable_irq_nosync(unsigned int irq)
367{
368 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100369 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100370
371 if (!desc)
372 return -EINVAL;
373 __disable_irq(desc, irq, false);
374 irq_put_desc_busunlock(desc, flags);
375 return 0;
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/**
379 * disable_irq_nosync - disable an irq without waiting
380 * @irq: Interrupt to disable
381 *
382 * Disable the selected interrupt line. Disables and Enables are
383 * nested.
384 * Unlike disable_irq(), this function does not ensure existing
385 * instances of the IRQ handler have completed before returning.
386 *
387 * This function may be called from IRQ context.
388 */
389void disable_irq_nosync(unsigned int irq)
390{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100391 __disable_irq_nosync(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393EXPORT_SYMBOL(disable_irq_nosync);
394
395/**
396 * disable_irq - disable an irq and wait for completion
397 * @irq: Interrupt to disable
398 *
399 * Disable the selected interrupt line. Enables and Disables are
400 * nested.
401 * This function waits for any pending IRQ handlers for this interrupt
402 * to complete before returning. If you use this function while
403 * holding a resource the IRQ handler may need you will deadlock.
404 *
405 * This function may be called - with care - from IRQ context.
406 */
407void disable_irq(unsigned int irq)
408{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100409 if (!__disable_irq_nosync(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 synchronize_irq(irq);
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412EXPORT_SYMBOL(disable_irq);
413
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100414void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200415{
Thomas Gleixnerdc5f2192011-02-04 13:19:20 +0100416 if (resume) {
Thomas Gleixnerc531e832011-02-08 12:44:58 +0100417 if (!(desc->istate & IRQS_SUSPENDED)) {
Thomas Gleixnerdc5f2192011-02-04 13:19:20 +0100418 if (!desc->action)
419 return;
420 if (!(desc->action->flags & IRQF_FORCE_RESUME))
421 return;
422 /* Pretend that it got disabled ! */
423 desc->depth++;
424 }
Thomas Gleixnerc531e832011-02-08 12:44:58 +0100425 desc->istate &= ~IRQS_SUSPENDED;
Thomas Gleixnerdc5f2192011-02-04 13:19:20 +0100426 }
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100427
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200428 switch (desc->depth) {
429 case 0:
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100430 err_out:
Arjan van de Venb8c512f2008-07-25 19:45:36 -0700431 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200432 break;
433 case 1: {
Thomas Gleixnerc531e832011-02-08 12:44:58 +0100434 if (desc->istate & IRQS_SUSPENDED)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100435 goto err_out;
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200436 /* Prevent probing on this irq: */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +0100437 irq_settings_set_noprobe(desc);
Thomas Gleixner3aae9942011-02-04 10:17:52 +0100438 irq_enable(desc);
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200439 check_irq_resend(desc, irq);
440 /* fall-through */
441 }
442 default:
443 desc->depth--;
444 }
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/**
448 * enable_irq - enable handling of an irq
449 * @irq: Interrupt to enable
450 *
451 * Undoes the effect of one call to disable_irq(). If this
452 * matches the last disable, processing of interrupts on this
453 * IRQ line is re-enabled.
454 *
Thomas Gleixner70aedd22009-08-13 12:17:48 +0200455 * This function may be called from IRQ context only when
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200456 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
458void enable_irq(unsigned int irq)
459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100461 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700463 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -0700464 return;
Thomas Gleixner50f7c032011-02-03 13:23:54 +0100465 if (WARN(!desc->irq_data.chip,
466 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
Thomas Gleixner02725e72011-02-12 10:37:36 +0100467 goto out;
Thomas Gleixner2656c362010-10-22 14:47:57 +0200468
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100469 __enable_irq(desc, irq, false);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100470out:
471 irq_put_desc_busunlock(desc, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473EXPORT_SYMBOL(enable_irq);
474
David Brownell0c5d1eb2008-10-01 14:46:18 -0700475static int set_irq_wake_real(unsigned int irq, unsigned int on)
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200476{
Yinghai Lu08678b02008-08-19 20:50:05 -0700477 struct irq_desc *desc = irq_to_desc(irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200478 int ret = -ENXIO;
479
Santosh Shilimkar60f96b42011-09-09 13:59:35 +0530480 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
481 return 0;
482
Thomas Gleixner2f7e99b2010-09-27 12:45:50 +0000483 if (desc->irq_data.chip->irq_set_wake)
484 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200485
486 return ret;
487}
488
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700489/**
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100490 * irq_set_irq_wake - control irq power management wakeup
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700491 * @irq: interrupt to control
492 * @on: enable/disable power management wakeup
493 *
David Brownell15a647e2006-07-30 03:03:08 -0700494 * Enable/disable power management wakeup mode, which is
495 * disabled by default. Enables and disables must match,
496 * just as they match for non-wakeup mode support.
497 *
498 * Wakeup mode lets this IRQ wake the system from sleep
499 * states like "suspend to RAM".
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700500 */
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100501int irq_set_irq_wake(unsigned int irq, unsigned int on)
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700502{
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700503 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100504 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200505 int ret = 0;
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700506
Jesper Juhl13863a62011-06-09 23:14:58 +0200507 if (!desc)
508 return -EINVAL;
509
David Brownell15a647e2006-07-30 03:03:08 -0700510 /* wakeup-capable irqs can be shared between drivers that
511 * don't need to have the same sleep mode behaviors.
512 */
David Brownell15a647e2006-07-30 03:03:08 -0700513 if (on) {
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200514 if (desc->wake_depth++ == 0) {
515 ret = set_irq_wake_real(irq, on);
516 if (ret)
517 desc->wake_depth = 0;
518 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100519 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200520 }
David Brownell15a647e2006-07-30 03:03:08 -0700521 } else {
522 if (desc->wake_depth == 0) {
Arjan van de Ven7a2c4772008-07-25 01:45:54 -0700523 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200524 } else if (--desc->wake_depth == 0) {
525 ret = set_irq_wake_real(irq, on);
526 if (ret)
527 desc->wake_depth = 1;
528 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100529 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200530 }
David Brownell15a647e2006-07-30 03:03:08 -0700531 }
Thomas Gleixner02725e72011-02-12 10:37:36 +0100532 irq_put_desc_busunlock(desc, flags);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700533 return ret;
534}
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100535EXPORT_SYMBOL(irq_set_irq_wake);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
538 * Internal function that tells the architecture code whether a
539 * particular irq has been exclusively allocated or is available
540 * for driver use.
541 */
542int can_request_irq(unsigned int irq, unsigned long irqflags)
543{
Thomas Gleixnercc8c3b72010-03-23 22:40:53 +0100544 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100545 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100546 int canrequest = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700548 if (!desc)
549 return 0;
550
Thomas Gleixner02725e72011-02-12 10:37:36 +0100551 if (irq_settings_can_request(desc)) {
552 if (desc->action)
553 if (irqflags & desc->action->flags & IRQF_SHARED)
554 canrequest =1;
555 }
556 irq_put_desc_unlock(desc, flags);
557 return canrequest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
David Brownell0c5d1eb2008-10-01 14:46:18 -0700560int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000561 unsigned long flags)
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700562{
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200563 struct irq_chip *chip = desc->irq_data.chip;
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100564 int ret, unmask = 0;
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700565
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000566 if (!chip || !chip->irq_set_type) {
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700567 /*
568 * IRQF_TRIGGER_* but the PIC does not support multiple
569 * flow-types?
570 */
Andrew Morton97fd75b2012-05-31 16:26:07 -0700571 pr_debug("No set_type function for IRQ %d (%s)\n", irq,
Thomas Gleixnerf5d89472012-04-19 12:06:13 +0200572 chip ? (chip->name ? : "unknown") : "unknown");
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700573 return 0;
574 }
575
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100576 flags &= IRQ_TYPE_SENSE_MASK;
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100577
578 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
Thomas Gleixner32f41252011-03-28 14:10:52 +0200579 if (!irqd_irq_masked(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100580 mask_irq(desc);
Thomas Gleixner32f41252011-03-28 14:10:52 +0200581 if (!irqd_irq_disabled(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100582 unmask = 1;
583 }
584
David Brownellf2b662d2008-12-01 14:31:38 -0800585 /* caller masked out all except trigger mode flags */
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000586 ret = chip->irq_set_type(&desc->irq_data, flags);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700587
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100588 switch (ret) {
589 case IRQ_SET_MASK_OK:
590 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
591 irqd_set(&desc->irq_data, flags);
592
593 case IRQ_SET_MASK_OK_NOCOPY:
594 flags = irqd_get_trigger_type(&desc->irq_data);
595 irq_settings_set_trigger_mask(desc, flags);
596 irqd_clear(&desc->irq_data, IRQD_LEVEL);
597 irq_settings_clr_level(desc);
598 if (flags & IRQ_TYPE_LEVEL_MASK) {
599 irq_settings_set_level(desc);
600 irqd_set(&desc->irq_data, IRQD_LEVEL);
601 }
Thomas Gleixner46732472010-06-07 17:53:51 +0200602
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100603 ret = 0;
Thomas Gleixner8fff39e2011-02-21 14:19:42 +0100604 break;
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100605 default:
Andrew Morton97fd75b2012-05-31 16:26:07 -0700606 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100607 flags, irq, chip->irq_set_type);
David Brownell0c5d1eb2008-10-01 14:46:18 -0700608 }
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100609 if (unmask)
610 unmask_irq(desc);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700611 return ret;
612}
613
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200614/*
615 * Default primary interrupt handler for threaded interrupts. Is
616 * assigned as primary handler when request_threaded_irq is called
617 * with handler == NULL. Useful for oneshot interrupts.
618 */
619static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
620{
621 return IRQ_WAKE_THREAD;
622}
623
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200624/*
625 * Primary handler for nested threaded interrupts. Should never be
626 * called.
627 */
628static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
629{
630 WARN(1, "Primary handler called for nested irq %d\n", irq);
631 return IRQ_NONE;
632}
633
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100634static int irq_wait_for_interrupt(struct irqaction *action)
635{
Ido Yariv550acb12011-12-01 13:55:08 +0200636 set_current_state(TASK_INTERRUPTIBLE);
637
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100638 while (!kthread_should_stop()) {
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100639
640 if (test_and_clear_bit(IRQTF_RUNTHREAD,
641 &action->thread_flags)) {
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100642 __set_current_state(TASK_RUNNING);
643 return 0;
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100644 }
645 schedule();
Ido Yariv550acb12011-12-01 13:55:08 +0200646 set_current_state(TASK_INTERRUPTIBLE);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100647 }
Ido Yariv550acb12011-12-01 13:55:08 +0200648 __set_current_state(TASK_RUNNING);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100649 return -1;
650}
651
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200652/*
653 * Oneshot interrupts keep the irq line masked until the threaded
654 * handler finished. unmask if the interrupt has not been disabled and
655 * is marked MASKED.
656 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000657static void irq_finalize_oneshot(struct irq_desc *desc,
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100658 struct irqaction *action)
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200659{
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000660 if (!(desc->istate & IRQS_ONESHOT))
661 return;
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100662again:
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000663 chip_bus_lock(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100664 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100665
666 /*
667 * Implausible though it may be we need to protect us against
668 * the following scenario:
669 *
670 * The thread is faster done than the hard interrupt handler
671 * on the other CPU. If we unmask the irq line then the
672 * interrupt can come in again and masks the line, leaves due
Thomas Gleixner009b4c32011-02-07 21:48:49 +0100673 * to IRQS_INPROGRESS and the irq line is masked forever.
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000674 *
675 * This also serializes the state of shared oneshot handlers
676 * versus "desc->threads_onehsot |= action->thread_mask;" in
677 * irq_wake_thread(). See the comment there which explains the
678 * serialization.
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100679 */
Thomas Gleixner32f41252011-03-28 14:10:52 +0200680 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100681 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000682 chip_bus_sync_unlock(desc);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100683 cpu_relax();
684 goto again;
685 }
686
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000687 /*
688 * Now check again, whether the thread should run. Otherwise
689 * we would clear the threads_oneshot bit of this thread which
690 * was just set.
691 */
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100692 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000693 goto out_unlock;
694
695 desc->threads_oneshot &= ~action->thread_mask;
696
Thomas Gleixner32f41252011-03-28 14:10:52 +0200697 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
698 irqd_irq_masked(&desc->irq_data))
699 unmask_irq(desc);
700
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000701out_unlock:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100702 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000703 chip_bus_sync_unlock(desc);
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200704}
705
Bruno Premont61f38262009-07-22 22:22:32 +0200706#ifdef CONFIG_SMP
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100707/*
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100708 * Check whether we need to chasnge the affinity of the interrupt thread.
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200709 */
710static void
711irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
712{
713 cpumask_var_t mask;
714
715 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
716 return;
717
718 /*
719 * In case we are out of memory we set IRQTF_AFFINITY again and
720 * try again next time
721 */
722 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
723 set_bit(IRQTF_AFFINITY, &action->thread_flags);
724 return;
725 }
726
Thomas Gleixner239007b2009-11-17 16:46:45 +0100727 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200728 cpumask_copy(mask, desc->irq_data.affinity);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100729 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200730
731 set_cpus_allowed_ptr(current, mask);
732 free_cpumask_var(mask);
733}
Bruno Premont61f38262009-07-22 22:22:32 +0200734#else
735static inline void
736irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
737#endif
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200738
739/*
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000740 * Interrupts which are not explicitely requested as threaded
741 * interrupts rely on the implicit bh/preempt disable of the hard irq
742 * context. So we need to disable bh here to avoid deadlocks and other
743 * side effects.
744 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200745static irqreturn_t
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000746irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
747{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200748 irqreturn_t ret;
749
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000750 local_bh_disable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200751 ret = action->thread_fn(action->irq, action->dev_id);
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100752 irq_finalize_oneshot(desc, action);
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000753 local_bh_enable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200754 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000755}
756
757/*
758 * Interrupts explicitely requested as threaded interupts want to be
759 * preemtible - many of them need to sleep and wait for slow busses to
760 * complete.
761 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200762static irqreturn_t irq_thread_fn(struct irq_desc *desc,
763 struct irqaction *action)
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000764{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200765 irqreturn_t ret;
766
767 ret = action->thread_fn(action->irq, action->dev_id);
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100768 irq_finalize_oneshot(desc, action);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200769 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000770}
771
Ido Yariv7140ea12011-12-02 18:24:12 +0200772static void wake_threads_waitq(struct irq_desc *desc)
773{
774 if (atomic_dec_and_test(&desc->threads_active) &&
775 waitqueue_active(&desc->wait_for_threads))
776 wake_up(&desc->wait_for_threads);
777}
778
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000779static void irq_thread_dtor(struct task_work *unused)
780{
781 struct task_struct *tsk = current;
782 struct irq_desc *desc;
783 struct irqaction *action;
784
785 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
786 return;
787
788 action = kthread_data(tsk);
789
Linus Torvaldsfb21aff2012-05-31 18:47:30 -0700790 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000791 tsk->comm ? tsk->comm : "", tsk->pid, action->irq);
792
793
794 desc = irq_to_desc(action->irq);
795 /*
796 * If IRQTF_RUNTHREAD is set, we need to decrement
797 * desc->threads_active and wake possible waiters.
798 */
799 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
800 wake_threads_waitq(desc);
801
802 /* Prevent a stale desc->threads_oneshot */
803 irq_finalize_oneshot(desc, action);
804}
805
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000806/*
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100807 * Interrupt handler thread
808 */
809static int irq_thread(void *data)
810{
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000811 struct task_work on_exit_work;
Peter Zijlstrac9b5f502011-01-07 13:41:40 +0100812 static const struct sched_param param = {
KOSAKI Motohirofe7de492010-10-20 16:01:12 -0700813 .sched_priority = MAX_USER_RT_PRIO/2,
814 };
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100815 struct irqaction *action = data;
816 struct irq_desc *desc = irq_to_desc(action->irq);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200817 irqreturn_t (*handler_fn)(struct irq_desc *desc,
818 struct irqaction *action);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100819
Alexander Gordeev540b60e2012-03-09 14:59:13 +0100820 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000821 &action->thread_flags))
822 handler_fn = irq_forced_thread_fn;
823 else
824 handler_fn = irq_thread_fn;
825
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100826 sched_setscheduler(current, SCHED_FIFO, &param);
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000827
828 init_task_work(&on_exit_work, irq_thread_dtor, NULL);
829 task_work_add(current, &on_exit_work, false);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100830
831 while (!irq_wait_for_interrupt(action)) {
Ido Yariv7140ea12011-12-02 18:24:12 +0200832 irqreturn_t action_ret;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100833
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200834 irq_thread_check_affinity(desc, action);
835
Ido Yariv7140ea12011-12-02 18:24:12 +0200836 action_ret = handler_fn(desc, action);
837 if (!noirqdebug)
838 note_interrupt(action->irq, desc, action_ret);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100839
Ido Yariv7140ea12011-12-02 18:24:12 +0200840 wake_threads_waitq(desc);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100841 }
842
Ido Yariv7140ea12011-12-02 18:24:12 +0200843 /*
844 * This is the regular exit path. __free_irq() is stopping the
845 * thread via kthread_stop() after calling
846 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
Thomas Gleixnere04268b2012-03-15 22:55:21 +0100847 * oneshot mask bit can be set. We cannot verify that as we
848 * cannot touch the oneshot mask at this point anymore as
849 * __setup_irq() might have given out currents thread_mask
850 * again.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100851 */
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000852 task_work_cancel(current, irq_thread_dtor);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100853 return 0;
854}
855
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000856static void irq_setup_forced_threading(struct irqaction *new)
857{
858 if (!force_irqthreads)
859 return;
860 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
861 return;
862
863 new->flags |= IRQF_ONESHOT;
864
865 if (!new->thread_fn) {
866 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
867 new->thread_fn = new->handler;
868 new->handler = irq_default_primary_handler;
869 }
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/*
873 * Internal function to register an irqaction - typically used to
874 * allocate special interrupts that are part of the architecture.
875 */
Thomas Gleixnerd3c60042008-10-16 09:55:00 +0200876static int
Ingo Molnar327ec562009-02-15 11:21:37 +0100877__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Ingo Molnarf17c7542009-02-17 20:43:37 +0100879 struct irqaction *old, **old_ptr;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000880 unsigned long flags, thread_mask = 0;
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100881 int ret, nested, shared = 0;
882 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700884 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -0700885 return -EINVAL;
886
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200887 if (desc->irq_data.chip == &no_irq_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -ENOSYS;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200889 if (!try_module_get(desc->owner))
890 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Some drivers like serial.c use request_irq() heavily,
893 * so we have to be careful not to interfere with a
894 * running system.
895 */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700896 if (new->flags & IRQF_SAMPLE_RANDOM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
898 * This function might sleep, we want to call it first,
899 * outside of the atomic block.
900 * Yes, this might clear the entropy pool if the wrong
901 * driver is attempted to be loaded, without actually
902 * installing a new handler, but is this really a problem,
903 * only the sysadmin is able to do this.
904 */
905 rand_initialize_irq(irq);
906 }
907
908 /*
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200909 * Check whether the interrupt nests into another interrupt
910 * thread.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100911 */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +0100912 nested = irq_settings_is_nested_thread(desc);
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200913 if (nested) {
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200914 if (!new->thread_fn) {
915 ret = -EINVAL;
916 goto out_mput;
917 }
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200918 /*
919 * Replace the primary handler which was provided from
920 * the driver for non nested interrupt handling by the
921 * dummy function which warns when called.
922 */
923 new->handler = irq_nested_primary_handler;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000924 } else {
Paul Mundt7f1b1242011-04-07 06:01:44 +0900925 if (irq_settings_can_thread(desc))
926 irq_setup_forced_threading(new);
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200927 }
928
929 /*
930 * Create a handler thread when a thread function is supplied
931 * and the interrupt does not nest into another interrupt
932 * thread.
933 */
934 if (new->thread_fn && !nested) {
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100935 struct task_struct *t;
936
937 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
938 new->name);
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +0200939 if (IS_ERR(t)) {
940 ret = PTR_ERR(t);
941 goto out_mput;
942 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100943 /*
944 * We keep the reference to the task struct even if
945 * the thread dies to avoid that the interrupt code
946 * references an already freed task_struct.
947 */
948 get_task_struct(t);
949 new->thread = t;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100950 }
951
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100952 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
953 ret = -ENOMEM;
954 goto out_thread;
955 }
956
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100957 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * The following block of code has to be executed atomically
959 */
Thomas Gleixner239007b2009-11-17 16:46:45 +0100960 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarf17c7542009-02-17 20:43:37 +0100961 old_ptr = &desc->action;
962 old = *old_ptr;
Ingo Molnar06fcb0c2006-06-29 02:24:40 -0700963 if (old) {
Thomas Gleixnere76de9f2006-06-29 02:24:56 -0700964 /*
965 * Can't share interrupts unless both agree to and are
966 * the same type (level, edge, polarity). So both flag
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700967 * fields must have IRQF_SHARED set and the bits which
Thomas Gleixner9d591ed2011-02-23 23:52:16 +0000968 * set the trigger type must match. Also all must
969 * agree on ONESHOT.
Thomas Gleixnere76de9f2006-06-29 02:24:56 -0700970 */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700971 if (!((old->flags & new->flags) & IRQF_SHARED) ||
Thomas Gleixner9d591ed2011-02-23 23:52:16 +0000972 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
Thomas Gleixnerf5d89472012-04-19 12:06:13 +0200973 ((old->flags ^ new->flags) & IRQF_ONESHOT))
Dimitri Sivanichf5163422006-03-25 03:08:23 -0800974 goto mismatch;
975
Dimitri Sivanichf5163422006-03-25 03:08:23 -0800976 /* All handlers must agree on per-cpuness */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -0700977 if ((old->flags & IRQF_PERCPU) !=
978 (new->flags & IRQF_PERCPU))
Dimitri Sivanichf5163422006-03-25 03:08:23 -0800979 goto mismatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /* add new interrupt at end of irq queue */
982 do {
Thomas Gleixner52abb702012-03-06 23:18:54 +0100983 /*
984 * Or all existing action->thread_mask bits,
985 * so we can find the next zero bit for this
986 * new action.
987 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000988 thread_mask |= old->thread_mask;
Ingo Molnarf17c7542009-02-17 20:43:37 +0100989 old_ptr = &old->next;
990 old = *old_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 } while (old);
992 shared = 1;
993 }
994
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000995 /*
Thomas Gleixner52abb702012-03-06 23:18:54 +0100996 * Setup the thread mask for this irqaction for ONESHOT. For
997 * !ONESHOT irqs the thread mask is 0 so we can avoid a
998 * conditional in irq_wake_thread().
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000999 */
Thomas Gleixner52abb702012-03-06 23:18:54 +01001000 if (new->flags & IRQF_ONESHOT) {
1001 /*
1002 * Unlikely to have 32 resp 64 irqs sharing one line,
1003 * but who knows.
1004 */
1005 if (thread_mask == ~0UL) {
1006 ret = -EBUSY;
1007 goto out_mask;
1008 }
1009 /*
1010 * The thread_mask for the action is or'ed to
1011 * desc->thread_active to indicate that the
1012 * IRQF_ONESHOT thread handler has been woken, but not
1013 * yet finished. The bit is cleared when a thread
1014 * completes. When all threads of a shared interrupt
1015 * line have completed desc->threads_active becomes
1016 * zero and the interrupt line is unmasked. See
1017 * handle.c:irq_wake_thread() for further information.
1018 *
1019 * If no thread is woken by primary (hard irq context)
1020 * interrupt handlers, then desc->threads_active is
1021 * also checked for zero to unmask the irq line in the
1022 * affected hard irq flow handlers
1023 * (handle_[fasteoi|level]_irq).
1024 *
1025 * The new action gets the first zero bit of
1026 * thread_mask assigned. See the loop above which or's
1027 * all existing action->thread_mask bits.
1028 */
1029 new->thread_mask = 1 << ffz(thread_mask);
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001030
1031 } else if (new->handler == irq_default_primary_handler) {
1032 /*
1033 * The interrupt was requested with handler = NULL, so
1034 * we use the default primary handler for it. But it
1035 * does not have the oneshot flag set. In combination
1036 * with level interrupts this is deadly, because the
1037 * default primary handler just wakes the thread, then
1038 * the irq lines is reenabled, but the device still
1039 * has the level irq asserted. Rinse and repeat....
1040 *
1041 * While this works for edge type interrupts, we play
1042 * it safe and reject unconditionally because we can't
1043 * say for sure which type this interrupt really
1044 * has. The type flags are unreliable as the
1045 * underlying chip implementation can override them.
1046 */
Andrew Morton97fd75b2012-05-31 16:26:07 -07001047 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001048 irq);
1049 ret = -EINVAL;
1050 goto out_mask;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001051 }
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (!shared) {
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001054 init_waitqueue_head(&desc->wait_for_threads);
1055
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001056 /* Setup the type (level, edge polarity) if configured: */
1057 if (new->flags & IRQF_TRIGGER_MASK) {
David Brownellf2b662d2008-12-01 14:31:38 -08001058 ret = __irq_set_trigger(desc, irq,
1059 new->flags & IRQF_TRIGGER_MASK);
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001060
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001061 if (ret)
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001062 goto out_mask;
Thomas Gleixner091738a2011-02-14 20:16:43 +01001063 }
Ahmed S. Darwishf75d2222007-05-08 00:27:55 -07001064
Thomas Gleixner009b4c32011-02-07 21:48:49 +01001065 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
Thomas Gleixner32f41252011-03-28 14:10:52 +02001066 IRQS_ONESHOT | IRQS_WAITING);
1067 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
Thomas Gleixner94d39e12006-06-29 02:24:50 -07001068
Thomas Gleixnera0056772011-02-08 17:11:03 +01001069 if (new->flags & IRQF_PERCPU) {
1070 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1071 irq_settings_set_per_cpu(desc);
1072 }
Thomas Gleixner6a58fb32011-02-08 15:40:05 +01001073
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001074 if (new->flags & IRQF_ONESHOT)
Thomas Gleixner3d67bae2011-02-07 21:02:10 +01001075 desc->istate |= IRQS_ONESHOT;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001076
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001077 if (irq_settings_can_autoenable(desc))
Thomas Gleixnerb4bc7242012-02-08 11:57:52 +01001078 irq_startup(desc, true);
Thomas Gleixner46999232011-02-02 21:41:14 +00001079 else
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001080 /* Undo nested disables: */
1081 desc->depth = 1;
Max Krasnyansky18404752008-05-29 11:02:52 -07001082
Thomas Gleixner612e3682008-11-07 13:58:46 +01001083 /* Exclude IRQ from balancing if requested */
Thomas Gleixnera0056772011-02-08 17:11:03 +01001084 if (new->flags & IRQF_NOBALANCING) {
1085 irq_settings_set_no_balancing(desc);
1086 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1087 }
Thomas Gleixner612e3682008-11-07 13:58:46 +01001088
Max Krasnyansky18404752008-05-29 11:02:52 -07001089 /* Set default affinity mask once everything is setup */
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001090 setup_affinity(irq, desc, mask);
David Brownell0c5d1eb2008-10-01 14:46:18 -07001091
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001092 } else if (new->flags & IRQF_TRIGGER_MASK) {
1093 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1094 unsigned int omsk = irq_settings_get_trigger_mask(desc);
1095
1096 if (nmsk != omsk)
1097 /* hope the handler works with current trigger mode */
Andrew Morton97fd75b2012-05-31 16:26:07 -07001098 pr_warning("irq %d uses trigger mode %u; requested %u\n",
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001099 irq, nmsk, omsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001101
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001102 new->irq = irq;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001103 *old_ptr = new;
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001104
Linus Torvalds8528b0f2007-01-23 14:16:31 -08001105 /* Reset broken irq detection when installing new handler */
1106 desc->irq_count = 0;
1107 desc->irqs_unhandled = 0;
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001108
1109 /*
1110 * Check whether we disabled the irq via the spurious handler
1111 * before. Reenable it and give it another chance.
1112 */
Thomas Gleixner7acdd532011-02-07 20:40:54 +01001113 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1114 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +01001115 __enable_irq(desc, irq, false);
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001116 }
1117
Thomas Gleixner239007b2009-11-17 16:46:45 +01001118 raw_spin_unlock_irqrestore(&desc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001120 /*
1121 * Strictly no need to wake it up, but hung_task complains
1122 * when no hard interrupt wakes the thread up.
1123 */
1124 if (new->thread)
1125 wake_up_process(new->thread);
1126
Yinghai Lu2c6927a2008-08-19 20:50:11 -07001127 register_irq_proc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 new->dir = NULL;
1129 register_handler_proc(irq, new);
Xiaotian Feng4f5058c2011-04-02 19:39:35 +08001130 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 return 0;
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001133
1134mismatch:
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001135 if (!(new->flags & IRQF_PROBE_SHARED)) {
Andrew Morton97fd75b2012-05-31 16:26:07 -07001136 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001137 irq, new->flags, new->name, old->flags, old->name);
1138#ifdef CONFIG_DEBUG_SHIRQ
Andrew Morton13e87ec2006-04-27 18:39:18 -07001139 dump_stack();
Alan Cox3f050442007-02-12 00:52:04 -08001140#endif
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001141 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001142 ret = -EBUSY;
1143
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001144out_mask:
Dan Carpenter1c389792011-03-17 14:43:07 +03001145 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001146 free_cpumask_var(mask);
1147
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001148out_thread:
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001149 if (new->thread) {
1150 struct task_struct *t = new->thread;
1151
1152 new->thread = NULL;
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001153 kthread_stop(t);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001154 put_task_struct(t);
1155 }
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001156out_mput:
1157 module_put(desc->owner);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001158 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161/**
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001162 * setup_irq - setup an interrupt
1163 * @irq: Interrupt line to setup
1164 * @act: irqaction for the interrupt
1165 *
1166 * Used to statically setup interrupts in the early boot process.
1167 */
1168int setup_irq(unsigned int irq, struct irqaction *act)
1169{
David Daney986c0112011-02-09 16:04:25 -08001170 int retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001171 struct irq_desc *desc = irq_to_desc(irq);
1172
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001173 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1174 return -EINVAL;
David Daney986c0112011-02-09 16:04:25 -08001175 chip_bus_lock(desc);
1176 retval = __setup_irq(irq, desc, act);
1177 chip_bus_sync_unlock(desc);
1178
1179 return retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001180}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001181EXPORT_SYMBOL_GPL(setup_irq);
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001182
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001183/*
Magnus Dammcbf94f02009-03-12 21:05:51 +09001184 * Internal function to unregister an irqaction - used to free
1185 * regular and special interrupts that are part of the architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
Magnus Dammcbf94f02009-03-12 21:05:51 +09001187static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001189 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnarf17c7542009-02-17 20:43:37 +01001190 struct irqaction *action, **action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 unsigned long flags;
1192
Ingo Molnarae88a232009-02-15 11:29:50 +01001193 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001194
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001195 if (!desc)
Magnus Dammf21cfb22009-03-12 21:05:42 +09001196 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Thomas Gleixner239007b2009-11-17 16:46:45 +01001198 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarae88a232009-02-15 11:29:50 +01001199
1200 /*
1201 * There can be multiple actions per IRQ descriptor, find the right
1202 * one based on the dev_id:
1203 */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001204 action_ptr = &desc->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 for (;;) {
Ingo Molnarf17c7542009-02-17 20:43:37 +01001206 action = *action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Ingo Molnarae88a232009-02-15 11:29:50 +01001208 if (!action) {
1209 WARN(1, "Trying to free already-free IRQ %d\n", irq);
Thomas Gleixner239007b2009-11-17 16:46:45 +01001210 raw_spin_unlock_irqrestore(&desc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Magnus Dammf21cfb22009-03-12 21:05:42 +09001212 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001214
Ingo Molnar8316e382009-02-17 20:28:29 +01001215 if (action->dev_id == dev_id)
1216 break;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001217 action_ptr = &action->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001219
1220 /* Found it - now remove it from the list of entries: */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001221 *action_ptr = action->next;
Ingo Molnarae88a232009-02-15 11:29:50 +01001222
Ingo Molnarae88a232009-02-15 11:29:50 +01001223 /* If this was the last handler, shut down the IRQ line: */
Thomas Gleixner46999232011-02-02 21:41:14 +00001224 if (!desc->action)
1225 irq_shutdown(desc);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001226
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -07001227#ifdef CONFIG_SMP
1228 /* make sure affinity_hint is cleaned up */
1229 if (WARN_ON_ONCE(desc->affinity_hint))
1230 desc->affinity_hint = NULL;
1231#endif
1232
Thomas Gleixner239007b2009-11-17 16:46:45 +01001233 raw_spin_unlock_irqrestore(&desc->lock, flags);
Ingo Molnarae88a232009-02-15 11:29:50 +01001234
1235 unregister_handler_proc(irq, action);
1236
1237 /* Make sure it's not being used on another CPU: */
1238 synchronize_irq(irq);
1239
1240#ifdef CONFIG_DEBUG_SHIRQ
1241 /*
1242 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1243 * event to happen even now it's being freed, so let's make sure that
1244 * is so by doing an extra call to the handler ....
1245 *
1246 * ( We do this after actually deregistering it, to make sure that a
1247 * 'real' IRQ doesn't run in * parallel with our fake. )
1248 */
1249 if (action->flags & IRQF_SHARED) {
1250 local_irq_save(flags);
1251 action->handler(irq, dev_id);
1252 local_irq_restore(flags);
1253 }
1254#endif
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001255
1256 if (action->thread) {
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001257 kthread_stop(action->thread);
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001258 put_task_struct(action->thread);
1259 }
1260
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001261 module_put(desc->owner);
Magnus Dammf21cfb22009-03-12 21:05:42 +09001262 return action;
1263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265/**
Magnus Dammcbf94f02009-03-12 21:05:51 +09001266 * remove_irq - free an interrupt
1267 * @irq: Interrupt line to free
1268 * @act: irqaction for the interrupt
1269 *
1270 * Used to remove interrupts statically setup by the early boot process.
1271 */
1272void remove_irq(unsigned int irq, struct irqaction *act)
1273{
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001274 struct irq_desc *desc = irq_to_desc(irq);
1275
1276 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1277 __free_irq(irq, act->dev_id);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001278}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001279EXPORT_SYMBOL_GPL(remove_irq);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001280
1281/**
Magnus Dammf21cfb22009-03-12 21:05:42 +09001282 * free_irq - free an interrupt allocated with request_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 * @irq: Interrupt line to free
1284 * @dev_id: Device identity to free
1285 *
1286 * Remove an interrupt handler. The handler is removed and if the
1287 * interrupt line is no longer in use by any driver it is disabled.
1288 * On a shared IRQ the caller must ensure the interrupt is disabled
1289 * on the card it drives before calling this function. The function
1290 * does not return until any executing interrupts for this IRQ
1291 * have completed.
1292 *
1293 * This function must not be called from interrupt context.
1294 */
1295void free_irq(unsigned int irq, void *dev_id)
1296{
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001297 struct irq_desc *desc = irq_to_desc(irq);
1298
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001299 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001300 return;
1301
Ben Hutchingscd7eab42011-01-19 21:01:44 +00001302#ifdef CONFIG_SMP
1303 if (WARN_ON(desc->affinity_notify))
1304 desc->affinity_notify = NULL;
1305#endif
1306
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001307 chip_bus_lock(desc);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001308 kfree(__free_irq(irq, dev_id));
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001309 chip_bus_sync_unlock(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311EXPORT_SYMBOL(free_irq);
1312
1313/**
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001314 * request_threaded_irq - allocate an interrupt line
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 * @irq: Interrupt line to allocate
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001316 * @handler: Function to be called when the IRQ occurs.
1317 * Primary handler for threaded interrupts
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001318 * If NULL and thread_fn != NULL the default
1319 * primary handler is installed
Thomas Gleixnerf48fe812009-03-24 11:46:22 +01001320 * @thread_fn: Function called from the irq handler thread
1321 * If NULL, no irq thread is created
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * @irqflags: Interrupt type flags
1323 * @devname: An ascii name for the claiming device
1324 * @dev_id: A cookie passed back to the handler function
1325 *
1326 * This call allocates interrupt resources and enables the
1327 * interrupt line and IRQ handling. From the point this
1328 * call is made your handler function may be invoked. Since
1329 * your handler function must clear any interrupt the board
1330 * raises, you must take care both to initialise your hardware
1331 * and to set up the interrupt handler in the right order.
1332 *
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001333 * If you want to set up a threaded irq handler for your device
Javi Merino6d21af42011-10-26 10:16:11 +01001334 * then you need to supply @handler and @thread_fn. @handler is
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001335 * still called in hard interrupt context and has to check
1336 * whether the interrupt originates from the device. If yes it
1337 * needs to disable the interrupt on the device and return
Steven Rostedt39a2edd2009-05-12 14:35:54 -04001338 * IRQ_WAKE_THREAD which will wake up the handler thread and run
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001339 * @thread_fn. This split handler design is necessary to support
1340 * shared interrupts.
1341 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 * Dev_id must be globally unique. Normally the address of the
1343 * device data structure is used as the cookie. Since the handler
1344 * receives this value it makes sense to use it.
1345 *
1346 * If your interrupt is shared you must pass a non NULL dev_id
1347 * as this is required when freeing the interrupt.
1348 *
1349 * Flags:
1350 *
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001351 * IRQF_SHARED Interrupt is shared
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001352 * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
David Brownell0c5d1eb2008-10-01 14:46:18 -07001353 * IRQF_TRIGGER_* Specify active edge(s) or level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 *
1355 */
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001356int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1357 irq_handler_t thread_fn, unsigned long irqflags,
1358 const char *devname, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Ingo Molnar06fcb0c2006-06-29 02:24:40 -07001360 struct irqaction *action;
Yinghai Lu08678b02008-08-19 20:50:05 -07001361 struct irq_desc *desc;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001362 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
David Brownell470c6622008-12-01 14:31:37 -08001364 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * Sanity-check: shared interrupts must pass in a real dev-ID,
1366 * otherwise we'll have trouble later trying to figure out
1367 * which interrupt is which (messes up the interrupt freeing
1368 * logic etc).
1369 */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001370 if ((irqflags & IRQF_SHARED) && !dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001372
Yinghai Lucb5bc832008-08-19 20:50:17 -07001373 desc = irq_to_desc(irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001374 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001376
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001377 if (!irq_settings_can_request(desc) ||
1378 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Thomas Gleixner6550c772006-06-29 02:24:49 -07001379 return -EINVAL;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001380
1381 if (!handler) {
1382 if (!thread_fn)
1383 return -EINVAL;
1384 handler = irq_default_primary_handler;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Thomas Gleixner45535732009-02-22 23:00:32 +01001387 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!action)
1389 return -ENOMEM;
1390
1391 action->handler = handler;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001392 action->thread_fn = thread_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 action->flags = irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 action->name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 action->dev_id = dev_id;
1396
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001397 chip_bus_lock(desc);
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001398 retval = __setup_irq(irq, desc, action);
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001399 chip_bus_sync_unlock(desc);
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001400
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001401 if (retval)
1402 kfree(action);
1403
Thomas Gleixner6d83f942011-02-18 23:27:23 +01001404#ifdef CONFIG_DEBUG_SHIRQ_FIXME
Luis Henriques6ce51c42009-04-01 18:06:35 +01001405 if (!retval && (irqflags & IRQF_SHARED)) {
David Woodhousea304e1b2007-02-12 00:52:00 -08001406 /*
1407 * It's a shared IRQ -- the driver ought to be prepared for it
1408 * to happen immediately, so let's make sure....
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001409 * We disable the irq to make sure that a 'real' IRQ doesn't
1410 * run in parallel with our fake.
David Woodhousea304e1b2007-02-12 00:52:00 -08001411 */
Jarek Poplawski59845b12007-08-30 23:56:34 -07001412 unsigned long flags;
David Woodhousea304e1b2007-02-12 00:52:00 -08001413
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001414 disable_irq(irq);
Jarek Poplawski59845b12007-08-30 23:56:34 -07001415 local_irq_save(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001416
Jarek Poplawski59845b12007-08-30 23:56:34 -07001417 handler(irq, dev_id);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001418
Jarek Poplawski59845b12007-08-30 23:56:34 -07001419 local_irq_restore(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001420 enable_irq(irq);
David Woodhousea304e1b2007-02-12 00:52:00 -08001421 }
1422#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return retval;
1424}
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001425EXPORT_SYMBOL(request_threaded_irq);
Marc Zyngierae731f82010-03-15 22:56:33 +00001426
1427/**
1428 * request_any_context_irq - allocate an interrupt line
1429 * @irq: Interrupt line to allocate
1430 * @handler: Function to be called when the IRQ occurs.
1431 * Threaded handler for threaded interrupts.
1432 * @flags: Interrupt type flags
1433 * @name: An ascii name for the claiming device
1434 * @dev_id: A cookie passed back to the handler function
1435 *
1436 * This call allocates interrupt resources and enables the
1437 * interrupt line and IRQ handling. It selects either a
1438 * hardirq or threaded handling method depending on the
1439 * context.
1440 *
1441 * On failure, it returns a negative value. On success,
1442 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1443 */
1444int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1445 unsigned long flags, const char *name, void *dev_id)
1446{
1447 struct irq_desc *desc = irq_to_desc(irq);
1448 int ret;
1449
1450 if (!desc)
1451 return -EINVAL;
1452
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001453 if (irq_settings_is_nested_thread(desc)) {
Marc Zyngierae731f82010-03-15 22:56:33 +00001454 ret = request_threaded_irq(irq, NULL, handler,
1455 flags, name, dev_id);
1456 return !ret ? IRQC_IS_NESTED : ret;
1457 }
1458
1459 ret = request_irq(irq, handler, flags, name, dev_id);
1460 return !ret ? IRQC_IS_HARDIRQ : ret;
1461}
1462EXPORT_SYMBOL_GPL(request_any_context_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001463
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001464void enable_percpu_irq(unsigned int irq, unsigned int type)
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001465{
1466 unsigned int cpu = smp_processor_id();
1467 unsigned long flags;
1468 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1469
1470 if (!desc)
1471 return;
1472
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001473 type &= IRQ_TYPE_SENSE_MASK;
1474 if (type != IRQ_TYPE_NONE) {
1475 int ret;
1476
1477 ret = __irq_set_trigger(desc, irq, type);
1478
1479 if (ret) {
Thomas Gleixner32cffdd2011-10-04 18:43:57 +02001480 WARN(1, "failed to set type for IRQ%d\n", irq);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001481 goto out;
1482 }
1483 }
1484
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001485 irq_percpu_enable(desc, cpu);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001486out:
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001487 irq_put_desc_unlock(desc, flags);
1488}
1489
1490void disable_percpu_irq(unsigned int irq)
1491{
1492 unsigned int cpu = smp_processor_id();
1493 unsigned long flags;
1494 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1495
1496 if (!desc)
1497 return;
1498
1499 irq_percpu_disable(desc, cpu);
1500 irq_put_desc_unlock(desc, flags);
1501}
1502
1503/*
1504 * Internal function to unregister a percpu irqaction.
1505 */
1506static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1507{
1508 struct irq_desc *desc = irq_to_desc(irq);
1509 struct irqaction *action;
1510 unsigned long flags;
1511
1512 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1513
1514 if (!desc)
1515 return NULL;
1516
1517 raw_spin_lock_irqsave(&desc->lock, flags);
1518
1519 action = desc->action;
1520 if (!action || action->percpu_dev_id != dev_id) {
1521 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1522 goto bad;
1523 }
1524
1525 if (!cpumask_empty(desc->percpu_enabled)) {
1526 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1527 irq, cpumask_first(desc->percpu_enabled));
1528 goto bad;
1529 }
1530
1531 /* Found it - now remove it from the list of entries: */
1532 desc->action = NULL;
1533
1534 raw_spin_unlock_irqrestore(&desc->lock, flags);
1535
1536 unregister_handler_proc(irq, action);
1537
1538 module_put(desc->owner);
1539 return action;
1540
1541bad:
1542 raw_spin_unlock_irqrestore(&desc->lock, flags);
1543 return NULL;
1544}
1545
1546/**
1547 * remove_percpu_irq - free a per-cpu interrupt
1548 * @irq: Interrupt line to free
1549 * @act: irqaction for the interrupt
1550 *
1551 * Used to remove interrupts statically setup by the early boot process.
1552 */
1553void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1554{
1555 struct irq_desc *desc = irq_to_desc(irq);
1556
1557 if (desc && irq_settings_is_per_cpu_devid(desc))
1558 __free_percpu_irq(irq, act->percpu_dev_id);
1559}
1560
1561/**
1562 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
1563 * @irq: Interrupt line to free
1564 * @dev_id: Device identity to free
1565 *
1566 * Remove a percpu interrupt handler. The handler is removed, but
1567 * the interrupt line is not disabled. This must be done on each
1568 * CPU before calling this function. The function does not return
1569 * until any executing interrupts for this IRQ have completed.
1570 *
1571 * This function must not be called from interrupt context.
1572 */
1573void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1574{
1575 struct irq_desc *desc = irq_to_desc(irq);
1576
1577 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1578 return;
1579
1580 chip_bus_lock(desc);
1581 kfree(__free_percpu_irq(irq, dev_id));
1582 chip_bus_sync_unlock(desc);
1583}
1584
1585/**
1586 * setup_percpu_irq - setup a per-cpu interrupt
1587 * @irq: Interrupt line to setup
1588 * @act: irqaction for the interrupt
1589 *
1590 * Used to statically setup per-cpu interrupts in the early boot process.
1591 */
1592int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1593{
1594 struct irq_desc *desc = irq_to_desc(irq);
1595 int retval;
1596
1597 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1598 return -EINVAL;
1599 chip_bus_lock(desc);
1600 retval = __setup_irq(irq, desc, act);
1601 chip_bus_sync_unlock(desc);
1602
1603 return retval;
1604}
1605
1606/**
1607 * request_percpu_irq - allocate a percpu interrupt line
1608 * @irq: Interrupt line to allocate
1609 * @handler: Function to be called when the IRQ occurs.
1610 * @devname: An ascii name for the claiming device
1611 * @dev_id: A percpu cookie passed back to the handler function
1612 *
1613 * This call allocates interrupt resources, but doesn't
1614 * automatically enable the interrupt. It has to be done on each
1615 * CPU using enable_percpu_irq().
1616 *
1617 * Dev_id must be globally unique. It is a per-cpu variable, and
1618 * the handler gets called with the interrupted CPU's instance of
1619 * that variable.
1620 */
1621int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1622 const char *devname, void __percpu *dev_id)
1623{
1624 struct irqaction *action;
1625 struct irq_desc *desc;
1626 int retval;
1627
1628 if (!dev_id)
1629 return -EINVAL;
1630
1631 desc = irq_to_desc(irq);
1632 if (!desc || !irq_settings_can_request(desc) ||
1633 !irq_settings_is_per_cpu_devid(desc))
1634 return -EINVAL;
1635
1636 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1637 if (!action)
1638 return -ENOMEM;
1639
1640 action->handler = handler;
Marc Zyngier2ed0e642011-11-16 12:27:39 +00001641 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001642 action->name = devname;
1643 action->percpu_dev_id = dev_id;
1644
1645 chip_bus_lock(desc);
1646 retval = __setup_irq(irq, desc, action);
1647 chip_bus_sync_unlock(desc);
1648
1649 if (retval)
1650 kfree(action);
1651
1652 return retval;
1653}