Merge branch 'sfc-2.6.39' of git://git.kernel.org/pub/scm/linux/kernel/git/bwh/sfc-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / irq / chip.c
blobc9c0601f0615a1eaef8e3c425c61662395987c8e
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 * irq_set_chip - set the irq chip for an irq
23 * @irq: irq number
24 * @chip: pointer to irq chip description structure
26 int irq_set_chip(unsigned int irq, struct irq_chip *chip)
28 unsigned long flags;
29 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
31 if (!desc)
32 return -EINVAL;
34 if (!chip)
35 chip = &no_irq_chip;
37 irq_chip_set_defaults(chip);
38 desc->irq_data.chip = chip;
39 irq_put_desc_unlock(desc, flags);
40 return 0;
42 EXPORT_SYMBOL(irq_set_chip);
44 /**
45 * irq_set_type - set the irq trigger type for an irq
46 * @irq: irq number
47 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
49 int irq_set_irq_type(unsigned int irq, unsigned int type)
51 unsigned long flags;
52 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags);
53 int ret = 0;
55 if (!desc)
56 return -EINVAL;
58 type &= IRQ_TYPE_SENSE_MASK;
59 if (type != IRQ_TYPE_NONE)
60 ret = __irq_set_trigger(desc, irq, type);
61 irq_put_desc_busunlock(desc, flags);
62 return ret;
64 EXPORT_SYMBOL(irq_set_irq_type);
66 /**
67 * irq_set_handler_data - set irq handler data for an irq
68 * @irq: Interrupt number
69 * @data: Pointer to interrupt specific data
71 * Set the hardware irq controller data for an irq
73 int irq_set_handler_data(unsigned int irq, void *data)
75 unsigned long flags;
76 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
78 if (!desc)
79 return -EINVAL;
80 desc->irq_data.handler_data = data;
81 irq_put_desc_unlock(desc, flags);
82 return 0;
84 EXPORT_SYMBOL(irq_set_handler_data);
86 /**
87 * irq_set_msi_desc - set MSI descriptor data for an irq
88 * @irq: Interrupt number
89 * @entry: Pointer to MSI descriptor data
91 * Set the MSI descriptor entry for an irq
93 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
95 unsigned long flags;
96 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
98 if (!desc)
99 return -EINVAL;
100 desc->irq_data.msi_desc = entry;
101 if (entry)
102 entry->irq = irq;
103 irq_put_desc_unlock(desc, flags);
104 return 0;
108 * irq_set_chip_data - set irq chip data for an irq
109 * @irq: Interrupt number
110 * @data: Pointer to chip specific data
112 * Set the hardware irq chip data for an irq
114 int irq_set_chip_data(unsigned int irq, void *data)
116 unsigned long flags;
117 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
119 if (!desc)
120 return -EINVAL;
121 desc->irq_data.chip_data = data;
122 irq_put_desc_unlock(desc, flags);
123 return 0;
125 EXPORT_SYMBOL(irq_set_chip_data);
127 struct irq_data *irq_get_irq_data(unsigned int irq)
129 struct irq_desc *desc = irq_to_desc(irq);
131 return desc ? &desc->irq_data : NULL;
133 EXPORT_SYMBOL_GPL(irq_get_irq_data);
135 static void irq_state_clr_disabled(struct irq_desc *desc)
137 desc->istate &= ~IRQS_DISABLED;
138 irq_compat_clr_disabled(desc);
141 static void irq_state_set_disabled(struct irq_desc *desc)
143 desc->istate |= IRQS_DISABLED;
144 irq_compat_set_disabled(desc);
147 static void irq_state_clr_masked(struct irq_desc *desc)
149 desc->istate &= ~IRQS_MASKED;
150 irq_compat_clr_masked(desc);
153 static void irq_state_set_masked(struct irq_desc *desc)
155 desc->istate |= IRQS_MASKED;
156 irq_compat_set_masked(desc);
159 int irq_startup(struct irq_desc *desc)
161 irq_state_clr_disabled(desc);
162 desc->depth = 0;
164 if (desc->irq_data.chip->irq_startup) {
165 int ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
166 irq_state_clr_masked(desc);
167 return ret;
170 irq_enable(desc);
171 return 0;
174 void irq_shutdown(struct irq_desc *desc)
176 irq_state_set_disabled(desc);
177 desc->depth = 1;
178 if (desc->irq_data.chip->irq_shutdown)
179 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
180 if (desc->irq_data.chip->irq_disable)
181 desc->irq_data.chip->irq_disable(&desc->irq_data);
182 else
183 desc->irq_data.chip->irq_mask(&desc->irq_data);
184 irq_state_set_masked(desc);
187 void irq_enable(struct irq_desc *desc)
189 irq_state_clr_disabled(desc);
190 if (desc->irq_data.chip->irq_enable)
191 desc->irq_data.chip->irq_enable(&desc->irq_data);
192 else
193 desc->irq_data.chip->irq_unmask(&desc->irq_data);
194 irq_state_clr_masked(desc);
197 void irq_disable(struct irq_desc *desc)
199 irq_state_set_disabled(desc);
200 if (desc->irq_data.chip->irq_disable) {
201 desc->irq_data.chip->irq_disable(&desc->irq_data);
202 irq_state_set_masked(desc);
206 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
207 /* Temporary migration helpers */
208 static void compat_irq_mask(struct irq_data *data)
210 data->chip->mask(data->irq);
213 static void compat_irq_unmask(struct irq_data *data)
215 data->chip->unmask(data->irq);
218 static void compat_irq_ack(struct irq_data *data)
220 data->chip->ack(data->irq);
223 static void compat_irq_mask_ack(struct irq_data *data)
225 data->chip->mask_ack(data->irq);
228 static void compat_irq_eoi(struct irq_data *data)
230 data->chip->eoi(data->irq);
233 static void compat_irq_enable(struct irq_data *data)
235 data->chip->enable(data->irq);
238 static void compat_irq_disable(struct irq_data *data)
240 data->chip->disable(data->irq);
243 static void compat_irq_shutdown(struct irq_data *data)
245 data->chip->shutdown(data->irq);
248 static unsigned int compat_irq_startup(struct irq_data *data)
250 return data->chip->startup(data->irq);
253 static int compat_irq_set_affinity(struct irq_data *data,
254 const struct cpumask *dest, bool force)
256 return data->chip->set_affinity(data->irq, dest);
259 static int compat_irq_set_type(struct irq_data *data, unsigned int type)
261 return data->chip->set_type(data->irq, type);
264 static int compat_irq_set_wake(struct irq_data *data, unsigned int on)
266 return data->chip->set_wake(data->irq, on);
269 static int compat_irq_retrigger(struct irq_data *data)
271 return data->chip->retrigger(data->irq);
274 static void compat_bus_lock(struct irq_data *data)
276 data->chip->bus_lock(data->irq);
279 static void compat_bus_sync_unlock(struct irq_data *data)
281 data->chip->bus_sync_unlock(data->irq);
283 #endif
286 * Fixup enable/disable function pointers
288 void irq_chip_set_defaults(struct irq_chip *chip)
290 #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
291 if (chip->enable)
292 chip->irq_enable = compat_irq_enable;
293 if (chip->disable)
294 chip->irq_disable = compat_irq_disable;
295 if (chip->shutdown)
296 chip->irq_shutdown = compat_irq_shutdown;
297 if (chip->startup)
298 chip->irq_startup = compat_irq_startup;
299 if (!chip->end)
300 chip->end = dummy_irq_chip.end;
301 if (chip->bus_lock)
302 chip->irq_bus_lock = compat_bus_lock;
303 if (chip->bus_sync_unlock)
304 chip->irq_bus_sync_unlock = compat_bus_sync_unlock;
305 if (chip->mask)
306 chip->irq_mask = compat_irq_mask;
307 if (chip->unmask)
308 chip->irq_unmask = compat_irq_unmask;
309 if (chip->ack)
310 chip->irq_ack = compat_irq_ack;
311 if (chip->mask_ack)
312 chip->irq_mask_ack = compat_irq_mask_ack;
313 if (chip->eoi)
314 chip->irq_eoi = compat_irq_eoi;
315 if (chip->set_affinity)
316 chip->irq_set_affinity = compat_irq_set_affinity;
317 if (chip->set_type)
318 chip->irq_set_type = compat_irq_set_type;
319 if (chip->set_wake)
320 chip->irq_set_wake = compat_irq_set_wake;
321 if (chip->retrigger)
322 chip->irq_retrigger = compat_irq_retrigger;
323 #endif
326 static inline void mask_ack_irq(struct irq_desc *desc)
328 if (desc->irq_data.chip->irq_mask_ack)
329 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
330 else {
331 desc->irq_data.chip->irq_mask(&desc->irq_data);
332 if (desc->irq_data.chip->irq_ack)
333 desc->irq_data.chip->irq_ack(&desc->irq_data);
335 irq_state_set_masked(desc);
338 void mask_irq(struct irq_desc *desc)
340 if (desc->irq_data.chip->irq_mask) {
341 desc->irq_data.chip->irq_mask(&desc->irq_data);
342 irq_state_set_masked(desc);
346 void unmask_irq(struct irq_desc *desc)
348 if (desc->irq_data.chip->irq_unmask) {
349 desc->irq_data.chip->irq_unmask(&desc->irq_data);
350 irq_state_clr_masked(desc);
355 * handle_nested_irq - Handle a nested irq from a irq thread
356 * @irq: the interrupt number
358 * Handle interrupts which are nested into a threaded interrupt
359 * handler. The handler function is called inside the calling
360 * threads context.
362 void handle_nested_irq(unsigned int irq)
364 struct irq_desc *desc = irq_to_desc(irq);
365 struct irqaction *action;
366 irqreturn_t action_ret;
368 might_sleep();
370 raw_spin_lock_irq(&desc->lock);
372 kstat_incr_irqs_this_cpu(irq, desc);
374 action = desc->action;
375 if (unlikely(!action || (desc->istate & IRQS_DISABLED)))
376 goto out_unlock;
378 irq_compat_set_progress(desc);
379 desc->istate |= IRQS_INPROGRESS;
380 raw_spin_unlock_irq(&desc->lock);
382 action_ret = action->thread_fn(action->irq, action->dev_id);
383 if (!noirqdebug)
384 note_interrupt(irq, desc, action_ret);
386 raw_spin_lock_irq(&desc->lock);
387 desc->istate &= ~IRQS_INPROGRESS;
388 irq_compat_clr_progress(desc);
390 out_unlock:
391 raw_spin_unlock_irq(&desc->lock);
393 EXPORT_SYMBOL_GPL(handle_nested_irq);
395 static bool irq_check_poll(struct irq_desc *desc)
397 if (!(desc->istate & IRQS_POLL_INPROGRESS))
398 return false;
399 return irq_wait_for_poll(desc);
403 * handle_simple_irq - Simple and software-decoded IRQs.
404 * @irq: the interrupt number
405 * @desc: the interrupt description structure for this irq
407 * Simple interrupts are either sent from a demultiplexing interrupt
408 * handler or come from hardware, where no interrupt hardware control
409 * is necessary.
411 * Note: The caller is expected to handle the ack, clear, mask and
412 * unmask issues if necessary.
414 void
415 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
417 raw_spin_lock(&desc->lock);
419 if (unlikely(desc->istate & IRQS_INPROGRESS))
420 if (!irq_check_poll(desc))
421 goto out_unlock;
423 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
424 kstat_incr_irqs_this_cpu(irq, desc);
426 if (unlikely(!desc->action || (desc->istate & IRQS_DISABLED)))
427 goto out_unlock;
429 handle_irq_event(desc);
431 out_unlock:
432 raw_spin_unlock(&desc->lock);
436 * handle_level_irq - Level type irq handler
437 * @irq: the interrupt number
438 * @desc: the interrupt description structure for this irq
440 * Level type interrupts are active as long as the hardware line has
441 * the active level. This may require to mask the interrupt and unmask
442 * it after the associated handler has acknowledged the device, so the
443 * interrupt line is back to inactive.
445 void
446 handle_level_irq(unsigned int irq, struct irq_desc *desc)
448 raw_spin_lock(&desc->lock);
449 mask_ack_irq(desc);
451 if (unlikely(desc->istate & IRQS_INPROGRESS))
452 if (!irq_check_poll(desc))
453 goto out_unlock;
455 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
456 kstat_incr_irqs_this_cpu(irq, desc);
459 * If its disabled or no action available
460 * keep it masked and get out of here
462 if (unlikely(!desc->action || (desc->istate & IRQS_DISABLED)))
463 goto out_unlock;
465 handle_irq_event(desc);
467 if (!(desc->istate & (IRQS_DISABLED | IRQS_ONESHOT)))
468 unmask_irq(desc);
469 out_unlock:
470 raw_spin_unlock(&desc->lock);
472 EXPORT_SYMBOL_GPL(handle_level_irq);
474 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
475 static inline void preflow_handler(struct irq_desc *desc)
477 if (desc->preflow_handler)
478 desc->preflow_handler(&desc->irq_data);
480 #else
481 static inline void preflow_handler(struct irq_desc *desc) { }
482 #endif
485 * handle_fasteoi_irq - irq handler for transparent controllers
486 * @irq: the interrupt number
487 * @desc: the interrupt description structure for this irq
489 * Only a single callback will be issued to the chip: an ->eoi()
490 * call when the interrupt has been serviced. This enables support
491 * for modern forms of interrupt handlers, which handle the flow
492 * details in hardware, transparently.
494 void
495 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
497 raw_spin_lock(&desc->lock);
499 if (unlikely(desc->istate & IRQS_INPROGRESS))
500 if (!irq_check_poll(desc))
501 goto out;
503 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
504 kstat_incr_irqs_this_cpu(irq, desc);
507 * If its disabled or no action available
508 * then mask it and get out of here:
510 if (unlikely(!desc->action || (desc->istate & IRQS_DISABLED))) {
511 irq_compat_set_pending(desc);
512 desc->istate |= IRQS_PENDING;
513 mask_irq(desc);
514 goto out;
517 if (desc->istate & IRQS_ONESHOT)
518 mask_irq(desc);
520 preflow_handler(desc);
521 handle_irq_event(desc);
523 out_eoi:
524 desc->irq_data.chip->irq_eoi(&desc->irq_data);
525 out_unlock:
526 raw_spin_unlock(&desc->lock);
527 return;
528 out:
529 if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
530 goto out_eoi;
531 goto out_unlock;
535 * handle_edge_irq - edge type IRQ handler
536 * @irq: the interrupt number
537 * @desc: the interrupt description structure for this irq
539 * Interrupt occures on the falling and/or rising edge of a hardware
540 * signal. The occurence is latched into the irq controller hardware
541 * and must be acked in order to be reenabled. After the ack another
542 * interrupt can happen on the same source even before the first one
543 * is handled by the associated event handler. If this happens it
544 * might be necessary to disable (mask) the interrupt depending on the
545 * controller hardware. This requires to reenable the interrupt inside
546 * of the loop which handles the interrupts which have arrived while
547 * the handler was running. If all pending interrupts are handled, the
548 * loop is left.
550 void
551 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
553 raw_spin_lock(&desc->lock);
555 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
557 * If we're currently running this IRQ, or its disabled,
558 * we shouldn't process the IRQ. Mark it pending, handle
559 * the necessary masking and go out
561 if (unlikely((desc->istate & (IRQS_DISABLED | IRQS_INPROGRESS) ||
562 !desc->action))) {
563 if (!irq_check_poll(desc)) {
564 irq_compat_set_pending(desc);
565 desc->istate |= IRQS_PENDING;
566 mask_ack_irq(desc);
567 goto out_unlock;
570 kstat_incr_irqs_this_cpu(irq, desc);
572 /* Start handling the irq */
573 desc->irq_data.chip->irq_ack(&desc->irq_data);
575 do {
576 if (unlikely(!desc->action)) {
577 mask_irq(desc);
578 goto out_unlock;
582 * When another irq arrived while we were handling
583 * one, we could have masked the irq.
584 * Renable it, if it was not disabled in meantime.
586 if (unlikely(desc->istate & IRQS_PENDING)) {
587 if (!(desc->istate & IRQS_DISABLED) &&
588 (desc->istate & IRQS_MASKED))
589 unmask_irq(desc);
592 handle_irq_event(desc);
594 } while ((desc->istate & IRQS_PENDING) &&
595 !(desc->istate & IRQS_DISABLED));
597 out_unlock:
598 raw_spin_unlock(&desc->lock);
602 * handle_percpu_irq - Per CPU local irq handler
603 * @irq: the interrupt number
604 * @desc: the interrupt description structure for this irq
606 * Per CPU interrupts on SMP machines without locking requirements
608 void
609 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
611 struct irq_chip *chip = irq_desc_get_chip(desc);
613 kstat_incr_irqs_this_cpu(irq, desc);
615 if (chip->irq_ack)
616 chip->irq_ack(&desc->irq_data);
618 handle_irq_event_percpu(desc, desc->action);
620 if (chip->irq_eoi)
621 chip->irq_eoi(&desc->irq_data);
624 void
625 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
626 const char *name)
628 unsigned long flags;
629 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags);
631 if (!desc)
632 return;
634 if (!handle) {
635 handle = handle_bad_irq;
636 } else {
637 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
638 goto out;
641 /* Uninstall? */
642 if (handle == handle_bad_irq) {
643 if (desc->irq_data.chip != &no_irq_chip)
644 mask_ack_irq(desc);
645 irq_compat_set_disabled(desc);
646 desc->istate |= IRQS_DISABLED;
647 desc->depth = 1;
649 desc->handle_irq = handle;
650 desc->name = name;
652 if (handle != handle_bad_irq && is_chained) {
653 irq_settings_set_noprobe(desc);
654 irq_settings_set_norequest(desc);
655 irq_startup(desc);
657 out:
658 irq_put_desc_busunlock(desc, flags);
660 EXPORT_SYMBOL_GPL(__irq_set_handler);
662 void
663 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
664 irq_flow_handler_t handle, const char *name)
666 irq_set_chip(irq, chip);
667 __irq_set_handler(irq, handle, 0, name);
670 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
672 unsigned long flags;
673 struct irq_desc *desc = irq_get_desc_lock(irq, &flags);
675 if (!desc)
676 return;
677 irq_settings_clr_and_set(desc, clr, set);
679 irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
680 IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
681 if (irq_settings_has_no_balance_set(desc))
682 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
683 if (irq_settings_is_per_cpu(desc))
684 irqd_set(&desc->irq_data, IRQD_PER_CPU);
685 if (irq_settings_can_move_pcntxt(desc))
686 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
688 irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
690 irq_put_desc_unlock(desc, flags);