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 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 = 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 if (type == IRQ_TYPE_NONE)
131 spin_lock_irqsave(&desc->lock, flags);
132 ret = __irq_set_trigger(desc, irq, type);
133 spin_unlock_irqrestore(&desc->lock, flags);
136 EXPORT_SYMBOL(set_irq_type);
139 * set_irq_data - set irq type data for an irq
140 * @irq: Interrupt number
141 * @data: Pointer to interrupt specific data
143 * Set the hardware irq controller data for an irq
145 int set_irq_data(unsigned int irq, void *data)
147 struct irq_desc *desc = irq_to_desc(irq);
152 "Trying to install controller data for IRQ%d\n", irq);
156 spin_lock_irqsave(&desc->lock, flags);
157 desc->handler_data = data;
158 spin_unlock_irqrestore(&desc->lock, flags);
161 EXPORT_SYMBOL(set_irq_data);
164 * set_irq_data - set irq type data for an irq
165 * @irq: Interrupt number
166 * @entry: Pointer to MSI descriptor data
168 * Set the hardware irq controller data for an irq
170 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
172 struct irq_desc *desc = irq_to_desc(irq);
177 "Trying to install msi data for IRQ%d\n", irq);
181 spin_lock_irqsave(&desc->lock, flags);
182 desc->msi_desc = entry;
185 spin_unlock_irqrestore(&desc->lock, flags);
190 * set_irq_chip_data - set irq chip data for an irq
191 * @irq: Interrupt number
192 * @data: Pointer to chip specific data
194 * Set the hardware irq chip data for an irq
196 int set_irq_chip_data(unsigned int irq, void *data)
198 struct irq_desc *desc = irq_to_desc(irq);
203 "Trying to install chip data for IRQ%d\n", irq);
208 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
212 spin_lock_irqsave(&desc->lock, flags);
213 desc->chip_data = data;
214 spin_unlock_irqrestore(&desc->lock, flags);
218 EXPORT_SYMBOL(set_irq_chip_data);
221 * default enable function
223 static void default_enable(unsigned int irq)
225 struct irq_desc *desc = irq_to_desc(irq);
227 desc->chip->unmask(irq);
228 desc->status &= ~IRQ_MASKED;
232 * default disable function
234 static void default_disable(unsigned int irq)
239 * default startup function
241 static unsigned int default_startup(unsigned int irq)
243 struct irq_desc *desc = irq_to_desc(irq);
245 desc->chip->enable(irq);
250 * default shutdown function
252 static void default_shutdown(unsigned int irq)
254 struct irq_desc *desc = irq_to_desc(irq);
256 desc->chip->mask(irq);
257 desc->status |= IRQ_MASKED;
261 * Fixup enable/disable function pointers
263 void irq_chip_set_defaults(struct irq_chip *chip)
266 chip->enable = default_enable;
268 chip->disable = default_disable;
270 chip->startup = default_startup;
272 * We use chip->disable, when the user provided its own. When
273 * we have default_disable set for chip->disable, then we need
274 * to use default_shutdown, otherwise the irq line is not
275 * disabled on free_irq():
278 chip->shutdown = chip->disable != default_disable ?
279 chip->disable : default_shutdown;
281 chip->name = chip->typename;
283 chip->end = dummy_irq_chip.end;
286 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
288 if (desc->chip->mask_ack)
289 desc->chip->mask_ack(irq);
291 desc->chip->mask(irq);
292 desc->chip->ack(irq);
297 * handle_simple_irq - Simple and software-decoded IRQs.
298 * @irq: the interrupt number
299 * @desc: the interrupt description structure for this irq
301 * Simple interrupts are either sent from a demultiplexing interrupt
302 * handler or come from hardware, where no interrupt hardware control
305 * Note: The caller is expected to handle the ack, clear, mask and
306 * unmask issues if necessary.
309 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
311 struct irqaction *action;
312 irqreturn_t action_ret;
314 spin_lock(&desc->lock);
316 if (unlikely(desc->status & IRQ_INPROGRESS))
318 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
319 kstat_incr_irqs_this_cpu(irq, desc);
321 action = desc->action;
322 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
325 desc->status |= IRQ_INPROGRESS;
326 spin_unlock(&desc->lock);
328 action_ret = handle_IRQ_event(irq, action);
330 note_interrupt(irq, desc, action_ret);
332 spin_lock(&desc->lock);
333 desc->status &= ~IRQ_INPROGRESS;
335 spin_unlock(&desc->lock);
339 * handle_level_irq - Level type irq handler
340 * @irq: the interrupt number
341 * @desc: the interrupt description structure for this irq
343 * Level type interrupts are active as long as the hardware line has
344 * the active level. This may require to mask the interrupt and unmask
345 * it after the associated handler has acknowledged the device, so the
346 * interrupt line is back to inactive.
349 handle_level_irq(unsigned int irq, struct irq_desc *desc)
351 struct irqaction *action;
352 irqreturn_t action_ret;
354 spin_lock(&desc->lock);
355 mask_ack_irq(desc, irq);
357 if (unlikely(desc->status & IRQ_INPROGRESS))
359 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
360 kstat_incr_irqs_this_cpu(irq, desc);
363 * If its disabled or no action available
364 * keep it masked and get out of here
366 action = desc->action;
367 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
370 desc->status |= IRQ_INPROGRESS;
371 spin_unlock(&desc->lock);
373 action_ret = handle_IRQ_event(irq, action);
375 note_interrupt(irq, desc, action_ret);
377 spin_lock(&desc->lock);
378 desc->status &= ~IRQ_INPROGRESS;
379 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
380 desc->chip->unmask(irq);
382 spin_unlock(&desc->lock);
386 * handle_fasteoi_irq - irq handler for transparent controllers
387 * @irq: the interrupt number
388 * @desc: the interrupt description structure for this irq
390 * Only a single callback will be issued to the chip: an ->eoi()
391 * call when the interrupt has been serviced. This enables support
392 * for modern forms of interrupt handlers, which handle the flow
393 * details in hardware, transparently.
396 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
398 struct irqaction *action;
399 irqreturn_t action_ret;
401 spin_lock(&desc->lock);
403 if (unlikely(desc->status & IRQ_INPROGRESS))
406 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
407 kstat_incr_irqs_this_cpu(irq, desc);
410 * If its disabled or no action available
411 * then mask it and get out of here:
413 action = desc->action;
414 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
415 desc->status |= IRQ_PENDING;
416 if (desc->chip->mask)
417 desc->chip->mask(irq);
421 desc->status |= IRQ_INPROGRESS;
422 desc->status &= ~IRQ_PENDING;
423 spin_unlock(&desc->lock);
425 action_ret = handle_IRQ_event(irq, action);
427 note_interrupt(irq, desc, action_ret);
429 spin_lock(&desc->lock);
430 desc->status &= ~IRQ_INPROGRESS;
432 desc->chip->eoi(irq);
434 spin_unlock(&desc->lock);
438 * handle_edge_irq - edge type IRQ handler
439 * @irq: the interrupt number
440 * @desc: the interrupt description structure for this irq
442 * Interrupt occures on the falling and/or rising edge of a hardware
443 * signal. The occurence is latched into the irq controller hardware
444 * and must be acked in order to be reenabled. After the ack another
445 * interrupt can happen on the same source even before the first one
446 * is handled by the assosiacted event handler. If this happens it
447 * might be necessary to disable (mask) the interrupt depending on the
448 * controller hardware. This requires to reenable the interrupt inside
449 * of the loop which handles the interrupts which have arrived while
450 * the handler was running. If all pending interrupts are handled, the
454 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
456 spin_lock(&desc->lock);
458 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
461 * If we're currently running this IRQ, or its disabled,
462 * we shouldn't process the IRQ. Mark it pending, handle
463 * the necessary masking and go out
465 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
467 desc->status |= (IRQ_PENDING | IRQ_MASKED);
468 mask_ack_irq(desc, irq);
471 kstat_incr_irqs_this_cpu(irq, desc);
473 /* Start handling the irq */
474 desc->chip->ack(irq);
476 /* Mark the IRQ currently in progress.*/
477 desc->status |= IRQ_INPROGRESS;
480 struct irqaction *action = desc->action;
481 irqreturn_t action_ret;
483 if (unlikely(!action)) {
484 desc->chip->mask(irq);
489 * When another irq arrived while we were handling
490 * one, we could have masked the irq.
491 * Renable it, if it was not disabled in meantime.
493 if (unlikely((desc->status &
494 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
495 (IRQ_PENDING | IRQ_MASKED))) {
496 desc->chip->unmask(irq);
497 desc->status &= ~IRQ_MASKED;
500 desc->status &= ~IRQ_PENDING;
501 spin_unlock(&desc->lock);
502 action_ret = handle_IRQ_event(irq, action);
504 note_interrupt(irq, desc, action_ret);
505 spin_lock(&desc->lock);
507 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
509 desc->status &= ~IRQ_INPROGRESS;
511 spin_unlock(&desc->lock);
515 * handle_percpu_IRQ - Per CPU local irq handler
516 * @irq: the interrupt number
517 * @desc: the interrupt description structure for this irq
519 * Per CPU interrupts on SMP machines without locking requirements
522 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
524 irqreturn_t action_ret;
526 kstat_incr_irqs_this_cpu(irq, desc);
529 desc->chip->ack(irq);
531 action_ret = handle_IRQ_event(irq, desc->action);
533 note_interrupt(irq, desc, action_ret);
536 desc->chip->eoi(irq);
540 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
543 struct irq_desc *desc = irq_to_desc(irq);
548 "Trying to install type control for IRQ%d\n", irq);
553 handle = handle_bad_irq;
554 else if (desc->chip == &no_irq_chip) {
555 printk(KERN_WARNING "Trying to install %sinterrupt handler "
556 "for IRQ%d\n", is_chained ? "chained " : "", irq);
558 * Some ARM implementations install a handler for really dumb
559 * interrupt hardware without setting an irq_chip. This worked
560 * with the ARM no_irq_chip but the check in setup_irq would
561 * prevent us to setup the interrupt at all. Switch it to
562 * dummy_irq_chip for easy transition.
564 desc->chip = &dummy_irq_chip;
567 spin_lock_irqsave(&desc->lock, flags);
570 if (handle == handle_bad_irq) {
571 if (desc->chip != &no_irq_chip)
572 mask_ack_irq(desc, irq);
573 desc->status |= IRQ_DISABLED;
576 desc->handle_irq = handle;
579 if (handle != handle_bad_irq && is_chained) {
580 desc->status &= ~IRQ_DISABLED;
581 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
583 desc->chip->startup(irq);
585 spin_unlock_irqrestore(&desc->lock, flags);
589 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
590 irq_flow_handler_t handle)
592 set_irq_chip(irq, chip);
593 __set_irq_handler(irq, handle, 0, NULL);
597 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
598 irq_flow_handler_t handle, const char *name)
600 set_irq_chip(irq, chip);
601 __set_irq_handler(irq, handle, 0, name);
604 void __init set_irq_noprobe(unsigned int irq)
606 struct irq_desc *desc = irq_to_desc(irq);
610 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
614 spin_lock_irqsave(&desc->lock, flags);
615 desc->status |= IRQ_NOPROBE;
616 spin_unlock_irqrestore(&desc->lock, flags);
619 void __init set_irq_probe(unsigned int irq)
621 struct irq_desc *desc = irq_to_desc(irq);
625 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
629 spin_lock_irqsave(&desc->lock, flags);
630 desc->status &= ~IRQ_NOPROBE;
631 spin_unlock_irqrestore(&desc->lock, flags);