This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / kernel / irq / handle.c
1 /*
2  * linux/kernel/irq/handle.c
3  *
4  * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
5  *
6  * This file contains the core interrupt handling code.
7  */
8
9 #include <linux/irq.h>
10 #include <linux/module.h>
11 #include <linux/random.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel_stat.h>
14
15 #include "internals.h"
16
17 /*
18  * Linux has a controller-independent interrupt architecture.
19  * Every controller has a 'controller-template', that is used
20  * by the main code to do the right thing. Each driver-visible
21  * interrupt source is transparently wired to the apropriate
22  * controller. Thus drivers need not be aware of the
23  * interrupt-controller.
24  *
25  * The code is designed to be easily extended with new/different
26  * interrupt controllers, without having to do assembly magic or
27  * having to touch the generic code.
28  *
29  * Controller mappings for all interrupt sources:
30  */
31 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
32         [0 ... NR_IRQS-1] = {
33                 .handler = &no_irq_type,
34                 .lock = SPIN_LOCK_UNLOCKED
35         }
36 };
37
38 /*
39  * Generic 'no controller' code
40  */
41 static void end_none(unsigned int irq) { }
42 static void enable_none(unsigned int irq) { }
43 static void disable_none(unsigned int irq) { }
44 static void shutdown_none(unsigned int irq) { }
45 static unsigned int startup_none(unsigned int irq) { return 0; }
46
47 static void ack_none(unsigned int irq)
48 {
49         /*
50          * 'what should we do if we get a hw irq event on an illegal vector'.
51          * each architecture has to answer this themself.
52          */
53         ack_bad_irq(irq);
54 }
55
56 struct hw_interrupt_type no_irq_type = {
57         .typename =     "none",
58         .startup =      startup_none,
59         .shutdown =     shutdown_none,
60         .enable =       enable_none,
61         .disable =      disable_none,
62         .ack =          ack_none,
63         .end =          end_none,
64         .set_affinity = NULL
65 };
66
67 /*
68  * Special, empty irq handler:
69  */
70 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
71 {
72         return IRQ_NONE;
73 }
74
75 /*
76  * Exit an interrupt context. Process softirqs if needed and possible:
77  */
78 void irq_exit(void)
79 {
80         preempt_count() -= IRQ_EXIT_OFFSET;
81         if (!in_interrupt() && local_softirq_pending())
82                 do_softirq();
83         preempt_enable_no_resched();
84 }
85
86 /*
87  * Have got an event to handle:
88  */
89 fastcall int handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
90                                 struct irqaction *action)
91 {
92         int ret, retval = 0, status = 0;
93
94         if (!(action->flags & SA_INTERRUPT))
95                 local_irq_enable();
96
97         do {
98                 ret = action->handler(irq, action->dev_id, regs);
99                 if (ret == IRQ_HANDLED)
100                         status |= action->flags;
101                 retval |= ret;
102                 action = action->next;
103         } while (action);
104
105         if (status & SA_SAMPLE_RANDOM)
106                 add_interrupt_randomness(irq);
107         local_irq_disable();
108
109         return retval;
110 }
111
112 /*
113  * do_IRQ handles all normal device IRQ's (the special
114  * SMP cross-CPU interrupts have their own specific
115  * handlers).
116  */
117 fastcall unsigned int __do_IRQ(unsigned int irq, struct pt_regs *regs)
118 {
119         irq_desc_t *desc = irq_desc + irq;
120         struct irqaction * action;
121         unsigned int status;
122
123         kstat_this_cpu.irqs[irq]++;
124         if (desc->status & IRQ_PER_CPU) {
125                 irqreturn_t action_ret;
126
127                 /*
128                  * No locking required for CPU-local interrupts:
129                  */
130                 desc->handler->ack(irq);
131                 action_ret = handle_IRQ_event(irq, regs, desc->action);
132                 if (!noirqdebug)
133                         note_interrupt(irq, desc, action_ret);
134                 desc->handler->end(irq);
135                 return 1;
136         }
137
138         spin_lock(&desc->lock);
139         desc->handler->ack(irq);
140         /*
141          * REPLAY is when Linux resends an IRQ that was dropped earlier
142          * WAITING is used by probe to mark irqs that are being tested
143          */
144         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
145         status |= IRQ_PENDING; /* we _want_ to handle it */
146
147         /*
148          * If the IRQ is disabled for whatever reason, we cannot
149          * use the action we have.
150          */
151         action = NULL;
152         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
153                 action = desc->action;
154                 status &= ~IRQ_PENDING; /* we commit to handling */
155                 status |= IRQ_INPROGRESS; /* we are handling it */
156         }
157         desc->status = status;
158
159         /*
160          * If there is no IRQ handler or it was disabled, exit early.
161          * Since we set PENDING, if another processor is handling
162          * a different instance of this same irq, the other processor
163          * will take care of it.
164          */
165         if (unlikely(!action))
166                 goto out;
167
168         /*
169          * Edge triggered interrupts need to remember
170          * pending events.
171          * This applies to any hw interrupts that allow a second
172          * instance of the same irq to arrive while we are in do_IRQ
173          * or in the handler. But the code here only handles the _second_
174          * instance of the irq, not the third or fourth. So it is mostly
175          * useful for irq hardware that does not mask cleanly in an
176          * SMP environment.
177          */
178         for (;;) {
179                 irqreturn_t action_ret;
180
181                 spin_unlock(&desc->lock);
182
183                 action_ret = handle_IRQ_event(irq, regs, action);
184
185                 spin_lock(&desc->lock);
186                 if (!noirqdebug)
187                         note_interrupt(irq, desc, action_ret);
188                 if (likely(!(desc->status & IRQ_PENDING)))
189                         break;
190                 desc->status &= ~IRQ_PENDING;
191         }
192         desc->status &= ~IRQ_INPROGRESS;
193
194 out:
195         /*
196          * The ->end() handler has to deal with interrupts which got
197          * disabled while the handler was running.
198          */
199         desc->handler->end(irq);
200         spin_unlock(&desc->lock);
201
202         return 1;
203 }
204