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_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_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_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 type for an irq
116 * @type: interrupt type - see include/linux/interrupt.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_desc + irq;
130 if (desc->chip->set_type) {
131 spin_lock_irqsave(&desc->lock, flags);
132 ret = desc->chip->set_type(irq, type);
133 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;
151 if (irq >= NR_IRQS) {
153 "Trying to install controller data for IRQ%d\n", irq);
157 desc = irq_desc + irq;
158 spin_lock_irqsave(&desc->lock, flags);
159 desc->handler_data = data;
160 spin_unlock_irqrestore(&desc->lock, flags);
163 EXPORT_SYMBOL(set_irq_data);
166 * set_irq_data - set irq type data for an irq
167 * @irq: Interrupt number
168 * @entry: Pointer to MSI descriptor data
170 * Set the hardware irq controller data for an irq
172 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
174 struct irq_desc *desc;
177 if (irq >= NR_IRQS) {
179 "Trying to install msi data for IRQ%d\n", irq);
182 desc = irq_desc + irq;
183 spin_lock_irqsave(&desc->lock, flags);
184 desc->msi_desc = entry;
187 spin_unlock_irqrestore(&desc->lock, flags);
192 * set_irq_chip_data - set irq chip data for an irq
193 * @irq: Interrupt number
194 * @data: Pointer to chip specific data
196 * Set the hardware irq chip data for an irq
198 int set_irq_chip_data(unsigned int irq, void *data)
200 struct irq_desc *desc = irq_desc + irq;
203 if (irq >= NR_IRQS || !desc->chip) {
204 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
208 spin_lock_irqsave(&desc->lock, flags);
209 desc->chip_data = data;
210 spin_unlock_irqrestore(&desc->lock, flags);
214 EXPORT_SYMBOL(set_irq_chip_data);
217 * default enable function
219 static void default_enable(unsigned int irq)
221 struct irq_desc *desc = irq_desc + irq;
223 desc->chip->unmask(irq);
224 desc->status &= ~IRQ_MASKED;
228 * default disable function
230 static void default_disable(unsigned int irq)
235 * default startup function
237 static unsigned int default_startup(unsigned int irq)
239 irq_desc[irq].chip->enable(irq);
245 * default shutdown function
247 static void default_shutdown(unsigned int irq)
249 struct irq_desc *desc = irq_desc + irq;
251 desc->chip->mask(irq);
252 desc->status |= IRQ_MASKED;
256 * Fixup enable/disable function pointers
258 void irq_chip_set_defaults(struct irq_chip *chip)
261 chip->enable = default_enable;
263 chip->disable = default_disable;
265 chip->startup = default_startup;
267 * We use chip->disable, when the user provided its own. When
268 * we have default_disable set for chip->disable, then we need
269 * to use default_shutdown, otherwise the irq line is not
270 * disabled on free_irq():
273 chip->shutdown = chip->disable != default_disable ?
274 chip->disable : default_shutdown;
276 chip->name = chip->typename;
278 chip->end = dummy_irq_chip.end;
281 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
283 if (desc->chip->mask_ack)
284 desc->chip->mask_ack(irq);
286 desc->chip->mask(irq);
287 desc->chip->ack(irq);
292 * handle_simple_irq - Simple and software-decoded IRQs.
293 * @irq: the interrupt number
294 * @desc: the interrupt description structure for this irq
296 * Simple interrupts are either sent from a demultiplexing interrupt
297 * handler or come from hardware, where no interrupt hardware control
300 * Note: The caller is expected to handle the ack, clear, mask and
301 * unmask issues if necessary.
304 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
306 struct irqaction *action;
307 irqreturn_t action_ret;
308 const unsigned int cpu = smp_processor_id();
310 spin_lock(&desc->lock);
312 if (unlikely(desc->status & IRQ_INPROGRESS))
314 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
315 kstat_cpu(cpu).irqs[irq]++;
317 action = desc->action;
318 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
321 desc->status |= IRQ_INPROGRESS;
322 spin_unlock(&desc->lock);
324 action_ret = handle_IRQ_event(irq, action);
326 note_interrupt(irq, desc, action_ret);
328 spin_lock(&desc->lock);
329 desc->status &= ~IRQ_INPROGRESS;
331 spin_unlock(&desc->lock);
335 * handle_level_irq - Level type irq handler
336 * @irq: the interrupt number
337 * @desc: the interrupt description structure for this irq
339 * Level type interrupts are active as long as the hardware line has
340 * the active level. This may require to mask the interrupt and unmask
341 * it after the associated handler has acknowledged the device, so the
342 * interrupt line is back to inactive.
345 handle_level_irq(unsigned int irq, struct irq_desc *desc)
347 unsigned int cpu = smp_processor_id();
348 struct irqaction *action;
349 irqreturn_t action_ret;
351 spin_lock(&desc->lock);
352 mask_ack_irq(desc, irq);
354 if (unlikely(desc->status & IRQ_INPROGRESS))
356 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
357 kstat_cpu(cpu).irqs[irq]++;
360 * If its disabled or no action available
361 * keep it masked and get out of here
363 action = desc->action;
364 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
367 desc->status |= IRQ_INPROGRESS;
368 spin_unlock(&desc->lock);
370 action_ret = handle_IRQ_event(irq, action);
372 note_interrupt(irq, desc, action_ret);
374 spin_lock(&desc->lock);
375 desc->status &= ~IRQ_INPROGRESS;
376 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
377 desc->chip->unmask(irq);
379 spin_unlock(&desc->lock);
383 * handle_fasteoi_irq - irq handler for transparent controllers
384 * @irq: the interrupt number
385 * @desc: the interrupt description structure for this irq
387 * Only a single callback will be issued to the chip: an ->eoi()
388 * call when the interrupt has been serviced. This enables support
389 * for modern forms of interrupt handlers, which handle the flow
390 * details in hardware, transparently.
393 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
395 unsigned int cpu = smp_processor_id();
396 struct irqaction *action;
397 irqreturn_t action_ret;
399 spin_lock(&desc->lock);
401 if (unlikely(desc->status & IRQ_INPROGRESS))
404 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
405 kstat_cpu(cpu).irqs[irq]++;
408 * If its disabled or no action available
409 * then mask it and get out of here:
411 action = desc->action;
412 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
413 desc->status |= IRQ_PENDING;
414 if (desc->chip->mask)
415 desc->chip->mask(irq);
419 desc->status |= IRQ_INPROGRESS;
420 desc->status &= ~IRQ_PENDING;
421 spin_unlock(&desc->lock);
423 action_ret = handle_IRQ_event(irq, action);
425 note_interrupt(irq, desc, action_ret);
427 spin_lock(&desc->lock);
428 desc->status &= ~IRQ_INPROGRESS;
430 desc->chip->eoi(irq);
432 spin_unlock(&desc->lock);
436 * handle_edge_irq - edge type IRQ handler
437 * @irq: the interrupt number
438 * @desc: the interrupt description structure for this irq
440 * Interrupt occures on the falling and/or rising edge of a hardware
441 * signal. The occurence is latched into the irq controller hardware
442 * and must be acked in order to be reenabled. After the ack another
443 * interrupt can happen on the same source even before the first one
444 * is handled by the assosiacted event handler. If this happens it
445 * might be necessary to disable (mask) the interrupt depending on the
446 * controller hardware. This requires to reenable the interrupt inside
447 * of the loop which handles the interrupts which have arrived while
448 * the handler was running. If all pending interrupts are handled, the
452 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
454 const unsigned int cpu = smp_processor_id();
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);
472 kstat_cpu(cpu).irqs[irq]++;
474 /* Start handling the irq */
475 desc->chip->ack(irq);
477 /* Mark the IRQ currently in progress.*/
478 desc->status |= IRQ_INPROGRESS;
481 struct irqaction *action = desc->action;
482 irqreturn_t action_ret;
484 if (unlikely(!action)) {
485 desc->chip->mask(irq);
490 * When another irq arrived while we were handling
491 * one, we could have masked the irq.
492 * Renable it, if it was not disabled in meantime.
494 if (unlikely((desc->status &
495 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
496 (IRQ_PENDING | IRQ_MASKED))) {
497 desc->chip->unmask(irq);
498 desc->status &= ~IRQ_MASKED;
501 desc->status &= ~IRQ_PENDING;
502 spin_unlock(&desc->lock);
503 action_ret = handle_IRQ_event(irq, action);
505 note_interrupt(irq, desc, action_ret);
506 spin_lock(&desc->lock);
508 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
510 desc->status &= ~IRQ_INPROGRESS;
512 spin_unlock(&desc->lock);
516 * handle_percpu_IRQ - Per CPU local irq handler
517 * @irq: the interrupt number
518 * @desc: the interrupt description structure for this irq
520 * Per CPU interrupts on SMP machines without locking requirements
523 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
525 irqreturn_t action_ret;
527 kstat_this_cpu.irqs[irq]++;
530 desc->chip->ack(irq);
532 action_ret = handle_IRQ_event(irq, desc->action);
534 note_interrupt(irq, desc, action_ret);
537 desc->chip->eoi(irq);
541 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
544 struct irq_desc *desc;
547 if (irq >= NR_IRQS) {
549 "Trying to install type control for IRQ%d\n", irq);
553 desc = irq_desc + irq;
556 handle = handle_bad_irq;
557 else if (desc->chip == &no_irq_chip) {
558 printk(KERN_WARNING "Trying to install %sinterrupt handler "
559 "for IRQ%d\n", is_chained ? "chained " : "", irq);
561 * Some ARM implementations install a handler for really dumb
562 * interrupt hardware without setting an irq_chip. This worked
563 * with the ARM no_irq_chip but the check in setup_irq would
564 * prevent us to setup the interrupt at all. Switch it to
565 * dummy_irq_chip for easy transition.
567 desc->chip = &dummy_irq_chip;
570 spin_lock_irqsave(&desc->lock, flags);
573 if (handle == handle_bad_irq) {
574 if (desc->chip != &no_irq_chip)
575 mask_ack_irq(desc, irq);
576 desc->status |= IRQ_DISABLED;
579 desc->handle_irq = handle;
582 if (handle != handle_bad_irq && is_chained) {
583 desc->status &= ~IRQ_DISABLED;
584 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
586 desc->chip->unmask(irq);
588 spin_unlock_irqrestore(&desc->lock, flags);
592 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
593 irq_flow_handler_t handle)
595 set_irq_chip(irq, chip);
596 __set_irq_handler(irq, handle, 0, NULL);
600 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
601 irq_flow_handler_t handle, const char *name)
603 set_irq_chip(irq, chip);
604 __set_irq_handler(irq, handle, 0, name);
607 void __init set_irq_noprobe(unsigned int irq)
609 struct irq_desc *desc;
612 if (irq >= NR_IRQS) {
613 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
618 desc = irq_desc + irq;
620 spin_lock_irqsave(&desc->lock, flags);
621 desc->status |= IRQ_NOPROBE;
622 spin_unlock_irqrestore(&desc->lock, flags);
625 void __init set_irq_probe(unsigned int irq)
627 struct irq_desc *desc;
630 if (irq >= NR_IRQS) {
631 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
636 desc = irq_desc + irq;
638 spin_lock_irqsave(&desc->lock, flags);
639 desc->status &= ~IRQ_NOPROBE;
640 spin_unlock_irqrestore(&desc->lock, flags);