x86: move kstat_irqs from kstat to irq_desc
[linux-2.6/mini2440.git] / kernel / irq / chip.c
blob2aa3d4b2fce80fb5b0e293fbdae1cbb938320f74
1 /*
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
8 * based architectures.
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"
21 /**
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;
28 unsigned long flags;
30 if (irq >= nr_irqs) {
31 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
32 return;
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;
41 desc->depth = 1;
42 desc->msi_desc = NULL;
43 desc->handler_data = NULL;
44 desc->chip_data = NULL;
45 desc->action = NULL;
46 desc->irq_count = 0;
47 desc->irqs_unhandled = 0;
48 #ifdef CONFIG_SMP
49 cpus_setall(desc->affinity);
50 #endif
51 spin_unlock_irqrestore(&desc->lock, flags);
54 /**
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;
61 unsigned long flags;
63 if (irq >= nr_irqs) {
64 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
65 return;
68 desc = irq_to_desc(irq);
69 spin_lock_irqsave(&desc->lock, flags);
70 if (desc->action) {
71 spin_unlock_irqrestore(&desc->lock, flags);
72 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
73 irq);
74 return;
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);
85 /**
86 * set_irq_chip - set the irq chip for an irq
87 * @irq: irq number
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;
93 unsigned long flags;
95 if (irq >= nr_irqs) {
96 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
97 return -EINVAL;
100 if (!chip)
101 chip = &no_irq_chip;
103 desc = irq_to_desc(irq);
104 spin_lock_irqsave(&desc->lock, flags);
105 irq_chip_set_defaults(chip);
106 desc->chip = chip;
107 spin_unlock_irqrestore(&desc->lock, flags);
109 return 0;
111 EXPORT_SYMBOL(set_irq_chip);
114 * set_irq_type - set the irq trigger type for an irq
115 * @irq: irq number
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;
121 unsigned long flags;
122 int ret = -ENXIO;
124 if (irq >= nr_irqs) {
125 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
126 return -ENODEV;
129 desc = irq_to_desc(irq);
130 if (type == IRQ_TYPE_NONE)
131 return 0;
133 spin_lock_irqsave(&desc->lock, flags);
134 ret = __irq_set_trigger(desc, irq, flags);
135 spin_unlock_irqrestore(&desc->lock, flags);
136 return ret;
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;
150 unsigned long flags;
152 if (irq >= nr_irqs) {
153 printk(KERN_ERR
154 "Trying to install controller data for IRQ%d\n", irq);
155 return -EINVAL;
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);
162 return 0;
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;
176 unsigned long flags;
178 if (irq >= nr_irqs) {
179 printk(KERN_ERR
180 "Trying to install msi data for IRQ%d\n", irq);
181 return -EINVAL;
183 desc = irq_to_desc(irq);
184 spin_lock_irqsave(&desc->lock, flags);
185 desc->msi_desc = entry;
186 if (entry)
187 entry->irq = irq;
188 spin_unlock_irqrestore(&desc->lock, flags);
189 return 0;
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;
202 unsigned long flags;
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);
207 return -EINVAL;
210 spin_lock_irqsave(&desc->lock, flags);
211 desc->chip_data = data;
212 spin_unlock_irqrestore(&desc->lock, flags);
214 return 0;
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);
247 return 0;
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)
267 if (!chip->enable)
268 chip->enable = default_enable;
269 if (!chip->disable)
270 chip->disable = default_disable;
271 if (!chip->startup)
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():
279 if (!chip->shutdown)
280 chip->shutdown = chip->disable != default_disable ?
281 chip->disable : default_shutdown;
282 if (!chip->name)
283 chip->name = chip->typename;
284 if (!chip->end)
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);
292 else {
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
305 * is necessary.
307 * Note: The caller is expected to handle the ack, clear, mask and
308 * unmask issues if necessary.
310 void
311 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
313 struct irqaction *action;
314 irqreturn_t action_ret;
316 spin_lock(&desc->lock);
318 if (unlikely(desc->status & IRQ_INPROGRESS))
319 goto out_unlock;
320 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
321 kstat_irqs_this_cpu(desc)++;
323 action = desc->action;
324 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
325 goto out_unlock;
327 desc->status |= IRQ_INPROGRESS;
328 spin_unlock(&desc->lock);
330 action_ret = handle_IRQ_event(irq, action);
331 if (!noirqdebug)
332 note_interrupt(irq, desc, action_ret);
334 spin_lock(&desc->lock);
335 desc->status &= ~IRQ_INPROGRESS;
336 out_unlock:
337 spin_unlock(&desc->lock);
341 * handle_level_irq - Level type irq handler
342 * @irq: the interrupt number
343 * @desc: the interrupt description structure for this irq
345 * Level type interrupts are active as long as the hardware line has
346 * the active level. This may require to mask the interrupt and unmask
347 * it after the associated handler has acknowledged the device, so the
348 * interrupt line is back to inactive.
350 void
351 handle_level_irq(unsigned int irq, struct irq_desc *desc)
353 struct irqaction *action;
354 irqreturn_t action_ret;
356 spin_lock(&desc->lock);
357 mask_ack_irq(desc, irq);
359 if (unlikely(desc->status & IRQ_INPROGRESS))
360 goto out_unlock;
361 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
362 kstat_irqs_this_cpu(desc)++;
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 goto out_unlock;
372 desc->status |= IRQ_INPROGRESS;
373 spin_unlock(&desc->lock);
375 action_ret = handle_IRQ_event(irq, action);
376 if (!noirqdebug)
377 note_interrupt(irq, desc, action_ret);
379 spin_lock(&desc->lock);
380 desc->status &= ~IRQ_INPROGRESS;
381 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
382 desc->chip->unmask(irq);
383 out_unlock:
384 spin_unlock(&desc->lock);
388 * handle_fasteoi_irq - irq handler for transparent controllers
389 * @irq: the interrupt number
390 * @desc: the interrupt description structure for this irq
392 * Only a single callback will be issued to the chip: an ->eoi()
393 * call when the interrupt has been serviced. This enables support
394 * for modern forms of interrupt handlers, which handle the flow
395 * details in hardware, transparently.
397 void
398 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
400 struct irqaction *action;
401 irqreturn_t action_ret;
403 spin_lock(&desc->lock);
405 if (unlikely(desc->status & IRQ_INPROGRESS))
406 goto out;
408 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
409 kstat_irqs_this_cpu(desc)++;
412 * If its disabled or no action available
413 * then mask it and get out of here:
415 action = desc->action;
416 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
417 desc->status |= IRQ_PENDING;
418 if (desc->chip->mask)
419 desc->chip->mask(irq);
420 goto out;
423 desc->status |= IRQ_INPROGRESS;
424 desc->status &= ~IRQ_PENDING;
425 spin_unlock(&desc->lock);
427 action_ret = handle_IRQ_event(irq, action);
428 if (!noirqdebug)
429 note_interrupt(irq, desc, action_ret);
431 spin_lock(&desc->lock);
432 desc->status &= ~IRQ_INPROGRESS;
433 out:
434 desc->chip->eoi(irq);
436 spin_unlock(&desc->lock);
440 * handle_edge_irq - edge type IRQ handler
441 * @irq: the interrupt number
442 * @desc: the interrupt description structure for this irq
444 * Interrupt occures on the falling and/or rising edge of a hardware
445 * signal. The occurence is latched into the irq controller hardware
446 * and must be acked in order to be reenabled. After the ack another
447 * interrupt can happen on the same source even before the first one
448 * is handled by the assosiacted event handler. If this happens it
449 * might be necessary to disable (mask) the interrupt depending on the
450 * controller hardware. This requires to reenable the interrupt inside
451 * of the loop which handles the interrupts which have arrived while
452 * the handler was running. If all pending interrupts are handled, the
453 * loop is left.
455 void
456 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
458 spin_lock(&desc->lock);
460 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
463 * If we're currently running this IRQ, or its disabled,
464 * we shouldn't process the IRQ. Mark it pending, handle
465 * the necessary masking and go out
467 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
468 !desc->action)) {
469 desc->status |= (IRQ_PENDING | IRQ_MASKED);
470 mask_ack_irq(desc, irq);
471 goto out_unlock;
474 kstat_irqs_this_cpu(desc)++;
476 /* Start handling the irq */
477 desc->chip->ack(irq);
479 /* Mark the IRQ currently in progress.*/
480 desc->status |= IRQ_INPROGRESS;
482 do {
483 struct irqaction *action = desc->action;
484 irqreturn_t action_ret;
486 if (unlikely(!action)) {
487 desc->chip->mask(irq);
488 goto out_unlock;
492 * When another irq arrived while we were handling
493 * one, we could have masked the irq.
494 * Renable it, if it was not disabled in meantime.
496 if (unlikely((desc->status &
497 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
498 (IRQ_PENDING | IRQ_MASKED))) {
499 desc->chip->unmask(irq);
500 desc->status &= ~IRQ_MASKED;
503 desc->status &= ~IRQ_PENDING;
504 spin_unlock(&desc->lock);
505 action_ret = handle_IRQ_event(irq, action);
506 if (!noirqdebug)
507 note_interrupt(irq, desc, action_ret);
508 spin_lock(&desc->lock);
510 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
512 desc->status &= ~IRQ_INPROGRESS;
513 out_unlock:
514 spin_unlock(&desc->lock);
518 * handle_percpu_IRQ - Per CPU local irq handler
519 * @irq: the interrupt number
520 * @desc: the interrupt description structure for this irq
522 * Per CPU interrupts on SMP machines without locking requirements
524 void
525 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
527 irqreturn_t action_ret;
529 kstat_irqs_this_cpu(desc)++;
531 if (desc->chip->ack)
532 desc->chip->ack(irq);
534 action_ret = handle_IRQ_event(irq, desc->action);
535 if (!noirqdebug)
536 note_interrupt(irq, desc, action_ret);
538 if (desc->chip->eoi)
539 desc->chip->eoi(irq);
542 void
543 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
544 const char *name)
546 struct irq_desc *desc;
547 unsigned long flags;
549 if (irq >= nr_irqs) {
550 printk(KERN_ERR
551 "Trying to install type control for IRQ%d\n", irq);
552 return;
555 desc = irq_to_desc(irq);
557 if (!handle)
558 handle = handle_bad_irq;
559 else if (desc->chip == &no_irq_chip) {
560 printk(KERN_WARNING "Trying to install %sinterrupt handler "
561 "for IRQ%d\n", is_chained ? "chained " : "", irq);
563 * Some ARM implementations install a handler for really dumb
564 * interrupt hardware without setting an irq_chip. This worked
565 * with the ARM no_irq_chip but the check in setup_irq would
566 * prevent us to setup the interrupt at all. Switch it to
567 * dummy_irq_chip for easy transition.
569 desc->chip = &dummy_irq_chip;
572 spin_lock_irqsave(&desc->lock, flags);
574 /* Uninstall? */
575 if (handle == handle_bad_irq) {
576 if (desc->chip != &no_irq_chip)
577 mask_ack_irq(desc, irq);
578 desc->status |= IRQ_DISABLED;
579 desc->depth = 1;
581 desc->handle_irq = handle;
582 desc->name = name;
584 if (handle != handle_bad_irq && is_chained) {
585 desc->status &= ~IRQ_DISABLED;
586 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
587 desc->depth = 0;
588 desc->chip->startup(irq);
590 spin_unlock_irqrestore(&desc->lock, flags);
593 void
594 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
595 irq_flow_handler_t handle)
597 set_irq_chip(irq, chip);
598 __set_irq_handler(irq, handle, 0, NULL);
601 void
602 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
603 irq_flow_handler_t handle, const char *name)
605 set_irq_chip(irq, chip);
606 __set_irq_handler(irq, handle, 0, name);
609 void __init set_irq_noprobe(unsigned int irq)
611 struct irq_desc *desc;
612 unsigned long flags;
614 if (irq >= nr_irqs) {
615 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
617 return;
620 desc = irq_to_desc(irq);
622 spin_lock_irqsave(&desc->lock, flags);
623 desc->status |= IRQ_NOPROBE;
624 spin_unlock_irqrestore(&desc->lock, flags);
627 void __init set_irq_probe(unsigned int irq)
629 struct irq_desc *desc;
630 unsigned long flags;
632 if (irq >= nr_irqs) {
633 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
635 return;
638 desc = irq_to_desc(irq);
640 spin_lock_irqsave(&desc->lock, flags);
641 desc->status &= ~IRQ_NOPROBE;
642 spin_unlock_irqrestore(&desc->lock, flags);