blob: 918fe0593386219c9e3449bf82f391086c2fd3b5 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
4 * Copyright (C) 2005-2006 Thomas Gleixner
5 *
6 * This file contains driver APIs to the irq subsystem.
7 */
8
9#define pr_fmt(fmt) "genirq: " fmt
10
11#include <linux/irq.h>
12#include <linux/kthread.h>
13#include <linux/module.h>
14#include <linux/random.h>
15#include <linux/interrupt.h>
David Brazdil0f672f62019-12-10 10:32:29 +000016#include <linux/irqdomain.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017#include <linux/slab.h>
18#include <linux/sched.h>
19#include <linux/sched/rt.h>
20#include <linux/sched/task.h>
21#include <uapi/linux/sched/types.h>
22#include <linux/task_work.h>
23
24#include "internals.h"
25
David Brazdil0f672f62019-12-10 10:32:29 +000026#if defined(CONFIG_IRQ_FORCED_THREADING) && !defined(CONFIG_PREEMPT_RT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027__read_mostly bool force_irqthreads;
28EXPORT_SYMBOL_GPL(force_irqthreads);
29
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
David Brazdil0f672f62019-12-10 10:32:29 +000038static void __synchronize_hardirq(struct irq_desc *desc, bool sync_chip)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039{
David Brazdil0f672f62019-12-10 10:32:29 +000040 struct irq_data *irqd = irq_desc_get_irq_data(desc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041 bool inprogress;
42
43 do {
44 unsigned long flags;
45
46 /*
47 * Wait until we're out of the critical section. This might
48 * give the wrong answer due to the lack of memory barriers.
49 */
50 while (irqd_irq_inprogress(&desc->irq_data))
51 cpu_relax();
52
53 /* Ok, that indicated we're done: double-check carefully. */
54 raw_spin_lock_irqsave(&desc->lock, flags);
55 inprogress = irqd_irq_inprogress(&desc->irq_data);
David Brazdil0f672f62019-12-10 10:32:29 +000056
57 /*
58 * If requested and supported, check at the chip whether it
59 * is in flight at the hardware level, i.e. already pending
60 * in a CPU and waiting for service and acknowledge.
61 */
62 if (!inprogress && sync_chip) {
63 /*
64 * Ignore the return code. inprogress is only updated
65 * when the chip supports it.
66 */
67 __irq_get_irqchip_state(irqd, IRQCHIP_STATE_ACTIVE,
68 &inprogress);
69 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 raw_spin_unlock_irqrestore(&desc->lock, flags);
71
72 /* Oops, that failed? */
73 } while (inprogress);
74}
75
76/**
77 * synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs)
78 * @irq: interrupt number to wait for
79 *
80 * This function waits for any pending hard IRQ handlers for this
81 * interrupt to complete before returning. If you use this
82 * function while holding a resource the IRQ handler may need you
83 * will deadlock. It does not take associated threaded handlers
84 * into account.
85 *
86 * Do not use this for shutdown scenarios where you must be sure
87 * that all parts (hardirq and threaded handler) have completed.
88 *
89 * Returns: false if a threaded handler is active.
90 *
91 * This function may be called - with care - from IRQ context.
David Brazdil0f672f62019-12-10 10:32:29 +000092 *
93 * It does not check whether there is an interrupt in flight at the
94 * hardware level, but not serviced yet, as this might deadlock when
95 * called with interrupts disabled and the target CPU of the interrupt
96 * is the current CPU.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097 */
98bool synchronize_hardirq(unsigned int irq)
99{
100 struct irq_desc *desc = irq_to_desc(irq);
101
102 if (desc) {
David Brazdil0f672f62019-12-10 10:32:29 +0000103 __synchronize_hardirq(desc, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104 return !atomic_read(&desc->threads_active);
105 }
106
107 return true;
108}
109EXPORT_SYMBOL(synchronize_hardirq);
110
111/**
112 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
113 * @irq: interrupt number to wait for
114 *
115 * This function waits for any pending IRQ handlers for this interrupt
116 * to complete before returning. If you use this function while
117 * holding a resource the IRQ handler may need you will deadlock.
118 *
David Brazdil0f672f62019-12-10 10:32:29 +0000119 * Can only be called from preemptible code as it might sleep when
120 * an interrupt thread is associated to @irq.
121 *
122 * It optionally makes sure (when the irq chip supports that method)
123 * that the interrupt is not pending in any CPU and waiting for
124 * service.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 */
126void synchronize_irq(unsigned int irq)
127{
128 struct irq_desc *desc = irq_to_desc(irq);
129
130 if (desc) {
David Brazdil0f672f62019-12-10 10:32:29 +0000131 __synchronize_hardirq(desc, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 /*
133 * We made sure that no hardirq handler is
134 * running. Now verify that no threaded handlers are
135 * active.
136 */
137 wait_event(desc->wait_for_threads,
138 !atomic_read(&desc->threads_active));
139 }
140}
141EXPORT_SYMBOL(synchronize_irq);
142
143#ifdef CONFIG_SMP
144cpumask_var_t irq_default_affinity;
145
146static bool __irq_can_set_affinity(struct irq_desc *desc)
147{
148 if (!desc || !irqd_can_balance(&desc->irq_data) ||
149 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
150 return false;
151 return true;
152}
153
154/**
155 * irq_can_set_affinity - Check if the affinity of a given irq can be set
156 * @irq: Interrupt to check
157 *
158 */
159int irq_can_set_affinity(unsigned int irq)
160{
161 return __irq_can_set_affinity(irq_to_desc(irq));
162}
163
164/**
165 * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space
166 * @irq: Interrupt to check
167 *
168 * Like irq_can_set_affinity() above, but additionally checks for the
169 * AFFINITY_MANAGED flag.
170 */
171bool irq_can_set_affinity_usr(unsigned int irq)
172{
173 struct irq_desc *desc = irq_to_desc(irq);
174
175 return __irq_can_set_affinity(desc) &&
176 !irqd_affinity_is_managed(&desc->irq_data);
177}
178
179/**
180 * irq_set_thread_affinity - Notify irq threads to adjust affinity
181 * @desc: irq descriptor which has affitnity changed
182 *
183 * We just set IRQTF_AFFINITY and delegate the affinity setting
184 * to the interrupt thread itself. We can not call
185 * set_cpus_allowed_ptr() here as we hold desc->lock and this
186 * code can be called from hard interrupt context.
187 */
188void irq_set_thread_affinity(struct irq_desc *desc)
189{
190 struct irqaction *action;
191
192 for_each_action_of_desc(desc, action)
193 if (action->thread)
194 set_bit(IRQTF_AFFINITY, &action->thread_flags);
195}
196
Olivier Deprez0e641232021-09-23 10:07:05 +0200197#ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198static void irq_validate_effective_affinity(struct irq_data *data)
199{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 const struct cpumask *m = irq_data_get_effective_affinity_mask(data);
201 struct irq_chip *chip = irq_data_get_irq_chip(data);
202
203 if (!cpumask_empty(m))
204 return;
205 pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n",
206 chip->name, data->irq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207}
208
Olivier Deprez0e641232021-09-23 10:07:05 +0200209static inline void irq_init_effective_affinity(struct irq_data *data,
210 const struct cpumask *mask)
211{
212 cpumask_copy(irq_data_get_effective_affinity_mask(data), mask);
213}
214#else
215static inline void irq_validate_effective_affinity(struct irq_data *data) { }
216static inline void irq_init_effective_affinity(struct irq_data *data,
217 const struct cpumask *mask) { }
218#endif
219
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
221 bool force)
222{
223 struct irq_desc *desc = irq_data_to_desc(data);
224 struct irq_chip *chip = irq_data_get_irq_chip(data);
225 int ret;
226
227 if (!chip || !chip->irq_set_affinity)
228 return -EINVAL;
229
230 ret = chip->irq_set_affinity(data, mask, force);
231 switch (ret) {
232 case IRQ_SET_MASK_OK:
233 case IRQ_SET_MASK_OK_DONE:
234 cpumask_copy(desc->irq_common_data.affinity, mask);
David Brazdil0f672f62019-12-10 10:32:29 +0000235 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236 case IRQ_SET_MASK_OK_NOCOPY:
237 irq_validate_effective_affinity(data);
238 irq_set_thread_affinity(desc);
239 ret = 0;
240 }
241
242 return ret;
243}
244
245#ifdef CONFIG_GENERIC_PENDING_IRQ
246static inline int irq_set_affinity_pending(struct irq_data *data,
247 const struct cpumask *dest)
248{
249 struct irq_desc *desc = irq_data_to_desc(data);
250
251 irqd_set_move_pending(data);
252 irq_copy_pending(desc, dest);
253 return 0;
254}
255#else
256static inline int irq_set_affinity_pending(struct irq_data *data,
257 const struct cpumask *dest)
258{
259 return -EBUSY;
260}
261#endif
262
263static int irq_try_set_affinity(struct irq_data *data,
264 const struct cpumask *dest, bool force)
265{
266 int ret = irq_do_set_affinity(data, dest, force);
267
268 /*
269 * In case that the underlying vector management is busy and the
270 * architecture supports the generic pending mechanism then utilize
271 * this to avoid returning an error to user space.
272 */
273 if (ret == -EBUSY && !force)
274 ret = irq_set_affinity_pending(data, dest);
275 return ret;
276}
277
Olivier Deprez0e641232021-09-23 10:07:05 +0200278static bool irq_set_affinity_deactivated(struct irq_data *data,
279 const struct cpumask *mask, bool force)
280{
281 struct irq_desc *desc = irq_data_to_desc(data);
282
283 /*
284 * Handle irq chips which can handle affinity only in activated
285 * state correctly
286 *
287 * If the interrupt is not yet activated, just store the affinity
288 * mask and do not call the chip driver at all. On activation the
289 * driver has to make sure anyway that the interrupt is in a
290 * useable state so startup works.
291 */
292 if (!IS_ENABLED(CONFIG_IRQ_DOMAIN_HIERARCHY) ||
293 irqd_is_activated(data) || !irqd_affinity_on_activate(data))
294 return false;
295
296 cpumask_copy(desc->irq_common_data.affinity, mask);
297 irq_init_effective_affinity(data, mask);
298 irqd_set(data, IRQD_AFFINITY_SET);
299 return true;
300}
301
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
303 bool force)
304{
305 struct irq_chip *chip = irq_data_get_irq_chip(data);
306 struct irq_desc *desc = irq_data_to_desc(data);
307 int ret = 0;
308
309 if (!chip || !chip->irq_set_affinity)
310 return -EINVAL;
311
Olivier Deprez0e641232021-09-23 10:07:05 +0200312 if (irq_set_affinity_deactivated(data, mask, force))
313 return 0;
314
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) {
316 ret = irq_try_set_affinity(data, mask, force);
317 } else {
318 irqd_set_move_pending(data);
319 irq_copy_pending(desc, mask);
320 }
321
322 if (desc->affinity_notify) {
323 kref_get(&desc->affinity_notify->kref);
Olivier Deprez0e641232021-09-23 10:07:05 +0200324 if (!schedule_work(&desc->affinity_notify->work)) {
325 /* Work was already scheduled, drop our extra ref */
326 kref_put(&desc->affinity_notify->kref,
327 desc->affinity_notify->release);
328 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329 }
330 irqd_set(data, IRQD_AFFINITY_SET);
331
332 return ret;
333}
334
335int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
336{
337 struct irq_desc *desc = irq_to_desc(irq);
338 unsigned long flags;
339 int ret;
340
341 if (!desc)
342 return -EINVAL;
343
344 raw_spin_lock_irqsave(&desc->lock, flags);
345 ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
346 raw_spin_unlock_irqrestore(&desc->lock, flags);
347 return ret;
348}
349
350int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
351{
352 unsigned long flags;
353 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
354
355 if (!desc)
356 return -EINVAL;
357 desc->affinity_hint = m;
358 irq_put_desc_unlock(desc, flags);
359 /* set the initial affinity to prevent every interrupt being on CPU0 */
360 if (m)
361 __irq_set_affinity(irq, m, false);
362 return 0;
363}
364EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
365
366static void irq_affinity_notify(struct work_struct *work)
367{
368 struct irq_affinity_notify *notify =
369 container_of(work, struct irq_affinity_notify, work);
370 struct irq_desc *desc = irq_to_desc(notify->irq);
371 cpumask_var_t cpumask;
372 unsigned long flags;
373
374 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
375 goto out;
376
377 raw_spin_lock_irqsave(&desc->lock, flags);
378 if (irq_move_pending(&desc->irq_data))
379 irq_get_pending(cpumask, desc);
380 else
381 cpumask_copy(cpumask, desc->irq_common_data.affinity);
382 raw_spin_unlock_irqrestore(&desc->lock, flags);
383
384 notify->notify(notify, cpumask);
385
386 free_cpumask_var(cpumask);
387out:
388 kref_put(&notify->kref, notify->release);
389}
390
391/**
392 * irq_set_affinity_notifier - control notification of IRQ affinity changes
393 * @irq: Interrupt for which to enable/disable notification
394 * @notify: Context for notification, or %NULL to disable
395 * notification. Function pointers must be initialised;
396 * the other fields will be initialised by this function.
397 *
398 * Must be called in process context. Notification may only be enabled
399 * after the IRQ is allocated and must be disabled before the IRQ is
400 * freed using free_irq().
401 */
402int
403irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
404{
405 struct irq_desc *desc = irq_to_desc(irq);
406 struct irq_affinity_notify *old_notify;
407 unsigned long flags;
408
409 /* The release function is promised process context */
410 might_sleep();
411
David Brazdil0f672f62019-12-10 10:32:29 +0000412 if (!desc || desc->istate & IRQS_NMI)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000413 return -EINVAL;
414
415 /* Complete initialisation of *notify */
416 if (notify) {
417 notify->irq = irq;
418 kref_init(&notify->kref);
419 INIT_WORK(&notify->work, irq_affinity_notify);
420 }
421
422 raw_spin_lock_irqsave(&desc->lock, flags);
423 old_notify = desc->affinity_notify;
424 desc->affinity_notify = notify;
425 raw_spin_unlock_irqrestore(&desc->lock, flags);
426
David Brazdil0f672f62019-12-10 10:32:29 +0000427 if (old_notify) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200428 if (cancel_work_sync(&old_notify->work)) {
429 /* Pending work had a ref, put that one too */
430 kref_put(&old_notify->kref, old_notify->release);
431 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000432 kref_put(&old_notify->kref, old_notify->release);
David Brazdil0f672f62019-12-10 10:32:29 +0000433 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000434
435 return 0;
436}
437EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
438
439#ifndef CONFIG_AUTO_IRQ_AFFINITY
440/*
441 * Generic version of the affinity autoselector.
442 */
443int irq_setup_affinity(struct irq_desc *desc)
444{
445 struct cpumask *set = irq_default_affinity;
446 int ret, node = irq_desc_get_node(desc);
447 static DEFINE_RAW_SPINLOCK(mask_lock);
448 static struct cpumask mask;
449
450 /* Excludes PER_CPU and NO_BALANCE interrupts */
451 if (!__irq_can_set_affinity(desc))
452 return 0;
453
454 raw_spin_lock(&mask_lock);
455 /*
456 * Preserve the managed affinity setting and a userspace affinity
457 * setup, but make sure that one of the targets is online.
458 */
459 if (irqd_affinity_is_managed(&desc->irq_data) ||
460 irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
461 if (cpumask_intersects(desc->irq_common_data.affinity,
462 cpu_online_mask))
463 set = desc->irq_common_data.affinity;
464 else
465 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
466 }
467
468 cpumask_and(&mask, cpu_online_mask, set);
David Brazdil0f672f62019-12-10 10:32:29 +0000469 if (cpumask_empty(&mask))
470 cpumask_copy(&mask, cpu_online_mask);
471
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472 if (node != NUMA_NO_NODE) {
473 const struct cpumask *nodemask = cpumask_of_node(node);
474
475 /* make sure at least one of the cpus in nodemask is online */
476 if (cpumask_intersects(&mask, nodemask))
477 cpumask_and(&mask, &mask, nodemask);
478 }
479 ret = irq_do_set_affinity(&desc->irq_data, &mask, false);
480 raw_spin_unlock(&mask_lock);
481 return ret;
482}
483#else
484/* Wrapper for ALPHA specific affinity selector magic */
485int irq_setup_affinity(struct irq_desc *desc)
486{
487 return irq_select_affinity(irq_desc_get_irq(desc));
488}
Olivier Deprez0e641232021-09-23 10:07:05 +0200489#endif /* CONFIG_AUTO_IRQ_AFFINITY */
490#endif /* CONFIG_SMP */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000492
493/**
494 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
495 * @irq: interrupt number to set affinity
496 * @vcpu_info: vCPU specific data or pointer to a percpu array of vCPU
497 * specific data for percpu_devid interrupts
498 *
499 * This function uses the vCPU specific data to set the vCPU
500 * affinity for an irq. The vCPU specific data is passed from
501 * outside, such as KVM. One example code path is as below:
502 * KVM -> IOMMU -> irq_set_vcpu_affinity().
503 */
504int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
505{
506 unsigned long flags;
507 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
508 struct irq_data *data;
509 struct irq_chip *chip;
510 int ret = -ENOSYS;
511
512 if (!desc)
513 return -EINVAL;
514
515 data = irq_desc_get_irq_data(desc);
516 do {
517 chip = irq_data_get_irq_chip(data);
518 if (chip && chip->irq_set_vcpu_affinity)
519 break;
520#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
521 data = data->parent_data;
522#else
523 data = NULL;
524#endif
525 } while (data);
526
527 if (data)
528 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
529 irq_put_desc_unlock(desc, flags);
530
531 return ret;
532}
533EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
534
535void __disable_irq(struct irq_desc *desc)
536{
537 if (!desc->depth++)
538 irq_disable(desc);
539}
540
541static int __disable_irq_nosync(unsigned int irq)
542{
543 unsigned long flags;
544 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
545
546 if (!desc)
547 return -EINVAL;
548 __disable_irq(desc);
549 irq_put_desc_busunlock(desc, flags);
550 return 0;
551}
552
553/**
554 * disable_irq_nosync - disable an irq without waiting
555 * @irq: Interrupt to disable
556 *
557 * Disable the selected interrupt line. Disables and Enables are
558 * nested.
559 * Unlike disable_irq(), this function does not ensure existing
560 * instances of the IRQ handler have completed before returning.
561 *
562 * This function may be called from IRQ context.
563 */
564void disable_irq_nosync(unsigned int irq)
565{
566 __disable_irq_nosync(irq);
567}
568EXPORT_SYMBOL(disable_irq_nosync);
569
570/**
571 * disable_irq - disable an irq and wait for completion
572 * @irq: Interrupt to disable
573 *
574 * Disable the selected interrupt line. Enables and Disables are
575 * nested.
576 * This function waits for any pending IRQ handlers for this interrupt
577 * to complete before returning. If you use this function while
578 * holding a resource the IRQ handler may need you will deadlock.
579 *
580 * This function may be called - with care - from IRQ context.
581 */
582void disable_irq(unsigned int irq)
583{
584 if (!__disable_irq_nosync(irq))
585 synchronize_irq(irq);
586}
587EXPORT_SYMBOL(disable_irq);
588
589/**
590 * disable_hardirq - disables an irq and waits for hardirq completion
591 * @irq: Interrupt to disable
592 *
593 * Disable the selected interrupt line. Enables and Disables are
594 * nested.
595 * This function waits for any pending hard IRQ handlers for this
596 * interrupt to complete before returning. If you use this function while
597 * holding a resource the hard IRQ handler may need you will deadlock.
598 *
599 * When used to optimistically disable an interrupt from atomic context
600 * the return value must be checked.
601 *
602 * Returns: false if a threaded handler is active.
603 *
604 * This function may be called - with care - from IRQ context.
605 */
606bool disable_hardirq(unsigned int irq)
607{
608 if (!__disable_irq_nosync(irq))
609 return synchronize_hardirq(irq);
610
611 return false;
612}
613EXPORT_SYMBOL_GPL(disable_hardirq);
614
David Brazdil0f672f62019-12-10 10:32:29 +0000615/**
616 * disable_nmi_nosync - disable an nmi without waiting
617 * @irq: Interrupt to disable
618 *
619 * Disable the selected interrupt line. Disables and enables are
620 * nested.
621 * The interrupt to disable must have been requested through request_nmi.
622 * Unlike disable_nmi(), this function does not ensure existing
623 * instances of the IRQ handler have completed before returning.
624 */
625void disable_nmi_nosync(unsigned int irq)
626{
627 disable_irq_nosync(irq);
628}
629
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000630void __enable_irq(struct irq_desc *desc)
631{
632 switch (desc->depth) {
633 case 0:
634 err_out:
635 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
636 irq_desc_get_irq(desc));
637 break;
638 case 1: {
639 if (desc->istate & IRQS_SUSPENDED)
640 goto err_out;
641 /* Prevent probing on this irq: */
642 irq_settings_set_noprobe(desc);
643 /*
644 * Call irq_startup() not irq_enable() here because the
645 * interrupt might be marked NOAUTOEN. So irq_startup()
646 * needs to be invoked when it gets enabled the first
647 * time. If it was already started up, then irq_startup()
648 * will invoke irq_enable() under the hood.
649 */
650 irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE);
651 break;
652 }
653 default:
654 desc->depth--;
655 }
656}
657
658/**
659 * enable_irq - enable handling of an irq
660 * @irq: Interrupt to enable
661 *
662 * Undoes the effect of one call to disable_irq(). If this
663 * matches the last disable, processing of interrupts on this
664 * IRQ line is re-enabled.
665 *
666 * This function may be called from IRQ context only when
667 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
668 */
669void enable_irq(unsigned int irq)
670{
671 unsigned long flags;
672 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
673
674 if (!desc)
675 return;
676 if (WARN(!desc->irq_data.chip,
677 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
678 goto out;
679
680 __enable_irq(desc);
681out:
682 irq_put_desc_busunlock(desc, flags);
683}
684EXPORT_SYMBOL(enable_irq);
685
David Brazdil0f672f62019-12-10 10:32:29 +0000686/**
687 * enable_nmi - enable handling of an nmi
688 * @irq: Interrupt to enable
689 *
690 * The interrupt to enable must have been requested through request_nmi.
691 * Undoes the effect of one call to disable_nmi(). If this
692 * matches the last disable, processing of interrupts on this
693 * IRQ line is re-enabled.
694 */
695void enable_nmi(unsigned int irq)
696{
697 enable_irq(irq);
698}
699
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000700static int set_irq_wake_real(unsigned int irq, unsigned int on)
701{
702 struct irq_desc *desc = irq_to_desc(irq);
703 int ret = -ENXIO;
704
705 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
706 return 0;
707
708 if (desc->irq_data.chip->irq_set_wake)
709 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
710
711 return ret;
712}
713
714/**
715 * irq_set_irq_wake - control irq power management wakeup
716 * @irq: interrupt to control
717 * @on: enable/disable power management wakeup
718 *
719 * Enable/disable power management wakeup mode, which is
720 * disabled by default. Enables and disables must match,
721 * just as they match for non-wakeup mode support.
722 *
723 * Wakeup mode lets this IRQ wake the system from sleep
724 * states like "suspend to RAM".
725 */
726int irq_set_irq_wake(unsigned int irq, unsigned int on)
727{
728 unsigned long flags;
729 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
730 int ret = 0;
731
732 if (!desc)
733 return -EINVAL;
734
David Brazdil0f672f62019-12-10 10:32:29 +0000735 /* Don't use NMIs as wake up interrupts please */
736 if (desc->istate & IRQS_NMI) {
737 ret = -EINVAL;
738 goto out_unlock;
739 }
740
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000741 /* wakeup-capable irqs can be shared between drivers that
742 * don't need to have the same sleep mode behaviors.
743 */
744 if (on) {
745 if (desc->wake_depth++ == 0) {
746 ret = set_irq_wake_real(irq, on);
747 if (ret)
748 desc->wake_depth = 0;
749 else
750 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
751 }
752 } else {
753 if (desc->wake_depth == 0) {
754 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
755 } else if (--desc->wake_depth == 0) {
756 ret = set_irq_wake_real(irq, on);
757 if (ret)
758 desc->wake_depth = 1;
759 else
760 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
761 }
762 }
David Brazdil0f672f62019-12-10 10:32:29 +0000763
764out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000765 irq_put_desc_busunlock(desc, flags);
766 return ret;
767}
768EXPORT_SYMBOL(irq_set_irq_wake);
769
770/*
771 * Internal function that tells the architecture code whether a
772 * particular irq has been exclusively allocated or is available
773 * for driver use.
774 */
775int can_request_irq(unsigned int irq, unsigned long irqflags)
776{
777 unsigned long flags;
778 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
779 int canrequest = 0;
780
781 if (!desc)
782 return 0;
783
784 if (irq_settings_can_request(desc)) {
785 if (!desc->action ||
786 irqflags & desc->action->flags & IRQF_SHARED)
787 canrequest = 1;
788 }
789 irq_put_desc_unlock(desc, flags);
790 return canrequest;
791}
792
793int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
794{
795 struct irq_chip *chip = desc->irq_data.chip;
796 int ret, unmask = 0;
797
798 if (!chip || !chip->irq_set_type) {
799 /*
800 * IRQF_TRIGGER_* but the PIC does not support multiple
801 * flow-types?
802 */
803 pr_debug("No set_type function for IRQ %d (%s)\n",
804 irq_desc_get_irq(desc),
805 chip ? (chip->name ? : "unknown") : "unknown");
806 return 0;
807 }
808
809 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
810 if (!irqd_irq_masked(&desc->irq_data))
811 mask_irq(desc);
812 if (!irqd_irq_disabled(&desc->irq_data))
813 unmask = 1;
814 }
815
816 /* Mask all flags except trigger mode */
817 flags &= IRQ_TYPE_SENSE_MASK;
818 ret = chip->irq_set_type(&desc->irq_data, flags);
819
820 switch (ret) {
821 case IRQ_SET_MASK_OK:
822 case IRQ_SET_MASK_OK_DONE:
823 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
824 irqd_set(&desc->irq_data, flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000825 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000826
827 case IRQ_SET_MASK_OK_NOCOPY:
828 flags = irqd_get_trigger_type(&desc->irq_data);
829 irq_settings_set_trigger_mask(desc, flags);
830 irqd_clear(&desc->irq_data, IRQD_LEVEL);
831 irq_settings_clr_level(desc);
832 if (flags & IRQ_TYPE_LEVEL_MASK) {
833 irq_settings_set_level(desc);
834 irqd_set(&desc->irq_data, IRQD_LEVEL);
835 }
836
837 ret = 0;
838 break;
839 default:
David Brazdil0f672f62019-12-10 10:32:29 +0000840 pr_err("Setting trigger mode %lu for irq %u failed (%pS)\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000841 flags, irq_desc_get_irq(desc), chip->irq_set_type);
842 }
843 if (unmask)
844 unmask_irq(desc);
845 return ret;
846}
847
848#ifdef CONFIG_HARDIRQS_SW_RESEND
849int irq_set_parent(int irq, int parent_irq)
850{
851 unsigned long flags;
852 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
853
854 if (!desc)
855 return -EINVAL;
856
857 desc->parent_irq = parent_irq;
858
859 irq_put_desc_unlock(desc, flags);
860 return 0;
861}
862EXPORT_SYMBOL_GPL(irq_set_parent);
863#endif
864
865/*
866 * Default primary interrupt handler for threaded interrupts. Is
867 * assigned as primary handler when request_threaded_irq is called
868 * with handler == NULL. Useful for oneshot interrupts.
869 */
870static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
871{
872 return IRQ_WAKE_THREAD;
873}
874
875/*
876 * Primary handler for nested threaded interrupts. Should never be
877 * called.
878 */
879static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
880{
881 WARN(1, "Primary handler called for nested irq %d\n", irq);
882 return IRQ_NONE;
883}
884
885static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
886{
887 WARN(1, "Secondary action handler called for irq %d\n", irq);
888 return IRQ_NONE;
889}
890
891static int irq_wait_for_interrupt(struct irqaction *action)
892{
893 for (;;) {
894 set_current_state(TASK_INTERRUPTIBLE);
895
896 if (kthread_should_stop()) {
897 /* may need to run one last time */
898 if (test_and_clear_bit(IRQTF_RUNTHREAD,
899 &action->thread_flags)) {
900 __set_current_state(TASK_RUNNING);
901 return 0;
902 }
903 __set_current_state(TASK_RUNNING);
904 return -1;
905 }
906
907 if (test_and_clear_bit(IRQTF_RUNTHREAD,
908 &action->thread_flags)) {
909 __set_current_state(TASK_RUNNING);
910 return 0;
911 }
912 schedule();
913 }
914}
915
916/*
917 * Oneshot interrupts keep the irq line masked until the threaded
918 * handler finished. unmask if the interrupt has not been disabled and
919 * is marked MASKED.
920 */
921static void irq_finalize_oneshot(struct irq_desc *desc,
922 struct irqaction *action)
923{
924 if (!(desc->istate & IRQS_ONESHOT) ||
925 action->handler == irq_forced_secondary_handler)
926 return;
927again:
928 chip_bus_lock(desc);
929 raw_spin_lock_irq(&desc->lock);
930
931 /*
932 * Implausible though it may be we need to protect us against
933 * the following scenario:
934 *
935 * The thread is faster done than the hard interrupt handler
936 * on the other CPU. If we unmask the irq line then the
937 * interrupt can come in again and masks the line, leaves due
938 * to IRQS_INPROGRESS and the irq line is masked forever.
939 *
940 * This also serializes the state of shared oneshot handlers
941 * versus "desc->threads_onehsot |= action->thread_mask;" in
942 * irq_wake_thread(). See the comment there which explains the
943 * serialization.
944 */
945 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
946 raw_spin_unlock_irq(&desc->lock);
947 chip_bus_sync_unlock(desc);
948 cpu_relax();
949 goto again;
950 }
951
952 /*
953 * Now check again, whether the thread should run. Otherwise
954 * we would clear the threads_oneshot bit of this thread which
955 * was just set.
956 */
957 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
958 goto out_unlock;
959
960 desc->threads_oneshot &= ~action->thread_mask;
961
962 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
963 irqd_irq_masked(&desc->irq_data))
964 unmask_threaded_irq(desc);
965
966out_unlock:
967 raw_spin_unlock_irq(&desc->lock);
968 chip_bus_sync_unlock(desc);
969}
970
971#ifdef CONFIG_SMP
972/*
973 * Check whether we need to change the affinity of the interrupt thread.
974 */
975static void
976irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
977{
978 cpumask_var_t mask;
979 bool valid = true;
980
981 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
982 return;
983
984 /*
985 * In case we are out of memory we set IRQTF_AFFINITY again and
986 * try again next time
987 */
988 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
989 set_bit(IRQTF_AFFINITY, &action->thread_flags);
990 return;
991 }
992
993 raw_spin_lock_irq(&desc->lock);
994 /*
995 * This code is triggered unconditionally. Check the affinity
996 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
997 */
998 if (cpumask_available(desc->irq_common_data.affinity)) {
999 const struct cpumask *m;
1000
1001 m = irq_data_get_effective_affinity_mask(&desc->irq_data);
1002 cpumask_copy(mask, m);
1003 } else {
1004 valid = false;
1005 }
1006 raw_spin_unlock_irq(&desc->lock);
1007
1008 if (valid)
1009 set_cpus_allowed_ptr(current, mask);
1010 free_cpumask_var(mask);
1011}
1012#else
1013static inline void
1014irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
1015#endif
1016
1017/*
David Brazdil0f672f62019-12-10 10:32:29 +00001018 * Interrupts which are not explicitly requested as threaded
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019 * interrupts rely on the implicit bh/preempt disable of the hard irq
1020 * context. So we need to disable bh here to avoid deadlocks and other
1021 * side effects.
1022 */
1023static irqreturn_t
1024irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
1025{
1026 irqreturn_t ret;
1027
1028 local_bh_disable();
Olivier Deprez0e641232021-09-23 10:07:05 +02001029 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
1030 local_irq_disable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001031 ret = action->thread_fn(action->irq, action->dev_id);
1032 if (ret == IRQ_HANDLED)
1033 atomic_inc(&desc->threads_handled);
1034
1035 irq_finalize_oneshot(desc, action);
Olivier Deprez0e641232021-09-23 10:07:05 +02001036 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
1037 local_irq_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 local_bh_enable();
1039 return ret;
1040}
1041
1042/*
1043 * Interrupts explicitly requested as threaded interrupts want to be
1044 * preemtible - many of them need to sleep and wait for slow busses to
1045 * complete.
1046 */
1047static irqreturn_t irq_thread_fn(struct irq_desc *desc,
1048 struct irqaction *action)
1049{
1050 irqreturn_t ret;
1051
1052 ret = action->thread_fn(action->irq, action->dev_id);
1053 if (ret == IRQ_HANDLED)
1054 atomic_inc(&desc->threads_handled);
1055
1056 irq_finalize_oneshot(desc, action);
1057 return ret;
1058}
1059
1060static void wake_threads_waitq(struct irq_desc *desc)
1061{
1062 if (atomic_dec_and_test(&desc->threads_active))
1063 wake_up(&desc->wait_for_threads);
1064}
1065
1066static void irq_thread_dtor(struct callback_head *unused)
1067{
1068 struct task_struct *tsk = current;
1069 struct irq_desc *desc;
1070 struct irqaction *action;
1071
1072 if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
1073 return;
1074
1075 action = kthread_data(tsk);
1076
1077 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
1078 tsk->comm, tsk->pid, action->irq);
1079
1080
1081 desc = irq_to_desc(action->irq);
1082 /*
1083 * If IRQTF_RUNTHREAD is set, we need to decrement
1084 * desc->threads_active and wake possible waiters.
1085 */
1086 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
1087 wake_threads_waitq(desc);
1088
1089 /* Prevent a stale desc->threads_oneshot */
1090 irq_finalize_oneshot(desc, action);
1091}
1092
1093static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
1094{
1095 struct irqaction *secondary = action->secondary;
1096
1097 if (WARN_ON_ONCE(!secondary))
1098 return;
1099
1100 raw_spin_lock_irq(&desc->lock);
1101 __irq_wake_thread(desc, secondary);
1102 raw_spin_unlock_irq(&desc->lock);
1103}
1104
1105/*
1106 * Interrupt handler thread
1107 */
1108static int irq_thread(void *data)
1109{
1110 struct callback_head on_exit_work;
1111 struct irqaction *action = data;
1112 struct irq_desc *desc = irq_to_desc(action->irq);
1113 irqreturn_t (*handler_fn)(struct irq_desc *desc,
1114 struct irqaction *action);
1115
1116 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
1117 &action->thread_flags))
1118 handler_fn = irq_forced_thread_fn;
1119 else
1120 handler_fn = irq_thread_fn;
1121
1122 init_task_work(&on_exit_work, irq_thread_dtor);
1123 task_work_add(current, &on_exit_work, false);
1124
1125 irq_thread_check_affinity(desc, action);
1126
1127 while (!irq_wait_for_interrupt(action)) {
1128 irqreturn_t action_ret;
1129
1130 irq_thread_check_affinity(desc, action);
1131
1132 action_ret = handler_fn(desc, action);
1133 if (action_ret == IRQ_WAKE_THREAD)
1134 irq_wake_secondary(desc, action);
1135
1136 wake_threads_waitq(desc);
1137 }
1138
1139 /*
1140 * This is the regular exit path. __free_irq() is stopping the
1141 * thread via kthread_stop() after calling
1142 * synchronize_hardirq(). So neither IRQTF_RUNTHREAD nor the
1143 * oneshot mask bit can be set.
1144 */
1145 task_work_cancel(current, irq_thread_dtor);
1146 return 0;
1147}
1148
1149/**
1150 * irq_wake_thread - wake the irq thread for the action identified by dev_id
1151 * @irq: Interrupt line
1152 * @dev_id: Device identity for which the thread should be woken
1153 *
1154 */
1155void irq_wake_thread(unsigned int irq, void *dev_id)
1156{
1157 struct irq_desc *desc = irq_to_desc(irq);
1158 struct irqaction *action;
1159 unsigned long flags;
1160
1161 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1162 return;
1163
1164 raw_spin_lock_irqsave(&desc->lock, flags);
1165 for_each_action_of_desc(desc, action) {
1166 if (action->dev_id == dev_id) {
1167 if (action->thread)
1168 __irq_wake_thread(desc, action);
1169 break;
1170 }
1171 }
1172 raw_spin_unlock_irqrestore(&desc->lock, flags);
1173}
1174EXPORT_SYMBOL_GPL(irq_wake_thread);
1175
1176static int irq_setup_forced_threading(struct irqaction *new)
1177{
1178 if (!force_irqthreads)
1179 return 0;
1180 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1181 return 0;
1182
1183 /*
1184 * No further action required for interrupts which are requested as
1185 * threaded interrupts already
1186 */
1187 if (new->handler == irq_default_primary_handler)
1188 return 0;
1189
1190 new->flags |= IRQF_ONESHOT;
1191
1192 /*
1193 * Handle the case where we have a real primary handler and a
1194 * thread handler. We force thread them as well by creating a
1195 * secondary action.
1196 */
1197 if (new->handler && new->thread_fn) {
1198 /* Allocate the secondary action */
1199 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1200 if (!new->secondary)
1201 return -ENOMEM;
1202 new->secondary->handler = irq_forced_secondary_handler;
1203 new->secondary->thread_fn = new->thread_fn;
1204 new->secondary->dev_id = new->dev_id;
1205 new->secondary->irq = new->irq;
1206 new->secondary->name = new->name;
1207 }
1208 /* Deal with the primary handler */
1209 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1210 new->thread_fn = new->handler;
1211 new->handler = irq_default_primary_handler;
1212 return 0;
1213}
1214
1215static int irq_request_resources(struct irq_desc *desc)
1216{
1217 struct irq_data *d = &desc->irq_data;
1218 struct irq_chip *c = d->chip;
1219
1220 return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1221}
1222
1223static void irq_release_resources(struct irq_desc *desc)
1224{
1225 struct irq_data *d = &desc->irq_data;
1226 struct irq_chip *c = d->chip;
1227
1228 if (c->irq_release_resources)
1229 c->irq_release_resources(d);
1230}
1231
David Brazdil0f672f62019-12-10 10:32:29 +00001232static bool irq_supports_nmi(struct irq_desc *desc)
1233{
1234 struct irq_data *d = irq_desc_get_irq_data(desc);
1235
1236#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1237 /* Only IRQs directly managed by the root irqchip can be set as NMI */
1238 if (d->parent_data)
1239 return false;
1240#endif
1241 /* Don't support NMIs for chips behind a slow bus */
1242 if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock)
1243 return false;
1244
1245 return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
1246}
1247
1248static int irq_nmi_setup(struct irq_desc *desc)
1249{
1250 struct irq_data *d = irq_desc_get_irq_data(desc);
1251 struct irq_chip *c = d->chip;
1252
1253 return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL;
1254}
1255
1256static void irq_nmi_teardown(struct irq_desc *desc)
1257{
1258 struct irq_data *d = irq_desc_get_irq_data(desc);
1259 struct irq_chip *c = d->chip;
1260
1261 if (c->irq_nmi_teardown)
1262 c->irq_nmi_teardown(d);
1263}
1264
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001265static int
1266setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1267{
1268 struct task_struct *t;
1269 struct sched_param param = {
1270 .sched_priority = MAX_USER_RT_PRIO/2,
1271 };
1272
1273 if (!secondary) {
1274 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1275 new->name);
1276 } else {
1277 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1278 new->name);
1279 param.sched_priority -= 1;
1280 }
1281
1282 if (IS_ERR(t))
1283 return PTR_ERR(t);
1284
1285 sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1286
1287 /*
1288 * We keep the reference to the task struct even if
1289 * the thread dies to avoid that the interrupt code
1290 * references an already freed task_struct.
1291 */
David Brazdil0f672f62019-12-10 10:32:29 +00001292 new->thread = get_task_struct(t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001293 /*
1294 * Tell the thread to set its affinity. This is
1295 * important for shared interrupt handlers as we do
1296 * not invoke setup_affinity() for the secondary
1297 * handlers as everything is already set up. Even for
1298 * interrupts marked with IRQF_NO_BALANCE this is
1299 * correct as we want the thread to move to the cpu(s)
1300 * on which the requesting code placed the interrupt.
1301 */
1302 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1303 return 0;
1304}
1305
1306/*
1307 * Internal function to register an irqaction - typically used to
1308 * allocate special interrupts that are part of the architecture.
1309 *
1310 * Locking rules:
1311 *
1312 * desc->request_mutex Provides serialization against a concurrent free_irq()
1313 * chip_bus_lock Provides serialization for slow bus operations
1314 * desc->lock Provides serialization against hard interrupts
1315 *
1316 * chip_bus_lock and desc->lock are sufficient for all other management and
1317 * interrupt related functions. desc->request_mutex solely serializes
1318 * request/free_irq().
1319 */
1320static int
1321__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1322{
1323 struct irqaction *old, **old_ptr;
1324 unsigned long flags, thread_mask = 0;
1325 int ret, nested, shared = 0;
1326
1327 if (!desc)
1328 return -EINVAL;
1329
1330 if (desc->irq_data.chip == &no_irq_chip)
1331 return -ENOSYS;
1332 if (!try_module_get(desc->owner))
1333 return -ENODEV;
1334
1335 new->irq = irq;
1336
1337 /*
1338 * If the trigger type is not specified by the caller,
1339 * then use the default for this interrupt.
1340 */
1341 if (!(new->flags & IRQF_TRIGGER_MASK))
1342 new->flags |= irqd_get_trigger_type(&desc->irq_data);
1343
1344 /*
1345 * Check whether the interrupt nests into another interrupt
1346 * thread.
1347 */
1348 nested = irq_settings_is_nested_thread(desc);
1349 if (nested) {
1350 if (!new->thread_fn) {
1351 ret = -EINVAL;
1352 goto out_mput;
1353 }
1354 /*
1355 * Replace the primary handler which was provided from
1356 * the driver for non nested interrupt handling by the
1357 * dummy function which warns when called.
1358 */
1359 new->handler = irq_nested_primary_handler;
1360 } else {
1361 if (irq_settings_can_thread(desc)) {
1362 ret = irq_setup_forced_threading(new);
1363 if (ret)
1364 goto out_mput;
1365 }
1366 }
1367
1368 /*
1369 * Create a handler thread when a thread function is supplied
1370 * and the interrupt does not nest into another interrupt
1371 * thread.
1372 */
1373 if (new->thread_fn && !nested) {
1374 ret = setup_irq_thread(new, irq, false);
1375 if (ret)
1376 goto out_mput;
1377 if (new->secondary) {
1378 ret = setup_irq_thread(new->secondary, irq, true);
1379 if (ret)
1380 goto out_thread;
1381 }
1382 }
1383
1384 /*
1385 * Drivers are often written to work w/o knowledge about the
1386 * underlying irq chip implementation, so a request for a
1387 * threaded irq without a primary hard irq context handler
1388 * requires the ONESHOT flag to be set. Some irq chips like
1389 * MSI based interrupts are per se one shot safe. Check the
1390 * chip flags, so we can avoid the unmask dance at the end of
1391 * the threaded handler for those.
1392 */
1393 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1394 new->flags &= ~IRQF_ONESHOT;
1395
1396 /*
1397 * Protects against a concurrent __free_irq() call which might wait
1398 * for synchronize_hardirq() to complete without holding the optional
1399 * chip bus lock and desc->lock. Also protects against handing out
1400 * a recycled oneshot thread_mask bit while it's still in use by
1401 * its previous owner.
1402 */
1403 mutex_lock(&desc->request_mutex);
1404
1405 /*
1406 * Acquire bus lock as the irq_request_resources() callback below
1407 * might rely on the serialization or the magic power management
1408 * functions which are abusing the irq_bus_lock() callback,
1409 */
1410 chip_bus_lock(desc);
1411
1412 /* First installed action requests resources. */
1413 if (!desc->action) {
1414 ret = irq_request_resources(desc);
1415 if (ret) {
1416 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1417 new->name, irq, desc->irq_data.chip->name);
1418 goto out_bus_unlock;
1419 }
1420 }
1421
1422 /*
1423 * The following block of code has to be executed atomically
1424 * protected against a concurrent interrupt and any of the other
1425 * management calls which are not serialized via
1426 * desc->request_mutex or the optional bus lock.
1427 */
1428 raw_spin_lock_irqsave(&desc->lock, flags);
1429 old_ptr = &desc->action;
1430 old = *old_ptr;
1431 if (old) {
1432 /*
1433 * Can't share interrupts unless both agree to and are
1434 * the same type (level, edge, polarity). So both flag
1435 * fields must have IRQF_SHARED set and the bits which
1436 * set the trigger type must match. Also all must
1437 * agree on ONESHOT.
David Brazdil0f672f62019-12-10 10:32:29 +00001438 * Interrupt lines used for NMIs cannot be shared.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001439 */
1440 unsigned int oldtype;
1441
David Brazdil0f672f62019-12-10 10:32:29 +00001442 if (desc->istate & IRQS_NMI) {
1443 pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n",
1444 new->name, irq, desc->irq_data.chip->name);
1445 ret = -EINVAL;
1446 goto out_unlock;
1447 }
1448
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449 /*
1450 * If nobody did set the configuration before, inherit
1451 * the one provided by the requester.
1452 */
1453 if (irqd_trigger_type_was_set(&desc->irq_data)) {
1454 oldtype = irqd_get_trigger_type(&desc->irq_data);
1455 } else {
1456 oldtype = new->flags & IRQF_TRIGGER_MASK;
1457 irqd_set_trigger_type(&desc->irq_data, oldtype);
1458 }
1459
1460 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1461 (oldtype != (new->flags & IRQF_TRIGGER_MASK)) ||
1462 ((old->flags ^ new->flags) & IRQF_ONESHOT))
1463 goto mismatch;
1464
1465 /* All handlers must agree on per-cpuness */
1466 if ((old->flags & IRQF_PERCPU) !=
1467 (new->flags & IRQF_PERCPU))
1468 goto mismatch;
1469
1470 /* add new interrupt at end of irq queue */
1471 do {
1472 /*
1473 * Or all existing action->thread_mask bits,
1474 * so we can find the next zero bit for this
1475 * new action.
1476 */
1477 thread_mask |= old->thread_mask;
1478 old_ptr = &old->next;
1479 old = *old_ptr;
1480 } while (old);
1481 shared = 1;
1482 }
1483
1484 /*
1485 * Setup the thread mask for this irqaction for ONESHOT. For
1486 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1487 * conditional in irq_wake_thread().
1488 */
1489 if (new->flags & IRQF_ONESHOT) {
1490 /*
1491 * Unlikely to have 32 resp 64 irqs sharing one line,
1492 * but who knows.
1493 */
1494 if (thread_mask == ~0UL) {
1495 ret = -EBUSY;
1496 goto out_unlock;
1497 }
1498 /*
1499 * The thread_mask for the action is or'ed to
1500 * desc->thread_active to indicate that the
1501 * IRQF_ONESHOT thread handler has been woken, but not
1502 * yet finished. The bit is cleared when a thread
1503 * completes. When all threads of a shared interrupt
1504 * line have completed desc->threads_active becomes
1505 * zero and the interrupt line is unmasked. See
1506 * handle.c:irq_wake_thread() for further information.
1507 *
1508 * If no thread is woken by primary (hard irq context)
1509 * interrupt handlers, then desc->threads_active is
1510 * also checked for zero to unmask the irq line in the
1511 * affected hard irq flow handlers
1512 * (handle_[fasteoi|level]_irq).
1513 *
1514 * The new action gets the first zero bit of
1515 * thread_mask assigned. See the loop above which or's
1516 * all existing action->thread_mask bits.
1517 */
1518 new->thread_mask = 1UL << ffz(thread_mask);
1519
1520 } else if (new->handler == irq_default_primary_handler &&
1521 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1522 /*
1523 * The interrupt was requested with handler = NULL, so
1524 * we use the default primary handler for it. But it
1525 * does not have the oneshot flag set. In combination
1526 * with level interrupts this is deadly, because the
1527 * default primary handler just wakes the thread, then
1528 * the irq lines is reenabled, but the device still
1529 * has the level irq asserted. Rinse and repeat....
1530 *
1531 * While this works for edge type interrupts, we play
1532 * it safe and reject unconditionally because we can't
1533 * say for sure which type this interrupt really
1534 * has. The type flags are unreliable as the
1535 * underlying chip implementation can override them.
1536 */
1537 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1538 irq);
1539 ret = -EINVAL;
1540 goto out_unlock;
1541 }
1542
1543 if (!shared) {
1544 init_waitqueue_head(&desc->wait_for_threads);
1545
1546 /* Setup the type (level, edge polarity) if configured: */
1547 if (new->flags & IRQF_TRIGGER_MASK) {
1548 ret = __irq_set_trigger(desc,
1549 new->flags & IRQF_TRIGGER_MASK);
1550
1551 if (ret)
1552 goto out_unlock;
1553 }
1554
1555 /*
1556 * Activate the interrupt. That activation must happen
1557 * independently of IRQ_NOAUTOEN. request_irq() can fail
1558 * and the callers are supposed to handle
1559 * that. enable_irq() of an interrupt requested with
1560 * IRQ_NOAUTOEN is not supposed to fail. The activation
1561 * keeps it in shutdown mode, it merily associates
1562 * resources if necessary and if that's not possible it
1563 * fails. Interrupts which are in managed shutdown mode
1564 * will simply ignore that activation request.
1565 */
1566 ret = irq_activate(desc);
1567 if (ret)
1568 goto out_unlock;
1569
1570 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1571 IRQS_ONESHOT | IRQS_WAITING);
1572 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1573
1574 if (new->flags & IRQF_PERCPU) {
1575 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1576 irq_settings_set_per_cpu(desc);
1577 }
1578
1579 if (new->flags & IRQF_ONESHOT)
1580 desc->istate |= IRQS_ONESHOT;
1581
1582 /* Exclude IRQ from balancing if requested */
1583 if (new->flags & IRQF_NOBALANCING) {
1584 irq_settings_set_no_balancing(desc);
1585 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1586 }
1587
1588 if (irq_settings_can_autoenable(desc)) {
1589 irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
1590 } else {
1591 /*
1592 * Shared interrupts do not go well with disabling
1593 * auto enable. The sharing interrupt might request
1594 * it while it's still disabled and then wait for
1595 * interrupts forever.
1596 */
1597 WARN_ON_ONCE(new->flags & IRQF_SHARED);
1598 /* Undo nested disables: */
1599 desc->depth = 1;
1600 }
1601
1602 } else if (new->flags & IRQF_TRIGGER_MASK) {
1603 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1604 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
1605
1606 if (nmsk != omsk)
1607 /* hope the handler works with current trigger mode */
1608 pr_warn("irq %d uses trigger mode %u; requested %u\n",
1609 irq, omsk, nmsk);
1610 }
1611
1612 *old_ptr = new;
1613
1614 irq_pm_install_action(desc, new);
1615
1616 /* Reset broken irq detection when installing new handler */
1617 desc->irq_count = 0;
1618 desc->irqs_unhandled = 0;
1619
1620 /*
1621 * Check whether we disabled the irq via the spurious handler
1622 * before. Reenable it and give it another chance.
1623 */
1624 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1625 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1626 __enable_irq(desc);
1627 }
1628
1629 raw_spin_unlock_irqrestore(&desc->lock, flags);
1630 chip_bus_sync_unlock(desc);
1631 mutex_unlock(&desc->request_mutex);
1632
1633 irq_setup_timings(desc, new);
1634
1635 /*
1636 * Strictly no need to wake it up, but hung_task complains
1637 * when no hard interrupt wakes the thread up.
1638 */
1639 if (new->thread)
1640 wake_up_process(new->thread);
1641 if (new->secondary)
1642 wake_up_process(new->secondary->thread);
1643
1644 register_irq_proc(irq, desc);
1645 new->dir = NULL;
1646 register_handler_proc(irq, new);
1647 return 0;
1648
1649mismatch:
1650 if (!(new->flags & IRQF_PROBE_SHARED)) {
1651 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1652 irq, new->flags, new->name, old->flags, old->name);
1653#ifdef CONFIG_DEBUG_SHIRQ
1654 dump_stack();
1655#endif
1656 }
1657 ret = -EBUSY;
1658
1659out_unlock:
1660 raw_spin_unlock_irqrestore(&desc->lock, flags);
1661
1662 if (!desc->action)
1663 irq_release_resources(desc);
1664out_bus_unlock:
1665 chip_bus_sync_unlock(desc);
1666 mutex_unlock(&desc->request_mutex);
1667
1668out_thread:
1669 if (new->thread) {
1670 struct task_struct *t = new->thread;
1671
1672 new->thread = NULL;
1673 kthread_stop(t);
1674 put_task_struct(t);
1675 }
1676 if (new->secondary && new->secondary->thread) {
1677 struct task_struct *t = new->secondary->thread;
1678
1679 new->secondary->thread = NULL;
1680 kthread_stop(t);
1681 put_task_struct(t);
1682 }
1683out_mput:
1684 module_put(desc->owner);
1685 return ret;
1686}
1687
1688/**
1689 * setup_irq - setup an interrupt
1690 * @irq: Interrupt line to setup
1691 * @act: irqaction for the interrupt
1692 *
1693 * Used to statically setup interrupts in the early boot process.
1694 */
1695int setup_irq(unsigned int irq, struct irqaction *act)
1696{
1697 int retval;
1698 struct irq_desc *desc = irq_to_desc(irq);
1699
1700 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1701 return -EINVAL;
1702
1703 retval = irq_chip_pm_get(&desc->irq_data);
1704 if (retval < 0)
1705 return retval;
1706
1707 retval = __setup_irq(irq, desc, act);
1708
1709 if (retval)
1710 irq_chip_pm_put(&desc->irq_data);
1711
1712 return retval;
1713}
1714EXPORT_SYMBOL_GPL(setup_irq);
1715
1716/*
1717 * Internal function to unregister an irqaction - used to free
1718 * regular and special interrupts that are part of the architecture.
1719 */
1720static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
1721{
1722 unsigned irq = desc->irq_data.irq;
1723 struct irqaction *action, **action_ptr;
1724 unsigned long flags;
1725
1726 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1727
1728 mutex_lock(&desc->request_mutex);
1729 chip_bus_lock(desc);
1730 raw_spin_lock_irqsave(&desc->lock, flags);
1731
1732 /*
1733 * There can be multiple actions per IRQ descriptor, find the right
1734 * one based on the dev_id:
1735 */
1736 action_ptr = &desc->action;
1737 for (;;) {
1738 action = *action_ptr;
1739
1740 if (!action) {
1741 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1742 raw_spin_unlock_irqrestore(&desc->lock, flags);
1743 chip_bus_sync_unlock(desc);
1744 mutex_unlock(&desc->request_mutex);
1745 return NULL;
1746 }
1747
1748 if (action->dev_id == dev_id)
1749 break;
1750 action_ptr = &action->next;
1751 }
1752
1753 /* Found it - now remove it from the list of entries: */
1754 *action_ptr = action->next;
1755
1756 irq_pm_remove_action(desc, action);
1757
1758 /* If this was the last handler, shut down the IRQ line: */
1759 if (!desc->action) {
1760 irq_settings_clr_disable_unlazy(desc);
David Brazdil0f672f62019-12-10 10:32:29 +00001761 /* Only shutdown. Deactivate after synchronize_hardirq() */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001762 irq_shutdown(desc);
1763 }
1764
1765#ifdef CONFIG_SMP
1766 /* make sure affinity_hint is cleaned up */
1767 if (WARN_ON_ONCE(desc->affinity_hint))
1768 desc->affinity_hint = NULL;
1769#endif
1770
1771 raw_spin_unlock_irqrestore(&desc->lock, flags);
1772 /*
1773 * Drop bus_lock here so the changes which were done in the chip
1774 * callbacks above are synced out to the irq chips which hang
1775 * behind a slow bus (I2C, SPI) before calling synchronize_hardirq().
1776 *
1777 * Aside of that the bus_lock can also be taken from the threaded
1778 * handler in irq_finalize_oneshot() which results in a deadlock
1779 * because kthread_stop() would wait forever for the thread to
1780 * complete, which is blocked on the bus lock.
1781 *
1782 * The still held desc->request_mutex() protects against a
1783 * concurrent request_irq() of this irq so the release of resources
1784 * and timing data is properly serialized.
1785 */
1786 chip_bus_sync_unlock(desc);
1787
1788 unregister_handler_proc(irq, action);
1789
David Brazdil0f672f62019-12-10 10:32:29 +00001790 /*
1791 * Make sure it's not being used on another CPU and if the chip
1792 * supports it also make sure that there is no (not yet serviced)
1793 * interrupt in flight at the hardware level.
1794 */
1795 __synchronize_hardirq(desc, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001796
1797#ifdef CONFIG_DEBUG_SHIRQ
1798 /*
1799 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1800 * event to happen even now it's being freed, so let's make sure that
1801 * is so by doing an extra call to the handler ....
1802 *
1803 * ( We do this after actually deregistering it, to make sure that a
1804 * 'real' IRQ doesn't run in parallel with our fake. )
1805 */
1806 if (action->flags & IRQF_SHARED) {
1807 local_irq_save(flags);
1808 action->handler(irq, dev_id);
1809 local_irq_restore(flags);
1810 }
1811#endif
1812
1813 /*
1814 * The action has already been removed above, but the thread writes
1815 * its oneshot mask bit when it completes. Though request_mutex is
1816 * held across this which prevents __setup_irq() from handing out
1817 * the same bit to a newly requested action.
1818 */
1819 if (action->thread) {
1820 kthread_stop(action->thread);
1821 put_task_struct(action->thread);
1822 if (action->secondary && action->secondary->thread) {
1823 kthread_stop(action->secondary->thread);
1824 put_task_struct(action->secondary->thread);
1825 }
1826 }
1827
1828 /* Last action releases resources */
1829 if (!desc->action) {
1830 /*
1831 * Reaquire bus lock as irq_release_resources() might
1832 * require it to deallocate resources over the slow bus.
1833 */
1834 chip_bus_lock(desc);
David Brazdil0f672f62019-12-10 10:32:29 +00001835 /*
1836 * There is no interrupt on the fly anymore. Deactivate it
1837 * completely.
1838 */
1839 raw_spin_lock_irqsave(&desc->lock, flags);
1840 irq_domain_deactivate_irq(&desc->irq_data);
1841 raw_spin_unlock_irqrestore(&desc->lock, flags);
1842
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001843 irq_release_resources(desc);
1844 chip_bus_sync_unlock(desc);
1845 irq_remove_timings(desc);
1846 }
1847
1848 mutex_unlock(&desc->request_mutex);
1849
1850 irq_chip_pm_put(&desc->irq_data);
1851 module_put(desc->owner);
1852 kfree(action->secondary);
1853 return action;
1854}
1855
1856/**
1857 * remove_irq - free an interrupt
1858 * @irq: Interrupt line to free
1859 * @act: irqaction for the interrupt
1860 *
1861 * Used to remove interrupts statically setup by the early boot process.
1862 */
1863void remove_irq(unsigned int irq, struct irqaction *act)
1864{
1865 struct irq_desc *desc = irq_to_desc(irq);
1866
1867 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1868 __free_irq(desc, act->dev_id);
1869}
1870EXPORT_SYMBOL_GPL(remove_irq);
1871
1872/**
1873 * free_irq - free an interrupt allocated with request_irq
1874 * @irq: Interrupt line to free
1875 * @dev_id: Device identity to free
1876 *
1877 * Remove an interrupt handler. The handler is removed and if the
1878 * interrupt line is no longer in use by any driver it is disabled.
1879 * On a shared IRQ the caller must ensure the interrupt is disabled
1880 * on the card it drives before calling this function. The function
1881 * does not return until any executing interrupts for this IRQ
1882 * have completed.
1883 *
1884 * This function must not be called from interrupt context.
1885 *
1886 * Returns the devname argument passed to request_irq.
1887 */
1888const void *free_irq(unsigned int irq, void *dev_id)
1889{
1890 struct irq_desc *desc = irq_to_desc(irq);
1891 struct irqaction *action;
1892 const char *devname;
1893
1894 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1895 return NULL;
1896
1897#ifdef CONFIG_SMP
1898 if (WARN_ON(desc->affinity_notify))
1899 desc->affinity_notify = NULL;
1900#endif
1901
1902 action = __free_irq(desc, dev_id);
1903
1904 if (!action)
1905 return NULL;
1906
1907 devname = action->name;
1908 kfree(action);
1909 return devname;
1910}
1911EXPORT_SYMBOL(free_irq);
1912
David Brazdil0f672f62019-12-10 10:32:29 +00001913/* This function must be called with desc->lock held */
1914static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc)
1915{
1916 const char *devname = NULL;
1917
1918 desc->istate &= ~IRQS_NMI;
1919
1920 if (!WARN_ON(desc->action == NULL)) {
1921 irq_pm_remove_action(desc, desc->action);
1922 devname = desc->action->name;
1923 unregister_handler_proc(irq, desc->action);
1924
1925 kfree(desc->action);
1926 desc->action = NULL;
1927 }
1928
1929 irq_settings_clr_disable_unlazy(desc);
1930 irq_shutdown_and_deactivate(desc);
1931
1932 irq_release_resources(desc);
1933
1934 irq_chip_pm_put(&desc->irq_data);
1935 module_put(desc->owner);
1936
1937 return devname;
1938}
1939
1940const void *free_nmi(unsigned int irq, void *dev_id)
1941{
1942 struct irq_desc *desc = irq_to_desc(irq);
1943 unsigned long flags;
1944 const void *devname;
1945
1946 if (!desc || WARN_ON(!(desc->istate & IRQS_NMI)))
1947 return NULL;
1948
1949 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1950 return NULL;
1951
1952 /* NMI still enabled */
1953 if (WARN_ON(desc->depth == 0))
1954 disable_nmi_nosync(irq);
1955
1956 raw_spin_lock_irqsave(&desc->lock, flags);
1957
1958 irq_nmi_teardown(desc);
1959 devname = __cleanup_nmi(irq, desc);
1960
1961 raw_spin_unlock_irqrestore(&desc->lock, flags);
1962
1963 return devname;
1964}
1965
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001966/**
1967 * request_threaded_irq - allocate an interrupt line
1968 * @irq: Interrupt line to allocate
1969 * @handler: Function to be called when the IRQ occurs.
1970 * Primary handler for threaded interrupts
1971 * If NULL and thread_fn != NULL the default
1972 * primary handler is installed
1973 * @thread_fn: Function called from the irq handler thread
1974 * If NULL, no irq thread is created
1975 * @irqflags: Interrupt type flags
1976 * @devname: An ascii name for the claiming device
1977 * @dev_id: A cookie passed back to the handler function
1978 *
1979 * This call allocates interrupt resources and enables the
1980 * interrupt line and IRQ handling. From the point this
1981 * call is made your handler function may be invoked. Since
1982 * your handler function must clear any interrupt the board
1983 * raises, you must take care both to initialise your hardware
1984 * and to set up the interrupt handler in the right order.
1985 *
1986 * If you want to set up a threaded irq handler for your device
1987 * then you need to supply @handler and @thread_fn. @handler is
1988 * still called in hard interrupt context and has to check
1989 * whether the interrupt originates from the device. If yes it
1990 * needs to disable the interrupt on the device and return
1991 * IRQ_WAKE_THREAD which will wake up the handler thread and run
1992 * @thread_fn. This split handler design is necessary to support
1993 * shared interrupts.
1994 *
1995 * Dev_id must be globally unique. Normally the address of the
1996 * device data structure is used as the cookie. Since the handler
1997 * receives this value it makes sense to use it.
1998 *
1999 * If your interrupt is shared you must pass a non NULL dev_id
2000 * as this is required when freeing the interrupt.
2001 *
2002 * Flags:
2003 *
2004 * IRQF_SHARED Interrupt is shared
2005 * IRQF_TRIGGER_* Specify active edge(s) or level
2006 *
2007 */
2008int request_threaded_irq(unsigned int irq, irq_handler_t handler,
2009 irq_handler_t thread_fn, unsigned long irqflags,
2010 const char *devname, void *dev_id)
2011{
2012 struct irqaction *action;
2013 struct irq_desc *desc;
2014 int retval;
2015
2016 if (irq == IRQ_NOTCONNECTED)
2017 return -ENOTCONN;
2018
2019 /*
2020 * Sanity-check: shared interrupts must pass in a real dev-ID,
2021 * otherwise we'll have trouble later trying to figure out
2022 * which interrupt is which (messes up the interrupt freeing
2023 * logic etc).
2024 *
2025 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
2026 * it cannot be set along with IRQF_NO_SUSPEND.
2027 */
2028 if (((irqflags & IRQF_SHARED) && !dev_id) ||
2029 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
2030 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
2031 return -EINVAL;
2032
2033 desc = irq_to_desc(irq);
2034 if (!desc)
2035 return -EINVAL;
2036
2037 if (!irq_settings_can_request(desc) ||
2038 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
2039 return -EINVAL;
2040
2041 if (!handler) {
2042 if (!thread_fn)
2043 return -EINVAL;
2044 handler = irq_default_primary_handler;
2045 }
2046
2047 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2048 if (!action)
2049 return -ENOMEM;
2050
2051 action->handler = handler;
2052 action->thread_fn = thread_fn;
2053 action->flags = irqflags;
2054 action->name = devname;
2055 action->dev_id = dev_id;
2056
2057 retval = irq_chip_pm_get(&desc->irq_data);
2058 if (retval < 0) {
2059 kfree(action);
2060 return retval;
2061 }
2062
2063 retval = __setup_irq(irq, desc, action);
2064
2065 if (retval) {
2066 irq_chip_pm_put(&desc->irq_data);
2067 kfree(action->secondary);
2068 kfree(action);
2069 }
2070
2071#ifdef CONFIG_DEBUG_SHIRQ_FIXME
2072 if (!retval && (irqflags & IRQF_SHARED)) {
2073 /*
2074 * It's a shared IRQ -- the driver ought to be prepared for it
2075 * to happen immediately, so let's make sure....
2076 * We disable the irq to make sure that a 'real' IRQ doesn't
2077 * run in parallel with our fake.
2078 */
2079 unsigned long flags;
2080
2081 disable_irq(irq);
2082 local_irq_save(flags);
2083
2084 handler(irq, dev_id);
2085
2086 local_irq_restore(flags);
2087 enable_irq(irq);
2088 }
2089#endif
2090 return retval;
2091}
2092EXPORT_SYMBOL(request_threaded_irq);
2093
2094/**
2095 * request_any_context_irq - allocate an interrupt line
2096 * @irq: Interrupt line to allocate
2097 * @handler: Function to be called when the IRQ occurs.
2098 * Threaded handler for threaded interrupts.
2099 * @flags: Interrupt type flags
2100 * @name: An ascii name for the claiming device
2101 * @dev_id: A cookie passed back to the handler function
2102 *
2103 * This call allocates interrupt resources and enables the
2104 * interrupt line and IRQ handling. It selects either a
2105 * hardirq or threaded handling method depending on the
2106 * context.
2107 *
2108 * On failure, it returns a negative value. On success,
2109 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
2110 */
2111int request_any_context_irq(unsigned int irq, irq_handler_t handler,
2112 unsigned long flags, const char *name, void *dev_id)
2113{
2114 struct irq_desc *desc;
2115 int ret;
2116
2117 if (irq == IRQ_NOTCONNECTED)
2118 return -ENOTCONN;
2119
2120 desc = irq_to_desc(irq);
2121 if (!desc)
2122 return -EINVAL;
2123
2124 if (irq_settings_is_nested_thread(desc)) {
2125 ret = request_threaded_irq(irq, NULL, handler,
2126 flags, name, dev_id);
2127 return !ret ? IRQC_IS_NESTED : ret;
2128 }
2129
2130 ret = request_irq(irq, handler, flags, name, dev_id);
2131 return !ret ? IRQC_IS_HARDIRQ : ret;
2132}
2133EXPORT_SYMBOL_GPL(request_any_context_irq);
2134
David Brazdil0f672f62019-12-10 10:32:29 +00002135/**
2136 * request_nmi - allocate an interrupt line for NMI delivery
2137 * @irq: Interrupt line to allocate
2138 * @handler: Function to be called when the IRQ occurs.
2139 * Threaded handler for threaded interrupts.
2140 * @irqflags: Interrupt type flags
2141 * @name: An ascii name for the claiming device
2142 * @dev_id: A cookie passed back to the handler function
2143 *
2144 * This call allocates interrupt resources and enables the
2145 * interrupt line and IRQ handling. It sets up the IRQ line
2146 * to be handled as an NMI.
2147 *
2148 * An interrupt line delivering NMIs cannot be shared and IRQ handling
2149 * cannot be threaded.
2150 *
2151 * Interrupt lines requested for NMI delivering must produce per cpu
2152 * interrupts and have auto enabling setting disabled.
2153 *
2154 * Dev_id must be globally unique. Normally the address of the
2155 * device data structure is used as the cookie. Since the handler
2156 * receives this value it makes sense to use it.
2157 *
2158 * If the interrupt line cannot be used to deliver NMIs, function
2159 * will fail and return a negative value.
2160 */
2161int request_nmi(unsigned int irq, irq_handler_t handler,
2162 unsigned long irqflags, const char *name, void *dev_id)
2163{
2164 struct irqaction *action;
2165 struct irq_desc *desc;
2166 unsigned long flags;
2167 int retval;
2168
2169 if (irq == IRQ_NOTCONNECTED)
2170 return -ENOTCONN;
2171
2172 /* NMI cannot be shared, used for Polling */
2173 if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
2174 return -EINVAL;
2175
2176 if (!(irqflags & IRQF_PERCPU))
2177 return -EINVAL;
2178
2179 if (!handler)
2180 return -EINVAL;
2181
2182 desc = irq_to_desc(irq);
2183
2184 if (!desc || irq_settings_can_autoenable(desc) ||
2185 !irq_settings_can_request(desc) ||
2186 WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
2187 !irq_supports_nmi(desc))
2188 return -EINVAL;
2189
2190 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2191 if (!action)
2192 return -ENOMEM;
2193
2194 action->handler = handler;
2195 action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING;
2196 action->name = name;
2197 action->dev_id = dev_id;
2198
2199 retval = irq_chip_pm_get(&desc->irq_data);
2200 if (retval < 0)
2201 goto err_out;
2202
2203 retval = __setup_irq(irq, desc, action);
2204 if (retval)
2205 goto err_irq_setup;
2206
2207 raw_spin_lock_irqsave(&desc->lock, flags);
2208
2209 /* Setup NMI state */
2210 desc->istate |= IRQS_NMI;
2211 retval = irq_nmi_setup(desc);
2212 if (retval) {
2213 __cleanup_nmi(irq, desc);
2214 raw_spin_unlock_irqrestore(&desc->lock, flags);
2215 return -EINVAL;
2216 }
2217
2218 raw_spin_unlock_irqrestore(&desc->lock, flags);
2219
2220 return 0;
2221
2222err_irq_setup:
2223 irq_chip_pm_put(&desc->irq_data);
2224err_out:
2225 kfree(action);
2226
2227 return retval;
2228}
2229
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002230void enable_percpu_irq(unsigned int irq, unsigned int type)
2231{
2232 unsigned int cpu = smp_processor_id();
2233 unsigned long flags;
2234 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2235
2236 if (!desc)
2237 return;
2238
2239 /*
2240 * If the trigger type is not specified by the caller, then
2241 * use the default for this interrupt.
2242 */
2243 type &= IRQ_TYPE_SENSE_MASK;
2244 if (type == IRQ_TYPE_NONE)
2245 type = irqd_get_trigger_type(&desc->irq_data);
2246
2247 if (type != IRQ_TYPE_NONE) {
2248 int ret;
2249
2250 ret = __irq_set_trigger(desc, type);
2251
2252 if (ret) {
2253 WARN(1, "failed to set type for IRQ%d\n", irq);
2254 goto out;
2255 }
2256 }
2257
2258 irq_percpu_enable(desc, cpu);
2259out:
2260 irq_put_desc_unlock(desc, flags);
2261}
2262EXPORT_SYMBOL_GPL(enable_percpu_irq);
2263
David Brazdil0f672f62019-12-10 10:32:29 +00002264void enable_percpu_nmi(unsigned int irq, unsigned int type)
2265{
2266 enable_percpu_irq(irq, type);
2267}
2268
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002269/**
2270 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
2271 * @irq: Linux irq number to check for
2272 *
2273 * Must be called from a non migratable context. Returns the enable
2274 * state of a per cpu interrupt on the current cpu.
2275 */
2276bool irq_percpu_is_enabled(unsigned int irq)
2277{
2278 unsigned int cpu = smp_processor_id();
2279 struct irq_desc *desc;
2280 unsigned long flags;
2281 bool is_enabled;
2282
2283 desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2284 if (!desc)
2285 return false;
2286
2287 is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
2288 irq_put_desc_unlock(desc, flags);
2289
2290 return is_enabled;
2291}
2292EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
2293
2294void disable_percpu_irq(unsigned int irq)
2295{
2296 unsigned int cpu = smp_processor_id();
2297 unsigned long flags;
2298 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
2299
2300 if (!desc)
2301 return;
2302
2303 irq_percpu_disable(desc, cpu);
2304 irq_put_desc_unlock(desc, flags);
2305}
2306EXPORT_SYMBOL_GPL(disable_percpu_irq);
2307
David Brazdil0f672f62019-12-10 10:32:29 +00002308void disable_percpu_nmi(unsigned int irq)
2309{
2310 disable_percpu_irq(irq);
2311}
2312
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002313/*
2314 * Internal function to unregister a percpu irqaction.
2315 */
2316static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2317{
2318 struct irq_desc *desc = irq_to_desc(irq);
2319 struct irqaction *action;
2320 unsigned long flags;
2321
2322 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
2323
2324 if (!desc)
2325 return NULL;
2326
2327 raw_spin_lock_irqsave(&desc->lock, flags);
2328
2329 action = desc->action;
2330 if (!action || action->percpu_dev_id != dev_id) {
2331 WARN(1, "Trying to free already-free IRQ %d\n", irq);
2332 goto bad;
2333 }
2334
2335 if (!cpumask_empty(desc->percpu_enabled)) {
2336 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
2337 irq, cpumask_first(desc->percpu_enabled));
2338 goto bad;
2339 }
2340
2341 /* Found it - now remove it from the list of entries: */
2342 desc->action = NULL;
2343
David Brazdil0f672f62019-12-10 10:32:29 +00002344 desc->istate &= ~IRQS_NMI;
2345
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002346 raw_spin_unlock_irqrestore(&desc->lock, flags);
2347
2348 unregister_handler_proc(irq, action);
2349
2350 irq_chip_pm_put(&desc->irq_data);
2351 module_put(desc->owner);
2352 return action;
2353
2354bad:
2355 raw_spin_unlock_irqrestore(&desc->lock, flags);
2356 return NULL;
2357}
2358
2359/**
2360 * remove_percpu_irq - free a per-cpu interrupt
2361 * @irq: Interrupt line to free
2362 * @act: irqaction for the interrupt
2363 *
2364 * Used to remove interrupts statically setup by the early boot process.
2365 */
2366void remove_percpu_irq(unsigned int irq, struct irqaction *act)
2367{
2368 struct irq_desc *desc = irq_to_desc(irq);
2369
2370 if (desc && irq_settings_is_per_cpu_devid(desc))
2371 __free_percpu_irq(irq, act->percpu_dev_id);
2372}
2373
2374/**
2375 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
2376 * @irq: Interrupt line to free
2377 * @dev_id: Device identity to free
2378 *
2379 * Remove a percpu interrupt handler. The handler is removed, but
2380 * the interrupt line is not disabled. This must be done on each
2381 * CPU before calling this function. The function does not return
2382 * until any executing interrupts for this IRQ have completed.
2383 *
2384 * This function must not be called from interrupt context.
2385 */
2386void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
2387{
2388 struct irq_desc *desc = irq_to_desc(irq);
2389
2390 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2391 return;
2392
2393 chip_bus_lock(desc);
2394 kfree(__free_percpu_irq(irq, dev_id));
2395 chip_bus_sync_unlock(desc);
2396}
2397EXPORT_SYMBOL_GPL(free_percpu_irq);
2398
David Brazdil0f672f62019-12-10 10:32:29 +00002399void free_percpu_nmi(unsigned int irq, void __percpu *dev_id)
2400{
2401 struct irq_desc *desc = irq_to_desc(irq);
2402
2403 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2404 return;
2405
2406 if (WARN_ON(!(desc->istate & IRQS_NMI)))
2407 return;
2408
2409 kfree(__free_percpu_irq(irq, dev_id));
2410}
2411
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002412/**
2413 * setup_percpu_irq - setup a per-cpu interrupt
2414 * @irq: Interrupt line to setup
2415 * @act: irqaction for the interrupt
2416 *
2417 * Used to statically setup per-cpu interrupts in the early boot process.
2418 */
2419int setup_percpu_irq(unsigned int irq, struct irqaction *act)
2420{
2421 struct irq_desc *desc = irq_to_desc(irq);
2422 int retval;
2423
2424 if (!desc || !irq_settings_is_per_cpu_devid(desc))
2425 return -EINVAL;
2426
2427 retval = irq_chip_pm_get(&desc->irq_data);
2428 if (retval < 0)
2429 return retval;
2430
2431 retval = __setup_irq(irq, desc, act);
2432
2433 if (retval)
2434 irq_chip_pm_put(&desc->irq_data);
2435
2436 return retval;
2437}
2438
2439/**
2440 * __request_percpu_irq - allocate a percpu interrupt line
2441 * @irq: Interrupt line to allocate
2442 * @handler: Function to be called when the IRQ occurs.
2443 * @flags: Interrupt type flags (IRQF_TIMER only)
2444 * @devname: An ascii name for the claiming device
2445 * @dev_id: A percpu cookie passed back to the handler function
2446 *
2447 * This call allocates interrupt resources and enables the
2448 * interrupt on the local CPU. If the interrupt is supposed to be
2449 * enabled on other CPUs, it has to be done on each CPU using
2450 * enable_percpu_irq().
2451 *
2452 * Dev_id must be globally unique. It is a per-cpu variable, and
2453 * the handler gets called with the interrupted CPU's instance of
2454 * that variable.
2455 */
2456int __request_percpu_irq(unsigned int irq, irq_handler_t handler,
2457 unsigned long flags, const char *devname,
2458 void __percpu *dev_id)
2459{
2460 struct irqaction *action;
2461 struct irq_desc *desc;
2462 int retval;
2463
2464 if (!dev_id)
2465 return -EINVAL;
2466
2467 desc = irq_to_desc(irq);
2468 if (!desc || !irq_settings_can_request(desc) ||
2469 !irq_settings_is_per_cpu_devid(desc))
2470 return -EINVAL;
2471
2472 if (flags && flags != IRQF_TIMER)
2473 return -EINVAL;
2474
2475 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2476 if (!action)
2477 return -ENOMEM;
2478
2479 action->handler = handler;
2480 action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND;
2481 action->name = devname;
2482 action->percpu_dev_id = dev_id;
2483
2484 retval = irq_chip_pm_get(&desc->irq_data);
2485 if (retval < 0) {
2486 kfree(action);
2487 return retval;
2488 }
2489
2490 retval = __setup_irq(irq, desc, action);
2491
2492 if (retval) {
2493 irq_chip_pm_put(&desc->irq_data);
2494 kfree(action);
2495 }
2496
2497 return retval;
2498}
2499EXPORT_SYMBOL_GPL(__request_percpu_irq);
2500
2501/**
David Brazdil0f672f62019-12-10 10:32:29 +00002502 * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery
2503 * @irq: Interrupt line to allocate
2504 * @handler: Function to be called when the IRQ occurs.
2505 * @name: An ascii name for the claiming device
2506 * @dev_id: A percpu cookie passed back to the handler function
2507 *
2508 * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
2509 * have to be setup on each CPU by calling prepare_percpu_nmi() before
2510 * being enabled on the same CPU by using enable_percpu_nmi().
2511 *
2512 * Dev_id must be globally unique. It is a per-cpu variable, and
2513 * the handler gets called with the interrupted CPU's instance of
2514 * that variable.
2515 *
2516 * Interrupt lines requested for NMI delivering should have auto enabling
2517 * setting disabled.
2518 *
2519 * If the interrupt line cannot be used to deliver NMIs, function
2520 * will fail returning a negative value.
2521 */
2522int request_percpu_nmi(unsigned int irq, irq_handler_t handler,
2523 const char *name, void __percpu *dev_id)
2524{
2525 struct irqaction *action;
2526 struct irq_desc *desc;
2527 unsigned long flags;
2528 int retval;
2529
2530 if (!handler)
2531 return -EINVAL;
2532
2533 desc = irq_to_desc(irq);
2534
2535 if (!desc || !irq_settings_can_request(desc) ||
2536 !irq_settings_is_per_cpu_devid(desc) ||
2537 irq_settings_can_autoenable(desc) ||
2538 !irq_supports_nmi(desc))
2539 return -EINVAL;
2540
2541 /* The line cannot already be NMI */
2542 if (desc->istate & IRQS_NMI)
2543 return -EINVAL;
2544
2545 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2546 if (!action)
2547 return -ENOMEM;
2548
2549 action->handler = handler;
2550 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND | IRQF_NO_THREAD
2551 | IRQF_NOBALANCING;
2552 action->name = name;
2553 action->percpu_dev_id = dev_id;
2554
2555 retval = irq_chip_pm_get(&desc->irq_data);
2556 if (retval < 0)
2557 goto err_out;
2558
2559 retval = __setup_irq(irq, desc, action);
2560 if (retval)
2561 goto err_irq_setup;
2562
2563 raw_spin_lock_irqsave(&desc->lock, flags);
2564 desc->istate |= IRQS_NMI;
2565 raw_spin_unlock_irqrestore(&desc->lock, flags);
2566
2567 return 0;
2568
2569err_irq_setup:
2570 irq_chip_pm_put(&desc->irq_data);
2571err_out:
2572 kfree(action);
2573
2574 return retval;
2575}
2576
2577/**
2578 * prepare_percpu_nmi - performs CPU local setup for NMI delivery
2579 * @irq: Interrupt line to prepare for NMI delivery
2580 *
2581 * This call prepares an interrupt line to deliver NMI on the current CPU,
2582 * before that interrupt line gets enabled with enable_percpu_nmi().
2583 *
2584 * As a CPU local operation, this should be called from non-preemptible
2585 * context.
2586 *
2587 * If the interrupt line cannot be used to deliver NMIs, function
2588 * will fail returning a negative value.
2589 */
2590int prepare_percpu_nmi(unsigned int irq)
2591{
2592 unsigned long flags;
2593 struct irq_desc *desc;
2594 int ret = 0;
2595
2596 WARN_ON(preemptible());
2597
2598 desc = irq_get_desc_lock(irq, &flags,
2599 IRQ_GET_DESC_CHECK_PERCPU);
2600 if (!desc)
2601 return -EINVAL;
2602
2603 if (WARN(!(desc->istate & IRQS_NMI),
2604 KERN_ERR "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n",
2605 irq)) {
2606 ret = -EINVAL;
2607 goto out;
2608 }
2609
2610 ret = irq_nmi_setup(desc);
2611 if (ret) {
2612 pr_err("Failed to setup NMI delivery: irq %u\n", irq);
2613 goto out;
2614 }
2615
2616out:
2617 irq_put_desc_unlock(desc, flags);
2618 return ret;
2619}
2620
2621/**
2622 * teardown_percpu_nmi - undoes NMI setup of IRQ line
2623 * @irq: Interrupt line from which CPU local NMI configuration should be
2624 * removed
2625 *
2626 * This call undoes the setup done by prepare_percpu_nmi().
2627 *
2628 * IRQ line should not be enabled for the current CPU.
2629 *
2630 * As a CPU local operation, this should be called from non-preemptible
2631 * context.
2632 */
2633void teardown_percpu_nmi(unsigned int irq)
2634{
2635 unsigned long flags;
2636 struct irq_desc *desc;
2637
2638 WARN_ON(preemptible());
2639
2640 desc = irq_get_desc_lock(irq, &flags,
2641 IRQ_GET_DESC_CHECK_PERCPU);
2642 if (!desc)
2643 return;
2644
2645 if (WARN_ON(!(desc->istate & IRQS_NMI)))
2646 goto out;
2647
2648 irq_nmi_teardown(desc);
2649out:
2650 irq_put_desc_unlock(desc, flags);
2651}
2652
2653int __irq_get_irqchip_state(struct irq_data *data, enum irqchip_irq_state which,
2654 bool *state)
2655{
2656 struct irq_chip *chip;
2657 int err = -EINVAL;
2658
2659 do {
2660 chip = irq_data_get_irq_chip(data);
2661 if (chip->irq_get_irqchip_state)
2662 break;
2663#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2664 data = data->parent_data;
2665#else
2666 data = NULL;
2667#endif
2668 } while (data);
2669
2670 if (data)
2671 err = chip->irq_get_irqchip_state(data, which, state);
2672 return err;
2673}
2674
2675/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002676 * irq_get_irqchip_state - returns the irqchip state of a interrupt.
2677 * @irq: Interrupt line that is forwarded to a VM
2678 * @which: One of IRQCHIP_STATE_* the caller wants to know about
2679 * @state: a pointer to a boolean where the state is to be storeed
2680 *
2681 * This call snapshots the internal irqchip state of an
2682 * interrupt, returning into @state the bit corresponding to
2683 * stage @which
2684 *
2685 * This function should be called with preemption disabled if the
2686 * interrupt controller has per-cpu registers.
2687 */
2688int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2689 bool *state)
2690{
2691 struct irq_desc *desc;
2692 struct irq_data *data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002693 unsigned long flags;
2694 int err = -EINVAL;
2695
2696 desc = irq_get_desc_buslock(irq, &flags, 0);
2697 if (!desc)
2698 return err;
2699
2700 data = irq_desc_get_irq_data(desc);
2701
David Brazdil0f672f62019-12-10 10:32:29 +00002702 err = __irq_get_irqchip_state(data, which, state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002703
2704 irq_put_desc_busunlock(desc, flags);
2705 return err;
2706}
2707EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
2708
2709/**
2710 * irq_set_irqchip_state - set the state of a forwarded interrupt.
2711 * @irq: Interrupt line that is forwarded to a VM
2712 * @which: State to be restored (one of IRQCHIP_STATE_*)
2713 * @val: Value corresponding to @which
2714 *
2715 * This call sets the internal irqchip state of an interrupt,
2716 * depending on the value of @which.
2717 *
2718 * This function should be called with preemption disabled if the
2719 * interrupt controller has per-cpu registers.
2720 */
2721int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2722 bool val)
2723{
2724 struct irq_desc *desc;
2725 struct irq_data *data;
2726 struct irq_chip *chip;
2727 unsigned long flags;
2728 int err = -EINVAL;
2729
2730 desc = irq_get_desc_buslock(irq, &flags, 0);
2731 if (!desc)
2732 return err;
2733
2734 data = irq_desc_get_irq_data(desc);
2735
2736 do {
2737 chip = irq_data_get_irq_chip(data);
2738 if (chip->irq_set_irqchip_state)
2739 break;
2740#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2741 data = data->parent_data;
2742#else
2743 data = NULL;
2744#endif
2745 } while (data);
2746
2747 if (data)
2748 err = chip->irq_set_irqchip_state(data, which, val);
2749
2750 irq_put_desc_busunlock(desc, flags);
2751 return err;
2752}
2753EXPORT_SYMBOL_GPL(irq_set_irqchip_state);