blob: b2fc2a581b86108387c5b3ecc434b82171dba0eb [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>
Clark Williams8bd75c72013-02-07 09:47:07 -060019#include <linux/sched/rt.h>
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +100020#include <linux/task_work.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "internals.h"
23
Thomas Gleixner8d32a302011-02-23 23:52:23 +000024#ifdef CONFIG_IRQ_FORCED_THREADING
25__read_mostly bool force_irqthreads;
26
27static int __init setup_forced_irqthreads(char *arg)
28{
29 force_irqthreads = true;
30 return 0;
31}
32early_param("threadirqs", setup_forced_irqthreads);
33#endif
34
Thomas Gleixner18258f72014-02-15 00:55:18 +000035static void __synchronize_hardirq(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Thomas Gleixner32f41252011-03-28 14:10:52 +020037 bool inprogress;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Herbert Xua98ce5c2007-10-23 11:26:25 +080039 do {
40 unsigned long flags;
41
42 /*
43 * Wait until we're out of the critical section. This might
44 * give the wrong answer due to the lack of memory barriers.
45 */
Thomas Gleixner32f41252011-03-28 14:10:52 +020046 while (irqd_irq_inprogress(&desc->irq_data))
Herbert Xua98ce5c2007-10-23 11:26:25 +080047 cpu_relax();
48
49 /* Ok, that indicated we're done: double-check carefully. */
Thomas Gleixner239007b2009-11-17 16:46:45 +010050 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner32f41252011-03-28 14:10:52 +020051 inprogress = irqd_irq_inprogress(&desc->irq_data);
Thomas Gleixner239007b2009-11-17 16:46:45 +010052 raw_spin_unlock_irqrestore(&desc->lock, flags);
Herbert Xua98ce5c2007-10-23 11:26:25 +080053
54 /* Oops, that failed? */
Thomas Gleixner32f41252011-03-28 14:10:52 +020055 } while (inprogress);
Thomas Gleixner18258f72014-02-15 00:55:18 +000056}
Thomas Gleixner3aa551c2009-03-23 18:28:15 +010057
Thomas Gleixner18258f72014-02-15 00:55:18 +000058/**
59 * synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs)
60 * @irq: interrupt number to wait for
61 *
62 * This function waits for any pending hard IRQ handlers for this
63 * interrupt to complete before returning. If you use this
64 * function while holding a resource the IRQ handler may need you
65 * will deadlock. It does not take associated threaded handlers
66 * into account.
67 *
68 * Do not use this for shutdown scenarios where you must be sure
69 * that all parts (hardirq and threaded handler) have completed.
70 *
Peter Zijlstra02cea392015-02-05 14:06:23 +010071 * Returns: false if a threaded handler is active.
72 *
Thomas Gleixner18258f72014-02-15 00:55:18 +000073 * This function may be called - with care - from IRQ context.
74 */
Peter Zijlstra02cea392015-02-05 14:06:23 +010075bool synchronize_hardirq(unsigned int irq)
Thomas Gleixner18258f72014-02-15 00:55:18 +000076{
77 struct irq_desc *desc = irq_to_desc(irq);
78
Peter Zijlstra02cea392015-02-05 14:06:23 +010079 if (desc) {
Thomas Gleixner18258f72014-02-15 00:55:18 +000080 __synchronize_hardirq(desc);
Peter Zijlstra02cea392015-02-05 14:06:23 +010081 return !atomic_read(&desc->threads_active);
82 }
83
84 return true;
Thomas Gleixner18258f72014-02-15 00:55:18 +000085}
86EXPORT_SYMBOL(synchronize_hardirq);
87
88/**
89 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
90 * @irq: interrupt number to wait for
91 *
92 * This function waits for any pending IRQ handlers for this interrupt
93 * to complete before returning. If you use this function while
94 * holding a resource the IRQ handler may need you will deadlock.
95 *
96 * This function may be called - with care - from IRQ context.
97 */
98void synchronize_irq(unsigned int irq)
99{
100 struct irq_desc *desc = irq_to_desc(irq);
101
102 if (desc) {
103 __synchronize_hardirq(desc);
104 /*
105 * We made sure that no hardirq handler is
106 * running. Now verify that no threaded handlers are
107 * active.
108 */
109 wait_event(desc->wait_for_threads,
110 !atomic_read(&desc->threads_active));
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113EXPORT_SYMBOL(synchronize_irq);
114
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100115#ifdef CONFIG_SMP
116cpumask_var_t irq_default_affinity;
117
Thomas Gleixner9c255582016-07-04 17:39:23 +0900118static bool __irq_can_set_affinity(struct irq_desc *desc)
Jiang Liue019c242015-06-23 20:29:34 +0200119{
120 if (!desc || !irqd_can_balance(&desc->irq_data) ||
121 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
Thomas Gleixner9c255582016-07-04 17:39:23 +0900122 return false;
123 return true;
Jiang Liue019c242015-06-23 20:29:34 +0200124}
125
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800126/**
127 * irq_can_set_affinity - Check if the affinity of a given irq can be set
128 * @irq: Interrupt to check
129 *
130 */
131int irq_can_set_affinity(unsigned int irq)
132{
Jiang Liue019c242015-06-23 20:29:34 +0200133 return __irq_can_set_affinity(irq_to_desc(irq));
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800134}
135
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200136/**
Thomas Gleixner9c255582016-07-04 17:39:23 +0900137 * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space
138 * @irq: Interrupt to check
139 *
140 * Like irq_can_set_affinity() above, but additionally checks for the
141 * AFFINITY_MANAGED flag.
142 */
143bool irq_can_set_affinity_usr(unsigned int irq)
144{
145 struct irq_desc *desc = irq_to_desc(irq);
146
147 return __irq_can_set_affinity(desc) &&
148 !irqd_affinity_is_managed(&desc->irq_data);
149}
150
151/**
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200152 * irq_set_thread_affinity - Notify irq threads to adjust affinity
153 * @desc: irq descriptor which has affitnity changed
154 *
155 * We just set IRQTF_AFFINITY and delegate the affinity setting
156 * to the interrupt thread itself. We can not call
157 * set_cpus_allowed_ptr() here as we hold desc->lock and this
158 * code can be called from hard interrupt context.
159 */
160void irq_set_thread_affinity(struct irq_desc *desc)
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100161{
Daniel Lezcanof944b5a2016-01-14 10:54:13 +0100162 struct irqaction *action;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100163
Daniel Lezcanof944b5a2016-01-14 10:54:13 +0100164 for_each_action_of_desc(desc, action)
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100165 if (action->thread)
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200166 set_bit(IRQTF_AFFINITY, &action->thread_flags);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100167}
168
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100169#ifdef CONFIG_GENERIC_PENDING_IRQ
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200170static inline bool irq_can_move_pcntxt(struct irq_data *data)
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100171{
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200172 return irqd_can_move_in_process_context(data);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100173}
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200174static inline bool irq_move_pending(struct irq_data *data)
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100175{
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200176 return irqd_is_setaffinity_pending(data);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100177}
178static inline void
179irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
180{
181 cpumask_copy(desc->pending_mask, mask);
182}
183static inline void
184irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
185{
186 cpumask_copy(mask, desc->pending_mask);
187}
188#else
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200189static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
Thomas Gleixnercd22c0e2011-03-29 11:36:05 +0200190static inline bool irq_move_pending(struct irq_data *data) { return false; }
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100191static inline void
192irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
193static inline void
194irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
195#endif
196
Jiang Liu818b0f32012-03-30 23:11:34 +0800197int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
198 bool force)
199{
200 struct irq_desc *desc = irq_data_to_desc(data);
201 struct irq_chip *chip = irq_data_get_irq_chip(data);
202 int ret;
203
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000204 ret = chip->irq_set_affinity(data, mask, force);
Jiang Liu818b0f32012-03-30 23:11:34 +0800205 switch (ret) {
206 case IRQ_SET_MASK_OK:
Jiang Liu2cb62542014-11-06 22:20:18 +0800207 case IRQ_SET_MASK_OK_DONE:
Jiang Liu9df872f2015-06-03 11:47:50 +0800208 cpumask_copy(desc->irq_common_data.affinity, mask);
Jiang Liu818b0f32012-03-30 23:11:34 +0800209 case IRQ_SET_MASK_OK_NOCOPY:
210 irq_set_thread_affinity(desc);
211 ret = 0;
212 }
213
214 return ret;
215}
216
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000217int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
218 bool force)
David Daneyc2d0c552011-03-25 12:38:50 -0700219{
220 struct irq_chip *chip = irq_data_get_irq_chip(data);
221 struct irq_desc *desc = irq_data_to_desc(data);
222 int ret = 0;
223
224 if (!chip || !chip->irq_set_affinity)
225 return -EINVAL;
226
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200227 if (irq_can_move_pcntxt(data)) {
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000228 ret = irq_do_set_affinity(data, mask, force);
David Daneyc2d0c552011-03-25 12:38:50 -0700229 } else {
230 irqd_set_move_pending(data);
231 irq_copy_pending(desc, mask);
232 }
233
234 if (desc->affinity_notify) {
235 kref_get(&desc->affinity_notify->kref);
Edward Creecc24d742020-03-13 20:33:07 +0000236 if (!schedule_work(&desc->affinity_notify->work)) {
237 /* Work was already scheduled, drop our extra ref */
238 kref_put(&desc->affinity_notify->kref,
239 desc->affinity_notify->release);
240 }
David Daneyc2d0c552011-03-25 12:38:50 -0700241 }
David Daneyc2d0c552011-03-25 12:38:50 -0700242 irqd_set(data, IRQD_AFFINITY_SET);
243
244 return ret;
245}
246
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000247int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800248{
Yinghai Lu08678b02008-08-19 20:50:05 -0700249 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100250 unsigned long flags;
David Daneyc2d0c552011-03-25 12:38:50 -0700251 int ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800252
David Daneyc2d0c552011-03-25 12:38:50 -0700253 if (!desc)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800254 return -EINVAL;
255
Thomas Gleixner239007b2009-11-17 16:46:45 +0100256 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000257 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100258 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100259 return ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800260}
261
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700262int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
263{
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700264 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100265 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700266
267 if (!desc)
268 return -EINVAL;
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700269 desc->affinity_hint = m;
Thomas Gleixner02725e72011-02-12 10:37:36 +0100270 irq_put_desc_unlock(desc, flags);
Jesse Brandeburge2e64a92014-12-18 17:22:06 -0800271 /* set the initial affinity to prevent every interrupt being on CPU0 */
Jesse Brandeburg4fe7ffb2015-01-28 10:57:39 -0800272 if (m)
273 __irq_set_affinity(irq, m, false);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700274 return 0;
275}
276EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
277
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000278static void irq_affinity_notify(struct work_struct *work)
279{
280 struct irq_affinity_notify *notify =
281 container_of(work, struct irq_affinity_notify, work);
282 struct irq_desc *desc = irq_to_desc(notify->irq);
283 cpumask_var_t cpumask;
284 unsigned long flags;
285
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100286 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000287 goto out;
288
289 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200290 if (irq_move_pending(&desc->irq_data))
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100291 irq_get_pending(cpumask, desc);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000292 else
Jiang Liu9df872f2015-06-03 11:47:50 +0800293 cpumask_copy(cpumask, desc->irq_common_data.affinity);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000294 raw_spin_unlock_irqrestore(&desc->lock, flags);
295
296 notify->notify(notify, cpumask);
297
298 free_cpumask_var(cpumask);
299out:
300 kref_put(&notify->kref, notify->release);
301}
302
303/**
304 * irq_set_affinity_notifier - control notification of IRQ affinity changes
305 * @irq: Interrupt for which to enable/disable notification
306 * @notify: Context for notification, or %NULL to disable
307 * notification. Function pointers must be initialised;
308 * the other fields will be initialised by this function.
309 *
310 * Must be called in process context. Notification may only be enabled
311 * after the IRQ is allocated and must be disabled before the IRQ is
312 * freed using free_irq().
313 */
314int
315irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
316{
317 struct irq_desc *desc = irq_to_desc(irq);
318 struct irq_affinity_notify *old_notify;
319 unsigned long flags;
320
321 /* The release function is promised process context */
322 might_sleep();
323
324 if (!desc)
325 return -EINVAL;
326
327 /* Complete initialisation of *notify */
328 if (notify) {
329 notify->irq = irq;
330 kref_init(&notify->kref);
331 INIT_WORK(&notify->work, irq_affinity_notify);
332 }
333
334 raw_spin_lock_irqsave(&desc->lock, flags);
335 old_notify = desc->affinity_notify;
336 desc->affinity_notify = notify;
337 raw_spin_unlock_irqrestore(&desc->lock, flags);
338
Prasad Sodagudi3df0c562019-03-24 07:57:04 -0700339 if (old_notify) {
Edward Creecc24d742020-03-13 20:33:07 +0000340 if (cancel_work_sync(&old_notify->work)) {
341 /* Pending work had a ref, put that one too */
342 kref_put(&old_notify->kref, old_notify->release);
343 }
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000344 kref_put(&old_notify->kref, old_notify->release);
Prasad Sodagudi3df0c562019-03-24 07:57:04 -0700345 }
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000346
347 return 0;
348}
349EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
350
Max Krasnyansky18404752008-05-29 11:02:52 -0700351#ifndef CONFIG_AUTO_IRQ_AFFINITY
352/*
353 * Generic version of the affinity autoselector.
354 */
Jiang Liua8a98ea2015-06-04 12:13:30 +0800355static int setup_affinity(struct irq_desc *desc, struct cpumask *mask)
Max Krasnyansky18404752008-05-29 11:02:52 -0700356{
Thomas Gleixner569bda82011-02-07 17:05:08 +0100357 struct cpumask *set = irq_default_affinity;
Jiang Liu67830112015-06-01 16:05:13 +0800358 int node = irq_desc_get_node(desc);
Thomas Gleixner569bda82011-02-07 17:05:08 +0100359
Thomas Gleixnerb0082072011-02-07 17:30:50 +0100360 /* Excludes PER_CPU and NO_BALANCE interrupts */
Jiang Liue019c242015-06-23 20:29:34 +0200361 if (!__irq_can_set_affinity(desc))
Max Krasnyansky18404752008-05-29 11:02:52 -0700362 return 0;
363
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100364 /*
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900365 * Preserve the managed affinity setting and an userspace affinity
366 * setup, but make sure that one of the targets is online.
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100367 */
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900368 if (irqd_affinity_is_managed(&desc->irq_data) ||
369 irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
Jiang Liu9df872f2015-06-03 11:47:50 +0800370 if (cpumask_intersects(desc->irq_common_data.affinity,
Thomas Gleixner569bda82011-02-07 17:05:08 +0100371 cpu_online_mask))
Jiang Liu9df872f2015-06-03 11:47:50 +0800372 set = desc->irq_common_data.affinity;
Thomas Gleixner0c6f8a82011-03-28 13:32:20 +0200373 else
Thomas Gleixner2bdd1052011-02-08 17:22:00 +0100374 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100375 }
Max Krasnyansky18404752008-05-29 11:02:52 -0700376
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100377 cpumask_and(mask, cpu_online_mask, set);
Prarit Bhargava241fc642012-03-26 15:02:18 -0400378 if (node != NUMA_NO_NODE) {
379 const struct cpumask *nodemask = cpumask_of_node(node);
380
381 /* make sure at least one of the cpus in nodemask is online */
382 if (cpumask_intersects(mask, nodemask))
383 cpumask_and(mask, mask, nodemask);
384 }
Jiang Liu818b0f32012-03-30 23:11:34 +0800385 irq_do_set_affinity(&desc->irq_data, mask, false);
Max Krasnyansky18404752008-05-29 11:02:52 -0700386 return 0;
387}
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100388#else
Jiang Liua8a98ea2015-06-04 12:13:30 +0800389/* Wrapper for ALPHA specific affinity selector magic */
390static inline int setup_affinity(struct irq_desc *d, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100391{
Jiang Liua8a98ea2015-06-04 12:13:30 +0800392 return irq_select_affinity(irq_desc_get_irq(d));
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100393}
Max Krasnyansky18404752008-05-29 11:02:52 -0700394#endif
395
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100396/*
397 * Called when affinity is set via /proc/irq
398 */
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100399int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100400{
401 struct irq_desc *desc = irq_to_desc(irq);
402 unsigned long flags;
403 int ret;
404
Thomas Gleixner239007b2009-11-17 16:46:45 +0100405 raw_spin_lock_irqsave(&desc->lock, flags);
Jiang Liua8a98ea2015-06-04 12:13:30 +0800406 ret = setup_affinity(desc, mask);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100407 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100408 return ret;
409}
410
411#else
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100412static inline int
Jiang Liua8a98ea2015-06-04 12:13:30 +0800413setup_affinity(struct irq_desc *desc, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100414{
415 return 0;
416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#endif
418
Feng Wufcf1ae22015-10-03 16:20:38 +0800419/**
420 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
421 * @irq: interrupt number to set affinity
422 * @vcpu_info: vCPU specific data
423 *
424 * This function uses the vCPU specific data to set the vCPU
425 * affinity for an irq. The vCPU specific data is passed from
426 * outside, such as KVM. One example code path is as below:
427 * KVM -> IOMMU -> irq_set_vcpu_affinity().
428 */
429int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
430{
431 unsigned long flags;
432 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
433 struct irq_data *data;
434 struct irq_chip *chip;
435 int ret = -ENOSYS;
436
437 if (!desc)
438 return -EINVAL;
439
440 data = irq_desc_get_irq_data(desc);
441 chip = irq_data_get_irq_chip(data);
442 if (chip && chip->irq_set_vcpu_affinity)
443 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
444 irq_put_desc_unlock(desc, flags);
445
446 return ret;
447}
448EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
449
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200450void __disable_irq(struct irq_desc *desc)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100451{
Thomas Gleixner3aae9942011-02-04 10:17:52 +0100452 if (!desc->depth++)
Thomas Gleixner87923472011-02-03 12:27:44 +0100453 irq_disable(desc);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100454}
455
Thomas Gleixner02725e72011-02-12 10:37:36 +0100456static int __disable_irq_nosync(unsigned int irq)
457{
458 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100459 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100460
461 if (!desc)
462 return -EINVAL;
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200463 __disable_irq(desc);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100464 irq_put_desc_busunlock(desc, flags);
465 return 0;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/**
469 * disable_irq_nosync - disable an irq without waiting
470 * @irq: Interrupt to disable
471 *
472 * Disable the selected interrupt line. Disables and Enables are
473 * nested.
474 * Unlike disable_irq(), this function does not ensure existing
475 * instances of the IRQ handler have completed before returning.
476 *
477 * This function may be called from IRQ context.
478 */
479void disable_irq_nosync(unsigned int irq)
480{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100481 __disable_irq_nosync(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483EXPORT_SYMBOL(disable_irq_nosync);
484
485/**
486 * disable_irq - disable an irq and wait for completion
487 * @irq: Interrupt to disable
488 *
489 * Disable the selected interrupt line. Enables and Disables are
490 * nested.
491 * This function waits for any pending IRQ handlers for this interrupt
492 * to complete before returning. If you use this function while
493 * holding a resource the IRQ handler may need you will deadlock.
494 *
495 * This function may be called - with care - from IRQ context.
496 */
497void disable_irq(unsigned int irq)
498{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100499 if (!__disable_irq_nosync(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 synchronize_irq(irq);
501}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502EXPORT_SYMBOL(disable_irq);
503
Peter Zijlstra02cea392015-02-05 14:06:23 +0100504/**
505 * disable_hardirq - disables an irq and waits for hardirq completion
506 * @irq: Interrupt to disable
507 *
508 * Disable the selected interrupt line. Enables and Disables are
509 * nested.
510 * This function waits for any pending hard IRQ handlers for this
511 * interrupt to complete before returning. If you use this function while
512 * holding a resource the hard IRQ handler may need you will deadlock.
513 *
514 * When used to optimistically disable an interrupt from atomic context
515 * the return value must be checked.
516 *
517 * Returns: false if a threaded handler is active.
518 *
519 * This function may be called - with care - from IRQ context.
520 */
521bool disable_hardirq(unsigned int irq)
522{
523 if (!__disable_irq_nosync(irq))
524 return synchronize_hardirq(irq);
525
526 return false;
527}
528EXPORT_SYMBOL_GPL(disable_hardirq);
529
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200530void __enable_irq(struct irq_desc *desc)
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200531{
532 switch (desc->depth) {
533 case 0:
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100534 err_out:
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200535 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
536 irq_desc_get_irq(desc));
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200537 break;
538 case 1: {
Thomas Gleixnerc531e832011-02-08 12:44:58 +0100539 if (desc->istate & IRQS_SUSPENDED)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100540 goto err_out;
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200541 /* Prevent probing on this irq: */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +0100542 irq_settings_set_noprobe(desc);
Thomas Gleixner3aae9942011-02-04 10:17:52 +0100543 irq_enable(desc);
Jiang Liu0798abe2015-06-04 12:13:27 +0800544 check_irq_resend(desc);
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200545 /* fall-through */
546 }
547 default:
548 desc->depth--;
549 }
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/**
553 * enable_irq - enable handling of an irq
554 * @irq: Interrupt to enable
555 *
556 * Undoes the effect of one call to disable_irq(). If this
557 * matches the last disable, processing of interrupts on this
558 * IRQ line is re-enabled.
559 *
Thomas Gleixner70aedd22009-08-13 12:17:48 +0200560 * This function may be called from IRQ context only when
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200561 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
563void enable_irq(unsigned int irq)
564{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100566 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700568 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -0700569 return;
Thomas Gleixner50f7c032011-02-03 13:23:54 +0100570 if (WARN(!desc->irq_data.chip,
571 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
Thomas Gleixner02725e72011-02-12 10:37:36 +0100572 goto out;
Thomas Gleixner2656c362010-10-22 14:47:57 +0200573
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200574 __enable_irq(desc);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100575out:
576 irq_put_desc_busunlock(desc, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578EXPORT_SYMBOL(enable_irq);
579
David Brownell0c5d1eb2008-10-01 14:46:18 -0700580static int set_irq_wake_real(unsigned int irq, unsigned int on)
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200581{
Yinghai Lu08678b02008-08-19 20:50:05 -0700582 struct irq_desc *desc = irq_to_desc(irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200583 int ret = -ENXIO;
584
Santosh Shilimkar60f96b42011-09-09 13:59:35 +0530585 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
586 return 0;
587
Thomas Gleixner2f7e99b2010-09-27 12:45:50 +0000588 if (desc->irq_data.chip->irq_set_wake)
589 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200590
591 return ret;
592}
593
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700594/**
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100595 * irq_set_irq_wake - control irq power management wakeup
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700596 * @irq: interrupt to control
597 * @on: enable/disable power management wakeup
598 *
David Brownell15a647e2006-07-30 03:03:08 -0700599 * Enable/disable power management wakeup mode, which is
600 * disabled by default. Enables and disables must match,
601 * just as they match for non-wakeup mode support.
602 *
603 * Wakeup mode lets this IRQ wake the system from sleep
604 * states like "suspend to RAM".
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700605 */
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100606int irq_set_irq_wake(unsigned int irq, unsigned int on)
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700607{
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700608 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100609 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200610 int ret = 0;
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700611
Jesper Juhl13863a62011-06-09 23:14:58 +0200612 if (!desc)
613 return -EINVAL;
614
David Brownell15a647e2006-07-30 03:03:08 -0700615 /* wakeup-capable irqs can be shared between drivers that
616 * don't need to have the same sleep mode behaviors.
617 */
David Brownell15a647e2006-07-30 03:03:08 -0700618 if (on) {
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200619 if (desc->wake_depth++ == 0) {
620 ret = set_irq_wake_real(irq, on);
621 if (ret)
622 desc->wake_depth = 0;
623 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100624 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200625 }
David Brownell15a647e2006-07-30 03:03:08 -0700626 } else {
627 if (desc->wake_depth == 0) {
Arjan van de Ven7a2c4772008-07-25 01:45:54 -0700628 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200629 } else if (--desc->wake_depth == 0) {
630 ret = set_irq_wake_real(irq, on);
631 if (ret)
632 desc->wake_depth = 1;
633 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100634 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200635 }
David Brownell15a647e2006-07-30 03:03:08 -0700636 }
Thomas Gleixner02725e72011-02-12 10:37:36 +0100637 irq_put_desc_busunlock(desc, flags);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700638 return ret;
639}
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100640EXPORT_SYMBOL(irq_set_irq_wake);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * Internal function that tells the architecture code whether a
644 * particular irq has been exclusively allocated or is available
645 * for driver use.
646 */
647int can_request_irq(unsigned int irq, unsigned long irqflags)
648{
Thomas Gleixnercc8c3b72010-03-23 22:40:53 +0100649 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100650 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100651 int canrequest = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700653 if (!desc)
654 return 0;
655
Thomas Gleixner02725e72011-02-12 10:37:36 +0100656 if (irq_settings_can_request(desc)) {
Ben Hutchings2779db82013-06-28 02:40:30 +0100657 if (!desc->action ||
658 irqflags & desc->action->flags & IRQF_SHARED)
659 canrequest = 1;
Thomas Gleixner02725e72011-02-12 10:37:36 +0100660 }
661 irq_put_desc_unlock(desc, flags);
662 return canrequest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Jiang Liua1ff5412015-06-23 19:47:29 +0200665int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700666{
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200667 struct irq_chip *chip = desc->irq_data.chip;
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100668 int ret, unmask = 0;
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700669
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000670 if (!chip || !chip->irq_set_type) {
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700671 /*
672 * IRQF_TRIGGER_* but the PIC does not support multiple
673 * flow-types?
674 */
Jiang Liua1ff5412015-06-23 19:47:29 +0200675 pr_debug("No set_type function for IRQ %d (%s)\n",
676 irq_desc_get_irq(desc),
Thomas Gleixnerf5d89472012-04-19 12:06:13 +0200677 chip ? (chip->name ? : "unknown") : "unknown");
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700678 return 0;
679 }
680
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100681 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
Thomas Gleixner32f41252011-03-28 14:10:52 +0200682 if (!irqd_irq_masked(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100683 mask_irq(desc);
Thomas Gleixner32f41252011-03-28 14:10:52 +0200684 if (!irqd_irq_disabled(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100685 unmask = 1;
686 }
687
Alexander Kuleshov00b992d2016-07-19 15:54:08 +0600688 /* Mask all flags except trigger mode */
689 flags &= IRQ_TYPE_SENSE_MASK;
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000690 ret = chip->irq_set_type(&desc->irq_data, flags);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700691
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100692 switch (ret) {
693 case IRQ_SET_MASK_OK:
Jiang Liu2cb62542014-11-06 22:20:18 +0800694 case IRQ_SET_MASK_OK_DONE:
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100695 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
696 irqd_set(&desc->irq_data, flags);
697
698 case IRQ_SET_MASK_OK_NOCOPY:
699 flags = irqd_get_trigger_type(&desc->irq_data);
700 irq_settings_set_trigger_mask(desc, flags);
701 irqd_clear(&desc->irq_data, IRQD_LEVEL);
702 irq_settings_clr_level(desc);
703 if (flags & IRQ_TYPE_LEVEL_MASK) {
704 irq_settings_set_level(desc);
705 irqd_set(&desc->irq_data, IRQD_LEVEL);
706 }
Thomas Gleixner46732472010-06-07 17:53:51 +0200707
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100708 ret = 0;
Thomas Gleixner8fff39e2011-02-21 14:19:42 +0100709 break;
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100710 default:
Andrew Morton97fd75b2012-05-31 16:26:07 -0700711 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
Jiang Liua1ff5412015-06-23 19:47:29 +0200712 flags, irq_desc_get_irq(desc), chip->irq_set_type);
David Brownell0c5d1eb2008-10-01 14:46:18 -0700713 }
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100714 if (unmask)
715 unmask_irq(desc);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700716 return ret;
717}
718
Thomas Gleixner293a7a02012-10-16 15:07:49 -0700719#ifdef CONFIG_HARDIRQS_SW_RESEND
720int irq_set_parent(int irq, int parent_irq)
721{
722 unsigned long flags;
723 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
724
725 if (!desc)
726 return -EINVAL;
727
728 desc->parent_irq = parent_irq;
729
730 irq_put_desc_unlock(desc, flags);
731 return 0;
732}
Sudip Mukherjee3118dac2016-10-06 23:06:43 +0530733EXPORT_SYMBOL_GPL(irq_set_parent);
Thomas Gleixner293a7a02012-10-16 15:07:49 -0700734#endif
735
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200736/*
737 * Default primary interrupt handler for threaded interrupts. Is
738 * assigned as primary handler when request_threaded_irq is called
739 * with handler == NULL. Useful for oneshot interrupts.
740 */
741static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
742{
743 return IRQ_WAKE_THREAD;
744}
745
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200746/*
747 * Primary handler for nested threaded interrupts. Should never be
748 * called.
749 */
750static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
751{
752 WARN(1, "Primary handler called for nested irq %d\n", irq);
753 return IRQ_NONE;
754}
755
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200756static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
757{
758 WARN(1, "Secondary action handler called for irq %d\n", irq);
759 return IRQ_NONE;
760}
761
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100762static int irq_wait_for_interrupt(struct irqaction *action)
763{
Ido Yariv550acb12011-12-01 13:55:08 +0200764 set_current_state(TASK_INTERRUPTIBLE);
765
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100766 while (!kthread_should_stop()) {
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100767
768 if (test_and_clear_bit(IRQTF_RUNTHREAD,
769 &action->thread_flags)) {
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100770 __set_current_state(TASK_RUNNING);
771 return 0;
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100772 }
773 schedule();
Ido Yariv550acb12011-12-01 13:55:08 +0200774 set_current_state(TASK_INTERRUPTIBLE);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100775 }
Ido Yariv550acb12011-12-01 13:55:08 +0200776 __set_current_state(TASK_RUNNING);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100777 return -1;
778}
779
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200780/*
781 * Oneshot interrupts keep the irq line masked until the threaded
782 * handler finished. unmask if the interrupt has not been disabled and
783 * is marked MASKED.
784 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000785static void irq_finalize_oneshot(struct irq_desc *desc,
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100786 struct irqaction *action)
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200787{
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200788 if (!(desc->istate & IRQS_ONESHOT) ||
789 action->handler == irq_forced_secondary_handler)
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000790 return;
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100791again:
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000792 chip_bus_lock(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100793 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100794
795 /*
796 * Implausible though it may be we need to protect us against
797 * the following scenario:
798 *
799 * The thread is faster done than the hard interrupt handler
800 * on the other CPU. If we unmask the irq line then the
801 * interrupt can come in again and masks the line, leaves due
Thomas Gleixner009b4c32011-02-07 21:48:49 +0100802 * to IRQS_INPROGRESS and the irq line is masked forever.
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000803 *
804 * This also serializes the state of shared oneshot handlers
805 * versus "desc->threads_onehsot |= action->thread_mask;" in
806 * irq_wake_thread(). See the comment there which explains the
807 * serialization.
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100808 */
Thomas Gleixner32f41252011-03-28 14:10:52 +0200809 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100810 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000811 chip_bus_sync_unlock(desc);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100812 cpu_relax();
813 goto again;
814 }
815
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000816 /*
817 * Now check again, whether the thread should run. Otherwise
818 * we would clear the threads_oneshot bit of this thread which
819 * was just set.
820 */
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100821 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000822 goto out_unlock;
823
824 desc->threads_oneshot &= ~action->thread_mask;
825
Thomas Gleixner32f41252011-03-28 14:10:52 +0200826 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
827 irqd_irq_masked(&desc->irq_data))
Thomas Gleixner328a4972014-03-13 19:03:51 +0100828 unmask_threaded_irq(desc);
Thomas Gleixner32f41252011-03-28 14:10:52 +0200829
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000830out_unlock:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100831 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000832 chip_bus_sync_unlock(desc);
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200833}
834
Bruno Premont61f38262009-07-22 22:22:32 +0200835#ifdef CONFIG_SMP
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100836/*
Chuansheng Liub04c6442014-02-10 16:13:57 +0800837 * Check whether we need to change the affinity of the interrupt thread.
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200838 */
839static void
840irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
841{
842 cpumask_var_t mask;
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100843 bool valid = true;
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200844
845 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
846 return;
847
848 /*
849 * In case we are out of memory we set IRQTF_AFFINITY again and
850 * try again next time
851 */
852 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
853 set_bit(IRQTF_AFFINITY, &action->thread_flags);
854 return;
855 }
856
Thomas Gleixner239007b2009-11-17 16:46:45 +0100857 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100858 /*
859 * This code is triggered unconditionally. Check the affinity
860 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
861 */
Matthias Kaehlcke02e3a7d2017-04-12 11:20:30 -0700862 if (cpumask_available(desc->irq_common_data.affinity))
Jiang Liu9df872f2015-06-03 11:47:50 +0800863 cpumask_copy(mask, desc->irq_common_data.affinity);
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100864 else
865 valid = false;
Thomas Gleixner239007b2009-11-17 16:46:45 +0100866 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200867
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100868 if (valid)
869 set_cpus_allowed_ptr(current, mask);
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200870 free_cpumask_var(mask);
871}
Bruno Premont61f38262009-07-22 22:22:32 +0200872#else
873static inline void
874irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
875#endif
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200876
877/*
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000878 * Interrupts which are not explicitely requested as threaded
879 * interrupts rely on the implicit bh/preempt disable of the hard irq
880 * context. So we need to disable bh here to avoid deadlocks and other
881 * side effects.
882 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200883static irqreturn_t
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000884irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
885{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200886 irqreturn_t ret;
887
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000888 local_bh_disable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200889 ret = action->thread_fn(action->irq, action->dev_id);
Lukas Wunner404a83a2018-10-18 15:15:05 +0200890 if (ret == IRQ_HANDLED)
891 atomic_inc(&desc->threads_handled);
892
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100893 irq_finalize_oneshot(desc, action);
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000894 local_bh_enable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200895 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000896}
897
898/*
Xie XiuQif788e7b2013-10-18 09:12:04 +0800899 * Interrupts explicitly requested as threaded interrupts want to be
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000900 * preemtible - many of them need to sleep and wait for slow busses to
901 * complete.
902 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200903static irqreturn_t irq_thread_fn(struct irq_desc *desc,
904 struct irqaction *action)
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000905{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200906 irqreturn_t ret;
907
908 ret = action->thread_fn(action->irq, action->dev_id);
Lukas Wunner404a83a2018-10-18 15:15:05 +0200909 if (ret == IRQ_HANDLED)
910 atomic_inc(&desc->threads_handled);
911
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100912 irq_finalize_oneshot(desc, action);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200913 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000914}
915
Ido Yariv7140ea12011-12-02 18:24:12 +0200916static void wake_threads_waitq(struct irq_desc *desc)
917{
Chuansheng Liuc6856892014-02-24 11:29:50 +0800918 if (atomic_dec_and_test(&desc->threads_active))
Ido Yariv7140ea12011-12-02 18:24:12 +0200919 wake_up(&desc->wait_for_threads);
920}
921
Al Viro67d12142012-06-27 11:07:19 +0400922static void irq_thread_dtor(struct callback_head *unused)
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000923{
924 struct task_struct *tsk = current;
925 struct irq_desc *desc;
926 struct irqaction *action;
927
928 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
929 return;
930
931 action = kthread_data(tsk);
932
Linus Torvaldsfb21aff2012-05-31 18:47:30 -0700933 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
Alan Cox19af3952012-12-18 14:21:25 -0800934 tsk->comm, tsk->pid, action->irq);
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000935
936
937 desc = irq_to_desc(action->irq);
938 /*
939 * If IRQTF_RUNTHREAD is set, we need to decrement
940 * desc->threads_active and wake possible waiters.
941 */
942 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
943 wake_threads_waitq(desc);
944
945 /* Prevent a stale desc->threads_oneshot */
946 irq_finalize_oneshot(desc, action);
947}
948
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200949static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
950{
951 struct irqaction *secondary = action->secondary;
952
953 if (WARN_ON_ONCE(!secondary))
954 return;
955
956 raw_spin_lock_irq(&desc->lock);
957 __irq_wake_thread(desc, secondary);
958 raw_spin_unlock_irq(&desc->lock);
959}
960
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000961/*
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100962 * Interrupt handler thread
963 */
964static int irq_thread(void *data)
965{
Al Viro67d12142012-06-27 11:07:19 +0400966 struct callback_head on_exit_work;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100967 struct irqaction *action = data;
968 struct irq_desc *desc = irq_to_desc(action->irq);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200969 irqreturn_t (*handler_fn)(struct irq_desc *desc,
970 struct irqaction *action);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100971
Alexander Gordeev540b60e2012-03-09 14:59:13 +0100972 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000973 &action->thread_flags))
974 handler_fn = irq_forced_thread_fn;
975 else
976 handler_fn = irq_thread_fn;
977
Al Viro41f9d292012-06-26 22:10:04 +0400978 init_task_work(&on_exit_work, irq_thread_dtor);
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000979 task_work_add(current, &on_exit_work, false);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100980
Sankara Muthukrishnanf3de44e2012-10-31 15:41:23 -0500981 irq_thread_check_affinity(desc, action);
982
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100983 while (!irq_wait_for_interrupt(action)) {
Ido Yariv7140ea12011-12-02 18:24:12 +0200984 irqreturn_t action_ret;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100985
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200986 irq_thread_check_affinity(desc, action);
987
Ido Yariv7140ea12011-12-02 18:24:12 +0200988 action_ret = handler_fn(desc, action);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200989 if (action_ret == IRQ_WAKE_THREAD)
990 irq_wake_secondary(desc, action);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100991
Ido Yariv7140ea12011-12-02 18:24:12 +0200992 wake_threads_waitq(desc);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100993 }
994
Ido Yariv7140ea12011-12-02 18:24:12 +0200995 /*
996 * This is the regular exit path. __free_irq() is stopping the
997 * thread via kthread_stop() after calling
998 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
Thomas Gleixnere04268b2012-03-15 22:55:21 +0100999 * oneshot mask bit can be set. We cannot verify that as we
1000 * cannot touch the oneshot mask at this point anymore as
1001 * __setup_irq() might have given out currents thread_mask
1002 * again.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001003 */
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +10001004 task_work_cancel(current, irq_thread_dtor);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001005 return 0;
1006}
1007
Thomas Gleixnera92444c2014-02-15 00:55:19 +00001008/**
1009 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1010 * @irq: Interrupt line
1011 * @dev_id: Device identity for which the thread should be woken
1012 *
1013 */
1014void irq_wake_thread(unsigned int irq, void *dev_id)
1015{
1016 struct irq_desc *desc = irq_to_desc(irq);
1017 struct irqaction *action;
1018 unsigned long flags;
1019
1020 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1021 return;
1022
1023 raw_spin_lock_irqsave(&desc->lock, flags);
Daniel Lezcanof944b5a2016-01-14 10:54:13 +01001024 for_each_action_of_desc(desc, action) {
Thomas Gleixnera92444c2014-02-15 00:55:19 +00001025 if (action->dev_id == dev_id) {
1026 if (action->thread)
1027 __irq_wake_thread(desc, action);
1028 break;
1029 }
1030 }
1031 raw_spin_unlock_irqrestore(&desc->lock, flags);
1032}
1033EXPORT_SYMBOL_GPL(irq_wake_thread);
1034
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001035static int irq_setup_forced_threading(struct irqaction *new)
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001036{
1037 if (!force_irqthreads)
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001038 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001039 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001040 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001041
Thomas Gleixnereecd08a2018-08-03 14:44:59 +02001042 /*
1043 * No further action required for interrupts which are requested as
1044 * threaded interrupts already
1045 */
1046 if (new->handler == irq_default_primary_handler)
1047 return 0;
1048
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001049 new->flags |= IRQF_ONESHOT;
1050
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001051 /*
1052 * Handle the case where we have a real primary handler and a
1053 * thread handler. We force thread them as well by creating a
1054 * secondary action.
1055 */
Thomas Gleixnereecd08a2018-08-03 14:44:59 +02001056 if (new->handler && new->thread_fn) {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001057 /* Allocate the secondary action */
1058 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1059 if (!new->secondary)
1060 return -ENOMEM;
1061 new->secondary->handler = irq_forced_secondary_handler;
1062 new->secondary->thread_fn = new->thread_fn;
1063 new->secondary->dev_id = new->dev_id;
1064 new->secondary->irq = new->irq;
1065 new->secondary->name = new->name;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001066 }
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001067 /* Deal with the primary handler */
1068 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1069 new->thread_fn = new->handler;
1070 new->handler = irq_default_primary_handler;
1071 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001072}
1073
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001074static int irq_request_resources(struct irq_desc *desc)
1075{
1076 struct irq_data *d = &desc->irq_data;
1077 struct irq_chip *c = d->chip;
1078
1079 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1080}
1081
1082static void irq_release_resources(struct irq_desc *desc)
1083{
1084 struct irq_data *d = &desc->irq_data;
1085 struct irq_chip *c = d->chip;
1086
1087 if (c->irq_release_resources)
1088 c->irq_release_resources(d);
1089}
1090
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001091static int
1092setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1093{
1094 struct task_struct *t;
1095 struct sched_param param = {
1096 .sched_priority = MAX_USER_RT_PRIO/2,
1097 };
1098
1099 if (!secondary) {
1100 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1101 new->name);
1102 } else {
1103 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1104 new->name);
1105 param.sched_priority -= 1;
1106 }
1107
1108 if (IS_ERR(t))
1109 return PTR_ERR(t);
1110
1111 sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1112
1113 /*
1114 * We keep the reference to the task struct even if
1115 * the thread dies to avoid that the interrupt code
1116 * references an already freed task_struct.
1117 */
1118 get_task_struct(t);
1119 new->thread = t;
1120 /*
1121 * Tell the thread to set its affinity. This is
1122 * important for shared interrupt handlers as we do
1123 * not invoke setup_affinity() for the secondary
1124 * handlers as everything is already set up. Even for
1125 * interrupts marked with IRQF_NO_BALANCE this is
1126 * correct as we want the thread to move to the cpu(s)
1127 * on which the requesting code placed the interrupt.
1128 */
1129 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1130 return 0;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/*
1134 * Internal function to register an irqaction - typically used to
1135 * allocate special interrupts that are part of the architecture.
1136 */
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001137static int
Ingo Molnar327ec562009-02-15 11:21:37 +01001138__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Ingo Molnarf17c7542009-02-17 20:43:37 +01001140 struct irqaction *old, **old_ptr;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001141 unsigned long flags, thread_mask = 0;
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001142 int ret, nested, shared = 0;
1143 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001145 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -07001146 return -EINVAL;
1147
Thomas Gleixner6b8ff312010-10-01 12:58:38 +02001148 if (desc->irq_data.chip == &no_irq_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -ENOSYS;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001150 if (!try_module_get(desc->owner))
1151 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001153 new->irq = irq;
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
Jon Hunter4b357da2016-06-07 16:12:27 +01001156 * If the trigger type is not specified by the caller,
1157 * then use the default for this interrupt.
1158 */
1159 if (!(new->flags & IRQF_TRIGGER_MASK))
1160 new->flags |= irqd_get_trigger_type(&desc->irq_data);
1161
1162 /*
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001163 * Check whether the interrupt nests into another interrupt
1164 * thread.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001165 */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001166 nested = irq_settings_is_nested_thread(desc);
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001167 if (nested) {
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001168 if (!new->thread_fn) {
1169 ret = -EINVAL;
1170 goto out_mput;
1171 }
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001172 /*
1173 * Replace the primary handler which was provided from
1174 * the driver for non nested interrupt handling by the
1175 * dummy function which warns when called.
1176 */
1177 new->handler = irq_nested_primary_handler;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001178 } else {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001179 if (irq_settings_can_thread(desc)) {
1180 ret = irq_setup_forced_threading(new);
1181 if (ret)
1182 goto out_mput;
1183 }
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001184 }
1185
1186 /*
1187 * Create a handler thread when a thread function is supplied
1188 * and the interrupt does not nest into another interrupt
1189 * thread.
1190 */
1191 if (new->thread_fn && !nested) {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001192 ret = setup_irq_thread(new, irq, false);
1193 if (ret)
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001194 goto out_mput;
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001195 if (new->secondary) {
1196 ret = setup_irq_thread(new->secondary, irq, true);
1197 if (ret)
1198 goto out_thread;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001199 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001200 }
1201
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001202 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1203 ret = -ENOMEM;
1204 goto out_thread;
1205 }
1206
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001207 /*
Thomas Gleixnerdc9b2292012-07-13 19:29:45 +02001208 * Drivers are often written to work w/o knowledge about the
1209 * underlying irq chip implementation, so a request for a
1210 * threaded irq without a primary hard irq context handler
1211 * requires the ONESHOT flag to be set. Some irq chips like
1212 * MSI based interrupts are per se one shot safe. Check the
1213 * chip flags, so we can avoid the unmask dance at the end of
1214 * the threaded handler for those.
1215 */
1216 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1217 new->flags &= ~IRQF_ONESHOT;
1218
1219 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * The following block of code has to be executed atomically
1221 */
Thomas Gleixner239007b2009-11-17 16:46:45 +01001222 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarf17c7542009-02-17 20:43:37 +01001223 old_ptr = &desc->action;
1224 old = *old_ptr;
Ingo Molnar06fcb0c2006-06-29 02:24:40 -07001225 if (old) {
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001226 /*
1227 * Can't share interrupts unless both agree to and are
1228 * the same type (level, edge, polarity). So both flag
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001229 * fields must have IRQF_SHARED set and the bits which
Thomas Gleixner9d591ed2011-02-23 23:52:16 +00001230 * set the trigger type must match. Also all must
1231 * agree on ONESHOT.
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001232 */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001233 if (!((old->flags & new->flags) & IRQF_SHARED) ||
Greg Kroah-Hartman6c9ca572018-03-30 10:53:44 +02001234 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001235 ((old->flags ^ new->flags) & IRQF_ONESHOT))
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001236 goto mismatch;
1237
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001238 /* All handlers must agree on per-cpuness */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001239 if ((old->flags & IRQF_PERCPU) !=
1240 (new->flags & IRQF_PERCPU))
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001241 goto mismatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* add new interrupt at end of irq queue */
1244 do {
Thomas Gleixner52abb702012-03-06 23:18:54 +01001245 /*
1246 * Or all existing action->thread_mask bits,
1247 * so we can find the next zero bit for this
1248 * new action.
1249 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001250 thread_mask |= old->thread_mask;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001251 old_ptr = &old->next;
1252 old = *old_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 } while (old);
1254 shared = 1;
1255 }
1256
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001257 /*
Thomas Gleixner52abb702012-03-06 23:18:54 +01001258 * Setup the thread mask for this irqaction for ONESHOT. For
1259 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1260 * conditional in irq_wake_thread().
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001261 */
Thomas Gleixner52abb702012-03-06 23:18:54 +01001262 if (new->flags & IRQF_ONESHOT) {
1263 /*
1264 * Unlikely to have 32 resp 64 irqs sharing one line,
1265 * but who knows.
1266 */
1267 if (thread_mask == ~0UL) {
1268 ret = -EBUSY;
1269 goto out_mask;
1270 }
1271 /*
1272 * The thread_mask for the action is or'ed to
1273 * desc->thread_active to indicate that the
1274 * IRQF_ONESHOT thread handler has been woken, but not
1275 * yet finished. The bit is cleared when a thread
1276 * completes. When all threads of a shared interrupt
1277 * line have completed desc->threads_active becomes
1278 * zero and the interrupt line is unmasked. See
1279 * handle.c:irq_wake_thread() for further information.
1280 *
1281 * If no thread is woken by primary (hard irq context)
1282 * interrupt handlers, then desc->threads_active is
1283 * also checked for zero to unmask the irq line in the
1284 * affected hard irq flow handlers
1285 * (handle_[fasteoi|level]_irq).
1286 *
1287 * The new action gets the first zero bit of
1288 * thread_mask assigned. See the loop above which or's
1289 * all existing action->thread_mask bits.
1290 */
1291 new->thread_mask = 1 << ffz(thread_mask);
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001292
Thomas Gleixnerdc9b2292012-07-13 19:29:45 +02001293 } else if (new->handler == irq_default_primary_handler &&
1294 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001295 /*
1296 * The interrupt was requested with handler = NULL, so
1297 * we use the default primary handler for it. But it
1298 * does not have the oneshot flag set. In combination
1299 * with level interrupts this is deadly, because the
1300 * default primary handler just wakes the thread, then
1301 * the irq lines is reenabled, but the device still
1302 * has the level irq asserted. Rinse and repeat....
1303 *
1304 * While this works for edge type interrupts, we play
1305 * it safe and reject unconditionally because we can't
1306 * say for sure which type this interrupt really
1307 * has. The type flags are unreliable as the
1308 * underlying chip implementation can override them.
1309 */
Andrew Morton97fd75b2012-05-31 16:26:07 -07001310 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001311 irq);
1312 ret = -EINVAL;
1313 goto out_mask;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001314 }
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (!shared) {
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001317 ret = irq_request_resources(desc);
1318 if (ret) {
1319 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1320 new->name, irq, desc->irq_data.chip->name);
1321 goto out_mask;
1322 }
1323
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001324 init_waitqueue_head(&desc->wait_for_threads);
1325
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001326 /* Setup the type (level, edge polarity) if configured: */
1327 if (new->flags & IRQF_TRIGGER_MASK) {
Jiang Liua1ff5412015-06-23 19:47:29 +02001328 ret = __irq_set_trigger(desc,
1329 new->flags & IRQF_TRIGGER_MASK);
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001330
Heiner Kallweit76628322017-06-11 00:38:36 +02001331 if (ret) {
1332 irq_release_resources(desc);
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001333 goto out_mask;
Heiner Kallweit76628322017-06-11 00:38:36 +02001334 }
Thomas Gleixner091738a2011-02-14 20:16:43 +01001335 }
Ahmed S. Darwishf75d2222007-05-08 00:27:55 -07001336
Thomas Gleixner009b4c32011-02-07 21:48:49 +01001337 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
Thomas Gleixner32f41252011-03-28 14:10:52 +02001338 IRQS_ONESHOT | IRQS_WAITING);
1339 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
Thomas Gleixner94d39e12006-06-29 02:24:50 -07001340
Thomas Gleixnera0056772011-02-08 17:11:03 +01001341 if (new->flags & IRQF_PERCPU) {
1342 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1343 irq_settings_set_per_cpu(desc);
1344 }
Thomas Gleixner6a58fb32011-02-08 15:40:05 +01001345
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001346 if (new->flags & IRQF_ONESHOT)
Thomas Gleixner3d67bae2011-02-07 21:02:10 +01001347 desc->istate |= IRQS_ONESHOT;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001348
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001349 if (irq_settings_can_autoenable(desc))
Thomas Gleixnerb4bc7242012-02-08 11:57:52 +01001350 irq_startup(desc, true);
Thomas Gleixner46999232011-02-02 21:41:14 +00001351 else
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001352 /* Undo nested disables: */
1353 desc->depth = 1;
Max Krasnyansky18404752008-05-29 11:02:52 -07001354
Thomas Gleixner612e3682008-11-07 13:58:46 +01001355 /* Exclude IRQ from balancing if requested */
Thomas Gleixnera0056772011-02-08 17:11:03 +01001356 if (new->flags & IRQF_NOBALANCING) {
1357 irq_settings_set_no_balancing(desc);
1358 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1359 }
Thomas Gleixner612e3682008-11-07 13:58:46 +01001360
Max Krasnyansky18404752008-05-29 11:02:52 -07001361 /* Set default affinity mask once everything is setup */
Jiang Liua8a98ea2015-06-04 12:13:30 +08001362 setup_affinity(desc, mask);
David Brownell0c5d1eb2008-10-01 14:46:18 -07001363
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001364 } else if (new->flags & IRQF_TRIGGER_MASK) {
1365 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
Thomas Gleixner7ee7e872016-11-07 19:57:00 +01001366 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001367
1368 if (nmsk != omsk)
1369 /* hope the handler works with current trigger mode */
Joe Perchesa395d6a2016-03-22 14:28:09 -07001370 pr_warn("irq %d uses trigger mode %u; requested %u\n",
Thomas Gleixner7ee7e872016-11-07 19:57:00 +01001371 irq, omsk, nmsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001373
Ingo Molnarf17c7542009-02-17 20:43:37 +01001374 *old_ptr = new;
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001375
Thomas Gleixnercab303b2014-08-28 11:44:31 +02001376 irq_pm_install_action(desc, new);
1377
Linus Torvalds8528b0f2007-01-23 14:16:31 -08001378 /* Reset broken irq detection when installing new handler */
1379 desc->irq_count = 0;
1380 desc->irqs_unhandled = 0;
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001381
1382 /*
1383 * Check whether we disabled the irq via the spurious handler
1384 * before. Reenable it and give it another chance.
1385 */
Thomas Gleixner7acdd532011-02-07 20:40:54 +01001386 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1387 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
Jiang Liu79ff1cd2015-06-23 19:52:36 +02001388 __enable_irq(desc);
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001389 }
1390
Thomas Gleixner239007b2009-11-17 16:46:45 +01001391 raw_spin_unlock_irqrestore(&desc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001393 /*
1394 * Strictly no need to wake it up, but hung_task complains
1395 * when no hard interrupt wakes the thread up.
1396 */
1397 if (new->thread)
1398 wake_up_process(new->thread);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001399 if (new->secondary)
1400 wake_up_process(new->secondary->thread);
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001401
Yinghai Lu2c6927a2008-08-19 20:50:11 -07001402 register_irq_proc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 new->dir = NULL;
1404 register_handler_proc(irq, new);
Xiaotian Feng4f5058c2011-04-02 19:39:35 +08001405 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 return 0;
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001408
1409mismatch:
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001410 if (!(new->flags & IRQF_PROBE_SHARED)) {
Andrew Morton97fd75b2012-05-31 16:26:07 -07001411 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001412 irq, new->flags, new->name, old->flags, old->name);
1413#ifdef CONFIG_DEBUG_SHIRQ
Andrew Morton13e87ec2006-04-27 18:39:18 -07001414 dump_stack();
Alan Cox3f050442007-02-12 00:52:04 -08001415#endif
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001416 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001417 ret = -EBUSY;
1418
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001419out_mask:
Dan Carpenter1c389792011-03-17 14:43:07 +03001420 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001421 free_cpumask_var(mask);
1422
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001423out_thread:
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001424 if (new->thread) {
1425 struct task_struct *t = new->thread;
1426
1427 new->thread = NULL;
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001428 kthread_stop(t);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001429 put_task_struct(t);
1430 }
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001431 if (new->secondary && new->secondary->thread) {
1432 struct task_struct *t = new->secondary->thread;
1433
1434 new->secondary->thread = NULL;
1435 kthread_stop(t);
1436 put_task_struct(t);
1437 }
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001438out_mput:
1439 module_put(desc->owner);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001440 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443/**
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001444 * setup_irq - setup an interrupt
1445 * @irq: Interrupt line to setup
1446 * @act: irqaction for the interrupt
1447 *
1448 * Used to statically setup interrupts in the early boot process.
1449 */
1450int setup_irq(unsigned int irq, struct irqaction *act)
1451{
David Daney986c0112011-02-09 16:04:25 -08001452 int retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001453 struct irq_desc *desc = irq_to_desc(irq);
1454
Jon Hunter9b5d5852016-05-10 16:14:35 +01001455 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001456 return -EINVAL;
Jon Hunterbe45beb2016-06-07 16:12:29 +01001457
1458 retval = irq_chip_pm_get(&desc->irq_data);
1459 if (retval < 0)
1460 return retval;
1461
David Daney986c0112011-02-09 16:04:25 -08001462 chip_bus_lock(desc);
1463 retval = __setup_irq(irq, desc, act);
1464 chip_bus_sync_unlock(desc);
1465
Jon Hunterbe45beb2016-06-07 16:12:29 +01001466 if (retval)
1467 irq_chip_pm_put(&desc->irq_data);
1468
David Daney986c0112011-02-09 16:04:25 -08001469 return retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001470}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001471EXPORT_SYMBOL_GPL(setup_irq);
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001472
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001473/*
Magnus Dammcbf94f02009-03-12 21:05:51 +09001474 * Internal function to unregister an irqaction - used to free
1475 * regular and special interrupts that are part of the architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 */
Magnus Dammcbf94f02009-03-12 21:05:51 +09001477static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001479 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnarf17c7542009-02-17 20:43:37 +01001480 struct irqaction *action, **action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 unsigned long flags;
1482
Ingo Molnarae88a232009-02-15 11:29:50 +01001483 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001484
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001485 if (!desc)
Magnus Dammf21cfb22009-03-12 21:05:42 +09001486 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001488 chip_bus_lock(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +01001489 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarae88a232009-02-15 11:29:50 +01001490
1491 /*
1492 * There can be multiple actions per IRQ descriptor, find the right
1493 * one based on the dev_id:
1494 */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001495 action_ptr = &desc->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 for (;;) {
Ingo Molnarf17c7542009-02-17 20:43:37 +01001497 action = *action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Ingo Molnarae88a232009-02-15 11:29:50 +01001499 if (!action) {
1500 WARN(1, "Trying to free already-free IRQ %d\n", irq);
Thomas Gleixner239007b2009-11-17 16:46:45 +01001501 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001502 chip_bus_sync_unlock(desc);
Magnus Dammf21cfb22009-03-12 21:05:42 +09001503 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001505
Ingo Molnar8316e382009-02-17 20:28:29 +01001506 if (action->dev_id == dev_id)
1507 break;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001508 action_ptr = &action->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001510
1511 /* Found it - now remove it from the list of entries: */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001512 *action_ptr = action->next;
Ingo Molnarae88a232009-02-15 11:29:50 +01001513
Thomas Gleixnercab303b2014-08-28 11:44:31 +02001514 irq_pm_remove_action(desc, action);
1515
Ingo Molnarae88a232009-02-15 11:29:50 +01001516 /* If this was the last handler, shut down the IRQ line: */
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001517 if (!desc->action) {
Thomas Gleixnere9849772015-10-09 23:28:58 +02001518 irq_settings_clr_disable_unlazy(desc);
Thomas Gleixner46999232011-02-02 21:41:14 +00001519 irq_shutdown(desc);
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001520 irq_release_resources(desc);
1521 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001522
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -07001523#ifdef CONFIG_SMP
1524 /* make sure affinity_hint is cleaned up */
1525 if (WARN_ON_ONCE(desc->affinity_hint))
1526 desc->affinity_hint = NULL;
1527#endif
1528
Thomas Gleixner239007b2009-11-17 16:46:45 +01001529 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001530 chip_bus_sync_unlock(desc);
Ingo Molnarae88a232009-02-15 11:29:50 +01001531
1532 unregister_handler_proc(irq, action);
1533
1534 /* Make sure it's not being used on another CPU: */
1535 synchronize_irq(irq);
1536
1537#ifdef CONFIG_DEBUG_SHIRQ
1538 /*
1539 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1540 * event to happen even now it's being freed, so let's make sure that
1541 * is so by doing an extra call to the handler ....
1542 *
1543 * ( We do this after actually deregistering it, to make sure that a
1544 * 'real' IRQ doesn't run in * parallel with our fake. )
1545 */
1546 if (action->flags & IRQF_SHARED) {
1547 local_irq_save(flags);
1548 action->handler(irq, dev_id);
1549 local_irq_restore(flags);
1550 }
1551#endif
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001552
1553 if (action->thread) {
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001554 kthread_stop(action->thread);
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001555 put_task_struct(action->thread);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001556 if (action->secondary && action->secondary->thread) {
1557 kthread_stop(action->secondary->thread);
1558 put_task_struct(action->secondary->thread);
1559 }
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001560 }
1561
Jon Hunterbe45beb2016-06-07 16:12:29 +01001562 irq_chip_pm_put(&desc->irq_data);
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001563 module_put(desc->owner);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001564 kfree(action->secondary);
Magnus Dammf21cfb22009-03-12 21:05:42 +09001565 return action;
1566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568/**
Magnus Dammcbf94f02009-03-12 21:05:51 +09001569 * remove_irq - free an interrupt
1570 * @irq: Interrupt line to free
1571 * @act: irqaction for the interrupt
1572 *
1573 * Used to remove interrupts statically setup by the early boot process.
1574 */
1575void remove_irq(unsigned int irq, struct irqaction *act)
1576{
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001577 struct irq_desc *desc = irq_to_desc(irq);
1578
1579 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1580 __free_irq(irq, act->dev_id);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001581}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001582EXPORT_SYMBOL_GPL(remove_irq);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001583
1584/**
Magnus Dammf21cfb22009-03-12 21:05:42 +09001585 * free_irq - free an interrupt allocated with request_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 * @irq: Interrupt line to free
1587 * @dev_id: Device identity to free
1588 *
1589 * Remove an interrupt handler. The handler is removed and if the
1590 * interrupt line is no longer in use by any driver it is disabled.
1591 * On a shared IRQ the caller must ensure the interrupt is disabled
1592 * on the card it drives before calling this function. The function
1593 * does not return until any executing interrupts for this IRQ
1594 * have completed.
1595 *
1596 * This function must not be called from interrupt context.
1597 */
1598void free_irq(unsigned int irq, void *dev_id)
1599{
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001600 struct irq_desc *desc = irq_to_desc(irq);
1601
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001602 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001603 return;
1604
Ben Hutchingscd7eab42011-01-19 21:01:44 +00001605#ifdef CONFIG_SMP
1606 if (WARN_ON(desc->affinity_notify))
1607 desc->affinity_notify = NULL;
1608#endif
1609
Magnus Dammcbf94f02009-03-12 21:05:51 +09001610 kfree(__free_irq(irq, dev_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612EXPORT_SYMBOL(free_irq);
1613
1614/**
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001615 * request_threaded_irq - allocate an interrupt line
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * @irq: Interrupt line to allocate
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001617 * @handler: Function to be called when the IRQ occurs.
1618 * Primary handler for threaded interrupts
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001619 * If NULL and thread_fn != NULL the default
1620 * primary handler is installed
Thomas Gleixnerf48fe812009-03-24 11:46:22 +01001621 * @thread_fn: Function called from the irq handler thread
1622 * If NULL, no irq thread is created
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * @irqflags: Interrupt type flags
1624 * @devname: An ascii name for the claiming device
1625 * @dev_id: A cookie passed back to the handler function
1626 *
1627 * This call allocates interrupt resources and enables the
1628 * interrupt line and IRQ handling. From the point this
1629 * call is made your handler function may be invoked. Since
1630 * your handler function must clear any interrupt the board
1631 * raises, you must take care both to initialise your hardware
1632 * and to set up the interrupt handler in the right order.
1633 *
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001634 * If you want to set up a threaded irq handler for your device
Javi Merino6d21af42011-10-26 10:16:11 +01001635 * then you need to supply @handler and @thread_fn. @handler is
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001636 * still called in hard interrupt context and has to check
1637 * whether the interrupt originates from the device. If yes it
1638 * needs to disable the interrupt on the device and return
Steven Rostedt39a2edd2009-05-12 14:35:54 -04001639 * IRQ_WAKE_THREAD which will wake up the handler thread and run
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001640 * @thread_fn. This split handler design is necessary to support
1641 * shared interrupts.
1642 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * Dev_id must be globally unique. Normally the address of the
1644 * device data structure is used as the cookie. Since the handler
1645 * receives this value it makes sense to use it.
1646 *
1647 * If your interrupt is shared you must pass a non NULL dev_id
1648 * as this is required when freeing the interrupt.
1649 *
1650 * Flags:
1651 *
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001652 * IRQF_SHARED Interrupt is shared
David Brownell0c5d1eb2008-10-01 14:46:18 -07001653 * IRQF_TRIGGER_* Specify active edge(s) or level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 *
1655 */
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001656int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1657 irq_handler_t thread_fn, unsigned long irqflags,
1658 const char *devname, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Ingo Molnar06fcb0c2006-06-29 02:24:40 -07001660 struct irqaction *action;
Yinghai Lu08678b02008-08-19 20:50:05 -07001661 struct irq_desc *desc;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001662 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Chen Fane237a552016-02-15 12:52:01 +08001664 if (irq == IRQ_NOTCONNECTED)
1665 return -ENOTCONN;
1666
David Brownell470c6622008-12-01 14:31:37 -08001667 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * Sanity-check: shared interrupts must pass in a real dev-ID,
1669 * otherwise we'll have trouble later trying to figure out
1670 * which interrupt is which (messes up the interrupt freeing
1671 * logic etc).
Rafael J. Wysocki17f48032015-02-27 00:07:55 +01001672 *
1673 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1674 * it cannot be set along with IRQF_NO_SUSPEND.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 */
Rafael J. Wysocki17f48032015-02-27 00:07:55 +01001676 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1677 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1678 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001680
Yinghai Lucb5bc832008-08-19 20:50:17 -07001681 desc = irq_to_desc(irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001682 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001684
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001685 if (!irq_settings_can_request(desc) ||
1686 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Thomas Gleixner6550c772006-06-29 02:24:49 -07001687 return -EINVAL;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001688
1689 if (!handler) {
1690 if (!thread_fn)
1691 return -EINVAL;
1692 handler = irq_default_primary_handler;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Thomas Gleixner45535732009-02-22 23:00:32 +01001695 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (!action)
1697 return -ENOMEM;
1698
1699 action->handler = handler;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001700 action->thread_fn = thread_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 action->flags = irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 action->name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 action->dev_id = dev_id;
1704
Jon Hunterbe45beb2016-06-07 16:12:29 +01001705 retval = irq_chip_pm_get(&desc->irq_data);
Shawn Lin4396f462016-08-22 16:21:52 +08001706 if (retval < 0) {
1707 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01001708 return retval;
Shawn Lin4396f462016-08-22 16:21:52 +08001709 }
Jon Hunterbe45beb2016-06-07 16:12:29 +01001710
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001711 chip_bus_lock(desc);
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001712 retval = __setup_irq(irq, desc, action);
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001713 chip_bus_sync_unlock(desc);
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001714
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001715 if (retval) {
Jon Hunterbe45beb2016-06-07 16:12:29 +01001716 irq_chip_pm_put(&desc->irq_data);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001717 kfree(action->secondary);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001718 kfree(action);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001719 }
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001720
Thomas Gleixner6d83f942011-02-18 23:27:23 +01001721#ifdef CONFIG_DEBUG_SHIRQ_FIXME
Luis Henriques6ce51c42009-04-01 18:06:35 +01001722 if (!retval && (irqflags & IRQF_SHARED)) {
David Woodhousea304e1b2007-02-12 00:52:00 -08001723 /*
1724 * It's a shared IRQ -- the driver ought to be prepared for it
1725 * to happen immediately, so let's make sure....
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001726 * We disable the irq to make sure that a 'real' IRQ doesn't
1727 * run in parallel with our fake.
David Woodhousea304e1b2007-02-12 00:52:00 -08001728 */
Jarek Poplawski59845b12007-08-30 23:56:34 -07001729 unsigned long flags;
David Woodhousea304e1b2007-02-12 00:52:00 -08001730
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001731 disable_irq(irq);
Jarek Poplawski59845b12007-08-30 23:56:34 -07001732 local_irq_save(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001733
Jarek Poplawski59845b12007-08-30 23:56:34 -07001734 handler(irq, dev_id);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001735
Jarek Poplawski59845b12007-08-30 23:56:34 -07001736 local_irq_restore(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001737 enable_irq(irq);
David Woodhousea304e1b2007-02-12 00:52:00 -08001738 }
1739#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return retval;
1741}
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001742EXPORT_SYMBOL(request_threaded_irq);
Marc Zyngierae731f82010-03-15 22:56:33 +00001743
1744/**
1745 * request_any_context_irq - allocate an interrupt line
1746 * @irq: Interrupt line to allocate
1747 * @handler: Function to be called when the IRQ occurs.
1748 * Threaded handler for threaded interrupts.
1749 * @flags: Interrupt type flags
1750 * @name: An ascii name for the claiming device
1751 * @dev_id: A cookie passed back to the handler function
1752 *
1753 * This call allocates interrupt resources and enables the
1754 * interrupt line and IRQ handling. It selects either a
1755 * hardirq or threaded handling method depending on the
1756 * context.
1757 *
1758 * On failure, it returns a negative value. On success,
1759 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1760 */
1761int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1762 unsigned long flags, const char *name, void *dev_id)
1763{
Chen Fane237a552016-02-15 12:52:01 +08001764 struct irq_desc *desc;
Marc Zyngierae731f82010-03-15 22:56:33 +00001765 int ret;
1766
Chen Fane237a552016-02-15 12:52:01 +08001767 if (irq == IRQ_NOTCONNECTED)
1768 return -ENOTCONN;
1769
1770 desc = irq_to_desc(irq);
Marc Zyngierae731f82010-03-15 22:56:33 +00001771 if (!desc)
1772 return -EINVAL;
1773
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001774 if (irq_settings_is_nested_thread(desc)) {
Marc Zyngierae731f82010-03-15 22:56:33 +00001775 ret = request_threaded_irq(irq, NULL, handler,
1776 flags, name, dev_id);
1777 return !ret ? IRQC_IS_NESTED : ret;
1778 }
1779
1780 ret = request_irq(irq, handler, flags, name, dev_id);
1781 return !ret ? IRQC_IS_HARDIRQ : ret;
1782}
1783EXPORT_SYMBOL_GPL(request_any_context_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001784
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001785void enable_percpu_irq(unsigned int irq, unsigned int type)
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001786{
1787 unsigned int cpu = smp_processor_id();
1788 unsigned long flags;
1789 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1790
1791 if (!desc)
1792 return;
1793
Marc Zyngierf35ad082016-06-13 10:39:44 +01001794 /*
1795 * If the trigger type is not specified by the caller, then
1796 * use the default for this interrupt.
1797 */
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001798 type &= IRQ_TYPE_SENSE_MASK;
Marc Zyngierf35ad082016-06-13 10:39:44 +01001799 if (type == IRQ_TYPE_NONE)
1800 type = irqd_get_trigger_type(&desc->irq_data);
1801
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001802 if (type != IRQ_TYPE_NONE) {
1803 int ret;
1804
Jiang Liua1ff5412015-06-23 19:47:29 +02001805 ret = __irq_set_trigger(desc, type);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001806
1807 if (ret) {
Thomas Gleixner32cffdd2011-10-04 18:43:57 +02001808 WARN(1, "failed to set type for IRQ%d\n", irq);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001809 goto out;
1810 }
1811 }
1812
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001813 irq_percpu_enable(desc, cpu);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001814out:
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001815 irq_put_desc_unlock(desc, flags);
1816}
Chris Metcalf36a5df82013-02-01 15:04:26 -05001817EXPORT_SYMBOL_GPL(enable_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001818
Thomas Petazzonif0cb3222015-10-20 15:23:51 +02001819/**
1820 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
1821 * @irq: Linux irq number to check for
1822 *
1823 * Must be called from a non migratable context. Returns the enable
1824 * state of a per cpu interrupt on the current cpu.
1825 */
1826bool irq_percpu_is_enabled(unsigned int irq)
1827{
1828 unsigned int cpu = smp_processor_id();
1829 struct irq_desc *desc;
1830 unsigned long flags;
1831 bool is_enabled;
1832
1833 desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1834 if (!desc)
1835 return false;
1836
1837 is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
1838 irq_put_desc_unlock(desc, flags);
1839
1840 return is_enabled;
1841}
1842EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
1843
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001844void disable_percpu_irq(unsigned int irq)
1845{
1846 unsigned int cpu = smp_processor_id();
1847 unsigned long flags;
1848 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1849
1850 if (!desc)
1851 return;
1852
1853 irq_percpu_disable(desc, cpu);
1854 irq_put_desc_unlock(desc, flags);
1855}
Chris Metcalf36a5df82013-02-01 15:04:26 -05001856EXPORT_SYMBOL_GPL(disable_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001857
1858/*
1859 * Internal function to unregister a percpu irqaction.
1860 */
1861static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1862{
1863 struct irq_desc *desc = irq_to_desc(irq);
1864 struct irqaction *action;
1865 unsigned long flags;
1866
1867 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1868
1869 if (!desc)
1870 return NULL;
1871
1872 raw_spin_lock_irqsave(&desc->lock, flags);
1873
1874 action = desc->action;
1875 if (!action || action->percpu_dev_id != dev_id) {
1876 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1877 goto bad;
1878 }
1879
1880 if (!cpumask_empty(desc->percpu_enabled)) {
1881 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1882 irq, cpumask_first(desc->percpu_enabled));
1883 goto bad;
1884 }
1885
1886 /* Found it - now remove it from the list of entries: */
1887 desc->action = NULL;
1888
1889 raw_spin_unlock_irqrestore(&desc->lock, flags);
1890
1891 unregister_handler_proc(irq, action);
1892
Jon Hunterbe45beb2016-06-07 16:12:29 +01001893 irq_chip_pm_put(&desc->irq_data);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001894 module_put(desc->owner);
1895 return action;
1896
1897bad:
1898 raw_spin_unlock_irqrestore(&desc->lock, flags);
1899 return NULL;
1900}
1901
1902/**
1903 * remove_percpu_irq - free a per-cpu interrupt
1904 * @irq: Interrupt line to free
1905 * @act: irqaction for the interrupt
1906 *
1907 * Used to remove interrupts statically setup by the early boot process.
1908 */
1909void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1910{
1911 struct irq_desc *desc = irq_to_desc(irq);
1912
1913 if (desc && irq_settings_is_per_cpu_devid(desc))
1914 __free_percpu_irq(irq, act->percpu_dev_id);
1915}
1916
1917/**
1918 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
1919 * @irq: Interrupt line to free
1920 * @dev_id: Device identity to free
1921 *
1922 * Remove a percpu interrupt handler. The handler is removed, but
1923 * the interrupt line is not disabled. This must be done on each
1924 * CPU before calling this function. The function does not return
1925 * until any executing interrupts for this IRQ have completed.
1926 *
1927 * This function must not be called from interrupt context.
1928 */
1929void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1930{
1931 struct irq_desc *desc = irq_to_desc(irq);
1932
1933 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1934 return;
1935
1936 chip_bus_lock(desc);
1937 kfree(__free_percpu_irq(irq, dev_id));
1938 chip_bus_sync_unlock(desc);
1939}
Maxime Ripardaec2e2a2015-09-25 18:09:33 +02001940EXPORT_SYMBOL_GPL(free_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001941
1942/**
1943 * setup_percpu_irq - setup a per-cpu interrupt
1944 * @irq: Interrupt line to setup
1945 * @act: irqaction for the interrupt
1946 *
1947 * Used to statically setup per-cpu interrupts in the early boot process.
1948 */
1949int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1950{
1951 struct irq_desc *desc = irq_to_desc(irq);
1952 int retval;
1953
1954 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1955 return -EINVAL;
Jon Hunterbe45beb2016-06-07 16:12:29 +01001956
1957 retval = irq_chip_pm_get(&desc->irq_data);
1958 if (retval < 0)
1959 return retval;
1960
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001961 chip_bus_lock(desc);
1962 retval = __setup_irq(irq, desc, act);
1963 chip_bus_sync_unlock(desc);
1964
Jon Hunterbe45beb2016-06-07 16:12:29 +01001965 if (retval)
1966 irq_chip_pm_put(&desc->irq_data);
1967
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001968 return retval;
1969}
1970
1971/**
1972 * request_percpu_irq - allocate a percpu interrupt line
1973 * @irq: Interrupt line to allocate
1974 * @handler: Function to be called when the IRQ occurs.
1975 * @devname: An ascii name for the claiming device
1976 * @dev_id: A percpu cookie passed back to the handler function
1977 *
Maxime Riparda1b7feb2015-09-25 18:09:32 +02001978 * This call allocates interrupt resources and enables the
1979 * interrupt on the local CPU. If the interrupt is supposed to be
1980 * enabled on other CPUs, it has to be done on each CPU using
1981 * enable_percpu_irq().
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001982 *
1983 * Dev_id must be globally unique. It is a per-cpu variable, and
1984 * the handler gets called with the interrupted CPU's instance of
1985 * that variable.
1986 */
1987int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1988 const char *devname, void __percpu *dev_id)
1989{
1990 struct irqaction *action;
1991 struct irq_desc *desc;
1992 int retval;
1993
1994 if (!dev_id)
1995 return -EINVAL;
1996
1997 desc = irq_to_desc(irq);
1998 if (!desc || !irq_settings_can_request(desc) ||
1999 !irq_settings_is_per_cpu_devid(desc))
2000 return -EINVAL;
2001
2002 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2003 if (!action)
2004 return -ENOMEM;
2005
2006 action->handler = handler;
Marc Zyngier2ed0e642011-11-16 12:27:39 +00002007 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002008 action->name = devname;
2009 action->percpu_dev_id = dev_id;
2010
Jon Hunterbe45beb2016-06-07 16:12:29 +01002011 retval = irq_chip_pm_get(&desc->irq_data);
Shawn Lin4396f462016-08-22 16:21:52 +08002012 if (retval < 0) {
2013 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01002014 return retval;
Shawn Lin4396f462016-08-22 16:21:52 +08002015 }
Jon Hunterbe45beb2016-06-07 16:12:29 +01002016
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002017 chip_bus_lock(desc);
2018 retval = __setup_irq(irq, desc, action);
2019 chip_bus_sync_unlock(desc);
2020
Jon Hunterbe45beb2016-06-07 16:12:29 +01002021 if (retval) {
2022 irq_chip_pm_put(&desc->irq_data);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002023 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01002024 }
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002025
2026 return retval;
2027}
Maxime Ripardaec2e2a2015-09-25 18:09:33 +02002028EXPORT_SYMBOL_GPL(request_percpu_irq);
Marc Zyngier1b7047e2015-03-18 11:01:22 +00002029
2030/**
2031 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2032 * @irq: Interrupt line that is forwarded to a VM
2033 * @which: One of IRQCHIP_STATE_* the caller wants to know about
2034 * @state: a pointer to a boolean where the state is to be storeed
2035 *
2036 * This call snapshots the internal irqchip state of an
2037 * interrupt, returning into @state the bit corresponding to
2038 * stage @which
2039 *
2040 * This function should be called with preemption disabled if the
2041 * interrupt controller has per-cpu registers.
2042 */
2043int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2044 bool *state)
2045{
2046 struct irq_desc *desc;
2047 struct irq_data *data;
2048 struct irq_chip *chip;
2049 unsigned long flags;
2050 int err = -EINVAL;
2051
2052 desc = irq_get_desc_buslock(irq, &flags, 0);
2053 if (!desc)
2054 return err;
2055
2056 data = irq_desc_get_irq_data(desc);
2057
2058 do {
2059 chip = irq_data_get_irq_chip(data);
2060 if (chip->irq_get_irqchip_state)
2061 break;
2062#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2063 data = data->parent_data;
2064#else
2065 data = NULL;
2066#endif
2067 } while (data);
2068
2069 if (data)
2070 err = chip->irq_get_irqchip_state(data, which, state);
2071
2072 irq_put_desc_busunlock(desc, flags);
2073 return err;
2074}
Bjorn Andersson1ee4fb32015-07-22 12:43:04 -07002075EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
Marc Zyngier1b7047e2015-03-18 11:01:22 +00002076
2077/**
2078 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2079 * @irq: Interrupt line that is forwarded to a VM
2080 * @which: State to be restored (one of IRQCHIP_STATE_*)
2081 * @val: Value corresponding to @which
2082 *
2083 * This call sets the internal irqchip state of an interrupt,
2084 * depending on the value of @which.
2085 *
2086 * This function should be called with preemption disabled if the
2087 * interrupt controller has per-cpu registers.
2088 */
2089int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2090 bool val)
2091{
2092 struct irq_desc *desc;
2093 struct irq_data *data;
2094 struct irq_chip *chip;
2095 unsigned long flags;
2096 int err = -EINVAL;
2097
2098 desc = irq_get_desc_buslock(irq, &flags, 0);
2099 if (!desc)
2100 return err;
2101
2102 data = irq_desc_get_irq_data(desc);
2103
2104 do {
2105 chip = irq_data_get_irq_chip(data);
2106 if (chip->irq_set_irqchip_state)
2107 break;
2108#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2109 data = data->parent_data;
2110#else
2111 data = NULL;
2112#endif
2113 } while (data);
2114
2115 if (data)
2116 err = chip->irq_set_irqchip_state(data, which, val);
2117
2118 irq_put_desc_busunlock(desc, flags);
2119 return err;
2120}
Bjorn Andersson1ee4fb32015-07-22 12:43:04 -07002121EXPORT_SYMBOL_GPL(irq_set_irqchip_state);