[POWERPC] PS3: Vuart cleanups
[linux-2.6/libata-dev.git] / kernel / irq / chip.c
blob475e8a71bcdc19772b312461b95ba0b171f1790e
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/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
18 #include "internals.h"
20 /**
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;
27 unsigned long flags;
29 if (irq >= NR_IRQS) {
30 printk(KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
31 WARN_ON(1);
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 desc->affinity = CPU_MASK_ALL;
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 printk(KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
65 WARN_ON(1);
66 return;
69 desc = irq_desc + irq;
70 spin_lock_irqsave(&desc->lock, flags);
71 if (desc->action) {
72 spin_unlock_irqrestore(&desc->lock, flags);
73 printk(KERN_ERR "Destroying IRQ%d without calling free_irq\n",
74 irq);
75 WARN_ON(1);
76 return;
78 desc->msi_desc = NULL;
79 desc->handler_data = NULL;
80 desc->chip_data = NULL;
81 desc->handle_irq = handle_bad_irq;
82 desc->chip = &no_irq_chip;
83 spin_unlock_irqrestore(&desc->lock, flags);
87 /**
88 * set_irq_chip - set the irq chip for an irq
89 * @irq: irq number
90 * @chip: pointer to irq chip description structure
92 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
94 struct irq_desc *desc;
95 unsigned long flags;
97 if (irq >= NR_IRQS) {
98 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
99 WARN_ON(1);
100 return -EINVAL;
103 if (!chip)
104 chip = &no_irq_chip;
106 desc = irq_desc + irq;
107 spin_lock_irqsave(&desc->lock, flags);
108 irq_chip_set_defaults(chip);
109 desc->chip = chip;
110 spin_unlock_irqrestore(&desc->lock, flags);
112 return 0;
114 EXPORT_SYMBOL(set_irq_chip);
117 * set_irq_type - set the irq type for an irq
118 * @irq: irq number
119 * @type: interrupt type - see include/linux/interrupt.h
121 int set_irq_type(unsigned int irq, unsigned int type)
123 struct irq_desc *desc;
124 unsigned long flags;
125 int ret = -ENXIO;
127 if (irq >= NR_IRQS) {
128 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
129 return -ENODEV;
132 desc = irq_desc + irq;
133 if (desc->chip->set_type) {
134 spin_lock_irqsave(&desc->lock, flags);
135 ret = desc->chip->set_type(irq, type);
136 spin_unlock_irqrestore(&desc->lock, flags);
138 return ret;
140 EXPORT_SYMBOL(set_irq_type);
143 * set_irq_data - set irq type data for an irq
144 * @irq: Interrupt number
145 * @data: Pointer to interrupt specific data
147 * Set the hardware irq controller data for an irq
149 int set_irq_data(unsigned int irq, void *data)
151 struct irq_desc *desc;
152 unsigned long flags;
154 if (irq >= NR_IRQS) {
155 printk(KERN_ERR
156 "Trying to install controller data for IRQ%d\n", irq);
157 return -EINVAL;
160 desc = irq_desc + irq;
161 spin_lock_irqsave(&desc->lock, flags);
162 desc->handler_data = data;
163 spin_unlock_irqrestore(&desc->lock, flags);
164 return 0;
166 EXPORT_SYMBOL(set_irq_data);
169 * set_irq_data - set irq type data for an irq
170 * @irq: Interrupt number
171 * @data: Pointer to interrupt specific data
173 * Set the hardware irq controller data for an irq
175 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
177 struct irq_desc *desc;
178 unsigned long flags;
180 if (irq >= NR_IRQS) {
181 printk(KERN_ERR
182 "Trying to install msi data for IRQ%d\n", irq);
183 return -EINVAL;
185 desc = irq_desc + irq;
186 spin_lock_irqsave(&desc->lock, flags);
187 desc->msi_desc = entry;
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)
233 struct irq_desc *desc = irq_desc + irq;
235 if (!(desc->status & IRQ_DELAYED_DISABLE))
236 desc->chip->mask(irq);
240 * default startup function
242 static unsigned int default_startup(unsigned int irq)
244 irq_desc[irq].chip->enable(irq);
246 return 0;
250 * Fixup enable/disable function pointers
252 void irq_chip_set_defaults(struct irq_chip *chip)
254 if (!chip->enable)
255 chip->enable = default_enable;
256 if (!chip->disable)
257 chip->disable = default_disable;
258 if (!chip->startup)
259 chip->startup = default_startup;
260 if (!chip->shutdown)
261 chip->shutdown = chip->disable;
262 if (!chip->name)
263 chip->name = chip->typename;
264 if (!chip->end)
265 chip->end = dummy_irq_chip.end;
268 static inline void mask_ack_irq(struct irq_desc *desc, int irq)
270 if (desc->chip->mask_ack)
271 desc->chip->mask_ack(irq);
272 else {
273 desc->chip->mask(irq);
274 desc->chip->ack(irq);
279 * handle_simple_irq - Simple and software-decoded IRQs.
280 * @irq: the interrupt number
281 * @desc: the interrupt description structure for this irq
283 * Simple interrupts are either sent from a demultiplexing interrupt
284 * handler or come from hardware, where no interrupt hardware control
285 * is necessary.
287 * Note: The caller is expected to handle the ack, clear, mask and
288 * unmask issues if necessary.
290 void fastcall
291 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
293 struct irqaction *action;
294 irqreturn_t action_ret;
295 const unsigned int cpu = smp_processor_id();
297 spin_lock(&desc->lock);
299 if (unlikely(desc->status & IRQ_INPROGRESS))
300 goto out_unlock;
301 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
302 kstat_cpu(cpu).irqs[irq]++;
304 action = desc->action;
305 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
306 goto out_unlock;
308 desc->status |= IRQ_INPROGRESS;
309 spin_unlock(&desc->lock);
311 action_ret = handle_IRQ_event(irq, action);
312 if (!noirqdebug)
313 note_interrupt(irq, desc, action_ret);
315 spin_lock(&desc->lock);
316 desc->status &= ~IRQ_INPROGRESS;
317 out_unlock:
318 spin_unlock(&desc->lock);
322 * handle_level_irq - Level type irq handler
323 * @irq: the interrupt number
324 * @desc: the interrupt description structure for this irq
326 * Level type interrupts are active as long as the hardware line has
327 * the active level. This may require to mask the interrupt and unmask
328 * it after the associated handler has acknowledged the device, so the
329 * interrupt line is back to inactive.
331 void fastcall
332 handle_level_irq(unsigned int irq, struct irq_desc *desc)
334 unsigned int cpu = smp_processor_id();
335 struct irqaction *action;
336 irqreturn_t action_ret;
338 spin_lock(&desc->lock);
339 mask_ack_irq(desc, irq);
341 if (unlikely(desc->status & IRQ_INPROGRESS))
342 goto out_unlock;
343 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
344 kstat_cpu(cpu).irqs[irq]++;
347 * If its disabled or no action available
348 * keep it masked and get out of here
350 action = desc->action;
351 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
352 desc->status |= IRQ_PENDING;
353 goto out_unlock;
356 desc->status |= IRQ_INPROGRESS;
357 desc->status &= ~IRQ_PENDING;
358 spin_unlock(&desc->lock);
360 action_ret = handle_IRQ_event(irq, action);
361 if (!noirqdebug)
362 note_interrupt(irq, desc, action_ret);
364 spin_lock(&desc->lock);
365 desc->status &= ~IRQ_INPROGRESS;
366 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
367 desc->chip->unmask(irq);
368 out_unlock:
369 spin_unlock(&desc->lock);
373 * handle_fasteoi_irq - irq handler for transparent controllers
374 * @irq: the interrupt number
375 * @desc: the interrupt description structure for this irq
377 * Only a single callback will be issued to the chip: an ->eoi()
378 * call when the interrupt has been serviced. This enables support
379 * for modern forms of interrupt handlers, which handle the flow
380 * details in hardware, transparently.
382 void fastcall
383 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
385 unsigned int cpu = smp_processor_id();
386 struct irqaction *action;
387 irqreturn_t action_ret;
389 spin_lock(&desc->lock);
391 if (unlikely(desc->status & IRQ_INPROGRESS))
392 goto out;
394 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
395 kstat_cpu(cpu).irqs[irq]++;
398 * If its disabled or no action available
399 * keep it masked and get out of here
401 action = desc->action;
402 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
403 desc->status |= IRQ_PENDING;
404 goto out;
407 desc->status |= IRQ_INPROGRESS;
408 desc->status &= ~IRQ_PENDING;
409 spin_unlock(&desc->lock);
411 action_ret = handle_IRQ_event(irq, action);
412 if (!noirqdebug)
413 note_interrupt(irq, desc, action_ret);
415 spin_lock(&desc->lock);
416 desc->status &= ~IRQ_INPROGRESS;
417 out:
418 desc->chip->eoi(irq);
420 spin_unlock(&desc->lock);
424 * handle_edge_irq - edge type IRQ handler
425 * @irq: the interrupt number
426 * @desc: the interrupt description structure for this irq
428 * Interrupt occures on the falling and/or rising edge of a hardware
429 * signal. The occurence is latched into the irq controller hardware
430 * and must be acked in order to be reenabled. After the ack another
431 * interrupt can happen on the same source even before the first one
432 * is handled by the assosiacted event handler. If this happens it
433 * might be necessary to disable (mask) the interrupt depending on the
434 * controller hardware. This requires to reenable the interrupt inside
435 * of the loop which handles the interrupts which have arrived while
436 * the handler was running. If all pending interrupts are handled, the
437 * loop is left.
439 void fastcall
440 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
442 const unsigned int cpu = smp_processor_id();
444 spin_lock(&desc->lock);
446 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
449 * If we're currently running this IRQ, or its disabled,
450 * we shouldn't process the IRQ. Mark it pending, handle
451 * the necessary masking and go out
453 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
454 !desc->action)) {
455 desc->status |= (IRQ_PENDING | IRQ_MASKED);
456 mask_ack_irq(desc, irq);
457 goto out_unlock;
460 kstat_cpu(cpu).irqs[irq]++;
462 /* Start handling the irq */
463 desc->chip->ack(irq);
465 /* Mark the IRQ currently in progress.*/
466 desc->status |= IRQ_INPROGRESS;
468 do {
469 struct irqaction *action = desc->action;
470 irqreturn_t action_ret;
472 if (unlikely(!action)) {
473 desc->chip->mask(irq);
474 goto out_unlock;
478 * When another irq arrived while we were handling
479 * one, we could have masked the irq.
480 * Renable it, if it was not disabled in meantime.
482 if (unlikely((desc->status &
483 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
484 (IRQ_PENDING | IRQ_MASKED))) {
485 desc->chip->unmask(irq);
486 desc->status &= ~IRQ_MASKED;
489 desc->status &= ~IRQ_PENDING;
490 spin_unlock(&desc->lock);
491 action_ret = handle_IRQ_event(irq, action);
492 if (!noirqdebug)
493 note_interrupt(irq, desc, action_ret);
494 spin_lock(&desc->lock);
496 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
498 desc->status &= ~IRQ_INPROGRESS;
499 out_unlock:
500 spin_unlock(&desc->lock);
503 #ifdef CONFIG_SMP
505 * handle_percpu_IRQ - Per CPU local irq handler
506 * @irq: the interrupt number
507 * @desc: the interrupt description structure for this irq
509 * Per CPU interrupts on SMP machines without locking requirements
511 void fastcall
512 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
514 irqreturn_t action_ret;
516 kstat_this_cpu.irqs[irq]++;
518 if (desc->chip->ack)
519 desc->chip->ack(irq);
521 action_ret = handle_IRQ_event(irq, desc->action);
522 if (!noirqdebug)
523 note_interrupt(irq, desc, action_ret);
525 if (desc->chip->eoi)
526 desc->chip->eoi(irq);
529 #endif /* CONFIG_SMP */
531 void
532 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
533 const char *name)
535 struct irq_desc *desc;
536 unsigned long flags;
538 if (irq >= NR_IRQS) {
539 printk(KERN_ERR
540 "Trying to install type control for IRQ%d\n", irq);
541 return;
544 desc = irq_desc + irq;
546 if (!handle)
547 handle = handle_bad_irq;
548 else if (desc->chip == &no_irq_chip) {
549 printk(KERN_WARNING "Trying to install %sinterrupt handler "
550 "for IRQ%d\n", is_chained ? "chained " : "", irq);
552 * Some ARM implementations install a handler for really dumb
553 * interrupt hardware without setting an irq_chip. This worked
554 * with the ARM no_irq_chip but the check in setup_irq would
555 * prevent us to setup the interrupt at all. Switch it to
556 * dummy_irq_chip for easy transition.
558 desc->chip = &dummy_irq_chip;
561 spin_lock_irqsave(&desc->lock, flags);
563 /* Uninstall? */
564 if (handle == handle_bad_irq) {
565 if (desc->chip != &no_irq_chip) {
566 desc->chip->mask(irq);
567 desc->chip->ack(irq);
569 desc->status |= IRQ_DISABLED;
570 desc->depth = 1;
572 desc->handle_irq = handle;
573 desc->name = name;
575 if (handle != handle_bad_irq && is_chained) {
576 desc->status &= ~IRQ_DISABLED;
577 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
578 desc->depth = 0;
579 desc->chip->unmask(irq);
581 spin_unlock_irqrestore(&desc->lock, flags);
584 void
585 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
586 irq_flow_handler_t handle)
588 set_irq_chip(irq, chip);
589 __set_irq_handler(irq, handle, 0, NULL);
592 void
593 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
594 irq_flow_handler_t handle, const char *name)
596 set_irq_chip(irq, chip);
597 __set_irq_handler(irq, handle, 0, name);