Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / sparc / kernel / sun4d_irq.c
blob4ce99b06b2b0ac7c51351fbb805049ba2f27f93d
1 /* $Id: sun4d_irq.c,v 1.26 2000/07/26 01:04:03 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/config.h>
10 #include <linux/ptrace.h>
11 #include <linux/errno.h>
12 #include <linux/linkage.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/malloc.h>
18 #include <linux/random.h>
19 #include <linux/init.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/spinlock.h>
24 #include <asm/ptrace.h>
25 #include <asm/processor.h>
26 #include <asm/system.h>
27 #include <asm/psr.h>
28 #include <asm/smp.h>
29 #include <asm/vaddrs.h>
30 #include <asm/timer.h>
31 #include <asm/openprom.h>
32 #include <asm/oplib.h>
33 #include <asm/traps.h>
34 #include <asm/irq.h>
35 #include <asm/io.h>
36 #include <asm/pgalloc.h>
37 #include <asm/pgtable.h>
38 #include <asm/sbus.h>
39 #include <asm/sbi.h>
41 /* If you trust current SCSI layer to handle different SCSI IRQs, enable this. I don't trust it... -jj */
42 /* #define DISTRIBUTE_IRQS */
44 struct sun4d_timer_regs *sun4d_timers;
45 #define TIMER_IRQ 10
47 #define MAX_STATIC_ALLOC 4
48 extern struct irqaction static_irqaction[MAX_STATIC_ALLOC];
49 extern int static_irq_count;
50 unsigned char cpu_leds[32];
51 #ifdef CONFIG_SMP
52 unsigned char sbus_tid[32];
53 #endif
55 extern struct irqaction *irq_action[];
57 struct sbus_action {
58 struct irqaction *action;
59 /* For SMP this needs to be extended */
60 } *sbus_actions;
62 static int pil_to_sbus[] = {
63 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
66 static int sbus_to_pil[] = {
67 0, 2, 3, 5, 7, 9, 11, 13,
70 static int nsbi;
71 #ifdef CONFIG_SMP
72 spinlock_t sun4d_imsk_lock = SPIN_LOCK_UNLOCKED;
73 #endif
75 int sun4d_get_irq_list(char *buf)
77 int i, j = 0, k = 0, len = 0, sbusl;
78 struct irqaction * action;
79 #ifdef CONFIG_SMP
80 int x;
81 #endif
83 for (i = 0 ; i < NR_IRQS ; i++) {
84 sbusl = pil_to_sbus[i];
85 if (!sbusl) {
86 action = *(i + irq_action);
87 if (!action)
88 continue;
89 } else {
90 for (j = 0; j < nsbi; j++) {
91 for (k = 0; k < 4; k++)
92 if ((action = sbus_actions [(j << 5) + (sbusl << 2) + k].action))
93 goto found_it;
95 continue;
97 found_it: len += sprintf(buf+len, "%3d: ", i);
98 #ifndef CONFIG_SMP
99 len += sprintf(buf+len, "%10u ", kstat_irqs(i));
100 #else
101 for (x = 0; x < smp_num_cpus; x++)
102 len += sprintf(buf+len, "%10u ",
103 kstat.irqs[cpu_logical_map(x)][i]);
104 #endif
105 len += sprintf(buf+len, "%c %s",
106 (action->flags & SA_INTERRUPT) ? '+' : ' ',
107 action->name);
108 action = action->next;
109 for (;;) {
110 for (; action; action = action->next) {
111 len += sprintf(buf+len, ",%s %s",
112 (action->flags & SA_INTERRUPT) ? " +" : "",
113 action->name);
115 if (!sbusl) break;
116 k++;
117 if (k < 4)
118 action = sbus_actions [(j << 5) + (sbusl << 2) + k].action;
119 else {
120 j++;
121 if (j == nsbi) break;
122 k = 0;
123 action = sbus_actions [(j << 5) + (sbusl << 2)].action;
126 len += sprintf(buf+len, "\n");
128 return len;
131 void sun4d_free_irq(unsigned int irq, void *dev_id)
133 struct irqaction *action, **actionp;
134 struct irqaction *tmp = NULL;
135 unsigned long flags;
137 if (irq < 15)
138 actionp = irq + irq_action;
139 else
140 actionp = &(sbus_actions[irq - (1 << 5)].action);
141 action = *actionp;
142 if (!action) {
143 printk("Trying to free free IRQ%d\n",irq);
144 return;
146 if (dev_id) {
147 for (; action; action = action->next) {
148 if (action->dev_id == dev_id)
149 break;
150 tmp = action;
152 if (!action) {
153 printk("Trying to free free shared IRQ%d\n",irq);
154 return;
156 } else if (action->flags & SA_SHIRQ) {
157 printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
158 return;
160 if (action->flags & SA_STATIC_ALLOC)
162 /* This interrupt is marked as specially allocated
163 * so it is a bad idea to free it.
165 printk("Attempt to free statically allocated IRQ%d (%s)\n",
166 irq, action->name);
167 return;
170 save_and_cli(flags);
171 if (action && tmp)
172 tmp->next = action->next;
173 else
174 *actionp = action->next;
176 kfree(action);
178 if (!(*actionp))
179 disable_irq(irq);
181 restore_flags(flags);
184 extern void unexpected_irq(int, void *, struct pt_regs *);
186 void sun4d_handler_irq(int irq, struct pt_regs * regs)
188 struct irqaction * action;
189 int cpu = smp_processor_id();
190 /* SBUS IRQ level (1 - 7) */
191 int sbusl = pil_to_sbus[irq];
193 /* FIXME: Is this necessary?? */
194 cc_get_ipen();
196 cc_set_iclr(1 << irq);
198 irq_enter(cpu, irq);
199 kstat.irqs[cpu][irq]++;
200 if (!sbusl) {
201 action = *(irq + irq_action);
202 if (!action)
203 unexpected_irq(irq, 0, regs);
204 do {
205 action->handler(irq, action->dev_id, regs);
206 action = action->next;
207 } while (action);
208 } else {
209 int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
210 int sbino;
211 struct sbus_action *actionp;
212 unsigned mask, slot;
213 int sbil = (sbusl << 2);
215 bw_clear_intr_mask(sbusl, bus_mask);
217 /* Loop for each pending SBI */
218 for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
219 if (bus_mask & 1) {
220 mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
221 mask &= (0xf << sbil);
222 actionp = sbus_actions + (sbino << 5) + (sbil);
223 /* Loop for each pending SBI slot */
224 for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
225 if (mask & slot) {
226 mask &= ~slot;
227 action = actionp->action;
229 if (!action)
230 unexpected_irq(irq, 0, regs);
231 do {
232 action->handler(irq, action->dev_id, regs);
233 action = action->next;
234 } while (action);
235 release_sbi(SBI2DEVID(sbino), slot);
239 irq_exit(cpu, irq);
242 unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq)
244 int sbusl = pil_to_sbus[irq];
246 if (sbusl)
247 return ((sdev->bus->board + 1) << 5) + (sbusl << 2) + sdev->slot;
248 else
249 return irq;
252 int sun4d_request_irq(unsigned int irq,
253 void (*handler)(int, void *, struct pt_regs *),
254 unsigned long irqflags, const char * devname, void *dev_id)
256 struct irqaction *action, *tmp = NULL, **actionp;
257 unsigned long flags;
259 if(irq > 14 && irq < (1 << 5))
260 return -EINVAL;
262 if (!handler)
263 return -EINVAL;
265 if (irq >= (1 << 5))
266 actionp = &(sbus_actions[irq - (1 << 5)].action);
267 else
268 actionp = irq + irq_action;
269 action = *actionp;
271 if (action) {
272 if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
273 for (tmp = action; tmp->next; tmp = tmp->next);
274 } else {
275 return -EBUSY;
277 if ((action->flags & SA_INTERRUPT) ^ (irqflags & SA_INTERRUPT)) {
278 printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
279 return -EBUSY;
281 action = NULL; /* Or else! */
284 save_and_cli(flags);
286 /* If this is flagged as statically allocated then we use our
287 * private struct which is never freed.
289 if (irqflags & SA_STATIC_ALLOC) {
290 if (static_irq_count < MAX_STATIC_ALLOC)
291 action = &static_irqaction[static_irq_count++];
292 else
293 printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",irq, devname);
296 if (action == NULL)
297 action = (struct irqaction *)kmalloc(sizeof(struct irqaction),
298 GFP_KERNEL);
300 if (!action) {
301 restore_flags(flags);
302 return -ENOMEM;
305 action->handler = handler;
306 action->flags = irqflags;
307 action->mask = 0;
308 action->name = devname;
309 action->next = NULL;
310 action->dev_id = dev_id;
312 if (tmp)
313 tmp->next = action;
314 else
315 *actionp = action;
317 enable_irq(irq);
318 restore_flags(flags);
319 return 0;
322 static void sun4d_disable_irq(unsigned int irq)
324 #ifdef CONFIG_SMP
325 int tid = sbus_tid[(irq >> 5) - 1];
326 unsigned long flags;
327 #endif
329 if (irq < NR_IRQS) return;
330 #ifdef CONFIG_SMP
331 spin_lock_irqsave(&sun4d_imsk_lock, flags);
332 cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
333 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
334 #else
335 cc_set_imsk(cc_get_imsk() | (1 << sbus_to_pil[(irq >> 2) & 7]));
336 #endif
339 static void sun4d_enable_irq(unsigned int irq)
341 #ifdef CONFIG_SMP
342 int tid = sbus_tid[(irq >> 5) - 1];
343 unsigned long flags;
344 #endif
346 if (irq < NR_IRQS) return;
347 #ifdef CONFIG_SMP
348 spin_lock_irqsave(&sun4d_imsk_lock, flags);
349 cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
350 spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
351 #else
352 cc_set_imsk(cc_get_imsk() & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
353 #endif
356 #ifdef CONFIG_SMP
357 static void sun4d_set_cpu_int(int cpu, int level)
359 sun4d_send_ipi(cpu, level);
362 static void sun4d_clear_ipi(int cpu, int level)
366 static void sun4d_set_udt(int cpu)
370 /* Setup IRQ distribution scheme. */
371 void __init sun4d_distribute_irqs(void)
373 #ifdef DISTRIBUTE_IRQS
374 struct sbus_bus *sbus;
375 unsigned long sbus_serving_map;
377 sbus_serving_map = cpu_present_map;
378 for_each_sbus(sbus) {
379 if ((sbus->board * 2) == boot_cpu_id && (cpu_present_map & (1 << (sbus->board * 2 + 1))))
380 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
381 else if (cpu_present_map & (1 << (sbus->board * 2)))
382 sbus_tid[sbus->board] = (sbus->board * 2);
383 else if (cpu_present_map & (1 << (sbus->board * 2 + 1)))
384 sbus_tid[sbus->board] = (sbus->board * 2 + 1);
385 else
386 sbus_tid[sbus->board] = 0xff;
387 if (sbus_tid[sbus->board] != 0xff)
388 sbus_serving_map &= ~(1 << sbus_tid[sbus->board]);
390 for_each_sbus(sbus)
391 if (sbus_tid[sbus->board] == 0xff) {
392 int i = 31;
394 if (!sbus_serving_map)
395 sbus_serving_map = cpu_present_map;
396 while (!(sbus_serving_map & (1 << i)))
397 i--;
398 sbus_tid[sbus->board] = i;
399 sbus_serving_map &= ~(1 << i);
401 for_each_sbus(sbus) {
402 printk("sbus%d IRQs directed to CPU%d\n", sbus->board, sbus_tid[sbus->board]);
403 set_sbi_tid(sbus->devid, sbus_tid[sbus->board] << 3);
405 #else
406 struct sbus_bus *sbus;
407 int cpuid = cpu_logical_map(1);
409 if (cpuid == -1)
410 cpuid = cpu_logical_map(0);
411 for_each_sbus(sbus) {
412 sbus_tid[sbus->board] = cpuid;
413 set_sbi_tid(sbus->devid, cpuid << 3);
415 printk("All sbus IRQs directed to CPU%d\n", cpuid);
416 #endif
418 #endif
420 static void sun4d_clear_clock_irq(void)
422 volatile unsigned int clear_intr;
423 clear_intr = sun4d_timers->l10_timer_limit;
426 static void sun4d_clear_profile_irq(int cpu)
428 bw_get_prof_limit(cpu);
431 static void sun4d_load_profile_irq(int cpu, unsigned int limit)
433 bw_set_prof_limit(cpu, limit);
436 static void __init sun4d_init_timers(void (*counter_fn)(int, void *, struct pt_regs *))
438 int irq;
439 extern struct prom_cpuinfo linux_cpus[NR_CPUS];
440 int cpu;
441 struct resource r;
443 /* Map the User Timer registers. */
444 memset(&r, 0, sizeof(r));
445 #ifdef CONFIG_SMP
446 r.start = CSR_BASE(boot_cpu_id)+BW_TIMER_LIMIT;
447 #else
448 r.start = CSR_BASE(0)+BW_TIMER_LIMIT;
449 #endif
450 r.flags = 0xf;
451 sun4d_timers = (struct sun4d_timer_regs *) sbus_ioremap(&r, 0,
452 PAGE_SIZE, "user timer");
454 sun4d_timers->l10_timer_limit = (((1000000/HZ) + 1) << 10);
455 master_l10_counter = &sun4d_timers->l10_cur_count;
456 master_l10_limit = &sun4d_timers->l10_timer_limit;
458 irq = request_irq(TIMER_IRQ,
459 counter_fn,
460 (SA_INTERRUPT | SA_STATIC_ALLOC),
461 "timer", NULL);
462 if (irq) {
463 prom_printf("time_init: unable to attach IRQ%d\n",TIMER_IRQ);
464 prom_halt();
467 /* Enable user timer free run for CPU 0 in BW */
468 /* bw_set_ctrl(0, bw_get_ctrl(0) | BW_CTRL_USER_TIMER); */
470 for(cpu = 0; cpu < linux_num_cpus; cpu++)
471 sun4d_load_profile_irq((linux_cpus[cpu].mid >> 3), 0);
473 #ifdef CONFIG_SMP
475 unsigned long flags;
476 extern unsigned long lvl14_save[4];
477 struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
478 extern unsigned int real_irq_entry[], smp4d_ticker[];
479 extern unsigned int patchme_maybe_smp_msg[];
481 /* Adjust so that we jump directly to smp4d_ticker */
482 lvl14_save[2] += smp4d_ticker - real_irq_entry;
484 /* For SMP we use the level 14 ticker, however the bootup code
485 * has copied the firmwares level 14 vector into boot cpu's
486 * trap table, we must fix this now or we get squashed.
488 __save_and_cli(flags);
489 patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
490 trap_table->inst_one = lvl14_save[0];
491 trap_table->inst_two = lvl14_save[1];
492 trap_table->inst_three = lvl14_save[2];
493 trap_table->inst_four = lvl14_save[3];
494 local_flush_cache_all();
495 __restore_flags(flags);
497 #endif
500 void __init sun4d_init_sbi_irq(void)
502 struct sbus_bus *sbus;
503 unsigned mask;
505 nsbi = 0;
506 for_each_sbus(sbus)
507 nsbi++;
508 sbus_actions = (struct sbus_action *)kmalloc (nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
509 memset (sbus_actions, 0, (nsbi * 8 * 4 * sizeof(struct sbus_action)));
510 for_each_sbus(sbus) {
511 #ifdef CONFIG_SMP
512 extern unsigned char boot_cpu_id;
514 set_sbi_tid(sbus->devid, boot_cpu_id << 3);
515 sbus_tid[sbus->board] = boot_cpu_id;
516 #endif
517 /* Get rid of pending irqs from PROM */
518 mask = acquire_sbi(sbus->devid, 0xffffffff);
519 if (mask) {
520 printk ("Clearing pending IRQs %08x on SBI %d\n", mask, sbus->board);
521 release_sbi(sbus->devid, mask);
526 static char *sun4d_irq_itoa(unsigned int irq)
528 static char buff[16];
530 if (irq < (1 << 5))
531 sprintf(buff, "%d", irq);
532 else
533 sprintf(buff, "%d,%x", sbus_to_pil[(irq >> 2) & 7], irq);
534 return buff;
537 void __init sun4d_init_IRQ(void)
539 __cli();
541 BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
542 BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
543 BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
544 BTFIXUPSET_CALL(clear_profile_irq, sun4d_clear_profile_irq, BTFIXUPCALL_NORM);
545 BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
546 BTFIXUPSET_CALL(__irq_itoa, sun4d_irq_itoa, BTFIXUPCALL_NORM);
547 init_timers = sun4d_init_timers;
548 #ifdef CONFIG_SMP
549 BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
550 BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
551 BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
552 #endif
553 /* Cannot enable interrupts until OBP ticker is disabled. */