Import 2.3.18pre1
[davej-history.git] / arch / sparc / kernel / sun4d_irq.c
blob828cb5d8970b2f9e64de6490ac0617df5867d777
1 /* $Id: sun4d_irq.c,v 1.19 1999/08/31 06:54:25 davem Exp $
2 * arch/sparc/kernel/sun4d_irq.c:
3 * SS1000/SC2000 interrupt handling.
5 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Heavily based on arch/sparc/kernel/irq.c.
7 */
9 #include <linux/ptrace.h>
10 #include <linux/errno.h>
11 #include <linux/linkage.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/malloc.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/spinlock.h>
23 #include <asm/ptrace.h>
24 #include <asm/processor.h>
25 #include <asm/system.h>
26 #include <asm/psr.h>
27 #include <asm/smp.h>
28 #include <asm/vaddrs.h>
29 #include <asm/timer.h>
30 #include <asm/openprom.h>
31 #include <asm/oplib.h>
32 #include <asm/traps.h>
33 #include <asm/irq.h>
34 #include <asm/io.h>
35 #include <asm/pgtable.h>
36 #include <asm/sbus.h>
37 #include <asm/sbi.h>
39 /* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */
40 /* #define DISTRIBUTE_IRQS */
42 struct sun4d_timer_regs *sun4d_timers;
43 #define TIMER_IRQ 10
45 #define MAX_STATIC_ALLOC 4
46 extern struct irqaction static_irqaction[MAX_STATIC_ALLOC];
47 extern int static_irq_count;
48 unsigned char cpu_leds[32];
49 #ifdef __SMP__
50 unsigned char sbus_tid[32];
51 #endif
53 extern struct irqaction *irq_action[];
55 struct sbus_action {
56 struct irqaction *action;
57 /* For SMP this needs to be extended */
58 } *sbus_actions;
60 static int pil_to_sbus[] = {
61 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
64 static int sbus_to_pil[] = {
65 0, 2, 3, 5, 7, 9, 11, 13,
68 static int nsbi;
69 #ifdef __SMP__
70 spinlock_t sun4d_imsk_lock = SPIN_LOCK_UNLOCKED;
71 #endif
73 int sun4d_get_irq_list(char *buf)
75 int i, j = 0, k = 0, len = 0, sbusl;
76 struct irqaction * action;
77 #ifdef __SMP__
78 int x;
79 #endif
81 for (i = 0 ; i < NR_IRQS ; i++) {
82 sbusl = pil_to_sbus[i];
83 if (!sbusl) {
84 action = *(i + irq_action);
85 if (!action)
86 continue;
87 } else {
88 for (j = 0; j < nsbi; j++) {
89 for (k = 0; k < 4; k++)
90 if ((action = sbus_actions [(j << 5) + (sbusl << 2) + k].action))
91 goto found_it;
93 continue;
95 found_it: len += sprintf(buf+len, "%3d: ", i);
96 #ifndef __SMP__
97 len += sprintf(buf+len, "%10u ", kstat_irqs(i));
98 #else
99 for (x = 0; x < smp_num_cpus; x++)
100 len += sprintf(buf+len, "%10u ",
101 kstat.irqs[cpu_logical_map(x)][i]);
102 #endif
103 len += sprintf(buf+len, "%c %s",
104 (action->flags & SA_INTERRUPT) ? '+' : ' ',
105 action->name);
106 action = action->next;
107 for (;;) {
108 for (; action; action = action->next) {
109 len += sprintf(buf+len, ",%s %s",
110 (action->flags & SA_INTERRUPT) ? " +" : "",
111 action->name);
113 if (!sbusl) break;
114 k++;
115 if (k < 4)
116 action = sbus_actions [(j << 5) + (sbusl << 2) + k].action;
117 else {
118 j++;
119 if (j == nsbi) break;
120 k = 0;
121 action = sbus_actions [(j << 5) + (sbusl << 2)].action;
124 len += sprintf(buf+len, "\n");
126 return len;
129 void sun4d_free_irq(unsigned int irq, void *dev_id)
131 struct irqaction *action, **actionp;
132 struct irqaction *tmp = NULL;
133 unsigned long flags;
135 if (irq < 15)
136 actionp = irq + irq_action;
137 else
138 actionp = &(sbus_actions[irq - (1 << 5)].action);
139 action = *actionp;
140 if (!action) {
141 printk("Trying to free free IRQ%d\n",irq);
142 return;
144 if (dev_id) {
145 for (; action; action = action->next) {
146 if (action->dev_id == dev_id)
147 break;
148 tmp = action;
150 if (!action) {
151 printk("Trying to free free shared IRQ%d\n",irq);
152 return;
154 } else if (action->flags & SA_SHIRQ) {
155 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
156 return;
158 if (action->flags & SA_STATIC_ALLOC)
160 /* This interrupt is marked as specially allocated
161 * so it is a bad idea to free it.
163 printk("Attempt to free statically allocated IRQ%d (%s)\n",
164 irq, action->name);
165 return;
168 save_and_cli(flags);
169 if (action && tmp)
170 tmp->next = action->next;
171 else
172 *actionp = action->next;
174 kfree_s(action, sizeof(struct irqaction));
176 if (!(*actionp))
177 disable_irq(irq);
179 restore_flags(flags);
182 extern void unexpected_irq(int, void *, struct pt_regs *);
184 void sun4d_handler_irq(int irq, struct pt_regs * regs)
186 struct irqaction * action;
187 int cpu = smp_processor_id();
188 /* SBUS IRQ level (1 - 7) */
189 int sbusl = pil_to_sbus[irq];
191 /* FIXME: Is this necessary?? */
192 cc_get_ipen();
194 cc_set_iclr(1 << irq);
196 irq_enter(cpu, irq);
197 kstat.irqs[cpu][irq]++;
198 if (!sbusl) {
199 action = *(irq + irq_action);
200 if (!action)
201 unexpected_irq(irq, 0, regs);
202 do {
203 action->handler(irq, action->dev_id, regs);
204 action = action->next;
205 } while (action);
206 } else {
207 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
208 int sbino;
209 struct sbus_action *actionp;
210 unsigned mask, slot;
211 int sbil = (sbusl << 2);
213 bw_clear_intr_mask(sbusl, bus_mask);
215 /* Loop for each pending SBI */
216 for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
217 if (bus_mask & 1) {
218 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
219 mask &= (0xf << sbil);
220 actionp = sbus_actions + (sbino << 5) + (sbil);
221 /* Loop for each pending SBI slot */
222 for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
223 if (mask & slot) {
224 mask &= ~slot;
225 action = actionp->action;
227 if (!action)
228 unexpected_irq(irq, 0, regs);
229 do {
230 action->handler(irq, action->dev_id, regs);
231 action = action->next;
232 } while (action);
233 release_sbi(SBI2DEVID(sbino), slot);
237 irq_exit(cpu, irq);
240 unsigned int sun4d_build_irq(struct linux_sbus_device *sdev, int irq)
242 int sbusl = pil_to_sbus[irq];
244 if (sbusl)
245 return ((sdev->my_bus->board + 1) << 5) + (sbusl << 2) + sdev->slot;
246 else
247 return irq;
250 int sun4d_request_irq(unsigned int irq,
251 void (*handler)(int, void *, struct pt_regs *),
252 unsigned long irqflags, const char * devname, void *dev_id)
254 struct irqaction *action, *tmp = NULL, **actionp;
255 unsigned long flags;
257 if(irq > 14 && irq < (1 << 5))
258 return -EINVAL;
260 if (!handler)
261 return -EINVAL;
263 if (irq >= (1 << 5))
264 actionp = &(sbus_actions[irq - (1 << 5)].action);
265 else
266 actionp = irq + irq_action;
267 action = *actionp;
269 if (action) {
270 if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
271 for (tmp = action; tmp->next; tmp = tmp->next);
272 } else {
273 return -EBUSY;
275 if ((action->flags & SA_INTERRUPT) ^ (irqflags & SA_INTERRUPT)) {
276 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
277 return -EBUSY;
279 action = NULL; /* Or else! */
282 save_and_cli(flags);
284 /* If this is flagged as statically allocated then we use our
285 * private struct which is never freed.
287 if (irqflags & SA_STATIC_ALLOC) {
288 if (static_irq_count < MAX_STATIC_ALLOC)
289 action = &static_irqaction[static_irq_count++];
290 else
291 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",irq, devname);
294 if (action == NULL)
295 action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
296 GFP_KERNEL);
298 if (!action) {
299 restore_flags(flags);
300 return -ENOMEM;
303 action->handler = handler;
304 action->flags = irqflags;
305 action->mask = 0;
306 action->name = devname;
307 action->next = NULL;
308 action->dev_id = dev_id;
310 if (tmp)
311 tmp->next = action;
312 else
313 *actionp = action;
315 enable_irq(irq);
316 restore_flags(flags);
317 return 0;
320 static void sun4d_disable_irq(unsigned int irq)
322 #ifdef __SMP__
323 int tid = sbus_tid[(irq >> 5) - 1];
324 unsigned long flags;
325 #endif
327 if (irq < NR_IRQS) return;
328 #ifdef __SMP__
329 spin_lock_irqsave(&sun4d_imsk_lock, flags);
330 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
331 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
332 #else
333 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil[(irq >> 2) & 7]));
334 #endif
337 static void sun4d_enable_irq(unsigned int irq)
339 #ifdef __SMP__
340 int tid = sbus_tid[(irq >> 5) - 1];
341 unsigned long flags;
342 #endif
344 if (irq < NR_IRQS) return;
345 #ifdef __SMP__
346 spin_lock_irqsave(&sun4d_imsk_lock, flags);
347 cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
348 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
349 #else
350 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
351 #endif
354 #ifdef __SMP__
355 static void sun4d_set_cpu_int(int cpu, int level)
357 sun4d_send_ipi(cpu, level);
360 static void sun4d_clear_ipi(int cpu, int level)
364 static void sun4d_set_udt(int cpu)
368 /* Setup IRQ distribution scheme. */
369 void __init sun4d_distribute_irqs(void)
371 #ifdef DISTRIBUTE_IRQS
372 struct linux_sbus *sbus;
373 unsigned long sbus_serving_map;
375 sbus_serving_map = cpu_present_map;
376 for_each_sbus(sbus) {
377 if ((sbus->board * 2) == boot_cpu_id && (cpu_present_map & (1 << (sbus->board * 2 + 1))))
378 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
379 else if (cpu_present_map & (1 << (sbus->board * 2)))
380 sbus_tid[sbus->board] = (sbus->board * 2);
381 else if (cpu_present_map & (1 << (sbus->board * 2 + 1)))
382 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
383 else
384 sbus_tid[sbus->board] = 0xff;
385 if (sbus_tid[sbus->board] != 0xff)
386 sbus_serving_map &= ~(1 << sbus_tid[sbus->board]);
388 for_each_sbus(sbus)
389 if (sbus_tid[sbus->board] == 0xff) {
390 int i = 31;
392 if (!sbus_serving_map)
393 sbus_serving_map = cpu_present_map;
394 while (!(sbus_serving_map & (1 << i)))
395 i--;
396 sbus_tid[sbus->board] = i;
397 sbus_serving_map &= ~(1 << i);
399 for_each_sbus(sbus) {
400 printk("sbus%d IRQs directed to CPU%d\n", sbus->board, sbus_tid[sbus->board]);
401 set_sbi_tid(sbus->devid, sbus_tid[sbus->board] << 3);
403 #else
404 struct linux_sbus *sbus;
405 int cpuid = cpu_logical_map(1);
407 if (cpuid == -1)
408 cpuid = cpu_logical_map(0);
409 for_each_sbus(sbus) {
410 sbus_tid[sbus->board] = cpuid;
411 set_sbi_tid(sbus->devid, cpuid << 3);
413 printk("All sbus IRQs directed to CPU%d\n", cpuid);
414 #endif
416 #endif
418 static void sun4d_clear_clock_irq(void)
420 volatile unsigned int clear_intr;
421 clear_intr = sun4d_timers->l10_timer_limit;
424 static void sun4d_clear_profile_irq(int cpu)
426 bw_get_prof_limit(cpu);
429 static void sun4d_load_profile_irq(int cpu, unsigned int limit)
431 bw_set_prof_limit(cpu, limit);
434 static void __init sun4d_init_timers(void (*counter_fn)(int, void *, struct pt_regs *))
436 int irq;
437 extern struct prom_cpuinfo linux_cpus[NR_CPUS];
438 int cpu;
440 /* Map the User Timer registers. */
441 #ifdef __SMP__
442 sun4d_timers = sparc_alloc_io(CSR_BASE(boot_cpu_id)+BW_TIMER_LIMIT, 0,
443 PAGE_SIZE, "user timer", 0xf, 0x0);
444 #else
445 sun4d_timers = sparc_alloc_io(CSR_BASE(0)+BW_TIMER_LIMIT, 0,
446 PAGE_SIZE, "user timer", 0xf, 0x0);
447 #endif
449 sun4d_timers->l10_timer_limit = (((1000000/HZ) + 1) << 10);
450 master_l10_counter = &sun4d_timers->l10_cur_count;
451 master_l10_limit = &sun4d_timers->l10_timer_limit;
453 irq = request_irq(TIMER_IRQ,
454 counter_fn,
455 (SA_INTERRUPT | SA_STATIC_ALLOC),
456 "timer", NULL);
457 if (irq) {
458 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ);
459 prom_halt();
462 /* Enable user timer free run for CPU 0 in BW */
463 /* bw_set_ctrl(0, bw_get_ctrl(0) | BW_CTRL_USER_TIMER); */
465 for(cpu = 0; cpu < linux_num_cpus; cpu++)
466 sun4d_load_profile_irq((linux_cpus[cpu].mid >> 3), 0);
468 #ifdef __SMP__
470 unsigned long flags;
471 extern unsigned long lvl14_save[4];
472 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
473 extern unsigned int real_irq_entry[], smp4d_ticker[];
474 extern unsigned int patchme_maybe_smp_msg[];
476 /* Adjust so that we jump directly to smp4d_ticker */
477 lvl14_save[2] += smp4d_ticker - real_irq_entry;
479 /* For SMP we use the level 14 ticker, however the bootup code
480 * has copied the firmwares level 14 vector into boot cpu's
481 * trap table, we must fix this now or we get squashed.
483 __save_and_cli(flags);
484 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
485 trap_table->inst_one = lvl14_save[0];
486 trap_table->inst_two = lvl14_save[1];
487 trap_table->inst_three = lvl14_save[2];
488 trap_table->inst_four = lvl14_save[3];
489 local_flush_cache_all();
490 __restore_flags(flags);
492 #endif
495 void __init sun4d_init_sbi_irq(void)
497 struct linux_sbus *sbus;
498 unsigned mask;
500 nsbi = 0;
501 for_each_sbus(sbus)
502 nsbi++;
503 sbus_actions = (struct sbus_action *)kmalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
504 memset (sbus_actions, 0, (nsbi * 8 * 4 * sizeof(struct sbus_action)));
505 for_each_sbus(sbus) {
506 #ifdef __SMP__
507 extern unsigned char boot_cpu_id;
509 set_sbi_tid(sbus->devid, boot_cpu_id << 3);
510 sbus_tid[sbus->board] = boot_cpu_id;
511 #endif
512 /* Get rid of pending irqs from PROM */
513 mask = acquire_sbi(sbus->devid, 0xffffffff);
514 if (mask) {
515 printk ("Clearing pending IRQs %08x on SBI %d\n", mask, sbus->board);
516 release_sbi(sbus->devid, mask);
521 static char *sun4d_irq_itoa(unsigned int irq)
523 static char buff[16];
525 if (irq < (1 << 5))
526 sprintf(buff, "%d", irq);
527 else
528 sprintf(buff, "%d,%x", sbus_to_pil[(irq >> 2) & 7], irq);
529 return buff;
532 void __init sun4d_init_IRQ(void)
534 __cli();
536 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
537 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
538 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
539 BTFIXUPSET_CALL(clear_profile_irq, sun4d_clear_profile_irq, BTFIXUPCALL_NORM);
540 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
541 BTFIXUPSET_CALL(__irq_itoa, sun4d_irq_itoa, BTFIXUPCALL_NORM);
542 init_timers = sun4d_init_timers;
543 #ifdef __SMP__
544 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
545 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
546 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
547 #endif
548 /* Cannot enable interrupts until OBP ticker is disabled. */