thinkpad-acpi: support MUTE-only ThinkPads
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / irq / chip.c
blob48c58fed6985a9b12fa7409f5a785a46d4683a2d
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_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_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 desc->name = NULL;
82 spin_unlock_irqrestore(&desc->lock, flags);
86 /**
87 * set_irq_chip - set the irq chip for an irq
88 * @irq: irq number
89 * @chip: pointer to irq chip description structure
91 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
93 struct irq_desc *desc;
94 unsigned long flags;
96 if (irq >= NR_IRQS) {
97 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
98 return -EINVAL;
101 if (!chip)
102 chip = &no_irq_chip;
104 desc = irq_desc + irq;
105 spin_lock_irqsave(&desc->lock, flags);
106 irq_chip_set_defaults(chip);
107 desc->chip = chip;
108 spin_unlock_irqrestore(&desc->lock, flags);
110 return 0;
112 EXPORT_SYMBOL(set_irq_chip);
115 * set_irq_type - set the irq type for an irq
116 * @irq: irq number
117 * @type: interrupt type - see include/linux/interrupt.h
119 int set_irq_type(unsigned int irq, unsigned int type)
121 struct irq_desc *desc;
122 unsigned long flags;
123 int ret = -ENXIO;
125 if (irq >= NR_IRQS) {
126 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
127 return -ENODEV;
130 desc = irq_desc + irq;
131 if (desc->chip->set_type) {
132 spin_lock_irqsave(&desc->lock, flags);
133 ret = desc->chip->set_type(irq, type);
134 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_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_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 = irq_desc + irq;
202 unsigned long flags;
204 if (irq >= NR_IRQS || !desc->chip) {
205 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
206 return -EINVAL;
209 spin_lock_irqsave(&desc->lock, flags);
210 desc->chip_data = data;
211 spin_unlock_irqrestore(&desc->lock, flags);
213 return 0;
215 EXPORT_SYMBOL(set_irq_chip_data);
218 * default enable function
220 static void default_enable(unsigned int irq)
222 struct irq_desc *desc = irq_desc + irq;
224 desc->chip->unmask(irq);
225 desc->status &= ~IRQ_MASKED;
229 * default disable function
231 static void default_disable(unsigned int irq)
236 * default startup function
238 static unsigned int default_startup(unsigned int irq)
240 irq_desc[irq].chip->enable(irq);
242 return 0;
246 * default shutdown function
248 static void default_shutdown(unsigned int irq)
250 struct irq_desc *desc = irq_desc + irq;
252 desc->chip->mask(irq);
253 desc->status |= IRQ_MASKED;
257 * Fixup enable/disable function pointers
259 void irq_chip_set_defaults(struct irq_chip *chip)
261 if (!chip->enable)
262 chip->enable = default_enable;
263 if (!chip->disable)
264 chip->disable = default_disable;
265 if (!chip->startup)
266 chip->startup = default_startup;
268 * We use chip->disable, when the user provided its own. When
269 * we have default_disable set for chip->disable, then we need
270 * to use default_shutdown, otherwise the irq line is not
271 * disabled on free_irq():
273 if (!chip->shutdown)
274 chip->shutdown = chip->disable != default_disable ?
275 chip->disable : default_shutdown;
276 if (!chip->name)
277 chip->name = chip->typename;
278 if (!chip->end)
279 chip->end = dummy_irq_chip.end;
282 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
284 if (desc->chip->mask_ack)
285 desc->chip->mask_ack(irq);
286 else {
287 desc->chip->mask(irq);
288 desc->chip->ack(irq);
293 * handle_simple_irq - Simple and software-decoded IRQs.
294 * @irq: the interrupt number
295 * @desc: the interrupt description structure for this irq
297 * Simple interrupts are either sent from a demultiplexing interrupt
298 * handler or come from hardware, where no interrupt hardware control
299 * is necessary.
301 * Note: The caller is expected to handle the ack, clear, mask and
302 * unmask issues if necessary.
304 void
305 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
307 struct irqaction *action;
308 irqreturn_t action_ret;
309 const unsigned int cpu = smp_processor_id();
311 spin_lock(&desc->lock);
313 if (unlikely(desc->status & IRQ_INPROGRESS))
314 goto out_unlock;
315 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
316 kstat_cpu(cpu).irqs[irq]++;
318 action = desc->action;
319 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
320 goto out_unlock;
322 desc->status |= IRQ_INPROGRESS;
323 spin_unlock(&desc->lock);
325 action_ret = handle_IRQ_event(irq, action);
326 if (!noirqdebug)
327 note_interrupt(irq, desc, action_ret);
329 spin_lock(&desc->lock);
330 desc->status &= ~IRQ_INPROGRESS;
331 out_unlock:
332 spin_unlock(&desc->lock);
336 * handle_level_irq - Level type irq handler
337 * @irq: the interrupt number
338 * @desc: the interrupt description structure for this irq
340 * Level type interrupts are active as long as the hardware line has
341 * the active level. This may require to mask the interrupt and unmask
342 * it after the associated handler has acknowledged the device, so the
343 * interrupt line is back to inactive.
345 void
346 handle_level_irq(unsigned int irq, struct irq_desc *desc)
348 unsigned int cpu = smp_processor_id();
349 struct irqaction *action;
350 irqreturn_t action_ret;
352 spin_lock(&desc->lock);
353 mask_ack_irq(desc, irq);
355 if (unlikely(desc->status & IRQ_INPROGRESS))
356 goto out_unlock;
357 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
358 kstat_cpu(cpu).irqs[irq]++;
361 * If its disabled or no action available
362 * keep it masked and get out of here
364 action = desc->action;
365 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
366 goto out_unlock;
368 desc->status |= IRQ_INPROGRESS;
369 spin_unlock(&desc->lock);
371 action_ret = handle_IRQ_event(irq, action);
372 if (!noirqdebug)
373 note_interrupt(irq, desc, action_ret);
375 spin_lock(&desc->lock);
376 desc->status &= ~IRQ_INPROGRESS;
377 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
378 desc->chip->unmask(irq);
379 out_unlock:
380 spin_unlock(&desc->lock);
384 * handle_fasteoi_irq - irq handler for transparent controllers
385 * @irq: the interrupt number
386 * @desc: the interrupt description structure for this irq
388 * Only a single callback will be issued to the chip: an ->eoi()
389 * call when the interrupt has been serviced. This enables support
390 * for modern forms of interrupt handlers, which handle the flow
391 * details in hardware, transparently.
393 void
394 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
396 unsigned int cpu = smp_processor_id();
397 struct irqaction *action;
398 irqreturn_t action_ret;
400 spin_lock(&desc->lock);
402 if (unlikely(desc->status & IRQ_INPROGRESS))
403 goto out;
405 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
406 kstat_cpu(cpu).irqs[irq]++;
409 * If its disabled or no action available
410 * then mask it and get out of here:
412 action = desc->action;
413 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
414 desc->status |= IRQ_PENDING;
415 if (desc->chip->mask)
416 desc->chip->mask(irq);
417 goto out;
420 desc->status |= IRQ_INPROGRESS;
421 desc->status &= ~IRQ_PENDING;
422 spin_unlock(&desc->lock);
424 action_ret = handle_IRQ_event(irq, action);
425 if (!noirqdebug)
426 note_interrupt(irq, desc, action_ret);
428 spin_lock(&desc->lock);
429 desc->status &= ~IRQ_INPROGRESS;
430 out:
431 desc->chip->eoi(irq);
433 spin_unlock(&desc->lock);
437 * handle_edge_irq - edge type IRQ handler
438 * @irq: the interrupt number
439 * @desc: the interrupt description structure for this irq
441 * Interrupt occures on the falling and/or rising edge of a hardware
442 * signal. The occurence is latched into the irq controller hardware
443 * and must be acked in order to be reenabled. After the ack another
444 * interrupt can happen on the same source even before the first one
445 * is handled by the assosiacted event handler. If this happens it
446 * might be necessary to disable (mask) the interrupt depending on the
447 * controller hardware. This requires to reenable the interrupt inside
448 * of the loop which handles the interrupts which have arrived while
449 * the handler was running. If all pending interrupts are handled, the
450 * loop is left.
452 void
453 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
455 const unsigned int cpu = smp_processor_id();
457 spin_lock(&desc->lock);
459 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
462 * If we're currently running this IRQ, or its disabled,
463 * we shouldn't process the IRQ. Mark it pending, handle
464 * the necessary masking and go out
466 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
467 !desc->action)) {
468 desc->status |= (IRQ_PENDING | IRQ_MASKED);
469 mask_ack_irq(desc, irq);
470 goto out_unlock;
473 kstat_cpu(cpu).irqs[irq]++;
475 /* Start handling the irq */
476 desc->chip->ack(irq);
478 /* Mark the IRQ currently in progress.*/
479 desc->status |= IRQ_INPROGRESS;
481 do {
482 struct irqaction *action = desc->action;
483 irqreturn_t action_ret;
485 if (unlikely(!action)) {
486 desc->chip->mask(irq);
487 goto out_unlock;
491 * When another irq arrived while we were handling
492 * one, we could have masked the irq.
493 * Renable it, if it was not disabled in meantime.
495 if (unlikely((desc->status &
496 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
497 (IRQ_PENDING | IRQ_MASKED))) {
498 desc->chip->unmask(irq);
499 desc->status &= ~IRQ_MASKED;
502 desc->status &= ~IRQ_PENDING;
503 spin_unlock(&desc->lock);
504 action_ret = handle_IRQ_event(irq, action);
505 if (!noirqdebug)
506 note_interrupt(irq, desc, action_ret);
507 spin_lock(&desc->lock);
509 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
511 desc->status &= ~IRQ_INPROGRESS;
512 out_unlock:
513 spin_unlock(&desc->lock);
517 * handle_percpu_IRQ - Per CPU local irq handler
518 * @irq: the interrupt number
519 * @desc: the interrupt description structure for this irq
521 * Per CPU interrupts on SMP machines without locking requirements
523 void
524 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
526 irqreturn_t action_ret;
528 kstat_this_cpu.irqs[irq]++;
530 if (desc->chip->ack)
531 desc->chip->ack(irq);
533 action_ret = handle_IRQ_event(irq, desc->action);
534 if (!noirqdebug)
535 note_interrupt(irq, desc, action_ret);
537 if (desc->chip->eoi)
538 desc->chip->eoi(irq);
541 void
542 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
543 const char *name)
545 struct irq_desc *desc;
546 unsigned long flags;
548 if (irq >= NR_IRQS) {
549 printk(KERN_ERR
550 "Trying to install type control for IRQ%d\n", irq);
551 return;
554 desc = irq_desc + irq;
556 if (!handle)
557 handle = handle_bad_irq;
558 else if (desc->chip == &no_irq_chip) {
559 printk(KERN_WARNING "Trying to install %sinterrupt handler "
560 "for IRQ%d\n", is_chained ? "chained " : "", irq);
562 * Some ARM implementations install a handler for really dumb
563 * interrupt hardware without setting an irq_chip. This worked
564 * with the ARM no_irq_chip but the check in setup_irq would
565 * prevent us to setup the interrupt at all. Switch it to
566 * dummy_irq_chip for easy transition.
568 desc->chip = &dummy_irq_chip;
571 spin_lock_irqsave(&desc->lock, flags);
573 /* Uninstall? */
574 if (handle == handle_bad_irq) {
575 if (desc->chip != &no_irq_chip)
576 mask_ack_irq(desc, irq);
577 desc->status |= IRQ_DISABLED;
578 desc->depth = 1;
580 desc->handle_irq = handle;
581 desc->name = name;
583 if (handle != handle_bad_irq && is_chained) {
584 desc->status &= ~IRQ_DISABLED;
585 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
586 desc->depth = 0;
587 desc->chip->unmask(irq);
589 spin_unlock_irqrestore(&desc->lock, flags);
592 void
593 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
594 irq_flow_handler_t handle)
596 set_irq_chip(irq, chip);
597 __set_irq_handler(irq, handle, 0, NULL);
600 void
601 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
602 irq_flow_handler_t handle, const char *name)
604 set_irq_chip(irq, chip);
605 __set_irq_handler(irq, handle, 0, name);
608 void __init set_irq_noprobe(unsigned int irq)
610 struct irq_desc *desc;
611 unsigned long flags;
613 if (irq >= NR_IRQS) {
614 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
616 return;
619 desc = irq_desc + irq;
621 spin_lock_irqsave(&desc->lock, flags);
622 desc->status |= IRQ_NOPROBE;
623 spin_unlock_irqrestore(&desc->lock, flags);
626 void __init set_irq_probe(unsigned int irq)
628 struct irq_desc *desc;
629 unsigned long flags;
631 if (irq >= NR_IRQS) {
632 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
634 return;
637 desc = irq_desc + irq;
639 spin_lock_irqsave(&desc->lock, flags);
640 desc->status &= ~IRQ_NOPROBE;
641 spin_unlock_irqrestore(&desc->lock, flags);