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 desc = irq_to_desc(irq);
32 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
36 /* Ensure we don't have left over values from a previous use of this 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 cpumask_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 = irq_to_desc(irq);
64 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
68 spin_lock_irqsave(&desc->lock, flags);
70 spin_unlock_irqrestore(&desc->lock, flags);
71 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
75 desc->msi_desc = NULL;
76 desc->handler_data = NULL;
77 desc->chip_data = NULL;
78 desc->handle_irq = handle_bad_irq;
79 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 = irq_to_desc(irq);
96 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
103 spin_lock_irqsave(&desc->lock, flags);
104 irq_chip_set_defaults(chip);
106 spin_unlock_irqrestore(&desc->lock, flags);
110 EXPORT_SYMBOL(set_irq_chip);
113 * set_irq_type - set the irq trigger type for an irq
115 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
117 int set_irq_type(unsigned int irq, unsigned int type)
119 struct irq_desc *desc = irq_to_desc(irq);
124 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
128 type &= IRQ_TYPE_SENSE_MASK;
129 if (type == IRQ_TYPE_NONE)
132 spin_lock_irqsave(&desc->lock, flags);
133 ret = __irq_set_trigger(desc, irq, type);
134 spin_unlock_irqrestore(&desc->lock, flags);
137 EXPORT_SYMBOL(set_irq_type);
140 * set_irq_data - set irq type data for an irq
141 * @irq: Interrupt number
142 * @data: Pointer to interrupt specific data
144 * Set the hardware irq controller data for an irq
146 int set_irq_data(unsigned int irq, void *data)
148 struct irq_desc *desc = irq_to_desc(irq);
153 "Trying to install controller data for IRQ%d\n", irq);
157 spin_lock_irqsave(&desc->lock, flags);
158 desc->handler_data = data;
159 spin_unlock_irqrestore(&desc->lock, flags);
162 EXPORT_SYMBOL(set_irq_data);
165 * set_irq_data - set irq type data for an irq
166 * @irq: Interrupt number
167 * @entry: Pointer to MSI descriptor data
169 * Set the hardware irq controller data for an irq
171 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
173 struct irq_desc *desc = irq_to_desc(irq);
178 "Trying to install msi data for IRQ%d\n", irq);
182 spin_lock_irqsave(&desc->lock, flags);
183 desc->msi_desc = entry;
186 spin_unlock_irqrestore(&desc->lock, flags);
191 * set_irq_chip_data - set irq chip data for an irq
192 * @irq: Interrupt number
193 * @data: Pointer to chip specific data
195 * Set the hardware irq chip data for an irq
197 int set_irq_chip_data(unsigned int irq, void *data)
199 struct irq_desc *desc = irq_to_desc(irq);
204 "Trying to install chip data for IRQ%d\n", irq);
209 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
213 spin_lock_irqsave(&desc->lock, flags);
214 desc->chip_data = data;
215 spin_unlock_irqrestore(&desc->lock, flags);
219 EXPORT_SYMBOL(set_irq_chip_data);
222 * default enable function
224 static void default_enable(unsigned int irq)
226 struct irq_desc *desc = irq_to_desc(irq);
228 desc->chip->unmask(irq);
229 desc->status &= ~IRQ_MASKED;
233 * default disable function
235 static void default_disable(unsigned int irq)
240 * default startup function
242 static unsigned int default_startup(unsigned int irq)
244 struct irq_desc *desc = irq_to_desc(irq);
246 desc->chip->enable(irq);
251 * default shutdown function
253 static void default_shutdown(unsigned int irq)
255 struct irq_desc *desc = irq_to_desc(irq);
257 desc->chip->mask(irq);
258 desc->status |= IRQ_MASKED;
262 * Fixup enable/disable function pointers
264 void irq_chip_set_defaults(struct irq_chip *chip)
267 chip->enable = default_enable;
269 chip->disable = default_disable;
271 chip->startup = default_startup;
273 * We use chip->disable, when the user provided its own. When
274 * we have default_disable set for chip->disable, then we need
275 * to use default_shutdown, otherwise the irq line is not
276 * disabled on free_irq():
279 chip->shutdown = chip->disable != default_disable ?
280 chip->disable : default_shutdown;
282 chip->name = chip->typename;
284 chip->end = dummy_irq_chip.end;
287 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
289 if (desc->chip->mask_ack)
290 desc->chip->mask_ack(irq);
292 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_incr_irqs_this_cpu(irq, 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);
358 desc = irq_remap_to_desc(irq, desc);
360 if (unlikely(desc->status & IRQ_INPROGRESS))
362 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
363 kstat_incr_irqs_this_cpu(irq, desc);
366 * If its disabled or no action available
367 * keep it masked and get out of here
369 action = desc->action;
370 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
373 desc->status |= IRQ_INPROGRESS;
374 spin_unlock(&desc->lock);
376 action_ret = handle_IRQ_event(irq, action);
378 note_interrupt(irq, desc, action_ret);
380 spin_lock(&desc->lock);
381 desc->status &= ~IRQ_INPROGRESS;
382 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
383 desc->chip->unmask(irq);
385 spin_unlock(&desc->lock);
389 * handle_fasteoi_irq - irq handler for transparent controllers
390 * @irq: the interrupt number
391 * @desc: the interrupt description structure for this irq
393 * Only a single callback will be issued to the chip: an ->eoi()
394 * call when the interrupt has been serviced. This enables support
395 * for modern forms of interrupt handlers, which handle the flow
396 * details in hardware, transparently.
399 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
401 struct irqaction *action;
402 irqreturn_t action_ret;
404 spin_lock(&desc->lock);
406 if (unlikely(desc->status & IRQ_INPROGRESS))
409 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
410 kstat_incr_irqs_this_cpu(irq, desc);
413 * If its disabled or no action available
414 * then mask it and get out of here:
416 action = desc->action;
417 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
418 desc->status |= IRQ_PENDING;
419 if (desc->chip->mask)
420 desc->chip->mask(irq);
424 desc->status |= IRQ_INPROGRESS;
425 desc->status &= ~IRQ_PENDING;
426 spin_unlock(&desc->lock);
428 action_ret = handle_IRQ_event(irq, action);
430 note_interrupt(irq, desc, action_ret);
432 spin_lock(&desc->lock);
433 desc->status &= ~IRQ_INPROGRESS;
435 desc->chip->eoi(irq);
436 desc = irq_remap_to_desc(irq, desc);
438 spin_unlock(&desc->lock);
442 * handle_edge_irq - edge type IRQ handler
443 * @irq: the interrupt number
444 * @desc: the interrupt description structure for this irq
446 * Interrupt occures on the falling and/or rising edge of a hardware
447 * signal. The occurence is latched into the irq controller hardware
448 * and must be acked in order to be reenabled. After the ack another
449 * interrupt can happen on the same source even before the first one
450 * is handled by the assosiacted event handler. If this happens it
451 * might be necessary to disable (mask) the interrupt depending on the
452 * controller hardware. This requires to reenable the interrupt inside
453 * of the loop which handles the interrupts which have arrived while
454 * the handler was running. If all pending interrupts are handled, the
458 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
460 spin_lock(&desc->lock);
462 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
465 * If we're currently running this IRQ, or its disabled,
466 * we shouldn't process the IRQ. Mark it pending, handle
467 * the necessary masking and go out
469 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
471 desc->status |= (IRQ_PENDING | IRQ_MASKED);
472 mask_ack_irq(desc, irq);
473 desc = irq_remap_to_desc(irq, desc);
476 kstat_incr_irqs_this_cpu(irq, desc);
478 /* Start handling the irq */
480 desc->chip->ack(irq);
481 desc = irq_remap_to_desc(irq, desc);
483 /* Mark the IRQ currently in progress.*/
484 desc->status |= IRQ_INPROGRESS;
487 struct irqaction *action = desc->action;
488 irqreturn_t action_ret;
490 if (unlikely(!action)) {
491 desc->chip->mask(irq);
496 * When another irq arrived while we were handling
497 * one, we could have masked the irq.
498 * Renable it, if it was not disabled in meantime.
500 if (unlikely((desc->status &
501 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
502 (IRQ_PENDING | IRQ_MASKED))) {
503 desc->chip->unmask(irq);
504 desc->status &= ~IRQ_MASKED;
507 desc->status &= ~IRQ_PENDING;
508 spin_unlock(&desc->lock);
509 action_ret = handle_IRQ_event(irq, action);
511 note_interrupt(irq, desc, action_ret);
512 spin_lock(&desc->lock);
514 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
516 desc->status &= ~IRQ_INPROGRESS;
518 spin_unlock(&desc->lock);
522 * handle_percpu_IRQ - Per CPU local irq handler
523 * @irq: the interrupt number
524 * @desc: the interrupt description structure for this irq
526 * Per CPU interrupts on SMP machines without locking requirements
529 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
531 irqreturn_t action_ret;
533 kstat_incr_irqs_this_cpu(irq, desc);
536 desc->chip->ack(irq);
538 action_ret = handle_IRQ_event(irq, desc->action);
540 note_interrupt(irq, desc, action_ret);
542 if (desc->chip->eoi) {
543 desc->chip->eoi(irq);
544 desc = irq_remap_to_desc(irq, desc);
549 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
552 struct irq_desc *desc = irq_to_desc(irq);
557 "Trying to install type control for IRQ%d\n", irq);
562 handle = handle_bad_irq;
563 else if (desc->chip == &no_irq_chip) {
564 printk(KERN_WARNING "Trying to install %sinterrupt handler "
565 "for IRQ%d\n", is_chained ? "chained " : "", irq);
567 * Some ARM implementations install a handler for really dumb
568 * interrupt hardware without setting an irq_chip. This worked
569 * with the ARM no_irq_chip but the check in setup_irq would
570 * prevent us to setup the interrupt at all. Switch it to
571 * dummy_irq_chip for easy transition.
573 desc->chip = &dummy_irq_chip;
576 spin_lock_irqsave(&desc->lock, flags);
579 if (handle == handle_bad_irq) {
580 if (desc->chip != &no_irq_chip) {
581 mask_ack_irq(desc, irq);
582 desc = irq_remap_to_desc(irq, desc);
584 desc->status |= IRQ_DISABLED;
587 desc->handle_irq = handle;
590 if (handle != handle_bad_irq && is_chained) {
591 desc->status &= ~IRQ_DISABLED;
592 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
594 desc->chip->startup(irq);
596 spin_unlock_irqrestore(&desc->lock, flags);
600 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
601 irq_flow_handler_t handle)
603 set_irq_chip(irq, chip);
604 __set_irq_handler(irq, handle, 0, NULL);
608 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
609 irq_flow_handler_t handle, const char *name)
611 set_irq_chip(irq, chip);
612 __set_irq_handler(irq, handle, 0, name);
615 void __init set_irq_noprobe(unsigned int irq)
617 struct irq_desc *desc = irq_to_desc(irq);
621 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
625 spin_lock_irqsave(&desc->lock, flags);
626 desc->status |= IRQ_NOPROBE;
627 spin_unlock_irqrestore(&desc->lock, flags);
630 void __init set_irq_probe(unsigned int irq)
632 struct irq_desc *desc = irq_to_desc(irq);
636 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
640 spin_lock_irqsave(&desc->lock, flags);
641 desc->status &= ~IRQ_NOPROBE;
642 spin_unlock_irqrestore(&desc->lock, flags);