blob: dc8b35f2d5458368afc2270cb16470c82e73059d [file] [log] [blame]
Thomas Gleixner52a65ff2018-03-14 22:15:19 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Ingo Molnara34db9b2006-06-29 02:24:50 -07003 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
4 * Copyright (C) 2005-2006 Thomas Gleixner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file contains driver APIs to the irq subsystem.
7 */
8
Andrew Morton97fd75b2012-05-31 16:26:07 -07009#define pr_fmt(fmt) "genirq: " fmt
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/irq.h>
Thomas Gleixner3aa551c2009-03-23 18:28:15 +010012#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/random.h>
15#include <linux/interrupt.h>
Thomas Gleixner4001d8e2019-06-28 13:11:49 +020016#include <linux/irqdomain.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>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010020#include <linux/sched/task.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010021#include <uapi/linux/sched/types.h>
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +100022#include <linux/task_work.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "internals.h"
25
Thomas Gleixner8d32a302011-02-23 23:52:23 +000026#ifdef CONFIG_IRQ_FORCED_THREADING
27__read_mostly bool force_irqthreads;
Sebastian Andrzej Siewior47b82e82018-05-04 16:24:46 +020028EXPORT_SYMBOL_GPL(force_irqthreads);
Thomas Gleixner8d32a302011-02-23 23:52:23 +000029
30static int __init setup_forced_irqthreads(char *arg)
31{
32 force_irqthreads = true;
33 return 0;
34}
35early_param("threadirqs", setup_forced_irqthreads);
36#endif
37
Thomas Gleixner18258f72014-02-15 00:55:18 +000038static void __synchronize_hardirq(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Thomas Gleixner32f41252011-03-28 14:10:52 +020040 bool inprogress;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Herbert Xua98ce5c2007-10-23 11:26:25 +080042 do {
43 unsigned long flags;
44
45 /*
46 * Wait until we're out of the critical section. This might
47 * give the wrong answer due to the lack of memory barriers.
48 */
Thomas Gleixner32f41252011-03-28 14:10:52 +020049 while (irqd_irq_inprogress(&desc->irq_data))
Herbert Xua98ce5c2007-10-23 11:26:25 +080050 cpu_relax();
51
52 /* Ok, that indicated we're done: double-check carefully. */
Thomas Gleixner239007b2009-11-17 16:46:45 +010053 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner32f41252011-03-28 14:10:52 +020054 inprogress = irqd_irq_inprogress(&desc->irq_data);
Thomas Gleixner239007b2009-11-17 16:46:45 +010055 raw_spin_unlock_irqrestore(&desc->lock, flags);
Herbert Xua98ce5c2007-10-23 11:26:25 +080056
57 /* Oops, that failed? */
Thomas Gleixner32f41252011-03-28 14:10:52 +020058 } while (inprogress);
Thomas Gleixner18258f72014-02-15 00:55:18 +000059}
Thomas Gleixner3aa551c2009-03-23 18:28:15 +010060
Thomas Gleixner18258f72014-02-15 00:55:18 +000061/**
62 * synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs)
63 * @irq: interrupt number to wait for
64 *
65 * This function waits for any pending hard IRQ handlers for this
66 * interrupt to complete before returning. If you use this
67 * function while holding a resource the IRQ handler may need you
68 * will deadlock. It does not take associated threaded handlers
69 * into account.
70 *
71 * Do not use this for shutdown scenarios where you must be sure
72 * that all parts (hardirq and threaded handler) have completed.
73 *
Peter Zijlstra02cea392015-02-05 14:06:23 +010074 * Returns: false if a threaded handler is active.
75 *
Thomas Gleixner18258f72014-02-15 00:55:18 +000076 * This function may be called - with care - from IRQ context.
77 */
Peter Zijlstra02cea392015-02-05 14:06:23 +010078bool synchronize_hardirq(unsigned int irq)
Thomas Gleixner18258f72014-02-15 00:55:18 +000079{
80 struct irq_desc *desc = irq_to_desc(irq);
81
Peter Zijlstra02cea392015-02-05 14:06:23 +010082 if (desc) {
Thomas Gleixner18258f72014-02-15 00:55:18 +000083 __synchronize_hardirq(desc);
Peter Zijlstra02cea392015-02-05 14:06:23 +010084 return !atomic_read(&desc->threads_active);
85 }
86
87 return true;
Thomas Gleixner18258f72014-02-15 00:55:18 +000088}
89EXPORT_SYMBOL(synchronize_hardirq);
90
91/**
92 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
93 * @irq: interrupt number to wait for
94 *
95 * This function waits for any pending IRQ handlers for this interrupt
96 * to complete before returning. If you use this function while
97 * holding a resource the IRQ handler may need you will deadlock.
98 *
99 * This function may be called - with care - from IRQ context.
100 */
101void synchronize_irq(unsigned int irq)
102{
103 struct irq_desc *desc = irq_to_desc(irq);
104
105 if (desc) {
106 __synchronize_hardirq(desc);
107 /*
108 * We made sure that no hardirq handler is
109 * running. Now verify that no threaded handlers are
110 * active.
111 */
112 wait_event(desc->wait_for_threads,
113 !atomic_read(&desc->threads_active));
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116EXPORT_SYMBOL(synchronize_irq);
117
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100118#ifdef CONFIG_SMP
119cpumask_var_t irq_default_affinity;
120
Thomas Gleixner9c255582016-07-04 17:39:23 +0900121static bool __irq_can_set_affinity(struct irq_desc *desc)
Jiang Liue019c242015-06-23 20:29:34 +0200122{
123 if (!desc || !irqd_can_balance(&desc->irq_data) ||
124 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
Thomas Gleixner9c255582016-07-04 17:39:23 +0900125 return false;
126 return true;
Jiang Liue019c242015-06-23 20:29:34 +0200127}
128
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800129/**
130 * irq_can_set_affinity - Check if the affinity of a given irq can be set
131 * @irq: Interrupt to check
132 *
133 */
134int irq_can_set_affinity(unsigned int irq)
135{
Jiang Liue019c242015-06-23 20:29:34 +0200136 return __irq_can_set_affinity(irq_to_desc(irq));
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800137}
138
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200139/**
Thomas Gleixner9c255582016-07-04 17:39:23 +0900140 * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space
141 * @irq: Interrupt to check
142 *
143 * Like irq_can_set_affinity() above, but additionally checks for the
144 * AFFINITY_MANAGED flag.
145 */
146bool irq_can_set_affinity_usr(unsigned int irq)
147{
148 struct irq_desc *desc = irq_to_desc(irq);
149
150 return __irq_can_set_affinity(desc) &&
151 !irqd_affinity_is_managed(&desc->irq_data);
152}
153
154/**
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200155 * irq_set_thread_affinity - Notify irq threads to adjust affinity
156 * @desc: irq descriptor which has affitnity changed
157 *
158 * We just set IRQTF_AFFINITY and delegate the affinity setting
159 * to the interrupt thread itself. We can not call
160 * set_cpus_allowed_ptr() here as we hold desc->lock and this
161 * code can be called from hard interrupt context.
162 */
163void irq_set_thread_affinity(struct irq_desc *desc)
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100164{
Daniel Lezcanof944b5a2016-01-14 10:54:13 +0100165 struct irqaction *action;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100166
Daniel Lezcanof944b5a2016-01-14 10:54:13 +0100167 for_each_action_of_desc(desc, action)
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100168 if (action->thread)
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200169 set_bit(IRQTF_AFFINITY, &action->thread_flags);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100170}
171
Thomas Gleixner19e1d4e2017-10-09 12:41:36 +0200172static void irq_validate_effective_affinity(struct irq_data *data)
173{
174#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
175 const struct cpumask *m = irq_data_get_effective_affinity_mask(data);
176 struct irq_chip *chip = irq_data_get_irq_chip(data);
177
178 if (!cpumask_empty(m))
179 return;
180 pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n",
181 chip->name, data->irq);
182#endif
183}
184
Jiang Liu818b0f32012-03-30 23:11:34 +0800185int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
186 bool force)
187{
188 struct irq_desc *desc = irq_data_to_desc(data);
189 struct irq_chip *chip = irq_data_get_irq_chip(data);
190 int ret;
191
Thomas Gleixnere43b3b52017-10-04 21:07:38 +0200192 if (!chip || !chip->irq_set_affinity)
193 return -EINVAL;
194
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000195 ret = chip->irq_set_affinity(data, mask, force);
Jiang Liu818b0f32012-03-30 23:11:34 +0800196 switch (ret) {
197 case IRQ_SET_MASK_OK:
Jiang Liu2cb62542014-11-06 22:20:18 +0800198 case IRQ_SET_MASK_OK_DONE:
Jiang Liu9df872f2015-06-03 11:47:50 +0800199 cpumask_copy(desc->irq_common_data.affinity, mask);
Gustavo A. R. Silva93417a32019-02-28 15:37:14 -0600200 /* fall through */
Jiang Liu818b0f32012-03-30 23:11:34 +0800201 case IRQ_SET_MASK_OK_NOCOPY:
Thomas Gleixner19e1d4e2017-10-09 12:41:36 +0200202 irq_validate_effective_affinity(data);
Jiang Liu818b0f32012-03-30 23:11:34 +0800203 irq_set_thread_affinity(desc);
204 ret = 0;
205 }
206
207 return ret;
208}
209
Thomas Gleixner12f47072018-06-04 17:33:59 +0200210#ifdef CONFIG_GENERIC_PENDING_IRQ
211static inline int irq_set_affinity_pending(struct irq_data *data,
212 const struct cpumask *dest)
213{
214 struct irq_desc *desc = irq_data_to_desc(data);
215
216 irqd_set_move_pending(data);
217 irq_copy_pending(desc, dest);
218 return 0;
219}
220#else
221static inline int irq_set_affinity_pending(struct irq_data *data,
222 const struct cpumask *dest)
223{
224 return -EBUSY;
225}
226#endif
227
228static int irq_try_set_affinity(struct irq_data *data,
229 const struct cpumask *dest, bool force)
230{
231 int ret = irq_do_set_affinity(data, dest, force);
232
233 /*
234 * In case that the underlying vector management is busy and the
235 * architecture supports the generic pending mechanism then utilize
236 * this to avoid returning an error to user space.
237 */
238 if (ret == -EBUSY && !force)
239 ret = irq_set_affinity_pending(data, dest);
240 return ret;
241}
242
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000243int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
244 bool force)
David Daneyc2d0c552011-03-25 12:38:50 -0700245{
246 struct irq_chip *chip = irq_data_get_irq_chip(data);
247 struct irq_desc *desc = irq_data_to_desc(data);
248 int ret = 0;
249
250 if (!chip || !chip->irq_set_affinity)
251 return -EINVAL;
252
Thomas Gleixner12f47072018-06-04 17:33:59 +0200253 if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) {
254 ret = irq_try_set_affinity(data, mask, force);
David Daneyc2d0c552011-03-25 12:38:50 -0700255 } else {
256 irqd_set_move_pending(data);
257 irq_copy_pending(desc, mask);
258 }
259
260 if (desc->affinity_notify) {
261 kref_get(&desc->affinity_notify->kref);
262 schedule_work(&desc->affinity_notify->work);
263 }
David Daneyc2d0c552011-03-25 12:38:50 -0700264 irqd_set(data, IRQD_AFFINITY_SET);
265
266 return ret;
267}
268
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000269int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800270{
Yinghai Lu08678b02008-08-19 20:50:05 -0700271 struct irq_desc *desc = irq_to_desc(irq);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100272 unsigned long flags;
David Daneyc2d0c552011-03-25 12:38:50 -0700273 int ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800274
David Daneyc2d0c552011-03-25 12:38:50 -0700275 if (!desc)
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800276 return -EINVAL;
277
Thomas Gleixner239007b2009-11-17 16:46:45 +0100278 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner01f8fa42014-04-16 14:36:44 +0000279 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100280 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100281 return ret;
Thomas Gleixner771ee3b2007-02-16 01:27:25 -0800282}
283
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700284int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
285{
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700286 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100287 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700288
289 if (!desc)
290 return -EINVAL;
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700291 desc->affinity_hint = m;
Thomas Gleixner02725e72011-02-12 10:37:36 +0100292 irq_put_desc_unlock(desc, flags);
Jesse Brandeburge2e64a92014-12-18 17:22:06 -0800293 /* set the initial affinity to prevent every interrupt being on CPU0 */
Jesse Brandeburg4fe7ffb2015-01-28 10:57:39 -0800294 if (m)
295 __irq_set_affinity(irq, m, false);
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -0700296 return 0;
297}
298EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
299
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000300static void irq_affinity_notify(struct work_struct *work)
301{
302 struct irq_affinity_notify *notify =
303 container_of(work, struct irq_affinity_notify, work);
304 struct irq_desc *desc = irq_to_desc(notify->irq);
305 cpumask_var_t cpumask;
306 unsigned long flags;
307
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100308 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000309 goto out;
310
311 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixner0ef5ca12011-03-28 21:59:37 +0200312 if (irq_move_pending(&desc->irq_data))
Thomas Gleixner1fa46f12011-02-07 16:46:58 +0100313 irq_get_pending(cpumask, desc);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000314 else
Jiang Liu9df872f2015-06-03 11:47:50 +0800315 cpumask_copy(cpumask, desc->irq_common_data.affinity);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000316 raw_spin_unlock_irqrestore(&desc->lock, flags);
317
318 notify->notify(notify, cpumask);
319
320 free_cpumask_var(cpumask);
321out:
322 kref_put(&notify->kref, notify->release);
323}
324
325/**
326 * irq_set_affinity_notifier - control notification of IRQ affinity changes
327 * @irq: Interrupt for which to enable/disable notification
328 * @notify: Context for notification, or %NULL to disable
329 * notification. Function pointers must be initialised;
330 * the other fields will be initialised by this function.
331 *
332 * Must be called in process context. Notification may only be enabled
333 * after the IRQ is allocated and must be disabled before the IRQ is
334 * freed using free_irq().
335 */
336int
337irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
338{
339 struct irq_desc *desc = irq_to_desc(irq);
340 struct irq_affinity_notify *old_notify;
341 unsigned long flags;
342
343 /* The release function is promised process context */
344 might_sleep();
345
Julien Thierryb5259032019-01-31 14:53:58 +0000346 if (!desc || desc->istate & IRQS_NMI)
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000347 return -EINVAL;
348
349 /* Complete initialisation of *notify */
350 if (notify) {
351 notify->irq = irq;
352 kref_init(&notify->kref);
353 INIT_WORK(&notify->work, irq_affinity_notify);
354 }
355
356 raw_spin_lock_irqsave(&desc->lock, flags);
357 old_notify = desc->affinity_notify;
358 desc->affinity_notify = notify;
359 raw_spin_unlock_irqrestore(&desc->lock, flags);
360
Prasad Sodagudi59c39842019-03-24 07:57:04 -0700361 if (old_notify) {
362 cancel_work_sync(&old_notify->work);
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000363 kref_put(&old_notify->kref, old_notify->release);
Prasad Sodagudi59c39842019-03-24 07:57:04 -0700364 }
Ben Hutchingscd7eab42011-01-19 21:01:44 +0000365
366 return 0;
367}
368EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
369
Max Krasnyansky18404752008-05-29 11:02:52 -0700370#ifndef CONFIG_AUTO_IRQ_AFFINITY
371/*
372 * Generic version of the affinity autoselector.
373 */
Thomas Gleixner43564bd2017-06-20 01:37:22 +0200374int irq_setup_affinity(struct irq_desc *desc)
Max Krasnyansky18404752008-05-29 11:02:52 -0700375{
Thomas Gleixner569bda82011-02-07 17:05:08 +0100376 struct cpumask *set = irq_default_affinity;
Thomas Gleixnercba42352017-06-20 01:37:21 +0200377 int ret, node = irq_desc_get_node(desc);
378 static DEFINE_RAW_SPINLOCK(mask_lock);
379 static struct cpumask mask;
Thomas Gleixner569bda82011-02-07 17:05:08 +0100380
Thomas Gleixnerb0082072011-02-07 17:30:50 +0100381 /* Excludes PER_CPU and NO_BALANCE interrupts */
Jiang Liue019c242015-06-23 20:29:34 +0200382 if (!__irq_can_set_affinity(desc))
Max Krasnyansky18404752008-05-29 11:02:52 -0700383 return 0;
384
Thomas Gleixnercba42352017-06-20 01:37:21 +0200385 raw_spin_lock(&mask_lock);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100386 /*
Masahiro Yamada9332ef92017-02-27 14:28:47 -0800387 * Preserve the managed affinity setting and a userspace affinity
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900388 * setup, but make sure that one of the targets is online.
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100389 */
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900390 if (irqd_affinity_is_managed(&desc->irq_data) ||
391 irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
Jiang Liu9df872f2015-06-03 11:47:50 +0800392 if (cpumask_intersects(desc->irq_common_data.affinity,
Thomas Gleixner569bda82011-02-07 17:05:08 +0100393 cpu_online_mask))
Jiang Liu9df872f2015-06-03 11:47:50 +0800394 set = desc->irq_common_data.affinity;
Thomas Gleixner0c6f8a82011-03-28 13:32:20 +0200395 else
Thomas Gleixner2bdd1052011-02-08 17:22:00 +0100396 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100397 }
Max Krasnyansky18404752008-05-29 11:02:52 -0700398
Thomas Gleixnercba42352017-06-20 01:37:21 +0200399 cpumask_and(&mask, cpu_online_mask, set);
Srinivas Ramanabddda602018-12-20 19:05:57 +0530400 if (cpumask_empty(&mask))
401 cpumask_copy(&mask, cpu_online_mask);
402
Prarit Bhargava241fc642012-03-26 15:02:18 -0400403 if (node != NUMA_NO_NODE) {
404 const struct cpumask *nodemask = cpumask_of_node(node);
405
406 /* make sure at least one of the cpus in nodemask is online */
Thomas Gleixnercba42352017-06-20 01:37:21 +0200407 if (cpumask_intersects(&mask, nodemask))
408 cpumask_and(&mask, &mask, nodemask);
Prarit Bhargava241fc642012-03-26 15:02:18 -0400409 }
Thomas Gleixnercba42352017-06-20 01:37:21 +0200410 ret = irq_do_set_affinity(&desc->irq_data, &mask, false);
411 raw_spin_unlock(&mask_lock);
412 return ret;
Max Krasnyansky18404752008-05-29 11:02:52 -0700413}
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100414#else
Jiang Liua8a98ea2015-06-04 12:13:30 +0800415/* Wrapper for ALPHA specific affinity selector magic */
Thomas Gleixnercba42352017-06-20 01:37:21 +0200416int irq_setup_affinity(struct irq_desc *desc)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100417{
Thomas Gleixnercba42352017-06-20 01:37:21 +0200418 return irq_select_affinity(irq_desc_get_irq(desc));
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100419}
Max Krasnyansky18404752008-05-29 11:02:52 -0700420#endif
421
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100422/*
Thomas Gleixnercba42352017-06-20 01:37:21 +0200423 * Called when a bogus affinity is set via /proc/irq
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100424 */
Thomas Gleixnercba42352017-06-20 01:37:21 +0200425int irq_select_affinity_usr(unsigned int irq)
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100426{
427 struct irq_desc *desc = irq_to_desc(irq);
428 unsigned long flags;
429 int ret;
430
Thomas Gleixner239007b2009-11-17 16:46:45 +0100431 raw_spin_lock_irqsave(&desc->lock, flags);
Thomas Gleixnercba42352017-06-20 01:37:21 +0200432 ret = irq_setup_affinity(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100433 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerf6d87f42008-11-07 13:18:30 +0100434 return ret;
435}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#endif
437
Feng Wufcf1ae22015-10-03 16:20:38 +0800438/**
439 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
440 * @irq: interrupt number to set affinity
Christoffer Dall250a53d2017-10-27 10:34:33 +0200441 * @vcpu_info: vCPU specific data or pointer to a percpu array of vCPU
442 * specific data for percpu_devid interrupts
Feng Wufcf1ae22015-10-03 16:20:38 +0800443 *
444 * This function uses the vCPU specific data to set the vCPU
445 * affinity for an irq. The vCPU specific data is passed from
446 * outside, such as KVM. One example code path is as below:
447 * KVM -> IOMMU -> irq_set_vcpu_affinity().
448 */
449int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
450{
451 unsigned long flags;
452 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
453 struct irq_data *data;
454 struct irq_chip *chip;
455 int ret = -ENOSYS;
456
457 if (!desc)
458 return -EINVAL;
459
460 data = irq_desc_get_irq_data(desc);
Marc Zyngier0abce642017-06-23 21:42:57 +0100461 do {
462 chip = irq_data_get_irq_chip(data);
463 if (chip && chip->irq_set_vcpu_affinity)
464 break;
465#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
466 data = data->parent_data;
467#else
468 data = NULL;
469#endif
470 } while (data);
471
472 if (data)
Feng Wufcf1ae22015-10-03 16:20:38 +0800473 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
474 irq_put_desc_unlock(desc, flags);
475
476 return ret;
477}
478EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
479
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200480void __disable_irq(struct irq_desc *desc)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100481{
Thomas Gleixner3aae9942011-02-04 10:17:52 +0100482 if (!desc->depth++)
Thomas Gleixner87923472011-02-03 12:27:44 +0100483 irq_disable(desc);
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100484}
485
Thomas Gleixner02725e72011-02-12 10:37:36 +0100486static int __disable_irq_nosync(unsigned int irq)
487{
488 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100489 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100490
491 if (!desc)
492 return -EINVAL;
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200493 __disable_irq(desc);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100494 irq_put_desc_busunlock(desc, flags);
495 return 0;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/**
499 * disable_irq_nosync - disable an irq without waiting
500 * @irq: Interrupt to disable
501 *
502 * Disable the selected interrupt line. Disables and Enables are
503 * nested.
504 * Unlike disable_irq(), this function does not ensure existing
505 * instances of the IRQ handler have completed before returning.
506 *
507 * This function may be called from IRQ context.
508 */
509void disable_irq_nosync(unsigned int irq)
510{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100511 __disable_irq_nosync(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513EXPORT_SYMBOL(disable_irq_nosync);
514
515/**
516 * disable_irq - disable an irq and wait for completion
517 * @irq: Interrupt to disable
518 *
519 * Disable the selected interrupt line. Enables and Disables are
520 * nested.
521 * This function waits for any pending IRQ handlers for this interrupt
522 * to complete before returning. If you use this function while
523 * holding a resource the IRQ handler may need you will deadlock.
524 *
525 * This function may be called - with care - from IRQ context.
526 */
527void disable_irq(unsigned int irq)
528{
Thomas Gleixner02725e72011-02-12 10:37:36 +0100529 if (!__disable_irq_nosync(irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 synchronize_irq(irq);
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532EXPORT_SYMBOL(disable_irq);
533
Peter Zijlstra02cea392015-02-05 14:06:23 +0100534/**
535 * disable_hardirq - disables an irq and waits for hardirq completion
536 * @irq: Interrupt to disable
537 *
538 * Disable the selected interrupt line. Enables and Disables are
539 * nested.
540 * This function waits for any pending hard IRQ handlers for this
541 * interrupt to complete before returning. If you use this function while
542 * holding a resource the hard IRQ handler may need you will deadlock.
543 *
544 * When used to optimistically disable an interrupt from atomic context
545 * the return value must be checked.
546 *
547 * Returns: false if a threaded handler is active.
548 *
549 * This function may be called - with care - from IRQ context.
550 */
551bool disable_hardirq(unsigned int irq)
552{
553 if (!__disable_irq_nosync(irq))
554 return synchronize_hardirq(irq);
555
556 return false;
557}
558EXPORT_SYMBOL_GPL(disable_hardirq);
559
Julien Thierryb5259032019-01-31 14:53:58 +0000560/**
561 * disable_nmi_nosync - disable an nmi without waiting
562 * @irq: Interrupt to disable
563 *
564 * Disable the selected interrupt line. Disables and enables are
565 * nested.
566 * The interrupt to disable must have been requested through request_nmi.
567 * Unlike disable_nmi(), this function does not ensure existing
568 * instances of the IRQ handler have completed before returning.
569 */
570void disable_nmi_nosync(unsigned int irq)
571{
572 disable_irq_nosync(irq);
573}
574
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200575void __enable_irq(struct irq_desc *desc)
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200576{
577 switch (desc->depth) {
578 case 0:
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100579 err_out:
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200580 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
581 irq_desc_get_irq(desc));
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200582 break;
583 case 1: {
Thomas Gleixnerc531e832011-02-08 12:44:58 +0100584 if (desc->istate & IRQS_SUSPENDED)
Rafael J. Wysocki0a0c5162009-03-16 22:33:49 +0100585 goto err_out;
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200586 /* Prevent probing on this irq: */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +0100587 irq_settings_set_noprobe(desc);
Thomas Gleixner201d7f42017-05-31 11:58:32 +0200588 /*
589 * Call irq_startup() not irq_enable() here because the
590 * interrupt might be marked NOAUTOEN. So irq_startup()
591 * needs to be invoked when it gets enabled the first
592 * time. If it was already started up, then irq_startup()
593 * will invoke irq_enable() under the hood.
594 */
Thomas Gleixnerc942cee2017-09-13 23:29:09 +0200595 irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE);
Thomas Gleixner201d7f42017-05-31 11:58:32 +0200596 break;
Thomas Gleixner1adb0852008-04-28 17:01:56 +0200597 }
598 default:
599 desc->depth--;
600 }
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/**
604 * enable_irq - enable handling of an irq
605 * @irq: Interrupt to enable
606 *
607 * Undoes the effect of one call to disable_irq(). If this
608 * matches the last disable, processing of interrupts on this
609 * IRQ line is re-enabled.
610 *
Thomas Gleixner70aedd22009-08-13 12:17:48 +0200611 * This function may be called from IRQ context only when
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200612 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
614void enable_irq(unsigned int irq)
615{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100617 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700619 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -0700620 return;
Thomas Gleixner50f7c032011-02-03 13:23:54 +0100621 if (WARN(!desc->irq_data.chip,
622 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
Thomas Gleixner02725e72011-02-12 10:37:36 +0100623 goto out;
Thomas Gleixner2656c362010-10-22 14:47:57 +0200624
Jiang Liu79ff1cd2015-06-23 19:52:36 +0200625 __enable_irq(desc);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100626out:
627 irq_put_desc_busunlock(desc, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629EXPORT_SYMBOL(enable_irq);
630
Julien Thierryb5259032019-01-31 14:53:58 +0000631/**
632 * enable_nmi - enable handling of an nmi
633 * @irq: Interrupt to enable
634 *
635 * The interrupt to enable must have been requested through request_nmi.
636 * Undoes the effect of one call to disable_nmi(). If this
637 * matches the last disable, processing of interrupts on this
638 * IRQ line is re-enabled.
639 */
640void enable_nmi(unsigned int irq)
641{
642 enable_irq(irq);
643}
644
David Brownell0c5d1eb2008-10-01 14:46:18 -0700645static int set_irq_wake_real(unsigned int irq, unsigned int on)
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200646{
Yinghai Lu08678b02008-08-19 20:50:05 -0700647 struct irq_desc *desc = irq_to_desc(irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200648 int ret = -ENXIO;
649
Santosh Shilimkar60f96b42011-09-09 13:59:35 +0530650 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
651 return 0;
652
Thomas Gleixner2f7e99b2010-09-27 12:45:50 +0000653 if (desc->irq_data.chip->irq_set_wake)
654 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200655
656 return ret;
657}
658
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700659/**
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100660 * irq_set_irq_wake - control irq power management wakeup
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700661 * @irq: interrupt to control
662 * @on: enable/disable power management wakeup
663 *
David Brownell15a647e2006-07-30 03:03:08 -0700664 * Enable/disable power management wakeup mode, which is
665 * disabled by default. Enables and disables must match,
666 * just as they match for non-wakeup mode support.
667 *
668 * Wakeup mode lets this IRQ wake the system from sleep
669 * states like "suspend to RAM".
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700670 */
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100671int irq_set_irq_wake(unsigned int irq, unsigned int on)
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700672{
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700673 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100674 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200675 int ret = 0;
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700676
Jesper Juhl13863a62011-06-09 23:14:58 +0200677 if (!desc)
678 return -EINVAL;
679
Julien Thierryb5259032019-01-31 14:53:58 +0000680 /* Don't use NMIs as wake up interrupts please */
681 if (desc->istate & IRQS_NMI) {
682 ret = -EINVAL;
683 goto out_unlock;
684 }
685
David Brownell15a647e2006-07-30 03:03:08 -0700686 /* wakeup-capable irqs can be shared between drivers that
687 * don't need to have the same sleep mode behaviors.
688 */
David Brownell15a647e2006-07-30 03:03:08 -0700689 if (on) {
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200690 if (desc->wake_depth++ == 0) {
691 ret = set_irq_wake_real(irq, on);
692 if (ret)
693 desc->wake_depth = 0;
694 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100695 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200696 }
David Brownell15a647e2006-07-30 03:03:08 -0700697 } else {
698 if (desc->wake_depth == 0) {
Arjan van de Ven7a2c4772008-07-25 01:45:54 -0700699 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200700 } else if (--desc->wake_depth == 0) {
701 ret = set_irq_wake_real(irq, on);
702 if (ret)
703 desc->wake_depth = 1;
704 else
Thomas Gleixner7f942262011-02-10 19:46:26 +0100705 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
Uwe Kleine-König2db87322008-07-23 14:42:25 +0200706 }
David Brownell15a647e2006-07-30 03:03:08 -0700707 }
Julien Thierryb5259032019-01-31 14:53:58 +0000708
709out_unlock:
Thomas Gleixner02725e72011-02-12 10:37:36 +0100710 irq_put_desc_busunlock(desc, flags);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700711 return ret;
712}
Thomas Gleixnera0cd9ca2011-02-10 11:36:33 +0100713EXPORT_SYMBOL(irq_set_irq_wake);
Thomas Gleixnerba9a2332006-06-29 02:24:55 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * Internal function that tells the architecture code whether a
717 * particular irq has been exclusively allocated or is available
718 * for driver use.
719 */
720int can_request_irq(unsigned int irq, unsigned long irqflags)
721{
Thomas Gleixnercc8c3b72010-03-23 22:40:53 +0100722 unsigned long flags;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +0100723 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
Thomas Gleixner02725e72011-02-12 10:37:36 +0100724 int canrequest = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Yinghai Lu7d94f7c2008-08-19 20:50:14 -0700726 if (!desc)
727 return 0;
728
Thomas Gleixner02725e72011-02-12 10:37:36 +0100729 if (irq_settings_can_request(desc)) {
Ben Hutchings2779db82013-06-28 02:40:30 +0100730 if (!desc->action ||
731 irqflags & desc->action->flags & IRQF_SHARED)
732 canrequest = 1;
Thomas Gleixner02725e72011-02-12 10:37:36 +0100733 }
734 irq_put_desc_unlock(desc, flags);
735 return canrequest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Jiang Liua1ff5412015-06-23 19:47:29 +0200738int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700739{
Thomas Gleixner6b8ff312010-10-01 12:58:38 +0200740 struct irq_chip *chip = desc->irq_data.chip;
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100741 int ret, unmask = 0;
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700742
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000743 if (!chip || !chip->irq_set_type) {
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700744 /*
745 * IRQF_TRIGGER_* but the PIC does not support multiple
746 * flow-types?
747 */
Jiang Liua1ff5412015-06-23 19:47:29 +0200748 pr_debug("No set_type function for IRQ %d (%s)\n",
749 irq_desc_get_irq(desc),
Thomas Gleixnerf5d89472012-04-19 12:06:13 +0200750 chip ? (chip->name ? : "unknown") : "unknown");
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700751 return 0;
752 }
753
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100754 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
Thomas Gleixner32f41252011-03-28 14:10:52 +0200755 if (!irqd_irq_masked(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100756 mask_irq(desc);
Thomas Gleixner32f41252011-03-28 14:10:52 +0200757 if (!irqd_irq_disabled(&desc->irq_data))
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100758 unmask = 1;
759 }
760
Alexander Kuleshov00b992d2016-07-19 15:54:08 +0600761 /* Mask all flags except trigger mode */
762 flags &= IRQ_TYPE_SENSE_MASK;
Thomas Gleixnerb2ba2c32010-09-27 12:45:47 +0000763 ret = chip->irq_set_type(&desc->irq_data, flags);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700764
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100765 switch (ret) {
766 case IRQ_SET_MASK_OK:
Jiang Liu2cb62542014-11-06 22:20:18 +0800767 case IRQ_SET_MASK_OK_DONE:
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100768 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
769 irqd_set(&desc->irq_data, flags);
Mathieu Malaterre44133f72019-01-14 21:31:54 +0100770 /* fall through */
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100771
772 case IRQ_SET_MASK_OK_NOCOPY:
773 flags = irqd_get_trigger_type(&desc->irq_data);
774 irq_settings_set_trigger_mask(desc, flags);
775 irqd_clear(&desc->irq_data, IRQD_LEVEL);
776 irq_settings_clr_level(desc);
777 if (flags & IRQ_TYPE_LEVEL_MASK) {
778 irq_settings_set_level(desc);
779 irqd_set(&desc->irq_data, IRQD_LEVEL);
780 }
Thomas Gleixner46732472010-06-07 17:53:51 +0200781
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100782 ret = 0;
Thomas Gleixner8fff39e2011-02-21 14:19:42 +0100783 break;
Thomas Gleixner876dbd42011-02-08 17:28:12 +0100784 default:
Andrew Morton97fd75b2012-05-31 16:26:07 -0700785 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
Jiang Liua1ff5412015-06-23 19:47:29 +0200786 flags, irq_desc_get_irq(desc), chip->irq_set_type);
David Brownell0c5d1eb2008-10-01 14:46:18 -0700787 }
Thomas Gleixnerd4d5e082011-02-10 13:16:14 +0100788 if (unmask)
789 unmask_irq(desc);
Uwe Kleine-König82736f42008-07-23 21:28:54 -0700790 return ret;
791}
792
Thomas Gleixner293a7a02012-10-16 15:07:49 -0700793#ifdef CONFIG_HARDIRQS_SW_RESEND
794int irq_set_parent(int irq, int parent_irq)
795{
796 unsigned long flags;
797 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
798
799 if (!desc)
800 return -EINVAL;
801
802 desc->parent_irq = parent_irq;
803
804 irq_put_desc_unlock(desc, flags);
805 return 0;
806}
Sudip Mukherjee3118dac2016-10-06 23:06:43 +0530807EXPORT_SYMBOL_GPL(irq_set_parent);
Thomas Gleixner293a7a02012-10-16 15:07:49 -0700808#endif
809
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200810/*
811 * Default primary interrupt handler for threaded interrupts. Is
812 * assigned as primary handler when request_threaded_irq is called
813 * with handler == NULL. Useful for oneshot interrupts.
814 */
815static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
816{
817 return IRQ_WAKE_THREAD;
818}
819
Thomas Gleixner399b5da2009-08-13 13:21:38 +0200820/*
821 * Primary handler for nested threaded interrupts. Should never be
822 * called.
823 */
824static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
825{
826 WARN(1, "Primary handler called for nested irq %d\n", irq);
827 return IRQ_NONE;
828}
829
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200830static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
831{
832 WARN(1, "Secondary action handler called for irq %d\n", irq);
833 return IRQ_NONE;
834}
835
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100836static int irq_wait_for_interrupt(struct irqaction *action)
837{
Lukas Wunner519cc862018-06-24 10:35:30 +0200838 for (;;) {
839 set_current_state(TASK_INTERRUPTIBLE);
Ido Yariv550acb12011-12-01 13:55:08 +0200840
Lukas Wunner519cc862018-06-24 10:35:30 +0200841 if (kthread_should_stop()) {
842 /* may need to run one last time */
843 if (test_and_clear_bit(IRQTF_RUNTHREAD,
844 &action->thread_flags)) {
845 __set_current_state(TASK_RUNNING);
846 return 0;
847 }
848 __set_current_state(TASK_RUNNING);
849 return -1;
850 }
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100851
852 if (test_and_clear_bit(IRQTF_RUNTHREAD,
853 &action->thread_flags)) {
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100854 __set_current_state(TASK_RUNNING);
855 return 0;
Thomas Gleixnerf48fe812009-03-24 11:46:22 +0100856 }
857 schedule();
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100858 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100859}
860
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200861/*
862 * Oneshot interrupts keep the irq line masked until the threaded
863 * handler finished. unmask if the interrupt has not been disabled and
864 * is marked MASKED.
865 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000866static void irq_finalize_oneshot(struct irq_desc *desc,
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100867 struct irqaction *action)
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200868{
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +0200869 if (!(desc->istate & IRQS_ONESHOT) ||
870 action->handler == irq_forced_secondary_handler)
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000871 return;
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100872again:
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000873 chip_bus_lock(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +0100874 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100875
876 /*
877 * Implausible though it may be we need to protect us against
878 * the following scenario:
879 *
880 * The thread is faster done than the hard interrupt handler
881 * on the other CPU. If we unmask the irq line then the
882 * interrupt can come in again and masks the line, leaves due
Thomas Gleixner009b4c32011-02-07 21:48:49 +0100883 * to IRQS_INPROGRESS and the irq line is masked forever.
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000884 *
885 * This also serializes the state of shared oneshot handlers
886 * versus "desc->threads_onehsot |= action->thread_mask;" in
887 * irq_wake_thread(). See the comment there which explains the
888 * serialization.
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100889 */
Thomas Gleixner32f41252011-03-28 14:10:52 +0200890 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100891 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000892 chip_bus_sync_unlock(desc);
Thomas Gleixner0b1adaa2010-03-09 19:45:54 +0100893 cpu_relax();
894 goto again;
895 }
896
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000897 /*
898 * Now check again, whether the thread should run. Otherwise
899 * we would clear the threads_oneshot bit of this thread which
900 * was just set.
901 */
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100902 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000903 goto out_unlock;
904
905 desc->threads_oneshot &= ~action->thread_mask;
906
Thomas Gleixner32f41252011-03-28 14:10:52 +0200907 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
908 irqd_irq_masked(&desc->irq_data))
Thomas Gleixner328a4972014-03-13 19:03:51 +0100909 unmask_threaded_irq(desc);
Thomas Gleixner32f41252011-03-28 14:10:52 +0200910
Thomas Gleixnerb5faba22011-02-23 23:52:13 +0000911out_unlock:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100912 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner3876ec92010-09-27 12:44:35 +0000913 chip_bus_sync_unlock(desc);
Thomas Gleixnerb25c3402009-08-13 12:17:22 +0200914}
915
Bruno Premont61f38262009-07-22 22:22:32 +0200916#ifdef CONFIG_SMP
Thomas Gleixner3aa551c2009-03-23 18:28:15 +0100917/*
Chuansheng Liub04c6442014-02-10 16:13:57 +0800918 * Check whether we need to change the affinity of the interrupt thread.
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200919 */
920static void
921irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
922{
923 cpumask_var_t mask;
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100924 bool valid = true;
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200925
926 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
927 return;
928
929 /*
930 * In case we are out of memory we set IRQTF_AFFINITY again and
931 * try again next time
932 */
933 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
934 set_bit(IRQTF_AFFINITY, &action->thread_flags);
935 return;
936 }
937
Thomas Gleixner239007b2009-11-17 16:46:45 +0100938 raw_spin_lock_irq(&desc->lock);
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100939 /*
940 * This code is triggered unconditionally. Check the affinity
941 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
942 */
Thomas Gleixnercbf86992018-02-16 15:21:20 +0100943 if (cpumask_available(desc->irq_common_data.affinity)) {
944 const struct cpumask *m;
945
946 m = irq_data_get_effective_affinity_mask(&desc->irq_data);
947 cpumask_copy(mask, m);
948 } else {
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100949 valid = false;
Thomas Gleixnercbf86992018-02-16 15:21:20 +0100950 }
Thomas Gleixner239007b2009-11-17 16:46:45 +0100951 raw_spin_unlock_irq(&desc->lock);
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200952
Thomas Gleixner04aa5302012-11-03 11:52:09 +0100953 if (valid)
954 set_cpus_allowed_ptr(current, mask);
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200955 free_cpumask_var(mask);
956}
Bruno Premont61f38262009-07-22 22:22:32 +0200957#else
958static inline void
959irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
960#endif
Thomas Gleixner591d2fb2009-07-21 11:09:39 +0200961
962/*
Ingo Molnarc5f48c02018-12-03 11:44:51 +0100963 * Interrupts which are not explicitly requested as threaded
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000964 * interrupts rely on the implicit bh/preempt disable of the hard irq
965 * context. So we need to disable bh here to avoid deadlocks and other
966 * side effects.
967 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200968static irqreturn_t
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000969irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
970{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200971 irqreturn_t ret;
972
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000973 local_bh_disable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200974 ret = action->thread_fn(action->irq, action->dev_id);
Lukas Wunner746a9232018-10-18 15:15:05 +0200975 if (ret == IRQ_HANDLED)
976 atomic_inc(&desc->threads_handled);
977
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100978 irq_finalize_oneshot(desc, action);
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000979 local_bh_enable();
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200980 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000981}
982
983/*
Xie XiuQif788e7b2013-10-18 09:12:04 +0800984 * Interrupts explicitly requested as threaded interrupts want to be
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000985 * preemtible - many of them need to sleep and wait for slow busses to
986 * complete.
987 */
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200988static irqreturn_t irq_thread_fn(struct irq_desc *desc,
989 struct irqaction *action)
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000990{
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200991 irqreturn_t ret;
992
993 ret = action->thread_fn(action->irq, action->dev_id);
Lukas Wunner746a9232018-10-18 15:15:05 +0200994 if (ret == IRQ_HANDLED)
995 atomic_inc(&desc->threads_handled);
996
Alexander Gordeevf3f79e32012-03-21 17:22:35 +0100997 irq_finalize_oneshot(desc, action);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +0200998 return ret;
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000999}
1000
Ido Yariv7140ea12011-12-02 18:24:12 +02001001static void wake_threads_waitq(struct irq_desc *desc)
1002{
Chuansheng Liuc6856892014-02-24 11:29:50 +08001003 if (atomic_dec_and_test(&desc->threads_active))
Ido Yariv7140ea12011-12-02 18:24:12 +02001004 wake_up(&desc->wait_for_threads);
1005}
1006
Al Viro67d12142012-06-27 11:07:19 +04001007static void irq_thread_dtor(struct callback_head *unused)
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +10001008{
1009 struct task_struct *tsk = current;
1010 struct irq_desc *desc;
1011 struct irqaction *action;
1012
1013 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
1014 return;
1015
1016 action = kthread_data(tsk);
1017
Linus Torvaldsfb21aff2012-05-31 18:47:30 -07001018 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
Alan Cox19af3952012-12-18 14:21:25 -08001019 tsk->comm, tsk->pid, action->irq);
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +10001020
1021
1022 desc = irq_to_desc(action->irq);
1023 /*
1024 * If IRQTF_RUNTHREAD is set, we need to decrement
1025 * desc->threads_active and wake possible waiters.
1026 */
1027 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
1028 wake_threads_waitq(desc);
1029
1030 /* Prevent a stale desc->threads_oneshot */
1031 irq_finalize_oneshot(desc, action);
1032}
1033
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001034static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
1035{
1036 struct irqaction *secondary = action->secondary;
1037
1038 if (WARN_ON_ONCE(!secondary))
1039 return;
1040
1041 raw_spin_lock_irq(&desc->lock);
1042 __irq_wake_thread(desc, secondary);
1043 raw_spin_unlock_irq(&desc->lock);
1044}
1045
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001046/*
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001047 * Interrupt handler thread
1048 */
1049static int irq_thread(void *data)
1050{
Al Viro67d12142012-06-27 11:07:19 +04001051 struct callback_head on_exit_work;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001052 struct irqaction *action = data;
1053 struct irq_desc *desc = irq_to_desc(action->irq);
Sebastian Andrzej Siewior3a43e052011-05-31 08:56:11 +02001054 irqreturn_t (*handler_fn)(struct irq_desc *desc,
1055 struct irqaction *action);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001056
Alexander Gordeev540b60e2012-03-09 14:59:13 +01001057 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001058 &action->thread_flags))
1059 handler_fn = irq_forced_thread_fn;
1060 else
1061 handler_fn = irq_thread_fn;
1062
Al Viro41f9d292012-06-26 22:10:04 +04001063 init_task_work(&on_exit_work, irq_thread_dtor);
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +10001064 task_work_add(current, &on_exit_work, false);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001065
Sankara Muthukrishnanf3de44e2012-10-31 15:41:23 -05001066 irq_thread_check_affinity(desc, action);
1067
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001068 while (!irq_wait_for_interrupt(action)) {
Ido Yariv7140ea12011-12-02 18:24:12 +02001069 irqreturn_t action_ret;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001070
Thomas Gleixner591d2fb2009-07-21 11:09:39 +02001071 irq_thread_check_affinity(desc, action);
1072
Ido Yariv7140ea12011-12-02 18:24:12 +02001073 action_ret = handler_fn(desc, action);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001074 if (action_ret == IRQ_WAKE_THREAD)
1075 irq_wake_secondary(desc, action);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001076
Ido Yariv7140ea12011-12-02 18:24:12 +02001077 wake_threads_waitq(desc);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001078 }
1079
Ido Yariv7140ea12011-12-02 18:24:12 +02001080 /*
1081 * This is the regular exit path. __free_irq() is stopping the
1082 * thread via kthread_stop() after calling
Lukas Wunner519cc862018-06-24 10:35:30 +02001083 * synchronize_hardirq(). So neither IRQTF_RUNTHREAD nor the
Lukas Wunner836557b2018-06-24 10:35:18 +02001084 * oneshot mask bit can be set.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001085 */
Oleg Nesterov4d1d61a2012-05-11 10:59:08 +10001086 task_work_cancel(current, irq_thread_dtor);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001087 return 0;
1088}
1089
Thomas Gleixnera92444c2014-02-15 00:55:19 +00001090/**
1091 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1092 * @irq: Interrupt line
1093 * @dev_id: Device identity for which the thread should be woken
1094 *
1095 */
1096void irq_wake_thread(unsigned int irq, void *dev_id)
1097{
1098 struct irq_desc *desc = irq_to_desc(irq);
1099 struct irqaction *action;
1100 unsigned long flags;
1101
1102 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1103 return;
1104
1105 raw_spin_lock_irqsave(&desc->lock, flags);
Daniel Lezcanof944b5a2016-01-14 10:54:13 +01001106 for_each_action_of_desc(desc, action) {
Thomas Gleixnera92444c2014-02-15 00:55:19 +00001107 if (action->dev_id == dev_id) {
1108 if (action->thread)
1109 __irq_wake_thread(desc, action);
1110 break;
1111 }
1112 }
1113 raw_spin_unlock_irqrestore(&desc->lock, flags);
1114}
1115EXPORT_SYMBOL_GPL(irq_wake_thread);
1116
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001117static int irq_setup_forced_threading(struct irqaction *new)
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001118{
1119 if (!force_irqthreads)
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001120 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001121 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001122 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001123
Thomas Gleixnerd1f03012018-08-03 14:44:59 +02001124 /*
1125 * No further action required for interrupts which are requested as
1126 * threaded interrupts already
1127 */
1128 if (new->handler == irq_default_primary_handler)
1129 return 0;
1130
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001131 new->flags |= IRQF_ONESHOT;
1132
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001133 /*
1134 * Handle the case where we have a real primary handler and a
1135 * thread handler. We force thread them as well by creating a
1136 * secondary action.
1137 */
Thomas Gleixnerd1f03012018-08-03 14:44:59 +02001138 if (new->handler && new->thread_fn) {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001139 /* Allocate the secondary action */
1140 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1141 if (!new->secondary)
1142 return -ENOMEM;
1143 new->secondary->handler = irq_forced_secondary_handler;
1144 new->secondary->thread_fn = new->thread_fn;
1145 new->secondary->dev_id = new->dev_id;
1146 new->secondary->irq = new->irq;
1147 new->secondary->name = new->name;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001148 }
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001149 /* Deal with the primary handler */
1150 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1151 new->thread_fn = new->handler;
1152 new->handler = irq_default_primary_handler;
1153 return 0;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001154}
1155
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001156static int irq_request_resources(struct irq_desc *desc)
1157{
1158 struct irq_data *d = &desc->irq_data;
1159 struct irq_chip *c = d->chip;
1160
1161 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1162}
1163
1164static void irq_release_resources(struct irq_desc *desc)
1165{
1166 struct irq_data *d = &desc->irq_data;
1167 struct irq_chip *c = d->chip;
1168
1169 if (c->irq_release_resources)
1170 c->irq_release_resources(d);
1171}
1172
Julien Thierryb5259032019-01-31 14:53:58 +00001173static bool irq_supports_nmi(struct irq_desc *desc)
1174{
1175 struct irq_data *d = irq_desc_get_irq_data(desc);
1176
1177#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1178 /* Only IRQs directly managed by the root irqchip can be set as NMI */
1179 if (d->parent_data)
1180 return false;
1181#endif
1182 /* Don't support NMIs for chips behind a slow bus */
1183 if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock)
1184 return false;
1185
1186 return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
1187}
1188
1189static int irq_nmi_setup(struct irq_desc *desc)
1190{
1191 struct irq_data *d = irq_desc_get_irq_data(desc);
1192 struct irq_chip *c = d->chip;
1193
1194 return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL;
1195}
1196
1197static void irq_nmi_teardown(struct irq_desc *desc)
1198{
1199 struct irq_data *d = irq_desc_get_irq_data(desc);
1200 struct irq_chip *c = d->chip;
1201
1202 if (c->irq_nmi_teardown)
1203 c->irq_nmi_teardown(d);
1204}
1205
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001206static int
1207setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1208{
1209 struct task_struct *t;
1210 struct sched_param param = {
1211 .sched_priority = MAX_USER_RT_PRIO/2,
1212 };
1213
1214 if (!secondary) {
1215 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1216 new->name);
1217 } else {
1218 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1219 new->name);
1220 param.sched_priority -= 1;
1221 }
1222
1223 if (IS_ERR(t))
1224 return PTR_ERR(t);
1225
1226 sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1227
1228 /*
1229 * We keep the reference to the task struct even if
1230 * the thread dies to avoid that the interrupt code
1231 * references an already freed task_struct.
1232 */
1233 get_task_struct(t);
1234 new->thread = t;
1235 /*
1236 * Tell the thread to set its affinity. This is
1237 * important for shared interrupt handlers as we do
1238 * not invoke setup_affinity() for the secondary
1239 * handlers as everything is already set up. Even for
1240 * interrupts marked with IRQF_NO_BALANCE this is
1241 * correct as we want the thread to move to the cpu(s)
1242 * on which the requesting code placed the interrupt.
1243 */
1244 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1245 return 0;
1246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/*
1249 * Internal function to register an irqaction - typically used to
1250 * allocate special interrupts that are part of the architecture.
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001251 *
1252 * Locking rules:
1253 *
1254 * desc->request_mutex Provides serialization against a concurrent free_irq()
1255 * chip_bus_lock Provides serialization for slow bus operations
1256 * desc->lock Provides serialization against hard interrupts
1257 *
1258 * chip_bus_lock and desc->lock are sufficient for all other management and
1259 * interrupt related functions. desc->request_mutex solely serializes
1260 * request/free_irq().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001262static int
Ingo Molnar327ec562009-02-15 11:21:37 +01001263__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Ingo Molnarf17c7542009-02-17 20:43:37 +01001265 struct irqaction *old, **old_ptr;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001266 unsigned long flags, thread_mask = 0;
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001267 int ret, nested, shared = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001269 if (!desc)
Matthew Wilcoxc2b5a252005-11-03 07:51:18 -07001270 return -EINVAL;
1271
Thomas Gleixner6b8ff312010-10-01 12:58:38 +02001272 if (desc->irq_data.chip == &no_irq_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -ENOSYS;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001274 if (!try_module_get(desc->owner))
1275 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001277 new->irq = irq;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
Jon Hunter4b357da2016-06-07 16:12:27 +01001280 * If the trigger type is not specified by the caller,
1281 * then use the default for this interrupt.
1282 */
1283 if (!(new->flags & IRQF_TRIGGER_MASK))
1284 new->flags |= irqd_get_trigger_type(&desc->irq_data);
1285
1286 /*
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001287 * Check whether the interrupt nests into another interrupt
1288 * thread.
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001289 */
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01001290 nested = irq_settings_is_nested_thread(desc);
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001291 if (nested) {
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001292 if (!new->thread_fn) {
1293 ret = -EINVAL;
1294 goto out_mput;
1295 }
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001296 /*
1297 * Replace the primary handler which was provided from
1298 * the driver for non nested interrupt handling by the
1299 * dummy function which warns when called.
1300 */
1301 new->handler = irq_nested_primary_handler;
Thomas Gleixner8d32a302011-02-23 23:52:23 +00001302 } else {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001303 if (irq_settings_can_thread(desc)) {
1304 ret = irq_setup_forced_threading(new);
1305 if (ret)
1306 goto out_mput;
1307 }
Thomas Gleixner399b5da2009-08-13 13:21:38 +02001308 }
1309
1310 /*
1311 * Create a handler thread when a thread function is supplied
1312 * and the interrupt does not nest into another interrupt
1313 * thread.
1314 */
1315 if (new->thread_fn && !nested) {
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001316 ret = setup_irq_thread(new, irq, false);
1317 if (ret)
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001318 goto out_mput;
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001319 if (new->secondary) {
1320 ret = setup_irq_thread(new->secondary, irq, true);
1321 if (ret)
1322 goto out_thread;
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001323 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001324 }
1325
1326 /*
Thomas Gleixnerdc9b2292012-07-13 19:29:45 +02001327 * Drivers are often written to work w/o knowledge about the
1328 * underlying irq chip implementation, so a request for a
1329 * threaded irq without a primary hard irq context handler
1330 * requires the ONESHOT flag to be set. Some irq chips like
1331 * MSI based interrupts are per se one shot safe. Check the
1332 * chip flags, so we can avoid the unmask dance at the end of
1333 * the threaded handler for those.
1334 */
1335 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1336 new->flags &= ~IRQF_ONESHOT;
1337
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001338 /*
1339 * Protects against a concurrent __free_irq() call which might wait
Lukas Wunner519cc862018-06-24 10:35:30 +02001340 * for synchronize_hardirq() to complete without holding the optional
Lukas Wunner836557b2018-06-24 10:35:18 +02001341 * chip bus lock and desc->lock. Also protects against handing out
1342 * a recycled oneshot thread_mask bit while it's still in use by
1343 * its previous owner.
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001344 */
Thomas Gleixner91140142017-06-29 23:33:37 +02001345 mutex_lock(&desc->request_mutex);
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001346
1347 /*
1348 * Acquire bus lock as the irq_request_resources() callback below
1349 * might rely on the serialization or the magic power management
1350 * functions which are abusing the irq_bus_lock() callback,
1351 */
1352 chip_bus_lock(desc);
1353
1354 /* First installed action requests resources. */
Thomas Gleixner46e48e22017-06-29 23:33:38 +02001355 if (!desc->action) {
1356 ret = irq_request_resources(desc);
1357 if (ret) {
1358 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1359 new->name, irq, desc->irq_data.chip->name);
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001360 goto out_bus_unlock;
Thomas Gleixner46e48e22017-06-29 23:33:38 +02001361 }
1362 }
Thomas Gleixner91140142017-06-29 23:33:37 +02001363
Thomas Gleixnerdc9b2292012-07-13 19:29:45 +02001364 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * The following block of code has to be executed atomically
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001366 * protected against a concurrent interrupt and any of the other
1367 * management calls which are not serialized via
1368 * desc->request_mutex or the optional bus lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
Thomas Gleixner239007b2009-11-17 16:46:45 +01001370 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarf17c7542009-02-17 20:43:37 +01001371 old_ptr = &desc->action;
1372 old = *old_ptr;
Ingo Molnar06fcb0c2006-06-29 02:24:40 -07001373 if (old) {
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001374 /*
1375 * Can't share interrupts unless both agree to and are
1376 * the same type (level, edge, polarity). So both flag
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001377 * fields must have IRQF_SHARED set and the bits which
Thomas Gleixner9d591ed2011-02-23 23:52:16 +00001378 * set the trigger type must match. Also all must
1379 * agree on ONESHOT.
Julien Thierryb5259032019-01-31 14:53:58 +00001380 * Interrupt lines used for NMIs cannot be shared.
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001381 */
Marc Zyngier4f8413a2017-11-09 14:17:59 +00001382 unsigned int oldtype;
1383
Julien Thierryb5259032019-01-31 14:53:58 +00001384 if (desc->istate & IRQS_NMI) {
1385 pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n",
1386 new->name, irq, desc->irq_data.chip->name);
1387 ret = -EINVAL;
1388 goto out_unlock;
1389 }
1390
Marc Zyngier4f8413a2017-11-09 14:17:59 +00001391 /*
1392 * If nobody did set the configuration before, inherit
1393 * the one provided by the requester.
1394 */
1395 if (irqd_trigger_type_was_set(&desc->irq_data)) {
1396 oldtype = irqd_get_trigger_type(&desc->irq_data);
1397 } else {
1398 oldtype = new->flags & IRQF_TRIGGER_MASK;
1399 irqd_set_trigger_type(&desc->irq_data, oldtype);
1400 }
Hans de Goede382bd4d2017-04-15 12:08:31 +02001401
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001402 if (!((old->flags & new->flags) & IRQF_SHARED) ||
Hans de Goede382bd4d2017-04-15 12:08:31 +02001403 (oldtype != (new->flags & IRQF_TRIGGER_MASK)) ||
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001404 ((old->flags ^ new->flags) & IRQF_ONESHOT))
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001405 goto mismatch;
1406
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001407 /* All handlers must agree on per-cpuness */
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001408 if ((old->flags & IRQF_PERCPU) !=
1409 (new->flags & IRQF_PERCPU))
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001410 goto mismatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* add new interrupt at end of irq queue */
1413 do {
Thomas Gleixner52abb702012-03-06 23:18:54 +01001414 /*
1415 * Or all existing action->thread_mask bits,
1416 * so we can find the next zero bit for this
1417 * new action.
1418 */
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001419 thread_mask |= old->thread_mask;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001420 old_ptr = &old->next;
1421 old = *old_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 } while (old);
1423 shared = 1;
1424 }
1425
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001426 /*
Thomas Gleixner52abb702012-03-06 23:18:54 +01001427 * Setup the thread mask for this irqaction for ONESHOT. For
1428 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1429 * conditional in irq_wake_thread().
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001430 */
Thomas Gleixner52abb702012-03-06 23:18:54 +01001431 if (new->flags & IRQF_ONESHOT) {
1432 /*
1433 * Unlikely to have 32 resp 64 irqs sharing one line,
1434 * but who knows.
1435 */
1436 if (thread_mask == ~0UL) {
1437 ret = -EBUSY;
Thomas Gleixnercba42352017-06-20 01:37:21 +02001438 goto out_unlock;
Thomas Gleixner52abb702012-03-06 23:18:54 +01001439 }
1440 /*
1441 * The thread_mask for the action is or'ed to
1442 * desc->thread_active to indicate that the
1443 * IRQF_ONESHOT thread handler has been woken, but not
1444 * yet finished. The bit is cleared when a thread
1445 * completes. When all threads of a shared interrupt
1446 * line have completed desc->threads_active becomes
1447 * zero and the interrupt line is unmasked. See
1448 * handle.c:irq_wake_thread() for further information.
1449 *
1450 * If no thread is woken by primary (hard irq context)
1451 * interrupt handlers, then desc->threads_active is
1452 * also checked for zero to unmask the irq line in the
1453 * affected hard irq flow handlers
1454 * (handle_[fasteoi|level]_irq).
1455 *
1456 * The new action gets the first zero bit of
1457 * thread_mask assigned. See the loop above which or's
1458 * all existing action->thread_mask bits.
1459 */
Rasmus Villemoesffc661c2017-10-30 22:35:47 +01001460 new->thread_mask = 1UL << ffz(thread_mask);
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001461
Thomas Gleixnerdc9b2292012-07-13 19:29:45 +02001462 } else if (new->handler == irq_default_primary_handler &&
1463 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001464 /*
1465 * The interrupt was requested with handler = NULL, so
1466 * we use the default primary handler for it. But it
1467 * does not have the oneshot flag set. In combination
1468 * with level interrupts this is deadly, because the
1469 * default primary handler just wakes the thread, then
1470 * the irq lines is reenabled, but the device still
1471 * has the level irq asserted. Rinse and repeat....
1472 *
1473 * While this works for edge type interrupts, we play
1474 * it safe and reject unconditionally because we can't
1475 * say for sure which type this interrupt really
1476 * has. The type flags are unreliable as the
1477 * underlying chip implementation can override them.
1478 */
Andrew Morton97fd75b2012-05-31 16:26:07 -07001479 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
Thomas Gleixner1c6c6952012-04-19 10:35:17 +02001480 irq);
1481 ret = -EINVAL;
Thomas Gleixnercba42352017-06-20 01:37:21 +02001482 goto out_unlock;
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001483 }
Thomas Gleixnerb5faba22011-02-23 23:52:13 +00001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (!shared) {
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001486 init_waitqueue_head(&desc->wait_for_threads);
1487
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001488 /* Setup the type (level, edge polarity) if configured: */
1489 if (new->flags & IRQF_TRIGGER_MASK) {
Jiang Liua1ff5412015-06-23 19:47:29 +02001490 ret = __irq_set_trigger(desc,
1491 new->flags & IRQF_TRIGGER_MASK);
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001492
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001493 if (ret)
Thomas Gleixnercba42352017-06-20 01:37:21 +02001494 goto out_unlock;
Thomas Gleixner091738a2011-02-14 20:16:43 +01001495 }
Ahmed S. Darwishf75d2222007-05-08 00:27:55 -07001496
Thomas Gleixnerc942cee2017-09-13 23:29:09 +02001497 /*
1498 * Activate the interrupt. That activation must happen
1499 * independently of IRQ_NOAUTOEN. request_irq() can fail
1500 * and the callers are supposed to handle
1501 * that. enable_irq() of an interrupt requested with
1502 * IRQ_NOAUTOEN is not supposed to fail. The activation
1503 * keeps it in shutdown mode, it merily associates
1504 * resources if necessary and if that's not possible it
1505 * fails. Interrupts which are in managed shutdown mode
1506 * will simply ignore that activation request.
1507 */
1508 ret = irq_activate(desc);
1509 if (ret)
1510 goto out_unlock;
1511
Thomas Gleixner009b4c32011-02-07 21:48:49 +01001512 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
Thomas Gleixner32f41252011-03-28 14:10:52 +02001513 IRQS_ONESHOT | IRQS_WAITING);
1514 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
Thomas Gleixner94d39e12006-06-29 02:24:50 -07001515
Thomas Gleixnera0056772011-02-08 17:11:03 +01001516 if (new->flags & IRQF_PERCPU) {
1517 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1518 irq_settings_set_per_cpu(desc);
1519 }
Thomas Gleixner6a58fb32011-02-08 15:40:05 +01001520
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001521 if (new->flags & IRQF_ONESHOT)
Thomas Gleixner3d67bae2011-02-07 21:02:10 +01001522 desc->istate |= IRQS_ONESHOT;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001523
Thomas Gleixner2e051552017-06-20 01:37:23 +02001524 /* Exclude IRQ from balancing if requested */
1525 if (new->flags & IRQF_NOBALANCING) {
1526 irq_settings_set_no_balancing(desc);
1527 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1528 }
1529
Thomas Gleixner04c848d2017-05-31 11:58:33 +02001530 if (irq_settings_can_autoenable(desc)) {
Thomas Gleixner4cde9c62017-06-20 01:37:49 +02001531 irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
Thomas Gleixner04c848d2017-05-31 11:58:33 +02001532 } else {
1533 /*
1534 * Shared interrupts do not go well with disabling
1535 * auto enable. The sharing interrupt might request
1536 * it while it's still disabled and then wait for
1537 * interrupts forever.
1538 */
1539 WARN_ON_ONCE(new->flags & IRQF_SHARED);
Thomas Gleixnere76de9f2006-06-29 02:24:56 -07001540 /* Undo nested disables: */
1541 desc->depth = 1;
Thomas Gleixner04c848d2017-05-31 11:58:33 +02001542 }
Max Krasnyansky18404752008-05-29 11:02:52 -07001543
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001544 } else if (new->flags & IRQF_TRIGGER_MASK) {
1545 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
Thomas Gleixner7ee7e872016-11-07 19:57:00 +01001546 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
Thomas Gleixner876dbd42011-02-08 17:28:12 +01001547
1548 if (nmsk != omsk)
1549 /* hope the handler works with current trigger mode */
Joe Perchesa395d6a2016-03-22 14:28:09 -07001550 pr_warn("irq %d uses trigger mode %u; requested %u\n",
Thomas Gleixner7ee7e872016-11-07 19:57:00 +01001551 irq, omsk, nmsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001553
Ingo Molnarf17c7542009-02-17 20:43:37 +01001554 *old_ptr = new;
Uwe Kleine-König82736f42008-07-23 21:28:54 -07001555
Thomas Gleixnercab303b2014-08-28 11:44:31 +02001556 irq_pm_install_action(desc, new);
1557
Linus Torvalds8528b0f2007-01-23 14:16:31 -08001558 /* Reset broken irq detection when installing new handler */
1559 desc->irq_count = 0;
1560 desc->irqs_unhandled = 0;
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001561
1562 /*
1563 * Check whether we disabled the irq via the spurious handler
1564 * before. Reenable it and give it another chance.
1565 */
Thomas Gleixner7acdd532011-02-07 20:40:54 +01001566 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1567 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
Jiang Liu79ff1cd2015-06-23 19:52:36 +02001568 __enable_irq(desc);
Thomas Gleixner1adb0852008-04-28 17:01:56 +02001569 }
1570
Thomas Gleixner239007b2009-11-17 16:46:45 +01001571 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner3a907952017-06-29 23:33:36 +02001572 chip_bus_sync_unlock(desc);
Thomas Gleixner91140142017-06-29 23:33:37 +02001573 mutex_unlock(&desc->request_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Daniel Lezcanob2d3d612017-06-23 16:11:07 +02001575 irq_setup_timings(desc, new);
1576
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001577 /*
1578 * Strictly no need to wake it up, but hung_task complains
1579 * when no hard interrupt wakes the thread up.
1580 */
1581 if (new->thread)
1582 wake_up_process(new->thread);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001583 if (new->secondary)
1584 wake_up_process(new->secondary->thread);
Thomas Gleixner69ab8492009-08-17 14:07:16 +02001585
Yinghai Lu2c6927a2008-08-19 20:50:11 -07001586 register_irq_proc(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 new->dir = NULL;
1588 register_handler_proc(irq, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return 0;
Dimitri Sivanichf5163422006-03-25 03:08:23 -08001590
1591mismatch:
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001592 if (!(new->flags & IRQF_PROBE_SHARED)) {
Andrew Morton97fd75b2012-05-31 16:26:07 -07001593 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001594 irq, new->flags, new->name, old->flags, old->name);
1595#ifdef CONFIG_DEBUG_SHIRQ
Andrew Morton13e87ec2006-04-27 18:39:18 -07001596 dump_stack();
Alan Cox3f050442007-02-12 00:52:04 -08001597#endif
Thomas Gleixnerf5d89472012-04-19 12:06:13 +02001598 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001599 ret = -EBUSY;
1600
Thomas Gleixnercba42352017-06-20 01:37:21 +02001601out_unlock:
Dan Carpenter1c389792011-03-17 14:43:07 +03001602 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner3b8249e2011-02-07 16:02:20 +01001603
Thomas Gleixner46e48e22017-06-29 23:33:38 +02001604 if (!desc->action)
1605 irq_release_resources(desc);
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001606out_bus_unlock:
1607 chip_bus_sync_unlock(desc);
Thomas Gleixner91140142017-06-29 23:33:37 +02001608 mutex_unlock(&desc->request_mutex);
1609
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001610out_thread:
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001611 if (new->thread) {
1612 struct task_struct *t = new->thread;
1613
1614 new->thread = NULL;
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001615 kthread_stop(t);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001616 put_task_struct(t);
1617 }
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001618 if (new->secondary && new->secondary->thread) {
1619 struct task_struct *t = new->secondary->thread;
1620
1621 new->secondary->thread = NULL;
1622 kthread_stop(t);
1623 put_task_struct(t);
1624 }
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001625out_mput:
1626 module_put(desc->owner);
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
1630/**
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001631 * setup_irq - setup an interrupt
1632 * @irq: Interrupt line to setup
1633 * @act: irqaction for the interrupt
1634 *
1635 * Used to statically setup interrupts in the early boot process.
1636 */
1637int setup_irq(unsigned int irq, struct irqaction *act)
1638{
David Daney986c0112011-02-09 16:04:25 -08001639 int retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001640 struct irq_desc *desc = irq_to_desc(irq);
1641
Jon Hunter9b5d5852016-05-10 16:14:35 +01001642 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001643 return -EINVAL;
Jon Hunterbe45beb2016-06-07 16:12:29 +01001644
1645 retval = irq_chip_pm_get(&desc->irq_data);
1646 if (retval < 0)
1647 return retval;
1648
David Daney986c0112011-02-09 16:04:25 -08001649 retval = __setup_irq(irq, desc, act);
David Daney986c0112011-02-09 16:04:25 -08001650
Jon Hunterbe45beb2016-06-07 16:12:29 +01001651 if (retval)
1652 irq_chip_pm_put(&desc->irq_data);
1653
David Daney986c0112011-02-09 16:04:25 -08001654 return retval;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001655}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001656EXPORT_SYMBOL_GPL(setup_irq);
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001657
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001658/*
Magnus Dammcbf94f02009-03-12 21:05:51 +09001659 * Internal function to unregister an irqaction - used to free
1660 * regular and special interrupts that are part of the architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
Uwe Kleine König83ac4ca2018-03-19 11:52:02 +01001662static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Uwe Kleine König83ac4ca2018-03-19 11:52:02 +01001664 unsigned irq = desc->irq_data.irq;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001665 struct irqaction *action, **action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 unsigned long flags;
1667
Ingo Molnarae88a232009-02-15 11:29:50 +01001668 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001669
Thomas Gleixner91140142017-06-29 23:33:37 +02001670 mutex_lock(&desc->request_mutex);
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001671 chip_bus_lock(desc);
Thomas Gleixner239007b2009-11-17 16:46:45 +01001672 raw_spin_lock_irqsave(&desc->lock, flags);
Ingo Molnarae88a232009-02-15 11:29:50 +01001673
1674 /*
1675 * There can be multiple actions per IRQ descriptor, find the right
1676 * one based on the dev_id:
1677 */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001678 action_ptr = &desc->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 for (;;) {
Ingo Molnarf17c7542009-02-17 20:43:37 +01001680 action = *action_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Ingo Molnarae88a232009-02-15 11:29:50 +01001682 if (!action) {
1683 WARN(1, "Trying to free already-free IRQ %d\n", irq);
Thomas Gleixner239007b2009-11-17 16:46:45 +01001684 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001685 chip_bus_sync_unlock(desc);
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001686 mutex_unlock(&desc->request_mutex);
Magnus Dammf21cfb22009-03-12 21:05:42 +09001687 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001689
Ingo Molnar8316e382009-02-17 20:28:29 +01001690 if (action->dev_id == dev_id)
1691 break;
Ingo Molnarf17c7542009-02-17 20:43:37 +01001692 action_ptr = &action->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Ingo Molnarae88a232009-02-15 11:29:50 +01001694
1695 /* Found it - now remove it from the list of entries: */
Ingo Molnarf17c7542009-02-17 20:43:37 +01001696 *action_ptr = action->next;
Ingo Molnarae88a232009-02-15 11:29:50 +01001697
Thomas Gleixnercab303b2014-08-28 11:44:31 +02001698 irq_pm_remove_action(desc, action);
1699
Ingo Molnarae88a232009-02-15 11:29:50 +01001700 /* If this was the last handler, shut down the IRQ line: */
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001701 if (!desc->action) {
Thomas Gleixnere9849772015-10-09 23:28:58 +02001702 irq_settings_clr_disable_unlazy(desc);
Thomas Gleixner4001d8e2019-06-28 13:11:49 +02001703 /* Only shutdown. Deactivate after synchronize_hardirq() */
Thomas Gleixner46999232011-02-02 21:41:14 +00001704 irq_shutdown(desc);
Thomas Gleixnerc1bacba2014-03-08 08:59:58 +01001705 }
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001706
Peter P Waskiewicz Jre7a297b2010-04-30 14:44:50 -07001707#ifdef CONFIG_SMP
1708 /* make sure affinity_hint is cleaned up */
1709 if (WARN_ON_ONCE(desc->affinity_hint))
1710 desc->affinity_hint = NULL;
1711#endif
1712
Thomas Gleixner239007b2009-11-17 16:46:45 +01001713 raw_spin_unlock_irqrestore(&desc->lock, flags);
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001714 /*
1715 * Drop bus_lock here so the changes which were done in the chip
1716 * callbacks above are synced out to the irq chips which hang
Lukas Wunner519cc862018-06-24 10:35:30 +02001717 * behind a slow bus (I2C, SPI) before calling synchronize_hardirq().
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001718 *
1719 * Aside of that the bus_lock can also be taken from the threaded
1720 * handler in irq_finalize_oneshot() which results in a deadlock
Lukas Wunner519cc862018-06-24 10:35:30 +02001721 * because kthread_stop() would wait forever for the thread to
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001722 * complete, which is blocked on the bus lock.
1723 *
1724 * The still held desc->request_mutex() protects against a
1725 * concurrent request_irq() of this irq so the release of resources
1726 * and timing data is properly serialized.
1727 */
Thomas Gleixnerabc7e402015-12-13 18:12:30 +01001728 chip_bus_sync_unlock(desc);
Ingo Molnarae88a232009-02-15 11:29:50 +01001729
1730 unregister_handler_proc(irq, action);
1731
1732 /* Make sure it's not being used on another CPU: */
Lukas Wunner519cc862018-06-24 10:35:30 +02001733 synchronize_hardirq(irq);
Ingo Molnarae88a232009-02-15 11:29:50 +01001734
1735#ifdef CONFIG_DEBUG_SHIRQ
1736 /*
1737 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1738 * event to happen even now it's being freed, so let's make sure that
1739 * is so by doing an extra call to the handler ....
1740 *
1741 * ( We do this after actually deregistering it, to make sure that a
Jonathan Neuschäfer0a13ec02018-06-17 14:40:18 +02001742 * 'real' IRQ doesn't run in parallel with our fake. )
Ingo Molnarae88a232009-02-15 11:29:50 +01001743 */
1744 if (action->flags & IRQF_SHARED) {
1745 local_irq_save(flags);
1746 action->handler(irq, dev_id);
1747 local_irq_restore(flags);
1748 }
1749#endif
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001750
Lukas Wunner519cc862018-06-24 10:35:30 +02001751 /*
1752 * The action has already been removed above, but the thread writes
1753 * its oneshot mask bit when it completes. Though request_mutex is
1754 * held across this which prevents __setup_irq() from handing out
1755 * the same bit to a newly requested action.
1756 */
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001757 if (action->thread) {
Alexander Gordeev05d74ef2012-03-09 14:59:40 +01001758 kthread_stop(action->thread);
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001759 put_task_struct(action->thread);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001760 if (action->secondary && action->secondary->thread) {
1761 kthread_stop(action->secondary->thread);
1762 put_task_struct(action->secondary->thread);
1763 }
Linus Torvalds2d860ad2009-08-13 13:05:10 -07001764 }
1765
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001766 /* Last action releases resources */
Thomas Gleixner23438772017-06-29 23:33:39 +02001767 if (!desc->action) {
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001768 /*
1769 * Reaquire bus lock as irq_release_resources() might
1770 * require it to deallocate resources over the slow bus.
1771 */
1772 chip_bus_lock(desc);
Thomas Gleixner4001d8e2019-06-28 13:11:49 +02001773 /*
1774 * There is no interrupt on the fly anymore. Deactivate it
1775 * completely.
1776 */
1777 raw_spin_lock_irqsave(&desc->lock, flags);
1778 irq_domain_deactivate_irq(&desc->irq_data);
1779 raw_spin_unlock_irqrestore(&desc->lock, flags);
1780
Thomas Gleixner46e48e22017-06-29 23:33:38 +02001781 irq_release_resources(desc);
Thomas Gleixner19d39a32017-07-11 23:41:52 +02001782 chip_bus_sync_unlock(desc);
Thomas Gleixner23438772017-06-29 23:33:39 +02001783 irq_remove_timings(desc);
1784 }
Thomas Gleixner46e48e22017-06-29 23:33:38 +02001785
Thomas Gleixner91140142017-06-29 23:33:37 +02001786 mutex_unlock(&desc->request_mutex);
1787
Jon Hunterbe45beb2016-06-07 16:12:29 +01001788 irq_chip_pm_put(&desc->irq_data);
Sebastian Andrzej Siewiorb6873802011-07-11 12:17:31 +02001789 module_put(desc->owner);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02001790 kfree(action->secondary);
Magnus Dammf21cfb22009-03-12 21:05:42 +09001791 return action;
1792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794/**
Magnus Dammcbf94f02009-03-12 21:05:51 +09001795 * remove_irq - free an interrupt
1796 * @irq: Interrupt line to free
1797 * @act: irqaction for the interrupt
1798 *
1799 * Used to remove interrupts statically setup by the early boot process.
1800 */
1801void remove_irq(unsigned int irq, struct irqaction *act)
1802{
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001803 struct irq_desc *desc = irq_to_desc(irq);
1804
1805 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Uwe Kleine König83ac4ca2018-03-19 11:52:02 +01001806 __free_irq(desc, act->dev_id);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001807}
Magnus Dammeb53b4e2009-03-12 21:05:59 +09001808EXPORT_SYMBOL_GPL(remove_irq);
Magnus Dammcbf94f02009-03-12 21:05:51 +09001809
1810/**
Magnus Dammf21cfb22009-03-12 21:05:42 +09001811 * free_irq - free an interrupt allocated with request_irq
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * @irq: Interrupt line to free
1813 * @dev_id: Device identity to free
1814 *
1815 * Remove an interrupt handler. The handler is removed and if the
1816 * interrupt line is no longer in use by any driver it is disabled.
1817 * On a shared IRQ the caller must ensure the interrupt is disabled
1818 * on the card it drives before calling this function. The function
1819 * does not return until any executing interrupts for this IRQ
1820 * have completed.
1821 *
1822 * This function must not be called from interrupt context.
Christoph Hellwig25ce4be2017-04-13 09:06:41 +02001823 *
1824 * Returns the devname argument passed to request_irq.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
Christoph Hellwig25ce4be2017-04-13 09:06:41 +02001826const void *free_irq(unsigned int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001828 struct irq_desc *desc = irq_to_desc(irq);
Christoph Hellwig25ce4be2017-04-13 09:06:41 +02001829 struct irqaction *action;
1830 const char *devname;
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001831
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001832 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Christoph Hellwig25ce4be2017-04-13 09:06:41 +02001833 return NULL;
Thomas Gleixner70aedd22009-08-13 12:17:48 +02001834
Ben Hutchingscd7eab42011-01-19 21:01:44 +00001835#ifdef CONFIG_SMP
1836 if (WARN_ON(desc->affinity_notify))
1837 desc->affinity_notify = NULL;
1838#endif
1839
Uwe Kleine König83ac4ca2018-03-19 11:52:02 +01001840 action = __free_irq(desc, dev_id);
Alexandru Moise2827a412017-09-19 22:04:12 +02001841
1842 if (!action)
1843 return NULL;
1844
Christoph Hellwig25ce4be2017-04-13 09:06:41 +02001845 devname = action->name;
1846 kfree(action);
1847 return devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849EXPORT_SYMBOL(free_irq);
1850
Julien Thierryb5259032019-01-31 14:53:58 +00001851/* This function must be called with desc->lock held */
1852static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc)
1853{
1854 const char *devname = NULL;
1855
1856 desc->istate &= ~IRQS_NMI;
1857
1858 if (!WARN_ON(desc->action == NULL)) {
1859 irq_pm_remove_action(desc, desc->action);
1860 devname = desc->action->name;
1861 unregister_handler_proc(irq, desc->action);
1862
1863 kfree(desc->action);
1864 desc->action = NULL;
1865 }
1866
1867 irq_settings_clr_disable_unlazy(desc);
Thomas Gleixner4001d8e2019-06-28 13:11:49 +02001868 irq_shutdown_and_deactivate(desc);
Julien Thierryb5259032019-01-31 14:53:58 +00001869
1870 irq_release_resources(desc);
1871
1872 irq_chip_pm_put(&desc->irq_data);
1873 module_put(desc->owner);
1874
1875 return devname;
1876}
1877
1878const void *free_nmi(unsigned int irq, void *dev_id)
1879{
1880 struct irq_desc *desc = irq_to_desc(irq);
1881 unsigned long flags;
1882 const void *devname;
1883
1884 if (!desc || WARN_ON(!(desc->istate & IRQS_NMI)))
1885 return NULL;
1886
1887 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1888 return NULL;
1889
1890 /* NMI still enabled */
1891 if (WARN_ON(desc->depth == 0))
1892 disable_nmi_nosync(irq);
1893
1894 raw_spin_lock_irqsave(&desc->lock, flags);
1895
1896 irq_nmi_teardown(desc);
1897 devname = __cleanup_nmi(irq, desc);
1898
1899 raw_spin_unlock_irqrestore(&desc->lock, flags);
1900
1901 return devname;
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/**
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001905 * request_threaded_irq - allocate an interrupt line
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 * @irq: Interrupt line to allocate
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001907 * @handler: Function to be called when the IRQ occurs.
1908 * Primary handler for threaded interrupts
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001909 * If NULL and thread_fn != NULL the default
1910 * primary handler is installed
Thomas Gleixnerf48fe812009-03-24 11:46:22 +01001911 * @thread_fn: Function called from the irq handler thread
1912 * If NULL, no irq thread is created
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 * @irqflags: Interrupt type flags
1914 * @devname: An ascii name for the claiming device
1915 * @dev_id: A cookie passed back to the handler function
1916 *
1917 * This call allocates interrupt resources and enables the
1918 * interrupt line and IRQ handling. From the point this
1919 * call is made your handler function may be invoked. Since
1920 * your handler function must clear any interrupt the board
1921 * raises, you must take care both to initialise your hardware
1922 * and to set up the interrupt handler in the right order.
1923 *
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001924 * If you want to set up a threaded irq handler for your device
Javi Merino6d21af42011-10-26 10:16:11 +01001925 * then you need to supply @handler and @thread_fn. @handler is
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001926 * still called in hard interrupt context and has to check
1927 * whether the interrupt originates from the device. If yes it
1928 * needs to disable the interrupt on the device and return
Steven Rostedt39a2edd2009-05-12 14:35:54 -04001929 * IRQ_WAKE_THREAD which will wake up the handler thread and run
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001930 * @thread_fn. This split handler design is necessary to support
1931 * shared interrupts.
1932 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 * Dev_id must be globally unique. Normally the address of the
1934 * device data structure is used as the cookie. Since the handler
1935 * receives this value it makes sense to use it.
1936 *
1937 * If your interrupt is shared you must pass a non NULL dev_id
1938 * as this is required when freeing the interrupt.
1939 *
1940 * Flags:
1941 *
Thomas Gleixner3cca53b2006-07-01 19:29:31 -07001942 * IRQF_SHARED Interrupt is shared
David Brownell0c5d1eb2008-10-01 14:46:18 -07001943 * IRQF_TRIGGER_* Specify active edge(s) or level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 *
1945 */
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001946int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1947 irq_handler_t thread_fn, unsigned long irqflags,
1948 const char *devname, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Ingo Molnar06fcb0c2006-06-29 02:24:40 -07001950 struct irqaction *action;
Yinghai Lu08678b02008-08-19 20:50:05 -07001951 struct irq_desc *desc;
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02001952 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Chen Fane237a552016-02-15 12:52:01 +08001954 if (irq == IRQ_NOTCONNECTED)
1955 return -ENOTCONN;
1956
David Brownell470c6622008-12-01 14:31:37 -08001957 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 * Sanity-check: shared interrupts must pass in a real dev-ID,
1959 * otherwise we'll have trouble later trying to figure out
1960 * which interrupt is which (messes up the interrupt freeing
1961 * logic etc).
Rafael J. Wysocki17f48032015-02-27 00:07:55 +01001962 *
1963 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1964 * it cannot be set along with IRQF_NO_SUSPEND.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
Rafael J. Wysocki17f48032015-02-27 00:07:55 +01001966 if (((irqflags & IRQF_SHARED) && !dev_id) ||
1967 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1968 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001970
Yinghai Lucb5bc832008-08-19 20:50:17 -07001971 desc = irq_to_desc(irq);
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001972 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return -EINVAL;
Yinghai Lu7d94f7c2008-08-19 20:50:14 -07001974
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01001975 if (!irq_settings_can_request(desc) ||
1976 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
Thomas Gleixner6550c772006-06-29 02:24:49 -07001977 return -EINVAL;
Thomas Gleixnerb25c3402009-08-13 12:17:22 +02001978
1979 if (!handler) {
1980 if (!thread_fn)
1981 return -EINVAL;
1982 handler = irq_default_primary_handler;
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Thomas Gleixner45535732009-02-22 23:00:32 +01001985 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (!action)
1987 return -ENOMEM;
1988
1989 action->handler = handler;
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01001990 action->thread_fn = thread_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 action->flags = irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 action->name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 action->dev_id = dev_id;
1994
Jon Hunterbe45beb2016-06-07 16:12:29 +01001995 retval = irq_chip_pm_get(&desc->irq_data);
Shawn Lin4396f462016-08-22 16:21:52 +08001996 if (retval < 0) {
1997 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01001998 return retval;
Shawn Lin4396f462016-08-22 16:21:52 +08001999 }
Jon Hunterbe45beb2016-06-07 16:12:29 +01002000
Thomas Gleixnerd3c60042008-10-16 09:55:00 +02002001 retval = __setup_irq(irq, desc, action);
Thomas Gleixner70aedd22009-08-13 12:17:48 +02002002
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02002003 if (retval) {
Jon Hunterbe45beb2016-06-07 16:12:29 +01002004 irq_chip_pm_put(&desc->irq_data);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02002005 kfree(action->secondary);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04002006 kfree(action);
Thomas Gleixner2a1d3ab2015-09-21 11:01:10 +02002007 }
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04002008
Thomas Gleixner6d83f942011-02-18 23:27:23 +01002009#ifdef CONFIG_DEBUG_SHIRQ_FIXME
Luis Henriques6ce51c42009-04-01 18:06:35 +01002010 if (!retval && (irqflags & IRQF_SHARED)) {
David Woodhousea304e1b2007-02-12 00:52:00 -08002011 /*
2012 * It's a shared IRQ -- the driver ought to be prepared for it
2013 * to happen immediately, so let's make sure....
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04002014 * We disable the irq to make sure that a 'real' IRQ doesn't
2015 * run in parallel with our fake.
David Woodhousea304e1b2007-02-12 00:52:00 -08002016 */
Jarek Poplawski59845b12007-08-30 23:56:34 -07002017 unsigned long flags;
David Woodhousea304e1b2007-02-12 00:52:00 -08002018
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04002019 disable_irq(irq);
Jarek Poplawski59845b12007-08-30 23:56:34 -07002020 local_irq_save(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04002021
Jarek Poplawski59845b12007-08-30 23:56:34 -07002022 handler(irq, dev_id);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04002023
Jarek Poplawski59845b12007-08-30 23:56:34 -07002024 local_irq_restore(flags);
Anton Vorontsov377bf1e2008-08-21 22:58:28 +04002025 enable_irq(irq);
David Woodhousea304e1b2007-02-12 00:52:00 -08002026 }
2027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return retval;
2029}
Thomas Gleixner3aa551c2009-03-23 18:28:15 +01002030EXPORT_SYMBOL(request_threaded_irq);
Marc Zyngierae731f82010-03-15 22:56:33 +00002031
2032/**
2033 * request_any_context_irq - allocate an interrupt line
2034 * @irq: Interrupt line to allocate
2035 * @handler: Function to be called when the IRQ occurs.
2036 * Threaded handler for threaded interrupts.
2037 * @flags: Interrupt type flags
2038 * @name: An ascii name for the claiming device
2039 * @dev_id: A cookie passed back to the handler function
2040 *
2041 * This call allocates interrupt resources and enables the
2042 * interrupt line and IRQ handling. It selects either a
2043 * hardirq or threaded handling method depending on the
2044 * context.
2045 *
2046 * On failure, it returns a negative value. On success,
2047 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
2048 */
2049int request_any_context_irq(unsigned int irq, irq_handler_t handler,
2050 unsigned long flags, const char *name, void *dev_id)
2051{
Chen Fane237a552016-02-15 12:52:01 +08002052 struct irq_desc *desc;
Marc Zyngierae731f82010-03-15 22:56:33 +00002053 int ret;
2054
Chen Fane237a552016-02-15 12:52:01 +08002055 if (irq == IRQ_NOTCONNECTED)
2056 return -ENOTCONN;
2057
2058 desc = irq_to_desc(irq);
Marc Zyngierae731f82010-03-15 22:56:33 +00002059 if (!desc)
2060 return -EINVAL;
2061
Thomas Gleixner1ccb4e62011-02-09 14:44:17 +01002062 if (irq_settings_is_nested_thread(desc)) {
Marc Zyngierae731f82010-03-15 22:56:33 +00002063 ret = request_threaded_irq(irq, NULL, handler,
2064 flags, name, dev_id);
2065 return !ret ? IRQC_IS_NESTED : ret;
2066 }
2067
2068 ret = request_irq(irq, handler, flags, name, dev_id);
2069 return !ret ? IRQC_IS_HARDIRQ : ret;
2070}
2071EXPORT_SYMBOL_GPL(request_any_context_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002072
Julien Thierryb5259032019-01-31 14:53:58 +00002073/**
2074 * request_nmi - allocate an interrupt line for NMI delivery
2075 * @irq: Interrupt line to allocate
2076 * @handler: Function to be called when the IRQ occurs.
2077 * Threaded handler for threaded interrupts.
2078 * @irqflags: Interrupt type flags
2079 * @name: An ascii name for the claiming device
2080 * @dev_id: A cookie passed back to the handler function
2081 *
2082 * This call allocates interrupt resources and enables the
2083 * interrupt line and IRQ handling. It sets up the IRQ line
2084 * to be handled as an NMI.
2085 *
2086 * An interrupt line delivering NMIs cannot be shared and IRQ handling
2087 * cannot be threaded.
2088 *
2089 * Interrupt lines requested for NMI delivering must produce per cpu
2090 * interrupts and have auto enabling setting disabled.
2091 *
2092 * Dev_id must be globally unique. Normally the address of the
2093 * device data structure is used as the cookie. Since the handler
2094 * receives this value it makes sense to use it.
2095 *
2096 * If the interrupt line cannot be used to deliver NMIs, function
2097 * will fail and return a negative value.
2098 */
2099int request_nmi(unsigned int irq, irq_handler_t handler,
2100 unsigned long irqflags, const char *name, void *dev_id)
2101{
2102 struct irqaction *action;
2103 struct irq_desc *desc;
2104 unsigned long flags;
2105 int retval;
2106
2107 if (irq == IRQ_NOTCONNECTED)
2108 return -ENOTCONN;
2109
2110 /* NMI cannot be shared, used for Polling */
2111 if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
2112 return -EINVAL;
2113
2114 if (!(irqflags & IRQF_PERCPU))
2115 return -EINVAL;
2116
2117 if (!handler)
2118 return -EINVAL;
2119
2120 desc = irq_to_desc(irq);
2121
2122 if (!desc || irq_settings_can_autoenable(desc) ||
2123 !irq_settings_can_request(desc) ||
2124 WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
2125 !irq_supports_nmi(desc))
2126 return -EINVAL;
2127
2128 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2129 if (!action)
2130 return -ENOMEM;
2131
2132 action->handler = handler;
2133 action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING;
2134 action->name = name;
2135 action->dev_id = dev_id;
2136
2137 retval = irq_chip_pm_get(&desc->irq_data);
2138 if (retval < 0)
2139 goto err_out;
2140
2141 retval = __setup_irq(irq, desc, action);
2142 if (retval)
2143 goto err_irq_setup;
2144
2145 raw_spin_lock_irqsave(&desc->lock, flags);
2146
2147 /* Setup NMI state */
2148 desc->istate |= IRQS_NMI;
2149 retval = irq_nmi_setup(desc);
2150 if (retval) {
2151 __cleanup_nmi(irq, desc);
2152 raw_spin_unlock_irqrestore(&desc->lock, flags);
2153 return -EINVAL;
2154 }
2155
2156 raw_spin_unlock_irqrestore(&desc->lock, flags);
2157
2158 return 0;
2159
2160err_irq_setup:
2161 irq_chip_pm_put(&desc->irq_data);
2162err_out:
2163 kfree(action);
2164
2165 return retval;
2166}
2167
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01002168void enable_percpu_irq(unsigned int irq, unsigned int type)
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002169{
2170 unsigned int cpu = smp_processor_id();
2171 unsigned long flags;
2172 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2173
2174 if (!desc)
2175 return;
2176
Marc Zyngierf35ad082016-06-13 10:39:44 +01002177 /*
2178 * If the trigger type is not specified by the caller, then
2179 * use the default for this interrupt.
2180 */
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01002181 type &= IRQ_TYPE_SENSE_MASK;
Marc Zyngierf35ad082016-06-13 10:39:44 +01002182 if (type == IRQ_TYPE_NONE)
2183 type = irqd_get_trigger_type(&desc->irq_data);
2184
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01002185 if (type != IRQ_TYPE_NONE) {
2186 int ret;
2187
Jiang Liua1ff5412015-06-23 19:47:29 +02002188 ret = __irq_set_trigger(desc, type);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01002189
2190 if (ret) {
Thomas Gleixner32cffdd2011-10-04 18:43:57 +02002191 WARN(1, "failed to set type for IRQ%d\n", irq);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01002192 goto out;
2193 }
2194 }
2195
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002196 irq_percpu_enable(desc, cpu);
Marc Zyngier1e7c5fd2011-09-30 10:48:47 +01002197out:
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002198 irq_put_desc_unlock(desc, flags);
2199}
Chris Metcalf36a5df82013-02-01 15:04:26 -05002200EXPORT_SYMBOL_GPL(enable_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002201
Julien Thierry4b078c32019-01-31 14:53:59 +00002202void enable_percpu_nmi(unsigned int irq, unsigned int type)
2203{
2204 enable_percpu_irq(irq, type);
2205}
2206
Thomas Petazzonif0cb3222015-10-20 15:23:51 +02002207/**
2208 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
2209 * @irq: Linux irq number to check for
2210 *
2211 * Must be called from a non migratable context. Returns the enable
2212 * state of a per cpu interrupt on the current cpu.
2213 */
2214bool irq_percpu_is_enabled(unsigned int irq)
2215{
2216 unsigned int cpu = smp_processor_id();
2217 struct irq_desc *desc;
2218 unsigned long flags;
2219 bool is_enabled;
2220
2221 desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2222 if (!desc)
2223 return false;
2224
2225 is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
2226 irq_put_desc_unlock(desc, flags);
2227
2228 return is_enabled;
2229}
2230EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
2231
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002232void disable_percpu_irq(unsigned int irq)
2233{
2234 unsigned int cpu = smp_processor_id();
2235 unsigned long flags;
2236 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2237
2238 if (!desc)
2239 return;
2240
2241 irq_percpu_disable(desc, cpu);
2242 irq_put_desc_unlock(desc, flags);
2243}
Chris Metcalf36a5df82013-02-01 15:04:26 -05002244EXPORT_SYMBOL_GPL(disable_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002245
Julien Thierry4b078c32019-01-31 14:53:59 +00002246void disable_percpu_nmi(unsigned int irq)
2247{
2248 disable_percpu_irq(irq);
2249}
2250
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002251/*
2252 * Internal function to unregister a percpu irqaction.
2253 */
2254static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2255{
2256 struct irq_desc *desc = irq_to_desc(irq);
2257 struct irqaction *action;
2258 unsigned long flags;
2259
2260 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
2261
2262 if (!desc)
2263 return NULL;
2264
2265 raw_spin_lock_irqsave(&desc->lock, flags);
2266
2267 action = desc->action;
2268 if (!action || action->percpu_dev_id != dev_id) {
2269 WARN(1, "Trying to free already-free IRQ %d\n", irq);
2270 goto bad;
2271 }
2272
2273 if (!cpumask_empty(desc->percpu_enabled)) {
2274 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
2275 irq, cpumask_first(desc->percpu_enabled));
2276 goto bad;
2277 }
2278
2279 /* Found it - now remove it from the list of entries: */
2280 desc->action = NULL;
2281
Julien Thierry4b078c32019-01-31 14:53:59 +00002282 desc->istate &= ~IRQS_NMI;
2283
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002284 raw_spin_unlock_irqrestore(&desc->lock, flags);
2285
2286 unregister_handler_proc(irq, action);
2287
Jon Hunterbe45beb2016-06-07 16:12:29 +01002288 irq_chip_pm_put(&desc->irq_data);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002289 module_put(desc->owner);
2290 return action;
2291
2292bad:
2293 raw_spin_unlock_irqrestore(&desc->lock, flags);
2294 return NULL;
2295}
2296
2297/**
2298 * remove_percpu_irq - free a per-cpu interrupt
2299 * @irq: Interrupt line to free
2300 * @act: irqaction for the interrupt
2301 *
2302 * Used to remove interrupts statically setup by the early boot process.
2303 */
2304void remove_percpu_irq(unsigned int irq, struct irqaction *act)
2305{
2306 struct irq_desc *desc = irq_to_desc(irq);
2307
2308 if (desc && irq_settings_is_per_cpu_devid(desc))
2309 __free_percpu_irq(irq, act->percpu_dev_id);
2310}
2311
2312/**
2313 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
2314 * @irq: Interrupt line to free
2315 * @dev_id: Device identity to free
2316 *
2317 * Remove a percpu interrupt handler. The handler is removed, but
2318 * the interrupt line is not disabled. This must be done on each
2319 * CPU before calling this function. The function does not return
2320 * until any executing interrupts for this IRQ have completed.
2321 *
2322 * This function must not be called from interrupt context.
2323 */
2324void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2325{
2326 struct irq_desc *desc = irq_to_desc(irq);
2327
2328 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2329 return;
2330
2331 chip_bus_lock(desc);
2332 kfree(__free_percpu_irq(irq, dev_id));
2333 chip_bus_sync_unlock(desc);
2334}
Maxime Ripardaec2e2a2015-09-25 18:09:33 +02002335EXPORT_SYMBOL_GPL(free_percpu_irq);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002336
Julien Thierry4b078c32019-01-31 14:53:59 +00002337void free_percpu_nmi(unsigned int irq, void __percpu *dev_id)
2338{
2339 struct irq_desc *desc = irq_to_desc(irq);
2340
2341 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2342 return;
2343
2344 if (WARN_ON(!(desc->istate & IRQS_NMI)))
2345 return;
2346
2347 kfree(__free_percpu_irq(irq, dev_id));
2348}
2349
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002350/**
2351 * setup_percpu_irq - setup a per-cpu interrupt
2352 * @irq: Interrupt line to setup
2353 * @act: irqaction for the interrupt
2354 *
2355 * Used to statically setup per-cpu interrupts in the early boot process.
2356 */
2357int setup_percpu_irq(unsigned int irq, struct irqaction *act)
2358{
2359 struct irq_desc *desc = irq_to_desc(irq);
2360 int retval;
2361
2362 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2363 return -EINVAL;
Jon Hunterbe45beb2016-06-07 16:12:29 +01002364
2365 retval = irq_chip_pm_get(&desc->irq_data);
2366 if (retval < 0)
2367 return retval;
2368
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002369 retval = __setup_irq(irq, desc, act);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002370
Jon Hunterbe45beb2016-06-07 16:12:29 +01002371 if (retval)
2372 irq_chip_pm_put(&desc->irq_data);
2373
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002374 return retval;
2375}
2376
2377/**
Daniel Lezcanoc80081b2017-07-06 14:29:04 +02002378 * __request_percpu_irq - allocate a percpu interrupt line
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002379 * @irq: Interrupt line to allocate
2380 * @handler: Function to be called when the IRQ occurs.
Daniel Lezcanoc80081b2017-07-06 14:29:04 +02002381 * @flags: Interrupt type flags (IRQF_TIMER only)
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002382 * @devname: An ascii name for the claiming device
2383 * @dev_id: A percpu cookie passed back to the handler function
2384 *
Maxime Riparda1b7feb2015-09-25 18:09:32 +02002385 * This call allocates interrupt resources and enables the
2386 * interrupt on the local CPU. If the interrupt is supposed to be
2387 * enabled on other CPUs, it has to be done on each CPU using
2388 * enable_percpu_irq().
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002389 *
2390 * Dev_id must be globally unique. It is a per-cpu variable, and
2391 * the handler gets called with the interrupted CPU's instance of
2392 * that variable.
2393 */
Daniel Lezcanoc80081b2017-07-06 14:29:04 +02002394int __request_percpu_irq(unsigned int irq, irq_handler_t handler,
2395 unsigned long flags, const char *devname,
2396 void __percpu *dev_id)
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002397{
2398 struct irqaction *action;
2399 struct irq_desc *desc;
2400 int retval;
2401
2402 if (!dev_id)
2403 return -EINVAL;
2404
2405 desc = irq_to_desc(irq);
2406 if (!desc || !irq_settings_can_request(desc) ||
2407 !irq_settings_is_per_cpu_devid(desc))
2408 return -EINVAL;
2409
Daniel Lezcanoc80081b2017-07-06 14:29:04 +02002410 if (flags && flags != IRQF_TIMER)
2411 return -EINVAL;
2412
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002413 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2414 if (!action)
2415 return -ENOMEM;
2416
2417 action->handler = handler;
Daniel Lezcanoc80081b2017-07-06 14:29:04 +02002418 action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND;
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002419 action->name = devname;
2420 action->percpu_dev_id = dev_id;
2421
Jon Hunterbe45beb2016-06-07 16:12:29 +01002422 retval = irq_chip_pm_get(&desc->irq_data);
Shawn Lin4396f462016-08-22 16:21:52 +08002423 if (retval < 0) {
2424 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01002425 return retval;
Shawn Lin4396f462016-08-22 16:21:52 +08002426 }
Jon Hunterbe45beb2016-06-07 16:12:29 +01002427
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002428 retval = __setup_irq(irq, desc, action);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002429
Jon Hunterbe45beb2016-06-07 16:12:29 +01002430 if (retval) {
2431 irq_chip_pm_put(&desc->irq_data);
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002432 kfree(action);
Jon Hunterbe45beb2016-06-07 16:12:29 +01002433 }
Marc Zyngier31d9d9b2011-09-23 17:03:06 +01002434
2435 return retval;
2436}
Daniel Lezcanoc80081b2017-07-06 14:29:04 +02002437EXPORT_SYMBOL_GPL(__request_percpu_irq);
Marc Zyngier1b7047e2015-03-18 11:01:22 +00002438
2439/**
Julien Thierry4b078c32019-01-31 14:53:59 +00002440 * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery
2441 * @irq: Interrupt line to allocate
2442 * @handler: Function to be called when the IRQ occurs.
2443 * @name: An ascii name for the claiming device
2444 * @dev_id: A percpu cookie passed back to the handler function
2445 *
2446 * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
Julien Thierrya5186692019-02-13 10:09:19 +00002447 * have to be setup on each CPU by calling prepare_percpu_nmi() before
2448 * being enabled on the same CPU by using enable_percpu_nmi().
Julien Thierry4b078c32019-01-31 14:53:59 +00002449 *
2450 * Dev_id must be globally unique. It is a per-cpu variable, and
2451 * the handler gets called with the interrupted CPU's instance of
2452 * that variable.
2453 *
2454 * Interrupt lines requested for NMI delivering should have auto enabling
2455 * setting disabled.
2456 *
2457 * If the interrupt line cannot be used to deliver NMIs, function
2458 * will fail returning a negative value.
2459 */
2460int request_percpu_nmi(unsigned int irq, irq_handler_t handler,
2461 const char *name, void __percpu *dev_id)
2462{
2463 struct irqaction *action;
2464 struct irq_desc *desc;
2465 unsigned long flags;
2466 int retval;
2467
2468 if (!handler)
2469 return -EINVAL;
2470
2471 desc = irq_to_desc(irq);
2472
2473 if (!desc || !irq_settings_can_request(desc) ||
2474 !irq_settings_is_per_cpu_devid(desc) ||
2475 irq_settings_can_autoenable(desc) ||
2476 !irq_supports_nmi(desc))
2477 return -EINVAL;
2478
2479 /* The line cannot already be NMI */
2480 if (desc->istate & IRQS_NMI)
2481 return -EINVAL;
2482
2483 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2484 if (!action)
2485 return -ENOMEM;
2486
2487 action->handler = handler;
2488 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND | IRQF_NO_THREAD
2489 | IRQF_NOBALANCING;
2490 action->name = name;
2491 action->percpu_dev_id = dev_id;
2492
2493 retval = irq_chip_pm_get(&desc->irq_data);
2494 if (retval < 0)
2495 goto err_out;
2496
2497 retval = __setup_irq(irq, desc, action);
2498 if (retval)
2499 goto err_irq_setup;
2500
2501 raw_spin_lock_irqsave(&desc->lock, flags);
2502 desc->istate |= IRQS_NMI;
2503 raw_spin_unlock_irqrestore(&desc->lock, flags);
2504
2505 return 0;
2506
2507err_irq_setup:
2508 irq_chip_pm_put(&desc->irq_data);
2509err_out:
2510 kfree(action);
2511
2512 return retval;
2513}
2514
2515/**
2516 * prepare_percpu_nmi - performs CPU local setup for NMI delivery
2517 * @irq: Interrupt line to prepare for NMI delivery
2518 *
2519 * This call prepares an interrupt line to deliver NMI on the current CPU,
2520 * before that interrupt line gets enabled with enable_percpu_nmi().
2521 *
2522 * As a CPU local operation, this should be called from non-preemptible
2523 * context.
2524 *
2525 * If the interrupt line cannot be used to deliver NMIs, function
2526 * will fail returning a negative value.
2527 */
2528int prepare_percpu_nmi(unsigned int irq)
2529{
2530 unsigned long flags;
2531 struct irq_desc *desc;
2532 int ret = 0;
2533
2534 WARN_ON(preemptible());
2535
2536 desc = irq_get_desc_lock(irq, &flags,
2537 IRQ_GET_DESC_CHECK_PERCPU);
2538 if (!desc)
2539 return -EINVAL;
2540
2541 if (WARN(!(desc->istate & IRQS_NMI),
2542 KERN_ERR "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n",
2543 irq)) {
2544 ret = -EINVAL;
2545 goto out;
2546 }
2547
2548 ret = irq_nmi_setup(desc);
2549 if (ret) {
2550 pr_err("Failed to setup NMI delivery: irq %u\n", irq);
2551 goto out;
2552 }
2553
2554out:
2555 irq_put_desc_unlock(desc, flags);
2556 return ret;
2557}
2558
2559/**
2560 * teardown_percpu_nmi - undoes NMI setup of IRQ line
2561 * @irq: Interrupt line from which CPU local NMI configuration should be
2562 * removed
2563 *
2564 * This call undoes the setup done by prepare_percpu_nmi().
2565 *
2566 * IRQ line should not be enabled for the current CPU.
2567 *
2568 * As a CPU local operation, this should be called from non-preemptible
2569 * context.
2570 */
2571void teardown_percpu_nmi(unsigned int irq)
2572{
2573 unsigned long flags;
2574 struct irq_desc *desc;
2575
2576 WARN_ON(preemptible());
2577
2578 desc = irq_get_desc_lock(irq, &flags,
2579 IRQ_GET_DESC_CHECK_PERCPU);
2580 if (!desc)
2581 return;
2582
2583 if (WARN_ON(!(desc->istate & IRQS_NMI)))
2584 goto out;
2585
2586 irq_nmi_teardown(desc);
2587out:
2588 irq_put_desc_unlock(desc, flags);
2589}
2590
2591/**
Marc Zyngier1b7047e2015-03-18 11:01:22 +00002592 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2593 * @irq: Interrupt line that is forwarded to a VM
2594 * @which: One of IRQCHIP_STATE_* the caller wants to know about
2595 * @state: a pointer to a boolean where the state is to be storeed
2596 *
2597 * This call snapshots the internal irqchip state of an
2598 * interrupt, returning into @state the bit corresponding to
2599 * stage @which
2600 *
2601 * This function should be called with preemption disabled if the
2602 * interrupt controller has per-cpu registers.
2603 */
2604int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2605 bool *state)
2606{
2607 struct irq_desc *desc;
2608 struct irq_data *data;
2609 struct irq_chip *chip;
2610 unsigned long flags;
2611 int err = -EINVAL;
2612
2613 desc = irq_get_desc_buslock(irq, &flags, 0);
2614 if (!desc)
2615 return err;
2616
2617 data = irq_desc_get_irq_data(desc);
2618
2619 do {
2620 chip = irq_data_get_irq_chip(data);
2621 if (chip->irq_get_irqchip_state)
2622 break;
2623#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2624 data = data->parent_data;
2625#else
2626 data = NULL;
2627#endif
2628 } while (data);
2629
2630 if (data)
2631 err = chip->irq_get_irqchip_state(data, which, state);
2632
2633 irq_put_desc_busunlock(desc, flags);
2634 return err;
2635}
Bjorn Andersson1ee4fb32015-07-22 12:43:04 -07002636EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
Marc Zyngier1b7047e2015-03-18 11:01:22 +00002637
2638/**
2639 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2640 * @irq: Interrupt line that is forwarded to a VM
2641 * @which: State to be restored (one of IRQCHIP_STATE_*)
2642 * @val: Value corresponding to @which
2643 *
2644 * This call sets the internal irqchip state of an interrupt,
2645 * depending on the value of @which.
2646 *
2647 * This function should be called with preemption disabled if the
2648 * interrupt controller has per-cpu registers.
2649 */
2650int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2651 bool val)
2652{
2653 struct irq_desc *desc;
2654 struct irq_data *data;
2655 struct irq_chip *chip;
2656 unsigned long flags;
2657 int err = -EINVAL;
2658
2659 desc = irq_get_desc_buslock(irq, &flags, 0);
2660 if (!desc)
2661 return err;
2662
2663 data = irq_desc_get_irq_data(desc);
2664
2665 do {
2666 chip = irq_data_get_irq_chip(data);
2667 if (chip->irq_set_irqchip_state)
2668 break;
2669#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2670 data = data->parent_data;
2671#else
2672 data = NULL;
2673#endif
2674 } while (data);
2675
2676 if (data)
2677 err = chip->irq_set_irqchip_state(data, which, val);
2678
2679 irq_put_desc_busunlock(desc, flags);
2680 return err;
2681}
Bjorn Andersson1ee4fb32015-07-22 12:43:04 -07002682EXPORT_SYMBOL_GPL(irq_set_irqchip_state);