blob: c9f3d95837f994a004c6283c1b7c9cf347fb656b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * linux/kernel/irq/manage.c
3 *
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006 Thomas Gleixner
6 *
7 * This file contains driver APIs to the irq subsystem.
8 */
9
10#include <linux/irq.h>
11#include <linux/kthread.h>
12#include <linux/module.h>
13#include <linux/random.h>
14#include <linux/interrupt.h>
15#include <linux/slab.h>
16#include <linux/sched.h>
17
18#include "internals.h"
19
20#ifdef CONFIG_IRQ_FORCED_THREADING
21# ifndef CONFIG_PREEMPT_RT_BASE
22__read_mostly bool force_irqthreads;
23
24static int __init setup_forced_irqthreads(char *arg)
25{
26 force_irqthreads = true;
27 return 0;
28}
29early_param("threadirqs", setup_forced_irqthreads);
30# endif
31#endif
32
33/**
34 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
35 * @irq: interrupt number to wait for
36 *
37 * This function waits for any pending IRQ handlers for this interrupt
38 * to complete before returning. If you use this function while
39 * holding a resource the IRQ handler may need you will deadlock.
40 *
41 * This function may be called - with care - from IRQ context.
42 */
43void synchronize_irq(unsigned int irq)
44{
45 struct irq_desc *desc = irq_to_desc(irq);
46 bool inprogress;
47
48 if (!desc)
49 return;
50
51 do {
52 unsigned long flags;
53
54 /*
55 * Wait until we're out of the critical section. This might
56 * give the wrong answer due to the lack of memory barriers.
57 */
58 while (irqd_irq_inprogress(&desc->irq_data))
59 cpu_relax();
60
61 /* Ok, that indicated we're done: double-check carefully. */
62 raw_spin_lock_irqsave(&desc->lock, flags);
63 inprogress = irqd_irq_inprogress(&desc->irq_data);
64 raw_spin_unlock_irqrestore(&desc->lock, flags);
65
66 /* Oops, that failed? */
67 } while (inprogress);
68
69 /*
70 * We made sure that no hardirq handler is running. Now verify
71 * that no threaded handlers are active.
72 */
73 wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
74}
75EXPORT_SYMBOL(synchronize_irq);
76
77#ifdef CONFIG_SMP
78cpumask_var_t irq_default_affinity;
79
80/**
81 * irq_can_set_affinity - Check if the affinity of a given irq can be set
82 * @irq: Interrupt to check
83 *
84 */
85int irq_can_set_affinity(unsigned int irq)
86{
87 struct irq_desc *desc = irq_to_desc(irq);
88
89 if (!desc || !irqd_can_balance(&desc->irq_data) ||
90 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
91 return 0;
92
93 return 1;
94}
95
96/**
97 * irq_set_thread_affinity - Notify irq threads to adjust affinity
98 * @desc: irq descriptor which has affitnity changed
99 *
100 * We just set IRQTF_AFFINITY and delegate the affinity setting
101 * to the interrupt thread itself. We can not call
102 * set_cpus_allowed_ptr() here as we hold desc->lock and this
103 * code can be called from hard interrupt context.
104 */
105void irq_set_thread_affinity(struct irq_desc *desc)
106{
107 struct irqaction *action = desc->action;
108
109 while (action) {
110 if (action->thread)
111 set_bit(IRQTF_AFFINITY, &action->thread_flags);
112 action = action->next;
113 }
114}
115
116#ifdef CONFIG_GENERIC_PENDING_IRQ
117static inline bool irq_can_move_pcntxt(struct irq_data *data)
118{
119 return irqd_can_move_in_process_context(data);
120}
121static inline bool irq_move_pending(struct irq_data *data)
122{
123 return irqd_is_setaffinity_pending(data);
124}
125static inline void
126irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
127{
128 cpumask_copy(desc->pending_mask, mask);
129}
130static inline void
131irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
132{
133 cpumask_copy(mask, desc->pending_mask);
134}
135#else
136static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
137static inline bool irq_move_pending(struct irq_data *data) { return false; }
138static inline void
139irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
140static inline void
141irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
142#endif
143
144#ifdef CONFIG_PREEMPT_RT_FULL
145static void _irq_affinity_notify(struct irq_affinity_notify *notify);
146static struct task_struct *set_affinity_helper;
147static LIST_HEAD(affinity_list);
148static DEFINE_RAW_SPINLOCK(affinity_list_lock);
149
150static int set_affinity_thread(void *unused)
151{
152 while (1) {
153 struct irq_affinity_notify *notify;
154 int empty;
155
156 set_current_state(TASK_INTERRUPTIBLE);
157
158 raw_spin_lock_irq(&affinity_list_lock);
159 empty = list_empty(&affinity_list);
160 raw_spin_unlock_irq(&affinity_list_lock);
161
162 if (empty)
163 schedule();
164 if (kthread_should_stop())
165 break;
166 set_current_state(TASK_RUNNING);
167try_next:
168 notify = NULL;
169
170 raw_spin_lock_irq(&affinity_list_lock);
171 if (!list_empty(&affinity_list)) {
172 notify = list_first_entry(&affinity_list,
173 struct irq_affinity_notify, list);
174 list_del_init(&notify->list);
175 }
176 raw_spin_unlock_irq(&affinity_list_lock);
177
178 if (!notify)
179 continue;
180 _irq_affinity_notify(notify);
181 goto try_next;
182 }
183 return 0;
184}
185
186static void init_helper_thread(void)
187{
188 if (set_affinity_helper)
189 return;
190 set_affinity_helper = kthread_run(set_affinity_thread, NULL,
191 "affinity-cb");
192 WARN_ON(IS_ERR(set_affinity_helper));
193}
194#else
195
196static inline void init_helper_thread(void) { }
197
198#endif
199
200int __irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask)
201{
202 struct irq_chip *chip = irq_data_get_irq_chip(data);
203 struct irq_desc *desc = irq_data_to_desc(data);
204 int ret = 0;
205
206 if (!chip || !chip->irq_set_affinity)
207 return -EINVAL;
208
209 if (irq_can_move_pcntxt(data)) {
210 ret = chip->irq_set_affinity(data, mask, false);
211 switch (ret) {
212 case IRQ_SET_MASK_OK:
213 cpumask_copy(data->affinity, mask);
214 case IRQ_SET_MASK_OK_NOCOPY:
215 irq_set_thread_affinity(desc);
216 ret = 0;
217 }
218 } else {
219 irqd_set_move_pending(data);
220 irq_copy_pending(desc, mask);
221 }
222
223 if (desc->affinity_notify) {
224 kref_get(&desc->affinity_notify->kref);
225
226#ifdef CONFIG_PREEMPT_RT_FULL
227 raw_spin_lock(&affinity_list_lock);
228 if (list_empty(&desc->affinity_notify->list))
229 list_add_tail(&affinity_list,
230 &desc->affinity_notify->list);
231 raw_spin_unlock(&affinity_list_lock);
232 wake_up_process(set_affinity_helper);
233#else
234 schedule_work(&desc->affinity_notify->work);
235#endif
236 }
237 irqd_set(data, IRQD_AFFINITY_SET);
238
239 return ret;
240}
241
242/**
243 * irq_set_affinity - Set the irq affinity of a given irq
244 * @irq: Interrupt to set affinity
245 * @mask: cpumask
246 *
247 */
248int irq_set_affinity(unsigned int irq, const struct cpumask *mask)
249{
250 struct irq_desc *desc = irq_to_desc(irq);
251 unsigned long flags;
252 int ret;
253
254 if (!desc)
255 return -EINVAL;
256
257 raw_spin_lock_irqsave(&desc->lock, flags);
258 ret = __irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask);
259 raw_spin_unlock_irqrestore(&desc->lock, flags);
260 return ret;
261}
262
263int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
264{
265 unsigned long flags;
266 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
267
268 if (!desc)
269 return -EINVAL;
270 desc->affinity_hint = m;
271 irq_put_desc_unlock(desc, flags);
272 return 0;
273}
274EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
275
276static void _irq_affinity_notify(struct irq_affinity_notify *notify)
277{
278 struct irq_desc *desc = irq_to_desc(notify->irq);
279 cpumask_var_t cpumask;
280 unsigned long flags;
281
282 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
283 goto out;
284
285 raw_spin_lock_irqsave(&desc->lock, flags);
286 if (irq_move_pending(&desc->irq_data))
287 irq_get_pending(cpumask, desc);
288 else
289 cpumask_copy(cpumask, desc->irq_data.affinity);
290 raw_spin_unlock_irqrestore(&desc->lock, flags);
291
292 notify->notify(notify, cpumask);
293
294 free_cpumask_var(cpumask);
295out:
296 kref_put(&notify->kref, notify->release);
297}
298
299static 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 _irq_affinity_notify(notify);
304}
305
306/**
307 * irq_set_affinity_notifier - control notification of IRQ affinity changes
308 * @irq: Interrupt for which to enable/disable notification
309 * @notify: Context for notification, or %NULL to disable
310 * notification. Function pointers must be initialised;
311 * the other fields will be initialised by this function.
312 *
313 * Must be called in process context. Notification may only be enabled
314 * after the IRQ is allocated and must be disabled before the IRQ is
315 * freed using free_irq().
316 */
317int
318irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
319{
320 struct irq_desc *desc = irq_to_desc(irq);
321 struct irq_affinity_notify *old_notify;
322 unsigned long flags;
323
324 /* The release function is promised process context */
325 might_sleep();
326
327 if (!desc)
328 return -EINVAL;
329
330 /* Complete initialisation of *notify */
331 if (notify) {
332 notify->irq = irq;
333 kref_init(&notify->kref);
334 INIT_WORK(&notify->work, irq_affinity_notify);
335 INIT_LIST_HEAD(&notify->list);
336 init_helper_thread();
337 }
338
339 raw_spin_lock_irqsave(&desc->lock, flags);
340 old_notify = desc->affinity_notify;
341 desc->affinity_notify = notify;
342 raw_spin_unlock_irqrestore(&desc->lock, flags);
343
344 if (old_notify)
345 kref_put(&old_notify->kref, old_notify->release);
346
347 return 0;
348}
349EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
350
351#ifndef CONFIG_AUTO_IRQ_AFFINITY
352/*
353 * Generic version of the affinity autoselector.
354 */
355static int
356setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
357{
358 struct irq_chip *chip = irq_desc_get_chip(desc);
359 struct cpumask *set = irq_default_affinity;
360 int ret, node = desc->irq_data.node;
361
362 /* Excludes PER_CPU and NO_BALANCE interrupts */
363 if (!irq_can_set_affinity(irq))
364 return 0;
365
366 /*
367 * Preserve an userspace affinity setup, but make sure that
368 * one of the targets is online.
369 */
370 if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
371 if (cpumask_intersects(desc->irq_data.affinity,
372 cpu_online_mask))
373 set = desc->irq_data.affinity;
374 else
375 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
376 }
377
378 cpumask_and(mask, cpu_online_mask, set);
379 if (node != NUMA_NO_NODE) {
380 const struct cpumask *nodemask = cpumask_of_node(node);
381
382 /* make sure at least one of the cpus in nodemask is online */
383 if (cpumask_intersects(mask, nodemask))
384 cpumask_and(mask, mask, nodemask);
385 }
386 ret = chip->irq_set_affinity(&desc->irq_data, mask, false);
387 switch (ret) {
388 case IRQ_SET_MASK_OK:
389 cpumask_copy(desc->irq_data.affinity, mask);
390 case IRQ_SET_MASK_OK_NOCOPY:
391 irq_set_thread_affinity(desc);
392 }
393 return 0;
394}
395#else
396static inline int
397setup_affinity(unsigned int irq, struct irq_desc *d, struct cpumask *mask)
398{
399 return irq_select_affinity(irq);
400}
401#endif
402
403/*
404 * Called when affinity is set via /proc/irq
405 */
406int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
407{
408 struct irq_desc *desc = irq_to_desc(irq);
409 unsigned long flags;
410 int ret;
411
412 raw_spin_lock_irqsave(&desc->lock, flags);
413 ret = setup_affinity(irq, desc, mask);
414 raw_spin_unlock_irqrestore(&desc->lock, flags);
415 return ret;
416}
417
418#else
419static inline int
420setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
421{
422 return 0;
423}
424#endif
425
426void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
427{
428 if (suspend) {
429 if (!desc->action || (desc->action->flags & IRQF_NO_SUSPEND))
430 return;
431 desc->istate |= IRQS_SUSPENDED;
432 }
433
434 if (!desc->depth++)
435 irq_disable(desc);
436}
437
438static int __disable_irq_nosync(unsigned int irq)
439{
440 unsigned long flags;
441 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
442
443 if (!desc)
444 return -EINVAL;
445 __disable_irq(desc, irq, false);
446 irq_put_desc_busunlock(desc, flags);
447 return 0;
448}
449
450/**
451 * disable_irq_nosync - disable an irq without waiting
452 * @irq: Interrupt to disable
453 *
454 * Disable the selected interrupt line. Disables and Enables are
455 * nested.
456 * Unlike disable_irq(), this function does not ensure existing
457 * instances of the IRQ handler have completed before returning.
458 *
459 * This function may be called from IRQ context.
460 */
461void disable_irq_nosync(unsigned int irq)
462{
463 __disable_irq_nosync(irq);
464}
465EXPORT_SYMBOL(disable_irq_nosync);
466
467/**
468 * disable_irq - disable an irq and wait for completion
469 * @irq: Interrupt to disable
470 *
471 * Disable the selected interrupt line. Enables and Disables are
472 * nested.
473 * This function waits for any pending IRQ handlers for this interrupt
474 * to complete before returning. If you use this function while
475 * holding a resource the IRQ handler may need you will deadlock.
476 *
477 * This function may be called - with care - from IRQ context.
478 */
479void disable_irq(unsigned int irq)
480{
481 if (!__disable_irq_nosync(irq))
482 synchronize_irq(irq);
483}
484EXPORT_SYMBOL(disable_irq);
485
486void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
487{
488 if (resume) {
489 if (!(desc->istate & IRQS_SUSPENDED)) {
490 if (!desc->action)
491 return;
492 if (!(desc->action->flags & IRQF_FORCE_RESUME))
493 return;
494 /* Pretend that it got disabled ! */
495 desc->depth++;
496 }
497 desc->istate &= ~IRQS_SUSPENDED;
498 }
499
500 switch (desc->depth) {
501 case 0:
502 err_out:
503 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
504 break;
505 case 1: {
506 if (desc->istate & IRQS_SUSPENDED)
507 goto err_out;
508 /* Prevent probing on this irq: */
509 irq_settings_set_noprobe(desc);
510 irq_enable(desc);
511 check_irq_resend(desc, irq);
512 /* fall-through */
513 }
514 default:
515 desc->depth--;
516 }
517}
518
519/**
520 * enable_irq - enable handling of an irq
521 * @irq: Interrupt to enable
522 *
523 * Undoes the effect of one call to disable_irq(). If this
524 * matches the last disable, processing of interrupts on this
525 * IRQ line is re-enabled.
526 *
527 * This function may be called from IRQ context only when
528 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
529 */
530void enable_irq(unsigned int irq)
531{
532 unsigned long flags;
533 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
534
535 if (!desc)
536 return;
537 if (WARN(!desc->irq_data.chip,
538 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
539 goto out;
540
541 __enable_irq(desc, irq, false);
542out:
543 irq_put_desc_busunlock(desc, flags);
544}
545EXPORT_SYMBOL(enable_irq);
546
547static int set_irq_wake_real(unsigned int irq, unsigned int on)
548{
549 struct irq_desc *desc = irq_to_desc(irq);
550 int ret = -ENXIO;
551
552 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
553 return 0;
554
555 if (desc->irq_data.chip->irq_set_wake)
556 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
557
558 return ret;
559}
560
561/**
562 * irq_set_irq_wake - control irq power management wakeup
563 * @irq: interrupt to control
564 * @on: enable/disable power management wakeup
565 *
566 * Enable/disable power management wakeup mode, which is
567 * disabled by default. Enables and disables must match,
568 * just as they match for non-wakeup mode support.
569 *
570 * Wakeup mode lets this IRQ wake the system from sleep
571 * states like "suspend to RAM".
572 */
573int irq_set_irq_wake(unsigned int irq, unsigned int on)
574{
575 unsigned long flags;
576 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
577 int ret = 0;
578
579 if (!desc)
580 return -EINVAL;
581
582 /* wakeup-capable irqs can be shared between drivers that
583 * don't need to have the same sleep mode behaviors.
584 */
585 if (on) {
586 if (desc->wake_depth++ == 0) {
587 ret = set_irq_wake_real(irq, on);
588 if (ret)
589 desc->wake_depth = 0;
590 else
591 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
592 }
593 } else {
594 if (desc->wake_depth == 0) {
595 WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
596 } else if (--desc->wake_depth == 0) {
597 ret = set_irq_wake_real(irq, on);
598 if (ret)
599 desc->wake_depth = 1;
600 else
601 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
602 }
603 }
604 irq_put_desc_busunlock(desc, flags);
605 return ret;
606}
607EXPORT_SYMBOL(irq_set_irq_wake);
608
609/*
610 * Internal function that tells the architecture code whether a
611 * particular irq has been exclusively allocated or is available
612 * for driver use.
613 */
614int can_request_irq(unsigned int irq, unsigned long irqflags)
615{
616 unsigned long flags;
617 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
618 int canrequest = 0;
619
620 if (!desc)
621 return 0;
622
623 if (irq_settings_can_request(desc)) {
624 if (!desc->action ||
625 irqflags & desc->action->flags & IRQF_SHARED)
626 canrequest = 1;
627 }
628 irq_put_desc_unlock(desc, flags);
629 return canrequest;
630}
631
632int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
633 unsigned long flags)
634{
635 struct irq_chip *chip = desc->irq_data.chip;
636 int ret, unmask = 0;
637
638 if (!chip || !chip->irq_set_type) {
639 /*
640 * IRQF_TRIGGER_* but the PIC does not support multiple
641 * flow-types?
642 */
643 pr_debug("No set_type function for IRQ %d (%s)\n", irq,
644 chip ? (chip->name ? : "unknown") : "unknown");
645 return 0;
646 }
647
648 flags &= IRQ_TYPE_SENSE_MASK;
649
650 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
651 if (!irqd_irq_masked(&desc->irq_data))
652 mask_irq(desc);
653 if (!irqd_irq_disabled(&desc->irq_data))
654 unmask = 1;
655 }
656
657 /* caller masked out all except trigger mode flags */
658 ret = chip->irq_set_type(&desc->irq_data, flags);
659
660 switch (ret) {
661 case IRQ_SET_MASK_OK:
662 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
663 irqd_set(&desc->irq_data, flags);
664
665 case IRQ_SET_MASK_OK_NOCOPY:
666 flags = irqd_get_trigger_type(&desc->irq_data);
667 irq_settings_set_trigger_mask(desc, flags);
668 irqd_clear(&desc->irq_data, IRQD_LEVEL);
669 irq_settings_clr_level(desc);
670 if (flags & IRQ_TYPE_LEVEL_MASK) {
671 irq_settings_set_level(desc);
672 irqd_set(&desc->irq_data, IRQD_LEVEL);
673 }
674
675 ret = 0;
676 break;
677 default:
678 pr_err("setting trigger mode %lu for irq %u failed (%pF)\n",
679 flags, irq, chip->irq_set_type);
680 }
681 if (unmask)
682 unmask_irq(desc);
683 return ret;
684}
685
686/*
687 * Default primary interrupt handler for threaded interrupts. Is
688 * assigned as primary handler when request_threaded_irq is called
689 * with handler == NULL. Useful for oneshot interrupts.
690 */
691static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
692{
693 return IRQ_WAKE_THREAD;
694}
695
696/*
697 * Primary handler for nested threaded interrupts. Should never be
698 * called.
699 */
700static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
701{
702 WARN(1, "Primary handler called for nested irq %d\n", irq);
703 return IRQ_NONE;
704}
705
706static int irq_wait_for_interrupt(struct irqaction *action)
707{
708 set_current_state(TASK_INTERRUPTIBLE);
709
710 while (!kthread_should_stop()) {
711
712 if (test_and_clear_bit(IRQTF_RUNTHREAD,
713 &action->thread_flags)) {
714 __set_current_state(TASK_RUNNING);
715 return 0;
716 }
717 schedule();
718 set_current_state(TASK_INTERRUPTIBLE);
719 }
720 __set_current_state(TASK_RUNNING);
721 return -1;
722}
723
724/*
725 * Oneshot interrupts keep the irq line masked until the threaded
726 * handler finished. unmask if the interrupt has not been disabled and
727 * is marked MASKED.
728 */
729static void irq_finalize_oneshot(struct irq_desc *desc,
730 struct irqaction *action)
731{
732 if (!(desc->istate & IRQS_ONESHOT))
733 return;
734again:
735 chip_bus_lock(desc);
736 raw_spin_lock_irq(&desc->lock);
737
738 /*
739 * Implausible though it may be we need to protect us against
740 * the following scenario:
741 *
742 * The thread is faster done than the hard interrupt handler
743 * on the other CPU. If we unmask the irq line then the
744 * interrupt can come in again and masks the line, leaves due
745 * to IRQS_INPROGRESS and the irq line is masked forever.
746 *
747 * This also serializes the state of shared oneshot handlers
748 * versus "desc->threads_onehsot |= action->thread_mask;" in
749 * irq_wake_thread(). See the comment there which explains the
750 * serialization.
751 */
752 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
753 raw_spin_unlock_irq(&desc->lock);
754 chip_bus_sync_unlock(desc);
755 cpu_relax();
756 goto again;
757 }
758
759 /*
760 * Now check again, whether the thread should run. Otherwise
761 * we would clear the threads_oneshot bit of this thread which
762 * was just set.
763 */
764 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
765 goto out_unlock;
766
767 desc->threads_oneshot &= ~action->thread_mask;
768
769 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
770 irqd_irq_masked(&desc->irq_data))
771 unmask_irq(desc);
772
773out_unlock:
774 raw_spin_unlock_irq(&desc->lock);
775 chip_bus_sync_unlock(desc);
776}
777
778#ifdef CONFIG_SMP
779/*
780 * Check whether we need to chasnge the affinity of the interrupt thread.
781 */
782static void
783irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
784{
785 cpumask_var_t mask;
786 bool valid = true;
787
788 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
789 return;
790
791 /*
792 * In case we are out of memory we set IRQTF_AFFINITY again and
793 * try again next time
794 */
795 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
796 set_bit(IRQTF_AFFINITY, &action->thread_flags);
797 return;
798 }
799
800 raw_spin_lock_irq(&desc->lock);
801 /*
802 * This code is triggered unconditionally. Check the affinity
803 * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
804 */
805 if (desc->irq_data.affinity)
806 cpumask_copy(mask, desc->irq_data.affinity);
807 else
808 valid = false;
809 raw_spin_unlock_irq(&desc->lock);
810
811 if (valid)
812 set_cpus_allowed_ptr(current, mask);
813 free_cpumask_var(mask);
814}
815#else
816static inline void
817irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
818#endif
819
820/*
821 * Interrupts which are not explicitely requested as threaded
822 * interrupts rely on the implicit bh/preempt disable of the hard irq
823 * context. So we need to disable bh here to avoid deadlocks and other
824 * side effects.
825 */
826static irqreturn_t
827irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
828{
829 irqreturn_t ret;
830
831 //local_bh_disable();
832 ret = action->thread_fn(action->irq, action->dev_id);
833 irq_finalize_oneshot(desc, action);
834 /*
835 * Interrupts which have real time requirements can be set up
836 * to avoid softirq processing in the thread handler. This is
837 * safe as these interrupts do not raise soft interrupts.
838 */
839 //if (irq_settings_no_softirq_call(desc))
840 // _local_bh_enable();
841 //else
842 // local_bh_enable();
843 return ret;
844}
845
846/*
847 * Interrupts explicitely requested as threaded interupts want to be
848 * preemtible - many of them need to sleep and wait for slow busses to
849 * complete.
850 */
851static irqreturn_t irq_thread_fn(struct irq_desc *desc,
852 struct irqaction *action)
853{
854 irqreturn_t ret;
855
856 ret = action->thread_fn(action->irq, action->dev_id);
857 irq_finalize_oneshot(desc, action);
858 return ret;
859}
860
861static void wake_threads_waitq(struct irq_desc *desc)
862{
863 if (atomic_dec_and_test(&desc->threads_active))
864 wake_up(&desc->wait_for_threads);
865}
866
867/*
868 * Interrupt handler thread
869 */
870static int irq_thread(void *data)
871{
872 struct irqaction *action = data;
873 struct irq_desc *desc = irq_to_desc(action->irq);
874 irqreturn_t (*handler_fn)(struct irq_desc *desc,
875 struct irqaction *action);
876
877 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
878 &action->thread_flags))
879 handler_fn = irq_forced_thread_fn;
880 else
881 handler_fn = irq_thread_fn;
882
883 current->irq_thread = 1;
884
885 while (!irq_wait_for_interrupt(action)) {
886 irqreturn_t action_ret;
887
888 irq_thread_check_affinity(desc, action);
889
890 action_ret = handler_fn(desc, action);
891 if (action_ret == IRQ_HANDLED)
892 atomic_inc(&desc->threads_handled);
893
894#ifdef CONFIG_PREEMPT_RT_FULL
895 migrate_disable();
896 add_interrupt_randomness(action->irq, 0,
897 desc->random_ip ^ (u64) action);
898 migrate_enable();
899#endif
900 wake_threads_waitq(desc);
901 }
902
903 /*
904 * This is the regular exit path. __free_irq() is stopping the
905 * thread via kthread_stop() after calling
906 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
907 * oneshot mask bit can be set. We cannot verify that as we
908 * cannot touch the oneshot mask at this point anymore as
909 * __setup_irq() might have given out currents thread_mask
910 * again.
911 *
912 * Clear irq_thread. Otherwise exit_irq_thread() would make
913 * fuzz about an active irq thread going into nirvana.
914 */
915 current->irq_thread = 0;
916 return 0;
917}
918
919/*
920 * Called from do_exit()
921 */
922void exit_irq_thread(void)
923{
924 struct task_struct *tsk = current;
925 struct irq_desc *desc;
926 struct irqaction *action;
927
928 if (!tsk->irq_thread)
929 return;
930
931 action = kthread_data(tsk);
932
933 printk(KERN_ERR
934 "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
935 tsk->comm ? tsk->comm : "", tsk->pid, action->irq);
936
937 desc = irq_to_desc(action->irq);
938
939 /*
940 * If IRQTF_RUNTHREAD is set, we need to decrement
941 * desc->threads_active and wake possible waiters.
942 */
943 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
944 wake_threads_waitq(desc);
945
946 /* Prevent a stale desc->threads_oneshot */
947 irq_finalize_oneshot(desc, action);
948}
949
950static void irq_setup_forced_threading(struct irqaction *new)
951{
952 if (!force_irqthreads)
953 return;
954 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
955 return;
956
957 new->flags |= IRQF_ONESHOT;
958
959 if (!new->thread_fn) {
960 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
961 new->thread_fn = new->handler;
962 new->handler = irq_default_primary_handler;
963 }
964}
965
966/*
967 * Internal function to register an irqaction - typically used to
968 * allocate special interrupts that are part of the architecture.
969 */
970static int
971__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
972{
973 struct irqaction *old, **old_ptr;
974 const char *old_name = NULL;
975 unsigned long flags, thread_mask = 0;
976 int ret, nested, shared = 0;
977 cpumask_var_t mask;
978
979 if (!desc)
980 return -EINVAL;
981
982 if (desc->irq_data.chip == &no_irq_chip)
983 return -ENOSYS;
984 if (!try_module_get(desc->owner))
985 return -ENODEV;
986
987 /*
988 * Check whether the interrupt nests into another interrupt
989 * thread.
990 */
991 nested = irq_settings_is_nested_thread(desc);
992 if (nested) {
993 if (!new->thread_fn) {
994 ret = -EINVAL;
995 goto out_mput;
996 }
997 /*
998 * Replace the primary handler which was provided from
999 * the driver for non nested interrupt handling by the
1000 * dummy function which warns when called.
1001 */
1002 new->handler = irq_nested_primary_handler;
1003 } else {
1004 if (irq_settings_can_thread(desc))
1005 irq_setup_forced_threading(new);
1006 }
1007
1008 /*
1009 * Create a handler thread when a thread function is supplied
1010 * and the interrupt does not nest into another interrupt
1011 * thread.
1012 */
1013 if (new->thread_fn && !nested) {
1014 struct task_struct *t;
1015 static const struct sched_param param = {
1016 .sched_priority = MAX_USER_RT_PRIO/2,
1017 };
1018 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1019 new->name);
1020 if (IS_ERR(t)) {
1021 ret = PTR_ERR(t);
1022 goto out_mput;
1023 }
1024
1025 sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1026
1027 /*
1028 * We keep the reference to the task struct even if
1029 * the thread dies to avoid that the interrupt code
1030 * references an already freed task_struct.
1031 */
1032 get_task_struct(t);
1033 new->thread = t;
1034 /*
1035 * Tell the thread to set its affinity. This is
1036 * important for shared interrupt handlers as we do
1037 * not invoke setup_affinity() for the secondary
1038 * handlers as everything is already set up. Even for
1039 * interrupts marked with IRQF_NO_BALANCE this is
1040 * correct as we want the thread to move to the cpu(s)
1041 * on which the requesting code placed the interrupt.
1042 */
1043 set_bit(IRQTF_AFFINITY, &new->thread_flags);
1044 }
1045
1046 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1047 ret = -ENOMEM;
1048 goto out_thread;
1049 }
1050
1051 /*
1052 * The following block of code has to be executed atomically
1053 */
1054 raw_spin_lock_irqsave(&desc->lock, flags);
1055 old_ptr = &desc->action;
1056 old = *old_ptr;
1057 if (old) {
1058 /*
1059 * Can't share interrupts unless both agree to and are
1060 * the same type (level, edge, polarity). So both flag
1061 * fields must have IRQF_SHARED set and the bits which
1062 * set the trigger type must match. Also all must
1063 * agree on ONESHOT.
1064 */
1065 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1066 ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
1067 ((old->flags ^ new->flags) & IRQF_ONESHOT)) {
1068 old_name = old->name;
1069 goto mismatch;
1070 }
1071
1072 /* All handlers must agree on per-cpuness */
1073 if ((old->flags & IRQF_PERCPU) !=
1074 (new->flags & IRQF_PERCPU))
1075 goto mismatch;
1076
1077 /* add new interrupt at end of irq queue */
1078 do {
1079 /*
1080 * Or all existing action->thread_mask bits,
1081 * so we can find the next zero bit for this
1082 * new action.
1083 */
1084 thread_mask |= old->thread_mask;
1085 old_ptr = &old->next;
1086 old = *old_ptr;
1087 } while (old);
1088 shared = 1;
1089 }
1090
1091 /*
1092 * Setup the thread mask for this irqaction for ONESHOT. For
1093 * !ONESHOT irqs the thread mask is 0 so we can avoid a
1094 * conditional in irq_wake_thread().
1095 */
1096 if (new->flags & IRQF_ONESHOT) {
1097 /*
1098 * Unlikely to have 32 resp 64 irqs sharing one line,
1099 * but who knows.
1100 */
1101 if (thread_mask == ~0UL) {
1102 ret = -EBUSY;
1103 goto out_mask;
1104 }
1105 /*
1106 * The thread_mask for the action is or'ed to
1107 * desc->thread_active to indicate that the
1108 * IRQF_ONESHOT thread handler has been woken, but not
1109 * yet finished. The bit is cleared when a thread
1110 * completes. When all threads of a shared interrupt
1111 * line have completed desc->threads_active becomes
1112 * zero and the interrupt line is unmasked. See
1113 * handle.c:irq_wake_thread() for further information.
1114 *
1115 * If no thread is woken by primary (hard irq context)
1116 * interrupt handlers, then desc->threads_active is
1117 * also checked for zero to unmask the irq line in the
1118 * affected hard irq flow handlers
1119 * (handle_[fasteoi|level]_irq).
1120 *
1121 * The new action gets the first zero bit of
1122 * thread_mask assigned. See the loop above which or's
1123 * all existing action->thread_mask bits.
1124 */
1125 new->thread_mask = 1 << ffz(thread_mask);
1126 }
1127
1128 if (!shared) {
1129 init_waitqueue_head(&desc->wait_for_threads);
1130
1131 /* Setup the type (level, edge polarity) if configured: */
1132 if (new->flags & IRQF_TRIGGER_MASK) {
1133 ret = __irq_set_trigger(desc, irq,
1134 new->flags & IRQF_TRIGGER_MASK);
1135
1136 if (ret)
1137 goto out_mask;
1138 }
1139
1140 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1141 IRQS_ONESHOT | IRQS_WAITING);
1142 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1143
1144 if (new->flags & IRQF_PERCPU) {
1145 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1146 irq_settings_set_per_cpu(desc);
1147 }
1148
1149 if (new->flags & IRQF_ONESHOT)
1150 desc->istate |= IRQS_ONESHOT;
1151
1152 if (irq_settings_can_autoenable(desc))
1153 irq_startup(desc, true);
1154 else
1155 /* Undo nested disables: */
1156 desc->depth = 1;
1157
1158 /* Exclude IRQ from balancing if requested */
1159 if (new->flags & IRQF_NOBALANCING) {
1160 irq_settings_set_no_balancing(desc);
1161 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1162 }
1163
1164 if (new->flags & IRQF_NO_SOFTIRQ_CALL)
1165 irq_settings_set_no_softirq_call(desc);
1166
1167 /* Set default affinity mask once everything is setup */
1168 setup_affinity(irq, desc, mask);
1169
1170 } else if (new->flags & IRQF_TRIGGER_MASK) {
1171 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1172 unsigned int omsk = irq_settings_get_trigger_mask(desc);
1173
1174 if (nmsk != omsk)
1175 /* hope the handler works with current trigger mode */
1176 pr_warning("IRQ %d uses trigger mode %u; requested %u\n",
1177 irq, nmsk, omsk);
1178 }
1179
1180 new->irq = irq;
1181 *old_ptr = new;
1182
1183 /* Reset broken irq detection when installing new handler */
1184 desc->irq_count = 0;
1185 desc->irqs_unhandled = 0;
1186
1187 /*
1188 * Check whether we disabled the irq via the spurious handler
1189 * before. Reenable it and give it another chance.
1190 */
1191 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1192 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1193 __enable_irq(desc, irq, false);
1194 }
1195
1196 raw_spin_unlock_irqrestore(&desc->lock, flags);
1197
1198 /*
1199 * Strictly no need to wake it up, but hung_task complains
1200 * when no hard interrupt wakes the thread up.
1201 */
1202 if (new->thread)
1203 wake_up_process(new->thread);
1204
1205 register_irq_proc(irq, desc);
1206 new->dir = NULL;
1207 register_handler_proc(irq, new);
1208 free_cpumask_var(mask);
1209
1210 return 0;
1211
1212mismatch:
1213#ifdef CONFIG_DEBUG_SHIRQ
1214 if (!(new->flags & IRQF_PROBE_SHARED)) {
1215 printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
1216 if (old_name)
1217 printk(KERN_ERR "current handler: %s\n", old_name);
1218 dump_stack();
1219 }
1220#endif
1221 ret = -EBUSY;
1222
1223out_mask:
1224 raw_spin_unlock_irqrestore(&desc->lock, flags);
1225 free_cpumask_var(mask);
1226
1227out_thread:
1228 if (new->thread) {
1229 struct task_struct *t = new->thread;
1230
1231 new->thread = NULL;
1232 kthread_stop(t);
1233 put_task_struct(t);
1234 }
1235out_mput:
1236 module_put(desc->owner);
1237 return ret;
1238}
1239
1240/**
1241 * setup_irq - setup an interrupt
1242 * @irq: Interrupt line to setup
1243 * @act: irqaction for the interrupt
1244 *
1245 * Used to statically setup interrupts in the early boot process.
1246 */
1247int setup_irq(unsigned int irq, struct irqaction *act)
1248{
1249 int retval;
1250 struct irq_desc *desc = irq_to_desc(irq);
1251
1252 if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1253 return -EINVAL;
1254 chip_bus_lock(desc);
1255 retval = __setup_irq(irq, desc, act);
1256 chip_bus_sync_unlock(desc);
1257
1258 return retval;
1259}
1260EXPORT_SYMBOL_GPL(setup_irq);
1261
1262/*
1263 * Internal function to unregister an irqaction - used to free
1264 * regular and special interrupts that are part of the architecture.
1265 */
1266static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
1267{
1268 struct irq_desc *desc = irq_to_desc(irq);
1269 struct irqaction *action, **action_ptr;
1270 unsigned long flags;
1271
1272 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1273
1274 if (!desc)
1275 return NULL;
1276
1277 raw_spin_lock_irqsave(&desc->lock, flags);
1278
1279 /*
1280 * There can be multiple actions per IRQ descriptor, find the right
1281 * one based on the dev_id:
1282 */
1283 action_ptr = &desc->action;
1284 for (;;) {
1285 action = *action_ptr;
1286
1287 if (!action) {
1288 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1289 raw_spin_unlock_irqrestore(&desc->lock, flags);
1290
1291 return NULL;
1292 }
1293
1294 if (action->dev_id == dev_id)
1295 break;
1296 action_ptr = &action->next;
1297 }
1298
1299 /* Found it - now remove it from the list of entries: */
1300 *action_ptr = action->next;
1301
1302 /* Currently used only by UML, might disappear one day: */
1303#ifdef CONFIG_IRQ_RELEASE_METHOD
1304 if (desc->irq_data.chip->release)
1305 desc->irq_data.chip->release(irq, dev_id);
1306#endif
1307
1308 /* If this was the last handler, shut down the IRQ line: */
1309 if (!desc->action)
1310 irq_shutdown(desc);
1311
1312#ifdef CONFIG_SMP
1313 /* make sure affinity_hint is cleaned up */
1314 if (WARN_ON_ONCE(desc->affinity_hint))
1315 desc->affinity_hint = NULL;
1316#endif
1317
1318 raw_spin_unlock_irqrestore(&desc->lock, flags);
1319
1320 unregister_handler_proc(irq, action);
1321
1322 /* Make sure it's not being used on another CPU: */
1323 synchronize_irq(irq);
1324
1325#ifdef CONFIG_DEBUG_SHIRQ
1326 /*
1327 * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1328 * event to happen even now it's being freed, so let's make sure that
1329 * is so by doing an extra call to the handler ....
1330 *
1331 * ( We do this after actually deregistering it, to make sure that a
1332 * 'real' IRQ doesn't run in * parallel with our fake. )
1333 */
1334 if (action->flags & IRQF_SHARED) {
1335 local_irq_save(flags);
1336 action->handler(irq, dev_id);
1337 local_irq_restore(flags);
1338 }
1339#endif
1340
1341 if (action->thread) {
1342 kthread_stop(action->thread);
1343 put_task_struct(action->thread);
1344 }
1345
1346 module_put(desc->owner);
1347 return action;
1348}
1349
1350/**
1351 * remove_irq - free an interrupt
1352 * @irq: Interrupt line to free
1353 * @act: irqaction for the interrupt
1354 *
1355 * Used to remove interrupts statically setup by the early boot process.
1356 */
1357void remove_irq(unsigned int irq, struct irqaction *act)
1358{
1359 struct irq_desc *desc = irq_to_desc(irq);
1360
1361 if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1362 __free_irq(irq, act->dev_id);
1363}
1364EXPORT_SYMBOL_GPL(remove_irq);
1365
1366/**
1367 * free_irq - free an interrupt allocated with request_irq
1368 * @irq: Interrupt line to free
1369 * @dev_id: Device identity to free
1370 *
1371 * Remove an interrupt handler. The handler is removed and if the
1372 * interrupt line is no longer in use by any driver it is disabled.
1373 * On a shared IRQ the caller must ensure the interrupt is disabled
1374 * on the card it drives before calling this function. The function
1375 * does not return until any executing interrupts for this IRQ
1376 * have completed.
1377 *
1378 * This function must not be called from interrupt context.
1379 */
1380void free_irq(unsigned int irq, void *dev_id)
1381{
1382 struct irq_desc *desc = irq_to_desc(irq);
1383
1384 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1385 return;
1386
1387#ifdef CONFIG_SMP
1388 if (WARN_ON(desc->affinity_notify))
1389 desc->affinity_notify = NULL;
1390#endif
1391
1392 chip_bus_lock(desc);
1393 kfree(__free_irq(irq, dev_id));
1394 chip_bus_sync_unlock(desc);
1395}
1396EXPORT_SYMBOL(free_irq);
1397
1398/**
1399 * request_threaded_irq - allocate an interrupt line
1400 * @irq: Interrupt line to allocate
1401 * @handler: Function to be called when the IRQ occurs.
1402 * Primary handler for threaded interrupts
1403 * If NULL and thread_fn != NULL the default
1404 * primary handler is installed
1405 * @thread_fn: Function called from the irq handler thread
1406 * If NULL, no irq thread is created
1407 * @irqflags: Interrupt type flags
1408 * @devname: An ascii name for the claiming device
1409 * @dev_id: A cookie passed back to the handler function
1410 *
1411 * This call allocates interrupt resources and enables the
1412 * interrupt line and IRQ handling. From the point this
1413 * call is made your handler function may be invoked. Since
1414 * your handler function must clear any interrupt the board
1415 * raises, you must take care both to initialise your hardware
1416 * and to set up the interrupt handler in the right order.
1417 *
1418 * If you want to set up a threaded irq handler for your device
1419 * then you need to supply @handler and @thread_fn. @handler is
1420 * still called in hard interrupt context and has to check
1421 * whether the interrupt originates from the device. If yes it
1422 * needs to disable the interrupt on the device and return
1423 * IRQ_WAKE_THREAD which will wake up the handler thread and run
1424 * @thread_fn. This split handler design is necessary to support
1425 * shared interrupts.
1426 *
1427 * Dev_id must be globally unique. Normally the address of the
1428 * device data structure is used as the cookie. Since the handler
1429 * receives this value it makes sense to use it.
1430 *
1431 * If your interrupt is shared you must pass a non NULL dev_id
1432 * as this is required when freeing the interrupt.
1433 *
1434 * Flags:
1435 *
1436 * IRQF_SHARED Interrupt is shared
1437 * IRQF_TRIGGER_* Specify active edge(s) or level
1438 *
1439 */
1440int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1441 irq_handler_t thread_fn, unsigned long irqflags,
1442 const char *devname, void *dev_id)
1443{
1444 struct irqaction *action;
1445 struct irq_desc *desc;
1446 int retval;
1447
1448 /*
1449 * Sanity-check: shared interrupts must pass in a real dev-ID,
1450 * otherwise we'll have trouble later trying to figure out
1451 * which interrupt is which (messes up the interrupt freeing
1452 * logic etc).
1453 */
1454 if ((irqflags & IRQF_SHARED) && !dev_id)
1455 return -EINVAL;
1456
1457 desc = irq_to_desc(irq);
1458 if (!desc)
1459 return -EINVAL;
1460
1461 if (!irq_settings_can_request(desc) ||
1462 WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1463 return -EINVAL;
1464
1465 if (!handler) {
1466 if (!thread_fn)
1467 return -EINVAL;
1468 handler = irq_default_primary_handler;
1469 }
1470
1471 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1472 if (!action)
1473 return -ENOMEM;
1474
1475 action->handler = handler;
1476 action->thread_fn = thread_fn;
1477 action->flags = irqflags;
1478 action->name = devname;
1479 action->dev_id = dev_id;
1480
1481 chip_bus_lock(desc);
1482 retval = __setup_irq(irq, desc, action);
1483 chip_bus_sync_unlock(desc);
1484
1485 if (retval)
1486 kfree(action);
1487
1488#ifdef CONFIG_DEBUG_SHIRQ_FIXME
1489 if (!retval && (irqflags & IRQF_SHARED)) {
1490 /*
1491 * It's a shared IRQ -- the driver ought to be prepared for it
1492 * to happen immediately, so let's make sure....
1493 * We disable the irq to make sure that a 'real' IRQ doesn't
1494 * run in parallel with our fake.
1495 */
1496 unsigned long flags;
1497
1498 disable_irq(irq);
1499 local_irq_save(flags);
1500
1501 handler(irq, dev_id);
1502
1503 local_irq_restore(flags);
1504 enable_irq(irq);
1505 }
1506#endif
1507 return retval;
1508}
1509EXPORT_SYMBOL(request_threaded_irq);
1510
1511/**
1512 * request_any_context_irq - allocate an interrupt line
1513 * @irq: Interrupt line to allocate
1514 * @handler: Function to be called when the IRQ occurs.
1515 * Threaded handler for threaded interrupts.
1516 * @flags: Interrupt type flags
1517 * @name: An ascii name for the claiming device
1518 * @dev_id: A cookie passed back to the handler function
1519 *
1520 * This call allocates interrupt resources and enables the
1521 * interrupt line and IRQ handling. It selects either a
1522 * hardirq or threaded handling method depending on the
1523 * context.
1524 *
1525 * On failure, it returns a negative value. On success,
1526 * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1527 */
1528int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1529 unsigned long flags, const char *name, void *dev_id)
1530{
1531 struct irq_desc *desc = irq_to_desc(irq);
1532 int ret;
1533
1534 if (!desc)
1535 return -EINVAL;
1536
1537 if (irq_settings_is_nested_thread(desc)) {
1538 ret = request_threaded_irq(irq, NULL, handler,
1539 flags, name, dev_id);
1540 return !ret ? IRQC_IS_NESTED : ret;
1541 }
1542
1543 ret = request_irq(irq, handler, flags, name, dev_id);
1544 return !ret ? IRQC_IS_HARDIRQ : ret;
1545}
1546EXPORT_SYMBOL_GPL(request_any_context_irq);
1547
1548void enable_percpu_irq(unsigned int irq, unsigned int type)
1549{
1550 unsigned int cpu = smp_processor_id();
1551 unsigned long flags;
1552 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1553
1554 if (!desc)
1555 return;
1556
1557 type &= IRQ_TYPE_SENSE_MASK;
1558 if (type != IRQ_TYPE_NONE) {
1559 int ret;
1560
1561 ret = __irq_set_trigger(desc, irq, type);
1562
1563 if (ret) {
1564 WARN(1, "failed to set type for IRQ%d\n", irq);
1565 goto out;
1566 }
1567 }
1568
1569 irq_percpu_enable(desc, cpu);
1570out:
1571 irq_put_desc_unlock(desc, flags);
1572}
1573
1574void disable_percpu_irq(unsigned int irq)
1575{
1576 unsigned int cpu = smp_processor_id();
1577 unsigned long flags;
1578 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1579
1580 if (!desc)
1581 return;
1582
1583 irq_percpu_disable(desc, cpu);
1584 irq_put_desc_unlock(desc, flags);
1585}
1586
1587/*
1588 * Internal function to unregister a percpu irqaction.
1589 */
1590static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1591{
1592 struct irq_desc *desc = irq_to_desc(irq);
1593 struct irqaction *action;
1594 unsigned long flags;
1595
1596 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1597
1598 if (!desc)
1599 return NULL;
1600
1601 raw_spin_lock_irqsave(&desc->lock, flags);
1602
1603 action = desc->action;
1604 if (!action || action->percpu_dev_id != dev_id) {
1605 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1606 goto bad;
1607 }
1608
1609 if (!cpumask_empty(desc->percpu_enabled)) {
1610 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1611 irq, cpumask_first(desc->percpu_enabled));
1612 goto bad;
1613 }
1614
1615 /* Found it - now remove it from the list of entries: */
1616 desc->action = NULL;
1617
1618 raw_spin_unlock_irqrestore(&desc->lock, flags);
1619
1620 unregister_handler_proc(irq, action);
1621
1622 module_put(desc->owner);
1623 return action;
1624
1625bad:
1626 raw_spin_unlock_irqrestore(&desc->lock, flags);
1627 return NULL;
1628}
1629
1630/**
1631 * remove_percpu_irq - free a per-cpu interrupt
1632 * @irq: Interrupt line to free
1633 * @act: irqaction for the interrupt
1634 *
1635 * Used to remove interrupts statically setup by the early boot process.
1636 */
1637void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1638{
1639 struct irq_desc *desc = irq_to_desc(irq);
1640
1641 if (desc && irq_settings_is_per_cpu_devid(desc))
1642 __free_percpu_irq(irq, act->percpu_dev_id);
1643}
1644
1645/**
1646 * free_percpu_irq - free an interrupt allocated with request_percpu_irq
1647 * @irq: Interrupt line to free
1648 * @dev_id: Device identity to free
1649 *
1650 * Remove a percpu interrupt handler. The handler is removed, but
1651 * the interrupt line is not disabled. This must be done on each
1652 * CPU before calling this function. The function does not return
1653 * until any executing interrupts for this IRQ have completed.
1654 *
1655 * This function must not be called from interrupt context.
1656 */
1657void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1658{
1659 struct irq_desc *desc = irq_to_desc(irq);
1660
1661 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1662 return;
1663
1664 chip_bus_lock(desc);
1665 kfree(__free_percpu_irq(irq, dev_id));
1666 chip_bus_sync_unlock(desc);
1667}
1668
1669/**
1670 * setup_percpu_irq - setup a per-cpu interrupt
1671 * @irq: Interrupt line to setup
1672 * @act: irqaction for the interrupt
1673 *
1674 * Used to statically setup per-cpu interrupts in the early boot process.
1675 */
1676int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1677{
1678 struct irq_desc *desc = irq_to_desc(irq);
1679 int retval;
1680
1681 if (!desc || !irq_settings_is_per_cpu_devid(desc))
1682 return -EINVAL;
1683 chip_bus_lock(desc);
1684 retval = __setup_irq(irq, desc, act);
1685 chip_bus_sync_unlock(desc);
1686
1687 return retval;
1688}
1689
1690/**
1691 * request_percpu_irq - allocate a percpu interrupt line
1692 * @irq: Interrupt line to allocate
1693 * @handler: Function to be called when the IRQ occurs.
1694 * @devname: An ascii name for the claiming device
1695 * @dev_id: A percpu cookie passed back to the handler function
1696 *
1697 * This call allocates interrupt resources, but doesn't
1698 * automatically enable the interrupt. It has to be done on each
1699 * CPU using enable_percpu_irq().
1700 *
1701 * Dev_id must be globally unique. It is a per-cpu variable, and
1702 * the handler gets called with the interrupted CPU's instance of
1703 * that variable.
1704 */
1705int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1706 const char *devname, void __percpu *dev_id)
1707{
1708 struct irqaction *action;
1709 struct irq_desc *desc;
1710 int retval;
1711
1712 if (!dev_id)
1713 return -EINVAL;
1714
1715 desc = irq_to_desc(irq);
1716 if (!desc || !irq_settings_can_request(desc) ||
1717 !irq_settings_is_per_cpu_devid(desc))
1718 return -EINVAL;
1719
1720 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1721 if (!action)
1722 return -ENOMEM;
1723
1724 action->handler = handler;
1725 action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
1726 action->name = devname;
1727 action->percpu_dev_id = dev_id;
1728
1729 chip_bus_lock(desc);
1730 retval = __setup_irq(irq, desc, action);
1731 chip_bus_sync_unlock(desc);
1732
1733 if (retval)
1734 kfree(action);
1735
1736 return retval;
1737}