]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - kernel/irq/chip.c
fc89eeb8a6b47df43cd30392e149847eab89f4ae
[karo-tx-linux.git] / kernel / irq / chip.c
1 /*
2  * linux/kernel/irq/chip.c
3  *
4  * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5  * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
6  *
7  * This file contains the core interrupt handling code, for irq-chip
8  * based architectures.
9  *
10  * Detailed information is available in Documentation/DocBook/genericirq
11  */
12
13 #include <linux/irq.h>
14 #include <linux/msi.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/irqdomain.h>
19
20 #include <trace/events/irq.h>
21
22 #include "internals.h"
23
24 static irqreturn_t bad_chained_irq(int irq, void *dev_id)
25 {
26         WARN_ONCE(1, "Chained irq %d should not call an action\n", irq);
27         return IRQ_NONE;
28 }
29
30 /*
31  * Chained handlers should never call action on their IRQ. This default
32  * action will emit warning if such thing happens.
33  */
34 struct irqaction chained_action = {
35         .handler = bad_chained_irq,
36 };
37
38 /**
39  *      irq_set_chip - set the irq chip for an irq
40  *      @irq:   irq number
41  *      @chip:  pointer to irq chip description structure
42  */
43 int irq_set_chip(unsigned int irq, struct irq_chip *chip)
44 {
45         unsigned long flags;
46         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
47
48         if (!desc)
49                 return -EINVAL;
50
51         if (!chip)
52                 chip = &no_irq_chip;
53
54         desc->irq_data.chip = chip;
55         irq_put_desc_unlock(desc, flags);
56         /*
57          * For !CONFIG_SPARSE_IRQ make the irq show up in
58          * allocated_irqs.
59          */
60         irq_mark_irq(irq);
61         return 0;
62 }
63 EXPORT_SYMBOL(irq_set_chip);
64
65 /**
66  *      irq_set_type - set the irq trigger type for an irq
67  *      @irq:   irq number
68  *      @type:  IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
69  */
70 int irq_set_irq_type(unsigned int irq, unsigned int type)
71 {
72         unsigned long flags;
73         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
74         int ret = 0;
75
76         if (!desc)
77                 return -EINVAL;
78
79         ret = __irq_set_trigger(desc, type);
80         irq_put_desc_busunlock(desc, flags);
81         return ret;
82 }
83 EXPORT_SYMBOL(irq_set_irq_type);
84
85 /**
86  *      irq_set_handler_data - set irq handler data for an irq
87  *      @irq:   Interrupt number
88  *      @data:  Pointer to interrupt specific data
89  *
90  *      Set the hardware irq controller data for an irq
91  */
92 int irq_set_handler_data(unsigned int irq, void *data)
93 {
94         unsigned long flags;
95         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
96
97         if (!desc)
98                 return -EINVAL;
99         desc->irq_common_data.handler_data = data;
100         irq_put_desc_unlock(desc, flags);
101         return 0;
102 }
103 EXPORT_SYMBOL(irq_set_handler_data);
104
105 /**
106  *      irq_set_msi_desc_off - set MSI descriptor data for an irq at offset
107  *      @irq_base:      Interrupt number base
108  *      @irq_offset:    Interrupt number offset
109  *      @entry:         Pointer to MSI descriptor data
110  *
111  *      Set the MSI descriptor entry for an irq at offset
112  */
113 int irq_set_msi_desc_off(unsigned int irq_base, unsigned int irq_offset,
114                          struct msi_desc *entry)
115 {
116         unsigned long flags;
117         struct irq_desc *desc = irq_get_desc_lock(irq_base + irq_offset, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
118
119         if (!desc)
120                 return -EINVAL;
121         desc->irq_common_data.msi_desc = entry;
122         if (entry && !irq_offset)
123                 entry->irq = irq_base;
124         irq_put_desc_unlock(desc, flags);
125         return 0;
126 }
127
128 /**
129  *      irq_set_msi_desc - set MSI descriptor data for an irq
130  *      @irq:   Interrupt number
131  *      @entry: Pointer to MSI descriptor data
132  *
133  *      Set the MSI descriptor entry for an irq
134  */
135 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
136 {
137         return irq_set_msi_desc_off(irq, 0, entry);
138 }
139
140 /**
141  *      irq_set_chip_data - set irq chip data for an irq
142  *      @irq:   Interrupt number
143  *      @data:  Pointer to chip specific data
144  *
145  *      Set the hardware irq chip data for an irq
146  */
147 int irq_set_chip_data(unsigned int irq, void *data)
148 {
149         unsigned long flags;
150         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
151
152         if (!desc)
153                 return -EINVAL;
154         desc->irq_data.chip_data = data;
155         irq_put_desc_unlock(desc, flags);
156         return 0;
157 }
158 EXPORT_SYMBOL(irq_set_chip_data);
159
160 struct irq_data *irq_get_irq_data(unsigned int irq)
161 {
162         struct irq_desc *desc = irq_to_desc(irq);
163
164         return desc ? &desc->irq_data : NULL;
165 }
166 EXPORT_SYMBOL_GPL(irq_get_irq_data);
167
168 static void irq_state_clr_disabled(struct irq_desc *desc)
169 {
170         irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
171 }
172
173 static void irq_state_set_disabled(struct irq_desc *desc)
174 {
175         irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
176 }
177
178 static void irq_state_clr_masked(struct irq_desc *desc)
179 {
180         irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
181 }
182
183 static void irq_state_set_masked(struct irq_desc *desc)
184 {
185         irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
186 }
187
188 static void irq_state_clr_started(struct irq_desc *desc)
189 {
190         irqd_clear(&desc->irq_data, IRQD_IRQ_STARTED);
191 }
192
193 static void irq_state_set_started(struct irq_desc *desc)
194 {
195         irqd_set(&desc->irq_data, IRQD_IRQ_STARTED);
196 }
197
198 enum {
199         IRQ_STARTUP_NORMAL,
200         IRQ_STARTUP_MANAGED,
201         IRQ_STARTUP_ABORT,
202 };
203
204 #ifdef CONFIG_SMP
205 static int
206 __irq_startup_managed(struct irq_desc *desc, struct cpumask *aff, bool force)
207 {
208         struct irq_data *d = irq_desc_get_irq_data(desc);
209
210         if (!irqd_affinity_is_managed(d))
211                 return IRQ_STARTUP_NORMAL;
212
213         irqd_clr_managed_shutdown(d);
214
215         if (cpumask_any_and(aff, cpu_online_mask) > nr_cpu_ids) {
216                 /*
217                  * Catch code which fiddles with enable_irq() on a managed
218                  * and potentially shutdown IRQ. Chained interrupt
219                  * installment or irq auto probing should not happen on
220                  * managed irqs either. Emit a warning, break the affinity
221                  * and start it up as a normal interrupt.
222                  */
223                 if (WARN_ON_ONCE(force))
224                         return IRQ_STARTUP_NORMAL;
225                 /*
226                  * The interrupt was requested, but there is no online CPU
227                  * in it's affinity mask. Put it into managed shutdown
228                  * state and let the cpu hotplug mechanism start it up once
229                  * a CPU in the mask becomes available.
230                  */
231                 irqd_set_managed_shutdown(d);
232                 return IRQ_STARTUP_ABORT;
233         }
234         return IRQ_STARTUP_MANAGED;
235 }
236 #else
237 static int
238 __irq_startup_managed(struct irq_desc *desc, struct cpumask *aff, bool force)
239 {
240         return IRQ_STARTUP_NORMAL;
241 }
242 #endif
243
244 static int __irq_startup(struct irq_desc *desc)
245 {
246         struct irq_data *d = irq_desc_get_irq_data(desc);
247         int ret = 0;
248
249         irq_domain_activate_irq(d);
250         if (d->chip->irq_startup) {
251                 ret = d->chip->irq_startup(d);
252                 irq_state_clr_disabled(desc);
253                 irq_state_clr_masked(desc);
254         } else {
255                 irq_enable(desc);
256         }
257         irq_state_set_started(desc);
258         return ret;
259 }
260
261 int irq_startup(struct irq_desc *desc, bool resend, bool force)
262 {
263         struct irq_data *d = irq_desc_get_irq_data(desc);
264         struct cpumask *aff = irq_data_get_affinity_mask(d);
265         int ret = 0;
266
267         desc->depth = 0;
268
269         if (irqd_is_started(d)) {
270                 irq_enable(desc);
271         } else {
272                 switch (__irq_startup_managed(desc, aff, force)) {
273                 case IRQ_STARTUP_NORMAL:
274                         ret = __irq_startup(desc);
275                         irq_setup_affinity(desc);
276                         break;
277                 case IRQ_STARTUP_MANAGED:
278                         ret = __irq_startup(desc);
279                         irq_set_affinity_locked(d, aff, false);
280                         break;
281                 case IRQ_STARTUP_ABORT:
282                         return 0;
283                 }
284         }
285         if (resend)
286                 check_irq_resend(desc);
287
288         return ret;
289 }
290
291 static void __irq_disable(struct irq_desc *desc, bool mask);
292
293 void irq_shutdown(struct irq_desc *desc)
294 {
295         if (irqd_is_started(&desc->irq_data)) {
296                 desc->depth = 1;
297                 if (desc->irq_data.chip->irq_shutdown) {
298                         desc->irq_data.chip->irq_shutdown(&desc->irq_data);
299                         irq_state_set_disabled(desc);
300                         irq_state_set_masked(desc);
301                 } else {
302                         __irq_disable(desc, true);
303                 }
304                 irq_state_clr_started(desc);
305         }
306         /*
307          * This must be called even if the interrupt was never started up,
308          * because the activation can happen before the interrupt is
309          * available for request/startup. It has it's own state tracking so
310          * it's safe to call it unconditionally.
311          */
312         irq_domain_deactivate_irq(&desc->irq_data);
313 }
314
315 void irq_enable(struct irq_desc *desc)
316 {
317         irq_state_clr_disabled(desc);
318         if (desc->irq_data.chip->irq_enable)
319                 desc->irq_data.chip->irq_enable(&desc->irq_data);
320         else
321                 desc->irq_data.chip->irq_unmask(&desc->irq_data);
322         irq_state_clr_masked(desc);
323 }
324
325 static void __irq_disable(struct irq_desc *desc, bool mask)
326 {
327         irq_state_set_disabled(desc);
328         if (desc->irq_data.chip->irq_disable) {
329                 desc->irq_data.chip->irq_disable(&desc->irq_data);
330                 irq_state_set_masked(desc);
331         } else if (mask) {
332                 mask_irq(desc);
333         }
334 }
335
336 /**
337  * irq_disable - Mark interrupt disabled
338  * @desc:       irq descriptor which should be disabled
339  *
340  * If the chip does not implement the irq_disable callback, we
341  * use a lazy disable approach. That means we mark the interrupt
342  * disabled, but leave the hardware unmasked. That's an
343  * optimization because we avoid the hardware access for the
344  * common case where no interrupt happens after we marked it
345  * disabled. If an interrupt happens, then the interrupt flow
346  * handler masks the line at the hardware level and marks it
347  * pending.
348  *
349  * If the interrupt chip does not implement the irq_disable callback,
350  * a driver can disable the lazy approach for a particular irq line by
351  * calling 'irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY)'. This can
352  * be used for devices which cannot disable the interrupt at the
353  * device level under certain circumstances and have to use
354  * disable_irq[_nosync] instead.
355  */
356 void irq_disable(struct irq_desc *desc)
357 {
358         __irq_disable(desc, irq_settings_disable_unlazy(desc));
359 }
360
361 void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
362 {
363         if (desc->irq_data.chip->irq_enable)
364                 desc->irq_data.chip->irq_enable(&desc->irq_data);
365         else
366                 desc->irq_data.chip->irq_unmask(&desc->irq_data);
367         cpumask_set_cpu(cpu, desc->percpu_enabled);
368 }
369
370 void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
371 {
372         if (desc->irq_data.chip->irq_disable)
373                 desc->irq_data.chip->irq_disable(&desc->irq_data);
374         else
375                 desc->irq_data.chip->irq_mask(&desc->irq_data);
376         cpumask_clear_cpu(cpu, desc->percpu_enabled);
377 }
378
379 static inline void mask_ack_irq(struct irq_desc *desc)
380 {
381         if (desc->irq_data.chip->irq_mask_ack)
382                 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
383         else {
384                 desc->irq_data.chip->irq_mask(&desc->irq_data);
385                 if (desc->irq_data.chip->irq_ack)
386                         desc->irq_data.chip->irq_ack(&desc->irq_data);
387         }
388         irq_state_set_masked(desc);
389 }
390
391 void mask_irq(struct irq_desc *desc)
392 {
393         if (desc->irq_data.chip->irq_mask) {
394                 desc->irq_data.chip->irq_mask(&desc->irq_data);
395                 irq_state_set_masked(desc);
396         }
397 }
398
399 void unmask_irq(struct irq_desc *desc)
400 {
401         if (desc->irq_data.chip->irq_unmask) {
402                 desc->irq_data.chip->irq_unmask(&desc->irq_data);
403                 irq_state_clr_masked(desc);
404         }
405 }
406
407 void unmask_threaded_irq(struct irq_desc *desc)
408 {
409         struct irq_chip *chip = desc->irq_data.chip;
410
411         if (chip->flags & IRQCHIP_EOI_THREADED)
412                 chip->irq_eoi(&desc->irq_data);
413
414         if (chip->irq_unmask) {
415                 chip->irq_unmask(&desc->irq_data);
416                 irq_state_clr_masked(desc);
417         }
418 }
419
420 /*
421  *      handle_nested_irq - Handle a nested irq from a irq thread
422  *      @irq:   the interrupt number
423  *
424  *      Handle interrupts which are nested into a threaded interrupt
425  *      handler. The handler function is called inside the calling
426  *      threads context.
427  */
428 void handle_nested_irq(unsigned int irq)
429 {
430         struct irq_desc *desc = irq_to_desc(irq);
431         struct irqaction *action;
432         irqreturn_t action_ret;
433
434         might_sleep();
435
436         raw_spin_lock_irq(&desc->lock);
437
438         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
439
440         action = desc->action;
441         if (unlikely(!action || irqd_irq_disabled(&desc->irq_data))) {
442                 desc->istate |= IRQS_PENDING;
443                 goto out_unlock;
444         }
445
446         kstat_incr_irqs_this_cpu(desc);
447         irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
448         raw_spin_unlock_irq(&desc->lock);
449
450         action_ret = IRQ_NONE;
451         for_each_action_of_desc(desc, action)
452                 action_ret |= action->thread_fn(action->irq, action->dev_id);
453
454         if (!noirqdebug)
455                 note_interrupt(desc, action_ret);
456
457         raw_spin_lock_irq(&desc->lock);
458         irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
459
460 out_unlock:
461         raw_spin_unlock_irq(&desc->lock);
462 }
463 EXPORT_SYMBOL_GPL(handle_nested_irq);
464
465 static bool irq_check_poll(struct irq_desc *desc)
466 {
467         if (!(desc->istate & IRQS_POLL_INPROGRESS))
468                 return false;
469         return irq_wait_for_poll(desc);
470 }
471
472 static bool irq_may_run(struct irq_desc *desc)
473 {
474         unsigned int mask = IRQD_IRQ_INPROGRESS | IRQD_WAKEUP_ARMED;
475
476         /*
477          * If the interrupt is not in progress and is not an armed
478          * wakeup interrupt, proceed.
479          */
480         if (!irqd_has_set(&desc->irq_data, mask))
481                 return true;
482
483         /*
484          * If the interrupt is an armed wakeup source, mark it pending
485          * and suspended, disable it and notify the pm core about the
486          * event.
487          */
488         if (irq_pm_check_wakeup(desc))
489                 return false;
490
491         /*
492          * Handle a potential concurrent poll on a different core.
493          */
494         return irq_check_poll(desc);
495 }
496
497 /**
498  *      handle_simple_irq - Simple and software-decoded IRQs.
499  *      @desc:  the interrupt description structure for this irq
500  *
501  *      Simple interrupts are either sent from a demultiplexing interrupt
502  *      handler or come from hardware, where no interrupt hardware control
503  *      is necessary.
504  *
505  *      Note: The caller is expected to handle the ack, clear, mask and
506  *      unmask issues if necessary.
507  */
508 void handle_simple_irq(struct irq_desc *desc)
509 {
510         raw_spin_lock(&desc->lock);
511
512         if (!irq_may_run(desc))
513                 goto out_unlock;
514
515         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
516
517         if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
518                 desc->istate |= IRQS_PENDING;
519                 goto out_unlock;
520         }
521
522         kstat_incr_irqs_this_cpu(desc);
523         handle_irq_event(desc);
524
525 out_unlock:
526         raw_spin_unlock(&desc->lock);
527 }
528 EXPORT_SYMBOL_GPL(handle_simple_irq);
529
530 /**
531  *      handle_untracked_irq - Simple and software-decoded IRQs.
532  *      @desc:  the interrupt description structure for this irq
533  *
534  *      Untracked interrupts are sent from a demultiplexing interrupt
535  *      handler when the demultiplexer does not know which device it its
536  *      multiplexed irq domain generated the interrupt. IRQ's handled
537  *      through here are not subjected to stats tracking, randomness, or
538  *      spurious interrupt detection.
539  *
540  *      Note: Like handle_simple_irq, the caller is expected to handle
541  *      the ack, clear, mask and unmask issues if necessary.
542  */
543 void handle_untracked_irq(struct irq_desc *desc)
544 {
545         unsigned int flags = 0;
546
547         raw_spin_lock(&desc->lock);
548
549         if (!irq_may_run(desc))
550                 goto out_unlock;
551
552         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
553
554         if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
555                 desc->istate |= IRQS_PENDING;
556                 goto out_unlock;
557         }
558
559         desc->istate &= ~IRQS_PENDING;
560         irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
561         raw_spin_unlock(&desc->lock);
562
563         __handle_irq_event_percpu(desc, &flags);
564
565         raw_spin_lock(&desc->lock);
566         irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
567
568 out_unlock:
569         raw_spin_unlock(&desc->lock);
570 }
571 EXPORT_SYMBOL_GPL(handle_untracked_irq);
572
573 /*
574  * Called unconditionally from handle_level_irq() and only for oneshot
575  * interrupts from handle_fasteoi_irq()
576  */
577 static void cond_unmask_irq(struct irq_desc *desc)
578 {
579         /*
580          * We need to unmask in the following cases:
581          * - Standard level irq (IRQF_ONESHOT is not set)
582          * - Oneshot irq which did not wake the thread (caused by a
583          *   spurious interrupt or a primary handler handling it
584          *   completely).
585          */
586         if (!irqd_irq_disabled(&desc->irq_data) &&
587             irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
588                 unmask_irq(desc);
589 }
590
591 /**
592  *      handle_level_irq - Level type irq handler
593  *      @desc:  the interrupt description structure for this irq
594  *
595  *      Level type interrupts are active as long as the hardware line has
596  *      the active level. This may require to mask the interrupt and unmask
597  *      it after the associated handler has acknowledged the device, so the
598  *      interrupt line is back to inactive.
599  */
600 void handle_level_irq(struct irq_desc *desc)
601 {
602         raw_spin_lock(&desc->lock);
603         mask_ack_irq(desc);
604
605         if (!irq_may_run(desc))
606                 goto out_unlock;
607
608         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
609
610         /*
611          * If its disabled or no action available
612          * keep it masked and get out of here
613          */
614         if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
615                 desc->istate |= IRQS_PENDING;
616                 goto out_unlock;
617         }
618
619         kstat_incr_irqs_this_cpu(desc);
620         handle_irq_event(desc);
621
622         cond_unmask_irq(desc);
623
624 out_unlock:
625         raw_spin_unlock(&desc->lock);
626 }
627 EXPORT_SYMBOL_GPL(handle_level_irq);
628
629 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
630 static inline void preflow_handler(struct irq_desc *desc)
631 {
632         if (desc->preflow_handler)
633                 desc->preflow_handler(&desc->irq_data);
634 }
635 #else
636 static inline void preflow_handler(struct irq_desc *desc) { }
637 #endif
638
639 static void cond_unmask_eoi_irq(struct irq_desc *desc, struct irq_chip *chip)
640 {
641         if (!(desc->istate & IRQS_ONESHOT)) {
642                 chip->irq_eoi(&desc->irq_data);
643                 return;
644         }
645         /*
646          * We need to unmask in the following cases:
647          * - Oneshot irq which did not wake the thread (caused by a
648          *   spurious interrupt or a primary handler handling it
649          *   completely).
650          */
651         if (!irqd_irq_disabled(&desc->irq_data) &&
652             irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot) {
653                 chip->irq_eoi(&desc->irq_data);
654                 unmask_irq(desc);
655         } else if (!(chip->flags & IRQCHIP_EOI_THREADED)) {
656                 chip->irq_eoi(&desc->irq_data);
657         }
658 }
659
660 /**
661  *      handle_fasteoi_irq - irq handler for transparent controllers
662  *      @desc:  the interrupt description structure for this irq
663  *
664  *      Only a single callback will be issued to the chip: an ->eoi()
665  *      call when the interrupt has been serviced. This enables support
666  *      for modern forms of interrupt handlers, which handle the flow
667  *      details in hardware, transparently.
668  */
669 void handle_fasteoi_irq(struct irq_desc *desc)
670 {
671         struct irq_chip *chip = desc->irq_data.chip;
672
673         raw_spin_lock(&desc->lock);
674
675         if (!irq_may_run(desc))
676                 goto out;
677
678         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
679
680         /*
681          * If its disabled or no action available
682          * then mask it and get out of here:
683          */
684         if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
685                 desc->istate |= IRQS_PENDING;
686                 mask_irq(desc);
687                 goto out;
688         }
689
690         kstat_incr_irqs_this_cpu(desc);
691         if (desc->istate & IRQS_ONESHOT)
692                 mask_irq(desc);
693
694         preflow_handler(desc);
695         handle_irq_event(desc);
696
697         cond_unmask_eoi_irq(desc, chip);
698
699         raw_spin_unlock(&desc->lock);
700         return;
701 out:
702         if (!(chip->flags & IRQCHIP_EOI_IF_HANDLED))
703                 chip->irq_eoi(&desc->irq_data);
704         raw_spin_unlock(&desc->lock);
705 }
706 EXPORT_SYMBOL_GPL(handle_fasteoi_irq);
707
708 /**
709  *      handle_edge_irq - edge type IRQ handler
710  *      @desc:  the interrupt description structure for this irq
711  *
712  *      Interrupt occures on the falling and/or rising edge of a hardware
713  *      signal. The occurrence is latched into the irq controller hardware
714  *      and must be acked in order to be reenabled. After the ack another
715  *      interrupt can happen on the same source even before the first one
716  *      is handled by the associated event handler. If this happens it
717  *      might be necessary to disable (mask) the interrupt depending on the
718  *      controller hardware. This requires to reenable the interrupt inside
719  *      of the loop which handles the interrupts which have arrived while
720  *      the handler was running. If all pending interrupts are handled, the
721  *      loop is left.
722  */
723 void handle_edge_irq(struct irq_desc *desc)
724 {
725         raw_spin_lock(&desc->lock);
726
727         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
728
729         if (!irq_may_run(desc)) {
730                 desc->istate |= IRQS_PENDING;
731                 mask_ack_irq(desc);
732                 goto out_unlock;
733         }
734
735         /*
736          * If its disabled or no action available then mask it and get
737          * out of here.
738          */
739         if (irqd_irq_disabled(&desc->irq_data) || !desc->action) {
740                 desc->istate |= IRQS_PENDING;
741                 mask_ack_irq(desc);
742                 goto out_unlock;
743         }
744
745         kstat_incr_irqs_this_cpu(desc);
746
747         /* Start handling the irq */
748         desc->irq_data.chip->irq_ack(&desc->irq_data);
749
750         do {
751                 if (unlikely(!desc->action)) {
752                         mask_irq(desc);
753                         goto out_unlock;
754                 }
755
756                 /*
757                  * When another irq arrived while we were handling
758                  * one, we could have masked the irq.
759                  * Renable it, if it was not disabled in meantime.
760                  */
761                 if (unlikely(desc->istate & IRQS_PENDING)) {
762                         if (!irqd_irq_disabled(&desc->irq_data) &&
763                             irqd_irq_masked(&desc->irq_data))
764                                 unmask_irq(desc);
765                 }
766
767                 handle_irq_event(desc);
768
769         } while ((desc->istate & IRQS_PENDING) &&
770                  !irqd_irq_disabled(&desc->irq_data));
771
772 out_unlock:
773         raw_spin_unlock(&desc->lock);
774 }
775 EXPORT_SYMBOL(handle_edge_irq);
776
777 #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
778 /**
779  *      handle_edge_eoi_irq - edge eoi type IRQ handler
780  *      @desc:  the interrupt description structure for this irq
781  *
782  * Similar as the above handle_edge_irq, but using eoi and w/o the
783  * mask/unmask logic.
784  */
785 void handle_edge_eoi_irq(struct irq_desc *desc)
786 {
787         struct irq_chip *chip = irq_desc_get_chip(desc);
788
789         raw_spin_lock(&desc->lock);
790
791         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
792
793         if (!irq_may_run(desc)) {
794                 desc->istate |= IRQS_PENDING;
795                 goto out_eoi;
796         }
797
798         /*
799          * If its disabled or no action available then mask it and get
800          * out of here.
801          */
802         if (irqd_irq_disabled(&desc->irq_data) || !desc->action) {
803                 desc->istate |= IRQS_PENDING;
804                 goto out_eoi;
805         }
806
807         kstat_incr_irqs_this_cpu(desc);
808
809         do {
810                 if (unlikely(!desc->action))
811                         goto out_eoi;
812
813                 handle_irq_event(desc);
814
815         } while ((desc->istate & IRQS_PENDING) &&
816                  !irqd_irq_disabled(&desc->irq_data));
817
818 out_eoi:
819         chip->irq_eoi(&desc->irq_data);
820         raw_spin_unlock(&desc->lock);
821 }
822 #endif
823
824 /**
825  *      handle_percpu_irq - Per CPU local irq handler
826  *      @desc:  the interrupt description structure for this irq
827  *
828  *      Per CPU interrupts on SMP machines without locking requirements
829  */
830 void handle_percpu_irq(struct irq_desc *desc)
831 {
832         struct irq_chip *chip = irq_desc_get_chip(desc);
833
834         kstat_incr_irqs_this_cpu(desc);
835
836         if (chip->irq_ack)
837                 chip->irq_ack(&desc->irq_data);
838
839         handle_irq_event_percpu(desc);
840
841         if (chip->irq_eoi)
842                 chip->irq_eoi(&desc->irq_data);
843 }
844
845 /**
846  * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
847  * @desc:       the interrupt description structure for this irq
848  *
849  * Per CPU interrupts on SMP machines without locking requirements. Same as
850  * handle_percpu_irq() above but with the following extras:
851  *
852  * action->percpu_dev_id is a pointer to percpu variables which
853  * contain the real device id for the cpu on which this handler is
854  * called
855  */
856 void handle_percpu_devid_irq(struct irq_desc *desc)
857 {
858         struct irq_chip *chip = irq_desc_get_chip(desc);
859         struct irqaction *action = desc->action;
860         unsigned int irq = irq_desc_get_irq(desc);
861         irqreturn_t res;
862
863         kstat_incr_irqs_this_cpu(desc);
864
865         if (chip->irq_ack)
866                 chip->irq_ack(&desc->irq_data);
867
868         if (likely(action)) {
869                 trace_irq_handler_entry(irq, action);
870                 res = action->handler(irq, raw_cpu_ptr(action->percpu_dev_id));
871                 trace_irq_handler_exit(irq, action, res);
872         } else {
873                 unsigned int cpu = smp_processor_id();
874                 bool enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
875
876                 if (enabled)
877                         irq_percpu_disable(desc, cpu);
878
879                 pr_err_once("Spurious%s percpu IRQ%u on CPU%u\n",
880                             enabled ? " and unmasked" : "", irq, cpu);
881         }
882
883         if (chip->irq_eoi)
884                 chip->irq_eoi(&desc->irq_data);
885 }
886
887 static void
888 __irq_do_set_handler(struct irq_desc *desc, irq_flow_handler_t handle,
889                      int is_chained, const char *name)
890 {
891         if (!handle) {
892                 handle = handle_bad_irq;
893         } else {
894                 struct irq_data *irq_data = &desc->irq_data;
895 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
896                 /*
897                  * With hierarchical domains we might run into a
898                  * situation where the outermost chip is not yet set
899                  * up, but the inner chips are there.  Instead of
900                  * bailing we install the handler, but obviously we
901                  * cannot enable/startup the interrupt at this point.
902                  */
903                 while (irq_data) {
904                         if (irq_data->chip != &no_irq_chip)
905                                 break;
906                         /*
907                          * Bail out if the outer chip is not set up
908                          * and the interrrupt supposed to be started
909                          * right away.
910                          */
911                         if (WARN_ON(is_chained))
912                                 return;
913                         /* Try the parent */
914                         irq_data = irq_data->parent_data;
915                 }
916 #endif
917                 if (WARN_ON(!irq_data || irq_data->chip == &no_irq_chip))
918                         return;
919         }
920
921         /* Uninstall? */
922         if (handle == handle_bad_irq) {
923                 if (desc->irq_data.chip != &no_irq_chip)
924                         mask_ack_irq(desc);
925                 irq_state_set_disabled(desc);
926                 if (is_chained)
927                         desc->action = NULL;
928                 desc->depth = 1;
929         }
930         desc->handle_irq = handle;
931         desc->name = name;
932
933         if (handle != handle_bad_irq && is_chained) {
934                 unsigned int type = irqd_get_trigger_type(&desc->irq_data);
935
936                 /*
937                  * We're about to start this interrupt immediately,
938                  * hence the need to set the trigger configuration.
939                  * But the .set_type callback may have overridden the
940                  * flow handler, ignoring that we're dealing with a
941                  * chained interrupt. Reset it immediately because we
942                  * do know better.
943                  */
944                 if (type != IRQ_TYPE_NONE) {
945                         __irq_set_trigger(desc, type);
946                         desc->handle_irq = handle;
947                 }
948
949                 irq_settings_set_noprobe(desc);
950                 irq_settings_set_norequest(desc);
951                 irq_settings_set_nothread(desc);
952                 desc->action = &chained_action;
953                 irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE);
954         }
955 }
956
957 void
958 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
959                   const char *name)
960 {
961         unsigned long flags;
962         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
963
964         if (!desc)
965                 return;
966
967         __irq_do_set_handler(desc, handle, is_chained, name);
968         irq_put_desc_busunlock(desc, flags);
969 }
970 EXPORT_SYMBOL_GPL(__irq_set_handler);
971
972 void
973 irq_set_chained_handler_and_data(unsigned int irq, irq_flow_handler_t handle,
974                                  void *data)
975 {
976         unsigned long flags;
977         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
978
979         if (!desc)
980                 return;
981
982         desc->irq_common_data.handler_data = data;
983         __irq_do_set_handler(desc, handle, 1, NULL);
984
985         irq_put_desc_busunlock(desc, flags);
986 }
987 EXPORT_SYMBOL_GPL(irq_set_chained_handler_and_data);
988
989 void
990 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
991                               irq_flow_handler_t handle, const char *name)
992 {
993         irq_set_chip(irq, chip);
994         __irq_set_handler(irq, handle, 0, name);
995 }
996 EXPORT_SYMBOL_GPL(irq_set_chip_and_handler_name);
997
998 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
999 {
1000         unsigned long flags;
1001         struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
1002
1003         if (!desc)
1004                 return;
1005
1006         /*
1007          * Warn when a driver sets the no autoenable flag on an already
1008          * active interrupt.
1009          */
1010         WARN_ON_ONCE(!desc->depth && (set & _IRQ_NOAUTOEN));
1011
1012         irq_settings_clr_and_set(desc, clr, set);
1013
1014         irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
1015                    IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
1016         if (irq_settings_has_no_balance_set(desc))
1017                 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
1018         if (irq_settings_is_per_cpu(desc))
1019                 irqd_set(&desc->irq_data, IRQD_PER_CPU);
1020         if (irq_settings_can_move_pcntxt(desc))
1021                 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
1022         if (irq_settings_is_level(desc))
1023                 irqd_set(&desc->irq_data, IRQD_LEVEL);
1024
1025         irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
1026
1027         irq_put_desc_unlock(desc, flags);
1028 }
1029 EXPORT_SYMBOL_GPL(irq_modify_status);
1030
1031 /**
1032  *      irq_cpu_online - Invoke all irq_cpu_online functions.
1033  *
1034  *      Iterate through all irqs and invoke the chip.irq_cpu_online()
1035  *      for each.
1036  */
1037 void irq_cpu_online(void)
1038 {
1039         struct irq_desc *desc;
1040         struct irq_chip *chip;
1041         unsigned long flags;
1042         unsigned int irq;
1043
1044         for_each_active_irq(irq) {
1045                 desc = irq_to_desc(irq);
1046                 if (!desc)
1047                         continue;
1048
1049                 raw_spin_lock_irqsave(&desc->lock, flags);
1050
1051                 chip = irq_data_get_irq_chip(&desc->irq_data);
1052                 if (chip && chip->irq_cpu_online &&
1053                     (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
1054                      !irqd_irq_disabled(&desc->irq_data)))
1055                         chip->irq_cpu_online(&desc->irq_data);
1056
1057                 raw_spin_unlock_irqrestore(&desc->lock, flags);
1058         }
1059 }
1060
1061 /**
1062  *      irq_cpu_offline - Invoke all irq_cpu_offline functions.
1063  *
1064  *      Iterate through all irqs and invoke the chip.irq_cpu_offline()
1065  *      for each.
1066  */
1067 void irq_cpu_offline(void)
1068 {
1069         struct irq_desc *desc;
1070         struct irq_chip *chip;
1071         unsigned long flags;
1072         unsigned int irq;
1073
1074         for_each_active_irq(irq) {
1075                 desc = irq_to_desc(irq);
1076                 if (!desc)
1077                         continue;
1078
1079                 raw_spin_lock_irqsave(&desc->lock, flags);
1080
1081                 chip = irq_data_get_irq_chip(&desc->irq_data);
1082                 if (chip && chip->irq_cpu_offline &&
1083                     (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
1084                      !irqd_irq_disabled(&desc->irq_data)))
1085                         chip->irq_cpu_offline(&desc->irq_data);
1086
1087                 raw_spin_unlock_irqrestore(&desc->lock, flags);
1088         }
1089 }
1090
1091 #ifdef  CONFIG_IRQ_DOMAIN_HIERARCHY
1092 /**
1093  * irq_chip_enable_parent - Enable the parent interrupt (defaults to unmask if
1094  * NULL)
1095  * @data:       Pointer to interrupt specific data
1096  */
1097 void irq_chip_enable_parent(struct irq_data *data)
1098 {
1099         data = data->parent_data;
1100         if (data->chip->irq_enable)
1101                 data->chip->irq_enable(data);
1102         else
1103                 data->chip->irq_unmask(data);
1104 }
1105
1106 /**
1107  * irq_chip_disable_parent - Disable the parent interrupt (defaults to mask if
1108  * NULL)
1109  * @data:       Pointer to interrupt specific data
1110  */
1111 void irq_chip_disable_parent(struct irq_data *data)
1112 {
1113         data = data->parent_data;
1114         if (data->chip->irq_disable)
1115                 data->chip->irq_disable(data);
1116         else
1117                 data->chip->irq_mask(data);
1118 }
1119
1120 /**
1121  * irq_chip_ack_parent - Acknowledge the parent interrupt
1122  * @data:       Pointer to interrupt specific data
1123  */
1124 void irq_chip_ack_parent(struct irq_data *data)
1125 {
1126         data = data->parent_data;
1127         data->chip->irq_ack(data);
1128 }
1129 EXPORT_SYMBOL_GPL(irq_chip_ack_parent);
1130
1131 /**
1132  * irq_chip_mask_parent - Mask the parent interrupt
1133  * @data:       Pointer to interrupt specific data
1134  */
1135 void irq_chip_mask_parent(struct irq_data *data)
1136 {
1137         data = data->parent_data;
1138         data->chip->irq_mask(data);
1139 }
1140 EXPORT_SYMBOL_GPL(irq_chip_mask_parent);
1141
1142 /**
1143  * irq_chip_unmask_parent - Unmask the parent interrupt
1144  * @data:       Pointer to interrupt specific data
1145  */
1146 void irq_chip_unmask_parent(struct irq_data *data)
1147 {
1148         data = data->parent_data;
1149         data->chip->irq_unmask(data);
1150 }
1151 EXPORT_SYMBOL_GPL(irq_chip_unmask_parent);
1152
1153 /**
1154  * irq_chip_eoi_parent - Invoke EOI on the parent interrupt
1155  * @data:       Pointer to interrupt specific data
1156  */
1157 void irq_chip_eoi_parent(struct irq_data *data)
1158 {
1159         data = data->parent_data;
1160         data->chip->irq_eoi(data);
1161 }
1162 EXPORT_SYMBOL_GPL(irq_chip_eoi_parent);
1163
1164 /**
1165  * irq_chip_set_affinity_parent - Set affinity on the parent interrupt
1166  * @data:       Pointer to interrupt specific data
1167  * @dest:       The affinity mask to set
1168  * @force:      Flag to enforce setting (disable online checks)
1169  *
1170  * Conditinal, as the underlying parent chip might not implement it.
1171  */
1172 int irq_chip_set_affinity_parent(struct irq_data *data,
1173                                  const struct cpumask *dest, bool force)
1174 {
1175         data = data->parent_data;
1176         if (data->chip->irq_set_affinity)
1177                 return data->chip->irq_set_affinity(data, dest, force);
1178
1179         return -ENOSYS;
1180 }
1181
1182 /**
1183  * irq_chip_set_type_parent - Set IRQ type on the parent interrupt
1184  * @data:       Pointer to interrupt specific data
1185  * @type:       IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
1186  *
1187  * Conditional, as the underlying parent chip might not implement it.
1188  */
1189 int irq_chip_set_type_parent(struct irq_data *data, unsigned int type)
1190 {
1191         data = data->parent_data;
1192
1193         if (data->chip->irq_set_type)
1194                 return data->chip->irq_set_type(data, type);
1195
1196         return -ENOSYS;
1197 }
1198 EXPORT_SYMBOL_GPL(irq_chip_set_type_parent);
1199
1200 /**
1201  * irq_chip_retrigger_hierarchy - Retrigger an interrupt in hardware
1202  * @data:       Pointer to interrupt specific data
1203  *
1204  * Iterate through the domain hierarchy of the interrupt and check
1205  * whether a hw retrigger function exists. If yes, invoke it.
1206  */
1207 int irq_chip_retrigger_hierarchy(struct irq_data *data)
1208 {
1209         for (data = data->parent_data; data; data = data->parent_data)
1210                 if (data->chip && data->chip->irq_retrigger)
1211                         return data->chip->irq_retrigger(data);
1212
1213         return 0;
1214 }
1215
1216 /**
1217  * irq_chip_set_vcpu_affinity_parent - Set vcpu affinity on the parent interrupt
1218  * @data:       Pointer to interrupt specific data
1219  * @vcpu_info:  The vcpu affinity information
1220  */
1221 int irq_chip_set_vcpu_affinity_parent(struct irq_data *data, void *vcpu_info)
1222 {
1223         data = data->parent_data;
1224         if (data->chip->irq_set_vcpu_affinity)
1225                 return data->chip->irq_set_vcpu_affinity(data, vcpu_info);
1226
1227         return -ENOSYS;
1228 }
1229
1230 /**
1231  * irq_chip_set_wake_parent - Set/reset wake-up on the parent interrupt
1232  * @data:       Pointer to interrupt specific data
1233  * @on:         Whether to set or reset the wake-up capability of this irq
1234  *
1235  * Conditional, as the underlying parent chip might not implement it.
1236  */
1237 int irq_chip_set_wake_parent(struct irq_data *data, unsigned int on)
1238 {
1239         data = data->parent_data;
1240         if (data->chip->irq_set_wake)
1241                 return data->chip->irq_set_wake(data, on);
1242
1243         return -ENOSYS;
1244 }
1245 #endif
1246
1247 /**
1248  * irq_chip_compose_msi_msg - Componse msi message for a irq chip
1249  * @data:       Pointer to interrupt specific data
1250  * @msg:        Pointer to the MSI message
1251  *
1252  * For hierarchical domains we find the first chip in the hierarchy
1253  * which implements the irq_compose_msi_msg callback. For non
1254  * hierarchical we use the top level chip.
1255  */
1256 int irq_chip_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1257 {
1258         struct irq_data *pos = NULL;
1259
1260 #ifdef  CONFIG_IRQ_DOMAIN_HIERARCHY
1261         for (; data; data = data->parent_data)
1262 #endif
1263                 if (data->chip && data->chip->irq_compose_msi_msg)
1264                         pos = data;
1265         if (!pos)
1266                 return -ENOSYS;
1267
1268         pos->chip->irq_compose_msi_msg(pos, msg);
1269
1270         return 0;
1271 }
1272
1273 /**
1274  * irq_chip_pm_get - Enable power for an IRQ chip
1275  * @data:       Pointer to interrupt specific data
1276  *
1277  * Enable the power to the IRQ chip referenced by the interrupt data
1278  * structure.
1279  */
1280 int irq_chip_pm_get(struct irq_data *data)
1281 {
1282         int retval;
1283
1284         if (IS_ENABLED(CONFIG_PM) && data->chip->parent_device) {
1285                 retval = pm_runtime_get_sync(data->chip->parent_device);
1286                 if (retval < 0) {
1287                         pm_runtime_put_noidle(data->chip->parent_device);
1288                         return retval;
1289                 }
1290         }
1291
1292         return 0;
1293 }
1294
1295 /**
1296  * irq_chip_pm_put - Disable power for an IRQ chip
1297  * @data:       Pointer to interrupt specific data
1298  *
1299  * Disable the power to the IRQ chip referenced by the interrupt data
1300  * structure, belongs. Note that power will only be disabled, once this
1301  * function has been called for all IRQs that have called irq_chip_pm_get().
1302  */
1303 int irq_chip_pm_put(struct irq_data *data)
1304 {
1305         int retval = 0;
1306
1307         if (IS_ENABLED(CONFIG_PM) && data->chip->parent_device)
1308                 retval = pm_runtime_put(data->chip->parent_device);
1309
1310         return (retval < 0) ? retval : 0;
1311 }