vserver 1.9.3
[linux-2.6.git] / arch / mips / baget / irq.c
1 /*
2  * Code to handle Baget/MIPS IRQs plus some generic interrupt stuff.
3  *
4  * Copyright (C) 1998 Vladimir Roganov & Gleb Raiko
5  *      Code (mostly sleleton and comments) derived from DECstation IRQ
6  *      handling.
7  */
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/kernel_stat.h>
11 #include <linux/module.h>
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/types.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/timex.h>
18 #include <linux/slab.h>
19 #include <linux/random.h>
20 #include <linux/delay.h>
21
22 #include <asm/bitops.h>
23 #include <asm/bootinfo.h>
24 #include <asm/io.h>
25 #include <asm/irq.h>
26 #include <asm/mipsregs.h>
27 #include <asm/system.h>
28
29 #include <asm/baget/baget.h>
30
31 volatile unsigned long irq_err_count;
32
33 /*
34  * This table is a correspondence between IRQ numbers and CPU PILs
35  */
36
37 static int irq_to_pil_map[BAGET_IRQ_NR] = {
38         7/*fixme: dma_err -1*/,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 0x00 - 0x0f */
39         -1,-1,-1,-1, 3,-1,-1,-1, 2, 2, 2,-1, 3,-1,-1,3/*fixme: lance*/, /* 0x10 - 0x1f */
40         -1,-1,-1,-1,-1,-1, 5,-1,-1,-1,-1,-1, 7,-1,-1,-1, /* 0x20 - 0x2f */
41         -1, 3, 2/*fixme systimer:3*/, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3  /* 0x30 - 0x3f */
42 };
43
44 static inline int irq_to_pil(int irq_nr)
45 {
46         int pil = -1;
47
48         if (irq_nr >= BAGET_IRQ_NR)
49                 baget_printk("irq_to_pil: too large irq_nr = 0x%x\n", irq_nr);
50         else {
51                 pil = irq_to_pil_map[irq_nr];
52                 if (pil == -1)
53                         baget_printk("irq_to_pil: unknown irq = 0x%x\n", irq_nr);
54         }
55
56         return pil;
57 }
58
59 /* Function for careful CP0 interrupt mask access */
60
61 static inline void modify_cp0_intmask(unsigned clr_mask, unsigned set_mask)
62 {
63         unsigned long status = read_c0_status();
64         status &= ~((clr_mask & 0xFF) << 8);
65         status |=   (set_mask & 0xFF) << 8;
66         write_c0_status(status);
67 }
68
69 /*
70  *  These two functions may be used for unconditional IRQ
71  *  masking via their PIL protection.
72  */
73
74 static inline void mask_irq(unsigned int irq_nr)
75 {
76         modify_cp0_intmask(irq_to_pil(irq_nr), 0);
77 }
78
79 static inline void unmask_irq(unsigned int irq_nr)
80 {
81         modify_cp0_intmask(0, irq_to_pil(irq_nr));
82 }
83
84 /*
85  * The following section is introduced for masking/unasking IRQ
86  * only while no more IRQs uses same CPU PIL.
87  *
88  * These functions are used in request_irq, free_irq, but it looks
89  * they cannot change something: CP0_STATUS is private for any
90  * process, and their action is invisible for system.
91  */
92
93 static volatile unsigned int pil_in_use[BAGET_PIL_NR] = { 0, };
94
95 void mask_irq_count(int irq_nr)
96 {
97         unsigned long flags;
98         int pil = irq_to_pil(irq_nr);
99
100         local_irq_save(flags);
101         if (!--pil_in_use[pil])
102                 mask_irq(irq_nr);
103         local_irq_restore(flags);
104 }
105
106 void unmask_irq_count(int irq_nr)
107 {
108         unsigned long flags;
109         int pil = irq_to_pil(irq_nr);
110
111         local_irq_save(flags);
112         if (!pil_in_use[pil]++)
113                 unmask_irq(irq_nr);
114         local_irq_restore(flags);
115 }
116
117 /*
118  * Two functions below are exported versions of mask/unmask IRQ
119  */
120
121 void disable_irq(unsigned int irq_nr)
122 {
123         unsigned long flags;
124
125         local_irq_save(flags);
126         mask_irq(irq_nr);
127         local_irq_restore(flags);
128 }
129
130 void enable_irq(unsigned int irq_nr)
131 {
132         unsigned long flags;
133
134         local_irq_save(flags);
135         unmask_irq(irq_nr);
136         local_irq_restore(flags);
137 }
138
139 /*
140  * Pointers to the low-level handlers: first the general ones, then the
141  * fast ones, then the bad ones.
142  */
143 static struct irqaction *irq_action[BAGET_IRQ_NR] = { NULL, };
144
145 int get_irq_list(char *buf)
146 {
147         int i, len = 0;
148         struct irqaction * action;
149         unsigned long flags;
150
151         for (i = 0 ; i < BAGET_IRQ_NR ; i++) {
152                 local_irq_save(flags);
153                 action = irq_action[i];
154                 if (!action)
155                         gotos skip;
156                 len += sprintf(buf+len, "%2d: %8d %c %s",
157                         i, kstat_this_cpu.irqs[i],
158                         (action->flags & SA_INTERRUPT) ? '+' : ' ',
159                         action->name);
160                 for (action=action->next; action; action = action->next) {
161                         len += sprintf(buf+len, ",%s %s",
162                                 (action->flags & SA_INTERRUPT) ? " +" : "",
163                                 action->name);
164                 }
165                 len += sprintf(buf+len, "\n");
166 skip:
167                 local_irq_restore(flags);
168         }
169         return len;
170 }
171
172
173 /*
174  * do_IRQ handles IRQ's that have been installed without the
175  * SA_INTERRUPT flag: it uses the full signal-handling return
176  * and runs with other interrupts enabled. All relatively slow
177  * IRQ's should use this format: notably the keyboard/timer
178  * routines.
179  */
180 static void do_IRQ(int irq, struct pt_regs * regs)
181 {
182         struct irqaction *action;
183         int ret, do_random, cpu;
184
185         cpu = smp_processor_id();
186         irq_enter();
187         kstat_cpus(cpu).irqs[irq]++;
188
189         mask_irq(irq);
190         action = *(irq + irq_action);
191         if (action) {
192                 if (!(action->flags & SA_INTERRUPT))
193                         local_irq_enable();
194                 action = *(irq + irq_action);
195                 do_random = 0;
196                 do {
197                         ret = action->handler(irq, action->dev_id, regs);
198                         if (ret == IRQ_HANDLED)
199                                 do_random |= action->flags;
200                         action = action->next;
201                 } while (action);
202                 if (do_random & SA_SAMPLE_RANDOM)
203                         add_interrupt_randomness(irq);
204                 local_irq_disable();
205         } else {
206                 printk("do_IRQ: Unregistered IRQ (0x%X) occurred\n", irq);
207         }
208         unmask_irq(irq);
209         irq_exit();
210
211         /* unmasking and bottom half handling is done magically for us. */
212 }
213
214 /*
215  *  What to do in case of 'no VIC register available' for current interrupt
216  */
217 static void vic_reg_error(unsigned long address, unsigned char active_pils)
218 {
219         printk("\nNo VIC register found: reg=%08lx active_pils=%02x\n"
220                "Current interrupt mask from CP0_CAUSE: %02x\n",
221                address, 0xff & active_pils,
222                0xff & (read_c0_cause()>>8));
223         { int i; for (i=0; i<10000; i++) udelay(1000); }
224 }
225
226 static char baget_fpu_irq = BAGET_FPU_IRQ;
227 #define BAGET_INT_FPU {(unsigned long)&baget_fpu_irq, 1}
228
229 /*
230  *  Main interrupt handler: interrupt demultiplexer
231  */
232 asmlinkage void baget_interrupt(struct pt_regs *regs)
233 {
234         static struct baget_int_reg int_reg[BAGET_PIL_NR] = {
235                 BAGET_INT_NONE, BAGET_INT_NONE, BAGET_INT0_ACK, BAGET_INT1_ACK,
236                 BAGET_INT_NONE, BAGET_INT_FPU,  BAGET_INT_NONE, BAGET_INT5_ACK
237         };
238         unsigned char active_pils;
239         while ((active_pils = read_c0_cause()>>8)) {
240                 int pil;
241                 struct baget_int_reg* reg;
242
243                 for (pil = 0; pil < BAGET_PIL_NR; pil++) {
244                         if (!(active_pils & (1<<pil))) continue;
245
246                         reg = &int_reg[pil];
247
248                         if (reg->address) {
249                                 extern int try_read(unsigned long,int);
250                                 int irq  = try_read(reg->address, reg->size);
251
252                                 if (irq != -1)
253                                       do_IRQ(BAGET_IRQ_MASK(irq), regs);
254                                 else
255                                       vic_reg_error(reg->address, active_pils);
256                         } else {
257                                 printk("baget_interrupt: unknown interrupt "
258                                        "(pil = %d)\n", pil);
259                         }
260                 }
261         }
262 }
263
264 /*
265  * Idea is to put all interrupts
266  * in a single table and differenciate them just by number.
267  */
268 int setup_baget_irq(int irq, struct irqaction * new)
269 {
270         int shared = 0;
271         struct irqaction *old, **p;
272         unsigned long flags;
273
274         p = irq_action + irq;
275         if ((old = *p) != NULL) {
276                 /* Can't share interrupts unless both agree to */
277                 if (!(old->flags & new->flags & SA_SHIRQ))
278                         return -EBUSY;
279
280                 /* Can't share interrupts unless both are same type */
281                 if ((old->flags ^ new->flags) & SA_INTERRUPT)
282                         return -EBUSY;
283
284                 /* add new interrupt at end of irq queue */
285                 do {
286                         p = &old->next;
287                         old = *p;
288                 } while (old);
289                 shared = 1;
290         }
291
292         if (new->flags & SA_SAMPLE_RANDOM)
293                 rand_initialize_irq(irq);
294
295         local_irq_save(flags);
296         *p = new;
297         local_irq_restore(flags);
298
299         if (!shared) {
300                 unmask_irq_count(irq);
301         }
302
303         return 0;
304 }
305
306 int request_irq(unsigned int irq,
307                 void (*handler)(int, void *, struct pt_regs *),
308                 unsigned long irqflags,
309                 const char * devname,
310                 void *dev_id)
311 {
312         int retval;
313         struct irqaction * action;
314
315         if (irq >= BAGET_IRQ_NR)
316                 return -EINVAL;
317         if (!handler)
318                 return -EINVAL;
319         if (irq_to_pil_map[irq] < 0)
320                 return -EINVAL;
321
322         action = (struct irqaction *)
323                         kmalloc(sizeof(struct irqaction), GFP_KERNEL);
324         if (!action)
325                 return -ENOMEM;
326
327         action->handler = handler;
328         action->flags = irqflags;
329         cpus_clear(action->mask);
330         action->name = devname;
331         action->next = NULL;
332         action->dev_id = dev_id;
333
334         retval = setup_baget_irq(irq, action);
335
336         if (retval)
337                 kfree(action);
338
339         return retval;
340 }
341
342 EXPORT_SYMBOL(request_irq);
343
344 void free_irq(unsigned int irq, void *dev_id)
345 {
346         struct irqaction * action, **p;
347         unsigned long flags;
348
349         if (irq >= BAGET_IRQ_NR)
350                 printk("Trying to free IRQ%d\n",irq);
351
352         for (p = irq + irq_action; (action = *p) != NULL; p = &action->next) {
353                 if (action->dev_id != dev_id)
354                         continue;
355
356                 /* Found it - now free it */
357                 local_irq_save(flags);
358                 *p = action->next;
359                 if (!irq[irq_action])
360                         unmask_irq_count(irq);
361                 local_irq_restore(flags);
362                 kfree(action);
363                 return;
364         }
365         printk("Trying to free free IRQ%d\n",irq);
366 }
367
368 EXPORT_SYMBOL(free_irq);
369
370 unsigned long probe_irq_on (void)
371 {
372         /* TODO */
373         return 0;
374 }
375
376 EXPORT_SYMBOL(probe_irq_on);
377
378 int probe_irq_off (unsigned long irqs)
379 {
380         /* TODO */
381         return 0;
382 }
383
384 EXPORT_SYMBOL(probe_irq_off);
385
386
387 static void write_err_interrupt(int irq, void *dev_id, struct pt_regs * regs)
388 {
389         *(volatile char*) BAGET_WRERR_ACK = 0;
390 }
391
392 static struct irqaction irq0  =
393 { write_err_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "bus write error", NULL, NULL};
394
395 void __init init_IRQ(void)
396 {
397         irq_setup();
398
399         /* Enable access to VIC interrupt registers */
400         vac_outw(0xacef | 0x8200, VAC_PIO_FUNC);
401
402         /* Enable interrupts for pils 2 and 3 (lines 0 and 1) */
403         modify_cp0_intmask(0, (1<<2)|(1<<3));
404
405         if (setup_baget_irq(0, &irq0) < 0)
406                 printk("init_IRQ: unable to register write_err irq\n");
407 }