blob: be7f489788e27d5790199d4e79bd07400cc59a81 [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);
236 schedule_work(&desc->affinity_notify->work);
237 }
David Daneyc2d0c552011-03-25 12:38:50 -0700238 irqd_set(data, IRQD_AFFINITY_SET);
239
240 return ret;
241}
242
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000243int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800244{
Yinghai Lu08678b02008-08-19 20:50:05 -0700245 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100246 unsigned long flags;
David Daneyc2d0c552011-03-25 12:38:50 -0700247 int ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800248
David Daneyc2d0c552011-03-25 12:38:50 -0700249 if (!desc)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800250 return -EINVAL;
251
Thomas Gleixner239007b2009-11-17 16:46:45 +0100252 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000253 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100254 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100255 return ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800256}
257
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700258int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
259{
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700260 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100261 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700262
263 if (!desc)
264 return -EINVAL;
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700265 desc->affinity_hint = m;
Thomas Gleixner02725e72011-02-12 10:37:36 +0100266 irq_put_desc_unlock(desc, flags);
Jesse Brandeburge2e64a92014-12-18 17:22:06 -0800267 /* set the initial affinity to prevent every interrupt being on CPU0 */
Jesse Brandeburg4fe7ffb2015-01-28 10:57:39 -0800268 if (m)
269 __irq_set_affinity(irq, m, false);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700270 return 0;
271}
272EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
273
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000274static void irq_affinity_notify(struct work_struct *work)
275{
276 struct irq_affinity_notify *notify =
277 container_of(work, struct irq_affinity_notify, work);
278 struct irq_desc *desc = irq_to_desc(notify->irq);
279 cpumask_var_t cpumask;
280 unsigned long flags;
281
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100282 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000283 goto out;
284
285 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200286 if (irq_move_pending(&desc->irq_data))
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100287 irq_get_pending(cpumask, desc);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000288 else
Jiang Liu9df872f2015-06-03 11:47:50 +0800289 cpumask_copy(cpumask, desc->irq_common_data.affinity);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000290 raw_spin_unlock_irqrestore(&desc->lock, flags);
291
292 notify->notify(notify, cpumask);
293
294 free_cpumask_var(cpumask);
295out:
296 kref_put(&notify->kref, notify->release);
297}
298
299/**
300 * irq_set_affinity_notifier - control notification of IRQ affinity changes
301 * @irq: Interrupt for which to enable/disable notification
302 * @notify: Context for notification, or %NULL to disable
303 * notification. Function pointers must be initialised;
304 * the other fields will be initialised by this function.
305 *
306 * Must be called in process context. Notification may only be enabled
307 * after the IRQ is allocated and must be disabled before the IRQ is
308 * freed using free_irq().
309 */
310int
311irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
312{
313 struct irq_desc *desc = irq_to_desc(irq);
314 struct irq_affinity_notify *old_notify;
315 unsigned long flags;
316
317 /* The release function is promised process context */
318 might_sleep();
319
320 if (!desc)
321 return -EINVAL;
322
323 /* Complete initialisation of *notify */
324 if (notify) {
325 notify->irq = irq;
326 kref_init(&notify->kref);
327 INIT_WORK(&notify->work, irq_affinity_notify);
328 }
329
330 raw_spin_lock_irqsave(&desc->lock, flags);
331 old_notify = desc->affinity_notify;
332 desc->affinity_notify = notify;
333 raw_spin_unlock_irqrestore(&desc->lock, flags);
334
Prasad Sodagudi3df0c562019-03-24 07:57:04 -0700335 if (old_notify) {
336 cancel_work_sync(&old_notify->work);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000337 kref_put(&old_notify->kref, old_notify->release);
Prasad Sodagudi3df0c562019-03-24 07:57:04 -0700338 }
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000339
340 return 0;
341}
342EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
343
Max Krasnyansky18404752008-05-29 11:02:52 -0700344#ifndef CONFIG_AUTO_IRQ_AFFINITY
345/*
346 * Generic version of the affinity autoselector.
347 */
Jiang Liua8a98ea2015-06-04 12:13:30 +0800348static int setup_affinity(struct irq_desc *desc, struct cpumask *mask)
Max Krasnyansky18404752008-05-29 11:02:52 -0700349{
Thomas Gleixner569bda82011-02-07 17:05:08 +0100350 struct cpumask *set = irq_default_affinity;
Jiang Liu67830112015-06-01 16:05:13 +0800351 int node = irq_desc_get_node(desc);
Thomas Gleixner569bda82011-02-07 17:05:08 +0100352
Thomas Gleixnerb0082072011-02-07 17:30:50 +0100353 /* Excludes PER_CPU and NO_BALANCE interrupts */
Jiang Liue019c242015-06-23 20:29:34 +0200354 if (!__irq_can_set_affinity(desc))
Max Krasnyansky18404752008-05-29 11:02:52 -0700355 return 0;
356
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100357 /*
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900358 * Preserve the managed affinity setting and an userspace affinity
359 * setup, but make sure that one of the targets is online.
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100360 */
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900361 if (irqd_affinity_is_managed(&desc->irq_data) ||
362 irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
Jiang Liu9df872f2015-06-03 11:47:50 +0800363 if (cpumask_intersects(desc->irq_common_data.affinity,
Thomas Gleixner569bda82011-02-07 17:05:08 +0100364 cpu_online_mask))
Jiang Liu9df872f2015-06-03 11:47:50 +0800365 set = desc->irq_common_data.affinity;
Thomas Gleixner0c6f8a82011-03-28 13:32:20 +0200366 else
Thomas Gleixner2bdd1052011-02-08 17:22:00 +0100367 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100368 }
Max Krasnyansky18404752008-05-29 11:02:52 -0700369
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100370 cpumask_and(mask, cpu_online_mask, set);
Prarit Bhargava241fc642012-03-26 15:02:18 -0400371 if (node != NUMA_NO_NODE) {
372 const struct cpumask *nodemask = cpumask_of_node(node);
373
374 /* make sure at least one of the cpus in nodemask is online */
375 if (cpumask_intersects(mask, nodemask))
376 cpumask_and(mask, mask, nodemask);
377 }
Jiang Liu818b0f32012-03-30 23:11:34 +0800378 irq_do_set_affinity(&desc->irq_data, mask, false);
Max Krasnyansky18404752008-05-29 11:02:52 -0700379 return 0;
380}
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100381#else
Jiang Liua8a98ea2015-06-04 12:13:30 +0800382/* Wrapper for ALPHA specific affinity selector magic */
383static inline int setup_affinity(struct irq_desc *d, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100384{
Jiang Liua8a98ea2015-06-04 12:13:30 +0800385 return irq_select_affinity(irq_desc_get_irq(d));
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100386}
Max Krasnyansky18404752008-05-29 11:02:52 -0700387#endif
388
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100389/*
390 * Called when affinity is set via /proc/irq
391 */
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100392int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100393{
394 struct irq_desc *desc = irq_to_desc(irq);
395 unsigned long flags;
396 int ret;
397
Thomas Gleixner239007b2009-11-17 16:46:45 +0100398 raw_spin_lock_irqsave(&desc->lock, flags);
Jiang Liua8a98ea2015-06-04 12:13:30 +0800399 ret = setup_affinity(desc, mask);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100400 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100401 return ret;
402}
403
404#else
Thomas Gleixner3b8249e2011-02-07 16:02:20 +0100405static inline int
Jiang Liua8a98ea2015-06-04 12:13:30 +0800406setup_affinity(struct irq_desc *desc, struct cpumask *mask)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100407{
408 return 0;
409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#endif
411
Feng Wufcf1ae22015-10-03 16:20:38 +0800412/**
413 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
414 * @irq: interrupt number to set affinity
415 * @vcpu_info: vCPU specific data
416 *
417 * This function uses the vCPU specific data to set the vCPU
418 * affinity for an irq. The vCPU specific data is passed from
419 * outside, such as KVM. One example code path is as below:
420 * KVM -> IOMMU -> irq_set_vcpu_affinity().
421 */
422int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
423{
424 unsigned long flags;
425 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
426 struct irq_data *data;
427 struct irq_chip *chip;
428 int ret = -ENOSYS;
429
430 if (!desc)
431 return -EINVAL;
432
433 data = irq_desc_get_irq_data(desc);
434 chip = irq_data_get_irq_chip(data);
435 if (chip && chip->irq_set_vcpu_affinity)
436 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
437 irq_put_desc_unlock(desc, flags);
438
439 return ret;
440}
441EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
442
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200443void __disable_irq(struct irq_desc *desc)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100444{
Thomas Gleixner3aae9942011-02-04 10:17:52 +0100445 if (!desc->depth++)
Thomas Gleixner87923472011-02-03 12:27:44 +0100446 irq_disable(desc);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100447}
448
Thomas Gleixner02725e72011-02-12 10:37:36 +0100449static int __disable_irq_nosync(unsigned int irq)
450{
451 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100452 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100453
454 if (!desc)
455 return -EINVAL;
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200456 __disable_irq(desc);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100457 irq_put_desc_busunlock(desc, flags);
458 return 0;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/**
462 * disable_irq_nosync - disable an irq without waiting
463 * @irq: Interrupt to disable
464 *
465 * Disable the selected interrupt line. Disables and Enables are
466 * nested.
467 * Unlike disable_irq(), this function does not ensure existing
468 * instances of the IRQ handler have completed before returning.
469 *
470 * This function may be called from IRQ context.
471 */
472void disable_irq_nosync(unsigned int irq)
473{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100474 __disable_irq_nosync(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476EXPORT_SYMBOL(disable_irq_nosync);
477
478/**
479 * disable_irq - disable an irq and wait for completion
480 * @irq: Interrupt to disable
481 *
482 * Disable the selected interrupt line. Enables and Disables are
483 * nested.
484 * This function waits for any pending IRQ handlers for this interrupt
485 * to complete before returning. If you use this function while
486 * holding a resource the IRQ handler may need you will deadlock.
487 *
488 * This function may be called - with care - from IRQ context.
489 */
490void disable_irq(unsigned int irq)
491{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100492 if (!__disable_irq_nosync(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 synchronize_irq(irq);
494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495EXPORT_SYMBOL(disable_irq);
496
Peter Zijlstra02cea392015-02-05 14:06:23 +0100497/**
498 * disable_hardirq - disables an irq and waits for hardirq completion
499 * @irq: Interrupt to disable
500 *
501 * Disable the selected interrupt line. Enables and Disables are
502 * nested.
503 * This function waits for any pending hard IRQ handlers for this
504 * interrupt to complete before returning. If you use this function while
505 * holding a resource the hard IRQ handler may need you will deadlock.
506 *
507 * When used to optimistically disable an interrupt from atomic context
508 * the return value must be checked.
509 *
510 * Returns: false if a threaded handler is active.
511 *
512 * This function may be called - with care - from IRQ context.
513 */
514bool disable_hardirq(unsigned int irq)
515{
516 if (!__disable_irq_nosync(irq))
517 return synchronize_hardirq(irq);
518
519 return false;
520}
521EXPORT_SYMBOL_GPL(disable_hardirq);
522
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200523void __enable_irq(struct irq_desc *desc)
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200524{
525 switch (desc->depth) {
526 case 0:
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100527 err_out:
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200528 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
529 irq_desc_get_irq(desc));
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200530 break;
531 case 1: {
Thomas Gleixnerc531e832011-02-08 12:44:58 +0100532 if (desc->istate & IRQS_SUSPENDED)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100533 goto err_out;
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200534 /* Prevent probing on this irq: */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +0100535 irq_settings_set_noprobe(desc);
Thomas Gleixner3aae9942011-02-04 10:17:52 +0100536 irq_enable(desc);
Jiang Liu0798abe2015-06-04 12:13:27 +0800537 check_irq_resend(desc);
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200538 /* fall-through */
539 }
540 default:
541 desc->depth--;
542 }
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/**
546 * enable_irq - enable handling of an irq
547 * @irq: Interrupt to enable
548 *
549 * Undoes the effect of one call to disable_irq(). If this
550 * matches the last disable, processing of interrupts on this
551 * IRQ line is re-enabled.
552 *
Thomas Gleixner70aedd22009-08-13 12:17:48 +0200553 * This function may be called from IRQ context only when
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200554 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
556void enable_irq(unsigned int irq)
557{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100559 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700561 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -0700562 return;
Thomas Gleixner50f7c032011-02-03 13:23:54 +0100563 if (WARN(!desc->irq_data.chip,
564 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
Thomas Gleixner02725e72011-02-12 10:37:36 +0100565 goto out;
Thomas Gleixner2656c362010-10-22 14:47:57 +0200566
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200567 __enable_irq(desc);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100568out:
569 irq_put_desc_busunlock(desc, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571EXPORT_SYMBOL(enable_irq);
572
David Brownell0c5d1eb2008-10-01 14:46:18 -0700573static int set_irq_wake_real(unsigned int irq, unsigned int on)
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200574{
Yinghai Lu08678b02008-08-19 20:50:05 -0700575 struct irq_desc *desc = irq_to_desc(irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200576 int ret = -ENXIO;
577
Santosh Shilimkar60f96b42011-09-09 13:59:35 +0530578 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
579 return 0;
580
Thomas Gleixner2f7e99b2010-09-27 12:45:50 +0000581 if (desc->irq_data.chip->irq_set_wake)
582 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200583
584 return ret;
585}
586
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700587/**
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100588 * irq_set_irq_wake - control irq power management wakeup
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700589 * @irq: interrupt to control
590 * @on: enable/disable power management wakeup
591 *
David Brownell15a647e2006-07-30 03:03:08 -0700592 * Enable/disable power management wakeup mode, which is
593 * disabled by default. Enables and disables must match,
594 * just as they match for non-wakeup mode support.
595 *
596 * Wakeup mode lets this IRQ wake the system from sleep
597 * states like "suspend to RAM".
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700598 */
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100599int irq_set_irq_wake(unsigned int irq, unsigned int on)
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700600{
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700601 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100602 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200603 int ret = 0;
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700604
Jesper Juhl13863a62011-06-09 23:14:58 +0200605 if (!desc)
606 return -EINVAL;
607
David Brownell15a647e2006-07-30 03:03:08 -0700608 /* wakeup-capable irqs can be shared between drivers that
609 * don't need to have the same sleep mode behaviors.
610 */
David Brownell15a647e2006-07-30 03:03:08 -0700611 if (on) {
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200612 if (desc->wake_depth++ == 0) {
613 ret = set_irq_wake_real(irq, on);
614 if (ret)
615 desc->wake_depth = 0;
616 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100617 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200618 }
David Brownell15a647e2006-07-30 03:03:08 -0700619 } else {
620 if (desc->wake_depth == 0) {
Arjan van de Ven7a2c4772008-07-25 01:45:54 -0700621 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200622 } else if (--desc->wake_depth == 0) {
623 ret = set_irq_wake_real(irq, on);
624 if (ret)
625 desc->wake_depth = 1;
626 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100627 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200628 }
David Brownell15a647e2006-07-30 03:03:08 -0700629 }
Thomas Gleixner02725e72011-02-12 10:37:36 +0100630 irq_put_desc_busunlock(desc, flags);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700631 return ret;
632}
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100633EXPORT_SYMBOL(irq_set_irq_wake);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/*
636 * Internal function that tells the architecture code whether a
637 * particular irq has been exclusively allocated or is available
638 * for driver use.
639 */
640int can_request_irq(unsigned int irq, unsigned long irqflags)
641{
Thomas Gleixnercc8c3b72010-03-23 22:40:53 +0100642 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100643 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100644 int canrequest = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700646 if (!desc)
647 return 0;
648
Thomas Gleixner02725e72011-02-12 10:37:36 +0100649 if (irq_settings_can_request(desc)) {
Ben Hutchings2779db82013-06-28 02:40:30 +0100650 if (!desc->action ||
651 irqflags & desc->action->flags & IRQF_SHARED)
652 canrequest = 1;
Thomas Gleixner02725e72011-02-12 10:37:36 +0100653 }
654 irq_put_desc_unlock(desc, flags);
655 return canrequest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Jiang Liua1ff5412015-06-23 19:47:29 +0200658int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700659{
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200660 struct irq_chip *chip = desc->irq_data.chip;
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100661 int ret, unmask = 0;
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700662
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000663 if (!chip || !chip->irq_set_type) {
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700664 /*
665 * IRQF_TRIGGER_* but the PIC does not support multiple
666 * flow-types?
667 */
Jiang Liua1ff5412015-06-23 19:47:29 +0200668 pr_debug("No set_type function for IRQ %d (%s)\n",
669 irq_desc_get_irq(desc),
Thomas Gleixnerf5d89472012-04-19 12:06:13 +0200670 chip ? (chip->name ? : "unknown") : "unknown");
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700671 return 0;
672 }
673
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100674 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
Thomas Gleixner32f41252011-03-28 14:10:52 +0200675 if (!irqd_irq_masked(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100676 mask_irq(desc);
Thomas Gleixner32f41252011-03-28 14:10:52 +0200677 if (!irqd_irq_disabled(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100678 unmask = 1;
679 }
680
Alexander Kuleshov00b992d2016-07-19 15:54:08 +0600681 /* Mask all flags except trigger mode */
682 flags &= IRQ_TYPE_SENSE_MASK;
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000683 ret = chip->irq_set_type(&desc->irq_data, flags);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700684
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100685 switch (ret) {
686 case IRQ_SET_MASK_OK:
Jiang Liu2cb62542014-11-06 22:20:18 +0800687 case IRQ_SET_MASK_OK_DONE:
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100688 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
689 irqd_set(&desc->irq_data, flags);
690
691 case IRQ_SET_MASK_OK_NOCOPY:
692 flags = irqd_get_trigger_type(&desc->irq_data);
693 irq_settings_set_trigger_mask(desc, flags);
694 irqd_clear(&desc->irq_data, IRQD_LEVEL);
695 irq_settings_clr_level(desc);
696 if (flags & IRQ_TYPE_LEVEL_MASK) {
697 irq_settings_set_level(desc);
698 irqd_set(&desc->irq_data, IRQD_LEVEL);
699 }
Thomas Gleixner46732472010-06-07 17:53:51 +0200700
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100701 ret = 0;
Thomas Gleixner8fff39e2011-02-21 14:19:42 +0100702 break;
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100703 default:
Andrew Morton97fd75b2012-05-31 16:26:07 -0700704 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
Jiang Liua1ff5412015-06-23 19:47:29 +0200705 flags, irq_desc_get_irq(desc), chip->irq_set_type);
David Brownell0c5d1eb2008-10-01 14:46:18 -0700706 }
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100707 if (unmask)
708 unmask_irq(desc);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700709 return ret;
710}
711
Thomas Gleixner293a7a02012-10-16 15:07:49 -0700712#ifdef CONFIG_HARDIRQS_SW_RESEND
713int irq_set_parent(int irq, int parent_irq)
714{
715 unsigned long flags;
716 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
717
718 if (!desc)
719 return -EINVAL;
720
721 desc->parent_irq = parent_irq;
722
723 irq_put_desc_unlock(desc, flags);
724 return 0;
725}
Sudip Mukherjee3118dac2016-10-06 23:06:43 +0530726EXPORT_SYMBOL_GPL(irq_set_parent);
Thomas Gleixner293a7a02012-10-16 15:07:49 -0700727#endif
728
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200729/*
730 * Default primary interrupt handler for threaded interrupts. Is
731 * assigned as primary handler when request_threaded_irq is called
732 * with handler == NULL. Useful for oneshot interrupts.
733 */
734static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
735{
736 return IRQ_WAKE_THREAD;
737}
738
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200739/*
740 * Primary handler for nested threaded interrupts. Should never be
741 * called.
742 */
743static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
744{
745 WARN(1, "Primary handler called for nested irq %d\n", irq);
746 return IRQ_NONE;
747}
748
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200749static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
750{
751 WARN(1, "Secondary action handler called for irq %d\n", irq);
752 return IRQ_NONE;
753}
754
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100755static int irq_wait_for_interrupt(struct irqaction *action)
756{
Ido Yariv550acb12011-12-01 13:55:08 +0200757 set_current_state(TASK_INTERRUPTIBLE);
758
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100759 while (!kthread_should_stop()) {
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100760
761 if (test_and_clear_bit(IRQTF_RUNTHREAD,
762 &action->thread_flags)) {
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100763 __set_current_state(TASK_RUNNING);
764 return 0;
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100765 }
766 schedule();
Ido Yariv550acb12011-12-01 13:55:08 +0200767 set_current_state(TASK_INTERRUPTIBLE);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100768 }
Ido Yariv550acb12011-12-01 13:55:08 +0200769 __set_current_state(TASK_RUNNING);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100770 return -1;
771}
772
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200773/*
774 * Oneshot interrupts keep the irq line masked until the threaded
775 * handler finished. unmask if the interrupt has not been disabled and
776 * is marked MASKED.
777 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000778static void irq_finalize_oneshot(struct irq_desc *desc,
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100779 struct irqaction *action)
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200780{
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200781 if (!(desc->istate & IRQS_ONESHOT) ||
782 action->handler == irq_forced_secondary_handler)
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000783 return;
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100784again:
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000785 chip_bus_lock(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100786 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100787
788 /*
789 * Implausible though it may be we need to protect us against
790 * the following scenario:
791 *
792 * The thread is faster done than the hard interrupt handler
793 * on the other CPU. If we unmask the irq line then the
794 * interrupt can come in again and masks the line, leaves due
Thomas Gleixner009b4c32011-02-07 21:48:49 +0100795 * to IRQS_INPROGRESS and the irq line is masked forever.
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000796 *
797 * This also serializes the state of shared oneshot handlers
798 * versus "desc->threads_onehsot |= action->thread_mask;" in
799 * irq_wake_thread(). See the comment there which explains the
800 * serialization.
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100801 */
Thomas Gleixner32f41252011-03-28 14:10:52 +0200802 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100803 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000804 chip_bus_sync_unlock(desc);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100805 cpu_relax();
806 goto again;
807 }
808
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000809 /*
810 * Now check again, whether the thread should run. Otherwise
811 * we would clear the threads_oneshot bit of this thread which
812 * was just set.
813 */
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100814 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000815 goto out_unlock;
816
817 desc->threads_oneshot &= ~action->thread_mask;
818
Thomas Gleixner32f41252011-03-28 14:10:52 +0200819 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
820 irqd_irq_masked(&desc->irq_data))
Thomas Gleixner328a4972014-03-13 19:03:51 +0100821 unmask_threaded_irq(desc);
Thomas Gleixner32f41252011-03-28 14:10:52 +0200822
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000823out_unlock:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100824 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000825 chip_bus_sync_unlock(desc);
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200826}
827
Bruno Premont61f38262009-07-22 22:22:32 +0200828#ifdef CONFIG_SMP
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100829/*
Chuansheng Liub04c6442014-02-10 16:13:57 +0800830 * Check whether we need to change the affinity of the interrupt thread.
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200831 */
832static void
833irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
834{
835 cpumask_var_t mask;
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100836 bool valid = true;
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200837
838 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
839 return;
840
841 /*
842 * In case we are out of memory we set IRQTF_AFFINITY again and
843 * try again next time
844 */
845 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
846 set_bit(IRQTF_AFFINITY, &action->thread_flags);
847 return;
848 }
849
Thomas Gleixner239007b2009-11-17 16:46:45 +0100850 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100851 /*
852 * This code is triggered unconditionally. Check the affinity
853 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
854 */
Matthias Kaehlcke02e3a7d2017-04-12 11:20:30 -0700855 if (cpumask_available(desc->irq_common_data.affinity))
Jiang Liu9df872f2015-06-03 11:47:50 +0800856 cpumask_copy(mask, desc->irq_common_data.affinity);
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100857 else
858 valid = false;
Thomas Gleixner239007b2009-11-17 16:46:45 +0100859 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200860
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100861 if (valid)
862 set_cpus_allowed_ptr(current, mask);
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200863 free_cpumask_var(mask);
864}
Bruno Premont61f38262009-07-22 22:22:32 +0200865#else
866static inline void
867irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
868#endif
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200869
870/*
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000871 * Interrupts which are not explicitely requested as threaded
872 * interrupts rely on the implicit bh/preempt disable of the hard irq
873 * context. So we need to disable bh here to avoid deadlocks and other
874 * side effects.
875 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200876static irqreturn_t
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000877irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
878{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200879 irqreturn_t ret;
880
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000881 local_bh_disable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200882 ret = action->thread_fn(action->irq, action->dev_id);
Lukas Wunner404a83a2018-10-18 15:15:05 +0200883 if (ret == IRQ_HANDLED)
884 atomic_inc(&desc->threads_handled);
885
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100886 irq_finalize_oneshot(desc, action);
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000887 local_bh_enable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200888 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000889}
890
891/*
Xie XiuQif788e7b2013-10-18 09:12:04 +0800892 * Interrupts explicitly requested as threaded interrupts want to be
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000893 * preemtible - many of them need to sleep and wait for slow busses to
894 * complete.
895 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200896static irqreturn_t irq_thread_fn(struct irq_desc *desc,
897 struct irqaction *action)
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000898{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200899 irqreturn_t ret;
900
901 ret = action->thread_fn(action->irq, action->dev_id);
Lukas Wunner404a83a2018-10-18 15:15:05 +0200902 if (ret == IRQ_HANDLED)
903 atomic_inc(&desc->threads_handled);
904
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100905 irq_finalize_oneshot(desc, action);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200906 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000907}
908
Ido Yariv7140ea12011-12-02 18:24:12 +0200909static void wake_threads_waitq(struct irq_desc *desc)
910{
Chuansheng Liuc6856892014-02-24 11:29:50 +0800911 if (atomic_dec_and_test(&desc->threads_active))
Ido Yariv7140ea12011-12-02 18:24:12 +0200912 wake_up(&desc->wait_for_threads);
913}
914
Al Viro67d12142012-06-27 11:07:19 +0400915static void irq_thread_dtor(struct callback_head *unused)
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000916{
917 struct task_struct *tsk = current;
918 struct irq_desc *desc;
919 struct irqaction *action;
920
921 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
922 return;
923
924 action = kthread_data(tsk);
925
Linus Torvaldsfb21aff2012-05-31 18:47:30 -0700926 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
Alan Cox19af3952012-12-18 14:21:25 -0800927 tsk->comm, tsk->pid, action->irq);
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000928
929
930 desc = irq_to_desc(action->irq);
931 /*
932 * If IRQTF_RUNTHREAD is set, we need to decrement
933 * desc->threads_active and wake possible waiters.
934 */
935 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
936 wake_threads_waitq(desc);
937
938 /* Prevent a stale desc->threads_oneshot */
939 irq_finalize_oneshot(desc, action);
940}
941
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200942static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
943{
944 struct irqaction *secondary = action->secondary;
945
946 if (WARN_ON_ONCE(!secondary))
947 return;
948
949 raw_spin_lock_irq(&desc->lock);
950 __irq_wake_thread(desc, secondary);
951 raw_spin_unlock_irq(&desc->lock);
952}
953
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000954/*
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100955 * Interrupt handler thread
956 */
957static int irq_thread(void *data)
958{
Al Viro67d12142012-06-27 11:07:19 +0400959 struct callback_head on_exit_work;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100960 struct irqaction *action = data;
961 struct irq_desc *desc = irq_to_desc(action->irq);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200962 irqreturn_t (*handler_fn)(struct irq_desc *desc,
963 struct irqaction *action);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100964
Alexander Gordeev540b60e2012-03-09 14:59:13 +0100965 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000966 &action->thread_flags))
967 handler_fn = irq_forced_thread_fn;
968 else
969 handler_fn = irq_thread_fn;
970
Al Viro41f9d292012-06-26 22:10:04 +0400971 init_task_work(&on_exit_work, irq_thread_dtor);
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000972 task_work_add(current, &on_exit_work, false);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100973
Sankara Muthukrishnanf3de44e2012-10-31 15:41:23 -0500974 irq_thread_check_affinity(desc, action);
975
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100976 while (!irq_wait_for_interrupt(action)) {
Ido Yariv7140ea12011-12-02 18:24:12 +0200977 irqreturn_t action_ret;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100978
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200979 irq_thread_check_affinity(desc, action);
980
Ido Yariv7140ea12011-12-02 18:24:12 +0200981 action_ret = handler_fn(desc, action);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200982 if (action_ret == IRQ_WAKE_THREAD)
983 irq_wake_secondary(desc, action);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100984
Ido Yariv7140ea12011-12-02 18:24:12 +0200985 wake_threads_waitq(desc);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100986 }
987
Ido Yariv7140ea12011-12-02 18:24:12 +0200988 /*
989 * This is the regular exit path. __free_irq() is stopping the
990 * thread via kthread_stop() after calling
991 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
Thomas Gleixnere04268b2012-03-15 22:55:21 +0100992 * oneshot mask bit can be set. We cannot verify that as we
993 * cannot touch the oneshot mask at this point anymore as
994 * __setup_irq() might have given out currents thread_mask
995 * again.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100996 */
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +1000997 task_work_cancel(current, irq_thread_dtor);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100998 return 0;
999}
1000
Thomas Gleixnera92444c2014-02-15 00:55:19 +00001001/**
1002 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1003 * @irq: Interrupt line
1004 * @dev_id: Device identity for which the thread should be woken
1005 *
1006 */
1007void irq_wake_thread(unsigned int irq, void *dev_id)
1008{
1009 struct irq_desc *desc = irq_to_desc(irq);
1010 struct irqaction *action;
1011 unsigned long flags;
1012
1013 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1014 return;
1015
1016 raw_spin_lock_irqsave(&desc->lock, flags);
Daniel Lezcanof944b5a2016-01-14 10:54:13 +01001017 for_each_action_of_desc(desc, action) {
Thomas Gleixnera92444c2014-02-15 00:55:19 +00001018 if (action->dev_id == dev_id) {
1019 if (action->thread)
1020 __irq_wake_thread(desc, action);
1021 break;
1022 }
1023 }
1024 raw_spin_unlock_irqrestore(&desc->lock, flags);
1025}
1026EXPORT_SYMBOL_GPL(irq_wake_thread);
1027
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001028static int irq_setup_forced_threading(struct irqaction *new)
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001029{
1030 if (!force_irqthreads)
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001031 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001032 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001033 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001034
Thomas Gleixnereecd08a2018-08-03 14:44:59 +02001035 /*
1036 * No further action required for interrupts which are requested as
1037 * threaded interrupts already
1038 */
1039 if (new->handler == irq_default_primary_handler)
1040 return 0;
1041
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001042 new->flags |= IRQF_ONESHOT;
1043
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001044 /*
1045 * Handle the case where we have a real primary handler and a
1046 * thread handler. We force thread them as well by creating a
1047 * secondary action.
1048 */
Thomas Gleixnereecd08a2018-08-03 14:44:59 +02001049 if (new->handler && new->thread_fn) {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001050 /* Allocate the secondary action */
1051 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1052 if (!new->secondary)
1053 return -ENOMEM;
1054 new->secondary->handler = irq_forced_secondary_handler;
1055 new->secondary->thread_fn = new->thread_fn;
1056 new->secondary->dev_id = new->dev_id;
1057 new->secondary->irq = new->irq;
1058 new->secondary->name = new->name;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001059 }
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001060 /* Deal with the primary handler */
1061 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1062 new->thread_fn = new->handler;
1063 new->handler = irq_default_primary_handler;
1064 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001065}
1066
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001067static int irq_request_resources(struct irq_desc *desc)
1068{
1069 struct irq_data *d = &desc->irq_data;
1070 struct irq_chip *c = d->chip;
1071
1072 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1073}
1074
1075static void irq_release_resources(struct irq_desc *desc)
1076{
1077 struct irq_data *d = &desc->irq_data;
1078 struct irq_chip *c = d->chip;
1079
1080 if (c->irq_release_resources)
1081 c->irq_release_resources(d);
1082}
1083
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001084static int
1085setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1086{
1087 struct task_struct *t;
1088 struct sched_param param = {
1089 .sched_priority = MAX_USER_RT_PRIO/2,
1090 };
1091
1092 if (!secondary) {
1093 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1094 new->name);
1095 } else {
1096 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1097 new->name);
1098 param.sched_priority -= 1;
1099 }
1100
1101 if (IS_ERR(t))
1102 return PTR_ERR(t);
1103
1104 sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1105
1106 /*
1107 * We keep the reference to the task struct even if
1108 * the thread dies to avoid that the interrupt code
1109 * references an already freed task_struct.
1110 */
1111 get_task_struct(t);
1112 new->thread = t;
1113 /*
1114 * Tell the thread to set its affinity. This is
1115 * important for shared interrupt handlers as we do
1116 * not invoke setup_affinity() for the secondary
1117 * handlers as everything is already set up. Even for
1118 * interrupts marked with IRQF_NO_BALANCE this is
1119 * correct as we want the thread to move to the cpu(s)
1120 * on which the requesting code placed the interrupt.
1121 */
1122 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1123 return 0;
1124}
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/*
1127 * Internal function to register an irqaction - typically used to
1128 * allocate special interrupts that are part of the architecture.
1129 */
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001130static int
Ingo Molnar327ec562009-02-15 11:21:37 +01001131__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Ingo Molnarf17c7542009-02-17 20:43:37 +01001133 struct irqaction *old, **old_ptr;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001134 unsigned long flags, thread_mask = 0;
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001135 int ret, nested, shared = 0;
1136 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001138 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -07001139 return -EINVAL;
1140
Thomas Gleixner6b8ff312010-10-01 12:58:38 +02001141 if (desc->irq_data.chip == &no_irq_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return -ENOSYS;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001143 if (!try_module_get(desc->owner))
1144 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001146 new->irq = irq;
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
Jon Hunter4b357da2016-06-07 16:12:27 +01001149 * If the trigger type is not specified by the caller,
1150 * then use the default for this interrupt.
1151 */
1152 if (!(new->flags & IRQF_TRIGGER_MASK))
1153 new->flags |= irqd_get_trigger_type(&desc->irq_data);
1154
1155 /*
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001156 * Check whether the interrupt nests into another interrupt
1157 * thread.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001158 */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001159 nested = irq_settings_is_nested_thread(desc);
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001160 if (nested) {
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001161 if (!new->thread_fn) {
1162 ret = -EINVAL;
1163 goto out_mput;
1164 }
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001165 /*
1166 * Replace the primary handler which was provided from
1167 * the driver for non nested interrupt handling by the
1168 * dummy function which warns when called.
1169 */
1170 new->handler = irq_nested_primary_handler;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001171 } else {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001172 if (irq_settings_can_thread(desc)) {
1173 ret = irq_setup_forced_threading(new);
1174 if (ret)
1175 goto out_mput;
1176 }
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001177 }
1178
1179 /*
1180 * Create a handler thread when a thread function is supplied
1181 * and the interrupt does not nest into another interrupt
1182 * thread.
1183 */
1184 if (new->thread_fn && !nested) {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001185 ret = setup_irq_thread(new, irq, false);
1186 if (ret)
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001187 goto out_mput;
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001188 if (new->secondary) {
1189 ret = setup_irq_thread(new->secondary, irq, true);
1190 if (ret)
1191 goto out_thread;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001192 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001193 }
1194
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001195 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1196 ret = -ENOMEM;
1197 goto out_thread;
1198 }
1199
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001200 /*
Thomas Gleixnerdc9b2292012-07-13 19:29:45 +02001201 * Drivers are often written to work w/o knowledge about the
1202 * underlying irq chip implementation, so a request for a
1203 * threaded irq without a primary hard irq context handler
1204 * requires the ONESHOT flag to be set. Some irq chips like
1205 * MSI based interrupts are per se one shot safe. Check the
1206 * chip flags, so we can avoid the unmask dance at the end of
1207 * the threaded handler for those.
1208 */
1209 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1210 new->flags &= ~IRQF_ONESHOT;
1211
1212 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 * The following block of code has to be executed atomically
1214 */
Thomas Gleixner239007b2009-11-17 16:46:45 +01001215 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarf17c7542009-02-17 20:43:37 +01001216 old_ptr = &desc->action;
1217 old = *old_ptr;
Ingo Molnar06fcb0c2006-06-29 02:24:40 -07001218 if (old) {
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001219 /*
1220 * Can't share interrupts unless both agree to and are
1221 * the same type (level, edge, polarity). So both flag
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001222 * fields must have IRQF_SHARED set and the bits which
Thomas Gleixner9d591ed2011-02-23 23:52:16 +00001223 * set the trigger type must match. Also all must
1224 * agree on ONESHOT.
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001225 */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001226 if (!((old->flags & new->flags) & IRQF_SHARED) ||
Greg Kroah-Hartman6c9ca572018-03-30 10:53:44 +02001227 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001228 ((old->flags ^ new->flags) & IRQF_ONESHOT))
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001229 goto mismatch;
1230
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001231 /* All handlers must agree on per-cpuness */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001232 if ((old->flags & IRQF_PERCPU) !=
1233 (new->flags & IRQF_PERCPU))
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001234 goto mismatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* add new interrupt at end of irq queue */
1237 do {
Thomas Gleixner52abb702012-03-06 23:18:54 +01001238 /*
1239 * Or all existing action->thread_mask bits,
1240 * so we can find the next zero bit for this
1241 * new action.
1242 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001243 thread_mask |= old->thread_mask;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001244 old_ptr = &old->next;
1245 old = *old_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 } while (old);
1247 shared = 1;
1248 }
1249
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001250 /*
Thomas Gleixner52abb702012-03-06 23:18:54 +01001251 * Setup the thread mask for this irqaction for ONESHOT. For
1252 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1253 * conditional in irq_wake_thread().
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001254 */
Thomas Gleixner52abb702012-03-06 23:18:54 +01001255 if (new->flags & IRQF_ONESHOT) {
1256 /*
1257 * Unlikely to have 32 resp 64 irqs sharing one line,
1258 * but who knows.
1259 */
1260 if (thread_mask == ~0UL) {
1261 ret = -EBUSY;
1262 goto out_mask;
1263 }
1264 /*
1265 * The thread_mask for the action is or'ed to
1266 * desc->thread_active to indicate that the
1267 * IRQF_ONESHOT thread handler has been woken, but not
1268 * yet finished. The bit is cleared when a thread
1269 * completes. When all threads of a shared interrupt
1270 * line have completed desc->threads_active becomes
1271 * zero and the interrupt line is unmasked. See
1272 * handle.c:irq_wake_thread() for further information.
1273 *
1274 * If no thread is woken by primary (hard irq context)
1275 * interrupt handlers, then desc->threads_active is
1276 * also checked for zero to unmask the irq line in the
1277 * affected hard irq flow handlers
1278 * (handle_[fasteoi|level]_irq).
1279 *
1280 * The new action gets the first zero bit of
1281 * thread_mask assigned. See the loop above which or's
1282 * all existing action->thread_mask bits.
1283 */
1284 new->thread_mask = 1 << ffz(thread_mask);
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001285
Thomas Gleixnerdc9b2292012-07-13 19:29:45 +02001286 } else if (new->handler == irq_default_primary_handler &&
1287 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001288 /*
1289 * The interrupt was requested with handler = NULL, so
1290 * we use the default primary handler for it. But it
1291 * does not have the oneshot flag set. In combination
1292 * with level interrupts this is deadly, because the
1293 * default primary handler just wakes the thread, then
1294 * the irq lines is reenabled, but the device still
1295 * has the level irq asserted. Rinse and repeat....
1296 *
1297 * While this works for edge type interrupts, we play
1298 * it safe and reject unconditionally because we can't
1299 * say for sure which type this interrupt really
1300 * has. The type flags are unreliable as the
1301 * underlying chip implementation can override them.
1302 */
Andrew Morton97fd75b2012-05-31 16:26:07 -07001303 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001304 irq);
1305 ret = -EINVAL;
1306 goto out_mask;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001307 }
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (!shared) {
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001310 ret = irq_request_resources(desc);
1311 if (ret) {
1312 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1313 new->name, irq, desc->irq_data.chip->name);
1314 goto out_mask;
1315 }
1316
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001317 init_waitqueue_head(&desc->wait_for_threads);
1318
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001319 /* Setup the type (level, edge polarity) if configured: */
1320 if (new->flags & IRQF_TRIGGER_MASK) {
Jiang Liua1ff5412015-06-23 19:47:29 +02001321 ret = __irq_set_trigger(desc,
1322 new->flags & IRQF_TRIGGER_MASK);
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001323
Heiner Kallweit76628322017-06-11 00:38:36 +02001324 if (ret) {
1325 irq_release_resources(desc);
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001326 goto out_mask;
Heiner Kallweit76628322017-06-11 00:38:36 +02001327 }
Thomas Gleixner091738a2011-02-14 20:16:43 +01001328 }
Ahmed S. Darwishf75d2222007-05-08 00:27:55 -07001329
Thomas Gleixner009b4c32011-02-07 21:48:49 +01001330 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
Thomas Gleixner32f41252011-03-28 14:10:52 +02001331 IRQS_ONESHOT | IRQS_WAITING);
1332 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
Thomas Gleixner94d39e12006-06-29 02:24:50 -07001333
Thomas Gleixnera0056772011-02-08 17:11:03 +01001334 if (new->flags & IRQF_PERCPU) {
1335 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1336 irq_settings_set_per_cpu(desc);
1337 }
Thomas Gleixner6a58fb32011-02-08 15:40:05 +01001338
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001339 if (new->flags & IRQF_ONESHOT)
Thomas Gleixner3d67bae2011-02-07 21:02:10 +01001340 desc->istate |= IRQS_ONESHOT;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001341
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001342 if (irq_settings_can_autoenable(desc))
Thomas Gleixnerb4bc7242012-02-08 11:57:52 +01001343 irq_startup(desc, true);
Thomas Gleixner46999232011-02-02 21:41:14 +00001344 else
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001345 /* Undo nested disables: */
1346 desc->depth = 1;
Max Krasnyansky18404752008-05-29 11:02:52 -07001347
Thomas Gleixner612e3682008-11-07 13:58:46 +01001348 /* Exclude IRQ from balancing if requested */
Thomas Gleixnera0056772011-02-08 17:11:03 +01001349 if (new->flags & IRQF_NOBALANCING) {
1350 irq_settings_set_no_balancing(desc);
1351 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1352 }
Thomas Gleixner612e3682008-11-07 13:58:46 +01001353
Max Krasnyansky18404752008-05-29 11:02:52 -07001354 /* Set default affinity mask once everything is setup */
Jiang Liua8a98ea2015-06-04 12:13:30 +08001355 setup_affinity(desc, mask);
David Brownell0c5d1eb2008-10-01 14:46:18 -07001356
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001357 } else if (new->flags & IRQF_TRIGGER_MASK) {
1358 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
Thomas Gleixner7ee7e872016-11-07 19:57:00 +01001359 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001360
1361 if (nmsk != omsk)
1362 /* hope the handler works with current trigger mode */
Joe Perchesa395d6a2016-03-22 14:28:09 -07001363 pr_warn("irq %d uses trigger mode %u; requested %u\n",
Thomas Gleixner7ee7e872016-11-07 19:57:00 +01001364 irq, omsk, nmsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001366
Ingo Molnarf17c7542009-02-17 20:43:37 +01001367 *old_ptr = new;
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001368
Thomas Gleixnercab303b2014-08-28 11:44:31 +02001369 irq_pm_install_action(desc, new);
1370
Linus Torvalds8528b0f2007-01-23 14:16:31 -08001371 /* Reset broken irq detection when installing new handler */
1372 desc->irq_count = 0;
1373 desc->irqs_unhandled = 0;
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001374
1375 /*
1376 * Check whether we disabled the irq via the spurious handler
1377 * before. Reenable it and give it another chance.
1378 */
Thomas Gleixner7acdd532011-02-07 20:40:54 +01001379 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1380 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
Jiang Liu79ff1cd2015-06-23 19:52:36 +02001381 __enable_irq(desc);
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001382 }
1383
Thomas Gleixner239007b2009-11-17 16:46:45 +01001384 raw_spin_unlock_irqrestore(&desc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001386 /*
1387 * Strictly no need to wake it up, but hung_task complains
1388 * when no hard interrupt wakes the thread up.
1389 */
1390 if (new->thread)
1391 wake_up_process(new->thread);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001392 if (new->secondary)
1393 wake_up_process(new->secondary->thread);
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001394
Yinghai Lu2c6927a2008-08-19 20:50:11 -07001395 register_irq_proc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 new->dir = NULL;
1397 register_handler_proc(irq, new);
Xiaotian Feng4f5058c2011-04-02 19:39:35 +08001398 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 return 0;
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001401
1402mismatch:
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001403 if (!(new->flags & IRQF_PROBE_SHARED)) {
Andrew Morton97fd75b2012-05-31 16:26:07 -07001404 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001405 irq, new->flags, new->name, old->flags, old->name);
1406#ifdef CONFIG_DEBUG_SHIRQ
Andrew Morton13e87ec2006-04-27 18:39:18 -07001407 dump_stack();
Alan Cox3f050442007-02-12 00:52:04 -08001408#endif
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001409 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001410 ret = -EBUSY;
1411
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001412out_mask:
Dan Carpenter1c389792011-03-17 14:43:07 +03001413 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001414 free_cpumask_var(mask);
1415
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001416out_thread:
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001417 if (new->thread) {
1418 struct task_struct *t = new->thread;
1419
1420 new->thread = NULL;
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001421 kthread_stop(t);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001422 put_task_struct(t);
1423 }
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001424 if (new->secondary && new->secondary->thread) {
1425 struct task_struct *t = new->secondary->thread;
1426
1427 new->secondary->thread = NULL;
1428 kthread_stop(t);
1429 put_task_struct(t);
1430 }
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001431out_mput:
1432 module_put(desc->owner);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
1436/**
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001437 * setup_irq - setup an interrupt
1438 * @irq: Interrupt line to setup
1439 * @act: irqaction for the interrupt
1440 *
1441 * Used to statically setup interrupts in the early boot process.
1442 */
1443int setup_irq(unsigned int irq, struct irqaction *act)
1444{
David Daney986c0112011-02-09 16:04:25 -08001445 int retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001446 struct irq_desc *desc = irq_to_desc(irq);
1447
Jon Hunter9b5d5852016-05-10 16:14:35 +01001448 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001449 return -EINVAL;
Jon Hunterbe45beb2016-06-07 16:12:29 +01001450
1451 retval = irq_chip_pm_get(&desc->irq_data);
1452 if (retval < 0)
1453 return retval;
1454
David Daney986c0112011-02-09 16:04:25 -08001455 chip_bus_lock(desc);
1456 retval = __setup_irq(irq, desc, act);
1457 chip_bus_sync_unlock(desc);
1458
Jon Hunterbe45beb2016-06-07 16:12:29 +01001459 if (retval)
1460 irq_chip_pm_put(&desc->irq_data);
1461
David Daney986c0112011-02-09 16:04:25 -08001462 return retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001463}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001464EXPORT_SYMBOL_GPL(setup_irq);
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001465
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001466/*
Magnus Dammcbf94f02009-03-12 21:05:51 +09001467 * Internal function to unregister an irqaction - used to free
1468 * regular and special interrupts that are part of the architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 */
Magnus Dammcbf94f02009-03-12 21:05:51 +09001470static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001472 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnarf17c7542009-02-17 20:43:37 +01001473 struct irqaction *action, **action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned long flags;
1475
Ingo Molnarae88a232009-02-15 11:29:50 +01001476 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001477
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001478 if (!desc)
Magnus Dammf21cfb22009-03-12 21:05:42 +09001479 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001481 chip_bus_lock(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +01001482 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarae88a232009-02-15 11:29:50 +01001483
1484 /*
1485 * There can be multiple actions per IRQ descriptor, find the right
1486 * one based on the dev_id:
1487 */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001488 action_ptr = &desc->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 for (;;) {
Ingo Molnarf17c7542009-02-17 20:43:37 +01001490 action = *action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Ingo Molnarae88a232009-02-15 11:29:50 +01001492 if (!action) {
1493 WARN(1, "Trying to free already-free IRQ %d\n", irq);
Thomas Gleixner239007b2009-11-17 16:46:45 +01001494 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001495 chip_bus_sync_unlock(desc);
Magnus Dammf21cfb22009-03-12 21:05:42 +09001496 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001498
Ingo Molnar8316e382009-02-17 20:28:29 +01001499 if (action->dev_id == dev_id)
1500 break;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001501 action_ptr = &action->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001503
1504 /* Found it - now remove it from the list of entries: */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001505 *action_ptr = action->next;
Ingo Molnarae88a232009-02-15 11:29:50 +01001506
Thomas Gleixnercab303b2014-08-28 11:44:31 +02001507 irq_pm_remove_action(desc, action);
1508
Ingo Molnarae88a232009-02-15 11:29:50 +01001509 /* If this was the last handler, shut down the IRQ line: */
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001510 if (!desc->action) {
Thomas Gleixnere9849772015-10-09 23:28:58 +02001511 irq_settings_clr_disable_unlazy(desc);
Thomas Gleixner46999232011-02-02 21:41:14 +00001512 irq_shutdown(desc);
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001513 irq_release_resources(desc);
1514 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001515
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -07001516#ifdef CONFIG_SMP
1517 /* make sure affinity_hint is cleaned up */
1518 if (WARN_ON_ONCE(desc->affinity_hint))
1519 desc->affinity_hint = NULL;
1520#endif
1521
Thomas Gleixner239007b2009-11-17 16:46:45 +01001522 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001523 chip_bus_sync_unlock(desc);
Ingo Molnarae88a232009-02-15 11:29:50 +01001524
1525 unregister_handler_proc(irq, action);
1526
1527 /* Make sure it's not being used on another CPU: */
1528 synchronize_irq(irq);
1529
1530#ifdef CONFIG_DEBUG_SHIRQ
1531 /*
1532 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1533 * event to happen even now it's being freed, so let's make sure that
1534 * is so by doing an extra call to the handler ....
1535 *
1536 * ( We do this after actually deregistering it, to make sure that a
1537 * 'real' IRQ doesn't run in * parallel with our fake. )
1538 */
1539 if (action->flags & IRQF_SHARED) {
1540 local_irq_save(flags);
1541 action->handler(irq, dev_id);
1542 local_irq_restore(flags);
1543 }
1544#endif
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001545
1546 if (action->thread) {
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001547 kthread_stop(action->thread);
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001548 put_task_struct(action->thread);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001549 if (action->secondary && action->secondary->thread) {
1550 kthread_stop(action->secondary->thread);
1551 put_task_struct(action->secondary->thread);
1552 }
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001553 }
1554
Jon Hunterbe45beb2016-06-07 16:12:29 +01001555 irq_chip_pm_put(&desc->irq_data);
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001556 module_put(desc->owner);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001557 kfree(action->secondary);
Magnus Dammf21cfb22009-03-12 21:05:42 +09001558 return action;
1559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561/**
Magnus Dammcbf94f02009-03-12 21:05:51 +09001562 * remove_irq - free an interrupt
1563 * @irq: Interrupt line to free
1564 * @act: irqaction for the interrupt
1565 *
1566 * Used to remove interrupts statically setup by the early boot process.
1567 */
1568void remove_irq(unsigned int irq, struct irqaction *act)
1569{
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001570 struct irq_desc *desc = irq_to_desc(irq);
1571
1572 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1573 __free_irq(irq, act->dev_id);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001574}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001575EXPORT_SYMBOL_GPL(remove_irq);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001576
1577/**
Magnus Dammf21cfb22009-03-12 21:05:42 +09001578 * free_irq - free an interrupt allocated with request_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * @irq: Interrupt line to free
1580 * @dev_id: Device identity to free
1581 *
1582 * Remove an interrupt handler. The handler is removed and if the
1583 * interrupt line is no longer in use by any driver it is disabled.
1584 * On a shared IRQ the caller must ensure the interrupt is disabled
1585 * on the card it drives before calling this function. The function
1586 * does not return until any executing interrupts for this IRQ
1587 * have completed.
1588 *
1589 * This function must not be called from interrupt context.
1590 */
1591void free_irq(unsigned int irq, void *dev_id)
1592{
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001593 struct irq_desc *desc = irq_to_desc(irq);
1594
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001595 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001596 return;
1597
Ben Hutchingscd7eab42011-01-19 21:01:44 +00001598#ifdef CONFIG_SMP
1599 if (WARN_ON(desc->affinity_notify))
1600 desc->affinity_notify = NULL;
1601#endif
1602
Magnus Dammcbf94f02009-03-12 21:05:51 +09001603 kfree(__free_irq(irq, dev_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605EXPORT_SYMBOL(free_irq);
1606
1607/**
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001608 * request_threaded_irq - allocate an interrupt line
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 * @irq: Interrupt line to allocate
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001610 * @handler: Function to be called when the IRQ occurs.
1611 * Primary handler for threaded interrupts
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001612 * If NULL and thread_fn != NULL the default
1613 * primary handler is installed
Thomas Gleixnerf48fe812009-03-24 11:46:22 +01001614 * @thread_fn: Function called from the irq handler thread
1615 * If NULL, no irq thread is created
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * @irqflags: Interrupt type flags
1617 * @devname: An ascii name for the claiming device
1618 * @dev_id: A cookie passed back to the handler function
1619 *
1620 * This call allocates interrupt resources and enables the
1621 * interrupt line and IRQ handling. From the point this
1622 * call is made your handler function may be invoked. Since
1623 * your handler function must clear any interrupt the board
1624 * raises, you must take care both to initialise your hardware
1625 * and to set up the interrupt handler in the right order.
1626 *
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001627 * If you want to set up a threaded irq handler for your device
Javi Merino6d21af42011-10-26 10:16:11 +01001628 * then you need to supply @handler and @thread_fn. @handler is
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001629 * still called in hard interrupt context and has to check
1630 * whether the interrupt originates from the device. If yes it
1631 * needs to disable the interrupt on the device and return
Steven Rostedt39a2edd2009-05-12 14:35:54 -04001632 * IRQ_WAKE_THREAD which will wake up the handler thread and run
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001633 * @thread_fn. This split handler design is necessary to support
1634 * shared interrupts.
1635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * Dev_id must be globally unique. Normally the address of the
1637 * device data structure is used as the cookie. Since the handler
1638 * receives this value it makes sense to use it.
1639 *
1640 * If your interrupt is shared you must pass a non NULL dev_id
1641 * as this is required when freeing the interrupt.
1642 *
1643 * Flags:
1644 *
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001645 * IRQF_SHARED Interrupt is shared
David Brownell0c5d1eb2008-10-01 14:46:18 -07001646 * IRQF_TRIGGER_* Specify active edge(s) or level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
1648 */
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001649int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1650 irq_handler_t thread_fn, unsigned long irqflags,
1651 const char *devname, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Ingo Molnar06fcb0c2006-06-29 02:24:40 -07001653 struct irqaction *action;
Yinghai Lu08678b02008-08-19 20:50:05 -07001654 struct irq_desc *desc;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001655 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Chen Fane237a552016-02-15 12:52:01 +08001657 if (irq == IRQ_NOTCONNECTED)
1658 return -ENOTCONN;
1659
David Brownell470c6622008-12-01 14:31:37 -08001660 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 * Sanity-check: shared interrupts must pass in a real dev-ID,
1662 * otherwise we'll have trouble later trying to figure out
1663 * which interrupt is which (messes up the interrupt freeing
1664 * logic etc).
Rafael J. Wysocki17f48032015-02-27 00:07:55 +01001665 *
1666 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1667 * it cannot be set along with IRQF_NO_SUSPEND.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Rafael J. Wysocki17f48032015-02-27 00:07:55 +01001669 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1670 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1671 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001673
Yinghai Lucb5bc832008-08-19 20:50:17 -07001674 desc = irq_to_desc(irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001675 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001677
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001678 if (!irq_settings_can_request(desc) ||
1679 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Thomas Gleixner6550c772006-06-29 02:24:49 -07001680 return -EINVAL;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001681
1682 if (!handler) {
1683 if (!thread_fn)
1684 return -EINVAL;
1685 handler = irq_default_primary_handler;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Thomas Gleixner45535732009-02-22 23:00:32 +01001688 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!action)
1690 return -ENOMEM;
1691
1692 action->handler = handler;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001693 action->thread_fn = thread_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 action->flags = irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 action->name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 action->dev_id = dev_id;
1697
Jon Hunterbe45beb2016-06-07 16:12:29 +01001698 retval = irq_chip_pm_get(&desc->irq_data);
Shawn Lin4396f462016-08-22 16:21:52 +08001699 if (retval < 0) {
1700 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01001701 return retval;
Shawn Lin4396f462016-08-22 16:21:52 +08001702 }
Jon Hunterbe45beb2016-06-07 16:12:29 +01001703
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001704 chip_bus_lock(desc);
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001705 retval = __setup_irq(irq, desc, action);
Thomas Gleixner3876ec92010-09-27 12:44:35 +00001706 chip_bus_sync_unlock(desc);
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001707
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001708 if (retval) {
Jon Hunterbe45beb2016-06-07 16:12:29 +01001709 irq_chip_pm_put(&desc->irq_data);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001710 kfree(action->secondary);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001711 kfree(action);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001712 }
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001713
Thomas Gleixner6d83f942011-02-18 23:27:23 +01001714#ifdef CONFIG_DEBUG_SHIRQ_FIXME
Luis Henriques6ce51c42009-04-01 18:06:35 +01001715 if (!retval && (irqflags & IRQF_SHARED)) {
David Woodhousea304e1b2007-02-12 00:52:00 -08001716 /*
1717 * It's a shared IRQ -- the driver ought to be prepared for it
1718 * to happen immediately, so let's make sure....
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001719 * We disable the irq to make sure that a 'real' IRQ doesn't
1720 * run in parallel with our fake.
David Woodhousea304e1b2007-02-12 00:52:00 -08001721 */
Jarek Poplawski59845b12007-08-30 23:56:34 -07001722 unsigned long flags;
David Woodhousea304e1b2007-02-12 00:52:00 -08001723
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001724 disable_irq(irq);
Jarek Poplawski59845b12007-08-30 23:56:34 -07001725 local_irq_save(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001726
Jarek Poplawski59845b12007-08-30 23:56:34 -07001727 handler(irq, dev_id);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001728
Jarek Poplawski59845b12007-08-30 23:56:34 -07001729 local_irq_restore(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04001730 enable_irq(irq);
David Woodhousea304e1b2007-02-12 00:52:00 -08001731 }
1732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return retval;
1734}
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001735EXPORT_SYMBOL(request_threaded_irq);
Marc Zyngierae731f82010-03-15 22:56:33 +00001736
1737/**
1738 * request_any_context_irq - allocate an interrupt line
1739 * @irq: Interrupt line to allocate
1740 * @handler: Function to be called when the IRQ occurs.
1741 * Threaded handler for threaded interrupts.
1742 * @flags: Interrupt type flags
1743 * @name: An ascii name for the claiming device
1744 * @dev_id: A cookie passed back to the handler function
1745 *
1746 * This call allocates interrupt resources and enables the
1747 * interrupt line and IRQ handling. It selects either a
1748 * hardirq or threaded handling method depending on the
1749 * context.
1750 *
1751 * On failure, it returns a negative value. On success,
1752 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1753 */
1754int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1755 unsigned long flags, const char *name, void *dev_id)
1756{
Chen Fane237a552016-02-15 12:52:01 +08001757 struct irq_desc *desc;
Marc Zyngierae731f82010-03-15 22:56:33 +00001758 int ret;
1759
Chen Fane237a552016-02-15 12:52:01 +08001760 if (irq == IRQ_NOTCONNECTED)
1761 return -ENOTCONN;
1762
1763 desc = irq_to_desc(irq);
Marc Zyngierae731f82010-03-15 22:56:33 +00001764 if (!desc)
1765 return -EINVAL;
1766
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001767 if (irq_settings_is_nested_thread(desc)) {
Marc Zyngierae731f82010-03-15 22:56:33 +00001768 ret = request_threaded_irq(irq, NULL, handler,
1769 flags, name, dev_id);
1770 return !ret ? IRQC_IS_NESTED : ret;
1771 }
1772
1773 ret = request_irq(irq, handler, flags, name, dev_id);
1774 return !ret ? IRQC_IS_HARDIRQ : ret;
1775}
1776EXPORT_SYMBOL_GPL(request_any_context_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001777
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001778void enable_percpu_irq(unsigned int irq, unsigned int type)
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001779{
1780 unsigned int cpu = smp_processor_id();
1781 unsigned long flags;
1782 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1783
1784 if (!desc)
1785 return;
1786
Marc Zyngierf35ad082016-06-13 10:39:44 +01001787 /*
1788 * If the trigger type is not specified by the caller, then
1789 * use the default for this interrupt.
1790 */
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001791 type &= IRQ_TYPE_SENSE_MASK;
Marc Zyngierf35ad082016-06-13 10:39:44 +01001792 if (type == IRQ_TYPE_NONE)
1793 type = irqd_get_trigger_type(&desc->irq_data);
1794
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001795 if (type != IRQ_TYPE_NONE) {
1796 int ret;
1797
Jiang Liua1ff5412015-06-23 19:47:29 +02001798 ret = __irq_set_trigger(desc, type);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001799
1800 if (ret) {
Thomas Gleixner32cffdd2011-10-04 18:43:57 +02001801 WARN(1, "failed to set type for IRQ%d\n", irq);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001802 goto out;
1803 }
1804 }
1805
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001806 irq_percpu_enable(desc, cpu);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01001807out:
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001808 irq_put_desc_unlock(desc, flags);
1809}
Chris Metcalf36a5df82013-02-01 15:04:26 -05001810EXPORT_SYMBOL_GPL(enable_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001811
Thomas Petazzonif0cb3222015-10-20 15:23:51 +02001812/**
1813 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
1814 * @irq: Linux irq number to check for
1815 *
1816 * Must be called from a non migratable context. Returns the enable
1817 * state of a per cpu interrupt on the current cpu.
1818 */
1819bool irq_percpu_is_enabled(unsigned int irq)
1820{
1821 unsigned int cpu = smp_processor_id();
1822 struct irq_desc *desc;
1823 unsigned long flags;
1824 bool is_enabled;
1825
1826 desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1827 if (!desc)
1828 return false;
1829
1830 is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
1831 irq_put_desc_unlock(desc, flags);
1832
1833 return is_enabled;
1834}
1835EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
1836
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001837void disable_percpu_irq(unsigned int irq)
1838{
1839 unsigned int cpu = smp_processor_id();
1840 unsigned long flags;
1841 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1842
1843 if (!desc)
1844 return;
1845
1846 irq_percpu_disable(desc, cpu);
1847 irq_put_desc_unlock(desc, flags);
1848}
Chris Metcalf36a5df82013-02-01 15:04:26 -05001849EXPORT_SYMBOL_GPL(disable_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001850
1851/*
1852 * Internal function to unregister a percpu irqaction.
1853 */
1854static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1855{
1856 struct irq_desc *desc = irq_to_desc(irq);
1857 struct irqaction *action;
1858 unsigned long flags;
1859
1860 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1861
1862 if (!desc)
1863 return NULL;
1864
1865 raw_spin_lock_irqsave(&desc->lock, flags);
1866
1867 action = desc->action;
1868 if (!action || action->percpu_dev_id != dev_id) {
1869 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1870 goto bad;
1871 }
1872
1873 if (!cpumask_empty(desc->percpu_enabled)) {
1874 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1875 irq, cpumask_first(desc->percpu_enabled));
1876 goto bad;
1877 }
1878
1879 /* Found it - now remove it from the list of entries: */
1880 desc->action = NULL;
1881
1882 raw_spin_unlock_irqrestore(&desc->lock, flags);
1883
1884 unregister_handler_proc(irq, action);
1885
Jon Hunterbe45beb2016-06-07 16:12:29 +01001886 irq_chip_pm_put(&desc->irq_data);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001887 module_put(desc->owner);
1888 return action;
1889
1890bad:
1891 raw_spin_unlock_irqrestore(&desc->lock, flags);
1892 return NULL;
1893}
1894
1895/**
1896 * remove_percpu_irq - free a per-cpu interrupt
1897 * @irq: Interrupt line to free
1898 * @act: irqaction for the interrupt
1899 *
1900 * Used to remove interrupts statically setup by the early boot process.
1901 */
1902void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1903{
1904 struct irq_desc *desc = irq_to_desc(irq);
1905
1906 if (desc && irq_settings_is_per_cpu_devid(desc))
1907 __free_percpu_irq(irq, act->percpu_dev_id);
1908}
1909
1910/**
1911 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
1912 * @irq: Interrupt line to free
1913 * @dev_id: Device identity to free
1914 *
1915 * Remove a percpu interrupt handler. The handler is removed, but
1916 * the interrupt line is not disabled. This must be done on each
1917 * CPU before calling this function. The function does not return
1918 * until any executing interrupts for this IRQ have completed.
1919 *
1920 * This function must not be called from interrupt context.
1921 */
1922void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1923{
1924 struct irq_desc *desc = irq_to_desc(irq);
1925
1926 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1927 return;
1928
1929 chip_bus_lock(desc);
1930 kfree(__free_percpu_irq(irq, dev_id));
1931 chip_bus_sync_unlock(desc);
1932}
Maxime Ripardaec2e2a2015-09-25 18:09:33 +02001933EXPORT_SYMBOL_GPL(free_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001934
1935/**
1936 * setup_percpu_irq - setup a per-cpu interrupt
1937 * @irq: Interrupt line to setup
1938 * @act: irqaction for the interrupt
1939 *
1940 * Used to statically setup per-cpu interrupts in the early boot process.
1941 */
1942int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1943{
1944 struct irq_desc *desc = irq_to_desc(irq);
1945 int retval;
1946
1947 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1948 return -EINVAL;
Jon Hunterbe45beb2016-06-07 16:12:29 +01001949
1950 retval = irq_chip_pm_get(&desc->irq_data);
1951 if (retval < 0)
1952 return retval;
1953
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001954 chip_bus_lock(desc);
1955 retval = __setup_irq(irq, desc, act);
1956 chip_bus_sync_unlock(desc);
1957
Jon Hunterbe45beb2016-06-07 16:12:29 +01001958 if (retval)
1959 irq_chip_pm_put(&desc->irq_data);
1960
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001961 return retval;
1962}
1963
1964/**
1965 * request_percpu_irq - allocate a percpu interrupt line
1966 * @irq: Interrupt line to allocate
1967 * @handler: Function to be called when the IRQ occurs.
1968 * @devname: An ascii name for the claiming device
1969 * @dev_id: A percpu cookie passed back to the handler function
1970 *
Maxime Riparda1b7feb2015-09-25 18:09:32 +02001971 * This call allocates interrupt resources and enables the
1972 * interrupt on the local CPU. If the interrupt is supposed to be
1973 * enabled on other CPUs, it has to be done on each CPU using
1974 * enable_percpu_irq().
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001975 *
1976 * Dev_id must be globally unique. It is a per-cpu variable, and
1977 * the handler gets called with the interrupted CPU's instance of
1978 * that variable.
1979 */
1980int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1981 const char *devname, void __percpu *dev_id)
1982{
1983 struct irqaction *action;
1984 struct irq_desc *desc;
1985 int retval;
1986
1987 if (!dev_id)
1988 return -EINVAL;
1989
1990 desc = irq_to_desc(irq);
1991 if (!desc || !irq_settings_can_request(desc) ||
1992 !irq_settings_is_per_cpu_devid(desc))
1993 return -EINVAL;
1994
1995 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1996 if (!action)
1997 return -ENOMEM;
1998
1999 action->handler = handler;
Marc Zyngier2ed0e642011-11-16 12:27:39 +00002000 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002001 action->name = devname;
2002 action->percpu_dev_id = dev_id;
2003
Jon Hunterbe45beb2016-06-07 16:12:29 +01002004 retval = irq_chip_pm_get(&desc->irq_data);
Shawn Lin4396f462016-08-22 16:21:52 +08002005 if (retval < 0) {
2006 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01002007 return retval;
Shawn Lin4396f462016-08-22 16:21:52 +08002008 }
Jon Hunterbe45beb2016-06-07 16:12:29 +01002009
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002010 chip_bus_lock(desc);
2011 retval = __setup_irq(irq, desc, action);
2012 chip_bus_sync_unlock(desc);
2013
Jon Hunterbe45beb2016-06-07 16:12:29 +01002014 if (retval) {
2015 irq_chip_pm_put(&desc->irq_data);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002016 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01002017 }
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002018
2019 return retval;
2020}
Maxime Ripardaec2e2a2015-09-25 18:09:33 +02002021EXPORT_SYMBOL_GPL(request_percpu_irq);
Marc Zyngier1b7047e2015-03-18 11:01:22 +00002022
2023/**
2024 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2025 * @irq: Interrupt line that is forwarded to a VM
2026 * @which: One of IRQCHIP_STATE_* the caller wants to know about
2027 * @state: a pointer to a boolean where the state is to be storeed
2028 *
2029 * This call snapshots the internal irqchip state of an
2030 * interrupt, returning into @state the bit corresponding to
2031 * stage @which
2032 *
2033 * This function should be called with preemption disabled if the
2034 * interrupt controller has per-cpu registers.
2035 */
2036int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2037 bool *state)
2038{
2039 struct irq_desc *desc;
2040 struct irq_data *data;
2041 struct irq_chip *chip;
2042 unsigned long flags;
2043 int err = -EINVAL;
2044
2045 desc = irq_get_desc_buslock(irq, &flags, 0);
2046 if (!desc)
2047 return err;
2048
2049 data = irq_desc_get_irq_data(desc);
2050
2051 do {
2052 chip = irq_data_get_irq_chip(data);
2053 if (chip->irq_get_irqchip_state)
2054 break;
2055#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2056 data = data->parent_data;
2057#else
2058 data = NULL;
2059#endif
2060 } while (data);
2061
2062 if (data)
2063 err = chip->irq_get_irqchip_state(data, which, state);
2064
2065 irq_put_desc_busunlock(desc, flags);
2066 return err;
2067}
Bjorn Andersson1ee4fb32015-07-22 12:43:04 -07002068EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
Marc Zyngier1b7047e2015-03-18 11:01:22 +00002069
2070/**
2071 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2072 * @irq: Interrupt line that is forwarded to a VM
2073 * @which: State to be restored (one of IRQCHIP_STATE_*)
2074 * @val: Value corresponding to @which
2075 *
2076 * This call sets the internal irqchip state of an interrupt,
2077 * depending on the value of @which.
2078 *
2079 * This function should be called with preemption disabled if the
2080 * interrupt controller has per-cpu registers.
2081 */
2082int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2083 bool val)
2084{
2085 struct irq_desc *desc;
2086 struct irq_data *data;
2087 struct irq_chip *chip;
2088 unsigned long flags;
2089 int err = -EINVAL;
2090
2091 desc = irq_get_desc_buslock(irq, &flags, 0);
2092 if (!desc)
2093 return err;
2094
2095 data = irq_desc_get_irq_data(desc);
2096
2097 do {
2098 chip = irq_data_get_irq_chip(data);
2099 if (chip->irq_set_irqchip_state)
2100 break;
2101#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2102 data = data->parent_data;
2103#else
2104 data = NULL;
2105#endif
2106 } while (data);
2107
2108 if (data)
2109 err = chip->irq_set_irqchip_state(data, which, val);
2110
2111 irq_put_desc_busunlock(desc, flags);
2112 return err;
2113}
Bjorn Andersson1ee4fb32015-07-22 12:43:04 -07002114EXPORT_SYMBOL_GPL(irq_set_irqchip_state);