genirq: Add irq disabled flag to irq_data state
[linux-flexiantxendom0-3.2.10.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
19 #include "internals.h"
20
21 /**
22  *      irq_set_chip - set the irq chip for an irq
23  *      @irq:   irq number
24  *      @chip:  pointer to irq chip description structure
25  */
26 int irq_set_chip(unsigned int irq, struct irq_chip *chip)
27 {
28         unsigned long flags;
29         struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
30
31         if (!desc)
32                 return -EINVAL;
33
34         if (!chip)
35                 chip = &no_irq_chip;
36
37         irq_chip_set_defaults(chip);
38         desc->irq_data.chip = chip;
39         irq_put_desc_unlock(desc, flags);
40         /*
41          * For !CONFIG_SPARSE_IRQ make the irq show up in
42          * allocated_irqs. For the CONFIG_SPARSE_IRQ case, it is
43          * already marked, and this call is harmless.
44          */
45         irq_reserve_irq(irq);
46         return 0;
47 }
48 EXPORT_SYMBOL(irq_set_chip);
49
50 /**
51  *      irq_set_type - set the irq trigger type for an irq
52  *      @irq:   irq number
53  *      @type:  IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
54  */
55 int irq_set_irq_type(unsigned int irq, unsigned int type)
56 {
57         unsigned long flags;
58         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags);
59         int ret = 0;
60
61         if (!desc)
62                 return -EINVAL;
63
64         type &= IRQ_TYPE_SENSE_MASK;
65         if (type != IRQ_TYPE_NONE)
66                 ret = __irq_set_trigger(desc, irq, type);
67         irq_put_desc_busunlock(desc, flags);
68         return ret;
69 }
70 EXPORT_SYMBOL(irq_set_irq_type);
71
72 /**
73  *      irq_set_handler_data - set irq handler data for an irq
74  *      @irq:   Interrupt number
75  *      @data:  Pointer to interrupt specific data
76  *
77  *      Set the hardware irq controller data for an irq
78  */
79 int irq_set_handler_data(unsigned int irq, void *data)
80 {
81         unsigned long flags;
82         struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
83
84         if (!desc)
85                 return -EINVAL;
86         desc->irq_data.handler_data = data;
87         irq_put_desc_unlock(desc, flags);
88         return 0;
89 }
90 EXPORT_SYMBOL(irq_set_handler_data);
91
92 /**
93  *      irq_set_msi_desc - set MSI descriptor data for an irq
94  *      @irq:   Interrupt number
95  *      @entry: Pointer to MSI descriptor data
96  *
97  *      Set the MSI descriptor entry for an irq
98  */
99 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
100 {
101         unsigned long flags;
102         struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
103
104         if (!desc)
105                 return -EINVAL;
106         desc->irq_data.msi_desc = entry;
107         if (entry)
108                 entry->irq = irq;
109         irq_put_desc_unlock(desc, flags);
110         return 0;
111 }
112
113 /**
114  *      irq_set_chip_data - set irq chip data for an irq
115  *      @irq:   Interrupt number
116  *      @data:  Pointer to chip specific data
117  *
118  *      Set the hardware irq chip data for an irq
119  */
120 int irq_set_chip_data(unsigned int irq, void *data)
121 {
122         unsigned long flags;
123         struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
124
125         if (!desc)
126                 return -EINVAL;
127         desc->irq_data.chip_data = data;
128         irq_put_desc_unlock(desc, flags);
129         return 0;
130 }
131 EXPORT_SYMBOL(irq_set_chip_data);
132
133 struct irq_data *irq_get_irq_data(unsigned int irq)
134 {
135         struct irq_desc *desc = irq_to_desc(irq);
136
137         return desc ? &desc->irq_data : NULL;
138 }
139 EXPORT_SYMBOL_GPL(irq_get_irq_data);
140
141 static void irq_state_clr_disabled(struct irq_desc *desc)
142 {
143         desc->istate &= ~IRQS_DISABLED;
144         irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
145         irq_compat_clr_disabled(desc);
146 }
147
148 static void irq_state_set_disabled(struct irq_desc *desc)
149 {
150         desc->istate |= IRQS_DISABLED;
151         irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
152         irq_compat_set_disabled(desc);
153 }
154
155 static void irq_state_clr_masked(struct irq_desc *desc)
156 {
157         desc->istate &= ~IRQS_MASKED;
158         irq_compat_clr_masked(desc);
159 }
160
161 static void irq_state_set_masked(struct irq_desc *desc)
162 {
163         desc->istate |= IRQS_MASKED;
164         irq_compat_set_masked(desc);
165 }
166
167 int irq_startup(struct irq_desc *desc)
168 {
169         irq_state_clr_disabled(desc);
170         desc->depth = 0;
171
172         if (desc->irq_data.chip->irq_startup) {
173                 int ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
174                 irq_state_clr_masked(desc);
175                 return ret;
176         }
177
178         irq_enable(desc);
179         return 0;
180 }
181
182 void irq_shutdown(struct irq_desc *desc)
183 {
184         irq_state_set_disabled(desc);
185         desc->depth = 1;
186         if (desc->irq_data.chip->irq_shutdown)
187                 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
188         if (desc->irq_data.chip->irq_disable)
189                 desc->irq_data.chip->irq_disable(&desc->irq_data);
190         else
191                 desc->irq_data.chip->irq_mask(&desc->irq_data);
192         irq_state_set_masked(desc);
193 }
194
195 void irq_enable(struct irq_desc *desc)
196 {
197         irq_state_clr_disabled(desc);
198         if (desc->irq_data.chip->irq_enable)
199                 desc->irq_data.chip->irq_enable(&desc->irq_data);
200         else
201                 desc->irq_data.chip->irq_unmask(&desc->irq_data);
202         irq_state_clr_masked(desc);
203 }
204
205 void irq_disable(struct irq_desc *desc)
206 {
207         irq_state_set_disabled(desc);
208         if (desc->irq_data.chip->irq_disable) {
209                 desc->irq_data.chip->irq_disable(&desc->irq_data);
210                 irq_state_set_masked(desc);
211         }
212 }
213
214 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
215 /* Temporary migration helpers */
216 static void compat_irq_mask(struct irq_data *data)
217 {
218         data->chip->mask(data->irq);
219 }
220
221 static void compat_irq_unmask(struct irq_data *data)
222 {
223         data->chip->unmask(data->irq);
224 }
225
226 static void compat_irq_ack(struct irq_data *data)
227 {
228         data->chip->ack(data->irq);
229 }
230
231 static void compat_irq_mask_ack(struct irq_data *data)
232 {
233         data->chip->mask_ack(data->irq);
234 }
235
236 static void compat_irq_eoi(struct irq_data *data)
237 {
238         data->chip->eoi(data->irq);
239 }
240
241 static void compat_irq_enable(struct irq_data *data)
242 {
243         data->chip->enable(data->irq);
244 }
245
246 static void compat_irq_disable(struct irq_data *data)
247 {
248         data->chip->disable(data->irq);
249 }
250
251 static void compat_irq_shutdown(struct irq_data *data)
252 {
253         data->chip->shutdown(data->irq);
254 }
255
256 static unsigned int compat_irq_startup(struct irq_data *data)
257 {
258         return data->chip->startup(data->irq);
259 }
260
261 static int compat_irq_set_affinity(struct irq_data *data,
262                                    const struct cpumask *dest, bool force)
263 {
264         return data->chip->set_affinity(data->irq, dest);
265 }
266
267 static int compat_irq_set_type(struct irq_data *data, unsigned int type)
268 {
269         return data->chip->set_type(data->irq, type);
270 }
271
272 static int compat_irq_set_wake(struct irq_data *data, unsigned int on)
273 {
274         return data->chip->set_wake(data->irq, on);
275 }
276
277 static int compat_irq_retrigger(struct irq_data *data)
278 {
279         return data->chip->retrigger(data->irq);
280 }
281
282 static void compat_bus_lock(struct irq_data *data)
283 {
284         data->chip->bus_lock(data->irq);
285 }
286
287 static void compat_bus_sync_unlock(struct irq_data *data)
288 {
289         data->chip->bus_sync_unlock(data->irq);
290 }
291 #endif
292
293 /*
294  * Fixup enable/disable function pointers
295  */
296 void irq_chip_set_defaults(struct irq_chip *chip)
297 {
298 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
299         if (chip->enable)
300                 chip->irq_enable = compat_irq_enable;
301         if (chip->disable)
302                 chip->irq_disable = compat_irq_disable;
303         if (chip->shutdown)
304                 chip->irq_shutdown = compat_irq_shutdown;
305         if (chip->startup)
306                 chip->irq_startup = compat_irq_startup;
307         if (!chip->end)
308                 chip->end = dummy_irq_chip.end;
309         if (chip->bus_lock)
310                 chip->irq_bus_lock = compat_bus_lock;
311         if (chip->bus_sync_unlock)
312                 chip->irq_bus_sync_unlock = compat_bus_sync_unlock;
313         if (chip->mask)
314                 chip->irq_mask = compat_irq_mask;
315         if (chip->unmask)
316                 chip->irq_unmask = compat_irq_unmask;
317         if (chip->ack)
318                 chip->irq_ack = compat_irq_ack;
319         if (chip->mask_ack)
320                 chip->irq_mask_ack = compat_irq_mask_ack;
321         if (chip->eoi)
322                 chip->irq_eoi = compat_irq_eoi;
323         if (chip->set_affinity)
324                 chip->irq_set_affinity = compat_irq_set_affinity;
325         if (chip->set_type)
326                 chip->irq_set_type = compat_irq_set_type;
327         if (chip->set_wake)
328                 chip->irq_set_wake = compat_irq_set_wake;
329         if (chip->retrigger)
330                 chip->irq_retrigger = compat_irq_retrigger;
331 #endif
332 }
333
334 static inline void mask_ack_irq(struct irq_desc *desc)
335 {
336         if (desc->irq_data.chip->irq_mask_ack)
337                 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
338         else {
339                 desc->irq_data.chip->irq_mask(&desc->irq_data);
340                 if (desc->irq_data.chip->irq_ack)
341                         desc->irq_data.chip->irq_ack(&desc->irq_data);
342         }
343         irq_state_set_masked(desc);
344 }
345
346 void mask_irq(struct irq_desc *desc)
347 {
348         if (desc->irq_data.chip->irq_mask) {
349                 desc->irq_data.chip->irq_mask(&desc->irq_data);
350                 irq_state_set_masked(desc);
351         }
352 }
353
354 void unmask_irq(struct irq_desc *desc)
355 {
356         if (desc->irq_data.chip->irq_unmask) {
357                 desc->irq_data.chip->irq_unmask(&desc->irq_data);
358                 irq_state_clr_masked(desc);
359         }
360 }
361
362 /*
363  *      handle_nested_irq - Handle a nested irq from a irq thread
364  *      @irq:   the interrupt number
365  *
366  *      Handle interrupts which are nested into a threaded interrupt
367  *      handler. The handler function is called inside the calling
368  *      threads context.
369  */
370 void handle_nested_irq(unsigned int irq)
371 {
372         struct irq_desc *desc = irq_to_desc(irq);
373         struct irqaction *action;
374         irqreturn_t action_ret;
375
376         might_sleep();
377
378         raw_spin_lock_irq(&desc->lock);
379
380         kstat_incr_irqs_this_cpu(irq, desc);
381
382         action = desc->action;
383         if (unlikely(!action || (desc->istate & IRQS_DISABLED)))
384                 goto out_unlock;
385
386         irq_compat_set_progress(desc);
387         desc->istate |= IRQS_INPROGRESS;
388         raw_spin_unlock_irq(&desc->lock);
389
390         action_ret = action->thread_fn(action->irq, action->dev_id);
391         if (!noirqdebug)
392                 note_interrupt(irq, desc, action_ret);
393
394         raw_spin_lock_irq(&desc->lock);
395         desc->istate &= ~IRQS_INPROGRESS;
396         irq_compat_clr_progress(desc);
397
398 out_unlock:
399         raw_spin_unlock_irq(&desc->lock);
400 }
401 EXPORT_SYMBOL_GPL(handle_nested_irq);
402
403 static bool irq_check_poll(struct irq_desc *desc)
404 {
405         if (!(desc->istate & IRQS_POLL_INPROGRESS))
406                 return false;
407         return irq_wait_for_poll(desc);
408 }
409
410 /**
411  *      handle_simple_irq - Simple and software-decoded IRQs.
412  *      @irq:   the interrupt number
413  *      @desc:  the interrupt description structure for this irq
414  *
415  *      Simple interrupts are either sent from a demultiplexing interrupt
416  *      handler or come from hardware, where no interrupt hardware control
417  *      is necessary.
418  *
419  *      Note: The caller is expected to handle the ack, clear, mask and
420  *      unmask issues if necessary.
421  */
422 void
423 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
424 {
425         raw_spin_lock(&desc->lock);
426
427         if (unlikely(desc->istate & IRQS_INPROGRESS))
428                 if (!irq_check_poll(desc))
429                         goto out_unlock;
430
431         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
432         kstat_incr_irqs_this_cpu(irq, desc);
433
434         if (unlikely(!desc->action || (desc->istate & IRQS_DISABLED)))
435                 goto out_unlock;
436
437         handle_irq_event(desc);
438
439 out_unlock:
440         raw_spin_unlock(&desc->lock);
441 }
442
443 /**
444  *      handle_level_irq - Level type irq handler
445  *      @irq:   the interrupt number
446  *      @desc:  the interrupt description structure for this irq
447  *
448  *      Level type interrupts are active as long as the hardware line has
449  *      the active level. This may require to mask the interrupt and unmask
450  *      it after the associated handler has acknowledged the device, so the
451  *      interrupt line is back to inactive.
452  */
453 void
454 handle_level_irq(unsigned int irq, struct irq_desc *desc)
455 {
456         raw_spin_lock(&desc->lock);
457         mask_ack_irq(desc);
458
459         if (unlikely(desc->istate & IRQS_INPROGRESS))
460                 if (!irq_check_poll(desc))
461                         goto out_unlock;
462
463         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
464         kstat_incr_irqs_this_cpu(irq, desc);
465
466         /*
467          * If its disabled or no action available
468          * keep it masked and get out of here
469          */
470         if (unlikely(!desc->action || (desc->istate & IRQS_DISABLED)))
471                 goto out_unlock;
472
473         handle_irq_event(desc);
474
475         if (!(desc->istate & (IRQS_DISABLED | IRQS_ONESHOT)))
476                 unmask_irq(desc);
477 out_unlock:
478         raw_spin_unlock(&desc->lock);
479 }
480 EXPORT_SYMBOL_GPL(handle_level_irq);
481
482 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
483 static inline void preflow_handler(struct irq_desc *desc)
484 {
485         if (desc->preflow_handler)
486                 desc->preflow_handler(&desc->irq_data);
487 }
488 #else
489 static inline void preflow_handler(struct irq_desc *desc) { }
490 #endif
491
492 /**
493  *      handle_fasteoi_irq - irq handler for transparent controllers
494  *      @irq:   the interrupt number
495  *      @desc:  the interrupt description structure for this irq
496  *
497  *      Only a single callback will be issued to the chip: an ->eoi()
498  *      call when the interrupt has been serviced. This enables support
499  *      for modern forms of interrupt handlers, which handle the flow
500  *      details in hardware, transparently.
501  */
502 void
503 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
504 {
505         raw_spin_lock(&desc->lock);
506
507         if (unlikely(desc->istate & IRQS_INPROGRESS))
508                 if (!irq_check_poll(desc))
509                         goto out;
510
511         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
512         kstat_incr_irqs_this_cpu(irq, desc);
513
514         /*
515          * If its disabled or no action available
516          * then mask it and get out of here:
517          */
518         if (unlikely(!desc->action || (desc->istate & IRQS_DISABLED))) {
519                 irq_compat_set_pending(desc);
520                 desc->istate |= IRQS_PENDING;
521                 mask_irq(desc);
522                 goto out;
523         }
524
525         if (desc->istate & IRQS_ONESHOT)
526                 mask_irq(desc);
527
528         preflow_handler(desc);
529         handle_irq_event(desc);
530
531 out_eoi:
532         desc->irq_data.chip->irq_eoi(&desc->irq_data);
533 out_unlock:
534         raw_spin_unlock(&desc->lock);
535         return;
536 out:
537         if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
538                 goto out_eoi;
539         goto out_unlock;
540 }
541
542 /**
543  *      handle_edge_irq - edge type IRQ handler
544  *      @irq:   the interrupt number
545  *      @desc:  the interrupt description structure for this irq
546  *
547  *      Interrupt occures on the falling and/or rising edge of a hardware
548  *      signal. The occurence is latched into the irq controller hardware
549  *      and must be acked in order to be reenabled. After the ack another
550  *      interrupt can happen on the same source even before the first one
551  *      is handled by the associated event handler. If this happens it
552  *      might be necessary to disable (mask) the interrupt depending on the
553  *      controller hardware. This requires to reenable the interrupt inside
554  *      of the loop which handles the interrupts which have arrived while
555  *      the handler was running. If all pending interrupts are handled, the
556  *      loop is left.
557  */
558 void
559 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
560 {
561         raw_spin_lock(&desc->lock);
562
563         desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
564         /*
565          * If we're currently running this IRQ, or its disabled,
566          * we shouldn't process the IRQ. Mark it pending, handle
567          * the necessary masking and go out
568          */
569         if (unlikely((desc->istate & (IRQS_DISABLED | IRQS_INPROGRESS) ||
570                       !desc->action))) {
571                 if (!irq_check_poll(desc)) {
572                         irq_compat_set_pending(desc);
573                         desc->istate |= IRQS_PENDING;
574                         mask_ack_irq(desc);
575                         goto out_unlock;
576                 }
577         }
578         kstat_incr_irqs_this_cpu(irq, desc);
579
580         /* Start handling the irq */
581         desc->irq_data.chip->irq_ack(&desc->irq_data);
582
583         do {
584                 if (unlikely(!desc->action)) {
585                         mask_irq(desc);
586                         goto out_unlock;
587                 }
588
589                 /*
590                  * When another irq arrived while we were handling
591                  * one, we could have masked the irq.
592                  * Renable it, if it was not disabled in meantime.
593                  */
594                 if (unlikely(desc->istate & IRQS_PENDING)) {
595                         if (!(desc->istate & IRQS_DISABLED) &&
596                             (desc->istate & IRQS_MASKED))
597                                 unmask_irq(desc);
598                 }
599
600                 handle_irq_event(desc);
601
602         } while ((desc->istate & IRQS_PENDING) &&
603                  !(desc->istate & IRQS_DISABLED));
604
605 out_unlock:
606         raw_spin_unlock(&desc->lock);
607 }
608
609 /**
610  *      handle_percpu_irq - Per CPU local irq handler
611  *      @irq:   the interrupt number
612  *      @desc:  the interrupt description structure for this irq
613  *
614  *      Per CPU interrupts on SMP machines without locking requirements
615  */
616 void
617 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
618 {
619         struct irq_chip *chip = irq_desc_get_chip(desc);
620
621         kstat_incr_irqs_this_cpu(irq, desc);
622
623         if (chip->irq_ack)
624                 chip->irq_ack(&desc->irq_data);
625
626         handle_irq_event_percpu(desc, desc->action);
627
628         if (chip->irq_eoi)
629                 chip->irq_eoi(&desc->irq_data);
630 }
631
632 void
633 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
634                   const char *name)
635 {
636         unsigned long flags;
637         struct irq_desc *desc = irq_get_desc_buslock(irq, &flags);
638
639         if (!desc)
640                 return;
641
642         if (!handle) {
643                 handle = handle_bad_irq;
644         } else {
645                 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
646                         goto out;
647         }
648
649         /* Uninstall? */
650         if (handle == handle_bad_irq) {
651                 if (desc->irq_data.chip != &no_irq_chip)
652                         mask_ack_irq(desc);
653                 irq_state_set_disabled(desc);
654                 desc->depth = 1;
655         }
656         desc->handle_irq = handle;
657         desc->name = name;
658
659         if (handle != handle_bad_irq && is_chained) {
660                 irq_settings_set_noprobe(desc);
661                 irq_settings_set_norequest(desc);
662                 irq_startup(desc);
663         }
664 out:
665         irq_put_desc_busunlock(desc, flags);
666 }
667 EXPORT_SYMBOL_GPL(__irq_set_handler);
668
669 void
670 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
671                               irq_flow_handler_t handle, const char *name)
672 {
673         irq_set_chip(irq, chip);
674         __irq_set_handler(irq, handle, 0, name);
675 }
676
677 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
678 {
679         unsigned long flags;
680         struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
681
682         if (!desc)
683                 return;
684         irq_settings_clr_and_set(desc, clr, set);
685
686         irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
687                    IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
688         if (irq_settings_has_no_balance_set(desc))
689                 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
690         if (irq_settings_is_per_cpu(desc))
691                 irqd_set(&desc->irq_data, IRQD_PER_CPU);
692         if (irq_settings_can_move_pcntxt(desc))
693                 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
694
695         irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
696
697         irq_put_desc_unlock(desc, flags);
698 }