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