Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[linux-2.6/mini2440.git] / kernel / irq / chip.c
blobf63c706d25e15f481f61548dd248d1eaf69702bb
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 desc = irq_to_desc(irq);
31 if (!desc) {
32 WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
33 return;
36 /* Ensure we don't have left over values from a previous use of this 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 cpumask_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 = irq_to_desc(irq);
61 unsigned long flags;
63 if (!desc) {
64 WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
65 return;
68 spin_lock_irqsave(&desc->lock, flags);
69 if (desc->action) {
70 spin_unlock_irqrestore(&desc->lock, flags);
71 WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
72 irq);
73 return;
75 desc->msi_desc = NULL;
76 desc->handler_data = NULL;
77 desc->chip_data = NULL;
78 desc->handle_irq = handle_bad_irq;
79 desc->chip = &no_irq_chip;
80 desc->name = NULL;
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 = irq_to_desc(irq);
93 unsigned long flags;
95 if (!desc) {
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 spin_lock_irqsave(&desc->lock, flags);
104 irq_chip_set_defaults(chip);
105 desc->chip = chip;
106 spin_unlock_irqrestore(&desc->lock, flags);
108 return 0;
110 EXPORT_SYMBOL(set_irq_chip);
113 * set_irq_type - set the irq trigger type for an irq
114 * @irq: irq number
115 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
117 int set_irq_type(unsigned int irq, unsigned int type)
119 struct irq_desc *desc = irq_to_desc(irq);
120 unsigned long flags;
121 int ret = -ENXIO;
123 if (!desc) {
124 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
125 return -ENODEV;
128 type &= IRQ_TYPE_SENSE_MASK;
129 if (type == IRQ_TYPE_NONE)
130 return 0;
132 spin_lock_irqsave(&desc->lock, flags);
133 ret = __irq_set_trigger(desc, irq, type);
134 spin_unlock_irqrestore(&desc->lock, flags);
135 return ret;
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 = irq_to_desc(irq);
149 unsigned long flags;
151 if (!desc) {
152 printk(KERN_ERR
153 "Trying to install controller data for IRQ%d\n", irq);
154 return -EINVAL;
157 spin_lock_irqsave(&desc->lock, flags);
158 desc->handler_data = data;
159 spin_unlock_irqrestore(&desc->lock, flags);
160 return 0;
162 EXPORT_SYMBOL(set_irq_data);
165 * set_irq_data - set irq type data for an irq
166 * @irq: Interrupt number
167 * @entry: Pointer to MSI descriptor data
169 * Set the hardware irq controller data for an irq
171 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
173 struct irq_desc *desc = irq_to_desc(irq);
174 unsigned long flags;
176 if (!desc) {
177 printk(KERN_ERR
178 "Trying to install msi data for IRQ%d\n", irq);
179 return -EINVAL;
182 spin_lock_irqsave(&desc->lock, flags);
183 desc->msi_desc = entry;
184 if (entry)
185 entry->irq = irq;
186 spin_unlock_irqrestore(&desc->lock, flags);
187 return 0;
191 * set_irq_chip_data - set irq chip data for an irq
192 * @irq: Interrupt number
193 * @data: Pointer to chip specific data
195 * Set the hardware irq chip data for an irq
197 int set_irq_chip_data(unsigned int irq, void *data)
199 struct irq_desc *desc = irq_to_desc(irq);
200 unsigned long flags;
202 if (!desc) {
203 printk(KERN_ERR
204 "Trying to install chip data for IRQ%d\n", irq);
205 return -EINVAL;
208 if (!desc->chip) {
209 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
210 return -EINVAL;
213 spin_lock_irqsave(&desc->lock, flags);
214 desc->chip_data = data;
215 spin_unlock_irqrestore(&desc->lock, flags);
217 return 0;
219 EXPORT_SYMBOL(set_irq_chip_data);
222 * default enable function
224 static void default_enable(unsigned int irq)
226 struct irq_desc *desc = irq_to_desc(irq);
228 desc->chip->unmask(irq);
229 desc->status &= ~IRQ_MASKED;
233 * default disable function
235 static void default_disable(unsigned int irq)
240 * default startup function
242 static unsigned int default_startup(unsigned int irq)
244 struct irq_desc *desc = irq_to_desc(irq);
246 desc->chip->enable(irq);
247 return 0;
251 * default shutdown function
253 static void default_shutdown(unsigned int irq)
255 struct irq_desc *desc = irq_to_desc(irq);
257 desc->chip->mask(irq);
258 desc->status |= IRQ_MASKED;
262 * Fixup enable/disable function pointers
264 void irq_chip_set_defaults(struct irq_chip *chip)
266 if (!chip->enable)
267 chip->enable = default_enable;
268 if (!chip->disable)
269 chip->disable = default_disable;
270 if (!chip->startup)
271 chip->startup = default_startup;
273 * We use chip->disable, when the user provided its own. When
274 * we have default_disable set for chip->disable, then we need
275 * to use default_shutdown, otherwise the irq line is not
276 * disabled on free_irq():
278 if (!chip->shutdown)
279 chip->shutdown = chip->disable != default_disable ?
280 chip->disable : default_shutdown;
281 if (!chip->name)
282 chip->name = chip->typename;
283 if (!chip->end)
284 chip->end = dummy_irq_chip.end;
287 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
289 if (desc->chip->mask_ack)
290 desc->chip->mask_ack(irq);
291 else {
292 desc->chip->mask(irq);
293 desc->chip->ack(irq);
298 * handle_simple_irq - Simple and software-decoded IRQs.
299 * @irq: the interrupt number
300 * @desc: the interrupt description structure for this irq
302 * Simple interrupts are either sent from a demultiplexing interrupt
303 * handler or come from hardware, where no interrupt hardware control
304 * is necessary.
306 * Note: The caller is expected to handle the ack, clear, mask and
307 * unmask issues if necessary.
309 void
310 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
312 struct irqaction *action;
313 irqreturn_t action_ret;
315 spin_lock(&desc->lock);
317 if (unlikely(desc->status & IRQ_INPROGRESS))
318 goto out_unlock;
319 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
320 kstat_incr_irqs_this_cpu(irq, desc);
322 action = desc->action;
323 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
324 goto out_unlock;
326 desc->status |= IRQ_INPROGRESS;
327 spin_unlock(&desc->lock);
329 action_ret = handle_IRQ_event(irq, action);
330 if (!noirqdebug)
331 note_interrupt(irq, desc, action_ret);
333 spin_lock(&desc->lock);
334 desc->status &= ~IRQ_INPROGRESS;
335 out_unlock:
336 spin_unlock(&desc->lock);
340 * handle_level_irq - Level type irq handler
341 * @irq: the interrupt number
342 * @desc: the interrupt description structure for this irq
344 * Level type interrupts are active as long as the hardware line has
345 * the active level. This may require to mask the interrupt and unmask
346 * it after the associated handler has acknowledged the device, so the
347 * interrupt line is back to inactive.
349 void
350 handle_level_irq(unsigned int irq, struct irq_desc *desc)
352 struct irqaction *action;
353 irqreturn_t action_ret;
355 spin_lock(&desc->lock);
356 mask_ack_irq(desc, irq);
357 desc = irq_remap_to_desc(irq, desc);
359 if (unlikely(desc->status & IRQ_INPROGRESS))
360 goto out_unlock;
361 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
362 kstat_incr_irqs_this_cpu(irq, 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_incr_irqs_this_cpu(irq, 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);
435 desc = irq_remap_to_desc(irq, desc);
437 spin_unlock(&desc->lock);
441 * handle_edge_irq - edge type IRQ handler
442 * @irq: the interrupt number
443 * @desc: the interrupt description structure for this irq
445 * Interrupt occures on the falling and/or rising edge of a hardware
446 * signal. The occurence is latched into the irq controller hardware
447 * and must be acked in order to be reenabled. After the ack another
448 * interrupt can happen on the same source even before the first one
449 * is handled by the assosiacted event handler. If this happens it
450 * might be necessary to disable (mask) the interrupt depending on the
451 * controller hardware. This requires to reenable the interrupt inside
452 * of the loop which handles the interrupts which have arrived while
453 * the handler was running. If all pending interrupts are handled, the
454 * loop is left.
456 void
457 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
459 spin_lock(&desc->lock);
461 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
464 * If we're currently running this IRQ, or its disabled,
465 * we shouldn't process the IRQ. Mark it pending, handle
466 * the necessary masking and go out
468 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
469 !desc->action)) {
470 desc->status |= (IRQ_PENDING | IRQ_MASKED);
471 mask_ack_irq(desc, irq);
472 desc = irq_remap_to_desc(irq, desc);
473 goto out_unlock;
475 kstat_incr_irqs_this_cpu(irq, desc);
477 /* Start handling the irq */
478 desc->chip->ack(irq);
479 desc = irq_remap_to_desc(irq, desc);
481 /* Mark the IRQ currently in progress.*/
482 desc->status |= IRQ_INPROGRESS;
484 do {
485 struct irqaction *action = desc->action;
486 irqreturn_t action_ret;
488 if (unlikely(!action)) {
489 desc->chip->mask(irq);
490 goto out_unlock;
494 * When another irq arrived while we were handling
495 * one, we could have masked the irq.
496 * Renable it, if it was not disabled in meantime.
498 if (unlikely((desc->status &
499 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
500 (IRQ_PENDING | IRQ_MASKED))) {
501 desc->chip->unmask(irq);
502 desc->status &= ~IRQ_MASKED;
505 desc->status &= ~IRQ_PENDING;
506 spin_unlock(&desc->lock);
507 action_ret = handle_IRQ_event(irq, action);
508 if (!noirqdebug)
509 note_interrupt(irq, desc, action_ret);
510 spin_lock(&desc->lock);
512 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
514 desc->status &= ~IRQ_INPROGRESS;
515 out_unlock:
516 spin_unlock(&desc->lock);
520 * handle_percpu_IRQ - Per CPU local irq handler
521 * @irq: the interrupt number
522 * @desc: the interrupt description structure for this irq
524 * Per CPU interrupts on SMP machines without locking requirements
526 void
527 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
529 irqreturn_t action_ret;
531 kstat_incr_irqs_this_cpu(irq, desc);
533 if (desc->chip->ack)
534 desc->chip->ack(irq);
536 action_ret = handle_IRQ_event(irq, desc->action);
537 if (!noirqdebug)
538 note_interrupt(irq, desc, action_ret);
540 if (desc->chip->eoi) {
541 desc->chip->eoi(irq);
542 desc = irq_remap_to_desc(irq, desc);
546 void
547 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
548 const char *name)
550 struct irq_desc *desc = irq_to_desc(irq);
551 unsigned long flags;
553 if (!desc) {
554 printk(KERN_ERR
555 "Trying to install type control for IRQ%d\n", irq);
556 return;
559 if (!handle)
560 handle = handle_bad_irq;
561 else if (desc->chip == &no_irq_chip) {
562 printk(KERN_WARNING "Trying to install %sinterrupt handler "
563 "for IRQ%d\n", is_chained ? "chained " : "", irq);
565 * Some ARM implementations install a handler for really dumb
566 * interrupt hardware without setting an irq_chip. This worked
567 * with the ARM no_irq_chip but the check in setup_irq would
568 * prevent us to setup the interrupt at all. Switch it to
569 * dummy_irq_chip for easy transition.
571 desc->chip = &dummy_irq_chip;
574 spin_lock_irqsave(&desc->lock, flags);
576 /* Uninstall? */
577 if (handle == handle_bad_irq) {
578 if (desc->chip != &no_irq_chip) {
579 mask_ack_irq(desc, irq);
580 desc = irq_remap_to_desc(irq, desc);
582 desc->status |= IRQ_DISABLED;
583 desc->depth = 1;
585 desc->handle_irq = handle;
586 desc->name = name;
588 if (handle != handle_bad_irq && is_chained) {
589 desc->status &= ~IRQ_DISABLED;
590 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
591 desc->depth = 0;
592 desc->chip->startup(irq);
594 spin_unlock_irqrestore(&desc->lock, flags);
597 void
598 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
599 irq_flow_handler_t handle)
601 set_irq_chip(irq, chip);
602 __set_irq_handler(irq, handle, 0, NULL);
605 void
606 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
607 irq_flow_handler_t handle, const char *name)
609 set_irq_chip(irq, chip);
610 __set_irq_handler(irq, handle, 0, name);
613 void __init set_irq_noprobe(unsigned int irq)
615 struct irq_desc *desc = irq_to_desc(irq);
616 unsigned long flags;
618 if (!desc) {
619 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
620 return;
623 spin_lock_irqsave(&desc->lock, flags);
624 desc->status |= IRQ_NOPROBE;
625 spin_unlock_irqrestore(&desc->lock, flags);
628 void __init set_irq_probe(unsigned int irq)
630 struct irq_desc *desc = irq_to_desc(irq);
631 unsigned long flags;
633 if (!desc) {
634 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
635 return;
638 spin_lock_irqsave(&desc->lock, flags);
639 desc->status &= ~IRQ_NOPROBE;
640 spin_unlock_irqrestore(&desc->lock, flags);