x86, AMD IOMMU: convert driver to generic iommu_num_pages function
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / irq / chip.c
blob3cd441ebf5d2178fcd2d9bdd49573706d6c78b51
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 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_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 type for an irq
115 * @irq: irq number
116 * @type: interrupt type - see include/linux/interrupt.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_desc + irq;
130 if (desc->chip->set_type) {
131 spin_lock_irqsave(&desc->lock, flags);
132 ret = desc->chip->set_type(irq, type);
133 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;
149 unsigned long flags;
151 if (irq >= NR_IRQS) {
152 printk(KERN_ERR
153 "Trying to install controller data for IRQ%d\n", irq);
154 return -EINVAL;
157 desc = irq_desc + irq;
158 spin_lock_irqsave(&desc->lock, flags);
159 desc->handler_data = data;
160 spin_unlock_irqrestore(&desc->lock, flags);
161 return 0;
163 EXPORT_SYMBOL(set_irq_data);
166 * set_irq_data - set irq type data for an irq
167 * @irq: Interrupt number
168 * @entry: Pointer to MSI descriptor data
170 * Set the hardware irq controller data for an irq
172 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
174 struct irq_desc *desc;
175 unsigned long flags;
177 if (irq >= NR_IRQS) {
178 printk(KERN_ERR
179 "Trying to install msi data for IRQ%d\n", irq);
180 return -EINVAL;
182 desc = irq_desc + irq;
183 spin_lock_irqsave(&desc->lock, flags);
184 desc->msi_desc = entry;
185 if (entry)
186 entry->irq = irq;
187 spin_unlock_irqrestore(&desc->lock, flags);
188 return 0;
192 * set_irq_chip_data - set irq chip data for an irq
193 * @irq: Interrupt number
194 * @data: Pointer to chip specific data
196 * Set the hardware irq chip data for an irq
198 int set_irq_chip_data(unsigned int irq, void *data)
200 struct irq_desc *desc = irq_desc + irq;
201 unsigned long flags;
203 if (irq >= NR_IRQS || !desc->chip) {
204 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
205 return -EINVAL;
208 spin_lock_irqsave(&desc->lock, flags);
209 desc->chip_data = data;
210 spin_unlock_irqrestore(&desc->lock, flags);
212 return 0;
214 EXPORT_SYMBOL(set_irq_chip_data);
217 * default enable function
219 static void default_enable(unsigned int irq)
221 struct irq_desc *desc = irq_desc + irq;
223 desc->chip->unmask(irq);
224 desc->status &= ~IRQ_MASKED;
228 * default disable function
230 static void default_disable(unsigned int irq)
235 * default startup function
237 static unsigned int default_startup(unsigned int irq)
239 irq_desc[irq].chip->enable(irq);
241 return 0;
245 * default shutdown function
247 static void default_shutdown(unsigned int irq)
249 struct irq_desc *desc = irq_desc + irq;
251 desc->chip->mask(irq);
252 desc->status |= IRQ_MASKED;
256 * Fixup enable/disable function pointers
258 void irq_chip_set_defaults(struct irq_chip *chip)
260 if (!chip->enable)
261 chip->enable = default_enable;
262 if (!chip->disable)
263 chip->disable = default_disable;
264 if (!chip->startup)
265 chip->startup = default_startup;
267 * We use chip->disable, when the user provided its own. When
268 * we have default_disable set for chip->disable, then we need
269 * to use default_shutdown, otherwise the irq line is not
270 * disabled on free_irq():
272 if (!chip->shutdown)
273 chip->shutdown = chip->disable != default_disable ?
274 chip->disable : default_shutdown;
275 if (!chip->name)
276 chip->name = chip->typename;
277 if (!chip->end)
278 chip->end = dummy_irq_chip.end;
281 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
283 if (desc->chip->mask_ack)
284 desc->chip->mask_ack(irq);
285 else {
286 desc->chip->mask(irq);
287 desc->chip->ack(irq);
292 * handle_simple_irq - Simple and software-decoded IRQs.
293 * @irq: the interrupt number
294 * @desc: the interrupt description structure for this irq
296 * Simple interrupts are either sent from a demultiplexing interrupt
297 * handler or come from hardware, where no interrupt hardware control
298 * is necessary.
300 * Note: The caller is expected to handle the ack, clear, mask and
301 * unmask issues if necessary.
303 void
304 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
306 struct irqaction *action;
307 irqreturn_t action_ret;
308 const unsigned int cpu = smp_processor_id();
310 spin_lock(&desc->lock);
312 if (unlikely(desc->status & IRQ_INPROGRESS))
313 goto out_unlock;
314 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
315 kstat_cpu(cpu).irqs[irq]++;
317 action = desc->action;
318 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
319 goto out_unlock;
321 desc->status |= IRQ_INPROGRESS;
322 spin_unlock(&desc->lock);
324 action_ret = handle_IRQ_event(irq, action);
325 if (!noirqdebug)
326 note_interrupt(irq, desc, action_ret);
328 spin_lock(&desc->lock);
329 desc->status &= ~IRQ_INPROGRESS;
330 out_unlock:
331 spin_unlock(&desc->lock);
335 * handle_level_irq - Level type irq handler
336 * @irq: the interrupt number
337 * @desc: the interrupt description structure for this irq
339 * Level type interrupts are active as long as the hardware line has
340 * the active level. This may require to mask the interrupt and unmask
341 * it after the associated handler has acknowledged the device, so the
342 * interrupt line is back to inactive.
344 void
345 handle_level_irq(unsigned int irq, struct irq_desc *desc)
347 unsigned int cpu = smp_processor_id();
348 struct irqaction *action;
349 irqreturn_t action_ret;
351 spin_lock(&desc->lock);
352 mask_ack_irq(desc, irq);
354 if (unlikely(desc->status & IRQ_INPROGRESS))
355 goto out_unlock;
356 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
357 kstat_cpu(cpu).irqs[irq]++;
360 * If its disabled or no action available
361 * keep it masked and get out of here
363 action = desc->action;
364 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
365 goto out_unlock;
367 desc->status |= IRQ_INPROGRESS;
368 spin_unlock(&desc->lock);
370 action_ret = handle_IRQ_event(irq, action);
371 if (!noirqdebug)
372 note_interrupt(irq, desc, action_ret);
374 spin_lock(&desc->lock);
375 desc->status &= ~IRQ_INPROGRESS;
376 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
377 desc->chip->unmask(irq);
378 out_unlock:
379 spin_unlock(&desc->lock);
383 * handle_fasteoi_irq - irq handler for transparent controllers
384 * @irq: the interrupt number
385 * @desc: the interrupt description structure for this irq
387 * Only a single callback will be issued to the chip: an ->eoi()
388 * call when the interrupt has been serviced. This enables support
389 * for modern forms of interrupt handlers, which handle the flow
390 * details in hardware, transparently.
392 void
393 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
395 unsigned int cpu = smp_processor_id();
396 struct irqaction *action;
397 irqreturn_t action_ret;
399 spin_lock(&desc->lock);
401 if (unlikely(desc->status & IRQ_INPROGRESS))
402 goto out;
404 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
405 kstat_cpu(cpu).irqs[irq]++;
408 * If its disabled or no action available
409 * then mask it and get out of here:
411 action = desc->action;
412 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
413 desc->status |= IRQ_PENDING;
414 if (desc->chip->mask)
415 desc->chip->mask(irq);
416 goto out;
419 desc->status |= IRQ_INPROGRESS;
420 desc->status &= ~IRQ_PENDING;
421 spin_unlock(&desc->lock);
423 action_ret = handle_IRQ_event(irq, action);
424 if (!noirqdebug)
425 note_interrupt(irq, desc, action_ret);
427 spin_lock(&desc->lock);
428 desc->status &= ~IRQ_INPROGRESS;
429 out:
430 desc->chip->eoi(irq);
432 spin_unlock(&desc->lock);
436 * handle_edge_irq - edge type IRQ handler
437 * @irq: the interrupt number
438 * @desc: the interrupt description structure for this irq
440 * Interrupt occures on the falling and/or rising edge of a hardware
441 * signal. The occurence is latched into the irq controller hardware
442 * and must be acked in order to be reenabled. After the ack another
443 * interrupt can happen on the same source even before the first one
444 * is handled by the assosiacted event handler. If this happens it
445 * might be necessary to disable (mask) the interrupt depending on the
446 * controller hardware. This requires to reenable the interrupt inside
447 * of the loop which handles the interrupts which have arrived while
448 * the handler was running. If all pending interrupts are handled, the
449 * loop is left.
451 void
452 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
454 const unsigned int cpu = smp_processor_id();
456 spin_lock(&desc->lock);
458 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
461 * If we're currently running this IRQ, or its disabled,
462 * we shouldn't process the IRQ. Mark it pending, handle
463 * the necessary masking and go out
465 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
466 !desc->action)) {
467 desc->status |= (IRQ_PENDING | IRQ_MASKED);
468 mask_ack_irq(desc, irq);
469 goto out_unlock;
472 kstat_cpu(cpu).irqs[irq]++;
474 /* Start handling the irq */
475 desc->chip->ack(irq);
477 /* Mark the IRQ currently in progress.*/
478 desc->status |= IRQ_INPROGRESS;
480 do {
481 struct irqaction *action = desc->action;
482 irqreturn_t action_ret;
484 if (unlikely(!action)) {
485 desc->chip->mask(irq);
486 goto out_unlock;
490 * When another irq arrived while we were handling
491 * one, we could have masked the irq.
492 * Renable it, if it was not disabled in meantime.
494 if (unlikely((desc->status &
495 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
496 (IRQ_PENDING | IRQ_MASKED))) {
497 desc->chip->unmask(irq);
498 desc->status &= ~IRQ_MASKED;
501 desc->status &= ~IRQ_PENDING;
502 spin_unlock(&desc->lock);
503 action_ret = handle_IRQ_event(irq, action);
504 if (!noirqdebug)
505 note_interrupt(irq, desc, action_ret);
506 spin_lock(&desc->lock);
508 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
510 desc->status &= ~IRQ_INPROGRESS;
511 out_unlock:
512 spin_unlock(&desc->lock);
516 * handle_percpu_IRQ - Per CPU local irq handler
517 * @irq: the interrupt number
518 * @desc: the interrupt description structure for this irq
520 * Per CPU interrupts on SMP machines without locking requirements
522 void
523 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
525 irqreturn_t action_ret;
527 kstat_this_cpu.irqs[irq]++;
529 if (desc->chip->ack)
530 desc->chip->ack(irq);
532 action_ret = handle_IRQ_event(irq, desc->action);
533 if (!noirqdebug)
534 note_interrupt(irq, desc, action_ret);
536 if (desc->chip->eoi)
537 desc->chip->eoi(irq);
540 void
541 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
542 const char *name)
544 struct irq_desc *desc;
545 unsigned long flags;
547 if (irq >= NR_IRQS) {
548 printk(KERN_ERR
549 "Trying to install type control for IRQ%d\n", irq);
550 return;
553 desc = irq_desc + irq;
555 if (!handle)
556 handle = handle_bad_irq;
557 else if (desc->chip == &no_irq_chip) {
558 printk(KERN_WARNING "Trying to install %sinterrupt handler "
559 "for IRQ%d\n", is_chained ? "chained " : "", irq);
561 * Some ARM implementations install a handler for really dumb
562 * interrupt hardware without setting an irq_chip. This worked
563 * with the ARM no_irq_chip but the check in setup_irq would
564 * prevent us to setup the interrupt at all. Switch it to
565 * dummy_irq_chip for easy transition.
567 desc->chip = &dummy_irq_chip;
570 spin_lock_irqsave(&desc->lock, flags);
572 /* Uninstall? */
573 if (handle == handle_bad_irq) {
574 if (desc->chip != &no_irq_chip)
575 mask_ack_irq(desc, irq);
576 desc->status |= IRQ_DISABLED;
577 desc->depth = 1;
579 desc->handle_irq = handle;
580 desc->name = name;
582 if (handle != handle_bad_irq && is_chained) {
583 desc->status &= ~IRQ_DISABLED;
584 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
585 desc->depth = 0;
586 desc->chip->unmask(irq);
588 spin_unlock_irqrestore(&desc->lock, flags);
591 void
592 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
593 irq_flow_handler_t handle)
595 set_irq_chip(irq, chip);
596 __set_irq_handler(irq, handle, 0, NULL);
599 void
600 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
601 irq_flow_handler_t handle, const char *name)
603 set_irq_chip(irq, chip);
604 __set_irq_handler(irq, handle, 0, name);
607 void __init set_irq_noprobe(unsigned int irq)
609 struct irq_desc *desc;
610 unsigned long flags;
612 if (irq >= NR_IRQS) {
613 printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
615 return;
618 desc = irq_desc + irq;
620 spin_lock_irqsave(&desc->lock, flags);
621 desc->status |= IRQ_NOPROBE;
622 spin_unlock_irqrestore(&desc->lock, flags);
625 void __init set_irq_probe(unsigned int irq)
627 struct irq_desc *desc;
628 unsigned long flags;
630 if (irq >= NR_IRQS) {
631 printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
633 return;
636 desc = irq_desc + irq;
638 spin_lock_irqsave(&desc->lock, flags);
639 desc->status &= ~IRQ_NOPROBE;
640 spin_unlock_irqrestore(&desc->lock, flags);