Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / kernel / irq / chip.c
blob25784d630a12aa96e5489a6575158d9e17c702c2
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, 0);
31 if (!desc)
32 return -EINVAL;
34 if (!chip)
35 chip = &no_irq_chip;
37 desc->irq_data.chip = chip;
38 irq_put_desc_unlock(desc, flags);
40 * For !CONFIG_SPARSE_IRQ make the irq show up in
41 * allocated_irqs. For the CONFIG_SPARSE_IRQ case, it is
42 * already marked, and this call is harmless.
44 irq_reserve_irq(irq);
45 return 0;
47 EXPORT_SYMBOL(irq_set_chip);
49 /**
50 * irq_set_type - set the irq trigger type for an irq
51 * @irq: irq number
52 * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
54 int irq_set_irq_type(unsigned int irq, unsigned int type)
56 unsigned long flags;
57 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
58 int ret = 0;
60 if (!desc)
61 return -EINVAL;
63 type &= IRQ_TYPE_SENSE_MASK;
64 ret = __irq_set_trigger(desc, irq, type);
65 irq_put_desc_busunlock(desc, flags);
66 return ret;
68 EXPORT_SYMBOL(irq_set_irq_type);
70 /**
71 * irq_set_handler_data - set irq handler data for an irq
72 * @irq: Interrupt number
73 * @data: Pointer to interrupt specific data
75 * Set the hardware irq controller data for an irq
77 int irq_set_handler_data(unsigned int irq, void *data)
79 unsigned long flags;
80 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
82 if (!desc)
83 return -EINVAL;
84 desc->irq_data.handler_data = data;
85 irq_put_desc_unlock(desc, flags);
86 return 0;
88 EXPORT_SYMBOL(irq_set_handler_data);
90 /**
91 * irq_set_msi_desc - set MSI descriptor data for an irq
92 * @irq: Interrupt number
93 * @entry: Pointer to MSI descriptor data
95 * Set the MSI descriptor entry for an irq
97 int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
99 unsigned long flags;
100 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
102 if (!desc)
103 return -EINVAL;
104 desc->irq_data.msi_desc = entry;
105 if (entry)
106 entry->irq = irq;
107 irq_put_desc_unlock(desc, flags);
108 return 0;
112 * irq_set_chip_data - set irq chip data for an irq
113 * @irq: Interrupt number
114 * @data: Pointer to chip specific data
116 * Set the hardware irq chip data for an irq
118 int irq_set_chip_data(unsigned int irq, void *data)
120 unsigned long flags;
121 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
123 if (!desc)
124 return -EINVAL;
125 desc->irq_data.chip_data = data;
126 irq_put_desc_unlock(desc, flags);
127 return 0;
129 EXPORT_SYMBOL(irq_set_chip_data);
131 struct irq_data *irq_get_irq_data(unsigned int irq)
133 struct irq_desc *desc = irq_to_desc(irq);
135 return desc ? &desc->irq_data : NULL;
137 EXPORT_SYMBOL_GPL(irq_get_irq_data);
139 static void irq_state_clr_disabled(struct irq_desc *desc)
141 irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
144 static void irq_state_set_disabled(struct irq_desc *desc)
146 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
149 static void irq_state_clr_masked(struct irq_desc *desc)
151 irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
154 static void irq_state_set_masked(struct irq_desc *desc)
156 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
159 int irq_startup(struct irq_desc *desc, bool resend)
161 int ret = 0;
163 irq_state_clr_disabled(desc);
164 desc->depth = 0;
166 if (desc->irq_data.chip->irq_startup) {
167 ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
168 irq_state_clr_masked(desc);
169 } else {
170 irq_enable(desc);
172 if (resend)
173 check_irq_resend(desc, desc->irq_data.irq);
174 return ret;
177 void irq_shutdown(struct irq_desc *desc)
179 irq_state_set_disabled(desc);
180 desc->depth = 1;
181 if (desc->irq_data.chip->irq_shutdown)
182 desc->irq_data.chip->irq_shutdown(&desc->irq_data);
183 else if (desc->irq_data.chip->irq_disable)
184 desc->irq_data.chip->irq_disable(&desc->irq_data);
185 else
186 desc->irq_data.chip->irq_mask(&desc->irq_data);
187 irq_state_set_masked(desc);
190 void irq_enable(struct irq_desc *desc)
192 irq_state_clr_disabled(desc);
193 if (desc->irq_data.chip->irq_enable)
194 desc->irq_data.chip->irq_enable(&desc->irq_data);
195 else
196 desc->irq_data.chip->irq_unmask(&desc->irq_data);
197 irq_state_clr_masked(desc);
200 void irq_disable(struct irq_desc *desc)
202 irq_state_set_disabled(desc);
203 if (desc->irq_data.chip->irq_disable) {
204 desc->irq_data.chip->irq_disable(&desc->irq_data);
205 irq_state_set_masked(desc);
209 void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
211 if (desc->irq_data.chip->irq_enable)
212 desc->irq_data.chip->irq_enable(&desc->irq_data);
213 else
214 desc->irq_data.chip->irq_unmask(&desc->irq_data);
215 cpumask_set_cpu(cpu, desc->percpu_enabled);
218 void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
220 if (desc->irq_data.chip->irq_disable)
221 desc->irq_data.chip->irq_disable(&desc->irq_data);
222 else
223 desc->irq_data.chip->irq_mask(&desc->irq_data);
224 cpumask_clear_cpu(cpu, desc->percpu_enabled);
227 static inline void mask_ack_irq(struct irq_desc *desc)
229 if (desc->irq_data.chip->irq_mask_ack)
230 desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
231 else {
232 desc->irq_data.chip->irq_mask(&desc->irq_data);
233 if (desc->irq_data.chip->irq_ack)
234 desc->irq_data.chip->irq_ack(&desc->irq_data);
236 irq_state_set_masked(desc);
239 void mask_irq(struct irq_desc *desc)
241 if (desc->irq_data.chip->irq_mask) {
242 desc->irq_data.chip->irq_mask(&desc->irq_data);
243 irq_state_set_masked(desc);
247 void unmask_irq(struct irq_desc *desc)
249 if (desc->irq_data.chip->irq_unmask) {
250 desc->irq_data.chip->irq_unmask(&desc->irq_data);
251 irq_state_clr_masked(desc);
256 * handle_nested_irq - Handle a nested irq from a irq thread
257 * @irq: the interrupt number
259 * Handle interrupts which are nested into a threaded interrupt
260 * handler. The handler function is called inside the calling
261 * threads context.
263 void handle_nested_irq(unsigned int irq)
265 struct irq_desc *desc = irq_to_desc(irq);
266 struct irqaction *action;
267 irqreturn_t action_ret;
269 might_sleep();
271 raw_spin_lock_irq(&desc->lock);
273 kstat_incr_irqs_this_cpu(irq, desc);
275 action = desc->action;
276 if (unlikely(!action || irqd_irq_disabled(&desc->irq_data)))
277 goto out_unlock;
279 irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
280 raw_spin_unlock_irq(&desc->lock);
282 action_ret = action->thread_fn(action->irq, action->dev_id);
283 if (!noirqdebug)
284 note_interrupt(irq, desc, action_ret);
286 raw_spin_lock_irq(&desc->lock);
287 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
289 out_unlock:
290 raw_spin_unlock_irq(&desc->lock);
292 EXPORT_SYMBOL_GPL(handle_nested_irq);
294 static bool irq_check_poll(struct irq_desc *desc)
296 if (!(desc->istate & IRQS_POLL_INPROGRESS))
297 return false;
298 return irq_wait_for_poll(desc);
302 * handle_simple_irq - Simple and software-decoded IRQs.
303 * @irq: the interrupt number
304 * @desc: the interrupt description structure for this irq
306 * Simple interrupts are either sent from a demultiplexing interrupt
307 * handler or come from hardware, where no interrupt hardware control
308 * is necessary.
310 * Note: The caller is expected to handle the ack, clear, mask and
311 * unmask issues if necessary.
313 void
314 handle_simple_irq(unsigned int irq, struct irq_desc *desc)
316 raw_spin_lock(&desc->lock);
318 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
319 if (!irq_check_poll(desc))
320 goto out_unlock;
322 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
323 kstat_incr_irqs_this_cpu(irq, desc);
325 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
326 goto out_unlock;
328 handle_irq_event(desc);
330 out_unlock:
331 raw_spin_unlock(&desc->lock);
333 EXPORT_SYMBOL_GPL(handle_simple_irq);
336 * Called unconditionally from handle_level_irq() and only for oneshot
337 * interrupts from handle_fasteoi_irq()
339 static void cond_unmask_irq(struct irq_desc *desc)
342 * We need to unmask in the following cases:
343 * - Standard level irq (IRQF_ONESHOT is not set)
344 * - Oneshot irq which did not wake the thread (caused by a
345 * spurious interrupt or a primary handler handling it
346 * completely).
348 if (!irqd_irq_disabled(&desc->irq_data) &&
349 irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
350 unmask_irq(desc);
354 * handle_level_irq - Level type irq handler
355 * @irq: the interrupt number
356 * @desc: the interrupt description structure for this irq
358 * Level type interrupts are active as long as the hardware line has
359 * the active level. This may require to mask the interrupt and unmask
360 * it after the associated handler has acknowledged the device, so the
361 * interrupt line is back to inactive.
363 void
364 handle_level_irq(unsigned int irq, struct irq_desc *desc)
366 raw_spin_lock(&desc->lock);
367 mask_ack_irq(desc);
369 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
370 if (!irq_check_poll(desc))
371 goto out_unlock;
373 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
374 kstat_incr_irqs_this_cpu(irq, desc);
377 * If its disabled or no action available
378 * keep it masked and get out of here
380 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data)))
381 goto out_unlock;
383 handle_irq_event(desc);
385 cond_unmask_irq(desc);
387 out_unlock:
388 raw_spin_unlock(&desc->lock);
390 EXPORT_SYMBOL_GPL(handle_level_irq);
392 #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
393 static inline void preflow_handler(struct irq_desc *desc)
395 if (desc->preflow_handler)
396 desc->preflow_handler(&desc->irq_data);
398 #else
399 static inline void preflow_handler(struct irq_desc *desc) { }
400 #endif
403 * handle_fasteoi_irq - irq handler for transparent controllers
404 * @irq: the interrupt number
405 * @desc: the interrupt description structure for this irq
407 * Only a single callback will be issued to the chip: an ->eoi()
408 * call when the interrupt has been serviced. This enables support
409 * for modern forms of interrupt handlers, which handle the flow
410 * details in hardware, transparently.
412 void
413 handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
415 raw_spin_lock(&desc->lock);
417 if (unlikely(irqd_irq_inprogress(&desc->irq_data)))
418 if (!irq_check_poll(desc))
419 goto out;
421 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
422 kstat_incr_irqs_this_cpu(irq, desc);
425 * If its disabled or no action available
426 * then mask it and get out of here:
428 if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
429 desc->istate |= IRQS_PENDING;
430 mask_irq(desc);
431 goto out;
434 if (desc->istate & IRQS_ONESHOT)
435 mask_irq(desc);
437 preflow_handler(desc);
438 handle_irq_event(desc);
440 if (desc->istate & IRQS_ONESHOT)
441 cond_unmask_irq(desc);
443 out_eoi:
444 desc->irq_data.chip->irq_eoi(&desc->irq_data);
445 out_unlock:
446 raw_spin_unlock(&desc->lock);
447 return;
448 out:
449 if (!(desc->irq_data.chip->flags & IRQCHIP_EOI_IF_HANDLED))
450 goto out_eoi;
451 goto out_unlock;
455 * handle_edge_irq - edge type IRQ handler
456 * @irq: the interrupt number
457 * @desc: the interrupt description structure for this irq
459 * Interrupt occures on the falling and/or rising edge of a hardware
460 * signal. The occurrence is latched into the irq controller hardware
461 * and must be acked in order to be reenabled. After the ack another
462 * interrupt can happen on the same source even before the first one
463 * is handled by the associated event handler. If this happens it
464 * might be necessary to disable (mask) the interrupt depending on the
465 * controller hardware. This requires to reenable the interrupt inside
466 * of the loop which handles the interrupts which have arrived while
467 * the handler was running. If all pending interrupts are handled, the
468 * loop is left.
470 void
471 handle_edge_irq(unsigned int irq, struct irq_desc *desc)
473 raw_spin_lock(&desc->lock);
475 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
477 * If we're currently running this IRQ, or its disabled,
478 * we shouldn't process the IRQ. Mark it pending, handle
479 * the necessary masking and go out
481 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
482 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
483 if (!irq_check_poll(desc)) {
484 desc->istate |= IRQS_PENDING;
485 mask_ack_irq(desc);
486 goto out_unlock;
489 kstat_incr_irqs_this_cpu(irq, desc);
491 /* Start handling the irq */
492 desc->irq_data.chip->irq_ack(&desc->irq_data);
494 do {
495 if (unlikely(!desc->action)) {
496 mask_irq(desc);
497 goto out_unlock;
501 * When another irq arrived while we were handling
502 * one, we could have masked the irq.
503 * Renable it, if it was not disabled in meantime.
505 if (unlikely(desc->istate & IRQS_PENDING)) {
506 if (!irqd_irq_disabled(&desc->irq_data) &&
507 irqd_irq_masked(&desc->irq_data))
508 unmask_irq(desc);
511 handle_irq_event(desc);
513 } while ((desc->istate & IRQS_PENDING) &&
514 !irqd_irq_disabled(&desc->irq_data));
516 out_unlock:
517 raw_spin_unlock(&desc->lock);
520 #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
522 * handle_edge_eoi_irq - edge eoi type IRQ handler
523 * @irq: the interrupt number
524 * @desc: the interrupt description structure for this irq
526 * Similar as the above handle_edge_irq, but using eoi and w/o the
527 * mask/unmask logic.
529 void handle_edge_eoi_irq(unsigned int irq, struct irq_desc *desc)
531 struct irq_chip *chip = irq_desc_get_chip(desc);
533 raw_spin_lock(&desc->lock);
535 desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
537 * If we're currently running this IRQ, or its disabled,
538 * we shouldn't process the IRQ. Mark it pending, handle
539 * the necessary masking and go out
541 if (unlikely(irqd_irq_disabled(&desc->irq_data) ||
542 irqd_irq_inprogress(&desc->irq_data) || !desc->action)) {
543 if (!irq_check_poll(desc)) {
544 desc->istate |= IRQS_PENDING;
545 goto out_eoi;
548 kstat_incr_irqs_this_cpu(irq, desc);
550 do {
551 if (unlikely(!desc->action))
552 goto out_eoi;
554 handle_irq_event(desc);
556 } while ((desc->istate & IRQS_PENDING) &&
557 !irqd_irq_disabled(&desc->irq_data));
559 out_eoi:
560 chip->irq_eoi(&desc->irq_data);
561 raw_spin_unlock(&desc->lock);
563 #endif
566 * handle_percpu_irq - Per CPU local irq handler
567 * @irq: the interrupt number
568 * @desc: the interrupt description structure for this irq
570 * Per CPU interrupts on SMP machines without locking requirements
572 void
573 handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
575 struct irq_chip *chip = irq_desc_get_chip(desc);
577 kstat_incr_irqs_this_cpu(irq, desc);
579 if (chip->irq_ack)
580 chip->irq_ack(&desc->irq_data);
582 handle_irq_event_percpu(desc, desc->action);
584 if (chip->irq_eoi)
585 chip->irq_eoi(&desc->irq_data);
589 * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
590 * @irq: the interrupt number
591 * @desc: the interrupt description structure for this irq
593 * Per CPU interrupts on SMP machines without locking requirements. Same as
594 * handle_percpu_irq() above but with the following extras:
596 * action->percpu_dev_id is a pointer to percpu variables which
597 * contain the real device id for the cpu on which this handler is
598 * called
600 void handle_percpu_devid_irq(unsigned int irq, struct irq_desc *desc)
602 struct irq_chip *chip = irq_desc_get_chip(desc);
603 struct irqaction *action = desc->action;
604 void *dev_id = __this_cpu_ptr(action->percpu_dev_id);
605 irqreturn_t res;
607 kstat_incr_irqs_this_cpu(irq, desc);
609 if (chip->irq_ack)
610 chip->irq_ack(&desc->irq_data);
612 trace_irq_handler_entry(irq, action);
613 res = action->handler(irq, dev_id);
614 trace_irq_handler_exit(irq, action, res);
616 if (chip->irq_eoi)
617 chip->irq_eoi(&desc->irq_data);
620 void
621 __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
622 const char *name)
624 unsigned long flags;
625 struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
627 if (!desc)
628 return;
630 if (!handle) {
631 handle = handle_bad_irq;
632 } else {
633 if (WARN_ON(desc->irq_data.chip == &no_irq_chip))
634 goto out;
637 /* Uninstall? */
638 if (handle == handle_bad_irq) {
639 if (desc->irq_data.chip != &no_irq_chip)
640 mask_ack_irq(desc);
641 irq_state_set_disabled(desc);
642 desc->depth = 1;
644 desc->handle_irq = handle;
645 desc->name = name;
647 if (handle != handle_bad_irq && is_chained) {
648 irq_settings_set_noprobe(desc);
649 irq_settings_set_norequest(desc);
650 irq_settings_set_nothread(desc);
651 irq_startup(desc, true);
653 out:
654 irq_put_desc_busunlock(desc, flags);
656 EXPORT_SYMBOL_GPL(__irq_set_handler);
658 void
659 irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
660 irq_flow_handler_t handle, const char *name)
662 irq_set_chip(irq, chip);
663 __irq_set_handler(irq, handle, 0, name);
666 void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
668 unsigned long flags;
669 struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
671 if (!desc)
672 return;
673 irq_settings_clr_and_set(desc, clr, set);
675 irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
676 IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
677 if (irq_settings_has_no_balance_set(desc))
678 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
679 if (irq_settings_is_per_cpu(desc))
680 irqd_set(&desc->irq_data, IRQD_PER_CPU);
681 if (irq_settings_can_move_pcntxt(desc))
682 irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
683 if (irq_settings_is_level(desc))
684 irqd_set(&desc->irq_data, IRQD_LEVEL);
686 irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
688 irq_put_desc_unlock(desc, flags);
690 EXPORT_SYMBOL_GPL(irq_modify_status);
693 * irq_cpu_online - Invoke all irq_cpu_online functions.
695 * Iterate through all irqs and invoke the chip.irq_cpu_online()
696 * for each.
698 void irq_cpu_online(void)
700 struct irq_desc *desc;
701 struct irq_chip *chip;
702 unsigned long flags;
703 unsigned int irq;
705 for_each_active_irq(irq) {
706 desc = irq_to_desc(irq);
707 if (!desc)
708 continue;
710 raw_spin_lock_irqsave(&desc->lock, flags);
712 chip = irq_data_get_irq_chip(&desc->irq_data);
713 if (chip && chip->irq_cpu_online &&
714 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
715 !irqd_irq_disabled(&desc->irq_data)))
716 chip->irq_cpu_online(&desc->irq_data);
718 raw_spin_unlock_irqrestore(&desc->lock, flags);
723 * irq_cpu_offline - Invoke all irq_cpu_offline functions.
725 * Iterate through all irqs and invoke the chip.irq_cpu_offline()
726 * for each.
728 void irq_cpu_offline(void)
730 struct irq_desc *desc;
731 struct irq_chip *chip;
732 unsigned long flags;
733 unsigned int irq;
735 for_each_active_irq(irq) {
736 desc = irq_to_desc(irq);
737 if (!desc)
738 continue;
740 raw_spin_lock_irqsave(&desc->lock, flags);
742 chip = irq_data_get_irq_chip(&desc->irq_data);
743 if (chip && chip->irq_cpu_offline &&
744 (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
745 !irqd_irq_disabled(&desc->irq_data)))
746 chip->irq_cpu_offline(&desc->irq_data);
748 raw_spin_unlock_irqrestore(&desc->lock, flags);