Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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