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