These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / kernel / irq / manage.c
1 /*
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 #define pr_fmt(fmt) "genirq: " fmt
11
12 #include <linux/irq.h>
13 #include <linux/kthread.h>
14 #include <linux/module.h>
15 #include <linux/random.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/sched/rt.h>
20 #include <linux/task_work.h>
21
22 #include "internals.h"
23
24 #ifdef CONFIG_IRQ_FORCED_THREADING
25 # ifndef CONFIG_PREEMPT_RT_BASE
26 __read_mostly bool force_irqthreads;
27
28 static int __init setup_forced_irqthreads(char *arg)
29 {
30         force_irqthreads = true;
31         return 0;
32 }
33 early_param("threadirqs", setup_forced_irqthreads);
34 # endif
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 int __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 0;
125         return 1;
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_set_thread_affinity - Notify irq threads to adjust affinity
140  *      @desc:          irq descriptor which has affitnity changed
141  *
142  *      We just set IRQTF_AFFINITY and delegate the affinity setting
143  *      to the interrupt thread itself. We can not call
144  *      set_cpus_allowed_ptr() here as we hold desc->lock and this
145  *      code can be called from hard interrupt context.
146  */
147 void irq_set_thread_affinity(struct irq_desc *desc)
148 {
149         struct irqaction *action = desc->action;
150
151         while (action) {
152                 if (action->thread)
153                         set_bit(IRQTF_AFFINITY, &action->thread_flags);
154                 action = action->next;
155         }
156 }
157
158 #ifdef CONFIG_GENERIC_PENDING_IRQ
159 static inline bool irq_can_move_pcntxt(struct irq_data *data)
160 {
161         return irqd_can_move_in_process_context(data);
162 }
163 static inline bool irq_move_pending(struct irq_data *data)
164 {
165         return irqd_is_setaffinity_pending(data);
166 }
167 static inline void
168 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
169 {
170         cpumask_copy(desc->pending_mask, mask);
171 }
172 static inline void
173 irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
174 {
175         cpumask_copy(mask, desc->pending_mask);
176 }
177 #else
178 static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
179 static inline bool irq_move_pending(struct irq_data *data) { return false; }
180 static inline void
181 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
182 static inline void
183 irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
184 #endif
185
186 #ifdef CONFIG_PREEMPT_RT_FULL
187 static void _irq_affinity_notify(struct irq_affinity_notify *notify);
188 static struct task_struct *set_affinity_helper;
189 static LIST_HEAD(affinity_list);
190 static DEFINE_RAW_SPINLOCK(affinity_list_lock);
191
192 static int set_affinity_thread(void *unused)
193 {
194         while (1) {
195                 struct irq_affinity_notify *notify;
196                 int empty;
197
198                 set_current_state(TASK_INTERRUPTIBLE);
199
200                 raw_spin_lock_irq(&affinity_list_lock);
201                 empty = list_empty(&affinity_list);
202                 raw_spin_unlock_irq(&affinity_list_lock);
203
204                 if (empty)
205                         schedule();
206                 if (kthread_should_stop())
207                         break;
208                 set_current_state(TASK_RUNNING);
209 try_next:
210                 notify = NULL;
211
212                 raw_spin_lock_irq(&affinity_list_lock);
213                 if (!list_empty(&affinity_list)) {
214                         notify = list_first_entry(&affinity_list,
215                                         struct irq_affinity_notify, list);
216                         list_del_init(&notify->list);
217                 }
218                 raw_spin_unlock_irq(&affinity_list_lock);
219
220                 if (!notify)
221                         continue;
222                 _irq_affinity_notify(notify);
223                 goto try_next;
224         }
225         return 0;
226 }
227
228 static void init_helper_thread(void)
229 {
230         if (set_affinity_helper)
231                 return;
232         set_affinity_helper = kthread_run(set_affinity_thread, NULL,
233                         "affinity-cb");
234         WARN_ON(IS_ERR(set_affinity_helper));
235 }
236 #else
237
238 static inline void init_helper_thread(void) { }
239
240 #endif
241
242 int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
243                         bool force)
244 {
245         struct irq_desc *desc = irq_data_to_desc(data);
246         struct irq_chip *chip = irq_data_get_irq_chip(data);
247         int ret;
248
249         ret = chip->irq_set_affinity(data, mask, force);
250         switch (ret) {
251         case IRQ_SET_MASK_OK:
252         case IRQ_SET_MASK_OK_DONE:
253                 cpumask_copy(desc->irq_common_data.affinity, mask);
254         case IRQ_SET_MASK_OK_NOCOPY:
255                 irq_set_thread_affinity(desc);
256                 ret = 0;
257         }
258
259         return ret;
260 }
261
262 int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
263                             bool force)
264 {
265         struct irq_chip *chip = irq_data_get_irq_chip(data);
266         struct irq_desc *desc = irq_data_to_desc(data);
267         int ret = 0;
268
269         if (!chip || !chip->irq_set_affinity)
270                 return -EINVAL;
271
272         if (irq_can_move_pcntxt(data)) {
273                 ret = irq_do_set_affinity(data, mask, force);
274         } else {
275                 irqd_set_move_pending(data);
276                 irq_copy_pending(desc, mask);
277         }
278
279         if (desc->affinity_notify) {
280                 kref_get(&desc->affinity_notify->kref);
281
282 #ifdef CONFIG_PREEMPT_RT_FULL
283                 raw_spin_lock(&affinity_list_lock);
284                 if (list_empty(&desc->affinity_notify->list))
285                         list_add_tail(&affinity_list,
286                                         &desc->affinity_notify->list);
287                 raw_spin_unlock(&affinity_list_lock);
288                 wake_up_process(set_affinity_helper);
289 #else
290                 schedule_work(&desc->affinity_notify->work);
291 #endif
292         }
293         irqd_set(data, IRQD_AFFINITY_SET);
294
295         return ret;
296 }
297
298 int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, bool force)
299 {
300         struct irq_desc *desc = irq_to_desc(irq);
301         unsigned long flags;
302         int ret;
303
304         if (!desc)
305                 return -EINVAL;
306
307         raw_spin_lock_irqsave(&desc->lock, flags);
308         ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
309         raw_spin_unlock_irqrestore(&desc->lock, flags);
310         return ret;
311 }
312
313 int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
314 {
315         unsigned long flags;
316         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
317
318         if (!desc)
319                 return -EINVAL;
320         desc->affinity_hint = m;
321         irq_put_desc_unlock(desc, flags);
322         /* set the initial affinity to prevent every interrupt being on CPU0 */
323         if (m)
324                 __irq_set_affinity(irq, m, false);
325         return 0;
326 }
327 EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
328
329 static void _irq_affinity_notify(struct irq_affinity_notify *notify)
330 {
331         struct irq_desc *desc = irq_to_desc(notify->irq);
332         cpumask_var_t cpumask;
333         unsigned long flags;
334
335         if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
336                 goto out;
337
338         raw_spin_lock_irqsave(&desc->lock, flags);
339         if (irq_move_pending(&desc->irq_data))
340                 irq_get_pending(cpumask, desc);
341         else
342                 cpumask_copy(cpumask, desc->irq_common_data.affinity);
343         raw_spin_unlock_irqrestore(&desc->lock, flags);
344
345         notify->notify(notify, cpumask);
346
347         free_cpumask_var(cpumask);
348 out:
349         kref_put(&notify->kref, notify->release);
350 }
351
352 static void irq_affinity_notify(struct work_struct *work)
353 {
354         struct irq_affinity_notify *notify =
355                 container_of(work, struct irq_affinity_notify, work);
356         _irq_affinity_notify(notify);
357 }
358
359 /**
360  *      irq_set_affinity_notifier - control notification of IRQ affinity changes
361  *      @irq:           Interrupt for which to enable/disable notification
362  *      @notify:        Context for notification, or %NULL to disable
363  *                      notification.  Function pointers must be initialised;
364  *                      the other fields will be initialised by this function.
365  *
366  *      Must be called in process context.  Notification may only be enabled
367  *      after the IRQ is allocated and must be disabled before the IRQ is
368  *      freed using free_irq().
369  */
370 int
371 irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
372 {
373         struct irq_desc *desc = irq_to_desc(irq);
374         struct irq_affinity_notify *old_notify;
375         unsigned long flags;
376
377         /* The release function is promised process context */
378         might_sleep();
379
380         if (!desc)
381                 return -EINVAL;
382
383         /* Complete initialisation of *notify */
384         if (notify) {
385                 notify->irq = irq;
386                 kref_init(&notify->kref);
387                 INIT_WORK(&notify->work, irq_affinity_notify);
388                 INIT_LIST_HEAD(&notify->list);
389                 init_helper_thread();
390         }
391
392         raw_spin_lock_irqsave(&desc->lock, flags);
393         old_notify = desc->affinity_notify;
394         desc->affinity_notify = notify;
395         raw_spin_unlock_irqrestore(&desc->lock, flags);
396
397         if (old_notify)
398                 kref_put(&old_notify->kref, old_notify->release);
399
400         return 0;
401 }
402 EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
403
404 #ifndef CONFIG_AUTO_IRQ_AFFINITY
405 /*
406  * Generic version of the affinity autoselector.
407  */
408 static int setup_affinity(struct irq_desc *desc, struct cpumask *mask)
409 {
410         struct cpumask *set = irq_default_affinity;
411         int node = irq_desc_get_node(desc);
412
413         /* Excludes PER_CPU and NO_BALANCE interrupts */
414         if (!__irq_can_set_affinity(desc))
415                 return 0;
416
417         /*
418          * Preserve an userspace affinity setup, but make sure that
419          * one of the targets is online.
420          */
421         if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
422                 if (cpumask_intersects(desc->irq_common_data.affinity,
423                                        cpu_online_mask))
424                         set = desc->irq_common_data.affinity;
425                 else
426                         irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
427         }
428
429         cpumask_and(mask, cpu_online_mask, set);
430         if (node != NUMA_NO_NODE) {
431                 const struct cpumask *nodemask = cpumask_of_node(node);
432
433                 /* make sure at least one of the cpus in nodemask is online */
434                 if (cpumask_intersects(mask, nodemask))
435                         cpumask_and(mask, mask, nodemask);
436         }
437         irq_do_set_affinity(&desc->irq_data, mask, false);
438         return 0;
439 }
440 #else
441 /* Wrapper for ALPHA specific affinity selector magic */
442 static inline int setup_affinity(struct irq_desc *d, struct cpumask *mask)
443 {
444         return irq_select_affinity(irq_desc_get_irq(d));
445 }
446 #endif
447
448 /*
449  * Called when affinity is set via /proc/irq
450  */
451 int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
452 {
453         struct irq_desc *desc = irq_to_desc(irq);
454         unsigned long flags;
455         int ret;
456
457         raw_spin_lock_irqsave(&desc->lock, flags);
458         ret = setup_affinity(desc, mask);
459         raw_spin_unlock_irqrestore(&desc->lock, flags);
460         return ret;
461 }
462
463 #else
464 static inline int
465 setup_affinity(struct irq_desc *desc, struct cpumask *mask)
466 {
467         return 0;
468 }
469 #endif
470
471 /**
472  *      irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
473  *      @irq: interrupt number to set affinity
474  *      @vcpu_info: vCPU specific data
475  *
476  *      This function uses the vCPU specific data to set the vCPU
477  *      affinity for an irq. The vCPU specific data is passed from
478  *      outside, such as KVM. One example code path is as below:
479  *      KVM -> IOMMU -> irq_set_vcpu_affinity().
480  */
481 int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
482 {
483         unsigned long flags;
484         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
485         struct irq_data *data;
486         struct irq_chip *chip;
487         int ret = -ENOSYS;
488
489         if (!desc)
490                 return -EINVAL;
491
492         data = irq_desc_get_irq_data(desc);
493         chip = irq_data_get_irq_chip(data);
494         if (chip && chip->irq_set_vcpu_affinity)
495                 ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
496         irq_put_desc_unlock(desc, flags);
497
498         return ret;
499 }
500 EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
501
502 void __disable_irq(struct irq_desc *desc)
503 {
504         if (!desc->depth++)
505                 irq_disable(desc);
506 }
507
508 static int __disable_irq_nosync(unsigned int irq)
509 {
510         unsigned long flags;
511         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
512
513         if (!desc)
514                 return -EINVAL;
515         __disable_irq(desc);
516         irq_put_desc_busunlock(desc, flags);
517         return 0;
518 }
519
520 /**
521  *      disable_irq_nosync - disable an irq without waiting
522  *      @irq: Interrupt to disable
523  *
524  *      Disable the selected interrupt line.  Disables and Enables are
525  *      nested.
526  *      Unlike disable_irq(), this function does not ensure existing
527  *      instances of the IRQ handler have completed before returning.
528  *
529  *      This function may be called from IRQ context.
530  */
531 void disable_irq_nosync(unsigned int irq)
532 {
533         __disable_irq_nosync(irq);
534 }
535 EXPORT_SYMBOL(disable_irq_nosync);
536
537 /**
538  *      disable_irq - disable an irq and wait for completion
539  *      @irq: Interrupt to disable
540  *
541  *      Disable the selected interrupt line.  Enables and Disables are
542  *      nested.
543  *      This function waits for any pending IRQ handlers for this interrupt
544  *      to complete before returning. If you use this function while
545  *      holding a resource the IRQ handler may need you will deadlock.
546  *
547  *      This function may be called - with care - from IRQ context.
548  */
549 void disable_irq(unsigned int irq)
550 {
551         if (!__disable_irq_nosync(irq))
552                 synchronize_irq(irq);
553 }
554 EXPORT_SYMBOL(disable_irq);
555
556 /**
557  *      disable_hardirq - disables an irq and waits for hardirq completion
558  *      @irq: Interrupt to disable
559  *
560  *      Disable the selected interrupt line.  Enables and Disables are
561  *      nested.
562  *      This function waits for any pending hard IRQ handlers for this
563  *      interrupt to complete before returning. If you use this function while
564  *      holding a resource the hard IRQ handler may need you will deadlock.
565  *
566  *      When used to optimistically disable an interrupt from atomic context
567  *      the return value must be checked.
568  *
569  *      Returns: false if a threaded handler is active.
570  *
571  *      This function may be called - with care - from IRQ context.
572  */
573 bool disable_hardirq(unsigned int irq)
574 {
575         if (!__disable_irq_nosync(irq))
576                 return synchronize_hardirq(irq);
577
578         return false;
579 }
580 EXPORT_SYMBOL_GPL(disable_hardirq);
581
582 void __enable_irq(struct irq_desc *desc)
583 {
584         switch (desc->depth) {
585         case 0:
586  err_out:
587                 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
588                      irq_desc_get_irq(desc));
589                 break;
590         case 1: {
591                 if (desc->istate & IRQS_SUSPENDED)
592                         goto err_out;
593                 /* Prevent probing on this irq: */
594                 irq_settings_set_noprobe(desc);
595                 irq_enable(desc);
596                 check_irq_resend(desc);
597                 /* fall-through */
598         }
599         default:
600                 desc->depth--;
601         }
602 }
603
604 /**
605  *      enable_irq - enable handling of an irq
606  *      @irq: Interrupt to enable
607  *
608  *      Undoes the effect of one call to disable_irq().  If this
609  *      matches the last disable, processing of interrupts on this
610  *      IRQ line is re-enabled.
611  *
612  *      This function may be called from IRQ context only when
613  *      desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
614  */
615 void enable_irq(unsigned int irq)
616 {
617         unsigned long flags;
618         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
619
620         if (!desc)
621                 return;
622         if (WARN(!desc->irq_data.chip,
623                  KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
624                 goto out;
625
626         __enable_irq(desc);
627 out:
628         irq_put_desc_busunlock(desc, flags);
629 }
630 EXPORT_SYMBOL(enable_irq);
631
632 static int set_irq_wake_real(unsigned int irq, unsigned int on)
633 {
634         struct irq_desc *desc = irq_to_desc(irq);
635         int ret = -ENXIO;
636
637         if (irq_desc_get_chip(desc)->flags &  IRQCHIP_SKIP_SET_WAKE)
638                 return 0;
639
640         if (desc->irq_data.chip->irq_set_wake)
641                 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
642
643         return ret;
644 }
645
646 /**
647  *      irq_set_irq_wake - control irq power management wakeup
648  *      @irq:   interrupt to control
649  *      @on:    enable/disable power management wakeup
650  *
651  *      Enable/disable power management wakeup mode, which is
652  *      disabled by default.  Enables and disables must match,
653  *      just as they match for non-wakeup mode support.
654  *
655  *      Wakeup mode lets this IRQ wake the system from sleep
656  *      states like "suspend to RAM".
657  */
658 int irq_set_irq_wake(unsigned int irq, unsigned int on)
659 {
660         unsigned long flags;
661         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
662         int ret = 0;
663
664         if (!desc)
665                 return -EINVAL;
666
667         /* wakeup-capable irqs can be shared between drivers that
668          * don't need to have the same sleep mode behaviors.
669          */
670         if (on) {
671                 if (desc->wake_depth++ == 0) {
672                         ret = set_irq_wake_real(irq, on);
673                         if (ret)
674                                 desc->wake_depth = 0;
675                         else
676                                 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
677                 }
678         } else {
679                 if (desc->wake_depth == 0) {
680                         WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
681                 } else if (--desc->wake_depth == 0) {
682                         ret = set_irq_wake_real(irq, on);
683                         if (ret)
684                                 desc->wake_depth = 1;
685                         else
686                                 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
687                 }
688         }
689         irq_put_desc_busunlock(desc, flags);
690         return ret;
691 }
692 EXPORT_SYMBOL(irq_set_irq_wake);
693
694 /*
695  * Internal function that tells the architecture code whether a
696  * particular irq has been exclusively allocated or is available
697  * for driver use.
698  */
699 int can_request_irq(unsigned int irq, unsigned long irqflags)
700 {
701         unsigned long flags;
702         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
703         int canrequest = 0;
704
705         if (!desc)
706                 return 0;
707
708         if (irq_settings_can_request(desc)) {
709                 if (!desc->action ||
710                     irqflags & desc->action->flags & IRQF_SHARED)
711                         canrequest = 1;
712         }
713         irq_put_desc_unlock(desc, flags);
714         return canrequest;
715 }
716
717 int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
718 {
719         struct irq_chip *chip = desc->irq_data.chip;
720         int ret, unmask = 0;
721
722         if (!chip || !chip->irq_set_type) {
723                 /*
724                  * IRQF_TRIGGER_* but the PIC does not support multiple
725                  * flow-types?
726                  */
727                 pr_debug("No set_type function for IRQ %d (%s)\n",
728                          irq_desc_get_irq(desc),
729                          chip ? (chip->name ? : "unknown") : "unknown");
730                 return 0;
731         }
732
733         flags &= IRQ_TYPE_SENSE_MASK;
734
735         if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
736                 if (!irqd_irq_masked(&desc->irq_data))
737                         mask_irq(desc);
738                 if (!irqd_irq_disabled(&desc->irq_data))
739                         unmask = 1;
740         }
741
742         /* caller masked out all except trigger mode flags */
743         ret = chip->irq_set_type(&desc->irq_data, flags);
744
745         switch (ret) {
746         case IRQ_SET_MASK_OK:
747         case IRQ_SET_MASK_OK_DONE:
748                 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
749                 irqd_set(&desc->irq_data, flags);
750
751         case IRQ_SET_MASK_OK_NOCOPY:
752                 flags = irqd_get_trigger_type(&desc->irq_data);
753                 irq_settings_set_trigger_mask(desc, flags);
754                 irqd_clear(&desc->irq_data, IRQD_LEVEL);
755                 irq_settings_clr_level(desc);
756                 if (flags & IRQ_TYPE_LEVEL_MASK) {
757                         irq_settings_set_level(desc);
758                         irqd_set(&desc->irq_data, IRQD_LEVEL);
759                 }
760
761                 ret = 0;
762                 break;
763         default:
764                 pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
765                        flags, irq_desc_get_irq(desc), chip->irq_set_type);
766         }
767         if (unmask)
768                 unmask_irq(desc);
769         return ret;
770 }
771
772 #ifdef CONFIG_HARDIRQS_SW_RESEND
773 int irq_set_parent(int irq, int parent_irq)
774 {
775         unsigned long flags;
776         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
777
778         if (!desc)
779                 return -EINVAL;
780
781         desc->parent_irq = parent_irq;
782
783         irq_put_desc_unlock(desc, flags);
784         return 0;
785 }
786 #endif
787
788 /*
789  * Default primary interrupt handler for threaded interrupts. Is
790  * assigned as primary handler when request_threaded_irq is called
791  * with handler == NULL. Useful for oneshot interrupts.
792  */
793 static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
794 {
795         return IRQ_WAKE_THREAD;
796 }
797
798 /*
799  * Primary handler for nested threaded interrupts. Should never be
800  * called.
801  */
802 static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
803 {
804         WARN(1, "Primary handler called for nested irq %d\n", irq);
805         return IRQ_NONE;
806 }
807
808 static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
809 {
810         WARN(1, "Secondary action handler called for irq %d\n", irq);
811         return IRQ_NONE;
812 }
813
814 static int irq_wait_for_interrupt(struct irqaction *action)
815 {
816         set_current_state(TASK_INTERRUPTIBLE);
817
818         while (!kthread_should_stop()) {
819
820                 if (test_and_clear_bit(IRQTF_RUNTHREAD,
821                                        &action->thread_flags)) {
822                         __set_current_state(TASK_RUNNING);
823                         return 0;
824                 }
825                 schedule();
826                 set_current_state(TASK_INTERRUPTIBLE);
827         }
828         __set_current_state(TASK_RUNNING);
829         return -1;
830 }
831
832 /*
833  * Oneshot interrupts keep the irq line masked until the threaded
834  * handler finished. unmask if the interrupt has not been disabled and
835  * is marked MASKED.
836  */
837 static void irq_finalize_oneshot(struct irq_desc *desc,
838                                  struct irqaction *action)
839 {
840         if (!(desc->istate & IRQS_ONESHOT) ||
841             action->handler == irq_forced_secondary_handler)
842                 return;
843 again:
844         chip_bus_lock(desc);
845         raw_spin_lock_irq(&desc->lock);
846
847         /*
848          * Implausible though it may be we need to protect us against
849          * the following scenario:
850          *
851          * The thread is faster done than the hard interrupt handler
852          * on the other CPU. If we unmask the irq line then the
853          * interrupt can come in again and masks the line, leaves due
854          * to IRQS_INPROGRESS and the irq line is masked forever.
855          *
856          * This also serializes the state of shared oneshot handlers
857          * versus "desc->threads_onehsot |= action->thread_mask;" in
858          * irq_wake_thread(). See the comment there which explains the
859          * serialization.
860          */
861         if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
862                 raw_spin_unlock_irq(&desc->lock);
863                 chip_bus_sync_unlock(desc);
864                 cpu_relax();
865                 goto again;
866         }
867
868         /*
869          * Now check again, whether the thread should run. Otherwise
870          * we would clear the threads_oneshot bit of this thread which
871          * was just set.
872          */
873         if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
874                 goto out_unlock;
875
876         desc->threads_oneshot &= ~action->thread_mask;
877
878         if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
879             irqd_irq_masked(&desc->irq_data))
880                 unmask_threaded_irq(desc);
881
882 out_unlock:
883         raw_spin_unlock_irq(&desc->lock);
884         chip_bus_sync_unlock(desc);
885 }
886
887 #ifdef CONFIG_SMP
888 /*
889  * Check whether we need to change the affinity of the interrupt thread.
890  */
891 static void
892 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
893 {
894         cpumask_var_t mask;
895         bool valid = true;
896
897         if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
898                 return;
899
900         /*
901          * In case we are out of memory we set IRQTF_AFFINITY again and
902          * try again next time
903          */
904         if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
905                 set_bit(IRQTF_AFFINITY, &action->thread_flags);
906                 return;
907         }
908
909         raw_spin_lock_irq(&desc->lock);
910         /*
911          * This code is triggered unconditionally. Check the affinity
912          * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
913          */
914         if (desc->irq_common_data.affinity)
915                 cpumask_copy(mask, desc->irq_common_data.affinity);
916         else
917                 valid = false;
918         raw_spin_unlock_irq(&desc->lock);
919
920         if (valid)
921                 set_cpus_allowed_ptr(current, mask);
922         free_cpumask_var(mask);
923 }
924 #else
925 static inline void
926 irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
927 #endif
928
929 /*
930  * Interrupts which are not explicitely requested as threaded
931  * interrupts rely on the implicit bh/preempt disable of the hard irq
932  * context. So we need to disable bh here to avoid deadlocks and other
933  * side effects.
934  */
935 static irqreturn_t
936 irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
937 {
938         irqreturn_t ret;
939
940         local_bh_disable();
941         ret = action->thread_fn(action->irq, action->dev_id);
942         irq_finalize_oneshot(desc, action);
943         /*
944          * Interrupts which have real time requirements can be set up
945          * to avoid softirq processing in the thread handler. This is
946          * safe as these interrupts do not raise soft interrupts.
947          */
948         if (irq_settings_no_softirq_call(desc))
949                 _local_bh_enable();
950         else
951                 local_bh_enable();
952         return ret;
953 }
954
955 /*
956  * Interrupts explicitly requested as threaded interrupts want to be
957  * preemtible - many of them need to sleep and wait for slow busses to
958  * complete.
959  */
960 static irqreturn_t irq_thread_fn(struct irq_desc *desc,
961                 struct irqaction *action)
962 {
963         irqreturn_t ret;
964
965         ret = action->thread_fn(action->irq, action->dev_id);
966         irq_finalize_oneshot(desc, action);
967         return ret;
968 }
969
970 static void wake_threads_waitq(struct irq_desc *desc)
971 {
972         if (atomic_dec_and_test(&desc->threads_active))
973                 wake_up(&desc->wait_for_threads);
974 }
975
976 static void irq_thread_dtor(struct callback_head *unused)
977 {
978         struct task_struct *tsk = current;
979         struct irq_desc *desc;
980         struct irqaction *action;
981
982         if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
983                 return;
984
985         action = kthread_data(tsk);
986
987         pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
988                tsk->comm, tsk->pid, action->irq);
989
990
991         desc = irq_to_desc(action->irq);
992         /*
993          * If IRQTF_RUNTHREAD is set, we need to decrement
994          * desc->threads_active and wake possible waiters.
995          */
996         if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
997                 wake_threads_waitq(desc);
998
999         /* Prevent a stale desc->threads_oneshot */
1000         irq_finalize_oneshot(desc, action);
1001 }
1002
1003 static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
1004 {
1005         struct irqaction *secondary = action->secondary;
1006
1007         if (WARN_ON_ONCE(!secondary))
1008                 return;
1009
1010         raw_spin_lock_irq(&desc->lock);
1011         __irq_wake_thread(desc, secondary);
1012         raw_spin_unlock_irq(&desc->lock);
1013 }
1014
1015 /*
1016  * Interrupt handler thread
1017  */
1018 static int irq_thread(void *data)
1019 {
1020         struct callback_head on_exit_work;
1021         struct irqaction *action = data;
1022         struct irq_desc *desc = irq_to_desc(action->irq);
1023         irqreturn_t (*handler_fn)(struct irq_desc *desc,
1024                         struct irqaction *action);
1025
1026         if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
1027                                         &action->thread_flags))
1028                 handler_fn = irq_forced_thread_fn;
1029         else
1030                 handler_fn = irq_thread_fn;
1031
1032         init_task_work(&on_exit_work, irq_thread_dtor);
1033         task_work_add(current, &on_exit_work, false);
1034
1035         irq_thread_check_affinity(desc, action);
1036
1037         while (!irq_wait_for_interrupt(action)) {
1038                 irqreturn_t action_ret;
1039
1040                 irq_thread_check_affinity(desc, action);
1041
1042                 action_ret = handler_fn(desc, action);
1043                 if (action_ret == IRQ_HANDLED)
1044                         atomic_inc(&desc->threads_handled);
1045                 if (action_ret == IRQ_WAKE_THREAD)
1046                         irq_wake_secondary(desc, action);
1047
1048 #ifdef CONFIG_PREEMPT_RT_FULL
1049                 migrate_disable();
1050                 add_interrupt_randomness(action->irq, 0,
1051                                  desc->random_ip ^ (unsigned long) action);
1052                 migrate_enable();
1053 #endif
1054                 wake_threads_waitq(desc);
1055         }
1056
1057         /*
1058          * This is the regular exit path. __free_irq() is stopping the
1059          * thread via kthread_stop() after calling
1060          * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
1061          * oneshot mask bit can be set. We cannot verify that as we
1062          * cannot touch the oneshot mask at this point anymore as
1063          * __setup_irq() might have given out currents thread_mask
1064          * again.
1065          */
1066         task_work_cancel(current, irq_thread_dtor);
1067         return 0;
1068 }
1069
1070 /**
1071  *      irq_wake_thread - wake the irq thread for the action identified by dev_id
1072  *      @irq:           Interrupt line
1073  *      @dev_id:        Device identity for which the thread should be woken
1074  *
1075  */
1076 void irq_wake_thread(unsigned int irq, void *dev_id)
1077 {
1078         struct irq_desc *desc = irq_to_desc(irq);
1079         struct irqaction *action;
1080         unsigned long flags;
1081
1082         if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1083                 return;
1084
1085         raw_spin_lock_irqsave(&desc->lock, flags);
1086         for (action = desc->action; action; action = action->next) {
1087                 if (action->dev_id == dev_id) {
1088                         if (action->thread)
1089                                 __irq_wake_thread(desc, action);
1090                         break;
1091                 }
1092         }
1093         raw_spin_unlock_irqrestore(&desc->lock, flags);
1094 }
1095 EXPORT_SYMBOL_GPL(irq_wake_thread);
1096
1097 static int irq_setup_forced_threading(struct irqaction *new)
1098 {
1099         if (!force_irqthreads)
1100                 return 0;
1101         if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
1102                 return 0;
1103
1104         new->flags |= IRQF_ONESHOT;
1105
1106         /*
1107          * Handle the case where we have a real primary handler and a
1108          * thread handler. We force thread them as well by creating a
1109          * secondary action.
1110          */
1111         if (new->handler != irq_default_primary_handler && new->thread_fn) {
1112                 /* Allocate the secondary action */
1113                 new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1114                 if (!new->secondary)
1115                         return -ENOMEM;
1116                 new->secondary->handler = irq_forced_secondary_handler;
1117                 new->secondary->thread_fn = new->thread_fn;
1118                 new->secondary->dev_id = new->dev_id;
1119                 new->secondary->irq = new->irq;
1120                 new->secondary->name = new->name;
1121         }
1122         /* Deal with the primary handler */
1123         set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
1124         new->thread_fn = new->handler;
1125         new->handler = irq_default_primary_handler;
1126         return 0;
1127 }
1128
1129 static int irq_request_resources(struct irq_desc *desc)
1130 {
1131         struct irq_data *d = &desc->irq_data;
1132         struct irq_chip *c = d->chip;
1133
1134         return c->irq_request_resources ? c->irq_request_resources(d) : 0;
1135 }
1136
1137 static void irq_release_resources(struct irq_desc *desc)
1138 {
1139         struct irq_data *d = &desc->irq_data;
1140         struct irq_chip *c = d->chip;
1141
1142         if (c->irq_release_resources)
1143                 c->irq_release_resources(d);
1144 }
1145
1146 static int
1147 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
1148 {
1149         struct task_struct *t;
1150         struct sched_param param = {
1151                 .sched_priority = MAX_USER_RT_PRIO/2,
1152         };
1153
1154         if (!secondary) {
1155                 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
1156                                    new->name);
1157         } else {
1158                 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
1159                                    new->name);
1160                 param.sched_priority -= 1;
1161         }
1162
1163         if (IS_ERR(t))
1164                 return PTR_ERR(t);
1165
1166         sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1167
1168         /*
1169          * We keep the reference to the task struct even if
1170          * the thread dies to avoid that the interrupt code
1171          * references an already freed task_struct.
1172          */
1173         get_task_struct(t);
1174         new->thread = t;
1175         /*
1176          * Tell the thread to set its affinity. This is
1177          * important for shared interrupt handlers as we do
1178          * not invoke setup_affinity() for the secondary
1179          * handlers as everything is already set up. Even for
1180          * interrupts marked with IRQF_NO_BALANCE this is
1181          * correct as we want the thread to move to the cpu(s)
1182          * on which the requesting code placed the interrupt.
1183          */
1184         set_bit(IRQTF_AFFINITY, &new->thread_flags);
1185         return 0;
1186 }
1187
1188 /*
1189  * Internal function to register an irqaction - typically used to
1190  * allocate special interrupts that are part of the architecture.
1191  */
1192 static int
1193 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
1194 {
1195         struct irqaction *old, **old_ptr;
1196         unsigned long flags, thread_mask = 0;
1197         int ret, nested, shared = 0;
1198         cpumask_var_t mask;
1199
1200         if (!desc)
1201                 return -EINVAL;
1202
1203         if (desc->irq_data.chip == &no_irq_chip)
1204                 return -ENOSYS;
1205         if (!try_module_get(desc->owner))
1206                 return -ENODEV;
1207
1208         new->irq = irq;
1209
1210         /*
1211          * Check whether the interrupt nests into another interrupt
1212          * thread.
1213          */
1214         nested = irq_settings_is_nested_thread(desc);
1215         if (nested) {
1216                 if (!new->thread_fn) {
1217                         ret = -EINVAL;
1218                         goto out_mput;
1219                 }
1220                 /*
1221                  * Replace the primary handler which was provided from
1222                  * the driver for non nested interrupt handling by the
1223                  * dummy function which warns when called.
1224                  */
1225                 new->handler = irq_nested_primary_handler;
1226         } else {
1227                 if (irq_settings_can_thread(desc)) {
1228                         ret = irq_setup_forced_threading(new);
1229                         if (ret)
1230                                 goto out_mput;
1231                 }
1232         }
1233
1234         /*
1235          * Create a handler thread when a thread function is supplied
1236          * and the interrupt does not nest into another interrupt
1237          * thread.
1238          */
1239         if (new->thread_fn && !nested) {
1240                 ret = setup_irq_thread(new, irq, false);
1241                 if (ret)
1242                         goto out_mput;
1243                 if (new->secondary) {
1244                         ret = setup_irq_thread(new->secondary, irq, true);
1245                         if (ret)
1246                                 goto out_thread;
1247                 }
1248         }
1249
1250         if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
1251                 ret = -ENOMEM;
1252                 goto out_thread;
1253         }
1254
1255         /*
1256          * Drivers are often written to work w/o knowledge about the
1257          * underlying irq chip implementation, so a request for a
1258          * threaded irq without a primary hard irq context handler
1259          * requires the ONESHOT flag to be set. Some irq chips like
1260          * MSI based interrupts are per se one shot safe. Check the
1261          * chip flags, so we can avoid the unmask dance at the end of
1262          * the threaded handler for those.
1263          */
1264         if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
1265                 new->flags &= ~IRQF_ONESHOT;
1266
1267         /*
1268          * The following block of code has to be executed atomically
1269          */
1270         raw_spin_lock_irqsave(&desc->lock, flags);
1271         old_ptr = &desc->action;
1272         old = *old_ptr;
1273         if (old) {
1274                 /*
1275                  * Can't share interrupts unless both agree to and are
1276                  * the same type (level, edge, polarity). So both flag
1277                  * fields must have IRQF_SHARED set and the bits which
1278                  * set the trigger type must match. Also all must
1279                  * agree on ONESHOT.
1280                  */
1281                 if (!((old->flags & new->flags) & IRQF_SHARED) ||
1282                     ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
1283                     ((old->flags ^ new->flags) & IRQF_ONESHOT))
1284                         goto mismatch;
1285
1286                 /* All handlers must agree on per-cpuness */
1287                 if ((old->flags & IRQF_PERCPU) !=
1288                     (new->flags & IRQF_PERCPU))
1289                         goto mismatch;
1290
1291                 /* add new interrupt at end of irq queue */
1292                 do {
1293                         /*
1294                          * Or all existing action->thread_mask bits,
1295                          * so we can find the next zero bit for this
1296                          * new action.
1297                          */
1298                         thread_mask |= old->thread_mask;
1299                         old_ptr = &old->next;
1300                         old = *old_ptr;
1301                 } while (old);
1302                 shared = 1;
1303         }
1304
1305         /*
1306          * Setup the thread mask for this irqaction for ONESHOT. For
1307          * !ONESHOT irqs the thread mask is 0 so we can avoid a
1308          * conditional in irq_wake_thread().
1309          */
1310         if (new->flags & IRQF_ONESHOT) {
1311                 /*
1312                  * Unlikely to have 32 resp 64 irqs sharing one line,
1313                  * but who knows.
1314                  */
1315                 if (thread_mask == ~0UL) {
1316                         ret = -EBUSY;
1317                         goto out_mask;
1318                 }
1319                 /*
1320                  * The thread_mask for the action is or'ed to
1321                  * desc->thread_active to indicate that the
1322                  * IRQF_ONESHOT thread handler has been woken, but not
1323                  * yet finished. The bit is cleared when a thread
1324                  * completes. When all threads of a shared interrupt
1325                  * line have completed desc->threads_active becomes
1326                  * zero and the interrupt line is unmasked. See
1327                  * handle.c:irq_wake_thread() for further information.
1328                  *
1329                  * If no thread is woken by primary (hard irq context)
1330                  * interrupt handlers, then desc->threads_active is
1331                  * also checked for zero to unmask the irq line in the
1332                  * affected hard irq flow handlers
1333                  * (handle_[fasteoi|level]_irq).
1334                  *
1335                  * The new action gets the first zero bit of
1336                  * thread_mask assigned. See the loop above which or's
1337                  * all existing action->thread_mask bits.
1338                  */
1339                 new->thread_mask = 1 << ffz(thread_mask);
1340
1341         } else if (new->handler == irq_default_primary_handler &&
1342                    !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
1343                 /*
1344                  * The interrupt was requested with handler = NULL, so
1345                  * we use the default primary handler for it. But it
1346                  * does not have the oneshot flag set. In combination
1347                  * with level interrupts this is deadly, because the
1348                  * default primary handler just wakes the thread, then
1349                  * the irq lines is reenabled, but the device still
1350                  * has the level irq asserted. Rinse and repeat....
1351                  *
1352                  * While this works for edge type interrupts, we play
1353                  * it safe and reject unconditionally because we can't
1354                  * say for sure which type this interrupt really
1355                  * has. The type flags are unreliable as the
1356                  * underlying chip implementation can override them.
1357                  */
1358                 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1359                        irq);
1360                 ret = -EINVAL;
1361                 goto out_mask;
1362         }
1363
1364         if (!shared) {
1365                 ret = irq_request_resources(desc);
1366                 if (ret) {
1367                         pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
1368                                new->name, irq, desc->irq_data.chip->name);
1369                         goto out_mask;
1370                 }
1371
1372                 init_waitqueue_head(&desc->wait_for_threads);
1373
1374                 /* Setup the type (level, edge polarity) if configured: */
1375                 if (new->flags & IRQF_TRIGGER_MASK) {
1376                         ret = __irq_set_trigger(desc,
1377                                                 new->flags & IRQF_TRIGGER_MASK);
1378
1379                         if (ret)
1380                                 goto out_mask;
1381                 }
1382
1383                 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
1384                                   IRQS_ONESHOT | IRQS_WAITING);
1385                 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
1386
1387                 if (new->flags & IRQF_PERCPU) {
1388                         irqd_set(&desc->irq_data, IRQD_PER_CPU);
1389                         irq_settings_set_per_cpu(desc);
1390                 }
1391
1392                 if (new->flags & IRQF_ONESHOT)
1393                         desc->istate |= IRQS_ONESHOT;
1394
1395                 if (irq_settings_can_autoenable(desc))
1396                         irq_startup(desc, true);
1397                 else
1398                         /* Undo nested disables: */
1399                         desc->depth = 1;
1400
1401                 /* Exclude IRQ from balancing if requested */
1402                 if (new->flags & IRQF_NOBALANCING) {
1403                         irq_settings_set_no_balancing(desc);
1404                         irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1405                 }
1406
1407                 if (new->flags & IRQF_NO_SOFTIRQ_CALL)
1408                         irq_settings_set_no_softirq_call(desc);
1409
1410                 /* Set default affinity mask once everything is setup */
1411                 setup_affinity(desc, mask);
1412
1413         } else if (new->flags & IRQF_TRIGGER_MASK) {
1414                 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
1415                 unsigned int omsk = irq_settings_get_trigger_mask(desc);
1416
1417                 if (nmsk != omsk)
1418                         /* hope the handler works with current  trigger mode */
1419                         pr_warning("irq %d uses trigger mode %u; requested %u\n",
1420                                    irq, nmsk, omsk);
1421         }
1422
1423         *old_ptr = new;
1424
1425         irq_pm_install_action(desc, new);
1426
1427         /* Reset broken irq detection when installing new handler */
1428         desc->irq_count = 0;
1429         desc->irqs_unhandled = 0;
1430
1431         /*
1432          * Check whether we disabled the irq via the spurious handler
1433          * before. Reenable it and give it another chance.
1434          */
1435         if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
1436                 desc->istate &= ~IRQS_SPURIOUS_DISABLED;
1437                 __enable_irq(desc);
1438         }
1439
1440         raw_spin_unlock_irqrestore(&desc->lock, flags);
1441
1442         /*
1443          * Strictly no need to wake it up, but hung_task complains
1444          * when no hard interrupt wakes the thread up.
1445          */
1446         if (new->thread)
1447                 wake_up_process(new->thread);
1448         if (new->secondary)
1449                 wake_up_process(new->secondary->thread);
1450
1451         register_irq_proc(irq, desc);
1452         new->dir = NULL;
1453         register_handler_proc(irq, new);
1454         free_cpumask_var(mask);
1455
1456         return 0;
1457
1458 mismatch:
1459         if (!(new->flags & IRQF_PROBE_SHARED)) {
1460                 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
1461                        irq, new->flags, new->name, old->flags, old->name);
1462 #ifdef CONFIG_DEBUG_SHIRQ
1463                 dump_stack();
1464 #endif
1465         }
1466         ret = -EBUSY;
1467
1468 out_mask:
1469         raw_spin_unlock_irqrestore(&desc->lock, flags);
1470         free_cpumask_var(mask);
1471
1472 out_thread:
1473         if (new->thread) {
1474                 struct task_struct *t = new->thread;
1475
1476                 new->thread = NULL;
1477                 kthread_stop(t);
1478                 put_task_struct(t);
1479         }
1480         if (new->secondary && new->secondary->thread) {
1481                 struct task_struct *t = new->secondary->thread;
1482
1483                 new->secondary->thread = NULL;
1484                 kthread_stop(t);
1485                 put_task_struct(t);
1486         }
1487 out_mput:
1488         module_put(desc->owner);
1489         return ret;
1490 }
1491
1492 /**
1493  *      setup_irq - setup an interrupt
1494  *      @irq: Interrupt line to setup
1495  *      @act: irqaction for the interrupt
1496  *
1497  * Used to statically setup interrupts in the early boot process.
1498  */
1499 int setup_irq(unsigned int irq, struct irqaction *act)
1500 {
1501         int retval;
1502         struct irq_desc *desc = irq_to_desc(irq);
1503
1504         if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1505                 return -EINVAL;
1506         chip_bus_lock(desc);
1507         retval = __setup_irq(irq, desc, act);
1508         chip_bus_sync_unlock(desc);
1509
1510         return retval;
1511 }
1512 EXPORT_SYMBOL_GPL(setup_irq);
1513
1514 /*
1515  * Internal function to unregister an irqaction - used to free
1516  * regular and special interrupts that are part of the architecture.
1517  */
1518 static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
1519 {
1520         struct irq_desc *desc = irq_to_desc(irq);
1521         struct irqaction *action, **action_ptr;
1522         unsigned long flags;
1523
1524         WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1525
1526         if (!desc)
1527                 return NULL;
1528
1529         chip_bus_lock(desc);
1530         raw_spin_lock_irqsave(&desc->lock, flags);
1531
1532         /*
1533          * There can be multiple actions per IRQ descriptor, find the right
1534          * one based on the dev_id:
1535          */
1536         action_ptr = &desc->action;
1537         for (;;) {
1538                 action = *action_ptr;
1539
1540                 if (!action) {
1541                         WARN(1, "Trying to free already-free IRQ %d\n", irq);
1542                         raw_spin_unlock_irqrestore(&desc->lock, flags);
1543                         chip_bus_sync_unlock(desc);
1544                         return NULL;
1545                 }
1546
1547                 if (action->dev_id == dev_id)
1548                         break;
1549                 action_ptr = &action->next;
1550         }
1551
1552         /* Found it - now remove it from the list of entries: */
1553         *action_ptr = action->next;
1554
1555         irq_pm_remove_action(desc, action);
1556
1557         /* If this was the last handler, shut down the IRQ line: */
1558         if (!desc->action) {
1559                 irq_settings_clr_disable_unlazy(desc);
1560                 irq_shutdown(desc);
1561                 irq_release_resources(desc);
1562         }
1563
1564 #ifdef CONFIG_SMP
1565         /* make sure affinity_hint is cleaned up */
1566         if (WARN_ON_ONCE(desc->affinity_hint))
1567                 desc->affinity_hint = NULL;
1568 #endif
1569
1570         raw_spin_unlock_irqrestore(&desc->lock, flags);
1571         chip_bus_sync_unlock(desc);
1572
1573         unregister_handler_proc(irq, action);
1574
1575         /* Make sure it's not being used on another CPU: */
1576         synchronize_irq(irq);
1577
1578 #ifdef CONFIG_DEBUG_SHIRQ
1579         /*
1580          * It's a shared IRQ -- the driver ought to be prepared for an IRQ
1581          * event to happen even now it's being freed, so let's make sure that
1582          * is so by doing an extra call to the handler ....
1583          *
1584          * ( We do this after actually deregistering it, to make sure that a
1585          *   'real' IRQ doesn't run in * parallel with our fake. )
1586          */
1587         if (action->flags & IRQF_SHARED) {
1588                 local_irq_save(flags);
1589                 action->handler(irq, dev_id);
1590                 local_irq_restore(flags);
1591         }
1592 #endif
1593
1594         if (action->thread) {
1595                 kthread_stop(action->thread);
1596                 put_task_struct(action->thread);
1597                 if (action->secondary && action->secondary->thread) {
1598                         kthread_stop(action->secondary->thread);
1599                         put_task_struct(action->secondary->thread);
1600                 }
1601         }
1602
1603         module_put(desc->owner);
1604         kfree(action->secondary);
1605         return action;
1606 }
1607
1608 /**
1609  *      remove_irq - free an interrupt
1610  *      @irq: Interrupt line to free
1611  *      @act: irqaction for the interrupt
1612  *
1613  * Used to remove interrupts statically setup by the early boot process.
1614  */
1615 void remove_irq(unsigned int irq, struct irqaction *act)
1616 {
1617         struct irq_desc *desc = irq_to_desc(irq);
1618
1619         if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1620             __free_irq(irq, act->dev_id);
1621 }
1622 EXPORT_SYMBOL_GPL(remove_irq);
1623
1624 /**
1625  *      free_irq - free an interrupt allocated with request_irq
1626  *      @irq: Interrupt line to free
1627  *      @dev_id: Device identity to free
1628  *
1629  *      Remove an interrupt handler. The handler is removed and if the
1630  *      interrupt line is no longer in use by any driver it is disabled.
1631  *      On a shared IRQ the caller must ensure the interrupt is disabled
1632  *      on the card it drives before calling this function. The function
1633  *      does not return until any executing interrupts for this IRQ
1634  *      have completed.
1635  *
1636  *      This function must not be called from interrupt context.
1637  */
1638 void free_irq(unsigned int irq, void *dev_id)
1639 {
1640         struct irq_desc *desc = irq_to_desc(irq);
1641
1642         if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1643                 return;
1644
1645 #ifdef CONFIG_SMP
1646         if (WARN_ON(desc->affinity_notify))
1647                 desc->affinity_notify = NULL;
1648 #endif
1649
1650         kfree(__free_irq(irq, dev_id));
1651 }
1652 EXPORT_SYMBOL(free_irq);
1653
1654 /**
1655  *      request_threaded_irq - allocate an interrupt line
1656  *      @irq: Interrupt line to allocate
1657  *      @handler: Function to be called when the IRQ occurs.
1658  *                Primary handler for threaded interrupts
1659  *                If NULL and thread_fn != NULL the default
1660  *                primary handler is installed
1661  *      @thread_fn: Function called from the irq handler thread
1662  *                  If NULL, no irq thread is created
1663  *      @irqflags: Interrupt type flags
1664  *      @devname: An ascii name for the claiming device
1665  *      @dev_id: A cookie passed back to the handler function
1666  *
1667  *      This call allocates interrupt resources and enables the
1668  *      interrupt line and IRQ handling. From the point this
1669  *      call is made your handler function may be invoked. Since
1670  *      your handler function must clear any interrupt the board
1671  *      raises, you must take care both to initialise your hardware
1672  *      and to set up the interrupt handler in the right order.
1673  *
1674  *      If you want to set up a threaded irq handler for your device
1675  *      then you need to supply @handler and @thread_fn. @handler is
1676  *      still called in hard interrupt context and has to check
1677  *      whether the interrupt originates from the device. If yes it
1678  *      needs to disable the interrupt on the device and return
1679  *      IRQ_WAKE_THREAD which will wake up the handler thread and run
1680  *      @thread_fn. This split handler design is necessary to support
1681  *      shared interrupts.
1682  *
1683  *      Dev_id must be globally unique. Normally the address of the
1684  *      device data structure is used as the cookie. Since the handler
1685  *      receives this value it makes sense to use it.
1686  *
1687  *      If your interrupt is shared you must pass a non NULL dev_id
1688  *      as this is required when freeing the interrupt.
1689  *
1690  *      Flags:
1691  *
1692  *      IRQF_SHARED             Interrupt is shared
1693  *      IRQF_TRIGGER_*          Specify active edge(s) or level
1694  *
1695  */
1696 int request_threaded_irq(unsigned int irq, irq_handler_t handler,
1697                          irq_handler_t thread_fn, unsigned long irqflags,
1698                          const char *devname, void *dev_id)
1699 {
1700         struct irqaction *action;
1701         struct irq_desc *desc;
1702         int retval;
1703
1704         /*
1705          * Sanity-check: shared interrupts must pass in a real dev-ID,
1706          * otherwise we'll have trouble later trying to figure out
1707          * which interrupt is which (messes up the interrupt freeing
1708          * logic etc).
1709          *
1710          * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
1711          * it cannot be set along with IRQF_NO_SUSPEND.
1712          */
1713         if (((irqflags & IRQF_SHARED) && !dev_id) ||
1714             (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
1715             ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
1716                 return -EINVAL;
1717
1718         desc = irq_to_desc(irq);
1719         if (!desc)
1720                 return -EINVAL;
1721
1722         if (!irq_settings_can_request(desc) ||
1723             WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1724                 return -EINVAL;
1725
1726         if (!handler) {
1727                 if (!thread_fn)
1728                         return -EINVAL;
1729                 handler = irq_default_primary_handler;
1730         }
1731
1732         action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1733         if (!action)
1734                 return -ENOMEM;
1735
1736         action->handler = handler;
1737         action->thread_fn = thread_fn;
1738         action->flags = irqflags;
1739         action->name = devname;
1740         action->dev_id = dev_id;
1741
1742         chip_bus_lock(desc);
1743         retval = __setup_irq(irq, desc, action);
1744         chip_bus_sync_unlock(desc);
1745
1746         if (retval) {
1747                 kfree(action->secondary);
1748                 kfree(action);
1749         }
1750
1751 #ifdef CONFIG_DEBUG_SHIRQ_FIXME
1752         if (!retval && (irqflags & IRQF_SHARED)) {
1753                 /*
1754                  * It's a shared IRQ -- the driver ought to be prepared for it
1755                  * to happen immediately, so let's make sure....
1756                  * We disable the irq to make sure that a 'real' IRQ doesn't
1757                  * run in parallel with our fake.
1758                  */
1759                 unsigned long flags;
1760
1761                 disable_irq(irq);
1762                 local_irq_save(flags);
1763
1764                 handler(irq, dev_id);
1765
1766                 local_irq_restore(flags);
1767                 enable_irq(irq);
1768         }
1769 #endif
1770         return retval;
1771 }
1772 EXPORT_SYMBOL(request_threaded_irq);
1773
1774 /**
1775  *      request_any_context_irq - allocate an interrupt line
1776  *      @irq: Interrupt line to allocate
1777  *      @handler: Function to be called when the IRQ occurs.
1778  *                Threaded handler for threaded interrupts.
1779  *      @flags: Interrupt type flags
1780  *      @name: An ascii name for the claiming device
1781  *      @dev_id: A cookie passed back to the handler function
1782  *
1783  *      This call allocates interrupt resources and enables the
1784  *      interrupt line and IRQ handling. It selects either a
1785  *      hardirq or threaded handling method depending on the
1786  *      context.
1787  *
1788  *      On failure, it returns a negative value. On success,
1789  *      it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
1790  */
1791 int request_any_context_irq(unsigned int irq, irq_handler_t handler,
1792                             unsigned long flags, const char *name, void *dev_id)
1793 {
1794         struct irq_desc *desc = irq_to_desc(irq);
1795         int ret;
1796
1797         if (!desc)
1798                 return -EINVAL;
1799
1800         if (irq_settings_is_nested_thread(desc)) {
1801                 ret = request_threaded_irq(irq, NULL, handler,
1802                                            flags, name, dev_id);
1803                 return !ret ? IRQC_IS_NESTED : ret;
1804         }
1805
1806         ret = request_irq(irq, handler, flags, name, dev_id);
1807         return !ret ? IRQC_IS_HARDIRQ : ret;
1808 }
1809 EXPORT_SYMBOL_GPL(request_any_context_irq);
1810
1811 void enable_percpu_irq(unsigned int irq, unsigned int type)
1812 {
1813         unsigned int cpu = smp_processor_id();
1814         unsigned long flags;
1815         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1816
1817         if (!desc)
1818                 return;
1819
1820         type &= IRQ_TYPE_SENSE_MASK;
1821         if (type != IRQ_TYPE_NONE) {
1822                 int ret;
1823
1824                 ret = __irq_set_trigger(desc, type);
1825
1826                 if (ret) {
1827                         WARN(1, "failed to set type for IRQ%d\n", irq);
1828                         goto out;
1829                 }
1830         }
1831
1832         irq_percpu_enable(desc, cpu);
1833 out:
1834         irq_put_desc_unlock(desc, flags);
1835 }
1836 EXPORT_SYMBOL_GPL(enable_percpu_irq);
1837
1838 void disable_percpu_irq(unsigned int irq)
1839 {
1840         unsigned int cpu = smp_processor_id();
1841         unsigned long flags;
1842         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
1843
1844         if (!desc)
1845                 return;
1846
1847         irq_percpu_disable(desc, cpu);
1848         irq_put_desc_unlock(desc, flags);
1849 }
1850 EXPORT_SYMBOL_GPL(disable_percpu_irq);
1851
1852 /*
1853  * Internal function to unregister a percpu irqaction.
1854  */
1855 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1856 {
1857         struct irq_desc *desc = irq_to_desc(irq);
1858         struct irqaction *action;
1859         unsigned long flags;
1860
1861         WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
1862
1863         if (!desc)
1864                 return NULL;
1865
1866         raw_spin_lock_irqsave(&desc->lock, flags);
1867
1868         action = desc->action;
1869         if (!action || action->percpu_dev_id != dev_id) {
1870                 WARN(1, "Trying to free already-free IRQ %d\n", irq);
1871                 goto bad;
1872         }
1873
1874         if (!cpumask_empty(desc->percpu_enabled)) {
1875                 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
1876                      irq, cpumask_first(desc->percpu_enabled));
1877                 goto bad;
1878         }
1879
1880         /* Found it - now remove it from the list of entries: */
1881         desc->action = NULL;
1882
1883         raw_spin_unlock_irqrestore(&desc->lock, flags);
1884
1885         unregister_handler_proc(irq, action);
1886
1887         module_put(desc->owner);
1888         return action;
1889
1890 bad:
1891         raw_spin_unlock_irqrestore(&desc->lock, flags);
1892         return NULL;
1893 }
1894
1895 /**
1896  *      remove_percpu_irq - free a per-cpu interrupt
1897  *      @irq: Interrupt line to free
1898  *      @act: irqaction for the interrupt
1899  *
1900  * Used to remove interrupts statically setup by the early boot process.
1901  */
1902 void remove_percpu_irq(unsigned int irq, struct irqaction *act)
1903 {
1904         struct irq_desc *desc = irq_to_desc(irq);
1905
1906         if (desc && irq_settings_is_per_cpu_devid(desc))
1907             __free_percpu_irq(irq, act->percpu_dev_id);
1908 }
1909
1910 /**
1911  *      free_percpu_irq - free an interrupt allocated with request_percpu_irq
1912  *      @irq: Interrupt line to free
1913  *      @dev_id: Device identity to free
1914  *
1915  *      Remove a percpu interrupt handler. The handler is removed, but
1916  *      the interrupt line is not disabled. This must be done on each
1917  *      CPU before calling this function. The function does not return
1918  *      until any executing interrupts for this IRQ have completed.
1919  *
1920  *      This function must not be called from interrupt context.
1921  */
1922 void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
1923 {
1924         struct irq_desc *desc = irq_to_desc(irq);
1925
1926         if (!desc || !irq_settings_is_per_cpu_devid(desc))
1927                 return;
1928
1929         chip_bus_lock(desc);
1930         kfree(__free_percpu_irq(irq, dev_id));
1931         chip_bus_sync_unlock(desc);
1932 }
1933 EXPORT_SYMBOL_GPL(free_percpu_irq);
1934
1935 /**
1936  *      setup_percpu_irq - setup a per-cpu interrupt
1937  *      @irq: Interrupt line to setup
1938  *      @act: irqaction for the interrupt
1939  *
1940  * Used to statically setup per-cpu interrupts in the early boot process.
1941  */
1942 int setup_percpu_irq(unsigned int irq, struct irqaction *act)
1943 {
1944         struct irq_desc *desc = irq_to_desc(irq);
1945         int retval;
1946
1947         if (!desc || !irq_settings_is_per_cpu_devid(desc))
1948                 return -EINVAL;
1949         chip_bus_lock(desc);
1950         retval = __setup_irq(irq, desc, act);
1951         chip_bus_sync_unlock(desc);
1952
1953         return retval;
1954 }
1955
1956 /**
1957  *      request_percpu_irq - allocate a percpu interrupt line
1958  *      @irq: Interrupt line to allocate
1959  *      @handler: Function to be called when the IRQ occurs.
1960  *      @devname: An ascii name for the claiming device
1961  *      @dev_id: A percpu cookie passed back to the handler function
1962  *
1963  *      This call allocates interrupt resources and enables the
1964  *      interrupt on the local CPU. If the interrupt is supposed to be
1965  *      enabled on other CPUs, it has to be done on each CPU using
1966  *      enable_percpu_irq().
1967  *
1968  *      Dev_id must be globally unique. It is a per-cpu variable, and
1969  *      the handler gets called with the interrupted CPU's instance of
1970  *      that variable.
1971  */
1972 int request_percpu_irq(unsigned int irq, irq_handler_t handler,
1973                        const char *devname, void __percpu *dev_id)
1974 {
1975         struct irqaction *action;
1976         struct irq_desc *desc;
1977         int retval;
1978
1979         if (!dev_id)
1980                 return -EINVAL;
1981
1982         desc = irq_to_desc(irq);
1983         if (!desc || !irq_settings_can_request(desc) ||
1984             !irq_settings_is_per_cpu_devid(desc))
1985                 return -EINVAL;
1986
1987         action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
1988         if (!action)
1989                 return -ENOMEM;
1990
1991         action->handler = handler;
1992         action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
1993         action->name = devname;
1994         action->percpu_dev_id = dev_id;
1995
1996         chip_bus_lock(desc);
1997         retval = __setup_irq(irq, desc, action);
1998         chip_bus_sync_unlock(desc);
1999
2000         if (retval)
2001                 kfree(action);
2002
2003         return retval;
2004 }
2005 EXPORT_SYMBOL_GPL(request_percpu_irq);
2006
2007 /**
2008  *      irq_get_irqchip_state - returns the irqchip state of a interrupt.
2009  *      @irq: Interrupt line that is forwarded to a VM
2010  *      @which: One of IRQCHIP_STATE_* the caller wants to know about
2011  *      @state: a pointer to a boolean where the state is to be storeed
2012  *
2013  *      This call snapshots the internal irqchip state of an
2014  *      interrupt, returning into @state the bit corresponding to
2015  *      stage @which
2016  *
2017  *      This function should be called with preemption disabled if the
2018  *      interrupt controller has per-cpu registers.
2019  */
2020 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2021                           bool *state)
2022 {
2023         struct irq_desc *desc;
2024         struct irq_data *data;
2025         struct irq_chip *chip;
2026         unsigned long flags;
2027         int err = -EINVAL;
2028
2029         desc = irq_get_desc_buslock(irq, &flags, 0);
2030         if (!desc)
2031                 return err;
2032
2033         data = irq_desc_get_irq_data(desc);
2034
2035         do {
2036                 chip = irq_data_get_irq_chip(data);
2037                 if (chip->irq_get_irqchip_state)
2038                         break;
2039 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2040                 data = data->parent_data;
2041 #else
2042                 data = NULL;
2043 #endif
2044         } while (data);
2045
2046         if (data)
2047                 err = chip->irq_get_irqchip_state(data, which, state);
2048
2049         irq_put_desc_busunlock(desc, flags);
2050         return err;
2051 }
2052 EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
2053
2054 /**
2055  *      irq_set_irqchip_state - set the state of a forwarded interrupt.
2056  *      @irq: Interrupt line that is forwarded to a VM
2057  *      @which: State to be restored (one of IRQCHIP_STATE_*)
2058  *      @val: Value corresponding to @which
2059  *
2060  *      This call sets the internal irqchip state of an interrupt,
2061  *      depending on the value of @which.
2062  *
2063  *      This function should be called with migration disabled if the
2064  *      interrupt controller has per-cpu registers.
2065  */
2066 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
2067                           bool val)
2068 {
2069         struct irq_desc *desc;
2070         struct irq_data *data;
2071         struct irq_chip *chip;
2072         unsigned long flags;
2073         int err = -EINVAL;
2074
2075         desc = irq_get_desc_buslock(irq, &flags, 0);
2076         if (!desc)
2077                 return err;
2078
2079         data = irq_desc_get_irq_data(desc);
2080
2081         do {
2082                 chip = irq_data_get_irq_chip(data);
2083                 if (chip->irq_set_irqchip_state)
2084                         break;
2085 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2086                 data = data->parent_data;
2087 #else
2088                 data = NULL;
2089 #endif
2090         } while (data);
2091
2092         if (data)
2093                 err = chip->irq_set_irqchip_state(data, which, val);
2094
2095         irq_put_desc_busunlock(desc, flags);
2096         return err;
2097 }
2098 EXPORT_SYMBOL_GPL(irq_set_irqchip_state);