Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / irq / chip.c
blobbaa5c4acad83cc4bd9efad28719d054afc960012
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 * set_irq_chip - set the irq chip for an irq
23 * @irq: irq number
24 * @chip: pointer to irq chip description structure
26 int set_irq_chip(unsigned int irq, struct irq_chip *chip)
28 struct irq_desc *desc = irq_to_desc(irq);
29 unsigned long flags;
31 if (!desc) {
32 WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
33 return -EINVAL;
36 if (!chip)
37 chip = &no_irq_chip;
39 raw_spin_lock_irqsave(&desc->lock, flags);
40 irq_chip_set_defaults(chip);
41 desc->irq_data.chip = chip;
42 raw_spin_unlock_irqrestore(&desc->lock, flags);
44 return 0;
46 EXPORT_SYMBOL(set_irq_chip);
48 /**
49 * set_irq_type - set the irq trigger type for an irq
50 * @irq: irq number
51 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
53 int set_irq_type(unsigned int irq, unsigned int type)
55 struct irq_desc *desc = irq_to_desc(irq);
56 unsigned long flags;
57 int ret = -ENXIO;
59 if (!desc) {
60 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
61 return -ENODEV;
64 type &= IRQ_TYPE_SENSE_MASK;
65 if (type == IRQ_TYPE_NONE)
66 return 0;
68 raw_spin_lock_irqsave(&desc->lock, flags);
69 ret = __irq_set_trigger(desc, irq, type);
70 raw_spin_unlock_irqrestore(&desc->lock, flags);
71 return ret;
73 EXPORT_SYMBOL(set_irq_type);
75 /**
76 * set_irq_data - set irq type data for an irq
77 * @irq: Interrupt number
78 * @data: Pointer to interrupt specific data
80 * Set the hardware irq controller data for an irq
82 int set_irq_data(unsigned int irq, void *data)
84 struct irq_desc *desc = irq_to_desc(irq);
85 unsigned long flags;
87 if (!desc) {
88 printk(KERN_ERR
89 "Trying to install controller data for IRQ%d\n", irq);
90 return -EINVAL;
93 raw_spin_lock_irqsave(&desc->lock, flags);
94 desc->irq_data.handler_data = data;
95 raw_spin_unlock_irqrestore(&desc->lock, flags);
96 return 0;
98 EXPORT_SYMBOL(set_irq_data);
101 * set_irq_msi - set MSI descriptor data for an irq
102 * @irq: Interrupt number
103 * @entry: Pointer to MSI descriptor data
105 * Set the MSI descriptor entry for an irq
107 int set_irq_msi(unsigned int irq, struct msi_desc *entry)
109 struct irq_desc *desc = irq_to_desc(irq);
110 unsigned long flags;
112 if (!desc) {
113 printk(KERN_ERR
114 "Trying to install msi data for IRQ%d\n", irq);
115 return -EINVAL;
118 raw_spin_lock_irqsave(&desc->lock, flags);
119 desc->irq_data.msi_desc = entry;
120 if (entry)
121 entry->irq = irq;
122 raw_spin_unlock_irqrestore(&desc->lock, flags);
123 return 0;
127 * set_irq_chip_data - set irq chip data for an irq
128 * @irq: Interrupt number
129 * @data: Pointer to chip specific data
131 * Set the hardware irq chip data for an irq
133 int set_irq_chip_data(unsigned int irq, void *data)
135 struct irq_desc *desc = irq_to_desc(irq);
136 unsigned long flags;
138 if (!desc) {
139 printk(KERN_ERR
140 "Trying to install chip data for IRQ%d\n", irq);
141 return -EINVAL;
144 if (!desc->irq_data.chip) {
145 printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
146 return -EINVAL;
149 raw_spin_lock_irqsave(&desc->lock, flags);
150 desc->irq_data.chip_data = data;
151 raw_spin_unlock_irqrestore(&desc->lock, flags);
153 return 0;
155 EXPORT_SYMBOL(set_irq_chip_data);
157 struct irq_data *irq_get_irq_data(unsigned int irq)
159 struct irq_desc *desc = irq_to_desc(irq);
161 return desc ? &desc->irq_data : NULL;
163 EXPORT_SYMBOL_GPL(irq_get_irq_data);
166 * set_irq_nested_thread - Set/Reset the IRQ_NESTED_THREAD flag of an irq
168 * @irq: Interrupt number
169 * @nest: 0 to clear / 1 to set the IRQ_NESTED_THREAD flag
171 * The IRQ_NESTED_THREAD flag indicates that on
172 * request_threaded_irq() no separate interrupt thread should be
173 * created for the irq as the handler are called nested in the
174 * context of a demultiplexing interrupt handler thread.
176 void set_irq_nested_thread(unsigned int irq, int nest)
178 struct irq_desc *desc = irq_to_desc(irq);
179 unsigned long flags;
181 if (!desc)
182 return;
184 raw_spin_lock_irqsave(&desc->lock, flags);
185 if (nest)
186 desc->status |= IRQ_NESTED_THREAD;
187 else
188 desc->status &= ~IRQ_NESTED_THREAD;
189 raw_spin_unlock_irqrestore(&desc->lock, flags);
191 EXPORT_SYMBOL_GPL(set_irq_nested_thread);
194 * default enable function
196 static void default_enable(struct irq_data *data)
198 struct irq_desc *desc = irq_data_to_desc(data);
200 desc->irq_data.chip->irq_unmask(&desc->irq_data);
201 desc->status &= ~IRQ_MASKED;
205 * default disable function
207 static void default_disable(struct irq_data *data)
212 * default startup function
214 static unsigned int default_startup(struct irq_data *data)
216 struct irq_desc *desc = irq_data_to_desc(data);
218 desc->irq_data.chip->irq_enable(data);
219 return 0;
223 * default shutdown function
225 static void default_shutdown(struct irq_data *data)
227 struct irq_desc *desc = irq_data_to_desc(data);
229 desc->irq_data.chip->irq_mask(&desc->irq_data);
230 desc->status |= IRQ_MASKED;
233 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
234 /* Temporary migration helpers */
235 static void compat_irq_mask(struct irq_data *data)
237 data->chip->mask(data->irq);
240 static void compat_irq_unmask(struct irq_data *data)
242 data->chip->unmask(data->irq);
245 static void compat_irq_ack(struct irq_data *data)
247 data->chip->ack(data->irq);
250 static void compat_irq_mask_ack(struct irq_data *data)
252 data->chip->mask_ack(data->irq);
255 static void compat_irq_eoi(struct irq_data *data)
257 data->chip->eoi(data->irq);
260 static void compat_irq_enable(struct irq_data *data)
262 data->chip->enable(data->irq);
265 static void compat_irq_disable(struct irq_data *data)
267 data->chip->disable(data->irq);
270 static void compat_irq_shutdown(struct irq_data *data)
272 data->chip->shutdown(data->irq);
275 static unsigned int compat_irq_startup(struct irq_data *data)
277 return data->chip->startup(data->irq);
280 static int compat_irq_set_affinity(struct irq_data *data,
281 const struct cpumask *dest, bool force)
283 return data->chip->set_affinity(data->irq, dest);
286 static int compat_irq_set_type(struct irq_data *data, unsigned int type)
288 return data->chip->set_type(data->irq, type);
291 static int compat_irq_set_wake(struct irq_data *data, unsigned int on)
293 return data->chip->set_wake(data->irq, on);
296 static int compat_irq_retrigger(struct irq_data *data)
298 return data->chip->retrigger(data->irq);
301 static void compat_bus_lock(struct irq_data *data)
303 data->chip->bus_lock(data->irq);
306 static void compat_bus_sync_unlock(struct irq_data *data)
308 data->chip->bus_sync_unlock(data->irq);
310 #endif
313 * Fixup enable/disable function pointers
315 void irq_chip_set_defaults(struct irq_chip *chip)
317 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
319 * Compat fixup functions need to be before we set the
320 * defaults for enable/disable/startup/shutdown
322 if (chip->enable)
323 chip->irq_enable = compat_irq_enable;
324 if (chip->disable)
325 chip->irq_disable = compat_irq_disable;
326 if (chip->shutdown)
327 chip->irq_shutdown = compat_irq_shutdown;
328 if (chip->startup)
329 chip->irq_startup = compat_irq_startup;
330 #endif
332 * The real defaults
334 if (!chip->irq_enable)
335 chip->irq_enable = default_enable;
336 if (!chip->irq_disable)
337 chip->irq_disable = default_disable;
338 if (!chip->irq_startup)
339 chip->irq_startup = default_startup;
341 * We use chip->irq_disable, when the user provided its own. When
342 * we have default_disable set for chip->irq_disable, then we need
343 * to use default_shutdown, otherwise the irq line is not
344 * disabled on free_irq():
346 if (!chip->irq_shutdown)
347 chip->irq_shutdown = chip->irq_disable != default_disable ?
348 chip->irq_disable : default_shutdown;
350 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
351 if (!chip->end)
352 chip->end = dummy_irq_chip.end;
355 * Now fix up the remaining compat handlers
357 if (chip->bus_lock)
358 chip->irq_bus_lock = compat_bus_lock;
359 if (chip->bus_sync_unlock)
360 chip->irq_bus_sync_unlock = compat_bus_sync_unlock;
361 if (chip->mask)
362 chip->irq_mask = compat_irq_mask;
363 if (chip->unmask)
364 chip->irq_unmask = compat_irq_unmask;
365 if (chip->ack)
366 chip->irq_ack = compat_irq_ack;
367 if (chip->mask_ack)
368 chip->irq_mask_ack = compat_irq_mask_ack;
369 if (chip->eoi)
370 chip->irq_eoi = compat_irq_eoi;
371 if (chip->set_affinity)
372 chip->irq_set_affinity = compat_irq_set_affinity;
373 if (chip->set_type)
374 chip->irq_set_type = compat_irq_set_type;
375 if (chip->set_wake)
376 chip->irq_set_wake = compat_irq_set_wake;
377 if (chip->retrigger)
378 chip->irq_retrigger = compat_irq_retrigger;
379 #endif
382 static inline void mask_ack_irq(struct irq_desc *desc)
384 if (desc->irq_data.chip->irq_mask_ack)
385 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
386 else {
387 desc->irq_data.chip->irq_mask(&desc->irq_data);
388 if (desc->irq_data.chip->irq_ack)
389 desc->irq_data.chip->irq_ack(&desc->irq_data);
391 desc->status |= IRQ_MASKED;
394 static inline void mask_irq(struct irq_desc *desc)
396 if (desc->irq_data.chip->irq_mask) {
397 desc->irq_data.chip->irq_mask(&desc->irq_data);
398 desc->status |= IRQ_MASKED;
402 static inline void unmask_irq(struct irq_desc *desc)
404 if (desc->irq_data.chip->irq_unmask) {
405 desc->irq_data.chip->irq_unmask(&desc->irq_data);
406 desc->status &= ~IRQ_MASKED;
411 * handle_nested_irq - Handle a nested irq from a irq thread
412 * @irq: the interrupt number
414 * Handle interrupts which are nested into a threaded interrupt
415 * handler. The handler function is called inside the calling
416 * threads context.
418 void handle_nested_irq(unsigned int irq)
420 struct irq_desc *desc = irq_to_desc(irq);
421 struct irqaction *action;
422 irqreturn_t action_ret;
424 might_sleep();
426 raw_spin_lock_irq(&desc->lock);
428 kstat_incr_irqs_this_cpu(irq, desc);
430 action = desc->action;
431 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
432 goto out_unlock;
434 desc->status |= IRQ_INPROGRESS;
435 raw_spin_unlock_irq(&desc->lock);
437 action_ret = action->thread_fn(action->irq, action->dev_id);
438 if (!noirqdebug)
439 note_interrupt(irq, desc, action_ret);
441 raw_spin_lock_irq(&desc->lock);
442 desc->status &= ~IRQ_INPROGRESS;
444 out_unlock:
445 raw_spin_unlock_irq(&desc->lock);
447 EXPORT_SYMBOL_GPL(handle_nested_irq);
450 * handle_simple_irq - Simple and software-decoded IRQs.
451 * @irq: the interrupt number
452 * @desc: the interrupt description structure for this irq
454 * Simple interrupts are either sent from a demultiplexing interrupt
455 * handler or come from hardware, where no interrupt hardware control
456 * is necessary.
458 * Note: The caller is expected to handle the ack, clear, mask and
459 * unmask issues if necessary.
461 void
462 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
464 struct irqaction *action;
465 irqreturn_t action_ret;
467 raw_spin_lock(&desc->lock);
469 if (unlikely(desc->status & IRQ_INPROGRESS))
470 goto out_unlock;
471 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
472 kstat_incr_irqs_this_cpu(irq, desc);
474 action = desc->action;
475 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
476 goto out_unlock;
478 desc->status |= IRQ_INPROGRESS;
479 raw_spin_unlock(&desc->lock);
481 action_ret = handle_IRQ_event(irq, action);
482 if (!noirqdebug)
483 note_interrupt(irq, desc, action_ret);
485 raw_spin_lock(&desc->lock);
486 desc->status &= ~IRQ_INPROGRESS;
487 out_unlock:
488 raw_spin_unlock(&desc->lock);
492 * handle_level_irq - Level type irq handler
493 * @irq: the interrupt number
494 * @desc: the interrupt description structure for this irq
496 * Level type interrupts are active as long as the hardware line has
497 * the active level. This may require to mask the interrupt and unmask
498 * it after the associated handler has acknowledged the device, so the
499 * interrupt line is back to inactive.
501 void
502 handle_level_irq(unsigned int irq, struct irq_desc *desc)
504 struct irqaction *action;
505 irqreturn_t action_ret;
507 raw_spin_lock(&desc->lock);
508 mask_ack_irq(desc);
510 if (unlikely(desc->status & IRQ_INPROGRESS))
511 goto out_unlock;
512 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
513 kstat_incr_irqs_this_cpu(irq, desc);
516 * If its disabled or no action available
517 * keep it masked and get out of here
519 action = desc->action;
520 if (unlikely(!action || (desc->status & IRQ_DISABLED)))
521 goto out_unlock;
523 desc->status |= IRQ_INPROGRESS;
524 raw_spin_unlock(&desc->lock);
526 action_ret = handle_IRQ_event(irq, action);
527 if (!noirqdebug)
528 note_interrupt(irq, desc, action_ret);
530 raw_spin_lock(&desc->lock);
531 desc->status &= ~IRQ_INPROGRESS;
533 if (!(desc->status & (IRQ_DISABLED | IRQ_ONESHOT)))
534 unmask_irq(desc);
535 out_unlock:
536 raw_spin_unlock(&desc->lock);
538 EXPORT_SYMBOL_GPL(handle_level_irq);
541 * handle_fasteoi_irq - irq handler for transparent controllers
542 * @irq: the interrupt number
543 * @desc: the interrupt description structure for this irq
545 * Only a single callback will be issued to the chip: an ->eoi()
546 * call when the interrupt has been serviced. This enables support
547 * for modern forms of interrupt handlers, which handle the flow
548 * details in hardware, transparently.
550 void
551 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
553 struct irqaction *action;
554 irqreturn_t action_ret;
556 raw_spin_lock(&desc->lock);
558 if (unlikely(desc->status & IRQ_INPROGRESS))
559 goto out;
561 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
562 kstat_incr_irqs_this_cpu(irq, desc);
565 * If its disabled or no action available
566 * then mask it and get out of here:
568 action = desc->action;
569 if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
570 desc->status |= IRQ_PENDING;
571 mask_irq(desc);
572 goto out;
575 desc->status |= IRQ_INPROGRESS;
576 desc->status &= ~IRQ_PENDING;
577 raw_spin_unlock(&desc->lock);
579 action_ret = handle_IRQ_event(irq, action);
580 if (!noirqdebug)
581 note_interrupt(irq, desc, action_ret);
583 raw_spin_lock(&desc->lock);
584 desc->status &= ~IRQ_INPROGRESS;
585 out:
586 desc->irq_data.chip->irq_eoi(&desc->irq_data);
588 raw_spin_unlock(&desc->lock);
592 * handle_edge_irq - edge type IRQ handler
593 * @irq: the interrupt number
594 * @desc: the interrupt description structure for this irq
596 * Interrupt occures on the falling and/or rising edge of a hardware
597 * signal. The occurence is latched into the irq controller hardware
598 * and must be acked in order to be reenabled. After the ack another
599 * interrupt can happen on the same source even before the first one
600 * is handled by the associated event handler. If this happens it
601 * might be necessary to disable (mask) the interrupt depending on the
602 * controller hardware. This requires to reenable the interrupt inside
603 * of the loop which handles the interrupts which have arrived while
604 * the handler was running. If all pending interrupts are handled, the
605 * loop is left.
607 void
608 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
610 raw_spin_lock(&desc->lock);
612 desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
615 * If we're currently running this IRQ, or its disabled,
616 * we shouldn't process the IRQ. Mark it pending, handle
617 * the necessary masking and go out
619 if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
620 !desc->action)) {
621 desc->status |= (IRQ_PENDING | IRQ_MASKED);
622 mask_ack_irq(desc);
623 goto out_unlock;
625 kstat_incr_irqs_this_cpu(irq, desc);
627 /* Start handling the irq */
628 desc->irq_data.chip->irq_ack(&desc->irq_data);
630 /* Mark the IRQ currently in progress.*/
631 desc->status |= IRQ_INPROGRESS;
633 do {
634 struct irqaction *action = desc->action;
635 irqreturn_t action_ret;
637 if (unlikely(!action)) {
638 mask_irq(desc);
639 goto out_unlock;
643 * When another irq arrived while we were handling
644 * one, we could have masked the irq.
645 * Renable it, if it was not disabled in meantime.
647 if (unlikely((desc->status &
648 (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
649 (IRQ_PENDING | IRQ_MASKED))) {
650 unmask_irq(desc);
653 desc->status &= ~IRQ_PENDING;
654 raw_spin_unlock(&desc->lock);
655 action_ret = handle_IRQ_event(irq, action);
656 if (!noirqdebug)
657 note_interrupt(irq, desc, action_ret);
658 raw_spin_lock(&desc->lock);
660 } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
662 desc->status &= ~IRQ_INPROGRESS;
663 out_unlock:
664 raw_spin_unlock(&desc->lock);
668 * handle_percpu_irq - Per CPU local irq handler
669 * @irq: the interrupt number
670 * @desc: the interrupt description structure for this irq
672 * Per CPU interrupts on SMP machines without locking requirements
674 void
675 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
677 irqreturn_t action_ret;
679 kstat_incr_irqs_this_cpu(irq, desc);
681 if (desc->irq_data.chip->irq_ack)
682 desc->irq_data.chip->irq_ack(&desc->irq_data);
684 action_ret = handle_IRQ_event(irq, desc->action);
685 if (!noirqdebug)
686 note_interrupt(irq, desc, action_ret);
688 if (desc->irq_data.chip->irq_eoi)
689 desc->irq_data.chip->irq_eoi(&desc->irq_data);
692 void
693 __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
694 const char *name)
696 struct irq_desc *desc = irq_to_desc(irq);
697 unsigned long flags;
699 if (!desc) {
700 printk(KERN_ERR
701 "Trying to install type control for IRQ%d\n", irq);
702 return;
705 if (!handle)
706 handle = handle_bad_irq;
707 else if (desc->irq_data.chip == &no_irq_chip) {
708 printk(KERN_WARNING "Trying to install %sinterrupt handler "
709 "for IRQ%d\n", is_chained ? "chained " : "", irq);
711 * Some ARM implementations install a handler for really dumb
712 * interrupt hardware without setting an irq_chip. This worked
713 * with the ARM no_irq_chip but the check in setup_irq would
714 * prevent us to setup the interrupt at all. Switch it to
715 * dummy_irq_chip for easy transition.
717 desc->irq_data.chip = &dummy_irq_chip;
720 chip_bus_lock(desc);
721 raw_spin_lock_irqsave(&desc->lock, flags);
723 /* Uninstall? */
724 if (handle == handle_bad_irq) {
725 if (desc->irq_data.chip != &no_irq_chip)
726 mask_ack_irq(desc);
727 desc->status |= IRQ_DISABLED;
728 desc->depth = 1;
730 desc->handle_irq = handle;
731 desc->name = name;
733 if (handle != handle_bad_irq && is_chained) {
734 desc->status &= ~IRQ_DISABLED;
735 desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
736 desc->depth = 0;
737 desc->irq_data.chip->irq_startup(&desc->irq_data);
739 raw_spin_unlock_irqrestore(&desc->lock, flags);
740 chip_bus_sync_unlock(desc);
742 EXPORT_SYMBOL_GPL(__set_irq_handler);
744 void
745 set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
746 irq_flow_handler_t handle)
748 set_irq_chip(irq, chip);
749 __set_irq_handler(irq, handle, 0, NULL);
752 void
753 set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
754 irq_flow_handler_t handle, const char *name)
756 set_irq_chip(irq, chip);
757 __set_irq_handler(irq, handle, 0, name);
760 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
762 struct irq_desc *desc = irq_to_desc(irq);
763 unsigned long flags;
765 if (!desc)
766 return;
768 /* Sanitize flags */
769 set &= IRQF_MODIFY_MASK;
770 clr &= IRQF_MODIFY_MASK;
772 raw_spin_lock_irqsave(&desc->lock, flags);
773 desc->status &= ~clr;
774 desc->status |= set;
775 raw_spin_unlock_irqrestore(&desc->lock, flags);