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;
30 /* first time to use this irq_desc */
31 desc = irq_to_desc_alloc(irq);
33 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
37 /* Ensure we don't have left over values from a previous use of this 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;
43 desc->msi_desc = NULL;
44 desc->handler_data = NULL;
45 desc->chip_data = NULL;
48 desc->irqs_unhandled = 0;
50 cpus_setall(desc->affinity);
52 spin_unlock_irqrestore(&desc->lock, flags);
56 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
57 * @irq: irq number to initialize
59 void dynamic_irq_cleanup(unsigned int irq)
61 struct irq_desc *desc;
64 desc = irq_to_desc(irq);
66 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
70 spin_lock_irqsave(&desc->lock, flags);
72 spin_unlock_irqrestore(&desc->lock, flags);
73 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
77 desc->msi_desc = NULL;
78 desc->handler_data = NULL;
79 desc->chip_data = NULL;
80 desc->handle_irq = handle_bad_irq;
81 desc->chip = &no_irq_chip;
82 spin_unlock_irqrestore(&desc->lock, flags);
87 * set_irq_chip - set the irq chip for an irq
89 * @chip: pointer to irq chip description structure
91 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
93 struct irq_desc *desc;
96 desc = irq_to_desc(irq);
98 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
105 spin_lock_irqsave(&desc->lock, flags);
106 irq_chip_set_defaults(chip);
108 spin_unlock_irqrestore(&desc->lock, flags);
112 EXPORT_SYMBOL(set_irq_chip);
115 * set_irq_type - set the irq trigger type for an irq
117 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
119 int set_irq_type(unsigned int irq, unsigned int type)
121 struct irq_desc *desc;
125 desc = irq_to_desc(irq);
127 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
131 if (type == IRQ_TYPE_NONE)
134 spin_lock_irqsave(&desc->lock, flags);
135 ret = __irq_set_trigger(desc, irq, flags);
136 spin_unlock_irqrestore(&desc->lock, flags);
139 EXPORT_SYMBOL(set_irq_type);
142 * set_irq_data - set irq type data for an irq
143 * @irq: Interrupt number
144 * @data: Pointer to interrupt specific data
146 * Set the hardware irq controller data for an irq
148 int set_irq_data(unsigned int irq, void *data)
150 struct irq_desc *desc;
153 desc = irq_to_desc(irq);
156 "Trying to install controller data for IRQ%d\n", irq);
160 spin_lock_irqsave(&desc->lock, flags);
161 desc->handler_data = data;
162 spin_unlock_irqrestore(&desc->lock, flags);
165 EXPORT_SYMBOL(set_irq_data);
168 * set_irq_data - set irq type data for an irq
169 * @irq: Interrupt number
170 * @entry: Pointer to MSI descriptor data
172 * Set the hardware irq controller data for an irq
174 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
176 struct irq_desc *desc;
179 desc = irq_to_desc(irq);
182 "Trying to install msi data for IRQ%d\n", irq);
186 spin_lock_irqsave(&desc->lock, flags);
187 desc->msi_desc = entry;
190 spin_unlock_irqrestore(&desc->lock, flags);
195 * set_irq_chip_data - set irq chip data for an irq
196 * @irq: Interrupt number
197 * @data: Pointer to chip specific data
199 * Set the hardware irq chip data for an irq
201 int set_irq_chip_data(unsigned int irq, void *data)
203 struct irq_desc *desc;
206 desc = irq_to_desc(irq);
209 "Trying to install chip data for IRQ%d\n", irq);
214 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
218 spin_lock_irqsave(&desc->lock, flags);
219 desc->chip_data = data;
220 spin_unlock_irqrestore(&desc->lock, flags);
224 EXPORT_SYMBOL(set_irq_chip_data);
227 * default enable function
229 static void default_enable(unsigned int irq)
231 struct irq_desc *desc;
233 desc = irq_to_desc(irq);
234 desc->chip->unmask(irq);
235 desc->status &= ~IRQ_MASKED;
239 * default disable function
241 static void default_disable(unsigned int irq)
246 * default startup function
248 static unsigned int default_startup(unsigned int irq)
250 struct irq_desc *desc;
252 desc = irq_to_desc(irq);
253 desc->chip->enable(irq);
259 * default shutdown function
261 static void default_shutdown(unsigned int irq)
263 struct irq_desc *desc;
265 desc = irq_to_desc(irq);
266 desc->chip->mask(irq);
267 desc->status |= IRQ_MASKED;
271 * Fixup enable/disable function pointers
273 void irq_chip_set_defaults(struct irq_chip *chip)
276 chip->enable = default_enable;
278 chip->disable = default_disable;
280 chip->startup = default_startup;
282 * We use chip->disable, when the user provided its own. When
283 * we have default_disable set for chip->disable, then we need
284 * to use default_shutdown, otherwise the irq line is not
285 * disabled on free_irq():
288 chip->shutdown = chip->disable != default_disable ?
289 chip->disable : default_shutdown;
291 chip->name = chip->typename;
293 chip->end = dummy_irq_chip.end;
296 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
298 if (desc->chip->mask_ack)
299 desc->chip->mask_ack(irq);
301 desc->chip->mask(irq);
302 desc->chip->ack(irq);
307 * handle_simple_irq - Simple and software-decoded IRQs.
308 * @irq: the interrupt number
309 * @desc: the interrupt description structure for this irq
311 * Simple interrupts are either sent from a demultiplexing interrupt
312 * handler or come from hardware, where no interrupt hardware control
315 * Note: The caller is expected to handle the ack, clear, mask and
316 * unmask issues if necessary.
319 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
321 struct irqaction *action;
322 irqreturn_t action_ret;
324 spin_lock(&desc->lock);
326 if (unlikely(desc->status & IRQ_INPROGRESS))
328 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
329 #ifdef CONFIG_HAVE_DYN_ARRAY
330 kstat_irqs_this_cpu(desc)++;
332 kstat_irqs_this_cpu(irq)++;
335 action = desc->action;
336 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
339 desc->status |= IRQ_INPROGRESS;
340 spin_unlock(&desc->lock);
342 action_ret = handle_IRQ_event(irq, action);
344 note_interrupt(irq, desc, action_ret);
346 spin_lock(&desc->lock);
347 desc->status &= ~IRQ_INPROGRESS;
349 spin_unlock(&desc->lock);
353 * handle_level_irq - Level type irq handler
354 * @irq: the interrupt number
355 * @desc: the interrupt description structure for this irq
357 * Level type interrupts are active as long as the hardware line has
358 * the active level. This may require to mask the interrupt and unmask
359 * it after the associated handler has acknowledged the device, so the
360 * interrupt line is back to inactive.
363 handle_level_irq(unsigned int irq, struct irq_desc *desc)
365 struct irqaction *action;
366 irqreturn_t action_ret;
368 spin_lock(&desc->lock);
369 mask_ack_irq(desc, irq);
371 if (unlikely(desc->status & IRQ_INPROGRESS))
373 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
374 #ifdef CONFIG_HAVE_DYN_ARRAY
375 kstat_irqs_this_cpu(desc)++;
377 kstat_irqs_this_cpu(irq)++;
381 * If its disabled or no action available
382 * keep it masked and get out of here
384 action = desc->action;
385 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
388 desc->status |= IRQ_INPROGRESS;
389 spin_unlock(&desc->lock);
391 action_ret = handle_IRQ_event(irq, action);
393 note_interrupt(irq, desc, action_ret);
395 spin_lock(&desc->lock);
396 desc->status &= ~IRQ_INPROGRESS;
397 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
398 desc->chip->unmask(irq);
400 spin_unlock(&desc->lock);
404 * handle_fasteoi_irq - irq handler for transparent controllers
405 * @irq: the interrupt number
406 * @desc: the interrupt description structure for this irq
408 * Only a single callback will be issued to the chip: an ->eoi()
409 * call when the interrupt has been serviced. This enables support
410 * for modern forms of interrupt handlers, which handle the flow
411 * details in hardware, transparently.
414 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
416 struct irqaction *action;
417 irqreturn_t action_ret;
419 spin_lock(&desc->lock);
421 if (unlikely(desc->status & IRQ_INPROGRESS))
424 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
425 #ifdef CONFIG_HAVE_DYN_ARRAY
426 kstat_irqs_this_cpu(desc)++;
428 kstat_irqs_this_cpu(irq)++;
432 * If its disabled or no action available
433 * then mask it and get out of here:
435 action = desc->action;
436 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
437 desc->status |= IRQ_PENDING;
438 if (desc->chip->mask)
439 desc->chip->mask(irq);
443 desc->status |= IRQ_INPROGRESS;
444 desc->status &= ~IRQ_PENDING;
445 spin_unlock(&desc->lock);
447 action_ret = handle_IRQ_event(irq, action);
449 note_interrupt(irq, desc, action_ret);
451 spin_lock(&desc->lock);
452 desc->status &= ~IRQ_INPROGRESS;
454 desc->chip->eoi(irq);
456 spin_unlock(&desc->lock);
460 * handle_edge_irq - edge type IRQ handler
461 * @irq: the interrupt number
462 * @desc: the interrupt description structure for this irq
464 * Interrupt occures on the falling and/or rising edge of a hardware
465 * signal. The occurence is latched into the irq controller hardware
466 * and must be acked in order to be reenabled. After the ack another
467 * interrupt can happen on the same source even before the first one
468 * is handled by the assosiacted event handler. If this happens it
469 * might be necessary to disable (mask) the interrupt depending on the
470 * controller hardware. This requires to reenable the interrupt inside
471 * of the loop which handles the interrupts which have arrived while
472 * the handler was running. If all pending interrupts are handled, the
476 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
478 spin_lock(&desc->lock);
480 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
483 * If we're currently running this IRQ, or its disabled,
484 * we shouldn't process the IRQ. Mark it pending, handle
485 * the necessary masking and go out
487 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
489 desc->status |= (IRQ_PENDING | IRQ_MASKED);
490 mask_ack_irq(desc, irq);
493 #ifdef CONFIG_HAVE_DYN_ARRAY
494 kstat_irqs_this_cpu(desc)++;
496 kstat_irqs_this_cpu(irq)++;
499 /* Start handling the irq */
500 desc->chip->ack(irq);
502 /* Mark the IRQ currently in progress.*/
503 desc->status |= IRQ_INPROGRESS;
506 struct irqaction *action = desc->action;
507 irqreturn_t action_ret;
509 if (unlikely(!action)) {
510 desc->chip->mask(irq);
515 * When another irq arrived while we were handling
516 * one, we could have masked the irq.
517 * Renable it, if it was not disabled in meantime.
519 if (unlikely((desc->status &
520 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
521 (IRQ_PENDING | IRQ_MASKED))) {
522 desc->chip->unmask(irq);
523 desc->status &= ~IRQ_MASKED;
526 desc->status &= ~IRQ_PENDING;
527 spin_unlock(&desc->lock);
528 action_ret = handle_IRQ_event(irq, action);
530 note_interrupt(irq, desc, action_ret);
531 spin_lock(&desc->lock);
533 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
535 desc->status &= ~IRQ_INPROGRESS;
537 spin_unlock(&desc->lock);
541 * handle_percpu_IRQ - Per CPU local irq handler
542 * @irq: the interrupt number
543 * @desc: the interrupt description structure for this irq
545 * Per CPU interrupts on SMP machines without locking requirements
548 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
550 irqreturn_t action_ret;
552 #ifdef CONFIG_HAVE_DYN_ARRAY
553 kstat_irqs_this_cpu(desc)++;
555 kstat_irqs_this_cpu(irq)++;
559 desc->chip->ack(irq);
561 action_ret = handle_IRQ_event(irq, desc->action);
563 note_interrupt(irq, desc, action_ret);
566 desc->chip->eoi(irq);
570 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
573 struct irq_desc *desc;
576 desc = irq_to_desc(irq);
579 "Trying to install type control for IRQ%d\n", irq);
584 handle = handle_bad_irq;
585 else if (desc->chip == &no_irq_chip) {
586 printk(KERN_WARNING "Trying to install %sinterrupt handler "
587 "for IRQ%d\n", is_chained ? "chained " : "", irq);
589 * Some ARM implementations install a handler for really dumb
590 * interrupt hardware without setting an irq_chip. This worked
591 * with the ARM no_irq_chip but the check in setup_irq would
592 * prevent us to setup the interrupt at all. Switch it to
593 * dummy_irq_chip for easy transition.
595 desc->chip = &dummy_irq_chip;
598 spin_lock_irqsave(&desc->lock, flags);
601 if (handle == handle_bad_irq) {
602 if (desc->chip != &no_irq_chip)
603 mask_ack_irq(desc, irq);
604 desc->status |= IRQ_DISABLED;
607 desc->handle_irq = handle;
610 if (handle != handle_bad_irq && is_chained) {
611 desc->status &= ~IRQ_DISABLED;
612 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
614 desc->chip->startup(irq);
616 spin_unlock_irqrestore(&desc->lock, flags);
620 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
621 irq_flow_handler_t handle)
623 set_irq_chip(irq, chip);
624 __set_irq_handler(irq, handle, 0, NULL);
628 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
629 irq_flow_handler_t handle, const char *name)
631 set_irq_chip(irq, chip);
632 __set_irq_handler(irq, handle, 0, name);
635 void __init set_irq_noprobe(unsigned int irq)
637 struct irq_desc *desc;
640 desc = irq_to_desc(irq);
642 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
647 spin_lock_irqsave(&desc->lock, flags);
648 desc->status |= IRQ_NOPROBE;
649 spin_unlock_irqrestore(&desc->lock, flags);
652 void __init set_irq_probe(unsigned int irq)
654 struct irq_desc *desc;
657 desc = irq_to_desc(irq);
659 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
664 spin_lock_irqsave(&desc->lock, flags);
665 desc->status &= ~IRQ_NOPROBE;
666 spin_unlock_irqrestore(&desc->lock, flags);