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->handler_data = NULL;
43 desc->chip_data = NULL;
46 desc->irqs_unhandled = 0;
48 desc->affinity = CPU_MASK_ALL;
50 spin_unlock_irqrestore(&desc->lock, flags);
54 * dynamic_irq_cleanup - cleanup a dynamically allocated irq
55 * @irq: irq number to initialize
57 void dynamic_irq_cleanup(unsigned int irq)
59 struct irq_desc *desc;
63 printk(KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
68 desc = irq_desc + irq;
69 spin_lock_irqsave(&desc->lock, flags);
70 desc->handle_irq = handle_bad_irq;
71 desc->chip = &no_irq_chip;
72 spin_unlock_irqrestore(&desc->lock, flags);
77 * set_irq_chip - set the irq chip for an irq
79 * @chip: pointer to irq chip description structure
81 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
83 struct irq_desc *desc;
87 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
95 desc = irq_desc + irq;
96 spin_lock_irqsave(&desc->lock, flags);
97 irq_chip_set_defaults(chip);
99 spin_unlock_irqrestore(&desc->lock, flags);
103 EXPORT_SYMBOL(set_irq_chip);
106 * set_irq_type - set the irq type for an irq
108 * @type: interrupt type - see include/linux/interrupt.h
110 int set_irq_type(unsigned int irq, unsigned int type)
112 struct irq_desc *desc;
116 if (irq >= NR_IRQS) {
117 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
121 desc = irq_desc + irq;
122 if (desc->chip->set_type) {
123 spin_lock_irqsave(&desc->lock, flags);
124 ret = desc->chip->set_type(irq, type);
125 spin_unlock_irqrestore(&desc->lock, flags);
129 EXPORT_SYMBOL(set_irq_type);
132 * set_irq_data - set irq type data for an irq
133 * @irq: Interrupt number
134 * @data: Pointer to interrupt specific data
136 * Set the hardware irq controller data for an irq
138 int set_irq_data(unsigned int irq, void *data)
140 struct irq_desc *desc;
143 if (irq >= NR_IRQS) {
145 "Trying to install controller data for IRQ%d\n", irq);
149 desc = irq_desc + irq;
150 spin_lock_irqsave(&desc->lock, flags);
151 desc->handler_data = data;
152 spin_unlock_irqrestore(&desc->lock, flags);
155 EXPORT_SYMBOL(set_irq_data);
158 * set_irq_chip_data - set irq chip data for an irq
159 * @irq: Interrupt number
160 * @data: Pointer to chip specific data
162 * Set the hardware irq chip data for an irq
164 int set_irq_chip_data(unsigned int irq, void *data)
166 struct irq_desc *desc = irq_desc + irq;
169 if (irq >= NR_IRQS || !desc->chip) {
170 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
174 spin_lock_irqsave(&desc->lock, flags);
175 desc->chip_data = data;
176 spin_unlock_irqrestore(&desc->lock, flags);
180 EXPORT_SYMBOL(set_irq_chip_data);
183 * default enable function
185 static void default_enable(unsigned int irq)
187 struct irq_desc *desc = irq_desc + irq;
189 desc->chip->unmask(irq);
190 desc->status &= ~IRQ_MASKED;
194 * default disable function
196 static void default_disable(unsigned int irq)
198 struct irq_desc *desc = irq_desc + irq;
200 if (!(desc->status & IRQ_DELAYED_DISABLE))
201 desc->chip->mask(irq);
205 * default startup function
207 static unsigned int default_startup(unsigned int irq)
209 irq_desc[irq].chip->enable(irq);
215 * Fixup enable/disable function pointers
217 void irq_chip_set_defaults(struct irq_chip *chip)
220 chip->enable = default_enable;
222 chip->disable = default_disable;
224 chip->startup = default_startup;
226 chip->shutdown = chip->disable;
228 chip->name = chip->typename;
231 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
233 if (desc->chip->mask_ack)
234 desc->chip->mask_ack(irq);
236 desc->chip->mask(irq);
237 desc->chip->ack(irq);
242 * handle_simple_irq - Simple and software-decoded IRQs.
243 * @irq: the interrupt number
244 * @desc: the interrupt description structure for this irq
245 * @regs: pointer to a register structure
247 * Simple interrupts are either sent from a demultiplexing interrupt
248 * handler or come from hardware, where no interrupt hardware control
251 * Note: The caller is expected to handle the ack, clear, mask and
252 * unmask issues if necessary.
255 handle_simple_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
257 struct irqaction *action;
258 irqreturn_t action_ret;
259 const unsigned int cpu = smp_processor_id();
261 spin_lock(&desc->lock);
263 if (unlikely(desc->status & IRQ_INPROGRESS))
265 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
266 kstat_cpu(cpu).irqs[irq]++;
268 action = desc->action;
269 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
272 desc->status |= IRQ_INPROGRESS;
273 spin_unlock(&desc->lock);
275 action_ret = handle_IRQ_event(irq, regs, action);
277 note_interrupt(irq, desc, action_ret, regs);
279 spin_lock(&desc->lock);
280 desc->status &= ~IRQ_INPROGRESS;
282 spin_unlock(&desc->lock);
286 * handle_level_irq - Level type irq handler
287 * @irq: the interrupt number
288 * @desc: the interrupt description structure for this irq
289 * @regs: pointer to a register structure
291 * Level type interrupts are active as long as the hardware line has
292 * the active level. This may require to mask the interrupt and unmask
293 * it after the associated handler has acknowledged the device, so the
294 * interrupt line is back to inactive.
297 handle_level_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
299 unsigned int cpu = smp_processor_id();
300 struct irqaction *action;
301 irqreturn_t action_ret;
303 spin_lock(&desc->lock);
304 mask_ack_irq(desc, irq);
306 if (unlikely(desc->status & IRQ_INPROGRESS))
308 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
309 kstat_cpu(cpu).irqs[irq]++;
312 * If its disabled or no action available
313 * keep it masked and get out of here
315 action = desc->action;
316 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
317 desc->status |= IRQ_PENDING;
321 desc->status |= IRQ_INPROGRESS;
322 desc->status &= ~IRQ_PENDING;
323 spin_unlock(&desc->lock);
325 action_ret = handle_IRQ_event(irq, regs, action);
327 note_interrupt(irq, desc, action_ret, regs);
329 spin_lock(&desc->lock);
330 desc->status &= ~IRQ_INPROGRESS;
331 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
332 desc->chip->unmask(irq);
334 spin_unlock(&desc->lock);
338 * handle_fasteoi_irq - irq handler for transparent controllers
339 * @irq: the interrupt number
340 * @desc: the interrupt description structure for this irq
341 * @regs: pointer to a register structure
343 * Only a single callback will be issued to the chip: an ->eoi()
344 * call when the interrupt has been serviced. This enables support
345 * for modern forms of interrupt handlers, which handle the flow
346 * details in hardware, transparently.
349 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc,
350 struct pt_regs *regs)
352 unsigned int cpu = smp_processor_id();
353 struct irqaction *action;
354 irqreturn_t action_ret;
356 spin_lock(&desc->lock);
358 if (unlikely(desc->status & IRQ_INPROGRESS))
361 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
362 kstat_cpu(cpu).irqs[irq]++;
365 * If its disabled or no action available
366 * keep it masked and get out of here
368 action = desc->action;
369 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
370 desc->status |= IRQ_PENDING;
374 desc->status |= IRQ_INPROGRESS;
375 desc->status &= ~IRQ_PENDING;
376 spin_unlock(&desc->lock);
378 action_ret = handle_IRQ_event(irq, regs, action);
380 note_interrupt(irq, desc, action_ret, regs);
382 spin_lock(&desc->lock);
383 desc->status &= ~IRQ_INPROGRESS;
385 desc->chip->eoi(irq);
387 spin_unlock(&desc->lock);
391 * handle_edge_irq - edge type IRQ handler
392 * @irq: the interrupt number
393 * @desc: the interrupt description structure for this irq
394 * @regs: pointer to a register structure
396 * Interrupt occures on the falling and/or rising edge of a hardware
397 * signal. The occurence is latched into the irq controller hardware
398 * and must be acked in order to be reenabled. After the ack another
399 * interrupt can happen on the same source even before the first one
400 * is handled by the assosiacted event handler. If this happens it
401 * might be necessary to disable (mask) the interrupt depending on the
402 * controller hardware. This requires to reenable the interrupt inside
403 * of the loop which handles the interrupts which have arrived while
404 * the handler was running. If all pending interrupts are handled, the
408 handle_edge_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
410 const unsigned int cpu = smp_processor_id();
412 spin_lock(&desc->lock);
414 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
417 * If we're currently running this IRQ, or its disabled,
418 * we shouldn't process the IRQ. Mark it pending, handle
419 * the necessary masking and go out
421 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
423 desc->status |= (IRQ_PENDING | IRQ_MASKED);
424 mask_ack_irq(desc, irq);
428 kstat_cpu(cpu).irqs[irq]++;
430 /* Start handling the irq */
431 desc->chip->ack(irq);
433 /* Mark the IRQ currently in progress.*/
434 desc->status |= IRQ_INPROGRESS;
437 struct irqaction *action = desc->action;
438 irqreturn_t action_ret;
440 if (unlikely(!action)) {
441 desc->chip->mask(irq);
446 * When another irq arrived while we were handling
447 * one, we could have masked the irq.
448 * Renable it, if it was not disabled in meantime.
450 if (unlikely((desc->status &
451 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
452 (IRQ_PENDING | IRQ_MASKED))) {
453 desc->chip->unmask(irq);
454 desc->status &= ~IRQ_MASKED;
457 desc->status &= ~IRQ_PENDING;
458 spin_unlock(&desc->lock);
459 action_ret = handle_IRQ_event(irq, regs, action);
461 note_interrupt(irq, desc, action_ret, regs);
462 spin_lock(&desc->lock);
464 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
466 desc->status &= ~IRQ_INPROGRESS;
468 spin_unlock(&desc->lock);
473 * handle_percpu_IRQ - Per CPU local irq handler
474 * @irq: the interrupt number
475 * @desc: the interrupt description structure for this irq
476 * @regs: pointer to a register structure
478 * Per CPU interrupts on SMP machines without locking requirements
481 handle_percpu_irq(unsigned int irq, struct irq_desc *desc, struct pt_regs *regs)
483 irqreturn_t action_ret;
485 kstat_this_cpu.irqs[irq]++;
488 desc->chip->ack(irq);
490 action_ret = handle_IRQ_event(irq, regs, desc->action);
492 note_interrupt(irq, desc, action_ret, regs);
495 desc->chip->eoi(irq);
498 #endif /* CONFIG_SMP */
501 __set_irq_handler(unsigned int irq,
502 void fastcall (*handle)(unsigned int, irq_desc_t *,
506 struct irq_desc *desc;
509 if (irq >= NR_IRQS) {
511 "Trying to install type control for IRQ%d\n", irq);
515 desc = irq_desc + irq;
518 handle = handle_bad_irq;
520 if (desc->chip == &no_irq_chip) {
521 printk(KERN_WARNING "Trying to install %sinterrupt handler "
522 "for IRQ%d\n", is_chained ? "chained " : " ", irq);
524 * Some ARM implementations install a handler for really dumb
525 * interrupt hardware without setting an irq_chip. This worked
526 * with the ARM no_irq_chip but the check in setup_irq would
527 * prevent us to setup the interrupt at all. Switch it to
528 * dummy_irq_chip for easy transition.
530 desc->chip = &dummy_irq_chip;
533 spin_lock_irqsave(&desc->lock, flags);
536 if (handle == handle_bad_irq) {
537 if (desc->chip != &no_irq_chip) {
538 desc->chip->mask(irq);
539 desc->chip->ack(irq);
541 desc->status |= IRQ_DISABLED;
544 desc->handle_irq = handle;
546 if (handle != handle_bad_irq && is_chained) {
547 desc->status &= ~IRQ_DISABLED;
548 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
550 desc->chip->unmask(irq);
552 spin_unlock_irqrestore(&desc->lock, flags);
556 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
557 void fastcall (*handle)(unsigned int,
561 set_irq_chip(irq, chip);
562 __set_irq_handler(irq, handle, 0);
566 * Get a descriptive string for the highlevel handler, for
567 * /proc/interrupts output:
570 handle_irq_name(void fastcall (*handle)(unsigned int, struct irq_desc *,
573 if (handle == handle_level_irq)
575 if (handle == handle_fasteoi_irq)
577 if (handle == handle_edge_irq)
579 if (handle == handle_simple_irq)
582 if (handle == handle_percpu_irq)
585 if (handle == handle_bad_irq)