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/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
18 #include "internals.h"
21 * dynamic_irq_init - initialize a dynamically allocated irq
22 * @irq: irq number to initialize
24 void dynamic_irq_init(unsigned int irq)
26 struct irq_desc *desc;
30 printk(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_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 desc->affinity = CPU_MASK_ALL;
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 printk(KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
69 desc = irq_desc + irq;
70 spin_lock_irqsave(&desc->lock, flags);
72 spin_unlock_irqrestore(&desc->lock, flags);
73 printk(KERN_ERR "Destroying IRQ%d without calling free_irq\n",
78 desc->msi_desc = NULL;
79 desc->handler_data = NULL;
80 desc->chip_data = NULL;
81 desc->handle_irq = handle_bad_irq;
82 desc->chip = &no_irq_chip;
83 spin_unlock_irqrestore(&desc->lock, flags);
88 * set_irq_chip - set the irq chip for an irq
90 * @chip: pointer to irq chip description structure
92 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
94 struct irq_desc *desc;
98 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
106 desc = irq_desc + irq;
107 spin_lock_irqsave(&desc->lock, flags);
108 irq_chip_set_defaults(chip);
110 spin_unlock_irqrestore(&desc->lock, flags);
114 EXPORT_SYMBOL(set_irq_chip);
117 * set_irq_type - set the irq type for an irq
119 * @type: interrupt type - see include/linux/interrupt.h
121 int set_irq_type(unsigned int irq, unsigned int type)
123 struct irq_desc *desc;
127 if (irq >= NR_IRQS) {
128 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
132 desc = irq_desc + irq;
133 if (desc->chip->set_type) {
134 spin_lock_irqsave(&desc->lock, flags);
135 ret = desc->chip->set_type(irq, type);
136 spin_unlock_irqrestore(&desc->lock, flags);
140 EXPORT_SYMBOL(set_irq_type);
143 * set_irq_data - set irq type data for an irq
144 * @irq: Interrupt number
145 * @data: Pointer to interrupt specific data
147 * Set the hardware irq controller data for an irq
149 int set_irq_data(unsigned int irq, void *data)
151 struct irq_desc *desc;
154 if (irq >= NR_IRQS) {
156 "Trying to install controller data for IRQ%d\n", irq);
160 desc = irq_desc + irq;
161 spin_lock_irqsave(&desc->lock, flags);
162 desc->handler_data = data;
163 spin_unlock_irqrestore(&desc->lock, flags);
166 EXPORT_SYMBOL(set_irq_data);
169 * set_irq_data - set irq type data for an irq
170 * @irq: Interrupt number
171 * @data: Pointer to interrupt specific data
173 * Set the hardware irq controller data for an irq
175 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
177 struct irq_desc *desc;
180 if (irq >= NR_IRQS) {
182 "Trying to install msi data for IRQ%d\n", irq);
185 desc = irq_desc + irq;
186 spin_lock_irqsave(&desc->lock, flags);
187 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 = irq_desc + irq;
204 if (irq >= NR_IRQS || !desc->chip) {
205 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
209 spin_lock_irqsave(&desc->lock, flags);
210 desc->chip_data = data;
211 spin_unlock_irqrestore(&desc->lock, flags);
215 EXPORT_SYMBOL(set_irq_chip_data);
218 * default enable function
220 static void default_enable(unsigned int irq)
222 struct irq_desc *desc = irq_desc + irq;
224 desc->chip->unmask(irq);
225 desc->status &= ~IRQ_MASKED;
229 * default disable function
231 static void default_disable(unsigned int irq)
233 struct irq_desc *desc = irq_desc + irq;
235 if (!(desc->status & IRQ_DELAYED_DISABLE))
236 desc->chip->mask(irq);
240 * default startup function
242 static unsigned int default_startup(unsigned int irq)
244 irq_desc[irq].chip->enable(irq);
250 * Fixup enable/disable function pointers
252 void irq_chip_set_defaults(struct irq_chip *chip)
255 chip->enable = default_enable;
257 chip->disable = default_disable;
259 chip->startup = default_startup;
261 chip->shutdown = chip->disable;
263 chip->name = chip->typename;
265 chip->end = dummy_irq_chip.end;
268 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
270 if (desc->chip->mask_ack)
271 desc->chip->mask_ack(irq);
273 desc->chip->mask(irq);
274 desc->chip->ack(irq);
279 * handle_simple_irq - Simple and software-decoded IRQs.
280 * @irq: the interrupt number
281 * @desc: the interrupt description structure for this irq
283 * Simple interrupts are either sent from a demultiplexing interrupt
284 * handler or come from hardware, where no interrupt hardware control
287 * Note: The caller is expected to handle the ack, clear, mask and
288 * unmask issues if necessary.
291 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
293 struct irqaction *action;
294 irqreturn_t action_ret;
295 const unsigned int cpu = smp_processor_id();
297 spin_lock(&desc->lock);
299 if (unlikely(desc->status & IRQ_INPROGRESS))
301 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
302 kstat_cpu(cpu).irqs[irq]++;
304 action = desc->action;
305 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
308 desc->status |= IRQ_INPROGRESS;
309 spin_unlock(&desc->lock);
311 action_ret = handle_IRQ_event(irq, action);
313 note_interrupt(irq, desc, action_ret);
315 spin_lock(&desc->lock);
316 desc->status &= ~IRQ_INPROGRESS;
318 spin_unlock(&desc->lock);
322 * handle_level_irq - Level type irq handler
323 * @irq: the interrupt number
324 * @desc: the interrupt description structure for this irq
326 * Level type interrupts are active as long as the hardware line has
327 * the active level. This may require to mask the interrupt and unmask
328 * it after the associated handler has acknowledged the device, so the
329 * interrupt line is back to inactive.
332 handle_level_irq(unsigned int irq, struct irq_desc *desc)
334 unsigned int cpu = smp_processor_id();
335 struct irqaction *action;
336 irqreturn_t action_ret;
338 spin_lock(&desc->lock);
339 mask_ack_irq(desc, irq);
341 if (unlikely(desc->status & IRQ_INPROGRESS))
343 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
344 kstat_cpu(cpu).irqs[irq]++;
347 * If its disabled or no action available
348 * keep it masked and get out of here
350 action = desc->action;
351 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
352 desc->status |= IRQ_PENDING;
356 desc->status |= IRQ_INPROGRESS;
357 desc->status &= ~IRQ_PENDING;
358 spin_unlock(&desc->lock);
360 action_ret = handle_IRQ_event(irq, action);
362 note_interrupt(irq, desc, action_ret);
364 spin_lock(&desc->lock);
365 desc->status &= ~IRQ_INPROGRESS;
366 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
367 desc->chip->unmask(irq);
369 spin_unlock(&desc->lock);
373 * handle_fasteoi_irq - irq handler for transparent controllers
374 * @irq: the interrupt number
375 * @desc: the interrupt description structure for this irq
377 * Only a single callback will be issued to the chip: an ->eoi()
378 * call when the interrupt has been serviced. This enables support
379 * for modern forms of interrupt handlers, which handle the flow
380 * details in hardware, transparently.
383 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
385 unsigned int cpu = smp_processor_id();
386 struct irqaction *action;
387 irqreturn_t action_ret;
389 spin_lock(&desc->lock);
391 if (unlikely(desc->status & IRQ_INPROGRESS))
394 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
395 kstat_cpu(cpu).irqs[irq]++;
398 * If its disabled or no action available
399 * keep it masked and get out of here
401 action = desc->action;
402 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
403 desc->status |= IRQ_PENDING;
407 desc->status |= IRQ_INPROGRESS;
408 desc->status &= ~IRQ_PENDING;
409 spin_unlock(&desc->lock);
411 action_ret = handle_IRQ_event(irq, action);
413 note_interrupt(irq, desc, action_ret);
415 spin_lock(&desc->lock);
416 desc->status &= ~IRQ_INPROGRESS;
418 desc->chip->eoi(irq);
420 spin_unlock(&desc->lock);
424 * handle_edge_irq - edge type IRQ handler
425 * @irq: the interrupt number
426 * @desc: the interrupt description structure for this irq
428 * Interrupt occures on the falling and/or rising edge of a hardware
429 * signal. The occurence is latched into the irq controller hardware
430 * and must be acked in order to be reenabled. After the ack another
431 * interrupt can happen on the same source even before the first one
432 * is handled by the assosiacted event handler. If this happens it
433 * might be necessary to disable (mask) the interrupt depending on the
434 * controller hardware. This requires to reenable the interrupt inside
435 * of the loop which handles the interrupts which have arrived while
436 * the handler was running. If all pending interrupts are handled, the
440 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
442 const unsigned int cpu = smp_processor_id();
444 spin_lock(&desc->lock);
446 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
449 * If we're currently running this IRQ, or its disabled,
450 * we shouldn't process the IRQ. Mark it pending, handle
451 * the necessary masking and go out
453 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
455 desc->status |= (IRQ_PENDING | IRQ_MASKED);
456 mask_ack_irq(desc, irq);
460 kstat_cpu(cpu).irqs[irq]++;
462 /* Start handling the irq */
463 desc->chip->ack(irq);
465 /* Mark the IRQ currently in progress.*/
466 desc->status |= IRQ_INPROGRESS;
469 struct irqaction *action = desc->action;
470 irqreturn_t action_ret;
472 if (unlikely(!action)) {
473 desc->chip->mask(irq);
478 * When another irq arrived while we were handling
479 * one, we could have masked the irq.
480 * Renable it, if it was not disabled in meantime.
482 if (unlikely((desc->status &
483 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
484 (IRQ_PENDING | IRQ_MASKED))) {
485 desc->chip->unmask(irq);
486 desc->status &= ~IRQ_MASKED;
489 desc->status &= ~IRQ_PENDING;
490 spin_unlock(&desc->lock);
491 action_ret = handle_IRQ_event(irq, action);
493 note_interrupt(irq, desc, action_ret);
494 spin_lock(&desc->lock);
496 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
498 desc->status &= ~IRQ_INPROGRESS;
500 spin_unlock(&desc->lock);
505 * handle_percpu_IRQ - Per CPU local irq handler
506 * @irq: the interrupt number
507 * @desc: the interrupt description structure for this irq
509 * Per CPU interrupts on SMP machines without locking requirements
512 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
514 irqreturn_t action_ret;
516 kstat_this_cpu.irqs[irq]++;
519 desc->chip->ack(irq);
521 action_ret = handle_IRQ_event(irq, desc->action);
523 note_interrupt(irq, desc, action_ret);
526 desc->chip->eoi(irq);
529 #endif /* CONFIG_SMP */
532 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
535 struct irq_desc *desc;
538 if (irq >= NR_IRQS) {
540 "Trying to install type control for IRQ%d\n", irq);
544 desc = irq_desc + irq;
547 handle = handle_bad_irq;
548 else if (desc->chip == &no_irq_chip) {
549 printk(KERN_WARNING "Trying to install %sinterrupt handler "
550 "for IRQ%d\n", is_chained ? "chained " : "", irq);
552 * Some ARM implementations install a handler for really dumb
553 * interrupt hardware without setting an irq_chip. This worked
554 * with the ARM no_irq_chip but the check in setup_irq would
555 * prevent us to setup the interrupt at all. Switch it to
556 * dummy_irq_chip for easy transition.
558 desc->chip = &dummy_irq_chip;
561 spin_lock_irqsave(&desc->lock, flags);
564 if (handle == handle_bad_irq) {
565 if (desc->chip != &no_irq_chip) {
566 desc->chip->mask(irq);
567 desc->chip->ack(irq);
569 desc->status |= IRQ_DISABLED;
572 desc->handle_irq = handle;
575 if (handle != handle_bad_irq && is_chained) {
576 desc->status &= ~IRQ_DISABLED;
577 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
579 desc->chip->unmask(irq);
581 spin_unlock_irqrestore(&desc->lock, flags);
585 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
586 irq_flow_handler_t handle)
588 set_irq_chip(irq, chip);
589 __set_irq_handler(irq, handle, 0, NULL);
593 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
594 irq_flow_handler_t handle, const char *name)
596 set_irq_chip(irq, chip);
597 __set_irq_handler(irq, handle, 0, name);