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