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