patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / sparc / kernel / irq.c
1 /*  $Id: irq.c,v 1.114 2001/12/11 04:55:51 davem Exp $
2  *  arch/sparc/kernel/irq.c:  Interrupt request handling routines. On the
3  *                            Sparc the IRQ's are basically 'cast in stone'
4  *                            and you are supposed to probe the prom's device
5  *                            node trees to find out who's got which IRQ.
6  *
7  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
8  *  Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
9  *  Copyright (C) 1995,2002 Pete A. Zaitcev (zaitcev@yahoo.com)
10  *  Copyright (C) 1996 Dave Redman (djhr@tadpole.co.uk)
11  *  Copyright (C) 1998-2000 Anton Blanchard (anton@samba.org)
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/ptrace.h>
18 #include <linux/errno.h>
19 #include <linux/linkage.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/random.h>
26 #include <linux/init.h>
27 #include <linux/smp.h>
28 #include <linux/smp_lock.h>
29 #include <linux/delay.h>
30 #include <linux/threads.h>
31 #include <linux/spinlock.h>
32 #include <linux/seq_file.h>
33
34 #include <asm/ptrace.h>
35 #include <asm/processor.h>
36 #include <asm/system.h>
37 #include <asm/psr.h>
38 #include <asm/smp.h>
39 #include <asm/vaddrs.h>
40 #include <asm/timer.h>
41 #include <asm/openprom.h>
42 #include <asm/oplib.h>
43 #include <asm/traps.h>
44 #include <asm/irq.h>
45 #include <asm/io.h>
46 #include <asm/pgalloc.h>
47 #include <asm/pgtable.h>
48 #include <asm/hardirq.h>
49 #include <asm/pcic.h>
50 #include <asm/cacheflush.h>
51
52 #ifdef CONFIG_SMP
53 #define SMP_NOP2 "nop; nop;\n\t"
54 #define SMP_NOP3 "nop; nop; nop;\n\t"
55 #else
56 #define SMP_NOP2
57 #define SMP_NOP3
58 #endif /* SMP */
59 unsigned long __local_irq_save(void)
60 {
61         unsigned long retval;
62         unsigned long tmp;
63
64         __asm__ __volatile__(
65                 "rd     %%psr, %0\n\t"
66                 SMP_NOP3        /* Sun4m + Cypress + SMP bug */
67                 "or     %0, %2, %1\n\t"
68                 "wr     %1, 0, %%psr\n\t"
69                 "nop; nop; nop\n"
70                 : "=&r" (retval), "=r" (tmp)
71                 : "i" (PSR_PIL)
72                 : "memory");
73
74         return retval;
75 }
76
77 void local_irq_enable(void)
78 {
79         unsigned long tmp;
80
81         __asm__ __volatile__(
82                 "rd     %%psr, %0\n\t"
83                 SMP_NOP3        /* Sun4m + Cypress + SMP bug */
84                 "andn   %0, %1, %0\n\t"
85                 "wr     %0, 0, %%psr\n\t"
86                 "nop; nop; nop\n"
87                 : "=&r" (tmp)
88                 : "i" (PSR_PIL)
89                 : "memory");
90 }
91
92 void local_irq_restore(unsigned long old_psr)
93 {
94         unsigned long tmp;
95
96         __asm__ __volatile__(
97                 "rd     %%psr, %0\n\t"
98                 "and    %2, %1, %2\n\t"
99                 SMP_NOP2        /* Sun4m + Cypress + SMP bug */
100                 "andn   %0, %1, %0\n\t"
101                 "wr     %0, %2, %%psr\n\t"
102                 "nop; nop; nop\n"
103                 : "=&r" (tmp)
104                 : "i" (PSR_PIL), "r" (old_psr)
105                 : "memory");
106 }
107
108 EXPORT_SYMBOL(__local_irq_save);
109 EXPORT_SYMBOL(local_irq_enable);
110 EXPORT_SYMBOL(local_irq_restore);
111
112 /*
113  * Dave Redman (djhr@tadpole.co.uk)
114  *
115  * IRQ numbers.. These are no longer restricted to 15..
116  *
117  * this is done to enable SBUS cards and onboard IO to be masked
118  * correctly. using the interrupt level isn't good enough.
119  *
120  * For example:
121  *   A device interrupting at sbus level6 and the Floppy both come in
122  *   at IRQ11, but enabling and disabling them requires writing to
123  *   different bits in the SLAVIO/SEC.
124  *
125  * As a result of these changes sun4m machines could now support
126  * directed CPU interrupts using the existing enable/disable irq code
127  * with tweaks.
128  *
129  */
130
131 static void irq_panic(void)
132 {
133     extern char *cputypval;
134     prom_printf("machine: %s doesn't have irq handlers defined!\n",cputypval);
135     prom_halt();
136 }
137
138 void (*sparc_init_timers)(irqreturn_t (*)(int, void *,struct pt_regs *)) =
139     (void (*)(irqreturn_t (*)(int, void *,struct pt_regs *))) irq_panic;
140
141 /*
142  * Dave Redman (djhr@tadpole.co.uk)
143  *
144  * There used to be extern calls and hard coded values here.. very sucky!
145  * instead, because some of the devices attach very early, I do something
146  * equally sucky but at least we'll never try to free statically allocated
147  * space or call kmalloc before kmalloc_init :(.
148  * 
149  * In fact it's the timer10 that attaches first.. then timer14
150  * then kmalloc_init is called.. then the tty interrupts attach.
151  * hmmm....
152  *
153  */
154 #define MAX_STATIC_ALLOC        4
155 struct irqaction static_irqaction[MAX_STATIC_ALLOC];
156 int static_irq_count;
157
158 struct irqaction *irq_action[NR_IRQS] = {
159         [0 ... (NR_IRQS-1)] = NULL
160 };
161
162 /* Used to protect the IRQ action lists */
163 spinlock_t irq_action_lock = SPIN_LOCK_UNLOCKED;
164
165 int show_interrupts(struct seq_file *p, void *v)
166 {
167         int i = *(loff_t *) v;
168         struct irqaction * action;
169         unsigned long flags;
170 #ifdef CONFIG_SMP
171         int j;
172 #endif
173
174         if (sparc_cpu_model == sun4d) {
175                 extern int show_sun4d_interrupts(struct seq_file *, void *);
176                 
177                 return show_sun4d_interrupts(p, v);
178         }
179         spin_lock_irqsave(&irq_action_lock, flags);
180         if (i < NR_IRQS) {
181                 action = *(i + irq_action);
182                 if (!action) 
183                         goto out_unlock;
184                 seq_printf(p, "%3d: ", i);
185 #ifndef CONFIG_SMP
186                 seq_printf(p, "%10u ", kstat_irqs(i));
187 #else
188                 for (j = 0; j < NR_CPUS; j++) {
189                         if (cpu_online(j))
190                                 seq_printf(p, "%10u ",
191                                     kstat_cpu(cpu_logical_map(j)).irqs[i]);
192                 }
193 #endif
194                 seq_printf(p, " %c %s",
195                         (action->flags & SA_INTERRUPT) ? '+' : ' ',
196                         action->name);
197                 for (action=action->next; action; action = action->next) {
198                         seq_printf(p, ",%s %s",
199                                 (action->flags & SA_INTERRUPT) ? " +" : "",
200                                 action->name);
201                 }
202                 seq_putc(p, '\n');
203         }
204 out_unlock:
205         spin_unlock_irqrestore(&irq_action_lock, flags);
206         return 0;
207 }
208
209 void free_irq(unsigned int irq, void *dev_id)
210 {
211         struct irqaction * action;
212         struct irqaction * tmp = NULL;
213         unsigned long flags;
214         unsigned int cpu_irq;
215         
216         if (sparc_cpu_model == sun4d) {
217                 extern void sun4d_free_irq(unsigned int, void *);
218                 
219                 sun4d_free_irq(irq, dev_id);
220                 return;
221         }
222         cpu_irq = irq & (NR_IRQS - 1);
223         if (cpu_irq > 14) {  /* 14 irq levels on the sparc */
224                 printk("Trying to free bogus IRQ %d\n", irq);
225                 return;
226         }
227
228         spin_lock_irqsave(&irq_action_lock, flags);
229
230         action = *(cpu_irq + irq_action);
231
232         if (!action->handler) {
233                 printk("Trying to free free IRQ%d\n",irq);
234                 goto out_unlock;
235         }
236         if (dev_id) {
237                 for (; action; action = action->next) {
238                         if (action->dev_id == dev_id)
239                                 break;
240                         tmp = action;
241                 }
242                 if (!action) {
243                         printk("Trying to free free shared IRQ%d\n",irq);
244                         goto out_unlock;
245                 }
246         } else if (action->flags & SA_SHIRQ) {
247                 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
248                 goto out_unlock;
249         }
250         if (action->flags & SA_STATIC_ALLOC)
251         {
252                 /* This interrupt is marked as specially allocated
253                  * so it is a bad idea to free it.
254                  */
255                 printk("Attempt to free statically allocated IRQ%d (%s)\n",
256                        irq, action->name);
257                 goto out_unlock;
258         }
259         
260         if (action && tmp)
261                 tmp->next = action->next;
262         else
263                 *(cpu_irq + irq_action) = action->next;
264
265         spin_unlock_irqrestore(&irq_action_lock, flags);
266
267         synchronize_irq(irq);
268
269         spin_lock_irqsave(&irq_action_lock, flags);
270
271         kfree(action);
272
273         if (!(*(cpu_irq + irq_action)))
274                 disable_irq(irq);
275
276 out_unlock:
277         spin_unlock_irqrestore(&irq_action_lock, flags);
278 }
279
280 EXPORT_SYMBOL(free_irq);
281
282 /*
283  * This is called when we want to synchronize with
284  * interrupts. We may for example tell a device to
285  * stop sending interrupts: but to make sure there
286  * are no interrupts that are executing on another
287  * CPU we need to call this function.
288  */
289 #ifdef CONFIG_SMP
290 void synchronize_irq(unsigned int irq)
291 {
292         printk("synchronize_irq says: implement me!\n");
293         BUG();
294 }
295 #endif /* SMP */
296
297 void unexpected_irq(int irq, void *dev_id, struct pt_regs * regs)
298 {
299         int i;
300         struct irqaction * action;
301         unsigned int cpu_irq;
302         
303         cpu_irq = irq & (NR_IRQS - 1);
304         action = *(cpu_irq + irq_action);
305
306         printk("IO device interrupt, irq = %d\n", irq);
307         printk("PC = %08lx NPC = %08lx FP=%08lx\n", regs->pc, 
308                     regs->npc, regs->u_regs[14]);
309         if (action) {
310                 printk("Expecting: ");
311                 for (i = 0; i < 16; i++)
312                         if (action->handler)
313                                 printk("[%s:%d:0x%x] ", action->name,
314                                        (int) i, (unsigned int) action->handler);
315         }
316         printk("AIEEE\n");
317         panic("bogus interrupt received");
318 }
319
320 void handler_irq(int irq, struct pt_regs * regs)
321 {
322         struct irqaction * action;
323         int cpu = smp_processor_id();
324 #ifdef CONFIG_SMP
325         extern void smp4m_irq_rotate(int cpu);
326 #endif
327
328         irq_enter();
329         disable_pil_irq(irq);
330 #ifdef CONFIG_SMP
331         /* Only rotate on lower priority IRQ's (scsi, ethernet, etc.). */
332         if(irq < 10)
333                 smp4m_irq_rotate(cpu);
334 #endif
335         action = *(irq + irq_action);
336         kstat_cpu(cpu).irqs[irq]++;
337         do {
338                 if (!action || !action->handler)
339                         unexpected_irq(irq, 0, regs);
340                 action->handler(irq, action->dev_id, regs);
341                 action = action->next;
342         } while (action);
343         enable_pil_irq(irq);
344         irq_exit();
345 }
346
347 #ifdef CONFIG_BLK_DEV_FD
348 extern void floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs);
349
350 void sparc_floppy_irq(int irq, void *dev_id, struct pt_regs *regs)
351 {
352         int cpu = smp_processor_id();
353
354         disable_pil_irq(irq);
355         irq_enter();
356         kstat_cpu(cpu).irqs[irq]++;
357         floppy_interrupt(irq, dev_id, regs);
358         irq_exit();
359         enable_pil_irq(irq);
360         // XXX Eek, it's totally changed with preempt_count() and such
361         // if (softirq_pending(cpu))
362         //      do_softirq();
363 }
364 #endif
365
366 /* Fast IRQ's on the Sparc can only have one routine attached to them,
367  * thus no sharing possible.
368  */
369 int request_fast_irq(unsigned int irq,
370                      irqreturn_t (*handler)(int, void *, struct pt_regs *),
371                      unsigned long irqflags, const char *devname)
372 {
373         struct irqaction *action;
374         unsigned long flags;
375         unsigned int cpu_irq;
376         int ret;
377 #ifdef CONFIG_SMP
378         struct tt_entry *trap_table;
379         extern struct tt_entry trapbase_cpu1, trapbase_cpu2, trapbase_cpu3;
380 #endif
381         
382         cpu_irq = irq & (NR_IRQS - 1);
383         if(cpu_irq > 14) {
384                 ret = -EINVAL;
385                 goto out;
386         }
387         if(!handler) {
388                 ret = -EINVAL;
389                 goto out;
390         }
391
392         spin_lock_irqsave(&irq_action_lock, flags);
393
394         action = *(cpu_irq + irq_action);
395         if(action) {
396                 if(action->flags & SA_SHIRQ)
397                         panic("Trying to register fast irq when already shared.\n");
398                 if(irqflags & SA_SHIRQ)
399                         panic("Trying to register fast irq as shared.\n");
400
401                 /* Anyway, someone already owns it so cannot be made fast. */
402                 printk("request_fast_irq: Trying to register yet already owned.\n");
403                 ret = -EBUSY;
404                 goto out_unlock;
405         }
406
407         /* If this is flagged as statically allocated then we use our
408          * private struct which is never freed.
409          */
410         if (irqflags & SA_STATIC_ALLOC) {
411             if (static_irq_count < MAX_STATIC_ALLOC)
412                 action = &static_irqaction[static_irq_count++];
413             else
414                 printk("Fast IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",
415                        irq, devname);
416         }
417         
418         if (action == NULL)
419             action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
420                                                  GFP_ATOMIC);
421         
422         if (!action) { 
423                 ret = -ENOMEM;
424                 goto out_unlock;
425         }
426
427         /* Dork with trap table if we get this far. */
428 #define INSTANTIATE(table) \
429         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_one = SPARC_RD_PSR_L0; \
430         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two = \
431                 SPARC_BRANCH((unsigned long) handler, \
432                              (unsigned long) &table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two);\
433         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_three = SPARC_RD_WIM_L3; \
434         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_four = SPARC_NOP;
435
436         INSTANTIATE(sparc_ttable)
437 #ifdef CONFIG_SMP
438         trap_table = &trapbase_cpu1; INSTANTIATE(trap_table)
439         trap_table = &trapbase_cpu2; INSTANTIATE(trap_table)
440         trap_table = &trapbase_cpu3; INSTANTIATE(trap_table)
441 #endif
442 #undef INSTANTIATE
443         /*
444          * XXX Correct thing whould be to flush only I- and D-cache lines
445          * which contain the handler in question. But as of time of the
446          * writing we have no CPU-neutral interface to fine-grained flushes.
447          */
448         flush_cache_all();
449
450         action->handler = handler;
451         action->flags = irqflags;
452         action->mask = 0;
453         action->name = devname;
454         action->dev_id = NULL;
455         action->next = NULL;
456
457         *(cpu_irq + irq_action) = action;
458
459         enable_irq(irq);
460
461         ret = 0;
462 out_unlock:
463         spin_unlock_irqrestore(&irq_action_lock, flags);
464 out:
465         return ret;
466 }
467
468 int request_irq(unsigned int irq,
469                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
470                 unsigned long irqflags, const char * devname, void *dev_id)
471 {
472         struct irqaction * action, *tmp = NULL;
473         unsigned long flags;
474         unsigned int cpu_irq;
475         int ret;
476         
477         if (sparc_cpu_model == sun4d) {
478                 extern int sun4d_request_irq(unsigned int, 
479                                              irqreturn_t (*)(int, void *, struct pt_regs *),
480                                              unsigned long, const char *, void *);
481                 return sun4d_request_irq(irq, handler, irqflags, devname, dev_id);
482         }
483         cpu_irq = irq & (NR_IRQS - 1);
484         if(cpu_irq > 14) {
485                 ret = -EINVAL;
486                 goto out;
487         }
488         if (!handler) {
489                 ret = -EINVAL;
490                 goto out;
491         }
492             
493         spin_lock_irqsave(&irq_action_lock, flags);
494
495         action = *(cpu_irq + irq_action);
496         if (action) {
497                 if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
498                         for (tmp = action; tmp->next; tmp = tmp->next);
499                 } else {
500                         ret = -EBUSY;
501                         goto out_unlock;
502                 }
503                 if ((action->flags & SA_INTERRUPT) ^ (irqflags & SA_INTERRUPT)) {
504                         printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
505                         ret = -EBUSY;
506                         goto out_unlock;
507                 }   
508                 action = NULL;          /* Or else! */
509         }
510
511         /* If this is flagged as statically allocated then we use our
512          * private struct which is never freed.
513          */
514         if (irqflags & SA_STATIC_ALLOC) {
515                 if (static_irq_count < MAX_STATIC_ALLOC)
516                         action = &static_irqaction[static_irq_count++];
517                 else
518                         printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname);
519         }
520         
521         if (action == NULL)
522                 action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
523                                                      GFP_ATOMIC);
524         
525         if (!action) { 
526                 ret = -ENOMEM;
527                 goto out_unlock;
528         }
529
530         action->handler = handler;
531         action->flags = irqflags;
532         action->mask = 0;
533         action->name = devname;
534         action->next = NULL;
535         action->dev_id = dev_id;
536
537         if (tmp)
538                 tmp->next = action;
539         else
540                 *(cpu_irq + irq_action) = action;
541
542         enable_irq(irq);
543
544         ret = 0;
545 out_unlock:
546         spin_unlock_irqrestore(&irq_action_lock, flags);
547 out:
548         return ret;
549 }
550
551 EXPORT_SYMBOL(request_irq);
552
553 /* We really don't need these at all on the Sparc.  We only have
554  * stubs here because they are exported to modules.
555  */
556 unsigned long probe_irq_on(void)
557 {
558         return 0;
559 }
560
561 EXPORT_SYMBOL(probe_irq_on);
562
563 int probe_irq_off(unsigned long mask)
564 {
565         return 0;
566 }
567
568 EXPORT_SYMBOL(probe_irq_off);
569
570 /* djhr
571  * This could probably be made indirect too and assigned in the CPU
572  * bits of the code. That would be much nicer I think and would also
573  * fit in with the idea of being able to tune your kernel for your machine
574  * by removing unrequired machine and device support.
575  *
576  */
577
578 void __init init_IRQ(void)
579 {
580         extern void sun4c_init_IRQ( void );
581         extern void sun4m_init_IRQ( void );
582         extern void sun4d_init_IRQ( void );
583
584         switch(sparc_cpu_model) {
585         case sun4c:
586         case sun4:
587                 sun4c_init_IRQ();
588                 break;
589
590         case sun4m:
591 #ifdef CONFIG_PCI
592                 pcic_probe();
593                 if (pcic_present()) {
594                         sun4m_pci_init_IRQ();
595                         break;
596                 }
597 #endif
598                 sun4m_init_IRQ();
599                 break;
600                 
601         case sun4d:
602                 sun4d_init_IRQ();
603                 break;
604
605         default:
606                 prom_printf("Cannot initialize IRQ's on this Sun machine...");
607                 break;
608         }
609         btfixup();
610 }
611
612 void init_irq_proc(void)
613 {
614         /* For now, nothing... */
615 }