2 * linux/kernel/irq/chip.c
4 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
5 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
7 * This file contains the core interrupt handling code, for irq-chip
10 * Detailed information is available in Documentation/DocBook/genericirq
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>
19 #include "internals.h"
22 * dynamic_irq_init - initialize a dynamically allocated irq
23 * @irq: irq number to initialize
25 void dynamic_irq_init(unsigned int irq)
27 struct irq_desc *desc;
31 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
35 /* Ensure we don't have left over values from a previous use of this irq */
36 desc = irq_to_desc(irq);
37 spin_lock_irqsave(&desc->lock, flags);
38 desc->status = IRQ_DISABLED;
39 desc->chip = &no_irq_chip;
40 desc->handle_irq = handle_bad_irq;
42 desc->msi_desc = NULL;
43 desc->handler_data = NULL;
44 desc->chip_data = NULL;
47 desc->irqs_unhandled = 0;
49 cpus_setall(desc->affinity);
51 spin_unlock_irqrestore(&desc->lock, flags);
55 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
56 * @irq: irq number to initialize
58 void dynamic_irq_cleanup(unsigned int irq)
60 struct irq_desc *desc;
64 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
68 desc = irq_to_desc(irq);
69 spin_lock_irqsave(&desc->lock, flags);
71 spin_unlock_irqrestore(&desc->lock, flags);
72 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
76 desc->msi_desc = NULL;
77 desc->handler_data = NULL;
78 desc->chip_data = NULL;
79 desc->handle_irq = handle_bad_irq;
80 desc->chip = &no_irq_chip;
81 spin_unlock_irqrestore(&desc->lock, flags);
86 * set_irq_chip - set the irq chip for an irq
88 * @chip: pointer to irq chip description structure
90 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
92 struct irq_desc *desc;
96 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
103 desc = irq_to_desc(irq);
104 spin_lock_irqsave(&desc->lock, flags);
105 irq_chip_set_defaults(chip);
107 spin_unlock_irqrestore(&desc->lock, flags);
111 EXPORT_SYMBOL(set_irq_chip);
114 * set_irq_type - set the irq trigger type for an irq
116 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
118 int set_irq_type(unsigned int irq, unsigned int type)
120 struct irq_desc *desc;
124 if (irq >= nr_irqs) {
125 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
129 desc = irq_to_desc(irq);
130 if (type == IRQ_TYPE_NONE)
133 spin_lock_irqsave(&desc->lock, flags);
134 ret = __irq_set_trigger(desc, irq, flags);
135 spin_unlock_irqrestore(&desc->lock, flags);
138 EXPORT_SYMBOL(set_irq_type);
141 * set_irq_data - set irq type data for an irq
142 * @irq: Interrupt number
143 * @data: Pointer to interrupt specific data
145 * Set the hardware irq controller data for an irq
147 int set_irq_data(unsigned int irq, void *data)
149 struct irq_desc *desc;
152 if (irq >= nr_irqs) {
154 "Trying to install controller data for IRQ%d\n", irq);
158 desc = irq_to_desc(irq);
159 spin_lock_irqsave(&desc->lock, flags);
160 desc->handler_data = data;
161 spin_unlock_irqrestore(&desc->lock, flags);
164 EXPORT_SYMBOL(set_irq_data);
167 * set_irq_data - set irq type data for an irq
168 * @irq: Interrupt number
169 * @entry: Pointer to MSI descriptor data
171 * Set the hardware irq controller data for an irq
173 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
175 struct irq_desc *desc;
178 if (irq >= nr_irqs) {
180 "Trying to install msi data for IRQ%d\n", irq);
183 desc = irq_to_desc(irq);
184 spin_lock_irqsave(&desc->lock, flags);
185 desc->msi_desc = entry;
188 spin_unlock_irqrestore(&desc->lock, flags);
193 * set_irq_chip_data - set irq chip data for an irq
194 * @irq: Interrupt number
195 * @data: Pointer to chip specific data
197 * Set the hardware irq chip data for an irq
199 int set_irq_chip_data(unsigned int irq, void *data)
201 struct irq_desc *desc;
204 desc = irq_to_desc(irq);
205 if (irq >= nr_irqs || !desc->chip) {
206 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
210 spin_lock_irqsave(&desc->lock, flags);
211 desc->chip_data = data;
212 spin_unlock_irqrestore(&desc->lock, flags);
216 EXPORT_SYMBOL(set_irq_chip_data);
219 * default enable function
221 static void default_enable(unsigned int irq)
223 struct irq_desc *desc;
225 desc = irq_to_desc(irq);
226 desc->chip->unmask(irq);
227 desc->status &= ~IRQ_MASKED;
231 * default disable function
233 static void default_disable(unsigned int irq)
238 * default startup function
240 static unsigned int default_startup(unsigned int irq)
242 struct irq_desc *desc;
244 desc = irq_to_desc(irq);
245 desc->chip->enable(irq);
251 * default shutdown function
253 static void default_shutdown(unsigned int irq)
255 struct irq_desc *desc;
257 desc = irq_to_desc(irq);
258 desc->chip->mask(irq);
259 desc->status |= IRQ_MASKED;
263 * Fixup enable/disable function pointers
265 void irq_chip_set_defaults(struct irq_chip *chip)
268 chip->enable = default_enable;
270 chip->disable = default_disable;
272 chip->startup = default_startup;
274 * We use chip->disable, when the user provided its own. When
275 * we have default_disable set for chip->disable, then we need
276 * to use default_shutdown, otherwise the irq line is not
277 * disabled on free_irq():
280 chip->shutdown = chip->disable != default_disable ?
281 chip->disable : default_shutdown;
283 chip->name = chip->typename;
285 chip->end = dummy_irq_chip.end;
288 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
290 if (desc->chip->mask_ack)
291 desc->chip->mask_ack(irq);
293 desc->chip->mask(irq);
294 desc->chip->ack(irq);
299 * handle_simple_irq - Simple and software-decoded IRQs.
300 * @irq: the interrupt number
301 * @desc: the interrupt description structure for this irq
303 * Simple interrupts are either sent from a demultiplexing interrupt
304 * handler or come from hardware, where no interrupt hardware control
307 * Note: The caller is expected to handle the ack, clear, mask and
308 * unmask issues if necessary.
311 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
313 struct irqaction *action;
314 irqreturn_t action_ret;
316 spin_lock(&desc->lock);
318 if (unlikely(desc->status & IRQ_INPROGRESS))
320 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
321 kstat_irqs_this_cpu(desc)++;
323 action = desc->action;
324 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
327 desc->status |= IRQ_INPROGRESS;
328 spin_unlock(&desc->lock);
330 action_ret = handle_IRQ_event(irq, action);
332 note_interrupt(irq, desc, action_ret);
334 spin_lock(&desc->lock);
335 desc->status &= ~IRQ_INPROGRESS;
337 spin_unlock(&desc->lock);
341 * handle_level_irq - Level type irq handler
342 * @irq: the interrupt number
343 * @desc: the interrupt description structure for this irq
345 * Level type interrupts are active as long as the hardware line has
346 * the active level. This may require to mask the interrupt and unmask
347 * it after the associated handler has acknowledged the device, so the
348 * interrupt line is back to inactive.
351 handle_level_irq(unsigned int irq, struct irq_desc *desc)
353 struct irqaction *action;
354 irqreturn_t action_ret;
356 spin_lock(&desc->lock);
357 mask_ack_irq(desc, irq);
359 if (unlikely(desc->status & IRQ_INPROGRESS))
361 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
362 kstat_irqs_this_cpu(desc)++;
365 * If its disabled or no action available
366 * keep it masked and get out of here
368 action = desc->action;
369 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
372 desc->status |= IRQ_INPROGRESS;
373 spin_unlock(&desc->lock);
375 action_ret = handle_IRQ_event(irq, action);
377 note_interrupt(irq, desc, action_ret);
379 spin_lock(&desc->lock);
380 desc->status &= ~IRQ_INPROGRESS;
381 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
382 desc->chip->unmask(irq);
384 spin_unlock(&desc->lock);
388 * handle_fasteoi_irq - irq handler for transparent controllers
389 * @irq: the interrupt number
390 * @desc: the interrupt description structure for this irq
392 * Only a single callback will be issued to the chip: an ->eoi()
393 * call when the interrupt has been serviced. This enables support
394 * for modern forms of interrupt handlers, which handle the flow
395 * details in hardware, transparently.
398 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
400 struct irqaction *action;
401 irqreturn_t action_ret;
403 spin_lock(&desc->lock);
405 if (unlikely(desc->status & IRQ_INPROGRESS))
408 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
409 kstat_irqs_this_cpu(desc)++;
412 * If its disabled or no action available
413 * then mask it and get out of here:
415 action = desc->action;
416 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
417 desc->status |= IRQ_PENDING;
418 if (desc->chip->mask)
419 desc->chip->mask(irq);
423 desc->status |= IRQ_INPROGRESS;
424 desc->status &= ~IRQ_PENDING;
425 spin_unlock(&desc->lock);
427 action_ret = handle_IRQ_event(irq, action);
429 note_interrupt(irq, desc, action_ret);
431 spin_lock(&desc->lock);
432 desc->status &= ~IRQ_INPROGRESS;
434 desc->chip->eoi(irq);
436 spin_unlock(&desc->lock);
440 * handle_edge_irq - edge type IRQ handler
441 * @irq: the interrupt number
442 * @desc: the interrupt description structure for this irq
444 * Interrupt occures on the falling and/or rising edge of a hardware
445 * signal. The occurence is latched into the irq controller hardware
446 * and must be acked in order to be reenabled. After the ack another
447 * interrupt can happen on the same source even before the first one
448 * is handled by the assosiacted event handler. If this happens it
449 * might be necessary to disable (mask) the interrupt depending on the
450 * controller hardware. This requires to reenable the interrupt inside
451 * of the loop which handles the interrupts which have arrived while
452 * the handler was running. If all pending interrupts are handled, the
456 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
458 spin_lock(&desc->lock);
460 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
463 * If we're currently running this IRQ, or its disabled,
464 * we shouldn't process the IRQ. Mark it pending, handle
465 * the necessary masking and go out
467 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
469 desc->status |= (IRQ_PENDING | IRQ_MASKED);
470 mask_ack_irq(desc, irq);
474 kstat_irqs_this_cpu(desc)++;
476 /* Start handling the irq */
477 desc->chip->ack(irq);
479 /* Mark the IRQ currently in progress.*/
480 desc->status |= IRQ_INPROGRESS;
483 struct irqaction *action = desc->action;
484 irqreturn_t action_ret;
486 if (unlikely(!action)) {
487 desc->chip->mask(irq);
492 * When another irq arrived while we were handling
493 * one, we could have masked the irq.
494 * Renable it, if it was not disabled in meantime.
496 if (unlikely((desc->status &
497 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
498 (IRQ_PENDING | IRQ_MASKED))) {
499 desc->chip->unmask(irq);
500 desc->status &= ~IRQ_MASKED;
503 desc->status &= ~IRQ_PENDING;
504 spin_unlock(&desc->lock);
505 action_ret = handle_IRQ_event(irq, action);
507 note_interrupt(irq, desc, action_ret);
508 spin_lock(&desc->lock);
510 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
512 desc->status &= ~IRQ_INPROGRESS;
514 spin_unlock(&desc->lock);
518 * handle_percpu_IRQ - Per CPU local irq handler
519 * @irq: the interrupt number
520 * @desc: the interrupt description structure for this irq
522 * Per CPU interrupts on SMP machines without locking requirements
525 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
527 irqreturn_t action_ret;
529 kstat_irqs_this_cpu(desc)++;
532 desc->chip->ack(irq);
534 action_ret = handle_IRQ_event(irq, desc->action);
536 note_interrupt(irq, desc, action_ret);
539 desc->chip->eoi(irq);
543 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
546 struct irq_desc *desc;
549 if (irq >= nr_irqs) {
551 "Trying to install type control for IRQ%d\n", irq);
555 desc = irq_to_desc(irq);
558 handle = handle_bad_irq;
559 else if (desc->chip == &no_irq_chip) {
560 printk(KERN_WARNING "Trying to install %sinterrupt handler "
561 "for IRQ%d\n", is_chained ? "chained " : "", irq);
563 * Some ARM implementations install a handler for really dumb
564 * interrupt hardware without setting an irq_chip. This worked
565 * with the ARM no_irq_chip but the check in setup_irq would
566 * prevent us to setup the interrupt at all. Switch it to
567 * dummy_irq_chip for easy transition.
569 desc->chip = &dummy_irq_chip;
572 spin_lock_irqsave(&desc->lock, flags);
575 if (handle == handle_bad_irq) {
576 if (desc->chip != &no_irq_chip)
577 mask_ack_irq(desc, irq);
578 desc->status |= IRQ_DISABLED;
581 desc->handle_irq = handle;
584 if (handle != handle_bad_irq && is_chained) {
585 desc->status &= ~IRQ_DISABLED;
586 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
588 desc->chip->startup(irq);
590 spin_unlock_irqrestore(&desc->lock, flags);
594 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
595 irq_flow_handler_t handle)
597 set_irq_chip(irq, chip);
598 __set_irq_handler(irq, handle, 0, NULL);
602 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
603 irq_flow_handler_t handle, const char *name)
605 set_irq_chip(irq, chip);
606 __set_irq_handler(irq, handle, 0, name);
609 void __init set_irq_noprobe(unsigned int irq)
611 struct irq_desc *desc;
614 if (irq >= nr_irqs) {
615 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
620 desc = irq_to_desc(irq);
622 spin_lock_irqsave(&desc->lock, flags);
623 desc->status |= IRQ_NOPROBE;
624 spin_unlock_irqrestore(&desc->lock, flags);
627 void __init set_irq_probe(unsigned int irq)
629 struct irq_desc *desc;
632 if (irq >= nr_irqs) {
633 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
638 desc = irq_to_desc(irq);
640 spin_lock_irqsave(&desc->lock, flags);
641 desc->status &= ~IRQ_NOPROBE;
642 spin_unlock_irqrestore(&desc->lock, flags);