ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / kernel / irq.c
1 /* $Id: irq.c,v 1.8 2003/07/04 08:27:52 starvik Exp $
2  *
3  *      linux/arch/cris/kernel/irq.c
4  *
5  *      Copyright (c) 2000,2001 Axis Communications AB
6  *
7  *      Authors: Bjorn Wesen (bjornw@axis.com)
8  *
9  * This file contains the code used by various IRQ handling routines:
10  * asking for different IRQ's should be done through these routines
11  * instead of just grabbing them. Thus setups with different IRQ numbers
12  * shouldn't result in any weird surprises, and installing new handlers
13  * should be easier.
14  *
15  * Notice Linux/CRIS: these routines do not care about SMP
16  *
17  */
18
19 /*
20  * IRQ's are in fact implemented a bit like signal handlers for the kernel.
21  * Naturally it's not a 1:1 relation, but there are similarities.
22  */
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/ptrace.h>
27
28 #include <linux/kernel_stat.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/ioport.h>
32 #include <linux/interrupt.h>
33 #include <linux/timex.h>
34 #include <linux/slab.h>
35 #include <linux/random.h>
36 #include <linux/init.h>
37 #include <linux/seq_file.h>
38 #include <linux/errno.h>
39
40 #include <asm/io.h>
41 #include <asm/bitops.h>
42
43 /* Defined in arch specific irq.c */
44 extern void arch_setup_irq(int irq);
45 extern void arch_free_irq(int irq);
46
47 void
48 disable_irq(unsigned int irq_nr)
49 {
50         unsigned long flags;
51         
52         local_save_flags(flags);
53         local_irq_disable();
54         mask_irq(irq_nr);
55         local_irq_restore(flags);
56 }
57
58 void
59 enable_irq(unsigned int irq_nr)
60 {
61         unsigned long flags;
62         local_save_flags(flags);
63         local_irq_disable();
64         unmask_irq(irq_nr);
65         local_irq_restore(flags);
66 }
67
68 unsigned long
69 probe_irq_on()
70 {
71         return 0;
72 }
73
74 EXPORT_SYMBOL(probe_irq_on);
75
76 int
77 probe_irq_off(unsigned long x)
78 {
79         return 0;
80 }
81
82 EXPORT_SYMBOL(probe_irq_off);
83
84 /*
85  * Initial irq handlers.
86  */
87
88 static struct irqaction *irq_action[NR_IRQS];
89
90 int show_interrupts(struct seq_file *p, void *v)
91 {
92         int i = *(loff_t *) v;
93         struct irqaction * action;
94         unsigned long flags;
95
96         if (i < NR_IRQS) {
97                 local_irq_save(flags);
98                 action = irq_action[i];
99                 if (!action) 
100                         goto skip;
101                 seq_printf(p, "%2d: %10u %c %s",
102                         i, kstat_cpu(0).irqs[i],
103                         (action->flags & SA_INTERRUPT) ? '+' : ' ',
104                         action->name);
105                 for (action = action->next; action; action = action->next) {
106                         seq_printf(p, ",%s %s",
107                                 (action->flags & SA_INTERRUPT) ? " +" : "",
108                                 action->name);
109                 }
110                 seq_putc(p, '\n');
111 skip:
112                 local_irq_restore(flags);
113         }
114         return 0;
115 }
116
117 /* called by the assembler IRQ entry functions defined in irq.h
118  * to dispatch the interrupts to registred handlers
119  * interrupts are disabled upon entry - depending on if the
120  * interrupt was registred with SA_INTERRUPT or not, interrupts
121  * are re-enabled or not.
122  */
123
124 asmlinkage void do_IRQ(int irq, struct pt_regs * regs)
125 {
126         struct irqaction *action;
127         int do_random, cpu;
128         int retval = 0;
129
130         cpu = smp_processor_id();
131         irq_enter();
132         kstat_cpu(cpu).irqs[irq]++;
133
134         action = irq_action[irq];
135         if (action) {
136                 if (!(action->flags & SA_INTERRUPT))
137                         local_irq_enable();
138                 action = irq_action[irq];
139                 do_random = 0;
140                 do {
141                         do_random |= action->flags;
142                         retval |= action->handler(irq, action->dev_id, regs);
143                         action = action->next;
144                 } while (action);
145
146                 if (retval != 1) {
147                         if (retval) {
148                                 printk("irq event %d: bogus retval mask %x\n",
149                                         irq, retval);
150                         } else {
151                                 printk("irq %d: nobody cared\n", irq);
152                         }
153                 }
154
155                 if (do_random & SA_SAMPLE_RANDOM)
156                         add_interrupt_randomness(irq);
157                 local_irq_disable();
158         }
159         irq_exit();
160
161         if (softirq_pending(cpu))
162                 do_softirq();
163
164         /* unmasking and bottom half handling is done magically for us. */
165 }
166
167 /* this function links in a handler into the chain of handlers for the
168    given irq, and if the irq has never been registred, the appropriate
169    handler is entered into the interrupt vector
170 */
171
172 int setup_irq(int irq, struct irqaction * new)
173 {
174         int shared = 0;
175         struct irqaction *old, **p;
176         unsigned long flags;
177
178         p = irq_action + irq;
179         if ((old = *p) != NULL) {
180                 /* Can't share interrupts unless both agree to */
181                 if (!(old->flags & new->flags & SA_SHIRQ))
182                         return -EBUSY;
183
184                 /* Can't share interrupts unless both are same type */
185                 if ((old->flags ^ new->flags) & SA_INTERRUPT)
186                         return -EBUSY;
187
188                 /* add new interrupt at end of irq queue */
189                 do {
190                         p = &old->next;
191                         old = *p;
192                 } while (old);
193                 shared = 1;
194         }
195
196         if (new->flags & SA_SAMPLE_RANDOM)
197                 rand_initialize_irq(irq);
198
199         local_save_flags(flags);
200         local_irq_disable();
201         *p = new;
202
203         if (!shared) {
204                 /* if the irq wasn't registred before, enter it into the vector table
205                    and unmask it physically 
206                 */
207                 arch_setup_irq(irq);
208                 unmask_irq(irq);
209         }
210         
211         local_irq_restore(flags);
212         return 0;
213 }
214
215 /* this function is called by a driver to register an irq handler
216    Valid flags:
217    SA_INTERRUPT -> it's a fast interrupt, handler called with irq disabled and
218                    no signal checking etc is performed upon exit
219    SA_SHIRQ -> the interrupt can be shared between different handlers, the handler
220                 is required to check if the irq was "aimed" at it explicitely
221    SA_RANDOM -> the interrupt will add to the random generators entropy
222 */
223
224 int request_irq(unsigned int irq, 
225                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
226                 unsigned long irqflags, 
227                 const char * devname,
228                 void *dev_id)
229 {
230         int retval;
231         struct irqaction * action;
232
233         /* interrupts 0 and 1 are hardware breakpoint and NMI and we can't support
234            these yet. interrupt 15 is the multiple irq, it's special. */
235
236         if(irq < 2 || irq == 15 || irq >= NR_IRQS)
237                 return -EINVAL;
238
239         if(!handler)
240                 return -EINVAL;
241
242         /* allocate and fill in a handler structure and setup the irq */
243
244         action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
245         if (!action)
246                 return -ENOMEM;
247
248         action->handler = handler;
249         action->flags = irqflags;
250         action->mask = 0;
251         action->name = devname;
252         action->next = NULL;
253         action->dev_id = dev_id;
254
255         retval = setup_irq(irq, action);
256
257         if (retval)
258                 kfree(action);
259         return retval;
260 }
261
262 EXPORT_SYMBOL(request_irq);
263                 
264 void free_irq(unsigned int irq, void *dev_id)
265 {
266         struct irqaction * action, **p;
267         unsigned long flags;
268
269         if (irq >= NR_IRQS) {
270                 printk("Trying to free IRQ%d\n",irq);
271                 return;
272         }
273         for (p = irq + irq_action; (action = *p) != NULL; p = &action->next) {
274                 if (action->dev_id != dev_id)
275                         continue;
276
277                 /* Found it - now free it */
278                 local_save_flags(flags);
279                 local_irq_disable();
280                 *p = action->next;
281                 if (!irq_action[irq]) {
282                         mask_irq(irq);
283                         arch_free_irq(irq);
284                 }
285                 local_irq_restore(flags);
286                 kfree(action);
287                 return;
288         }
289         printk("Trying to free free IRQ%d\n",irq);
290 }
291
292 EXPORT_SYMBOL(free_irq);
293
294 void weird_irq(void)
295 {
296         local_irq_disable();
297         printk("weird irq\n");
298         while(1);
299 }
300
301 #if defined(CONFIG_PROC_FS) && defined(CONFIG_SYSCTL)
302 /* Used by other archs to show/control IRQ steering during SMP */
303 void __init
304 init_irq_proc(void)
305 {
306 }
307 #endif