Merge nommu tree
[sfrench/cifs-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/pcic.h>
49 #include <asm/cacheflush.h>
50
51 #ifdef CONFIG_SMP
52 #define SMP_NOP2 "nop; nop;\n\t"
53 #define SMP_NOP3 "nop; nop; nop;\n\t"
54 #else
55 #define SMP_NOP2
56 #define SMP_NOP3
57 #endif /* SMP */
58 unsigned long __local_irq_save(void)
59 {
60         unsigned long retval;
61         unsigned long tmp;
62
63         __asm__ __volatile__(
64                 "rd     %%psr, %0\n\t"
65                 SMP_NOP3        /* Sun4m + Cypress + SMP bug */
66                 "or     %0, %2, %1\n\t"
67                 "wr     %1, 0, %%psr\n\t"
68                 "nop; nop; nop\n"
69                 : "=&r" (retval), "=r" (tmp)
70                 : "i" (PSR_PIL)
71                 : "memory");
72
73         return retval;
74 }
75
76 void local_irq_enable(void)
77 {
78         unsigned long tmp;
79
80         __asm__ __volatile__(
81                 "rd     %%psr, %0\n\t"
82                 SMP_NOP3        /* Sun4m + Cypress + SMP bug */
83                 "andn   %0, %1, %0\n\t"
84                 "wr     %0, 0, %%psr\n\t"
85                 "nop; nop; nop\n"
86                 : "=&r" (tmp)
87                 : "i" (PSR_PIL)
88                 : "memory");
89 }
90
91 void local_irq_restore(unsigned long old_psr)
92 {
93         unsigned long tmp;
94
95         __asm__ __volatile__(
96                 "rd     %%psr, %0\n\t"
97                 "and    %2, %1, %2\n\t"
98                 SMP_NOP2        /* Sun4m + Cypress + SMP bug */
99                 "andn   %0, %1, %0\n\t"
100                 "wr     %0, %2, %%psr\n\t"
101                 "nop; nop; nop\n"
102                 : "=&r" (tmp)
103                 : "i" (PSR_PIL), "r" (old_psr)
104                 : "memory");
105 }
106
107 EXPORT_SYMBOL(__local_irq_save);
108 EXPORT_SYMBOL(local_irq_enable);
109 EXPORT_SYMBOL(local_irq_restore);
110
111 /*
112  * Dave Redman (djhr@tadpole.co.uk)
113  *
114  * IRQ numbers.. These are no longer restricted to 15..
115  *
116  * this is done to enable SBUS cards and onboard IO to be masked
117  * correctly. using the interrupt level isn't good enough.
118  *
119  * For example:
120  *   A device interrupting at sbus level6 and the Floppy both come in
121  *   at IRQ11, but enabling and disabling them requires writing to
122  *   different bits in the SLAVIO/SEC.
123  *
124  * As a result of these changes sun4m machines could now support
125  * directed CPU interrupts using the existing enable/disable irq code
126  * with tweaks.
127  *
128  */
129
130 static void irq_panic(void)
131 {
132     extern char *cputypval;
133     prom_printf("machine: %s doesn't have irq handlers defined!\n",cputypval);
134     prom_halt();
135 }
136
137 void (*sparc_init_timers)(irqreturn_t (*)(int, void *,struct pt_regs *)) =
138     (void (*)(irqreturn_t (*)(int, void *,struct pt_regs *))) irq_panic;
139
140 /*
141  * Dave Redman (djhr@tadpole.co.uk)
142  *
143  * There used to be extern calls and hard coded values here.. very sucky!
144  * instead, because some of the devices attach very early, I do something
145  * equally sucky but at least we'll never try to free statically allocated
146  * space or call kmalloc before kmalloc_init :(.
147  * 
148  * In fact it's the timer10 that attaches first.. then timer14
149  * then kmalloc_init is called.. then the tty interrupts attach.
150  * hmmm....
151  *
152  */
153 #define MAX_STATIC_ALLOC        4
154 struct irqaction static_irqaction[MAX_STATIC_ALLOC];
155 int static_irq_count;
156
157 struct {
158         struct irqaction *action;
159         int flags;
160 } sparc_irq[NR_IRQS];
161 #define SPARC_IRQ_INPROGRESS 1
162
163 /* Used to protect the IRQ action lists */
164 DEFINE_SPINLOCK(irq_action_lock);
165
166 int show_interrupts(struct seq_file *p, void *v)
167 {
168         int i = *(loff_t *) v;
169         struct irqaction * action;
170         unsigned long flags;
171 #ifdef CONFIG_SMP
172         int j;
173 #endif
174
175         if (sparc_cpu_model == sun4d) {
176                 extern int show_sun4d_interrupts(struct seq_file *, void *);
177                 
178                 return show_sun4d_interrupts(p, v);
179         }
180         spin_lock_irqsave(&irq_action_lock, flags);
181         if (i < NR_IRQS) {
182                 action = sparc_irq[i].action;
183                 if (!action) 
184                         goto out_unlock;
185                 seq_printf(p, "%3d: ", i);
186 #ifndef CONFIG_SMP
187                 seq_printf(p, "%10u ", kstat_irqs(i));
188 #else
189                 for_each_online_cpu(j) {
190                         seq_printf(p, "%10u ",
191                                     kstat_cpu(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 **actionp;
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         actionp = &sparc_irq[cpu_irq].action;
231         action = *actionp;
232
233         if (!action->handler) {
234                 printk("Trying to free free IRQ%d\n",irq);
235                 goto out_unlock;
236         }
237         if (dev_id) {
238                 for (; action; action = action->next) {
239                         if (action->dev_id == dev_id)
240                                 break;
241                         actionp = &action->next;
242                 }
243                 if (!action) {
244                         printk("Trying to free free shared IRQ%d\n",irq);
245                         goto out_unlock;
246                 }
247         } else if (action->flags & SA_SHIRQ) {
248                 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
249                 goto out_unlock;
250         }
251         if (action->flags & SA_STATIC_ALLOC)
252         {
253                 /* This interrupt is marked as specially allocated
254                  * so it is a bad idea to free it.
255                  */
256                 printk("Attempt to free statically allocated IRQ%d (%s)\n",
257                        irq, action->name);
258                 goto out_unlock;
259         }
260
261         *actionp = action->next;
262
263         spin_unlock_irqrestore(&irq_action_lock, flags);
264
265         synchronize_irq(irq);
266
267         spin_lock_irqsave(&irq_action_lock, flags);
268
269         kfree(action);
270
271         if (!sparc_irq[cpu_irq].action)
272                 disable_irq(irq);
273
274 out_unlock:
275         spin_unlock_irqrestore(&irq_action_lock, flags);
276 }
277
278 EXPORT_SYMBOL(free_irq);
279
280 /*
281  * This is called when we want to synchronize with
282  * interrupts. We may for example tell a device to
283  * stop sending interrupts: but to make sure there
284  * are no interrupts that are executing on another
285  * CPU we need to call this function.
286  */
287 #ifdef CONFIG_SMP
288 void synchronize_irq(unsigned int irq)
289 {
290         unsigned int cpu_irq;
291
292         cpu_irq = irq & (NR_IRQS - 1);
293         while (sparc_irq[cpu_irq].flags & SPARC_IRQ_INPROGRESS)
294                 cpu_relax();
295 }
296 #endif /* SMP */
297
298 void unexpected_irq(int irq, void *dev_id, struct pt_regs * regs)
299 {
300         int i;
301         struct irqaction * action;
302         unsigned int cpu_irq;
303         
304         cpu_irq = irq & (NR_IRQS - 1);
305         action = sparc_irq[cpu_irq].action;
306
307         printk("IO device interrupt, irq = %d\n", irq);
308         printk("PC = %08lx NPC = %08lx FP=%08lx\n", regs->pc, 
309                     regs->npc, regs->u_regs[14]);
310         if (action) {
311                 printk("Expecting: ");
312                 for (i = 0; i < 16; i++)
313                         if (action->handler)
314                                 printk("[%s:%d:0x%x] ", action->name,
315                                        (int) i, (unsigned int) action->handler);
316         }
317         printk("AIEEE\n");
318         panic("bogus interrupt received");
319 }
320
321 void handler_irq(int irq, struct pt_regs * regs)
322 {
323         struct irqaction * action;
324         int cpu = smp_processor_id();
325 #ifdef CONFIG_SMP
326         extern void smp4m_irq_rotate(int cpu);
327 #endif
328
329         irq_enter();
330         disable_pil_irq(irq);
331 #ifdef CONFIG_SMP
332         /* Only rotate on lower priority IRQ's (scsi, ethernet, etc.). */
333         if(irq < 10)
334                 smp4m_irq_rotate(cpu);
335 #endif
336         action = sparc_irq[irq].action;
337         sparc_irq[irq].flags |= SPARC_IRQ_INPROGRESS;
338         kstat_cpu(cpu).irqs[irq]++;
339         do {
340                 if (!action || !action->handler)
341                         unexpected_irq(irq, NULL, regs);
342                 action->handler(irq, action->dev_id, regs);
343                 action = action->next;
344         } while (action);
345         sparc_irq[irq].flags &= ~SPARC_IRQ_INPROGRESS;
346         enable_pil_irq(irq);
347         irq_exit();
348 }
349
350 #ifdef CONFIG_BLK_DEV_FD
351 extern void floppy_interrupt(int irq, void *dev_id, struct pt_regs *regs);
352
353 void sparc_floppy_irq(int irq, void *dev_id, struct pt_regs *regs)
354 {
355         int cpu = smp_processor_id();
356
357         disable_pil_irq(irq);
358         irq_enter();
359         kstat_cpu(cpu).irqs[irq]++;
360         floppy_interrupt(irq, dev_id, regs);
361         irq_exit();
362         enable_pil_irq(irq);
363         // XXX Eek, it's totally changed with preempt_count() and such
364         // if (softirq_pending(cpu))
365         //      do_softirq();
366 }
367 #endif
368
369 /* Fast IRQ's on the Sparc can only have one routine attached to them,
370  * thus no sharing possible.
371  */
372 int request_fast_irq(unsigned int irq,
373                      irqreturn_t (*handler)(int, void *, struct pt_regs *),
374                      unsigned long irqflags, const char *devname)
375 {
376         struct irqaction *action;
377         unsigned long flags;
378         unsigned int cpu_irq;
379         int ret;
380 #ifdef CONFIG_SMP
381         struct tt_entry *trap_table;
382         extern struct tt_entry trapbase_cpu1, trapbase_cpu2, trapbase_cpu3;
383 #endif
384         
385         cpu_irq = irq & (NR_IRQS - 1);
386         if(cpu_irq > 14) {
387                 ret = -EINVAL;
388                 goto out;
389         }
390         if(!handler) {
391                 ret = -EINVAL;
392                 goto out;
393         }
394
395         spin_lock_irqsave(&irq_action_lock, flags);
396
397         action = sparc_irq[cpu_irq].action;
398         if(action) {
399                 if(action->flags & SA_SHIRQ)
400                         panic("Trying to register fast irq when already shared.\n");
401                 if(irqflags & SA_SHIRQ)
402                         panic("Trying to register fast irq as shared.\n");
403
404                 /* Anyway, someone already owns it so cannot be made fast. */
405                 printk("request_fast_irq: Trying to register yet already owned.\n");
406                 ret = -EBUSY;
407                 goto out_unlock;
408         }
409
410         /* If this is flagged as statically allocated then we use our
411          * private struct which is never freed.
412          */
413         if (irqflags & SA_STATIC_ALLOC) {
414             if (static_irq_count < MAX_STATIC_ALLOC)
415                 action = &static_irqaction[static_irq_count++];
416             else
417                 printk("Fast IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",
418                        irq, devname);
419         }
420         
421         if (action == NULL)
422             action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
423                                                  GFP_ATOMIC);
424         
425         if (!action) { 
426                 ret = -ENOMEM;
427                 goto out_unlock;
428         }
429
430         /* Dork with trap table if we get this far. */
431 #define INSTANTIATE(table) \
432         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_one = SPARC_RD_PSR_L0; \
433         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two = \
434                 SPARC_BRANCH((unsigned long) handler, \
435                              (unsigned long) &table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two);\
436         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_three = SPARC_RD_WIM_L3; \
437         table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_four = SPARC_NOP;
438
439         INSTANTIATE(sparc_ttable)
440 #ifdef CONFIG_SMP
441         trap_table = &trapbase_cpu1; INSTANTIATE(trap_table)
442         trap_table = &trapbase_cpu2; INSTANTIATE(trap_table)
443         trap_table = &trapbase_cpu3; INSTANTIATE(trap_table)
444 #endif
445 #undef INSTANTIATE
446         /*
447          * XXX Correct thing whould be to flush only I- and D-cache lines
448          * which contain the handler in question. But as of time of the
449          * writing we have no CPU-neutral interface to fine-grained flushes.
450          */
451         flush_cache_all();
452
453         action->handler = handler;
454         action->flags = irqflags;
455         cpus_clear(action->mask);
456         action->name = devname;
457         action->dev_id = NULL;
458         action->next = NULL;
459
460         sparc_irq[cpu_irq].action = action;
461
462         enable_irq(irq);
463
464         ret = 0;
465 out_unlock:
466         spin_unlock_irqrestore(&irq_action_lock, flags);
467 out:
468         return ret;
469 }
470
471 int request_irq(unsigned int irq,
472                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
473                 unsigned long irqflags, const char * devname, void *dev_id)
474 {
475         struct irqaction * action, **actionp;
476         unsigned long flags;
477         unsigned int cpu_irq;
478         int ret;
479         
480         if (sparc_cpu_model == sun4d) {
481                 extern int sun4d_request_irq(unsigned int, 
482                                              irqreturn_t (*)(int, void *, struct pt_regs *),
483                                              unsigned long, const char *, void *);
484                 return sun4d_request_irq(irq, handler, irqflags, devname, dev_id);
485         }
486         cpu_irq = irq & (NR_IRQS - 1);
487         if(cpu_irq > 14) {
488                 ret = -EINVAL;
489                 goto out;
490         }
491         if (!handler) {
492                 ret = -EINVAL;
493                 goto out;
494         }
495             
496         spin_lock_irqsave(&irq_action_lock, flags);
497
498         actionp = &sparc_irq[cpu_irq].action;
499         action = *actionp;
500         if (action) {
501                 if (!(action->flags & SA_SHIRQ) || !(irqflags & SA_SHIRQ)) {
502                         ret = -EBUSY;
503                         goto out_unlock;
504                 }
505                 if ((action->flags & SA_INTERRUPT) != (irqflags & SA_INTERRUPT)) {
506                         printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
507                         ret = -EBUSY;
508                         goto out_unlock;
509                 }
510                 for ( ; action; action = *actionp)
511                         actionp = &action->next;
512         }
513
514         /* If this is flagged as statically allocated then we use our
515          * private struct which is never freed.
516          */
517         if (irqflags & SA_STATIC_ALLOC) {
518                 if (static_irq_count < MAX_STATIC_ALLOC)
519                         action = &static_irqaction[static_irq_count++];
520                 else
521                         printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname);
522         }
523         
524         if (action == NULL)
525                 action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
526                                                      GFP_ATOMIC);
527         
528         if (!action) { 
529                 ret = -ENOMEM;
530                 goto out_unlock;
531         }
532
533         action->handler = handler;
534         action->flags = irqflags;
535         cpus_clear(action->mask);
536         action->name = devname;
537         action->next = NULL;
538         action->dev_id = dev_id;
539
540         *actionp = 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 }