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_to_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_to_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_to_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 trigger type for an irq
116 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.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_to_desc(irq);
130 if (type == IRQ_TYPE_NONE)
133 spin_lock_irqsave(&desc->lock, flags);
134 ret = __irq_set_trigger(desc, irq, flags);
135 spin_unlock_irqrestore(&desc->lock, flags);
138 EXPORT_SYMBOL(set_irq_type);
141 * set_irq_data - set irq type data for an irq
142 * @irq: Interrupt number
143 * @data: Pointer to interrupt specific data
145 * Set the hardware irq controller data for an irq
147 int set_irq_data(unsigned int irq, void *data)
149 struct irq_desc *desc;
152 if (irq >= nr_irqs) {
154 "Trying to install controller data for IRQ%d\n", irq);
158 desc = irq_to_desc(irq);
159 spin_lock_irqsave(&desc->lock, flags);
160 desc->handler_data = data;
161 spin_unlock_irqrestore(&desc->lock, flags);
164 EXPORT_SYMBOL(set_irq_data);
167 * set_irq_data - set irq type data for an irq
168 * @irq: Interrupt number
169 * @entry: Pointer to MSI descriptor data
171 * Set the hardware irq controller data for an irq
173 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
175 struct irq_desc *desc;
178 if (irq >= nr_irqs) {
180 "Trying to install msi data for IRQ%d\n", irq);
183 desc = irq_to_desc(irq);
184 spin_lock_irqsave(&desc->lock, flags);
185 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;
204 desc = irq_to_desc(irq);
205 if (irq >= nr_irqs || !desc->chip) {
206 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
210 spin_lock_irqsave(&desc->lock, flags);
211 desc->chip_data = data;
212 spin_unlock_irqrestore(&desc->lock, flags);
216 EXPORT_SYMBOL(set_irq_chip_data);
219 * default enable function
221 static void default_enable(unsigned int irq)
223 struct irq_desc *desc;
225 desc = irq_to_desc(irq);
226 desc->chip->unmask(irq);
227 desc->status &= ~IRQ_MASKED;
231 * default disable function
233 static void default_disable(unsigned int irq)
238 * default startup function
240 static unsigned int default_startup(unsigned int irq)
242 struct irq_desc *desc;
244 desc = irq_to_desc(irq);
245 desc->chip->enable(irq);
251 * default shutdown function
253 static void default_shutdown(unsigned int irq)
255 struct irq_desc *desc;
257 desc = irq_to_desc(irq);
258 desc->chip->mask(irq);
259 desc->status |= IRQ_MASKED;
263 * Fixup enable/disable function pointers
265 void irq_chip_set_defaults(struct irq_chip *chip)
268 chip->enable = default_enable;
270 chip->disable = default_disable;
272 chip->startup = default_startup;
274 * We use chip->disable, when the user provided its own. When
275 * we have default_disable set for chip->disable, then we need
276 * to use default_shutdown, otherwise the irq line is not
277 * disabled on free_irq():
280 chip->shutdown = chip->disable != default_disable ?
281 chip->disable : default_shutdown;
283 chip->name = chip->typename;
285 chip->end = dummy_irq_chip.end;
288 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
290 if (desc->chip->mask_ack)
291 desc->chip->mask_ack(irq);
293 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;
315 const unsigned int cpu = smp_processor_id();
317 spin_lock(&desc->lock);
319 if (unlikely(desc->status & IRQ_INPROGRESS))
321 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
322 kstat_cpu(cpu).irqs[irq]++;
324 action = desc->action;
325 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
328 desc->status |= IRQ_INPROGRESS;
329 spin_unlock(&desc->lock);
331 action_ret = handle_IRQ_event(irq, action);
333 note_interrupt(irq, desc, action_ret);
335 spin_lock(&desc->lock);
336 desc->status &= ~IRQ_INPROGRESS;
338 spin_unlock(&desc->lock);
342 * handle_level_irq - Level type irq handler
343 * @irq: the interrupt number
344 * @desc: the interrupt description structure for this irq
346 * Level type interrupts are active as long as the hardware line has
347 * the active level. This may require to mask the interrupt and unmask
348 * it after the associated handler has acknowledged the device, so the
349 * interrupt line is back to inactive.
352 handle_level_irq(unsigned int irq, struct irq_desc *desc)
354 unsigned int cpu = smp_processor_id();
355 struct irqaction *action;
356 irqreturn_t action_ret;
358 spin_lock(&desc->lock);
359 mask_ack_irq(desc, irq);
361 if (unlikely(desc->status & IRQ_INPROGRESS))
363 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
364 kstat_cpu(cpu).irqs[irq]++;
367 * If its disabled or no action available
368 * keep it masked and get out of here
370 action = desc->action;
371 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
374 desc->status |= IRQ_INPROGRESS;
375 spin_unlock(&desc->lock);
377 action_ret = handle_IRQ_event(irq, action);
379 note_interrupt(irq, desc, action_ret);
381 spin_lock(&desc->lock);
382 desc->status &= ~IRQ_INPROGRESS;
383 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
384 desc->chip->unmask(irq);
386 spin_unlock(&desc->lock);
390 * handle_fasteoi_irq - irq handler for transparent controllers
391 * @irq: the interrupt number
392 * @desc: the interrupt description structure for this irq
394 * Only a single callback will be issued to the chip: an ->eoi()
395 * call when the interrupt has been serviced. This enables support
396 * for modern forms of interrupt handlers, which handle the flow
397 * details in hardware, transparently.
400 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
402 unsigned int cpu = smp_processor_id();
403 struct irqaction *action;
404 irqreturn_t action_ret;
406 spin_lock(&desc->lock);
408 if (unlikely(desc->status & IRQ_INPROGRESS))
411 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
412 kstat_cpu(cpu).irqs[irq]++;
415 * If its disabled or no action available
416 * then mask it and get out of here:
418 action = desc->action;
419 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
420 desc->status |= IRQ_PENDING;
421 if (desc->chip->mask)
422 desc->chip->mask(irq);
426 desc->status |= IRQ_INPROGRESS;
427 desc->status &= ~IRQ_PENDING;
428 spin_unlock(&desc->lock);
430 action_ret = handle_IRQ_event(irq, action);
432 note_interrupt(irq, desc, action_ret);
434 spin_lock(&desc->lock);
435 desc->status &= ~IRQ_INPROGRESS;
437 desc->chip->eoi(irq);
439 spin_unlock(&desc->lock);
443 * handle_edge_irq - edge type IRQ handler
444 * @irq: the interrupt number
445 * @desc: the interrupt description structure for this irq
447 * Interrupt occures on the falling and/or rising edge of a hardware
448 * signal. The occurence is latched into the irq controller hardware
449 * and must be acked in order to be reenabled. After the ack another
450 * interrupt can happen on the same source even before the first one
451 * is handled by the assosiacted event handler. If this happens it
452 * might be necessary to disable (mask) the interrupt depending on the
453 * controller hardware. This requires to reenable the interrupt inside
454 * of the loop which handles the interrupts which have arrived while
455 * the handler was running. If all pending interrupts are handled, the
459 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
461 const unsigned int cpu = smp_processor_id();
463 spin_lock(&desc->lock);
465 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
468 * If we're currently running this IRQ, or its disabled,
469 * we shouldn't process the IRQ. Mark it pending, handle
470 * the necessary masking and go out
472 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
474 desc->status |= (IRQ_PENDING | IRQ_MASKED);
475 mask_ack_irq(desc, irq);
479 kstat_cpu(cpu).irqs[irq]++;
481 /* Start handling the irq */
482 desc->chip->ack(irq);
484 /* Mark the IRQ currently in progress.*/
485 desc->status |= IRQ_INPROGRESS;
488 struct irqaction *action = desc->action;
489 irqreturn_t action_ret;
491 if (unlikely(!action)) {
492 desc->chip->mask(irq);
497 * When another irq arrived while we were handling
498 * one, we could have masked the irq.
499 * Renable it, if it was not disabled in meantime.
501 if (unlikely((desc->status &
502 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
503 (IRQ_PENDING | IRQ_MASKED))) {
504 desc->chip->unmask(irq);
505 desc->status &= ~IRQ_MASKED;
508 desc->status &= ~IRQ_PENDING;
509 spin_unlock(&desc->lock);
510 action_ret = handle_IRQ_event(irq, action);
512 note_interrupt(irq, desc, action_ret);
513 spin_lock(&desc->lock);
515 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
517 desc->status &= ~IRQ_INPROGRESS;
519 spin_unlock(&desc->lock);
523 * handle_percpu_IRQ - Per CPU local irq handler
524 * @irq: the interrupt number
525 * @desc: the interrupt description structure for this irq
527 * Per CPU interrupts on SMP machines without locking requirements
530 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
532 irqreturn_t action_ret;
534 kstat_this_cpu.irqs[irq]++;
537 desc->chip->ack(irq);
539 action_ret = handle_IRQ_event(irq, desc->action);
541 note_interrupt(irq, desc, action_ret);
544 desc->chip->eoi(irq);
548 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
551 struct irq_desc *desc;
554 if (irq >= nr_irqs) {
556 "Trying to install type control for IRQ%d\n", irq);
560 desc = irq_to_desc(irq);
563 handle = handle_bad_irq;
564 else if (desc->chip == &no_irq_chip) {
565 printk(KERN_WARNING "Trying to install %sinterrupt handler "
566 "for IRQ%d\n", is_chained ? "chained " : "", irq);
568 * Some ARM implementations install a handler for really dumb
569 * interrupt hardware without setting an irq_chip. This worked
570 * with the ARM no_irq_chip but the check in setup_irq would
571 * prevent us to setup the interrupt at all. Switch it to
572 * dummy_irq_chip for easy transition.
574 desc->chip = &dummy_irq_chip;
577 spin_lock_irqsave(&desc->lock, flags);
580 if (handle == handle_bad_irq) {
581 if (desc->chip != &no_irq_chip)
582 mask_ack_irq(desc, irq);
583 desc->status |= IRQ_DISABLED;
586 desc->handle_irq = handle;
589 if (handle != handle_bad_irq && is_chained) {
590 desc->status &= ~IRQ_DISABLED;
591 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
593 desc->chip->startup(irq);
595 spin_unlock_irqrestore(&desc->lock, flags);
599 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
600 irq_flow_handler_t handle)
602 set_irq_chip(irq, chip);
603 __set_irq_handler(irq, handle, 0, NULL);
607 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
608 irq_flow_handler_t handle, const char *name)
610 set_irq_chip(irq, chip);
611 __set_irq_handler(irq, handle, 0, name);
614 void __init set_irq_noprobe(unsigned int irq)
616 struct irq_desc *desc;
619 if (irq >= nr_irqs) {
620 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
625 desc = irq_to_desc(irq);
627 spin_lock_irqsave(&desc->lock, flags);
628 desc->status |= IRQ_NOPROBE;
629 spin_unlock_irqrestore(&desc->lock, flags);
632 void __init set_irq_probe(unsigned int irq)
634 struct irq_desc *desc;
637 if (irq >= nr_irqs) {
638 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
643 desc = irq_to_desc(irq);
645 spin_lock_irqsave(&desc->lock, flags);
646 desc->status &= ~IRQ_NOPROBE;
647 spin_unlock_irqrestore(&desc->lock, flags);