~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/irq/manage.c

Version: ~ [ linux-5.13-rc5 ] ~ [ linux-5.12.9 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.42 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.124 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.193 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.235 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.271 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.271 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp